]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/match.pd
MATCH: Fix infinite loop between `vec_cond(vec_cond(a,b,0), c, d)` and `a & b`
[thirdparty/gcc.git] / gcc / match.pd
1 /* Match-and-simplify patterns for shared GENERIC and GIMPLE folding.
2 This file is consumed by genmatch which produces gimple-match.cc
3 and generic-match.cc from it.
4
5 Copyright (C) 2014-2023 Free Software Foundation, Inc.
6 Contributed by Richard Biener <rguenther@suse.de>
7 and Prathamesh Kulkarni <bilbotheelffriend@gmail.com>
8
9 This file is part of GCC.
10
11 GCC is free software; you can redistribute it and/or modify it under
12 the terms of the GNU General Public License as published by the Free
13 Software Foundation; either version 3, or (at your option) any later
14 version.
15
16 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
17 WARRANTY; without even the implied warranty of MERCHANTABILITY or
18 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19 for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with GCC; see the file COPYING3. If not see
23 <http://www.gnu.org/licenses/>. */
24
25
26 /* Generic tree predicates we inherit. */
27 (define_predicates
28 integer_onep integer_zerop integer_all_onesp integer_minus_onep
29 integer_each_onep integer_truep integer_nonzerop
30 real_zerop real_onep real_minus_onep
31 zerop
32 initializer_each_zero_or_onep
33 CONSTANT_CLASS_P
34 tree_expr_nonnegative_p
35 tree_expr_nonzero_p
36 integer_valued_real_p
37 integer_pow2p
38 uniform_integer_cst_p
39 HONOR_NANS
40 uniform_vector_p
41 expand_vec_cmp_expr_p
42 bitmask_inv_cst_vector_p)
43
44 /* Operator lists. */
45 (define_operator_list tcc_comparison
46 lt le eq ne ge gt unordered ordered unlt unle ungt unge uneq ltgt)
47 (define_operator_list inverted_tcc_comparison
48 ge gt ne eq lt le ordered unordered ge gt le lt ltgt uneq)
49 (define_operator_list inverted_tcc_comparison_with_nans
50 unge ungt ne eq unlt unle ordered unordered ge gt le lt ltgt uneq)
51 (define_operator_list swapped_tcc_comparison
52 gt ge eq ne le lt unordered ordered ungt unge unlt unle uneq ltgt)
53 (define_operator_list simple_comparison lt le eq ne ge gt)
54 (define_operator_list swapped_simple_comparison gt ge eq ne le lt)
55 (define_operator_list BSWAP BUILT_IN_BSWAP16 BUILT_IN_BSWAP32
56 BUILT_IN_BSWAP64 BUILT_IN_BSWAP128)
57
58 #include "cfn-operators.pd"
59
60 /* Define operand lists for math rounding functions {,i,l,ll}FN,
61 where the versions prefixed with "i" return an int, those prefixed with
62 "l" return a long and those prefixed with "ll" return a long long.
63
64 Also define operand lists:
65
66 X<FN>F for all float functions, in the order i, l, ll
67 X<FN> for all double functions, in the same order
68 X<FN>L for all long double functions, in the same order. */
69 #define DEFINE_INT_AND_FLOAT_ROUND_FN(FN) \
70 (define_operator_list X##FN##F BUILT_IN_I##FN##F \
71 BUILT_IN_L##FN##F \
72 BUILT_IN_LL##FN##F) \
73 (define_operator_list X##FN BUILT_IN_I##FN \
74 BUILT_IN_L##FN \
75 BUILT_IN_LL##FN) \
76 (define_operator_list X##FN##L BUILT_IN_I##FN##L \
77 BUILT_IN_L##FN##L \
78 BUILT_IN_LL##FN##L)
79
80 DEFINE_INT_AND_FLOAT_ROUND_FN (FLOOR)
81 DEFINE_INT_AND_FLOAT_ROUND_FN (CEIL)
82 DEFINE_INT_AND_FLOAT_ROUND_FN (ROUND)
83 DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
84
85 /* Unary operations and their associated IFN_COND_* function. */
86 (define_operator_list UNCOND_UNARY
87 negate bit_not)
88 (define_operator_list COND_UNARY
89 IFN_COND_NEG IFN_COND_NOT)
90
91 /* Binary operations and their associated IFN_COND_* function. */
92 (define_operator_list UNCOND_BINARY
93 plus minus
94 mult trunc_div trunc_mod rdiv
95 min max
96 IFN_FMIN IFN_FMAX
97 bit_and bit_ior bit_xor
98 lshift rshift)
99 (define_operator_list COND_BINARY
100 IFN_COND_ADD IFN_COND_SUB
101 IFN_COND_MUL IFN_COND_DIV IFN_COND_MOD IFN_COND_RDIV
102 IFN_COND_MIN IFN_COND_MAX
103 IFN_COND_FMIN IFN_COND_FMAX
104 IFN_COND_AND IFN_COND_IOR IFN_COND_XOR
105 IFN_COND_SHL IFN_COND_SHR)
106
107 /* Same for ternary operations. */
108 (define_operator_list UNCOND_TERNARY
109 IFN_FMA IFN_FMS IFN_FNMA IFN_FNMS)
110 (define_operator_list COND_TERNARY
111 IFN_COND_FMA IFN_COND_FMS IFN_COND_FNMA IFN_COND_FNMS)
112
113 /* __atomic_fetch_or_*, __atomic_fetch_xor_*, __atomic_xor_fetch_* */
114 (define_operator_list ATOMIC_FETCH_OR_XOR_N
115 BUILT_IN_ATOMIC_FETCH_OR_1 BUILT_IN_ATOMIC_FETCH_OR_2
116 BUILT_IN_ATOMIC_FETCH_OR_4 BUILT_IN_ATOMIC_FETCH_OR_8
117 BUILT_IN_ATOMIC_FETCH_OR_16
118 BUILT_IN_ATOMIC_FETCH_XOR_1 BUILT_IN_ATOMIC_FETCH_XOR_2
119 BUILT_IN_ATOMIC_FETCH_XOR_4 BUILT_IN_ATOMIC_FETCH_XOR_8
120 BUILT_IN_ATOMIC_FETCH_XOR_16
121 BUILT_IN_ATOMIC_XOR_FETCH_1 BUILT_IN_ATOMIC_XOR_FETCH_2
122 BUILT_IN_ATOMIC_XOR_FETCH_4 BUILT_IN_ATOMIC_XOR_FETCH_8
123 BUILT_IN_ATOMIC_XOR_FETCH_16)
124 /* __sync_fetch_and_or_*, __sync_fetch_and_xor_*, __sync_xor_and_fetch_* */
125 (define_operator_list SYNC_FETCH_OR_XOR_N
126 BUILT_IN_SYNC_FETCH_AND_OR_1 BUILT_IN_SYNC_FETCH_AND_OR_2
127 BUILT_IN_SYNC_FETCH_AND_OR_4 BUILT_IN_SYNC_FETCH_AND_OR_8
128 BUILT_IN_SYNC_FETCH_AND_OR_16
129 BUILT_IN_SYNC_FETCH_AND_XOR_1 BUILT_IN_SYNC_FETCH_AND_XOR_2
130 BUILT_IN_SYNC_FETCH_AND_XOR_4 BUILT_IN_SYNC_FETCH_AND_XOR_8
131 BUILT_IN_SYNC_FETCH_AND_XOR_16
132 BUILT_IN_SYNC_XOR_AND_FETCH_1 BUILT_IN_SYNC_XOR_AND_FETCH_2
133 BUILT_IN_SYNC_XOR_AND_FETCH_4 BUILT_IN_SYNC_XOR_AND_FETCH_8
134 BUILT_IN_SYNC_XOR_AND_FETCH_16)
135 /* __atomic_fetch_and_*. */
136 (define_operator_list ATOMIC_FETCH_AND_N
137 BUILT_IN_ATOMIC_FETCH_AND_1 BUILT_IN_ATOMIC_FETCH_AND_2
138 BUILT_IN_ATOMIC_FETCH_AND_4 BUILT_IN_ATOMIC_FETCH_AND_8
139 BUILT_IN_ATOMIC_FETCH_AND_16)
140 /* __sync_fetch_and_and_*. */
141 (define_operator_list SYNC_FETCH_AND_AND_N
142 BUILT_IN_SYNC_FETCH_AND_AND_1 BUILT_IN_SYNC_FETCH_AND_AND_2
143 BUILT_IN_SYNC_FETCH_AND_AND_4 BUILT_IN_SYNC_FETCH_AND_AND_8
144 BUILT_IN_SYNC_FETCH_AND_AND_16)
145
146 /* With nop_convert? combine convert? and view_convert? in one pattern
147 plus conditionalize on tree_nop_conversion_p conversions. */
148 (match (nop_convert @0)
149 (convert @0)
150 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))))
151 (match (nop_convert @0)
152 (view_convert @0)
153 (if (VECTOR_TYPE_P (type) && VECTOR_TYPE_P (TREE_TYPE (@0))
154 && known_eq (TYPE_VECTOR_SUBPARTS (type),
155 TYPE_VECTOR_SUBPARTS (TREE_TYPE (@0)))
156 && tree_nop_conversion_p (TREE_TYPE (type), TREE_TYPE (TREE_TYPE (@0))))))
157
158 #if GIMPLE
159 /* These are used by gimple_bitwise_inverted_equal_p to simplify
160 detection of BIT_NOT and comparisons. */
161 (match (bit_not_with_nop @0)
162 (bit_not @0))
163 (match (bit_not_with_nop @0)
164 (convert (bit_not @0))
165 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))))
166 (for cmp (tcc_comparison)
167 (match (maybe_cmp @0)
168 (cmp@0 @1 @2))
169 (match (maybe_cmp @0)
170 (convert (cmp@0 @1 @2))
171 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))))
172 )
173 #endif
174
175 /* Transform likes of (char) ABS_EXPR <(int) x> into (char) ABSU_EXPR <x>
176 ABSU_EXPR returns unsigned absolute value of the operand and the operand
177 of the ABSU_EXPR will have the corresponding signed type. */
178 (simplify (abs (convert @0))
179 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
180 && !TYPE_UNSIGNED (TREE_TYPE (@0))
181 && element_precision (type) > element_precision (TREE_TYPE (@0)))
182 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
183 (convert (absu:utype @0)))))
184
185 #if GIMPLE
186 /* Optimize (X + (X >> (prec - 1))) ^ (X >> (prec - 1)) into abs (X). */
187 (simplify
188 (bit_xor:c (plus:c @0 (rshift@2 @0 INTEGER_CST@1)) @2)
189 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
190 && !TYPE_UNSIGNED (TREE_TYPE (@0))
191 && wi::to_widest (@1) == element_precision (TREE_TYPE (@0)) - 1)
192 (abs @0)))
193 #endif
194
195 /* Simplifications of operations with one constant operand and
196 simplifications to constants or single values. */
197
198 (for op (plus pointer_plus minus bit_ior bit_xor)
199 (simplify
200 (op @0 integer_zerop)
201 (non_lvalue @0)))
202
203 /* 0 +p index -> (type)index */
204 (simplify
205 (pointer_plus integer_zerop @1)
206 (non_lvalue (convert @1)))
207
208 /* ptr - 0 -> (type)ptr */
209 (simplify
210 (pointer_diff @0 integer_zerop)
211 (convert @0))
212
213 /* See if ARG1 is zero and X + ARG1 reduces to X.
214 Likewise if the operands are reversed. */
215 (simplify
216 (plus:c @0 real_zerop@1)
217 (if (fold_real_zero_addition_p (type, @0, @1, 0))
218 (non_lvalue @0)))
219
220 /* See if ARG1 is zero and X - ARG1 reduces to X. */
221 (simplify
222 (minus @0 real_zerop@1)
223 (if (fold_real_zero_addition_p (type, @0, @1, 1))
224 (non_lvalue @0)))
225
226 /* Even if the fold_real_zero_addition_p can't simplify X + 0.0
227 into X, we can optimize (X + 0.0) + 0.0 or (X + 0.0) - 0.0
228 or (X - 0.0) + 0.0 into X + 0.0 and (X - 0.0) - 0.0 into X - 0.0
229 if not -frounding-math. For sNaNs the first operation would raise
230 exceptions but turn the result into qNan, so the second operation
231 would not raise it. */
232 (for inner_op (plus minus)
233 (for outer_op (plus minus)
234 (simplify
235 (outer_op (inner_op@3 @0 REAL_CST@1) REAL_CST@2)
236 (if (real_zerop (@1)
237 && real_zerop (@2)
238 && !HONOR_SIGN_DEPENDENT_ROUNDING (type))
239 (with { bool inner_plus = ((inner_op == PLUS_EXPR)
240 ^ REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@1)));
241 bool outer_plus
242 = ((outer_op == PLUS_EXPR)
243 ^ REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@2))); }
244 (if (outer_plus && !inner_plus)
245 (outer_op @0 @2)
246 @3))))))
247
248 /* Simplify x - x.
249 This is unsafe for certain floats even in non-IEEE formats.
250 In IEEE, it is unsafe because it does wrong for NaNs.
251 PR middle-end/98420: x - x may be -0.0 with FE_DOWNWARD.
252 Also note that operand_equal_p is always false if an operand
253 is volatile. */
254 (simplify
255 (minus @0 @0)
256 (if (!FLOAT_TYPE_P (type)
257 || (!tree_expr_maybe_nan_p (@0)
258 && !tree_expr_maybe_infinite_p (@0)
259 && (!HONOR_SIGN_DEPENDENT_ROUNDING (type)
260 || !HONOR_SIGNED_ZEROS (type))))
261 { build_zero_cst (type); }))
262 (simplify
263 (pointer_diff @@0 @0)
264 { build_zero_cst (type); })
265
266 (simplify
267 (mult @0 integer_zerop@1)
268 @1)
269
270 /* -x == x -> x == 0 */
271 (for cmp (eq ne)
272 (simplify
273 (cmp:c @0 (negate @0))
274 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
275 && !TYPE_OVERFLOW_WRAPS (TREE_TYPE(@0)))
276 (cmp @0 { build_zero_cst (TREE_TYPE(@0)); }))))
277
278 /* Maybe fold x * 0 to 0. The expressions aren't the same
279 when x is NaN, since x * 0 is also NaN. Nor are they the
280 same in modes with signed zeros, since multiplying a
281 negative value by 0 gives -0, not +0. Nor when x is +-Inf,
282 since x * 0 is NaN. */
283 (simplify
284 (mult @0 real_zerop@1)
285 (if (!tree_expr_maybe_nan_p (@0)
286 && (!HONOR_NANS (type) || !tree_expr_maybe_infinite_p (@0))
287 && (!HONOR_SIGNED_ZEROS (type) || tree_expr_nonnegative_p (@0)))
288 @1))
289
290 /* In IEEE floating point, x*1 is not equivalent to x for snans.
291 Likewise for complex arithmetic with signed zeros. */
292 (simplify
293 (mult @0 real_onep)
294 (if (!tree_expr_maybe_signaling_nan_p (@0)
295 && (!HONOR_SIGNED_ZEROS (type)
296 || !COMPLEX_FLOAT_TYPE_P (type)))
297 (non_lvalue @0)))
298
299 /* Transform x * -1.0 into -x. */
300 (simplify
301 (mult @0 real_minus_onep)
302 (if (!tree_expr_maybe_signaling_nan_p (@0)
303 && (!HONOR_SIGNED_ZEROS (type)
304 || !COMPLEX_FLOAT_TYPE_P (type)))
305 (negate @0)))
306
307 /* Transform x * { 0 or 1, 0 or 1, ... } into x & { 0 or -1, 0 or -1, ...},
308 unless the target has native support for the former but not the latter. */
309 (simplify
310 (mult @0 VECTOR_CST@1)
311 (if (initializer_each_zero_or_onep (@1)
312 && !HONOR_SNANS (type)
313 && !HONOR_SIGNED_ZEROS (type))
314 (with { tree itype = FLOAT_TYPE_P (type) ? unsigned_type_for (type) : type; }
315 (if (itype
316 && (!VECTOR_MODE_P (TYPE_MODE (type))
317 || (VECTOR_MODE_P (TYPE_MODE (itype))
318 && optab_handler (and_optab,
319 TYPE_MODE (itype)) != CODE_FOR_nothing)))
320 (view_convert (bit_and:itype (view_convert @0)
321 (ne @1 { build_zero_cst (type); })))))))
322
323 /* In SWAR (SIMD within a register) code a signed comparison of packed data
324 can be constructed with a particular combination of shift, bitwise and,
325 and multiplication by constants. If that code is vectorized we can
326 convert this pattern into a more efficient vector comparison. */
327 (simplify
328 (mult (bit_and (rshift @0 uniform_integer_cst_p@1)
329 uniform_integer_cst_p@2)
330 uniform_integer_cst_p@3)
331 (with {
332 tree rshift_cst = uniform_integer_cst_p (@1);
333 tree bit_and_cst = uniform_integer_cst_p (@2);
334 tree mult_cst = uniform_integer_cst_p (@3);
335 }
336 /* Make sure we're working with vectors and uniform vector constants. */
337 (if (VECTOR_TYPE_P (type)
338 && tree_fits_uhwi_p (rshift_cst)
339 && tree_fits_uhwi_p (mult_cst)
340 && tree_fits_uhwi_p (bit_and_cst))
341 /* Compute what constants would be needed for this to represent a packed
342 comparison based on the shift amount denoted by RSHIFT_CST. */
343 (with {
344 HOST_WIDE_INT vec_elem_bits = vector_element_bits (type);
345 poly_int64 vec_nelts = TYPE_VECTOR_SUBPARTS (type);
346 poly_int64 vec_bits = vec_elem_bits * vec_nelts;
347 unsigned HOST_WIDE_INT cmp_bits_i, bit_and_i, mult_i;
348 unsigned HOST_WIDE_INT target_mult_i, target_bit_and_i;
349 cmp_bits_i = tree_to_uhwi (rshift_cst) + 1;
350 mult_i = tree_to_uhwi (mult_cst);
351 target_mult_i = (HOST_WIDE_INT_1U << cmp_bits_i) - 1;
352 bit_and_i = tree_to_uhwi (bit_and_cst);
353 target_bit_and_i = 0;
354
355 /* The bit pattern in BIT_AND_I should be a mask for the least
356 significant bit of each packed element that is CMP_BITS wide. */
357 for (unsigned i = 0; i < vec_elem_bits / cmp_bits_i; i++)
358 target_bit_and_i = (target_bit_and_i << cmp_bits_i) | 1U;
359 }
360 (if ((exact_log2 (cmp_bits_i)) >= 0
361 && cmp_bits_i < HOST_BITS_PER_WIDE_INT
362 && multiple_p (vec_bits, cmp_bits_i)
363 && vec_elem_bits <= HOST_BITS_PER_WIDE_INT
364 && target_mult_i == mult_i
365 && target_bit_and_i == bit_and_i)
366 /* Compute the vector shape for the comparison and check if the target is
367 able to expand the comparison with that type. */
368 (with {
369 /* We're doing a signed comparison. */
370 tree cmp_type = build_nonstandard_integer_type (cmp_bits_i, 0);
371 poly_int64 vector_type_nelts = exact_div (vec_bits, cmp_bits_i);
372 tree vec_cmp_type = build_vector_type (cmp_type, vector_type_nelts);
373 tree vec_truth_type = truth_type_for (vec_cmp_type);
374 tree zeros = build_zero_cst (vec_cmp_type);
375 tree ones = build_all_ones_cst (vec_cmp_type);
376 }
377 (if (expand_vec_cmp_expr_p (vec_cmp_type, vec_truth_type, LT_EXPR)
378 && expand_vec_cond_expr_p (vec_cmp_type, vec_truth_type, LT_EXPR))
379 (view_convert:type (vec_cond (lt:vec_truth_type
380 (view_convert:vec_cmp_type @0)
381 { zeros; })
382 { ones; } { zeros; })))))))))
383
384 (for cmp (gt ge lt le)
385 outp (convert convert negate negate)
386 outn (negate negate convert convert)
387 /* Transform X * (X > 0.0 ? 1.0 : -1.0) into abs(X). */
388 /* Transform X * (X >= 0.0 ? 1.0 : -1.0) into abs(X). */
389 /* Transform X * (X < 0.0 ? 1.0 : -1.0) into -abs(X). */
390 /* Transform X * (X <= 0.0 ? 1.0 : -1.0) into -abs(X). */
391 (simplify
392 (mult:c @0 (cond (cmp @0 real_zerop) real_onep@1 real_minus_onep))
393 (if (!tree_expr_maybe_nan_p (@0) && !HONOR_SIGNED_ZEROS (type))
394 (outp (abs @0))))
395 /* Transform X * (X > 0.0 ? -1.0 : 1.0) into -abs(X). */
396 /* Transform X * (X >= 0.0 ? -1.0 : 1.0) into -abs(X). */
397 /* Transform X * (X < 0.0 ? -1.0 : 1.0) into abs(X). */
398 /* Transform X * (X <= 0.0 ? -1.0 : 1.0) into abs(X). */
399 (simplify
400 (mult:c @0 (cond (cmp @0 real_zerop) real_minus_onep real_onep@1))
401 (if (!tree_expr_maybe_nan_p (@0) && !HONOR_SIGNED_ZEROS (type))
402 (outn (abs @0)))))
403
404 /* Transform X * copysign (1.0, X) into abs(X). */
405 (simplify
406 (mult:c @0 (COPYSIGN_ALL real_onep @0))
407 (if (!tree_expr_maybe_nan_p (@0) && !HONOR_SIGNED_ZEROS (type))
408 (abs @0)))
409
410 /* Transform X * copysign (1.0, -X) into -abs(X). */
411 (simplify
412 (mult:c @0 (COPYSIGN_ALL real_onep (negate @0)))
413 (if (!tree_expr_maybe_nan_p (@0) && !HONOR_SIGNED_ZEROS (type))
414 (negate (abs @0))))
415
416 /* Transform copysign (CST, X) into copysign (ABS(CST), X). */
417 (simplify
418 (COPYSIGN_ALL REAL_CST@0 @1)
419 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@0)))
420 (COPYSIGN_ALL (negate @0) @1)))
421
422 /* Transform c ? x * copysign (1, y) : z to c ? x ^ signs(y) : z.
423 tree-ssa-math-opts.cc does the corresponding optimization for
424 unconditional multiplications (via xorsign). */
425 (simplify
426 (IFN_COND_MUL:c @0 @1 (IFN_COPYSIGN real_onep @2) @3)
427 (with { tree signs = sign_mask_for (type); }
428 (if (signs)
429 (with { tree inttype = TREE_TYPE (signs); }
430 (view_convert:type
431 (IFN_COND_XOR:inttype @0
432 (view_convert:inttype @1)
433 (bit_and (view_convert:inttype @2) { signs; })
434 (view_convert:inttype @3)))))))
435
436 /* (x >= 0 ? x : 0) + (x <= 0 ? -x : 0) -> abs x. */
437 (simplify
438 (plus:c (max @0 integer_zerop) (max (negate @0) integer_zerop))
439 (abs @0))
440
441 /* X * 1, X / 1 -> X. */
442 (for op (mult trunc_div ceil_div floor_div round_div exact_div)
443 (simplify
444 (op @0 integer_onep)
445 (non_lvalue @0)))
446
447 /* (A / (1 << B)) -> (A >> B).
448 Only for unsigned A. For signed A, this would not preserve rounding
449 toward zero.
450 For example: (-1 / ( 1 << B)) != -1 >> B.
451 Also handle widening conversions, like:
452 (A / (unsigned long long) (1U << B)) -> (A >> B)
453 or
454 (A / (unsigned long long) (1 << B)) -> (A >> B).
455 If the left shift is signed, it can be done only if the upper bits
456 of A starting from shift's type sign bit are zero, as
457 (unsigned long long) (1 << 31) is -2147483648ULL, not 2147483648ULL,
458 so it is valid only if A >> 31 is zero. */
459 (simplify
460 (trunc_div (convert?@0 @3) (convert2? (lshift integer_onep@1 @2)))
461 (if ((TYPE_UNSIGNED (type) || tree_expr_nonnegative_p (@0))
462 && (!VECTOR_TYPE_P (type)
463 || target_supports_op_p (type, RSHIFT_EXPR, optab_vector)
464 || target_supports_op_p (type, RSHIFT_EXPR, optab_scalar))
465 && (useless_type_conversion_p (type, TREE_TYPE (@1))
466 || (element_precision (type) >= element_precision (TREE_TYPE (@1))
467 && (TYPE_UNSIGNED (TREE_TYPE (@1))
468 || (element_precision (type)
469 == element_precision (TREE_TYPE (@1)))
470 || (INTEGRAL_TYPE_P (type)
471 && (tree_nonzero_bits (@0)
472 & wi::mask (element_precision (TREE_TYPE (@1)) - 1,
473 true,
474 element_precision (type))) == 0)))))
475 (if (!VECTOR_TYPE_P (type)
476 && useless_type_conversion_p (TREE_TYPE (@3), TREE_TYPE (@1))
477 && element_precision (TREE_TYPE (@3)) < element_precision (type))
478 (convert (rshift @3 @2))
479 (rshift @0 @2))))
480
481 /* Preserve explicit divisions by 0: the C++ front-end wants to detect
482 undefined behavior in constexpr evaluation, and assuming that the division
483 traps enables better optimizations than these anyway. */
484 (for div (trunc_div ceil_div floor_div round_div exact_div)
485 /* 0 / X is always zero. */
486 (simplify
487 (div integer_zerop@0 @1)
488 /* But not for 0 / 0 so that we can get the proper warnings and errors. */
489 (if (!integer_zerop (@1))
490 @0))
491 /* X / -1 is -X. */
492 (simplify
493 (div @0 integer_minus_onep@1)
494 (if (!TYPE_UNSIGNED (type))
495 (negate @0)))
496 /* X / bool_range_Y is X. */
497 (simplify
498 (div @0 SSA_NAME@1)
499 (if (INTEGRAL_TYPE_P (type)
500 && ssa_name_has_boolean_range (@1)
501 && !flag_non_call_exceptions)
502 @0))
503 /* X / X is one. */
504 (simplify
505 (div @0 @0)
506 /* But not for 0 / 0 so that we can get the proper warnings and errors.
507 And not for _Fract types where we can't build 1. */
508 (if (!ALL_FRACT_MODE_P (TYPE_MODE (type))
509 && !integer_zerop (@0)
510 && (!flag_non_call_exceptions || tree_expr_nonzero_p (@0)))
511 { build_one_cst (type); }))
512 /* X / abs (X) is X < 0 ? -1 : 1. */
513 (simplify
514 (div:C @0 (abs @0))
515 (if (INTEGRAL_TYPE_P (type)
516 && TYPE_OVERFLOW_UNDEFINED (type)
517 && !integer_zerop (@0)
518 && (!flag_non_call_exceptions || tree_expr_nonzero_p (@0)))
519 (cond (lt @0 { build_zero_cst (type); })
520 { build_minus_one_cst (type); } { build_one_cst (type); })))
521 /* X / -X is -1. */
522 (simplify
523 (div:C @0 (negate @0))
524 (if ((INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
525 && TYPE_OVERFLOW_UNDEFINED (type)
526 && !integer_zerop (@0)
527 && (!flag_non_call_exceptions || tree_expr_nonzero_p (@0)))
528 { build_minus_one_cst (type); })))
529
530 /* For unsigned integral types, FLOOR_DIV_EXPR is the same as
531 TRUNC_DIV_EXPR. Rewrite into the latter in this case. Similarly
532 for MOD instead of DIV. */
533 (for floor_divmod (floor_div floor_mod)
534 trunc_divmod (trunc_div trunc_mod)
535 (simplify
536 (floor_divmod @0 @1)
537 (if ((INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
538 && TYPE_UNSIGNED (type))
539 (trunc_divmod @0 @1))))
540
541 /* 1 / X -> X == 1 for unsigned integer X.
542 1 / X -> X >= -1 && X <= 1 ? X : 0 for signed integer X.
543 But not for 1 / 0 so that we can get proper warnings and errors,
544 and not for 1-bit integers as they are edge cases better handled
545 elsewhere. */
546 (simplify
547 (trunc_div integer_onep@0 @1)
548 (if (INTEGRAL_TYPE_P (type)
549 && TYPE_PRECISION (type) > 1
550 && !integer_zerop (@1)
551 && (!flag_non_call_exceptions || tree_expr_nonzero_p (@1)))
552 (if (TYPE_UNSIGNED (type))
553 (convert (eq:boolean_type_node @1 { build_one_cst (type); }))
554 (with { tree utype = unsigned_type_for (type); }
555 (cond (le (plus (convert:utype @1) { build_one_cst (utype); })
556 { build_int_cst (utype, 2); })
557 @1 { build_zero_cst (type); })))))
558
559 /* Combine two successive divisions. Note that combining ceil_div
560 and floor_div is trickier and combining round_div even more so. */
561 (for div (trunc_div exact_div)
562 (simplify
563 (div (div@3 @0 INTEGER_CST@1) INTEGER_CST@2)
564 (with {
565 wi::overflow_type overflow;
566 wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@2),
567 TYPE_SIGN (type), &overflow);
568 }
569 (if (div == EXACT_DIV_EXPR
570 || optimize_successive_divisions_p (@2, @3))
571 (if (!overflow)
572 (div @0 { wide_int_to_tree (type, mul); })
573 (if (TYPE_UNSIGNED (type)
574 || mul != wi::min_value (TYPE_PRECISION (type), SIGNED))
575 { build_zero_cst (type); }))))))
576
577 /* Combine successive multiplications. Similar to above, but handling
578 overflow is different. */
579 (simplify
580 (mult (mult @0 INTEGER_CST@1) INTEGER_CST@2)
581 (with {
582 wi::overflow_type overflow;
583 wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@2),
584 TYPE_SIGN (type), &overflow);
585 }
586 /* Skip folding on overflow: the only special case is @1 * @2 == -INT_MIN,
587 otherwise undefined overflow implies that @0 must be zero. */
588 (if (!overflow || TYPE_OVERFLOW_WRAPS (type))
589 (mult @0 { wide_int_to_tree (type, mul); }))))
590
591 /* Similar to above, but there could be an extra add/sub between
592 successive multuiplications. */
593 (simplify
594 (mult (plus:s (mult:s@4 @0 INTEGER_CST@1) INTEGER_CST@2) INTEGER_CST@3)
595 (with {
596 bool overflowed = true;
597 wi::overflow_type ovf1, ovf2;
598 wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@3),
599 TYPE_SIGN (type), &ovf1);
600 wide_int add = wi::mul (wi::to_wide (@2), wi::to_wide (@3),
601 TYPE_SIGN (type), &ovf2);
602 if (TYPE_OVERFLOW_UNDEFINED (type))
603 {
604 #if GIMPLE
605 value_range vr0;
606 if (ovf1 == wi::OVF_NONE && ovf2 == wi::OVF_NONE
607 && get_global_range_query ()->range_of_expr (vr0, @4)
608 && !vr0.varying_p () && !vr0.undefined_p ())
609 {
610 wide_int wmin0 = vr0.lower_bound ();
611 wide_int wmax0 = vr0.upper_bound ();
612 wmin0 = wi::mul (wmin0, wi::to_wide (@3), TYPE_SIGN (type), &ovf1);
613 wmax0 = wi::mul (wmax0, wi::to_wide (@3), TYPE_SIGN (type), &ovf2);
614 if (ovf1 == wi::OVF_NONE && ovf2 == wi::OVF_NONE)
615 {
616 wi::add (wmin0, add, TYPE_SIGN (type), &ovf1);
617 wi::add (wmax0, add, TYPE_SIGN (type), &ovf2);
618 if (ovf1 == wi::OVF_NONE && ovf2 == wi::OVF_NONE)
619 overflowed = false;
620 }
621 }
622 #endif
623 }
624 else
625 overflowed = false;
626 }
627 /* Skip folding on overflow. */
628 (if (!overflowed)
629 (plus (mult @0 { wide_int_to_tree (type, mul); })
630 { wide_int_to_tree (type, add); }))))
631
632 /* Similar to above, but a multiplication between successive additions. */
633 (simplify
634 (plus (mult:s (plus:s @0 INTEGER_CST@1) INTEGER_CST@2) INTEGER_CST@3)
635 (with {
636 bool overflowed = true;
637 wi::overflow_type ovf1;
638 wi::overflow_type ovf2;
639 wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@2),
640 TYPE_SIGN (type), &ovf1);
641 wide_int add = wi::add (mul, wi::to_wide (@3),
642 TYPE_SIGN (type), &ovf2);
643 if (TYPE_OVERFLOW_UNDEFINED (type))
644 {
645 #if GIMPLE
646 value_range vr0;
647 if (ovf1 == wi::OVF_NONE && ovf2 == wi::OVF_NONE
648 && get_global_range_query ()->range_of_expr (vr0, @0)
649 && !vr0.varying_p () && !vr0.undefined_p ())
650 {
651 wide_int wmin0 = vr0.lower_bound ();
652 wide_int wmax0 = vr0.upper_bound ();
653 wmin0 = wi::mul (wmin0, wi::to_wide (@2), TYPE_SIGN (type), &ovf1);
654 wmax0 = wi::mul (wmax0, wi::to_wide (@2), TYPE_SIGN (type), &ovf2);
655 if (ovf1 == wi::OVF_NONE && ovf2 == wi::OVF_NONE)
656 {
657 wi::add (wmin0, mul, TYPE_SIGN (type), &ovf1);
658 wi::add (wmax0, mul, TYPE_SIGN (type), &ovf2);
659 if (ovf1 == wi::OVF_NONE && ovf2 == wi::OVF_NONE)
660 overflowed = false;
661 }
662 }
663 #endif
664 }
665 else
666 overflowed = false;
667 }
668 /* Skip folding on overflow. */
669 (if (!overflowed)
670 (plus (mult @0 @2) { wide_int_to_tree (type, add); }))))
671
672 /* Optimize A / A to 1.0 if we don't care about
673 NaNs or Infinities. */
674 (simplify
675 (rdiv @0 @0)
676 (if (FLOAT_TYPE_P (type)
677 && ! HONOR_NANS (type)
678 && ! HONOR_INFINITIES (type))
679 { build_one_cst (type); }))
680
681 /* Optimize -A / A to -1.0 if we don't care about
682 NaNs or Infinities. */
683 (simplify
684 (rdiv:C @0 (negate @0))
685 (if (FLOAT_TYPE_P (type)
686 && ! HONOR_NANS (type)
687 && ! HONOR_INFINITIES (type))
688 { build_minus_one_cst (type); }))
689
690 /* PR71078: x / abs(x) -> copysign (1.0, x) */
691 (simplify
692 (rdiv:C (convert? @0) (convert? (abs @0)))
693 (if (SCALAR_FLOAT_TYPE_P (type)
694 && ! HONOR_NANS (type)
695 && ! HONOR_INFINITIES (type))
696 (switch
697 (if (types_match (type, float_type_node))
698 (BUILT_IN_COPYSIGNF { build_one_cst (type); } (convert @0)))
699 (if (types_match (type, double_type_node))
700 (BUILT_IN_COPYSIGN { build_one_cst (type); } (convert @0)))
701 (if (types_match (type, long_double_type_node))
702 (BUILT_IN_COPYSIGNL { build_one_cst (type); } (convert @0))))))
703
704 /* In IEEE floating point, x/1 is not equivalent to x for snans. */
705 (simplify
706 (rdiv @0 real_onep)
707 (if (!tree_expr_maybe_signaling_nan_p (@0))
708 (non_lvalue @0)))
709
710 /* In IEEE floating point, x/-1 is not equivalent to -x for snans. */
711 (simplify
712 (rdiv @0 real_minus_onep)
713 (if (!tree_expr_maybe_signaling_nan_p (@0))
714 (negate @0)))
715
716 (if (flag_reciprocal_math)
717 /* Convert (A/B)/C to A/(B*C). */
718 (simplify
719 (rdiv (rdiv:s @0 @1) @2)
720 (rdiv @0 (mult @1 @2)))
721
722 /* Canonicalize x / (C1 * y) to (x * C2) / y. */
723 (simplify
724 (rdiv @0 (mult:s @1 REAL_CST@2))
725 (with
726 { tree tem = const_binop (RDIV_EXPR, type, build_one_cst (type), @2); }
727 (if (tem)
728 (rdiv (mult @0 { tem; } ) @1))))
729
730 /* Convert A/(B/C) to (A/B)*C */
731 (simplify
732 (rdiv @0 (rdiv:s @1 @2))
733 (mult (rdiv @0 @1) @2)))
734
735 /* Simplify x / (- y) to -x / y. */
736 (simplify
737 (rdiv @0 (negate @1))
738 (rdiv (negate @0) @1))
739
740 (if (flag_unsafe_math_optimizations)
741 /* Simplify (C / x op 0.0) to x op 0.0 for C != 0, C != Inf/Nan.
742 Since C / x may underflow to zero, do this only for unsafe math. */
743 (for op (lt le gt ge)
744 neg_op (gt ge lt le)
745 (simplify
746 (op (rdiv REAL_CST@0 @1) real_zerop@2)
747 (if (!HONOR_SIGNED_ZEROS (@1) && !HONOR_INFINITIES (@1))
748 (switch
749 (if (real_less (&dconst0, TREE_REAL_CST_PTR (@0)))
750 (op @1 @2))
751 /* For C < 0, use the inverted operator. */
752 (if (real_less (TREE_REAL_CST_PTR (@0), &dconst0))
753 (neg_op @1 @2)))))))
754
755 /* Optimize (X & (-A)) / A where A is a power of 2, to X >> log2(A) */
756 (for div (trunc_div ceil_div floor_div round_div exact_div)
757 (simplify
758 (div (convert? (bit_and @0 INTEGER_CST@1)) INTEGER_CST@2)
759 (if (integer_pow2p (@2)
760 && tree_int_cst_sgn (@2) > 0
761 && tree_nop_conversion_p (type, TREE_TYPE (@0))
762 && wi::to_wide (@2) + wi::to_wide (@1) == 0)
763 (rshift (convert @0)
764 { build_int_cst (integer_type_node,
765 wi::exact_log2 (wi::to_wide (@2))); }))))
766
767 /* If ARG1 is a constant, we can convert this to a multiply by the
768 reciprocal. This does not have the same rounding properties,
769 so only do this if -freciprocal-math. We can actually
770 always safely do it if ARG1 is a power of two, but it's hard to
771 tell if it is or not in a portable manner. */
772 (for cst (REAL_CST COMPLEX_CST VECTOR_CST)
773 (simplify
774 (rdiv @0 cst@1)
775 (if (optimize)
776 (if (flag_reciprocal_math
777 && !real_zerop (@1))
778 (with
779 { tree tem = const_binop (RDIV_EXPR, type, build_one_cst (type), @1); }
780 (if (tem)
781 (mult @0 { tem; } )))
782 (if (cst != COMPLEX_CST)
783 (with { tree inverse = exact_inverse (type, @1); }
784 (if (inverse)
785 (mult @0 { inverse; } ))))))))
786
787 (for mod (ceil_mod floor_mod round_mod trunc_mod)
788 /* 0 % X is always zero. */
789 (simplify
790 (mod integer_zerop@0 @1)
791 /* But not for 0 % 0 so that we can get the proper warnings and errors. */
792 (if (!integer_zerop (@1))
793 @0))
794 /* X % 1 is always zero. */
795 (simplify
796 (mod @0 integer_onep)
797 { build_zero_cst (type); })
798 /* X % -1 is zero. */
799 (simplify
800 (mod @0 integer_minus_onep@1)
801 (if (!TYPE_UNSIGNED (type))
802 { build_zero_cst (type); }))
803 /* X % X is zero. */
804 (simplify
805 (mod @0 @0)
806 /* But not for 0 % 0 so that we can get the proper warnings and errors. */
807 (if (!integer_zerop (@0))
808 { build_zero_cst (type); }))
809 /* (X % Y) % Y is just X % Y. */
810 (simplify
811 (mod (mod@2 @0 @1) @1)
812 @2)
813 /* From extract_muldiv_1: (X * C1) % C2 is zero if C1 is a multiple of C2. */
814 (simplify
815 (mod (mult @0 INTEGER_CST@1) INTEGER_CST@2)
816 (if (ANY_INTEGRAL_TYPE_P (type)
817 && TYPE_OVERFLOW_UNDEFINED (type)
818 && wi::multiple_of_p (wi::to_wide (@1), wi::to_wide (@2),
819 TYPE_SIGN (type)))
820 { build_zero_cst (type); }))
821 /* For (X % C) == 0, if X is signed and C is power of 2, use unsigned
822 modulo and comparison, since it is simpler and equivalent. */
823 (for cmp (eq ne)
824 (simplify
825 (cmp (mod @0 integer_pow2p@2) integer_zerop@1)
826 (if (!TYPE_UNSIGNED (TREE_TYPE (@0)))
827 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
828 (cmp (mod (convert:utype @0) (convert:utype @2)) (convert:utype @1)))))))
829
830 /* X % -C is the same as X % C. */
831 (simplify
832 (trunc_mod @0 INTEGER_CST@1)
833 (if (TYPE_SIGN (type) == SIGNED
834 && !TREE_OVERFLOW (@1)
835 && wi::neg_p (wi::to_wide (@1))
836 && !TYPE_OVERFLOW_TRAPS (type)
837 /* Avoid this transformation if C is INT_MIN, i.e. C == -C. */
838 && !sign_bit_p (@1, @1))
839 (trunc_mod @0 (negate @1))))
840
841 /* X % -Y is the same as X % Y. */
842 (simplify
843 (trunc_mod @0 (convert? (negate @1)))
844 (if (INTEGRAL_TYPE_P (type)
845 && !TYPE_UNSIGNED (type)
846 && !TYPE_OVERFLOW_TRAPS (type)
847 && tree_nop_conversion_p (type, TREE_TYPE (@1))
848 /* Avoid this transformation if X might be INT_MIN or
849 Y might be -1, because we would then change valid
850 INT_MIN % -(-1) into invalid INT_MIN % -1. */
851 && (expr_not_equal_to (@0, wi::to_wide (TYPE_MIN_VALUE (type)))
852 || expr_not_equal_to (@1, wi::minus_one (TYPE_PRECISION
853 (TREE_TYPE (@1))))))
854 (trunc_mod @0 (convert @1))))
855
856 /* X - (X / Y) * Y is the same as X % Y. */
857 (simplify
858 (minus (convert1? @0) (convert2? (mult:c (trunc_div @@0 @@1) @1)))
859 (if (INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
860 (convert (trunc_mod @0 @1))))
861
862 /* x * (1 + y / x) - y -> x - y % x */
863 (simplify
864 (minus (mult:cs @0 (plus:s (trunc_div:s @1 @0) integer_onep)) @1)
865 (if (INTEGRAL_TYPE_P (type))
866 (minus @0 (trunc_mod @1 @0))))
867
868 /* Optimize TRUNC_MOD_EXPR by a power of two into a BIT_AND_EXPR,
869 i.e. "X % C" into "X & (C - 1)", if X and C are positive.
870 Also optimize A % (C << N) where C is a power of 2,
871 to A & ((C << N) - 1).
872 Also optimize "A shift (B % C)", if C is a power of 2, to
873 "A shift (B & (C - 1))". SHIFT operation include "<<" and ">>"
874 and assume (B % C) is nonnegative as shifts negative values would
875 be UB. */
876 (match (power_of_two_cand @1)
877 INTEGER_CST@1)
878 (match (power_of_two_cand @1)
879 (lshift INTEGER_CST@1 @2))
880 (for mod (trunc_mod floor_mod)
881 (for shift (lshift rshift)
882 (simplify
883 (shift @0 (mod @1 (power_of_two_cand@2 @3)))
884 (if (integer_pow2p (@3) && tree_int_cst_sgn (@3) > 0)
885 (shift @0 (bit_and @1 (minus @2 { build_int_cst (TREE_TYPE (@2),
886 1); }))))))
887 (simplify
888 (mod @0 (convert? (power_of_two_cand@1 @2)))
889 (if ((TYPE_UNSIGNED (type) || tree_expr_nonnegative_p (@0))
890 /* Allow any integral conversions of the divisor, except
891 conversion from narrower signed to wider unsigned type
892 where if @1 would be negative power of two, the divisor
893 would not be a power of two. */
894 && INTEGRAL_TYPE_P (type)
895 && INTEGRAL_TYPE_P (TREE_TYPE (@1))
896 && (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@1))
897 || TYPE_UNSIGNED (TREE_TYPE (@1))
898 || !TYPE_UNSIGNED (type))
899 && integer_pow2p (@2) && tree_int_cst_sgn (@2) > 0)
900 (with { tree utype = TREE_TYPE (@1);
901 if (!TYPE_OVERFLOW_WRAPS (utype))
902 utype = unsigned_type_for (utype); }
903 (bit_and @0 (convert (minus (convert:utype @1)
904 { build_one_cst (utype); })))))))
905
906 /* Simplify (unsigned t * 2)/2 -> unsigned t & 0x7FFFFFFF. */
907 (simplify
908 (trunc_div (mult @0 integer_pow2p@1) @1)
909 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) && TYPE_UNSIGNED (TREE_TYPE (@0)))
910 (bit_and @0 { wide_int_to_tree
911 (type, wi::mask (TYPE_PRECISION (type)
912 - wi::exact_log2 (wi::to_wide (@1)),
913 false, TYPE_PRECISION (type))); })))
914
915 /* Simplify (unsigned t / 2) * 2 -> unsigned t & ~1. */
916 (simplify
917 (mult (trunc_div @0 integer_pow2p@1) @1)
918 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) && TYPE_UNSIGNED (TREE_TYPE (@0)))
919 (bit_and @0 (negate @1))))
920
921 /* Simplify (t * 2) / 2) -> t. */
922 (for div (trunc_div ceil_div floor_div round_div exact_div)
923 (simplify
924 (div (mult:c @0 @1) @1)
925 (if (ANY_INTEGRAL_TYPE_P (type))
926 (if (TYPE_OVERFLOW_UNDEFINED (type))
927 @0
928 #if GIMPLE
929 (with {value_range vr0, vr1;}
930 (if (INTEGRAL_TYPE_P (type)
931 && get_range_query (cfun)->range_of_expr (vr0, @0)
932 && get_range_query (cfun)->range_of_expr (vr1, @1)
933 && range_op_handler (MULT_EXPR).overflow_free_p (vr0, vr1))
934 @0))
935 #endif
936 ))))
937
938 #if GIMPLE
939 (for div (trunc_div exact_div)
940 /* Simplify (X + M*N) / N -> X / N + M. */
941 (simplify
942 (div (plus:c@4 @0 (mult:c@3 @1 @2)) @2)
943 (with {value_range vr0, vr1, vr2, vr3, vr4;}
944 (if (INTEGRAL_TYPE_P (type)
945 && get_range_query (cfun)->range_of_expr (vr1, @1)
946 && get_range_query (cfun)->range_of_expr (vr2, @2)
947 /* "N*M" doesn't overflow. */
948 && range_op_handler (MULT_EXPR).overflow_free_p (vr1, vr2)
949 && get_range_query (cfun)->range_of_expr (vr0, @0)
950 && get_range_query (cfun)->range_of_expr (vr3, @3)
951 /* "X+(N*M)" doesn't overflow. */
952 && range_op_handler (PLUS_EXPR).overflow_free_p (vr0, vr3)
953 && get_range_query (cfun)->range_of_expr (vr4, @4)
954 && !vr4.undefined_p ()
955 /* "X+N*M" is not with opposite sign as "X". */
956 && (TYPE_UNSIGNED (type)
957 || (vr0.nonnegative_p () && vr4.nonnegative_p ())
958 || (vr0.nonpositive_p () && vr4.nonpositive_p ())))
959 (plus (div @0 @2) @1))))
960
961 /* Simplify (X - M*N) / N -> X / N - M. */
962 (simplify
963 (div (minus@4 @0 (mult:c@3 @1 @2)) @2)
964 (with {value_range vr0, vr1, vr2, vr3, vr4;}
965 (if (INTEGRAL_TYPE_P (type)
966 && get_range_query (cfun)->range_of_expr (vr1, @1)
967 && get_range_query (cfun)->range_of_expr (vr2, @2)
968 /* "N * M" doesn't overflow. */
969 && range_op_handler (MULT_EXPR).overflow_free_p (vr1, vr2)
970 && get_range_query (cfun)->range_of_expr (vr0, @0)
971 && get_range_query (cfun)->range_of_expr (vr3, @3)
972 /* "X - (N*M)" doesn't overflow. */
973 && range_op_handler (MINUS_EXPR).overflow_free_p (vr0, vr3)
974 && get_range_query (cfun)->range_of_expr (vr4, @4)
975 && !vr4.undefined_p ()
976 /* "X-N*M" is not with opposite sign as "X". */
977 && (TYPE_UNSIGNED (type)
978 || (vr0.nonnegative_p () && vr4.nonnegative_p ())
979 || (vr0.nonpositive_p () && vr4.nonpositive_p ())))
980 (minus (div @0 @2) @1)))))
981
982 /* Simplify
983 (X + C) / N -> X / N + C / N where C is multiple of N.
984 (X + C) >> N -> X >> N + C>>N if low N bits of C is 0. */
985 (for op (trunc_div exact_div rshift)
986 (simplify
987 (op (plus@3 @0 INTEGER_CST@1) INTEGER_CST@2)
988 (with
989 {
990 wide_int c = wi::to_wide (@1);
991 wide_int n = wi::to_wide (@2);
992 bool shift = op == RSHIFT_EXPR;
993 #define plus_op1(v) (shift ? wi::rshift (v, n, TYPE_SIGN (type)) \
994 : wi::div_trunc (v, n, TYPE_SIGN (type)))
995 #define exact_mod(v) (shift ? wi::ctz (v) >= n.to_shwi () \
996 : wi::multiple_of_p (v, n, TYPE_SIGN (type)))
997 value_range vr0, vr1, vr3;
998 }
999 (if (INTEGRAL_TYPE_P (type)
1000 && get_range_query (cfun)->range_of_expr (vr0, @0))
1001 (if (exact_mod (c)
1002 && get_range_query (cfun)->range_of_expr (vr1, @1)
1003 /* "X+C" doesn't overflow. */
1004 && range_op_handler (PLUS_EXPR).overflow_free_p (vr0, vr1)
1005 && get_range_query (cfun)->range_of_expr (vr3, @3)
1006 && !vr3.undefined_p ()
1007 /* "X+C" and "X" are not of opposite sign. */
1008 && (TYPE_UNSIGNED (type)
1009 || (vr0.nonnegative_p () && vr3.nonnegative_p ())
1010 || (vr0.nonpositive_p () && vr3.nonpositive_p ())))
1011 (plus (op @0 @2) { wide_int_to_tree (type, plus_op1 (c)); })
1012 (if (!vr0.undefined_p () && TYPE_UNSIGNED (type) && c.sign_mask () < 0
1013 && exact_mod (-c)
1014 /* unsigned "X-(-C)" doesn't underflow. */
1015 && wi::geu_p (vr0.lower_bound (), -c))
1016 (plus (op @0 @2) { wide_int_to_tree (type, -plus_op1 (-c)); })))))))
1017 #undef plus_op1
1018 #undef exact_mod
1019 #endif
1020
1021 /* (nop_outer_cast)-(inner_cast)var -> -(outer_cast)(var)
1022 if var is smaller in precision.
1023 This is always safe for both doing the negative in signed or unsigned
1024 as the value for undefined will not show up. */
1025 (simplify
1026 (convert (negate:s@1 (convert:s @0)))
1027 (if (INTEGRAL_TYPE_P (type)
1028 && tree_nop_conversion_p (type, TREE_TYPE (@1))
1029 && TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (@0)))
1030 (negate (convert @0))))
1031
1032 (for op (negate abs)
1033 /* Simplify cos(-x) and cos(|x|) -> cos(x). Similarly for cosh. */
1034 (for coss (COS COSH)
1035 (simplify
1036 (coss (op @0))
1037 (coss @0)))
1038 /* Simplify pow(-x, y) and pow(|x|,y) -> pow(x,y) if y is an even integer. */
1039 (for pows (POW)
1040 (simplify
1041 (pows (op @0) REAL_CST@1)
1042 (with { HOST_WIDE_INT n; }
1043 (if (real_isinteger (&TREE_REAL_CST (@1), &n) && (n & 1) == 0)
1044 (pows @0 @1)))))
1045 /* Likewise for powi. */
1046 (for pows (POWI)
1047 (simplify
1048 (pows (op @0) INTEGER_CST@1)
1049 (if ((wi::to_wide (@1) & 1) == 0)
1050 (pows @0 @1))))
1051 /* Strip negate and abs from both operands of hypot. */
1052 (for hypots (HYPOT)
1053 (simplify
1054 (hypots (op @0) @1)
1055 (hypots @0 @1))
1056 (simplify
1057 (hypots @0 (op @1))
1058 (hypots @0 @1)))
1059 /* copysign(-x, y) and copysign(abs(x), y) -> copysign(x, y). */
1060 (for copysigns (COPYSIGN_ALL)
1061 (simplify
1062 (copysigns (op @0) @1)
1063 (copysigns @0 @1))))
1064
1065 /* abs(x)*abs(x) -> x*x. Should be valid for all types. */
1066 (simplify
1067 (mult (abs@1 @0) @1)
1068 (mult @0 @0))
1069
1070 /* Convert absu(x)*absu(x) -> x*x. */
1071 (simplify
1072 (mult (absu@1 @0) @1)
1073 (mult (convert@2 @0) @2))
1074
1075 /* cos(copysign(x, y)) -> cos(x). Similarly for cosh. */
1076 (for coss (COS COSH)
1077 copysigns (COPYSIGN)
1078 (simplify
1079 (coss (copysigns @0 @1))
1080 (coss @0)))
1081
1082 /* pow(copysign(x, y), z) -> pow(x, z) if z is an even integer. */
1083 (for pows (POW)
1084 copysigns (COPYSIGN)
1085 (simplify
1086 (pows (copysigns @0 @2) REAL_CST@1)
1087 (with { HOST_WIDE_INT n; }
1088 (if (real_isinteger (&TREE_REAL_CST (@1), &n) && (n & 1) == 0)
1089 (pows @0 @1)))))
1090 /* Likewise for powi. */
1091 (for pows (POWI)
1092 copysigns (COPYSIGN)
1093 (simplify
1094 (pows (copysigns @0 @2) INTEGER_CST@1)
1095 (if ((wi::to_wide (@1) & 1) == 0)
1096 (pows @0 @1))))
1097
1098 (for hypots (HYPOT)
1099 copysigns (COPYSIGN)
1100 /* hypot(copysign(x, y), z) -> hypot(x, z). */
1101 (simplify
1102 (hypots (copysigns @0 @1) @2)
1103 (hypots @0 @2))
1104 /* hypot(x, copysign(y, z)) -> hypot(x, y). */
1105 (simplify
1106 (hypots @0 (copysigns @1 @2))
1107 (hypots @0 @1)))
1108
1109 /* copysign(x, CST) -> [-]abs (x). */
1110 (for copysigns (COPYSIGN_ALL)
1111 (simplify
1112 (copysigns @0 REAL_CST@1)
1113 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
1114 (negate (abs @0))
1115 (abs @0))))
1116
1117 /* copysign(copysign(x, y), z) -> copysign(x, z). */
1118 (for copysigns (COPYSIGN_ALL)
1119 (simplify
1120 (copysigns (copysigns @0 @1) @2)
1121 (copysigns @0 @2)))
1122
1123 /* copysign(x,y)*copysign(x,y) -> x*x. */
1124 (for copysigns (COPYSIGN_ALL)
1125 (simplify
1126 (mult (copysigns@2 @0 @1) @2)
1127 (mult @0 @0)))
1128
1129 /* ccos(-x) -> ccos(x). Similarly for ccosh. */
1130 (for ccoss (CCOS CCOSH)
1131 (simplify
1132 (ccoss (negate @0))
1133 (ccoss @0)))
1134
1135 /* cabs(-x) and cos(conj(x)) -> cabs(x). */
1136 (for ops (conj negate)
1137 (for cabss (CABS)
1138 (simplify
1139 (cabss (ops @0))
1140 (cabss @0))))
1141
1142 /* Fold (a * (1 << b)) into (a << b) */
1143 (simplify
1144 (mult:c @0 (convert? (lshift integer_onep@1 @2)))
1145 (if (! FLOAT_TYPE_P (type)
1146 && tree_nop_conversion_p (type, TREE_TYPE (@1)))
1147 (lshift @0 @2)))
1148
1149 /* Shifts by precision or greater result in zero. */
1150 (for shift (lshift rshift)
1151 (simplify
1152 (shift @0 uniform_integer_cst_p@1)
1153 (if ((GIMPLE || !sanitize_flags_p (SANITIZE_SHIFT_EXPONENT))
1154 /* Leave arithmetic right shifts of possibly negative values alone. */
1155 && (TYPE_UNSIGNED (type)
1156 || shift == LSHIFT_EXPR
1157 || tree_expr_nonnegative_p (@0))
1158 /* Use a signed compare to leave negative shift counts alone. */
1159 && wi::ges_p (wi::to_wide (uniform_integer_cst_p (@1)),
1160 element_precision (type)))
1161 { build_zero_cst (type); })))
1162
1163 /* Shifts by constants distribute over several binary operations,
1164 hence (X << C) + (Y << C) can be simplified to (X + Y) << C. */
1165 (for op (plus minus)
1166 (simplify
1167 (op (lshift:s @0 @1) (lshift:s @2 @1))
1168 (if (INTEGRAL_TYPE_P (type)
1169 && TYPE_OVERFLOW_WRAPS (type)
1170 && !TYPE_SATURATING (type))
1171 (lshift (op @0 @2) @1))))
1172
1173 (for op (bit_and bit_ior bit_xor)
1174 (simplify
1175 (op (lshift:s @0 @1) (lshift:s @2 @1))
1176 (if (INTEGRAL_TYPE_P (type))
1177 (lshift (op @0 @2) @1)))
1178 (simplify
1179 (op (rshift:s @0 @1) (rshift:s @2 @1))
1180 (if (INTEGRAL_TYPE_P (type))
1181 (rshift (op @0 @2) @1))))
1182
1183 /* Fold (1 << (C - x)) where C = precision(type) - 1
1184 into ((1 << C) >> x). */
1185 (simplify
1186 (lshift integer_onep@0 (minus@1 INTEGER_CST@2 @3))
1187 (if (INTEGRAL_TYPE_P (type)
1188 && wi::eq_p (wi::to_wide (@2), TYPE_PRECISION (type) - 1)
1189 && single_use (@1))
1190 (if (TYPE_UNSIGNED (type))
1191 (rshift (lshift @0 @2) @3)
1192 (with
1193 { tree utype = unsigned_type_for (type); }
1194 (convert (rshift (lshift (convert:utype @0) @2) @3))))))
1195
1196 /* Fold ((type)(a<0)) << SIGNBITOFA into ((type)a) & signbit. */
1197 (simplify
1198 (lshift (convert (lt @0 integer_zerop@1)) INTEGER_CST@2)
1199 (if (TYPE_SIGN (TREE_TYPE (@0)) == SIGNED
1200 && wi::eq_p (wi::to_wide (@2), TYPE_PRECISION (TREE_TYPE (@0)) - 1))
1201 (with { wide_int wone = wi::one (TYPE_PRECISION (type)); }
1202 (bit_and (convert @0)
1203 { wide_int_to_tree (type,
1204 wi::lshift (wone, wi::to_wide (@2))); }))))
1205
1206 /* Fold (-x >> C) into -(x > 0) where C = precision(type) - 1. */
1207 (for cst (INTEGER_CST VECTOR_CST)
1208 (simplify
1209 (rshift (negate:s @0) cst@1)
1210 (if (!TYPE_UNSIGNED (type)
1211 && TYPE_OVERFLOW_UNDEFINED (type))
1212 (with { tree stype = TREE_TYPE (@1);
1213 tree bt = truth_type_for (type);
1214 tree zeros = build_zero_cst (type);
1215 tree cst = NULL_TREE; }
1216 (switch
1217 /* Handle scalar case. */
1218 (if (INTEGRAL_TYPE_P (type)
1219 /* If we apply the rule to the scalar type before vectorization
1220 we will enforce the result of the comparison being a bool
1221 which will require an extra AND on the result that will be
1222 indistinguishable from when the user did actually want 0
1223 or 1 as the result so it can't be removed. */
1224 && canonicalize_math_after_vectorization_p ()
1225 && wi::eq_p (wi::to_wide (@1), TYPE_PRECISION (type) - 1))
1226 (negate (convert (gt @0 { zeros; }))))
1227 /* Handle vector case. */
1228 (if (VECTOR_INTEGER_TYPE_P (type)
1229 /* First check whether the target has the same mode for vector
1230 comparison results as it's operands do. */
1231 && TYPE_MODE (bt) == TYPE_MODE (type)
1232 /* Then check to see if the target is able to expand the comparison
1233 with the given type later on, otherwise we may ICE. */
1234 && expand_vec_cmp_expr_p (type, bt, GT_EXPR)
1235 && (cst = uniform_integer_cst_p (@1)) != NULL
1236 && wi::eq_p (wi::to_wide (cst), element_precision (type) - 1))
1237 (view_convert (gt:bt @0 { zeros; }))))))))
1238
1239 /* Fold (C1/X)*C2 into (C1*C2)/X. */
1240 (simplify
1241 (mult (rdiv@3 REAL_CST@0 @1) REAL_CST@2)
1242 (if (flag_associative_math
1243 && single_use (@3))
1244 (with
1245 { tree tem = const_binop (MULT_EXPR, type, @0, @2); }
1246 (if (tem)
1247 (rdiv { tem; } @1)))))
1248
1249 /* Simplify ~X & X as zero. */
1250 (simplify
1251 (bit_and (convert? @0) (convert? @1))
1252 (with { bool wascmp; }
1253 (if (types_match (TREE_TYPE (@0), TREE_TYPE (@1))
1254 && bitwise_inverted_equal_p (@0, @1, wascmp))
1255 { wascmp ? constant_boolean_node (false, type) : build_zero_cst (type); })))
1256
1257 /* PR71636: Transform x & ((1U << b) - 1) -> x & ~(~0U << b); */
1258 (simplify
1259 (bit_and:c @0 (plus:s (lshift:s integer_onep @1) integer_minus_onep))
1260 (if (TYPE_UNSIGNED (type))
1261 (bit_and @0 (bit_not (lshift { build_all_ones_cst (type); } @1)))))
1262
1263 (for bitop (bit_and bit_ior)
1264 cmp (eq ne)
1265 /* PR35691: Transform
1266 (x == 0 & y == 0) -> (x | typeof(x)(y)) == 0.
1267 (x != 0 | y != 0) -> (x | typeof(x)(y)) != 0. */
1268 (simplify
1269 (bitop (cmp @0 integer_zerop@2) (cmp @1 integer_zerop))
1270 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1271 && INTEGRAL_TYPE_P (TREE_TYPE (@1))
1272 && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
1273 (cmp (bit_ior @0 (convert @1)) @2)))
1274 /* Transform:
1275 (x == -1 & y == -1) -> (x & typeof(x)(y)) == -1.
1276 (x != -1 | y != -1) -> (x & typeof(x)(y)) != -1. */
1277 (simplify
1278 (bitop (cmp @0 integer_all_onesp@2) (cmp @1 integer_all_onesp))
1279 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1280 && INTEGRAL_TYPE_P (TREE_TYPE (@1))
1281 && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
1282 (cmp (bit_and @0 (convert @1)) @2))))
1283
1284 /* Fold (A & ~B) - (A & B) into (A ^ B) - B. */
1285 (simplify
1286 (minus (bit_and:cs @0 (bit_not @1)) (bit_and:cs @0 @1))
1287 (minus (bit_xor @0 @1) @1))
1288 (simplify
1289 (minus (bit_and:s @0 INTEGER_CST@2) (bit_and:s @0 INTEGER_CST@1))
1290 (if (~wi::to_wide (@2) == wi::to_wide (@1))
1291 (minus (bit_xor @0 @1) @1)))
1292
1293 /* Fold (A & B) - (A & ~B) into B - (A ^ B). */
1294 (simplify
1295 (minus (bit_and:cs @0 @1) (bit_and:cs @0 (bit_not @1)))
1296 (minus @1 (bit_xor @0 @1)))
1297
1298 /* Simplify (X & ~Y) |^+ (~X & Y) -> X ^ Y. */
1299 (for op (bit_ior bit_xor plus)
1300 (simplify
1301 (op (bit_and:c @0 @2) (bit_and:c @3 @1))
1302 (with { bool wascmp0, wascmp1; }
1303 (if (bitwise_inverted_equal_p (@2, @1, wascmp0)
1304 && bitwise_inverted_equal_p (@0, @3, wascmp1)
1305 && ((!wascmp0 && !wascmp1)
1306 || element_precision (type) == 1))
1307 (bit_xor @0 @1)))))
1308
1309 /* PR53979: Transform ((a ^ b) | a) -> (a | b) */
1310 (simplify
1311 (bit_ior:c (bit_xor:c @0 @1) @0)
1312 (bit_ior @0 @1))
1313
1314 /* (a & ~b) | (a ^ b) --> a ^ b */
1315 (simplify
1316 (bit_ior:c (bit_and:c @0 (bit_not @1)) (bit_xor:c@2 @0 @1))
1317 @2)
1318
1319 /* (a & ~b) ^ ~a --> ~(a & b) */
1320 (simplify
1321 (bit_xor:c (bit_and:cs @0 (bit_not @1)) (bit_not @0))
1322 (bit_not (bit_and @0 @1)))
1323
1324 /* (~a & b) ^ a --> (a | b) */
1325 (simplify
1326 (bit_xor:c (bit_and:cs (bit_not @0) @1) @0)
1327 (bit_ior @0 @1))
1328
1329 /* (a | b) & ~(a ^ b) --> a & b */
1330 (simplify
1331 (bit_and:c (bit_ior @0 @1) (bit_not (bit_xor:c @0 @1)))
1332 (bit_and @0 @1))
1333
1334 /* (a | b) & (a == b) --> a & b (boolean version of the above). */
1335 (simplify
1336 (bit_and:c (bit_ior @0 @1) (nop_convert? (eq:c @0 @1)))
1337 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1338 && TYPE_PRECISION (TREE_TYPE (@0)) == 1)
1339 (bit_and @0 @1)))
1340
1341 /* a | ~(a ^ b) --> a | ~b */
1342 (simplify
1343 (bit_ior:c @0 (bit_not:s (bit_xor:c @0 @1)))
1344 (bit_ior @0 (bit_not @1)))
1345
1346 /* a | (a == b) --> a | (b^1) (boolean version of the above). */
1347 (simplify
1348 (bit_ior:c @0 (nop_convert? (eq:c @0 @1)))
1349 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1350 && TYPE_PRECISION (TREE_TYPE (@0)) == 1)
1351 (bit_ior @0 (bit_xor @1 { build_one_cst (type); }))))
1352
1353 /* (a | b) | (a &^ b) --> a | b */
1354 (for op (bit_and bit_xor)
1355 (simplify
1356 (bit_ior:c (bit_ior@2 @0 @1) (op:c @0 @1))
1357 @2))
1358
1359 /* (a & b) | ~(a ^ b) --> ~(a ^ b) */
1360 (simplify
1361 (bit_ior:c (bit_and:c @0 @1) (bit_not@2 (bit_xor @0 @1)))
1362 @2)
1363
1364 /* (a & b) | (a == b) --> a == b */
1365 (simplify
1366 (bit_ior:c (bit_and:c @0 @1) (nop_convert?@2 (eq @0 @1)))
1367 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1368 && TYPE_PRECISION (TREE_TYPE (@0)) == 1)
1369 @2))
1370
1371 /* ~(~a & b) --> a | ~b */
1372 (simplify
1373 (bit_not (bit_and:cs (bit_not @0) @1))
1374 (bit_ior @0 (bit_not @1)))
1375
1376 /* ~(~a | b) --> a & ~b */
1377 (simplify
1378 (bit_not (bit_ior:cs (bit_not @0) @1))
1379 (bit_and @0 (bit_not @1)))
1380
1381 /* (a ^ b) & ((b ^ c) ^ a) --> (a ^ b) & ~c */
1382 (simplify
1383 (bit_and:c (bit_xor:c@3 @0 @1) (bit_xor:cs (bit_xor:cs @1 @2) @0))
1384 (bit_and @3 (bit_not @2)))
1385
1386 /* (a ^ b) | ((b ^ c) ^ a) --> (a ^ b) | c */
1387 (simplify
1388 (bit_ior:c (bit_xor:c@3 @0 @1) (bit_xor:c (bit_xor:c @1 @2) @0))
1389 (bit_ior @3 @2))
1390
1391 /* (~X | C) ^ D -> (X | C) ^ (~D ^ C) if (~D ^ C) can be simplified. */
1392 (simplify
1393 (bit_xor:c (bit_ior:cs (bit_not:s @0) @1) @2)
1394 (bit_xor (bit_ior @0 @1) (bit_xor! (bit_not! @2) @1)))
1395
1396 /* (~X & C) ^ D -> (X & C) ^ (D ^ C) if (D ^ C) can be simplified. */
1397 (simplify
1398 (bit_xor:c (bit_and:cs (bit_not:s @0) @1) @2)
1399 (bit_xor (bit_and @0 @1) (bit_xor! @2 @1)))
1400
1401 /* Simplify (~X & Y) to X ^ Y if we know that (X & ~Y) is 0. */
1402 (simplify
1403 (bit_and (bit_not SSA_NAME@0) INTEGER_CST@1)
1404 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1405 && wi::bit_and_not (get_nonzero_bits (@0), wi::to_wide (@1)) == 0)
1406 (bit_xor @0 @1)))
1407
1408 /* For constants M and N, if M == (1LL << cst) - 1 && (N & M) == M,
1409 ((A & N) + B) & M -> (A + B) & M
1410 Similarly if (N & M) == 0,
1411 ((A | N) + B) & M -> (A + B) & M
1412 and for - instead of + (or unary - instead of +)
1413 and/or ^ instead of |.
1414 If B is constant and (B & M) == 0, fold into A & M. */
1415 (for op (plus minus)
1416 (for bitop (bit_and bit_ior bit_xor)
1417 (simplify
1418 (bit_and (op:s (bitop:s@0 @3 INTEGER_CST@4) @1) INTEGER_CST@2)
1419 (with
1420 { tree pmop[2];
1421 tree utype = fold_bit_and_mask (TREE_TYPE (@0), @2, op, @0, bitop,
1422 @3, @4, @1, ERROR_MARK, NULL_TREE,
1423 NULL_TREE, pmop); }
1424 (if (utype)
1425 (convert (bit_and (op (convert:utype { pmop[0]; })
1426 (convert:utype { pmop[1]; }))
1427 (convert:utype @2))))))
1428 (simplify
1429 (bit_and (op:s @0 (bitop:s@1 @3 INTEGER_CST@4)) INTEGER_CST@2)
1430 (with
1431 { tree pmop[2];
1432 tree utype = fold_bit_and_mask (TREE_TYPE (@0), @2, op, @0, ERROR_MARK,
1433 NULL_TREE, NULL_TREE, @1, bitop, @3,
1434 @4, pmop); }
1435 (if (utype)
1436 (convert (bit_and (op (convert:utype { pmop[0]; })
1437 (convert:utype { pmop[1]; }))
1438 (convert:utype @2)))))))
1439 (simplify
1440 (bit_and (op:s @0 @1) INTEGER_CST@2)
1441 (with
1442 { tree pmop[2];
1443 tree utype = fold_bit_and_mask (TREE_TYPE (@0), @2, op, @0, ERROR_MARK,
1444 NULL_TREE, NULL_TREE, @1, ERROR_MARK,
1445 NULL_TREE, NULL_TREE, pmop); }
1446 (if (utype)
1447 (convert (bit_and (op (convert:utype { pmop[0]; })
1448 (convert:utype { pmop[1]; }))
1449 (convert:utype @2)))))))
1450 (for bitop (bit_and bit_ior bit_xor)
1451 (simplify
1452 (bit_and (negate:s (bitop:s@0 @2 INTEGER_CST@3)) INTEGER_CST@1)
1453 (with
1454 { tree pmop[2];
1455 tree utype = fold_bit_and_mask (TREE_TYPE (@0), @1, NEGATE_EXPR, @0,
1456 bitop, @2, @3, NULL_TREE, ERROR_MARK,
1457 NULL_TREE, NULL_TREE, pmop); }
1458 (if (utype)
1459 (convert (bit_and (negate (convert:utype { pmop[0]; }))
1460 (convert:utype @1)))))))
1461
1462 /* X % Y is smaller than Y. */
1463 (for cmp (lt ge)
1464 (simplify
1465 (cmp:c (trunc_mod @0 @1) @1)
1466 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
1467 { constant_boolean_node (cmp == LT_EXPR, type); })))
1468
1469 /* x | ~0 -> ~0 */
1470 (simplify
1471 (bit_ior @0 integer_all_onesp@1)
1472 @1)
1473
1474 /* x | 0 -> x */
1475 (simplify
1476 (bit_ior @0 integer_zerop)
1477 @0)
1478
1479 /* x & 0 -> 0 */
1480 (simplify
1481 (bit_and @0 integer_zerop@1)
1482 @1)
1483
1484 /* ~x | x -> -1 */
1485 /* ~x ^ x -> -1 */
1486 (for op (bit_ior bit_xor)
1487 (simplify
1488 (op (convert? @0) (convert? @1))
1489 (with { bool wascmp; }
1490 (if (types_match (TREE_TYPE (@0), TREE_TYPE (@1))
1491 && bitwise_inverted_equal_p (@0, @1, wascmp))
1492 (convert
1493 { wascmp
1494 ? constant_boolean_node (true, type)
1495 : build_all_ones_cst (TREE_TYPE (@0)); })))))
1496
1497 /* x ^ x -> 0 */
1498 (simplify
1499 (bit_xor @0 @0)
1500 { build_zero_cst (type); })
1501
1502 /* Canonicalize X ^ ~0 to ~X. */
1503 (simplify
1504 (bit_xor @0 integer_all_onesp@1)
1505 (bit_not @0))
1506
1507 /* x & ~0 -> x */
1508 (simplify
1509 (bit_and @0 integer_all_onesp)
1510 (non_lvalue @0))
1511
1512 /* x & x -> x, x | x -> x */
1513 (for bitop (bit_and bit_ior)
1514 (simplify
1515 (bitop @0 @0)
1516 (non_lvalue @0)))
1517
1518 /* x & C -> x if we know that x & ~C == 0. */
1519 #if GIMPLE
1520 (simplify
1521 (bit_and SSA_NAME@0 INTEGER_CST@1)
1522 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1523 && wi::bit_and_not (get_nonzero_bits (@0), wi::to_wide (@1)) == 0)
1524 @0))
1525 /* x | C -> C if we know that x & ~C == 0. */
1526 (simplify
1527 (bit_ior SSA_NAME@0 INTEGER_CST@1)
1528 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1529 && wi::bit_and_not (get_nonzero_bits (@0), wi::to_wide (@1)) == 0)
1530 @1))
1531 #endif
1532
1533 /* ~(~X - Y) -> X + Y and ~(~X + Y) -> X - Y. */
1534 (simplify
1535 (bit_not (minus (bit_not @0) @1))
1536 (plus @0 @1))
1537 (simplify
1538 (bit_not (plus:c (bit_not @0) @1))
1539 (minus @0 @1))
1540 /* (~X - ~Y) -> Y - X. */
1541 (simplify
1542 (minus (bit_not @0) (bit_not @1))
1543 (if (!TYPE_OVERFLOW_SANITIZED (type))
1544 (with { tree utype = unsigned_type_for (type); }
1545 (convert (minus (convert:utype @1) (convert:utype @0))))))
1546
1547 /* ~(X - Y) -> ~X + Y. */
1548 (simplify
1549 (bit_not (minus:s @0 @1))
1550 (plus (bit_not @0) @1))
1551 (simplify
1552 (bit_not (plus:s @0 INTEGER_CST@1))
1553 (if ((INTEGRAL_TYPE_P (type)
1554 && TYPE_UNSIGNED (type))
1555 || (!TYPE_OVERFLOW_SANITIZED (type)
1556 && may_negate_without_overflow_p (@1)))
1557 (plus (bit_not @0) { const_unop (NEGATE_EXPR, type, @1); })))
1558
1559 #if GIMPLE
1560 /* ~X + Y -> (Y - X) - 1. */
1561 (simplify
1562 (plus:c (bit_not @0) @1)
1563 (if (ANY_INTEGRAL_TYPE_P (type)
1564 && TYPE_OVERFLOW_WRAPS (type)
1565 /* -1 - X is folded to ~X, so we'd recurse endlessly. */
1566 && !integer_all_onesp (@1))
1567 (plus (minus @1 @0) { build_minus_one_cst (type); })
1568 (if (INTEGRAL_TYPE_P (type)
1569 && TREE_CODE (@1) == INTEGER_CST
1570 && wi::to_wide (@1) != wi::min_value (TYPE_PRECISION (type),
1571 SIGNED))
1572 (minus (plus @1 { build_minus_one_cst (type); }) @0))))
1573 #endif
1574
1575 /* ~(X >> Y) -> ~X >> Y if ~X can be simplified. */
1576 (simplify
1577 (bit_not (rshift:s @0 @1))
1578 (if (!TYPE_UNSIGNED (TREE_TYPE (@0)))
1579 (rshift (bit_not! @0) @1)
1580 /* For logical right shifts, this is possible only if @0 doesn't
1581 have MSB set and the logical right shift is changed into
1582 arithmetic shift. */
1583 (if (INTEGRAL_TYPE_P (type)
1584 && !wi::neg_p (tree_nonzero_bits (@0)))
1585 (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
1586 (convert (rshift (bit_not! (convert:stype @0)) @1))))))
1587
1588 /* x + (x & 1) -> (x + 1) & ~1 */
1589 (simplify
1590 (plus:c @0 (bit_and:s @0 integer_onep@1))
1591 (bit_and (plus @0 @1) (bit_not @1)))
1592
1593 /* x & ~(x & y) -> x & ~y */
1594 /* x | ~(x | y) -> x | ~y */
1595 (for bitop (bit_and bit_ior)
1596 (simplify
1597 (bitop:c @0 (bit_not (bitop:cs @0 @1)))
1598 (bitop @0 (bit_not @1))))
1599
1600 /* (~x & y) | ~(x | y) -> ~x */
1601 (simplify
1602 (bit_ior:c (bit_and:c (bit_not@2 @0) @1) (bit_not (bit_ior:c @0 @1)))
1603 @2)
1604
1605 /* (x | y) ^ (x | ~y) -> ~x */
1606 (simplify
1607 (bit_xor:c (bit_ior:c @0 @1) (bit_ior:c @0 (bit_not @1)))
1608 (bit_not @0))
1609
1610 /* (x & y) | ~(x | y) -> ~(x ^ y) */
1611 (simplify
1612 (bit_ior:c (bit_and:s @0 @1) (bit_not:s (bit_ior:s @0 @1)))
1613 (bit_not (bit_xor @0 @1)))
1614
1615 /* (~x | y) ^ (x ^ y) -> x | ~y */
1616 (simplify
1617 (bit_xor:c (bit_ior:cs (bit_not @0) @1) (bit_xor:s @0 @1))
1618 (bit_ior @0 (bit_not @1)))
1619
1620 /* (x ^ y) | ~(x | y) -> ~(x & y) */
1621 (simplify
1622 (bit_ior:c (bit_xor:s @0 @1) (bit_not:s (bit_ior:s @0 @1)))
1623 (bit_not (bit_and @0 @1)))
1624
1625 /* (x & y) ^ (x | y) -> x ^ y */
1626 (simplify
1627 (bit_xor:c (bit_and @0 @1) (bit_ior @0 @1))
1628 (bit_xor @0 @1))
1629
1630 /* (x ^ y) ^ (x | y) -> x & y */
1631 (simplify
1632 (bit_xor:c (bit_xor @0 @1) (bit_ior @0 @1))
1633 (bit_and @0 @1))
1634
1635 /* (x & y) + (x ^ y) -> x | y */
1636 /* (x & y) | (x ^ y) -> x | y */
1637 /* (x & y) ^ (x ^ y) -> x | y */
1638 (for op (plus bit_ior bit_xor)
1639 (simplify
1640 (op:c (bit_and @0 @1) (bit_xor @0 @1))
1641 (bit_ior @0 @1)))
1642
1643 /* (x & y) + (x | y) -> x + y */
1644 (simplify
1645 (plus:c (bit_and @0 @1) (bit_ior @0 @1))
1646 (plus @0 @1))
1647
1648 /* (x + y) - (x | y) -> x & y */
1649 (simplify
1650 (minus (plus @0 @1) (bit_ior @0 @1))
1651 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1652 && !TYPE_SATURATING (type))
1653 (bit_and @0 @1)))
1654
1655 /* (x + y) - (x & y) -> x | y */
1656 (simplify
1657 (minus (plus @0 @1) (bit_and @0 @1))
1658 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1659 && !TYPE_SATURATING (type))
1660 (bit_ior @0 @1)))
1661
1662 /* (x | y) - y -> (x & ~y) */
1663 (simplify
1664 (minus (bit_ior:cs @0 @1) @1)
1665 (bit_and @0 (bit_not @1)))
1666
1667 /* (x | y) - (x ^ y) -> x & y */
1668 (simplify
1669 (minus (bit_ior @0 @1) (bit_xor @0 @1))
1670 (bit_and @0 @1))
1671
1672 /* (x | y) - (x & y) -> x ^ y */
1673 (simplify
1674 (minus (bit_ior @0 @1) (bit_and @0 @1))
1675 (bit_xor @0 @1))
1676
1677 /* (x | y) & ~(x & y) -> x ^ y */
1678 (simplify
1679 (bit_and:c (bit_ior @0 @1) (bit_not (bit_and @0 @1)))
1680 (bit_xor @0 @1))
1681
1682 /* (x | y) & (~x ^ y) -> x & y */
1683 (simplify
1684 (bit_and:c (bit_ior:c @0 @1) (bit_xor:c @1 @2))
1685 (with { bool wascmp; }
1686 (if (bitwise_inverted_equal_p (@0, @2, wascmp)
1687 && (!wascmp || element_precision (type) == 1))
1688 (bit_and @0 @1))))
1689
1690 /* (~x | y) & (x | ~y) -> ~(x ^ y) */
1691 (simplify
1692 (bit_and (bit_ior:cs (bit_not @0) @1) (bit_ior:cs @0 (bit_not @1)))
1693 (bit_not (bit_xor @0 @1)))
1694
1695 /* (~x | y) ^ (x | ~y) -> x ^ y */
1696 (simplify
1697 (bit_xor (bit_ior:c (bit_not @0) @1) (bit_ior:c @0 (bit_not @1)))
1698 (bit_xor @0 @1))
1699
1700 /* ((x & y) - (x | y)) - 1 -> ~(x ^ y) */
1701 (simplify
1702 (plus (nop_convert1? (minus@2 (nop_convert2? (bit_and:c @0 @1))
1703 (nop_convert2? (bit_ior @0 @1))))
1704 integer_all_onesp)
1705 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1706 && !TYPE_SATURATING (type) && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2))
1707 && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@2))
1708 && !TYPE_SATURATING (TREE_TYPE (@2)))
1709 (bit_not (convert (bit_xor @0 @1)))))
1710 (simplify
1711 (minus (nop_convert1? (plus@2 (nop_convert2? (bit_and:c @0 @1))
1712 integer_all_onesp))
1713 (nop_convert3? (bit_ior @0 @1)))
1714 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1715 && !TYPE_SATURATING (type) && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2))
1716 && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@2))
1717 && !TYPE_SATURATING (TREE_TYPE (@2)))
1718 (bit_not (convert (bit_xor @0 @1)))))
1719 (simplify
1720 (minus (nop_convert1? (bit_and @0 @1))
1721 (nop_convert2? (plus@2 (nop_convert3? (bit_ior:c @0 @1))
1722 integer_onep)))
1723 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1724 && !TYPE_SATURATING (type) && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2))
1725 && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@2))
1726 && !TYPE_SATURATING (TREE_TYPE (@2)))
1727 (bit_not (convert (bit_xor @0 @1)))))
1728
1729 /* ~x & ~y -> ~(x | y)
1730 ~x | ~y -> ~(x & y) */
1731 (for op (bit_and bit_ior)
1732 rop (bit_ior bit_and)
1733 (simplify
1734 (op (convert1? (bit_not @0)) (convert2? (bit_not @1)))
1735 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1736 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
1737 (bit_not (rop (convert @0) (convert @1))))))
1738
1739 /* If we are XORing or adding two BIT_AND_EXPR's, both of which are and'ing
1740 with a constant, and the two constants have no bits in common,
1741 we should treat this as a BIT_IOR_EXPR since this may produce more
1742 simplifications. */
1743 (for op (bit_xor plus)
1744 (simplify
1745 (op (convert1? (bit_and@4 @0 INTEGER_CST@1))
1746 (convert2? (bit_and@5 @2 INTEGER_CST@3)))
1747 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
1748 && tree_nop_conversion_p (type, TREE_TYPE (@2))
1749 && (wi::to_wide (@1) & wi::to_wide (@3)) == 0)
1750 (bit_ior (convert @4) (convert @5)))))
1751
1752 /* (X | Y) ^ X -> Y & ~ X*/
1753 (simplify
1754 (bit_xor:c (convert1? (bit_ior:c @@0 @1)) (convert2? @0))
1755 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1756 (convert (bit_and @1 (bit_not @0)))))
1757
1758 /* (~X | Y) ^ X -> ~(X & Y). */
1759 (simplify
1760 (bit_xor:c (nop_convert1? (bit_ior:c (nop_convert2? (bit_not @0)) @1)) @2)
1761 (if (bitwise_equal_p (@0, @2))
1762 (convert (bit_not (bit_and @0 (convert @1))))))
1763
1764 /* Convert ~X ^ ~Y to X ^ Y. */
1765 (simplify
1766 (bit_xor (convert1? (bit_not @0)) (convert2? (bit_not @1)))
1767 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1768 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
1769 (bit_xor (convert @0) (convert @1))))
1770
1771 /* Convert ~X ^ C to X ^ ~C. */
1772 (simplify
1773 (bit_xor (convert? (bit_not @0)) INTEGER_CST@1)
1774 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1775 (bit_xor (convert @0) (bit_not @1))))
1776
1777 /* Fold (X & Y) ^ Y and (X ^ Y) & Y as ~X & Y. */
1778 (for opo (bit_and bit_xor)
1779 opi (bit_xor bit_and)
1780 (simplify
1781 (opo:c (opi:cs @0 @1) @1)
1782 (bit_and (bit_not @0) @1)))
1783
1784 /* Given a bit-wise operation CODE applied to ARG0 and ARG1, see if both
1785 operands are another bit-wise operation with a common input. If so,
1786 distribute the bit operations to save an operation and possibly two if
1787 constants are involved. For example, convert
1788 (A | B) & (A | C) into A | (B & C)
1789 Further simplification will occur if B and C are constants. */
1790 (for op (bit_and bit_ior bit_xor)
1791 rop (bit_ior bit_and bit_and)
1792 (simplify
1793 (op (convert? (rop:c @@0 @1)) (convert? (rop:c @0 @2)))
1794 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1795 && tree_nop_conversion_p (type, TREE_TYPE (@2)))
1796 (rop (convert @0) (op (convert @1) (convert @2))))))
1797
1798 /* Some simple reassociation for bit operations, also handled in reassoc. */
1799 /* (X & Y) & Y -> X & Y
1800 (X | Y) | Y -> X | Y */
1801 (for op (bit_and bit_ior)
1802 (simplify
1803 (op:c (convert1?@2 (op:c @0 @@1)) (convert2? @1))
1804 @2))
1805 /* (X ^ Y) ^ Y -> X */
1806 (simplify
1807 (bit_xor:c (convert1? (bit_xor:c @0 @@1)) (convert2? @1))
1808 (convert @0))
1809
1810 /* (X & ~Y) & Y -> 0 */
1811 (simplify
1812 (bit_and:c (bit_and @0 @1) @2)
1813 (with { bool wascmp; }
1814 (if (bitwise_inverted_equal_p (@0, @2, wascmp)
1815 || bitwise_inverted_equal_p (@1, @2, wascmp))
1816 { wascmp ? constant_boolean_node (false, type) : build_zero_cst (type); })))
1817 /* (X | ~Y) | Y -> -1 */
1818 (simplify
1819 (bit_ior:c (bit_ior @0 @1) @2)
1820 (with { bool wascmp; }
1821 (if ((bitwise_inverted_equal_p (@0, @2, wascmp)
1822 || bitwise_inverted_equal_p (@1, @2, wascmp))
1823 && (!wascmp || element_precision (type) == 1))
1824 { build_all_ones_cst (TREE_TYPE (@0)); })))
1825
1826 /* (X & Y) & (X & Z) -> (X & Y) & Z
1827 (X | Y) | (X | Z) -> (X | Y) | Z */
1828 (for op (bit_and bit_ior)
1829 (simplify
1830 (op (convert1?@3 (op:c@4 @0 @1)) (convert2?@5 (op:c@6 @0 @2)))
1831 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1832 && tree_nop_conversion_p (type, TREE_TYPE (@2)))
1833 (if (single_use (@5) && single_use (@6))
1834 (op @3 (convert @2))
1835 (if (single_use (@3) && single_use (@4))
1836 (op (convert @1) @5))))))
1837 /* (X ^ Y) ^ (X ^ Z) -> Y ^ Z */
1838 (simplify
1839 (bit_xor (convert1? (bit_xor:c @0 @1)) (convert2? (bit_xor:c @0 @2)))
1840 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1841 && tree_nop_conversion_p (type, TREE_TYPE (@2)))
1842 (bit_xor (convert @1) (convert @2))))
1843
1844 /* Convert abs (abs (X)) into abs (X).
1845 also absu (absu (X)) into absu (X). */
1846 (simplify
1847 (abs (abs@1 @0))
1848 @1)
1849
1850 (simplify
1851 (absu (convert@2 (absu@1 @0)))
1852 (if (tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@1)))
1853 @1))
1854
1855 /* Convert abs[u] (-X) -> abs[u] (X). */
1856 (simplify
1857 (abs (negate @0))
1858 (abs @0))
1859
1860 (simplify
1861 (absu (negate @0))
1862 (absu @0))
1863
1864 /* Convert abs[u] (X) where X is nonnegative -> (X). */
1865 (simplify
1866 (abs tree_expr_nonnegative_p@0)
1867 @0)
1868
1869 (simplify
1870 (absu tree_expr_nonnegative_p@0)
1871 (convert @0))
1872
1873 /* Simplify (-(X < 0) | 1) * X into abs (X) or absu(X). */
1874 (simplify
1875 (mult:c (nop_convert1?
1876 (bit_ior (nop_convert2? (negate (convert? (lt @0 integer_zerop))))
1877 integer_onep))
1878 (nop_convert3? @0))
1879 (if (INTEGRAL_TYPE_P (type)
1880 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
1881 && !TYPE_UNSIGNED (TREE_TYPE (@0)))
1882 (if (TYPE_UNSIGNED (type))
1883 (absu @0)
1884 (abs @0)
1885 )
1886 )
1887 )
1888
1889 /* A few cases of fold-const.cc negate_expr_p predicate. */
1890 (match negate_expr_p
1891 INTEGER_CST
1892 (if ((INTEGRAL_TYPE_P (type)
1893 && TYPE_UNSIGNED (type))
1894 || (!TYPE_OVERFLOW_SANITIZED (type)
1895 && may_negate_without_overflow_p (t)))))
1896 (match negate_expr_p
1897 FIXED_CST)
1898 (match negate_expr_p
1899 (negate @0)
1900 (if (!TYPE_OVERFLOW_SANITIZED (type))))
1901 (match negate_expr_p
1902 REAL_CST
1903 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (t)))))
1904 /* VECTOR_CST handling of non-wrapping types would recurse in unsupported
1905 ways. */
1906 (match negate_expr_p
1907 VECTOR_CST
1908 (if (FLOAT_TYPE_P (TREE_TYPE (type)) || TYPE_OVERFLOW_WRAPS (type))))
1909 (match negate_expr_p
1910 (minus @0 @1)
1911 (if ((ANY_INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_WRAPS (type))
1912 || (FLOAT_TYPE_P (type)
1913 && !HONOR_SIGN_DEPENDENT_ROUNDING (type)
1914 && !HONOR_SIGNED_ZEROS (type)))))
1915
1916 /* (-A) * (-B) -> A * B */
1917 (simplify
1918 (mult:c (convert1? (negate @0)) (convert2? negate_expr_p@1))
1919 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
1920 && tree_nop_conversion_p (type, TREE_TYPE (@1)))
1921 (mult (convert @0) (convert (negate @1)))))
1922
1923 /* -(A + B) -> (-B) - A. */
1924 (simplify
1925 (negate (plus:c @0 negate_expr_p@1))
1926 (if (!HONOR_SIGN_DEPENDENT_ROUNDING (type)
1927 && !HONOR_SIGNED_ZEROS (type))
1928 (minus (negate @1) @0)))
1929
1930 /* -(A - B) -> B - A. */
1931 (simplify
1932 (negate (minus @0 @1))
1933 (if ((ANY_INTEGRAL_TYPE_P (type) && !TYPE_OVERFLOW_SANITIZED (type))
1934 || (FLOAT_TYPE_P (type)
1935 && !HONOR_SIGN_DEPENDENT_ROUNDING (type)
1936 && !HONOR_SIGNED_ZEROS (type)))
1937 (minus @1 @0)))
1938 (simplify
1939 (negate (pointer_diff @0 @1))
1940 (if (TYPE_OVERFLOW_UNDEFINED (type))
1941 (pointer_diff @1 @0)))
1942
1943 /* A - B -> A + (-B) if B is easily negatable. */
1944 (simplify
1945 (minus @0 negate_expr_p@1)
1946 (if (!FIXED_POINT_TYPE_P (type))
1947 (plus @0 (negate @1))))
1948
1949 /* 1 - a is a ^ 1 if a had a bool range. */
1950 /* This is only enabled for gimple as sometimes
1951 cfun is not set for the function which contains
1952 the SSA_NAME (e.g. while IPA passes are happening,
1953 fold might be called). */
1954 (simplify
1955 (minus integer_onep@0 SSA_NAME@1)
1956 (if (INTEGRAL_TYPE_P (type)
1957 && ssa_name_has_boolean_range (@1))
1958 (bit_xor @1 @0)))
1959
1960 /* Other simplifications of negation (c.f. fold_negate_expr_1). */
1961 (simplify
1962 (negate (mult:c@0 @1 negate_expr_p@2))
1963 (if (! TYPE_UNSIGNED (type)
1964 && ! HONOR_SIGN_DEPENDENT_ROUNDING (type)
1965 && single_use (@0))
1966 (mult @1 (negate @2))))
1967
1968 (simplify
1969 (negate (rdiv@0 @1 negate_expr_p@2))
1970 (if (! HONOR_SIGN_DEPENDENT_ROUNDING (type)
1971 && single_use (@0))
1972 (rdiv @1 (negate @2))))
1973
1974 (simplify
1975 (negate (rdiv@0 negate_expr_p@1 @2))
1976 (if (! HONOR_SIGN_DEPENDENT_ROUNDING (type)
1977 && single_use (@0))
1978 (rdiv (negate @1) @2)))
1979
1980 /* Fold -((int)x >> (prec - 1)) into (unsigned)x >> (prec - 1). */
1981 (simplify
1982 (negate (convert? (rshift @0 INTEGER_CST@1)))
1983 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
1984 && wi::to_wide (@1) == element_precision (type) - 1)
1985 (with { tree stype = TREE_TYPE (@0);
1986 tree ntype = TYPE_UNSIGNED (stype) ? signed_type_for (stype)
1987 : unsigned_type_for (stype); }
1988 (if (VECTOR_TYPE_P (type))
1989 (view_convert (rshift (view_convert:ntype @0) @1))
1990 (convert (rshift (convert:ntype @0) @1))))))
1991
1992 /* Try to fold (type) X op CST -> (type) (X op ((type-x) CST))
1993 when profitable.
1994 For bitwise binary operations apply operand conversions to the
1995 binary operation result instead of to the operands. This allows
1996 to combine successive conversions and bitwise binary operations.
1997 We combine the above two cases by using a conditional convert. */
1998 (for bitop (bit_and bit_ior bit_xor)
1999 (simplify
2000 (bitop (convert@2 @0) (convert?@3 @1))
2001 (if (((TREE_CODE (@1) == INTEGER_CST
2002 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
2003 && (int_fits_type_p (@1, TREE_TYPE (@0))
2004 || tree_nop_conversion_p (TREE_TYPE (@0), type)))
2005 || types_match (@0, @1))
2006 && !POINTER_TYPE_P (TREE_TYPE (@0))
2007 && !VECTOR_TYPE_P (TREE_TYPE (@0))
2008 && TREE_CODE (TREE_TYPE (@0)) != OFFSET_TYPE
2009 /* ??? This transform conflicts with fold-const.cc doing
2010 Convert (T)(x & c) into (T)x & (T)c, if c is an integer
2011 constants (if x has signed type, the sign bit cannot be set
2012 in c). This folds extension into the BIT_AND_EXPR.
2013 Restrict it to GIMPLE to avoid endless recursions. */
2014 && (bitop != BIT_AND_EXPR || GIMPLE)
2015 && (/* That's a good idea if the conversion widens the operand, thus
2016 after hoisting the conversion the operation will be narrower.
2017 It is also a good if the conversion is a nop as moves the
2018 conversion to one side; allowing for combining of the conversions. */
2019 TYPE_PRECISION (TREE_TYPE (@0)) < TYPE_PRECISION (type)
2020 /* The conversion check for being a nop can only be done at the gimple
2021 level as fold_binary has some re-association code which can conflict
2022 with this if there is a "constant" which is not a full INTEGER_CST. */
2023 || (GIMPLE && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (type))
2024 /* It's also a good idea if the conversion is to a non-integer
2025 mode. */
2026 || GET_MODE_CLASS (TYPE_MODE (type)) != MODE_INT
2027 /* Or if the precision of TO is not the same as the precision
2028 of its mode. */
2029 || !type_has_mode_precision_p (type)
2030 /* In GIMPLE, getting rid of 2 conversions for one new results
2031 in smaller IL. */
2032 || (GIMPLE
2033 && TREE_CODE (@1) != INTEGER_CST
2034 && tree_nop_conversion_p (type, TREE_TYPE (@0))
2035 && single_use (@2)
2036 && single_use (@3))))
2037 (convert (bitop @0 (convert @1)))))
2038 /* In GIMPLE, getting rid of 2 conversions for one new results
2039 in smaller IL. */
2040 (simplify
2041 (convert (bitop:cs@2 (nop_convert:s @0) @1))
2042 (if (GIMPLE
2043 && TREE_CODE (@1) != INTEGER_CST
2044 && tree_nop_conversion_p (type, TREE_TYPE (@2))
2045 && types_match (type, @0)
2046 && !POINTER_TYPE_P (TREE_TYPE (@0))
2047 && TREE_CODE (TREE_TYPE (@0)) != OFFSET_TYPE)
2048 (bitop @0 (convert @1)))))
2049
2050 (for bitop (bit_and bit_ior)
2051 rbitop (bit_ior bit_and)
2052 /* (x | y) & x -> x */
2053 /* (x & y) | x -> x */
2054 (simplify
2055 (bitop:c (rbitop:c @0 @1) @0)
2056 @0)
2057 /* (~x | y) & x -> x & y */
2058 /* (~x & y) | x -> x | y */
2059 (simplify
2060 (bitop:c (rbitop:c @2 @1) @0)
2061 (with { bool wascmp; }
2062 (if (bitwise_inverted_equal_p (@0, @2, wascmp)
2063 && (!wascmp || element_precision (type) == 1))
2064 (bitop @0 @1))))
2065 /* (x | y) & (x & z) -> (x & z) */
2066 /* (x & y) | (x | z) -> (x | z) */
2067 (simplify
2068 (bitop:c (rbitop:c @0 @1) (bitop:c@3 @0 @2))
2069 @3)
2070 /* (x | c) & ~(y | c) -> x & ~(y | c) */
2071 /* (x & c) | ~(y & c) -> x | ~(y & c) */
2072 (simplify
2073 (bitop:c (rbitop:c @0 @1) (bit_not@3 (rbitop:c @1 @2)))
2074 (bitop @0 @3))
2075 /* x & ~(y | x) -> 0 */
2076 /* x | ~(y & x) -> -1 */
2077 (simplify
2078 (bitop:c @0 (bit_not (rbitop:c @0 @1)))
2079 (if (bitop == BIT_AND_EXPR)
2080 { build_zero_cst (type); }
2081 { build_minus_one_cst (type); })))
2082
2083 /* ((x | y) & z) | x -> (z & y) | x
2084 ((x ^ y) & z) | x -> (z & y) | x */
2085 (for op (bit_ior bit_xor)
2086 (simplify
2087 (bit_ior:c (nop_convert1?:s
2088 (bit_and:cs (nop_convert2?:s (op:cs @0 @1)) @2)) @3)
2089 (if (bitwise_equal_p (@0, @3))
2090 (convert (bit_ior (bit_and @1 (convert @2)) (convert @0))))))
2091
2092 /* (x | CST1) & CST2 -> (x & CST2) | (CST1 & CST2) */
2093 (simplify
2094 (bit_and (bit_ior @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
2095 (bit_ior (bit_and @0 @2) (bit_and! @1 @2)))
2096
2097 /* Combine successive equal operations with constants. */
2098 (for bitop (bit_and bit_ior bit_xor)
2099 (simplify
2100 (bitop (bitop @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
2101 (if (!CONSTANT_CLASS_P (@0))
2102 /* This is the canonical form regardless of whether (bitop @1 @2) can be
2103 folded to a constant. */
2104 (bitop @0 (bitop! @1 @2))
2105 /* In this case we have three constants and (bitop @0 @1) doesn't fold
2106 to a constant. This can happen if @0 or @1 is a POLY_INT_CST and if
2107 the values involved are such that the operation can't be decided at
2108 compile time. Try folding one of @0 or @1 with @2 to see whether
2109 that combination can be decided at compile time.
2110
2111 Keep the existing form if both folds fail, to avoid endless
2112 oscillation. */
2113 (with { tree cst1 = const_binop (bitop, type, @0, @2); }
2114 (if (cst1)
2115 (bitop @1 { cst1; })
2116 (with { tree cst2 = const_binop (bitop, type, @1, @2); }
2117 (if (cst2)
2118 (bitop @0 { cst2; }))))))))
2119
2120 /* Try simple folding for X op !X, and X op X with the help
2121 of the truth_valued_p and logical_inverted_value predicates. */
2122 (match truth_valued_p
2123 @0
2124 (if (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1)))
2125 (for op (tcc_comparison truth_and truth_andif truth_or truth_orif truth_xor)
2126 (match truth_valued_p
2127 (op @0 @1)))
2128 (match truth_valued_p
2129 (truth_not @0))
2130
2131 (match (logical_inverted_value @0)
2132 (truth_not @0))
2133 (match (logical_inverted_value @0)
2134 (bit_not truth_valued_p@0))
2135 (match (logical_inverted_value @0)
2136 (eq @0 integer_zerop))
2137 (match (logical_inverted_value @0)
2138 (ne truth_valued_p@0 integer_truep))
2139 (match (logical_inverted_value @0)
2140 (bit_xor truth_valued_p@0 integer_truep))
2141
2142 /* X & !X -> 0. */
2143 (simplify
2144 (bit_and:c @0 (logical_inverted_value @0))
2145 { build_zero_cst (type); })
2146 /* X | !X and X ^ !X -> 1, , if X is truth-valued. */
2147 (for op (bit_ior bit_xor)
2148 (simplify
2149 (op:c truth_valued_p@0 (logical_inverted_value @0))
2150 { constant_boolean_node (true, type); }))
2151 /* X ==/!= !X is false/true. */
2152 (for op (eq ne)
2153 (simplify
2154 (op:c truth_valued_p@0 (logical_inverted_value @0))
2155 { constant_boolean_node (op == NE_EXPR ? true : false, type); }))
2156
2157 /* ~~x -> x */
2158 (simplify
2159 (bit_not (bit_not @0))
2160 @0)
2161
2162 /* zero_one_valued_p will match when a value is known to be either
2163 0 or 1 including constants 0 or 1.
2164 Signed 1-bits includes -1 so they cannot match here. */
2165 (match zero_one_valued_p
2166 @0
2167 (if (INTEGRAL_TYPE_P (type)
2168 && (TYPE_UNSIGNED (type)
2169 || TYPE_PRECISION (type) > 1)
2170 && wi::leu_p (tree_nonzero_bits (@0), 1))))
2171 (match zero_one_valued_p
2172 truth_valued_p@0
2173 (if (INTEGRAL_TYPE_P (type)
2174 && (TYPE_UNSIGNED (type)
2175 || TYPE_PRECISION (type) > 1))))
2176
2177 /* (a&1) is always [0,1] too. This is useful again when
2178 the range is not known. */
2179 /* Note this can't be recursive due to VN handling of equivalents,
2180 VN and would cause an infinite recursion. */
2181 (match zero_one_valued_p
2182 (bit_and:c@0 @1 integer_onep)
2183 (if (INTEGRAL_TYPE_P (type))))
2184
2185 /* A conversion from an zero_one_valued_p is still a [0,1].
2186 This is useful when the range of a variable is not known */
2187 /* Note this matches can't be recursive because of the way VN handles
2188 nop conversions being equivalent and then recursive between them. */
2189 (match zero_one_valued_p
2190 (convert@0 @1)
2191 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2192 && (TYPE_UNSIGNED (TREE_TYPE (@1))
2193 || TYPE_PRECISION (TREE_TYPE (@1)) > 1)
2194 && wi::leu_p (tree_nonzero_bits (@1), 1))))
2195
2196 /* Transform { 0 or 1 } * { 0 or 1 } into { 0 or 1 } & { 0 or 1 }. */
2197 (simplify
2198 (mult zero_one_valued_p@0 zero_one_valued_p@1)
2199 (if (INTEGRAL_TYPE_P (type))
2200 (bit_and @0 @1)))
2201
2202 (for cmp (tcc_comparison)
2203 icmp (inverted_tcc_comparison)
2204 /* Fold (((a < b) & c) | ((a >= b) & d)) into (a < b ? c : d) & 1. */
2205 (simplify
2206 (bit_ior
2207 (bit_and:c (convert? (cmp@0 @01 @02)) @3)
2208 (bit_and:c (convert? (icmp@4 @01 @02)) @5))
2209 (if (INTEGRAL_TYPE_P (type)
2210 && invert_tree_comparison (cmp, HONOR_NANS (@01)) == icmp
2211 /* The scalar version has to be canonicalized after vectorization
2212 because it makes unconditional loads conditional ones, which
2213 means we lose vectorization because the loads may trap. */
2214 && canonicalize_math_after_vectorization_p ())
2215 (bit_and (cond @0 @3 @5) { build_one_cst (type); })))
2216
2217 /* Fold ((-(a < b) & c) | (-(a >= b) & d)) into a < b ? c : d. This is
2218 canonicalized further and we recognize the conditional form:
2219 (a < b ? c : 0) | (a >= b ? d : 0) into a < b ? c : d. */
2220 (simplify
2221 (bit_ior
2222 (cond (cmp@0 @01 @02) @3 zerop)
2223 (cond (icmp@4 @01 @02) @5 zerop))
2224 (if (INTEGRAL_TYPE_P (type)
2225 && invert_tree_comparison (cmp, HONOR_NANS (@01)) == icmp
2226 /* The scalar version has to be canonicalized after vectorization
2227 because it makes unconditional loads conditional ones, which
2228 means we lose vectorization because the loads may trap. */
2229 && canonicalize_math_after_vectorization_p ())
2230 (cond @0 @3 @5)))
2231
2232 /* Vector Fold (((a < b) & c) | ((a >= b) & d)) into a < b ? c : d.
2233 and ((~(a < b) & c) | (~(a >= b) & d)) into a < b ? c : d. */
2234 (simplify
2235 (bit_ior
2236 (bit_and:c (vec_cond:s (cmp@0 @6 @7) @4 @5) @2)
2237 (bit_and:c (vec_cond:s (icmp@1 @6 @7) @4 @5) @3))
2238 (if (integer_zerop (@5)
2239 && invert_tree_comparison (cmp, HONOR_NANS (@6)) == icmp)
2240 (switch
2241 (if (integer_onep (@4))
2242 (bit_and (vec_cond @0 @2 @3) @4))
2243 (if (integer_minus_onep (@4))
2244 (vec_cond @0 @2 @3)))
2245 (if (integer_zerop (@4)
2246 && invert_tree_comparison (cmp, HONOR_NANS (@6)) == icmp)
2247 (switch
2248 (if (integer_onep (@5))
2249 (bit_and (vec_cond @0 @3 @2) @5))
2250 (if (integer_minus_onep (@5))
2251 (vec_cond @0 @3 @2))))))
2252
2253 /* Scalar Vectorized Fold ((-(a < b) & c) | (-(a >= b) & d))
2254 into a < b ? d : c. */
2255 (simplify
2256 (bit_ior
2257 (vec_cond:s (cmp@0 @4 @5) @2 integer_zerop)
2258 (vec_cond:s (icmp@1 @4 @5) @3 integer_zerop))
2259 (if (invert_tree_comparison (cmp, HONOR_NANS (@4)) == icmp)
2260 (vec_cond @0 @2 @3))))
2261
2262 /* Transform X & -Y into X * Y when Y is { 0 or 1 }. */
2263 (simplify
2264 (bit_and:c (convert? (negate zero_one_valued_p@0)) @1)
2265 (if (INTEGRAL_TYPE_P (type)
2266 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
2267 && TREE_CODE (TREE_TYPE (@0)) != BOOLEAN_TYPE
2268 /* Sign extending of the neg or a truncation of the neg
2269 is needed. */
2270 && (!TYPE_UNSIGNED (TREE_TYPE (@0))
2271 || TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))))
2272 (mult (convert @0) @1)))
2273
2274 /* Narrow integer multiplication by a zero_one_valued_p operand.
2275 Multiplication by [0,1] is guaranteed not to overflow. */
2276 (simplify
2277 (convert (mult@0 zero_one_valued_p@1 INTEGER_CST@2))
2278 (if (INTEGRAL_TYPE_P (type)
2279 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
2280 && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (@0)))
2281 (mult (convert @1) (convert @2))))
2282
2283 /* (X << C) != 0 can be simplified to X, when C is zero_one_valued_p.
2284 Check that the shift is well-defined (C is less than TYPE_PRECISION)
2285 as some targets (such as x86's SSE) may return zero for larger C. */
2286 (simplify
2287 (ne (lshift zero_one_valued_p@0 INTEGER_CST@1) integer_zerop@2)
2288 (if (tree_fits_shwi_p (@1)
2289 && tree_to_shwi (@1) > 0
2290 && tree_to_shwi (@1) < TYPE_PRECISION (TREE_TYPE (@0)))
2291 (convert @0)))
2292
2293 /* (X << C) == 0 can be simplified to X == 0, when C is zero_one_valued_p.
2294 Check that the shift is well-defined (C is less than TYPE_PRECISION)
2295 as some targets (such as x86's SSE) may return zero for larger C. */
2296 (simplify
2297 (eq (lshift zero_one_valued_p@0 INTEGER_CST@1) integer_zerop@2)
2298 (if (tree_fits_shwi_p (@1)
2299 && tree_to_shwi (@1) > 0
2300 && tree_to_shwi (@1) < TYPE_PRECISION (TREE_TYPE (@0)))
2301 (eq @0 @2)))
2302
2303 /* Convert ~ (-A) to A - 1. */
2304 (simplify
2305 (bit_not (convert? (negate @0)))
2306 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
2307 || !TYPE_UNSIGNED (TREE_TYPE (@0)))
2308 (convert (minus @0 { build_each_one_cst (TREE_TYPE (@0)); }))))
2309
2310 /* Convert - (~A) to A + 1. */
2311 (simplify
2312 (negate (nop_convert? (bit_not @0)))
2313 (plus (view_convert @0) { build_each_one_cst (type); }))
2314
2315 /* (a & b) ^ (a == b) -> !(a | b) */
2316 /* (a & b) == (a ^ b) -> !(a | b) */
2317 (for first_op (bit_xor eq)
2318 second_op (eq bit_xor)
2319 (simplify
2320 (first_op:c (bit_and:c truth_valued_p@0 truth_valued_p@1) (second_op:c @0 @1))
2321 (bit_not (bit_ior @0 @1))))
2322
2323 /* Convert ~ (A - 1) or ~ (A + -1) to -A. */
2324 (simplify
2325 (bit_not (convert? (minus @0 integer_each_onep)))
2326 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
2327 || !TYPE_UNSIGNED (TREE_TYPE (@0)))
2328 (convert (negate @0))))
2329 (simplify
2330 (bit_not (convert? (plus @0 integer_all_onesp)))
2331 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
2332 || !TYPE_UNSIGNED (TREE_TYPE (@0)))
2333 (convert (negate @0))))
2334
2335 /* Part of convert ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify. */
2336 (simplify
2337 (bit_not (convert? (bit_xor @0 INTEGER_CST@1)))
2338 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2339 (convert (bit_xor @0 (bit_not @1)))))
2340 (simplify
2341 (bit_not (convert? (bit_xor:c (bit_not @0) @1)))
2342 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2343 (convert (bit_xor @0 @1))))
2344
2345 /* Otherwise prefer ~(X ^ Y) to ~X ^ Y as more canonical. */
2346 (simplify
2347 (bit_xor:c (nop_convert?:s (bit_not:s @0)) @1)
2348 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2349 (bit_not (bit_xor (view_convert @0) @1))))
2350
2351 /* ~(a ^ b) is a == b for truth valued a and b. */
2352 (simplify
2353 (bit_not (bit_xor:s truth_valued_p@0 truth_valued_p@1))
2354 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2355 && TYPE_PRECISION (TREE_TYPE (@0)) == 1)
2356 (convert (eq @0 @1))))
2357
2358 /* (~a) == b is a ^ b for truth valued a and b. */
2359 (simplify
2360 (eq:c (bit_not:s truth_valued_p@0) truth_valued_p@1)
2361 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2362 && TYPE_PRECISION (TREE_TYPE (@0)) == 1)
2363 (convert (bit_xor @0 @1))))
2364
2365 /* (x & ~m) | (y & m) -> ((x ^ y) & m) ^ x */
2366 (simplify
2367 (bit_ior:c (bit_and:cs @0 (bit_not @2)) (bit_and:cs @1 @2))
2368 (bit_xor (bit_and (bit_xor @0 @1) @2) @0))
2369
2370 /* Fold A - (A & B) into ~B & A. */
2371 (simplify
2372 (minus (convert1? @0) (convert2?:s (bit_and:cs @@0 @1)))
2373 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
2374 && tree_nop_conversion_p (type, TREE_TYPE (@1)))
2375 (convert (bit_and (bit_not @1) @0))))
2376
2377 /* (m1 CMP m2) * d -> (m1 CMP m2) ? d : 0 */
2378 (if (!canonicalize_math_p ())
2379 (for cmp (tcc_comparison)
2380 (simplify
2381 (mult:c (convert (cmp@0 @1 @2)) @3)
2382 (if (INTEGRAL_TYPE_P (type)
2383 && INTEGRAL_TYPE_P (TREE_TYPE (@0)))
2384 (cond @0 @3 { build_zero_cst (type); })))
2385 /* (-(m1 CMP m2)) & d -> (m1 CMP m2) ? d : 0 */
2386 (simplify
2387 (bit_and:c (negate (convert (cmp@0 @1 @2))) @3)
2388 (if (INTEGRAL_TYPE_P (type)
2389 && INTEGRAL_TYPE_P (TREE_TYPE (@0)))
2390 (cond @0 @3 { build_zero_cst (type); })))
2391 )
2392 )
2393
2394 /* For integral types with undefined overflow and C != 0 fold
2395 x * C EQ/NE y * C into x EQ/NE y. */
2396 (for cmp (eq ne)
2397 (simplify
2398 (cmp (mult:c @0 @1) (mult:c @2 @1))
2399 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2400 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2401 && tree_expr_nonzero_p (@1))
2402 (cmp @0 @2))))
2403
2404 /* For integral types with wrapping overflow and C odd fold
2405 x * C EQ/NE y * C into x EQ/NE y. */
2406 (for cmp (eq ne)
2407 (simplify
2408 (cmp (mult @0 INTEGER_CST@1) (mult @2 @1))
2409 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2410 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))
2411 && (TREE_INT_CST_LOW (@1) & 1) != 0)
2412 (cmp @0 @2))))
2413
2414 /* For integral types with undefined overflow and C != 0 fold
2415 x * C RELOP y * C into:
2416
2417 x RELOP y for nonnegative C
2418 y RELOP x for negative C */
2419 (for cmp (lt gt le ge)
2420 (simplify
2421 (cmp (mult:c @0 @1) (mult:c @2 @1))
2422 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2423 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
2424 (if (tree_expr_nonnegative_p (@1) && tree_expr_nonzero_p (@1))
2425 (cmp @0 @2)
2426 (if (TREE_CODE (@1) == INTEGER_CST
2427 && wi::neg_p (wi::to_wide (@1), TYPE_SIGN (TREE_TYPE (@1))))
2428 (cmp @2 @0))))))
2429
2430 /* (X - 1U) <= INT_MAX-1U into (int) X > 0. */
2431 (for cmp (le gt)
2432 icmp (gt le)
2433 (simplify
2434 (cmp (plus @0 integer_minus_onep@1) INTEGER_CST@2)
2435 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2436 && TYPE_UNSIGNED (TREE_TYPE (@0))
2437 && TYPE_PRECISION (TREE_TYPE (@0)) > 1
2438 && (wi::to_wide (@2)
2439 == wi::max_value (TYPE_PRECISION (TREE_TYPE (@0)), SIGNED) - 1))
2440 (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
2441 (icmp (convert:stype @0) { build_int_cst (stype, 0); })))))
2442
2443 /* X / 4 < Y / 4 iff X < Y when the division is known to be exact. */
2444 (for cmp (simple_comparison)
2445 (simplify
2446 (cmp (convert?@3 (exact_div @0 INTEGER_CST@2)) (convert? (exact_div @1 @2)))
2447 (if (element_precision (@3) >= element_precision (@0)
2448 && types_match (@0, @1))
2449 (if (wi::lt_p (wi::to_wide (@2), 0, TYPE_SIGN (TREE_TYPE (@2))))
2450 (if (!TYPE_UNSIGNED (TREE_TYPE (@3)))
2451 (cmp @1 @0)
2452 (if (tree_expr_nonzero_p (@0) && tree_expr_nonzero_p (@1))
2453 (with
2454 {
2455 tree utype = unsigned_type_for (TREE_TYPE (@0));
2456 }
2457 (cmp (convert:utype @1) (convert:utype @0)))))
2458 (if (wi::gt_p (wi::to_wide (@2), 1, TYPE_SIGN (TREE_TYPE (@2))))
2459 (if (TYPE_UNSIGNED (TREE_TYPE (@0)) || !TYPE_UNSIGNED (TREE_TYPE (@3)))
2460 (cmp @0 @1)
2461 (with
2462 {
2463 tree utype = unsigned_type_for (TREE_TYPE (@0));
2464 }
2465 (cmp (convert:utype @0) (convert:utype @1)))))))))
2466
2467 /* X / C1 op C2 into a simple range test. */
2468 (for cmp (simple_comparison)
2469 (simplify
2470 (cmp (trunc_div:s @0 INTEGER_CST@1) INTEGER_CST@2)
2471 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2472 && integer_nonzerop (@1)
2473 && !TREE_OVERFLOW (@1)
2474 && !TREE_OVERFLOW (@2))
2475 (with { tree lo, hi; bool neg_overflow;
2476 enum tree_code code = fold_div_compare (cmp, @1, @2, &lo, &hi,
2477 &neg_overflow); }
2478 (switch
2479 (if (code == LT_EXPR || code == GE_EXPR)
2480 (if (TREE_OVERFLOW (lo))
2481 { build_int_cst (type, (code == LT_EXPR) ^ neg_overflow); }
2482 (if (code == LT_EXPR)
2483 (lt @0 { lo; })
2484 (ge @0 { lo; }))))
2485 (if (code == LE_EXPR || code == GT_EXPR)
2486 (if (TREE_OVERFLOW (hi))
2487 { build_int_cst (type, (code == LE_EXPR) ^ neg_overflow); }
2488 (if (code == LE_EXPR)
2489 (le @0 { hi; })
2490 (gt @0 { hi; }))))
2491 (if (!lo && !hi)
2492 { build_int_cst (type, code == NE_EXPR); })
2493 (if (code == EQ_EXPR && !hi)
2494 (ge @0 { lo; }))
2495 (if (code == EQ_EXPR && !lo)
2496 (le @0 { hi; }))
2497 (if (code == NE_EXPR && !hi)
2498 (lt @0 { lo; }))
2499 (if (code == NE_EXPR && !lo)
2500 (gt @0 { hi; }))
2501 (if (GENERIC)
2502 { build_range_check (UNKNOWN_LOCATION, type, @0, code == EQ_EXPR,
2503 lo, hi); })
2504 (with
2505 {
2506 tree etype = range_check_type (TREE_TYPE (@0));
2507 if (etype)
2508 {
2509 hi = fold_convert (etype, hi);
2510 lo = fold_convert (etype, lo);
2511 hi = const_binop (MINUS_EXPR, etype, hi, lo);
2512 }
2513 }
2514 (if (etype && hi && !TREE_OVERFLOW (hi))
2515 (if (code == EQ_EXPR)
2516 (le (minus (convert:etype @0) { lo; }) { hi; })
2517 (gt (minus (convert:etype @0) { lo; }) { hi; })))))))))
2518
2519 /* X + Z < Y + Z is the same as X < Y when there is no overflow. */
2520 (for op (lt le ge gt)
2521 (simplify
2522 (op (plus:c @0 @2) (plus:c @1 @2))
2523 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2524 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
2525 (op @0 @1))))
2526
2527 /* As a special case, X + C < Y + C is the same as (signed) X < (signed) Y
2528 when C is an unsigned integer constant with only the MSB set, and X and
2529 Y have types of equal or lower integer conversion rank than C's. */
2530 (for op (lt le ge gt)
2531 (simplify
2532 (op (plus @1 INTEGER_CST@0) (plus @2 @0))
2533 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2534 && TYPE_UNSIGNED (TREE_TYPE (@0))
2535 && wi::only_sign_bit_p (wi::to_wide (@0)))
2536 (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
2537 (op (convert:stype @1) (convert:stype @2))))))
2538
2539 /* For equality and subtraction, this is also true with wrapping overflow. */
2540 (for op (eq ne minus)
2541 (simplify
2542 (op (plus:c @0 @2) (plus:c @1 @2))
2543 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2544 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2545 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
2546 (op @0 @1))))
2547
2548 /* X - Z < Y - Z is the same as X < Y when there is no overflow. */
2549 (for op (lt le ge gt)
2550 (simplify
2551 (op (minus @0 @2) (minus @1 @2))
2552 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2553 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
2554 (op @0 @1))))
2555 /* For equality and subtraction, this is also true with wrapping overflow. */
2556 (for op (eq ne minus)
2557 (simplify
2558 (op (minus @0 @2) (minus @1 @2))
2559 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2560 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2561 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
2562 (op @0 @1))))
2563 /* And for pointers... */
2564 (for op (simple_comparison)
2565 (simplify
2566 (op (pointer_diff@3 @0 @2) (pointer_diff @1 @2))
2567 (if (!TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
2568 (op @0 @1))))
2569 (simplify
2570 (minus (pointer_diff@3 @0 @2) (pointer_diff @1 @2))
2571 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@3))
2572 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
2573 (pointer_diff @0 @1)))
2574
2575 /* Z - X < Z - Y is the same as Y < X when there is no overflow. */
2576 (for op (lt le ge gt)
2577 (simplify
2578 (op (minus @2 @0) (minus @2 @1))
2579 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2580 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
2581 (op @1 @0))))
2582 /* For equality and subtraction, this is also true with wrapping overflow. */
2583 (for op (eq ne minus)
2584 (simplify
2585 (op (minus @2 @0) (minus @2 @1))
2586 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2587 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2588 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
2589 (op @1 @0))))
2590 /* And for pointers... */
2591 (for op (simple_comparison)
2592 (simplify
2593 (op (pointer_diff@3 @2 @0) (pointer_diff @2 @1))
2594 (if (!TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
2595 (op @1 @0))))
2596 (simplify
2597 (minus (pointer_diff@3 @2 @0) (pointer_diff @2 @1))
2598 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@3))
2599 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
2600 (pointer_diff @1 @0)))
2601
2602 /* X + Y < Y is the same as X < 0 when there is no overflow. */
2603 (for op (lt le gt ge)
2604 (simplify
2605 (op:c (plus:c@2 @0 @1) @1)
2606 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2607 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2608 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0))
2609 && (CONSTANT_CLASS_P (@0) || single_use (@2)))
2610 (op @0 { build_zero_cst (TREE_TYPE (@0)); }))))
2611 /* For equality, this is also true with wrapping overflow. */
2612 (for op (eq ne)
2613 (simplify
2614 (op:c (nop_convert?@3 (plus:c@2 @0 (convert1? @1))) (convert2? @1))
2615 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2616 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2617 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
2618 && (CONSTANT_CLASS_P (@0) || (single_use (@2) && single_use (@3)))
2619 && tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@2))
2620 && tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@1)))
2621 (op @0 { build_zero_cst (TREE_TYPE (@0)); })))
2622 (simplify
2623 (op:c (nop_convert?@3 (pointer_plus@2 (convert1? @0) @1)) (convert2? @0))
2624 (if (tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@0))
2625 && tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0))
2626 && (CONSTANT_CLASS_P (@1) || (single_use (@2) && single_use (@3))))
2627 (op @1 { build_zero_cst (TREE_TYPE (@1)); }))))
2628
2629 /* (&a + b) !=/== (&a[1] + c) -> (&a[0] - &a[1]) + b !=/== c */
2630 (for neeq (ne eq)
2631 (simplify
2632 (neeq:c ADDR_EXPR@0 (pointer_plus @2 @3))
2633 (with { poly_int64 diff; tree inner_type = TREE_TYPE (@3);}
2634 (if (ptr_difference_const (@0, @2, &diff))
2635 (neeq { build_int_cst_type (inner_type, diff); } @3))))
2636 (simplify
2637 (neeq (pointer_plus ADDR_EXPR@0 @1) (pointer_plus ADDR_EXPR@2 @3))
2638 (with { poly_int64 diff; tree inner_type = TREE_TYPE (@1);}
2639 (if (ptr_difference_const (@0, @2, &diff))
2640 (neeq (plus { build_int_cst_type (inner_type, diff); } @1) @3)))))
2641
2642 /* X - Y < X is the same as Y > 0 when there is no overflow.
2643 For equality, this is also true with wrapping overflow. */
2644 (for op (simple_comparison)
2645 (simplify
2646 (op:c @0 (minus@2 @0 @1))
2647 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2648 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2649 || ((op == EQ_EXPR || op == NE_EXPR)
2650 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
2651 && (CONSTANT_CLASS_P (@1) || single_use (@2)))
2652 (op @1 { build_zero_cst (TREE_TYPE (@1)); }))))
2653
2654 /* Transform:
2655 (X / Y) == 0 -> X < Y if X, Y are unsigned.
2656 (X / Y) != 0 -> X >= Y, if X, Y are unsigned. */
2657 (for cmp (eq ne)
2658 ocmp (lt ge)
2659 (simplify
2660 (cmp (trunc_div @0 @1) integer_zerop)
2661 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
2662 /* Complex ==/!= is allowed, but not </>=. */
2663 && TREE_CODE (TREE_TYPE (@0)) != COMPLEX_TYPE
2664 && (VECTOR_TYPE_P (type) || !VECTOR_TYPE_P (TREE_TYPE (@0))))
2665 (ocmp @0 @1))))
2666
2667 /* X == C - X can never be true if C is odd. */
2668 (for cmp (eq ne)
2669 (simplify
2670 (cmp:c (convert? @0) (convert1? (minus INTEGER_CST@1 (convert2? @0))))
2671 (if (TREE_INT_CST_LOW (@1) & 1)
2672 { constant_boolean_node (cmp == NE_EXPR, type); })))
2673
2674 /* Arguments on which one can call get_nonzero_bits to get the bits
2675 possibly set. */
2676 (match with_possible_nonzero_bits
2677 INTEGER_CST@0)
2678 (match with_possible_nonzero_bits
2679 SSA_NAME@0
2680 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))))
2681 /* Slightly extended version, do not make it recursive to keep it cheap. */
2682 (match (with_possible_nonzero_bits2 @0)
2683 with_possible_nonzero_bits@0)
2684 (match (with_possible_nonzero_bits2 @0)
2685 (bit_and:c with_possible_nonzero_bits@0 @2))
2686
2687 /* Same for bits that are known to be set, but we do not have
2688 an equivalent to get_nonzero_bits yet. */
2689 (match (with_certain_nonzero_bits2 @0)
2690 INTEGER_CST@0)
2691 (match (with_certain_nonzero_bits2 @0)
2692 (bit_ior @1 INTEGER_CST@0))
2693
2694 /* X == C (or X & Z == Y | C) is impossible if ~nonzero(X) & C != 0. */
2695 (for cmp (eq ne)
2696 (simplify
2697 (cmp:c (with_possible_nonzero_bits2 @0) (with_certain_nonzero_bits2 @1))
2698 (if (wi::bit_and_not (wi::to_wide (@1), get_nonzero_bits (@0)) != 0)
2699 { constant_boolean_node (cmp == NE_EXPR, type); })))
2700
2701 /* ((X inner_op C0) outer_op C1)
2702 With X being a tree where value_range has reasoned certain bits to always be
2703 zero throughout its computed value range,
2704 inner_op = {|,^}, outer_op = {|,^} and inner_op != outer_op
2705 where zero_mask has 1's for all bits that are sure to be 0 in
2706 and 0's otherwise.
2707 if (inner_op == '^') C0 &= ~C1;
2708 if ((C0 & ~zero_mask) == 0) then emit (X outer_op (C0 outer_op C1)
2709 if ((C1 & ~zero_mask) == 0) then emit (X inner_op (C0 outer_op C1)
2710 */
2711 (for inner_op (bit_ior bit_xor)
2712 outer_op (bit_xor bit_ior)
2713 (simplify
2714 (outer_op
2715 (inner_op:s @2 INTEGER_CST@0) INTEGER_CST@1)
2716 (with
2717 {
2718 bool fail = false;
2719 wide_int zero_mask_not;
2720 wide_int C0;
2721 wide_int cst_emit;
2722
2723 if (TREE_CODE (@2) == SSA_NAME)
2724 zero_mask_not = get_nonzero_bits (@2);
2725 else
2726 fail = true;
2727
2728 if (inner_op == BIT_XOR_EXPR)
2729 {
2730 C0 = wi::bit_and_not (wi::to_wide (@0), wi::to_wide (@1));
2731 cst_emit = C0 | wi::to_wide (@1);
2732 }
2733 else
2734 {
2735 C0 = wi::to_wide (@0);
2736 cst_emit = C0 ^ wi::to_wide (@1);
2737 }
2738 }
2739 (if (!fail && (C0 & zero_mask_not) == 0)
2740 (outer_op @2 { wide_int_to_tree (type, cst_emit); })
2741 (if (!fail && (wi::to_wide (@1) & zero_mask_not) == 0)
2742 (inner_op @2 { wide_int_to_tree (type, cst_emit); }))))))
2743
2744 /* Associate (p +p off1) +p off2 as (p +p (off1 + off2)). */
2745 (simplify
2746 (pointer_plus (pointer_plus:s @0 @1) @3)
2747 (pointer_plus @0 (plus @1 @3)))
2748 #if GENERIC
2749 (simplify
2750 (pointer_plus (convert:s (pointer_plus:s @0 @1)) @3)
2751 (convert:type (pointer_plus @0 (plus @1 @3))))
2752 #endif
2753
2754 /* Pattern match
2755 tem1 = (long) ptr1;
2756 tem2 = (long) ptr2;
2757 tem3 = tem2 - tem1;
2758 tem4 = (unsigned long) tem3;
2759 tem5 = ptr1 + tem4;
2760 and produce
2761 tem5 = ptr2; */
2762 (simplify
2763 (pointer_plus @0 (convert?@2 (minus@3 (convert @1) (convert @0))))
2764 /* Conditionally look through a sign-changing conversion. */
2765 (if (TYPE_PRECISION (TREE_TYPE (@2)) == TYPE_PRECISION (TREE_TYPE (@3))
2766 && ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@1)))
2767 || (GENERIC && type == TREE_TYPE (@1))))
2768 @1))
2769 (simplify
2770 (pointer_plus @0 (convert?@2 (pointer_diff@3 @1 @@0)))
2771 (if (TYPE_PRECISION (TREE_TYPE (@2)) >= TYPE_PRECISION (TREE_TYPE (@3)))
2772 (convert @1)))
2773
2774 /* Pattern match
2775 tem = (sizetype) ptr;
2776 tem = tem & algn;
2777 tem = -tem;
2778 ... = ptr p+ tem;
2779 and produce the simpler and easier to analyze with respect to alignment
2780 ... = ptr & ~algn; */
2781 (simplify
2782 (pointer_plus @0 (negate (bit_and (convert @0) INTEGER_CST@1)))
2783 (with { tree algn = wide_int_to_tree (TREE_TYPE (@0), ~wi::to_wide (@1)); }
2784 (bit_and @0 { algn; })))
2785
2786 /* Try folding difference of addresses. */
2787 (simplify
2788 (minus (convert ADDR_EXPR@0) (convert (pointer_plus @1 @2)))
2789 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2790 (with { poly_int64 diff; }
2791 (if (ptr_difference_const (@0, @1, &diff))
2792 (minus { build_int_cst_type (type, diff); } (convert @2))))))
2793 (simplify
2794 (minus (convert (pointer_plus @0 @2)) (convert ADDR_EXPR@1))
2795 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2796 (with { poly_int64 diff; }
2797 (if (ptr_difference_const (@0, @1, &diff))
2798 (plus (convert @2) { build_int_cst_type (type, diff); })))))
2799 (simplify
2800 (minus (convert ADDR_EXPR@0) (convert @1))
2801 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2802 (with { poly_int64 diff; }
2803 (if (ptr_difference_const (@0, @1, &diff))
2804 { build_int_cst_type (type, diff); }))))
2805 (simplify
2806 (minus (convert @0) (convert ADDR_EXPR@1))
2807 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2808 (with { poly_int64 diff; }
2809 (if (ptr_difference_const (@0, @1, &diff))
2810 { build_int_cst_type (type, diff); }))))
2811 (simplify
2812 (pointer_diff (convert?@2 ADDR_EXPR@0) (convert1?@3 @1))
2813 (if (tree_nop_conversion_p (TREE_TYPE(@2), TREE_TYPE (@0))
2814 && tree_nop_conversion_p (TREE_TYPE(@3), TREE_TYPE (@1)))
2815 (with { poly_int64 diff; }
2816 (if (ptr_difference_const (@0, @1, &diff))
2817 { build_int_cst_type (type, diff); }))))
2818 (simplify
2819 (pointer_diff (convert?@2 @0) (convert1?@3 ADDR_EXPR@1))
2820 (if (tree_nop_conversion_p (TREE_TYPE(@2), TREE_TYPE (@0))
2821 && tree_nop_conversion_p (TREE_TYPE(@3), TREE_TYPE (@1)))
2822 (with { poly_int64 diff; }
2823 (if (ptr_difference_const (@0, @1, &diff))
2824 { build_int_cst_type (type, diff); }))))
2825
2826 /* (&a+b) - (&a[1] + c) -> sizeof(a[0]) + (b - c) */
2827 (simplify
2828 (pointer_diff (pointer_plus ADDR_EXPR@0 @1) (pointer_plus ADDR_EXPR@2 @3))
2829 (with { poly_int64 diff; }
2830 (if (ptr_difference_const (@0, @2, &diff))
2831 (plus { build_int_cst_type (type, diff); } (convert (minus @1 @3))))))
2832 /* (p + b) - &p->d -> offsetof (*p, d) + b */
2833 (simplify
2834 (pointer_diff (pointer_plus @0 @1) ADDR_EXPR@2)
2835 (with { poly_int64 diff; }
2836 (if (ptr_difference_const (@0, @2, &diff))
2837 (plus { build_int_cst_type (type, diff); } (convert @1)))))
2838 (simplify
2839 (pointer_diff ADDR_EXPR@0 (pointer_plus @1 @2))
2840 (with { poly_int64 diff; }
2841 (if (ptr_difference_const (@0, @1, &diff))
2842 (minus { build_int_cst_type (type, diff); } (convert @2)))))
2843
2844 /* Canonicalize (T *)(ptr - ptr-cst) to &MEM[ptr + -ptr-cst]. */
2845 (simplify
2846 (convert (pointer_diff @0 INTEGER_CST@1))
2847 (if (POINTER_TYPE_P (type))
2848 { build_fold_addr_expr_with_type
2849 (build2 (MEM_REF, char_type_node, @0,
2850 wide_int_to_tree (ptr_type_node, wi::neg (wi::to_wide (@1)))),
2851 type); }))
2852
2853 /* If arg0 is derived from the address of an object or function, we may
2854 be able to fold this expression using the object or function's
2855 alignment. */
2856 (simplify
2857 (bit_and (convert? @0) INTEGER_CST@1)
2858 (if (POINTER_TYPE_P (TREE_TYPE (@0))
2859 && tree_nop_conversion_p (type, TREE_TYPE (@0)))
2860 (with
2861 {
2862 unsigned int align;
2863 unsigned HOST_WIDE_INT bitpos;
2864 get_pointer_alignment_1 (@0, &align, &bitpos);
2865 }
2866 (if (wi::ltu_p (wi::to_wide (@1), align / BITS_PER_UNIT))
2867 { wide_int_to_tree (type, (wi::to_wide (@1)
2868 & (bitpos / BITS_PER_UNIT))); }))))
2869
2870 (match min_value
2871 uniform_integer_cst_p
2872 (with {
2873 tree int_cst = uniform_integer_cst_p (t);
2874 tree inner_type = TREE_TYPE (int_cst);
2875 }
2876 (if ((INTEGRAL_TYPE_P (inner_type)
2877 || POINTER_TYPE_P (inner_type))
2878 && wi::eq_p (wi::to_wide (int_cst), wi::min_value (inner_type))))))
2879
2880 (match max_value
2881 uniform_integer_cst_p
2882 (with {
2883 tree int_cst = uniform_integer_cst_p (t);
2884 tree itype = TREE_TYPE (int_cst);
2885 }
2886 (if ((INTEGRAL_TYPE_P (itype)
2887 || POINTER_TYPE_P (itype))
2888 && wi::eq_p (wi::to_wide (int_cst), wi::max_value (itype))))))
2889
2890 /* x > y && x != XXX_MIN --> x > y
2891 x > y && x == XXX_MIN --> false . */
2892 (for eqne (eq ne)
2893 (simplify
2894 (bit_and:c (gt:c@2 @0 @1) (eqne @0 min_value))
2895 (switch
2896 (if (eqne == EQ_EXPR)
2897 { constant_boolean_node (false, type); })
2898 (if (eqne == NE_EXPR)
2899 @2)
2900 )))
2901
2902 /* x < y && x != XXX_MAX --> x < y
2903 x < y && x == XXX_MAX --> false. */
2904 (for eqne (eq ne)
2905 (simplify
2906 (bit_and:c (lt:c@2 @0 @1) (eqne @0 max_value))
2907 (switch
2908 (if (eqne == EQ_EXPR)
2909 { constant_boolean_node (false, type); })
2910 (if (eqne == NE_EXPR)
2911 @2)
2912 )))
2913
2914 /* x <= y && x == XXX_MIN --> x == XXX_MIN. */
2915 (simplify
2916 (bit_and:c (le:c @0 @1) (eq@2 @0 min_value))
2917 @2)
2918
2919 /* x >= y && x == XXX_MAX --> x == XXX_MAX. */
2920 (simplify
2921 (bit_and:c (ge:c @0 @1) (eq@2 @0 max_value))
2922 @2)
2923
2924 /* x > y || x != XXX_MIN --> x != XXX_MIN. */
2925 (simplify
2926 (bit_ior:c (gt:c @0 @1) (ne@2 @0 min_value))
2927 @2)
2928
2929 /* x <= y || x != XXX_MIN --> true. */
2930 (simplify
2931 (bit_ior:c (le:c @0 @1) (ne @0 min_value))
2932 { constant_boolean_node (true, type); })
2933
2934 /* x <= y || x == XXX_MIN --> x <= y. */
2935 (simplify
2936 (bit_ior:c (le:c@2 @0 @1) (eq @0 min_value))
2937 @2)
2938
2939 /* x < y || x != XXX_MAX --> x != XXX_MAX. */
2940 (simplify
2941 (bit_ior:c (lt:c @0 @1) (ne@2 @0 max_value))
2942 @2)
2943
2944 /* x >= y || x != XXX_MAX --> true
2945 x >= y || x == XXX_MAX --> x >= y. */
2946 (for eqne (eq ne)
2947 (simplify
2948 (bit_ior:c (ge:c@2 @0 @1) (eqne @0 max_value))
2949 (switch
2950 (if (eqne == EQ_EXPR)
2951 @2)
2952 (if (eqne == NE_EXPR)
2953 { constant_boolean_node (true, type); }))))
2954
2955 /* y == XXX_MIN || x < y --> x <= y - 1 */
2956 (simplify
2957 (bit_ior:c (eq:s @1 min_value) (lt:cs @0 @1))
2958 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2959 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
2960 (le @0 (minus @1 { build_int_cst (TREE_TYPE (@1), 1); }))))
2961
2962 /* y != XXX_MIN && x >= y --> x > y - 1 */
2963 (simplify
2964 (bit_and:c (ne:s @1 min_value) (ge:cs @0 @1))
2965 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2966 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
2967 (gt @0 (minus @1 { build_int_cst (TREE_TYPE (@1), 1); }))))
2968
2969 /* Convert (X == CST1) && ((other)X OP2 CST2) to a known value
2970 based on CST1 OP2 CST2. Similarly for (X != CST1). */
2971 /* Convert (X == Y) && (X OP2 Y) to a known value if X is an integral type.
2972 Similarly for (X != Y). */
2973
2974 (for code1 (eq ne)
2975 (for code2 (eq ne lt gt le ge)
2976 (simplify
2977 (bit_and:c (code1:c@3 @0 @1) (code2:c@4 (convert?@c0 @0) @2))
2978 (if ((TREE_CODE (@1) == INTEGER_CST
2979 && TREE_CODE (@2) == INTEGER_CST)
2980 || ((INTEGRAL_TYPE_P (TREE_TYPE (@1))
2981 || POINTER_TYPE_P (TREE_TYPE (@1)))
2982 && bitwise_equal_p (@1, @2)))
2983 (with
2984 {
2985 bool one_before = false;
2986 bool one_after = false;
2987 int cmp = 0;
2988 bool allbits = true;
2989 if (TREE_CODE (@1) == INTEGER_CST
2990 && TREE_CODE (@2) == INTEGER_CST)
2991 {
2992 allbits = TYPE_PRECISION (TREE_TYPE (@1)) <= TYPE_PRECISION (TREE_TYPE (@2));
2993 auto t1 = wi::to_wide (fold_convert (TREE_TYPE (@2), @1));
2994 auto t2 = wi::to_wide (@2);
2995 cmp = wi::cmp (t1, t2, TYPE_SIGN (TREE_TYPE (@2)));
2996 if (cmp < 0
2997 && t1 == t2 - 1)
2998 one_before = true;
2999 if (cmp > 0
3000 && t1 == t2 + 1)
3001 one_after = true;
3002 }
3003 bool val;
3004 switch (code2)
3005 {
3006 case EQ_EXPR: val = (cmp == 0); break;
3007 case NE_EXPR: val = (cmp != 0); break;
3008 case LT_EXPR: val = (cmp < 0); break;
3009 case GT_EXPR: val = (cmp > 0); break;
3010 case LE_EXPR: val = (cmp <= 0); break;
3011 case GE_EXPR: val = (cmp >= 0); break;
3012 default: gcc_unreachable ();
3013 }
3014 }
3015 (switch
3016 (if (code1 == EQ_EXPR && val) @3)
3017 (if (code1 == EQ_EXPR && !val) { constant_boolean_node (false, type); })
3018 (if (code1 == NE_EXPR && !val && allbits) @4)
3019 (if (code1 == NE_EXPR
3020 && code2 == GE_EXPR
3021 && cmp == 0
3022 && allbits)
3023 (gt @c0 (convert @1)))
3024 (if (code1 == NE_EXPR
3025 && code2 == LE_EXPR
3026 && cmp == 0
3027 && allbits)
3028 (lt @c0 (convert @1)))
3029 /* (a != (b+1)) & (a > b) -> a > (b+1) */
3030 (if (code1 == NE_EXPR
3031 && code2 == GT_EXPR
3032 && one_after
3033 && allbits)
3034 (gt @c0 (convert @1)))
3035 /* (a != (b-1)) & (a < b) -> a < (b-1) */
3036 (if (code1 == NE_EXPR
3037 && code2 == LT_EXPR
3038 && one_before
3039 && allbits)
3040 (lt @c0 (convert @1)))
3041 )
3042 )
3043 )
3044 )
3045 )
3046 )
3047
3048 /* Convert (X OP1 CST1) && (X OP2 CST2).
3049 Convert (X OP1 Y) && (X OP2 Y). */
3050
3051 (for code1 (lt le gt ge)
3052 (for code2 (lt le gt ge)
3053 (simplify
3054 (bit_and (code1:c@3 @0 @1) (code2:c@4 @0 @2))
3055 (if ((TREE_CODE (@1) == INTEGER_CST
3056 && TREE_CODE (@2) == INTEGER_CST)
3057 || ((INTEGRAL_TYPE_P (TREE_TYPE (@1))
3058 || POINTER_TYPE_P (TREE_TYPE (@1)))
3059 && operand_equal_p (@1, @2)))
3060 (with
3061 {
3062 int cmp = 0;
3063 if (TREE_CODE (@1) == INTEGER_CST
3064 && TREE_CODE (@2) == INTEGER_CST)
3065 cmp = tree_int_cst_compare (@1, @2);
3066 }
3067 (switch
3068 /* Choose the more restrictive of two < or <= comparisons. */
3069 (if ((code1 == LT_EXPR || code1 == LE_EXPR)
3070 && (code2 == LT_EXPR || code2 == LE_EXPR))
3071 (if ((cmp < 0) || (cmp == 0 && code1 == LT_EXPR))
3072 @3
3073 @4))
3074 /* Likewise chose the more restrictive of two > or >= comparisons. */
3075 (if ((code1 == GT_EXPR || code1 == GE_EXPR)
3076 && (code2 == GT_EXPR || code2 == GE_EXPR))
3077 (if ((cmp > 0) || (cmp == 0 && code1 == GT_EXPR))
3078 @3
3079 @4))
3080 /* Check for singleton ranges. */
3081 (if (cmp == 0
3082 && ((code1 == LE_EXPR && code2 == GE_EXPR)
3083 || (code1 == GE_EXPR && code2 == LE_EXPR)))
3084 (eq @0 @1))
3085 /* Check for disjoint ranges. */
3086 (if (cmp <= 0
3087 && (code1 == LT_EXPR || code1 == LE_EXPR)
3088 && (code2 == GT_EXPR || code2 == GE_EXPR))
3089 { constant_boolean_node (false, type); })
3090 (if (cmp >= 0
3091 && (code1 == GT_EXPR || code1 == GE_EXPR)
3092 && (code2 == LT_EXPR || code2 == LE_EXPR))
3093 { constant_boolean_node (false, type); })
3094 ))))))
3095
3096 /* Convert (X == CST1) || (X OP2 CST2) to a known value
3097 based on CST1 OP2 CST2. Similarly for (X != CST1). */
3098 /* Convert (X == Y) || (X OP2 Y) to a known value if X is an integral type.
3099 Similarly for (X != Y). */
3100
3101 (for code1 (eq ne)
3102 (for code2 (eq ne lt gt le ge)
3103 (simplify
3104 (bit_ior:c (code1:c@3 @0 @1) (code2:c@4 (convert?@c0 @0) @2))
3105 (if ((TREE_CODE (@1) == INTEGER_CST
3106 && TREE_CODE (@2) == INTEGER_CST)
3107 || ((INTEGRAL_TYPE_P (TREE_TYPE (@1))
3108 || POINTER_TYPE_P (TREE_TYPE (@1)))
3109 && bitwise_equal_p (@1, @2)))
3110 (with
3111 {
3112 bool one_before = false;
3113 bool one_after = false;
3114 int cmp = 0;
3115 bool allbits = true;
3116 if (TREE_CODE (@1) == INTEGER_CST
3117 && TREE_CODE (@2) == INTEGER_CST)
3118 {
3119 allbits = TYPE_PRECISION (TREE_TYPE (@1)) <= TYPE_PRECISION (TREE_TYPE (@2));
3120 auto t1 = wi::to_wide (fold_convert (TREE_TYPE (@2), @1));
3121 auto t2 = wi::to_wide (@2);
3122 cmp = wi::cmp (t1, t2, TYPE_SIGN (TREE_TYPE (@2)));
3123 if (cmp < 0
3124 && t1 == t2 - 1)
3125 one_before = true;
3126 if (cmp > 0
3127 && t1 == t2 + 1)
3128 one_after = true;
3129 }
3130 bool val;
3131 switch (code2)
3132 {
3133 case EQ_EXPR: val = (cmp == 0); break;
3134 case NE_EXPR: val = (cmp != 0); break;
3135 case LT_EXPR: val = (cmp < 0); break;
3136 case GT_EXPR: val = (cmp > 0); break;
3137 case LE_EXPR: val = (cmp <= 0); break;
3138 case GE_EXPR: val = (cmp >= 0); break;
3139 default: gcc_unreachable ();
3140 }
3141 }
3142 (switch
3143 (if (code1 == EQ_EXPR && val) @4)
3144 (if (code1 == NE_EXPR && val && allbits) { constant_boolean_node (true, type); })
3145 (if (code1 == NE_EXPR && !val && allbits) @3)
3146 (if (code1 == EQ_EXPR
3147 && code2 == GT_EXPR
3148 && cmp == 0
3149 && allbits)
3150 (ge @c0 @2))
3151 (if (code1 == EQ_EXPR
3152 && code2 == LT_EXPR
3153 && cmp == 0
3154 && allbits)
3155 (le @c0 @2))
3156 /* (a == (b-1)) | (a >= b) -> a >= (b-1) */
3157 (if (code1 == EQ_EXPR
3158 && code2 == GE_EXPR
3159 && one_before
3160 && allbits)
3161 (ge @c0 (convert @1)))
3162 /* (a == (b+1)) | (a <= b) -> a <= (b-1) */
3163 (if (code1 == EQ_EXPR
3164 && code2 == LE_EXPR
3165 && one_after
3166 && allbits)
3167 (le @c0 (convert @1)))
3168 )
3169 )
3170 )
3171 )
3172 )
3173 )
3174
3175 /* Convert (X OP1 CST1) || (X OP2 CST2).
3176 Convert (X OP1 Y) || (X OP2 Y). */
3177
3178 (for code1 (lt le gt ge)
3179 (for code2 (lt le gt ge)
3180 (simplify
3181 (bit_ior (code1@3 @0 @1) (code2@4 @0 @2))
3182 (if ((TREE_CODE (@1) == INTEGER_CST
3183 && TREE_CODE (@2) == INTEGER_CST)
3184 || ((INTEGRAL_TYPE_P (TREE_TYPE (@1))
3185 || POINTER_TYPE_P (TREE_TYPE (@1)))
3186 && operand_equal_p (@1, @2)))
3187 (with
3188 {
3189 int cmp = 0;
3190 if (TREE_CODE (@1) == INTEGER_CST
3191 && TREE_CODE (@2) == INTEGER_CST)
3192 cmp = tree_int_cst_compare (@1, @2);
3193 }
3194 (switch
3195 /* Choose the more restrictive of two < or <= comparisons. */
3196 (if ((code1 == LT_EXPR || code1 == LE_EXPR)
3197 && (code2 == LT_EXPR || code2 == LE_EXPR))
3198 (if ((cmp < 0) || (cmp == 0 && code1 == LT_EXPR))
3199 @4
3200 @3))
3201 /* Likewise chose the more restrictive of two > or >= comparisons. */
3202 (if ((code1 == GT_EXPR || code1 == GE_EXPR)
3203 && (code2 == GT_EXPR || code2 == GE_EXPR))
3204 (if ((cmp > 0) || (cmp == 0 && code1 == GT_EXPR))
3205 @4
3206 @3))
3207 /* Check for singleton ranges. */
3208 (if (cmp == 0
3209 && ((code1 == LT_EXPR && code2 == GT_EXPR)
3210 || (code1 == GT_EXPR && code2 == LT_EXPR)))
3211 (ne @0 @2))
3212 /* Check for disjoint ranges. */
3213 (if (cmp >= 0
3214 && (code1 == LT_EXPR || code1 == LE_EXPR)
3215 && (code2 == GT_EXPR || code2 == GE_EXPR))
3216 { constant_boolean_node (true, type); })
3217 (if (cmp <= 0
3218 && (code1 == GT_EXPR || code1 == GE_EXPR)
3219 && (code2 == LT_EXPR || code2 == LE_EXPR))
3220 { constant_boolean_node (true, type); })
3221 ))))))
3222
3223 /* Optimize (a CMP b) ^ (a CMP b) */
3224 /* Optimize (a CMP b) != (a CMP b) */
3225 (for op (bit_xor ne)
3226 (for cmp1 (lt lt lt le le le)
3227 cmp2 (gt eq ne ge eq ne)
3228 rcmp (ne le gt ne lt ge)
3229 (simplify
3230 (op:c (cmp1:c @0 @1) (cmp2:c @0 @1))
3231 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
3232 (rcmp @0 @1)))))
3233
3234 /* Optimize (a CMP b) == (a CMP b) */
3235 (for cmp1 (lt lt lt le le le)
3236 cmp2 (gt eq ne ge eq ne)
3237 rcmp (eq gt le eq ge lt)
3238 (simplify
3239 (eq:c (cmp1:c @0 @1) (cmp2:c @0 @1))
3240 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
3241 (rcmp @0 @1))))
3242
3243 /* We can't reassociate at all for saturating types. */
3244 (if (!TYPE_SATURATING (type))
3245
3246 /* Contract negates. */
3247 /* A + (-B) -> A - B */
3248 (simplify
3249 (plus:c @0 (convert? (negate @1)))
3250 /* Apply STRIP_NOPS on the negate. */
3251 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
3252 && !TYPE_OVERFLOW_SANITIZED (type))
3253 (with
3254 {
3255 tree t1 = type;
3256 if (INTEGRAL_TYPE_P (type)
3257 && TYPE_OVERFLOW_WRAPS (type) != TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
3258 t1 = TYPE_OVERFLOW_WRAPS (type) ? type : TREE_TYPE (@1);
3259 }
3260 (convert (minus (convert:t1 @0) (convert:t1 @1))))))
3261 /* A - (-B) -> A + B */
3262 (simplify
3263 (minus @0 (convert? (negate @1)))
3264 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
3265 && !TYPE_OVERFLOW_SANITIZED (type))
3266 (with
3267 {
3268 tree t1 = type;
3269 if (INTEGRAL_TYPE_P (type)
3270 && TYPE_OVERFLOW_WRAPS (type) != TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
3271 t1 = TYPE_OVERFLOW_WRAPS (type) ? type : TREE_TYPE (@1);
3272 }
3273 (convert (plus (convert:t1 @0) (convert:t1 @1))))))
3274 /* -(T)(-A) -> (T)A
3275 Sign-extension is ok except for INT_MIN, which thankfully cannot
3276 happen without overflow. */
3277 (simplify
3278 (negate (convert (negate @1)))
3279 (if (INTEGRAL_TYPE_P (type)
3280 && (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@1))
3281 || (!TYPE_UNSIGNED (TREE_TYPE (@1))
3282 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))))
3283 && !TYPE_OVERFLOW_SANITIZED (type)
3284 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@1)))
3285 (convert @1)))
3286 (simplify
3287 (negate (convert negate_expr_p@1))
3288 (if (SCALAR_FLOAT_TYPE_P (type)
3289 && ((DECIMAL_FLOAT_TYPE_P (type)
3290 == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1))
3291 && TYPE_PRECISION (type) >= TYPE_PRECISION (TREE_TYPE (@1)))
3292 || !HONOR_SIGN_DEPENDENT_ROUNDING (type)))
3293 (convert (negate @1))))
3294 (simplify
3295 (negate (nop_convert? (negate @1)))
3296 (if (!TYPE_OVERFLOW_SANITIZED (type)
3297 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@1)))
3298 (view_convert @1)))
3299
3300 /* We can't reassociate floating-point unless -fassociative-math
3301 or fixed-point plus or minus because of saturation to +-Inf. */
3302 (if ((!FLOAT_TYPE_P (type) || flag_associative_math)
3303 && !FIXED_POINT_TYPE_P (type))
3304
3305 /* Match patterns that allow contracting a plus-minus pair
3306 irrespective of overflow issues. */
3307 /* (A +- B) - A -> +- B */
3308 /* (A +- B) -+ B -> A */
3309 /* A - (A +- B) -> -+ B */
3310 /* A +- (B -+ A) -> +- B */
3311 (simplify
3312 (minus (nop_convert1? (plus:c (nop_convert2? @0) @1)) @0)
3313 (view_convert @1))
3314 (simplify
3315 (minus (nop_convert1? (minus (nop_convert2? @0) @1)) @0)
3316 (if (!ANY_INTEGRAL_TYPE_P (type)
3317 || TYPE_OVERFLOW_WRAPS (type))
3318 (negate (view_convert @1))
3319 (view_convert (negate @1))))
3320 (simplify
3321 (plus:c (nop_convert1? (minus @0 (nop_convert2? @1))) @1)
3322 (view_convert @0))
3323 (simplify
3324 (minus @0 (nop_convert1? (plus:c (nop_convert2? @0) @1)))
3325 (if (!ANY_INTEGRAL_TYPE_P (type)
3326 || TYPE_OVERFLOW_WRAPS (type))
3327 (negate (view_convert @1))
3328 (view_convert (negate @1))))
3329 (simplify
3330 (minus @0 (nop_convert1? (minus (nop_convert2? @0) @1)))
3331 (view_convert @1))
3332 /* (A +- B) + (C - A) -> C +- B */
3333 /* (A + B) - (A - C) -> B + C */
3334 /* More cases are handled with comparisons. */
3335 (simplify
3336 (plus:c (plus:c @0 @1) (minus @2 @0))
3337 (plus @2 @1))
3338 (simplify
3339 (plus:c (minus @0 @1) (minus @2 @0))
3340 (minus @2 @1))
3341 (simplify
3342 (plus:c (pointer_diff @0 @1) (pointer_diff @2 @0))
3343 (if (TYPE_OVERFLOW_UNDEFINED (type)
3344 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0)))
3345 (pointer_diff @2 @1)))
3346 (simplify
3347 (minus (plus:c @0 @1) (minus @0 @2))
3348 (plus @1 @2))
3349
3350 /* (A +- CST1) +- CST2 -> A + CST3
3351 Use view_convert because it is safe for vectors and equivalent for
3352 scalars. */
3353 (for outer_op (plus minus)
3354 (for inner_op (plus minus)
3355 neg_inner_op (minus plus)
3356 (simplify
3357 (outer_op (nop_convert? (inner_op @0 CONSTANT_CLASS_P@1))
3358 CONSTANT_CLASS_P@2)
3359 /* If one of the types wraps, use that one. */
3360 (if (!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_WRAPS (type))
3361 /* If all 3 captures are CONSTANT_CLASS_P, punt, as we might recurse
3362 forever if something doesn't simplify into a constant. */
3363 (if (!CONSTANT_CLASS_P (@0))
3364 (if (outer_op == PLUS_EXPR)
3365 (plus (view_convert @0) (inner_op! @2 (view_convert @1)))
3366 (minus (view_convert @0) (neg_inner_op! @2 (view_convert @1)))))
3367 (if (!ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3368 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
3369 (if (outer_op == PLUS_EXPR)
3370 (view_convert (plus @0 (inner_op! (view_convert @2) @1)))
3371 (view_convert (minus @0 (neg_inner_op! (view_convert @2) @1))))
3372 /* If the constant operation overflows we cannot do the transform
3373 directly as we would introduce undefined overflow, for example
3374 with (a - 1) + INT_MIN. */
3375 (if (types_match (type, @0) && !TYPE_OVERFLOW_SANITIZED (type))
3376 (with { tree cst = const_binop (outer_op == inner_op
3377 ? PLUS_EXPR : MINUS_EXPR,
3378 type, @1, @2); }
3379 (if (cst)
3380 (if (INTEGRAL_TYPE_P (type) && !TREE_OVERFLOW (cst))
3381 (inner_op @0 { cst; } )
3382 /* X+INT_MAX+1 is X-INT_MIN. */
3383 (if (INTEGRAL_TYPE_P (type)
3384 && wi::to_wide (cst) == wi::min_value (type))
3385 (neg_inner_op @0 { wide_int_to_tree (type, wi::to_wide (cst)); })
3386 /* Last resort, use some unsigned type. */
3387 (with { tree utype = unsigned_type_for (type); }
3388 (if (utype)
3389 (view_convert (inner_op
3390 (view_convert:utype @0)
3391 (view_convert:utype
3392 { TREE_OVERFLOW (cst)
3393 ? drop_tree_overflow (cst) : cst; })))))))))))))))
3394
3395 /* (CST1 - A) +- CST2 -> CST3 - A */
3396 (for outer_op (plus minus)
3397 (simplify
3398 (outer_op (nop_convert? (minus CONSTANT_CLASS_P@1 @0)) CONSTANT_CLASS_P@2)
3399 /* If one of the types wraps, use that one. */
3400 (if (!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_WRAPS (type))
3401 /* If all 3 captures are CONSTANT_CLASS_P, punt, as we might recurse
3402 forever if something doesn't simplify into a constant. */
3403 (if (!CONSTANT_CLASS_P (@0))
3404 (minus (outer_op! (view_convert @1) @2) (view_convert @0)))
3405 (if (!ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3406 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
3407 (view_convert (minus (outer_op! @1 (view_convert @2)) @0))
3408 (if (types_match (type, @0) && !TYPE_OVERFLOW_SANITIZED (type))
3409 (with { tree cst = const_binop (outer_op, type, @1, @2); }
3410 (if (cst && !TREE_OVERFLOW (cst))
3411 (minus { cst; } @0))))))))
3412
3413 /* CST1 - (CST2 - A) -> CST3 + A
3414 Use view_convert because it is safe for vectors and equivalent for
3415 scalars. */
3416 (simplify
3417 (minus CONSTANT_CLASS_P@1 (nop_convert? (minus CONSTANT_CLASS_P@2 @0)))
3418 /* If one of the types wraps, use that one. */
3419 (if (!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_WRAPS (type))
3420 /* If all 3 captures are CONSTANT_CLASS_P, punt, as we might recurse
3421 forever if something doesn't simplify into a constant. */
3422 (if (!CONSTANT_CLASS_P (@0))
3423 (plus (view_convert @0) (minus! @1 (view_convert @2))))
3424 (if (!ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3425 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
3426 (view_convert (plus @0 (minus! (view_convert @1) @2)))
3427 (if (types_match (type, @0) && !TYPE_OVERFLOW_SANITIZED (type))
3428 (with { tree cst = const_binop (MINUS_EXPR, type, @1, @2); }
3429 (if (cst && !TREE_OVERFLOW (cst))
3430 (plus { cst; } @0)))))))
3431
3432 /* ((T)(A)) + CST -> (T)(A + CST) */
3433 #if GIMPLE
3434 (simplify
3435 (plus (convert:s SSA_NAME@0) INTEGER_CST@1)
3436 (if (TREE_CODE (TREE_TYPE (@0)) == INTEGER_TYPE
3437 && TREE_CODE (type) == INTEGER_TYPE
3438 && TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (@0))
3439 && int_fits_type_p (@1, TREE_TYPE (@0)))
3440 /* Perform binary operation inside the cast if the constant fits
3441 and (A + CST)'s range does not overflow. */
3442 (with
3443 {
3444 wi::overflow_type min_ovf = wi::OVF_OVERFLOW,
3445 max_ovf = wi::OVF_OVERFLOW;
3446 tree inner_type = TREE_TYPE (@0);
3447
3448 wide_int w1
3449 = wide_int::from (wi::to_wide (@1), TYPE_PRECISION (inner_type),
3450 TYPE_SIGN (inner_type));
3451
3452 value_range vr;
3453 if (get_global_range_query ()->range_of_expr (vr, @0)
3454 && !vr.varying_p () && !vr.undefined_p ())
3455 {
3456 wide_int wmin0 = vr.lower_bound ();
3457 wide_int wmax0 = vr.upper_bound ();
3458 wi::add (wmin0, w1, TYPE_SIGN (inner_type), &min_ovf);
3459 wi::add (wmax0, w1, TYPE_SIGN (inner_type), &max_ovf);
3460 }
3461 }
3462 (if (min_ovf == wi::OVF_NONE && max_ovf == wi::OVF_NONE)
3463 (convert (plus @0 { wide_int_to_tree (TREE_TYPE (@0), w1); } )))
3464 )))
3465 #endif
3466
3467 /* ((T)(A + CST1)) + CST2 -> (T)(A) + (T)CST1 + CST2 */
3468 #if GIMPLE
3469 (for op (plus minus)
3470 (simplify
3471 (plus (convert:s (op:s @0 INTEGER_CST@1)) INTEGER_CST@2)
3472 (if (TREE_CODE (TREE_TYPE (@0)) == INTEGER_TYPE
3473 && TREE_CODE (type) == INTEGER_TYPE
3474 && TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (@0))
3475 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
3476 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0))
3477 && TYPE_OVERFLOW_WRAPS (type))
3478 (plus (convert @0) (op @2 (convert @1))))))
3479 #endif
3480
3481 /* (T)(A) +- (T)(B) -> (T)(A +- B) only when (A +- B) could be simplified
3482 to a simple value. */
3483 (for op (plus minus)
3484 (simplify
3485 (op (convert @0) (convert @1))
3486 (if (INTEGRAL_TYPE_P (type)
3487 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
3488 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))
3489 && types_match (TREE_TYPE (@0), TREE_TYPE (@1))
3490 && !TYPE_OVERFLOW_TRAPS (type)
3491 && !TYPE_OVERFLOW_SANITIZED (type))
3492 (convert (op! @0 @1)))))
3493
3494 /* ~A + A -> -1 */
3495 (simplify
3496 (plus:c (convert? (bit_not @0)) (convert? @0))
3497 (if (!TYPE_OVERFLOW_TRAPS (type))
3498 (convert { build_all_ones_cst (TREE_TYPE (@0)); })))
3499
3500 /* ~A + 1 -> -A */
3501 (simplify
3502 (plus (convert? (bit_not @0)) integer_each_onep)
3503 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
3504 (negate (convert @0))))
3505
3506 /* -A - 1 -> ~A */
3507 (simplify
3508 (minus (convert? (negate @0)) integer_each_onep)
3509 (if (!TYPE_OVERFLOW_TRAPS (type)
3510 && TREE_CODE (type) != COMPLEX_TYPE
3511 && tree_nop_conversion_p (type, TREE_TYPE (@0)))
3512 (bit_not (convert @0))))
3513
3514 /* -1 - A -> ~A */
3515 (simplify
3516 (minus integer_all_onesp @0)
3517 (if (TREE_CODE (type) != COMPLEX_TYPE)
3518 (bit_not @0)))
3519
3520 /* (T)(P + A) - (T)P -> (T) A */
3521 (simplify
3522 (minus (convert (plus:c @@0 @1))
3523 (convert? @0))
3524 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
3525 /* For integer types, if A has a smaller type
3526 than T the result depends on the possible
3527 overflow in P + A.
3528 E.g. T=size_t, A=(unsigned)429497295, P>0.
3529 However, if an overflow in P + A would cause
3530 undefined behavior, we can assume that there
3531 is no overflow. */
3532 || (INTEGRAL_TYPE_P (TREE_TYPE (@1))
3533 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))))
3534 (convert @1)))
3535 (simplify
3536 (minus (convert (pointer_plus @@0 @1))
3537 (convert @0))
3538 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
3539 /* For pointer types, if the conversion of A to the
3540 final type requires a sign- or zero-extension,
3541 then we have to punt - it is not defined which
3542 one is correct. */
3543 || (POINTER_TYPE_P (TREE_TYPE (@0))
3544 && TREE_CODE (@1) == INTEGER_CST
3545 && tree_int_cst_sign_bit (@1) == 0))
3546 (convert @1)))
3547 (simplify
3548 (pointer_diff (pointer_plus @@0 @1) @0)
3549 /* The second argument of pointer_plus must be interpreted as signed, and
3550 thus sign-extended if necessary. */
3551 (with { tree stype = signed_type_for (TREE_TYPE (@1)); }
3552 /* Use view_convert instead of convert here, as POINTER_PLUS_EXPR
3553 second arg is unsigned even when we need to consider it as signed,
3554 we don't want to diagnose overflow here. */
3555 (convert (view_convert:stype @1))))
3556
3557 /* (T)P - (T)(P + A) -> -(T) A */
3558 (simplify
3559 (minus (convert? @0)
3560 (convert (plus:c @@0 @1)))
3561 (if (INTEGRAL_TYPE_P (type)
3562 && TYPE_OVERFLOW_UNDEFINED (type)
3563 /* For integer literals, using an intermediate unsigned type to avoid
3564 an overflow at run time is counter-productive because it introduces
3565 spurious overflows at compile time, in the form of TREE_OVERFLOW on
3566 the result, which may be problematic in GENERIC for some front-ends:
3567 (T)P - (T)(P + 4) -> (T)(-(U)4) -> (T)(4294967292) -> -4(OVF)
3568 so we use the direct path for them. */
3569 && TREE_CODE (@1) != INTEGER_CST
3570 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
3571 (with { tree utype = unsigned_type_for (type); }
3572 (convert (negate (convert:utype @1))))
3573 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
3574 /* For integer types, if A has a smaller type
3575 than T the result depends on the possible
3576 overflow in P + A.
3577 E.g. T=size_t, A=(unsigned)429497295, P>0.
3578 However, if an overflow in P + A would cause
3579 undefined behavior, we can assume that there
3580 is no overflow. */
3581 || (INTEGRAL_TYPE_P (TREE_TYPE (@1))
3582 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))))
3583 (negate (convert @1)))))
3584 (simplify
3585 (minus (convert @0)
3586 (convert (pointer_plus @@0 @1)))
3587 (if (INTEGRAL_TYPE_P (type)
3588 && TYPE_OVERFLOW_UNDEFINED (type)
3589 /* See above the rationale for this condition. */
3590 && TREE_CODE (@1) != INTEGER_CST
3591 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
3592 (with { tree utype = unsigned_type_for (type); }
3593 (convert (negate (convert:utype @1))))
3594 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
3595 /* For pointer types, if the conversion of A to the
3596 final type requires a sign- or zero-extension,
3597 then we have to punt - it is not defined which
3598 one is correct. */
3599 || (POINTER_TYPE_P (TREE_TYPE (@0))
3600 && TREE_CODE (@1) == INTEGER_CST
3601 && tree_int_cst_sign_bit (@1) == 0))
3602 (negate (convert @1)))))
3603 (simplify
3604 (pointer_diff @0 (pointer_plus @@0 @1))
3605 /* The second argument of pointer_plus must be interpreted as signed, and
3606 thus sign-extended if necessary. */
3607 (with { tree stype = signed_type_for (TREE_TYPE (@1)); }
3608 /* Use view_convert instead of convert here, as POINTER_PLUS_EXPR
3609 second arg is unsigned even when we need to consider it as signed,
3610 we don't want to diagnose overflow here. */
3611 (negate (convert (view_convert:stype @1)))))
3612
3613 /* (T)(P + A) - (T)(P + B) -> (T)A - (T)B */
3614 (simplify
3615 (minus (convert (plus:c @@0 @1))
3616 (convert (plus:c @0 @2)))
3617 (if (INTEGRAL_TYPE_P (type)
3618 && TYPE_OVERFLOW_UNDEFINED (type)
3619 && element_precision (type) <= element_precision (TREE_TYPE (@1))
3620 && element_precision (type) <= element_precision (TREE_TYPE (@2)))
3621 (with { tree utype = unsigned_type_for (type); }
3622 (convert (minus (convert:utype @1) (convert:utype @2))))
3623 (if (((element_precision (type) <= element_precision (TREE_TYPE (@1)))
3624 == (element_precision (type) <= element_precision (TREE_TYPE (@2))))
3625 && (element_precision (type) <= element_precision (TREE_TYPE (@1))
3626 /* For integer types, if A has a smaller type
3627 than T the result depends on the possible
3628 overflow in P + A.
3629 E.g. T=size_t, A=(unsigned)429497295, P>0.
3630 However, if an overflow in P + A would cause
3631 undefined behavior, we can assume that there
3632 is no overflow. */
3633 || (INTEGRAL_TYPE_P (TREE_TYPE (@1))
3634 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
3635 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))
3636 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@2)))))
3637 (minus (convert @1) (convert @2)))))
3638 (simplify
3639 (minus (convert (pointer_plus @@0 @1))
3640 (convert (pointer_plus @0 @2)))
3641 (if (INTEGRAL_TYPE_P (type)
3642 && TYPE_OVERFLOW_UNDEFINED (type)
3643 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
3644 (with { tree utype = unsigned_type_for (type); }
3645 (convert (minus (convert:utype @1) (convert:utype @2))))
3646 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
3647 /* For pointer types, if the conversion of A to the
3648 final type requires a sign- or zero-extension,
3649 then we have to punt - it is not defined which
3650 one is correct. */
3651 || (POINTER_TYPE_P (TREE_TYPE (@0))
3652 && TREE_CODE (@1) == INTEGER_CST
3653 && tree_int_cst_sign_bit (@1) == 0
3654 && TREE_CODE (@2) == INTEGER_CST
3655 && tree_int_cst_sign_bit (@2) == 0))
3656 (minus (convert @1) (convert @2)))))
3657 (simplify
3658 (pointer_diff (pointer_plus @0 @2) (pointer_plus @1 @2))
3659 (pointer_diff @0 @1))
3660 (simplify
3661 (pointer_diff (pointer_plus @@0 @1) (pointer_plus @0 @2))
3662 /* The second argument of pointer_plus must be interpreted as signed, and
3663 thus sign-extended if necessary. */
3664 (with { tree stype = signed_type_for (TREE_TYPE (@1)); }
3665 /* Use view_convert instead of convert here, as POINTER_PLUS_EXPR
3666 second arg is unsigned even when we need to consider it as signed,
3667 we don't want to diagnose overflow here. */
3668 (minus (convert (view_convert:stype @1))
3669 (convert (view_convert:stype @2)))))))
3670
3671 /* (A * C) +- (B * C) -> (A+-B) * C and (A * C) +- A -> A * (C+-1).
3672 Modeled after fold_plusminus_mult_expr. */
3673 (if (!TYPE_SATURATING (type)
3674 && (!FLOAT_TYPE_P (type) || flag_associative_math))
3675 (for plusminus (plus minus)
3676 (simplify
3677 (plusminus (mult:cs@3 @0 @1) (mult:cs@4 @0 @2))
3678 (if (!ANY_INTEGRAL_TYPE_P (type)
3679 || TYPE_OVERFLOW_WRAPS (type)
3680 || (INTEGRAL_TYPE_P (type)
3681 && tree_expr_nonzero_p (@0)
3682 && expr_not_equal_to (@0, wi::minus_one (TYPE_PRECISION (type)))))
3683 (if (single_use (@3) || single_use (@4))
3684 /* If @1 +- @2 is constant require a hard single-use on either
3685 original operand (but not on both). */
3686 (mult (plusminus @1 @2) @0)
3687 (mult! (plusminus @1 @2) @0)
3688 )))
3689 /* We cannot generate constant 1 for fract. */
3690 (if (!ALL_FRACT_MODE_P (TYPE_MODE (type)))
3691 (simplify
3692 (plusminus @0 (mult:c@3 @0 @2))
3693 (if ((!ANY_INTEGRAL_TYPE_P (type)
3694 || TYPE_OVERFLOW_WRAPS (type)
3695 /* For @0 + @0*@2 this transformation would introduce UB
3696 (where there was none before) for @0 in [-1,0] and @2 max.
3697 For @0 - @0*@2 this transformation would introduce UB
3698 for @0 0 and @2 in [min,min+1] or @0 -1 and @2 min+1. */
3699 || (INTEGRAL_TYPE_P (type)
3700 && ((tree_expr_nonzero_p (@0)
3701 && expr_not_equal_to (@0,
3702 wi::minus_one (TYPE_PRECISION (type))))
3703 || (plusminus == PLUS_EXPR
3704 ? expr_not_equal_to (@2,
3705 wi::max_value (TYPE_PRECISION (type), SIGNED))
3706 /* Let's ignore the @0 -1 and @2 min case. */
3707 : (expr_not_equal_to (@2,
3708 wi::min_value (TYPE_PRECISION (type), SIGNED))
3709 && expr_not_equal_to (@2,
3710 wi::min_value (TYPE_PRECISION (type), SIGNED)
3711 + 1))))))
3712 && single_use (@3))
3713 (mult (plusminus { build_one_cst (type); } @2) @0)))
3714 (simplify
3715 (plusminus (mult:c@3 @0 @2) @0)
3716 (if ((!ANY_INTEGRAL_TYPE_P (type)
3717 || TYPE_OVERFLOW_WRAPS (type)
3718 /* For @0*@2 + @0 this transformation would introduce UB
3719 (where there was none before) for @0 in [-1,0] and @2 max.
3720 For @0*@2 - @0 this transformation would introduce UB
3721 for @0 0 and @2 min. */
3722 || (INTEGRAL_TYPE_P (type)
3723 && ((tree_expr_nonzero_p (@0)
3724 && (plusminus == MINUS_EXPR
3725 || expr_not_equal_to (@0,
3726 wi::minus_one (TYPE_PRECISION (type)))))
3727 || expr_not_equal_to (@2,
3728 (plusminus == PLUS_EXPR
3729 ? wi::max_value (TYPE_PRECISION (type), SIGNED)
3730 : wi::min_value (TYPE_PRECISION (type), SIGNED))))))
3731 && single_use (@3))
3732 (mult (plusminus @2 { build_one_cst (type); }) @0))))))
3733
3734 #if GIMPLE
3735 /* Canonicalize X + (X << C) into X * (1 + (1 << C)) and
3736 (X << C1) + (X << C2) into X * ((1 << C1) + (1 << C2)). */
3737 (simplify
3738 (plus:c @0 (lshift:s @0 INTEGER_CST@1))
3739 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3740 && tree_fits_uhwi_p (@1)
3741 && tree_to_uhwi (@1) < element_precision (type)
3742 && (INTEGRAL_TYPE_P (TREE_TYPE (@0))
3743 || optab_handler (smul_optab,
3744 TYPE_MODE (type)) != CODE_FOR_nothing))
3745 (with { tree t = type;
3746 if (!TYPE_OVERFLOW_WRAPS (t)) t = unsigned_type_for (t);
3747 wide_int w = wi::set_bit_in_zero (tree_to_uhwi (@1),
3748 element_precision (type));
3749 w += 1;
3750 tree cst = wide_int_to_tree (VECTOR_TYPE_P (t) ? TREE_TYPE (t)
3751 : t, w);
3752 cst = build_uniform_cst (t, cst); }
3753 (convert (mult (convert:t @0) { cst; })))))
3754 (simplify
3755 (plus (lshift:s @0 INTEGER_CST@1) (lshift:s @0 INTEGER_CST@2))
3756 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3757 && tree_fits_uhwi_p (@1)
3758 && tree_to_uhwi (@1) < element_precision (type)
3759 && tree_fits_uhwi_p (@2)
3760 && tree_to_uhwi (@2) < element_precision (type)
3761 && (INTEGRAL_TYPE_P (TREE_TYPE (@0))
3762 || optab_handler (smul_optab,
3763 TYPE_MODE (type)) != CODE_FOR_nothing))
3764 (with { tree t = type;
3765 if (!TYPE_OVERFLOW_WRAPS (t)) t = unsigned_type_for (t);
3766 unsigned int prec = element_precision (type);
3767 wide_int w = wi::set_bit_in_zero (tree_to_uhwi (@1), prec);
3768 w += wi::set_bit_in_zero (tree_to_uhwi (@2), prec);
3769 tree cst = wide_int_to_tree (VECTOR_TYPE_P (t) ? TREE_TYPE (t)
3770 : t, w);
3771 cst = build_uniform_cst (t, cst); }
3772 (convert (mult (convert:t @0) { cst; })))))
3773 #endif
3774
3775 /* Canonicalize (X*C1)|(X*C2) and (X*C1)^(X*C2) to (C1+C2)*X when
3776 tree_nonzero_bits allows IOR and XOR to be treated like PLUS.
3777 Likewise, handle (X<<C3) and X as legitimate variants of X*C. */
3778 (for op (bit_ior bit_xor)
3779 (simplify
3780 (op (mult:s@0 @1 INTEGER_CST@2)
3781 (mult:s@3 @1 INTEGER_CST@4))
3782 (if (INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_WRAPS (type)
3783 && (tree_nonzero_bits (@0) & tree_nonzero_bits (@3)) == 0)
3784 (mult @1
3785 { wide_int_to_tree (type, wi::to_wide (@2) + wi::to_wide (@4)); })))
3786 (simplify
3787 (op:c (mult:s@0 @1 INTEGER_CST@2)
3788 (lshift:s@3 @1 INTEGER_CST@4))
3789 (if (INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_WRAPS (type)
3790 && tree_int_cst_sgn (@4) > 0
3791 && (tree_nonzero_bits (@0) & tree_nonzero_bits (@3)) == 0)
3792 (with { wide_int wone = wi::one (TYPE_PRECISION (type));
3793 wide_int c = wi::add (wi::to_wide (@2),
3794 wi::lshift (wone, wi::to_wide (@4))); }
3795 (mult @1 { wide_int_to_tree (type, c); }))))
3796 (simplify
3797 (op:c (mult:s@0 @1 INTEGER_CST@2)
3798 @1)
3799 (if (INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_WRAPS (type)
3800 && (tree_nonzero_bits (@0) & tree_nonzero_bits (@1)) == 0)
3801 (mult @1
3802 { wide_int_to_tree (type,
3803 wi::add (wi::to_wide (@2), 1)); })))
3804 (simplify
3805 (op (lshift:s@0 @1 INTEGER_CST@2)
3806 (lshift:s@3 @1 INTEGER_CST@4))
3807 (if (INTEGRAL_TYPE_P (type)
3808 && tree_int_cst_sgn (@2) > 0
3809 && tree_int_cst_sgn (@4) > 0
3810 && (tree_nonzero_bits (@0) & tree_nonzero_bits (@3)) == 0)
3811 (with { tree t = type;
3812 if (!TYPE_OVERFLOW_WRAPS (t))
3813 t = unsigned_type_for (t);
3814 wide_int wone = wi::one (TYPE_PRECISION (t));
3815 wide_int c = wi::add (wi::lshift (wone, wi::to_wide (@2)),
3816 wi::lshift (wone, wi::to_wide (@4))); }
3817 (convert (mult:t (convert:t @1) { wide_int_to_tree (t,c); })))))
3818 (simplify
3819 (op:c (lshift:s@0 @1 INTEGER_CST@2)
3820 @1)
3821 (if (INTEGRAL_TYPE_P (type)
3822 && tree_int_cst_sgn (@2) > 0
3823 && (tree_nonzero_bits (@0) & tree_nonzero_bits (@1)) == 0)
3824 (with { tree t = type;
3825 if (!TYPE_OVERFLOW_WRAPS (t))
3826 t = unsigned_type_for (t);
3827 wide_int wone = wi::one (TYPE_PRECISION (t));
3828 wide_int c = wi::add (wi::lshift (wone, wi::to_wide (@2)), wone); }
3829 (convert (mult:t (convert:t @1) { wide_int_to_tree (t, c); }))))))
3830
3831 /* Simplifications of MIN_EXPR, MAX_EXPR, fmin() and fmax(). */
3832
3833 (for minmax (min max)
3834 (simplify
3835 (minmax @0 @0)
3836 @0)
3837 /* max(max(x,y),x) -> max(x,y) */
3838 (simplify
3839 (minmax:c (minmax:c@2 @0 @1) @0)
3840 @2))
3841 /* For fmin() and fmax(), skip folding when both are sNaN. */
3842 (for minmax (FMIN_ALL FMAX_ALL)
3843 (simplify
3844 (minmax @0 @0)
3845 (if (!tree_expr_maybe_signaling_nan_p (@0))
3846 @0)))
3847 /* min(max(x,y),y) -> y. */
3848 (simplify
3849 (min:c (max:c @0 @1) @1)
3850 @1)
3851 /* max(min(x,y),y) -> y. */
3852 (simplify
3853 (max:c (min:c @0 @1) @1)
3854 @1)
3855 /* max(a,-a) -> abs(a). */
3856 (simplify
3857 (max:c @0 (negate @0))
3858 (if (TREE_CODE (type) != COMPLEX_TYPE
3859 && (! ANY_INTEGRAL_TYPE_P (type)
3860 || TYPE_OVERFLOW_UNDEFINED (type)))
3861 (abs @0)))
3862 /* min(a,-a) -> -abs(a). */
3863 (simplify
3864 (min:c @0 (negate @0))
3865 (if (TREE_CODE (type) != COMPLEX_TYPE
3866 && (! ANY_INTEGRAL_TYPE_P (type)
3867 || TYPE_OVERFLOW_UNDEFINED (type)))
3868 (negate (abs @0))))
3869 (simplify
3870 (min @0 @1)
3871 (switch
3872 (if (INTEGRAL_TYPE_P (type)
3873 && TYPE_MIN_VALUE (type)
3874 && operand_equal_p (@1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
3875 @1)
3876 (if (INTEGRAL_TYPE_P (type)
3877 && TYPE_MAX_VALUE (type)
3878 && operand_equal_p (@1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
3879 @0)))
3880 (simplify
3881 (max @0 @1)
3882 (switch
3883 (if (INTEGRAL_TYPE_P (type)
3884 && TYPE_MAX_VALUE (type)
3885 && operand_equal_p (@1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
3886 @1)
3887 (if (INTEGRAL_TYPE_P (type)
3888 && TYPE_MIN_VALUE (type)
3889 && operand_equal_p (@1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
3890 @0)))
3891
3892 /* max (a, a + CST) -> a + CST where CST is positive. */
3893 /* max (a, a + CST) -> a where CST is negative. */
3894 (simplify
3895 (max:c @0 (plus@2 @0 INTEGER_CST@1))
3896 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
3897 (if (tree_int_cst_sgn (@1) > 0)
3898 @2
3899 @0)))
3900
3901 /* min (a, a + CST) -> a where CST is positive. */
3902 /* min (a, a + CST) -> a + CST where CST is negative. */
3903 (simplify
3904 (min:c @0 (plus@2 @0 INTEGER_CST@1))
3905 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
3906 (if (tree_int_cst_sgn (@1) > 0)
3907 @0
3908 @2)))
3909
3910 /* Simplify min (&var[off0], &var[off1]) etc. depending on whether
3911 the addresses are known to be less, equal or greater. */
3912 (for minmax (min max)
3913 cmp (lt gt)
3914 (simplify
3915 (minmax (convert1?@2 addr@0) (convert2?@3 addr@1))
3916 (with
3917 {
3918 poly_int64 off0, off1;
3919 tree base0, base1;
3920 int equal = address_compare (cmp, TREE_TYPE (@2), @0, @1, base0, base1,
3921 off0, off1, GENERIC);
3922 }
3923 (if (equal == 1)
3924 (if (minmax == MIN_EXPR)
3925 (if (known_le (off0, off1))
3926 @2
3927 (if (known_gt (off0, off1))
3928 @3))
3929 (if (known_ge (off0, off1))
3930 @2
3931 (if (known_lt (off0, off1))
3932 @3)))))))
3933
3934 /* (convert (minmax ((convert (x) c)))) -> minmax (x c) if x is promoted
3935 and the outer convert demotes the expression back to x's type. */
3936 (for minmax (min max)
3937 (simplify
3938 (convert (minmax@0 (convert @1) INTEGER_CST@2))
3939 (if (INTEGRAL_TYPE_P (type)
3940 && types_match (@1, type) && int_fits_type_p (@2, type)
3941 && TYPE_SIGN (TREE_TYPE (@0)) == TYPE_SIGN (type)
3942 && TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type))
3943 (minmax @1 (convert @2)))))
3944
3945 (for minmax (FMIN_ALL FMAX_ALL)
3946 /* If either argument is NaN and other one is not sNaN, return the other
3947 one. Avoid the transformation if we get (and honor) a signalling NaN. */
3948 (simplify
3949 (minmax:c @0 REAL_CST@1)
3950 (if (real_isnan (TREE_REAL_CST_PTR (@1))
3951 && (!HONOR_SNANS (@1) || !TREE_REAL_CST (@1).signalling)
3952 && !tree_expr_maybe_signaling_nan_p (@0))
3953 @0)))
3954 /* Convert fmin/fmax to MIN_EXPR/MAX_EXPR. C99 requires these
3955 functions to return the numeric arg if the other one is NaN.
3956 MIN and MAX don't honor that, so only transform if -ffinite-math-only
3957 is set. C99 doesn't require -0.0 to be handled, so we don't have to
3958 worry about it either. */
3959 (if (flag_finite_math_only)
3960 (simplify
3961 (FMIN_ALL @0 @1)
3962 (min @0 @1))
3963 (simplify
3964 (FMAX_ALL @0 @1)
3965 (max @0 @1)))
3966 /* min (-A, -B) -> -max (A, B) */
3967 (for minmax (min max FMIN_ALL FMAX_ALL)
3968 maxmin (max min FMAX_ALL FMIN_ALL)
3969 (simplify
3970 (minmax (negate:s@2 @0) (negate:s@3 @1))
3971 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
3972 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3973 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
3974 (negate (maxmin @0 @1)))))
3975 /* MIN (~X, ~Y) -> ~MAX (X, Y)
3976 MAX (~X, ~Y) -> ~MIN (X, Y) */
3977 (for minmax (min max)
3978 maxmin (max min)
3979 (simplify
3980 (minmax (bit_not:s@2 @0) (bit_not:s@3 @1))
3981 (bit_not (maxmin @0 @1)))
3982 /* ~MAX(~X, Y) --> MIN(X, ~Y) */
3983 /* ~MIN(~X, Y) --> MAX(X, ~Y) */
3984 (simplify
3985 (bit_not (minmax:cs (bit_not @0) @1))
3986 (maxmin @0 (bit_not @1))))
3987
3988 /* MIN (X, Y) == X -> X <= Y */
3989 /* MIN (X, Y) < X -> X > Y */
3990 /* MIN (X, Y) >= X -> X <= Y */
3991 (for minmax (min min min min max max max max)
3992 cmp (eq ne lt ge eq ne gt le )
3993 out (le gt gt le ge lt lt ge )
3994 (simplify
3995 (cmp:c (minmax:c @0 @1) @0)
3996 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0)))
3997 (out @0 @1))))
3998 /* MIN (X, 5) == 0 -> X == 0
3999 MIN (X, 5) == 7 -> false */
4000 (for cmp (eq ne)
4001 (simplify
4002 (cmp (min @0 INTEGER_CST@1) INTEGER_CST@2)
4003 (if (wi::lt_p (wi::to_wide (@1), wi::to_wide (@2),
4004 TYPE_SIGN (TREE_TYPE (@0))))
4005 { constant_boolean_node (cmp == NE_EXPR, type); }
4006 (if (wi::gt_p (wi::to_wide (@1), wi::to_wide (@2),
4007 TYPE_SIGN (TREE_TYPE (@0))))
4008 (cmp @0 @2)))))
4009 (for cmp (eq ne)
4010 (simplify
4011 (cmp (max @0 INTEGER_CST@1) INTEGER_CST@2)
4012 (if (wi::gt_p (wi::to_wide (@1), wi::to_wide (@2),
4013 TYPE_SIGN (TREE_TYPE (@0))))
4014 { constant_boolean_node (cmp == NE_EXPR, type); }
4015 (if (wi::lt_p (wi::to_wide (@1), wi::to_wide (@2),
4016 TYPE_SIGN (TREE_TYPE (@0))))
4017 (cmp @0 @2)))))
4018
4019 /* X <= MAX(X, Y) -> true
4020 X > MAX(X, Y) -> false
4021 X >= MIN(X, Y) -> true
4022 X < MIN(X, Y) -> false */
4023 (for minmax (min min max max )
4024 cmp (ge lt le gt )
4025 (simplify
4026 (cmp:c @0 (minmax:c @0 @1))
4027 { constant_boolean_node (cmp == GE_EXPR || cmp == LE_EXPR, type); } ))
4028
4029 /* MIN (X, C1) < C2 -> X < C2 || C1 < C2 */
4030 (for minmax (min min max max min min max max )
4031 cmp (lt le gt ge gt ge lt le )
4032 comb (bit_ior bit_ior bit_ior bit_ior bit_and bit_and bit_and bit_and)
4033 (simplify
4034 (cmp (minmax @0 INTEGER_CST@1) INTEGER_CST@2)
4035 (comb (cmp @0 @2) (cmp @1 @2))))
4036
4037 /* Undo fancy ways of writing max/min or other ?: expressions, like
4038 a - ((a - b) & -(a < b)) and a - (a - b) * (a < b) into (a < b) ? b : a.
4039 People normally use ?: and that is what we actually try to optimize. */
4040 /* Transform A + (B-A)*cmp into cmp ? B : A. */
4041 (simplify
4042 (plus:c @0 (mult:c (minus @1 @0) zero_one_valued_p@2))
4043 (if (INTEGRAL_TYPE_P (type)
4044 && (GIMPLE || !TREE_SIDE_EFFECTS (@1)))
4045 (cond (convert:boolean_type_node @2) @1 @0)))
4046 /* Transform A - (A-B)*cmp into cmp ? B : A. */
4047 (simplify
4048 (minus @0 (mult:c (minus @0 @1) zero_one_valued_p@2))
4049 (if (INTEGRAL_TYPE_P (type)
4050 && (GIMPLE || !TREE_SIDE_EFFECTS (@1)))
4051 (cond (convert:boolean_type_node @2) @1 @0)))
4052 /* Transform A ^ (A^B)*cmp into cmp ? B : A. */
4053 (simplify
4054 (bit_xor:c @0 (mult:c (bit_xor:c @0 @1) zero_one_valued_p@2))
4055 (if (INTEGRAL_TYPE_P (type)
4056 && (GIMPLE || !TREE_SIDE_EFFECTS (@1)))
4057 (cond (convert:boolean_type_node @2) @1 @0)))
4058
4059 /* (x <= 0 ? -x : 0) -> max(-x, 0). */
4060 (simplify
4061 (cond (le @0 integer_zerop@1) (negate@2 @0) integer_zerop@1)
4062 (max @2 @1))
4063
4064 /* (zero_one == 0) ? y : z <op> y -> ((typeof(y))zero_one * z) <op> y */
4065 (for op (bit_xor bit_ior plus)
4066 (simplify
4067 (cond (eq zero_one_valued_p@0
4068 integer_zerop)
4069 @1
4070 (op:c @2 @1))
4071 (if (INTEGRAL_TYPE_P (type)
4072 && TYPE_PRECISION (type) > 1
4073 && (INTEGRAL_TYPE_P (TREE_TYPE (@0))))
4074 (op (mult (convert:type @0) @2) @1))))
4075
4076 /* (zero_one != 0) ? z <op> y : y -> ((typeof(y))zero_one * z) <op> y */
4077 (for op (bit_xor bit_ior plus)
4078 (simplify
4079 (cond (ne zero_one_valued_p@0
4080 integer_zerop)
4081 (op:c @2 @1)
4082 @1)
4083 (if (INTEGRAL_TYPE_P (type)
4084 && TYPE_PRECISION (type) > 1
4085 && (INTEGRAL_TYPE_P (TREE_TYPE (@0))))
4086 (op (mult (convert:type @0) @2) @1))))
4087
4088 /* Simplifications of shift and rotates. */
4089
4090 (for rotate (lrotate rrotate)
4091 (simplify
4092 (rotate integer_all_onesp@0 @1)
4093 @0))
4094
4095 /* Optimize -1 >> x for arithmetic right shifts. */
4096 (simplify
4097 (rshift integer_all_onesp@0 @1)
4098 (if (!TYPE_UNSIGNED (type))
4099 @0))
4100
4101 /* Optimize (x >> c) << c into x & (-1<<c). */
4102 (simplify
4103 (lshift (nop_convert? (rshift @0 INTEGER_CST@1)) @1)
4104 (if (wi::ltu_p (wi::to_wide (@1), element_precision (type)))
4105 /* It doesn't matter if the right shift is arithmetic or logical. */
4106 (bit_and (view_convert @0) (lshift { build_minus_one_cst (type); } @1))))
4107
4108 (simplify
4109 (lshift (convert (convert@2 (rshift @0 INTEGER_CST@1))) @1)
4110 (if (wi::ltu_p (wi::to_wide (@1), element_precision (type))
4111 /* Allow intermediate conversion to integral type with whatever sign, as
4112 long as the low TYPE_PRECISION (type)
4113 - TYPE_PRECISION (TREE_TYPE (@2)) bits are preserved. */
4114 && INTEGRAL_TYPE_P (type)
4115 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
4116 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
4117 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (@0))
4118 && (TYPE_PRECISION (TREE_TYPE (@2)) >= TYPE_PRECISION (type)
4119 || wi::geu_p (wi::to_wide (@1),
4120 TYPE_PRECISION (type)
4121 - TYPE_PRECISION (TREE_TYPE (@2)))))
4122 (bit_and (convert @0) (lshift { build_minus_one_cst (type); } @1))))
4123
4124 /* For (x << c) >> c, optimize into x & ((unsigned)-1 >> c) for
4125 unsigned x OR truncate into the precision(type) - c lowest bits
4126 of signed x (if they have mode precision or a precision of 1). */
4127 (simplify
4128 (rshift (nop_convert? (lshift @0 INTEGER_CST@1)) @@1)
4129 (if (wi::ltu_p (wi::to_wide (@1), element_precision (type)))
4130 (if (TYPE_UNSIGNED (type))
4131 (bit_and (convert @0) (rshift { build_minus_one_cst (type); } @1))
4132 (if (INTEGRAL_TYPE_P (type))
4133 (with {
4134 int width = element_precision (type) - tree_to_uhwi (@1);
4135 tree stype = NULL_TREE;
4136 if (width <= MAX_FIXED_MODE_SIZE)
4137 stype = build_nonstandard_integer_type (width, 0);
4138 }
4139 (if (stype && (width == 1 || type_has_mode_precision_p (stype)))
4140 (convert (convert:stype @0))))))))
4141
4142 /* Optimize x >> x into 0 */
4143 (simplify
4144 (rshift @0 @0)
4145 { build_zero_cst (type); })
4146
4147 (for shiftrotate (lrotate rrotate lshift rshift)
4148 (simplify
4149 (shiftrotate @0 integer_zerop)
4150 (non_lvalue @0))
4151 (simplify
4152 (shiftrotate integer_zerop@0 @1)
4153 @0)
4154 /* Prefer vector1 << scalar to vector1 << vector2
4155 if vector2 is uniform. */
4156 (for vec (VECTOR_CST CONSTRUCTOR)
4157 (simplify
4158 (shiftrotate @0 vec@1)
4159 (with { tree tem = uniform_vector_p (@1); }
4160 (if (tem)
4161 (shiftrotate @0 { tem; }))))))
4162
4163 /* Simplify X << Y where Y's low width bits are 0 to X, as only valid
4164 Y is 0. Similarly for X >> Y. */
4165 #if GIMPLE
4166 (for shift (lshift rshift)
4167 (simplify
4168 (shift @0 SSA_NAME@1)
4169 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1)))
4170 (with {
4171 int width = ceil_log2 (element_precision (TREE_TYPE (@0)));
4172 int prec = TYPE_PRECISION (TREE_TYPE (@1));
4173 }
4174 (if ((get_nonzero_bits (@1) & wi::mask (width, false, prec)) == 0)
4175 @0)))))
4176 #endif
4177
4178 /* Rewrite an LROTATE_EXPR by a constant into an
4179 RROTATE_EXPR by a new constant. */
4180 (simplify
4181 (lrotate @0 INTEGER_CST@1)
4182 (rrotate @0 { const_binop (MINUS_EXPR, TREE_TYPE (@1),
4183 build_int_cst (TREE_TYPE (@1),
4184 element_precision (type)), @1); }))
4185
4186 /* Turn (a OP c1) OP c2 into a OP (c1+c2). */
4187 (for op (lrotate rrotate rshift lshift)
4188 (simplify
4189 (op (op @0 INTEGER_CST@1) INTEGER_CST@2)
4190 (with { unsigned int prec = element_precision (type); }
4191 (if (wi::ge_p (wi::to_wide (@1), 0, TYPE_SIGN (TREE_TYPE (@1)))
4192 && wi::lt_p (wi::to_wide (@1), prec, TYPE_SIGN (TREE_TYPE (@1)))
4193 && wi::ge_p (wi::to_wide (@2), 0, TYPE_SIGN (TREE_TYPE (@2)))
4194 && wi::lt_p (wi::to_wide (@2), prec, TYPE_SIGN (TREE_TYPE (@2))))
4195 (with { unsigned int low = (tree_to_uhwi (@1)
4196 + tree_to_uhwi (@2)); }
4197 /* Deal with a OP (c1 + c2) being undefined but (a OP c1) OP c2
4198 being well defined. */
4199 (if (low >= prec)
4200 (if (op == LROTATE_EXPR || op == RROTATE_EXPR)
4201 (op @0 { build_int_cst (TREE_TYPE (@1), low % prec); })
4202 (if (TYPE_UNSIGNED (type) || op == LSHIFT_EXPR)
4203 { build_zero_cst (type); }
4204 (op @0 { build_int_cst (TREE_TYPE (@1), prec - 1); })))
4205 (op @0 { build_int_cst (TREE_TYPE (@1), low); })))))))
4206
4207
4208 /* Simplify (CST << x) & 1 to 0 if CST is even or to x == 0 if it is odd. */
4209 (simplify
4210 (bit_and (lshift INTEGER_CST@1 @0) integer_onep)
4211 (if ((wi::to_wide (@1) & 1) != 0)
4212 (convert (eq:boolean_type_node @0 { build_zero_cst (TREE_TYPE (@0)); }))
4213 { build_zero_cst (type); }))
4214
4215 /* Simplify ((C << x) & D) != 0 where C and D are power of two constants,
4216 either to false if D is smaller (unsigned comparison) than C, or to
4217 x == log2 (D) - log2 (C). Similarly for right shifts.
4218 Note for `(1 >> x)`, the & 1 has been removed so matching that seperately. */
4219 (for cmp (ne eq)
4220 icmp (eq ne)
4221 (simplify
4222 (cmp (bit_and (lshift integer_pow2p@1 @0) integer_pow2p@2) integer_zerop)
4223 (with { int c1 = wi::clz (wi::to_wide (@1));
4224 int c2 = wi::clz (wi::to_wide (@2)); }
4225 (if (c1 < c2)
4226 { constant_boolean_node (cmp == NE_EXPR ? false : true, type); }
4227 (icmp @0 { build_int_cst (TREE_TYPE (@0), c1 - c2); }))))
4228 (simplify
4229 (cmp (bit_and (rshift integer_pow2p@1 @0) integer_pow2p@2) integer_zerop)
4230 (if (tree_int_cst_sgn (@1) > 0)
4231 (with { int c1 = wi::clz (wi::to_wide (@1));
4232 int c2 = wi::clz (wi::to_wide (@2)); }
4233 (if (c1 > c2)
4234 { constant_boolean_node (cmp == NE_EXPR ? false : true, type); }
4235 (icmp @0 { build_int_cst (TREE_TYPE (@0), c2 - c1); })))))
4236 /* `(1 >> X) != 0` -> `X == 0` */
4237 /* `(1 >> X) == 0` -> `X != 0` */
4238 (simplify
4239 (cmp (rshift integer_onep@1 @0) integer_zerop)
4240 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1)))
4241 (icmp @0 { build_zero_cst (TREE_TYPE (@0)); }))))
4242
4243 /* (CST1 << A) == CST2 -> A == ctz (CST2) - ctz (CST1)
4244 (CST1 << A) != CST2 -> A != ctz (CST2) - ctz (CST1)
4245 if CST2 != 0. */
4246 (for cmp (ne eq)
4247 (simplify
4248 (cmp (lshift INTEGER_CST@0 @1) INTEGER_CST@2)
4249 (with { int cand = wi::ctz (wi::to_wide (@2)) - wi::ctz (wi::to_wide (@0)); }
4250 (if (cand < 0
4251 || (!integer_zerop (@2)
4252 && wi::lshift (wi::to_wide (@0), cand) != wi::to_wide (@2)))
4253 { constant_boolean_node (cmp == NE_EXPR, type); }
4254 (if (!integer_zerop (@2)
4255 && wi::lshift (wi::to_wide (@0), cand) == wi::to_wide (@2))
4256 (cmp @1 { build_int_cst (TREE_TYPE (@1), cand); }))))))
4257
4258 /* Fold ((X << C1) & C2) cmp C3 into (X & (C2 >> C1)) cmp (C3 >> C1)
4259 ((X >> C1) & C2) cmp C3 into (X & (C2 << C1)) cmp (C3 << C1). */
4260 (for cmp (ne eq)
4261 (simplify
4262 (cmp (bit_and:s (lshift:s @0 INTEGER_CST@1) INTEGER_CST@2) INTEGER_CST@3)
4263 (if (tree_fits_shwi_p (@1)
4264 && tree_to_shwi (@1) > 0
4265 && tree_to_shwi (@1) < TYPE_PRECISION (TREE_TYPE (@0)))
4266 (if (tree_to_shwi (@1) > wi::ctz (wi::to_wide (@3)))
4267 { constant_boolean_node (cmp == NE_EXPR, type); }
4268 (with { wide_int c1 = wi::to_wide (@1);
4269 wide_int c2 = wi::lrshift (wi::to_wide (@2), c1);
4270 wide_int c3 = wi::lrshift (wi::to_wide (@3), c1); }
4271 (cmp (bit_and @0 { wide_int_to_tree (TREE_TYPE (@0), c2); })
4272 { wide_int_to_tree (TREE_TYPE (@0), c3); })))))
4273 (simplify
4274 (cmp (bit_and:s (rshift:s @0 INTEGER_CST@1) INTEGER_CST@2) INTEGER_CST@3)
4275 (if (tree_fits_shwi_p (@1)
4276 && tree_to_shwi (@1) > 0
4277 && tree_to_shwi (@1) < TYPE_PRECISION (TREE_TYPE (@0)))
4278 (with { tree t0 = TREE_TYPE (@0);
4279 unsigned int prec = TYPE_PRECISION (t0);
4280 wide_int c1 = wi::to_wide (@1);
4281 wide_int c2 = wi::to_wide (@2);
4282 wide_int c3 = wi::to_wide (@3);
4283 wide_int sb = wi::set_bit_in_zero (prec - 1, prec); }
4284 (if ((c2 & c3) != c3)
4285 { constant_boolean_node (cmp == NE_EXPR, type); }
4286 (if (TYPE_UNSIGNED (t0))
4287 (if ((c3 & wi::arshift (sb, c1 - 1)) != 0)
4288 { constant_boolean_node (cmp == NE_EXPR, type); }
4289 (cmp (bit_and @0 { wide_int_to_tree (t0, c2 << c1); })
4290 { wide_int_to_tree (t0, c3 << c1); }))
4291 (with { wide_int smask = wi::arshift (sb, c1); }
4292 (switch
4293 (if ((c2 & smask) == 0)
4294 (cmp (bit_and @0 { wide_int_to_tree (t0, c2 << c1); })
4295 { wide_int_to_tree (t0, c3 << c1); }))
4296 (if ((c3 & smask) == 0)
4297 (cmp (bit_and @0 { wide_int_to_tree (t0, (c2 << c1) | sb); })
4298 { wide_int_to_tree (t0, c3 << c1); }))
4299 (if ((c2 & smask) != (c3 & smask))
4300 { constant_boolean_node (cmp == NE_EXPR, type); })
4301 (cmp (bit_and @0 { wide_int_to_tree (t0, (c2 << c1) | sb); })
4302 { wide_int_to_tree (t0, (c3 << c1) | sb); })))))))))
4303
4304 /* Fold (X << C1) & C2 into (X << C1) & (C2 | ((1 << C1) - 1))
4305 (X >> C1) & C2 into (X >> C1) & (C2 | ~((type) -1 >> C1))
4306 if the new mask might be further optimized. */
4307 (for shift (lshift rshift)
4308 (simplify
4309 (bit_and (convert?:s@4 (shift:s@5 (convert1?@3 @0) INTEGER_CST@1))
4310 INTEGER_CST@2)
4311 (if (tree_nop_conversion_p (TREE_TYPE (@4), TREE_TYPE (@5))
4312 && TYPE_PRECISION (type) <= HOST_BITS_PER_WIDE_INT
4313 && tree_fits_uhwi_p (@1)
4314 && tree_to_uhwi (@1) > 0
4315 && tree_to_uhwi (@1) < TYPE_PRECISION (type))
4316 (with
4317 {
4318 unsigned int shiftc = tree_to_uhwi (@1);
4319 unsigned HOST_WIDE_INT mask = TREE_INT_CST_LOW (@2);
4320 unsigned HOST_WIDE_INT newmask, zerobits = 0;
4321 tree shift_type = TREE_TYPE (@3);
4322 unsigned int prec;
4323
4324 if (shift == LSHIFT_EXPR)
4325 zerobits = ((HOST_WIDE_INT_1U << shiftc) - 1);
4326 else if (shift == RSHIFT_EXPR
4327 && type_has_mode_precision_p (shift_type))
4328 {
4329 prec = TYPE_PRECISION (TREE_TYPE (@3));
4330 tree arg00 = @0;
4331 /* See if more bits can be proven as zero because of
4332 zero extension. */
4333 if (@3 != @0
4334 && TYPE_UNSIGNED (TREE_TYPE (@0)))
4335 {
4336 tree inner_type = TREE_TYPE (@0);
4337 if (type_has_mode_precision_p (inner_type)
4338 && TYPE_PRECISION (inner_type) < prec)
4339 {
4340 prec = TYPE_PRECISION (inner_type);
4341 /* See if we can shorten the right shift. */
4342 if (shiftc < prec)
4343 shift_type = inner_type;
4344 /* Otherwise X >> C1 is all zeros, so we'll optimize
4345 it into (X, 0) later on by making sure zerobits
4346 is all ones. */
4347 }
4348 }
4349 zerobits = HOST_WIDE_INT_M1U;
4350 if (shiftc < prec)
4351 {
4352 zerobits >>= HOST_BITS_PER_WIDE_INT - shiftc;
4353 zerobits <<= prec - shiftc;
4354 }
4355 /* For arithmetic shift if sign bit could be set, zerobits
4356 can contain actually sign bits, so no transformation is
4357 possible, unless MASK masks them all away. In that
4358 case the shift needs to be converted into logical shift. */
4359 if (!TYPE_UNSIGNED (TREE_TYPE (@3))
4360 && prec == TYPE_PRECISION (TREE_TYPE (@3)))
4361 {
4362 if ((mask & zerobits) == 0)
4363 shift_type = unsigned_type_for (TREE_TYPE (@3));
4364 else
4365 zerobits = 0;
4366 }
4367 }
4368 }
4369 /* ((X << 16) & 0xff00) is (X, 0). */
4370 (if ((mask & zerobits) == mask)
4371 { build_int_cst (type, 0); }
4372 (with { newmask = mask | zerobits; }
4373 (if (newmask != mask && (newmask & (newmask + 1)) == 0)
4374 (with
4375 {
4376 /* Only do the transformation if NEWMASK is some integer
4377 mode's mask. */
4378 for (prec = BITS_PER_UNIT;
4379 prec < HOST_BITS_PER_WIDE_INT; prec <<= 1)
4380 if (newmask == (HOST_WIDE_INT_1U << prec) - 1)
4381 break;
4382 }
4383 (if (prec < HOST_BITS_PER_WIDE_INT
4384 || newmask == HOST_WIDE_INT_M1U)
4385 (with
4386 { tree newmaskt = build_int_cst_type (TREE_TYPE (@2), newmask); }
4387 (if (!tree_int_cst_equal (newmaskt, @2))
4388 (if (shift_type != TREE_TYPE (@3))
4389 (bit_and (convert (shift:shift_type (convert @3) @1)) { newmaskt; })
4390 (bit_and @4 { newmaskt; })))))))))))))
4391
4392 /* ((1 << n) & M) != 0 -> n == log2 (M) */
4393 (for cmp (ne eq)
4394 icmp (eq ne)
4395 (simplify
4396 (cmp
4397 (bit_and
4398 (nop_convert? (lshift integer_onep @0)) integer_pow2p@1) integer_zerop)
4399 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)))
4400 (icmp @0 { wide_int_to_tree (TREE_TYPE (@0),
4401 wi::exact_log2 (wi::to_wide (@1))); }))))
4402
4403 /* Fold (X {&,^,|} C2) << C1 into (X << C1) {&,^,|} (C2 << C1)
4404 (X {&,^,|} C2) >> C1 into (X >> C1) & (C2 >> C1). */
4405 (for shift (lshift rshift)
4406 (for bit_op (bit_and bit_xor bit_ior)
4407 (simplify
4408 (shift (convert?:s (bit_op:s @0 INTEGER_CST@2)) INTEGER_CST@1)
4409 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
4410 (with { tree mask = int_const_binop (shift, fold_convert (type, @2), @1); }
4411 (if (mask)
4412 (bit_op (shift (convert @0) @1) { mask; })))))))
4413
4414 /* ~(~X >> Y) -> X >> Y (for arithmetic shift). */
4415 (simplify
4416 (bit_not (convert1?:s (rshift:s (convert2?@0 (bit_not @1)) @2)))
4417 (if (!TYPE_UNSIGNED (TREE_TYPE (@0))
4418 && (element_precision (TREE_TYPE (@0))
4419 <= element_precision (TREE_TYPE (@1))
4420 || !TYPE_UNSIGNED (TREE_TYPE (@1))))
4421 (with
4422 { tree shift_type = TREE_TYPE (@0); }
4423 (convert (rshift (convert:shift_type @1) @2)))))
4424
4425 /* ~(~X >>r Y) -> X >>r Y
4426 ~(~X <<r Y) -> X <<r Y */
4427 (for rotate (lrotate rrotate)
4428 (simplify
4429 (bit_not (convert1?:s (rotate:s (convert2?@0 (bit_not @1)) @2)))
4430 (if ((element_precision (TREE_TYPE (@0))
4431 <= element_precision (TREE_TYPE (@1))
4432 || !TYPE_UNSIGNED (TREE_TYPE (@1)))
4433 && (element_precision (type) <= element_precision (TREE_TYPE (@0))
4434 || !TYPE_UNSIGNED (TREE_TYPE (@0))))
4435 (with
4436 { tree rotate_type = TREE_TYPE (@0); }
4437 (convert (rotate (convert:rotate_type @1) @2))))))
4438
4439 (for cmp (eq ne)
4440 (for rotate (lrotate rrotate)
4441 invrot (rrotate lrotate)
4442 /* (X >>r Y) cmp (Z >>r Y) may simplify to X cmp Y. */
4443 (simplify
4444 (cmp (rotate @1 @0) (rotate @2 @0))
4445 (cmp @1 @2))
4446 /* (X >>r C1) cmp C2 may simplify to X cmp C3. */
4447 (simplify
4448 (cmp (rotate @0 INTEGER_CST@1) INTEGER_CST@2)
4449 (cmp @0 { const_binop (invrot, TREE_TYPE (@0), @2, @1); }))
4450 /* (X >>r Y) cmp C where C is 0 or ~0, may simplify to X cmp C. */
4451 (simplify
4452 (cmp (rotate @0 @1) INTEGER_CST@2)
4453 (if (integer_zerop (@2) || integer_all_onesp (@2))
4454 (cmp @0 @2)))))
4455
4456 /* Narrow a lshift by constant. */
4457 (simplify
4458 (convert (lshift:s@0 @1 INTEGER_CST@2))
4459 (if (INTEGRAL_TYPE_P (type)
4460 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
4461 && !integer_zerop (@2)
4462 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0)))
4463 (if (TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (@0))
4464 || wi::ltu_p (wi::to_wide (@2), TYPE_PRECISION (type)))
4465 (lshift (convert @1) @2)
4466 (if (wi::ltu_p (wi::to_wide (@2), TYPE_PRECISION (TREE_TYPE (@0))))
4467 { build_zero_cst (type); }))))
4468
4469 /* Simplifications of conversions. */
4470
4471 /* Basic strip-useless-type-conversions / strip_nops. */
4472 (for cvt (convert view_convert float fix_trunc)
4473 (simplify
4474 (cvt @0)
4475 (if ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@0)))
4476 || (GENERIC && type == TREE_TYPE (@0)))
4477 @0)))
4478
4479 /* Contract view-conversions. */
4480 (simplify
4481 (view_convert (view_convert @0))
4482 (view_convert @0))
4483
4484 /* For integral conversions with the same precision or pointer
4485 conversions use a NOP_EXPR instead. */
4486 (simplify
4487 (view_convert @0)
4488 (if ((INTEGRAL_TYPE_P (type) || POINTER_TYPE_P (type))
4489 && (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
4490 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (@0)))
4491 (convert @0)))
4492
4493 /* Strip inner integral conversions that do not change precision or size, or
4494 zero-extend while keeping the same size (for bool-to-char). */
4495 (simplify
4496 (view_convert (convert@0 @1))
4497 (if ((INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
4498 && (INTEGRAL_TYPE_P (TREE_TYPE (@1)) || POINTER_TYPE_P (TREE_TYPE (@1)))
4499 && TYPE_SIZE (TREE_TYPE (@0)) == TYPE_SIZE (TREE_TYPE (@1))
4500 && (TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1))
4501 || (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (TREE_TYPE (@1))
4502 && TYPE_UNSIGNED (TREE_TYPE (@1)))))
4503 (view_convert @1)))
4504
4505 /* Simplify a view-converted empty or single-element constructor. */
4506 (simplify
4507 (view_convert CONSTRUCTOR@0)
4508 (with
4509 { tree ctor = (TREE_CODE (@0) == SSA_NAME
4510 ? gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0)) : @0); }
4511 (switch
4512 (if (CONSTRUCTOR_NELTS (ctor) == 0)
4513 { build_zero_cst (type); })
4514 (if (CONSTRUCTOR_NELTS (ctor) == 1
4515 && VECTOR_TYPE_P (TREE_TYPE (ctor))
4516 && operand_equal_p (TYPE_SIZE (type),
4517 TYPE_SIZE (TREE_TYPE
4518 (CONSTRUCTOR_ELT (ctor, 0)->value))))
4519 (view_convert { CONSTRUCTOR_ELT (ctor, 0)->value; })))))
4520
4521 /* Re-association barriers around constants and other re-association
4522 barriers can be removed. */
4523 (simplify
4524 (paren CONSTANT_CLASS_P@0)
4525 @0)
4526 (simplify
4527 (paren (paren@1 @0))
4528 @1)
4529
4530 /* Handle cases of two conversions in a row. */
4531 (for ocvt (convert float fix_trunc)
4532 (for icvt (convert float)
4533 (simplify
4534 (ocvt (icvt@1 @0))
4535 (with
4536 {
4537 tree inside_type = TREE_TYPE (@0);
4538 tree inter_type = TREE_TYPE (@1);
4539 int inside_int = INTEGRAL_TYPE_P (inside_type);
4540 int inside_ptr = POINTER_TYPE_P (inside_type);
4541 int inside_float = FLOAT_TYPE_P (inside_type);
4542 int inside_vec = VECTOR_TYPE_P (inside_type);
4543 unsigned int inside_prec = element_precision (inside_type);
4544 int inside_unsignedp = TYPE_UNSIGNED (inside_type);
4545 int inter_int = INTEGRAL_TYPE_P (inter_type);
4546 int inter_ptr = POINTER_TYPE_P (inter_type);
4547 int inter_float = FLOAT_TYPE_P (inter_type);
4548 int inter_vec = VECTOR_TYPE_P (inter_type);
4549 unsigned int inter_prec = element_precision (inter_type);
4550 int inter_unsignedp = TYPE_UNSIGNED (inter_type);
4551 int final_int = INTEGRAL_TYPE_P (type);
4552 int final_ptr = POINTER_TYPE_P (type);
4553 int final_float = FLOAT_TYPE_P (type);
4554 int final_vec = VECTOR_TYPE_P (type);
4555 unsigned int final_prec = element_precision (type);
4556 int final_unsignedp = TYPE_UNSIGNED (type);
4557 }
4558 (switch
4559 /* In addition to the cases of two conversions in a row
4560 handled below, if we are converting something to its own
4561 type via an object of identical or wider precision, neither
4562 conversion is needed. */
4563 (if (((GIMPLE && useless_type_conversion_p (type, inside_type))
4564 || (GENERIC
4565 && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (inside_type)))
4566 && (((inter_int || inter_ptr) && final_int)
4567 || (inter_float && final_float))
4568 && inter_prec >= final_prec)
4569 (ocvt @0))
4570
4571 /* Likewise, if the intermediate and initial types are either both
4572 float or both integer, we don't need the middle conversion if the
4573 former is wider than the latter and doesn't change the signedness
4574 (for integers). Avoid this if the final type is a pointer since
4575 then we sometimes need the middle conversion. */
4576 (if (((inter_int && inside_int) || (inter_float && inside_float))
4577 && (final_int || final_float)
4578 && inter_prec >= inside_prec
4579 && (inter_float || inter_unsignedp == inside_unsignedp))
4580 (ocvt @0))
4581
4582 /* If we have a sign-extension of a zero-extended value, we can
4583 replace that by a single zero-extension. Likewise if the
4584 final conversion does not change precision we can drop the
4585 intermediate conversion. */
4586 (if (inside_int && inter_int && final_int
4587 && ((inside_prec < inter_prec && inter_prec < final_prec
4588 && inside_unsignedp && !inter_unsignedp)
4589 || final_prec == inter_prec))
4590 (ocvt @0))
4591
4592 /* Two conversions in a row are not needed unless:
4593 - some conversion is floating-point (overstrict for now), or
4594 - some conversion is a vector (overstrict for now), or
4595 - the intermediate type is narrower than both initial and
4596 final, or
4597 - the intermediate type and innermost type differ in signedness,
4598 and the outermost type is wider than the intermediate, or
4599 - the initial type is a pointer type and the precisions of the
4600 intermediate and final types differ, or
4601 - the final type is a pointer type and the precisions of the
4602 initial and intermediate types differ. */
4603 (if (! inside_float && ! inter_float && ! final_float
4604 && ! inside_vec && ! inter_vec && ! final_vec
4605 && (inter_prec >= inside_prec || inter_prec >= final_prec)
4606 && ! (inside_int && inter_int
4607 && inter_unsignedp != inside_unsignedp
4608 && inter_prec < final_prec)
4609 && ((inter_unsignedp && inter_prec > inside_prec)
4610 == (final_unsignedp && final_prec > inter_prec))
4611 && ! (inside_ptr && inter_prec != final_prec)
4612 && ! (final_ptr && inside_prec != inter_prec))
4613 (ocvt @0))
4614
4615 /* `(outer:M)(inter:N) a:O`
4616 can be converted to `(outer:M) a`
4617 if M <= O && N >= O. No matter what signedness of the casts,
4618 as the final is either a truncation from the original or just
4619 a sign change of the type. */
4620 (if (inside_int && inter_int && final_int
4621 && final_prec <= inside_prec
4622 && inter_prec >= inside_prec)
4623 (convert @0))
4624
4625 /* A truncation to an unsigned type (a zero-extension) should be
4626 canonicalized as bitwise and of a mask. */
4627 (if (GIMPLE /* PR70366: doing this in GENERIC breaks -Wconversion. */
4628 && final_int && inter_int && inside_int
4629 && final_prec == inside_prec
4630 && final_prec > inter_prec
4631 && inter_unsignedp)
4632 (convert (bit_and @0 { wide_int_to_tree
4633 (inside_type,
4634 wi::mask (inter_prec, false,
4635 TYPE_PRECISION (inside_type))); })))
4636
4637 /* If we are converting an integer to a floating-point that can
4638 represent it exactly and back to an integer, we can skip the
4639 floating-point conversion. */
4640 (if (GIMPLE /* PR66211 */
4641 && inside_int && inter_float && final_int &&
4642 (unsigned) significand_size (TYPE_MODE (inter_type))
4643 >= inside_prec - !inside_unsignedp)
4644 (convert @0)))))))
4645
4646 /* (float_type)(integer_type) x -> trunc (x) if the type of x matches
4647 float_type. Only do the transformation if we do not need to preserve
4648 trapping behaviour, so require !flag_trapping_math. */
4649 #if GIMPLE
4650 (simplify
4651 (float (fix_trunc @0))
4652 (if (!flag_trapping_math
4653 && types_match (type, TREE_TYPE (@0))
4654 && direct_internal_fn_supported_p (IFN_TRUNC, type,
4655 OPTIMIZE_FOR_BOTH))
4656 (IFN_TRUNC @0)))
4657 #endif
4658
4659 /* If we have a narrowing conversion to an integral type that is fed by a
4660 BIT_AND_EXPR, we might be able to remove the BIT_AND_EXPR if it merely
4661 masks off bits outside the final type (and nothing else). */
4662 (simplify
4663 (convert (bit_and @0 INTEGER_CST@1))
4664 (if (INTEGRAL_TYPE_P (type)
4665 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
4666 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))
4667 && operand_equal_p (@1, build_low_bits_mask (TREE_TYPE (@1),
4668 TYPE_PRECISION (type)), 0))
4669 (convert @0)))
4670
4671
4672 /* (X /[ex] A) * A -> X. */
4673 (simplify
4674 (mult (convert1? (exact_div @0 @@1)) (convert2? @1))
4675 (convert @0))
4676
4677 /* Simplify (A / B) * B + (A % B) -> A. */
4678 (for div (trunc_div ceil_div floor_div round_div)
4679 mod (trunc_mod ceil_mod floor_mod round_mod)
4680 (simplify
4681 (plus:c (mult:c (div @0 @1) @1) (mod @0 @1))
4682 @0))
4683
4684 /* x / y * y == x -> x % y == 0. */
4685 (simplify
4686 (eq:c (mult:c (trunc_div:s @0 @1) @1) @0)
4687 (if (TREE_CODE (TREE_TYPE (@0)) != COMPLEX_TYPE)
4688 (eq (trunc_mod @0 @1) { build_zero_cst (TREE_TYPE (@0)); })))
4689
4690 /* ((X /[ex] A) +- B) * A --> X +- A * B. */
4691 (for op (plus minus)
4692 (simplify
4693 (mult (convert1? (op (convert2? (exact_div @0 INTEGER_CST@@1)) INTEGER_CST@2)) @1)
4694 (if (tree_nop_conversion_p (type, TREE_TYPE (@2))
4695 && tree_nop_conversion_p (TREE_TYPE (@0), TREE_TYPE (@2)))
4696 (with
4697 {
4698 wi::overflow_type overflow;
4699 wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@2),
4700 TYPE_SIGN (type), &overflow);
4701 }
4702 (if (types_match (type, TREE_TYPE (@2))
4703 && types_match (TREE_TYPE (@0), TREE_TYPE (@2)) && !overflow)
4704 (op @0 { wide_int_to_tree (type, mul); })
4705 (with { tree utype = unsigned_type_for (type); }
4706 (convert (op (convert:utype @0)
4707 (mult (convert:utype @1) (convert:utype @2))))))))))
4708
4709 /* Canonicalization of binary operations. */
4710
4711 /* Convert X + -C into X - C. */
4712 (simplify
4713 (plus @0 REAL_CST@1)
4714 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
4715 (with { tree tem = const_unop (NEGATE_EXPR, type, @1); }
4716 (if (!TREE_OVERFLOW (tem) || !flag_trapping_math)
4717 (minus @0 { tem; })))))
4718
4719 /* Convert x+x into x*2. */
4720 (simplify
4721 (plus @0 @0)
4722 (if (SCALAR_FLOAT_TYPE_P (type))
4723 (mult @0 { build_real (type, dconst2); })
4724 (if (INTEGRAL_TYPE_P (type))
4725 (mult @0 { build_int_cst (type, 2); }))))
4726
4727 /* 0 - X -> -X. */
4728 (simplify
4729 (minus integer_zerop @1)
4730 (negate @1))
4731 (simplify
4732 (pointer_diff integer_zerop @1)
4733 (negate (convert @1)))
4734
4735 /* (ARG0 - ARG1) is the same as (-ARG1 + ARG0). So check whether
4736 ARG0 is zero and X + ARG0 reduces to X, since that would mean
4737 (-ARG1 + ARG0) reduces to -ARG1. */
4738 (simplify
4739 (minus real_zerop@0 @1)
4740 (if (fold_real_zero_addition_p (type, @1, @0, 0))
4741 (negate @1)))
4742
4743 /* Transform x * -1 into -x. */
4744 (simplify
4745 (mult @0 integer_minus_onep)
4746 (negate @0))
4747
4748 /* Reassociate (X * CST) * Y to (X * Y) * CST. This does not introduce
4749 signed overflow for CST != 0 && CST != -1. */
4750 (simplify
4751 (mult:c (mult:s@3 @0 INTEGER_CST@1) @2)
4752 (if (TREE_CODE (@2) != INTEGER_CST
4753 && single_use (@3)
4754 && !integer_zerop (@1) && !integer_minus_onep (@1))
4755 (mult (mult @0 @2) @1)))
4756
4757 /* True if we can easily extract the real and imaginary parts of a complex
4758 number. */
4759 (match compositional_complex
4760 (convert? (complex @0 @1)))
4761
4762 /* COMPLEX_EXPR and REALPART/IMAGPART_EXPR cancellations. */
4763 (simplify
4764 (complex (realpart @0) (imagpart @0))
4765 @0)
4766 (simplify
4767 (realpart (complex @0 @1))
4768 @0)
4769 (simplify
4770 (imagpart (complex @0 @1))
4771 @1)
4772
4773 /* Sometimes we only care about half of a complex expression. */
4774 (simplify
4775 (realpart (convert?:s (conj:s @0)))
4776 (convert (realpart @0)))
4777 (simplify
4778 (imagpart (convert?:s (conj:s @0)))
4779 (convert (negate (imagpart @0))))
4780 (for part (realpart imagpart)
4781 (for op (plus minus)
4782 (simplify
4783 (part (convert?:s@2 (op:s @0 @1)))
4784 (convert (op (part @0) (part @1))))))
4785 (simplify
4786 (realpart (convert?:s (CEXPI:s @0)))
4787 (convert (COS @0)))
4788 (simplify
4789 (imagpart (convert?:s (CEXPI:s @0)))
4790 (convert (SIN @0)))
4791
4792 /* conj(conj(x)) -> x */
4793 (simplify
4794 (conj (convert? (conj @0)))
4795 (if (tree_nop_conversion_p (TREE_TYPE (@0), type))
4796 (convert @0)))
4797
4798 /* conj({x,y}) -> {x,-y} */
4799 (simplify
4800 (conj (convert?:s (complex:s @0 @1)))
4801 (with { tree itype = TREE_TYPE (type); }
4802 (complex (convert:itype @0) (negate (convert:itype @1)))))
4803
4804 /* BSWAP simplifications, transforms checked by gcc.dg/builtin-bswap-8.c. */
4805 (for bswap (BSWAP)
4806 (simplify
4807 (bswap (bswap @0))
4808 @0)
4809 (simplify
4810 (bswap (bit_not (bswap @0)))
4811 (bit_not @0))
4812 (for bitop (bit_xor bit_ior bit_and)
4813 (simplify
4814 (bswap (bitop:c (bswap @0) @1))
4815 (bitop @0 (bswap @1))))
4816 (for cmp (eq ne)
4817 (simplify
4818 (cmp (bswap@2 @0) (bswap @1))
4819 (with { tree ctype = TREE_TYPE (@2); }
4820 (cmp (convert:ctype @0) (convert:ctype @1))))
4821 (simplify
4822 (cmp (bswap @0) INTEGER_CST@1)
4823 (with { tree ctype = TREE_TYPE (@1); }
4824 (cmp (convert:ctype @0) (bswap! @1)))))
4825 /* (bswap(x) >> C1) & C2 can sometimes be simplified to (x >> C3) & C2. */
4826 (simplify
4827 (bit_and (convert1? (rshift@0 (convert2? (bswap@4 @1)) INTEGER_CST@2))
4828 INTEGER_CST@3)
4829 (if (BITS_PER_UNIT == 8
4830 && tree_fits_uhwi_p (@2)
4831 && tree_fits_uhwi_p (@3))
4832 (with
4833 {
4834 unsigned HOST_WIDE_INT prec = TYPE_PRECISION (TREE_TYPE (@4));
4835 unsigned HOST_WIDE_INT bits = tree_to_uhwi (@2);
4836 unsigned HOST_WIDE_INT mask = tree_to_uhwi (@3);
4837 unsigned HOST_WIDE_INT lo = bits & 7;
4838 unsigned HOST_WIDE_INT hi = bits - lo;
4839 }
4840 (if (bits < prec
4841 && mask < (256u>>lo)
4842 && bits < TYPE_PRECISION (TREE_TYPE(@0)))
4843 (with { unsigned HOST_WIDE_INT ns = (prec - (hi + 8)) + lo; }
4844 (if (ns == 0)
4845 (bit_and (convert @1) @3)
4846 (with
4847 {
4848 tree utype = unsigned_type_for (TREE_TYPE (@1));
4849 tree nst = build_int_cst (integer_type_node, ns);
4850 }
4851 (bit_and (convert (rshift:utype (convert:utype @1) {nst;})) @3))))))))
4852 /* bswap(x) >> C1 can sometimes be simplified to (T)x >> C2. */
4853 (simplify
4854 (rshift (convert? (bswap@2 @0)) INTEGER_CST@1)
4855 (if (BITS_PER_UNIT == 8
4856 && CHAR_TYPE_SIZE == 8
4857 && tree_fits_uhwi_p (@1))
4858 (with
4859 {
4860 unsigned HOST_WIDE_INT prec = TYPE_PRECISION (TREE_TYPE (@2));
4861 unsigned HOST_WIDE_INT bits = tree_to_uhwi (@1);
4862 /* If the bswap was extended before the original shift, this
4863 byte (shift) has the sign of the extension, not the sign of
4864 the original shift. */
4865 tree st = TYPE_PRECISION (type) > prec ? TREE_TYPE (@2) : type;
4866 }
4867 /* Special case: logical right shift of sign-extended bswap.
4868 (unsigned)(short)bswap16(x)>>12 is (unsigned)((short)x<<8)>>12. */
4869 (if (TYPE_PRECISION (type) > prec
4870 && !TYPE_UNSIGNED (TREE_TYPE (@2))
4871 && TYPE_UNSIGNED (type)
4872 && bits < prec && bits + 8 >= prec)
4873 (with { tree nst = build_int_cst (integer_type_node, prec - 8); }
4874 (rshift (convert (lshift:st (convert:st @0) {nst;})) @1))
4875 (if (bits + 8 == prec)
4876 (if (TYPE_UNSIGNED (st))
4877 (convert (convert:unsigned_char_type_node @0))
4878 (convert (convert:signed_char_type_node @0)))
4879 (if (bits < prec && bits + 8 > prec)
4880 (with
4881 {
4882 tree nst = build_int_cst (integer_type_node, bits & 7);
4883 tree bt = TYPE_UNSIGNED (st) ? unsigned_char_type_node
4884 : signed_char_type_node;
4885 }
4886 (convert (rshift:bt (convert:bt @0) {nst;})))))))))
4887 /* bswap(x) & C1 can sometimes be simplified to (x >> C2) & C1. */
4888 (simplify
4889 (bit_and (convert? (bswap@2 @0)) INTEGER_CST@1)
4890 (if (BITS_PER_UNIT == 8
4891 && tree_fits_uhwi_p (@1)
4892 && tree_to_uhwi (@1) < 256)
4893 (with
4894 {
4895 unsigned HOST_WIDE_INT prec = TYPE_PRECISION (TREE_TYPE (@2));
4896 tree utype = unsigned_type_for (TREE_TYPE (@0));
4897 tree nst = build_int_cst (integer_type_node, prec - 8);
4898 }
4899 (bit_and (convert (rshift:utype (convert:utype @0) {nst;})) @1)))))
4900
4901
4902 /* Combine COND_EXPRs and VEC_COND_EXPRs. */
4903
4904 /* Simplify constant conditions.
4905 Only optimize constant conditions when the selected branch
4906 has the same type as the COND_EXPR. This avoids optimizing
4907 away "c ? x : throw", where the throw has a void type.
4908 Note that we cannot throw away the fold-const.cc variant nor
4909 this one as we depend on doing this transform before possibly
4910 A ? B : B -> B triggers and the fold-const.cc one can optimize
4911 0 ? A : B to B even if A has side-effects. Something
4912 genmatch cannot handle. */
4913 (simplify
4914 (cond INTEGER_CST@0 @1 @2)
4915 (if (integer_zerop (@0))
4916 (if (!VOID_TYPE_P (TREE_TYPE (@2)) || VOID_TYPE_P (type))
4917 @2)
4918 (if (!VOID_TYPE_P (TREE_TYPE (@1)) || VOID_TYPE_P (type))
4919 @1)))
4920 (simplify
4921 (vec_cond VECTOR_CST@0 @1 @2)
4922 (if (integer_all_onesp (@0))
4923 @1
4924 (if (integer_zerop (@0))
4925 @2)))
4926
4927 /* Sink unary operations to branches, but only if we do fold both. */
4928 (for op (negate bit_not abs absu)
4929 (simplify
4930 (op (vec_cond:s @0 @1 @2))
4931 (vec_cond @0 (op! @1) (op! @2))))
4932
4933 /* Sink unary conversions to branches, but only if we do fold both
4934 and the target's truth type is the same as we already have. */
4935 (simplify
4936 (convert (vec_cond:s @0 @1 @2))
4937 (if (VECTOR_TYPE_P (type)
4938 && types_match (TREE_TYPE (@0), truth_type_for (type)))
4939 (vec_cond @0 (convert! @1) (convert! @2))))
4940
4941 /* Likewise for view_convert of nop_conversions. */
4942 (simplify
4943 (view_convert (vec_cond:s @0 @1 @2))
4944 (if (VECTOR_TYPE_P (type) && VECTOR_TYPE_P (TREE_TYPE (@1))
4945 && known_eq (TYPE_VECTOR_SUBPARTS (type),
4946 TYPE_VECTOR_SUBPARTS (TREE_TYPE (@1)))
4947 && tree_nop_conversion_p (TREE_TYPE (type), TREE_TYPE (TREE_TYPE (@1))))
4948 (vec_cond @0 (view_convert! @1) (view_convert! @2))))
4949
4950 /* Sink binary operation to branches, but only if we can fold it. */
4951 (for op (tcc_comparison plus minus mult bit_and bit_ior bit_xor
4952 lshift rshift rdiv trunc_div ceil_div floor_div round_div
4953 trunc_mod ceil_mod floor_mod round_mod min max)
4954 /* (c ? a : b) op (c ? d : e) --> c ? (a op d) : (b op e) */
4955 (simplify
4956 (op (vec_cond:s @0 @1 @2) (vec_cond:s @0 @3 @4))
4957 (vec_cond @0 (op! @1 @3) (op! @2 @4)))
4958
4959 /* (c ? a : b) op d --> c ? (a op d) : (b op d) */
4960 (simplify
4961 (op (vec_cond:s @0 @1 @2) @3)
4962 (vec_cond @0 (op! @1 @3) (op! @2 @3)))
4963 (simplify
4964 (op @3 (vec_cond:s @0 @1 @2))
4965 (vec_cond @0 (op! @3 @1) (op! @3 @2))))
4966
4967 #if GIMPLE
4968 (match (nop_atomic_bit_test_and_p @0 @1 @4)
4969 (bit_and (convert?@4 (ATOMIC_FETCH_OR_XOR_N @2 INTEGER_CST@0 @3))
4970 INTEGER_CST@1)
4971 (with {
4972 int ibit = tree_log2 (@0);
4973 int ibit2 = tree_log2 (@1);
4974 }
4975 (if (ibit == ibit2
4976 && ibit >= 0
4977 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))))))
4978
4979 (match (nop_atomic_bit_test_and_p @0 @1 @3)
4980 (bit_and (convert?@3 (SYNC_FETCH_OR_XOR_N @2 INTEGER_CST@0))
4981 INTEGER_CST@1)
4982 (with {
4983 int ibit = tree_log2 (@0);
4984 int ibit2 = tree_log2 (@1);
4985 }
4986 (if (ibit == ibit2
4987 && ibit >= 0
4988 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))))))
4989
4990 (match (nop_atomic_bit_test_and_p @0 @0 @4)
4991 (bit_and:c
4992 (convert1?@4
4993 (ATOMIC_FETCH_OR_XOR_N @2 (nop_convert? (lshift@0 integer_onep@5 @6)) @3))
4994 (convert2? @0))
4995 (if (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0)))))
4996
4997 (match (nop_atomic_bit_test_and_p @0 @0 @4)
4998 (bit_and:c
4999 (convert1?@4
5000 (SYNC_FETCH_OR_XOR_N @2 (nop_convert? (lshift@0 integer_onep@3 @5))))
5001 (convert2? @0))
5002 (if (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0)))))
5003
5004 (match (nop_atomic_bit_test_and_p @0 @1 @3)
5005 (bit_and@4 (convert?@3 (ATOMIC_FETCH_AND_N @2 INTEGER_CST@0 @5))
5006 INTEGER_CST@1)
5007 (with {
5008 int ibit = wi::exact_log2 (wi::zext (wi::bit_not (wi::to_wide (@0)),
5009 TYPE_PRECISION(type)));
5010 int ibit2 = tree_log2 (@1);
5011 }
5012 (if (ibit == ibit2
5013 && ibit >= 0
5014 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))))))
5015
5016 (match (nop_atomic_bit_test_and_p @0 @1 @3)
5017 (bit_and@4
5018 (convert?@3 (SYNC_FETCH_AND_AND_N @2 INTEGER_CST@0))
5019 INTEGER_CST@1)
5020 (with {
5021 int ibit = wi::exact_log2 (wi::zext (wi::bit_not (wi::to_wide (@0)),
5022 TYPE_PRECISION(type)));
5023 int ibit2 = tree_log2 (@1);
5024 }
5025 (if (ibit == ibit2
5026 && ibit >= 0
5027 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))))))
5028
5029 (match (nop_atomic_bit_test_and_p @4 @0 @3)
5030 (bit_and:c
5031 (convert1?@3
5032 (ATOMIC_FETCH_AND_N @2 (nop_convert?@4 (bit_not (lshift@0 integer_onep@6 @7))) @5))
5033 (convert2? @0))
5034 (if (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@4)))))
5035
5036 (match (nop_atomic_bit_test_and_p @4 @0 @3)
5037 (bit_and:c
5038 (convert1?@3
5039 (SYNC_FETCH_AND_AND_N @2 (nop_convert?@4 (bit_not (lshift@0 integer_onep@6 @7)))))
5040 (convert2? @0))
5041 (if (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@4)))))
5042
5043 #endif
5044
5045 /* (v ? w : 0) ? a : b is just (v & w) ? a : b
5046 Currently disabled after pass lvec because ARM understands
5047 VEC_COND_EXPR<v==w,-1,0> but not a plain v==w fed to BIT_IOR_EXPR. */
5048 #if GIMPLE
5049 /* These can only be done in gimple as fold likes to convert:
5050 (CMP) & N into (CMP) ? N : 0
5051 and we try to match the same pattern again and again. */
5052 (simplify
5053 (vec_cond (vec_cond:s @0 @3 integer_zerop) @1 @2)
5054 (if (optimize_vectors_before_lowering_p () && types_match (@0, @3))
5055 (vec_cond (bit_and @0 @3) @1 @2)))
5056 (simplify
5057 (vec_cond (vec_cond:s @0 integer_all_onesp @3) @1 @2)
5058 (if (optimize_vectors_before_lowering_p () && types_match (@0, @3))
5059 (vec_cond (bit_ior @0 @3) @1 @2)))
5060 (simplify
5061 (vec_cond (vec_cond:s @0 integer_zerop @3) @1 @2)
5062 (if (optimize_vectors_before_lowering_p () && types_match (@0, @3))
5063 (vec_cond (bit_ior @0 (bit_not @3)) @2 @1)))
5064 (simplify
5065 (vec_cond (vec_cond:s @0 @3 integer_all_onesp) @1 @2)
5066 (if (optimize_vectors_before_lowering_p () && types_match (@0, @3))
5067 (vec_cond (bit_and @0 (bit_not @3)) @2 @1)))
5068
5069 /* c1 ? c2 ? a : b : b --> (c1 & c2) ? a : b */
5070 (simplify
5071 (vec_cond @0 (vec_cond:s @1 @2 @3) @3)
5072 (if (optimize_vectors_before_lowering_p () && types_match (@0, @1))
5073 (vec_cond (bit_and @0 @1) @2 @3)))
5074 (simplify
5075 (vec_cond @0 @2 (vec_cond:s @1 @2 @3))
5076 (if (optimize_vectors_before_lowering_p () && types_match (@0, @1))
5077 (vec_cond (bit_ior @0 @1) @2 @3)))
5078 (simplify
5079 (vec_cond @0 (vec_cond:s @1 @2 @3) @2)
5080 (if (optimize_vectors_before_lowering_p () && types_match (@0, @1))
5081 (vec_cond (bit_ior (bit_not @0) @1) @2 @3)))
5082 (simplify
5083 (vec_cond @0 @3 (vec_cond:s @1 @2 @3))
5084 (if (optimize_vectors_before_lowering_p () && types_match (@0, @1))
5085 (vec_cond (bit_and (bit_not @0) @1) @2 @3)))
5086 #endif
5087
5088 /* Canonicalize mask ? { 0, ... } : { -1, ...} to ~mask if the mask
5089 types are compatible. */
5090 (simplify
5091 (vec_cond @0 VECTOR_CST@1 VECTOR_CST@2)
5092 (if (VECTOR_BOOLEAN_TYPE_P (type)
5093 && types_match (type, TREE_TYPE (@0)))
5094 (if (integer_zerop (@1) && integer_all_onesp (@2))
5095 (bit_not @0)
5096 (if (integer_all_onesp (@1) && integer_zerop (@2))
5097 @0))))
5098
5099 /* A few simplifications of "a ? CST1 : CST2". */
5100 /* NOTE: Only do this on gimple as the if-chain-to-switch
5101 optimization depends on the gimple to have if statements in it. */
5102 #if GIMPLE
5103 (simplify
5104 (cond @0 INTEGER_CST@1 INTEGER_CST@2)
5105 (switch
5106 (if (integer_zerop (@2))
5107 (switch
5108 /* a ? 1 : 0 -> a if 0 and 1 are integral types. */
5109 (if (integer_onep (@1))
5110 (convert (convert:boolean_type_node @0)))
5111 /* a ? -1 : 0 -> -a. */
5112 (if (INTEGRAL_TYPE_P (type) && integer_all_onesp (@1))
5113 (if (TYPE_PRECISION (type) == 1)
5114 /* For signed 1-bit precision just cast bool to the type. */
5115 (convert (convert:boolean_type_node @0))
5116 (if (TREE_CODE (type) == BOOLEAN_TYPE)
5117 (with {
5118 tree intt = build_nonstandard_integer_type (TYPE_PRECISION (type),
5119 TYPE_UNSIGNED (type));
5120 }
5121 (convert (negate (convert:intt (convert:boolean_type_node @0)))))
5122 (negate (convert:type (convert:boolean_type_node @0))))))
5123 /* a ? powerof2cst : 0 -> a << (log2(powerof2cst)) */
5124 (if (INTEGRAL_TYPE_P (type) && integer_pow2p (@1))
5125 (with {
5126 tree shift = build_int_cst (integer_type_node, tree_log2 (@1));
5127 }
5128 (lshift (convert (convert:boolean_type_node @0)) { shift; })))))
5129 (if (integer_zerop (@1))
5130 (switch
5131 /* a ? 0 : 1 -> !a. */
5132 (if (integer_onep (@2))
5133 (convert (bit_xor (convert:boolean_type_node @0) { boolean_true_node; })))
5134 /* a ? 0 : -1 -> -(!a). */
5135 (if (INTEGRAL_TYPE_P (type) && integer_all_onesp (@2))
5136 (if (TYPE_PRECISION (type) == 1)
5137 /* For signed 1-bit precision just cast bool to the type. */
5138 (convert (bit_xor (convert:boolean_type_node @0) { boolean_true_node; }))
5139 (if (TREE_CODE (type) == BOOLEAN_TYPE)
5140 (with {
5141 tree intt = build_nonstandard_integer_type (TYPE_PRECISION (type),
5142 TYPE_UNSIGNED (type));
5143 }
5144 (convert (negate (convert:intt (bit_xor (convert:boolean_type_node @0)
5145 { boolean_true_node; })))))
5146 (negate (convert:type (bit_xor (convert:boolean_type_node @0)
5147 { boolean_true_node; }))))))
5148 /* a ? 0 : powerof2cst -> (!a) << (log2(powerof2cst)) */
5149 (if (INTEGRAL_TYPE_P (type) && integer_pow2p (@2))
5150 (with {
5151 tree shift = build_int_cst (integer_type_node, tree_log2 (@2));
5152 }
5153 (lshift (convert (bit_xor (convert:boolean_type_node @0)
5154 { boolean_true_node; })) { shift; })))))))
5155
5156 /* (a > 1) ? 0 : (cast)a is the same as (cast)(a == 1)
5157 for unsigned types. */
5158 (simplify
5159 (cond (gt @0 integer_onep@1) integer_zerop (convert? @2))
5160 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
5161 && bitwise_equal_p (@0, @2))
5162 (convert (eq @0 @1))
5163 )
5164 )
5165
5166 /* (a <= 1) & (cast)a is the same as (cast)(a == 1)
5167 for unsigned types. */
5168 (simplify
5169 (bit_and:c (convert1? (le @0 integer_onep@1)) (convert2? @2))
5170 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
5171 && bitwise_equal_p (@0, @2))
5172 (convert (eq @0 @1))
5173 )
5174 )
5175
5176 /* `(a == CST) & a` can be simplified to `0` or `(a == CST)` depending
5177 on the first bit of the CST. */
5178 (simplify
5179 (bit_and:c (convert@2 (eq @0 INTEGER_CST@1)) (convert? @0))
5180 (if ((wi::to_wide (@1) & 1) != 0)
5181 @2
5182 { build_zero_cst (type); }))
5183
5184 /* Optimize
5185 # x_5 in range [cst1, cst2] where cst2 = cst1 + 1
5186 x_5 == cstN ? cst4 : cst3
5187 # op is == or != and N is 1 or 2
5188 to r_6 = x_5 + (min (cst3, cst4) - cst1) or
5189 r_6 = (min (cst3, cst4) + cst1) - x_5 depending on op, N and which
5190 of cst3 and cst4 is smaller.
5191 This was originally done by two_value_replacement in phiopt (PR 88676). */
5192 (for eqne (ne eq)
5193 (simplify
5194 (cond (eqne SSA_NAME@0 INTEGER_CST@1) INTEGER_CST@2 INTEGER_CST@3)
5195 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
5196 && INTEGRAL_TYPE_P (type)
5197 && (wi::to_widest (@2) + 1 == wi::to_widest (@3)
5198 || wi::to_widest (@2) == wi::to_widest (@3) + 1))
5199 (with {
5200 value_range r;
5201 get_range_query (cfun)->range_of_expr (r, @0);
5202 if (r.undefined_p ())
5203 r.set_varying (TREE_TYPE (@0));
5204
5205 wide_int min = r.lower_bound ();
5206 wide_int max = r.upper_bound ();
5207 }
5208 (if (min + 1 == max
5209 && (wi::to_wide (@1) == min
5210 || wi::to_wide (@1) == max))
5211 (with {
5212 tree arg0 = @2, arg1 = @3;
5213 tree type1;
5214 if ((eqne == EQ_EXPR) ^ (wi::to_wide (@1) == min))
5215 std::swap (arg0, arg1);
5216 if (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type))
5217 type1 = TREE_TYPE (@0);
5218 else
5219 type1 = type;
5220 auto prec = TYPE_PRECISION (type1);
5221 auto unsign = TYPE_UNSIGNED (type1);
5222 if (TREE_CODE (type1) == BOOLEAN_TYPE)
5223 type1 = build_nonstandard_integer_type (prec, unsign);
5224 min = wide_int::from (min, prec,
5225 TYPE_SIGN (TREE_TYPE (@0)));
5226 wide_int a = wide_int::from (wi::to_wide (arg0), prec,
5227 TYPE_SIGN (type));
5228 enum tree_code code;
5229 wi::overflow_type ovf;
5230 if (tree_int_cst_lt (arg0, arg1))
5231 {
5232 code = PLUS_EXPR;
5233 a -= min;
5234 if (!unsign)
5235 {
5236 /* lhs is known to be in range [min, min+1] and we want to add a
5237 to it. Check if that operation can overflow for those 2 values
5238 and if yes, force unsigned type. */
5239 wi::add (min + (wi::neg_p (a) ? 0 : 1), a, SIGNED, &ovf);
5240 if (ovf)
5241 type1 = unsigned_type_for (type1);
5242 }
5243 }
5244 else
5245 {
5246 code = MINUS_EXPR;
5247 a += min;
5248 if (!unsign)
5249 {
5250 /* lhs is known to be in range [min, min+1] and we want to subtract
5251 it from a. Check if that operation can overflow for those 2
5252 values and if yes, force unsigned type. */
5253 wi::sub (a, min + (wi::neg_p (min) ? 0 : 1), SIGNED, &ovf);
5254 if (ovf)
5255 type1 = unsigned_type_for (type1);
5256 }
5257 }
5258 tree arg = wide_int_to_tree (type1, a);
5259 }
5260 (if (code == PLUS_EXPR)
5261 (convert (plus (convert:type1 @0) { arg; }))
5262 (convert (minus { arg; } (convert:type1 @0))))))))))
5263 #endif
5264
5265 (simplify
5266 (convert (cond@0 @1 INTEGER_CST@2 INTEGER_CST@3))
5267 (if (INTEGRAL_TYPE_P (type)
5268 && INTEGRAL_TYPE_P (TREE_TYPE (@0)))
5269 (cond @1 (convert @2) (convert @3))))
5270
5271 /* Simplification moved from fold_cond_expr_with_comparison. It may also
5272 be extended. */
5273 /* This pattern implements two kinds simplification:
5274
5275 Case 1)
5276 (cond (cmp (convert1? x) c1) (convert2? x) c2) -> (minmax (x c)) if:
5277 1) Conversions are type widening from smaller type.
5278 2) Const c1 equals to c2 after canonicalizing comparison.
5279 3) Comparison has tree code LT, LE, GT or GE.
5280 This specific pattern is needed when (cmp (convert x) c) may not
5281 be simplified by comparison patterns because of multiple uses of
5282 x. It also makes sense here because simplifying across multiple
5283 referred var is always benefitial for complicated cases.
5284
5285 Case 2)
5286 (cond (eq (convert1? x) c1) (convert2? x) c2) -> (cond (eq x c1) c1 c2). */
5287 (for cmp (lt le gt ge eq ne)
5288 (simplify
5289 (cond (cmp (convert1? @1) INTEGER_CST@3) (convert2? @1) INTEGER_CST@2)
5290 (with
5291 {
5292 tree from_type = TREE_TYPE (@1);
5293 tree c1_type = TREE_TYPE (@3), c2_type = TREE_TYPE (@2);
5294 enum tree_code code = ERROR_MARK;
5295
5296 if (INTEGRAL_TYPE_P (from_type)
5297 && int_fits_type_p (@2, from_type)
5298 && (types_match (c1_type, from_type)
5299 || (TYPE_PRECISION (c1_type) > TYPE_PRECISION (from_type)
5300 && (TYPE_UNSIGNED (from_type)
5301 || TYPE_SIGN (c1_type) == TYPE_SIGN (from_type))))
5302 && (types_match (c2_type, from_type)
5303 || (TYPE_PRECISION (c2_type) > TYPE_PRECISION (from_type)
5304 && (TYPE_UNSIGNED (from_type)
5305 || TYPE_SIGN (c2_type) == TYPE_SIGN (from_type)))))
5306 {
5307 if (cmp != EQ_EXPR)
5308 code = minmax_from_comparison (cmp, @1, @3, @1, @2);
5309 /* Can do A == C1 ? A : C2 -> A == C1 ? C1 : C2? */
5310 else if (int_fits_type_p (@3, from_type))
5311 code = EQ_EXPR;
5312 }
5313 }
5314 (if (code == MAX_EXPR)
5315 (convert (max @1 (convert @2)))
5316 (if (code == MIN_EXPR)
5317 (convert (min @1 (convert @2)))
5318 (if (code == EQ_EXPR)
5319 (convert (cond (eq @1 (convert @3))
5320 (convert:from_type @3) (convert:from_type @2)))))))))
5321
5322 /* (cond (cmp (convert? x) c1) (op x c2) c3) -> (op (minmax x c1) c2) if:
5323
5324 1) OP is PLUS or MINUS.
5325 2) CMP is LT, LE, GT or GE.
5326 3) C3 == (C1 op C2), and computation doesn't have undefined behavior.
5327
5328 This pattern also handles special cases like:
5329
5330 A) Operand x is a unsigned to signed type conversion and c1 is
5331 integer zero. In this case,
5332 (signed type)x < 0 <=> x > MAX_VAL(signed type)
5333 (signed type)x >= 0 <=> x <= MAX_VAL(signed type)
5334 B) Const c1 may not equal to (C3 op' C2). In this case we also
5335 check equality for (c1+1) and (c1-1) by adjusting comparison
5336 code.
5337
5338 TODO: Though signed type is handled by this pattern, it cannot be
5339 simplified at the moment because C standard requires additional
5340 type promotion. In order to match&simplify it here, the IR needs
5341 to be cleaned up by other optimizers, i.e, VRP. */
5342 (for op (plus minus)
5343 (for cmp (lt le gt ge)
5344 (simplify
5345 (cond (cmp (convert? @X) INTEGER_CST@1) (op @X INTEGER_CST@2) INTEGER_CST@3)
5346 (with { tree from_type = TREE_TYPE (@X), to_type = TREE_TYPE (@1); }
5347 (if (types_match (from_type, to_type)
5348 /* Check if it is special case A). */
5349 || (TYPE_UNSIGNED (from_type)
5350 && !TYPE_UNSIGNED (to_type)
5351 && TYPE_PRECISION (from_type) == TYPE_PRECISION (to_type)
5352 && integer_zerop (@1)
5353 && (cmp == LT_EXPR || cmp == GE_EXPR)))
5354 (with
5355 {
5356 wi::overflow_type overflow = wi::OVF_NONE;
5357 enum tree_code code, cmp_code = cmp;
5358 wide_int real_c1;
5359 wide_int c1 = wi::to_wide (@1);
5360 wide_int c2 = wi::to_wide (@2);
5361 wide_int c3 = wi::to_wide (@3);
5362 signop sgn = TYPE_SIGN (from_type);
5363
5364 /* Handle special case A), given x of unsigned type:
5365 ((signed type)x < 0) <=> (x > MAX_VAL(signed type))
5366 ((signed type)x >= 0) <=> (x <= MAX_VAL(signed type)) */
5367 if (!types_match (from_type, to_type))
5368 {
5369 if (cmp_code == LT_EXPR)
5370 cmp_code = GT_EXPR;
5371 if (cmp_code == GE_EXPR)
5372 cmp_code = LE_EXPR;
5373 c1 = wi::max_value (to_type);
5374 }
5375 /* To simplify this pattern, we require c3 = (c1 op c2). Here we
5376 compute (c3 op' c2) and check if it equals to c1 with op' being
5377 the inverted operator of op. Make sure overflow doesn't happen
5378 if it is undefined. */
5379 if (op == PLUS_EXPR)
5380 real_c1 = wi::sub (c3, c2, sgn, &overflow);
5381 else
5382 real_c1 = wi::add (c3, c2, sgn, &overflow);
5383
5384 code = cmp_code;
5385 if (!overflow || !TYPE_OVERFLOW_UNDEFINED (from_type))
5386 {
5387 /* Check if c1 equals to real_c1. Boundary condition is handled
5388 by adjusting comparison operation if necessary. */
5389 if (!wi::cmp (wi::sub (real_c1, 1, sgn, &overflow), c1, sgn)
5390 && !overflow)
5391 {
5392 /* X <= Y - 1 equals to X < Y. */
5393 if (cmp_code == LE_EXPR)
5394 code = LT_EXPR;
5395 /* X > Y - 1 equals to X >= Y. */
5396 if (cmp_code == GT_EXPR)
5397 code = GE_EXPR;
5398 }
5399 if (!wi::cmp (wi::add (real_c1, 1, sgn, &overflow), c1, sgn)
5400 && !overflow)
5401 {
5402 /* X < Y + 1 equals to X <= Y. */
5403 if (cmp_code == LT_EXPR)
5404 code = LE_EXPR;
5405 /* X >= Y + 1 equals to X > Y. */
5406 if (cmp_code == GE_EXPR)
5407 code = GT_EXPR;
5408 }
5409 if (code != cmp_code || !wi::cmp (real_c1, c1, sgn))
5410 {
5411 if (cmp_code == LT_EXPR || cmp_code == LE_EXPR)
5412 code = MIN_EXPR;
5413 if (cmp_code == GT_EXPR || cmp_code == GE_EXPR)
5414 code = MAX_EXPR;
5415 }
5416 }
5417 }
5418 (if (code == MAX_EXPR)
5419 (op (max @X { wide_int_to_tree (from_type, real_c1); })
5420 { wide_int_to_tree (from_type, c2); })
5421 (if (code == MIN_EXPR)
5422 (op (min @X { wide_int_to_tree (from_type, real_c1); })
5423 { wide_int_to_tree (from_type, c2); })))))))))
5424
5425 #if GIMPLE
5426 /* A >= B ? A : B -> max (A, B) and friends. The code is still
5427 in fold_cond_expr_with_comparison for GENERIC folding with
5428 some extra constraints. */
5429 (for cmp (eq ne le lt unle unlt ge gt unge ungt uneq ltgt)
5430 (simplify
5431 (cond (cmp:c (nop_convert1?@c0 @0) (nop_convert2?@c1 @1))
5432 (convert3? @0) (convert4? @1))
5433 (if (!HONOR_SIGNED_ZEROS (type)
5434 && (/* Allow widening conversions of the compare operands as data. */
5435 (INTEGRAL_TYPE_P (type)
5436 && types_match (TREE_TYPE (@c0), TREE_TYPE (@0))
5437 && types_match (TREE_TYPE (@c1), TREE_TYPE (@1))
5438 && TYPE_PRECISION (TREE_TYPE (@0)) <= TYPE_PRECISION (type)
5439 && TYPE_PRECISION (TREE_TYPE (@1)) <= TYPE_PRECISION (type))
5440 /* Or sign conversions for the comparison. */
5441 || (types_match (type, TREE_TYPE (@0))
5442 && types_match (type, TREE_TYPE (@1)))))
5443 (switch
5444 (if (cmp == EQ_EXPR)
5445 (if (VECTOR_TYPE_P (type))
5446 (view_convert @c1)
5447 (convert @c1)))
5448 (if (cmp == NE_EXPR)
5449 (if (VECTOR_TYPE_P (type))
5450 (view_convert @c0)
5451 (convert @c0)))
5452 (if (cmp == LE_EXPR || cmp == UNLE_EXPR || cmp == LT_EXPR || cmp == UNLT_EXPR)
5453 (if (!HONOR_NANS (type))
5454 (if (VECTOR_TYPE_P (type))
5455 (view_convert (min @c0 @c1))
5456 (convert (min @c0 @c1)))))
5457 (if (cmp == GE_EXPR || cmp == UNGE_EXPR || cmp == GT_EXPR || cmp == UNGT_EXPR)
5458 (if (!HONOR_NANS (type))
5459 (if (VECTOR_TYPE_P (type))
5460 (view_convert (max @c0 @c1))
5461 (convert (max @c0 @c1)))))
5462 (if (cmp == UNEQ_EXPR)
5463 (if (!HONOR_NANS (type))
5464 (if (VECTOR_TYPE_P (type))
5465 (view_convert @c1)
5466 (convert @c1))))
5467 (if (cmp == LTGT_EXPR)
5468 (if (!HONOR_NANS (type))
5469 (if (VECTOR_TYPE_P (type))
5470 (view_convert @c0)
5471 (convert @c0))))))))
5472 #endif
5473
5474 (for cnd (cond vec_cond)
5475 /* (a != b) ? (a - b) : 0 -> (a - b) */
5476 (simplify
5477 (cnd (ne:c @0 @1) (minus@2 @0 @1) integer_zerop)
5478 @2)
5479 /* (a != b) ? (a ^ b) : 0 -> (a ^ b) */
5480 (simplify
5481 (cnd (ne:c @0 @1) (bit_xor:c@2 @0 @1) integer_zerop)
5482 @2)
5483 /* (a != b) ? (a & b) : a -> (a & b) */
5484 /* (a != b) ? (a | b) : a -> (a | b) */
5485 /* (a != b) ? min(a,b) : a -> min(a,b) */
5486 /* (a != b) ? max(a,b) : a -> max(a,b) */
5487 (for op (bit_and bit_ior min max)
5488 (simplify
5489 (cnd (ne:c @0 @1) (op:c@2 @0 @1) @0)
5490 @2))
5491 /* (a != b) ? (a * b) : (a * a) -> (a * b) */
5492 /* (a != b) ? (a + b) : (a + a) -> (a + b) */
5493 (for op (mult plus)
5494 (simplify
5495 (cnd (ne:c @0 @1) (op@2 @0 @1) (op @0 @0))
5496 (if (ANY_INTEGRAL_TYPE_P (type))
5497 @2)))
5498 /* (a != b) ? (a + b) : (2 * a) -> (a + b) */
5499 (simplify
5500 (cnd (ne:c @0 @1) (plus@2 @0 @1) (mult @0 uniform_integer_cst_p@3))
5501 (if (wi::to_wide (uniform_integer_cst_p (@3)) == 2)
5502 @2))
5503 )
5504
5505 /* These was part of minmax phiopt. */
5506 /* Optimize (a CMP b) ? minmax<a, c> : minmax<b, c>
5507 to minmax<min/max<a, b>, c> */
5508 (for minmax (min max)
5509 (for cmp (lt le gt ge ne)
5510 (simplify
5511 (cond (cmp:c @1 @3) (minmax:c @1 @4) (minmax:c @2 @4))
5512 (with
5513 {
5514 tree_code code = minmax_from_comparison (cmp, @1, @2, @1, @3);
5515 }
5516 (if (code == MIN_EXPR)
5517 (minmax (min @1 @2) @4)
5518 (if (code == MAX_EXPR)
5519 (minmax (max @1 @2) @4)))))))
5520
5521 /* Optimize (a CMP CST1) ? max<a,CST2> : a */
5522 (for cmp (gt ge lt le)
5523 minmax (min min max max)
5524 (simplify
5525 (cond (cmp:c @0 @1) (minmax:c@2 @0 @3) @4)
5526 (with
5527 {
5528 tree_code code = minmax_from_comparison (cmp, @0, @1, @0, @4);
5529 }
5530 (if ((cmp == LT_EXPR || cmp == LE_EXPR)
5531 && code == MIN_EXPR
5532 && integer_nonzerop (fold_build2 (LE_EXPR, boolean_type_node, @3, @4)))
5533 (min @2 @4)
5534 (if ((cmp == GT_EXPR || cmp == GE_EXPR)
5535 && code == MAX_EXPR
5536 && integer_nonzerop (fold_build2 (GE_EXPR, boolean_type_node, @3, @4)))
5537 (max @2 @4))))))
5538
5539 #if GIMPLE
5540 /* These patterns should be after min/max detection as simplifications
5541 of `(type)(zero_one ==/!= 0)` to `(type)(zero_one)`
5542 and `(type)(zero_one^1)` are not done yet. See PR 110637.
5543 Even without those, reaching min/max/and/ior faster is better. */
5544 (simplify
5545 (cond @0 zero_one_valued_p@1 zero_one_valued_p@2)
5546 (switch
5547 /* bool0 ? bool1 : 0 -> bool0 & bool1 */
5548 (if (integer_zerop (@2))
5549 (bit_and (convert @0) @1))
5550 /* bool0 ? 0 : bool2 -> (bool0^1) & bool2 */
5551 (if (integer_zerop (@1))
5552 (bit_and (bit_xor (convert @0) { build_one_cst (type); } ) @2))
5553 /* bool0 ? 1 : bool2 -> bool0 | bool2 */
5554 (if (integer_onep (@1))
5555 (bit_ior (convert @0) @2))
5556 /* bool0 ? bool1 : 1 -> (bool0^1) | bool1 */
5557 (if (integer_onep (@2))
5558 (bit_ior (bit_xor (convert @0) @2) @1))
5559 )
5560 )
5561 #endif
5562
5563 /* X != C1 ? -X : C2 simplifies to -X when -C1 == C2. */
5564 (simplify
5565 (cond (ne @0 INTEGER_CST@1) (negate@3 @0) INTEGER_CST@2)
5566 (if (!TYPE_SATURATING (type)
5567 && (TYPE_OVERFLOW_WRAPS (type)
5568 || !wi::only_sign_bit_p (wi::to_wide (@1)))
5569 && wi::eq_p (wi::neg (wi::to_wide (@1)), wi::to_wide (@2)))
5570 @3))
5571
5572 /* X != C1 ? ~X : C2 simplifies to ~X when ~C1 == C2. */
5573 (simplify
5574 (cond (ne @0 INTEGER_CST@1) (bit_not@3 @0) INTEGER_CST@2)
5575 (if (wi::eq_p (wi::bit_not (wi::to_wide (@1)), wi::to_wide (@2)))
5576 @3))
5577
5578 /* (X + 1) > Y ? -X : 1 simplifies to X >= Y ? -X : 1 when
5579 X is unsigned, as when X + 1 overflows, X is -1, so -X == 1. */
5580 (simplify
5581 (cond (gt (plus @0 integer_onep) @1) (negate @0) integer_onep@2)
5582 (if (TYPE_UNSIGNED (type))
5583 (cond (ge @0 @1) (negate @0) @2)))
5584
5585 (for cnd (cond vec_cond)
5586 /* A ? B : (A ? X : C) -> A ? B : C. */
5587 (simplify
5588 (cnd @0 (cnd @0 @1 @2) @3)
5589 (cnd @0 @1 @3))
5590 (simplify
5591 (cnd @0 @1 (cnd @0 @2 @3))
5592 (cnd @0 @1 @3))
5593 /* A ? B : (!A ? C : X) -> A ? B : C. */
5594 /* ??? This matches embedded conditions open-coded because genmatch
5595 would generate matching code for conditions in separate stmts only.
5596 The following is still important to merge then and else arm cases
5597 from if-conversion. */
5598 (simplify
5599 (cnd @0 @1 (cnd @2 @3 @4))
5600 (if (inverse_conditions_p (@0, @2))
5601 (cnd @0 @1 @3)))
5602 (simplify
5603 (cnd @0 (cnd @1 @2 @3) @4)
5604 (if (inverse_conditions_p (@0, @1))
5605 (cnd @0 @3 @4)))
5606
5607 /* A ? B : B -> B. */
5608 (simplify
5609 (cnd @0 @1 @1)
5610 @1)
5611
5612 /* !A ? B : C -> A ? C : B. */
5613 (simplify
5614 (cnd (logical_inverted_value truth_valued_p@0) @1 @2)
5615 (cnd @0 @2 @1)))
5616
5617 /* abs/negative simplifications moved from fold_cond_expr_with_comparison,
5618 Need to handle (A - B) case as fold_cond_expr_with_comparison does.
5619 Need to handle UN* comparisons.
5620
5621 None of these transformations work for modes with signed
5622 zeros. If A is +/-0, the first two transformations will
5623 change the sign of the result (from +0 to -0, or vice
5624 versa). The last four will fix the sign of the result,
5625 even though the original expressions could be positive or
5626 negative, depending on the sign of A.
5627
5628 Note that all these transformations are correct if A is
5629 NaN, since the two alternatives (A and -A) are also NaNs. */
5630
5631 (for cnd (cond vec_cond)
5632 /* A == 0 ? A : -A same as -A */
5633 (for cmp (eq uneq)
5634 (simplify
5635 (cnd (cmp @0 zerop) @0 (negate@1 @0))
5636 (if (!HONOR_SIGNED_ZEROS (type))
5637 @1))
5638 (simplify
5639 (cnd (cmp @0 zerop) zerop (negate@1 @0))
5640 (if (!HONOR_SIGNED_ZEROS (type))
5641 @1))
5642 )
5643 /* A != 0 ? A : -A same as A */
5644 (for cmp (ne ltgt)
5645 (simplify
5646 (cnd (cmp @0 zerop) @0 (negate @0))
5647 (if (!HONOR_SIGNED_ZEROS (type))
5648 @0))
5649 (simplify
5650 (cnd (cmp @0 zerop) @0 integer_zerop)
5651 (if (!HONOR_SIGNED_ZEROS (type))
5652 @0))
5653 )
5654 /* A >=/> 0 ? A : -A same as abs (A) */
5655 (for cmp (ge gt)
5656 (simplify
5657 (cnd (cmp @0 zerop) @0 (negate @0))
5658 (if (!HONOR_SIGNED_ZEROS (type)
5659 && !TYPE_UNSIGNED (type))
5660 (abs @0))))
5661 /* A <=/< 0 ? A : -A same as -abs (A) */
5662 (for cmp (le lt)
5663 (simplify
5664 (cnd (cmp @0 zerop) @0 (negate @0))
5665 (if (!HONOR_SIGNED_ZEROS (type)
5666 && !TYPE_UNSIGNED (type))
5667 (if (ANY_INTEGRAL_TYPE_P (type)
5668 && !TYPE_OVERFLOW_WRAPS (type))
5669 (with {
5670 tree utype = unsigned_type_for (type);
5671 }
5672 (convert (negate (absu:utype @0))))
5673 (negate (abs @0)))))
5674 )
5675 )
5676
5677 /* -(type)!A -> (type)A - 1. */
5678 (simplify
5679 (negate (convert?:s (logical_inverted_value:s @0)))
5680 (if (INTEGRAL_TYPE_P (type)
5681 && TREE_CODE (type) != BOOLEAN_TYPE
5682 && TYPE_PRECISION (type) > 1
5683 && TREE_CODE (@0) == SSA_NAME
5684 && ssa_name_has_boolean_range (@0))
5685 (plus (convert:type @0) { build_all_ones_cst (type); })))
5686
5687 /* A + (B vcmp C ? 1 : 0) -> A - (B vcmp C ? -1 : 0), since vector comparisons
5688 return all -1 or all 0 results. */
5689 /* ??? We could instead convert all instances of the vec_cond to negate,
5690 but that isn't necessarily a win on its own. */
5691 (simplify
5692 (plus:c @3 (view_convert? (vec_cond:s @0 integer_each_onep@1 integer_zerop@2)))
5693 (if (VECTOR_TYPE_P (type)
5694 && known_eq (TYPE_VECTOR_SUBPARTS (type),
5695 TYPE_VECTOR_SUBPARTS (TREE_TYPE (@1)))
5696 && (TYPE_MODE (TREE_TYPE (type))
5697 == TYPE_MODE (TREE_TYPE (TREE_TYPE (@1)))))
5698 (minus @3 (view_convert (vec_cond @0 (negate @1) @2)))))
5699
5700 /* ... likewise A - (B vcmp C ? 1 : 0) -> A + (B vcmp C ? -1 : 0). */
5701 (simplify
5702 (minus @3 (view_convert? (vec_cond:s @0 integer_each_onep@1 integer_zerop@2)))
5703 (if (VECTOR_TYPE_P (type)
5704 && known_eq (TYPE_VECTOR_SUBPARTS (type),
5705 TYPE_VECTOR_SUBPARTS (TREE_TYPE (@1)))
5706 && (TYPE_MODE (TREE_TYPE (type))
5707 == TYPE_MODE (TREE_TYPE (TREE_TYPE (@1)))))
5708 (plus @3 (view_convert (vec_cond @0 (negate @1) @2)))))
5709
5710
5711 /* Simplifications of comparisons. */
5712
5713 /* See if we can reduce the magnitude of a constant involved in a
5714 comparison by changing the comparison code. This is a canonicalization
5715 formerly done by maybe_canonicalize_comparison_1. */
5716 (for cmp (le gt)
5717 acmp (lt ge)
5718 (simplify
5719 (cmp @0 uniform_integer_cst_p@1)
5720 (with { tree cst = uniform_integer_cst_p (@1); }
5721 (if (tree_int_cst_sgn (cst) == -1)
5722 (acmp @0 { build_uniform_cst (TREE_TYPE (@1),
5723 wide_int_to_tree (TREE_TYPE (cst),
5724 wi::to_wide (cst)
5725 + 1)); })))))
5726 (for cmp (ge lt)
5727 acmp (gt le)
5728 (simplify
5729 (cmp @0 uniform_integer_cst_p@1)
5730 (with { tree cst = uniform_integer_cst_p (@1); }
5731 (if (tree_int_cst_sgn (cst) == 1)
5732 (acmp @0 { build_uniform_cst (TREE_TYPE (@1),
5733 wide_int_to_tree (TREE_TYPE (cst),
5734 wi::to_wide (cst) - 1)); })))))
5735
5736 /* We can simplify a logical negation of a comparison to the
5737 inverted comparison. As we cannot compute an expression
5738 operator using invert_tree_comparison we have to simulate
5739 that with expression code iteration. */
5740 (for cmp (tcc_comparison)
5741 icmp (inverted_tcc_comparison)
5742 ncmp (inverted_tcc_comparison_with_nans)
5743 /* Ideally we'd like to combine the following two patterns
5744 and handle some more cases by using
5745 (logical_inverted_value (cmp @0 @1))
5746 here but for that genmatch would need to "inline" that.
5747 For now implement what forward_propagate_comparison did. */
5748 (simplify
5749 (bit_not (cmp @0 @1))
5750 (if (VECTOR_TYPE_P (type)
5751 || (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1))
5752 /* Comparison inversion may be impossible for trapping math,
5753 invert_tree_comparison will tell us. But we can't use
5754 a computed operator in the replacement tree thus we have
5755 to play the trick below. */
5756 (with { enum tree_code ic = invert_tree_comparison
5757 (cmp, HONOR_NANS (@0)); }
5758 (if (ic == icmp)
5759 (icmp @0 @1)
5760 (if (ic == ncmp)
5761 (ncmp @0 @1))))))
5762 (simplify
5763 (bit_xor (cmp @0 @1) integer_truep)
5764 (with { enum tree_code ic = invert_tree_comparison
5765 (cmp, HONOR_NANS (@0)); }
5766 (if (ic == icmp)
5767 (icmp @0 @1)
5768 (if (ic == ncmp)
5769 (ncmp @0 @1)))))
5770 /* The following bits are handled by fold_binary_op_with_conditional_arg. */
5771 (simplify
5772 (ne (cmp@2 @0 @1) integer_zerop)
5773 (if (types_match (type, TREE_TYPE (@2)))
5774 (cmp @0 @1)))
5775 (simplify
5776 (eq (cmp@2 @0 @1) integer_truep)
5777 (if (types_match (type, TREE_TYPE (@2)))
5778 (cmp @0 @1)))
5779 (simplify
5780 (ne (cmp@2 @0 @1) integer_truep)
5781 (if (types_match (type, TREE_TYPE (@2)))
5782 (with { enum tree_code ic = invert_tree_comparison
5783 (cmp, HONOR_NANS (@0)); }
5784 (if (ic == icmp)
5785 (icmp @0 @1)
5786 (if (ic == ncmp)
5787 (ncmp @0 @1))))))
5788 (simplify
5789 (eq (cmp@2 @0 @1) integer_zerop)
5790 (if (types_match (type, TREE_TYPE (@2)))
5791 (with { enum tree_code ic = invert_tree_comparison
5792 (cmp, HONOR_NANS (@0)); }
5793 (if (ic == icmp)
5794 (icmp @0 @1)
5795 (if (ic == ncmp)
5796 (ncmp @0 @1)))))))
5797
5798 /* Transform comparisons of the form X - Y CMP 0 to X CMP Y.
5799 ??? The transformation is valid for the other operators if overflow
5800 is undefined for the type, but performing it here badly interacts
5801 with the transformation in fold_cond_expr_with_comparison which
5802 attempts to synthetize ABS_EXPR. */
5803 (for cmp (eq ne)
5804 (for sub (minus pointer_diff)
5805 (simplify
5806 (cmp (sub@2 @0 @1) integer_zerop)
5807 (if (single_use (@2))
5808 (cmp @0 @1)))))
5809
5810 /* Simplify (x < 0) ^ (y < 0) to (x ^ y) < 0 and
5811 (x >= 0) ^ (y >= 0) to (x ^ y) < 0. */
5812 (for cmp (lt ge)
5813 (simplify
5814 (bit_xor (cmp:s @0 integer_zerop) (cmp:s @1 integer_zerop))
5815 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
5816 && !TYPE_UNSIGNED (TREE_TYPE (@0))
5817 && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
5818 (lt (bit_xor @0 @1) { build_zero_cst (TREE_TYPE (@0)); }))))
5819 /* Simplify (x < 0) ^ (y >= 0) to (x ^ y) >= 0 and
5820 (x >= 0) ^ (y < 0) to (x ^ y) >= 0. */
5821 (simplify
5822 (bit_xor:c (lt:s @0 integer_zerop) (ge:s @1 integer_zerop))
5823 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
5824 && !TYPE_UNSIGNED (TREE_TYPE (@0))
5825 && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
5826 (ge (bit_xor @0 @1) { build_zero_cst (TREE_TYPE (@0)); })))
5827
5828 /* Transform comparisons of the form X * C1 CMP 0 to X CMP 0 in the
5829 signed arithmetic case. That form is created by the compiler
5830 often enough for folding it to be of value. One example is in
5831 computing loop trip counts after Operator Strength Reduction. */
5832 (for cmp (simple_comparison)
5833 scmp (swapped_simple_comparison)
5834 (simplify
5835 (cmp (mult@3 @0 INTEGER_CST@1) integer_zerop@2)
5836 /* Handle unfolded multiplication by zero. */
5837 (if (integer_zerop (@1))
5838 (cmp @1 @2)
5839 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
5840 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
5841 && single_use (@3))
5842 /* If @1 is negative we swap the sense of the comparison. */
5843 (if (tree_int_cst_sgn (@1) < 0)
5844 (scmp @0 @2)
5845 (cmp @0 @2))))))
5846
5847 /* For integral types with undefined overflow fold
5848 x * C1 == C2 into x == C2 / C1 or false.
5849 If overflow wraps and C1 is odd, simplify to x == C2 / C1 in the ring
5850 Z / 2^n Z. */
5851 (for cmp (eq ne)
5852 (simplify
5853 (cmp (mult @0 INTEGER_CST@1) INTEGER_CST@2)
5854 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
5855 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
5856 && wi::to_wide (@1) != 0)
5857 (with { widest_int quot; }
5858 (if (wi::multiple_of_p (wi::to_widest (@2), wi::to_widest (@1),
5859 TYPE_SIGN (TREE_TYPE (@0)), &quot))
5860 (cmp @0 { wide_int_to_tree (TREE_TYPE (@0), quot); })
5861 { constant_boolean_node (cmp == NE_EXPR, type); }))
5862 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
5863 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))
5864 && (wi::bit_and (wi::to_wide (@1), 1) == 1))
5865 (cmp @0
5866 {
5867 tree itype = TREE_TYPE (@0);
5868 int p = TYPE_PRECISION (itype);
5869 wide_int m = wi::one (p + 1) << p;
5870 wide_int a = wide_int::from (wi::to_wide (@1), p + 1, UNSIGNED);
5871 wide_int i = wide_int::from (wi::mod_inv (a, m),
5872 p, TYPE_SIGN (itype));
5873 wide_int_to_tree (itype, wi::mul (i, wi::to_wide (@2)));
5874 })))))
5875
5876 /* Simplify comparison of something with itself. For IEEE
5877 floating-point, we can only do some of these simplifications. */
5878 (for cmp (eq ge le)
5879 (simplify
5880 (cmp @0 @0)
5881 (if (! FLOAT_TYPE_P (TREE_TYPE (@0))
5882 || ! tree_expr_maybe_nan_p (@0))
5883 { constant_boolean_node (true, type); }
5884 (if (cmp != EQ_EXPR
5885 /* With -ftrapping-math conversion to EQ loses an exception. */
5886 && (! FLOAT_TYPE_P (TREE_TYPE (@0))
5887 || ! flag_trapping_math))
5888 (eq @0 @0)))))
5889 (for cmp (ne gt lt)
5890 (simplify
5891 (cmp @0 @0)
5892 (if (cmp != NE_EXPR
5893 || ! FLOAT_TYPE_P (TREE_TYPE (@0))
5894 || ! tree_expr_maybe_nan_p (@0))
5895 { constant_boolean_node (false, type); })))
5896 (for cmp (unle unge uneq)
5897 (simplify
5898 (cmp @0 @0)
5899 { constant_boolean_node (true, type); }))
5900 (for cmp (unlt ungt)
5901 (simplify
5902 (cmp @0 @0)
5903 (unordered @0 @0)))
5904 (simplify
5905 (ltgt @0 @0)
5906 (if (!flag_trapping_math || !tree_expr_maybe_nan_p (@0))
5907 { constant_boolean_node (false, type); }))
5908
5909 /* x == ~x -> false */
5910 /* x != ~x -> true */
5911 (for cmp (eq ne)
5912 (simplify
5913 (cmp:c @0 (bit_not @0))
5914 { constant_boolean_node (cmp == NE_EXPR, type); }))
5915
5916 /* Fold ~X op ~Y as Y op X. */
5917 (for cmp (simple_comparison)
5918 (simplify
5919 (cmp (bit_not@2 @0) (bit_not@3 @1))
5920 (if (single_use (@2) && single_use (@3))
5921 (cmp @1 @0))))
5922
5923 /* Fold ~X op C as X op' ~C, where op' is the swapped comparison. */
5924 (for cmp (simple_comparison)
5925 scmp (swapped_simple_comparison)
5926 (simplify
5927 (cmp (bit_not@2 @0) CONSTANT_CLASS_P@1)
5928 (if (single_use (@2)
5929 && (TREE_CODE (@1) == INTEGER_CST || TREE_CODE (@1) == VECTOR_CST))
5930 (scmp @0 (bit_not @1)))))
5931
5932 (for cmp (simple_comparison)
5933 (simplify
5934 (cmp @0 REAL_CST@1)
5935 /* IEEE doesn't distinguish +0 and -0 in comparisons. */
5936 (switch
5937 /* a CMP (-0) -> a CMP 0 */
5938 (if (REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@1)))
5939 (cmp @0 { build_real (TREE_TYPE (@1), dconst0); }))
5940 /* (-0) CMP b -> 0 CMP b. */
5941 (if (TREE_CODE (@0) == REAL_CST
5942 && REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@0)))
5943 (cmp { build_real (TREE_TYPE (@0), dconst0); } @1))
5944 /* x != NaN is always true, other ops are always false. */
5945 (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
5946 && (cmp == EQ_EXPR || cmp == NE_EXPR || !flag_trapping_math)
5947 && !tree_expr_signaling_nan_p (@1)
5948 && !tree_expr_maybe_signaling_nan_p (@0))
5949 { constant_boolean_node (cmp == NE_EXPR, type); })
5950 /* NaN != y is always true, other ops are always false. */
5951 (if (TREE_CODE (@0) == REAL_CST
5952 && REAL_VALUE_ISNAN (TREE_REAL_CST (@0))
5953 && (cmp == EQ_EXPR || cmp == NE_EXPR || !flag_trapping_math)
5954 && !tree_expr_signaling_nan_p (@0)
5955 && !tree_expr_signaling_nan_p (@1))
5956 { constant_boolean_node (cmp == NE_EXPR, type); })
5957 /* Fold comparisons against infinity. */
5958 (if (REAL_VALUE_ISINF (TREE_REAL_CST (@1))
5959 && MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (@1))))
5960 (with
5961 {
5962 REAL_VALUE_TYPE max;
5963 enum tree_code code = cmp;
5964 bool neg = REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1));
5965 if (neg)
5966 code = swap_tree_comparison (code);
5967 }
5968 (switch
5969 /* x > +Inf is always false, if we ignore NaNs or exceptions. */
5970 (if (code == GT_EXPR
5971 && !(HONOR_NANS (@0) && flag_trapping_math))
5972 { constant_boolean_node (false, type); })
5973 (if (code == LE_EXPR)
5974 /* x <= +Inf is always true, if we don't care about NaNs. */
5975 (if (! HONOR_NANS (@0))
5976 { constant_boolean_node (true, type); }
5977 /* x <= +Inf is the same as x == x, i.e. !isnan(x), but this loses
5978 an "invalid" exception. */
5979 (if (!flag_trapping_math)
5980 (eq @0 @0))))
5981 /* x == +Inf and x >= +Inf are always equal to x > DBL_MAX, but
5982 for == this introduces an exception for x a NaN. */
5983 (if ((code == EQ_EXPR && !(HONOR_NANS (@0) && flag_trapping_math))
5984 || code == GE_EXPR)
5985 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
5986 (if (neg)
5987 (lt @0 { build_real (TREE_TYPE (@0), max); })
5988 (gt @0 { build_real (TREE_TYPE (@0), max); }))))
5989 /* x < +Inf is always equal to x <= DBL_MAX. */
5990 (if (code == LT_EXPR)
5991 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
5992 (if (neg)
5993 (ge @0 { build_real (TREE_TYPE (@0), max); })
5994 (le @0 { build_real (TREE_TYPE (@0), max); }))))
5995 /* x != +Inf is always equal to !(x > DBL_MAX), but this introduces
5996 an exception for x a NaN so use an unordered comparison. */
5997 (if (code == NE_EXPR)
5998 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
5999 (if (! HONOR_NANS (@0))
6000 (if (neg)
6001 (ge @0 { build_real (TREE_TYPE (@0), max); })
6002 (le @0 { build_real (TREE_TYPE (@0), max); }))
6003 (if (neg)
6004 (unge @0 { build_real (TREE_TYPE (@0), max); })
6005 (unle @0 { build_real (TREE_TYPE (@0), max); }))))))))))
6006
6007 /* If this is a comparison of a real constant with a PLUS_EXPR
6008 or a MINUS_EXPR of a real constant, we can convert it into a
6009 comparison with a revised real constant as long as no overflow
6010 occurs when unsafe_math_optimizations are enabled. */
6011 (if (flag_unsafe_math_optimizations)
6012 (for op (plus minus)
6013 (simplify
6014 (cmp (op @0 REAL_CST@1) REAL_CST@2)
6015 (with
6016 {
6017 tree tem = const_binop (op == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR,
6018 TREE_TYPE (@1), @2, @1);
6019 }
6020 (if (tem && !TREE_OVERFLOW (tem))
6021 (cmp @0 { tem; }))))))
6022
6023 /* Likewise, we can simplify a comparison of a real constant with
6024 a MINUS_EXPR whose first operand is also a real constant, i.e.
6025 (c1 - x) < c2 becomes x > c1-c2. Reordering is allowed on
6026 floating-point types only if -fassociative-math is set. */
6027 (if (flag_associative_math)
6028 (simplify
6029 (cmp (minus REAL_CST@0 @1) REAL_CST@2)
6030 (with { tree tem = const_binop (MINUS_EXPR, TREE_TYPE (@1), @0, @2); }
6031 (if (tem && !TREE_OVERFLOW (tem))
6032 (cmp { tem; } @1)))))
6033
6034 /* Fold comparisons against built-in math functions. */
6035 (if (flag_unsafe_math_optimizations && ! flag_errno_math)
6036 (for sq (SQRT)
6037 (simplify
6038 (cmp (sq @0) REAL_CST@1)
6039 (switch
6040 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
6041 (switch
6042 /* sqrt(x) < y is always false, if y is negative. */
6043 (if (cmp == EQ_EXPR || cmp == LT_EXPR || cmp == LE_EXPR)
6044 { constant_boolean_node (false, type); })
6045 /* sqrt(x) > y is always true, if y is negative and we
6046 don't care about NaNs, i.e. negative values of x. */
6047 (if (cmp == NE_EXPR || !HONOR_NANS (@0))
6048 { constant_boolean_node (true, type); })
6049 /* sqrt(x) > y is the same as x >= 0, if y is negative. */
6050 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })))
6051 (if (real_equal (TREE_REAL_CST_PTR (@1), &dconst0))
6052 (switch
6053 /* sqrt(x) < 0 is always false. */
6054 (if (cmp == LT_EXPR)
6055 { constant_boolean_node (false, type); })
6056 /* sqrt(x) >= 0 is always true if we don't care about NaNs. */
6057 (if (cmp == GE_EXPR && !HONOR_NANS (@0))
6058 { constant_boolean_node (true, type); })
6059 /* sqrt(x) <= 0 -> x == 0. */
6060 (if (cmp == LE_EXPR)
6061 (eq @0 @1))
6062 /* Otherwise sqrt(x) cmp 0 -> x cmp 0. Here cmp can be >=, >,
6063 == or !=. In the last case:
6064
6065 (sqrt(x) != 0) == (NaN != 0) == true == (x != 0)
6066
6067 if x is negative or NaN. Due to -funsafe-math-optimizations,
6068 the results for other x follow from natural arithmetic. */
6069 (cmp @0 @1)))
6070 (if ((cmp == LT_EXPR
6071 || cmp == LE_EXPR
6072 || cmp == GT_EXPR
6073 || cmp == GE_EXPR)
6074 && !REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
6075 /* Give up for -frounding-math. */
6076 && !HONOR_SIGN_DEPENDENT_ROUNDING (TREE_TYPE (@0)))
6077 (with
6078 {
6079 REAL_VALUE_TYPE c2;
6080 enum tree_code ncmp = cmp;
6081 const real_format *fmt
6082 = REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (@0)));
6083 real_arithmetic (&c2, MULT_EXPR,
6084 &TREE_REAL_CST (@1), &TREE_REAL_CST (@1));
6085 real_convert (&c2, fmt, &c2);
6086 /* See PR91734: if c2 is inexact and sqrt(c2) < c (or sqrt(c2) >= c),
6087 then change LT_EXPR into LE_EXPR or GE_EXPR into GT_EXPR. */
6088 if (!REAL_VALUE_ISINF (c2))
6089 {
6090 tree c3 = fold_const_call (CFN_SQRT, TREE_TYPE (@0),
6091 build_real (TREE_TYPE (@0), c2));
6092 if (c3 == NULL_TREE || TREE_CODE (c3) != REAL_CST)
6093 ncmp = ERROR_MARK;
6094 else if ((cmp == LT_EXPR || cmp == GE_EXPR)
6095 && real_less (&TREE_REAL_CST (c3), &TREE_REAL_CST (@1)))
6096 ncmp = cmp == LT_EXPR ? LE_EXPR : GT_EXPR;
6097 else if ((cmp == LE_EXPR || cmp == GT_EXPR)
6098 && real_less (&TREE_REAL_CST (@1), &TREE_REAL_CST (c3)))
6099 ncmp = cmp == LE_EXPR ? LT_EXPR : GE_EXPR;
6100 else
6101 {
6102 /* With rounding to even, sqrt of up to 3 different values
6103 gives the same normal result, so in some cases c2 needs
6104 to be adjusted. */
6105 REAL_VALUE_TYPE c2alt, tow;
6106 if (cmp == LT_EXPR || cmp == GE_EXPR)
6107 tow = dconst0;
6108 else
6109 tow = dconstinf;
6110 real_nextafter (&c2alt, fmt, &c2, &tow);
6111 real_convert (&c2alt, fmt, &c2alt);
6112 if (REAL_VALUE_ISINF (c2alt))
6113 ncmp = ERROR_MARK;
6114 else
6115 {
6116 c3 = fold_const_call (CFN_SQRT, TREE_TYPE (@0),
6117 build_real (TREE_TYPE (@0), c2alt));
6118 if (c3 == NULL_TREE || TREE_CODE (c3) != REAL_CST)
6119 ncmp = ERROR_MARK;
6120 else if (real_equal (&TREE_REAL_CST (c3),
6121 &TREE_REAL_CST (@1)))
6122 c2 = c2alt;
6123 }
6124 }
6125 }
6126 }
6127 (if (cmp == GT_EXPR || cmp == GE_EXPR)
6128 (if (REAL_VALUE_ISINF (c2))
6129 /* sqrt(x) > y is x == +Inf, when y is very large. */
6130 (if (HONOR_INFINITIES (@0))
6131 (eq @0 { build_real (TREE_TYPE (@0), c2); })
6132 { constant_boolean_node (false, type); })
6133 /* sqrt(x) > c is the same as x > c*c. */
6134 (if (ncmp != ERROR_MARK)
6135 (if (ncmp == GE_EXPR)
6136 (ge @0 { build_real (TREE_TYPE (@0), c2); })
6137 (gt @0 { build_real (TREE_TYPE (@0), c2); }))))
6138 /* else if (cmp == LT_EXPR || cmp == LE_EXPR) */
6139 (if (REAL_VALUE_ISINF (c2))
6140 (switch
6141 /* sqrt(x) < y is always true, when y is a very large
6142 value and we don't care about NaNs or Infinities. */
6143 (if (! HONOR_NANS (@0) && ! HONOR_INFINITIES (@0))
6144 { constant_boolean_node (true, type); })
6145 /* sqrt(x) < y is x != +Inf when y is very large and we
6146 don't care about NaNs. */
6147 (if (! HONOR_NANS (@0))
6148 (ne @0 { build_real (TREE_TYPE (@0), c2); }))
6149 /* sqrt(x) < y is x >= 0 when y is very large and we
6150 don't care about Infinities. */
6151 (if (! HONOR_INFINITIES (@0))
6152 (ge @0 { build_real (TREE_TYPE (@0), dconst0); }))
6153 /* sqrt(x) < y is x >= 0 && x != +Inf, when y is large. */
6154 (if (GENERIC)
6155 (truth_andif
6156 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
6157 (ne @0 { build_real (TREE_TYPE (@0), c2); }))))
6158 /* sqrt(x) < c is the same as x < c*c, if we ignore NaNs. */
6159 (if (ncmp != ERROR_MARK && ! HONOR_NANS (@0))
6160 (if (ncmp == LT_EXPR)
6161 (lt @0 { build_real (TREE_TYPE (@0), c2); })
6162 (le @0 { build_real (TREE_TYPE (@0), c2); }))
6163 /* sqrt(x) < c is the same as x >= 0 && x < c*c. */
6164 (if (ncmp != ERROR_MARK && GENERIC)
6165 (if (ncmp == LT_EXPR)
6166 (truth_andif
6167 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
6168 (lt @0 { build_real (TREE_TYPE (@0), c2); }))
6169 (truth_andif
6170 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
6171 (le @0 { build_real (TREE_TYPE (@0), c2); })))))))))))
6172 /* Transform sqrt(x) cmp sqrt(y) -> x cmp y. */
6173 (simplify
6174 (cmp (sq @0) (sq @1))
6175 (if (! HONOR_NANS (@0))
6176 (cmp @0 @1))))))
6177
6178 /* Optimize various special cases of (FTYPE) N CMP (FTYPE) M. */
6179 (for cmp (lt le eq ne ge gt unordered ordered unlt unle ungt unge uneq ltgt)
6180 icmp (lt le eq ne ge gt unordered ordered lt le gt ge eq ne)
6181 (simplify
6182 (cmp (float@0 @1) (float @2))
6183 (if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (@0))
6184 && ! DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0)))
6185 (with
6186 {
6187 format_helper fmt (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (@0))));
6188 tree type1 = TREE_TYPE (@1);
6189 bool type1_signed_p = TYPE_SIGN (type1) == SIGNED;
6190 tree type2 = TREE_TYPE (@2);
6191 bool type2_signed_p = TYPE_SIGN (type2) == SIGNED;
6192 }
6193 (if (fmt.can_represent_integral_type_p (type1)
6194 && fmt.can_represent_integral_type_p (type2))
6195 (if (cmp == ORDERED_EXPR || cmp == UNORDERED_EXPR)
6196 { constant_boolean_node (cmp == ORDERED_EXPR, type); }
6197 (if (TYPE_PRECISION (type1) > TYPE_PRECISION (type2)
6198 && type1_signed_p >= type2_signed_p)
6199 (icmp @1 (convert @2))
6200 (if (TYPE_PRECISION (type1) < TYPE_PRECISION (type2)
6201 && type1_signed_p <= type2_signed_p)
6202 (icmp (convert:type2 @1) @2)
6203 (if (TYPE_PRECISION (type1) == TYPE_PRECISION (type2)
6204 && type1_signed_p == type2_signed_p)
6205 (icmp @1 @2))))))))))
6206
6207 /* Optimize various special cases of (FTYPE) N CMP CST. */
6208 (for cmp (lt le eq ne ge gt)
6209 icmp (le le eq ne ge ge)
6210 (simplify
6211 (cmp (float @0) REAL_CST@1)
6212 (if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (@1))
6213 && ! DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1)))
6214 (with
6215 {
6216 tree itype = TREE_TYPE (@0);
6217 format_helper fmt (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (@1))));
6218 const REAL_VALUE_TYPE *cst = TREE_REAL_CST_PTR (@1);
6219 /* Be careful to preserve any potential exceptions due to
6220 NaNs. qNaNs are ok in == or != context.
6221 TODO: relax under -fno-trapping-math or
6222 -fno-signaling-nans. */
6223 bool exception_p
6224 = real_isnan (cst) && (cst->signalling
6225 || (cmp != EQ_EXPR && cmp != NE_EXPR));
6226 }
6227 /* TODO: allow non-fitting itype and SNaNs when
6228 -fno-trapping-math. */
6229 (if (fmt.can_represent_integral_type_p (itype) && ! exception_p)
6230 (with
6231 {
6232 signop isign = TYPE_SIGN (itype);
6233 REAL_VALUE_TYPE imin, imax;
6234 real_from_integer (&imin, fmt, wi::min_value (itype), isign);
6235 real_from_integer (&imax, fmt, wi::max_value (itype), isign);
6236
6237 REAL_VALUE_TYPE icst;
6238 if (cmp == GT_EXPR || cmp == GE_EXPR)
6239 real_ceil (&icst, fmt, cst);
6240 else if (cmp == LT_EXPR || cmp == LE_EXPR)
6241 real_floor (&icst, fmt, cst);
6242 else
6243 real_trunc (&icst, fmt, cst);
6244
6245 bool cst_int_p = !real_isnan (cst) && real_identical (&icst, cst);
6246
6247 bool overflow_p = false;
6248 wide_int icst_val
6249 = real_to_integer (&icst, &overflow_p, TYPE_PRECISION (itype));
6250 }
6251 (switch
6252 /* Optimize cases when CST is outside of ITYPE's range. */
6253 (if (real_compare (LT_EXPR, cst, &imin))
6254 { constant_boolean_node (cmp == GT_EXPR || cmp == GE_EXPR || cmp == NE_EXPR,
6255 type); })
6256 (if (real_compare (GT_EXPR, cst, &imax))
6257 { constant_boolean_node (cmp == LT_EXPR || cmp == LE_EXPR || cmp == NE_EXPR,
6258 type); })
6259 /* Remove cast if CST is an integer representable by ITYPE. */
6260 (if (cst_int_p)
6261 (cmp @0 { gcc_assert (!overflow_p);
6262 wide_int_to_tree (itype, icst_val); })
6263 )
6264 /* When CST is fractional, optimize
6265 (FTYPE) N == CST -> 0
6266 (FTYPE) N != CST -> 1. */
6267 (if (cmp == EQ_EXPR || cmp == NE_EXPR)
6268 { constant_boolean_node (cmp == NE_EXPR, type); })
6269 /* Otherwise replace with sensible integer constant. */
6270 (with
6271 {
6272 gcc_checking_assert (!overflow_p);
6273 }
6274 (icmp @0 { wide_int_to_tree (itype, icst_val); })))))))))
6275
6276 /* Fold A /[ex] B CMP C to A CMP B * C. */
6277 (for cmp (eq ne)
6278 (simplify
6279 (cmp (exact_div @0 @1) INTEGER_CST@2)
6280 (if (!integer_zerop (@1))
6281 (if (wi::to_wide (@2) == 0)
6282 (cmp @0 @2)
6283 (if (TREE_CODE (@1) == INTEGER_CST)
6284 (with
6285 {
6286 wi::overflow_type ovf;
6287 wide_int prod = wi::mul (wi::to_wide (@2), wi::to_wide (@1),
6288 TYPE_SIGN (TREE_TYPE (@1)), &ovf);
6289 }
6290 (if (ovf)
6291 { constant_boolean_node (cmp == NE_EXPR, type); }
6292 (cmp @0 { wide_int_to_tree (TREE_TYPE (@0), prod); }))))))))
6293 (for cmp (lt le gt ge)
6294 (simplify
6295 (cmp (exact_div @0 INTEGER_CST@1) INTEGER_CST@2)
6296 (if (wi::gt_p (wi::to_wide (@1), 0, TYPE_SIGN (TREE_TYPE (@1))))
6297 (with
6298 {
6299 wi::overflow_type ovf;
6300 wide_int prod = wi::mul (wi::to_wide (@2), wi::to_wide (@1),
6301 TYPE_SIGN (TREE_TYPE (@1)), &ovf);
6302 }
6303 (if (ovf)
6304 { constant_boolean_node (wi::lt_p (wi::to_wide (@2), 0,
6305 TYPE_SIGN (TREE_TYPE (@2)))
6306 != (cmp == LT_EXPR || cmp == LE_EXPR), type); }
6307 (cmp @0 { wide_int_to_tree (TREE_TYPE (@0), prod); }))))))
6308
6309 /* Fold (size_t)(A /[ex] B) CMP C to (size_t)A CMP (size_t)B * C or A CMP' 0.
6310
6311 For small C (less than max/B), this is (size_t)A CMP (size_t)B * C.
6312 For large C (more than min/B+2^size), this is also true, with the
6313 multiplication computed modulo 2^size.
6314 For intermediate C, this just tests the sign of A. */
6315 (for cmp (lt le gt ge)
6316 cmp2 (ge ge lt lt)
6317 (simplify
6318 (cmp (convert (exact_div @0 INTEGER_CST@1)) INTEGER_CST@2)
6319 (if (tree_nop_conversion_p (TREE_TYPE (@0), TREE_TYPE (@2))
6320 && TYPE_UNSIGNED (TREE_TYPE (@2)) && !TYPE_UNSIGNED (TREE_TYPE (@0))
6321 && wi::gt_p (wi::to_wide (@1), 0, TYPE_SIGN (TREE_TYPE (@1))))
6322 (with
6323 {
6324 tree utype = TREE_TYPE (@2);
6325 wide_int denom = wi::to_wide (@1);
6326 wide_int right = wi::to_wide (@2);
6327 wide_int smax = wi::sdiv_trunc (wi::max_value (TREE_TYPE (@0)), denom);
6328 wide_int smin = wi::sdiv_trunc (wi::min_value (TREE_TYPE (@0)), denom);
6329 bool small = wi::leu_p (right, smax);
6330 bool large = wi::geu_p (right, smin);
6331 }
6332 (if (small || large)
6333 (cmp (convert:utype @0) (mult @2 (convert @1)))
6334 (cmp2 @0 { build_zero_cst (TREE_TYPE (@0)); }))))))
6335
6336 /* Unordered tests if either argument is a NaN. */
6337 (simplify
6338 (bit_ior (unordered @0 @0) (unordered @1 @1))
6339 (if (types_match (@0, @1))
6340 (unordered @0 @1)))
6341 (simplify
6342 (bit_and (ordered @0 @0) (ordered @1 @1))
6343 (if (types_match (@0, @1))
6344 (ordered @0 @1)))
6345 (simplify
6346 (bit_ior:c (unordered @0 @0) (unordered:c@2 @0 @1))
6347 @2)
6348 (simplify
6349 (bit_and:c (ordered @0 @0) (ordered:c@2 @0 @1))
6350 @2)
6351
6352 /* A & (2**N - 1) <= 2**K - 1 -> A & (2**N - 2**K) == 0
6353 A & (2**N - 1) > 2**K - 1 -> A & (2**N - 2**K) != 0
6354
6355 Note that comparisons
6356 A & (2**N - 1) < 2**K -> A & (2**N - 2**K) == 0
6357 A & (2**N - 1) >= 2**K -> A & (2**N - 2**K) != 0
6358 will be canonicalized to above so there's no need to
6359 consider them here.
6360 */
6361
6362 (for cmp (le gt)
6363 eqcmp (eq ne)
6364 (simplify
6365 (cmp (bit_and@0 @1 INTEGER_CST@2) INTEGER_CST@3)
6366 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)))
6367 (with
6368 {
6369 tree ty = TREE_TYPE (@0);
6370 unsigned prec = TYPE_PRECISION (ty);
6371 wide_int mask = wi::to_wide (@2, prec);
6372 wide_int rhs = wi::to_wide (@3, prec);
6373 signop sgn = TYPE_SIGN (ty);
6374 }
6375 (if ((mask & (mask + 1)) == 0 && wi::gt_p (rhs, 0, sgn)
6376 && (rhs & (rhs + 1)) == 0 && wi::ge_p (mask, rhs, sgn))
6377 (eqcmp (bit_and @1 { wide_int_to_tree (ty, mask - rhs); })
6378 { build_zero_cst (ty); }))))))
6379
6380 /* -A CMP -B -> B CMP A. */
6381 (for cmp (tcc_comparison)
6382 scmp (swapped_tcc_comparison)
6383 (simplify
6384 (cmp (negate @0) (negate @1))
6385 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
6386 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
6387 && (cmp == EQ_EXPR
6388 || cmp == NE_EXPR
6389 || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))))
6390 (scmp @0 @1)))
6391 (simplify
6392 (cmp (negate @0) CONSTANT_CLASS_P@1)
6393 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
6394 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
6395 && (cmp == EQ_EXPR
6396 || cmp == NE_EXPR
6397 || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))))
6398 (with { tree tem = const_unop (NEGATE_EXPR, TREE_TYPE (@0), @1); }
6399 (if (tem && !TREE_OVERFLOW (tem))
6400 (scmp @0 { tem; }))))))
6401
6402 /* Convert ABS[U]_EXPR<x> == 0 or ABS[U]_EXPR<x> != 0 to x == 0 or x != 0. */
6403 (for op (abs absu)
6404 (for eqne (eq ne)
6405 (simplify
6406 (eqne (op @0) zerop@1)
6407 (eqne @0 { build_zero_cst (TREE_TYPE (@0)); }))))
6408
6409 /* From fold_sign_changed_comparison and fold_widened_comparison.
6410 FIXME: the lack of symmetry is disturbing. */
6411 (for cmp (simple_comparison)
6412 (simplify
6413 (cmp (convert@0 @00) (convert?@1 @10))
6414 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6415 /* Disable this optimization if we're casting a function pointer
6416 type on targets that require function pointer canonicalization. */
6417 && !(targetm.have_canonicalize_funcptr_for_compare ()
6418 && ((POINTER_TYPE_P (TREE_TYPE (@00))
6419 && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@00))))
6420 || (POINTER_TYPE_P (TREE_TYPE (@10))
6421 && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@10))))))
6422 && single_use (@0))
6423 (if (TYPE_PRECISION (TREE_TYPE (@00)) == TYPE_PRECISION (TREE_TYPE (@0))
6424 && (TREE_CODE (@10) == INTEGER_CST
6425 || @1 != @10)
6426 && (TYPE_UNSIGNED (TREE_TYPE (@00)) == TYPE_UNSIGNED (TREE_TYPE (@0))
6427 || cmp == NE_EXPR
6428 || cmp == EQ_EXPR)
6429 && !POINTER_TYPE_P (TREE_TYPE (@00))
6430 /* (int)bool:32 != (int)uint is not the same as
6431 bool:32 != (bool:32)uint since boolean types only have two valid
6432 values independent of their precision. */
6433 && (TREE_CODE (TREE_TYPE (@00)) != BOOLEAN_TYPE
6434 || TREE_CODE (TREE_TYPE (@10)) == BOOLEAN_TYPE))
6435 /* ??? The special-casing of INTEGER_CST conversion was in the original
6436 code and here to avoid a spurious overflow flag on the resulting
6437 constant which fold_convert produces. */
6438 (if (TREE_CODE (@1) == INTEGER_CST)
6439 (cmp @00 { force_fit_type (TREE_TYPE (@00), wi::to_widest (@1), 0,
6440 TREE_OVERFLOW (@1)); })
6441 (cmp @00 (convert @1)))
6442
6443 (if (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (TREE_TYPE (@00)))
6444 /* If possible, express the comparison in the shorter mode. */
6445 (if ((cmp == EQ_EXPR || cmp == NE_EXPR
6446 || TYPE_UNSIGNED (TREE_TYPE (@0)) == TYPE_UNSIGNED (TREE_TYPE (@00))
6447 || (!TYPE_UNSIGNED (TREE_TYPE (@0))
6448 && TYPE_UNSIGNED (TREE_TYPE (@00))))
6449 && (types_match (TREE_TYPE (@10), TREE_TYPE (@00))
6450 || ((TYPE_PRECISION (TREE_TYPE (@00))
6451 >= TYPE_PRECISION (TREE_TYPE (@10)))
6452 && (TYPE_UNSIGNED (TREE_TYPE (@00))
6453 == TYPE_UNSIGNED (TREE_TYPE (@10))))
6454 || (TREE_CODE (@10) == INTEGER_CST
6455 && INTEGRAL_TYPE_P (TREE_TYPE (@00))
6456 && int_fits_type_p (@10, TREE_TYPE (@00)))))
6457 (cmp @00 (convert @10))
6458 (if (TREE_CODE (@10) == INTEGER_CST
6459 && INTEGRAL_TYPE_P (TREE_TYPE (@00))
6460 && !int_fits_type_p (@10, TREE_TYPE (@00)))
6461 (with
6462 {
6463 tree min = lower_bound_in_type (TREE_TYPE (@10), TREE_TYPE (@00));
6464 tree max = upper_bound_in_type (TREE_TYPE (@10), TREE_TYPE (@00));
6465 bool above = integer_nonzerop (const_binop (LT_EXPR, type, max, @10));
6466 bool below = integer_nonzerop (const_binop (LT_EXPR, type, @10, min));
6467 }
6468 (if (above || below)
6469 (if (cmp == EQ_EXPR || cmp == NE_EXPR)
6470 { constant_boolean_node (cmp == EQ_EXPR ? false : true, type); }
6471 (if (cmp == LT_EXPR || cmp == LE_EXPR)
6472 { constant_boolean_node (above ? true : false, type); }
6473 (if (cmp == GT_EXPR || cmp == GE_EXPR)
6474 { constant_boolean_node (above ? false : true, type); })))))))))
6475 /* Fold (double)float1 CMP (double)float2 into float1 CMP float2. */
6476 (if (FLOAT_TYPE_P (TREE_TYPE (@00))
6477 && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0))
6478 == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@00)))
6479 && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0))
6480 == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@10))))
6481 (with
6482 {
6483 tree type1 = TREE_TYPE (@10);
6484 if (TREE_CODE (@10) == REAL_CST && !DECIMAL_FLOAT_TYPE_P (type1))
6485 {
6486 REAL_VALUE_TYPE orig = TREE_REAL_CST (@10);
6487 if (TYPE_PRECISION (type1) > TYPE_PRECISION (float_type_node)
6488 && exact_real_truncate (TYPE_MODE (float_type_node), &orig))
6489 type1 = float_type_node;
6490 if (TYPE_PRECISION (type1) > TYPE_PRECISION (double_type_node)
6491 && exact_real_truncate (TYPE_MODE (double_type_node), &orig))
6492 type1 = double_type_node;
6493 }
6494 tree newtype
6495 = (element_precision (TREE_TYPE (@00)) > element_precision (type1)
6496 ? TREE_TYPE (@00) : type1);
6497 }
6498 (if (element_precision (TREE_TYPE (@0)) > element_precision (newtype))
6499 (cmp (convert:newtype @00) (convert:newtype @10))))))))
6500
6501
6502 (for cmp (eq ne)
6503 (simplify
6504 /* SSA names are canonicalized to 2nd place. */
6505 (cmp addr@0 SSA_NAME@1)
6506 (with
6507 {
6508 poly_int64 off; tree base;
6509 tree addr = (TREE_CODE (@0) == SSA_NAME
6510 ? gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0)) : @0);
6511 }
6512 /* A local variable can never be pointed to by
6513 the default SSA name of an incoming parameter. */
6514 (if (SSA_NAME_IS_DEFAULT_DEF (@1)
6515 && TREE_CODE (SSA_NAME_VAR (@1)) == PARM_DECL
6516 && (base = get_base_address (TREE_OPERAND (addr, 0)))
6517 && TREE_CODE (base) == VAR_DECL
6518 && auto_var_in_fn_p (base, current_function_decl))
6519 (if (cmp == NE_EXPR)
6520 { constant_boolean_node (true, type); }
6521 { constant_boolean_node (false, type); })
6522 /* If the address is based on @1 decide using the offset. */
6523 (if ((base = get_addr_base_and_unit_offset (TREE_OPERAND (addr, 0), &off))
6524 && TREE_CODE (base) == MEM_REF
6525 && TREE_OPERAND (base, 0) == @1)
6526 (with { off += mem_ref_offset (base).force_shwi (); }
6527 (if (known_ne (off, 0))
6528 { constant_boolean_node (cmp == NE_EXPR, type); }
6529 (if (known_eq (off, 0))
6530 { constant_boolean_node (cmp == EQ_EXPR, type); }))))))))
6531
6532 /* Equality compare simplifications from fold_binary */
6533 (for cmp (eq ne)
6534
6535 /* If we have (A | C) == D where C & ~D != 0, convert this into 0.
6536 Similarly for NE_EXPR. */
6537 (simplify
6538 (cmp (convert?@3 (bit_ior @0 INTEGER_CST@1)) INTEGER_CST@2)
6539 (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0))
6540 && wi::bit_and_not (wi::to_wide (@1), wi::to_wide (@2)) != 0)
6541 { constant_boolean_node (cmp == NE_EXPR, type); }))
6542
6543 /* (X ^ Y) == 0 becomes X == Y, and (X ^ Y) != 0 becomes X != Y. */
6544 (simplify
6545 (cmp (bit_xor @0 @1) integer_zerop)
6546 (cmp @0 @1))
6547
6548 /* (X ^ Y) == Y becomes X == 0.
6549 Likewise (X ^ Y) == X becomes Y == 0. */
6550 (simplify
6551 (cmp:c (bit_xor:c @0 @1) @0)
6552 (cmp @1 { build_zero_cst (TREE_TYPE (@1)); }))
6553
6554 /* (X & Y) == X becomes (X & ~Y) == 0. */
6555 (simplify
6556 (cmp:c (bit_and:c @0 @1) @0)
6557 (cmp (bit_and @0 (bit_not! @1)) { build_zero_cst (TREE_TYPE (@0)); }))
6558 (simplify
6559 (cmp:c (convert@3 (bit_and (convert@2 @0) INTEGER_CST@1)) (convert @0))
6560 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6561 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
6562 && INTEGRAL_TYPE_P (TREE_TYPE (@3))
6563 && TYPE_PRECISION (TREE_TYPE (@2)) == TYPE_PRECISION (TREE_TYPE (@0))
6564 && TYPE_PRECISION (TREE_TYPE (@3)) > TYPE_PRECISION (TREE_TYPE (@2))
6565 && !wi::neg_p (wi::to_wide (@1)))
6566 (cmp (bit_and @0 (convert (bit_not @1)))
6567 { build_zero_cst (TREE_TYPE (@0)); })))
6568
6569 /* (X | Y) == Y becomes (X & ~Y) == 0. */
6570 (simplify
6571 (cmp:c (bit_ior:c @0 @1) @1)
6572 (cmp (bit_and @0 (bit_not! @1)) { build_zero_cst (TREE_TYPE (@0)); }))
6573
6574 /* (X ^ C1) op C2 can be rewritten as X op (C1 ^ C2). */
6575 (simplify
6576 (cmp (convert?@3 (bit_xor @0 INTEGER_CST@1)) INTEGER_CST@2)
6577 (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0)))
6578 (cmp @0 (bit_xor @1 (convert @2)))))
6579
6580 (simplify
6581 (cmp (nop_convert? @0) integer_zerop)
6582 (if (tree_expr_nonzero_p (@0))
6583 { constant_boolean_node (cmp == NE_EXPR, type); }))
6584
6585 /* (X & C) op (Y & C) into (X ^ Y) & C op 0. */
6586 (simplify
6587 (cmp (bit_and:cs @0 @2) (bit_and:cs @1 @2))
6588 (cmp (bit_and (bit_xor @0 @1) @2) { build_zero_cst (TREE_TYPE (@2)); })))
6589
6590 /* (X < 0) != (Y < 0) into (X ^ Y) < 0.
6591 (X >= 0) != (Y >= 0) into (X ^ Y) < 0.
6592 (X < 0) == (Y < 0) into (X ^ Y) >= 0.
6593 (X >= 0) == (Y >= 0) into (X ^ Y) >= 0. */
6594 (for cmp (eq ne)
6595 ncmp (ge lt)
6596 (for sgncmp (ge lt)
6597 (simplify
6598 (cmp (sgncmp @0 integer_zerop@2) (sgncmp @1 integer_zerop))
6599 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
6600 && !TYPE_UNSIGNED (TREE_TYPE (@0))
6601 && types_match (@0, @1))
6602 (ncmp (bit_xor @0 @1) @2)))))
6603 /* (X < 0) == (Y >= 0) into (X ^ Y) < 0.
6604 (X < 0) != (Y >= 0) into (X ^ Y) >= 0. */
6605 (for cmp (eq ne)
6606 ncmp (lt ge)
6607 (simplify
6608 (cmp:c (lt @0 integer_zerop@2) (ge @1 integer_zerop))
6609 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
6610 && !TYPE_UNSIGNED (TREE_TYPE (@0))
6611 && types_match (@0, @1))
6612 (ncmp (bit_xor @0 @1) @2))))
6613
6614 /* If we have (A & C) == C where C is a power of 2, convert this into
6615 (A & C) != 0. Similarly for NE_EXPR. */
6616 (for cmp (eq ne)
6617 icmp (ne eq)
6618 (simplify
6619 (cmp (bit_and@2 @0 integer_pow2p@1) @1)
6620 (icmp @2 { build_zero_cst (TREE_TYPE (@0)); })))
6621
6622 #if GIMPLE
6623 /* From fold_binary_op_with_conditional_arg handle the case of
6624 rewriting (a ? b : c) > d to a ? (b > d) : (c > d) when the
6625 compares simplify. */
6626 (for cmp (simple_comparison)
6627 (simplify
6628 (cmp:c (cond @0 @1 @2) @3)
6629 /* Do not move possibly trapping operations into the conditional as this
6630 pessimizes code and causes gimplification issues when applied late. */
6631 (if (!FLOAT_TYPE_P (TREE_TYPE (@3))
6632 || !operation_could_trap_p (cmp, true, false, @3))
6633 (cond @0 (cmp! @1 @3) (cmp! @2 @3)))))
6634 #endif
6635
6636 (for cmp (ge lt)
6637 /* x < 0 ? ~y : y into (x >> (prec-1)) ^ y. */
6638 /* x >= 0 ? ~y : y into ~((x >> (prec-1)) ^ y). */
6639 (simplify
6640 (cond (cmp @0 integer_zerop) (bit_not @1) @1)
6641 (if (INTEGRAL_TYPE_P (type)
6642 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
6643 && !TYPE_UNSIGNED (TREE_TYPE (@0))
6644 && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (type))
6645 (with
6646 {
6647 tree shifter = build_int_cst (integer_type_node, TYPE_PRECISION (type) - 1);
6648 }
6649 (if (cmp == LT_EXPR)
6650 (bit_xor (convert (rshift @0 {shifter;})) @1)
6651 (bit_not (bit_xor (convert (rshift @0 {shifter;})) @1))))))
6652 /* x < 0 ? y : ~y into ~((x >> (prec-1)) ^ y). */
6653 /* x >= 0 ? y : ~y into (x >> (prec-1)) ^ y. */
6654 (simplify
6655 (cond (cmp @0 integer_zerop) @1 (bit_not @1))
6656 (if (INTEGRAL_TYPE_P (type)
6657 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
6658 && !TYPE_UNSIGNED (TREE_TYPE (@0))
6659 && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (type))
6660 (with
6661 {
6662 tree shifter = build_int_cst (integer_type_node, TYPE_PRECISION (type) - 1);
6663 }
6664 (if (cmp == GE_EXPR)
6665 (bit_xor (convert (rshift @0 {shifter;})) @1)
6666 (bit_not (bit_xor (convert (rshift @0 {shifter;})) @1)))))))
6667
6668 /* If we have (A & C) != 0 ? D : 0 where C and D are powers of 2,
6669 convert this into a shift followed by ANDing with D. */
6670 (simplify
6671 (cond
6672 (ne (bit_and @0 integer_pow2p@1) integer_zerop)
6673 INTEGER_CST@2 integer_zerop)
6674 (if (!POINTER_TYPE_P (type) && integer_pow2p (@2))
6675 (with {
6676 int shift = (wi::exact_log2 (wi::to_wide (@2))
6677 - wi::exact_log2 (wi::to_wide (@1)));
6678 }
6679 (if (shift > 0)
6680 (bit_and
6681 (lshift (convert @0) { build_int_cst (integer_type_node, shift); }) @2)
6682 (bit_and
6683 (convert (rshift @0 { build_int_cst (integer_type_node, -shift); }))
6684 @2)))))
6685
6686 /* If we have (A & C) != 0 where C is the sign bit of A, convert
6687 this into A < 0. Similarly for (A & C) == 0 into A >= 0. */
6688 (for cmp (eq ne)
6689 ncmp (ge lt)
6690 (simplify
6691 (cmp (bit_and (convert?@2 @0) integer_pow2p@1) integer_zerop)
6692 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6693 && type_has_mode_precision_p (TREE_TYPE (@0))
6694 && element_precision (@2) >= element_precision (@0)
6695 && wi::only_sign_bit_p (wi::to_wide (@1), element_precision (@0)))
6696 (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
6697 (ncmp (convert:stype @0) { build_zero_cst (stype); })))))
6698
6699 /* If we have A < 0 ? C : 0 where C is a power of 2, convert
6700 this into a right shift or sign extension followed by ANDing with C. */
6701 (simplify
6702 (cond
6703 (lt @0 integer_zerop)
6704 INTEGER_CST@1 integer_zerop)
6705 (if (integer_pow2p (@1)
6706 && !TYPE_UNSIGNED (TREE_TYPE (@0)))
6707 (with {
6708 int shift = element_precision (@0) - wi::exact_log2 (wi::to_wide (@1)) - 1;
6709 }
6710 (if (shift >= 0)
6711 (bit_and
6712 (convert (rshift @0 { build_int_cst (integer_type_node, shift); }))
6713 @1)
6714 /* Otherwise ctype must be wider than TREE_TYPE (@0) and pure
6715 sign extension followed by AND with C will achieve the effect. */
6716 (bit_and (convert @0) @1)))))
6717
6718 /* When the addresses are not directly of decls compare base and offset.
6719 This implements some remaining parts of fold_comparison address
6720 comparisons but still no complete part of it. Still it is good
6721 enough to make fold_stmt not regress when not dispatching to fold_binary. */
6722 (for cmp (simple_comparison)
6723 (simplify
6724 (cmp (convert1?@2 addr@0) (convert2? addr@1))
6725 (with
6726 {
6727 poly_int64 off0, off1;
6728 tree base0, base1;
6729 int equal = address_compare (cmp, TREE_TYPE (@2), @0, @1, base0, base1,
6730 off0, off1, GENERIC);
6731 }
6732 (if (equal == 1)
6733 (switch
6734 (if (cmp == EQ_EXPR && (known_eq (off0, off1) || known_ne (off0, off1)))
6735 { constant_boolean_node (known_eq (off0, off1), type); })
6736 (if (cmp == NE_EXPR && (known_eq (off0, off1) || known_ne (off0, off1)))
6737 { constant_boolean_node (known_ne (off0, off1), type); })
6738 (if (cmp == LT_EXPR && (known_lt (off0, off1) || known_ge (off0, off1)))
6739 { constant_boolean_node (known_lt (off0, off1), type); })
6740 (if (cmp == LE_EXPR && (known_le (off0, off1) || known_gt (off0, off1)))
6741 { constant_boolean_node (known_le (off0, off1), type); })
6742 (if (cmp == GE_EXPR && (known_ge (off0, off1) || known_lt (off0, off1)))
6743 { constant_boolean_node (known_ge (off0, off1), type); })
6744 (if (cmp == GT_EXPR && (known_gt (off0, off1) || known_le (off0, off1)))
6745 { constant_boolean_node (known_gt (off0, off1), type); }))
6746 (if (equal == 0)
6747 (switch
6748 (if (cmp == EQ_EXPR)
6749 { constant_boolean_node (false, type); })
6750 (if (cmp == NE_EXPR)
6751 { constant_boolean_node (true, type); })))))))
6752
6753 #if GIMPLE
6754 /* a?~t:t -> (-(a))^t */
6755 (simplify
6756 (cond @0 @1 @2)
6757 (with { bool wascmp; }
6758 (if (INTEGRAL_TYPE_P (type)
6759 && bitwise_inverted_equal_p (@1, @2, wascmp)
6760 && (!wascmp || TYPE_PRECISION (type) == 1))
6761 (if ((!TYPE_UNSIGNED (type) && TREE_CODE (type) == BOOLEAN_TYPE)
6762 || TYPE_PRECISION (type) == 1)
6763 (bit_xor (convert:type @0) @2)
6764 (bit_xor (negate (convert:type @0)) @2)))))
6765 #endif
6766
6767 /* Simplify pointer equality compares using PTA. */
6768 (for neeq (ne eq)
6769 (simplify
6770 (neeq @0 @1)
6771 (if (POINTER_TYPE_P (TREE_TYPE (@0))
6772 && ptrs_compare_unequal (@0, @1))
6773 { constant_boolean_node (neeq != EQ_EXPR, type); })))
6774
6775 /* PR70920: Transform (intptr_t)x eq/ne CST to x eq/ne (typeof x) CST.
6776 and (typeof ptr_cst) x eq/ne ptr_cst to x eq/ne (typeof x) CST.
6777 Disable the transform if either operand is pointer to function.
6778 This broke pr22051-2.c for arm where function pointer
6779 canonicalizaion is not wanted. */
6780
6781 (for cmp (ne eq)
6782 (simplify
6783 (cmp (convert @0) INTEGER_CST@1)
6784 (if (((POINTER_TYPE_P (TREE_TYPE (@0))
6785 && !FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@0)))
6786 && INTEGRAL_TYPE_P (TREE_TYPE (@1))
6787 /* Don't perform this optimization in GENERIC if @0 has reference
6788 type when sanitizing. See PR101210. */
6789 && !(GENERIC
6790 && TREE_CODE (TREE_TYPE (@0)) == REFERENCE_TYPE
6791 && (flag_sanitize & (SANITIZE_NULL | SANITIZE_ALIGNMENT))))
6792 || (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6793 && POINTER_TYPE_P (TREE_TYPE (@1))
6794 && !FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@1)))))
6795 && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
6796 (cmp @0 (convert @1)))))
6797
6798 /* Non-equality compare simplifications from fold_binary */
6799 (for cmp (lt gt le ge)
6800 /* Comparisons with the highest or lowest possible integer of
6801 the specified precision will have known values. */
6802 (simplify
6803 (cmp (convert?@2 @0) uniform_integer_cst_p@1)
6804 (if ((INTEGRAL_TYPE_P (TREE_TYPE (@1))
6805 || POINTER_TYPE_P (TREE_TYPE (@1))
6806 || VECTOR_INTEGER_TYPE_P (TREE_TYPE (@1)))
6807 && tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@0)))
6808 (with
6809 {
6810 tree cst = uniform_integer_cst_p (@1);
6811 tree arg1_type = TREE_TYPE (cst);
6812 unsigned int prec = TYPE_PRECISION (arg1_type);
6813 wide_int max = wi::max_value (arg1_type);
6814 wide_int signed_max = wi::max_value (prec, SIGNED);
6815 wide_int min = wi::min_value (arg1_type);
6816 }
6817 (switch
6818 (if (wi::to_wide (cst) == max)
6819 (switch
6820 (if (cmp == GT_EXPR)
6821 { constant_boolean_node (false, type); })
6822 (if (cmp == GE_EXPR)
6823 (eq @2 @1))
6824 (if (cmp == LE_EXPR)
6825 { constant_boolean_node (true, type); })
6826 (if (cmp == LT_EXPR)
6827 (ne @2 @1))))
6828 (if (wi::to_wide (cst) == min)
6829 (switch
6830 (if (cmp == LT_EXPR)
6831 { constant_boolean_node (false, type); })
6832 (if (cmp == LE_EXPR)
6833 (eq @2 @1))
6834 (if (cmp == GE_EXPR)
6835 { constant_boolean_node (true, type); })
6836 (if (cmp == GT_EXPR)
6837 (ne @2 @1))))
6838 (if (wi::to_wide (cst) == max - 1)
6839 (switch
6840 (if (cmp == GT_EXPR)
6841 (eq @2 { build_uniform_cst (TREE_TYPE (@1),
6842 wide_int_to_tree (TREE_TYPE (cst),
6843 wi::to_wide (cst)
6844 + 1)); }))
6845 (if (cmp == LE_EXPR)
6846 (ne @2 { build_uniform_cst (TREE_TYPE (@1),
6847 wide_int_to_tree (TREE_TYPE (cst),
6848 wi::to_wide (cst)
6849 + 1)); }))))
6850 (if (wi::to_wide (cst) == min + 1)
6851 (switch
6852 (if (cmp == GE_EXPR)
6853 (ne @2 { build_uniform_cst (TREE_TYPE (@1),
6854 wide_int_to_tree (TREE_TYPE (cst),
6855 wi::to_wide (cst)
6856 - 1)); }))
6857 (if (cmp == LT_EXPR)
6858 (eq @2 { build_uniform_cst (TREE_TYPE (@1),
6859 wide_int_to_tree (TREE_TYPE (cst),
6860 wi::to_wide (cst)
6861 - 1)); }))))
6862 (if (wi::to_wide (cst) == signed_max
6863 && TYPE_UNSIGNED (arg1_type)
6864 && TYPE_MODE (arg1_type) != BLKmode
6865 /* We will flip the signedness of the comparison operator
6866 associated with the mode of @1, so the sign bit is
6867 specified by this mode. Check that @1 is the signed
6868 max associated with this sign bit. */
6869 && prec == GET_MODE_PRECISION (SCALAR_INT_TYPE_MODE (arg1_type))
6870 /* signed_type does not work on pointer types. */
6871 && INTEGRAL_TYPE_P (arg1_type))
6872 /* The following case also applies to X < signed_max+1
6873 and X >= signed_max+1 because previous transformations. */
6874 (if (cmp == LE_EXPR || cmp == GT_EXPR)
6875 (with { tree st = signed_type_for (TREE_TYPE (@1)); }
6876 (switch
6877 (if (cst == @1 && cmp == LE_EXPR)
6878 (ge (convert:st @0) { build_zero_cst (st); }))
6879 (if (cst == @1 && cmp == GT_EXPR)
6880 (lt (convert:st @0) { build_zero_cst (st); }))
6881 (if (cmp == LE_EXPR)
6882 (ge (view_convert:st @0) { build_zero_cst (st); }))
6883 (if (cmp == GT_EXPR)
6884 (lt (view_convert:st @0) { build_zero_cst (st); })))))))))))
6885
6886 /* unsigned < (typeof unsigned)(unsigned != 0) is always false. */
6887 (simplify
6888 (lt:c @0 (convert (ne @0 integer_zerop)))
6889 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
6890 { constant_boolean_node (false, type); }))
6891
6892 /* x != (typeof x)(x == CST) -> CST == 0 ? 1 : (CST == 1 ? (x!=0&&x!=1) : x != 0) */
6893 /* x != (typeof x)(x != CST) -> CST == 1 ? 1 : (CST == 0 ? (x!=0&&x!=1) : x != 1) */
6894 /* x == (typeof x)(x == CST) -> CST == 0 ? 0 : (CST == 1 ? (x==0||x==1) : x == 0) */
6895 /* x == (typeof x)(x != CST) -> CST == 1 ? 0 : (CST == 0 ? (x==0||x==1) : x == 1) */
6896 (for outer (ne eq)
6897 (for inner (ne eq)
6898 (simplify
6899 (outer:c @0 (convert (inner @0 INTEGER_CST@1)))
6900 (with {
6901 bool cst1 = integer_onep (@1);
6902 bool cst0 = integer_zerop (@1);
6903 bool innereq = inner == EQ_EXPR;
6904 bool outereq = outer == EQ_EXPR;
6905 }
6906 (switch
6907 (if (innereq ? cst0 : cst1)
6908 { constant_boolean_node (!outereq, type); })
6909 (if (innereq ? cst1 : cst0)
6910 (with {
6911 tree utype = unsigned_type_for (TREE_TYPE (@0));
6912 tree ucst1 = build_one_cst (utype);
6913 }
6914 (if (!outereq)
6915 (gt (convert:utype @0) { ucst1; })
6916 (le (convert:utype @0) { ucst1; })
6917 )
6918 )
6919 )
6920 (with {
6921 tree value = build_int_cst (TREE_TYPE (@0), !innereq);
6922 }
6923 (if (outereq)
6924 (eq @0 { value; })
6925 (ne @0 { value; })
6926 )
6927 )
6928 )
6929 )
6930 )
6931 )
6932 )
6933
6934 (for cmp (unordered ordered unlt unle ungt unge uneq ltgt)
6935 /* If the second operand is NaN, the result is constant. */
6936 (simplify
6937 (cmp @0 REAL_CST@1)
6938 (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
6939 && (cmp != LTGT_EXPR || ! flag_trapping_math))
6940 { constant_boolean_node (cmp == ORDERED_EXPR || cmp == LTGT_EXPR
6941 ? false : true, type); })))
6942
6943 /* Fold UNORDERED if either operand must be NaN, or neither can be. */
6944 (simplify
6945 (unordered @0 @1)
6946 (switch
6947 (if (tree_expr_nan_p (@0) || tree_expr_nan_p (@1))
6948 { constant_boolean_node (true, type); })
6949 (if (!tree_expr_maybe_nan_p (@0) && !tree_expr_maybe_nan_p (@1))
6950 { constant_boolean_node (false, type); })))
6951
6952 /* Fold ORDERED if either operand must be NaN, or neither can be. */
6953 (simplify
6954 (ordered @0 @1)
6955 (switch
6956 (if (tree_expr_nan_p (@0) || tree_expr_nan_p (@1))
6957 { constant_boolean_node (false, type); })
6958 (if (!tree_expr_maybe_nan_p (@0) && !tree_expr_maybe_nan_p (@1))
6959 { constant_boolean_node (true, type); })))
6960
6961 /* bool_var != 0 becomes bool_var. */
6962 (simplify
6963 (ne @0 integer_zerop)
6964 (if (TREE_CODE (TREE_TYPE (@0)) == BOOLEAN_TYPE
6965 && types_match (type, TREE_TYPE (@0)))
6966 (non_lvalue @0)))
6967 /* bool_var == 1 becomes bool_var. */
6968 (simplify
6969 (eq @0 integer_onep)
6970 (if (TREE_CODE (TREE_TYPE (@0)) == BOOLEAN_TYPE
6971 && types_match (type, TREE_TYPE (@0)))
6972 (non_lvalue @0)))
6973 /* Do not handle
6974 bool_var == 0 becomes !bool_var or
6975 bool_var != 1 becomes !bool_var
6976 here because that only is good in assignment context as long
6977 as we require a tcc_comparison in GIMPLE_CONDs where we'd
6978 replace if (x == 0) with tem = ~x; if (tem != 0) which is
6979 clearly less optimal and which we'll transform again in forwprop. */
6980
6981 /* Transform comparisons of the form (X & Y) CMP 0 to X CMP2 Z
6982 where ~Y + 1 == pow2 and Z = ~Y. */
6983 (for cst (VECTOR_CST INTEGER_CST)
6984 (for cmp (eq ne)
6985 icmp (le gt)
6986 (simplify
6987 (cmp (bit_and:c@2 @0 cst@1) integer_zerop)
6988 (with { tree csts = bitmask_inv_cst_vector_p (@1); }
6989 (if (csts && (VECTOR_TYPE_P (TREE_TYPE (@1)) || single_use (@2)))
6990 (with { auto optab = VECTOR_TYPE_P (TREE_TYPE (@1))
6991 ? optab_vector : optab_default;
6992 tree utype = unsigned_type_for (TREE_TYPE (@1)); }
6993 (if (target_supports_op_p (utype, icmp, optab)
6994 || (optimize_vectors_before_lowering_p ()
6995 && (!target_supports_op_p (type, cmp, optab)
6996 || !target_supports_op_p (type, BIT_AND_EXPR, optab))))
6997 (if (TYPE_UNSIGNED (TREE_TYPE (@1)))
6998 (icmp @0 { csts; })
6999 (icmp (view_convert:utype @0) { csts; })))))))))
7000
7001 /* When one argument is a constant, overflow detection can be simplified.
7002 Currently restricted to single use so as not to interfere too much with
7003 ADD_OVERFLOW detection in tree-ssa-math-opts.cc.
7004 CONVERT?(CONVERT?(A) + CST) CMP A -> A CMP' CST' */
7005 (for cmp (lt le ge gt)
7006 out (gt gt le le)
7007 (simplify
7008 (cmp:c (convert?@3 (plus@2 (convert?@4 @0) INTEGER_CST@1)) @0)
7009 (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@2))
7010 && types_match (TREE_TYPE (@0), TREE_TYPE (@3))
7011 && tree_nop_conversion_p (TREE_TYPE (@4), TREE_TYPE (@0))
7012 && wi::to_wide (@1) != 0
7013 && single_use (@2))
7014 (with {
7015 unsigned int prec = TYPE_PRECISION (TREE_TYPE (@0));
7016 signop sign = TYPE_SIGN (TREE_TYPE (@0));
7017 }
7018 (out @0 { wide_int_to_tree (TREE_TYPE (@0),
7019 wi::max_value (prec, sign)
7020 - wi::to_wide (@1)); })))))
7021
7022 /* To detect overflow in unsigned A - B, A < B is simpler than A - B > A.
7023 However, the detection logic for SUB_OVERFLOW in tree-ssa-math-opts.cc
7024 expects the long form, so we restrict the transformation for now. */
7025 (for cmp (gt le)
7026 (simplify
7027 (cmp:c (minus@2 @0 @1) @0)
7028 (if (single_use (@2)
7029 && ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
7030 && TYPE_UNSIGNED (TREE_TYPE (@0)))
7031 (cmp @1 @0))))
7032
7033 /* Optimize A - B + -1 >= A into B >= A for unsigned comparisons. */
7034 (for cmp (ge lt)
7035 (simplify
7036 (cmp:c (plus (minus @0 @1) integer_minus_onep) @0)
7037 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
7038 && TYPE_UNSIGNED (TREE_TYPE (@0)))
7039 (cmp @1 @0))))
7040
7041 /* Testing for overflow is unnecessary if we already know the result. */
7042 /* A - B > A */
7043 (for cmp (gt le)
7044 out (ne eq)
7045 (simplify
7046 (cmp:c (realpart (IFN_SUB_OVERFLOW@2 @0 @1)) @0)
7047 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
7048 && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
7049 (out (imagpart @2) { build_zero_cst (TREE_TYPE (@0)); }))))
7050 /* A + B < A */
7051 (for cmp (lt ge)
7052 out (ne eq)
7053 (simplify
7054 (cmp:c (realpart (IFN_ADD_OVERFLOW:c@2 @0 @1)) @0)
7055 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
7056 && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
7057 (out (imagpart @2) { build_zero_cst (TREE_TYPE (@0)); }))))
7058
7059 /* For unsigned operands, -1 / B < A checks whether A * B would overflow.
7060 Simplify it to __builtin_mul_overflow (A, B, <unused>). */
7061 (for cmp (lt ge)
7062 out (ne eq)
7063 (simplify
7064 (cmp:c (trunc_div:s integer_all_onesp @1) @0)
7065 (if (TYPE_UNSIGNED (TREE_TYPE (@0)) && !VECTOR_TYPE_P (TREE_TYPE (@0)))
7066 (with { tree t = TREE_TYPE (@0), cpx = build_complex_type (t); }
7067 (out (imagpart (IFN_MUL_OVERFLOW:cpx @0 @1)) { build_zero_cst (t); })))))
7068
7069 /* Similarly, for unsigned operands, (((type) A * B) >> prec) != 0 where type
7070 is at least twice as wide as type of A and B, simplify to
7071 __builtin_mul_overflow (A, B, <unused>). */
7072 (for cmp (eq ne)
7073 (simplify
7074 (cmp (rshift (mult:s (convert@3 @0) (convert @1)) INTEGER_CST@2)
7075 integer_zerop)
7076 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
7077 && INTEGRAL_TYPE_P (TREE_TYPE (@3))
7078 && TYPE_UNSIGNED (TREE_TYPE (@0))
7079 && (TYPE_PRECISION (TREE_TYPE (@3))
7080 >= 2 * TYPE_PRECISION (TREE_TYPE (@0)))
7081 && tree_fits_uhwi_p (@2)
7082 && tree_to_uhwi (@2) == TYPE_PRECISION (TREE_TYPE (@0))
7083 && types_match (@0, @1)
7084 && type_has_mode_precision_p (TREE_TYPE (@0))
7085 && (optab_handler (umulv4_optab, TYPE_MODE (TREE_TYPE (@0)))
7086 != CODE_FOR_nothing))
7087 (with { tree t = TREE_TYPE (@0), cpx = build_complex_type (t); }
7088 (cmp (imagpart (IFN_MUL_OVERFLOW:cpx @0 @1)) { build_zero_cst (t); })))))
7089
7090 /* Demote operands of IFN_{ADD,SUB,MUL}_OVERFLOW. */
7091 (for ovf (IFN_ADD_OVERFLOW IFN_SUB_OVERFLOW IFN_MUL_OVERFLOW)
7092 (simplify
7093 (ovf (convert@2 @0) @1)
7094 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
7095 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
7096 && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
7097 && (!TYPE_UNSIGNED (TREE_TYPE (@2)) || TYPE_UNSIGNED (TREE_TYPE (@0))))
7098 (ovf @0 @1)))
7099 (simplify
7100 (ovf @1 (convert@2 @0))
7101 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
7102 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
7103 && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
7104 && (!TYPE_UNSIGNED (TREE_TYPE (@2)) || TYPE_UNSIGNED (TREE_TYPE (@0))))
7105 (ovf @1 @0))))
7106
7107 /* Optimize __builtin_mul_overflow_p (x, cst, (utype) 0) if all 3 types
7108 are unsigned to x > (umax / cst). Similarly for signed type, but
7109 in that case it needs to be outside of a range. */
7110 (simplify
7111 (imagpart (IFN_MUL_OVERFLOW:cs@2 @0 integer_nonzerop@1))
7112 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
7113 && TYPE_MAX_VALUE (TREE_TYPE (@0))
7114 && types_match (TREE_TYPE (@0), TREE_TYPE (TREE_TYPE (@2)))
7115 && int_fits_type_p (@1, TREE_TYPE (@0)))
7116 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
7117 (convert (gt @0 (trunc_div! { TYPE_MAX_VALUE (TREE_TYPE (@0)); } @1)))
7118 (if (TYPE_MIN_VALUE (TREE_TYPE (@0)))
7119 (if (integer_minus_onep (@1))
7120 (convert (eq @0 { TYPE_MIN_VALUE (TREE_TYPE (@0)); }))
7121 (with
7122 {
7123 tree div = fold_convert (TREE_TYPE (@0), @1);
7124 tree lo = int_const_binop (TRUNC_DIV_EXPR,
7125 TYPE_MIN_VALUE (TREE_TYPE (@0)), div);
7126 tree hi = int_const_binop (TRUNC_DIV_EXPR,
7127 TYPE_MAX_VALUE (TREE_TYPE (@0)), div);
7128 tree etype = range_check_type (TREE_TYPE (@0));
7129 if (etype)
7130 {
7131 if (wi::neg_p (wi::to_wide (div)))
7132 std::swap (lo, hi);
7133 lo = fold_convert (etype, lo);
7134 hi = fold_convert (etype, hi);
7135 hi = int_const_binop (MINUS_EXPR, hi, lo);
7136 }
7137 }
7138 (if (etype)
7139 (convert (gt (minus (convert:etype @0) { lo; }) { hi; })))))))))
7140
7141 /* Simplification of math builtins. These rules must all be optimizations
7142 as well as IL simplifications. If there is a possibility that the new
7143 form could be a pessimization, the rule should go in the canonicalization
7144 section that follows this one.
7145
7146 Rules can generally go in this section if they satisfy one of
7147 the following:
7148
7149 - the rule describes an identity
7150
7151 - the rule replaces calls with something as simple as addition or
7152 multiplication
7153
7154 - the rule contains unary calls only and simplifies the surrounding
7155 arithmetic. (The idea here is to exclude non-unary calls in which
7156 one operand is constant and in which the call is known to be cheap
7157 when the operand has that value.) */
7158
7159 (if (flag_unsafe_math_optimizations)
7160 /* Simplify sqrt(x) * sqrt(x) -> x. */
7161 (simplify
7162 (mult (SQRT_ALL@1 @0) @1)
7163 (if (!tree_expr_maybe_signaling_nan_p (@0))
7164 @0))
7165
7166 (for op (plus minus)
7167 /* Simplify (A / C) +- (B / C) -> (A +- B) / C. */
7168 (simplify
7169 (op (rdiv @0 @1)
7170 (rdiv @2 @1))
7171 (rdiv (op @0 @2) @1)))
7172
7173 (for cmp (lt le gt ge)
7174 neg_cmp (gt ge lt le)
7175 /* Simplify (x * C1) cmp C2 -> x cmp (C2 / C1), where C1 != 0. */
7176 (simplify
7177 (cmp (mult @0 REAL_CST@1) REAL_CST@2)
7178 (with
7179 { tree tem = const_binop (RDIV_EXPR, type, @2, @1); }
7180 (if (tem
7181 && !(REAL_VALUE_ISINF (TREE_REAL_CST (tem))
7182 || (real_zerop (tem) && !real_zerop (@1))))
7183 (switch
7184 (if (real_less (&dconst0, TREE_REAL_CST_PTR (@1)))
7185 (cmp @0 { tem; }))
7186 (if (real_less (TREE_REAL_CST_PTR (@1), &dconst0))
7187 (neg_cmp @0 { tem; })))))))
7188
7189 /* Simplify sqrt(x) * sqrt(y) -> sqrt(x*y). */
7190 (for root (SQRT CBRT)
7191 (simplify
7192 (mult (root:s @0) (root:s @1))
7193 (root (mult @0 @1))))
7194
7195 /* Simplify expN(x) * expN(y) -> expN(x+y). */
7196 (for exps (EXP EXP2 EXP10 POW10)
7197 (simplify
7198 (mult (exps:s @0) (exps:s @1))
7199 (exps (plus @0 @1))))
7200
7201 /* Simplify a/root(b/c) into a*root(c/b). */
7202 (for root (SQRT CBRT)
7203 (simplify
7204 (rdiv @0 (root:s (rdiv:s @1 @2)))
7205 (mult @0 (root (rdiv @2 @1)))))
7206
7207 /* Simplify x/expN(y) into x*expN(-y). */
7208 (for exps (EXP EXP2 EXP10 POW10)
7209 (simplify
7210 (rdiv @0 (exps:s @1))
7211 (mult @0 (exps (negate @1)))))
7212
7213 (for logs (LOG LOG2 LOG10 LOG10)
7214 exps (EXP EXP2 EXP10 POW10)
7215 /* logN(expN(x)) -> x. */
7216 (simplify
7217 (logs (exps @0))
7218 @0)
7219 /* expN(logN(x)) -> x. */
7220 (simplify
7221 (exps (logs @0))
7222 @0))
7223
7224 /* Optimize logN(func()) for various exponential functions. We
7225 want to determine the value "x" and the power "exponent" in
7226 order to transform logN(x**exponent) into exponent*logN(x). */
7227 (for logs (LOG LOG LOG LOG2 LOG2 LOG2 LOG10 LOG10)
7228 exps (EXP2 EXP10 POW10 EXP EXP10 POW10 EXP EXP2)
7229 (simplify
7230 (logs (exps @0))
7231 (if (SCALAR_FLOAT_TYPE_P (type))
7232 (with {
7233 tree x;
7234 switch (exps)
7235 {
7236 CASE_CFN_EXP:
7237 /* Prepare to do logN(exp(exponent)) -> exponent*logN(e). */
7238 x = build_real_truncate (type, dconst_e ());
7239 break;
7240 CASE_CFN_EXP2:
7241 /* Prepare to do logN(exp2(exponent)) -> exponent*logN(2). */
7242 x = build_real (type, dconst2);
7243 break;
7244 CASE_CFN_EXP10:
7245 CASE_CFN_POW10:
7246 /* Prepare to do logN(exp10(exponent)) -> exponent*logN(10). */
7247 {
7248 REAL_VALUE_TYPE dconst10;
7249 real_from_integer (&dconst10, VOIDmode, 10, SIGNED);
7250 x = build_real (type, dconst10);
7251 }
7252 break;
7253 default:
7254 gcc_unreachable ();
7255 }
7256 }
7257 (mult (logs { x; }) @0)))))
7258
7259 (for logs (LOG LOG
7260 LOG2 LOG2
7261 LOG10 LOG10)
7262 exps (SQRT CBRT)
7263 (simplify
7264 (logs (exps @0))
7265 (if (SCALAR_FLOAT_TYPE_P (type))
7266 (with {
7267 tree x;
7268 switch (exps)
7269 {
7270 CASE_CFN_SQRT:
7271 /* Prepare to do logN(sqrt(x)) -> 0.5*logN(x). */
7272 x = build_real (type, dconsthalf);
7273 break;
7274 CASE_CFN_CBRT:
7275 /* Prepare to do logN(cbrt(x)) -> (1/3)*logN(x). */
7276 x = build_real_truncate (type, dconst_third ());
7277 break;
7278 default:
7279 gcc_unreachable ();
7280 }
7281 }
7282 (mult { x; } (logs @0))))))
7283
7284 /* logN(pow(x,exponent)) -> exponent*logN(x). */
7285 (for logs (LOG LOG2 LOG10)
7286 pows (POW)
7287 (simplify
7288 (logs (pows @0 @1))
7289 (mult @1 (logs @0))))
7290
7291 /* pow(C,x) -> exp(log(C)*x) if C > 0,
7292 or if C is a positive power of 2,
7293 pow(C,x) -> exp2(log2(C)*x). */
7294 #if GIMPLE
7295 (for pows (POW)
7296 exps (EXP)
7297 logs (LOG)
7298 exp2s (EXP2)
7299 log2s (LOG2)
7300 (simplify
7301 (pows REAL_CST@0 @1)
7302 (if (real_compare (GT_EXPR, TREE_REAL_CST_PTR (@0), &dconst0)
7303 && real_isfinite (TREE_REAL_CST_PTR (@0))
7304 /* As libmvec doesn't have a vectorized exp2, defer optimizing
7305 the use_exp2 case until after vectorization. It seems actually
7306 beneficial for all constants to postpone this until later,
7307 because exp(log(C)*x), while faster, will have worse precision
7308 and if x folds into a constant too, that is unnecessary
7309 pessimization. */
7310 && canonicalize_math_after_vectorization_p ())
7311 (with {
7312 const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (@0);
7313 bool use_exp2 = false;
7314 if (targetm.libc_has_function (function_c99_misc, TREE_TYPE (@0))
7315 && value->cl == rvc_normal)
7316 {
7317 REAL_VALUE_TYPE frac_rvt = *value;
7318 SET_REAL_EXP (&frac_rvt, 1);
7319 if (real_equal (&frac_rvt, &dconst1))
7320 use_exp2 = true;
7321 }
7322 }
7323 (if (!use_exp2)
7324 (if (optimize_pow_to_exp (@0, @1))
7325 (exps (mult (logs @0) @1)))
7326 (exp2s (mult (log2s @0) @1)))))))
7327 #endif
7328
7329 /* pow(C,x)*expN(y) -> expN(logN(C)*x+y) if C > 0. */
7330 (for pows (POW)
7331 exps (EXP EXP2 EXP10 POW10)
7332 logs (LOG LOG2 LOG10 LOG10)
7333 (simplify
7334 (mult:c (pows:s REAL_CST@0 @1) (exps:s @2))
7335 (if (real_compare (GT_EXPR, TREE_REAL_CST_PTR (@0), &dconst0)
7336 && real_isfinite (TREE_REAL_CST_PTR (@0)))
7337 (exps (plus (mult (logs @0) @1) @2)))))
7338
7339 (for sqrts (SQRT)
7340 cbrts (CBRT)
7341 pows (POW)
7342 exps (EXP EXP2 EXP10 POW10)
7343 /* sqrt(expN(x)) -> expN(x*0.5). */
7344 (simplify
7345 (sqrts (exps @0))
7346 (exps (mult @0 { build_real (type, dconsthalf); })))
7347 /* cbrt(expN(x)) -> expN(x/3). */
7348 (simplify
7349 (cbrts (exps @0))
7350 (exps (mult @0 { build_real_truncate (type, dconst_third ()); })))
7351 /* pow(expN(x), y) -> expN(x*y). */
7352 (simplify
7353 (pows (exps @0) @1)
7354 (exps (mult @0 @1))))
7355
7356 /* tan(atan(x)) -> x. */
7357 (for tans (TAN)
7358 atans (ATAN)
7359 (simplify
7360 (tans (atans @0))
7361 @0)))
7362
7363 /* Simplify sin(atan(x)) -> x / sqrt(x*x + 1). */
7364 (for sins (SIN)
7365 atans (ATAN)
7366 sqrts (SQRT)
7367 copysigns (COPYSIGN)
7368 (simplify
7369 (sins (atans:s @0))
7370 (with
7371 {
7372 REAL_VALUE_TYPE r_cst;
7373 build_sinatan_real (&r_cst, type);
7374 tree t_cst = build_real (type, r_cst);
7375 tree t_one = build_one_cst (type);
7376 }
7377 (if (SCALAR_FLOAT_TYPE_P (type))
7378 (cond (lt (abs @0) { t_cst; })
7379 (rdiv @0 (sqrts (plus (mult @0 @0) { t_one; })))
7380 (copysigns { t_one; } @0))))))
7381
7382 /* Simplify cos(atan(x)) -> 1 / sqrt(x*x + 1). */
7383 (for coss (COS)
7384 atans (ATAN)
7385 sqrts (SQRT)
7386 copysigns (COPYSIGN)
7387 (simplify
7388 (coss (atans:s @0))
7389 (with
7390 {
7391 REAL_VALUE_TYPE r_cst;
7392 build_sinatan_real (&r_cst, type);
7393 tree t_cst = build_real (type, r_cst);
7394 tree t_one = build_one_cst (type);
7395 tree t_zero = build_zero_cst (type);
7396 }
7397 (if (SCALAR_FLOAT_TYPE_P (type))
7398 (cond (lt (abs @0) { t_cst; })
7399 (rdiv { t_one; } (sqrts (plus (mult @0 @0) { t_one; })))
7400 (copysigns { t_zero; } @0))))))
7401
7402 (if (!flag_errno_math)
7403 /* Simplify sinh(atanh(x)) -> x / sqrt((1 - x)*(1 + x)). */
7404 (for sinhs (SINH)
7405 atanhs (ATANH)
7406 sqrts (SQRT)
7407 (simplify
7408 (sinhs (atanhs:s @0))
7409 (with { tree t_one = build_one_cst (type); }
7410 (rdiv @0 (sqrts (mult (minus { t_one; } @0) (plus { t_one; } @0)))))))
7411
7412 /* Simplify cosh(atanh(x)) -> 1 / sqrt((1 - x)*(1 + x)) */
7413 (for coshs (COSH)
7414 atanhs (ATANH)
7415 sqrts (SQRT)
7416 (simplify
7417 (coshs (atanhs:s @0))
7418 (with { tree t_one = build_one_cst (type); }
7419 (rdiv { t_one; } (sqrts (mult (minus { t_one; } @0) (plus { t_one; } @0))))))))
7420
7421 /* cabs(x+0i) or cabs(0+xi) -> abs(x). */
7422 (simplify
7423 (CABS (complex:C @0 real_zerop@1))
7424 (abs @0))
7425
7426 /* trunc(trunc(x)) -> trunc(x), etc. */
7427 (for fns (TRUNC_ALL FLOOR_ALL CEIL_ALL ROUND_ALL NEARBYINT_ALL RINT_ALL)
7428 (simplify
7429 (fns (fns @0))
7430 (fns @0)))
7431 /* f(x) -> x if x is integer valued and f does nothing for such values. */
7432 (for fns (TRUNC_ALL FLOOR_ALL CEIL_ALL ROUND_ALL NEARBYINT_ALL RINT_ALL)
7433 (simplify
7434 (fns integer_valued_real_p@0)
7435 @0))
7436
7437 /* hypot(x,0) and hypot(0,x) -> abs(x). */
7438 (simplify
7439 (HYPOT:c @0 real_zerop@1)
7440 (abs @0))
7441
7442 /* pow(1,x) -> 1. */
7443 (simplify
7444 (POW real_onep@0 @1)
7445 @0)
7446
7447 (simplify
7448 /* copysign(x,x) -> x. */
7449 (COPYSIGN_ALL @0 @0)
7450 @0)
7451
7452 (simplify
7453 /* copysign(x,-x) -> -x. */
7454 (COPYSIGN_ALL @0 (negate@1 @0))
7455 @1)
7456
7457 (simplify
7458 /* copysign(x,y) -> fabs(x) if y is nonnegative. */
7459 (COPYSIGN_ALL @0 tree_expr_nonnegative_p@1)
7460 (abs @0))
7461
7462 (simplify
7463 /* fabs (copysign(x, y)) -> fabs (x). */
7464 (abs (COPYSIGN_ALL @0 @1))
7465 (abs @0))
7466
7467 (for scale (LDEXP SCALBN SCALBLN)
7468 /* ldexp(0, x) -> 0. */
7469 (simplify
7470 (scale real_zerop@0 @1)
7471 @0)
7472 /* ldexp(x, 0) -> x. */
7473 (simplify
7474 (scale @0 integer_zerop@1)
7475 @0)
7476 /* ldexp(x, y) -> x if x is +-Inf or NaN. */
7477 (simplify
7478 (scale REAL_CST@0 @1)
7479 (if (!real_isfinite (TREE_REAL_CST_PTR (@0)))
7480 @0)))
7481
7482 /* Canonicalization of sequences of math builtins. These rules represent
7483 IL simplifications but are not necessarily optimizations.
7484
7485 The sincos pass is responsible for picking "optimal" implementations
7486 of math builtins, which may be more complicated and can sometimes go
7487 the other way, e.g. converting pow into a sequence of sqrts.
7488 We only want to do these canonicalizations before the pass has run. */
7489
7490 (if (flag_unsafe_math_optimizations && canonicalize_math_p ())
7491 /* Simplify tan(x) * cos(x) -> sin(x). */
7492 (simplify
7493 (mult:c (TAN:s @0) (COS:s @0))
7494 (SIN @0))
7495
7496 /* Simplify x * pow(x,c) -> pow(x,c+1). */
7497 (simplify
7498 (mult:c @0 (POW:s @0 REAL_CST@1))
7499 (if (!TREE_OVERFLOW (@1))
7500 (POW @0 (plus @1 { build_one_cst (type); }))))
7501
7502 /* Simplify sin(x) / cos(x) -> tan(x). */
7503 (simplify
7504 (rdiv (SIN:s @0) (COS:s @0))
7505 (TAN @0))
7506
7507 /* Simplify sinh(x) / cosh(x) -> tanh(x). */
7508 (simplify
7509 (rdiv (SINH:s @0) (COSH:s @0))
7510 (TANH @0))
7511
7512 /* Simplify tanh (x) / sinh (x) -> 1.0 / cosh (x). */
7513 (simplify
7514 (rdiv (TANH:s @0) (SINH:s @0))
7515 (rdiv {build_one_cst (type);} (COSH @0)))
7516
7517 /* Simplify cos(x) / sin(x) -> 1 / tan(x). */
7518 (simplify
7519 (rdiv (COS:s @0) (SIN:s @0))
7520 (rdiv { build_one_cst (type); } (TAN @0)))
7521
7522 /* Simplify sin(x) / tan(x) -> cos(x). */
7523 (simplify
7524 (rdiv (SIN:s @0) (TAN:s @0))
7525 (if (! HONOR_NANS (@0)
7526 && ! HONOR_INFINITIES (@0))
7527 (COS @0)))
7528
7529 /* Simplify tan(x) / sin(x) -> 1.0 / cos(x). */
7530 (simplify
7531 (rdiv (TAN:s @0) (SIN:s @0))
7532 (if (! HONOR_NANS (@0)
7533 && ! HONOR_INFINITIES (@0))
7534 (rdiv { build_one_cst (type); } (COS @0))))
7535
7536 /* Simplify pow(x,y) * pow(x,z) -> pow(x,y+z). */
7537 (simplify
7538 (mult (POW:s @0 @1) (POW:s @0 @2))
7539 (POW @0 (plus @1 @2)))
7540
7541 /* Simplify pow(x,y) * pow(z,y) -> pow(x*z,y). */
7542 (simplify
7543 (mult (POW:s @0 @1) (POW:s @2 @1))
7544 (POW (mult @0 @2) @1))
7545
7546 /* Simplify powi(x,y) * powi(z,y) -> powi(x*z,y). */
7547 (simplify
7548 (mult (POWI:s @0 @1) (POWI:s @2 @1))
7549 (POWI (mult @0 @2) @1))
7550
7551 /* Simplify pow(x,c) / x -> pow(x,c-1). */
7552 (simplify
7553 (rdiv (POW:s @0 REAL_CST@1) @0)
7554 (if (!TREE_OVERFLOW (@1))
7555 (POW @0 (minus @1 { build_one_cst (type); }))))
7556
7557 /* Simplify x / pow (y,z) -> x * pow(y,-z). */
7558 (simplify
7559 (rdiv @0 (POW:s @1 @2))
7560 (mult @0 (POW @1 (negate @2))))
7561
7562 (for sqrts (SQRT)
7563 cbrts (CBRT)
7564 pows (POW)
7565 /* sqrt(sqrt(x)) -> pow(x,1/4). */
7566 (simplify
7567 (sqrts (sqrts @0))
7568 (pows @0 { build_real (type, dconst_quarter ()); }))
7569 /* sqrt(cbrt(x)) -> pow(x,1/6). */
7570 (simplify
7571 (sqrts (cbrts @0))
7572 (pows @0 { build_real_truncate (type, dconst_sixth ()); }))
7573 /* cbrt(sqrt(x)) -> pow(x,1/6). */
7574 (simplify
7575 (cbrts (sqrts @0))
7576 (pows @0 { build_real_truncate (type, dconst_sixth ()); }))
7577 /* cbrt(cbrt(x)) -> pow(x,1/9), iff x is nonnegative. */
7578 (simplify
7579 (cbrts (cbrts tree_expr_nonnegative_p@0))
7580 (pows @0 { build_real_truncate (type, dconst_ninth ()); }))
7581 /* sqrt(pow(x,y)) -> pow(|x|,y*0.5). */
7582 (simplify
7583 (sqrts (pows @0 @1))
7584 (pows (abs @0) (mult @1 { build_real (type, dconsthalf); })))
7585 /* cbrt(pow(x,y)) -> pow(x,y/3), iff x is nonnegative. */
7586 (simplify
7587 (cbrts (pows tree_expr_nonnegative_p@0 @1))
7588 (pows @0 (mult @1 { build_real_truncate (type, dconst_third ()); })))
7589 /* pow(sqrt(x),y) -> pow(x,y*0.5). */
7590 (simplify
7591 (pows (sqrts @0) @1)
7592 (pows @0 (mult @1 { build_real (type, dconsthalf); })))
7593 /* pow(cbrt(x),y) -> pow(x,y/3) iff x is nonnegative. */
7594 (simplify
7595 (pows (cbrts tree_expr_nonnegative_p@0) @1)
7596 (pows @0 (mult @1 { build_real_truncate (type, dconst_third ()); })))
7597 /* pow(pow(x,y),z) -> pow(x,y*z) iff x is nonnegative. */
7598 (simplify
7599 (pows (pows tree_expr_nonnegative_p@0 @1) @2)
7600 (pows @0 (mult @1 @2))))
7601
7602 /* cabs(x+xi) -> fabs(x)*sqrt(2). */
7603 (simplify
7604 (CABS (complex @0 @0))
7605 (mult (abs @0) { build_real_truncate (type, dconst_sqrt2 ()); }))
7606
7607 /* hypot(x,x) -> fabs(x)*sqrt(2). */
7608 (simplify
7609 (HYPOT @0 @0)
7610 (mult (abs @0) { build_real_truncate (type, dconst_sqrt2 ()); }))
7611
7612 /* cexp(x+yi) -> exp(x)*cexpi(y). */
7613 (for cexps (CEXP)
7614 exps (EXP)
7615 cexpis (CEXPI)
7616 (simplify
7617 (cexps compositional_complex@0)
7618 (if (targetm.libc_has_function (function_c99_math_complex, TREE_TYPE (@0)))
7619 (complex
7620 (mult (exps@1 (realpart @0)) (realpart (cexpis:type@2 (imagpart @0))))
7621 (mult @1 (imagpart @2)))))))
7622
7623 (if (canonicalize_math_p ())
7624 /* floor(x) -> trunc(x) if x is nonnegative. */
7625 (for floors (FLOOR_ALL)
7626 truncs (TRUNC_ALL)
7627 (simplify
7628 (floors tree_expr_nonnegative_p@0)
7629 (truncs @0))))
7630
7631 (match double_value_p
7632 @0
7633 (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == double_type_node)))
7634 (for froms (BUILT_IN_TRUNCL
7635 BUILT_IN_FLOORL
7636 BUILT_IN_CEILL
7637 BUILT_IN_ROUNDL
7638 BUILT_IN_NEARBYINTL
7639 BUILT_IN_RINTL)
7640 tos (BUILT_IN_TRUNC
7641 BUILT_IN_FLOOR
7642 BUILT_IN_CEIL
7643 BUILT_IN_ROUND
7644 BUILT_IN_NEARBYINT
7645 BUILT_IN_RINT)
7646 /* truncl(extend(x)) -> extend(trunc(x)), etc., if x is a double. */
7647 (if (optimize && canonicalize_math_p ())
7648 (simplify
7649 (froms (convert double_value_p@0))
7650 (convert (tos @0)))))
7651
7652 (match float_value_p
7653 @0
7654 (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == float_type_node)))
7655 (for froms (BUILT_IN_TRUNCL BUILT_IN_TRUNC
7656 BUILT_IN_FLOORL BUILT_IN_FLOOR
7657 BUILT_IN_CEILL BUILT_IN_CEIL
7658 BUILT_IN_ROUNDL BUILT_IN_ROUND
7659 BUILT_IN_NEARBYINTL BUILT_IN_NEARBYINT
7660 BUILT_IN_RINTL BUILT_IN_RINT)
7661 tos (BUILT_IN_TRUNCF BUILT_IN_TRUNCF
7662 BUILT_IN_FLOORF BUILT_IN_FLOORF
7663 BUILT_IN_CEILF BUILT_IN_CEILF
7664 BUILT_IN_ROUNDF BUILT_IN_ROUNDF
7665 BUILT_IN_NEARBYINTF BUILT_IN_NEARBYINTF
7666 BUILT_IN_RINTF BUILT_IN_RINTF)
7667 /* truncl(extend(x)) and trunc(extend(x)) -> extend(truncf(x)), etc.,
7668 if x is a float. */
7669 (if (optimize && canonicalize_math_p ()
7670 && targetm.libc_has_function (function_c99_misc, NULL_TREE))
7671 (simplify
7672 (froms (convert float_value_p@0))
7673 (convert (tos @0)))))
7674
7675 #if GIMPLE
7676 (match float16_value_p
7677 @0
7678 (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == float16_type_node)))
7679 (for froms (BUILT_IN_TRUNCL BUILT_IN_TRUNC BUILT_IN_TRUNCF
7680 BUILT_IN_FLOORL BUILT_IN_FLOOR BUILT_IN_FLOORF
7681 BUILT_IN_CEILL BUILT_IN_CEIL BUILT_IN_CEILF
7682 BUILT_IN_ROUNDEVENL BUILT_IN_ROUNDEVEN BUILT_IN_ROUNDEVENF
7683 BUILT_IN_ROUNDL BUILT_IN_ROUND BUILT_IN_ROUNDF
7684 BUILT_IN_NEARBYINTL BUILT_IN_NEARBYINT BUILT_IN_NEARBYINTF
7685 BUILT_IN_RINTL BUILT_IN_RINT BUILT_IN_RINTF
7686 BUILT_IN_SQRTL BUILT_IN_SQRT BUILT_IN_SQRTF)
7687 tos (IFN_TRUNC IFN_TRUNC IFN_TRUNC
7688 IFN_FLOOR IFN_FLOOR IFN_FLOOR
7689 IFN_CEIL IFN_CEIL IFN_CEIL
7690 IFN_ROUNDEVEN IFN_ROUNDEVEN IFN_ROUNDEVEN
7691 IFN_ROUND IFN_ROUND IFN_ROUND
7692 IFN_NEARBYINT IFN_NEARBYINT IFN_NEARBYINT
7693 IFN_RINT IFN_RINT IFN_RINT
7694 IFN_SQRT IFN_SQRT IFN_SQRT)
7695 /* (_Float16) round ((doube) x) -> __built_in_roundf16 (x), etc.,
7696 if x is a _Float16. */
7697 (simplify
7698 (convert (froms (convert float16_value_p@0)))
7699 (if (optimize
7700 && types_match (type, TREE_TYPE (@0))
7701 && direct_internal_fn_supported_p (as_internal_fn (tos),
7702 type, OPTIMIZE_FOR_BOTH))
7703 (tos @0))))
7704
7705 /* Simplify (trunc)copysign ((extend)x, (extend)y) to copysignf (x, y),
7706 x,y is float value, similar for _Float16/double. */
7707 (for copysigns (COPYSIGN_ALL)
7708 (simplify
7709 (convert (copysigns (convert@2 @0) (convert @1)))
7710 (if (optimize
7711 && !HONOR_SNANS (@2)
7712 && types_match (type, TREE_TYPE (@0))
7713 && types_match (type, TREE_TYPE (@1))
7714 && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (@2))
7715 && direct_internal_fn_supported_p (IFN_COPYSIGN,
7716 type, OPTIMIZE_FOR_BOTH))
7717 (IFN_COPYSIGN @0 @1))))
7718
7719 (for froms (BUILT_IN_FMAF BUILT_IN_FMA BUILT_IN_FMAL)
7720 tos (IFN_FMA IFN_FMA IFN_FMA)
7721 (simplify
7722 (convert (froms (convert@3 @0) (convert @1) (convert @2)))
7723 (if (flag_unsafe_math_optimizations
7724 && optimize
7725 && FLOAT_TYPE_P (type)
7726 && FLOAT_TYPE_P (TREE_TYPE (@3))
7727 && types_match (type, TREE_TYPE (@0))
7728 && types_match (type, TREE_TYPE (@1))
7729 && types_match (type, TREE_TYPE (@2))
7730 && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (@3))
7731 && direct_internal_fn_supported_p (as_internal_fn (tos),
7732 type, OPTIMIZE_FOR_BOTH))
7733 (tos @0 @1 @2))))
7734
7735 (for maxmin (max min)
7736 (simplify
7737 (convert (maxmin (convert@2 @0) (convert @1)))
7738 (if (optimize
7739 && FLOAT_TYPE_P (type)
7740 && FLOAT_TYPE_P (TREE_TYPE (@2))
7741 && types_match (type, TREE_TYPE (@0))
7742 && types_match (type, TREE_TYPE (@1))
7743 && element_precision (type) < element_precision (TREE_TYPE (@2)))
7744 (maxmin @0 @1))))
7745 #endif
7746
7747 (for froms (XFLOORL XCEILL XROUNDL XRINTL)
7748 tos (XFLOOR XCEIL XROUND XRINT)
7749 /* llfloorl(extend(x)) -> llfloor(x), etc., if x is a double. */
7750 (if (optimize && canonicalize_math_p ())
7751 (simplify
7752 (froms (convert double_value_p@0))
7753 (tos @0))))
7754
7755 (for froms (XFLOORL XCEILL XROUNDL XRINTL
7756 XFLOOR XCEIL XROUND XRINT)
7757 tos (XFLOORF XCEILF XROUNDF XRINTF)
7758 /* llfloorl(extend(x)) and llfloor(extend(x)) -> llfloorf(x), etc.,
7759 if x is a float. */
7760 (if (optimize && canonicalize_math_p ())
7761 (simplify
7762 (froms (convert float_value_p@0))
7763 (tos @0))))
7764
7765 (if (canonicalize_math_p ())
7766 /* xfloor(x) -> fix_trunc(x) if x is nonnegative. */
7767 (for floors (IFLOOR LFLOOR LLFLOOR)
7768 (simplify
7769 (floors tree_expr_nonnegative_p@0)
7770 (fix_trunc @0))))
7771
7772 (if (canonicalize_math_p ())
7773 /* xfloor(x) -> fix_trunc(x), etc., if x is integer valued. */
7774 (for fns (IFLOOR LFLOOR LLFLOOR
7775 ICEIL LCEIL LLCEIL
7776 IROUND LROUND LLROUND)
7777 (simplify
7778 (fns integer_valued_real_p@0)
7779 (fix_trunc @0)))
7780 (if (!flag_errno_math)
7781 /* xrint(x) -> fix_trunc(x), etc., if x is integer valued. */
7782 (for rints (IRINT LRINT LLRINT)
7783 (simplify
7784 (rints integer_valued_real_p@0)
7785 (fix_trunc @0)))))
7786
7787 (if (canonicalize_math_p ())
7788 (for ifn (IFLOOR ICEIL IROUND IRINT)
7789 lfn (LFLOOR LCEIL LROUND LRINT)
7790 llfn (LLFLOOR LLCEIL LLROUND LLRINT)
7791 /* Canonicalize iround (x) to lround (x) on ILP32 targets where
7792 sizeof (int) == sizeof (long). */
7793 (if (TYPE_PRECISION (integer_type_node)
7794 == TYPE_PRECISION (long_integer_type_node))
7795 (simplify
7796 (ifn @0)
7797 (lfn:long_integer_type_node @0)))
7798 /* Canonicalize llround (x) to lround (x) on LP64 targets where
7799 sizeof (long long) == sizeof (long). */
7800 (if (TYPE_PRECISION (long_long_integer_type_node)
7801 == TYPE_PRECISION (long_integer_type_node))
7802 (simplify
7803 (llfn @0)
7804 (lfn:long_integer_type_node @0)))))
7805
7806 /* cproj(x) -> x if we're ignoring infinities. */
7807 (simplify
7808 (CPROJ @0)
7809 (if (!HONOR_INFINITIES (type))
7810 @0))
7811
7812 /* If the real part is inf and the imag part is known to be
7813 nonnegative, return (inf + 0i). */
7814 (simplify
7815 (CPROJ (complex REAL_CST@0 tree_expr_nonnegative_p@1))
7816 (if (real_isinf (TREE_REAL_CST_PTR (@0)))
7817 { build_complex_inf (type, false); }))
7818
7819 /* If the imag part is inf, return (inf+I*copysign(0,imag)). */
7820 (simplify
7821 (CPROJ (complex @0 REAL_CST@1))
7822 (if (real_isinf (TREE_REAL_CST_PTR (@1)))
7823 { build_complex_inf (type, TREE_REAL_CST_PTR (@1)->sign); }))
7824
7825 (for pows (POW)
7826 sqrts (SQRT)
7827 cbrts (CBRT)
7828 (simplify
7829 (pows @0 REAL_CST@1)
7830 (with {
7831 const REAL_VALUE_TYPE *value = TREE_REAL_CST_PTR (@1);
7832 REAL_VALUE_TYPE tmp;
7833 }
7834 (switch
7835 /* pow(x,0) -> 1. */
7836 (if (real_equal (value, &dconst0))
7837 { build_real (type, dconst1); })
7838 /* pow(x,1) -> x. */
7839 (if (real_equal (value, &dconst1))
7840 @0)
7841 /* pow(x,-1) -> 1/x. */
7842 (if (real_equal (value, &dconstm1))
7843 (rdiv { build_real (type, dconst1); } @0))
7844 /* pow(x,0.5) -> sqrt(x). */
7845 (if (flag_unsafe_math_optimizations
7846 && canonicalize_math_p ()
7847 && real_equal (value, &dconsthalf))
7848 (sqrts @0))
7849 /* pow(x,1/3) -> cbrt(x). */
7850 (if (flag_unsafe_math_optimizations
7851 && canonicalize_math_p ()
7852 && (tmp = real_value_truncate (TYPE_MODE (type), dconst_third ()),
7853 real_equal (value, &tmp)))
7854 (cbrts @0))))))
7855
7856 /* powi(1,x) -> 1. */
7857 (simplify
7858 (POWI real_onep@0 @1)
7859 @0)
7860
7861 (simplify
7862 (POWI @0 INTEGER_CST@1)
7863 (switch
7864 /* powi(x,0) -> 1. */
7865 (if (wi::to_wide (@1) == 0)
7866 { build_real (type, dconst1); })
7867 /* powi(x,1) -> x. */
7868 (if (wi::to_wide (@1) == 1)
7869 @0)
7870 /* powi(x,-1) -> 1/x. */
7871 (if (wi::to_wide (@1) == -1)
7872 (rdiv { build_real (type, dconst1); } @0))))
7873
7874 /* Narrowing of arithmetic and logical operations.
7875
7876 These are conceptually similar to the transformations performed for
7877 the C/C++ front-ends by shorten_binary_op and shorten_compare. Long
7878 term we want to move all that code out of the front-ends into here. */
7879
7880 /* Convert (outertype)((innertype0)a+(innertype1)b)
7881 into ((newtype)a+(newtype)b) where newtype
7882 is the widest mode from all of these. */
7883 (for op (plus minus mult rdiv)
7884 (simplify
7885 (convert (op:s@0 (convert1?@3 @1) (convert2?@4 @2)))
7886 /* If we have a narrowing conversion of an arithmetic operation where
7887 both operands are widening conversions from the same type as the outer
7888 narrowing conversion. Then convert the innermost operands to a
7889 suitable unsigned type (to avoid introducing undefined behavior),
7890 perform the operation and convert the result to the desired type. */
7891 (if (INTEGRAL_TYPE_P (type)
7892 && op != MULT_EXPR
7893 && op != RDIV_EXPR
7894 /* We check for type compatibility between @0 and @1 below,
7895 so there's no need to check that @2/@4 are integral types. */
7896 && INTEGRAL_TYPE_P (TREE_TYPE (@1))
7897 && INTEGRAL_TYPE_P (TREE_TYPE (@3))
7898 /* The precision of the type of each operand must match the
7899 precision of the mode of each operand, similarly for the
7900 result. */
7901 && type_has_mode_precision_p (TREE_TYPE (@1))
7902 && type_has_mode_precision_p (TREE_TYPE (@2))
7903 && type_has_mode_precision_p (type)
7904 /* The inner conversion must be a widening conversion. */
7905 && TYPE_PRECISION (TREE_TYPE (@3)) > TYPE_PRECISION (TREE_TYPE (@1))
7906 && types_match (@1, type)
7907 && (types_match (@1, @2)
7908 /* Or the second operand is const integer or converted const
7909 integer from valueize. */
7910 || poly_int_tree_p (@4)))
7911 (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
7912 (op @1 (convert @2))
7913 (with { tree utype = unsigned_type_for (TREE_TYPE (@1)); }
7914 (convert (op (convert:utype @1)
7915 (convert:utype @2)))))
7916 (if (FLOAT_TYPE_P (type)
7917 && DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0))
7918 == DECIMAL_FLOAT_TYPE_P (type))
7919 (with { tree arg0 = strip_float_extensions (@1);
7920 tree arg1 = strip_float_extensions (@2);
7921 tree itype = TREE_TYPE (@0);
7922 tree ty1 = TREE_TYPE (arg0);
7923 tree ty2 = TREE_TYPE (arg1);
7924 enum tree_code code = TREE_CODE (itype); }
7925 (if (FLOAT_TYPE_P (ty1)
7926 && FLOAT_TYPE_P (ty2))
7927 (with { tree newtype = type;
7928 if (TYPE_MODE (ty1) == SDmode
7929 || TYPE_MODE (ty2) == SDmode
7930 || TYPE_MODE (type) == SDmode)
7931 newtype = dfloat32_type_node;
7932 if (TYPE_MODE (ty1) == DDmode
7933 || TYPE_MODE (ty2) == DDmode
7934 || TYPE_MODE (type) == DDmode)
7935 newtype = dfloat64_type_node;
7936 if (TYPE_MODE (ty1) == TDmode
7937 || TYPE_MODE (ty2) == TDmode
7938 || TYPE_MODE (type) == TDmode)
7939 newtype = dfloat128_type_node; }
7940 (if ((newtype == dfloat32_type_node
7941 || newtype == dfloat64_type_node
7942 || newtype == dfloat128_type_node)
7943 && newtype == type
7944 && types_match (newtype, type))
7945 (op (convert:newtype @1) (convert:newtype @2))
7946 (with { if (element_precision (ty1) > element_precision (newtype))
7947 newtype = ty1;
7948 if (element_precision (ty2) > element_precision (newtype))
7949 newtype = ty2; }
7950 /* Sometimes this transformation is safe (cannot
7951 change results through affecting double rounding
7952 cases) and sometimes it is not. If NEWTYPE is
7953 wider than TYPE, e.g. (float)((long double)double
7954 + (long double)double) converted to
7955 (float)(double + double), the transformation is
7956 unsafe regardless of the details of the types
7957 involved; double rounding can arise if the result
7958 of NEWTYPE arithmetic is a NEWTYPE value half way
7959 between two representable TYPE values but the
7960 exact value is sufficiently different (in the
7961 right direction) for this difference to be
7962 visible in ITYPE arithmetic. If NEWTYPE is the
7963 same as TYPE, however, the transformation may be
7964 safe depending on the types involved: it is safe
7965 if the ITYPE has strictly more than twice as many
7966 mantissa bits as TYPE, can represent infinities
7967 and NaNs if the TYPE can, and has sufficient
7968 exponent range for the product or ratio of two
7969 values representable in the TYPE to be within the
7970 range of normal values of ITYPE. */
7971 (if (element_precision (newtype) < element_precision (itype)
7972 && (!VECTOR_MODE_P (TYPE_MODE (newtype))
7973 || target_supports_op_p (newtype, op, optab_default))
7974 && (flag_unsafe_math_optimizations
7975 || (element_precision (newtype) == element_precision (type)
7976 && real_can_shorten_arithmetic (element_mode (itype),
7977 element_mode (type))
7978 && !excess_precision_type (newtype)))
7979 && !types_match (itype, newtype))
7980 (convert:type (op (convert:newtype @1)
7981 (convert:newtype @2)))
7982 )))) )
7983 ))
7984 )))
7985
7986 /* This is another case of narrowing, specifically when there's an outer
7987 BIT_AND_EXPR which masks off bits outside the type of the innermost
7988 operands. Like the previous case we have to convert the operands
7989 to unsigned types to avoid introducing undefined behavior for the
7990 arithmetic operation. */
7991 (for op (minus plus)
7992 (simplify
7993 (bit_and (op:s (convert@2 @0) (convert@3 @1)) INTEGER_CST@4)
7994 (if (INTEGRAL_TYPE_P (type)
7995 /* We check for type compatibility between @0 and @1 below,
7996 so there's no need to check that @1/@3 are integral types. */
7997 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
7998 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
7999 /* The precision of the type of each operand must match the
8000 precision of the mode of each operand, similarly for the
8001 result. */
8002 && type_has_mode_precision_p (TREE_TYPE (@0))
8003 && type_has_mode_precision_p (TREE_TYPE (@1))
8004 && type_has_mode_precision_p (type)
8005 /* The inner conversion must be a widening conversion. */
8006 && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
8007 && types_match (@0, @1)
8008 && (tree_int_cst_min_precision (@4, TYPE_SIGN (TREE_TYPE (@0)))
8009 <= TYPE_PRECISION (TREE_TYPE (@0)))
8010 && (wi::to_wide (@4)
8011 & wi::mask (TYPE_PRECISION (TREE_TYPE (@0)),
8012 true, TYPE_PRECISION (type))) == 0)
8013 (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
8014 (with { tree ntype = TREE_TYPE (@0); }
8015 (convert (bit_and (op @0 @1) (convert:ntype @4))))
8016 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
8017 (convert (bit_and (op (convert:utype @0) (convert:utype @1))
8018 (convert:utype @4))))))))
8019
8020 /* Transform (@0 < @1 and @0 < @2) to use min,
8021 (@0 > @1 and @0 > @2) to use max */
8022 (for logic (bit_and bit_and bit_and bit_and bit_ior bit_ior bit_ior bit_ior)
8023 op (lt le gt ge lt le gt ge )
8024 ext (min min max max max max min min )
8025 (simplify
8026 (logic (op:cs @0 @1) (op:cs @0 @2))
8027 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
8028 && TREE_CODE (@0) != INTEGER_CST)
8029 (op @0 (ext @1 @2)))))
8030
8031 /* Max<bool0, bool1> -> bool0 | bool1
8032 Min<bool0, bool1> -> bool0 & bool1 */
8033 (for op (max min)
8034 logic (bit_ior bit_and)
8035 (simplify
8036 (op zero_one_valued_p@0 zero_one_valued_p@1)
8037 (logic @0 @1)))
8038
8039 /* signbit(x) != 0 ? -x : x -> abs(x)
8040 signbit(x) == 0 ? -x : x -> -abs(x) */
8041 (for sign (SIGNBIT)
8042 (for neeq (ne eq)
8043 (simplify
8044 (cond (neeq (sign @0) integer_zerop) (negate @0) @0)
8045 (if (neeq == NE_EXPR)
8046 (abs @0)
8047 (negate (abs @0))))))
8048
8049 (simplify
8050 /* signbit(x) -> 0 if x is nonnegative. */
8051 (SIGNBIT tree_expr_nonnegative_p@0)
8052 { integer_zero_node; })
8053
8054 (simplify
8055 /* signbit(x) -> x<0 if x doesn't have signed zeros. */
8056 (SIGNBIT @0)
8057 (if (!HONOR_SIGNED_ZEROS (@0))
8058 (convert (lt @0 { build_real (TREE_TYPE (@0), dconst0); }))))
8059
8060 /* Transform comparisons of the form X +- C1 CMP C2 to X CMP C2 -+ C1. */
8061 (for cmp (eq ne)
8062 (for op (plus minus)
8063 rop (minus plus)
8064 (simplify
8065 (cmp (op@3 @0 INTEGER_CST@1) INTEGER_CST@2)
8066 (if (!TREE_OVERFLOW (@1) && !TREE_OVERFLOW (@2)
8067 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0))
8068 && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@0))
8069 && !TYPE_SATURATING (TREE_TYPE (@0)))
8070 (with { tree res = int_const_binop (rop, @2, @1); }
8071 (if (TREE_OVERFLOW (res)
8072 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
8073 { constant_boolean_node (cmp == NE_EXPR, type); }
8074 (if (single_use (@3))
8075 (cmp @0 { TREE_OVERFLOW (res)
8076 ? drop_tree_overflow (res) : res; }))))))))
8077 (for cmp (lt le gt ge)
8078 (for op (plus minus)
8079 rop (minus plus)
8080 (simplify
8081 (cmp (op@3 @0 INTEGER_CST@1) INTEGER_CST@2)
8082 (if (!TREE_OVERFLOW (@1) && !TREE_OVERFLOW (@2)
8083 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
8084 (with { tree res = int_const_binop (rop, @2, @1); }
8085 (if (TREE_OVERFLOW (res))
8086 {
8087 fold_overflow_warning (("assuming signed overflow does not occur "
8088 "when simplifying conditional to constant"),
8089 WARN_STRICT_OVERFLOW_CONDITIONAL);
8090 bool less = cmp == LE_EXPR || cmp == LT_EXPR;
8091 /* wi::ges_p (@2, 0) should be sufficient for a signed type. */
8092 bool ovf_high = wi::lt_p (wi::to_wide (@1), 0,
8093 TYPE_SIGN (TREE_TYPE (@1)))
8094 != (op == MINUS_EXPR);
8095 constant_boolean_node (less == ovf_high, type);
8096 }
8097 (if (single_use (@3))
8098 (with
8099 {
8100 fold_overflow_warning (("assuming signed overflow does not occur "
8101 "when changing X +- C1 cmp C2 to "
8102 "X cmp C2 -+ C1"),
8103 WARN_STRICT_OVERFLOW_COMPARISON);
8104 }
8105 (cmp @0 { res; })))))))))
8106
8107 /* Canonicalizations of BIT_FIELD_REFs. */
8108
8109 (simplify
8110 (BIT_FIELD_REF (BIT_FIELD_REF @0 @1 @2) @3 @4)
8111 (BIT_FIELD_REF @0 @3 { const_binop (PLUS_EXPR, bitsizetype, @2, @4); }))
8112
8113 (simplify
8114 (BIT_FIELD_REF (view_convert @0) @1 @2)
8115 (BIT_FIELD_REF @0 @1 @2))
8116
8117 (simplify
8118 (BIT_FIELD_REF @0 @1 integer_zerop)
8119 (if (tree_int_cst_equal (@1, TYPE_SIZE (TREE_TYPE (@0))))
8120 (view_convert @0)))
8121
8122 (simplify
8123 (BIT_FIELD_REF @0 @1 @2)
8124 (switch
8125 (if (TREE_CODE (TREE_TYPE (@0)) == COMPLEX_TYPE
8126 && tree_int_cst_equal (@1, TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))))
8127 (switch
8128 (if (integer_zerop (@2))
8129 (view_convert (realpart @0)))
8130 (if (tree_int_cst_equal (@2, TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))))
8131 (view_convert (imagpart @0)))))
8132 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
8133 && INTEGRAL_TYPE_P (type)
8134 /* On GIMPLE this should only apply to register arguments. */
8135 && (! GIMPLE || is_gimple_reg (@0))
8136 /* A bit-field-ref that referenced the full argument can be stripped. */
8137 && ((compare_tree_int (@1, TYPE_PRECISION (TREE_TYPE (@0))) == 0
8138 && integer_zerop (@2))
8139 /* Low-parts can be reduced to integral conversions.
8140 ??? The following doesn't work for PDP endian. */
8141 || (BYTES_BIG_ENDIAN == WORDS_BIG_ENDIAN
8142 /* But only do this after vectorization. */
8143 && canonicalize_math_after_vectorization_p ()
8144 /* Don't even think about BITS_BIG_ENDIAN. */
8145 && TYPE_PRECISION (TREE_TYPE (@0)) % BITS_PER_UNIT == 0
8146 && TYPE_PRECISION (type) % BITS_PER_UNIT == 0
8147 && compare_tree_int (@2, (BYTES_BIG_ENDIAN
8148 ? (TYPE_PRECISION (TREE_TYPE (@0))
8149 - TYPE_PRECISION (type))
8150 : 0)) == 0)))
8151 (convert @0))))
8152
8153 /* Simplify vector extracts. */
8154
8155 (simplify
8156 (BIT_FIELD_REF CONSTRUCTOR@0 @1 @2)
8157 (if (VECTOR_TYPE_P (TREE_TYPE (@0))
8158 && tree_fits_uhwi_p (TYPE_SIZE (type))
8159 && ((tree_to_uhwi (TYPE_SIZE (type))
8160 == tree_to_uhwi (TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))))
8161 || (VECTOR_TYPE_P (type)
8162 && (tree_to_uhwi (TYPE_SIZE (TREE_TYPE (type)))
8163 == tree_to_uhwi (TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0))))))))
8164 (with
8165 {
8166 tree ctor = (TREE_CODE (@0) == SSA_NAME
8167 ? gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0)) : @0);
8168 tree eltype = TREE_TYPE (TREE_TYPE (ctor));
8169 unsigned HOST_WIDE_INT width = tree_to_uhwi (TYPE_SIZE (eltype));
8170 unsigned HOST_WIDE_INT n = tree_to_uhwi (@1);
8171 unsigned HOST_WIDE_INT idx = tree_to_uhwi (@2);
8172 }
8173 (if (n != 0
8174 && (idx % width) == 0
8175 && (n % width) == 0
8176 && known_le ((idx + n) / width,
8177 TYPE_VECTOR_SUBPARTS (TREE_TYPE (ctor))))
8178 (with
8179 {
8180 idx = idx / width;
8181 n = n / width;
8182 /* Constructor elements can be subvectors. */
8183 poly_uint64 k = 1;
8184 if (CONSTRUCTOR_NELTS (ctor) != 0)
8185 {
8186 tree cons_elem = TREE_TYPE (CONSTRUCTOR_ELT (ctor, 0)->value);
8187 if (TREE_CODE (cons_elem) == VECTOR_TYPE)
8188 k = TYPE_VECTOR_SUBPARTS (cons_elem);
8189 }
8190 unsigned HOST_WIDE_INT elt, count, const_k;
8191 }
8192 (switch
8193 /* We keep an exact subset of the constructor elements. */
8194 (if (multiple_p (idx, k, &elt) && multiple_p (n, k, &count))
8195 (if (CONSTRUCTOR_NELTS (ctor) == 0)
8196 { build_zero_cst (type); }
8197 (if (count == 1)
8198 (if (elt < CONSTRUCTOR_NELTS (ctor))
8199 (view_convert { CONSTRUCTOR_ELT (ctor, elt)->value; })
8200 { build_zero_cst (type); })
8201 /* We don't want to emit new CTORs unless the old one goes away.
8202 ??? Eventually allow this if the CTOR ends up constant or
8203 uniform. */
8204 (if (single_use (@0))
8205 (with
8206 {
8207 vec<constructor_elt, va_gc> *vals;
8208 vec_alloc (vals, count);
8209 bool constant_p = true;
8210 tree res;
8211 for (unsigned i = 0;
8212 i < count && elt + i < CONSTRUCTOR_NELTS (ctor); ++i)
8213 {
8214 tree e = CONSTRUCTOR_ELT (ctor, elt + i)->value;
8215 CONSTRUCTOR_APPEND_ELT (vals, NULL_TREE, e);
8216 if (!CONSTANT_CLASS_P (e))
8217 constant_p = false;
8218 }
8219 tree evtype = (types_match (TREE_TYPE (type),
8220 TREE_TYPE (TREE_TYPE (ctor)))
8221 ? type
8222 : build_vector_type (TREE_TYPE (TREE_TYPE (ctor)),
8223 count * k));
8224 /* We used to build a CTOR in the non-constant case here
8225 but that's not a GIMPLE value. We'd have to expose this
8226 operation somehow so the code generation can properly
8227 split it out to a separate stmt. */
8228 res = (constant_p ? build_vector_from_ctor (evtype, vals)
8229 : (GIMPLE ? NULL_TREE : build_constructor (evtype, vals)));
8230 }
8231 (if (res)
8232 (view_convert { res; })))))))
8233 /* The bitfield references a single constructor element. */
8234 (if (k.is_constant (&const_k)
8235 && idx + n <= (idx / const_k + 1) * const_k)
8236 (switch
8237 (if (CONSTRUCTOR_NELTS (ctor) <= idx / const_k)
8238 { build_zero_cst (type); })
8239 (if (n == const_k)
8240 (view_convert { CONSTRUCTOR_ELT (ctor, idx / const_k)->value; }))
8241 (BIT_FIELD_REF { CONSTRUCTOR_ELT (ctor, idx / const_k)->value; }
8242 @1 { bitsize_int ((idx % const_k) * width); })))))))))
8243
8244 /* Simplify a bit extraction from a bit insertion for the cases with
8245 the inserted element fully covering the extraction or the insertion
8246 not touching the extraction. */
8247 (simplify
8248 (BIT_FIELD_REF (bit_insert @0 @1 @ipos) @rsize @rpos)
8249 (with
8250 {
8251 unsigned HOST_WIDE_INT isize;
8252 if (INTEGRAL_TYPE_P (TREE_TYPE (@1)))
8253 isize = TYPE_PRECISION (TREE_TYPE (@1));
8254 else
8255 isize = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (@1)));
8256 }
8257 (switch
8258 (if ((!INTEGRAL_TYPE_P (TREE_TYPE (@1))
8259 || type_has_mode_precision_p (TREE_TYPE (@1)))
8260 && wi::leu_p (wi::to_wide (@ipos), wi::to_wide (@rpos))
8261 && wi::leu_p (wi::to_wide (@rpos) + wi::to_wide (@rsize),
8262 wi::to_wide (@ipos) + isize))
8263 (BIT_FIELD_REF @1 @rsize { wide_int_to_tree (bitsizetype,
8264 wi::to_wide (@rpos)
8265 - wi::to_wide (@ipos)); }))
8266 (if (wi::eq_p (wi::to_wide (@ipos), wi::to_wide (@rpos))
8267 && compare_tree_int (@rsize, isize) == 0)
8268 (convert @1))
8269 (if (wi::geu_p (wi::to_wide (@ipos),
8270 wi::to_wide (@rpos) + wi::to_wide (@rsize))
8271 || wi::geu_p (wi::to_wide (@rpos),
8272 wi::to_wide (@ipos) + isize))
8273 (BIT_FIELD_REF @0 @rsize @rpos)))))
8274
8275 /* Simplify vector inserts of other vector extracts to a permute. */
8276 (simplify
8277 (bit_insert @0 (BIT_FIELD_REF@2 @1 @rsize @rpos) @ipos)
8278 (if (VECTOR_TYPE_P (type)
8279 && types_match (@0, @1)
8280 && types_match (TREE_TYPE (TREE_TYPE (@0)), TREE_TYPE (@2))
8281 && TYPE_VECTOR_SUBPARTS (type).is_constant ())
8282 (with
8283 {
8284 unsigned HOST_WIDE_INT elsz
8285 = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (TREE_TYPE (@1))));
8286 poly_uint64 relt = exact_div (tree_to_poly_uint64 (@rpos), elsz);
8287 poly_uint64 ielt = exact_div (tree_to_poly_uint64 (@ipos), elsz);
8288 unsigned nunits = TYPE_VECTOR_SUBPARTS (type).to_constant ();
8289 vec_perm_builder builder;
8290 builder.new_vector (nunits, nunits, 1);
8291 for (unsigned i = 0; i < nunits; ++i)
8292 builder.quick_push (known_eq (ielt, i) ? nunits + relt : i);
8293 vec_perm_indices sel (builder, 2, nunits);
8294 }
8295 (if (!VECTOR_MODE_P (TYPE_MODE (type))
8296 || can_vec_perm_const_p (TYPE_MODE (type), TYPE_MODE (type), sel, false))
8297 (vec_perm @0 @1 { vec_perm_indices_to_tree
8298 (build_vector_type (ssizetype, nunits), sel); })))))
8299
8300 (if (canonicalize_math_after_vectorization_p ())
8301 (for fmas (FMA)
8302 (simplify
8303 (fmas:c (negate @0) @1 @2)
8304 (IFN_FNMA @0 @1 @2))
8305 (simplify
8306 (fmas @0 @1 (negate @2))
8307 (IFN_FMS @0 @1 @2))
8308 (simplify
8309 (fmas:c (negate @0) @1 (negate @2))
8310 (IFN_FNMS @0 @1 @2))
8311 (simplify
8312 (negate (fmas@3 @0 @1 @2))
8313 (if (single_use (@3))
8314 (IFN_FNMS @0 @1 @2))))
8315
8316 (simplify
8317 (IFN_FMS:c (negate @0) @1 @2)
8318 (IFN_FNMS @0 @1 @2))
8319 (simplify
8320 (IFN_FMS @0 @1 (negate @2))
8321 (IFN_FMA @0 @1 @2))
8322 (simplify
8323 (IFN_FMS:c (negate @0) @1 (negate @2))
8324 (IFN_FNMA @0 @1 @2))
8325 (simplify
8326 (negate (IFN_FMS@3 @0 @1 @2))
8327 (if (single_use (@3))
8328 (IFN_FNMA @0 @1 @2)))
8329
8330 (simplify
8331 (IFN_FNMA:c (negate @0) @1 @2)
8332 (IFN_FMA @0 @1 @2))
8333 (simplify
8334 (IFN_FNMA @0 @1 (negate @2))
8335 (IFN_FNMS @0 @1 @2))
8336 (simplify
8337 (IFN_FNMA:c (negate @0) @1 (negate @2))
8338 (IFN_FMS @0 @1 @2))
8339 (simplify
8340 (negate (IFN_FNMA@3 @0 @1 @2))
8341 (if (single_use (@3))
8342 (IFN_FMS @0 @1 @2)))
8343
8344 (simplify
8345 (IFN_FNMS:c (negate @0) @1 @2)
8346 (IFN_FMS @0 @1 @2))
8347 (simplify
8348 (IFN_FNMS @0 @1 (negate @2))
8349 (IFN_FNMA @0 @1 @2))
8350 (simplify
8351 (IFN_FNMS:c (negate @0) @1 (negate @2))
8352 (IFN_FMA @0 @1 @2))
8353 (simplify
8354 (negate (IFN_FNMS@3 @0 @1 @2))
8355 (if (single_use (@3))
8356 (IFN_FMA @0 @1 @2))))
8357
8358 /* CLZ simplifications. */
8359 (for clz (CLZ)
8360 (for op (eq ne)
8361 cmp (lt ge)
8362 (simplify
8363 (op (clz:s@2 @0) INTEGER_CST@1)
8364 (if (integer_zerop (@1) && single_use (@2))
8365 /* clz(X) == 0 is (int)X < 0 and clz(X) != 0 is (int)X >= 0. */
8366 (with { tree type0 = TREE_TYPE (@0);
8367 tree stype = signed_type_for (type0);
8368 HOST_WIDE_INT val = 0;
8369 /* Punt on hypothetical weird targets. */
8370 if (clz == CFN_CLZ
8371 && CLZ_DEFINED_VALUE_AT_ZERO (SCALAR_TYPE_MODE (type0),
8372 val) == 2
8373 && val == 0)
8374 stype = NULL_TREE;
8375 }
8376 (if (stype)
8377 (cmp (convert:stype @0) { build_zero_cst (stype); })))
8378 /* clz(X) == (prec-1) is X == 1 and clz(X) != (prec-1) is X != 1. */
8379 (with { bool ok = true;
8380 HOST_WIDE_INT val = 0;
8381 tree type0 = TREE_TYPE (@0);
8382 /* Punt on hypothetical weird targets. */
8383 if (clz == CFN_CLZ
8384 && CLZ_DEFINED_VALUE_AT_ZERO (SCALAR_TYPE_MODE (type0),
8385 val) == 2
8386 && val == TYPE_PRECISION (type0) - 1)
8387 ok = false;
8388 }
8389 (if (ok && wi::to_wide (@1) == (TYPE_PRECISION (type0) - 1))
8390 (op @0 { build_one_cst (type0); })))))))
8391
8392 /* CTZ simplifications. */
8393 (for ctz (CTZ)
8394 (for op (ge gt le lt)
8395 cmp (eq eq ne ne)
8396 (simplify
8397 /* __builtin_ctz (x) >= C -> (x & ((1 << C) - 1)) == 0. */
8398 (op (ctz:s @0) INTEGER_CST@1)
8399 (with { bool ok = true;
8400 HOST_WIDE_INT val = 0;
8401 if (!tree_fits_shwi_p (@1))
8402 ok = false;
8403 else
8404 {
8405 val = tree_to_shwi (@1);
8406 /* Canonicalize to >= or <. */
8407 if (op == GT_EXPR || op == LE_EXPR)
8408 {
8409 if (val == HOST_WIDE_INT_MAX)
8410 ok = false;
8411 else
8412 val++;
8413 }
8414 }
8415 bool zero_res = false;
8416 HOST_WIDE_INT zero_val = 0;
8417 tree type0 = TREE_TYPE (@0);
8418 int prec = TYPE_PRECISION (type0);
8419 if (ctz == CFN_CTZ
8420 && CTZ_DEFINED_VALUE_AT_ZERO (SCALAR_TYPE_MODE (type0),
8421 zero_val) == 2)
8422 zero_res = true;
8423 }
8424 (if (val <= 0)
8425 (if (ok && (!zero_res || zero_val >= val))
8426 { constant_boolean_node (cmp == EQ_EXPR ? true : false, type); })
8427 (if (val >= prec)
8428 (if (ok && (!zero_res || zero_val < val))
8429 { constant_boolean_node (cmp == EQ_EXPR ? false : true, type); })
8430 (if (ok && (!zero_res || zero_val < 0 || zero_val >= prec))
8431 (cmp (bit_and @0 { wide_int_to_tree (type0,
8432 wi::mask (val, false, prec)); })
8433 { build_zero_cst (type0); })))))))
8434 (for op (eq ne)
8435 (simplify
8436 /* __builtin_ctz (x) == C -> (x & ((1 << (C + 1)) - 1)) == (1 << C). */
8437 (op (ctz:s @0) INTEGER_CST@1)
8438 (with { bool zero_res = false;
8439 HOST_WIDE_INT zero_val = 0;
8440 tree type0 = TREE_TYPE (@0);
8441 int prec = TYPE_PRECISION (type0);
8442 if (ctz == CFN_CTZ
8443 && CTZ_DEFINED_VALUE_AT_ZERO (SCALAR_TYPE_MODE (type0),
8444 zero_val) == 2)
8445 zero_res = true;
8446 }
8447 (if (tree_int_cst_sgn (@1) < 0 || wi::to_widest (@1) >= prec)
8448 (if (!zero_res || zero_val != wi::to_widest (@1))
8449 { constant_boolean_node (op == EQ_EXPR ? false : true, type); })
8450 (if (!zero_res || zero_val < 0 || zero_val >= prec)
8451 (op (bit_and @0 { wide_int_to_tree (type0,
8452 wi::mask (tree_to_uhwi (@1) + 1,
8453 false, prec)); })
8454 { wide_int_to_tree (type0,
8455 wi::shifted_mask (tree_to_uhwi (@1), 1,
8456 false, prec)); })))))))
8457
8458 /* POPCOUNT simplifications. */
8459 /* popcount(X) + popcount(Y) is popcount(X|Y) when X&Y must be zero. */
8460 (simplify
8461 (plus (POPCOUNT:s @0) (POPCOUNT:s @1))
8462 (if (INTEGRAL_TYPE_P (type)
8463 && wi::bit_and (tree_nonzero_bits (@0), tree_nonzero_bits (@1)) == 0)
8464 (POPCOUNT (bit_ior @0 @1))))
8465
8466 /* popcount(X) == 0 is X == 0, and related (in)equalities. */
8467 (for popcount (POPCOUNT)
8468 (for cmp (le eq ne gt)
8469 rep (eq eq ne ne)
8470 (simplify
8471 (cmp (popcount @0) integer_zerop)
8472 (rep @0 { build_zero_cst (TREE_TYPE (@0)); }))))
8473
8474 /* popcount(bswap(x)) is popcount(x). */
8475 (for popcount (POPCOUNT)
8476 (for bswap (BUILT_IN_BSWAP16 BUILT_IN_BSWAP32
8477 BUILT_IN_BSWAP64 BUILT_IN_BSWAP128)
8478 (simplify
8479 (popcount (convert?@0 (bswap:s@1 @2)))
8480 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
8481 && INTEGRAL_TYPE_P (TREE_TYPE (@1)))
8482 (with { tree type0 = TREE_TYPE (@0);
8483 tree type1 = TREE_TYPE (@1);
8484 unsigned int prec0 = TYPE_PRECISION (type0);
8485 unsigned int prec1 = TYPE_PRECISION (type1); }
8486 (if (prec0 == prec1 || (prec0 > prec1 && TYPE_UNSIGNED (type1)))
8487 (popcount (convert:type0 (convert:type1 @2)))))))))
8488
8489 /* popcount(rotate(X Y)) is popcount(X). */
8490 (for popcount (POPCOUNT)
8491 (for rot (lrotate rrotate)
8492 (simplify
8493 (popcount (convert?@0 (rot:s@1 @2 @3)))
8494 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
8495 && INTEGRAL_TYPE_P (TREE_TYPE (@1))
8496 && (GIMPLE || !TREE_SIDE_EFFECTS (@3)))
8497 (with { tree type0 = TREE_TYPE (@0);
8498 tree type1 = TREE_TYPE (@1);
8499 unsigned int prec0 = TYPE_PRECISION (type0);
8500 unsigned int prec1 = TYPE_PRECISION (type1); }
8501 (if (prec0 == prec1 || (prec0 > prec1 && TYPE_UNSIGNED (type1)))
8502 (popcount (convert:type0 @2))))))))
8503
8504 /* Canonicalize POPCOUNT(x)&1 as PARITY(X). */
8505 (simplify
8506 (bit_and (POPCOUNT @0) integer_onep)
8507 (PARITY @0))
8508
8509 /* popcount(X&Y) + popcount(X|Y) is popcount(x) + popcount(Y). */
8510 (simplify
8511 (plus:c (POPCOUNT:s (bit_and:s @0 @1)) (POPCOUNT:s (bit_ior:cs @0 @1)))
8512 (plus (POPCOUNT @0) (POPCOUNT @1)))
8513
8514 /* popcount(X) + popcount(Y) - popcount(X&Y) is popcount(X|Y). */
8515 /* popcount(X) + popcount(Y) - popcount(X|Y) is popcount(X&Y). */
8516 (for popcount (POPCOUNT)
8517 (for log1 (bit_and bit_ior)
8518 log2 (bit_ior bit_and)
8519 (simplify
8520 (minus (plus:s (popcount:s @0) (popcount:s @1))
8521 (popcount:s (log1:cs @0 @1)))
8522 (popcount (log2 @0 @1)))
8523 (simplify
8524 (plus:c (minus:s (popcount:s @0) (popcount:s (log1:cs @0 @1)))
8525 (popcount:s @1))
8526 (popcount (log2 @0 @1)))))
8527
8528 /* PARITY simplifications. */
8529 /* parity(~X) is parity(X). */
8530 (simplify
8531 (PARITY (bit_not @0))
8532 (PARITY @0))
8533
8534 /* parity(bswap(x)) is parity(x). */
8535 (for parity (PARITY)
8536 (for bswap (BUILT_IN_BSWAP16 BUILT_IN_BSWAP32
8537 BUILT_IN_BSWAP64 BUILT_IN_BSWAP128)
8538 (simplify
8539 (parity (convert?@0 (bswap:s@1 @2)))
8540 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
8541 && INTEGRAL_TYPE_P (TREE_TYPE (@1))
8542 && TYPE_PRECISION (TREE_TYPE (@0))
8543 >= TYPE_PRECISION (TREE_TYPE (@1)))
8544 (with { tree type0 = TREE_TYPE (@0);
8545 tree type1 = TREE_TYPE (@1); }
8546 (parity (convert:type0 (convert:type1 @2))))))))
8547
8548 /* parity(rotate(X Y)) is parity(X). */
8549 (for parity (PARITY)
8550 (for rot (lrotate rrotate)
8551 (simplify
8552 (parity (convert?@0 (rot:s@1 @2 @3)))
8553 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
8554 && INTEGRAL_TYPE_P (TREE_TYPE (@1))
8555 && (GIMPLE || !TREE_SIDE_EFFECTS (@3))
8556 && TYPE_PRECISION (TREE_TYPE (@0))
8557 >= TYPE_PRECISION (TREE_TYPE (@1)))
8558 (with { tree type0 = TREE_TYPE (@0); }
8559 (parity (convert:type0 @2)))))))
8560
8561 /* parity(X)^parity(Y) is parity(X^Y). */
8562 (simplify
8563 (bit_xor (PARITY:s @0) (PARITY:s @1))
8564 (PARITY (bit_xor @0 @1)))
8565
8566 /* a != 0 ? FUN(a) : 0 -> Fun(a) for some builtin functions. */
8567 (for func (POPCOUNT BSWAP FFS PARITY)
8568 (simplify
8569 (cond (ne @0 integer_zerop@1) (func@3 (convert? @0)) integer_zerop@2)
8570 @3))
8571
8572 /* a != 0 ? FUN(a) : CST -> Fun(a) for some CLRSB builtins
8573 where CST is precision-1. */
8574 (for func (CLRSB)
8575 (simplify
8576 (cond (ne @0 integer_zerop@1) (func@4 (convert?@3 @0)) INTEGER_CST@2)
8577 (if (wi::to_widest (@2) == TYPE_PRECISION (TREE_TYPE (@3)) - 1)
8578 @4)))
8579
8580 #if GIMPLE
8581 /* a != 0 ? CLZ(a) : CST -> .CLZ(a) where CST is the result of the internal function for 0. */
8582 (for func (CLZ)
8583 (simplify
8584 (cond (ne @0 integer_zerop@1) (func (convert?@3 @0)) INTEGER_CST@2)
8585 (with { int val;
8586 internal_fn ifn = IFN_LAST;
8587 if (direct_internal_fn_supported_p (IFN_CLZ, type, OPTIMIZE_FOR_BOTH)
8588 && CLZ_DEFINED_VALUE_AT_ZERO (SCALAR_INT_TYPE_MODE (type),
8589 val) == 2)
8590 ifn = IFN_CLZ;
8591 }
8592 (if (ifn == IFN_CLZ && wi::to_widest (@2) == val)
8593 (IFN_CLZ @3)))))
8594
8595 /* a != 0 ? CTZ(a) : CST -> .CTZ(a) where CST is the result of the internal function for 0. */
8596 (for func (CTZ)
8597 (simplify
8598 (cond (ne @0 integer_zerop@1) (func (convert?@3 @0)) INTEGER_CST@2)
8599 (with { int val;
8600 internal_fn ifn = IFN_LAST;
8601 if (direct_internal_fn_supported_p (IFN_CTZ, type, OPTIMIZE_FOR_BOTH)
8602 && CTZ_DEFINED_VALUE_AT_ZERO (SCALAR_INT_TYPE_MODE (type),
8603 val) == 2)
8604 ifn = IFN_CTZ;
8605 }
8606 (if (ifn == IFN_CTZ && wi::to_widest (@2) == val)
8607 (IFN_CTZ @3)))))
8608 #endif
8609
8610 /* Common POPCOUNT/PARITY simplifications. */
8611 /* popcount(X&C1) is (X>>C2)&1 when C1 == 1<<C2. Same for parity(X&C1). */
8612 (for pfun (POPCOUNT PARITY)
8613 (simplify
8614 (pfun @0)
8615 (if (INTEGRAL_TYPE_P (type))
8616 (with { wide_int nz = tree_nonzero_bits (@0); }
8617 (switch
8618 (if (nz == 1)
8619 (convert @0))
8620 (if (wi::popcount (nz) == 1)
8621 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
8622 (convert (rshift:utype (convert:utype @0)
8623 { build_int_cst (integer_type_node,
8624 wi::ctz (nz)); })))))))))
8625
8626 #if GIMPLE
8627 /* 64- and 32-bits branchless implementations of popcount are detected:
8628
8629 int popcount64c (uint64_t x)
8630 {
8631 x -= (x >> 1) & 0x5555555555555555ULL;
8632 x = (x & 0x3333333333333333ULL) + ((x >> 2) & 0x3333333333333333ULL);
8633 x = (x + (x >> 4)) & 0x0f0f0f0f0f0f0f0fULL;
8634 return (x * 0x0101010101010101ULL) >> 56;
8635 }
8636
8637 int popcount32c (uint32_t x)
8638 {
8639 x -= (x >> 1) & 0x55555555;
8640 x = (x & 0x33333333) + ((x >> 2) & 0x33333333);
8641 x = (x + (x >> 4)) & 0x0f0f0f0f;
8642 return (x * 0x01010101) >> 24;
8643 } */
8644 (simplify
8645 (rshift
8646 (mult
8647 (bit_and
8648 (plus:c
8649 (rshift @8 INTEGER_CST@5)
8650 (plus:c@8
8651 (bit_and @6 INTEGER_CST@7)
8652 (bit_and
8653 (rshift
8654 (minus@6 @0
8655 (bit_and (rshift @0 INTEGER_CST@4) INTEGER_CST@11))
8656 INTEGER_CST@10)
8657 INTEGER_CST@9)))
8658 INTEGER_CST@3)
8659 INTEGER_CST@2)
8660 INTEGER_CST@1)
8661 /* Check constants and optab. */
8662 (with { unsigned prec = TYPE_PRECISION (type);
8663 int shift = (64 - prec) & 63;
8664 unsigned HOST_WIDE_INT c1
8665 = HOST_WIDE_INT_UC (0x0101010101010101) >> shift;
8666 unsigned HOST_WIDE_INT c2
8667 = HOST_WIDE_INT_UC (0x0F0F0F0F0F0F0F0F) >> shift;
8668 unsigned HOST_WIDE_INT c3
8669 = HOST_WIDE_INT_UC (0x3333333333333333) >> shift;
8670 unsigned HOST_WIDE_INT c4
8671 = HOST_WIDE_INT_UC (0x5555555555555555) >> shift;
8672 }
8673 (if (prec >= 16
8674 && prec <= 64
8675 && pow2p_hwi (prec)
8676 && TYPE_UNSIGNED (type)
8677 && integer_onep (@4)
8678 && wi::to_widest (@10) == 2
8679 && wi::to_widest (@5) == 4
8680 && wi::to_widest (@1) == prec - 8
8681 && tree_to_uhwi (@2) == c1
8682 && tree_to_uhwi (@3) == c2
8683 && tree_to_uhwi (@9) == c3
8684 && tree_to_uhwi (@7) == c3
8685 && tree_to_uhwi (@11) == c4)
8686 (if (direct_internal_fn_supported_p (IFN_POPCOUNT, type,
8687 OPTIMIZE_FOR_BOTH))
8688 (convert (IFN_POPCOUNT:type @0))
8689 /* Try to do popcount in two halves. PREC must be at least
8690 five bits for this to work without extension before adding. */
8691 (with {
8692 tree half_type = NULL_TREE;
8693 opt_machine_mode m = mode_for_size ((prec + 1) / 2, MODE_INT, 1);
8694 int half_prec = 8;
8695 if (m.exists ()
8696 && m.require () != TYPE_MODE (type))
8697 {
8698 half_prec = GET_MODE_PRECISION (as_a <scalar_int_mode> (m));
8699 half_type = build_nonstandard_integer_type (half_prec, 1);
8700 }
8701 gcc_assert (half_prec > 2);
8702 }
8703 (if (half_type != NULL_TREE
8704 && direct_internal_fn_supported_p (IFN_POPCOUNT, half_type,
8705 OPTIMIZE_FOR_BOTH))
8706 (convert (plus
8707 (IFN_POPCOUNT:half_type (convert @0))
8708 (IFN_POPCOUNT:half_type (convert (rshift @0
8709 { build_int_cst (integer_type_node, half_prec); } )))))))))))
8710
8711 /* __builtin_ffs needs to deal on many targets with the possible zero
8712 argument. If we know the argument is always non-zero, __builtin_ctz + 1
8713 should lead to better code. */
8714 (simplify
8715 (FFS tree_expr_nonzero_p@0)
8716 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
8717 && direct_internal_fn_supported_p (IFN_CTZ, TREE_TYPE (@0),
8718 OPTIMIZE_FOR_SPEED))
8719 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
8720 (plus (CTZ:type (convert:utype @0)) { build_one_cst (type); }))))
8721 #endif
8722
8723 (for ffs (BUILT_IN_FFS BUILT_IN_FFSL BUILT_IN_FFSLL
8724 BUILT_IN_FFSIMAX)
8725 /* __builtin_ffs (X) == 0 -> X == 0.
8726 __builtin_ffs (X) == 6 -> (X & 63) == 32. */
8727 (for cmp (eq ne)
8728 (simplify
8729 (cmp (ffs@2 @0) INTEGER_CST@1)
8730 (with { int prec = TYPE_PRECISION (TREE_TYPE (@0)); }
8731 (switch
8732 (if (integer_zerop (@1))
8733 (cmp @0 { build_zero_cst (TREE_TYPE (@0)); }))
8734 (if (tree_int_cst_sgn (@1) < 0 || wi::to_widest (@1) > prec)
8735 { constant_boolean_node (cmp == NE_EXPR ? true : false, type); })
8736 (if (single_use (@2))
8737 (cmp (bit_and @0 { wide_int_to_tree (TREE_TYPE (@0),
8738 wi::mask (tree_to_uhwi (@1),
8739 false, prec)); })
8740 { wide_int_to_tree (TREE_TYPE (@0),
8741 wi::shifted_mask (tree_to_uhwi (@1) - 1, 1,
8742 false, prec)); }))))))
8743
8744 /* __builtin_ffs (X) > 6 -> X != 0 && (X & 63) == 0. */
8745 (for cmp (gt le)
8746 cmp2 (ne eq)
8747 cmp3 (eq ne)
8748 bit_op (bit_and bit_ior)
8749 (simplify
8750 (cmp (ffs@2 @0) INTEGER_CST@1)
8751 (with { int prec = TYPE_PRECISION (TREE_TYPE (@0)); }
8752 (switch
8753 (if (integer_zerop (@1))
8754 (cmp2 @0 { build_zero_cst (TREE_TYPE (@0)); }))
8755 (if (tree_int_cst_sgn (@1) < 0)
8756 { constant_boolean_node (cmp == GT_EXPR ? true : false, type); })
8757 (if (wi::to_widest (@1) >= prec)
8758 { constant_boolean_node (cmp == GT_EXPR ? false : true, type); })
8759 (if (wi::to_widest (@1) == prec - 1)
8760 (cmp3 @0 { wide_int_to_tree (TREE_TYPE (@0),
8761 wi::shifted_mask (prec - 1, 1,
8762 false, prec)); }))
8763 (if (single_use (@2))
8764 (bit_op (cmp2 @0 { build_zero_cst (TREE_TYPE (@0)); })
8765 (cmp3 (bit_and @0
8766 { wide_int_to_tree (TREE_TYPE (@0),
8767 wi::mask (tree_to_uhwi (@1),
8768 false, prec)); })
8769 { build_zero_cst (TREE_TYPE (@0)); }))))))))
8770
8771 #if GIMPLE
8772
8773 /* Simplify:
8774 a = op a1
8775 r = cond ? a : b
8776 --> r = .COND_FN (cond, a, b)
8777 and,
8778 a = op a1
8779 r = cond ? b : a
8780 --> r = .COND_FN (~cond, b, a). */
8781
8782 (for uncond_op (UNCOND_UNARY)
8783 cond_op (COND_UNARY)
8784 (simplify
8785 (vec_cond @0 (view_convert? (uncond_op@3 @1)) @2)
8786 (with { tree op_type = TREE_TYPE (@3); }
8787 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
8788 && is_truth_type_for (op_type, TREE_TYPE (@0)))
8789 (cond_op @0 @1 @2))))
8790 (simplify
8791 (vec_cond @0 @1 (view_convert? (uncond_op@3 @2)))
8792 (with { tree op_type = TREE_TYPE (@3); }
8793 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
8794 && is_truth_type_for (op_type, TREE_TYPE (@0)))
8795 (cond_op (bit_not @0) @2 @1)))))
8796
8797 /* `(a ? -1 : 0) ^ b` can be converted into a conditional not. */
8798 (simplify
8799 (bit_xor:c (vec_cond @0 uniform_integer_cst_p@1 uniform_integer_cst_p@2) @3)
8800 (if (canonicalize_math_after_vectorization_p ()
8801 && vectorized_internal_fn_supported_p (IFN_COND_NOT, type)
8802 && is_truth_type_for (type, TREE_TYPE (@0)))
8803 (if (integer_all_onesp (@1) && integer_zerop (@2))
8804 (IFN_COND_NOT @0 @3 @3))
8805 (if (integer_all_onesp (@2) && integer_zerop (@1))
8806 (IFN_COND_NOT (bit_not @0) @3 @3))))
8807
8808 /* Simplify:
8809
8810 a = a1 op a2
8811 r = c ? a : b;
8812
8813 to:
8814
8815 r = c ? a1 op a2 : b;
8816
8817 if the target can do it in one go. This makes the operation conditional
8818 on c, so could drop potentially-trapping arithmetic, but that's a valid
8819 simplification if the result of the operation isn't needed.
8820
8821 Avoid speculatively generating a stand-alone vector comparison
8822 on targets that might not support them. Any target implementing
8823 conditional internal functions must support the same comparisons
8824 inside and outside a VEC_COND_EXPR. */
8825
8826 (for uncond_op (UNCOND_BINARY)
8827 cond_op (COND_BINARY)
8828 (simplify
8829 (vec_cond @0 (view_convert? (uncond_op@4 @1 @2)) @3)
8830 (with { tree op_type = TREE_TYPE (@4); }
8831 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
8832 && is_truth_type_for (op_type, TREE_TYPE (@0))
8833 && single_use (@4))
8834 (view_convert (cond_op @0 @1 @2 (view_convert:op_type @3))))))
8835 (simplify
8836 (vec_cond @0 @1 (view_convert? (uncond_op@4 @2 @3)))
8837 (with { tree op_type = TREE_TYPE (@4); }
8838 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
8839 && is_truth_type_for (op_type, TREE_TYPE (@0))
8840 && single_use (@4))
8841 (view_convert (cond_op (bit_not @0) @2 @3 (view_convert:op_type @1)))))))
8842
8843 /* Same for ternary operations. */
8844 (for uncond_op (UNCOND_TERNARY)
8845 cond_op (COND_TERNARY)
8846 (simplify
8847 (vec_cond @0 (view_convert? (uncond_op@5 @1 @2 @3)) @4)
8848 (with { tree op_type = TREE_TYPE (@5); }
8849 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
8850 && is_truth_type_for (op_type, TREE_TYPE (@0))
8851 && single_use (@5))
8852 (view_convert (cond_op @0 @1 @2 @3 (view_convert:op_type @4))))))
8853 (simplify
8854 (vec_cond @0 @1 (view_convert? (uncond_op@5 @2 @3 @4)))
8855 (with { tree op_type = TREE_TYPE (@5); }
8856 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
8857 && is_truth_type_for (op_type, TREE_TYPE (@0))
8858 && single_use (@5))
8859 (view_convert (cond_op (bit_not @0) @2 @3 @4
8860 (view_convert:op_type @1)))))))
8861 #endif
8862
8863 /* Detect cases in which a VEC_COND_EXPR effectively replaces the
8864 "else" value of an IFN_COND_*. */
8865 (for cond_op (COND_BINARY)
8866 (simplify
8867 (vec_cond @0 (view_convert? (cond_op @0 @1 @2 @3)) @4)
8868 (with { tree op_type = TREE_TYPE (@3); }
8869 (if (element_precision (type) == element_precision (op_type))
8870 (view_convert (cond_op @0 @1 @2 (view_convert:op_type @4))))))
8871 (simplify
8872 (vec_cond @0 @1 (view_convert? (cond_op @2 @3 @4 @5)))
8873 (with { tree op_type = TREE_TYPE (@5); }
8874 (if (inverse_conditions_p (@0, @2)
8875 && element_precision (type) == element_precision (op_type))
8876 (view_convert (cond_op @2 @3 @4 (view_convert:op_type @1)))))))
8877
8878 /* Same for ternary operations. */
8879 (for cond_op (COND_TERNARY)
8880 (simplify
8881 (vec_cond @0 (view_convert? (cond_op @0 @1 @2 @3 @4)) @5)
8882 (with { tree op_type = TREE_TYPE (@4); }
8883 (if (element_precision (type) == element_precision (op_type))
8884 (view_convert (cond_op @0 @1 @2 @3 (view_convert:op_type @5))))))
8885 (simplify
8886 (vec_cond @0 @1 (view_convert? (cond_op @2 @3 @4 @5 @6)))
8887 (with { tree op_type = TREE_TYPE (@6); }
8888 (if (inverse_conditions_p (@0, @2)
8889 && element_precision (type) == element_precision (op_type))
8890 (view_convert (cond_op @2 @3 @4 @5 (view_convert:op_type @1)))))))
8891
8892 /* Detect simplication for a conditional reduction where
8893
8894 a = mask1 ? b : 0
8895 c = mask2 ? d + a : d
8896
8897 is turned into
8898
8899 c = mask1 && mask2 ? d + b : d. */
8900 (simplify
8901 (IFN_COND_ADD @0 @1 (vec_cond @2 @3 zerop@4) @1)
8902 (if (ANY_INTEGRAL_TYPE_P (type)
8903 || (FLOAT_TYPE_P (type)
8904 && fold_real_zero_addition_p (type, NULL_TREE, @4, 0)))
8905 (IFN_COND_ADD (bit_and @0 @2) @1 @3 @1)))
8906
8907 /* Detect simplication for a conditional length reduction where
8908
8909 a = mask ? b : 0
8910 c = i < len + bias ? d + a : d
8911
8912 is turned into
8913
8914 c = mask && i < len + bias ? d + b : d. */
8915 (simplify
8916 (IFN_COND_LEN_ADD integer_truep @0 (vec_cond @1 @2 zerop@5) @0 @3 @4)
8917 (if (ANY_INTEGRAL_TYPE_P (type)
8918 || (FLOAT_TYPE_P (type)
8919 && fold_real_zero_addition_p (type, NULL_TREE, @5, 0)))
8920 (IFN_COND_LEN_ADD @1 @0 @2 @0 @3 @4)))
8921
8922 /* For pointers @0 and @2 and nonnegative constant offset @1, look for
8923 expressions like:
8924
8925 A: (@0 + @1 < @2) | (@2 + @1 < @0)
8926 B: (@0 + @1 <= @2) | (@2 + @1 <= @0)
8927
8928 If pointers are known not to wrap, B checks whether @1 bytes starting
8929 at @0 and @2 do not overlap, while A tests the same thing for @1 + 1
8930 bytes. A is more efficiently tested as:
8931
8932 A: (sizetype) (@0 + @1 - @2) > @1 * 2
8933
8934 The equivalent expression for B is given by replacing @1 with @1 - 1:
8935
8936 B: (sizetype) (@0 + (@1 - 1) - @2) > (@1 - 1) * 2
8937
8938 @0 and @2 can be swapped in both expressions without changing the result.
8939
8940 The folds rely on sizetype's being unsigned (which is always true)
8941 and on its being the same width as the pointer (which we have to check).
8942
8943 The fold replaces two pointer_plus expressions, two comparisons and
8944 an IOR with a pointer_plus, a pointer_diff, and a comparison, so in
8945 the best case it's a saving of two operations. The A fold retains one
8946 of the original pointer_pluses, so is a win even if both pointer_pluses
8947 are used elsewhere. The B fold is a wash if both pointer_pluses are
8948 used elsewhere, since all we end up doing is replacing a comparison with
8949 a pointer_plus. We do still apply the fold under those circumstances
8950 though, in case applying it to other conditions eventually makes one of the
8951 pointer_pluses dead. */
8952 (for ior (truth_orif truth_or bit_ior)
8953 (for cmp (le lt)
8954 (simplify
8955 (ior (cmp:cs (pointer_plus@3 @0 INTEGER_CST@1) @2)
8956 (cmp:cs (pointer_plus@4 @2 @1) @0))
8957 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
8958 && TYPE_OVERFLOW_WRAPS (sizetype)
8959 && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (sizetype))
8960 /* Calculate the rhs constant. */
8961 (with { offset_int off = wi::to_offset (@1) - (cmp == LE_EXPR ? 1 : 0);
8962 offset_int rhs = off * 2; }
8963 /* Always fails for negative values. */
8964 (if (wi::min_precision (rhs, UNSIGNED) <= TYPE_PRECISION (sizetype))
8965 /* Since the order of @0 and @2 doesn't matter, let tree_swap_operands_p
8966 pick a canonical order. This increases the chances of using the
8967 same pointer_plus in multiple checks. */
8968 (with { bool swap_p = tree_swap_operands_p (@0, @2);
8969 tree rhs_tree = wide_int_to_tree (sizetype, rhs); }
8970 (if (cmp == LT_EXPR)
8971 (gt (convert:sizetype
8972 (pointer_diff:ssizetype { swap_p ? @4 : @3; }
8973 { swap_p ? @0 : @2; }))
8974 { rhs_tree; })
8975 (gt (convert:sizetype
8976 (pointer_diff:ssizetype
8977 (pointer_plus { swap_p ? @2 : @0; }
8978 { wide_int_to_tree (sizetype, off); })
8979 { swap_p ? @0 : @2; }))
8980 { rhs_tree; })))))))))
8981
8982 /* Fold REDUC (@0 & @1) -> @0[I] & @1[I] if element I is the only nonzero
8983 element of @1. */
8984 (for reduc (IFN_REDUC_PLUS IFN_REDUC_IOR IFN_REDUC_XOR)
8985 (simplify (reduc (view_convert? (bit_and @0 VECTOR_CST@1)))
8986 (with { int i = single_nonzero_element (@1); }
8987 (if (i >= 0)
8988 (with { tree elt = vector_cst_elt (@1, i);
8989 tree elt_type = TREE_TYPE (elt);
8990 unsigned int elt_bits = tree_to_uhwi (TYPE_SIZE (elt_type));
8991 tree size = bitsize_int (elt_bits);
8992 tree pos = bitsize_int (elt_bits * i); }
8993 (view_convert
8994 (bit_and:elt_type
8995 (BIT_FIELD_REF:elt_type @0 { size; } { pos; })
8996 { elt; })))))))
8997
8998 /* Fold reduction of a single nonzero element constructor. */
8999 (for reduc (IFN_REDUC_PLUS IFN_REDUC_IOR IFN_REDUC_XOR)
9000 (simplify (reduc (CONSTRUCTOR@0))
9001 (with { tree ctor = (TREE_CODE (@0) == SSA_NAME
9002 ? gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0)) : @0);
9003 tree elt = ctor_single_nonzero_element (ctor); }
9004 (if (elt
9005 && !HONOR_SNANS (type)
9006 && !HONOR_SIGNED_ZEROS (type))
9007 { elt; }))))
9008
9009 /* Fold REDUC (@0 op VECTOR_CST) as REDUC (@0) op REDUC (VECTOR_CST). */
9010 (for reduc (IFN_REDUC_PLUS IFN_REDUC_MAX IFN_REDUC_MIN IFN_REDUC_FMAX
9011 IFN_REDUC_FMIN IFN_REDUC_AND IFN_REDUC_IOR IFN_REDUC_XOR)
9012 op (plus max min IFN_FMAX IFN_FMIN bit_and bit_ior bit_xor)
9013 (simplify (reduc (op @0 VECTOR_CST@1))
9014 (op (reduc:type @0) (reduc:type @1))))
9015
9016 /* Simplify vector floating point operations of alternating sub/add pairs
9017 into using an fneg of a wider element type followed by a normal add.
9018 under IEEE 754 the fneg of the wider type will negate every even entry
9019 and when doing an add we get a sub of the even and add of every odd
9020 elements. */
9021 (for plusminus (plus minus)
9022 minusplus (minus plus)
9023 (simplify
9024 (vec_perm (plusminus @0 @1) (minusplus @2 @3) VECTOR_CST@4)
9025 (if (!VECTOR_INTEGER_TYPE_P (type)
9026 && !FLOAT_WORDS_BIG_ENDIAN
9027 /* plus is commutative, while minus is not, so :c can't be used.
9028 Do equality comparisons by hand and at the end pick the operands
9029 from the minus. */
9030 && (operand_equal_p (@0, @2, 0)
9031 ? operand_equal_p (@1, @3, 0)
9032 : operand_equal_p (@0, @3, 0) && operand_equal_p (@1, @2, 0)))
9033 (with
9034 {
9035 /* Build a vector of integers from the tree mask. */
9036 vec_perm_builder builder;
9037 }
9038 (if (tree_to_vec_perm_builder (&builder, @4))
9039 (with
9040 {
9041 /* Create a vec_perm_indices for the integer vector. */
9042 poly_uint64 nelts = TYPE_VECTOR_SUBPARTS (type);
9043 vec_perm_indices sel (builder, 2, nelts);
9044 machine_mode vec_mode = TYPE_MODE (type);
9045 machine_mode wide_mode;
9046 scalar_mode wide_elt_mode;
9047 poly_uint64 wide_nunits;
9048 scalar_mode inner_mode = GET_MODE_INNER (vec_mode);
9049 }
9050 (if (VECTOR_MODE_P (vec_mode)
9051 && sel.series_p (0, 2, 0, 2)
9052 && sel.series_p (1, 2, nelts + 1, 2)
9053 && GET_MODE_2XWIDER_MODE (inner_mode).exists (&wide_elt_mode)
9054 && multiple_p (GET_MODE_NUNITS (vec_mode), 2, &wide_nunits)
9055 && related_vector_mode (vec_mode, wide_elt_mode,
9056 wide_nunits).exists (&wide_mode))
9057 (with
9058 {
9059 tree stype
9060 = lang_hooks.types.type_for_mode (GET_MODE_INNER (wide_mode),
9061 TYPE_UNSIGNED (type));
9062 tree ntype = build_vector_type_for_mode (stype, wide_mode);
9063
9064 /* The format has to be a non-extended ieee format. */
9065 const struct real_format *fmt_old = FLOAT_MODE_FORMAT (vec_mode);
9066 const struct real_format *fmt_new = FLOAT_MODE_FORMAT (wide_mode);
9067 }
9068 (if (TYPE_MODE (stype) != BLKmode
9069 && VECTOR_TYPE_P (ntype)
9070 && fmt_old != NULL
9071 && fmt_new != NULL)
9072 (with
9073 {
9074 /* If the target doesn't support v1xx vectors, try using
9075 scalar mode xx instead. */
9076 if (known_eq (GET_MODE_NUNITS (wide_mode), 1)
9077 && !target_supports_op_p (ntype, NEGATE_EXPR, optab_vector))
9078 ntype = stype;
9079 }
9080 (if (fmt_new->signbit_rw
9081 == fmt_old->signbit_rw + GET_MODE_UNIT_BITSIZE (vec_mode)
9082 && fmt_new->signbit_rw == fmt_new->signbit_ro
9083 && targetm.can_change_mode_class (TYPE_MODE (ntype),
9084 TYPE_MODE (type), ALL_REGS)
9085 && ((optimize_vectors_before_lowering_p ()
9086 && VECTOR_TYPE_P (ntype))
9087 || target_supports_op_p (ntype, NEGATE_EXPR, optab_vector)))
9088 (if (plusminus == PLUS_EXPR)
9089 (plus (view_convert:type (negate (view_convert:ntype @3))) @2)
9090 (minus @0 (view_convert:type
9091 (negate (view_convert:ntype @1))))))))))))))))
9092
9093 (simplify
9094 (vec_perm @0 @1 VECTOR_CST@2)
9095 (with
9096 {
9097 tree op0 = @0, op1 = @1, op2 = @2;
9098 machine_mode result_mode = TYPE_MODE (type);
9099 machine_mode op_mode = TYPE_MODE (TREE_TYPE (op0));
9100
9101 /* Build a vector of integers from the tree mask. */
9102 vec_perm_builder builder;
9103 }
9104 (if (tree_to_vec_perm_builder (&builder, op2))
9105 (with
9106 {
9107 /* Create a vec_perm_indices for the integer vector. */
9108 poly_uint64 nelts = TYPE_VECTOR_SUBPARTS (type);
9109 bool single_arg = (op0 == op1);
9110 vec_perm_indices sel (builder, single_arg ? 1 : 2, nelts);
9111 }
9112 (if (sel.series_p (0, 1, 0, 1))
9113 { op0; }
9114 (if (sel.series_p (0, 1, nelts, 1))
9115 { op1; }
9116 (with
9117 {
9118 if (!single_arg)
9119 {
9120 if (sel.all_from_input_p (0))
9121 op1 = op0;
9122 else if (sel.all_from_input_p (1))
9123 {
9124 op0 = op1;
9125 sel.rotate_inputs (1);
9126 }
9127 else if (known_ge (poly_uint64 (sel[0]), nelts))
9128 {
9129 std::swap (op0, op1);
9130 sel.rotate_inputs (1);
9131 }
9132 }
9133 gassign *def;
9134 tree cop0 = op0, cop1 = op1;
9135 if (TREE_CODE (op0) == SSA_NAME
9136 && (def = dyn_cast <gassign *> (SSA_NAME_DEF_STMT (op0)))
9137 && gimple_assign_rhs_code (def) == CONSTRUCTOR)
9138 cop0 = gimple_assign_rhs1 (def);
9139 if (TREE_CODE (op1) == SSA_NAME
9140 && (def = dyn_cast <gassign *> (SSA_NAME_DEF_STMT (op1)))
9141 && gimple_assign_rhs_code (def) == CONSTRUCTOR)
9142 cop1 = gimple_assign_rhs1 (def);
9143 tree t;
9144 }
9145 (if ((TREE_CODE (cop0) == VECTOR_CST
9146 || TREE_CODE (cop0) == CONSTRUCTOR)
9147 && (TREE_CODE (cop1) == VECTOR_CST
9148 || TREE_CODE (cop1) == CONSTRUCTOR)
9149 && (t = fold_vec_perm (type, cop0, cop1, sel)))
9150 { t; }
9151 (with
9152 {
9153 bool changed = (op0 == op1 && !single_arg);
9154 tree ins = NULL_TREE;
9155 unsigned at = 0;
9156
9157 /* See if the permutation is performing a single element
9158 insert from a CONSTRUCTOR or constant and use a BIT_INSERT_EXPR
9159 in that case. But only if the vector mode is supported,
9160 otherwise this is invalid GIMPLE. */
9161 if (op_mode != BLKmode
9162 && (TREE_CODE (cop0) == VECTOR_CST
9163 || TREE_CODE (cop0) == CONSTRUCTOR
9164 || TREE_CODE (cop1) == VECTOR_CST
9165 || TREE_CODE (cop1) == CONSTRUCTOR))
9166 {
9167 bool insert_first_p = sel.series_p (1, 1, nelts + 1, 1);
9168 if (insert_first_p)
9169 {
9170 /* After canonicalizing the first elt to come from the
9171 first vector we only can insert the first elt from
9172 the first vector. */
9173 at = 0;
9174 if ((ins = fold_read_from_vector (cop0, sel[0])))
9175 op0 = op1;
9176 }
9177 /* The above can fail for two-element vectors which always
9178 appear to insert the first element, so try inserting
9179 into the second lane as well. For more than two
9180 elements that's wasted time. */
9181 if (!insert_first_p || (!ins && maybe_eq (nelts, 2u)))
9182 {
9183 unsigned int encoded_nelts = sel.encoding ().encoded_nelts ();
9184 for (at = 0; at < encoded_nelts; ++at)
9185 if (maybe_ne (sel[at], at))
9186 break;
9187 if (at < encoded_nelts
9188 && (known_eq (at + 1, nelts)
9189 || sel.series_p (at + 1, 1, at + 1, 1)))
9190 {
9191 if (known_lt (poly_uint64 (sel[at]), nelts))
9192 ins = fold_read_from_vector (cop0, sel[at]);
9193 else
9194 ins = fold_read_from_vector (cop1, sel[at] - nelts);
9195 }
9196 }
9197 }
9198
9199 /* Generate a canonical form of the selector. */
9200 if (!ins && sel.encoding () != builder)
9201 {
9202 /* Some targets are deficient and fail to expand a single
9203 argument permutation while still allowing an equivalent
9204 2-argument version. */
9205 tree oldop2 = op2;
9206 if (sel.ninputs () == 2
9207 || can_vec_perm_const_p (result_mode, op_mode, sel, false))
9208 op2 = vec_perm_indices_to_tree (TREE_TYPE (op2), sel);
9209 else
9210 {
9211 vec_perm_indices sel2 (builder, 2, nelts);
9212 if (can_vec_perm_const_p (result_mode, op_mode, sel2, false))
9213 op2 = vec_perm_indices_to_tree (TREE_TYPE (op2), sel2);
9214 else
9215 /* Not directly supported with either encoding,
9216 so use the preferred form. */
9217 op2 = vec_perm_indices_to_tree (TREE_TYPE (op2), sel);
9218 }
9219 if (!operand_equal_p (op2, oldop2, 0))
9220 changed = true;
9221 }
9222 }
9223 (if (ins)
9224 (bit_insert { op0; } { ins; }
9225 { bitsize_int (at * vector_element_bits (type)); })
9226 (if (changed)
9227 (vec_perm { op0; } { op1; } { op2; }))))))))))))
9228
9229 /* VEC_PERM_EXPR (v, v, mask) -> v where v contains same element. */
9230
9231 (match vec_same_elem_p
9232 (vec_duplicate @0))
9233
9234 (match vec_same_elem_p
9235 CONSTRUCTOR@0
9236 (if (TREE_CODE (@0) == SSA_NAME
9237 && uniform_vector_p (gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0))))))
9238
9239 (match vec_same_elem_p
9240 @0
9241 (if (uniform_vector_p (@0))))
9242
9243
9244 (simplify
9245 (vec_perm vec_same_elem_p@0 @0 @1)
9246 (if (types_match (type, TREE_TYPE (@0)))
9247 @0
9248 (with
9249 {
9250 tree elem = uniform_vector_p (@0);
9251 }
9252 (if (elem)
9253 { build_vector_from_val (type, elem); }))))
9254
9255 /* Push VEC_PERM earlier if that may help FMA perception (PR101895). */
9256 (simplify
9257 (plus:c (vec_perm:s (mult:c@0 @1 vec_same_elem_p@2) @0 @3) @4)
9258 (if (TREE_CODE (@0) == SSA_NAME && num_imm_uses (@0) == 2)
9259 (plus (mult (vec_perm @1 @1 @3) @2) @4)))
9260 (simplify
9261 (minus (vec_perm:s (mult:c@0 @1 vec_same_elem_p@2) @0 @3) @4)
9262 (if (TREE_CODE (@0) == SSA_NAME && num_imm_uses (@0) == 2)
9263 (minus (mult (vec_perm @1 @1 @3) @2) @4)))
9264
9265
9266 /* Merge
9267 c = VEC_PERM_EXPR <a, b, VCST0>;
9268 d = VEC_PERM_EXPR <c, c, VCST1>;
9269 to
9270 d = VEC_PERM_EXPR <a, b, NEW_VCST>; */
9271
9272 (simplify
9273 (vec_perm (vec_perm@0 @1 @2 VECTOR_CST@3) @0 VECTOR_CST@4)
9274 (if (TYPE_VECTOR_SUBPARTS (type).is_constant ())
9275 (with
9276 {
9277 machine_mode result_mode = TYPE_MODE (type);
9278 machine_mode op_mode = TYPE_MODE (TREE_TYPE (@1));
9279 int nelts = TYPE_VECTOR_SUBPARTS (type).to_constant ();
9280 vec_perm_builder builder0;
9281 vec_perm_builder builder1;
9282 vec_perm_builder builder2 (nelts, nelts, 1);
9283 }
9284 (if (tree_to_vec_perm_builder (&builder0, @3)
9285 && tree_to_vec_perm_builder (&builder1, @4))
9286 (with
9287 {
9288 vec_perm_indices sel0 (builder0, 2, nelts);
9289 vec_perm_indices sel1 (builder1, 1, nelts);
9290
9291 for (int i = 0; i < nelts; i++)
9292 builder2.quick_push (sel0[sel1[i].to_constant ()]);
9293
9294 vec_perm_indices sel2 (builder2, 2, nelts);
9295
9296 tree op0 = NULL_TREE;
9297 /* If the new VEC_PERM_EXPR can't be handled but both
9298 original VEC_PERM_EXPRs can, punt.
9299 If one or both of the original VEC_PERM_EXPRs can't be
9300 handled and the new one can't be either, don't increase
9301 number of VEC_PERM_EXPRs that can't be handled. */
9302 if (can_vec_perm_const_p (result_mode, op_mode, sel2, false)
9303 || (single_use (@0)
9304 ? (!can_vec_perm_const_p (result_mode, op_mode, sel0, false)
9305 || !can_vec_perm_const_p (result_mode, op_mode, sel1, false))
9306 : !can_vec_perm_const_p (result_mode, op_mode, sel1, false)))
9307 op0 = vec_perm_indices_to_tree (TREE_TYPE (@4), sel2);
9308 }
9309 (if (op0)
9310 (vec_perm @1 @2 { op0; })))))))
9311
9312 /* Merge
9313 c = VEC_PERM_EXPR <a, b, VCST0>;
9314 d = VEC_PERM_EXPR <x, c, VCST1>;
9315 to
9316 d = VEC_PERM_EXPR <x, {a,b}, NEW_VCST>;
9317 when all elements from a or b are replaced by the later
9318 permutation. */
9319
9320 (simplify
9321 (vec_perm @5 (vec_perm@0 @1 @2 VECTOR_CST@3) VECTOR_CST@4)
9322 (if (TYPE_VECTOR_SUBPARTS (type).is_constant ())
9323 (with
9324 {
9325 machine_mode result_mode = TYPE_MODE (type);
9326 machine_mode op_mode = TYPE_MODE (TREE_TYPE (@1));
9327 int nelts = TYPE_VECTOR_SUBPARTS (type).to_constant ();
9328 vec_perm_builder builder0;
9329 vec_perm_builder builder1;
9330 vec_perm_builder builder2 (nelts, nelts, 2);
9331 }
9332 (if (tree_to_vec_perm_builder (&builder0, @3)
9333 && tree_to_vec_perm_builder (&builder1, @4))
9334 (with
9335 {
9336 vec_perm_indices sel0 (builder0, 2, nelts);
9337 vec_perm_indices sel1 (builder1, 2, nelts);
9338 bool use_1 = false, use_2 = false;
9339
9340 for (int i = 0; i < nelts; i++)
9341 {
9342 if (known_lt ((poly_uint64)sel1[i], sel1.nelts_per_input ()))
9343 builder2.quick_push (sel1[i]);
9344 else
9345 {
9346 poly_uint64 j = sel0[(sel1[i] - sel1.nelts_per_input ())
9347 .to_constant ()];
9348 if (known_lt (j, sel0.nelts_per_input ()))
9349 use_1 = true;
9350 else
9351 {
9352 use_2 = true;
9353 j -= sel0.nelts_per_input ();
9354 }
9355 builder2.quick_push (j + sel1.nelts_per_input ());
9356 }
9357 }
9358 }
9359 (if (use_1 ^ use_2)
9360 (with
9361 {
9362 vec_perm_indices sel2 (builder2, 2, nelts);
9363 tree op0 = NULL_TREE;
9364 /* If the new VEC_PERM_EXPR can't be handled but both
9365 original VEC_PERM_EXPRs can, punt.
9366 If one or both of the original VEC_PERM_EXPRs can't be
9367 handled and the new one can't be either, don't increase
9368 number of VEC_PERM_EXPRs that can't be handled. */
9369 if (can_vec_perm_const_p (result_mode, op_mode, sel2, false)
9370 || (single_use (@0)
9371 ? (!can_vec_perm_const_p (result_mode, op_mode, sel0, false)
9372 || !can_vec_perm_const_p (result_mode, op_mode, sel1, false))
9373 : !can_vec_perm_const_p (result_mode, op_mode, sel1, false)))
9374 op0 = vec_perm_indices_to_tree (TREE_TYPE (@4), sel2);
9375 }
9376 (if (op0)
9377 (switch
9378 (if (use_1)
9379 (vec_perm @5 @1 { op0; }))
9380 (if (use_2)
9381 (vec_perm @5 @2 { op0; })))))))))))
9382
9383 /* And the case with swapped outer permute sources. */
9384
9385 (simplify
9386 (vec_perm (vec_perm@0 @1 @2 VECTOR_CST@3) @5 VECTOR_CST@4)
9387 (if (TYPE_VECTOR_SUBPARTS (type).is_constant ())
9388 (with
9389 {
9390 machine_mode result_mode = TYPE_MODE (type);
9391 machine_mode op_mode = TYPE_MODE (TREE_TYPE (@1));
9392 int nelts = TYPE_VECTOR_SUBPARTS (type).to_constant ();
9393 vec_perm_builder builder0;
9394 vec_perm_builder builder1;
9395 vec_perm_builder builder2 (nelts, nelts, 2);
9396 }
9397 (if (tree_to_vec_perm_builder (&builder0, @3)
9398 && tree_to_vec_perm_builder (&builder1, @4))
9399 (with
9400 {
9401 vec_perm_indices sel0 (builder0, 2, nelts);
9402 vec_perm_indices sel1 (builder1, 2, nelts);
9403 bool use_1 = false, use_2 = false;
9404
9405 for (int i = 0; i < nelts; i++)
9406 {
9407 if (known_ge ((poly_uint64)sel1[i], sel1.nelts_per_input ()))
9408 builder2.quick_push (sel1[i]);
9409 else
9410 {
9411 poly_uint64 j = sel0[sel1[i].to_constant ()];
9412 if (known_lt (j, sel0.nelts_per_input ()))
9413 use_1 = true;
9414 else
9415 {
9416 use_2 = true;
9417 j -= sel0.nelts_per_input ();
9418 }
9419 builder2.quick_push (j);
9420 }
9421 }
9422 }
9423 (if (use_1 ^ use_2)
9424 (with
9425 {
9426 vec_perm_indices sel2 (builder2, 2, nelts);
9427 tree op0 = NULL_TREE;
9428 /* If the new VEC_PERM_EXPR can't be handled but both
9429 original VEC_PERM_EXPRs can, punt.
9430 If one or both of the original VEC_PERM_EXPRs can't be
9431 handled and the new one can't be either, don't increase
9432 number of VEC_PERM_EXPRs that can't be handled. */
9433 if (can_vec_perm_const_p (result_mode, op_mode, sel2, false)
9434 || (single_use (@0)
9435 ? (!can_vec_perm_const_p (result_mode, op_mode, sel0, false)
9436 || !can_vec_perm_const_p (result_mode, op_mode, sel1, false))
9437 : !can_vec_perm_const_p (result_mode, op_mode, sel1, false)))
9438 op0 = vec_perm_indices_to_tree (TREE_TYPE (@4), sel2);
9439 }
9440 (if (op0)
9441 (switch
9442 (if (use_1)
9443 (vec_perm @1 @5 { op0; }))
9444 (if (use_2)
9445 (vec_perm @2 @5 { op0; })))))))))))
9446
9447
9448 /* Match count trailing zeroes for simplify_count_trailing_zeroes in fwprop.
9449 The canonical form is array[((x & -x) * C) >> SHIFT] where C is a magic
9450 constant which when multiplied by a power of 2 contains a unique value
9451 in the top 5 or 6 bits. This is then indexed into a table which maps it
9452 to the number of trailing zeroes. */
9453 (match (ctz_table_index @1 @2 @3)
9454 (rshift (mult (bit_and:c (negate @1) @1) INTEGER_CST@2) INTEGER_CST@3))
9455
9456 (match (cond_expr_convert_p @0 @2 @3 @6)
9457 (cond (simple_comparison@6 @0 @1) (convert@4 @2) (convert@5 @3))
9458 (if (INTEGRAL_TYPE_P (type)
9459 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
9460 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
9461 && INTEGRAL_TYPE_P (TREE_TYPE (@3))
9462 && TYPE_PRECISION (type) != TYPE_PRECISION (TREE_TYPE (@0))
9463 && TYPE_PRECISION (TREE_TYPE (@0))
9464 == TYPE_PRECISION (TREE_TYPE (@2))
9465 && TYPE_PRECISION (TREE_TYPE (@0))
9466 == TYPE_PRECISION (TREE_TYPE (@3))
9467 /* For vect_recog_cond_expr_convert_pattern, @2 and @3 can differ in
9468 signess when convert is truncation, but not ok for extension since
9469 it's sign_extend vs zero_extend. */
9470 && (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type)
9471 || (TYPE_UNSIGNED (TREE_TYPE (@2))
9472 == TYPE_UNSIGNED (TREE_TYPE (@3))))
9473 && single_use (@4)
9474 && single_use (@5))))
9475
9476 (for bit_op (bit_and bit_ior bit_xor)
9477 (match (bitwise_induction_p @0 @2 @3)
9478 (bit_op:c
9479 (nop_convert1? (bit_not2?@0 (convert3? (lshift integer_onep@1 @2))))
9480 @3)))
9481
9482 (match (bitwise_induction_p @0 @2 @3)
9483 (bit_not
9484 (nop_convert1? (bit_xor@0 (convert2? (lshift integer_onep@1 @2)) @3))))
9485
9486 /* n - (((n > C1) ? n : C1) & -C2) -> n & C1 for unsigned case.
9487 n - (((n > C1) ? n : C1) & -C2) -> (n <= C1) ? n : (n & C1) for signed case. */
9488 (simplify
9489 (minus @0 (bit_and (max @0 INTEGER_CST@1) INTEGER_CST@2))
9490 (with { auto i = wi::neg (wi::to_wide (@2)); }
9491 /* Check if -C2 is a power of 2 and C1 = -C2 - 1. */
9492 (if (wi::popcount (i) == 1
9493 && (wi::to_wide (@1)) == (i - 1))
9494 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
9495 (bit_and @0 @1)
9496 (cond (le @0 @1) @0 (bit_and @0 @1))))))
9497
9498 /* -x & 1 -> x & 1. */
9499 (simplify
9500 (bit_and (negate @0) integer_onep@1)
9501 (if (!TYPE_OVERFLOW_SANITIZED (type))
9502 (bit_and @0 @1)))
9503
9504 /* `-a` is just `a` if the type is 1bit wide or when converting
9505 to a 1bit type; similar to the above transformation of `(-x)&1`.
9506 This is used mostly with the transformation of
9507 `a ? ~b : b` into `(-a)^b`.
9508 It also can show up with bitfields. */
9509 (simplify
9510 (convert? (negate @0))
9511 (if (INTEGRAL_TYPE_P (type)
9512 && TYPE_PRECISION (type) == 1
9513 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0)))
9514 (convert @0)))
9515
9516 /* Optimize
9517 c1 = VEC_PERM_EXPR (a, a, mask)
9518 c2 = VEC_PERM_EXPR (b, b, mask)
9519 c3 = c1 op c2
9520 -->
9521 c = a op b
9522 c3 = VEC_PERM_EXPR (c, c, mask)
9523 For all integer non-div operations. */
9524 (for op (plus minus mult bit_and bit_ior bit_xor
9525 lshift rshift)
9526 (simplify
9527 (op (vec_perm @0 @0 @2) (vec_perm @1 @1 @2))
9528 (if (VECTOR_INTEGER_TYPE_P (type))
9529 (vec_perm (op@3 @0 @1) @3 @2))))
9530
9531 /* Similar for float arithmetic when permutation constant covers
9532 all vector elements. */
9533 (for op (plus minus mult)
9534 (simplify
9535 (op (vec_perm @0 @0 VECTOR_CST@2) (vec_perm @1 @1 VECTOR_CST@2))
9536 (if (VECTOR_FLOAT_TYPE_P (type)
9537 && TYPE_VECTOR_SUBPARTS (type).is_constant ())
9538 (with
9539 {
9540 tree perm_cst = @2;
9541 vec_perm_builder builder;
9542 bool full_perm_p = false;
9543 if (tree_to_vec_perm_builder (&builder, perm_cst))
9544 {
9545 unsigned HOST_WIDE_INT nelts;
9546
9547 nelts = TYPE_VECTOR_SUBPARTS (type).to_constant ();
9548 /* Create a vec_perm_indices for the VECTOR_CST. */
9549 vec_perm_indices sel (builder, 1, nelts);
9550
9551 /* Check if perm indices covers all vector elements. */
9552 if (sel.encoding ().encoded_full_vector_p ())
9553 {
9554 auto_sbitmap seen (nelts);
9555 bitmap_clear (seen);
9556
9557 unsigned HOST_WIDE_INT count = 0, i;
9558
9559 for (i = 0; i < nelts; i++)
9560 {
9561 if (!bitmap_set_bit (seen, sel[i].to_constant ()))
9562 break;
9563 count++;
9564 }
9565 full_perm_p = count == nelts;
9566 }
9567 }
9568 }
9569 (if (full_perm_p)
9570 (vec_perm (op@3 @0 @1) @3 @2))))))