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-2015 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 /* Define an operand list for math function FN, with float, double and
51 long double variants (in that order). */
52 #define DEFINE_MATH_FN(FN) \
53 (define_operator_list FN BUILT_IN_##FN##F BUILT_IN_##FN BUILT_IN_##FN##L)
55 /* Define operand lists for math rounding functions {,i,l,ll}FN,
56 where the versions prefixed with "i" return an int, those prefixed with
57 "l" return a long and those prefixed with "ll" return a long long.
59 Also define operand lists:
61 X<FN>F for all float functions, in the order i, l, ll
62 X<FN> for all double functions, in the same order
63 X<FN>L for all long double functions, in the same order. */
64 #define DEFINE_INT_AND_FLOAT_ROUND_FN(FN) \
66 DEFINE_MATH_FN (I##FN) \
67 DEFINE_MATH_FN (L##FN) \
68 DEFINE_MATH_FN (LL##FN) \
69 (define_operator_list X##FN##F BUILT_IN_I##FN##F \
72 (define_operator_list X##FN BUILT_IN_I##FN \
75 (define_operator_list X##FN##L BUILT_IN_I##FN##L \
83 DEFINE_MATH_FN (LOG10)
84 DEFINE_MATH_FN (EXP10)
86 DEFINE_MATH_FN (POW10)
96 DEFINE_MATH_FN (CEXPI)
97 DEFINE_MATH_FN (CPROJ)
99 DEFINE_MATH_FN (CCOSH)
100 DEFINE_MATH_FN (HYPOT)
101 DEFINE_MATH_FN (COPYSIGN)
102 DEFINE_MATH_FN (CABS)
103 DEFINE_MATH_FN (TRUNC)
104 DEFINE_MATH_FN (NEARBYINT)
105 DEFINE_MATH_FN (SIGNBIT)
106 DEFINE_MATH_FN (FMIN)
107 DEFINE_MATH_FN (FMAX)
108 DEFINE_MATH_FN (LDEXP)
109 DEFINE_MATH_FN (SCALBN)
110 DEFINE_MATH_FN (SCALBLN)
112 DEFINE_INT_AND_FLOAT_ROUND_FN (FLOOR)
113 DEFINE_INT_AND_FLOAT_ROUND_FN (CEIL)
114 DEFINE_INT_AND_FLOAT_ROUND_FN (ROUND)
115 DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
117 /* Simplifications of operations with one constant operand and
118 simplifications to constants or single values. */
120 (for op (plus pointer_plus minus bit_ior bit_xor)
122 (op @0 integer_zerop)
125 /* 0 +p index -> (type)index */
127 (pointer_plus integer_zerop @1)
128 (non_lvalue (convert @1)))
130 /* See if ARG1 is zero and X + ARG1 reduces to X.
131 Likewise if the operands are reversed. */
133 (plus:c @0 real_zerop@1)
134 (if (fold_real_zero_addition_p (type, @1, 0))
137 /* See if ARG1 is zero and X - ARG1 reduces to X. */
139 (minus @0 real_zerop@1)
140 (if (fold_real_zero_addition_p (type, @1, 1))
144 This is unsafe for certain floats even in non-IEEE formats.
145 In IEEE, it is unsafe because it does wrong for NaNs.
146 Also note that operand_equal_p is always false if an operand
150 (if (!FLOAT_TYPE_P (type) || !HONOR_NANS (type))
151 { build_zero_cst (type); }))
154 (mult @0 integer_zerop@1)
157 /* Maybe fold x * 0 to 0. The expressions aren't the same
158 when x is NaN, since x * 0 is also NaN. Nor are they the
159 same in modes with signed zeros, since multiplying a
160 negative value by 0 gives -0, not +0. */
162 (mult @0 real_zerop@1)
163 (if (!HONOR_NANS (type) && !HONOR_SIGNED_ZEROS (type))
166 /* In IEEE floating point, x*1 is not equivalent to x for snans.
167 Likewise for complex arithmetic with signed zeros. */
170 (if (!HONOR_SNANS (type)
171 && (!HONOR_SIGNED_ZEROS (type)
172 || !COMPLEX_FLOAT_TYPE_P (type)))
175 /* Transform x * -1.0 into -x. */
177 (mult @0 real_minus_onep)
178 (if (!HONOR_SNANS (type)
179 && (!HONOR_SIGNED_ZEROS (type)
180 || !COMPLEX_FLOAT_TYPE_P (type)))
183 /* Make sure to preserve divisions by zero. This is the reason why
184 we don't simplify x / x to 1 or 0 / x to 0. */
185 (for op (mult trunc_div ceil_div floor_div round_div exact_div)
191 (for div (trunc_div ceil_div floor_div round_div exact_div)
193 (div @0 integer_minus_onep@1)
194 (if (!TYPE_UNSIGNED (type))
197 /* For unsigned integral types, FLOOR_DIV_EXPR is the same as
198 TRUNC_DIV_EXPR. Rewrite into the latter in this case. */
201 (if ((INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
202 && TYPE_UNSIGNED (type))
205 /* Combine two successive divisions. Note that combining ceil_div
206 and floor_div is trickier and combining round_div even more so. */
207 (for div (trunc_div exact_div)
209 (div (div @0 INTEGER_CST@1) INTEGER_CST@2)
212 wide_int mul = wi::mul (@1, @2, TYPE_SIGN (type), &overflow_p);
215 (div @0 { wide_int_to_tree (type, mul); })
216 (if (TYPE_UNSIGNED (type)
217 || mul != wi::min_value (TYPE_PRECISION (type), SIGNED))
218 { build_zero_cst (type); })))))
220 /* Optimize A / A to 1.0 if we don't care about
221 NaNs or Infinities. */
224 (if (FLOAT_TYPE_P (type)
225 && ! HONOR_NANS (type)
226 && ! HONOR_INFINITIES (type))
227 { build_one_cst (type); }))
229 /* Optimize -A / A to -1.0 if we don't care about
230 NaNs or Infinities. */
232 (rdiv:c @0 (negate @0))
233 (if (FLOAT_TYPE_P (type)
234 && ! HONOR_NANS (type)
235 && ! HONOR_INFINITIES (type))
236 { build_minus_one_cst (type); }))
238 /* In IEEE floating point, x/1 is not equivalent to x for snans. */
241 (if (!HONOR_SNANS (type))
244 /* In IEEE floating point, x/-1 is not equivalent to -x for snans. */
246 (rdiv @0 real_minus_onep)
247 (if (!HONOR_SNANS (type))
250 /* If ARG1 is a constant, we can convert this to a multiply by the
251 reciprocal. This does not have the same rounding properties,
252 so only do this if -freciprocal-math. We can actually
253 always safely do it if ARG1 is a power of two, but it's hard to
254 tell if it is or not in a portable manner. */
255 (for cst (REAL_CST COMPLEX_CST VECTOR_CST)
259 (if (flag_reciprocal_math
262 { tree tem = const_binop (RDIV_EXPR, type, build_one_cst (type), @1); }
264 (mult @0 { tem; } )))
265 (if (cst != COMPLEX_CST)
266 (with { tree inverse = exact_inverse (type, @1); }
268 (mult @0 { inverse; } ))))))))
270 /* Same applies to modulo operations, but fold is inconsistent here
271 and simplifies 0 % x to 0, only preserving literal 0 % 0. */
272 (for mod (ceil_mod floor_mod round_mod trunc_mod)
273 /* 0 % X is always zero. */
275 (mod integer_zerop@0 @1)
276 /* But not for 0 % 0 so that we can get the proper warnings and errors. */
277 (if (!integer_zerop (@1))
279 /* X % 1 is always zero. */
281 (mod @0 integer_onep)
282 { build_zero_cst (type); })
283 /* X % -1 is zero. */
285 (mod @0 integer_minus_onep@1)
286 (if (!TYPE_UNSIGNED (type))
287 { build_zero_cst (type); }))
288 /* (X % Y) % Y is just X % Y. */
290 (mod (mod@2 @0 @1) @1)
292 /* From extract_muldiv_1: (X * C1) % C2 is zero if C1 is a multiple of C2. */
294 (mod (mult @0 INTEGER_CST@1) INTEGER_CST@2)
295 (if (ANY_INTEGRAL_TYPE_P (type)
296 && TYPE_OVERFLOW_UNDEFINED (type)
297 && wi::multiple_of_p (@1, @2, TYPE_SIGN (type)))
298 { build_zero_cst (type); })))
300 /* X % -C is the same as X % C. */
302 (trunc_mod @0 INTEGER_CST@1)
303 (if (TYPE_SIGN (type) == SIGNED
304 && !TREE_OVERFLOW (@1)
306 && !TYPE_OVERFLOW_TRAPS (type)
307 /* Avoid this transformation if C is INT_MIN, i.e. C == -C. */
308 && !sign_bit_p (@1, @1))
309 (trunc_mod @0 (negate @1))))
311 /* X % -Y is the same as X % Y. */
313 (trunc_mod @0 (convert? (negate @1)))
314 (if (!TYPE_UNSIGNED (type)
315 && !TYPE_OVERFLOW_TRAPS (type)
316 && tree_nop_conversion_p (type, TREE_TYPE (@1)))
317 (trunc_mod @0 (convert @1))))
319 /* X - (X / Y) * Y is the same as X % Y. */
321 (minus (convert1? @2) (convert2? (mult:c (trunc_div @0 @1) @1)))
322 /* We cannot use matching captures here, since in the case of
323 constants we really want the type of @0, not @2. */
324 (if (operand_equal_p (@0, @2, 0)
325 && (INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type)))
326 (convert (trunc_mod @0 @1))))
328 /* Optimize TRUNC_MOD_EXPR by a power of two into a BIT_AND_EXPR,
329 i.e. "X % C" into "X & (C - 1)", if X and C are positive.
330 Also optimize A % (C << N) where C is a power of 2,
331 to A & ((C << N) - 1). */
332 (match (power_of_two_cand @1)
334 (match (power_of_two_cand @1)
335 (lshift INTEGER_CST@1 @2))
336 (for mod (trunc_mod floor_mod)
338 (mod @0 (convert?@3 (power_of_two_cand@1 @2)))
339 (if ((TYPE_UNSIGNED (type)
340 || tree_expr_nonnegative_p (@0))
341 && tree_nop_conversion_p (type, TREE_TYPE (@3))
342 && integer_pow2p (@2) && tree_int_cst_sgn (@2) > 0)
343 (bit_and @0 (convert (minus @1 { build_int_cst (TREE_TYPE (@1), 1); }))))))
345 /* Simplify (unsigned t * 2)/2 -> unsigned t & 0x7FFFFFFF. */
347 (trunc_div (mult @0 integer_pow2p@1) @1)
348 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
349 (bit_and @0 { wide_int_to_tree
350 (type, wi::mask (TYPE_PRECISION (type) - wi::exact_log2 (@1),
351 false, TYPE_PRECISION (type))); })))
353 /* Simplify (unsigned t / 2) * 2 -> unsigned t & ~1. */
355 (mult (trunc_div @0 integer_pow2p@1) @1)
356 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
357 (bit_and @0 (negate @1))))
359 /* Simplify (t * 2) / 2) -> t. */
360 (for div (trunc_div ceil_div floor_div round_div exact_div)
362 (div (mult @0 @1) @1)
363 (if (ANY_INTEGRAL_TYPE_P (type)
364 && TYPE_OVERFLOW_UNDEFINED (type))
368 /* Simplify cos(-x) and cos(|x|) -> cos(x). Similarly for cosh. */
373 /* Simplify pow(-x, y) and pow(|x|,y) -> pow(x,y) if y is an even integer. */
376 (pows (op @0) REAL_CST@1)
377 (with { HOST_WIDE_INT n; }
378 (if (real_isinteger (&TREE_REAL_CST (@1), &n) && (n & 1) == 0)
380 /* Strip negate and abs from both operands of hypot. */
388 /* copysign(-x, y) and copysign(abs(x), y) -> copysign(x, y). */
389 (for copysigns (COPYSIGN)
391 (copysigns (op @0) @1)
394 /* abs(x)*abs(x) -> x*x. Should be valid for all types. */
399 /* cos(copysign(x, y)) -> cos(x). Similarly for cosh. */
403 (coss (copysigns @0 @1))
406 /* pow(copysign(x, y), z) -> pow(x, z) if z is an even integer. */
410 (pows (copysigns @0 @1) REAL_CST@1)
411 (with { HOST_WIDE_INT n; }
412 (if (real_isinteger (&TREE_REAL_CST (@1), &n) && (n & 1) == 0)
417 /* hypot(copysign(x, y), z) -> hypot(x, z). */
419 (hypots (copysigns @0 @1) @2)
421 /* hypot(x, copysign(y, z)) -> hypot(x, y). */
423 (hypots @0 (copysigns @1 @2))
426 /* copysign(copysign(x, y), z) -> copysign(x, z). */
427 (for copysigns (COPYSIGN)
429 (copysigns (copysigns @0 @1) @2)
432 /* copysign(x,y)*copysign(x,y) -> x*x. */
433 (for copysigns (COPYSIGN)
435 (mult (copysigns@2 @0 @1) @2)
438 /* ccos(-x) -> ccos(x). Similarly for ccosh. */
439 (for ccoss (CCOS CCOSH)
444 /* cabs(-x) and cos(conj(x)) -> cabs(x). */
445 (for ops (conj negate)
451 /* Fold (a * (1 << b)) into (a << b) */
453 (mult:c @0 (convert? (lshift integer_onep@1 @2)))
454 (if (! FLOAT_TYPE_P (type)
455 && tree_nop_conversion_p (type, TREE_TYPE (@1)))
458 /* Fold (C1/X)*C2 into (C1*C2)/X. */
460 (mult (rdiv:s REAL_CST@0 @1) REAL_CST@2)
461 (if (flag_associative_math)
463 { tree tem = const_binop (MULT_EXPR, type, @0, @2); }
465 (rdiv { tem; } @1)))))
467 /* Simplify ~X & X as zero. */
469 (bit_and:c (convert? @0) (convert? (bit_not @0)))
470 { build_zero_cst (type); })
472 /* Fold (A & ~B) - (A & B) into (A ^ B) - B. */
474 (minus (bit_and:cs @0 (bit_not @1)) (bit_and:cs @0 @1))
475 (minus (bit_xor @0 @1) @1))
477 (minus (bit_and:s @0 INTEGER_CST@2) (bit_and:s @0 INTEGER_CST@1))
478 (if (wi::bit_not (@2) == @1)
479 (minus (bit_xor @0 @1) @1)))
481 /* Fold (A & B) - (A & ~B) into B - (A ^ B). */
483 (minus (bit_and:s @0 @1) (bit_and:cs @0 (bit_not @1)))
484 (minus @1 (bit_xor @0 @1)))
486 /* Simplify (X & ~Y) | (~X & Y) -> X ^ Y. */
488 (bit_ior (bit_and:c @0 (bit_not @1)) (bit_and:c (bit_not @0) @1))
491 (bit_ior:c (bit_and @0 INTEGER_CST@2) (bit_and (bit_not @0) INTEGER_CST@1))
492 (if (wi::bit_not (@2) == @1)
495 /* X % Y is smaller than Y. */
498 (cmp (trunc_mod @0 @1) @1)
499 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
500 { constant_boolean_node (cmp == LT_EXPR, type); })))
503 (cmp @1 (trunc_mod @0 @1))
504 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
505 { constant_boolean_node (cmp == GT_EXPR, type); })))
509 (bit_ior @0 integer_all_onesp@1)
514 (bit_and @0 integer_zerop@1)
520 (for op (bit_ior bit_xor plus)
522 (op:c (convert? @0) (convert? (bit_not @0)))
523 (convert { build_all_ones_cst (TREE_TYPE (@0)); })))
528 { build_zero_cst (type); })
530 /* Canonicalize X ^ ~0 to ~X. */
532 (bit_xor @0 integer_all_onesp@1)
537 (bit_and @0 integer_all_onesp)
540 /* x & x -> x, x | x -> x */
541 (for bitop (bit_and bit_ior)
546 /* x + (x & 1) -> (x + 1) & ~1 */
548 (plus:c @0 (bit_and:s @0 integer_onep@1))
549 (bit_and (plus @0 @1) (bit_not @1)))
551 /* x & ~(x & y) -> x & ~y */
552 /* x | ~(x | y) -> x | ~y */
553 (for bitop (bit_and bit_ior)
555 (bitop:c @0 (bit_not (bitop:cs @0 @1)))
556 (bitop @0 (bit_not @1))))
558 /* (x | y) & ~x -> y & ~x */
559 /* (x & y) | ~x -> y | ~x */
560 (for bitop (bit_and bit_ior)
561 rbitop (bit_ior bit_and)
563 (bitop:c (rbitop:c @0 @1) (bit_not@2 @0))
566 /* (x & y) ^ (x | y) -> x ^ y */
568 (bit_xor:c (bit_and @0 @1) (bit_ior @0 @1))
571 /* (x ^ y) ^ (x | y) -> x & y */
573 (bit_xor:c (bit_xor @0 @1) (bit_ior @0 @1))
576 /* (x & y) + (x ^ y) -> x | y */
577 /* (x & y) | (x ^ y) -> x | y */
578 /* (x & y) ^ (x ^ y) -> x | y */
579 (for op (plus bit_ior bit_xor)
581 (op:c (bit_and @0 @1) (bit_xor @0 @1))
584 /* (x & y) + (x | y) -> x + y */
586 (plus:c (bit_and @0 @1) (bit_ior @0 @1))
589 /* (x + y) - (x | y) -> x & y */
591 (minus (plus @0 @1) (bit_ior @0 @1))
592 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
593 && !TYPE_SATURATING (type))
596 /* (x + y) - (x & y) -> x | y */
598 (minus (plus @0 @1) (bit_and @0 @1))
599 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
600 && !TYPE_SATURATING (type))
603 /* (x | y) - (x ^ y) -> x & y */
605 (minus (bit_ior @0 @1) (bit_xor @0 @1))
608 /* (x | y) - (x & y) -> x ^ y */
610 (minus (bit_ior @0 @1) (bit_and @0 @1))
613 /* (x | y) & ~(x & y) -> x ^ y */
615 (bit_and:c (bit_ior @0 @1) (bit_not (bit_and @0 @1)))
618 /* (x | y) & (~x ^ y) -> x & y */
620 (bit_and:c (bit_ior:c @0 @1) (bit_xor:c @1 (bit_not @0)))
623 /* ~x & ~y -> ~(x | y)
624 ~x | ~y -> ~(x & y) */
625 (for op (bit_and bit_ior)
626 rop (bit_ior bit_and)
628 (op (convert1? (bit_not @0)) (convert2? (bit_not @1)))
629 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
630 && tree_nop_conversion_p (type, TREE_TYPE (@1)))
631 (bit_not (rop (convert @0) (convert @1))))))
633 /* If we are XORing or adding two BIT_AND_EXPR's, both of which are and'ing
634 with a constant, and the two constants have no bits in common,
635 we should treat this as a BIT_IOR_EXPR since this may produce more
637 (for op (bit_xor plus)
639 (op (convert1? (bit_and@4 @0 INTEGER_CST@1))
640 (convert2? (bit_and@5 @2 INTEGER_CST@3)))
641 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
642 && tree_nop_conversion_p (type, TREE_TYPE (@2))
643 && wi::bit_and (@1, @3) == 0)
644 (bit_ior (convert @4) (convert @5)))))
646 /* (X | Y) ^ X -> Y & ~ X*/
648 (bit_xor:c (convert? (bit_ior:c @0 @1)) (convert? @0))
649 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
650 (convert (bit_and @1 (bit_not @0)))))
652 /* Convert ~X ^ ~Y to X ^ Y. */
654 (bit_xor (convert1? (bit_not @0)) (convert2? (bit_not @1)))
655 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
656 && tree_nop_conversion_p (type, TREE_TYPE (@1)))
657 (bit_xor (convert @0) (convert @1))))
659 /* Convert ~X ^ C to X ^ ~C. */
661 (bit_xor (convert? (bit_not @0)) INTEGER_CST@1)
662 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
663 (bit_xor (convert @0) (bit_not @1))))
665 /* Fold (X & Y) ^ Y as ~X & Y. */
667 (bit_xor:c (bit_and:c @0 @1) @1)
668 (bit_and (bit_not @0) @1))
670 /* Given a bit-wise operation CODE applied to ARG0 and ARG1, see if both
671 operands are another bit-wise operation with a common input. If so,
672 distribute the bit operations to save an operation and possibly two if
673 constants are involved. For example, convert
674 (A | B) & (A | C) into A | (B & C)
675 Further simplification will occur if B and C are constants. */
676 (for op (bit_and bit_ior)
677 rop (bit_ior bit_and)
679 (op (convert? (rop:c @0 @1)) (convert? (rop @0 @2)))
680 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
681 (rop (convert @0) (op (convert @1) (convert @2))))))
691 (abs tree_expr_nonnegative_p@0)
694 /* A few cases of fold-const.c negate_expr_p predicate. */
697 (if ((INTEGRAL_TYPE_P (type)
698 && TYPE_OVERFLOW_WRAPS (type))
699 || (!TYPE_OVERFLOW_SANITIZED (type)
700 && may_negate_without_overflow_p (t)))))
705 (if (!TYPE_OVERFLOW_SANITIZED (type))))
708 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (t)))))
709 /* VECTOR_CST handling of non-wrapping types would recurse in unsupported
713 (if (FLOAT_TYPE_P (TREE_TYPE (type)) || TYPE_OVERFLOW_WRAPS (type))))
715 /* (-A) * (-B) -> A * B */
717 (mult:c (convert1? (negate @0)) (convert2? negate_expr_p@1))
718 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
719 && tree_nop_conversion_p (type, TREE_TYPE (@1)))
720 (mult (convert @0) (convert (negate @1)))))
722 /* -(A + B) -> (-B) - A. */
724 (negate (plus:c @0 negate_expr_p@1))
725 (if (!HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type))
726 && !HONOR_SIGNED_ZEROS (element_mode (type)))
727 (minus (negate @1) @0)))
729 /* A - B -> A + (-B) if B is easily negatable. */
731 (minus @0 negate_expr_p@1)
732 (if (!FIXED_POINT_TYPE_P (type))
733 (plus @0 (negate @1))))
735 /* Try to fold (type) X op CST -> (type) (X op ((type-x) CST))
737 For bitwise binary operations apply operand conversions to the
738 binary operation result instead of to the operands. This allows
739 to combine successive conversions and bitwise binary operations.
740 We combine the above two cases by using a conditional convert. */
741 (for bitop (bit_and bit_ior bit_xor)
743 (bitop (convert @0) (convert? @1))
744 (if (((TREE_CODE (@1) == INTEGER_CST
745 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
746 && int_fits_type_p (@1, TREE_TYPE (@0)))
747 || types_match (@0, @1))
748 /* ??? This transform conflicts with fold-const.c doing
749 Convert (T)(x & c) into (T)x & (T)c, if c is an integer
750 constants (if x has signed type, the sign bit cannot be set
751 in c). This folds extension into the BIT_AND_EXPR.
752 Restrict it to GIMPLE to avoid endless recursions. */
753 && (bitop != BIT_AND_EXPR || GIMPLE)
754 && (/* That's a good idea if the conversion widens the operand, thus
755 after hoisting the conversion the operation will be narrower. */
756 TYPE_PRECISION (TREE_TYPE (@0)) < TYPE_PRECISION (type)
757 /* It's also a good idea if the conversion is to a non-integer
759 || GET_MODE_CLASS (TYPE_MODE (type)) != MODE_INT
760 /* Or if the precision of TO is not the same as the precision
762 || TYPE_PRECISION (type) != GET_MODE_PRECISION (TYPE_MODE (type))))
763 (convert (bitop @0 (convert @1))))))
765 (for bitop (bit_and bit_ior)
766 rbitop (bit_ior bit_and)
767 /* (x | y) & x -> x */
768 /* (x & y) | x -> x */
770 (bitop:c (rbitop:c @0 @1) @0)
772 /* (~x | y) & x -> x & y */
773 /* (~x & y) | x -> x | y */
775 (bitop:c (rbitop:c (bit_not @0) @1) @0)
778 /* Simplify (A & B) OP0 (C & B) to (A OP0 C) & B. */
779 (for bitop (bit_and bit_ior bit_xor)
781 (bitop (bit_and:c @0 @1) (bit_and @2 @1))
782 (bit_and (bitop @0 @2) @1)))
784 /* (x | CST1) & CST2 -> (x & CST2) | (CST1 & CST2) */
786 (bit_and (bit_ior @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
787 (bit_ior (bit_and @0 @2) (bit_and @1 @2)))
789 /* Combine successive equal operations with constants. */
790 (for bitop (bit_and bit_ior bit_xor)
792 (bitop (bitop @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
793 (bitop @0 (bitop @1 @2))))
795 /* Try simple folding for X op !X, and X op X with the help
796 of the truth_valued_p and logical_inverted_value predicates. */
797 (match truth_valued_p
799 (if (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1)))
800 (for op (tcc_comparison truth_and truth_andif truth_or truth_orif truth_xor)
801 (match truth_valued_p
803 (match truth_valued_p
806 (match (logical_inverted_value @0)
808 (match (logical_inverted_value @0)
809 (bit_not truth_valued_p@0))
810 (match (logical_inverted_value @0)
811 (eq @0 integer_zerop))
812 (match (logical_inverted_value @0)
813 (ne truth_valued_p@0 integer_truep))
814 (match (logical_inverted_value @0)
815 (bit_xor truth_valued_p@0 integer_truep))
819 (bit_and:c @0 (logical_inverted_value @0))
820 { build_zero_cst (type); })
821 /* X | !X and X ^ !X -> 1, , if X is truth-valued. */
822 (for op (bit_ior bit_xor)
824 (op:c truth_valued_p@0 (logical_inverted_value @0))
825 { constant_boolean_node (true, type); }))
826 /* X ==/!= !X is false/true. */
829 (op:c truth_valued_p@0 (logical_inverted_value @0))
830 { constant_boolean_node (op == NE_EXPR ? true : false, type); }))
832 /* If arg1 and arg2 are booleans (or any single bit type)
833 then try to simplify:
840 But only do this if our result feeds into a comparison as
841 this transformation is not always a win, particularly on
842 targets with and-not instructions.
843 -> simplify_bitwise_binary_boolean */
845 (ne (bit_and:c (bit_not @0) @1) integer_zerop)
846 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
847 && TYPE_PRECISION (TREE_TYPE (@1)) == 1)
850 (ne (bit_ior:c (bit_not @0) @1) integer_zerop)
851 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
852 && TYPE_PRECISION (TREE_TYPE (@1)) == 1)
857 (bit_not (bit_not @0))
860 /* Convert ~ (-A) to A - 1. */
862 (bit_not (convert? (negate @0)))
863 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
864 (convert (minus @0 { build_each_one_cst (TREE_TYPE (@0)); }))))
866 /* Convert ~ (A - 1) or ~ (A + -1) to -A. */
868 (bit_not (convert? (minus @0 integer_each_onep)))
869 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
870 (convert (negate @0))))
872 (bit_not (convert? (plus @0 integer_all_onesp)))
873 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
874 (convert (negate @0))))
876 /* Part of convert ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify. */
878 (bit_not (convert? (bit_xor @0 INTEGER_CST@1)))
879 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
880 (convert (bit_xor @0 (bit_not @1)))))
882 (bit_not (convert? (bit_xor:c (bit_not @0) @1)))
883 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
884 (convert (bit_xor @0 @1))))
886 /* (x & ~m) | (y & m) -> ((x ^ y) & m) ^ x */
888 (bit_ior:c (bit_and:cs @0 (bit_not @2)) (bit_and:cs @1 @2))
889 (bit_xor (bit_and (bit_xor @0 @1) @2) @0))
891 /* Fold A - (A & B) into ~B & A. */
893 (minus (convert? @0) (convert?:s (bit_and:cs @0 @1)))
894 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
895 && tree_nop_conversion_p (type, TREE_TYPE (@1)))
896 (convert (bit_and (bit_not @1) @0))))
900 /* ((X inner_op C0) outer_op C1)
901 With X being a tree where value_range has reasoned certain bits to always be
902 zero throughout its computed value range,
903 inner_op = {|,^}, outer_op = {|,^} and inner_op != outer_op
904 where zero_mask has 1's for all bits that are sure to be 0 in
906 if (inner_op == '^') C0 &= ~C1;
907 if ((C0 & ~zero_mask) == 0) then emit (X outer_op (C0 outer_op C1)
908 if ((C1 & ~zero_mask) == 0) then emit (X inner_op (C0 outer_op C1)
910 (for inner_op (bit_ior bit_xor)
911 outer_op (bit_xor bit_ior)
914 (inner_op:s @2 INTEGER_CST@0) INTEGER_CST@1)
918 wide_int zero_mask_not;
922 if (TREE_CODE (@2) == SSA_NAME)
923 zero_mask_not = get_nonzero_bits (@2);
927 if (inner_op == BIT_XOR_EXPR)
929 C0 = wi::bit_and_not (@0, @1);
930 cst_emit = wi::bit_or (C0, @1);
935 cst_emit = wi::bit_xor (@0, @1);
938 (if (!fail && wi::bit_and (C0, zero_mask_not) == 0)
939 (outer_op @2 { wide_int_to_tree (type, cst_emit); })
940 (if (!fail && wi::bit_and (@1, zero_mask_not) == 0)
941 (inner_op @2 { wide_int_to_tree (type, cst_emit); }))))))
943 /* Associate (p +p off1) +p off2 as (p +p (off1 + off2)). */
945 (pointer_plus (pointer_plus:s @0 @1) @3)
946 (pointer_plus @0 (plus @1 @3)))
952 tem4 = (unsigned long) tem3;
957 (pointer_plus @0 (convert?@2 (minus@3 (convert @1) (convert @0))))
958 /* Conditionally look through a sign-changing conversion. */
959 (if (TYPE_PRECISION (TREE_TYPE (@2)) == TYPE_PRECISION (TREE_TYPE (@3))
960 && ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@1)))
961 || (GENERIC && type == TREE_TYPE (@1))))
965 tem = (sizetype) ptr;
969 and produce the simpler and easier to analyze with respect to alignment
970 ... = ptr & ~algn; */
972 (pointer_plus @0 (negate (bit_and (convert @0) INTEGER_CST@1)))
973 (with { tree algn = wide_int_to_tree (TREE_TYPE (@0), wi::bit_not (@1)); }
974 (bit_and @0 { algn; })))
976 /* Try folding difference of addresses. */
978 (minus (convert ADDR_EXPR@0) (convert @1))
979 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
980 (with { HOST_WIDE_INT diff; }
981 (if (ptr_difference_const (@0, @1, &diff))
982 { build_int_cst_type (type, diff); }))))
984 (minus (convert @0) (convert ADDR_EXPR@1))
985 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
986 (with { HOST_WIDE_INT diff; }
987 (if (ptr_difference_const (@0, @1, &diff))
988 { build_int_cst_type (type, diff); }))))
990 /* If arg0 is derived from the address of an object or function, we may
991 be able to fold this expression using the object or function's
994 (bit_and (convert? @0) INTEGER_CST@1)
995 (if (POINTER_TYPE_P (TREE_TYPE (@0))
996 && tree_nop_conversion_p (type, TREE_TYPE (@0)))
1000 unsigned HOST_WIDE_INT bitpos;
1001 get_pointer_alignment_1 (@0, &align, &bitpos);
1003 (if (wi::ltu_p (@1, align / BITS_PER_UNIT))
1004 { wide_int_to_tree (type, wi::bit_and (@1, bitpos / BITS_PER_UNIT)); }))))
1007 /* We can't reassociate at all for saturating types. */
1008 (if (!TYPE_SATURATING (type))
1010 /* Contract negates. */
1011 /* A + (-B) -> A - B */
1013 (plus:c (convert1? @0) (convert2? (negate @1)))
1014 /* Apply STRIP_NOPS on @0 and the negate. */
1015 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
1016 && tree_nop_conversion_p (type, TREE_TYPE (@1))
1017 && !TYPE_OVERFLOW_SANITIZED (type))
1018 (minus (convert @0) (convert @1))))
1019 /* A - (-B) -> A + B */
1021 (minus (convert1? @0) (convert2? (negate @1)))
1022 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
1023 && tree_nop_conversion_p (type, TREE_TYPE (@1))
1024 && !TYPE_OVERFLOW_SANITIZED (type))
1025 (plus (convert @0) (convert @1))))
1028 (negate (convert? (negate @1)))
1029 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1030 && !TYPE_OVERFLOW_SANITIZED (type))
1033 /* We can't reassociate floating-point unless -fassociative-math
1034 or fixed-point plus or minus because of saturation to +-Inf. */
1035 (if ((!FLOAT_TYPE_P (type) || flag_associative_math)
1036 && !FIXED_POINT_TYPE_P (type))
1038 /* Match patterns that allow contracting a plus-minus pair
1039 irrespective of overflow issues. */
1040 /* (A +- B) - A -> +- B */
1041 /* (A +- B) -+ B -> A */
1042 /* A - (A +- B) -> -+ B */
1043 /* A +- (B -+ A) -> +- B */
1045 (minus (plus:c @0 @1) @0)
1048 (minus (minus @0 @1) @0)
1051 (plus:c (minus @0 @1) @1)
1054 (minus @0 (plus:c @0 @1))
1057 (minus @0 (minus @0 @1))
1060 /* (A +- CST) +- CST -> A + CST */
1061 (for outer_op (plus minus)
1062 (for inner_op (plus minus)
1064 (outer_op (inner_op @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
1065 /* If the constant operation overflows we cannot do the transform
1066 as we would introduce undefined overflow, for example
1067 with (a - 1) + INT_MIN. */
1068 (with { tree cst = fold_binary (outer_op == inner_op
1069 ? PLUS_EXPR : MINUS_EXPR, type, @1, @2); }
1070 (if (cst && !TREE_OVERFLOW (cst))
1071 (inner_op @0 { cst; } ))))))
1073 /* (CST - A) +- CST -> CST - A */
1074 (for outer_op (plus minus)
1076 (outer_op (minus CONSTANT_CLASS_P@1 @0) CONSTANT_CLASS_P@2)
1077 (with { tree cst = fold_binary (outer_op, type, @1, @2); }
1078 (if (cst && !TREE_OVERFLOW (cst))
1079 (minus { cst; } @0)))))
1083 (plus:c (bit_not @0) @0)
1084 (if (!TYPE_OVERFLOW_TRAPS (type))
1085 { build_all_ones_cst (type); }))
1089 (plus (convert? (bit_not @0)) integer_each_onep)
1090 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1091 (negate (convert @0))))
1095 (minus (convert? (negate @0)) integer_each_onep)
1096 (if (!TYPE_OVERFLOW_TRAPS (type)
1097 && tree_nop_conversion_p (type, TREE_TYPE (@0)))
1098 (bit_not (convert @0))))
1102 (minus integer_all_onesp @0)
1105 /* (T)(P + A) - (T)P -> (T) A */
1106 (for add (plus pointer_plus)
1108 (minus (convert (add @0 @1))
1110 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
1111 /* For integer types, if A has a smaller type
1112 than T the result depends on the possible
1114 E.g. T=size_t, A=(unsigned)429497295, P>0.
1115 However, if an overflow in P + A would cause
1116 undefined behavior, we can assume that there
1118 || (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1119 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1120 /* For pointer types, if the conversion of A to the
1121 final type requires a sign- or zero-extension,
1122 then we have to punt - it is not defined which
1124 || (POINTER_TYPE_P (TREE_TYPE (@0))
1125 && TREE_CODE (@1) == INTEGER_CST
1126 && tree_int_cst_sign_bit (@1) == 0))
1129 /* (T)P - (T)(P + A) -> -(T) A */
1130 (for add (plus pointer_plus)
1133 (convert (add @0 @1)))
1134 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
1135 /* For integer types, if A has a smaller type
1136 than T the result depends on the possible
1138 E.g. T=size_t, A=(unsigned)429497295, P>0.
1139 However, if an overflow in P + A would cause
1140 undefined behavior, we can assume that there
1142 || (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1143 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1144 /* For pointer types, if the conversion of A to the
1145 final type requires a sign- or zero-extension,
1146 then we have to punt - it is not defined which
1148 || (POINTER_TYPE_P (TREE_TYPE (@0))
1149 && TREE_CODE (@1) == INTEGER_CST
1150 && tree_int_cst_sign_bit (@1) == 0))
1151 (negate (convert @1)))))
1153 /* (T)(P + A) - (T)(P + B) -> (T)A - (T)B */
1154 (for add (plus pointer_plus)
1156 (minus (convert (add @0 @1))
1157 (convert (add @0 @2)))
1158 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
1159 /* For integer types, if A has a smaller type
1160 than T the result depends on the possible
1162 E.g. T=size_t, A=(unsigned)429497295, P>0.
1163 However, if an overflow in P + A would cause
1164 undefined behavior, we can assume that there
1166 || (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1167 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1168 /* For pointer types, if the conversion of A to the
1169 final type requires a sign- or zero-extension,
1170 then we have to punt - it is not defined which
1172 || (POINTER_TYPE_P (TREE_TYPE (@0))
1173 && TREE_CODE (@1) == INTEGER_CST
1174 && tree_int_cst_sign_bit (@1) == 0
1175 && TREE_CODE (@2) == INTEGER_CST
1176 && tree_int_cst_sign_bit (@2) == 0))
1177 (minus (convert @1) (convert @2)))))))
1180 /* Simplifications of MIN_EXPR, MAX_EXPR, fmin() and fmax(). */
1182 (for minmax (min max FMIN FMAX)
1186 /* min(max(x,y),y) -> y. */
1188 (min:c (max:c @0 @1) @1)
1190 /* max(min(x,y),y) -> y. */
1192 (max:c (min:c @0 @1) @1)
1196 (if (INTEGRAL_TYPE_P (type)
1197 && TYPE_MIN_VALUE (type)
1198 && operand_equal_p (@1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
1202 (if (INTEGRAL_TYPE_P (type)
1203 && TYPE_MAX_VALUE (type)
1204 && operand_equal_p (@1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
1206 (for minmax (FMIN FMAX)
1207 /* If either argument is NaN, return the other one. Avoid the
1208 transformation if we get (and honor) a signalling NaN. */
1210 (minmax:c @0 REAL_CST@1)
1211 (if (real_isnan (TREE_REAL_CST_PTR (@1))
1212 && (!HONOR_SNANS (@1) || !TREE_REAL_CST (@1).signalling))
1214 /* Convert fmin/fmax to MIN_EXPR/MAX_EXPR. C99 requires these
1215 functions to return the numeric arg if the other one is NaN.
1216 MIN and MAX don't honor that, so only transform if -ffinite-math-only
1217 is set. C99 doesn't require -0.0 to be handled, so we don't have to
1218 worry about it either. */
1219 (if (flag_finite_math_only)
1227 /* Simplifications of shift and rotates. */
1229 (for rotate (lrotate rrotate)
1231 (rotate integer_all_onesp@0 @1)
1234 /* Optimize -1 >> x for arithmetic right shifts. */
1236 (rshift integer_all_onesp@0 @1)
1237 (if (!TYPE_UNSIGNED (type)
1238 && tree_expr_nonnegative_p (@1))
1241 /* Optimize (x >> c) << c into x & (-1<<c). */
1243 (lshift (rshift @0 INTEGER_CST@1) @1)
1244 (if (wi::ltu_p (@1, element_precision (type)))
1245 (bit_and @0 (lshift { build_minus_one_cst (type); } @1))))
1247 /* Optimize (x << c) >> c into x & ((unsigned)-1 >> c) for unsigned
1250 (rshift (lshift @0 INTEGER_CST@1) @1)
1251 (if (TYPE_UNSIGNED (type)
1252 && (wi::ltu_p (@1, element_precision (type))))
1253 (bit_and @0 (rshift { build_minus_one_cst (type); } @1))))
1255 (for shiftrotate (lrotate rrotate lshift rshift)
1257 (shiftrotate @0 integer_zerop)
1260 (shiftrotate integer_zerop@0 @1)
1262 /* Prefer vector1 << scalar to vector1 << vector2
1263 if vector2 is uniform. */
1264 (for vec (VECTOR_CST CONSTRUCTOR)
1266 (shiftrotate @0 vec@1)
1267 (with { tree tem = uniform_vector_p (@1); }
1269 (shiftrotate @0 { tem; }))))))
1271 /* Rewrite an LROTATE_EXPR by a constant into an
1272 RROTATE_EXPR by a new constant. */
1274 (lrotate @0 INTEGER_CST@1)
1275 (rrotate @0 { fold_binary (MINUS_EXPR, TREE_TYPE (@1),
1276 build_int_cst (TREE_TYPE (@1),
1277 element_precision (type)), @1); }))
1279 /* Turn (a OP c1) OP c2 into a OP (c1+c2). */
1280 (for op (lrotate rrotate rshift lshift)
1282 (op (op @0 INTEGER_CST@1) INTEGER_CST@2)
1283 (with { unsigned int prec = element_precision (type); }
1284 (if (wi::ge_p (@1, 0, TYPE_SIGN (TREE_TYPE (@1)))
1285 && wi::lt_p (@1, prec, TYPE_SIGN (TREE_TYPE (@1)))
1286 && wi::ge_p (@2, 0, TYPE_SIGN (TREE_TYPE (@2)))
1287 && wi::lt_p (@2, prec, TYPE_SIGN (TREE_TYPE (@2))))
1288 (with { unsigned int low = wi::add (@1, @2).to_uhwi (); }
1289 /* Deal with a OP (c1 + c2) being undefined but (a OP c1) OP c2
1290 being well defined. */
1292 (if (op == LROTATE_EXPR || op == RROTATE_EXPR)
1293 (op @0 { build_int_cst (TREE_TYPE (@1), low % prec); })
1294 (if (TYPE_UNSIGNED (type) || op == LSHIFT_EXPR)
1295 { build_zero_cst (type); }
1296 (op @0 { build_int_cst (TREE_TYPE (@1), prec - 1); })))
1297 (op @0 { build_int_cst (TREE_TYPE (@1), low); })))))))
1300 /* ((1 << A) & 1) != 0 -> A == 0
1301 ((1 << A) & 1) == 0 -> A != 0 */
1305 (cmp (bit_and (lshift integer_onep @0) integer_onep) integer_zerop)
1306 (icmp @0 { build_zero_cst (TREE_TYPE (@0)); })))
1308 /* (CST1 << A) == CST2 -> A == ctz (CST2) - ctz (CST1)
1309 (CST1 << A) != CST2 -> A != ctz (CST2) - ctz (CST1)
1313 (cmp (lshift INTEGER_CST@0 @1) INTEGER_CST@2)
1314 (with { int cand = wi::ctz (@2) - wi::ctz (@0); }
1316 || (!integer_zerop (@2)
1317 && wi::ne_p (wi::lshift (@0, cand), @2)))
1318 { constant_boolean_node (cmp == NE_EXPR, type); }
1319 (if (!integer_zerop (@2)
1320 && wi::eq_p (wi::lshift (@0, cand), @2))
1321 (cmp @1 { build_int_cst (TREE_TYPE (@1), cand); }))))))
1323 /* Fold (X << C1) & C2 into (X << C1) & (C2 | ((1 << C1) - 1))
1324 (X >> C1) & C2 into (X >> C1) & (C2 | ~((type) -1 >> C1))
1325 if the new mask might be further optimized. */
1326 (for shift (lshift rshift)
1328 (bit_and (convert?:s@4 (shift:s@5 (convert1?@3 @0) INTEGER_CST@1))
1330 (if (tree_nop_conversion_p (TREE_TYPE (@4), TREE_TYPE (@5))
1331 && TYPE_PRECISION (type) <= HOST_BITS_PER_WIDE_INT
1332 && tree_fits_uhwi_p (@1)
1333 && tree_to_uhwi (@1) > 0
1334 && tree_to_uhwi (@1) < TYPE_PRECISION (type))
1337 unsigned int shiftc = tree_to_uhwi (@1);
1338 unsigned HOST_WIDE_INT mask = TREE_INT_CST_LOW (@2);
1339 unsigned HOST_WIDE_INT newmask, zerobits = 0;
1340 tree shift_type = TREE_TYPE (@3);
1343 if (shift == LSHIFT_EXPR)
1344 zerobits = ((((unsigned HOST_WIDE_INT) 1) << shiftc) - 1);
1345 else if (shift == RSHIFT_EXPR
1346 && (TYPE_PRECISION (shift_type)
1347 == GET_MODE_PRECISION (TYPE_MODE (shift_type))))
1349 prec = TYPE_PRECISION (TREE_TYPE (@3));
1351 /* See if more bits can be proven as zero because of
1354 && TYPE_UNSIGNED (TREE_TYPE (@0)))
1356 tree inner_type = TREE_TYPE (@0);
1357 if ((TYPE_PRECISION (inner_type)
1358 == GET_MODE_PRECISION (TYPE_MODE (inner_type)))
1359 && TYPE_PRECISION (inner_type) < prec)
1361 prec = TYPE_PRECISION (inner_type);
1362 /* See if we can shorten the right shift. */
1364 shift_type = inner_type;
1365 /* Otherwise X >> C1 is all zeros, so we'll optimize
1366 it into (X, 0) later on by making sure zerobits
1370 zerobits = ~(unsigned HOST_WIDE_INT) 0;
1373 zerobits >>= HOST_BITS_PER_WIDE_INT - shiftc;
1374 zerobits <<= prec - shiftc;
1376 /* For arithmetic shift if sign bit could be set, zerobits
1377 can contain actually sign bits, so no transformation is
1378 possible, unless MASK masks them all away. In that
1379 case the shift needs to be converted into logical shift. */
1380 if (!TYPE_UNSIGNED (TREE_TYPE (@3))
1381 && prec == TYPE_PRECISION (TREE_TYPE (@3)))
1383 if ((mask & zerobits) == 0)
1384 shift_type = unsigned_type_for (TREE_TYPE (@3));
1390 /* ((X << 16) & 0xff00) is (X, 0). */
1391 (if ((mask & zerobits) == mask)
1392 { build_int_cst (type, 0); }
1393 (with { newmask = mask | zerobits; }
1394 (if (newmask != mask && (newmask & (newmask + 1)) == 0)
1397 /* Only do the transformation if NEWMASK is some integer
1399 for (prec = BITS_PER_UNIT;
1400 prec < HOST_BITS_PER_WIDE_INT; prec <<= 1)
1401 if (newmask == (((unsigned HOST_WIDE_INT) 1) << prec) - 1)
1404 (if (prec < HOST_BITS_PER_WIDE_INT
1405 || newmask == ~(unsigned HOST_WIDE_INT) 0)
1407 { tree newmaskt = build_int_cst_type (TREE_TYPE (@2), newmask); }
1408 (if (!tree_int_cst_equal (newmaskt, @2))
1409 (if (shift_type != TREE_TYPE (@3))
1410 (bit_and (convert (shift:shift_type (convert @3) @1)) { newmaskt; })
1411 (bit_and @4 { newmaskt; })))))))))))))
1413 /* Fold (X {&,^,|} C2) << C1 into (X << C1) {&,^,|} (C2 << C1)
1414 (X {&,^,|} C2) >> C1 into (X >> C1) & (C2 >> C1). */
1415 (for shift (lshift rshift)
1416 (for bit_op (bit_and bit_xor bit_ior)
1418 (shift (convert?:s (bit_op:s @0 INTEGER_CST@2)) INTEGER_CST@1)
1419 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1420 (with { tree mask = int_const_binop (shift, fold_convert (type, @2), @1); }
1421 (bit_op (shift (convert @0) @1) { mask; }))))))
1424 /* Simplifications of conversions. */
1426 /* Basic strip-useless-type-conversions / strip_nops. */
1427 (for cvt (convert view_convert float fix_trunc)
1430 (if ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@0)))
1431 || (GENERIC && type == TREE_TYPE (@0)))
1434 /* Contract view-conversions. */
1436 (view_convert (view_convert @0))
1439 /* For integral conversions with the same precision or pointer
1440 conversions use a NOP_EXPR instead. */
1443 (if ((INTEGRAL_TYPE_P (type) || POINTER_TYPE_P (type))
1444 && (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
1445 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (@0)))
1448 /* Strip inner integral conversions that do not change precision or size. */
1450 (view_convert (convert@0 @1))
1451 (if ((INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
1452 && (INTEGRAL_TYPE_P (TREE_TYPE (@1)) || POINTER_TYPE_P (TREE_TYPE (@1)))
1453 && (TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
1454 && (TYPE_SIZE (TREE_TYPE (@0)) == TYPE_SIZE (TREE_TYPE (@1))))
1457 /* Re-association barriers around constants and other re-association
1458 barriers can be removed. */
1460 (paren CONSTANT_CLASS_P@0)
1463 (paren (paren@1 @0))
1466 /* Handle cases of two conversions in a row. */
1467 (for ocvt (convert float fix_trunc)
1468 (for icvt (convert float)
1473 tree inside_type = TREE_TYPE (@0);
1474 tree inter_type = TREE_TYPE (@1);
1475 int inside_int = INTEGRAL_TYPE_P (inside_type);
1476 int inside_ptr = POINTER_TYPE_P (inside_type);
1477 int inside_float = FLOAT_TYPE_P (inside_type);
1478 int inside_vec = VECTOR_TYPE_P (inside_type);
1479 unsigned int inside_prec = TYPE_PRECISION (inside_type);
1480 int inside_unsignedp = TYPE_UNSIGNED (inside_type);
1481 int inter_int = INTEGRAL_TYPE_P (inter_type);
1482 int inter_ptr = POINTER_TYPE_P (inter_type);
1483 int inter_float = FLOAT_TYPE_P (inter_type);
1484 int inter_vec = VECTOR_TYPE_P (inter_type);
1485 unsigned int inter_prec = TYPE_PRECISION (inter_type);
1486 int inter_unsignedp = TYPE_UNSIGNED (inter_type);
1487 int final_int = INTEGRAL_TYPE_P (type);
1488 int final_ptr = POINTER_TYPE_P (type);
1489 int final_float = FLOAT_TYPE_P (type);
1490 int final_vec = VECTOR_TYPE_P (type);
1491 unsigned int final_prec = TYPE_PRECISION (type);
1492 int final_unsignedp = TYPE_UNSIGNED (type);
1495 /* In addition to the cases of two conversions in a row
1496 handled below, if we are converting something to its own
1497 type via an object of identical or wider precision, neither
1498 conversion is needed. */
1499 (if (((GIMPLE && useless_type_conversion_p (type, inside_type))
1501 && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (inside_type)))
1502 && (((inter_int || inter_ptr) && final_int)
1503 || (inter_float && final_float))
1504 && inter_prec >= final_prec)
1507 /* Likewise, if the intermediate and initial types are either both
1508 float or both integer, we don't need the middle conversion if the
1509 former is wider than the latter and doesn't change the signedness
1510 (for integers). Avoid this if the final type is a pointer since
1511 then we sometimes need the middle conversion. Likewise if the
1512 final type has a precision not equal to the size of its mode. */
1513 (if (((inter_int && inside_int) || (inter_float && inside_float))
1514 && (final_int || final_float)
1515 && inter_prec >= inside_prec
1516 && (inter_float || inter_unsignedp == inside_unsignedp)
1517 && ! (final_prec != GET_MODE_PRECISION (TYPE_MODE (type))
1518 && TYPE_MODE (type) == TYPE_MODE (inter_type)))
1521 /* If we have a sign-extension of a zero-extended value, we can
1522 replace that by a single zero-extension. Likewise if the
1523 final conversion does not change precision we can drop the
1524 intermediate conversion. */
1525 (if (inside_int && inter_int && final_int
1526 && ((inside_prec < inter_prec && inter_prec < final_prec
1527 && inside_unsignedp && !inter_unsignedp)
1528 || final_prec == inter_prec))
1531 /* Two conversions in a row are not needed unless:
1532 - some conversion is floating-point (overstrict for now), or
1533 - some conversion is a vector (overstrict for now), or
1534 - the intermediate type is narrower than both initial and
1536 - the intermediate type and innermost type differ in signedness,
1537 and the outermost type is wider than the intermediate, or
1538 - the initial type is a pointer type and the precisions of the
1539 intermediate and final types differ, or
1540 - the final type is a pointer type and the precisions of the
1541 initial and intermediate types differ. */
1542 (if (! inside_float && ! inter_float && ! final_float
1543 && ! inside_vec && ! inter_vec && ! final_vec
1544 && (inter_prec >= inside_prec || inter_prec >= final_prec)
1545 && ! (inside_int && inter_int
1546 && inter_unsignedp != inside_unsignedp
1547 && inter_prec < final_prec)
1548 && ((inter_unsignedp && inter_prec > inside_prec)
1549 == (final_unsignedp && final_prec > inter_prec))
1550 && ! (inside_ptr && inter_prec != final_prec)
1551 && ! (final_ptr && inside_prec != inter_prec)
1552 && ! (final_prec != GET_MODE_PRECISION (TYPE_MODE (type))
1553 && TYPE_MODE (type) == TYPE_MODE (inter_type)))
1556 /* A truncation to an unsigned type (a zero-extension) should be
1557 canonicalized as bitwise and of a mask. */
1558 (if (final_int && inter_int && inside_int
1559 && final_prec == inside_prec
1560 && final_prec > inter_prec
1562 (convert (bit_and @0 { wide_int_to_tree
1564 wi::mask (inter_prec, false,
1565 TYPE_PRECISION (inside_type))); })))
1567 /* If we are converting an integer to a floating-point that can
1568 represent it exactly and back to an integer, we can skip the
1569 floating-point conversion. */
1570 (if (GIMPLE /* PR66211 */
1571 && inside_int && inter_float && final_int &&
1572 (unsigned) significand_size (TYPE_MODE (inter_type))
1573 >= inside_prec - !inside_unsignedp)
1576 /* If we have a narrowing conversion to an integral type that is fed by a
1577 BIT_AND_EXPR, we might be able to remove the BIT_AND_EXPR if it merely
1578 masks off bits outside the final type (and nothing else). */
1580 (convert (bit_and @0 INTEGER_CST@1))
1581 (if (INTEGRAL_TYPE_P (type)
1582 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
1583 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))
1584 && operand_equal_p (@1, build_low_bits_mask (TREE_TYPE (@1),
1585 TYPE_PRECISION (type)), 0))
1589 /* (X /[ex] A) * A -> X. */
1591 (mult (convert? (exact_div @0 @1)) @1)
1592 /* Look through a sign-changing conversion. */
1595 /* Canonicalization of binary operations. */
1597 /* Convert X + -C into X - C. */
1599 (plus @0 REAL_CST@1)
1600 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
1601 (with { tree tem = fold_unary (NEGATE_EXPR, type, @1); }
1602 (if (!TREE_OVERFLOW (tem) || !flag_trapping_math)
1603 (minus @0 { tem; })))))
1605 /* Convert x+x into x*2.0. */
1608 (if (SCALAR_FLOAT_TYPE_P (type))
1609 (mult @0 { build_real (type, dconst2); })))
1612 (minus integer_zerop @1)
1615 /* (ARG0 - ARG1) is the same as (-ARG1 + ARG0). So check whether
1616 ARG0 is zero and X + ARG0 reduces to X, since that would mean
1617 (-ARG1 + ARG0) reduces to -ARG1. */
1619 (minus real_zerop@0 @1)
1620 (if (fold_real_zero_addition_p (type, @0, 0))
1623 /* Transform x * -1 into -x. */
1625 (mult @0 integer_minus_onep)
1628 /* True if we can easily extract the real and imaginary parts of a complex
1630 (match compositional_complex
1631 (convert? (complex @0 @1)))
1633 /* COMPLEX_EXPR and REALPART/IMAGPART_EXPR cancellations. */
1635 (complex (realpart @0) (imagpart @0))
1638 (realpart (complex @0 @1))
1641 (imagpart (complex @0 @1))
1644 /* Sometimes we only care about half of a complex expression. */
1646 (realpart (convert?:s (conj:s @0)))
1647 (convert (realpart @0)))
1649 (imagpart (convert?:s (conj:s @0)))
1650 (convert (negate (imagpart @0))))
1651 (for part (realpart imagpart)
1652 (for op (plus minus)
1654 (part (convert?:s@2 (op:s @0 @1)))
1655 (convert (op (part @0) (part @1))))))
1657 (realpart (convert?:s (CEXPI:s @0)))
1660 (imagpart (convert?:s (CEXPI:s @0)))
1663 /* conj(conj(x)) -> x */
1665 (conj (convert? (conj @0)))
1666 (if (tree_nop_conversion_p (TREE_TYPE (@0), type))
1669 /* conj({x,y}) -> {x,-y} */
1671 (conj (convert?:s (complex:s @0 @1)))
1672 (with { tree itype = TREE_TYPE (type); }
1673 (complex (convert:itype @0) (negate (convert:itype @1)))))
1675 /* BSWAP simplifications, transforms checked by gcc.dg/builtin-bswap-8.c. */
1676 (for bswap (BUILT_IN_BSWAP16 BUILT_IN_BSWAP32 BUILT_IN_BSWAP64)
1681 (bswap (bit_not (bswap @0)))
1683 (for bitop (bit_xor bit_ior bit_and)
1685 (bswap (bitop:c (bswap @0) @1))
1686 (bitop @0 (bswap @1)))))
1689 /* Combine COND_EXPRs and VEC_COND_EXPRs. */
1691 /* Simplify constant conditions.
1692 Only optimize constant conditions when the selected branch
1693 has the same type as the COND_EXPR. This avoids optimizing
1694 away "c ? x : throw", where the throw has a void type.
1695 Note that we cannot throw away the fold-const.c variant nor
1696 this one as we depend on doing this transform before possibly
1697 A ? B : B -> B triggers and the fold-const.c one can optimize
1698 0 ? A : B to B even if A has side-effects. Something
1699 genmatch cannot handle. */
1701 (cond INTEGER_CST@0 @1 @2)
1702 (if (integer_zerop (@0))
1703 (if (!VOID_TYPE_P (TREE_TYPE (@2)) || VOID_TYPE_P (type))
1705 (if (!VOID_TYPE_P (TREE_TYPE (@1)) || VOID_TYPE_P (type))
1708 (vec_cond VECTOR_CST@0 @1 @2)
1709 (if (integer_all_onesp (@0))
1711 (if (integer_zerop (@0))
1714 (for cnd (cond vec_cond)
1715 /* A ? B : (A ? X : C) -> A ? B : C. */
1717 (cnd @0 (cnd @0 @1 @2) @3)
1720 (cnd @0 @1 (cnd @0 @2 @3))
1723 /* A ? B : B -> B. */
1728 /* !A ? B : C -> A ? C : B. */
1730 (cnd (logical_inverted_value truth_valued_p@0) @1 @2)
1733 /* A + (B vcmp C ? 1 : 0) -> A - (B vcmp C), since vector comparisons
1734 return all-1 or all-0 results. */
1735 /* ??? We could instead convert all instances of the vec_cond to negate,
1736 but that isn't necessarily a win on its own. */
1738 (plus:c @3 (view_convert? (vec_cond @0 integer_each_onep@1 integer_zerop@2)))
1739 (if (VECTOR_TYPE_P (type)
1740 && TYPE_VECTOR_SUBPARTS (type) == TYPE_VECTOR_SUBPARTS (TREE_TYPE (@0))
1741 && (TYPE_MODE (TREE_TYPE (type))
1742 == TYPE_MODE (TREE_TYPE (TREE_TYPE (@0)))))
1743 (minus @3 (view_convert @0))))
1745 /* ... likewise A - (B vcmp C ? 1 : 0) -> A + (B vcmp C). */
1747 (minus @3 (view_convert? (vec_cond @0 integer_each_onep@1 integer_zerop@2)))
1748 (if (VECTOR_TYPE_P (type)
1749 && TYPE_VECTOR_SUBPARTS (type) == TYPE_VECTOR_SUBPARTS (TREE_TYPE (@0))
1750 && (TYPE_MODE (TREE_TYPE (type))
1751 == TYPE_MODE (TREE_TYPE (TREE_TYPE (@0)))))
1752 (plus @3 (view_convert @0))))
1755 /* Simplifications of comparisons. */
1757 /* See if we can reduce the magnitude of a constant involved in a
1758 comparison by changing the comparison code. This is a canonicalization
1759 formerly done by maybe_canonicalize_comparison_1. */
1763 (cmp @0 INTEGER_CST@1)
1764 (if (tree_int_cst_sgn (@1) == -1)
1765 (acmp @0 { wide_int_to_tree (TREE_TYPE (@1), wi::add (@1, 1)); }))))
1769 (cmp @0 INTEGER_CST@1)
1770 (if (tree_int_cst_sgn (@1) == 1)
1771 (acmp @0 { wide_int_to_tree (TREE_TYPE (@1), wi::sub (@1, 1)); }))))
1774 /* We can simplify a logical negation of a comparison to the
1775 inverted comparison. As we cannot compute an expression
1776 operator using invert_tree_comparison we have to simulate
1777 that with expression code iteration. */
1778 (for cmp (tcc_comparison)
1779 icmp (inverted_tcc_comparison)
1780 ncmp (inverted_tcc_comparison_with_nans)
1781 /* Ideally we'd like to combine the following two patterns
1782 and handle some more cases by using
1783 (logical_inverted_value (cmp @0 @1))
1784 here but for that genmatch would need to "inline" that.
1785 For now implement what forward_propagate_comparison did. */
1787 (bit_not (cmp @0 @1))
1788 (if (VECTOR_TYPE_P (type)
1789 || (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1))
1790 /* Comparison inversion may be impossible for trapping math,
1791 invert_tree_comparison will tell us. But we can't use
1792 a computed operator in the replacement tree thus we have
1793 to play the trick below. */
1794 (with { enum tree_code ic = invert_tree_comparison
1795 (cmp, HONOR_NANS (@0)); }
1801 (bit_xor (cmp @0 @1) integer_truep)
1802 (with { enum tree_code ic = invert_tree_comparison
1803 (cmp, HONOR_NANS (@0)); }
1809 /* Transform comparisons of the form X - Y CMP 0 to X CMP Y.
1810 ??? The transformation is valid for the other operators if overflow
1811 is undefined for the type, but performing it here badly interacts
1812 with the transformation in fold_cond_expr_with_comparison which
1813 attempts to synthetize ABS_EXPR. */
1816 (cmp (minus@2 @0 @1) integer_zerop)
1817 (if (single_use (@2))
1820 /* Transform comparisons of the form X * C1 CMP 0 to X CMP 0 in the
1821 signed arithmetic case. That form is created by the compiler
1822 often enough for folding it to be of value. One example is in
1823 computing loop trip counts after Operator Strength Reduction. */
1824 (for cmp (simple_comparison)
1825 scmp (swapped_simple_comparison)
1827 (cmp (mult @0 INTEGER_CST@1) integer_zerop@2)
1828 /* Handle unfolded multiplication by zero. */
1829 (if (integer_zerop (@1))
1831 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1832 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1833 /* If @1 is negative we swap the sense of the comparison. */
1834 (if (tree_int_cst_sgn (@1) < 0)
1838 /* Simplify comparison of something with itself. For IEEE
1839 floating-point, we can only do some of these simplifications. */
1842 (if (! FLOAT_TYPE_P (TREE_TYPE (@0))
1843 || ! HONOR_NANS (TYPE_MODE (TREE_TYPE (@0))))
1844 { constant_boolean_node (true, type); }))
1853 || ! FLOAT_TYPE_P (TREE_TYPE (@0))
1854 || ! HONOR_NANS (TYPE_MODE (TREE_TYPE (@0))))
1855 { constant_boolean_node (false, type); })))
1856 (for cmp (unle unge uneq)
1859 { constant_boolean_node (true, type); }))
1862 (if (!flag_trapping_math)
1863 { constant_boolean_node (false, type); }))
1865 /* Fold ~X op ~Y as Y op X. */
1866 (for cmp (simple_comparison)
1868 (cmp (bit_not @0) (bit_not @1))
1871 /* Fold ~X op C as X op' ~C, where op' is the swapped comparison. */
1872 (for cmp (simple_comparison)
1873 scmp (swapped_simple_comparison)
1875 (cmp (bit_not @0) CONSTANT_CLASS_P@1)
1876 (if (TREE_CODE (@1) == INTEGER_CST || TREE_CODE (@1) == VECTOR_CST)
1877 (scmp @0 (bit_not @1)))))
1879 (for cmp (simple_comparison)
1880 /* Fold (double)float1 CMP (double)float2 into float1 CMP float2. */
1882 (cmp (convert@2 @0) (convert? @1))
1883 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
1884 && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@2))
1885 == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0)))
1886 && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@2))
1887 == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1))))
1890 tree type1 = TREE_TYPE (@1);
1891 if (TREE_CODE (@1) == REAL_CST && !DECIMAL_FLOAT_TYPE_P (type1))
1893 REAL_VALUE_TYPE orig = TREE_REAL_CST (@1);
1894 if (TYPE_PRECISION (type1) > TYPE_PRECISION (float_type_node)
1895 && exact_real_truncate (TYPE_MODE (float_type_node), &orig))
1896 type1 = float_type_node;
1897 if (TYPE_PRECISION (type1) > TYPE_PRECISION (double_type_node)
1898 && exact_real_truncate (TYPE_MODE (double_type_node), &orig))
1899 type1 = double_type_node;
1902 = (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type1)
1903 ? TREE_TYPE (@0) : type1);
1905 (if (TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (newtype))
1906 (cmp (convert:newtype @0) (convert:newtype @1))))))
1910 /* IEEE doesn't distinguish +0 and -0 in comparisons. */
1912 /* a CMP (-0) -> a CMP 0 */
1913 (if (REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@1)))
1914 (cmp @0 { build_real (TREE_TYPE (@1), dconst0); }))
1915 /* x != NaN is always true, other ops are always false. */
1916 (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
1917 && ! HONOR_SNANS (@1))
1918 { constant_boolean_node (cmp == NE_EXPR, type); })
1919 /* Fold comparisons against infinity. */
1920 (if (REAL_VALUE_ISINF (TREE_REAL_CST (@1))
1921 && MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (@1))))
1924 REAL_VALUE_TYPE max;
1925 enum tree_code code = cmp;
1926 bool neg = REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1));
1928 code = swap_tree_comparison (code);
1931 /* x > +Inf is always false, if with ignore sNANs. */
1932 (if (code == GT_EXPR
1933 && ! HONOR_SNANS (@0))
1934 { constant_boolean_node (false, type); })
1935 (if (code == LE_EXPR)
1936 /* x <= +Inf is always true, if we don't case about NaNs. */
1937 (if (! HONOR_NANS (@0))
1938 { constant_boolean_node (true, type); }
1939 /* x <= +Inf is the same as x == x, i.e. !isnan(x). */
1941 /* x == +Inf and x >= +Inf are always equal to x > DBL_MAX. */
1942 (if (code == EQ_EXPR || code == GE_EXPR)
1943 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
1945 (lt @0 { build_real (TREE_TYPE (@0), max); })
1946 (gt @0 { build_real (TREE_TYPE (@0), max); }))))
1947 /* x < +Inf is always equal to x <= DBL_MAX. */
1948 (if (code == LT_EXPR)
1949 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
1951 (ge @0 { build_real (TREE_TYPE (@0), max); })
1952 (le @0 { build_real (TREE_TYPE (@0), max); }))))
1953 /* x != +Inf is always equal to !(x > DBL_MAX). */
1954 (if (code == NE_EXPR)
1955 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
1956 (if (! HONOR_NANS (@0))
1958 (ge @0 { build_real (TREE_TYPE (@0), max); })
1959 (le @0 { build_real (TREE_TYPE (@0), max); }))
1961 (bit_xor (lt @0 { build_real (TREE_TYPE (@0), max); })
1962 { build_one_cst (type); })
1963 (bit_xor (gt @0 { build_real (TREE_TYPE (@0), max); })
1964 { build_one_cst (type); }))))))))))
1966 /* If this is a comparison of a real constant with a PLUS_EXPR
1967 or a MINUS_EXPR of a real constant, we can convert it into a
1968 comparison with a revised real constant as long as no overflow
1969 occurs when unsafe_math_optimizations are enabled. */
1970 (if (flag_unsafe_math_optimizations)
1971 (for op (plus minus)
1973 (cmp (op @0 REAL_CST@1) REAL_CST@2)
1976 tree tem = const_binop (op == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR,
1977 TREE_TYPE (@1), @2, @1);
1979 (if (tem && !TREE_OVERFLOW (tem))
1980 (cmp @0 { tem; }))))))
1982 /* Likewise, we can simplify a comparison of a real constant with
1983 a MINUS_EXPR whose first operand is also a real constant, i.e.
1984 (c1 - x) < c2 becomes x > c1-c2. Reordering is allowed on
1985 floating-point types only if -fassociative-math is set. */
1986 (if (flag_associative_math)
1988 (cmp (minus REAL_CST@0 @1) REAL_CST@2)
1989 (with { tree tem = const_binop (MINUS_EXPR, TREE_TYPE (@1), @0, @2); }
1990 (if (tem && !TREE_OVERFLOW (tem))
1991 (cmp { tem; } @1)))))
1993 /* Fold comparisons against built-in math functions. */
1994 (if (flag_unsafe_math_optimizations
1995 && ! flag_errno_math)
1998 (cmp (sq @0) REAL_CST@1)
2000 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
2002 /* sqrt(x) < y is always false, if y is negative. */
2003 (if (cmp == EQ_EXPR || cmp == LT_EXPR || cmp == LE_EXPR)
2004 { constant_boolean_node (false, type); })
2005 /* sqrt(x) > y is always true, if y is negative and we
2006 don't care about NaNs, i.e. negative values of x. */
2007 (if (cmp == NE_EXPR || !HONOR_NANS (@0))
2008 { constant_boolean_node (true, type); })
2009 /* sqrt(x) > y is the same as x >= 0, if y is negative. */
2010 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })))
2011 (if (real_equal (TREE_REAL_CST_PTR (@1), &dconst0))
2013 /* sqrt(x) < 0 is always false. */
2014 (if (cmp == LT_EXPR)
2015 { constant_boolean_node (false, type); })
2016 /* sqrt(x) >= 0 is always true if we don't care about NaNs. */
2017 (if (cmp == GE_EXPR && !HONOR_NANS (@0))
2018 { constant_boolean_node (true, type); })
2019 /* sqrt(x) <= 0 -> x == 0. */
2020 (if (cmp == LE_EXPR)
2022 /* Otherwise sqrt(x) cmp 0 -> x cmp 0. Here cmp can be >=, >,
2023 == or !=. In the last case:
2025 (sqrt(x) != 0) == (NaN != 0) == true == (x != 0)
2027 if x is negative or NaN. Due to -funsafe-math-optimizations,
2028 the results for other x follow from natural arithmetic. */
2030 (if (cmp == GT_EXPR || cmp == GE_EXPR)
2034 real_arithmetic (&c2, MULT_EXPR,
2035 &TREE_REAL_CST (@1), &TREE_REAL_CST (@1));
2036 real_convert (&c2, TYPE_MODE (TREE_TYPE (@0)), &c2);
2038 (if (REAL_VALUE_ISINF (c2))
2039 /* sqrt(x) > y is x == +Inf, when y is very large. */
2040 (if (HONOR_INFINITIES (@0))
2041 (eq @0 { build_real (TREE_TYPE (@0), c2); })
2042 { constant_boolean_node (false, type); })
2043 /* sqrt(x) > c is the same as x > c*c. */
2044 (cmp @0 { build_real (TREE_TYPE (@0), c2); }))))
2045 (if (cmp == LT_EXPR || cmp == LE_EXPR)
2049 real_arithmetic (&c2, MULT_EXPR,
2050 &TREE_REAL_CST (@1), &TREE_REAL_CST (@1));
2051 real_convert (&c2, TYPE_MODE (TREE_TYPE (@0)), &c2);
2053 (if (REAL_VALUE_ISINF (c2))
2055 /* sqrt(x) < y is always true, when y is a very large
2056 value and we don't care about NaNs or Infinities. */
2057 (if (! HONOR_NANS (@0) && ! HONOR_INFINITIES (@0))
2058 { constant_boolean_node (true, type); })
2059 /* sqrt(x) < y is x != +Inf when y is very large and we
2060 don't care about NaNs. */
2061 (if (! HONOR_NANS (@0))
2062 (ne @0 { build_real (TREE_TYPE (@0), c2); }))
2063 /* sqrt(x) < y is x >= 0 when y is very large and we
2064 don't care about Infinities. */
2065 (if (! HONOR_INFINITIES (@0))
2066 (ge @0 { build_real (TREE_TYPE (@0), dconst0); }))
2067 /* sqrt(x) < y is x >= 0 && x != +Inf, when y is large. */
2070 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
2071 (ne @0 { build_real (TREE_TYPE (@0), c2); }))))
2072 /* sqrt(x) < c is the same as x < c*c, if we ignore NaNs. */
2073 (if (! HONOR_NANS (@0))
2074 (cmp @0 { build_real (TREE_TYPE (@0), c2); })
2075 /* sqrt(x) < c is the same as x >= 0 && x < c*c. */
2078 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
2079 (cmp @0 { build_real (TREE_TYPE (@0), c2); }))))))))))))
2081 /* Unordered tests if either argument is a NaN. */
2083 (bit_ior (unordered @0 @0) (unordered @1 @1))
2084 (if (types_match (@0, @1))
2087 (bit_and (ordered @0 @0) (ordered @1 @1))
2088 (if (types_match (@0, @1))
2091 (bit_ior:c (unordered @0 @0) (unordered:c@2 @0 @1))
2094 (bit_and:c (ordered @0 @0) (ordered:c@2 @0 @1))
2097 /* -A CMP -B -> B CMP A. */
2098 (for cmp (tcc_comparison)
2099 scmp (swapped_tcc_comparison)
2101 (cmp (negate @0) (negate @1))
2102 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
2103 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2104 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
2107 (cmp (negate @0) CONSTANT_CLASS_P@1)
2108 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
2109 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2110 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
2111 (with { tree tem = fold_unary (NEGATE_EXPR, TREE_TYPE (@0), @1); }
2112 (if (tem && !TREE_OVERFLOW (tem))
2113 (scmp @0 { tem; }))))))
2115 /* Convert ABS_EXPR<x> == 0 or ABS_EXPR<x> != 0 to x == 0 or x != 0. */
2118 (op (abs @0) zerop@1)
2121 /* From fold_sign_changed_comparison and fold_widened_comparison. */
2122 (for cmp (simple_comparison)
2124 (cmp (convert@0 @00) (convert?@1 @10))
2125 (if (TREE_CODE (TREE_TYPE (@0)) == INTEGER_TYPE
2126 /* Disable this optimization if we're casting a function pointer
2127 type on targets that require function pointer canonicalization. */
2128 && !(targetm.have_canonicalize_funcptr_for_compare ()
2129 && TREE_CODE (TREE_TYPE (@00)) == POINTER_TYPE
2130 && TREE_CODE (TREE_TYPE (TREE_TYPE (@00))) == FUNCTION_TYPE)
2132 (if (TYPE_PRECISION (TREE_TYPE (@00)) == TYPE_PRECISION (TREE_TYPE (@0))
2133 && (TREE_CODE (@10) == INTEGER_CST
2134 || (@1 != @10 && types_match (TREE_TYPE (@10), TREE_TYPE (@00))))
2135 && (TYPE_UNSIGNED (TREE_TYPE (@00)) == TYPE_UNSIGNED (TREE_TYPE (@0))
2138 && (POINTER_TYPE_P (TREE_TYPE (@00)) == POINTER_TYPE_P (TREE_TYPE (@0))))
2139 /* ??? The special-casing of INTEGER_CST conversion was in the original
2140 code and here to avoid a spurious overflow flag on the resulting
2141 constant which fold_convert produces. */
2142 (if (TREE_CODE (@1) == INTEGER_CST)
2143 (cmp @00 { force_fit_type (TREE_TYPE (@00), wi::to_widest (@1), 0,
2144 TREE_OVERFLOW (@1)); })
2145 (cmp @00 (convert @1)))
2147 (if (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (TREE_TYPE (@00)))
2148 /* If possible, express the comparison in the shorter mode. */
2149 (if ((cmp == EQ_EXPR || cmp == NE_EXPR
2150 || TYPE_UNSIGNED (TREE_TYPE (@0)) == TYPE_UNSIGNED (TREE_TYPE (@00)))
2151 && (types_match (TREE_TYPE (@10), TREE_TYPE (@00))
2152 || ((TYPE_PRECISION (TREE_TYPE (@00))
2153 >= TYPE_PRECISION (TREE_TYPE (@10)))
2154 && (TYPE_UNSIGNED (TREE_TYPE (@00))
2155 == TYPE_UNSIGNED (TREE_TYPE (@10))))
2156 || (TREE_CODE (@10) == INTEGER_CST
2157 && INTEGRAL_TYPE_P (TREE_TYPE (@00))
2158 && int_fits_type_p (@10, TREE_TYPE (@00)))))
2159 (cmp @00 (convert @10))
2160 (if (TREE_CODE (@10) == INTEGER_CST
2161 && INTEGRAL_TYPE_P (TREE_TYPE (@00))
2162 && !int_fits_type_p (@10, TREE_TYPE (@00)))
2165 tree min = lower_bound_in_type (TREE_TYPE (@10), TREE_TYPE (@00));
2166 tree max = upper_bound_in_type (TREE_TYPE (@10), TREE_TYPE (@00));
2167 bool above = integer_nonzerop (const_binop (LT_EXPR, type, max, @10));
2168 bool below = integer_nonzerop (const_binop (LT_EXPR, type, @10, min));
2170 (if (above || below)
2171 (if (cmp == EQ_EXPR || cmp == NE_EXPR)
2172 { constant_boolean_node (cmp == EQ_EXPR ? false : true, type); }
2173 (if (cmp == LT_EXPR || cmp == LE_EXPR)
2174 { constant_boolean_node (above ? true : false, type); }
2175 (if (cmp == GT_EXPR || cmp == GE_EXPR)
2176 { constant_boolean_node (above ? false : true, type); }))))))))))))
2179 /* A local variable can never be pointed to by
2180 the default SSA name of an incoming parameter.
2181 SSA names are canonicalized to 2nd place. */
2183 (cmp addr@0 SSA_NAME@1)
2184 (if (SSA_NAME_IS_DEFAULT_DEF (@1)
2185 && TREE_CODE (SSA_NAME_VAR (@1)) == PARM_DECL)
2186 (with { tree base = get_base_address (TREE_OPERAND (@0, 0)); }
2187 (if (TREE_CODE (base) == VAR_DECL
2188 && auto_var_in_fn_p (base, current_function_decl))
2189 (if (cmp == NE_EXPR)
2190 { constant_boolean_node (true, type); }
2191 { constant_boolean_node (false, type); }))))))
2193 /* Equality compare simplifications from fold_binary */
2196 /* If we have (A | C) == D where C & ~D != 0, convert this into 0.
2197 Similarly for NE_EXPR. */
2199 (cmp (convert?@3 (bit_ior @0 INTEGER_CST@1)) INTEGER_CST@2)
2200 (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0))
2201 && wi::bit_and_not (@1, @2) != 0)
2202 { constant_boolean_node (cmp == NE_EXPR, type); }))
2204 /* (X ^ Y) == 0 becomes X == Y, and (X ^ Y) != 0 becomes X != Y. */
2206 (cmp (bit_xor @0 @1) integer_zerop)
2209 /* (X ^ Y) == Y becomes X == 0.
2210 Likewise (X ^ Y) == X becomes Y == 0. */
2212 (cmp:c (bit_xor:c @0 @1) @0)
2213 (cmp @1 { build_zero_cst (TREE_TYPE (@1)); }))
2215 /* (X ^ C1) op C2 can be rewritten as X op (C1 ^ C2). */
2217 (cmp (convert?@3 (bit_xor @0 INTEGER_CST@1)) INTEGER_CST@2)
2218 (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0)))
2219 (cmp @0 (bit_xor @1 (convert @2)))))
2222 (cmp (convert? addr@0) integer_zerop)
2223 (if (tree_single_nonzero_warnv_p (@0, NULL))
2224 { constant_boolean_node (cmp == NE_EXPR, type); })))
2226 /* If we have (A & C) == C where C is a power of 2, convert this into
2227 (A & C) != 0. Similarly for NE_EXPR. */
2231 (cmp (bit_and@2 @0 integer_pow2p@1) @1)
2232 (icmp @2 { build_zero_cst (TREE_TYPE (@0)); })))
2234 /* If we have (A & C) != 0 where C is the sign bit of A, convert
2235 this into A < 0. Similarly for (A & C) == 0 into A >= 0. */
2239 (cmp (bit_and (convert?@2 @0) integer_pow2p@1) integer_zerop)
2240 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2241 && (TYPE_PRECISION (TREE_TYPE (@0))
2242 == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (@0))))
2243 && element_precision (@2) >= element_precision (@0)
2244 && wi::only_sign_bit_p (@1, element_precision (@0)))
2245 (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
2246 (ncmp (convert:stype @0) { build_zero_cst (stype); })))))
2248 /* When the addresses are not directly of decls compare base and offset.
2249 This implements some remaining parts of fold_comparison address
2250 comparisons but still no complete part of it. Still it is good
2251 enough to make fold_stmt not regress when not dispatching to fold_binary. */
2252 (for cmp (simple_comparison)
2254 (cmp (convert1?@2 addr@0) (convert2? addr@1))
2257 HOST_WIDE_INT off0, off1;
2258 tree base0 = get_addr_base_and_unit_offset (TREE_OPERAND (@0, 0), &off0);
2259 tree base1 = get_addr_base_and_unit_offset (TREE_OPERAND (@1, 0), &off1);
2260 if (base0 && TREE_CODE (base0) == MEM_REF)
2262 off0 += mem_ref_offset (base0).to_short_addr ();
2263 base0 = TREE_OPERAND (base0, 0);
2265 if (base1 && TREE_CODE (base1) == MEM_REF)
2267 off1 += mem_ref_offset (base1).to_short_addr ();
2268 base1 = TREE_OPERAND (base1, 0);
2271 (if (base0 && base1)
2275 if (decl_in_symtab_p (base0)
2276 && decl_in_symtab_p (base1))
2277 equal = symtab_node::get_create (base0)
2278 ->equal_address_to (symtab_node::get_create (base1));
2279 else if ((DECL_P (base0)
2280 || TREE_CODE (base0) == SSA_NAME
2281 || TREE_CODE (base0) == STRING_CST)
2283 || TREE_CODE (base1) == SSA_NAME
2284 || TREE_CODE (base1) == STRING_CST))
2285 equal = (base0 == base1);
2288 && (cmp == EQ_EXPR || cmp == NE_EXPR
2289 /* If the offsets are equal we can ignore overflow. */
2291 || POINTER_TYPE_OVERFLOW_UNDEFINED
2292 /* Or if we compare using pointers to decls or strings. */
2293 || (POINTER_TYPE_P (TREE_TYPE (@2))
2294 && (DECL_P (base0) || TREE_CODE (base0) == STRING_CST))))
2296 (if (cmp == EQ_EXPR)
2297 { constant_boolean_node (off0 == off1, type); })
2298 (if (cmp == NE_EXPR)
2299 { constant_boolean_node (off0 != off1, type); })
2300 (if (cmp == LT_EXPR)
2301 { constant_boolean_node (off0 < off1, type); })
2302 (if (cmp == LE_EXPR)
2303 { constant_boolean_node (off0 <= off1, type); })
2304 (if (cmp == GE_EXPR)
2305 { constant_boolean_node (off0 >= off1, type); })
2306 (if (cmp == GT_EXPR)
2307 { constant_boolean_node (off0 > off1, type); }))
2309 && DECL_P (base0) && DECL_P (base1)
2310 /* If we compare this as integers require equal offset. */
2311 && (!INTEGRAL_TYPE_P (TREE_TYPE (@2))
2314 (if (cmp == EQ_EXPR)
2315 { constant_boolean_node (false, type); })
2316 (if (cmp == NE_EXPR)
2317 { constant_boolean_node (true, type); })))))))))
2319 /* Non-equality compare simplifications from fold_binary */
2320 (for cmp (lt gt le ge)
2321 /* Comparisons with the highest or lowest possible integer of
2322 the specified precision will have known values. */
2324 (cmp (convert?@2 @0) INTEGER_CST@1)
2325 (if ((INTEGRAL_TYPE_P (TREE_TYPE (@1)) || POINTER_TYPE_P (TREE_TYPE (@1)))
2326 && tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@0)))
2329 tree arg1_type = TREE_TYPE (@1);
2330 unsigned int prec = TYPE_PRECISION (arg1_type);
2331 wide_int max = wi::max_value (arg1_type);
2332 wide_int signed_max = wi::max_value (prec, SIGNED);
2333 wide_int min = wi::min_value (arg1_type);
2336 (if (wi::eq_p (@1, max))
2338 (if (cmp == GT_EXPR)
2339 { constant_boolean_node (false, type); })
2340 (if (cmp == GE_EXPR)
2342 (if (cmp == LE_EXPR)
2343 { constant_boolean_node (true, type); })
2344 (if (cmp == LT_EXPR)
2346 (if (wi::eq_p (@1, min))
2348 (if (cmp == LT_EXPR)
2349 { constant_boolean_node (false, type); })
2350 (if (cmp == LE_EXPR)
2352 (if (cmp == GE_EXPR)
2353 { constant_boolean_node (true, type); })
2354 (if (cmp == GT_EXPR)
2356 (if (wi::eq_p (@1, max - 1))
2358 (if (cmp == GT_EXPR)
2359 (eq @2 { wide_int_to_tree (TREE_TYPE (@1), wi::add (@1, 1)); }))
2360 (if (cmp == LE_EXPR)
2361 (ne @2 { wide_int_to_tree (TREE_TYPE (@1), wi::add (@1, 1)); }))))
2362 (if (wi::eq_p (@1, min + 1))
2364 (if (cmp == GE_EXPR)
2365 (ne @2 { wide_int_to_tree (TREE_TYPE (@1), wi::sub (@1, 1)); }))
2366 (if (cmp == LT_EXPR)
2367 (eq @2 { wide_int_to_tree (TREE_TYPE (@1), wi::sub (@1, 1)); }))))
2368 (if (wi::eq_p (@1, signed_max)
2369 && TYPE_UNSIGNED (arg1_type)
2370 /* We will flip the signedness of the comparison operator
2371 associated with the mode of @1, so the sign bit is
2372 specified by this mode. Check that @1 is the signed
2373 max associated with this sign bit. */
2374 && prec == GET_MODE_PRECISION (TYPE_MODE (arg1_type))
2375 /* signed_type does not work on pointer types. */
2376 && INTEGRAL_TYPE_P (arg1_type))
2377 /* The following case also applies to X < signed_max+1
2378 and X >= signed_max+1 because previous transformations. */
2379 (if (cmp == LE_EXPR || cmp == GT_EXPR)
2380 (with { tree st = signed_type_for (arg1_type); }
2381 (if (cmp == LE_EXPR)
2382 (ge (convert:st @0) { build_zero_cst (st); })
2383 (lt (convert:st @0) { build_zero_cst (st); }))))))))))
2385 (for cmp (unordered ordered unlt unle ungt unge uneq ltgt)
2386 /* If the second operand is NaN, the result is constant. */
2389 (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
2390 && (cmp != LTGT_EXPR || ! flag_trapping_math))
2391 { constant_boolean_node (cmp == ORDERED_EXPR || cmp == LTGT_EXPR
2392 ? false : true, type); })))
2394 /* bool_var != 0 becomes bool_var. */
2396 (ne @0 integer_zerop)
2397 (if (TREE_CODE (TREE_TYPE (@0)) == BOOLEAN_TYPE
2398 && types_match (type, TREE_TYPE (@0)))
2400 /* bool_var == 1 becomes bool_var. */
2402 (eq @0 integer_onep)
2403 (if (TREE_CODE (TREE_TYPE (@0)) == BOOLEAN_TYPE
2404 && types_match (type, TREE_TYPE (@0)))
2407 bool_var == 0 becomes !bool_var or
2408 bool_var != 1 becomes !bool_var
2409 here because that only is good in assignment context as long
2410 as we require a tcc_comparison in GIMPLE_CONDs where we'd
2411 replace if (x == 0) with tem = ~x; if (tem != 0) which is
2412 clearly less optimal and which we'll transform again in forwprop. */
2415 /* Simplification of math builtins. These rules must all be optimizations
2416 as well as IL simplifications. If there is a possibility that the new
2417 form could be a pessimization, the rule should go in the canonicalization
2418 section that follows this one.
2420 Rules can generally go in this section if they satisfy one of
2423 - the rule describes an identity
2425 - the rule replaces calls with something as simple as addition or
2428 - the rule contains unary calls only and simplifies the surrounding
2429 arithmetic. (The idea here is to exclude non-unary calls in which
2430 one operand is constant and in which the call is known to be cheap
2431 when the operand has that value.) */
2433 (if (flag_unsafe_math_optimizations)
2434 /* Simplify sqrt(x) * sqrt(x) -> x. */
2436 (mult (SQRT@1 @0) @1)
2437 (if (!HONOR_SNANS (type))
2440 /* Simplify sqrt(x) * sqrt(y) -> sqrt(x*y). */
2441 (for root (SQRT CBRT)
2443 (mult (root:s @0) (root:s @1))
2444 (root (mult @0 @1))))
2446 /* Simplify expN(x) * expN(y) -> expN(x+y). */
2447 (for exps (EXP EXP2 EXP10 POW10)
2449 (mult (exps:s @0) (exps:s @1))
2450 (exps (plus @0 @1))))
2452 /* Simplify a/root(b/c) into a*root(c/b). */
2453 (for root (SQRT CBRT)
2455 (rdiv @0 (root:s (rdiv:s @1 @2)))
2456 (mult @0 (root (rdiv @2 @1)))))
2458 /* Simplify x/expN(y) into x*expN(-y). */
2459 (for exps (EXP EXP2 EXP10 POW10)
2461 (rdiv @0 (exps:s @1))
2462 (mult @0 (exps (negate @1)))))
2464 (for logs (LOG LOG2 LOG10 LOG10)
2465 exps (EXP EXP2 EXP10 POW10)
2466 /* logN(expN(x)) -> x. */
2470 /* expN(logN(x)) -> x. */
2475 /* Optimize logN(func()) for various exponential functions. We
2476 want to determine the value "x" and the power "exponent" in
2477 order to transform logN(x**exponent) into exponent*logN(x). */
2478 (for logs (LOG LOG LOG LOG2 LOG2 LOG2 LOG10 LOG10)
2479 exps (EXP2 EXP10 POW10 EXP EXP10 POW10 EXP EXP2)
2486 CASE_FLT_FN (BUILT_IN_EXP):
2487 /* Prepare to do logN(exp(exponent)) -> exponent*logN(e). */
2488 x = build_real_truncate (type, dconst_e ());
2490 CASE_FLT_FN (BUILT_IN_EXP2):
2491 /* Prepare to do logN(exp2(exponent)) -> exponent*logN(2). */
2492 x = build_real (type, dconst2);
2494 CASE_FLT_FN (BUILT_IN_EXP10):
2495 CASE_FLT_FN (BUILT_IN_POW10):
2496 /* Prepare to do logN(exp10(exponent)) -> exponent*logN(10). */
2498 REAL_VALUE_TYPE dconst10;
2499 real_from_integer (&dconst10, VOIDmode, 10, SIGNED);
2500 x = build_real (type, dconst10);
2507 (mult (logs { x; }) @0))))
2519 CASE_FLT_FN (BUILT_IN_SQRT):
2520 /* Prepare to do logN(sqrt(x)) -> 0.5*logN(x). */
2521 x = build_real (type, dconsthalf);
2523 CASE_FLT_FN (BUILT_IN_CBRT):
2524 /* Prepare to do logN(cbrt(x)) -> (1/3)*logN(x). */
2525 x = build_real_truncate (type, dconst_third ());
2531 (mult { x; } (logs @0)))))
2533 /* logN(pow(x,exponent)) -> exponent*logN(x). */
2534 (for logs (LOG LOG2 LOG10)
2538 (mult @1 (logs @0))))
2543 exps (EXP EXP2 EXP10 POW10)
2544 /* sqrt(expN(x)) -> expN(x*0.5). */
2547 (exps (mult @0 { build_real (type, dconsthalf); })))
2548 /* cbrt(expN(x)) -> expN(x/3). */
2551 (exps (mult @0 { build_real_truncate (type, dconst_third ()); })))
2552 /* pow(expN(x), y) -> expN(x*y). */
2555 (exps (mult @0 @1))))
2557 /* tan(atan(x)) -> x. */
2564 /* cabs(x+0i) or cabs(0+xi) -> abs(x). */
2566 (CABS (complex:c @0 real_zerop@1))
2569 /* trunc(trunc(x)) -> trunc(x), etc. */
2570 (for fns (TRUNC FLOOR CEIL ROUND NEARBYINT RINT)
2574 /* f(x) -> x if x is integer valued and f does nothing for such values. */
2575 (for fns (TRUNC FLOOR CEIL ROUND NEARBYINT)
2577 (fns integer_valued_real_p@0)
2579 /* Same for rint. We have to check flag_errno_math because
2580 integer_valued_real_p accepts +Inf, -Inf and NaNs as integers. */
2581 (if (!flag_errno_math)
2583 (RINT integer_valued_real_p@0)
2586 /* hypot(x,0) and hypot(0,x) -> abs(x). */
2588 (hypot:c @0 real_zerop@1)
2591 /* pow(1,x) -> 1. */
2593 (POW real_onep@0 @1)
2597 /* copysign(x,x) -> x. */
2602 /* copysign(x,y) -> fabs(x) if y is nonnegative. */
2603 (COPYSIGN @0 tree_expr_nonnegative_p@1)
2606 (for scale (LDEXP SCALBN SCALBLN)
2607 /* ldexp(0, x) -> 0. */
2609 (scale real_zerop@0 @1)
2611 /* ldexp(x, 0) -> x. */
2613 (scale @0 integer_zerop@1)
2615 /* ldexp(x, y) -> x if x is +-Inf or NaN. */
2617 (scale REAL_CST@0 @1)
2618 (if (!real_isfinite (TREE_REAL_CST_PTR (@0)))
2621 /* Canonicalization of sequences of math builtins. These rules represent
2622 IL simplifications but are not necessarily optimizations.
2624 The sincos pass is responsible for picking "optimal" implementations
2625 of math builtins, which may be more complicated and can sometimes go
2626 the other way, e.g. converting pow into a sequence of sqrts.
2627 We only want to do these canonicalizations before the pass has run. */
2629 (if (flag_unsafe_math_optimizations && canonicalize_math_p ())
2630 /* Simplify tan(x) * cos(x) -> sin(x). */
2632 (mult:c (TAN:s @0) (COS:s @0))
2635 /* Simplify x * pow(x,c) -> pow(x,c+1). */
2637 (mult @0 (POW:s @0 REAL_CST@1))
2638 (if (!TREE_OVERFLOW (@1))
2639 (POW @0 (plus @1 { build_one_cst (type); }))))
2641 /* Simplify sin(x) / cos(x) -> tan(x). */
2643 (rdiv (SIN:s @0) (COS:s @0))
2646 /* Simplify cos(x) / sin(x) -> 1 / tan(x). */
2648 (rdiv (COS:s @0) (SIN:s @0))
2649 (rdiv { build_one_cst (type); } (TAN @0)))
2651 /* Simplify sin(x) / tan(x) -> cos(x). */
2653 (rdiv (SIN:s @0) (TAN:s @0))
2654 (if (! HONOR_NANS (@0)
2655 && ! HONOR_INFINITIES (@0))
2658 /* Simplify tan(x) / sin(x) -> 1.0 / cos(x). */
2660 (rdiv (TAN:s @0) (SIN:s @0))
2661 (if (! HONOR_NANS (@0)
2662 && ! HONOR_INFINITIES (@0))
2663 (rdiv { build_one_cst (type); } (COS @0))))
2665 /* Simplify pow(x,y) * pow(x,z) -> pow(x,y+z). */
2667 (mult (POW:s @0 @1) (POW:s @0 @2))
2668 (POW @0 (plus @1 @2)))
2670 /* Simplify pow(x,y) * pow(z,y) -> pow(x*z,y). */
2672 (mult (POW:s @0 @1) (POW:s @2 @1))
2673 (POW (mult @0 @2) @1))
2675 /* Simplify pow(x,c) / x -> pow(x,c-1). */
2677 (rdiv (POW:s @0 REAL_CST@1) @0)
2678 (if (!TREE_OVERFLOW (@1))
2679 (POW @0 (minus @1 { build_one_cst (type); }))))
2681 /* Simplify x / pow (y,z) -> x * pow(y,-z). */
2683 (rdiv @0 (POW:s @1 @2))
2684 (mult @0 (POW @1 (negate @2))))
2689 /* sqrt(sqrt(x)) -> pow(x,1/4). */
2692 (pows @0 { build_real (type, dconst_quarter ()); }))
2693 /* sqrt(cbrt(x)) -> pow(x,1/6). */
2696 (pows @0 { build_real_truncate (type, dconst_sixth ()); }))
2697 /* cbrt(sqrt(x)) -> pow(x,1/6). */
2700 (pows @0 { build_real_truncate (type, dconst_sixth ()); }))
2701 /* cbrt(cbrt(x)) -> pow(x,1/9), iff x is nonnegative. */
2703 (cbrts (cbrts tree_expr_nonnegative_p@0))
2704 (pows @0 { build_real_truncate (type, dconst_ninth ()); }))
2705 /* sqrt(pow(x,y)) -> pow(|x|,y*0.5). */
2707 (sqrts (pows @0 @1))
2708 (pows (abs @0) (mult @1 { build_real (type, dconsthalf); })))
2709 /* cbrt(pow(x,y)) -> pow(x,y/3), iff x is nonnegative. */
2711 (cbrts (pows tree_expr_nonnegative_p@0 @1))
2712 (pows @0 (mult @1 { build_real_truncate (type, dconst_third ()); })))
2713 /* pow(sqrt(x),y) -> pow(x,y*0.5). */
2715 (pows (sqrts @0) @1)
2716 (pows @0 (mult @1 { build_real (type, dconsthalf); })))
2717 /* pow(cbrt(x),y) -> pow(x,y/3) iff x is nonnegative. */
2719 (pows (cbrts tree_expr_nonnegative_p@0) @1)
2720 (pows @0 (mult @1 { build_real_truncate (type, dconst_third ()); })))
2721 /* pow(pow(x,y),z) -> pow(x,y*z) iff x is nonnegative. */
2723 (pows (pows tree_expr_nonnegative_p@0 @1) @2)
2724 (pows @0 (mult @1 @2))))
2726 /* cabs(x+xi) -> fabs(x)*sqrt(2). */
2728 (CABS (complex @0 @0))
2729 (mult (abs @0) { build_real_truncate (type, dconst_sqrt2 ()); }))
2731 /* hypot(x,x) -> fabs(x)*sqrt(2). */
2734 (mult (abs @0) { build_real_truncate (type, dconst_sqrt2 ()); }))
2736 /* cexp(x+yi) -> exp(x)*cexpi(y). */
2741 (cexps compositional_complex@0)
2742 (if (targetm.libc_has_function (function_c99_math_complex))
2744 (mult (exps@1 (realpart @0)) (realpart (cexpis:type@2 (imagpart @0))))
2745 (mult @1 (imagpart @2)))))))
2747 (if (canonicalize_math_p ())
2748 /* floor(x) -> trunc(x) if x is nonnegative. */
2752 (floors tree_expr_nonnegative_p@0)
2755 (match double_value_p
2757 (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == double_type_node)))
2758 (for froms (BUILT_IN_TRUNCL
2770 /* truncl(extend(x)) -> extend(trunc(x)), etc., if x is a double. */
2771 (if (optimize && canonicalize_math_p ())
2773 (froms (convert double_value_p@0))
2774 (convert (tos @0)))))
2776 (match float_value_p
2778 (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == float_type_node)))
2779 (for froms (BUILT_IN_TRUNCL BUILT_IN_TRUNC
2780 BUILT_IN_FLOORL BUILT_IN_FLOOR
2781 BUILT_IN_CEILL BUILT_IN_CEIL
2782 BUILT_IN_ROUNDL BUILT_IN_ROUND
2783 BUILT_IN_NEARBYINTL BUILT_IN_NEARBYINT
2784 BUILT_IN_RINTL BUILT_IN_RINT)
2785 tos (BUILT_IN_TRUNCF BUILT_IN_TRUNCF
2786 BUILT_IN_FLOORF BUILT_IN_FLOORF
2787 BUILT_IN_CEILF BUILT_IN_CEILF
2788 BUILT_IN_ROUNDF BUILT_IN_ROUNDF
2789 BUILT_IN_NEARBYINTF BUILT_IN_NEARBYINTF
2790 BUILT_IN_RINTF BUILT_IN_RINTF)
2791 /* truncl(extend(x)) and trunc(extend(x)) -> extend(truncf(x)), etc.,
2793 (if (optimize && canonicalize_math_p ())
2795 (froms (convert float_value_p@0))
2796 (convert (tos @0)))))
2798 (for froms (XFLOORL XCEILL XROUNDL XRINTL)
2799 tos (XFLOOR XCEIL XROUND XRINT)
2800 /* llfloorl(extend(x)) -> llfloor(x), etc., if x is a double. */
2801 (if (optimize && canonicalize_math_p ())
2803 (froms (convert double_value_p@0))
2806 (for froms (XFLOORL XCEILL XROUNDL XRINTL
2807 XFLOOR XCEIL XROUND XRINT)
2808 tos (XFLOORF XCEILF XROUNDF XRINTF)
2809 /* llfloorl(extend(x)) and llfloor(extend(x)) -> llfloorf(x), etc.,
2811 (if (optimize && canonicalize_math_p ())
2813 (froms (convert float_value_p@0))
2816 (if (canonicalize_math_p ())
2817 /* xfloor(x) -> fix_trunc(x) if x is nonnegative. */
2818 (for floors (IFLOOR LFLOOR LLFLOOR)
2820 (floors tree_expr_nonnegative_p@0)
2823 (if (canonicalize_math_p ())
2824 /* xfloor(x) -> fix_trunc(x), etc., if x is integer valued. */
2825 (for fns (IFLOOR LFLOOR LLFLOOR
2827 IROUND LROUND LLROUND)
2829 (fns integer_valued_real_p@0)
2831 (if (!flag_errno_math)
2832 /* xrint(x) -> fix_trunc(x), etc., if x is integer valued. */
2833 (for rints (IRINT LRINT LLRINT)
2835 (rints integer_valued_real_p@0)
2838 (if (canonicalize_math_p ())
2839 (for ifn (IFLOOR ICEIL IROUND IRINT)
2840 lfn (LFLOOR LCEIL LROUND LRINT)
2841 llfn (LLFLOOR LLCEIL LLROUND LLRINT)
2842 /* Canonicalize iround (x) to lround (x) on ILP32 targets where
2843 sizeof (int) == sizeof (long). */
2844 (if (TYPE_PRECISION (integer_type_node)
2845 == TYPE_PRECISION (long_integer_type_node))
2848 (lfn:long_integer_type_node @0)))
2849 /* Canonicalize llround (x) to lround (x) on LP64 targets where
2850 sizeof (long long) == sizeof (long). */
2851 (if (TYPE_PRECISION (long_long_integer_type_node)
2852 == TYPE_PRECISION (long_integer_type_node))
2855 (lfn:long_integer_type_node @0)))))
2857 /* cproj(x) -> x if we're ignoring infinities. */
2860 (if (!HONOR_INFINITIES (type))
2863 /* If the real part is inf and the imag part is known to be
2864 nonnegative, return (inf + 0i). */
2866 (CPROJ (complex REAL_CST@0 tree_expr_nonnegative_p@1))
2867 (if (real_isinf (TREE_REAL_CST_PTR (@0)))
2868 { build_complex_inf (type, false); }))
2870 /* If the imag part is inf, return (inf+I*copysign(0,imag)). */
2872 (CPROJ (complex @0 REAL_CST@1))
2873 (if (real_isinf (TREE_REAL_CST_PTR (@1)))
2874 { build_complex_inf (type, TREE_REAL_CST_PTR (@1)->sign); }))
2880 (pows @0 REAL_CST@1)
2882 const REAL_VALUE_TYPE *value = TREE_REAL_CST_PTR (@1);
2883 REAL_VALUE_TYPE tmp;
2886 /* pow(x,0) -> 1. */
2887 (if (real_equal (value, &dconst0))
2888 { build_real (type, dconst1); })
2889 /* pow(x,1) -> x. */
2890 (if (real_equal (value, &dconst1))
2892 /* pow(x,-1) -> 1/x. */
2893 (if (real_equal (value, &dconstm1))
2894 (rdiv { build_real (type, dconst1); } @0))
2895 /* pow(x,0.5) -> sqrt(x). */
2896 (if (flag_unsafe_math_optimizations
2897 && canonicalize_math_p ()
2898 && real_equal (value, &dconsthalf))
2900 /* pow(x,1/3) -> cbrt(x). */
2901 (if (flag_unsafe_math_optimizations
2902 && canonicalize_math_p ()
2903 && (tmp = real_value_truncate (TYPE_MODE (type), dconst_third ()),
2904 real_equal (value, &tmp)))
2907 /* powi(1,x) -> 1. */
2909 (POWI real_onep@0 @1)
2913 (POWI @0 INTEGER_CST@1)
2915 /* powi(x,0) -> 1. */
2916 (if (wi::eq_p (@1, 0))
2917 { build_real (type, dconst1); })
2918 /* powi(x,1) -> x. */
2919 (if (wi::eq_p (@1, 1))
2921 /* powi(x,-1) -> 1/x. */
2922 (if (wi::eq_p (@1, -1))
2923 (rdiv { build_real (type, dconst1); } @0))))
2925 /* Narrowing of arithmetic and logical operations.
2927 These are conceptually similar to the transformations performed for
2928 the C/C++ front-ends by shorten_binary_op and shorten_compare. Long
2929 term we want to move all that code out of the front-ends into here. */
2931 /* If we have a narrowing conversion of an arithmetic operation where
2932 both operands are widening conversions from the same type as the outer
2933 narrowing conversion. Then convert the innermost operands to a suitable
2934 unsigned type (to avoid introducing undefined behaviour), perform the
2935 operation and convert the result to the desired type. */
2936 (for op (plus minus)
2938 (convert (op:s (convert@2 @0) (convert@3 @1)))
2939 (if (INTEGRAL_TYPE_P (type)
2940 /* We check for type compatibility between @0 and @1 below,
2941 so there's no need to check that @1/@3 are integral types. */
2942 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
2943 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
2944 /* The precision of the type of each operand must match the
2945 precision of the mode of each operand, similarly for the
2947 && (TYPE_PRECISION (TREE_TYPE (@0))
2948 == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (@0))))
2949 && (TYPE_PRECISION (TREE_TYPE (@1))
2950 == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (@1))))
2951 && TYPE_PRECISION (type) == GET_MODE_PRECISION (TYPE_MODE (type))
2952 /* The inner conversion must be a widening conversion. */
2953 && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
2954 && types_match (@0, @1)
2955 && types_match (@0, type))
2956 (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
2957 (convert (op @0 @1))
2958 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
2959 (convert (op (convert:utype @0) (convert:utype @1))))))))
2961 /* This is another case of narrowing, specifically when there's an outer
2962 BIT_AND_EXPR which masks off bits outside the type of the innermost
2963 operands. Like the previous case we have to convert the operands
2964 to unsigned types to avoid introducing undefined behaviour for the
2965 arithmetic operation. */
2966 (for op (minus plus)
2968 (bit_and (op:s (convert@2 @0) (convert@3 @1)) INTEGER_CST@4)
2969 (if (INTEGRAL_TYPE_P (type)
2970 /* We check for type compatibility between @0 and @1 below,
2971 so there's no need to check that @1/@3 are integral types. */
2972 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
2973 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
2974 /* The precision of the type of each operand must match the
2975 precision of the mode of each operand, similarly for the
2977 && (TYPE_PRECISION (TREE_TYPE (@0))
2978 == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (@0))))
2979 && (TYPE_PRECISION (TREE_TYPE (@1))
2980 == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (@1))))
2981 && TYPE_PRECISION (type) == GET_MODE_PRECISION (TYPE_MODE (type))
2982 /* The inner conversion must be a widening conversion. */
2983 && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
2984 && types_match (@0, @1)
2985 && (tree_int_cst_min_precision (@4, TYPE_SIGN (TREE_TYPE (@0)))
2986 <= TYPE_PRECISION (TREE_TYPE (@0)))
2987 && (wi::bit_and (@4, wi::mask (TYPE_PRECISION (TREE_TYPE (@0)),
2988 true, TYPE_PRECISION (type))) == 0))
2989 (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
2990 (with { tree ntype = TREE_TYPE (@0); }
2991 (convert (bit_and (op @0 @1) (convert:ntype @4))))
2992 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
2993 (convert (bit_and (op (convert:utype @0) (convert:utype @1))
2994 (convert:utype @4))))))))
2996 /* Transform (@0 < @1 and @0 < @2) to use min,
2997 (@0 > @1 and @0 > @2) to use max */
2998 (for op (lt le gt ge)
2999 ext (min min max max)
3001 (bit_and (op:s @0 @1) (op:s @0 @2))
3002 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)))
3003 (op @0 (ext @1 @2)))))
3006 /* signbit(x) -> 0 if x is nonnegative. */
3007 (SIGNBIT tree_expr_nonnegative_p@0)
3008 { integer_zero_node; })
3011 /* signbit(x) -> x<0 if x doesn't have signed zeros. */
3013 (if (!HONOR_SIGNED_ZEROS (@0))
3014 (convert (lt @0 { build_real (TREE_TYPE (@0), dconst0); }))))