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)
544 /* Simplify (~X & Y) to X ^ Y if we know that (X & ~Y) is 0. */
547 (bit_and (bit_not SSA_NAME@0) INTEGER_CST@1)
548 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
549 && (get_nonzero_bits (@0) & wi::bit_not (@1)) == 0)
553 /* X % Y is smaller than Y. */
556 (cmp (trunc_mod @0 @1) @1)
557 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
558 { constant_boolean_node (cmp == LT_EXPR, type); })))
561 (cmp @1 (trunc_mod @0 @1))
562 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
563 { constant_boolean_node (cmp == GT_EXPR, type); })))
567 (bit_ior @0 integer_all_onesp@1)
572 (bit_ior @0 integer_zerop)
577 (bit_and @0 integer_zerop@1)
583 (for op (bit_ior bit_xor plus)
585 (op:c (convert? @0) (convert? (bit_not @0)))
586 (convert { build_all_ones_cst (TREE_TYPE (@0)); })))
591 { build_zero_cst (type); })
593 /* Canonicalize X ^ ~0 to ~X. */
595 (bit_xor @0 integer_all_onesp@1)
600 (bit_and @0 integer_all_onesp)
603 /* x & x -> x, x | x -> x */
604 (for bitop (bit_and bit_ior)
609 /* x & C -> x if we know that x & ~C == 0. */
612 (bit_and SSA_NAME@0 INTEGER_CST@1)
613 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
614 && (get_nonzero_bits (@0) & wi::bit_not (@1)) == 0)
618 /* x + (x & 1) -> (x + 1) & ~1 */
620 (plus:c @0 (bit_and:s @0 integer_onep@1))
621 (bit_and (plus @0 @1) (bit_not @1)))
623 /* x & ~(x & y) -> x & ~y */
624 /* x | ~(x | y) -> x | ~y */
625 (for bitop (bit_and bit_ior)
627 (bitop:c @0 (bit_not (bitop:cs @0 @1)))
628 (bitop @0 (bit_not @1))))
630 /* (x | y) & ~x -> y & ~x */
631 /* (x & y) | ~x -> y | ~x */
632 (for bitop (bit_and bit_ior)
633 rbitop (bit_ior bit_and)
635 (bitop:c (rbitop:c @0 @1) (bit_not@2 @0))
638 /* (x & y) ^ (x | y) -> x ^ y */
640 (bit_xor:c (bit_and @0 @1) (bit_ior @0 @1))
643 /* (x ^ y) ^ (x | y) -> x & y */
645 (bit_xor:c (bit_xor @0 @1) (bit_ior @0 @1))
648 /* (x & y) + (x ^ y) -> x | y */
649 /* (x & y) | (x ^ y) -> x | y */
650 /* (x & y) ^ (x ^ y) -> x | y */
651 (for op (plus bit_ior bit_xor)
653 (op:c (bit_and @0 @1) (bit_xor @0 @1))
656 /* (x & y) + (x | y) -> x + y */
658 (plus:c (bit_and @0 @1) (bit_ior @0 @1))
661 /* (x + y) - (x | y) -> x & y */
663 (minus (plus @0 @1) (bit_ior @0 @1))
664 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
665 && !TYPE_SATURATING (type))
668 /* (x + y) - (x & y) -> x | y */
670 (minus (plus @0 @1) (bit_and @0 @1))
671 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
672 && !TYPE_SATURATING (type))
675 /* (x | y) - (x ^ y) -> x & y */
677 (minus (bit_ior @0 @1) (bit_xor @0 @1))
680 /* (x | y) - (x & y) -> x ^ y */
682 (minus (bit_ior @0 @1) (bit_and @0 @1))
685 /* (x | y) & ~(x & y) -> x ^ y */
687 (bit_and:c (bit_ior @0 @1) (bit_not (bit_and @0 @1)))
690 /* (x | y) & (~x ^ y) -> x & y */
692 (bit_and:c (bit_ior:c @0 @1) (bit_xor:c @1 (bit_not @0)))
695 /* ~x & ~y -> ~(x | y)
696 ~x | ~y -> ~(x & y) */
697 (for op (bit_and bit_ior)
698 rop (bit_ior bit_and)
700 (op (convert1? (bit_not @0)) (convert2? (bit_not @1)))
701 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
702 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
703 (bit_not (rop (convert @0) (convert @1))))))
705 /* If we are XORing or adding two BIT_AND_EXPR's, both of which are and'ing
706 with a constant, and the two constants have no bits in common,
707 we should treat this as a BIT_IOR_EXPR since this may produce more
709 (for op (bit_xor plus)
711 (op (convert1? (bit_and@4 @0 INTEGER_CST@1))
712 (convert2? (bit_and@5 @2 INTEGER_CST@3)))
713 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
714 && tree_nop_conversion_p (type, TREE_TYPE (@2))
715 && wi::bit_and (@1, @3) == 0)
716 (bit_ior (convert @4) (convert @5)))))
718 /* (X | Y) ^ X -> Y & ~ X*/
720 (bit_xor:c (convert1? (bit_ior:c @@0 @1)) (convert2? @0))
721 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
722 (convert (bit_and @1 (bit_not @0)))))
724 /* Convert ~X ^ ~Y to X ^ Y. */
726 (bit_xor (convert1? (bit_not @0)) (convert2? (bit_not @1)))
727 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
728 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
729 (bit_xor (convert @0) (convert @1))))
731 /* Convert ~X ^ C to X ^ ~C. */
733 (bit_xor (convert? (bit_not @0)) INTEGER_CST@1)
734 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
735 (bit_xor (convert @0) (bit_not @1))))
737 /* Fold (X & Y) ^ Y and (X ^ Y) & Y as ~X & Y. */
738 (for opo (bit_and bit_xor)
739 opi (bit_xor bit_and)
741 (opo:c (opi:c @0 @1) @1)
742 (bit_and (bit_not @0) @1)))
744 /* Given a bit-wise operation CODE applied to ARG0 and ARG1, see if both
745 operands are another bit-wise operation with a common input. If so,
746 distribute the bit operations to save an operation and possibly two if
747 constants are involved. For example, convert
748 (A | B) & (A | C) into A | (B & C)
749 Further simplification will occur if B and C are constants. */
750 (for op (bit_and bit_ior bit_xor)
751 rop (bit_ior bit_and bit_and)
753 (op (convert? (rop:c @@0 @1)) (convert? (rop:c @0 @2)))
754 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
755 && tree_nop_conversion_p (type, TREE_TYPE (@2)))
756 (rop (convert @0) (op (convert @1) (convert @2))))))
758 /* Some simple reassociation for bit operations, also handled in reassoc. */
759 /* (X & Y) & Y -> X & Y
760 (X | Y) | Y -> X | Y */
761 (for op (bit_and bit_ior)
763 (op:c (convert1?@2 (op:c @0 @@1)) (convert2? @1))
765 /* (X ^ Y) ^ Y -> X */
767 (bit_xor:c (convert1? (bit_xor:c @0 @@1)) (convert2? @1))
769 /* (X & Y) & (X & Z) -> (X & Y) & Z
770 (X | Y) | (X | Z) -> (X | Y) | Z */
771 (for op (bit_and bit_ior)
773 (op:c (convert1?@3 (op:c@4 @0 @1)) (convert2?@5 (op:c@6 @0 @2)))
774 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
775 && tree_nop_conversion_p (type, TREE_TYPE (@2)))
776 (if (single_use (@5) && single_use (@6))
778 (if (single_use (@3) && single_use (@4))
779 (op (convert @1) @5))))))
780 /* (X ^ Y) ^ (X ^ Z) -> Y ^ Z */
782 (bit_xor (convert1? (bit_xor:c @0 @1)) (convert2? (bit_xor:c @0 @2)))
783 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
784 && tree_nop_conversion_p (type, TREE_TYPE (@2)))
785 (bit_xor (convert @1) (convert @2))))
794 (abs tree_expr_nonnegative_p@0)
797 /* A few cases of fold-const.c negate_expr_p predicate. */
800 (if ((INTEGRAL_TYPE_P (type)
801 && TYPE_OVERFLOW_WRAPS (type))
802 || (!TYPE_OVERFLOW_SANITIZED (type)
803 && may_negate_without_overflow_p (t)))))
808 (if (!TYPE_OVERFLOW_SANITIZED (type))))
811 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (t)))))
812 /* VECTOR_CST handling of non-wrapping types would recurse in unsupported
816 (if (FLOAT_TYPE_P (TREE_TYPE (type)) || TYPE_OVERFLOW_WRAPS (type))))
818 /* (-A) * (-B) -> A * B */
820 (mult:c (convert1? (negate @0)) (convert2? negate_expr_p@1))
821 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
822 && tree_nop_conversion_p (type, TREE_TYPE (@1)))
823 (mult (convert @0) (convert (negate @1)))))
825 /* -(A + B) -> (-B) - A. */
827 (negate (plus:c @0 negate_expr_p@1))
828 (if (!HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type))
829 && !HONOR_SIGNED_ZEROS (element_mode (type)))
830 (minus (negate @1) @0)))
832 /* A - B -> A + (-B) if B is easily negatable. */
834 (minus @0 negate_expr_p@1)
835 (if (!FIXED_POINT_TYPE_P (type))
836 (plus @0 (negate @1))))
838 /* Try to fold (type) X op CST -> (type) (X op ((type-x) CST))
840 For bitwise binary operations apply operand conversions to the
841 binary operation result instead of to the operands. This allows
842 to combine successive conversions and bitwise binary operations.
843 We combine the above two cases by using a conditional convert. */
844 (for bitop (bit_and bit_ior bit_xor)
846 (bitop (convert @0) (convert? @1))
847 (if (((TREE_CODE (@1) == INTEGER_CST
848 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
849 && int_fits_type_p (@1, TREE_TYPE (@0)))
850 || types_match (@0, @1))
851 /* ??? This transform conflicts with fold-const.c doing
852 Convert (T)(x & c) into (T)x & (T)c, if c is an integer
853 constants (if x has signed type, the sign bit cannot be set
854 in c). This folds extension into the BIT_AND_EXPR.
855 Restrict it to GIMPLE to avoid endless recursions. */
856 && (bitop != BIT_AND_EXPR || GIMPLE)
857 && (/* That's a good idea if the conversion widens the operand, thus
858 after hoisting the conversion the operation will be narrower. */
859 TYPE_PRECISION (TREE_TYPE (@0)) < TYPE_PRECISION (type)
860 /* It's also a good idea if the conversion is to a non-integer
862 || GET_MODE_CLASS (TYPE_MODE (type)) != MODE_INT
863 /* Or if the precision of TO is not the same as the precision
865 || TYPE_PRECISION (type) != GET_MODE_PRECISION (TYPE_MODE (type))))
866 (convert (bitop @0 (convert @1))))))
868 (for bitop (bit_and bit_ior)
869 rbitop (bit_ior bit_and)
870 /* (x | y) & x -> x */
871 /* (x & y) | x -> x */
873 (bitop:c (rbitop:c @0 @1) @0)
875 /* (~x | y) & x -> x & y */
876 /* (~x & y) | x -> x | y */
878 (bitop:c (rbitop:c (bit_not @0) @1) @0)
881 /* (x | CST1) & CST2 -> (x & CST2) | (CST1 & CST2) */
883 (bit_and (bit_ior @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
884 (bit_ior (bit_and @0 @2) (bit_and @1 @2)))
886 /* Combine successive equal operations with constants. */
887 (for bitop (bit_and bit_ior bit_xor)
889 (bitop (bitop @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
890 (bitop @0 (bitop @1 @2))))
892 /* Try simple folding for X op !X, and X op X with the help
893 of the truth_valued_p and logical_inverted_value predicates. */
894 (match truth_valued_p
896 (if (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1)))
897 (for op (tcc_comparison truth_and truth_andif truth_or truth_orif truth_xor)
898 (match truth_valued_p
900 (match truth_valued_p
903 (match (logical_inverted_value @0)
905 (match (logical_inverted_value @0)
906 (bit_not truth_valued_p@0))
907 (match (logical_inverted_value @0)
908 (eq @0 integer_zerop))
909 (match (logical_inverted_value @0)
910 (ne truth_valued_p@0 integer_truep))
911 (match (logical_inverted_value @0)
912 (bit_xor truth_valued_p@0 integer_truep))
916 (bit_and:c @0 (logical_inverted_value @0))
917 { build_zero_cst (type); })
918 /* X | !X and X ^ !X -> 1, , if X is truth-valued. */
919 (for op (bit_ior bit_xor)
921 (op:c truth_valued_p@0 (logical_inverted_value @0))
922 { constant_boolean_node (true, type); }))
923 /* X ==/!= !X is false/true. */
926 (op:c truth_valued_p@0 (logical_inverted_value @0))
927 { constant_boolean_node (op == NE_EXPR ? true : false, type); }))
929 /* If arg1 and arg2 are booleans (or any single bit type)
930 then try to simplify:
937 But only do this if our result feeds into a comparison as
938 this transformation is not always a win, particularly on
939 targets with and-not instructions.
940 -> simplify_bitwise_binary_boolean */
942 (ne (bit_and:c (bit_not @0) @1) integer_zerop)
943 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
944 && TYPE_PRECISION (TREE_TYPE (@1)) == 1)
945 (if (TYPE_UNSIGNED (TREE_TYPE (@1)))
949 (ne (bit_ior:c (bit_not @0) @1) integer_zerop)
950 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
951 && TYPE_PRECISION (TREE_TYPE (@1)) == 1)
952 (if (TYPE_UNSIGNED (TREE_TYPE (@1)))
958 (bit_not (bit_not @0))
961 /* Convert ~ (-A) to A - 1. */
963 (bit_not (convert? (negate @0)))
964 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
965 || !TYPE_UNSIGNED (TREE_TYPE (@0)))
966 (convert (minus @0 { build_each_one_cst (TREE_TYPE (@0)); }))))
968 /* Convert ~ (A - 1) or ~ (A + -1) to -A. */
970 (bit_not (convert? (minus @0 integer_each_onep)))
971 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
972 || !TYPE_UNSIGNED (TREE_TYPE (@0)))
973 (convert (negate @0))))
975 (bit_not (convert? (plus @0 integer_all_onesp)))
976 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
977 || !TYPE_UNSIGNED (TREE_TYPE (@0)))
978 (convert (negate @0))))
980 /* Part of convert ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify. */
982 (bit_not (convert? (bit_xor @0 INTEGER_CST@1)))
983 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
984 (convert (bit_xor @0 (bit_not @1)))))
986 (bit_not (convert? (bit_xor:c (bit_not @0) @1)))
987 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
988 (convert (bit_xor @0 @1))))
990 /* (x & ~m) | (y & m) -> ((x ^ y) & m) ^ x */
992 (bit_ior:c (bit_and:cs @0 (bit_not @2)) (bit_and:cs @1 @2))
993 (bit_xor (bit_and (bit_xor @0 @1) @2) @0))
995 /* Fold A - (A & B) into ~B & A. */
997 (minus (convert1? @0) (convert2?:s (bit_and:cs @@0 @1)))
998 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
999 && tree_nop_conversion_p (type, TREE_TYPE (@1)))
1000 (convert (bit_and (bit_not @1) @0))))
1004 /* ((X inner_op C0) outer_op C1)
1005 With X being a tree where value_range has reasoned certain bits to always be
1006 zero throughout its computed value range,
1007 inner_op = {|,^}, outer_op = {|,^} and inner_op != outer_op
1008 where zero_mask has 1's for all bits that are sure to be 0 in
1010 if (inner_op == '^') C0 &= ~C1;
1011 if ((C0 & ~zero_mask) == 0) then emit (X outer_op (C0 outer_op C1)
1012 if ((C1 & ~zero_mask) == 0) then emit (X inner_op (C0 outer_op C1)
1014 (for inner_op (bit_ior bit_xor)
1015 outer_op (bit_xor bit_ior)
1018 (inner_op:s @2 INTEGER_CST@0) INTEGER_CST@1)
1022 wide_int zero_mask_not;
1026 if (TREE_CODE (@2) == SSA_NAME)
1027 zero_mask_not = get_nonzero_bits (@2);
1031 if (inner_op == BIT_XOR_EXPR)
1033 C0 = wi::bit_and_not (@0, @1);
1034 cst_emit = wi::bit_or (C0, @1);
1039 cst_emit = wi::bit_xor (@0, @1);
1042 (if (!fail && wi::bit_and (C0, zero_mask_not) == 0)
1043 (outer_op @2 { wide_int_to_tree (type, cst_emit); })
1044 (if (!fail && wi::bit_and (@1, zero_mask_not) == 0)
1045 (inner_op @2 { wide_int_to_tree (type, cst_emit); }))))))
1047 /* Associate (p +p off1) +p off2 as (p +p (off1 + off2)). */
1049 (pointer_plus (pointer_plus:s @0 @1) @3)
1050 (pointer_plus @0 (plus @1 @3)))
1056 tem4 = (unsigned long) tem3;
1061 (pointer_plus @0 (convert?@2 (minus@3 (convert @1) (convert @0))))
1062 /* Conditionally look through a sign-changing conversion. */
1063 (if (TYPE_PRECISION (TREE_TYPE (@2)) == TYPE_PRECISION (TREE_TYPE (@3))
1064 && ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@1)))
1065 || (GENERIC && type == TREE_TYPE (@1))))
1069 tem = (sizetype) ptr;
1073 and produce the simpler and easier to analyze with respect to alignment
1074 ... = ptr & ~algn; */
1076 (pointer_plus @0 (negate (bit_and (convert @0) INTEGER_CST@1)))
1077 (with { tree algn = wide_int_to_tree (TREE_TYPE (@0), wi::bit_not (@1)); }
1078 (bit_and @0 { algn; })))
1080 /* Try folding difference of addresses. */
1082 (minus (convert ADDR_EXPR@0) (convert @1))
1083 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1084 (with { HOST_WIDE_INT diff; }
1085 (if (ptr_difference_const (@0, @1, &diff))
1086 { build_int_cst_type (type, diff); }))))
1088 (minus (convert @0) (convert ADDR_EXPR@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 /* If arg0 is derived from the address of an object or function, we may
1095 be able to fold this expression using the object or function's
1098 (bit_and (convert? @0) INTEGER_CST@1)
1099 (if (POINTER_TYPE_P (TREE_TYPE (@0))
1100 && tree_nop_conversion_p (type, TREE_TYPE (@0)))
1104 unsigned HOST_WIDE_INT bitpos;
1105 get_pointer_alignment_1 (@0, &align, &bitpos);
1107 (if (wi::ltu_p (@1, align / BITS_PER_UNIT))
1108 { wide_int_to_tree (type, wi::bit_and (@1, bitpos / BITS_PER_UNIT)); }))))
1111 /* We can't reassociate at all for saturating types. */
1112 (if (!TYPE_SATURATING (type))
1114 /* Contract negates. */
1115 /* A + (-B) -> A - B */
1117 (plus:c (convert1? @0) (convert2? (negate @1)))
1118 /* Apply STRIP_NOPS on @0 and the negate. */
1119 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
1120 && tree_nop_conversion_p (type, TREE_TYPE (@1))
1121 && !TYPE_OVERFLOW_SANITIZED (type))
1122 (minus (convert @0) (convert @1))))
1123 /* A - (-B) -> A + B */
1125 (minus (convert1? @0) (convert2? (negate @1)))
1126 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
1127 && tree_nop_conversion_p (type, TREE_TYPE (@1))
1128 && !TYPE_OVERFLOW_SANITIZED (type))
1129 (plus (convert @0) (convert @1))))
1132 (negate (convert? (negate @1)))
1133 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1134 && !TYPE_OVERFLOW_SANITIZED (type))
1137 /* We can't reassociate floating-point unless -fassociative-math
1138 or fixed-point plus or minus because of saturation to +-Inf. */
1139 (if ((!FLOAT_TYPE_P (type) || flag_associative_math)
1140 && !FIXED_POINT_TYPE_P (type))
1142 /* Match patterns that allow contracting a plus-minus pair
1143 irrespective of overflow issues. */
1144 /* (A +- B) - A -> +- B */
1145 /* (A +- B) -+ B -> A */
1146 /* A - (A +- B) -> -+ B */
1147 /* A +- (B -+ A) -> +- B */
1149 (minus (plus:c @0 @1) @0)
1152 (minus (minus @0 @1) @0)
1155 (plus:c (minus @0 @1) @1)
1158 (minus @0 (plus:c @0 @1))
1161 (minus @0 (minus @0 @1))
1164 /* (A +- CST) +- CST -> A + CST */
1165 (for outer_op (plus minus)
1166 (for inner_op (plus minus)
1168 (outer_op (inner_op @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
1169 /* If the constant operation overflows we cannot do the transform
1170 as we would introduce undefined overflow, for example
1171 with (a - 1) + INT_MIN. */
1172 (with { tree cst = const_binop (outer_op == inner_op
1173 ? PLUS_EXPR : MINUS_EXPR, type, @1, @2); }
1174 (if (cst && !TREE_OVERFLOW (cst))
1175 (inner_op @0 { cst; } ))))))
1177 /* (CST - A) +- CST -> CST - A */
1178 (for outer_op (plus minus)
1180 (outer_op (minus CONSTANT_CLASS_P@1 @0) CONSTANT_CLASS_P@2)
1181 (with { tree cst = const_binop (outer_op, type, @1, @2); }
1182 (if (cst && !TREE_OVERFLOW (cst))
1183 (minus { cst; } @0)))))
1187 (plus:c (bit_not @0) @0)
1188 (if (!TYPE_OVERFLOW_TRAPS (type))
1189 { build_all_ones_cst (type); }))
1193 (plus (convert? (bit_not @0)) integer_each_onep)
1194 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1195 (negate (convert @0))))
1199 (minus (convert? (negate @0)) integer_each_onep)
1200 (if (!TYPE_OVERFLOW_TRAPS (type)
1201 && tree_nop_conversion_p (type, TREE_TYPE (@0)))
1202 (bit_not (convert @0))))
1206 (minus integer_all_onesp @0)
1209 /* (T)(P + A) - (T)P -> (T) A */
1210 (for add (plus pointer_plus)
1212 (minus (convert (add @@0 @1))
1214 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
1215 /* For integer types, if A has a smaller type
1216 than T the result depends on the possible
1218 E.g. T=size_t, A=(unsigned)429497295, P>0.
1219 However, if an overflow in P + A would cause
1220 undefined behavior, we can assume that there
1222 || (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1223 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1224 /* For pointer types, if the conversion of A to the
1225 final type requires a sign- or zero-extension,
1226 then we have to punt - it is not defined which
1228 || (POINTER_TYPE_P (TREE_TYPE (@0))
1229 && TREE_CODE (@1) == INTEGER_CST
1230 && tree_int_cst_sign_bit (@1) == 0))
1233 /* (T)P - (T)(P + A) -> -(T) A */
1234 (for add (plus pointer_plus)
1237 (convert (add @@0 @1)))
1238 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
1239 /* For integer types, if A has a smaller type
1240 than T the result depends on the possible
1242 E.g. T=size_t, A=(unsigned)429497295, P>0.
1243 However, if an overflow in P + A would cause
1244 undefined behavior, we can assume that there
1246 || (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1247 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1248 /* For pointer types, if the conversion of A to the
1249 final type requires a sign- or zero-extension,
1250 then we have to punt - it is not defined which
1252 || (POINTER_TYPE_P (TREE_TYPE (@0))
1253 && TREE_CODE (@1) == INTEGER_CST
1254 && tree_int_cst_sign_bit (@1) == 0))
1255 (negate (convert @1)))))
1257 /* (T)(P + A) - (T)(P + B) -> (T)A - (T)B */
1258 (for add (plus pointer_plus)
1260 (minus (convert (add @@0 @1))
1261 (convert (add @0 @2)))
1262 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
1263 /* For integer types, if A has a smaller type
1264 than T the result depends on the possible
1266 E.g. T=size_t, A=(unsigned)429497295, P>0.
1267 However, if an overflow in P + A would cause
1268 undefined behavior, we can assume that there
1270 || (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1271 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1272 /* For pointer types, if the conversion of A to the
1273 final type requires a sign- or zero-extension,
1274 then we have to punt - it is not defined which
1276 || (POINTER_TYPE_P (TREE_TYPE (@0))
1277 && TREE_CODE (@1) == INTEGER_CST
1278 && tree_int_cst_sign_bit (@1) == 0
1279 && TREE_CODE (@2) == INTEGER_CST
1280 && tree_int_cst_sign_bit (@2) == 0))
1281 (minus (convert @1) (convert @2)))))))
1284 /* Simplifications of MIN_EXPR, MAX_EXPR, fmin() and fmax(). */
1286 (for minmax (min max FMIN FMAX)
1290 /* min(max(x,y),y) -> y. */
1292 (min:c (max:c @0 @1) @1)
1294 /* max(min(x,y),y) -> y. */
1296 (max:c (min:c @0 @1) @1)
1298 /* max(a,-a) -> abs(a). */
1300 (max:c @0 (negate @0))
1301 (if (TREE_CODE (type) != COMPLEX_TYPE
1302 && (! ANY_INTEGRAL_TYPE_P (type)
1303 || TYPE_OVERFLOW_UNDEFINED (type)))
1305 /* min(a,-a) -> -abs(a). */
1307 (min:c @0 (negate @0))
1308 (if (TREE_CODE (type) != COMPLEX_TYPE
1309 && (! ANY_INTEGRAL_TYPE_P (type)
1310 || TYPE_OVERFLOW_UNDEFINED (type)))
1315 (if (INTEGRAL_TYPE_P (type)
1316 && TYPE_MIN_VALUE (type)
1317 && operand_equal_p (@1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
1319 (if (INTEGRAL_TYPE_P (type)
1320 && TYPE_MAX_VALUE (type)
1321 && operand_equal_p (@1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
1326 (if (INTEGRAL_TYPE_P (type)
1327 && TYPE_MAX_VALUE (type)
1328 && operand_equal_p (@1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
1330 (if (INTEGRAL_TYPE_P (type)
1331 && TYPE_MIN_VALUE (type)
1332 && operand_equal_p (@1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
1334 (for minmax (FMIN FMAX)
1335 /* If either argument is NaN, return the other one. Avoid the
1336 transformation if we get (and honor) a signalling NaN. */
1338 (minmax:c @0 REAL_CST@1)
1339 (if (real_isnan (TREE_REAL_CST_PTR (@1))
1340 && (!HONOR_SNANS (@1) || !TREE_REAL_CST (@1).signalling))
1342 /* Convert fmin/fmax to MIN_EXPR/MAX_EXPR. C99 requires these
1343 functions to return the numeric arg if the other one is NaN.
1344 MIN and MAX don't honor that, so only transform if -ffinite-math-only
1345 is set. C99 doesn't require -0.0 to be handled, so we don't have to
1346 worry about it either. */
1347 (if (flag_finite_math_only)
1354 /* min (-A, -B) -> -max (A, B) */
1355 (for minmax (min max FMIN FMAX)
1356 maxmin (max min FMAX FMIN)
1358 (minmax (negate:s@2 @0) (negate:s@3 @1))
1359 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
1360 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1361 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
1362 (negate (maxmin @0 @1)))))
1363 /* MIN (~X, ~Y) -> ~MAX (X, Y)
1364 MAX (~X, ~Y) -> ~MIN (X, Y) */
1365 (for minmax (min max)
1368 (minmax (bit_not:s@2 @0) (bit_not:s@3 @1))
1369 (bit_not (maxmin @0 @1))))
1371 /* MIN (X, Y) == X -> X <= Y */
1372 (for minmax (min min max max)
1376 (cmp:c (minmax:c @0 @1) @0)
1377 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0)))
1379 /* MIN (X, 5) == 0 -> X == 0
1380 MIN (X, 5) == 7 -> false */
1383 (cmp (min @0 INTEGER_CST@1) INTEGER_CST@2)
1384 (if (wi::lt_p (@1, @2, TYPE_SIGN (TREE_TYPE (@0))))
1385 { constant_boolean_node (cmp == NE_EXPR, type); }
1386 (if (wi::gt_p (@1, @2, TYPE_SIGN (TREE_TYPE (@0))))
1390 (cmp (max @0 INTEGER_CST@1) INTEGER_CST@2)
1391 (if (wi::gt_p (@1, @2, TYPE_SIGN (TREE_TYPE (@0))))
1392 { constant_boolean_node (cmp == NE_EXPR, type); }
1393 (if (wi::lt_p (@1, @2, TYPE_SIGN (TREE_TYPE (@0))))
1395 /* MIN (X, C1) < C2 -> X < C2 || C1 < C2 */
1396 (for minmax (min min max max min min max max )
1397 cmp (lt le gt ge gt ge lt le )
1398 comb (bit_ior bit_ior bit_ior bit_ior bit_and bit_and bit_and bit_and)
1400 (cmp (minmax @0 INTEGER_CST@1) INTEGER_CST@2)
1401 (comb (cmp @0 @2) (cmp @1 @2))))
1403 /* Simplifications of shift and rotates. */
1405 (for rotate (lrotate rrotate)
1407 (rotate integer_all_onesp@0 @1)
1410 /* Optimize -1 >> x for arithmetic right shifts. */
1412 (rshift integer_all_onesp@0 @1)
1413 (if (!TYPE_UNSIGNED (type)
1414 && tree_expr_nonnegative_p (@1))
1417 /* Optimize (x >> c) << c into x & (-1<<c). */
1419 (lshift (rshift @0 INTEGER_CST@1) @1)
1420 (if (wi::ltu_p (@1, element_precision (type)))
1421 (bit_and @0 (lshift { build_minus_one_cst (type); } @1))))
1423 /* Optimize (x << c) >> c into x & ((unsigned)-1 >> c) for unsigned
1426 (rshift (lshift @0 INTEGER_CST@1) @1)
1427 (if (TYPE_UNSIGNED (type)
1428 && (wi::ltu_p (@1, element_precision (type))))
1429 (bit_and @0 (rshift { build_minus_one_cst (type); } @1))))
1431 (for shiftrotate (lrotate rrotate lshift rshift)
1433 (shiftrotate @0 integer_zerop)
1436 (shiftrotate integer_zerop@0 @1)
1438 /* Prefer vector1 << scalar to vector1 << vector2
1439 if vector2 is uniform. */
1440 (for vec (VECTOR_CST CONSTRUCTOR)
1442 (shiftrotate @0 vec@1)
1443 (with { tree tem = uniform_vector_p (@1); }
1445 (shiftrotate @0 { tem; }))))))
1447 /* Rewrite an LROTATE_EXPR by a constant into an
1448 RROTATE_EXPR by a new constant. */
1450 (lrotate @0 INTEGER_CST@1)
1451 (rrotate @0 { const_binop (MINUS_EXPR, TREE_TYPE (@1),
1452 build_int_cst (TREE_TYPE (@1),
1453 element_precision (type)), @1); }))
1455 /* Turn (a OP c1) OP c2 into a OP (c1+c2). */
1456 (for op (lrotate rrotate rshift lshift)
1458 (op (op @0 INTEGER_CST@1) INTEGER_CST@2)
1459 (with { unsigned int prec = element_precision (type); }
1460 (if (wi::ge_p (@1, 0, TYPE_SIGN (TREE_TYPE (@1)))
1461 && wi::lt_p (@1, prec, TYPE_SIGN (TREE_TYPE (@1)))
1462 && wi::ge_p (@2, 0, TYPE_SIGN (TREE_TYPE (@2)))
1463 && wi::lt_p (@2, prec, TYPE_SIGN (TREE_TYPE (@2))))
1464 (with { unsigned int low = wi::add (@1, @2).to_uhwi (); }
1465 /* Deal with a OP (c1 + c2) being undefined but (a OP c1) OP c2
1466 being well defined. */
1468 (if (op == LROTATE_EXPR || op == RROTATE_EXPR)
1469 (op @0 { build_int_cst (TREE_TYPE (@1), low % prec); })
1470 (if (TYPE_UNSIGNED (type) || op == LSHIFT_EXPR)
1471 { build_zero_cst (type); }
1472 (op @0 { build_int_cst (TREE_TYPE (@1), prec - 1); })))
1473 (op @0 { build_int_cst (TREE_TYPE (@1), low); })))))))
1476 /* ((1 << A) & 1) != 0 -> A == 0
1477 ((1 << A) & 1) == 0 -> A != 0 */
1481 (cmp (bit_and (lshift integer_onep @0) integer_onep) integer_zerop)
1482 (icmp @0 { build_zero_cst (TREE_TYPE (@0)); })))
1484 /* (CST1 << A) == CST2 -> A == ctz (CST2) - ctz (CST1)
1485 (CST1 << A) != CST2 -> A != ctz (CST2) - ctz (CST1)
1489 (cmp (lshift INTEGER_CST@0 @1) INTEGER_CST@2)
1490 (with { int cand = wi::ctz (@2) - wi::ctz (@0); }
1492 || (!integer_zerop (@2)
1493 && wi::ne_p (wi::lshift (@0, cand), @2)))
1494 { constant_boolean_node (cmp == NE_EXPR, type); }
1495 (if (!integer_zerop (@2)
1496 && wi::eq_p (wi::lshift (@0, cand), @2))
1497 (cmp @1 { build_int_cst (TREE_TYPE (@1), cand); }))))))
1499 /* Fold (X << C1) & C2 into (X << C1) & (C2 | ((1 << C1) - 1))
1500 (X >> C1) & C2 into (X >> C1) & (C2 | ~((type) -1 >> C1))
1501 if the new mask might be further optimized. */
1502 (for shift (lshift rshift)
1504 (bit_and (convert?:s@4 (shift:s@5 (convert1?@3 @0) INTEGER_CST@1))
1506 (if (tree_nop_conversion_p (TREE_TYPE (@4), TREE_TYPE (@5))
1507 && TYPE_PRECISION (type) <= HOST_BITS_PER_WIDE_INT
1508 && tree_fits_uhwi_p (@1)
1509 && tree_to_uhwi (@1) > 0
1510 && tree_to_uhwi (@1) < TYPE_PRECISION (type))
1513 unsigned int shiftc = tree_to_uhwi (@1);
1514 unsigned HOST_WIDE_INT mask = TREE_INT_CST_LOW (@2);
1515 unsigned HOST_WIDE_INT newmask, zerobits = 0;
1516 tree shift_type = TREE_TYPE (@3);
1519 if (shift == LSHIFT_EXPR)
1520 zerobits = ((HOST_WIDE_INT_1U << shiftc) - 1);
1521 else if (shift == RSHIFT_EXPR
1522 && (TYPE_PRECISION (shift_type)
1523 == GET_MODE_PRECISION (TYPE_MODE (shift_type))))
1525 prec = TYPE_PRECISION (TREE_TYPE (@3));
1527 /* See if more bits can be proven as zero because of
1530 && TYPE_UNSIGNED (TREE_TYPE (@0)))
1532 tree inner_type = TREE_TYPE (@0);
1533 if ((TYPE_PRECISION (inner_type)
1534 == GET_MODE_PRECISION (TYPE_MODE (inner_type)))
1535 && TYPE_PRECISION (inner_type) < prec)
1537 prec = TYPE_PRECISION (inner_type);
1538 /* See if we can shorten the right shift. */
1540 shift_type = inner_type;
1541 /* Otherwise X >> C1 is all zeros, so we'll optimize
1542 it into (X, 0) later on by making sure zerobits
1546 zerobits = HOST_WIDE_INT_M1U;
1549 zerobits >>= HOST_BITS_PER_WIDE_INT - shiftc;
1550 zerobits <<= prec - shiftc;
1552 /* For arithmetic shift if sign bit could be set, zerobits
1553 can contain actually sign bits, so no transformation is
1554 possible, unless MASK masks them all away. In that
1555 case the shift needs to be converted into logical shift. */
1556 if (!TYPE_UNSIGNED (TREE_TYPE (@3))
1557 && prec == TYPE_PRECISION (TREE_TYPE (@3)))
1559 if ((mask & zerobits) == 0)
1560 shift_type = unsigned_type_for (TREE_TYPE (@3));
1566 /* ((X << 16) & 0xff00) is (X, 0). */
1567 (if ((mask & zerobits) == mask)
1568 { build_int_cst (type, 0); }
1569 (with { newmask = mask | zerobits; }
1570 (if (newmask != mask && (newmask & (newmask + 1)) == 0)
1573 /* Only do the transformation if NEWMASK is some integer
1575 for (prec = BITS_PER_UNIT;
1576 prec < HOST_BITS_PER_WIDE_INT; prec <<= 1)
1577 if (newmask == (HOST_WIDE_INT_1U << prec) - 1)
1580 (if (prec < HOST_BITS_PER_WIDE_INT
1581 || newmask == HOST_WIDE_INT_M1U)
1583 { tree newmaskt = build_int_cst_type (TREE_TYPE (@2), newmask); }
1584 (if (!tree_int_cst_equal (newmaskt, @2))
1585 (if (shift_type != TREE_TYPE (@3))
1586 (bit_and (convert (shift:shift_type (convert @3) @1)) { newmaskt; })
1587 (bit_and @4 { newmaskt; })))))))))))))
1589 /* Fold (X {&,^,|} C2) << C1 into (X << C1) {&,^,|} (C2 << C1)
1590 (X {&,^,|} C2) >> C1 into (X >> C1) & (C2 >> C1). */
1591 (for shift (lshift rshift)
1592 (for bit_op (bit_and bit_xor bit_ior)
1594 (shift (convert?:s (bit_op:s @0 INTEGER_CST@2)) INTEGER_CST@1)
1595 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1596 (with { tree mask = int_const_binop (shift, fold_convert (type, @2), @1); }
1597 (bit_op (shift (convert @0) @1) { mask; }))))))
1599 /* ~(~X >> Y) -> X >> Y (for arithmetic shift). */
1601 (bit_not (convert1?:s (rshift:s (convert2?@0 (bit_not @1)) @2)))
1602 (if (!TYPE_UNSIGNED (TREE_TYPE (@0))
1603 && (element_precision (TREE_TYPE (@0))
1604 <= element_precision (TREE_TYPE (@1))
1605 || !TYPE_UNSIGNED (TREE_TYPE (@1))))
1607 { tree shift_type = TREE_TYPE (@0); }
1608 (convert (rshift (convert:shift_type @1) @2)))))
1610 /* ~(~X >>r Y) -> X >>r Y
1611 ~(~X <<r Y) -> X <<r Y */
1612 (for rotate (lrotate rrotate)
1614 (bit_not (convert1?:s (rotate:s (convert2?@0 (bit_not @1)) @2)))
1615 (if ((element_precision (TREE_TYPE (@0))
1616 <= element_precision (TREE_TYPE (@1))
1617 || !TYPE_UNSIGNED (TREE_TYPE (@1)))
1618 && (element_precision (type) <= element_precision (TREE_TYPE (@0))
1619 || !TYPE_UNSIGNED (TREE_TYPE (@0))))
1621 { tree rotate_type = TREE_TYPE (@0); }
1622 (convert (rotate (convert:rotate_type @1) @2))))))
1624 /* Simplifications of conversions. */
1626 /* Basic strip-useless-type-conversions / strip_nops. */
1627 (for cvt (convert view_convert float fix_trunc)
1630 (if ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@0)))
1631 || (GENERIC && type == TREE_TYPE (@0)))
1634 /* Contract view-conversions. */
1636 (view_convert (view_convert @0))
1639 /* For integral conversions with the same precision or pointer
1640 conversions use a NOP_EXPR instead. */
1643 (if ((INTEGRAL_TYPE_P (type) || POINTER_TYPE_P (type))
1644 && (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
1645 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (@0)))
1648 /* Strip inner integral conversions that do not change precision or size. */
1650 (view_convert (convert@0 @1))
1651 (if ((INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
1652 && (INTEGRAL_TYPE_P (TREE_TYPE (@1)) || POINTER_TYPE_P (TREE_TYPE (@1)))
1653 && (TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
1654 && (TYPE_SIZE (TREE_TYPE (@0)) == TYPE_SIZE (TREE_TYPE (@1))))
1657 /* Re-association barriers around constants and other re-association
1658 barriers can be removed. */
1660 (paren CONSTANT_CLASS_P@0)
1663 (paren (paren@1 @0))
1666 /* Handle cases of two conversions in a row. */
1667 (for ocvt (convert float fix_trunc)
1668 (for icvt (convert float)
1673 tree inside_type = TREE_TYPE (@0);
1674 tree inter_type = TREE_TYPE (@1);
1675 int inside_int = INTEGRAL_TYPE_P (inside_type);
1676 int inside_ptr = POINTER_TYPE_P (inside_type);
1677 int inside_float = FLOAT_TYPE_P (inside_type);
1678 int inside_vec = VECTOR_TYPE_P (inside_type);
1679 unsigned int inside_prec = TYPE_PRECISION (inside_type);
1680 int inside_unsignedp = TYPE_UNSIGNED (inside_type);
1681 int inter_int = INTEGRAL_TYPE_P (inter_type);
1682 int inter_ptr = POINTER_TYPE_P (inter_type);
1683 int inter_float = FLOAT_TYPE_P (inter_type);
1684 int inter_vec = VECTOR_TYPE_P (inter_type);
1685 unsigned int inter_prec = TYPE_PRECISION (inter_type);
1686 int inter_unsignedp = TYPE_UNSIGNED (inter_type);
1687 int final_int = INTEGRAL_TYPE_P (type);
1688 int final_ptr = POINTER_TYPE_P (type);
1689 int final_float = FLOAT_TYPE_P (type);
1690 int final_vec = VECTOR_TYPE_P (type);
1691 unsigned int final_prec = TYPE_PRECISION (type);
1692 int final_unsignedp = TYPE_UNSIGNED (type);
1695 /* In addition to the cases of two conversions in a row
1696 handled below, if we are converting something to its own
1697 type via an object of identical or wider precision, neither
1698 conversion is needed. */
1699 (if (((GIMPLE && useless_type_conversion_p (type, inside_type))
1701 && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (inside_type)))
1702 && (((inter_int || inter_ptr) && final_int)
1703 || (inter_float && final_float))
1704 && inter_prec >= final_prec)
1707 /* Likewise, if the intermediate and initial types are either both
1708 float or both integer, we don't need the middle conversion if the
1709 former is wider than the latter and doesn't change the signedness
1710 (for integers). Avoid this if the final type is a pointer since
1711 then we sometimes need the middle conversion. */
1712 (if (((inter_int && inside_int) || (inter_float && inside_float))
1713 && (final_int || final_float)
1714 && inter_prec >= inside_prec
1715 && (inter_float || inter_unsignedp == inside_unsignedp))
1718 /* If we have a sign-extension of a zero-extended value, we can
1719 replace that by a single zero-extension. Likewise if the
1720 final conversion does not change precision we can drop the
1721 intermediate conversion. */
1722 (if (inside_int && inter_int && final_int
1723 && ((inside_prec < inter_prec && inter_prec < final_prec
1724 && inside_unsignedp && !inter_unsignedp)
1725 || final_prec == inter_prec))
1728 /* Two conversions in a row are not needed unless:
1729 - some conversion is floating-point (overstrict for now), or
1730 - some conversion is a vector (overstrict for now), or
1731 - the intermediate type is narrower than both initial and
1733 - the intermediate type and innermost type differ in signedness,
1734 and the outermost type is wider than the intermediate, or
1735 - the initial type is a pointer type and the precisions of the
1736 intermediate and final types differ, or
1737 - the final type is a pointer type and the precisions of the
1738 initial and intermediate types differ. */
1739 (if (! inside_float && ! inter_float && ! final_float
1740 && ! inside_vec && ! inter_vec && ! final_vec
1741 && (inter_prec >= inside_prec || inter_prec >= final_prec)
1742 && ! (inside_int && inter_int
1743 && inter_unsignedp != inside_unsignedp
1744 && inter_prec < final_prec)
1745 && ((inter_unsignedp && inter_prec > inside_prec)
1746 == (final_unsignedp && final_prec > inter_prec))
1747 && ! (inside_ptr && inter_prec != final_prec)
1748 && ! (final_ptr && inside_prec != inter_prec))
1751 /* A truncation to an unsigned type (a zero-extension) should be
1752 canonicalized as bitwise and of a mask. */
1753 (if (GIMPLE /* PR70366: doing this in GENERIC breaks -Wconversion. */
1754 && final_int && inter_int && inside_int
1755 && final_prec == inside_prec
1756 && final_prec > inter_prec
1758 (convert (bit_and @0 { wide_int_to_tree
1760 wi::mask (inter_prec, false,
1761 TYPE_PRECISION (inside_type))); })))
1763 /* If we are converting an integer to a floating-point that can
1764 represent it exactly and back to an integer, we can skip the
1765 floating-point conversion. */
1766 (if (GIMPLE /* PR66211 */
1767 && inside_int && inter_float && final_int &&
1768 (unsigned) significand_size (TYPE_MODE (inter_type))
1769 >= inside_prec - !inside_unsignedp)
1772 /* If we have a narrowing conversion to an integral type that is fed by a
1773 BIT_AND_EXPR, we might be able to remove the BIT_AND_EXPR if it merely
1774 masks off bits outside the final type (and nothing else). */
1776 (convert (bit_and @0 INTEGER_CST@1))
1777 (if (INTEGRAL_TYPE_P (type)
1778 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
1779 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))
1780 && operand_equal_p (@1, build_low_bits_mask (TREE_TYPE (@1),
1781 TYPE_PRECISION (type)), 0))
1785 /* (X /[ex] A) * A -> X. */
1787 (mult (convert1? (exact_div @0 @@1)) (convert2? @1))
1790 /* Canonicalization of binary operations. */
1792 /* Convert X + -C into X - C. */
1794 (plus @0 REAL_CST@1)
1795 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
1796 (with { tree tem = const_unop (NEGATE_EXPR, type, @1); }
1797 (if (!TREE_OVERFLOW (tem) || !flag_trapping_math)
1798 (minus @0 { tem; })))))
1800 /* Convert x+x into x*2. */
1803 (if (SCALAR_FLOAT_TYPE_P (type))
1804 (mult @0 { build_real (type, dconst2); })
1805 (if (INTEGRAL_TYPE_P (type))
1806 (mult @0 { build_int_cst (type, 2); }))))
1809 (minus integer_zerop @1)
1812 /* (ARG0 - ARG1) is the same as (-ARG1 + ARG0). So check whether
1813 ARG0 is zero and X + ARG0 reduces to X, since that would mean
1814 (-ARG1 + ARG0) reduces to -ARG1. */
1816 (minus real_zerop@0 @1)
1817 (if (fold_real_zero_addition_p (type, @0, 0))
1820 /* Transform x * -1 into -x. */
1822 (mult @0 integer_minus_onep)
1825 /* True if we can easily extract the real and imaginary parts of a complex
1827 (match compositional_complex
1828 (convert? (complex @0 @1)))
1830 /* COMPLEX_EXPR and REALPART/IMAGPART_EXPR cancellations. */
1832 (complex (realpart @0) (imagpart @0))
1835 (realpart (complex @0 @1))
1838 (imagpart (complex @0 @1))
1841 /* Sometimes we only care about half of a complex expression. */
1843 (realpart (convert?:s (conj:s @0)))
1844 (convert (realpart @0)))
1846 (imagpart (convert?:s (conj:s @0)))
1847 (convert (negate (imagpart @0))))
1848 (for part (realpart imagpart)
1849 (for op (plus minus)
1851 (part (convert?:s@2 (op:s @0 @1)))
1852 (convert (op (part @0) (part @1))))))
1854 (realpart (convert?:s (CEXPI:s @0)))
1857 (imagpart (convert?:s (CEXPI:s @0)))
1860 /* conj(conj(x)) -> x */
1862 (conj (convert? (conj @0)))
1863 (if (tree_nop_conversion_p (TREE_TYPE (@0), type))
1866 /* conj({x,y}) -> {x,-y} */
1868 (conj (convert?:s (complex:s @0 @1)))
1869 (with { tree itype = TREE_TYPE (type); }
1870 (complex (convert:itype @0) (negate (convert:itype @1)))))
1872 /* BSWAP simplifications, transforms checked by gcc.dg/builtin-bswap-8.c. */
1873 (for bswap (BUILT_IN_BSWAP16 BUILT_IN_BSWAP32 BUILT_IN_BSWAP64)
1878 (bswap (bit_not (bswap @0)))
1880 (for bitop (bit_xor bit_ior bit_and)
1882 (bswap (bitop:c (bswap @0) @1))
1883 (bitop @0 (bswap @1)))))
1886 /* Combine COND_EXPRs and VEC_COND_EXPRs. */
1888 /* Simplify constant conditions.
1889 Only optimize constant conditions when the selected branch
1890 has the same type as the COND_EXPR. This avoids optimizing
1891 away "c ? x : throw", where the throw has a void type.
1892 Note that we cannot throw away the fold-const.c variant nor
1893 this one as we depend on doing this transform before possibly
1894 A ? B : B -> B triggers and the fold-const.c one can optimize
1895 0 ? A : B to B even if A has side-effects. Something
1896 genmatch cannot handle. */
1898 (cond INTEGER_CST@0 @1 @2)
1899 (if (integer_zerop (@0))
1900 (if (!VOID_TYPE_P (TREE_TYPE (@2)) || VOID_TYPE_P (type))
1902 (if (!VOID_TYPE_P (TREE_TYPE (@1)) || VOID_TYPE_P (type))
1905 (vec_cond VECTOR_CST@0 @1 @2)
1906 (if (integer_all_onesp (@0))
1908 (if (integer_zerop (@0))
1911 (for cnd (cond vec_cond)
1912 /* A ? B : (A ? X : C) -> A ? B : C. */
1914 (cnd @0 (cnd @0 @1 @2) @3)
1917 (cnd @0 @1 (cnd @0 @2 @3))
1919 /* A ? B : (!A ? C : X) -> A ? B : C. */
1920 /* ??? This matches embedded conditions open-coded because genmatch
1921 would generate matching code for conditions in separate stmts only.
1922 The following is still important to merge then and else arm cases
1923 from if-conversion. */
1925 (cnd @0 @1 (cnd @2 @3 @4))
1926 (if (COMPARISON_CLASS_P (@0)
1927 && COMPARISON_CLASS_P (@2)
1928 && invert_tree_comparison
1929 (TREE_CODE (@0), HONOR_NANS (TREE_OPERAND (@0, 0))) == TREE_CODE (@2)
1930 && operand_equal_p (TREE_OPERAND (@0, 0), TREE_OPERAND (@2, 0), 0)
1931 && operand_equal_p (TREE_OPERAND (@0, 1), TREE_OPERAND (@2, 1), 0))
1934 (cnd @0 (cnd @1 @2 @3) @4)
1935 (if (COMPARISON_CLASS_P (@0)
1936 && COMPARISON_CLASS_P (@1)
1937 && invert_tree_comparison
1938 (TREE_CODE (@0), HONOR_NANS (TREE_OPERAND (@0, 0))) == TREE_CODE (@1)
1939 && operand_equal_p (TREE_OPERAND (@0, 0), TREE_OPERAND (@1, 0), 0)
1940 && operand_equal_p (TREE_OPERAND (@0, 1), TREE_OPERAND (@1, 1), 0))
1943 /* A ? B : B -> B. */
1948 /* !A ? B : C -> A ? C : B. */
1950 (cnd (logical_inverted_value truth_valued_p@0) @1 @2)
1953 /* A + (B vcmp C ? 1 : 0) -> A - (B vcmp C ? -1 : 0), since vector comparisons
1954 return all -1 or all 0 results. */
1955 /* ??? We could instead convert all instances of the vec_cond to negate,
1956 but that isn't necessarily a win on its own. */
1958 (plus:c @3 (view_convert? (vec_cond:s @0 integer_each_onep@1 integer_zerop@2)))
1959 (if (VECTOR_TYPE_P (type)
1960 && TYPE_VECTOR_SUBPARTS (type) == TYPE_VECTOR_SUBPARTS (TREE_TYPE (@1))
1961 && (TYPE_MODE (TREE_TYPE (type))
1962 == TYPE_MODE (TREE_TYPE (TREE_TYPE (@1)))))
1963 (minus @3 (view_convert (vec_cond @0 (negate @1) @2)))))
1965 /* ... likewise A - (B vcmp C ? 1 : 0) -> A + (B vcmp C ? -1 : 0). */
1967 (minus @3 (view_convert? (vec_cond:s @0 integer_each_onep@1 integer_zerop@2)))
1968 (if (VECTOR_TYPE_P (type)
1969 && TYPE_VECTOR_SUBPARTS (type) == TYPE_VECTOR_SUBPARTS (TREE_TYPE (@1))
1970 && (TYPE_MODE (TREE_TYPE (type))
1971 == TYPE_MODE (TREE_TYPE (TREE_TYPE (@1)))))
1972 (plus @3 (view_convert (vec_cond @0 (negate @1) @2)))))
1975 /* Simplifications of comparisons. */
1977 /* See if we can reduce the magnitude of a constant involved in a
1978 comparison by changing the comparison code. This is a canonicalization
1979 formerly done by maybe_canonicalize_comparison_1. */
1983 (cmp @0 INTEGER_CST@1)
1984 (if (tree_int_cst_sgn (@1) == -1)
1985 (acmp @0 { wide_int_to_tree (TREE_TYPE (@1), wi::add (@1, 1)); }))))
1989 (cmp @0 INTEGER_CST@1)
1990 (if (tree_int_cst_sgn (@1) == 1)
1991 (acmp @0 { wide_int_to_tree (TREE_TYPE (@1), wi::sub (@1, 1)); }))))
1994 /* We can simplify a logical negation of a comparison to the
1995 inverted comparison. As we cannot compute an expression
1996 operator using invert_tree_comparison we have to simulate
1997 that with expression code iteration. */
1998 (for cmp (tcc_comparison)
1999 icmp (inverted_tcc_comparison)
2000 ncmp (inverted_tcc_comparison_with_nans)
2001 /* Ideally we'd like to combine the following two patterns
2002 and handle some more cases by using
2003 (logical_inverted_value (cmp @0 @1))
2004 here but for that genmatch would need to "inline" that.
2005 For now implement what forward_propagate_comparison did. */
2007 (bit_not (cmp @0 @1))
2008 (if (VECTOR_TYPE_P (type)
2009 || (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1))
2010 /* Comparison inversion may be impossible for trapping math,
2011 invert_tree_comparison will tell us. But we can't use
2012 a computed operator in the replacement tree thus we have
2013 to play the trick below. */
2014 (with { enum tree_code ic = invert_tree_comparison
2015 (cmp, HONOR_NANS (@0)); }
2021 (bit_xor (cmp @0 @1) integer_truep)
2022 (with { enum tree_code ic = invert_tree_comparison
2023 (cmp, HONOR_NANS (@0)); }
2029 /* Transform comparisons of the form X - Y CMP 0 to X CMP Y.
2030 ??? The transformation is valid for the other operators if overflow
2031 is undefined for the type, but performing it here badly interacts
2032 with the transformation in fold_cond_expr_with_comparison which
2033 attempts to synthetize ABS_EXPR. */
2036 (cmp (minus@2 @0 @1) integer_zerop)
2037 (if (single_use (@2))
2040 /* Transform comparisons of the form X * C1 CMP 0 to X CMP 0 in the
2041 signed arithmetic case. That form is created by the compiler
2042 often enough for folding it to be of value. One example is in
2043 computing loop trip counts after Operator Strength Reduction. */
2044 (for cmp (simple_comparison)
2045 scmp (swapped_simple_comparison)
2047 (cmp (mult@3 @0 INTEGER_CST@1) integer_zerop@2)
2048 /* Handle unfolded multiplication by zero. */
2049 (if (integer_zerop (@1))
2051 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2052 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2054 /* If @1 is negative we swap the sense of the comparison. */
2055 (if (tree_int_cst_sgn (@1) < 0)
2059 /* Simplify comparison of something with itself. For IEEE
2060 floating-point, we can only do some of these simplifications. */
2064 (if (! FLOAT_TYPE_P (TREE_TYPE (@0))
2065 || ! HONOR_NANS (@0))
2066 { constant_boolean_node (true, type); }
2067 (if (cmp != EQ_EXPR)
2073 || ! FLOAT_TYPE_P (TREE_TYPE (@0))
2074 || ! HONOR_NANS (@0))
2075 { constant_boolean_node (false, type); })))
2076 (for cmp (unle unge uneq)
2079 { constant_boolean_node (true, type); }))
2080 (for cmp (unlt ungt)
2086 (if (!flag_trapping_math)
2087 { constant_boolean_node (false, type); }))
2089 /* Fold ~X op ~Y as Y op X. */
2090 (for cmp (simple_comparison)
2092 (cmp (bit_not@2 @0) (bit_not@3 @1))
2093 (if (single_use (@2) && single_use (@3))
2096 /* Fold ~X op C as X op' ~C, where op' is the swapped comparison. */
2097 (for cmp (simple_comparison)
2098 scmp (swapped_simple_comparison)
2100 (cmp (bit_not@2 @0) CONSTANT_CLASS_P@1)
2101 (if (single_use (@2)
2102 && (TREE_CODE (@1) == INTEGER_CST || TREE_CODE (@1) == VECTOR_CST))
2103 (scmp @0 (bit_not @1)))))
2105 (for cmp (simple_comparison)
2106 /* Fold (double)float1 CMP (double)float2 into float1 CMP float2. */
2108 (cmp (convert@2 @0) (convert? @1))
2109 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
2110 && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@2))
2111 == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0)))
2112 && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@2))
2113 == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1))))
2116 tree type1 = TREE_TYPE (@1);
2117 if (TREE_CODE (@1) == REAL_CST && !DECIMAL_FLOAT_TYPE_P (type1))
2119 REAL_VALUE_TYPE orig = TREE_REAL_CST (@1);
2120 if (TYPE_PRECISION (type1) > TYPE_PRECISION (float_type_node)
2121 && exact_real_truncate (TYPE_MODE (float_type_node), &orig))
2122 type1 = float_type_node;
2123 if (TYPE_PRECISION (type1) > TYPE_PRECISION (double_type_node)
2124 && exact_real_truncate (TYPE_MODE (double_type_node), &orig))
2125 type1 = double_type_node;
2128 = (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type1)
2129 ? TREE_TYPE (@0) : type1);
2131 (if (TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (newtype))
2132 (cmp (convert:newtype @0) (convert:newtype @1))))))
2136 /* IEEE doesn't distinguish +0 and -0 in comparisons. */
2138 /* a CMP (-0) -> a CMP 0 */
2139 (if (REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@1)))
2140 (cmp @0 { build_real (TREE_TYPE (@1), dconst0); }))
2141 /* x != NaN is always true, other ops are always false. */
2142 (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
2143 && ! HONOR_SNANS (@1))
2144 { constant_boolean_node (cmp == NE_EXPR, type); })
2145 /* Fold comparisons against infinity. */
2146 (if (REAL_VALUE_ISINF (TREE_REAL_CST (@1))
2147 && MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (@1))))
2150 REAL_VALUE_TYPE max;
2151 enum tree_code code = cmp;
2152 bool neg = REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1));
2154 code = swap_tree_comparison (code);
2157 /* x > +Inf is always false, if with ignore sNANs. */
2158 (if (code == GT_EXPR
2159 && ! HONOR_SNANS (@0))
2160 { constant_boolean_node (false, type); })
2161 (if (code == LE_EXPR)
2162 /* x <= +Inf is always true, if we don't case about NaNs. */
2163 (if (! HONOR_NANS (@0))
2164 { constant_boolean_node (true, type); }
2165 /* x <= +Inf is the same as x == x, i.e. !isnan(x). */
2167 /* x == +Inf and x >= +Inf are always equal to x > DBL_MAX. */
2168 (if (code == EQ_EXPR || code == GE_EXPR)
2169 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
2171 (lt @0 { build_real (TREE_TYPE (@0), max); })
2172 (gt @0 { build_real (TREE_TYPE (@0), max); }))))
2173 /* x < +Inf is always equal to x <= DBL_MAX. */
2174 (if (code == LT_EXPR)
2175 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
2177 (ge @0 { build_real (TREE_TYPE (@0), max); })
2178 (le @0 { build_real (TREE_TYPE (@0), max); }))))
2179 /* x != +Inf is always equal to !(x > DBL_MAX). */
2180 (if (code == NE_EXPR)
2181 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
2182 (if (! HONOR_NANS (@0))
2184 (ge @0 { build_real (TREE_TYPE (@0), max); })
2185 (le @0 { build_real (TREE_TYPE (@0), max); }))
2187 (bit_xor (lt @0 { build_real (TREE_TYPE (@0), max); })
2188 { build_one_cst (type); })
2189 (bit_xor (gt @0 { build_real (TREE_TYPE (@0), max); })
2190 { build_one_cst (type); }))))))))))
2192 /* If this is a comparison of a real constant with a PLUS_EXPR
2193 or a MINUS_EXPR of a real constant, we can convert it into a
2194 comparison with a revised real constant as long as no overflow
2195 occurs when unsafe_math_optimizations are enabled. */
2196 (if (flag_unsafe_math_optimizations)
2197 (for op (plus minus)
2199 (cmp (op @0 REAL_CST@1) REAL_CST@2)
2202 tree tem = const_binop (op == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR,
2203 TREE_TYPE (@1), @2, @1);
2205 (if (tem && !TREE_OVERFLOW (tem))
2206 (cmp @0 { tem; }))))))
2208 /* Likewise, we can simplify a comparison of a real constant with
2209 a MINUS_EXPR whose first operand is also a real constant, i.e.
2210 (c1 - x) < c2 becomes x > c1-c2. Reordering is allowed on
2211 floating-point types only if -fassociative-math is set. */
2212 (if (flag_associative_math)
2214 (cmp (minus REAL_CST@0 @1) REAL_CST@2)
2215 (with { tree tem = const_binop (MINUS_EXPR, TREE_TYPE (@1), @0, @2); }
2216 (if (tem && !TREE_OVERFLOW (tem))
2217 (cmp { tem; } @1)))))
2219 /* Fold comparisons against built-in math functions. */
2220 (if (flag_unsafe_math_optimizations
2221 && ! flag_errno_math)
2224 (cmp (sq @0) REAL_CST@1)
2226 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
2228 /* sqrt(x) < y is always false, if y is negative. */
2229 (if (cmp == EQ_EXPR || cmp == LT_EXPR || cmp == LE_EXPR)
2230 { constant_boolean_node (false, type); })
2231 /* sqrt(x) > y is always true, if y is negative and we
2232 don't care about NaNs, i.e. negative values of x. */
2233 (if (cmp == NE_EXPR || !HONOR_NANS (@0))
2234 { constant_boolean_node (true, type); })
2235 /* sqrt(x) > y is the same as x >= 0, if y is negative. */
2236 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })))
2237 (if (real_equal (TREE_REAL_CST_PTR (@1), &dconst0))
2239 /* sqrt(x) < 0 is always false. */
2240 (if (cmp == LT_EXPR)
2241 { constant_boolean_node (false, type); })
2242 /* sqrt(x) >= 0 is always true if we don't care about NaNs. */
2243 (if (cmp == GE_EXPR && !HONOR_NANS (@0))
2244 { constant_boolean_node (true, type); })
2245 /* sqrt(x) <= 0 -> x == 0. */
2246 (if (cmp == LE_EXPR)
2248 /* Otherwise sqrt(x) cmp 0 -> x cmp 0. Here cmp can be >=, >,
2249 == or !=. In the last case:
2251 (sqrt(x) != 0) == (NaN != 0) == true == (x != 0)
2253 if x is negative or NaN. Due to -funsafe-math-optimizations,
2254 the results for other x follow from natural arithmetic. */
2256 (if (cmp == GT_EXPR || cmp == GE_EXPR)
2260 real_arithmetic (&c2, MULT_EXPR,
2261 &TREE_REAL_CST (@1), &TREE_REAL_CST (@1));
2262 real_convert (&c2, TYPE_MODE (TREE_TYPE (@0)), &c2);
2264 (if (REAL_VALUE_ISINF (c2))
2265 /* sqrt(x) > y is x == +Inf, when y is very large. */
2266 (if (HONOR_INFINITIES (@0))
2267 (eq @0 { build_real (TREE_TYPE (@0), c2); })
2268 { constant_boolean_node (false, type); })
2269 /* sqrt(x) > c is the same as x > c*c. */
2270 (cmp @0 { build_real (TREE_TYPE (@0), c2); }))))
2271 (if (cmp == LT_EXPR || cmp == LE_EXPR)
2275 real_arithmetic (&c2, MULT_EXPR,
2276 &TREE_REAL_CST (@1), &TREE_REAL_CST (@1));
2277 real_convert (&c2, TYPE_MODE (TREE_TYPE (@0)), &c2);
2279 (if (REAL_VALUE_ISINF (c2))
2281 /* sqrt(x) < y is always true, when y is a very large
2282 value and we don't care about NaNs or Infinities. */
2283 (if (! HONOR_NANS (@0) && ! HONOR_INFINITIES (@0))
2284 { constant_boolean_node (true, type); })
2285 /* sqrt(x) < y is x != +Inf when y is very large and we
2286 don't care about NaNs. */
2287 (if (! HONOR_NANS (@0))
2288 (ne @0 { build_real (TREE_TYPE (@0), c2); }))
2289 /* sqrt(x) < y is x >= 0 when y is very large and we
2290 don't care about Infinities. */
2291 (if (! HONOR_INFINITIES (@0))
2292 (ge @0 { build_real (TREE_TYPE (@0), dconst0); }))
2293 /* sqrt(x) < y is x >= 0 && x != +Inf, when y is large. */
2296 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
2297 (ne @0 { build_real (TREE_TYPE (@0), c2); }))))
2298 /* sqrt(x) < c is the same as x < c*c, if we ignore NaNs. */
2299 (if (! HONOR_NANS (@0))
2300 (cmp @0 { build_real (TREE_TYPE (@0), c2); })
2301 /* sqrt(x) < c is the same as x >= 0 && x < c*c. */
2304 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
2305 (cmp @0 { build_real (TREE_TYPE (@0), c2); }))))))))))))
2307 /* Unordered tests if either argument is a NaN. */
2309 (bit_ior (unordered @0 @0) (unordered @1 @1))
2310 (if (types_match (@0, @1))
2313 (bit_and (ordered @0 @0) (ordered @1 @1))
2314 (if (types_match (@0, @1))
2317 (bit_ior:c (unordered @0 @0) (unordered:c@2 @0 @1))
2320 (bit_and:c (ordered @0 @0) (ordered:c@2 @0 @1))
2323 /* Simple range test simplifications. */
2324 /* A < B || A >= B -> true. */
2325 (for test1 (lt le le le ne ge)
2326 test2 (ge gt ge ne eq ne)
2328 (bit_ior:c (test1 @0 @1) (test2 @0 @1))
2329 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2330 || VECTOR_INTEGER_TYPE_P (TREE_TYPE (@0)))
2331 { constant_boolean_node (true, type); })))
2332 /* A < B && A >= B -> false. */
2333 (for test1 (lt lt lt le ne eq)
2334 test2 (ge gt eq gt eq gt)
2336 (bit_and:c (test1 @0 @1) (test2 @0 @1))
2337 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2338 || VECTOR_INTEGER_TYPE_P (TREE_TYPE (@0)))
2339 { constant_boolean_node (false, type); })))
2341 /* -A CMP -B -> B CMP A. */
2342 (for cmp (tcc_comparison)
2343 scmp (swapped_tcc_comparison)
2345 (cmp (negate @0) (negate @1))
2346 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
2347 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2348 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
2351 (cmp (negate @0) CONSTANT_CLASS_P@1)
2352 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
2353 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2354 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
2355 (with { tree tem = const_unop (NEGATE_EXPR, TREE_TYPE (@0), @1); }
2356 (if (tem && !TREE_OVERFLOW (tem))
2357 (scmp @0 { tem; }))))))
2359 /* Convert ABS_EXPR<x> == 0 or ABS_EXPR<x> != 0 to x == 0 or x != 0. */
2362 (op (abs @0) zerop@1)
2365 /* From fold_sign_changed_comparison and fold_widened_comparison. */
2366 (for cmp (simple_comparison)
2368 (cmp (convert@0 @00) (convert?@1 @10))
2369 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2370 /* Disable this optimization if we're casting a function pointer
2371 type on targets that require function pointer canonicalization. */
2372 && !(targetm.have_canonicalize_funcptr_for_compare ()
2373 && TREE_CODE (TREE_TYPE (@00)) == POINTER_TYPE
2374 && TREE_CODE (TREE_TYPE (TREE_TYPE (@00))) == FUNCTION_TYPE)
2376 (if (TYPE_PRECISION (TREE_TYPE (@00)) == TYPE_PRECISION (TREE_TYPE (@0))
2377 && (TREE_CODE (@10) == INTEGER_CST
2378 || (@1 != @10 && types_match (TREE_TYPE (@10), TREE_TYPE (@00))))
2379 && (TYPE_UNSIGNED (TREE_TYPE (@00)) == TYPE_UNSIGNED (TREE_TYPE (@0))
2382 && (POINTER_TYPE_P (TREE_TYPE (@00)) == POINTER_TYPE_P (TREE_TYPE (@0))))
2383 /* ??? The special-casing of INTEGER_CST conversion was in the original
2384 code and here to avoid a spurious overflow flag on the resulting
2385 constant which fold_convert produces. */
2386 (if (TREE_CODE (@1) == INTEGER_CST)
2387 (cmp @00 { force_fit_type (TREE_TYPE (@00), wi::to_widest (@1), 0,
2388 TREE_OVERFLOW (@1)); })
2389 (cmp @00 (convert @1)))
2391 (if (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (TREE_TYPE (@00)))
2392 /* If possible, express the comparison in the shorter mode. */
2393 (if ((cmp == EQ_EXPR || cmp == NE_EXPR
2394 || TYPE_UNSIGNED (TREE_TYPE (@0)) == TYPE_UNSIGNED (TREE_TYPE (@00))
2395 || (!TYPE_UNSIGNED (TREE_TYPE (@0))
2396 && TYPE_UNSIGNED (TREE_TYPE (@00))))
2397 && (types_match (TREE_TYPE (@10), TREE_TYPE (@00))
2398 || ((TYPE_PRECISION (TREE_TYPE (@00))
2399 >= TYPE_PRECISION (TREE_TYPE (@10)))
2400 && (TYPE_UNSIGNED (TREE_TYPE (@00))
2401 == TYPE_UNSIGNED (TREE_TYPE (@10))))
2402 || (TREE_CODE (@10) == INTEGER_CST
2403 && INTEGRAL_TYPE_P (TREE_TYPE (@00))
2404 && int_fits_type_p (@10, TREE_TYPE (@00)))))
2405 (cmp @00 (convert @10))
2406 (if (TREE_CODE (@10) == INTEGER_CST
2407 && INTEGRAL_TYPE_P (TREE_TYPE (@00))
2408 && !int_fits_type_p (@10, TREE_TYPE (@00)))
2411 tree min = lower_bound_in_type (TREE_TYPE (@10), TREE_TYPE (@00));
2412 tree max = upper_bound_in_type (TREE_TYPE (@10), TREE_TYPE (@00));
2413 bool above = integer_nonzerop (const_binop (LT_EXPR, type, max, @10));
2414 bool below = integer_nonzerop (const_binop (LT_EXPR, type, @10, min));
2416 (if (above || below)
2417 (if (cmp == EQ_EXPR || cmp == NE_EXPR)
2418 { constant_boolean_node (cmp == EQ_EXPR ? false : true, type); }
2419 (if (cmp == LT_EXPR || cmp == LE_EXPR)
2420 { constant_boolean_node (above ? true : false, type); }
2421 (if (cmp == GT_EXPR || cmp == GE_EXPR)
2422 { constant_boolean_node (above ? false : true, type); }))))))))))))
2425 /* A local variable can never be pointed to by
2426 the default SSA name of an incoming parameter.
2427 SSA names are canonicalized to 2nd place. */
2429 (cmp addr@0 SSA_NAME@1)
2430 (if (SSA_NAME_IS_DEFAULT_DEF (@1)
2431 && TREE_CODE (SSA_NAME_VAR (@1)) == PARM_DECL)
2432 (with { tree base = get_base_address (TREE_OPERAND (@0, 0)); }
2433 (if (TREE_CODE (base) == VAR_DECL
2434 && auto_var_in_fn_p (base, current_function_decl))
2435 (if (cmp == NE_EXPR)
2436 { constant_boolean_node (true, type); }
2437 { constant_boolean_node (false, type); }))))))
2439 /* Equality compare simplifications from fold_binary */
2442 /* If we have (A | C) == D where C & ~D != 0, convert this into 0.
2443 Similarly for NE_EXPR. */
2445 (cmp (convert?@3 (bit_ior @0 INTEGER_CST@1)) INTEGER_CST@2)
2446 (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0))
2447 && wi::bit_and_not (@1, @2) != 0)
2448 { constant_boolean_node (cmp == NE_EXPR, type); }))
2450 /* (X ^ Y) == 0 becomes X == Y, and (X ^ Y) != 0 becomes X != Y. */
2452 (cmp (bit_xor @0 @1) integer_zerop)
2455 /* (X ^ Y) == Y becomes X == 0.
2456 Likewise (X ^ Y) == X becomes Y == 0. */
2458 (cmp:c (bit_xor:c @0 @1) @0)
2459 (cmp @1 { build_zero_cst (TREE_TYPE (@1)); }))
2461 /* (X ^ C1) op C2 can be rewritten as X op (C1 ^ C2). */
2463 (cmp (convert?@3 (bit_xor @0 INTEGER_CST@1)) INTEGER_CST@2)
2464 (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0)))
2465 (cmp @0 (bit_xor @1 (convert @2)))))
2468 (cmp (convert? addr@0) integer_zerop)
2469 (if (tree_single_nonzero_warnv_p (@0, NULL))
2470 { constant_boolean_node (cmp == NE_EXPR, type); })))
2472 /* If we have (A & C) == C where C is a power of 2, convert this into
2473 (A & C) != 0. Similarly for NE_EXPR. */
2477 (cmp (bit_and@2 @0 integer_pow2p@1) @1)
2478 (icmp @2 { build_zero_cst (TREE_TYPE (@0)); })))
2480 /* If we have (A & C) != 0 where C is the sign bit of A, convert
2481 this into A < 0. Similarly for (A & C) == 0 into A >= 0. */
2485 (cmp (bit_and (convert?@2 @0) integer_pow2p@1) integer_zerop)
2486 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2487 && (TYPE_PRECISION (TREE_TYPE (@0))
2488 == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (@0))))
2489 && element_precision (@2) >= element_precision (@0)
2490 && wi::only_sign_bit_p (@1, element_precision (@0)))
2491 (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
2492 (ncmp (convert:stype @0) { build_zero_cst (stype); })))))
2494 /* When the addresses are not directly of decls compare base and offset.
2495 This implements some remaining parts of fold_comparison address
2496 comparisons but still no complete part of it. Still it is good
2497 enough to make fold_stmt not regress when not dispatching to fold_binary. */
2498 (for cmp (simple_comparison)
2500 (cmp (convert1?@2 addr@0) (convert2? addr@1))
2503 HOST_WIDE_INT off0, off1;
2504 tree base0 = get_addr_base_and_unit_offset (TREE_OPERAND (@0, 0), &off0);
2505 tree base1 = get_addr_base_and_unit_offset (TREE_OPERAND (@1, 0), &off1);
2506 if (base0 && TREE_CODE (base0) == MEM_REF)
2508 off0 += mem_ref_offset (base0).to_short_addr ();
2509 base0 = TREE_OPERAND (base0, 0);
2511 if (base1 && TREE_CODE (base1) == MEM_REF)
2513 off1 += mem_ref_offset (base1).to_short_addr ();
2514 base1 = TREE_OPERAND (base1, 0);
2517 (if (base0 && base1)
2521 if (decl_in_symtab_p (base0)
2522 && decl_in_symtab_p (base1))
2523 equal = symtab_node::get_create (base0)
2524 ->equal_address_to (symtab_node::get_create (base1));
2525 else if ((DECL_P (base0)
2526 || TREE_CODE (base0) == SSA_NAME
2527 || TREE_CODE (base0) == STRING_CST)
2529 || TREE_CODE (base1) == SSA_NAME
2530 || TREE_CODE (base1) == STRING_CST))
2531 equal = (base0 == base1);
2534 && (cmp == EQ_EXPR || cmp == NE_EXPR
2535 /* If the offsets are equal we can ignore overflow. */
2537 || POINTER_TYPE_OVERFLOW_UNDEFINED
2538 /* Or if we compare using pointers to decls or strings. */
2539 || (POINTER_TYPE_P (TREE_TYPE (@2))
2540 && (DECL_P (base0) || TREE_CODE (base0) == STRING_CST))))
2542 (if (cmp == EQ_EXPR)
2543 { constant_boolean_node (off0 == off1, type); })
2544 (if (cmp == NE_EXPR)
2545 { constant_boolean_node (off0 != off1, type); })
2546 (if (cmp == LT_EXPR)
2547 { constant_boolean_node (off0 < off1, type); })
2548 (if (cmp == LE_EXPR)
2549 { constant_boolean_node (off0 <= off1, type); })
2550 (if (cmp == GE_EXPR)
2551 { constant_boolean_node (off0 >= off1, type); })
2552 (if (cmp == GT_EXPR)
2553 { constant_boolean_node (off0 > off1, type); }))
2555 && DECL_P (base0) && DECL_P (base1)
2556 /* If we compare this as integers require equal offset. */
2557 && (!INTEGRAL_TYPE_P (TREE_TYPE (@2))
2560 (if (cmp == EQ_EXPR)
2561 { constant_boolean_node (false, type); })
2562 (if (cmp == NE_EXPR)
2563 { constant_boolean_node (true, type); })))))))))
2565 /* Simplify pointer equality compares using PTA. */
2569 (if (POINTER_TYPE_P (TREE_TYPE (@0))
2570 && ptrs_compare_unequal (@0, @1))
2571 { neeq == EQ_EXPR ? boolean_false_node : boolean_true_node; })))
2573 /* PR70920: Transform (intptr_t)x eq/ne CST to x eq/ne (typeof x) CST.
2574 and (typeof ptr_cst) x eq/ne ptr_cst to x eq/ne (typeof x) CST.
2575 Disable the transform if either operand is pointer to function.
2576 This broke pr22051-2.c for arm where function pointer
2577 canonicalizaion is not wanted. */
2581 (cmp (convert @0) INTEGER_CST@1)
2582 (if ((POINTER_TYPE_P (TREE_TYPE (@0)) && !FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@0)))
2583 && INTEGRAL_TYPE_P (TREE_TYPE (@1)))
2584 || (INTEGRAL_TYPE_P (TREE_TYPE (@0)) && POINTER_TYPE_P (TREE_TYPE (@1))
2585 && !FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@1)))))
2586 (cmp @0 (convert @1)))))
2588 /* Non-equality compare simplifications from fold_binary */
2589 (for cmp (lt gt le ge)
2590 /* Comparisons with the highest or lowest possible integer of
2591 the specified precision will have known values. */
2593 (cmp (convert?@2 @0) INTEGER_CST@1)
2594 (if ((INTEGRAL_TYPE_P (TREE_TYPE (@1)) || POINTER_TYPE_P (TREE_TYPE (@1)))
2595 && tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@0)))
2598 tree arg1_type = TREE_TYPE (@1);
2599 unsigned int prec = TYPE_PRECISION (arg1_type);
2600 wide_int max = wi::max_value (arg1_type);
2601 wide_int signed_max = wi::max_value (prec, SIGNED);
2602 wide_int min = wi::min_value (arg1_type);
2605 (if (wi::eq_p (@1, max))
2607 (if (cmp == GT_EXPR)
2608 { constant_boolean_node (false, type); })
2609 (if (cmp == GE_EXPR)
2611 (if (cmp == LE_EXPR)
2612 { constant_boolean_node (true, type); })
2613 (if (cmp == LT_EXPR)
2615 (if (wi::eq_p (@1, min))
2617 (if (cmp == LT_EXPR)
2618 { constant_boolean_node (false, type); })
2619 (if (cmp == LE_EXPR)
2621 (if (cmp == GE_EXPR)
2622 { constant_boolean_node (true, type); })
2623 (if (cmp == GT_EXPR)
2625 (if (wi::eq_p (@1, max - 1))
2627 (if (cmp == GT_EXPR)
2628 (eq @2 { wide_int_to_tree (TREE_TYPE (@1), wi::add (@1, 1)); }))
2629 (if (cmp == LE_EXPR)
2630 (ne @2 { wide_int_to_tree (TREE_TYPE (@1), wi::add (@1, 1)); }))))
2631 (if (wi::eq_p (@1, min + 1))
2633 (if (cmp == GE_EXPR)
2634 (ne @2 { wide_int_to_tree (TREE_TYPE (@1), wi::sub (@1, 1)); }))
2635 (if (cmp == LT_EXPR)
2636 (eq @2 { wide_int_to_tree (TREE_TYPE (@1), wi::sub (@1, 1)); }))))
2637 (if (wi::eq_p (@1, signed_max)
2638 && TYPE_UNSIGNED (arg1_type)
2639 /* We will flip the signedness of the comparison operator
2640 associated with the mode of @1, so the sign bit is
2641 specified by this mode. Check that @1 is the signed
2642 max associated with this sign bit. */
2643 && prec == GET_MODE_PRECISION (TYPE_MODE (arg1_type))
2644 /* signed_type does not work on pointer types. */
2645 && INTEGRAL_TYPE_P (arg1_type))
2646 /* The following case also applies to X < signed_max+1
2647 and X >= signed_max+1 because previous transformations. */
2648 (if (cmp == LE_EXPR || cmp == GT_EXPR)
2649 (with { tree st = signed_type_for (arg1_type); }
2650 (if (cmp == LE_EXPR)
2651 (ge (convert:st @0) { build_zero_cst (st); })
2652 (lt (convert:st @0) { build_zero_cst (st); }))))))))))
2654 (for cmp (unordered ordered unlt unle ungt unge uneq ltgt)
2655 /* If the second operand is NaN, the result is constant. */
2658 (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
2659 && (cmp != LTGT_EXPR || ! flag_trapping_math))
2660 { constant_boolean_node (cmp == ORDERED_EXPR || cmp == LTGT_EXPR
2661 ? false : true, type); })))
2663 /* bool_var != 0 becomes bool_var. */
2665 (ne @0 integer_zerop)
2666 (if (TREE_CODE (TREE_TYPE (@0)) == BOOLEAN_TYPE
2667 && types_match (type, TREE_TYPE (@0)))
2669 /* bool_var == 1 becomes bool_var. */
2671 (eq @0 integer_onep)
2672 (if (TREE_CODE (TREE_TYPE (@0)) == BOOLEAN_TYPE
2673 && types_match (type, TREE_TYPE (@0)))
2676 bool_var == 0 becomes !bool_var or
2677 bool_var != 1 becomes !bool_var
2678 here because that only is good in assignment context as long
2679 as we require a tcc_comparison in GIMPLE_CONDs where we'd
2680 replace if (x == 0) with tem = ~x; if (tem != 0) which is
2681 clearly less optimal and which we'll transform again in forwprop. */
2683 /* When one argument is a constant, overflow detection can be simplified.
2684 Currently restricted to single use so as not to interfere too much with
2685 ADD_OVERFLOW detection in tree-ssa-math-opts.c.
2686 A + CST CMP A -> A CMP' CST' */
2687 (for cmp (lt le ge gt)
2690 (cmp:c (plus@2 @0 INTEGER_CST@1) @0)
2691 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
2692 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))
2695 (out @0 { wide_int_to_tree (TREE_TYPE (@0), wi::max_value
2696 (TYPE_PRECISION (TREE_TYPE (@0)), UNSIGNED) - @1); }))))
2698 /* To detect overflow in unsigned A - B, A < B is simpler than A - B > A.
2699 However, the detection logic for SUB_OVERFLOW in tree-ssa-math-opts.c
2700 expects the long form, so we restrict the transformation for now. */
2703 (cmp:c (minus@2 @0 @1) @0)
2704 (if (single_use (@2)
2705 && ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2706 && TYPE_UNSIGNED (TREE_TYPE (@0))
2707 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
2710 /* Testing for overflow is unnecessary if we already know the result. */
2715 (cmp:c (realpart (IFN_SUB_OVERFLOW@2 @0 @1)) @0)
2716 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
2717 && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
2718 (out (imagpart @2) { build_zero_cst (TREE_TYPE (@0)); }))))
2723 (cmp:c (realpart (IFN_ADD_OVERFLOW:c@2 @0 @1)) @0)
2724 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
2725 && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
2726 (out (imagpart @2) { build_zero_cst (TREE_TYPE (@0)); }))))
2728 /* For unsigned operands, -1 / B < A checks whether A * B would overflow.
2729 Simplify it to __builtin_mul_overflow (A, B, <unused>). */
2733 (cmp:c (trunc_div:s integer_all_onesp @1) @0)
2734 (if (TYPE_UNSIGNED (TREE_TYPE (@0)) && !VECTOR_TYPE_P (TREE_TYPE (@0)))
2735 (with { tree t = TREE_TYPE (@0), cpx = build_complex_type (t); }
2736 (out (imagpart (IFN_MUL_OVERFLOW:cpx @0 @1)) { build_zero_cst (t); })))))
2738 /* Simplification of math builtins. These rules must all be optimizations
2739 as well as IL simplifications. If there is a possibility that the new
2740 form could be a pessimization, the rule should go in the canonicalization
2741 section that follows this one.
2743 Rules can generally go in this section if they satisfy one of
2746 - the rule describes an identity
2748 - the rule replaces calls with something as simple as addition or
2751 - the rule contains unary calls only and simplifies the surrounding
2752 arithmetic. (The idea here is to exclude non-unary calls in which
2753 one operand is constant and in which the call is known to be cheap
2754 when the operand has that value.) */
2756 (if (flag_unsafe_math_optimizations)
2757 /* Simplify sqrt(x) * sqrt(x) -> x. */
2759 (mult (SQRT@1 @0) @1)
2760 (if (!HONOR_SNANS (type))
2763 /* Simplify sqrt(x) * sqrt(y) -> sqrt(x*y). */
2764 (for root (SQRT CBRT)
2766 (mult (root:s @0) (root:s @1))
2767 (root (mult @0 @1))))
2769 /* Simplify expN(x) * expN(y) -> expN(x+y). */
2770 (for exps (EXP EXP2 EXP10 POW10)
2772 (mult (exps:s @0) (exps:s @1))
2773 (exps (plus @0 @1))))
2775 /* Simplify a/root(b/c) into a*root(c/b). */
2776 (for root (SQRT CBRT)
2778 (rdiv @0 (root:s (rdiv:s @1 @2)))
2779 (mult @0 (root (rdiv @2 @1)))))
2781 /* Simplify x/expN(y) into x*expN(-y). */
2782 (for exps (EXP EXP2 EXP10 POW10)
2784 (rdiv @0 (exps:s @1))
2785 (mult @0 (exps (negate @1)))))
2787 (for logs (LOG LOG2 LOG10 LOG10)
2788 exps (EXP EXP2 EXP10 POW10)
2789 /* logN(expN(x)) -> x. */
2793 /* expN(logN(x)) -> x. */
2798 /* Optimize logN(func()) for various exponential functions. We
2799 want to determine the value "x" and the power "exponent" in
2800 order to transform logN(x**exponent) into exponent*logN(x). */
2801 (for logs (LOG LOG LOG LOG2 LOG2 LOG2 LOG10 LOG10)
2802 exps (EXP2 EXP10 POW10 EXP EXP10 POW10 EXP EXP2)
2805 (if (SCALAR_FLOAT_TYPE_P (type))
2811 /* Prepare to do logN(exp(exponent)) -> exponent*logN(e). */
2812 x = build_real_truncate (type, dconst_e ());
2815 /* Prepare to do logN(exp2(exponent)) -> exponent*logN(2). */
2816 x = build_real (type, dconst2);
2820 /* Prepare to do logN(exp10(exponent)) -> exponent*logN(10). */
2822 REAL_VALUE_TYPE dconst10;
2823 real_from_integer (&dconst10, VOIDmode, 10, SIGNED);
2824 x = build_real (type, dconst10);
2831 (mult (logs { x; }) @0)))))
2839 (if (SCALAR_FLOAT_TYPE_P (type))
2845 /* Prepare to do logN(sqrt(x)) -> 0.5*logN(x). */
2846 x = build_real (type, dconsthalf);
2849 /* Prepare to do logN(cbrt(x)) -> (1/3)*logN(x). */
2850 x = build_real_truncate (type, dconst_third ());
2856 (mult { x; } (logs @0))))))
2858 /* logN(pow(x,exponent)) -> exponent*logN(x). */
2859 (for logs (LOG LOG2 LOG10)
2863 (mult @1 (logs @0))))
2868 exps (EXP EXP2 EXP10 POW10)
2869 /* sqrt(expN(x)) -> expN(x*0.5). */
2872 (exps (mult @0 { build_real (type, dconsthalf); })))
2873 /* cbrt(expN(x)) -> expN(x/3). */
2876 (exps (mult @0 { build_real_truncate (type, dconst_third ()); })))
2877 /* pow(expN(x), y) -> expN(x*y). */
2880 (exps (mult @0 @1))))
2882 /* tan(atan(x)) -> x. */
2889 /* cabs(x+0i) or cabs(0+xi) -> abs(x). */
2891 (CABS (complex:C @0 real_zerop@1))
2894 /* trunc(trunc(x)) -> trunc(x), etc. */
2895 (for fns (TRUNC FLOOR CEIL ROUND NEARBYINT RINT)
2899 /* f(x) -> x if x is integer valued and f does nothing for such values. */
2900 (for fns (TRUNC FLOOR CEIL ROUND NEARBYINT RINT)
2902 (fns integer_valued_real_p@0)
2905 /* hypot(x,0) and hypot(0,x) -> abs(x). */
2907 (HYPOT:c @0 real_zerop@1)
2910 /* pow(1,x) -> 1. */
2912 (POW real_onep@0 @1)
2916 /* copysign(x,x) -> x. */
2921 /* copysign(x,y) -> fabs(x) if y is nonnegative. */
2922 (COPYSIGN @0 tree_expr_nonnegative_p@1)
2925 (for scale (LDEXP SCALBN SCALBLN)
2926 /* ldexp(0, x) -> 0. */
2928 (scale real_zerop@0 @1)
2930 /* ldexp(x, 0) -> x. */
2932 (scale @0 integer_zerop@1)
2934 /* ldexp(x, y) -> x if x is +-Inf or NaN. */
2936 (scale REAL_CST@0 @1)
2937 (if (!real_isfinite (TREE_REAL_CST_PTR (@0)))
2940 /* Canonicalization of sequences of math builtins. These rules represent
2941 IL simplifications but are not necessarily optimizations.
2943 The sincos pass is responsible for picking "optimal" implementations
2944 of math builtins, which may be more complicated and can sometimes go
2945 the other way, e.g. converting pow into a sequence of sqrts.
2946 We only want to do these canonicalizations before the pass has run. */
2948 (if (flag_unsafe_math_optimizations && canonicalize_math_p ())
2949 /* Simplify tan(x) * cos(x) -> sin(x). */
2951 (mult:c (TAN:s @0) (COS:s @0))
2954 /* Simplify x * pow(x,c) -> pow(x,c+1). */
2956 (mult:c @0 (POW:s @0 REAL_CST@1))
2957 (if (!TREE_OVERFLOW (@1))
2958 (POW @0 (plus @1 { build_one_cst (type); }))))
2960 /* Simplify sin(x) / cos(x) -> tan(x). */
2962 (rdiv (SIN:s @0) (COS:s @0))
2965 /* Simplify cos(x) / sin(x) -> 1 / tan(x). */
2967 (rdiv (COS:s @0) (SIN:s @0))
2968 (rdiv { build_one_cst (type); } (TAN @0)))
2970 /* Simplify sin(x) / tan(x) -> cos(x). */
2972 (rdiv (SIN:s @0) (TAN:s @0))
2973 (if (! HONOR_NANS (@0)
2974 && ! HONOR_INFINITIES (@0))
2977 /* Simplify tan(x) / sin(x) -> 1.0 / cos(x). */
2979 (rdiv (TAN:s @0) (SIN:s @0))
2980 (if (! HONOR_NANS (@0)
2981 && ! HONOR_INFINITIES (@0))
2982 (rdiv { build_one_cst (type); } (COS @0))))
2984 /* Simplify pow(x,y) * pow(x,z) -> pow(x,y+z). */
2986 (mult (POW:s @0 @1) (POW:s @0 @2))
2987 (POW @0 (plus @1 @2)))
2989 /* Simplify pow(x,y) * pow(z,y) -> pow(x*z,y). */
2991 (mult (POW:s @0 @1) (POW:s @2 @1))
2992 (POW (mult @0 @2) @1))
2994 /* Simplify powi(x,y) * powi(z,y) -> powi(x*z,y). */
2996 (mult (POWI:s @0 @1) (POWI:s @2 @1))
2997 (POWI (mult @0 @2) @1))
2999 /* Simplify pow(x,c) / x -> pow(x,c-1). */
3001 (rdiv (POW:s @0 REAL_CST@1) @0)
3002 (if (!TREE_OVERFLOW (@1))
3003 (POW @0 (minus @1 { build_one_cst (type); }))))
3005 /* Simplify x / pow (y,z) -> x * pow(y,-z). */
3007 (rdiv @0 (POW:s @1 @2))
3008 (mult @0 (POW @1 (negate @2))))
3013 /* sqrt(sqrt(x)) -> pow(x,1/4). */
3016 (pows @0 { build_real (type, dconst_quarter ()); }))
3017 /* sqrt(cbrt(x)) -> pow(x,1/6). */
3020 (pows @0 { build_real_truncate (type, dconst_sixth ()); }))
3021 /* cbrt(sqrt(x)) -> pow(x,1/6). */
3024 (pows @0 { build_real_truncate (type, dconst_sixth ()); }))
3025 /* cbrt(cbrt(x)) -> pow(x,1/9), iff x is nonnegative. */
3027 (cbrts (cbrts tree_expr_nonnegative_p@0))
3028 (pows @0 { build_real_truncate (type, dconst_ninth ()); }))
3029 /* sqrt(pow(x,y)) -> pow(|x|,y*0.5). */
3031 (sqrts (pows @0 @1))
3032 (pows (abs @0) (mult @1 { build_real (type, dconsthalf); })))
3033 /* cbrt(pow(x,y)) -> pow(x,y/3), iff x is nonnegative. */
3035 (cbrts (pows tree_expr_nonnegative_p@0 @1))
3036 (pows @0 (mult @1 { build_real_truncate (type, dconst_third ()); })))
3037 /* pow(sqrt(x),y) -> pow(x,y*0.5). */
3039 (pows (sqrts @0) @1)
3040 (pows @0 (mult @1 { build_real (type, dconsthalf); })))
3041 /* pow(cbrt(x),y) -> pow(x,y/3) iff x is nonnegative. */
3043 (pows (cbrts tree_expr_nonnegative_p@0) @1)
3044 (pows @0 (mult @1 { build_real_truncate (type, dconst_third ()); })))
3045 /* pow(pow(x,y),z) -> pow(x,y*z) iff x is nonnegative. */
3047 (pows (pows tree_expr_nonnegative_p@0 @1) @2)
3048 (pows @0 (mult @1 @2))))
3050 /* cabs(x+xi) -> fabs(x)*sqrt(2). */
3052 (CABS (complex @0 @0))
3053 (mult (abs @0) { build_real_truncate (type, dconst_sqrt2 ()); }))
3055 /* hypot(x,x) -> fabs(x)*sqrt(2). */
3058 (mult (abs @0) { build_real_truncate (type, dconst_sqrt2 ()); }))
3060 /* cexp(x+yi) -> exp(x)*cexpi(y). */
3065 (cexps compositional_complex@0)
3066 (if (targetm.libc_has_function (function_c99_math_complex))
3068 (mult (exps@1 (realpart @0)) (realpart (cexpis:type@2 (imagpart @0))))
3069 (mult @1 (imagpart @2)))))))
3071 (if (canonicalize_math_p ())
3072 /* floor(x) -> trunc(x) if x is nonnegative. */
3076 (floors tree_expr_nonnegative_p@0)
3079 (match double_value_p
3081 (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == double_type_node)))
3082 (for froms (BUILT_IN_TRUNCL
3094 /* truncl(extend(x)) -> extend(trunc(x)), etc., if x is a double. */
3095 (if (optimize && canonicalize_math_p ())
3097 (froms (convert double_value_p@0))
3098 (convert (tos @0)))))
3100 (match float_value_p
3102 (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == float_type_node)))
3103 (for froms (BUILT_IN_TRUNCL BUILT_IN_TRUNC
3104 BUILT_IN_FLOORL BUILT_IN_FLOOR
3105 BUILT_IN_CEILL BUILT_IN_CEIL
3106 BUILT_IN_ROUNDL BUILT_IN_ROUND
3107 BUILT_IN_NEARBYINTL BUILT_IN_NEARBYINT
3108 BUILT_IN_RINTL BUILT_IN_RINT)
3109 tos (BUILT_IN_TRUNCF BUILT_IN_TRUNCF
3110 BUILT_IN_FLOORF BUILT_IN_FLOORF
3111 BUILT_IN_CEILF BUILT_IN_CEILF
3112 BUILT_IN_ROUNDF BUILT_IN_ROUNDF
3113 BUILT_IN_NEARBYINTF BUILT_IN_NEARBYINTF
3114 BUILT_IN_RINTF BUILT_IN_RINTF)
3115 /* truncl(extend(x)) and trunc(extend(x)) -> extend(truncf(x)), etc.,
3117 (if (optimize && canonicalize_math_p ()
3118 && targetm.libc_has_function (function_c99_misc))
3120 (froms (convert float_value_p@0))
3121 (convert (tos @0)))))
3123 (for froms (XFLOORL XCEILL XROUNDL XRINTL)
3124 tos (XFLOOR XCEIL XROUND XRINT)
3125 /* llfloorl(extend(x)) -> llfloor(x), etc., if x is a double. */
3126 (if (optimize && canonicalize_math_p ())
3128 (froms (convert double_value_p@0))
3131 (for froms (XFLOORL XCEILL XROUNDL XRINTL
3132 XFLOOR XCEIL XROUND XRINT)
3133 tos (XFLOORF XCEILF XROUNDF XRINTF)
3134 /* llfloorl(extend(x)) and llfloor(extend(x)) -> llfloorf(x), etc.,
3136 (if (optimize && canonicalize_math_p ())
3138 (froms (convert float_value_p@0))
3141 (if (canonicalize_math_p ())
3142 /* xfloor(x) -> fix_trunc(x) if x is nonnegative. */
3143 (for floors (IFLOOR LFLOOR LLFLOOR)
3145 (floors tree_expr_nonnegative_p@0)
3148 (if (canonicalize_math_p ())
3149 /* xfloor(x) -> fix_trunc(x), etc., if x is integer valued. */
3150 (for fns (IFLOOR LFLOOR LLFLOOR
3152 IROUND LROUND LLROUND)
3154 (fns integer_valued_real_p@0)
3156 (if (!flag_errno_math)
3157 /* xrint(x) -> fix_trunc(x), etc., if x is integer valued. */
3158 (for rints (IRINT LRINT LLRINT)
3160 (rints integer_valued_real_p@0)
3163 (if (canonicalize_math_p ())
3164 (for ifn (IFLOOR ICEIL IROUND IRINT)
3165 lfn (LFLOOR LCEIL LROUND LRINT)
3166 llfn (LLFLOOR LLCEIL LLROUND LLRINT)
3167 /* Canonicalize iround (x) to lround (x) on ILP32 targets where
3168 sizeof (int) == sizeof (long). */
3169 (if (TYPE_PRECISION (integer_type_node)
3170 == TYPE_PRECISION (long_integer_type_node))
3173 (lfn:long_integer_type_node @0)))
3174 /* Canonicalize llround (x) to lround (x) on LP64 targets where
3175 sizeof (long long) == sizeof (long). */
3176 (if (TYPE_PRECISION (long_long_integer_type_node)
3177 == TYPE_PRECISION (long_integer_type_node))
3180 (lfn:long_integer_type_node @0)))))
3182 /* cproj(x) -> x if we're ignoring infinities. */
3185 (if (!HONOR_INFINITIES (type))
3188 /* If the real part is inf and the imag part is known to be
3189 nonnegative, return (inf + 0i). */
3191 (CPROJ (complex REAL_CST@0 tree_expr_nonnegative_p@1))
3192 (if (real_isinf (TREE_REAL_CST_PTR (@0)))
3193 { build_complex_inf (type, false); }))
3195 /* If the imag part is inf, return (inf+I*copysign(0,imag)). */
3197 (CPROJ (complex @0 REAL_CST@1))
3198 (if (real_isinf (TREE_REAL_CST_PTR (@1)))
3199 { build_complex_inf (type, TREE_REAL_CST_PTR (@1)->sign); }))
3205 (pows @0 REAL_CST@1)
3207 const REAL_VALUE_TYPE *value = TREE_REAL_CST_PTR (@1);
3208 REAL_VALUE_TYPE tmp;
3211 /* pow(x,0) -> 1. */
3212 (if (real_equal (value, &dconst0))
3213 { build_real (type, dconst1); })
3214 /* pow(x,1) -> x. */
3215 (if (real_equal (value, &dconst1))
3217 /* pow(x,-1) -> 1/x. */
3218 (if (real_equal (value, &dconstm1))
3219 (rdiv { build_real (type, dconst1); } @0))
3220 /* pow(x,0.5) -> sqrt(x). */
3221 (if (flag_unsafe_math_optimizations
3222 && canonicalize_math_p ()
3223 && real_equal (value, &dconsthalf))
3225 /* pow(x,1/3) -> cbrt(x). */
3226 (if (flag_unsafe_math_optimizations
3227 && canonicalize_math_p ()
3228 && (tmp = real_value_truncate (TYPE_MODE (type), dconst_third ()),
3229 real_equal (value, &tmp)))
3232 /* powi(1,x) -> 1. */
3234 (POWI real_onep@0 @1)
3238 (POWI @0 INTEGER_CST@1)
3240 /* powi(x,0) -> 1. */
3241 (if (wi::eq_p (@1, 0))
3242 { build_real (type, dconst1); })
3243 /* powi(x,1) -> x. */
3244 (if (wi::eq_p (@1, 1))
3246 /* powi(x,-1) -> 1/x. */
3247 (if (wi::eq_p (@1, -1))
3248 (rdiv { build_real (type, dconst1); } @0))))
3250 /* Narrowing of arithmetic and logical operations.
3252 These are conceptually similar to the transformations performed for
3253 the C/C++ front-ends by shorten_binary_op and shorten_compare. Long
3254 term we want to move all that code out of the front-ends into here. */
3256 /* If we have a narrowing conversion of an arithmetic operation where
3257 both operands are widening conversions from the same type as the outer
3258 narrowing conversion. Then convert the innermost operands to a suitable
3259 unsigned type (to avoid introducing undefined behavior), perform the
3260 operation and convert the result to the desired type. */
3261 (for op (plus minus)
3263 (convert (op:s (convert@2 @0) (convert@3 @1)))
3264 (if (INTEGRAL_TYPE_P (type)
3265 /* We check for type compatibility between @0 and @1 below,
3266 so there's no need to check that @1/@3 are integral types. */
3267 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
3268 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
3269 /* The precision of the type of each operand must match the
3270 precision of the mode of each operand, similarly for the
3272 && (TYPE_PRECISION (TREE_TYPE (@0))
3273 == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (@0))))
3274 && (TYPE_PRECISION (TREE_TYPE (@1))
3275 == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (@1))))
3276 && TYPE_PRECISION (type) == GET_MODE_PRECISION (TYPE_MODE (type))
3277 /* The inner conversion must be a widening conversion. */
3278 && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
3279 && types_match (@0, @1)
3280 && types_match (@0, type))
3281 (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
3282 (convert (op @0 @1))
3283 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
3284 (convert (op (convert:utype @0) (convert:utype @1))))))))
3286 /* This is another case of narrowing, specifically when there's an outer
3287 BIT_AND_EXPR which masks off bits outside the type of the innermost
3288 operands. Like the previous case we have to convert the operands
3289 to unsigned types to avoid introducing undefined behavior for the
3290 arithmetic operation. */
3291 (for op (minus plus)
3293 (bit_and (op:s (convert@2 @0) (convert@3 @1)) INTEGER_CST@4)
3294 (if (INTEGRAL_TYPE_P (type)
3295 /* We check for type compatibility between @0 and @1 below,
3296 so there's no need to check that @1/@3 are integral types. */
3297 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
3298 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
3299 /* The precision of the type of each operand must match the
3300 precision of the mode of each operand, similarly for the
3302 && (TYPE_PRECISION (TREE_TYPE (@0))
3303 == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (@0))))
3304 && (TYPE_PRECISION (TREE_TYPE (@1))
3305 == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (@1))))
3306 && TYPE_PRECISION (type) == GET_MODE_PRECISION (TYPE_MODE (type))
3307 /* The inner conversion must be a widening conversion. */
3308 && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
3309 && types_match (@0, @1)
3310 && (tree_int_cst_min_precision (@4, TYPE_SIGN (TREE_TYPE (@0)))
3311 <= TYPE_PRECISION (TREE_TYPE (@0)))
3312 && (wi::bit_and (@4, wi::mask (TYPE_PRECISION (TREE_TYPE (@0)),
3313 true, TYPE_PRECISION (type))) == 0))
3314 (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
3315 (with { tree ntype = TREE_TYPE (@0); }
3316 (convert (bit_and (op @0 @1) (convert:ntype @4))))
3317 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
3318 (convert (bit_and (op (convert:utype @0) (convert:utype @1))
3319 (convert:utype @4))))))))
3321 /* Transform (@0 < @1 and @0 < @2) to use min,
3322 (@0 > @1 and @0 > @2) to use max */
3323 (for op (lt le gt ge)
3324 ext (min min max max)
3326 (bit_and (op:cs @0 @1) (op:cs @0 @2))
3327 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
3328 && TREE_CODE (@0) != INTEGER_CST)
3329 (op @0 (ext @1 @2)))))
3332 /* signbit(x) -> 0 if x is nonnegative. */
3333 (SIGNBIT tree_expr_nonnegative_p@0)
3334 { integer_zero_node; })
3337 /* signbit(x) -> x<0 if x doesn't have signed zeros. */
3339 (if (!HONOR_SIGNED_ZEROS (@0))
3340 (convert (lt @0 { build_real (TREE_TYPE (@0), dconst0); }))))
3342 /* Transform comparisons of the form X +- C1 CMP C2 to X CMP C2 -+ C1. */
3344 (for op (plus minus)
3347 (cmp (op@3 @0 INTEGER_CST@1) INTEGER_CST@2)
3348 (if (!TREE_OVERFLOW (@1) && !TREE_OVERFLOW (@2)
3349 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0))
3350 && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@0))
3351 && !TYPE_SATURATING (TREE_TYPE (@0)))
3352 (with { tree res = int_const_binop (rop, @2, @1); }
3353 (if (TREE_OVERFLOW (res))
3354 { constant_boolean_node (cmp == NE_EXPR, type); }
3355 (if (single_use (@3))
3356 (cmp @0 { res; }))))))))
3357 (for cmp (lt le gt ge)
3358 (for op (plus minus)
3361 (cmp (op@3 @0 INTEGER_CST@1) INTEGER_CST@2)
3362 (if (!TREE_OVERFLOW (@1) && !TREE_OVERFLOW (@2)
3363 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
3364 (with { tree res = int_const_binop (rop, @2, @1); }
3365 (if (TREE_OVERFLOW (res))
3367 fold_overflow_warning (("assuming signed overflow does not occur "
3368 "when simplifying conditional to constant"),
3369 WARN_STRICT_OVERFLOW_CONDITIONAL);
3370 bool less = cmp == LE_EXPR || cmp == LT_EXPR;
3371 /* wi::ges_p (@2, 0) should be sufficient for a signed type. */
3372 bool ovf_high = wi::lt_p (@1, 0, TYPE_SIGN (TREE_TYPE (@1)))
3373 != (op == MINUS_EXPR);
3374 constant_boolean_node (less == ovf_high, type);
3376 (if (single_use (@3))
3379 fold_overflow_warning (("assuming signed overflow does not occur "
3380 "when changing X +- C1 cmp C2 to "
3382 WARN_STRICT_OVERFLOW_COMPARISON);
3384 (cmp @0 { res; })))))))))
3386 /* Canonicalizations of BIT_FIELD_REFs. */
3389 (BIT_FIELD_REF @0 @1 @2)
3391 (if (TREE_CODE (TREE_TYPE (@0)) == COMPLEX_TYPE
3392 && tree_int_cst_equal (@1, TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))))
3394 (if (integer_zerop (@2))
3395 (view_convert (realpart @0)))
3396 (if (tree_int_cst_equal (@2, TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))))
3397 (view_convert (imagpart @0)))))
3398 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
3399 && INTEGRAL_TYPE_P (type)
3400 /* On GIMPLE this should only apply to register arguments. */
3401 && (! GIMPLE || is_gimple_reg (@0))
3402 /* A bit-field-ref that referenced the full argument can be stripped. */
3403 && ((compare_tree_int (@1, TYPE_PRECISION (TREE_TYPE (@0))) == 0
3404 && integer_zerop (@2))
3405 /* Low-parts can be reduced to integral conversions.
3406 ??? The following doesn't work for PDP endian. */
3407 || (BYTES_BIG_ENDIAN == WORDS_BIG_ENDIAN
3408 /* Don't even think about BITS_BIG_ENDIAN. */
3409 && TYPE_PRECISION (TREE_TYPE (@0)) % BITS_PER_UNIT == 0
3410 && TYPE_PRECISION (type) % BITS_PER_UNIT == 0
3411 && compare_tree_int (@2, (BYTES_BIG_ENDIAN
3412 ? (TYPE_PRECISION (TREE_TYPE (@0))
3413 - TYPE_PRECISION (type))
3417 /* Simplify vector extracts. */
3420 (BIT_FIELD_REF CONSTRUCTOR@0 @1 @2)
3421 (if (VECTOR_TYPE_P (TREE_TYPE (@0))
3422 && (types_match (type, TREE_TYPE (TREE_TYPE (@0)))
3423 || (VECTOR_TYPE_P (type)
3424 && types_match (TREE_TYPE (type), TREE_TYPE (TREE_TYPE (@0))))))
3427 tree ctor = (TREE_CODE (@0) == SSA_NAME
3428 ? gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0)) : @0);
3429 tree eltype = TREE_TYPE (TREE_TYPE (ctor));
3430 unsigned HOST_WIDE_INT width = tree_to_uhwi (TYPE_SIZE (eltype));
3431 unsigned HOST_WIDE_INT n = tree_to_uhwi (@1);
3432 unsigned HOST_WIDE_INT idx = tree_to_uhwi (@2);
3435 && (idx % width) == 0
3437 && ((idx + n) / width) <= TYPE_VECTOR_SUBPARTS (TREE_TYPE (ctor)))
3442 /* Constructor elements can be subvectors. */
3443 unsigned HOST_WIDE_INT k = 1;
3444 if (CONSTRUCTOR_NELTS (ctor) != 0)
3446 tree cons_elem = TREE_TYPE (CONSTRUCTOR_ELT (ctor, 0)->value);
3447 if (TREE_CODE (cons_elem) == VECTOR_TYPE)
3448 k = TYPE_VECTOR_SUBPARTS (cons_elem);
3452 /* We keep an exact subset of the constructor elements. */
3453 (if ((idx % k) == 0 && (n % k) == 0)
3454 (if (CONSTRUCTOR_NELTS (ctor) == 0)
3455 { build_constructor (type, NULL); }
3462 (if (idx < CONSTRUCTOR_NELTS (ctor))
3463 { CONSTRUCTOR_ELT (ctor, idx)->value; }
3464 { build_zero_cst (type); })
3466 vec<constructor_elt, va_gc> *vals;
3467 vec_alloc (vals, n);
3468 for (unsigned i = 0;
3469 i < n && idx + i < CONSTRUCTOR_NELTS (ctor); ++i)
3470 CONSTRUCTOR_APPEND_ELT (vals, NULL_TREE,
3471 CONSTRUCTOR_ELT (ctor, idx + i)->value);
3472 build_constructor (type, vals);
3474 /* The bitfield references a single constructor element. */
3475 (if (idx + n <= (idx / k + 1) * k)
3477 (if (CONSTRUCTOR_NELTS (ctor) <= idx / k)
3478 { build_zero_cst (type); })
3480 { CONSTRUCTOR_ELT (ctor, idx / k)->value; })
3481 (BIT_FIELD_REF { CONSTRUCTOR_ELT (ctor, idx / k)->value; }
3482 @1 { bitsize_int ((idx % k) * width); })))))))))