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))
1062 /* Simplifications of MIN_EXPR and MAX_EXPR. */
1064 (for minmax (min max)
1070 (if (INTEGRAL_TYPE_P (type)
1071 && TYPE_MIN_VALUE (type)
1072 && operand_equal_p (@1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
1076 (if (INTEGRAL_TYPE_P (type)
1077 && TYPE_MAX_VALUE (type)
1078 && operand_equal_p (@1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
1082 /* Simplifications of shift and rotates. */
1084 (for rotate (lrotate rrotate)
1086 (rotate integer_all_onesp@0 @1)
1089 /* Optimize -1 >> x for arithmetic right shifts. */
1091 (rshift integer_all_onesp@0 @1)
1092 (if (!TYPE_UNSIGNED (type)
1093 && tree_expr_nonnegative_p (@1))
1096 /* Optimize (x >> c) << c into x & (-1<<c). */
1098 (lshift (rshift @0 INTEGER_CST@1) @1)
1099 (if (wi::ltu_p (@1, element_precision (type)))
1100 (bit_and @0 (lshift { build_minus_one_cst (type); } @1))))
1102 /* Optimize (x << c) >> c into x & ((unsigned)-1 >> c) for unsigned
1105 (rshift (lshift @0 INTEGER_CST@1) @1)
1106 (if (TYPE_UNSIGNED (type)
1107 && (wi::ltu_p (@1, element_precision (type))))
1108 (bit_and @0 (rshift { build_minus_one_cst (type); } @1))))
1110 (for shiftrotate (lrotate rrotate lshift rshift)
1112 (shiftrotate @0 integer_zerop)
1115 (shiftrotate integer_zerop@0 @1)
1117 /* Prefer vector1 << scalar to vector1 << vector2
1118 if vector2 is uniform. */
1119 (for vec (VECTOR_CST CONSTRUCTOR)
1121 (shiftrotate @0 vec@1)
1122 (with { tree tem = uniform_vector_p (@1); }
1124 (shiftrotate @0 { tem; }))))))
1126 /* Rewrite an LROTATE_EXPR by a constant into an
1127 RROTATE_EXPR by a new constant. */
1129 (lrotate @0 INTEGER_CST@1)
1130 (rrotate @0 { fold_binary (MINUS_EXPR, TREE_TYPE (@1),
1131 build_int_cst (TREE_TYPE (@1),
1132 element_precision (type)), @1); }))
1134 /* Turn (a OP c1) OP c2 into a OP (c1+c2). */
1135 (for op (lrotate rrotate rshift lshift)
1137 (op (op @0 INTEGER_CST@1) INTEGER_CST@2)
1138 (with { unsigned int prec = element_precision (type); }
1139 (if (wi::ge_p (@1, 0, TYPE_SIGN (TREE_TYPE (@1)))
1140 && wi::lt_p (@1, prec, TYPE_SIGN (TREE_TYPE (@1)))
1141 && wi::ge_p (@2, 0, TYPE_SIGN (TREE_TYPE (@2)))
1142 && wi::lt_p (@2, prec, TYPE_SIGN (TREE_TYPE (@2))))
1143 (with { unsigned int low = wi::add (@1, @2).to_uhwi (); }
1144 /* Deal with a OP (c1 + c2) being undefined but (a OP c1) OP c2
1145 being well defined. */
1147 (if (op == LROTATE_EXPR || op == RROTATE_EXPR)
1148 (op @0 { build_int_cst (TREE_TYPE (@1), low % prec); })
1149 (if (TYPE_UNSIGNED (type) || op == LSHIFT_EXPR)
1150 { build_zero_cst (type); }
1151 (op @0 { build_int_cst (TREE_TYPE (@1), prec - 1); })))
1152 (op @0 { build_int_cst (TREE_TYPE (@1), low); })))))))
1155 /* ((1 << A) & 1) != 0 -> A == 0
1156 ((1 << A) & 1) == 0 -> A != 0 */
1160 (cmp (bit_and (lshift integer_onep @0) integer_onep) integer_zerop)
1161 (icmp @0 { build_zero_cst (TREE_TYPE (@0)); })))
1163 /* (CST1 << A) == CST2 -> A == ctz (CST2) - ctz (CST1)
1164 (CST1 << A) != CST2 -> A != ctz (CST2) - ctz (CST1)
1168 (cmp (lshift INTEGER_CST@0 @1) INTEGER_CST@2)
1169 (with { int cand = wi::ctz (@2) - wi::ctz (@0); }
1171 || (!integer_zerop (@2)
1172 && wi::ne_p (wi::lshift (@0, cand), @2)))
1173 { constant_boolean_node (cmp == NE_EXPR, type); }
1174 (if (!integer_zerop (@2)
1175 && wi::eq_p (wi::lshift (@0, cand), @2))
1176 (cmp @1 { build_int_cst (TREE_TYPE (@1), cand); }))))))
1178 /* Fold (X << C1) & C2 into (X << C1) & (C2 | ((1 << C1) - 1))
1179 (X >> C1) & C2 into (X >> C1) & (C2 | ~((type) -1 >> C1))
1180 if the new mask might be further optimized. */
1181 (for shift (lshift rshift)
1183 (bit_and (convert?:s@4 (shift:s@5 (convert1?@3 @0) INTEGER_CST@1))
1185 (if (tree_nop_conversion_p (TREE_TYPE (@4), TREE_TYPE (@5))
1186 && TYPE_PRECISION (type) <= HOST_BITS_PER_WIDE_INT
1187 && tree_fits_uhwi_p (@1)
1188 && tree_to_uhwi (@1) > 0
1189 && tree_to_uhwi (@1) < TYPE_PRECISION (type))
1192 unsigned int shiftc = tree_to_uhwi (@1);
1193 unsigned HOST_WIDE_INT mask = TREE_INT_CST_LOW (@2);
1194 unsigned HOST_WIDE_INT newmask, zerobits = 0;
1195 tree shift_type = TREE_TYPE (@3);
1198 if (shift == LSHIFT_EXPR)
1199 zerobits = ((((unsigned HOST_WIDE_INT) 1) << shiftc) - 1);
1200 else if (shift == RSHIFT_EXPR
1201 && (TYPE_PRECISION (shift_type)
1202 == GET_MODE_PRECISION (TYPE_MODE (shift_type))))
1204 prec = TYPE_PRECISION (TREE_TYPE (@3));
1206 /* See if more bits can be proven as zero because of
1209 && TYPE_UNSIGNED (TREE_TYPE (@0)))
1211 tree inner_type = TREE_TYPE (@0);
1212 if ((TYPE_PRECISION (inner_type)
1213 == GET_MODE_PRECISION (TYPE_MODE (inner_type)))
1214 && TYPE_PRECISION (inner_type) < prec)
1216 prec = TYPE_PRECISION (inner_type);
1217 /* See if we can shorten the right shift. */
1219 shift_type = inner_type;
1220 /* Otherwise X >> C1 is all zeros, so we'll optimize
1221 it into (X, 0) later on by making sure zerobits
1225 zerobits = ~(unsigned HOST_WIDE_INT) 0;
1228 zerobits >>= HOST_BITS_PER_WIDE_INT - shiftc;
1229 zerobits <<= prec - shiftc;
1231 /* For arithmetic shift if sign bit could be set, zerobits
1232 can contain actually sign bits, so no transformation is
1233 possible, unless MASK masks them all away. In that
1234 case the shift needs to be converted into logical shift. */
1235 if (!TYPE_UNSIGNED (TREE_TYPE (@3))
1236 && prec == TYPE_PRECISION (TREE_TYPE (@3)))
1238 if ((mask & zerobits) == 0)
1239 shift_type = unsigned_type_for (TREE_TYPE (@3));
1245 /* ((X << 16) & 0xff00) is (X, 0). */
1246 (if ((mask & zerobits) == mask)
1247 { build_int_cst (type, 0); }
1248 (with { newmask = mask | zerobits; }
1249 (if (newmask != mask && (newmask & (newmask + 1)) == 0)
1252 /* Only do the transformation if NEWMASK is some integer
1254 for (prec = BITS_PER_UNIT;
1255 prec < HOST_BITS_PER_WIDE_INT; prec <<= 1)
1256 if (newmask == (((unsigned HOST_WIDE_INT) 1) << prec) - 1)
1259 (if (prec < HOST_BITS_PER_WIDE_INT
1260 || newmask == ~(unsigned HOST_WIDE_INT) 0)
1262 { tree newmaskt = build_int_cst_type (TREE_TYPE (@2), newmask); }
1263 (if (!tree_int_cst_equal (newmaskt, @2))
1264 (if (shift_type != TREE_TYPE (@3))
1265 (bit_and (convert (shift:shift_type (convert @3) @1)) { newmaskt; })
1266 (bit_and @4 { newmaskt; })))))))))))))
1268 /* Fold (X {&,^,|} C2) << C1 into (X << C1) {&,^,|} (C2 << C1)
1269 (X {&,^,|} C2) >> C1 into (X >> C1) & (C2 >> C1). */
1270 (for shift (lshift rshift)
1271 (for bit_op (bit_and bit_xor bit_ior)
1273 (shift (convert?:s (bit_op:s @0 INTEGER_CST@2)) INTEGER_CST@1)
1274 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1275 (with { tree mask = int_const_binop (shift, fold_convert (type, @2), @1); }
1276 (bit_op (shift (convert @0) @1) { mask; }))))))
1279 /* Simplifications of conversions. */
1281 /* Basic strip-useless-type-conversions / strip_nops. */
1282 (for cvt (convert view_convert float fix_trunc)
1285 (if ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@0)))
1286 || (GENERIC && type == TREE_TYPE (@0)))
1289 /* Contract view-conversions. */
1291 (view_convert (view_convert @0))
1294 /* For integral conversions with the same precision or pointer
1295 conversions use a NOP_EXPR instead. */
1298 (if ((INTEGRAL_TYPE_P (type) || POINTER_TYPE_P (type))
1299 && (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
1300 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (@0)))
1303 /* Strip inner integral conversions that do not change precision or size. */
1305 (view_convert (convert@0 @1))
1306 (if ((INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
1307 && (INTEGRAL_TYPE_P (TREE_TYPE (@1)) || POINTER_TYPE_P (TREE_TYPE (@1)))
1308 && (TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
1309 && (TYPE_SIZE (TREE_TYPE (@0)) == TYPE_SIZE (TREE_TYPE (@1))))
1312 /* Re-association barriers around constants and other re-association
1313 barriers can be removed. */
1315 (paren CONSTANT_CLASS_P@0)
1318 (paren (paren@1 @0))
1321 /* Handle cases of two conversions in a row. */
1322 (for ocvt (convert float fix_trunc)
1323 (for icvt (convert float)
1328 tree inside_type = TREE_TYPE (@0);
1329 tree inter_type = TREE_TYPE (@1);
1330 int inside_int = INTEGRAL_TYPE_P (inside_type);
1331 int inside_ptr = POINTER_TYPE_P (inside_type);
1332 int inside_float = FLOAT_TYPE_P (inside_type);
1333 int inside_vec = VECTOR_TYPE_P (inside_type);
1334 unsigned int inside_prec = TYPE_PRECISION (inside_type);
1335 int inside_unsignedp = TYPE_UNSIGNED (inside_type);
1336 int inter_int = INTEGRAL_TYPE_P (inter_type);
1337 int inter_ptr = POINTER_TYPE_P (inter_type);
1338 int inter_float = FLOAT_TYPE_P (inter_type);
1339 int inter_vec = VECTOR_TYPE_P (inter_type);
1340 unsigned int inter_prec = TYPE_PRECISION (inter_type);
1341 int inter_unsignedp = TYPE_UNSIGNED (inter_type);
1342 int final_int = INTEGRAL_TYPE_P (type);
1343 int final_ptr = POINTER_TYPE_P (type);
1344 int final_float = FLOAT_TYPE_P (type);
1345 int final_vec = VECTOR_TYPE_P (type);
1346 unsigned int final_prec = TYPE_PRECISION (type);
1347 int final_unsignedp = TYPE_UNSIGNED (type);
1350 /* In addition to the cases of two conversions in a row
1351 handled below, if we are converting something to its own
1352 type via an object of identical or wider precision, neither
1353 conversion is needed. */
1354 (if (((GIMPLE && useless_type_conversion_p (type, inside_type))
1356 && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (inside_type)))
1357 && (((inter_int || inter_ptr) && final_int)
1358 || (inter_float && final_float))
1359 && inter_prec >= final_prec)
1362 /* Likewise, if the intermediate and initial types are either both
1363 float or both integer, we don't need the middle conversion if the
1364 former is wider than the latter and doesn't change the signedness
1365 (for integers). Avoid this if the final type is a pointer since
1366 then we sometimes need the middle conversion. Likewise if the
1367 final type has a precision not equal to the size of its mode. */
1368 (if (((inter_int && inside_int) || (inter_float && inside_float))
1369 && (final_int || final_float)
1370 && inter_prec >= inside_prec
1371 && (inter_float || inter_unsignedp == inside_unsignedp)
1372 && ! (final_prec != GET_MODE_PRECISION (TYPE_MODE (type))
1373 && TYPE_MODE (type) == TYPE_MODE (inter_type)))
1376 /* If we have a sign-extension of a zero-extended value, we can
1377 replace that by a single zero-extension. Likewise if the
1378 final conversion does not change precision we can drop the
1379 intermediate conversion. */
1380 (if (inside_int && inter_int && final_int
1381 && ((inside_prec < inter_prec && inter_prec < final_prec
1382 && inside_unsignedp && !inter_unsignedp)
1383 || final_prec == inter_prec))
1386 /* Two conversions in a row are not needed unless:
1387 - some conversion is floating-point (overstrict for now), or
1388 - some conversion is a vector (overstrict for now), or
1389 - the intermediate type is narrower than both initial and
1391 - the intermediate type and innermost type differ in signedness,
1392 and the outermost type is wider than the intermediate, or
1393 - the initial type is a pointer type and the precisions of the
1394 intermediate and final types differ, or
1395 - the final type is a pointer type and the precisions of the
1396 initial and intermediate types differ. */
1397 (if (! inside_float && ! inter_float && ! final_float
1398 && ! inside_vec && ! inter_vec && ! final_vec
1399 && (inter_prec >= inside_prec || inter_prec >= final_prec)
1400 && ! (inside_int && inter_int
1401 && inter_unsignedp != inside_unsignedp
1402 && inter_prec < final_prec)
1403 && ((inter_unsignedp && inter_prec > inside_prec)
1404 == (final_unsignedp && final_prec > inter_prec))
1405 && ! (inside_ptr && inter_prec != final_prec)
1406 && ! (final_ptr && inside_prec != inter_prec)
1407 && ! (final_prec != GET_MODE_PRECISION (TYPE_MODE (type))
1408 && TYPE_MODE (type) == TYPE_MODE (inter_type)))
1411 /* A truncation to an unsigned type (a zero-extension) should be
1412 canonicalized as bitwise and of a mask. */
1413 (if (final_int && inter_int && inside_int
1414 && final_prec == inside_prec
1415 && final_prec > inter_prec
1417 (convert (bit_and @0 { wide_int_to_tree
1419 wi::mask (inter_prec, false,
1420 TYPE_PRECISION (inside_type))); })))
1422 /* If we are converting an integer to a floating-point that can
1423 represent it exactly and back to an integer, we can skip the
1424 floating-point conversion. */
1425 (if (GIMPLE /* PR66211 */
1426 && inside_int && inter_float && final_int &&
1427 (unsigned) significand_size (TYPE_MODE (inter_type))
1428 >= inside_prec - !inside_unsignedp)
1431 /* If we have a narrowing conversion to an integral type that is fed by a
1432 BIT_AND_EXPR, we might be able to remove the BIT_AND_EXPR if it merely
1433 masks off bits outside the final type (and nothing else). */
1435 (convert (bit_and @0 INTEGER_CST@1))
1436 (if (INTEGRAL_TYPE_P (type)
1437 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
1438 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))
1439 && operand_equal_p (@1, build_low_bits_mask (TREE_TYPE (@1),
1440 TYPE_PRECISION (type)), 0))
1444 /* (X /[ex] A) * A -> X. */
1446 (mult (convert? (exact_div @0 @1)) @1)
1447 /* Look through a sign-changing conversion. */
1450 /* Canonicalization of binary operations. */
1452 /* Convert X + -C into X - C. */
1454 (plus @0 REAL_CST@1)
1455 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
1456 (with { tree tem = fold_unary (NEGATE_EXPR, type, @1); }
1457 (if (!TREE_OVERFLOW (tem) || !flag_trapping_math)
1458 (minus @0 { tem; })))))
1460 /* Convert x+x into x*2.0. */
1463 (if (SCALAR_FLOAT_TYPE_P (type))
1464 (mult @0 { build_real (type, dconst2); })))
1467 (minus integer_zerop @1)
1470 /* (ARG0 - ARG1) is the same as (-ARG1 + ARG0). So check whether
1471 ARG0 is zero and X + ARG0 reduces to X, since that would mean
1472 (-ARG1 + ARG0) reduces to -ARG1. */
1474 (minus real_zerop@0 @1)
1475 (if (fold_real_zero_addition_p (type, @0, 0))
1478 /* Transform x * -1 into -x. */
1480 (mult @0 integer_minus_onep)
1483 /* COMPLEX_EXPR and REALPART/IMAGPART_EXPR cancellations. */
1485 (complex (realpart @0) (imagpart @0))
1488 (realpart (complex @0 @1))
1491 (imagpart (complex @0 @1))
1494 /* Sometimes we only care about half of a complex expression. */
1496 (realpart (convert?:s (conj:s @0)))
1497 (convert (realpart @0)))
1499 (imagpart (convert?:s (conj:s @0)))
1500 (convert (negate (imagpart @0))))
1501 (for part (realpart imagpart)
1502 (for op (plus minus)
1504 (part (convert?:s@2 (op:s @0 @1)))
1505 (convert (op (part @0) (part @1))))))
1507 (realpart (convert?:s (CEXPI:s @0)))
1510 (imagpart (convert?:s (CEXPI:s @0)))
1513 /* conj(conj(x)) -> x */
1515 (conj (convert? (conj @0)))
1516 (if (tree_nop_conversion_p (TREE_TYPE (@0), type))
1519 /* conj({x,y}) -> {x,-y} */
1521 (conj (convert?:s (complex:s @0 @1)))
1522 (with { tree itype = TREE_TYPE (type); }
1523 (complex (convert:itype @0) (negate (convert:itype @1)))))
1525 /* BSWAP simplifications, transforms checked by gcc.dg/builtin-bswap-8.c. */
1526 (for bswap (BUILT_IN_BSWAP16 BUILT_IN_BSWAP32 BUILT_IN_BSWAP64)
1531 (bswap (bit_not (bswap @0)))
1533 (for bitop (bit_xor bit_ior bit_and)
1535 (bswap (bitop:c (bswap @0) @1))
1536 (bitop @0 (bswap @1)))))
1539 /* Combine COND_EXPRs and VEC_COND_EXPRs. */
1541 /* Simplify constant conditions.
1542 Only optimize constant conditions when the selected branch
1543 has the same type as the COND_EXPR. This avoids optimizing
1544 away "c ? x : throw", where the throw has a void type.
1545 Note that we cannot throw away the fold-const.c variant nor
1546 this one as we depend on doing this transform before possibly
1547 A ? B : B -> B triggers and the fold-const.c one can optimize
1548 0 ? A : B to B even if A has side-effects. Something
1549 genmatch cannot handle. */
1551 (cond INTEGER_CST@0 @1 @2)
1552 (if (integer_zerop (@0))
1553 (if (!VOID_TYPE_P (TREE_TYPE (@2)) || VOID_TYPE_P (type))
1555 (if (!VOID_TYPE_P (TREE_TYPE (@1)) || VOID_TYPE_P (type))
1558 (vec_cond VECTOR_CST@0 @1 @2)
1559 (if (integer_all_onesp (@0))
1561 (if (integer_zerop (@0))
1564 (for cnd (cond vec_cond)
1565 /* A ? B : (A ? X : C) -> A ? B : C. */
1567 (cnd @0 (cnd @0 @1 @2) @3)
1570 (cnd @0 @1 (cnd @0 @2 @3))
1573 /* A ? B : B -> B. */
1578 /* !A ? B : C -> A ? C : B. */
1580 (cnd (logical_inverted_value truth_valued_p@0) @1 @2)
1583 /* A + (B vcmp C ? 1 : 0) -> A - (B vcmp C), since vector comparisons
1584 return all-1 or all-0 results. */
1585 /* ??? We could instead convert all instances of the vec_cond to negate,
1586 but that isn't necessarily a win on its own. */
1588 (plus:c @3 (view_convert? (vec_cond @0 integer_each_onep@1 integer_zerop@2)))
1589 (if (VECTOR_TYPE_P (type)
1590 && TYPE_VECTOR_SUBPARTS (type) == TYPE_VECTOR_SUBPARTS (TREE_TYPE (@0))
1591 && (TYPE_MODE (TREE_TYPE (type))
1592 == TYPE_MODE (TREE_TYPE (TREE_TYPE (@0)))))
1593 (minus @3 (view_convert @0))))
1595 /* ... likewise A - (B vcmp C ? 1 : 0) -> A + (B vcmp C). */
1597 (minus @3 (view_convert? (vec_cond @0 integer_each_onep@1 integer_zerop@2)))
1598 (if (VECTOR_TYPE_P (type)
1599 && TYPE_VECTOR_SUBPARTS (type) == TYPE_VECTOR_SUBPARTS (TREE_TYPE (@0))
1600 && (TYPE_MODE (TREE_TYPE (type))
1601 == TYPE_MODE (TREE_TYPE (TREE_TYPE (@0)))))
1602 (plus @3 (view_convert @0))))
1605 /* Simplifications of comparisons. */
1607 /* See if we can reduce the magnitude of a constant involved in a
1608 comparison by changing the comparison code. This is a canonicalization
1609 formerly done by maybe_canonicalize_comparison_1. */
1613 (cmp @0 INTEGER_CST@1)
1614 (if (tree_int_cst_sgn (@1) == -1)
1615 (acmp @0 { wide_int_to_tree (TREE_TYPE (@1), wi::add (@1, 1)); }))))
1619 (cmp @0 INTEGER_CST@1)
1620 (if (tree_int_cst_sgn (@1) == 1)
1621 (acmp @0 { wide_int_to_tree (TREE_TYPE (@1), wi::sub (@1, 1)); }))))
1624 /* We can simplify a logical negation of a comparison to the
1625 inverted comparison. As we cannot compute an expression
1626 operator using invert_tree_comparison we have to simulate
1627 that with expression code iteration. */
1628 (for cmp (tcc_comparison)
1629 icmp (inverted_tcc_comparison)
1630 ncmp (inverted_tcc_comparison_with_nans)
1631 /* Ideally we'd like to combine the following two patterns
1632 and handle some more cases by using
1633 (logical_inverted_value (cmp @0 @1))
1634 here but for that genmatch would need to "inline" that.
1635 For now implement what forward_propagate_comparison did. */
1637 (bit_not (cmp @0 @1))
1638 (if (VECTOR_TYPE_P (type)
1639 || (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1))
1640 /* Comparison inversion may be impossible for trapping math,
1641 invert_tree_comparison will tell us. But we can't use
1642 a computed operator in the replacement tree thus we have
1643 to play the trick below. */
1644 (with { enum tree_code ic = invert_tree_comparison
1645 (cmp, HONOR_NANS (@0)); }
1651 (bit_xor (cmp @0 @1) integer_truep)
1652 (with { enum tree_code ic = invert_tree_comparison
1653 (cmp, HONOR_NANS (@0)); }
1659 /* Transform comparisons of the form X - Y CMP 0 to X CMP Y.
1660 ??? The transformation is valid for the other operators if overflow
1661 is undefined for the type, but performing it here badly interacts
1662 with the transformation in fold_cond_expr_with_comparison which
1663 attempts to synthetize ABS_EXPR. */
1666 (cmp (minus@2 @0 @1) integer_zerop)
1667 (if (single_use (@2))
1670 /* Transform comparisons of the form X * C1 CMP 0 to X CMP 0 in the
1671 signed arithmetic case. That form is created by the compiler
1672 often enough for folding it to be of value. One example is in
1673 computing loop trip counts after Operator Strength Reduction. */
1674 (for cmp (simple_comparison)
1675 scmp (swapped_simple_comparison)
1677 (cmp (mult @0 INTEGER_CST@1) integer_zerop@2)
1678 /* Handle unfolded multiplication by zero. */
1679 (if (integer_zerop (@1))
1681 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1682 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1683 /* If @1 is negative we swap the sense of the comparison. */
1684 (if (tree_int_cst_sgn (@1) < 0)
1688 /* Simplify comparison of something with itself. For IEEE
1689 floating-point, we can only do some of these simplifications. */
1692 (if (! FLOAT_TYPE_P (TREE_TYPE (@0))
1693 || ! HONOR_NANS (TYPE_MODE (TREE_TYPE (@0))))
1694 { constant_boolean_node (true, type); }))
1703 || ! FLOAT_TYPE_P (TREE_TYPE (@0))
1704 || ! HONOR_NANS (TYPE_MODE (TREE_TYPE (@0))))
1705 { constant_boolean_node (false, type); })))
1706 (for cmp (unle unge uneq)
1709 { constant_boolean_node (true, type); }))
1712 (if (!flag_trapping_math)
1713 { constant_boolean_node (false, type); }))
1715 /* Fold ~X op ~Y as Y op X. */
1716 (for cmp (simple_comparison)
1718 (cmp (bit_not @0) (bit_not @1))
1721 /* Fold ~X op C as X op' ~C, where op' is the swapped comparison. */
1722 (for cmp (simple_comparison)
1723 scmp (swapped_simple_comparison)
1725 (cmp (bit_not @0) CONSTANT_CLASS_P@1)
1726 (if (TREE_CODE (@1) == INTEGER_CST || TREE_CODE (@1) == VECTOR_CST)
1727 (scmp @0 (bit_not @1)))))
1729 (for cmp (simple_comparison)
1730 /* Fold (double)float1 CMP (double)float2 into float1 CMP float2. */
1732 (cmp (convert@2 @0) (convert? @1))
1733 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
1734 && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@2))
1735 == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0)))
1736 && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@2))
1737 == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1))))
1740 tree type1 = TREE_TYPE (@1);
1741 if (TREE_CODE (@1) == REAL_CST && !DECIMAL_FLOAT_TYPE_P (type1))
1743 REAL_VALUE_TYPE orig = TREE_REAL_CST (@1);
1744 if (TYPE_PRECISION (type1) > TYPE_PRECISION (float_type_node)
1745 && exact_real_truncate (TYPE_MODE (float_type_node), &orig))
1746 type1 = float_type_node;
1747 if (TYPE_PRECISION (type1) > TYPE_PRECISION (double_type_node)
1748 && exact_real_truncate (TYPE_MODE (double_type_node), &orig))
1749 type1 = double_type_node;
1752 = (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type1)
1753 ? TREE_TYPE (@0) : type1);
1755 (if (TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (newtype))
1756 (cmp (convert:newtype @0) (convert:newtype @1))))))
1760 /* IEEE doesn't distinguish +0 and -0 in comparisons. */
1762 /* a CMP (-0) -> a CMP 0 */
1763 (if (REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@1)))
1764 (cmp @0 { build_real (TREE_TYPE (@1), dconst0); }))
1765 /* x != NaN is always true, other ops are always false. */
1766 (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
1767 && ! HONOR_SNANS (@1))
1768 { constant_boolean_node (cmp == NE_EXPR, type); })
1769 /* Fold comparisons against infinity. */
1770 (if (REAL_VALUE_ISINF (TREE_REAL_CST (@1))
1771 && MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (@1))))
1774 REAL_VALUE_TYPE max;
1775 enum tree_code code = cmp;
1776 bool neg = REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1));
1778 code = swap_tree_comparison (code);
1781 /* x > +Inf is always false, if with ignore sNANs. */
1782 (if (code == GT_EXPR
1783 && ! HONOR_SNANS (@0))
1784 { constant_boolean_node (false, type); })
1785 (if (code == LE_EXPR)
1786 /* x <= +Inf is always true, if we don't case about NaNs. */
1787 (if (! HONOR_NANS (@0))
1788 { constant_boolean_node (true, type); }
1789 /* x <= +Inf is the same as x == x, i.e. !isnan(x). */
1791 /* x == +Inf and x >= +Inf are always equal to x > DBL_MAX. */
1792 (if (code == EQ_EXPR || code == GE_EXPR)
1793 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
1795 (lt @0 { build_real (TREE_TYPE (@0), max); })
1796 (gt @0 { build_real (TREE_TYPE (@0), max); }))))
1797 /* x < +Inf is always equal to x <= DBL_MAX. */
1798 (if (code == LT_EXPR)
1799 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
1801 (ge @0 { build_real (TREE_TYPE (@0), max); })
1802 (le @0 { build_real (TREE_TYPE (@0), max); }))))
1803 /* x != +Inf is always equal to !(x > DBL_MAX). */
1804 (if (code == NE_EXPR)
1805 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
1806 (if (! HONOR_NANS (@0))
1808 (ge @0 { build_real (TREE_TYPE (@0), max); })
1809 (le @0 { build_real (TREE_TYPE (@0), max); }))
1811 (bit_xor (lt @0 { build_real (TREE_TYPE (@0), max); })
1812 { build_one_cst (type); })
1813 (bit_xor (gt @0 { build_real (TREE_TYPE (@0), max); })
1814 { build_one_cst (type); }))))))))))
1816 /* If this is a comparison of a real constant with a PLUS_EXPR
1817 or a MINUS_EXPR of a real constant, we can convert it into a
1818 comparison with a revised real constant as long as no overflow
1819 occurs when unsafe_math_optimizations are enabled. */
1820 (if (flag_unsafe_math_optimizations)
1821 (for op (plus minus)
1823 (cmp (op @0 REAL_CST@1) REAL_CST@2)
1826 tree tem = const_binop (op == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR,
1827 TREE_TYPE (@1), @2, @1);
1829 (if (tem && !TREE_OVERFLOW (tem))
1830 (cmp @0 { tem; }))))))
1832 /* Likewise, we can simplify a comparison of a real constant with
1833 a MINUS_EXPR whose first operand is also a real constant, i.e.
1834 (c1 - x) < c2 becomes x > c1-c2. Reordering is allowed on
1835 floating-point types only if -fassociative-math is set. */
1836 (if (flag_associative_math)
1838 (cmp (minus REAL_CST@0 @1) REAL_CST@2)
1839 (with { tree tem = const_binop (MINUS_EXPR, TREE_TYPE (@1), @0, @2); }
1840 (if (tem && !TREE_OVERFLOW (tem))
1841 (cmp { tem; } @1)))))
1843 /* Fold comparisons against built-in math functions. */
1844 (if (flag_unsafe_math_optimizations
1845 && ! flag_errno_math)
1848 (cmp (sq @0) REAL_CST@1)
1850 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
1852 /* sqrt(x) < y is always false, if y is negative. */
1853 (if (cmp == EQ_EXPR || cmp == LT_EXPR || cmp == LE_EXPR)
1854 { constant_boolean_node (false, type); })
1855 /* sqrt(x) > y is always true, if y is negative and we
1856 don't care about NaNs, i.e. negative values of x. */
1857 (if (cmp == NE_EXPR || !HONOR_NANS (@0))
1858 { constant_boolean_node (true, type); })
1859 /* sqrt(x) > y is the same as x >= 0, if y is negative. */
1860 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })))
1861 (if (cmp == GT_EXPR || cmp == GE_EXPR)
1865 real_arithmetic (&c2, MULT_EXPR,
1866 &TREE_REAL_CST (@1), &TREE_REAL_CST (@1));
1867 real_convert (&c2, TYPE_MODE (TREE_TYPE (@0)), &c2);
1869 (if (REAL_VALUE_ISINF (c2))
1870 /* sqrt(x) > y is x == +Inf, when y is very large. */
1871 (if (HONOR_INFINITIES (@0))
1872 (eq @0 { build_real (TREE_TYPE (@0), c2); })
1873 { constant_boolean_node (false, type); })
1874 /* sqrt(x) > c is the same as x > c*c. */
1875 (cmp @0 { build_real (TREE_TYPE (@0), c2); }))))
1876 (if (cmp == LT_EXPR || cmp == LE_EXPR)
1880 real_arithmetic (&c2, MULT_EXPR,
1881 &TREE_REAL_CST (@1), &TREE_REAL_CST (@1));
1882 real_convert (&c2, TYPE_MODE (TREE_TYPE (@0)), &c2);
1884 (if (REAL_VALUE_ISINF (c2))
1886 /* sqrt(x) < y is always true, when y is a very large
1887 value and we don't care about NaNs or Infinities. */
1888 (if (! HONOR_NANS (@0) && ! HONOR_INFINITIES (@0))
1889 { constant_boolean_node (true, type); })
1890 /* sqrt(x) < y is x != +Inf when y is very large and we
1891 don't care about NaNs. */
1892 (if (! HONOR_NANS (@0))
1893 (ne @0 { build_real (TREE_TYPE (@0), c2); }))
1894 /* sqrt(x) < y is x >= 0 when y is very large and we
1895 don't care about Infinities. */
1896 (if (! HONOR_INFINITIES (@0))
1897 (ge @0 { build_real (TREE_TYPE (@0), dconst0); }))
1898 /* sqrt(x) < y is x >= 0 && x != +Inf, when y is large. */
1901 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
1902 (ne @0 { build_real (TREE_TYPE (@0), c2); }))))
1903 /* sqrt(x) < c is the same as x < c*c, if we ignore NaNs. */
1904 (if (! HONOR_NANS (@0))
1905 (cmp @0 { build_real (TREE_TYPE (@0), c2); })
1906 /* sqrt(x) < c is the same as x >= 0 && x < c*c. */
1909 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
1910 (cmp @0 { build_real (TREE_TYPE (@0), c2); }))))))))))))
1912 /* Unordered tests if either argument is a NaN. */
1914 (bit_ior (unordered @0 @0) (unordered @1 @1))
1915 (if (types_match (@0, @1))
1918 (bit_and (ordered @0 @0) (ordered @1 @1))
1919 (if (types_match (@0, @1))
1922 (bit_ior:c (unordered @0 @0) (unordered:c@2 @0 @1))
1925 (bit_and:c (ordered @0 @0) (ordered:c@2 @0 @1))
1928 /* -A CMP -B -> B CMP A. */
1929 (for cmp (tcc_comparison)
1930 scmp (swapped_tcc_comparison)
1932 (cmp (negate @0) (negate @1))
1933 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
1934 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1935 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
1938 (cmp (negate @0) CONSTANT_CLASS_P@1)
1939 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
1940 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1941 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
1942 (with { tree tem = fold_unary (NEGATE_EXPR, TREE_TYPE (@0), @1); }
1943 (if (tem && !TREE_OVERFLOW (tem))
1944 (scmp @0 { tem; }))))))
1946 /* Convert ABS_EXPR<x> == 0 or ABS_EXPR<x> != 0 to x == 0 or x != 0. */
1949 (op (abs @0) zerop@1)
1952 /* From fold_sign_changed_comparison and fold_widened_comparison. */
1953 (for cmp (simple_comparison)
1955 (cmp (convert@0 @00) (convert?@1 @10))
1956 (if (TREE_CODE (TREE_TYPE (@0)) == INTEGER_TYPE
1957 /* Disable this optimization if we're casting a function pointer
1958 type on targets that require function pointer canonicalization. */
1959 && !(targetm.have_canonicalize_funcptr_for_compare ()
1960 && TREE_CODE (TREE_TYPE (@00)) == POINTER_TYPE
1961 && TREE_CODE (TREE_TYPE (TREE_TYPE (@00))) == FUNCTION_TYPE)
1963 (if (TYPE_PRECISION (TREE_TYPE (@00)) == TYPE_PRECISION (TREE_TYPE (@0))
1964 && (TREE_CODE (@10) == INTEGER_CST
1965 || (@1 != @10 && types_match (TREE_TYPE (@10), TREE_TYPE (@00))))
1966 && (TYPE_UNSIGNED (TREE_TYPE (@00)) == TYPE_UNSIGNED (TREE_TYPE (@0))
1969 && (POINTER_TYPE_P (TREE_TYPE (@00)) == POINTER_TYPE_P (TREE_TYPE (@0))))
1970 /* ??? The special-casing of INTEGER_CST conversion was in the original
1971 code and here to avoid a spurious overflow flag on the resulting
1972 constant which fold_convert produces. */
1973 (if (TREE_CODE (@1) == INTEGER_CST)
1974 (cmp @00 { force_fit_type (TREE_TYPE (@00), wi::to_widest (@1), 0,
1975 TREE_OVERFLOW (@1)); })
1976 (cmp @00 (convert @1)))
1978 (if (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (TREE_TYPE (@00)))
1979 /* If possible, express the comparison in the shorter mode. */
1980 (if ((cmp == EQ_EXPR || cmp == NE_EXPR
1981 || TYPE_UNSIGNED (TREE_TYPE (@0)) == TYPE_UNSIGNED (TREE_TYPE (@00)))
1982 && (types_match (TREE_TYPE (@10), TREE_TYPE (@00))
1983 || ((TYPE_PRECISION (TREE_TYPE (@00))
1984 >= TYPE_PRECISION (TREE_TYPE (@10)))
1985 && (TYPE_UNSIGNED (TREE_TYPE (@00))
1986 == TYPE_UNSIGNED (TREE_TYPE (@10))))
1987 || (TREE_CODE (@10) == INTEGER_CST
1988 && (TREE_CODE (TREE_TYPE (@00)) == INTEGER_TYPE
1989 || TREE_CODE (TREE_TYPE (@00)) == BOOLEAN_TYPE)
1990 && int_fits_type_p (@10, TREE_TYPE (@00)))))
1991 (cmp @00 (convert @10))
1992 (if (TREE_CODE (@10) == INTEGER_CST
1993 && TREE_CODE (TREE_TYPE (@00)) == INTEGER_TYPE
1994 && !int_fits_type_p (@10, TREE_TYPE (@00)))
1997 tree min = lower_bound_in_type (TREE_TYPE (@10), TREE_TYPE (@00));
1998 tree max = upper_bound_in_type (TREE_TYPE (@10), TREE_TYPE (@00));
1999 bool above = integer_nonzerop (const_binop (LT_EXPR, type, max, @10));
2000 bool below = integer_nonzerop (const_binop (LT_EXPR, type, @10, min));
2002 (if (above || below)
2003 (if (cmp == EQ_EXPR || cmp == NE_EXPR)
2004 { constant_boolean_node (cmp == EQ_EXPR ? false : true, type); }
2005 (if (cmp == LT_EXPR || cmp == LE_EXPR)
2006 { constant_boolean_node (above ? true : false, type); }
2007 (if (cmp == GT_EXPR || cmp == GE_EXPR)
2008 { constant_boolean_node (above ? false : true, type); }))))))))))))
2011 /* A local variable can never be pointed to by
2012 the default SSA name of an incoming parameter.
2013 SSA names are canonicalized to 2nd place. */
2015 (cmp addr@0 SSA_NAME@1)
2016 (if (SSA_NAME_IS_DEFAULT_DEF (@1)
2017 && TREE_CODE (SSA_NAME_VAR (@1)) == PARM_DECL)
2018 (with { tree base = get_base_address (TREE_OPERAND (@0, 0)); }
2019 (if (TREE_CODE (base) == VAR_DECL
2020 && auto_var_in_fn_p (base, current_function_decl))
2021 (if (cmp == NE_EXPR)
2022 { constant_boolean_node (true, type); }
2023 { constant_boolean_node (false, type); }))))))
2025 /* Equality compare simplifications from fold_binary */
2028 /* If we have (A | C) == D where C & ~D != 0, convert this into 0.
2029 Similarly for NE_EXPR. */
2031 (cmp (convert?@3 (bit_ior @0 INTEGER_CST@1)) INTEGER_CST@2)
2032 (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0))
2033 && wi::bit_and_not (@1, @2) != 0)
2034 { constant_boolean_node (cmp == NE_EXPR, type); }))
2036 /* (X ^ Y) == 0 becomes X == Y, and (X ^ Y) != 0 becomes X != Y. */
2038 (cmp (bit_xor @0 @1) integer_zerop)
2041 /* (X ^ Y) == Y becomes X == 0.
2042 Likewise (X ^ Y) == X becomes Y == 0. */
2044 (cmp:c (bit_xor:c @0 @1) @0)
2045 (cmp @1 { build_zero_cst (TREE_TYPE (@1)); }))
2047 /* (X ^ C1) op C2 can be rewritten as X op (C1 ^ C2). */
2049 (cmp (convert?@3 (bit_xor @0 INTEGER_CST@1)) INTEGER_CST@2)
2050 (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0)))
2051 (cmp @0 (bit_xor @1 (convert @2)))))
2054 (cmp (convert? addr@0) integer_zerop)
2055 (if (tree_single_nonzero_warnv_p (@0, NULL))
2056 { constant_boolean_node (cmp == NE_EXPR, type); })))
2058 /* If we have (A & C) == C where C is a power of 2, convert this into
2059 (A & C) != 0. Similarly for NE_EXPR. */
2063 (cmp (bit_and@2 @0 integer_pow2p@1) @1)
2064 (icmp @2 { build_zero_cst (TREE_TYPE (@0)); })))
2066 /* If we have (A & C) != 0 where C is the sign bit of A, convert
2067 this into A < 0. Similarly for (A & C) == 0 into A >= 0. */
2071 (cmp (bit_and (convert?@2 @0) integer_pow2p@1) integer_zerop)
2072 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2073 && (TYPE_PRECISION (TREE_TYPE (@0))
2074 == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (@0))))
2075 && element_precision (@2) >= element_precision (@0)
2076 && wi::only_sign_bit_p (@1, element_precision (@0)))
2077 (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
2078 (ncmp (convert:stype @0) { build_zero_cst (stype); })))))
2080 /* When the addresses are not directly of decls compare base and offset.
2081 This implements some remaining parts of fold_comparison address
2082 comparisons but still no complete part of it. Still it is good
2083 enough to make fold_stmt not regress when not dispatching to fold_binary. */
2084 (for cmp (simple_comparison)
2086 (cmp (convert1?@2 addr@0) (convert2? addr@1))
2089 HOST_WIDE_INT off0, off1;
2090 tree base0 = get_addr_base_and_unit_offset (TREE_OPERAND (@0, 0), &off0);
2091 tree base1 = get_addr_base_and_unit_offset (TREE_OPERAND (@1, 0), &off1);
2092 if (base0 && TREE_CODE (base0) == MEM_REF)
2094 off0 += mem_ref_offset (base0).to_short_addr ();
2095 base0 = TREE_OPERAND (base0, 0);
2097 if (base1 && TREE_CODE (base1) == MEM_REF)
2099 off1 += mem_ref_offset (base1).to_short_addr ();
2100 base1 = TREE_OPERAND (base1, 0);
2103 (if (base0 && base1)
2107 if (decl_in_symtab_p (base0)
2108 && decl_in_symtab_p (base1))
2109 equal = symtab_node::get_create (base0)
2110 ->equal_address_to (symtab_node::get_create (base1));
2111 else if ((DECL_P (base0)
2112 || TREE_CODE (base0) == SSA_NAME
2113 || TREE_CODE (base0) == STRING_CST)
2115 || TREE_CODE (base1) == SSA_NAME
2116 || TREE_CODE (base1) == STRING_CST))
2117 equal = (base0 == base1);
2120 && (cmp == EQ_EXPR || cmp == NE_EXPR
2121 /* If the offsets are equal we can ignore overflow. */
2123 || POINTER_TYPE_OVERFLOW_UNDEFINED
2124 /* Or if we compare using pointers to decls or strings. */
2125 || (POINTER_TYPE_P (TREE_TYPE (@2))
2126 && (DECL_P (base0) || TREE_CODE (base0) == STRING_CST))))
2128 (if (cmp == EQ_EXPR)
2129 { constant_boolean_node (off0 == off1, type); })
2130 (if (cmp == NE_EXPR)
2131 { constant_boolean_node (off0 != off1, type); })
2132 (if (cmp == LT_EXPR)
2133 { constant_boolean_node (off0 < off1, type); })
2134 (if (cmp == LE_EXPR)
2135 { constant_boolean_node (off0 <= off1, type); })
2136 (if (cmp == GE_EXPR)
2137 { constant_boolean_node (off0 >= off1, type); })
2138 (if (cmp == GT_EXPR)
2139 { constant_boolean_node (off0 > off1, type); }))
2141 && DECL_P (base0) && DECL_P (base1)
2142 /* If we compare this as integers require equal offset. */
2143 && (!INTEGRAL_TYPE_P (TREE_TYPE (@2))
2146 (if (cmp == EQ_EXPR)
2147 { constant_boolean_node (false, type); })
2148 (if (cmp == NE_EXPR)
2149 { constant_boolean_node (true, type); })))))))))
2151 /* Non-equality compare simplifications from fold_binary */
2152 (for cmp (lt gt le ge)
2153 /* Comparisons with the highest or lowest possible integer of
2154 the specified precision will have known values. */
2156 (cmp (convert?@2 @0) INTEGER_CST@1)
2157 (if ((INTEGRAL_TYPE_P (TREE_TYPE (@1)) || POINTER_TYPE_P (TREE_TYPE (@1)))
2158 && tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@0)))
2161 tree arg1_type = TREE_TYPE (@1);
2162 unsigned int prec = TYPE_PRECISION (arg1_type);
2163 wide_int max = wi::max_value (arg1_type);
2164 wide_int signed_max = wi::max_value (prec, SIGNED);
2165 wide_int min = wi::min_value (arg1_type);
2168 (if (wi::eq_p (@1, max))
2170 (if (cmp == GT_EXPR)
2171 { constant_boolean_node (false, type); })
2172 (if (cmp == GE_EXPR)
2174 (if (cmp == LE_EXPR)
2175 { constant_boolean_node (true, type); })
2176 (if (cmp == LT_EXPR)
2178 (if (wi::eq_p (@1, min))
2180 (if (cmp == LT_EXPR)
2181 { constant_boolean_node (false, type); })
2182 (if (cmp == LE_EXPR)
2184 (if (cmp == GE_EXPR)
2185 { constant_boolean_node (true, type); })
2186 (if (cmp == GT_EXPR)
2188 (if (wi::eq_p (@1, max - 1))
2190 (if (cmp == GT_EXPR)
2191 (eq @2 { wide_int_to_tree (TREE_TYPE (@1), wi::add (@1, 1)); }))
2192 (if (cmp == LE_EXPR)
2193 (ne @2 { wide_int_to_tree (TREE_TYPE (@1), wi::add (@1, 1)); }))))
2194 (if (wi::eq_p (@1, min + 1))
2196 (if (cmp == GE_EXPR)
2197 (ne @2 { wide_int_to_tree (TREE_TYPE (@1), wi::sub (@1, 1)); }))
2198 (if (cmp == LT_EXPR)
2199 (eq @2 { wide_int_to_tree (TREE_TYPE (@1), wi::sub (@1, 1)); }))))
2200 (if (wi::eq_p (@1, signed_max)
2201 && TYPE_UNSIGNED (arg1_type)
2202 /* We will flip the signedness of the comparison operator
2203 associated with the mode of @1, so the sign bit is
2204 specified by this mode. Check that @1 is the signed
2205 max associated with this sign bit. */
2206 && prec == GET_MODE_PRECISION (TYPE_MODE (arg1_type))
2207 /* signed_type does not work on pointer types. */
2208 && INTEGRAL_TYPE_P (arg1_type))
2209 /* The following case also applies to X < signed_max+1
2210 and X >= signed_max+1 because previous transformations. */
2211 (if (cmp == LE_EXPR || cmp == GT_EXPR)
2212 (with { tree st = signed_type_for (arg1_type); }
2213 (if (cmp == LE_EXPR)
2214 (ge (convert:st @0) { build_zero_cst (st); })
2215 (lt (convert:st @0) { build_zero_cst (st); }))))))))))
2217 (for cmp (unordered ordered unlt unle ungt unge uneq ltgt)
2218 /* If the second operand is NaN, the result is constant. */
2221 (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
2222 && (cmp != LTGT_EXPR || ! flag_trapping_math))
2223 { constant_boolean_node (cmp == ORDERED_EXPR || cmp == LTGT_EXPR
2224 ? false : true, type); })))
2226 /* bool_var != 0 becomes bool_var. */
2228 (ne @0 integer_zerop)
2229 (if (TREE_CODE (TREE_TYPE (@0)) == BOOLEAN_TYPE
2230 && types_match (type, TREE_TYPE (@0)))
2232 /* bool_var == 1 becomes bool_var. */
2234 (eq @0 integer_onep)
2235 (if (TREE_CODE (TREE_TYPE (@0)) == BOOLEAN_TYPE
2236 && types_match (type, TREE_TYPE (@0)))
2239 bool_var == 0 becomes !bool_var or
2240 bool_var != 1 becomes !bool_var
2241 here because that only is good in assignment context as long
2242 as we require a tcc_comparison in GIMPLE_CONDs where we'd
2243 replace if (x == 0) with tem = ~x; if (tem != 0) which is
2244 clearly less optimal and which we'll transform again in forwprop. */
2247 /* Simplification of math builtins. These rules must all be optimizations
2248 as well as IL simplifications. If there is a possibility that the new
2249 form could be a pessimization, the rule should go in the canonicalization
2250 section that follows this one.
2252 Rules can generally go in this section if they satisfy one of
2255 - the rule describes an identity
2257 - the rule replaces calls with something as simple as addition or
2260 - the rule contains unary calls only and simplifies the surrounding
2261 arithmetic. (The idea here is to exclude non-unary calls in which
2262 one operand is constant and in which the call is known to be cheap
2263 when the operand has that value.) */
2265 (if (flag_unsafe_math_optimizations)
2266 /* Simplify sqrt(x) * sqrt(x) -> x. */
2268 (mult (SQRT@1 @0) @1)
2269 (if (!HONOR_SNANS (type))
2272 /* Simplify sqrt(x) * sqrt(y) -> sqrt(x*y). */
2273 (for root (SQRT CBRT)
2275 (mult (root:s @0) (root:s @1))
2276 (root (mult @0 @1))))
2278 /* Simplify expN(x) * expN(y) -> expN(x+y). */
2279 (for exps (EXP EXP2 EXP10 POW10)
2281 (mult (exps:s @0) (exps:s @1))
2282 (exps (plus @0 @1))))
2284 /* Simplify a/root(b/c) into a*root(c/b). */
2285 (for root (SQRT CBRT)
2287 (rdiv @0 (root:s (rdiv:s @1 @2)))
2288 (mult @0 (root (rdiv @2 @1)))))
2290 /* Simplify x/expN(y) into x*expN(-y). */
2291 (for exps (EXP EXP2 EXP10 POW10)
2293 (rdiv @0 (exps:s @1))
2294 (mult @0 (exps (negate @1)))))
2296 /* Special case, optimize logN(expN(x)) = x. */
2297 (for logs (LOG LOG2 LOG10 LOG10)
2298 exps (EXP EXP2 EXP10 POW10)
2303 /* Optimize logN(func()) for various exponential functions. We
2304 want to determine the value "x" and the power "exponent" in
2305 order to transform logN(x**exponent) into exponent*logN(x). */
2306 (for logs (LOG LOG LOG LOG2 LOG2 LOG2 LOG10 LOG10)
2307 exps (EXP2 EXP10 POW10 EXP EXP10 POW10 EXP EXP2)
2314 CASE_FLT_FN (BUILT_IN_EXP):
2315 /* Prepare to do logN(exp(exponent)) -> exponent*logN(e). */
2316 x = build_real_truncate (type, dconst_e ());
2318 CASE_FLT_FN (BUILT_IN_EXP2):
2319 /* Prepare to do logN(exp2(exponent)) -> exponent*logN(2). */
2320 x = build_real (type, dconst2);
2322 CASE_FLT_FN (BUILT_IN_EXP10):
2323 CASE_FLT_FN (BUILT_IN_POW10):
2324 /* Prepare to do logN(exp10(exponent)) -> exponent*logN(10). */
2326 REAL_VALUE_TYPE dconst10;
2327 real_from_integer (&dconst10, VOIDmode, 10, SIGNED);
2328 x = build_real (type, dconst10);
2335 (mult (logs { x; }) @0))))
2347 CASE_FLT_FN (BUILT_IN_SQRT):
2348 /* Prepare to do logN(sqrt(x)) -> 0.5*logN(x). */
2349 x = build_real (type, dconsthalf);
2351 CASE_FLT_FN (BUILT_IN_CBRT):
2352 /* Prepare to do logN(cbrt(x)) -> (1/3)*logN(x). */
2353 x = build_real_truncate (type, dconst_third ());
2359 (mult { x; } (logs @0)))))
2361 /* logN(pow(x,exponent)) -> exponent*logN(x). */
2362 (for logs (LOG LOG2 LOG10)
2366 (mult @1 (logs @0))))
2370 exps (EXP EXP2 EXP10 POW10)
2371 /* sqrt(expN(x)) -> expN(x*0.5). */
2374 (exps (mult @0 { build_real (type, dconsthalf); })))
2375 /* cbrt(expN(x)) -> expN(x/3). */
2378 (exps (mult @0 { build_real_truncate (type, dconst_third ()); }))))
2380 /* tan(atan(x)) -> x. */
2387 /* cabs(x+0i) or cabs(0+xi) -> abs(x). */
2389 (CABS (complex:c @0 real_zerop@1))
2392 /* Canonicalization of sequences of math builtins. These rules represent
2393 IL simplifications but are not necessarily optimizations.
2395 The sincos pass is responsible for picking "optimal" implementations
2396 of math builtins, which may be more complicated and can sometimes go
2397 the other way, e.g. converting pow into a sequence of sqrts.
2398 We only want to do these canonicalizations before the pass has run. */
2400 (if (flag_unsafe_math_optimizations && canonicalize_math_p ())
2401 /* Simplify tan(x) * cos(x) -> sin(x). */
2403 (mult:c (TAN:s @0) (COS:s @0))
2406 /* Simplify x * pow(x,c) -> pow(x,c+1). */
2408 (mult @0 (POW:s @0 REAL_CST@1))
2409 (if (!TREE_OVERFLOW (@1))
2410 (POW @0 (plus @1 { build_one_cst (type); }))))
2412 /* Simplify sin(x) / cos(x) -> tan(x). */
2414 (rdiv (SIN:s @0) (COS:s @0))
2417 /* Simplify cos(x) / sin(x) -> 1 / tan(x). */
2419 (rdiv (COS:s @0) (SIN:s @0))
2420 (rdiv { build_one_cst (type); } (TAN @0)))
2422 /* Simplify sin(x) / tan(x) -> cos(x). */
2424 (rdiv (SIN:s @0) (TAN:s @0))
2425 (if (! HONOR_NANS (@0)
2426 && ! HONOR_INFINITIES (@0))
2429 /* Simplify tan(x) / sin(x) -> 1.0 / cos(x). */
2431 (rdiv (TAN:s @0) (SIN:s @0))
2432 (if (! HONOR_NANS (@0)
2433 && ! HONOR_INFINITIES (@0))
2434 (rdiv { build_one_cst (type); } (COS @0))))
2436 /* Simplify pow(x,y) * pow(x,z) -> pow(x,y+z). */
2438 (mult (POW:s @0 @1) (POW:s @0 @2))
2439 (POW @0 (plus @1 @2)))
2441 /* Simplify pow(x,y) * pow(z,y) -> pow(x*z,y). */
2443 (mult (POW:s @0 @1) (POW:s @2 @1))
2444 (POW (mult @0 @2) @1))
2446 /* Simplify pow(x,c) / x -> pow(x,c-1). */
2448 (rdiv (POW:s @0 REAL_CST@1) @0)
2449 (if (!TREE_OVERFLOW (@1))
2450 (POW @0 (minus @1 { build_one_cst (type); }))))
2452 /* Simplify x / pow (y,z) -> x * pow(y,-z). */
2454 (rdiv @0 (POW:s @1 @2))
2455 (mult @0 (POW @1 (negate @2))))
2460 /* sqrt(sqrt(x)) -> pow(x,1/4). */
2463 (pows @0 { build_real (type, dconst_quarter ()); }))
2464 /* sqrt(cbrt(x)) -> pow(x,1/6). */
2467 (pows @0 { build_real_truncate (type, dconst_sixth ()); }))
2468 /* cbrt(sqrt(x)) -> pow(x,1/6). */
2471 (pows @0 { build_real_truncate (type, dconst_sixth ()); }))
2472 /* cbrt(cbrt(x)) -> pow(x,1/9), iff x is nonnegative. */
2474 (cbrts (cbrts tree_expr_nonnegative_p@0))
2475 (pows @0 { build_real_truncate (type, dconst_ninth ()); }))
2476 /* sqrt(pow(x,y)) -> pow(|x|,y*0.5). */
2478 (sqrts (pows @0 @1))
2479 (pows (abs @0) (mult @1 { build_real (type, dconsthalf); })))
2480 /* cbrt(pow(x,y)) -> pow(x,y/3), iff x is nonnegative. */
2482 (cbrts (pows tree_expr_nonnegative_p@0 @1))
2483 (pows @0 (mult @1 { build_real_truncate (type, dconst_third ()); }))))
2485 /* cabs(x+xi) -> fabs(x)*sqrt(2). */
2487 (CABS (complex @0 @0))
2488 (mult (abs @0) { build_real_truncate (type, dconst_sqrt2 ()); })))
2490 /* cproj(x) -> x if we're ignoring infinities. */
2493 (if (!HONOR_INFINITIES (type))
2496 /* If the real part is inf and the imag part is known to be
2497 nonnegative, return (inf + 0i). */
2499 (CPROJ (complex REAL_CST@0 tree_expr_nonnegative_p@1))
2500 (if (real_isinf (TREE_REAL_CST_PTR (@0)))
2501 { build_complex_inf (type, false); }))
2503 /* If the imag part is inf, return (inf+I*copysign(0,imag)). */
2505 (CPROJ (complex @0 REAL_CST@1))
2506 (if (real_isinf (TREE_REAL_CST_PTR (@1)))
2507 { build_complex_inf (type, TREE_REAL_CST_PTR (@1)->sign); }))
2510 /* Narrowing of arithmetic and logical operations.
2512 These are conceptually similar to the transformations performed for
2513 the C/C++ front-ends by shorten_binary_op and shorten_compare. Long
2514 term we want to move all that code out of the front-ends into here. */
2516 /* If we have a narrowing conversion of an arithmetic operation where
2517 both operands are widening conversions from the same type as the outer
2518 narrowing conversion. Then convert the innermost operands to a suitable
2519 unsigned type (to avoid introducing undefined behaviour), perform the
2520 operation and convert the result to the desired type. */
2521 (for op (plus minus)
2523 (convert (op:s (convert@2 @0) (convert@3 @1)))
2524 (if (INTEGRAL_TYPE_P (type)
2525 /* We check for type compatibility between @0 and @1 below,
2526 so there's no need to check that @1/@3 are integral types. */
2527 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
2528 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
2529 /* The precision of the type of each operand must match the
2530 precision of the mode of each operand, similarly for the
2532 && (TYPE_PRECISION (TREE_TYPE (@0))
2533 == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (@0))))
2534 && (TYPE_PRECISION (TREE_TYPE (@1))
2535 == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (@1))))
2536 && TYPE_PRECISION (type) == GET_MODE_PRECISION (TYPE_MODE (type))
2537 /* The inner conversion must be a widening conversion. */
2538 && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
2539 && types_match (@0, @1)
2540 && types_match (@0, type))
2541 (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
2542 (convert (op @0 @1))
2543 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
2544 (convert (op (convert:utype @0) (convert:utype @1))))))))
2546 /* This is another case of narrowing, specifically when there's an outer
2547 BIT_AND_EXPR which masks off bits outside the type of the innermost
2548 operands. Like the previous case we have to convert the operands
2549 to unsigned types to avoid introducing undefined behaviour for the
2550 arithmetic operation. */
2551 (for op (minus plus)
2553 (bit_and (op:s (convert@2 @0) (convert@3 @1)) INTEGER_CST@4)
2554 (if (INTEGRAL_TYPE_P (type)
2555 /* We check for type compatibility between @0 and @1 below,
2556 so there's no need to check that @1/@3 are integral types. */
2557 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
2558 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
2559 /* The precision of the type of each operand must match the
2560 precision of the mode of each operand, similarly for the
2562 && (TYPE_PRECISION (TREE_TYPE (@0))
2563 == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (@0))))
2564 && (TYPE_PRECISION (TREE_TYPE (@1))
2565 == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (@1))))
2566 && TYPE_PRECISION (type) == GET_MODE_PRECISION (TYPE_MODE (type))
2567 /* The inner conversion must be a widening conversion. */
2568 && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
2569 && types_match (@0, @1)
2570 && (tree_int_cst_min_precision (@4, TYPE_SIGN (TREE_TYPE (@0)))
2571 <= TYPE_PRECISION (TREE_TYPE (@0)))
2572 && (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))
2573 || tree_int_cst_sgn (@4) >= 0))
2574 (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
2575 (with { tree ntype = TREE_TYPE (@0); }
2576 (convert (bit_and (op @0 @1) (convert:ntype @4))))
2577 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
2578 (convert (bit_and (op (convert:utype @0) (convert:utype @1))
2579 (convert:utype @4))))))))
2581 /* Transform (@0 < @1 and @0 < @2) to use min,
2582 (@0 > @1 and @0 > @2) to use max */
2583 (for op (lt le gt ge)
2584 ext (min min max max)
2586 (bit_and (op:s @0 @1) (op:s @0 @2))
2587 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)))
2588 (op @0 (ext @1 @2)))))