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)
150 (for div (trunc_div ceil_div floor_div round_div exact_div)
153 (div @0 integer_minus_onep@1)
154 (if (!TYPE_UNSIGNED (type))
156 /* X / abs (X) is X < 0 ? -1 : 1. */
159 (if (INTEGRAL_TYPE_P (type)
160 && TYPE_OVERFLOW_UNDEFINED (type))
161 (cond (lt @0 { build_zero_cst (type); })
162 { build_minus_one_cst (type); } { build_one_cst (type); })))
165 (div:C @0 (negate @0))
166 (if ((INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
167 && TYPE_OVERFLOW_UNDEFINED (type))
168 { build_minus_one_cst (type); })))
170 /* For unsigned integral types, FLOOR_DIV_EXPR is the same as
171 TRUNC_DIV_EXPR. Rewrite into the latter in this case. */
174 (if ((INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
175 && TYPE_UNSIGNED (type))
178 /* Combine two successive divisions. Note that combining ceil_div
179 and floor_div is trickier and combining round_div even more so. */
180 (for div (trunc_div exact_div)
182 (div (div @0 INTEGER_CST@1) INTEGER_CST@2)
185 wide_int mul = wi::mul (@1, @2, TYPE_SIGN (type), &overflow_p);
188 (div @0 { wide_int_to_tree (type, mul); })
189 (if (TYPE_UNSIGNED (type)
190 || mul != wi::min_value (TYPE_PRECISION (type), SIGNED))
191 { build_zero_cst (type); })))))
193 /* Optimize A / A to 1.0 if we don't care about
194 NaNs or Infinities. */
197 (if (FLOAT_TYPE_P (type)
198 && ! HONOR_NANS (type)
199 && ! HONOR_INFINITIES (type))
200 { build_one_cst (type); }))
202 /* Optimize -A / A to -1.0 if we don't care about
203 NaNs or Infinities. */
205 (rdiv:C @0 (negate @0))
206 (if (FLOAT_TYPE_P (type)
207 && ! HONOR_NANS (type)
208 && ! HONOR_INFINITIES (type))
209 { build_minus_one_cst (type); }))
211 /* PR71078: x / abs(x) -> copysign (1.0, x) */
213 (rdiv:C (convert? @0) (convert? (abs @0)))
214 (if (SCALAR_FLOAT_TYPE_P (type)
215 && ! HONOR_NANS (type)
216 && ! HONOR_INFINITIES (type))
218 (if (types_match (type, float_type_node))
219 (BUILT_IN_COPYSIGNF { build_one_cst (type); } (convert @0)))
220 (if (types_match (type, double_type_node))
221 (BUILT_IN_COPYSIGN { build_one_cst (type); } (convert @0)))
222 (if (types_match (type, long_double_type_node))
223 (BUILT_IN_COPYSIGNL { build_one_cst (type); } (convert @0))))))
225 /* In IEEE floating point, x/1 is not equivalent to x for snans. */
228 (if (!HONOR_SNANS (type))
231 /* In IEEE floating point, x/-1 is not equivalent to -x for snans. */
233 (rdiv @0 real_minus_onep)
234 (if (!HONOR_SNANS (type))
237 (if (flag_reciprocal_math)
238 /* Convert (A/B)/C to A/(B*C) */
240 (rdiv (rdiv:s @0 @1) @2)
241 (rdiv @0 (mult @1 @2)))
243 /* Convert A/(B/C) to (A/B)*C */
245 (rdiv @0 (rdiv:s @1 @2))
246 (mult (rdiv @0 @1) @2)))
248 /* Optimize (X & (-A)) / A where A is a power of 2, to X >> log2(A) */
249 (for div (trunc_div ceil_div floor_div round_div exact_div)
251 (div (convert? (bit_and @0 INTEGER_CST@1)) INTEGER_CST@2)
252 (if (integer_pow2p (@2)
253 && tree_int_cst_sgn (@2) > 0
254 && wi::add (@2, @1) == 0
255 && tree_nop_conversion_p (type, TREE_TYPE (@0)))
256 (rshift (convert @0) { build_int_cst (integer_type_node,
257 wi::exact_log2 (@2)); }))))
259 /* If ARG1 is a constant, we can convert this to a multiply by the
260 reciprocal. This does not have the same rounding properties,
261 so only do this if -freciprocal-math. We can actually
262 always safely do it if ARG1 is a power of two, but it's hard to
263 tell if it is or not in a portable manner. */
264 (for cst (REAL_CST COMPLEX_CST VECTOR_CST)
268 (if (flag_reciprocal_math
271 { tree tem = const_binop (RDIV_EXPR, type, build_one_cst (type), @1); }
273 (mult @0 { tem; } )))
274 (if (cst != COMPLEX_CST)
275 (with { tree inverse = exact_inverse (type, @1); }
277 (mult @0 { inverse; } ))))))))
279 /* Same applies to modulo operations, but fold is inconsistent here
280 and simplifies 0 % x to 0, only preserving literal 0 % 0. */
281 (for mod (ceil_mod floor_mod round_mod trunc_mod)
282 /* 0 % X is always zero. */
284 (mod integer_zerop@0 @1)
285 /* But not for 0 % 0 so that we can get the proper warnings and errors. */
286 (if (!integer_zerop (@1))
288 /* X % 1 is always zero. */
290 (mod @0 integer_onep)
291 { build_zero_cst (type); })
292 /* X % -1 is zero. */
294 (mod @0 integer_minus_onep@1)
295 (if (!TYPE_UNSIGNED (type))
296 { build_zero_cst (type); }))
297 /* (X % Y) % Y is just X % Y. */
299 (mod (mod@2 @0 @1) @1)
301 /* From extract_muldiv_1: (X * C1) % C2 is zero if C1 is a multiple of C2. */
303 (mod (mult @0 INTEGER_CST@1) INTEGER_CST@2)
304 (if (ANY_INTEGRAL_TYPE_P (type)
305 && TYPE_OVERFLOW_UNDEFINED (type)
306 && wi::multiple_of_p (@1, @2, TYPE_SIGN (type)))
307 { build_zero_cst (type); })))
309 /* X % -C is the same as X % C. */
311 (trunc_mod @0 INTEGER_CST@1)
312 (if (TYPE_SIGN (type) == SIGNED
313 && !TREE_OVERFLOW (@1)
315 && !TYPE_OVERFLOW_TRAPS (type)
316 /* Avoid this transformation if C is INT_MIN, i.e. C == -C. */
317 && !sign_bit_p (@1, @1))
318 (trunc_mod @0 (negate @1))))
320 /* X % -Y is the same as X % Y. */
322 (trunc_mod @0 (convert? (negate @1)))
323 (if (INTEGRAL_TYPE_P (type)
324 && !TYPE_UNSIGNED (type)
325 && !TYPE_OVERFLOW_TRAPS (type)
326 && tree_nop_conversion_p (type, TREE_TYPE (@1))
327 /* Avoid this transformation if X might be INT_MIN or
328 Y might be -1, because we would then change valid
329 INT_MIN % -(-1) into invalid INT_MIN % -1. */
330 && (expr_not_equal_to (@0, TYPE_MIN_VALUE (type))
331 || expr_not_equal_to (@1, wi::minus_one (TYPE_PRECISION
333 (trunc_mod @0 (convert @1))))
335 /* X - (X / Y) * Y is the same as X % Y. */
337 (minus (convert1? @0) (convert2? (mult:c (trunc_div @@0 @@1) @1)))
338 (if (INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
339 (convert (trunc_mod @0 @1))))
341 /* Optimize TRUNC_MOD_EXPR by a power of two into a BIT_AND_EXPR,
342 i.e. "X % C" into "X & (C - 1)", if X and C are positive.
343 Also optimize A % (C << N) where C is a power of 2,
344 to A & ((C << N) - 1). */
345 (match (power_of_two_cand @1)
347 (match (power_of_two_cand @1)
348 (lshift INTEGER_CST@1 @2))
349 (for mod (trunc_mod floor_mod)
351 (mod @0 (convert?@3 (power_of_two_cand@1 @2)))
352 (if ((TYPE_UNSIGNED (type)
353 || tree_expr_nonnegative_p (@0))
354 && tree_nop_conversion_p (type, TREE_TYPE (@3))
355 && integer_pow2p (@2) && tree_int_cst_sgn (@2) > 0)
356 (bit_and @0 (convert (minus @1 { build_int_cst (TREE_TYPE (@1), 1); }))))))
358 /* Simplify (unsigned t * 2)/2 -> unsigned t & 0x7FFFFFFF. */
360 (trunc_div (mult @0 integer_pow2p@1) @1)
361 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
362 (bit_and @0 { wide_int_to_tree
363 (type, wi::mask (TYPE_PRECISION (type) - wi::exact_log2 (@1),
364 false, TYPE_PRECISION (type))); })))
366 /* Simplify (unsigned t / 2) * 2 -> unsigned t & ~1. */
368 (mult (trunc_div @0 integer_pow2p@1) @1)
369 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
370 (bit_and @0 (negate @1))))
372 /* Simplify (t * 2) / 2) -> t. */
373 (for div (trunc_div ceil_div floor_div round_div exact_div)
375 (div (mult @0 @1) @1)
376 (if (ANY_INTEGRAL_TYPE_P (type)
377 && TYPE_OVERFLOW_UNDEFINED (type))
381 /* Simplify cos(-x) and cos(|x|) -> cos(x). Similarly for cosh. */
386 /* Simplify pow(-x, y) and pow(|x|,y) -> pow(x,y) if y is an even integer. */
389 (pows (op @0) REAL_CST@1)
390 (with { HOST_WIDE_INT n; }
391 (if (real_isinteger (&TREE_REAL_CST (@1), &n) && (n & 1) == 0)
393 /* Likewise for powi. */
396 (pows (op @0) INTEGER_CST@1)
397 (if (wi::bit_and (@1, 1) == 0)
399 /* Strip negate and abs from both operands of hypot. */
407 /* copysign(-x, y) and copysign(abs(x), y) -> copysign(x, y). */
408 (for copysigns (COPYSIGN)
410 (copysigns (op @0) @1)
413 /* abs(x)*abs(x) -> x*x. Should be valid for all types. */
418 /* cos(copysign(x, y)) -> cos(x). Similarly for cosh. */
422 (coss (copysigns @0 @1))
425 /* pow(copysign(x, y), z) -> pow(x, z) if z is an even integer. */
429 (pows (copysigns @0 @2) REAL_CST@1)
430 (with { HOST_WIDE_INT n; }
431 (if (real_isinteger (&TREE_REAL_CST (@1), &n) && (n & 1) == 0)
433 /* Likewise for powi. */
437 (pows (copysigns @0 @2) INTEGER_CST@1)
438 (if (wi::bit_and (@1, 1) == 0)
443 /* hypot(copysign(x, y), z) -> hypot(x, z). */
445 (hypots (copysigns @0 @1) @2)
447 /* hypot(x, copysign(y, z)) -> hypot(x, y). */
449 (hypots @0 (copysigns @1 @2))
452 /* copysign(x, CST) -> [-]abs (x). */
453 (for copysigns (COPYSIGN)
455 (copysigns @0 REAL_CST@1)
456 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
460 /* copysign(copysign(x, y), z) -> copysign(x, z). */
461 (for copysigns (COPYSIGN)
463 (copysigns (copysigns @0 @1) @2)
466 /* copysign(x,y)*copysign(x,y) -> x*x. */
467 (for copysigns (COPYSIGN)
469 (mult (copysigns@2 @0 @1) @2)
472 /* ccos(-x) -> ccos(x). Similarly for ccosh. */
473 (for ccoss (CCOS CCOSH)
478 /* cabs(-x) and cos(conj(x)) -> cabs(x). */
479 (for ops (conj negate)
485 /* Fold (a * (1 << b)) into (a << b) */
487 (mult:c @0 (convert? (lshift integer_onep@1 @2)))
488 (if (! FLOAT_TYPE_P (type)
489 && tree_nop_conversion_p (type, TREE_TYPE (@1)))
492 /* Fold (C1/X)*C2 into (C1*C2)/X. */
494 (mult (rdiv@3 REAL_CST@0 @1) REAL_CST@2)
495 (if (flag_associative_math
498 { tree tem = const_binop (MULT_EXPR, type, @0, @2); }
500 (rdiv { tem; } @1)))))
502 /* Convert C1/(X*C2) into (C1/C2)/X */
504 (rdiv REAL_CST@0 (mult @1 REAL_CST@2))
505 (if (flag_reciprocal_math)
507 { tree tem = const_binop (RDIV_EXPR, type, @0, @2); }
509 (rdiv { tem; } @1)))))
511 /* Simplify ~X & X as zero. */
513 (bit_and:c (convert? @0) (convert? (bit_not @0)))
514 { build_zero_cst (type); })
516 /* PR71636: Transform x & ((1U << b) - 1) -> x & ~(~0U << b); */
518 (bit_and:c @0 (plus:s (lshift:s integer_onep @1) integer_minus_onep))
519 (if (TYPE_UNSIGNED (type))
520 (bit_and @0 (bit_not (lshift { build_all_ones_cst (type); } @1)))))
522 /* Fold (A & ~B) - (A & B) into (A ^ B) - B. */
524 (minus (bit_and:cs @0 (bit_not @1)) (bit_and:cs @0 @1))
525 (minus (bit_xor @0 @1) @1))
527 (minus (bit_and:s @0 INTEGER_CST@2) (bit_and:s @0 INTEGER_CST@1))
528 (if (wi::bit_not (@2) == @1)
529 (minus (bit_xor @0 @1) @1)))
531 /* Fold (A & B) - (A & ~B) into B - (A ^ B). */
533 (minus (bit_and:cs @0 @1) (bit_and:cs @0 (bit_not @1)))
534 (minus @1 (bit_xor @0 @1)))
536 /* Simplify (X & ~Y) | (~X & Y) -> X ^ Y. */
538 (bit_ior (bit_and:c @0 (bit_not @1)) (bit_and:c (bit_not @0) @1))
541 (bit_ior:c (bit_and @0 INTEGER_CST@2) (bit_and (bit_not @0) INTEGER_CST@1))
542 (if (wi::bit_not (@2) == @1)
545 /* PR53979: Transform ((a ^ b) | a) -> (a | b) */
547 (bit_ior:c (bit_xor:c @0 @1) @0)
550 /* Simplify (~X & Y) to X ^ Y if we know that (X & ~Y) is 0. */
553 (bit_and (bit_not SSA_NAME@0) INTEGER_CST@1)
554 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
555 && (get_nonzero_bits (@0) & wi::bit_not (@1)) == 0)
559 /* X % Y is smaller than Y. */
562 (cmp (trunc_mod @0 @1) @1)
563 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
564 { constant_boolean_node (cmp == LT_EXPR, type); })))
567 (cmp @1 (trunc_mod @0 @1))
568 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
569 { constant_boolean_node (cmp == GT_EXPR, type); })))
573 (bit_ior @0 integer_all_onesp@1)
578 (bit_ior @0 integer_zerop)
583 (bit_and @0 integer_zerop@1)
589 (for op (bit_ior bit_xor plus)
591 (op:c (convert? @0) (convert? (bit_not @0)))
592 (convert { build_all_ones_cst (TREE_TYPE (@0)); })))
597 { build_zero_cst (type); })
599 /* Canonicalize X ^ ~0 to ~X. */
601 (bit_xor @0 integer_all_onesp@1)
606 (bit_and @0 integer_all_onesp)
609 /* x & x -> x, x | x -> x */
610 (for bitop (bit_and bit_ior)
615 /* x & C -> x if we know that x & ~C == 0. */
618 (bit_and SSA_NAME@0 INTEGER_CST@1)
619 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
620 && (get_nonzero_bits (@0) & wi::bit_not (@1)) == 0)
624 /* x + (x & 1) -> (x + 1) & ~1 */
626 (plus:c @0 (bit_and:s @0 integer_onep@1))
627 (bit_and (plus @0 @1) (bit_not @1)))
629 /* x & ~(x & y) -> x & ~y */
630 /* x | ~(x | y) -> x | ~y */
631 (for bitop (bit_and bit_ior)
633 (bitop:c @0 (bit_not (bitop:cs @0 @1)))
634 (bitop @0 (bit_not @1))))
636 /* (x | y) & ~x -> y & ~x */
637 /* (x & y) | ~x -> y | ~x */
638 (for bitop (bit_and bit_ior)
639 rbitop (bit_ior bit_and)
641 (bitop:c (rbitop:c @0 @1) (bit_not@2 @0))
644 /* (x & y) ^ (x | y) -> x ^ y */
646 (bit_xor:c (bit_and @0 @1) (bit_ior @0 @1))
649 /* (x ^ y) ^ (x | y) -> x & y */
651 (bit_xor:c (bit_xor @0 @1) (bit_ior @0 @1))
654 /* (x & y) + (x ^ y) -> x | y */
655 /* (x & y) | (x ^ y) -> x | y */
656 /* (x & y) ^ (x ^ y) -> x | y */
657 (for op (plus bit_ior bit_xor)
659 (op:c (bit_and @0 @1) (bit_xor @0 @1))
662 /* (x & y) + (x | y) -> x + y */
664 (plus:c (bit_and @0 @1) (bit_ior @0 @1))
667 /* (x + y) - (x | y) -> x & y */
669 (minus (plus @0 @1) (bit_ior @0 @1))
670 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
671 && !TYPE_SATURATING (type))
674 /* (x + y) - (x & y) -> x | y */
676 (minus (plus @0 @1) (bit_and @0 @1))
677 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
678 && !TYPE_SATURATING (type))
681 /* (x | y) - (x ^ y) -> x & y */
683 (minus (bit_ior @0 @1) (bit_xor @0 @1))
686 /* (x | y) - (x & y) -> x ^ y */
688 (minus (bit_ior @0 @1) (bit_and @0 @1))
691 /* (x | y) & ~(x & y) -> x ^ y */
693 (bit_and:c (bit_ior @0 @1) (bit_not (bit_and @0 @1)))
696 /* (x | y) & (~x ^ y) -> x & y */
698 (bit_and:c (bit_ior:c @0 @1) (bit_xor:c @1 (bit_not @0)))
701 /* ~x & ~y -> ~(x | y)
702 ~x | ~y -> ~(x & y) */
703 (for op (bit_and bit_ior)
704 rop (bit_ior bit_and)
706 (op (convert1? (bit_not @0)) (convert2? (bit_not @1)))
707 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
708 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
709 (bit_not (rop (convert @0) (convert @1))))))
711 /* If we are XORing or adding two BIT_AND_EXPR's, both of which are and'ing
712 with a constant, and the two constants have no bits in common,
713 we should treat this as a BIT_IOR_EXPR since this may produce more
715 (for op (bit_xor plus)
717 (op (convert1? (bit_and@4 @0 INTEGER_CST@1))
718 (convert2? (bit_and@5 @2 INTEGER_CST@3)))
719 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
720 && tree_nop_conversion_p (type, TREE_TYPE (@2))
721 && wi::bit_and (@1, @3) == 0)
722 (bit_ior (convert @4) (convert @5)))))
724 /* (X | Y) ^ X -> Y & ~ X*/
726 (bit_xor:c (convert1? (bit_ior:c @@0 @1)) (convert2? @0))
727 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
728 (convert (bit_and @1 (bit_not @0)))))
730 /* Convert ~X ^ ~Y to X ^ Y. */
732 (bit_xor (convert1? (bit_not @0)) (convert2? (bit_not @1)))
733 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
734 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
735 (bit_xor (convert @0) (convert @1))))
737 /* Convert ~X ^ C to X ^ ~C. */
739 (bit_xor (convert? (bit_not @0)) INTEGER_CST@1)
740 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
741 (bit_xor (convert @0) (bit_not @1))))
743 /* Fold (X & Y) ^ Y and (X ^ Y) & Y as ~X & Y. */
744 (for opo (bit_and bit_xor)
745 opi (bit_xor bit_and)
747 (opo:c (opi:c @0 @1) @1)
748 (bit_and (bit_not @0) @1)))
750 /* Given a bit-wise operation CODE applied to ARG0 and ARG1, see if both
751 operands are another bit-wise operation with a common input. If so,
752 distribute the bit operations to save an operation and possibly two if
753 constants are involved. For example, convert
754 (A | B) & (A | C) into A | (B & C)
755 Further simplification will occur if B and C are constants. */
756 (for op (bit_and bit_ior bit_xor)
757 rop (bit_ior bit_and bit_and)
759 (op (convert? (rop:c @@0 @1)) (convert? (rop:c @0 @2)))
760 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
761 && tree_nop_conversion_p (type, TREE_TYPE (@2)))
762 (rop (convert @0) (op (convert @1) (convert @2))))))
764 /* Some simple reassociation for bit operations, also handled in reassoc. */
765 /* (X & Y) & Y -> X & Y
766 (X | Y) | Y -> X | Y */
767 (for op (bit_and bit_ior)
769 (op:c (convert1?@2 (op:c @0 @@1)) (convert2? @1))
771 /* (X ^ Y) ^ Y -> X */
773 (bit_xor:c (convert1? (bit_xor:c @0 @@1)) (convert2? @1))
775 /* (X & Y) & (X & Z) -> (X & Y) & Z
776 (X | Y) | (X | Z) -> (X | Y) | Z */
777 (for op (bit_and bit_ior)
779 (op:c (convert1?@3 (op:c@4 @0 @1)) (convert2?@5 (op:c@6 @0 @2)))
780 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
781 && tree_nop_conversion_p (type, TREE_TYPE (@2)))
782 (if (single_use (@5) && single_use (@6))
784 (if (single_use (@3) && single_use (@4))
785 (op (convert @1) @5))))))
786 /* (X ^ Y) ^ (X ^ Z) -> Y ^ Z */
788 (bit_xor (convert1? (bit_xor:c @0 @1)) (convert2? (bit_xor:c @0 @2)))
789 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
790 && tree_nop_conversion_p (type, TREE_TYPE (@2)))
791 (bit_xor (convert @1) (convert @2))))
800 (abs tree_expr_nonnegative_p@0)
803 /* A few cases of fold-const.c negate_expr_p predicate. */
806 (if ((INTEGRAL_TYPE_P (type)
807 && TYPE_OVERFLOW_WRAPS (type))
808 || (!TYPE_OVERFLOW_SANITIZED (type)
809 && may_negate_without_overflow_p (t)))))
814 (if (!TYPE_OVERFLOW_SANITIZED (type))))
817 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (t)))))
818 /* VECTOR_CST handling of non-wrapping types would recurse in unsupported
822 (if (FLOAT_TYPE_P (TREE_TYPE (type)) || TYPE_OVERFLOW_WRAPS (type))))
824 /* (-A) * (-B) -> A * B */
826 (mult:c (convert1? (negate @0)) (convert2? negate_expr_p@1))
827 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
828 && tree_nop_conversion_p (type, TREE_TYPE (@1)))
829 (mult (convert @0) (convert (negate @1)))))
831 /* -(A + B) -> (-B) - A. */
833 (negate (plus:c @0 negate_expr_p@1))
834 (if (!HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type))
835 && !HONOR_SIGNED_ZEROS (element_mode (type)))
836 (minus (negate @1) @0)))
838 /* A - B -> A + (-B) if B is easily negatable. */
840 (minus @0 negate_expr_p@1)
841 (if (!FIXED_POINT_TYPE_P (type))
842 (plus @0 (negate @1))))
844 /* Try to fold (type) X op CST -> (type) (X op ((type-x) CST))
846 For bitwise binary operations apply operand conversions to the
847 binary operation result instead of to the operands. This allows
848 to combine successive conversions and bitwise binary operations.
849 We combine the above two cases by using a conditional convert. */
850 (for bitop (bit_and bit_ior bit_xor)
852 (bitop (convert @0) (convert? @1))
853 (if (((TREE_CODE (@1) == INTEGER_CST
854 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
855 && int_fits_type_p (@1, TREE_TYPE (@0)))
856 || types_match (@0, @1))
857 /* ??? This transform conflicts with fold-const.c doing
858 Convert (T)(x & c) into (T)x & (T)c, if c is an integer
859 constants (if x has signed type, the sign bit cannot be set
860 in c). This folds extension into the BIT_AND_EXPR.
861 Restrict it to GIMPLE to avoid endless recursions. */
862 && (bitop != BIT_AND_EXPR || GIMPLE)
863 && (/* That's a good idea if the conversion widens the operand, thus
864 after hoisting the conversion the operation will be narrower. */
865 TYPE_PRECISION (TREE_TYPE (@0)) < TYPE_PRECISION (type)
866 /* It's also a good idea if the conversion is to a non-integer
868 || GET_MODE_CLASS (TYPE_MODE (type)) != MODE_INT
869 /* Or if the precision of TO is not the same as the precision
871 || TYPE_PRECISION (type) != GET_MODE_PRECISION (TYPE_MODE (type))))
872 (convert (bitop @0 (convert @1))))))
874 (for bitop (bit_and bit_ior)
875 rbitop (bit_ior bit_and)
876 /* (x | y) & x -> x */
877 /* (x & y) | x -> x */
879 (bitop:c (rbitop:c @0 @1) @0)
881 /* (~x | y) & x -> x & y */
882 /* (~x & y) | x -> x | y */
884 (bitop:c (rbitop:c (bit_not @0) @1) @0)
887 /* (x | CST1) & CST2 -> (x & CST2) | (CST1 & CST2) */
889 (bit_and (bit_ior @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
890 (bit_ior (bit_and @0 @2) (bit_and @1 @2)))
892 /* Combine successive equal operations with constants. */
893 (for bitop (bit_and bit_ior bit_xor)
895 (bitop (bitop @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
896 (bitop @0 (bitop @1 @2))))
898 /* Try simple folding for X op !X, and X op X with the help
899 of the truth_valued_p and logical_inverted_value predicates. */
900 (match truth_valued_p
902 (if (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1)))
903 (for op (tcc_comparison truth_and truth_andif truth_or truth_orif truth_xor)
904 (match truth_valued_p
906 (match truth_valued_p
909 (match (logical_inverted_value @0)
911 (match (logical_inverted_value @0)
912 (bit_not truth_valued_p@0))
913 (match (logical_inverted_value @0)
914 (eq @0 integer_zerop))
915 (match (logical_inverted_value @0)
916 (ne truth_valued_p@0 integer_truep))
917 (match (logical_inverted_value @0)
918 (bit_xor truth_valued_p@0 integer_truep))
922 (bit_and:c @0 (logical_inverted_value @0))
923 { build_zero_cst (type); })
924 /* X | !X and X ^ !X -> 1, , if X is truth-valued. */
925 (for op (bit_ior bit_xor)
927 (op:c truth_valued_p@0 (logical_inverted_value @0))
928 { constant_boolean_node (true, type); }))
929 /* X ==/!= !X is false/true. */
932 (op:c truth_valued_p@0 (logical_inverted_value @0))
933 { constant_boolean_node (op == NE_EXPR ? true : false, type); }))
935 /* If arg1 and arg2 are booleans (or any single bit type)
936 then try to simplify:
943 But only do this if our result feeds into a comparison as
944 this transformation is not always a win, particularly on
945 targets with and-not instructions.
946 -> simplify_bitwise_binary_boolean */
948 (ne (bit_and:c (bit_not @0) @1) integer_zerop)
949 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
950 && TYPE_PRECISION (TREE_TYPE (@1)) == 1)
951 (if (TYPE_UNSIGNED (TREE_TYPE (@1)))
955 (ne (bit_ior:c (bit_not @0) @1) integer_zerop)
956 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
957 && TYPE_PRECISION (TREE_TYPE (@1)) == 1)
958 (if (TYPE_UNSIGNED (TREE_TYPE (@1)))
964 (bit_not (bit_not @0))
967 /* Convert ~ (-A) to A - 1. */
969 (bit_not (convert? (negate @0)))
970 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
971 || !TYPE_UNSIGNED (TREE_TYPE (@0)))
972 (convert (minus @0 { build_each_one_cst (TREE_TYPE (@0)); }))))
974 /* Convert ~ (A - 1) or ~ (A + -1) to -A. */
976 (bit_not (convert? (minus @0 integer_each_onep)))
977 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
978 || !TYPE_UNSIGNED (TREE_TYPE (@0)))
979 (convert (negate @0))))
981 (bit_not (convert? (plus @0 integer_all_onesp)))
982 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
983 || !TYPE_UNSIGNED (TREE_TYPE (@0)))
984 (convert (negate @0))))
986 /* Part of convert ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify. */
988 (bit_not (convert? (bit_xor @0 INTEGER_CST@1)))
989 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
990 (convert (bit_xor @0 (bit_not @1)))))
992 (bit_not (convert? (bit_xor:c (bit_not @0) @1)))
993 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
994 (convert (bit_xor @0 @1))))
996 /* (x & ~m) | (y & m) -> ((x ^ y) & m) ^ x */
998 (bit_ior:c (bit_and:cs @0 (bit_not @2)) (bit_and:cs @1 @2))
999 (bit_xor (bit_and (bit_xor @0 @1) @2) @0))
1001 /* Fold A - (A & B) into ~B & A. */
1003 (minus (convert1? @0) (convert2?:s (bit_and:cs @@0 @1)))
1004 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
1005 && tree_nop_conversion_p (type, TREE_TYPE (@1)))
1006 (convert (bit_and (bit_not @1) @0))))
1010 /* ((X inner_op C0) outer_op C1)
1011 With X being a tree where value_range has reasoned certain bits to always be
1012 zero throughout its computed value range,
1013 inner_op = {|,^}, outer_op = {|,^} and inner_op != outer_op
1014 where zero_mask has 1's for all bits that are sure to be 0 in
1016 if (inner_op == '^') C0 &= ~C1;
1017 if ((C0 & ~zero_mask) == 0) then emit (X outer_op (C0 outer_op C1)
1018 if ((C1 & ~zero_mask) == 0) then emit (X inner_op (C0 outer_op C1)
1020 (for inner_op (bit_ior bit_xor)
1021 outer_op (bit_xor bit_ior)
1024 (inner_op:s @2 INTEGER_CST@0) INTEGER_CST@1)
1028 wide_int zero_mask_not;
1032 if (TREE_CODE (@2) == SSA_NAME)
1033 zero_mask_not = get_nonzero_bits (@2);
1037 if (inner_op == BIT_XOR_EXPR)
1039 C0 = wi::bit_and_not (@0, @1);
1040 cst_emit = wi::bit_or (C0, @1);
1045 cst_emit = wi::bit_xor (@0, @1);
1048 (if (!fail && wi::bit_and (C0, zero_mask_not) == 0)
1049 (outer_op @2 { wide_int_to_tree (type, cst_emit); })
1050 (if (!fail && wi::bit_and (@1, zero_mask_not) == 0)
1051 (inner_op @2 { wide_int_to_tree (type, cst_emit); }))))))
1053 /* Associate (p +p off1) +p off2 as (p +p (off1 + off2)). */
1055 (pointer_plus (pointer_plus:s @0 @1) @3)
1056 (pointer_plus @0 (plus @1 @3)))
1062 tem4 = (unsigned long) tem3;
1067 (pointer_plus @0 (convert?@2 (minus@3 (convert @1) (convert @0))))
1068 /* Conditionally look through a sign-changing conversion. */
1069 (if (TYPE_PRECISION (TREE_TYPE (@2)) == TYPE_PRECISION (TREE_TYPE (@3))
1070 && ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@1)))
1071 || (GENERIC && type == TREE_TYPE (@1))))
1075 tem = (sizetype) ptr;
1079 and produce the simpler and easier to analyze with respect to alignment
1080 ... = ptr & ~algn; */
1082 (pointer_plus @0 (negate (bit_and (convert @0) INTEGER_CST@1)))
1083 (with { tree algn = wide_int_to_tree (TREE_TYPE (@0), wi::bit_not (@1)); }
1084 (bit_and @0 { algn; })))
1086 /* Try folding difference of addresses. */
1088 (minus (convert ADDR_EXPR@0) (convert @1))
1089 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1090 (with { HOST_WIDE_INT diff; }
1091 (if (ptr_difference_const (@0, @1, &diff))
1092 { build_int_cst_type (type, diff); }))))
1094 (minus (convert @0) (convert ADDR_EXPR@1))
1095 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1096 (with { HOST_WIDE_INT diff; }
1097 (if (ptr_difference_const (@0, @1, &diff))
1098 { build_int_cst_type (type, diff); }))))
1100 /* If arg0 is derived from the address of an object or function, we may
1101 be able to fold this expression using the object or function's
1104 (bit_and (convert? @0) INTEGER_CST@1)
1105 (if (POINTER_TYPE_P (TREE_TYPE (@0))
1106 && tree_nop_conversion_p (type, TREE_TYPE (@0)))
1110 unsigned HOST_WIDE_INT bitpos;
1111 get_pointer_alignment_1 (@0, &align, &bitpos);
1113 (if (wi::ltu_p (@1, align / BITS_PER_UNIT))
1114 { wide_int_to_tree (type, wi::bit_and (@1, bitpos / BITS_PER_UNIT)); }))))
1117 /* We can't reassociate at all for saturating types. */
1118 (if (!TYPE_SATURATING (type))
1120 /* Contract negates. */
1121 /* A + (-B) -> A - B */
1123 (plus:c (convert1? @0) (convert2? (negate @1)))
1124 /* Apply STRIP_NOPS on @0 and the negate. */
1125 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
1126 && tree_nop_conversion_p (type, TREE_TYPE (@1))
1127 && !TYPE_OVERFLOW_SANITIZED (type))
1128 (minus (convert @0) (convert @1))))
1129 /* A - (-B) -> A + B */
1131 (minus (convert1? @0) (convert2? (negate @1)))
1132 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
1133 && tree_nop_conversion_p (type, TREE_TYPE (@1))
1134 && !TYPE_OVERFLOW_SANITIZED (type))
1135 (plus (convert @0) (convert @1))))
1138 (negate (convert? (negate @1)))
1139 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1140 && !TYPE_OVERFLOW_SANITIZED (type))
1143 /* We can't reassociate floating-point unless -fassociative-math
1144 or fixed-point plus or minus because of saturation to +-Inf. */
1145 (if ((!FLOAT_TYPE_P (type) || flag_associative_math)
1146 && !FIXED_POINT_TYPE_P (type))
1148 /* Match patterns that allow contracting a plus-minus pair
1149 irrespective of overflow issues. */
1150 /* (A +- B) - A -> +- B */
1151 /* (A +- B) -+ B -> A */
1152 /* A - (A +- B) -> -+ B */
1153 /* A +- (B -+ A) -> +- B */
1155 (minus (plus:c @0 @1) @0)
1158 (minus (minus @0 @1) @0)
1161 (plus:c (minus @0 @1) @1)
1164 (minus @0 (plus:c @0 @1))
1167 (minus @0 (minus @0 @1))
1170 /* (A +- CST) +- CST -> A + CST */
1171 (for outer_op (plus minus)
1172 (for inner_op (plus minus)
1174 (outer_op (inner_op @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
1175 /* If the constant operation overflows we cannot do the transform
1176 as we would introduce undefined overflow, for example
1177 with (a - 1) + INT_MIN. */
1178 (with { tree cst = const_binop (outer_op == inner_op
1179 ? PLUS_EXPR : MINUS_EXPR, type, @1, @2); }
1180 (if (cst && !TREE_OVERFLOW (cst))
1181 (inner_op @0 { cst; } ))))))
1183 /* (CST - A) +- CST -> CST - A */
1184 (for outer_op (plus minus)
1186 (outer_op (minus CONSTANT_CLASS_P@1 @0) CONSTANT_CLASS_P@2)
1187 (with { tree cst = const_binop (outer_op, type, @1, @2); }
1188 (if (cst && !TREE_OVERFLOW (cst))
1189 (minus { cst; } @0)))))
1193 (plus:c (bit_not @0) @0)
1194 (if (!TYPE_OVERFLOW_TRAPS (type))
1195 { build_all_ones_cst (type); }))
1199 (plus (convert? (bit_not @0)) integer_each_onep)
1200 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1201 (negate (convert @0))))
1205 (minus (convert? (negate @0)) integer_each_onep)
1206 (if (!TYPE_OVERFLOW_TRAPS (type)
1207 && tree_nop_conversion_p (type, TREE_TYPE (@0)))
1208 (bit_not (convert @0))))
1212 (minus integer_all_onesp @0)
1215 /* (T)(P + A) - (T)P -> (T) A */
1216 (for add (plus pointer_plus)
1218 (minus (convert (add @@0 @1))
1220 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
1221 /* For integer types, if A has a smaller type
1222 than T the result depends on the possible
1224 E.g. T=size_t, A=(unsigned)429497295, P>0.
1225 However, if an overflow in P + A would cause
1226 undefined behavior, we can assume that there
1228 || (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1229 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1230 /* For pointer types, if the conversion of A to the
1231 final type requires a sign- or zero-extension,
1232 then we have to punt - it is not defined which
1234 || (POINTER_TYPE_P (TREE_TYPE (@0))
1235 && TREE_CODE (@1) == INTEGER_CST
1236 && tree_int_cst_sign_bit (@1) == 0))
1239 /* (T)P - (T)(P + A) -> -(T) A */
1240 (for add (plus pointer_plus)
1243 (convert (add @@0 @1)))
1244 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
1245 /* For integer types, if A has a smaller type
1246 than T the result depends on the possible
1248 E.g. T=size_t, A=(unsigned)429497295, P>0.
1249 However, if an overflow in P + A would cause
1250 undefined behavior, we can assume that there
1252 || (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1253 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1254 /* For pointer types, if the conversion of A to the
1255 final type requires a sign- or zero-extension,
1256 then we have to punt - it is not defined which
1258 || (POINTER_TYPE_P (TREE_TYPE (@0))
1259 && TREE_CODE (@1) == INTEGER_CST
1260 && tree_int_cst_sign_bit (@1) == 0))
1261 (negate (convert @1)))))
1263 /* (T)(P + A) - (T)(P + B) -> (T)A - (T)B */
1264 (for add (plus pointer_plus)
1266 (minus (convert (add @@0 @1))
1267 (convert (add @0 @2)))
1268 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
1269 /* For integer types, if A has a smaller type
1270 than T the result depends on the possible
1272 E.g. T=size_t, A=(unsigned)429497295, P>0.
1273 However, if an overflow in P + A would cause
1274 undefined behavior, we can assume that there
1276 || (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1277 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1278 /* For pointer types, if the conversion of A to the
1279 final type requires a sign- or zero-extension,
1280 then we have to punt - it is not defined which
1282 || (POINTER_TYPE_P (TREE_TYPE (@0))
1283 && TREE_CODE (@1) == INTEGER_CST
1284 && tree_int_cst_sign_bit (@1) == 0
1285 && TREE_CODE (@2) == INTEGER_CST
1286 && tree_int_cst_sign_bit (@2) == 0))
1287 (minus (convert @1) (convert @2)))))))
1290 /* Simplifications of MIN_EXPR, MAX_EXPR, fmin() and fmax(). */
1292 (for minmax (min max FMIN FMAX)
1296 /* min(max(x,y),y) -> y. */
1298 (min:c (max:c @0 @1) @1)
1300 /* max(min(x,y),y) -> y. */
1302 (max:c (min:c @0 @1) @1)
1304 /* max(a,-a) -> abs(a). */
1306 (max:c @0 (negate @0))
1307 (if (TREE_CODE (type) != COMPLEX_TYPE
1308 && (! ANY_INTEGRAL_TYPE_P (type)
1309 || TYPE_OVERFLOW_UNDEFINED (type)))
1311 /* min(a,-a) -> -abs(a). */
1313 (min:c @0 (negate @0))
1314 (if (TREE_CODE (type) != COMPLEX_TYPE
1315 && (! ANY_INTEGRAL_TYPE_P (type)
1316 || TYPE_OVERFLOW_UNDEFINED (type)))
1321 (if (INTEGRAL_TYPE_P (type)
1322 && TYPE_MIN_VALUE (type)
1323 && operand_equal_p (@1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
1325 (if (INTEGRAL_TYPE_P (type)
1326 && TYPE_MAX_VALUE (type)
1327 && operand_equal_p (@1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
1332 (if (INTEGRAL_TYPE_P (type)
1333 && TYPE_MAX_VALUE (type)
1334 && operand_equal_p (@1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
1336 (if (INTEGRAL_TYPE_P (type)
1337 && TYPE_MIN_VALUE (type)
1338 && operand_equal_p (@1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
1341 /* (convert (minmax ((convert (x) c)))) -> minmax (x c) if x is promoted
1342 and the outer convert demotes the expression back to x's type. */
1343 (for minmax (min max)
1345 (convert (minmax@0 (convert @1) INTEGER_CST@2))
1346 (if (types_match (@1, type) && int_fits_type_p (@2, type)
1347 && TYPE_SIGN (TREE_TYPE (@0)) == TYPE_SIGN (type)
1348 && TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type))
1349 (minmax @1 (convert @2)))))
1351 (for minmax (FMIN FMAX)
1352 /* If either argument is NaN, return the other one. Avoid the
1353 transformation if we get (and honor) a signalling NaN. */
1355 (minmax:c @0 REAL_CST@1)
1356 (if (real_isnan (TREE_REAL_CST_PTR (@1))
1357 && (!HONOR_SNANS (@1) || !TREE_REAL_CST (@1).signalling))
1359 /* Convert fmin/fmax to MIN_EXPR/MAX_EXPR. C99 requires these
1360 functions to return the numeric arg if the other one is NaN.
1361 MIN and MAX don't honor that, so only transform if -ffinite-math-only
1362 is set. C99 doesn't require -0.0 to be handled, so we don't have to
1363 worry about it either. */
1364 (if (flag_finite_math_only)
1371 /* min (-A, -B) -> -max (A, B) */
1372 (for minmax (min max FMIN FMAX)
1373 maxmin (max min FMAX FMIN)
1375 (minmax (negate:s@2 @0) (negate:s@3 @1))
1376 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
1377 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1378 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
1379 (negate (maxmin @0 @1)))))
1380 /* MIN (~X, ~Y) -> ~MAX (X, Y)
1381 MAX (~X, ~Y) -> ~MIN (X, Y) */
1382 (for minmax (min max)
1385 (minmax (bit_not:s@2 @0) (bit_not:s@3 @1))
1386 (bit_not (maxmin @0 @1))))
1388 /* MIN (X, Y) == X -> X <= Y */
1389 (for minmax (min min max max)
1393 (cmp:c (minmax:c @0 @1) @0)
1394 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0)))
1396 /* MIN (X, 5) == 0 -> X == 0
1397 MIN (X, 5) == 7 -> false */
1400 (cmp (min @0 INTEGER_CST@1) INTEGER_CST@2)
1401 (if (wi::lt_p (@1, @2, TYPE_SIGN (TREE_TYPE (@0))))
1402 { constant_boolean_node (cmp == NE_EXPR, type); }
1403 (if (wi::gt_p (@1, @2, TYPE_SIGN (TREE_TYPE (@0))))
1407 (cmp (max @0 INTEGER_CST@1) INTEGER_CST@2)
1408 (if (wi::gt_p (@1, @2, TYPE_SIGN (TREE_TYPE (@0))))
1409 { constant_boolean_node (cmp == NE_EXPR, type); }
1410 (if (wi::lt_p (@1, @2, TYPE_SIGN (TREE_TYPE (@0))))
1412 /* MIN (X, C1) < C2 -> X < C2 || C1 < C2 */
1413 (for minmax (min min max max min min max max )
1414 cmp (lt le gt ge gt ge lt le )
1415 comb (bit_ior bit_ior bit_ior bit_ior bit_and bit_and bit_and bit_and)
1417 (cmp (minmax @0 INTEGER_CST@1) INTEGER_CST@2)
1418 (comb (cmp @0 @2) (cmp @1 @2))))
1420 /* Simplifications of shift and rotates. */
1422 (for rotate (lrotate rrotate)
1424 (rotate integer_all_onesp@0 @1)
1427 /* Optimize -1 >> x for arithmetic right shifts. */
1429 (rshift integer_all_onesp@0 @1)
1430 (if (!TYPE_UNSIGNED (type)
1431 && tree_expr_nonnegative_p (@1))
1434 /* Optimize (x >> c) << c into x & (-1<<c). */
1436 (lshift (rshift @0 INTEGER_CST@1) @1)
1437 (if (wi::ltu_p (@1, element_precision (type)))
1438 (bit_and @0 (lshift { build_minus_one_cst (type); } @1))))
1440 /* Optimize (x << c) >> c into x & ((unsigned)-1 >> c) for unsigned
1443 (rshift (lshift @0 INTEGER_CST@1) @1)
1444 (if (TYPE_UNSIGNED (type)
1445 && (wi::ltu_p (@1, element_precision (type))))
1446 (bit_and @0 (rshift { build_minus_one_cst (type); } @1))))
1448 (for shiftrotate (lrotate rrotate lshift rshift)
1450 (shiftrotate @0 integer_zerop)
1453 (shiftrotate integer_zerop@0 @1)
1455 /* Prefer vector1 << scalar to vector1 << vector2
1456 if vector2 is uniform. */
1457 (for vec (VECTOR_CST CONSTRUCTOR)
1459 (shiftrotate @0 vec@1)
1460 (with { tree tem = uniform_vector_p (@1); }
1462 (shiftrotate @0 { tem; }))))))
1464 /* Rewrite an LROTATE_EXPR by a constant into an
1465 RROTATE_EXPR by a new constant. */
1467 (lrotate @0 INTEGER_CST@1)
1468 (rrotate @0 { const_binop (MINUS_EXPR, TREE_TYPE (@1),
1469 build_int_cst (TREE_TYPE (@1),
1470 element_precision (type)), @1); }))
1472 /* Turn (a OP c1) OP c2 into a OP (c1+c2). */
1473 (for op (lrotate rrotate rshift lshift)
1475 (op (op @0 INTEGER_CST@1) INTEGER_CST@2)
1476 (with { unsigned int prec = element_precision (type); }
1477 (if (wi::ge_p (@1, 0, TYPE_SIGN (TREE_TYPE (@1)))
1478 && wi::lt_p (@1, prec, TYPE_SIGN (TREE_TYPE (@1)))
1479 && wi::ge_p (@2, 0, TYPE_SIGN (TREE_TYPE (@2)))
1480 && wi::lt_p (@2, prec, TYPE_SIGN (TREE_TYPE (@2))))
1481 (with { unsigned int low = wi::add (@1, @2).to_uhwi (); }
1482 /* Deal with a OP (c1 + c2) being undefined but (a OP c1) OP c2
1483 being well defined. */
1485 (if (op == LROTATE_EXPR || op == RROTATE_EXPR)
1486 (op @0 { build_int_cst (TREE_TYPE (@1), low % prec); })
1487 (if (TYPE_UNSIGNED (type) || op == LSHIFT_EXPR)
1488 { build_zero_cst (type); }
1489 (op @0 { build_int_cst (TREE_TYPE (@1), prec - 1); })))
1490 (op @0 { build_int_cst (TREE_TYPE (@1), low); })))))))
1493 /* ((1 << A) & 1) != 0 -> A == 0
1494 ((1 << A) & 1) == 0 -> A != 0 */
1498 (cmp (bit_and (lshift integer_onep @0) integer_onep) integer_zerop)
1499 (icmp @0 { build_zero_cst (TREE_TYPE (@0)); })))
1501 /* (CST1 << A) == CST2 -> A == ctz (CST2) - ctz (CST1)
1502 (CST1 << A) != CST2 -> A != ctz (CST2) - ctz (CST1)
1506 (cmp (lshift INTEGER_CST@0 @1) INTEGER_CST@2)
1507 (with { int cand = wi::ctz (@2) - wi::ctz (@0); }
1509 || (!integer_zerop (@2)
1510 && wi::ne_p (wi::lshift (@0, cand), @2)))
1511 { constant_boolean_node (cmp == NE_EXPR, type); }
1512 (if (!integer_zerop (@2)
1513 && wi::eq_p (wi::lshift (@0, cand), @2))
1514 (cmp @1 { build_int_cst (TREE_TYPE (@1), cand); }))))))
1516 /* Fold (X << C1) & C2 into (X << C1) & (C2 | ((1 << C1) - 1))
1517 (X >> C1) & C2 into (X >> C1) & (C2 | ~((type) -1 >> C1))
1518 if the new mask might be further optimized. */
1519 (for shift (lshift rshift)
1521 (bit_and (convert?:s@4 (shift:s@5 (convert1?@3 @0) INTEGER_CST@1))
1523 (if (tree_nop_conversion_p (TREE_TYPE (@4), TREE_TYPE (@5))
1524 && TYPE_PRECISION (type) <= HOST_BITS_PER_WIDE_INT
1525 && tree_fits_uhwi_p (@1)
1526 && tree_to_uhwi (@1) > 0
1527 && tree_to_uhwi (@1) < TYPE_PRECISION (type))
1530 unsigned int shiftc = tree_to_uhwi (@1);
1531 unsigned HOST_WIDE_INT mask = TREE_INT_CST_LOW (@2);
1532 unsigned HOST_WIDE_INT newmask, zerobits = 0;
1533 tree shift_type = TREE_TYPE (@3);
1536 if (shift == LSHIFT_EXPR)
1537 zerobits = ((HOST_WIDE_INT_1U << shiftc) - 1);
1538 else if (shift == RSHIFT_EXPR
1539 && (TYPE_PRECISION (shift_type)
1540 == GET_MODE_PRECISION (TYPE_MODE (shift_type))))
1542 prec = TYPE_PRECISION (TREE_TYPE (@3));
1544 /* See if more bits can be proven as zero because of
1547 && TYPE_UNSIGNED (TREE_TYPE (@0)))
1549 tree inner_type = TREE_TYPE (@0);
1550 if ((TYPE_PRECISION (inner_type)
1551 == GET_MODE_PRECISION (TYPE_MODE (inner_type)))
1552 && TYPE_PRECISION (inner_type) < prec)
1554 prec = TYPE_PRECISION (inner_type);
1555 /* See if we can shorten the right shift. */
1557 shift_type = inner_type;
1558 /* Otherwise X >> C1 is all zeros, so we'll optimize
1559 it into (X, 0) later on by making sure zerobits
1563 zerobits = HOST_WIDE_INT_M1U;
1566 zerobits >>= HOST_BITS_PER_WIDE_INT - shiftc;
1567 zerobits <<= prec - shiftc;
1569 /* For arithmetic shift if sign bit could be set, zerobits
1570 can contain actually sign bits, so no transformation is
1571 possible, unless MASK masks them all away. In that
1572 case the shift needs to be converted into logical shift. */
1573 if (!TYPE_UNSIGNED (TREE_TYPE (@3))
1574 && prec == TYPE_PRECISION (TREE_TYPE (@3)))
1576 if ((mask & zerobits) == 0)
1577 shift_type = unsigned_type_for (TREE_TYPE (@3));
1583 /* ((X << 16) & 0xff00) is (X, 0). */
1584 (if ((mask & zerobits) == mask)
1585 { build_int_cst (type, 0); }
1586 (with { newmask = mask | zerobits; }
1587 (if (newmask != mask && (newmask & (newmask + 1)) == 0)
1590 /* Only do the transformation if NEWMASK is some integer
1592 for (prec = BITS_PER_UNIT;
1593 prec < HOST_BITS_PER_WIDE_INT; prec <<= 1)
1594 if (newmask == (HOST_WIDE_INT_1U << prec) - 1)
1597 (if (prec < HOST_BITS_PER_WIDE_INT
1598 || newmask == HOST_WIDE_INT_M1U)
1600 { tree newmaskt = build_int_cst_type (TREE_TYPE (@2), newmask); }
1601 (if (!tree_int_cst_equal (newmaskt, @2))
1602 (if (shift_type != TREE_TYPE (@3))
1603 (bit_and (convert (shift:shift_type (convert @3) @1)) { newmaskt; })
1604 (bit_and @4 { newmaskt; })))))))))))))
1606 /* Fold (X {&,^,|} C2) << C1 into (X << C1) {&,^,|} (C2 << C1)
1607 (X {&,^,|} C2) >> C1 into (X >> C1) & (C2 >> C1). */
1608 (for shift (lshift rshift)
1609 (for bit_op (bit_and bit_xor bit_ior)
1611 (shift (convert?:s (bit_op:s @0 INTEGER_CST@2)) INTEGER_CST@1)
1612 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1613 (with { tree mask = int_const_binop (shift, fold_convert (type, @2), @1); }
1614 (bit_op (shift (convert @0) @1) { mask; }))))))
1616 /* ~(~X >> Y) -> X >> Y (for arithmetic shift). */
1618 (bit_not (convert1?:s (rshift:s (convert2?@0 (bit_not @1)) @2)))
1619 (if (!TYPE_UNSIGNED (TREE_TYPE (@0))
1620 && (element_precision (TREE_TYPE (@0))
1621 <= element_precision (TREE_TYPE (@1))
1622 || !TYPE_UNSIGNED (TREE_TYPE (@1))))
1624 { tree shift_type = TREE_TYPE (@0); }
1625 (convert (rshift (convert:shift_type @1) @2)))))
1627 /* ~(~X >>r Y) -> X >>r Y
1628 ~(~X <<r Y) -> X <<r Y */
1629 (for rotate (lrotate rrotate)
1631 (bit_not (convert1?:s (rotate:s (convert2?@0 (bit_not @1)) @2)))
1632 (if ((element_precision (TREE_TYPE (@0))
1633 <= element_precision (TREE_TYPE (@1))
1634 || !TYPE_UNSIGNED (TREE_TYPE (@1)))
1635 && (element_precision (type) <= element_precision (TREE_TYPE (@0))
1636 || !TYPE_UNSIGNED (TREE_TYPE (@0))))
1638 { tree rotate_type = TREE_TYPE (@0); }
1639 (convert (rotate (convert:rotate_type @1) @2))))))
1641 /* Simplifications of conversions. */
1643 /* Basic strip-useless-type-conversions / strip_nops. */
1644 (for cvt (convert view_convert float fix_trunc)
1647 (if ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@0)))
1648 || (GENERIC && type == TREE_TYPE (@0)))
1651 /* Contract view-conversions. */
1653 (view_convert (view_convert @0))
1656 /* For integral conversions with the same precision or pointer
1657 conversions use a NOP_EXPR instead. */
1660 (if ((INTEGRAL_TYPE_P (type) || POINTER_TYPE_P (type))
1661 && (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
1662 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (@0)))
1665 /* Strip inner integral conversions that do not change precision or size. */
1667 (view_convert (convert@0 @1))
1668 (if ((INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
1669 && (INTEGRAL_TYPE_P (TREE_TYPE (@1)) || POINTER_TYPE_P (TREE_TYPE (@1)))
1670 && (TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
1671 && (TYPE_SIZE (TREE_TYPE (@0)) == TYPE_SIZE (TREE_TYPE (@1))))
1674 /* Re-association barriers around constants and other re-association
1675 barriers can be removed. */
1677 (paren CONSTANT_CLASS_P@0)
1680 (paren (paren@1 @0))
1683 /* Handle cases of two conversions in a row. */
1684 (for ocvt (convert float fix_trunc)
1685 (for icvt (convert float)
1690 tree inside_type = TREE_TYPE (@0);
1691 tree inter_type = TREE_TYPE (@1);
1692 int inside_int = INTEGRAL_TYPE_P (inside_type);
1693 int inside_ptr = POINTER_TYPE_P (inside_type);
1694 int inside_float = FLOAT_TYPE_P (inside_type);
1695 int inside_vec = VECTOR_TYPE_P (inside_type);
1696 unsigned int inside_prec = TYPE_PRECISION (inside_type);
1697 int inside_unsignedp = TYPE_UNSIGNED (inside_type);
1698 int inter_int = INTEGRAL_TYPE_P (inter_type);
1699 int inter_ptr = POINTER_TYPE_P (inter_type);
1700 int inter_float = FLOAT_TYPE_P (inter_type);
1701 int inter_vec = VECTOR_TYPE_P (inter_type);
1702 unsigned int inter_prec = TYPE_PRECISION (inter_type);
1703 int inter_unsignedp = TYPE_UNSIGNED (inter_type);
1704 int final_int = INTEGRAL_TYPE_P (type);
1705 int final_ptr = POINTER_TYPE_P (type);
1706 int final_float = FLOAT_TYPE_P (type);
1707 int final_vec = VECTOR_TYPE_P (type);
1708 unsigned int final_prec = TYPE_PRECISION (type);
1709 int final_unsignedp = TYPE_UNSIGNED (type);
1712 /* In addition to the cases of two conversions in a row
1713 handled below, if we are converting something to its own
1714 type via an object of identical or wider precision, neither
1715 conversion is needed. */
1716 (if (((GIMPLE && useless_type_conversion_p (type, inside_type))
1718 && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (inside_type)))
1719 && (((inter_int || inter_ptr) && final_int)
1720 || (inter_float && final_float))
1721 && inter_prec >= final_prec)
1724 /* Likewise, if the intermediate and initial types are either both
1725 float or both integer, we don't need the middle conversion if the
1726 former is wider than the latter and doesn't change the signedness
1727 (for integers). Avoid this if the final type is a pointer since
1728 then we sometimes need the middle conversion. */
1729 (if (((inter_int && inside_int) || (inter_float && inside_float))
1730 && (final_int || final_float)
1731 && inter_prec >= inside_prec
1732 && (inter_float || inter_unsignedp == inside_unsignedp))
1735 /* If we have a sign-extension of a zero-extended value, we can
1736 replace that by a single zero-extension. Likewise if the
1737 final conversion does not change precision we can drop the
1738 intermediate conversion. */
1739 (if (inside_int && inter_int && final_int
1740 && ((inside_prec < inter_prec && inter_prec < final_prec
1741 && inside_unsignedp && !inter_unsignedp)
1742 || final_prec == inter_prec))
1745 /* Two conversions in a row are not needed unless:
1746 - some conversion is floating-point (overstrict for now), or
1747 - some conversion is a vector (overstrict for now), or
1748 - the intermediate type is narrower than both initial and
1750 - the intermediate type and innermost type differ in signedness,
1751 and the outermost type is wider than the intermediate, or
1752 - the initial type is a pointer type and the precisions of the
1753 intermediate and final types differ, or
1754 - the final type is a pointer type and the precisions of the
1755 initial and intermediate types differ. */
1756 (if (! inside_float && ! inter_float && ! final_float
1757 && ! inside_vec && ! inter_vec && ! final_vec
1758 && (inter_prec >= inside_prec || inter_prec >= final_prec)
1759 && ! (inside_int && inter_int
1760 && inter_unsignedp != inside_unsignedp
1761 && inter_prec < final_prec)
1762 && ((inter_unsignedp && inter_prec > inside_prec)
1763 == (final_unsignedp && final_prec > inter_prec))
1764 && ! (inside_ptr && inter_prec != final_prec)
1765 && ! (final_ptr && inside_prec != inter_prec))
1768 /* A truncation to an unsigned type (a zero-extension) should be
1769 canonicalized as bitwise and of a mask. */
1770 (if (GIMPLE /* PR70366: doing this in GENERIC breaks -Wconversion. */
1771 && final_int && inter_int && inside_int
1772 && final_prec == inside_prec
1773 && final_prec > inter_prec
1775 (convert (bit_and @0 { wide_int_to_tree
1777 wi::mask (inter_prec, false,
1778 TYPE_PRECISION (inside_type))); })))
1780 /* If we are converting an integer to a floating-point that can
1781 represent it exactly and back to an integer, we can skip the
1782 floating-point conversion. */
1783 (if (GIMPLE /* PR66211 */
1784 && inside_int && inter_float && final_int &&
1785 (unsigned) significand_size (TYPE_MODE (inter_type))
1786 >= inside_prec - !inside_unsignedp)
1789 /* If we have a narrowing conversion to an integral type that is fed by a
1790 BIT_AND_EXPR, we might be able to remove the BIT_AND_EXPR if it merely
1791 masks off bits outside the final type (and nothing else). */
1793 (convert (bit_and @0 INTEGER_CST@1))
1794 (if (INTEGRAL_TYPE_P (type)
1795 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
1796 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))
1797 && operand_equal_p (@1, build_low_bits_mask (TREE_TYPE (@1),
1798 TYPE_PRECISION (type)), 0))
1802 /* (X /[ex] A) * A -> X. */
1804 (mult (convert1? (exact_div @0 @@1)) (convert2? @1))
1807 /* Canonicalization of binary operations. */
1809 /* Convert X + -C into X - C. */
1811 (plus @0 REAL_CST@1)
1812 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
1813 (with { tree tem = const_unop (NEGATE_EXPR, type, @1); }
1814 (if (!TREE_OVERFLOW (tem) || !flag_trapping_math)
1815 (minus @0 { tem; })))))
1817 /* Convert x+x into x*2. */
1820 (if (SCALAR_FLOAT_TYPE_P (type))
1821 (mult @0 { build_real (type, dconst2); })
1822 (if (INTEGRAL_TYPE_P (type))
1823 (mult @0 { build_int_cst (type, 2); }))))
1826 (minus integer_zerop @1)
1829 /* (ARG0 - ARG1) is the same as (-ARG1 + ARG0). So check whether
1830 ARG0 is zero and X + ARG0 reduces to X, since that would mean
1831 (-ARG1 + ARG0) reduces to -ARG1. */
1833 (minus real_zerop@0 @1)
1834 (if (fold_real_zero_addition_p (type, @0, 0))
1837 /* Transform x * -1 into -x. */
1839 (mult @0 integer_minus_onep)
1842 /* True if we can easily extract the real and imaginary parts of a complex
1844 (match compositional_complex
1845 (convert? (complex @0 @1)))
1847 /* COMPLEX_EXPR and REALPART/IMAGPART_EXPR cancellations. */
1849 (complex (realpart @0) (imagpart @0))
1852 (realpart (complex @0 @1))
1855 (imagpart (complex @0 @1))
1858 /* Sometimes we only care about half of a complex expression. */
1860 (realpart (convert?:s (conj:s @0)))
1861 (convert (realpart @0)))
1863 (imagpart (convert?:s (conj:s @0)))
1864 (convert (negate (imagpart @0))))
1865 (for part (realpart imagpart)
1866 (for op (plus minus)
1868 (part (convert?:s@2 (op:s @0 @1)))
1869 (convert (op (part @0) (part @1))))))
1871 (realpart (convert?:s (CEXPI:s @0)))
1874 (imagpart (convert?:s (CEXPI:s @0)))
1877 /* conj(conj(x)) -> x */
1879 (conj (convert? (conj @0)))
1880 (if (tree_nop_conversion_p (TREE_TYPE (@0), type))
1883 /* conj({x,y}) -> {x,-y} */
1885 (conj (convert?:s (complex:s @0 @1)))
1886 (with { tree itype = TREE_TYPE (type); }
1887 (complex (convert:itype @0) (negate (convert:itype @1)))))
1889 /* BSWAP simplifications, transforms checked by gcc.dg/builtin-bswap-8.c. */
1890 (for bswap (BUILT_IN_BSWAP16 BUILT_IN_BSWAP32 BUILT_IN_BSWAP64)
1895 (bswap (bit_not (bswap @0)))
1897 (for bitop (bit_xor bit_ior bit_and)
1899 (bswap (bitop:c (bswap @0) @1))
1900 (bitop @0 (bswap @1)))))
1903 /* Combine COND_EXPRs and VEC_COND_EXPRs. */
1905 /* Simplify constant conditions.
1906 Only optimize constant conditions when the selected branch
1907 has the same type as the COND_EXPR. This avoids optimizing
1908 away "c ? x : throw", where the throw has a void type.
1909 Note that we cannot throw away the fold-const.c variant nor
1910 this one as we depend on doing this transform before possibly
1911 A ? B : B -> B triggers and the fold-const.c one can optimize
1912 0 ? A : B to B even if A has side-effects. Something
1913 genmatch cannot handle. */
1915 (cond INTEGER_CST@0 @1 @2)
1916 (if (integer_zerop (@0))
1917 (if (!VOID_TYPE_P (TREE_TYPE (@2)) || VOID_TYPE_P (type))
1919 (if (!VOID_TYPE_P (TREE_TYPE (@1)) || VOID_TYPE_P (type))
1922 (vec_cond VECTOR_CST@0 @1 @2)
1923 (if (integer_all_onesp (@0))
1925 (if (integer_zerop (@0))
1928 (for cnd (cond vec_cond)
1929 /* A ? B : (A ? X : C) -> A ? B : C. */
1931 (cnd @0 (cnd @0 @1 @2) @3)
1934 (cnd @0 @1 (cnd @0 @2 @3))
1936 /* A ? B : (!A ? C : X) -> A ? B : C. */
1937 /* ??? This matches embedded conditions open-coded because genmatch
1938 would generate matching code for conditions in separate stmts only.
1939 The following is still important to merge then and else arm cases
1940 from if-conversion. */
1942 (cnd @0 @1 (cnd @2 @3 @4))
1943 (if (COMPARISON_CLASS_P (@0)
1944 && COMPARISON_CLASS_P (@2)
1945 && invert_tree_comparison
1946 (TREE_CODE (@0), HONOR_NANS (TREE_OPERAND (@0, 0))) == TREE_CODE (@2)
1947 && operand_equal_p (TREE_OPERAND (@0, 0), TREE_OPERAND (@2, 0), 0)
1948 && operand_equal_p (TREE_OPERAND (@0, 1), TREE_OPERAND (@2, 1), 0))
1951 (cnd @0 (cnd @1 @2 @3) @4)
1952 (if (COMPARISON_CLASS_P (@0)
1953 && COMPARISON_CLASS_P (@1)
1954 && invert_tree_comparison
1955 (TREE_CODE (@0), HONOR_NANS (TREE_OPERAND (@0, 0))) == TREE_CODE (@1)
1956 && operand_equal_p (TREE_OPERAND (@0, 0), TREE_OPERAND (@1, 0), 0)
1957 && operand_equal_p (TREE_OPERAND (@0, 1), TREE_OPERAND (@1, 1), 0))
1960 /* A ? B : B -> B. */
1965 /* !A ? B : C -> A ? C : B. */
1967 (cnd (logical_inverted_value truth_valued_p@0) @1 @2)
1970 /* A + (B vcmp C ? 1 : 0) -> A - (B vcmp C ? -1 : 0), since vector comparisons
1971 return all -1 or all 0 results. */
1972 /* ??? We could instead convert all instances of the vec_cond to negate,
1973 but that isn't necessarily a win on its own. */
1975 (plus:c @3 (view_convert? (vec_cond:s @0 integer_each_onep@1 integer_zerop@2)))
1976 (if (VECTOR_TYPE_P (type)
1977 && TYPE_VECTOR_SUBPARTS (type) == TYPE_VECTOR_SUBPARTS (TREE_TYPE (@1))
1978 && (TYPE_MODE (TREE_TYPE (type))
1979 == TYPE_MODE (TREE_TYPE (TREE_TYPE (@1)))))
1980 (minus @3 (view_convert (vec_cond @0 (negate @1) @2)))))
1982 /* ... likewise A - (B vcmp C ? 1 : 0) -> A + (B vcmp C ? -1 : 0). */
1984 (minus @3 (view_convert? (vec_cond:s @0 integer_each_onep@1 integer_zerop@2)))
1985 (if (VECTOR_TYPE_P (type)
1986 && TYPE_VECTOR_SUBPARTS (type) == TYPE_VECTOR_SUBPARTS (TREE_TYPE (@1))
1987 && (TYPE_MODE (TREE_TYPE (type))
1988 == TYPE_MODE (TREE_TYPE (TREE_TYPE (@1)))))
1989 (plus @3 (view_convert (vec_cond @0 (negate @1) @2)))))
1992 /* Simplifications of comparisons. */
1994 /* See if we can reduce the magnitude of a constant involved in a
1995 comparison by changing the comparison code. This is a canonicalization
1996 formerly done by maybe_canonicalize_comparison_1. */
2000 (cmp @0 INTEGER_CST@1)
2001 (if (tree_int_cst_sgn (@1) == -1)
2002 (acmp @0 { wide_int_to_tree (TREE_TYPE (@1), wi::add (@1, 1)); }))))
2006 (cmp @0 INTEGER_CST@1)
2007 (if (tree_int_cst_sgn (@1) == 1)
2008 (acmp @0 { wide_int_to_tree (TREE_TYPE (@1), wi::sub (@1, 1)); }))))
2011 /* We can simplify a logical negation of a comparison to the
2012 inverted comparison. As we cannot compute an expression
2013 operator using invert_tree_comparison we have to simulate
2014 that with expression code iteration. */
2015 (for cmp (tcc_comparison)
2016 icmp (inverted_tcc_comparison)
2017 ncmp (inverted_tcc_comparison_with_nans)
2018 /* Ideally we'd like to combine the following two patterns
2019 and handle some more cases by using
2020 (logical_inverted_value (cmp @0 @1))
2021 here but for that genmatch would need to "inline" that.
2022 For now implement what forward_propagate_comparison did. */
2024 (bit_not (cmp @0 @1))
2025 (if (VECTOR_TYPE_P (type)
2026 || (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1))
2027 /* Comparison inversion may be impossible for trapping math,
2028 invert_tree_comparison will tell us. But we can't use
2029 a computed operator in the replacement tree thus we have
2030 to play the trick below. */
2031 (with { enum tree_code ic = invert_tree_comparison
2032 (cmp, HONOR_NANS (@0)); }
2038 (bit_xor (cmp @0 @1) integer_truep)
2039 (with { enum tree_code ic = invert_tree_comparison
2040 (cmp, HONOR_NANS (@0)); }
2046 /* Transform comparisons of the form X - Y CMP 0 to X CMP Y.
2047 ??? The transformation is valid for the other operators if overflow
2048 is undefined for the type, but performing it here badly interacts
2049 with the transformation in fold_cond_expr_with_comparison which
2050 attempts to synthetize ABS_EXPR. */
2053 (cmp (minus@2 @0 @1) integer_zerop)
2054 (if (single_use (@2))
2057 /* Transform comparisons of the form X * C1 CMP 0 to X CMP 0 in the
2058 signed arithmetic case. That form is created by the compiler
2059 often enough for folding it to be of value. One example is in
2060 computing loop trip counts after Operator Strength Reduction. */
2061 (for cmp (simple_comparison)
2062 scmp (swapped_simple_comparison)
2064 (cmp (mult@3 @0 INTEGER_CST@1) integer_zerop@2)
2065 /* Handle unfolded multiplication by zero. */
2066 (if (integer_zerop (@1))
2068 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2069 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2071 /* If @1 is negative we swap the sense of the comparison. */
2072 (if (tree_int_cst_sgn (@1) < 0)
2076 /* Simplify comparison of something with itself. For IEEE
2077 floating-point, we can only do some of these simplifications. */
2081 (if (! FLOAT_TYPE_P (TREE_TYPE (@0))
2082 || ! HONOR_NANS (@0))
2083 { constant_boolean_node (true, type); }
2084 (if (cmp != EQ_EXPR)
2090 || ! FLOAT_TYPE_P (TREE_TYPE (@0))
2091 || ! HONOR_NANS (@0))
2092 { constant_boolean_node (false, type); })))
2093 (for cmp (unle unge uneq)
2096 { constant_boolean_node (true, type); }))
2097 (for cmp (unlt ungt)
2103 (if (!flag_trapping_math)
2104 { constant_boolean_node (false, type); }))
2106 /* Fold ~X op ~Y as Y op X. */
2107 (for cmp (simple_comparison)
2109 (cmp (bit_not@2 @0) (bit_not@3 @1))
2110 (if (single_use (@2) && single_use (@3))
2113 /* Fold ~X op C as X op' ~C, where op' is the swapped comparison. */
2114 (for cmp (simple_comparison)
2115 scmp (swapped_simple_comparison)
2117 (cmp (bit_not@2 @0) CONSTANT_CLASS_P@1)
2118 (if (single_use (@2)
2119 && (TREE_CODE (@1) == INTEGER_CST || TREE_CODE (@1) == VECTOR_CST))
2120 (scmp @0 (bit_not @1)))))
2122 (for cmp (simple_comparison)
2123 /* Fold (double)float1 CMP (double)float2 into float1 CMP float2. */
2125 (cmp (convert@2 @0) (convert? @1))
2126 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
2127 && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@2))
2128 == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0)))
2129 && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@2))
2130 == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1))))
2133 tree type1 = TREE_TYPE (@1);
2134 if (TREE_CODE (@1) == REAL_CST && !DECIMAL_FLOAT_TYPE_P (type1))
2136 REAL_VALUE_TYPE orig = TREE_REAL_CST (@1);
2137 if (TYPE_PRECISION (type1) > TYPE_PRECISION (float_type_node)
2138 && exact_real_truncate (TYPE_MODE (float_type_node), &orig))
2139 type1 = float_type_node;
2140 if (TYPE_PRECISION (type1) > TYPE_PRECISION (double_type_node)
2141 && exact_real_truncate (TYPE_MODE (double_type_node), &orig))
2142 type1 = double_type_node;
2145 = (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type1)
2146 ? TREE_TYPE (@0) : type1);
2148 (if (TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (newtype))
2149 (cmp (convert:newtype @0) (convert:newtype @1))))))
2153 /* IEEE doesn't distinguish +0 and -0 in comparisons. */
2155 /* a CMP (-0) -> a CMP 0 */
2156 (if (REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@1)))
2157 (cmp @0 { build_real (TREE_TYPE (@1), dconst0); }))
2158 /* x != NaN is always true, other ops are always false. */
2159 (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
2160 && ! HONOR_SNANS (@1))
2161 { constant_boolean_node (cmp == NE_EXPR, type); })
2162 /* Fold comparisons against infinity. */
2163 (if (REAL_VALUE_ISINF (TREE_REAL_CST (@1))
2164 && MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (@1))))
2167 REAL_VALUE_TYPE max;
2168 enum tree_code code = cmp;
2169 bool neg = REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1));
2171 code = swap_tree_comparison (code);
2174 /* x > +Inf is always false, if with ignore sNANs. */
2175 (if (code == GT_EXPR
2176 && ! HONOR_SNANS (@0))
2177 { constant_boolean_node (false, type); })
2178 (if (code == LE_EXPR)
2179 /* x <= +Inf is always true, if we don't case about NaNs. */
2180 (if (! HONOR_NANS (@0))
2181 { constant_boolean_node (true, type); }
2182 /* x <= +Inf is the same as x == x, i.e. !isnan(x). */
2184 /* x == +Inf and x >= +Inf are always equal to x > DBL_MAX. */
2185 (if (code == EQ_EXPR || code == GE_EXPR)
2186 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
2188 (lt @0 { build_real (TREE_TYPE (@0), max); })
2189 (gt @0 { build_real (TREE_TYPE (@0), max); }))))
2190 /* x < +Inf is always equal to x <= DBL_MAX. */
2191 (if (code == LT_EXPR)
2192 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
2194 (ge @0 { build_real (TREE_TYPE (@0), max); })
2195 (le @0 { build_real (TREE_TYPE (@0), max); }))))
2196 /* x != +Inf is always equal to !(x > DBL_MAX). */
2197 (if (code == NE_EXPR)
2198 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
2199 (if (! HONOR_NANS (@0))
2201 (ge @0 { build_real (TREE_TYPE (@0), max); })
2202 (le @0 { build_real (TREE_TYPE (@0), max); }))
2204 (bit_xor (lt @0 { build_real (TREE_TYPE (@0), max); })
2205 { build_one_cst (type); })
2206 (bit_xor (gt @0 { build_real (TREE_TYPE (@0), max); })
2207 { build_one_cst (type); }))))))))))
2209 /* If this is a comparison of a real constant with a PLUS_EXPR
2210 or a MINUS_EXPR of a real constant, we can convert it into a
2211 comparison with a revised real constant as long as no overflow
2212 occurs when unsafe_math_optimizations are enabled. */
2213 (if (flag_unsafe_math_optimizations)
2214 (for op (plus minus)
2216 (cmp (op @0 REAL_CST@1) REAL_CST@2)
2219 tree tem = const_binop (op == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR,
2220 TREE_TYPE (@1), @2, @1);
2222 (if (tem && !TREE_OVERFLOW (tem))
2223 (cmp @0 { tem; }))))))
2225 /* Likewise, we can simplify a comparison of a real constant with
2226 a MINUS_EXPR whose first operand is also a real constant, i.e.
2227 (c1 - x) < c2 becomes x > c1-c2. Reordering is allowed on
2228 floating-point types only if -fassociative-math is set. */
2229 (if (flag_associative_math)
2231 (cmp (minus REAL_CST@0 @1) REAL_CST@2)
2232 (with { tree tem = const_binop (MINUS_EXPR, TREE_TYPE (@1), @0, @2); }
2233 (if (tem && !TREE_OVERFLOW (tem))
2234 (cmp { tem; } @1)))))
2236 /* Fold comparisons against built-in math functions. */
2237 (if (flag_unsafe_math_optimizations
2238 && ! flag_errno_math)
2241 (cmp (sq @0) REAL_CST@1)
2243 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
2245 /* sqrt(x) < y is always false, if y is negative. */
2246 (if (cmp == EQ_EXPR || cmp == LT_EXPR || cmp == LE_EXPR)
2247 { constant_boolean_node (false, type); })
2248 /* sqrt(x) > y is always true, if y is negative and we
2249 don't care about NaNs, i.e. negative values of x. */
2250 (if (cmp == NE_EXPR || !HONOR_NANS (@0))
2251 { constant_boolean_node (true, type); })
2252 /* sqrt(x) > y is the same as x >= 0, if y is negative. */
2253 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })))
2254 (if (real_equal (TREE_REAL_CST_PTR (@1), &dconst0))
2256 /* sqrt(x) < 0 is always false. */
2257 (if (cmp == LT_EXPR)
2258 { constant_boolean_node (false, type); })
2259 /* sqrt(x) >= 0 is always true if we don't care about NaNs. */
2260 (if (cmp == GE_EXPR && !HONOR_NANS (@0))
2261 { constant_boolean_node (true, type); })
2262 /* sqrt(x) <= 0 -> x == 0. */
2263 (if (cmp == LE_EXPR)
2265 /* Otherwise sqrt(x) cmp 0 -> x cmp 0. Here cmp can be >=, >,
2266 == or !=. In the last case:
2268 (sqrt(x) != 0) == (NaN != 0) == true == (x != 0)
2270 if x is negative or NaN. Due to -funsafe-math-optimizations,
2271 the results for other x follow from natural arithmetic. */
2273 (if (cmp == GT_EXPR || cmp == GE_EXPR)
2277 real_arithmetic (&c2, MULT_EXPR,
2278 &TREE_REAL_CST (@1), &TREE_REAL_CST (@1));
2279 real_convert (&c2, TYPE_MODE (TREE_TYPE (@0)), &c2);
2281 (if (REAL_VALUE_ISINF (c2))
2282 /* sqrt(x) > y is x == +Inf, when y is very large. */
2283 (if (HONOR_INFINITIES (@0))
2284 (eq @0 { build_real (TREE_TYPE (@0), c2); })
2285 { constant_boolean_node (false, type); })
2286 /* sqrt(x) > c is the same as x > c*c. */
2287 (cmp @0 { build_real (TREE_TYPE (@0), c2); }))))
2288 (if (cmp == LT_EXPR || cmp == LE_EXPR)
2292 real_arithmetic (&c2, MULT_EXPR,
2293 &TREE_REAL_CST (@1), &TREE_REAL_CST (@1));
2294 real_convert (&c2, TYPE_MODE (TREE_TYPE (@0)), &c2);
2296 (if (REAL_VALUE_ISINF (c2))
2298 /* sqrt(x) < y is always true, when y is a very large
2299 value and we don't care about NaNs or Infinities. */
2300 (if (! HONOR_NANS (@0) && ! HONOR_INFINITIES (@0))
2301 { constant_boolean_node (true, type); })
2302 /* sqrt(x) < y is x != +Inf when y is very large and we
2303 don't care about NaNs. */
2304 (if (! HONOR_NANS (@0))
2305 (ne @0 { build_real (TREE_TYPE (@0), c2); }))
2306 /* sqrt(x) < y is x >= 0 when y is very large and we
2307 don't care about Infinities. */
2308 (if (! HONOR_INFINITIES (@0))
2309 (ge @0 { build_real (TREE_TYPE (@0), dconst0); }))
2310 /* sqrt(x) < y is x >= 0 && x != +Inf, when y is large. */
2313 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
2314 (ne @0 { build_real (TREE_TYPE (@0), c2); }))))
2315 /* sqrt(x) < c is the same as x < c*c, if we ignore NaNs. */
2316 (if (! HONOR_NANS (@0))
2317 (cmp @0 { build_real (TREE_TYPE (@0), c2); })
2318 /* sqrt(x) < c is the same as x >= 0 && x < c*c. */
2321 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
2322 (cmp @0 { build_real (TREE_TYPE (@0), c2); }))))))))))))
2324 /* Unordered tests if either argument is a NaN. */
2326 (bit_ior (unordered @0 @0) (unordered @1 @1))
2327 (if (types_match (@0, @1))
2330 (bit_and (ordered @0 @0) (ordered @1 @1))
2331 (if (types_match (@0, @1))
2334 (bit_ior:c (unordered @0 @0) (unordered:c@2 @0 @1))
2337 (bit_and:c (ordered @0 @0) (ordered:c@2 @0 @1))
2340 /* Simple range test simplifications. */
2341 /* A < B || A >= B -> true. */
2342 (for test1 (lt le le le ne ge)
2343 test2 (ge gt ge ne eq ne)
2345 (bit_ior:c (test1 @0 @1) (test2 @0 @1))
2346 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2347 || VECTOR_INTEGER_TYPE_P (TREE_TYPE (@0)))
2348 { constant_boolean_node (true, type); })))
2349 /* A < B && A >= B -> false. */
2350 (for test1 (lt lt lt le ne eq)
2351 test2 (ge gt eq gt eq gt)
2353 (bit_and:c (test1 @0 @1) (test2 @0 @1))
2354 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2355 || VECTOR_INTEGER_TYPE_P (TREE_TYPE (@0)))
2356 { constant_boolean_node (false, type); })))
2358 /* -A CMP -B -> B CMP A. */
2359 (for cmp (tcc_comparison)
2360 scmp (swapped_tcc_comparison)
2362 (cmp (negate @0) (negate @1))
2363 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
2364 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2365 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
2368 (cmp (negate @0) CONSTANT_CLASS_P@1)
2369 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
2370 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2371 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
2372 (with { tree tem = const_unop (NEGATE_EXPR, TREE_TYPE (@0), @1); }
2373 (if (tem && !TREE_OVERFLOW (tem))
2374 (scmp @0 { tem; }))))))
2376 /* Convert ABS_EXPR<x> == 0 or ABS_EXPR<x> != 0 to x == 0 or x != 0. */
2379 (op (abs @0) zerop@1)
2382 /* From fold_sign_changed_comparison and fold_widened_comparison. */
2383 (for cmp (simple_comparison)
2385 (cmp (convert@0 @00) (convert?@1 @10))
2386 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2387 /* Disable this optimization if we're casting a function pointer
2388 type on targets that require function pointer canonicalization. */
2389 && !(targetm.have_canonicalize_funcptr_for_compare ()
2390 && TREE_CODE (TREE_TYPE (@00)) == POINTER_TYPE
2391 && TREE_CODE (TREE_TYPE (TREE_TYPE (@00))) == FUNCTION_TYPE)
2393 (if (TYPE_PRECISION (TREE_TYPE (@00)) == TYPE_PRECISION (TREE_TYPE (@0))
2394 && (TREE_CODE (@10) == INTEGER_CST
2395 || (@1 != @10 && types_match (TREE_TYPE (@10), TREE_TYPE (@00))))
2396 && (TYPE_UNSIGNED (TREE_TYPE (@00)) == TYPE_UNSIGNED (TREE_TYPE (@0))
2399 && (POINTER_TYPE_P (TREE_TYPE (@00)) == POINTER_TYPE_P (TREE_TYPE (@0))))
2400 /* ??? The special-casing of INTEGER_CST conversion was in the original
2401 code and here to avoid a spurious overflow flag on the resulting
2402 constant which fold_convert produces. */
2403 (if (TREE_CODE (@1) == INTEGER_CST)
2404 (cmp @00 { force_fit_type (TREE_TYPE (@00), wi::to_widest (@1), 0,
2405 TREE_OVERFLOW (@1)); })
2406 (cmp @00 (convert @1)))
2408 (if (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (TREE_TYPE (@00)))
2409 /* If possible, express the comparison in the shorter mode. */
2410 (if ((cmp == EQ_EXPR || cmp == NE_EXPR
2411 || TYPE_UNSIGNED (TREE_TYPE (@0)) == TYPE_UNSIGNED (TREE_TYPE (@00))
2412 || (!TYPE_UNSIGNED (TREE_TYPE (@0))
2413 && TYPE_UNSIGNED (TREE_TYPE (@00))))
2414 && (types_match (TREE_TYPE (@10), TREE_TYPE (@00))
2415 || ((TYPE_PRECISION (TREE_TYPE (@00))
2416 >= TYPE_PRECISION (TREE_TYPE (@10)))
2417 && (TYPE_UNSIGNED (TREE_TYPE (@00))
2418 == TYPE_UNSIGNED (TREE_TYPE (@10))))
2419 || (TREE_CODE (@10) == INTEGER_CST
2420 && INTEGRAL_TYPE_P (TREE_TYPE (@00))
2421 && int_fits_type_p (@10, TREE_TYPE (@00)))))
2422 (cmp @00 (convert @10))
2423 (if (TREE_CODE (@10) == INTEGER_CST
2424 && INTEGRAL_TYPE_P (TREE_TYPE (@00))
2425 && !int_fits_type_p (@10, TREE_TYPE (@00)))
2428 tree min = lower_bound_in_type (TREE_TYPE (@10), TREE_TYPE (@00));
2429 tree max = upper_bound_in_type (TREE_TYPE (@10), TREE_TYPE (@00));
2430 bool above = integer_nonzerop (const_binop (LT_EXPR, type, max, @10));
2431 bool below = integer_nonzerop (const_binop (LT_EXPR, type, @10, min));
2433 (if (above || below)
2434 (if (cmp == EQ_EXPR || cmp == NE_EXPR)
2435 { constant_boolean_node (cmp == EQ_EXPR ? false : true, type); }
2436 (if (cmp == LT_EXPR || cmp == LE_EXPR)
2437 { constant_boolean_node (above ? true : false, type); }
2438 (if (cmp == GT_EXPR || cmp == GE_EXPR)
2439 { constant_boolean_node (above ? false : true, type); }))))))))))))
2442 /* A local variable can never be pointed to by
2443 the default SSA name of an incoming parameter.
2444 SSA names are canonicalized to 2nd place. */
2446 (cmp addr@0 SSA_NAME@1)
2447 (if (SSA_NAME_IS_DEFAULT_DEF (@1)
2448 && TREE_CODE (SSA_NAME_VAR (@1)) == PARM_DECL)
2449 (with { tree base = get_base_address (TREE_OPERAND (@0, 0)); }
2450 (if (TREE_CODE (base) == VAR_DECL
2451 && auto_var_in_fn_p (base, current_function_decl))
2452 (if (cmp == NE_EXPR)
2453 { constant_boolean_node (true, type); }
2454 { constant_boolean_node (false, type); }))))))
2456 /* Equality compare simplifications from fold_binary */
2459 /* If we have (A | C) == D where C & ~D != 0, convert this into 0.
2460 Similarly for NE_EXPR. */
2462 (cmp (convert?@3 (bit_ior @0 INTEGER_CST@1)) INTEGER_CST@2)
2463 (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0))
2464 && wi::bit_and_not (@1, @2) != 0)
2465 { constant_boolean_node (cmp == NE_EXPR, type); }))
2467 /* (X ^ Y) == 0 becomes X == Y, and (X ^ Y) != 0 becomes X != Y. */
2469 (cmp (bit_xor @0 @1) integer_zerop)
2472 /* (X ^ Y) == Y becomes X == 0.
2473 Likewise (X ^ Y) == X becomes Y == 0. */
2475 (cmp:c (bit_xor:c @0 @1) @0)
2476 (cmp @1 { build_zero_cst (TREE_TYPE (@1)); }))
2478 /* (X ^ C1) op C2 can be rewritten as X op (C1 ^ C2). */
2480 (cmp (convert?@3 (bit_xor @0 INTEGER_CST@1)) INTEGER_CST@2)
2481 (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0)))
2482 (cmp @0 (bit_xor @1 (convert @2)))))
2485 (cmp (convert? addr@0) integer_zerop)
2486 (if (tree_single_nonzero_warnv_p (@0, NULL))
2487 { constant_boolean_node (cmp == NE_EXPR, type); })))
2489 /* If we have (A & C) == C where C is a power of 2, convert this into
2490 (A & C) != 0. Similarly for NE_EXPR. */
2494 (cmp (bit_and@2 @0 integer_pow2p@1) @1)
2495 (icmp @2 { build_zero_cst (TREE_TYPE (@0)); })))
2497 /* If we have (A & C) != 0 where C is the sign bit of A, convert
2498 this into A < 0. Similarly for (A & C) == 0 into A >= 0. */
2502 (cmp (bit_and (convert?@2 @0) integer_pow2p@1) integer_zerop)
2503 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2504 && (TYPE_PRECISION (TREE_TYPE (@0))
2505 == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (@0))))
2506 && element_precision (@2) >= element_precision (@0)
2507 && wi::only_sign_bit_p (@1, element_precision (@0)))
2508 (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
2509 (ncmp (convert:stype @0) { build_zero_cst (stype); })))))
2511 /* When the addresses are not directly of decls compare base and offset.
2512 This implements some remaining parts of fold_comparison address
2513 comparisons but still no complete part of it. Still it is good
2514 enough to make fold_stmt not regress when not dispatching to fold_binary. */
2515 (for cmp (simple_comparison)
2517 (cmp (convert1?@2 addr@0) (convert2? addr@1))
2520 HOST_WIDE_INT off0, off1;
2521 tree base0 = get_addr_base_and_unit_offset (TREE_OPERAND (@0, 0), &off0);
2522 tree base1 = get_addr_base_and_unit_offset (TREE_OPERAND (@1, 0), &off1);
2523 if (base0 && TREE_CODE (base0) == MEM_REF)
2525 off0 += mem_ref_offset (base0).to_short_addr ();
2526 base0 = TREE_OPERAND (base0, 0);
2528 if (base1 && TREE_CODE (base1) == MEM_REF)
2530 off1 += mem_ref_offset (base1).to_short_addr ();
2531 base1 = TREE_OPERAND (base1, 0);
2534 (if (base0 && base1)
2538 if (decl_in_symtab_p (base0)
2539 && decl_in_symtab_p (base1))
2540 equal = symtab_node::get_create (base0)
2541 ->equal_address_to (symtab_node::get_create (base1));
2542 else if ((DECL_P (base0)
2543 || TREE_CODE (base0) == SSA_NAME
2544 || TREE_CODE (base0) == STRING_CST)
2546 || TREE_CODE (base1) == SSA_NAME
2547 || TREE_CODE (base1) == STRING_CST))
2548 equal = (base0 == base1);
2551 && (cmp == EQ_EXPR || cmp == NE_EXPR
2552 /* If the offsets are equal we can ignore overflow. */
2554 || POINTER_TYPE_OVERFLOW_UNDEFINED
2555 /* Or if we compare using pointers to decls or strings. */
2556 || (POINTER_TYPE_P (TREE_TYPE (@2))
2557 && (DECL_P (base0) || TREE_CODE (base0) == STRING_CST))))
2559 (if (cmp == EQ_EXPR)
2560 { constant_boolean_node (off0 == off1, type); })
2561 (if (cmp == NE_EXPR)
2562 { constant_boolean_node (off0 != off1, type); })
2563 (if (cmp == LT_EXPR)
2564 { constant_boolean_node (off0 < off1, type); })
2565 (if (cmp == LE_EXPR)
2566 { constant_boolean_node (off0 <= off1, type); })
2567 (if (cmp == GE_EXPR)
2568 { constant_boolean_node (off0 >= off1, type); })
2569 (if (cmp == GT_EXPR)
2570 { constant_boolean_node (off0 > off1, type); }))
2572 && DECL_P (base0) && DECL_P (base1)
2573 /* If we compare this as integers require equal offset. */
2574 && (!INTEGRAL_TYPE_P (TREE_TYPE (@2))
2577 (if (cmp == EQ_EXPR)
2578 { constant_boolean_node (false, type); })
2579 (if (cmp == NE_EXPR)
2580 { constant_boolean_node (true, type); })))))))))
2582 /* Simplify pointer equality compares using PTA. */
2586 (if (POINTER_TYPE_P (TREE_TYPE (@0))
2587 && ptrs_compare_unequal (@0, @1))
2588 { neeq == EQ_EXPR ? boolean_false_node : boolean_true_node; })))
2590 /* PR70920: Transform (intptr_t)x eq/ne CST to x eq/ne (typeof x) CST.
2591 and (typeof ptr_cst) x eq/ne ptr_cst to x eq/ne (typeof x) CST.
2592 Disable the transform if either operand is pointer to function.
2593 This broke pr22051-2.c for arm where function pointer
2594 canonicalizaion is not wanted. */
2598 (cmp (convert @0) INTEGER_CST@1)
2599 (if ((POINTER_TYPE_P (TREE_TYPE (@0)) && !FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@0)))
2600 && INTEGRAL_TYPE_P (TREE_TYPE (@1)))
2601 || (INTEGRAL_TYPE_P (TREE_TYPE (@0)) && POINTER_TYPE_P (TREE_TYPE (@1))
2602 && !FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@1)))))
2603 (cmp @0 (convert @1)))))
2605 /* Non-equality compare simplifications from fold_binary */
2606 (for cmp (lt gt le ge)
2607 /* Comparisons with the highest or lowest possible integer of
2608 the specified precision will have known values. */
2610 (cmp (convert?@2 @0) INTEGER_CST@1)
2611 (if ((INTEGRAL_TYPE_P (TREE_TYPE (@1)) || POINTER_TYPE_P (TREE_TYPE (@1)))
2612 && tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@0)))
2615 tree arg1_type = TREE_TYPE (@1);
2616 unsigned int prec = TYPE_PRECISION (arg1_type);
2617 wide_int max = wi::max_value (arg1_type);
2618 wide_int signed_max = wi::max_value (prec, SIGNED);
2619 wide_int min = wi::min_value (arg1_type);
2622 (if (wi::eq_p (@1, max))
2624 (if (cmp == GT_EXPR)
2625 { constant_boolean_node (false, type); })
2626 (if (cmp == GE_EXPR)
2628 (if (cmp == LE_EXPR)
2629 { constant_boolean_node (true, type); })
2630 (if (cmp == LT_EXPR)
2632 (if (wi::eq_p (@1, min))
2634 (if (cmp == LT_EXPR)
2635 { constant_boolean_node (false, type); })
2636 (if (cmp == LE_EXPR)
2638 (if (cmp == GE_EXPR)
2639 { constant_boolean_node (true, type); })
2640 (if (cmp == GT_EXPR)
2642 (if (wi::eq_p (@1, max - 1))
2644 (if (cmp == GT_EXPR)
2645 (eq @2 { wide_int_to_tree (TREE_TYPE (@1), wi::add (@1, 1)); }))
2646 (if (cmp == LE_EXPR)
2647 (ne @2 { wide_int_to_tree (TREE_TYPE (@1), wi::add (@1, 1)); }))))
2648 (if (wi::eq_p (@1, min + 1))
2650 (if (cmp == GE_EXPR)
2651 (ne @2 { wide_int_to_tree (TREE_TYPE (@1), wi::sub (@1, 1)); }))
2652 (if (cmp == LT_EXPR)
2653 (eq @2 { wide_int_to_tree (TREE_TYPE (@1), wi::sub (@1, 1)); }))))
2654 (if (wi::eq_p (@1, signed_max)
2655 && TYPE_UNSIGNED (arg1_type)
2656 /* We will flip the signedness of the comparison operator
2657 associated with the mode of @1, so the sign bit is
2658 specified by this mode. Check that @1 is the signed
2659 max associated with this sign bit. */
2660 && prec == GET_MODE_PRECISION (TYPE_MODE (arg1_type))
2661 /* signed_type does not work on pointer types. */
2662 && INTEGRAL_TYPE_P (arg1_type))
2663 /* The following case also applies to X < signed_max+1
2664 and X >= signed_max+1 because previous transformations. */
2665 (if (cmp == LE_EXPR || cmp == GT_EXPR)
2666 (with { tree st = signed_type_for (arg1_type); }
2667 (if (cmp == LE_EXPR)
2668 (ge (convert:st @0) { build_zero_cst (st); })
2669 (lt (convert:st @0) { build_zero_cst (st); }))))))))))
2671 (for cmp (unordered ordered unlt unle ungt unge uneq ltgt)
2672 /* If the second operand is NaN, the result is constant. */
2675 (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
2676 && (cmp != LTGT_EXPR || ! flag_trapping_math))
2677 { constant_boolean_node (cmp == ORDERED_EXPR || cmp == LTGT_EXPR
2678 ? false : true, type); })))
2680 /* bool_var != 0 becomes bool_var. */
2682 (ne @0 integer_zerop)
2683 (if (TREE_CODE (TREE_TYPE (@0)) == BOOLEAN_TYPE
2684 && types_match (type, TREE_TYPE (@0)))
2686 /* bool_var == 1 becomes bool_var. */
2688 (eq @0 integer_onep)
2689 (if (TREE_CODE (TREE_TYPE (@0)) == BOOLEAN_TYPE
2690 && types_match (type, TREE_TYPE (@0)))
2693 bool_var == 0 becomes !bool_var or
2694 bool_var != 1 becomes !bool_var
2695 here because that only is good in assignment context as long
2696 as we require a tcc_comparison in GIMPLE_CONDs where we'd
2697 replace if (x == 0) with tem = ~x; if (tem != 0) which is
2698 clearly less optimal and which we'll transform again in forwprop. */
2700 /* When one argument is a constant, overflow detection can be simplified.
2701 Currently restricted to single use so as not to interfere too much with
2702 ADD_OVERFLOW detection in tree-ssa-math-opts.c.
2703 A + CST CMP A -> A CMP' CST' */
2704 (for cmp (lt le ge gt)
2707 (cmp:c (plus@2 @0 INTEGER_CST@1) @0)
2708 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
2709 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))
2712 (out @0 { wide_int_to_tree (TREE_TYPE (@0), wi::max_value
2713 (TYPE_PRECISION (TREE_TYPE (@0)), UNSIGNED) - @1); }))))
2715 /* To detect overflow in unsigned A - B, A < B is simpler than A - B > A.
2716 However, the detection logic for SUB_OVERFLOW in tree-ssa-math-opts.c
2717 expects the long form, so we restrict the transformation for now. */
2720 (cmp:c (minus@2 @0 @1) @0)
2721 (if (single_use (@2)
2722 && ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2723 && TYPE_UNSIGNED (TREE_TYPE (@0))
2724 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
2727 /* Testing for overflow is unnecessary if we already know the result. */
2732 (cmp:c (realpart (IFN_SUB_OVERFLOW@2 @0 @1)) @0)
2733 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
2734 && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
2735 (out (imagpart @2) { build_zero_cst (TREE_TYPE (@0)); }))))
2740 (cmp:c (realpart (IFN_ADD_OVERFLOW:c@2 @0 @1)) @0)
2741 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
2742 && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
2743 (out (imagpart @2) { build_zero_cst (TREE_TYPE (@0)); }))))
2745 /* For unsigned operands, -1 / B < A checks whether A * B would overflow.
2746 Simplify it to __builtin_mul_overflow (A, B, <unused>). */
2750 (cmp:c (trunc_div:s integer_all_onesp @1) @0)
2751 (if (TYPE_UNSIGNED (TREE_TYPE (@0)) && !VECTOR_TYPE_P (TREE_TYPE (@0)))
2752 (with { tree t = TREE_TYPE (@0), cpx = build_complex_type (t); }
2753 (out (imagpart (IFN_MUL_OVERFLOW:cpx @0 @1)) { build_zero_cst (t); })))))
2755 /* Simplification of math builtins. These rules must all be optimizations
2756 as well as IL simplifications. If there is a possibility that the new
2757 form could be a pessimization, the rule should go in the canonicalization
2758 section that follows this one.
2760 Rules can generally go in this section if they satisfy one of
2763 - the rule describes an identity
2765 - the rule replaces calls with something as simple as addition or
2768 - the rule contains unary calls only and simplifies the surrounding
2769 arithmetic. (The idea here is to exclude non-unary calls in which
2770 one operand is constant and in which the call is known to be cheap
2771 when the operand has that value.) */
2773 (if (flag_unsafe_math_optimizations)
2774 /* Simplify sqrt(x) * sqrt(x) -> x. */
2776 (mult (SQRT@1 @0) @1)
2777 (if (!HONOR_SNANS (type))
2780 /* Simplify sqrt(x) * sqrt(y) -> sqrt(x*y). */
2781 (for root (SQRT CBRT)
2783 (mult (root:s @0) (root:s @1))
2784 (root (mult @0 @1))))
2786 /* Simplify expN(x) * expN(y) -> expN(x+y). */
2787 (for exps (EXP EXP2 EXP10 POW10)
2789 (mult (exps:s @0) (exps:s @1))
2790 (exps (plus @0 @1))))
2792 /* Simplify a/root(b/c) into a*root(c/b). */
2793 (for root (SQRT CBRT)
2795 (rdiv @0 (root:s (rdiv:s @1 @2)))
2796 (mult @0 (root (rdiv @2 @1)))))
2798 /* Simplify x/expN(y) into x*expN(-y). */
2799 (for exps (EXP EXP2 EXP10 POW10)
2801 (rdiv @0 (exps:s @1))
2802 (mult @0 (exps (negate @1)))))
2804 (for logs (LOG LOG2 LOG10 LOG10)
2805 exps (EXP EXP2 EXP10 POW10)
2806 /* logN(expN(x)) -> x. */
2810 /* expN(logN(x)) -> x. */
2815 /* Optimize logN(func()) for various exponential functions. We
2816 want to determine the value "x" and the power "exponent" in
2817 order to transform logN(x**exponent) into exponent*logN(x). */
2818 (for logs (LOG LOG LOG LOG2 LOG2 LOG2 LOG10 LOG10)
2819 exps (EXP2 EXP10 POW10 EXP EXP10 POW10 EXP EXP2)
2822 (if (SCALAR_FLOAT_TYPE_P (type))
2828 /* Prepare to do logN(exp(exponent)) -> exponent*logN(e). */
2829 x = build_real_truncate (type, dconst_e ());
2832 /* Prepare to do logN(exp2(exponent)) -> exponent*logN(2). */
2833 x = build_real (type, dconst2);
2837 /* Prepare to do logN(exp10(exponent)) -> exponent*logN(10). */
2839 REAL_VALUE_TYPE dconst10;
2840 real_from_integer (&dconst10, VOIDmode, 10, SIGNED);
2841 x = build_real (type, dconst10);
2848 (mult (logs { x; }) @0)))))
2856 (if (SCALAR_FLOAT_TYPE_P (type))
2862 /* Prepare to do logN(sqrt(x)) -> 0.5*logN(x). */
2863 x = build_real (type, dconsthalf);
2866 /* Prepare to do logN(cbrt(x)) -> (1/3)*logN(x). */
2867 x = build_real_truncate (type, dconst_third ());
2873 (mult { x; } (logs @0))))))
2875 /* logN(pow(x,exponent)) -> exponent*logN(x). */
2876 (for logs (LOG LOG2 LOG10)
2880 (mult @1 (logs @0))))
2885 exps (EXP EXP2 EXP10 POW10)
2886 /* sqrt(expN(x)) -> expN(x*0.5). */
2889 (exps (mult @0 { build_real (type, dconsthalf); })))
2890 /* cbrt(expN(x)) -> expN(x/3). */
2893 (exps (mult @0 { build_real_truncate (type, dconst_third ()); })))
2894 /* pow(expN(x), y) -> expN(x*y). */
2897 (exps (mult @0 @1))))
2899 /* tan(atan(x)) -> x. */
2906 /* cabs(x+0i) or cabs(0+xi) -> abs(x). */
2908 (CABS (complex:C @0 real_zerop@1))
2911 /* trunc(trunc(x)) -> trunc(x), etc. */
2912 (for fns (TRUNC FLOOR CEIL ROUND NEARBYINT RINT)
2916 /* f(x) -> x if x is integer valued and f does nothing for such values. */
2917 (for fns (TRUNC FLOOR CEIL ROUND NEARBYINT RINT)
2919 (fns integer_valued_real_p@0)
2922 /* hypot(x,0) and hypot(0,x) -> abs(x). */
2924 (HYPOT:c @0 real_zerop@1)
2927 /* pow(1,x) -> 1. */
2929 (POW real_onep@0 @1)
2933 /* copysign(x,x) -> x. */
2938 /* copysign(x,y) -> fabs(x) if y is nonnegative. */
2939 (COPYSIGN @0 tree_expr_nonnegative_p@1)
2942 (for scale (LDEXP SCALBN SCALBLN)
2943 /* ldexp(0, x) -> 0. */
2945 (scale real_zerop@0 @1)
2947 /* ldexp(x, 0) -> x. */
2949 (scale @0 integer_zerop@1)
2951 /* ldexp(x, y) -> x if x is +-Inf or NaN. */
2953 (scale REAL_CST@0 @1)
2954 (if (!real_isfinite (TREE_REAL_CST_PTR (@0)))
2957 /* Canonicalization of sequences of math builtins. These rules represent
2958 IL simplifications but are not necessarily optimizations.
2960 The sincos pass is responsible for picking "optimal" implementations
2961 of math builtins, which may be more complicated and can sometimes go
2962 the other way, e.g. converting pow into a sequence of sqrts.
2963 We only want to do these canonicalizations before the pass has run. */
2965 (if (flag_unsafe_math_optimizations && canonicalize_math_p ())
2966 /* Simplify tan(x) * cos(x) -> sin(x). */
2968 (mult:c (TAN:s @0) (COS:s @0))
2971 /* Simplify x * pow(x,c) -> pow(x,c+1). */
2973 (mult:c @0 (POW:s @0 REAL_CST@1))
2974 (if (!TREE_OVERFLOW (@1))
2975 (POW @0 (plus @1 { build_one_cst (type); }))))
2977 /* Simplify sin(x) / cos(x) -> tan(x). */
2979 (rdiv (SIN:s @0) (COS:s @0))
2982 /* Simplify cos(x) / sin(x) -> 1 / tan(x). */
2984 (rdiv (COS:s @0) (SIN:s @0))
2985 (rdiv { build_one_cst (type); } (TAN @0)))
2987 /* Simplify sin(x) / tan(x) -> cos(x). */
2989 (rdiv (SIN:s @0) (TAN:s @0))
2990 (if (! HONOR_NANS (@0)
2991 && ! HONOR_INFINITIES (@0))
2994 /* Simplify tan(x) / sin(x) -> 1.0 / cos(x). */
2996 (rdiv (TAN:s @0) (SIN:s @0))
2997 (if (! HONOR_NANS (@0)
2998 && ! HONOR_INFINITIES (@0))
2999 (rdiv { build_one_cst (type); } (COS @0))))
3001 /* Simplify pow(x,y) * pow(x,z) -> pow(x,y+z). */
3003 (mult (POW:s @0 @1) (POW:s @0 @2))
3004 (POW @0 (plus @1 @2)))
3006 /* Simplify pow(x,y) * pow(z,y) -> pow(x*z,y). */
3008 (mult (POW:s @0 @1) (POW:s @2 @1))
3009 (POW (mult @0 @2) @1))
3011 /* Simplify powi(x,y) * powi(z,y) -> powi(x*z,y). */
3013 (mult (POWI:s @0 @1) (POWI:s @2 @1))
3014 (POWI (mult @0 @2) @1))
3016 /* Simplify pow(x,c) / x -> pow(x,c-1). */
3018 (rdiv (POW:s @0 REAL_CST@1) @0)
3019 (if (!TREE_OVERFLOW (@1))
3020 (POW @0 (minus @1 { build_one_cst (type); }))))
3022 /* Simplify x / pow (y,z) -> x * pow(y,-z). */
3024 (rdiv @0 (POW:s @1 @2))
3025 (mult @0 (POW @1 (negate @2))))
3030 /* sqrt(sqrt(x)) -> pow(x,1/4). */
3033 (pows @0 { build_real (type, dconst_quarter ()); }))
3034 /* sqrt(cbrt(x)) -> pow(x,1/6). */
3037 (pows @0 { build_real_truncate (type, dconst_sixth ()); }))
3038 /* cbrt(sqrt(x)) -> pow(x,1/6). */
3041 (pows @0 { build_real_truncate (type, dconst_sixth ()); }))
3042 /* cbrt(cbrt(x)) -> pow(x,1/9), iff x is nonnegative. */
3044 (cbrts (cbrts tree_expr_nonnegative_p@0))
3045 (pows @0 { build_real_truncate (type, dconst_ninth ()); }))
3046 /* sqrt(pow(x,y)) -> pow(|x|,y*0.5). */
3048 (sqrts (pows @0 @1))
3049 (pows (abs @0) (mult @1 { build_real (type, dconsthalf); })))
3050 /* cbrt(pow(x,y)) -> pow(x,y/3), iff x is nonnegative. */
3052 (cbrts (pows tree_expr_nonnegative_p@0 @1))
3053 (pows @0 (mult @1 { build_real_truncate (type, dconst_third ()); })))
3054 /* pow(sqrt(x),y) -> pow(x,y*0.5). */
3056 (pows (sqrts @0) @1)
3057 (pows @0 (mult @1 { build_real (type, dconsthalf); })))
3058 /* pow(cbrt(x),y) -> pow(x,y/3) iff x is nonnegative. */
3060 (pows (cbrts tree_expr_nonnegative_p@0) @1)
3061 (pows @0 (mult @1 { build_real_truncate (type, dconst_third ()); })))
3062 /* pow(pow(x,y),z) -> pow(x,y*z) iff x is nonnegative. */
3064 (pows (pows tree_expr_nonnegative_p@0 @1) @2)
3065 (pows @0 (mult @1 @2))))
3067 /* cabs(x+xi) -> fabs(x)*sqrt(2). */
3069 (CABS (complex @0 @0))
3070 (mult (abs @0) { build_real_truncate (type, dconst_sqrt2 ()); }))
3072 /* hypot(x,x) -> fabs(x)*sqrt(2). */
3075 (mult (abs @0) { build_real_truncate (type, dconst_sqrt2 ()); }))
3077 /* cexp(x+yi) -> exp(x)*cexpi(y). */
3082 (cexps compositional_complex@0)
3083 (if (targetm.libc_has_function (function_c99_math_complex))
3085 (mult (exps@1 (realpart @0)) (realpart (cexpis:type@2 (imagpart @0))))
3086 (mult @1 (imagpart @2)))))))
3088 (if (canonicalize_math_p ())
3089 /* floor(x) -> trunc(x) if x is nonnegative. */
3093 (floors tree_expr_nonnegative_p@0)
3096 (match double_value_p
3098 (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == double_type_node)))
3099 (for froms (BUILT_IN_TRUNCL
3111 /* truncl(extend(x)) -> extend(trunc(x)), etc., if x is a double. */
3112 (if (optimize && canonicalize_math_p ())
3114 (froms (convert double_value_p@0))
3115 (convert (tos @0)))))
3117 (match float_value_p
3119 (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == float_type_node)))
3120 (for froms (BUILT_IN_TRUNCL BUILT_IN_TRUNC
3121 BUILT_IN_FLOORL BUILT_IN_FLOOR
3122 BUILT_IN_CEILL BUILT_IN_CEIL
3123 BUILT_IN_ROUNDL BUILT_IN_ROUND
3124 BUILT_IN_NEARBYINTL BUILT_IN_NEARBYINT
3125 BUILT_IN_RINTL BUILT_IN_RINT)
3126 tos (BUILT_IN_TRUNCF BUILT_IN_TRUNCF
3127 BUILT_IN_FLOORF BUILT_IN_FLOORF
3128 BUILT_IN_CEILF BUILT_IN_CEILF
3129 BUILT_IN_ROUNDF BUILT_IN_ROUNDF
3130 BUILT_IN_NEARBYINTF BUILT_IN_NEARBYINTF
3131 BUILT_IN_RINTF BUILT_IN_RINTF)
3132 /* truncl(extend(x)) and trunc(extend(x)) -> extend(truncf(x)), etc.,
3134 (if (optimize && canonicalize_math_p ()
3135 && targetm.libc_has_function (function_c99_misc))
3137 (froms (convert float_value_p@0))
3138 (convert (tos @0)))))
3140 (for froms (XFLOORL XCEILL XROUNDL XRINTL)
3141 tos (XFLOOR XCEIL XROUND XRINT)
3142 /* llfloorl(extend(x)) -> llfloor(x), etc., if x is a double. */
3143 (if (optimize && canonicalize_math_p ())
3145 (froms (convert double_value_p@0))
3148 (for froms (XFLOORL XCEILL XROUNDL XRINTL
3149 XFLOOR XCEIL XROUND XRINT)
3150 tos (XFLOORF XCEILF XROUNDF XRINTF)
3151 /* llfloorl(extend(x)) and llfloor(extend(x)) -> llfloorf(x), etc.,
3153 (if (optimize && canonicalize_math_p ())
3155 (froms (convert float_value_p@0))
3158 (if (canonicalize_math_p ())
3159 /* xfloor(x) -> fix_trunc(x) if x is nonnegative. */
3160 (for floors (IFLOOR LFLOOR LLFLOOR)
3162 (floors tree_expr_nonnegative_p@0)
3165 (if (canonicalize_math_p ())
3166 /* xfloor(x) -> fix_trunc(x), etc., if x is integer valued. */
3167 (for fns (IFLOOR LFLOOR LLFLOOR
3169 IROUND LROUND LLROUND)
3171 (fns integer_valued_real_p@0)
3173 (if (!flag_errno_math)
3174 /* xrint(x) -> fix_trunc(x), etc., if x is integer valued. */
3175 (for rints (IRINT LRINT LLRINT)
3177 (rints integer_valued_real_p@0)
3180 (if (canonicalize_math_p ())
3181 (for ifn (IFLOOR ICEIL IROUND IRINT)
3182 lfn (LFLOOR LCEIL LROUND LRINT)
3183 llfn (LLFLOOR LLCEIL LLROUND LLRINT)
3184 /* Canonicalize iround (x) to lround (x) on ILP32 targets where
3185 sizeof (int) == sizeof (long). */
3186 (if (TYPE_PRECISION (integer_type_node)
3187 == TYPE_PRECISION (long_integer_type_node))
3190 (lfn:long_integer_type_node @0)))
3191 /* Canonicalize llround (x) to lround (x) on LP64 targets where
3192 sizeof (long long) == sizeof (long). */
3193 (if (TYPE_PRECISION (long_long_integer_type_node)
3194 == TYPE_PRECISION (long_integer_type_node))
3197 (lfn:long_integer_type_node @0)))))
3199 /* cproj(x) -> x if we're ignoring infinities. */
3202 (if (!HONOR_INFINITIES (type))
3205 /* If the real part is inf and the imag part is known to be
3206 nonnegative, return (inf + 0i). */
3208 (CPROJ (complex REAL_CST@0 tree_expr_nonnegative_p@1))
3209 (if (real_isinf (TREE_REAL_CST_PTR (@0)))
3210 { build_complex_inf (type, false); }))
3212 /* If the imag part is inf, return (inf+I*copysign(0,imag)). */
3214 (CPROJ (complex @0 REAL_CST@1))
3215 (if (real_isinf (TREE_REAL_CST_PTR (@1)))
3216 { build_complex_inf (type, TREE_REAL_CST_PTR (@1)->sign); }))
3222 (pows @0 REAL_CST@1)
3224 const REAL_VALUE_TYPE *value = TREE_REAL_CST_PTR (@1);
3225 REAL_VALUE_TYPE tmp;
3228 /* pow(x,0) -> 1. */
3229 (if (real_equal (value, &dconst0))
3230 { build_real (type, dconst1); })
3231 /* pow(x,1) -> x. */
3232 (if (real_equal (value, &dconst1))
3234 /* pow(x,-1) -> 1/x. */
3235 (if (real_equal (value, &dconstm1))
3236 (rdiv { build_real (type, dconst1); } @0))
3237 /* pow(x,0.5) -> sqrt(x). */
3238 (if (flag_unsafe_math_optimizations
3239 && canonicalize_math_p ()
3240 && real_equal (value, &dconsthalf))
3242 /* pow(x,1/3) -> cbrt(x). */
3243 (if (flag_unsafe_math_optimizations
3244 && canonicalize_math_p ()
3245 && (tmp = real_value_truncate (TYPE_MODE (type), dconst_third ()),
3246 real_equal (value, &tmp)))
3249 /* powi(1,x) -> 1. */
3251 (POWI real_onep@0 @1)
3255 (POWI @0 INTEGER_CST@1)
3257 /* powi(x,0) -> 1. */
3258 (if (wi::eq_p (@1, 0))
3259 { build_real (type, dconst1); })
3260 /* powi(x,1) -> x. */
3261 (if (wi::eq_p (@1, 1))
3263 /* powi(x,-1) -> 1/x. */
3264 (if (wi::eq_p (@1, -1))
3265 (rdiv { build_real (type, dconst1); } @0))))
3267 /* Narrowing of arithmetic and logical operations.
3269 These are conceptually similar to the transformations performed for
3270 the C/C++ front-ends by shorten_binary_op and shorten_compare. Long
3271 term we want to move all that code out of the front-ends into here. */
3273 /* If we have a narrowing conversion of an arithmetic operation where
3274 both operands are widening conversions from the same type as the outer
3275 narrowing conversion. Then convert the innermost operands to a suitable
3276 unsigned type (to avoid introducing undefined behavior), perform the
3277 operation and convert the result to the desired type. */
3278 (for op (plus minus)
3280 (convert (op:s (convert@2 @0) (convert?@3 @1)))
3281 (if (INTEGRAL_TYPE_P (type)
3282 /* We check for type compatibility between @0 and @1 below,
3283 so there's no need to check that @1/@3 are integral types. */
3284 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
3285 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
3286 /* The precision of the type of each operand must match the
3287 precision of the mode of each operand, similarly for the
3289 && (TYPE_PRECISION (TREE_TYPE (@0))
3290 == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (@0))))
3291 && (TYPE_PRECISION (TREE_TYPE (@1))
3292 == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (@1))))
3293 && TYPE_PRECISION (type) == GET_MODE_PRECISION (TYPE_MODE (type))
3294 /* The inner conversion must be a widening conversion. */
3295 && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
3296 && types_match (@0, type)
3297 && (types_match (@0, @1)
3298 /* Or the second operand is const integer or converted const
3299 integer from valueize. */
3300 || TREE_CODE (@1) == INTEGER_CST))
3301 (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
3302 (op @0 (convert @1))
3303 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
3304 (convert (op (convert:utype @0)
3305 (convert:utype @1))))))))
3307 /* This is another case of narrowing, specifically when there's an outer
3308 BIT_AND_EXPR which masks off bits outside the type of the innermost
3309 operands. Like the previous case we have to convert the operands
3310 to unsigned types to avoid introducing undefined behavior for the
3311 arithmetic operation. */
3312 (for op (minus plus)
3314 (bit_and (op:s (convert@2 @0) (convert@3 @1)) INTEGER_CST@4)
3315 (if (INTEGRAL_TYPE_P (type)
3316 /* We check for type compatibility between @0 and @1 below,
3317 so there's no need to check that @1/@3 are integral types. */
3318 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
3319 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
3320 /* The precision of the type of each operand must match the
3321 precision of the mode of each operand, similarly for the
3323 && (TYPE_PRECISION (TREE_TYPE (@0))
3324 == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (@0))))
3325 && (TYPE_PRECISION (TREE_TYPE (@1))
3326 == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (@1))))
3327 && TYPE_PRECISION (type) == GET_MODE_PRECISION (TYPE_MODE (type))
3328 /* The inner conversion must be a widening conversion. */
3329 && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
3330 && types_match (@0, @1)
3331 && (tree_int_cst_min_precision (@4, TYPE_SIGN (TREE_TYPE (@0)))
3332 <= TYPE_PRECISION (TREE_TYPE (@0)))
3333 && (wi::bit_and (@4, wi::mask (TYPE_PRECISION (TREE_TYPE (@0)),
3334 true, TYPE_PRECISION (type))) == 0))
3335 (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
3336 (with { tree ntype = TREE_TYPE (@0); }
3337 (convert (bit_and (op @0 @1) (convert:ntype @4))))
3338 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
3339 (convert (bit_and (op (convert:utype @0) (convert:utype @1))
3340 (convert:utype @4))))))))
3342 /* Transform (@0 < @1 and @0 < @2) to use min,
3343 (@0 > @1 and @0 > @2) to use max */
3344 (for op (lt le gt ge)
3345 ext (min min max max)
3347 (bit_and (op:cs @0 @1) (op:cs @0 @2))
3348 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
3349 && TREE_CODE (@0) != INTEGER_CST)
3350 (op @0 (ext @1 @2)))))
3353 /* signbit(x) -> 0 if x is nonnegative. */
3354 (SIGNBIT tree_expr_nonnegative_p@0)
3355 { integer_zero_node; })
3358 /* signbit(x) -> x<0 if x doesn't have signed zeros. */
3360 (if (!HONOR_SIGNED_ZEROS (@0))
3361 (convert (lt @0 { build_real (TREE_TYPE (@0), dconst0); }))))
3363 /* Transform comparisons of the form X +- C1 CMP C2 to X CMP C2 -+ C1. */
3365 (for op (plus minus)
3368 (cmp (op@3 @0 INTEGER_CST@1) INTEGER_CST@2)
3369 (if (!TREE_OVERFLOW (@1) && !TREE_OVERFLOW (@2)
3370 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0))
3371 && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@0))
3372 && !TYPE_SATURATING (TREE_TYPE (@0)))
3373 (with { tree res = int_const_binop (rop, @2, @1); }
3374 (if (TREE_OVERFLOW (res))
3375 { constant_boolean_node (cmp == NE_EXPR, type); }
3376 (if (single_use (@3))
3377 (cmp @0 { res; }))))))))
3378 (for cmp (lt le gt ge)
3379 (for op (plus minus)
3382 (cmp (op@3 @0 INTEGER_CST@1) INTEGER_CST@2)
3383 (if (!TREE_OVERFLOW (@1) && !TREE_OVERFLOW (@2)
3384 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
3385 (with { tree res = int_const_binop (rop, @2, @1); }
3386 (if (TREE_OVERFLOW (res))
3388 fold_overflow_warning (("assuming signed overflow does not occur "
3389 "when simplifying conditional to constant"),
3390 WARN_STRICT_OVERFLOW_CONDITIONAL);
3391 bool less = cmp == LE_EXPR || cmp == LT_EXPR;
3392 /* wi::ges_p (@2, 0) should be sufficient for a signed type. */
3393 bool ovf_high = wi::lt_p (@1, 0, TYPE_SIGN (TREE_TYPE (@1)))
3394 != (op == MINUS_EXPR);
3395 constant_boolean_node (less == ovf_high, type);
3397 (if (single_use (@3))
3400 fold_overflow_warning (("assuming signed overflow does not occur "
3401 "when changing X +- C1 cmp C2 to "
3403 WARN_STRICT_OVERFLOW_COMPARISON);
3405 (cmp @0 { res; })))))))))
3407 /* Canonicalizations of BIT_FIELD_REFs. */
3410 (BIT_FIELD_REF @0 @1 @2)
3412 (if (TREE_CODE (TREE_TYPE (@0)) == COMPLEX_TYPE
3413 && tree_int_cst_equal (@1, TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))))
3415 (if (integer_zerop (@2))
3416 (view_convert (realpart @0)))
3417 (if (tree_int_cst_equal (@2, TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))))
3418 (view_convert (imagpart @0)))))
3419 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
3420 && INTEGRAL_TYPE_P (type)
3421 /* On GIMPLE this should only apply to register arguments. */
3422 && (! GIMPLE || is_gimple_reg (@0))
3423 /* A bit-field-ref that referenced the full argument can be stripped. */
3424 && ((compare_tree_int (@1, TYPE_PRECISION (TREE_TYPE (@0))) == 0
3425 && integer_zerop (@2))
3426 /* Low-parts can be reduced to integral conversions.
3427 ??? The following doesn't work for PDP endian. */
3428 || (BYTES_BIG_ENDIAN == WORDS_BIG_ENDIAN
3429 /* Don't even think about BITS_BIG_ENDIAN. */
3430 && TYPE_PRECISION (TREE_TYPE (@0)) % BITS_PER_UNIT == 0
3431 && TYPE_PRECISION (type) % BITS_PER_UNIT == 0
3432 && compare_tree_int (@2, (BYTES_BIG_ENDIAN
3433 ? (TYPE_PRECISION (TREE_TYPE (@0))
3434 - TYPE_PRECISION (type))
3438 /* Simplify vector extracts. */
3441 (BIT_FIELD_REF CONSTRUCTOR@0 @1 @2)
3442 (if (VECTOR_TYPE_P (TREE_TYPE (@0))
3443 && (types_match (type, TREE_TYPE (TREE_TYPE (@0)))
3444 || (VECTOR_TYPE_P (type)
3445 && types_match (TREE_TYPE (type), TREE_TYPE (TREE_TYPE (@0))))))
3448 tree ctor = (TREE_CODE (@0) == SSA_NAME
3449 ? gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0)) : @0);
3450 tree eltype = TREE_TYPE (TREE_TYPE (ctor));
3451 unsigned HOST_WIDE_INT width = tree_to_uhwi (TYPE_SIZE (eltype));
3452 unsigned HOST_WIDE_INT n = tree_to_uhwi (@1);
3453 unsigned HOST_WIDE_INT idx = tree_to_uhwi (@2);
3456 && (idx % width) == 0
3458 && ((idx + n) / width) <= TYPE_VECTOR_SUBPARTS (TREE_TYPE (ctor)))
3463 /* Constructor elements can be subvectors. */
3464 unsigned HOST_WIDE_INT k = 1;
3465 if (CONSTRUCTOR_NELTS (ctor) != 0)
3467 tree cons_elem = TREE_TYPE (CONSTRUCTOR_ELT (ctor, 0)->value);
3468 if (TREE_CODE (cons_elem) == VECTOR_TYPE)
3469 k = TYPE_VECTOR_SUBPARTS (cons_elem);
3473 /* We keep an exact subset of the constructor elements. */
3474 (if ((idx % k) == 0 && (n % k) == 0)
3475 (if (CONSTRUCTOR_NELTS (ctor) == 0)
3476 { build_constructor (type, NULL); }
3483 (if (idx < CONSTRUCTOR_NELTS (ctor))
3484 { CONSTRUCTOR_ELT (ctor, idx)->value; }
3485 { build_zero_cst (type); })
3487 vec<constructor_elt, va_gc> *vals;
3488 vec_alloc (vals, n);
3489 for (unsigned i = 0;
3490 i < n && idx + i < CONSTRUCTOR_NELTS (ctor); ++i)
3491 CONSTRUCTOR_APPEND_ELT (vals, NULL_TREE,
3492 CONSTRUCTOR_ELT (ctor, idx + i)->value);
3493 build_constructor (type, vals);
3495 /* The bitfield references a single constructor element. */
3496 (if (idx + n <= (idx / k + 1) * k)
3498 (if (CONSTRUCTOR_NELTS (ctor) <= idx / k)
3499 { build_zero_cst (type); })
3501 { CONSTRUCTOR_ELT (ctor, idx / k)->value; })
3502 (BIT_FIELD_REF { CONSTRUCTOR_ELT (ctor, idx / k)->value; }
3503 @1 { bitsize_int ((idx % k) * width); })))))))))