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
38 (define_operator_list tcc_comparison
39 lt le eq ne ge gt unordered ordered unlt unle ungt unge uneq ltgt)
40 (define_operator_list inverted_tcc_comparison
41 ge gt ne eq lt le ordered unordered ge gt le lt ltgt uneq)
42 (define_operator_list inverted_tcc_comparison_with_nans
43 unge ungt ne eq unlt unle ordered unordered ge gt le lt ltgt uneq)
44 (define_operator_list swapped_tcc_comparison
45 gt ge eq ne le lt unordered ordered ungt unge unlt unle uneq ltgt)
46 (define_operator_list simple_comparison lt le eq ne ge gt)
47 (define_operator_list swapped_simple_comparison gt ge eq ne le lt)
49 (define_operator_list LOG BUILT_IN_LOGF BUILT_IN_LOG BUILT_IN_LOGL)
50 (define_operator_list EXP BUILT_IN_EXPF BUILT_IN_EXP BUILT_IN_EXPL)
51 (define_operator_list LOG2 BUILT_IN_LOG2F BUILT_IN_LOG2 BUILT_IN_LOG2L)
52 (define_operator_list EXP2 BUILT_IN_EXP2F BUILT_IN_EXP2 BUILT_IN_EXP2L)
53 (define_operator_list LOG10 BUILT_IN_LOG10F BUILT_IN_LOG10 BUILT_IN_LOG10L)
54 (define_operator_list EXP10 BUILT_IN_EXP10F BUILT_IN_EXP10 BUILT_IN_EXP10L)
55 (define_operator_list POW BUILT_IN_POWF BUILT_IN_POW BUILT_IN_POWL)
56 (define_operator_list POW10 BUILT_IN_POW10F BUILT_IN_POW10 BUILT_IN_POW10L)
57 (define_operator_list SQRT BUILT_IN_SQRTF BUILT_IN_SQRT BUILT_IN_SQRTL)
58 (define_operator_list CBRT BUILT_IN_CBRTF BUILT_IN_CBRT BUILT_IN_CBRTL)
59 (define_operator_list SIN BUILT_IN_SINF BUILT_IN_SIN BUILT_IN_SINL)
60 (define_operator_list COS BUILT_IN_COSF BUILT_IN_COS BUILT_IN_COSL)
61 (define_operator_list TAN BUILT_IN_TANF BUILT_IN_TAN BUILT_IN_TANL)
62 (define_operator_list ATAN BUILT_IN_ATANF BUILT_IN_ATAN BUILT_IN_ATANL)
63 (define_operator_list COSH BUILT_IN_COSHF BUILT_IN_COSH BUILT_IN_COSHL)
64 (define_operator_list CEXPI BUILT_IN_CEXPIF BUILT_IN_CEXPI BUILT_IN_CEXPIL)
65 (define_operator_list CPROJ BUILT_IN_CPROJF BUILT_IN_CPROJ BUILT_IN_CPROJL)
66 (define_operator_list CCOS BUILT_IN_CCOSF BUILT_IN_CCOS BUILT_IN_CCOSL)
67 (define_operator_list CCOSH BUILT_IN_CCOSHF BUILT_IN_CCOSH BUILT_IN_CCOSHL)
68 (define_operator_list HYPOT BUILT_IN_HYPOTF BUILT_IN_HYPOT BUILT_IN_HYPOTL)
69 (define_operator_list COPYSIGN BUILT_IN_COPYSIGNF
72 (define_operator_list CABS BUILT_IN_CABSF BUILT_IN_CABS BUILT_IN_CABSL)
74 /* Simplifications of operations with one constant operand and
75 simplifications to constants or single values. */
77 (for op (plus pointer_plus minus bit_ior bit_xor)
82 /* 0 +p index -> (type)index */
84 (pointer_plus integer_zerop @1)
85 (non_lvalue (convert @1)))
87 /* See if ARG1 is zero and X + ARG1 reduces to X.
88 Likewise if the operands are reversed. */
90 (plus:c @0 real_zerop@1)
91 (if (fold_real_zero_addition_p (type, @1, 0))
94 /* See if ARG1 is zero and X - ARG1 reduces to X. */
96 (minus @0 real_zerop@1)
97 (if (fold_real_zero_addition_p (type, @1, 1))
101 This is unsafe for certain floats even in non-IEEE formats.
102 In IEEE, it is unsafe because it does wrong for NaNs.
103 Also note that operand_equal_p is always false if an operand
107 (if (!FLOAT_TYPE_P (type) || !HONOR_NANS (type))
108 { build_zero_cst (type); }))
111 (mult @0 integer_zerop@1)
114 /* Maybe fold x * 0 to 0. The expressions aren't the same
115 when x is NaN, since x * 0 is also NaN. Nor are they the
116 same in modes with signed zeros, since multiplying a
117 negative value by 0 gives -0, not +0. */
119 (mult @0 real_zerop@1)
120 (if (!HONOR_NANS (type) && !HONOR_SIGNED_ZEROS (type))
123 /* In IEEE floating point, x*1 is not equivalent to x for snans.
124 Likewise for complex arithmetic with signed zeros. */
127 (if (!HONOR_SNANS (type)
128 && (!HONOR_SIGNED_ZEROS (type)
129 || !COMPLEX_FLOAT_TYPE_P (type)))
132 /* Transform x * -1.0 into -x. */
134 (mult @0 real_minus_onep)
135 (if (!HONOR_SNANS (type)
136 && (!HONOR_SIGNED_ZEROS (type)
137 || !COMPLEX_FLOAT_TYPE_P (type)))
140 /* Make sure to preserve divisions by zero. This is the reason why
141 we don't simplify x / x to 1 or 0 / x to 0. */
142 (for op (mult trunc_div ceil_div floor_div round_div exact_div)
148 (for div (trunc_div ceil_div floor_div round_div exact_div)
150 (div @0 integer_minus_onep@1)
151 (if (!TYPE_UNSIGNED (type))
154 /* For unsigned integral types, FLOOR_DIV_EXPR is the same as
155 TRUNC_DIV_EXPR. Rewrite into the latter in this case. */
158 (if ((INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
159 && TYPE_UNSIGNED (type))
162 /* Combine two successive divisions. Note that combining ceil_div
163 and floor_div is trickier and combining round_div even more so. */
164 (for div (trunc_div exact_div)
166 (div (div @0 INTEGER_CST@1) INTEGER_CST@2)
169 wide_int mul = wi::mul (@1, @2, TYPE_SIGN (type), &overflow_p);
172 (div @0 { wide_int_to_tree (type, mul); })
173 (if (TYPE_UNSIGNED (type)
174 || mul != wi::min_value (TYPE_PRECISION (type), SIGNED))
175 { build_zero_cst (type); })))))
177 /* Optimize A / A to 1.0 if we don't care about
178 NaNs or Infinities. */
181 (if (FLOAT_TYPE_P (type)
182 && ! HONOR_NANS (type)
183 && ! HONOR_INFINITIES (type))
184 { build_one_cst (type); }))
186 /* Optimize -A / A to -1.0 if we don't care about
187 NaNs or Infinities. */
189 (rdiv:c @0 (negate @0))
190 (if (FLOAT_TYPE_P (type)
191 && ! HONOR_NANS (type)
192 && ! HONOR_INFINITIES (type))
193 { build_minus_one_cst (type); }))
195 /* In IEEE floating point, x/1 is not equivalent to x for snans. */
198 (if (!HONOR_SNANS (type))
201 /* In IEEE floating point, x/-1 is not equivalent to -x for snans. */
203 (rdiv @0 real_minus_onep)
204 (if (!HONOR_SNANS (type))
207 /* If ARG1 is a constant, we can convert this to a multiply by the
208 reciprocal. This does not have the same rounding properties,
209 so only do this if -freciprocal-math. We can actually
210 always safely do it if ARG1 is a power of two, but it's hard to
211 tell if it is or not in a portable manner. */
212 (for cst (REAL_CST COMPLEX_CST VECTOR_CST)
216 (if (flag_reciprocal_math
219 { tree tem = const_binop (RDIV_EXPR, type, build_one_cst (type), @1); }
221 (mult @0 { tem; } )))
222 (if (cst != COMPLEX_CST)
223 (with { tree inverse = exact_inverse (type, @1); }
225 (mult @0 { inverse; } ))))))))
227 /* Same applies to modulo operations, but fold is inconsistent here
228 and simplifies 0 % x to 0, only preserving literal 0 % 0. */
229 (for mod (ceil_mod floor_mod round_mod trunc_mod)
230 /* 0 % X is always zero. */
232 (mod integer_zerop@0 @1)
233 /* But not for 0 % 0 so that we can get the proper warnings and errors. */
234 (if (!integer_zerop (@1))
236 /* X % 1 is always zero. */
238 (mod @0 integer_onep)
239 { build_zero_cst (type); })
240 /* X % -1 is zero. */
242 (mod @0 integer_minus_onep@1)
243 (if (!TYPE_UNSIGNED (type))
244 { build_zero_cst (type); }))
245 /* (X % Y) % Y is just X % Y. */
247 (mod (mod@2 @0 @1) @1)
249 /* From extract_muldiv_1: (X * C1) % C2 is zero if C1 is a multiple of C2. */
251 (mod (mult @0 INTEGER_CST@1) INTEGER_CST@2)
252 (if (ANY_INTEGRAL_TYPE_P (type)
253 && TYPE_OVERFLOW_UNDEFINED (type)
254 && wi::multiple_of_p (@1, @2, TYPE_SIGN (type)))
255 { build_zero_cst (type); })))
257 /* X % -C is the same as X % C. */
259 (trunc_mod @0 INTEGER_CST@1)
260 (if (TYPE_SIGN (type) == SIGNED
261 && !TREE_OVERFLOW (@1)
263 && !TYPE_OVERFLOW_TRAPS (type)
264 /* Avoid this transformation if C is INT_MIN, i.e. C == -C. */
265 && !sign_bit_p (@1, @1))
266 (trunc_mod @0 (negate @1))))
268 /* X % -Y is the same as X % Y. */
270 (trunc_mod @0 (convert? (negate @1)))
271 (if (!TYPE_UNSIGNED (type)
272 && !TYPE_OVERFLOW_TRAPS (type)
273 && tree_nop_conversion_p (type, TREE_TYPE (@1)))
274 (trunc_mod @0 (convert @1))))
276 /* X - (X / Y) * Y is the same as X % Y. */
278 (minus (convert1? @0) (convert2? (mult (trunc_div @0 @1) @1)))
279 (if ((INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
280 && TYPE_UNSIGNED (TREE_TYPE (@0)) == TYPE_UNSIGNED (type))
281 (trunc_mod (convert @0) (convert @1))))
283 /* Optimize TRUNC_MOD_EXPR by a power of two into a BIT_AND_EXPR,
284 i.e. "X % C" into "X & (C - 1)", if X and C are positive.
285 Also optimize A % (C << N) where C is a power of 2,
286 to A & ((C << N) - 1). */
287 (match (power_of_two_cand @1)
289 (match (power_of_two_cand @1)
290 (lshift INTEGER_CST@1 @2))
291 (for mod (trunc_mod floor_mod)
293 (mod @0 (convert?@3 (power_of_two_cand@1 @2)))
294 (if ((TYPE_UNSIGNED (type)
295 || tree_expr_nonnegative_p (@0))
296 && tree_nop_conversion_p (type, TREE_TYPE (@3))
297 && integer_pow2p (@2) && tree_int_cst_sgn (@2) > 0)
298 (bit_and @0 (convert (minus @1 { build_int_cst (TREE_TYPE (@1), 1); }))))))
300 /* Simplify (unsigned t * 2)/2 -> unsigned t & 0x7FFFFFFF. */
302 (trunc_div (mult @0 integer_pow2p@1) @1)
303 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
304 (bit_and @0 { wide_int_to_tree
305 (type, wi::mask (TYPE_PRECISION (type) - wi::exact_log2 (@1),
306 false, TYPE_PRECISION (type))); })))
308 /* Simplify (unsigned t / 2) * 2 -> unsigned t & ~1. */
310 (mult (trunc_div @0 integer_pow2p@1) @1)
311 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
312 (bit_and @0 (negate @1))))
314 /* Simplify (t * 2) / 2) -> t. */
315 (for div (trunc_div ceil_div floor_div round_div exact_div)
317 (div (mult @0 @1) @1)
318 (if (ANY_INTEGRAL_TYPE_P (type)
319 && TYPE_OVERFLOW_UNDEFINED (type))
323 /* Simplify cos(-x) and cos(|x|) -> cos(x). Similarly for cosh. */
328 /* Simplify pow(-x, y) and pow(|x|,y) -> pow(x,y) if y is an even integer. */
331 (pows (op @0) REAL_CST@1)
332 (with { HOST_WIDE_INT n; }
333 (if (real_isinteger (&TREE_REAL_CST (@1), &n) && (n & 1) == 0)
335 /* Strip negate and abs from both operands of hypot. */
343 /* copysign(-x, y) and copysign(abs(x), y) -> copysign(x, y). */
344 (for copysigns (COPYSIGN)
346 (copysigns (op @0) @1)
349 /* abs(x)*abs(x) -> x*x. Should be valid for all types. */
354 /* cos(copysign(x, y)) -> cos(x). Similarly for cosh. */
358 (coss (copysigns @0 @1))
361 /* pow(copysign(x, y), z) -> pow(x, z) if z is an even integer. */
365 (pows (copysigns @0 @1) REAL_CST@1)
366 (with { HOST_WIDE_INT n; }
367 (if (real_isinteger (&TREE_REAL_CST (@1), &n) && (n & 1) == 0)
372 /* hypot(copysign(x, y), z) -> hypot(x, z). */
374 (hypots (copysigns @0 @1) @2)
376 /* hypot(x, copysign(y, z)) -> hypot(x, y). */
378 (hypots @0 (copysigns @1 @2))
381 /* copysign(copysign(x, y), z) -> copysign(x, z). */
382 (for copysigns (COPYSIGN)
384 (copysigns (copysigns @0 @1) @2)
387 /* copysign(x,y)*copysign(x,y) -> x*x. */
388 (for copysigns (COPYSIGN)
390 (mult (copysigns@2 @0 @1) @2)
393 /* ccos(-x) -> ccos(x). Similarly for ccosh. */
394 (for ccoss (CCOS CCOSH)
399 /* cabs(-x) and cos(conj(x)) -> cabs(x). */
400 (for ops (conj negate)
406 /* Fold (a * (1 << b)) into (a << b) */
408 (mult:c @0 (convert? (lshift integer_onep@1 @2)))
409 (if (! FLOAT_TYPE_P (type)
410 && tree_nop_conversion_p (type, TREE_TYPE (@1)))
413 /* Fold (C1/X)*C2 into (C1*C2)/X. */
415 (mult (rdiv:s REAL_CST@0 @1) REAL_CST@2)
416 (if (flag_associative_math)
418 { tree tem = const_binop (MULT_EXPR, type, @0, @2); }
420 (rdiv { tem; } @1)))))
422 /* Simplify ~X & X as zero. */
424 (bit_and:c (convert? @0) (convert? (bit_not @0)))
425 { build_zero_cst (type); })
427 /* X % Y is smaller than Y. */
430 (cmp (trunc_mod @0 @1) @1)
431 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
432 { constant_boolean_node (cmp == LT_EXPR, type); })))
435 (cmp @1 (trunc_mod @0 @1))
436 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
437 { constant_boolean_node (cmp == GT_EXPR, type); })))
441 (bit_ior @0 integer_all_onesp@1)
446 (bit_and @0 integer_zerop@1)
452 (for op (bit_ior bit_xor plus)
454 (op:c (convert? @0) (convert? (bit_not @0)))
455 (convert { build_all_ones_cst (TREE_TYPE (@0)); })))
460 { build_zero_cst (type); })
462 /* Canonicalize X ^ ~0 to ~X. */
464 (bit_xor @0 integer_all_onesp@1)
469 (bit_and @0 integer_all_onesp)
472 /* x & x -> x, x | x -> x */
473 (for bitop (bit_and bit_ior)
478 /* x + (x & 1) -> (x + 1) & ~1 */
480 (plus:c @0 (bit_and:s @0 integer_onep@1))
481 (bit_and (plus @0 @1) (bit_not @1)))
483 /* x & ~(x & y) -> x & ~y */
484 /* x | ~(x | y) -> x | ~y */
485 (for bitop (bit_and bit_ior)
487 (bitop:c @0 (bit_not (bitop:cs @0 @1)))
488 (bitop @0 (bit_not @1))))
490 /* (x | y) & ~x -> y & ~x */
491 /* (x & y) | ~x -> y | ~x */
492 (for bitop (bit_and bit_ior)
493 rbitop (bit_ior bit_and)
495 (bitop:c (rbitop:c @0 @1) (bit_not@2 @0))
498 /* (x & y) ^ (x | y) -> x ^ y */
500 (bit_xor:c (bit_and @0 @1) (bit_ior @0 @1))
503 /* (x ^ y) ^ (x | y) -> x & y */
505 (bit_xor:c (bit_xor @0 @1) (bit_ior @0 @1))
508 /* (x & y) + (x ^ y) -> x | y */
509 /* (x & y) | (x ^ y) -> x | y */
510 /* (x & y) ^ (x ^ y) -> x | y */
511 (for op (plus bit_ior bit_xor)
513 (op:c (bit_and @0 @1) (bit_xor @0 @1))
516 /* (x & y) + (x | y) -> x + y */
518 (plus:c (bit_and @0 @1) (bit_ior @0 @1))
521 /* (x + y) - (x | y) -> x & y */
523 (minus (plus @0 @1) (bit_ior @0 @1))
524 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
525 && !TYPE_SATURATING (type))
528 /* (x + y) - (x & y) -> x | y */
530 (minus (plus @0 @1) (bit_and @0 @1))
531 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
532 && !TYPE_SATURATING (type))
535 /* (x | y) - (x ^ y) -> x & y */
537 (minus (bit_ior @0 @1) (bit_xor @0 @1))
540 /* (x | y) - (x & y) -> x ^ y */
542 (minus (bit_ior @0 @1) (bit_and @0 @1))
545 /* (x | y) & ~(x & y) -> x ^ y */
547 (bit_and:c (bit_ior @0 @1) (bit_not (bit_and @0 @1)))
550 /* (x | y) & (~x ^ y) -> x & y */
552 (bit_and:c (bit_ior:c @0 @1) (bit_xor:c @1 (bit_not @0)))
555 /* ~x & ~y -> ~(x | y)
556 ~x | ~y -> ~(x & y) */
557 (for op (bit_and bit_ior)
558 rop (bit_ior bit_and)
560 (op (convert1? (bit_not @0)) (convert2? (bit_not @1)))
561 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
562 && tree_nop_conversion_p (type, TREE_TYPE (@1)))
563 (bit_not (rop (convert @0) (convert @1))))))
565 /* If we are XORing or adding two BIT_AND_EXPR's, both of which are and'ing
566 with a constant, and the two constants have no bits in common,
567 we should treat this as a BIT_IOR_EXPR since this may produce more
569 (for op (bit_xor plus)
571 (op (convert1? (bit_and@4 @0 INTEGER_CST@1))
572 (convert2? (bit_and@5 @2 INTEGER_CST@3)))
573 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
574 && tree_nop_conversion_p (type, TREE_TYPE (@2))
575 && wi::bit_and (@1, @3) == 0)
576 (bit_ior (convert @4) (convert @5)))))
578 /* (X | Y) ^ X -> Y & ~ X*/
580 (bit_xor:c (convert? (bit_ior:c @0 @1)) (convert? @0))
581 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
582 (convert (bit_and @1 (bit_not @0)))))
584 /* Convert ~X ^ ~Y to X ^ Y. */
586 (bit_xor (convert1? (bit_not @0)) (convert2? (bit_not @1)))
587 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
588 && tree_nop_conversion_p (type, TREE_TYPE (@1)))
589 (bit_xor (convert @0) (convert @1))))
591 /* Convert ~X ^ C to X ^ ~C. */
593 (bit_xor (convert? (bit_not @0)) INTEGER_CST@1)
594 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
595 (bit_xor (convert @0) (bit_not @1))))
597 /* Fold (X & Y) ^ Y as ~X & Y. */
599 (bit_xor:c (bit_and:c @0 @1) @1)
600 (bit_and (bit_not @0) @1))
602 /* Given a bit-wise operation CODE applied to ARG0 and ARG1, see if both
603 operands are another bit-wise operation with a common input. If so,
604 distribute the bit operations to save an operation and possibly two if
605 constants are involved. For example, convert
606 (A | B) & (A | C) into A | (B & C)
607 Further simplification will occur if B and C are constants. */
608 (for op (bit_and bit_ior)
609 rop (bit_ior bit_and)
611 (op (convert? (rop:c @0 @1)) (convert? (rop @0 @2)))
612 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
613 (rop (convert @0) (op (convert @1) (convert @2))))))
623 (abs tree_expr_nonnegative_p@0)
626 /* A few cases of fold-const.c negate_expr_p predicate. */
629 (if ((INTEGRAL_TYPE_P (type)
630 && TYPE_OVERFLOW_WRAPS (type))
631 || (!TYPE_OVERFLOW_SANITIZED (type)
632 && may_negate_without_overflow_p (t)))))
637 (if (!TYPE_OVERFLOW_SANITIZED (type))))
640 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (t)))))
641 /* VECTOR_CST handling of non-wrapping types would recurse in unsupported
645 (if (FLOAT_TYPE_P (TREE_TYPE (type)) || TYPE_OVERFLOW_WRAPS (type))))
647 /* (-A) * (-B) -> A * B */
649 (mult:c (convert1? (negate @0)) (convert2? negate_expr_p@1))
650 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
651 && tree_nop_conversion_p (type, TREE_TYPE (@1)))
652 (mult (convert @0) (convert (negate @1)))))
654 /* -(A + B) -> (-B) - A. */
656 (negate (plus:c @0 negate_expr_p@1))
657 (if (!HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type))
658 && !HONOR_SIGNED_ZEROS (element_mode (type)))
659 (minus (negate @1) @0)))
661 /* A - B -> A + (-B) if B is easily negatable. */
663 (minus @0 negate_expr_p@1)
664 (if (!FIXED_POINT_TYPE_P (type))
665 (plus @0 (negate @1))))
667 /* Try to fold (type) X op CST -> (type) (X op ((type-x) CST))
669 For bitwise binary operations apply operand conversions to the
670 binary operation result instead of to the operands. This allows
671 to combine successive conversions and bitwise binary operations.
672 We combine the above two cases by using a conditional convert. */
673 (for bitop (bit_and bit_ior bit_xor)
675 (bitop (convert @0) (convert? @1))
676 (if (((TREE_CODE (@1) == INTEGER_CST
677 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
678 && int_fits_type_p (@1, TREE_TYPE (@0)))
679 || types_match (@0, @1))
680 /* ??? This transform conflicts with fold-const.c doing
681 Convert (T)(x & c) into (T)x & (T)c, if c is an integer
682 constants (if x has signed type, the sign bit cannot be set
683 in c). This folds extension into the BIT_AND_EXPR.
684 Restrict it to GIMPLE to avoid endless recursions. */
685 && (bitop != BIT_AND_EXPR || GIMPLE)
686 && (/* That's a good idea if the conversion widens the operand, thus
687 after hoisting the conversion the operation will be narrower. */
688 TYPE_PRECISION (TREE_TYPE (@0)) < TYPE_PRECISION (type)
689 /* It's also a good idea if the conversion is to a non-integer
691 || GET_MODE_CLASS (TYPE_MODE (type)) != MODE_INT
692 /* Or if the precision of TO is not the same as the precision
694 || TYPE_PRECISION (type) != GET_MODE_PRECISION (TYPE_MODE (type))))
695 (convert (bitop @0 (convert @1))))))
697 (for bitop (bit_and bit_ior)
698 rbitop (bit_ior bit_and)
699 /* (x | y) & x -> x */
700 /* (x & y) | x -> x */
702 (bitop:c (rbitop:c @0 @1) @0)
704 /* (~x | y) & x -> x & y */
705 /* (~x & y) | x -> x | y */
707 (bitop:c (rbitop:c (bit_not @0) @1) @0)
710 /* Simplify (A & B) OP0 (C & B) to (A OP0 C) & B. */
711 (for bitop (bit_and bit_ior bit_xor)
713 (bitop (bit_and:c @0 @1) (bit_and @2 @1))
714 (bit_and (bitop @0 @2) @1)))
716 /* (x | CST1) & CST2 -> (x & CST2) | (CST1 & CST2) */
718 (bit_and (bit_ior @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
719 (bit_ior (bit_and @0 @2) (bit_and @1 @2)))
721 /* Combine successive equal operations with constants. */
722 (for bitop (bit_and bit_ior bit_xor)
724 (bitop (bitop @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
725 (bitop @0 (bitop @1 @2))))
727 /* Try simple folding for X op !X, and X op X with the help
728 of the truth_valued_p and logical_inverted_value predicates. */
729 (match truth_valued_p
731 (if (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1)))
732 (for op (tcc_comparison truth_and truth_andif truth_or truth_orif truth_xor)
733 (match truth_valued_p
735 (match truth_valued_p
738 (match (logical_inverted_value @0)
740 (match (logical_inverted_value @0)
741 (bit_not truth_valued_p@0))
742 (match (logical_inverted_value @0)
743 (eq @0 integer_zerop))
744 (match (logical_inverted_value @0)
745 (ne truth_valued_p@0 integer_truep))
746 (match (logical_inverted_value @0)
747 (bit_xor truth_valued_p@0 integer_truep))
751 (bit_and:c @0 (logical_inverted_value @0))
752 { build_zero_cst (type); })
753 /* X | !X and X ^ !X -> 1, , if X is truth-valued. */
754 (for op (bit_ior bit_xor)
756 (op:c truth_valued_p@0 (logical_inverted_value @0))
757 { constant_boolean_node (true, type); }))
758 /* X ==/!= !X is false/true. */
761 (op:c truth_valued_p@0 (logical_inverted_value @0))
762 { constant_boolean_node (op == NE_EXPR ? true : false, type); }))
764 /* If arg1 and arg2 are booleans (or any single bit type)
765 then try to simplify:
772 But only do this if our result feeds into a comparison as
773 this transformation is not always a win, particularly on
774 targets with and-not instructions.
775 -> simplify_bitwise_binary_boolean */
777 (ne (bit_and:c (bit_not @0) @1) integer_zerop)
778 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
779 && TYPE_PRECISION (TREE_TYPE (@1)) == 1)
782 (ne (bit_ior:c (bit_not @0) @1) integer_zerop)
783 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
784 && TYPE_PRECISION (TREE_TYPE (@1)) == 1)
789 (bit_not (bit_not @0))
792 /* Convert ~ (-A) to A - 1. */
794 (bit_not (convert? (negate @0)))
795 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
796 (convert (minus @0 { build_each_one_cst (TREE_TYPE (@0)); }))))
798 /* Convert ~ (A - 1) or ~ (A + -1) to -A. */
800 (bit_not (convert? (minus @0 integer_each_onep)))
801 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
802 (convert (negate @0))))
804 (bit_not (convert? (plus @0 integer_all_onesp)))
805 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
806 (convert (negate @0))))
808 /* Part of convert ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify. */
810 (bit_not (convert? (bit_xor @0 INTEGER_CST@1)))
811 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
812 (convert (bit_xor @0 (bit_not @1)))))
814 (bit_not (convert? (bit_xor:c (bit_not @0) @1)))
815 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
816 (convert (bit_xor @0 @1))))
818 /* (x & ~m) | (y & m) -> ((x ^ y) & m) ^ x */
820 (bit_ior:c (bit_and:cs @0 (bit_not @2)) (bit_and:cs @1 @2))
821 (bit_xor (bit_and (bit_xor @0 @1) @2) @0))
823 /* Fold A - (A & B) into ~B & A. */
825 (minus (convert? @0) (convert?:s (bit_and:cs @0 @1)))
826 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
827 && tree_nop_conversion_p (type, TREE_TYPE (@1)))
828 (convert (bit_and (bit_not @1) @0))))
832 /* ((X inner_op C0) outer_op C1)
833 With X being a tree where value_range has reasoned certain bits to always be
834 zero throughout its computed value range,
835 inner_op = {|,^}, outer_op = {|,^} and inner_op != outer_op
836 where zero_mask has 1's for all bits that are sure to be 0 in
838 if (inner_op == '^') C0 &= ~C1;
839 if ((C0 & ~zero_mask) == 0) then emit (X outer_op (C0 outer_op C1)
840 if ((C1 & ~zero_mask) == 0) then emit (X inner_op (C0 outer_op C1)
842 (for inner_op (bit_ior bit_xor)
843 outer_op (bit_xor bit_ior)
846 (inner_op:s @2 INTEGER_CST@0) INTEGER_CST@1)
850 wide_int zero_mask_not;
854 if (TREE_CODE (@2) == SSA_NAME)
855 zero_mask_not = get_nonzero_bits (@2);
859 if (inner_op == BIT_XOR_EXPR)
861 C0 = wi::bit_and_not (@0, @1);
862 cst_emit = wi::bit_or (C0, @1);
867 cst_emit = wi::bit_xor (@0, @1);
870 (if (!fail && wi::bit_and (C0, zero_mask_not) == 0)
871 (outer_op @2 { wide_int_to_tree (type, cst_emit); })
872 (if (!fail && wi::bit_and (@1, zero_mask_not) == 0)
873 (inner_op @2 { wide_int_to_tree (type, cst_emit); }))))))
875 /* Associate (p +p off1) +p off2 as (p +p (off1 + off2)). */
877 (pointer_plus (pointer_plus:s @0 @1) @3)
878 (pointer_plus @0 (plus @1 @3)))
884 tem4 = (unsigned long) tem3;
889 (pointer_plus @0 (convert?@2 (minus@3 (convert @1) (convert @0))))
890 /* Conditionally look through a sign-changing conversion. */
891 (if (TYPE_PRECISION (TREE_TYPE (@2)) == TYPE_PRECISION (TREE_TYPE (@3))
892 && ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@1)))
893 || (GENERIC && type == TREE_TYPE (@1))))
897 tem = (sizetype) ptr;
901 and produce the simpler and easier to analyze with respect to alignment
902 ... = ptr & ~algn; */
904 (pointer_plus @0 (negate (bit_and (convert @0) INTEGER_CST@1)))
905 (with { tree algn = wide_int_to_tree (TREE_TYPE (@0), wi::bit_not (@1)); }
906 (bit_and @0 { algn; })))
908 /* Try folding difference of addresses. */
910 (minus (convert ADDR_EXPR@0) (convert @1))
911 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
912 (with { HOST_WIDE_INT diff; }
913 (if (ptr_difference_const (@0, @1, &diff))
914 { build_int_cst_type (type, diff); }))))
916 (minus (convert @0) (convert ADDR_EXPR@1))
917 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
918 (with { HOST_WIDE_INT diff; }
919 (if (ptr_difference_const (@0, @1, &diff))
920 { build_int_cst_type (type, diff); }))))
922 /* If arg0 is derived from the address of an object or function, we may
923 be able to fold this expression using the object or function's
926 (bit_and (convert? @0) INTEGER_CST@1)
927 (if (POINTER_TYPE_P (TREE_TYPE (@0))
928 && tree_nop_conversion_p (type, TREE_TYPE (@0)))
932 unsigned HOST_WIDE_INT bitpos;
933 get_pointer_alignment_1 (@0, &align, &bitpos);
935 (if (wi::ltu_p (@1, align / BITS_PER_UNIT))
936 { wide_int_to_tree (type, wi::bit_and (@1, bitpos / BITS_PER_UNIT)); }))))
939 /* We can't reassociate at all for saturating types. */
940 (if (!TYPE_SATURATING (type))
942 /* Contract negates. */
943 /* A + (-B) -> A - B */
945 (plus:c (convert1? @0) (convert2? (negate @1)))
946 /* Apply STRIP_NOPS on @0 and the negate. */
947 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
948 && tree_nop_conversion_p (type, TREE_TYPE (@1))
949 && !TYPE_OVERFLOW_SANITIZED (type))
950 (minus (convert @0) (convert @1))))
951 /* A - (-B) -> A + B */
953 (minus (convert1? @0) (convert2? (negate @1)))
954 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
955 && tree_nop_conversion_p (type, TREE_TYPE (@1))
956 && !TYPE_OVERFLOW_SANITIZED (type))
957 (plus (convert @0) (convert @1))))
960 (negate (convert? (negate @1)))
961 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
962 && !TYPE_OVERFLOW_SANITIZED (type))
965 /* We can't reassociate floating-point unless -fassociative-math
966 or fixed-point plus or minus because of saturation to +-Inf. */
967 (if ((!FLOAT_TYPE_P (type) || flag_associative_math)
968 && !FIXED_POINT_TYPE_P (type))
970 /* Match patterns that allow contracting a plus-minus pair
971 irrespective of overflow issues. */
972 /* (A +- B) - A -> +- B */
973 /* (A +- B) -+ B -> A */
974 /* A - (A +- B) -> -+ B */
975 /* A +- (B -+ A) -> +- B */
977 (minus (plus:c @0 @1) @0)
980 (minus (minus @0 @1) @0)
983 (plus:c (minus @0 @1) @1)
986 (minus @0 (plus:c @0 @1))
989 (minus @0 (minus @0 @1))
992 /* (A +- CST) +- CST -> A + CST */
993 (for outer_op (plus minus)
994 (for inner_op (plus minus)
996 (outer_op (inner_op @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
997 /* If the constant operation overflows we cannot do the transform
998 as we would introduce undefined overflow, for example
999 with (a - 1) + INT_MIN. */
1000 (with { tree cst = fold_binary (outer_op == inner_op
1001 ? PLUS_EXPR : MINUS_EXPR, type, @1, @2); }
1002 (if (cst && !TREE_OVERFLOW (cst))
1003 (inner_op @0 { cst; } ))))))
1005 /* (CST - A) +- CST -> CST - A */
1006 (for outer_op (plus minus)
1008 (outer_op (minus CONSTANT_CLASS_P@1 @0) CONSTANT_CLASS_P@2)
1009 (with { tree cst = fold_binary (outer_op, type, @1, @2); }
1010 (if (cst && !TREE_OVERFLOW (cst))
1011 (minus { cst; } @0)))))
1015 (plus:c (bit_not @0) @0)
1016 (if (!TYPE_OVERFLOW_TRAPS (type))
1017 { build_all_ones_cst (type); }))
1021 (plus (convert? (bit_not @0)) integer_each_onep)
1022 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1023 (negate (convert @0))))
1027 (minus (convert? (negate @0)) integer_each_onep)
1028 (if (!TYPE_OVERFLOW_TRAPS (type)
1029 && tree_nop_conversion_p (type, TREE_TYPE (@0)))
1030 (bit_not (convert @0))))
1034 (minus integer_all_onesp @0)
1037 /* (T)(P + A) - (T)P -> (T) A */
1038 (for add (plus pointer_plus)
1040 (minus (convert (add @0 @1))
1042 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
1043 /* For integer types, if A has a smaller type
1044 than T the result depends on the possible
1046 E.g. T=size_t, A=(unsigned)429497295, P>0.
1047 However, if an overflow in P + A would cause
1048 undefined behavior, we can assume that there
1050 || (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1051 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1052 /* For pointer types, if the conversion of A to the
1053 final type requires a sign- or zero-extension,
1054 then we have to punt - it is not defined which
1056 || (POINTER_TYPE_P (TREE_TYPE (@0))
1057 && TREE_CODE (@1) == INTEGER_CST
1058 && tree_int_cst_sign_bit (@1) == 0))
1061 /* (T)P - (T)(P + A) -> -(T) A */
1062 (for add (plus pointer_plus)
1065 (convert (add @0 @1)))
1066 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
1067 /* For integer types, if A has a smaller type
1068 than T the result depends on the possible
1070 E.g. T=size_t, A=(unsigned)429497295, P>0.
1071 However, if an overflow in P + A would cause
1072 undefined behavior, we can assume that there
1074 || (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1075 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1076 /* For pointer types, if the conversion of A to the
1077 final type requires a sign- or zero-extension,
1078 then we have to punt - it is not defined which
1080 || (POINTER_TYPE_P (TREE_TYPE (@0))
1081 && TREE_CODE (@1) == INTEGER_CST
1082 && tree_int_cst_sign_bit (@1) == 0))
1083 (negate (convert @1)))))
1085 /* (T)(P + A) - (T)(P + B) -> (T)A - (T)B */
1086 (for add (plus pointer_plus)
1088 (minus (convert (add @0 @1))
1089 (convert (add @0 @2)))
1090 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
1091 /* For integer types, if A has a smaller type
1092 than T the result depends on the possible
1094 E.g. T=size_t, A=(unsigned)429497295, P>0.
1095 However, if an overflow in P + A would cause
1096 undefined behavior, we can assume that there
1098 || (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1099 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1100 /* For pointer types, if the conversion of A to the
1101 final type requires a sign- or zero-extension,
1102 then we have to punt - it is not defined which
1104 || (POINTER_TYPE_P (TREE_TYPE (@0))
1105 && TREE_CODE (@1) == INTEGER_CST
1106 && tree_int_cst_sign_bit (@1) == 0
1107 && TREE_CODE (@2) == INTEGER_CST
1108 && tree_int_cst_sign_bit (@2) == 0))
1109 (minus (convert @1) (convert @2)))))))
1112 /* Simplifications of MIN_EXPR and MAX_EXPR. */
1114 (for minmax (min max)
1120 (if (INTEGRAL_TYPE_P (type)
1121 && TYPE_MIN_VALUE (type)
1122 && operand_equal_p (@1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
1126 (if (INTEGRAL_TYPE_P (type)
1127 && TYPE_MAX_VALUE (type)
1128 && operand_equal_p (@1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
1132 /* Simplifications of shift and rotates. */
1134 (for rotate (lrotate rrotate)
1136 (rotate integer_all_onesp@0 @1)
1139 /* Optimize -1 >> x for arithmetic right shifts. */
1141 (rshift integer_all_onesp@0 @1)
1142 (if (!TYPE_UNSIGNED (type)
1143 && tree_expr_nonnegative_p (@1))
1146 /* Optimize (x >> c) << c into x & (-1<<c). */
1148 (lshift (rshift @0 INTEGER_CST@1) @1)
1149 (if (wi::ltu_p (@1, element_precision (type)))
1150 (bit_and @0 (lshift { build_minus_one_cst (type); } @1))))
1152 /* Optimize (x << c) >> c into x & ((unsigned)-1 >> c) for unsigned
1155 (rshift (lshift @0 INTEGER_CST@1) @1)
1156 (if (TYPE_UNSIGNED (type)
1157 && (wi::ltu_p (@1, element_precision (type))))
1158 (bit_and @0 (rshift { build_minus_one_cst (type); } @1))))
1160 (for shiftrotate (lrotate rrotate lshift rshift)
1162 (shiftrotate @0 integer_zerop)
1165 (shiftrotate integer_zerop@0 @1)
1167 /* Prefer vector1 << scalar to vector1 << vector2
1168 if vector2 is uniform. */
1169 (for vec (VECTOR_CST CONSTRUCTOR)
1171 (shiftrotate @0 vec@1)
1172 (with { tree tem = uniform_vector_p (@1); }
1174 (shiftrotate @0 { tem; }))))))
1176 /* Rewrite an LROTATE_EXPR by a constant into an
1177 RROTATE_EXPR by a new constant. */
1179 (lrotate @0 INTEGER_CST@1)
1180 (rrotate @0 { fold_binary (MINUS_EXPR, TREE_TYPE (@1),
1181 build_int_cst (TREE_TYPE (@1),
1182 element_precision (type)), @1); }))
1184 /* Turn (a OP c1) OP c2 into a OP (c1+c2). */
1185 (for op (lrotate rrotate rshift lshift)
1187 (op (op @0 INTEGER_CST@1) INTEGER_CST@2)
1188 (with { unsigned int prec = element_precision (type); }
1189 (if (wi::ge_p (@1, 0, TYPE_SIGN (TREE_TYPE (@1)))
1190 && wi::lt_p (@1, prec, TYPE_SIGN (TREE_TYPE (@1)))
1191 && wi::ge_p (@2, 0, TYPE_SIGN (TREE_TYPE (@2)))
1192 && wi::lt_p (@2, prec, TYPE_SIGN (TREE_TYPE (@2))))
1193 (with { unsigned int low = wi::add (@1, @2).to_uhwi (); }
1194 /* Deal with a OP (c1 + c2) being undefined but (a OP c1) OP c2
1195 being well defined. */
1197 (if (op == LROTATE_EXPR || op == RROTATE_EXPR)
1198 (op @0 { build_int_cst (TREE_TYPE (@1), low % prec); })
1199 (if (TYPE_UNSIGNED (type) || op == LSHIFT_EXPR)
1200 { build_zero_cst (type); }
1201 (op @0 { build_int_cst (TREE_TYPE (@1), prec - 1); })))
1202 (op @0 { build_int_cst (TREE_TYPE (@1), low); })))))))
1205 /* ((1 << A) & 1) != 0 -> A == 0
1206 ((1 << A) & 1) == 0 -> A != 0 */
1210 (cmp (bit_and (lshift integer_onep @0) integer_onep) integer_zerop)
1211 (icmp @0 { build_zero_cst (TREE_TYPE (@0)); })))
1213 /* (CST1 << A) == CST2 -> A == ctz (CST2) - ctz (CST1)
1214 (CST1 << A) != CST2 -> A != ctz (CST2) - ctz (CST1)
1218 (cmp (lshift INTEGER_CST@0 @1) INTEGER_CST@2)
1219 (with { int cand = wi::ctz (@2) - wi::ctz (@0); }
1221 || (!integer_zerop (@2)
1222 && wi::ne_p (wi::lshift (@0, cand), @2)))
1223 { constant_boolean_node (cmp == NE_EXPR, type); }
1224 (if (!integer_zerop (@2)
1225 && wi::eq_p (wi::lshift (@0, cand), @2))
1226 (cmp @1 { build_int_cst (TREE_TYPE (@1), cand); }))))))
1228 /* Fold (X << C1) & C2 into (X << C1) & (C2 | ((1 << C1) - 1))
1229 (X >> C1) & C2 into (X >> C1) & (C2 | ~((type) -1 >> C1))
1230 if the new mask might be further optimized. */
1231 (for shift (lshift rshift)
1233 (bit_and (convert?:s@4 (shift:s@5 (convert1?@3 @0) INTEGER_CST@1))
1235 (if (tree_nop_conversion_p (TREE_TYPE (@4), TREE_TYPE (@5))
1236 && TYPE_PRECISION (type) <= HOST_BITS_PER_WIDE_INT
1237 && tree_fits_uhwi_p (@1)
1238 && tree_to_uhwi (@1) > 0
1239 && tree_to_uhwi (@1) < TYPE_PRECISION (type))
1242 unsigned int shiftc = tree_to_uhwi (@1);
1243 unsigned HOST_WIDE_INT mask = TREE_INT_CST_LOW (@2);
1244 unsigned HOST_WIDE_INT newmask, zerobits = 0;
1245 tree shift_type = TREE_TYPE (@3);
1248 if (shift == LSHIFT_EXPR)
1249 zerobits = ((((unsigned HOST_WIDE_INT) 1) << shiftc) - 1);
1250 else if (shift == RSHIFT_EXPR
1251 && (TYPE_PRECISION (shift_type)
1252 == GET_MODE_PRECISION (TYPE_MODE (shift_type))))
1254 prec = TYPE_PRECISION (TREE_TYPE (@3));
1256 /* See if more bits can be proven as zero because of
1259 && TYPE_UNSIGNED (TREE_TYPE (@0)))
1261 tree inner_type = TREE_TYPE (@0);
1262 if ((TYPE_PRECISION (inner_type)
1263 == GET_MODE_PRECISION (TYPE_MODE (inner_type)))
1264 && TYPE_PRECISION (inner_type) < prec)
1266 prec = TYPE_PRECISION (inner_type);
1267 /* See if we can shorten the right shift. */
1269 shift_type = inner_type;
1270 /* Otherwise X >> C1 is all zeros, so we'll optimize
1271 it into (X, 0) later on by making sure zerobits
1275 zerobits = ~(unsigned HOST_WIDE_INT) 0;
1278 zerobits >>= HOST_BITS_PER_WIDE_INT - shiftc;
1279 zerobits <<= prec - shiftc;
1281 /* For arithmetic shift if sign bit could be set, zerobits
1282 can contain actually sign bits, so no transformation is
1283 possible, unless MASK masks them all away. In that
1284 case the shift needs to be converted into logical shift. */
1285 if (!TYPE_UNSIGNED (TREE_TYPE (@3))
1286 && prec == TYPE_PRECISION (TREE_TYPE (@3)))
1288 if ((mask & zerobits) == 0)
1289 shift_type = unsigned_type_for (TREE_TYPE (@3));
1295 /* ((X << 16) & 0xff00) is (X, 0). */
1296 (if ((mask & zerobits) == mask)
1297 { build_int_cst (type, 0); }
1298 (with { newmask = mask | zerobits; }
1299 (if (newmask != mask && (newmask & (newmask + 1)) == 0)
1302 /* Only do the transformation if NEWMASK is some integer
1304 for (prec = BITS_PER_UNIT;
1305 prec < HOST_BITS_PER_WIDE_INT; prec <<= 1)
1306 if (newmask == (((unsigned HOST_WIDE_INT) 1) << prec) - 1)
1309 (if (prec < HOST_BITS_PER_WIDE_INT
1310 || newmask == ~(unsigned HOST_WIDE_INT) 0)
1312 { tree newmaskt = build_int_cst_type (TREE_TYPE (@2), newmask); }
1313 (if (!tree_int_cst_equal (newmaskt, @2))
1314 (if (shift_type != TREE_TYPE (@3))
1315 (bit_and (convert (shift:shift_type (convert @3) @1)) { newmaskt; })
1316 (bit_and @4 { newmaskt; })))))))))))))
1318 /* Fold (X {&,^,|} C2) << C1 into (X << C1) {&,^,|} (C2 << C1)
1319 (X {&,^,|} C2) >> C1 into (X >> C1) & (C2 >> C1). */
1320 (for shift (lshift rshift)
1321 (for bit_op (bit_and bit_xor bit_ior)
1323 (shift (convert?:s (bit_op:s @0 INTEGER_CST@2)) INTEGER_CST@1)
1324 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1325 (with { tree mask = int_const_binop (shift, fold_convert (type, @2), @1); }
1326 (bit_op (shift (convert @0) @1) { mask; }))))))
1329 /* Simplifications of conversions. */
1331 /* Basic strip-useless-type-conversions / strip_nops. */
1332 (for cvt (convert view_convert float fix_trunc)
1335 (if ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@0)))
1336 || (GENERIC && type == TREE_TYPE (@0)))
1339 /* Contract view-conversions. */
1341 (view_convert (view_convert @0))
1344 /* For integral conversions with the same precision or pointer
1345 conversions use a NOP_EXPR instead. */
1348 (if ((INTEGRAL_TYPE_P (type) || POINTER_TYPE_P (type))
1349 && (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
1350 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (@0)))
1353 /* Strip inner integral conversions that do not change precision or size. */
1355 (view_convert (convert@0 @1))
1356 (if ((INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
1357 && (INTEGRAL_TYPE_P (TREE_TYPE (@1)) || POINTER_TYPE_P (TREE_TYPE (@1)))
1358 && (TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
1359 && (TYPE_SIZE (TREE_TYPE (@0)) == TYPE_SIZE (TREE_TYPE (@1))))
1362 /* Re-association barriers around constants and other re-association
1363 barriers can be removed. */
1365 (paren CONSTANT_CLASS_P@0)
1368 (paren (paren@1 @0))
1371 /* Handle cases of two conversions in a row. */
1372 (for ocvt (convert float fix_trunc)
1373 (for icvt (convert float)
1378 tree inside_type = TREE_TYPE (@0);
1379 tree inter_type = TREE_TYPE (@1);
1380 int inside_int = INTEGRAL_TYPE_P (inside_type);
1381 int inside_ptr = POINTER_TYPE_P (inside_type);
1382 int inside_float = FLOAT_TYPE_P (inside_type);
1383 int inside_vec = VECTOR_TYPE_P (inside_type);
1384 unsigned int inside_prec = TYPE_PRECISION (inside_type);
1385 int inside_unsignedp = TYPE_UNSIGNED (inside_type);
1386 int inter_int = INTEGRAL_TYPE_P (inter_type);
1387 int inter_ptr = POINTER_TYPE_P (inter_type);
1388 int inter_float = FLOAT_TYPE_P (inter_type);
1389 int inter_vec = VECTOR_TYPE_P (inter_type);
1390 unsigned int inter_prec = TYPE_PRECISION (inter_type);
1391 int inter_unsignedp = TYPE_UNSIGNED (inter_type);
1392 int final_int = INTEGRAL_TYPE_P (type);
1393 int final_ptr = POINTER_TYPE_P (type);
1394 int final_float = FLOAT_TYPE_P (type);
1395 int final_vec = VECTOR_TYPE_P (type);
1396 unsigned int final_prec = TYPE_PRECISION (type);
1397 int final_unsignedp = TYPE_UNSIGNED (type);
1400 /* In addition to the cases of two conversions in a row
1401 handled below, if we are converting something to its own
1402 type via an object of identical or wider precision, neither
1403 conversion is needed. */
1404 (if (((GIMPLE && useless_type_conversion_p (type, inside_type))
1406 && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (inside_type)))
1407 && (((inter_int || inter_ptr) && final_int)
1408 || (inter_float && final_float))
1409 && inter_prec >= final_prec)
1412 /* Likewise, if the intermediate and initial types are either both
1413 float or both integer, we don't need the middle conversion if the
1414 former is wider than the latter and doesn't change the signedness
1415 (for integers). Avoid this if the final type is a pointer since
1416 then we sometimes need the middle conversion. Likewise if the
1417 final type has a precision not equal to the size of its mode. */
1418 (if (((inter_int && inside_int) || (inter_float && inside_float))
1419 && (final_int || final_float)
1420 && inter_prec >= inside_prec
1421 && (inter_float || inter_unsignedp == inside_unsignedp)
1422 && ! (final_prec != GET_MODE_PRECISION (TYPE_MODE (type))
1423 && TYPE_MODE (type) == TYPE_MODE (inter_type)))
1426 /* If we have a sign-extension of a zero-extended value, we can
1427 replace that by a single zero-extension. Likewise if the
1428 final conversion does not change precision we can drop the
1429 intermediate conversion. */
1430 (if (inside_int && inter_int && final_int
1431 && ((inside_prec < inter_prec && inter_prec < final_prec
1432 && inside_unsignedp && !inter_unsignedp)
1433 || final_prec == inter_prec))
1436 /* Two conversions in a row are not needed unless:
1437 - some conversion is floating-point (overstrict for now), or
1438 - some conversion is a vector (overstrict for now), or
1439 - the intermediate type is narrower than both initial and
1441 - the intermediate type and innermost type differ in signedness,
1442 and the outermost type is wider than the intermediate, or
1443 - the initial type is a pointer type and the precisions of the
1444 intermediate and final types differ, or
1445 - the final type is a pointer type and the precisions of the
1446 initial and intermediate types differ. */
1447 (if (! inside_float && ! inter_float && ! final_float
1448 && ! inside_vec && ! inter_vec && ! final_vec
1449 && (inter_prec >= inside_prec || inter_prec >= final_prec)
1450 && ! (inside_int && inter_int
1451 && inter_unsignedp != inside_unsignedp
1452 && inter_prec < final_prec)
1453 && ((inter_unsignedp && inter_prec > inside_prec)
1454 == (final_unsignedp && final_prec > inter_prec))
1455 && ! (inside_ptr && inter_prec != final_prec)
1456 && ! (final_ptr && inside_prec != inter_prec)
1457 && ! (final_prec != GET_MODE_PRECISION (TYPE_MODE (type))
1458 && TYPE_MODE (type) == TYPE_MODE (inter_type)))
1461 /* A truncation to an unsigned type (a zero-extension) should be
1462 canonicalized as bitwise and of a mask. */
1463 (if (final_int && inter_int && inside_int
1464 && final_prec == inside_prec
1465 && final_prec > inter_prec
1467 (convert (bit_and @0 { wide_int_to_tree
1469 wi::mask (inter_prec, false,
1470 TYPE_PRECISION (inside_type))); })))
1472 /* If we are converting an integer to a floating-point that can
1473 represent it exactly and back to an integer, we can skip the
1474 floating-point conversion. */
1475 (if (GIMPLE /* PR66211 */
1476 && inside_int && inter_float && final_int &&
1477 (unsigned) significand_size (TYPE_MODE (inter_type))
1478 >= inside_prec - !inside_unsignedp)
1481 /* If we have a narrowing conversion to an integral type that is fed by a
1482 BIT_AND_EXPR, we might be able to remove the BIT_AND_EXPR if it merely
1483 masks off bits outside the final type (and nothing else). */
1485 (convert (bit_and @0 INTEGER_CST@1))
1486 (if (INTEGRAL_TYPE_P (type)
1487 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
1488 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))
1489 && operand_equal_p (@1, build_low_bits_mask (TREE_TYPE (@1),
1490 TYPE_PRECISION (type)), 0))
1494 /* (X /[ex] A) * A -> X. */
1496 (mult (convert? (exact_div @0 @1)) @1)
1497 /* Look through a sign-changing conversion. */
1500 /* Canonicalization of binary operations. */
1502 /* Convert X + -C into X - C. */
1504 (plus @0 REAL_CST@1)
1505 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
1506 (with { tree tem = fold_unary (NEGATE_EXPR, type, @1); }
1507 (if (!TREE_OVERFLOW (tem) || !flag_trapping_math)
1508 (minus @0 { tem; })))))
1510 /* Convert x+x into x*2.0. */
1513 (if (SCALAR_FLOAT_TYPE_P (type))
1514 (mult @0 { build_real (type, dconst2); })))
1517 (minus integer_zerop @1)
1520 /* (ARG0 - ARG1) is the same as (-ARG1 + ARG0). So check whether
1521 ARG0 is zero and X + ARG0 reduces to X, since that would mean
1522 (-ARG1 + ARG0) reduces to -ARG1. */
1524 (minus real_zerop@0 @1)
1525 (if (fold_real_zero_addition_p (type, @0, 0))
1528 /* Transform x * -1 into -x. */
1530 (mult @0 integer_minus_onep)
1533 /* COMPLEX_EXPR and REALPART/IMAGPART_EXPR cancellations. */
1535 (complex (realpart @0) (imagpart @0))
1538 (realpart (complex @0 @1))
1541 (imagpart (complex @0 @1))
1544 /* Sometimes we only care about half of a complex expression. */
1546 (realpart (convert?:s (conj:s @0)))
1547 (convert (realpart @0)))
1549 (imagpart (convert?:s (conj:s @0)))
1550 (convert (negate (imagpart @0))))
1551 (for part (realpart imagpart)
1552 (for op (plus minus)
1554 (part (convert?:s@2 (op:s @0 @1)))
1555 (convert (op (part @0) (part @1))))))
1557 (realpart (convert?:s (CEXPI:s @0)))
1560 (imagpart (convert?:s (CEXPI:s @0)))
1563 /* conj(conj(x)) -> x */
1565 (conj (convert? (conj @0)))
1566 (if (tree_nop_conversion_p (TREE_TYPE (@0), type))
1569 /* conj({x,y}) -> {x,-y} */
1571 (conj (convert?:s (complex:s @0 @1)))
1572 (with { tree itype = TREE_TYPE (type); }
1573 (complex (convert:itype @0) (negate (convert:itype @1)))))
1575 /* BSWAP simplifications, transforms checked by gcc.dg/builtin-bswap-8.c. */
1576 (for bswap (BUILT_IN_BSWAP16 BUILT_IN_BSWAP32 BUILT_IN_BSWAP64)
1581 (bswap (bit_not (bswap @0)))
1583 (for bitop (bit_xor bit_ior bit_and)
1585 (bswap (bitop:c (bswap @0) @1))
1586 (bitop @0 (bswap @1)))))
1589 /* Combine COND_EXPRs and VEC_COND_EXPRs. */
1591 /* Simplify constant conditions.
1592 Only optimize constant conditions when the selected branch
1593 has the same type as the COND_EXPR. This avoids optimizing
1594 away "c ? x : throw", where the throw has a void type.
1595 Note that we cannot throw away the fold-const.c variant nor
1596 this one as we depend on doing this transform before possibly
1597 A ? B : B -> B triggers and the fold-const.c one can optimize
1598 0 ? A : B to B even if A has side-effects. Something
1599 genmatch cannot handle. */
1601 (cond INTEGER_CST@0 @1 @2)
1602 (if (integer_zerop (@0))
1603 (if (!VOID_TYPE_P (TREE_TYPE (@2)) || VOID_TYPE_P (type))
1605 (if (!VOID_TYPE_P (TREE_TYPE (@1)) || VOID_TYPE_P (type))
1608 (vec_cond VECTOR_CST@0 @1 @2)
1609 (if (integer_all_onesp (@0))
1611 (if (integer_zerop (@0))
1614 (for cnd (cond vec_cond)
1615 /* A ? B : (A ? X : C) -> A ? B : C. */
1617 (cnd @0 (cnd @0 @1 @2) @3)
1620 (cnd @0 @1 (cnd @0 @2 @3))
1623 /* A ? B : B -> B. */
1628 /* !A ? B : C -> A ? C : B. */
1630 (cnd (logical_inverted_value truth_valued_p@0) @1 @2)
1633 /* A + (B vcmp C ? 1 : 0) -> A - (B vcmp C), since vector comparisons
1634 return all-1 or all-0 results. */
1635 /* ??? We could instead convert all instances of the vec_cond to negate,
1636 but that isn't necessarily a win on its own. */
1638 (plus:c @3 (view_convert? (vec_cond @0 integer_each_onep@1 integer_zerop@2)))
1639 (if (VECTOR_TYPE_P (type)
1640 && TYPE_VECTOR_SUBPARTS (type) == TYPE_VECTOR_SUBPARTS (TREE_TYPE (@0))
1641 && (TYPE_MODE (TREE_TYPE (type))
1642 == TYPE_MODE (TREE_TYPE (TREE_TYPE (@0)))))
1643 (minus @3 (view_convert @0))))
1645 /* ... likewise A - (B vcmp C ? 1 : 0) -> A + (B vcmp C). */
1647 (minus @3 (view_convert? (vec_cond @0 integer_each_onep@1 integer_zerop@2)))
1648 (if (VECTOR_TYPE_P (type)
1649 && TYPE_VECTOR_SUBPARTS (type) == TYPE_VECTOR_SUBPARTS (TREE_TYPE (@0))
1650 && (TYPE_MODE (TREE_TYPE (type))
1651 == TYPE_MODE (TREE_TYPE (TREE_TYPE (@0)))))
1652 (plus @3 (view_convert @0))))
1655 /* Simplifications of comparisons. */
1657 /* See if we can reduce the magnitude of a constant involved in a
1658 comparison by changing the comparison code. This is a canonicalization
1659 formerly done by maybe_canonicalize_comparison_1. */
1663 (cmp @0 INTEGER_CST@1)
1664 (if (tree_int_cst_sgn (@1) == -1)
1665 (acmp @0 { wide_int_to_tree (TREE_TYPE (@1), wi::add (@1, 1)); }))))
1669 (cmp @0 INTEGER_CST@1)
1670 (if (tree_int_cst_sgn (@1) == 1)
1671 (acmp @0 { wide_int_to_tree (TREE_TYPE (@1), wi::sub (@1, 1)); }))))
1674 /* We can simplify a logical negation of a comparison to the
1675 inverted comparison. As we cannot compute an expression
1676 operator using invert_tree_comparison we have to simulate
1677 that with expression code iteration. */
1678 (for cmp (tcc_comparison)
1679 icmp (inverted_tcc_comparison)
1680 ncmp (inverted_tcc_comparison_with_nans)
1681 /* Ideally we'd like to combine the following two patterns
1682 and handle some more cases by using
1683 (logical_inverted_value (cmp @0 @1))
1684 here but for that genmatch would need to "inline" that.
1685 For now implement what forward_propagate_comparison did. */
1687 (bit_not (cmp @0 @1))
1688 (if (VECTOR_TYPE_P (type)
1689 || (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1))
1690 /* Comparison inversion may be impossible for trapping math,
1691 invert_tree_comparison will tell us. But we can't use
1692 a computed operator in the replacement tree thus we have
1693 to play the trick below. */
1694 (with { enum tree_code ic = invert_tree_comparison
1695 (cmp, HONOR_NANS (@0)); }
1701 (bit_xor (cmp @0 @1) integer_truep)
1702 (with { enum tree_code ic = invert_tree_comparison
1703 (cmp, HONOR_NANS (@0)); }
1709 /* Transform comparisons of the form X - Y CMP 0 to X CMP Y.
1710 ??? The transformation is valid for the other operators if overflow
1711 is undefined for the type, but performing it here badly interacts
1712 with the transformation in fold_cond_expr_with_comparison which
1713 attempts to synthetize ABS_EXPR. */
1716 (cmp (minus@2 @0 @1) integer_zerop)
1717 (if (single_use (@2))
1720 /* Transform comparisons of the form X * C1 CMP 0 to X CMP 0 in the
1721 signed arithmetic case. That form is created by the compiler
1722 often enough for folding it to be of value. One example is in
1723 computing loop trip counts after Operator Strength Reduction. */
1724 (for cmp (simple_comparison)
1725 scmp (swapped_simple_comparison)
1727 (cmp (mult @0 INTEGER_CST@1) integer_zerop@2)
1728 /* Handle unfolded multiplication by zero. */
1729 (if (integer_zerop (@1))
1731 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1732 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1733 /* If @1 is negative we swap the sense of the comparison. */
1734 (if (tree_int_cst_sgn (@1) < 0)
1738 /* Simplify comparison of something with itself. For IEEE
1739 floating-point, we can only do some of these simplifications. */
1742 (if (! FLOAT_TYPE_P (TREE_TYPE (@0))
1743 || ! HONOR_NANS (TYPE_MODE (TREE_TYPE (@0))))
1744 { constant_boolean_node (true, type); }))
1753 || ! FLOAT_TYPE_P (TREE_TYPE (@0))
1754 || ! HONOR_NANS (TYPE_MODE (TREE_TYPE (@0))))
1755 { constant_boolean_node (false, type); })))
1756 (for cmp (unle unge uneq)
1759 { constant_boolean_node (true, type); }))
1762 (if (!flag_trapping_math)
1763 { constant_boolean_node (false, type); }))
1765 /* Fold ~X op ~Y as Y op X. */
1766 (for cmp (simple_comparison)
1768 (cmp (bit_not @0) (bit_not @1))
1771 /* Fold ~X op C as X op' ~C, where op' is the swapped comparison. */
1772 (for cmp (simple_comparison)
1773 scmp (swapped_simple_comparison)
1775 (cmp (bit_not @0) CONSTANT_CLASS_P@1)
1776 (if (TREE_CODE (@1) == INTEGER_CST || TREE_CODE (@1) == VECTOR_CST)
1777 (scmp @0 (bit_not @1)))))
1779 (for cmp (simple_comparison)
1780 /* Fold (double)float1 CMP (double)float2 into float1 CMP float2. */
1782 (cmp (convert@2 @0) (convert? @1))
1783 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
1784 && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@2))
1785 == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0)))
1786 && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@2))
1787 == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1))))
1790 tree type1 = TREE_TYPE (@1);
1791 if (TREE_CODE (@1) == REAL_CST && !DECIMAL_FLOAT_TYPE_P (type1))
1793 REAL_VALUE_TYPE orig = TREE_REAL_CST (@1);
1794 if (TYPE_PRECISION (type1) > TYPE_PRECISION (float_type_node)
1795 && exact_real_truncate (TYPE_MODE (float_type_node), &orig))
1796 type1 = float_type_node;
1797 if (TYPE_PRECISION (type1) > TYPE_PRECISION (double_type_node)
1798 && exact_real_truncate (TYPE_MODE (double_type_node), &orig))
1799 type1 = double_type_node;
1802 = (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type1)
1803 ? TREE_TYPE (@0) : type1);
1805 (if (TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (newtype))
1806 (cmp (convert:newtype @0) (convert:newtype @1))))))
1810 /* IEEE doesn't distinguish +0 and -0 in comparisons. */
1812 /* a CMP (-0) -> a CMP 0 */
1813 (if (REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@1)))
1814 (cmp @0 { build_real (TREE_TYPE (@1), dconst0); }))
1815 /* x != NaN is always true, other ops are always false. */
1816 (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
1817 && ! HONOR_SNANS (@1))
1818 { constant_boolean_node (cmp == NE_EXPR, type); })
1819 /* Fold comparisons against infinity. */
1820 (if (REAL_VALUE_ISINF (TREE_REAL_CST (@1))
1821 && MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (@1))))
1824 REAL_VALUE_TYPE max;
1825 enum tree_code code = cmp;
1826 bool neg = REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1));
1828 code = swap_tree_comparison (code);
1831 /* x > +Inf is always false, if with ignore sNANs. */
1832 (if (code == GT_EXPR
1833 && ! HONOR_SNANS (@0))
1834 { constant_boolean_node (false, type); })
1835 (if (code == LE_EXPR)
1836 /* x <= +Inf is always true, if we don't case about NaNs. */
1837 (if (! HONOR_NANS (@0))
1838 { constant_boolean_node (true, type); }
1839 /* x <= +Inf is the same as x == x, i.e. !isnan(x). */
1841 /* x == +Inf and x >= +Inf are always equal to x > DBL_MAX. */
1842 (if (code == EQ_EXPR || code == GE_EXPR)
1843 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
1845 (lt @0 { build_real (TREE_TYPE (@0), max); })
1846 (gt @0 { build_real (TREE_TYPE (@0), max); }))))
1847 /* x < +Inf is always equal to x <= DBL_MAX. */
1848 (if (code == LT_EXPR)
1849 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
1851 (ge @0 { build_real (TREE_TYPE (@0), max); })
1852 (le @0 { build_real (TREE_TYPE (@0), max); }))))
1853 /* x != +Inf is always equal to !(x > DBL_MAX). */
1854 (if (code == NE_EXPR)
1855 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
1856 (if (! HONOR_NANS (@0))
1858 (ge @0 { build_real (TREE_TYPE (@0), max); })
1859 (le @0 { build_real (TREE_TYPE (@0), max); }))
1861 (bit_xor (lt @0 { build_real (TREE_TYPE (@0), max); })
1862 { build_one_cst (type); })
1863 (bit_xor (gt @0 { build_real (TREE_TYPE (@0), max); })
1864 { build_one_cst (type); }))))))))))
1866 /* If this is a comparison of a real constant with a PLUS_EXPR
1867 or a MINUS_EXPR of a real constant, we can convert it into a
1868 comparison with a revised real constant as long as no overflow
1869 occurs when unsafe_math_optimizations are enabled. */
1870 (if (flag_unsafe_math_optimizations)
1871 (for op (plus minus)
1873 (cmp (op @0 REAL_CST@1) REAL_CST@2)
1876 tree tem = const_binop (op == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR,
1877 TREE_TYPE (@1), @2, @1);
1879 (if (tem && !TREE_OVERFLOW (tem))
1880 (cmp @0 { tem; }))))))
1882 /* Likewise, we can simplify a comparison of a real constant with
1883 a MINUS_EXPR whose first operand is also a real constant, i.e.
1884 (c1 - x) < c2 becomes x > c1-c2. Reordering is allowed on
1885 floating-point types only if -fassociative-math is set. */
1886 (if (flag_associative_math)
1888 (cmp (minus REAL_CST@0 @1) REAL_CST@2)
1889 (with { tree tem = const_binop (MINUS_EXPR, TREE_TYPE (@1), @0, @2); }
1890 (if (tem && !TREE_OVERFLOW (tem))
1891 (cmp { tem; } @1)))))
1893 /* Fold comparisons against built-in math functions. */
1894 (if (flag_unsafe_math_optimizations
1895 && ! flag_errno_math)
1898 (cmp (sq @0) REAL_CST@1)
1900 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
1902 /* sqrt(x) < y is always false, if y is negative. */
1903 (if (cmp == EQ_EXPR || cmp == LT_EXPR || cmp == LE_EXPR)
1904 { constant_boolean_node (false, type); })
1905 /* sqrt(x) > y is always true, if y is negative and we
1906 don't care about NaNs, i.e. negative values of x. */
1907 (if (cmp == NE_EXPR || !HONOR_NANS (@0))
1908 { constant_boolean_node (true, type); })
1909 /* sqrt(x) > y is the same as x >= 0, if y is negative. */
1910 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })))
1911 (if (cmp == GT_EXPR || cmp == GE_EXPR)
1915 real_arithmetic (&c2, MULT_EXPR,
1916 &TREE_REAL_CST (@1), &TREE_REAL_CST (@1));
1917 real_convert (&c2, TYPE_MODE (TREE_TYPE (@0)), &c2);
1919 (if (REAL_VALUE_ISINF (c2))
1920 /* sqrt(x) > y is x == +Inf, when y is very large. */
1921 (if (HONOR_INFINITIES (@0))
1922 (eq @0 { build_real (TREE_TYPE (@0), c2); })
1923 { constant_boolean_node (false, type); })
1924 /* sqrt(x) > c is the same as x > c*c. */
1925 (cmp @0 { build_real (TREE_TYPE (@0), c2); }))))
1926 (if (cmp == LT_EXPR || cmp == LE_EXPR)
1930 real_arithmetic (&c2, MULT_EXPR,
1931 &TREE_REAL_CST (@1), &TREE_REAL_CST (@1));
1932 real_convert (&c2, TYPE_MODE (TREE_TYPE (@0)), &c2);
1934 (if (REAL_VALUE_ISINF (c2))
1936 /* sqrt(x) < y is always true, when y is a very large
1937 value and we don't care about NaNs or Infinities. */
1938 (if (! HONOR_NANS (@0) && ! HONOR_INFINITIES (@0))
1939 { constant_boolean_node (true, type); })
1940 /* sqrt(x) < y is x != +Inf when y is very large and we
1941 don't care about NaNs. */
1942 (if (! HONOR_NANS (@0))
1943 (ne @0 { build_real (TREE_TYPE (@0), c2); }))
1944 /* sqrt(x) < y is x >= 0 when y is very large and we
1945 don't care about Infinities. */
1946 (if (! HONOR_INFINITIES (@0))
1947 (ge @0 { build_real (TREE_TYPE (@0), dconst0); }))
1948 /* sqrt(x) < y is x >= 0 && x != +Inf, when y is large. */
1951 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
1952 (ne @0 { build_real (TREE_TYPE (@0), c2); }))))
1953 /* sqrt(x) < c is the same as x < c*c, if we ignore NaNs. */
1954 (if (! HONOR_NANS (@0))
1955 (cmp @0 { build_real (TREE_TYPE (@0), c2); })
1956 /* sqrt(x) < c is the same as x >= 0 && x < c*c. */
1959 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
1960 (cmp @0 { build_real (TREE_TYPE (@0), c2); }))))))))))))
1962 /* Unordered tests if either argument is a NaN. */
1964 (bit_ior (unordered @0 @0) (unordered @1 @1))
1965 (if (types_match (@0, @1))
1968 (bit_and (ordered @0 @0) (ordered @1 @1))
1969 (if (types_match (@0, @1))
1972 (bit_ior:c (unordered @0 @0) (unordered:c@2 @0 @1))
1975 (bit_and:c (ordered @0 @0) (ordered:c@2 @0 @1))
1978 /* -A CMP -B -> B CMP A. */
1979 (for cmp (tcc_comparison)
1980 scmp (swapped_tcc_comparison)
1982 (cmp (negate @0) (negate @1))
1983 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
1984 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1985 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
1988 (cmp (negate @0) CONSTANT_CLASS_P@1)
1989 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
1990 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1991 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
1992 (with { tree tem = fold_unary (NEGATE_EXPR, TREE_TYPE (@0), @1); }
1993 (if (tem && !TREE_OVERFLOW (tem))
1994 (scmp @0 { tem; }))))))
1996 /* Convert ABS_EXPR<x> == 0 or ABS_EXPR<x> != 0 to x == 0 or x != 0. */
1999 (op (abs @0) zerop@1)
2002 /* From fold_sign_changed_comparison and fold_widened_comparison. */
2003 (for cmp (simple_comparison)
2005 (cmp (convert@0 @00) (convert?@1 @10))
2006 (if (TREE_CODE (TREE_TYPE (@0)) == INTEGER_TYPE
2007 /* Disable this optimization if we're casting a function pointer
2008 type on targets that require function pointer canonicalization. */
2009 && !(targetm.have_canonicalize_funcptr_for_compare ()
2010 && TREE_CODE (TREE_TYPE (@00)) == POINTER_TYPE
2011 && TREE_CODE (TREE_TYPE (TREE_TYPE (@00))) == FUNCTION_TYPE)
2013 (if (TYPE_PRECISION (TREE_TYPE (@00)) == TYPE_PRECISION (TREE_TYPE (@0))
2014 && (TREE_CODE (@10) == INTEGER_CST
2015 || (@1 != @10 && types_match (TREE_TYPE (@10), TREE_TYPE (@00))))
2016 && (TYPE_UNSIGNED (TREE_TYPE (@00)) == TYPE_UNSIGNED (TREE_TYPE (@0))
2019 && (POINTER_TYPE_P (TREE_TYPE (@00)) == POINTER_TYPE_P (TREE_TYPE (@0))))
2020 /* ??? The special-casing of INTEGER_CST conversion was in the original
2021 code and here to avoid a spurious overflow flag on the resulting
2022 constant which fold_convert produces. */
2023 (if (TREE_CODE (@1) == INTEGER_CST)
2024 (cmp @00 { force_fit_type (TREE_TYPE (@00), wi::to_widest (@1), 0,
2025 TREE_OVERFLOW (@1)); })
2026 (cmp @00 (convert @1)))
2028 (if (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (TREE_TYPE (@00)))
2029 /* If possible, express the comparison in the shorter mode. */
2030 (if ((cmp == EQ_EXPR || cmp == NE_EXPR
2031 || TYPE_UNSIGNED (TREE_TYPE (@0)) == TYPE_UNSIGNED (TREE_TYPE (@00)))
2032 && (types_match (TREE_TYPE (@10), TREE_TYPE (@00))
2033 || ((TYPE_PRECISION (TREE_TYPE (@00))
2034 >= TYPE_PRECISION (TREE_TYPE (@10)))
2035 && (TYPE_UNSIGNED (TREE_TYPE (@00))
2036 == TYPE_UNSIGNED (TREE_TYPE (@10))))
2037 || (TREE_CODE (@10) == INTEGER_CST
2038 && (TREE_CODE (TREE_TYPE (@00)) == INTEGER_TYPE
2039 || TREE_CODE (TREE_TYPE (@00)) == BOOLEAN_TYPE)
2040 && int_fits_type_p (@10, TREE_TYPE (@00)))))
2041 (cmp @00 (convert @10))
2042 (if (TREE_CODE (@10) == INTEGER_CST
2043 && TREE_CODE (TREE_TYPE (@00)) == INTEGER_TYPE
2044 && !int_fits_type_p (@10, TREE_TYPE (@00)))
2047 tree min = lower_bound_in_type (TREE_TYPE (@10), TREE_TYPE (@00));
2048 tree max = upper_bound_in_type (TREE_TYPE (@10), TREE_TYPE (@00));
2049 bool above = integer_nonzerop (const_binop (LT_EXPR, type, max, @10));
2050 bool below = integer_nonzerop (const_binop (LT_EXPR, type, @10, min));
2052 (if (above || below)
2053 (if (cmp == EQ_EXPR || cmp == NE_EXPR)
2054 { constant_boolean_node (cmp == EQ_EXPR ? false : true, type); }
2055 (if (cmp == LT_EXPR || cmp == LE_EXPR)
2056 { constant_boolean_node (above ? true : false, type); }
2057 (if (cmp == GT_EXPR || cmp == GE_EXPR)
2058 { constant_boolean_node (above ? false : true, type); }))))))))))))
2061 /* A local variable can never be pointed to by
2062 the default SSA name of an incoming parameter.
2063 SSA names are canonicalized to 2nd place. */
2065 (cmp addr@0 SSA_NAME@1)
2066 (if (SSA_NAME_IS_DEFAULT_DEF (@1)
2067 && TREE_CODE (SSA_NAME_VAR (@1)) == PARM_DECL)
2068 (with { tree base = get_base_address (TREE_OPERAND (@0, 0)); }
2069 (if (TREE_CODE (base) == VAR_DECL
2070 && auto_var_in_fn_p (base, current_function_decl))
2071 (if (cmp == NE_EXPR)
2072 { constant_boolean_node (true, type); }
2073 { constant_boolean_node (false, type); }))))))
2075 /* Equality compare simplifications from fold_binary */
2078 /* If we have (A | C) == D where C & ~D != 0, convert this into 0.
2079 Similarly for NE_EXPR. */
2081 (cmp (convert?@3 (bit_ior @0 INTEGER_CST@1)) INTEGER_CST@2)
2082 (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0))
2083 && wi::bit_and_not (@1, @2) != 0)
2084 { constant_boolean_node (cmp == NE_EXPR, type); }))
2086 /* (X ^ Y) == 0 becomes X == Y, and (X ^ Y) != 0 becomes X != Y. */
2088 (cmp (bit_xor @0 @1) integer_zerop)
2091 /* (X ^ Y) == Y becomes X == 0.
2092 Likewise (X ^ Y) == X becomes Y == 0. */
2094 (cmp:c (bit_xor:c @0 @1) @0)
2095 (cmp @1 { build_zero_cst (TREE_TYPE (@1)); }))
2097 /* (X ^ C1) op C2 can be rewritten as X op (C1 ^ C2). */
2099 (cmp (convert?@3 (bit_xor @0 INTEGER_CST@1)) INTEGER_CST@2)
2100 (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0)))
2101 (cmp @0 (bit_xor @1 (convert @2)))))
2104 (cmp (convert? addr@0) integer_zerop)
2105 (if (tree_single_nonzero_warnv_p (@0, NULL))
2106 { constant_boolean_node (cmp == NE_EXPR, type); })))
2108 /* If we have (A & C) == C where C is a power of 2, convert this into
2109 (A & C) != 0. Similarly for NE_EXPR. */
2113 (cmp (bit_and@2 @0 integer_pow2p@1) @1)
2114 (icmp @2 { build_zero_cst (TREE_TYPE (@0)); })))
2116 /* If we have (A & C) != 0 where C is the sign bit of A, convert
2117 this into A < 0. Similarly for (A & C) == 0 into A >= 0. */
2121 (cmp (bit_and (convert?@2 @0) integer_pow2p@1) integer_zerop)
2122 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2123 && (TYPE_PRECISION (TREE_TYPE (@0))
2124 == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (@0))))
2125 && element_precision (@2) >= element_precision (@0)
2126 && wi::only_sign_bit_p (@1, element_precision (@0)))
2127 (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
2128 (ncmp (convert:stype @0) { build_zero_cst (stype); })))))
2130 /* When the addresses are not directly of decls compare base and offset.
2131 This implements some remaining parts of fold_comparison address
2132 comparisons but still no complete part of it. Still it is good
2133 enough to make fold_stmt not regress when not dispatching to fold_binary. */
2134 (for cmp (simple_comparison)
2136 (cmp (convert1?@2 addr@0) (convert2? addr@1))
2139 HOST_WIDE_INT off0, off1;
2140 tree base0 = get_addr_base_and_unit_offset (TREE_OPERAND (@0, 0), &off0);
2141 tree base1 = get_addr_base_and_unit_offset (TREE_OPERAND (@1, 0), &off1);
2142 if (base0 && TREE_CODE (base0) == MEM_REF)
2144 off0 += mem_ref_offset (base0).to_short_addr ();
2145 base0 = TREE_OPERAND (base0, 0);
2147 if (base1 && TREE_CODE (base1) == MEM_REF)
2149 off1 += mem_ref_offset (base1).to_short_addr ();
2150 base1 = TREE_OPERAND (base1, 0);
2153 (if (base0 && base1)
2157 if (decl_in_symtab_p (base0)
2158 && decl_in_symtab_p (base1))
2159 equal = symtab_node::get_create (base0)
2160 ->equal_address_to (symtab_node::get_create (base1));
2161 else if ((DECL_P (base0)
2162 || TREE_CODE (base0) == SSA_NAME
2163 || TREE_CODE (base0) == STRING_CST)
2165 || TREE_CODE (base1) == SSA_NAME
2166 || TREE_CODE (base1) == STRING_CST))
2167 equal = (base0 == base1);
2170 && (cmp == EQ_EXPR || cmp == NE_EXPR
2171 /* If the offsets are equal we can ignore overflow. */
2173 || POINTER_TYPE_OVERFLOW_UNDEFINED
2174 /* Or if we compare using pointers to decls or strings. */
2175 || (POINTER_TYPE_P (TREE_TYPE (@2))
2176 && (DECL_P (base0) || TREE_CODE (base0) == STRING_CST))))
2178 (if (cmp == EQ_EXPR)
2179 { constant_boolean_node (off0 == off1, type); })
2180 (if (cmp == NE_EXPR)
2181 { constant_boolean_node (off0 != off1, type); })
2182 (if (cmp == LT_EXPR)
2183 { constant_boolean_node (off0 < off1, type); })
2184 (if (cmp == LE_EXPR)
2185 { constant_boolean_node (off0 <= off1, type); })
2186 (if (cmp == GE_EXPR)
2187 { constant_boolean_node (off0 >= off1, type); })
2188 (if (cmp == GT_EXPR)
2189 { constant_boolean_node (off0 > off1, type); }))
2191 && DECL_P (base0) && DECL_P (base1)
2192 /* If we compare this as integers require equal offset. */
2193 && (!INTEGRAL_TYPE_P (TREE_TYPE (@2))
2196 (if (cmp == EQ_EXPR)
2197 { constant_boolean_node (false, type); })
2198 (if (cmp == NE_EXPR)
2199 { constant_boolean_node (true, type); })))))))))
2201 /* Non-equality compare simplifications from fold_binary */
2202 (for cmp (lt gt le ge)
2203 /* Comparisons with the highest or lowest possible integer of
2204 the specified precision will have known values. */
2206 (cmp (convert?@2 @0) INTEGER_CST@1)
2207 (if ((INTEGRAL_TYPE_P (TREE_TYPE (@1)) || POINTER_TYPE_P (TREE_TYPE (@1)))
2208 && tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@0)))
2211 tree arg1_type = TREE_TYPE (@1);
2212 unsigned int prec = TYPE_PRECISION (arg1_type);
2213 wide_int max = wi::max_value (arg1_type);
2214 wide_int signed_max = wi::max_value (prec, SIGNED);
2215 wide_int min = wi::min_value (arg1_type);
2218 (if (wi::eq_p (@1, max))
2220 (if (cmp == GT_EXPR)
2221 { constant_boolean_node (false, type); })
2222 (if (cmp == GE_EXPR)
2224 (if (cmp == LE_EXPR)
2225 { constant_boolean_node (true, type); })
2226 (if (cmp == LT_EXPR)
2228 (if (wi::eq_p (@1, min))
2230 (if (cmp == LT_EXPR)
2231 { constant_boolean_node (false, type); })
2232 (if (cmp == LE_EXPR)
2234 (if (cmp == GE_EXPR)
2235 { constant_boolean_node (true, type); })
2236 (if (cmp == GT_EXPR)
2238 (if (wi::eq_p (@1, max - 1))
2240 (if (cmp == GT_EXPR)
2241 (eq @2 { wide_int_to_tree (TREE_TYPE (@1), wi::add (@1, 1)); }))
2242 (if (cmp == LE_EXPR)
2243 (ne @2 { wide_int_to_tree (TREE_TYPE (@1), wi::add (@1, 1)); }))))
2244 (if (wi::eq_p (@1, min + 1))
2246 (if (cmp == GE_EXPR)
2247 (ne @2 { wide_int_to_tree (TREE_TYPE (@1), wi::sub (@1, 1)); }))
2248 (if (cmp == LT_EXPR)
2249 (eq @2 { wide_int_to_tree (TREE_TYPE (@1), wi::sub (@1, 1)); }))))
2250 (if (wi::eq_p (@1, signed_max)
2251 && TYPE_UNSIGNED (arg1_type)
2252 /* We will flip the signedness of the comparison operator
2253 associated with the mode of @1, so the sign bit is
2254 specified by this mode. Check that @1 is the signed
2255 max associated with this sign bit. */
2256 && prec == GET_MODE_PRECISION (TYPE_MODE (arg1_type))
2257 /* signed_type does not work on pointer types. */
2258 && INTEGRAL_TYPE_P (arg1_type))
2259 /* The following case also applies to X < signed_max+1
2260 and X >= signed_max+1 because previous transformations. */
2261 (if (cmp == LE_EXPR || cmp == GT_EXPR)
2262 (with { tree st = signed_type_for (arg1_type); }
2263 (if (cmp == LE_EXPR)
2264 (ge (convert:st @0) { build_zero_cst (st); })
2265 (lt (convert:st @0) { build_zero_cst (st); }))))))))))
2267 (for cmp (unordered ordered unlt unle ungt unge uneq ltgt)
2268 /* If the second operand is NaN, the result is constant. */
2271 (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
2272 && (cmp != LTGT_EXPR || ! flag_trapping_math))
2273 { constant_boolean_node (cmp == ORDERED_EXPR || cmp == LTGT_EXPR
2274 ? false : true, type); })))
2276 /* bool_var != 0 becomes bool_var. */
2278 (ne @0 integer_zerop)
2279 (if (TREE_CODE (TREE_TYPE (@0)) == BOOLEAN_TYPE
2280 && types_match (type, TREE_TYPE (@0)))
2282 /* bool_var == 1 becomes bool_var. */
2284 (eq @0 integer_onep)
2285 (if (TREE_CODE (TREE_TYPE (@0)) == BOOLEAN_TYPE
2286 && types_match (type, TREE_TYPE (@0)))
2289 bool_var == 0 becomes !bool_var or
2290 bool_var != 1 becomes !bool_var
2291 here because that only is good in assignment context as long
2292 as we require a tcc_comparison in GIMPLE_CONDs where we'd
2293 replace if (x == 0) with tem = ~x; if (tem != 0) which is
2294 clearly less optimal and which we'll transform again in forwprop. */
2297 /* Simplification of math builtins. These rules must all be optimizations
2298 as well as IL simplifications. If there is a possibility that the new
2299 form could be a pessimization, the rule should go in the canonicalization
2300 section that follows this one.
2302 Rules can generally go in this section if they satisfy one of
2305 - the rule describes an identity
2307 - the rule replaces calls with something as simple as addition or
2310 - the rule contains unary calls only and simplifies the surrounding
2311 arithmetic. (The idea here is to exclude non-unary calls in which
2312 one operand is constant and in which the call is known to be cheap
2313 when the operand has that value.) */
2315 (if (flag_unsafe_math_optimizations)
2316 /* Simplify sqrt(x) * sqrt(x) -> x. */
2318 (mult (SQRT@1 @0) @1)
2319 (if (!HONOR_SNANS (type))
2322 /* Simplify sqrt(x) * sqrt(y) -> sqrt(x*y). */
2323 (for root (SQRT CBRT)
2325 (mult (root:s @0) (root:s @1))
2326 (root (mult @0 @1))))
2328 /* Simplify expN(x) * expN(y) -> expN(x+y). */
2329 (for exps (EXP EXP2 EXP10 POW10)
2331 (mult (exps:s @0) (exps:s @1))
2332 (exps (plus @0 @1))))
2334 /* Simplify a/root(b/c) into a*root(c/b). */
2335 (for root (SQRT CBRT)
2337 (rdiv @0 (root:s (rdiv:s @1 @2)))
2338 (mult @0 (root (rdiv @2 @1)))))
2340 /* Simplify x/expN(y) into x*expN(-y). */
2341 (for exps (EXP EXP2 EXP10 POW10)
2343 (rdiv @0 (exps:s @1))
2344 (mult @0 (exps (negate @1)))))
2346 /* Special case, optimize logN(expN(x)) = x. */
2347 (for logs (LOG LOG2 LOG10 LOG10)
2348 exps (EXP EXP2 EXP10 POW10)
2353 /* Optimize logN(func()) for various exponential functions. We
2354 want to determine the value "x" and the power "exponent" in
2355 order to transform logN(x**exponent) into exponent*logN(x). */
2356 (for logs (LOG LOG LOG LOG2 LOG2 LOG2 LOG10 LOG10)
2357 exps (EXP2 EXP10 POW10 EXP EXP10 POW10 EXP EXP2)
2364 CASE_FLT_FN (BUILT_IN_EXP):
2365 /* Prepare to do logN(exp(exponent)) -> exponent*logN(e). */
2366 x = build_real_truncate (type, dconst_e ());
2368 CASE_FLT_FN (BUILT_IN_EXP2):
2369 /* Prepare to do logN(exp2(exponent)) -> exponent*logN(2). */
2370 x = build_real (type, dconst2);
2372 CASE_FLT_FN (BUILT_IN_EXP10):
2373 CASE_FLT_FN (BUILT_IN_POW10):
2374 /* Prepare to do logN(exp10(exponent)) -> exponent*logN(10). */
2376 REAL_VALUE_TYPE dconst10;
2377 real_from_integer (&dconst10, VOIDmode, 10, SIGNED);
2378 x = build_real (type, dconst10);
2385 (mult (logs { x; }) @0))))
2397 CASE_FLT_FN (BUILT_IN_SQRT):
2398 /* Prepare to do logN(sqrt(x)) -> 0.5*logN(x). */
2399 x = build_real (type, dconsthalf);
2401 CASE_FLT_FN (BUILT_IN_CBRT):
2402 /* Prepare to do logN(cbrt(x)) -> (1/3)*logN(x). */
2403 x = build_real_truncate (type, dconst_third ());
2409 (mult { x; } (logs @0)))))
2411 /* logN(pow(x,exponent)) -> exponent*logN(x). */
2412 (for logs (LOG LOG2 LOG10)
2416 (mult @1 (logs @0))))
2420 exps (EXP EXP2 EXP10 POW10)
2421 /* sqrt(expN(x)) -> expN(x*0.5). */
2424 (exps (mult @0 { build_real (type, dconsthalf); })))
2425 /* cbrt(expN(x)) -> expN(x/3). */
2428 (exps (mult @0 { build_real_truncate (type, dconst_third ()); }))))
2430 /* tan(atan(x)) -> x. */
2437 /* cabs(x+0i) or cabs(0+xi) -> abs(x). */
2439 (CABS (complex:c @0 real_zerop@1))
2442 /* Canonicalization of sequences of math builtins. These rules represent
2443 IL simplifications but are not necessarily optimizations.
2445 The sincos pass is responsible for picking "optimal" implementations
2446 of math builtins, which may be more complicated and can sometimes go
2447 the other way, e.g. converting pow into a sequence of sqrts.
2448 We only want to do these canonicalizations before the pass has run. */
2450 (if (flag_unsafe_math_optimizations && canonicalize_math_p ())
2451 /* Simplify tan(x) * cos(x) -> sin(x). */
2453 (mult:c (TAN:s @0) (COS:s @0))
2456 /* Simplify x * pow(x,c) -> pow(x,c+1). */
2458 (mult @0 (POW:s @0 REAL_CST@1))
2459 (if (!TREE_OVERFLOW (@1))
2460 (POW @0 (plus @1 { build_one_cst (type); }))))
2462 /* Simplify sin(x) / cos(x) -> tan(x). */
2464 (rdiv (SIN:s @0) (COS:s @0))
2467 /* Simplify cos(x) / sin(x) -> 1 / tan(x). */
2469 (rdiv (COS:s @0) (SIN:s @0))
2470 (rdiv { build_one_cst (type); } (TAN @0)))
2472 /* Simplify sin(x) / tan(x) -> cos(x). */
2474 (rdiv (SIN:s @0) (TAN:s @0))
2475 (if (! HONOR_NANS (@0)
2476 && ! HONOR_INFINITIES (@0))
2479 /* Simplify tan(x) / sin(x) -> 1.0 / cos(x). */
2481 (rdiv (TAN:s @0) (SIN:s @0))
2482 (if (! HONOR_NANS (@0)
2483 && ! HONOR_INFINITIES (@0))
2484 (rdiv { build_one_cst (type); } (COS @0))))
2486 /* Simplify pow(x,y) * pow(x,z) -> pow(x,y+z). */
2488 (mult (POW:s @0 @1) (POW:s @0 @2))
2489 (POW @0 (plus @1 @2)))
2491 /* Simplify pow(x,y) * pow(z,y) -> pow(x*z,y). */
2493 (mult (POW:s @0 @1) (POW:s @2 @1))
2494 (POW (mult @0 @2) @1))
2496 /* Simplify pow(x,c) / x -> pow(x,c-1). */
2498 (rdiv (POW:s @0 REAL_CST@1) @0)
2499 (if (!TREE_OVERFLOW (@1))
2500 (POW @0 (minus @1 { build_one_cst (type); }))))
2502 /* Simplify x / pow (y,z) -> x * pow(y,-z). */
2504 (rdiv @0 (POW:s @1 @2))
2505 (mult @0 (POW @1 (negate @2))))
2510 /* sqrt(sqrt(x)) -> pow(x,1/4). */
2513 (pows @0 { build_real (type, dconst_quarter ()); }))
2514 /* sqrt(cbrt(x)) -> pow(x,1/6). */
2517 (pows @0 { build_real_truncate (type, dconst_sixth ()); }))
2518 /* cbrt(sqrt(x)) -> pow(x,1/6). */
2521 (pows @0 { build_real_truncate (type, dconst_sixth ()); }))
2522 /* cbrt(cbrt(x)) -> pow(x,1/9), iff x is nonnegative. */
2524 (cbrts (cbrts tree_expr_nonnegative_p@0))
2525 (pows @0 { build_real_truncate (type, dconst_ninth ()); }))
2526 /* sqrt(pow(x,y)) -> pow(|x|,y*0.5). */
2528 (sqrts (pows @0 @1))
2529 (pows (abs @0) (mult @1 { build_real (type, dconsthalf); })))
2530 /* cbrt(pow(x,y)) -> pow(x,y/3), iff x is nonnegative. */
2532 (cbrts (pows tree_expr_nonnegative_p@0 @1))
2533 (pows @0 (mult @1 { build_real_truncate (type, dconst_third ()); }))))
2535 /* cabs(x+xi) -> fabs(x)*sqrt(2). */
2537 (CABS (complex @0 @0))
2538 (mult (abs @0) { build_real_truncate (type, dconst_sqrt2 ()); })))
2540 /* cproj(x) -> x if we're ignoring infinities. */
2543 (if (!HONOR_INFINITIES (type))
2546 /* If the real part is inf and the imag part is known to be
2547 nonnegative, return (inf + 0i). */
2549 (CPROJ (complex REAL_CST@0 tree_expr_nonnegative_p@1))
2550 (if (real_isinf (TREE_REAL_CST_PTR (@0)))
2551 { build_complex_inf (type, false); }))
2553 /* If the imag part is inf, return (inf+I*copysign(0,imag)). */
2555 (CPROJ (complex @0 REAL_CST@1))
2556 (if (real_isinf (TREE_REAL_CST_PTR (@1)))
2557 { build_complex_inf (type, TREE_REAL_CST_PTR (@1)->sign); }))
2560 /* Narrowing of arithmetic and logical operations.
2562 These are conceptually similar to the transformations performed for
2563 the C/C++ front-ends by shorten_binary_op and shorten_compare. Long
2564 term we want to move all that code out of the front-ends into here. */
2566 /* If we have a narrowing conversion of an arithmetic operation where
2567 both operands are widening conversions from the same type as the outer
2568 narrowing conversion. Then convert the innermost operands to a suitable
2569 unsigned type (to avoid introducing undefined behaviour), perform the
2570 operation and convert the result to the desired type. */
2571 (for op (plus minus)
2573 (convert (op:s (convert@2 @0) (convert@3 @1)))
2574 (if (INTEGRAL_TYPE_P (type)
2575 /* We check for type compatibility between @0 and @1 below,
2576 so there's no need to check that @1/@3 are integral types. */
2577 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
2578 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
2579 /* The precision of the type of each operand must match the
2580 precision of the mode of each operand, similarly for the
2582 && (TYPE_PRECISION (TREE_TYPE (@0))
2583 == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (@0))))
2584 && (TYPE_PRECISION (TREE_TYPE (@1))
2585 == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (@1))))
2586 && TYPE_PRECISION (type) == GET_MODE_PRECISION (TYPE_MODE (type))
2587 /* The inner conversion must be a widening conversion. */
2588 && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
2589 && types_match (@0, @1)
2590 && types_match (@0, type))
2591 (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
2592 (convert (op @0 @1))
2593 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
2594 (convert (op (convert:utype @0) (convert:utype @1))))))))
2596 /* This is another case of narrowing, specifically when there's an outer
2597 BIT_AND_EXPR which masks off bits outside the type of the innermost
2598 operands. Like the previous case we have to convert the operands
2599 to unsigned types to avoid introducing undefined behaviour for the
2600 arithmetic operation. */
2601 (for op (minus plus)
2603 (bit_and (op:s (convert@2 @0) (convert@3 @1)) INTEGER_CST@4)
2604 (if (INTEGRAL_TYPE_P (type)
2605 /* We check for type compatibility between @0 and @1 below,
2606 so there's no need to check that @1/@3 are integral types. */
2607 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
2608 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
2609 /* The precision of the type of each operand must match the
2610 precision of the mode of each operand, similarly for the
2612 && (TYPE_PRECISION (TREE_TYPE (@0))
2613 == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (@0))))
2614 && (TYPE_PRECISION (TREE_TYPE (@1))
2615 == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (@1))))
2616 && TYPE_PRECISION (type) == GET_MODE_PRECISION (TYPE_MODE (type))
2617 /* The inner conversion must be a widening conversion. */
2618 && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
2619 && types_match (@0, @1)
2620 && (tree_int_cst_min_precision (@4, TYPE_SIGN (TREE_TYPE (@0)))
2621 <= TYPE_PRECISION (TREE_TYPE (@0)))
2622 && (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))
2623 || tree_int_cst_sgn (@4) >= 0))
2624 (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
2625 (with { tree ntype = TREE_TYPE (@0); }
2626 (convert (bit_and (op @0 @1) (convert:ntype @4))))
2627 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
2628 (convert (bit_and (op (convert:utype @0) (convert:utype @1))
2629 (convert:utype @4))))))))
2631 /* Transform (@0 < @1 and @0 < @2) to use min,
2632 (@0 > @1 and @0 > @2) to use max */
2633 (for op (lt le gt ge)
2634 ext (min min max max)
2636 (bit_and (op:s @0 @1) (op:s @0 @2))
2637 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)))
2638 (op @0 (ext @1 @2)))))