1 /* Match-and-simplify patterns for shared GENERIC and GIMPLE folding.
2 This file is consumed by genmatch which produces gimple-match.c
3 and generic-match.c from it.
5 Copyright (C) 2014-2021 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)
56 #include "cfn-operators.pd"
58 /* Define operand lists for math rounding functions {,i,l,ll}FN,
59 where the versions prefixed with "i" return an int, those prefixed with
60 "l" return a long and those prefixed with "ll" return a long long.
62 Also define operand lists:
64 X<FN>F for all float functions, in the order i, l, ll
65 X<FN> for all double functions, in the same order
66 X<FN>L for all long double functions, in the same order. */
67 #define DEFINE_INT_AND_FLOAT_ROUND_FN(FN) \
68 (define_operator_list X##FN##F BUILT_IN_I##FN##F \
71 (define_operator_list X##FN BUILT_IN_I##FN \
74 (define_operator_list X##FN##L BUILT_IN_I##FN##L \
78 DEFINE_INT_AND_FLOAT_ROUND_FN (FLOOR)
79 DEFINE_INT_AND_FLOAT_ROUND_FN (CEIL)
80 DEFINE_INT_AND_FLOAT_ROUND_FN (ROUND)
81 DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
83 /* Unary operations and their associated IFN_COND_* function. */
84 (define_operator_list UNCOND_UNARY
86 (define_operator_list COND_UNARY
89 /* Binary operations and their associated IFN_COND_* function. */
90 (define_operator_list UNCOND_BINARY
92 mult trunc_div trunc_mod rdiv
95 bit_and bit_ior bit_xor
97 (define_operator_list COND_BINARY
98 IFN_COND_ADD IFN_COND_SUB
99 IFN_COND_MUL IFN_COND_DIV IFN_COND_MOD IFN_COND_RDIV
100 IFN_COND_MIN IFN_COND_MAX
101 IFN_COND_FMIN IFN_COND_FMAX
102 IFN_COND_AND IFN_COND_IOR IFN_COND_XOR
103 IFN_COND_SHL IFN_COND_SHR)
105 /* Same for ternary operations. */
106 (define_operator_list UNCOND_TERNARY
107 IFN_FMA IFN_FMS IFN_FNMA IFN_FNMS)
108 (define_operator_list COND_TERNARY
109 IFN_COND_FMA IFN_COND_FMS IFN_COND_FNMA IFN_COND_FNMS)
111 /* __atomic_fetch_or_*, __atomic_fetch_xor_*, __atomic_xor_fetch_* */
112 (define_operator_list ATOMIC_FETCH_OR_XOR_N
113 BUILT_IN_ATOMIC_FETCH_OR_1 BUILT_IN_ATOMIC_FETCH_OR_2
114 BUILT_IN_ATOMIC_FETCH_OR_4 BUILT_IN_ATOMIC_FETCH_OR_8
115 BUILT_IN_ATOMIC_FETCH_OR_16
116 BUILT_IN_ATOMIC_FETCH_XOR_1 BUILT_IN_ATOMIC_FETCH_XOR_2
117 BUILT_IN_ATOMIC_FETCH_XOR_4 BUILT_IN_ATOMIC_FETCH_XOR_8
118 BUILT_IN_ATOMIC_FETCH_XOR_16
119 BUILT_IN_ATOMIC_XOR_FETCH_1 BUILT_IN_ATOMIC_XOR_FETCH_2
120 BUILT_IN_ATOMIC_XOR_FETCH_4 BUILT_IN_ATOMIC_XOR_FETCH_8
121 BUILT_IN_ATOMIC_XOR_FETCH_16)
122 /* __sync_fetch_and_or_*, __sync_fetch_and_xor_*, __sync_xor_and_fetch_* */
123 (define_operator_list SYNC_FETCH_OR_XOR_N
124 BUILT_IN_SYNC_FETCH_AND_OR_1 BUILT_IN_SYNC_FETCH_AND_OR_2
125 BUILT_IN_SYNC_FETCH_AND_OR_4 BUILT_IN_SYNC_FETCH_AND_OR_8
126 BUILT_IN_SYNC_FETCH_AND_OR_16
127 BUILT_IN_SYNC_FETCH_AND_XOR_1 BUILT_IN_SYNC_FETCH_AND_XOR_2
128 BUILT_IN_SYNC_FETCH_AND_XOR_4 BUILT_IN_SYNC_FETCH_AND_XOR_8
129 BUILT_IN_SYNC_FETCH_AND_XOR_16
130 BUILT_IN_SYNC_XOR_AND_FETCH_1 BUILT_IN_SYNC_XOR_AND_FETCH_2
131 BUILT_IN_SYNC_XOR_AND_FETCH_4 BUILT_IN_SYNC_XOR_AND_FETCH_8
132 BUILT_IN_SYNC_XOR_AND_FETCH_16)
133 /* __atomic_fetch_and_*. */
134 (define_operator_list ATOMIC_FETCH_AND_N
135 BUILT_IN_ATOMIC_FETCH_AND_1 BUILT_IN_ATOMIC_FETCH_AND_2
136 BUILT_IN_ATOMIC_FETCH_AND_4 BUILT_IN_ATOMIC_FETCH_AND_8
137 BUILT_IN_ATOMIC_FETCH_AND_16)
138 /* __sync_fetch_and_and_*. */
139 (define_operator_list SYNC_FETCH_AND_AND_N
140 BUILT_IN_SYNC_FETCH_AND_AND_1 BUILT_IN_SYNC_FETCH_AND_AND_2
141 BUILT_IN_SYNC_FETCH_AND_AND_4 BUILT_IN_SYNC_FETCH_AND_AND_8
142 BUILT_IN_SYNC_FETCH_AND_AND_16)
144 /* With nop_convert? combine convert? and view_convert? in one pattern
145 plus conditionalize on tree_nop_conversion_p conversions. */
146 (match (nop_convert @0)
148 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))))
149 (match (nop_convert @0)
151 (if (VECTOR_TYPE_P (type) && VECTOR_TYPE_P (TREE_TYPE (@0))
152 && known_eq (TYPE_VECTOR_SUBPARTS (type),
153 TYPE_VECTOR_SUBPARTS (TREE_TYPE (@0)))
154 && tree_nop_conversion_p (TREE_TYPE (type), TREE_TYPE (TREE_TYPE (@0))))))
156 /* Transform likes of (char) ABS_EXPR <(int) x> into (char) ABSU_EXPR <x>
157 ABSU_EXPR returns unsigned absolute value of the operand and the operand
158 of the ABSU_EXPR will have the corresponding signed type. */
159 (simplify (abs (convert @0))
160 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
161 && !TYPE_UNSIGNED (TREE_TYPE (@0))
162 && element_precision (type) > element_precision (TREE_TYPE (@0)))
163 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
164 (convert (absu:utype @0)))))
167 /* Optimize (X + (X >> (prec - 1))) ^ (X >> (prec - 1)) into abs (X). */
169 (bit_xor:c (plus:c @0 (rshift@2 @0 INTEGER_CST@1)) @2)
170 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
171 && !TYPE_UNSIGNED (TREE_TYPE (@0))
172 && wi::to_widest (@1) == element_precision (TREE_TYPE (@0)) - 1)
176 /* Simplifications of operations with one constant operand and
177 simplifications to constants or single values. */
179 (for op (plus pointer_plus minus bit_ior bit_xor)
181 (op @0 integer_zerop)
184 /* 0 +p index -> (type)index */
186 (pointer_plus integer_zerop @1)
187 (non_lvalue (convert @1)))
189 /* ptr - 0 -> (type)ptr */
191 (pointer_diff @0 integer_zerop)
194 /* See if ARG1 is zero and X + ARG1 reduces to X.
195 Likewise if the operands are reversed. */
197 (plus:c @0 real_zerop@1)
198 (if (fold_real_zero_addition_p (type, @0, @1, 0))
201 /* See if ARG1 is zero and X - ARG1 reduces to X. */
203 (minus @0 real_zerop@1)
204 (if (fold_real_zero_addition_p (type, @0, @1, 1))
207 /* Even if the fold_real_zero_addition_p can't simplify X + 0.0
208 into X, we can optimize (X + 0.0) + 0.0 or (X + 0.0) - 0.0
209 or (X - 0.0) + 0.0 into X + 0.0 and (X - 0.0) - 0.0 into X - 0.0
210 if not -frounding-math. For sNaNs the first operation would raise
211 exceptions but turn the result into qNan, so the second operation
212 would not raise it. */
213 (for inner_op (plus minus)
214 (for outer_op (plus minus)
216 (outer_op (inner_op@3 @0 REAL_CST@1) REAL_CST@2)
219 && !HONOR_SIGN_DEPENDENT_ROUNDING (type))
220 (with { bool inner_plus = ((inner_op == PLUS_EXPR)
221 ^ REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@1)));
223 = ((outer_op == PLUS_EXPR)
224 ^ REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@2))); }
225 (if (outer_plus && !inner_plus)
230 This is unsafe for certain floats even in non-IEEE formats.
231 In IEEE, it is unsafe because it does wrong for NaNs.
232 Also note that operand_equal_p is always false if an operand
236 (if (!FLOAT_TYPE_P (type)
237 || (!tree_expr_maybe_nan_p (@0)
238 && !tree_expr_maybe_infinite_p (@0)))
239 { build_zero_cst (type); }))
241 (pointer_diff @@0 @0)
242 { build_zero_cst (type); })
245 (mult @0 integer_zerop@1)
248 /* -x == x -> x == 0 */
251 (cmp:c @0 (negate @0))
252 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
253 && !TYPE_OVERFLOW_WRAPS (TREE_TYPE(@0)))
254 (cmp @0 { build_zero_cst (TREE_TYPE(@0)); }))))
256 /* Maybe fold x * 0 to 0. The expressions aren't the same
257 when x is NaN, since x * 0 is also NaN. Nor are they the
258 same in modes with signed zeros, since multiplying a
259 negative value by 0 gives -0, not +0. */
261 (mult @0 real_zerop@1)
262 (if (!tree_expr_maybe_nan_p (@0)
263 && !tree_expr_maybe_real_minus_zero_p (@0)
264 && !tree_expr_maybe_real_minus_zero_p (@1))
267 /* In IEEE floating point, x*1 is not equivalent to x for snans.
268 Likewise for complex arithmetic with signed zeros. */
271 (if (!tree_expr_maybe_signaling_nan_p (@0)
272 && (!HONOR_SIGNED_ZEROS (type)
273 || !COMPLEX_FLOAT_TYPE_P (type)))
276 /* Transform x * -1.0 into -x. */
278 (mult @0 real_minus_onep)
279 (if (!tree_expr_maybe_signaling_nan_p (@0)
280 && (!HONOR_SIGNED_ZEROS (type)
281 || !COMPLEX_FLOAT_TYPE_P (type)))
284 /* Transform { 0 or 1 } * { 0 or 1 } into { 0 or 1 } & { 0 or 1 } */
286 (mult SSA_NAME@1 SSA_NAME@2)
287 (if (INTEGRAL_TYPE_P (type)
288 && get_nonzero_bits (@1) == 1
289 && get_nonzero_bits (@2) == 1)
292 /* Transform x * { 0 or 1, 0 or 1, ... } into x & { 0 or -1, 0 or -1, ...},
293 unless the target has native support for the former but not the latter. */
295 (mult @0 VECTOR_CST@1)
296 (if (initializer_each_zero_or_onep (@1)
297 && !HONOR_SNANS (type)
298 && !HONOR_SIGNED_ZEROS (type))
299 (with { tree itype = FLOAT_TYPE_P (type) ? unsigned_type_for (type) : type; }
301 && (!VECTOR_MODE_P (TYPE_MODE (type))
302 || (VECTOR_MODE_P (TYPE_MODE (itype))
303 && optab_handler (and_optab,
304 TYPE_MODE (itype)) != CODE_FOR_nothing)))
305 (view_convert (bit_and:itype (view_convert @0)
306 (ne @1 { build_zero_cst (type); })))))))
308 (for cmp (gt ge lt le)
309 outp (convert convert negate negate)
310 outn (negate negate convert convert)
311 /* Transform X * (X > 0.0 ? 1.0 : -1.0) into abs(X). */
312 /* Transform X * (X >= 0.0 ? 1.0 : -1.0) into abs(X). */
313 /* Transform X * (X < 0.0 ? 1.0 : -1.0) into -abs(X). */
314 /* Transform X * (X <= 0.0 ? 1.0 : -1.0) into -abs(X). */
316 (mult:c @0 (cond (cmp @0 real_zerop) real_onep@1 real_minus_onep))
317 (if (!tree_expr_maybe_nan_p (@0) && !HONOR_SIGNED_ZEROS (type))
319 /* Transform X * (X > 0.0 ? -1.0 : 1.0) into -abs(X). */
320 /* Transform X * (X >= 0.0 ? -1.0 : 1.0) into -abs(X). */
321 /* Transform X * (X < 0.0 ? -1.0 : 1.0) into abs(X). */
322 /* Transform X * (X <= 0.0 ? -1.0 : 1.0) into abs(X). */
324 (mult:c @0 (cond (cmp @0 real_zerop) real_minus_onep real_onep@1))
325 (if (!tree_expr_maybe_nan_p (@0) && !HONOR_SIGNED_ZEROS (type))
328 /* Transform X * copysign (1.0, X) into abs(X). */
330 (mult:c @0 (COPYSIGN_ALL real_onep @0))
331 (if (!tree_expr_maybe_nan_p (@0) && !HONOR_SIGNED_ZEROS (type))
334 /* Transform X * copysign (1.0, -X) into -abs(X). */
336 (mult:c @0 (COPYSIGN_ALL real_onep (negate @0)))
337 (if (!tree_expr_maybe_nan_p (@0) && !HONOR_SIGNED_ZEROS (type))
340 /* Transform copysign (CST, X) into copysign (ABS(CST), X). */
342 (COPYSIGN_ALL REAL_CST@0 @1)
343 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@0)))
344 (COPYSIGN_ALL (negate @0) @1)))
346 /* X * 1, X / 1 -> X. */
347 (for op (mult trunc_div ceil_div floor_div round_div exact_div)
352 /* (A / (1 << B)) -> (A >> B).
353 Only for unsigned A. For signed A, this would not preserve rounding
355 For example: (-1 / ( 1 << B)) != -1 >> B.
356 Also also widening conversions, like:
357 (A / (unsigned long long) (1U << B)) -> (A >> B)
359 (A / (unsigned long long) (1 << B)) -> (A >> B).
360 If the left shift is signed, it can be done only if the upper bits
361 of A starting from shift's type sign bit are zero, as
362 (unsigned long long) (1 << 31) is -2147483648ULL, not 2147483648ULL,
363 so it is valid only if A >> 31 is zero. */
365 (trunc_div (convert?@0 @3) (convert2? (lshift integer_onep@1 @2)))
366 (if ((TYPE_UNSIGNED (type) || tree_expr_nonnegative_p (@0))
367 && (!VECTOR_TYPE_P (type)
368 || target_supports_op_p (type, RSHIFT_EXPR, optab_vector)
369 || target_supports_op_p (type, RSHIFT_EXPR, optab_scalar))
370 && (useless_type_conversion_p (type, TREE_TYPE (@1))
371 || (element_precision (type) >= element_precision (TREE_TYPE (@1))
372 && (TYPE_UNSIGNED (TREE_TYPE (@1))
373 || (element_precision (type)
374 == element_precision (TREE_TYPE (@1)))
375 || (INTEGRAL_TYPE_P (type)
376 && (tree_nonzero_bits (@0)
377 & wi::mask (element_precision (TREE_TYPE (@1)) - 1,
379 element_precision (type))) == 0)))))
380 (if (!VECTOR_TYPE_P (type)
381 && useless_type_conversion_p (TREE_TYPE (@3), TREE_TYPE (@1))
382 && element_precision (TREE_TYPE (@3)) < element_precision (type))
383 (convert (rshift @3 @2))
386 /* Preserve explicit divisions by 0: the C++ front-end wants to detect
387 undefined behavior in constexpr evaluation, and assuming that the division
388 traps enables better optimizations than these anyway. */
389 (for div (trunc_div ceil_div floor_div round_div exact_div)
390 /* 0 / X is always zero. */
392 (div integer_zerop@0 @1)
393 /* But not for 0 / 0 so that we can get the proper warnings and errors. */
394 (if (!integer_zerop (@1))
398 (div @0 integer_minus_onep@1)
399 (if (!TYPE_UNSIGNED (type))
401 /* X / bool_range_Y is X. */
404 (if (INTEGRAL_TYPE_P (type) && ssa_name_has_boolean_range (@1))
409 /* But not for 0 / 0 so that we can get the proper warnings and errors.
410 And not for _Fract types where we can't build 1. */
411 (if (!integer_zerop (@0) && !ALL_FRACT_MODE_P (TYPE_MODE (type)))
412 { build_one_cst (type); }))
413 /* X / abs (X) is X < 0 ? -1 : 1. */
416 (if (INTEGRAL_TYPE_P (type)
417 && TYPE_OVERFLOW_UNDEFINED (type))
418 (cond (lt @0 { build_zero_cst (type); })
419 { build_minus_one_cst (type); } { build_one_cst (type); })))
422 (div:C @0 (negate @0))
423 (if ((INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
424 && TYPE_OVERFLOW_UNDEFINED (type))
425 { build_minus_one_cst (type); })))
427 /* For unsigned integral types, FLOOR_DIV_EXPR is the same as
428 TRUNC_DIV_EXPR. Rewrite into the latter in this case. */
431 (if ((INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
432 && TYPE_UNSIGNED (type))
435 /* Combine two successive divisions. Note that combining ceil_div
436 and floor_div is trickier and combining round_div even more so. */
437 (for div (trunc_div exact_div)
439 (div (div@3 @0 INTEGER_CST@1) INTEGER_CST@2)
441 wi::overflow_type overflow;
442 wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@2),
443 TYPE_SIGN (type), &overflow);
445 (if (div == EXACT_DIV_EXPR
446 || optimize_successive_divisions_p (@2, @3))
448 (div @0 { wide_int_to_tree (type, mul); })
449 (if (TYPE_UNSIGNED (type)
450 || mul != wi::min_value (TYPE_PRECISION (type), SIGNED))
451 { build_zero_cst (type); }))))))
453 /* Combine successive multiplications. Similar to above, but handling
454 overflow is different. */
456 (mult (mult @0 INTEGER_CST@1) INTEGER_CST@2)
458 wi::overflow_type overflow;
459 wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@2),
460 TYPE_SIGN (type), &overflow);
462 /* Skip folding on overflow: the only special case is @1 * @2 == -INT_MIN,
463 otherwise undefined overflow implies that @0 must be zero. */
464 (if (!overflow || TYPE_OVERFLOW_WRAPS (type))
465 (mult @0 { wide_int_to_tree (type, mul); }))))
467 /* Optimize A / A to 1.0 if we don't care about
468 NaNs or Infinities. */
471 (if (FLOAT_TYPE_P (type)
472 && ! HONOR_NANS (type)
473 && ! HONOR_INFINITIES (type))
474 { build_one_cst (type); }))
476 /* Optimize -A / A to -1.0 if we don't care about
477 NaNs or Infinities. */
479 (rdiv:C @0 (negate @0))
480 (if (FLOAT_TYPE_P (type)
481 && ! HONOR_NANS (type)
482 && ! HONOR_INFINITIES (type))
483 { build_minus_one_cst (type); }))
485 /* PR71078: x / abs(x) -> copysign (1.0, x) */
487 (rdiv:C (convert? @0) (convert? (abs @0)))
488 (if (SCALAR_FLOAT_TYPE_P (type)
489 && ! HONOR_NANS (type)
490 && ! HONOR_INFINITIES (type))
492 (if (types_match (type, float_type_node))
493 (BUILT_IN_COPYSIGNF { build_one_cst (type); } (convert @0)))
494 (if (types_match (type, double_type_node))
495 (BUILT_IN_COPYSIGN { build_one_cst (type); } (convert @0)))
496 (if (types_match (type, long_double_type_node))
497 (BUILT_IN_COPYSIGNL { build_one_cst (type); } (convert @0))))))
499 /* In IEEE floating point, x/1 is not equivalent to x for snans. */
502 (if (!tree_expr_maybe_signaling_nan_p (@0))
505 /* In IEEE floating point, x/-1 is not equivalent to -x for snans. */
507 (rdiv @0 real_minus_onep)
508 (if (!tree_expr_maybe_signaling_nan_p (@0))
511 (if (flag_reciprocal_math)
512 /* Convert (A/B)/C to A/(B*C). */
514 (rdiv (rdiv:s @0 @1) @2)
515 (rdiv @0 (mult @1 @2)))
517 /* Canonicalize x / (C1 * y) to (x * C2) / y. */
519 (rdiv @0 (mult:s @1 REAL_CST@2))
521 { tree tem = const_binop (RDIV_EXPR, type, build_one_cst (type), @2); }
523 (rdiv (mult @0 { tem; } ) @1))))
525 /* Convert A/(B/C) to (A/B)*C */
527 (rdiv @0 (rdiv:s @1 @2))
528 (mult (rdiv @0 @1) @2)))
530 /* Simplify x / (- y) to -x / y. */
532 (rdiv @0 (negate @1))
533 (rdiv (negate @0) @1))
535 (if (flag_unsafe_math_optimizations)
536 /* Simplify (C / x op 0.0) to x op 0.0 for C != 0, C != Inf/Nan.
537 Since C / x may underflow to zero, do this only for unsafe math. */
538 (for op (lt le gt ge)
541 (op (rdiv REAL_CST@0 @1) real_zerop@2)
542 (if (!HONOR_SIGNED_ZEROS (@1) && !HONOR_INFINITIES (@1))
544 (if (real_less (&dconst0, TREE_REAL_CST_PTR (@0)))
546 /* For C < 0, use the inverted operator. */
547 (if (real_less (TREE_REAL_CST_PTR (@0), &dconst0))
550 /* Optimize (X & (-A)) / A where A is a power of 2, to X >> log2(A) */
551 (for div (trunc_div ceil_div floor_div round_div exact_div)
553 (div (convert? (bit_and @0 INTEGER_CST@1)) INTEGER_CST@2)
554 (if (integer_pow2p (@2)
555 && tree_int_cst_sgn (@2) > 0
556 && tree_nop_conversion_p (type, TREE_TYPE (@0))
557 && wi::to_wide (@2) + wi::to_wide (@1) == 0)
559 { build_int_cst (integer_type_node,
560 wi::exact_log2 (wi::to_wide (@2))); }))))
562 /* If ARG1 is a constant, we can convert this to a multiply by the
563 reciprocal. This does not have the same rounding properties,
564 so only do this if -freciprocal-math. We can actually
565 always safely do it if ARG1 is a power of two, but it's hard to
566 tell if it is or not in a portable manner. */
567 (for cst (REAL_CST COMPLEX_CST VECTOR_CST)
571 (if (flag_reciprocal_math
574 { tree tem = const_binop (RDIV_EXPR, type, build_one_cst (type), @1); }
576 (mult @0 { tem; } )))
577 (if (cst != COMPLEX_CST)
578 (with { tree inverse = exact_inverse (type, @1); }
580 (mult @0 { inverse; } ))))))))
582 (for mod (ceil_mod floor_mod round_mod trunc_mod)
583 /* 0 % X is always zero. */
585 (mod integer_zerop@0 @1)
586 /* But not for 0 % 0 so that we can get the proper warnings and errors. */
587 (if (!integer_zerop (@1))
589 /* X % 1 is always zero. */
591 (mod @0 integer_onep)
592 { build_zero_cst (type); })
593 /* X % -1 is zero. */
595 (mod @0 integer_minus_onep@1)
596 (if (!TYPE_UNSIGNED (type))
597 { build_zero_cst (type); }))
601 /* But not for 0 % 0 so that we can get the proper warnings and errors. */
602 (if (!integer_zerop (@0))
603 { build_zero_cst (type); }))
604 /* (X % Y) % Y is just X % Y. */
606 (mod (mod@2 @0 @1) @1)
608 /* From extract_muldiv_1: (X * C1) % C2 is zero if C1 is a multiple of C2. */
610 (mod (mult @0 INTEGER_CST@1) INTEGER_CST@2)
611 (if (ANY_INTEGRAL_TYPE_P (type)
612 && TYPE_OVERFLOW_UNDEFINED (type)
613 && wi::multiple_of_p (wi::to_wide (@1), wi::to_wide (@2),
615 { build_zero_cst (type); }))
616 /* For (X % C) == 0, if X is signed and C is power of 2, use unsigned
617 modulo and comparison, since it is simpler and equivalent. */
620 (cmp (mod @0 integer_pow2p@2) integer_zerop@1)
621 (if (!TYPE_UNSIGNED (TREE_TYPE (@0)))
622 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
623 (cmp (mod (convert:utype @0) (convert:utype @2)) (convert:utype @1)))))))
625 /* X % -C is the same as X % C. */
627 (trunc_mod @0 INTEGER_CST@1)
628 (if (TYPE_SIGN (type) == SIGNED
629 && !TREE_OVERFLOW (@1)
630 && wi::neg_p (wi::to_wide (@1))
631 && !TYPE_OVERFLOW_TRAPS (type)
632 /* Avoid this transformation if C is INT_MIN, i.e. C == -C. */
633 && !sign_bit_p (@1, @1))
634 (trunc_mod @0 (negate @1))))
636 /* X % -Y is the same as X % Y. */
638 (trunc_mod @0 (convert? (negate @1)))
639 (if (INTEGRAL_TYPE_P (type)
640 && !TYPE_UNSIGNED (type)
641 && !TYPE_OVERFLOW_TRAPS (type)
642 && tree_nop_conversion_p (type, TREE_TYPE (@1))
643 /* Avoid this transformation if X might be INT_MIN or
644 Y might be -1, because we would then change valid
645 INT_MIN % -(-1) into invalid INT_MIN % -1. */
646 && (expr_not_equal_to (@0, wi::to_wide (TYPE_MIN_VALUE (type)))
647 || expr_not_equal_to (@1, wi::minus_one (TYPE_PRECISION
649 (trunc_mod @0 (convert @1))))
651 /* X - (X / Y) * Y is the same as X % Y. */
653 (minus (convert1? @0) (convert2? (mult:c (trunc_div @@0 @@1) @1)))
654 (if (INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
655 (convert (trunc_mod @0 @1))))
657 /* x * (1 + y / x) - y -> x - y % x */
659 (minus (mult:cs @0 (plus:s (trunc_div:s @1 @0) integer_onep)) @1)
660 (if (INTEGRAL_TYPE_P (type))
661 (minus @0 (trunc_mod @1 @0))))
663 /* Optimize TRUNC_MOD_EXPR by a power of two into a BIT_AND_EXPR,
664 i.e. "X % C" into "X & (C - 1)", if X and C are positive.
665 Also optimize A % (C << N) where C is a power of 2,
666 to A & ((C << N) - 1).
667 Also optimize "A shift (B % C)", if C is a power of 2, to
668 "A shift (B & (C - 1))". SHIFT operation include "<<" and ">>"
669 and assume (B % C) is nonnegative as shifts negative values would
671 (match (power_of_two_cand @1)
673 (match (power_of_two_cand @1)
674 (lshift INTEGER_CST@1 @2))
675 (for mod (trunc_mod floor_mod)
676 (for shift (lshift rshift)
678 (shift @0 (mod @1 (power_of_two_cand@2 @3)))
679 (if (integer_pow2p (@3) && tree_int_cst_sgn (@3) > 0)
680 (shift @0 (bit_and @1 (minus @2 { build_int_cst (TREE_TYPE (@2),
683 (mod @0 (convert? (power_of_two_cand@1 @2)))
684 (if ((TYPE_UNSIGNED (type) || tree_expr_nonnegative_p (@0))
685 /* Allow any integral conversions of the divisor, except
686 conversion from narrower signed to wider unsigned type
687 where if @1 would be negative power of two, the divisor
688 would not be a power of two. */
689 && INTEGRAL_TYPE_P (type)
690 && INTEGRAL_TYPE_P (TREE_TYPE (@1))
691 && (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@1))
692 || TYPE_UNSIGNED (TREE_TYPE (@1))
693 || !TYPE_UNSIGNED (type))
694 && integer_pow2p (@2) && tree_int_cst_sgn (@2) > 0)
695 (with { tree utype = TREE_TYPE (@1);
696 if (!TYPE_OVERFLOW_WRAPS (utype))
697 utype = unsigned_type_for (utype); }
698 (bit_and @0 (convert (minus (convert:utype @1)
699 { build_one_cst (utype); })))))))
701 /* Simplify (unsigned t * 2)/2 -> unsigned t & 0x7FFFFFFF. */
703 (trunc_div (mult @0 integer_pow2p@1) @1)
704 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
705 (bit_and @0 { wide_int_to_tree
706 (type, wi::mask (TYPE_PRECISION (type)
707 - wi::exact_log2 (wi::to_wide (@1)),
708 false, TYPE_PRECISION (type))); })))
710 /* Simplify (unsigned t / 2) * 2 -> unsigned t & ~1. */
712 (mult (trunc_div @0 integer_pow2p@1) @1)
713 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
714 (bit_and @0 (negate @1))))
716 /* Simplify (t * 2) / 2) -> t. */
717 (for div (trunc_div ceil_div floor_div round_div exact_div)
719 (div (mult:c @0 @1) @1)
720 (if (ANY_INTEGRAL_TYPE_P (type))
721 (if (TYPE_OVERFLOW_UNDEFINED (type))
726 bool overflowed = true;
727 value_range vr0, vr1;
728 if (INTEGRAL_TYPE_P (type)
729 && get_global_range_query ()->range_of_expr (vr0, @0)
730 && get_global_range_query ()->range_of_expr (vr1, @1)
731 && vr0.kind () == VR_RANGE
732 && vr1.kind () == VR_RANGE)
734 wide_int wmin0 = vr0.lower_bound ();
735 wide_int wmax0 = vr0.upper_bound ();
736 wide_int wmin1 = vr1.lower_bound ();
737 wide_int wmax1 = vr1.upper_bound ();
738 /* If the multiplication can't overflow/wrap around, then
739 it can be optimized too. */
740 wi::overflow_type min_ovf, max_ovf;
741 wi::mul (wmin0, wmin1, TYPE_SIGN (type), &min_ovf);
742 wi::mul (wmax0, wmax1, TYPE_SIGN (type), &max_ovf);
743 if (min_ovf == wi::OVF_NONE && max_ovf == wi::OVF_NONE)
745 wi::mul (wmin0, wmax1, TYPE_SIGN (type), &min_ovf);
746 wi::mul (wmax0, wmin1, TYPE_SIGN (type), &max_ovf);
747 if (min_ovf == wi::OVF_NONE && max_ovf == wi::OVF_NONE)
758 /* Simplify cos(-x) and cos(|x|) -> cos(x). Similarly for cosh. */
763 /* Simplify pow(-x, y) and pow(|x|,y) -> pow(x,y) if y is an even integer. */
766 (pows (op @0) REAL_CST@1)
767 (with { HOST_WIDE_INT n; }
768 (if (real_isinteger (&TREE_REAL_CST (@1), &n) && (n & 1) == 0)
770 /* Likewise for powi. */
773 (pows (op @0) INTEGER_CST@1)
774 (if ((wi::to_wide (@1) & 1) == 0)
776 /* Strip negate and abs from both operands of hypot. */
784 /* copysign(-x, y) and copysign(abs(x), y) -> copysign(x, y). */
785 (for copysigns (COPYSIGN_ALL)
787 (copysigns (op @0) @1)
790 /* abs(x)*abs(x) -> x*x. Should be valid for all types. */
795 /* Convert absu(x)*absu(x) -> x*x. */
797 (mult (absu@1 @0) @1)
798 (mult (convert@2 @0) @2))
800 /* cos(copysign(x, y)) -> cos(x). Similarly for cosh. */
804 (coss (copysigns @0 @1))
807 /* pow(copysign(x, y), z) -> pow(x, z) if z is an even integer. */
811 (pows (copysigns @0 @2) REAL_CST@1)
812 (with { HOST_WIDE_INT n; }
813 (if (real_isinteger (&TREE_REAL_CST (@1), &n) && (n & 1) == 0)
815 /* Likewise for powi. */
819 (pows (copysigns @0 @2) INTEGER_CST@1)
820 (if ((wi::to_wide (@1) & 1) == 0)
825 /* hypot(copysign(x, y), z) -> hypot(x, z). */
827 (hypots (copysigns @0 @1) @2)
829 /* hypot(x, copysign(y, z)) -> hypot(x, y). */
831 (hypots @0 (copysigns @1 @2))
834 /* copysign(x, CST) -> [-]abs (x). */
835 (for copysigns (COPYSIGN_ALL)
837 (copysigns @0 REAL_CST@1)
838 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
842 /* copysign(copysign(x, y), z) -> copysign(x, z). */
843 (for copysigns (COPYSIGN_ALL)
845 (copysigns (copysigns @0 @1) @2)
848 /* copysign(x,y)*copysign(x,y) -> x*x. */
849 (for copysigns (COPYSIGN_ALL)
851 (mult (copysigns@2 @0 @1) @2)
854 /* ccos(-x) -> ccos(x). Similarly for ccosh. */
855 (for ccoss (CCOS CCOSH)
860 /* cabs(-x) and cos(conj(x)) -> cabs(x). */
861 (for ops (conj negate)
867 /* Fold (a * (1 << b)) into (a << b) */
869 (mult:c @0 (convert? (lshift integer_onep@1 @2)))
870 (if (! FLOAT_TYPE_P (type)
871 && tree_nop_conversion_p (type, TREE_TYPE (@1)))
874 /* Fold (1 << (C - x)) where C = precision(type) - 1
875 into ((1 << C) >> x). */
877 (lshift integer_onep@0 (minus@1 INTEGER_CST@2 @3))
878 (if (INTEGRAL_TYPE_P (type)
879 && wi::eq_p (wi::to_wide (@2), TYPE_PRECISION (type) - 1)
881 (if (TYPE_UNSIGNED (type))
882 (rshift (lshift @0 @2) @3)
884 { tree utype = unsigned_type_for (type); }
885 (convert (rshift (lshift (convert:utype @0) @2) @3))))))
887 /* Fold ((type)(a<0)) << SIGNBITOFA into ((type)a) & signbit. */
889 (lshift (convert (lt @0 integer_zerop@1)) INTEGER_CST@2)
890 (if (TYPE_SIGN (TREE_TYPE (@0)) == SIGNED
891 && wi::eq_p (wi::to_wide (@2), TYPE_PRECISION (TREE_TYPE (@0)) - 1))
892 (with { wide_int wone = wi::one (TYPE_PRECISION (type)); }
893 (bit_and (convert @0)
894 { wide_int_to_tree (type,
895 wi::lshift (wone, wi::to_wide (@2))); }))))
897 /* Fold (-x >> C) into -(x > 0) where C = precision(type) - 1. */
898 (for cst (INTEGER_CST VECTOR_CST)
900 (rshift (negate:s @0) cst@1)
901 (if (!TYPE_UNSIGNED (type)
902 && TYPE_OVERFLOW_UNDEFINED (type))
903 (with { tree stype = TREE_TYPE (@1);
904 tree bt = truth_type_for (type);
905 tree zeros = build_zero_cst (type);
906 tree cst = NULL_TREE; }
908 /* Handle scalar case. */
909 (if (INTEGRAL_TYPE_P (type)
910 /* If we apply the rule to the scalar type before vectorization
911 we will enforce the result of the comparison being a bool
912 which will require an extra AND on the result that will be
913 indistinguishable from when the user did actually want 0
914 or 1 as the result so it can't be removed. */
915 && canonicalize_math_after_vectorization_p ()
916 && wi::eq_p (wi::to_wide (@1), TYPE_PRECISION (type) - 1))
917 (negate (convert (gt @0 { zeros; }))))
918 /* Handle vector case. */
919 (if (VECTOR_INTEGER_TYPE_P (type)
920 /* First check whether the target has the same mode for vector
921 comparison results as it's operands do. */
922 && TYPE_MODE (bt) == TYPE_MODE (type)
923 /* Then check to see if the target is able to expand the comparison
924 with the given type later on, otherwise we may ICE. */
925 && expand_vec_cmp_expr_p (type, bt, GT_EXPR)
926 && (cst = uniform_integer_cst_p (@1)) != NULL
927 && wi::eq_p (wi::to_wide (cst), element_precision (type) - 1))
928 (view_convert (gt:bt @0 { zeros; }))))))))
930 /* Fold (C1/X)*C2 into (C1*C2)/X. */
932 (mult (rdiv@3 REAL_CST@0 @1) REAL_CST@2)
933 (if (flag_associative_math
936 { tree tem = const_binop (MULT_EXPR, type, @0, @2); }
938 (rdiv { tem; } @1)))))
940 /* Simplify ~X & X as zero. */
942 (bit_and:c (convert? @0) (convert? (bit_not @0)))
943 { build_zero_cst (type); })
945 /* PR71636: Transform x & ((1U << b) - 1) -> x & ~(~0U << b); */
947 (bit_and:c @0 (plus:s (lshift:s integer_onep @1) integer_minus_onep))
948 (if (TYPE_UNSIGNED (type))
949 (bit_and @0 (bit_not (lshift { build_all_ones_cst (type); } @1)))))
951 (for bitop (bit_and bit_ior)
953 /* PR35691: Transform
954 (x == 0 & y == 0) -> (x | typeof(x)(y)) == 0.
955 (x != 0 | y != 0) -> (x | typeof(x)(y)) != 0. */
957 (bitop (cmp @0 integer_zerop@2) (cmp @1 integer_zerop))
958 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
959 && INTEGRAL_TYPE_P (TREE_TYPE (@1))
960 && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
961 (cmp (bit_ior @0 (convert @1)) @2)))
963 (x == -1 & y == -1) -> (x & typeof(x)(y)) == -1.
964 (x != -1 | y != -1) -> (x & typeof(x)(y)) != -1. */
966 (bitop (cmp @0 integer_all_onesp@2) (cmp @1 integer_all_onesp))
967 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
968 && INTEGRAL_TYPE_P (TREE_TYPE (@1))
969 && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
970 (cmp (bit_and @0 (convert @1)) @2))))
972 /* Fold (A & ~B) - (A & B) into (A ^ B) - B. */
974 (minus (bit_and:cs @0 (bit_not @1)) (bit_and:cs @0 @1))
975 (minus (bit_xor @0 @1) @1))
977 (minus (bit_and:s @0 INTEGER_CST@2) (bit_and:s @0 INTEGER_CST@1))
978 (if (~wi::to_wide (@2) == wi::to_wide (@1))
979 (minus (bit_xor @0 @1) @1)))
981 /* Fold (A & B) - (A & ~B) into B - (A ^ B). */
983 (minus (bit_and:cs @0 @1) (bit_and:cs @0 (bit_not @1)))
984 (minus @1 (bit_xor @0 @1)))
986 /* Simplify (X & ~Y) |^+ (~X & Y) -> X ^ Y. */
987 (for op (bit_ior bit_xor plus)
989 (op (bit_and:c @0 (bit_not @1)) (bit_and:c (bit_not @0) @1))
992 (op:c (bit_and @0 INTEGER_CST@2) (bit_and (bit_not @0) INTEGER_CST@1))
993 (if (~wi::to_wide (@2) == wi::to_wide (@1))
996 /* PR53979: Transform ((a ^ b) | a) -> (a | b) */
998 (bit_ior:c (bit_xor:c @0 @1) @0)
1001 /* (a & ~b) | (a ^ b) --> a ^ b */
1003 (bit_ior:c (bit_and:c @0 (bit_not @1)) (bit_xor:c@2 @0 @1))
1006 /* (a & ~b) ^ ~a --> ~(a & b) */
1008 (bit_xor:c (bit_and:cs @0 (bit_not @1)) (bit_not @0))
1009 (bit_not (bit_and @0 @1)))
1011 /* (~a & b) ^ a --> (a | b) */
1013 (bit_xor:c (bit_and:cs (bit_not @0) @1) @0)
1016 /* (a | b) & ~(a ^ b) --> a & b */
1018 (bit_and:c (bit_ior @0 @1) (bit_not (bit_xor:c @0 @1)))
1021 /* a | ~(a ^ b) --> a | ~b */
1023 (bit_ior:c @0 (bit_not:s (bit_xor:c @0 @1)))
1024 (bit_ior @0 (bit_not @1)))
1026 /* (a | b) | (a &^ b) --> a | b */
1027 (for op (bit_and bit_xor)
1029 (bit_ior:c (bit_ior@2 @0 @1) (op:c @0 @1))
1032 /* (a & b) | ~(a ^ b) --> ~(a ^ b) */
1034 (bit_ior:c (bit_and:c @0 @1) (bit_not@2 (bit_xor @0 @1)))
1037 /* ~(~a & b) --> a | ~b */
1039 (bit_not (bit_and:cs (bit_not @0) @1))
1040 (bit_ior @0 (bit_not @1)))
1042 /* ~(~a | b) --> a & ~b */
1044 (bit_not (bit_ior:cs (bit_not @0) @1))
1045 (bit_and @0 (bit_not @1)))
1047 /* (a ^ b) & ((b ^ c) ^ a) --> (a ^ b) & ~c */
1049 (bit_and:c (bit_xor:c@3 @0 @1) (bit_xor:cs (bit_xor:cs @1 @2) @0))
1050 (bit_and @3 (bit_not @2)))
1052 /* (a ^ b) | ((b ^ c) ^ a) --> (a ^ b) | c */
1054 (bit_ior:c (bit_xor:c@3 @0 @1) (bit_xor:c (bit_xor:c @1 @2) @0))
1058 /* (~X | C) ^ D -> (X | C) ^ (~D ^ C) if (~D ^ C) can be simplified. */
1060 (bit_xor:c (bit_ior:cs (bit_not:s @0) @1) @2)
1061 (bit_xor (bit_ior @0 @1) (bit_xor! (bit_not! @2) @1)))
1063 /* (~X & C) ^ D -> (X & C) ^ (D ^ C) if (D ^ C) can be simplified. */
1065 (bit_xor:c (bit_and:cs (bit_not:s @0) @1) @2)
1066 (bit_xor (bit_and @0 @1) (bit_xor! @2 @1)))
1068 /* Simplify (~X & Y) to X ^ Y if we know that (X & ~Y) is 0. */
1070 (bit_and (bit_not SSA_NAME@0) INTEGER_CST@1)
1071 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1072 && wi::bit_and_not (get_nonzero_bits (@0), wi::to_wide (@1)) == 0)
1076 /* For constants M and N, if M == (1LL << cst) - 1 && (N & M) == M,
1077 ((A & N) + B) & M -> (A + B) & M
1078 Similarly if (N & M) == 0,
1079 ((A | N) + B) & M -> (A + B) & M
1080 and for - instead of + (or unary - instead of +)
1081 and/or ^ instead of |.
1082 If B is constant and (B & M) == 0, fold into A & M. */
1083 (for op (plus minus)
1084 (for bitop (bit_and bit_ior bit_xor)
1086 (bit_and (op:s (bitop:s@0 @3 INTEGER_CST@4) @1) INTEGER_CST@2)
1089 tree utype = fold_bit_and_mask (TREE_TYPE (@0), @2, op, @0, bitop,
1090 @3, @4, @1, ERROR_MARK, NULL_TREE,
1093 (convert (bit_and (op (convert:utype { pmop[0]; })
1094 (convert:utype { pmop[1]; }))
1095 (convert:utype @2))))))
1097 (bit_and (op:s @0 (bitop:s@1 @3 INTEGER_CST@4)) INTEGER_CST@2)
1100 tree utype = fold_bit_and_mask (TREE_TYPE (@0), @2, op, @0, ERROR_MARK,
1101 NULL_TREE, NULL_TREE, @1, bitop, @3,
1104 (convert (bit_and (op (convert:utype { pmop[0]; })
1105 (convert:utype { pmop[1]; }))
1106 (convert:utype @2)))))))
1108 (bit_and (op:s @0 @1) INTEGER_CST@2)
1111 tree utype = fold_bit_and_mask (TREE_TYPE (@0), @2, op, @0, ERROR_MARK,
1112 NULL_TREE, NULL_TREE, @1, ERROR_MARK,
1113 NULL_TREE, NULL_TREE, pmop); }
1115 (convert (bit_and (op (convert:utype { pmop[0]; })
1116 (convert:utype { pmop[1]; }))
1117 (convert:utype @2)))))))
1118 (for bitop (bit_and bit_ior bit_xor)
1120 (bit_and (negate:s (bitop:s@0 @2 INTEGER_CST@3)) INTEGER_CST@1)
1123 tree utype = fold_bit_and_mask (TREE_TYPE (@0), @1, NEGATE_EXPR, @0,
1124 bitop, @2, @3, NULL_TREE, ERROR_MARK,
1125 NULL_TREE, NULL_TREE, pmop); }
1127 (convert (bit_and (negate (convert:utype { pmop[0]; }))
1128 (convert:utype @1)))))))
1130 /* X % Y is smaller than Y. */
1133 (cmp (trunc_mod @0 @1) @1)
1134 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
1135 { constant_boolean_node (cmp == LT_EXPR, type); })))
1138 (cmp @1 (trunc_mod @0 @1))
1139 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
1140 { constant_boolean_node (cmp == GT_EXPR, type); })))
1144 (bit_ior @0 integer_all_onesp@1)
1149 (bit_ior @0 integer_zerop)
1154 (bit_and @0 integer_zerop@1)
1160 (for op (bit_ior bit_xor plus)
1162 (op:c (convert? @0) (convert? (bit_not @0)))
1163 (convert { build_all_ones_cst (TREE_TYPE (@0)); })))
1168 { build_zero_cst (type); })
1170 /* Canonicalize X ^ ~0 to ~X. */
1172 (bit_xor @0 integer_all_onesp@1)
1177 (bit_and @0 integer_all_onesp)
1180 /* x & x -> x, x | x -> x */
1181 (for bitop (bit_and bit_ior)
1186 /* x & C -> x if we know that x & ~C == 0. */
1189 (bit_and SSA_NAME@0 INTEGER_CST@1)
1190 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1191 && wi::bit_and_not (get_nonzero_bits (@0), wi::to_wide (@1)) == 0)
1195 /* ~(~X - Y) -> X + Y and ~(~X + Y) -> X - Y. */
1197 (bit_not (minus (bit_not @0) @1))
1200 (bit_not (plus:c (bit_not @0) @1))
1203 /* ~(X - Y) -> ~X + Y. */
1205 (bit_not (minus:s @0 @1))
1206 (plus (bit_not @0) @1))
1208 (bit_not (plus:s @0 INTEGER_CST@1))
1209 (if ((INTEGRAL_TYPE_P (type)
1210 && TYPE_UNSIGNED (type))
1211 || (!TYPE_OVERFLOW_SANITIZED (type)
1212 && may_negate_without_overflow_p (@1)))
1213 (plus (bit_not @0) { const_unop (NEGATE_EXPR, type, @1); })))
1216 /* ~X + Y -> (Y - X) - 1. */
1218 (plus:c (bit_not @0) @1)
1219 (if (ANY_INTEGRAL_TYPE_P (type)
1220 && TYPE_OVERFLOW_WRAPS (type)
1221 /* -1 - X is folded to ~X, so we'd recurse endlessly. */
1222 && !integer_all_onesp (@1))
1223 (plus (minus @1 @0) { build_minus_one_cst (type); })
1224 (if (INTEGRAL_TYPE_P (type)
1225 && TREE_CODE (@1) == INTEGER_CST
1226 && wi::to_wide (@1) != wi::min_value (TYPE_PRECISION (type),
1228 (minus (plus @1 { build_minus_one_cst (type); }) @0))))
1230 /* ~(X >> Y) -> ~X >> Y if ~X can be simplified. */
1232 (bit_not (rshift:s @0 @1))
1233 (if (!TYPE_UNSIGNED (TREE_TYPE (@0)))
1234 (rshift (bit_not! @0) @1)
1235 /* For logical right shifts, this is possible only if @0 doesn't
1236 have MSB set and the logical right shift is changed into
1237 arithmetic shift. */
1238 (if (!wi::neg_p (tree_nonzero_bits (@0)))
1239 (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
1240 (convert (rshift (bit_not! (convert:stype @0)) @1))))))
1243 /* x + (x & 1) -> (x + 1) & ~1 */
1245 (plus:c @0 (bit_and:s @0 integer_onep@1))
1246 (bit_and (plus @0 @1) (bit_not @1)))
1248 /* x & ~(x & y) -> x & ~y */
1249 /* x | ~(x | y) -> x | ~y */
1250 (for bitop (bit_and bit_ior)
1252 (bitop:c @0 (bit_not (bitop:cs @0 @1)))
1253 (bitop @0 (bit_not @1))))
1255 /* (~x & y) | ~(x | y) -> ~x */
1257 (bit_ior:c (bit_and:c (bit_not@2 @0) @1) (bit_not (bit_ior:c @0 @1)))
1260 /* (x | y) ^ (x | ~y) -> ~x */
1262 (bit_xor:c (bit_ior:c @0 @1) (bit_ior:c @0 (bit_not @1)))
1265 /* (x & y) | ~(x | y) -> ~(x ^ y) */
1267 (bit_ior:c (bit_and:s @0 @1) (bit_not:s (bit_ior:s @0 @1)))
1268 (bit_not (bit_xor @0 @1)))
1270 /* (~x | y) ^ (x ^ y) -> x | ~y */
1272 (bit_xor:c (bit_ior:cs (bit_not @0) @1) (bit_xor:s @0 @1))
1273 (bit_ior @0 (bit_not @1)))
1275 /* (x ^ y) | ~(x | y) -> ~(x & y) */
1277 (bit_ior:c (bit_xor:s @0 @1) (bit_not:s (bit_ior:s @0 @1)))
1278 (bit_not (bit_and @0 @1)))
1280 /* (x | y) & ~x -> y & ~x */
1281 /* (x & y) | ~x -> y | ~x */
1282 (for bitop (bit_and bit_ior)
1283 rbitop (bit_ior bit_and)
1285 (bitop:c (rbitop:c @0 @1) (bit_not@2 @0))
1288 /* (x & y) ^ (x | y) -> x ^ y */
1290 (bit_xor:c (bit_and @0 @1) (bit_ior @0 @1))
1293 /* (x ^ y) ^ (x | y) -> x & y */
1295 (bit_xor:c (bit_xor @0 @1) (bit_ior @0 @1))
1298 /* (x & y) + (x ^ y) -> x | y */
1299 /* (x & y) | (x ^ y) -> x | y */
1300 /* (x & y) ^ (x ^ y) -> x | y */
1301 (for op (plus bit_ior bit_xor)
1303 (op:c (bit_and @0 @1) (bit_xor @0 @1))
1306 /* (x & y) + (x | y) -> x + y */
1308 (plus:c (bit_and @0 @1) (bit_ior @0 @1))
1311 /* (x + y) - (x | y) -> x & y */
1313 (minus (plus @0 @1) (bit_ior @0 @1))
1314 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1315 && !TYPE_SATURATING (type))
1318 /* (x + y) - (x & y) -> x | y */
1320 (minus (plus @0 @1) (bit_and @0 @1))
1321 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1322 && !TYPE_SATURATING (type))
1325 /* (x | y) - y -> (x & ~y) */
1327 (minus (bit_ior:cs @0 @1) @1)
1328 (bit_and @0 (bit_not @1)))
1330 /* (x | y) - (x ^ y) -> x & y */
1332 (minus (bit_ior @0 @1) (bit_xor @0 @1))
1335 /* (x | y) - (x & y) -> x ^ y */
1337 (minus (bit_ior @0 @1) (bit_and @0 @1))
1340 /* (x | y) & ~(x & y) -> x ^ y */
1342 (bit_and:c (bit_ior @0 @1) (bit_not (bit_and @0 @1)))
1345 /* (x | y) & (~x ^ y) -> x & y */
1347 (bit_and:c (bit_ior:c @0 @1) (bit_xor:c @1 (bit_not @0)))
1350 /* (~x | y) & (x | ~y) -> ~(x ^ y) */
1352 (bit_and (bit_ior:cs (bit_not @0) @1) (bit_ior:cs @0 (bit_not @1)))
1353 (bit_not (bit_xor @0 @1)))
1355 /* (~x | y) ^ (x | ~y) -> x ^ y */
1357 (bit_xor (bit_ior:c (bit_not @0) @1) (bit_ior:c @0 (bit_not @1)))
1360 /* ((x & y) - (x | y)) - 1 -> ~(x ^ y) */
1362 (plus (nop_convert1? (minus@2 (nop_convert2? (bit_and:c @0 @1))
1363 (nop_convert2? (bit_ior @0 @1))))
1365 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1366 && !TYPE_SATURATING (type) && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2))
1367 && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@2))
1368 && !TYPE_SATURATING (TREE_TYPE (@2)))
1369 (bit_not (convert (bit_xor @0 @1)))))
1371 (minus (nop_convert1? (plus@2 (nop_convert2? (bit_and:c @0 @1))
1373 (nop_convert3? (bit_ior @0 @1)))
1374 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1375 && !TYPE_SATURATING (type) && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2))
1376 && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@2))
1377 && !TYPE_SATURATING (TREE_TYPE (@2)))
1378 (bit_not (convert (bit_xor @0 @1)))))
1380 (minus (nop_convert1? (bit_and @0 @1))
1381 (nop_convert2? (plus@2 (nop_convert3? (bit_ior:c @0 @1))
1383 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1384 && !TYPE_SATURATING (type) && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2))
1385 && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@2))
1386 && !TYPE_SATURATING (TREE_TYPE (@2)))
1387 (bit_not (convert (bit_xor @0 @1)))))
1389 /* ~x & ~y -> ~(x | y)
1390 ~x | ~y -> ~(x & y) */
1391 (for op (bit_and bit_ior)
1392 rop (bit_ior bit_and)
1394 (op (convert1? (bit_not @0)) (convert2? (bit_not @1)))
1395 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1396 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
1397 (bit_not (rop (convert @0) (convert @1))))))
1399 /* If we are XORing or adding two BIT_AND_EXPR's, both of which are and'ing
1400 with a constant, and the two constants have no bits in common,
1401 we should treat this as a BIT_IOR_EXPR since this may produce more
1403 (for op (bit_xor plus)
1405 (op (convert1? (bit_and@4 @0 INTEGER_CST@1))
1406 (convert2? (bit_and@5 @2 INTEGER_CST@3)))
1407 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
1408 && tree_nop_conversion_p (type, TREE_TYPE (@2))
1409 && (wi::to_wide (@1) & wi::to_wide (@3)) == 0)
1410 (bit_ior (convert @4) (convert @5)))))
1412 /* (X | Y) ^ X -> Y & ~ X*/
1414 (bit_xor:c (convert1? (bit_ior:c @@0 @1)) (convert2? @0))
1415 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1416 (convert (bit_and @1 (bit_not @0)))))
1418 /* Convert ~X ^ ~Y to X ^ Y. */
1420 (bit_xor (convert1? (bit_not @0)) (convert2? (bit_not @1)))
1421 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1422 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
1423 (bit_xor (convert @0) (convert @1))))
1425 /* Convert ~X ^ C to X ^ ~C. */
1427 (bit_xor (convert? (bit_not @0)) INTEGER_CST@1)
1428 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1429 (bit_xor (convert @0) (bit_not @1))))
1431 /* Fold (X & Y) ^ Y and (X ^ Y) & Y as ~X & Y. */
1432 (for opo (bit_and bit_xor)
1433 opi (bit_xor bit_and)
1435 (opo:c (opi:cs @0 @1) @1)
1436 (bit_and (bit_not @0) @1)))
1438 /* Given a bit-wise operation CODE applied to ARG0 and ARG1, see if both
1439 operands are another bit-wise operation with a common input. If so,
1440 distribute the bit operations to save an operation and possibly two if
1441 constants are involved. For example, convert
1442 (A | B) & (A | C) into A | (B & C)
1443 Further simplification will occur if B and C are constants. */
1444 (for op (bit_and bit_ior bit_xor)
1445 rop (bit_ior bit_and bit_and)
1447 (op (convert? (rop:c @@0 @1)) (convert? (rop:c @0 @2)))
1448 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1449 && tree_nop_conversion_p (type, TREE_TYPE (@2)))
1450 (rop (convert @0) (op (convert @1) (convert @2))))))
1452 /* Some simple reassociation for bit operations, also handled in reassoc. */
1453 /* (X & Y) & Y -> X & Y
1454 (X | Y) | Y -> X | Y */
1455 (for op (bit_and bit_ior)
1457 (op:c (convert1?@2 (op:c @0 @@1)) (convert2? @1))
1459 /* (X ^ Y) ^ Y -> X */
1461 (bit_xor:c (convert1? (bit_xor:c @0 @@1)) (convert2? @1))
1463 /* (X & Y) & (X & Z) -> (X & Y) & Z
1464 (X | Y) | (X | Z) -> (X | Y) | Z */
1465 (for op (bit_and bit_ior)
1467 (op (convert1?@3 (op:c@4 @0 @1)) (convert2?@5 (op:c@6 @0 @2)))
1468 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1469 && tree_nop_conversion_p (type, TREE_TYPE (@2)))
1470 (if (single_use (@5) && single_use (@6))
1471 (op @3 (convert @2))
1472 (if (single_use (@3) && single_use (@4))
1473 (op (convert @1) @5))))))
1474 /* (X ^ Y) ^ (X ^ Z) -> Y ^ Z */
1476 (bit_xor (convert1? (bit_xor:c @0 @1)) (convert2? (bit_xor:c @0 @2)))
1477 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1478 && tree_nop_conversion_p (type, TREE_TYPE (@2)))
1479 (bit_xor (convert @1) (convert @2))))
1481 /* Convert abs (abs (X)) into abs (X).
1482 also absu (absu (X)) into absu (X). */
1488 (absu (convert@2 (absu@1 @0)))
1489 (if (tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@1)))
1492 /* Convert abs[u] (-X) -> abs[u] (X). */
1501 /* Convert abs[u] (X) where X is nonnegative -> (X). */
1503 (abs tree_expr_nonnegative_p@0)
1507 (absu tree_expr_nonnegative_p@0)
1510 /* Simplify (-(X < 0) | 1) * X into abs (X) or absu(X). */
1512 (mult:c (nop_convert1?
1513 (bit_ior (nop_convert2? (negate (convert? (lt @0 integer_zerop))))
1516 (if (INTEGRAL_TYPE_P (type)
1517 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
1518 && !TYPE_UNSIGNED (TREE_TYPE (@0)))
1519 (if (TYPE_UNSIGNED (type))
1526 /* A few cases of fold-const.c negate_expr_p predicate. */
1527 (match negate_expr_p
1529 (if ((INTEGRAL_TYPE_P (type)
1530 && TYPE_UNSIGNED (type))
1531 || (!TYPE_OVERFLOW_SANITIZED (type)
1532 && may_negate_without_overflow_p (t)))))
1533 (match negate_expr_p
1535 (match negate_expr_p
1537 (if (!TYPE_OVERFLOW_SANITIZED (type))))
1538 (match negate_expr_p
1540 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (t)))))
1541 /* VECTOR_CST handling of non-wrapping types would recurse in unsupported
1543 (match negate_expr_p
1545 (if (FLOAT_TYPE_P (TREE_TYPE (type)) || TYPE_OVERFLOW_WRAPS (type))))
1546 (match negate_expr_p
1548 (if ((ANY_INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_WRAPS (type))
1549 || (FLOAT_TYPE_P (type)
1550 && !HONOR_SIGN_DEPENDENT_ROUNDING (type)
1551 && !HONOR_SIGNED_ZEROS (type)))))
1553 /* (-A) * (-B) -> A * B */
1555 (mult:c (convert1? (negate @0)) (convert2? negate_expr_p@1))
1556 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
1557 && tree_nop_conversion_p (type, TREE_TYPE (@1)))
1558 (mult (convert @0) (convert (negate @1)))))
1560 /* -(A + B) -> (-B) - A. */
1562 (negate (plus:c @0 negate_expr_p@1))
1563 (if (!HONOR_SIGN_DEPENDENT_ROUNDING (type)
1564 && !HONOR_SIGNED_ZEROS (type))
1565 (minus (negate @1) @0)))
1567 /* -(A - B) -> B - A. */
1569 (negate (minus @0 @1))
1570 (if ((ANY_INTEGRAL_TYPE_P (type) && !TYPE_OVERFLOW_SANITIZED (type))
1571 || (FLOAT_TYPE_P (type)
1572 && !HONOR_SIGN_DEPENDENT_ROUNDING (type)
1573 && !HONOR_SIGNED_ZEROS (type)))
1576 (negate (pointer_diff @0 @1))
1577 (if (TYPE_OVERFLOW_UNDEFINED (type))
1578 (pointer_diff @1 @0)))
1580 /* A - B -> A + (-B) if B is easily negatable. */
1582 (minus @0 negate_expr_p@1)
1583 (if (!FIXED_POINT_TYPE_P (type))
1584 (plus @0 (negate @1))))
1586 /* Other simplifications of negation (c.f. fold_negate_expr_1). */
1588 (negate (mult:c@0 @1 negate_expr_p@2))
1589 (if (! TYPE_UNSIGNED (type)
1590 && ! HONOR_SIGN_DEPENDENT_ROUNDING (type)
1592 (mult @1 (negate @2))))
1595 (negate (rdiv@0 @1 negate_expr_p@2))
1596 (if (! HONOR_SIGN_DEPENDENT_ROUNDING (type)
1598 (rdiv @1 (negate @2))))
1601 (negate (rdiv@0 negate_expr_p@1 @2))
1602 (if (! HONOR_SIGN_DEPENDENT_ROUNDING (type)
1604 (rdiv (negate @1) @2)))
1606 /* Fold -((int)x >> (prec - 1)) into (unsigned)x >> (prec - 1). */
1608 (negate (convert? (rshift @0 INTEGER_CST@1)))
1609 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
1610 && wi::to_wide (@1) == element_precision (type) - 1)
1611 (with { tree stype = TREE_TYPE (@0);
1612 tree ntype = TYPE_UNSIGNED (stype) ? signed_type_for (stype)
1613 : unsigned_type_for (stype); }
1614 (convert (rshift:ntype (convert:ntype @0) @1)))))
1616 /* Try to fold (type) X op CST -> (type) (X op ((type-x) CST))
1618 For bitwise binary operations apply operand conversions to the
1619 binary operation result instead of to the operands. This allows
1620 to combine successive conversions and bitwise binary operations.
1621 We combine the above two cases by using a conditional convert. */
1622 (for bitop (bit_and bit_ior bit_xor)
1624 (bitop (convert@2 @0) (convert?@3 @1))
1625 (if (((TREE_CODE (@1) == INTEGER_CST
1626 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
1627 && (int_fits_type_p (@1, TREE_TYPE (@0))
1628 || tree_nop_conversion_p (TREE_TYPE (@0), type)))
1629 || types_match (@0, @1))
1630 /* ??? This transform conflicts with fold-const.c doing
1631 Convert (T)(x & c) into (T)x & (T)c, if c is an integer
1632 constants (if x has signed type, the sign bit cannot be set
1633 in c). This folds extension into the BIT_AND_EXPR.
1634 Restrict it to GIMPLE to avoid endless recursions. */
1635 && (bitop != BIT_AND_EXPR || GIMPLE)
1636 && (/* That's a good idea if the conversion widens the operand, thus
1637 after hoisting the conversion the operation will be narrower.
1638 It is also a good if the conversion is a nop as moves the
1639 conversion to one side; allowing for combining of the conversions. */
1640 TYPE_PRECISION (TREE_TYPE (@0)) < TYPE_PRECISION (type)
1641 /* The conversion check for being a nop can only be done at the gimple
1642 level as fold_binary has some re-association code which can conflict
1643 with this if there is a "constant" which is not a full INTEGER_CST. */
1644 || (GIMPLE && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (type))
1645 /* It's also a good idea if the conversion is to a non-integer
1647 || GET_MODE_CLASS (TYPE_MODE (type)) != MODE_INT
1648 /* Or if the precision of TO is not the same as the precision
1650 || !type_has_mode_precision_p (type)
1651 /* In GIMPLE, getting rid of 2 conversions for one new results
1654 && TREE_CODE (@1) != INTEGER_CST
1655 && tree_nop_conversion_p (type, TREE_TYPE (@0))
1657 && single_use (@3))))
1658 (convert (bitop @0 (convert @1)))))
1659 /* In GIMPLE, getting rid of 2 conversions for one new results
1662 (convert (bitop:cs@2 (nop_convert:s @0) @1))
1664 && TREE_CODE (@1) != INTEGER_CST
1665 && tree_nop_conversion_p (type, TREE_TYPE (@2))
1666 && types_match (type, @0))
1667 (bitop @0 (convert @1)))))
1669 (for bitop (bit_and bit_ior)
1670 rbitop (bit_ior bit_and)
1671 /* (x | y) & x -> x */
1672 /* (x & y) | x -> x */
1674 (bitop:c (rbitop:c @0 @1) @0)
1676 /* (~x | y) & x -> x & y */
1677 /* (~x & y) | x -> x | y */
1679 (bitop:c (rbitop:c (bit_not @0) @1) @0)
1682 /* ((x | y) & z) | x -> (z & y) | x */
1684 (bit_ior:c (bit_and:cs (bit_ior:cs @0 @1) @2) @0)
1685 (bit_ior (bit_and @2 @1) @0))
1687 /* (x | CST1) & CST2 -> (x & CST2) | (CST1 & CST2) */
1689 (bit_and (bit_ior @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
1690 (bit_ior (bit_and @0 @2) (bit_and @1 @2)))
1692 /* Combine successive equal operations with constants. */
1693 (for bitop (bit_and bit_ior bit_xor)
1695 (bitop (bitop @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
1696 (if (!CONSTANT_CLASS_P (@0))
1697 /* This is the canonical form regardless of whether (bitop @1 @2) can be
1698 folded to a constant. */
1699 (bitop @0 (bitop @1 @2))
1700 /* In this case we have three constants and (bitop @0 @1) doesn't fold
1701 to a constant. This can happen if @0 or @1 is a POLY_INT_CST and if
1702 the values involved are such that the operation can't be decided at
1703 compile time. Try folding one of @0 or @1 with @2 to see whether
1704 that combination can be decided at compile time.
1706 Keep the existing form if both folds fail, to avoid endless
1708 (with { tree cst1 = const_binop (bitop, type, @0, @2); }
1710 (bitop @1 { cst1; })
1711 (with { tree cst2 = const_binop (bitop, type, @1, @2); }
1713 (bitop @0 { cst2; }))))))))
1715 /* Try simple folding for X op !X, and X op X with the help
1716 of the truth_valued_p and logical_inverted_value predicates. */
1717 (match truth_valued_p
1719 (if (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1)))
1720 (for op (tcc_comparison truth_and truth_andif truth_or truth_orif truth_xor)
1721 (match truth_valued_p
1723 (match truth_valued_p
1726 (match (logical_inverted_value @0)
1728 (match (logical_inverted_value @0)
1729 (bit_not truth_valued_p@0))
1730 (match (logical_inverted_value @0)
1731 (eq @0 integer_zerop))
1732 (match (logical_inverted_value @0)
1733 (ne truth_valued_p@0 integer_truep))
1734 (match (logical_inverted_value @0)
1735 (bit_xor truth_valued_p@0 integer_truep))
1739 (bit_and:c @0 (logical_inverted_value @0))
1740 { build_zero_cst (type); })
1741 /* X | !X and X ^ !X -> 1, , if X is truth-valued. */
1742 (for op (bit_ior bit_xor)
1744 (op:c truth_valued_p@0 (logical_inverted_value @0))
1745 { constant_boolean_node (true, type); }))
1746 /* X ==/!= !X is false/true. */
1749 (op:c truth_valued_p@0 (logical_inverted_value @0))
1750 { constant_boolean_node (op == NE_EXPR ? true : false, type); }))
1754 (bit_not (bit_not @0))
1757 /* Convert ~ (-A) to A - 1. */
1759 (bit_not (convert? (negate @0)))
1760 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1761 || !TYPE_UNSIGNED (TREE_TYPE (@0)))
1762 (convert (minus @0 { build_each_one_cst (TREE_TYPE (@0)); }))))
1764 /* Convert - (~A) to A + 1. */
1766 (negate (nop_convert? (bit_not @0)))
1767 (plus (view_convert @0) { build_each_one_cst (type); }))
1769 /* Convert ~ (A - 1) or ~ (A + -1) to -A. */
1771 (bit_not (convert? (minus @0 integer_each_onep)))
1772 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1773 || !TYPE_UNSIGNED (TREE_TYPE (@0)))
1774 (convert (negate @0))))
1776 (bit_not (convert? (plus @0 integer_all_onesp)))
1777 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1778 || !TYPE_UNSIGNED (TREE_TYPE (@0)))
1779 (convert (negate @0))))
1781 /* Part of convert ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify. */
1783 (bit_not (convert? (bit_xor @0 INTEGER_CST@1)))
1784 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1785 (convert (bit_xor @0 (bit_not @1)))))
1787 (bit_not (convert? (bit_xor:c (bit_not @0) @1)))
1788 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1789 (convert (bit_xor @0 @1))))
1791 /* Otherwise prefer ~(X ^ Y) to ~X ^ Y as more canonical. */
1793 (bit_xor:c (nop_convert?:s (bit_not:s @0)) @1)
1794 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1795 (bit_not (bit_xor (view_convert @0) @1))))
1797 /* (x & ~m) | (y & m) -> ((x ^ y) & m) ^ x */
1799 (bit_ior:c (bit_and:cs @0 (bit_not @2)) (bit_and:cs @1 @2))
1800 (bit_xor (bit_and (bit_xor @0 @1) @2) @0))
1802 /* Fold A - (A & B) into ~B & A. */
1804 (minus (convert1? @0) (convert2?:s (bit_and:cs @@0 @1)))
1805 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
1806 && tree_nop_conversion_p (type, TREE_TYPE (@1)))
1807 (convert (bit_and (bit_not @1) @0))))
1809 /* (m1 CMP m2) * d -> (m1 CMP m2) ? d : 0 */
1810 (if (!canonicalize_math_p ())
1811 (for cmp (gt lt ge le)
1813 (mult (convert (cmp @0 @1)) @2)
1814 (cond (cmp @0 @1) @2 { build_zero_cst (type); }))))
1816 /* For integral types with undefined overflow and C != 0 fold
1817 x * C EQ/NE y * C into x EQ/NE y. */
1820 (cmp (mult:c @0 @1) (mult:c @2 @1))
1821 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
1822 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1823 && tree_expr_nonzero_p (@1))
1826 /* For integral types with wrapping overflow and C odd fold
1827 x * C EQ/NE y * C into x EQ/NE y. */
1830 (cmp (mult @0 INTEGER_CST@1) (mult @2 @1))
1831 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
1832 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))
1833 && (TREE_INT_CST_LOW (@1) & 1) != 0)
1836 /* For integral types with undefined overflow and C != 0 fold
1837 x * C RELOP y * C into:
1839 x RELOP y for nonnegative C
1840 y RELOP x for negative C */
1841 (for cmp (lt gt le ge)
1843 (cmp (mult:c @0 @1) (mult:c @2 @1))
1844 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
1845 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1846 (if (tree_expr_nonnegative_p (@1) && tree_expr_nonzero_p (@1))
1848 (if (TREE_CODE (@1) == INTEGER_CST
1849 && wi::neg_p (wi::to_wide (@1), TYPE_SIGN (TREE_TYPE (@1))))
1852 /* (X - 1U) <= INT_MAX-1U into (int) X > 0. */
1856 (cmp (plus @0 integer_minus_onep@1) INTEGER_CST@2)
1857 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1858 && TYPE_UNSIGNED (TREE_TYPE (@0))
1859 && TYPE_PRECISION (TREE_TYPE (@0)) > 1
1860 && (wi::to_wide (@2)
1861 == wi::max_value (TYPE_PRECISION (TREE_TYPE (@0)), SIGNED) - 1))
1862 (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
1863 (icmp (convert:stype @0) { build_int_cst (stype, 0); })))))
1865 /* X / 4 < Y / 4 iff X < Y when the division is known to be exact. */
1866 (for cmp (simple_comparison)
1868 (cmp (convert?@3 (exact_div @0 INTEGER_CST@2)) (convert? (exact_div @1 @2)))
1869 (if (element_precision (@3) >= element_precision (@0)
1870 && types_match (@0, @1))
1871 (if (wi::lt_p (wi::to_wide (@2), 0, TYPE_SIGN (TREE_TYPE (@2))))
1872 (if (!TYPE_UNSIGNED (TREE_TYPE (@3)))
1874 (if (tree_expr_nonzero_p (@0) && tree_expr_nonzero_p (@1))
1877 tree utype = unsigned_type_for (TREE_TYPE (@0));
1879 (cmp (convert:utype @1) (convert:utype @0)))))
1880 (if (wi::gt_p (wi::to_wide (@2), 1, TYPE_SIGN (TREE_TYPE (@2))))
1881 (if (TYPE_UNSIGNED (TREE_TYPE (@0)) || !TYPE_UNSIGNED (TREE_TYPE (@3)))
1885 tree utype = unsigned_type_for (TREE_TYPE (@0));
1887 (cmp (convert:utype @0) (convert:utype @1)))))))))
1889 /* X / C1 op C2 into a simple range test. */
1890 (for cmp (simple_comparison)
1892 (cmp (trunc_div:s @0 INTEGER_CST@1) INTEGER_CST@2)
1893 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1894 && integer_nonzerop (@1)
1895 && !TREE_OVERFLOW (@1)
1896 && !TREE_OVERFLOW (@2))
1897 (with { tree lo, hi; bool neg_overflow;
1898 enum tree_code code = fold_div_compare (cmp, @1, @2, &lo, &hi,
1901 (if (code == LT_EXPR || code == GE_EXPR)
1902 (if (TREE_OVERFLOW (lo))
1903 { build_int_cst (type, (code == LT_EXPR) ^ neg_overflow); }
1904 (if (code == LT_EXPR)
1907 (if (code == LE_EXPR || code == GT_EXPR)
1908 (if (TREE_OVERFLOW (hi))
1909 { build_int_cst (type, (code == LE_EXPR) ^ neg_overflow); }
1910 (if (code == LE_EXPR)
1914 { build_int_cst (type, code == NE_EXPR); })
1915 (if (code == EQ_EXPR && !hi)
1917 (if (code == EQ_EXPR && !lo)
1919 (if (code == NE_EXPR && !hi)
1921 (if (code == NE_EXPR && !lo)
1924 { build_range_check (UNKNOWN_LOCATION, type, @0, code == EQ_EXPR,
1928 tree etype = range_check_type (TREE_TYPE (@0));
1931 hi = fold_convert (etype, hi);
1932 lo = fold_convert (etype, lo);
1933 hi = const_binop (MINUS_EXPR, etype, hi, lo);
1936 (if (etype && hi && !TREE_OVERFLOW (hi))
1937 (if (code == EQ_EXPR)
1938 (le (minus (convert:etype @0) { lo; }) { hi; })
1939 (gt (minus (convert:etype @0) { lo; }) { hi; })))))))))
1941 /* X + Z < Y + Z is the same as X < Y when there is no overflow. */
1942 (for op (lt le ge gt)
1944 (op (plus:c @0 @2) (plus:c @1 @2))
1945 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1946 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1948 /* For equality and subtraction, this is also true with wrapping overflow. */
1949 (for op (eq ne minus)
1951 (op (plus:c @0 @2) (plus:c @1 @2))
1952 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1953 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1954 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
1957 /* X - Z < Y - Z is the same as X < Y when there is no overflow. */
1958 (for op (lt le ge gt)
1960 (op (minus @0 @2) (minus @1 @2))
1961 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1962 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1964 /* For equality and subtraction, this is also true with wrapping overflow. */
1965 (for op (eq ne minus)
1967 (op (minus @0 @2) (minus @1 @2))
1968 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1969 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1970 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
1972 /* And for pointers... */
1973 (for op (simple_comparison)
1975 (op (pointer_diff@3 @0 @2) (pointer_diff @1 @2))
1976 (if (!TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
1979 (minus (pointer_diff@3 @0 @2) (pointer_diff @1 @2))
1980 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@3))
1981 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
1982 (pointer_diff @0 @1)))
1984 /* Z - X < Z - Y is the same as Y < X when there is no overflow. */
1985 (for op (lt le ge gt)
1987 (op (minus @2 @0) (minus @2 @1))
1988 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1989 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1991 /* For equality and subtraction, this is also true with wrapping overflow. */
1992 (for op (eq ne minus)
1994 (op (minus @2 @0) (minus @2 @1))
1995 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1996 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1997 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
1999 /* And for pointers... */
2000 (for op (simple_comparison)
2002 (op (pointer_diff@3 @2 @0) (pointer_diff @2 @1))
2003 (if (!TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
2006 (minus (pointer_diff@3 @2 @0) (pointer_diff @2 @1))
2007 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@3))
2008 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
2009 (pointer_diff @1 @0)))
2011 /* X + Y < Y is the same as X < 0 when there is no overflow. */
2012 (for op (lt le gt ge)
2014 (op:c (plus:c@2 @0 @1) @1)
2015 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2016 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2017 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0))
2018 && (CONSTANT_CLASS_P (@0) || single_use (@2)))
2019 (op @0 { build_zero_cst (TREE_TYPE (@0)); }))))
2020 /* For equality, this is also true with wrapping overflow. */
2023 (op:c (nop_convert?@3 (plus:c@2 @0 (convert1? @1))) (convert2? @1))
2024 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2025 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2026 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
2027 && (CONSTANT_CLASS_P (@0) || (single_use (@2) && single_use (@3)))
2028 && tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@2))
2029 && tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@1)))
2030 (op @0 { build_zero_cst (TREE_TYPE (@0)); })))
2032 (op:c (nop_convert?@3 (pointer_plus@2 (convert1? @0) @1)) (convert2? @0))
2033 (if (tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@0))
2034 && tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0))
2035 && (CONSTANT_CLASS_P (@1) || (single_use (@2) && single_use (@3))))
2036 (op @1 { build_zero_cst (TREE_TYPE (@1)); }))))
2038 /* X - Y < X is the same as Y > 0 when there is no overflow.
2039 For equality, this is also true with wrapping overflow. */
2040 (for op (simple_comparison)
2042 (op:c @0 (minus@2 @0 @1))
2043 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2044 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2045 || ((op == EQ_EXPR || op == NE_EXPR)
2046 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
2047 && (CONSTANT_CLASS_P (@1) || single_use (@2)))
2048 (op @1 { build_zero_cst (TREE_TYPE (@1)); }))))
2051 (X / Y) == 0 -> X < Y if X, Y are unsigned.
2052 (X / Y) != 0 -> X >= Y, if X, Y are unsigned. */
2056 (cmp (trunc_div @0 @1) integer_zerop)
2057 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
2058 /* Complex ==/!= is allowed, but not </>=. */
2059 && TREE_CODE (TREE_TYPE (@0)) != COMPLEX_TYPE
2060 && (VECTOR_TYPE_P (type) || !VECTOR_TYPE_P (TREE_TYPE (@0))))
2063 /* X == C - X can never be true if C is odd. */
2066 (cmp:c (convert? @0) (convert1? (minus INTEGER_CST@1 (convert2? @0))))
2067 (if (TREE_INT_CST_LOW (@1) & 1)
2068 { constant_boolean_node (cmp == NE_EXPR, type); })))
2070 /* Arguments on which one can call get_nonzero_bits to get the bits
2072 (match with_possible_nonzero_bits
2074 (match with_possible_nonzero_bits
2076 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))))
2077 /* Slightly extended version, do not make it recursive to keep it cheap. */
2078 (match (with_possible_nonzero_bits2 @0)
2079 with_possible_nonzero_bits@0)
2080 (match (with_possible_nonzero_bits2 @0)
2081 (bit_and:c with_possible_nonzero_bits@0 @2))
2083 /* Same for bits that are known to be set, but we do not have
2084 an equivalent to get_nonzero_bits yet. */
2085 (match (with_certain_nonzero_bits2 @0)
2087 (match (with_certain_nonzero_bits2 @0)
2088 (bit_ior @1 INTEGER_CST@0))
2090 /* X == C (or X & Z == Y | C) is impossible if ~nonzero(X) & C != 0. */
2093 (cmp:c (with_possible_nonzero_bits2 @0) (with_certain_nonzero_bits2 @1))
2094 (if (wi::bit_and_not (wi::to_wide (@1), get_nonzero_bits (@0)) != 0)
2095 { constant_boolean_node (cmp == NE_EXPR, type); })))
2097 /* ((X inner_op C0) outer_op C1)
2098 With X being a tree where value_range has reasoned certain bits to always be
2099 zero throughout its computed value range,
2100 inner_op = {|,^}, outer_op = {|,^} and inner_op != outer_op
2101 where zero_mask has 1's for all bits that are sure to be 0 in
2103 if (inner_op == '^') C0 &= ~C1;
2104 if ((C0 & ~zero_mask) == 0) then emit (X outer_op (C0 outer_op C1)
2105 if ((C1 & ~zero_mask) == 0) then emit (X inner_op (C0 outer_op C1)
2107 (for inner_op (bit_ior bit_xor)
2108 outer_op (bit_xor bit_ior)
2111 (inner_op:s @2 INTEGER_CST@0) INTEGER_CST@1)
2115 wide_int zero_mask_not;
2119 if (TREE_CODE (@2) == SSA_NAME)
2120 zero_mask_not = get_nonzero_bits (@2);
2124 if (inner_op == BIT_XOR_EXPR)
2126 C0 = wi::bit_and_not (wi::to_wide (@0), wi::to_wide (@1));
2127 cst_emit = C0 | wi::to_wide (@1);
2131 C0 = wi::to_wide (@0);
2132 cst_emit = C0 ^ wi::to_wide (@1);
2135 (if (!fail && (C0 & zero_mask_not) == 0)
2136 (outer_op @2 { wide_int_to_tree (type, cst_emit); })
2137 (if (!fail && (wi::to_wide (@1) & zero_mask_not) == 0)
2138 (inner_op @2 { wide_int_to_tree (type, cst_emit); }))))))
2140 /* Associate (p +p off1) +p off2 as (p +p (off1 + off2)). */
2142 (pointer_plus (pointer_plus:s @0 @1) @3)
2143 (pointer_plus @0 (plus @1 @3)))
2149 tem4 = (unsigned long) tem3;
2154 (pointer_plus @0 (convert?@2 (minus@3 (convert @1) (convert @0))))
2155 /* Conditionally look through a sign-changing conversion. */
2156 (if (TYPE_PRECISION (TREE_TYPE (@2)) == TYPE_PRECISION (TREE_TYPE (@3))
2157 && ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@1)))
2158 || (GENERIC && type == TREE_TYPE (@1))))
2161 (pointer_plus @0 (convert?@2 (pointer_diff@3 @1 @@0)))
2162 (if (TYPE_PRECISION (TREE_TYPE (@2)) >= TYPE_PRECISION (TREE_TYPE (@3)))
2166 tem = (sizetype) ptr;
2170 and produce the simpler and easier to analyze with respect to alignment
2171 ... = ptr & ~algn; */
2173 (pointer_plus @0 (negate (bit_and (convert @0) INTEGER_CST@1)))
2174 (with { tree algn = wide_int_to_tree (TREE_TYPE (@0), ~wi::to_wide (@1)); }
2175 (bit_and @0 { algn; })))
2177 /* Try folding difference of addresses. */
2179 (minus (convert ADDR_EXPR@0) (convert @1))
2180 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2181 (with { poly_int64 diff; }
2182 (if (ptr_difference_const (@0, @1, &diff))
2183 { build_int_cst_type (type, diff); }))))
2185 (minus (convert @0) (convert ADDR_EXPR@1))
2186 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2187 (with { poly_int64 diff; }
2188 (if (ptr_difference_const (@0, @1, &diff))
2189 { build_int_cst_type (type, diff); }))))
2191 (pointer_diff (convert?@2 ADDR_EXPR@0) (convert1?@3 @1))
2192 (if (tree_nop_conversion_p (TREE_TYPE(@2), TREE_TYPE (@0))
2193 && tree_nop_conversion_p (TREE_TYPE(@3), TREE_TYPE (@1)))
2194 (with { poly_int64 diff; }
2195 (if (ptr_difference_const (@0, @1, &diff))
2196 { build_int_cst_type (type, diff); }))))
2198 (pointer_diff (convert?@2 @0) (convert1?@3 ADDR_EXPR@1))
2199 (if (tree_nop_conversion_p (TREE_TYPE(@2), TREE_TYPE (@0))
2200 && tree_nop_conversion_p (TREE_TYPE(@3), TREE_TYPE (@1)))
2201 (with { poly_int64 diff; }
2202 (if (ptr_difference_const (@0, @1, &diff))
2203 { build_int_cst_type (type, diff); }))))
2205 /* (&a+b) - (&a[1] + c) -> sizeof(a[0]) + (b - c) */
2207 (pointer_diff (pointer_plus ADDR_EXPR@0 @1) (pointer_plus ADDR_EXPR@2 @3))
2208 (with { poly_int64 diff; }
2209 (if (ptr_difference_const (@0, @2, &diff))
2210 (plus { build_int_cst_type (type, diff); } (convert (minus @1 @3))))))
2212 /* (&a+b) !=/== (&a[1] + c) -> sizeof(a[0]) + b !=/== c */
2215 (neeq (pointer_plus ADDR_EXPR@0 @1) (pointer_plus ADDR_EXPR@2 @3))
2216 (with { poly_int64 diff; tree inner_type = TREE_TYPE (@1);}
2217 (if (ptr_difference_const (@0, @2, &diff))
2218 (neeq (plus { build_int_cst_type (inner_type, diff); } @1) @3)))))
2220 /* Canonicalize (T *)(ptr - ptr-cst) to &MEM[ptr + -ptr-cst]. */
2222 (convert (pointer_diff @0 INTEGER_CST@1))
2223 (if (POINTER_TYPE_P (type))
2224 { build_fold_addr_expr_with_type
2225 (build2 (MEM_REF, char_type_node, @0,
2226 wide_int_to_tree (ptr_type_node, wi::neg (wi::to_wide (@1)))),
2229 /* If arg0 is derived from the address of an object or function, we may
2230 be able to fold this expression using the object or function's
2233 (bit_and (convert? @0) INTEGER_CST@1)
2234 (if (POINTER_TYPE_P (TREE_TYPE (@0))
2235 && tree_nop_conversion_p (type, TREE_TYPE (@0)))
2239 unsigned HOST_WIDE_INT bitpos;
2240 get_pointer_alignment_1 (@0, &align, &bitpos);
2242 (if (wi::ltu_p (wi::to_wide (@1), align / BITS_PER_UNIT))
2243 { wide_int_to_tree (type, (wi::to_wide (@1)
2244 & (bitpos / BITS_PER_UNIT))); }))))
2248 (if (INTEGRAL_TYPE_P (type)
2249 && wi::eq_p (wi::to_wide (t), wi::min_value (type)))))
2253 (if (INTEGRAL_TYPE_P (type)
2254 && wi::eq_p (wi::to_wide (t), wi::max_value (type)))))
2256 /* x > y && x != XXX_MIN --> x > y
2257 x > y && x == XXX_MIN --> false . */
2260 (bit_and:c (gt:c@2 @0 @1) (eqne @0 min_value))
2262 (if (eqne == EQ_EXPR)
2263 { constant_boolean_node (false, type); })
2264 (if (eqne == NE_EXPR)
2268 /* x < y && x != XXX_MAX --> x < y
2269 x < y && x == XXX_MAX --> false. */
2272 (bit_and:c (lt:c@2 @0 @1) (eqne @0 max_value))
2274 (if (eqne == EQ_EXPR)
2275 { constant_boolean_node (false, type); })
2276 (if (eqne == NE_EXPR)
2280 /* x <= y && x == XXX_MIN --> x == XXX_MIN. */
2282 (bit_and:c (le:c @0 @1) (eq@2 @0 min_value))
2285 /* x >= y && x == XXX_MAX --> x == XXX_MAX. */
2287 (bit_and:c (ge:c @0 @1) (eq@2 @0 max_value))
2290 /* x > y || x != XXX_MIN --> x != XXX_MIN. */
2292 (bit_ior:c (gt:c @0 @1) (ne@2 @0 min_value))
2295 /* x <= y || x != XXX_MIN --> true. */
2297 (bit_ior:c (le:c @0 @1) (ne @0 min_value))
2298 { constant_boolean_node (true, type); })
2300 /* x <= y || x == XXX_MIN --> x <= y. */
2302 (bit_ior:c (le:c@2 @0 @1) (eq @0 min_value))
2305 /* x < y || x != XXX_MAX --> x != XXX_MAX. */
2307 (bit_ior:c (lt:c @0 @1) (ne@2 @0 max_value))
2310 /* x >= y || x != XXX_MAX --> true
2311 x >= y || x == XXX_MAX --> x >= y. */
2314 (bit_ior:c (ge:c@2 @0 @1) (eqne @0 max_value))
2316 (if (eqne == EQ_EXPR)
2318 (if (eqne == NE_EXPR)
2319 { constant_boolean_node (true, type); }))))
2321 /* y == XXX_MIN || x < y --> x <= y - 1 */
2323 (bit_ior:c (eq:s @1 min_value) (lt:s @0 @1))
2324 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2325 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
2326 (le @0 (minus @1 { build_int_cst (TREE_TYPE (@1), 1); }))))
2328 /* y != XXX_MIN && x >= y --> x > y - 1 */
2330 (bit_and:c (ne:s @1 min_value) (ge:s @0 @1))
2331 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2332 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
2333 (gt @0 (minus @1 { build_int_cst (TREE_TYPE (@1), 1); }))))
2335 /* Convert (X == CST1) && (X OP2 CST2) to a known value
2336 based on CST1 OP2 CST2. Similarly for (X != CST1). */
2339 (for code2 (eq ne lt gt le ge)
2341 (bit_and:c (code1@3 @0 INTEGER_CST@1) (code2@4 @0 INTEGER_CST@2))
2344 int cmp = tree_int_cst_compare (@1, @2);
2348 case EQ_EXPR: val = (cmp == 0); break;
2349 case NE_EXPR: val = (cmp != 0); break;
2350 case LT_EXPR: val = (cmp < 0); break;
2351 case GT_EXPR: val = (cmp > 0); break;
2352 case LE_EXPR: val = (cmp <= 0); break;
2353 case GE_EXPR: val = (cmp >= 0); break;
2354 default: gcc_unreachable ();
2358 (if (code1 == EQ_EXPR && val) @3)
2359 (if (code1 == EQ_EXPR && !val) { constant_boolean_node (false, type); })
2360 (if (code1 == NE_EXPR && !val) @4))))))
2362 /* Convert (X OP1 CST1) && (X OP2 CST2). */
2364 (for code1 (lt le gt ge)
2365 (for code2 (lt le gt ge)
2367 (bit_and (code1:c@3 @0 INTEGER_CST@1) (code2:c@4 @0 INTEGER_CST@2))
2370 int cmp = tree_int_cst_compare (@1, @2);
2373 /* Choose the more restrictive of two < or <= comparisons. */
2374 (if ((code1 == LT_EXPR || code1 == LE_EXPR)
2375 && (code2 == LT_EXPR || code2 == LE_EXPR))
2376 (if ((cmp < 0) || (cmp == 0 && code1 == LT_EXPR))
2379 /* Likewise chose the more restrictive of two > or >= comparisons. */
2380 (if ((code1 == GT_EXPR || code1 == GE_EXPR)
2381 && (code2 == GT_EXPR || code2 == GE_EXPR))
2382 (if ((cmp > 0) || (cmp == 0 && code1 == GT_EXPR))
2385 /* Check for singleton ranges. */
2387 && ((code1 == LE_EXPR && code2 == GE_EXPR)
2388 || (code1 == GE_EXPR && code2 == LE_EXPR)))
2390 /* Check for disjoint ranges. */
2392 && (code1 == LT_EXPR || code1 == LE_EXPR)
2393 && (code2 == GT_EXPR || code2 == GE_EXPR))
2394 { constant_boolean_node (false, type); })
2396 && (code1 == GT_EXPR || code1 == GE_EXPR)
2397 && (code2 == LT_EXPR || code2 == LE_EXPR))
2398 { constant_boolean_node (false, type); })
2401 /* Convert (X == CST1) || (X OP2 CST2) to a known value
2402 based on CST1 OP2 CST2. Similarly for (X != CST1). */
2405 (for code2 (eq ne lt gt le ge)
2407 (bit_ior:c (code1@3 @0 INTEGER_CST@1) (code2@4 @0 INTEGER_CST@2))
2410 int cmp = tree_int_cst_compare (@1, @2);
2414 case EQ_EXPR: val = (cmp == 0); break;
2415 case NE_EXPR: val = (cmp != 0); break;
2416 case LT_EXPR: val = (cmp < 0); break;
2417 case GT_EXPR: val = (cmp > 0); break;
2418 case LE_EXPR: val = (cmp <= 0); break;
2419 case GE_EXPR: val = (cmp >= 0); break;
2420 default: gcc_unreachable ();
2424 (if (code1 == EQ_EXPR && val) @4)
2425 (if (code1 == NE_EXPR && val) { constant_boolean_node (true, type); })
2426 (if (code1 == NE_EXPR && !val) @3))))))
2428 /* Convert (X OP1 CST1) || (X OP2 CST2). */
2430 (for code1 (lt le gt ge)
2431 (for code2 (lt le gt ge)
2433 (bit_ior (code1@3 @0 INTEGER_CST@1) (code2@4 @0 INTEGER_CST@2))
2436 int cmp = tree_int_cst_compare (@1, @2);
2439 /* Choose the more restrictive of two < or <= comparisons. */
2440 (if ((code1 == LT_EXPR || code1 == LE_EXPR)
2441 && (code2 == LT_EXPR || code2 == LE_EXPR))
2442 (if ((cmp < 0) || (cmp == 0 && code1 == LT_EXPR))
2445 /* Likewise chose the more restrictive of two > or >= comparisons. */
2446 (if ((code1 == GT_EXPR || code1 == GE_EXPR)
2447 && (code2 == GT_EXPR || code2 == GE_EXPR))
2448 (if ((cmp > 0) || (cmp == 0 && code1 == GT_EXPR))
2451 /* Check for singleton ranges. */
2453 && ((code1 == LT_EXPR && code2 == GT_EXPR)
2454 || (code1 == GT_EXPR && code2 == LT_EXPR)))
2456 /* Check for disjoint ranges. */
2458 && (code1 == LT_EXPR || code1 == LE_EXPR)
2459 && (code2 == GT_EXPR || code2 == GE_EXPR))
2460 { constant_boolean_node (true, type); })
2462 && (code1 == GT_EXPR || code1 == GE_EXPR)
2463 && (code2 == LT_EXPR || code2 == LE_EXPR))
2464 { constant_boolean_node (true, type); })
2467 /* We can't reassociate at all for saturating types. */
2468 (if (!TYPE_SATURATING (type))
2470 /* Contract negates. */
2471 /* A + (-B) -> A - B */
2473 (plus:c @0 (convert? (negate @1)))
2474 /* Apply STRIP_NOPS on the negate. */
2475 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
2476 && !TYPE_OVERFLOW_SANITIZED (type))
2480 if (INTEGRAL_TYPE_P (type)
2481 && TYPE_OVERFLOW_WRAPS (type) != TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
2482 t1 = TYPE_OVERFLOW_WRAPS (type) ? type : TREE_TYPE (@1);
2484 (convert (minus (convert:t1 @0) (convert:t1 @1))))))
2485 /* A - (-B) -> A + B */
2487 (minus @0 (convert? (negate @1)))
2488 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
2489 && !TYPE_OVERFLOW_SANITIZED (type))
2493 if (INTEGRAL_TYPE_P (type)
2494 && TYPE_OVERFLOW_WRAPS (type) != TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
2495 t1 = TYPE_OVERFLOW_WRAPS (type) ? type : TREE_TYPE (@1);
2497 (convert (plus (convert:t1 @0) (convert:t1 @1))))))
2499 Sign-extension is ok except for INT_MIN, which thankfully cannot
2500 happen without overflow. */
2502 (negate (convert (negate @1)))
2503 (if (INTEGRAL_TYPE_P (type)
2504 && (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@1))
2505 || (!TYPE_UNSIGNED (TREE_TYPE (@1))
2506 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))))
2507 && !TYPE_OVERFLOW_SANITIZED (type)
2508 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@1)))
2511 (negate (convert negate_expr_p@1))
2512 (if (SCALAR_FLOAT_TYPE_P (type)
2513 && ((DECIMAL_FLOAT_TYPE_P (type)
2514 == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1))
2515 && TYPE_PRECISION (type) >= TYPE_PRECISION (TREE_TYPE (@1)))
2516 || !HONOR_SIGN_DEPENDENT_ROUNDING (type)))
2517 (convert (negate @1))))
2519 (negate (nop_convert? (negate @1)))
2520 (if (!TYPE_OVERFLOW_SANITIZED (type)
2521 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@1)))
2524 /* We can't reassociate floating-point unless -fassociative-math
2525 or fixed-point plus or minus because of saturation to +-Inf. */
2526 (if ((!FLOAT_TYPE_P (type) || flag_associative_math)
2527 && !FIXED_POINT_TYPE_P (type))
2529 /* Match patterns that allow contracting a plus-minus pair
2530 irrespective of overflow issues. */
2531 /* (A +- B) - A -> +- B */
2532 /* (A +- B) -+ B -> A */
2533 /* A - (A +- B) -> -+ B */
2534 /* A +- (B -+ A) -> +- B */
2536 (minus (nop_convert1? (plus:c (nop_convert2? @0) @1)) @0)
2539 (minus (nop_convert1? (minus (nop_convert2? @0) @1)) @0)
2540 (if (!ANY_INTEGRAL_TYPE_P (type)
2541 || TYPE_OVERFLOW_WRAPS (type))
2542 (negate (view_convert @1))
2543 (view_convert (negate @1))))
2545 (plus:c (nop_convert1? (minus @0 (nop_convert2? @1))) @1)
2548 (minus @0 (nop_convert1? (plus:c (nop_convert2? @0) @1)))
2549 (if (!ANY_INTEGRAL_TYPE_P (type)
2550 || TYPE_OVERFLOW_WRAPS (type))
2551 (negate (view_convert @1))
2552 (view_convert (negate @1))))
2554 (minus @0 (nop_convert1? (minus (nop_convert2? @0) @1)))
2556 /* (A +- B) + (C - A) -> C +- B */
2557 /* (A + B) - (A - C) -> B + C */
2558 /* More cases are handled with comparisons. */
2560 (plus:c (plus:c @0 @1) (minus @2 @0))
2563 (plus:c (minus @0 @1) (minus @2 @0))
2566 (plus:c (pointer_diff @0 @1) (pointer_diff @2 @0))
2567 (if (TYPE_OVERFLOW_UNDEFINED (type)
2568 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0)))
2569 (pointer_diff @2 @1)))
2571 (minus (plus:c @0 @1) (minus @0 @2))
2574 /* (A +- CST1) +- CST2 -> A + CST3
2575 Use view_convert because it is safe for vectors and equivalent for
2577 (for outer_op (plus minus)
2578 (for inner_op (plus minus)
2579 neg_inner_op (minus plus)
2581 (outer_op (nop_convert? (inner_op @0 CONSTANT_CLASS_P@1))
2583 /* If one of the types wraps, use that one. */
2584 (if (!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_WRAPS (type))
2585 /* If all 3 captures are CONSTANT_CLASS_P, punt, as we might recurse
2586 forever if something doesn't simplify into a constant. */
2587 (if (!CONSTANT_CLASS_P (@0))
2588 (if (outer_op == PLUS_EXPR)
2589 (plus (view_convert @0) (inner_op @2 (view_convert @1)))
2590 (minus (view_convert @0) (neg_inner_op @2 (view_convert @1)))))
2591 (if (!ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2592 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
2593 (if (outer_op == PLUS_EXPR)
2594 (view_convert (plus @0 (inner_op (view_convert @2) @1)))
2595 (view_convert (minus @0 (neg_inner_op (view_convert @2) @1))))
2596 /* If the constant operation overflows we cannot do the transform
2597 directly as we would introduce undefined overflow, for example
2598 with (a - 1) + INT_MIN. */
2599 (if (types_match (type, @0))
2600 (with { tree cst = const_binop (outer_op == inner_op
2601 ? PLUS_EXPR : MINUS_EXPR,
2603 (if (cst && !TREE_OVERFLOW (cst))
2604 (inner_op @0 { cst; } )
2605 /* X+INT_MAX+1 is X-INT_MIN. */
2606 (if (INTEGRAL_TYPE_P (type) && cst
2607 && wi::to_wide (cst) == wi::min_value (type))
2608 (neg_inner_op @0 { wide_int_to_tree (type, wi::to_wide (cst)); })
2609 /* Last resort, use some unsigned type. */
2610 (with { tree utype = unsigned_type_for (type); }
2612 (view_convert (inner_op
2613 (view_convert:utype @0)
2615 { drop_tree_overflow (cst); }))))))))))))))
2617 /* (CST1 - A) +- CST2 -> CST3 - A */
2618 (for outer_op (plus minus)
2620 (outer_op (nop_convert? (minus CONSTANT_CLASS_P@1 @0)) CONSTANT_CLASS_P@2)
2621 /* If one of the types wraps, use that one. */
2622 (if (!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_WRAPS (type))
2623 /* If all 3 captures are CONSTANT_CLASS_P, punt, as we might recurse
2624 forever if something doesn't simplify into a constant. */
2625 (if (!CONSTANT_CLASS_P (@0))
2626 (minus (outer_op (view_convert @1) @2) (view_convert @0)))
2627 (if (!ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2628 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
2629 (view_convert (minus (outer_op @1 (view_convert @2)) @0))
2630 (if (types_match (type, @0))
2631 (with { tree cst = const_binop (outer_op, type, @1, @2); }
2632 (if (cst && !TREE_OVERFLOW (cst))
2633 (minus { cst; } @0))))))))
2635 /* CST1 - (CST2 - A) -> CST3 + A
2636 Use view_convert because it is safe for vectors and equivalent for
2639 (minus CONSTANT_CLASS_P@1 (nop_convert? (minus CONSTANT_CLASS_P@2 @0)))
2640 /* If one of the types wraps, use that one. */
2641 (if (!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_WRAPS (type))
2642 /* If all 3 captures are CONSTANT_CLASS_P, punt, as we might recurse
2643 forever if something doesn't simplify into a constant. */
2644 (if (!CONSTANT_CLASS_P (@0))
2645 (plus (view_convert @0) (minus @1 (view_convert @2))))
2646 (if (!ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2647 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
2648 (view_convert (plus @0 (minus (view_convert @1) @2)))
2649 (if (types_match (type, @0))
2650 (with { tree cst = const_binop (MINUS_EXPR, type, @1, @2); }
2651 (if (cst && !TREE_OVERFLOW (cst))
2652 (plus { cst; } @0)))))))
2654 /* ((T)(A)) + CST -> (T)(A + CST) */
2657 (plus (convert:s SSA_NAME@0) INTEGER_CST@1)
2658 (if (TREE_CODE (TREE_TYPE (@0)) == INTEGER_TYPE
2659 && TREE_CODE (type) == INTEGER_TYPE
2660 && TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (@0))
2661 && int_fits_type_p (@1, TREE_TYPE (@0)))
2662 /* Perform binary operation inside the cast if the constant fits
2663 and (A + CST)'s range does not overflow. */
2666 wi::overflow_type min_ovf = wi::OVF_OVERFLOW,
2667 max_ovf = wi::OVF_OVERFLOW;
2668 tree inner_type = TREE_TYPE (@0);
2671 = wide_int::from (wi::to_wide (@1), TYPE_PRECISION (inner_type),
2672 TYPE_SIGN (inner_type));
2675 if (get_global_range_query ()->range_of_expr (vr, @0)
2676 && vr.kind () == VR_RANGE)
2678 wide_int wmin0 = vr.lower_bound ();
2679 wide_int wmax0 = vr.upper_bound ();
2680 wi::add (wmin0, w1, TYPE_SIGN (inner_type), &min_ovf);
2681 wi::add (wmax0, w1, TYPE_SIGN (inner_type), &max_ovf);
2684 (if (min_ovf == wi::OVF_NONE && max_ovf == wi::OVF_NONE)
2685 (convert (plus @0 { wide_int_to_tree (TREE_TYPE (@0), w1); } )))
2689 /* ((T)(A + CST1)) + CST2 -> (T)(A) + (T)CST1 + CST2 */
2691 (for op (plus minus)
2693 (plus (convert:s (op:s @0 INTEGER_CST@1)) INTEGER_CST@2)
2694 (if (TREE_CODE (TREE_TYPE (@0)) == INTEGER_TYPE
2695 && TREE_CODE (type) == INTEGER_TYPE
2696 && TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (@0))
2697 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2698 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0))
2699 && TYPE_OVERFLOW_WRAPS (type))
2700 (plus (convert @0) (op @2 (convert @1))))))
2703 /* (T)(A) +- (T)(B) -> (T)(A +- B) only when (A +- B) could be simplified
2704 to a simple value. */
2706 (for op (plus minus)
2708 (op (convert @0) (convert @1))
2709 (if (INTEGRAL_TYPE_P (type)
2710 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
2711 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))
2712 && types_match (TREE_TYPE (@0), TREE_TYPE (@1))
2713 && !TYPE_OVERFLOW_TRAPS (type)
2714 && !TYPE_OVERFLOW_SANITIZED (type))
2715 (convert (op! @0 @1)))))
2720 (plus:c (bit_not @0) @0)
2721 (if (!TYPE_OVERFLOW_TRAPS (type))
2722 { build_all_ones_cst (type); }))
2726 (plus (convert? (bit_not @0)) integer_each_onep)
2727 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2728 (negate (convert @0))))
2732 (minus (convert? (negate @0)) integer_each_onep)
2733 (if (!TYPE_OVERFLOW_TRAPS (type)
2734 && tree_nop_conversion_p (type, TREE_TYPE (@0)))
2735 (bit_not (convert @0))))
2739 (minus integer_all_onesp @0)
2742 /* (T)(P + A) - (T)P -> (T) A */
2744 (minus (convert (plus:c @@0 @1))
2746 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
2747 /* For integer types, if A has a smaller type
2748 than T the result depends on the possible
2750 E.g. T=size_t, A=(unsigned)429497295, P>0.
2751 However, if an overflow in P + A would cause
2752 undefined behavior, we can assume that there
2754 || (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2755 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))))
2758 (minus (convert (pointer_plus @@0 @1))
2760 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
2761 /* For pointer types, if the conversion of A to the
2762 final type requires a sign- or zero-extension,
2763 then we have to punt - it is not defined which
2765 || (POINTER_TYPE_P (TREE_TYPE (@0))
2766 && TREE_CODE (@1) == INTEGER_CST
2767 && tree_int_cst_sign_bit (@1) == 0))
2770 (pointer_diff (pointer_plus @@0 @1) @0)
2771 /* The second argument of pointer_plus must be interpreted as signed, and
2772 thus sign-extended if necessary. */
2773 (with { tree stype = signed_type_for (TREE_TYPE (@1)); }
2774 /* Use view_convert instead of convert here, as POINTER_PLUS_EXPR
2775 second arg is unsigned even when we need to consider it as signed,
2776 we don't want to diagnose overflow here. */
2777 (convert (view_convert:stype @1))))
2779 /* (T)P - (T)(P + A) -> -(T) A */
2781 (minus (convert? @0)
2782 (convert (plus:c @@0 @1)))
2783 (if (INTEGRAL_TYPE_P (type)
2784 && TYPE_OVERFLOW_UNDEFINED (type)
2785 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
2786 (with { tree utype = unsigned_type_for (type); }
2787 (convert (negate (convert:utype @1))))
2788 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
2789 /* For integer types, if A has a smaller type
2790 than T the result depends on the possible
2792 E.g. T=size_t, A=(unsigned)429497295, P>0.
2793 However, if an overflow in P + A would cause
2794 undefined behavior, we can assume that there
2796 || (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2797 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))))
2798 (negate (convert @1)))))
2801 (convert (pointer_plus @@0 @1)))
2802 (if (INTEGRAL_TYPE_P (type)
2803 && TYPE_OVERFLOW_UNDEFINED (type)
2804 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
2805 (with { tree utype = unsigned_type_for (type); }
2806 (convert (negate (convert:utype @1))))
2807 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
2808 /* For pointer types, if the conversion of A to the
2809 final type requires a sign- or zero-extension,
2810 then we have to punt - it is not defined which
2812 || (POINTER_TYPE_P (TREE_TYPE (@0))
2813 && TREE_CODE (@1) == INTEGER_CST
2814 && tree_int_cst_sign_bit (@1) == 0))
2815 (negate (convert @1)))))
2817 (pointer_diff @0 (pointer_plus @@0 @1))
2818 /* The second argument of pointer_plus must be interpreted as signed, and
2819 thus sign-extended if necessary. */
2820 (with { tree stype = signed_type_for (TREE_TYPE (@1)); }
2821 /* Use view_convert instead of convert here, as POINTER_PLUS_EXPR
2822 second arg is unsigned even when we need to consider it as signed,
2823 we don't want to diagnose overflow here. */
2824 (negate (convert (view_convert:stype @1)))))
2826 /* (T)(P + A) - (T)(P + B) -> (T)A - (T)B */
2828 (minus (convert (plus:c @@0 @1))
2829 (convert (plus:c @0 @2)))
2830 (if (INTEGRAL_TYPE_P (type)
2831 && TYPE_OVERFLOW_UNDEFINED (type)
2832 && element_precision (type) <= element_precision (TREE_TYPE (@1))
2833 && element_precision (type) <= element_precision (TREE_TYPE (@2)))
2834 (with { tree utype = unsigned_type_for (type); }
2835 (convert (minus (convert:utype @1) (convert:utype @2))))
2836 (if (((element_precision (type) <= element_precision (TREE_TYPE (@1)))
2837 == (element_precision (type) <= element_precision (TREE_TYPE (@2))))
2838 && (element_precision (type) <= element_precision (TREE_TYPE (@1))
2839 /* For integer types, if A has a smaller type
2840 than T the result depends on the possible
2842 E.g. T=size_t, A=(unsigned)429497295, P>0.
2843 However, if an overflow in P + A would cause
2844 undefined behavior, we can assume that there
2846 || (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2847 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
2848 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))
2849 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@2)))))
2850 (minus (convert @1) (convert @2)))))
2852 (minus (convert (pointer_plus @@0 @1))
2853 (convert (pointer_plus @0 @2)))
2854 (if (INTEGRAL_TYPE_P (type)
2855 && TYPE_OVERFLOW_UNDEFINED (type)
2856 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
2857 (with { tree utype = unsigned_type_for (type); }
2858 (convert (minus (convert:utype @1) (convert:utype @2))))
2859 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
2860 /* For pointer types, if the conversion of A to the
2861 final type requires a sign- or zero-extension,
2862 then we have to punt - it is not defined which
2864 || (POINTER_TYPE_P (TREE_TYPE (@0))
2865 && TREE_CODE (@1) == INTEGER_CST
2866 && tree_int_cst_sign_bit (@1) == 0
2867 && TREE_CODE (@2) == INTEGER_CST
2868 && tree_int_cst_sign_bit (@2) == 0))
2869 (minus (convert @1) (convert @2)))))
2871 (pointer_diff (pointer_plus @0 @2) (pointer_plus @1 @2))
2872 (pointer_diff @0 @1))
2874 (pointer_diff (pointer_plus @@0 @1) (pointer_plus @0 @2))
2875 /* The second argument of pointer_plus must be interpreted as signed, and
2876 thus sign-extended if necessary. */
2877 (with { tree stype = signed_type_for (TREE_TYPE (@1)); }
2878 /* Use view_convert instead of convert here, as POINTER_PLUS_EXPR
2879 second arg is unsigned even when we need to consider it as signed,
2880 we don't want to diagnose overflow here. */
2881 (minus (convert (view_convert:stype @1))
2882 (convert (view_convert:stype @2)))))))
2884 /* (A * C) +- (B * C) -> (A+-B) * C and (A * C) +- A -> A * (C+-1).
2885 Modeled after fold_plusminus_mult_expr. */
2886 (if (!TYPE_SATURATING (type)
2887 && (!FLOAT_TYPE_P (type) || flag_associative_math))
2888 (for plusminus (plus minus)
2890 (plusminus (mult:cs@3 @0 @1) (mult:cs@4 @0 @2))
2891 (if (!ANY_INTEGRAL_TYPE_P (type)
2892 || TYPE_OVERFLOW_WRAPS (type)
2893 || (INTEGRAL_TYPE_P (type)
2894 && tree_expr_nonzero_p (@0)
2895 && expr_not_equal_to (@0, wi::minus_one (TYPE_PRECISION (type)))))
2896 (if (single_use (@3) || single_use (@4))
2897 /* If @1 +- @2 is constant require a hard single-use on either
2898 original operand (but not on both). */
2899 (mult (plusminus @1 @2) @0)
2901 (mult! (plusminus @1 @2) @0)
2904 /* We cannot generate constant 1 for fract. */
2905 (if (!ALL_FRACT_MODE_P (TYPE_MODE (type)))
2907 (plusminus @0 (mult:c@3 @0 @2))
2908 (if ((!ANY_INTEGRAL_TYPE_P (type)
2909 || TYPE_OVERFLOW_WRAPS (type)
2910 /* For @0 + @0*@2 this transformation would introduce UB
2911 (where there was none before) for @0 in [-1,0] and @2 max.
2912 For @0 - @0*@2 this transformation would introduce UB
2913 for @0 0 and @2 in [min,min+1] or @0 -1 and @2 min+1. */
2914 || (INTEGRAL_TYPE_P (type)
2915 && ((tree_expr_nonzero_p (@0)
2916 && expr_not_equal_to (@0,
2917 wi::minus_one (TYPE_PRECISION (type))))
2918 || (plusminus == PLUS_EXPR
2919 ? expr_not_equal_to (@2,
2920 wi::max_value (TYPE_PRECISION (type), SIGNED))
2921 /* Let's ignore the @0 -1 and @2 min case. */
2922 : (expr_not_equal_to (@2,
2923 wi::min_value (TYPE_PRECISION (type), SIGNED))
2924 && expr_not_equal_to (@2,
2925 wi::min_value (TYPE_PRECISION (type), SIGNED)
2928 (mult (plusminus { build_one_cst (type); } @2) @0)))
2930 (plusminus (mult:c@3 @0 @2) @0)
2931 (if ((!ANY_INTEGRAL_TYPE_P (type)
2932 || TYPE_OVERFLOW_WRAPS (type)
2933 /* For @0*@2 + @0 this transformation would introduce UB
2934 (where there was none before) for @0 in [-1,0] and @2 max.
2935 For @0*@2 - @0 this transformation would introduce UB
2936 for @0 0 and @2 min. */
2937 || (INTEGRAL_TYPE_P (type)
2938 && ((tree_expr_nonzero_p (@0)
2939 && (plusminus == MINUS_EXPR
2940 || expr_not_equal_to (@0,
2941 wi::minus_one (TYPE_PRECISION (type)))))
2942 || expr_not_equal_to (@2,
2943 (plusminus == PLUS_EXPR
2944 ? wi::max_value (TYPE_PRECISION (type), SIGNED)
2945 : wi::min_value (TYPE_PRECISION (type), SIGNED))))))
2947 (mult (plusminus @2 { build_one_cst (type); }) @0))))))
2950 /* Canonicalize X + (X << C) into X * (1 + (1 << C)) and
2951 (X << C1) + (X << C2) into X * ((1 << C1) + (1 << C2)). */
2953 (plus:c @0 (lshift:s @0 INTEGER_CST@1))
2954 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2955 && tree_fits_uhwi_p (@1)
2956 && tree_to_uhwi (@1) < element_precision (type)
2957 && (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2958 || optab_handler (smul_optab,
2959 TYPE_MODE (type)) != CODE_FOR_nothing))
2960 (with { tree t = type;
2961 if (!TYPE_OVERFLOW_WRAPS (t)) t = unsigned_type_for (t);
2962 wide_int w = wi::set_bit_in_zero (tree_to_uhwi (@1),
2963 element_precision (type));
2965 tree cst = wide_int_to_tree (VECTOR_TYPE_P (t) ? TREE_TYPE (t)
2967 cst = build_uniform_cst (t, cst); }
2968 (convert (mult (convert:t @0) { cst; })))))
2970 (plus (lshift:s @0 INTEGER_CST@1) (lshift:s @0 INTEGER_CST@2))
2971 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2972 && tree_fits_uhwi_p (@1)
2973 && tree_to_uhwi (@1) < element_precision (type)
2974 && tree_fits_uhwi_p (@2)
2975 && tree_to_uhwi (@2) < element_precision (type)
2976 && (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2977 || optab_handler (smul_optab,
2978 TYPE_MODE (type)) != CODE_FOR_nothing))
2979 (with { tree t = type;
2980 if (!TYPE_OVERFLOW_WRAPS (t)) t = unsigned_type_for (t);
2981 unsigned int prec = element_precision (type);
2982 wide_int w = wi::set_bit_in_zero (tree_to_uhwi (@1), prec);
2983 w += wi::set_bit_in_zero (tree_to_uhwi (@2), prec);
2984 tree cst = wide_int_to_tree (VECTOR_TYPE_P (t) ? TREE_TYPE (t)
2986 cst = build_uniform_cst (t, cst); }
2987 (convert (mult (convert:t @0) { cst; })))))
2990 /* Canonicalize (X*C1)|(X*C2) and (X*C1)^(X*C2) to (C1+C2)*X when
2991 tree_nonzero_bits allows IOR and XOR to be treated like PLUS.
2992 Likewise, handle (X<<C3) and X as legitimate variants of X*C. */
2993 (for op (bit_ior bit_xor)
2995 (op (mult:s@0 @1 INTEGER_CST@2)
2996 (mult:s@3 @1 INTEGER_CST@4))
2997 (if (INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_WRAPS (type)
2998 && (tree_nonzero_bits (@0) & tree_nonzero_bits (@3)) == 0)
3000 { wide_int_to_tree (type, wi::to_wide (@2) + wi::to_wide (@4)); })))
3002 (op:c (mult:s@0 @1 INTEGER_CST@2)
3003 (lshift:s@3 @1 INTEGER_CST@4))
3004 (if (INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_WRAPS (type)
3005 && tree_int_cst_sgn (@4) > 0
3006 && (tree_nonzero_bits (@0) & tree_nonzero_bits (@3)) == 0)
3007 (with { wide_int wone = wi::one (TYPE_PRECISION (type));
3008 wide_int c = wi::add (wi::to_wide (@2),
3009 wi::lshift (wone, wi::to_wide (@4))); }
3010 (mult @1 { wide_int_to_tree (type, c); }))))
3012 (op:c (mult:s@0 @1 INTEGER_CST@2)
3014 (if (INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_WRAPS (type)
3015 && (tree_nonzero_bits (@0) & tree_nonzero_bits (@1)) == 0)
3017 { wide_int_to_tree (type,
3018 wi::add (wi::to_wide (@2), 1)); })))
3020 (op (lshift:s@0 @1 INTEGER_CST@2)
3021 (lshift:s@3 @1 INTEGER_CST@4))
3022 (if (INTEGRAL_TYPE_P (type)
3023 && tree_int_cst_sgn (@2) > 0
3024 && tree_int_cst_sgn (@4) > 0
3025 && (tree_nonzero_bits (@0) & tree_nonzero_bits (@3)) == 0)
3026 (with { tree t = type;
3027 if (!TYPE_OVERFLOW_WRAPS (t))
3028 t = unsigned_type_for (t);
3029 wide_int wone = wi::one (TYPE_PRECISION (t));
3030 wide_int c = wi::add (wi::lshift (wone, wi::to_wide (@2)),
3031 wi::lshift (wone, wi::to_wide (@4))); }
3032 (convert (mult:t (convert:t @1) { wide_int_to_tree (t,c); })))))
3034 (op:c (lshift:s@0 @1 INTEGER_CST@2)
3036 (if (INTEGRAL_TYPE_P (type)
3037 && tree_int_cst_sgn (@2) > 0
3038 && (tree_nonzero_bits (@0) & tree_nonzero_bits (@1)) == 0)
3039 (with { tree t = type;
3040 if (!TYPE_OVERFLOW_WRAPS (t))
3041 t = unsigned_type_for (t);
3042 wide_int wone = wi::one (TYPE_PRECISION (t));
3043 wide_int c = wi::add (wi::lshift (wone, wi::to_wide (@2)), wone); }
3044 (convert (mult:t (convert:t @1) { wide_int_to_tree (t, c); }))))))
3046 /* Simplifications of MIN_EXPR, MAX_EXPR, fmin() and fmax(). */
3048 (for minmax (min max FMIN_ALL FMAX_ALL)
3052 /* min(max(x,y),y) -> y. */
3054 (min:c (max:c @0 @1) @1)
3056 /* max(min(x,y),y) -> y. */
3058 (max:c (min:c @0 @1) @1)
3060 /* max(a,-a) -> abs(a). */
3062 (max:c @0 (negate @0))
3063 (if (TREE_CODE (type) != COMPLEX_TYPE
3064 && (! ANY_INTEGRAL_TYPE_P (type)
3065 || TYPE_OVERFLOW_UNDEFINED (type)))
3067 /* min(a,-a) -> -abs(a). */
3069 (min:c @0 (negate @0))
3070 (if (TREE_CODE (type) != COMPLEX_TYPE
3071 && (! ANY_INTEGRAL_TYPE_P (type)
3072 || TYPE_OVERFLOW_UNDEFINED (type)))
3077 (if (INTEGRAL_TYPE_P (type)
3078 && TYPE_MIN_VALUE (type)
3079 && operand_equal_p (@1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
3081 (if (INTEGRAL_TYPE_P (type)
3082 && TYPE_MAX_VALUE (type)
3083 && operand_equal_p (@1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
3088 (if (INTEGRAL_TYPE_P (type)
3089 && TYPE_MAX_VALUE (type)
3090 && operand_equal_p (@1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
3092 (if (INTEGRAL_TYPE_P (type)
3093 && TYPE_MIN_VALUE (type)
3094 && operand_equal_p (@1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
3097 /* max (a, a + CST) -> a + CST where CST is positive. */
3098 /* max (a, a + CST) -> a where CST is negative. */
3100 (max:c @0 (plus@2 @0 INTEGER_CST@1))
3101 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
3102 (if (tree_int_cst_sgn (@1) > 0)
3106 /* min (a, a + CST) -> a where CST is positive. */
3107 /* min (a, a + CST) -> a + CST where CST is negative. */
3109 (min:c @0 (plus@2 @0 INTEGER_CST@1))
3110 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
3111 (if (tree_int_cst_sgn (@1) > 0)
3115 /* Simplify min (&var[off0], &var[off1]) etc. depending on whether
3116 the addresses are known to be less, equal or greater. */
3117 (for minmax (min max)
3120 (minmax (convert1?@2 addr@0) (convert2?@3 addr@1))
3123 poly_int64 off0, off1;
3125 int equal = address_compare (cmp, TREE_TYPE (@2), @0, @1, base0, base1,
3126 off0, off1, GENERIC);
3129 (if (minmax == MIN_EXPR)
3130 (if (known_le (off0, off1))
3132 (if (known_gt (off0, off1))
3134 (if (known_ge (off0, off1))
3136 (if (known_lt (off0, off1))
3139 /* (convert (minmax ((convert (x) c)))) -> minmax (x c) if x is promoted
3140 and the outer convert demotes the expression back to x's type. */
3141 (for minmax (min max)
3143 (convert (minmax@0 (convert @1) INTEGER_CST@2))
3144 (if (INTEGRAL_TYPE_P (type)
3145 && types_match (@1, type) && int_fits_type_p (@2, type)
3146 && TYPE_SIGN (TREE_TYPE (@0)) == TYPE_SIGN (type)
3147 && TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type))
3148 (minmax @1 (convert @2)))))
3150 (for minmax (FMIN_ALL FMAX_ALL)
3151 /* If either argument is NaN, return the other one. Avoid the
3152 transformation if we get (and honor) a signalling NaN. */
3154 (minmax:c @0 REAL_CST@1)
3155 (if (real_isnan (TREE_REAL_CST_PTR (@1))
3156 && (!HONOR_SNANS (@1) || !TREE_REAL_CST (@1).signalling))
3158 /* Convert fmin/fmax to MIN_EXPR/MAX_EXPR. C99 requires these
3159 functions to return the numeric arg if the other one is NaN.
3160 MIN and MAX don't honor that, so only transform if -ffinite-math-only
3161 is set. C99 doesn't require -0.0 to be handled, so we don't have to
3162 worry about it either. */
3163 (if (flag_finite_math_only)
3170 /* min (-A, -B) -> -max (A, B) */
3171 (for minmax (min max FMIN_ALL FMAX_ALL)
3172 maxmin (max min FMAX_ALL FMIN_ALL)
3174 (minmax (negate:s@2 @0) (negate:s@3 @1))
3175 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
3176 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3177 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
3178 (negate (maxmin @0 @1)))))
3179 /* MIN (~X, ~Y) -> ~MAX (X, Y)
3180 MAX (~X, ~Y) -> ~MIN (X, Y) */
3181 (for minmax (min max)
3184 (minmax (bit_not:s@2 @0) (bit_not:s@3 @1))
3185 (bit_not (maxmin @0 @1))))
3187 /* MIN (X, Y) == X -> X <= Y */
3188 (for minmax (min min max max)
3192 (cmp:c (minmax:c @0 @1) @0)
3193 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0)))
3195 /* MIN (X, 5) == 0 -> X == 0
3196 MIN (X, 5) == 7 -> false */
3199 (cmp (min @0 INTEGER_CST@1) INTEGER_CST@2)
3200 (if (wi::lt_p (wi::to_wide (@1), wi::to_wide (@2),
3201 TYPE_SIGN (TREE_TYPE (@0))))
3202 { constant_boolean_node (cmp == NE_EXPR, type); }
3203 (if (wi::gt_p (wi::to_wide (@1), wi::to_wide (@2),
3204 TYPE_SIGN (TREE_TYPE (@0))))
3208 (cmp (max @0 INTEGER_CST@1) INTEGER_CST@2)
3209 (if (wi::gt_p (wi::to_wide (@1), wi::to_wide (@2),
3210 TYPE_SIGN (TREE_TYPE (@0))))
3211 { constant_boolean_node (cmp == NE_EXPR, type); }
3212 (if (wi::lt_p (wi::to_wide (@1), wi::to_wide (@2),
3213 TYPE_SIGN (TREE_TYPE (@0))))
3215 /* MIN (X, C1) < C2 -> X < C2 || C1 < C2 */
3216 (for minmax (min min max max min min max max )
3217 cmp (lt le gt ge gt ge lt le )
3218 comb (bit_ior bit_ior bit_ior bit_ior bit_and bit_and bit_and bit_and)
3220 (cmp (minmax @0 INTEGER_CST@1) INTEGER_CST@2)
3221 (comb (cmp @0 @2) (cmp @1 @2))))
3223 /* X <= MAX(X, Y) -> true
3224 X > MAX(X, Y) -> false
3225 X >= MIN(X, Y) -> true
3226 X < MIN(X, Y) -> false */
3227 (for minmax (min min max max )
3230 (cmp @0 (minmax:c @0 @1))
3231 { constant_boolean_node (cmp == GE_EXPR || cmp == LE_EXPR, type); } ))
3233 /* Undo fancy way of writing max/min or other ?: expressions,
3234 like a - ((a - b) & -(a < b)), in this case into (a < b) ? b : a.
3235 People normally use ?: and that is what we actually try to optimize. */
3236 (for cmp (simple_comparison)
3238 (minus @0 (bit_and:c (minus @0 @1)
3239 (convert? (negate@4 (convert? (cmp@5 @2 @3))))))
3240 (if (INTEGRAL_TYPE_P (type)
3241 && INTEGRAL_TYPE_P (TREE_TYPE (@4))
3242 && TREE_CODE (TREE_TYPE (@4)) != BOOLEAN_TYPE
3243 && INTEGRAL_TYPE_P (TREE_TYPE (@5))
3244 && (TYPE_PRECISION (TREE_TYPE (@4)) >= TYPE_PRECISION (type)
3245 || !TYPE_UNSIGNED (TREE_TYPE (@4)))
3246 && (GIMPLE || !TREE_SIDE_EFFECTS (@1)))
3247 (cond (cmp @2 @3) @1 @0)))
3249 (plus:c @0 (bit_and:c (minus @1 @0)
3250 (convert? (negate@4 (convert? (cmp@5 @2 @3))))))
3251 (if (INTEGRAL_TYPE_P (type)
3252 && INTEGRAL_TYPE_P (TREE_TYPE (@4))
3253 && TREE_CODE (TREE_TYPE (@4)) != BOOLEAN_TYPE
3254 && INTEGRAL_TYPE_P (TREE_TYPE (@5))
3255 && (TYPE_PRECISION (TREE_TYPE (@4)) >= TYPE_PRECISION (type)
3256 || !TYPE_UNSIGNED (TREE_TYPE (@4)))
3257 && (GIMPLE || !TREE_SIDE_EFFECTS (@1)))
3258 (cond (cmp @2 @3) @1 @0)))
3259 /* Similarly with ^ instead of - though in that case with :c. */
3261 (bit_xor:c @0 (bit_and:c (bit_xor:c @0 @1)
3262 (convert? (negate@4 (convert? (cmp@5 @2 @3))))))
3263 (if (INTEGRAL_TYPE_P (type)
3264 && INTEGRAL_TYPE_P (TREE_TYPE (@4))
3265 && TREE_CODE (TREE_TYPE (@4)) != BOOLEAN_TYPE
3266 && INTEGRAL_TYPE_P (TREE_TYPE (@5))
3267 && (TYPE_PRECISION (TREE_TYPE (@4)) >= TYPE_PRECISION (type)
3268 || !TYPE_UNSIGNED (TREE_TYPE (@4)))
3269 && (GIMPLE || !TREE_SIDE_EFFECTS (@1)))
3270 (cond (cmp @2 @3) @1 @0))))
3272 /* Simplifications of shift and rotates. */
3274 (for rotate (lrotate rrotate)
3276 (rotate integer_all_onesp@0 @1)
3279 /* Optimize -1 >> x for arithmetic right shifts. */
3281 (rshift integer_all_onesp@0 @1)
3282 (if (!TYPE_UNSIGNED (type))
3285 /* Optimize (x >> c) << c into x & (-1<<c). */
3287 (lshift (nop_convert? (rshift @0 INTEGER_CST@1)) @1)
3288 (if (wi::ltu_p (wi::to_wide (@1), element_precision (type)))
3289 /* It doesn't matter if the right shift is arithmetic or logical. */
3290 (bit_and (view_convert @0) (lshift { build_minus_one_cst (type); } @1))))
3293 (lshift (convert (convert@2 (rshift @0 INTEGER_CST@1))) @1)
3294 (if (wi::ltu_p (wi::to_wide (@1), element_precision (type))
3295 /* Allow intermediate conversion to integral type with whatever sign, as
3296 long as the low TYPE_PRECISION (type)
3297 - TYPE_PRECISION (TREE_TYPE (@2)) bits are preserved. */
3298 && INTEGRAL_TYPE_P (type)
3299 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
3300 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
3301 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (@0))
3302 && (TYPE_PRECISION (TREE_TYPE (@2)) >= TYPE_PRECISION (type)
3303 || wi::geu_p (wi::to_wide (@1),
3304 TYPE_PRECISION (type)
3305 - TYPE_PRECISION (TREE_TYPE (@2)))))
3306 (bit_and (convert @0) (lshift { build_minus_one_cst (type); } @1))))
3308 /* Optimize (x << c) >> c into x & ((unsigned)-1 >> c) for unsigned
3311 (rshift (lshift @0 INTEGER_CST@1) @1)
3312 (if (TYPE_UNSIGNED (type)
3313 && (wi::ltu_p (wi::to_wide (@1), element_precision (type))))
3314 (bit_and @0 (rshift { build_minus_one_cst (type); } @1))))
3316 /* Optimize x >> x into 0 */
3319 { build_zero_cst (type); })
3321 (for shiftrotate (lrotate rrotate lshift rshift)
3323 (shiftrotate @0 integer_zerop)
3326 (shiftrotate integer_zerop@0 @1)
3328 /* Prefer vector1 << scalar to vector1 << vector2
3329 if vector2 is uniform. */
3330 (for vec (VECTOR_CST CONSTRUCTOR)
3332 (shiftrotate @0 vec@1)
3333 (with { tree tem = uniform_vector_p (@1); }
3335 (shiftrotate @0 { tem; }))))))
3337 /* Simplify X << Y where Y's low width bits are 0 to X, as only valid
3338 Y is 0. Similarly for X >> Y. */
3340 (for shift (lshift rshift)
3342 (shift @0 SSA_NAME@1)
3343 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1)))
3345 int width = ceil_log2 (element_precision (TREE_TYPE (@0)));
3346 int prec = TYPE_PRECISION (TREE_TYPE (@1));
3348 (if ((get_nonzero_bits (@1) & wi::mask (width, false, prec)) == 0)
3352 /* Rewrite an LROTATE_EXPR by a constant into an
3353 RROTATE_EXPR by a new constant. */
3355 (lrotate @0 INTEGER_CST@1)
3356 (rrotate @0 { const_binop (MINUS_EXPR, TREE_TYPE (@1),
3357 build_int_cst (TREE_TYPE (@1),
3358 element_precision (type)), @1); }))
3360 /* Turn (a OP c1) OP c2 into a OP (c1+c2). */
3361 (for op (lrotate rrotate rshift lshift)
3363 (op (op @0 INTEGER_CST@1) INTEGER_CST@2)
3364 (with { unsigned int prec = element_precision (type); }
3365 (if (wi::ge_p (wi::to_wide (@1), 0, TYPE_SIGN (TREE_TYPE (@1)))
3366 && wi::lt_p (wi::to_wide (@1), prec, TYPE_SIGN (TREE_TYPE (@1)))
3367 && wi::ge_p (wi::to_wide (@2), 0, TYPE_SIGN (TREE_TYPE (@2)))
3368 && wi::lt_p (wi::to_wide (@2), prec, TYPE_SIGN (TREE_TYPE (@2))))
3369 (with { unsigned int low = (tree_to_uhwi (@1)
3370 + tree_to_uhwi (@2)); }
3371 /* Deal with a OP (c1 + c2) being undefined but (a OP c1) OP c2
3372 being well defined. */
3374 (if (op == LROTATE_EXPR || op == RROTATE_EXPR)
3375 (op @0 { build_int_cst (TREE_TYPE (@1), low % prec); })
3376 (if (TYPE_UNSIGNED (type) || op == LSHIFT_EXPR)
3377 { build_zero_cst (type); }
3378 (op @0 { build_int_cst (TREE_TYPE (@1), prec - 1); })))
3379 (op @0 { build_int_cst (TREE_TYPE (@1), low); })))))))
3382 /* Simplify (CST << x) & 1 to 0 if CST is even or to x == 0 if it is odd. */
3384 (bit_and (lshift INTEGER_CST@1 @0) integer_onep)
3385 (if ((wi::to_wide (@1) & 1) != 0)
3386 (convert (eq:boolean_type_node @0 { build_zero_cst (TREE_TYPE (@0)); }))
3387 { build_zero_cst (type); }))
3389 /* Simplify ((C << x) & D) != 0 where C and D are power of two constants,
3390 either to false if D is smaller (unsigned comparison) than C, or to
3391 x == log2 (D) - log2 (C). Similarly for right shifts. */
3395 (cmp (bit_and (lshift integer_pow2p@1 @0) integer_pow2p@2) integer_zerop)
3396 (with { int c1 = wi::clz (wi::to_wide (@1));
3397 int c2 = wi::clz (wi::to_wide (@2)); }
3399 { constant_boolean_node (cmp == NE_EXPR ? false : true, type); }
3400 (icmp @0 { build_int_cst (TREE_TYPE (@0), c1 - c2); }))))
3402 (cmp (bit_and (rshift integer_pow2p@1 @0) integer_pow2p@2) integer_zerop)
3403 (if (tree_int_cst_sgn (@1) > 0)
3404 (with { int c1 = wi::clz (wi::to_wide (@1));
3405 int c2 = wi::clz (wi::to_wide (@2)); }
3407 { constant_boolean_node (cmp == NE_EXPR ? false : true, type); }
3408 (icmp @0 { build_int_cst (TREE_TYPE (@0), c2 - c1); }))))))
3410 /* (CST1 << A) == CST2 -> A == ctz (CST2) - ctz (CST1)
3411 (CST1 << A) != CST2 -> A != ctz (CST2) - ctz (CST1)
3415 (cmp (lshift INTEGER_CST@0 @1) INTEGER_CST@2)
3416 (with { int cand = wi::ctz (wi::to_wide (@2)) - wi::ctz (wi::to_wide (@0)); }
3418 || (!integer_zerop (@2)
3419 && wi::lshift (wi::to_wide (@0), cand) != wi::to_wide (@2)))
3420 { constant_boolean_node (cmp == NE_EXPR, type); }
3421 (if (!integer_zerop (@2)
3422 && wi::lshift (wi::to_wide (@0), cand) == wi::to_wide (@2))
3423 (cmp @1 { build_int_cst (TREE_TYPE (@1), cand); }))))))
3425 /* Fold (X << C1) & C2 into (X << C1) & (C2 | ((1 << C1) - 1))
3426 (X >> C1) & C2 into (X >> C1) & (C2 | ~((type) -1 >> C1))
3427 if the new mask might be further optimized. */
3428 (for shift (lshift rshift)
3430 (bit_and (convert?:s@4 (shift:s@5 (convert1?@3 @0) INTEGER_CST@1))
3432 (if (tree_nop_conversion_p (TREE_TYPE (@4), TREE_TYPE (@5))
3433 && TYPE_PRECISION (type) <= HOST_BITS_PER_WIDE_INT
3434 && tree_fits_uhwi_p (@1)
3435 && tree_to_uhwi (@1) > 0
3436 && tree_to_uhwi (@1) < TYPE_PRECISION (type))
3439 unsigned int shiftc = tree_to_uhwi (@1);
3440 unsigned HOST_WIDE_INT mask = TREE_INT_CST_LOW (@2);
3441 unsigned HOST_WIDE_INT newmask, zerobits = 0;
3442 tree shift_type = TREE_TYPE (@3);
3445 if (shift == LSHIFT_EXPR)
3446 zerobits = ((HOST_WIDE_INT_1U << shiftc) - 1);
3447 else if (shift == RSHIFT_EXPR
3448 && type_has_mode_precision_p (shift_type))
3450 prec = TYPE_PRECISION (TREE_TYPE (@3));
3452 /* See if more bits can be proven as zero because of
3455 && TYPE_UNSIGNED (TREE_TYPE (@0)))
3457 tree inner_type = TREE_TYPE (@0);
3458 if (type_has_mode_precision_p (inner_type)
3459 && TYPE_PRECISION (inner_type) < prec)
3461 prec = TYPE_PRECISION (inner_type);
3462 /* See if we can shorten the right shift. */
3464 shift_type = inner_type;
3465 /* Otherwise X >> C1 is all zeros, so we'll optimize
3466 it into (X, 0) later on by making sure zerobits
3470 zerobits = HOST_WIDE_INT_M1U;
3473 zerobits >>= HOST_BITS_PER_WIDE_INT - shiftc;
3474 zerobits <<= prec - shiftc;
3476 /* For arithmetic shift if sign bit could be set, zerobits
3477 can contain actually sign bits, so no transformation is
3478 possible, unless MASK masks them all away. In that
3479 case the shift needs to be converted into logical shift. */
3480 if (!TYPE_UNSIGNED (TREE_TYPE (@3))
3481 && prec == TYPE_PRECISION (TREE_TYPE (@3)))
3483 if ((mask & zerobits) == 0)
3484 shift_type = unsigned_type_for (TREE_TYPE (@3));
3490 /* ((X << 16) & 0xff00) is (X, 0). */
3491 (if ((mask & zerobits) == mask)
3492 { build_int_cst (type, 0); }
3493 (with { newmask = mask | zerobits; }
3494 (if (newmask != mask && (newmask & (newmask + 1)) == 0)
3497 /* Only do the transformation if NEWMASK is some integer
3499 for (prec = BITS_PER_UNIT;
3500 prec < HOST_BITS_PER_WIDE_INT; prec <<= 1)
3501 if (newmask == (HOST_WIDE_INT_1U << prec) - 1)
3504 (if (prec < HOST_BITS_PER_WIDE_INT
3505 || newmask == HOST_WIDE_INT_M1U)
3507 { tree newmaskt = build_int_cst_type (TREE_TYPE (@2), newmask); }
3508 (if (!tree_int_cst_equal (newmaskt, @2))
3509 (if (shift_type != TREE_TYPE (@3))
3510 (bit_and (convert (shift:shift_type (convert @3) @1)) { newmaskt; })
3511 (bit_and @4 { newmaskt; })))))))))))))
3513 /* ((1 << n) & M) != 0 -> n == log2 (M) */
3519 (nop_convert? (lshift integer_onep @0)) integer_pow2p@1) integer_zerop)
3520 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)))
3521 (icmp @0 { wide_int_to_tree (TREE_TYPE (@0),
3522 wi::exact_log2 (wi::to_wide (@1))); }))))
3524 /* Fold (X {&,^,|} C2) << C1 into (X << C1) {&,^,|} (C2 << C1)
3525 (X {&,^,|} C2) >> C1 into (X >> C1) & (C2 >> C1). */
3526 (for shift (lshift rshift)
3527 (for bit_op (bit_and bit_xor bit_ior)
3529 (shift (convert?:s (bit_op:s @0 INTEGER_CST@2)) INTEGER_CST@1)
3530 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
3531 (with { tree mask = int_const_binop (shift, fold_convert (type, @2), @1); }
3533 (bit_op (shift (convert @0) @1) { mask; })))))))
3535 /* ~(~X >> Y) -> X >> Y (for arithmetic shift). */
3537 (bit_not (convert1?:s (rshift:s (convert2?@0 (bit_not @1)) @2)))
3538 (if (!TYPE_UNSIGNED (TREE_TYPE (@0))
3539 && (element_precision (TREE_TYPE (@0))
3540 <= element_precision (TREE_TYPE (@1))
3541 || !TYPE_UNSIGNED (TREE_TYPE (@1))))
3543 { tree shift_type = TREE_TYPE (@0); }
3544 (convert (rshift (convert:shift_type @1) @2)))))
3546 /* ~(~X >>r Y) -> X >>r Y
3547 ~(~X <<r Y) -> X <<r Y */
3548 (for rotate (lrotate rrotate)
3550 (bit_not (convert1?:s (rotate:s (convert2?@0 (bit_not @1)) @2)))
3551 (if ((element_precision (TREE_TYPE (@0))
3552 <= element_precision (TREE_TYPE (@1))
3553 || !TYPE_UNSIGNED (TREE_TYPE (@1)))
3554 && (element_precision (type) <= element_precision (TREE_TYPE (@0))
3555 || !TYPE_UNSIGNED (TREE_TYPE (@0))))
3557 { tree rotate_type = TREE_TYPE (@0); }
3558 (convert (rotate (convert:rotate_type @1) @2))))))
3561 (for rotate (lrotate rrotate)
3562 invrot (rrotate lrotate)
3563 /* (X >>r Y) cmp (Z >>r Y) may simplify to X cmp Y. */
3565 (cmp (rotate @1 @0) (rotate @2 @0))
3567 /* (X >>r C1) cmp C2 may simplify to X cmp C3. */
3569 (cmp (rotate @0 INTEGER_CST@1) INTEGER_CST@2)
3570 (cmp @0 { const_binop (invrot, TREE_TYPE (@0), @2, @1); }))
3571 /* (X >>r Y) cmp C where C is 0 or ~0, may simplify to X cmp C. */
3573 (cmp (rotate @0 @1) INTEGER_CST@2)
3574 (if (integer_zerop (@2) || integer_all_onesp (@2))
3577 /* Both signed and unsigned lshift produce the same result, so use
3578 the form that minimizes the number of conversions. Postpone this
3579 transformation until after shifts by zero have been folded. */
3581 (convert (lshift:s@0 (convert:s@1 @2) INTEGER_CST@3))
3582 (if (INTEGRAL_TYPE_P (type)
3583 && tree_nop_conversion_p (type, TREE_TYPE (@0))
3584 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
3585 && TYPE_PRECISION (TREE_TYPE (@2)) <= TYPE_PRECISION (type)
3586 && !integer_zerop (@3))
3587 (lshift (convert @2) @3)))
3589 /* Simplifications of conversions. */
3591 /* Basic strip-useless-type-conversions / strip_nops. */
3592 (for cvt (convert view_convert float fix_trunc)
3595 (if ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@0)))
3596 || (GENERIC && type == TREE_TYPE (@0)))
3599 /* Contract view-conversions. */
3601 (view_convert (view_convert @0))
3604 /* For integral conversions with the same precision or pointer
3605 conversions use a NOP_EXPR instead. */
3608 (if ((INTEGRAL_TYPE_P (type) || POINTER_TYPE_P (type))
3609 && (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
3610 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (@0)))
3613 /* Strip inner integral conversions that do not change precision or size, or
3614 zero-extend while keeping the same size (for bool-to-char). */
3616 (view_convert (convert@0 @1))
3617 (if ((INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
3618 && (INTEGRAL_TYPE_P (TREE_TYPE (@1)) || POINTER_TYPE_P (TREE_TYPE (@1)))
3619 && TYPE_SIZE (TREE_TYPE (@0)) == TYPE_SIZE (TREE_TYPE (@1))
3620 && (TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1))
3621 || (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (TREE_TYPE (@1))
3622 && TYPE_UNSIGNED (TREE_TYPE (@1)))))
3625 /* Simplify a view-converted empty constructor. */
3627 (view_convert CONSTRUCTOR@0)
3628 (if (TREE_CODE (@0) != SSA_NAME
3629 && CONSTRUCTOR_NELTS (@0) == 0)
3630 { build_zero_cst (type); }))
3632 /* Re-association barriers around constants and other re-association
3633 barriers can be removed. */
3635 (paren CONSTANT_CLASS_P@0)
3638 (paren (paren@1 @0))
3641 /* Handle cases of two conversions in a row. */
3642 (for ocvt (convert float fix_trunc)
3643 (for icvt (convert float)
3648 tree inside_type = TREE_TYPE (@0);
3649 tree inter_type = TREE_TYPE (@1);
3650 int inside_int = INTEGRAL_TYPE_P (inside_type);
3651 int inside_ptr = POINTER_TYPE_P (inside_type);
3652 int inside_float = FLOAT_TYPE_P (inside_type);
3653 int inside_vec = VECTOR_TYPE_P (inside_type);
3654 unsigned int inside_prec = TYPE_PRECISION (inside_type);
3655 int inside_unsignedp = TYPE_UNSIGNED (inside_type);
3656 int inter_int = INTEGRAL_TYPE_P (inter_type);
3657 int inter_ptr = POINTER_TYPE_P (inter_type);
3658 int inter_float = FLOAT_TYPE_P (inter_type);
3659 int inter_vec = VECTOR_TYPE_P (inter_type);
3660 unsigned int inter_prec = TYPE_PRECISION (inter_type);
3661 int inter_unsignedp = TYPE_UNSIGNED (inter_type);
3662 int final_int = INTEGRAL_TYPE_P (type);
3663 int final_ptr = POINTER_TYPE_P (type);
3664 int final_float = FLOAT_TYPE_P (type);
3665 int final_vec = VECTOR_TYPE_P (type);
3666 unsigned int final_prec = TYPE_PRECISION (type);
3667 int final_unsignedp = TYPE_UNSIGNED (type);
3670 /* In addition to the cases of two conversions in a row
3671 handled below, if we are converting something to its own
3672 type via an object of identical or wider precision, neither
3673 conversion is needed. */
3674 (if (((GIMPLE && useless_type_conversion_p (type, inside_type))
3676 && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (inside_type)))
3677 && (((inter_int || inter_ptr) && final_int)
3678 || (inter_float && final_float))
3679 && inter_prec >= final_prec)
3682 /* Likewise, if the intermediate and initial types are either both
3683 float or both integer, we don't need the middle conversion if the
3684 former is wider than the latter and doesn't change the signedness
3685 (for integers). Avoid this if the final type is a pointer since
3686 then we sometimes need the middle conversion. */
3687 (if (((inter_int && inside_int) || (inter_float && inside_float))
3688 && (final_int || final_float)
3689 && inter_prec >= inside_prec
3690 && (inter_float || inter_unsignedp == inside_unsignedp))
3693 /* If we have a sign-extension of a zero-extended value, we can
3694 replace that by a single zero-extension. Likewise if the
3695 final conversion does not change precision we can drop the
3696 intermediate conversion. */
3697 (if (inside_int && inter_int && final_int
3698 && ((inside_prec < inter_prec && inter_prec < final_prec
3699 && inside_unsignedp && !inter_unsignedp)
3700 || final_prec == inter_prec))
3703 /* Two conversions in a row are not needed unless:
3704 - some conversion is floating-point (overstrict for now), or
3705 - some conversion is a vector (overstrict for now), or
3706 - the intermediate type is narrower than both initial and
3708 - the intermediate type and innermost type differ in signedness,
3709 and the outermost type is wider than the intermediate, or
3710 - the initial type is a pointer type and the precisions of the
3711 intermediate and final types differ, or
3712 - the final type is a pointer type and the precisions of the
3713 initial and intermediate types differ. */
3714 (if (! inside_float && ! inter_float && ! final_float
3715 && ! inside_vec && ! inter_vec && ! final_vec
3716 && (inter_prec >= inside_prec || inter_prec >= final_prec)
3717 && ! (inside_int && inter_int
3718 && inter_unsignedp != inside_unsignedp
3719 && inter_prec < final_prec)
3720 && ((inter_unsignedp && inter_prec > inside_prec)
3721 == (final_unsignedp && final_prec > inter_prec))
3722 && ! (inside_ptr && inter_prec != final_prec)
3723 && ! (final_ptr && inside_prec != inter_prec))
3726 /* A truncation to an unsigned type (a zero-extension) should be
3727 canonicalized as bitwise and of a mask. */
3728 (if (GIMPLE /* PR70366: doing this in GENERIC breaks -Wconversion. */
3729 && final_int && inter_int && inside_int
3730 && final_prec == inside_prec
3731 && final_prec > inter_prec
3733 (convert (bit_and @0 { wide_int_to_tree
3735 wi::mask (inter_prec, false,
3736 TYPE_PRECISION (inside_type))); })))
3738 /* If we are converting an integer to a floating-point that can
3739 represent it exactly and back to an integer, we can skip the
3740 floating-point conversion. */
3741 (if (GIMPLE /* PR66211 */
3742 && inside_int && inter_float && final_int &&
3743 (unsigned) significand_size (TYPE_MODE (inter_type))
3744 >= inside_prec - !inside_unsignedp)
3747 /* (float_type)(integer_type) x -> trunc (x) if the type of x matches
3748 float_type. Only do the transformation if we do not need to preserve
3749 trapping behaviour, so require !flag_trapping_math. */
3752 (float (fix_trunc @0))
3753 (if (!flag_trapping_math
3754 && types_match (type, TREE_TYPE (@0))
3755 && direct_internal_fn_supported_p (IFN_TRUNC, type,
3760 /* If we have a narrowing conversion to an integral type that is fed by a
3761 BIT_AND_EXPR, we might be able to remove the BIT_AND_EXPR if it merely
3762 masks off bits outside the final type (and nothing else). */
3764 (convert (bit_and @0 INTEGER_CST@1))
3765 (if (INTEGRAL_TYPE_P (type)
3766 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
3767 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))
3768 && operand_equal_p (@1, build_low_bits_mask (TREE_TYPE (@1),
3769 TYPE_PRECISION (type)), 0))
3773 /* (X /[ex] A) * A -> X. */
3775 (mult (convert1? (exact_div @0 @@1)) (convert2? @1))
3778 /* Simplify (A / B) * B + (A % B) -> A. */
3779 (for div (trunc_div ceil_div floor_div round_div)
3780 mod (trunc_mod ceil_mod floor_mod round_mod)
3782 (plus:c (mult:c (div @0 @1) @1) (mod @0 @1))
3785 /* ((X /[ex] A) +- B) * A --> X +- A * B. */
3786 (for op (plus minus)
3788 (mult (convert1? (op (convert2? (exact_div @0 INTEGER_CST@@1)) INTEGER_CST@2)) @1)
3789 (if (tree_nop_conversion_p (type, TREE_TYPE (@2))
3790 && tree_nop_conversion_p (TREE_TYPE (@0), TREE_TYPE (@2)))
3793 wi::overflow_type overflow;
3794 wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@2),
3795 TYPE_SIGN (type), &overflow);
3797 (if (types_match (type, TREE_TYPE (@2))
3798 && types_match (TREE_TYPE (@0), TREE_TYPE (@2)) && !overflow)
3799 (op @0 { wide_int_to_tree (type, mul); })
3800 (with { tree utype = unsigned_type_for (type); }
3801 (convert (op (convert:utype @0)
3802 (mult (convert:utype @1) (convert:utype @2))))))))))
3804 /* Canonicalization of binary operations. */
3806 /* Convert X + -C into X - C. */
3808 (plus @0 REAL_CST@1)
3809 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
3810 (with { tree tem = const_unop (NEGATE_EXPR, type, @1); }
3811 (if (!TREE_OVERFLOW (tem) || !flag_trapping_math)
3812 (minus @0 { tem; })))))
3814 /* Convert x+x into x*2. */
3817 (if (SCALAR_FLOAT_TYPE_P (type))
3818 (mult @0 { build_real (type, dconst2); })
3819 (if (INTEGRAL_TYPE_P (type))
3820 (mult @0 { build_int_cst (type, 2); }))))
3824 (minus integer_zerop @1)
3827 (pointer_diff integer_zerop @1)
3828 (negate (convert @1)))
3830 /* (ARG0 - ARG1) is the same as (-ARG1 + ARG0). So check whether
3831 ARG0 is zero and X + ARG0 reduces to X, since that would mean
3832 (-ARG1 + ARG0) reduces to -ARG1. */
3834 (minus real_zerop@0 @1)
3835 (if (fold_real_zero_addition_p (type, @1, @0, 0))
3838 /* Transform x * -1 into -x. */
3840 (mult @0 integer_minus_onep)
3843 /* Reassociate (X * CST) * Y to (X * Y) * CST. This does not introduce
3844 signed overflow for CST != 0 && CST != -1. */
3846 (mult:c (mult:s@3 @0 INTEGER_CST@1) @2)
3847 (if (TREE_CODE (@2) != INTEGER_CST
3849 && !integer_zerop (@1) && !integer_minus_onep (@1))
3850 (mult (mult @0 @2) @1)))
3852 /* True if we can easily extract the real and imaginary parts of a complex
3854 (match compositional_complex
3855 (convert? (complex @0 @1)))
3857 /* COMPLEX_EXPR and REALPART/IMAGPART_EXPR cancellations. */
3859 (complex (realpart @0) (imagpart @0))
3862 (realpart (complex @0 @1))
3865 (imagpart (complex @0 @1))
3868 /* Sometimes we only care about half of a complex expression. */
3870 (realpart (convert?:s (conj:s @0)))
3871 (convert (realpart @0)))
3873 (imagpart (convert?:s (conj:s @0)))
3874 (convert (negate (imagpart @0))))
3875 (for part (realpart imagpart)
3876 (for op (plus minus)
3878 (part (convert?:s@2 (op:s @0 @1)))
3879 (convert (op (part @0) (part @1))))))
3881 (realpart (convert?:s (CEXPI:s @0)))
3884 (imagpart (convert?:s (CEXPI:s @0)))
3887 /* conj(conj(x)) -> x */
3889 (conj (convert? (conj @0)))
3890 (if (tree_nop_conversion_p (TREE_TYPE (@0), type))
3893 /* conj({x,y}) -> {x,-y} */
3895 (conj (convert?:s (complex:s @0 @1)))
3896 (with { tree itype = TREE_TYPE (type); }
3897 (complex (convert:itype @0) (negate (convert:itype @1)))))
3899 /* BSWAP simplifications, transforms checked by gcc.dg/builtin-bswap-8.c. */
3900 (for bswap (BUILT_IN_BSWAP16 BUILT_IN_BSWAP32
3901 BUILT_IN_BSWAP64 BUILT_IN_BSWAP128)
3906 (bswap (bit_not (bswap @0)))
3908 (for bitop (bit_xor bit_ior bit_and)
3910 (bswap (bitop:c (bswap @0) @1))
3911 (bitop @0 (bswap @1))))
3914 (cmp (bswap@2 @0) (bswap @1))
3915 (with { tree ctype = TREE_TYPE (@2); }
3916 (cmp (convert:ctype @0) (convert:ctype @1))))
3918 (cmp (bswap @0) INTEGER_CST@1)
3919 (with { tree ctype = TREE_TYPE (@1); }
3920 (cmp (convert:ctype @0) (bswap @1)))))
3921 /* (bswap(x) >> C1) & C2 can sometimes be simplified to (x >> C3) & C2. */
3923 (bit_and (convert1? (rshift@0 (convert2? (bswap@4 @1)) INTEGER_CST@2))
3925 (if (BITS_PER_UNIT == 8
3926 && tree_fits_uhwi_p (@2)
3927 && tree_fits_uhwi_p (@3))
3930 unsigned HOST_WIDE_INT prec = TYPE_PRECISION (TREE_TYPE (@4));
3931 unsigned HOST_WIDE_INT bits = tree_to_uhwi (@2);
3932 unsigned HOST_WIDE_INT mask = tree_to_uhwi (@3);
3933 unsigned HOST_WIDE_INT lo = bits & 7;
3934 unsigned HOST_WIDE_INT hi = bits - lo;
3937 && mask < (256u>>lo)
3938 && bits < TYPE_PRECISION (TREE_TYPE(@0)))
3939 (with { unsigned HOST_WIDE_INT ns = (prec - (hi + 8)) + lo; }
3941 (bit_and (convert @1) @3)
3944 tree utype = unsigned_type_for (TREE_TYPE (@1));
3945 tree nst = build_int_cst (integer_type_node, ns);
3947 (bit_and (convert (rshift:utype (convert:utype @1) {nst;})) @3))))))))
3948 /* bswap(x) >> C1 can sometimes be simplified to (T)x >> C2. */
3950 (rshift (convert? (bswap@2 @0)) INTEGER_CST@1)
3951 (if (BITS_PER_UNIT == 8
3952 && CHAR_TYPE_SIZE == 8
3953 && tree_fits_uhwi_p (@1))
3956 unsigned HOST_WIDE_INT prec = TYPE_PRECISION (TREE_TYPE (@2));
3957 unsigned HOST_WIDE_INT bits = tree_to_uhwi (@1);
3958 /* If the bswap was extended before the original shift, this
3959 byte (shift) has the sign of the extension, not the sign of
3960 the original shift. */
3961 tree st = TYPE_PRECISION (type) > prec ? TREE_TYPE (@2) : type;
3963 /* Special case: logical right shift of sign-extended bswap.
3964 (unsigned)(short)bswap16(x)>>12 is (unsigned)((short)x<<8)>>12. */
3965 (if (TYPE_PRECISION (type) > prec
3966 && !TYPE_UNSIGNED (TREE_TYPE (@2))
3967 && TYPE_UNSIGNED (type)
3968 && bits < prec && bits + 8 >= prec)
3969 (with { tree nst = build_int_cst (integer_type_node, prec - 8); }
3970 (rshift (convert (lshift:st (convert:st @0) {nst;})) @1))
3971 (if (bits + 8 == prec)
3972 (if (TYPE_UNSIGNED (st))
3973 (convert (convert:unsigned_char_type_node @0))
3974 (convert (convert:signed_char_type_node @0)))
3975 (if (bits < prec && bits + 8 > prec)
3978 tree nst = build_int_cst (integer_type_node, bits & 7);
3979 tree bt = TYPE_UNSIGNED (st) ? unsigned_char_type_node
3980 : signed_char_type_node;
3982 (convert (rshift:bt (convert:bt @0) {nst;})))))))))
3983 /* bswap(x) & C1 can sometimes be simplified to (x >> C2) & C1. */
3985 (bit_and (convert? (bswap@2 @0)) INTEGER_CST@1)
3986 (if (BITS_PER_UNIT == 8
3987 && tree_fits_uhwi_p (@1)
3988 && tree_to_uhwi (@1) < 256)
3991 unsigned HOST_WIDE_INT prec = TYPE_PRECISION (TREE_TYPE (@2));
3992 tree utype = unsigned_type_for (TREE_TYPE (@0));
3993 tree nst = build_int_cst (integer_type_node, prec - 8);
3995 (bit_and (convert (rshift:utype (convert:utype @0) {nst;})) @1)))))
3998 /* Combine COND_EXPRs and VEC_COND_EXPRs. */
4000 /* Simplify constant conditions.
4001 Only optimize constant conditions when the selected branch
4002 has the same type as the COND_EXPR. This avoids optimizing
4003 away "c ? x : throw", where the throw has a void type.
4004 Note that we cannot throw away the fold-const.c variant nor
4005 this one as we depend on doing this transform before possibly
4006 A ? B : B -> B triggers and the fold-const.c one can optimize
4007 0 ? A : B to B even if A has side-effects. Something
4008 genmatch cannot handle. */
4010 (cond INTEGER_CST@0 @1 @2)
4011 (if (integer_zerop (@0))
4012 (if (!VOID_TYPE_P (TREE_TYPE (@2)) || VOID_TYPE_P (type))
4014 (if (!VOID_TYPE_P (TREE_TYPE (@1)) || VOID_TYPE_P (type))
4017 (vec_cond VECTOR_CST@0 @1 @2)
4018 (if (integer_all_onesp (@0))
4020 (if (integer_zerop (@0))
4024 /* Sink unary operations to branches, but only if we do fold both. */
4025 (for op (negate bit_not abs absu)
4027 (op (vec_cond:s @0 @1 @2))
4028 (vec_cond @0 (op! @1) (op! @2))))
4030 /* Sink binary operation to branches, but only if we can fold it. */
4031 (for op (tcc_comparison plus minus mult bit_and bit_ior bit_xor
4032 lshift rshift rdiv trunc_div ceil_div floor_div round_div
4033 trunc_mod ceil_mod floor_mod round_mod min max)
4034 /* (c ? a : b) op (c ? d : e) --> c ? (a op d) : (b op e) */
4036 (op (vec_cond:s @0 @1 @2) (vec_cond:s @0 @3 @4))
4037 (vec_cond @0 (op! @1 @3) (op! @2 @4)))
4039 /* (c ? a : b) op d --> c ? (a op d) : (b op d) */
4041 (op (vec_cond:s @0 @1 @2) @3)
4042 (vec_cond @0 (op! @1 @3) (op! @2 @3)))
4044 (op @3 (vec_cond:s @0 @1 @2))
4045 (vec_cond @0 (op! @3 @1) (op! @3 @2))))
4049 (match (nop_atomic_bit_test_and_p @0 @1 @4)
4050 (bit_and (convert?@4 (ATOMIC_FETCH_OR_XOR_N @2 INTEGER_CST@0 @3))
4053 int ibit = tree_log2 (@0);
4054 int ibit2 = tree_log2 (@1);
4058 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))))))
4060 (match (nop_atomic_bit_test_and_p @0 @1 @3)
4061 (bit_and (convert?@3 (SYNC_FETCH_OR_XOR_N @2 INTEGER_CST@0))
4064 int ibit = tree_log2 (@0);
4065 int ibit2 = tree_log2 (@1);
4069 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))))))
4071 (match (nop_atomic_bit_test_and_p @0 @0 @4)
4074 (ATOMIC_FETCH_OR_XOR_N @2 (nop_convert? (lshift@0 integer_onep@5 @6)) @3))
4076 (if (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0)))))
4078 (match (nop_atomic_bit_test_and_p @0 @0 @4)
4081 (SYNC_FETCH_OR_XOR_N @2 (nop_convert? (lshift@0 integer_onep@3 @5))))
4083 (if (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0)))))
4085 (match (nop_atomic_bit_test_and_p @0 @1 @3)
4086 (bit_and@4 (convert?@3 (ATOMIC_FETCH_AND_N @2 INTEGER_CST@0 @5))
4089 int ibit = wi::exact_log2 (wi::zext (wi::bit_not (wi::to_wide (@0)),
4090 TYPE_PRECISION(type)));
4091 int ibit2 = tree_log2 (@1);
4095 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))))))
4097 (match (nop_atomic_bit_test_and_p @0 @1 @3)
4099 (convert?@3 (SYNC_FETCH_AND_AND_N @2 INTEGER_CST@0))
4102 int ibit = wi::exact_log2 (wi::zext (wi::bit_not (wi::to_wide (@0)),
4103 TYPE_PRECISION(type)));
4104 int ibit2 = tree_log2 (@1);
4108 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))))))
4110 (match (nop_atomic_bit_test_and_p @4 @0 @3)
4113 (ATOMIC_FETCH_AND_N @2 (nop_convert?@4 (bit_not (lshift@0 integer_onep@6 @7))) @5))
4115 (if (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@4)))))
4117 (match (nop_atomic_bit_test_and_p @4 @0 @3)
4120 (SYNC_FETCH_AND_AND_N @2 (nop_convert?@4 (bit_not (lshift@0 integer_onep@6 @7)))))
4122 (if (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@4)))))
4126 /* (v ? w : 0) ? a : b is just (v & w) ? a : b
4127 Currently disabled after pass lvec because ARM understands
4128 VEC_COND_EXPR<v==w,-1,0> but not a plain v==w fed to BIT_IOR_EXPR. */
4130 (vec_cond (vec_cond:s @0 @3 integer_zerop) @1 @2)
4131 (if (optimize_vectors_before_lowering_p () && types_match (@0, @3))
4132 (vec_cond (bit_and @0 @3) @1 @2)))
4134 (vec_cond (vec_cond:s @0 integer_all_onesp @3) @1 @2)
4135 (if (optimize_vectors_before_lowering_p () && types_match (@0, @3))
4136 (vec_cond (bit_ior @0 @3) @1 @2)))
4138 (vec_cond (vec_cond:s @0 integer_zerop @3) @1 @2)
4139 (if (optimize_vectors_before_lowering_p () && types_match (@0, @3))
4140 (vec_cond (bit_ior @0 (bit_not @3)) @2 @1)))
4142 (vec_cond (vec_cond:s @0 @3 integer_all_onesp) @1 @2)
4143 (if (optimize_vectors_before_lowering_p () && types_match (@0, @3))
4144 (vec_cond (bit_and @0 (bit_not @3)) @2 @1)))
4146 /* c1 ? c2 ? a : b : b --> (c1 & c2) ? a : b */
4148 (vec_cond @0 (vec_cond:s @1 @2 @3) @3)
4149 (if (optimize_vectors_before_lowering_p () && types_match (@0, @1))
4150 (vec_cond (bit_and @0 @1) @2 @3)))
4152 (vec_cond @0 @2 (vec_cond:s @1 @2 @3))
4153 (if (optimize_vectors_before_lowering_p () && types_match (@0, @1))
4154 (vec_cond (bit_ior @0 @1) @2 @3)))
4156 (vec_cond @0 (vec_cond:s @1 @2 @3) @2)
4157 (if (optimize_vectors_before_lowering_p () && types_match (@0, @1))
4158 (vec_cond (bit_ior (bit_not @0) @1) @2 @3)))
4160 (vec_cond @0 @3 (vec_cond:s @1 @2 @3))
4161 (if (optimize_vectors_before_lowering_p () && types_match (@0, @1))
4162 (vec_cond (bit_and (bit_not @0) @1) @2 @3)))
4164 /* Canonicalize mask ? { 0, ... } : { -1, ...} to ~mask if the mask
4165 types are compatible. */
4167 (vec_cond @0 VECTOR_CST@1 VECTOR_CST@2)
4168 (if (VECTOR_BOOLEAN_TYPE_P (type)
4169 && types_match (type, TREE_TYPE (@0)))
4170 (if (integer_zerop (@1) && integer_all_onesp (@2))
4172 (if (integer_all_onesp (@1) && integer_zerop (@2))
4175 /* A few simplifications of "a ? CST1 : CST2". */
4176 /* NOTE: Only do this on gimple as the if-chain-to-switch
4177 optimization depends on the gimple to have if statements in it. */
4180 (cond @0 INTEGER_CST@1 INTEGER_CST@2)
4182 (if (integer_zerop (@2))
4184 /* a ? 1 : 0 -> a if 0 and 1 are integral types. */
4185 (if (integer_onep (@1))
4186 (convert (convert:boolean_type_node @0)))
4187 /* a ? powerof2cst : 0 -> a << (log2(powerof2cst)) */
4188 (if (INTEGRAL_TYPE_P (type) && integer_pow2p (@1))
4190 tree shift = build_int_cst (integer_type_node, tree_log2 (@1));
4192 (lshift (convert (convert:boolean_type_node @0)) { shift; })))
4193 /* a ? -1 : 0 -> -a. No need to check the TYPE_PRECISION not being 1
4194 here as the powerof2cst case above will handle that case correctly. */
4195 (if (INTEGRAL_TYPE_P (type) && integer_all_onesp (@1))
4196 (negate (convert (convert:boolean_type_node @0))))))
4197 (if (integer_zerop (@1))
4199 tree booltrue = constant_boolean_node (true, boolean_type_node);
4202 /* a ? 0 : 1 -> !a. */
4203 (if (integer_onep (@2))
4204 (convert (bit_xor (convert:boolean_type_node @0) { booltrue; } )))
4205 /* a ? powerof2cst : 0 -> (!a) << (log2(powerof2cst)) */
4206 (if (INTEGRAL_TYPE_P (type) && integer_pow2p (@2))
4208 tree shift = build_int_cst (integer_type_node, tree_log2 (@2));
4210 (lshift (convert (bit_xor (convert:boolean_type_node @0) { booltrue; } ))
4212 /* a ? -1 : 0 -> -(!a). No need to check the TYPE_PRECISION not being 1
4213 here as the powerof2cst case above will handle that case correctly. */
4214 (if (INTEGRAL_TYPE_P (type) && integer_all_onesp (@2))
4215 (negate (convert (bit_xor (convert:boolean_type_node @0) { booltrue; } ))))
4223 /* Simplification moved from fold_cond_expr_with_comparison. It may also
4225 /* This pattern implements two kinds simplification:
4228 (cond (cmp (convert1? x) c1) (convert2? x) c2) -> (minmax (x c)) if:
4229 1) Conversions are type widening from smaller type.
4230 2) Const c1 equals to c2 after canonicalizing comparison.
4231 3) Comparison has tree code LT, LE, GT or GE.
4232 This specific pattern is needed when (cmp (convert x) c) may not
4233 be simplified by comparison patterns because of multiple uses of
4234 x. It also makes sense here because simplifying across multiple
4235 referred var is always benefitial for complicated cases.
4238 (cond (eq (convert1? x) c1) (convert2? x) c2) -> (cond (eq x c1) c1 c2). */
4239 (for cmp (lt le gt ge eq)
4241 (cond (cmp (convert1? @1) INTEGER_CST@3) (convert2? @1) INTEGER_CST@2)
4244 tree from_type = TREE_TYPE (@1);
4245 tree c1_type = TREE_TYPE (@3), c2_type = TREE_TYPE (@2);
4246 enum tree_code code = ERROR_MARK;
4248 if (INTEGRAL_TYPE_P (from_type)
4249 && int_fits_type_p (@2, from_type)
4250 && (types_match (c1_type, from_type)
4251 || (TYPE_PRECISION (c1_type) > TYPE_PRECISION (from_type)
4252 && (TYPE_UNSIGNED (from_type)
4253 || TYPE_SIGN (c1_type) == TYPE_SIGN (from_type))))
4254 && (types_match (c2_type, from_type)
4255 || (TYPE_PRECISION (c2_type) > TYPE_PRECISION (from_type)
4256 && (TYPE_UNSIGNED (from_type)
4257 || TYPE_SIGN (c2_type) == TYPE_SIGN (from_type)))))
4261 if (wi::to_widest (@3) == (wi::to_widest (@2) - 1))
4263 /* X <= Y - 1 equals to X < Y. */
4266 /* X > Y - 1 equals to X >= Y. */
4270 if (wi::to_widest (@3) == (wi::to_widest (@2) + 1))
4272 /* X < Y + 1 equals to X <= Y. */
4275 /* X >= Y + 1 equals to X > Y. */
4279 if (code != ERROR_MARK
4280 || wi::to_widest (@2) == wi::to_widest (@3))
4282 if (cmp == LT_EXPR || cmp == LE_EXPR)
4284 if (cmp == GT_EXPR || cmp == GE_EXPR)
4288 /* Can do A == C1 ? A : C2 -> A == C1 ? C1 : C2? */
4289 else if (int_fits_type_p (@3, from_type))
4293 (if (code == MAX_EXPR)
4294 (convert (max @1 (convert @2)))
4295 (if (code == MIN_EXPR)
4296 (convert (min @1 (convert @2)))
4297 (if (code == EQ_EXPR)
4298 (convert (cond (eq @1 (convert @3))
4299 (convert:from_type @3) (convert:from_type @2)))))))))
4301 /* (cond (cmp (convert? x) c1) (op x c2) c3) -> (op (minmax x c1) c2) if:
4303 1) OP is PLUS or MINUS.
4304 2) CMP is LT, LE, GT or GE.
4305 3) C3 == (C1 op C2), and computation doesn't have undefined behavior.
4307 This pattern also handles special cases like:
4309 A) Operand x is a unsigned to signed type conversion and c1 is
4310 integer zero. In this case,
4311 (signed type)x < 0 <=> x > MAX_VAL(signed type)
4312 (signed type)x >= 0 <=> x <= MAX_VAL(signed type)
4313 B) Const c1 may not equal to (C3 op' C2). In this case we also
4314 check equality for (c1+1) and (c1-1) by adjusting comparison
4317 TODO: Though signed type is handled by this pattern, it cannot be
4318 simplified at the moment because C standard requires additional
4319 type promotion. In order to match&simplify it here, the IR needs
4320 to be cleaned up by other optimizers, i.e, VRP. */
4321 (for op (plus minus)
4322 (for cmp (lt le gt ge)
4324 (cond (cmp (convert? @X) INTEGER_CST@1) (op @X INTEGER_CST@2) INTEGER_CST@3)
4325 (with { tree from_type = TREE_TYPE (@X), to_type = TREE_TYPE (@1); }
4326 (if (types_match (from_type, to_type)
4327 /* Check if it is special case A). */
4328 || (TYPE_UNSIGNED (from_type)
4329 && !TYPE_UNSIGNED (to_type)
4330 && TYPE_PRECISION (from_type) == TYPE_PRECISION (to_type)
4331 && integer_zerop (@1)
4332 && (cmp == LT_EXPR || cmp == GE_EXPR)))
4335 wi::overflow_type overflow = wi::OVF_NONE;
4336 enum tree_code code, cmp_code = cmp;
4338 wide_int c1 = wi::to_wide (@1);
4339 wide_int c2 = wi::to_wide (@2);
4340 wide_int c3 = wi::to_wide (@3);
4341 signop sgn = TYPE_SIGN (from_type);
4343 /* Handle special case A), given x of unsigned type:
4344 ((signed type)x < 0) <=> (x > MAX_VAL(signed type))
4345 ((signed type)x >= 0) <=> (x <= MAX_VAL(signed type)) */
4346 if (!types_match (from_type, to_type))
4348 if (cmp_code == LT_EXPR)
4350 if (cmp_code == GE_EXPR)
4352 c1 = wi::max_value (to_type);
4354 /* To simplify this pattern, we require c3 = (c1 op c2). Here we
4355 compute (c3 op' c2) and check if it equals to c1 with op' being
4356 the inverted operator of op. Make sure overflow doesn't happen
4357 if it is undefined. */
4358 if (op == PLUS_EXPR)
4359 real_c1 = wi::sub (c3, c2, sgn, &overflow);
4361 real_c1 = wi::add (c3, c2, sgn, &overflow);
4364 if (!overflow || !TYPE_OVERFLOW_UNDEFINED (from_type))
4366 /* Check if c1 equals to real_c1. Boundary condition is handled
4367 by adjusting comparison operation if necessary. */
4368 if (!wi::cmp (wi::sub (real_c1, 1, sgn, &overflow), c1, sgn)
4371 /* X <= Y - 1 equals to X < Y. */
4372 if (cmp_code == LE_EXPR)
4374 /* X > Y - 1 equals to X >= Y. */
4375 if (cmp_code == GT_EXPR)
4378 if (!wi::cmp (wi::add (real_c1, 1, sgn, &overflow), c1, sgn)
4381 /* X < Y + 1 equals to X <= Y. */
4382 if (cmp_code == LT_EXPR)
4384 /* X >= Y + 1 equals to X > Y. */
4385 if (cmp_code == GE_EXPR)
4388 if (code != cmp_code || !wi::cmp (real_c1, c1, sgn))
4390 if (cmp_code == LT_EXPR || cmp_code == LE_EXPR)
4392 if (cmp_code == GT_EXPR || cmp_code == GE_EXPR)
4397 (if (code == MAX_EXPR)
4398 (op (max @X { wide_int_to_tree (from_type, real_c1); })
4399 { wide_int_to_tree (from_type, c2); })
4400 (if (code == MIN_EXPR)
4401 (op (min @X { wide_int_to_tree (from_type, real_c1); })
4402 { wide_int_to_tree (from_type, c2); })))))))))
4404 (for cnd (cond vec_cond)
4405 /* A ? B : (A ? X : C) -> A ? B : C. */
4407 (cnd @0 (cnd @0 @1 @2) @3)
4410 (cnd @0 @1 (cnd @0 @2 @3))
4412 /* A ? B : (!A ? C : X) -> A ? B : C. */
4413 /* ??? This matches embedded conditions open-coded because genmatch
4414 would generate matching code for conditions in separate stmts only.
4415 The following is still important to merge then and else arm cases
4416 from if-conversion. */
4418 (cnd @0 @1 (cnd @2 @3 @4))
4419 (if (inverse_conditions_p (@0, @2))
4422 (cnd @0 (cnd @1 @2 @3) @4)
4423 (if (inverse_conditions_p (@0, @1))
4426 /* A ? B : B -> B. */
4431 /* !A ? B : C -> A ? C : B. */
4433 (cnd (logical_inverted_value truth_valued_p@0) @1 @2)
4436 /* abs/negative simplifications moved from fold_cond_expr_with_comparison,
4437 Need to handle (A - B) case as fold_cond_expr_with_comparison does.
4438 Need to handle UN* comparisons.
4440 None of these transformations work for modes with signed
4441 zeros. If A is +/-0, the first two transformations will
4442 change the sign of the result (from +0 to -0, or vice
4443 versa). The last four will fix the sign of the result,
4444 even though the original expressions could be positive or
4445 negative, depending on the sign of A.
4447 Note that all these transformations are correct if A is
4448 NaN, since the two alternatives (A and -A) are also NaNs. */
4450 (for cnd (cond vec_cond)
4451 /* A == 0 ? A : -A same as -A */
4454 (cnd (cmp @0 zerop) @0 (negate@1 @0))
4455 (if (!HONOR_SIGNED_ZEROS (type))
4458 (cnd (cmp @0 zerop) integer_zerop (negate@1 @0))
4459 (if (!HONOR_SIGNED_ZEROS (type))
4462 /* A != 0 ? A : -A same as A */
4465 (cnd (cmp @0 zerop) @0 (negate @0))
4466 (if (!HONOR_SIGNED_ZEROS (type))
4469 (cnd (cmp @0 zerop) @0 integer_zerop)
4470 (if (!HONOR_SIGNED_ZEROS (type))
4473 /* A >=/> 0 ? A : -A same as abs (A) */
4476 (cnd (cmp @0 zerop) @0 (negate @0))
4477 (if (!HONOR_SIGNED_ZEROS (type)
4478 && !TYPE_UNSIGNED (type))
4480 /* A <=/< 0 ? A : -A same as -abs (A) */
4483 (cnd (cmp @0 zerop) @0 (negate @0))
4484 (if (!HONOR_SIGNED_ZEROS (type)
4485 && !TYPE_UNSIGNED (type))
4486 (if (ANY_INTEGRAL_TYPE_P (type)
4487 && !TYPE_OVERFLOW_WRAPS (type))
4489 tree utype = unsigned_type_for (type);
4491 (convert (negate (absu:utype @0))))
4492 (negate (abs @0)))))
4496 /* -(type)!A -> (type)A - 1. */
4498 (negate (convert?:s (logical_inverted_value:s @0)))
4499 (if (INTEGRAL_TYPE_P (type)
4500 && TREE_CODE (type) != BOOLEAN_TYPE
4501 && TYPE_PRECISION (type) > 1
4502 && TREE_CODE (@0) == SSA_NAME
4503 && ssa_name_has_boolean_range (@0))
4504 (plus (convert:type @0) { build_all_ones_cst (type); })))
4506 /* A + (B vcmp C ? 1 : 0) -> A - (B vcmp C ? -1 : 0), since vector comparisons
4507 return all -1 or all 0 results. */
4508 /* ??? We could instead convert all instances of the vec_cond to negate,
4509 but that isn't necessarily a win on its own. */
4511 (plus:c @3 (view_convert? (vec_cond:s @0 integer_each_onep@1 integer_zerop@2)))
4512 (if (VECTOR_TYPE_P (type)
4513 && known_eq (TYPE_VECTOR_SUBPARTS (type),
4514 TYPE_VECTOR_SUBPARTS (TREE_TYPE (@1)))
4515 && (TYPE_MODE (TREE_TYPE (type))
4516 == TYPE_MODE (TREE_TYPE (TREE_TYPE (@1)))))
4517 (minus @3 (view_convert (vec_cond @0 (negate @1) @2)))))
4519 /* ... likewise A - (B vcmp C ? 1 : 0) -> A + (B vcmp C ? -1 : 0). */
4521 (minus @3 (view_convert? (vec_cond:s @0 integer_each_onep@1 integer_zerop@2)))
4522 (if (VECTOR_TYPE_P (type)
4523 && known_eq (TYPE_VECTOR_SUBPARTS (type),
4524 TYPE_VECTOR_SUBPARTS (TREE_TYPE (@1)))
4525 && (TYPE_MODE (TREE_TYPE (type))
4526 == TYPE_MODE (TREE_TYPE (TREE_TYPE (@1)))))
4527 (plus @3 (view_convert (vec_cond @0 (negate @1) @2)))))
4530 /* Simplifications of comparisons. */
4532 /* See if we can reduce the magnitude of a constant involved in a
4533 comparison by changing the comparison code. This is a canonicalization
4534 formerly done by maybe_canonicalize_comparison_1. */
4538 (cmp @0 uniform_integer_cst_p@1)
4539 (with { tree cst = uniform_integer_cst_p (@1); }
4540 (if (tree_int_cst_sgn (cst) == -1)
4541 (acmp @0 { build_uniform_cst (TREE_TYPE (@1),
4542 wide_int_to_tree (TREE_TYPE (cst),
4548 (cmp @0 uniform_integer_cst_p@1)
4549 (with { tree cst = uniform_integer_cst_p (@1); }
4550 (if (tree_int_cst_sgn (cst) == 1)
4551 (acmp @0 { build_uniform_cst (TREE_TYPE (@1),
4552 wide_int_to_tree (TREE_TYPE (cst),
4553 wi::to_wide (cst) - 1)); })))))
4555 /* We can simplify a logical negation of a comparison to the
4556 inverted comparison. As we cannot compute an expression
4557 operator using invert_tree_comparison we have to simulate
4558 that with expression code iteration. */
4559 (for cmp (tcc_comparison)
4560 icmp (inverted_tcc_comparison)
4561 ncmp (inverted_tcc_comparison_with_nans)
4562 /* Ideally we'd like to combine the following two patterns
4563 and handle some more cases by using
4564 (logical_inverted_value (cmp @0 @1))
4565 here but for that genmatch would need to "inline" that.
4566 For now implement what forward_propagate_comparison did. */
4568 (bit_not (cmp @0 @1))
4569 (if (VECTOR_TYPE_P (type)
4570 || (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1))
4571 /* Comparison inversion may be impossible for trapping math,
4572 invert_tree_comparison will tell us. But we can't use
4573 a computed operator in the replacement tree thus we have
4574 to play the trick below. */
4575 (with { enum tree_code ic = invert_tree_comparison
4576 (cmp, HONOR_NANS (@0)); }
4582 (bit_xor (cmp @0 @1) integer_truep)
4583 (with { enum tree_code ic = invert_tree_comparison
4584 (cmp, HONOR_NANS (@0)); }
4590 /* Transform comparisons of the form X - Y CMP 0 to X CMP Y.
4591 ??? The transformation is valid for the other operators if overflow
4592 is undefined for the type, but performing it here badly interacts
4593 with the transformation in fold_cond_expr_with_comparison which
4594 attempts to synthetize ABS_EXPR. */
4596 (for sub (minus pointer_diff)
4598 (cmp (sub@2 @0 @1) integer_zerop)
4599 (if (single_use (@2))
4602 /* Simplify (x < 0) ^ (y < 0) to (x ^ y) < 0 and
4603 (x >= 0) ^ (y >= 0) to (x ^ y) < 0. */
4606 (bit_xor (cmp:s @0 integer_zerop) (cmp:s @1 integer_zerop))
4607 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
4608 && !TYPE_UNSIGNED (TREE_TYPE (@0))
4609 && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
4610 (lt (bit_xor @0 @1) { build_zero_cst (TREE_TYPE (@0)); }))))
4611 /* Simplify (x < 0) ^ (y >= 0) to (x ^ y) >= 0 and
4612 (x >= 0) ^ (y < 0) to (x ^ y) >= 0. */
4614 (bit_xor:c (lt:s @0 integer_zerop) (ge:s @1 integer_zerop))
4615 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
4616 && !TYPE_UNSIGNED (TREE_TYPE (@0))
4617 && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
4618 (ge (bit_xor @0 @1) { build_zero_cst (TREE_TYPE (@0)); })))
4620 /* Transform comparisons of the form X * C1 CMP 0 to X CMP 0 in the
4621 signed arithmetic case. That form is created by the compiler
4622 often enough for folding it to be of value. One example is in
4623 computing loop trip counts after Operator Strength Reduction. */
4624 (for cmp (simple_comparison)
4625 scmp (swapped_simple_comparison)
4627 (cmp (mult@3 @0 INTEGER_CST@1) integer_zerop@2)
4628 /* Handle unfolded multiplication by zero. */
4629 (if (integer_zerop (@1))
4631 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
4632 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
4634 /* If @1 is negative we swap the sense of the comparison. */
4635 (if (tree_int_cst_sgn (@1) < 0)
4639 /* For integral types with undefined overflow fold
4640 x * C1 == C2 into x == C2 / C1 or false.
4641 If overflow wraps and C1 is odd, simplify to x == C2 / C1 in the ring
4645 (cmp (mult @0 INTEGER_CST@1) INTEGER_CST@2)
4646 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
4647 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
4648 && wi::to_wide (@1) != 0)
4649 (with { widest_int quot; }
4650 (if (wi::multiple_of_p (wi::to_widest (@2), wi::to_widest (@1),
4651 TYPE_SIGN (TREE_TYPE (@0)), "))
4652 (cmp @0 { wide_int_to_tree (TREE_TYPE (@0), quot); })
4653 { constant_boolean_node (cmp == NE_EXPR, type); }))
4654 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
4655 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))
4656 && (wi::bit_and (wi::to_wide (@1), 1) == 1))
4659 tree itype = TREE_TYPE (@0);
4660 int p = TYPE_PRECISION (itype);
4661 wide_int m = wi::one (p + 1) << p;
4662 wide_int a = wide_int::from (wi::to_wide (@1), p + 1, UNSIGNED);
4663 wide_int i = wide_int::from (wi::mod_inv (a, m),
4664 p, TYPE_SIGN (itype));
4665 wide_int_to_tree (itype, wi::mul (i, wi::to_wide (@2)));
4668 /* Simplify comparison of something with itself. For IEEE
4669 floating-point, we can only do some of these simplifications. */
4673 (if (! FLOAT_TYPE_P (TREE_TYPE (@0))
4674 || ! HONOR_NANS (@0))
4675 { constant_boolean_node (true, type); }
4677 /* With -ftrapping-math conversion to EQ loses an exception. */
4678 && (! FLOAT_TYPE_P (TREE_TYPE (@0))
4679 || ! flag_trapping_math))
4685 || ! FLOAT_TYPE_P (TREE_TYPE (@0))
4686 || ! HONOR_NANS (@0))
4687 { constant_boolean_node (false, type); })))
4688 (for cmp (unle unge uneq)
4691 { constant_boolean_node (true, type); }))
4692 (for cmp (unlt ungt)
4698 (if (!flag_trapping_math)
4699 { constant_boolean_node (false, type); }))
4701 /* x == ~x -> false */
4702 /* x != ~x -> true */
4705 (cmp:c @0 (bit_not @0))
4706 { constant_boolean_node (cmp == NE_EXPR, type); }))
4708 /* Fold ~X op ~Y as Y op X. */
4709 (for cmp (simple_comparison)
4711 (cmp (bit_not@2 @0) (bit_not@3 @1))
4712 (if (single_use (@2) && single_use (@3))
4715 /* Fold ~X op C as X op' ~C, where op' is the swapped comparison. */
4716 (for cmp (simple_comparison)
4717 scmp (swapped_simple_comparison)
4719 (cmp (bit_not@2 @0) CONSTANT_CLASS_P@1)
4720 (if (single_use (@2)
4721 && (TREE_CODE (@1) == INTEGER_CST || TREE_CODE (@1) == VECTOR_CST))
4722 (scmp @0 (bit_not @1)))))
4724 (for cmp (simple_comparison)
4725 /* Fold (double)float1 CMP (double)float2 into float1 CMP float2. */
4727 (cmp (convert@2 @0) (convert? @1))
4728 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
4729 && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@2))
4730 == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0)))
4731 && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@2))
4732 == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1))))
4735 tree type1 = TREE_TYPE (@1);
4736 if (TREE_CODE (@1) == REAL_CST && !DECIMAL_FLOAT_TYPE_P (type1))
4738 REAL_VALUE_TYPE orig = TREE_REAL_CST (@1);
4739 if (TYPE_PRECISION (type1) > TYPE_PRECISION (float_type_node)
4740 && exact_real_truncate (TYPE_MODE (float_type_node), &orig))
4741 type1 = float_type_node;
4742 if (TYPE_PRECISION (type1) > TYPE_PRECISION (double_type_node)
4743 && exact_real_truncate (TYPE_MODE (double_type_node), &orig))
4744 type1 = double_type_node;
4747 = (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type1)
4748 ? TREE_TYPE (@0) : type1);
4750 (if (TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (newtype))
4751 (cmp (convert:newtype @0) (convert:newtype @1))))))
4755 /* IEEE doesn't distinguish +0 and -0 in comparisons. */
4757 /* a CMP (-0) -> a CMP 0 */
4758 (if (REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@1)))
4759 (cmp @0 { build_real (TREE_TYPE (@1), dconst0); }))
4760 /* (-0) CMP b -> 0 CMP b. */
4761 (if (TREE_CODE (@0) == REAL_CST
4762 && REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@0)))
4763 (cmp { build_real (TREE_TYPE (@0), dconst0); } @1))
4764 /* x != NaN is always true, other ops are always false. */
4765 (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
4766 && !tree_expr_signaling_nan_p (@1)
4767 && !tree_expr_maybe_signaling_nan_p (@0))
4768 { constant_boolean_node (cmp == NE_EXPR, type); })
4769 /* NaN != y is always true, other ops are always false. */
4770 (if (TREE_CODE (@0) == REAL_CST
4771 && REAL_VALUE_ISNAN (TREE_REAL_CST (@0))
4772 && !tree_expr_signaling_nan_p (@0)
4773 && !tree_expr_signaling_nan_p (@1))
4774 { constant_boolean_node (cmp == NE_EXPR, type); })
4775 /* Fold comparisons against infinity. */
4776 (if (REAL_VALUE_ISINF (TREE_REAL_CST (@1))
4777 && MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (@1))))
4780 REAL_VALUE_TYPE max;
4781 enum tree_code code = cmp;
4782 bool neg = REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1));
4784 code = swap_tree_comparison (code);
4787 /* x > +Inf is always false, if we ignore NaNs or exceptions. */
4788 (if (code == GT_EXPR
4789 && !(HONOR_NANS (@0) && flag_trapping_math))
4790 { constant_boolean_node (false, type); })
4791 (if (code == LE_EXPR)
4792 /* x <= +Inf is always true, if we don't care about NaNs. */
4793 (if (! HONOR_NANS (@0))
4794 { constant_boolean_node (true, type); }
4795 /* x <= +Inf is the same as x == x, i.e. !isnan(x), but this loses
4796 an "invalid" exception. */
4797 (if (!flag_trapping_math)
4799 /* x == +Inf and x >= +Inf are always equal to x > DBL_MAX, but
4800 for == this introduces an exception for x a NaN. */
4801 (if ((code == EQ_EXPR && !(HONOR_NANS (@0) && flag_trapping_math))
4803 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
4805 (lt @0 { build_real (TREE_TYPE (@0), max); })
4806 (gt @0 { build_real (TREE_TYPE (@0), max); }))))
4807 /* x < +Inf is always equal to x <= DBL_MAX. */
4808 (if (code == LT_EXPR)
4809 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
4811 (ge @0 { build_real (TREE_TYPE (@0), max); })
4812 (le @0 { build_real (TREE_TYPE (@0), max); }))))
4813 /* x != +Inf is always equal to !(x > DBL_MAX), but this introduces
4814 an exception for x a NaN so use an unordered comparison. */
4815 (if (code == NE_EXPR)
4816 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
4817 (if (! HONOR_NANS (@0))
4819 (ge @0 { build_real (TREE_TYPE (@0), max); })
4820 (le @0 { build_real (TREE_TYPE (@0), max); }))
4822 (unge @0 { build_real (TREE_TYPE (@0), max); })
4823 (unle @0 { build_real (TREE_TYPE (@0), max); }))))))))))
4825 /* If this is a comparison of a real constant with a PLUS_EXPR
4826 or a MINUS_EXPR of a real constant, we can convert it into a
4827 comparison with a revised real constant as long as no overflow
4828 occurs when unsafe_math_optimizations are enabled. */
4829 (if (flag_unsafe_math_optimizations)
4830 (for op (plus minus)
4832 (cmp (op @0 REAL_CST@1) REAL_CST@2)
4835 tree tem = const_binop (op == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR,
4836 TREE_TYPE (@1), @2, @1);
4838 (if (tem && !TREE_OVERFLOW (tem))
4839 (cmp @0 { tem; }))))))
4841 /* Likewise, we can simplify a comparison of a real constant with
4842 a MINUS_EXPR whose first operand is also a real constant, i.e.
4843 (c1 - x) < c2 becomes x > c1-c2. Reordering is allowed on
4844 floating-point types only if -fassociative-math is set. */
4845 (if (flag_associative_math)
4847 (cmp (minus REAL_CST@0 @1) REAL_CST@2)
4848 (with { tree tem = const_binop (MINUS_EXPR, TREE_TYPE (@1), @0, @2); }
4849 (if (tem && !TREE_OVERFLOW (tem))
4850 (cmp { tem; } @1)))))
4852 /* Fold comparisons against built-in math functions. */
4853 (if (flag_unsafe_math_optimizations && ! flag_errno_math)
4856 (cmp (sq @0) REAL_CST@1)
4858 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
4860 /* sqrt(x) < y is always false, if y is negative. */
4861 (if (cmp == EQ_EXPR || cmp == LT_EXPR || cmp == LE_EXPR)
4862 { constant_boolean_node (false, type); })
4863 /* sqrt(x) > y is always true, if y is negative and we
4864 don't care about NaNs, i.e. negative values of x. */
4865 (if (cmp == NE_EXPR || !HONOR_NANS (@0))
4866 { constant_boolean_node (true, type); })
4867 /* sqrt(x) > y is the same as x >= 0, if y is negative. */
4868 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })))
4869 (if (real_equal (TREE_REAL_CST_PTR (@1), &dconst0))
4871 /* sqrt(x) < 0 is always false. */
4872 (if (cmp == LT_EXPR)
4873 { constant_boolean_node (false, type); })
4874 /* sqrt(x) >= 0 is always true if we don't care about NaNs. */
4875 (if (cmp == GE_EXPR && !HONOR_NANS (@0))
4876 { constant_boolean_node (true, type); })
4877 /* sqrt(x) <= 0 -> x == 0. */
4878 (if (cmp == LE_EXPR)
4880 /* Otherwise sqrt(x) cmp 0 -> x cmp 0. Here cmp can be >=, >,
4881 == or !=. In the last case:
4883 (sqrt(x) != 0) == (NaN != 0) == true == (x != 0)
4885 if x is negative or NaN. Due to -funsafe-math-optimizations,
4886 the results for other x follow from natural arithmetic. */
4888 (if ((cmp == LT_EXPR
4892 && !REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
4893 /* Give up for -frounding-math. */
4894 && !HONOR_SIGN_DEPENDENT_ROUNDING (TREE_TYPE (@0)))
4898 enum tree_code ncmp = cmp;
4899 const real_format *fmt
4900 = REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (@0)));
4901 real_arithmetic (&c2, MULT_EXPR,
4902 &TREE_REAL_CST (@1), &TREE_REAL_CST (@1));
4903 real_convert (&c2, fmt, &c2);
4904 /* See PR91734: if c2 is inexact and sqrt(c2) < c (or sqrt(c2) >= c),
4905 then change LT_EXPR into LE_EXPR or GE_EXPR into GT_EXPR. */
4906 if (!REAL_VALUE_ISINF (c2))
4908 tree c3 = fold_const_call (CFN_SQRT, TREE_TYPE (@0),
4909 build_real (TREE_TYPE (@0), c2));
4910 if (c3 == NULL_TREE || TREE_CODE (c3) != REAL_CST)
4912 else if ((cmp == LT_EXPR || cmp == GE_EXPR)
4913 && real_less (&TREE_REAL_CST (c3), &TREE_REAL_CST (@1)))
4914 ncmp = cmp == LT_EXPR ? LE_EXPR : GT_EXPR;
4915 else if ((cmp == LE_EXPR || cmp == GT_EXPR)
4916 && real_less (&TREE_REAL_CST (@1), &TREE_REAL_CST (c3)))
4917 ncmp = cmp == LE_EXPR ? LT_EXPR : GE_EXPR;
4920 /* With rounding to even, sqrt of up to 3 different values
4921 gives the same normal result, so in some cases c2 needs
4923 REAL_VALUE_TYPE c2alt, tow;
4924 if (cmp == LT_EXPR || cmp == GE_EXPR)
4928 real_nextafter (&c2alt, fmt, &c2, &tow);
4929 real_convert (&c2alt, fmt, &c2alt);
4930 if (REAL_VALUE_ISINF (c2alt))
4934 c3 = fold_const_call (CFN_SQRT, TREE_TYPE (@0),
4935 build_real (TREE_TYPE (@0), c2alt));
4936 if (c3 == NULL_TREE || TREE_CODE (c3) != REAL_CST)
4938 else if (real_equal (&TREE_REAL_CST (c3),
4939 &TREE_REAL_CST (@1)))
4945 (if (cmp == GT_EXPR || cmp == GE_EXPR)
4946 (if (REAL_VALUE_ISINF (c2))
4947 /* sqrt(x) > y is x == +Inf, when y is very large. */
4948 (if (HONOR_INFINITIES (@0))
4949 (eq @0 { build_real (TREE_TYPE (@0), c2); })
4950 { constant_boolean_node (false, type); })
4951 /* sqrt(x) > c is the same as x > c*c. */
4952 (if (ncmp != ERROR_MARK)
4953 (if (ncmp == GE_EXPR)
4954 (ge @0 { build_real (TREE_TYPE (@0), c2); })
4955 (gt @0 { build_real (TREE_TYPE (@0), c2); }))))
4956 /* else if (cmp == LT_EXPR || cmp == LE_EXPR) */
4957 (if (REAL_VALUE_ISINF (c2))
4959 /* sqrt(x) < y is always true, when y is a very large
4960 value and we don't care about NaNs or Infinities. */
4961 (if (! HONOR_NANS (@0) && ! HONOR_INFINITIES (@0))
4962 { constant_boolean_node (true, type); })
4963 /* sqrt(x) < y is x != +Inf when y is very large and we
4964 don't care about NaNs. */
4965 (if (! HONOR_NANS (@0))
4966 (ne @0 { build_real (TREE_TYPE (@0), c2); }))
4967 /* sqrt(x) < y is x >= 0 when y is very large and we
4968 don't care about Infinities. */
4969 (if (! HONOR_INFINITIES (@0))
4970 (ge @0 { build_real (TREE_TYPE (@0), dconst0); }))
4971 /* sqrt(x) < y is x >= 0 && x != +Inf, when y is large. */
4974 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
4975 (ne @0 { build_real (TREE_TYPE (@0), c2); }))))
4976 /* sqrt(x) < c is the same as x < c*c, if we ignore NaNs. */
4977 (if (ncmp != ERROR_MARK && ! HONOR_NANS (@0))
4978 (if (ncmp == LT_EXPR)
4979 (lt @0 { build_real (TREE_TYPE (@0), c2); })
4980 (le @0 { build_real (TREE_TYPE (@0), c2); }))
4981 /* sqrt(x) < c is the same as x >= 0 && x < c*c. */
4982 (if (ncmp != ERROR_MARK && GENERIC)
4983 (if (ncmp == LT_EXPR)
4985 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
4986 (lt @0 { build_real (TREE_TYPE (@0), c2); }))
4988 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
4989 (le @0 { build_real (TREE_TYPE (@0), c2); })))))))))))
4990 /* Transform sqrt(x) cmp sqrt(y) -> x cmp y. */
4992 (cmp (sq @0) (sq @1))
4993 (if (! HONOR_NANS (@0))
4996 /* Optimize various special cases of (FTYPE) N CMP (FTYPE) M. */
4997 (for cmp (lt le eq ne ge gt unordered ordered unlt unle ungt unge uneq ltgt)
4998 icmp (lt le eq ne ge gt unordered ordered lt le gt ge eq ne)
5000 (cmp (float@0 @1) (float @2))
5001 (if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (@0))
5002 && ! DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0)))
5005 format_helper fmt (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (@0))));
5006 tree type1 = TREE_TYPE (@1);
5007 bool type1_signed_p = TYPE_SIGN (type1) == SIGNED;
5008 tree type2 = TREE_TYPE (@2);
5009 bool type2_signed_p = TYPE_SIGN (type2) == SIGNED;
5011 (if (fmt.can_represent_integral_type_p (type1)
5012 && fmt.can_represent_integral_type_p (type2))
5013 (if (cmp == ORDERED_EXPR || cmp == UNORDERED_EXPR)
5014 { constant_boolean_node (cmp == ORDERED_EXPR, type); }
5015 (if (TYPE_PRECISION (type1) > TYPE_PRECISION (type2)
5016 && type1_signed_p >= type2_signed_p)
5017 (icmp @1 (convert @2))
5018 (if (TYPE_PRECISION (type1) < TYPE_PRECISION (type2)
5019 && type1_signed_p <= type2_signed_p)
5020 (icmp (convert:type2 @1) @2)
5021 (if (TYPE_PRECISION (type1) == TYPE_PRECISION (type2)
5022 && type1_signed_p == type2_signed_p)
5023 (icmp @1 @2))))))))))
5025 /* Optimize various special cases of (FTYPE) N CMP CST. */
5026 (for cmp (lt le eq ne ge gt)
5027 icmp (le le eq ne ge ge)
5029 (cmp (float @0) REAL_CST@1)
5030 (if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (@1))
5031 && ! DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1)))
5034 tree itype = TREE_TYPE (@0);
5035 format_helper fmt (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (@1))));
5036 const REAL_VALUE_TYPE *cst = TREE_REAL_CST_PTR (@1);
5037 /* Be careful to preserve any potential exceptions due to
5038 NaNs. qNaNs are ok in == or != context.
5039 TODO: relax under -fno-trapping-math or
5040 -fno-signaling-nans. */
5042 = real_isnan (cst) && (cst->signalling
5043 || (cmp != EQ_EXPR && cmp != NE_EXPR));
5045 /* TODO: allow non-fitting itype and SNaNs when
5046 -fno-trapping-math. */
5047 (if (fmt.can_represent_integral_type_p (itype) && ! exception_p)
5050 signop isign = TYPE_SIGN (itype);
5051 REAL_VALUE_TYPE imin, imax;
5052 real_from_integer (&imin, fmt, wi::min_value (itype), isign);
5053 real_from_integer (&imax, fmt, wi::max_value (itype), isign);
5055 REAL_VALUE_TYPE icst;
5056 if (cmp == GT_EXPR || cmp == GE_EXPR)
5057 real_ceil (&icst, fmt, cst);
5058 else if (cmp == LT_EXPR || cmp == LE_EXPR)
5059 real_floor (&icst, fmt, cst);
5061 real_trunc (&icst, fmt, cst);
5063 bool cst_int_p = !real_isnan (cst) && real_identical (&icst, cst);
5065 bool overflow_p = false;
5067 = real_to_integer (&icst, &overflow_p, TYPE_PRECISION (itype));
5070 /* Optimize cases when CST is outside of ITYPE's range. */
5071 (if (real_compare (LT_EXPR, cst, &imin))
5072 { constant_boolean_node (cmp == GT_EXPR || cmp == GE_EXPR || cmp == NE_EXPR,
5074 (if (real_compare (GT_EXPR, cst, &imax))
5075 { constant_boolean_node (cmp == LT_EXPR || cmp == LE_EXPR || cmp == NE_EXPR,
5077 /* Remove cast if CST is an integer representable by ITYPE. */
5079 (cmp @0 { gcc_assert (!overflow_p);
5080 wide_int_to_tree (itype, icst_val); })
5082 /* When CST is fractional, optimize
5083 (FTYPE) N == CST -> 0
5084 (FTYPE) N != CST -> 1. */
5085 (if (cmp == EQ_EXPR || cmp == NE_EXPR)
5086 { constant_boolean_node (cmp == NE_EXPR, type); })
5087 /* Otherwise replace with sensible integer constant. */
5090 gcc_checking_assert (!overflow_p);
5092 (icmp @0 { wide_int_to_tree (itype, icst_val); })))))))))
5094 /* Fold A /[ex] B CMP C to A CMP B * C. */
5097 (cmp (exact_div @0 @1) INTEGER_CST@2)
5098 (if (!integer_zerop (@1))
5099 (if (wi::to_wide (@2) == 0)
5101 (if (TREE_CODE (@1) == INTEGER_CST)
5104 wi::overflow_type ovf;
5105 wide_int prod = wi::mul (wi::to_wide (@2), wi::to_wide (@1),
5106 TYPE_SIGN (TREE_TYPE (@1)), &ovf);
5109 { constant_boolean_node (cmp == NE_EXPR, type); }
5110 (cmp @0 { wide_int_to_tree (TREE_TYPE (@0), prod); }))))))))
5111 (for cmp (lt le gt ge)
5113 (cmp (exact_div @0 INTEGER_CST@1) INTEGER_CST@2)
5114 (if (wi::gt_p (wi::to_wide (@1), 0, TYPE_SIGN (TREE_TYPE (@1))))
5117 wi::overflow_type ovf;
5118 wide_int prod = wi::mul (wi::to_wide (@2), wi::to_wide (@1),
5119 TYPE_SIGN (TREE_TYPE (@1)), &ovf);
5122 { constant_boolean_node (wi::lt_p (wi::to_wide (@2), 0,
5123 TYPE_SIGN (TREE_TYPE (@2)))
5124 != (cmp == LT_EXPR || cmp == LE_EXPR), type); }
5125 (cmp @0 { wide_int_to_tree (TREE_TYPE (@0), prod); }))))))
5127 /* Fold (size_t)(A /[ex] B) CMP C to (size_t)A CMP (size_t)B * C or A CMP' 0.
5129 For small C (less than max/B), this is (size_t)A CMP (size_t)B * C.
5130 For large C (more than min/B+2^size), this is also true, with the
5131 multiplication computed modulo 2^size.
5132 For intermediate C, this just tests the sign of A. */
5133 (for cmp (lt le gt ge)
5136 (cmp (convert (exact_div @0 INTEGER_CST@1)) INTEGER_CST@2)
5137 (if (tree_nop_conversion_p (TREE_TYPE (@0), TREE_TYPE (@2))
5138 && TYPE_UNSIGNED (TREE_TYPE (@2)) && !TYPE_UNSIGNED (TREE_TYPE (@0))
5139 && wi::gt_p (wi::to_wide (@1), 0, TYPE_SIGN (TREE_TYPE (@1))))
5142 tree utype = TREE_TYPE (@2);
5143 wide_int denom = wi::to_wide (@1);
5144 wide_int right = wi::to_wide (@2);
5145 wide_int smax = wi::sdiv_trunc (wi::max_value (TREE_TYPE (@0)), denom);
5146 wide_int smin = wi::sdiv_trunc (wi::min_value (TREE_TYPE (@0)), denom);
5147 bool small = wi::leu_p (right, smax);
5148 bool large = wi::geu_p (right, smin);
5150 (if (small || large)
5151 (cmp (convert:utype @0) (mult @2 (convert @1)))
5152 (cmp2 @0 { build_zero_cst (TREE_TYPE (@0)); }))))))
5154 /* Unordered tests if either argument is a NaN. */
5156 (bit_ior (unordered @0 @0) (unordered @1 @1))
5157 (if (types_match (@0, @1))
5160 (bit_and (ordered @0 @0) (ordered @1 @1))
5161 (if (types_match (@0, @1))
5164 (bit_ior:c (unordered @0 @0) (unordered:c@2 @0 @1))
5167 (bit_and:c (ordered @0 @0) (ordered:c@2 @0 @1))
5170 /* Simple range test simplifications. */
5171 /* A < B || A >= B -> true. */
5172 (for test1 (lt le le le ne ge)
5173 test2 (ge gt ge ne eq ne)
5175 (bit_ior:c (test1 @0 @1) (test2 @0 @1))
5176 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
5177 || VECTOR_INTEGER_TYPE_P (TREE_TYPE (@0)))
5178 { constant_boolean_node (true, type); })))
5179 /* A < B && A >= B -> false. */
5180 (for test1 (lt lt lt le ne eq)
5181 test2 (ge gt eq gt eq gt)
5183 (bit_and:c (test1 @0 @1) (test2 @0 @1))
5184 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
5185 || VECTOR_INTEGER_TYPE_P (TREE_TYPE (@0)))
5186 { constant_boolean_node (false, type); })))
5188 /* A & (2**N - 1) <= 2**K - 1 -> A & (2**N - 2**K) == 0
5189 A & (2**N - 1) > 2**K - 1 -> A & (2**N - 2**K) != 0
5191 Note that comparisons
5192 A & (2**N - 1) < 2**K -> A & (2**N - 2**K) == 0
5193 A & (2**N - 1) >= 2**K -> A & (2**N - 2**K) != 0
5194 will be canonicalized to above so there's no need to
5201 (cmp (bit_and@0 @1 INTEGER_CST@2) INTEGER_CST@3)
5202 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)))
5205 tree ty = TREE_TYPE (@0);
5206 unsigned prec = TYPE_PRECISION (ty);
5207 wide_int mask = wi::to_wide (@2, prec);
5208 wide_int rhs = wi::to_wide (@3, prec);
5209 signop sgn = TYPE_SIGN (ty);
5211 (if ((mask & (mask + 1)) == 0 && wi::gt_p (rhs, 0, sgn)
5212 && (rhs & (rhs + 1)) == 0 && wi::ge_p (mask, rhs, sgn))
5213 (eqcmp (bit_and @1 { wide_int_to_tree (ty, mask - rhs); })
5214 { build_zero_cst (ty); }))))))
5216 /* Transform comparisons of the form (X & Y) CMP 0 to X CMP2 Z
5217 where ~Y + 1 == pow2 and Z = ~Y. */
5218 (for cst (VECTOR_CST INTEGER_CST)
5222 (cmp (bit_and:c@2 @0 cst@1) integer_zerop)
5223 (with { tree csts = bitmask_inv_cst_vector_p (@1); }
5224 (if (csts && (VECTOR_TYPE_P (TREE_TYPE (@1)) || single_use (@2)))
5225 (if (TYPE_UNSIGNED (TREE_TYPE (@1)))
5227 (with { tree utype = unsigned_type_for (TREE_TYPE (@1)); }
5228 (icmp (convert:utype @0) { csts; }))))))))
5230 /* -A CMP -B -> B CMP A. */
5231 (for cmp (tcc_comparison)
5232 scmp (swapped_tcc_comparison)
5234 (cmp (negate @0) (negate @1))
5235 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
5236 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
5237 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
5240 (cmp (negate @0) CONSTANT_CLASS_P@1)
5241 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
5242 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
5243 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
5244 (with { tree tem = const_unop (NEGATE_EXPR, TREE_TYPE (@0), @1); }
5245 (if (tem && !TREE_OVERFLOW (tem))
5246 (scmp @0 { tem; }))))))
5248 /* Convert ABS_EXPR<x> == 0 or ABS_EXPR<x> != 0 to x == 0 or x != 0. */
5251 (op (abs @0) zerop@1)
5254 /* From fold_sign_changed_comparison and fold_widened_comparison.
5255 FIXME: the lack of symmetry is disturbing. */
5256 (for cmp (simple_comparison)
5258 (cmp (convert@0 @00) (convert?@1 @10))
5259 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
5260 /* Disable this optimization if we're casting a function pointer
5261 type on targets that require function pointer canonicalization. */
5262 && !(targetm.have_canonicalize_funcptr_for_compare ()
5263 && ((POINTER_TYPE_P (TREE_TYPE (@00))
5264 && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@00))))
5265 || (POINTER_TYPE_P (TREE_TYPE (@10))
5266 && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@10))))))
5268 (if (TYPE_PRECISION (TREE_TYPE (@00)) == TYPE_PRECISION (TREE_TYPE (@0))
5269 && (TREE_CODE (@10) == INTEGER_CST
5271 && (TYPE_UNSIGNED (TREE_TYPE (@00)) == TYPE_UNSIGNED (TREE_TYPE (@0))
5274 && !POINTER_TYPE_P (TREE_TYPE (@00)))
5275 /* ??? The special-casing of INTEGER_CST conversion was in the original
5276 code and here to avoid a spurious overflow flag on the resulting
5277 constant which fold_convert produces. */
5278 (if (TREE_CODE (@1) == INTEGER_CST)
5279 (cmp @00 { force_fit_type (TREE_TYPE (@00), wi::to_widest (@1), 0,
5280 TREE_OVERFLOW (@1)); })
5281 (cmp @00 (convert @1)))
5283 (if (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (TREE_TYPE (@00)))
5284 /* If possible, express the comparison in the shorter mode. */
5285 (if ((cmp == EQ_EXPR || cmp == NE_EXPR
5286 || TYPE_UNSIGNED (TREE_TYPE (@0)) == TYPE_UNSIGNED (TREE_TYPE (@00))
5287 || (!TYPE_UNSIGNED (TREE_TYPE (@0))
5288 && TYPE_UNSIGNED (TREE_TYPE (@00))))
5289 && (types_match (TREE_TYPE (@10), TREE_TYPE (@00))
5290 || ((TYPE_PRECISION (TREE_TYPE (@00))
5291 >= TYPE_PRECISION (TREE_TYPE (@10)))
5292 && (TYPE_UNSIGNED (TREE_TYPE (@00))
5293 == TYPE_UNSIGNED (TREE_TYPE (@10))))
5294 || (TREE_CODE (@10) == INTEGER_CST
5295 && INTEGRAL_TYPE_P (TREE_TYPE (@00))
5296 && int_fits_type_p (@10, TREE_TYPE (@00)))))
5297 (cmp @00 (convert @10))
5298 (if (TREE_CODE (@10) == INTEGER_CST
5299 && INTEGRAL_TYPE_P (TREE_TYPE (@00))
5300 && !int_fits_type_p (@10, TREE_TYPE (@00)))
5303 tree min = lower_bound_in_type (TREE_TYPE (@10), TREE_TYPE (@00));
5304 tree max = upper_bound_in_type (TREE_TYPE (@10), TREE_TYPE (@00));
5305 bool above = integer_nonzerop (const_binop (LT_EXPR, type, max, @10));
5306 bool below = integer_nonzerop (const_binop (LT_EXPR, type, @10, min));
5308 (if (above || below)
5309 (if (cmp == EQ_EXPR || cmp == NE_EXPR)
5310 { constant_boolean_node (cmp == EQ_EXPR ? false : true, type); }
5311 (if (cmp == LT_EXPR || cmp == LE_EXPR)
5312 { constant_boolean_node (above ? true : false, type); }
5313 (if (cmp == GT_EXPR || cmp == GE_EXPR)
5314 { constant_boolean_node (above ? false : true, type); }))))))))))))
5318 /* SSA names are canonicalized to 2nd place. */
5319 (cmp addr@0 SSA_NAME@1)
5321 { poly_int64 off; tree base; }
5322 /* A local variable can never be pointed to by
5323 the default SSA name of an incoming parameter. */
5324 (if (SSA_NAME_IS_DEFAULT_DEF (@1)
5325 && TREE_CODE (SSA_NAME_VAR (@1)) == PARM_DECL
5326 && (base = get_base_address (TREE_OPERAND (@0, 0)))
5327 && TREE_CODE (base) == VAR_DECL
5328 && auto_var_in_fn_p (base, current_function_decl))
5329 (if (cmp == NE_EXPR)
5330 { constant_boolean_node (true, type); }
5331 { constant_boolean_node (false, type); })
5332 /* If the address is based on @1 decide using the offset. */
5333 (if ((base = get_addr_base_and_unit_offset (TREE_OPERAND (@0, 0), &off))
5334 && TREE_CODE (base) == MEM_REF
5335 && TREE_OPERAND (base, 0) == @1)
5336 (with { off += mem_ref_offset (base).force_shwi (); }
5337 (if (known_ne (off, 0))
5338 { constant_boolean_node (cmp == NE_EXPR, type); }
5339 (if (known_eq (off, 0))
5340 { constant_boolean_node (cmp == EQ_EXPR, type); }))))))))
5342 /* Equality compare simplifications from fold_binary */
5345 /* If we have (A | C) == D where C & ~D != 0, convert this into 0.
5346 Similarly for NE_EXPR. */
5348 (cmp (convert?@3 (bit_ior @0 INTEGER_CST@1)) INTEGER_CST@2)
5349 (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0))
5350 && wi::bit_and_not (wi::to_wide (@1), wi::to_wide (@2)) != 0)
5351 { constant_boolean_node (cmp == NE_EXPR, type); }))
5353 /* (X ^ Y) == 0 becomes X == Y, and (X ^ Y) != 0 becomes X != Y. */
5355 (cmp (bit_xor @0 @1) integer_zerop)
5358 /* (X ^ Y) == Y becomes X == 0.
5359 Likewise (X ^ Y) == X becomes Y == 0. */
5361 (cmp:c (bit_xor:c @0 @1) @0)
5362 (cmp @1 { build_zero_cst (TREE_TYPE (@1)); }))
5365 /* (X & Y) == X becomes (X & ~Y) == 0. */
5367 (cmp:c (bit_and:c @0 @1) @0)
5368 (cmp (bit_and @0 (bit_not! @1)) { build_zero_cst (TREE_TYPE (@0)); }))
5370 (cmp:c (convert@3 (bit_and (convert@2 @0) INTEGER_CST@1)) (convert @0))
5371 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
5372 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
5373 && INTEGRAL_TYPE_P (TREE_TYPE (@3))
5374 && TYPE_PRECISION (TREE_TYPE (@2)) == TYPE_PRECISION (TREE_TYPE (@0))
5375 && TYPE_PRECISION (TREE_TYPE (@3)) > TYPE_PRECISION (TREE_TYPE (@2))
5376 && !wi::neg_p (wi::to_wide (@1)))
5377 (cmp (bit_and @0 (convert (bit_not @1)))
5378 { build_zero_cst (TREE_TYPE (@0)); })))
5380 /* (X | Y) == Y becomes (X & ~Y) == 0. */
5382 (cmp:c (bit_ior:c @0 @1) @1)
5383 (cmp (bit_and @0 (bit_not! @1)) { build_zero_cst (TREE_TYPE (@0)); }))
5386 /* (X ^ C1) op C2 can be rewritten as X op (C1 ^ C2). */
5388 (cmp (convert?@3 (bit_xor @0 INTEGER_CST@1)) INTEGER_CST@2)
5389 (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0)))
5390 (cmp @0 (bit_xor @1 (convert @2)))))
5393 (cmp (convert? addr@0) integer_zerop)
5394 (if (tree_single_nonzero_warnv_p (@0, NULL))
5395 { constant_boolean_node (cmp == NE_EXPR, type); }))
5397 /* (X & C) op (Y & C) into (X ^ Y) & C op 0. */
5399 (cmp (bit_and:cs @0 @2) (bit_and:cs @1 @2))
5400 (cmp (bit_and (bit_xor @0 @1) @2) { build_zero_cst (TREE_TYPE (@2)); })))
5402 /* (X < 0) != (Y < 0) into (X ^ Y) < 0.
5403 (X >= 0) != (Y >= 0) into (X ^ Y) < 0.
5404 (X < 0) == (Y < 0) into (X ^ Y) >= 0.
5405 (X >= 0) == (Y >= 0) into (X ^ Y) >= 0. */
5410 (cmp (sgncmp @0 integer_zerop@2) (sgncmp @1 integer_zerop))
5411 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
5412 && !TYPE_UNSIGNED (TREE_TYPE (@0))
5413 && types_match (@0, @1))
5414 (ncmp (bit_xor @0 @1) @2)))))
5415 /* (X < 0) == (Y >= 0) into (X ^ Y) < 0.
5416 (X < 0) != (Y >= 0) into (X ^ Y) >= 0. */
5420 (cmp:c (lt @0 integer_zerop@2) (ge @1 integer_zerop))
5421 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
5422 && !TYPE_UNSIGNED (TREE_TYPE (@0))
5423 && types_match (@0, @1))
5424 (ncmp (bit_xor @0 @1) @2))))
5426 /* If we have (A & C) == C where C is a power of 2, convert this into
5427 (A & C) != 0. Similarly for NE_EXPR. */
5431 (cmp (bit_and@2 @0 integer_pow2p@1) @1)
5432 (icmp @2 { build_zero_cst (TREE_TYPE (@0)); })))
5435 /* x < 0 ? ~y : y into (x >> (prec-1)) ^ y. */
5436 /* x >= 0 ? ~y : y into ~((x >> (prec-1)) ^ y). */
5438 (cond (cmp @0 integer_zerop) (bit_not @1) @1)
5439 (if (INTEGRAL_TYPE_P (type)
5440 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
5441 && !TYPE_UNSIGNED (TREE_TYPE (@0))
5442 && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (type))
5445 tree shifter = build_int_cst (integer_type_node, TYPE_PRECISION (type) - 1);
5447 (if (cmp == LT_EXPR)
5448 (bit_xor (convert (rshift @0 {shifter;})) @1)
5449 (bit_not (bit_xor (convert (rshift @0 {shifter;})) @1))))))
5450 /* x < 0 ? y : ~y into ~((x >> (prec-1)) ^ y). */
5451 /* x >= 0 ? y : ~y into (x >> (prec-1)) ^ y. */
5453 (cond (cmp @0 integer_zerop) @1 (bit_not @1))
5454 (if (INTEGRAL_TYPE_P (type)
5455 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
5456 && !TYPE_UNSIGNED (TREE_TYPE (@0))
5457 && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (type))
5460 tree shifter = build_int_cst (integer_type_node, TYPE_PRECISION (type) - 1);
5462 (if (cmp == GE_EXPR)
5463 (bit_xor (convert (rshift @0 {shifter;})) @1)
5464 (bit_not (bit_xor (convert (rshift @0 {shifter;})) @1)))))))
5466 /* If we have (A & C) != 0 ? D : 0 where C and D are powers of 2,
5467 convert this into a shift followed by ANDing with D. */
5470 (ne (bit_and @0 integer_pow2p@1) integer_zerop)
5471 INTEGER_CST@2 integer_zerop)
5472 (if (!POINTER_TYPE_P (type) && integer_pow2p (@2))
5474 int shift = (wi::exact_log2 (wi::to_wide (@2))
5475 - wi::exact_log2 (wi::to_wide (@1)));
5479 (lshift (convert @0) { build_int_cst (integer_type_node, shift); }) @2)
5481 (convert (rshift @0 { build_int_cst (integer_type_node, -shift); }))
5484 /* If we have (A & C) != 0 where C is the sign bit of A, convert
5485 this into A < 0. Similarly for (A & C) == 0 into A >= 0. */
5489 (cmp (bit_and (convert?@2 @0) integer_pow2p@1) integer_zerop)
5490 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
5491 && type_has_mode_precision_p (TREE_TYPE (@0))
5492 && element_precision (@2) >= element_precision (@0)
5493 && wi::only_sign_bit_p (wi::to_wide (@1), element_precision (@0)))
5494 (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
5495 (ncmp (convert:stype @0) { build_zero_cst (stype); })))))
5497 /* If we have A < 0 ? C : 0 where C is a power of 2, convert
5498 this into a right shift or sign extension followed by ANDing with C. */
5501 (lt @0 integer_zerop)
5502 INTEGER_CST@1 integer_zerop)
5503 (if (integer_pow2p (@1)
5504 && !TYPE_UNSIGNED (TREE_TYPE (@0)))
5506 int shift = element_precision (@0) - wi::exact_log2 (wi::to_wide (@1)) - 1;
5510 (convert (rshift @0 { build_int_cst (integer_type_node, shift); }))
5512 /* Otherwise ctype must be wider than TREE_TYPE (@0) and pure
5513 sign extension followed by AND with C will achieve the effect. */
5514 (bit_and (convert @0) @1)))))
5516 /* When the addresses are not directly of decls compare base and offset.
5517 This implements some remaining parts of fold_comparison address
5518 comparisons but still no complete part of it. Still it is good
5519 enough to make fold_stmt not regress when not dispatching to fold_binary. */
5520 (for cmp (simple_comparison)
5522 (cmp (convert1?@2 addr@0) (convert2? addr@1))
5525 poly_int64 off0, off1;
5527 int equal = address_compare (cmp, TREE_TYPE (@2), @0, @1, base0, base1,
5528 off0, off1, GENERIC);
5532 (if (cmp == EQ_EXPR && (known_eq (off0, off1) || known_ne (off0, off1)))
5533 { constant_boolean_node (known_eq (off0, off1), type); })
5534 (if (cmp == NE_EXPR && (known_eq (off0, off1) || known_ne (off0, off1)))
5535 { constant_boolean_node (known_ne (off0, off1), type); })
5536 (if (cmp == LT_EXPR && (known_lt (off0, off1) || known_ge (off0, off1)))
5537 { constant_boolean_node (known_lt (off0, off1), type); })
5538 (if (cmp == LE_EXPR && (known_le (off0, off1) || known_gt (off0, off1)))
5539 { constant_boolean_node (known_le (off0, off1), type); })
5540 (if (cmp == GE_EXPR && (known_ge (off0, off1) || known_lt (off0, off1)))
5541 { constant_boolean_node (known_ge (off0, off1), type); })
5542 (if (cmp == GT_EXPR && (known_gt (off0, off1) || known_le (off0, off1)))
5543 { constant_boolean_node (known_gt (off0, off1), type); }))
5546 (if (cmp == EQ_EXPR)
5547 { constant_boolean_node (false, type); })
5548 (if (cmp == NE_EXPR)
5549 { constant_boolean_node (true, type); })))))))
5551 /* Simplify pointer equality compares using PTA. */
5555 (if (POINTER_TYPE_P (TREE_TYPE (@0))
5556 && ptrs_compare_unequal (@0, @1))
5557 { constant_boolean_node (neeq != EQ_EXPR, type); })))
5559 /* PR70920: Transform (intptr_t)x eq/ne CST to x eq/ne (typeof x) CST.
5560 and (typeof ptr_cst) x eq/ne ptr_cst to x eq/ne (typeof x) CST.
5561 Disable the transform if either operand is pointer to function.
5562 This broke pr22051-2.c for arm where function pointer
5563 canonicalizaion is not wanted. */
5567 (cmp (convert @0) INTEGER_CST@1)
5568 (if (((POINTER_TYPE_P (TREE_TYPE (@0))
5569 && !FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@0)))
5570 && INTEGRAL_TYPE_P (TREE_TYPE (@1))
5571 /* Don't perform this optimization in GENERIC if @0 has reference
5572 type when sanitizing. See PR101210. */
5574 && TREE_CODE (TREE_TYPE (@0)) == REFERENCE_TYPE
5575 && (flag_sanitize & (SANITIZE_NULL | SANITIZE_ALIGNMENT))))
5576 || (INTEGRAL_TYPE_P (TREE_TYPE (@0))
5577 && POINTER_TYPE_P (TREE_TYPE (@1))
5578 && !FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@1)))))
5579 && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
5580 (cmp @0 (convert @1)))))
5582 /* Non-equality compare simplifications from fold_binary */
5583 (for cmp (lt gt le ge)
5584 /* Comparisons with the highest or lowest possible integer of
5585 the specified precision will have known values. */
5587 (cmp (convert?@2 @0) uniform_integer_cst_p@1)
5588 (if ((INTEGRAL_TYPE_P (TREE_TYPE (@1))
5589 || POINTER_TYPE_P (TREE_TYPE (@1))
5590 || VECTOR_INTEGER_TYPE_P (TREE_TYPE (@1)))
5591 && tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@0)))
5594 tree cst = uniform_integer_cst_p (@1);
5595 tree arg1_type = TREE_TYPE (cst);
5596 unsigned int prec = TYPE_PRECISION (arg1_type);
5597 wide_int max = wi::max_value (arg1_type);
5598 wide_int signed_max = wi::max_value (prec, SIGNED);
5599 wide_int min = wi::min_value (arg1_type);
5602 (if (wi::to_wide (cst) == max)
5604 (if (cmp == GT_EXPR)
5605 { constant_boolean_node (false, type); })
5606 (if (cmp == GE_EXPR)
5608 (if (cmp == LE_EXPR)
5609 { constant_boolean_node (true, type); })
5610 (if (cmp == LT_EXPR)
5612 (if (wi::to_wide (cst) == min)
5614 (if (cmp == LT_EXPR)
5615 { constant_boolean_node (false, type); })
5616 (if (cmp == LE_EXPR)
5618 (if (cmp == GE_EXPR)
5619 { constant_boolean_node (true, type); })
5620 (if (cmp == GT_EXPR)
5622 (if (wi::to_wide (cst) == max - 1)
5624 (if (cmp == GT_EXPR)
5625 (eq @2 { build_uniform_cst (TREE_TYPE (@1),
5626 wide_int_to_tree (TREE_TYPE (cst),
5629 (if (cmp == LE_EXPR)
5630 (ne @2 { build_uniform_cst (TREE_TYPE (@1),
5631 wide_int_to_tree (TREE_TYPE (cst),
5634 (if (wi::to_wide (cst) == min + 1)
5636 (if (cmp == GE_EXPR)
5637 (ne @2 { build_uniform_cst (TREE_TYPE (@1),
5638 wide_int_to_tree (TREE_TYPE (cst),
5641 (if (cmp == LT_EXPR)
5642 (eq @2 { build_uniform_cst (TREE_TYPE (@1),
5643 wide_int_to_tree (TREE_TYPE (cst),
5646 (if (wi::to_wide (cst) == signed_max
5647 && TYPE_UNSIGNED (arg1_type)
5648 /* We will flip the signedness of the comparison operator
5649 associated with the mode of @1, so the sign bit is
5650 specified by this mode. Check that @1 is the signed
5651 max associated with this sign bit. */
5652 && prec == GET_MODE_PRECISION (SCALAR_INT_TYPE_MODE (arg1_type))
5653 /* signed_type does not work on pointer types. */
5654 && INTEGRAL_TYPE_P (arg1_type))
5655 /* The following case also applies to X < signed_max+1
5656 and X >= signed_max+1 because previous transformations. */
5657 (if (cmp == LE_EXPR || cmp == GT_EXPR)
5658 (with { tree st = signed_type_for (TREE_TYPE (@1)); }
5660 (if (cst == @1 && cmp == LE_EXPR)
5661 (ge (convert:st @0) { build_zero_cst (st); }))
5662 (if (cst == @1 && cmp == GT_EXPR)
5663 (lt (convert:st @0) { build_zero_cst (st); }))
5664 (if (cmp == LE_EXPR)
5665 (ge (view_convert:st @0) { build_zero_cst (st); }))
5666 (if (cmp == GT_EXPR)
5667 (lt (view_convert:st @0) { build_zero_cst (st); })))))))))))
5669 (for cmp (unordered ordered unlt unle ungt unge uneq ltgt)
5670 /* If the second operand is NaN, the result is constant. */
5673 (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
5674 && (cmp != LTGT_EXPR || ! flag_trapping_math))
5675 { constant_boolean_node (cmp == ORDERED_EXPR || cmp == LTGT_EXPR
5676 ? false : true, type); })))
5678 /* Fold UNORDERED if either operand must be NaN, or neither can be. */
5682 (if (tree_expr_nan_p (@0) || tree_expr_nan_p (@1))
5683 { constant_boolean_node (true, type); })
5684 (if (!tree_expr_maybe_nan_p (@0) && !tree_expr_maybe_nan_p (@1))
5685 { constant_boolean_node (false, type); })))
5687 /* Fold ORDERED if either operand must be NaN, or neither can be. */
5691 (if (tree_expr_nan_p (@0) || tree_expr_nan_p (@1))
5692 { constant_boolean_node (false, type); })
5693 (if (!tree_expr_maybe_nan_p (@0) && !tree_expr_maybe_nan_p (@1))
5694 { constant_boolean_node (true, type); })))
5696 /* bool_var != 0 becomes bool_var. */
5698 (ne @0 integer_zerop)
5699 (if (TREE_CODE (TREE_TYPE (@0)) == BOOLEAN_TYPE
5700 && types_match (type, TREE_TYPE (@0)))
5702 /* bool_var == 1 becomes bool_var. */
5704 (eq @0 integer_onep)
5705 (if (TREE_CODE (TREE_TYPE (@0)) == BOOLEAN_TYPE
5706 && types_match (type, TREE_TYPE (@0)))
5709 bool_var == 0 becomes !bool_var or
5710 bool_var != 1 becomes !bool_var
5711 here because that only is good in assignment context as long
5712 as we require a tcc_comparison in GIMPLE_CONDs where we'd
5713 replace if (x == 0) with tem = ~x; if (tem != 0) which is
5714 clearly less optimal and which we'll transform again in forwprop. */
5716 /* When one argument is a constant, overflow detection can be simplified.
5717 Currently restricted to single use so as not to interfere too much with
5718 ADD_OVERFLOW detection in tree-ssa-math-opts.c.
5719 CONVERT?(CONVERT?(A) + CST) CMP A -> A CMP' CST' */
5720 (for cmp (lt le ge gt)
5723 (cmp:c (convert?@3 (plus@2 (convert?@4 @0) INTEGER_CST@1)) @0)
5724 (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@2))
5725 && types_match (TREE_TYPE (@0), TREE_TYPE (@3))
5726 && tree_nop_conversion_p (TREE_TYPE (@4), TREE_TYPE (@0))
5727 && wi::to_wide (@1) != 0
5730 unsigned int prec = TYPE_PRECISION (TREE_TYPE (@0));
5731 signop sign = TYPE_SIGN (TREE_TYPE (@0));
5733 (out @0 { wide_int_to_tree (TREE_TYPE (@0),
5734 wi::max_value (prec, sign)
5735 - wi::to_wide (@1)); })))))
5737 /* To detect overflow in unsigned A - B, A < B is simpler than A - B > A.
5738 However, the detection logic for SUB_OVERFLOW in tree-ssa-math-opts.c
5739 expects the long form, so we restrict the transformation for now. */
5742 (cmp:c (minus@2 @0 @1) @0)
5743 (if (single_use (@2)
5744 && ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
5745 && TYPE_UNSIGNED (TREE_TYPE (@0)))
5748 /* Optimize A - B + -1 >= A into B >= A for unsigned comparisons. */
5751 (cmp:c (plus (minus @0 @1) integer_minus_onep) @0)
5752 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
5753 && TYPE_UNSIGNED (TREE_TYPE (@0)))
5756 /* Testing for overflow is unnecessary if we already know the result. */
5761 (cmp:c (realpart (IFN_SUB_OVERFLOW@2 @0 @1)) @0)
5762 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
5763 && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
5764 (out (imagpart @2) { build_zero_cst (TREE_TYPE (@0)); }))))
5769 (cmp:c (realpart (IFN_ADD_OVERFLOW:c@2 @0 @1)) @0)
5770 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
5771 && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
5772 (out (imagpart @2) { build_zero_cst (TREE_TYPE (@0)); }))))
5774 /* For unsigned operands, -1 / B < A checks whether A * B would overflow.
5775 Simplify it to __builtin_mul_overflow (A, B, <unused>). */
5779 (cmp:c (trunc_div:s integer_all_onesp @1) @0)
5780 (if (TYPE_UNSIGNED (TREE_TYPE (@0)) && !VECTOR_TYPE_P (TREE_TYPE (@0)))
5781 (with { tree t = TREE_TYPE (@0), cpx = build_complex_type (t); }
5782 (out (imagpart (IFN_MUL_OVERFLOW:cpx @0 @1)) { build_zero_cst (t); })))))
5784 /* Similarly, for unsigned operands, (((type) A * B) >> prec) != 0 where type
5785 is at least twice as wide as type of A and B, simplify to
5786 __builtin_mul_overflow (A, B, <unused>). */
5789 (cmp (rshift (mult:s (convert@3 @0) (convert @1)) INTEGER_CST@2)
5791 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
5792 && INTEGRAL_TYPE_P (TREE_TYPE (@3))
5793 && TYPE_UNSIGNED (TREE_TYPE (@0))
5794 && (TYPE_PRECISION (TREE_TYPE (@3))
5795 >= 2 * TYPE_PRECISION (TREE_TYPE (@0)))
5796 && tree_fits_uhwi_p (@2)
5797 && tree_to_uhwi (@2) == TYPE_PRECISION (TREE_TYPE (@0))
5798 && types_match (@0, @1)
5799 && type_has_mode_precision_p (TREE_TYPE (@0))
5800 && (optab_handler (umulv4_optab, TYPE_MODE (TREE_TYPE (@0)))
5801 != CODE_FOR_nothing))
5802 (with { tree t = TREE_TYPE (@0), cpx = build_complex_type (t); }
5803 (cmp (imagpart (IFN_MUL_OVERFLOW:cpx @0 @1)) { build_zero_cst (t); })))))
5805 /* Demote operands of IFN_{ADD,SUB,MUL}_OVERFLOW. */
5806 (for ovf (IFN_ADD_OVERFLOW IFN_SUB_OVERFLOW IFN_MUL_OVERFLOW)
5808 (ovf (convert@2 @0) @1)
5809 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
5810 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
5811 && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
5812 && (!TYPE_UNSIGNED (TREE_TYPE (@2)) || TYPE_UNSIGNED (TREE_TYPE (@0))))
5815 (ovf @1 (convert@2 @0))
5816 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
5817 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
5818 && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
5819 && (!TYPE_UNSIGNED (TREE_TYPE (@2)) || TYPE_UNSIGNED (TREE_TYPE (@0))))
5822 /* Simplification of math builtins. These rules must all be optimizations
5823 as well as IL simplifications. If there is a possibility that the new
5824 form could be a pessimization, the rule should go in the canonicalization
5825 section that follows this one.
5827 Rules can generally go in this section if they satisfy one of
5830 - the rule describes an identity
5832 - the rule replaces calls with something as simple as addition or
5835 - the rule contains unary calls only and simplifies the surrounding
5836 arithmetic. (The idea here is to exclude non-unary calls in which
5837 one operand is constant and in which the call is known to be cheap
5838 when the operand has that value.) */
5840 (if (flag_unsafe_math_optimizations)
5841 /* Simplify sqrt(x) * sqrt(x) -> x. */
5843 (mult (SQRT_ALL@1 @0) @1)
5844 (if (!tree_expr_maybe_signaling_nan_p (@0))
5847 (for op (plus minus)
5848 /* Simplify (A / C) +- (B / C) -> (A +- B) / C. */
5852 (rdiv (op @0 @2) @1)))
5854 (for cmp (lt le gt ge)
5855 neg_cmp (gt ge lt le)
5856 /* Simplify (x * C1) cmp C2 -> x cmp (C2 / C1), where C1 != 0. */
5858 (cmp (mult @0 REAL_CST@1) REAL_CST@2)
5860 { tree tem = const_binop (RDIV_EXPR, type, @2, @1); }
5862 && !(REAL_VALUE_ISINF (TREE_REAL_CST (tem))
5863 || (real_zerop (tem) && !real_zerop (@1))))
5865 (if (real_less (&dconst0, TREE_REAL_CST_PTR (@1)))
5867 (if (real_less (TREE_REAL_CST_PTR (@1), &dconst0))
5868 (neg_cmp @0 { tem; })))))))
5870 /* Simplify sqrt(x) * sqrt(y) -> sqrt(x*y). */
5871 (for root (SQRT CBRT)
5873 (mult (root:s @0) (root:s @1))
5874 (root (mult @0 @1))))
5876 /* Simplify expN(x) * expN(y) -> expN(x+y). */
5877 (for exps (EXP EXP2 EXP10 POW10)
5879 (mult (exps:s @0) (exps:s @1))
5880 (exps (plus @0 @1))))
5882 /* Simplify a/root(b/c) into a*root(c/b). */
5883 (for root (SQRT CBRT)
5885 (rdiv @0 (root:s (rdiv:s @1 @2)))
5886 (mult @0 (root (rdiv @2 @1)))))
5888 /* Simplify x/expN(y) into x*expN(-y). */
5889 (for exps (EXP EXP2 EXP10 POW10)
5891 (rdiv @0 (exps:s @1))
5892 (mult @0 (exps (negate @1)))))
5894 (for logs (LOG LOG2 LOG10 LOG10)
5895 exps (EXP EXP2 EXP10 POW10)
5896 /* logN(expN(x)) -> x. */
5900 /* expN(logN(x)) -> x. */
5905 /* Optimize logN(func()) for various exponential functions. We
5906 want to determine the value "x" and the power "exponent" in
5907 order to transform logN(x**exponent) into exponent*logN(x). */
5908 (for logs (LOG LOG LOG LOG2 LOG2 LOG2 LOG10 LOG10)
5909 exps (EXP2 EXP10 POW10 EXP EXP10 POW10 EXP EXP2)
5912 (if (SCALAR_FLOAT_TYPE_P (type))
5918 /* Prepare to do logN(exp(exponent)) -> exponent*logN(e). */
5919 x = build_real_truncate (type, dconst_e ());
5922 /* Prepare to do logN(exp2(exponent)) -> exponent*logN(2). */
5923 x = build_real (type, dconst2);
5927 /* Prepare to do logN(exp10(exponent)) -> exponent*logN(10). */
5929 REAL_VALUE_TYPE dconst10;
5930 real_from_integer (&dconst10, VOIDmode, 10, SIGNED);
5931 x = build_real (type, dconst10);
5938 (mult (logs { x; }) @0)))))
5946 (if (SCALAR_FLOAT_TYPE_P (type))
5952 /* Prepare to do logN(sqrt(x)) -> 0.5*logN(x). */
5953 x = build_real (type, dconsthalf);
5956 /* Prepare to do logN(cbrt(x)) -> (1/3)*logN(x). */
5957 x = build_real_truncate (type, dconst_third ());
5963 (mult { x; } (logs @0))))))
5965 /* logN(pow(x,exponent)) -> exponent*logN(x). */
5966 (for logs (LOG LOG2 LOG10)
5970 (mult @1 (logs @0))))
5972 /* pow(C,x) -> exp(log(C)*x) if C > 0,
5973 or if C is a positive power of 2,
5974 pow(C,x) -> exp2(log2(C)*x). */
5982 (pows REAL_CST@0 @1)
5983 (if (real_compare (GT_EXPR, TREE_REAL_CST_PTR (@0), &dconst0)
5984 && real_isfinite (TREE_REAL_CST_PTR (@0))
5985 /* As libmvec doesn't have a vectorized exp2, defer optimizing
5986 the use_exp2 case until after vectorization. It seems actually
5987 beneficial for all constants to postpone this until later,
5988 because exp(log(C)*x), while faster, will have worse precision
5989 and if x folds into a constant too, that is unnecessary
5991 && canonicalize_math_after_vectorization_p ())
5993 const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (@0);
5994 bool use_exp2 = false;
5995 if (targetm.libc_has_function (function_c99_misc, TREE_TYPE (@0))
5996 && value->cl == rvc_normal)
5998 REAL_VALUE_TYPE frac_rvt = *value;
5999 SET_REAL_EXP (&frac_rvt, 1);
6000 if (real_equal (&frac_rvt, &dconst1))
6005 (if (optimize_pow_to_exp (@0, @1))
6006 (exps (mult (logs @0) @1)))
6007 (exp2s (mult (log2s @0) @1)))))))
6010 /* pow(C,x)*expN(y) -> expN(logN(C)*x+y) if C > 0. */
6012 exps (EXP EXP2 EXP10 POW10)
6013 logs (LOG LOG2 LOG10 LOG10)
6015 (mult:c (pows:s REAL_CST@0 @1) (exps:s @2))
6016 (if (real_compare (GT_EXPR, TREE_REAL_CST_PTR (@0), &dconst0)
6017 && real_isfinite (TREE_REAL_CST_PTR (@0)))
6018 (exps (plus (mult (logs @0) @1) @2)))))
6023 exps (EXP EXP2 EXP10 POW10)
6024 /* sqrt(expN(x)) -> expN(x*0.5). */
6027 (exps (mult @0 { build_real (type, dconsthalf); })))
6028 /* cbrt(expN(x)) -> expN(x/3). */
6031 (exps (mult @0 { build_real_truncate (type, dconst_third ()); })))
6032 /* pow(expN(x), y) -> expN(x*y). */
6035 (exps (mult @0 @1))))
6037 /* tan(atan(x)) -> x. */
6044 /* Simplify sin(atan(x)) -> x / sqrt(x*x + 1). */
6048 copysigns (COPYSIGN)
6053 REAL_VALUE_TYPE r_cst;
6054 build_sinatan_real (&r_cst, type);
6055 tree t_cst = build_real (type, r_cst);
6056 tree t_one = build_one_cst (type);
6058 (if (SCALAR_FLOAT_TYPE_P (type))
6059 (cond (lt (abs @0) { t_cst; })
6060 (rdiv @0 (sqrts (plus (mult @0 @0) { t_one; })))
6061 (copysigns { t_one; } @0))))))
6063 /* Simplify cos(atan(x)) -> 1 / sqrt(x*x + 1). */
6067 copysigns (COPYSIGN)
6072 REAL_VALUE_TYPE r_cst;
6073 build_sinatan_real (&r_cst, type);
6074 tree t_cst = build_real (type, r_cst);
6075 tree t_one = build_one_cst (type);
6076 tree t_zero = build_zero_cst (type);
6078 (if (SCALAR_FLOAT_TYPE_P (type))
6079 (cond (lt (abs @0) { t_cst; })
6080 (rdiv { t_one; } (sqrts (plus (mult @0 @0) { t_one; })))
6081 (copysigns { t_zero; } @0))))))
6083 (if (!flag_errno_math)
6084 /* Simplify sinh(atanh(x)) -> x / sqrt((1 - x)*(1 + x)). */
6089 (sinhs (atanhs:s @0))
6090 (with { tree t_one = build_one_cst (type); }
6091 (rdiv @0 (sqrts (mult (minus { t_one; } @0) (plus { t_one; } @0)))))))
6093 /* Simplify cosh(atanh(x)) -> 1 / sqrt((1 - x)*(1 + x)) */
6098 (coshs (atanhs:s @0))
6099 (with { tree t_one = build_one_cst (type); }
6100 (rdiv { t_one; } (sqrts (mult (minus { t_one; } @0) (plus { t_one; } @0))))))))
6102 /* cabs(x+0i) or cabs(0+xi) -> abs(x). */
6104 (CABS (complex:C @0 real_zerop@1))
6107 /* trunc(trunc(x)) -> trunc(x), etc. */
6108 (for fns (TRUNC_ALL FLOOR_ALL CEIL_ALL ROUND_ALL NEARBYINT_ALL RINT_ALL)
6112 /* f(x) -> x if x is integer valued and f does nothing for such values. */
6113 (for fns (TRUNC_ALL FLOOR_ALL CEIL_ALL ROUND_ALL NEARBYINT_ALL RINT_ALL)
6115 (fns integer_valued_real_p@0)
6118 /* hypot(x,0) and hypot(0,x) -> abs(x). */
6120 (HYPOT:c @0 real_zerop@1)
6123 /* pow(1,x) -> 1. */
6125 (POW real_onep@0 @1)
6129 /* copysign(x,x) -> x. */
6130 (COPYSIGN_ALL @0 @0)
6134 /* copysign(x,-x) -> -x. */
6135 (COPYSIGN_ALL @0 (negate@1 @0))
6139 /* copysign(x,y) -> fabs(x) if y is nonnegative. */
6140 (COPYSIGN_ALL @0 tree_expr_nonnegative_p@1)
6143 (for scale (LDEXP SCALBN SCALBLN)
6144 /* ldexp(0, x) -> 0. */
6146 (scale real_zerop@0 @1)
6148 /* ldexp(x, 0) -> x. */
6150 (scale @0 integer_zerop@1)
6152 /* ldexp(x, y) -> x if x is +-Inf or NaN. */
6154 (scale REAL_CST@0 @1)
6155 (if (!real_isfinite (TREE_REAL_CST_PTR (@0)))
6158 /* Canonicalization of sequences of math builtins. These rules represent
6159 IL simplifications but are not necessarily optimizations.
6161 The sincos pass is responsible for picking "optimal" implementations
6162 of math builtins, which may be more complicated and can sometimes go
6163 the other way, e.g. converting pow into a sequence of sqrts.
6164 We only want to do these canonicalizations before the pass has run. */
6166 (if (flag_unsafe_math_optimizations && canonicalize_math_p ())
6167 /* Simplify tan(x) * cos(x) -> sin(x). */
6169 (mult:c (TAN:s @0) (COS:s @0))
6172 /* Simplify x * pow(x,c) -> pow(x,c+1). */
6174 (mult:c @0 (POW:s @0 REAL_CST@1))
6175 (if (!TREE_OVERFLOW (@1))
6176 (POW @0 (plus @1 { build_one_cst (type); }))))
6178 /* Simplify sin(x) / cos(x) -> tan(x). */
6180 (rdiv (SIN:s @0) (COS:s @0))
6183 /* Simplify sinh(x) / cosh(x) -> tanh(x). */
6185 (rdiv (SINH:s @0) (COSH:s @0))
6188 /* Simplify tanh (x) / sinh (x) -> 1.0 / cosh (x). */
6190 (rdiv (TANH:s @0) (SINH:s @0))
6191 (rdiv {build_one_cst (type);} (COSH @0)))
6193 /* Simplify cos(x) / sin(x) -> 1 / tan(x). */
6195 (rdiv (COS:s @0) (SIN:s @0))
6196 (rdiv { build_one_cst (type); } (TAN @0)))
6198 /* Simplify sin(x) / tan(x) -> cos(x). */
6200 (rdiv (SIN:s @0) (TAN:s @0))
6201 (if (! HONOR_NANS (@0)
6202 && ! HONOR_INFINITIES (@0))
6205 /* Simplify tan(x) / sin(x) -> 1.0 / cos(x). */
6207 (rdiv (TAN:s @0) (SIN:s @0))
6208 (if (! HONOR_NANS (@0)
6209 && ! HONOR_INFINITIES (@0))
6210 (rdiv { build_one_cst (type); } (COS @0))))
6212 /* Simplify pow(x,y) * pow(x,z) -> pow(x,y+z). */
6214 (mult (POW:s @0 @1) (POW:s @0 @2))
6215 (POW @0 (plus @1 @2)))
6217 /* Simplify pow(x,y) * pow(z,y) -> pow(x*z,y). */
6219 (mult (POW:s @0 @1) (POW:s @2 @1))
6220 (POW (mult @0 @2) @1))
6222 /* Simplify powi(x,y) * powi(z,y) -> powi(x*z,y). */
6224 (mult (POWI:s @0 @1) (POWI:s @2 @1))
6225 (POWI (mult @0 @2) @1))
6227 /* Simplify pow(x,c) / x -> pow(x,c-1). */
6229 (rdiv (POW:s @0 REAL_CST@1) @0)
6230 (if (!TREE_OVERFLOW (@1))
6231 (POW @0 (minus @1 { build_one_cst (type); }))))
6233 /* Simplify x / pow (y,z) -> x * pow(y,-z). */
6235 (rdiv @0 (POW:s @1 @2))
6236 (mult @0 (POW @1 (negate @2))))
6241 /* sqrt(sqrt(x)) -> pow(x,1/4). */
6244 (pows @0 { build_real (type, dconst_quarter ()); }))
6245 /* sqrt(cbrt(x)) -> pow(x,1/6). */
6248 (pows @0 { build_real_truncate (type, dconst_sixth ()); }))
6249 /* cbrt(sqrt(x)) -> pow(x,1/6). */
6252 (pows @0 { build_real_truncate (type, dconst_sixth ()); }))
6253 /* cbrt(cbrt(x)) -> pow(x,1/9), iff x is nonnegative. */
6255 (cbrts (cbrts tree_expr_nonnegative_p@0))
6256 (pows @0 { build_real_truncate (type, dconst_ninth ()); }))
6257 /* sqrt(pow(x,y)) -> pow(|x|,y*0.5). */
6259 (sqrts (pows @0 @1))
6260 (pows (abs @0) (mult @1 { build_real (type, dconsthalf); })))
6261 /* cbrt(pow(x,y)) -> pow(x,y/3), iff x is nonnegative. */
6263 (cbrts (pows tree_expr_nonnegative_p@0 @1))
6264 (pows @0 (mult @1 { build_real_truncate (type, dconst_third ()); })))
6265 /* pow(sqrt(x),y) -> pow(x,y*0.5). */
6267 (pows (sqrts @0) @1)
6268 (pows @0 (mult @1 { build_real (type, dconsthalf); })))
6269 /* pow(cbrt(x),y) -> pow(x,y/3) iff x is nonnegative. */
6271 (pows (cbrts tree_expr_nonnegative_p@0) @1)
6272 (pows @0 (mult @1 { build_real_truncate (type, dconst_third ()); })))
6273 /* pow(pow(x,y),z) -> pow(x,y*z) iff x is nonnegative. */
6275 (pows (pows tree_expr_nonnegative_p@0 @1) @2)
6276 (pows @0 (mult @1 @2))))
6278 /* cabs(x+xi) -> fabs(x)*sqrt(2). */
6280 (CABS (complex @0 @0))
6281 (mult (abs @0) { build_real_truncate (type, dconst_sqrt2 ()); }))
6283 /* hypot(x,x) -> fabs(x)*sqrt(2). */
6286 (mult (abs @0) { build_real_truncate (type, dconst_sqrt2 ()); }))
6288 /* cexp(x+yi) -> exp(x)*cexpi(y). */
6293 (cexps compositional_complex@0)
6294 (if (targetm.libc_has_function (function_c99_math_complex, TREE_TYPE (@0)))
6296 (mult (exps@1 (realpart @0)) (realpart (cexpis:type@2 (imagpart @0))))
6297 (mult @1 (imagpart @2)))))))
6299 (if (canonicalize_math_p ())
6300 /* floor(x) -> trunc(x) if x is nonnegative. */
6301 (for floors (FLOOR_ALL)
6304 (floors tree_expr_nonnegative_p@0)
6307 (match double_value_p
6309 (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == double_type_node)))
6310 (for froms (BUILT_IN_TRUNCL
6322 /* truncl(extend(x)) -> extend(trunc(x)), etc., if x is a double. */
6323 (if (optimize && canonicalize_math_p ())
6325 (froms (convert double_value_p@0))
6326 (convert (tos @0)))))
6328 (match float_value_p
6330 (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == float_type_node)))
6331 (for froms (BUILT_IN_TRUNCL BUILT_IN_TRUNC
6332 BUILT_IN_FLOORL BUILT_IN_FLOOR
6333 BUILT_IN_CEILL BUILT_IN_CEIL
6334 BUILT_IN_ROUNDL BUILT_IN_ROUND
6335 BUILT_IN_NEARBYINTL BUILT_IN_NEARBYINT
6336 BUILT_IN_RINTL BUILT_IN_RINT)
6337 tos (BUILT_IN_TRUNCF BUILT_IN_TRUNCF
6338 BUILT_IN_FLOORF BUILT_IN_FLOORF
6339 BUILT_IN_CEILF BUILT_IN_CEILF
6340 BUILT_IN_ROUNDF BUILT_IN_ROUNDF
6341 BUILT_IN_NEARBYINTF BUILT_IN_NEARBYINTF
6342 BUILT_IN_RINTF BUILT_IN_RINTF)
6343 /* truncl(extend(x)) and trunc(extend(x)) -> extend(truncf(x)), etc.,
6345 (if (optimize && canonicalize_math_p ()
6346 && targetm.libc_has_function (function_c99_misc, NULL_TREE))
6348 (froms (convert float_value_p@0))
6349 (convert (tos @0)))))
6352 (match float16_value_p
6354 (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == float16_type_node)))
6355 (for froms (BUILT_IN_TRUNCL BUILT_IN_TRUNC BUILT_IN_TRUNCF
6356 BUILT_IN_FLOORL BUILT_IN_FLOOR BUILT_IN_FLOORF
6357 BUILT_IN_CEILL BUILT_IN_CEIL BUILT_IN_CEILF
6358 BUILT_IN_ROUNDEVENL BUILT_IN_ROUNDEVEN BUILT_IN_ROUNDEVENF
6359 BUILT_IN_ROUNDL BUILT_IN_ROUND BUILT_IN_ROUNDF
6360 BUILT_IN_NEARBYINTL BUILT_IN_NEARBYINT BUILT_IN_NEARBYINTF
6361 BUILT_IN_RINTL BUILT_IN_RINT BUILT_IN_RINTF
6362 BUILT_IN_SQRTL BUILT_IN_SQRT BUILT_IN_SQRTF)
6363 tos (IFN_TRUNC IFN_TRUNC IFN_TRUNC
6364 IFN_FLOOR IFN_FLOOR IFN_FLOOR
6365 IFN_CEIL IFN_CEIL IFN_CEIL
6366 IFN_ROUNDEVEN IFN_ROUNDEVEN IFN_ROUNDEVEN
6367 IFN_ROUND IFN_ROUND IFN_ROUND
6368 IFN_NEARBYINT IFN_NEARBYINT IFN_NEARBYINT
6369 IFN_RINT IFN_RINT IFN_RINT
6370 IFN_SQRT IFN_SQRT IFN_SQRT)
6371 /* (_Float16) round ((doube) x) -> __built_in_roundf16 (x), etc.,
6372 if x is a _Float16. */
6374 (convert (froms (convert float16_value_p@0)))
6376 && types_match (type, TREE_TYPE (@0))
6377 && direct_internal_fn_supported_p (as_internal_fn (tos),
6378 type, OPTIMIZE_FOR_BOTH))
6381 /* Simplify (trunc)copysign ((extend)x, (extend)y) to copysignf (x, y),
6382 x,y is float value, similar for _Float16/double. */
6383 (for copysigns (COPYSIGN_ALL)
6385 (convert (copysigns (convert@2 @0) (convert @1)))
6387 && !HONOR_SNANS (@2)
6388 && types_match (type, TREE_TYPE (@0))
6389 && types_match (type, TREE_TYPE (@1))
6390 && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (@2))
6391 && direct_internal_fn_supported_p (IFN_COPYSIGN,
6392 type, OPTIMIZE_FOR_BOTH))
6393 (IFN_COPYSIGN @0 @1))))
6395 (for froms (BUILT_IN_FMAF BUILT_IN_FMA BUILT_IN_FMAL)
6396 tos (IFN_FMA IFN_FMA IFN_FMA)
6398 (convert (froms (convert@3 @0) (convert @1) (convert @2)))
6399 (if (flag_unsafe_math_optimizations
6401 && FLOAT_TYPE_P (type)
6402 && FLOAT_TYPE_P (TREE_TYPE (@3))
6403 && types_match (type, TREE_TYPE (@0))
6404 && types_match (type, TREE_TYPE (@1))
6405 && types_match (type, TREE_TYPE (@2))
6406 && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (@3))
6407 && direct_internal_fn_supported_p (as_internal_fn (tos),
6408 type, OPTIMIZE_FOR_BOTH))
6411 (for maxmin (max min)
6413 (convert (maxmin (convert@2 @0) (convert @1)))
6415 && FLOAT_TYPE_P (type)
6416 && FLOAT_TYPE_P (TREE_TYPE (@2))
6417 && types_match (type, TREE_TYPE (@0))
6418 && types_match (type, TREE_TYPE (@1))
6419 && element_precision (type) < element_precision (TREE_TYPE (@2)))
6423 (for froms (XFLOORL XCEILL XROUNDL XRINTL)
6424 tos (XFLOOR XCEIL XROUND XRINT)
6425 /* llfloorl(extend(x)) -> llfloor(x), etc., if x is a double. */
6426 (if (optimize && canonicalize_math_p ())
6428 (froms (convert double_value_p@0))
6431 (for froms (XFLOORL XCEILL XROUNDL XRINTL
6432 XFLOOR XCEIL XROUND XRINT)
6433 tos (XFLOORF XCEILF XROUNDF XRINTF)
6434 /* llfloorl(extend(x)) and llfloor(extend(x)) -> llfloorf(x), etc.,
6436 (if (optimize && canonicalize_math_p ())
6438 (froms (convert float_value_p@0))
6441 (if (canonicalize_math_p ())
6442 /* xfloor(x) -> fix_trunc(x) if x is nonnegative. */
6443 (for floors (IFLOOR LFLOOR LLFLOOR)
6445 (floors tree_expr_nonnegative_p@0)
6448 (if (canonicalize_math_p ())
6449 /* xfloor(x) -> fix_trunc(x), etc., if x is integer valued. */
6450 (for fns (IFLOOR LFLOOR LLFLOOR
6452 IROUND LROUND LLROUND)
6454 (fns integer_valued_real_p@0)
6456 (if (!flag_errno_math)
6457 /* xrint(x) -> fix_trunc(x), etc., if x is integer valued. */
6458 (for rints (IRINT LRINT LLRINT)
6460 (rints integer_valued_real_p@0)
6463 (if (canonicalize_math_p ())
6464 (for ifn (IFLOOR ICEIL IROUND IRINT)
6465 lfn (LFLOOR LCEIL LROUND LRINT)
6466 llfn (LLFLOOR LLCEIL LLROUND LLRINT)
6467 /* Canonicalize iround (x) to lround (x) on ILP32 targets where
6468 sizeof (int) == sizeof (long). */
6469 (if (TYPE_PRECISION (integer_type_node)
6470 == TYPE_PRECISION (long_integer_type_node))
6473 (lfn:long_integer_type_node @0)))
6474 /* Canonicalize llround (x) to lround (x) on LP64 targets where
6475 sizeof (long long) == sizeof (long). */
6476 (if (TYPE_PRECISION (long_long_integer_type_node)
6477 == TYPE_PRECISION (long_integer_type_node))
6480 (lfn:long_integer_type_node @0)))))
6482 /* cproj(x) -> x if we're ignoring infinities. */
6485 (if (!HONOR_INFINITIES (type))
6488 /* If the real part is inf and the imag part is known to be
6489 nonnegative, return (inf + 0i). */
6491 (CPROJ (complex REAL_CST@0 tree_expr_nonnegative_p@1))
6492 (if (real_isinf (TREE_REAL_CST_PTR (@0)))
6493 { build_complex_inf (type, false); }))
6495 /* If the imag part is inf, return (inf+I*copysign(0,imag)). */
6497 (CPROJ (complex @0 REAL_CST@1))
6498 (if (real_isinf (TREE_REAL_CST_PTR (@1)))
6499 { build_complex_inf (type, TREE_REAL_CST_PTR (@1)->sign); }))
6505 (pows @0 REAL_CST@1)
6507 const REAL_VALUE_TYPE *value = TREE_REAL_CST_PTR (@1);
6508 REAL_VALUE_TYPE tmp;
6511 /* pow(x,0) -> 1. */
6512 (if (real_equal (value, &dconst0))
6513 { build_real (type, dconst1); })
6514 /* pow(x,1) -> x. */
6515 (if (real_equal (value, &dconst1))
6517 /* pow(x,-1) -> 1/x. */
6518 (if (real_equal (value, &dconstm1))
6519 (rdiv { build_real (type, dconst1); } @0))
6520 /* pow(x,0.5) -> sqrt(x). */
6521 (if (flag_unsafe_math_optimizations
6522 && canonicalize_math_p ()
6523 && real_equal (value, &dconsthalf))
6525 /* pow(x,1/3) -> cbrt(x). */
6526 (if (flag_unsafe_math_optimizations
6527 && canonicalize_math_p ()
6528 && (tmp = real_value_truncate (TYPE_MODE (type), dconst_third ()),
6529 real_equal (value, &tmp)))
6532 /* powi(1,x) -> 1. */
6534 (POWI real_onep@0 @1)
6538 (POWI @0 INTEGER_CST@1)
6540 /* powi(x,0) -> 1. */
6541 (if (wi::to_wide (@1) == 0)
6542 { build_real (type, dconst1); })
6543 /* powi(x,1) -> x. */
6544 (if (wi::to_wide (@1) == 1)
6546 /* powi(x,-1) -> 1/x. */
6547 (if (wi::to_wide (@1) == -1)
6548 (rdiv { build_real (type, dconst1); } @0))))
6550 /* Narrowing of arithmetic and logical operations.
6552 These are conceptually similar to the transformations performed for
6553 the C/C++ front-ends by shorten_binary_op and shorten_compare. Long
6554 term we want to move all that code out of the front-ends into here. */
6556 /* Convert (outertype)((innertype0)a+(innertype1)b)
6557 into ((newtype)a+(newtype)b) where newtype
6558 is the widest mode from all of these. */
6559 (for op (plus minus mult rdiv)
6561 (convert (op:s@0 (convert1?@3 @1) (convert2?@4 @2)))
6562 /* If we have a narrowing conversion of an arithmetic operation where
6563 both operands are widening conversions from the same type as the outer
6564 narrowing conversion. Then convert the innermost operands to a
6565 suitable unsigned type (to avoid introducing undefined behavior),
6566 perform the operation and convert the result to the desired type. */
6567 (if (INTEGRAL_TYPE_P (type)
6570 /* We check for type compatibility between @0 and @1 below,
6571 so there's no need to check that @2/@4 are integral types. */
6572 && INTEGRAL_TYPE_P (TREE_TYPE (@1))
6573 && INTEGRAL_TYPE_P (TREE_TYPE (@3))
6574 /* The precision of the type of each operand must match the
6575 precision of the mode of each operand, similarly for the
6577 && type_has_mode_precision_p (TREE_TYPE (@1))
6578 && type_has_mode_precision_p (TREE_TYPE (@2))
6579 && type_has_mode_precision_p (type)
6580 /* The inner conversion must be a widening conversion. */
6581 && TYPE_PRECISION (TREE_TYPE (@3)) > TYPE_PRECISION (TREE_TYPE (@1))
6582 && types_match (@1, type)
6583 && (types_match (@1, @2)
6584 /* Or the second operand is const integer or converted const
6585 integer from valueize. */
6586 || poly_int_tree_p (@4)))
6587 (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
6588 (op @1 (convert @2))
6589 (with { tree utype = unsigned_type_for (TREE_TYPE (@1)); }
6590 (convert (op (convert:utype @1)
6591 (convert:utype @2)))))
6592 (if (FLOAT_TYPE_P (type)
6593 && DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0))
6594 == DECIMAL_FLOAT_TYPE_P (type))
6595 (with { tree arg0 = strip_float_extensions (@1);
6596 tree arg1 = strip_float_extensions (@2);
6597 tree itype = TREE_TYPE (@0);
6598 tree ty1 = TREE_TYPE (arg0);
6599 tree ty2 = TREE_TYPE (arg1);
6600 enum tree_code code = TREE_CODE (itype); }
6601 (if (FLOAT_TYPE_P (ty1)
6602 && FLOAT_TYPE_P (ty2))
6603 (with { tree newtype = type;
6604 if (TYPE_MODE (ty1) == SDmode
6605 || TYPE_MODE (ty2) == SDmode
6606 || TYPE_MODE (type) == SDmode)
6607 newtype = dfloat32_type_node;
6608 if (TYPE_MODE (ty1) == DDmode
6609 || TYPE_MODE (ty2) == DDmode
6610 || TYPE_MODE (type) == DDmode)
6611 newtype = dfloat64_type_node;
6612 if (TYPE_MODE (ty1) == TDmode
6613 || TYPE_MODE (ty2) == TDmode
6614 || TYPE_MODE (type) == TDmode)
6615 newtype = dfloat128_type_node; }
6616 (if ((newtype == dfloat32_type_node
6617 || newtype == dfloat64_type_node
6618 || newtype == dfloat128_type_node)
6620 && types_match (newtype, type))
6621 (op (convert:newtype @1) (convert:newtype @2))
6622 (with { if (TYPE_PRECISION (ty1) > TYPE_PRECISION (newtype))
6624 if (TYPE_PRECISION (ty2) > TYPE_PRECISION (newtype))
6626 /* Sometimes this transformation is safe (cannot
6627 change results through affecting double rounding
6628 cases) and sometimes it is not. If NEWTYPE is
6629 wider than TYPE, e.g. (float)((long double)double
6630 + (long double)double) converted to
6631 (float)(double + double), the transformation is
6632 unsafe regardless of the details of the types
6633 involved; double rounding can arise if the result
6634 of NEWTYPE arithmetic is a NEWTYPE value half way
6635 between two representable TYPE values but the
6636 exact value is sufficiently different (in the
6637 right direction) for this difference to be
6638 visible in ITYPE arithmetic. If NEWTYPE is the
6639 same as TYPE, however, the transformation may be
6640 safe depending on the types involved: it is safe
6641 if the ITYPE has strictly more than twice as many
6642 mantissa bits as TYPE, can represent infinities
6643 and NaNs if the TYPE can, and has sufficient
6644 exponent range for the product or ratio of two
6645 values representable in the TYPE to be within the
6646 range of normal values of ITYPE. */
6647 (if (TYPE_PRECISION (newtype) < TYPE_PRECISION (itype)
6648 && (flag_unsafe_math_optimizations
6649 || (TYPE_PRECISION (newtype) == TYPE_PRECISION (type)
6650 && real_can_shorten_arithmetic (TYPE_MODE (itype),
6652 && !excess_precision_type (newtype)))
6653 && !types_match (itype, newtype))
6654 (convert:type (op (convert:newtype @1)
6655 (convert:newtype @2)))
6660 /* This is another case of narrowing, specifically when there's an outer
6661 BIT_AND_EXPR which masks off bits outside the type of the innermost
6662 operands. Like the previous case we have to convert the operands
6663 to unsigned types to avoid introducing undefined behavior for the
6664 arithmetic operation. */
6665 (for op (minus plus)
6667 (bit_and (op:s (convert@2 @0) (convert@3 @1)) INTEGER_CST@4)
6668 (if (INTEGRAL_TYPE_P (type)
6669 /* We check for type compatibility between @0 and @1 below,
6670 so there's no need to check that @1/@3 are integral types. */
6671 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
6672 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
6673 /* The precision of the type of each operand must match the
6674 precision of the mode of each operand, similarly for the
6676 && type_has_mode_precision_p (TREE_TYPE (@0))
6677 && type_has_mode_precision_p (TREE_TYPE (@1))
6678 && type_has_mode_precision_p (type)
6679 /* The inner conversion must be a widening conversion. */
6680 && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
6681 && types_match (@0, @1)
6682 && (tree_int_cst_min_precision (@4, TYPE_SIGN (TREE_TYPE (@0)))
6683 <= TYPE_PRECISION (TREE_TYPE (@0)))
6684 && (wi::to_wide (@4)
6685 & wi::mask (TYPE_PRECISION (TREE_TYPE (@0)),
6686 true, TYPE_PRECISION (type))) == 0)
6687 (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
6688 (with { tree ntype = TREE_TYPE (@0); }
6689 (convert (bit_and (op @0 @1) (convert:ntype @4))))
6690 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
6691 (convert (bit_and (op (convert:utype @0) (convert:utype @1))
6692 (convert:utype @4))))))))
6694 /* Transform (@0 < @1 and @0 < @2) to use min,
6695 (@0 > @1 and @0 > @2) to use max */
6696 (for logic (bit_and bit_and bit_and bit_and bit_ior bit_ior bit_ior bit_ior)
6697 op (lt le gt ge lt le gt ge )
6698 ext (min min max max max max min min )
6700 (logic (op:cs @0 @1) (op:cs @0 @2))
6701 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6702 && TREE_CODE (@0) != INTEGER_CST)
6703 (op @0 (ext @1 @2)))))
6706 /* signbit(x) -> 0 if x is nonnegative. */
6707 (SIGNBIT tree_expr_nonnegative_p@0)
6708 { integer_zero_node; })
6711 /* signbit(x) -> x<0 if x doesn't have signed zeros. */
6713 (if (!HONOR_SIGNED_ZEROS (@0))
6714 (convert (lt @0 { build_real (TREE_TYPE (@0), dconst0); }))))
6716 /* Transform comparisons of the form X +- C1 CMP C2 to X CMP C2 -+ C1. */
6718 (for op (plus minus)
6721 (cmp (op@3 @0 INTEGER_CST@1) INTEGER_CST@2)
6722 (if (!TREE_OVERFLOW (@1) && !TREE_OVERFLOW (@2)
6723 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0))
6724 && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@0))
6725 && !TYPE_SATURATING (TREE_TYPE (@0)))
6726 (with { tree res = int_const_binop (rop, @2, @1); }
6727 (if (TREE_OVERFLOW (res)
6728 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
6729 { constant_boolean_node (cmp == NE_EXPR, type); }
6730 (if (single_use (@3))
6731 (cmp @0 { TREE_OVERFLOW (res)
6732 ? drop_tree_overflow (res) : res; }))))))))
6733 (for cmp (lt le gt ge)
6734 (for op (plus minus)
6737 (cmp (op@3 @0 INTEGER_CST@1) INTEGER_CST@2)
6738 (if (!TREE_OVERFLOW (@1) && !TREE_OVERFLOW (@2)
6739 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
6740 (with { tree res = int_const_binop (rop, @2, @1); }
6741 (if (TREE_OVERFLOW (res))
6743 fold_overflow_warning (("assuming signed overflow does not occur "
6744 "when simplifying conditional to constant"),
6745 WARN_STRICT_OVERFLOW_CONDITIONAL);
6746 bool less = cmp == LE_EXPR || cmp == LT_EXPR;
6747 /* wi::ges_p (@2, 0) should be sufficient for a signed type. */
6748 bool ovf_high = wi::lt_p (wi::to_wide (@1), 0,
6749 TYPE_SIGN (TREE_TYPE (@1)))
6750 != (op == MINUS_EXPR);
6751 constant_boolean_node (less == ovf_high, type);
6753 (if (single_use (@3))
6756 fold_overflow_warning (("assuming signed overflow does not occur "
6757 "when changing X +- C1 cmp C2 to "
6759 WARN_STRICT_OVERFLOW_COMPARISON);
6761 (cmp @0 { res; })))))))))
6763 /* Canonicalizations of BIT_FIELD_REFs. */
6766 (BIT_FIELD_REF (BIT_FIELD_REF @0 @1 @2) @3 @4)
6767 (BIT_FIELD_REF @0 @3 { const_binop (PLUS_EXPR, bitsizetype, @2, @4); }))
6770 (BIT_FIELD_REF (view_convert @0) @1 @2)
6771 (BIT_FIELD_REF @0 @1 @2))
6774 (BIT_FIELD_REF @0 @1 integer_zerop)
6775 (if (tree_int_cst_equal (@1, TYPE_SIZE (TREE_TYPE (@0))))
6779 (BIT_FIELD_REF @0 @1 @2)
6781 (if (TREE_CODE (TREE_TYPE (@0)) == COMPLEX_TYPE
6782 && tree_int_cst_equal (@1, TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))))
6784 (if (integer_zerop (@2))
6785 (view_convert (realpart @0)))
6786 (if (tree_int_cst_equal (@2, TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))))
6787 (view_convert (imagpart @0)))))
6788 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6789 && INTEGRAL_TYPE_P (type)
6790 /* On GIMPLE this should only apply to register arguments. */
6791 && (! GIMPLE || is_gimple_reg (@0))
6792 /* A bit-field-ref that referenced the full argument can be stripped. */
6793 && ((compare_tree_int (@1, TYPE_PRECISION (TREE_TYPE (@0))) == 0
6794 && integer_zerop (@2))
6795 /* Low-parts can be reduced to integral conversions.
6796 ??? The following doesn't work for PDP endian. */
6797 || (BYTES_BIG_ENDIAN == WORDS_BIG_ENDIAN
6798 /* But only do this after vectorization. */
6799 && canonicalize_math_after_vectorization_p ()
6800 /* Don't even think about BITS_BIG_ENDIAN. */
6801 && TYPE_PRECISION (TREE_TYPE (@0)) % BITS_PER_UNIT == 0
6802 && TYPE_PRECISION (type) % BITS_PER_UNIT == 0
6803 && compare_tree_int (@2, (BYTES_BIG_ENDIAN
6804 ? (TYPE_PRECISION (TREE_TYPE (@0))
6805 - TYPE_PRECISION (type))
6809 /* Simplify vector extracts. */
6812 (BIT_FIELD_REF CONSTRUCTOR@0 @1 @2)
6813 (if (VECTOR_TYPE_P (TREE_TYPE (@0))
6814 && tree_fits_uhwi_p (TYPE_SIZE (type))
6815 && ((tree_to_uhwi (TYPE_SIZE (type))
6816 == tree_to_uhwi (TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))))
6817 || (VECTOR_TYPE_P (type)
6818 && (tree_to_uhwi (TYPE_SIZE (TREE_TYPE (type)))
6819 == tree_to_uhwi (TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0))))))))
6822 tree ctor = (TREE_CODE (@0) == SSA_NAME
6823 ? gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0)) : @0);
6824 tree eltype = TREE_TYPE (TREE_TYPE (ctor));
6825 unsigned HOST_WIDE_INT width = tree_to_uhwi (TYPE_SIZE (eltype));
6826 unsigned HOST_WIDE_INT n = tree_to_uhwi (@1);
6827 unsigned HOST_WIDE_INT idx = tree_to_uhwi (@2);
6830 && (idx % width) == 0
6832 && known_le ((idx + n) / width,
6833 TYPE_VECTOR_SUBPARTS (TREE_TYPE (ctor))))
6838 /* Constructor elements can be subvectors. */
6840 if (CONSTRUCTOR_NELTS (ctor) != 0)
6842 tree cons_elem = TREE_TYPE (CONSTRUCTOR_ELT (ctor, 0)->value);
6843 if (TREE_CODE (cons_elem) == VECTOR_TYPE)
6844 k = TYPE_VECTOR_SUBPARTS (cons_elem);
6846 unsigned HOST_WIDE_INT elt, count, const_k;
6849 /* We keep an exact subset of the constructor elements. */
6850 (if (multiple_p (idx, k, &elt) && multiple_p (n, k, &count))
6851 (if (CONSTRUCTOR_NELTS (ctor) == 0)
6852 { build_zero_cst (type); }
6854 (if (elt < CONSTRUCTOR_NELTS (ctor))
6855 (view_convert { CONSTRUCTOR_ELT (ctor, elt)->value; })
6856 { build_zero_cst (type); })
6857 /* We don't want to emit new CTORs unless the old one goes away.
6858 ??? Eventually allow this if the CTOR ends up constant or
6860 (if (single_use (@0))
6863 vec<constructor_elt, va_gc> *vals;
6864 vec_alloc (vals, count);
6865 bool constant_p = true;
6867 for (unsigned i = 0;
6868 i < count && elt + i < CONSTRUCTOR_NELTS (ctor); ++i)
6870 tree e = CONSTRUCTOR_ELT (ctor, elt + i)->value;
6871 CONSTRUCTOR_APPEND_ELT (vals, NULL_TREE, e);
6872 if (!CONSTANT_CLASS_P (e))
6875 tree evtype = (types_match (TREE_TYPE (type),
6876 TREE_TYPE (TREE_TYPE (ctor)))
6878 : build_vector_type (TREE_TYPE (TREE_TYPE (ctor)),
6880 res = (constant_p ? build_vector_from_ctor (evtype, vals)
6881 : build_constructor (evtype, vals));
6883 (view_convert { res; }))))))
6884 /* The bitfield references a single constructor element. */
6885 (if (k.is_constant (&const_k)
6886 && idx + n <= (idx / const_k + 1) * const_k)
6888 (if (CONSTRUCTOR_NELTS (ctor) <= idx / const_k)
6889 { build_zero_cst (type); })
6891 (view_convert { CONSTRUCTOR_ELT (ctor, idx / const_k)->value; }))
6892 (BIT_FIELD_REF { CONSTRUCTOR_ELT (ctor, idx / const_k)->value; }
6893 @1 { bitsize_int ((idx % const_k) * width); })))))))))
6895 /* Simplify a bit extraction from a bit insertion for the cases with
6896 the inserted element fully covering the extraction or the insertion
6897 not touching the extraction. */
6899 (BIT_FIELD_REF (bit_insert @0 @1 @ipos) @rsize @rpos)
6902 unsigned HOST_WIDE_INT isize;
6903 if (INTEGRAL_TYPE_P (TREE_TYPE (@1)))
6904 isize = TYPE_PRECISION (TREE_TYPE (@1));
6906 isize = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (@1)));
6909 (if (wi::leu_p (wi::to_wide (@ipos), wi::to_wide (@rpos))
6910 && wi::leu_p (wi::to_wide (@rpos) + wi::to_wide (@rsize),
6911 wi::to_wide (@ipos) + isize))
6912 (BIT_FIELD_REF @1 @rsize { wide_int_to_tree (bitsizetype,
6914 - wi::to_wide (@ipos)); }))
6915 (if (wi::geu_p (wi::to_wide (@ipos),
6916 wi::to_wide (@rpos) + wi::to_wide (@rsize))
6917 || wi::geu_p (wi::to_wide (@rpos),
6918 wi::to_wide (@ipos) + isize))
6919 (BIT_FIELD_REF @0 @rsize @rpos)))))
6921 (if (canonicalize_math_after_vectorization_p ())
6924 (fmas:c (negate @0) @1 @2)
6925 (IFN_FNMA @0 @1 @2))
6927 (fmas @0 @1 (negate @2))
6930 (fmas:c (negate @0) @1 (negate @2))
6931 (IFN_FNMS @0 @1 @2))
6933 (negate (fmas@3 @0 @1 @2))
6934 (if (single_use (@3))
6935 (IFN_FNMS @0 @1 @2))))
6938 (IFN_FMS:c (negate @0) @1 @2)
6939 (IFN_FNMS @0 @1 @2))
6941 (IFN_FMS @0 @1 (negate @2))
6944 (IFN_FMS:c (negate @0) @1 (negate @2))
6945 (IFN_FNMA @0 @1 @2))
6947 (negate (IFN_FMS@3 @0 @1 @2))
6948 (if (single_use (@3))
6949 (IFN_FNMA @0 @1 @2)))
6952 (IFN_FNMA:c (negate @0) @1 @2)
6955 (IFN_FNMA @0 @1 (negate @2))
6956 (IFN_FNMS @0 @1 @2))
6958 (IFN_FNMA:c (negate @0) @1 (negate @2))
6961 (negate (IFN_FNMA@3 @0 @1 @2))
6962 (if (single_use (@3))
6963 (IFN_FMS @0 @1 @2)))
6966 (IFN_FNMS:c (negate @0) @1 @2)
6969 (IFN_FNMS @0 @1 (negate @2))
6970 (IFN_FNMA @0 @1 @2))
6972 (IFN_FNMS:c (negate @0) @1 (negate @2))
6975 (negate (IFN_FNMS@3 @0 @1 @2))
6976 (if (single_use (@3))
6977 (IFN_FMA @0 @1 @2))))
6979 /* CLZ simplifications. */
6984 (op (clz:s@2 @0) INTEGER_CST@1)
6985 (if (integer_zerop (@1) && single_use (@2))
6986 /* clz(X) == 0 is (int)X < 0 and clz(X) != 0 is (int)X >= 0. */
6987 (with { tree type0 = TREE_TYPE (@0);
6988 tree stype = signed_type_for (type0);
6989 HOST_WIDE_INT val = 0;
6990 /* Punt on hypothetical weird targets. */
6992 && CLZ_DEFINED_VALUE_AT_ZERO (SCALAR_TYPE_MODE (type0),
6998 (cmp (convert:stype @0) { build_zero_cst (stype); })))
6999 /* clz(X) == (prec-1) is X == 1 and clz(X) != (prec-1) is X != 1. */
7000 (with { bool ok = true;
7001 HOST_WIDE_INT val = 0;
7002 tree type0 = TREE_TYPE (@0);
7003 /* Punt on hypothetical weird targets. */
7005 && CLZ_DEFINED_VALUE_AT_ZERO (SCALAR_TYPE_MODE (type0),
7007 && val == TYPE_PRECISION (type0) - 1)
7010 (if (ok && wi::to_wide (@1) == (TYPE_PRECISION (type0) - 1))
7011 (op @0 { build_one_cst (type0); })))))))
7013 /* CTZ simplifications. */
7015 (for op (ge gt le lt)
7018 /* __builtin_ctz (x) >= C -> (x & ((1 << C) - 1)) == 0. */
7019 (op (ctz:s @0) INTEGER_CST@1)
7020 (with { bool ok = true;
7021 HOST_WIDE_INT val = 0;
7022 if (!tree_fits_shwi_p (@1))
7026 val = tree_to_shwi (@1);
7027 /* Canonicalize to >= or <. */
7028 if (op == GT_EXPR || op == LE_EXPR)
7030 if (val == HOST_WIDE_INT_MAX)
7036 bool zero_res = false;
7037 HOST_WIDE_INT zero_val = 0;
7038 tree type0 = TREE_TYPE (@0);
7039 int prec = TYPE_PRECISION (type0);
7041 && CTZ_DEFINED_VALUE_AT_ZERO (SCALAR_TYPE_MODE (type0),
7046 (if (ok && (!zero_res || zero_val >= val))
7047 { constant_boolean_node (cmp == EQ_EXPR ? true : false, type); })
7049 (if (ok && (!zero_res || zero_val < val))
7050 { constant_boolean_node (cmp == EQ_EXPR ? false : true, type); })
7051 (if (ok && (!zero_res || zero_val < 0 || zero_val >= prec))
7052 (cmp (bit_and @0 { wide_int_to_tree (type0,
7053 wi::mask (val, false, prec)); })
7054 { build_zero_cst (type0); })))))))
7057 /* __builtin_ctz (x) == C -> (x & ((1 << (C + 1)) - 1)) == (1 << C). */
7058 (op (ctz:s @0) INTEGER_CST@1)
7059 (with { bool zero_res = false;
7060 HOST_WIDE_INT zero_val = 0;
7061 tree type0 = TREE_TYPE (@0);
7062 int prec = TYPE_PRECISION (type0);
7064 && CTZ_DEFINED_VALUE_AT_ZERO (SCALAR_TYPE_MODE (type0),
7068 (if (tree_int_cst_sgn (@1) < 0 || wi::to_widest (@1) >= prec)
7069 (if (!zero_res || zero_val != wi::to_widest (@1))
7070 { constant_boolean_node (op == EQ_EXPR ? false : true, type); })
7071 (if (!zero_res || zero_val < 0 || zero_val >= prec)
7072 (op (bit_and @0 { wide_int_to_tree (type0,
7073 wi::mask (tree_to_uhwi (@1) + 1,
7075 { wide_int_to_tree (type0,
7076 wi::shifted_mask (tree_to_uhwi (@1), 1,
7077 false, prec)); })))))))
7079 /* POPCOUNT simplifications. */
7080 /* popcount(X) + popcount(Y) is popcount(X|Y) when X&Y must be zero. */
7082 (plus (POPCOUNT:s @0) (POPCOUNT:s @1))
7083 (if (wi::bit_and (tree_nonzero_bits (@0), tree_nonzero_bits (@1)) == 0)
7084 (POPCOUNT (bit_ior @0 @1))))
7086 /* popcount(X) == 0 is X == 0, and related (in)equalities. */
7087 (for popcount (POPCOUNT)
7088 (for cmp (le eq ne gt)
7091 (cmp (popcount @0) integer_zerop)
7092 (rep @0 { build_zero_cst (TREE_TYPE (@0)); }))))
7094 /* Canonicalize POPCOUNT(x)&1 as PARITY(X). */
7096 (bit_and (POPCOUNT @0) integer_onep)
7099 /* PARITY simplifications. */
7100 /* parity(~X) is parity(X). */
7102 (PARITY (bit_not @0))
7105 /* parity(X)^parity(Y) is parity(X^Y). */
7107 (bit_xor (PARITY:s @0) (PARITY:s @1))
7108 (PARITY (bit_xor @0 @1)))
7110 /* Common POPCOUNT/PARITY simplifications. */
7111 /* popcount(X&C1) is (X>>C2)&1 when C1 == 1<<C2. Same for parity(X&C1). */
7112 (for pfun (POPCOUNT PARITY)
7115 (with { wide_int nz = tree_nonzero_bits (@0); }
7119 (if (wi::popcount (nz) == 1)
7120 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
7121 (convert (rshift:utype (convert:utype @0)
7122 { build_int_cst (integer_type_node,
7123 wi::ctz (nz)); }))))))))
7126 /* 64- and 32-bits branchless implementations of popcount are detected:
7128 int popcount64c (uint64_t x)
7130 x -= (x >> 1) & 0x5555555555555555ULL;
7131 x = (x & 0x3333333333333333ULL) + ((x >> 2) & 0x3333333333333333ULL);
7132 x = (x + (x >> 4)) & 0x0f0f0f0f0f0f0f0fULL;
7133 return (x * 0x0101010101010101ULL) >> 56;
7136 int popcount32c (uint32_t x)
7138 x -= (x >> 1) & 0x55555555;
7139 x = (x & 0x33333333) + ((x >> 2) & 0x33333333);
7140 x = (x + (x >> 4)) & 0x0f0f0f0f;
7141 return (x * 0x01010101) >> 24;
7148 (rshift @8 INTEGER_CST@5)
7150 (bit_and @6 INTEGER_CST@7)
7154 (bit_and (rshift @0 INTEGER_CST@4) INTEGER_CST@11))
7160 /* Check constants and optab. */
7161 (with { unsigned prec = TYPE_PRECISION (type);
7162 int shift = (64 - prec) & 63;
7163 unsigned HOST_WIDE_INT c1
7164 = HOST_WIDE_INT_UC (0x0101010101010101) >> shift;
7165 unsigned HOST_WIDE_INT c2
7166 = HOST_WIDE_INT_UC (0x0F0F0F0F0F0F0F0F) >> shift;
7167 unsigned HOST_WIDE_INT c3
7168 = HOST_WIDE_INT_UC (0x3333333333333333) >> shift;
7169 unsigned HOST_WIDE_INT c4
7170 = HOST_WIDE_INT_UC (0x5555555555555555) >> shift;
7175 && TYPE_UNSIGNED (type)
7176 && integer_onep (@4)
7177 && wi::to_widest (@10) == 2
7178 && wi::to_widest (@5) == 4
7179 && wi::to_widest (@1) == prec - 8
7180 && tree_to_uhwi (@2) == c1
7181 && tree_to_uhwi (@3) == c2
7182 && tree_to_uhwi (@9) == c3
7183 && tree_to_uhwi (@7) == c3
7184 && tree_to_uhwi (@11) == c4)
7185 (if (direct_internal_fn_supported_p (IFN_POPCOUNT, type,
7187 (convert (IFN_POPCOUNT:type @0))
7188 /* Try to do popcount in two halves. PREC must be at least
7189 five bits for this to work without extension before adding. */
7191 tree half_type = NULL_TREE;
7192 opt_machine_mode m = mode_for_size ((prec + 1) / 2, MODE_INT, 1);
7195 && m.require () != TYPE_MODE (type))
7197 half_prec = GET_MODE_PRECISION (as_a <scalar_int_mode> (m));
7198 half_type = build_nonstandard_integer_type (half_prec, 1);
7200 gcc_assert (half_prec > 2);
7202 (if (half_type != NULL_TREE
7203 && direct_internal_fn_supported_p (IFN_POPCOUNT, half_type,
7206 (IFN_POPCOUNT:half_type (convert @0))
7207 (IFN_POPCOUNT:half_type (convert (rshift @0
7208 { build_int_cst (integer_type_node, half_prec); } )))))))))))
7210 /* __builtin_ffs needs to deal on many targets with the possible zero
7211 argument. If we know the argument is always non-zero, __builtin_ctz + 1
7212 should lead to better code. */
7214 (FFS tree_expr_nonzero_p@0)
7215 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
7216 && direct_internal_fn_supported_p (IFN_CTZ, TREE_TYPE (@0),
7217 OPTIMIZE_FOR_SPEED))
7218 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
7219 (plus (CTZ:type (convert:utype @0)) { build_one_cst (type); }))))
7222 (for ffs (BUILT_IN_FFS BUILT_IN_FFSL BUILT_IN_FFSLL
7224 /* __builtin_ffs (X) == 0 -> X == 0.
7225 __builtin_ffs (X) == 6 -> (X & 63) == 32. */
7228 (cmp (ffs@2 @0) INTEGER_CST@1)
7229 (with { int prec = TYPE_PRECISION (TREE_TYPE (@0)); }
7231 (if (integer_zerop (@1))
7232 (cmp @0 { build_zero_cst (TREE_TYPE (@0)); }))
7233 (if (tree_int_cst_sgn (@1) < 0 || wi::to_widest (@1) > prec)
7234 { constant_boolean_node (cmp == NE_EXPR ? true : false, type); })
7235 (if (single_use (@2))
7236 (cmp (bit_and @0 { wide_int_to_tree (TREE_TYPE (@0),
7237 wi::mask (tree_to_uhwi (@1),
7239 { wide_int_to_tree (TREE_TYPE (@0),
7240 wi::shifted_mask (tree_to_uhwi (@1) - 1, 1,
7241 false, prec)); }))))))
7243 /* __builtin_ffs (X) > 6 -> X != 0 && (X & 63) == 0. */
7247 bit_op (bit_and bit_ior)
7249 (cmp (ffs@2 @0) INTEGER_CST@1)
7250 (with { int prec = TYPE_PRECISION (TREE_TYPE (@0)); }
7252 (if (integer_zerop (@1))
7253 (cmp2 @0 { build_zero_cst (TREE_TYPE (@0)); }))
7254 (if (tree_int_cst_sgn (@1) < 0)
7255 { constant_boolean_node (cmp == GT_EXPR ? true : false, type); })
7256 (if (wi::to_widest (@1) >= prec)
7257 { constant_boolean_node (cmp == GT_EXPR ? false : true, type); })
7258 (if (wi::to_widest (@1) == prec - 1)
7259 (cmp3 @0 { wide_int_to_tree (TREE_TYPE (@0),
7260 wi::shifted_mask (prec - 1, 1,
7262 (if (single_use (@2))
7263 (bit_op (cmp2 @0 { build_zero_cst (TREE_TYPE (@0)); })
7265 { wide_int_to_tree (TREE_TYPE (@0),
7266 wi::mask (tree_to_uhwi (@1),
7268 { build_zero_cst (TREE_TYPE (@0)); }))))))))
7275 --> r = .COND_FN (cond, a, b)
7279 --> r = .COND_FN (~cond, b, a). */
7281 (for uncond_op (UNCOND_UNARY)
7282 cond_op (COND_UNARY)
7284 (vec_cond @0 (view_convert? (uncond_op@3 @1)) @2)
7285 (with { tree op_type = TREE_TYPE (@3); }
7286 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
7287 && is_truth_type_for (op_type, TREE_TYPE (@0)))
7288 (cond_op @0 @1 @2))))
7290 (vec_cond @0 @1 (view_convert? (uncond_op@3 @2)))
7291 (with { tree op_type = TREE_TYPE (@3); }
7292 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
7293 && is_truth_type_for (op_type, TREE_TYPE (@0)))
7294 (cond_op (bit_not @0) @2 @1)))))
7303 r = c ? a1 op a2 : b;
7305 if the target can do it in one go. This makes the operation conditional
7306 on c, so could drop potentially-trapping arithmetic, but that's a valid
7307 simplification if the result of the operation isn't needed.
7309 Avoid speculatively generating a stand-alone vector comparison
7310 on targets that might not support them. Any target implementing
7311 conditional internal functions must support the same comparisons
7312 inside and outside a VEC_COND_EXPR. */
7314 (for uncond_op (UNCOND_BINARY)
7315 cond_op (COND_BINARY)
7317 (vec_cond @0 (view_convert? (uncond_op@4 @1 @2)) @3)
7318 (with { tree op_type = TREE_TYPE (@4); }
7319 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
7320 && is_truth_type_for (op_type, TREE_TYPE (@0)))
7321 (view_convert (cond_op @0 @1 @2 (view_convert:op_type @3))))))
7323 (vec_cond @0 @1 (view_convert? (uncond_op@4 @2 @3)))
7324 (with { tree op_type = TREE_TYPE (@4); }
7325 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
7326 && is_truth_type_for (op_type, TREE_TYPE (@0)))
7327 (view_convert (cond_op (bit_not @0) @2 @3 (view_convert:op_type @1)))))))
7329 /* Same for ternary operations. */
7330 (for uncond_op (UNCOND_TERNARY)
7331 cond_op (COND_TERNARY)
7333 (vec_cond @0 (view_convert? (uncond_op@5 @1 @2 @3)) @4)
7334 (with { tree op_type = TREE_TYPE (@5); }
7335 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
7336 && is_truth_type_for (op_type, TREE_TYPE (@0)))
7337 (view_convert (cond_op @0 @1 @2 @3 (view_convert:op_type @4))))))
7339 (vec_cond @0 @1 (view_convert? (uncond_op@5 @2 @3 @4)))
7340 (with { tree op_type = TREE_TYPE (@5); }
7341 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
7342 && is_truth_type_for (op_type, TREE_TYPE (@0)))
7343 (view_convert (cond_op (bit_not @0) @2 @3 @4
7344 (view_convert:op_type @1)))))))
7347 /* Detect cases in which a VEC_COND_EXPR effectively replaces the
7348 "else" value of an IFN_COND_*. */
7349 (for cond_op (COND_BINARY)
7351 (vec_cond @0 (view_convert? (cond_op @0 @1 @2 @3)) @4)
7352 (with { tree op_type = TREE_TYPE (@3); }
7353 (if (element_precision (type) == element_precision (op_type))
7354 (view_convert (cond_op @0 @1 @2 (view_convert:op_type @4))))))
7356 (vec_cond @0 @1 (view_convert? (cond_op @2 @3 @4 @5)))
7357 (with { tree op_type = TREE_TYPE (@5); }
7358 (if (inverse_conditions_p (@0, @2)
7359 && element_precision (type) == element_precision (op_type))
7360 (view_convert (cond_op @2 @3 @4 (view_convert:op_type @1)))))))
7362 /* Same for ternary operations. */
7363 (for cond_op (COND_TERNARY)
7365 (vec_cond @0 (view_convert? (cond_op @0 @1 @2 @3 @4)) @5)
7366 (with { tree op_type = TREE_TYPE (@4); }
7367 (if (element_precision (type) == element_precision (op_type))
7368 (view_convert (cond_op @0 @1 @2 @3 (view_convert:op_type @5))))))
7370 (vec_cond @0 @1 (view_convert? (cond_op @2 @3 @4 @5 @6)))
7371 (with { tree op_type = TREE_TYPE (@6); }
7372 (if (inverse_conditions_p (@0, @2)
7373 && element_precision (type) == element_precision (op_type))
7374 (view_convert (cond_op @2 @3 @4 @5 (view_convert:op_type @1)))))))
7376 /* Detect simplication for a conditional reduction where
7379 c = mask2 ? d + a : d
7383 c = mask1 && mask2 ? d + b : d. */
7385 (IFN_COND_ADD @0 @1 (vec_cond @2 @3 integer_zerop) @1)
7386 (IFN_COND_ADD (bit_and @0 @2) @1 @3 @1))
7388 /* For pointers @0 and @2 and nonnegative constant offset @1, look for
7391 A: (@0 + @1 < @2) | (@2 + @1 < @0)
7392 B: (@0 + @1 <= @2) | (@2 + @1 <= @0)
7394 If pointers are known not to wrap, B checks whether @1 bytes starting
7395 at @0 and @2 do not overlap, while A tests the same thing for @1 + 1
7396 bytes. A is more efficiently tested as:
7398 A: (sizetype) (@0 + @1 - @2) > @1 * 2
7400 The equivalent expression for B is given by replacing @1 with @1 - 1:
7402 B: (sizetype) (@0 + (@1 - 1) - @2) > (@1 - 1) * 2
7404 @0 and @2 can be swapped in both expressions without changing the result.
7406 The folds rely on sizetype's being unsigned (which is always true)
7407 and on its being the same width as the pointer (which we have to check).
7409 The fold replaces two pointer_plus expressions, two comparisons and
7410 an IOR with a pointer_plus, a pointer_diff, and a comparison, so in
7411 the best case it's a saving of two operations. The A fold retains one
7412 of the original pointer_pluses, so is a win even if both pointer_pluses
7413 are used elsewhere. The B fold is a wash if both pointer_pluses are
7414 used elsewhere, since all we end up doing is replacing a comparison with
7415 a pointer_plus. We do still apply the fold under those circumstances
7416 though, in case applying it to other conditions eventually makes one of the
7417 pointer_pluses dead. */
7418 (for ior (truth_orif truth_or bit_ior)
7421 (ior (cmp:cs (pointer_plus@3 @0 INTEGER_CST@1) @2)
7422 (cmp:cs (pointer_plus@4 @2 @1) @0))
7423 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
7424 && TYPE_OVERFLOW_WRAPS (sizetype)
7425 && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (sizetype))
7426 /* Calculate the rhs constant. */
7427 (with { offset_int off = wi::to_offset (@1) - (cmp == LE_EXPR ? 1 : 0);
7428 offset_int rhs = off * 2; }
7429 /* Always fails for negative values. */
7430 (if (wi::min_precision (rhs, UNSIGNED) <= TYPE_PRECISION (sizetype))
7431 /* Since the order of @0 and @2 doesn't matter, let tree_swap_operands_p
7432 pick a canonical order. This increases the chances of using the
7433 same pointer_plus in multiple checks. */
7434 (with { bool swap_p = tree_swap_operands_p (@0, @2);
7435 tree rhs_tree = wide_int_to_tree (sizetype, rhs); }
7436 (if (cmp == LT_EXPR)
7437 (gt (convert:sizetype
7438 (pointer_diff:ssizetype { swap_p ? @4 : @3; }
7439 { swap_p ? @0 : @2; }))
7441 (gt (convert:sizetype
7442 (pointer_diff:ssizetype
7443 (pointer_plus { swap_p ? @2 : @0; }
7444 { wide_int_to_tree (sizetype, off); })
7445 { swap_p ? @0 : @2; }))
7446 { rhs_tree; })))))))))
7448 /* Fold REDUC (@0 & @1) -> @0[I] & @1[I] if element I is the only nonzero
7450 (for reduc (IFN_REDUC_PLUS IFN_REDUC_IOR IFN_REDUC_XOR)
7451 (simplify (reduc (view_convert? (bit_and @0 VECTOR_CST@1)))
7452 (with { int i = single_nonzero_element (@1); }
7454 (with { tree elt = vector_cst_elt (@1, i);
7455 tree elt_type = TREE_TYPE (elt);
7456 unsigned int elt_bits = tree_to_uhwi (TYPE_SIZE (elt_type));
7457 tree size = bitsize_int (elt_bits);
7458 tree pos = bitsize_int (elt_bits * i); }
7461 (BIT_FIELD_REF:elt_type @0 { size; } { pos; })
7465 (vec_perm @0 @1 VECTOR_CST@2)
7468 tree op0 = @0, op1 = @1, op2 = @2;
7470 /* Build a vector of integers from the tree mask. */
7471 vec_perm_builder builder;
7472 if (!tree_to_vec_perm_builder (&builder, op2))
7475 /* Create a vec_perm_indices for the integer vector. */
7476 poly_uint64 nelts = TYPE_VECTOR_SUBPARTS (type);
7477 bool single_arg = (op0 == op1);
7478 vec_perm_indices sel (builder, single_arg ? 1 : 2, nelts);
7480 (if (sel.series_p (0, 1, 0, 1))
7482 (if (sel.series_p (0, 1, nelts, 1))
7488 if (sel.all_from_input_p (0))
7490 else if (sel.all_from_input_p (1))
7493 sel.rotate_inputs (1);
7495 else if (known_ge (poly_uint64 (sel[0]), nelts))
7497 std::swap (op0, op1);
7498 sel.rotate_inputs (1);
7502 tree cop0 = op0, cop1 = op1;
7503 if (TREE_CODE (op0) == SSA_NAME
7504 && (def = dyn_cast <gassign *> (SSA_NAME_DEF_STMT (op0)))
7505 && gimple_assign_rhs_code (def) == CONSTRUCTOR)
7506 cop0 = gimple_assign_rhs1 (def);
7507 if (TREE_CODE (op1) == SSA_NAME
7508 && (def = dyn_cast <gassign *> (SSA_NAME_DEF_STMT (op1)))
7509 && gimple_assign_rhs_code (def) == CONSTRUCTOR)
7510 cop1 = gimple_assign_rhs1 (def);
7514 (if ((TREE_CODE (cop0) == VECTOR_CST
7515 || TREE_CODE (cop0) == CONSTRUCTOR)
7516 && (TREE_CODE (cop1) == VECTOR_CST
7517 || TREE_CODE (cop1) == CONSTRUCTOR)
7518 && (t = fold_vec_perm (type, cop0, cop1, sel)))
7522 bool changed = (op0 == op1 && !single_arg);
7523 tree ins = NULL_TREE;
7526 /* See if the permutation is performing a single element
7527 insert from a CONSTRUCTOR or constant and use a BIT_INSERT_EXPR
7528 in that case. But only if the vector mode is supported,
7529 otherwise this is invalid GIMPLE. */
7530 if (TYPE_MODE (type) != BLKmode
7531 && (TREE_CODE (cop0) == VECTOR_CST
7532 || TREE_CODE (cop0) == CONSTRUCTOR
7533 || TREE_CODE (cop1) == VECTOR_CST
7534 || TREE_CODE (cop1) == CONSTRUCTOR))
7536 bool insert_first_p = sel.series_p (1, 1, nelts + 1, 1);
7539 /* After canonicalizing the first elt to come from the
7540 first vector we only can insert the first elt from
7541 the first vector. */
7543 if ((ins = fold_read_from_vector (cop0, sel[0])))
7546 /* The above can fail for two-element vectors which always
7547 appear to insert the first element, so try inserting
7548 into the second lane as well. For more than two
7549 elements that's wasted time. */
7550 if (!insert_first_p || (!ins && maybe_eq (nelts, 2u)))
7552 unsigned int encoded_nelts = sel.encoding ().encoded_nelts ();
7553 for (at = 0; at < encoded_nelts; ++at)
7554 if (maybe_ne (sel[at], at))
7556 if (at < encoded_nelts
7557 && (known_eq (at + 1, nelts)
7558 || sel.series_p (at + 1, 1, at + 1, 1)))
7560 if (known_lt (poly_uint64 (sel[at]), nelts))
7561 ins = fold_read_from_vector (cop0, sel[at]);
7563 ins = fold_read_from_vector (cop1, sel[at] - nelts);
7568 /* Generate a canonical form of the selector. */
7569 if (!ins && sel.encoding () != builder)
7571 /* Some targets are deficient and fail to expand a single
7572 argument permutation while still allowing an equivalent
7573 2-argument version. */
7575 if (sel.ninputs () == 2
7576 || can_vec_perm_const_p (TYPE_MODE (type), sel, false))
7577 op2 = vec_perm_indices_to_tree (TREE_TYPE (op2), sel);
7580 vec_perm_indices sel2 (builder, 2, nelts);
7581 if (can_vec_perm_const_p (TYPE_MODE (type), sel2, false))
7582 op2 = vec_perm_indices_to_tree (TREE_TYPE (op2), sel2);
7584 /* Not directly supported with either encoding,
7585 so use the preferred form. */
7586 op2 = vec_perm_indices_to_tree (TREE_TYPE (op2), sel);
7588 if (!operand_equal_p (op2, oldop2, 0))
7593 (bit_insert { op0; } { ins; }
7594 { bitsize_int (at * vector_element_bits (type)); })
7596 (vec_perm { op0; } { op1; } { op2; }))))))))))
7598 /* VEC_PERM_EXPR (v, v, mask) -> v where v contains same element. */
7600 (match vec_same_elem_p
7602 (if (uniform_vector_p (@0))))
7604 (match vec_same_elem_p
7608 (vec_perm vec_same_elem_p@0 @0 @1)
7611 /* Match count trailing zeroes for simplify_count_trailing_zeroes in fwprop.
7612 The canonical form is array[((x & -x) * C) >> SHIFT] where C is a magic
7613 constant which when multiplied by a power of 2 contains a unique value
7614 in the top 5 or 6 bits. This is then indexed into a table which maps it
7615 to the number of trailing zeroes. */
7616 (match (ctz_table_index @1 @2 @3)
7617 (rshift (mult (bit_and:c (negate @1) @1) INTEGER_CST@2) INTEGER_CST@3))