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:s @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)
905 (ne (bit_ior:c (bit_not @0) @1) integer_zerop)
906 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
907 && TYPE_PRECISION (TREE_TYPE (@1)) == 1)
912 (bit_not (bit_not @0))
915 /* Convert ~ (-A) to A - 1. */
917 (bit_not (convert? (negate @0)))
918 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
919 || !TYPE_UNSIGNED (TREE_TYPE (@0)))
920 (convert (minus @0 { build_each_one_cst (TREE_TYPE (@0)); }))))
922 /* Convert ~ (A - 1) or ~ (A + -1) to -A. */
924 (bit_not (convert? (minus @0 integer_each_onep)))
925 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
926 || !TYPE_UNSIGNED (TREE_TYPE (@0)))
927 (convert (negate @0))))
929 (bit_not (convert? (plus @0 integer_all_onesp)))
930 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
931 || !TYPE_UNSIGNED (TREE_TYPE (@0)))
932 (convert (negate @0))))
934 /* Part of convert ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify. */
936 (bit_not (convert? (bit_xor @0 INTEGER_CST@1)))
937 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
938 (convert (bit_xor @0 (bit_not @1)))))
940 (bit_not (convert? (bit_xor:c (bit_not @0) @1)))
941 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
942 (convert (bit_xor @0 @1))))
944 /* (x & ~m) | (y & m) -> ((x ^ y) & m) ^ x */
946 (bit_ior:c (bit_and:cs @0 (bit_not @2)) (bit_and:cs @1 @2))
947 (bit_xor (bit_and (bit_xor @0 @1) @2) @0))
949 /* Fold A - (A & B) into ~B & A. */
951 (minus (convert? @0) (convert?:s (bit_and:cs @0 @1)))
952 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
953 && tree_nop_conversion_p (type, TREE_TYPE (@1)))
954 (convert (bit_and (bit_not @1) @0))))
958 /* ((X inner_op C0) outer_op C1)
959 With X being a tree where value_range has reasoned certain bits to always be
960 zero throughout its computed value range,
961 inner_op = {|,^}, outer_op = {|,^} and inner_op != outer_op
962 where zero_mask has 1's for all bits that are sure to be 0 in
964 if (inner_op == '^') C0 &= ~C1;
965 if ((C0 & ~zero_mask) == 0) then emit (X outer_op (C0 outer_op C1)
966 if ((C1 & ~zero_mask) == 0) then emit (X inner_op (C0 outer_op C1)
968 (for inner_op (bit_ior bit_xor)
969 outer_op (bit_xor bit_ior)
972 (inner_op:s @2 INTEGER_CST@0) INTEGER_CST@1)
976 wide_int zero_mask_not;
980 if (TREE_CODE (@2) == SSA_NAME)
981 zero_mask_not = get_nonzero_bits (@2);
985 if (inner_op == BIT_XOR_EXPR)
987 C0 = wi::bit_and_not (@0, @1);
988 cst_emit = wi::bit_or (C0, @1);
993 cst_emit = wi::bit_xor (@0, @1);
996 (if (!fail && wi::bit_and (C0, zero_mask_not) == 0)
997 (outer_op @2 { wide_int_to_tree (type, cst_emit); })
998 (if (!fail && wi::bit_and (@1, zero_mask_not) == 0)
999 (inner_op @2 { wide_int_to_tree (type, cst_emit); }))))))
1001 /* Associate (p +p off1) +p off2 as (p +p (off1 + off2)). */
1003 (pointer_plus (pointer_plus:s @0 @1) @3)
1004 (pointer_plus @0 (plus @1 @3)))
1010 tem4 = (unsigned long) tem3;
1015 (pointer_plus @0 (convert?@2 (minus@3 (convert @1) (convert @0))))
1016 /* Conditionally look through a sign-changing conversion. */
1017 (if (TYPE_PRECISION (TREE_TYPE (@2)) == TYPE_PRECISION (TREE_TYPE (@3))
1018 && ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@1)))
1019 || (GENERIC && type == TREE_TYPE (@1))))
1023 tem = (sizetype) ptr;
1027 and produce the simpler and easier to analyze with respect to alignment
1028 ... = ptr & ~algn; */
1030 (pointer_plus @0 (negate (bit_and (convert @0) INTEGER_CST@1)))
1031 (with { tree algn = wide_int_to_tree (TREE_TYPE (@0), wi::bit_not (@1)); }
1032 (bit_and @0 { algn; })))
1034 /* Try folding difference of addresses. */
1036 (minus (convert ADDR_EXPR@0) (convert @1))
1037 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1038 (with { HOST_WIDE_INT diff; }
1039 (if (ptr_difference_const (@0, @1, &diff))
1040 { build_int_cst_type (type, diff); }))))
1042 (minus (convert @0) (convert ADDR_EXPR@1))
1043 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1044 (with { HOST_WIDE_INT diff; }
1045 (if (ptr_difference_const (@0, @1, &diff))
1046 { build_int_cst_type (type, diff); }))))
1048 /* If arg0 is derived from the address of an object or function, we may
1049 be able to fold this expression using the object or function's
1052 (bit_and (convert? @0) INTEGER_CST@1)
1053 (if (POINTER_TYPE_P (TREE_TYPE (@0))
1054 && tree_nop_conversion_p (type, TREE_TYPE (@0)))
1058 unsigned HOST_WIDE_INT bitpos;
1059 get_pointer_alignment_1 (@0, &align, &bitpos);
1061 (if (wi::ltu_p (@1, align / BITS_PER_UNIT))
1062 { wide_int_to_tree (type, wi::bit_and (@1, bitpos / BITS_PER_UNIT)); }))))
1065 /* We can't reassociate at all for saturating types. */
1066 (if (!TYPE_SATURATING (type))
1068 /* Contract negates. */
1069 /* A + (-B) -> A - B */
1071 (plus:c (convert1? @0) (convert2? (negate @1)))
1072 /* Apply STRIP_NOPS on @0 and the negate. */
1073 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
1074 && tree_nop_conversion_p (type, TREE_TYPE (@1))
1075 && !TYPE_OVERFLOW_SANITIZED (type))
1076 (minus (convert @0) (convert @1))))
1077 /* A - (-B) -> A + B */
1079 (minus (convert1? @0) (convert2? (negate @1)))
1080 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
1081 && tree_nop_conversion_p (type, TREE_TYPE (@1))
1082 && !TYPE_OVERFLOW_SANITIZED (type))
1083 (plus (convert @0) (convert @1))))
1086 (negate (convert? (negate @1)))
1087 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1088 && !TYPE_OVERFLOW_SANITIZED (type))
1091 /* We can't reassociate floating-point unless -fassociative-math
1092 or fixed-point plus or minus because of saturation to +-Inf. */
1093 (if ((!FLOAT_TYPE_P (type) || flag_associative_math)
1094 && !FIXED_POINT_TYPE_P (type))
1096 /* Match patterns that allow contracting a plus-minus pair
1097 irrespective of overflow issues. */
1098 /* (A +- B) - A -> +- B */
1099 /* (A +- B) -+ B -> A */
1100 /* A - (A +- B) -> -+ B */
1101 /* A +- (B -+ A) -> +- B */
1103 (minus (plus:c @0 @1) @0)
1106 (minus (minus @0 @1) @0)
1109 (plus:c (minus @0 @1) @1)
1112 (minus @0 (plus:c @0 @1))
1115 (minus @0 (minus @0 @1))
1118 /* (A +- CST) +- CST -> A + CST */
1119 (for outer_op (plus minus)
1120 (for inner_op (plus minus)
1122 (outer_op (inner_op @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
1123 /* If the constant operation overflows we cannot do the transform
1124 as we would introduce undefined overflow, for example
1125 with (a - 1) + INT_MIN. */
1126 (with { tree cst = const_binop (outer_op == inner_op
1127 ? PLUS_EXPR : MINUS_EXPR, type, @1, @2); }
1128 (if (cst && !TREE_OVERFLOW (cst))
1129 (inner_op @0 { cst; } ))))))
1131 /* (CST - A) +- CST -> CST - A */
1132 (for outer_op (plus minus)
1134 (outer_op (minus CONSTANT_CLASS_P@1 @0) CONSTANT_CLASS_P@2)
1135 (with { tree cst = const_binop (outer_op, type, @1, @2); }
1136 (if (cst && !TREE_OVERFLOW (cst))
1137 (minus { cst; } @0)))))
1141 (plus:c (bit_not @0) @0)
1142 (if (!TYPE_OVERFLOW_TRAPS (type))
1143 { build_all_ones_cst (type); }))
1147 (plus (convert? (bit_not @0)) integer_each_onep)
1148 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1149 (negate (convert @0))))
1153 (minus (convert? (negate @0)) integer_each_onep)
1154 (if (!TYPE_OVERFLOW_TRAPS (type)
1155 && tree_nop_conversion_p (type, TREE_TYPE (@0)))
1156 (bit_not (convert @0))))
1160 (minus integer_all_onesp @0)
1163 /* (T)(P + A) - (T)P -> (T) A */
1164 (for add (plus pointer_plus)
1166 (minus (convert (add @0 @1))
1168 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
1169 /* For integer types, if A has a smaller type
1170 than T the result depends on the possible
1172 E.g. T=size_t, A=(unsigned)429497295, P>0.
1173 However, if an overflow in P + A would cause
1174 undefined behavior, we can assume that there
1176 || (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1177 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1178 /* For pointer types, if the conversion of A to the
1179 final type requires a sign- or zero-extension,
1180 then we have to punt - it is not defined which
1182 || (POINTER_TYPE_P (TREE_TYPE (@0))
1183 && TREE_CODE (@1) == INTEGER_CST
1184 && tree_int_cst_sign_bit (@1) == 0))
1187 /* (T)P - (T)(P + A) -> -(T) A */
1188 (for add (plus pointer_plus)
1191 (convert (add @0 @1)))
1192 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
1193 /* For integer types, if A has a smaller type
1194 than T the result depends on the possible
1196 E.g. T=size_t, A=(unsigned)429497295, P>0.
1197 However, if an overflow in P + A would cause
1198 undefined behavior, we can assume that there
1200 || (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1201 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1202 /* For pointer types, if the conversion of A to the
1203 final type requires a sign- or zero-extension,
1204 then we have to punt - it is not defined which
1206 || (POINTER_TYPE_P (TREE_TYPE (@0))
1207 && TREE_CODE (@1) == INTEGER_CST
1208 && tree_int_cst_sign_bit (@1) == 0))
1209 (negate (convert @1)))))
1211 /* (T)(P + A) - (T)(P + B) -> (T)A - (T)B */
1212 (for add (plus pointer_plus)
1214 (minus (convert (add @0 @1))
1215 (convert (add @0 @2)))
1216 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
1217 /* For integer types, if A has a smaller type
1218 than T the result depends on the possible
1220 E.g. T=size_t, A=(unsigned)429497295, P>0.
1221 However, if an overflow in P + A would cause
1222 undefined behavior, we can assume that there
1224 || (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1225 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1226 /* For pointer types, if the conversion of A to the
1227 final type requires a sign- or zero-extension,
1228 then we have to punt - it is not defined which
1230 || (POINTER_TYPE_P (TREE_TYPE (@0))
1231 && TREE_CODE (@1) == INTEGER_CST
1232 && tree_int_cst_sign_bit (@1) == 0
1233 && TREE_CODE (@2) == INTEGER_CST
1234 && tree_int_cst_sign_bit (@2) == 0))
1235 (minus (convert @1) (convert @2)))))))
1238 /* Simplifications of MIN_EXPR, MAX_EXPR, fmin() and fmax(). */
1240 (for minmax (min max FMIN FMAX)
1244 /* min(max(x,y),y) -> y. */
1246 (min:c (max:c @0 @1) @1)
1248 /* max(min(x,y),y) -> y. */
1250 (max:c (min:c @0 @1) @1)
1255 (if (INTEGRAL_TYPE_P (type)
1256 && TYPE_MIN_VALUE (type)
1257 && operand_equal_p (@1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
1259 (if (INTEGRAL_TYPE_P (type)
1260 && TYPE_MAX_VALUE (type)
1261 && operand_equal_p (@1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
1266 (if (INTEGRAL_TYPE_P (type)
1267 && TYPE_MAX_VALUE (type)
1268 && operand_equal_p (@1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
1270 (if (INTEGRAL_TYPE_P (type)
1271 && TYPE_MIN_VALUE (type)
1272 && operand_equal_p (@1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
1274 (for minmax (FMIN FMAX)
1275 /* If either argument is NaN, return the other one. Avoid the
1276 transformation if we get (and honor) a signalling NaN. */
1278 (minmax:c @0 REAL_CST@1)
1279 (if (real_isnan (TREE_REAL_CST_PTR (@1))
1280 && (!HONOR_SNANS (@1) || !TREE_REAL_CST (@1).signalling))
1282 /* Convert fmin/fmax to MIN_EXPR/MAX_EXPR. C99 requires these
1283 functions to return the numeric arg if the other one is NaN.
1284 MIN and MAX don't honor that, so only transform if -ffinite-math-only
1285 is set. C99 doesn't require -0.0 to be handled, so we don't have to
1286 worry about it either. */
1287 (if (flag_finite_math_only)
1294 /* min (-A, -B) -> -max (A, B) */
1295 (for minmax (min max FMIN FMAX)
1296 maxmin (max min FMAX FMIN)
1298 (minmax (negate:s@2 @0) (negate:s@3 @1))
1299 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
1300 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1301 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
1302 (negate (maxmin @0 @1)))))
1303 /* MIN (~X, ~Y) -> ~MAX (X, Y)
1304 MAX (~X, ~Y) -> ~MIN (X, Y) */
1305 (for minmax (min max)
1308 (minmax (bit_not:s@2 @0) (bit_not:s@3 @1))
1309 (bit_not (maxmin @0 @1))))
1311 /* Simplifications of shift and rotates. */
1313 (for rotate (lrotate rrotate)
1315 (rotate integer_all_onesp@0 @1)
1318 /* Optimize -1 >> x for arithmetic right shifts. */
1320 (rshift integer_all_onesp@0 @1)
1321 (if (!TYPE_UNSIGNED (type)
1322 && tree_expr_nonnegative_p (@1))
1325 /* Optimize (x >> c) << c into x & (-1<<c). */
1327 (lshift (rshift @0 INTEGER_CST@1) @1)
1328 (if (wi::ltu_p (@1, element_precision (type)))
1329 (bit_and @0 (lshift { build_minus_one_cst (type); } @1))))
1331 /* Optimize (x << c) >> c into x & ((unsigned)-1 >> c) for unsigned
1334 (rshift (lshift @0 INTEGER_CST@1) @1)
1335 (if (TYPE_UNSIGNED (type)
1336 && (wi::ltu_p (@1, element_precision (type))))
1337 (bit_and @0 (rshift { build_minus_one_cst (type); } @1))))
1339 (for shiftrotate (lrotate rrotate lshift rshift)
1341 (shiftrotate @0 integer_zerop)
1344 (shiftrotate integer_zerop@0 @1)
1346 /* Prefer vector1 << scalar to vector1 << vector2
1347 if vector2 is uniform. */
1348 (for vec (VECTOR_CST CONSTRUCTOR)
1350 (shiftrotate @0 vec@1)
1351 (with { tree tem = uniform_vector_p (@1); }
1353 (shiftrotate @0 { tem; }))))))
1355 /* Rewrite an LROTATE_EXPR by a constant into an
1356 RROTATE_EXPR by a new constant. */
1358 (lrotate @0 INTEGER_CST@1)
1359 (rrotate @0 { const_binop (MINUS_EXPR, TREE_TYPE (@1),
1360 build_int_cst (TREE_TYPE (@1),
1361 element_precision (type)), @1); }))
1363 /* Turn (a OP c1) OP c2 into a OP (c1+c2). */
1364 (for op (lrotate rrotate rshift lshift)
1366 (op (op @0 INTEGER_CST@1) INTEGER_CST@2)
1367 (with { unsigned int prec = element_precision (type); }
1368 (if (wi::ge_p (@1, 0, TYPE_SIGN (TREE_TYPE (@1)))
1369 && wi::lt_p (@1, prec, TYPE_SIGN (TREE_TYPE (@1)))
1370 && wi::ge_p (@2, 0, TYPE_SIGN (TREE_TYPE (@2)))
1371 && wi::lt_p (@2, prec, TYPE_SIGN (TREE_TYPE (@2))))
1372 (with { unsigned int low = wi::add (@1, @2).to_uhwi (); }
1373 /* Deal with a OP (c1 + c2) being undefined but (a OP c1) OP c2
1374 being well defined. */
1376 (if (op == LROTATE_EXPR || op == RROTATE_EXPR)
1377 (op @0 { build_int_cst (TREE_TYPE (@1), low % prec); })
1378 (if (TYPE_UNSIGNED (type) || op == LSHIFT_EXPR)
1379 { build_zero_cst (type); }
1380 (op @0 { build_int_cst (TREE_TYPE (@1), prec - 1); })))
1381 (op @0 { build_int_cst (TREE_TYPE (@1), low); })))))))
1384 /* ((1 << A) & 1) != 0 -> A == 0
1385 ((1 << A) & 1) == 0 -> A != 0 */
1389 (cmp (bit_and (lshift integer_onep @0) integer_onep) integer_zerop)
1390 (icmp @0 { build_zero_cst (TREE_TYPE (@0)); })))
1392 /* (CST1 << A) == CST2 -> A == ctz (CST2) - ctz (CST1)
1393 (CST1 << A) != CST2 -> A != ctz (CST2) - ctz (CST1)
1397 (cmp (lshift INTEGER_CST@0 @1) INTEGER_CST@2)
1398 (with { int cand = wi::ctz (@2) - wi::ctz (@0); }
1400 || (!integer_zerop (@2)
1401 && wi::ne_p (wi::lshift (@0, cand), @2)))
1402 { constant_boolean_node (cmp == NE_EXPR, type); }
1403 (if (!integer_zerop (@2)
1404 && wi::eq_p (wi::lshift (@0, cand), @2))
1405 (cmp @1 { build_int_cst (TREE_TYPE (@1), cand); }))))))
1407 /* Fold (X << C1) & C2 into (X << C1) & (C2 | ((1 << C1) - 1))
1408 (X >> C1) & C2 into (X >> C1) & (C2 | ~((type) -1 >> C1))
1409 if the new mask might be further optimized. */
1410 (for shift (lshift rshift)
1412 (bit_and (convert?:s@4 (shift:s@5 (convert1?@3 @0) INTEGER_CST@1))
1414 (if (tree_nop_conversion_p (TREE_TYPE (@4), TREE_TYPE (@5))
1415 && TYPE_PRECISION (type) <= HOST_BITS_PER_WIDE_INT
1416 && tree_fits_uhwi_p (@1)
1417 && tree_to_uhwi (@1) > 0
1418 && tree_to_uhwi (@1) < TYPE_PRECISION (type))
1421 unsigned int shiftc = tree_to_uhwi (@1);
1422 unsigned HOST_WIDE_INT mask = TREE_INT_CST_LOW (@2);
1423 unsigned HOST_WIDE_INT newmask, zerobits = 0;
1424 tree shift_type = TREE_TYPE (@3);
1427 if (shift == LSHIFT_EXPR)
1428 zerobits = ((((unsigned HOST_WIDE_INT) 1) << shiftc) - 1);
1429 else if (shift == RSHIFT_EXPR
1430 && (TYPE_PRECISION (shift_type)
1431 == GET_MODE_PRECISION (TYPE_MODE (shift_type))))
1433 prec = TYPE_PRECISION (TREE_TYPE (@3));
1435 /* See if more bits can be proven as zero because of
1438 && TYPE_UNSIGNED (TREE_TYPE (@0)))
1440 tree inner_type = TREE_TYPE (@0);
1441 if ((TYPE_PRECISION (inner_type)
1442 == GET_MODE_PRECISION (TYPE_MODE (inner_type)))
1443 && TYPE_PRECISION (inner_type) < prec)
1445 prec = TYPE_PRECISION (inner_type);
1446 /* See if we can shorten the right shift. */
1448 shift_type = inner_type;
1449 /* Otherwise X >> C1 is all zeros, so we'll optimize
1450 it into (X, 0) later on by making sure zerobits
1454 zerobits = ~(unsigned HOST_WIDE_INT) 0;
1457 zerobits >>= HOST_BITS_PER_WIDE_INT - shiftc;
1458 zerobits <<= prec - shiftc;
1460 /* For arithmetic shift if sign bit could be set, zerobits
1461 can contain actually sign bits, so no transformation is
1462 possible, unless MASK masks them all away. In that
1463 case the shift needs to be converted into logical shift. */
1464 if (!TYPE_UNSIGNED (TREE_TYPE (@3))
1465 && prec == TYPE_PRECISION (TREE_TYPE (@3)))
1467 if ((mask & zerobits) == 0)
1468 shift_type = unsigned_type_for (TREE_TYPE (@3));
1474 /* ((X << 16) & 0xff00) is (X, 0). */
1475 (if ((mask & zerobits) == mask)
1476 { build_int_cst (type, 0); }
1477 (with { newmask = mask | zerobits; }
1478 (if (newmask != mask && (newmask & (newmask + 1)) == 0)
1481 /* Only do the transformation if NEWMASK is some integer
1483 for (prec = BITS_PER_UNIT;
1484 prec < HOST_BITS_PER_WIDE_INT; prec <<= 1)
1485 if (newmask == (((unsigned HOST_WIDE_INT) 1) << prec) - 1)
1488 (if (prec < HOST_BITS_PER_WIDE_INT
1489 || newmask == ~(unsigned HOST_WIDE_INT) 0)
1491 { tree newmaskt = build_int_cst_type (TREE_TYPE (@2), newmask); }
1492 (if (!tree_int_cst_equal (newmaskt, @2))
1493 (if (shift_type != TREE_TYPE (@3))
1494 (bit_and (convert (shift:shift_type (convert @3) @1)) { newmaskt; })
1495 (bit_and @4 { newmaskt; })))))))))))))
1497 /* Fold (X {&,^,|} C2) << C1 into (X << C1) {&,^,|} (C2 << C1)
1498 (X {&,^,|} C2) >> C1 into (X >> C1) & (C2 >> C1). */
1499 (for shift (lshift rshift)
1500 (for bit_op (bit_and bit_xor bit_ior)
1502 (shift (convert?:s (bit_op:s @0 INTEGER_CST@2)) INTEGER_CST@1)
1503 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1504 (with { tree mask = int_const_binop (shift, fold_convert (type, @2), @1); }
1505 (bit_op (shift (convert @0) @1) { mask; }))))))
1507 /* ~(~X >> Y) -> X >> Y (for arithmetic shift). */
1509 (bit_not (convert1?:s (rshift:s (convert2?@0 (bit_not @1)) @2)))
1510 (if (!TYPE_UNSIGNED (TREE_TYPE (@0))
1511 && (element_precision (TREE_TYPE (@0))
1512 <= element_precision (TREE_TYPE (@1))
1513 || !TYPE_UNSIGNED (TREE_TYPE (@1))))
1515 { tree shift_type = TREE_TYPE (@0); }
1516 (convert (rshift (convert:shift_type @1) @2)))))
1518 /* ~(~X >>r Y) -> X >>r Y
1519 ~(~X <<r Y) -> X <<r Y */
1520 (for rotate (lrotate rrotate)
1522 (bit_not (convert1?:s (rotate:s (convert2?@0 (bit_not @1)) @2)))
1523 (if ((element_precision (TREE_TYPE (@0))
1524 <= element_precision (TREE_TYPE (@1))
1525 || !TYPE_UNSIGNED (TREE_TYPE (@1)))
1526 && (element_precision (type) <= element_precision (TREE_TYPE (@0))
1527 || !TYPE_UNSIGNED (TREE_TYPE (@0))))
1529 { tree rotate_type = TREE_TYPE (@0); }
1530 (convert (rotate (convert:rotate_type @1) @2))))))
1532 /* Simplifications of conversions. */
1534 /* Basic strip-useless-type-conversions / strip_nops. */
1535 (for cvt (convert view_convert float fix_trunc)
1538 (if ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@0)))
1539 || (GENERIC && type == TREE_TYPE (@0)))
1542 /* Contract view-conversions. */
1544 (view_convert (view_convert @0))
1547 /* For integral conversions with the same precision or pointer
1548 conversions use a NOP_EXPR instead. */
1551 (if ((INTEGRAL_TYPE_P (type) || POINTER_TYPE_P (type))
1552 && (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
1553 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (@0)))
1556 /* Strip inner integral conversions that do not change precision or size. */
1558 (view_convert (convert@0 @1))
1559 (if ((INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
1560 && (INTEGRAL_TYPE_P (TREE_TYPE (@1)) || POINTER_TYPE_P (TREE_TYPE (@1)))
1561 && (TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
1562 && (TYPE_SIZE (TREE_TYPE (@0)) == TYPE_SIZE (TREE_TYPE (@1))))
1565 /* Re-association barriers around constants and other re-association
1566 barriers can be removed. */
1568 (paren CONSTANT_CLASS_P@0)
1571 (paren (paren@1 @0))
1574 /* Handle cases of two conversions in a row. */
1575 (for ocvt (convert float fix_trunc)
1576 (for icvt (convert float)
1581 tree inside_type = TREE_TYPE (@0);
1582 tree inter_type = TREE_TYPE (@1);
1583 int inside_int = INTEGRAL_TYPE_P (inside_type);
1584 int inside_ptr = POINTER_TYPE_P (inside_type);
1585 int inside_float = FLOAT_TYPE_P (inside_type);
1586 int inside_vec = VECTOR_TYPE_P (inside_type);
1587 unsigned int inside_prec = TYPE_PRECISION (inside_type);
1588 int inside_unsignedp = TYPE_UNSIGNED (inside_type);
1589 int inter_int = INTEGRAL_TYPE_P (inter_type);
1590 int inter_ptr = POINTER_TYPE_P (inter_type);
1591 int inter_float = FLOAT_TYPE_P (inter_type);
1592 int inter_vec = VECTOR_TYPE_P (inter_type);
1593 unsigned int inter_prec = TYPE_PRECISION (inter_type);
1594 int inter_unsignedp = TYPE_UNSIGNED (inter_type);
1595 int final_int = INTEGRAL_TYPE_P (type);
1596 int final_ptr = POINTER_TYPE_P (type);
1597 int final_float = FLOAT_TYPE_P (type);
1598 int final_vec = VECTOR_TYPE_P (type);
1599 unsigned int final_prec = TYPE_PRECISION (type);
1600 int final_unsignedp = TYPE_UNSIGNED (type);
1603 /* In addition to the cases of two conversions in a row
1604 handled below, if we are converting something to its own
1605 type via an object of identical or wider precision, neither
1606 conversion is needed. */
1607 (if (((GIMPLE && useless_type_conversion_p (type, inside_type))
1609 && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (inside_type)))
1610 && (((inter_int || inter_ptr) && final_int)
1611 || (inter_float && final_float))
1612 && inter_prec >= final_prec)
1615 /* Likewise, if the intermediate and initial types are either both
1616 float or both integer, we don't need the middle conversion if the
1617 former is wider than the latter and doesn't change the signedness
1618 (for integers). Avoid this if the final type is a pointer since
1619 then we sometimes need the middle conversion. Likewise if the
1620 final type has a precision not equal to the size of its mode. */
1621 (if (((inter_int && inside_int) || (inter_float && inside_float))
1622 && (final_int || final_float)
1623 && inter_prec >= inside_prec
1624 && (inter_float || inter_unsignedp == inside_unsignedp)
1625 && ! (final_prec != GET_MODE_PRECISION (TYPE_MODE (type))
1626 && TYPE_MODE (type) == TYPE_MODE (inter_type)))
1629 /* If we have a sign-extension of a zero-extended value, we can
1630 replace that by a single zero-extension. Likewise if the
1631 final conversion does not change precision we can drop the
1632 intermediate conversion. */
1633 (if (inside_int && inter_int && final_int
1634 && ((inside_prec < inter_prec && inter_prec < final_prec
1635 && inside_unsignedp && !inter_unsignedp)
1636 || final_prec == inter_prec))
1639 /* Two conversions in a row are not needed unless:
1640 - some conversion is floating-point (overstrict for now), or
1641 - some conversion is a vector (overstrict for now), or
1642 - the intermediate type is narrower than both initial and
1644 - the intermediate type and innermost type differ in signedness,
1645 and the outermost type is wider than the intermediate, or
1646 - the initial type is a pointer type and the precisions of the
1647 intermediate and final types differ, or
1648 - the final type is a pointer type and the precisions of the
1649 initial and intermediate types differ. */
1650 (if (! inside_float && ! inter_float && ! final_float
1651 && ! inside_vec && ! inter_vec && ! final_vec
1652 && (inter_prec >= inside_prec || inter_prec >= final_prec)
1653 && ! (inside_int && inter_int
1654 && inter_unsignedp != inside_unsignedp
1655 && inter_prec < final_prec)
1656 && ((inter_unsignedp && inter_prec > inside_prec)
1657 == (final_unsignedp && final_prec > inter_prec))
1658 && ! (inside_ptr && inter_prec != final_prec)
1659 && ! (final_ptr && inside_prec != inter_prec)
1660 && ! (final_prec != GET_MODE_PRECISION (TYPE_MODE (type))
1661 && TYPE_MODE (type) == TYPE_MODE (inter_type)))
1664 /* A truncation to an unsigned type (a zero-extension) should be
1665 canonicalized as bitwise and of a mask. */
1666 (if (GIMPLE /* PR70366: doing this in GENERIC breaks -Wconversion. */
1667 && final_int && inter_int && inside_int
1668 && final_prec == inside_prec
1669 && final_prec > inter_prec
1671 (convert (bit_and @0 { wide_int_to_tree
1673 wi::mask (inter_prec, false,
1674 TYPE_PRECISION (inside_type))); })))
1676 /* If we are converting an integer to a floating-point that can
1677 represent it exactly and back to an integer, we can skip the
1678 floating-point conversion. */
1679 (if (GIMPLE /* PR66211 */
1680 && inside_int && inter_float && final_int &&
1681 (unsigned) significand_size (TYPE_MODE (inter_type))
1682 >= inside_prec - !inside_unsignedp)
1685 /* If we have a narrowing conversion to an integral type that is fed by a
1686 BIT_AND_EXPR, we might be able to remove the BIT_AND_EXPR if it merely
1687 masks off bits outside the final type (and nothing else). */
1689 (convert (bit_and @0 INTEGER_CST@1))
1690 (if (INTEGRAL_TYPE_P (type)
1691 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
1692 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))
1693 && operand_equal_p (@1, build_low_bits_mask (TREE_TYPE (@1),
1694 TYPE_PRECISION (type)), 0))
1698 /* (X /[ex] A) * A -> X. */
1700 (mult (convert? (exact_div @0 @1)) @1)
1701 /* Look through a sign-changing conversion. */
1704 /* Canonicalization of binary operations. */
1706 /* Convert X + -C into X - C. */
1708 (plus @0 REAL_CST@1)
1709 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
1710 (with { tree tem = const_unop (NEGATE_EXPR, type, @1); }
1711 (if (!TREE_OVERFLOW (tem) || !flag_trapping_math)
1712 (minus @0 { tem; })))))
1714 /* Convert x+x into x*2. */
1717 (if (SCALAR_FLOAT_TYPE_P (type))
1718 (mult @0 { build_real (type, dconst2); })
1719 (if (INTEGRAL_TYPE_P (type))
1720 (mult @0 { build_int_cst (type, 2); }))))
1723 (minus integer_zerop @1)
1726 /* (ARG0 - ARG1) is the same as (-ARG1 + ARG0). So check whether
1727 ARG0 is zero and X + ARG0 reduces to X, since that would mean
1728 (-ARG1 + ARG0) reduces to -ARG1. */
1730 (minus real_zerop@0 @1)
1731 (if (fold_real_zero_addition_p (type, @0, 0))
1734 /* Transform x * -1 into -x. */
1736 (mult @0 integer_minus_onep)
1739 /* True if we can easily extract the real and imaginary parts of a complex
1741 (match compositional_complex
1742 (convert? (complex @0 @1)))
1744 /* COMPLEX_EXPR and REALPART/IMAGPART_EXPR cancellations. */
1746 (complex (realpart @0) (imagpart @0))
1749 (realpart (complex @0 @1))
1752 (imagpart (complex @0 @1))
1755 /* Sometimes we only care about half of a complex expression. */
1757 (realpart (convert?:s (conj:s @0)))
1758 (convert (realpart @0)))
1760 (imagpart (convert?:s (conj:s @0)))
1761 (convert (negate (imagpart @0))))
1762 (for part (realpart imagpart)
1763 (for op (plus minus)
1765 (part (convert?:s@2 (op:s @0 @1)))
1766 (convert (op (part @0) (part @1))))))
1768 (realpart (convert?:s (CEXPI:s @0)))
1771 (imagpart (convert?:s (CEXPI:s @0)))
1774 /* conj(conj(x)) -> x */
1776 (conj (convert? (conj @0)))
1777 (if (tree_nop_conversion_p (TREE_TYPE (@0), type))
1780 /* conj({x,y}) -> {x,-y} */
1782 (conj (convert?:s (complex:s @0 @1)))
1783 (with { tree itype = TREE_TYPE (type); }
1784 (complex (convert:itype @0) (negate (convert:itype @1)))))
1786 /* BSWAP simplifications, transforms checked by gcc.dg/builtin-bswap-8.c. */
1787 (for bswap (BUILT_IN_BSWAP16 BUILT_IN_BSWAP32 BUILT_IN_BSWAP64)
1792 (bswap (bit_not (bswap @0)))
1794 (for bitop (bit_xor bit_ior bit_and)
1796 (bswap (bitop:c (bswap @0) @1))
1797 (bitop @0 (bswap @1)))))
1800 /* Combine COND_EXPRs and VEC_COND_EXPRs. */
1802 /* Simplify constant conditions.
1803 Only optimize constant conditions when the selected branch
1804 has the same type as the COND_EXPR. This avoids optimizing
1805 away "c ? x : throw", where the throw has a void type.
1806 Note that we cannot throw away the fold-const.c variant nor
1807 this one as we depend on doing this transform before possibly
1808 A ? B : B -> B triggers and the fold-const.c one can optimize
1809 0 ? A : B to B even if A has side-effects. Something
1810 genmatch cannot handle. */
1812 (cond INTEGER_CST@0 @1 @2)
1813 (if (integer_zerop (@0))
1814 (if (!VOID_TYPE_P (TREE_TYPE (@2)) || VOID_TYPE_P (type))
1816 (if (!VOID_TYPE_P (TREE_TYPE (@1)) || VOID_TYPE_P (type))
1819 (vec_cond VECTOR_CST@0 @1 @2)
1820 (if (integer_all_onesp (@0))
1822 (if (integer_zerop (@0))
1825 (for cnd (cond vec_cond)
1826 /* A ? B : (A ? X : C) -> A ? B : C. */
1828 (cnd @0 (cnd @0 @1 @2) @3)
1831 (cnd @0 @1 (cnd @0 @2 @3))
1833 /* A ? B : (!A ? C : X) -> A ? B : C. */
1834 /* ??? This matches embedded conditions open-coded because genmatch
1835 would generate matching code for conditions in separate stmts only.
1836 The following is still important to merge then and else arm cases
1837 from if-conversion. */
1839 (cnd @0 @1 (cnd @2 @3 @4))
1840 (if (COMPARISON_CLASS_P (@0)
1841 && COMPARISON_CLASS_P (@2)
1842 && invert_tree_comparison
1843 (TREE_CODE (@0), HONOR_NANS (TREE_OPERAND (@0, 0))) == TREE_CODE (@2)
1844 && operand_equal_p (TREE_OPERAND (@0, 0), TREE_OPERAND (@2, 0), 0)
1845 && operand_equal_p (TREE_OPERAND (@0, 1), TREE_OPERAND (@2, 1), 0))
1848 (cnd @0 (cnd @1 @2 @3) @4)
1849 (if (COMPARISON_CLASS_P (@0)
1850 && COMPARISON_CLASS_P (@1)
1851 && invert_tree_comparison
1852 (TREE_CODE (@0), HONOR_NANS (TREE_OPERAND (@0, 0))) == TREE_CODE (@1)
1853 && operand_equal_p (TREE_OPERAND (@0, 0), TREE_OPERAND (@1, 0), 0)
1854 && operand_equal_p (TREE_OPERAND (@0, 1), TREE_OPERAND (@1, 1), 0))
1857 /* A ? B : B -> B. */
1862 /* !A ? B : C -> A ? C : B. */
1864 (cnd (logical_inverted_value truth_valued_p@0) @1 @2)
1867 /* A + (B vcmp C ? 1 : 0) -> A - (B vcmp C ? -1 : 0), since vector comparisons
1868 return all -1 or all 0 results. */
1869 /* ??? We could instead convert all instances of the vec_cond to negate,
1870 but that isn't necessarily a win on its own. */
1872 (plus:c @3 (view_convert? (vec_cond:s @0 integer_each_onep@1 integer_zerop@2)))
1873 (if (VECTOR_TYPE_P (type)
1874 && TYPE_VECTOR_SUBPARTS (type) == TYPE_VECTOR_SUBPARTS (TREE_TYPE (@1))
1875 && (TYPE_MODE (TREE_TYPE (type))
1876 == TYPE_MODE (TREE_TYPE (TREE_TYPE (@1)))))
1877 (minus @3 (view_convert (vec_cond @0 (negate @1) @2)))))
1879 /* ... likewise A - (B vcmp C ? 1 : 0) -> A + (B vcmp C ? -1 : 0). */
1881 (minus @3 (view_convert? (vec_cond:s @0 integer_each_onep@1 integer_zerop@2)))
1882 (if (VECTOR_TYPE_P (type)
1883 && TYPE_VECTOR_SUBPARTS (type) == TYPE_VECTOR_SUBPARTS (TREE_TYPE (@1))
1884 && (TYPE_MODE (TREE_TYPE (type))
1885 == TYPE_MODE (TREE_TYPE (TREE_TYPE (@1)))))
1886 (plus @3 (view_convert (vec_cond @0 (negate @1) @2)))))
1889 /* Simplifications of comparisons. */
1891 /* See if we can reduce the magnitude of a constant involved in a
1892 comparison by changing the comparison code. This is a canonicalization
1893 formerly done by maybe_canonicalize_comparison_1. */
1897 (cmp @0 INTEGER_CST@1)
1898 (if (tree_int_cst_sgn (@1) == -1)
1899 (acmp @0 { wide_int_to_tree (TREE_TYPE (@1), wi::add (@1, 1)); }))))
1903 (cmp @0 INTEGER_CST@1)
1904 (if (tree_int_cst_sgn (@1) == 1)
1905 (acmp @0 { wide_int_to_tree (TREE_TYPE (@1), wi::sub (@1, 1)); }))))
1908 /* We can simplify a logical negation of a comparison to the
1909 inverted comparison. As we cannot compute an expression
1910 operator using invert_tree_comparison we have to simulate
1911 that with expression code iteration. */
1912 (for cmp (tcc_comparison)
1913 icmp (inverted_tcc_comparison)
1914 ncmp (inverted_tcc_comparison_with_nans)
1915 /* Ideally we'd like to combine the following two patterns
1916 and handle some more cases by using
1917 (logical_inverted_value (cmp @0 @1))
1918 here but for that genmatch would need to "inline" that.
1919 For now implement what forward_propagate_comparison did. */
1921 (bit_not (cmp @0 @1))
1922 (if (VECTOR_TYPE_P (type)
1923 || (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1))
1924 /* Comparison inversion may be impossible for trapping math,
1925 invert_tree_comparison will tell us. But we can't use
1926 a computed operator in the replacement tree thus we have
1927 to play the trick below. */
1928 (with { enum tree_code ic = invert_tree_comparison
1929 (cmp, HONOR_NANS (@0)); }
1935 (bit_xor (cmp @0 @1) integer_truep)
1936 (with { enum tree_code ic = invert_tree_comparison
1937 (cmp, HONOR_NANS (@0)); }
1943 /* Transform comparisons of the form X - Y CMP 0 to X CMP Y.
1944 ??? The transformation is valid for the other operators if overflow
1945 is undefined for the type, but performing it here badly interacts
1946 with the transformation in fold_cond_expr_with_comparison which
1947 attempts to synthetize ABS_EXPR. */
1950 (cmp (minus@2 @0 @1) integer_zerop)
1951 (if (single_use (@2))
1954 /* Transform comparisons of the form X * C1 CMP 0 to X CMP 0 in the
1955 signed arithmetic case. That form is created by the compiler
1956 often enough for folding it to be of value. One example is in
1957 computing loop trip counts after Operator Strength Reduction. */
1958 (for cmp (simple_comparison)
1959 scmp (swapped_simple_comparison)
1961 (cmp (mult@3 @0 INTEGER_CST@1) integer_zerop@2)
1962 /* Handle unfolded multiplication by zero. */
1963 (if (integer_zerop (@1))
1965 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1966 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1968 /* If @1 is negative we swap the sense of the comparison. */
1969 (if (tree_int_cst_sgn (@1) < 0)
1973 /* Simplify comparison of something with itself. For IEEE
1974 floating-point, we can only do some of these simplifications. */
1978 (if (! FLOAT_TYPE_P (TREE_TYPE (@0))
1979 || ! HONOR_NANS (@0))
1980 { constant_boolean_node (true, type); }
1981 (if (cmp != EQ_EXPR)
1987 || ! FLOAT_TYPE_P (TREE_TYPE (@0))
1988 || ! HONOR_NANS (@0))
1989 { constant_boolean_node (false, type); })))
1990 (for cmp (unle unge uneq)
1993 { constant_boolean_node (true, type); }))
1994 (for cmp (unlt ungt)
2000 (if (!flag_trapping_math)
2001 { constant_boolean_node (false, type); }))
2003 /* Fold ~X op ~Y as Y op X. */
2004 (for cmp (simple_comparison)
2006 (cmp (bit_not@2 @0) (bit_not@3 @1))
2007 (if (single_use (@2) && single_use (@3))
2010 /* Fold ~X op C as X op' ~C, where op' is the swapped comparison. */
2011 (for cmp (simple_comparison)
2012 scmp (swapped_simple_comparison)
2014 (cmp (bit_not@2 @0) CONSTANT_CLASS_P@1)
2015 (if (single_use (@2)
2016 && (TREE_CODE (@1) == INTEGER_CST || TREE_CODE (@1) == VECTOR_CST))
2017 (scmp @0 (bit_not @1)))))
2019 (for cmp (simple_comparison)
2020 /* Fold (double)float1 CMP (double)float2 into float1 CMP float2. */
2022 (cmp (convert@2 @0) (convert? @1))
2023 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
2024 && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@2))
2025 == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0)))
2026 && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@2))
2027 == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1))))
2030 tree type1 = TREE_TYPE (@1);
2031 if (TREE_CODE (@1) == REAL_CST && !DECIMAL_FLOAT_TYPE_P (type1))
2033 REAL_VALUE_TYPE orig = TREE_REAL_CST (@1);
2034 if (TYPE_PRECISION (type1) > TYPE_PRECISION (float_type_node)
2035 && exact_real_truncate (TYPE_MODE (float_type_node), &orig))
2036 type1 = float_type_node;
2037 if (TYPE_PRECISION (type1) > TYPE_PRECISION (double_type_node)
2038 && exact_real_truncate (TYPE_MODE (double_type_node), &orig))
2039 type1 = double_type_node;
2042 = (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type1)
2043 ? TREE_TYPE (@0) : type1);
2045 (if (TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (newtype))
2046 (cmp (convert:newtype @0) (convert:newtype @1))))))
2050 /* IEEE doesn't distinguish +0 and -0 in comparisons. */
2052 /* a CMP (-0) -> a CMP 0 */
2053 (if (REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@1)))
2054 (cmp @0 { build_real (TREE_TYPE (@1), dconst0); }))
2055 /* x != NaN is always true, other ops are always false. */
2056 (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
2057 && ! HONOR_SNANS (@1))
2058 { constant_boolean_node (cmp == NE_EXPR, type); })
2059 /* Fold comparisons against infinity. */
2060 (if (REAL_VALUE_ISINF (TREE_REAL_CST (@1))
2061 && MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (@1))))
2064 REAL_VALUE_TYPE max;
2065 enum tree_code code = cmp;
2066 bool neg = REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1));
2068 code = swap_tree_comparison (code);
2071 /* x > +Inf is always false, if with ignore sNANs. */
2072 (if (code == GT_EXPR
2073 && ! HONOR_SNANS (@0))
2074 { constant_boolean_node (false, type); })
2075 (if (code == LE_EXPR)
2076 /* x <= +Inf is always true, if we don't case about NaNs. */
2077 (if (! HONOR_NANS (@0))
2078 { constant_boolean_node (true, type); }
2079 /* x <= +Inf is the same as x == x, i.e. !isnan(x). */
2081 /* x == +Inf and x >= +Inf are always equal to x > DBL_MAX. */
2082 (if (code == EQ_EXPR || code == GE_EXPR)
2083 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
2085 (lt @0 { build_real (TREE_TYPE (@0), max); })
2086 (gt @0 { build_real (TREE_TYPE (@0), max); }))))
2087 /* x < +Inf is always equal to x <= DBL_MAX. */
2088 (if (code == LT_EXPR)
2089 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
2091 (ge @0 { build_real (TREE_TYPE (@0), max); })
2092 (le @0 { build_real (TREE_TYPE (@0), max); }))))
2093 /* x != +Inf is always equal to !(x > DBL_MAX). */
2094 (if (code == NE_EXPR)
2095 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
2096 (if (! HONOR_NANS (@0))
2098 (ge @0 { build_real (TREE_TYPE (@0), max); })
2099 (le @0 { build_real (TREE_TYPE (@0), max); }))
2101 (bit_xor (lt @0 { build_real (TREE_TYPE (@0), max); })
2102 { build_one_cst (type); })
2103 (bit_xor (gt @0 { build_real (TREE_TYPE (@0), max); })
2104 { build_one_cst (type); }))))))))))
2106 /* If this is a comparison of a real constant with a PLUS_EXPR
2107 or a MINUS_EXPR of a real constant, we can convert it into a
2108 comparison with a revised real constant as long as no overflow
2109 occurs when unsafe_math_optimizations are enabled. */
2110 (if (flag_unsafe_math_optimizations)
2111 (for op (plus minus)
2113 (cmp (op @0 REAL_CST@1) REAL_CST@2)
2116 tree tem = const_binop (op == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR,
2117 TREE_TYPE (@1), @2, @1);
2119 (if (tem && !TREE_OVERFLOW (tem))
2120 (cmp @0 { tem; }))))))
2122 /* Likewise, we can simplify a comparison of a real constant with
2123 a MINUS_EXPR whose first operand is also a real constant, i.e.
2124 (c1 - x) < c2 becomes x > c1-c2. Reordering is allowed on
2125 floating-point types only if -fassociative-math is set. */
2126 (if (flag_associative_math)
2128 (cmp (minus REAL_CST@0 @1) REAL_CST@2)
2129 (with { tree tem = const_binop (MINUS_EXPR, TREE_TYPE (@1), @0, @2); }
2130 (if (tem && !TREE_OVERFLOW (tem))
2131 (cmp { tem; } @1)))))
2133 /* Fold comparisons against built-in math functions. */
2134 (if (flag_unsafe_math_optimizations
2135 && ! flag_errno_math)
2138 (cmp (sq @0) REAL_CST@1)
2140 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
2142 /* sqrt(x) < y is always false, if y is negative. */
2143 (if (cmp == EQ_EXPR || cmp == LT_EXPR || cmp == LE_EXPR)
2144 { constant_boolean_node (false, type); })
2145 /* sqrt(x) > y is always true, if y is negative and we
2146 don't care about NaNs, i.e. negative values of x. */
2147 (if (cmp == NE_EXPR || !HONOR_NANS (@0))
2148 { constant_boolean_node (true, type); })
2149 /* sqrt(x) > y is the same as x >= 0, if y is negative. */
2150 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })))
2151 (if (real_equal (TREE_REAL_CST_PTR (@1), &dconst0))
2153 /* sqrt(x) < 0 is always false. */
2154 (if (cmp == LT_EXPR)
2155 { constant_boolean_node (false, type); })
2156 /* sqrt(x) >= 0 is always true if we don't care about NaNs. */
2157 (if (cmp == GE_EXPR && !HONOR_NANS (@0))
2158 { constant_boolean_node (true, type); })
2159 /* sqrt(x) <= 0 -> x == 0. */
2160 (if (cmp == LE_EXPR)
2162 /* Otherwise sqrt(x) cmp 0 -> x cmp 0. Here cmp can be >=, >,
2163 == or !=. In the last case:
2165 (sqrt(x) != 0) == (NaN != 0) == true == (x != 0)
2167 if x is negative or NaN. Due to -funsafe-math-optimizations,
2168 the results for other x follow from natural arithmetic. */
2170 (if (cmp == GT_EXPR || cmp == GE_EXPR)
2174 real_arithmetic (&c2, MULT_EXPR,
2175 &TREE_REAL_CST (@1), &TREE_REAL_CST (@1));
2176 real_convert (&c2, TYPE_MODE (TREE_TYPE (@0)), &c2);
2178 (if (REAL_VALUE_ISINF (c2))
2179 /* sqrt(x) > y is x == +Inf, when y is very large. */
2180 (if (HONOR_INFINITIES (@0))
2181 (eq @0 { build_real (TREE_TYPE (@0), c2); })
2182 { constant_boolean_node (false, type); })
2183 /* sqrt(x) > c is the same as x > c*c. */
2184 (cmp @0 { build_real (TREE_TYPE (@0), c2); }))))
2185 (if (cmp == LT_EXPR || cmp == LE_EXPR)
2189 real_arithmetic (&c2, MULT_EXPR,
2190 &TREE_REAL_CST (@1), &TREE_REAL_CST (@1));
2191 real_convert (&c2, TYPE_MODE (TREE_TYPE (@0)), &c2);
2193 (if (REAL_VALUE_ISINF (c2))
2195 /* sqrt(x) < y is always true, when y is a very large
2196 value and we don't care about NaNs or Infinities. */
2197 (if (! HONOR_NANS (@0) && ! HONOR_INFINITIES (@0))
2198 { constant_boolean_node (true, type); })
2199 /* sqrt(x) < y is x != +Inf when y is very large and we
2200 don't care about NaNs. */
2201 (if (! HONOR_NANS (@0))
2202 (ne @0 { build_real (TREE_TYPE (@0), c2); }))
2203 /* sqrt(x) < y is x >= 0 when y is very large and we
2204 don't care about Infinities. */
2205 (if (! HONOR_INFINITIES (@0))
2206 (ge @0 { build_real (TREE_TYPE (@0), dconst0); }))
2207 /* sqrt(x) < y is x >= 0 && x != +Inf, when y is large. */
2210 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
2211 (ne @0 { build_real (TREE_TYPE (@0), c2); }))))
2212 /* sqrt(x) < c is the same as x < c*c, if we ignore NaNs. */
2213 (if (! HONOR_NANS (@0))
2214 (cmp @0 { build_real (TREE_TYPE (@0), c2); })
2215 /* sqrt(x) < c is the same as x >= 0 && x < c*c. */
2218 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
2219 (cmp @0 { build_real (TREE_TYPE (@0), c2); }))))))))))))
2221 /* Unordered tests if either argument is a NaN. */
2223 (bit_ior (unordered @0 @0) (unordered @1 @1))
2224 (if (types_match (@0, @1))
2227 (bit_and (ordered @0 @0) (ordered @1 @1))
2228 (if (types_match (@0, @1))
2231 (bit_ior:c (unordered @0 @0) (unordered:c@2 @0 @1))
2234 (bit_and:c (ordered @0 @0) (ordered:c@2 @0 @1))
2237 /* Simple range test simplifications. */
2238 /* A < B || A >= B -> true. */
2239 (for test1 (lt le le le ne ge)
2240 test2 (ge gt ge ne eq ne)
2242 (bit_ior:c (test1 @0 @1) (test2 @0 @1))
2243 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2244 || VECTOR_INTEGER_TYPE_P (TREE_TYPE (@0)))
2245 { constant_boolean_node (true, type); })))
2246 /* A < B && A >= B -> false. */
2247 (for test1 (lt lt lt le ne eq)
2248 test2 (ge gt eq gt eq gt)
2250 (bit_and:c (test1 @0 @1) (test2 @0 @1))
2251 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2252 || VECTOR_INTEGER_TYPE_P (TREE_TYPE (@0)))
2253 { constant_boolean_node (false, type); })))
2255 /* -A CMP -B -> B CMP A. */
2256 (for cmp (tcc_comparison)
2257 scmp (swapped_tcc_comparison)
2259 (cmp (negate @0) (negate @1))
2260 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
2261 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2262 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
2265 (cmp (negate @0) CONSTANT_CLASS_P@1)
2266 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
2267 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2268 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
2269 (with { tree tem = const_unop (NEGATE_EXPR, TREE_TYPE (@0), @1); }
2270 (if (tem && !TREE_OVERFLOW (tem))
2271 (scmp @0 { tem; }))))))
2273 /* Convert ABS_EXPR<x> == 0 or ABS_EXPR<x> != 0 to x == 0 or x != 0. */
2276 (op (abs @0) zerop@1)
2279 /* From fold_sign_changed_comparison and fold_widened_comparison. */
2280 (for cmp (simple_comparison)
2282 (cmp (convert@0 @00) (convert?@1 @10))
2283 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2284 /* Disable this optimization if we're casting a function pointer
2285 type on targets that require function pointer canonicalization. */
2286 && !(targetm.have_canonicalize_funcptr_for_compare ()
2287 && TREE_CODE (TREE_TYPE (@00)) == POINTER_TYPE
2288 && TREE_CODE (TREE_TYPE (TREE_TYPE (@00))) == FUNCTION_TYPE)
2290 (if (TYPE_PRECISION (TREE_TYPE (@00)) == TYPE_PRECISION (TREE_TYPE (@0))
2291 && (TREE_CODE (@10) == INTEGER_CST
2292 || (@1 != @10 && types_match (TREE_TYPE (@10), TREE_TYPE (@00))))
2293 && (TYPE_UNSIGNED (TREE_TYPE (@00)) == TYPE_UNSIGNED (TREE_TYPE (@0))
2296 && (POINTER_TYPE_P (TREE_TYPE (@00)) == POINTER_TYPE_P (TREE_TYPE (@0))))
2297 /* ??? The special-casing of INTEGER_CST conversion was in the original
2298 code and here to avoid a spurious overflow flag on the resulting
2299 constant which fold_convert produces. */
2300 (if (TREE_CODE (@1) == INTEGER_CST)
2301 (cmp @00 { force_fit_type (TREE_TYPE (@00), wi::to_widest (@1), 0,
2302 TREE_OVERFLOW (@1)); })
2303 (cmp @00 (convert @1)))
2305 (if (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (TREE_TYPE (@00)))
2306 /* If possible, express the comparison in the shorter mode. */
2307 (if ((cmp == EQ_EXPR || cmp == NE_EXPR
2308 || TYPE_UNSIGNED (TREE_TYPE (@0)) == TYPE_UNSIGNED (TREE_TYPE (@00)))
2309 && (types_match (TREE_TYPE (@10), TREE_TYPE (@00))
2310 || ((TYPE_PRECISION (TREE_TYPE (@00))
2311 >= TYPE_PRECISION (TREE_TYPE (@10)))
2312 && (TYPE_UNSIGNED (TREE_TYPE (@00))
2313 == TYPE_UNSIGNED (TREE_TYPE (@10))))
2314 || (TREE_CODE (@10) == INTEGER_CST
2315 && INTEGRAL_TYPE_P (TREE_TYPE (@00))
2316 && int_fits_type_p (@10, TREE_TYPE (@00)))))
2317 (cmp @00 (convert @10))
2318 (if (TREE_CODE (@10) == INTEGER_CST
2319 && INTEGRAL_TYPE_P (TREE_TYPE (@00))
2320 && !int_fits_type_p (@10, TREE_TYPE (@00)))
2323 tree min = lower_bound_in_type (TREE_TYPE (@10), TREE_TYPE (@00));
2324 tree max = upper_bound_in_type (TREE_TYPE (@10), TREE_TYPE (@00));
2325 bool above = integer_nonzerop (const_binop (LT_EXPR, type, max, @10));
2326 bool below = integer_nonzerop (const_binop (LT_EXPR, type, @10, min));
2328 (if (above || below)
2329 (if (cmp == EQ_EXPR || cmp == NE_EXPR)
2330 { constant_boolean_node (cmp == EQ_EXPR ? false : true, type); }
2331 (if (cmp == LT_EXPR || cmp == LE_EXPR)
2332 { constant_boolean_node (above ? true : false, type); }
2333 (if (cmp == GT_EXPR || cmp == GE_EXPR)
2334 { constant_boolean_node (above ? false : true, type); }))))))))))))
2337 /* A local variable can never be pointed to by
2338 the default SSA name of an incoming parameter.
2339 SSA names are canonicalized to 2nd place. */
2341 (cmp addr@0 SSA_NAME@1)
2342 (if (SSA_NAME_IS_DEFAULT_DEF (@1)
2343 && TREE_CODE (SSA_NAME_VAR (@1)) == PARM_DECL)
2344 (with { tree base = get_base_address (TREE_OPERAND (@0, 0)); }
2345 (if (TREE_CODE (base) == VAR_DECL
2346 && auto_var_in_fn_p (base, current_function_decl))
2347 (if (cmp == NE_EXPR)
2348 { constant_boolean_node (true, type); }
2349 { constant_boolean_node (false, type); }))))))
2351 /* Equality compare simplifications from fold_binary */
2354 /* If we have (A | C) == D where C & ~D != 0, convert this into 0.
2355 Similarly for NE_EXPR. */
2357 (cmp (convert?@3 (bit_ior @0 INTEGER_CST@1)) INTEGER_CST@2)
2358 (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0))
2359 && wi::bit_and_not (@1, @2) != 0)
2360 { constant_boolean_node (cmp == NE_EXPR, type); }))
2362 /* (X ^ Y) == 0 becomes X == Y, and (X ^ Y) != 0 becomes X != Y. */
2364 (cmp (bit_xor @0 @1) integer_zerop)
2367 /* (X ^ Y) == Y becomes X == 0.
2368 Likewise (X ^ Y) == X becomes Y == 0. */
2370 (cmp:c (bit_xor:c @0 @1) @0)
2371 (cmp @1 { build_zero_cst (TREE_TYPE (@1)); }))
2373 /* (X ^ C1) op C2 can be rewritten as X op (C1 ^ C2). */
2375 (cmp (convert?@3 (bit_xor @0 INTEGER_CST@1)) INTEGER_CST@2)
2376 (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0)))
2377 (cmp @0 (bit_xor @1 (convert @2)))))
2380 (cmp (convert? addr@0) integer_zerop)
2381 (if (tree_single_nonzero_warnv_p (@0, NULL))
2382 { constant_boolean_node (cmp == NE_EXPR, type); })))
2384 /* If we have (A & C) == C where C is a power of 2, convert this into
2385 (A & C) != 0. Similarly for NE_EXPR. */
2389 (cmp (bit_and@2 @0 integer_pow2p@1) @1)
2390 (icmp @2 { build_zero_cst (TREE_TYPE (@0)); })))
2392 /* If we have (A & C) != 0 where C is the sign bit of A, convert
2393 this into A < 0. Similarly for (A & C) == 0 into A >= 0. */
2397 (cmp (bit_and (convert?@2 @0) integer_pow2p@1) integer_zerop)
2398 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2399 && (TYPE_PRECISION (TREE_TYPE (@0))
2400 == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (@0))))
2401 && element_precision (@2) >= element_precision (@0)
2402 && wi::only_sign_bit_p (@1, element_precision (@0)))
2403 (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
2404 (ncmp (convert:stype @0) { build_zero_cst (stype); })))))
2406 /* When the addresses are not directly of decls compare base and offset.
2407 This implements some remaining parts of fold_comparison address
2408 comparisons but still no complete part of it. Still it is good
2409 enough to make fold_stmt not regress when not dispatching to fold_binary. */
2410 (for cmp (simple_comparison)
2412 (cmp (convert1?@2 addr@0) (convert2? addr@1))
2415 HOST_WIDE_INT off0, off1;
2416 tree base0 = get_addr_base_and_unit_offset (TREE_OPERAND (@0, 0), &off0);
2417 tree base1 = get_addr_base_and_unit_offset (TREE_OPERAND (@1, 0), &off1);
2418 if (base0 && TREE_CODE (base0) == MEM_REF)
2420 off0 += mem_ref_offset (base0).to_short_addr ();
2421 base0 = TREE_OPERAND (base0, 0);
2423 if (base1 && TREE_CODE (base1) == MEM_REF)
2425 off1 += mem_ref_offset (base1).to_short_addr ();
2426 base1 = TREE_OPERAND (base1, 0);
2429 (if (base0 && base1)
2433 if (decl_in_symtab_p (base0)
2434 && decl_in_symtab_p (base1))
2435 equal = symtab_node::get_create (base0)
2436 ->equal_address_to (symtab_node::get_create (base1));
2437 else if ((DECL_P (base0)
2438 || TREE_CODE (base0) == SSA_NAME
2439 || TREE_CODE (base0) == STRING_CST)
2441 || TREE_CODE (base1) == SSA_NAME
2442 || TREE_CODE (base1) == STRING_CST))
2443 equal = (base0 == base1);
2446 && (cmp == EQ_EXPR || cmp == NE_EXPR
2447 /* If the offsets are equal we can ignore overflow. */
2449 || POINTER_TYPE_OVERFLOW_UNDEFINED
2450 /* Or if we compare using pointers to decls or strings. */
2451 || (POINTER_TYPE_P (TREE_TYPE (@2))
2452 && (DECL_P (base0) || TREE_CODE (base0) == STRING_CST))))
2454 (if (cmp == EQ_EXPR)
2455 { constant_boolean_node (off0 == off1, type); })
2456 (if (cmp == NE_EXPR)
2457 { constant_boolean_node (off0 != off1, type); })
2458 (if (cmp == LT_EXPR)
2459 { constant_boolean_node (off0 < off1, type); })
2460 (if (cmp == LE_EXPR)
2461 { constant_boolean_node (off0 <= off1, type); })
2462 (if (cmp == GE_EXPR)
2463 { constant_boolean_node (off0 >= off1, type); })
2464 (if (cmp == GT_EXPR)
2465 { constant_boolean_node (off0 > off1, type); }))
2467 && DECL_P (base0) && DECL_P (base1)
2468 /* If we compare this as integers require equal offset. */
2469 && (!INTEGRAL_TYPE_P (TREE_TYPE (@2))
2472 (if (cmp == EQ_EXPR)
2473 { constant_boolean_node (false, type); })
2474 (if (cmp == NE_EXPR)
2475 { constant_boolean_node (true, type); })))))))))
2477 /* Simplify pointer equality compares using PTA. */
2481 (if (POINTER_TYPE_P (TREE_TYPE (@0))
2482 && ptrs_compare_unequal (@0, @1))
2483 { neeq == EQ_EXPR ? boolean_false_node : boolean_true_node; })))
2485 /* Non-equality compare simplifications from fold_binary */
2486 (for cmp (lt gt le ge)
2487 /* Comparisons with the highest or lowest possible integer of
2488 the specified precision will have known values. */
2490 (cmp (convert?@2 @0) INTEGER_CST@1)
2491 (if ((INTEGRAL_TYPE_P (TREE_TYPE (@1)) || POINTER_TYPE_P (TREE_TYPE (@1)))
2492 && tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@0)))
2495 tree arg1_type = TREE_TYPE (@1);
2496 unsigned int prec = TYPE_PRECISION (arg1_type);
2497 wide_int max = wi::max_value (arg1_type);
2498 wide_int signed_max = wi::max_value (prec, SIGNED);
2499 wide_int min = wi::min_value (arg1_type);
2502 (if (wi::eq_p (@1, max))
2504 (if (cmp == GT_EXPR)
2505 { constant_boolean_node (false, type); })
2506 (if (cmp == GE_EXPR)
2508 (if (cmp == LE_EXPR)
2509 { constant_boolean_node (true, type); })
2510 (if (cmp == LT_EXPR)
2512 (if (wi::eq_p (@1, min))
2514 (if (cmp == LT_EXPR)
2515 { constant_boolean_node (false, type); })
2516 (if (cmp == LE_EXPR)
2518 (if (cmp == GE_EXPR)
2519 { constant_boolean_node (true, type); })
2520 (if (cmp == GT_EXPR)
2522 (if (wi::eq_p (@1, max - 1))
2524 (if (cmp == GT_EXPR)
2525 (eq @2 { wide_int_to_tree (TREE_TYPE (@1), wi::add (@1, 1)); }))
2526 (if (cmp == LE_EXPR)
2527 (ne @2 { wide_int_to_tree (TREE_TYPE (@1), wi::add (@1, 1)); }))))
2528 (if (wi::eq_p (@1, min + 1))
2530 (if (cmp == GE_EXPR)
2531 (ne @2 { wide_int_to_tree (TREE_TYPE (@1), wi::sub (@1, 1)); }))
2532 (if (cmp == LT_EXPR)
2533 (eq @2 { wide_int_to_tree (TREE_TYPE (@1), wi::sub (@1, 1)); }))))
2534 (if (wi::eq_p (@1, signed_max)
2535 && TYPE_UNSIGNED (arg1_type)
2536 /* We will flip the signedness of the comparison operator
2537 associated with the mode of @1, so the sign bit is
2538 specified by this mode. Check that @1 is the signed
2539 max associated with this sign bit. */
2540 && prec == GET_MODE_PRECISION (TYPE_MODE (arg1_type))
2541 /* signed_type does not work on pointer types. */
2542 && INTEGRAL_TYPE_P (arg1_type))
2543 /* The following case also applies to X < signed_max+1
2544 and X >= signed_max+1 because previous transformations. */
2545 (if (cmp == LE_EXPR || cmp == GT_EXPR)
2546 (with { tree st = signed_type_for (arg1_type); }
2547 (if (cmp == LE_EXPR)
2548 (ge (convert:st @0) { build_zero_cst (st); })
2549 (lt (convert:st @0) { build_zero_cst (st); }))))))))))
2551 (for cmp (unordered ordered unlt unle ungt unge uneq ltgt)
2552 /* If the second operand is NaN, the result is constant. */
2555 (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
2556 && (cmp != LTGT_EXPR || ! flag_trapping_math))
2557 { constant_boolean_node (cmp == ORDERED_EXPR || cmp == LTGT_EXPR
2558 ? false : true, type); })))
2560 /* bool_var != 0 becomes bool_var. */
2562 (ne @0 integer_zerop)
2563 (if (TREE_CODE (TREE_TYPE (@0)) == BOOLEAN_TYPE
2564 && types_match (type, TREE_TYPE (@0)))
2566 /* bool_var == 1 becomes bool_var. */
2568 (eq @0 integer_onep)
2569 (if (TREE_CODE (TREE_TYPE (@0)) == BOOLEAN_TYPE
2570 && types_match (type, TREE_TYPE (@0)))
2573 bool_var == 0 becomes !bool_var or
2574 bool_var != 1 becomes !bool_var
2575 here because that only is good in assignment context as long
2576 as we require a tcc_comparison in GIMPLE_CONDs where we'd
2577 replace if (x == 0) with tem = ~x; if (tem != 0) which is
2578 clearly less optimal and which we'll transform again in forwprop. */
2580 /* When one argument is a constant, overflow detection can be simplified.
2581 Currently restricted to single use so as not to interfere too much with
2582 ADD_OVERFLOW detection in tree-ssa-math-opts.c.
2583 A + CST CMP A -> A CMP' CST' */
2584 (for cmp (lt le ge gt)
2587 (cmp (plus@2 @0 INTEGER_CST@1) @0)
2588 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
2589 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))
2592 (out @0 { wide_int_to_tree (TREE_TYPE (@0), wi::max_value
2593 (TYPE_PRECISION (TREE_TYPE (@0)), UNSIGNED) - @1); }))))
2594 /* A CMP A + CST -> A CMP' CST' */
2595 (for cmp (gt ge le lt)
2598 (cmp @0 (plus@2 @0 INTEGER_CST@1))
2599 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
2600 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))
2603 (out @0 { wide_int_to_tree (TREE_TYPE (@0), wi::max_value
2604 (TYPE_PRECISION (TREE_TYPE (@0)), UNSIGNED) - @1); }))))
2606 /* To detect overflow in unsigned A - B, A < B is simpler than A - B > A.
2607 However, the detection logic for SUB_OVERFLOW in tree-ssa-math-opts.c
2608 expects the long form, so we restrict the transformation for now. */
2611 (cmp (minus@2 @0 @1) @0)
2612 (if (single_use (@2)
2613 && ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2614 && TYPE_UNSIGNED (TREE_TYPE (@0))
2615 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
2619 (cmp @0 (minus@2 @0 @1))
2620 (if (single_use (@2)
2621 && ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2622 && TYPE_UNSIGNED (TREE_TYPE (@0))
2623 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
2626 /* Testing for overflow is unnecessary if we already know the result. */
2631 (cmp @0 (realpart (IFN_SUB_OVERFLOW@2 @0 @1)))
2632 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
2633 && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
2634 (out (imagpart @2) { build_zero_cst (TREE_TYPE (@0)); }))))
2639 (cmp (realpart (IFN_SUB_OVERFLOW@2 @0 @1)) @0)
2640 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
2641 && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
2642 (out (imagpart @2) { build_zero_cst (TREE_TYPE (@0)); }))))
2647 (cmp (realpart (IFN_ADD_OVERFLOW:c@2 @0 @1)) @0)
2648 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
2649 && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
2650 (out (imagpart @2) { build_zero_cst (TREE_TYPE (@0)); }))))
2655 (cmp @0 (realpart (IFN_ADD_OVERFLOW:c@2 @0 @1)))
2656 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
2657 && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
2658 (out (imagpart @2) { build_zero_cst (TREE_TYPE (@0)); }))))
2661 /* Simplification of math builtins. These rules must all be optimizations
2662 as well as IL simplifications. If there is a possibility that the new
2663 form could be a pessimization, the rule should go in the canonicalization
2664 section that follows this one.
2666 Rules can generally go in this section if they satisfy one of
2669 - the rule describes an identity
2671 - the rule replaces calls with something as simple as addition or
2674 - the rule contains unary calls only and simplifies the surrounding
2675 arithmetic. (The idea here is to exclude non-unary calls in which
2676 one operand is constant and in which the call is known to be cheap
2677 when the operand has that value.) */
2679 (if (flag_unsafe_math_optimizations)
2680 /* Simplify sqrt(x) * sqrt(x) -> x. */
2682 (mult (SQRT@1 @0) @1)
2683 (if (!HONOR_SNANS (type))
2686 /* Simplify sqrt(x) * sqrt(y) -> sqrt(x*y). */
2687 (for root (SQRT CBRT)
2689 (mult (root:s @0) (root:s @1))
2690 (root (mult @0 @1))))
2692 /* Simplify expN(x) * expN(y) -> expN(x+y). */
2693 (for exps (EXP EXP2 EXP10 POW10)
2695 (mult (exps:s @0) (exps:s @1))
2696 (exps (plus @0 @1))))
2698 /* Simplify a/root(b/c) into a*root(c/b). */
2699 (for root (SQRT CBRT)
2701 (rdiv @0 (root:s (rdiv:s @1 @2)))
2702 (mult @0 (root (rdiv @2 @1)))))
2704 /* Simplify x/expN(y) into x*expN(-y). */
2705 (for exps (EXP EXP2 EXP10 POW10)
2707 (rdiv @0 (exps:s @1))
2708 (mult @0 (exps (negate @1)))))
2710 (for logs (LOG LOG2 LOG10 LOG10)
2711 exps (EXP EXP2 EXP10 POW10)
2712 /* logN(expN(x)) -> x. */
2716 /* expN(logN(x)) -> x. */
2721 /* Optimize logN(func()) for various exponential functions. We
2722 want to determine the value "x" and the power "exponent" in
2723 order to transform logN(x**exponent) into exponent*logN(x). */
2724 (for logs (LOG LOG LOG LOG2 LOG2 LOG2 LOG10 LOG10)
2725 exps (EXP2 EXP10 POW10 EXP EXP10 POW10 EXP EXP2)
2728 (if (SCALAR_FLOAT_TYPE_P (type))
2734 /* Prepare to do logN(exp(exponent)) -> exponent*logN(e). */
2735 x = build_real_truncate (type, dconst_e ());
2738 /* Prepare to do logN(exp2(exponent)) -> exponent*logN(2). */
2739 x = build_real (type, dconst2);
2743 /* Prepare to do logN(exp10(exponent)) -> exponent*logN(10). */
2745 REAL_VALUE_TYPE dconst10;
2746 real_from_integer (&dconst10, VOIDmode, 10, SIGNED);
2747 x = build_real (type, dconst10);
2754 (mult (logs { x; }) @0)))))
2762 (if (SCALAR_FLOAT_TYPE_P (type))
2768 /* Prepare to do logN(sqrt(x)) -> 0.5*logN(x). */
2769 x = build_real (type, dconsthalf);
2772 /* Prepare to do logN(cbrt(x)) -> (1/3)*logN(x). */
2773 x = build_real_truncate (type, dconst_third ());
2779 (mult { x; } (logs @0))))))
2781 /* logN(pow(x,exponent)) -> exponent*logN(x). */
2782 (for logs (LOG LOG2 LOG10)
2786 (mult @1 (logs @0))))
2791 exps (EXP EXP2 EXP10 POW10)
2792 /* sqrt(expN(x)) -> expN(x*0.5). */
2795 (exps (mult @0 { build_real (type, dconsthalf); })))
2796 /* cbrt(expN(x)) -> expN(x/3). */
2799 (exps (mult @0 { build_real_truncate (type, dconst_third ()); })))
2800 /* pow(expN(x), y) -> expN(x*y). */
2803 (exps (mult @0 @1))))
2805 /* tan(atan(x)) -> x. */
2812 /* cabs(x+0i) or cabs(0+xi) -> abs(x). */
2814 (CABS (complex:c @0 real_zerop@1))
2817 /* trunc(trunc(x)) -> trunc(x), etc. */
2818 (for fns (TRUNC FLOOR CEIL ROUND NEARBYINT RINT)
2822 /* f(x) -> x if x is integer valued and f does nothing for such values. */
2823 (for fns (TRUNC FLOOR CEIL ROUND NEARBYINT RINT)
2825 (fns integer_valued_real_p@0)
2828 /* hypot(x,0) and hypot(0,x) -> abs(x). */
2830 (HYPOT:c @0 real_zerop@1)
2833 /* pow(1,x) -> 1. */
2835 (POW real_onep@0 @1)
2839 /* copysign(x,x) -> x. */
2844 /* copysign(x,y) -> fabs(x) if y is nonnegative. */
2845 (COPYSIGN @0 tree_expr_nonnegative_p@1)
2848 (for scale (LDEXP SCALBN SCALBLN)
2849 /* ldexp(0, x) -> 0. */
2851 (scale real_zerop@0 @1)
2853 /* ldexp(x, 0) -> x. */
2855 (scale @0 integer_zerop@1)
2857 /* ldexp(x, y) -> x if x is +-Inf or NaN. */
2859 (scale REAL_CST@0 @1)
2860 (if (!real_isfinite (TREE_REAL_CST_PTR (@0)))
2863 /* Canonicalization of sequences of math builtins. These rules represent
2864 IL simplifications but are not necessarily optimizations.
2866 The sincos pass is responsible for picking "optimal" implementations
2867 of math builtins, which may be more complicated and can sometimes go
2868 the other way, e.g. converting pow into a sequence of sqrts.
2869 We only want to do these canonicalizations before the pass has run. */
2871 (if (flag_unsafe_math_optimizations && canonicalize_math_p ())
2872 /* Simplify tan(x) * cos(x) -> sin(x). */
2874 (mult:c (TAN:s @0) (COS:s @0))
2877 /* Simplify x * pow(x,c) -> pow(x,c+1). */
2879 (mult:c @0 (POW:s @0 REAL_CST@1))
2880 (if (!TREE_OVERFLOW (@1))
2881 (POW @0 (plus @1 { build_one_cst (type); }))))
2883 /* Simplify sin(x) / cos(x) -> tan(x). */
2885 (rdiv (SIN:s @0) (COS:s @0))
2888 /* Simplify cos(x) / sin(x) -> 1 / tan(x). */
2890 (rdiv (COS:s @0) (SIN:s @0))
2891 (rdiv { build_one_cst (type); } (TAN @0)))
2893 /* Simplify sin(x) / tan(x) -> cos(x). */
2895 (rdiv (SIN:s @0) (TAN:s @0))
2896 (if (! HONOR_NANS (@0)
2897 && ! HONOR_INFINITIES (@0))
2900 /* Simplify tan(x) / sin(x) -> 1.0 / cos(x). */
2902 (rdiv (TAN:s @0) (SIN:s @0))
2903 (if (! HONOR_NANS (@0)
2904 && ! HONOR_INFINITIES (@0))
2905 (rdiv { build_one_cst (type); } (COS @0))))
2907 /* Simplify pow(x,y) * pow(x,z) -> pow(x,y+z). */
2909 (mult (POW:s @0 @1) (POW:s @0 @2))
2910 (POW @0 (plus @1 @2)))
2912 /* Simplify pow(x,y) * pow(z,y) -> pow(x*z,y). */
2914 (mult (POW:s @0 @1) (POW:s @2 @1))
2915 (POW (mult @0 @2) @1))
2917 /* Simplify powi(x,y) * powi(z,y) -> powi(x*z,y). */
2919 (mult (POWI:s @0 @1) (POWI:s @2 @1))
2920 (POWI (mult @0 @2) @1))
2922 /* Simplify pow(x,c) / x -> pow(x,c-1). */
2924 (rdiv (POW:s @0 REAL_CST@1) @0)
2925 (if (!TREE_OVERFLOW (@1))
2926 (POW @0 (minus @1 { build_one_cst (type); }))))
2928 /* Simplify x / pow (y,z) -> x * pow(y,-z). */
2930 (rdiv @0 (POW:s @1 @2))
2931 (mult @0 (POW @1 (negate @2))))
2936 /* sqrt(sqrt(x)) -> pow(x,1/4). */
2939 (pows @0 { build_real (type, dconst_quarter ()); }))
2940 /* sqrt(cbrt(x)) -> pow(x,1/6). */
2943 (pows @0 { build_real_truncate (type, dconst_sixth ()); }))
2944 /* cbrt(sqrt(x)) -> pow(x,1/6). */
2947 (pows @0 { build_real_truncate (type, dconst_sixth ()); }))
2948 /* cbrt(cbrt(x)) -> pow(x,1/9), iff x is nonnegative. */
2950 (cbrts (cbrts tree_expr_nonnegative_p@0))
2951 (pows @0 { build_real_truncate (type, dconst_ninth ()); }))
2952 /* sqrt(pow(x,y)) -> pow(|x|,y*0.5). */
2954 (sqrts (pows @0 @1))
2955 (pows (abs @0) (mult @1 { build_real (type, dconsthalf); })))
2956 /* cbrt(pow(x,y)) -> pow(x,y/3), iff x is nonnegative. */
2958 (cbrts (pows tree_expr_nonnegative_p@0 @1))
2959 (pows @0 (mult @1 { build_real_truncate (type, dconst_third ()); })))
2960 /* pow(sqrt(x),y) -> pow(x,y*0.5). */
2962 (pows (sqrts @0) @1)
2963 (pows @0 (mult @1 { build_real (type, dconsthalf); })))
2964 /* pow(cbrt(x),y) -> pow(x,y/3) iff x is nonnegative. */
2966 (pows (cbrts tree_expr_nonnegative_p@0) @1)
2967 (pows @0 (mult @1 { build_real_truncate (type, dconst_third ()); })))
2968 /* pow(pow(x,y),z) -> pow(x,y*z) iff x is nonnegative. */
2970 (pows (pows tree_expr_nonnegative_p@0 @1) @2)
2971 (pows @0 (mult @1 @2))))
2973 /* cabs(x+xi) -> fabs(x)*sqrt(2). */
2975 (CABS (complex @0 @0))
2976 (mult (abs @0) { build_real_truncate (type, dconst_sqrt2 ()); }))
2978 /* hypot(x,x) -> fabs(x)*sqrt(2). */
2981 (mult (abs @0) { build_real_truncate (type, dconst_sqrt2 ()); }))
2983 /* cexp(x+yi) -> exp(x)*cexpi(y). */
2988 (cexps compositional_complex@0)
2989 (if (targetm.libc_has_function (function_c99_math_complex))
2991 (mult (exps@1 (realpart @0)) (realpart (cexpis:type@2 (imagpart @0))))
2992 (mult @1 (imagpart @2)))))))
2994 (if (canonicalize_math_p ())
2995 /* floor(x) -> trunc(x) if x is nonnegative. */
2999 (floors tree_expr_nonnegative_p@0)
3002 (match double_value_p
3004 (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == double_type_node)))
3005 (for froms (BUILT_IN_TRUNCL
3017 /* truncl(extend(x)) -> extend(trunc(x)), etc., if x is a double. */
3018 (if (optimize && canonicalize_math_p ())
3020 (froms (convert double_value_p@0))
3021 (convert (tos @0)))))
3023 (match float_value_p
3025 (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == float_type_node)))
3026 (for froms (BUILT_IN_TRUNCL BUILT_IN_TRUNC
3027 BUILT_IN_FLOORL BUILT_IN_FLOOR
3028 BUILT_IN_CEILL BUILT_IN_CEIL
3029 BUILT_IN_ROUNDL BUILT_IN_ROUND
3030 BUILT_IN_NEARBYINTL BUILT_IN_NEARBYINT
3031 BUILT_IN_RINTL BUILT_IN_RINT)
3032 tos (BUILT_IN_TRUNCF BUILT_IN_TRUNCF
3033 BUILT_IN_FLOORF BUILT_IN_FLOORF
3034 BUILT_IN_CEILF BUILT_IN_CEILF
3035 BUILT_IN_ROUNDF BUILT_IN_ROUNDF
3036 BUILT_IN_NEARBYINTF BUILT_IN_NEARBYINTF
3037 BUILT_IN_RINTF BUILT_IN_RINTF)
3038 /* truncl(extend(x)) and trunc(extend(x)) -> extend(truncf(x)), etc.,
3040 (if (optimize && canonicalize_math_p ()
3041 && targetm.libc_has_function (function_c99_misc))
3043 (froms (convert float_value_p@0))
3044 (convert (tos @0)))))
3046 (for froms (XFLOORL XCEILL XROUNDL XRINTL)
3047 tos (XFLOOR XCEIL XROUND XRINT)
3048 /* llfloorl(extend(x)) -> llfloor(x), etc., if x is a double. */
3049 (if (optimize && canonicalize_math_p ())
3051 (froms (convert double_value_p@0))
3054 (for froms (XFLOORL XCEILL XROUNDL XRINTL
3055 XFLOOR XCEIL XROUND XRINT)
3056 tos (XFLOORF XCEILF XROUNDF XRINTF)
3057 /* llfloorl(extend(x)) and llfloor(extend(x)) -> llfloorf(x), etc.,
3059 (if (optimize && canonicalize_math_p ())
3061 (froms (convert float_value_p@0))
3064 (if (canonicalize_math_p ())
3065 /* xfloor(x) -> fix_trunc(x) if x is nonnegative. */
3066 (for floors (IFLOOR LFLOOR LLFLOOR)
3068 (floors tree_expr_nonnegative_p@0)
3071 (if (canonicalize_math_p ())
3072 /* xfloor(x) -> fix_trunc(x), etc., if x is integer valued. */
3073 (for fns (IFLOOR LFLOOR LLFLOOR
3075 IROUND LROUND LLROUND)
3077 (fns integer_valued_real_p@0)
3079 (if (!flag_errno_math)
3080 /* xrint(x) -> fix_trunc(x), etc., if x is integer valued. */
3081 (for rints (IRINT LRINT LLRINT)
3083 (rints integer_valued_real_p@0)
3086 (if (canonicalize_math_p ())
3087 (for ifn (IFLOOR ICEIL IROUND IRINT)
3088 lfn (LFLOOR LCEIL LROUND LRINT)
3089 llfn (LLFLOOR LLCEIL LLROUND LLRINT)
3090 /* Canonicalize iround (x) to lround (x) on ILP32 targets where
3091 sizeof (int) == sizeof (long). */
3092 (if (TYPE_PRECISION (integer_type_node)
3093 == TYPE_PRECISION (long_integer_type_node))
3096 (lfn:long_integer_type_node @0)))
3097 /* Canonicalize llround (x) to lround (x) on LP64 targets where
3098 sizeof (long long) == sizeof (long). */
3099 (if (TYPE_PRECISION (long_long_integer_type_node)
3100 == TYPE_PRECISION (long_integer_type_node))
3103 (lfn:long_integer_type_node @0)))))
3105 /* cproj(x) -> x if we're ignoring infinities. */
3108 (if (!HONOR_INFINITIES (type))
3111 /* If the real part is inf and the imag part is known to be
3112 nonnegative, return (inf + 0i). */
3114 (CPROJ (complex REAL_CST@0 tree_expr_nonnegative_p@1))
3115 (if (real_isinf (TREE_REAL_CST_PTR (@0)))
3116 { build_complex_inf (type, false); }))
3118 /* If the imag part is inf, return (inf+I*copysign(0,imag)). */
3120 (CPROJ (complex @0 REAL_CST@1))
3121 (if (real_isinf (TREE_REAL_CST_PTR (@1)))
3122 { build_complex_inf (type, TREE_REAL_CST_PTR (@1)->sign); }))
3128 (pows @0 REAL_CST@1)
3130 const REAL_VALUE_TYPE *value = TREE_REAL_CST_PTR (@1);
3131 REAL_VALUE_TYPE tmp;
3134 /* pow(x,0) -> 1. */
3135 (if (real_equal (value, &dconst0))
3136 { build_real (type, dconst1); })
3137 /* pow(x,1) -> x. */
3138 (if (real_equal (value, &dconst1))
3140 /* pow(x,-1) -> 1/x. */
3141 (if (real_equal (value, &dconstm1))
3142 (rdiv { build_real (type, dconst1); } @0))
3143 /* pow(x,0.5) -> sqrt(x). */
3144 (if (flag_unsafe_math_optimizations
3145 && canonicalize_math_p ()
3146 && real_equal (value, &dconsthalf))
3148 /* pow(x,1/3) -> cbrt(x). */
3149 (if (flag_unsafe_math_optimizations
3150 && canonicalize_math_p ()
3151 && (tmp = real_value_truncate (TYPE_MODE (type), dconst_third ()),
3152 real_equal (value, &tmp)))
3155 /* powi(1,x) -> 1. */
3157 (POWI real_onep@0 @1)
3161 (POWI @0 INTEGER_CST@1)
3163 /* powi(x,0) -> 1. */
3164 (if (wi::eq_p (@1, 0))
3165 { build_real (type, dconst1); })
3166 /* powi(x,1) -> x. */
3167 (if (wi::eq_p (@1, 1))
3169 /* powi(x,-1) -> 1/x. */
3170 (if (wi::eq_p (@1, -1))
3171 (rdiv { build_real (type, dconst1); } @0))))
3173 /* Narrowing of arithmetic and logical operations.
3175 These are conceptually similar to the transformations performed for
3176 the C/C++ front-ends by shorten_binary_op and shorten_compare. Long
3177 term we want to move all that code out of the front-ends into here. */
3179 /* If we have a narrowing conversion of an arithmetic operation where
3180 both operands are widening conversions from the same type as the outer
3181 narrowing conversion. Then convert the innermost operands to a suitable
3182 unsigned type (to avoid introducing undefined behavior), perform the
3183 operation and convert the result to the desired type. */
3184 (for op (plus minus)
3186 (convert (op:s (convert@2 @0) (convert@3 @1)))
3187 (if (INTEGRAL_TYPE_P (type)
3188 /* We check for type compatibility between @0 and @1 below,
3189 so there's no need to check that @1/@3 are integral types. */
3190 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
3191 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
3192 /* The precision of the type of each operand must match the
3193 precision of the mode of each operand, similarly for the
3195 && (TYPE_PRECISION (TREE_TYPE (@0))
3196 == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (@0))))
3197 && (TYPE_PRECISION (TREE_TYPE (@1))
3198 == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (@1))))
3199 && TYPE_PRECISION (type) == GET_MODE_PRECISION (TYPE_MODE (type))
3200 /* The inner conversion must be a widening conversion. */
3201 && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
3202 && types_match (@0, @1)
3203 && types_match (@0, type))
3204 (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
3205 (convert (op @0 @1))
3206 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
3207 (convert (op (convert:utype @0) (convert:utype @1))))))))
3209 /* This is another case of narrowing, specifically when there's an outer
3210 BIT_AND_EXPR which masks off bits outside the type of the innermost
3211 operands. Like the previous case we have to convert the operands
3212 to unsigned types to avoid introducing undefined behavior for the
3213 arithmetic operation. */
3214 (for op (minus plus)
3216 (bit_and (op:s (convert@2 @0) (convert@3 @1)) INTEGER_CST@4)
3217 (if (INTEGRAL_TYPE_P (type)
3218 /* We check for type compatibility between @0 and @1 below,
3219 so there's no need to check that @1/@3 are integral types. */
3220 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
3221 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
3222 /* The precision of the type of each operand must match the
3223 precision of the mode of each operand, similarly for the
3225 && (TYPE_PRECISION (TREE_TYPE (@0))
3226 == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (@0))))
3227 && (TYPE_PRECISION (TREE_TYPE (@1))
3228 == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (@1))))
3229 && TYPE_PRECISION (type) == GET_MODE_PRECISION (TYPE_MODE (type))
3230 /* The inner conversion must be a widening conversion. */
3231 && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
3232 && types_match (@0, @1)
3233 && (tree_int_cst_min_precision (@4, TYPE_SIGN (TREE_TYPE (@0)))
3234 <= TYPE_PRECISION (TREE_TYPE (@0)))
3235 && (wi::bit_and (@4, wi::mask (TYPE_PRECISION (TREE_TYPE (@0)),
3236 true, TYPE_PRECISION (type))) == 0))
3237 (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
3238 (with { tree ntype = TREE_TYPE (@0); }
3239 (convert (bit_and (op @0 @1) (convert:ntype @4))))
3240 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
3241 (convert (bit_and (op (convert:utype @0) (convert:utype @1))
3242 (convert:utype @4))))))))
3244 /* Transform (@0 < @1 and @0 < @2) to use min,
3245 (@0 > @1 and @0 > @2) to use max */
3246 (for op (lt le gt ge)
3247 ext (min min max max)
3249 (bit_and (op:s @0 @1) (op:s @0 @2))
3250 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)))
3251 (op @0 (ext @1 @2)))))
3254 /* signbit(x) -> 0 if x is nonnegative. */
3255 (SIGNBIT tree_expr_nonnegative_p@0)
3256 { integer_zero_node; })
3259 /* signbit(x) -> x<0 if x doesn't have signed zeros. */
3261 (if (!HONOR_SIGNED_ZEROS (@0))
3262 (convert (lt @0 { build_real (TREE_TYPE (@0), dconst0); }))))
3264 /* Transform comparisons of the form X +- C1 CMP C2 to X CMP C2 -+ C1. */
3266 (for op (plus minus)
3269 (cmp (op@3 @0 INTEGER_CST@1) INTEGER_CST@2)
3270 (if (!TREE_OVERFLOW (@1) && !TREE_OVERFLOW (@2)
3271 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0))
3272 && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@0))
3273 && !TYPE_SATURATING (TREE_TYPE (@0)))
3274 (with { tree res = int_const_binop (rop, @2, @1); }
3275 (if (TREE_OVERFLOW (res))
3276 { constant_boolean_node (cmp == NE_EXPR, type); }
3277 (if (single_use (@3))
3278 (cmp @0 { res; }))))))))
3279 (for cmp (lt le gt ge)
3280 (for op (plus minus)
3283 (cmp (op@3 @0 INTEGER_CST@1) INTEGER_CST@2)
3284 (if (!TREE_OVERFLOW (@1) && !TREE_OVERFLOW (@2)
3285 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
3286 (with { tree res = int_const_binop (rop, @2, @1); }
3287 (if (TREE_OVERFLOW (res))
3289 fold_overflow_warning (("assuming signed overflow does not occur "
3290 "when simplifying conditional to constant"),
3291 WARN_STRICT_OVERFLOW_CONDITIONAL);
3292 bool less = cmp == LE_EXPR || cmp == LT_EXPR;
3293 /* wi::ges_p (@2, 0) should be sufficient for a signed type. */
3294 bool ovf_high = wi::lt_p (@1, 0, TYPE_SIGN (TREE_TYPE (@1)))
3295 != (op == MINUS_EXPR);
3296 constant_boolean_node (less == ovf_high, type);
3298 (if (single_use (@3))
3301 fold_overflow_warning (("assuming signed overflow does not occur "
3302 "when changing X +- C1 cmp C2 to "
3304 WARN_STRICT_OVERFLOW_COMPARISON);
3306 (cmp @0 { res; })))))))))
3308 /* Canonicalizations of BIT_FIELD_REFs. */
3311 (BIT_FIELD_REF @0 @1 @2)
3313 (if (TREE_CODE (TREE_TYPE (@0)) == COMPLEX_TYPE
3314 && tree_int_cst_equal (@1, TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))))
3316 (if (integer_zerop (@2))
3317 (view_convert (realpart @0)))
3318 (if (tree_int_cst_equal (@2, TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))))
3319 (view_convert (imagpart @0)))))
3320 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
3321 && INTEGRAL_TYPE_P (type)
3322 /* On GIMPLE this should only apply to register arguments. */
3323 && (! GIMPLE || is_gimple_reg (@0))
3324 /* A bit-field-ref that referenced the full argument can be stripped. */
3325 && ((compare_tree_int (@1, TYPE_PRECISION (TREE_TYPE (@0))) == 0
3326 && integer_zerop (@2))
3327 /* Low-parts can be reduced to integral conversions.
3328 ??? The following doesn't work for PDP endian. */
3329 || (BYTES_BIG_ENDIAN == WORDS_BIG_ENDIAN
3330 /* Don't even think about BITS_BIG_ENDIAN. */
3331 && TYPE_PRECISION (TREE_TYPE (@0)) % BITS_PER_UNIT == 0
3332 && TYPE_PRECISION (type) % BITS_PER_UNIT == 0
3333 && compare_tree_int (@2, (BYTES_BIG_ENDIAN
3334 ? (TYPE_PRECISION (TREE_TYPE (@0))
3335 - TYPE_PRECISION (type))
3339 /* Simplify vector extracts. */
3342 (BIT_FIELD_REF CONSTRUCTOR@0 @1 @2)
3343 (if (VECTOR_TYPE_P (TREE_TYPE (@0))
3344 && (types_match (type, TREE_TYPE (TREE_TYPE (@0)))
3345 || (VECTOR_TYPE_P (type)
3346 && types_match (TREE_TYPE (type), TREE_TYPE (TREE_TYPE (@0))))))
3349 tree ctor = (TREE_CODE (@0) == SSA_NAME
3350 ? gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0)) : @0);
3351 tree eltype = TREE_TYPE (TREE_TYPE (ctor));
3352 unsigned HOST_WIDE_INT width = tree_to_uhwi (TYPE_SIZE (eltype));
3353 unsigned HOST_WIDE_INT n = tree_to_uhwi (@1);
3354 unsigned HOST_WIDE_INT idx = tree_to_uhwi (@2);
3357 && (idx % width) == 0
3359 && ((idx + n) / width) <= TYPE_VECTOR_SUBPARTS (TREE_TYPE (ctor)))
3364 /* Constructor elements can be subvectors. */
3365 unsigned HOST_WIDE_INT k = 1;
3366 if (CONSTRUCTOR_NELTS (ctor) != 0)
3368 tree cons_elem = TREE_TYPE (CONSTRUCTOR_ELT (ctor, 0)->value);
3369 if (TREE_CODE (cons_elem) == VECTOR_TYPE)
3370 k = TYPE_VECTOR_SUBPARTS (cons_elem);
3374 /* We keep an exact subset of the constructor elements. */
3375 (if ((idx % k) == 0 && (n % k) == 0)
3376 (if (CONSTRUCTOR_NELTS (ctor) == 0)
3377 { build_constructor (type, NULL); }
3384 (if (idx < CONSTRUCTOR_NELTS (ctor))
3385 { CONSTRUCTOR_ELT (ctor, idx)->value; }
3386 { build_zero_cst (type); })
3388 vec<constructor_elt, va_gc> *vals;
3389 vec_alloc (vals, n);
3390 for (unsigned i = 0;
3391 i < n && idx + i < CONSTRUCTOR_NELTS (ctor); ++i)
3392 CONSTRUCTOR_APPEND_ELT (vals, NULL_TREE,
3393 CONSTRUCTOR_ELT (ctor, idx + i)->value);
3394 build_constructor (type, vals);
3396 /* The bitfield references a single constructor element. */
3397 (if (idx + n <= (idx / k + 1) * k)
3399 (if (CONSTRUCTOR_NELTS (ctor) <= idx / k)
3400 { build_zero_cst (type); })
3402 { CONSTRUCTOR_ELT (ctor, idx / k)->value; })
3403 (BIT_FIELD_REF { CONSTRUCTOR_ELT (ctor, idx / k)->value; }
3404 @1 { bitsize_int ((idx % k) * width); })))))))))