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-2016 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
33 tree_expr_nonnegative_p
39 (define_operator_list tcc_comparison
40 lt le eq ne ge gt unordered ordered unlt unle ungt unge uneq ltgt)
41 (define_operator_list inverted_tcc_comparison
42 ge gt ne eq lt le ordered unordered ge gt le lt ltgt uneq)
43 (define_operator_list inverted_tcc_comparison_with_nans
44 unge ungt ne eq unlt unle ordered unordered ge gt le lt ltgt uneq)
45 (define_operator_list swapped_tcc_comparison
46 gt ge eq ne le lt unordered ordered ungt unge unlt unle uneq ltgt)
47 (define_operator_list simple_comparison lt le eq ne ge gt)
48 (define_operator_list swapped_simple_comparison gt ge eq ne le lt)
50 #include "cfn-operators.pd"
52 /* Define operand lists for math rounding functions {,i,l,ll}FN,
53 where the versions prefixed with "i" return an int, those prefixed with
54 "l" return a long and those prefixed with "ll" return a long long.
56 Also define operand lists:
58 X<FN>F for all float functions, in the order i, l, ll
59 X<FN> for all double functions, in the same order
60 X<FN>L for all long double functions, in the same order. */
61 #define DEFINE_INT_AND_FLOAT_ROUND_FN(FN) \
62 (define_operator_list X##FN##F BUILT_IN_I##FN##F \
65 (define_operator_list X##FN BUILT_IN_I##FN \
68 (define_operator_list X##FN##L BUILT_IN_I##FN##L \
72 DEFINE_INT_AND_FLOAT_ROUND_FN (FLOOR)
73 DEFINE_INT_AND_FLOAT_ROUND_FN (CEIL)
74 DEFINE_INT_AND_FLOAT_ROUND_FN (ROUND)
75 DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
77 /* Simplifications of operations with one constant operand and
78 simplifications to constants or single values. */
80 (for op (plus pointer_plus minus bit_ior bit_xor)
85 /* 0 +p index -> (type)index */
87 (pointer_plus integer_zerop @1)
88 (non_lvalue (convert @1)))
90 /* See if ARG1 is zero and X + ARG1 reduces to X.
91 Likewise if the operands are reversed. */
93 (plus:c @0 real_zerop@1)
94 (if (fold_real_zero_addition_p (type, @1, 0))
97 /* See if ARG1 is zero and X - ARG1 reduces to X. */
99 (minus @0 real_zerop@1)
100 (if (fold_real_zero_addition_p (type, @1, 1))
104 This is unsafe for certain floats even in non-IEEE formats.
105 In IEEE, it is unsafe because it does wrong for NaNs.
106 Also note that operand_equal_p is always false if an operand
110 (if (!FLOAT_TYPE_P (type) || !HONOR_NANS (type))
111 { build_zero_cst (type); }))
114 (mult @0 integer_zerop@1)
117 /* Maybe fold x * 0 to 0. The expressions aren't the same
118 when x is NaN, since x * 0 is also NaN. Nor are they the
119 same in modes with signed zeros, since multiplying a
120 negative value by 0 gives -0, not +0. */
122 (mult @0 real_zerop@1)
123 (if (!HONOR_NANS (type) && !HONOR_SIGNED_ZEROS (type))
126 /* In IEEE floating point, x*1 is not equivalent to x for snans.
127 Likewise for complex arithmetic with signed zeros. */
130 (if (!HONOR_SNANS (type)
131 && (!HONOR_SIGNED_ZEROS (type)
132 || !COMPLEX_FLOAT_TYPE_P (type)))
135 /* Transform x * -1.0 into -x. */
137 (mult @0 real_minus_onep)
138 (if (!HONOR_SNANS (type)
139 && (!HONOR_SIGNED_ZEROS (type)
140 || !COMPLEX_FLOAT_TYPE_P (type)))
143 /* Make sure to preserve divisions by zero. This is the reason why
144 we don't simplify x / x to 1 or 0 / x to 0. */
145 (for op (mult trunc_div ceil_div floor_div round_div exact_div)
151 (for div (trunc_div ceil_div floor_div round_div exact_div)
153 (div @0 integer_minus_onep@1)
154 (if (!TYPE_UNSIGNED (type))
157 /* For unsigned integral types, FLOOR_DIV_EXPR is the same as
158 TRUNC_DIV_EXPR. Rewrite into the latter in this case. */
161 (if ((INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
162 && TYPE_UNSIGNED (type))
165 /* Combine two successive divisions. Note that combining ceil_div
166 and floor_div is trickier and combining round_div even more so. */
167 (for div (trunc_div exact_div)
169 (div (div @0 INTEGER_CST@1) INTEGER_CST@2)
172 wide_int mul = wi::mul (@1, @2, TYPE_SIGN (type), &overflow_p);
175 (div @0 { wide_int_to_tree (type, mul); })
176 (if (TYPE_UNSIGNED (type)
177 || mul != wi::min_value (TYPE_PRECISION (type), SIGNED))
178 { build_zero_cst (type); })))))
180 /* Optimize A / A to 1.0 if we don't care about
181 NaNs or Infinities. */
184 (if (FLOAT_TYPE_P (type)
185 && ! HONOR_NANS (type)
186 && ! HONOR_INFINITIES (type))
187 { build_one_cst (type); }))
189 /* Optimize -A / A to -1.0 if we don't care about
190 NaNs or Infinities. */
192 (rdiv:c @0 (negate @0))
193 (if (FLOAT_TYPE_P (type)
194 && ! HONOR_NANS (type)
195 && ! HONOR_INFINITIES (type))
196 { build_minus_one_cst (type); }))
198 /* In IEEE floating point, x/1 is not equivalent to x for snans. */
201 (if (!HONOR_SNANS (type))
204 /* In IEEE floating point, x/-1 is not equivalent to -x for snans. */
206 (rdiv @0 real_minus_onep)
207 (if (!HONOR_SNANS (type))
210 (if (flag_reciprocal_math)
211 /* Convert (A/B)/C to A/(B*C) */
213 (rdiv (rdiv:s @0 @1) @2)
214 (rdiv @0 (mult @1 @2)))
216 /* Convert A/(B/C) to (A/B)*C */
218 (rdiv @0 (rdiv:s @1 @2))
219 (mult (rdiv @0 @1) @2)))
221 /* Optimize (X & (-A)) / A where A is a power of 2, to X >> log2(A) */
222 (for div (trunc_div ceil_div floor_div round_div exact_div)
224 (div (convert? (bit_and @0 INTEGER_CST@1)) INTEGER_CST@2)
225 (if (integer_pow2p (@2)
226 && tree_int_cst_sgn (@2) > 0
227 && wi::add (@2, @1) == 0
228 && tree_nop_conversion_p (type, TREE_TYPE (@0)))
229 (rshift (convert @0) { build_int_cst (integer_type_node,
230 wi::exact_log2 (@2)); }))))
232 /* If ARG1 is a constant, we can convert this to a multiply by the
233 reciprocal. This does not have the same rounding properties,
234 so only do this if -freciprocal-math. We can actually
235 always safely do it if ARG1 is a power of two, but it's hard to
236 tell if it is or not in a portable manner. */
237 (for cst (REAL_CST COMPLEX_CST VECTOR_CST)
241 (if (flag_reciprocal_math
244 { tree tem = const_binop (RDIV_EXPR, type, build_one_cst (type), @1); }
246 (mult @0 { tem; } )))
247 (if (cst != COMPLEX_CST)
248 (with { tree inverse = exact_inverse (type, @1); }
250 (mult @0 { inverse; } ))))))))
252 /* Same applies to modulo operations, but fold is inconsistent here
253 and simplifies 0 % x to 0, only preserving literal 0 % 0. */
254 (for mod (ceil_mod floor_mod round_mod trunc_mod)
255 /* 0 % X is always zero. */
257 (mod integer_zerop@0 @1)
258 /* But not for 0 % 0 so that we can get the proper warnings and errors. */
259 (if (!integer_zerop (@1))
261 /* X % 1 is always zero. */
263 (mod @0 integer_onep)
264 { build_zero_cst (type); })
265 /* X % -1 is zero. */
267 (mod @0 integer_minus_onep@1)
268 (if (!TYPE_UNSIGNED (type))
269 { build_zero_cst (type); }))
270 /* (X % Y) % Y is just X % Y. */
272 (mod (mod@2 @0 @1) @1)
274 /* From extract_muldiv_1: (X * C1) % C2 is zero if C1 is a multiple of C2. */
276 (mod (mult @0 INTEGER_CST@1) INTEGER_CST@2)
277 (if (ANY_INTEGRAL_TYPE_P (type)
278 && TYPE_OVERFLOW_UNDEFINED (type)
279 && wi::multiple_of_p (@1, @2, TYPE_SIGN (type)))
280 { build_zero_cst (type); })))
282 /* X % -C is the same as X % C. */
284 (trunc_mod @0 INTEGER_CST@1)
285 (if (TYPE_SIGN (type) == SIGNED
286 && !TREE_OVERFLOW (@1)
288 && !TYPE_OVERFLOW_TRAPS (type)
289 /* Avoid this transformation if C is INT_MIN, i.e. C == -C. */
290 && !sign_bit_p (@1, @1))
291 (trunc_mod @0 (negate @1))))
293 /* X % -Y is the same as X % Y. */
295 (trunc_mod @0 (convert? (negate @1)))
296 (if (INTEGRAL_TYPE_P (type)
297 && !TYPE_UNSIGNED (type)
298 && !TYPE_OVERFLOW_TRAPS (type)
299 && tree_nop_conversion_p (type, TREE_TYPE (@1))
300 /* Avoid this transformation if X might be INT_MIN or
301 Y might be -1, because we would then change valid
302 INT_MIN % -(-1) into invalid INT_MIN % -1. */
303 && (expr_not_equal_to (@0, TYPE_MIN_VALUE (type))
304 || expr_not_equal_to (@1, wi::minus_one (TYPE_PRECISION
306 (trunc_mod @0 (convert @1))))
308 /* X - (X / Y) * Y is the same as X % Y. */
310 (minus (convert1? @2) (convert2? (mult:c (trunc_div @0 @1) @1)))
311 /* We cannot use matching captures here, since in the case of
312 constants we really want the type of @0, not @2. */
313 (if (operand_equal_p (@0, @2, 0)
314 && (INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type)))
315 (convert (trunc_mod @0 @1))))
317 /* Optimize TRUNC_MOD_EXPR by a power of two into a BIT_AND_EXPR,
318 i.e. "X % C" into "X & (C - 1)", if X and C are positive.
319 Also optimize A % (C << N) where C is a power of 2,
320 to A & ((C << N) - 1). */
321 (match (power_of_two_cand @1)
323 (match (power_of_two_cand @1)
324 (lshift INTEGER_CST@1 @2))
325 (for mod (trunc_mod floor_mod)
327 (mod @0 (convert?@3 (power_of_two_cand@1 @2)))
328 (if ((TYPE_UNSIGNED (type)
329 || tree_expr_nonnegative_p (@0))
330 && tree_nop_conversion_p (type, TREE_TYPE (@3))
331 && integer_pow2p (@2) && tree_int_cst_sgn (@2) > 0)
332 (bit_and @0 (convert (minus @1 { build_int_cst (TREE_TYPE (@1), 1); }))))))
334 /* Simplify (unsigned t * 2)/2 -> unsigned t & 0x7FFFFFFF. */
336 (trunc_div (mult @0 integer_pow2p@1) @1)
337 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
338 (bit_and @0 { wide_int_to_tree
339 (type, wi::mask (TYPE_PRECISION (type) - wi::exact_log2 (@1),
340 false, TYPE_PRECISION (type))); })))
342 /* Simplify (unsigned t / 2) * 2 -> unsigned t & ~1. */
344 (mult (trunc_div @0 integer_pow2p@1) @1)
345 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
346 (bit_and @0 (negate @1))))
348 /* Simplify (t * 2) / 2) -> t. */
349 (for div (trunc_div ceil_div floor_div round_div exact_div)
351 (div (mult @0 @1) @1)
352 (if (ANY_INTEGRAL_TYPE_P (type)
353 && TYPE_OVERFLOW_UNDEFINED (type))
357 /* Simplify cos(-x) and cos(|x|) -> cos(x). Similarly for cosh. */
362 /* Simplify pow(-x, y) and pow(|x|,y) -> pow(x,y) if y is an even integer. */
365 (pows (op @0) REAL_CST@1)
366 (with { HOST_WIDE_INT n; }
367 (if (real_isinteger (&TREE_REAL_CST (@1), &n) && (n & 1) == 0)
369 /* Likewise for powi. */
372 (pows (op @0) INTEGER_CST@1)
373 (if (wi::bit_and (@1, 1) == 0)
375 /* Strip negate and abs from both operands of hypot. */
383 /* copysign(-x, y) and copysign(abs(x), y) -> copysign(x, y). */
384 (for copysigns (COPYSIGN)
386 (copysigns (op @0) @1)
389 /* abs(x)*abs(x) -> x*x. Should be valid for all types. */
394 /* cos(copysign(x, y)) -> cos(x). Similarly for cosh. */
398 (coss (copysigns @0 @1))
401 /* pow(copysign(x, y), z) -> pow(x, z) if z is an even integer. */
405 (pows (copysigns @0 @2) REAL_CST@1)
406 (with { HOST_WIDE_INT n; }
407 (if (real_isinteger (&TREE_REAL_CST (@1), &n) && (n & 1) == 0)
409 /* Likewise for powi. */
413 (pows (copysigns @0 @2) INTEGER_CST@1)
414 (if (wi::bit_and (@1, 1) == 0)
419 /* hypot(copysign(x, y), z) -> hypot(x, z). */
421 (hypots (copysigns @0 @1) @2)
423 /* hypot(x, copysign(y, z)) -> hypot(x, y). */
425 (hypots @0 (copysigns @1 @2))
428 /* copysign(copysign(x, y), z) -> copysign(x, z). */
429 (for copysigns (COPYSIGN)
431 (copysigns (copysigns @0 @1) @2)
434 /* copysign(x,y)*copysign(x,y) -> x*x. */
435 (for copysigns (COPYSIGN)
437 (mult (copysigns@2 @0 @1) @2)
440 /* ccos(-x) -> ccos(x). Similarly for ccosh. */
441 (for ccoss (CCOS CCOSH)
446 /* cabs(-x) and cos(conj(x)) -> cabs(x). */
447 (for ops (conj negate)
453 /* Fold (a * (1 << b)) into (a << b) */
455 (mult:c @0 (convert? (lshift integer_onep@1 @2)))
456 (if (! FLOAT_TYPE_P (type)
457 && tree_nop_conversion_p (type, TREE_TYPE (@1)))
460 /* Fold (C1/X)*C2 into (C1*C2)/X. */
462 (mult (rdiv@3 REAL_CST@0 @1) REAL_CST@2)
463 (if (flag_associative_math
466 { tree tem = const_binop (MULT_EXPR, type, @0, @2); }
468 (rdiv { tem; } @1)))))
470 /* Convert C1/(X*C2) into (C1/C2)/X */
472 (rdiv REAL_CST@0 (mult @1 REAL_CST@2))
473 (if (flag_reciprocal_math)
475 { tree tem = const_binop (RDIV_EXPR, type, @0, @2); }
477 (rdiv { tem; } @1)))))
479 /* Simplify ~X & X as zero. */
481 (bit_and:c (convert? @0) (convert? (bit_not @0)))
482 { build_zero_cst (type); })
484 /* Fold (A & ~B) - (A & B) into (A ^ B) - B. */
486 (minus (bit_and:cs @0 (bit_not @1)) (bit_and:cs @0 @1))
487 (minus (bit_xor @0 @1) @1))
489 (minus (bit_and:s @0 INTEGER_CST@2) (bit_and:s @0 INTEGER_CST@1))
490 (if (wi::bit_not (@2) == @1)
491 (minus (bit_xor @0 @1) @1)))
493 /* Fold (A & B) - (A & ~B) into B - (A ^ B). */
495 (minus (bit_and:s @0 @1) (bit_and:cs @0 (bit_not @1)))
496 (minus @1 (bit_xor @0 @1)))
498 /* Simplify (X & ~Y) | (~X & Y) -> X ^ Y. */
500 (bit_ior (bit_and:c @0 (bit_not @1)) (bit_and:c (bit_not @0) @1))
503 (bit_ior:c (bit_and @0 INTEGER_CST@2) (bit_and (bit_not @0) INTEGER_CST@1))
504 (if (wi::bit_not (@2) == @1)
507 /* X % Y is smaller than Y. */
510 (cmp (trunc_mod @0 @1) @1)
511 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
512 { constant_boolean_node (cmp == LT_EXPR, type); })))
515 (cmp @1 (trunc_mod @0 @1))
516 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
517 { constant_boolean_node (cmp == GT_EXPR, type); })))
521 (bit_ior @0 integer_all_onesp@1)
526 (bit_and @0 integer_zerop@1)
532 (for op (bit_ior bit_xor plus)
534 (op:c (convert? @0) (convert? (bit_not @0)))
535 (convert { build_all_ones_cst (TREE_TYPE (@0)); })))
540 { build_zero_cst (type); })
542 /* Canonicalize X ^ ~0 to ~X. */
544 (bit_xor @0 integer_all_onesp@1)
549 (bit_and @0 integer_all_onesp)
552 /* x & x -> x, x | x -> x */
553 (for bitop (bit_and bit_ior)
558 /* x + (x & 1) -> (x + 1) & ~1 */
560 (plus:c @0 (bit_and:s @0 integer_onep@1))
561 (bit_and (plus @0 @1) (bit_not @1)))
563 /* x & ~(x & y) -> x & ~y */
564 /* x | ~(x | y) -> x | ~y */
565 (for bitop (bit_and bit_ior)
567 (bitop:c @0 (bit_not (bitop:cs @0 @1)))
568 (bitop @0 (bit_not @1))))
570 /* (x | y) & ~x -> y & ~x */
571 /* (x & y) | ~x -> y | ~x */
572 (for bitop (bit_and bit_ior)
573 rbitop (bit_ior bit_and)
575 (bitop:c (rbitop:c @0 @1) (bit_not@2 @0))
578 /* (x & y) ^ (x | y) -> x ^ y */
580 (bit_xor:c (bit_and @0 @1) (bit_ior @0 @1))
583 /* (x ^ y) ^ (x | y) -> x & y */
585 (bit_xor:c (bit_xor @0 @1) (bit_ior @0 @1))
588 /* (x & y) + (x ^ y) -> x | y */
589 /* (x & y) | (x ^ y) -> x | y */
590 /* (x & y) ^ (x ^ y) -> x | y */
591 (for op (plus bit_ior bit_xor)
593 (op:c (bit_and @0 @1) (bit_xor @0 @1))
596 /* (x & y) + (x | y) -> x + y */
598 (plus:c (bit_and @0 @1) (bit_ior @0 @1))
601 /* (x + y) - (x | y) -> x & y */
603 (minus (plus @0 @1) (bit_ior @0 @1))
604 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
605 && !TYPE_SATURATING (type))
608 /* (x + y) - (x & y) -> x | y */
610 (minus (plus @0 @1) (bit_and @0 @1))
611 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
612 && !TYPE_SATURATING (type))
615 /* (x | y) - (x ^ y) -> x & y */
617 (minus (bit_ior @0 @1) (bit_xor @0 @1))
620 /* (x | y) - (x & y) -> x ^ y */
622 (minus (bit_ior @0 @1) (bit_and @0 @1))
625 /* (x | y) & ~(x & y) -> x ^ y */
627 (bit_and:c (bit_ior @0 @1) (bit_not (bit_and @0 @1)))
630 /* (x | y) & (~x ^ y) -> x & y */
632 (bit_and:c (bit_ior:c @0 @1) (bit_xor:c @1 (bit_not @0)))
635 /* ~x & ~y -> ~(x | y)
636 ~x | ~y -> ~(x & y) */
637 (for op (bit_and bit_ior)
638 rop (bit_ior bit_and)
640 (op (convert1? (bit_not @0)) (convert2? (bit_not @1)))
641 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
642 && tree_nop_conversion_p (type, TREE_TYPE (@1)))
643 (bit_not (rop (convert @0) (convert @1))))))
645 /* If we are XORing or adding two BIT_AND_EXPR's, both of which are and'ing
646 with a constant, and the two constants have no bits in common,
647 we should treat this as a BIT_IOR_EXPR since this may produce more
649 (for op (bit_xor plus)
651 (op (convert1? (bit_and@4 @0 INTEGER_CST@1))
652 (convert2? (bit_and@5 @2 INTEGER_CST@3)))
653 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
654 && tree_nop_conversion_p (type, TREE_TYPE (@2))
655 && wi::bit_and (@1, @3) == 0)
656 (bit_ior (convert @4) (convert @5)))))
658 /* (X | Y) ^ X -> Y & ~ X*/
660 (bit_xor:c (convert? (bit_ior:c @0 @1)) (convert? @0))
661 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
662 (convert (bit_and @1 (bit_not @0)))))
664 /* Convert ~X ^ ~Y to X ^ Y. */
666 (bit_xor (convert1? (bit_not @0)) (convert2? (bit_not @1)))
667 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
668 && tree_nop_conversion_p (type, TREE_TYPE (@1)))
669 (bit_xor (convert @0) (convert @1))))
671 /* Convert ~X ^ C to X ^ ~C. */
673 (bit_xor (convert? (bit_not @0)) INTEGER_CST@1)
674 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
675 (bit_xor (convert @0) (bit_not @1))))
677 /* Fold (X & Y) ^ Y as ~X & Y. */
679 (bit_xor:c (bit_and:c @0 @1) @1)
680 (bit_and (bit_not @0) @1))
682 /* Given a bit-wise operation CODE applied to ARG0 and ARG1, see if both
683 operands are another bit-wise operation with a common input. If so,
684 distribute the bit operations to save an operation and possibly two if
685 constants are involved. For example, convert
686 (A | B) & (A | C) into A | (B & C)
687 Further simplification will occur if B and C are constants. */
688 (for op (bit_and bit_ior)
689 rop (bit_ior bit_and)
691 (op (convert? (rop:c @0 @1)) (convert? (rop @0 @2)))
692 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
693 (rop (convert @0) (op (convert @1) (convert @2))))))
703 (abs tree_expr_nonnegative_p@0)
706 /* A few cases of fold-const.c negate_expr_p predicate. */
709 (if ((INTEGRAL_TYPE_P (type)
710 && TYPE_OVERFLOW_WRAPS (type))
711 || (!TYPE_OVERFLOW_SANITIZED (type)
712 && may_negate_without_overflow_p (t)))))
717 (if (!TYPE_OVERFLOW_SANITIZED (type))))
720 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (t)))))
721 /* VECTOR_CST handling of non-wrapping types would recurse in unsupported
725 (if (FLOAT_TYPE_P (TREE_TYPE (type)) || TYPE_OVERFLOW_WRAPS (type))))
727 /* (-A) * (-B) -> A * B */
729 (mult:c (convert1? (negate @0)) (convert2? negate_expr_p@1))
730 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
731 && tree_nop_conversion_p (type, TREE_TYPE (@1)))
732 (mult (convert @0) (convert (negate @1)))))
734 /* -(A + B) -> (-B) - A. */
736 (negate (plus:c @0 negate_expr_p@1))
737 (if (!HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type))
738 && !HONOR_SIGNED_ZEROS (element_mode (type)))
739 (minus (negate @1) @0)))
741 /* A - B -> A + (-B) if B is easily negatable. */
743 (minus @0 negate_expr_p@1)
744 (if (!FIXED_POINT_TYPE_P (type))
745 (plus @0 (negate @1))))
747 /* Try to fold (type) X op CST -> (type) (X op ((type-x) CST))
749 For bitwise binary operations apply operand conversions to the
750 binary operation result instead of to the operands. This allows
751 to combine successive conversions and bitwise binary operations.
752 We combine the above two cases by using a conditional convert. */
753 (for bitop (bit_and bit_ior bit_xor)
755 (bitop (convert @0) (convert? @1))
756 (if (((TREE_CODE (@1) == INTEGER_CST
757 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
758 && int_fits_type_p (@1, TREE_TYPE (@0)))
759 || types_match (@0, @1))
760 /* ??? This transform conflicts with fold-const.c doing
761 Convert (T)(x & c) into (T)x & (T)c, if c is an integer
762 constants (if x has signed type, the sign bit cannot be set
763 in c). This folds extension into the BIT_AND_EXPR.
764 Restrict it to GIMPLE to avoid endless recursions. */
765 && (bitop != BIT_AND_EXPR || GIMPLE)
766 && (/* That's a good idea if the conversion widens the operand, thus
767 after hoisting the conversion the operation will be narrower. */
768 TYPE_PRECISION (TREE_TYPE (@0)) < TYPE_PRECISION (type)
769 /* It's also a good idea if the conversion is to a non-integer
771 || GET_MODE_CLASS (TYPE_MODE (type)) != MODE_INT
772 /* Or if the precision of TO is not the same as the precision
774 || TYPE_PRECISION (type) != GET_MODE_PRECISION (TYPE_MODE (type))))
775 (convert (bitop @0 (convert @1))))))
777 (for bitop (bit_and bit_ior)
778 rbitop (bit_ior bit_and)
779 /* (x | y) & x -> x */
780 /* (x & y) | x -> x */
782 (bitop:c (rbitop:c @0 @1) @0)
784 /* (~x | y) & x -> x & y */
785 /* (~x & y) | x -> x | y */
787 (bitop:c (rbitop:c (bit_not @0) @1) @0)
790 /* Simplify (A & B) OP0 (C & B) to (A OP0 C) & B. */
791 (for bitop (bit_and bit_ior bit_xor)
793 (bitop (bit_and:c @0 @1) (bit_and @2 @1))
794 (bit_and (bitop @0 @2) @1)))
796 /* (x | CST1) & CST2 -> (x & CST2) | (CST1 & CST2) */
798 (bit_and (bit_ior @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
799 (bit_ior (bit_and @0 @2) (bit_and @1 @2)))
801 /* Combine successive equal operations with constants. */
802 (for bitop (bit_and bit_ior bit_xor)
804 (bitop (bitop @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
805 (bitop @0 (bitop @1 @2))))
807 /* Try simple folding for X op !X, and X op X with the help
808 of the truth_valued_p and logical_inverted_value predicates. */
809 (match truth_valued_p
811 (if (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1)))
812 (for op (tcc_comparison truth_and truth_andif truth_or truth_orif truth_xor)
813 (match truth_valued_p
815 (match truth_valued_p
818 (match (logical_inverted_value @0)
820 (match (logical_inverted_value @0)
821 (bit_not truth_valued_p@0))
822 (match (logical_inverted_value @0)
823 (eq @0 integer_zerop))
824 (match (logical_inverted_value @0)
825 (ne truth_valued_p@0 integer_truep))
826 (match (logical_inverted_value @0)
827 (bit_xor truth_valued_p@0 integer_truep))
831 (bit_and:c @0 (logical_inverted_value @0))
832 { build_zero_cst (type); })
833 /* X | !X and X ^ !X -> 1, , if X is truth-valued. */
834 (for op (bit_ior bit_xor)
836 (op:c truth_valued_p@0 (logical_inverted_value @0))
837 { constant_boolean_node (true, type); }))
838 /* X ==/!= !X is false/true. */
841 (op:c truth_valued_p@0 (logical_inverted_value @0))
842 { constant_boolean_node (op == NE_EXPR ? true : false, type); }))
844 /* If arg1 and arg2 are booleans (or any single bit type)
845 then try to simplify:
852 But only do this if our result feeds into a comparison as
853 this transformation is not always a win, particularly on
854 targets with and-not instructions.
855 -> simplify_bitwise_binary_boolean */
857 (ne (bit_and:c (bit_not @0) @1) integer_zerop)
858 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
859 && TYPE_PRECISION (TREE_TYPE (@1)) == 1)
862 (ne (bit_ior:c (bit_not @0) @1) integer_zerop)
863 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
864 && TYPE_PRECISION (TREE_TYPE (@1)) == 1)
869 (bit_not (bit_not @0))
872 /* Convert ~ (-A) to A - 1. */
874 (bit_not (convert? (negate @0)))
875 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
876 (convert (minus @0 { build_each_one_cst (TREE_TYPE (@0)); }))))
878 /* Convert ~ (A - 1) or ~ (A + -1) to -A. */
880 (bit_not (convert? (minus @0 integer_each_onep)))
881 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
882 (convert (negate @0))))
884 (bit_not (convert? (plus @0 integer_all_onesp)))
885 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
886 (convert (negate @0))))
888 /* Part of convert ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify. */
890 (bit_not (convert? (bit_xor @0 INTEGER_CST@1)))
891 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
892 (convert (bit_xor @0 (bit_not @1)))))
894 (bit_not (convert? (bit_xor:c (bit_not @0) @1)))
895 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
896 (convert (bit_xor @0 @1))))
898 /* (x & ~m) | (y & m) -> ((x ^ y) & m) ^ x */
900 (bit_ior:c (bit_and:cs @0 (bit_not @2)) (bit_and:cs @1 @2))
901 (bit_xor (bit_and (bit_xor @0 @1) @2) @0))
903 /* Fold A - (A & B) into ~B & A. */
905 (minus (convert? @0) (convert?:s (bit_and:cs @0 @1)))
906 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
907 && tree_nop_conversion_p (type, TREE_TYPE (@1)))
908 (convert (bit_and (bit_not @1) @0))))
912 /* ((X inner_op C0) outer_op C1)
913 With X being a tree where value_range has reasoned certain bits to always be
914 zero throughout its computed value range,
915 inner_op = {|,^}, outer_op = {|,^} and inner_op != outer_op
916 where zero_mask has 1's for all bits that are sure to be 0 in
918 if (inner_op == '^') C0 &= ~C1;
919 if ((C0 & ~zero_mask) == 0) then emit (X outer_op (C0 outer_op C1)
920 if ((C1 & ~zero_mask) == 0) then emit (X inner_op (C0 outer_op C1)
922 (for inner_op (bit_ior bit_xor)
923 outer_op (bit_xor bit_ior)
926 (inner_op:s @2 INTEGER_CST@0) INTEGER_CST@1)
930 wide_int zero_mask_not;
934 if (TREE_CODE (@2) == SSA_NAME)
935 zero_mask_not = get_nonzero_bits (@2);
939 if (inner_op == BIT_XOR_EXPR)
941 C0 = wi::bit_and_not (@0, @1);
942 cst_emit = wi::bit_or (C0, @1);
947 cst_emit = wi::bit_xor (@0, @1);
950 (if (!fail && wi::bit_and (C0, zero_mask_not) == 0)
951 (outer_op @2 { wide_int_to_tree (type, cst_emit); })
952 (if (!fail && wi::bit_and (@1, zero_mask_not) == 0)
953 (inner_op @2 { wide_int_to_tree (type, cst_emit); }))))))
955 /* Associate (p +p off1) +p off2 as (p +p (off1 + off2)). */
957 (pointer_plus (pointer_plus:s @0 @1) @3)
958 (pointer_plus @0 (plus @1 @3)))
964 tem4 = (unsigned long) tem3;
969 (pointer_plus @0 (convert?@2 (minus@3 (convert @1) (convert @0))))
970 /* Conditionally look through a sign-changing conversion. */
971 (if (TYPE_PRECISION (TREE_TYPE (@2)) == TYPE_PRECISION (TREE_TYPE (@3))
972 && ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@1)))
973 || (GENERIC && type == TREE_TYPE (@1))))
977 tem = (sizetype) ptr;
981 and produce the simpler and easier to analyze with respect to alignment
982 ... = ptr & ~algn; */
984 (pointer_plus @0 (negate (bit_and (convert @0) INTEGER_CST@1)))
985 (with { tree algn = wide_int_to_tree (TREE_TYPE (@0), wi::bit_not (@1)); }
986 (bit_and @0 { algn; })))
988 /* Try folding difference of addresses. */
990 (minus (convert ADDR_EXPR@0) (convert @1))
991 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
992 (with { HOST_WIDE_INT diff; }
993 (if (ptr_difference_const (@0, @1, &diff))
994 { build_int_cst_type (type, diff); }))))
996 (minus (convert @0) (convert ADDR_EXPR@1))
997 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
998 (with { HOST_WIDE_INT diff; }
999 (if (ptr_difference_const (@0, @1, &diff))
1000 { build_int_cst_type (type, diff); }))))
1002 /* If arg0 is derived from the address of an object or function, we may
1003 be able to fold this expression using the object or function's
1006 (bit_and (convert? @0) INTEGER_CST@1)
1007 (if (POINTER_TYPE_P (TREE_TYPE (@0))
1008 && tree_nop_conversion_p (type, TREE_TYPE (@0)))
1012 unsigned HOST_WIDE_INT bitpos;
1013 get_pointer_alignment_1 (@0, &align, &bitpos);
1015 (if (wi::ltu_p (@1, align / BITS_PER_UNIT))
1016 { wide_int_to_tree (type, wi::bit_and (@1, bitpos / BITS_PER_UNIT)); }))))
1019 /* We can't reassociate at all for saturating types. */
1020 (if (!TYPE_SATURATING (type))
1022 /* Contract negates. */
1023 /* A + (-B) -> A - B */
1025 (plus:c (convert1? @0) (convert2? (negate @1)))
1026 /* Apply STRIP_NOPS on @0 and the negate. */
1027 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
1028 && tree_nop_conversion_p (type, TREE_TYPE (@1))
1029 && !TYPE_OVERFLOW_SANITIZED (type))
1030 (minus (convert @0) (convert @1))))
1031 /* A - (-B) -> A + B */
1033 (minus (convert1? @0) (convert2? (negate @1)))
1034 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
1035 && tree_nop_conversion_p (type, TREE_TYPE (@1))
1036 && !TYPE_OVERFLOW_SANITIZED (type))
1037 (plus (convert @0) (convert @1))))
1040 (negate (convert? (negate @1)))
1041 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1042 && !TYPE_OVERFLOW_SANITIZED (type))
1045 /* We can't reassociate floating-point unless -fassociative-math
1046 or fixed-point plus or minus because of saturation to +-Inf. */
1047 (if ((!FLOAT_TYPE_P (type) || flag_associative_math)
1048 && !FIXED_POINT_TYPE_P (type))
1050 /* Match patterns that allow contracting a plus-minus pair
1051 irrespective of overflow issues. */
1052 /* (A +- B) - A -> +- B */
1053 /* (A +- B) -+ B -> A */
1054 /* A - (A +- B) -> -+ B */
1055 /* A +- (B -+ A) -> +- B */
1057 (minus (plus:c @0 @1) @0)
1060 (minus (minus @0 @1) @0)
1063 (plus:c (minus @0 @1) @1)
1066 (minus @0 (plus:c @0 @1))
1069 (minus @0 (minus @0 @1))
1072 /* (A +- CST) +- CST -> A + CST */
1073 (for outer_op (plus minus)
1074 (for inner_op (plus minus)
1076 (outer_op (inner_op @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
1077 /* If the constant operation overflows we cannot do the transform
1078 as we would introduce undefined overflow, for example
1079 with (a - 1) + INT_MIN. */
1080 (with { tree cst = const_binop (outer_op == inner_op
1081 ? PLUS_EXPR : MINUS_EXPR, type, @1, @2); }
1082 (if (cst && !TREE_OVERFLOW (cst))
1083 (inner_op @0 { cst; } ))))))
1085 /* (CST - A) +- CST -> CST - A */
1086 (for outer_op (plus minus)
1088 (outer_op (minus CONSTANT_CLASS_P@1 @0) CONSTANT_CLASS_P@2)
1089 (with { tree cst = const_binop (outer_op, type, @1, @2); }
1090 (if (cst && !TREE_OVERFLOW (cst))
1091 (minus { cst; } @0)))))
1095 (plus:c (bit_not @0) @0)
1096 (if (!TYPE_OVERFLOW_TRAPS (type))
1097 { build_all_ones_cst (type); }))
1101 (plus (convert? (bit_not @0)) integer_each_onep)
1102 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1103 (negate (convert @0))))
1107 (minus (convert? (negate @0)) integer_each_onep)
1108 (if (!TYPE_OVERFLOW_TRAPS (type)
1109 && tree_nop_conversion_p (type, TREE_TYPE (@0)))
1110 (bit_not (convert @0))))
1114 (minus integer_all_onesp @0)
1117 /* (T)(P + A) - (T)P -> (T) A */
1118 (for add (plus pointer_plus)
1120 (minus (convert (add @0 @1))
1122 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
1123 /* For integer types, if A has a smaller type
1124 than T the result depends on the possible
1126 E.g. T=size_t, A=(unsigned)429497295, P>0.
1127 However, if an overflow in P + A would cause
1128 undefined behavior, we can assume that there
1130 || (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1131 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1132 /* For pointer types, if the conversion of A to the
1133 final type requires a sign- or zero-extension,
1134 then we have to punt - it is not defined which
1136 || (POINTER_TYPE_P (TREE_TYPE (@0))
1137 && TREE_CODE (@1) == INTEGER_CST
1138 && tree_int_cst_sign_bit (@1) == 0))
1141 /* (T)P - (T)(P + A) -> -(T) A */
1142 (for add (plus pointer_plus)
1145 (convert (add @0 @1)))
1146 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
1147 /* For integer types, if A has a smaller type
1148 than T the result depends on the possible
1150 E.g. T=size_t, A=(unsigned)429497295, P>0.
1151 However, if an overflow in P + A would cause
1152 undefined behavior, we can assume that there
1154 || (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1155 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1156 /* For pointer types, if the conversion of A to the
1157 final type requires a sign- or zero-extension,
1158 then we have to punt - it is not defined which
1160 || (POINTER_TYPE_P (TREE_TYPE (@0))
1161 && TREE_CODE (@1) == INTEGER_CST
1162 && tree_int_cst_sign_bit (@1) == 0))
1163 (negate (convert @1)))))
1165 /* (T)(P + A) - (T)(P + B) -> (T)A - (T)B */
1166 (for add (plus pointer_plus)
1168 (minus (convert (add @0 @1))
1169 (convert (add @0 @2)))
1170 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
1171 /* For integer types, if A has a smaller type
1172 than T the result depends on the possible
1174 E.g. T=size_t, A=(unsigned)429497295, P>0.
1175 However, if an overflow in P + A would cause
1176 undefined behavior, we can assume that there
1178 || (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1179 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1180 /* For pointer types, if the conversion of A to the
1181 final type requires a sign- or zero-extension,
1182 then we have to punt - it is not defined which
1184 || (POINTER_TYPE_P (TREE_TYPE (@0))
1185 && TREE_CODE (@1) == INTEGER_CST
1186 && tree_int_cst_sign_bit (@1) == 0
1187 && TREE_CODE (@2) == INTEGER_CST
1188 && tree_int_cst_sign_bit (@2) == 0))
1189 (minus (convert @1) (convert @2)))))))
1192 /* Simplifications of MIN_EXPR, MAX_EXPR, fmin() and fmax(). */
1194 (for minmax (min max FMIN FMAX)
1198 /* min(max(x,y),y) -> y. */
1200 (min:c (max:c @0 @1) @1)
1202 /* max(min(x,y),y) -> y. */
1204 (max:c (min:c @0 @1) @1)
1209 (if (INTEGRAL_TYPE_P (type)
1210 && TYPE_MIN_VALUE (type)
1211 && operand_equal_p (@1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
1213 (if (INTEGRAL_TYPE_P (type)
1214 && TYPE_MAX_VALUE (type)
1215 && operand_equal_p (@1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
1220 (if (INTEGRAL_TYPE_P (type)
1221 && TYPE_MAX_VALUE (type)
1222 && operand_equal_p (@1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
1224 (if (INTEGRAL_TYPE_P (type)
1225 && TYPE_MIN_VALUE (type)
1226 && operand_equal_p (@1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
1228 (for minmax (FMIN FMAX)
1229 /* If either argument is NaN, return the other one. Avoid the
1230 transformation if we get (and honor) a signalling NaN. */
1232 (minmax:c @0 REAL_CST@1)
1233 (if (real_isnan (TREE_REAL_CST_PTR (@1))
1234 && (!HONOR_SNANS (@1) || !TREE_REAL_CST (@1).signalling))
1236 /* Convert fmin/fmax to MIN_EXPR/MAX_EXPR. C99 requires these
1237 functions to return the numeric arg if the other one is NaN.
1238 MIN and MAX don't honor that, so only transform if -ffinite-math-only
1239 is set. C99 doesn't require -0.0 to be handled, so we don't have to
1240 worry about it either. */
1241 (if (flag_finite_math_only)
1248 /* min (-A, -B) -> -max (A, B) */
1249 (for minmax (min max FMIN FMAX)
1250 maxmin (max min FMAX FMIN)
1252 (minmax (negate:s@2 @0) (negate:s@3 @1))
1253 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
1254 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1255 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
1256 (negate (maxmin @0 @1)))))
1257 /* MIN (~X, ~Y) -> ~MAX (X, Y)
1258 MAX (~X, ~Y) -> ~MIN (X, Y) */
1259 (for minmax (min max)
1262 (minmax (bit_not:s@2 @0) (bit_not:s@3 @1))
1263 (bit_not (maxmin @0 @1))))
1265 /* Simplifications of shift and rotates. */
1267 (for rotate (lrotate rrotate)
1269 (rotate integer_all_onesp@0 @1)
1272 /* Optimize -1 >> x for arithmetic right shifts. */
1274 (rshift integer_all_onesp@0 @1)
1275 (if (!TYPE_UNSIGNED (type)
1276 && tree_expr_nonnegative_p (@1))
1279 /* Optimize (x >> c) << c into x & (-1<<c). */
1281 (lshift (rshift @0 INTEGER_CST@1) @1)
1282 (if (wi::ltu_p (@1, element_precision (type)))
1283 (bit_and @0 (lshift { build_minus_one_cst (type); } @1))))
1285 /* Optimize (x << c) >> c into x & ((unsigned)-1 >> c) for unsigned
1288 (rshift (lshift @0 INTEGER_CST@1) @1)
1289 (if (TYPE_UNSIGNED (type)
1290 && (wi::ltu_p (@1, element_precision (type))))
1291 (bit_and @0 (rshift { build_minus_one_cst (type); } @1))))
1293 (for shiftrotate (lrotate rrotate lshift rshift)
1295 (shiftrotate @0 integer_zerop)
1298 (shiftrotate integer_zerop@0 @1)
1300 /* Prefer vector1 << scalar to vector1 << vector2
1301 if vector2 is uniform. */
1302 (for vec (VECTOR_CST CONSTRUCTOR)
1304 (shiftrotate @0 vec@1)
1305 (with { tree tem = uniform_vector_p (@1); }
1307 (shiftrotate @0 { tem; }))))))
1309 /* Rewrite an LROTATE_EXPR by a constant into an
1310 RROTATE_EXPR by a new constant. */
1312 (lrotate @0 INTEGER_CST@1)
1313 (rrotate @0 { const_binop (MINUS_EXPR, TREE_TYPE (@1),
1314 build_int_cst (TREE_TYPE (@1),
1315 element_precision (type)), @1); }))
1317 /* Turn (a OP c1) OP c2 into a OP (c1+c2). */
1318 (for op (lrotate rrotate rshift lshift)
1320 (op (op @0 INTEGER_CST@1) INTEGER_CST@2)
1321 (with { unsigned int prec = element_precision (type); }
1322 (if (wi::ge_p (@1, 0, TYPE_SIGN (TREE_TYPE (@1)))
1323 && wi::lt_p (@1, prec, TYPE_SIGN (TREE_TYPE (@1)))
1324 && wi::ge_p (@2, 0, TYPE_SIGN (TREE_TYPE (@2)))
1325 && wi::lt_p (@2, prec, TYPE_SIGN (TREE_TYPE (@2))))
1326 (with { unsigned int low = wi::add (@1, @2).to_uhwi (); }
1327 /* Deal with a OP (c1 + c2) being undefined but (a OP c1) OP c2
1328 being well defined. */
1330 (if (op == LROTATE_EXPR || op == RROTATE_EXPR)
1331 (op @0 { build_int_cst (TREE_TYPE (@1), low % prec); })
1332 (if (TYPE_UNSIGNED (type) || op == LSHIFT_EXPR)
1333 { build_zero_cst (type); }
1334 (op @0 { build_int_cst (TREE_TYPE (@1), prec - 1); })))
1335 (op @0 { build_int_cst (TREE_TYPE (@1), low); })))))))
1338 /* ((1 << A) & 1) != 0 -> A == 0
1339 ((1 << A) & 1) == 0 -> A != 0 */
1343 (cmp (bit_and (lshift integer_onep @0) integer_onep) integer_zerop)
1344 (icmp @0 { build_zero_cst (TREE_TYPE (@0)); })))
1346 /* (CST1 << A) == CST2 -> A == ctz (CST2) - ctz (CST1)
1347 (CST1 << A) != CST2 -> A != ctz (CST2) - ctz (CST1)
1351 (cmp (lshift INTEGER_CST@0 @1) INTEGER_CST@2)
1352 (with { int cand = wi::ctz (@2) - wi::ctz (@0); }
1354 || (!integer_zerop (@2)
1355 && wi::ne_p (wi::lshift (@0, cand), @2)))
1356 { constant_boolean_node (cmp == NE_EXPR, type); }
1357 (if (!integer_zerop (@2)
1358 && wi::eq_p (wi::lshift (@0, cand), @2))
1359 (cmp @1 { build_int_cst (TREE_TYPE (@1), cand); }))))))
1361 /* Fold (X << C1) & C2 into (X << C1) & (C2 | ((1 << C1) - 1))
1362 (X >> C1) & C2 into (X >> C1) & (C2 | ~((type) -1 >> C1))
1363 if the new mask might be further optimized. */
1364 (for shift (lshift rshift)
1366 (bit_and (convert?:s@4 (shift:s@5 (convert1?@3 @0) INTEGER_CST@1))
1368 (if (tree_nop_conversion_p (TREE_TYPE (@4), TREE_TYPE (@5))
1369 && TYPE_PRECISION (type) <= HOST_BITS_PER_WIDE_INT
1370 && tree_fits_uhwi_p (@1)
1371 && tree_to_uhwi (@1) > 0
1372 && tree_to_uhwi (@1) < TYPE_PRECISION (type))
1375 unsigned int shiftc = tree_to_uhwi (@1);
1376 unsigned HOST_WIDE_INT mask = TREE_INT_CST_LOW (@2);
1377 unsigned HOST_WIDE_INT newmask, zerobits = 0;
1378 tree shift_type = TREE_TYPE (@3);
1381 if (shift == LSHIFT_EXPR)
1382 zerobits = ((((unsigned HOST_WIDE_INT) 1) << shiftc) - 1);
1383 else if (shift == RSHIFT_EXPR
1384 && (TYPE_PRECISION (shift_type)
1385 == GET_MODE_PRECISION (TYPE_MODE (shift_type))))
1387 prec = TYPE_PRECISION (TREE_TYPE (@3));
1389 /* See if more bits can be proven as zero because of
1392 && TYPE_UNSIGNED (TREE_TYPE (@0)))
1394 tree inner_type = TREE_TYPE (@0);
1395 if ((TYPE_PRECISION (inner_type)
1396 == GET_MODE_PRECISION (TYPE_MODE (inner_type)))
1397 && TYPE_PRECISION (inner_type) < prec)
1399 prec = TYPE_PRECISION (inner_type);
1400 /* See if we can shorten the right shift. */
1402 shift_type = inner_type;
1403 /* Otherwise X >> C1 is all zeros, so we'll optimize
1404 it into (X, 0) later on by making sure zerobits
1408 zerobits = ~(unsigned HOST_WIDE_INT) 0;
1411 zerobits >>= HOST_BITS_PER_WIDE_INT - shiftc;
1412 zerobits <<= prec - shiftc;
1414 /* For arithmetic shift if sign bit could be set, zerobits
1415 can contain actually sign bits, so no transformation is
1416 possible, unless MASK masks them all away. In that
1417 case the shift needs to be converted into logical shift. */
1418 if (!TYPE_UNSIGNED (TREE_TYPE (@3))
1419 && prec == TYPE_PRECISION (TREE_TYPE (@3)))
1421 if ((mask & zerobits) == 0)
1422 shift_type = unsigned_type_for (TREE_TYPE (@3));
1428 /* ((X << 16) & 0xff00) is (X, 0). */
1429 (if ((mask & zerobits) == mask)
1430 { build_int_cst (type, 0); }
1431 (with { newmask = mask | zerobits; }
1432 (if (newmask != mask && (newmask & (newmask + 1)) == 0)
1435 /* Only do the transformation if NEWMASK is some integer
1437 for (prec = BITS_PER_UNIT;
1438 prec < HOST_BITS_PER_WIDE_INT; prec <<= 1)
1439 if (newmask == (((unsigned HOST_WIDE_INT) 1) << prec) - 1)
1442 (if (prec < HOST_BITS_PER_WIDE_INT
1443 || newmask == ~(unsigned HOST_WIDE_INT) 0)
1445 { tree newmaskt = build_int_cst_type (TREE_TYPE (@2), newmask); }
1446 (if (!tree_int_cst_equal (newmaskt, @2))
1447 (if (shift_type != TREE_TYPE (@3))
1448 (bit_and (convert (shift:shift_type (convert @3) @1)) { newmaskt; })
1449 (bit_and @4 { newmaskt; })))))))))))))
1451 /* Fold (X {&,^,|} C2) << C1 into (X << C1) {&,^,|} (C2 << C1)
1452 (X {&,^,|} C2) >> C1 into (X >> C1) & (C2 >> C1). */
1453 (for shift (lshift rshift)
1454 (for bit_op (bit_and bit_xor bit_ior)
1456 (shift (convert?:s (bit_op:s @0 INTEGER_CST@2)) INTEGER_CST@1)
1457 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1458 (with { tree mask = int_const_binop (shift, fold_convert (type, @2), @1); }
1459 (bit_op (shift (convert @0) @1) { mask; }))))))
1462 /* Simplifications of conversions. */
1464 /* Basic strip-useless-type-conversions / strip_nops. */
1465 (for cvt (convert view_convert float fix_trunc)
1468 (if ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@0)))
1469 || (GENERIC && type == TREE_TYPE (@0)))
1472 /* Contract view-conversions. */
1474 (view_convert (view_convert @0))
1477 /* For integral conversions with the same precision or pointer
1478 conversions use a NOP_EXPR instead. */
1481 (if ((INTEGRAL_TYPE_P (type) || POINTER_TYPE_P (type))
1482 && (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
1483 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (@0)))
1486 /* Strip inner integral conversions that do not change precision or size. */
1488 (view_convert (convert@0 @1))
1489 (if ((INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
1490 && (INTEGRAL_TYPE_P (TREE_TYPE (@1)) || POINTER_TYPE_P (TREE_TYPE (@1)))
1491 && (TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
1492 && (TYPE_SIZE (TREE_TYPE (@0)) == TYPE_SIZE (TREE_TYPE (@1))))
1495 /* Re-association barriers around constants and other re-association
1496 barriers can be removed. */
1498 (paren CONSTANT_CLASS_P@0)
1501 (paren (paren@1 @0))
1504 /* Handle cases of two conversions in a row. */
1505 (for ocvt (convert float fix_trunc)
1506 (for icvt (convert float)
1511 tree inside_type = TREE_TYPE (@0);
1512 tree inter_type = TREE_TYPE (@1);
1513 int inside_int = INTEGRAL_TYPE_P (inside_type);
1514 int inside_ptr = POINTER_TYPE_P (inside_type);
1515 int inside_float = FLOAT_TYPE_P (inside_type);
1516 int inside_vec = VECTOR_TYPE_P (inside_type);
1517 unsigned int inside_prec = TYPE_PRECISION (inside_type);
1518 int inside_unsignedp = TYPE_UNSIGNED (inside_type);
1519 int inter_int = INTEGRAL_TYPE_P (inter_type);
1520 int inter_ptr = POINTER_TYPE_P (inter_type);
1521 int inter_float = FLOAT_TYPE_P (inter_type);
1522 int inter_vec = VECTOR_TYPE_P (inter_type);
1523 unsigned int inter_prec = TYPE_PRECISION (inter_type);
1524 int inter_unsignedp = TYPE_UNSIGNED (inter_type);
1525 int final_int = INTEGRAL_TYPE_P (type);
1526 int final_ptr = POINTER_TYPE_P (type);
1527 int final_float = FLOAT_TYPE_P (type);
1528 int final_vec = VECTOR_TYPE_P (type);
1529 unsigned int final_prec = TYPE_PRECISION (type);
1530 int final_unsignedp = TYPE_UNSIGNED (type);
1533 /* In addition to the cases of two conversions in a row
1534 handled below, if we are converting something to its own
1535 type via an object of identical or wider precision, neither
1536 conversion is needed. */
1537 (if (((GIMPLE && useless_type_conversion_p (type, inside_type))
1539 && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (inside_type)))
1540 && (((inter_int || inter_ptr) && final_int)
1541 || (inter_float && final_float))
1542 && inter_prec >= final_prec)
1545 /* Likewise, if the intermediate and initial types are either both
1546 float or both integer, we don't need the middle conversion if the
1547 former is wider than the latter and doesn't change the signedness
1548 (for integers). Avoid this if the final type is a pointer since
1549 then we sometimes need the middle conversion. Likewise if the
1550 final type has a precision not equal to the size of its mode. */
1551 (if (((inter_int && inside_int) || (inter_float && inside_float))
1552 && (final_int || final_float)
1553 && inter_prec >= inside_prec
1554 && (inter_float || inter_unsignedp == inside_unsignedp)
1555 && ! (final_prec != GET_MODE_PRECISION (TYPE_MODE (type))
1556 && TYPE_MODE (type) == TYPE_MODE (inter_type)))
1559 /* If we have a sign-extension of a zero-extended value, we can
1560 replace that by a single zero-extension. Likewise if the
1561 final conversion does not change precision we can drop the
1562 intermediate conversion. */
1563 (if (inside_int && inter_int && final_int
1564 && ((inside_prec < inter_prec && inter_prec < final_prec
1565 && inside_unsignedp && !inter_unsignedp)
1566 || final_prec == inter_prec))
1569 /* Two conversions in a row are not needed unless:
1570 - some conversion is floating-point (overstrict for now), or
1571 - some conversion is a vector (overstrict for now), or
1572 - the intermediate type is narrower than both initial and
1574 - the intermediate type and innermost type differ in signedness,
1575 and the outermost type is wider than the intermediate, or
1576 - the initial type is a pointer type and the precisions of the
1577 intermediate and final types differ, or
1578 - the final type is a pointer type and the precisions of the
1579 initial and intermediate types differ. */
1580 (if (! inside_float && ! inter_float && ! final_float
1581 && ! inside_vec && ! inter_vec && ! final_vec
1582 && (inter_prec >= inside_prec || inter_prec >= final_prec)
1583 && ! (inside_int && inter_int
1584 && inter_unsignedp != inside_unsignedp
1585 && inter_prec < final_prec)
1586 && ((inter_unsignedp && inter_prec > inside_prec)
1587 == (final_unsignedp && final_prec > inter_prec))
1588 && ! (inside_ptr && inter_prec != final_prec)
1589 && ! (final_ptr && inside_prec != inter_prec)
1590 && ! (final_prec != GET_MODE_PRECISION (TYPE_MODE (type))
1591 && TYPE_MODE (type) == TYPE_MODE (inter_type)))
1594 /* A truncation to an unsigned type (a zero-extension) should be
1595 canonicalized as bitwise and of a mask. */
1596 (if (GIMPLE /* PR70366: doing this in GENERIC breaks -Wconversion. */
1597 && final_int && inter_int && inside_int
1598 && final_prec == inside_prec
1599 && final_prec > inter_prec
1601 (convert (bit_and @0 { wide_int_to_tree
1603 wi::mask (inter_prec, false,
1604 TYPE_PRECISION (inside_type))); })))
1606 /* If we are converting an integer to a floating-point that can
1607 represent it exactly and back to an integer, we can skip the
1608 floating-point conversion. */
1609 (if (GIMPLE /* PR66211 */
1610 && inside_int && inter_float && final_int &&
1611 (unsigned) significand_size (TYPE_MODE (inter_type))
1612 >= inside_prec - !inside_unsignedp)
1615 /* If we have a narrowing conversion to an integral type that is fed by a
1616 BIT_AND_EXPR, we might be able to remove the BIT_AND_EXPR if it merely
1617 masks off bits outside the final type (and nothing else). */
1619 (convert (bit_and @0 INTEGER_CST@1))
1620 (if (INTEGRAL_TYPE_P (type)
1621 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
1622 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))
1623 && operand_equal_p (@1, build_low_bits_mask (TREE_TYPE (@1),
1624 TYPE_PRECISION (type)), 0))
1628 /* (X /[ex] A) * A -> X. */
1630 (mult (convert? (exact_div @0 @1)) @1)
1631 /* Look through a sign-changing conversion. */
1634 /* Canonicalization of binary operations. */
1636 /* Convert X + -C into X - C. */
1638 (plus @0 REAL_CST@1)
1639 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
1640 (with { tree tem = const_unop (NEGATE_EXPR, type, @1); }
1641 (if (!TREE_OVERFLOW (tem) || !flag_trapping_math)
1642 (minus @0 { tem; })))))
1644 /* Convert x+x into x*2. */
1647 (if (SCALAR_FLOAT_TYPE_P (type))
1648 (mult @0 { build_real (type, dconst2); })
1649 (if (INTEGRAL_TYPE_P (type))
1650 (mult @0 { build_int_cst (type, 2); }))))
1653 (minus integer_zerop @1)
1656 /* (ARG0 - ARG1) is the same as (-ARG1 + ARG0). So check whether
1657 ARG0 is zero and X + ARG0 reduces to X, since that would mean
1658 (-ARG1 + ARG0) reduces to -ARG1. */
1660 (minus real_zerop@0 @1)
1661 (if (fold_real_zero_addition_p (type, @0, 0))
1664 /* Transform x * -1 into -x. */
1666 (mult @0 integer_minus_onep)
1669 /* True if we can easily extract the real and imaginary parts of a complex
1671 (match compositional_complex
1672 (convert? (complex @0 @1)))
1674 /* COMPLEX_EXPR and REALPART/IMAGPART_EXPR cancellations. */
1676 (complex (realpart @0) (imagpart @0))
1679 (realpart (complex @0 @1))
1682 (imagpart (complex @0 @1))
1685 /* Sometimes we only care about half of a complex expression. */
1687 (realpart (convert?:s (conj:s @0)))
1688 (convert (realpart @0)))
1690 (imagpart (convert?:s (conj:s @0)))
1691 (convert (negate (imagpart @0))))
1692 (for part (realpart imagpart)
1693 (for op (plus minus)
1695 (part (convert?:s@2 (op:s @0 @1)))
1696 (convert (op (part @0) (part @1))))))
1698 (realpart (convert?:s (CEXPI:s @0)))
1701 (imagpart (convert?:s (CEXPI:s @0)))
1704 /* conj(conj(x)) -> x */
1706 (conj (convert? (conj @0)))
1707 (if (tree_nop_conversion_p (TREE_TYPE (@0), type))
1710 /* conj({x,y}) -> {x,-y} */
1712 (conj (convert?:s (complex:s @0 @1)))
1713 (with { tree itype = TREE_TYPE (type); }
1714 (complex (convert:itype @0) (negate (convert:itype @1)))))
1716 /* BSWAP simplifications, transforms checked by gcc.dg/builtin-bswap-8.c. */
1717 (for bswap (BUILT_IN_BSWAP16 BUILT_IN_BSWAP32 BUILT_IN_BSWAP64)
1722 (bswap (bit_not (bswap @0)))
1724 (for bitop (bit_xor bit_ior bit_and)
1726 (bswap (bitop:c (bswap @0) @1))
1727 (bitop @0 (bswap @1)))))
1730 /* Combine COND_EXPRs and VEC_COND_EXPRs. */
1732 /* Simplify constant conditions.
1733 Only optimize constant conditions when the selected branch
1734 has the same type as the COND_EXPR. This avoids optimizing
1735 away "c ? x : throw", where the throw has a void type.
1736 Note that we cannot throw away the fold-const.c variant nor
1737 this one as we depend on doing this transform before possibly
1738 A ? B : B -> B triggers and the fold-const.c one can optimize
1739 0 ? A : B to B even if A has side-effects. Something
1740 genmatch cannot handle. */
1742 (cond INTEGER_CST@0 @1 @2)
1743 (if (integer_zerop (@0))
1744 (if (!VOID_TYPE_P (TREE_TYPE (@2)) || VOID_TYPE_P (type))
1746 (if (!VOID_TYPE_P (TREE_TYPE (@1)) || VOID_TYPE_P (type))
1749 (vec_cond VECTOR_CST@0 @1 @2)
1750 (if (integer_all_onesp (@0))
1752 (if (integer_zerop (@0))
1755 (for cnd (cond vec_cond)
1756 /* A ? B : (A ? X : C) -> A ? B : C. */
1758 (cnd @0 (cnd @0 @1 @2) @3)
1761 (cnd @0 @1 (cnd @0 @2 @3))
1763 /* A ? B : (!A ? C : X) -> A ? B : C. */
1764 /* ??? This matches embedded conditions open-coded because genmatch
1765 would generate matching code for conditions in separate stmts only.
1766 The following is still important to merge then and else arm cases
1767 from if-conversion. */
1769 (cnd @0 @1 (cnd @2 @3 @4))
1770 (if (COMPARISON_CLASS_P (@0)
1771 && COMPARISON_CLASS_P (@2)
1772 && invert_tree_comparison
1773 (TREE_CODE (@0), HONOR_NANS (TREE_OPERAND (@0, 0))) == TREE_CODE (@2)
1774 && operand_equal_p (TREE_OPERAND (@0, 0), TREE_OPERAND (@2, 0), 0)
1775 && operand_equal_p (TREE_OPERAND (@0, 1), TREE_OPERAND (@2, 1), 0))
1778 (cnd @0 (cnd @1 @2 @3) @4)
1779 (if (COMPARISON_CLASS_P (@0)
1780 && COMPARISON_CLASS_P (@1)
1781 && invert_tree_comparison
1782 (TREE_CODE (@0), HONOR_NANS (TREE_OPERAND (@0, 0))) == TREE_CODE (@1)
1783 && operand_equal_p (TREE_OPERAND (@0, 0), TREE_OPERAND (@1, 0), 0)
1784 && operand_equal_p (TREE_OPERAND (@0, 1), TREE_OPERAND (@1, 1), 0))
1787 /* A ? B : B -> B. */
1792 /* !A ? B : C -> A ? C : B. */
1794 (cnd (logical_inverted_value truth_valued_p@0) @1 @2)
1797 /* A + (B vcmp C ? 1 : 0) -> A - (B vcmp C ? -1 : 0), since vector comparisons
1798 return all -1 or all 0 results. */
1799 /* ??? We could instead convert all instances of the vec_cond to negate,
1800 but that isn't necessarily a win on its own. */
1802 (plus:c @3 (view_convert? (vec_cond:s @0 integer_each_onep@1 integer_zerop@2)))
1803 (if (VECTOR_TYPE_P (type)
1804 && TYPE_VECTOR_SUBPARTS (type) == TYPE_VECTOR_SUBPARTS (TREE_TYPE (@1))
1805 && (TYPE_MODE (TREE_TYPE (type))
1806 == TYPE_MODE (TREE_TYPE (TREE_TYPE (@1)))))
1807 (minus @3 (view_convert (vec_cond @0 (negate @1) @2)))))
1809 /* ... likewise A - (B vcmp C ? 1 : 0) -> A + (B vcmp C ? -1 : 0). */
1811 (minus @3 (view_convert? (vec_cond:s @0 integer_each_onep@1 integer_zerop@2)))
1812 (if (VECTOR_TYPE_P (type)
1813 && TYPE_VECTOR_SUBPARTS (type) == TYPE_VECTOR_SUBPARTS (TREE_TYPE (@1))
1814 && (TYPE_MODE (TREE_TYPE (type))
1815 == TYPE_MODE (TREE_TYPE (TREE_TYPE (@1)))))
1816 (plus @3 (view_convert (vec_cond @0 (negate @1) @2)))))
1819 /* Simplifications of comparisons. */
1821 /* See if we can reduce the magnitude of a constant involved in a
1822 comparison by changing the comparison code. This is a canonicalization
1823 formerly done by maybe_canonicalize_comparison_1. */
1827 (cmp @0 INTEGER_CST@1)
1828 (if (tree_int_cst_sgn (@1) == -1)
1829 (acmp @0 { wide_int_to_tree (TREE_TYPE (@1), wi::add (@1, 1)); }))))
1833 (cmp @0 INTEGER_CST@1)
1834 (if (tree_int_cst_sgn (@1) == 1)
1835 (acmp @0 { wide_int_to_tree (TREE_TYPE (@1), wi::sub (@1, 1)); }))))
1838 /* We can simplify a logical negation of a comparison to the
1839 inverted comparison. As we cannot compute an expression
1840 operator using invert_tree_comparison we have to simulate
1841 that with expression code iteration. */
1842 (for cmp (tcc_comparison)
1843 icmp (inverted_tcc_comparison)
1844 ncmp (inverted_tcc_comparison_with_nans)
1845 /* Ideally we'd like to combine the following two patterns
1846 and handle some more cases by using
1847 (logical_inverted_value (cmp @0 @1))
1848 here but for that genmatch would need to "inline" that.
1849 For now implement what forward_propagate_comparison did. */
1851 (bit_not (cmp @0 @1))
1852 (if (VECTOR_TYPE_P (type)
1853 || (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1))
1854 /* Comparison inversion may be impossible for trapping math,
1855 invert_tree_comparison will tell us. But we can't use
1856 a computed operator in the replacement tree thus we have
1857 to play the trick below. */
1858 (with { enum tree_code ic = invert_tree_comparison
1859 (cmp, HONOR_NANS (@0)); }
1865 (bit_xor (cmp @0 @1) integer_truep)
1866 (with { enum tree_code ic = invert_tree_comparison
1867 (cmp, HONOR_NANS (@0)); }
1873 /* Transform comparisons of the form X - Y CMP 0 to X CMP Y.
1874 ??? The transformation is valid for the other operators if overflow
1875 is undefined for the type, but performing it here badly interacts
1876 with the transformation in fold_cond_expr_with_comparison which
1877 attempts to synthetize ABS_EXPR. */
1880 (cmp (minus@2 @0 @1) integer_zerop)
1881 (if (single_use (@2))
1884 /* Transform comparisons of the form X * C1 CMP 0 to X CMP 0 in the
1885 signed arithmetic case. That form is created by the compiler
1886 often enough for folding it to be of value. One example is in
1887 computing loop trip counts after Operator Strength Reduction. */
1888 (for cmp (simple_comparison)
1889 scmp (swapped_simple_comparison)
1891 (cmp (mult@3 @0 INTEGER_CST@1) integer_zerop@2)
1892 /* Handle unfolded multiplication by zero. */
1893 (if (integer_zerop (@1))
1895 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1896 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1898 /* If @1 is negative we swap the sense of the comparison. */
1899 (if (tree_int_cst_sgn (@1) < 0)
1903 /* Simplify comparison of something with itself. For IEEE
1904 floating-point, we can only do some of these simplifications. */
1908 (if (! FLOAT_TYPE_P (TREE_TYPE (@0))
1909 || ! HONOR_NANS (@0))
1910 { constant_boolean_node (true, type); }
1911 (if (cmp != EQ_EXPR)
1917 || ! FLOAT_TYPE_P (TREE_TYPE (@0))
1918 || ! HONOR_NANS (@0))
1919 { constant_boolean_node (false, type); })))
1920 (for cmp (unle unge uneq)
1923 { constant_boolean_node (true, type); }))
1924 (for cmp (unlt ungt)
1930 (if (!flag_trapping_math)
1931 { constant_boolean_node (false, type); }))
1933 /* Fold ~X op ~Y as Y op X. */
1934 (for cmp (simple_comparison)
1936 (cmp (bit_not@2 @0) (bit_not@3 @1))
1937 (if (single_use (@2) && single_use (@3))
1940 /* Fold ~X op C as X op' ~C, where op' is the swapped comparison. */
1941 (for cmp (simple_comparison)
1942 scmp (swapped_simple_comparison)
1944 (cmp (bit_not@2 @0) CONSTANT_CLASS_P@1)
1945 (if (single_use (@2)
1946 && (TREE_CODE (@1) == INTEGER_CST || TREE_CODE (@1) == VECTOR_CST))
1947 (scmp @0 (bit_not @1)))))
1949 (for cmp (simple_comparison)
1950 /* Fold (double)float1 CMP (double)float2 into float1 CMP float2. */
1952 (cmp (convert@2 @0) (convert? @1))
1953 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
1954 && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@2))
1955 == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0)))
1956 && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@2))
1957 == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1))))
1960 tree type1 = TREE_TYPE (@1);
1961 if (TREE_CODE (@1) == REAL_CST && !DECIMAL_FLOAT_TYPE_P (type1))
1963 REAL_VALUE_TYPE orig = TREE_REAL_CST (@1);
1964 if (TYPE_PRECISION (type1) > TYPE_PRECISION (float_type_node)
1965 && exact_real_truncate (TYPE_MODE (float_type_node), &orig))
1966 type1 = float_type_node;
1967 if (TYPE_PRECISION (type1) > TYPE_PRECISION (double_type_node)
1968 && exact_real_truncate (TYPE_MODE (double_type_node), &orig))
1969 type1 = double_type_node;
1972 = (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type1)
1973 ? TREE_TYPE (@0) : type1);
1975 (if (TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (newtype))
1976 (cmp (convert:newtype @0) (convert:newtype @1))))))
1980 /* IEEE doesn't distinguish +0 and -0 in comparisons. */
1982 /* a CMP (-0) -> a CMP 0 */
1983 (if (REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@1)))
1984 (cmp @0 { build_real (TREE_TYPE (@1), dconst0); }))
1985 /* x != NaN is always true, other ops are always false. */
1986 (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
1987 && ! HONOR_SNANS (@1))
1988 { constant_boolean_node (cmp == NE_EXPR, type); })
1989 /* Fold comparisons against infinity. */
1990 (if (REAL_VALUE_ISINF (TREE_REAL_CST (@1))
1991 && MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (@1))))
1994 REAL_VALUE_TYPE max;
1995 enum tree_code code = cmp;
1996 bool neg = REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1));
1998 code = swap_tree_comparison (code);
2001 /* x > +Inf is always false, if with ignore sNANs. */
2002 (if (code == GT_EXPR
2003 && ! HONOR_SNANS (@0))
2004 { constant_boolean_node (false, type); })
2005 (if (code == LE_EXPR)
2006 /* x <= +Inf is always true, if we don't case about NaNs. */
2007 (if (! HONOR_NANS (@0))
2008 { constant_boolean_node (true, type); }
2009 /* x <= +Inf is the same as x == x, i.e. !isnan(x). */
2011 /* x == +Inf and x >= +Inf are always equal to x > DBL_MAX. */
2012 (if (code == EQ_EXPR || code == GE_EXPR)
2013 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
2015 (lt @0 { build_real (TREE_TYPE (@0), max); })
2016 (gt @0 { build_real (TREE_TYPE (@0), max); }))))
2017 /* x < +Inf is always equal to x <= DBL_MAX. */
2018 (if (code == LT_EXPR)
2019 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
2021 (ge @0 { build_real (TREE_TYPE (@0), max); })
2022 (le @0 { build_real (TREE_TYPE (@0), max); }))))
2023 /* x != +Inf is always equal to !(x > DBL_MAX). */
2024 (if (code == NE_EXPR)
2025 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
2026 (if (! HONOR_NANS (@0))
2028 (ge @0 { build_real (TREE_TYPE (@0), max); })
2029 (le @0 { build_real (TREE_TYPE (@0), max); }))
2031 (bit_xor (lt @0 { build_real (TREE_TYPE (@0), max); })
2032 { build_one_cst (type); })
2033 (bit_xor (gt @0 { build_real (TREE_TYPE (@0), max); })
2034 { build_one_cst (type); }))))))))))
2036 /* If this is a comparison of a real constant with a PLUS_EXPR
2037 or a MINUS_EXPR of a real constant, we can convert it into a
2038 comparison with a revised real constant as long as no overflow
2039 occurs when unsafe_math_optimizations are enabled. */
2040 (if (flag_unsafe_math_optimizations)
2041 (for op (plus minus)
2043 (cmp (op @0 REAL_CST@1) REAL_CST@2)
2046 tree tem = const_binop (op == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR,
2047 TREE_TYPE (@1), @2, @1);
2049 (if (tem && !TREE_OVERFLOW (tem))
2050 (cmp @0 { tem; }))))))
2052 /* Likewise, we can simplify a comparison of a real constant with
2053 a MINUS_EXPR whose first operand is also a real constant, i.e.
2054 (c1 - x) < c2 becomes x > c1-c2. Reordering is allowed on
2055 floating-point types only if -fassociative-math is set. */
2056 (if (flag_associative_math)
2058 (cmp (minus REAL_CST@0 @1) REAL_CST@2)
2059 (with { tree tem = const_binop (MINUS_EXPR, TREE_TYPE (@1), @0, @2); }
2060 (if (tem && !TREE_OVERFLOW (tem))
2061 (cmp { tem; } @1)))))
2063 /* Fold comparisons against built-in math functions. */
2064 (if (flag_unsafe_math_optimizations
2065 && ! flag_errno_math)
2068 (cmp (sq @0) REAL_CST@1)
2070 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
2072 /* sqrt(x) < y is always false, if y is negative. */
2073 (if (cmp == EQ_EXPR || cmp == LT_EXPR || cmp == LE_EXPR)
2074 { constant_boolean_node (false, type); })
2075 /* sqrt(x) > y is always true, if y is negative and we
2076 don't care about NaNs, i.e. negative values of x. */
2077 (if (cmp == NE_EXPR || !HONOR_NANS (@0))
2078 { constant_boolean_node (true, type); })
2079 /* sqrt(x) > y is the same as x >= 0, if y is negative. */
2080 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })))
2081 (if (real_equal (TREE_REAL_CST_PTR (@1), &dconst0))
2083 /* sqrt(x) < 0 is always false. */
2084 (if (cmp == LT_EXPR)
2085 { constant_boolean_node (false, type); })
2086 /* sqrt(x) >= 0 is always true if we don't care about NaNs. */
2087 (if (cmp == GE_EXPR && !HONOR_NANS (@0))
2088 { constant_boolean_node (true, type); })
2089 /* sqrt(x) <= 0 -> x == 0. */
2090 (if (cmp == LE_EXPR)
2092 /* Otherwise sqrt(x) cmp 0 -> x cmp 0. Here cmp can be >=, >,
2093 == or !=. In the last case:
2095 (sqrt(x) != 0) == (NaN != 0) == true == (x != 0)
2097 if x is negative or NaN. Due to -funsafe-math-optimizations,
2098 the results for other x follow from natural arithmetic. */
2100 (if (cmp == GT_EXPR || cmp == GE_EXPR)
2104 real_arithmetic (&c2, MULT_EXPR,
2105 &TREE_REAL_CST (@1), &TREE_REAL_CST (@1));
2106 real_convert (&c2, TYPE_MODE (TREE_TYPE (@0)), &c2);
2108 (if (REAL_VALUE_ISINF (c2))
2109 /* sqrt(x) > y is x == +Inf, when y is very large. */
2110 (if (HONOR_INFINITIES (@0))
2111 (eq @0 { build_real (TREE_TYPE (@0), c2); })
2112 { constant_boolean_node (false, type); })
2113 /* sqrt(x) > c is the same as x > c*c. */
2114 (cmp @0 { build_real (TREE_TYPE (@0), c2); }))))
2115 (if (cmp == LT_EXPR || cmp == LE_EXPR)
2119 real_arithmetic (&c2, MULT_EXPR,
2120 &TREE_REAL_CST (@1), &TREE_REAL_CST (@1));
2121 real_convert (&c2, TYPE_MODE (TREE_TYPE (@0)), &c2);
2123 (if (REAL_VALUE_ISINF (c2))
2125 /* sqrt(x) < y is always true, when y is a very large
2126 value and we don't care about NaNs or Infinities. */
2127 (if (! HONOR_NANS (@0) && ! HONOR_INFINITIES (@0))
2128 { constant_boolean_node (true, type); })
2129 /* sqrt(x) < y is x != +Inf when y is very large and we
2130 don't care about NaNs. */
2131 (if (! HONOR_NANS (@0))
2132 (ne @0 { build_real (TREE_TYPE (@0), c2); }))
2133 /* sqrt(x) < y is x >= 0 when y is very large and we
2134 don't care about Infinities. */
2135 (if (! HONOR_INFINITIES (@0))
2136 (ge @0 { build_real (TREE_TYPE (@0), dconst0); }))
2137 /* sqrt(x) < y is x >= 0 && x != +Inf, when y is large. */
2140 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
2141 (ne @0 { build_real (TREE_TYPE (@0), c2); }))))
2142 /* sqrt(x) < c is the same as x < c*c, if we ignore NaNs. */
2143 (if (! HONOR_NANS (@0))
2144 (cmp @0 { build_real (TREE_TYPE (@0), c2); })
2145 /* sqrt(x) < c is the same as x >= 0 && x < c*c. */
2148 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
2149 (cmp @0 { build_real (TREE_TYPE (@0), c2); }))))))))))))
2151 /* Unordered tests if either argument is a NaN. */
2153 (bit_ior (unordered @0 @0) (unordered @1 @1))
2154 (if (types_match (@0, @1))
2157 (bit_and (ordered @0 @0) (ordered @1 @1))
2158 (if (types_match (@0, @1))
2161 (bit_ior:c (unordered @0 @0) (unordered:c@2 @0 @1))
2164 (bit_and:c (ordered @0 @0) (ordered:c@2 @0 @1))
2167 /* Simple range test simplifications. */
2168 /* A < B || A >= B -> true. */
2169 (for test1 (lt le le le ne ge)
2170 test2 (ge gt ge ne eq ne)
2172 (bit_ior:c (test1 @0 @1) (test2 @0 @1))
2173 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2174 || VECTOR_INTEGER_TYPE_P (TREE_TYPE (@0)))
2175 { constant_boolean_node (true, type); })))
2176 /* A < B && A >= B -> false. */
2177 (for test1 (lt lt lt le ne eq)
2178 test2 (ge gt eq gt eq gt)
2180 (bit_and:c (test1 @0 @1) (test2 @0 @1))
2181 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2182 || VECTOR_INTEGER_TYPE_P (TREE_TYPE (@0)))
2183 { constant_boolean_node (false, type); })))
2185 /* -A CMP -B -> B CMP A. */
2186 (for cmp (tcc_comparison)
2187 scmp (swapped_tcc_comparison)
2189 (cmp (negate @0) (negate @1))
2190 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
2191 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2192 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
2195 (cmp (negate @0) CONSTANT_CLASS_P@1)
2196 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
2197 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2198 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
2199 (with { tree tem = const_unop (NEGATE_EXPR, TREE_TYPE (@0), @1); }
2200 (if (tem && !TREE_OVERFLOW (tem))
2201 (scmp @0 { tem; }))))))
2203 /* Convert ABS_EXPR<x> == 0 or ABS_EXPR<x> != 0 to x == 0 or x != 0. */
2206 (op (abs @0) zerop@1)
2209 /* From fold_sign_changed_comparison and fold_widened_comparison. */
2210 (for cmp (simple_comparison)
2212 (cmp (convert@0 @00) (convert?@1 @10))
2213 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2214 /* Disable this optimization if we're casting a function pointer
2215 type on targets that require function pointer canonicalization. */
2216 && !(targetm.have_canonicalize_funcptr_for_compare ()
2217 && TREE_CODE (TREE_TYPE (@00)) == POINTER_TYPE
2218 && TREE_CODE (TREE_TYPE (TREE_TYPE (@00))) == FUNCTION_TYPE)
2220 (if (TYPE_PRECISION (TREE_TYPE (@00)) == TYPE_PRECISION (TREE_TYPE (@0))
2221 && (TREE_CODE (@10) == INTEGER_CST
2222 || (@1 != @10 && types_match (TREE_TYPE (@10), TREE_TYPE (@00))))
2223 && (TYPE_UNSIGNED (TREE_TYPE (@00)) == TYPE_UNSIGNED (TREE_TYPE (@0))
2226 && (POINTER_TYPE_P (TREE_TYPE (@00)) == POINTER_TYPE_P (TREE_TYPE (@0))))
2227 /* ??? The special-casing of INTEGER_CST conversion was in the original
2228 code and here to avoid a spurious overflow flag on the resulting
2229 constant which fold_convert produces. */
2230 (if (TREE_CODE (@1) == INTEGER_CST)
2231 (cmp @00 { force_fit_type (TREE_TYPE (@00), wi::to_widest (@1), 0,
2232 TREE_OVERFLOW (@1)); })
2233 (cmp @00 (convert @1)))
2235 (if (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (TREE_TYPE (@00)))
2236 /* If possible, express the comparison in the shorter mode. */
2237 (if ((cmp == EQ_EXPR || cmp == NE_EXPR
2238 || TYPE_UNSIGNED (TREE_TYPE (@0)) == TYPE_UNSIGNED (TREE_TYPE (@00)))
2239 && (types_match (TREE_TYPE (@10), TREE_TYPE (@00))
2240 || ((TYPE_PRECISION (TREE_TYPE (@00))
2241 >= TYPE_PRECISION (TREE_TYPE (@10)))
2242 && (TYPE_UNSIGNED (TREE_TYPE (@00))
2243 == TYPE_UNSIGNED (TREE_TYPE (@10))))
2244 || (TREE_CODE (@10) == INTEGER_CST
2245 && INTEGRAL_TYPE_P (TREE_TYPE (@00))
2246 && int_fits_type_p (@10, TREE_TYPE (@00)))))
2247 (cmp @00 (convert @10))
2248 (if (TREE_CODE (@10) == INTEGER_CST
2249 && INTEGRAL_TYPE_P (TREE_TYPE (@00))
2250 && !int_fits_type_p (@10, TREE_TYPE (@00)))
2253 tree min = lower_bound_in_type (TREE_TYPE (@10), TREE_TYPE (@00));
2254 tree max = upper_bound_in_type (TREE_TYPE (@10), TREE_TYPE (@00));
2255 bool above = integer_nonzerop (const_binop (LT_EXPR, type, max, @10));
2256 bool below = integer_nonzerop (const_binop (LT_EXPR, type, @10, min));
2258 (if (above || below)
2259 (if (cmp == EQ_EXPR || cmp == NE_EXPR)
2260 { constant_boolean_node (cmp == EQ_EXPR ? false : true, type); }
2261 (if (cmp == LT_EXPR || cmp == LE_EXPR)
2262 { constant_boolean_node (above ? true : false, type); }
2263 (if (cmp == GT_EXPR || cmp == GE_EXPR)
2264 { constant_boolean_node (above ? false : true, type); }))))))))))))
2267 /* A local variable can never be pointed to by
2268 the default SSA name of an incoming parameter.
2269 SSA names are canonicalized to 2nd place. */
2271 (cmp addr@0 SSA_NAME@1)
2272 (if (SSA_NAME_IS_DEFAULT_DEF (@1)
2273 && TREE_CODE (SSA_NAME_VAR (@1)) == PARM_DECL)
2274 (with { tree base = get_base_address (TREE_OPERAND (@0, 0)); }
2275 (if (TREE_CODE (base) == VAR_DECL
2276 && auto_var_in_fn_p (base, current_function_decl))
2277 (if (cmp == NE_EXPR)
2278 { constant_boolean_node (true, type); }
2279 { constant_boolean_node (false, type); }))))))
2281 /* Equality compare simplifications from fold_binary */
2284 /* If we have (A | C) == D where C & ~D != 0, convert this into 0.
2285 Similarly for NE_EXPR. */
2287 (cmp (convert?@3 (bit_ior @0 INTEGER_CST@1)) INTEGER_CST@2)
2288 (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0))
2289 && wi::bit_and_not (@1, @2) != 0)
2290 { constant_boolean_node (cmp == NE_EXPR, type); }))
2292 /* (X ^ Y) == 0 becomes X == Y, and (X ^ Y) != 0 becomes X != Y. */
2294 (cmp (bit_xor @0 @1) integer_zerop)
2297 /* (X ^ Y) == Y becomes X == 0.
2298 Likewise (X ^ Y) == X becomes Y == 0. */
2300 (cmp:c (bit_xor:c @0 @1) @0)
2301 (cmp @1 { build_zero_cst (TREE_TYPE (@1)); }))
2303 /* (X ^ C1) op C2 can be rewritten as X op (C1 ^ C2). */
2305 (cmp (convert?@3 (bit_xor @0 INTEGER_CST@1)) INTEGER_CST@2)
2306 (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0)))
2307 (cmp @0 (bit_xor @1 (convert @2)))))
2310 (cmp (convert? addr@0) integer_zerop)
2311 (if (tree_single_nonzero_warnv_p (@0, NULL))
2312 { constant_boolean_node (cmp == NE_EXPR, type); })))
2314 /* If we have (A & C) == C where C is a power of 2, convert this into
2315 (A & C) != 0. Similarly for NE_EXPR. */
2319 (cmp (bit_and@2 @0 integer_pow2p@1) @1)
2320 (icmp @2 { build_zero_cst (TREE_TYPE (@0)); })))
2322 /* If we have (A & C) != 0 where C is the sign bit of A, convert
2323 this into A < 0. Similarly for (A & C) == 0 into A >= 0. */
2327 (cmp (bit_and (convert?@2 @0) integer_pow2p@1) integer_zerop)
2328 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2329 && (TYPE_PRECISION (TREE_TYPE (@0))
2330 == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (@0))))
2331 && element_precision (@2) >= element_precision (@0)
2332 && wi::only_sign_bit_p (@1, element_precision (@0)))
2333 (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
2334 (ncmp (convert:stype @0) { build_zero_cst (stype); })))))
2336 /* When the addresses are not directly of decls compare base and offset.
2337 This implements some remaining parts of fold_comparison address
2338 comparisons but still no complete part of it. Still it is good
2339 enough to make fold_stmt not regress when not dispatching to fold_binary. */
2340 (for cmp (simple_comparison)
2342 (cmp (convert1?@2 addr@0) (convert2? addr@1))
2345 HOST_WIDE_INT off0, off1;
2346 tree base0 = get_addr_base_and_unit_offset (TREE_OPERAND (@0, 0), &off0);
2347 tree base1 = get_addr_base_and_unit_offset (TREE_OPERAND (@1, 0), &off1);
2348 if (base0 && TREE_CODE (base0) == MEM_REF)
2350 off0 += mem_ref_offset (base0).to_short_addr ();
2351 base0 = TREE_OPERAND (base0, 0);
2353 if (base1 && TREE_CODE (base1) == MEM_REF)
2355 off1 += mem_ref_offset (base1).to_short_addr ();
2356 base1 = TREE_OPERAND (base1, 0);
2359 (if (base0 && base1)
2363 if (decl_in_symtab_p (base0)
2364 && decl_in_symtab_p (base1))
2365 equal = symtab_node::get_create (base0)
2366 ->equal_address_to (symtab_node::get_create (base1));
2367 else if ((DECL_P (base0)
2368 || TREE_CODE (base0) == SSA_NAME
2369 || TREE_CODE (base0) == STRING_CST)
2371 || TREE_CODE (base1) == SSA_NAME
2372 || TREE_CODE (base1) == STRING_CST))
2373 equal = (base0 == base1);
2376 && (cmp == EQ_EXPR || cmp == NE_EXPR
2377 /* If the offsets are equal we can ignore overflow. */
2379 || POINTER_TYPE_OVERFLOW_UNDEFINED
2380 /* Or if we compare using pointers to decls or strings. */
2381 || (POINTER_TYPE_P (TREE_TYPE (@2))
2382 && (DECL_P (base0) || TREE_CODE (base0) == STRING_CST))))
2384 (if (cmp == EQ_EXPR)
2385 { constant_boolean_node (off0 == off1, type); })
2386 (if (cmp == NE_EXPR)
2387 { constant_boolean_node (off0 != off1, type); })
2388 (if (cmp == LT_EXPR)
2389 { constant_boolean_node (off0 < off1, type); })
2390 (if (cmp == LE_EXPR)
2391 { constant_boolean_node (off0 <= off1, type); })
2392 (if (cmp == GE_EXPR)
2393 { constant_boolean_node (off0 >= off1, type); })
2394 (if (cmp == GT_EXPR)
2395 { constant_boolean_node (off0 > off1, type); }))
2397 && DECL_P (base0) && DECL_P (base1)
2398 /* If we compare this as integers require equal offset. */
2399 && (!INTEGRAL_TYPE_P (TREE_TYPE (@2))
2402 (if (cmp == EQ_EXPR)
2403 { constant_boolean_node (false, type); })
2404 (if (cmp == NE_EXPR)
2405 { constant_boolean_node (true, type); })))))))))
2407 /* Simplify pointer equality compares using PTA. */
2411 (if (POINTER_TYPE_P (TREE_TYPE (@0))
2412 && ptrs_compare_unequal (@0, @1))
2413 { neeq == EQ_EXPR ? boolean_false_node : boolean_true_node; })))
2415 /* Non-equality compare simplifications from fold_binary */
2416 (for cmp (lt gt le ge)
2417 /* Comparisons with the highest or lowest possible integer of
2418 the specified precision will have known values. */
2420 (cmp (convert?@2 @0) INTEGER_CST@1)
2421 (if ((INTEGRAL_TYPE_P (TREE_TYPE (@1)) || POINTER_TYPE_P (TREE_TYPE (@1)))
2422 && tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@0)))
2425 tree arg1_type = TREE_TYPE (@1);
2426 unsigned int prec = TYPE_PRECISION (arg1_type);
2427 wide_int max = wi::max_value (arg1_type);
2428 wide_int signed_max = wi::max_value (prec, SIGNED);
2429 wide_int min = wi::min_value (arg1_type);
2432 (if (wi::eq_p (@1, max))
2434 (if (cmp == GT_EXPR)
2435 { constant_boolean_node (false, type); })
2436 (if (cmp == GE_EXPR)
2438 (if (cmp == LE_EXPR)
2439 { constant_boolean_node (true, type); })
2440 (if (cmp == LT_EXPR)
2442 (if (wi::eq_p (@1, min))
2444 (if (cmp == LT_EXPR)
2445 { constant_boolean_node (false, type); })
2446 (if (cmp == LE_EXPR)
2448 (if (cmp == GE_EXPR)
2449 { constant_boolean_node (true, type); })
2450 (if (cmp == GT_EXPR)
2452 (if (wi::eq_p (@1, max - 1))
2454 (if (cmp == GT_EXPR)
2455 (eq @2 { wide_int_to_tree (TREE_TYPE (@1), wi::add (@1, 1)); }))
2456 (if (cmp == LE_EXPR)
2457 (ne @2 { wide_int_to_tree (TREE_TYPE (@1), wi::add (@1, 1)); }))))
2458 (if (wi::eq_p (@1, min + 1))
2460 (if (cmp == GE_EXPR)
2461 (ne @2 { wide_int_to_tree (TREE_TYPE (@1), wi::sub (@1, 1)); }))
2462 (if (cmp == LT_EXPR)
2463 (eq @2 { wide_int_to_tree (TREE_TYPE (@1), wi::sub (@1, 1)); }))))
2464 (if (wi::eq_p (@1, signed_max)
2465 && TYPE_UNSIGNED (arg1_type)
2466 /* We will flip the signedness of the comparison operator
2467 associated with the mode of @1, so the sign bit is
2468 specified by this mode. Check that @1 is the signed
2469 max associated with this sign bit. */
2470 && prec == GET_MODE_PRECISION (TYPE_MODE (arg1_type))
2471 /* signed_type does not work on pointer types. */
2472 && INTEGRAL_TYPE_P (arg1_type))
2473 /* The following case also applies to X < signed_max+1
2474 and X >= signed_max+1 because previous transformations. */
2475 (if (cmp == LE_EXPR || cmp == GT_EXPR)
2476 (with { tree st = signed_type_for (arg1_type); }
2477 (if (cmp == LE_EXPR)
2478 (ge (convert:st @0) { build_zero_cst (st); })
2479 (lt (convert:st @0) { build_zero_cst (st); }))))))))))
2481 (for cmp (unordered ordered unlt unle ungt unge uneq ltgt)
2482 /* If the second operand is NaN, the result is constant. */
2485 (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
2486 && (cmp != LTGT_EXPR || ! flag_trapping_math))
2487 { constant_boolean_node (cmp == ORDERED_EXPR || cmp == LTGT_EXPR
2488 ? false : true, type); })))
2490 /* bool_var != 0 becomes bool_var. */
2492 (ne @0 integer_zerop)
2493 (if (TREE_CODE (TREE_TYPE (@0)) == BOOLEAN_TYPE
2494 && types_match (type, TREE_TYPE (@0)))
2496 /* bool_var == 1 becomes bool_var. */
2498 (eq @0 integer_onep)
2499 (if (TREE_CODE (TREE_TYPE (@0)) == BOOLEAN_TYPE
2500 && types_match (type, TREE_TYPE (@0)))
2503 bool_var == 0 becomes !bool_var or
2504 bool_var != 1 becomes !bool_var
2505 here because that only is good in assignment context as long
2506 as we require a tcc_comparison in GIMPLE_CONDs where we'd
2507 replace if (x == 0) with tem = ~x; if (tem != 0) which is
2508 clearly less optimal and which we'll transform again in forwprop. */
2510 /* When one argument is a constant, overflow detection can be simplified.
2511 Currently restricted to single use so as not to interfere too much with
2512 ADD_OVERFLOW detection in tree-ssa-math-opts.c.
2513 A + CST CMP A -> A CMP' CST' */
2514 (for cmp (lt le ge gt)
2517 (cmp (plus@2 @0 INTEGER_CST@1) @0)
2518 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
2519 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))
2522 (out @0 { wide_int_to_tree (TREE_TYPE (@0), wi::max_value
2523 (TYPE_PRECISION (TREE_TYPE (@0)), UNSIGNED) - @1); }))))
2524 /* A CMP A + CST -> A CMP' CST' */
2525 (for cmp (gt ge le lt)
2528 (cmp @0 (plus@2 @0 INTEGER_CST@1))
2529 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
2530 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))
2533 (out @0 { wide_int_to_tree (TREE_TYPE (@0), wi::max_value
2534 (TYPE_PRECISION (TREE_TYPE (@0)), UNSIGNED) - @1); }))))
2536 /* To detect overflow in unsigned A - B, A < B is simpler than A - B > A.
2537 However, the detection logic for SUB_OVERFLOW in tree-ssa-math-opts.c
2538 expects the long form, so we restrict the transformation for now. */
2541 (cmp (minus@2 @0 @1) @0)
2542 (if (single_use (@2)
2543 && ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2544 && TYPE_UNSIGNED (TREE_TYPE (@0))
2545 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
2549 (cmp @0 (minus@2 @0 @1))
2550 (if (single_use (@2)
2551 && ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2552 && TYPE_UNSIGNED (TREE_TYPE (@0))
2553 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
2556 /* Testing for overflow is unnecessary if we already know the result. */
2561 (cmp @0 (realpart (IFN_SUB_OVERFLOW@2 @0 @1)))
2562 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
2563 && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
2564 (out (imagpart @2) { build_zero_cst (TREE_TYPE (@0)); }))))
2569 (cmp (realpart (IFN_SUB_OVERFLOW@2 @0 @1)) @0)
2570 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
2571 && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
2572 (out (imagpart @2) { build_zero_cst (TREE_TYPE (@0)); }))))
2577 (cmp (realpart (IFN_ADD_OVERFLOW:c@2 @0 @1)) @0)
2578 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
2579 && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
2580 (out (imagpart @2) { build_zero_cst (TREE_TYPE (@0)); }))))
2585 (cmp @0 (realpart (IFN_ADD_OVERFLOW:c@2 @0 @1)))
2586 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
2587 && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
2588 (out (imagpart @2) { build_zero_cst (TREE_TYPE (@0)); }))))
2591 /* Simplification of math builtins. These rules must all be optimizations
2592 as well as IL simplifications. If there is a possibility that the new
2593 form could be a pessimization, the rule should go in the canonicalization
2594 section that follows this one.
2596 Rules can generally go in this section if they satisfy one of
2599 - the rule describes an identity
2601 - the rule replaces calls with something as simple as addition or
2604 - the rule contains unary calls only and simplifies the surrounding
2605 arithmetic. (The idea here is to exclude non-unary calls in which
2606 one operand is constant and in which the call is known to be cheap
2607 when the operand has that value.) */
2609 (if (flag_unsafe_math_optimizations)
2610 /* Simplify sqrt(x) * sqrt(x) -> x. */
2612 (mult (SQRT@1 @0) @1)
2613 (if (!HONOR_SNANS (type))
2616 /* Simplify sqrt(x) * sqrt(y) -> sqrt(x*y). */
2617 (for root (SQRT CBRT)
2619 (mult (root:s @0) (root:s @1))
2620 (root (mult @0 @1))))
2622 /* Simplify expN(x) * expN(y) -> expN(x+y). */
2623 (for exps (EXP EXP2 EXP10 POW10)
2625 (mult (exps:s @0) (exps:s @1))
2626 (exps (plus @0 @1))))
2628 /* Simplify a/root(b/c) into a*root(c/b). */
2629 (for root (SQRT CBRT)
2631 (rdiv @0 (root:s (rdiv:s @1 @2)))
2632 (mult @0 (root (rdiv @2 @1)))))
2634 /* Simplify x/expN(y) into x*expN(-y). */
2635 (for exps (EXP EXP2 EXP10 POW10)
2637 (rdiv @0 (exps:s @1))
2638 (mult @0 (exps (negate @1)))))
2640 (for logs (LOG LOG2 LOG10 LOG10)
2641 exps (EXP EXP2 EXP10 POW10)
2642 /* logN(expN(x)) -> x. */
2646 /* expN(logN(x)) -> x. */
2651 /* Optimize logN(func()) for various exponential functions. We
2652 want to determine the value "x" and the power "exponent" in
2653 order to transform logN(x**exponent) into exponent*logN(x). */
2654 (for logs (LOG LOG LOG LOG2 LOG2 LOG2 LOG10 LOG10)
2655 exps (EXP2 EXP10 POW10 EXP EXP10 POW10 EXP EXP2)
2658 (if (SCALAR_FLOAT_TYPE_P (type))
2664 /* Prepare to do logN(exp(exponent)) -> exponent*logN(e). */
2665 x = build_real_truncate (type, dconst_e ());
2668 /* Prepare to do logN(exp2(exponent)) -> exponent*logN(2). */
2669 x = build_real (type, dconst2);
2673 /* Prepare to do logN(exp10(exponent)) -> exponent*logN(10). */
2675 REAL_VALUE_TYPE dconst10;
2676 real_from_integer (&dconst10, VOIDmode, 10, SIGNED);
2677 x = build_real (type, dconst10);
2684 (mult (logs { x; }) @0)))))
2692 (if (SCALAR_FLOAT_TYPE_P (type))
2698 /* Prepare to do logN(sqrt(x)) -> 0.5*logN(x). */
2699 x = build_real (type, dconsthalf);
2702 /* Prepare to do logN(cbrt(x)) -> (1/3)*logN(x). */
2703 x = build_real_truncate (type, dconst_third ());
2709 (mult { x; } (logs @0))))))
2711 /* logN(pow(x,exponent)) -> exponent*logN(x). */
2712 (for logs (LOG LOG2 LOG10)
2716 (mult @1 (logs @0))))
2721 exps (EXP EXP2 EXP10 POW10)
2722 /* sqrt(expN(x)) -> expN(x*0.5). */
2725 (exps (mult @0 { build_real (type, dconsthalf); })))
2726 /* cbrt(expN(x)) -> expN(x/3). */
2729 (exps (mult @0 { build_real_truncate (type, dconst_third ()); })))
2730 /* pow(expN(x), y) -> expN(x*y). */
2733 (exps (mult @0 @1))))
2735 /* tan(atan(x)) -> x. */
2742 /* cabs(x+0i) or cabs(0+xi) -> abs(x). */
2744 (CABS (complex:c @0 real_zerop@1))
2747 /* trunc(trunc(x)) -> trunc(x), etc. */
2748 (for fns (TRUNC FLOOR CEIL ROUND NEARBYINT RINT)
2752 /* f(x) -> x if x is integer valued and f does nothing for such values. */
2753 (for fns (TRUNC FLOOR CEIL ROUND NEARBYINT RINT)
2755 (fns integer_valued_real_p@0)
2758 /* hypot(x,0) and hypot(0,x) -> abs(x). */
2760 (HYPOT:c @0 real_zerop@1)
2763 /* pow(1,x) -> 1. */
2765 (POW real_onep@0 @1)
2769 /* copysign(x,x) -> x. */
2774 /* copysign(x,y) -> fabs(x) if y is nonnegative. */
2775 (COPYSIGN @0 tree_expr_nonnegative_p@1)
2778 (for scale (LDEXP SCALBN SCALBLN)
2779 /* ldexp(0, x) -> 0. */
2781 (scale real_zerop@0 @1)
2783 /* ldexp(x, 0) -> x. */
2785 (scale @0 integer_zerop@1)
2787 /* ldexp(x, y) -> x if x is +-Inf or NaN. */
2789 (scale REAL_CST@0 @1)
2790 (if (!real_isfinite (TREE_REAL_CST_PTR (@0)))
2793 /* Canonicalization of sequences of math builtins. These rules represent
2794 IL simplifications but are not necessarily optimizations.
2796 The sincos pass is responsible for picking "optimal" implementations
2797 of math builtins, which may be more complicated and can sometimes go
2798 the other way, e.g. converting pow into a sequence of sqrts.
2799 We only want to do these canonicalizations before the pass has run. */
2801 (if (flag_unsafe_math_optimizations && canonicalize_math_p ())
2802 /* Simplify tan(x) * cos(x) -> sin(x). */
2804 (mult:c (TAN:s @0) (COS:s @0))
2807 /* Simplify x * pow(x,c) -> pow(x,c+1). */
2809 (mult:c @0 (POW:s @0 REAL_CST@1))
2810 (if (!TREE_OVERFLOW (@1))
2811 (POW @0 (plus @1 { build_one_cst (type); }))))
2813 /* Simplify sin(x) / cos(x) -> tan(x). */
2815 (rdiv (SIN:s @0) (COS:s @0))
2818 /* Simplify cos(x) / sin(x) -> 1 / tan(x). */
2820 (rdiv (COS:s @0) (SIN:s @0))
2821 (rdiv { build_one_cst (type); } (TAN @0)))
2823 /* Simplify sin(x) / tan(x) -> cos(x). */
2825 (rdiv (SIN:s @0) (TAN:s @0))
2826 (if (! HONOR_NANS (@0)
2827 && ! HONOR_INFINITIES (@0))
2830 /* Simplify tan(x) / sin(x) -> 1.0 / cos(x). */
2832 (rdiv (TAN:s @0) (SIN:s @0))
2833 (if (! HONOR_NANS (@0)
2834 && ! HONOR_INFINITIES (@0))
2835 (rdiv { build_one_cst (type); } (COS @0))))
2837 /* Simplify pow(x,y) * pow(x,z) -> pow(x,y+z). */
2839 (mult (POW:s @0 @1) (POW:s @0 @2))
2840 (POW @0 (plus @1 @2)))
2842 /* Simplify pow(x,y) * pow(z,y) -> pow(x*z,y). */
2844 (mult (POW:s @0 @1) (POW:s @2 @1))
2845 (POW (mult @0 @2) @1))
2847 /* Simplify powi(x,y) * powi(z,y) -> powi(x*z,y). */
2849 (mult (POWI:s @0 @1) (POWI:s @2 @1))
2850 (POWI (mult @0 @2) @1))
2852 /* Simplify pow(x,c) / x -> pow(x,c-1). */
2854 (rdiv (POW:s @0 REAL_CST@1) @0)
2855 (if (!TREE_OVERFLOW (@1))
2856 (POW @0 (minus @1 { build_one_cst (type); }))))
2858 /* Simplify x / pow (y,z) -> x * pow(y,-z). */
2860 (rdiv @0 (POW:s @1 @2))
2861 (mult @0 (POW @1 (negate @2))))
2866 /* sqrt(sqrt(x)) -> pow(x,1/4). */
2869 (pows @0 { build_real (type, dconst_quarter ()); }))
2870 /* sqrt(cbrt(x)) -> pow(x,1/6). */
2873 (pows @0 { build_real_truncate (type, dconst_sixth ()); }))
2874 /* cbrt(sqrt(x)) -> pow(x,1/6). */
2877 (pows @0 { build_real_truncate (type, dconst_sixth ()); }))
2878 /* cbrt(cbrt(x)) -> pow(x,1/9), iff x is nonnegative. */
2880 (cbrts (cbrts tree_expr_nonnegative_p@0))
2881 (pows @0 { build_real_truncate (type, dconst_ninth ()); }))
2882 /* sqrt(pow(x,y)) -> pow(|x|,y*0.5). */
2884 (sqrts (pows @0 @1))
2885 (pows (abs @0) (mult @1 { build_real (type, dconsthalf); })))
2886 /* cbrt(pow(x,y)) -> pow(x,y/3), iff x is nonnegative. */
2888 (cbrts (pows tree_expr_nonnegative_p@0 @1))
2889 (pows @0 (mult @1 { build_real_truncate (type, dconst_third ()); })))
2890 /* pow(sqrt(x),y) -> pow(x,y*0.5). */
2892 (pows (sqrts @0) @1)
2893 (pows @0 (mult @1 { build_real (type, dconsthalf); })))
2894 /* pow(cbrt(x),y) -> pow(x,y/3) iff x is nonnegative. */
2896 (pows (cbrts tree_expr_nonnegative_p@0) @1)
2897 (pows @0 (mult @1 { build_real_truncate (type, dconst_third ()); })))
2898 /* pow(pow(x,y),z) -> pow(x,y*z) iff x is nonnegative. */
2900 (pows (pows tree_expr_nonnegative_p@0 @1) @2)
2901 (pows @0 (mult @1 @2))))
2903 /* cabs(x+xi) -> fabs(x)*sqrt(2). */
2905 (CABS (complex @0 @0))
2906 (mult (abs @0) { build_real_truncate (type, dconst_sqrt2 ()); }))
2908 /* hypot(x,x) -> fabs(x)*sqrt(2). */
2911 (mult (abs @0) { build_real_truncate (type, dconst_sqrt2 ()); }))
2913 /* cexp(x+yi) -> exp(x)*cexpi(y). */
2918 (cexps compositional_complex@0)
2919 (if (targetm.libc_has_function (function_c99_math_complex))
2921 (mult (exps@1 (realpart @0)) (realpart (cexpis:type@2 (imagpart @0))))
2922 (mult @1 (imagpart @2)))))))
2924 (if (canonicalize_math_p ())
2925 /* floor(x) -> trunc(x) if x is nonnegative. */
2929 (floors tree_expr_nonnegative_p@0)
2932 (match double_value_p
2934 (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == double_type_node)))
2935 (for froms (BUILT_IN_TRUNCL
2947 /* truncl(extend(x)) -> extend(trunc(x)), etc., if x is a double. */
2948 (if (optimize && canonicalize_math_p ())
2950 (froms (convert double_value_p@0))
2951 (convert (tos @0)))))
2953 (match float_value_p
2955 (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == float_type_node)))
2956 (for froms (BUILT_IN_TRUNCL BUILT_IN_TRUNC
2957 BUILT_IN_FLOORL BUILT_IN_FLOOR
2958 BUILT_IN_CEILL BUILT_IN_CEIL
2959 BUILT_IN_ROUNDL BUILT_IN_ROUND
2960 BUILT_IN_NEARBYINTL BUILT_IN_NEARBYINT
2961 BUILT_IN_RINTL BUILT_IN_RINT)
2962 tos (BUILT_IN_TRUNCF BUILT_IN_TRUNCF
2963 BUILT_IN_FLOORF BUILT_IN_FLOORF
2964 BUILT_IN_CEILF BUILT_IN_CEILF
2965 BUILT_IN_ROUNDF BUILT_IN_ROUNDF
2966 BUILT_IN_NEARBYINTF BUILT_IN_NEARBYINTF
2967 BUILT_IN_RINTF BUILT_IN_RINTF)
2968 /* truncl(extend(x)) and trunc(extend(x)) -> extend(truncf(x)), etc.,
2970 (if (optimize && canonicalize_math_p ()
2971 && targetm.libc_has_function (function_c99_misc))
2973 (froms (convert float_value_p@0))
2974 (convert (tos @0)))))
2976 (for froms (XFLOORL XCEILL XROUNDL XRINTL)
2977 tos (XFLOOR XCEIL XROUND XRINT)
2978 /* llfloorl(extend(x)) -> llfloor(x), etc., if x is a double. */
2979 (if (optimize && canonicalize_math_p ())
2981 (froms (convert double_value_p@0))
2984 (for froms (XFLOORL XCEILL XROUNDL XRINTL
2985 XFLOOR XCEIL XROUND XRINT)
2986 tos (XFLOORF XCEILF XROUNDF XRINTF)
2987 /* llfloorl(extend(x)) and llfloor(extend(x)) -> llfloorf(x), etc.,
2989 (if (optimize && canonicalize_math_p ())
2991 (froms (convert float_value_p@0))
2994 (if (canonicalize_math_p ())
2995 /* xfloor(x) -> fix_trunc(x) if x is nonnegative. */
2996 (for floors (IFLOOR LFLOOR LLFLOOR)
2998 (floors tree_expr_nonnegative_p@0)
3001 (if (canonicalize_math_p ())
3002 /* xfloor(x) -> fix_trunc(x), etc., if x is integer valued. */
3003 (for fns (IFLOOR LFLOOR LLFLOOR
3005 IROUND LROUND LLROUND)
3007 (fns integer_valued_real_p@0)
3009 (if (!flag_errno_math)
3010 /* xrint(x) -> fix_trunc(x), etc., if x is integer valued. */
3011 (for rints (IRINT LRINT LLRINT)
3013 (rints integer_valued_real_p@0)
3016 (if (canonicalize_math_p ())
3017 (for ifn (IFLOOR ICEIL IROUND IRINT)
3018 lfn (LFLOOR LCEIL LROUND LRINT)
3019 llfn (LLFLOOR LLCEIL LLROUND LLRINT)
3020 /* Canonicalize iround (x) to lround (x) on ILP32 targets where
3021 sizeof (int) == sizeof (long). */
3022 (if (TYPE_PRECISION (integer_type_node)
3023 == TYPE_PRECISION (long_integer_type_node))
3026 (lfn:long_integer_type_node @0)))
3027 /* Canonicalize llround (x) to lround (x) on LP64 targets where
3028 sizeof (long long) == sizeof (long). */
3029 (if (TYPE_PRECISION (long_long_integer_type_node)
3030 == TYPE_PRECISION (long_integer_type_node))
3033 (lfn:long_integer_type_node @0)))))
3035 /* cproj(x) -> x if we're ignoring infinities. */
3038 (if (!HONOR_INFINITIES (type))
3041 /* If the real part is inf and the imag part is known to be
3042 nonnegative, return (inf + 0i). */
3044 (CPROJ (complex REAL_CST@0 tree_expr_nonnegative_p@1))
3045 (if (real_isinf (TREE_REAL_CST_PTR (@0)))
3046 { build_complex_inf (type, false); }))
3048 /* If the imag part is inf, return (inf+I*copysign(0,imag)). */
3050 (CPROJ (complex @0 REAL_CST@1))
3051 (if (real_isinf (TREE_REAL_CST_PTR (@1)))
3052 { build_complex_inf (type, TREE_REAL_CST_PTR (@1)->sign); }))
3058 (pows @0 REAL_CST@1)
3060 const REAL_VALUE_TYPE *value = TREE_REAL_CST_PTR (@1);
3061 REAL_VALUE_TYPE tmp;
3064 /* pow(x,0) -> 1. */
3065 (if (real_equal (value, &dconst0))
3066 { build_real (type, dconst1); })
3067 /* pow(x,1) -> x. */
3068 (if (real_equal (value, &dconst1))
3070 /* pow(x,-1) -> 1/x. */
3071 (if (real_equal (value, &dconstm1))
3072 (rdiv { build_real (type, dconst1); } @0))
3073 /* pow(x,0.5) -> sqrt(x). */
3074 (if (flag_unsafe_math_optimizations
3075 && canonicalize_math_p ()
3076 && real_equal (value, &dconsthalf))
3078 /* pow(x,1/3) -> cbrt(x). */
3079 (if (flag_unsafe_math_optimizations
3080 && canonicalize_math_p ()
3081 && (tmp = real_value_truncate (TYPE_MODE (type), dconst_third ()),
3082 real_equal (value, &tmp)))
3085 /* powi(1,x) -> 1. */
3087 (POWI real_onep@0 @1)
3091 (POWI @0 INTEGER_CST@1)
3093 /* powi(x,0) -> 1. */
3094 (if (wi::eq_p (@1, 0))
3095 { build_real (type, dconst1); })
3096 /* powi(x,1) -> x. */
3097 (if (wi::eq_p (@1, 1))
3099 /* powi(x,-1) -> 1/x. */
3100 (if (wi::eq_p (@1, -1))
3101 (rdiv { build_real (type, dconst1); } @0))))
3103 /* Narrowing of arithmetic and logical operations.
3105 These are conceptually similar to the transformations performed for
3106 the C/C++ front-ends by shorten_binary_op and shorten_compare. Long
3107 term we want to move all that code out of the front-ends into here. */
3109 /* If we have a narrowing conversion of an arithmetic operation where
3110 both operands are widening conversions from the same type as the outer
3111 narrowing conversion. Then convert the innermost operands to a suitable
3112 unsigned type (to avoid introducing undefined behavior), perform the
3113 operation and convert the result to the desired type. */
3114 (for op (plus minus)
3116 (convert (op:s (convert@2 @0) (convert@3 @1)))
3117 (if (INTEGRAL_TYPE_P (type)
3118 /* We check for type compatibility between @0 and @1 below,
3119 so there's no need to check that @1/@3 are integral types. */
3120 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
3121 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
3122 /* The precision of the type of each operand must match the
3123 precision of the mode of each operand, similarly for the
3125 && (TYPE_PRECISION (TREE_TYPE (@0))
3126 == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (@0))))
3127 && (TYPE_PRECISION (TREE_TYPE (@1))
3128 == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (@1))))
3129 && TYPE_PRECISION (type) == GET_MODE_PRECISION (TYPE_MODE (type))
3130 /* The inner conversion must be a widening conversion. */
3131 && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
3132 && types_match (@0, @1)
3133 && types_match (@0, type))
3134 (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
3135 (convert (op @0 @1))
3136 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
3137 (convert (op (convert:utype @0) (convert:utype @1))))))))
3139 /* This is another case of narrowing, specifically when there's an outer
3140 BIT_AND_EXPR which masks off bits outside the type of the innermost
3141 operands. Like the previous case we have to convert the operands
3142 to unsigned types to avoid introducing undefined behavior for the
3143 arithmetic operation. */
3144 (for op (minus plus)
3146 (bit_and (op:s (convert@2 @0) (convert@3 @1)) INTEGER_CST@4)
3147 (if (INTEGRAL_TYPE_P (type)
3148 /* We check for type compatibility between @0 and @1 below,
3149 so there's no need to check that @1/@3 are integral types. */
3150 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
3151 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
3152 /* The precision of the type of each operand must match the
3153 precision of the mode of each operand, similarly for the
3155 && (TYPE_PRECISION (TREE_TYPE (@0))
3156 == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (@0))))
3157 && (TYPE_PRECISION (TREE_TYPE (@1))
3158 == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (@1))))
3159 && TYPE_PRECISION (type) == GET_MODE_PRECISION (TYPE_MODE (type))
3160 /* The inner conversion must be a widening conversion. */
3161 && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
3162 && types_match (@0, @1)
3163 && (tree_int_cst_min_precision (@4, TYPE_SIGN (TREE_TYPE (@0)))
3164 <= TYPE_PRECISION (TREE_TYPE (@0)))
3165 && (wi::bit_and (@4, wi::mask (TYPE_PRECISION (TREE_TYPE (@0)),
3166 true, TYPE_PRECISION (type))) == 0))
3167 (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
3168 (with { tree ntype = TREE_TYPE (@0); }
3169 (convert (bit_and (op @0 @1) (convert:ntype @4))))
3170 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
3171 (convert (bit_and (op (convert:utype @0) (convert:utype @1))
3172 (convert:utype @4))))))))
3174 /* Transform (@0 < @1 and @0 < @2) to use min,
3175 (@0 > @1 and @0 > @2) to use max */
3176 (for op (lt le gt ge)
3177 ext (min min max max)
3179 (bit_and (op:s @0 @1) (op:s @0 @2))
3180 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)))
3181 (op @0 (ext @1 @2)))))
3184 /* signbit(x) -> 0 if x is nonnegative. */
3185 (SIGNBIT tree_expr_nonnegative_p@0)
3186 { integer_zero_node; })
3189 /* signbit(x) -> x<0 if x doesn't have signed zeros. */
3191 (if (!HONOR_SIGNED_ZEROS (@0))
3192 (convert (lt @0 { build_real (TREE_TYPE (@0), dconst0); }))))
3194 /* Transform comparisons of the form X +- C1 CMP C2 to X CMP C2 -+ C1. */
3196 (for op (plus minus)
3199 (cmp (op@3 @0 INTEGER_CST@1) INTEGER_CST@2)
3200 (if (!TREE_OVERFLOW (@1) && !TREE_OVERFLOW (@2)
3201 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0))
3202 && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@0))
3203 && !TYPE_SATURATING (TREE_TYPE (@0)))
3204 (with { tree res = int_const_binop (rop, @2, @1); }
3205 (if (TREE_OVERFLOW (res))
3206 { constant_boolean_node (cmp == NE_EXPR, type); }
3207 (if (single_use (@3))
3208 (cmp @0 { res; }))))))))
3209 (for cmp (lt le gt ge)
3210 (for op (plus minus)
3213 (cmp (op@3 @0 INTEGER_CST@1) INTEGER_CST@2)
3214 (if (!TREE_OVERFLOW (@1) && !TREE_OVERFLOW (@2)
3215 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
3216 (with { tree res = int_const_binop (rop, @2, @1); }
3217 (if (TREE_OVERFLOW (res))
3219 fold_overflow_warning (("assuming signed overflow does not occur "
3220 "when simplifying conditional to constant"),
3221 WARN_STRICT_OVERFLOW_CONDITIONAL);
3222 bool less = cmp == LE_EXPR || cmp == LT_EXPR;
3223 /* wi::ges_p (@2, 0) should be sufficient for a signed type. */
3224 bool ovf_high = wi::lt_p (@1, 0, TYPE_SIGN (TREE_TYPE (@1)))
3225 != (op == MINUS_EXPR);
3226 constant_boolean_node (less == ovf_high, type);
3228 (if (single_use (@3))
3231 fold_overflow_warning (("assuming signed overflow does not occur "
3232 "when changing X +- C1 cmp C2 to "
3234 WARN_STRICT_OVERFLOW_COMPARISON);
3236 (cmp @0 { res; })))))))))