1 /* Match-and-simplify patterns for shared GENERIC and GIMPLE folding.
2 This file is consumed by genmatch which produces gimple-match.c
3 and generic-match.c from it.
5 Copyright (C) 2014-2016 Free Software Foundation, Inc.
6 Contributed by Richard Biener <rguenther@suse.de>
7 and Prathamesh Kulkarni <bilbotheelffriend@gmail.com>
9 This file is part of GCC.
11 GCC is free software; you can redistribute it and/or modify it under
12 the terms of the GNU General Public License as published by the Free
13 Software Foundation; either version 3, or (at your option) any later
16 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
17 WARRANTY; without even the implied warranty of MERCHANTABILITY or
18 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21 You should have received a copy of the GNU General Public License
22 along with GCC; see the file COPYING3. If not see
23 <http://www.gnu.org/licenses/>. */
26 /* Generic tree predicates we inherit. */
28 integer_onep integer_zerop integer_all_onesp integer_minus_onep
29 integer_each_onep integer_truep integer_nonzerop
30 real_zerop real_onep real_minus_onep
33 tree_expr_nonnegative_p
39 (define_operator_list tcc_comparison
40 lt le eq ne ge gt unordered ordered unlt unle ungt unge uneq ltgt)
41 (define_operator_list inverted_tcc_comparison
42 ge gt ne eq lt le ordered unordered ge gt le lt ltgt uneq)
43 (define_operator_list inverted_tcc_comparison_with_nans
44 unge ungt ne eq unlt unle ordered unordered ge gt le lt ltgt uneq)
45 (define_operator_list swapped_tcc_comparison
46 gt ge eq ne le lt unordered ordered ungt unge unlt unle uneq ltgt)
47 (define_operator_list simple_comparison lt le eq ne ge gt)
48 (define_operator_list swapped_simple_comparison gt ge eq ne le lt)
50 #include "cfn-operators.pd"
52 /* Define operand lists for math rounding functions {,i,l,ll}FN,
53 where the versions prefixed with "i" return an int, those prefixed with
54 "l" return a long and those prefixed with "ll" return a long long.
56 Also define operand lists:
58 X<FN>F for all float functions, in the order i, l, ll
59 X<FN> for all double functions, in the same order
60 X<FN>L for all long double functions, in the same order. */
61 #define DEFINE_INT_AND_FLOAT_ROUND_FN(FN) \
62 (define_operator_list X##FN##F BUILT_IN_I##FN##F \
65 (define_operator_list X##FN BUILT_IN_I##FN \
68 (define_operator_list X##FN##L BUILT_IN_I##FN##L \
72 DEFINE_INT_AND_FLOAT_ROUND_FN (FLOOR)
73 DEFINE_INT_AND_FLOAT_ROUND_FN (CEIL)
74 DEFINE_INT_AND_FLOAT_ROUND_FN (ROUND)
75 DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
77 /* Simplifications of operations with one constant operand and
78 simplifications to constants or single values. */
80 (for op (plus pointer_plus minus bit_ior bit_xor)
85 /* 0 +p index -> (type)index */
87 (pointer_plus integer_zerop @1)
88 (non_lvalue (convert @1)))
90 /* See if ARG1 is zero and X + ARG1 reduces to X.
91 Likewise if the operands are reversed. */
93 (plus:c @0 real_zerop@1)
94 (if (fold_real_zero_addition_p (type, @1, 0))
97 /* See if ARG1 is zero and X - ARG1 reduces to X. */
99 (minus @0 real_zerop@1)
100 (if (fold_real_zero_addition_p (type, @1, 1))
104 This is unsafe for certain floats even in non-IEEE formats.
105 In IEEE, it is unsafe because it does wrong for NaNs.
106 Also note that operand_equal_p is always false if an operand
110 (if (!FLOAT_TYPE_P (type) || !HONOR_NANS (type))
111 { build_zero_cst (type); }))
114 (mult @0 integer_zerop@1)
117 /* Maybe fold x * 0 to 0. The expressions aren't the same
118 when x is NaN, since x * 0 is also NaN. Nor are they the
119 same in modes with signed zeros, since multiplying a
120 negative value by 0 gives -0, not +0. */
122 (mult @0 real_zerop@1)
123 (if (!HONOR_NANS (type) && !HONOR_SIGNED_ZEROS (type))
126 /* In IEEE floating point, x*1 is not equivalent to x for snans.
127 Likewise for complex arithmetic with signed zeros. */
130 (if (!HONOR_SNANS (type)
131 && (!HONOR_SIGNED_ZEROS (type)
132 || !COMPLEX_FLOAT_TYPE_P (type)))
135 /* Transform x * -1.0 into -x. */
137 (mult @0 real_minus_onep)
138 (if (!HONOR_SNANS (type)
139 && (!HONOR_SIGNED_ZEROS (type)
140 || !COMPLEX_FLOAT_TYPE_P (type)))
143 /* Make sure to preserve divisions by zero. This is the reason why
144 we don't simplify x / x to 1 or 0 / x to 0. */
145 (for op (mult trunc_div ceil_div floor_div round_div exact_div)
151 (for div (trunc_div ceil_div floor_div round_div exact_div)
153 (div @0 integer_minus_onep@1)
154 (if (!TYPE_UNSIGNED (type))
157 /* For unsigned integral types, FLOOR_DIV_EXPR is the same as
158 TRUNC_DIV_EXPR. Rewrite into the latter in this case. */
161 (if ((INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
162 && TYPE_UNSIGNED (type))
165 /* Combine two successive divisions. Note that combining ceil_div
166 and floor_div is trickier and combining round_div even more so. */
167 (for div (trunc_div exact_div)
169 (div (div @0 INTEGER_CST@1) INTEGER_CST@2)
172 wide_int mul = wi::mul (@1, @2, TYPE_SIGN (type), &overflow_p);
175 (div @0 { wide_int_to_tree (type, mul); })
176 (if (TYPE_UNSIGNED (type)
177 || mul != wi::min_value (TYPE_PRECISION (type), SIGNED))
178 { build_zero_cst (type); })))))
180 /* Optimize A / A to 1.0 if we don't care about
181 NaNs or Infinities. */
184 (if (FLOAT_TYPE_P (type)
185 && ! HONOR_NANS (type)
186 && ! HONOR_INFINITIES (type))
187 { build_one_cst (type); }))
189 /* Optimize -A / A to -1.0 if we don't care about
190 NaNs or Infinities. */
192 (rdiv:C @0 (negate @0))
193 (if (FLOAT_TYPE_P (type)
194 && ! HONOR_NANS (type)
195 && ! HONOR_INFINITIES (type))
196 { build_minus_one_cst (type); }))
198 /* In IEEE floating point, x/1 is not equivalent to x for snans. */
201 (if (!HONOR_SNANS (type))
204 /* In IEEE floating point, x/-1 is not equivalent to -x for snans. */
206 (rdiv @0 real_minus_onep)
207 (if (!HONOR_SNANS (type))
210 (if (flag_reciprocal_math)
211 /* Convert (A/B)/C to A/(B*C) */
213 (rdiv (rdiv:s @0 @1) @2)
214 (rdiv @0 (mult @1 @2)))
216 /* Convert A/(B/C) to (A/B)*C */
218 (rdiv @0 (rdiv:s @1 @2))
219 (mult (rdiv @0 @1) @2)))
221 /* Optimize (X & (-A)) / A where A is a power of 2, to X >> log2(A) */
222 (for div (trunc_div ceil_div floor_div round_div exact_div)
224 (div (convert? (bit_and @0 INTEGER_CST@1)) INTEGER_CST@2)
225 (if (integer_pow2p (@2)
226 && tree_int_cst_sgn (@2) > 0
227 && wi::add (@2, @1) == 0
228 && tree_nop_conversion_p (type, TREE_TYPE (@0)))
229 (rshift (convert @0) { build_int_cst (integer_type_node,
230 wi::exact_log2 (@2)); }))))
232 /* If ARG1 is a constant, we can convert this to a multiply by the
233 reciprocal. This does not have the same rounding properties,
234 so only do this if -freciprocal-math. We can actually
235 always safely do it if ARG1 is a power of two, but it's hard to
236 tell if it is or not in a portable manner. */
237 (for cst (REAL_CST COMPLEX_CST VECTOR_CST)
241 (if (flag_reciprocal_math
244 { tree tem = const_binop (RDIV_EXPR, type, build_one_cst (type), @1); }
246 (mult @0 { tem; } )))
247 (if (cst != COMPLEX_CST)
248 (with { tree inverse = exact_inverse (type, @1); }
250 (mult @0 { inverse; } ))))))))
252 /* Same applies to modulo operations, but fold is inconsistent here
253 and simplifies 0 % x to 0, only preserving literal 0 % 0. */
254 (for mod (ceil_mod floor_mod round_mod trunc_mod)
255 /* 0 % X is always zero. */
257 (mod integer_zerop@0 @1)
258 /* But not for 0 % 0 so that we can get the proper warnings and errors. */
259 (if (!integer_zerop (@1))
261 /* X % 1 is always zero. */
263 (mod @0 integer_onep)
264 { build_zero_cst (type); })
265 /* X % -1 is zero. */
267 (mod @0 integer_minus_onep@1)
268 (if (!TYPE_UNSIGNED (type))
269 { build_zero_cst (type); }))
270 /* (X % Y) % Y is just X % Y. */
272 (mod (mod@2 @0 @1) @1)
274 /* From extract_muldiv_1: (X * C1) % C2 is zero if C1 is a multiple of C2. */
276 (mod (mult @0 INTEGER_CST@1) INTEGER_CST@2)
277 (if (ANY_INTEGRAL_TYPE_P (type)
278 && TYPE_OVERFLOW_UNDEFINED (type)
279 && wi::multiple_of_p (@1, @2, TYPE_SIGN (type)))
280 { build_zero_cst (type); })))
282 /* X % -C is the same as X % C. */
284 (trunc_mod @0 INTEGER_CST@1)
285 (if (TYPE_SIGN (type) == SIGNED
286 && !TREE_OVERFLOW (@1)
288 && !TYPE_OVERFLOW_TRAPS (type)
289 /* Avoid this transformation if C is INT_MIN, i.e. C == -C. */
290 && !sign_bit_p (@1, @1))
291 (trunc_mod @0 (negate @1))))
293 /* X % -Y is the same as X % Y. */
295 (trunc_mod @0 (convert? (negate @1)))
296 (if (INTEGRAL_TYPE_P (type)
297 && !TYPE_UNSIGNED (type)
298 && !TYPE_OVERFLOW_TRAPS (type)
299 && tree_nop_conversion_p (type, TREE_TYPE (@1))
300 /* Avoid this transformation if X might be INT_MIN or
301 Y might be -1, because we would then change valid
302 INT_MIN % -(-1) into invalid INT_MIN % -1. */
303 && (expr_not_equal_to (@0, TYPE_MIN_VALUE (type))
304 || expr_not_equal_to (@1, wi::minus_one (TYPE_PRECISION
306 (trunc_mod @0 (convert @1))))
308 /* X - (X / Y) * Y is the same as X % Y. */
310 (minus (convert1? @2) (convert2? (mult:c (trunc_div @0 @1) @1)))
311 /* We cannot use matching captures here, since in the case of
312 constants we really want the type of @0, not @2. */
313 (if (operand_equal_p (@0, @2, 0)
314 && (INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type)))
315 (convert (trunc_mod @0 @1))))
317 /* Optimize TRUNC_MOD_EXPR by a power of two into a BIT_AND_EXPR,
318 i.e. "X % C" into "X & (C - 1)", if X and C are positive.
319 Also optimize A % (C << N) where C is a power of 2,
320 to A & ((C << N) - 1). */
321 (match (power_of_two_cand @1)
323 (match (power_of_two_cand @1)
324 (lshift INTEGER_CST@1 @2))
325 (for mod (trunc_mod floor_mod)
327 (mod @0 (convert?@3 (power_of_two_cand@1 @2)))
328 (if ((TYPE_UNSIGNED (type)
329 || tree_expr_nonnegative_p (@0))
330 && tree_nop_conversion_p (type, TREE_TYPE (@3))
331 && integer_pow2p (@2) && tree_int_cst_sgn (@2) > 0)
332 (bit_and @0 (convert (minus @1 { build_int_cst (TREE_TYPE (@1), 1); }))))))
334 /* Simplify (unsigned t * 2)/2 -> unsigned t & 0x7FFFFFFF. */
336 (trunc_div (mult @0 integer_pow2p@1) @1)
337 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
338 (bit_and @0 { wide_int_to_tree
339 (type, wi::mask (TYPE_PRECISION (type) - wi::exact_log2 (@1),
340 false, TYPE_PRECISION (type))); })))
342 /* Simplify (unsigned t / 2) * 2 -> unsigned t & ~1. */
344 (mult (trunc_div @0 integer_pow2p@1) @1)
345 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
346 (bit_and @0 (negate @1))))
348 /* Simplify (t * 2) / 2) -> t. */
349 (for div (trunc_div ceil_div floor_div round_div exact_div)
351 (div (mult @0 @1) @1)
352 (if (ANY_INTEGRAL_TYPE_P (type)
353 && TYPE_OVERFLOW_UNDEFINED (type))
357 /* Simplify cos(-x) and cos(|x|) -> cos(x). Similarly for cosh. */
362 /* Simplify pow(-x, y) and pow(|x|,y) -> pow(x,y) if y is an even integer. */
365 (pows (op @0) REAL_CST@1)
366 (with { HOST_WIDE_INT n; }
367 (if (real_isinteger (&TREE_REAL_CST (@1), &n) && (n & 1) == 0)
369 /* Likewise for powi. */
372 (pows (op @0) INTEGER_CST@1)
373 (if (wi::bit_and (@1, 1) == 0)
375 /* Strip negate and abs from both operands of hypot. */
383 /* copysign(-x, y) and copysign(abs(x), y) -> copysign(x, y). */
384 (for copysigns (COPYSIGN)
386 (copysigns (op @0) @1)
389 /* abs(x)*abs(x) -> x*x. Should be valid for all types. */
394 /* cos(copysign(x, y)) -> cos(x). Similarly for cosh. */
398 (coss (copysigns @0 @1))
401 /* pow(copysign(x, y), z) -> pow(x, z) if z is an even integer. */
405 (pows (copysigns @0 @2) REAL_CST@1)
406 (with { HOST_WIDE_INT n; }
407 (if (real_isinteger (&TREE_REAL_CST (@1), &n) && (n & 1) == 0)
409 /* Likewise for powi. */
413 (pows (copysigns @0 @2) INTEGER_CST@1)
414 (if (wi::bit_and (@1, 1) == 0)
419 /* hypot(copysign(x, y), z) -> hypot(x, z). */
421 (hypots (copysigns @0 @1) @2)
423 /* hypot(x, copysign(y, z)) -> hypot(x, y). */
425 (hypots @0 (copysigns @1 @2))
428 /* copysign(copysign(x, y), z) -> copysign(x, z). */
429 (for copysigns (COPYSIGN)
431 (copysigns (copysigns @0 @1) @2)
434 /* copysign(x,y)*copysign(x,y) -> x*x. */
435 (for copysigns (COPYSIGN)
437 (mult (copysigns@2 @0 @1) @2)
440 /* ccos(-x) -> ccos(x). Similarly for ccosh. */
441 (for ccoss (CCOS CCOSH)
446 /* cabs(-x) and cos(conj(x)) -> cabs(x). */
447 (for ops (conj negate)
453 /* Fold (a * (1 << b)) into (a << b) */
455 (mult:c @0 (convert? (lshift integer_onep@1 @2)))
456 (if (! FLOAT_TYPE_P (type)
457 && (element_precision (type) <= element_precision (TREE_TYPE (@1))
458 || TYPE_UNSIGNED (TREE_TYPE (@1))))
461 /* Fold (C1/X)*C2 into (C1*C2)/X. */
463 (mult (rdiv@3 REAL_CST@0 @1) REAL_CST@2)
464 (if (flag_associative_math
467 { tree tem = const_binop (MULT_EXPR, type, @0, @2); }
469 (rdiv { tem; } @1)))))
471 /* Convert C1/(X*C2) into (C1/C2)/X */
473 (rdiv REAL_CST@0 (mult @1 REAL_CST@2))
474 (if (flag_reciprocal_math)
476 { tree tem = const_binop (RDIV_EXPR, type, @0, @2); }
478 (rdiv { tem; } @1)))))
480 /* Simplify ~X & X as zero. */
482 (bit_and:c (convert? @0) (convert? (bit_not @0)))
483 { build_zero_cst (type); })
485 /* Fold (A & ~B) - (A & B) into (A ^ B) - B. */
487 (minus (bit_and:cs @0 (bit_not @1)) (bit_and:cs @0 @1))
488 (minus (bit_xor @0 @1) @1))
490 (minus (bit_and:s @0 INTEGER_CST@2) (bit_and:s @0 INTEGER_CST@1))
491 (if (wi::bit_not (@2) == @1)
492 (minus (bit_xor @0 @1) @1)))
494 /* Fold (A & B) - (A & ~B) into B - (A ^ B). */
496 (minus (bit_and:cs @0 @1) (bit_and:cs @0 (bit_not @1)))
497 (minus @1 (bit_xor @0 @1)))
499 /* Simplify (X & ~Y) | (~X & Y) -> X ^ Y. */
501 (bit_ior (bit_and:c @0 (bit_not @1)) (bit_and:c (bit_not @0) @1))
504 (bit_ior:c (bit_and @0 INTEGER_CST@2) (bit_and (bit_not @0) INTEGER_CST@1))
505 (if (wi::bit_not (@2) == @1)
507 /* Simplify (~X & Y) to X ^ Y if we know that (X & ~Y) is 0. */
510 (bit_and (bit_not SSA_NAME@0) INTEGER_CST@1)
511 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
512 && (get_nonzero_bits (@0) & wi::bit_not (@1)) == 0)
516 /* X % Y is smaller than Y. */
519 (cmp (trunc_mod @0 @1) @1)
520 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
521 { constant_boolean_node (cmp == LT_EXPR, type); })))
524 (cmp @1 (trunc_mod @0 @1))
525 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
526 { constant_boolean_node (cmp == GT_EXPR, type); })))
530 (bit_ior @0 integer_all_onesp@1)
535 (bit_and @0 integer_zerop@1)
541 (for op (bit_ior bit_xor plus)
543 (op:c (convert? @0) (convert? (bit_not @0)))
544 (convert { build_all_ones_cst (TREE_TYPE (@0)); })))
549 { build_zero_cst (type); })
551 /* Canonicalize X ^ ~0 to ~X. */
553 (bit_xor @0 integer_all_onesp@1)
558 (bit_and @0 integer_all_onesp)
561 /* x & x -> x, x | x -> x */
562 (for bitop (bit_and bit_ior)
567 /* x & C -> x if we know that x & ~C == 0. */
570 (bit_and SSA_NAME@0 INTEGER_CST@1)
571 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
572 && (get_nonzero_bits (@0) & wi::bit_not (@1)) == 0)
576 /* x + (x & 1) -> (x + 1) & ~1 */
578 (plus:c @0 (bit_and:s @0 integer_onep@1))
579 (bit_and (plus @0 @1) (bit_not @1)))
581 /* x & ~(x & y) -> x & ~y */
582 /* x | ~(x | y) -> x | ~y */
583 (for bitop (bit_and bit_ior)
585 (bitop:c @0 (bit_not (bitop:cs @0 @1)))
586 (bitop @0 (bit_not @1))))
588 /* (x | y) & ~x -> y & ~x */
589 /* (x & y) | ~x -> y | ~x */
590 (for bitop (bit_and bit_ior)
591 rbitop (bit_ior bit_and)
593 (bitop:c (rbitop:c @0 @1) (bit_not@2 @0))
596 /* (x & y) ^ (x | y) -> x ^ y */
598 (bit_xor:c (bit_and @0 @1) (bit_ior @0 @1))
601 /* (x ^ y) ^ (x | y) -> x & y */
603 (bit_xor:c (bit_xor @0 @1) (bit_ior @0 @1))
606 /* (x & y) + (x ^ y) -> x | y */
607 /* (x & y) | (x ^ y) -> x | y */
608 /* (x & y) ^ (x ^ y) -> x | y */
609 (for op (plus bit_ior bit_xor)
611 (op:c (bit_and @0 @1) (bit_xor @0 @1))
614 /* (x & y) + (x | y) -> x + y */
616 (plus:c (bit_and @0 @1) (bit_ior @0 @1))
619 /* (x + y) - (x | y) -> x & y */
621 (minus (plus @0 @1) (bit_ior @0 @1))
622 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
623 && !TYPE_SATURATING (type))
626 /* (x + y) - (x & y) -> x | y */
628 (minus (plus @0 @1) (bit_and @0 @1))
629 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
630 && !TYPE_SATURATING (type))
633 /* (x | y) - (x ^ y) -> x & y */
635 (minus (bit_ior @0 @1) (bit_xor @0 @1))
638 /* (x | y) - (x & y) -> x ^ y */
640 (minus (bit_ior @0 @1) (bit_and @0 @1))
643 /* (x | y) & ~(x & y) -> x ^ y */
645 (bit_and:c (bit_ior @0 @1) (bit_not (bit_and @0 @1)))
648 /* (x | y) & (~x ^ y) -> x & y */
650 (bit_and:c (bit_ior:c @0 @1) (bit_xor:c @1 (bit_not @0)))
653 /* ~x & ~y -> ~(x | y)
654 ~x | ~y -> ~(x & y) */
655 (for op (bit_and bit_ior)
656 rop (bit_ior bit_and)
658 (op (convert1? (bit_not @0)) (convert2? (bit_not @1)))
659 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
660 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
661 (bit_not (rop (convert @0) (convert @1))))))
663 /* If we are XORing or adding two BIT_AND_EXPR's, both of which are and'ing
664 with a constant, and the two constants have no bits in common,
665 we should treat this as a BIT_IOR_EXPR since this may produce more
667 (for op (bit_xor plus)
669 (op (convert1? (bit_and@4 @0 INTEGER_CST@1))
670 (convert2? (bit_and@5 @2 INTEGER_CST@3)))
671 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
672 && tree_nop_conversion_p (type, TREE_TYPE (@2))
673 && wi::bit_and (@1, @3) == 0)
674 (bit_ior (convert @4) (convert @5)))))
676 /* (X | Y) ^ X -> Y & ~ X*/
678 (bit_xor:c (convert? (bit_ior:c @0 @1)) (convert? @0))
679 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
680 (convert (bit_and @1 (bit_not @0)))))
682 /* Convert ~X ^ ~Y to X ^ Y. */
684 (bit_xor (convert1? (bit_not @0)) (convert2? (bit_not @1)))
685 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
686 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
687 (bit_xor (convert @0) (convert @1))))
689 /* Convert ~X ^ C to X ^ ~C. */
691 (bit_xor (convert? (bit_not @0)) INTEGER_CST@1)
692 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
693 (bit_xor (convert @0) (bit_not @1))))
695 /* Fold (X & Y) ^ Y and (X ^ Y) & Y as ~X & Y. */
696 (for opo (bit_and bit_xor)
697 opi (bit_xor bit_and)
699 (opo:c (opi:c @0 @1) @1)
700 (bit_and (bit_not @0) @1)))
702 /* Given a bit-wise operation CODE applied to ARG0 and ARG1, see if both
703 operands are another bit-wise operation with a common input. If so,
704 distribute the bit operations to save an operation and possibly two if
705 constants are involved. For example, convert
706 (A | B) & (A | C) into A | (B & C)
707 Further simplification will occur if B and C are constants. */
708 (for op (bit_and bit_ior bit_xor)
709 rop (bit_ior bit_and bit_and)
711 (op (convert? (rop:c @0 @1)) (convert? (rop:c @0 @2)))
712 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
713 && tree_nop_conversion_p (type, TREE_TYPE (@2)))
714 (rop (convert @0) (op (convert @1) (convert @2))))))
716 /* Some simple reassociation for bit operations, also handled in reassoc. */
717 /* (X & Y) & Y -> X & Y
718 (X | Y) | Y -> X | Y */
719 (for op (bit_and bit_ior)
721 (op:c (convert?@2 (op:c @0 @1)) (convert? @1))
723 /* (X ^ Y) ^ Y -> X */
725 (bit_xor:c (convert? (bit_xor:c @0 @1)) (convert? @1))
727 /* (X & Y) & (X & Z) -> (X & Y) & Z
728 (X | Y) | (X | Z) -> (X | Y) | Z */
729 (for op (bit_and bit_ior)
731 (op:c (convert1?@3 (op:c@4 @0 @1)) (convert2?@5 (op:c@6 @0 @2)))
732 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
733 && tree_nop_conversion_p (type, TREE_TYPE (@2)))
734 (if (single_use (@5) && single_use (@6))
736 (if (single_use (@3) && single_use (@4))
737 (op (convert @1) @5))))))
738 /* (X ^ Y) ^ (X ^ Z) -> Y ^ Z */
740 (bit_xor (convert1? (bit_xor:c @0 @1)) (convert2? (bit_xor:c @0 @2)))
741 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
742 && tree_nop_conversion_p (type, TREE_TYPE (@2)))
743 (bit_xor (convert @1) (convert @2))))
752 (abs tree_expr_nonnegative_p@0)
755 /* A few cases of fold-const.c negate_expr_p predicate. */
758 (if ((INTEGRAL_TYPE_P (type)
759 && TYPE_OVERFLOW_WRAPS (type))
760 || (!TYPE_OVERFLOW_SANITIZED (type)
761 && may_negate_without_overflow_p (t)))))
766 (if (!TYPE_OVERFLOW_SANITIZED (type))))
769 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (t)))))
770 /* VECTOR_CST handling of non-wrapping types would recurse in unsupported
774 (if (FLOAT_TYPE_P (TREE_TYPE (type)) || TYPE_OVERFLOW_WRAPS (type))))
776 /* (-A) * (-B) -> A * B */
778 (mult:c (convert1? (negate @0)) (convert2? negate_expr_p@1))
779 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
780 && tree_nop_conversion_p (type, TREE_TYPE (@1)))
781 (mult (convert @0) (convert (negate @1)))))
783 /* -(A + B) -> (-B) - A. */
785 (negate (plus:c @0 negate_expr_p@1))
786 (if (!HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type))
787 && !HONOR_SIGNED_ZEROS (element_mode (type)))
788 (minus (negate @1) @0)))
790 /* A - B -> A + (-B) if B is easily negatable. */
792 (minus @0 negate_expr_p@1)
793 (if (!FIXED_POINT_TYPE_P (type))
794 (plus @0 (negate @1))))
796 /* Try to fold (type) X op CST -> (type) (X op ((type-x) CST))
798 For bitwise binary operations apply operand conversions to the
799 binary operation result instead of to the operands. This allows
800 to combine successive conversions and bitwise binary operations.
801 We combine the above two cases by using a conditional convert. */
802 (for bitop (bit_and bit_ior bit_xor)
804 (bitop (convert @0) (convert? @1))
805 (if (((TREE_CODE (@1) == INTEGER_CST
806 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
807 && int_fits_type_p (@1, TREE_TYPE (@0)))
808 || types_match (@0, @1))
809 /* ??? This transform conflicts with fold-const.c doing
810 Convert (T)(x & c) into (T)x & (T)c, if c is an integer
811 constants (if x has signed type, the sign bit cannot be set
812 in c). This folds extension into the BIT_AND_EXPR.
813 Restrict it to GIMPLE to avoid endless recursions. */
814 && (bitop != BIT_AND_EXPR || GIMPLE)
815 && (/* That's a good idea if the conversion widens the operand, thus
816 after hoisting the conversion the operation will be narrower. */
817 TYPE_PRECISION (TREE_TYPE (@0)) < TYPE_PRECISION (type)
818 /* It's also a good idea if the conversion is to a non-integer
820 || GET_MODE_CLASS (TYPE_MODE (type)) != MODE_INT
821 /* Or if the precision of TO is not the same as the precision
823 || TYPE_PRECISION (type) != GET_MODE_PRECISION (TYPE_MODE (type))))
824 (convert (bitop @0 (convert @1))))))
826 (for bitop (bit_and bit_ior)
827 rbitop (bit_ior bit_and)
828 /* (x | y) & x -> x */
829 /* (x & y) | x -> x */
831 (bitop:c (rbitop:c @0 @1) @0)
833 /* (~x | y) & x -> x & y */
834 /* (~x & y) | x -> x | y */
836 (bitop:c (rbitop:c (bit_not @0) @1) @0)
839 /* (x | CST1) & CST2 -> (x & CST2) | (CST1 & CST2) */
841 (bit_and (bit_ior @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
842 (bit_ior (bit_and @0 @2) (bit_and @1 @2)))
844 /* Combine successive equal operations with constants. */
845 (for bitop (bit_and bit_ior bit_xor)
847 (bitop (bitop @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
848 (bitop @0 (bitop @1 @2))))
850 /* Try simple folding for X op !X, and X op X with the help
851 of the truth_valued_p and logical_inverted_value predicates. */
852 (match truth_valued_p
854 (if (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1)))
855 (for op (tcc_comparison truth_and truth_andif truth_or truth_orif truth_xor)
856 (match truth_valued_p
858 (match truth_valued_p
861 (match (logical_inverted_value @0)
863 (match (logical_inverted_value @0)
864 (bit_not truth_valued_p@0))
865 (match (logical_inverted_value @0)
866 (eq @0 integer_zerop))
867 (match (logical_inverted_value @0)
868 (ne truth_valued_p@0 integer_truep))
869 (match (logical_inverted_value @0)
870 (bit_xor truth_valued_p@0 integer_truep))
874 (bit_and:c @0 (logical_inverted_value @0))
875 { build_zero_cst (type); })
876 /* X | !X and X ^ !X -> 1, , if X is truth-valued. */
877 (for op (bit_ior bit_xor)
879 (op:c truth_valued_p@0 (logical_inverted_value @0))
880 { constant_boolean_node (true, type); }))
881 /* X ==/!= !X is false/true. */
884 (op:c truth_valued_p@0 (logical_inverted_value @0))
885 { constant_boolean_node (op == NE_EXPR ? true : false, type); }))
887 /* If arg1 and arg2 are booleans (or any single bit type)
888 then try to simplify:
895 But only do this if our result feeds into a comparison as
896 this transformation is not always a win, particularly on
897 targets with and-not instructions.
898 -> simplify_bitwise_binary_boolean */
900 (ne (bit_and:c (bit_not @0) @1) integer_zerop)
901 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
902 && TYPE_PRECISION (TREE_TYPE (@1)) == 1)
903 (if (TYPE_UNSIGNED (TREE_TYPE (@1)))
907 (ne (bit_ior:c (bit_not @0) @1) integer_zerop)
908 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
909 && TYPE_PRECISION (TREE_TYPE (@1)) == 1)
910 (if (TYPE_UNSIGNED (TREE_TYPE (@1)))
916 (bit_not (bit_not @0))
919 /* Convert ~ (-A) to A - 1. */
921 (bit_not (convert? (negate @0)))
922 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
923 || !TYPE_UNSIGNED (TREE_TYPE (@0)))
924 (convert (minus @0 { build_each_one_cst (TREE_TYPE (@0)); }))))
926 /* Convert ~ (A - 1) or ~ (A + -1) to -A. */
928 (bit_not (convert? (minus @0 integer_each_onep)))
929 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
930 || !TYPE_UNSIGNED (TREE_TYPE (@0)))
931 (convert (negate @0))))
933 (bit_not (convert? (plus @0 integer_all_onesp)))
934 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
935 || !TYPE_UNSIGNED (TREE_TYPE (@0)))
936 (convert (negate @0))))
938 /* Part of convert ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify. */
940 (bit_not (convert? (bit_xor @0 INTEGER_CST@1)))
941 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
942 (convert (bit_xor @0 (bit_not @1)))))
944 (bit_not (convert? (bit_xor:c (bit_not @0) @1)))
945 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
946 (convert (bit_xor @0 @1))))
948 /* (x & ~m) | (y & m) -> ((x ^ y) & m) ^ x */
950 (bit_ior:c (bit_and:cs @0 (bit_not @2)) (bit_and:cs @1 @2))
951 (bit_xor (bit_and (bit_xor @0 @1) @2) @0))
953 /* Fold A - (A & B) into ~B & A. */
955 (minus (convert? @0) (convert?:s (bit_and:cs @0 @1)))
956 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
957 && tree_nop_conversion_p (type, TREE_TYPE (@1)))
958 (convert (bit_and (bit_not @1) @0))))
962 /* ((X inner_op C0) outer_op C1)
963 With X being a tree where value_range has reasoned certain bits to always be
964 zero throughout its computed value range,
965 inner_op = {|,^}, outer_op = {|,^} and inner_op != outer_op
966 where zero_mask has 1's for all bits that are sure to be 0 in
968 if (inner_op == '^') C0 &= ~C1;
969 if ((C0 & ~zero_mask) == 0) then emit (X outer_op (C0 outer_op C1)
970 if ((C1 & ~zero_mask) == 0) then emit (X inner_op (C0 outer_op C1)
972 (for inner_op (bit_ior bit_xor)
973 outer_op (bit_xor bit_ior)
976 (inner_op:s @2 INTEGER_CST@0) INTEGER_CST@1)
980 wide_int zero_mask_not;
984 if (TREE_CODE (@2) == SSA_NAME)
985 zero_mask_not = get_nonzero_bits (@2);
989 if (inner_op == BIT_XOR_EXPR)
991 C0 = wi::bit_and_not (@0, @1);
992 cst_emit = wi::bit_or (C0, @1);
997 cst_emit = wi::bit_xor (@0, @1);
1000 (if (!fail && wi::bit_and (C0, zero_mask_not) == 0)
1001 (outer_op @2 { wide_int_to_tree (type, cst_emit); })
1002 (if (!fail && wi::bit_and (@1, zero_mask_not) == 0)
1003 (inner_op @2 { wide_int_to_tree (type, cst_emit); }))))))
1005 /* Associate (p +p off1) +p off2 as (p +p (off1 + off2)). */
1007 (pointer_plus (pointer_plus:s @0 @1) @3)
1008 (pointer_plus @0 (plus @1 @3)))
1014 tem4 = (unsigned long) tem3;
1019 (pointer_plus @0 (convert?@2 (minus@3 (convert @1) (convert @0))))
1020 /* Conditionally look through a sign-changing conversion. */
1021 (if (TYPE_PRECISION (TREE_TYPE (@2)) == TYPE_PRECISION (TREE_TYPE (@3))
1022 && ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@1)))
1023 || (GENERIC && type == TREE_TYPE (@1))))
1027 tem = (sizetype) ptr;
1031 and produce the simpler and easier to analyze with respect to alignment
1032 ... = ptr & ~algn; */
1034 (pointer_plus @0 (negate (bit_and (convert @0) INTEGER_CST@1)))
1035 (with { tree algn = wide_int_to_tree (TREE_TYPE (@0), wi::bit_not (@1)); }
1036 (bit_and @0 { algn; })))
1038 /* Try folding difference of addresses. */
1040 (minus (convert ADDR_EXPR@0) (convert @1))
1041 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1042 (with { HOST_WIDE_INT diff; }
1043 (if (ptr_difference_const (@0, @1, &diff))
1044 { build_int_cst_type (type, diff); }))))
1046 (minus (convert @0) (convert ADDR_EXPR@1))
1047 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1048 (with { HOST_WIDE_INT diff; }
1049 (if (ptr_difference_const (@0, @1, &diff))
1050 { build_int_cst_type (type, diff); }))))
1052 /* If arg0 is derived from the address of an object or function, we may
1053 be able to fold this expression using the object or function's
1056 (bit_and (convert? @0) INTEGER_CST@1)
1057 (if (POINTER_TYPE_P (TREE_TYPE (@0))
1058 && tree_nop_conversion_p (type, TREE_TYPE (@0)))
1062 unsigned HOST_WIDE_INT bitpos;
1063 get_pointer_alignment_1 (@0, &align, &bitpos);
1065 (if (wi::ltu_p (@1, align / BITS_PER_UNIT))
1066 { wide_int_to_tree (type, wi::bit_and (@1, bitpos / BITS_PER_UNIT)); }))))
1069 /* We can't reassociate at all for saturating types. */
1070 (if (!TYPE_SATURATING (type))
1072 /* Contract negates. */
1073 /* A + (-B) -> A - B */
1075 (plus:c (convert1? @0) (convert2? (negate @1)))
1076 /* Apply STRIP_NOPS on @0 and the negate. */
1077 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
1078 && tree_nop_conversion_p (type, TREE_TYPE (@1))
1079 && !TYPE_OVERFLOW_SANITIZED (type))
1080 (minus (convert @0) (convert @1))))
1081 /* A - (-B) -> A + B */
1083 (minus (convert1? @0) (convert2? (negate @1)))
1084 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
1085 && tree_nop_conversion_p (type, TREE_TYPE (@1))
1086 && !TYPE_OVERFLOW_SANITIZED (type))
1087 (plus (convert @0) (convert @1))))
1090 (negate (convert? (negate @1)))
1091 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1092 && !TYPE_OVERFLOW_SANITIZED (type))
1095 /* We can't reassociate floating-point unless -fassociative-math
1096 or fixed-point plus or minus because of saturation to +-Inf. */
1097 (if ((!FLOAT_TYPE_P (type) || flag_associative_math)
1098 && !FIXED_POINT_TYPE_P (type))
1100 /* Match patterns that allow contracting a plus-minus pair
1101 irrespective of overflow issues. */
1102 /* (A +- B) - A -> +- B */
1103 /* (A +- B) -+ B -> A */
1104 /* A - (A +- B) -> -+ B */
1105 /* A +- (B -+ A) -> +- B */
1107 (minus (plus:c @0 @1) @0)
1110 (minus (minus @0 @1) @0)
1113 (plus:c (minus @0 @1) @1)
1116 (minus @0 (plus:c @0 @1))
1119 (minus @0 (minus @0 @1))
1122 /* (A +- CST) +- CST -> A + CST */
1123 (for outer_op (plus minus)
1124 (for inner_op (plus minus)
1126 (outer_op (inner_op @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
1127 /* If the constant operation overflows we cannot do the transform
1128 as we would introduce undefined overflow, for example
1129 with (a - 1) + INT_MIN. */
1130 (with { tree cst = const_binop (outer_op == inner_op
1131 ? PLUS_EXPR : MINUS_EXPR, type, @1, @2); }
1132 (if (cst && !TREE_OVERFLOW (cst))
1133 (inner_op @0 { cst; } ))))))
1135 /* (CST - A) +- CST -> CST - A */
1136 (for outer_op (plus minus)
1138 (outer_op (minus CONSTANT_CLASS_P@1 @0) CONSTANT_CLASS_P@2)
1139 (with { tree cst = const_binop (outer_op, type, @1, @2); }
1140 (if (cst && !TREE_OVERFLOW (cst))
1141 (minus { cst; } @0)))))
1145 (plus:c (bit_not @0) @0)
1146 (if (!TYPE_OVERFLOW_TRAPS (type))
1147 { build_all_ones_cst (type); }))
1151 (plus (convert? (bit_not @0)) integer_each_onep)
1152 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1153 (negate (convert @0))))
1157 (minus (convert? (negate @0)) integer_each_onep)
1158 (if (!TYPE_OVERFLOW_TRAPS (type)
1159 && tree_nop_conversion_p (type, TREE_TYPE (@0)))
1160 (bit_not (convert @0))))
1164 (minus integer_all_onesp @0)
1167 /* (T)(P + A) - (T)P -> (T) A */
1168 (for add (plus pointer_plus)
1170 (minus (convert (add @0 @1))
1172 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
1173 /* For integer types, if A has a smaller type
1174 than T the result depends on the possible
1176 E.g. T=size_t, A=(unsigned)429497295, P>0.
1177 However, if an overflow in P + A would cause
1178 undefined behavior, we can assume that there
1180 || (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1181 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1182 /* For pointer types, if the conversion of A to the
1183 final type requires a sign- or zero-extension,
1184 then we have to punt - it is not defined which
1186 || (POINTER_TYPE_P (TREE_TYPE (@0))
1187 && TREE_CODE (@1) == INTEGER_CST
1188 && tree_int_cst_sign_bit (@1) == 0))
1191 /* (T)P - (T)(P + A) -> -(T) A */
1192 (for add (plus pointer_plus)
1195 (convert (add @0 @1)))
1196 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
1197 /* For integer types, if A has a smaller type
1198 than T the result depends on the possible
1200 E.g. T=size_t, A=(unsigned)429497295, P>0.
1201 However, if an overflow in P + A would cause
1202 undefined behavior, we can assume that there
1204 || (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1205 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1206 /* For pointer types, if the conversion of A to the
1207 final type requires a sign- or zero-extension,
1208 then we have to punt - it is not defined which
1210 || (POINTER_TYPE_P (TREE_TYPE (@0))
1211 && TREE_CODE (@1) == INTEGER_CST
1212 && tree_int_cst_sign_bit (@1) == 0))
1213 (negate (convert @1)))))
1215 /* (T)(P + A) - (T)(P + B) -> (T)A - (T)B */
1216 (for add (plus pointer_plus)
1218 (minus (convert (add @0 @1))
1219 (convert (add @0 @2)))
1220 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
1221 /* For integer types, if A has a smaller type
1222 than T the result depends on the possible
1224 E.g. T=size_t, A=(unsigned)429497295, P>0.
1225 However, if an overflow in P + A would cause
1226 undefined behavior, we can assume that there
1228 || (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1229 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1230 /* For pointer types, if the conversion of A to the
1231 final type requires a sign- or zero-extension,
1232 then we have to punt - it is not defined which
1234 || (POINTER_TYPE_P (TREE_TYPE (@0))
1235 && TREE_CODE (@1) == INTEGER_CST
1236 && tree_int_cst_sign_bit (@1) == 0
1237 && TREE_CODE (@2) == INTEGER_CST
1238 && tree_int_cst_sign_bit (@2) == 0))
1239 (minus (convert @1) (convert @2)))))))
1242 /* Simplifications of MIN_EXPR, MAX_EXPR, fmin() and fmax(). */
1244 (for minmax (min max FMIN FMAX)
1248 /* min(max(x,y),y) -> y. */
1250 (min:c (max:c @0 @1) @1)
1252 /* max(min(x,y),y) -> y. */
1254 (max:c (min:c @0 @1) @1)
1259 (if (INTEGRAL_TYPE_P (type)
1260 && TYPE_MIN_VALUE (type)
1261 && operand_equal_p (@1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
1263 (if (INTEGRAL_TYPE_P (type)
1264 && TYPE_MAX_VALUE (type)
1265 && operand_equal_p (@1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
1270 (if (INTEGRAL_TYPE_P (type)
1271 && TYPE_MAX_VALUE (type)
1272 && operand_equal_p (@1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
1274 (if (INTEGRAL_TYPE_P (type)
1275 && TYPE_MIN_VALUE (type)
1276 && operand_equal_p (@1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
1278 (for minmax (FMIN FMAX)
1279 /* If either argument is NaN, return the other one. Avoid the
1280 transformation if we get (and honor) a signalling NaN. */
1282 (minmax:c @0 REAL_CST@1)
1283 (if (real_isnan (TREE_REAL_CST_PTR (@1))
1284 && (!HONOR_SNANS (@1) || !TREE_REAL_CST (@1).signalling))
1286 /* Convert fmin/fmax to MIN_EXPR/MAX_EXPR. C99 requires these
1287 functions to return the numeric arg if the other one is NaN.
1288 MIN and MAX don't honor that, so only transform if -ffinite-math-only
1289 is set. C99 doesn't require -0.0 to be handled, so we don't have to
1290 worry about it either. */
1291 (if (flag_finite_math_only)
1298 /* min (-A, -B) -> -max (A, B) */
1299 (for minmax (min max FMIN FMAX)
1300 maxmin (max min FMAX FMIN)
1302 (minmax (negate:s@2 @0) (negate:s@3 @1))
1303 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
1304 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1305 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
1306 (negate (maxmin @0 @1)))))
1307 /* MIN (~X, ~Y) -> ~MAX (X, Y)
1308 MAX (~X, ~Y) -> ~MIN (X, Y) */
1309 (for minmax (min max)
1312 (minmax (bit_not:s@2 @0) (bit_not:s@3 @1))
1313 (bit_not (maxmin @0 @1))))
1315 /* MIN (X, Y) == X -> X <= Y */
1316 (for minmax (min min max max)
1320 (cmp:c (minmax:c @0 @1) @0)
1321 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0)))
1323 /* MIN (X, 5) == 0 -> X == 0
1324 MIN (X, 5) == 7 -> false */
1327 (cmp (min @0 INTEGER_CST@1) INTEGER_CST@2)
1328 (if (wi::lt_p (@1, @2, TYPE_SIGN (TREE_TYPE (@0))))
1329 { constant_boolean_node (cmp == NE_EXPR, type); }
1330 (if (wi::gt_p (@1, @2, TYPE_SIGN (TREE_TYPE (@0))))
1334 (cmp (max @0 INTEGER_CST@1) INTEGER_CST@2)
1335 (if (wi::gt_p (@1, @2, TYPE_SIGN (TREE_TYPE (@0))))
1336 { constant_boolean_node (cmp == NE_EXPR, type); }
1337 (if (wi::lt_p (@1, @2, TYPE_SIGN (TREE_TYPE (@0))))
1339 /* MIN (X, C1) < C2 -> X < C2 || C1 < C2 */
1340 (for minmax (min min max max min min max max )
1341 cmp (lt le gt ge gt ge lt le )
1342 comb (bit_ior bit_ior bit_ior bit_ior bit_and bit_and bit_and bit_and)
1344 (cmp (minmax @0 INTEGER_CST@1) INTEGER_CST@2)
1345 (comb (cmp @0 @2) (cmp @1 @2))))
1347 /* Simplifications of shift and rotates. */
1349 (for rotate (lrotate rrotate)
1351 (rotate integer_all_onesp@0 @1)
1354 /* Optimize -1 >> x for arithmetic right shifts. */
1356 (rshift integer_all_onesp@0 @1)
1357 (if (!TYPE_UNSIGNED (type)
1358 && tree_expr_nonnegative_p (@1))
1361 /* Optimize (x >> c) << c into x & (-1<<c). */
1363 (lshift (rshift @0 INTEGER_CST@1) @1)
1364 (if (wi::ltu_p (@1, element_precision (type)))
1365 (bit_and @0 (lshift { build_minus_one_cst (type); } @1))))
1367 /* Optimize (x << c) >> c into x & ((unsigned)-1 >> c) for unsigned
1370 (rshift (lshift @0 INTEGER_CST@1) @1)
1371 (if (TYPE_UNSIGNED (type)
1372 && (wi::ltu_p (@1, element_precision (type))))
1373 (bit_and @0 (rshift { build_minus_one_cst (type); } @1))))
1375 (for shiftrotate (lrotate rrotate lshift rshift)
1377 (shiftrotate @0 integer_zerop)
1380 (shiftrotate integer_zerop@0 @1)
1382 /* Prefer vector1 << scalar to vector1 << vector2
1383 if vector2 is uniform. */
1384 (for vec (VECTOR_CST CONSTRUCTOR)
1386 (shiftrotate @0 vec@1)
1387 (with { tree tem = uniform_vector_p (@1); }
1389 (shiftrotate @0 { tem; }))))))
1391 /* Rewrite an LROTATE_EXPR by a constant into an
1392 RROTATE_EXPR by a new constant. */
1394 (lrotate @0 INTEGER_CST@1)
1395 (rrotate @0 { const_binop (MINUS_EXPR, TREE_TYPE (@1),
1396 build_int_cst (TREE_TYPE (@1),
1397 element_precision (type)), @1); }))
1399 /* Turn (a OP c1) OP c2 into a OP (c1+c2). */
1400 (for op (lrotate rrotate rshift lshift)
1402 (op (op @0 INTEGER_CST@1) INTEGER_CST@2)
1403 (with { unsigned int prec = element_precision (type); }
1404 (if (wi::ge_p (@1, 0, TYPE_SIGN (TREE_TYPE (@1)))
1405 && wi::lt_p (@1, prec, TYPE_SIGN (TREE_TYPE (@1)))
1406 && wi::ge_p (@2, 0, TYPE_SIGN (TREE_TYPE (@2)))
1407 && wi::lt_p (@2, prec, TYPE_SIGN (TREE_TYPE (@2))))
1408 (with { unsigned int low = wi::add (@1, @2).to_uhwi (); }
1409 /* Deal with a OP (c1 + c2) being undefined but (a OP c1) OP c2
1410 being well defined. */
1412 (if (op == LROTATE_EXPR || op == RROTATE_EXPR)
1413 (op @0 { build_int_cst (TREE_TYPE (@1), low % prec); })
1414 (if (TYPE_UNSIGNED (type) || op == LSHIFT_EXPR)
1415 { build_zero_cst (type); }
1416 (op @0 { build_int_cst (TREE_TYPE (@1), prec - 1); })))
1417 (op @0 { build_int_cst (TREE_TYPE (@1), low); })))))))
1420 /* ((1 << A) & 1) != 0 -> A == 0
1421 ((1 << A) & 1) == 0 -> A != 0 */
1425 (cmp (bit_and (lshift integer_onep @0) integer_onep) integer_zerop)
1426 (icmp @0 { build_zero_cst (TREE_TYPE (@0)); })))
1428 /* (CST1 << A) == CST2 -> A == ctz (CST2) - ctz (CST1)
1429 (CST1 << A) != CST2 -> A != ctz (CST2) - ctz (CST1)
1433 (cmp (lshift INTEGER_CST@0 @1) INTEGER_CST@2)
1434 (with { int cand = wi::ctz (@2) - wi::ctz (@0); }
1436 || (!integer_zerop (@2)
1437 && wi::ne_p (wi::lshift (@0, cand), @2)))
1438 { constant_boolean_node (cmp == NE_EXPR, type); }
1439 (if (!integer_zerop (@2)
1440 && wi::eq_p (wi::lshift (@0, cand), @2))
1441 (cmp @1 { build_int_cst (TREE_TYPE (@1), cand); }))))))
1443 /* Fold (X << C1) & C2 into (X << C1) & (C2 | ((1 << C1) - 1))
1444 (X >> C1) & C2 into (X >> C1) & (C2 | ~((type) -1 >> C1))
1445 if the new mask might be further optimized. */
1446 (for shift (lshift rshift)
1448 (bit_and (convert?:s@4 (shift:s@5 (convert1?@3 @0) INTEGER_CST@1))
1450 (if (tree_nop_conversion_p (TREE_TYPE (@4), TREE_TYPE (@5))
1451 && TYPE_PRECISION (type) <= HOST_BITS_PER_WIDE_INT
1452 && tree_fits_uhwi_p (@1)
1453 && tree_to_uhwi (@1) > 0
1454 && tree_to_uhwi (@1) < TYPE_PRECISION (type))
1457 unsigned int shiftc = tree_to_uhwi (@1);
1458 unsigned HOST_WIDE_INT mask = TREE_INT_CST_LOW (@2);
1459 unsigned HOST_WIDE_INT newmask, zerobits = 0;
1460 tree shift_type = TREE_TYPE (@3);
1463 if (shift == LSHIFT_EXPR)
1464 zerobits = ((HOST_WIDE_INT_1U << shiftc) - 1);
1465 else if (shift == RSHIFT_EXPR
1466 && (TYPE_PRECISION (shift_type)
1467 == GET_MODE_PRECISION (TYPE_MODE (shift_type))))
1469 prec = TYPE_PRECISION (TREE_TYPE (@3));
1471 /* See if more bits can be proven as zero because of
1474 && TYPE_UNSIGNED (TREE_TYPE (@0)))
1476 tree inner_type = TREE_TYPE (@0);
1477 if ((TYPE_PRECISION (inner_type)
1478 == GET_MODE_PRECISION (TYPE_MODE (inner_type)))
1479 && TYPE_PRECISION (inner_type) < prec)
1481 prec = TYPE_PRECISION (inner_type);
1482 /* See if we can shorten the right shift. */
1484 shift_type = inner_type;
1485 /* Otherwise X >> C1 is all zeros, so we'll optimize
1486 it into (X, 0) later on by making sure zerobits
1490 zerobits = HOST_WIDE_INT_M1U;
1493 zerobits >>= HOST_BITS_PER_WIDE_INT - shiftc;
1494 zerobits <<= prec - shiftc;
1496 /* For arithmetic shift if sign bit could be set, zerobits
1497 can contain actually sign bits, so no transformation is
1498 possible, unless MASK masks them all away. In that
1499 case the shift needs to be converted into logical shift. */
1500 if (!TYPE_UNSIGNED (TREE_TYPE (@3))
1501 && prec == TYPE_PRECISION (TREE_TYPE (@3)))
1503 if ((mask & zerobits) == 0)
1504 shift_type = unsigned_type_for (TREE_TYPE (@3));
1510 /* ((X << 16) & 0xff00) is (X, 0). */
1511 (if ((mask & zerobits) == mask)
1512 { build_int_cst (type, 0); }
1513 (with { newmask = mask | zerobits; }
1514 (if (newmask != mask && (newmask & (newmask + 1)) == 0)
1517 /* Only do the transformation if NEWMASK is some integer
1519 for (prec = BITS_PER_UNIT;
1520 prec < HOST_BITS_PER_WIDE_INT; prec <<= 1)
1521 if (newmask == (HOST_WIDE_INT_1U << prec) - 1)
1524 (if (prec < HOST_BITS_PER_WIDE_INT
1525 || newmask == HOST_WIDE_INT_M1U)
1527 { tree newmaskt = build_int_cst_type (TREE_TYPE (@2), newmask); }
1528 (if (!tree_int_cst_equal (newmaskt, @2))
1529 (if (shift_type != TREE_TYPE (@3))
1530 (bit_and (convert (shift:shift_type (convert @3) @1)) { newmaskt; })
1531 (bit_and @4 { newmaskt; })))))))))))))
1533 /* Fold (X {&,^,|} C2) << C1 into (X << C1) {&,^,|} (C2 << C1)
1534 (X {&,^,|} C2) >> C1 into (X >> C1) & (C2 >> C1). */
1535 (for shift (lshift rshift)
1536 (for bit_op (bit_and bit_xor bit_ior)
1538 (shift (convert?:s (bit_op:s @0 INTEGER_CST@2)) INTEGER_CST@1)
1539 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1540 (with { tree mask = int_const_binop (shift, fold_convert (type, @2), @1); }
1541 (bit_op (shift (convert @0) @1) { mask; }))))))
1543 /* ~(~X >> Y) -> X >> Y (for arithmetic shift). */
1545 (bit_not (convert1?:s (rshift:s (convert2?@0 (bit_not @1)) @2)))
1546 (if (!TYPE_UNSIGNED (TREE_TYPE (@0))
1547 && (element_precision (TREE_TYPE (@0))
1548 <= element_precision (TREE_TYPE (@1))
1549 || !TYPE_UNSIGNED (TREE_TYPE (@1))))
1551 { tree shift_type = TREE_TYPE (@0); }
1552 (convert (rshift (convert:shift_type @1) @2)))))
1554 /* ~(~X >>r Y) -> X >>r Y
1555 ~(~X <<r Y) -> X <<r Y */
1556 (for rotate (lrotate rrotate)
1558 (bit_not (convert1?:s (rotate:s (convert2?@0 (bit_not @1)) @2)))
1559 (if ((element_precision (TREE_TYPE (@0))
1560 <= element_precision (TREE_TYPE (@1))
1561 || !TYPE_UNSIGNED (TREE_TYPE (@1)))
1562 && (element_precision (type) <= element_precision (TREE_TYPE (@0))
1563 || !TYPE_UNSIGNED (TREE_TYPE (@0))))
1565 { tree rotate_type = TREE_TYPE (@0); }
1566 (convert (rotate (convert:rotate_type @1) @2))))))
1568 /* Simplifications of conversions. */
1570 /* Basic strip-useless-type-conversions / strip_nops. */
1571 (for cvt (convert view_convert float fix_trunc)
1574 (if ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@0)))
1575 || (GENERIC && type == TREE_TYPE (@0)))
1578 /* Contract view-conversions. */
1580 (view_convert (view_convert @0))
1583 /* For integral conversions with the same precision or pointer
1584 conversions use a NOP_EXPR instead. */
1587 (if ((INTEGRAL_TYPE_P (type) || POINTER_TYPE_P (type))
1588 && (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
1589 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (@0)))
1592 /* Strip inner integral conversions that do not change precision or size. */
1594 (view_convert (convert@0 @1))
1595 (if ((INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
1596 && (INTEGRAL_TYPE_P (TREE_TYPE (@1)) || POINTER_TYPE_P (TREE_TYPE (@1)))
1597 && (TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
1598 && (TYPE_SIZE (TREE_TYPE (@0)) == TYPE_SIZE (TREE_TYPE (@1))))
1601 /* Re-association barriers around constants and other re-association
1602 barriers can be removed. */
1604 (paren CONSTANT_CLASS_P@0)
1607 (paren (paren@1 @0))
1610 /* Handle cases of two conversions in a row. */
1611 (for ocvt (convert float fix_trunc)
1612 (for icvt (convert float)
1617 tree inside_type = TREE_TYPE (@0);
1618 tree inter_type = TREE_TYPE (@1);
1619 int inside_int = INTEGRAL_TYPE_P (inside_type);
1620 int inside_ptr = POINTER_TYPE_P (inside_type);
1621 int inside_float = FLOAT_TYPE_P (inside_type);
1622 int inside_vec = VECTOR_TYPE_P (inside_type);
1623 unsigned int inside_prec = TYPE_PRECISION (inside_type);
1624 int inside_unsignedp = TYPE_UNSIGNED (inside_type);
1625 int inter_int = INTEGRAL_TYPE_P (inter_type);
1626 int inter_ptr = POINTER_TYPE_P (inter_type);
1627 int inter_float = FLOAT_TYPE_P (inter_type);
1628 int inter_vec = VECTOR_TYPE_P (inter_type);
1629 unsigned int inter_prec = TYPE_PRECISION (inter_type);
1630 int inter_unsignedp = TYPE_UNSIGNED (inter_type);
1631 int final_int = INTEGRAL_TYPE_P (type);
1632 int final_ptr = POINTER_TYPE_P (type);
1633 int final_float = FLOAT_TYPE_P (type);
1634 int final_vec = VECTOR_TYPE_P (type);
1635 unsigned int final_prec = TYPE_PRECISION (type);
1636 int final_unsignedp = TYPE_UNSIGNED (type);
1639 /* In addition to the cases of two conversions in a row
1640 handled below, if we are converting something to its own
1641 type via an object of identical or wider precision, neither
1642 conversion is needed. */
1643 (if (((GIMPLE && useless_type_conversion_p (type, inside_type))
1645 && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (inside_type)))
1646 && (((inter_int || inter_ptr) && final_int)
1647 || (inter_float && final_float))
1648 && inter_prec >= final_prec)
1651 /* Likewise, if the intermediate and initial types are either both
1652 float or both integer, we don't need the middle conversion if the
1653 former is wider than the latter and doesn't change the signedness
1654 (for integers). Avoid this if the final type is a pointer since
1655 then we sometimes need the middle conversion. */
1656 (if (((inter_int && inside_int) || (inter_float && inside_float))
1657 && (final_int || final_float)
1658 && inter_prec >= inside_prec
1659 && (inter_float || inter_unsignedp == inside_unsignedp))
1662 /* If we have a sign-extension of a zero-extended value, we can
1663 replace that by a single zero-extension. Likewise if the
1664 final conversion does not change precision we can drop the
1665 intermediate conversion. */
1666 (if (inside_int && inter_int && final_int
1667 && ((inside_prec < inter_prec && inter_prec < final_prec
1668 && inside_unsignedp && !inter_unsignedp)
1669 || final_prec == inter_prec))
1672 /* Two conversions in a row are not needed unless:
1673 - some conversion is floating-point (overstrict for now), or
1674 - some conversion is a vector (overstrict for now), or
1675 - the intermediate type is narrower than both initial and
1677 - the intermediate type and innermost type differ in signedness,
1678 and the outermost type is wider than the intermediate, or
1679 - the initial type is a pointer type and the precisions of the
1680 intermediate and final types differ, or
1681 - the final type is a pointer type and the precisions of the
1682 initial and intermediate types differ. */
1683 (if (! inside_float && ! inter_float && ! final_float
1684 && ! inside_vec && ! inter_vec && ! final_vec
1685 && (inter_prec >= inside_prec || inter_prec >= final_prec)
1686 && ! (inside_int && inter_int
1687 && inter_unsignedp != inside_unsignedp
1688 && inter_prec < final_prec)
1689 && ((inter_unsignedp && inter_prec > inside_prec)
1690 == (final_unsignedp && final_prec > inter_prec))
1691 && ! (inside_ptr && inter_prec != final_prec)
1692 && ! (final_ptr && inside_prec != inter_prec))
1695 /* A truncation to an unsigned type (a zero-extension) should be
1696 canonicalized as bitwise and of a mask. */
1697 (if (GIMPLE /* PR70366: doing this in GENERIC breaks -Wconversion. */
1698 && final_int && inter_int && inside_int
1699 && final_prec == inside_prec
1700 && final_prec > inter_prec
1702 (convert (bit_and @0 { wide_int_to_tree
1704 wi::mask (inter_prec, false,
1705 TYPE_PRECISION (inside_type))); })))
1707 /* If we are converting an integer to a floating-point that can
1708 represent it exactly and back to an integer, we can skip the
1709 floating-point conversion. */
1710 (if (GIMPLE /* PR66211 */
1711 && inside_int && inter_float && final_int &&
1712 (unsigned) significand_size (TYPE_MODE (inter_type))
1713 >= inside_prec - !inside_unsignedp)
1716 /* If we have a narrowing conversion to an integral type that is fed by a
1717 BIT_AND_EXPR, we might be able to remove the BIT_AND_EXPR if it merely
1718 masks off bits outside the final type (and nothing else). */
1720 (convert (bit_and @0 INTEGER_CST@1))
1721 (if (INTEGRAL_TYPE_P (type)
1722 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
1723 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))
1724 && operand_equal_p (@1, build_low_bits_mask (TREE_TYPE (@1),
1725 TYPE_PRECISION (type)), 0))
1729 /* (X /[ex] A) * A -> X. */
1731 (mult (convert? (exact_div @0 @1)) @1)
1732 /* Look through a sign-changing conversion. */
1735 /* Canonicalization of binary operations. */
1737 /* Convert X + -C into X - C. */
1739 (plus @0 REAL_CST@1)
1740 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
1741 (with { tree tem = const_unop (NEGATE_EXPR, type, @1); }
1742 (if (!TREE_OVERFLOW (tem) || !flag_trapping_math)
1743 (minus @0 { tem; })))))
1745 /* Convert x+x into x*2. */
1748 (if (SCALAR_FLOAT_TYPE_P (type))
1749 (mult @0 { build_real (type, dconst2); })
1750 (if (INTEGRAL_TYPE_P (type))
1751 (mult @0 { build_int_cst (type, 2); }))))
1754 (minus integer_zerop @1)
1757 /* (ARG0 - ARG1) is the same as (-ARG1 + ARG0). So check whether
1758 ARG0 is zero and X + ARG0 reduces to X, since that would mean
1759 (-ARG1 + ARG0) reduces to -ARG1. */
1761 (minus real_zerop@0 @1)
1762 (if (fold_real_zero_addition_p (type, @0, 0))
1765 /* Transform x * -1 into -x. */
1767 (mult @0 integer_minus_onep)
1770 /* True if we can easily extract the real and imaginary parts of a complex
1772 (match compositional_complex
1773 (convert? (complex @0 @1)))
1775 /* COMPLEX_EXPR and REALPART/IMAGPART_EXPR cancellations. */
1777 (complex (realpart @0) (imagpart @0))
1780 (realpart (complex @0 @1))
1783 (imagpart (complex @0 @1))
1786 /* Sometimes we only care about half of a complex expression. */
1788 (realpart (convert?:s (conj:s @0)))
1789 (convert (realpart @0)))
1791 (imagpart (convert?:s (conj:s @0)))
1792 (convert (negate (imagpart @0))))
1793 (for part (realpart imagpart)
1794 (for op (plus minus)
1796 (part (convert?:s@2 (op:s @0 @1)))
1797 (convert (op (part @0) (part @1))))))
1799 (realpart (convert?:s (CEXPI:s @0)))
1802 (imagpart (convert?:s (CEXPI:s @0)))
1805 /* conj(conj(x)) -> x */
1807 (conj (convert? (conj @0)))
1808 (if (tree_nop_conversion_p (TREE_TYPE (@0), type))
1811 /* conj({x,y}) -> {x,-y} */
1813 (conj (convert?:s (complex:s @0 @1)))
1814 (with { tree itype = TREE_TYPE (type); }
1815 (complex (convert:itype @0) (negate (convert:itype @1)))))
1817 /* BSWAP simplifications, transforms checked by gcc.dg/builtin-bswap-8.c. */
1818 (for bswap (BUILT_IN_BSWAP16 BUILT_IN_BSWAP32 BUILT_IN_BSWAP64)
1823 (bswap (bit_not (bswap @0)))
1825 (for bitop (bit_xor bit_ior bit_and)
1827 (bswap (bitop:c (bswap @0) @1))
1828 (bitop @0 (bswap @1)))))
1831 /* Combine COND_EXPRs and VEC_COND_EXPRs. */
1833 /* Simplify constant conditions.
1834 Only optimize constant conditions when the selected branch
1835 has the same type as the COND_EXPR. This avoids optimizing
1836 away "c ? x : throw", where the throw has a void type.
1837 Note that we cannot throw away the fold-const.c variant nor
1838 this one as we depend on doing this transform before possibly
1839 A ? B : B -> B triggers and the fold-const.c one can optimize
1840 0 ? A : B to B even if A has side-effects. Something
1841 genmatch cannot handle. */
1843 (cond INTEGER_CST@0 @1 @2)
1844 (if (integer_zerop (@0))
1845 (if (!VOID_TYPE_P (TREE_TYPE (@2)) || VOID_TYPE_P (type))
1847 (if (!VOID_TYPE_P (TREE_TYPE (@1)) || VOID_TYPE_P (type))
1850 (vec_cond VECTOR_CST@0 @1 @2)
1851 (if (integer_all_onesp (@0))
1853 (if (integer_zerop (@0))
1856 (for cnd (cond vec_cond)
1857 /* A ? B : (A ? X : C) -> A ? B : C. */
1859 (cnd @0 (cnd @0 @1 @2) @3)
1862 (cnd @0 @1 (cnd @0 @2 @3))
1864 /* A ? B : (!A ? C : X) -> A ? B : C. */
1865 /* ??? This matches embedded conditions open-coded because genmatch
1866 would generate matching code for conditions in separate stmts only.
1867 The following is still important to merge then and else arm cases
1868 from if-conversion. */
1870 (cnd @0 @1 (cnd @2 @3 @4))
1871 (if (COMPARISON_CLASS_P (@0)
1872 && COMPARISON_CLASS_P (@2)
1873 && invert_tree_comparison
1874 (TREE_CODE (@0), HONOR_NANS (TREE_OPERAND (@0, 0))) == TREE_CODE (@2)
1875 && operand_equal_p (TREE_OPERAND (@0, 0), TREE_OPERAND (@2, 0), 0)
1876 && operand_equal_p (TREE_OPERAND (@0, 1), TREE_OPERAND (@2, 1), 0))
1879 (cnd @0 (cnd @1 @2 @3) @4)
1880 (if (COMPARISON_CLASS_P (@0)
1881 && COMPARISON_CLASS_P (@1)
1882 && invert_tree_comparison
1883 (TREE_CODE (@0), HONOR_NANS (TREE_OPERAND (@0, 0))) == TREE_CODE (@1)
1884 && operand_equal_p (TREE_OPERAND (@0, 0), TREE_OPERAND (@1, 0), 0)
1885 && operand_equal_p (TREE_OPERAND (@0, 1), TREE_OPERAND (@1, 1), 0))
1888 /* A ? B : B -> B. */
1893 /* !A ? B : C -> A ? C : B. */
1895 (cnd (logical_inverted_value truth_valued_p@0) @1 @2)
1898 /* A + (B vcmp C ? 1 : 0) -> A - (B vcmp C ? -1 : 0), since vector comparisons
1899 return all -1 or all 0 results. */
1900 /* ??? We could instead convert all instances of the vec_cond to negate,
1901 but that isn't necessarily a win on its own. */
1903 (plus:c @3 (view_convert? (vec_cond:s @0 integer_each_onep@1 integer_zerop@2)))
1904 (if (VECTOR_TYPE_P (type)
1905 && TYPE_VECTOR_SUBPARTS (type) == TYPE_VECTOR_SUBPARTS (TREE_TYPE (@1))
1906 && (TYPE_MODE (TREE_TYPE (type))
1907 == TYPE_MODE (TREE_TYPE (TREE_TYPE (@1)))))
1908 (minus @3 (view_convert (vec_cond @0 (negate @1) @2)))))
1910 /* ... likewise A - (B vcmp C ? 1 : 0) -> A + (B vcmp C ? -1 : 0). */
1912 (minus @3 (view_convert? (vec_cond:s @0 integer_each_onep@1 integer_zerop@2)))
1913 (if (VECTOR_TYPE_P (type)
1914 && TYPE_VECTOR_SUBPARTS (type) == TYPE_VECTOR_SUBPARTS (TREE_TYPE (@1))
1915 && (TYPE_MODE (TREE_TYPE (type))
1916 == TYPE_MODE (TREE_TYPE (TREE_TYPE (@1)))))
1917 (plus @3 (view_convert (vec_cond @0 (negate @1) @2)))))
1920 /* Simplifications of comparisons. */
1922 /* See if we can reduce the magnitude of a constant involved in a
1923 comparison by changing the comparison code. This is a canonicalization
1924 formerly done by maybe_canonicalize_comparison_1. */
1928 (cmp @0 INTEGER_CST@1)
1929 (if (tree_int_cst_sgn (@1) == -1)
1930 (acmp @0 { wide_int_to_tree (TREE_TYPE (@1), wi::add (@1, 1)); }))))
1934 (cmp @0 INTEGER_CST@1)
1935 (if (tree_int_cst_sgn (@1) == 1)
1936 (acmp @0 { wide_int_to_tree (TREE_TYPE (@1), wi::sub (@1, 1)); }))))
1939 /* We can simplify a logical negation of a comparison to the
1940 inverted comparison. As we cannot compute an expression
1941 operator using invert_tree_comparison we have to simulate
1942 that with expression code iteration. */
1943 (for cmp (tcc_comparison)
1944 icmp (inverted_tcc_comparison)
1945 ncmp (inverted_tcc_comparison_with_nans)
1946 /* Ideally we'd like to combine the following two patterns
1947 and handle some more cases by using
1948 (logical_inverted_value (cmp @0 @1))
1949 here but for that genmatch would need to "inline" that.
1950 For now implement what forward_propagate_comparison did. */
1952 (bit_not (cmp @0 @1))
1953 (if (VECTOR_TYPE_P (type)
1954 || (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1))
1955 /* Comparison inversion may be impossible for trapping math,
1956 invert_tree_comparison will tell us. But we can't use
1957 a computed operator in the replacement tree thus we have
1958 to play the trick below. */
1959 (with { enum tree_code ic = invert_tree_comparison
1960 (cmp, HONOR_NANS (@0)); }
1966 (bit_xor (cmp @0 @1) integer_truep)
1967 (with { enum tree_code ic = invert_tree_comparison
1968 (cmp, HONOR_NANS (@0)); }
1974 /* Transform comparisons of the form X - Y CMP 0 to X CMP Y.
1975 ??? The transformation is valid for the other operators if overflow
1976 is undefined for the type, but performing it here badly interacts
1977 with the transformation in fold_cond_expr_with_comparison which
1978 attempts to synthetize ABS_EXPR. */
1981 (cmp (minus@2 @0 @1) integer_zerop)
1982 (if (single_use (@2))
1985 /* Transform comparisons of the form X * C1 CMP 0 to X CMP 0 in the
1986 signed arithmetic case. That form is created by the compiler
1987 often enough for folding it to be of value. One example is in
1988 computing loop trip counts after Operator Strength Reduction. */
1989 (for cmp (simple_comparison)
1990 scmp (swapped_simple_comparison)
1992 (cmp (mult@3 @0 INTEGER_CST@1) integer_zerop@2)
1993 /* Handle unfolded multiplication by zero. */
1994 (if (integer_zerop (@1))
1996 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1997 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1999 /* If @1 is negative we swap the sense of the comparison. */
2000 (if (tree_int_cst_sgn (@1) < 0)
2004 /* Simplify comparison of something with itself. For IEEE
2005 floating-point, we can only do some of these simplifications. */
2009 (if (! FLOAT_TYPE_P (TREE_TYPE (@0))
2010 || ! HONOR_NANS (@0))
2011 { constant_boolean_node (true, type); }
2012 (if (cmp != EQ_EXPR)
2018 || ! FLOAT_TYPE_P (TREE_TYPE (@0))
2019 || ! HONOR_NANS (@0))
2020 { constant_boolean_node (false, type); })))
2021 (for cmp (unle unge uneq)
2024 { constant_boolean_node (true, type); }))
2025 (for cmp (unlt ungt)
2031 (if (!flag_trapping_math)
2032 { constant_boolean_node (false, type); }))
2034 /* Fold ~X op ~Y as Y op X. */
2035 (for cmp (simple_comparison)
2037 (cmp (bit_not@2 @0) (bit_not@3 @1))
2038 (if (single_use (@2) && single_use (@3))
2041 /* Fold ~X op C as X op' ~C, where op' is the swapped comparison. */
2042 (for cmp (simple_comparison)
2043 scmp (swapped_simple_comparison)
2045 (cmp (bit_not@2 @0) CONSTANT_CLASS_P@1)
2046 (if (single_use (@2)
2047 && (TREE_CODE (@1) == INTEGER_CST || TREE_CODE (@1) == VECTOR_CST))
2048 (scmp @0 (bit_not @1)))))
2050 (for cmp (simple_comparison)
2051 /* Fold (double)float1 CMP (double)float2 into float1 CMP float2. */
2053 (cmp (convert@2 @0) (convert? @1))
2054 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
2055 && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@2))
2056 == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0)))
2057 && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@2))
2058 == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1))))
2061 tree type1 = TREE_TYPE (@1);
2062 if (TREE_CODE (@1) == REAL_CST && !DECIMAL_FLOAT_TYPE_P (type1))
2064 REAL_VALUE_TYPE orig = TREE_REAL_CST (@1);
2065 if (TYPE_PRECISION (type1) > TYPE_PRECISION (float_type_node)
2066 && exact_real_truncate (TYPE_MODE (float_type_node), &orig))
2067 type1 = float_type_node;
2068 if (TYPE_PRECISION (type1) > TYPE_PRECISION (double_type_node)
2069 && exact_real_truncate (TYPE_MODE (double_type_node), &orig))
2070 type1 = double_type_node;
2073 = (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type1)
2074 ? TREE_TYPE (@0) : type1);
2076 (if (TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (newtype))
2077 (cmp (convert:newtype @0) (convert:newtype @1))))))
2081 /* IEEE doesn't distinguish +0 and -0 in comparisons. */
2083 /* a CMP (-0) -> a CMP 0 */
2084 (if (REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@1)))
2085 (cmp @0 { build_real (TREE_TYPE (@1), dconst0); }))
2086 /* x != NaN is always true, other ops are always false. */
2087 (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
2088 && ! HONOR_SNANS (@1))
2089 { constant_boolean_node (cmp == NE_EXPR, type); })
2090 /* Fold comparisons against infinity. */
2091 (if (REAL_VALUE_ISINF (TREE_REAL_CST (@1))
2092 && MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (@1))))
2095 REAL_VALUE_TYPE max;
2096 enum tree_code code = cmp;
2097 bool neg = REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1));
2099 code = swap_tree_comparison (code);
2102 /* x > +Inf is always false, if with ignore sNANs. */
2103 (if (code == GT_EXPR
2104 && ! HONOR_SNANS (@0))
2105 { constant_boolean_node (false, type); })
2106 (if (code == LE_EXPR)
2107 /* x <= +Inf is always true, if we don't case about NaNs. */
2108 (if (! HONOR_NANS (@0))
2109 { constant_boolean_node (true, type); }
2110 /* x <= +Inf is the same as x == x, i.e. !isnan(x). */
2112 /* x == +Inf and x >= +Inf are always equal to x > DBL_MAX. */
2113 (if (code == EQ_EXPR || code == GE_EXPR)
2114 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
2116 (lt @0 { build_real (TREE_TYPE (@0), max); })
2117 (gt @0 { build_real (TREE_TYPE (@0), max); }))))
2118 /* x < +Inf is always equal to x <= DBL_MAX. */
2119 (if (code == LT_EXPR)
2120 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
2122 (ge @0 { build_real (TREE_TYPE (@0), max); })
2123 (le @0 { build_real (TREE_TYPE (@0), max); }))))
2124 /* x != +Inf is always equal to !(x > DBL_MAX). */
2125 (if (code == NE_EXPR)
2126 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
2127 (if (! HONOR_NANS (@0))
2129 (ge @0 { build_real (TREE_TYPE (@0), max); })
2130 (le @0 { build_real (TREE_TYPE (@0), max); }))
2132 (bit_xor (lt @0 { build_real (TREE_TYPE (@0), max); })
2133 { build_one_cst (type); })
2134 (bit_xor (gt @0 { build_real (TREE_TYPE (@0), max); })
2135 { build_one_cst (type); }))))))))))
2137 /* If this is a comparison of a real constant with a PLUS_EXPR
2138 or a MINUS_EXPR of a real constant, we can convert it into a
2139 comparison with a revised real constant as long as no overflow
2140 occurs when unsafe_math_optimizations are enabled. */
2141 (if (flag_unsafe_math_optimizations)
2142 (for op (plus minus)
2144 (cmp (op @0 REAL_CST@1) REAL_CST@2)
2147 tree tem = const_binop (op == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR,
2148 TREE_TYPE (@1), @2, @1);
2150 (if (tem && !TREE_OVERFLOW (tem))
2151 (cmp @0 { tem; }))))))
2153 /* Likewise, we can simplify a comparison of a real constant with
2154 a MINUS_EXPR whose first operand is also a real constant, i.e.
2155 (c1 - x) < c2 becomes x > c1-c2. Reordering is allowed on
2156 floating-point types only if -fassociative-math is set. */
2157 (if (flag_associative_math)
2159 (cmp (minus REAL_CST@0 @1) REAL_CST@2)
2160 (with { tree tem = const_binop (MINUS_EXPR, TREE_TYPE (@1), @0, @2); }
2161 (if (tem && !TREE_OVERFLOW (tem))
2162 (cmp { tem; } @1)))))
2164 /* Fold comparisons against built-in math functions. */
2165 (if (flag_unsafe_math_optimizations
2166 && ! flag_errno_math)
2169 (cmp (sq @0) REAL_CST@1)
2171 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
2173 /* sqrt(x) < y is always false, if y is negative. */
2174 (if (cmp == EQ_EXPR || cmp == LT_EXPR || cmp == LE_EXPR)
2175 { constant_boolean_node (false, type); })
2176 /* sqrt(x) > y is always true, if y is negative and we
2177 don't care about NaNs, i.e. negative values of x. */
2178 (if (cmp == NE_EXPR || !HONOR_NANS (@0))
2179 { constant_boolean_node (true, type); })
2180 /* sqrt(x) > y is the same as x >= 0, if y is negative. */
2181 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })))
2182 (if (real_equal (TREE_REAL_CST_PTR (@1), &dconst0))
2184 /* sqrt(x) < 0 is always false. */
2185 (if (cmp == LT_EXPR)
2186 { constant_boolean_node (false, type); })
2187 /* sqrt(x) >= 0 is always true if we don't care about NaNs. */
2188 (if (cmp == GE_EXPR && !HONOR_NANS (@0))
2189 { constant_boolean_node (true, type); })
2190 /* sqrt(x) <= 0 -> x == 0. */
2191 (if (cmp == LE_EXPR)
2193 /* Otherwise sqrt(x) cmp 0 -> x cmp 0. Here cmp can be >=, >,
2194 == or !=. In the last case:
2196 (sqrt(x) != 0) == (NaN != 0) == true == (x != 0)
2198 if x is negative or NaN. Due to -funsafe-math-optimizations,
2199 the results for other x follow from natural arithmetic. */
2201 (if (cmp == GT_EXPR || cmp == GE_EXPR)
2205 real_arithmetic (&c2, MULT_EXPR,
2206 &TREE_REAL_CST (@1), &TREE_REAL_CST (@1));
2207 real_convert (&c2, TYPE_MODE (TREE_TYPE (@0)), &c2);
2209 (if (REAL_VALUE_ISINF (c2))
2210 /* sqrt(x) > y is x == +Inf, when y is very large. */
2211 (if (HONOR_INFINITIES (@0))
2212 (eq @0 { build_real (TREE_TYPE (@0), c2); })
2213 { constant_boolean_node (false, type); })
2214 /* sqrt(x) > c is the same as x > c*c. */
2215 (cmp @0 { build_real (TREE_TYPE (@0), c2); }))))
2216 (if (cmp == LT_EXPR || cmp == LE_EXPR)
2220 real_arithmetic (&c2, MULT_EXPR,
2221 &TREE_REAL_CST (@1), &TREE_REAL_CST (@1));
2222 real_convert (&c2, TYPE_MODE (TREE_TYPE (@0)), &c2);
2224 (if (REAL_VALUE_ISINF (c2))
2226 /* sqrt(x) < y is always true, when y is a very large
2227 value and we don't care about NaNs or Infinities. */
2228 (if (! HONOR_NANS (@0) && ! HONOR_INFINITIES (@0))
2229 { constant_boolean_node (true, type); })
2230 /* sqrt(x) < y is x != +Inf when y is very large and we
2231 don't care about NaNs. */
2232 (if (! HONOR_NANS (@0))
2233 (ne @0 { build_real (TREE_TYPE (@0), c2); }))
2234 /* sqrt(x) < y is x >= 0 when y is very large and we
2235 don't care about Infinities. */
2236 (if (! HONOR_INFINITIES (@0))
2237 (ge @0 { build_real (TREE_TYPE (@0), dconst0); }))
2238 /* sqrt(x) < y is x >= 0 && x != +Inf, when y is large. */
2241 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
2242 (ne @0 { build_real (TREE_TYPE (@0), c2); }))))
2243 /* sqrt(x) < c is the same as x < c*c, if we ignore NaNs. */
2244 (if (! HONOR_NANS (@0))
2245 (cmp @0 { build_real (TREE_TYPE (@0), c2); })
2246 /* sqrt(x) < c is the same as x >= 0 && x < c*c. */
2249 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
2250 (cmp @0 { build_real (TREE_TYPE (@0), c2); }))))))))))))
2252 /* Unordered tests if either argument is a NaN. */
2254 (bit_ior (unordered @0 @0) (unordered @1 @1))
2255 (if (types_match (@0, @1))
2258 (bit_and (ordered @0 @0) (ordered @1 @1))
2259 (if (types_match (@0, @1))
2262 (bit_ior:c (unordered @0 @0) (unordered:c@2 @0 @1))
2265 (bit_and:c (ordered @0 @0) (ordered:c@2 @0 @1))
2268 /* Simple range test simplifications. */
2269 /* A < B || A >= B -> true. */
2270 (for test1 (lt le le le ne ge)
2271 test2 (ge gt ge ne eq ne)
2273 (bit_ior:c (test1 @0 @1) (test2 @0 @1))
2274 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2275 || VECTOR_INTEGER_TYPE_P (TREE_TYPE (@0)))
2276 { constant_boolean_node (true, type); })))
2277 /* A < B && A >= B -> false. */
2278 (for test1 (lt lt lt le ne eq)
2279 test2 (ge gt eq gt eq gt)
2281 (bit_and:c (test1 @0 @1) (test2 @0 @1))
2282 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2283 || VECTOR_INTEGER_TYPE_P (TREE_TYPE (@0)))
2284 { constant_boolean_node (false, type); })))
2286 /* -A CMP -B -> B CMP A. */
2287 (for cmp (tcc_comparison)
2288 scmp (swapped_tcc_comparison)
2290 (cmp (negate @0) (negate @1))
2291 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
2292 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2293 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
2296 (cmp (negate @0) CONSTANT_CLASS_P@1)
2297 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
2298 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2299 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
2300 (with { tree tem = const_unop (NEGATE_EXPR, TREE_TYPE (@0), @1); }
2301 (if (tem && !TREE_OVERFLOW (tem))
2302 (scmp @0 { tem; }))))))
2304 /* Convert ABS_EXPR<x> == 0 or ABS_EXPR<x> != 0 to x == 0 or x != 0. */
2307 (op (abs @0) zerop@1)
2310 /* From fold_sign_changed_comparison and fold_widened_comparison. */
2311 (for cmp (simple_comparison)
2313 (cmp (convert@0 @00) (convert?@1 @10))
2314 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2315 /* Disable this optimization if we're casting a function pointer
2316 type on targets that require function pointer canonicalization. */
2317 && !(targetm.have_canonicalize_funcptr_for_compare ()
2318 && TREE_CODE (TREE_TYPE (@00)) == POINTER_TYPE
2319 && TREE_CODE (TREE_TYPE (TREE_TYPE (@00))) == FUNCTION_TYPE)
2321 (if (TYPE_PRECISION (TREE_TYPE (@00)) == TYPE_PRECISION (TREE_TYPE (@0))
2322 && (TREE_CODE (@10) == INTEGER_CST
2323 || (@1 != @10 && types_match (TREE_TYPE (@10), TREE_TYPE (@00))))
2324 && (TYPE_UNSIGNED (TREE_TYPE (@00)) == TYPE_UNSIGNED (TREE_TYPE (@0))
2327 && (POINTER_TYPE_P (TREE_TYPE (@00)) == POINTER_TYPE_P (TREE_TYPE (@0))))
2328 /* ??? The special-casing of INTEGER_CST conversion was in the original
2329 code and here to avoid a spurious overflow flag on the resulting
2330 constant which fold_convert produces. */
2331 (if (TREE_CODE (@1) == INTEGER_CST)
2332 (cmp @00 { force_fit_type (TREE_TYPE (@00), wi::to_widest (@1), 0,
2333 TREE_OVERFLOW (@1)); })
2334 (cmp @00 (convert @1)))
2336 (if (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (TREE_TYPE (@00)))
2337 /* If possible, express the comparison in the shorter mode. */
2338 (if ((cmp == EQ_EXPR || cmp == NE_EXPR
2339 || TYPE_UNSIGNED (TREE_TYPE (@0)) == TYPE_UNSIGNED (TREE_TYPE (@00)))
2340 && (types_match (TREE_TYPE (@10), TREE_TYPE (@00))
2341 || ((TYPE_PRECISION (TREE_TYPE (@00))
2342 >= TYPE_PRECISION (TREE_TYPE (@10)))
2343 && (TYPE_UNSIGNED (TREE_TYPE (@00))
2344 == TYPE_UNSIGNED (TREE_TYPE (@10))))
2345 || (TREE_CODE (@10) == INTEGER_CST
2346 && INTEGRAL_TYPE_P (TREE_TYPE (@00))
2347 && int_fits_type_p (@10, TREE_TYPE (@00)))))
2348 (cmp @00 (convert @10))
2349 (if (TREE_CODE (@10) == INTEGER_CST
2350 && INTEGRAL_TYPE_P (TREE_TYPE (@00))
2351 && !int_fits_type_p (@10, TREE_TYPE (@00)))
2354 tree min = lower_bound_in_type (TREE_TYPE (@10), TREE_TYPE (@00));
2355 tree max = upper_bound_in_type (TREE_TYPE (@10), TREE_TYPE (@00));
2356 bool above = integer_nonzerop (const_binop (LT_EXPR, type, max, @10));
2357 bool below = integer_nonzerop (const_binop (LT_EXPR, type, @10, min));
2359 (if (above || below)
2360 (if (cmp == EQ_EXPR || cmp == NE_EXPR)
2361 { constant_boolean_node (cmp == EQ_EXPR ? false : true, type); }
2362 (if (cmp == LT_EXPR || cmp == LE_EXPR)
2363 { constant_boolean_node (above ? true : false, type); }
2364 (if (cmp == GT_EXPR || cmp == GE_EXPR)
2365 { constant_boolean_node (above ? false : true, type); }))))))))))))
2368 /* A local variable can never be pointed to by
2369 the default SSA name of an incoming parameter.
2370 SSA names are canonicalized to 2nd place. */
2372 (cmp addr@0 SSA_NAME@1)
2373 (if (SSA_NAME_IS_DEFAULT_DEF (@1)
2374 && TREE_CODE (SSA_NAME_VAR (@1)) == PARM_DECL)
2375 (with { tree base = get_base_address (TREE_OPERAND (@0, 0)); }
2376 (if (TREE_CODE (base) == VAR_DECL
2377 && auto_var_in_fn_p (base, current_function_decl))
2378 (if (cmp == NE_EXPR)
2379 { constant_boolean_node (true, type); }
2380 { constant_boolean_node (false, type); }))))))
2382 /* Equality compare simplifications from fold_binary */
2385 /* If we have (A | C) == D where C & ~D != 0, convert this into 0.
2386 Similarly for NE_EXPR. */
2388 (cmp (convert?@3 (bit_ior @0 INTEGER_CST@1)) INTEGER_CST@2)
2389 (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0))
2390 && wi::bit_and_not (@1, @2) != 0)
2391 { constant_boolean_node (cmp == NE_EXPR, type); }))
2393 /* (X ^ Y) == 0 becomes X == Y, and (X ^ Y) != 0 becomes X != Y. */
2395 (cmp (bit_xor @0 @1) integer_zerop)
2398 /* (X ^ Y) == Y becomes X == 0.
2399 Likewise (X ^ Y) == X becomes Y == 0. */
2401 (cmp:c (bit_xor:c @0 @1) @0)
2402 (cmp @1 { build_zero_cst (TREE_TYPE (@1)); }))
2404 /* (X ^ C1) op C2 can be rewritten as X op (C1 ^ C2). */
2406 (cmp (convert?@3 (bit_xor @0 INTEGER_CST@1)) INTEGER_CST@2)
2407 (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0)))
2408 (cmp @0 (bit_xor @1 (convert @2)))))
2411 (cmp (convert? addr@0) integer_zerop)
2412 (if (tree_single_nonzero_warnv_p (@0, NULL))
2413 { constant_boolean_node (cmp == NE_EXPR, type); })))
2415 /* If we have (A & C) == C where C is a power of 2, convert this into
2416 (A & C) != 0. Similarly for NE_EXPR. */
2420 (cmp (bit_and@2 @0 integer_pow2p@1) @1)
2421 (icmp @2 { build_zero_cst (TREE_TYPE (@0)); })))
2423 /* If we have (A & C) != 0 where C is the sign bit of A, convert
2424 this into A < 0. Similarly for (A & C) == 0 into A >= 0. */
2428 (cmp (bit_and (convert?@2 @0) integer_pow2p@1) integer_zerop)
2429 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2430 && (TYPE_PRECISION (TREE_TYPE (@0))
2431 == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (@0))))
2432 && element_precision (@2) >= element_precision (@0)
2433 && wi::only_sign_bit_p (@1, element_precision (@0)))
2434 (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
2435 (ncmp (convert:stype @0) { build_zero_cst (stype); })))))
2437 /* When the addresses are not directly of decls compare base and offset.
2438 This implements some remaining parts of fold_comparison address
2439 comparisons but still no complete part of it. Still it is good
2440 enough to make fold_stmt not regress when not dispatching to fold_binary. */
2441 (for cmp (simple_comparison)
2443 (cmp (convert1?@2 addr@0) (convert2? addr@1))
2446 HOST_WIDE_INT off0, off1;
2447 tree base0 = get_addr_base_and_unit_offset (TREE_OPERAND (@0, 0), &off0);
2448 tree base1 = get_addr_base_and_unit_offset (TREE_OPERAND (@1, 0), &off1);
2449 if (base0 && TREE_CODE (base0) == MEM_REF)
2451 off0 += mem_ref_offset (base0).to_short_addr ();
2452 base0 = TREE_OPERAND (base0, 0);
2454 if (base1 && TREE_CODE (base1) == MEM_REF)
2456 off1 += mem_ref_offset (base1).to_short_addr ();
2457 base1 = TREE_OPERAND (base1, 0);
2460 (if (base0 && base1)
2464 if (decl_in_symtab_p (base0)
2465 && decl_in_symtab_p (base1))
2466 equal = symtab_node::get_create (base0)
2467 ->equal_address_to (symtab_node::get_create (base1));
2468 else if ((DECL_P (base0)
2469 || TREE_CODE (base0) == SSA_NAME
2470 || TREE_CODE (base0) == STRING_CST)
2472 || TREE_CODE (base1) == SSA_NAME
2473 || TREE_CODE (base1) == STRING_CST))
2474 equal = (base0 == base1);
2477 && (cmp == EQ_EXPR || cmp == NE_EXPR
2478 /* If the offsets are equal we can ignore overflow. */
2480 || POINTER_TYPE_OVERFLOW_UNDEFINED
2481 /* Or if we compare using pointers to decls or strings. */
2482 || (POINTER_TYPE_P (TREE_TYPE (@2))
2483 && (DECL_P (base0) || TREE_CODE (base0) == STRING_CST))))
2485 (if (cmp == EQ_EXPR)
2486 { constant_boolean_node (off0 == off1, type); })
2487 (if (cmp == NE_EXPR)
2488 { constant_boolean_node (off0 != off1, type); })
2489 (if (cmp == LT_EXPR)
2490 { constant_boolean_node (off0 < off1, type); })
2491 (if (cmp == LE_EXPR)
2492 { constant_boolean_node (off0 <= off1, type); })
2493 (if (cmp == GE_EXPR)
2494 { constant_boolean_node (off0 >= off1, type); })
2495 (if (cmp == GT_EXPR)
2496 { constant_boolean_node (off0 > off1, type); }))
2498 && DECL_P (base0) && DECL_P (base1)
2499 /* If we compare this as integers require equal offset. */
2500 && (!INTEGRAL_TYPE_P (TREE_TYPE (@2))
2503 (if (cmp == EQ_EXPR)
2504 { constant_boolean_node (false, type); })
2505 (if (cmp == NE_EXPR)
2506 { constant_boolean_node (true, type); })))))))))
2508 /* Simplify pointer equality compares using PTA. */
2512 (if (POINTER_TYPE_P (TREE_TYPE (@0))
2513 && ptrs_compare_unequal (@0, @1))
2514 { neeq == EQ_EXPR ? boolean_false_node : boolean_true_node; })))
2516 /* Non-equality compare simplifications from fold_binary */
2517 (for cmp (lt gt le ge)
2518 /* Comparisons with the highest or lowest possible integer of
2519 the specified precision will have known values. */
2521 (cmp (convert?@2 @0) INTEGER_CST@1)
2522 (if ((INTEGRAL_TYPE_P (TREE_TYPE (@1)) || POINTER_TYPE_P (TREE_TYPE (@1)))
2523 && tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@0)))
2526 tree arg1_type = TREE_TYPE (@1);
2527 unsigned int prec = TYPE_PRECISION (arg1_type);
2528 wide_int max = wi::max_value (arg1_type);
2529 wide_int signed_max = wi::max_value (prec, SIGNED);
2530 wide_int min = wi::min_value (arg1_type);
2533 (if (wi::eq_p (@1, max))
2535 (if (cmp == GT_EXPR)
2536 { constant_boolean_node (false, type); })
2537 (if (cmp == GE_EXPR)
2539 (if (cmp == LE_EXPR)
2540 { constant_boolean_node (true, type); })
2541 (if (cmp == LT_EXPR)
2543 (if (wi::eq_p (@1, min))
2545 (if (cmp == LT_EXPR)
2546 { constant_boolean_node (false, type); })
2547 (if (cmp == LE_EXPR)
2549 (if (cmp == GE_EXPR)
2550 { constant_boolean_node (true, type); })
2551 (if (cmp == GT_EXPR)
2553 (if (wi::eq_p (@1, max - 1))
2555 (if (cmp == GT_EXPR)
2556 (eq @2 { wide_int_to_tree (TREE_TYPE (@1), wi::add (@1, 1)); }))
2557 (if (cmp == LE_EXPR)
2558 (ne @2 { wide_int_to_tree (TREE_TYPE (@1), wi::add (@1, 1)); }))))
2559 (if (wi::eq_p (@1, min + 1))
2561 (if (cmp == GE_EXPR)
2562 (ne @2 { wide_int_to_tree (TREE_TYPE (@1), wi::sub (@1, 1)); }))
2563 (if (cmp == LT_EXPR)
2564 (eq @2 { wide_int_to_tree (TREE_TYPE (@1), wi::sub (@1, 1)); }))))
2565 (if (wi::eq_p (@1, signed_max)
2566 && TYPE_UNSIGNED (arg1_type)
2567 /* We will flip the signedness of the comparison operator
2568 associated with the mode of @1, so the sign bit is
2569 specified by this mode. Check that @1 is the signed
2570 max associated with this sign bit. */
2571 && prec == GET_MODE_PRECISION (TYPE_MODE (arg1_type))
2572 /* signed_type does not work on pointer types. */
2573 && INTEGRAL_TYPE_P (arg1_type))
2574 /* The following case also applies to X < signed_max+1
2575 and X >= signed_max+1 because previous transformations. */
2576 (if (cmp == LE_EXPR || cmp == GT_EXPR)
2577 (with { tree st = signed_type_for (arg1_type); }
2578 (if (cmp == LE_EXPR)
2579 (ge (convert:st @0) { build_zero_cst (st); })
2580 (lt (convert:st @0) { build_zero_cst (st); }))))))))))
2582 (for cmp (unordered ordered unlt unle ungt unge uneq ltgt)
2583 /* If the second operand is NaN, the result is constant. */
2586 (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
2587 && (cmp != LTGT_EXPR || ! flag_trapping_math))
2588 { constant_boolean_node (cmp == ORDERED_EXPR || cmp == LTGT_EXPR
2589 ? false : true, type); })))
2591 /* bool_var != 0 becomes bool_var. */
2593 (ne @0 integer_zerop)
2594 (if (TREE_CODE (TREE_TYPE (@0)) == BOOLEAN_TYPE
2595 && types_match (type, TREE_TYPE (@0)))
2597 /* bool_var == 1 becomes bool_var. */
2599 (eq @0 integer_onep)
2600 (if (TREE_CODE (TREE_TYPE (@0)) == BOOLEAN_TYPE
2601 && types_match (type, TREE_TYPE (@0)))
2604 bool_var == 0 becomes !bool_var or
2605 bool_var != 1 becomes !bool_var
2606 here because that only is good in assignment context as long
2607 as we require a tcc_comparison in GIMPLE_CONDs where we'd
2608 replace if (x == 0) with tem = ~x; if (tem != 0) which is
2609 clearly less optimal and which we'll transform again in forwprop. */
2611 /* When one argument is a constant, overflow detection can be simplified.
2612 Currently restricted to single use so as not to interfere too much with
2613 ADD_OVERFLOW detection in tree-ssa-math-opts.c.
2614 A + CST CMP A -> A CMP' CST' */
2615 (for cmp (lt le ge gt)
2618 (cmp:c (plus@2 @0 INTEGER_CST@1) @0)
2619 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
2620 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))
2623 (out @0 { wide_int_to_tree (TREE_TYPE (@0), wi::max_value
2624 (TYPE_PRECISION (TREE_TYPE (@0)), UNSIGNED) - @1); }))))
2626 /* To detect overflow in unsigned A - B, A < B is simpler than A - B > A.
2627 However, the detection logic for SUB_OVERFLOW in tree-ssa-math-opts.c
2628 expects the long form, so we restrict the transformation for now. */
2631 (cmp:c (minus@2 @0 @1) @0)
2632 (if (single_use (@2)
2633 && ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2634 && TYPE_UNSIGNED (TREE_TYPE (@0))
2635 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
2638 /* Testing for overflow is unnecessary if we already know the result. */
2643 (cmp:c (realpart (IFN_SUB_OVERFLOW@2 @0 @1)) @0)
2644 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
2645 && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
2646 (out (imagpart @2) { build_zero_cst (TREE_TYPE (@0)); }))))
2651 (cmp:c (realpart (IFN_ADD_OVERFLOW:c@2 @0 @1)) @0)
2652 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
2653 && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
2654 (out (imagpart @2) { build_zero_cst (TREE_TYPE (@0)); }))))
2656 /* For unsigned operands, -1 / B < A checks whether A * B would overflow.
2657 Simplify it to __builtin_mul_overflow (A, B, <unused>). */
2661 (cmp:c (trunc_div:s integer_all_onesp @1) @0)
2662 (if (TYPE_UNSIGNED (TREE_TYPE (@0)) && !VECTOR_TYPE_P (TREE_TYPE (@0)))
2663 (with { tree t = TREE_TYPE (@0), cpx = build_complex_type (t); }
2664 (out (imagpart (IFN_MUL_OVERFLOW:cpx @0 @1)) { build_zero_cst (t); })))))
2666 /* Simplification of math builtins. These rules must all be optimizations
2667 as well as IL simplifications. If there is a possibility that the new
2668 form could be a pessimization, the rule should go in the canonicalization
2669 section that follows this one.
2671 Rules can generally go in this section if they satisfy one of
2674 - the rule describes an identity
2676 - the rule replaces calls with something as simple as addition or
2679 - the rule contains unary calls only and simplifies the surrounding
2680 arithmetic. (The idea here is to exclude non-unary calls in which
2681 one operand is constant and in which the call is known to be cheap
2682 when the operand has that value.) */
2684 (if (flag_unsafe_math_optimizations)
2685 /* Simplify sqrt(x) * sqrt(x) -> x. */
2687 (mult (SQRT@1 @0) @1)
2688 (if (!HONOR_SNANS (type))
2691 /* Simplify sqrt(x) * sqrt(y) -> sqrt(x*y). */
2692 (for root (SQRT CBRT)
2694 (mult (root:s @0) (root:s @1))
2695 (root (mult @0 @1))))
2697 /* Simplify expN(x) * expN(y) -> expN(x+y). */
2698 (for exps (EXP EXP2 EXP10 POW10)
2700 (mult (exps:s @0) (exps:s @1))
2701 (exps (plus @0 @1))))
2703 /* Simplify a/root(b/c) into a*root(c/b). */
2704 (for root (SQRT CBRT)
2706 (rdiv @0 (root:s (rdiv:s @1 @2)))
2707 (mult @0 (root (rdiv @2 @1)))))
2709 /* Simplify x/expN(y) into x*expN(-y). */
2710 (for exps (EXP EXP2 EXP10 POW10)
2712 (rdiv @0 (exps:s @1))
2713 (mult @0 (exps (negate @1)))))
2715 (for logs (LOG LOG2 LOG10 LOG10)
2716 exps (EXP EXP2 EXP10 POW10)
2717 /* logN(expN(x)) -> x. */
2721 /* expN(logN(x)) -> x. */
2726 /* Optimize logN(func()) for various exponential functions. We
2727 want to determine the value "x" and the power "exponent" in
2728 order to transform logN(x**exponent) into exponent*logN(x). */
2729 (for logs (LOG LOG LOG LOG2 LOG2 LOG2 LOG10 LOG10)
2730 exps (EXP2 EXP10 POW10 EXP EXP10 POW10 EXP EXP2)
2733 (if (SCALAR_FLOAT_TYPE_P (type))
2739 /* Prepare to do logN(exp(exponent)) -> exponent*logN(e). */
2740 x = build_real_truncate (type, dconst_e ());
2743 /* Prepare to do logN(exp2(exponent)) -> exponent*logN(2). */
2744 x = build_real (type, dconst2);
2748 /* Prepare to do logN(exp10(exponent)) -> exponent*logN(10). */
2750 REAL_VALUE_TYPE dconst10;
2751 real_from_integer (&dconst10, VOIDmode, 10, SIGNED);
2752 x = build_real (type, dconst10);
2759 (mult (logs { x; }) @0)))))
2767 (if (SCALAR_FLOAT_TYPE_P (type))
2773 /* Prepare to do logN(sqrt(x)) -> 0.5*logN(x). */
2774 x = build_real (type, dconsthalf);
2777 /* Prepare to do logN(cbrt(x)) -> (1/3)*logN(x). */
2778 x = build_real_truncate (type, dconst_third ());
2784 (mult { x; } (logs @0))))))
2786 /* logN(pow(x,exponent)) -> exponent*logN(x). */
2787 (for logs (LOG LOG2 LOG10)
2791 (mult @1 (logs @0))))
2796 exps (EXP EXP2 EXP10 POW10)
2797 /* sqrt(expN(x)) -> expN(x*0.5). */
2800 (exps (mult @0 { build_real (type, dconsthalf); })))
2801 /* cbrt(expN(x)) -> expN(x/3). */
2804 (exps (mult @0 { build_real_truncate (type, dconst_third ()); })))
2805 /* pow(expN(x), y) -> expN(x*y). */
2808 (exps (mult @0 @1))))
2810 /* tan(atan(x)) -> x. */
2817 /* cabs(x+0i) or cabs(0+xi) -> abs(x). */
2819 (CABS (complex:C @0 real_zerop@1))
2822 /* trunc(trunc(x)) -> trunc(x), etc. */
2823 (for fns (TRUNC FLOOR CEIL ROUND NEARBYINT RINT)
2827 /* f(x) -> x if x is integer valued and f does nothing for such values. */
2828 (for fns (TRUNC FLOOR CEIL ROUND NEARBYINT RINT)
2830 (fns integer_valued_real_p@0)
2833 /* hypot(x,0) and hypot(0,x) -> abs(x). */
2835 (HYPOT:c @0 real_zerop@1)
2838 /* pow(1,x) -> 1. */
2840 (POW real_onep@0 @1)
2844 /* copysign(x,x) -> x. */
2849 /* copysign(x,y) -> fabs(x) if y is nonnegative. */
2850 (COPYSIGN @0 tree_expr_nonnegative_p@1)
2853 (for scale (LDEXP SCALBN SCALBLN)
2854 /* ldexp(0, x) -> 0. */
2856 (scale real_zerop@0 @1)
2858 /* ldexp(x, 0) -> x. */
2860 (scale @0 integer_zerop@1)
2862 /* ldexp(x, y) -> x if x is +-Inf or NaN. */
2864 (scale REAL_CST@0 @1)
2865 (if (!real_isfinite (TREE_REAL_CST_PTR (@0)))
2868 /* Canonicalization of sequences of math builtins. These rules represent
2869 IL simplifications but are not necessarily optimizations.
2871 The sincos pass is responsible for picking "optimal" implementations
2872 of math builtins, which may be more complicated and can sometimes go
2873 the other way, e.g. converting pow into a sequence of sqrts.
2874 We only want to do these canonicalizations before the pass has run. */
2876 (if (flag_unsafe_math_optimizations && canonicalize_math_p ())
2877 /* Simplify tan(x) * cos(x) -> sin(x). */
2879 (mult:c (TAN:s @0) (COS:s @0))
2882 /* Simplify x * pow(x,c) -> pow(x,c+1). */
2884 (mult:c @0 (POW:s @0 REAL_CST@1))
2885 (if (!TREE_OVERFLOW (@1))
2886 (POW @0 (plus @1 { build_one_cst (type); }))))
2888 /* Simplify sin(x) / cos(x) -> tan(x). */
2890 (rdiv (SIN:s @0) (COS:s @0))
2893 /* Simplify cos(x) / sin(x) -> 1 / tan(x). */
2895 (rdiv (COS:s @0) (SIN:s @0))
2896 (rdiv { build_one_cst (type); } (TAN @0)))
2898 /* Simplify sin(x) / tan(x) -> cos(x). */
2900 (rdiv (SIN:s @0) (TAN:s @0))
2901 (if (! HONOR_NANS (@0)
2902 && ! HONOR_INFINITIES (@0))
2905 /* Simplify tan(x) / sin(x) -> 1.0 / cos(x). */
2907 (rdiv (TAN:s @0) (SIN:s @0))
2908 (if (! HONOR_NANS (@0)
2909 && ! HONOR_INFINITIES (@0))
2910 (rdiv { build_one_cst (type); } (COS @0))))
2912 /* Simplify pow(x,y) * pow(x,z) -> pow(x,y+z). */
2914 (mult (POW:s @0 @1) (POW:s @0 @2))
2915 (POW @0 (plus @1 @2)))
2917 /* Simplify pow(x,y) * pow(z,y) -> pow(x*z,y). */
2919 (mult (POW:s @0 @1) (POW:s @2 @1))
2920 (POW (mult @0 @2) @1))
2922 /* Simplify powi(x,y) * powi(z,y) -> powi(x*z,y). */
2924 (mult (POWI:s @0 @1) (POWI:s @2 @1))
2925 (POWI (mult @0 @2) @1))
2927 /* Simplify pow(x,c) / x -> pow(x,c-1). */
2929 (rdiv (POW:s @0 REAL_CST@1) @0)
2930 (if (!TREE_OVERFLOW (@1))
2931 (POW @0 (minus @1 { build_one_cst (type); }))))
2933 /* Simplify x / pow (y,z) -> x * pow(y,-z). */
2935 (rdiv @0 (POW:s @1 @2))
2936 (mult @0 (POW @1 (negate @2))))
2941 /* sqrt(sqrt(x)) -> pow(x,1/4). */
2944 (pows @0 { build_real (type, dconst_quarter ()); }))
2945 /* sqrt(cbrt(x)) -> pow(x,1/6). */
2948 (pows @0 { build_real_truncate (type, dconst_sixth ()); }))
2949 /* cbrt(sqrt(x)) -> pow(x,1/6). */
2952 (pows @0 { build_real_truncate (type, dconst_sixth ()); }))
2953 /* cbrt(cbrt(x)) -> pow(x,1/9), iff x is nonnegative. */
2955 (cbrts (cbrts tree_expr_nonnegative_p@0))
2956 (pows @0 { build_real_truncate (type, dconst_ninth ()); }))
2957 /* sqrt(pow(x,y)) -> pow(|x|,y*0.5). */
2959 (sqrts (pows @0 @1))
2960 (pows (abs @0) (mult @1 { build_real (type, dconsthalf); })))
2961 /* cbrt(pow(x,y)) -> pow(x,y/3), iff x is nonnegative. */
2963 (cbrts (pows tree_expr_nonnegative_p@0 @1))
2964 (pows @0 (mult @1 { build_real_truncate (type, dconst_third ()); })))
2965 /* pow(sqrt(x),y) -> pow(x,y*0.5). */
2967 (pows (sqrts @0) @1)
2968 (pows @0 (mult @1 { build_real (type, dconsthalf); })))
2969 /* pow(cbrt(x),y) -> pow(x,y/3) iff x is nonnegative. */
2971 (pows (cbrts tree_expr_nonnegative_p@0) @1)
2972 (pows @0 (mult @1 { build_real_truncate (type, dconst_third ()); })))
2973 /* pow(pow(x,y),z) -> pow(x,y*z) iff x is nonnegative. */
2975 (pows (pows tree_expr_nonnegative_p@0 @1) @2)
2976 (pows @0 (mult @1 @2))))
2978 /* cabs(x+xi) -> fabs(x)*sqrt(2). */
2980 (CABS (complex @0 @0))
2981 (mult (abs @0) { build_real_truncate (type, dconst_sqrt2 ()); }))
2983 /* hypot(x,x) -> fabs(x)*sqrt(2). */
2986 (mult (abs @0) { build_real_truncate (type, dconst_sqrt2 ()); }))
2988 /* cexp(x+yi) -> exp(x)*cexpi(y). */
2993 (cexps compositional_complex@0)
2994 (if (targetm.libc_has_function (function_c99_math_complex))
2996 (mult (exps@1 (realpart @0)) (realpart (cexpis:type@2 (imagpart @0))))
2997 (mult @1 (imagpart @2)))))))
2999 (if (canonicalize_math_p ())
3000 /* floor(x) -> trunc(x) if x is nonnegative. */
3004 (floors tree_expr_nonnegative_p@0)
3007 (match double_value_p
3009 (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == double_type_node)))
3010 (for froms (BUILT_IN_TRUNCL
3022 /* truncl(extend(x)) -> extend(trunc(x)), etc., if x is a double. */
3023 (if (optimize && canonicalize_math_p ())
3025 (froms (convert double_value_p@0))
3026 (convert (tos @0)))))
3028 (match float_value_p
3030 (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == float_type_node)))
3031 (for froms (BUILT_IN_TRUNCL BUILT_IN_TRUNC
3032 BUILT_IN_FLOORL BUILT_IN_FLOOR
3033 BUILT_IN_CEILL BUILT_IN_CEIL
3034 BUILT_IN_ROUNDL BUILT_IN_ROUND
3035 BUILT_IN_NEARBYINTL BUILT_IN_NEARBYINT
3036 BUILT_IN_RINTL BUILT_IN_RINT)
3037 tos (BUILT_IN_TRUNCF BUILT_IN_TRUNCF
3038 BUILT_IN_FLOORF BUILT_IN_FLOORF
3039 BUILT_IN_CEILF BUILT_IN_CEILF
3040 BUILT_IN_ROUNDF BUILT_IN_ROUNDF
3041 BUILT_IN_NEARBYINTF BUILT_IN_NEARBYINTF
3042 BUILT_IN_RINTF BUILT_IN_RINTF)
3043 /* truncl(extend(x)) and trunc(extend(x)) -> extend(truncf(x)), etc.,
3045 (if (optimize && canonicalize_math_p ()
3046 && targetm.libc_has_function (function_c99_misc))
3048 (froms (convert float_value_p@0))
3049 (convert (tos @0)))))
3051 (for froms (XFLOORL XCEILL XROUNDL XRINTL)
3052 tos (XFLOOR XCEIL XROUND XRINT)
3053 /* llfloorl(extend(x)) -> llfloor(x), etc., if x is a double. */
3054 (if (optimize && canonicalize_math_p ())
3056 (froms (convert double_value_p@0))
3059 (for froms (XFLOORL XCEILL XROUNDL XRINTL
3060 XFLOOR XCEIL XROUND XRINT)
3061 tos (XFLOORF XCEILF XROUNDF XRINTF)
3062 /* llfloorl(extend(x)) and llfloor(extend(x)) -> llfloorf(x), etc.,
3064 (if (optimize && canonicalize_math_p ())
3066 (froms (convert float_value_p@0))
3069 (if (canonicalize_math_p ())
3070 /* xfloor(x) -> fix_trunc(x) if x is nonnegative. */
3071 (for floors (IFLOOR LFLOOR LLFLOOR)
3073 (floors tree_expr_nonnegative_p@0)
3076 (if (canonicalize_math_p ())
3077 /* xfloor(x) -> fix_trunc(x), etc., if x is integer valued. */
3078 (for fns (IFLOOR LFLOOR LLFLOOR
3080 IROUND LROUND LLROUND)
3082 (fns integer_valued_real_p@0)
3084 (if (!flag_errno_math)
3085 /* xrint(x) -> fix_trunc(x), etc., if x is integer valued. */
3086 (for rints (IRINT LRINT LLRINT)
3088 (rints integer_valued_real_p@0)
3091 (if (canonicalize_math_p ())
3092 (for ifn (IFLOOR ICEIL IROUND IRINT)
3093 lfn (LFLOOR LCEIL LROUND LRINT)
3094 llfn (LLFLOOR LLCEIL LLROUND LLRINT)
3095 /* Canonicalize iround (x) to lround (x) on ILP32 targets where
3096 sizeof (int) == sizeof (long). */
3097 (if (TYPE_PRECISION (integer_type_node)
3098 == TYPE_PRECISION (long_integer_type_node))
3101 (lfn:long_integer_type_node @0)))
3102 /* Canonicalize llround (x) to lround (x) on LP64 targets where
3103 sizeof (long long) == sizeof (long). */
3104 (if (TYPE_PRECISION (long_long_integer_type_node)
3105 == TYPE_PRECISION (long_integer_type_node))
3108 (lfn:long_integer_type_node @0)))))
3110 /* cproj(x) -> x if we're ignoring infinities. */
3113 (if (!HONOR_INFINITIES (type))
3116 /* If the real part is inf and the imag part is known to be
3117 nonnegative, return (inf + 0i). */
3119 (CPROJ (complex REAL_CST@0 tree_expr_nonnegative_p@1))
3120 (if (real_isinf (TREE_REAL_CST_PTR (@0)))
3121 { build_complex_inf (type, false); }))
3123 /* If the imag part is inf, return (inf+I*copysign(0,imag)). */
3125 (CPROJ (complex @0 REAL_CST@1))
3126 (if (real_isinf (TREE_REAL_CST_PTR (@1)))
3127 { build_complex_inf (type, TREE_REAL_CST_PTR (@1)->sign); }))
3133 (pows @0 REAL_CST@1)
3135 const REAL_VALUE_TYPE *value = TREE_REAL_CST_PTR (@1);
3136 REAL_VALUE_TYPE tmp;
3139 /* pow(x,0) -> 1. */
3140 (if (real_equal (value, &dconst0))
3141 { build_real (type, dconst1); })
3142 /* pow(x,1) -> x. */
3143 (if (real_equal (value, &dconst1))
3145 /* pow(x,-1) -> 1/x. */
3146 (if (real_equal (value, &dconstm1))
3147 (rdiv { build_real (type, dconst1); } @0))
3148 /* pow(x,0.5) -> sqrt(x). */
3149 (if (flag_unsafe_math_optimizations
3150 && canonicalize_math_p ()
3151 && real_equal (value, &dconsthalf))
3153 /* pow(x,1/3) -> cbrt(x). */
3154 (if (flag_unsafe_math_optimizations
3155 && canonicalize_math_p ()
3156 && (tmp = real_value_truncate (TYPE_MODE (type), dconst_third ()),
3157 real_equal (value, &tmp)))
3160 /* powi(1,x) -> 1. */
3162 (POWI real_onep@0 @1)
3166 (POWI @0 INTEGER_CST@1)
3168 /* powi(x,0) -> 1. */
3169 (if (wi::eq_p (@1, 0))
3170 { build_real (type, dconst1); })
3171 /* powi(x,1) -> x. */
3172 (if (wi::eq_p (@1, 1))
3174 /* powi(x,-1) -> 1/x. */
3175 (if (wi::eq_p (@1, -1))
3176 (rdiv { build_real (type, dconst1); } @0))))
3178 /* Narrowing of arithmetic and logical operations.
3180 These are conceptually similar to the transformations performed for
3181 the C/C++ front-ends by shorten_binary_op and shorten_compare. Long
3182 term we want to move all that code out of the front-ends into here. */
3184 /* If we have a narrowing conversion of an arithmetic operation where
3185 both operands are widening conversions from the same type as the outer
3186 narrowing conversion. Then convert the innermost operands to a suitable
3187 unsigned type (to avoid introducing undefined behavior), perform the
3188 operation and convert the result to the desired type. */
3189 (for op (plus minus)
3191 (convert (op:s (convert@2 @0) (convert@3 @1)))
3192 (if (INTEGRAL_TYPE_P (type)
3193 /* We check for type compatibility between @0 and @1 below,
3194 so there's no need to check that @1/@3 are integral types. */
3195 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
3196 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
3197 /* The precision of the type of each operand must match the
3198 precision of the mode of each operand, similarly for the
3200 && (TYPE_PRECISION (TREE_TYPE (@0))
3201 == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (@0))))
3202 && (TYPE_PRECISION (TREE_TYPE (@1))
3203 == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (@1))))
3204 && TYPE_PRECISION (type) == GET_MODE_PRECISION (TYPE_MODE (type))
3205 /* The inner conversion must be a widening conversion. */
3206 && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
3207 && types_match (@0, @1)
3208 && types_match (@0, type))
3209 (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
3210 (convert (op @0 @1))
3211 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
3212 (convert (op (convert:utype @0) (convert:utype @1))))))))
3214 /* This is another case of narrowing, specifically when there's an outer
3215 BIT_AND_EXPR which masks off bits outside the type of the innermost
3216 operands. Like the previous case we have to convert the operands
3217 to unsigned types to avoid introducing undefined behavior for the
3218 arithmetic operation. */
3219 (for op (minus plus)
3221 (bit_and (op:s (convert@2 @0) (convert@3 @1)) INTEGER_CST@4)
3222 (if (INTEGRAL_TYPE_P (type)
3223 /* We check for type compatibility between @0 and @1 below,
3224 so there's no need to check that @1/@3 are integral types. */
3225 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
3226 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
3227 /* The precision of the type of each operand must match the
3228 precision of the mode of each operand, similarly for the
3230 && (TYPE_PRECISION (TREE_TYPE (@0))
3231 == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (@0))))
3232 && (TYPE_PRECISION (TREE_TYPE (@1))
3233 == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (@1))))
3234 && TYPE_PRECISION (type) == GET_MODE_PRECISION (TYPE_MODE (type))
3235 /* The inner conversion must be a widening conversion. */
3236 && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
3237 && types_match (@0, @1)
3238 && (tree_int_cst_min_precision (@4, TYPE_SIGN (TREE_TYPE (@0)))
3239 <= TYPE_PRECISION (TREE_TYPE (@0)))
3240 && (wi::bit_and (@4, wi::mask (TYPE_PRECISION (TREE_TYPE (@0)),
3241 true, TYPE_PRECISION (type))) == 0))
3242 (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
3243 (with { tree ntype = TREE_TYPE (@0); }
3244 (convert (bit_and (op @0 @1) (convert:ntype @4))))
3245 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
3246 (convert (bit_and (op (convert:utype @0) (convert:utype @1))
3247 (convert:utype @4))))))))
3249 /* Transform (@0 < @1 and @0 < @2) to use min,
3250 (@0 > @1 and @0 > @2) to use max */
3251 (for op (lt le gt ge)
3252 ext (min min max max)
3254 (bit_and (op:cs @0 @1) (op:cs @0 @2))
3255 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
3256 && TREE_CODE (@0) != INTEGER_CST)
3257 (op @0 (ext @1 @2)))))
3260 /* signbit(x) -> 0 if x is nonnegative. */
3261 (SIGNBIT tree_expr_nonnegative_p@0)
3262 { integer_zero_node; })
3265 /* signbit(x) -> x<0 if x doesn't have signed zeros. */
3267 (if (!HONOR_SIGNED_ZEROS (@0))
3268 (convert (lt @0 { build_real (TREE_TYPE (@0), dconst0); }))))
3270 /* Transform comparisons of the form X +- C1 CMP C2 to X CMP C2 -+ C1. */
3272 (for op (plus minus)
3275 (cmp (op@3 @0 INTEGER_CST@1) INTEGER_CST@2)
3276 (if (!TREE_OVERFLOW (@1) && !TREE_OVERFLOW (@2)
3277 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0))
3278 && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@0))
3279 && !TYPE_SATURATING (TREE_TYPE (@0)))
3280 (with { tree res = int_const_binop (rop, @2, @1); }
3281 (if (TREE_OVERFLOW (res))
3282 { constant_boolean_node (cmp == NE_EXPR, type); }
3283 (if (single_use (@3))
3284 (cmp @0 { res; }))))))))
3285 (for cmp (lt le gt ge)
3286 (for op (plus minus)
3289 (cmp (op@3 @0 INTEGER_CST@1) INTEGER_CST@2)
3290 (if (!TREE_OVERFLOW (@1) && !TREE_OVERFLOW (@2)
3291 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
3292 (with { tree res = int_const_binop (rop, @2, @1); }
3293 (if (TREE_OVERFLOW (res))
3295 fold_overflow_warning (("assuming signed overflow does not occur "
3296 "when simplifying conditional to constant"),
3297 WARN_STRICT_OVERFLOW_CONDITIONAL);
3298 bool less = cmp == LE_EXPR || cmp == LT_EXPR;
3299 /* wi::ges_p (@2, 0) should be sufficient for a signed type. */
3300 bool ovf_high = wi::lt_p (@1, 0, TYPE_SIGN (TREE_TYPE (@1)))
3301 != (op == MINUS_EXPR);
3302 constant_boolean_node (less == ovf_high, type);
3304 (if (single_use (@3))
3307 fold_overflow_warning (("assuming signed overflow does not occur "
3308 "when changing X +- C1 cmp C2 to "
3310 WARN_STRICT_OVERFLOW_COMPARISON);
3312 (cmp @0 { res; })))))))))
3314 /* Canonicalizations of BIT_FIELD_REFs. */
3317 (BIT_FIELD_REF @0 @1 @2)
3319 (if (TREE_CODE (TREE_TYPE (@0)) == COMPLEX_TYPE
3320 && tree_int_cst_equal (@1, TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))))
3322 (if (integer_zerop (@2))
3323 (view_convert (realpart @0)))
3324 (if (tree_int_cst_equal (@2, TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))))
3325 (view_convert (imagpart @0)))))
3326 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
3327 && INTEGRAL_TYPE_P (type)
3328 /* On GIMPLE this should only apply to register arguments. */
3329 && (! GIMPLE || is_gimple_reg (@0))
3330 /* A bit-field-ref that referenced the full argument can be stripped. */
3331 && ((compare_tree_int (@1, TYPE_PRECISION (TREE_TYPE (@0))) == 0
3332 && integer_zerop (@2))
3333 /* Low-parts can be reduced to integral conversions.
3334 ??? The following doesn't work for PDP endian. */
3335 || (BYTES_BIG_ENDIAN == WORDS_BIG_ENDIAN
3336 /* Don't even think about BITS_BIG_ENDIAN. */
3337 && TYPE_PRECISION (TREE_TYPE (@0)) % BITS_PER_UNIT == 0
3338 && TYPE_PRECISION (type) % BITS_PER_UNIT == 0
3339 && compare_tree_int (@2, (BYTES_BIG_ENDIAN
3340 ? (TYPE_PRECISION (TREE_TYPE (@0))
3341 - TYPE_PRECISION (type))
3345 /* Simplify vector extracts. */
3348 (BIT_FIELD_REF CONSTRUCTOR@0 @1 @2)
3349 (if (VECTOR_TYPE_P (TREE_TYPE (@0))
3350 && (types_match (type, TREE_TYPE (TREE_TYPE (@0)))
3351 || (VECTOR_TYPE_P (type)
3352 && types_match (TREE_TYPE (type), TREE_TYPE (TREE_TYPE (@0))))))
3355 tree ctor = (TREE_CODE (@0) == SSA_NAME
3356 ? gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0)) : @0);
3357 tree eltype = TREE_TYPE (TREE_TYPE (ctor));
3358 unsigned HOST_WIDE_INT width = tree_to_uhwi (TYPE_SIZE (eltype));
3359 unsigned HOST_WIDE_INT n = tree_to_uhwi (@1);
3360 unsigned HOST_WIDE_INT idx = tree_to_uhwi (@2);
3363 && (idx % width) == 0
3365 && ((idx + n) / width) <= TYPE_VECTOR_SUBPARTS (TREE_TYPE (ctor)))
3370 /* Constructor elements can be subvectors. */
3371 unsigned HOST_WIDE_INT k = 1;
3372 if (CONSTRUCTOR_NELTS (ctor) != 0)
3374 tree cons_elem = TREE_TYPE (CONSTRUCTOR_ELT (ctor, 0)->value);
3375 if (TREE_CODE (cons_elem) == VECTOR_TYPE)
3376 k = TYPE_VECTOR_SUBPARTS (cons_elem);
3380 /* We keep an exact subset of the constructor elements. */
3381 (if ((idx % k) == 0 && (n % k) == 0)
3382 (if (CONSTRUCTOR_NELTS (ctor) == 0)
3383 { build_constructor (type, NULL); }
3390 (if (idx < CONSTRUCTOR_NELTS (ctor))
3391 { CONSTRUCTOR_ELT (ctor, idx)->value; }
3392 { build_zero_cst (type); })
3394 vec<constructor_elt, va_gc> *vals;
3395 vec_alloc (vals, n);
3396 for (unsigned i = 0;
3397 i < n && idx + i < CONSTRUCTOR_NELTS (ctor); ++i)
3398 CONSTRUCTOR_APPEND_ELT (vals, NULL_TREE,
3399 CONSTRUCTOR_ELT (ctor, idx + i)->value);
3400 build_constructor (type, vals);
3402 /* The bitfield references a single constructor element. */
3403 (if (idx + n <= (idx / k + 1) * k)
3405 (if (CONSTRUCTOR_NELTS (ctor) <= idx / k)
3406 { build_zero_cst (type); })
3408 { CONSTRUCTOR_ELT (ctor, idx / k)->value; })
3409 (BIT_FIELD_REF { CONSTRUCTOR_ELT (ctor, idx / k)->value; }
3410 @1 { bitsize_int ((idx % k) * width); })))))))))