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.
5 Copyright (C) 2014-2023 Free Software Foundation, Inc.
6 Contributed by Richard Biener <rguenther@suse.de>
7 and Prathamesh Kulkarni <bilbotheelffriend@gmail.com>
9 This file is part of GCC.
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
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
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/>. */
26 /* Generic tree predicates we inherit. */
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
32 initializer_each_zero_or_onep
34 tree_expr_nonnegative_p
42 bitmask_inv_cst_vector_p)
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)
58 #include "cfn-operators.pd"
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.
64 Also define operand lists:
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 \
73 (define_operator_list X##FN BUILT_IN_I##FN \
76 (define_operator_list X##FN##L BUILT_IN_I##FN##L \
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)
85 /* Unary operations and their associated IFN_COND_* function. */
86 (define_operator_list UNCOND_UNARY
88 (define_operator_list COND_UNARY
89 IFN_COND_NEG IFN_COND_NOT)
91 /* Binary operations and their associated IFN_COND_* function. */
92 (define_operator_list UNCOND_BINARY
94 mult trunc_div trunc_mod rdiv
97 bit_and bit_ior bit_xor
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)
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)
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)
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)
150 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))))
151 (match (nop_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))))))
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)
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)
169 (match (maybe_cmp @0)
170 (convert (cmp@0 @1 @2))
171 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))))
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)))))
186 /* Optimize (X + (X >> (prec - 1))) ^ (X >> (prec - 1)) into abs (X). */
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)
195 /* Simplifications of operations with one constant operand and
196 simplifications to constants or single values. */
198 (for op (plus pointer_plus minus bit_ior bit_xor)
200 (op @0 integer_zerop)
203 /* 0 +p index -> (type)index */
205 (pointer_plus integer_zerop @1)
206 (non_lvalue (convert @1)))
208 /* ptr - 0 -> (type)ptr */
210 (pointer_diff @0 integer_zerop)
213 /* See if ARG1 is zero and X + ARG1 reduces to X.
214 Likewise if the operands are reversed. */
216 (plus:c @0 real_zerop@1)
217 (if (fold_real_zero_addition_p (type, @0, @1, 0))
220 /* See if ARG1 is zero and X - ARG1 reduces to X. */
222 (minus @0 real_zerop@1)
223 (if (fold_real_zero_addition_p (type, @0, @1, 1))
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)
235 (outer_op (inner_op@3 @0 REAL_CST@1) REAL_CST@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)));
242 = ((outer_op == PLUS_EXPR)
243 ^ REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@2))); }
244 (if (outer_plus && !inner_plus)
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
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); }))
263 (pointer_diff @@0 @0)
264 { build_zero_cst (type); })
267 (mult @0 integer_zerop@1)
270 /* -x == x -> x == 0 */
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)); }))))
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. */
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)))
290 /* In IEEE floating point, x*1 is not equivalent to x for snans.
291 Likewise for complex arithmetic with signed zeros. */
294 (if (!tree_expr_maybe_signaling_nan_p (@0)
295 && (!HONOR_SIGNED_ZEROS (type)
296 || !COMPLEX_FLOAT_TYPE_P (type)))
299 /* Transform x * -1.0 into -x. */
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)))
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. */
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; }
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); })))))))
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. */
328 (mult (bit_and (rshift @0 uniform_integer_cst_p@1)
329 uniform_integer_cst_p@2)
330 uniform_integer_cst_p@3)
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);
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. */
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;
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;
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. */
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);
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)
382 { ones; } { zeros; })))))))))
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). */
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))
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). */
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))
404 /* Transform X * copysign (1.0, X) into abs(X). */
406 (mult:c @0 (COPYSIGN_ALL real_onep @0))
407 (if (!tree_expr_maybe_nan_p (@0) && !HONOR_SIGNED_ZEROS (type))
410 /* Transform X * copysign (1.0, -X) into -abs(X). */
412 (mult:c @0 (COPYSIGN_ALL real_onep (negate @0)))
413 (if (!tree_expr_maybe_nan_p (@0) && !HONOR_SIGNED_ZEROS (type))
416 /* Transform copysign (CST, X) into copysign (ABS(CST), X). */
418 (COPYSIGN_ALL REAL_CST@0 @1)
419 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@0)))
420 (COPYSIGN_ALL (negate @0) @1)))
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). */
426 (IFN_COND_MUL:c @0 @1 (IFN_COPYSIGN real_onep @2) @3)
427 (with { tree signs = sign_mask_for (type); }
429 (with { tree inttype = TREE_TYPE (signs); }
431 (IFN_COND_XOR:inttype @0
432 (view_convert:inttype @1)
433 (bit_and (view_convert:inttype @2) { signs; })
434 (view_convert:inttype @3)))))))
436 /* (x >= 0 ? x : 0) + (x <= 0 ? -x : 0) -> abs x. */
438 (plus:c (max @0 integer_zerop) (max (negate @0) integer_zerop))
441 /* X * 1, X / 1 -> X. */
442 (for op (mult trunc_div ceil_div floor_div round_div exact_div)
447 /* (A / (1 << B)) -> (A >> B).
448 Only for unsigned A. For signed A, this would not preserve rounding
450 For example: (-1 / ( 1 << B)) != -1 >> B.
451 Also handle widening conversions, like:
452 (A / (unsigned long long) (1U << B)) -> (A >> B)
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. */
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,
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))
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. */
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))
493 (div @0 integer_minus_onep@1)
494 (if (!TYPE_UNSIGNED (type))
496 /* X / bool_range_Y is X. */
499 (if (INTEGRAL_TYPE_P (type)
500 && ssa_name_has_boolean_range (@1)
501 && !flag_non_call_exceptions)
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. */
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); })))
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); })))
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)
537 (if ((INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
538 && TYPE_UNSIGNED (type))
539 (trunc_divmod @0 @1))))
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
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); })))))
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)
563 (div (div@3 @0 INTEGER_CST@1) INTEGER_CST@2)
565 wi::overflow_type overflow;
566 wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@2),
567 TYPE_SIGN (type), &overflow);
569 (if (div == EXACT_DIV_EXPR
570 || optimize_successive_divisions_p (@2, @3))
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); }))))))
577 /* Combine successive multiplications. Similar to above, but handling
578 overflow is different. */
580 (mult (mult @0 INTEGER_CST@1) INTEGER_CST@2)
582 wi::overflow_type overflow;
583 wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@2),
584 TYPE_SIGN (type), &overflow);
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); }))))
591 /* Similar to above, but there could be an extra add/sub between
592 successive multuiplications. */
594 (mult (plus:s (mult:s@4 @0 INTEGER_CST@1) INTEGER_CST@2) INTEGER_CST@3)
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))
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 ())
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)
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)
627 /* Skip folding on overflow. */
629 (plus (mult @0 { wide_int_to_tree (type, mul); })
630 { wide_int_to_tree (type, add); }))))
632 /* Similar to above, but a multiplication between successive additions. */
634 (plus (mult:s (plus:s @0 INTEGER_CST@1) INTEGER_CST@2) INTEGER_CST@3)
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))
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 ())
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)
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)
668 /* Skip folding on overflow. */
670 (plus (mult @0 @2) { wide_int_to_tree (type, add); }))))
672 /* Optimize A / A to 1.0 if we don't care about
673 NaNs or Infinities. */
676 (if (FLOAT_TYPE_P (type)
677 && ! HONOR_NANS (type)
678 && ! HONOR_INFINITIES (type))
679 { build_one_cst (type); }))
681 /* Optimize -A / A to -1.0 if we don't care about
682 NaNs or Infinities. */
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); }))
690 /* PR71078: x / abs(x) -> copysign (1.0, x) */
692 (rdiv:C (convert? @0) (convert? (abs @0)))
693 (if (SCALAR_FLOAT_TYPE_P (type)
694 && ! HONOR_NANS (type)
695 && ! HONOR_INFINITIES (type))
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))))))
704 /* In IEEE floating point, x/1 is not equivalent to x for snans. */
707 (if (!tree_expr_maybe_signaling_nan_p (@0))
710 /* In IEEE floating point, x/-1 is not equivalent to -x for snans. */
712 (rdiv @0 real_minus_onep)
713 (if (!tree_expr_maybe_signaling_nan_p (@0))
716 (if (flag_reciprocal_math)
717 /* Convert (A/B)/C to A/(B*C). */
719 (rdiv (rdiv:s @0 @1) @2)
720 (rdiv @0 (mult @1 @2)))
722 /* Canonicalize x / (C1 * y) to (x * C2) / y. */
724 (rdiv @0 (mult:s @1 REAL_CST@2))
726 { tree tem = const_binop (RDIV_EXPR, type, build_one_cst (type), @2); }
728 (rdiv (mult @0 { tem; } ) @1))))
730 /* Convert A/(B/C) to (A/B)*C */
732 (rdiv @0 (rdiv:s @1 @2))
733 (mult (rdiv @0 @1) @2)))
735 /* Simplify x / (- y) to -x / y. */
737 (rdiv @0 (negate @1))
738 (rdiv (negate @0) @1))
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)
746 (op (rdiv REAL_CST@0 @1) real_zerop@2)
747 (if (!HONOR_SIGNED_ZEROS (@1) && !HONOR_INFINITIES (@1))
749 (if (real_less (&dconst0, TREE_REAL_CST_PTR (@0)))
751 /* For C < 0, use the inverted operator. */
752 (if (real_less (TREE_REAL_CST_PTR (@0), &dconst0))
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)
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)
764 { build_int_cst (integer_type_node,
765 wi::exact_log2 (wi::to_wide (@2))); }))))
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)
776 (if (flag_reciprocal_math
779 { tree tem = const_binop (RDIV_EXPR, type, build_one_cst (type), @1); }
781 (mult @0 { tem; } )))
782 (if (cst != COMPLEX_CST)
783 (with { tree inverse = exact_inverse (type, @1); }
785 (mult @0 { inverse; } ))))))))
787 (for mod (ceil_mod floor_mod round_mod trunc_mod)
788 /* 0 % X is always zero. */
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))
794 /* X % 1 is always zero. */
796 (mod @0 integer_onep)
797 { build_zero_cst (type); })
798 /* X % -1 is zero. */
800 (mod @0 integer_minus_onep@1)
801 (if (!TYPE_UNSIGNED (type))
802 { build_zero_cst (type); }))
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. */
811 (mod (mod@2 @0 @1) @1)
813 /* From extract_muldiv_1: (X * C1) % C2 is zero if C1 is a multiple of C2. */
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),
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. */
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)))))))
830 /* X % -C is the same as X % C. */
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))))
841 /* X % -Y is the same as X % Y. */
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
854 (trunc_mod @0 (convert @1))))
856 /* X - (X / Y) * Y is the same as X % Y. */
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))))
862 /* x * (1 + y / x) - y -> x - y % x */
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))))
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
876 (match (power_of_two_cand @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)
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),
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); })))))))
906 /* Simplify (unsigned t * 2)/2 -> unsigned t & 0x7FFFFFFF. */
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))); })))
915 /* Simplify (unsigned t / 2) * 2 -> unsigned t & ~1. */
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))))
921 /* Simplify (t * 2) / 2) -> t. */
922 (for div (trunc_div ceil_div floor_div round_div exact_div)
924 (div (mult:c @0 @1) @1)
925 (if (ANY_INTEGRAL_TYPE_P (type))
926 (if (TYPE_OVERFLOW_UNDEFINED (type))
931 bool overflowed = true;
932 value_range vr0, vr1;
933 if (INTEGRAL_TYPE_P (type)
934 && get_global_range_query ()->range_of_expr (vr0, @0)
935 && get_global_range_query ()->range_of_expr (vr1, @1)
936 && !vr0.varying_p () && !vr0.undefined_p ()
937 && !vr1.varying_p () && !vr1.undefined_p ())
939 wide_int wmin0 = vr0.lower_bound ();
940 wide_int wmax0 = vr0.upper_bound ();
941 wide_int wmin1 = vr1.lower_bound ();
942 wide_int wmax1 = vr1.upper_bound ();
943 /* If the multiplication can't overflow/wrap around, then
944 it can be optimized too. */
945 wi::overflow_type min_ovf, max_ovf;
946 wi::mul (wmin0, wmin1, TYPE_SIGN (type), &min_ovf);
947 wi::mul (wmax0, wmax1, TYPE_SIGN (type), &max_ovf);
948 if (min_ovf == wi::OVF_NONE && max_ovf == wi::OVF_NONE)
950 wi::mul (wmin0, wmax1, TYPE_SIGN (type), &min_ovf);
951 wi::mul (wmax0, wmin1, TYPE_SIGN (type), &max_ovf);
952 if (min_ovf == wi::OVF_NONE && max_ovf == wi::OVF_NONE)
963 (for div (trunc_div exact_div)
964 /* Simplify (X + M*N) / N -> X / N + M. */
966 (div (plus:c@4 @0 (mult:c@3 @1 @2)) @2)
967 (with {value_range vr0, vr1, vr2, vr3, vr4;}
968 (if (INTEGRAL_TYPE_P (type)
969 && get_range_query (cfun)->range_of_expr (vr1, @1)
970 && get_range_query (cfun)->range_of_expr (vr2, @2)
971 /* "N*M" doesn't overflow. */
972 && range_op_handler (MULT_EXPR).overflow_free_p (vr1, vr2)
973 && get_range_query (cfun)->range_of_expr (vr0, @0)
974 && get_range_query (cfun)->range_of_expr (vr3, @3)
975 /* "X+(N*M)" doesn't overflow. */
976 && range_op_handler (PLUS_EXPR).overflow_free_p (vr0, vr3)
977 && get_range_query (cfun)->range_of_expr (vr4, @4)
978 && !vr4.undefined_p ()
979 /* "X+N*M" is not with opposite sign as "X". */
980 && (TYPE_UNSIGNED (type)
981 || (vr0.nonnegative_p () && vr4.nonnegative_p ())
982 || (vr0.nonpositive_p () && vr4.nonpositive_p ())))
983 (plus (div @0 @2) @1))))
985 /* Simplify (X - M*N) / N -> X / N - M. */
987 (div (minus@4 @0 (mult:c@3 @1 @2)) @2)
988 (with {value_range vr0, vr1, vr2, vr3, vr4;}
989 (if (INTEGRAL_TYPE_P (type)
990 && get_range_query (cfun)->range_of_expr (vr1, @1)
991 && get_range_query (cfun)->range_of_expr (vr2, @2)
992 /* "N * M" doesn't overflow. */
993 && range_op_handler (MULT_EXPR).overflow_free_p (vr1, vr2)
994 && get_range_query (cfun)->range_of_expr (vr0, @0)
995 && get_range_query (cfun)->range_of_expr (vr3, @3)
996 /* "X - (N*M)" doesn't overflow. */
997 && range_op_handler (MINUS_EXPR).overflow_free_p (vr0, vr3)
998 && get_range_query (cfun)->range_of_expr (vr4, @4)
999 && !vr4.undefined_p ()
1000 /* "X-N*M" is not with opposite sign as "X". */
1001 && (TYPE_UNSIGNED (type)
1002 || (vr0.nonnegative_p () && vr4.nonnegative_p ())
1003 || (vr0.nonpositive_p () && vr4.nonpositive_p ())))
1004 (minus (div @0 @2) @1)))))
1007 (X + C) / N -> X / N + C / N where C is multiple of N.
1008 (X + C) >> N -> X >> N + C>>N if low N bits of C is 0. */
1009 (for op (trunc_div exact_div rshift)
1011 (op (plus@3 @0 INTEGER_CST@1) INTEGER_CST@2)
1014 wide_int c = wi::to_wide (@1);
1015 wide_int n = wi::to_wide (@2);
1016 bool shift = op == RSHIFT_EXPR;
1017 #define plus_op1(v) (shift ? wi::rshift (v, n, TYPE_SIGN (type)) \
1018 : wi::div_trunc (v, n, TYPE_SIGN (type)))
1019 #define exact_mod(v) (shift ? wi::ctz (v) >= n.to_shwi () \
1020 : wi::multiple_of_p (v, n, TYPE_SIGN (type)))
1021 value_range vr0, vr1, vr3;
1023 (if (INTEGRAL_TYPE_P (type)
1024 && get_range_query (cfun)->range_of_expr (vr0, @0))
1026 && get_range_query (cfun)->range_of_expr (vr1, @1)
1027 /* "X+C" doesn't overflow. */
1028 && range_op_handler (PLUS_EXPR).overflow_free_p (vr0, vr1)
1029 && get_range_query (cfun)->range_of_expr (vr3, @3)
1030 && !vr3.undefined_p ()
1031 /* "X+C" and "X" are not of opposite sign. */
1032 && (TYPE_UNSIGNED (type)
1033 || (vr0.nonnegative_p () && vr3.nonnegative_p ())
1034 || (vr0.nonpositive_p () && vr3.nonpositive_p ())))
1035 (plus (op @0 @2) { wide_int_to_tree (type, plus_op1 (c)); })
1036 (if (TYPE_UNSIGNED (type) && c.sign_mask () < 0
1038 /* unsigned "X-(-C)" doesn't underflow. */
1039 && wi::geu_p (vr0.lower_bound (), -c))
1040 (plus (op @0 @2) { wide_int_to_tree (type, -plus_op1 (-c)); })))))))
1045 /* (nop_outer_cast)-(inner_cast)var -> -(outer_cast)(var)
1046 if var is smaller in precision.
1047 This is always safe for both doing the negative in signed or unsigned
1048 as the value for undefined will not show up. */
1050 (convert (negate:s@1 (convert:s @0)))
1051 (if (INTEGRAL_TYPE_P (type)
1052 && tree_nop_conversion_p (type, TREE_TYPE (@1))
1053 && TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (@0)))
1054 (negate (convert @0))))
1056 (for op (negate abs)
1057 /* Simplify cos(-x) and cos(|x|) -> cos(x). Similarly for cosh. */
1058 (for coss (COS COSH)
1062 /* Simplify pow(-x, y) and pow(|x|,y) -> pow(x,y) if y is an even integer. */
1065 (pows (op @0) REAL_CST@1)
1066 (with { HOST_WIDE_INT n; }
1067 (if (real_isinteger (&TREE_REAL_CST (@1), &n) && (n & 1) == 0)
1069 /* Likewise for powi. */
1072 (pows (op @0) INTEGER_CST@1)
1073 (if ((wi::to_wide (@1) & 1) == 0)
1075 /* Strip negate and abs from both operands of hypot. */
1083 /* copysign(-x, y) and copysign(abs(x), y) -> copysign(x, y). */
1084 (for copysigns (COPYSIGN_ALL)
1086 (copysigns (op @0) @1)
1087 (copysigns @0 @1))))
1089 /* abs(x)*abs(x) -> x*x. Should be valid for all types. */
1091 (mult (abs@1 @0) @1)
1094 /* Convert absu(x)*absu(x) -> x*x. */
1096 (mult (absu@1 @0) @1)
1097 (mult (convert@2 @0) @2))
1099 /* cos(copysign(x, y)) -> cos(x). Similarly for cosh. */
1100 (for coss (COS COSH)
1101 copysigns (COPYSIGN)
1103 (coss (copysigns @0 @1))
1106 /* pow(copysign(x, y), z) -> pow(x, z) if z is an even integer. */
1108 copysigns (COPYSIGN)
1110 (pows (copysigns @0 @2) REAL_CST@1)
1111 (with { HOST_WIDE_INT n; }
1112 (if (real_isinteger (&TREE_REAL_CST (@1), &n) && (n & 1) == 0)
1114 /* Likewise for powi. */
1116 copysigns (COPYSIGN)
1118 (pows (copysigns @0 @2) INTEGER_CST@1)
1119 (if ((wi::to_wide (@1) & 1) == 0)
1123 copysigns (COPYSIGN)
1124 /* hypot(copysign(x, y), z) -> hypot(x, z). */
1126 (hypots (copysigns @0 @1) @2)
1128 /* hypot(x, copysign(y, z)) -> hypot(x, y). */
1130 (hypots @0 (copysigns @1 @2))
1133 /* copysign(x, CST) -> [-]abs (x). */
1134 (for copysigns (COPYSIGN_ALL)
1136 (copysigns @0 REAL_CST@1)
1137 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
1141 /* copysign(copysign(x, y), z) -> copysign(x, z). */
1142 (for copysigns (COPYSIGN_ALL)
1144 (copysigns (copysigns @0 @1) @2)
1147 /* copysign(x,y)*copysign(x,y) -> x*x. */
1148 (for copysigns (COPYSIGN_ALL)
1150 (mult (copysigns@2 @0 @1) @2)
1153 /* ccos(-x) -> ccos(x). Similarly for ccosh. */
1154 (for ccoss (CCOS CCOSH)
1159 /* cabs(-x) and cos(conj(x)) -> cabs(x). */
1160 (for ops (conj negate)
1166 /* Fold (a * (1 << b)) into (a << b) */
1168 (mult:c @0 (convert? (lshift integer_onep@1 @2)))
1169 (if (! FLOAT_TYPE_P (type)
1170 && tree_nop_conversion_p (type, TREE_TYPE (@1)))
1173 /* Shifts by precision or greater result in zero. */
1174 (for shift (lshift rshift)
1176 (shift @0 uniform_integer_cst_p@1)
1177 (if ((GIMPLE || !sanitize_flags_p (SANITIZE_SHIFT_EXPONENT))
1178 /* Leave arithmetic right shifts of possibly negative values alone. */
1179 && (TYPE_UNSIGNED (type)
1180 || shift == LSHIFT_EXPR
1181 || tree_expr_nonnegative_p (@0))
1182 /* Use a signed compare to leave negative shift counts alone. */
1183 && wi::ges_p (wi::to_wide (uniform_integer_cst_p (@1)),
1184 element_precision (type)))
1185 { build_zero_cst (type); })))
1187 /* Shifts by constants distribute over several binary operations,
1188 hence (X << C) + (Y << C) can be simplified to (X + Y) << C. */
1189 (for op (plus minus)
1191 (op (lshift:s @0 @1) (lshift:s @2 @1))
1192 (if (INTEGRAL_TYPE_P (type)
1193 && TYPE_OVERFLOW_WRAPS (type)
1194 && !TYPE_SATURATING (type))
1195 (lshift (op @0 @2) @1))))
1197 (for op (bit_and bit_ior bit_xor)
1199 (op (lshift:s @0 @1) (lshift:s @2 @1))
1200 (if (INTEGRAL_TYPE_P (type))
1201 (lshift (op @0 @2) @1)))
1203 (op (rshift:s @0 @1) (rshift:s @2 @1))
1204 (if (INTEGRAL_TYPE_P (type))
1205 (rshift (op @0 @2) @1))))
1207 /* Fold (1 << (C - x)) where C = precision(type) - 1
1208 into ((1 << C) >> x). */
1210 (lshift integer_onep@0 (minus@1 INTEGER_CST@2 @3))
1211 (if (INTEGRAL_TYPE_P (type)
1212 && wi::eq_p (wi::to_wide (@2), TYPE_PRECISION (type) - 1)
1214 (if (TYPE_UNSIGNED (type))
1215 (rshift (lshift @0 @2) @3)
1217 { tree utype = unsigned_type_for (type); }
1218 (convert (rshift (lshift (convert:utype @0) @2) @3))))))
1220 /* Fold ((type)(a<0)) << SIGNBITOFA into ((type)a) & signbit. */
1222 (lshift (convert (lt @0 integer_zerop@1)) INTEGER_CST@2)
1223 (if (TYPE_SIGN (TREE_TYPE (@0)) == SIGNED
1224 && wi::eq_p (wi::to_wide (@2), TYPE_PRECISION (TREE_TYPE (@0)) - 1))
1225 (with { wide_int wone = wi::one (TYPE_PRECISION (type)); }
1226 (bit_and (convert @0)
1227 { wide_int_to_tree (type,
1228 wi::lshift (wone, wi::to_wide (@2))); }))))
1230 /* Fold (-x >> C) into -(x > 0) where C = precision(type) - 1. */
1231 (for cst (INTEGER_CST VECTOR_CST)
1233 (rshift (negate:s @0) cst@1)
1234 (if (!TYPE_UNSIGNED (type)
1235 && TYPE_OVERFLOW_UNDEFINED (type))
1236 (with { tree stype = TREE_TYPE (@1);
1237 tree bt = truth_type_for (type);
1238 tree zeros = build_zero_cst (type);
1239 tree cst = NULL_TREE; }
1241 /* Handle scalar case. */
1242 (if (INTEGRAL_TYPE_P (type)
1243 /* If we apply the rule to the scalar type before vectorization
1244 we will enforce the result of the comparison being a bool
1245 which will require an extra AND on the result that will be
1246 indistinguishable from when the user did actually want 0
1247 or 1 as the result so it can't be removed. */
1248 && canonicalize_math_after_vectorization_p ()
1249 && wi::eq_p (wi::to_wide (@1), TYPE_PRECISION (type) - 1))
1250 (negate (convert (gt @0 { zeros; }))))
1251 /* Handle vector case. */
1252 (if (VECTOR_INTEGER_TYPE_P (type)
1253 /* First check whether the target has the same mode for vector
1254 comparison results as it's operands do. */
1255 && TYPE_MODE (bt) == TYPE_MODE (type)
1256 /* Then check to see if the target is able to expand the comparison
1257 with the given type later on, otherwise we may ICE. */
1258 && expand_vec_cmp_expr_p (type, bt, GT_EXPR)
1259 && (cst = uniform_integer_cst_p (@1)) != NULL
1260 && wi::eq_p (wi::to_wide (cst), element_precision (type) - 1))
1261 (view_convert (gt:bt @0 { zeros; }))))))))
1263 /* Fold (C1/X)*C2 into (C1*C2)/X. */
1265 (mult (rdiv@3 REAL_CST@0 @1) REAL_CST@2)
1266 (if (flag_associative_math
1269 { tree tem = const_binop (MULT_EXPR, type, @0, @2); }
1271 (rdiv { tem; } @1)))))
1273 /* Simplify ~X & X as zero. */
1275 (bit_and (convert? @0) (convert? @1))
1276 (with { bool wascmp; }
1277 (if (types_match (TREE_TYPE (@0), TREE_TYPE (@1))
1278 && bitwise_inverted_equal_p (@0, @1, wascmp))
1279 { wascmp ? constant_boolean_node (false, type) : build_zero_cst (type); })))
1281 /* PR71636: Transform x & ((1U << b) - 1) -> x & ~(~0U << b); */
1283 (bit_and:c @0 (plus:s (lshift:s integer_onep @1) integer_minus_onep))
1284 (if (TYPE_UNSIGNED (type))
1285 (bit_and @0 (bit_not (lshift { build_all_ones_cst (type); } @1)))))
1287 (for bitop (bit_and bit_ior)
1289 /* PR35691: Transform
1290 (x == 0 & y == 0) -> (x | typeof(x)(y)) == 0.
1291 (x != 0 | y != 0) -> (x | typeof(x)(y)) != 0. */
1293 (bitop (cmp @0 integer_zerop@2) (cmp @1 integer_zerop))
1294 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1295 && INTEGRAL_TYPE_P (TREE_TYPE (@1))
1296 && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
1297 (cmp (bit_ior @0 (convert @1)) @2)))
1299 (x == -1 & y == -1) -> (x & typeof(x)(y)) == -1.
1300 (x != -1 | y != -1) -> (x & typeof(x)(y)) != -1. */
1302 (bitop (cmp @0 integer_all_onesp@2) (cmp @1 integer_all_onesp))
1303 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1304 && INTEGRAL_TYPE_P (TREE_TYPE (@1))
1305 && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
1306 (cmp (bit_and @0 (convert @1)) @2))))
1308 /* Fold (A & ~B) - (A & B) into (A ^ B) - B. */
1310 (minus (bit_and:cs @0 (bit_not @1)) (bit_and:cs @0 @1))
1311 (minus (bit_xor @0 @1) @1))
1313 (minus (bit_and:s @0 INTEGER_CST@2) (bit_and:s @0 INTEGER_CST@1))
1314 (if (~wi::to_wide (@2) == wi::to_wide (@1))
1315 (minus (bit_xor @0 @1) @1)))
1317 /* Fold (A & B) - (A & ~B) into B - (A ^ B). */
1319 (minus (bit_and:cs @0 @1) (bit_and:cs @0 (bit_not @1)))
1320 (minus @1 (bit_xor @0 @1)))
1322 /* Simplify (X & ~Y) |^+ (~X & Y) -> X ^ Y. */
1323 (for op (bit_ior bit_xor plus)
1325 (op (bit_and:c @0 @2) (bit_and:c @3 @1))
1326 (with { bool wascmp0, wascmp1; }
1327 (if (bitwise_inverted_equal_p (@2, @1, wascmp0)
1328 && bitwise_inverted_equal_p (@0, @3, wascmp1)
1329 && ((!wascmp0 && !wascmp1)
1330 || element_precision (type) == 1))
1333 /* PR53979: Transform ((a ^ b) | a) -> (a | b) */
1335 (bit_ior:c (bit_xor:c @0 @1) @0)
1338 /* (a & ~b) | (a ^ b) --> a ^ b */
1340 (bit_ior:c (bit_and:c @0 (bit_not @1)) (bit_xor:c@2 @0 @1))
1343 /* (a & ~b) ^ ~a --> ~(a & b) */
1345 (bit_xor:c (bit_and:cs @0 (bit_not @1)) (bit_not @0))
1346 (bit_not (bit_and @0 @1)))
1348 /* (~a & b) ^ a --> (a | b) */
1350 (bit_xor:c (bit_and:cs (bit_not @0) @1) @0)
1353 /* (a | b) & ~(a ^ b) --> a & b */
1355 (bit_and:c (bit_ior @0 @1) (bit_not (bit_xor:c @0 @1)))
1358 /* (a | b) & (a == b) --> a & b (boolean version of the above). */
1360 (bit_and:c (bit_ior @0 @1) (nop_convert? (eq:c @0 @1)))
1361 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1362 && TYPE_PRECISION (TREE_TYPE (@0)) == 1)
1365 /* a | ~(a ^ b) --> a | ~b */
1367 (bit_ior:c @0 (bit_not:s (bit_xor:c @0 @1)))
1368 (bit_ior @0 (bit_not @1)))
1370 /* a | (a == b) --> a | (b^1) (boolean version of the above). */
1372 (bit_ior:c @0 (nop_convert? (eq:c @0 @1)))
1373 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1374 && TYPE_PRECISION (TREE_TYPE (@0)) == 1)
1375 (bit_ior @0 (bit_xor @1 { build_one_cst (type); }))))
1377 /* (a | b) | (a &^ b) --> a | b */
1378 (for op (bit_and bit_xor)
1380 (bit_ior:c (bit_ior@2 @0 @1) (op:c @0 @1))
1383 /* (a & b) | ~(a ^ b) --> ~(a ^ b) */
1385 (bit_ior:c (bit_and:c @0 @1) (bit_not@2 (bit_xor @0 @1)))
1388 /* (a & b) | (a == b) --> a == b */
1390 (bit_ior:c (bit_and:c @0 @1) (nop_convert?@2 (eq @0 @1)))
1391 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1392 && TYPE_PRECISION (TREE_TYPE (@0)) == 1)
1395 /* ~(~a & b) --> a | ~b */
1397 (bit_not (bit_and:cs (bit_not @0) @1))
1398 (bit_ior @0 (bit_not @1)))
1400 /* ~(~a | b) --> a & ~b */
1402 (bit_not (bit_ior:cs (bit_not @0) @1))
1403 (bit_and @0 (bit_not @1)))
1405 /* (a ^ b) & ((b ^ c) ^ a) --> (a ^ b) & ~c */
1407 (bit_and:c (bit_xor:c@3 @0 @1) (bit_xor:cs (bit_xor:cs @1 @2) @0))
1408 (bit_and @3 (bit_not @2)))
1410 /* (a ^ b) | ((b ^ c) ^ a) --> (a ^ b) | c */
1412 (bit_ior:c (bit_xor:c@3 @0 @1) (bit_xor:c (bit_xor:c @1 @2) @0))
1415 /* (~X | C) ^ D -> (X | C) ^ (~D ^ C) if (~D ^ C) can be simplified. */
1417 (bit_xor:c (bit_ior:cs (bit_not:s @0) @1) @2)
1418 (bit_xor (bit_ior @0 @1) (bit_xor! (bit_not! @2) @1)))
1420 /* (~X & C) ^ D -> (X & C) ^ (D ^ C) if (D ^ C) can be simplified. */
1422 (bit_xor:c (bit_and:cs (bit_not:s @0) @1) @2)
1423 (bit_xor (bit_and @0 @1) (bit_xor! @2 @1)))
1425 /* Simplify (~X & Y) to X ^ Y if we know that (X & ~Y) is 0. */
1427 (bit_and (bit_not SSA_NAME@0) INTEGER_CST@1)
1428 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1429 && wi::bit_and_not (get_nonzero_bits (@0), wi::to_wide (@1)) == 0)
1432 /* For constants M and N, if M == (1LL << cst) - 1 && (N & M) == M,
1433 ((A & N) + B) & M -> (A + B) & M
1434 Similarly if (N & M) == 0,
1435 ((A | N) + B) & M -> (A + B) & M
1436 and for - instead of + (or unary - instead of +)
1437 and/or ^ instead of |.
1438 If B is constant and (B & M) == 0, fold into A & M. */
1439 (for op (plus minus)
1440 (for bitop (bit_and bit_ior bit_xor)
1442 (bit_and (op:s (bitop:s@0 @3 INTEGER_CST@4) @1) INTEGER_CST@2)
1445 tree utype = fold_bit_and_mask (TREE_TYPE (@0), @2, op, @0, bitop,
1446 @3, @4, @1, ERROR_MARK, NULL_TREE,
1449 (convert (bit_and (op (convert:utype { pmop[0]; })
1450 (convert:utype { pmop[1]; }))
1451 (convert:utype @2))))))
1453 (bit_and (op:s @0 (bitop:s@1 @3 INTEGER_CST@4)) INTEGER_CST@2)
1456 tree utype = fold_bit_and_mask (TREE_TYPE (@0), @2, op, @0, ERROR_MARK,
1457 NULL_TREE, NULL_TREE, @1, bitop, @3,
1460 (convert (bit_and (op (convert:utype { pmop[0]; })
1461 (convert:utype { pmop[1]; }))
1462 (convert:utype @2)))))))
1464 (bit_and (op:s @0 @1) INTEGER_CST@2)
1467 tree utype = fold_bit_and_mask (TREE_TYPE (@0), @2, op, @0, ERROR_MARK,
1468 NULL_TREE, NULL_TREE, @1, ERROR_MARK,
1469 NULL_TREE, NULL_TREE, pmop); }
1471 (convert (bit_and (op (convert:utype { pmop[0]; })
1472 (convert:utype { pmop[1]; }))
1473 (convert:utype @2)))))))
1474 (for bitop (bit_and bit_ior bit_xor)
1476 (bit_and (negate:s (bitop:s@0 @2 INTEGER_CST@3)) INTEGER_CST@1)
1479 tree utype = fold_bit_and_mask (TREE_TYPE (@0), @1, NEGATE_EXPR, @0,
1480 bitop, @2, @3, NULL_TREE, ERROR_MARK,
1481 NULL_TREE, NULL_TREE, pmop); }
1483 (convert (bit_and (negate (convert:utype { pmop[0]; }))
1484 (convert:utype @1)))))))
1486 /* X % Y is smaller than Y. */
1489 (cmp:c (trunc_mod @0 @1) @1)
1490 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
1491 { constant_boolean_node (cmp == LT_EXPR, type); })))
1495 (bit_ior @0 integer_all_onesp@1)
1500 (bit_ior @0 integer_zerop)
1505 (bit_and @0 integer_zerop@1)
1510 (for op (bit_ior bit_xor)
1512 (op (convert? @0) (convert? @1))
1513 (with { bool wascmp; }
1514 (if (types_match (TREE_TYPE (@0), TREE_TYPE (@1))
1515 && bitwise_inverted_equal_p (@0, @1, wascmp))
1518 ? constant_boolean_node (true, type)
1519 : build_all_ones_cst (TREE_TYPE (@0)); })))))
1524 { build_zero_cst (type); })
1526 /* Canonicalize X ^ ~0 to ~X. */
1528 (bit_xor @0 integer_all_onesp@1)
1533 (bit_and @0 integer_all_onesp)
1536 /* x & x -> x, x | x -> x */
1537 (for bitop (bit_and bit_ior)
1542 /* x & C -> x if we know that x & ~C == 0. */
1545 (bit_and SSA_NAME@0 INTEGER_CST@1)
1546 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1547 && wi::bit_and_not (get_nonzero_bits (@0), wi::to_wide (@1)) == 0)
1549 /* x | C -> C if we know that x & ~C == 0. */
1551 (bit_ior SSA_NAME@0 INTEGER_CST@1)
1552 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1553 && wi::bit_and_not (get_nonzero_bits (@0), wi::to_wide (@1)) == 0)
1557 /* ~(~X - Y) -> X + Y and ~(~X + Y) -> X - Y. */
1559 (bit_not (minus (bit_not @0) @1))
1562 (bit_not (plus:c (bit_not @0) @1))
1564 /* (~X - ~Y) -> Y - X. */
1566 (minus (bit_not @0) (bit_not @1))
1567 (if (!TYPE_OVERFLOW_SANITIZED (type))
1568 (with { tree utype = unsigned_type_for (type); }
1569 (convert (minus (convert:utype @1) (convert:utype @0))))))
1571 /* ~(X - Y) -> ~X + Y. */
1573 (bit_not (minus:s @0 @1))
1574 (plus (bit_not @0) @1))
1576 (bit_not (plus:s @0 INTEGER_CST@1))
1577 (if ((INTEGRAL_TYPE_P (type)
1578 && TYPE_UNSIGNED (type))
1579 || (!TYPE_OVERFLOW_SANITIZED (type)
1580 && may_negate_without_overflow_p (@1)))
1581 (plus (bit_not @0) { const_unop (NEGATE_EXPR, type, @1); })))
1584 /* ~X + Y -> (Y - X) - 1. */
1586 (plus:c (bit_not @0) @1)
1587 (if (ANY_INTEGRAL_TYPE_P (type)
1588 && TYPE_OVERFLOW_WRAPS (type)
1589 /* -1 - X is folded to ~X, so we'd recurse endlessly. */
1590 && !integer_all_onesp (@1))
1591 (plus (minus @1 @0) { build_minus_one_cst (type); })
1592 (if (INTEGRAL_TYPE_P (type)
1593 && TREE_CODE (@1) == INTEGER_CST
1594 && wi::to_wide (@1) != wi::min_value (TYPE_PRECISION (type),
1596 (minus (plus @1 { build_minus_one_cst (type); }) @0))))
1599 /* ~(X >> Y) -> ~X >> Y if ~X can be simplified. */
1601 (bit_not (rshift:s @0 @1))
1602 (if (!TYPE_UNSIGNED (TREE_TYPE (@0)))
1603 (rshift (bit_not! @0) @1)
1604 /* For logical right shifts, this is possible only if @0 doesn't
1605 have MSB set and the logical right shift is changed into
1606 arithmetic shift. */
1607 (if (INTEGRAL_TYPE_P (type)
1608 && !wi::neg_p (tree_nonzero_bits (@0)))
1609 (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
1610 (convert (rshift (bit_not! (convert:stype @0)) @1))))))
1612 /* x + (x & 1) -> (x + 1) & ~1 */
1614 (plus:c @0 (bit_and:s @0 integer_onep@1))
1615 (bit_and (plus @0 @1) (bit_not @1)))
1617 /* x & ~(x & y) -> x & ~y */
1618 /* x | ~(x | y) -> x | ~y */
1619 (for bitop (bit_and bit_ior)
1621 (bitop:c @0 (bit_not (bitop:cs @0 @1)))
1622 (bitop @0 (bit_not @1))))
1624 /* (~x & y) | ~(x | y) -> ~x */
1626 (bit_ior:c (bit_and:c (bit_not@2 @0) @1) (bit_not (bit_ior:c @0 @1)))
1629 /* (x | y) ^ (x | ~y) -> ~x */
1631 (bit_xor:c (bit_ior:c @0 @1) (bit_ior:c @0 (bit_not @1)))
1634 /* (x & y) | ~(x | y) -> ~(x ^ y) */
1636 (bit_ior:c (bit_and:s @0 @1) (bit_not:s (bit_ior:s @0 @1)))
1637 (bit_not (bit_xor @0 @1)))
1639 /* (~x | y) ^ (x ^ y) -> x | ~y */
1641 (bit_xor:c (bit_ior:cs (bit_not @0) @1) (bit_xor:s @0 @1))
1642 (bit_ior @0 (bit_not @1)))
1644 /* (x ^ y) | ~(x | y) -> ~(x & y) */
1646 (bit_ior:c (bit_xor:s @0 @1) (bit_not:s (bit_ior:s @0 @1)))
1647 (bit_not (bit_and @0 @1)))
1649 /* (x & y) ^ (x | y) -> x ^ y */
1651 (bit_xor:c (bit_and @0 @1) (bit_ior @0 @1))
1654 /* (x ^ y) ^ (x | y) -> x & y */
1656 (bit_xor:c (bit_xor @0 @1) (bit_ior @0 @1))
1659 /* (x & y) + (x ^ y) -> x | y */
1660 /* (x & y) | (x ^ y) -> x | y */
1661 /* (x & y) ^ (x ^ y) -> x | y */
1662 (for op (plus bit_ior bit_xor)
1664 (op:c (bit_and @0 @1) (bit_xor @0 @1))
1667 /* (x & y) + (x | y) -> x + y */
1669 (plus:c (bit_and @0 @1) (bit_ior @0 @1))
1672 /* (x + y) - (x | y) -> x & y */
1674 (minus (plus @0 @1) (bit_ior @0 @1))
1675 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1676 && !TYPE_SATURATING (type))
1679 /* (x + y) - (x & y) -> x | y */
1681 (minus (plus @0 @1) (bit_and @0 @1))
1682 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1683 && !TYPE_SATURATING (type))
1686 /* (x | y) - y -> (x & ~y) */
1688 (minus (bit_ior:cs @0 @1) @1)
1689 (bit_and @0 (bit_not @1)))
1691 /* (x | y) - (x ^ y) -> x & y */
1693 (minus (bit_ior @0 @1) (bit_xor @0 @1))
1696 /* (x | y) - (x & y) -> x ^ y */
1698 (minus (bit_ior @0 @1) (bit_and @0 @1))
1701 /* (x | y) & ~(x & y) -> x ^ y */
1703 (bit_and:c (bit_ior @0 @1) (bit_not (bit_and @0 @1)))
1706 /* (x | y) & (~x ^ y) -> x & y */
1708 (bit_and:c (bit_ior:c @0 @1) (bit_xor:c @1 @2))
1709 (with { bool wascmp; }
1710 (if (bitwise_inverted_equal_p (@0, @2, wascmp)
1711 && (!wascmp || element_precision (type) == 1))
1714 /* (~x | y) & (x | ~y) -> ~(x ^ y) */
1716 (bit_and (bit_ior:cs (bit_not @0) @1) (bit_ior:cs @0 (bit_not @1)))
1717 (bit_not (bit_xor @0 @1)))
1719 /* (~x | y) ^ (x | ~y) -> x ^ y */
1721 (bit_xor (bit_ior:c (bit_not @0) @1) (bit_ior:c @0 (bit_not @1)))
1724 /* ((x & y) - (x | y)) - 1 -> ~(x ^ y) */
1726 (plus (nop_convert1? (minus@2 (nop_convert2? (bit_and:c @0 @1))
1727 (nop_convert2? (bit_ior @0 @1))))
1729 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1730 && !TYPE_SATURATING (type) && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2))
1731 && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@2))
1732 && !TYPE_SATURATING (TREE_TYPE (@2)))
1733 (bit_not (convert (bit_xor @0 @1)))))
1735 (minus (nop_convert1? (plus@2 (nop_convert2? (bit_and:c @0 @1))
1737 (nop_convert3? (bit_ior @0 @1)))
1738 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1739 && !TYPE_SATURATING (type) && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2))
1740 && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@2))
1741 && !TYPE_SATURATING (TREE_TYPE (@2)))
1742 (bit_not (convert (bit_xor @0 @1)))))
1744 (minus (nop_convert1? (bit_and @0 @1))
1745 (nop_convert2? (plus@2 (nop_convert3? (bit_ior:c @0 @1))
1747 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1748 && !TYPE_SATURATING (type) && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2))
1749 && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@2))
1750 && !TYPE_SATURATING (TREE_TYPE (@2)))
1751 (bit_not (convert (bit_xor @0 @1)))))
1753 /* ~x & ~y -> ~(x | y)
1754 ~x | ~y -> ~(x & y) */
1755 (for op (bit_and bit_ior)
1756 rop (bit_ior bit_and)
1758 (op (convert1? (bit_not @0)) (convert2? (bit_not @1)))
1759 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1760 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
1761 (bit_not (rop (convert @0) (convert @1))))))
1763 /* If we are XORing or adding two BIT_AND_EXPR's, both of which are and'ing
1764 with a constant, and the two constants have no bits in common,
1765 we should treat this as a BIT_IOR_EXPR since this may produce more
1767 (for op (bit_xor plus)
1769 (op (convert1? (bit_and@4 @0 INTEGER_CST@1))
1770 (convert2? (bit_and@5 @2 INTEGER_CST@3)))
1771 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
1772 && tree_nop_conversion_p (type, TREE_TYPE (@2))
1773 && (wi::to_wide (@1) & wi::to_wide (@3)) == 0)
1774 (bit_ior (convert @4) (convert @5)))))
1776 /* (X | Y) ^ X -> Y & ~ X*/
1778 (bit_xor:c (convert1? (bit_ior:c @@0 @1)) (convert2? @0))
1779 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1780 (convert (bit_and @1 (bit_not @0)))))
1782 /* (~X | Y) ^ X -> ~(X & Y). */
1784 (bit_xor:c (nop_convert1? (bit_ior:c (nop_convert2? (bit_not @0)) @1)) @2)
1785 (if (bitwise_equal_p (@0, @2))
1786 (convert (bit_not (bit_and @0 (convert @1))))))
1788 /* Convert ~X ^ ~Y to X ^ Y. */
1790 (bit_xor (convert1? (bit_not @0)) (convert2? (bit_not @1)))
1791 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1792 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
1793 (bit_xor (convert @0) (convert @1))))
1795 /* Convert ~X ^ C to X ^ ~C. */
1797 (bit_xor (convert? (bit_not @0)) INTEGER_CST@1)
1798 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1799 (bit_xor (convert @0) (bit_not @1))))
1801 /* Fold (X & Y) ^ Y and (X ^ Y) & Y as ~X & Y. */
1802 (for opo (bit_and bit_xor)
1803 opi (bit_xor bit_and)
1805 (opo:c (opi:cs @0 @1) @1)
1806 (bit_and (bit_not @0) @1)))
1808 /* Given a bit-wise operation CODE applied to ARG0 and ARG1, see if both
1809 operands are another bit-wise operation with a common input. If so,
1810 distribute the bit operations to save an operation and possibly two if
1811 constants are involved. For example, convert
1812 (A | B) & (A | C) into A | (B & C)
1813 Further simplification will occur if B and C are constants. */
1814 (for op (bit_and bit_ior bit_xor)
1815 rop (bit_ior bit_and bit_and)
1817 (op (convert? (rop:c @@0 @1)) (convert? (rop:c @0 @2)))
1818 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1819 && tree_nop_conversion_p (type, TREE_TYPE (@2)))
1820 (rop (convert @0) (op (convert @1) (convert @2))))))
1822 /* Some simple reassociation for bit operations, also handled in reassoc. */
1823 /* (X & Y) & Y -> X & Y
1824 (X | Y) | Y -> X | Y */
1825 (for op (bit_and bit_ior)
1827 (op:c (convert1?@2 (op:c @0 @@1)) (convert2? @1))
1829 /* (X ^ Y) ^ Y -> X */
1831 (bit_xor:c (convert1? (bit_xor:c @0 @@1)) (convert2? @1))
1833 /* (X & Y) & (X & Z) -> (X & Y) & Z
1834 (X | Y) | (X | Z) -> (X | Y) | Z */
1835 (for op (bit_and bit_ior)
1837 (op (convert1?@3 (op:c@4 @0 @1)) (convert2?@5 (op:c@6 @0 @2)))
1838 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1839 && tree_nop_conversion_p (type, TREE_TYPE (@2)))
1840 (if (single_use (@5) && single_use (@6))
1841 (op @3 (convert @2))
1842 (if (single_use (@3) && single_use (@4))
1843 (op (convert @1) @5))))))
1844 /* (X ^ Y) ^ (X ^ Z) -> Y ^ Z */
1846 (bit_xor (convert1? (bit_xor:c @0 @1)) (convert2? (bit_xor:c @0 @2)))
1847 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1848 && tree_nop_conversion_p (type, TREE_TYPE (@2)))
1849 (bit_xor (convert @1) (convert @2))))
1851 /* Convert abs (abs (X)) into abs (X).
1852 also absu (absu (X)) into absu (X). */
1858 (absu (convert@2 (absu@1 @0)))
1859 (if (tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@1)))
1862 /* Convert abs[u] (-X) -> abs[u] (X). */
1871 /* Convert abs[u] (X) where X is nonnegative -> (X). */
1873 (abs tree_expr_nonnegative_p@0)
1877 (absu tree_expr_nonnegative_p@0)
1880 /* Simplify (-(X < 0) | 1) * X into abs (X) or absu(X). */
1882 (mult:c (nop_convert1?
1883 (bit_ior (nop_convert2? (negate (convert? (lt @0 integer_zerop))))
1886 (if (INTEGRAL_TYPE_P (type)
1887 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
1888 && !TYPE_UNSIGNED (TREE_TYPE (@0)))
1889 (if (TYPE_UNSIGNED (type))
1896 /* A few cases of fold-const.cc negate_expr_p predicate. */
1897 (match negate_expr_p
1899 (if ((INTEGRAL_TYPE_P (type)
1900 && TYPE_UNSIGNED (type))
1901 || (!TYPE_OVERFLOW_SANITIZED (type)
1902 && may_negate_without_overflow_p (t)))))
1903 (match negate_expr_p
1905 (match negate_expr_p
1907 (if (!TYPE_OVERFLOW_SANITIZED (type))))
1908 (match negate_expr_p
1910 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (t)))))
1911 /* VECTOR_CST handling of non-wrapping types would recurse in unsupported
1913 (match negate_expr_p
1915 (if (FLOAT_TYPE_P (TREE_TYPE (type)) || TYPE_OVERFLOW_WRAPS (type))))
1916 (match negate_expr_p
1918 (if ((ANY_INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_WRAPS (type))
1919 || (FLOAT_TYPE_P (type)
1920 && !HONOR_SIGN_DEPENDENT_ROUNDING (type)
1921 && !HONOR_SIGNED_ZEROS (type)))))
1923 /* (-A) * (-B) -> A * B */
1925 (mult:c (convert1? (negate @0)) (convert2? negate_expr_p@1))
1926 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
1927 && tree_nop_conversion_p (type, TREE_TYPE (@1)))
1928 (mult (convert @0) (convert (negate @1)))))
1930 /* -(A + B) -> (-B) - A. */
1932 (negate (plus:c @0 negate_expr_p@1))
1933 (if (!HONOR_SIGN_DEPENDENT_ROUNDING (type)
1934 && !HONOR_SIGNED_ZEROS (type))
1935 (minus (negate @1) @0)))
1937 /* -(A - B) -> B - A. */
1939 (negate (minus @0 @1))
1940 (if ((ANY_INTEGRAL_TYPE_P (type) && !TYPE_OVERFLOW_SANITIZED (type))
1941 || (FLOAT_TYPE_P (type)
1942 && !HONOR_SIGN_DEPENDENT_ROUNDING (type)
1943 && !HONOR_SIGNED_ZEROS (type)))
1946 (negate (pointer_diff @0 @1))
1947 (if (TYPE_OVERFLOW_UNDEFINED (type))
1948 (pointer_diff @1 @0)))
1950 /* A - B -> A + (-B) if B is easily negatable. */
1952 (minus @0 negate_expr_p@1)
1953 (if (!FIXED_POINT_TYPE_P (type))
1954 (plus @0 (negate @1))))
1956 /* 1 - a is a ^ 1 if a had a bool range. */
1957 /* This is only enabled for gimple as sometimes
1958 cfun is not set for the function which contains
1959 the SSA_NAME (e.g. while IPA passes are happening,
1960 fold might be called). */
1962 (minus integer_onep@0 SSA_NAME@1)
1963 (if (INTEGRAL_TYPE_P (type)
1964 && ssa_name_has_boolean_range (@1))
1967 /* Other simplifications of negation (c.f. fold_negate_expr_1). */
1969 (negate (mult:c@0 @1 negate_expr_p@2))
1970 (if (! TYPE_UNSIGNED (type)
1971 && ! HONOR_SIGN_DEPENDENT_ROUNDING (type)
1973 (mult @1 (negate @2))))
1976 (negate (rdiv@0 @1 negate_expr_p@2))
1977 (if (! HONOR_SIGN_DEPENDENT_ROUNDING (type)
1979 (rdiv @1 (negate @2))))
1982 (negate (rdiv@0 negate_expr_p@1 @2))
1983 (if (! HONOR_SIGN_DEPENDENT_ROUNDING (type)
1985 (rdiv (negate @1) @2)))
1987 /* Fold -((int)x >> (prec - 1)) into (unsigned)x >> (prec - 1). */
1989 (negate (convert? (rshift @0 INTEGER_CST@1)))
1990 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
1991 && wi::to_wide (@1) == element_precision (type) - 1)
1992 (with { tree stype = TREE_TYPE (@0);
1993 tree ntype = TYPE_UNSIGNED (stype) ? signed_type_for (stype)
1994 : unsigned_type_for (stype); }
1995 (if (VECTOR_TYPE_P (type))
1996 (view_convert (rshift (view_convert:ntype @0) @1))
1997 (convert (rshift (convert:ntype @0) @1))))))
1999 /* Try to fold (type) X op CST -> (type) (X op ((type-x) CST))
2001 For bitwise binary operations apply operand conversions to the
2002 binary operation result instead of to the operands. This allows
2003 to combine successive conversions and bitwise binary operations.
2004 We combine the above two cases by using a conditional convert. */
2005 (for bitop (bit_and bit_ior bit_xor)
2007 (bitop (convert@2 @0) (convert?@3 @1))
2008 (if (((TREE_CODE (@1) == INTEGER_CST
2009 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
2010 && (int_fits_type_p (@1, TREE_TYPE (@0))
2011 || tree_nop_conversion_p (TREE_TYPE (@0), type)))
2012 || types_match (@0, @1))
2013 && !POINTER_TYPE_P (TREE_TYPE (@0))
2014 && !VECTOR_TYPE_P (TREE_TYPE (@0))
2015 && TREE_CODE (TREE_TYPE (@0)) != OFFSET_TYPE
2016 /* ??? This transform conflicts with fold-const.cc doing
2017 Convert (T)(x & c) into (T)x & (T)c, if c is an integer
2018 constants (if x has signed type, the sign bit cannot be set
2019 in c). This folds extension into the BIT_AND_EXPR.
2020 Restrict it to GIMPLE to avoid endless recursions. */
2021 && (bitop != BIT_AND_EXPR || GIMPLE)
2022 && (/* That's a good idea if the conversion widens the operand, thus
2023 after hoisting the conversion the operation will be narrower.
2024 It is also a good if the conversion is a nop as moves the
2025 conversion to one side; allowing for combining of the conversions. */
2026 TYPE_PRECISION (TREE_TYPE (@0)) < TYPE_PRECISION (type)
2027 /* The conversion check for being a nop can only be done at the gimple
2028 level as fold_binary has some re-association code which can conflict
2029 with this if there is a "constant" which is not a full INTEGER_CST. */
2030 || (GIMPLE && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (type))
2031 /* It's also a good idea if the conversion is to a non-integer
2033 || GET_MODE_CLASS (TYPE_MODE (type)) != MODE_INT
2034 /* Or if the precision of TO is not the same as the precision
2036 || !type_has_mode_precision_p (type)
2037 /* In GIMPLE, getting rid of 2 conversions for one new results
2040 && TREE_CODE (@1) != INTEGER_CST
2041 && tree_nop_conversion_p (type, TREE_TYPE (@0))
2043 && single_use (@3))))
2044 (convert (bitop @0 (convert @1)))))
2045 /* In GIMPLE, getting rid of 2 conversions for one new results
2048 (convert (bitop:cs@2 (nop_convert:s @0) @1))
2050 && TREE_CODE (@1) != INTEGER_CST
2051 && tree_nop_conversion_p (type, TREE_TYPE (@2))
2052 && types_match (type, @0)
2053 && !POINTER_TYPE_P (TREE_TYPE (@0))
2054 && TREE_CODE (TREE_TYPE (@0)) != OFFSET_TYPE)
2055 (bitop @0 (convert @1)))))
2057 (for bitop (bit_and bit_ior)
2058 rbitop (bit_ior bit_and)
2059 /* (x | y) & x -> x */
2060 /* (x & y) | x -> x */
2062 (bitop:c (rbitop:c @0 @1) @0)
2064 /* (~x | y) & x -> x & y */
2065 /* (~x & y) | x -> x | y */
2067 (bitop:c (rbitop:c @2 @1) @0)
2068 (with { bool wascmp; }
2069 (if (bitwise_inverted_equal_p (@0, @2, wascmp)
2070 && (!wascmp || element_precision (type) == 1))
2072 /* (x | y) & (x & z) -> (x & z) */
2073 /* (x & y) | (x | z) -> (x | z) */
2075 (bitop:c (rbitop:c @0 @1) (bitop:c@3 @0 @2))
2077 /* (x | c) & ~(y | c) -> x & ~(y | c) */
2078 /* (x & c) | ~(y & c) -> x | ~(y & c) */
2080 (bitop:c (rbitop:c @0 @1) (bit_not@3 (rbitop:c @1 @2)))
2082 /* x & ~(y | x) -> 0 */
2083 /* x | ~(y & x) -> -1 */
2085 (bitop:c @0 (bit_not (rbitop:c @0 @1)))
2086 (if (bitop == BIT_AND_EXPR)
2087 { build_zero_cst (type); }
2088 { build_minus_one_cst (type); })))
2090 /* ((x | y) & z) | x -> (z & y) | x
2091 ((x ^ y) & z) | x -> (z & y) | x */
2092 (for op (bit_ior bit_xor)
2094 (bit_ior:c (nop_convert1?:s
2095 (bit_and:cs (nop_convert2?:s (op:cs @0 @1)) @2)) @3)
2096 (if (bitwise_equal_p (@0, @3))
2097 (convert (bit_ior (bit_and @1 (convert @2)) (convert @0))))))
2099 /* (x | CST1) & CST2 -> (x & CST2) | (CST1 & CST2) */
2101 (bit_and (bit_ior @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
2102 (bit_ior (bit_and @0 @2) (bit_and! @1 @2)))
2104 /* Combine successive equal operations with constants. */
2105 (for bitop (bit_and bit_ior bit_xor)
2107 (bitop (bitop @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
2108 (if (!CONSTANT_CLASS_P (@0))
2109 /* This is the canonical form regardless of whether (bitop @1 @2) can be
2110 folded to a constant. */
2111 (bitop @0 (bitop! @1 @2))
2112 /* In this case we have three constants and (bitop @0 @1) doesn't fold
2113 to a constant. This can happen if @0 or @1 is a POLY_INT_CST and if
2114 the values involved are such that the operation can't be decided at
2115 compile time. Try folding one of @0 or @1 with @2 to see whether
2116 that combination can be decided at compile time.
2118 Keep the existing form if both folds fail, to avoid endless
2120 (with { tree cst1 = const_binop (bitop, type, @0, @2); }
2122 (bitop @1 { cst1; })
2123 (with { tree cst2 = const_binop (bitop, type, @1, @2); }
2125 (bitop @0 { cst2; }))))))))
2127 /* Try simple folding for X op !X, and X op X with the help
2128 of the truth_valued_p and logical_inverted_value predicates. */
2129 (match truth_valued_p
2131 (if (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1)))
2132 (for op (tcc_comparison truth_and truth_andif truth_or truth_orif truth_xor)
2133 (match truth_valued_p
2135 (match truth_valued_p
2138 (match (logical_inverted_value @0)
2140 (match (logical_inverted_value @0)
2141 (bit_not truth_valued_p@0))
2142 (match (logical_inverted_value @0)
2143 (eq @0 integer_zerop))
2144 (match (logical_inverted_value @0)
2145 (ne truth_valued_p@0 integer_truep))
2146 (match (logical_inverted_value @0)
2147 (bit_xor truth_valued_p@0 integer_truep))
2151 (bit_and:c @0 (logical_inverted_value @0))
2152 { build_zero_cst (type); })
2153 /* X | !X and X ^ !X -> 1, , if X is truth-valued. */
2154 (for op (bit_ior bit_xor)
2156 (op:c truth_valued_p@0 (logical_inverted_value @0))
2157 { constant_boolean_node (true, type); }))
2158 /* X ==/!= !X is false/true. */
2161 (op:c truth_valued_p@0 (logical_inverted_value @0))
2162 { constant_boolean_node (op == NE_EXPR ? true : false, type); }))
2166 (bit_not (bit_not @0))
2169 /* zero_one_valued_p will match when a value is known to be either
2170 0 or 1 including constants 0 or 1.
2171 Signed 1-bits includes -1 so they cannot match here. */
2172 (match zero_one_valued_p
2174 (if (INTEGRAL_TYPE_P (type)
2175 && (TYPE_UNSIGNED (type)
2176 || TYPE_PRECISION (type) > 1)
2177 && wi::leu_p (tree_nonzero_bits (@0), 1))))
2178 (match zero_one_valued_p
2180 (if (INTEGRAL_TYPE_P (type)
2181 && (TYPE_UNSIGNED (type)
2182 || TYPE_PRECISION (type) > 1))))
2184 /* Transform { 0 or 1 } * { 0 or 1 } into { 0 or 1 } & { 0 or 1 }. */
2186 (mult zero_one_valued_p@0 zero_one_valued_p@1)
2187 (if (INTEGRAL_TYPE_P (type))
2190 (for cmp (tcc_comparison)
2191 icmp (inverted_tcc_comparison)
2192 /* Fold (((a < b) & c) | ((a >= b) & d)) into (a < b ? c : d) & 1. */
2195 (bit_and:c (convert? (cmp@0 @01 @02)) @3)
2196 (bit_and:c (convert? (icmp@4 @01 @02)) @5))
2197 (if (INTEGRAL_TYPE_P (type)
2198 && invert_tree_comparison (cmp, HONOR_NANS (@01)) == icmp
2199 /* The scalar version has to be canonicalized after vectorization
2200 because it makes unconditional loads conditional ones, which
2201 means we lose vectorization because the loads may trap. */
2202 && canonicalize_math_after_vectorization_p ())
2203 (bit_and (cond @0 @3 @5) { build_one_cst (type); })))
2205 /* Fold ((-(a < b) & c) | (-(a >= b) & d)) into a < b ? c : d. This is
2206 canonicalized further and we recognize the conditional form:
2207 (a < b ? c : 0) | (a >= b ? d : 0) into a < b ? c : d. */
2210 (cond (cmp@0 @01 @02) @3 zerop)
2211 (cond (icmp@4 @01 @02) @5 zerop))
2212 (if (INTEGRAL_TYPE_P (type)
2213 && invert_tree_comparison (cmp, HONOR_NANS (@01)) == icmp
2214 /* The scalar version has to be canonicalized after vectorization
2215 because it makes unconditional loads conditional ones, which
2216 means we lose vectorization because the loads may trap. */
2217 && canonicalize_math_after_vectorization_p ())
2220 /* Vector Fold (((a < b) & c) | ((a >= b) & d)) into a < b ? c : d.
2221 and ((~(a < b) & c) | (~(a >= b) & d)) into a < b ? c : d. */
2224 (bit_and:c (vec_cond:s (cmp@0 @6 @7) @4 @5) @2)
2225 (bit_and:c (vec_cond:s (icmp@1 @6 @7) @4 @5) @3))
2226 (if (integer_zerop (@5)
2227 && invert_tree_comparison (cmp, HONOR_NANS (@6)) == icmp)
2229 (if (integer_onep (@4))
2230 (bit_and (vec_cond @0 @2 @3) @4))
2231 (if (integer_minus_onep (@4))
2232 (vec_cond @0 @2 @3)))
2233 (if (integer_zerop (@4)
2234 && invert_tree_comparison (cmp, HONOR_NANS (@6)) == icmp)
2236 (if (integer_onep (@5))
2237 (bit_and (vec_cond @0 @3 @2) @5))
2238 (if (integer_minus_onep (@5))
2239 (vec_cond @0 @3 @2))))))
2241 /* Scalar Vectorized Fold ((-(a < b) & c) | (-(a >= b) & d))
2242 into a < b ? d : c. */
2245 (vec_cond:s (cmp@0 @4 @5) @2 integer_zerop)
2246 (vec_cond:s (icmp@1 @4 @5) @3 integer_zerop))
2247 (if (invert_tree_comparison (cmp, HONOR_NANS (@4)) == icmp)
2248 (vec_cond @0 @2 @3))))
2250 /* Transform X & -Y into X * Y when Y is { 0 or 1 }. */
2252 (bit_and:c (convert? (negate zero_one_valued_p@0)) @1)
2253 (if (INTEGRAL_TYPE_P (type)
2254 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
2255 && TREE_CODE (TREE_TYPE (@0)) != BOOLEAN_TYPE
2256 /* Sign extending of the neg or a truncation of the neg
2258 && (!TYPE_UNSIGNED (TREE_TYPE (@0))
2259 || TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))))
2260 (mult (convert @0) @1)))
2262 /* Narrow integer multiplication by a zero_one_valued_p operand.
2263 Multiplication by [0,1] is guaranteed not to overflow. */
2265 (convert (mult@0 zero_one_valued_p@1 INTEGER_CST@2))
2266 (if (INTEGRAL_TYPE_P (type)
2267 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
2268 && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (@0)))
2269 (mult (convert @1) (convert @2))))
2271 /* (X << C) != 0 can be simplified to X, when C is zero_one_valued_p.
2272 Check that the shift is well-defined (C is less than TYPE_PRECISION)
2273 as some targets (such as x86's SSE) may return zero for larger C. */
2275 (ne (lshift zero_one_valued_p@0 INTEGER_CST@1) integer_zerop@2)
2276 (if (tree_fits_shwi_p (@1)
2277 && tree_to_shwi (@1) > 0
2278 && tree_to_shwi (@1) < TYPE_PRECISION (TREE_TYPE (@0)))
2281 /* (X << C) == 0 can be simplified to X == 0, when C is zero_one_valued_p.
2282 Check that the shift is well-defined (C is less than TYPE_PRECISION)
2283 as some targets (such as x86's SSE) may return zero for larger C. */
2285 (eq (lshift zero_one_valued_p@0 INTEGER_CST@1) integer_zerop@2)
2286 (if (tree_fits_shwi_p (@1)
2287 && tree_to_shwi (@1) > 0
2288 && tree_to_shwi (@1) < TYPE_PRECISION (TREE_TYPE (@0)))
2291 /* Convert ~ (-A) to A - 1. */
2293 (bit_not (convert? (negate @0)))
2294 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
2295 || !TYPE_UNSIGNED (TREE_TYPE (@0)))
2296 (convert (minus @0 { build_each_one_cst (TREE_TYPE (@0)); }))))
2298 /* Convert - (~A) to A + 1. */
2300 (negate (nop_convert? (bit_not @0)))
2301 (plus (view_convert @0) { build_each_one_cst (type); }))
2303 /* (a & b) ^ (a == b) -> !(a | b) */
2304 /* (a & b) == (a ^ b) -> !(a | b) */
2305 (for first_op (bit_xor eq)
2306 second_op (eq bit_xor)
2308 (first_op:c (bit_and:c truth_valued_p@0 truth_valued_p@1) (second_op:c @0 @1))
2309 (bit_not (bit_ior @0 @1))))
2311 /* Convert ~ (A - 1) or ~ (A + -1) to -A. */
2313 (bit_not (convert? (minus @0 integer_each_onep)))
2314 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
2315 || !TYPE_UNSIGNED (TREE_TYPE (@0)))
2316 (convert (negate @0))))
2318 (bit_not (convert? (plus @0 integer_all_onesp)))
2319 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
2320 || !TYPE_UNSIGNED (TREE_TYPE (@0)))
2321 (convert (negate @0))))
2323 /* Part of convert ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify. */
2325 (bit_not (convert? (bit_xor @0 INTEGER_CST@1)))
2326 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2327 (convert (bit_xor @0 (bit_not @1)))))
2329 (bit_not (convert? (bit_xor:c (bit_not @0) @1)))
2330 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2331 (convert (bit_xor @0 @1))))
2333 /* Otherwise prefer ~(X ^ Y) to ~X ^ Y as more canonical. */
2335 (bit_xor:c (nop_convert?:s (bit_not:s @0)) @1)
2336 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2337 (bit_not (bit_xor (view_convert @0) @1))))
2339 /* ~(a ^ b) is a == b for truth valued a and b. */
2341 (bit_not (bit_xor:s truth_valued_p@0 truth_valued_p@1))
2342 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2343 && TYPE_PRECISION (TREE_TYPE (@0)) == 1)
2344 (convert (eq @0 @1))))
2346 /* (~a) == b is a ^ b for truth valued a and b. */
2348 (eq:c (bit_not:s truth_valued_p@0) truth_valued_p@1)
2349 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2350 && TYPE_PRECISION (TREE_TYPE (@0)) == 1)
2351 (convert (bit_xor @0 @1))))
2353 /* (x & ~m) | (y & m) -> ((x ^ y) & m) ^ x */
2355 (bit_ior:c (bit_and:cs @0 (bit_not @2)) (bit_and:cs @1 @2))
2356 (bit_xor (bit_and (bit_xor @0 @1) @2) @0))
2358 /* Fold A - (A & B) into ~B & A. */
2360 (minus (convert1? @0) (convert2?:s (bit_and:cs @@0 @1)))
2361 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
2362 && tree_nop_conversion_p (type, TREE_TYPE (@1)))
2363 (convert (bit_and (bit_not @1) @0))))
2365 /* (m1 CMP m2) * d -> (m1 CMP m2) ? d : 0 */
2366 (if (!canonicalize_math_p ())
2367 (for cmp (tcc_comparison)
2369 (mult:c (convert (cmp@0 @1 @2)) @3)
2370 (if (INTEGRAL_TYPE_P (type)
2371 && INTEGRAL_TYPE_P (TREE_TYPE (@0)))
2372 (cond @0 @3 { build_zero_cst (type); })))
2373 /* (-(m1 CMP m2)) & d -> (m1 CMP m2) ? d : 0 */
2375 (bit_and:c (negate (convert (cmp@0 @1 @2))) @3)
2376 (if (INTEGRAL_TYPE_P (type)
2377 && INTEGRAL_TYPE_P (TREE_TYPE (@0)))
2378 (cond @0 @3 { build_zero_cst (type); })))
2382 /* For integral types with undefined overflow and C != 0 fold
2383 x * C EQ/NE y * C into x EQ/NE y. */
2386 (cmp (mult:c @0 @1) (mult:c @2 @1))
2387 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2388 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2389 && tree_expr_nonzero_p (@1))
2392 /* For integral types with wrapping overflow and C odd fold
2393 x * C EQ/NE y * C into x EQ/NE y. */
2396 (cmp (mult @0 INTEGER_CST@1) (mult @2 @1))
2397 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2398 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))
2399 && (TREE_INT_CST_LOW (@1) & 1) != 0)
2402 /* For integral types with undefined overflow and C != 0 fold
2403 x * C RELOP y * C into:
2405 x RELOP y for nonnegative C
2406 y RELOP x for negative C */
2407 (for cmp (lt gt le ge)
2409 (cmp (mult:c @0 @1) (mult:c @2 @1))
2410 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2411 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
2412 (if (tree_expr_nonnegative_p (@1) && tree_expr_nonzero_p (@1))
2414 (if (TREE_CODE (@1) == INTEGER_CST
2415 && wi::neg_p (wi::to_wide (@1), TYPE_SIGN (TREE_TYPE (@1))))
2418 /* (X - 1U) <= INT_MAX-1U into (int) X > 0. */
2422 (cmp (plus @0 integer_minus_onep@1) INTEGER_CST@2)
2423 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2424 && TYPE_UNSIGNED (TREE_TYPE (@0))
2425 && TYPE_PRECISION (TREE_TYPE (@0)) > 1
2426 && (wi::to_wide (@2)
2427 == wi::max_value (TYPE_PRECISION (TREE_TYPE (@0)), SIGNED) - 1))
2428 (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
2429 (icmp (convert:stype @0) { build_int_cst (stype, 0); })))))
2431 /* X / 4 < Y / 4 iff X < Y when the division is known to be exact. */
2432 (for cmp (simple_comparison)
2434 (cmp (convert?@3 (exact_div @0 INTEGER_CST@2)) (convert? (exact_div @1 @2)))
2435 (if (element_precision (@3) >= element_precision (@0)
2436 && types_match (@0, @1))
2437 (if (wi::lt_p (wi::to_wide (@2), 0, TYPE_SIGN (TREE_TYPE (@2))))
2438 (if (!TYPE_UNSIGNED (TREE_TYPE (@3)))
2440 (if (tree_expr_nonzero_p (@0) && tree_expr_nonzero_p (@1))
2443 tree utype = unsigned_type_for (TREE_TYPE (@0));
2445 (cmp (convert:utype @1) (convert:utype @0)))))
2446 (if (wi::gt_p (wi::to_wide (@2), 1, TYPE_SIGN (TREE_TYPE (@2))))
2447 (if (TYPE_UNSIGNED (TREE_TYPE (@0)) || !TYPE_UNSIGNED (TREE_TYPE (@3)))
2451 tree utype = unsigned_type_for (TREE_TYPE (@0));
2453 (cmp (convert:utype @0) (convert:utype @1)))))))))
2455 /* X / C1 op C2 into a simple range test. */
2456 (for cmp (simple_comparison)
2458 (cmp (trunc_div:s @0 INTEGER_CST@1) INTEGER_CST@2)
2459 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2460 && integer_nonzerop (@1)
2461 && !TREE_OVERFLOW (@1)
2462 && !TREE_OVERFLOW (@2))
2463 (with { tree lo, hi; bool neg_overflow;
2464 enum tree_code code = fold_div_compare (cmp, @1, @2, &lo, &hi,
2467 (if (code == LT_EXPR || code == GE_EXPR)
2468 (if (TREE_OVERFLOW (lo))
2469 { build_int_cst (type, (code == LT_EXPR) ^ neg_overflow); }
2470 (if (code == LT_EXPR)
2473 (if (code == LE_EXPR || code == GT_EXPR)
2474 (if (TREE_OVERFLOW (hi))
2475 { build_int_cst (type, (code == LE_EXPR) ^ neg_overflow); }
2476 (if (code == LE_EXPR)
2480 { build_int_cst (type, code == NE_EXPR); })
2481 (if (code == EQ_EXPR && !hi)
2483 (if (code == EQ_EXPR && !lo)
2485 (if (code == NE_EXPR && !hi)
2487 (if (code == NE_EXPR && !lo)
2490 { build_range_check (UNKNOWN_LOCATION, type, @0, code == EQ_EXPR,
2494 tree etype = range_check_type (TREE_TYPE (@0));
2497 hi = fold_convert (etype, hi);
2498 lo = fold_convert (etype, lo);
2499 hi = const_binop (MINUS_EXPR, etype, hi, lo);
2502 (if (etype && hi && !TREE_OVERFLOW (hi))
2503 (if (code == EQ_EXPR)
2504 (le (minus (convert:etype @0) { lo; }) { hi; })
2505 (gt (minus (convert:etype @0) { lo; }) { hi; })))))))))
2507 /* X + Z < Y + Z is the same as X < Y when there is no overflow. */
2508 (for op (lt le ge gt)
2510 (op (plus:c @0 @2) (plus:c @1 @2))
2511 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2512 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
2515 /* As a special case, X + C < Y + C is the same as (signed) X < (signed) Y
2516 when C is an unsigned integer constant with only the MSB set, and X and
2517 Y have types of equal or lower integer conversion rank than C's. */
2518 (for op (lt le ge gt)
2520 (op (plus @1 INTEGER_CST@0) (plus @2 @0))
2521 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2522 && TYPE_UNSIGNED (TREE_TYPE (@0))
2523 && wi::only_sign_bit_p (wi::to_wide (@0)))
2524 (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
2525 (op (convert:stype @1) (convert:stype @2))))))
2527 /* For equality and subtraction, this is also true with wrapping overflow. */
2528 (for op (eq ne minus)
2530 (op (plus:c @0 @2) (plus:c @1 @2))
2531 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2532 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2533 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
2536 /* X - Z < Y - Z is the same as X < Y when there is no overflow. */
2537 (for op (lt le ge gt)
2539 (op (minus @0 @2) (minus @1 @2))
2540 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2541 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
2543 /* For equality and subtraction, this is also true with wrapping overflow. */
2544 (for op (eq ne minus)
2546 (op (minus @0 @2) (minus @1 @2))
2547 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2548 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2549 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
2551 /* And for pointers... */
2552 (for op (simple_comparison)
2554 (op (pointer_diff@3 @0 @2) (pointer_diff @1 @2))
2555 (if (!TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
2558 (minus (pointer_diff@3 @0 @2) (pointer_diff @1 @2))
2559 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@3))
2560 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
2561 (pointer_diff @0 @1)))
2563 /* Z - X < Z - Y is the same as Y < X when there is no overflow. */
2564 (for op (lt le ge gt)
2566 (op (minus @2 @0) (minus @2 @1))
2567 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2568 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
2570 /* For equality and subtraction, this is also true with wrapping overflow. */
2571 (for op (eq ne minus)
2573 (op (minus @2 @0) (minus @2 @1))
2574 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2575 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2576 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
2578 /* And for pointers... */
2579 (for op (simple_comparison)
2581 (op (pointer_diff@3 @2 @0) (pointer_diff @2 @1))
2582 (if (!TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
2585 (minus (pointer_diff@3 @2 @0) (pointer_diff @2 @1))
2586 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@3))
2587 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
2588 (pointer_diff @1 @0)))
2590 /* X + Y < Y is the same as X < 0 when there is no overflow. */
2591 (for op (lt le gt ge)
2593 (op:c (plus:c@2 @0 @1) @1)
2594 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2595 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2596 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0))
2597 && (CONSTANT_CLASS_P (@0) || single_use (@2)))
2598 (op @0 { build_zero_cst (TREE_TYPE (@0)); }))))
2599 /* For equality, this is also true with wrapping overflow. */
2602 (op:c (nop_convert?@3 (plus:c@2 @0 (convert1? @1))) (convert2? @1))
2603 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2604 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2605 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
2606 && (CONSTANT_CLASS_P (@0) || (single_use (@2) && single_use (@3)))
2607 && tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@2))
2608 && tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@1)))
2609 (op @0 { build_zero_cst (TREE_TYPE (@0)); })))
2611 (op:c (nop_convert?@3 (pointer_plus@2 (convert1? @0) @1)) (convert2? @0))
2612 (if (tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@0))
2613 && tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0))
2614 && (CONSTANT_CLASS_P (@1) || (single_use (@2) && single_use (@3))))
2615 (op @1 { build_zero_cst (TREE_TYPE (@1)); }))))
2617 /* (&a + b) !=/== (&a[1] + c) -> (&a[0] - &a[1]) + b !=/== c */
2620 (neeq:c ADDR_EXPR@0 (pointer_plus @2 @3))
2621 (with { poly_int64 diff; tree inner_type = TREE_TYPE (@3);}
2622 (if (ptr_difference_const (@0, @2, &diff))
2623 (neeq { build_int_cst_type (inner_type, diff); } @3))))
2625 (neeq (pointer_plus ADDR_EXPR@0 @1) (pointer_plus ADDR_EXPR@2 @3))
2626 (with { poly_int64 diff; tree inner_type = TREE_TYPE (@1);}
2627 (if (ptr_difference_const (@0, @2, &diff))
2628 (neeq (plus { build_int_cst_type (inner_type, diff); } @1) @3)))))
2630 /* X - Y < X is the same as Y > 0 when there is no overflow.
2631 For equality, this is also true with wrapping overflow. */
2632 (for op (simple_comparison)
2634 (op:c @0 (minus@2 @0 @1))
2635 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2636 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2637 || ((op == EQ_EXPR || op == NE_EXPR)
2638 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
2639 && (CONSTANT_CLASS_P (@1) || single_use (@2)))
2640 (op @1 { build_zero_cst (TREE_TYPE (@1)); }))))
2643 (X / Y) == 0 -> X < Y if X, Y are unsigned.
2644 (X / Y) != 0 -> X >= Y, if X, Y are unsigned. */
2648 (cmp (trunc_div @0 @1) integer_zerop)
2649 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
2650 /* Complex ==/!= is allowed, but not </>=. */
2651 && TREE_CODE (TREE_TYPE (@0)) != COMPLEX_TYPE
2652 && (VECTOR_TYPE_P (type) || !VECTOR_TYPE_P (TREE_TYPE (@0))))
2655 /* X == C - X can never be true if C is odd. */
2658 (cmp:c (convert? @0) (convert1? (minus INTEGER_CST@1 (convert2? @0))))
2659 (if (TREE_INT_CST_LOW (@1) & 1)
2660 { constant_boolean_node (cmp == NE_EXPR, type); })))
2662 /* Arguments on which one can call get_nonzero_bits to get the bits
2664 (match with_possible_nonzero_bits
2666 (match with_possible_nonzero_bits
2668 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))))
2669 /* Slightly extended version, do not make it recursive to keep it cheap. */
2670 (match (with_possible_nonzero_bits2 @0)
2671 with_possible_nonzero_bits@0)
2672 (match (with_possible_nonzero_bits2 @0)
2673 (bit_and:c with_possible_nonzero_bits@0 @2))
2675 /* Same for bits that are known to be set, but we do not have
2676 an equivalent to get_nonzero_bits yet. */
2677 (match (with_certain_nonzero_bits2 @0)
2679 (match (with_certain_nonzero_bits2 @0)
2680 (bit_ior @1 INTEGER_CST@0))
2682 /* X == C (or X & Z == Y | C) is impossible if ~nonzero(X) & C != 0. */
2685 (cmp:c (with_possible_nonzero_bits2 @0) (with_certain_nonzero_bits2 @1))
2686 (if (wi::bit_and_not (wi::to_wide (@1), get_nonzero_bits (@0)) != 0)
2687 { constant_boolean_node (cmp == NE_EXPR, type); })))
2689 /* ((X inner_op C0) outer_op C1)
2690 With X being a tree where value_range has reasoned certain bits to always be
2691 zero throughout its computed value range,
2692 inner_op = {|,^}, outer_op = {|,^} and inner_op != outer_op
2693 where zero_mask has 1's for all bits that are sure to be 0 in
2695 if (inner_op == '^') C0 &= ~C1;
2696 if ((C0 & ~zero_mask) == 0) then emit (X outer_op (C0 outer_op C1)
2697 if ((C1 & ~zero_mask) == 0) then emit (X inner_op (C0 outer_op C1)
2699 (for inner_op (bit_ior bit_xor)
2700 outer_op (bit_xor bit_ior)
2703 (inner_op:s @2 INTEGER_CST@0) INTEGER_CST@1)
2707 wide_int zero_mask_not;
2711 if (TREE_CODE (@2) == SSA_NAME)
2712 zero_mask_not = get_nonzero_bits (@2);
2716 if (inner_op == BIT_XOR_EXPR)
2718 C0 = wi::bit_and_not (wi::to_wide (@0), wi::to_wide (@1));
2719 cst_emit = C0 | wi::to_wide (@1);
2723 C0 = wi::to_wide (@0);
2724 cst_emit = C0 ^ wi::to_wide (@1);
2727 (if (!fail && (C0 & zero_mask_not) == 0)
2728 (outer_op @2 { wide_int_to_tree (type, cst_emit); })
2729 (if (!fail && (wi::to_wide (@1) & zero_mask_not) == 0)
2730 (inner_op @2 { wide_int_to_tree (type, cst_emit); }))))))
2732 /* Associate (p +p off1) +p off2 as (p +p (off1 + off2)). */
2734 (pointer_plus (pointer_plus:s @0 @1) @3)
2735 (pointer_plus @0 (plus @1 @3)))
2738 (pointer_plus (convert:s (pointer_plus:s @0 @1)) @3)
2739 (convert:type (pointer_plus @0 (plus @1 @3))))
2746 tem4 = (unsigned long) tem3;
2751 (pointer_plus @0 (convert?@2 (minus@3 (convert @1) (convert @0))))
2752 /* Conditionally look through a sign-changing conversion. */
2753 (if (TYPE_PRECISION (TREE_TYPE (@2)) == TYPE_PRECISION (TREE_TYPE (@3))
2754 && ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@1)))
2755 || (GENERIC && type == TREE_TYPE (@1))))
2758 (pointer_plus @0 (convert?@2 (pointer_diff@3 @1 @@0)))
2759 (if (TYPE_PRECISION (TREE_TYPE (@2)) >= TYPE_PRECISION (TREE_TYPE (@3)))
2763 tem = (sizetype) ptr;
2767 and produce the simpler and easier to analyze with respect to alignment
2768 ... = ptr & ~algn; */
2770 (pointer_plus @0 (negate (bit_and (convert @0) INTEGER_CST@1)))
2771 (with { tree algn = wide_int_to_tree (TREE_TYPE (@0), ~wi::to_wide (@1)); }
2772 (bit_and @0 { algn; })))
2774 /* Try folding difference of addresses. */
2776 (minus (convert ADDR_EXPR@0) (convert (pointer_plus @1 @2)))
2777 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2778 (with { poly_int64 diff; }
2779 (if (ptr_difference_const (@0, @1, &diff))
2780 (minus { build_int_cst_type (type, diff); } (convert @2))))))
2782 (minus (convert (pointer_plus @0 @2)) (convert ADDR_EXPR@1))
2783 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2784 (with { poly_int64 diff; }
2785 (if (ptr_difference_const (@0, @1, &diff))
2786 (plus (convert @2) { build_int_cst_type (type, diff); })))))
2788 (minus (convert ADDR_EXPR@0) (convert @1))
2789 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2790 (with { poly_int64 diff; }
2791 (if (ptr_difference_const (@0, @1, &diff))
2792 { build_int_cst_type (type, diff); }))))
2794 (minus (convert @0) (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 { build_int_cst_type (type, diff); }))))
2800 (pointer_diff (convert?@2 ADDR_EXPR@0) (convert1?@3 @1))
2801 (if (tree_nop_conversion_p (TREE_TYPE(@2), TREE_TYPE (@0))
2802 && tree_nop_conversion_p (TREE_TYPE(@3), TREE_TYPE (@1)))
2803 (with { poly_int64 diff; }
2804 (if (ptr_difference_const (@0, @1, &diff))
2805 { build_int_cst_type (type, diff); }))))
2807 (pointer_diff (convert?@2 @0) (convert1?@3 ADDR_EXPR@1))
2808 (if (tree_nop_conversion_p (TREE_TYPE(@2), TREE_TYPE (@0))
2809 && tree_nop_conversion_p (TREE_TYPE(@3), TREE_TYPE (@1)))
2810 (with { poly_int64 diff; }
2811 (if (ptr_difference_const (@0, @1, &diff))
2812 { build_int_cst_type (type, diff); }))))
2814 /* (&a+b) - (&a[1] + c) -> sizeof(a[0]) + (b - c) */
2816 (pointer_diff (pointer_plus ADDR_EXPR@0 @1) (pointer_plus ADDR_EXPR@2 @3))
2817 (with { poly_int64 diff; }
2818 (if (ptr_difference_const (@0, @2, &diff))
2819 (plus { build_int_cst_type (type, diff); } (convert (minus @1 @3))))))
2820 /* (p + b) - &p->d -> offsetof (*p, d) + b */
2822 (pointer_diff (pointer_plus @0 @1) ADDR_EXPR@2)
2823 (with { poly_int64 diff; }
2824 (if (ptr_difference_const (@0, @2, &diff))
2825 (plus { build_int_cst_type (type, diff); } (convert @1)))))
2827 (pointer_diff ADDR_EXPR@0 (pointer_plus @1 @2))
2828 (with { poly_int64 diff; }
2829 (if (ptr_difference_const (@0, @1, &diff))
2830 (minus { build_int_cst_type (type, diff); } (convert @2)))))
2832 /* Canonicalize (T *)(ptr - ptr-cst) to &MEM[ptr + -ptr-cst]. */
2834 (convert (pointer_diff @0 INTEGER_CST@1))
2835 (if (POINTER_TYPE_P (type))
2836 { build_fold_addr_expr_with_type
2837 (build2 (MEM_REF, char_type_node, @0,
2838 wide_int_to_tree (ptr_type_node, wi::neg (wi::to_wide (@1)))),
2841 /* If arg0 is derived from the address of an object or function, we may
2842 be able to fold this expression using the object or function's
2845 (bit_and (convert? @0) INTEGER_CST@1)
2846 (if (POINTER_TYPE_P (TREE_TYPE (@0))
2847 && tree_nop_conversion_p (type, TREE_TYPE (@0)))
2851 unsigned HOST_WIDE_INT bitpos;
2852 get_pointer_alignment_1 (@0, &align, &bitpos);
2854 (if (wi::ltu_p (wi::to_wide (@1), align / BITS_PER_UNIT))
2855 { wide_int_to_tree (type, (wi::to_wide (@1)
2856 & (bitpos / BITS_PER_UNIT))); }))))
2859 uniform_integer_cst_p
2861 tree int_cst = uniform_integer_cst_p (t);
2862 tree inner_type = TREE_TYPE (int_cst);
2864 (if ((INTEGRAL_TYPE_P (inner_type)
2865 || POINTER_TYPE_P (inner_type))
2866 && wi::eq_p (wi::to_wide (int_cst), wi::min_value (inner_type))))))
2869 uniform_integer_cst_p
2871 tree int_cst = uniform_integer_cst_p (t);
2872 tree itype = TREE_TYPE (int_cst);
2874 (if ((INTEGRAL_TYPE_P (itype)
2875 || POINTER_TYPE_P (itype))
2876 && wi::eq_p (wi::to_wide (int_cst), wi::max_value (itype))))))
2878 /* x > y && x != XXX_MIN --> x > y
2879 x > y && x == XXX_MIN --> false . */
2882 (bit_and:c (gt:c@2 @0 @1) (eqne @0 min_value))
2884 (if (eqne == EQ_EXPR)
2885 { constant_boolean_node (false, type); })
2886 (if (eqne == NE_EXPR)
2890 /* x < y && x != XXX_MAX --> x < y
2891 x < y && x == XXX_MAX --> false. */
2894 (bit_and:c (lt:c@2 @0 @1) (eqne @0 max_value))
2896 (if (eqne == EQ_EXPR)
2897 { constant_boolean_node (false, type); })
2898 (if (eqne == NE_EXPR)
2902 /* x <= y && x == XXX_MIN --> x == XXX_MIN. */
2904 (bit_and:c (le:c @0 @1) (eq@2 @0 min_value))
2907 /* x >= y && x == XXX_MAX --> x == XXX_MAX. */
2909 (bit_and:c (ge:c @0 @1) (eq@2 @0 max_value))
2912 /* x > y || x != XXX_MIN --> x != XXX_MIN. */
2914 (bit_ior:c (gt:c @0 @1) (ne@2 @0 min_value))
2917 /* x <= y || x != XXX_MIN --> true. */
2919 (bit_ior:c (le:c @0 @1) (ne @0 min_value))
2920 { constant_boolean_node (true, type); })
2922 /* x <= y || x == XXX_MIN --> x <= y. */
2924 (bit_ior:c (le:c@2 @0 @1) (eq @0 min_value))
2927 /* x < y || x != XXX_MAX --> x != XXX_MAX. */
2929 (bit_ior:c (lt:c @0 @1) (ne@2 @0 max_value))
2932 /* x >= y || x != XXX_MAX --> true
2933 x >= y || x == XXX_MAX --> x >= y. */
2936 (bit_ior:c (ge:c@2 @0 @1) (eqne @0 max_value))
2938 (if (eqne == EQ_EXPR)
2940 (if (eqne == NE_EXPR)
2941 { constant_boolean_node (true, type); }))))
2943 /* y == XXX_MIN || x < y --> x <= y - 1 */
2945 (bit_ior:c (eq:s @1 min_value) (lt:cs @0 @1))
2946 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2947 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
2948 (le @0 (minus @1 { build_int_cst (TREE_TYPE (@1), 1); }))))
2950 /* y != XXX_MIN && x >= y --> x > y - 1 */
2952 (bit_and:c (ne:s @1 min_value) (ge:cs @0 @1))
2953 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2954 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
2955 (gt @0 (minus @1 { build_int_cst (TREE_TYPE (@1), 1); }))))
2957 /* Convert (X == CST1) && (X OP2 CST2) to a known value
2958 based on CST1 OP2 CST2. Similarly for (X != CST1). */
2959 /* Convert (X == Y) && (X OP2 Y) to a known value if X is an integral type.
2960 Similarly for (X != Y). */
2963 (for code2 (eq ne lt gt le ge)
2965 (bit_and:c (code1@3 @0 @1) (code2@4 @0 @2))
2966 (if ((TREE_CODE (@1) == INTEGER_CST
2967 && TREE_CODE (@2) == INTEGER_CST)
2968 || ((INTEGRAL_TYPE_P (TREE_TYPE (@1))
2969 || POINTER_TYPE_P (TREE_TYPE (@1)))
2970 && operand_equal_p (@1, @2)))
2973 bool one_before = false;
2974 bool one_after = false;
2976 if (TREE_CODE (@1) == INTEGER_CST
2977 && TREE_CODE (@2) == INTEGER_CST)
2979 cmp = tree_int_cst_compare (@1, @2);
2981 && wi::to_wide (@1) == wi::to_wide (@2) - 1)
2984 && wi::to_wide (@1) == wi::to_wide (@2) + 1)
2990 case EQ_EXPR: val = (cmp == 0); break;
2991 case NE_EXPR: val = (cmp != 0); break;
2992 case LT_EXPR: val = (cmp < 0); break;
2993 case GT_EXPR: val = (cmp > 0); break;
2994 case LE_EXPR: val = (cmp <= 0); break;
2995 case GE_EXPR: val = (cmp >= 0); break;
2996 default: gcc_unreachable ();
3000 (if (code1 == EQ_EXPR && val) @3)
3001 (if (code1 == EQ_EXPR && !val) { constant_boolean_node (false, type); })
3002 (if (code1 == NE_EXPR && !val) @4)
3003 (if (code1 == NE_EXPR
3007 (if (code1 == NE_EXPR
3011 /* (a != (b+1)) & (a > b) -> a > (b+1) */
3012 (if (code1 == NE_EXPR
3016 /* (a != (b-1)) & (a < b) -> a < (b-1) */
3017 (if (code1 == NE_EXPR
3028 /* Convert (X OP1 CST1) && (X OP2 CST2).
3029 Convert (X OP1 Y) && (X OP2 Y). */
3031 (for code1 (lt le gt ge)
3032 (for code2 (lt le gt ge)
3034 (bit_and (code1:c@3 @0 @1) (code2:c@4 @0 @2))
3035 (if ((TREE_CODE (@1) == INTEGER_CST
3036 && TREE_CODE (@2) == INTEGER_CST)
3037 || ((INTEGRAL_TYPE_P (TREE_TYPE (@1))
3038 || POINTER_TYPE_P (TREE_TYPE (@1)))
3039 && operand_equal_p (@1, @2)))
3043 if (TREE_CODE (@1) == INTEGER_CST
3044 && TREE_CODE (@2) == INTEGER_CST)
3045 cmp = tree_int_cst_compare (@1, @2);
3048 /* Choose the more restrictive of two < or <= comparisons. */
3049 (if ((code1 == LT_EXPR || code1 == LE_EXPR)
3050 && (code2 == LT_EXPR || code2 == LE_EXPR))
3051 (if ((cmp < 0) || (cmp == 0 && code1 == LT_EXPR))
3054 /* Likewise chose the more restrictive of two > or >= comparisons. */
3055 (if ((code1 == GT_EXPR || code1 == GE_EXPR)
3056 && (code2 == GT_EXPR || code2 == GE_EXPR))
3057 (if ((cmp > 0) || (cmp == 0 && code1 == GT_EXPR))
3060 /* Check for singleton ranges. */
3062 && ((code1 == LE_EXPR && code2 == GE_EXPR)
3063 || (code1 == GE_EXPR && code2 == LE_EXPR)))
3065 /* Check for disjoint ranges. */
3067 && (code1 == LT_EXPR || code1 == LE_EXPR)
3068 && (code2 == GT_EXPR || code2 == GE_EXPR))
3069 { constant_boolean_node (false, type); })
3071 && (code1 == GT_EXPR || code1 == GE_EXPR)
3072 && (code2 == LT_EXPR || code2 == LE_EXPR))
3073 { constant_boolean_node (false, type); })
3076 /* Convert (X == CST1) || (X OP2 CST2) to a known value
3077 based on CST1 OP2 CST2. Similarly for (X != CST1). */
3078 /* Convert (X == Y) || (X OP2 Y) to a known value if X is an integral type.
3079 Similarly for (X != Y). */
3082 (for code2 (eq ne lt gt le ge)
3084 (bit_ior:c (code1@3 @0 @1) (code2@4 @0 @2))
3085 (if ((TREE_CODE (@1) == INTEGER_CST
3086 && TREE_CODE (@2) == INTEGER_CST)
3087 || ((INTEGRAL_TYPE_P (TREE_TYPE (@1))
3088 || POINTER_TYPE_P (TREE_TYPE (@1)))
3089 && operand_equal_p (@1, @2)))
3092 bool one_before = false;
3093 bool one_after = false;
3095 if (TREE_CODE (@1) == INTEGER_CST
3096 && TREE_CODE (@2) == INTEGER_CST)
3098 cmp = tree_int_cst_compare (@1, @2);
3100 && wi::to_wide (@1) == wi::to_wide (@2) - 1)
3103 && wi::to_wide (@1) == wi::to_wide (@2) + 1)
3109 case EQ_EXPR: val = (cmp == 0); break;
3110 case NE_EXPR: val = (cmp != 0); break;
3111 case LT_EXPR: val = (cmp < 0); break;
3112 case GT_EXPR: val = (cmp > 0); break;
3113 case LE_EXPR: val = (cmp <= 0); break;
3114 case GE_EXPR: val = (cmp >= 0); break;
3115 default: gcc_unreachable ();
3119 (if (code1 == EQ_EXPR && val) @4)
3120 (if (code1 == NE_EXPR && val) { constant_boolean_node (true, type); })
3121 (if (code1 == NE_EXPR && !val) @3)
3122 (if (code1 == EQ_EXPR
3126 (if (code1 == EQ_EXPR
3130 /* (a == (b-1)) | (a >= b) -> a >= (b-1) */
3131 (if (code1 == EQ_EXPR
3135 /* (a == (b+1)) | (a <= b) -> a <= (b-1) */
3136 (if (code1 == EQ_EXPR
3147 /* Convert (X OP1 CST1) || (X OP2 CST2).
3148 Convert (X OP1 Y) || (X OP2 Y). */
3150 (for code1 (lt le gt ge)
3151 (for code2 (lt le gt ge)
3153 (bit_ior (code1@3 @0 @1) (code2@4 @0 @2))
3154 (if ((TREE_CODE (@1) == INTEGER_CST
3155 && TREE_CODE (@2) == INTEGER_CST)
3156 || ((INTEGRAL_TYPE_P (TREE_TYPE (@1))
3157 || POINTER_TYPE_P (TREE_TYPE (@1)))
3158 && operand_equal_p (@1, @2)))
3162 if (TREE_CODE (@1) == INTEGER_CST
3163 && TREE_CODE (@2) == INTEGER_CST)
3164 cmp = tree_int_cst_compare (@1, @2);
3167 /* Choose the more restrictive of two < or <= comparisons. */
3168 (if ((code1 == LT_EXPR || code1 == LE_EXPR)
3169 && (code2 == LT_EXPR || code2 == LE_EXPR))
3170 (if ((cmp < 0) || (cmp == 0 && code1 == LT_EXPR))
3173 /* Likewise chose the more restrictive of two > or >= comparisons. */
3174 (if ((code1 == GT_EXPR || code1 == GE_EXPR)
3175 && (code2 == GT_EXPR || code2 == GE_EXPR))
3176 (if ((cmp > 0) || (cmp == 0 && code1 == GT_EXPR))
3179 /* Check for singleton ranges. */
3181 && ((code1 == LT_EXPR && code2 == GT_EXPR)
3182 || (code1 == GT_EXPR && code2 == LT_EXPR)))
3184 /* Check for disjoint ranges. */
3186 && (code1 == LT_EXPR || code1 == LE_EXPR)
3187 && (code2 == GT_EXPR || code2 == GE_EXPR))
3188 { constant_boolean_node (true, type); })
3190 && (code1 == GT_EXPR || code1 == GE_EXPR)
3191 && (code2 == LT_EXPR || code2 == LE_EXPR))
3192 { constant_boolean_node (true, type); })
3195 /* Optimize (a CMP b) ^ (a CMP b) */
3196 /* Optimize (a CMP b) != (a CMP b) */
3197 (for op (bit_xor ne)
3198 (for cmp1 (lt lt lt le le le)
3199 cmp2 (gt eq ne ge eq ne)
3200 rcmp (ne le gt ne lt ge)
3202 (op:c (cmp1:c @0 @1) (cmp2:c @0 @1))
3203 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
3206 /* Optimize (a CMP b) == (a CMP b) */
3207 (for cmp1 (lt lt lt le le le)
3208 cmp2 (gt eq ne ge eq ne)
3209 rcmp (eq gt le eq ge lt)
3211 (eq:c (cmp1:c @0 @1) (cmp2:c @0 @1))
3212 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
3215 /* We can't reassociate at all for saturating types. */
3216 (if (!TYPE_SATURATING (type))
3218 /* Contract negates. */
3219 /* A + (-B) -> A - B */
3221 (plus:c @0 (convert? (negate @1)))
3222 /* Apply STRIP_NOPS on the negate. */
3223 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
3224 && !TYPE_OVERFLOW_SANITIZED (type))
3228 if (INTEGRAL_TYPE_P (type)
3229 && TYPE_OVERFLOW_WRAPS (type) != TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
3230 t1 = TYPE_OVERFLOW_WRAPS (type) ? type : TREE_TYPE (@1);
3232 (convert (minus (convert:t1 @0) (convert:t1 @1))))))
3233 /* A - (-B) -> A + B */
3235 (minus @0 (convert? (negate @1)))
3236 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
3237 && !TYPE_OVERFLOW_SANITIZED (type))
3241 if (INTEGRAL_TYPE_P (type)
3242 && TYPE_OVERFLOW_WRAPS (type) != TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
3243 t1 = TYPE_OVERFLOW_WRAPS (type) ? type : TREE_TYPE (@1);
3245 (convert (plus (convert:t1 @0) (convert:t1 @1))))))
3247 Sign-extension is ok except for INT_MIN, which thankfully cannot
3248 happen without overflow. */
3250 (negate (convert (negate @1)))
3251 (if (INTEGRAL_TYPE_P (type)
3252 && (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@1))
3253 || (!TYPE_UNSIGNED (TREE_TYPE (@1))
3254 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))))
3255 && !TYPE_OVERFLOW_SANITIZED (type)
3256 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@1)))
3259 (negate (convert negate_expr_p@1))
3260 (if (SCALAR_FLOAT_TYPE_P (type)
3261 && ((DECIMAL_FLOAT_TYPE_P (type)
3262 == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1))
3263 && TYPE_PRECISION (type) >= TYPE_PRECISION (TREE_TYPE (@1)))
3264 || !HONOR_SIGN_DEPENDENT_ROUNDING (type)))
3265 (convert (negate @1))))
3267 (negate (nop_convert? (negate @1)))
3268 (if (!TYPE_OVERFLOW_SANITIZED (type)
3269 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@1)))
3272 /* We can't reassociate floating-point unless -fassociative-math
3273 or fixed-point plus or minus because of saturation to +-Inf. */
3274 (if ((!FLOAT_TYPE_P (type) || flag_associative_math)
3275 && !FIXED_POINT_TYPE_P (type))
3277 /* Match patterns that allow contracting a plus-minus pair
3278 irrespective of overflow issues. */
3279 /* (A +- B) - A -> +- B */
3280 /* (A +- B) -+ B -> A */
3281 /* A - (A +- B) -> -+ B */
3282 /* A +- (B -+ A) -> +- B */
3284 (minus (nop_convert1? (plus:c (nop_convert2? @0) @1)) @0)
3287 (minus (nop_convert1? (minus (nop_convert2? @0) @1)) @0)
3288 (if (!ANY_INTEGRAL_TYPE_P (type)
3289 || TYPE_OVERFLOW_WRAPS (type))
3290 (negate (view_convert @1))
3291 (view_convert (negate @1))))
3293 (plus:c (nop_convert1? (minus @0 (nop_convert2? @1))) @1)
3296 (minus @0 (nop_convert1? (plus:c (nop_convert2? @0) @1)))
3297 (if (!ANY_INTEGRAL_TYPE_P (type)
3298 || TYPE_OVERFLOW_WRAPS (type))
3299 (negate (view_convert @1))
3300 (view_convert (negate @1))))
3302 (minus @0 (nop_convert1? (minus (nop_convert2? @0) @1)))
3304 /* (A +- B) + (C - A) -> C +- B */
3305 /* (A + B) - (A - C) -> B + C */
3306 /* More cases are handled with comparisons. */
3308 (plus:c (plus:c @0 @1) (minus @2 @0))
3311 (plus:c (minus @0 @1) (minus @2 @0))
3314 (plus:c (pointer_diff @0 @1) (pointer_diff @2 @0))
3315 (if (TYPE_OVERFLOW_UNDEFINED (type)
3316 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0)))
3317 (pointer_diff @2 @1)))
3319 (minus (plus:c @0 @1) (minus @0 @2))
3322 /* (A +- CST1) +- CST2 -> A + CST3
3323 Use view_convert because it is safe for vectors and equivalent for
3325 (for outer_op (plus minus)
3326 (for inner_op (plus minus)
3327 neg_inner_op (minus plus)
3329 (outer_op (nop_convert? (inner_op @0 CONSTANT_CLASS_P@1))
3331 /* If one of the types wraps, use that one. */
3332 (if (!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_WRAPS (type))
3333 /* If all 3 captures are CONSTANT_CLASS_P, punt, as we might recurse
3334 forever if something doesn't simplify into a constant. */
3335 (if (!CONSTANT_CLASS_P (@0))
3336 (if (outer_op == PLUS_EXPR)
3337 (plus (view_convert @0) (inner_op! @2 (view_convert @1)))
3338 (minus (view_convert @0) (neg_inner_op! @2 (view_convert @1)))))
3339 (if (!ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3340 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
3341 (if (outer_op == PLUS_EXPR)
3342 (view_convert (plus @0 (inner_op! (view_convert @2) @1)))
3343 (view_convert (minus @0 (neg_inner_op! (view_convert @2) @1))))
3344 /* If the constant operation overflows we cannot do the transform
3345 directly as we would introduce undefined overflow, for example
3346 with (a - 1) + INT_MIN. */
3347 (if (types_match (type, @0) && !TYPE_OVERFLOW_SANITIZED (type))
3348 (with { tree cst = const_binop (outer_op == inner_op
3349 ? PLUS_EXPR : MINUS_EXPR,
3352 (if (INTEGRAL_TYPE_P (type) && !TREE_OVERFLOW (cst))
3353 (inner_op @0 { cst; } )
3354 /* X+INT_MAX+1 is X-INT_MIN. */
3355 (if (INTEGRAL_TYPE_P (type)
3356 && wi::to_wide (cst) == wi::min_value (type))
3357 (neg_inner_op @0 { wide_int_to_tree (type, wi::to_wide (cst)); })
3358 /* Last resort, use some unsigned type. */
3359 (with { tree utype = unsigned_type_for (type); }
3361 (view_convert (inner_op
3362 (view_convert:utype @0)
3364 { TREE_OVERFLOW (cst)
3365 ? drop_tree_overflow (cst) : cst; })))))))))))))))
3367 /* (CST1 - A) +- CST2 -> CST3 - A */
3368 (for outer_op (plus minus)
3370 (outer_op (nop_convert? (minus CONSTANT_CLASS_P@1 @0)) CONSTANT_CLASS_P@2)
3371 /* If one of the types wraps, use that one. */
3372 (if (!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_WRAPS (type))
3373 /* If all 3 captures are CONSTANT_CLASS_P, punt, as we might recurse
3374 forever if something doesn't simplify into a constant. */
3375 (if (!CONSTANT_CLASS_P (@0))
3376 (minus (outer_op! (view_convert @1) @2) (view_convert @0)))
3377 (if (!ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3378 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
3379 (view_convert (minus (outer_op! @1 (view_convert @2)) @0))
3380 (if (types_match (type, @0) && !TYPE_OVERFLOW_SANITIZED (type))
3381 (with { tree cst = const_binop (outer_op, type, @1, @2); }
3382 (if (cst && !TREE_OVERFLOW (cst))
3383 (minus { cst; } @0))))))))
3385 /* CST1 - (CST2 - A) -> CST3 + A
3386 Use view_convert because it is safe for vectors and equivalent for
3389 (minus CONSTANT_CLASS_P@1 (nop_convert? (minus CONSTANT_CLASS_P@2 @0)))
3390 /* If one of the types wraps, use that one. */
3391 (if (!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_WRAPS (type))
3392 /* If all 3 captures are CONSTANT_CLASS_P, punt, as we might recurse
3393 forever if something doesn't simplify into a constant. */
3394 (if (!CONSTANT_CLASS_P (@0))
3395 (plus (view_convert @0) (minus! @1 (view_convert @2))))
3396 (if (!ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3397 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
3398 (view_convert (plus @0 (minus! (view_convert @1) @2)))
3399 (if (types_match (type, @0) && !TYPE_OVERFLOW_SANITIZED (type))
3400 (with { tree cst = const_binop (MINUS_EXPR, type, @1, @2); }
3401 (if (cst && !TREE_OVERFLOW (cst))
3402 (plus { cst; } @0)))))))
3404 /* ((T)(A)) + CST -> (T)(A + CST) */
3407 (plus (convert:s SSA_NAME@0) INTEGER_CST@1)
3408 (if (TREE_CODE (TREE_TYPE (@0)) == INTEGER_TYPE
3409 && TREE_CODE (type) == INTEGER_TYPE
3410 && TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (@0))
3411 && int_fits_type_p (@1, TREE_TYPE (@0)))
3412 /* Perform binary operation inside the cast if the constant fits
3413 and (A + CST)'s range does not overflow. */
3416 wi::overflow_type min_ovf = wi::OVF_OVERFLOW,
3417 max_ovf = wi::OVF_OVERFLOW;
3418 tree inner_type = TREE_TYPE (@0);
3421 = wide_int::from (wi::to_wide (@1), TYPE_PRECISION (inner_type),
3422 TYPE_SIGN (inner_type));
3425 if (get_global_range_query ()->range_of_expr (vr, @0)
3426 && !vr.varying_p () && !vr.undefined_p ())
3428 wide_int wmin0 = vr.lower_bound ();
3429 wide_int wmax0 = vr.upper_bound ();
3430 wi::add (wmin0, w1, TYPE_SIGN (inner_type), &min_ovf);
3431 wi::add (wmax0, w1, TYPE_SIGN (inner_type), &max_ovf);
3434 (if (min_ovf == wi::OVF_NONE && max_ovf == wi::OVF_NONE)
3435 (convert (plus @0 { wide_int_to_tree (TREE_TYPE (@0), w1); } )))
3439 /* ((T)(A + CST1)) + CST2 -> (T)(A) + (T)CST1 + CST2 */
3441 (for op (plus minus)
3443 (plus (convert:s (op:s @0 INTEGER_CST@1)) INTEGER_CST@2)
3444 (if (TREE_CODE (TREE_TYPE (@0)) == INTEGER_TYPE
3445 && TREE_CODE (type) == INTEGER_TYPE
3446 && TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (@0))
3447 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
3448 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0))
3449 && TYPE_OVERFLOW_WRAPS (type))
3450 (plus (convert @0) (op @2 (convert @1))))))
3453 /* (T)(A) +- (T)(B) -> (T)(A +- B) only when (A +- B) could be simplified
3454 to a simple value. */
3455 (for op (plus minus)
3457 (op (convert @0) (convert @1))
3458 (if (INTEGRAL_TYPE_P (type)
3459 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
3460 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))
3461 && types_match (TREE_TYPE (@0), TREE_TYPE (@1))
3462 && !TYPE_OVERFLOW_TRAPS (type)
3463 && !TYPE_OVERFLOW_SANITIZED (type))
3464 (convert (op! @0 @1)))))
3468 (plus:c (convert? (bit_not @0)) (convert? @0))
3469 (if (!TYPE_OVERFLOW_TRAPS (type))
3470 (convert { build_all_ones_cst (TREE_TYPE (@0)); })))
3474 (plus (convert? (bit_not @0)) integer_each_onep)
3475 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
3476 (negate (convert @0))))
3480 (minus (convert? (negate @0)) integer_each_onep)
3481 (if (!TYPE_OVERFLOW_TRAPS (type)
3482 && TREE_CODE (type) != COMPLEX_TYPE
3483 && tree_nop_conversion_p (type, TREE_TYPE (@0)))
3484 (bit_not (convert @0))))
3488 (minus integer_all_onesp @0)
3489 (if (TREE_CODE (type) != COMPLEX_TYPE)
3492 /* (T)(P + A) - (T)P -> (T) A */
3494 (minus (convert (plus:c @@0 @1))
3496 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
3497 /* For integer types, if A has a smaller type
3498 than T the result depends on the possible
3500 E.g. T=size_t, A=(unsigned)429497295, P>0.
3501 However, if an overflow in P + A would cause
3502 undefined behavior, we can assume that there
3504 || (INTEGRAL_TYPE_P (TREE_TYPE (@1))
3505 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))))
3508 (minus (convert (pointer_plus @@0 @1))
3510 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
3511 /* For pointer types, if the conversion of A to the
3512 final type requires a sign- or zero-extension,
3513 then we have to punt - it is not defined which
3515 || (POINTER_TYPE_P (TREE_TYPE (@0))
3516 && TREE_CODE (@1) == INTEGER_CST
3517 && tree_int_cst_sign_bit (@1) == 0))
3520 (pointer_diff (pointer_plus @@0 @1) @0)
3521 /* The second argument of pointer_plus must be interpreted as signed, and
3522 thus sign-extended if necessary. */
3523 (with { tree stype = signed_type_for (TREE_TYPE (@1)); }
3524 /* Use view_convert instead of convert here, as POINTER_PLUS_EXPR
3525 second arg is unsigned even when we need to consider it as signed,
3526 we don't want to diagnose overflow here. */
3527 (convert (view_convert:stype @1))))
3529 /* (T)P - (T)(P + A) -> -(T) A */
3531 (minus (convert? @0)
3532 (convert (plus:c @@0 @1)))
3533 (if (INTEGRAL_TYPE_P (type)
3534 && TYPE_OVERFLOW_UNDEFINED (type)
3535 /* For integer literals, using an intermediate unsigned type to avoid
3536 an overflow at run time is counter-productive because it introduces
3537 spurious overflows at compile time, in the form of TREE_OVERFLOW on
3538 the result, which may be problematic in GENERIC for some front-ends:
3539 (T)P - (T)(P + 4) -> (T)(-(U)4) -> (T)(4294967292) -> -4(OVF)
3540 so we use the direct path for them. */
3541 && TREE_CODE (@1) != INTEGER_CST
3542 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
3543 (with { tree utype = unsigned_type_for (type); }
3544 (convert (negate (convert:utype @1))))
3545 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
3546 /* For integer types, if A has a smaller type
3547 than T the result depends on the possible
3549 E.g. T=size_t, A=(unsigned)429497295, P>0.
3550 However, if an overflow in P + A would cause
3551 undefined behavior, we can assume that there
3553 || (INTEGRAL_TYPE_P (TREE_TYPE (@1))
3554 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))))
3555 (negate (convert @1)))))
3558 (convert (pointer_plus @@0 @1)))
3559 (if (INTEGRAL_TYPE_P (type)
3560 && TYPE_OVERFLOW_UNDEFINED (type)
3561 /* See above the rationale for this condition. */
3562 && TREE_CODE (@1) != INTEGER_CST
3563 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
3564 (with { tree utype = unsigned_type_for (type); }
3565 (convert (negate (convert:utype @1))))
3566 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
3567 /* For pointer types, if the conversion of A to the
3568 final type requires a sign- or zero-extension,
3569 then we have to punt - it is not defined which
3571 || (POINTER_TYPE_P (TREE_TYPE (@0))
3572 && TREE_CODE (@1) == INTEGER_CST
3573 && tree_int_cst_sign_bit (@1) == 0))
3574 (negate (convert @1)))))
3576 (pointer_diff @0 (pointer_plus @@0 @1))
3577 /* The second argument of pointer_plus must be interpreted as signed, and
3578 thus sign-extended if necessary. */
3579 (with { tree stype = signed_type_for (TREE_TYPE (@1)); }
3580 /* Use view_convert instead of convert here, as POINTER_PLUS_EXPR
3581 second arg is unsigned even when we need to consider it as signed,
3582 we don't want to diagnose overflow here. */
3583 (negate (convert (view_convert:stype @1)))))
3585 /* (T)(P + A) - (T)(P + B) -> (T)A - (T)B */
3587 (minus (convert (plus:c @@0 @1))
3588 (convert (plus:c @0 @2)))
3589 (if (INTEGRAL_TYPE_P (type)
3590 && TYPE_OVERFLOW_UNDEFINED (type)
3591 && element_precision (type) <= element_precision (TREE_TYPE (@1))
3592 && element_precision (type) <= element_precision (TREE_TYPE (@2)))
3593 (with { tree utype = unsigned_type_for (type); }
3594 (convert (minus (convert:utype @1) (convert:utype @2))))
3595 (if (((element_precision (type) <= element_precision (TREE_TYPE (@1)))
3596 == (element_precision (type) <= element_precision (TREE_TYPE (@2))))
3597 && (element_precision (type) <= element_precision (TREE_TYPE (@1))
3598 /* For integer types, if A has a smaller type
3599 than T the result depends on the possible
3601 E.g. T=size_t, A=(unsigned)429497295, P>0.
3602 However, if an overflow in P + A would cause
3603 undefined behavior, we can assume that there
3605 || (INTEGRAL_TYPE_P (TREE_TYPE (@1))
3606 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
3607 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))
3608 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@2)))))
3609 (minus (convert @1) (convert @2)))))
3611 (minus (convert (pointer_plus @@0 @1))
3612 (convert (pointer_plus @0 @2)))
3613 (if (INTEGRAL_TYPE_P (type)
3614 && TYPE_OVERFLOW_UNDEFINED (type)
3615 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
3616 (with { tree utype = unsigned_type_for (type); }
3617 (convert (minus (convert:utype @1) (convert:utype @2))))
3618 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
3619 /* For pointer types, if the conversion of A to the
3620 final type requires a sign- or zero-extension,
3621 then we have to punt - it is not defined which
3623 || (POINTER_TYPE_P (TREE_TYPE (@0))
3624 && TREE_CODE (@1) == INTEGER_CST
3625 && tree_int_cst_sign_bit (@1) == 0
3626 && TREE_CODE (@2) == INTEGER_CST
3627 && tree_int_cst_sign_bit (@2) == 0))
3628 (minus (convert @1) (convert @2)))))
3630 (pointer_diff (pointer_plus @0 @2) (pointer_plus @1 @2))
3631 (pointer_diff @0 @1))
3633 (pointer_diff (pointer_plus @@0 @1) (pointer_plus @0 @2))
3634 /* The second argument of pointer_plus must be interpreted as signed, and
3635 thus sign-extended if necessary. */
3636 (with { tree stype = signed_type_for (TREE_TYPE (@1)); }
3637 /* Use view_convert instead of convert here, as POINTER_PLUS_EXPR
3638 second arg is unsigned even when we need to consider it as signed,
3639 we don't want to diagnose overflow here. */
3640 (minus (convert (view_convert:stype @1))
3641 (convert (view_convert:stype @2)))))))
3643 /* (A * C) +- (B * C) -> (A+-B) * C and (A * C) +- A -> A * (C+-1).
3644 Modeled after fold_plusminus_mult_expr. */
3645 (if (!TYPE_SATURATING (type)
3646 && (!FLOAT_TYPE_P (type) || flag_associative_math))
3647 (for plusminus (plus minus)
3649 (plusminus (mult:cs@3 @0 @1) (mult:cs@4 @0 @2))
3650 (if (!ANY_INTEGRAL_TYPE_P (type)
3651 || TYPE_OVERFLOW_WRAPS (type)
3652 || (INTEGRAL_TYPE_P (type)
3653 && tree_expr_nonzero_p (@0)
3654 && expr_not_equal_to (@0, wi::minus_one (TYPE_PRECISION (type)))))
3655 (if (single_use (@3) || single_use (@4))
3656 /* If @1 +- @2 is constant require a hard single-use on either
3657 original operand (but not on both). */
3658 (mult (plusminus @1 @2) @0)
3659 (mult! (plusminus @1 @2) @0)
3661 /* We cannot generate constant 1 for fract. */
3662 (if (!ALL_FRACT_MODE_P (TYPE_MODE (type)))
3664 (plusminus @0 (mult:c@3 @0 @2))
3665 (if ((!ANY_INTEGRAL_TYPE_P (type)
3666 || TYPE_OVERFLOW_WRAPS (type)
3667 /* For @0 + @0*@2 this transformation would introduce UB
3668 (where there was none before) for @0 in [-1,0] and @2 max.
3669 For @0 - @0*@2 this transformation would introduce UB
3670 for @0 0 and @2 in [min,min+1] or @0 -1 and @2 min+1. */
3671 || (INTEGRAL_TYPE_P (type)
3672 && ((tree_expr_nonzero_p (@0)
3673 && expr_not_equal_to (@0,
3674 wi::minus_one (TYPE_PRECISION (type))))
3675 || (plusminus == PLUS_EXPR
3676 ? expr_not_equal_to (@2,
3677 wi::max_value (TYPE_PRECISION (type), SIGNED))
3678 /* Let's ignore the @0 -1 and @2 min case. */
3679 : (expr_not_equal_to (@2,
3680 wi::min_value (TYPE_PRECISION (type), SIGNED))
3681 && expr_not_equal_to (@2,
3682 wi::min_value (TYPE_PRECISION (type), SIGNED)
3685 (mult (plusminus { build_one_cst (type); } @2) @0)))
3687 (plusminus (mult:c@3 @0 @2) @0)
3688 (if ((!ANY_INTEGRAL_TYPE_P (type)
3689 || TYPE_OVERFLOW_WRAPS (type)
3690 /* For @0*@2 + @0 this transformation would introduce UB
3691 (where there was none before) for @0 in [-1,0] and @2 max.
3692 For @0*@2 - @0 this transformation would introduce UB
3693 for @0 0 and @2 min. */
3694 || (INTEGRAL_TYPE_P (type)
3695 && ((tree_expr_nonzero_p (@0)
3696 && (plusminus == MINUS_EXPR
3697 || expr_not_equal_to (@0,
3698 wi::minus_one (TYPE_PRECISION (type)))))
3699 || expr_not_equal_to (@2,
3700 (plusminus == PLUS_EXPR
3701 ? wi::max_value (TYPE_PRECISION (type), SIGNED)
3702 : wi::min_value (TYPE_PRECISION (type), SIGNED))))))
3704 (mult (plusminus @2 { build_one_cst (type); }) @0))))))
3707 /* Canonicalize X + (X << C) into X * (1 + (1 << C)) and
3708 (X << C1) + (X << C2) into X * ((1 << C1) + (1 << C2)). */
3710 (plus:c @0 (lshift:s @0 INTEGER_CST@1))
3711 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3712 && tree_fits_uhwi_p (@1)
3713 && tree_to_uhwi (@1) < element_precision (type)
3714 && (INTEGRAL_TYPE_P (TREE_TYPE (@0))
3715 || optab_handler (smul_optab,
3716 TYPE_MODE (type)) != CODE_FOR_nothing))
3717 (with { tree t = type;
3718 if (!TYPE_OVERFLOW_WRAPS (t)) t = unsigned_type_for (t);
3719 wide_int w = wi::set_bit_in_zero (tree_to_uhwi (@1),
3720 element_precision (type));
3722 tree cst = wide_int_to_tree (VECTOR_TYPE_P (t) ? TREE_TYPE (t)
3724 cst = build_uniform_cst (t, cst); }
3725 (convert (mult (convert:t @0) { cst; })))))
3727 (plus (lshift:s @0 INTEGER_CST@1) (lshift:s @0 INTEGER_CST@2))
3728 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3729 && tree_fits_uhwi_p (@1)
3730 && tree_to_uhwi (@1) < element_precision (type)
3731 && tree_fits_uhwi_p (@2)
3732 && tree_to_uhwi (@2) < element_precision (type)
3733 && (INTEGRAL_TYPE_P (TREE_TYPE (@0))
3734 || optab_handler (smul_optab,
3735 TYPE_MODE (type)) != CODE_FOR_nothing))
3736 (with { tree t = type;
3737 if (!TYPE_OVERFLOW_WRAPS (t)) t = unsigned_type_for (t);
3738 unsigned int prec = element_precision (type);
3739 wide_int w = wi::set_bit_in_zero (tree_to_uhwi (@1), prec);
3740 w += wi::set_bit_in_zero (tree_to_uhwi (@2), prec);
3741 tree cst = wide_int_to_tree (VECTOR_TYPE_P (t) ? TREE_TYPE (t)
3743 cst = build_uniform_cst (t, cst); }
3744 (convert (mult (convert:t @0) { cst; })))))
3747 /* Canonicalize (X*C1)|(X*C2) and (X*C1)^(X*C2) to (C1+C2)*X when
3748 tree_nonzero_bits allows IOR and XOR to be treated like PLUS.
3749 Likewise, handle (X<<C3) and X as legitimate variants of X*C. */
3750 (for op (bit_ior bit_xor)
3752 (op (mult:s@0 @1 INTEGER_CST@2)
3753 (mult:s@3 @1 INTEGER_CST@4))
3754 (if (INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_WRAPS (type)
3755 && (tree_nonzero_bits (@0) & tree_nonzero_bits (@3)) == 0)
3757 { wide_int_to_tree (type, wi::to_wide (@2) + wi::to_wide (@4)); })))
3759 (op:c (mult:s@0 @1 INTEGER_CST@2)
3760 (lshift:s@3 @1 INTEGER_CST@4))
3761 (if (INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_WRAPS (type)
3762 && tree_int_cst_sgn (@4) > 0
3763 && (tree_nonzero_bits (@0) & tree_nonzero_bits (@3)) == 0)
3764 (with { wide_int wone = wi::one (TYPE_PRECISION (type));
3765 wide_int c = wi::add (wi::to_wide (@2),
3766 wi::lshift (wone, wi::to_wide (@4))); }
3767 (mult @1 { wide_int_to_tree (type, c); }))))
3769 (op:c (mult:s@0 @1 INTEGER_CST@2)
3771 (if (INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_WRAPS (type)
3772 && (tree_nonzero_bits (@0) & tree_nonzero_bits (@1)) == 0)
3774 { wide_int_to_tree (type,
3775 wi::add (wi::to_wide (@2), 1)); })))
3777 (op (lshift:s@0 @1 INTEGER_CST@2)
3778 (lshift:s@3 @1 INTEGER_CST@4))
3779 (if (INTEGRAL_TYPE_P (type)
3780 && tree_int_cst_sgn (@2) > 0
3781 && tree_int_cst_sgn (@4) > 0
3782 && (tree_nonzero_bits (@0) & tree_nonzero_bits (@3)) == 0)
3783 (with { tree t = type;
3784 if (!TYPE_OVERFLOW_WRAPS (t))
3785 t = unsigned_type_for (t);
3786 wide_int wone = wi::one (TYPE_PRECISION (t));
3787 wide_int c = wi::add (wi::lshift (wone, wi::to_wide (@2)),
3788 wi::lshift (wone, wi::to_wide (@4))); }
3789 (convert (mult:t (convert:t @1) { wide_int_to_tree (t,c); })))))
3791 (op:c (lshift:s@0 @1 INTEGER_CST@2)
3793 (if (INTEGRAL_TYPE_P (type)
3794 && tree_int_cst_sgn (@2) > 0
3795 && (tree_nonzero_bits (@0) & tree_nonzero_bits (@1)) == 0)
3796 (with { tree t = type;
3797 if (!TYPE_OVERFLOW_WRAPS (t))
3798 t = unsigned_type_for (t);
3799 wide_int wone = wi::one (TYPE_PRECISION (t));
3800 wide_int c = wi::add (wi::lshift (wone, wi::to_wide (@2)), wone); }
3801 (convert (mult:t (convert:t @1) { wide_int_to_tree (t, c); }))))))
3803 /* Simplifications of MIN_EXPR, MAX_EXPR, fmin() and fmax(). */
3805 (for minmax (min max)
3809 /* max(max(x,y),x) -> max(x,y) */
3811 (minmax:c (minmax:c@2 @0 @1) @0)
3813 /* For fmin() and fmax(), skip folding when both are sNaN. */
3814 (for minmax (FMIN_ALL FMAX_ALL)
3817 (if (!tree_expr_maybe_signaling_nan_p (@0))
3819 /* min(max(x,y),y) -> y. */
3821 (min:c (max:c @0 @1) @1)
3823 /* max(min(x,y),y) -> y. */
3825 (max:c (min:c @0 @1) @1)
3827 /* max(a,-a) -> abs(a). */
3829 (max:c @0 (negate @0))
3830 (if (TREE_CODE (type) != COMPLEX_TYPE
3831 && (! ANY_INTEGRAL_TYPE_P (type)
3832 || TYPE_OVERFLOW_UNDEFINED (type)))
3834 /* min(a,-a) -> -abs(a). */
3836 (min:c @0 (negate @0))
3837 (if (TREE_CODE (type) != COMPLEX_TYPE
3838 && (! ANY_INTEGRAL_TYPE_P (type)
3839 || TYPE_OVERFLOW_UNDEFINED (type)))
3844 (if (INTEGRAL_TYPE_P (type)
3845 && TYPE_MIN_VALUE (type)
3846 && operand_equal_p (@1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
3848 (if (INTEGRAL_TYPE_P (type)
3849 && TYPE_MAX_VALUE (type)
3850 && operand_equal_p (@1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
3855 (if (INTEGRAL_TYPE_P (type)
3856 && TYPE_MAX_VALUE (type)
3857 && operand_equal_p (@1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
3859 (if (INTEGRAL_TYPE_P (type)
3860 && TYPE_MIN_VALUE (type)
3861 && operand_equal_p (@1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
3864 /* max (a, a + CST) -> a + CST where CST is positive. */
3865 /* max (a, a + CST) -> a where CST is negative. */
3867 (max:c @0 (plus@2 @0 INTEGER_CST@1))
3868 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
3869 (if (tree_int_cst_sgn (@1) > 0)
3873 /* min (a, a + CST) -> a where CST is positive. */
3874 /* min (a, a + CST) -> a + CST where CST is negative. */
3876 (min:c @0 (plus@2 @0 INTEGER_CST@1))
3877 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
3878 (if (tree_int_cst_sgn (@1) > 0)
3882 /* Simplify min (&var[off0], &var[off1]) etc. depending on whether
3883 the addresses are known to be less, equal or greater. */
3884 (for minmax (min max)
3887 (minmax (convert1?@2 addr@0) (convert2?@3 addr@1))
3890 poly_int64 off0, off1;
3892 int equal = address_compare (cmp, TREE_TYPE (@2), @0, @1, base0, base1,
3893 off0, off1, GENERIC);
3896 (if (minmax == MIN_EXPR)
3897 (if (known_le (off0, off1))
3899 (if (known_gt (off0, off1))
3901 (if (known_ge (off0, off1))
3903 (if (known_lt (off0, off1))
3906 /* (convert (minmax ((convert (x) c)))) -> minmax (x c) if x is promoted
3907 and the outer convert demotes the expression back to x's type. */
3908 (for minmax (min max)
3910 (convert (minmax@0 (convert @1) INTEGER_CST@2))
3911 (if (INTEGRAL_TYPE_P (type)
3912 && types_match (@1, type) && int_fits_type_p (@2, type)
3913 && TYPE_SIGN (TREE_TYPE (@0)) == TYPE_SIGN (type)
3914 && TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type))
3915 (minmax @1 (convert @2)))))
3917 (for minmax (FMIN_ALL FMAX_ALL)
3918 /* If either argument is NaN and other one is not sNaN, return the other
3919 one. Avoid the transformation if we get (and honor) a signalling NaN. */
3921 (minmax:c @0 REAL_CST@1)
3922 (if (real_isnan (TREE_REAL_CST_PTR (@1))
3923 && (!HONOR_SNANS (@1) || !TREE_REAL_CST (@1).signalling)
3924 && !tree_expr_maybe_signaling_nan_p (@0))
3926 /* Convert fmin/fmax to MIN_EXPR/MAX_EXPR. C99 requires these
3927 functions to return the numeric arg if the other one is NaN.
3928 MIN and MAX don't honor that, so only transform if -ffinite-math-only
3929 is set. C99 doesn't require -0.0 to be handled, so we don't have to
3930 worry about it either. */
3931 (if (flag_finite_math_only)
3938 /* min (-A, -B) -> -max (A, B) */
3939 (for minmax (min max FMIN_ALL FMAX_ALL)
3940 maxmin (max min FMAX_ALL FMIN_ALL)
3942 (minmax (negate:s@2 @0) (negate:s@3 @1))
3943 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
3944 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3945 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
3946 (negate (maxmin @0 @1)))))
3947 /* MIN (~X, ~Y) -> ~MAX (X, Y)
3948 MAX (~X, ~Y) -> ~MIN (X, Y) */
3949 (for minmax (min max)
3952 (minmax (bit_not:s@2 @0) (bit_not:s@3 @1))
3953 (bit_not (maxmin @0 @1)))
3954 /* ~MAX(~X, Y) --> MIN(X, ~Y) */
3955 /* ~MIN(~X, Y) --> MAX(X, ~Y) */
3957 (bit_not (minmax:cs (bit_not @0) @1))
3958 (maxmin @0 (bit_not @1))))
3960 /* MIN (X, Y) == X -> X <= Y */
3961 /* MIN (X, Y) < X -> X > Y */
3962 /* MIN (X, Y) >= X -> X <= Y */
3963 (for minmax (min min min min max max max max)
3964 cmp (eq ne lt ge eq ne gt le )
3965 out (le gt gt le ge lt lt ge )
3967 (cmp:c (minmax:c @0 @1) @0)
3968 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0)))
3970 /* MIN (X, 5) == 0 -> X == 0
3971 MIN (X, 5) == 7 -> false */
3974 (cmp (min @0 INTEGER_CST@1) INTEGER_CST@2)
3975 (if (wi::lt_p (wi::to_wide (@1), wi::to_wide (@2),
3976 TYPE_SIGN (TREE_TYPE (@0))))
3977 { constant_boolean_node (cmp == NE_EXPR, type); }
3978 (if (wi::gt_p (wi::to_wide (@1), wi::to_wide (@2),
3979 TYPE_SIGN (TREE_TYPE (@0))))
3983 (cmp (max @0 INTEGER_CST@1) INTEGER_CST@2)
3984 (if (wi::gt_p (wi::to_wide (@1), wi::to_wide (@2),
3985 TYPE_SIGN (TREE_TYPE (@0))))
3986 { constant_boolean_node (cmp == NE_EXPR, type); }
3987 (if (wi::lt_p (wi::to_wide (@1), wi::to_wide (@2),
3988 TYPE_SIGN (TREE_TYPE (@0))))
3991 /* X <= MAX(X, Y) -> true
3992 X > MAX(X, Y) -> false
3993 X >= MIN(X, Y) -> true
3994 X < MIN(X, Y) -> false */
3995 (for minmax (min min max max )
3998 (cmp:c @0 (minmax:c @0 @1))
3999 { constant_boolean_node (cmp == GE_EXPR || cmp == LE_EXPR, type); } ))
4001 /* MIN (X, C1) < C2 -> X < C2 || C1 < C2 */
4002 (for minmax (min min max max min min max max )
4003 cmp (lt le gt ge gt ge lt le )
4004 comb (bit_ior bit_ior bit_ior bit_ior bit_and bit_and bit_and bit_and)
4006 (cmp (minmax @0 INTEGER_CST@1) INTEGER_CST@2)
4007 (comb (cmp @0 @2) (cmp @1 @2))))
4009 /* Undo fancy ways of writing max/min or other ?: expressions, like
4010 a - ((a - b) & -(a < b)) and a - (a - b) * (a < b) into (a < b) ? b : a.
4011 People normally use ?: and that is what we actually try to optimize. */
4012 /* Transform A + (B-A)*cmp into cmp ? B : A. */
4014 (plus:c @0 (mult:c (minus @1 @0) zero_one_valued_p@2))
4015 (if (INTEGRAL_TYPE_P (type)
4016 && (GIMPLE || !TREE_SIDE_EFFECTS (@1)))
4017 (cond (convert:boolean_type_node @2) @1 @0)))
4018 /* Transform A - (A-B)*cmp into cmp ? B : A. */
4020 (minus @0 (mult:c (minus @0 @1) zero_one_valued_p@2))
4021 (if (INTEGRAL_TYPE_P (type)
4022 && (GIMPLE || !TREE_SIDE_EFFECTS (@1)))
4023 (cond (convert:boolean_type_node @2) @1 @0)))
4024 /* Transform A ^ (A^B)*cmp into cmp ? B : A. */
4026 (bit_xor:c @0 (mult:c (bit_xor:c @0 @1) zero_one_valued_p@2))
4027 (if (INTEGRAL_TYPE_P (type)
4028 && (GIMPLE || !TREE_SIDE_EFFECTS (@1)))
4029 (cond (convert:boolean_type_node @2) @1 @0)))
4031 /* (x <= 0 ? -x : 0) -> max(-x, 0). */
4033 (cond (le @0 integer_zerop@1) (negate@2 @0) integer_zerop@1)
4036 /* (zero_one == 0) ? y : z <op> y -> ((typeof(y))zero_one * z) <op> y */
4037 (for op (bit_xor bit_ior plus)
4039 (cond (eq zero_one_valued_p@0
4043 (if (INTEGRAL_TYPE_P (type)
4044 && TYPE_PRECISION (type) > 1
4045 && (INTEGRAL_TYPE_P (TREE_TYPE (@0))))
4046 (op (mult (convert:type @0) @2) @1))))
4048 /* (zero_one != 0) ? z <op> y : y -> ((typeof(y))zero_one * z) <op> y */
4049 (for op (bit_xor bit_ior plus)
4051 (cond (ne zero_one_valued_p@0
4055 (if (INTEGRAL_TYPE_P (type)
4056 && TYPE_PRECISION (type) > 1
4057 && (INTEGRAL_TYPE_P (TREE_TYPE (@0))))
4058 (op (mult (convert:type @0) @2) @1))))
4060 /* Simplifications of shift and rotates. */
4062 (for rotate (lrotate rrotate)
4064 (rotate integer_all_onesp@0 @1)
4067 /* Optimize -1 >> x for arithmetic right shifts. */
4069 (rshift integer_all_onesp@0 @1)
4070 (if (!TYPE_UNSIGNED (type))
4073 /* Optimize (x >> c) << c into x & (-1<<c). */
4075 (lshift (nop_convert? (rshift @0 INTEGER_CST@1)) @1)
4076 (if (wi::ltu_p (wi::to_wide (@1), element_precision (type)))
4077 /* It doesn't matter if the right shift is arithmetic or logical. */
4078 (bit_and (view_convert @0) (lshift { build_minus_one_cst (type); } @1))))
4081 (lshift (convert (convert@2 (rshift @0 INTEGER_CST@1))) @1)
4082 (if (wi::ltu_p (wi::to_wide (@1), element_precision (type))
4083 /* Allow intermediate conversion to integral type with whatever sign, as
4084 long as the low TYPE_PRECISION (type)
4085 - TYPE_PRECISION (TREE_TYPE (@2)) bits are preserved. */
4086 && INTEGRAL_TYPE_P (type)
4087 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
4088 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
4089 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (@0))
4090 && (TYPE_PRECISION (TREE_TYPE (@2)) >= TYPE_PRECISION (type)
4091 || wi::geu_p (wi::to_wide (@1),
4092 TYPE_PRECISION (type)
4093 - TYPE_PRECISION (TREE_TYPE (@2)))))
4094 (bit_and (convert @0) (lshift { build_minus_one_cst (type); } @1))))
4096 /* For (x << c) >> c, optimize into x & ((unsigned)-1 >> c) for
4097 unsigned x OR truncate into the precision(type) - c lowest bits
4098 of signed x (if they have mode precision or a precision of 1). */
4100 (rshift (nop_convert? (lshift @0 INTEGER_CST@1)) @@1)
4101 (if (wi::ltu_p (wi::to_wide (@1), element_precision (type)))
4102 (if (TYPE_UNSIGNED (type))
4103 (bit_and (convert @0) (rshift { build_minus_one_cst (type); } @1))
4104 (if (INTEGRAL_TYPE_P (type))
4106 int width = element_precision (type) - tree_to_uhwi (@1);
4107 tree stype = build_nonstandard_integer_type (width, 0);
4109 (if (width == 1 || type_has_mode_precision_p (stype))
4110 (convert (convert:stype @0))))))))
4112 /* Optimize x >> x into 0 */
4115 { build_zero_cst (type); })
4117 (for shiftrotate (lrotate rrotate lshift rshift)
4119 (shiftrotate @0 integer_zerop)
4122 (shiftrotate integer_zerop@0 @1)
4124 /* Prefer vector1 << scalar to vector1 << vector2
4125 if vector2 is uniform. */
4126 (for vec (VECTOR_CST CONSTRUCTOR)
4128 (shiftrotate @0 vec@1)
4129 (with { tree tem = uniform_vector_p (@1); }
4131 (shiftrotate @0 { tem; }))))))
4133 /* Simplify X << Y where Y's low width bits are 0 to X, as only valid
4134 Y is 0. Similarly for X >> Y. */
4136 (for shift (lshift rshift)
4138 (shift @0 SSA_NAME@1)
4139 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1)))
4141 int width = ceil_log2 (element_precision (TREE_TYPE (@0)));
4142 int prec = TYPE_PRECISION (TREE_TYPE (@1));
4144 (if ((get_nonzero_bits (@1) & wi::mask (width, false, prec)) == 0)
4148 /* Rewrite an LROTATE_EXPR by a constant into an
4149 RROTATE_EXPR by a new constant. */
4151 (lrotate @0 INTEGER_CST@1)
4152 (rrotate @0 { const_binop (MINUS_EXPR, TREE_TYPE (@1),
4153 build_int_cst (TREE_TYPE (@1),
4154 element_precision (type)), @1); }))
4156 /* Turn (a OP c1) OP c2 into a OP (c1+c2). */
4157 (for op (lrotate rrotate rshift lshift)
4159 (op (op @0 INTEGER_CST@1) INTEGER_CST@2)
4160 (with { unsigned int prec = element_precision (type); }
4161 (if (wi::ge_p (wi::to_wide (@1), 0, TYPE_SIGN (TREE_TYPE (@1)))
4162 && wi::lt_p (wi::to_wide (@1), prec, TYPE_SIGN (TREE_TYPE (@1)))
4163 && wi::ge_p (wi::to_wide (@2), 0, TYPE_SIGN (TREE_TYPE (@2)))
4164 && wi::lt_p (wi::to_wide (@2), prec, TYPE_SIGN (TREE_TYPE (@2))))
4165 (with { unsigned int low = (tree_to_uhwi (@1)
4166 + tree_to_uhwi (@2)); }
4167 /* Deal with a OP (c1 + c2) being undefined but (a OP c1) OP c2
4168 being well defined. */
4170 (if (op == LROTATE_EXPR || op == RROTATE_EXPR)
4171 (op @0 { build_int_cst (TREE_TYPE (@1), low % prec); })
4172 (if (TYPE_UNSIGNED (type) || op == LSHIFT_EXPR)
4173 { build_zero_cst (type); }
4174 (op @0 { build_int_cst (TREE_TYPE (@1), prec - 1); })))
4175 (op @0 { build_int_cst (TREE_TYPE (@1), low); })))))))
4178 /* Simplify (CST << x) & 1 to 0 if CST is even or to x == 0 if it is odd. */
4180 (bit_and (lshift INTEGER_CST@1 @0) integer_onep)
4181 (if ((wi::to_wide (@1) & 1) != 0)
4182 (convert (eq:boolean_type_node @0 { build_zero_cst (TREE_TYPE (@0)); }))
4183 { build_zero_cst (type); }))
4185 /* Simplify ((C << x) & D) != 0 where C and D are power of two constants,
4186 either to false if D is smaller (unsigned comparison) than C, or to
4187 x == log2 (D) - log2 (C). Similarly for right shifts.
4188 Note for `(1 >> x)`, the & 1 has been removed so matching that seperately. */
4192 (cmp (bit_and (lshift integer_pow2p@1 @0) integer_pow2p@2) integer_zerop)
4193 (with { int c1 = wi::clz (wi::to_wide (@1));
4194 int c2 = wi::clz (wi::to_wide (@2)); }
4196 { constant_boolean_node (cmp == NE_EXPR ? false : true, type); }
4197 (icmp @0 { build_int_cst (TREE_TYPE (@0), c1 - c2); }))))
4199 (cmp (bit_and (rshift integer_pow2p@1 @0) integer_pow2p@2) integer_zerop)
4200 (if (tree_int_cst_sgn (@1) > 0)
4201 (with { int c1 = wi::clz (wi::to_wide (@1));
4202 int c2 = wi::clz (wi::to_wide (@2)); }
4204 { constant_boolean_node (cmp == NE_EXPR ? false : true, type); }
4205 (icmp @0 { build_int_cst (TREE_TYPE (@0), c2 - c1); })))))
4206 /* `(1 >> X) != 0` -> `X == 0` */
4207 /* `(1 >> X) == 0` -> `X != 0` */
4209 (cmp (rshift integer_onep @0) integer_zerop)
4210 (icmp @0 { build_zero_cst (TREE_TYPE (@0)); })))
4212 /* (CST1 << A) == CST2 -> A == ctz (CST2) - ctz (CST1)
4213 (CST1 << A) != CST2 -> A != ctz (CST2) - ctz (CST1)
4217 (cmp (lshift INTEGER_CST@0 @1) INTEGER_CST@2)
4218 (with { int cand = wi::ctz (wi::to_wide (@2)) - wi::ctz (wi::to_wide (@0)); }
4220 || (!integer_zerop (@2)
4221 && wi::lshift (wi::to_wide (@0), cand) != wi::to_wide (@2)))
4222 { constant_boolean_node (cmp == NE_EXPR, type); }
4223 (if (!integer_zerop (@2)
4224 && wi::lshift (wi::to_wide (@0), cand) == wi::to_wide (@2))
4225 (cmp @1 { build_int_cst (TREE_TYPE (@1), cand); }))))))
4227 /* Fold ((X << C1) & C2) cmp C3 into (X & (C2 >> C1)) cmp (C3 >> C1)
4228 ((X >> C1) & C2) cmp C3 into (X & (C2 << C1)) cmp (C3 << C1). */
4231 (cmp (bit_and:s (lshift:s @0 INTEGER_CST@1) INTEGER_CST@2) INTEGER_CST@3)
4232 (if (tree_fits_shwi_p (@1)
4233 && tree_to_shwi (@1) > 0
4234 && tree_to_shwi (@1) < TYPE_PRECISION (TREE_TYPE (@0)))
4235 (if (tree_to_shwi (@1) > wi::ctz (wi::to_wide (@3)))
4236 { constant_boolean_node (cmp == NE_EXPR, type); }
4237 (with { wide_int c1 = wi::to_wide (@1);
4238 wide_int c2 = wi::lrshift (wi::to_wide (@2), c1);
4239 wide_int c3 = wi::lrshift (wi::to_wide (@3), c1); }
4240 (cmp (bit_and @0 { wide_int_to_tree (TREE_TYPE (@0), c2); })
4241 { wide_int_to_tree (TREE_TYPE (@0), c3); })))))
4243 (cmp (bit_and:s (rshift:s @0 INTEGER_CST@1) INTEGER_CST@2) INTEGER_CST@3)
4244 (if (tree_fits_shwi_p (@1)
4245 && tree_to_shwi (@1) > 0
4246 && tree_to_shwi (@1) < TYPE_PRECISION (TREE_TYPE (@0)))
4247 (with { tree t0 = TREE_TYPE (@0);
4248 unsigned int prec = TYPE_PRECISION (t0);
4249 wide_int c1 = wi::to_wide (@1);
4250 wide_int c2 = wi::to_wide (@2);
4251 wide_int c3 = wi::to_wide (@3);
4252 wide_int sb = wi::set_bit_in_zero (prec - 1, prec); }
4253 (if ((c2 & c3) != c3)
4254 { constant_boolean_node (cmp == NE_EXPR, type); }
4255 (if (TYPE_UNSIGNED (t0))
4256 (if ((c3 & wi::arshift (sb, c1 - 1)) != 0)
4257 { constant_boolean_node (cmp == NE_EXPR, type); }
4258 (cmp (bit_and @0 { wide_int_to_tree (t0, c2 << c1); })
4259 { wide_int_to_tree (t0, c3 << c1); }))
4260 (with { wide_int smask = wi::arshift (sb, c1); }
4262 (if ((c2 & smask) == 0)
4263 (cmp (bit_and @0 { wide_int_to_tree (t0, c2 << c1); })
4264 { wide_int_to_tree (t0, c3 << c1); }))
4265 (if ((c3 & smask) == 0)
4266 (cmp (bit_and @0 { wide_int_to_tree (t0, (c2 << c1) | sb); })
4267 { wide_int_to_tree (t0, c3 << c1); }))
4268 (if ((c2 & smask) != (c3 & smask))
4269 { constant_boolean_node (cmp == NE_EXPR, type); })
4270 (cmp (bit_and @0 { wide_int_to_tree (t0, (c2 << c1) | sb); })
4271 { wide_int_to_tree (t0, (c3 << c1) | sb); })))))))))
4273 /* Fold (X << C1) & C2 into (X << C1) & (C2 | ((1 << C1) - 1))
4274 (X >> C1) & C2 into (X >> C1) & (C2 | ~((type) -1 >> C1))
4275 if the new mask might be further optimized. */
4276 (for shift (lshift rshift)
4278 (bit_and (convert?:s@4 (shift:s@5 (convert1?@3 @0) INTEGER_CST@1))
4280 (if (tree_nop_conversion_p (TREE_TYPE (@4), TREE_TYPE (@5))
4281 && TYPE_PRECISION (type) <= HOST_BITS_PER_WIDE_INT
4282 && tree_fits_uhwi_p (@1)
4283 && tree_to_uhwi (@1) > 0
4284 && tree_to_uhwi (@1) < TYPE_PRECISION (type))
4287 unsigned int shiftc = tree_to_uhwi (@1);
4288 unsigned HOST_WIDE_INT mask = TREE_INT_CST_LOW (@2);
4289 unsigned HOST_WIDE_INT newmask, zerobits = 0;
4290 tree shift_type = TREE_TYPE (@3);
4293 if (shift == LSHIFT_EXPR)
4294 zerobits = ((HOST_WIDE_INT_1U << shiftc) - 1);
4295 else if (shift == RSHIFT_EXPR
4296 && type_has_mode_precision_p (shift_type))
4298 prec = TYPE_PRECISION (TREE_TYPE (@3));
4300 /* See if more bits can be proven as zero because of
4303 && TYPE_UNSIGNED (TREE_TYPE (@0)))
4305 tree inner_type = TREE_TYPE (@0);
4306 if (type_has_mode_precision_p (inner_type)
4307 && TYPE_PRECISION (inner_type) < prec)
4309 prec = TYPE_PRECISION (inner_type);
4310 /* See if we can shorten the right shift. */
4312 shift_type = inner_type;
4313 /* Otherwise X >> C1 is all zeros, so we'll optimize
4314 it into (X, 0) later on by making sure zerobits
4318 zerobits = HOST_WIDE_INT_M1U;
4321 zerobits >>= HOST_BITS_PER_WIDE_INT - shiftc;
4322 zerobits <<= prec - shiftc;
4324 /* For arithmetic shift if sign bit could be set, zerobits
4325 can contain actually sign bits, so no transformation is
4326 possible, unless MASK masks them all away. In that
4327 case the shift needs to be converted into logical shift. */
4328 if (!TYPE_UNSIGNED (TREE_TYPE (@3))
4329 && prec == TYPE_PRECISION (TREE_TYPE (@3)))
4331 if ((mask & zerobits) == 0)
4332 shift_type = unsigned_type_for (TREE_TYPE (@3));
4338 /* ((X << 16) & 0xff00) is (X, 0). */
4339 (if ((mask & zerobits) == mask)
4340 { build_int_cst (type, 0); }
4341 (with { newmask = mask | zerobits; }
4342 (if (newmask != mask && (newmask & (newmask + 1)) == 0)
4345 /* Only do the transformation if NEWMASK is some integer
4347 for (prec = BITS_PER_UNIT;
4348 prec < HOST_BITS_PER_WIDE_INT; prec <<= 1)
4349 if (newmask == (HOST_WIDE_INT_1U << prec) - 1)
4352 (if (prec < HOST_BITS_PER_WIDE_INT
4353 || newmask == HOST_WIDE_INT_M1U)
4355 { tree newmaskt = build_int_cst_type (TREE_TYPE (@2), newmask); }
4356 (if (!tree_int_cst_equal (newmaskt, @2))
4357 (if (shift_type != TREE_TYPE (@3))
4358 (bit_and (convert (shift:shift_type (convert @3) @1)) { newmaskt; })
4359 (bit_and @4 { newmaskt; })))))))))))))
4361 /* ((1 << n) & M) != 0 -> n == log2 (M) */
4367 (nop_convert? (lshift integer_onep @0)) integer_pow2p@1) integer_zerop)
4368 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)))
4369 (icmp @0 { wide_int_to_tree (TREE_TYPE (@0),
4370 wi::exact_log2 (wi::to_wide (@1))); }))))
4372 /* Fold (X {&,^,|} C2) << C1 into (X << C1) {&,^,|} (C2 << C1)
4373 (X {&,^,|} C2) >> C1 into (X >> C1) & (C2 >> C1). */
4374 (for shift (lshift rshift)
4375 (for bit_op (bit_and bit_xor bit_ior)
4377 (shift (convert?:s (bit_op:s @0 INTEGER_CST@2)) INTEGER_CST@1)
4378 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
4379 (with { tree mask = int_const_binop (shift, fold_convert (type, @2), @1); }
4381 (bit_op (shift (convert @0) @1) { mask; })))))))
4383 /* ~(~X >> Y) -> X >> Y (for arithmetic shift). */
4385 (bit_not (convert1?:s (rshift:s (convert2?@0 (bit_not @1)) @2)))
4386 (if (!TYPE_UNSIGNED (TREE_TYPE (@0))
4387 && (element_precision (TREE_TYPE (@0))
4388 <= element_precision (TREE_TYPE (@1))
4389 || !TYPE_UNSIGNED (TREE_TYPE (@1))))
4391 { tree shift_type = TREE_TYPE (@0); }
4392 (convert (rshift (convert:shift_type @1) @2)))))
4394 /* ~(~X >>r Y) -> X >>r Y
4395 ~(~X <<r Y) -> X <<r Y */
4396 (for rotate (lrotate rrotate)
4398 (bit_not (convert1?:s (rotate:s (convert2?@0 (bit_not @1)) @2)))
4399 (if ((element_precision (TREE_TYPE (@0))
4400 <= element_precision (TREE_TYPE (@1))
4401 || !TYPE_UNSIGNED (TREE_TYPE (@1)))
4402 && (element_precision (type) <= element_precision (TREE_TYPE (@0))
4403 || !TYPE_UNSIGNED (TREE_TYPE (@0))))
4405 { tree rotate_type = TREE_TYPE (@0); }
4406 (convert (rotate (convert:rotate_type @1) @2))))))
4409 (for rotate (lrotate rrotate)
4410 invrot (rrotate lrotate)
4411 /* (X >>r Y) cmp (Z >>r Y) may simplify to X cmp Y. */
4413 (cmp (rotate @1 @0) (rotate @2 @0))
4415 /* (X >>r C1) cmp C2 may simplify to X cmp C3. */
4417 (cmp (rotate @0 INTEGER_CST@1) INTEGER_CST@2)
4418 (cmp @0 { const_binop (invrot, TREE_TYPE (@0), @2, @1); }))
4419 /* (X >>r Y) cmp C where C is 0 or ~0, may simplify to X cmp C. */
4421 (cmp (rotate @0 @1) INTEGER_CST@2)
4422 (if (integer_zerop (@2) || integer_all_onesp (@2))
4425 /* Narrow a lshift by constant. */
4427 (convert (lshift:s@0 @1 INTEGER_CST@2))
4428 (if (INTEGRAL_TYPE_P (type)
4429 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
4430 && !integer_zerop (@2)
4431 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0)))
4432 (if (TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (@0))
4433 || wi::ltu_p (wi::to_wide (@2), TYPE_PRECISION (type)))
4434 (lshift (convert @1) @2)
4435 (if (wi::ltu_p (wi::to_wide (@2), TYPE_PRECISION (TREE_TYPE (@0))))
4436 { build_zero_cst (type); }))))
4438 /* Simplifications of conversions. */
4440 /* Basic strip-useless-type-conversions / strip_nops. */
4441 (for cvt (convert view_convert float fix_trunc)
4444 (if ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@0)))
4445 || (GENERIC && type == TREE_TYPE (@0)))
4448 /* Contract view-conversions. */
4450 (view_convert (view_convert @0))
4453 /* For integral conversions with the same precision or pointer
4454 conversions use a NOP_EXPR instead. */
4457 (if ((INTEGRAL_TYPE_P (type) || POINTER_TYPE_P (type))
4458 && (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
4459 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (@0)))
4462 /* Strip inner integral conversions that do not change precision or size, or
4463 zero-extend while keeping the same size (for bool-to-char). */
4465 (view_convert (convert@0 @1))
4466 (if ((INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
4467 && (INTEGRAL_TYPE_P (TREE_TYPE (@1)) || POINTER_TYPE_P (TREE_TYPE (@1)))
4468 && TYPE_SIZE (TREE_TYPE (@0)) == TYPE_SIZE (TREE_TYPE (@1))
4469 && (TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1))
4470 || (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (TREE_TYPE (@1))
4471 && TYPE_UNSIGNED (TREE_TYPE (@1)))))
4474 /* Simplify a view-converted empty or single-element constructor. */
4476 (view_convert CONSTRUCTOR@0)
4478 { tree ctor = (TREE_CODE (@0) == SSA_NAME
4479 ? gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0)) : @0); }
4481 (if (CONSTRUCTOR_NELTS (ctor) == 0)
4482 { build_zero_cst (type); })
4483 (if (CONSTRUCTOR_NELTS (ctor) == 1
4484 && VECTOR_TYPE_P (TREE_TYPE (ctor))
4485 && operand_equal_p (TYPE_SIZE (type),
4486 TYPE_SIZE (TREE_TYPE
4487 (CONSTRUCTOR_ELT (ctor, 0)->value))))
4488 (view_convert { CONSTRUCTOR_ELT (ctor, 0)->value; })))))
4490 /* Re-association barriers around constants and other re-association
4491 barriers can be removed. */
4493 (paren CONSTANT_CLASS_P@0)
4496 (paren (paren@1 @0))
4499 /* Handle cases of two conversions in a row. */
4500 (for ocvt (convert float fix_trunc)
4501 (for icvt (convert float)
4506 tree inside_type = TREE_TYPE (@0);
4507 tree inter_type = TREE_TYPE (@1);
4508 int inside_int = INTEGRAL_TYPE_P (inside_type);
4509 int inside_ptr = POINTER_TYPE_P (inside_type);
4510 int inside_float = FLOAT_TYPE_P (inside_type);
4511 int inside_vec = VECTOR_TYPE_P (inside_type);
4512 unsigned int inside_prec = element_precision (inside_type);
4513 int inside_unsignedp = TYPE_UNSIGNED (inside_type);
4514 int inter_int = INTEGRAL_TYPE_P (inter_type);
4515 int inter_ptr = POINTER_TYPE_P (inter_type);
4516 int inter_float = FLOAT_TYPE_P (inter_type);
4517 int inter_vec = VECTOR_TYPE_P (inter_type);
4518 unsigned int inter_prec = element_precision (inter_type);
4519 int inter_unsignedp = TYPE_UNSIGNED (inter_type);
4520 int final_int = INTEGRAL_TYPE_P (type);
4521 int final_ptr = POINTER_TYPE_P (type);
4522 int final_float = FLOAT_TYPE_P (type);
4523 int final_vec = VECTOR_TYPE_P (type);
4524 unsigned int final_prec = element_precision (type);
4525 int final_unsignedp = TYPE_UNSIGNED (type);
4528 /* In addition to the cases of two conversions in a row
4529 handled below, if we are converting something to its own
4530 type via an object of identical or wider precision, neither
4531 conversion is needed. */
4532 (if (((GIMPLE && useless_type_conversion_p (type, inside_type))
4534 && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (inside_type)))
4535 && (((inter_int || inter_ptr) && final_int)
4536 || (inter_float && final_float))
4537 && inter_prec >= final_prec)
4540 /* Likewise, if the intermediate and initial types are either both
4541 float or both integer, we don't need the middle conversion if the
4542 former is wider than the latter and doesn't change the signedness
4543 (for integers). Avoid this if the final type is a pointer since
4544 then we sometimes need the middle conversion. */
4545 (if (((inter_int && inside_int) || (inter_float && inside_float))
4546 && (final_int || final_float)
4547 && inter_prec >= inside_prec
4548 && (inter_float || inter_unsignedp == inside_unsignedp))
4551 /* If we have a sign-extension of a zero-extended value, we can
4552 replace that by a single zero-extension. Likewise if the
4553 final conversion does not change precision we can drop the
4554 intermediate conversion. */
4555 (if (inside_int && inter_int && final_int
4556 && ((inside_prec < inter_prec && inter_prec < final_prec
4557 && inside_unsignedp && !inter_unsignedp)
4558 || final_prec == inter_prec))
4561 /* Two conversions in a row are not needed unless:
4562 - some conversion is floating-point (overstrict for now), or
4563 - some conversion is a vector (overstrict for now), or
4564 - the intermediate type is narrower than both initial and
4566 - the intermediate type and innermost type differ in signedness,
4567 and the outermost type is wider than the intermediate, or
4568 - the initial type is a pointer type and the precisions of the
4569 intermediate and final types differ, or
4570 - the final type is a pointer type and the precisions of the
4571 initial and intermediate types differ. */
4572 (if (! inside_float && ! inter_float && ! final_float
4573 && ! inside_vec && ! inter_vec && ! final_vec
4574 && (inter_prec >= inside_prec || inter_prec >= final_prec)
4575 && ! (inside_int && inter_int
4576 && inter_unsignedp != inside_unsignedp
4577 && inter_prec < final_prec)
4578 && ((inter_unsignedp && inter_prec > inside_prec)
4579 == (final_unsignedp && final_prec > inter_prec))
4580 && ! (inside_ptr && inter_prec != final_prec)
4581 && ! (final_ptr && inside_prec != inter_prec))
4584 /* `(outer:M)(inter:N) a:O`
4585 can be converted to `(outer:M) a`
4586 if M <= O && N >= O. No matter what signedness of the casts,
4587 as the final is either a truncation from the original or just
4588 a sign change of the type. */
4589 (if (inside_int && inter_int && final_int
4590 && final_prec <= inside_prec
4591 && inter_prec >= inside_prec)
4594 /* A truncation to an unsigned type (a zero-extension) should be
4595 canonicalized as bitwise and of a mask. */
4596 (if (GIMPLE /* PR70366: doing this in GENERIC breaks -Wconversion. */
4597 && final_int && inter_int && inside_int
4598 && final_prec == inside_prec
4599 && final_prec > inter_prec
4601 (convert (bit_and @0 { wide_int_to_tree
4603 wi::mask (inter_prec, false,
4604 TYPE_PRECISION (inside_type))); })))
4606 /* If we are converting an integer to a floating-point that can
4607 represent it exactly and back to an integer, we can skip the
4608 floating-point conversion. */
4609 (if (GIMPLE /* PR66211 */
4610 && inside_int && inter_float && final_int &&
4611 (unsigned) significand_size (TYPE_MODE (inter_type))
4612 >= inside_prec - !inside_unsignedp)
4615 /* (float_type)(integer_type) x -> trunc (x) if the type of x matches
4616 float_type. Only do the transformation if we do not need to preserve
4617 trapping behaviour, so require !flag_trapping_math. */
4620 (float (fix_trunc @0))
4621 (if (!flag_trapping_math
4622 && types_match (type, TREE_TYPE (@0))
4623 && direct_internal_fn_supported_p (IFN_TRUNC, type,
4628 /* If we have a narrowing conversion to an integral type that is fed by a
4629 BIT_AND_EXPR, we might be able to remove the BIT_AND_EXPR if it merely
4630 masks off bits outside the final type (and nothing else). */
4632 (convert (bit_and @0 INTEGER_CST@1))
4633 (if (INTEGRAL_TYPE_P (type)
4634 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
4635 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))
4636 && operand_equal_p (@1, build_low_bits_mask (TREE_TYPE (@1),
4637 TYPE_PRECISION (type)), 0))
4641 /* (X /[ex] A) * A -> X. */
4643 (mult (convert1? (exact_div @0 @@1)) (convert2? @1))
4646 /* Simplify (A / B) * B + (A % B) -> A. */
4647 (for div (trunc_div ceil_div floor_div round_div)
4648 mod (trunc_mod ceil_mod floor_mod round_mod)
4650 (plus:c (mult:c (div @0 @1) @1) (mod @0 @1))
4653 /* x / y * y == x -> x % y == 0. */
4655 (eq:c (mult:c (trunc_div:s @0 @1) @1) @0)
4656 (if (TREE_CODE (TREE_TYPE (@0)) != COMPLEX_TYPE)
4657 (eq (trunc_mod @0 @1) { build_zero_cst (TREE_TYPE (@0)); })))
4659 /* ((X /[ex] A) +- B) * A --> X +- A * B. */
4660 (for op (plus minus)
4662 (mult (convert1? (op (convert2? (exact_div @0 INTEGER_CST@@1)) INTEGER_CST@2)) @1)
4663 (if (tree_nop_conversion_p (type, TREE_TYPE (@2))
4664 && tree_nop_conversion_p (TREE_TYPE (@0), TREE_TYPE (@2)))
4667 wi::overflow_type overflow;
4668 wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@2),
4669 TYPE_SIGN (type), &overflow);
4671 (if (types_match (type, TREE_TYPE (@2))
4672 && types_match (TREE_TYPE (@0), TREE_TYPE (@2)) && !overflow)
4673 (op @0 { wide_int_to_tree (type, mul); })
4674 (with { tree utype = unsigned_type_for (type); }
4675 (convert (op (convert:utype @0)
4676 (mult (convert:utype @1) (convert:utype @2))))))))))
4678 /* Canonicalization of binary operations. */
4680 /* Convert X + -C into X - C. */
4682 (plus @0 REAL_CST@1)
4683 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
4684 (with { tree tem = const_unop (NEGATE_EXPR, type, @1); }
4685 (if (!TREE_OVERFLOW (tem) || !flag_trapping_math)
4686 (minus @0 { tem; })))))
4688 /* Convert x+x into x*2. */
4691 (if (SCALAR_FLOAT_TYPE_P (type))
4692 (mult @0 { build_real (type, dconst2); })
4693 (if (INTEGRAL_TYPE_P (type))
4694 (mult @0 { build_int_cst (type, 2); }))))
4698 (minus integer_zerop @1)
4701 (pointer_diff integer_zerop @1)
4702 (negate (convert @1)))
4704 /* (ARG0 - ARG1) is the same as (-ARG1 + ARG0). So check whether
4705 ARG0 is zero and X + ARG0 reduces to X, since that would mean
4706 (-ARG1 + ARG0) reduces to -ARG1. */
4708 (minus real_zerop@0 @1)
4709 (if (fold_real_zero_addition_p (type, @1, @0, 0))
4712 /* Transform x * -1 into -x. */
4714 (mult @0 integer_minus_onep)
4717 /* Reassociate (X * CST) * Y to (X * Y) * CST. This does not introduce
4718 signed overflow for CST != 0 && CST != -1. */
4720 (mult:c (mult:s@3 @0 INTEGER_CST@1) @2)
4721 (if (TREE_CODE (@2) != INTEGER_CST
4723 && !integer_zerop (@1) && !integer_minus_onep (@1))
4724 (mult (mult @0 @2) @1)))
4726 /* True if we can easily extract the real and imaginary parts of a complex
4728 (match compositional_complex
4729 (convert? (complex @0 @1)))
4731 /* COMPLEX_EXPR and REALPART/IMAGPART_EXPR cancellations. */
4733 (complex (realpart @0) (imagpart @0))
4736 (realpart (complex @0 @1))
4739 (imagpart (complex @0 @1))
4742 /* Sometimes we only care about half of a complex expression. */
4744 (realpart (convert?:s (conj:s @0)))
4745 (convert (realpart @0)))
4747 (imagpart (convert?:s (conj:s @0)))
4748 (convert (negate (imagpart @0))))
4749 (for part (realpart imagpart)
4750 (for op (plus minus)
4752 (part (convert?:s@2 (op:s @0 @1)))
4753 (convert (op (part @0) (part @1))))))
4755 (realpart (convert?:s (CEXPI:s @0)))
4758 (imagpart (convert?:s (CEXPI:s @0)))
4761 /* conj(conj(x)) -> x */
4763 (conj (convert? (conj @0)))
4764 (if (tree_nop_conversion_p (TREE_TYPE (@0), type))
4767 /* conj({x,y}) -> {x,-y} */
4769 (conj (convert?:s (complex:s @0 @1)))
4770 (with { tree itype = TREE_TYPE (type); }
4771 (complex (convert:itype @0) (negate (convert:itype @1)))))
4773 /* BSWAP simplifications, transforms checked by gcc.dg/builtin-bswap-8.c. */
4779 (bswap (bit_not (bswap @0)))
4781 (for bitop (bit_xor bit_ior bit_and)
4783 (bswap (bitop:c (bswap @0) @1))
4784 (bitop @0 (bswap @1))))
4787 (cmp (bswap@2 @0) (bswap @1))
4788 (with { tree ctype = TREE_TYPE (@2); }
4789 (cmp (convert:ctype @0) (convert:ctype @1))))
4791 (cmp (bswap @0) INTEGER_CST@1)
4792 (with { tree ctype = TREE_TYPE (@1); }
4793 (cmp (convert:ctype @0) (bswap! @1)))))
4794 /* (bswap(x) >> C1) & C2 can sometimes be simplified to (x >> C3) & C2. */
4796 (bit_and (convert1? (rshift@0 (convert2? (bswap@4 @1)) INTEGER_CST@2))
4798 (if (BITS_PER_UNIT == 8
4799 && tree_fits_uhwi_p (@2)
4800 && tree_fits_uhwi_p (@3))
4803 unsigned HOST_WIDE_INT prec = TYPE_PRECISION (TREE_TYPE (@4));
4804 unsigned HOST_WIDE_INT bits = tree_to_uhwi (@2);
4805 unsigned HOST_WIDE_INT mask = tree_to_uhwi (@3);
4806 unsigned HOST_WIDE_INT lo = bits & 7;
4807 unsigned HOST_WIDE_INT hi = bits - lo;
4810 && mask < (256u>>lo)
4811 && bits < TYPE_PRECISION (TREE_TYPE(@0)))
4812 (with { unsigned HOST_WIDE_INT ns = (prec - (hi + 8)) + lo; }
4814 (bit_and (convert @1) @3)
4817 tree utype = unsigned_type_for (TREE_TYPE (@1));
4818 tree nst = build_int_cst (integer_type_node, ns);
4820 (bit_and (convert (rshift:utype (convert:utype @1) {nst;})) @3))))))))
4821 /* bswap(x) >> C1 can sometimes be simplified to (T)x >> C2. */
4823 (rshift (convert? (bswap@2 @0)) INTEGER_CST@1)
4824 (if (BITS_PER_UNIT == 8
4825 && CHAR_TYPE_SIZE == 8
4826 && tree_fits_uhwi_p (@1))
4829 unsigned HOST_WIDE_INT prec = TYPE_PRECISION (TREE_TYPE (@2));
4830 unsigned HOST_WIDE_INT bits = tree_to_uhwi (@1);
4831 /* If the bswap was extended before the original shift, this
4832 byte (shift) has the sign of the extension, not the sign of
4833 the original shift. */
4834 tree st = TYPE_PRECISION (type) > prec ? TREE_TYPE (@2) : type;
4836 /* Special case: logical right shift of sign-extended bswap.
4837 (unsigned)(short)bswap16(x)>>12 is (unsigned)((short)x<<8)>>12. */
4838 (if (TYPE_PRECISION (type) > prec
4839 && !TYPE_UNSIGNED (TREE_TYPE (@2))
4840 && TYPE_UNSIGNED (type)
4841 && bits < prec && bits + 8 >= prec)
4842 (with { tree nst = build_int_cst (integer_type_node, prec - 8); }
4843 (rshift (convert (lshift:st (convert:st @0) {nst;})) @1))
4844 (if (bits + 8 == prec)
4845 (if (TYPE_UNSIGNED (st))
4846 (convert (convert:unsigned_char_type_node @0))
4847 (convert (convert:signed_char_type_node @0)))
4848 (if (bits < prec && bits + 8 > prec)
4851 tree nst = build_int_cst (integer_type_node, bits & 7);
4852 tree bt = TYPE_UNSIGNED (st) ? unsigned_char_type_node
4853 : signed_char_type_node;
4855 (convert (rshift:bt (convert:bt @0) {nst;})))))))))
4856 /* bswap(x) & C1 can sometimes be simplified to (x >> C2) & C1. */
4858 (bit_and (convert? (bswap@2 @0)) INTEGER_CST@1)
4859 (if (BITS_PER_UNIT == 8
4860 && tree_fits_uhwi_p (@1)
4861 && tree_to_uhwi (@1) < 256)
4864 unsigned HOST_WIDE_INT prec = TYPE_PRECISION (TREE_TYPE (@2));
4865 tree utype = unsigned_type_for (TREE_TYPE (@0));
4866 tree nst = build_int_cst (integer_type_node, prec - 8);
4868 (bit_and (convert (rshift:utype (convert:utype @0) {nst;})) @1)))))
4871 /* Combine COND_EXPRs and VEC_COND_EXPRs. */
4873 /* Simplify constant conditions.
4874 Only optimize constant conditions when the selected branch
4875 has the same type as the COND_EXPR. This avoids optimizing
4876 away "c ? x : throw", where the throw has a void type.
4877 Note that we cannot throw away the fold-const.cc variant nor
4878 this one as we depend on doing this transform before possibly
4879 A ? B : B -> B triggers and the fold-const.cc one can optimize
4880 0 ? A : B to B even if A has side-effects. Something
4881 genmatch cannot handle. */
4883 (cond INTEGER_CST@0 @1 @2)
4884 (if (integer_zerop (@0))
4885 (if (!VOID_TYPE_P (TREE_TYPE (@2)) || VOID_TYPE_P (type))
4887 (if (!VOID_TYPE_P (TREE_TYPE (@1)) || VOID_TYPE_P (type))
4890 (vec_cond VECTOR_CST@0 @1 @2)
4891 (if (integer_all_onesp (@0))
4893 (if (integer_zerop (@0))
4896 /* Sink unary operations to branches, but only if we do fold both. */
4897 (for op (negate bit_not abs absu)
4899 (op (vec_cond:s @0 @1 @2))
4900 (vec_cond @0 (op! @1) (op! @2))))
4902 /* Sink unary conversions to branches, but only if we do fold both
4903 and the target's truth type is the same as we already have. */
4905 (convert (vec_cond:s @0 @1 @2))
4906 (if (VECTOR_TYPE_P (type)
4907 && types_match (TREE_TYPE (@0), truth_type_for (type)))
4908 (vec_cond @0 (convert! @1) (convert! @2))))
4910 /* Likewise for view_convert of nop_conversions. */
4912 (view_convert (vec_cond:s @0 @1 @2))
4913 (if (VECTOR_TYPE_P (type) && VECTOR_TYPE_P (TREE_TYPE (@1))
4914 && known_eq (TYPE_VECTOR_SUBPARTS (type),
4915 TYPE_VECTOR_SUBPARTS (TREE_TYPE (@1)))
4916 && tree_nop_conversion_p (TREE_TYPE (type), TREE_TYPE (TREE_TYPE (@1))))
4917 (vec_cond @0 (view_convert! @1) (view_convert! @2))))
4919 /* Sink binary operation to branches, but only if we can fold it. */
4920 (for op (tcc_comparison plus minus mult bit_and bit_ior bit_xor
4921 lshift rshift rdiv trunc_div ceil_div floor_div round_div
4922 trunc_mod ceil_mod floor_mod round_mod min max)
4923 /* (c ? a : b) op (c ? d : e) --> c ? (a op d) : (b op e) */
4925 (op (vec_cond:s @0 @1 @2) (vec_cond:s @0 @3 @4))
4926 (vec_cond @0 (op! @1 @3) (op! @2 @4)))
4928 /* (c ? a : b) op d --> c ? (a op d) : (b op d) */
4930 (op (vec_cond:s @0 @1 @2) @3)
4931 (vec_cond @0 (op! @1 @3) (op! @2 @3)))
4933 (op @3 (vec_cond:s @0 @1 @2))
4934 (vec_cond @0 (op! @3 @1) (op! @3 @2))))
4937 (match (nop_atomic_bit_test_and_p @0 @1 @4)
4938 (bit_and (convert?@4 (ATOMIC_FETCH_OR_XOR_N @2 INTEGER_CST@0 @3))
4941 int ibit = tree_log2 (@0);
4942 int ibit2 = tree_log2 (@1);
4946 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))))))
4948 (match (nop_atomic_bit_test_and_p @0 @1 @3)
4949 (bit_and (convert?@3 (SYNC_FETCH_OR_XOR_N @2 INTEGER_CST@0))
4952 int ibit = tree_log2 (@0);
4953 int ibit2 = tree_log2 (@1);
4957 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))))))
4959 (match (nop_atomic_bit_test_and_p @0 @0 @4)
4962 (ATOMIC_FETCH_OR_XOR_N @2 (nop_convert? (lshift@0 integer_onep@5 @6)) @3))
4964 (if (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0)))))
4966 (match (nop_atomic_bit_test_and_p @0 @0 @4)
4969 (SYNC_FETCH_OR_XOR_N @2 (nop_convert? (lshift@0 integer_onep@3 @5))))
4971 (if (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0)))))
4973 (match (nop_atomic_bit_test_and_p @0 @1 @3)
4974 (bit_and@4 (convert?@3 (ATOMIC_FETCH_AND_N @2 INTEGER_CST@0 @5))
4977 int ibit = wi::exact_log2 (wi::zext (wi::bit_not (wi::to_wide (@0)),
4978 TYPE_PRECISION(type)));
4979 int ibit2 = tree_log2 (@1);
4983 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))))))
4985 (match (nop_atomic_bit_test_and_p @0 @1 @3)
4987 (convert?@3 (SYNC_FETCH_AND_AND_N @2 INTEGER_CST@0))
4990 int ibit = wi::exact_log2 (wi::zext (wi::bit_not (wi::to_wide (@0)),
4991 TYPE_PRECISION(type)));
4992 int ibit2 = tree_log2 (@1);
4996 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))))))
4998 (match (nop_atomic_bit_test_and_p @4 @0 @3)
5001 (ATOMIC_FETCH_AND_N @2 (nop_convert?@4 (bit_not (lshift@0 integer_onep@6 @7))) @5))
5003 (if (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@4)))))
5005 (match (nop_atomic_bit_test_and_p @4 @0 @3)
5008 (SYNC_FETCH_AND_AND_N @2 (nop_convert?@4 (bit_not (lshift@0 integer_onep@6 @7)))))
5010 (if (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@4)))))
5014 /* (v ? w : 0) ? a : b is just (v & w) ? a : b
5015 Currently disabled after pass lvec because ARM understands
5016 VEC_COND_EXPR<v==w,-1,0> but not a plain v==w fed to BIT_IOR_EXPR. */
5018 (vec_cond (vec_cond:s @0 @3 integer_zerop) @1 @2)
5019 (if (optimize_vectors_before_lowering_p () && types_match (@0, @3))
5020 (vec_cond (bit_and @0 @3) @1 @2)))
5022 (vec_cond (vec_cond:s @0 integer_all_onesp @3) @1 @2)
5023 (if (optimize_vectors_before_lowering_p () && types_match (@0, @3))
5024 (vec_cond (bit_ior @0 @3) @1 @2)))
5026 (vec_cond (vec_cond:s @0 integer_zerop @3) @1 @2)
5027 (if (optimize_vectors_before_lowering_p () && types_match (@0, @3))
5028 (vec_cond (bit_ior @0 (bit_not @3)) @2 @1)))
5030 (vec_cond (vec_cond:s @0 @3 integer_all_onesp) @1 @2)
5031 (if (optimize_vectors_before_lowering_p () && types_match (@0, @3))
5032 (vec_cond (bit_and @0 (bit_not @3)) @2 @1)))
5034 /* c1 ? c2 ? a : b : b --> (c1 & c2) ? a : b */
5036 (vec_cond @0 (vec_cond:s @1 @2 @3) @3)
5037 (if (optimize_vectors_before_lowering_p () && types_match (@0, @1))
5038 (vec_cond (bit_and @0 @1) @2 @3)))
5040 (vec_cond @0 @2 (vec_cond:s @1 @2 @3))
5041 (if (optimize_vectors_before_lowering_p () && types_match (@0, @1))
5042 (vec_cond (bit_ior @0 @1) @2 @3)))
5044 (vec_cond @0 (vec_cond:s @1 @2 @3) @2)
5045 (if (optimize_vectors_before_lowering_p () && types_match (@0, @1))
5046 (vec_cond (bit_ior (bit_not @0) @1) @2 @3)))
5048 (vec_cond @0 @3 (vec_cond:s @1 @2 @3))
5049 (if (optimize_vectors_before_lowering_p () && types_match (@0, @1))
5050 (vec_cond (bit_and (bit_not @0) @1) @2 @3)))
5052 /* Canonicalize mask ? { 0, ... } : { -1, ...} to ~mask if the mask
5053 types are compatible. */
5055 (vec_cond @0 VECTOR_CST@1 VECTOR_CST@2)
5056 (if (VECTOR_BOOLEAN_TYPE_P (type)
5057 && types_match (type, TREE_TYPE (@0)))
5058 (if (integer_zerop (@1) && integer_all_onesp (@2))
5060 (if (integer_all_onesp (@1) && integer_zerop (@2))
5063 /* A few simplifications of "a ? CST1 : CST2". */
5064 /* NOTE: Only do this on gimple as the if-chain-to-switch
5065 optimization depends on the gimple to have if statements in it. */
5068 (cond @0 INTEGER_CST@1 INTEGER_CST@2)
5070 (if (integer_zerop (@2))
5072 /* a ? 1 : 0 -> a if 0 and 1 are integral types. */
5073 (if (integer_onep (@1))
5074 (convert (convert:boolean_type_node @0)))
5075 /* a ? powerof2cst : 0 -> a << (log2(powerof2cst)) */
5076 (if (INTEGRAL_TYPE_P (type) && integer_pow2p (@1))
5078 tree shift = build_int_cst (integer_type_node, tree_log2 (@1));
5080 (lshift (convert (convert:boolean_type_node @0)) { shift; })))
5081 /* a ? -1 : 0 -> -a. No need to check the TYPE_PRECISION not being 1
5082 here as the powerof2cst case above will handle that case correctly. */
5083 (if (INTEGRAL_TYPE_P (type) && integer_all_onesp (@1))
5085 auto prec = TYPE_PRECISION (type);
5086 auto unsign = TYPE_UNSIGNED (type);
5087 tree inttype = build_nonstandard_integer_type (prec, unsign);
5089 (convert (negate (convert:inttype (convert:boolean_type_node @0))))))))
5090 (if (integer_zerop (@1))
5092 tree booltrue = constant_boolean_node (true, boolean_type_node);
5095 /* a ? 0 : 1 -> !a. */
5096 (if (integer_onep (@2))
5097 (convert (bit_xor (convert:boolean_type_node @0) { booltrue; } )))
5098 /* a ? powerof2cst : 0 -> (!a) << (log2(powerof2cst)) */
5099 (if (INTEGRAL_TYPE_P (type) && integer_pow2p (@2))
5101 tree shift = build_int_cst (integer_type_node, tree_log2 (@2));
5103 (lshift (convert (bit_xor (convert:boolean_type_node @0) { booltrue; } ))
5105 /* a ? -1 : 0 -> -(!a). No need to check the TYPE_PRECISION not being 1
5106 here as the powerof2cst case above will handle that case correctly. */
5107 (if (INTEGRAL_TYPE_P (type) && integer_all_onesp (@2))
5109 auto prec = TYPE_PRECISION (type);
5110 auto unsign = TYPE_UNSIGNED (type);
5111 tree inttype = build_nonstandard_integer_type (prec, unsign);
5116 (bit_xor (convert:boolean_type_node @0) { booltrue; } )
5128 /* (a > 1) ? 0 : (cast)a is the same as (cast)(a == 1)
5129 for unsigned types. */
5131 (cond (gt @0 integer_onep@1) integer_zerop (convert? @2))
5132 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
5133 && bitwise_equal_p (@0, @2))
5134 (convert (eq @0 @1))
5138 /* (a <= 1) & (cast)a is the same as (cast)(a == 1)
5139 for unsigned types. */
5141 (bit_and:c (convert1? (le @0 integer_onep@1)) (convert2? @2))
5142 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
5143 && bitwise_equal_p (@0, @2))
5144 (convert (eq @0 @1))
5149 # x_5 in range [cst1, cst2] where cst2 = cst1 + 1
5150 x_5 ? cstN ? cst4 : cst3
5151 # op is == or != and N is 1 or 2
5152 to r_6 = x_5 + (min (cst3, cst4) - cst1) or
5153 r_6 = (min (cst3, cst4) + cst1) - x_5 depending on op, N and which
5154 of cst3 and cst4 is smaller.
5155 This was originally done by two_value_replacement in phiopt (PR 88676). */
5158 (cond (eqne SSA_NAME@0 INTEGER_CST@1) INTEGER_CST@2 INTEGER_CST@3)
5159 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
5160 && INTEGRAL_TYPE_P (type)
5161 && (wi::to_widest (@2) + 1 == wi::to_widest (@3)
5162 || wi::to_widest (@2) == wi::to_widest (@3) + 1))
5165 get_range_query (cfun)->range_of_expr (r, @0);
5166 if (r.undefined_p ())
5167 r.set_varying (TREE_TYPE (@0));
5169 wide_int min = r.lower_bound ();
5170 wide_int max = r.upper_bound ();
5173 && (wi::to_wide (@1) == min
5174 || wi::to_wide (@1) == max))
5176 tree arg0 = @2, arg1 = @3;
5178 if ((eqne == EQ_EXPR) ^ (wi::to_wide (@1) == min))
5179 std::swap (arg0, arg1);
5180 if (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type))
5181 type1 = TREE_TYPE (@0);
5184 auto prec = TYPE_PRECISION (type1);
5185 auto unsign = TYPE_UNSIGNED (type1);
5186 type1 = build_nonstandard_integer_type (prec, unsign);
5187 min = wide_int::from (min, prec,
5188 TYPE_SIGN (TREE_TYPE (@0)));
5189 wide_int a = wide_int::from (wi::to_wide (arg0), prec,
5191 enum tree_code code;
5192 wi::overflow_type ovf;
5193 if (tree_int_cst_lt (arg0, arg1))
5199 /* lhs is known to be in range [min, min+1] and we want to add a
5200 to it. Check if that operation can overflow for those 2 values
5201 and if yes, force unsigned type. */
5202 wi::add (min + (wi::neg_p (a) ? 0 : 1), a, SIGNED, &ovf);
5204 type1 = unsigned_type_for (type1);
5213 /* lhs is known to be in range [min, min+1] and we want to subtract
5214 it from a. Check if that operation can overflow for those 2
5215 values and if yes, force unsigned type. */
5216 wi::sub (a, min + (wi::neg_p (min) ? 0 : 1), SIGNED, &ovf);
5218 type1 = unsigned_type_for (type1);
5221 tree arg = wide_int_to_tree (type1, a);
5223 (if (code == PLUS_EXPR)
5224 (convert (plus (convert:type1 @0) { arg; }))
5225 (convert (minus { arg; } (convert:type1 @0)))
5236 (convert (cond@0 @1 INTEGER_CST@2 INTEGER_CST@3))
5237 (if (INTEGRAL_TYPE_P (type)
5238 && INTEGRAL_TYPE_P (TREE_TYPE (@0)))
5239 (cond @1 (convert @2) (convert @3))))
5241 /* Simplification moved from fold_cond_expr_with_comparison. It may also
5243 /* This pattern implements two kinds simplification:
5246 (cond (cmp (convert1? x) c1) (convert2? x) c2) -> (minmax (x c)) if:
5247 1) Conversions are type widening from smaller type.
5248 2) Const c1 equals to c2 after canonicalizing comparison.
5249 3) Comparison has tree code LT, LE, GT or GE.
5250 This specific pattern is needed when (cmp (convert x) c) may not
5251 be simplified by comparison patterns because of multiple uses of
5252 x. It also makes sense here because simplifying across multiple
5253 referred var is always benefitial for complicated cases.
5256 (cond (eq (convert1? x) c1) (convert2? x) c2) -> (cond (eq x c1) c1 c2). */
5257 (for cmp (lt le gt ge eq ne)
5259 (cond (cmp (convert1? @1) INTEGER_CST@3) (convert2? @1) INTEGER_CST@2)
5262 tree from_type = TREE_TYPE (@1);
5263 tree c1_type = TREE_TYPE (@3), c2_type = TREE_TYPE (@2);
5264 enum tree_code code = ERROR_MARK;
5266 if (INTEGRAL_TYPE_P (from_type)
5267 && int_fits_type_p (@2, from_type)
5268 && (types_match (c1_type, from_type)
5269 || (TYPE_PRECISION (c1_type) > TYPE_PRECISION (from_type)
5270 && (TYPE_UNSIGNED (from_type)
5271 || TYPE_SIGN (c1_type) == TYPE_SIGN (from_type))))
5272 && (types_match (c2_type, from_type)
5273 || (TYPE_PRECISION (c2_type) > TYPE_PRECISION (from_type)
5274 && (TYPE_UNSIGNED (from_type)
5275 || TYPE_SIGN (c2_type) == TYPE_SIGN (from_type)))))
5278 code = minmax_from_comparison (cmp, @1, @3, @1, @2);
5279 /* Can do A == C1 ? A : C2 -> A == C1 ? C1 : C2? */
5280 else if (int_fits_type_p (@3, from_type))
5284 (if (code == MAX_EXPR)
5285 (convert (max @1 (convert @2)))
5286 (if (code == MIN_EXPR)
5287 (convert (min @1 (convert @2)))
5288 (if (code == EQ_EXPR)
5289 (convert (cond (eq @1 (convert @3))
5290 (convert:from_type @3) (convert:from_type @2)))))))))
5292 /* (cond (cmp (convert? x) c1) (op x c2) c3) -> (op (minmax x c1) c2) if:
5294 1) OP is PLUS or MINUS.
5295 2) CMP is LT, LE, GT or GE.
5296 3) C3 == (C1 op C2), and computation doesn't have undefined behavior.
5298 This pattern also handles special cases like:
5300 A) Operand x is a unsigned to signed type conversion and c1 is
5301 integer zero. In this case,
5302 (signed type)x < 0 <=> x > MAX_VAL(signed type)
5303 (signed type)x >= 0 <=> x <= MAX_VAL(signed type)
5304 B) Const c1 may not equal to (C3 op' C2). In this case we also
5305 check equality for (c1+1) and (c1-1) by adjusting comparison
5308 TODO: Though signed type is handled by this pattern, it cannot be
5309 simplified at the moment because C standard requires additional
5310 type promotion. In order to match&simplify it here, the IR needs
5311 to be cleaned up by other optimizers, i.e, VRP. */
5312 (for op (plus minus)
5313 (for cmp (lt le gt ge)
5315 (cond (cmp (convert? @X) INTEGER_CST@1) (op @X INTEGER_CST@2) INTEGER_CST@3)
5316 (with { tree from_type = TREE_TYPE (@X), to_type = TREE_TYPE (@1); }
5317 (if (types_match (from_type, to_type)
5318 /* Check if it is special case A). */
5319 || (TYPE_UNSIGNED (from_type)
5320 && !TYPE_UNSIGNED (to_type)
5321 && TYPE_PRECISION (from_type) == TYPE_PRECISION (to_type)
5322 && integer_zerop (@1)
5323 && (cmp == LT_EXPR || cmp == GE_EXPR)))
5326 wi::overflow_type overflow = wi::OVF_NONE;
5327 enum tree_code code, cmp_code = cmp;
5329 wide_int c1 = wi::to_wide (@1);
5330 wide_int c2 = wi::to_wide (@2);
5331 wide_int c3 = wi::to_wide (@3);
5332 signop sgn = TYPE_SIGN (from_type);
5334 /* Handle special case A), given x of unsigned type:
5335 ((signed type)x < 0) <=> (x > MAX_VAL(signed type))
5336 ((signed type)x >= 0) <=> (x <= MAX_VAL(signed type)) */
5337 if (!types_match (from_type, to_type))
5339 if (cmp_code == LT_EXPR)
5341 if (cmp_code == GE_EXPR)
5343 c1 = wi::max_value (to_type);
5345 /* To simplify this pattern, we require c3 = (c1 op c2). Here we
5346 compute (c3 op' c2) and check if it equals to c1 with op' being
5347 the inverted operator of op. Make sure overflow doesn't happen
5348 if it is undefined. */
5349 if (op == PLUS_EXPR)
5350 real_c1 = wi::sub (c3, c2, sgn, &overflow);
5352 real_c1 = wi::add (c3, c2, sgn, &overflow);
5355 if (!overflow || !TYPE_OVERFLOW_UNDEFINED (from_type))
5357 /* Check if c1 equals to real_c1. Boundary condition is handled
5358 by adjusting comparison operation if necessary. */
5359 if (!wi::cmp (wi::sub (real_c1, 1, sgn, &overflow), c1, sgn)
5362 /* X <= Y - 1 equals to X < Y. */
5363 if (cmp_code == LE_EXPR)
5365 /* X > Y - 1 equals to X >= Y. */
5366 if (cmp_code == GT_EXPR)
5369 if (!wi::cmp (wi::add (real_c1, 1, sgn, &overflow), c1, sgn)
5372 /* X < Y + 1 equals to X <= Y. */
5373 if (cmp_code == LT_EXPR)
5375 /* X >= Y + 1 equals to X > Y. */
5376 if (cmp_code == GE_EXPR)
5379 if (code != cmp_code || !wi::cmp (real_c1, c1, sgn))
5381 if (cmp_code == LT_EXPR || cmp_code == LE_EXPR)
5383 if (cmp_code == GT_EXPR || cmp_code == GE_EXPR)
5388 (if (code == MAX_EXPR)
5389 (op (max @X { wide_int_to_tree (from_type, real_c1); })
5390 { wide_int_to_tree (from_type, c2); })
5391 (if (code == MIN_EXPR)
5392 (op (min @X { wide_int_to_tree (from_type, real_c1); })
5393 { wide_int_to_tree (from_type, c2); })))))))))
5396 /* A >= B ? A : B -> max (A, B) and friends. The code is still
5397 in fold_cond_expr_with_comparison for GENERIC folding with
5398 some extra constraints. */
5399 (for cmp (eq ne le lt unle unlt ge gt unge ungt uneq ltgt)
5401 (cond (cmp:c (nop_convert1?@c0 @0) (nop_convert2?@c1 @1))
5402 (convert3? @0) (convert4? @1))
5403 (if (!HONOR_SIGNED_ZEROS (type)
5404 && (/* Allow widening conversions of the compare operands as data. */
5405 (INTEGRAL_TYPE_P (type)
5406 && types_match (TREE_TYPE (@c0), TREE_TYPE (@0))
5407 && types_match (TREE_TYPE (@c1), TREE_TYPE (@1))
5408 && TYPE_PRECISION (TREE_TYPE (@0)) <= TYPE_PRECISION (type)
5409 && TYPE_PRECISION (TREE_TYPE (@1)) <= TYPE_PRECISION (type))
5410 /* Or sign conversions for the comparison. */
5411 || (types_match (type, TREE_TYPE (@0))
5412 && types_match (type, TREE_TYPE (@1)))))
5414 (if (cmp == EQ_EXPR)
5415 (if (VECTOR_TYPE_P (type))
5418 (if (cmp == NE_EXPR)
5419 (if (VECTOR_TYPE_P (type))
5422 (if (cmp == LE_EXPR || cmp == UNLE_EXPR || cmp == LT_EXPR || cmp == UNLT_EXPR)
5423 (if (!HONOR_NANS (type))
5424 (if (VECTOR_TYPE_P (type))
5425 (view_convert (min @c0 @c1))
5426 (convert (min @c0 @c1)))))
5427 (if (cmp == GE_EXPR || cmp == UNGE_EXPR || cmp == GT_EXPR || cmp == UNGT_EXPR)
5428 (if (!HONOR_NANS (type))
5429 (if (VECTOR_TYPE_P (type))
5430 (view_convert (max @c0 @c1))
5431 (convert (max @c0 @c1)))))
5432 (if (cmp == UNEQ_EXPR)
5433 (if (!HONOR_NANS (type))
5434 (if (VECTOR_TYPE_P (type))
5437 (if (cmp == LTGT_EXPR)
5438 (if (!HONOR_NANS (type))
5439 (if (VECTOR_TYPE_P (type))
5441 (convert @c0))))))))
5444 (for cnd (cond vec_cond)
5445 /* (a != b) ? (a - b) : 0 -> (a - b) */
5447 (cnd (ne:c @0 @1) (minus@2 @0 @1) integer_zerop)
5449 /* (a != b) ? (a ^ b) : 0 -> (a ^ b) */
5451 (cnd (ne:c @0 @1) (bit_xor:c@2 @0 @1) integer_zerop)
5453 /* (a != b) ? (a & b) : a -> (a & b) */
5454 /* (a != b) ? (a | b) : a -> (a | b) */
5455 /* (a != b) ? min(a,b) : a -> min(a,b) */
5456 /* (a != b) ? max(a,b) : a -> max(a,b) */
5457 (for op (bit_and bit_ior min max)
5459 (cnd (ne:c @0 @1) (op:c@2 @0 @1) @0)
5461 /* (a != b) ? (a * b) : (a * a) -> (a * b) */
5462 /* (a != b) ? (a + b) : (a + a) -> (a + b) */
5465 (cnd (ne:c @0 @1) (op@2 @0 @1) (op @0 @0))
5466 (if (ANY_INTEGRAL_TYPE_P (type))
5468 /* (a != b) ? (a + b) : (2 * a) -> (a + b) */
5470 (cnd (ne:c @0 @1) (plus@2 @0 @1) (mult @0 uniform_integer_cst_p@3))
5471 (if (wi::to_wide (uniform_integer_cst_p (@3)) == 2)
5475 /* These was part of minmax phiopt. */
5476 /* Optimize (a CMP b) ? minmax<a, c> : minmax<b, c>
5477 to minmax<min/max<a, b>, c> */
5478 (for minmax (min max)
5479 (for cmp (lt le gt ge ne)
5481 (cond (cmp:c @1 @3) (minmax:c @1 @4) (minmax:c @2 @4))
5484 tree_code code = minmax_from_comparison (cmp, @1, @2, @1, @3);
5486 (if (code == MIN_EXPR)
5487 (minmax (min @1 @2) @4)
5488 (if (code == MAX_EXPR)
5489 (minmax (max @1 @2) @4)))))))
5491 /* Optimize (a CMP CST1) ? max<a,CST2> : a */
5492 (for cmp (gt ge lt le)
5493 minmax (min min max max)
5495 (cond (cmp:c @0 @1) (minmax:c@2 @0 @3) @4)
5498 tree_code code = minmax_from_comparison (cmp, @0, @1, @0, @4);
5500 (if ((cmp == LT_EXPR || cmp == LE_EXPR)
5502 && integer_nonzerop (fold_build2 (LE_EXPR, boolean_type_node, @3, @4)))
5504 (if ((cmp == GT_EXPR || cmp == GE_EXPR)
5506 && integer_nonzerop (fold_build2 (GE_EXPR, boolean_type_node, @3, @4)))
5510 /* These patterns should be after min/max detection as simplifications
5511 of `(type)(zero_one ==/!= 0)` to `(type)(zero_one)`
5512 and `(type)(zero_one^1)` are not done yet. See PR 110637.
5513 Even without those, reaching min/max/and/ior faster is better. */
5515 (cond @0 zero_one_valued_p@1 zero_one_valued_p@2)
5517 /* bool0 ? bool1 : 0 -> bool0 & bool1 */
5518 (if (integer_zerop (@2))
5519 (bit_and (convert @0) @1))
5520 /* bool0 ? 0 : bool2 -> (bool0^1) & bool2 */
5521 (if (integer_zerop (@1))
5522 (bit_and (bit_xor (convert @0) { build_one_cst (type); } ) @2))
5523 /* bool0 ? 1 : bool2 -> bool0 | bool2 */
5524 (if (integer_onep (@1))
5525 (bit_ior (convert @0) @2))
5526 /* bool0 ? bool1 : 1 -> (bool0^1) | bool1 */
5527 (if (integer_onep (@2))
5528 (bit_ior (bit_xor (convert @0) @2) @1))
5533 /* X != C1 ? -X : C2 simplifies to -X when -C1 == C2. */
5535 (cond (ne @0 INTEGER_CST@1) (negate@3 @0) INTEGER_CST@2)
5536 (if (!TYPE_SATURATING (type)
5537 && (TYPE_OVERFLOW_WRAPS (type)
5538 || !wi::only_sign_bit_p (wi::to_wide (@1)))
5539 && wi::eq_p (wi::neg (wi::to_wide (@1)), wi::to_wide (@2)))
5542 /* X != C1 ? ~X : C2 simplifies to ~X when ~C1 == C2. */
5544 (cond (ne @0 INTEGER_CST@1) (bit_not@3 @0) INTEGER_CST@2)
5545 (if (wi::eq_p (wi::bit_not (wi::to_wide (@1)), wi::to_wide (@2)))
5548 /* (X + 1) > Y ? -X : 1 simplifies to X >= Y ? -X : 1 when
5549 X is unsigned, as when X + 1 overflows, X is -1, so -X == 1. */
5551 (cond (gt (plus @0 integer_onep) @1) (negate @0) integer_onep@2)
5552 (if (TYPE_UNSIGNED (type))
5553 (cond (ge @0 @1) (negate @0) @2)))
5555 (for cnd (cond vec_cond)
5556 /* A ? B : (A ? X : C) -> A ? B : C. */
5558 (cnd @0 (cnd @0 @1 @2) @3)
5561 (cnd @0 @1 (cnd @0 @2 @3))
5563 /* A ? B : (!A ? C : X) -> A ? B : C. */
5564 /* ??? This matches embedded conditions open-coded because genmatch
5565 would generate matching code for conditions in separate stmts only.
5566 The following is still important to merge then and else arm cases
5567 from if-conversion. */
5569 (cnd @0 @1 (cnd @2 @3 @4))
5570 (if (inverse_conditions_p (@0, @2))
5573 (cnd @0 (cnd @1 @2 @3) @4)
5574 (if (inverse_conditions_p (@0, @1))
5577 /* A ? B : B -> B. */
5582 /* !A ? B : C -> A ? C : B. */
5584 (cnd (logical_inverted_value truth_valued_p@0) @1 @2)
5587 /* abs/negative simplifications moved from fold_cond_expr_with_comparison,
5588 Need to handle (A - B) case as fold_cond_expr_with_comparison does.
5589 Need to handle UN* comparisons.
5591 None of these transformations work for modes with signed
5592 zeros. If A is +/-0, the first two transformations will
5593 change the sign of the result (from +0 to -0, or vice
5594 versa). The last four will fix the sign of the result,
5595 even though the original expressions could be positive or
5596 negative, depending on the sign of A.
5598 Note that all these transformations are correct if A is
5599 NaN, since the two alternatives (A and -A) are also NaNs. */
5601 (for cnd (cond vec_cond)
5602 /* A == 0 ? A : -A same as -A */
5605 (cnd (cmp @0 zerop) @0 (negate@1 @0))
5606 (if (!HONOR_SIGNED_ZEROS (type))
5609 (cnd (cmp @0 zerop) zerop (negate@1 @0))
5610 (if (!HONOR_SIGNED_ZEROS (type))
5613 /* A != 0 ? A : -A same as A */
5616 (cnd (cmp @0 zerop) @0 (negate @0))
5617 (if (!HONOR_SIGNED_ZEROS (type))
5620 (cnd (cmp @0 zerop) @0 integer_zerop)
5621 (if (!HONOR_SIGNED_ZEROS (type))
5624 /* A >=/> 0 ? A : -A same as abs (A) */
5627 (cnd (cmp @0 zerop) @0 (negate @0))
5628 (if (!HONOR_SIGNED_ZEROS (type)
5629 && !TYPE_UNSIGNED (type))
5631 /* A <=/< 0 ? A : -A same as -abs (A) */
5634 (cnd (cmp @0 zerop) @0 (negate @0))
5635 (if (!HONOR_SIGNED_ZEROS (type)
5636 && !TYPE_UNSIGNED (type))
5637 (if (ANY_INTEGRAL_TYPE_P (type)
5638 && !TYPE_OVERFLOW_WRAPS (type))
5640 tree utype = unsigned_type_for (type);
5642 (convert (negate (absu:utype @0))))
5643 (negate (abs @0)))))
5647 /* -(type)!A -> (type)A - 1. */
5649 (negate (convert?:s (logical_inverted_value:s @0)))
5650 (if (INTEGRAL_TYPE_P (type)
5651 && TREE_CODE (type) != BOOLEAN_TYPE
5652 && TYPE_PRECISION (type) > 1
5653 && TREE_CODE (@0) == SSA_NAME
5654 && ssa_name_has_boolean_range (@0))
5655 (plus (convert:type @0) { build_all_ones_cst (type); })))
5657 /* A + (B vcmp C ? 1 : 0) -> A - (B vcmp C ? -1 : 0), since vector comparisons
5658 return all -1 or all 0 results. */
5659 /* ??? We could instead convert all instances of the vec_cond to negate,
5660 but that isn't necessarily a win on its own. */
5662 (plus:c @3 (view_convert? (vec_cond:s @0 integer_each_onep@1 integer_zerop@2)))
5663 (if (VECTOR_TYPE_P (type)
5664 && known_eq (TYPE_VECTOR_SUBPARTS (type),
5665 TYPE_VECTOR_SUBPARTS (TREE_TYPE (@1)))
5666 && (TYPE_MODE (TREE_TYPE (type))
5667 == TYPE_MODE (TREE_TYPE (TREE_TYPE (@1)))))
5668 (minus @3 (view_convert (vec_cond @0 (negate @1) @2)))))
5670 /* ... likewise A - (B vcmp C ? 1 : 0) -> A + (B vcmp C ? -1 : 0). */
5672 (minus @3 (view_convert? (vec_cond:s @0 integer_each_onep@1 integer_zerop@2)))
5673 (if (VECTOR_TYPE_P (type)
5674 && known_eq (TYPE_VECTOR_SUBPARTS (type),
5675 TYPE_VECTOR_SUBPARTS (TREE_TYPE (@1)))
5676 && (TYPE_MODE (TREE_TYPE (type))
5677 == TYPE_MODE (TREE_TYPE (TREE_TYPE (@1)))))
5678 (plus @3 (view_convert (vec_cond @0 (negate @1) @2)))))
5681 /* Simplifications of comparisons. */
5683 /* See if we can reduce the magnitude of a constant involved in a
5684 comparison by changing the comparison code. This is a canonicalization
5685 formerly done by maybe_canonicalize_comparison_1. */
5689 (cmp @0 uniform_integer_cst_p@1)
5690 (with { tree cst = uniform_integer_cst_p (@1); }
5691 (if (tree_int_cst_sgn (cst) == -1)
5692 (acmp @0 { build_uniform_cst (TREE_TYPE (@1),
5693 wide_int_to_tree (TREE_TYPE (cst),
5699 (cmp @0 uniform_integer_cst_p@1)
5700 (with { tree cst = uniform_integer_cst_p (@1); }
5701 (if (tree_int_cst_sgn (cst) == 1)
5702 (acmp @0 { build_uniform_cst (TREE_TYPE (@1),
5703 wide_int_to_tree (TREE_TYPE (cst),
5704 wi::to_wide (cst) - 1)); })))))
5706 /* We can simplify a logical negation of a comparison to the
5707 inverted comparison. As we cannot compute an expression
5708 operator using invert_tree_comparison we have to simulate
5709 that with expression code iteration. */
5710 (for cmp (tcc_comparison)
5711 icmp (inverted_tcc_comparison)
5712 ncmp (inverted_tcc_comparison_with_nans)
5713 /* Ideally we'd like to combine the following two patterns
5714 and handle some more cases by using
5715 (logical_inverted_value (cmp @0 @1))
5716 here but for that genmatch would need to "inline" that.
5717 For now implement what forward_propagate_comparison did. */
5719 (bit_not (cmp @0 @1))
5720 (if (VECTOR_TYPE_P (type)
5721 || (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1))
5722 /* Comparison inversion may be impossible for trapping math,
5723 invert_tree_comparison will tell us. But we can't use
5724 a computed operator in the replacement tree thus we have
5725 to play the trick below. */
5726 (with { enum tree_code ic = invert_tree_comparison
5727 (cmp, HONOR_NANS (@0)); }
5733 (bit_xor (cmp @0 @1) integer_truep)
5734 (with { enum tree_code ic = invert_tree_comparison
5735 (cmp, HONOR_NANS (@0)); }
5740 /* The following bits are handled by fold_binary_op_with_conditional_arg. */
5742 (ne (cmp@2 @0 @1) integer_zerop)
5743 (if (types_match (type, TREE_TYPE (@2)))
5746 (eq (cmp@2 @0 @1) integer_truep)
5747 (if (types_match (type, TREE_TYPE (@2)))
5750 (ne (cmp@2 @0 @1) integer_truep)
5751 (if (types_match (type, TREE_TYPE (@2)))
5752 (with { enum tree_code ic = invert_tree_comparison
5753 (cmp, HONOR_NANS (@0)); }
5759 (eq (cmp@2 @0 @1) integer_zerop)
5760 (if (types_match (type, TREE_TYPE (@2)))
5761 (with { enum tree_code ic = invert_tree_comparison
5762 (cmp, HONOR_NANS (@0)); }
5768 /* Transform comparisons of the form X - Y CMP 0 to X CMP Y.
5769 ??? The transformation is valid for the other operators if overflow
5770 is undefined for the type, but performing it here badly interacts
5771 with the transformation in fold_cond_expr_with_comparison which
5772 attempts to synthetize ABS_EXPR. */
5774 (for sub (minus pointer_diff)
5776 (cmp (sub@2 @0 @1) integer_zerop)
5777 (if (single_use (@2))
5780 /* Simplify (x < 0) ^ (y < 0) to (x ^ y) < 0 and
5781 (x >= 0) ^ (y >= 0) to (x ^ y) < 0. */
5784 (bit_xor (cmp:s @0 integer_zerop) (cmp:s @1 integer_zerop))
5785 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
5786 && !TYPE_UNSIGNED (TREE_TYPE (@0))
5787 && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
5788 (lt (bit_xor @0 @1) { build_zero_cst (TREE_TYPE (@0)); }))))
5789 /* Simplify (x < 0) ^ (y >= 0) to (x ^ y) >= 0 and
5790 (x >= 0) ^ (y < 0) to (x ^ y) >= 0. */
5792 (bit_xor:c (lt:s @0 integer_zerop) (ge:s @1 integer_zerop))
5793 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
5794 && !TYPE_UNSIGNED (TREE_TYPE (@0))
5795 && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
5796 (ge (bit_xor @0 @1) { build_zero_cst (TREE_TYPE (@0)); })))
5798 /* Transform comparisons of the form X * C1 CMP 0 to X CMP 0 in the
5799 signed arithmetic case. That form is created by the compiler
5800 often enough for folding it to be of value. One example is in
5801 computing loop trip counts after Operator Strength Reduction. */
5802 (for cmp (simple_comparison)
5803 scmp (swapped_simple_comparison)
5805 (cmp (mult@3 @0 INTEGER_CST@1) integer_zerop@2)
5806 /* Handle unfolded multiplication by zero. */
5807 (if (integer_zerop (@1))
5809 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
5810 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
5812 /* If @1 is negative we swap the sense of the comparison. */
5813 (if (tree_int_cst_sgn (@1) < 0)
5817 /* For integral types with undefined overflow fold
5818 x * C1 == C2 into x == C2 / C1 or false.
5819 If overflow wraps and C1 is odd, simplify to x == C2 / C1 in the ring
5823 (cmp (mult @0 INTEGER_CST@1) INTEGER_CST@2)
5824 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
5825 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
5826 && wi::to_wide (@1) != 0)
5827 (with { widest_int quot; }
5828 (if (wi::multiple_of_p (wi::to_widest (@2), wi::to_widest (@1),
5829 TYPE_SIGN (TREE_TYPE (@0)), "))
5830 (cmp @0 { wide_int_to_tree (TREE_TYPE (@0), quot); })
5831 { constant_boolean_node (cmp == NE_EXPR, type); }))
5832 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
5833 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))
5834 && (wi::bit_and (wi::to_wide (@1), 1) == 1))
5837 tree itype = TREE_TYPE (@0);
5838 int p = TYPE_PRECISION (itype);
5839 wide_int m = wi::one (p + 1) << p;
5840 wide_int a = wide_int::from (wi::to_wide (@1), p + 1, UNSIGNED);
5841 wide_int i = wide_int::from (wi::mod_inv (a, m),
5842 p, TYPE_SIGN (itype));
5843 wide_int_to_tree (itype, wi::mul (i, wi::to_wide (@2)));
5846 /* Simplify comparison of something with itself. For IEEE
5847 floating-point, we can only do some of these simplifications. */
5851 (if (! FLOAT_TYPE_P (TREE_TYPE (@0))
5852 || ! tree_expr_maybe_nan_p (@0))
5853 { constant_boolean_node (true, type); }
5855 /* With -ftrapping-math conversion to EQ loses an exception. */
5856 && (! FLOAT_TYPE_P (TREE_TYPE (@0))
5857 || ! flag_trapping_math))
5863 || ! FLOAT_TYPE_P (TREE_TYPE (@0))
5864 || ! tree_expr_maybe_nan_p (@0))
5865 { constant_boolean_node (false, type); })))
5866 (for cmp (unle unge uneq)
5869 { constant_boolean_node (true, type); }))
5870 (for cmp (unlt ungt)
5876 (if (!flag_trapping_math || !tree_expr_maybe_nan_p (@0))
5877 { constant_boolean_node (false, type); }))
5879 /* x == ~x -> false */
5880 /* x != ~x -> true */
5883 (cmp:c @0 (bit_not @0))
5884 { constant_boolean_node (cmp == NE_EXPR, type); }))
5886 /* Fold ~X op ~Y as Y op X. */
5887 (for cmp (simple_comparison)
5889 (cmp (bit_not@2 @0) (bit_not@3 @1))
5890 (if (single_use (@2) && single_use (@3))
5893 /* Fold ~X op C as X op' ~C, where op' is the swapped comparison. */
5894 (for cmp (simple_comparison)
5895 scmp (swapped_simple_comparison)
5897 (cmp (bit_not@2 @0) CONSTANT_CLASS_P@1)
5898 (if (single_use (@2)
5899 && (TREE_CODE (@1) == INTEGER_CST || TREE_CODE (@1) == VECTOR_CST))
5900 (scmp @0 (bit_not @1)))))
5902 (for cmp (simple_comparison)
5905 /* IEEE doesn't distinguish +0 and -0 in comparisons. */
5907 /* a CMP (-0) -> a CMP 0 */
5908 (if (REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@1)))
5909 (cmp @0 { build_real (TREE_TYPE (@1), dconst0); }))
5910 /* (-0) CMP b -> 0 CMP b. */
5911 (if (TREE_CODE (@0) == REAL_CST
5912 && REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@0)))
5913 (cmp { build_real (TREE_TYPE (@0), dconst0); } @1))
5914 /* x != NaN is always true, other ops are always false. */
5915 (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
5916 && (cmp == EQ_EXPR || cmp == NE_EXPR || !flag_trapping_math)
5917 && !tree_expr_signaling_nan_p (@1)
5918 && !tree_expr_maybe_signaling_nan_p (@0))
5919 { constant_boolean_node (cmp == NE_EXPR, type); })
5920 /* NaN != y is always true, other ops are always false. */
5921 (if (TREE_CODE (@0) == REAL_CST
5922 && REAL_VALUE_ISNAN (TREE_REAL_CST (@0))
5923 && (cmp == EQ_EXPR || cmp == NE_EXPR || !flag_trapping_math)
5924 && !tree_expr_signaling_nan_p (@0)
5925 && !tree_expr_signaling_nan_p (@1))
5926 { constant_boolean_node (cmp == NE_EXPR, type); })
5927 /* Fold comparisons against infinity. */
5928 (if (REAL_VALUE_ISINF (TREE_REAL_CST (@1))
5929 && MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (@1))))
5932 REAL_VALUE_TYPE max;
5933 enum tree_code code = cmp;
5934 bool neg = REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1));
5936 code = swap_tree_comparison (code);
5939 /* x > +Inf is always false, if we ignore NaNs or exceptions. */
5940 (if (code == GT_EXPR
5941 && !(HONOR_NANS (@0) && flag_trapping_math))
5942 { constant_boolean_node (false, type); })
5943 (if (code == LE_EXPR)
5944 /* x <= +Inf is always true, if we don't care about NaNs. */
5945 (if (! HONOR_NANS (@0))
5946 { constant_boolean_node (true, type); }
5947 /* x <= +Inf is the same as x == x, i.e. !isnan(x), but this loses
5948 an "invalid" exception. */
5949 (if (!flag_trapping_math)
5951 /* x == +Inf and x >= +Inf are always equal to x > DBL_MAX, but
5952 for == this introduces an exception for x a NaN. */
5953 (if ((code == EQ_EXPR && !(HONOR_NANS (@0) && flag_trapping_math))
5955 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
5957 (lt @0 { build_real (TREE_TYPE (@0), max); })
5958 (gt @0 { build_real (TREE_TYPE (@0), max); }))))
5959 /* x < +Inf is always equal to x <= DBL_MAX. */
5960 (if (code == LT_EXPR)
5961 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
5963 (ge @0 { build_real (TREE_TYPE (@0), max); })
5964 (le @0 { build_real (TREE_TYPE (@0), max); }))))
5965 /* x != +Inf is always equal to !(x > DBL_MAX), but this introduces
5966 an exception for x a NaN so use an unordered comparison. */
5967 (if (code == NE_EXPR)
5968 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
5969 (if (! HONOR_NANS (@0))
5971 (ge @0 { build_real (TREE_TYPE (@0), max); })
5972 (le @0 { build_real (TREE_TYPE (@0), max); }))
5974 (unge @0 { build_real (TREE_TYPE (@0), max); })
5975 (unle @0 { build_real (TREE_TYPE (@0), max); }))))))))))
5977 /* If this is a comparison of a real constant with a PLUS_EXPR
5978 or a MINUS_EXPR of a real constant, we can convert it into a
5979 comparison with a revised real constant as long as no overflow
5980 occurs when unsafe_math_optimizations are enabled. */
5981 (if (flag_unsafe_math_optimizations)
5982 (for op (plus minus)
5984 (cmp (op @0 REAL_CST@1) REAL_CST@2)
5987 tree tem = const_binop (op == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR,
5988 TREE_TYPE (@1), @2, @1);
5990 (if (tem && !TREE_OVERFLOW (tem))
5991 (cmp @0 { tem; }))))))
5993 /* Likewise, we can simplify a comparison of a real constant with
5994 a MINUS_EXPR whose first operand is also a real constant, i.e.
5995 (c1 - x) < c2 becomes x > c1-c2. Reordering is allowed on
5996 floating-point types only if -fassociative-math is set. */
5997 (if (flag_associative_math)
5999 (cmp (minus REAL_CST@0 @1) REAL_CST@2)
6000 (with { tree tem = const_binop (MINUS_EXPR, TREE_TYPE (@1), @0, @2); }
6001 (if (tem && !TREE_OVERFLOW (tem))
6002 (cmp { tem; } @1)))))
6004 /* Fold comparisons against built-in math functions. */
6005 (if (flag_unsafe_math_optimizations && ! flag_errno_math)
6008 (cmp (sq @0) REAL_CST@1)
6010 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
6012 /* sqrt(x) < y is always false, if y is negative. */
6013 (if (cmp == EQ_EXPR || cmp == LT_EXPR || cmp == LE_EXPR)
6014 { constant_boolean_node (false, type); })
6015 /* sqrt(x) > y is always true, if y is negative and we
6016 don't care about NaNs, i.e. negative values of x. */
6017 (if (cmp == NE_EXPR || !HONOR_NANS (@0))
6018 { constant_boolean_node (true, type); })
6019 /* sqrt(x) > y is the same as x >= 0, if y is negative. */
6020 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })))
6021 (if (real_equal (TREE_REAL_CST_PTR (@1), &dconst0))
6023 /* sqrt(x) < 0 is always false. */
6024 (if (cmp == LT_EXPR)
6025 { constant_boolean_node (false, type); })
6026 /* sqrt(x) >= 0 is always true if we don't care about NaNs. */
6027 (if (cmp == GE_EXPR && !HONOR_NANS (@0))
6028 { constant_boolean_node (true, type); })
6029 /* sqrt(x) <= 0 -> x == 0. */
6030 (if (cmp == LE_EXPR)
6032 /* Otherwise sqrt(x) cmp 0 -> x cmp 0. Here cmp can be >=, >,
6033 == or !=. In the last case:
6035 (sqrt(x) != 0) == (NaN != 0) == true == (x != 0)
6037 if x is negative or NaN. Due to -funsafe-math-optimizations,
6038 the results for other x follow from natural arithmetic. */
6040 (if ((cmp == LT_EXPR
6044 && !REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
6045 /* Give up for -frounding-math. */
6046 && !HONOR_SIGN_DEPENDENT_ROUNDING (TREE_TYPE (@0)))
6050 enum tree_code ncmp = cmp;
6051 const real_format *fmt
6052 = REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (@0)));
6053 real_arithmetic (&c2, MULT_EXPR,
6054 &TREE_REAL_CST (@1), &TREE_REAL_CST (@1));
6055 real_convert (&c2, fmt, &c2);
6056 /* See PR91734: if c2 is inexact and sqrt(c2) < c (or sqrt(c2) >= c),
6057 then change LT_EXPR into LE_EXPR or GE_EXPR into GT_EXPR. */
6058 if (!REAL_VALUE_ISINF (c2))
6060 tree c3 = fold_const_call (CFN_SQRT, TREE_TYPE (@0),
6061 build_real (TREE_TYPE (@0), c2));
6062 if (c3 == NULL_TREE || TREE_CODE (c3) != REAL_CST)
6064 else if ((cmp == LT_EXPR || cmp == GE_EXPR)
6065 && real_less (&TREE_REAL_CST (c3), &TREE_REAL_CST (@1)))
6066 ncmp = cmp == LT_EXPR ? LE_EXPR : GT_EXPR;
6067 else if ((cmp == LE_EXPR || cmp == GT_EXPR)
6068 && real_less (&TREE_REAL_CST (@1), &TREE_REAL_CST (c3)))
6069 ncmp = cmp == LE_EXPR ? LT_EXPR : GE_EXPR;
6072 /* With rounding to even, sqrt of up to 3 different values
6073 gives the same normal result, so in some cases c2 needs
6075 REAL_VALUE_TYPE c2alt, tow;
6076 if (cmp == LT_EXPR || cmp == GE_EXPR)
6080 real_nextafter (&c2alt, fmt, &c2, &tow);
6081 real_convert (&c2alt, fmt, &c2alt);
6082 if (REAL_VALUE_ISINF (c2alt))
6086 c3 = fold_const_call (CFN_SQRT, TREE_TYPE (@0),
6087 build_real (TREE_TYPE (@0), c2alt));
6088 if (c3 == NULL_TREE || TREE_CODE (c3) != REAL_CST)
6090 else if (real_equal (&TREE_REAL_CST (c3),
6091 &TREE_REAL_CST (@1)))
6097 (if (cmp == GT_EXPR || cmp == GE_EXPR)
6098 (if (REAL_VALUE_ISINF (c2))
6099 /* sqrt(x) > y is x == +Inf, when y is very large. */
6100 (if (HONOR_INFINITIES (@0))
6101 (eq @0 { build_real (TREE_TYPE (@0), c2); })
6102 { constant_boolean_node (false, type); })
6103 /* sqrt(x) > c is the same as x > c*c. */
6104 (if (ncmp != ERROR_MARK)
6105 (if (ncmp == GE_EXPR)
6106 (ge @0 { build_real (TREE_TYPE (@0), c2); })
6107 (gt @0 { build_real (TREE_TYPE (@0), c2); }))))
6108 /* else if (cmp == LT_EXPR || cmp == LE_EXPR) */
6109 (if (REAL_VALUE_ISINF (c2))
6111 /* sqrt(x) < y is always true, when y is a very large
6112 value and we don't care about NaNs or Infinities. */
6113 (if (! HONOR_NANS (@0) && ! HONOR_INFINITIES (@0))
6114 { constant_boolean_node (true, type); })
6115 /* sqrt(x) < y is x != +Inf when y is very large and we
6116 don't care about NaNs. */
6117 (if (! HONOR_NANS (@0))
6118 (ne @0 { build_real (TREE_TYPE (@0), c2); }))
6119 /* sqrt(x) < y is x >= 0 when y is very large and we
6120 don't care about Infinities. */
6121 (if (! HONOR_INFINITIES (@0))
6122 (ge @0 { build_real (TREE_TYPE (@0), dconst0); }))
6123 /* sqrt(x) < y is x >= 0 && x != +Inf, when y is large. */
6126 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
6127 (ne @0 { build_real (TREE_TYPE (@0), c2); }))))
6128 /* sqrt(x) < c is the same as x < c*c, if we ignore NaNs. */
6129 (if (ncmp != ERROR_MARK && ! HONOR_NANS (@0))
6130 (if (ncmp == LT_EXPR)
6131 (lt @0 { build_real (TREE_TYPE (@0), c2); })
6132 (le @0 { build_real (TREE_TYPE (@0), c2); }))
6133 /* sqrt(x) < c is the same as x >= 0 && x < c*c. */
6134 (if (ncmp != ERROR_MARK && GENERIC)
6135 (if (ncmp == LT_EXPR)
6137 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
6138 (lt @0 { build_real (TREE_TYPE (@0), c2); }))
6140 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
6141 (le @0 { build_real (TREE_TYPE (@0), c2); })))))))))))
6142 /* Transform sqrt(x) cmp sqrt(y) -> x cmp y. */
6144 (cmp (sq @0) (sq @1))
6145 (if (! HONOR_NANS (@0))
6148 /* Optimize various special cases of (FTYPE) N CMP (FTYPE) M. */
6149 (for cmp (lt le eq ne ge gt unordered ordered unlt unle ungt unge uneq ltgt)
6150 icmp (lt le eq ne ge gt unordered ordered lt le gt ge eq ne)
6152 (cmp (float@0 @1) (float @2))
6153 (if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (@0))
6154 && ! DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0)))
6157 format_helper fmt (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (@0))));
6158 tree type1 = TREE_TYPE (@1);
6159 bool type1_signed_p = TYPE_SIGN (type1) == SIGNED;
6160 tree type2 = TREE_TYPE (@2);
6161 bool type2_signed_p = TYPE_SIGN (type2) == SIGNED;
6163 (if (fmt.can_represent_integral_type_p (type1)
6164 && fmt.can_represent_integral_type_p (type2))
6165 (if (cmp == ORDERED_EXPR || cmp == UNORDERED_EXPR)
6166 { constant_boolean_node (cmp == ORDERED_EXPR, type); }
6167 (if (TYPE_PRECISION (type1) > TYPE_PRECISION (type2)
6168 && type1_signed_p >= type2_signed_p)
6169 (icmp @1 (convert @2))
6170 (if (TYPE_PRECISION (type1) < TYPE_PRECISION (type2)
6171 && type1_signed_p <= type2_signed_p)
6172 (icmp (convert:type2 @1) @2)
6173 (if (TYPE_PRECISION (type1) == TYPE_PRECISION (type2)
6174 && type1_signed_p == type2_signed_p)
6175 (icmp @1 @2))))))))))
6177 /* Optimize various special cases of (FTYPE) N CMP CST. */
6178 (for cmp (lt le eq ne ge gt)
6179 icmp (le le eq ne ge ge)
6181 (cmp (float @0) REAL_CST@1)
6182 (if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (@1))
6183 && ! DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1)))
6186 tree itype = TREE_TYPE (@0);
6187 format_helper fmt (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (@1))));
6188 const REAL_VALUE_TYPE *cst = TREE_REAL_CST_PTR (@1);
6189 /* Be careful to preserve any potential exceptions due to
6190 NaNs. qNaNs are ok in == or != context.
6191 TODO: relax under -fno-trapping-math or
6192 -fno-signaling-nans. */
6194 = real_isnan (cst) && (cst->signalling
6195 || (cmp != EQ_EXPR && cmp != NE_EXPR));
6197 /* TODO: allow non-fitting itype and SNaNs when
6198 -fno-trapping-math. */
6199 (if (fmt.can_represent_integral_type_p (itype) && ! exception_p)
6202 signop isign = TYPE_SIGN (itype);
6203 REAL_VALUE_TYPE imin, imax;
6204 real_from_integer (&imin, fmt, wi::min_value (itype), isign);
6205 real_from_integer (&imax, fmt, wi::max_value (itype), isign);
6207 REAL_VALUE_TYPE icst;
6208 if (cmp == GT_EXPR || cmp == GE_EXPR)
6209 real_ceil (&icst, fmt, cst);
6210 else if (cmp == LT_EXPR || cmp == LE_EXPR)
6211 real_floor (&icst, fmt, cst);
6213 real_trunc (&icst, fmt, cst);
6215 bool cst_int_p = !real_isnan (cst) && real_identical (&icst, cst);
6217 bool overflow_p = false;
6219 = real_to_integer (&icst, &overflow_p, TYPE_PRECISION (itype));
6222 /* Optimize cases when CST is outside of ITYPE's range. */
6223 (if (real_compare (LT_EXPR, cst, &imin))
6224 { constant_boolean_node (cmp == GT_EXPR || cmp == GE_EXPR || cmp == NE_EXPR,
6226 (if (real_compare (GT_EXPR, cst, &imax))
6227 { constant_boolean_node (cmp == LT_EXPR || cmp == LE_EXPR || cmp == NE_EXPR,
6229 /* Remove cast if CST is an integer representable by ITYPE. */
6231 (cmp @0 { gcc_assert (!overflow_p);
6232 wide_int_to_tree (itype, icst_val); })
6234 /* When CST is fractional, optimize
6235 (FTYPE) N == CST -> 0
6236 (FTYPE) N != CST -> 1. */
6237 (if (cmp == EQ_EXPR || cmp == NE_EXPR)
6238 { constant_boolean_node (cmp == NE_EXPR, type); })
6239 /* Otherwise replace with sensible integer constant. */
6242 gcc_checking_assert (!overflow_p);
6244 (icmp @0 { wide_int_to_tree (itype, icst_val); })))))))))
6246 /* Fold A /[ex] B CMP C to A CMP B * C. */
6249 (cmp (exact_div @0 @1) INTEGER_CST@2)
6250 (if (!integer_zerop (@1))
6251 (if (wi::to_wide (@2) == 0)
6253 (if (TREE_CODE (@1) == INTEGER_CST)
6256 wi::overflow_type ovf;
6257 wide_int prod = wi::mul (wi::to_wide (@2), wi::to_wide (@1),
6258 TYPE_SIGN (TREE_TYPE (@1)), &ovf);
6261 { constant_boolean_node (cmp == NE_EXPR, type); }
6262 (cmp @0 { wide_int_to_tree (TREE_TYPE (@0), prod); }))))))))
6263 (for cmp (lt le gt ge)
6265 (cmp (exact_div @0 INTEGER_CST@1) INTEGER_CST@2)
6266 (if (wi::gt_p (wi::to_wide (@1), 0, TYPE_SIGN (TREE_TYPE (@1))))
6269 wi::overflow_type ovf;
6270 wide_int prod = wi::mul (wi::to_wide (@2), wi::to_wide (@1),
6271 TYPE_SIGN (TREE_TYPE (@1)), &ovf);
6274 { constant_boolean_node (wi::lt_p (wi::to_wide (@2), 0,
6275 TYPE_SIGN (TREE_TYPE (@2)))
6276 != (cmp == LT_EXPR || cmp == LE_EXPR), type); }
6277 (cmp @0 { wide_int_to_tree (TREE_TYPE (@0), prod); }))))))
6279 /* Fold (size_t)(A /[ex] B) CMP C to (size_t)A CMP (size_t)B * C or A CMP' 0.
6281 For small C (less than max/B), this is (size_t)A CMP (size_t)B * C.
6282 For large C (more than min/B+2^size), this is also true, with the
6283 multiplication computed modulo 2^size.
6284 For intermediate C, this just tests the sign of A. */
6285 (for cmp (lt le gt ge)
6288 (cmp (convert (exact_div @0 INTEGER_CST@1)) INTEGER_CST@2)
6289 (if (tree_nop_conversion_p (TREE_TYPE (@0), TREE_TYPE (@2))
6290 && TYPE_UNSIGNED (TREE_TYPE (@2)) && !TYPE_UNSIGNED (TREE_TYPE (@0))
6291 && wi::gt_p (wi::to_wide (@1), 0, TYPE_SIGN (TREE_TYPE (@1))))
6294 tree utype = TREE_TYPE (@2);
6295 wide_int denom = wi::to_wide (@1);
6296 wide_int right = wi::to_wide (@2);
6297 wide_int smax = wi::sdiv_trunc (wi::max_value (TREE_TYPE (@0)), denom);
6298 wide_int smin = wi::sdiv_trunc (wi::min_value (TREE_TYPE (@0)), denom);
6299 bool small = wi::leu_p (right, smax);
6300 bool large = wi::geu_p (right, smin);
6302 (if (small || large)
6303 (cmp (convert:utype @0) (mult @2 (convert @1)))
6304 (cmp2 @0 { build_zero_cst (TREE_TYPE (@0)); }))))))
6306 /* Unordered tests if either argument is a NaN. */
6308 (bit_ior (unordered @0 @0) (unordered @1 @1))
6309 (if (types_match (@0, @1))
6312 (bit_and (ordered @0 @0) (ordered @1 @1))
6313 (if (types_match (@0, @1))
6316 (bit_ior:c (unordered @0 @0) (unordered:c@2 @0 @1))
6319 (bit_and:c (ordered @0 @0) (ordered:c@2 @0 @1))
6322 /* A & (2**N - 1) <= 2**K - 1 -> A & (2**N - 2**K) == 0
6323 A & (2**N - 1) > 2**K - 1 -> A & (2**N - 2**K) != 0
6325 Note that comparisons
6326 A & (2**N - 1) < 2**K -> A & (2**N - 2**K) == 0
6327 A & (2**N - 1) >= 2**K -> A & (2**N - 2**K) != 0
6328 will be canonicalized to above so there's no need to
6335 (cmp (bit_and@0 @1 INTEGER_CST@2) INTEGER_CST@3)
6336 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)))
6339 tree ty = TREE_TYPE (@0);
6340 unsigned prec = TYPE_PRECISION (ty);
6341 wide_int mask = wi::to_wide (@2, prec);
6342 wide_int rhs = wi::to_wide (@3, prec);
6343 signop sgn = TYPE_SIGN (ty);
6345 (if ((mask & (mask + 1)) == 0 && wi::gt_p (rhs, 0, sgn)
6346 && (rhs & (rhs + 1)) == 0 && wi::ge_p (mask, rhs, sgn))
6347 (eqcmp (bit_and @1 { wide_int_to_tree (ty, mask - rhs); })
6348 { build_zero_cst (ty); }))))))
6350 /* -A CMP -B -> B CMP A. */
6351 (for cmp (tcc_comparison)
6352 scmp (swapped_tcc_comparison)
6354 (cmp (negate @0) (negate @1))
6355 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
6356 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
6359 || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))))
6362 (cmp (negate @0) CONSTANT_CLASS_P@1)
6363 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
6364 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
6367 || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))))
6368 (with { tree tem = const_unop (NEGATE_EXPR, TREE_TYPE (@0), @1); }
6369 (if (tem && !TREE_OVERFLOW (tem))
6370 (scmp @0 { tem; }))))))
6372 /* Convert ABS[U]_EXPR<x> == 0 or ABS[U]_EXPR<x> != 0 to x == 0 or x != 0. */
6376 (eqne (op @0) zerop@1)
6377 (eqne @0 { build_zero_cst (TREE_TYPE (@0)); }))))
6379 /* From fold_sign_changed_comparison and fold_widened_comparison.
6380 FIXME: the lack of symmetry is disturbing. */
6381 (for cmp (simple_comparison)
6383 (cmp (convert@0 @00) (convert?@1 @10))
6384 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6385 /* Disable this optimization if we're casting a function pointer
6386 type on targets that require function pointer canonicalization. */
6387 && !(targetm.have_canonicalize_funcptr_for_compare ()
6388 && ((POINTER_TYPE_P (TREE_TYPE (@00))
6389 && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@00))))
6390 || (POINTER_TYPE_P (TREE_TYPE (@10))
6391 && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@10))))))
6393 (if (TYPE_PRECISION (TREE_TYPE (@00)) == TYPE_PRECISION (TREE_TYPE (@0))
6394 && (TREE_CODE (@10) == INTEGER_CST
6396 && (TYPE_UNSIGNED (TREE_TYPE (@00)) == TYPE_UNSIGNED (TREE_TYPE (@0))
6399 && !POINTER_TYPE_P (TREE_TYPE (@00))
6400 /* (int)bool:32 != (int)uint is not the same as
6401 bool:32 != (bool:32)uint since boolean types only have two valid
6402 values independent of their precision. */
6403 && (TREE_CODE (TREE_TYPE (@00)) != BOOLEAN_TYPE
6404 || TREE_CODE (TREE_TYPE (@10)) == BOOLEAN_TYPE))
6405 /* ??? The special-casing of INTEGER_CST conversion was in the original
6406 code and here to avoid a spurious overflow flag on the resulting
6407 constant which fold_convert produces. */
6408 (if (TREE_CODE (@1) == INTEGER_CST)
6409 (cmp @00 { force_fit_type (TREE_TYPE (@00), wi::to_widest (@1), 0,
6410 TREE_OVERFLOW (@1)); })
6411 (cmp @00 (convert @1)))
6413 (if (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (TREE_TYPE (@00)))
6414 /* If possible, express the comparison in the shorter mode. */
6415 (if ((cmp == EQ_EXPR || cmp == NE_EXPR
6416 || TYPE_UNSIGNED (TREE_TYPE (@0)) == TYPE_UNSIGNED (TREE_TYPE (@00))
6417 || (!TYPE_UNSIGNED (TREE_TYPE (@0))
6418 && TYPE_UNSIGNED (TREE_TYPE (@00))))
6419 && (types_match (TREE_TYPE (@10), TREE_TYPE (@00))
6420 || ((TYPE_PRECISION (TREE_TYPE (@00))
6421 >= TYPE_PRECISION (TREE_TYPE (@10)))
6422 && (TYPE_UNSIGNED (TREE_TYPE (@00))
6423 == TYPE_UNSIGNED (TREE_TYPE (@10))))
6424 || (TREE_CODE (@10) == INTEGER_CST
6425 && INTEGRAL_TYPE_P (TREE_TYPE (@00))
6426 && int_fits_type_p (@10, TREE_TYPE (@00)))))
6427 (cmp @00 (convert @10))
6428 (if (TREE_CODE (@10) == INTEGER_CST
6429 && INTEGRAL_TYPE_P (TREE_TYPE (@00))
6430 && !int_fits_type_p (@10, TREE_TYPE (@00)))
6433 tree min = lower_bound_in_type (TREE_TYPE (@10), TREE_TYPE (@00));
6434 tree max = upper_bound_in_type (TREE_TYPE (@10), TREE_TYPE (@00));
6435 bool above = integer_nonzerop (const_binop (LT_EXPR, type, max, @10));
6436 bool below = integer_nonzerop (const_binop (LT_EXPR, type, @10, min));
6438 (if (above || below)
6439 (if (cmp == EQ_EXPR || cmp == NE_EXPR)
6440 { constant_boolean_node (cmp == EQ_EXPR ? false : true, type); }
6441 (if (cmp == LT_EXPR || cmp == LE_EXPR)
6442 { constant_boolean_node (above ? true : false, type); }
6443 (if (cmp == GT_EXPR || cmp == GE_EXPR)
6444 { constant_boolean_node (above ? false : true, type); })))))))))
6445 /* Fold (double)float1 CMP (double)float2 into float1 CMP float2. */
6446 (if (FLOAT_TYPE_P (TREE_TYPE (@00))
6447 && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0))
6448 == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@00)))
6449 && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0))
6450 == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@10))))
6453 tree type1 = TREE_TYPE (@10);
6454 if (TREE_CODE (@10) == REAL_CST && !DECIMAL_FLOAT_TYPE_P (type1))
6456 REAL_VALUE_TYPE orig = TREE_REAL_CST (@10);
6457 if (TYPE_PRECISION (type1) > TYPE_PRECISION (float_type_node)
6458 && exact_real_truncate (TYPE_MODE (float_type_node), &orig))
6459 type1 = float_type_node;
6460 if (TYPE_PRECISION (type1) > TYPE_PRECISION (double_type_node)
6461 && exact_real_truncate (TYPE_MODE (double_type_node), &orig))
6462 type1 = double_type_node;
6465 = (element_precision (TREE_TYPE (@00)) > element_precision (type1)
6466 ? TREE_TYPE (@00) : type1);
6468 (if (element_precision (TREE_TYPE (@0)) > element_precision (newtype))
6469 (cmp (convert:newtype @00) (convert:newtype @10))))))))
6474 /* SSA names are canonicalized to 2nd place. */
6475 (cmp addr@0 SSA_NAME@1)
6478 poly_int64 off; tree base;
6479 tree addr = (TREE_CODE (@0) == SSA_NAME
6480 ? gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0)) : @0);
6482 /* A local variable can never be pointed to by
6483 the default SSA name of an incoming parameter. */
6484 (if (SSA_NAME_IS_DEFAULT_DEF (@1)
6485 && TREE_CODE (SSA_NAME_VAR (@1)) == PARM_DECL
6486 && (base = get_base_address (TREE_OPERAND (addr, 0)))
6487 && TREE_CODE (base) == VAR_DECL
6488 && auto_var_in_fn_p (base, current_function_decl))
6489 (if (cmp == NE_EXPR)
6490 { constant_boolean_node (true, type); }
6491 { constant_boolean_node (false, type); })
6492 /* If the address is based on @1 decide using the offset. */
6493 (if ((base = get_addr_base_and_unit_offset (TREE_OPERAND (addr, 0), &off))
6494 && TREE_CODE (base) == MEM_REF
6495 && TREE_OPERAND (base, 0) == @1)
6496 (with { off += mem_ref_offset (base).force_shwi (); }
6497 (if (known_ne (off, 0))
6498 { constant_boolean_node (cmp == NE_EXPR, type); }
6499 (if (known_eq (off, 0))
6500 { constant_boolean_node (cmp == EQ_EXPR, type); }))))))))
6502 /* Equality compare simplifications from fold_binary */
6505 /* If we have (A | C) == D where C & ~D != 0, convert this into 0.
6506 Similarly for NE_EXPR. */
6508 (cmp (convert?@3 (bit_ior @0 INTEGER_CST@1)) INTEGER_CST@2)
6509 (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0))
6510 && wi::bit_and_not (wi::to_wide (@1), wi::to_wide (@2)) != 0)
6511 { constant_boolean_node (cmp == NE_EXPR, type); }))
6513 /* (X ^ Y) == 0 becomes X == Y, and (X ^ Y) != 0 becomes X != Y. */
6515 (cmp (bit_xor @0 @1) integer_zerop)
6518 /* (X ^ Y) == Y becomes X == 0.
6519 Likewise (X ^ Y) == X becomes Y == 0. */
6521 (cmp:c (bit_xor:c @0 @1) @0)
6522 (cmp @1 { build_zero_cst (TREE_TYPE (@1)); }))
6524 /* (X & Y) == X becomes (X & ~Y) == 0. */
6526 (cmp:c (bit_and:c @0 @1) @0)
6527 (cmp (bit_and @0 (bit_not! @1)) { build_zero_cst (TREE_TYPE (@0)); }))
6529 (cmp:c (convert@3 (bit_and (convert@2 @0) INTEGER_CST@1)) (convert @0))
6530 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6531 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
6532 && INTEGRAL_TYPE_P (TREE_TYPE (@3))
6533 && TYPE_PRECISION (TREE_TYPE (@2)) == TYPE_PRECISION (TREE_TYPE (@0))
6534 && TYPE_PRECISION (TREE_TYPE (@3)) > TYPE_PRECISION (TREE_TYPE (@2))
6535 && !wi::neg_p (wi::to_wide (@1)))
6536 (cmp (bit_and @0 (convert (bit_not @1)))
6537 { build_zero_cst (TREE_TYPE (@0)); })))
6539 /* (X | Y) == Y becomes (X & ~Y) == 0. */
6541 (cmp:c (bit_ior:c @0 @1) @1)
6542 (cmp (bit_and @0 (bit_not! @1)) { build_zero_cst (TREE_TYPE (@0)); }))
6544 /* (X ^ C1) op C2 can be rewritten as X op (C1 ^ C2). */
6546 (cmp (convert?@3 (bit_xor @0 INTEGER_CST@1)) INTEGER_CST@2)
6547 (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0)))
6548 (cmp @0 (bit_xor @1 (convert @2)))))
6551 (cmp (nop_convert? @0) integer_zerop)
6552 (if (tree_expr_nonzero_p (@0))
6553 { constant_boolean_node (cmp == NE_EXPR, type); }))
6555 /* (X & C) op (Y & C) into (X ^ Y) & C op 0. */
6557 (cmp (bit_and:cs @0 @2) (bit_and:cs @1 @2))
6558 (cmp (bit_and (bit_xor @0 @1) @2) { build_zero_cst (TREE_TYPE (@2)); })))
6560 /* (X < 0) != (Y < 0) into (X ^ Y) < 0.
6561 (X >= 0) != (Y >= 0) into (X ^ Y) < 0.
6562 (X < 0) == (Y < 0) into (X ^ Y) >= 0.
6563 (X >= 0) == (Y >= 0) into (X ^ Y) >= 0. */
6568 (cmp (sgncmp @0 integer_zerop@2) (sgncmp @1 integer_zerop))
6569 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
6570 && !TYPE_UNSIGNED (TREE_TYPE (@0))
6571 && types_match (@0, @1))
6572 (ncmp (bit_xor @0 @1) @2)))))
6573 /* (X < 0) == (Y >= 0) into (X ^ Y) < 0.
6574 (X < 0) != (Y >= 0) into (X ^ Y) >= 0. */
6578 (cmp:c (lt @0 integer_zerop@2) (ge @1 integer_zerop))
6579 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
6580 && !TYPE_UNSIGNED (TREE_TYPE (@0))
6581 && types_match (@0, @1))
6582 (ncmp (bit_xor @0 @1) @2))))
6584 /* If we have (A & C) == C where C is a power of 2, convert this into
6585 (A & C) != 0. Similarly for NE_EXPR. */
6589 (cmp (bit_and@2 @0 integer_pow2p@1) @1)
6590 (icmp @2 { build_zero_cst (TREE_TYPE (@0)); })))
6593 /* From fold_binary_op_with_conditional_arg handle the case of
6594 rewriting (a ? b : c) > d to a ? (b > d) : (c > d) when the
6595 compares simplify. */
6596 (for cmp (simple_comparison)
6598 (cmp:c (cond @0 @1 @2) @3)
6599 /* Do not move possibly trapping operations into the conditional as this
6600 pessimizes code and causes gimplification issues when applied late. */
6601 (if (!FLOAT_TYPE_P (TREE_TYPE (@3))
6602 || !operation_could_trap_p (cmp, true, false, @3))
6603 (cond @0 (cmp! @1 @3) (cmp! @2 @3)))))
6607 /* x < 0 ? ~y : y into (x >> (prec-1)) ^ y. */
6608 /* x >= 0 ? ~y : y into ~((x >> (prec-1)) ^ y). */
6610 (cond (cmp @0 integer_zerop) (bit_not @1) @1)
6611 (if (INTEGRAL_TYPE_P (type)
6612 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
6613 && !TYPE_UNSIGNED (TREE_TYPE (@0))
6614 && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (type))
6617 tree shifter = build_int_cst (integer_type_node, TYPE_PRECISION (type) - 1);
6619 (if (cmp == LT_EXPR)
6620 (bit_xor (convert (rshift @0 {shifter;})) @1)
6621 (bit_not (bit_xor (convert (rshift @0 {shifter;})) @1))))))
6622 /* x < 0 ? y : ~y into ~((x >> (prec-1)) ^ y). */
6623 /* x >= 0 ? y : ~y into (x >> (prec-1)) ^ y. */
6625 (cond (cmp @0 integer_zerop) @1 (bit_not @1))
6626 (if (INTEGRAL_TYPE_P (type)
6627 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
6628 && !TYPE_UNSIGNED (TREE_TYPE (@0))
6629 && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (type))
6632 tree shifter = build_int_cst (integer_type_node, TYPE_PRECISION (type) - 1);
6634 (if (cmp == GE_EXPR)
6635 (bit_xor (convert (rshift @0 {shifter;})) @1)
6636 (bit_not (bit_xor (convert (rshift @0 {shifter;})) @1)))))))
6638 /* If we have (A & C) != 0 ? D : 0 where C and D are powers of 2,
6639 convert this into a shift followed by ANDing with D. */
6642 (ne (bit_and @0 integer_pow2p@1) integer_zerop)
6643 INTEGER_CST@2 integer_zerop)
6644 (if (!POINTER_TYPE_P (type) && integer_pow2p (@2))
6646 int shift = (wi::exact_log2 (wi::to_wide (@2))
6647 - wi::exact_log2 (wi::to_wide (@1)));
6651 (lshift (convert @0) { build_int_cst (integer_type_node, shift); }) @2)
6653 (convert (rshift @0 { build_int_cst (integer_type_node, -shift); }))
6656 /* If we have (A & C) != 0 where C is the sign bit of A, convert
6657 this into A < 0. Similarly for (A & C) == 0 into A >= 0. */
6661 (cmp (bit_and (convert?@2 @0) integer_pow2p@1) integer_zerop)
6662 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6663 && type_has_mode_precision_p (TREE_TYPE (@0))
6664 && element_precision (@2) >= element_precision (@0)
6665 && wi::only_sign_bit_p (wi::to_wide (@1), element_precision (@0)))
6666 (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
6667 (ncmp (convert:stype @0) { build_zero_cst (stype); })))))
6669 /* If we have A < 0 ? C : 0 where C is a power of 2, convert
6670 this into a right shift or sign extension followed by ANDing with C. */
6673 (lt @0 integer_zerop)
6674 INTEGER_CST@1 integer_zerop)
6675 (if (integer_pow2p (@1)
6676 && !TYPE_UNSIGNED (TREE_TYPE (@0)))
6678 int shift = element_precision (@0) - wi::exact_log2 (wi::to_wide (@1)) - 1;
6682 (convert (rshift @0 { build_int_cst (integer_type_node, shift); }))
6684 /* Otherwise ctype must be wider than TREE_TYPE (@0) and pure
6685 sign extension followed by AND with C will achieve the effect. */
6686 (bit_and (convert @0) @1)))))
6688 /* When the addresses are not directly of decls compare base and offset.
6689 This implements some remaining parts of fold_comparison address
6690 comparisons but still no complete part of it. Still it is good
6691 enough to make fold_stmt not regress when not dispatching to fold_binary. */
6692 (for cmp (simple_comparison)
6694 (cmp (convert1?@2 addr@0) (convert2? addr@1))
6697 poly_int64 off0, off1;
6699 int equal = address_compare (cmp, TREE_TYPE (@2), @0, @1, base0, base1,
6700 off0, off1, GENERIC);
6704 (if (cmp == EQ_EXPR && (known_eq (off0, off1) || known_ne (off0, off1)))
6705 { constant_boolean_node (known_eq (off0, off1), type); })
6706 (if (cmp == NE_EXPR && (known_eq (off0, off1) || known_ne (off0, off1)))
6707 { constant_boolean_node (known_ne (off0, off1), type); })
6708 (if (cmp == LT_EXPR && (known_lt (off0, off1) || known_ge (off0, off1)))
6709 { constant_boolean_node (known_lt (off0, off1), type); })
6710 (if (cmp == LE_EXPR && (known_le (off0, off1) || known_gt (off0, off1)))
6711 { constant_boolean_node (known_le (off0, off1), type); })
6712 (if (cmp == GE_EXPR && (known_ge (off0, off1) || known_lt (off0, off1)))
6713 { constant_boolean_node (known_ge (off0, off1), type); })
6714 (if (cmp == GT_EXPR && (known_gt (off0, off1) || known_le (off0, off1)))
6715 { constant_boolean_node (known_gt (off0, off1), type); }))
6718 (if (cmp == EQ_EXPR)
6719 { constant_boolean_node (false, type); })
6720 (if (cmp == NE_EXPR)
6721 { constant_boolean_node (true, type); })))))))
6724 /* a?~t:t -> (-(a))^t */
6727 (with { bool wascmp; }
6728 (if (INTEGRAL_TYPE_P (type)
6729 && bitwise_inverted_equal_p (@1, @2, wascmp)
6730 && (!wascmp || element_precision (type) == 1))
6732 auto prec = TYPE_PRECISION (type);
6733 auto unsign = TYPE_UNSIGNED (type);
6734 tree inttype = build_nonstandard_integer_type (prec, unsign);
6736 (convert (bit_xor (negate (convert:inttype @0)) (convert:inttype @2)))))))
6739 /* Simplify pointer equality compares using PTA. */
6743 (if (POINTER_TYPE_P (TREE_TYPE (@0))
6744 && ptrs_compare_unequal (@0, @1))
6745 { constant_boolean_node (neeq != EQ_EXPR, type); })))
6747 /* PR70920: Transform (intptr_t)x eq/ne CST to x eq/ne (typeof x) CST.
6748 and (typeof ptr_cst) x eq/ne ptr_cst to x eq/ne (typeof x) CST.
6749 Disable the transform if either operand is pointer to function.
6750 This broke pr22051-2.c for arm where function pointer
6751 canonicalizaion is not wanted. */
6755 (cmp (convert @0) INTEGER_CST@1)
6756 (if (((POINTER_TYPE_P (TREE_TYPE (@0))
6757 && !FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@0)))
6758 && INTEGRAL_TYPE_P (TREE_TYPE (@1))
6759 /* Don't perform this optimization in GENERIC if @0 has reference
6760 type when sanitizing. See PR101210. */
6762 && TREE_CODE (TREE_TYPE (@0)) == REFERENCE_TYPE
6763 && (flag_sanitize & (SANITIZE_NULL | SANITIZE_ALIGNMENT))))
6764 || (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6765 && POINTER_TYPE_P (TREE_TYPE (@1))
6766 && !FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@1)))))
6767 && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
6768 (cmp @0 (convert @1)))))
6770 /* Non-equality compare simplifications from fold_binary */
6771 (for cmp (lt gt le ge)
6772 /* Comparisons with the highest or lowest possible integer of
6773 the specified precision will have known values. */
6775 (cmp (convert?@2 @0) uniform_integer_cst_p@1)
6776 (if ((INTEGRAL_TYPE_P (TREE_TYPE (@1))
6777 || POINTER_TYPE_P (TREE_TYPE (@1))
6778 || VECTOR_INTEGER_TYPE_P (TREE_TYPE (@1)))
6779 && tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@0)))
6782 tree cst = uniform_integer_cst_p (@1);
6783 tree arg1_type = TREE_TYPE (cst);
6784 unsigned int prec = TYPE_PRECISION (arg1_type);
6785 wide_int max = wi::max_value (arg1_type);
6786 wide_int signed_max = wi::max_value (prec, SIGNED);
6787 wide_int min = wi::min_value (arg1_type);
6790 (if (wi::to_wide (cst) == max)
6792 (if (cmp == GT_EXPR)
6793 { constant_boolean_node (false, type); })
6794 (if (cmp == GE_EXPR)
6796 (if (cmp == LE_EXPR)
6797 { constant_boolean_node (true, type); })
6798 (if (cmp == LT_EXPR)
6800 (if (wi::to_wide (cst) == min)
6802 (if (cmp == LT_EXPR)
6803 { constant_boolean_node (false, type); })
6804 (if (cmp == LE_EXPR)
6806 (if (cmp == GE_EXPR)
6807 { constant_boolean_node (true, type); })
6808 (if (cmp == GT_EXPR)
6810 (if (wi::to_wide (cst) == max - 1)
6812 (if (cmp == GT_EXPR)
6813 (eq @2 { build_uniform_cst (TREE_TYPE (@1),
6814 wide_int_to_tree (TREE_TYPE (cst),
6817 (if (cmp == LE_EXPR)
6818 (ne @2 { build_uniform_cst (TREE_TYPE (@1),
6819 wide_int_to_tree (TREE_TYPE (cst),
6822 (if (wi::to_wide (cst) == min + 1)
6824 (if (cmp == GE_EXPR)
6825 (ne @2 { build_uniform_cst (TREE_TYPE (@1),
6826 wide_int_to_tree (TREE_TYPE (cst),
6829 (if (cmp == LT_EXPR)
6830 (eq @2 { build_uniform_cst (TREE_TYPE (@1),
6831 wide_int_to_tree (TREE_TYPE (cst),
6834 (if (wi::to_wide (cst) == signed_max
6835 && TYPE_UNSIGNED (arg1_type)
6836 && TYPE_MODE (arg1_type) != BLKmode
6837 /* We will flip the signedness of the comparison operator
6838 associated with the mode of @1, so the sign bit is
6839 specified by this mode. Check that @1 is the signed
6840 max associated with this sign bit. */
6841 && prec == GET_MODE_PRECISION (SCALAR_INT_TYPE_MODE (arg1_type))
6842 /* signed_type does not work on pointer types. */
6843 && INTEGRAL_TYPE_P (arg1_type))
6844 /* The following case also applies to X < signed_max+1
6845 and X >= signed_max+1 because previous transformations. */
6846 (if (cmp == LE_EXPR || cmp == GT_EXPR)
6847 (with { tree st = signed_type_for (TREE_TYPE (@1)); }
6849 (if (cst == @1 && cmp == LE_EXPR)
6850 (ge (convert:st @0) { build_zero_cst (st); }))
6851 (if (cst == @1 && cmp == GT_EXPR)
6852 (lt (convert:st @0) { build_zero_cst (st); }))
6853 (if (cmp == LE_EXPR)
6854 (ge (view_convert:st @0) { build_zero_cst (st); }))
6855 (if (cmp == GT_EXPR)
6856 (lt (view_convert:st @0) { build_zero_cst (st); })))))))))))
6858 /* unsigned < (typeof unsigned)(unsigned != 0) is always false. */
6860 (lt:c @0 (convert (ne @0 integer_zerop)))
6861 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
6862 { constant_boolean_node (false, type); }))
6864 /* x != (typeof x)(x == CST) -> CST == 0 ? 1 : (CST == 1 ? (x!=0&&x!=1) : x != 0) */
6865 /* x != (typeof x)(x != CST) -> CST == 1 ? 1 : (CST == 0 ? (x!=0&&x!=1) : x != 1) */
6866 /* x == (typeof x)(x == CST) -> CST == 0 ? 0 : (CST == 1 ? (x==0||x==1) : x == 0) */
6867 /* x == (typeof x)(x != CST) -> CST == 1 ? 0 : (CST == 0 ? (x==0||x==1) : x == 1) */
6871 (outer:c @0 (convert (inner @0 INTEGER_CST@1)))
6873 bool cst1 = integer_onep (@1);
6874 bool cst0 = integer_zerop (@1);
6875 bool innereq = inner == EQ_EXPR;
6876 bool outereq = outer == EQ_EXPR;
6879 (if (innereq ? cst0 : cst1)
6880 { constant_boolean_node (!outereq, type); })
6881 (if (innereq ? cst1 : cst0)
6883 tree utype = unsigned_type_for (TREE_TYPE (@0));
6884 tree ucst1 = build_one_cst (utype);
6887 (gt (convert:utype @0) { ucst1; })
6888 (le (convert:utype @0) { ucst1; })
6893 tree value = build_int_cst (TREE_TYPE (@0), !innereq);
6906 (for cmp (unordered ordered unlt unle ungt unge uneq ltgt)
6907 /* If the second operand is NaN, the result is constant. */
6910 (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
6911 && (cmp != LTGT_EXPR || ! flag_trapping_math))
6912 { constant_boolean_node (cmp == ORDERED_EXPR || cmp == LTGT_EXPR
6913 ? false : true, type); })))
6915 /* Fold UNORDERED if either operand must be NaN, or neither can be. */
6919 (if (tree_expr_nan_p (@0) || tree_expr_nan_p (@1))
6920 { constant_boolean_node (true, type); })
6921 (if (!tree_expr_maybe_nan_p (@0) && !tree_expr_maybe_nan_p (@1))
6922 { constant_boolean_node (false, type); })))
6924 /* Fold ORDERED if either operand must be NaN, or neither can be. */
6928 (if (tree_expr_nan_p (@0) || tree_expr_nan_p (@1))
6929 { constant_boolean_node (false, type); })
6930 (if (!tree_expr_maybe_nan_p (@0) && !tree_expr_maybe_nan_p (@1))
6931 { constant_boolean_node (true, type); })))
6933 /* bool_var != 0 becomes bool_var. */
6935 (ne @0 integer_zerop)
6936 (if (TREE_CODE (TREE_TYPE (@0)) == BOOLEAN_TYPE
6937 && types_match (type, TREE_TYPE (@0)))
6939 /* bool_var == 1 becomes bool_var. */
6941 (eq @0 integer_onep)
6942 (if (TREE_CODE (TREE_TYPE (@0)) == BOOLEAN_TYPE
6943 && types_match (type, TREE_TYPE (@0)))
6946 bool_var == 0 becomes !bool_var or
6947 bool_var != 1 becomes !bool_var
6948 here because that only is good in assignment context as long
6949 as we require a tcc_comparison in GIMPLE_CONDs where we'd
6950 replace if (x == 0) with tem = ~x; if (tem != 0) which is
6951 clearly less optimal and which we'll transform again in forwprop. */
6953 /* Transform comparisons of the form (X & Y) CMP 0 to X CMP2 Z
6954 where ~Y + 1 == pow2 and Z = ~Y. */
6955 (for cst (VECTOR_CST INTEGER_CST)
6959 (cmp (bit_and:c@2 @0 cst@1) integer_zerop)
6960 (with { tree csts = bitmask_inv_cst_vector_p (@1); }
6961 (if (csts && (VECTOR_TYPE_P (TREE_TYPE (@1)) || single_use (@2)))
6962 (with { auto optab = VECTOR_TYPE_P (TREE_TYPE (@1))
6963 ? optab_vector : optab_default;
6964 tree utype = unsigned_type_for (TREE_TYPE (@1)); }
6965 (if (target_supports_op_p (utype, icmp, optab)
6966 || (optimize_vectors_before_lowering_p ()
6967 && (!target_supports_op_p (type, cmp, optab)
6968 || !target_supports_op_p (type, BIT_AND_EXPR, optab))))
6969 (if (TYPE_UNSIGNED (TREE_TYPE (@1)))
6971 (icmp (view_convert:utype @0) { csts; })))))))))
6973 /* When one argument is a constant, overflow detection can be simplified.
6974 Currently restricted to single use so as not to interfere too much with
6975 ADD_OVERFLOW detection in tree-ssa-math-opts.cc.
6976 CONVERT?(CONVERT?(A) + CST) CMP A -> A CMP' CST' */
6977 (for cmp (lt le ge gt)
6980 (cmp:c (convert?@3 (plus@2 (convert?@4 @0) INTEGER_CST@1)) @0)
6981 (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@2))
6982 && types_match (TREE_TYPE (@0), TREE_TYPE (@3))
6983 && tree_nop_conversion_p (TREE_TYPE (@4), TREE_TYPE (@0))
6984 && wi::to_wide (@1) != 0
6987 unsigned int prec = TYPE_PRECISION (TREE_TYPE (@0));
6988 signop sign = TYPE_SIGN (TREE_TYPE (@0));
6990 (out @0 { wide_int_to_tree (TREE_TYPE (@0),
6991 wi::max_value (prec, sign)
6992 - wi::to_wide (@1)); })))))
6994 /* To detect overflow in unsigned A - B, A < B is simpler than A - B > A.
6995 However, the detection logic for SUB_OVERFLOW in tree-ssa-math-opts.cc
6996 expects the long form, so we restrict the transformation for now. */
6999 (cmp:c (minus@2 @0 @1) @0)
7000 (if (single_use (@2)
7001 && ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
7002 && TYPE_UNSIGNED (TREE_TYPE (@0)))
7005 /* Optimize A - B + -1 >= A into B >= A for unsigned comparisons. */
7008 (cmp:c (plus (minus @0 @1) integer_minus_onep) @0)
7009 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
7010 && TYPE_UNSIGNED (TREE_TYPE (@0)))
7013 /* Testing for overflow is unnecessary if we already know the result. */
7018 (cmp:c (realpart (IFN_SUB_OVERFLOW@2 @0 @1)) @0)
7019 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
7020 && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
7021 (out (imagpart @2) { build_zero_cst (TREE_TYPE (@0)); }))))
7026 (cmp:c (realpart (IFN_ADD_OVERFLOW:c@2 @0 @1)) @0)
7027 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
7028 && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
7029 (out (imagpart @2) { build_zero_cst (TREE_TYPE (@0)); }))))
7031 /* For unsigned operands, -1 / B < A checks whether A * B would overflow.
7032 Simplify it to __builtin_mul_overflow (A, B, <unused>). */
7036 (cmp:c (trunc_div:s integer_all_onesp @1) @0)
7037 (if (TYPE_UNSIGNED (TREE_TYPE (@0)) && !VECTOR_TYPE_P (TREE_TYPE (@0)))
7038 (with { tree t = TREE_TYPE (@0), cpx = build_complex_type (t); }
7039 (out (imagpart (IFN_MUL_OVERFLOW:cpx @0 @1)) { build_zero_cst (t); })))))
7041 /* Similarly, for unsigned operands, (((type) A * B) >> prec) != 0 where type
7042 is at least twice as wide as type of A and B, simplify to
7043 __builtin_mul_overflow (A, B, <unused>). */
7046 (cmp (rshift (mult:s (convert@3 @0) (convert @1)) INTEGER_CST@2)
7048 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
7049 && INTEGRAL_TYPE_P (TREE_TYPE (@3))
7050 && TYPE_UNSIGNED (TREE_TYPE (@0))
7051 && (TYPE_PRECISION (TREE_TYPE (@3))
7052 >= 2 * TYPE_PRECISION (TREE_TYPE (@0)))
7053 && tree_fits_uhwi_p (@2)
7054 && tree_to_uhwi (@2) == TYPE_PRECISION (TREE_TYPE (@0))
7055 && types_match (@0, @1)
7056 && type_has_mode_precision_p (TREE_TYPE (@0))
7057 && (optab_handler (umulv4_optab, TYPE_MODE (TREE_TYPE (@0)))
7058 != CODE_FOR_nothing))
7059 (with { tree t = TREE_TYPE (@0), cpx = build_complex_type (t); }
7060 (cmp (imagpart (IFN_MUL_OVERFLOW:cpx @0 @1)) { build_zero_cst (t); })))))
7062 /* Demote operands of IFN_{ADD,SUB,MUL}_OVERFLOW. */
7063 (for ovf (IFN_ADD_OVERFLOW IFN_SUB_OVERFLOW IFN_MUL_OVERFLOW)
7065 (ovf (convert@2 @0) @1)
7066 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
7067 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
7068 && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
7069 && (!TYPE_UNSIGNED (TREE_TYPE (@2)) || TYPE_UNSIGNED (TREE_TYPE (@0))))
7072 (ovf @1 (convert@2 @0))
7073 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
7074 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
7075 && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
7076 && (!TYPE_UNSIGNED (TREE_TYPE (@2)) || TYPE_UNSIGNED (TREE_TYPE (@0))))
7079 /* Optimize __builtin_mul_overflow_p (x, cst, (utype) 0) if all 3 types
7080 are unsigned to x > (umax / cst). Similarly for signed type, but
7081 in that case it needs to be outside of a range. */
7083 (imagpart (IFN_MUL_OVERFLOW:cs@2 @0 integer_nonzerop@1))
7084 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
7085 && TYPE_MAX_VALUE (TREE_TYPE (@0))
7086 && types_match (TREE_TYPE (@0), TREE_TYPE (TREE_TYPE (@2)))
7087 && int_fits_type_p (@1, TREE_TYPE (@0)))
7088 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
7089 (convert (gt @0 (trunc_div! { TYPE_MAX_VALUE (TREE_TYPE (@0)); } @1)))
7090 (if (TYPE_MIN_VALUE (TREE_TYPE (@0)))
7091 (if (integer_minus_onep (@1))
7092 (convert (eq @0 { TYPE_MIN_VALUE (TREE_TYPE (@0)); }))
7095 tree div = fold_convert (TREE_TYPE (@0), @1);
7096 tree lo = int_const_binop (TRUNC_DIV_EXPR,
7097 TYPE_MIN_VALUE (TREE_TYPE (@0)), div);
7098 tree hi = int_const_binop (TRUNC_DIV_EXPR,
7099 TYPE_MAX_VALUE (TREE_TYPE (@0)), div);
7100 tree etype = range_check_type (TREE_TYPE (@0));
7103 if (wi::neg_p (wi::to_wide (div)))
7105 lo = fold_convert (etype, lo);
7106 hi = fold_convert (etype, hi);
7107 hi = int_const_binop (MINUS_EXPR, hi, lo);
7111 (convert (gt (minus (convert:etype @0) { lo; }) { hi; })))))))))
7113 /* Simplification of math builtins. These rules must all be optimizations
7114 as well as IL simplifications. If there is a possibility that the new
7115 form could be a pessimization, the rule should go in the canonicalization
7116 section that follows this one.
7118 Rules can generally go in this section if they satisfy one of
7121 - the rule describes an identity
7123 - the rule replaces calls with something as simple as addition or
7126 - the rule contains unary calls only and simplifies the surrounding
7127 arithmetic. (The idea here is to exclude non-unary calls in which
7128 one operand is constant and in which the call is known to be cheap
7129 when the operand has that value.) */
7131 (if (flag_unsafe_math_optimizations)
7132 /* Simplify sqrt(x) * sqrt(x) -> x. */
7134 (mult (SQRT_ALL@1 @0) @1)
7135 (if (!tree_expr_maybe_signaling_nan_p (@0))
7138 (for op (plus minus)
7139 /* Simplify (A / C) +- (B / C) -> (A +- B) / C. */
7143 (rdiv (op @0 @2) @1)))
7145 (for cmp (lt le gt ge)
7146 neg_cmp (gt ge lt le)
7147 /* Simplify (x * C1) cmp C2 -> x cmp (C2 / C1), where C1 != 0. */
7149 (cmp (mult @0 REAL_CST@1) REAL_CST@2)
7151 { tree tem = const_binop (RDIV_EXPR, type, @2, @1); }
7153 && !(REAL_VALUE_ISINF (TREE_REAL_CST (tem))
7154 || (real_zerop (tem) && !real_zerop (@1))))
7156 (if (real_less (&dconst0, TREE_REAL_CST_PTR (@1)))
7158 (if (real_less (TREE_REAL_CST_PTR (@1), &dconst0))
7159 (neg_cmp @0 { tem; })))))))
7161 /* Simplify sqrt(x) * sqrt(y) -> sqrt(x*y). */
7162 (for root (SQRT CBRT)
7164 (mult (root:s @0) (root:s @1))
7165 (root (mult @0 @1))))
7167 /* Simplify expN(x) * expN(y) -> expN(x+y). */
7168 (for exps (EXP EXP2 EXP10 POW10)
7170 (mult (exps:s @0) (exps:s @1))
7171 (exps (plus @0 @1))))
7173 /* Simplify a/root(b/c) into a*root(c/b). */
7174 (for root (SQRT CBRT)
7176 (rdiv @0 (root:s (rdiv:s @1 @2)))
7177 (mult @0 (root (rdiv @2 @1)))))
7179 /* Simplify x/expN(y) into x*expN(-y). */
7180 (for exps (EXP EXP2 EXP10 POW10)
7182 (rdiv @0 (exps:s @1))
7183 (mult @0 (exps (negate @1)))))
7185 (for logs (LOG LOG2 LOG10 LOG10)
7186 exps (EXP EXP2 EXP10 POW10)
7187 /* logN(expN(x)) -> x. */
7191 /* expN(logN(x)) -> x. */
7196 /* Optimize logN(func()) for various exponential functions. We
7197 want to determine the value "x" and the power "exponent" in
7198 order to transform logN(x**exponent) into exponent*logN(x). */
7199 (for logs (LOG LOG LOG LOG2 LOG2 LOG2 LOG10 LOG10)
7200 exps (EXP2 EXP10 POW10 EXP EXP10 POW10 EXP EXP2)
7203 (if (SCALAR_FLOAT_TYPE_P (type))
7209 /* Prepare to do logN(exp(exponent)) -> exponent*logN(e). */
7210 x = build_real_truncate (type, dconst_e ());
7213 /* Prepare to do logN(exp2(exponent)) -> exponent*logN(2). */
7214 x = build_real (type, dconst2);
7218 /* Prepare to do logN(exp10(exponent)) -> exponent*logN(10). */
7220 REAL_VALUE_TYPE dconst10;
7221 real_from_integer (&dconst10, VOIDmode, 10, SIGNED);
7222 x = build_real (type, dconst10);
7229 (mult (logs { x; }) @0)))))
7237 (if (SCALAR_FLOAT_TYPE_P (type))
7243 /* Prepare to do logN(sqrt(x)) -> 0.5*logN(x). */
7244 x = build_real (type, dconsthalf);
7247 /* Prepare to do logN(cbrt(x)) -> (1/3)*logN(x). */
7248 x = build_real_truncate (type, dconst_third ());
7254 (mult { x; } (logs @0))))))
7256 /* logN(pow(x,exponent)) -> exponent*logN(x). */
7257 (for logs (LOG LOG2 LOG10)
7261 (mult @1 (logs @0))))
7263 /* pow(C,x) -> exp(log(C)*x) if C > 0,
7264 or if C is a positive power of 2,
7265 pow(C,x) -> exp2(log2(C)*x). */
7273 (pows REAL_CST@0 @1)
7274 (if (real_compare (GT_EXPR, TREE_REAL_CST_PTR (@0), &dconst0)
7275 && real_isfinite (TREE_REAL_CST_PTR (@0))
7276 /* As libmvec doesn't have a vectorized exp2, defer optimizing
7277 the use_exp2 case until after vectorization. It seems actually
7278 beneficial for all constants to postpone this until later,
7279 because exp(log(C)*x), while faster, will have worse precision
7280 and if x folds into a constant too, that is unnecessary
7282 && canonicalize_math_after_vectorization_p ())
7284 const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (@0);
7285 bool use_exp2 = false;
7286 if (targetm.libc_has_function (function_c99_misc, TREE_TYPE (@0))
7287 && value->cl == rvc_normal)
7289 REAL_VALUE_TYPE frac_rvt = *value;
7290 SET_REAL_EXP (&frac_rvt, 1);
7291 if (real_equal (&frac_rvt, &dconst1))
7296 (if (optimize_pow_to_exp (@0, @1))
7297 (exps (mult (logs @0) @1)))
7298 (exp2s (mult (log2s @0) @1)))))))
7301 /* pow(C,x)*expN(y) -> expN(logN(C)*x+y) if C > 0. */
7303 exps (EXP EXP2 EXP10 POW10)
7304 logs (LOG LOG2 LOG10 LOG10)
7306 (mult:c (pows:s REAL_CST@0 @1) (exps:s @2))
7307 (if (real_compare (GT_EXPR, TREE_REAL_CST_PTR (@0), &dconst0)
7308 && real_isfinite (TREE_REAL_CST_PTR (@0)))
7309 (exps (plus (mult (logs @0) @1) @2)))))
7314 exps (EXP EXP2 EXP10 POW10)
7315 /* sqrt(expN(x)) -> expN(x*0.5). */
7318 (exps (mult @0 { build_real (type, dconsthalf); })))
7319 /* cbrt(expN(x)) -> expN(x/3). */
7322 (exps (mult @0 { build_real_truncate (type, dconst_third ()); })))
7323 /* pow(expN(x), y) -> expN(x*y). */
7326 (exps (mult @0 @1))))
7328 /* tan(atan(x)) -> x. */
7335 /* Simplify sin(atan(x)) -> x / sqrt(x*x + 1). */
7339 copysigns (COPYSIGN)
7344 REAL_VALUE_TYPE r_cst;
7345 build_sinatan_real (&r_cst, type);
7346 tree t_cst = build_real (type, r_cst);
7347 tree t_one = build_one_cst (type);
7349 (if (SCALAR_FLOAT_TYPE_P (type))
7350 (cond (lt (abs @0) { t_cst; })
7351 (rdiv @0 (sqrts (plus (mult @0 @0) { t_one; })))
7352 (copysigns { t_one; } @0))))))
7354 /* Simplify cos(atan(x)) -> 1 / sqrt(x*x + 1). */
7358 copysigns (COPYSIGN)
7363 REAL_VALUE_TYPE r_cst;
7364 build_sinatan_real (&r_cst, type);
7365 tree t_cst = build_real (type, r_cst);
7366 tree t_one = build_one_cst (type);
7367 tree t_zero = build_zero_cst (type);
7369 (if (SCALAR_FLOAT_TYPE_P (type))
7370 (cond (lt (abs @0) { t_cst; })
7371 (rdiv { t_one; } (sqrts (plus (mult @0 @0) { t_one; })))
7372 (copysigns { t_zero; } @0))))))
7374 (if (!flag_errno_math)
7375 /* Simplify sinh(atanh(x)) -> x / sqrt((1 - x)*(1 + x)). */
7380 (sinhs (atanhs:s @0))
7381 (with { tree t_one = build_one_cst (type); }
7382 (rdiv @0 (sqrts (mult (minus { t_one; } @0) (plus { t_one; } @0)))))))
7384 /* Simplify cosh(atanh(x)) -> 1 / sqrt((1 - x)*(1 + x)) */
7389 (coshs (atanhs:s @0))
7390 (with { tree t_one = build_one_cst (type); }
7391 (rdiv { t_one; } (sqrts (mult (minus { t_one; } @0) (plus { t_one; } @0))))))))
7393 /* cabs(x+0i) or cabs(0+xi) -> abs(x). */
7395 (CABS (complex:C @0 real_zerop@1))
7398 /* trunc(trunc(x)) -> trunc(x), etc. */
7399 (for fns (TRUNC_ALL FLOOR_ALL CEIL_ALL ROUND_ALL NEARBYINT_ALL RINT_ALL)
7403 /* f(x) -> x if x is integer valued and f does nothing for such values. */
7404 (for fns (TRUNC_ALL FLOOR_ALL CEIL_ALL ROUND_ALL NEARBYINT_ALL RINT_ALL)
7406 (fns integer_valued_real_p@0)
7409 /* hypot(x,0) and hypot(0,x) -> abs(x). */
7411 (HYPOT:c @0 real_zerop@1)
7414 /* pow(1,x) -> 1. */
7416 (POW real_onep@0 @1)
7420 /* copysign(x,x) -> x. */
7421 (COPYSIGN_ALL @0 @0)
7425 /* copysign(x,-x) -> -x. */
7426 (COPYSIGN_ALL @0 (negate@1 @0))
7430 /* copysign(x,y) -> fabs(x) if y is nonnegative. */
7431 (COPYSIGN_ALL @0 tree_expr_nonnegative_p@1)
7434 (for scale (LDEXP SCALBN SCALBLN)
7435 /* ldexp(0, x) -> 0. */
7437 (scale real_zerop@0 @1)
7439 /* ldexp(x, 0) -> x. */
7441 (scale @0 integer_zerop@1)
7443 /* ldexp(x, y) -> x if x is +-Inf or NaN. */
7445 (scale REAL_CST@0 @1)
7446 (if (!real_isfinite (TREE_REAL_CST_PTR (@0)))
7449 /* Canonicalization of sequences of math builtins. These rules represent
7450 IL simplifications but are not necessarily optimizations.
7452 The sincos pass is responsible for picking "optimal" implementations
7453 of math builtins, which may be more complicated and can sometimes go
7454 the other way, e.g. converting pow into a sequence of sqrts.
7455 We only want to do these canonicalizations before the pass has run. */
7457 (if (flag_unsafe_math_optimizations && canonicalize_math_p ())
7458 /* Simplify tan(x) * cos(x) -> sin(x). */
7460 (mult:c (TAN:s @0) (COS:s @0))
7463 /* Simplify x * pow(x,c) -> pow(x,c+1). */
7465 (mult:c @0 (POW:s @0 REAL_CST@1))
7466 (if (!TREE_OVERFLOW (@1))
7467 (POW @0 (plus @1 { build_one_cst (type); }))))
7469 /* Simplify sin(x) / cos(x) -> tan(x). */
7471 (rdiv (SIN:s @0) (COS:s @0))
7474 /* Simplify sinh(x) / cosh(x) -> tanh(x). */
7476 (rdiv (SINH:s @0) (COSH:s @0))
7479 /* Simplify tanh (x) / sinh (x) -> 1.0 / cosh (x). */
7481 (rdiv (TANH:s @0) (SINH:s @0))
7482 (rdiv {build_one_cst (type);} (COSH @0)))
7484 /* Simplify cos(x) / sin(x) -> 1 / tan(x). */
7486 (rdiv (COS:s @0) (SIN:s @0))
7487 (rdiv { build_one_cst (type); } (TAN @0)))
7489 /* Simplify sin(x) / tan(x) -> cos(x). */
7491 (rdiv (SIN:s @0) (TAN:s @0))
7492 (if (! HONOR_NANS (@0)
7493 && ! HONOR_INFINITIES (@0))
7496 /* Simplify tan(x) / sin(x) -> 1.0 / cos(x). */
7498 (rdiv (TAN:s @0) (SIN:s @0))
7499 (if (! HONOR_NANS (@0)
7500 && ! HONOR_INFINITIES (@0))
7501 (rdiv { build_one_cst (type); } (COS @0))))
7503 /* Simplify pow(x,y) * pow(x,z) -> pow(x,y+z). */
7505 (mult (POW:s @0 @1) (POW:s @0 @2))
7506 (POW @0 (plus @1 @2)))
7508 /* Simplify pow(x,y) * pow(z,y) -> pow(x*z,y). */
7510 (mult (POW:s @0 @1) (POW:s @2 @1))
7511 (POW (mult @0 @2) @1))
7513 /* Simplify powi(x,y) * powi(z,y) -> powi(x*z,y). */
7515 (mult (POWI:s @0 @1) (POWI:s @2 @1))
7516 (POWI (mult @0 @2) @1))
7518 /* Simplify pow(x,c) / x -> pow(x,c-1). */
7520 (rdiv (POW:s @0 REAL_CST@1) @0)
7521 (if (!TREE_OVERFLOW (@1))
7522 (POW @0 (minus @1 { build_one_cst (type); }))))
7524 /* Simplify x / pow (y,z) -> x * pow(y,-z). */
7526 (rdiv @0 (POW:s @1 @2))
7527 (mult @0 (POW @1 (negate @2))))
7532 /* sqrt(sqrt(x)) -> pow(x,1/4). */
7535 (pows @0 { build_real (type, dconst_quarter ()); }))
7536 /* sqrt(cbrt(x)) -> pow(x,1/6). */
7539 (pows @0 { build_real_truncate (type, dconst_sixth ()); }))
7540 /* cbrt(sqrt(x)) -> pow(x,1/6). */
7543 (pows @0 { build_real_truncate (type, dconst_sixth ()); }))
7544 /* cbrt(cbrt(x)) -> pow(x,1/9), iff x is nonnegative. */
7546 (cbrts (cbrts tree_expr_nonnegative_p@0))
7547 (pows @0 { build_real_truncate (type, dconst_ninth ()); }))
7548 /* sqrt(pow(x,y)) -> pow(|x|,y*0.5). */
7550 (sqrts (pows @0 @1))
7551 (pows (abs @0) (mult @1 { build_real (type, dconsthalf); })))
7552 /* cbrt(pow(x,y)) -> pow(x,y/3), iff x is nonnegative. */
7554 (cbrts (pows tree_expr_nonnegative_p@0 @1))
7555 (pows @0 (mult @1 { build_real_truncate (type, dconst_third ()); })))
7556 /* pow(sqrt(x),y) -> pow(x,y*0.5). */
7558 (pows (sqrts @0) @1)
7559 (pows @0 (mult @1 { build_real (type, dconsthalf); })))
7560 /* pow(cbrt(x),y) -> pow(x,y/3) iff x is nonnegative. */
7562 (pows (cbrts tree_expr_nonnegative_p@0) @1)
7563 (pows @0 (mult @1 { build_real_truncate (type, dconst_third ()); })))
7564 /* pow(pow(x,y),z) -> pow(x,y*z) iff x is nonnegative. */
7566 (pows (pows tree_expr_nonnegative_p@0 @1) @2)
7567 (pows @0 (mult @1 @2))))
7569 /* cabs(x+xi) -> fabs(x)*sqrt(2). */
7571 (CABS (complex @0 @0))
7572 (mult (abs @0) { build_real_truncate (type, dconst_sqrt2 ()); }))
7574 /* hypot(x,x) -> fabs(x)*sqrt(2). */
7577 (mult (abs @0) { build_real_truncate (type, dconst_sqrt2 ()); }))
7579 /* cexp(x+yi) -> exp(x)*cexpi(y). */
7584 (cexps compositional_complex@0)
7585 (if (targetm.libc_has_function (function_c99_math_complex, TREE_TYPE (@0)))
7587 (mult (exps@1 (realpart @0)) (realpart (cexpis:type@2 (imagpart @0))))
7588 (mult @1 (imagpart @2)))))))
7590 (if (canonicalize_math_p ())
7591 /* floor(x) -> trunc(x) if x is nonnegative. */
7592 (for floors (FLOOR_ALL)
7595 (floors tree_expr_nonnegative_p@0)
7598 (match double_value_p
7600 (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == double_type_node)))
7601 (for froms (BUILT_IN_TRUNCL
7613 /* truncl(extend(x)) -> extend(trunc(x)), etc., if x is a double. */
7614 (if (optimize && canonicalize_math_p ())
7616 (froms (convert double_value_p@0))
7617 (convert (tos @0)))))
7619 (match float_value_p
7621 (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == float_type_node)))
7622 (for froms (BUILT_IN_TRUNCL BUILT_IN_TRUNC
7623 BUILT_IN_FLOORL BUILT_IN_FLOOR
7624 BUILT_IN_CEILL BUILT_IN_CEIL
7625 BUILT_IN_ROUNDL BUILT_IN_ROUND
7626 BUILT_IN_NEARBYINTL BUILT_IN_NEARBYINT
7627 BUILT_IN_RINTL BUILT_IN_RINT)
7628 tos (BUILT_IN_TRUNCF BUILT_IN_TRUNCF
7629 BUILT_IN_FLOORF BUILT_IN_FLOORF
7630 BUILT_IN_CEILF BUILT_IN_CEILF
7631 BUILT_IN_ROUNDF BUILT_IN_ROUNDF
7632 BUILT_IN_NEARBYINTF BUILT_IN_NEARBYINTF
7633 BUILT_IN_RINTF BUILT_IN_RINTF)
7634 /* truncl(extend(x)) and trunc(extend(x)) -> extend(truncf(x)), etc.,
7636 (if (optimize && canonicalize_math_p ()
7637 && targetm.libc_has_function (function_c99_misc, NULL_TREE))
7639 (froms (convert float_value_p@0))
7640 (convert (tos @0)))))
7643 (match float16_value_p
7645 (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == float16_type_node)))
7646 (for froms (BUILT_IN_TRUNCL BUILT_IN_TRUNC BUILT_IN_TRUNCF
7647 BUILT_IN_FLOORL BUILT_IN_FLOOR BUILT_IN_FLOORF
7648 BUILT_IN_CEILL BUILT_IN_CEIL BUILT_IN_CEILF
7649 BUILT_IN_ROUNDEVENL BUILT_IN_ROUNDEVEN BUILT_IN_ROUNDEVENF
7650 BUILT_IN_ROUNDL BUILT_IN_ROUND BUILT_IN_ROUNDF
7651 BUILT_IN_NEARBYINTL BUILT_IN_NEARBYINT BUILT_IN_NEARBYINTF
7652 BUILT_IN_RINTL BUILT_IN_RINT BUILT_IN_RINTF
7653 BUILT_IN_SQRTL BUILT_IN_SQRT BUILT_IN_SQRTF)
7654 tos (IFN_TRUNC IFN_TRUNC IFN_TRUNC
7655 IFN_FLOOR IFN_FLOOR IFN_FLOOR
7656 IFN_CEIL IFN_CEIL IFN_CEIL
7657 IFN_ROUNDEVEN IFN_ROUNDEVEN IFN_ROUNDEVEN
7658 IFN_ROUND IFN_ROUND IFN_ROUND
7659 IFN_NEARBYINT IFN_NEARBYINT IFN_NEARBYINT
7660 IFN_RINT IFN_RINT IFN_RINT
7661 IFN_SQRT IFN_SQRT IFN_SQRT)
7662 /* (_Float16) round ((doube) x) -> __built_in_roundf16 (x), etc.,
7663 if x is a _Float16. */
7665 (convert (froms (convert float16_value_p@0)))
7667 && types_match (type, TREE_TYPE (@0))
7668 && direct_internal_fn_supported_p (as_internal_fn (tos),
7669 type, OPTIMIZE_FOR_BOTH))
7672 /* Simplify (trunc)copysign ((extend)x, (extend)y) to copysignf (x, y),
7673 x,y is float value, similar for _Float16/double. */
7674 (for copysigns (COPYSIGN_ALL)
7676 (convert (copysigns (convert@2 @0) (convert @1)))
7678 && !HONOR_SNANS (@2)
7679 && types_match (type, TREE_TYPE (@0))
7680 && types_match (type, TREE_TYPE (@1))
7681 && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (@2))
7682 && direct_internal_fn_supported_p (IFN_COPYSIGN,
7683 type, OPTIMIZE_FOR_BOTH))
7684 (IFN_COPYSIGN @0 @1))))
7686 (for froms (BUILT_IN_FMAF BUILT_IN_FMA BUILT_IN_FMAL)
7687 tos (IFN_FMA IFN_FMA IFN_FMA)
7689 (convert (froms (convert@3 @0) (convert @1) (convert @2)))
7690 (if (flag_unsafe_math_optimizations
7692 && FLOAT_TYPE_P (type)
7693 && FLOAT_TYPE_P (TREE_TYPE (@3))
7694 && types_match (type, TREE_TYPE (@0))
7695 && types_match (type, TREE_TYPE (@1))
7696 && types_match (type, TREE_TYPE (@2))
7697 && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (@3))
7698 && direct_internal_fn_supported_p (as_internal_fn (tos),
7699 type, OPTIMIZE_FOR_BOTH))
7702 (for maxmin (max min)
7704 (convert (maxmin (convert@2 @0) (convert @1)))
7706 && FLOAT_TYPE_P (type)
7707 && FLOAT_TYPE_P (TREE_TYPE (@2))
7708 && types_match (type, TREE_TYPE (@0))
7709 && types_match (type, TREE_TYPE (@1))
7710 && element_precision (type) < element_precision (TREE_TYPE (@2)))
7714 (for froms (XFLOORL XCEILL XROUNDL XRINTL)
7715 tos (XFLOOR XCEIL XROUND XRINT)
7716 /* llfloorl(extend(x)) -> llfloor(x), etc., if x is a double. */
7717 (if (optimize && canonicalize_math_p ())
7719 (froms (convert double_value_p@0))
7722 (for froms (XFLOORL XCEILL XROUNDL XRINTL
7723 XFLOOR XCEIL XROUND XRINT)
7724 tos (XFLOORF XCEILF XROUNDF XRINTF)
7725 /* llfloorl(extend(x)) and llfloor(extend(x)) -> llfloorf(x), etc.,
7727 (if (optimize && canonicalize_math_p ())
7729 (froms (convert float_value_p@0))
7732 (if (canonicalize_math_p ())
7733 /* xfloor(x) -> fix_trunc(x) if x is nonnegative. */
7734 (for floors (IFLOOR LFLOOR LLFLOOR)
7736 (floors tree_expr_nonnegative_p@0)
7739 (if (canonicalize_math_p ())
7740 /* xfloor(x) -> fix_trunc(x), etc., if x is integer valued. */
7741 (for fns (IFLOOR LFLOOR LLFLOOR
7743 IROUND LROUND LLROUND)
7745 (fns integer_valued_real_p@0)
7747 (if (!flag_errno_math)
7748 /* xrint(x) -> fix_trunc(x), etc., if x is integer valued. */
7749 (for rints (IRINT LRINT LLRINT)
7751 (rints integer_valued_real_p@0)
7754 (if (canonicalize_math_p ())
7755 (for ifn (IFLOOR ICEIL IROUND IRINT)
7756 lfn (LFLOOR LCEIL LROUND LRINT)
7757 llfn (LLFLOOR LLCEIL LLROUND LLRINT)
7758 /* Canonicalize iround (x) to lround (x) on ILP32 targets where
7759 sizeof (int) == sizeof (long). */
7760 (if (TYPE_PRECISION (integer_type_node)
7761 == TYPE_PRECISION (long_integer_type_node))
7764 (lfn:long_integer_type_node @0)))
7765 /* Canonicalize llround (x) to lround (x) on LP64 targets where
7766 sizeof (long long) == sizeof (long). */
7767 (if (TYPE_PRECISION (long_long_integer_type_node)
7768 == TYPE_PRECISION (long_integer_type_node))
7771 (lfn:long_integer_type_node @0)))))
7773 /* cproj(x) -> x if we're ignoring infinities. */
7776 (if (!HONOR_INFINITIES (type))
7779 /* If the real part is inf and the imag part is known to be
7780 nonnegative, return (inf + 0i). */
7782 (CPROJ (complex REAL_CST@0 tree_expr_nonnegative_p@1))
7783 (if (real_isinf (TREE_REAL_CST_PTR (@0)))
7784 { build_complex_inf (type, false); }))
7786 /* If the imag part is inf, return (inf+I*copysign(0,imag)). */
7788 (CPROJ (complex @0 REAL_CST@1))
7789 (if (real_isinf (TREE_REAL_CST_PTR (@1)))
7790 { build_complex_inf (type, TREE_REAL_CST_PTR (@1)->sign); }))
7796 (pows @0 REAL_CST@1)
7798 const REAL_VALUE_TYPE *value = TREE_REAL_CST_PTR (@1);
7799 REAL_VALUE_TYPE tmp;
7802 /* pow(x,0) -> 1. */
7803 (if (real_equal (value, &dconst0))
7804 { build_real (type, dconst1); })
7805 /* pow(x,1) -> x. */
7806 (if (real_equal (value, &dconst1))
7808 /* pow(x,-1) -> 1/x. */
7809 (if (real_equal (value, &dconstm1))
7810 (rdiv { build_real (type, dconst1); } @0))
7811 /* pow(x,0.5) -> sqrt(x). */
7812 (if (flag_unsafe_math_optimizations
7813 && canonicalize_math_p ()
7814 && real_equal (value, &dconsthalf))
7816 /* pow(x,1/3) -> cbrt(x). */
7817 (if (flag_unsafe_math_optimizations
7818 && canonicalize_math_p ()
7819 && (tmp = real_value_truncate (TYPE_MODE (type), dconst_third ()),
7820 real_equal (value, &tmp)))
7823 /* powi(1,x) -> 1. */
7825 (POWI real_onep@0 @1)
7829 (POWI @0 INTEGER_CST@1)
7831 /* powi(x,0) -> 1. */
7832 (if (wi::to_wide (@1) == 0)
7833 { build_real (type, dconst1); })
7834 /* powi(x,1) -> x. */
7835 (if (wi::to_wide (@1) == 1)
7837 /* powi(x,-1) -> 1/x. */
7838 (if (wi::to_wide (@1) == -1)
7839 (rdiv { build_real (type, dconst1); } @0))))
7841 /* Narrowing of arithmetic and logical operations.
7843 These are conceptually similar to the transformations performed for
7844 the C/C++ front-ends by shorten_binary_op and shorten_compare. Long
7845 term we want to move all that code out of the front-ends into here. */
7847 /* Convert (outertype)((innertype0)a+(innertype1)b)
7848 into ((newtype)a+(newtype)b) where newtype
7849 is the widest mode from all of these. */
7850 (for op (plus minus mult rdiv)
7852 (convert (op:s@0 (convert1?@3 @1) (convert2?@4 @2)))
7853 /* If we have a narrowing conversion of an arithmetic operation where
7854 both operands are widening conversions from the same type as the outer
7855 narrowing conversion. Then convert the innermost operands to a
7856 suitable unsigned type (to avoid introducing undefined behavior),
7857 perform the operation and convert the result to the desired type. */
7858 (if (INTEGRAL_TYPE_P (type)
7861 /* We check for type compatibility between @0 and @1 below,
7862 so there's no need to check that @2/@4 are integral types. */
7863 && INTEGRAL_TYPE_P (TREE_TYPE (@1))
7864 && INTEGRAL_TYPE_P (TREE_TYPE (@3))
7865 /* The precision of the type of each operand must match the
7866 precision of the mode of each operand, similarly for the
7868 && type_has_mode_precision_p (TREE_TYPE (@1))
7869 && type_has_mode_precision_p (TREE_TYPE (@2))
7870 && type_has_mode_precision_p (type)
7871 /* The inner conversion must be a widening conversion. */
7872 && TYPE_PRECISION (TREE_TYPE (@3)) > TYPE_PRECISION (TREE_TYPE (@1))
7873 && types_match (@1, type)
7874 && (types_match (@1, @2)
7875 /* Or the second operand is const integer or converted const
7876 integer from valueize. */
7877 || poly_int_tree_p (@4)))
7878 (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
7879 (op @1 (convert @2))
7880 (with { tree utype = unsigned_type_for (TREE_TYPE (@1)); }
7881 (convert (op (convert:utype @1)
7882 (convert:utype @2)))))
7883 (if (FLOAT_TYPE_P (type)
7884 && DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0))
7885 == DECIMAL_FLOAT_TYPE_P (type))
7886 (with { tree arg0 = strip_float_extensions (@1);
7887 tree arg1 = strip_float_extensions (@2);
7888 tree itype = TREE_TYPE (@0);
7889 tree ty1 = TREE_TYPE (arg0);
7890 tree ty2 = TREE_TYPE (arg1);
7891 enum tree_code code = TREE_CODE (itype); }
7892 (if (FLOAT_TYPE_P (ty1)
7893 && FLOAT_TYPE_P (ty2))
7894 (with { tree newtype = type;
7895 if (TYPE_MODE (ty1) == SDmode
7896 || TYPE_MODE (ty2) == SDmode
7897 || TYPE_MODE (type) == SDmode)
7898 newtype = dfloat32_type_node;
7899 if (TYPE_MODE (ty1) == DDmode
7900 || TYPE_MODE (ty2) == DDmode
7901 || TYPE_MODE (type) == DDmode)
7902 newtype = dfloat64_type_node;
7903 if (TYPE_MODE (ty1) == TDmode
7904 || TYPE_MODE (ty2) == TDmode
7905 || TYPE_MODE (type) == TDmode)
7906 newtype = dfloat128_type_node; }
7907 (if ((newtype == dfloat32_type_node
7908 || newtype == dfloat64_type_node
7909 || newtype == dfloat128_type_node)
7911 && types_match (newtype, type))
7912 (op (convert:newtype @1) (convert:newtype @2))
7913 (with { if (element_precision (ty1) > element_precision (newtype))
7915 if (element_precision (ty2) > element_precision (newtype))
7917 /* Sometimes this transformation is safe (cannot
7918 change results through affecting double rounding
7919 cases) and sometimes it is not. If NEWTYPE is
7920 wider than TYPE, e.g. (float)((long double)double
7921 + (long double)double) converted to
7922 (float)(double + double), the transformation is
7923 unsafe regardless of the details of the types
7924 involved; double rounding can arise if the result
7925 of NEWTYPE arithmetic is a NEWTYPE value half way
7926 between two representable TYPE values but the
7927 exact value is sufficiently different (in the
7928 right direction) for this difference to be
7929 visible in ITYPE arithmetic. If NEWTYPE is the
7930 same as TYPE, however, the transformation may be
7931 safe depending on the types involved: it is safe
7932 if the ITYPE has strictly more than twice as many
7933 mantissa bits as TYPE, can represent infinities
7934 and NaNs if the TYPE can, and has sufficient
7935 exponent range for the product or ratio of two
7936 values representable in the TYPE to be within the
7937 range of normal values of ITYPE. */
7938 (if (element_precision (newtype) < element_precision (itype)
7939 && (!VECTOR_MODE_P (TYPE_MODE (newtype))
7940 || target_supports_op_p (newtype, op, optab_default))
7941 && (flag_unsafe_math_optimizations
7942 || (element_precision (newtype) == element_precision (type)
7943 && real_can_shorten_arithmetic (element_mode (itype),
7944 element_mode (type))
7945 && !excess_precision_type (newtype)))
7946 && !types_match (itype, newtype))
7947 (convert:type (op (convert:newtype @1)
7948 (convert:newtype @2)))
7953 /* This is another case of narrowing, specifically when there's an outer
7954 BIT_AND_EXPR which masks off bits outside the type of the innermost
7955 operands. Like the previous case we have to convert the operands
7956 to unsigned types to avoid introducing undefined behavior for the
7957 arithmetic operation. */
7958 (for op (minus plus)
7960 (bit_and (op:s (convert@2 @0) (convert@3 @1)) INTEGER_CST@4)
7961 (if (INTEGRAL_TYPE_P (type)
7962 /* We check for type compatibility between @0 and @1 below,
7963 so there's no need to check that @1/@3 are integral types. */
7964 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
7965 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
7966 /* The precision of the type of each operand must match the
7967 precision of the mode of each operand, similarly for the
7969 && type_has_mode_precision_p (TREE_TYPE (@0))
7970 && type_has_mode_precision_p (TREE_TYPE (@1))
7971 && type_has_mode_precision_p (type)
7972 /* The inner conversion must be a widening conversion. */
7973 && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
7974 && types_match (@0, @1)
7975 && (tree_int_cst_min_precision (@4, TYPE_SIGN (TREE_TYPE (@0)))
7976 <= TYPE_PRECISION (TREE_TYPE (@0)))
7977 && (wi::to_wide (@4)
7978 & wi::mask (TYPE_PRECISION (TREE_TYPE (@0)),
7979 true, TYPE_PRECISION (type))) == 0)
7980 (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
7981 (with { tree ntype = TREE_TYPE (@0); }
7982 (convert (bit_and (op @0 @1) (convert:ntype @4))))
7983 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
7984 (convert (bit_and (op (convert:utype @0) (convert:utype @1))
7985 (convert:utype @4))))))))
7987 /* Transform (@0 < @1 and @0 < @2) to use min,
7988 (@0 > @1 and @0 > @2) to use max */
7989 (for logic (bit_and bit_and bit_and bit_and bit_ior bit_ior bit_ior bit_ior)
7990 op (lt le gt ge lt le gt ge )
7991 ext (min min max max max max min min )
7993 (logic (op:cs @0 @1) (op:cs @0 @2))
7994 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
7995 && TREE_CODE (@0) != INTEGER_CST)
7996 (op @0 (ext @1 @2)))))
7998 /* Max<bool0, bool1> -> bool0 | bool1
7999 Min<bool0, bool1> -> bool0 & bool1 */
8001 logic (bit_ior bit_and)
8003 (op zero_one_valued_p@0 zero_one_valued_p@1)
8006 /* signbit(x) != 0 ? -x : x -> abs(x)
8007 signbit(x) == 0 ? -x : x -> -abs(x) */
8011 (cond (neeq (sign @0) integer_zerop) (negate @0) @0)
8012 (if (neeq == NE_EXPR)
8014 (negate (abs @0))))))
8017 /* signbit(x) -> 0 if x is nonnegative. */
8018 (SIGNBIT tree_expr_nonnegative_p@0)
8019 { integer_zero_node; })
8022 /* signbit(x) -> x<0 if x doesn't have signed zeros. */
8024 (if (!HONOR_SIGNED_ZEROS (@0))
8025 (convert (lt @0 { build_real (TREE_TYPE (@0), dconst0); }))))
8027 /* Transform comparisons of the form X +- C1 CMP C2 to X CMP C2 -+ C1. */
8029 (for op (plus minus)
8032 (cmp (op@3 @0 INTEGER_CST@1) INTEGER_CST@2)
8033 (if (!TREE_OVERFLOW (@1) && !TREE_OVERFLOW (@2)
8034 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0))
8035 && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@0))
8036 && !TYPE_SATURATING (TREE_TYPE (@0)))
8037 (with { tree res = int_const_binop (rop, @2, @1); }
8038 (if (TREE_OVERFLOW (res)
8039 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
8040 { constant_boolean_node (cmp == NE_EXPR, type); }
8041 (if (single_use (@3))
8042 (cmp @0 { TREE_OVERFLOW (res)
8043 ? drop_tree_overflow (res) : res; }))))))))
8044 (for cmp (lt le gt ge)
8045 (for op (plus minus)
8048 (cmp (op@3 @0 INTEGER_CST@1) INTEGER_CST@2)
8049 (if (!TREE_OVERFLOW (@1) && !TREE_OVERFLOW (@2)
8050 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
8051 (with { tree res = int_const_binop (rop, @2, @1); }
8052 (if (TREE_OVERFLOW (res))
8054 fold_overflow_warning (("assuming signed overflow does not occur "
8055 "when simplifying conditional to constant"),
8056 WARN_STRICT_OVERFLOW_CONDITIONAL);
8057 bool less = cmp == LE_EXPR || cmp == LT_EXPR;
8058 /* wi::ges_p (@2, 0) should be sufficient for a signed type. */
8059 bool ovf_high = wi::lt_p (wi::to_wide (@1), 0,
8060 TYPE_SIGN (TREE_TYPE (@1)))
8061 != (op == MINUS_EXPR);
8062 constant_boolean_node (less == ovf_high, type);
8064 (if (single_use (@3))
8067 fold_overflow_warning (("assuming signed overflow does not occur "
8068 "when changing X +- C1 cmp C2 to "
8070 WARN_STRICT_OVERFLOW_COMPARISON);
8072 (cmp @0 { res; })))))))))
8074 /* Canonicalizations of BIT_FIELD_REFs. */
8077 (BIT_FIELD_REF (BIT_FIELD_REF @0 @1 @2) @3 @4)
8078 (BIT_FIELD_REF @0 @3 { const_binop (PLUS_EXPR, bitsizetype, @2, @4); }))
8081 (BIT_FIELD_REF (view_convert @0) @1 @2)
8082 (BIT_FIELD_REF @0 @1 @2))
8085 (BIT_FIELD_REF @0 @1 integer_zerop)
8086 (if (tree_int_cst_equal (@1, TYPE_SIZE (TREE_TYPE (@0))))
8090 (BIT_FIELD_REF @0 @1 @2)
8092 (if (TREE_CODE (TREE_TYPE (@0)) == COMPLEX_TYPE
8093 && tree_int_cst_equal (@1, TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))))
8095 (if (integer_zerop (@2))
8096 (view_convert (realpart @0)))
8097 (if (tree_int_cst_equal (@2, TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))))
8098 (view_convert (imagpart @0)))))
8099 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
8100 && INTEGRAL_TYPE_P (type)
8101 /* On GIMPLE this should only apply to register arguments. */
8102 && (! GIMPLE || is_gimple_reg (@0))
8103 /* A bit-field-ref that referenced the full argument can be stripped. */
8104 && ((compare_tree_int (@1, TYPE_PRECISION (TREE_TYPE (@0))) == 0
8105 && integer_zerop (@2))
8106 /* Low-parts can be reduced to integral conversions.
8107 ??? The following doesn't work for PDP endian. */
8108 || (BYTES_BIG_ENDIAN == WORDS_BIG_ENDIAN
8109 /* But only do this after vectorization. */
8110 && canonicalize_math_after_vectorization_p ()
8111 /* Don't even think about BITS_BIG_ENDIAN. */
8112 && TYPE_PRECISION (TREE_TYPE (@0)) % BITS_PER_UNIT == 0
8113 && TYPE_PRECISION (type) % BITS_PER_UNIT == 0
8114 && compare_tree_int (@2, (BYTES_BIG_ENDIAN
8115 ? (TYPE_PRECISION (TREE_TYPE (@0))
8116 - TYPE_PRECISION (type))
8120 /* Simplify vector extracts. */
8123 (BIT_FIELD_REF CONSTRUCTOR@0 @1 @2)
8124 (if (VECTOR_TYPE_P (TREE_TYPE (@0))
8125 && tree_fits_uhwi_p (TYPE_SIZE (type))
8126 && ((tree_to_uhwi (TYPE_SIZE (type))
8127 == tree_to_uhwi (TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))))
8128 || (VECTOR_TYPE_P (type)
8129 && (tree_to_uhwi (TYPE_SIZE (TREE_TYPE (type)))
8130 == tree_to_uhwi (TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0))))))))
8133 tree ctor = (TREE_CODE (@0) == SSA_NAME
8134 ? gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0)) : @0);
8135 tree eltype = TREE_TYPE (TREE_TYPE (ctor));
8136 unsigned HOST_WIDE_INT width = tree_to_uhwi (TYPE_SIZE (eltype));
8137 unsigned HOST_WIDE_INT n = tree_to_uhwi (@1);
8138 unsigned HOST_WIDE_INT idx = tree_to_uhwi (@2);
8141 && (idx % width) == 0
8143 && known_le ((idx + n) / width,
8144 TYPE_VECTOR_SUBPARTS (TREE_TYPE (ctor))))
8149 /* Constructor elements can be subvectors. */
8151 if (CONSTRUCTOR_NELTS (ctor) != 0)
8153 tree cons_elem = TREE_TYPE (CONSTRUCTOR_ELT (ctor, 0)->value);
8154 if (TREE_CODE (cons_elem) == VECTOR_TYPE)
8155 k = TYPE_VECTOR_SUBPARTS (cons_elem);
8157 unsigned HOST_WIDE_INT elt, count, const_k;
8160 /* We keep an exact subset of the constructor elements. */
8161 (if (multiple_p (idx, k, &elt) && multiple_p (n, k, &count))
8162 (if (CONSTRUCTOR_NELTS (ctor) == 0)
8163 { build_zero_cst (type); }
8165 (if (elt < CONSTRUCTOR_NELTS (ctor))
8166 (view_convert { CONSTRUCTOR_ELT (ctor, elt)->value; })
8167 { build_zero_cst (type); })
8168 /* We don't want to emit new CTORs unless the old one goes away.
8169 ??? Eventually allow this if the CTOR ends up constant or
8171 (if (single_use (@0))
8174 vec<constructor_elt, va_gc> *vals;
8175 vec_alloc (vals, count);
8176 bool constant_p = true;
8178 for (unsigned i = 0;
8179 i < count && elt + i < CONSTRUCTOR_NELTS (ctor); ++i)
8181 tree e = CONSTRUCTOR_ELT (ctor, elt + i)->value;
8182 CONSTRUCTOR_APPEND_ELT (vals, NULL_TREE, e);
8183 if (!CONSTANT_CLASS_P (e))
8186 tree evtype = (types_match (TREE_TYPE (type),
8187 TREE_TYPE (TREE_TYPE (ctor)))
8189 : build_vector_type (TREE_TYPE (TREE_TYPE (ctor)),
8191 /* We used to build a CTOR in the non-constant case here
8192 but that's not a GIMPLE value. We'd have to expose this
8193 operation somehow so the code generation can properly
8194 split it out to a separate stmt. */
8195 res = (constant_p ? build_vector_from_ctor (evtype, vals)
8196 : (GIMPLE ? NULL_TREE : build_constructor (evtype, vals)));
8199 (view_convert { res; })))))))
8200 /* The bitfield references a single constructor element. */
8201 (if (k.is_constant (&const_k)
8202 && idx + n <= (idx / const_k + 1) * const_k)
8204 (if (CONSTRUCTOR_NELTS (ctor) <= idx / const_k)
8205 { build_zero_cst (type); })
8207 (view_convert { CONSTRUCTOR_ELT (ctor, idx / const_k)->value; }))
8208 (BIT_FIELD_REF { CONSTRUCTOR_ELT (ctor, idx / const_k)->value; }
8209 @1 { bitsize_int ((idx % const_k) * width); })))))))))
8211 /* Simplify a bit extraction from a bit insertion for the cases with
8212 the inserted element fully covering the extraction or the insertion
8213 not touching the extraction. */
8215 (BIT_FIELD_REF (bit_insert @0 @1 @ipos) @rsize @rpos)
8218 unsigned HOST_WIDE_INT isize;
8219 if (INTEGRAL_TYPE_P (TREE_TYPE (@1)))
8220 isize = TYPE_PRECISION (TREE_TYPE (@1));
8222 isize = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (@1)));
8225 (if ((!INTEGRAL_TYPE_P (TREE_TYPE (@1))
8226 || type_has_mode_precision_p (TREE_TYPE (@1)))
8227 && wi::leu_p (wi::to_wide (@ipos), wi::to_wide (@rpos))
8228 && wi::leu_p (wi::to_wide (@rpos) + wi::to_wide (@rsize),
8229 wi::to_wide (@ipos) + isize))
8230 (BIT_FIELD_REF @1 @rsize { wide_int_to_tree (bitsizetype,
8232 - wi::to_wide (@ipos)); }))
8233 (if (wi::eq_p (wi::to_wide (@ipos), wi::to_wide (@rpos))
8234 && compare_tree_int (@rsize, isize) == 0)
8236 (if (wi::geu_p (wi::to_wide (@ipos),
8237 wi::to_wide (@rpos) + wi::to_wide (@rsize))
8238 || wi::geu_p (wi::to_wide (@rpos),
8239 wi::to_wide (@ipos) + isize))
8240 (BIT_FIELD_REF @0 @rsize @rpos)))))
8242 /* Simplify vector inserts of other vector extracts to a permute. */
8244 (bit_insert @0 (BIT_FIELD_REF@2 @1 @rsize @rpos) @ipos)
8245 (if (VECTOR_TYPE_P (type)
8246 && types_match (@0, @1)
8247 && types_match (TREE_TYPE (TREE_TYPE (@0)), TREE_TYPE (@2))
8248 && TYPE_VECTOR_SUBPARTS (type).is_constant ())
8251 unsigned HOST_WIDE_INT elsz
8252 = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (TREE_TYPE (@1))));
8253 poly_uint64 relt = exact_div (tree_to_poly_uint64 (@rpos), elsz);
8254 poly_uint64 ielt = exact_div (tree_to_poly_uint64 (@ipos), elsz);
8255 unsigned nunits = TYPE_VECTOR_SUBPARTS (type).to_constant ();
8256 vec_perm_builder builder;
8257 builder.new_vector (nunits, nunits, 1);
8258 for (unsigned i = 0; i < nunits; ++i)
8259 builder.quick_push (known_eq (ielt, i) ? nunits + relt : i);
8260 vec_perm_indices sel (builder, 2, nunits);
8262 (if (!VECTOR_MODE_P (TYPE_MODE (type))
8263 || can_vec_perm_const_p (TYPE_MODE (type), TYPE_MODE (type), sel, false))
8264 (vec_perm @0 @1 { vec_perm_indices_to_tree
8265 (build_vector_type (ssizetype, nunits), sel); })))))
8267 (if (canonicalize_math_after_vectorization_p ())
8270 (fmas:c (negate @0) @1 @2)
8271 (IFN_FNMA @0 @1 @2))
8273 (fmas @0 @1 (negate @2))
8276 (fmas:c (negate @0) @1 (negate @2))
8277 (IFN_FNMS @0 @1 @2))
8279 (negate (fmas@3 @0 @1 @2))
8280 (if (single_use (@3))
8281 (IFN_FNMS @0 @1 @2))))
8284 (IFN_FMS:c (negate @0) @1 @2)
8285 (IFN_FNMS @0 @1 @2))
8287 (IFN_FMS @0 @1 (negate @2))
8290 (IFN_FMS:c (negate @0) @1 (negate @2))
8291 (IFN_FNMA @0 @1 @2))
8293 (negate (IFN_FMS@3 @0 @1 @2))
8294 (if (single_use (@3))
8295 (IFN_FNMA @0 @1 @2)))
8298 (IFN_FNMA:c (negate @0) @1 @2)
8301 (IFN_FNMA @0 @1 (negate @2))
8302 (IFN_FNMS @0 @1 @2))
8304 (IFN_FNMA:c (negate @0) @1 (negate @2))
8307 (negate (IFN_FNMA@3 @0 @1 @2))
8308 (if (single_use (@3))
8309 (IFN_FMS @0 @1 @2)))
8312 (IFN_FNMS:c (negate @0) @1 @2)
8315 (IFN_FNMS @0 @1 (negate @2))
8316 (IFN_FNMA @0 @1 @2))
8318 (IFN_FNMS:c (negate @0) @1 (negate @2))
8321 (negate (IFN_FNMS@3 @0 @1 @2))
8322 (if (single_use (@3))
8323 (IFN_FMA @0 @1 @2))))
8325 /* CLZ simplifications. */
8330 (op (clz:s@2 @0) INTEGER_CST@1)
8331 (if (integer_zerop (@1) && single_use (@2))
8332 /* clz(X) == 0 is (int)X < 0 and clz(X) != 0 is (int)X >= 0. */
8333 (with { tree type0 = TREE_TYPE (@0);
8334 tree stype = signed_type_for (type0);
8335 HOST_WIDE_INT val = 0;
8336 /* Punt on hypothetical weird targets. */
8338 && CLZ_DEFINED_VALUE_AT_ZERO (SCALAR_TYPE_MODE (type0),
8344 (cmp (convert:stype @0) { build_zero_cst (stype); })))
8345 /* clz(X) == (prec-1) is X == 1 and clz(X) != (prec-1) is X != 1. */
8346 (with { bool ok = true;
8347 HOST_WIDE_INT val = 0;
8348 tree type0 = TREE_TYPE (@0);
8349 /* Punt on hypothetical weird targets. */
8351 && CLZ_DEFINED_VALUE_AT_ZERO (SCALAR_TYPE_MODE (type0),
8353 && val == TYPE_PRECISION (type0) - 1)
8356 (if (ok && wi::to_wide (@1) == (TYPE_PRECISION (type0) - 1))
8357 (op @0 { build_one_cst (type0); })))))))
8359 /* CTZ simplifications. */
8361 (for op (ge gt le lt)
8364 /* __builtin_ctz (x) >= C -> (x & ((1 << C) - 1)) == 0. */
8365 (op (ctz:s @0) INTEGER_CST@1)
8366 (with { bool ok = true;
8367 HOST_WIDE_INT val = 0;
8368 if (!tree_fits_shwi_p (@1))
8372 val = tree_to_shwi (@1);
8373 /* Canonicalize to >= or <. */
8374 if (op == GT_EXPR || op == LE_EXPR)
8376 if (val == HOST_WIDE_INT_MAX)
8382 bool zero_res = false;
8383 HOST_WIDE_INT zero_val = 0;
8384 tree type0 = TREE_TYPE (@0);
8385 int prec = TYPE_PRECISION (type0);
8387 && CTZ_DEFINED_VALUE_AT_ZERO (SCALAR_TYPE_MODE (type0),
8392 (if (ok && (!zero_res || zero_val >= val))
8393 { constant_boolean_node (cmp == EQ_EXPR ? true : false, type); })
8395 (if (ok && (!zero_res || zero_val < val))
8396 { constant_boolean_node (cmp == EQ_EXPR ? false : true, type); })
8397 (if (ok && (!zero_res || zero_val < 0 || zero_val >= prec))
8398 (cmp (bit_and @0 { wide_int_to_tree (type0,
8399 wi::mask (val, false, prec)); })
8400 { build_zero_cst (type0); })))))))
8403 /* __builtin_ctz (x) == C -> (x & ((1 << (C + 1)) - 1)) == (1 << C). */
8404 (op (ctz:s @0) INTEGER_CST@1)
8405 (with { bool zero_res = false;
8406 HOST_WIDE_INT zero_val = 0;
8407 tree type0 = TREE_TYPE (@0);
8408 int prec = TYPE_PRECISION (type0);
8410 && CTZ_DEFINED_VALUE_AT_ZERO (SCALAR_TYPE_MODE (type0),
8414 (if (tree_int_cst_sgn (@1) < 0 || wi::to_widest (@1) >= prec)
8415 (if (!zero_res || zero_val != wi::to_widest (@1))
8416 { constant_boolean_node (op == EQ_EXPR ? false : true, type); })
8417 (if (!zero_res || zero_val < 0 || zero_val >= prec)
8418 (op (bit_and @0 { wide_int_to_tree (type0,
8419 wi::mask (tree_to_uhwi (@1) + 1,
8421 { wide_int_to_tree (type0,
8422 wi::shifted_mask (tree_to_uhwi (@1), 1,
8423 false, prec)); })))))))
8425 /* POPCOUNT simplifications. */
8426 /* popcount(X) + popcount(Y) is popcount(X|Y) when X&Y must be zero. */
8428 (plus (POPCOUNT:s @0) (POPCOUNT:s @1))
8429 (if (INTEGRAL_TYPE_P (type)
8430 && wi::bit_and (tree_nonzero_bits (@0), tree_nonzero_bits (@1)) == 0)
8431 (POPCOUNT (bit_ior @0 @1))))
8433 /* popcount(X) == 0 is X == 0, and related (in)equalities. */
8434 (for popcount (POPCOUNT)
8435 (for cmp (le eq ne gt)
8438 (cmp (popcount @0) integer_zerop)
8439 (rep @0 { build_zero_cst (TREE_TYPE (@0)); }))))
8441 /* popcount(bswap(x)) is popcount(x). */
8442 (for popcount (POPCOUNT)
8443 (for bswap (BUILT_IN_BSWAP16 BUILT_IN_BSWAP32
8444 BUILT_IN_BSWAP64 BUILT_IN_BSWAP128)
8446 (popcount (convert?@0 (bswap:s@1 @2)))
8447 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
8448 && INTEGRAL_TYPE_P (TREE_TYPE (@1)))
8449 (with { tree type0 = TREE_TYPE (@0);
8450 tree type1 = TREE_TYPE (@1);
8451 unsigned int prec0 = TYPE_PRECISION (type0);
8452 unsigned int prec1 = TYPE_PRECISION (type1); }
8453 (if (prec0 == prec1 || (prec0 > prec1 && TYPE_UNSIGNED (type1)))
8454 (popcount (convert:type0 (convert:type1 @2)))))))))
8456 /* popcount(rotate(X Y)) is popcount(X). */
8457 (for popcount (POPCOUNT)
8458 (for rot (lrotate rrotate)
8460 (popcount (convert?@0 (rot:s@1 @2 @3)))
8461 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
8462 && INTEGRAL_TYPE_P (TREE_TYPE (@1))
8463 && (GIMPLE || !TREE_SIDE_EFFECTS (@3)))
8464 (with { tree type0 = TREE_TYPE (@0);
8465 tree type1 = TREE_TYPE (@1);
8466 unsigned int prec0 = TYPE_PRECISION (type0);
8467 unsigned int prec1 = TYPE_PRECISION (type1); }
8468 (if (prec0 == prec1 || (prec0 > prec1 && TYPE_UNSIGNED (type1)))
8469 (popcount (convert:type0 @2))))))))
8471 /* Canonicalize POPCOUNT(x)&1 as PARITY(X). */
8473 (bit_and (POPCOUNT @0) integer_onep)
8476 /* popcount(X&Y) + popcount(X|Y) is popcount(x) + popcount(Y). */
8478 (plus:c (POPCOUNT:s (bit_and:s @0 @1)) (POPCOUNT:s (bit_ior:cs @0 @1)))
8479 (plus (POPCOUNT @0) (POPCOUNT @1)))
8481 /* popcount(X) + popcount(Y) - popcount(X&Y) is popcount(X|Y). */
8482 /* popcount(X) + popcount(Y) - popcount(X|Y) is popcount(X&Y). */
8483 (for popcount (POPCOUNT)
8484 (for log1 (bit_and bit_ior)
8485 log2 (bit_ior bit_and)
8487 (minus (plus:s (popcount:s @0) (popcount:s @1))
8488 (popcount:s (log1:cs @0 @1)))
8489 (popcount (log2 @0 @1)))
8491 (plus:c (minus:s (popcount:s @0) (popcount:s (log1:cs @0 @1)))
8493 (popcount (log2 @0 @1)))))
8495 /* PARITY simplifications. */
8496 /* parity(~X) is parity(X). */
8498 (PARITY (bit_not @0))
8501 /* parity(bswap(x)) is parity(x). */
8502 (for parity (PARITY)
8503 (for bswap (BUILT_IN_BSWAP16 BUILT_IN_BSWAP32
8504 BUILT_IN_BSWAP64 BUILT_IN_BSWAP128)
8506 (parity (convert?@0 (bswap:s@1 @2)))
8507 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
8508 && INTEGRAL_TYPE_P (TREE_TYPE (@1))
8509 && TYPE_PRECISION (TREE_TYPE (@0))
8510 >= TYPE_PRECISION (TREE_TYPE (@1)))
8511 (with { tree type0 = TREE_TYPE (@0);
8512 tree type1 = TREE_TYPE (@1); }
8513 (parity (convert:type0 (convert:type1 @2))))))))
8515 /* parity(rotate(X Y)) is parity(X). */
8516 (for parity (PARITY)
8517 (for rot (lrotate rrotate)
8519 (parity (convert?@0 (rot:s@1 @2 @3)))
8520 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
8521 && INTEGRAL_TYPE_P (TREE_TYPE (@1))
8522 && (GIMPLE || !TREE_SIDE_EFFECTS (@3))
8523 && TYPE_PRECISION (TREE_TYPE (@0))
8524 >= TYPE_PRECISION (TREE_TYPE (@1)))
8525 (with { tree type0 = TREE_TYPE (@0); }
8526 (parity (convert:type0 @2)))))))
8528 /* parity(X)^parity(Y) is parity(X^Y). */
8530 (bit_xor (PARITY:s @0) (PARITY:s @1))
8531 (PARITY (bit_xor @0 @1)))
8533 /* a != 0 ? FUN(a) : 0 -> Fun(a) for some builtin functions. */
8534 (for func (POPCOUNT BSWAP FFS PARITY)
8536 (cond (ne @0 integer_zerop@1) (func@3 (convert? @0)) integer_zerop@2)
8539 /* a != 0 ? FUN(a) : CST -> Fun(a) for some CLRSB builtins
8540 where CST is precision-1. */
8543 (cond (ne @0 integer_zerop@1) (func@4 (convert?@3 @0)) INTEGER_CST@2)
8544 (if (wi::to_widest (@2) == TYPE_PRECISION (TREE_TYPE (@3)) - 1)
8548 /* a != 0 ? CLZ(a) : CST -> .CLZ(a) where CST is the result of the internal function for 0. */
8551 (cond (ne @0 integer_zerop@1) (func (convert?@3 @0)) INTEGER_CST@2)
8553 internal_fn ifn = IFN_LAST;
8554 if (direct_internal_fn_supported_p (IFN_CLZ, type, OPTIMIZE_FOR_BOTH)
8555 && CLZ_DEFINED_VALUE_AT_ZERO (SCALAR_INT_TYPE_MODE (type),
8559 (if (ifn == IFN_CLZ && wi::to_widest (@2) == val)
8562 /* a != 0 ? CTZ(a) : CST -> .CTZ(a) where CST is the result of the internal function for 0. */
8565 (cond (ne @0 integer_zerop@1) (func (convert?@3 @0)) INTEGER_CST@2)
8567 internal_fn ifn = IFN_LAST;
8568 if (direct_internal_fn_supported_p (IFN_CTZ, type, OPTIMIZE_FOR_BOTH)
8569 && CTZ_DEFINED_VALUE_AT_ZERO (SCALAR_INT_TYPE_MODE (type),
8573 (if (ifn == IFN_CTZ && wi::to_widest (@2) == val)
8577 /* Common POPCOUNT/PARITY simplifications. */
8578 /* popcount(X&C1) is (X>>C2)&1 when C1 == 1<<C2. Same for parity(X&C1). */
8579 (for pfun (POPCOUNT PARITY)
8582 (if (INTEGRAL_TYPE_P (type))
8583 (with { wide_int nz = tree_nonzero_bits (@0); }
8587 (if (wi::popcount (nz) == 1)
8588 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
8589 (convert (rshift:utype (convert:utype @0)
8590 { build_int_cst (integer_type_node,
8591 wi::ctz (nz)); })))))))))
8594 /* 64- and 32-bits branchless implementations of popcount are detected:
8596 int popcount64c (uint64_t x)
8598 x -= (x >> 1) & 0x5555555555555555ULL;
8599 x = (x & 0x3333333333333333ULL) + ((x >> 2) & 0x3333333333333333ULL);
8600 x = (x + (x >> 4)) & 0x0f0f0f0f0f0f0f0fULL;
8601 return (x * 0x0101010101010101ULL) >> 56;
8604 int popcount32c (uint32_t x)
8606 x -= (x >> 1) & 0x55555555;
8607 x = (x & 0x33333333) + ((x >> 2) & 0x33333333);
8608 x = (x + (x >> 4)) & 0x0f0f0f0f;
8609 return (x * 0x01010101) >> 24;
8616 (rshift @8 INTEGER_CST@5)
8618 (bit_and @6 INTEGER_CST@7)
8622 (bit_and (rshift @0 INTEGER_CST@4) INTEGER_CST@11))
8628 /* Check constants and optab. */
8629 (with { unsigned prec = TYPE_PRECISION (type);
8630 int shift = (64 - prec) & 63;
8631 unsigned HOST_WIDE_INT c1
8632 = HOST_WIDE_INT_UC (0x0101010101010101) >> shift;
8633 unsigned HOST_WIDE_INT c2
8634 = HOST_WIDE_INT_UC (0x0F0F0F0F0F0F0F0F) >> shift;
8635 unsigned HOST_WIDE_INT c3
8636 = HOST_WIDE_INT_UC (0x3333333333333333) >> shift;
8637 unsigned HOST_WIDE_INT c4
8638 = HOST_WIDE_INT_UC (0x5555555555555555) >> shift;
8643 && TYPE_UNSIGNED (type)
8644 && integer_onep (@4)
8645 && wi::to_widest (@10) == 2
8646 && wi::to_widest (@5) == 4
8647 && wi::to_widest (@1) == prec - 8
8648 && tree_to_uhwi (@2) == c1
8649 && tree_to_uhwi (@3) == c2
8650 && tree_to_uhwi (@9) == c3
8651 && tree_to_uhwi (@7) == c3
8652 && tree_to_uhwi (@11) == c4)
8653 (if (direct_internal_fn_supported_p (IFN_POPCOUNT, type,
8655 (convert (IFN_POPCOUNT:type @0))
8656 /* Try to do popcount in two halves. PREC must be at least
8657 five bits for this to work without extension before adding. */
8659 tree half_type = NULL_TREE;
8660 opt_machine_mode m = mode_for_size ((prec + 1) / 2, MODE_INT, 1);
8663 && m.require () != TYPE_MODE (type))
8665 half_prec = GET_MODE_PRECISION (as_a <scalar_int_mode> (m));
8666 half_type = build_nonstandard_integer_type (half_prec, 1);
8668 gcc_assert (half_prec > 2);
8670 (if (half_type != NULL_TREE
8671 && direct_internal_fn_supported_p (IFN_POPCOUNT, half_type,
8674 (IFN_POPCOUNT:half_type (convert @0))
8675 (IFN_POPCOUNT:half_type (convert (rshift @0
8676 { build_int_cst (integer_type_node, half_prec); } )))))))))))
8678 /* __builtin_ffs needs to deal on many targets with the possible zero
8679 argument. If we know the argument is always non-zero, __builtin_ctz + 1
8680 should lead to better code. */
8682 (FFS tree_expr_nonzero_p@0)
8683 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
8684 && direct_internal_fn_supported_p (IFN_CTZ, TREE_TYPE (@0),
8685 OPTIMIZE_FOR_SPEED))
8686 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
8687 (plus (CTZ:type (convert:utype @0)) { build_one_cst (type); }))))
8690 (for ffs (BUILT_IN_FFS BUILT_IN_FFSL BUILT_IN_FFSLL
8692 /* __builtin_ffs (X) == 0 -> X == 0.
8693 __builtin_ffs (X) == 6 -> (X & 63) == 32. */
8696 (cmp (ffs@2 @0) INTEGER_CST@1)
8697 (with { int prec = TYPE_PRECISION (TREE_TYPE (@0)); }
8699 (if (integer_zerop (@1))
8700 (cmp @0 { build_zero_cst (TREE_TYPE (@0)); }))
8701 (if (tree_int_cst_sgn (@1) < 0 || wi::to_widest (@1) > prec)
8702 { constant_boolean_node (cmp == NE_EXPR ? true : false, type); })
8703 (if (single_use (@2))
8704 (cmp (bit_and @0 { wide_int_to_tree (TREE_TYPE (@0),
8705 wi::mask (tree_to_uhwi (@1),
8707 { wide_int_to_tree (TREE_TYPE (@0),
8708 wi::shifted_mask (tree_to_uhwi (@1) - 1, 1,
8709 false, prec)); }))))))
8711 /* __builtin_ffs (X) > 6 -> X != 0 && (X & 63) == 0. */
8715 bit_op (bit_and bit_ior)
8717 (cmp (ffs@2 @0) INTEGER_CST@1)
8718 (with { int prec = TYPE_PRECISION (TREE_TYPE (@0)); }
8720 (if (integer_zerop (@1))
8721 (cmp2 @0 { build_zero_cst (TREE_TYPE (@0)); }))
8722 (if (tree_int_cst_sgn (@1) < 0)
8723 { constant_boolean_node (cmp == GT_EXPR ? true : false, type); })
8724 (if (wi::to_widest (@1) >= prec)
8725 { constant_boolean_node (cmp == GT_EXPR ? false : true, type); })
8726 (if (wi::to_widest (@1) == prec - 1)
8727 (cmp3 @0 { wide_int_to_tree (TREE_TYPE (@0),
8728 wi::shifted_mask (prec - 1, 1,
8730 (if (single_use (@2))
8731 (bit_op (cmp2 @0 { build_zero_cst (TREE_TYPE (@0)); })
8733 { wide_int_to_tree (TREE_TYPE (@0),
8734 wi::mask (tree_to_uhwi (@1),
8736 { build_zero_cst (TREE_TYPE (@0)); }))))))))
8743 --> r = .COND_FN (cond, a, b)
8747 --> r = .COND_FN (~cond, b, a). */
8749 (for uncond_op (UNCOND_UNARY)
8750 cond_op (COND_UNARY)
8752 (vec_cond @0 (view_convert? (uncond_op@3 @1)) @2)
8753 (with { tree op_type = TREE_TYPE (@3); }
8754 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
8755 && is_truth_type_for (op_type, TREE_TYPE (@0)))
8756 (cond_op @0 @1 @2))))
8758 (vec_cond @0 @1 (view_convert? (uncond_op@3 @2)))
8759 (with { tree op_type = TREE_TYPE (@3); }
8760 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
8761 && is_truth_type_for (op_type, TREE_TYPE (@0)))
8762 (cond_op (bit_not @0) @2 @1)))))
8764 /* `(a ? -1 : 0) ^ b` can be converted into a conditional not. */
8766 (bit_xor:c (vec_cond @0 uniform_integer_cst_p@1 uniform_integer_cst_p@2) @3)
8767 (if (canonicalize_math_after_vectorization_p ()
8768 && vectorized_internal_fn_supported_p (IFN_COND_NOT, type)
8769 && is_truth_type_for (type, TREE_TYPE (@0)))
8770 (if (integer_all_onesp (@1) && integer_zerop (@2))
8771 (IFN_COND_NOT @0 @3 @3))
8772 (if (integer_all_onesp (@2) && integer_zerop (@1))
8773 (IFN_COND_NOT (bit_not @0) @3 @3))))
8782 r = c ? a1 op a2 : b;
8784 if the target can do it in one go. This makes the operation conditional
8785 on c, so could drop potentially-trapping arithmetic, but that's a valid
8786 simplification if the result of the operation isn't needed.
8788 Avoid speculatively generating a stand-alone vector comparison
8789 on targets that might not support them. Any target implementing
8790 conditional internal functions must support the same comparisons
8791 inside and outside a VEC_COND_EXPR. */
8793 (for uncond_op (UNCOND_BINARY)
8794 cond_op (COND_BINARY)
8796 (vec_cond @0 (view_convert? (uncond_op@4 @1 @2)) @3)
8797 (with { tree op_type = TREE_TYPE (@4); }
8798 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
8799 && is_truth_type_for (op_type, TREE_TYPE (@0))
8801 (view_convert (cond_op @0 @1 @2 (view_convert:op_type @3))))))
8803 (vec_cond @0 @1 (view_convert? (uncond_op@4 @2 @3)))
8804 (with { tree op_type = TREE_TYPE (@4); }
8805 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
8806 && is_truth_type_for (op_type, TREE_TYPE (@0))
8808 (view_convert (cond_op (bit_not @0) @2 @3 (view_convert:op_type @1)))))))
8810 /* Same for ternary operations. */
8811 (for uncond_op (UNCOND_TERNARY)
8812 cond_op (COND_TERNARY)
8814 (vec_cond @0 (view_convert? (uncond_op@5 @1 @2 @3)) @4)
8815 (with { tree op_type = TREE_TYPE (@5); }
8816 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
8817 && is_truth_type_for (op_type, TREE_TYPE (@0))
8819 (view_convert (cond_op @0 @1 @2 @3 (view_convert:op_type @4))))))
8821 (vec_cond @0 @1 (view_convert? (uncond_op@5 @2 @3 @4)))
8822 (with { tree op_type = TREE_TYPE (@5); }
8823 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
8824 && is_truth_type_for (op_type, TREE_TYPE (@0))
8826 (view_convert (cond_op (bit_not @0) @2 @3 @4
8827 (view_convert:op_type @1)))))))
8830 /* Detect cases in which a VEC_COND_EXPR effectively replaces the
8831 "else" value of an IFN_COND_*. */
8832 (for cond_op (COND_BINARY)
8834 (vec_cond @0 (view_convert? (cond_op @0 @1 @2 @3)) @4)
8835 (with { tree op_type = TREE_TYPE (@3); }
8836 (if (element_precision (type) == element_precision (op_type))
8837 (view_convert (cond_op @0 @1 @2 (view_convert:op_type @4))))))
8839 (vec_cond @0 @1 (view_convert? (cond_op @2 @3 @4 @5)))
8840 (with { tree op_type = TREE_TYPE (@5); }
8841 (if (inverse_conditions_p (@0, @2)
8842 && element_precision (type) == element_precision (op_type))
8843 (view_convert (cond_op @2 @3 @4 (view_convert:op_type @1)))))))
8845 /* Same for ternary operations. */
8846 (for cond_op (COND_TERNARY)
8848 (vec_cond @0 (view_convert? (cond_op @0 @1 @2 @3 @4)) @5)
8849 (with { tree op_type = TREE_TYPE (@4); }
8850 (if (element_precision (type) == element_precision (op_type))
8851 (view_convert (cond_op @0 @1 @2 @3 (view_convert:op_type @5))))))
8853 (vec_cond @0 @1 (view_convert? (cond_op @2 @3 @4 @5 @6)))
8854 (with { tree op_type = TREE_TYPE (@6); }
8855 (if (inverse_conditions_p (@0, @2)
8856 && element_precision (type) == element_precision (op_type))
8857 (view_convert (cond_op @2 @3 @4 @5 (view_convert:op_type @1)))))))
8859 /* Detect simplication for a conditional reduction where
8862 c = mask2 ? d + a : d
8866 c = mask1 && mask2 ? d + b : d. */
8868 (IFN_COND_ADD @0 @1 (vec_cond @2 @3 integer_zerop) @1)
8869 (IFN_COND_ADD (bit_and @0 @2) @1 @3 @1))
8871 /* For pointers @0 and @2 and nonnegative constant offset @1, look for
8874 A: (@0 + @1 < @2) | (@2 + @1 < @0)
8875 B: (@0 + @1 <= @2) | (@2 + @1 <= @0)
8877 If pointers are known not to wrap, B checks whether @1 bytes starting
8878 at @0 and @2 do not overlap, while A tests the same thing for @1 + 1
8879 bytes. A is more efficiently tested as:
8881 A: (sizetype) (@0 + @1 - @2) > @1 * 2
8883 The equivalent expression for B is given by replacing @1 with @1 - 1:
8885 B: (sizetype) (@0 + (@1 - 1) - @2) > (@1 - 1) * 2
8887 @0 and @2 can be swapped in both expressions without changing the result.
8889 The folds rely on sizetype's being unsigned (which is always true)
8890 and on its being the same width as the pointer (which we have to check).
8892 The fold replaces two pointer_plus expressions, two comparisons and
8893 an IOR with a pointer_plus, a pointer_diff, and a comparison, so in
8894 the best case it's a saving of two operations. The A fold retains one
8895 of the original pointer_pluses, so is a win even if both pointer_pluses
8896 are used elsewhere. The B fold is a wash if both pointer_pluses are
8897 used elsewhere, since all we end up doing is replacing a comparison with
8898 a pointer_plus. We do still apply the fold under those circumstances
8899 though, in case applying it to other conditions eventually makes one of the
8900 pointer_pluses dead. */
8901 (for ior (truth_orif truth_or bit_ior)
8904 (ior (cmp:cs (pointer_plus@3 @0 INTEGER_CST@1) @2)
8905 (cmp:cs (pointer_plus@4 @2 @1) @0))
8906 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
8907 && TYPE_OVERFLOW_WRAPS (sizetype)
8908 && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (sizetype))
8909 /* Calculate the rhs constant. */
8910 (with { offset_int off = wi::to_offset (@1) - (cmp == LE_EXPR ? 1 : 0);
8911 offset_int rhs = off * 2; }
8912 /* Always fails for negative values. */
8913 (if (wi::min_precision (rhs, UNSIGNED) <= TYPE_PRECISION (sizetype))
8914 /* Since the order of @0 and @2 doesn't matter, let tree_swap_operands_p
8915 pick a canonical order. This increases the chances of using the
8916 same pointer_plus in multiple checks. */
8917 (with { bool swap_p = tree_swap_operands_p (@0, @2);
8918 tree rhs_tree = wide_int_to_tree (sizetype, rhs); }
8919 (if (cmp == LT_EXPR)
8920 (gt (convert:sizetype
8921 (pointer_diff:ssizetype { swap_p ? @4 : @3; }
8922 { swap_p ? @0 : @2; }))
8924 (gt (convert:sizetype
8925 (pointer_diff:ssizetype
8926 (pointer_plus { swap_p ? @2 : @0; }
8927 { wide_int_to_tree (sizetype, off); })
8928 { swap_p ? @0 : @2; }))
8929 { rhs_tree; })))))))))
8931 /* Fold REDUC (@0 & @1) -> @0[I] & @1[I] if element I is the only nonzero
8933 (for reduc (IFN_REDUC_PLUS IFN_REDUC_IOR IFN_REDUC_XOR)
8934 (simplify (reduc (view_convert? (bit_and @0 VECTOR_CST@1)))
8935 (with { int i = single_nonzero_element (@1); }
8937 (with { tree elt = vector_cst_elt (@1, i);
8938 tree elt_type = TREE_TYPE (elt);
8939 unsigned int elt_bits = tree_to_uhwi (TYPE_SIZE (elt_type));
8940 tree size = bitsize_int (elt_bits);
8941 tree pos = bitsize_int (elt_bits * i); }
8944 (BIT_FIELD_REF:elt_type @0 { size; } { pos; })
8947 /* Fold reduction of a single nonzero element constructor. */
8948 (for reduc (IFN_REDUC_PLUS IFN_REDUC_IOR IFN_REDUC_XOR)
8949 (simplify (reduc (CONSTRUCTOR@0))
8950 (with { tree ctor = (TREE_CODE (@0) == SSA_NAME
8951 ? gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0)) : @0);
8952 tree elt = ctor_single_nonzero_element (ctor); }
8954 && !HONOR_SNANS (type)
8955 && !HONOR_SIGNED_ZEROS (type))
8958 /* Fold REDUC (@0 op VECTOR_CST) as REDUC (@0) op REDUC (VECTOR_CST). */
8959 (for reduc (IFN_REDUC_PLUS IFN_REDUC_MAX IFN_REDUC_MIN IFN_REDUC_FMAX
8960 IFN_REDUC_FMIN IFN_REDUC_AND IFN_REDUC_IOR IFN_REDUC_XOR)
8961 op (plus max min IFN_FMAX IFN_FMIN bit_and bit_ior bit_xor)
8962 (simplify (reduc (op @0 VECTOR_CST@1))
8963 (op (reduc:type @0) (reduc:type @1))))
8965 /* Simplify vector floating point operations of alternating sub/add pairs
8966 into using an fneg of a wider element type followed by a normal add.
8967 under IEEE 754 the fneg of the wider type will negate every even entry
8968 and when doing an add we get a sub of the even and add of every odd
8970 (for plusminus (plus minus)
8971 minusplus (minus plus)
8973 (vec_perm (plusminus @0 @1) (minusplus @2 @3) VECTOR_CST@4)
8974 (if (!VECTOR_INTEGER_TYPE_P (type)
8975 && !FLOAT_WORDS_BIG_ENDIAN
8976 /* plus is commutative, while minus is not, so :c can't be used.
8977 Do equality comparisons by hand and at the end pick the operands
8979 && (operand_equal_p (@0, @2, 0)
8980 ? operand_equal_p (@1, @3, 0)
8981 : operand_equal_p (@0, @3, 0) && operand_equal_p (@1, @2, 0)))
8984 /* Build a vector of integers from the tree mask. */
8985 vec_perm_builder builder;
8987 (if (tree_to_vec_perm_builder (&builder, @4))
8990 /* Create a vec_perm_indices for the integer vector. */
8991 poly_uint64 nelts = TYPE_VECTOR_SUBPARTS (type);
8992 vec_perm_indices sel (builder, 2, nelts);
8993 machine_mode vec_mode = TYPE_MODE (type);
8994 machine_mode wide_mode;
8995 scalar_mode wide_elt_mode;
8996 poly_uint64 wide_nunits;
8997 scalar_mode inner_mode = GET_MODE_INNER (vec_mode);
8999 (if (VECTOR_MODE_P (vec_mode)
9000 && sel.series_p (0, 2, 0, 2)
9001 && sel.series_p (1, 2, nelts + 1, 2)
9002 && GET_MODE_2XWIDER_MODE (inner_mode).exists (&wide_elt_mode)
9003 && multiple_p (GET_MODE_NUNITS (vec_mode), 2, &wide_nunits)
9004 && related_vector_mode (vec_mode, wide_elt_mode,
9005 wide_nunits).exists (&wide_mode))
9009 = lang_hooks.types.type_for_mode (GET_MODE_INNER (wide_mode),
9010 TYPE_UNSIGNED (type));
9011 tree ntype = build_vector_type_for_mode (stype, wide_mode);
9013 /* The format has to be a non-extended ieee format. */
9014 const struct real_format *fmt_old = FLOAT_MODE_FORMAT (vec_mode);
9015 const struct real_format *fmt_new = FLOAT_MODE_FORMAT (wide_mode);
9017 (if (TYPE_MODE (stype) != BLKmode
9018 && VECTOR_TYPE_P (ntype)
9023 /* If the target doesn't support v1xx vectors, try using
9024 scalar mode xx instead. */
9025 if (known_eq (GET_MODE_NUNITS (wide_mode), 1)
9026 && !target_supports_op_p (ntype, NEGATE_EXPR, optab_vector))
9029 (if (fmt_new->signbit_rw
9030 == fmt_old->signbit_rw + GET_MODE_UNIT_BITSIZE (vec_mode)
9031 && fmt_new->signbit_rw == fmt_new->signbit_ro
9032 && targetm.can_change_mode_class (TYPE_MODE (ntype),
9033 TYPE_MODE (type), ALL_REGS)
9034 && ((optimize_vectors_before_lowering_p ()
9035 && VECTOR_TYPE_P (ntype))
9036 || target_supports_op_p (ntype, NEGATE_EXPR, optab_vector)))
9037 (if (plusminus == PLUS_EXPR)
9038 (plus (view_convert:type (negate (view_convert:ntype @3))) @2)
9039 (minus @0 (view_convert:type
9040 (negate (view_convert:ntype @1))))))))))))))))
9043 (vec_perm @0 @1 VECTOR_CST@2)
9046 tree op0 = @0, op1 = @1, op2 = @2;
9047 machine_mode result_mode = TYPE_MODE (type);
9048 machine_mode op_mode = TYPE_MODE (TREE_TYPE (op0));
9050 /* Build a vector of integers from the tree mask. */
9051 vec_perm_builder builder;
9053 (if (tree_to_vec_perm_builder (&builder, op2))
9056 /* Create a vec_perm_indices for the integer vector. */
9057 poly_uint64 nelts = TYPE_VECTOR_SUBPARTS (type);
9058 bool single_arg = (op0 == op1);
9059 vec_perm_indices sel (builder, single_arg ? 1 : 2, nelts);
9061 (if (sel.series_p (0, 1, 0, 1))
9063 (if (sel.series_p (0, 1, nelts, 1))
9069 if (sel.all_from_input_p (0))
9071 else if (sel.all_from_input_p (1))
9074 sel.rotate_inputs (1);
9076 else if (known_ge (poly_uint64 (sel[0]), nelts))
9078 std::swap (op0, op1);
9079 sel.rotate_inputs (1);
9083 tree cop0 = op0, cop1 = op1;
9084 if (TREE_CODE (op0) == SSA_NAME
9085 && (def = dyn_cast <gassign *> (SSA_NAME_DEF_STMT (op0)))
9086 && gimple_assign_rhs_code (def) == CONSTRUCTOR)
9087 cop0 = gimple_assign_rhs1 (def);
9088 if (TREE_CODE (op1) == SSA_NAME
9089 && (def = dyn_cast <gassign *> (SSA_NAME_DEF_STMT (op1)))
9090 && gimple_assign_rhs_code (def) == CONSTRUCTOR)
9091 cop1 = gimple_assign_rhs1 (def);
9094 (if ((TREE_CODE (cop0) == VECTOR_CST
9095 || TREE_CODE (cop0) == CONSTRUCTOR)
9096 && (TREE_CODE (cop1) == VECTOR_CST
9097 || TREE_CODE (cop1) == CONSTRUCTOR)
9098 && (t = fold_vec_perm (type, cop0, cop1, sel)))
9102 bool changed = (op0 == op1 && !single_arg);
9103 tree ins = NULL_TREE;
9106 /* See if the permutation is performing a single element
9107 insert from a CONSTRUCTOR or constant and use a BIT_INSERT_EXPR
9108 in that case. But only if the vector mode is supported,
9109 otherwise this is invalid GIMPLE. */
9110 if (op_mode != BLKmode
9111 && (TREE_CODE (cop0) == VECTOR_CST
9112 || TREE_CODE (cop0) == CONSTRUCTOR
9113 || TREE_CODE (cop1) == VECTOR_CST
9114 || TREE_CODE (cop1) == CONSTRUCTOR))
9116 bool insert_first_p = sel.series_p (1, 1, nelts + 1, 1);
9119 /* After canonicalizing the first elt to come from the
9120 first vector we only can insert the first elt from
9121 the first vector. */
9123 if ((ins = fold_read_from_vector (cop0, sel[0])))
9126 /* The above can fail for two-element vectors which always
9127 appear to insert the first element, so try inserting
9128 into the second lane as well. For more than two
9129 elements that's wasted time. */
9130 if (!insert_first_p || (!ins && maybe_eq (nelts, 2u)))
9132 unsigned int encoded_nelts = sel.encoding ().encoded_nelts ();
9133 for (at = 0; at < encoded_nelts; ++at)
9134 if (maybe_ne (sel[at], at))
9136 if (at < encoded_nelts
9137 && (known_eq (at + 1, nelts)
9138 || sel.series_p (at + 1, 1, at + 1, 1)))
9140 if (known_lt (poly_uint64 (sel[at]), nelts))
9141 ins = fold_read_from_vector (cop0, sel[at]);
9143 ins = fold_read_from_vector (cop1, sel[at] - nelts);
9148 /* Generate a canonical form of the selector. */
9149 if (!ins && sel.encoding () != builder)
9151 /* Some targets are deficient and fail to expand a single
9152 argument permutation while still allowing an equivalent
9153 2-argument version. */
9155 if (sel.ninputs () == 2
9156 || can_vec_perm_const_p (result_mode, op_mode, sel, false))
9157 op2 = vec_perm_indices_to_tree (TREE_TYPE (op2), sel);
9160 vec_perm_indices sel2 (builder, 2, nelts);
9161 if (can_vec_perm_const_p (result_mode, op_mode, sel2, false))
9162 op2 = vec_perm_indices_to_tree (TREE_TYPE (op2), sel2);
9164 /* Not directly supported with either encoding,
9165 so use the preferred form. */
9166 op2 = vec_perm_indices_to_tree (TREE_TYPE (op2), sel);
9168 if (!operand_equal_p (op2, oldop2, 0))
9173 (bit_insert { op0; } { ins; }
9174 { bitsize_int (at * vector_element_bits (type)); })
9176 (vec_perm { op0; } { op1; } { op2; }))))))))))))
9178 /* VEC_PERM_EXPR (v, v, mask) -> v where v contains same element. */
9180 (match vec_same_elem_p
9183 (match vec_same_elem_p
9185 (if (TREE_CODE (@0) == SSA_NAME
9186 && uniform_vector_p (gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0))))))
9188 (match vec_same_elem_p
9190 (if (uniform_vector_p (@0))))
9194 (vec_perm vec_same_elem_p@0 @0 @1)
9195 (if (types_match (type, TREE_TYPE (@0)))
9199 tree elem = uniform_vector_p (@0);
9202 { build_vector_from_val (type, elem); }))))
9204 /* Push VEC_PERM earlier if that may help FMA perception (PR101895). */
9206 (plus:c (vec_perm:s (mult:c@0 @1 vec_same_elem_p@2) @0 @3) @4)
9207 (if (TREE_CODE (@0) == SSA_NAME && num_imm_uses (@0) == 2)
9208 (plus (mult (vec_perm @1 @1 @3) @2) @4)))
9210 (minus (vec_perm:s (mult:c@0 @1 vec_same_elem_p@2) @0 @3) @4)
9211 (if (TREE_CODE (@0) == SSA_NAME && num_imm_uses (@0) == 2)
9212 (minus (mult (vec_perm @1 @1 @3) @2) @4)))
9216 c = VEC_PERM_EXPR <a, b, VCST0>;
9217 d = VEC_PERM_EXPR <c, c, VCST1>;
9219 d = VEC_PERM_EXPR <a, b, NEW_VCST>; */
9222 (vec_perm (vec_perm@0 @1 @2 VECTOR_CST@3) @0 VECTOR_CST@4)
9223 (if (TYPE_VECTOR_SUBPARTS (type).is_constant ())
9226 machine_mode result_mode = TYPE_MODE (type);
9227 machine_mode op_mode = TYPE_MODE (TREE_TYPE (@1));
9228 int nelts = TYPE_VECTOR_SUBPARTS (type).to_constant ();
9229 vec_perm_builder builder0;
9230 vec_perm_builder builder1;
9231 vec_perm_builder builder2 (nelts, nelts, 1);
9233 (if (tree_to_vec_perm_builder (&builder0, @3)
9234 && tree_to_vec_perm_builder (&builder1, @4))
9237 vec_perm_indices sel0 (builder0, 2, nelts);
9238 vec_perm_indices sel1 (builder1, 1, nelts);
9240 for (int i = 0; i < nelts; i++)
9241 builder2.quick_push (sel0[sel1[i].to_constant ()]);
9243 vec_perm_indices sel2 (builder2, 2, nelts);
9245 tree op0 = NULL_TREE;
9246 /* If the new VEC_PERM_EXPR can't be handled but both
9247 original VEC_PERM_EXPRs can, punt.
9248 If one or both of the original VEC_PERM_EXPRs can't be
9249 handled and the new one can't be either, don't increase
9250 number of VEC_PERM_EXPRs that can't be handled. */
9251 if (can_vec_perm_const_p (result_mode, op_mode, sel2, false)
9253 ? (!can_vec_perm_const_p (result_mode, op_mode, sel0, false)
9254 || !can_vec_perm_const_p (result_mode, op_mode, sel1, false))
9255 : !can_vec_perm_const_p (result_mode, op_mode, sel1, false)))
9256 op0 = vec_perm_indices_to_tree (TREE_TYPE (@4), sel2);
9259 (vec_perm @1 @2 { op0; })))))))
9262 c = VEC_PERM_EXPR <a, b, VCST0>;
9263 d = VEC_PERM_EXPR <x, c, VCST1>;
9265 d = VEC_PERM_EXPR <x, {a,b}, NEW_VCST>;
9266 when all elements from a or b are replaced by the later
9270 (vec_perm @5 (vec_perm@0 @1 @2 VECTOR_CST@3) VECTOR_CST@4)
9271 (if (TYPE_VECTOR_SUBPARTS (type).is_constant ())
9274 machine_mode result_mode = TYPE_MODE (type);
9275 machine_mode op_mode = TYPE_MODE (TREE_TYPE (@1));
9276 int nelts = TYPE_VECTOR_SUBPARTS (type).to_constant ();
9277 vec_perm_builder builder0;
9278 vec_perm_builder builder1;
9279 vec_perm_builder builder2 (nelts, nelts, 2);
9281 (if (tree_to_vec_perm_builder (&builder0, @3)
9282 && tree_to_vec_perm_builder (&builder1, @4))
9285 vec_perm_indices sel0 (builder0, 2, nelts);
9286 vec_perm_indices sel1 (builder1, 2, nelts);
9287 bool use_1 = false, use_2 = false;
9289 for (int i = 0; i < nelts; i++)
9291 if (known_lt ((poly_uint64)sel1[i], sel1.nelts_per_input ()))
9292 builder2.quick_push (sel1[i]);
9295 poly_uint64 j = sel0[(sel1[i] - sel1.nelts_per_input ())
9297 if (known_lt (j, sel0.nelts_per_input ()))
9302 j -= sel0.nelts_per_input ();
9304 builder2.quick_push (j + sel1.nelts_per_input ());
9311 vec_perm_indices sel2 (builder2, 2, nelts);
9312 tree op0 = NULL_TREE;
9313 /* If the new VEC_PERM_EXPR can't be handled but both
9314 original VEC_PERM_EXPRs can, punt.
9315 If one or both of the original VEC_PERM_EXPRs can't be
9316 handled and the new one can't be either, don't increase
9317 number of VEC_PERM_EXPRs that can't be handled. */
9318 if (can_vec_perm_const_p (result_mode, op_mode, sel2, false)
9320 ? (!can_vec_perm_const_p (result_mode, op_mode, sel0, false)
9321 || !can_vec_perm_const_p (result_mode, op_mode, sel1, false))
9322 : !can_vec_perm_const_p (result_mode, op_mode, sel1, false)))
9323 op0 = vec_perm_indices_to_tree (TREE_TYPE (@4), sel2);
9328 (vec_perm @5 @1 { op0; }))
9330 (vec_perm @5 @2 { op0; })))))))))))
9332 /* And the case with swapped outer permute sources. */
9335 (vec_perm (vec_perm@0 @1 @2 VECTOR_CST@3) @5 VECTOR_CST@4)
9336 (if (TYPE_VECTOR_SUBPARTS (type).is_constant ())
9339 machine_mode result_mode = TYPE_MODE (type);
9340 machine_mode op_mode = TYPE_MODE (TREE_TYPE (@1));
9341 int nelts = TYPE_VECTOR_SUBPARTS (type).to_constant ();
9342 vec_perm_builder builder0;
9343 vec_perm_builder builder1;
9344 vec_perm_builder builder2 (nelts, nelts, 2);
9346 (if (tree_to_vec_perm_builder (&builder0, @3)
9347 && tree_to_vec_perm_builder (&builder1, @4))
9350 vec_perm_indices sel0 (builder0, 2, nelts);
9351 vec_perm_indices sel1 (builder1, 2, nelts);
9352 bool use_1 = false, use_2 = false;
9354 for (int i = 0; i < nelts; i++)
9356 if (known_ge ((poly_uint64)sel1[i], sel1.nelts_per_input ()))
9357 builder2.quick_push (sel1[i]);
9360 poly_uint64 j = sel0[sel1[i].to_constant ()];
9361 if (known_lt (j, sel0.nelts_per_input ()))
9366 j -= sel0.nelts_per_input ();
9368 builder2.quick_push (j);
9375 vec_perm_indices sel2 (builder2, 2, nelts);
9376 tree op0 = NULL_TREE;
9377 /* If the new VEC_PERM_EXPR can't be handled but both
9378 original VEC_PERM_EXPRs can, punt.
9379 If one or both of the original VEC_PERM_EXPRs can't be
9380 handled and the new one can't be either, don't increase
9381 number of VEC_PERM_EXPRs that can't be handled. */
9382 if (can_vec_perm_const_p (result_mode, op_mode, sel2, false)
9384 ? (!can_vec_perm_const_p (result_mode, op_mode, sel0, false)
9385 || !can_vec_perm_const_p (result_mode, op_mode, sel1, false))
9386 : !can_vec_perm_const_p (result_mode, op_mode, sel1, false)))
9387 op0 = vec_perm_indices_to_tree (TREE_TYPE (@4), sel2);
9392 (vec_perm @1 @5 { op0; }))
9394 (vec_perm @2 @5 { op0; })))))))))))
9397 /* Match count trailing zeroes for simplify_count_trailing_zeroes in fwprop.
9398 The canonical form is array[((x & -x) * C) >> SHIFT] where C is a magic
9399 constant which when multiplied by a power of 2 contains a unique value
9400 in the top 5 or 6 bits. This is then indexed into a table which maps it
9401 to the number of trailing zeroes. */
9402 (match (ctz_table_index @1 @2 @3)
9403 (rshift (mult (bit_and:c (negate @1) @1) INTEGER_CST@2) INTEGER_CST@3))
9405 (match (cond_expr_convert_p @0 @2 @3 @6)
9406 (cond (simple_comparison@6 @0 @1) (convert@4 @2) (convert@5 @3))
9407 (if (INTEGRAL_TYPE_P (type)
9408 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
9409 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
9410 && INTEGRAL_TYPE_P (TREE_TYPE (@3))
9411 && TYPE_PRECISION (type) != TYPE_PRECISION (TREE_TYPE (@0))
9412 && TYPE_PRECISION (TREE_TYPE (@0))
9413 == TYPE_PRECISION (TREE_TYPE (@2))
9414 && TYPE_PRECISION (TREE_TYPE (@0))
9415 == TYPE_PRECISION (TREE_TYPE (@3))
9416 /* For vect_recog_cond_expr_convert_pattern, @2 and @3 can differ in
9417 signess when convert is truncation, but not ok for extension since
9418 it's sign_extend vs zero_extend. */
9419 && (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type)
9420 || (TYPE_UNSIGNED (TREE_TYPE (@2))
9421 == TYPE_UNSIGNED (TREE_TYPE (@3))))
9423 && single_use (@5))))
9425 (for bit_op (bit_and bit_ior bit_xor)
9426 (match (bitwise_induction_p @0 @2 @3)
9428 (nop_convert1? (bit_not2?@0 (convert3? (lshift integer_onep@1 @2))))
9431 (match (bitwise_induction_p @0 @2 @3)
9433 (nop_convert1? (bit_xor@0 (convert2? (lshift integer_onep@1 @2)) @3))))
9435 /* n - (((n > C1) ? n : C1) & -C2) -> n & C1 for unsigned case.
9436 n - (((n > C1) ? n : C1) & -C2) -> (n <= C1) ? n : (n & C1) for signed case. */
9438 (minus @0 (bit_and (max @0 INTEGER_CST@1) INTEGER_CST@2))
9439 (with { auto i = wi::neg (wi::to_wide (@2)); }
9440 /* Check if -C2 is a power of 2 and C1 = -C2 - 1. */
9441 (if (wi::popcount (i) == 1
9442 && (wi::to_wide (@1)) == (i - 1))
9443 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
9445 (cond (le @0 @1) @0 (bit_and @0 @1))))))
9447 /* -x & 1 -> x & 1. */
9449 (bit_and (negate @0) integer_onep@1)
9450 (if (!TYPE_OVERFLOW_SANITIZED (type))
9453 /* `-a` is just `a` if the type is 1bit wide or when converting
9454 to a 1bit type; similar to the above transformation of `(-x)&1`.
9455 This is used mostly with the transformation of
9456 `a ? ~b : b` into `(-a)^b`.
9457 It also can show up with bitfields. */
9459 (convert? (negate @0))
9460 (if (INTEGRAL_TYPE_P (type)
9461 && TYPE_PRECISION (type) == 1
9462 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0)))
9466 c1 = VEC_PERM_EXPR (a, a, mask)
9467 c2 = VEC_PERM_EXPR (b, b, mask)
9471 c3 = VEC_PERM_EXPR (c, c, mask)
9472 For all integer non-div operations. */
9473 (for op (plus minus mult bit_and bit_ior bit_xor
9476 (op (vec_perm @0 @0 @2) (vec_perm @1 @1 @2))
9477 (if (VECTOR_INTEGER_TYPE_P (type))
9478 (vec_perm (op@3 @0 @1) @3 @2))))
9480 /* Similar for float arithmetic when permutation constant covers
9481 all vector elements. */
9482 (for op (plus minus mult)
9484 (op (vec_perm @0 @0 VECTOR_CST@2) (vec_perm @1 @1 VECTOR_CST@2))
9485 (if (VECTOR_FLOAT_TYPE_P (type)
9486 && TYPE_VECTOR_SUBPARTS (type).is_constant ())
9490 vec_perm_builder builder;
9491 bool full_perm_p = false;
9492 if (tree_to_vec_perm_builder (&builder, perm_cst))
9494 unsigned HOST_WIDE_INT nelts;
9496 nelts = TYPE_VECTOR_SUBPARTS (type).to_constant ();
9497 /* Create a vec_perm_indices for the VECTOR_CST. */
9498 vec_perm_indices sel (builder, 1, nelts);
9500 /* Check if perm indices covers all vector elements. */
9501 if (sel.encoding ().encoded_full_vector_p ())
9503 auto_sbitmap seen (nelts);
9504 bitmap_clear (seen);
9506 unsigned HOST_WIDE_INT count = 0, i;
9508 for (i = 0; i < nelts; i++)
9510 if (!bitmap_set_bit (seen, sel[i].to_constant ()))
9514 full_perm_p = count == nelts;
9519 (vec_perm (op@3 @0 @1) @3 @2))))))