1 /* Match-and-simplify patterns for shared GENERIC and GIMPLE folding.
2 This file is consumed by genmatch which produces gimple-match.c
3 and generic-match.c from it.
5 Copyright (C) 2014-2016 Free Software Foundation, Inc.
6 Contributed by Richard Biener <rguenther@suse.de>
7 and Prathamesh Kulkarni <bilbotheelffriend@gmail.com>
9 This file is part of GCC.
11 GCC is free software; you can redistribute it and/or modify it under
12 the terms of the GNU General Public License as published by the Free
13 Software Foundation; either version 3, or (at your option) any later
16 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
17 WARRANTY; without even the implied warranty of MERCHANTABILITY or
18 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21 You should have received a copy of the GNU General Public License
22 along with GCC; see the file COPYING3. If not see
23 <http://www.gnu.org/licenses/>. */
26 /* Generic tree predicates we inherit. */
28 integer_onep integer_zerop integer_all_onesp integer_minus_onep
29 integer_each_onep integer_truep integer_nonzerop
30 real_zerop real_onep real_minus_onep
33 tree_expr_nonnegative_p
39 (define_operator_list tcc_comparison
40 lt le eq ne ge gt unordered ordered unlt unle ungt unge uneq ltgt)
41 (define_operator_list inverted_tcc_comparison
42 ge gt ne eq lt le ordered unordered ge gt le lt ltgt uneq)
43 (define_operator_list inverted_tcc_comparison_with_nans
44 unge ungt ne eq unlt unle ordered unordered ge gt le lt ltgt uneq)
45 (define_operator_list swapped_tcc_comparison
46 gt ge eq ne le lt unordered ordered ungt unge unlt unle uneq ltgt)
47 (define_operator_list simple_comparison lt le eq ne ge gt)
48 (define_operator_list swapped_simple_comparison gt ge eq ne le lt)
50 #include "cfn-operators.pd"
52 /* Define operand lists for math rounding functions {,i,l,ll}FN,
53 where the versions prefixed with "i" return an int, those prefixed with
54 "l" return a long and those prefixed with "ll" return a long long.
56 Also define operand lists:
58 X<FN>F for all float functions, in the order i, l, ll
59 X<FN> for all double functions, in the same order
60 X<FN>L for all long double functions, in the same order. */
61 #define DEFINE_INT_AND_FLOAT_ROUND_FN(FN) \
62 (define_operator_list X##FN##F BUILT_IN_I##FN##F \
65 (define_operator_list X##FN BUILT_IN_I##FN \
68 (define_operator_list X##FN##L BUILT_IN_I##FN##L \
72 DEFINE_INT_AND_FLOAT_ROUND_FN (FLOOR)
73 DEFINE_INT_AND_FLOAT_ROUND_FN (CEIL)
74 DEFINE_INT_AND_FLOAT_ROUND_FN (ROUND)
75 DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
77 /* Simplifications of operations with one constant operand and
78 simplifications to constants or single values. */
80 (for op (plus pointer_plus minus bit_ior bit_xor)
85 /* 0 +p index -> (type)index */
87 (pointer_plus integer_zerop @1)
88 (non_lvalue (convert @1)))
90 /* See if ARG1 is zero and X + ARG1 reduces to X.
91 Likewise if the operands are reversed. */
93 (plus:c @0 real_zerop@1)
94 (if (fold_real_zero_addition_p (type, @1, 0))
97 /* See if ARG1 is zero and X - ARG1 reduces to X. */
99 (minus @0 real_zerop@1)
100 (if (fold_real_zero_addition_p (type, @1, 1))
104 This is unsafe for certain floats even in non-IEEE formats.
105 In IEEE, it is unsafe because it does wrong for NaNs.
106 Also note that operand_equal_p is always false if an operand
110 (if (!FLOAT_TYPE_P (type) || !HONOR_NANS (type))
111 { build_zero_cst (type); }))
114 (mult @0 integer_zerop@1)
117 /* Maybe fold x * 0 to 0. The expressions aren't the same
118 when x is NaN, since x * 0 is also NaN. Nor are they the
119 same in modes with signed zeros, since multiplying a
120 negative value by 0 gives -0, not +0. */
122 (mult @0 real_zerop@1)
123 (if (!HONOR_NANS (type) && !HONOR_SIGNED_ZEROS (type))
126 /* In IEEE floating point, x*1 is not equivalent to x for snans.
127 Likewise for complex arithmetic with signed zeros. */
130 (if (!HONOR_SNANS (type)
131 && (!HONOR_SIGNED_ZEROS (type)
132 || !COMPLEX_FLOAT_TYPE_P (type)))
135 /* Transform x * -1.0 into -x. */
137 (mult @0 real_minus_onep)
138 (if (!HONOR_SNANS (type)
139 && (!HONOR_SIGNED_ZEROS (type)
140 || !COMPLEX_FLOAT_TYPE_P (type)))
143 /* Make sure to preserve divisions by zero. This is the reason why
144 we don't simplify x / x to 1 or 0 / x to 0. */
145 (for op (mult trunc_div ceil_div floor_div round_div exact_div)
150 (for div (trunc_div ceil_div floor_div round_div exact_div)
153 (div @0 integer_minus_onep@1)
154 (if (!TYPE_UNSIGNED (type))
156 /* X / abs (X) is X < 0 ? -1 : 1. */
159 (if (INTEGRAL_TYPE_P (type)
160 && TYPE_OVERFLOW_UNDEFINED (type))
161 (cond (lt @0 { build_zero_cst (type); })
162 { build_minus_one_cst (type); } { build_one_cst (type); })))
165 (div:C @0 (negate @0))
166 (if ((INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
167 && TYPE_OVERFLOW_UNDEFINED (type))
168 { build_minus_one_cst (type); })))
170 /* For unsigned integral types, FLOOR_DIV_EXPR is the same as
171 TRUNC_DIV_EXPR. Rewrite into the latter in this case. */
174 (if ((INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
175 && TYPE_UNSIGNED (type))
178 /* Combine two successive divisions. Note that combining ceil_div
179 and floor_div is trickier and combining round_div even more so. */
180 (for div (trunc_div exact_div)
182 (div (div @0 INTEGER_CST@1) INTEGER_CST@2)
185 wide_int mul = wi::mul (@1, @2, TYPE_SIGN (type), &overflow_p);
188 (div @0 { wide_int_to_tree (type, mul); })
189 (if (TYPE_UNSIGNED (type)
190 || mul != wi::min_value (TYPE_PRECISION (type), SIGNED))
191 { build_zero_cst (type); })))))
193 /* Optimize A / A to 1.0 if we don't care about
194 NaNs or Infinities. */
197 (if (FLOAT_TYPE_P (type)
198 && ! HONOR_NANS (type)
199 && ! HONOR_INFINITIES (type))
200 { build_one_cst (type); }))
202 /* Optimize -A / A to -1.0 if we don't care about
203 NaNs or Infinities. */
205 (rdiv:C @0 (negate @0))
206 (if (FLOAT_TYPE_P (type)
207 && ! HONOR_NANS (type)
208 && ! HONOR_INFINITIES (type))
209 { build_minus_one_cst (type); }))
211 /* PR71078: x / abs(x) -> copysign (1.0, x) */
213 (rdiv:C (convert? @0) (convert? (abs @0)))
214 (if (SCALAR_FLOAT_TYPE_P (type)
215 && ! HONOR_NANS (type)
216 && ! HONOR_INFINITIES (type))
218 (if (types_match (type, float_type_node))
219 (BUILT_IN_COPYSIGNF { build_one_cst (type); } (convert @0)))
220 (if (types_match (type, double_type_node))
221 (BUILT_IN_COPYSIGN { build_one_cst (type); } (convert @0)))
222 (if (types_match (type, long_double_type_node))
223 (BUILT_IN_COPYSIGNL { build_one_cst (type); } (convert @0))))))
225 /* In IEEE floating point, x/1 is not equivalent to x for snans. */
228 (if (!HONOR_SNANS (type))
231 /* In IEEE floating point, x/-1 is not equivalent to -x for snans. */
233 (rdiv @0 real_minus_onep)
234 (if (!HONOR_SNANS (type))
237 (if (flag_reciprocal_math)
238 /* Convert (A/B)/C to A/(B*C) */
240 (rdiv (rdiv:s @0 @1) @2)
241 (rdiv @0 (mult @1 @2)))
243 /* Convert A/(B/C) to (A/B)*C */
245 (rdiv @0 (rdiv:s @1 @2))
246 (mult (rdiv @0 @1) @2)))
248 /* Optimize (X & (-A)) / A where A is a power of 2, to X >> log2(A) */
249 (for div (trunc_div ceil_div floor_div round_div exact_div)
251 (div (convert? (bit_and @0 INTEGER_CST@1)) INTEGER_CST@2)
252 (if (integer_pow2p (@2)
253 && tree_int_cst_sgn (@2) > 0
254 && wi::add (@2, @1) == 0
255 && tree_nop_conversion_p (type, TREE_TYPE (@0)))
256 (rshift (convert @0) { build_int_cst (integer_type_node,
257 wi::exact_log2 (@2)); }))))
259 /* If ARG1 is a constant, we can convert this to a multiply by the
260 reciprocal. This does not have the same rounding properties,
261 so only do this if -freciprocal-math. We can actually
262 always safely do it if ARG1 is a power of two, but it's hard to
263 tell if it is or not in a portable manner. */
264 (for cst (REAL_CST COMPLEX_CST VECTOR_CST)
268 (if (flag_reciprocal_math
271 { tree tem = const_binop (RDIV_EXPR, type, build_one_cst (type), @1); }
273 (mult @0 { tem; } )))
274 (if (cst != COMPLEX_CST)
275 (with { tree inverse = exact_inverse (type, @1); }
277 (mult @0 { inverse; } ))))))))
279 /* Same applies to modulo operations, but fold is inconsistent here
280 and simplifies 0 % x to 0, only preserving literal 0 % 0. */
281 (for mod (ceil_mod floor_mod round_mod trunc_mod)
282 /* 0 % X is always zero. */
284 (mod integer_zerop@0 @1)
285 /* But not for 0 % 0 so that we can get the proper warnings and errors. */
286 (if (!integer_zerop (@1))
288 /* X % 1 is always zero. */
290 (mod @0 integer_onep)
291 { build_zero_cst (type); })
292 /* X % -1 is zero. */
294 (mod @0 integer_minus_onep@1)
295 (if (!TYPE_UNSIGNED (type))
296 { build_zero_cst (type); }))
297 /* (X % Y) % Y is just X % Y. */
299 (mod (mod@2 @0 @1) @1)
301 /* From extract_muldiv_1: (X * C1) % C2 is zero if C1 is a multiple of C2. */
303 (mod (mult @0 INTEGER_CST@1) INTEGER_CST@2)
304 (if (ANY_INTEGRAL_TYPE_P (type)
305 && TYPE_OVERFLOW_UNDEFINED (type)
306 && wi::multiple_of_p (@1, @2, TYPE_SIGN (type)))
307 { build_zero_cst (type); })))
309 /* X % -C is the same as X % C. */
311 (trunc_mod @0 INTEGER_CST@1)
312 (if (TYPE_SIGN (type) == SIGNED
313 && !TREE_OVERFLOW (@1)
315 && !TYPE_OVERFLOW_TRAPS (type)
316 /* Avoid this transformation if C is INT_MIN, i.e. C == -C. */
317 && !sign_bit_p (@1, @1))
318 (trunc_mod @0 (negate @1))))
320 /* X % -Y is the same as X % Y. */
322 (trunc_mod @0 (convert? (negate @1)))
323 (if (INTEGRAL_TYPE_P (type)
324 && !TYPE_UNSIGNED (type)
325 && !TYPE_OVERFLOW_TRAPS (type)
326 && tree_nop_conversion_p (type, TREE_TYPE (@1))
327 /* Avoid this transformation if X might be INT_MIN or
328 Y might be -1, because we would then change valid
329 INT_MIN % -(-1) into invalid INT_MIN % -1. */
330 && (expr_not_equal_to (@0, TYPE_MIN_VALUE (type))
331 || expr_not_equal_to (@1, wi::minus_one (TYPE_PRECISION
333 (trunc_mod @0 (convert @1))))
335 /* X - (X / Y) * Y is the same as X % Y. */
337 (minus (convert1? @0) (convert2? (mult:c (trunc_div @@0 @@1) @1)))
338 (if (INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
339 (convert (trunc_mod @0 @1))))
341 /* Optimize TRUNC_MOD_EXPR by a power of two into a BIT_AND_EXPR,
342 i.e. "X % C" into "X & (C - 1)", if X and C are positive.
343 Also optimize A % (C << N) where C is a power of 2,
344 to A & ((C << N) - 1). */
345 (match (power_of_two_cand @1)
347 (match (power_of_two_cand @1)
348 (lshift INTEGER_CST@1 @2))
349 (for mod (trunc_mod floor_mod)
351 (mod @0 (convert?@3 (power_of_two_cand@1 @2)))
352 (if ((TYPE_UNSIGNED (type)
353 || tree_expr_nonnegative_p (@0))
354 && tree_nop_conversion_p (type, TREE_TYPE (@3))
355 && integer_pow2p (@2) && tree_int_cst_sgn (@2) > 0)
356 (bit_and @0 (convert (minus @1 { build_int_cst (TREE_TYPE (@1), 1); }))))))
358 /* Simplify (unsigned t * 2)/2 -> unsigned t & 0x7FFFFFFF. */
360 (trunc_div (mult @0 integer_pow2p@1) @1)
361 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
362 (bit_and @0 { wide_int_to_tree
363 (type, wi::mask (TYPE_PRECISION (type) - wi::exact_log2 (@1),
364 false, TYPE_PRECISION (type))); })))
366 /* Simplify (unsigned t / 2) * 2 -> unsigned t & ~1. */
368 (mult (trunc_div @0 integer_pow2p@1) @1)
369 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
370 (bit_and @0 (negate @1))))
372 /* Simplify (t * 2) / 2) -> t. */
373 (for div (trunc_div ceil_div floor_div round_div exact_div)
375 (div (mult @0 @1) @1)
376 (if (ANY_INTEGRAL_TYPE_P (type)
377 && TYPE_OVERFLOW_UNDEFINED (type))
381 /* Simplify cos(-x) and cos(|x|) -> cos(x). Similarly for cosh. */
386 /* Simplify pow(-x, y) and pow(|x|,y) -> pow(x,y) if y is an even integer. */
389 (pows (op @0) REAL_CST@1)
390 (with { HOST_WIDE_INT n; }
391 (if (real_isinteger (&TREE_REAL_CST (@1), &n) && (n & 1) == 0)
393 /* Likewise for powi. */
396 (pows (op @0) INTEGER_CST@1)
397 (if (wi::bit_and (@1, 1) == 0)
399 /* Strip negate and abs from both operands of hypot. */
407 /* copysign(-x, y) and copysign(abs(x), y) -> copysign(x, y). */
408 (for copysigns (COPYSIGN)
410 (copysigns (op @0) @1)
413 /* abs(x)*abs(x) -> x*x. Should be valid for all types. */
418 /* cos(copysign(x, y)) -> cos(x). Similarly for cosh. */
422 (coss (copysigns @0 @1))
425 /* pow(copysign(x, y), z) -> pow(x, z) if z is an even integer. */
429 (pows (copysigns @0 @2) REAL_CST@1)
430 (with { HOST_WIDE_INT n; }
431 (if (real_isinteger (&TREE_REAL_CST (@1), &n) && (n & 1) == 0)
433 /* Likewise for powi. */
437 (pows (copysigns @0 @2) INTEGER_CST@1)
438 (if (wi::bit_and (@1, 1) == 0)
443 /* hypot(copysign(x, y), z) -> hypot(x, z). */
445 (hypots (copysigns @0 @1) @2)
447 /* hypot(x, copysign(y, z)) -> hypot(x, y). */
449 (hypots @0 (copysigns @1 @2))
452 /* copysign(x, CST) -> [-]abs (x). */
453 (for copysigns (COPYSIGN)
455 (copysigns @0 REAL_CST@1)
456 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
460 /* copysign(copysign(x, y), z) -> copysign(x, z). */
461 (for copysigns (COPYSIGN)
463 (copysigns (copysigns @0 @1) @2)
466 /* copysign(x,y)*copysign(x,y) -> x*x. */
467 (for copysigns (COPYSIGN)
469 (mult (copysigns@2 @0 @1) @2)
472 /* ccos(-x) -> ccos(x). Similarly for ccosh. */
473 (for ccoss (CCOS CCOSH)
478 /* cabs(-x) and cos(conj(x)) -> cabs(x). */
479 (for ops (conj negate)
485 /* Fold (a * (1 << b)) into (a << b) */
487 (mult:c @0 (convert? (lshift integer_onep@1 @2)))
488 (if (! FLOAT_TYPE_P (type)
489 && tree_nop_conversion_p (type, TREE_TYPE (@1)))
492 /* Fold (C1/X)*C2 into (C1*C2)/X. */
494 (mult (rdiv@3 REAL_CST@0 @1) REAL_CST@2)
495 (if (flag_associative_math
498 { tree tem = const_binop (MULT_EXPR, type, @0, @2); }
500 (rdiv { tem; } @1)))))
502 /* Convert C1/(X*C2) into (C1/C2)/X */
504 (rdiv REAL_CST@0 (mult @1 REAL_CST@2))
505 (if (flag_reciprocal_math)
507 { tree tem = const_binop (RDIV_EXPR, type, @0, @2); }
509 (rdiv { tem; } @1)))))
511 /* Simplify ~X & X as zero. */
513 (bit_and:c (convert? @0) (convert? (bit_not @0)))
514 { build_zero_cst (type); })
516 /* PR71636: Transform x & ((1U << b) - 1) -> x & ~(~0U << b); */
518 (bit_and:c @0 (plus:s (lshift:s integer_onep @1) integer_minus_onep))
519 (if (TYPE_UNSIGNED (type))
520 (bit_and @0 (bit_not (lshift { build_all_ones_cst (type); } @1)))))
522 /* PR35691: Transform
523 (x == 0 & y == 0) -> (x | typeof(x)(y)) == 0.
524 (x != 0 | y != 0) -> (x | typeof(x)(y)) != 0. */
525 (for bitop (bit_and bit_ior)
528 (bitop (cmp @0 integer_zerop@2) (cmp @1 integer_zerop))
529 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
530 && INTEGRAL_TYPE_P (TREE_TYPE (@1))
531 && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
532 (cmp (bit_ior @0 (convert @1)) @2))))
534 /* Fold (A & ~B) - (A & B) into (A ^ B) - B. */
536 (minus (bit_and:cs @0 (bit_not @1)) (bit_and:cs @0 @1))
537 (minus (bit_xor @0 @1) @1))
539 (minus (bit_and:s @0 INTEGER_CST@2) (bit_and:s @0 INTEGER_CST@1))
540 (if (wi::bit_not (@2) == @1)
541 (minus (bit_xor @0 @1) @1)))
543 /* Fold (A & B) - (A & ~B) into B - (A ^ B). */
545 (minus (bit_and:cs @0 @1) (bit_and:cs @0 (bit_not @1)))
546 (minus @1 (bit_xor @0 @1)))
548 /* Simplify (X & ~Y) | (~X & Y) -> X ^ Y. */
550 (bit_ior (bit_and:c @0 (bit_not @1)) (bit_and:c (bit_not @0) @1))
553 (bit_ior:c (bit_and @0 INTEGER_CST@2) (bit_and (bit_not @0) INTEGER_CST@1))
554 (if (wi::bit_not (@2) == @1)
557 /* PR53979: Transform ((a ^ b) | a) -> (a | b) */
559 (bit_ior:c (bit_xor:c @0 @1) @0)
562 /* Simplify (~X & Y) to X ^ Y if we know that (X & ~Y) is 0. */
565 (bit_and (bit_not SSA_NAME@0) INTEGER_CST@1)
566 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
567 && (get_nonzero_bits (@0) & wi::bit_not (@1)) == 0)
571 /* X % Y is smaller than Y. */
574 (cmp (trunc_mod @0 @1) @1)
575 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
576 { constant_boolean_node (cmp == LT_EXPR, type); })))
579 (cmp @1 (trunc_mod @0 @1))
580 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
581 { constant_boolean_node (cmp == GT_EXPR, type); })))
585 (bit_ior @0 integer_all_onesp@1)
590 (bit_ior @0 integer_zerop)
595 (bit_and @0 integer_zerop@1)
601 (for op (bit_ior bit_xor plus)
603 (op:c (convert? @0) (convert? (bit_not @0)))
604 (convert { build_all_ones_cst (TREE_TYPE (@0)); })))
609 { build_zero_cst (type); })
611 /* Canonicalize X ^ ~0 to ~X. */
613 (bit_xor @0 integer_all_onesp@1)
618 (bit_and @0 integer_all_onesp)
621 /* x & x -> x, x | x -> x */
622 (for bitop (bit_and bit_ior)
627 /* x & C -> x if we know that x & ~C == 0. */
630 (bit_and SSA_NAME@0 INTEGER_CST@1)
631 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
632 && (get_nonzero_bits (@0) & wi::bit_not (@1)) == 0)
636 /* x + (x & 1) -> (x + 1) & ~1 */
638 (plus:c @0 (bit_and:s @0 integer_onep@1))
639 (bit_and (plus @0 @1) (bit_not @1)))
641 /* x & ~(x & y) -> x & ~y */
642 /* x | ~(x | y) -> x | ~y */
643 (for bitop (bit_and bit_ior)
645 (bitop:c @0 (bit_not (bitop:cs @0 @1)))
646 (bitop @0 (bit_not @1))))
648 /* (x | y) & ~x -> y & ~x */
649 /* (x & y) | ~x -> y | ~x */
650 (for bitop (bit_and bit_ior)
651 rbitop (bit_ior bit_and)
653 (bitop:c (rbitop:c @0 @1) (bit_not@2 @0))
656 /* (x & y) ^ (x | y) -> x ^ y */
658 (bit_xor:c (bit_and @0 @1) (bit_ior @0 @1))
661 /* (x ^ y) ^ (x | y) -> x & y */
663 (bit_xor:c (bit_xor @0 @1) (bit_ior @0 @1))
666 /* (x & y) + (x ^ y) -> x | y */
667 /* (x & y) | (x ^ y) -> x | y */
668 /* (x & y) ^ (x ^ y) -> x | y */
669 (for op (plus bit_ior bit_xor)
671 (op:c (bit_and @0 @1) (bit_xor @0 @1))
674 /* (x & y) + (x | y) -> x + y */
676 (plus:c (bit_and @0 @1) (bit_ior @0 @1))
679 /* (x + y) - (x | y) -> x & y */
681 (minus (plus @0 @1) (bit_ior @0 @1))
682 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
683 && !TYPE_SATURATING (type))
686 /* (x + y) - (x & y) -> x | y */
688 (minus (plus @0 @1) (bit_and @0 @1))
689 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
690 && !TYPE_SATURATING (type))
693 /* (x | y) - (x ^ y) -> x & y */
695 (minus (bit_ior @0 @1) (bit_xor @0 @1))
698 /* (x | y) - (x & y) -> x ^ y */
700 (minus (bit_ior @0 @1) (bit_and @0 @1))
703 /* (x | y) & ~(x & y) -> x ^ y */
705 (bit_and:c (bit_ior @0 @1) (bit_not (bit_and @0 @1)))
708 /* (x | y) & (~x ^ y) -> x & y */
710 (bit_and:c (bit_ior:c @0 @1) (bit_xor:c @1 (bit_not @0)))
713 /* ~x & ~y -> ~(x | y)
714 ~x | ~y -> ~(x & y) */
715 (for op (bit_and bit_ior)
716 rop (bit_ior bit_and)
718 (op (convert1? (bit_not @0)) (convert2? (bit_not @1)))
719 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
720 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
721 (bit_not (rop (convert @0) (convert @1))))))
723 /* If we are XORing or adding two BIT_AND_EXPR's, both of which are and'ing
724 with a constant, and the two constants have no bits in common,
725 we should treat this as a BIT_IOR_EXPR since this may produce more
727 (for op (bit_xor plus)
729 (op (convert1? (bit_and@4 @0 INTEGER_CST@1))
730 (convert2? (bit_and@5 @2 INTEGER_CST@3)))
731 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
732 && tree_nop_conversion_p (type, TREE_TYPE (@2))
733 && wi::bit_and (@1, @3) == 0)
734 (bit_ior (convert @4) (convert @5)))))
736 /* (X | Y) ^ X -> Y & ~ X*/
738 (bit_xor:c (convert1? (bit_ior:c @@0 @1)) (convert2? @0))
739 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
740 (convert (bit_and @1 (bit_not @0)))))
742 /* Convert ~X ^ ~Y to X ^ Y. */
744 (bit_xor (convert1? (bit_not @0)) (convert2? (bit_not @1)))
745 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
746 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
747 (bit_xor (convert @0) (convert @1))))
749 /* Convert ~X ^ C to X ^ ~C. */
751 (bit_xor (convert? (bit_not @0)) INTEGER_CST@1)
752 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
753 (bit_xor (convert @0) (bit_not @1))))
755 /* Fold (X & Y) ^ Y and (X ^ Y) & Y as ~X & Y. */
756 (for opo (bit_and bit_xor)
757 opi (bit_xor bit_and)
759 (opo:c (opi:c @0 @1) @1)
760 (bit_and (bit_not @0) @1)))
762 /* Given a bit-wise operation CODE applied to ARG0 and ARG1, see if both
763 operands are another bit-wise operation with a common input. If so,
764 distribute the bit operations to save an operation and possibly two if
765 constants are involved. For example, convert
766 (A | B) & (A | C) into A | (B & C)
767 Further simplification will occur if B and C are constants. */
768 (for op (bit_and bit_ior bit_xor)
769 rop (bit_ior bit_and bit_and)
771 (op (convert? (rop:c @@0 @1)) (convert? (rop:c @0 @2)))
772 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
773 && tree_nop_conversion_p (type, TREE_TYPE (@2)))
774 (rop (convert @0) (op (convert @1) (convert @2))))))
776 /* Some simple reassociation for bit operations, also handled in reassoc. */
777 /* (X & Y) & Y -> X & Y
778 (X | Y) | Y -> X | Y */
779 (for op (bit_and bit_ior)
781 (op:c (convert1?@2 (op:c @0 @@1)) (convert2? @1))
783 /* (X ^ Y) ^ Y -> X */
785 (bit_xor:c (convert1? (bit_xor:c @0 @@1)) (convert2? @1))
787 /* (X & Y) & (X & Z) -> (X & Y) & Z
788 (X | Y) | (X | Z) -> (X | Y) | Z */
789 (for op (bit_and bit_ior)
791 (op:c (convert1?@3 (op:c@4 @0 @1)) (convert2?@5 (op:c@6 @0 @2)))
792 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
793 && tree_nop_conversion_p (type, TREE_TYPE (@2)))
794 (if (single_use (@5) && single_use (@6))
796 (if (single_use (@3) && single_use (@4))
797 (op (convert @1) @5))))))
798 /* (X ^ Y) ^ (X ^ Z) -> Y ^ Z */
800 (bit_xor (convert1? (bit_xor:c @0 @1)) (convert2? (bit_xor:c @0 @2)))
801 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
802 && tree_nop_conversion_p (type, TREE_TYPE (@2)))
803 (bit_xor (convert @1) (convert @2))))
812 (abs tree_expr_nonnegative_p@0)
815 /* A few cases of fold-const.c negate_expr_p predicate. */
818 (if ((INTEGRAL_TYPE_P (type)
819 && TYPE_OVERFLOW_WRAPS (type))
820 || (!TYPE_OVERFLOW_SANITIZED (type)
821 && may_negate_without_overflow_p (t)))))
826 (if (!TYPE_OVERFLOW_SANITIZED (type))))
829 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (t)))))
830 /* VECTOR_CST handling of non-wrapping types would recurse in unsupported
834 (if (FLOAT_TYPE_P (TREE_TYPE (type)) || TYPE_OVERFLOW_WRAPS (type))))
836 /* (-A) * (-B) -> A * B */
838 (mult:c (convert1? (negate @0)) (convert2? negate_expr_p@1))
839 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
840 && tree_nop_conversion_p (type, TREE_TYPE (@1)))
841 (mult (convert @0) (convert (negate @1)))))
843 /* -(A + B) -> (-B) - A. */
845 (negate (plus:c @0 negate_expr_p@1))
846 (if (!HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type))
847 && !HONOR_SIGNED_ZEROS (element_mode (type)))
848 (minus (negate @1) @0)))
850 /* A - B -> A + (-B) if B is easily negatable. */
852 (minus @0 negate_expr_p@1)
853 (if (!FIXED_POINT_TYPE_P (type))
854 (plus @0 (negate @1))))
856 /* Try to fold (type) X op CST -> (type) (X op ((type-x) CST))
858 For bitwise binary operations apply operand conversions to the
859 binary operation result instead of to the operands. This allows
860 to combine successive conversions and bitwise binary operations.
861 We combine the above two cases by using a conditional convert. */
862 (for bitop (bit_and bit_ior bit_xor)
864 (bitop (convert @0) (convert? @1))
865 (if (((TREE_CODE (@1) == INTEGER_CST
866 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
867 && int_fits_type_p (@1, TREE_TYPE (@0)))
868 || types_match (@0, @1))
869 /* ??? This transform conflicts with fold-const.c doing
870 Convert (T)(x & c) into (T)x & (T)c, if c is an integer
871 constants (if x has signed type, the sign bit cannot be set
872 in c). This folds extension into the BIT_AND_EXPR.
873 Restrict it to GIMPLE to avoid endless recursions. */
874 && (bitop != BIT_AND_EXPR || GIMPLE)
875 && (/* That's a good idea if the conversion widens the operand, thus
876 after hoisting the conversion the operation will be narrower. */
877 TYPE_PRECISION (TREE_TYPE (@0)) < TYPE_PRECISION (type)
878 /* It's also a good idea if the conversion is to a non-integer
880 || GET_MODE_CLASS (TYPE_MODE (type)) != MODE_INT
881 /* Or if the precision of TO is not the same as the precision
883 || TYPE_PRECISION (type) != GET_MODE_PRECISION (TYPE_MODE (type))))
884 (convert (bitop @0 (convert @1))))))
886 (for bitop (bit_and bit_ior)
887 rbitop (bit_ior bit_and)
888 /* (x | y) & x -> x */
889 /* (x & y) | x -> x */
891 (bitop:c (rbitop:c @0 @1) @0)
893 /* (~x | y) & x -> x & y */
894 /* (~x & y) | x -> x | y */
896 (bitop:c (rbitop:c (bit_not @0) @1) @0)
899 /* (x | CST1) & CST2 -> (x & CST2) | (CST1 & CST2) */
901 (bit_and (bit_ior @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
902 (bit_ior (bit_and @0 @2) (bit_and @1 @2)))
904 /* Combine successive equal operations with constants. */
905 (for bitop (bit_and bit_ior bit_xor)
907 (bitop (bitop @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
908 (bitop @0 (bitop @1 @2))))
910 /* Try simple folding for X op !X, and X op X with the help
911 of the truth_valued_p and logical_inverted_value predicates. */
912 (match truth_valued_p
914 (if (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1)))
915 (for op (tcc_comparison truth_and truth_andif truth_or truth_orif truth_xor)
916 (match truth_valued_p
918 (match truth_valued_p
921 (match (logical_inverted_value @0)
923 (match (logical_inverted_value @0)
924 (bit_not truth_valued_p@0))
925 (match (logical_inverted_value @0)
926 (eq @0 integer_zerop))
927 (match (logical_inverted_value @0)
928 (ne truth_valued_p@0 integer_truep))
929 (match (logical_inverted_value @0)
930 (bit_xor truth_valued_p@0 integer_truep))
934 (bit_and:c @0 (logical_inverted_value @0))
935 { build_zero_cst (type); })
936 /* X | !X and X ^ !X -> 1, , if X is truth-valued. */
937 (for op (bit_ior bit_xor)
939 (op:c truth_valued_p@0 (logical_inverted_value @0))
940 { constant_boolean_node (true, type); }))
941 /* X ==/!= !X is false/true. */
944 (op:c truth_valued_p@0 (logical_inverted_value @0))
945 { constant_boolean_node (op == NE_EXPR ? true : false, type); }))
947 /* If arg1 and arg2 are booleans (or any single bit type)
948 then try to simplify:
955 But only do this if our result feeds into a comparison as
956 this transformation is not always a win, particularly on
957 targets with and-not instructions.
958 -> simplify_bitwise_binary_boolean */
960 (ne (bit_and:c (bit_not @0) @1) integer_zerop)
961 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
962 && TYPE_PRECISION (TREE_TYPE (@1)) == 1)
963 (if (TYPE_UNSIGNED (TREE_TYPE (@1)))
967 (ne (bit_ior:c (bit_not @0) @1) integer_zerop)
968 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
969 && TYPE_PRECISION (TREE_TYPE (@1)) == 1)
970 (if (TYPE_UNSIGNED (TREE_TYPE (@1)))
976 (bit_not (bit_not @0))
979 /* Convert ~ (-A) to A - 1. */
981 (bit_not (convert? (negate @0)))
982 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
983 || !TYPE_UNSIGNED (TREE_TYPE (@0)))
984 (convert (minus @0 { build_each_one_cst (TREE_TYPE (@0)); }))))
986 /* Convert ~ (A - 1) or ~ (A + -1) to -A. */
988 (bit_not (convert? (minus @0 integer_each_onep)))
989 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
990 || !TYPE_UNSIGNED (TREE_TYPE (@0)))
991 (convert (negate @0))))
993 (bit_not (convert? (plus @0 integer_all_onesp)))
994 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
995 || !TYPE_UNSIGNED (TREE_TYPE (@0)))
996 (convert (negate @0))))
998 /* Part of convert ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify. */
1000 (bit_not (convert? (bit_xor @0 INTEGER_CST@1)))
1001 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1002 (convert (bit_xor @0 (bit_not @1)))))
1004 (bit_not (convert? (bit_xor:c (bit_not @0) @1)))
1005 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1006 (convert (bit_xor @0 @1))))
1008 /* (x & ~m) | (y & m) -> ((x ^ y) & m) ^ x */
1010 (bit_ior:c (bit_and:cs @0 (bit_not @2)) (bit_and:cs @1 @2))
1011 (bit_xor (bit_and (bit_xor @0 @1) @2) @0))
1013 /* Fold A - (A & B) into ~B & A. */
1015 (minus (convert1? @0) (convert2?:s (bit_and:cs @@0 @1)))
1016 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
1017 && tree_nop_conversion_p (type, TREE_TYPE (@1)))
1018 (convert (bit_and (bit_not @1) @0))))
1022 /* ((X inner_op C0) outer_op C1)
1023 With X being a tree where value_range has reasoned certain bits to always be
1024 zero throughout its computed value range,
1025 inner_op = {|,^}, outer_op = {|,^} and inner_op != outer_op
1026 where zero_mask has 1's for all bits that are sure to be 0 in
1028 if (inner_op == '^') C0 &= ~C1;
1029 if ((C0 & ~zero_mask) == 0) then emit (X outer_op (C0 outer_op C1)
1030 if ((C1 & ~zero_mask) == 0) then emit (X inner_op (C0 outer_op C1)
1032 (for inner_op (bit_ior bit_xor)
1033 outer_op (bit_xor bit_ior)
1036 (inner_op:s @2 INTEGER_CST@0) INTEGER_CST@1)
1040 wide_int zero_mask_not;
1044 if (TREE_CODE (@2) == SSA_NAME)
1045 zero_mask_not = get_nonzero_bits (@2);
1049 if (inner_op == BIT_XOR_EXPR)
1051 C0 = wi::bit_and_not (@0, @1);
1052 cst_emit = wi::bit_or (C0, @1);
1057 cst_emit = wi::bit_xor (@0, @1);
1060 (if (!fail && wi::bit_and (C0, zero_mask_not) == 0)
1061 (outer_op @2 { wide_int_to_tree (type, cst_emit); })
1062 (if (!fail && wi::bit_and (@1, zero_mask_not) == 0)
1063 (inner_op @2 { wide_int_to_tree (type, cst_emit); }))))))
1065 /* Associate (p +p off1) +p off2 as (p +p (off1 + off2)). */
1067 (pointer_plus (pointer_plus:s @0 @1) @3)
1068 (pointer_plus @0 (plus @1 @3)))
1074 tem4 = (unsigned long) tem3;
1079 (pointer_plus @0 (convert?@2 (minus@3 (convert @1) (convert @0))))
1080 /* Conditionally look through a sign-changing conversion. */
1081 (if (TYPE_PRECISION (TREE_TYPE (@2)) == TYPE_PRECISION (TREE_TYPE (@3))
1082 && ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@1)))
1083 || (GENERIC && type == TREE_TYPE (@1))))
1087 tem = (sizetype) ptr;
1091 and produce the simpler and easier to analyze with respect to alignment
1092 ... = ptr & ~algn; */
1094 (pointer_plus @0 (negate (bit_and (convert @0) INTEGER_CST@1)))
1095 (with { tree algn = wide_int_to_tree (TREE_TYPE (@0), wi::bit_not (@1)); }
1096 (bit_and @0 { algn; })))
1098 /* Try folding difference of addresses. */
1100 (minus (convert ADDR_EXPR@0) (convert @1))
1101 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1102 (with { HOST_WIDE_INT diff; }
1103 (if (ptr_difference_const (@0, @1, &diff))
1104 { build_int_cst_type (type, diff); }))))
1106 (minus (convert @0) (convert ADDR_EXPR@1))
1107 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1108 (with { HOST_WIDE_INT diff; }
1109 (if (ptr_difference_const (@0, @1, &diff))
1110 { build_int_cst_type (type, diff); }))))
1112 /* If arg0 is derived from the address of an object or function, we may
1113 be able to fold this expression using the object or function's
1116 (bit_and (convert? @0) INTEGER_CST@1)
1117 (if (POINTER_TYPE_P (TREE_TYPE (@0))
1118 && tree_nop_conversion_p (type, TREE_TYPE (@0)))
1122 unsigned HOST_WIDE_INT bitpos;
1123 get_pointer_alignment_1 (@0, &align, &bitpos);
1125 (if (wi::ltu_p (@1, align / BITS_PER_UNIT))
1126 { wide_int_to_tree (type, wi::bit_and (@1, bitpos / BITS_PER_UNIT)); }))))
1129 /* We can't reassociate at all for saturating types. */
1130 (if (!TYPE_SATURATING (type))
1132 /* Contract negates. */
1133 /* A + (-B) -> A - B */
1135 (plus:c (convert1? @0) (convert2? (negate @1)))
1136 /* Apply STRIP_NOPS on @0 and the negate. */
1137 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
1138 && tree_nop_conversion_p (type, TREE_TYPE (@1))
1139 && !TYPE_OVERFLOW_SANITIZED (type))
1140 (minus (convert @0) (convert @1))))
1141 /* A - (-B) -> A + B */
1143 (minus (convert1? @0) (convert2? (negate @1)))
1144 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
1145 && tree_nop_conversion_p (type, TREE_TYPE (@1))
1146 && !TYPE_OVERFLOW_SANITIZED (type))
1147 (plus (convert @0) (convert @1))))
1150 (negate (convert? (negate @1)))
1151 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1152 && !TYPE_OVERFLOW_SANITIZED (type))
1155 /* We can't reassociate floating-point unless -fassociative-math
1156 or fixed-point plus or minus because of saturation to +-Inf. */
1157 (if ((!FLOAT_TYPE_P (type) || flag_associative_math)
1158 && !FIXED_POINT_TYPE_P (type))
1160 /* Match patterns that allow contracting a plus-minus pair
1161 irrespective of overflow issues. */
1162 /* (A +- B) - A -> +- B */
1163 /* (A +- B) -+ B -> A */
1164 /* A - (A +- B) -> -+ B */
1165 /* A +- (B -+ A) -> +- B */
1167 (minus (plus:c @0 @1) @0)
1170 (minus (minus @0 @1) @0)
1173 (plus:c (minus @0 @1) @1)
1176 (minus @0 (plus:c @0 @1))
1179 (minus @0 (minus @0 @1))
1182 /* (A +- CST) +- CST -> A + CST */
1183 (for outer_op (plus minus)
1184 (for inner_op (plus minus)
1186 (outer_op (inner_op @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
1187 /* If the constant operation overflows we cannot do the transform
1188 as we would introduce undefined overflow, for example
1189 with (a - 1) + INT_MIN. */
1190 (with { tree cst = const_binop (outer_op == inner_op
1191 ? PLUS_EXPR : MINUS_EXPR, type, @1, @2); }
1192 (if (cst && !TREE_OVERFLOW (cst))
1193 (inner_op @0 { cst; } ))))))
1195 /* (CST - A) +- CST -> CST - A */
1196 (for outer_op (plus minus)
1198 (outer_op (minus CONSTANT_CLASS_P@1 @0) CONSTANT_CLASS_P@2)
1199 (with { tree cst = const_binop (outer_op, type, @1, @2); }
1200 (if (cst && !TREE_OVERFLOW (cst))
1201 (minus { cst; } @0)))))
1205 (plus:c (bit_not @0) @0)
1206 (if (!TYPE_OVERFLOW_TRAPS (type))
1207 { build_all_ones_cst (type); }))
1211 (plus (convert? (bit_not @0)) integer_each_onep)
1212 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1213 (negate (convert @0))))
1217 (minus (convert? (negate @0)) integer_each_onep)
1218 (if (!TYPE_OVERFLOW_TRAPS (type)
1219 && tree_nop_conversion_p (type, TREE_TYPE (@0)))
1220 (bit_not (convert @0))))
1224 (minus integer_all_onesp @0)
1227 /* (T)(P + A) - (T)P -> (T) A */
1228 (for add (plus pointer_plus)
1230 (minus (convert (add @@0 @1))
1232 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
1233 /* For integer types, if A has a smaller type
1234 than T the result depends on the possible
1236 E.g. T=size_t, A=(unsigned)429497295, P>0.
1237 However, if an overflow in P + A would cause
1238 undefined behavior, we can assume that there
1240 || (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1241 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1242 /* For pointer types, if the conversion of A to the
1243 final type requires a sign- or zero-extension,
1244 then we have to punt - it is not defined which
1246 || (POINTER_TYPE_P (TREE_TYPE (@0))
1247 && TREE_CODE (@1) == INTEGER_CST
1248 && tree_int_cst_sign_bit (@1) == 0))
1251 /* (T)P - (T)(P + A) -> -(T) A */
1252 (for add (plus pointer_plus)
1255 (convert (add @@0 @1)))
1256 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
1257 /* For integer types, if A has a smaller type
1258 than T the result depends on the possible
1260 E.g. T=size_t, A=(unsigned)429497295, P>0.
1261 However, if an overflow in P + A would cause
1262 undefined behavior, we can assume that there
1264 || (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1265 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1266 /* For pointer types, if the conversion of A to the
1267 final type requires a sign- or zero-extension,
1268 then we have to punt - it is not defined which
1270 || (POINTER_TYPE_P (TREE_TYPE (@0))
1271 && TREE_CODE (@1) == INTEGER_CST
1272 && tree_int_cst_sign_bit (@1) == 0))
1273 (negate (convert @1)))))
1275 /* (T)(P + A) - (T)(P + B) -> (T)A - (T)B */
1276 (for add (plus pointer_plus)
1278 (minus (convert (add @@0 @1))
1279 (convert (add @0 @2)))
1280 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
1281 /* For integer types, if A has a smaller type
1282 than T the result depends on the possible
1284 E.g. T=size_t, A=(unsigned)429497295, P>0.
1285 However, if an overflow in P + A would cause
1286 undefined behavior, we can assume that there
1288 || (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1289 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1290 /* For pointer types, if the conversion of A to the
1291 final type requires a sign- or zero-extension,
1292 then we have to punt - it is not defined which
1294 || (POINTER_TYPE_P (TREE_TYPE (@0))
1295 && TREE_CODE (@1) == INTEGER_CST
1296 && tree_int_cst_sign_bit (@1) == 0
1297 && TREE_CODE (@2) == INTEGER_CST
1298 && tree_int_cst_sign_bit (@2) == 0))
1299 (minus (convert @1) (convert @2)))))))
1302 /* Simplifications of MIN_EXPR, MAX_EXPR, fmin() and fmax(). */
1304 (for minmax (min max FMIN FMAX)
1308 /* min(max(x,y),y) -> y. */
1310 (min:c (max:c @0 @1) @1)
1312 /* max(min(x,y),y) -> y. */
1314 (max:c (min:c @0 @1) @1)
1316 /* max(a,-a) -> abs(a). */
1318 (max:c @0 (negate @0))
1319 (if (TREE_CODE (type) != COMPLEX_TYPE
1320 && (! ANY_INTEGRAL_TYPE_P (type)
1321 || TYPE_OVERFLOW_UNDEFINED (type)))
1323 /* min(a,-a) -> -abs(a). */
1325 (min:c @0 (negate @0))
1326 (if (TREE_CODE (type) != COMPLEX_TYPE
1327 && (! ANY_INTEGRAL_TYPE_P (type)
1328 || TYPE_OVERFLOW_UNDEFINED (type)))
1333 (if (INTEGRAL_TYPE_P (type)
1334 && TYPE_MIN_VALUE (type)
1335 && operand_equal_p (@1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
1337 (if (INTEGRAL_TYPE_P (type)
1338 && TYPE_MAX_VALUE (type)
1339 && operand_equal_p (@1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
1344 (if (INTEGRAL_TYPE_P (type)
1345 && TYPE_MAX_VALUE (type)
1346 && operand_equal_p (@1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
1348 (if (INTEGRAL_TYPE_P (type)
1349 && TYPE_MIN_VALUE (type)
1350 && operand_equal_p (@1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
1353 /* (convert (minmax ((convert (x) c)))) -> minmax (x c) if x is promoted
1354 and the outer convert demotes the expression back to x's type. */
1355 (for minmax (min max)
1357 (convert (minmax@0 (convert @1) INTEGER_CST@2))
1358 (if (types_match (@1, type) && int_fits_type_p (@2, type)
1359 && TYPE_SIGN (TREE_TYPE (@0)) == TYPE_SIGN (type)
1360 && TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type))
1361 (minmax @1 (convert @2)))))
1363 (for minmax (FMIN FMAX)
1364 /* If either argument is NaN, return the other one. Avoid the
1365 transformation if we get (and honor) a signalling NaN. */
1367 (minmax:c @0 REAL_CST@1)
1368 (if (real_isnan (TREE_REAL_CST_PTR (@1))
1369 && (!HONOR_SNANS (@1) || !TREE_REAL_CST (@1).signalling))
1371 /* Convert fmin/fmax to MIN_EXPR/MAX_EXPR. C99 requires these
1372 functions to return the numeric arg if the other one is NaN.
1373 MIN and MAX don't honor that, so only transform if -ffinite-math-only
1374 is set. C99 doesn't require -0.0 to be handled, so we don't have to
1375 worry about it either. */
1376 (if (flag_finite_math_only)
1383 /* min (-A, -B) -> -max (A, B) */
1384 (for minmax (min max FMIN FMAX)
1385 maxmin (max min FMAX FMIN)
1387 (minmax (negate:s@2 @0) (negate:s@3 @1))
1388 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
1389 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1390 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
1391 (negate (maxmin @0 @1)))))
1392 /* MIN (~X, ~Y) -> ~MAX (X, Y)
1393 MAX (~X, ~Y) -> ~MIN (X, Y) */
1394 (for minmax (min max)
1397 (minmax (bit_not:s@2 @0) (bit_not:s@3 @1))
1398 (bit_not (maxmin @0 @1))))
1400 /* MIN (X, Y) == X -> X <= Y */
1401 (for minmax (min min max max)
1405 (cmp:c (minmax:c @0 @1) @0)
1406 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0)))
1408 /* MIN (X, 5) == 0 -> X == 0
1409 MIN (X, 5) == 7 -> false */
1412 (cmp (min @0 INTEGER_CST@1) INTEGER_CST@2)
1413 (if (wi::lt_p (@1, @2, TYPE_SIGN (TREE_TYPE (@0))))
1414 { constant_boolean_node (cmp == NE_EXPR, type); }
1415 (if (wi::gt_p (@1, @2, TYPE_SIGN (TREE_TYPE (@0))))
1419 (cmp (max @0 INTEGER_CST@1) INTEGER_CST@2)
1420 (if (wi::gt_p (@1, @2, TYPE_SIGN (TREE_TYPE (@0))))
1421 { constant_boolean_node (cmp == NE_EXPR, type); }
1422 (if (wi::lt_p (@1, @2, TYPE_SIGN (TREE_TYPE (@0))))
1424 /* MIN (X, C1) < C2 -> X < C2 || C1 < C2 */
1425 (for minmax (min min max max min min max max )
1426 cmp (lt le gt ge gt ge lt le )
1427 comb (bit_ior bit_ior bit_ior bit_ior bit_and bit_and bit_and bit_and)
1429 (cmp (minmax @0 INTEGER_CST@1) INTEGER_CST@2)
1430 (comb (cmp @0 @2) (cmp @1 @2))))
1432 /* Simplifications of shift and rotates. */
1434 (for rotate (lrotate rrotate)
1436 (rotate integer_all_onesp@0 @1)
1439 /* Optimize -1 >> x for arithmetic right shifts. */
1441 (rshift integer_all_onesp@0 @1)
1442 (if (!TYPE_UNSIGNED (type)
1443 && tree_expr_nonnegative_p (@1))
1446 /* Optimize (x >> c) << c into x & (-1<<c). */
1448 (lshift (rshift @0 INTEGER_CST@1) @1)
1449 (if (wi::ltu_p (@1, element_precision (type)))
1450 (bit_and @0 (lshift { build_minus_one_cst (type); } @1))))
1452 /* Optimize (x << c) >> c into x & ((unsigned)-1 >> c) for unsigned
1455 (rshift (lshift @0 INTEGER_CST@1) @1)
1456 (if (TYPE_UNSIGNED (type)
1457 && (wi::ltu_p (@1, element_precision (type))))
1458 (bit_and @0 (rshift { build_minus_one_cst (type); } @1))))
1460 (for shiftrotate (lrotate rrotate lshift rshift)
1462 (shiftrotate @0 integer_zerop)
1465 (shiftrotate integer_zerop@0 @1)
1467 /* Prefer vector1 << scalar to vector1 << vector2
1468 if vector2 is uniform. */
1469 (for vec (VECTOR_CST CONSTRUCTOR)
1471 (shiftrotate @0 vec@1)
1472 (with { tree tem = uniform_vector_p (@1); }
1474 (shiftrotate @0 { tem; }))))))
1476 /* Rewrite an LROTATE_EXPR by a constant into an
1477 RROTATE_EXPR by a new constant. */
1479 (lrotate @0 INTEGER_CST@1)
1480 (rrotate @0 { const_binop (MINUS_EXPR, TREE_TYPE (@1),
1481 build_int_cst (TREE_TYPE (@1),
1482 element_precision (type)), @1); }))
1484 /* Turn (a OP c1) OP c2 into a OP (c1+c2). */
1485 (for op (lrotate rrotate rshift lshift)
1487 (op (op @0 INTEGER_CST@1) INTEGER_CST@2)
1488 (with { unsigned int prec = element_precision (type); }
1489 (if (wi::ge_p (@1, 0, TYPE_SIGN (TREE_TYPE (@1)))
1490 && wi::lt_p (@1, prec, TYPE_SIGN (TREE_TYPE (@1)))
1491 && wi::ge_p (@2, 0, TYPE_SIGN (TREE_TYPE (@2)))
1492 && wi::lt_p (@2, prec, TYPE_SIGN (TREE_TYPE (@2))))
1493 (with { unsigned int low = wi::add (@1, @2).to_uhwi (); }
1494 /* Deal with a OP (c1 + c2) being undefined but (a OP c1) OP c2
1495 being well defined. */
1497 (if (op == LROTATE_EXPR || op == RROTATE_EXPR)
1498 (op @0 { build_int_cst (TREE_TYPE (@1), low % prec); })
1499 (if (TYPE_UNSIGNED (type) || op == LSHIFT_EXPR)
1500 { build_zero_cst (type); }
1501 (op @0 { build_int_cst (TREE_TYPE (@1), prec - 1); })))
1502 (op @0 { build_int_cst (TREE_TYPE (@1), low); })))))))
1505 /* ((1 << A) & 1) != 0 -> A == 0
1506 ((1 << A) & 1) == 0 -> A != 0 */
1510 (cmp (bit_and (lshift integer_onep @0) integer_onep) integer_zerop)
1511 (icmp @0 { build_zero_cst (TREE_TYPE (@0)); })))
1513 /* (CST1 << A) == CST2 -> A == ctz (CST2) - ctz (CST1)
1514 (CST1 << A) != CST2 -> A != ctz (CST2) - ctz (CST1)
1518 (cmp (lshift INTEGER_CST@0 @1) INTEGER_CST@2)
1519 (with { int cand = wi::ctz (@2) - wi::ctz (@0); }
1521 || (!integer_zerop (@2)
1522 && wi::ne_p (wi::lshift (@0, cand), @2)))
1523 { constant_boolean_node (cmp == NE_EXPR, type); }
1524 (if (!integer_zerop (@2)
1525 && wi::eq_p (wi::lshift (@0, cand), @2))
1526 (cmp @1 { build_int_cst (TREE_TYPE (@1), cand); }))))))
1528 /* Fold (X << C1) & C2 into (X << C1) & (C2 | ((1 << C1) - 1))
1529 (X >> C1) & C2 into (X >> C1) & (C2 | ~((type) -1 >> C1))
1530 if the new mask might be further optimized. */
1531 (for shift (lshift rshift)
1533 (bit_and (convert?:s@4 (shift:s@5 (convert1?@3 @0) INTEGER_CST@1))
1535 (if (tree_nop_conversion_p (TREE_TYPE (@4), TREE_TYPE (@5))
1536 && TYPE_PRECISION (type) <= HOST_BITS_PER_WIDE_INT
1537 && tree_fits_uhwi_p (@1)
1538 && tree_to_uhwi (@1) > 0
1539 && tree_to_uhwi (@1) < TYPE_PRECISION (type))
1542 unsigned int shiftc = tree_to_uhwi (@1);
1543 unsigned HOST_WIDE_INT mask = TREE_INT_CST_LOW (@2);
1544 unsigned HOST_WIDE_INT newmask, zerobits = 0;
1545 tree shift_type = TREE_TYPE (@3);
1548 if (shift == LSHIFT_EXPR)
1549 zerobits = ((HOST_WIDE_INT_1U << shiftc) - 1);
1550 else if (shift == RSHIFT_EXPR
1551 && (TYPE_PRECISION (shift_type)
1552 == GET_MODE_PRECISION (TYPE_MODE (shift_type))))
1554 prec = TYPE_PRECISION (TREE_TYPE (@3));
1556 /* See if more bits can be proven as zero because of
1559 && TYPE_UNSIGNED (TREE_TYPE (@0)))
1561 tree inner_type = TREE_TYPE (@0);
1562 if ((TYPE_PRECISION (inner_type)
1563 == GET_MODE_PRECISION (TYPE_MODE (inner_type)))
1564 && TYPE_PRECISION (inner_type) < prec)
1566 prec = TYPE_PRECISION (inner_type);
1567 /* See if we can shorten the right shift. */
1569 shift_type = inner_type;
1570 /* Otherwise X >> C1 is all zeros, so we'll optimize
1571 it into (X, 0) later on by making sure zerobits
1575 zerobits = HOST_WIDE_INT_M1U;
1578 zerobits >>= HOST_BITS_PER_WIDE_INT - shiftc;
1579 zerobits <<= prec - shiftc;
1581 /* For arithmetic shift if sign bit could be set, zerobits
1582 can contain actually sign bits, so no transformation is
1583 possible, unless MASK masks them all away. In that
1584 case the shift needs to be converted into logical shift. */
1585 if (!TYPE_UNSIGNED (TREE_TYPE (@3))
1586 && prec == TYPE_PRECISION (TREE_TYPE (@3)))
1588 if ((mask & zerobits) == 0)
1589 shift_type = unsigned_type_for (TREE_TYPE (@3));
1595 /* ((X << 16) & 0xff00) is (X, 0). */
1596 (if ((mask & zerobits) == mask)
1597 { build_int_cst (type, 0); }
1598 (with { newmask = mask | zerobits; }
1599 (if (newmask != mask && (newmask & (newmask + 1)) == 0)
1602 /* Only do the transformation if NEWMASK is some integer
1604 for (prec = BITS_PER_UNIT;
1605 prec < HOST_BITS_PER_WIDE_INT; prec <<= 1)
1606 if (newmask == (HOST_WIDE_INT_1U << prec) - 1)
1609 (if (prec < HOST_BITS_PER_WIDE_INT
1610 || newmask == HOST_WIDE_INT_M1U)
1612 { tree newmaskt = build_int_cst_type (TREE_TYPE (@2), newmask); }
1613 (if (!tree_int_cst_equal (newmaskt, @2))
1614 (if (shift_type != TREE_TYPE (@3))
1615 (bit_and (convert (shift:shift_type (convert @3) @1)) { newmaskt; })
1616 (bit_and @4 { newmaskt; })))))))))))))
1618 /* Fold (X {&,^,|} C2) << C1 into (X << C1) {&,^,|} (C2 << C1)
1619 (X {&,^,|} C2) >> C1 into (X >> C1) & (C2 >> C1). */
1620 (for shift (lshift rshift)
1621 (for bit_op (bit_and bit_xor bit_ior)
1623 (shift (convert?:s (bit_op:s @0 INTEGER_CST@2)) INTEGER_CST@1)
1624 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1625 (with { tree mask = int_const_binop (shift, fold_convert (type, @2), @1); }
1626 (bit_op (shift (convert @0) @1) { mask; }))))))
1628 /* ~(~X >> Y) -> X >> Y (for arithmetic shift). */
1630 (bit_not (convert1?:s (rshift:s (convert2?@0 (bit_not @1)) @2)))
1631 (if (!TYPE_UNSIGNED (TREE_TYPE (@0))
1632 && (element_precision (TREE_TYPE (@0))
1633 <= element_precision (TREE_TYPE (@1))
1634 || !TYPE_UNSIGNED (TREE_TYPE (@1))))
1636 { tree shift_type = TREE_TYPE (@0); }
1637 (convert (rshift (convert:shift_type @1) @2)))))
1639 /* ~(~X >>r Y) -> X >>r Y
1640 ~(~X <<r Y) -> X <<r Y */
1641 (for rotate (lrotate rrotate)
1643 (bit_not (convert1?:s (rotate:s (convert2?@0 (bit_not @1)) @2)))
1644 (if ((element_precision (TREE_TYPE (@0))
1645 <= element_precision (TREE_TYPE (@1))
1646 || !TYPE_UNSIGNED (TREE_TYPE (@1)))
1647 && (element_precision (type) <= element_precision (TREE_TYPE (@0))
1648 || !TYPE_UNSIGNED (TREE_TYPE (@0))))
1650 { tree rotate_type = TREE_TYPE (@0); }
1651 (convert (rotate (convert:rotate_type @1) @2))))))
1653 /* Simplifications of conversions. */
1655 /* Basic strip-useless-type-conversions / strip_nops. */
1656 (for cvt (convert view_convert float fix_trunc)
1659 (if ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@0)))
1660 || (GENERIC && type == TREE_TYPE (@0)))
1663 /* Contract view-conversions. */
1665 (view_convert (view_convert @0))
1668 /* For integral conversions with the same precision or pointer
1669 conversions use a NOP_EXPR instead. */
1672 (if ((INTEGRAL_TYPE_P (type) || POINTER_TYPE_P (type))
1673 && (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
1674 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (@0)))
1677 /* Strip inner integral conversions that do not change precision or size. */
1679 (view_convert (convert@0 @1))
1680 (if ((INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
1681 && (INTEGRAL_TYPE_P (TREE_TYPE (@1)) || POINTER_TYPE_P (TREE_TYPE (@1)))
1682 && (TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
1683 && (TYPE_SIZE (TREE_TYPE (@0)) == TYPE_SIZE (TREE_TYPE (@1))))
1686 /* Re-association barriers around constants and other re-association
1687 barriers can be removed. */
1689 (paren CONSTANT_CLASS_P@0)
1692 (paren (paren@1 @0))
1695 /* Handle cases of two conversions in a row. */
1696 (for ocvt (convert float fix_trunc)
1697 (for icvt (convert float)
1702 tree inside_type = TREE_TYPE (@0);
1703 tree inter_type = TREE_TYPE (@1);
1704 int inside_int = INTEGRAL_TYPE_P (inside_type);
1705 int inside_ptr = POINTER_TYPE_P (inside_type);
1706 int inside_float = FLOAT_TYPE_P (inside_type);
1707 int inside_vec = VECTOR_TYPE_P (inside_type);
1708 unsigned int inside_prec = TYPE_PRECISION (inside_type);
1709 int inside_unsignedp = TYPE_UNSIGNED (inside_type);
1710 int inter_int = INTEGRAL_TYPE_P (inter_type);
1711 int inter_ptr = POINTER_TYPE_P (inter_type);
1712 int inter_float = FLOAT_TYPE_P (inter_type);
1713 int inter_vec = VECTOR_TYPE_P (inter_type);
1714 unsigned int inter_prec = TYPE_PRECISION (inter_type);
1715 int inter_unsignedp = TYPE_UNSIGNED (inter_type);
1716 int final_int = INTEGRAL_TYPE_P (type);
1717 int final_ptr = POINTER_TYPE_P (type);
1718 int final_float = FLOAT_TYPE_P (type);
1719 int final_vec = VECTOR_TYPE_P (type);
1720 unsigned int final_prec = TYPE_PRECISION (type);
1721 int final_unsignedp = TYPE_UNSIGNED (type);
1724 /* In addition to the cases of two conversions in a row
1725 handled below, if we are converting something to its own
1726 type via an object of identical or wider precision, neither
1727 conversion is needed. */
1728 (if (((GIMPLE && useless_type_conversion_p (type, inside_type))
1730 && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (inside_type)))
1731 && (((inter_int || inter_ptr) && final_int)
1732 || (inter_float && final_float))
1733 && inter_prec >= final_prec)
1736 /* Likewise, if the intermediate and initial types are either both
1737 float or both integer, we don't need the middle conversion if the
1738 former is wider than the latter and doesn't change the signedness
1739 (for integers). Avoid this if the final type is a pointer since
1740 then we sometimes need the middle conversion. */
1741 (if (((inter_int && inside_int) || (inter_float && inside_float))
1742 && (final_int || final_float)
1743 && inter_prec >= inside_prec
1744 && (inter_float || inter_unsignedp == inside_unsignedp))
1747 /* If we have a sign-extension of a zero-extended value, we can
1748 replace that by a single zero-extension. Likewise if the
1749 final conversion does not change precision we can drop the
1750 intermediate conversion. */
1751 (if (inside_int && inter_int && final_int
1752 && ((inside_prec < inter_prec && inter_prec < final_prec
1753 && inside_unsignedp && !inter_unsignedp)
1754 || final_prec == inter_prec))
1757 /* Two conversions in a row are not needed unless:
1758 - some conversion is floating-point (overstrict for now), or
1759 - some conversion is a vector (overstrict for now), or
1760 - the intermediate type is narrower than both initial and
1762 - the intermediate type and innermost type differ in signedness,
1763 and the outermost type is wider than the intermediate, or
1764 - the initial type is a pointer type and the precisions of the
1765 intermediate and final types differ, or
1766 - the final type is a pointer type and the precisions of the
1767 initial and intermediate types differ. */
1768 (if (! inside_float && ! inter_float && ! final_float
1769 && ! inside_vec && ! inter_vec && ! final_vec
1770 && (inter_prec >= inside_prec || inter_prec >= final_prec)
1771 && ! (inside_int && inter_int
1772 && inter_unsignedp != inside_unsignedp
1773 && inter_prec < final_prec)
1774 && ((inter_unsignedp && inter_prec > inside_prec)
1775 == (final_unsignedp && final_prec > inter_prec))
1776 && ! (inside_ptr && inter_prec != final_prec)
1777 && ! (final_ptr && inside_prec != inter_prec))
1780 /* A truncation to an unsigned type (a zero-extension) should be
1781 canonicalized as bitwise and of a mask. */
1782 (if (GIMPLE /* PR70366: doing this in GENERIC breaks -Wconversion. */
1783 && final_int && inter_int && inside_int
1784 && final_prec == inside_prec
1785 && final_prec > inter_prec
1787 (convert (bit_and @0 { wide_int_to_tree
1789 wi::mask (inter_prec, false,
1790 TYPE_PRECISION (inside_type))); })))
1792 /* If we are converting an integer to a floating-point that can
1793 represent it exactly and back to an integer, we can skip the
1794 floating-point conversion. */
1795 (if (GIMPLE /* PR66211 */
1796 && inside_int && inter_float && final_int &&
1797 (unsigned) significand_size (TYPE_MODE (inter_type))
1798 >= inside_prec - !inside_unsignedp)
1801 /* If we have a narrowing conversion to an integral type that is fed by a
1802 BIT_AND_EXPR, we might be able to remove the BIT_AND_EXPR if it merely
1803 masks off bits outside the final type (and nothing else). */
1805 (convert (bit_and @0 INTEGER_CST@1))
1806 (if (INTEGRAL_TYPE_P (type)
1807 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
1808 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))
1809 && operand_equal_p (@1, build_low_bits_mask (TREE_TYPE (@1),
1810 TYPE_PRECISION (type)), 0))
1814 /* (X /[ex] A) * A -> X. */
1816 (mult (convert1? (exact_div @0 @@1)) (convert2? @1))
1819 /* Canonicalization of binary operations. */
1821 /* Convert X + -C into X - C. */
1823 (plus @0 REAL_CST@1)
1824 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
1825 (with { tree tem = const_unop (NEGATE_EXPR, type, @1); }
1826 (if (!TREE_OVERFLOW (tem) || !flag_trapping_math)
1827 (minus @0 { tem; })))))
1829 /* Convert x+x into x*2. */
1832 (if (SCALAR_FLOAT_TYPE_P (type))
1833 (mult @0 { build_real (type, dconst2); })
1834 (if (INTEGRAL_TYPE_P (type))
1835 (mult @0 { build_int_cst (type, 2); }))))
1838 (minus integer_zerop @1)
1841 /* (ARG0 - ARG1) is the same as (-ARG1 + ARG0). So check whether
1842 ARG0 is zero and X + ARG0 reduces to X, since that would mean
1843 (-ARG1 + ARG0) reduces to -ARG1. */
1845 (minus real_zerop@0 @1)
1846 (if (fold_real_zero_addition_p (type, @0, 0))
1849 /* Transform x * -1 into -x. */
1851 (mult @0 integer_minus_onep)
1854 /* True if we can easily extract the real and imaginary parts of a complex
1856 (match compositional_complex
1857 (convert? (complex @0 @1)))
1859 /* COMPLEX_EXPR and REALPART/IMAGPART_EXPR cancellations. */
1861 (complex (realpart @0) (imagpart @0))
1864 (realpart (complex @0 @1))
1867 (imagpart (complex @0 @1))
1870 /* Sometimes we only care about half of a complex expression. */
1872 (realpart (convert?:s (conj:s @0)))
1873 (convert (realpart @0)))
1875 (imagpart (convert?:s (conj:s @0)))
1876 (convert (negate (imagpart @0))))
1877 (for part (realpart imagpart)
1878 (for op (plus minus)
1880 (part (convert?:s@2 (op:s @0 @1)))
1881 (convert (op (part @0) (part @1))))))
1883 (realpart (convert?:s (CEXPI:s @0)))
1886 (imagpart (convert?:s (CEXPI:s @0)))
1889 /* conj(conj(x)) -> x */
1891 (conj (convert? (conj @0)))
1892 (if (tree_nop_conversion_p (TREE_TYPE (@0), type))
1895 /* conj({x,y}) -> {x,-y} */
1897 (conj (convert?:s (complex:s @0 @1)))
1898 (with { tree itype = TREE_TYPE (type); }
1899 (complex (convert:itype @0) (negate (convert:itype @1)))))
1901 /* BSWAP simplifications, transforms checked by gcc.dg/builtin-bswap-8.c. */
1902 (for bswap (BUILT_IN_BSWAP16 BUILT_IN_BSWAP32 BUILT_IN_BSWAP64)
1907 (bswap (bit_not (bswap @0)))
1909 (for bitop (bit_xor bit_ior bit_and)
1911 (bswap (bitop:c (bswap @0) @1))
1912 (bitop @0 (bswap @1)))))
1915 /* Combine COND_EXPRs and VEC_COND_EXPRs. */
1917 /* Simplify constant conditions.
1918 Only optimize constant conditions when the selected branch
1919 has the same type as the COND_EXPR. This avoids optimizing
1920 away "c ? x : throw", where the throw has a void type.
1921 Note that we cannot throw away the fold-const.c variant nor
1922 this one as we depend on doing this transform before possibly
1923 A ? B : B -> B triggers and the fold-const.c one can optimize
1924 0 ? A : B to B even if A has side-effects. Something
1925 genmatch cannot handle. */
1927 (cond INTEGER_CST@0 @1 @2)
1928 (if (integer_zerop (@0))
1929 (if (!VOID_TYPE_P (TREE_TYPE (@2)) || VOID_TYPE_P (type))
1931 (if (!VOID_TYPE_P (TREE_TYPE (@1)) || VOID_TYPE_P (type))
1934 (vec_cond VECTOR_CST@0 @1 @2)
1935 (if (integer_all_onesp (@0))
1937 (if (integer_zerop (@0))
1940 (for cnd (cond vec_cond)
1941 /* A ? B : (A ? X : C) -> A ? B : C. */
1943 (cnd @0 (cnd @0 @1 @2) @3)
1946 (cnd @0 @1 (cnd @0 @2 @3))
1948 /* A ? B : (!A ? C : X) -> A ? B : C. */
1949 /* ??? This matches embedded conditions open-coded because genmatch
1950 would generate matching code for conditions in separate stmts only.
1951 The following is still important to merge then and else arm cases
1952 from if-conversion. */
1954 (cnd @0 @1 (cnd @2 @3 @4))
1955 (if (COMPARISON_CLASS_P (@0)
1956 && COMPARISON_CLASS_P (@2)
1957 && invert_tree_comparison
1958 (TREE_CODE (@0), HONOR_NANS (TREE_OPERAND (@0, 0))) == TREE_CODE (@2)
1959 && operand_equal_p (TREE_OPERAND (@0, 0), TREE_OPERAND (@2, 0), 0)
1960 && operand_equal_p (TREE_OPERAND (@0, 1), TREE_OPERAND (@2, 1), 0))
1963 (cnd @0 (cnd @1 @2 @3) @4)
1964 (if (COMPARISON_CLASS_P (@0)
1965 && COMPARISON_CLASS_P (@1)
1966 && invert_tree_comparison
1967 (TREE_CODE (@0), HONOR_NANS (TREE_OPERAND (@0, 0))) == TREE_CODE (@1)
1968 && operand_equal_p (TREE_OPERAND (@0, 0), TREE_OPERAND (@1, 0), 0)
1969 && operand_equal_p (TREE_OPERAND (@0, 1), TREE_OPERAND (@1, 1), 0))
1972 /* A ? B : B -> B. */
1977 /* !A ? B : C -> A ? C : B. */
1979 (cnd (logical_inverted_value truth_valued_p@0) @1 @2)
1982 /* A + (B vcmp C ? 1 : 0) -> A - (B vcmp C ? -1 : 0), since vector comparisons
1983 return all -1 or all 0 results. */
1984 /* ??? We could instead convert all instances of the vec_cond to negate,
1985 but that isn't necessarily a win on its own. */
1987 (plus:c @3 (view_convert? (vec_cond:s @0 integer_each_onep@1 integer_zerop@2)))
1988 (if (VECTOR_TYPE_P (type)
1989 && TYPE_VECTOR_SUBPARTS (type) == TYPE_VECTOR_SUBPARTS (TREE_TYPE (@1))
1990 && (TYPE_MODE (TREE_TYPE (type))
1991 == TYPE_MODE (TREE_TYPE (TREE_TYPE (@1)))))
1992 (minus @3 (view_convert (vec_cond @0 (negate @1) @2)))))
1994 /* ... likewise A - (B vcmp C ? 1 : 0) -> A + (B vcmp C ? -1 : 0). */
1996 (minus @3 (view_convert? (vec_cond:s @0 integer_each_onep@1 integer_zerop@2)))
1997 (if (VECTOR_TYPE_P (type)
1998 && TYPE_VECTOR_SUBPARTS (type) == TYPE_VECTOR_SUBPARTS (TREE_TYPE (@1))
1999 && (TYPE_MODE (TREE_TYPE (type))
2000 == TYPE_MODE (TREE_TYPE (TREE_TYPE (@1)))))
2001 (plus @3 (view_convert (vec_cond @0 (negate @1) @2)))))
2004 /* Simplifications of comparisons. */
2006 /* See if we can reduce the magnitude of a constant involved in a
2007 comparison by changing the comparison code. This is a canonicalization
2008 formerly done by maybe_canonicalize_comparison_1. */
2012 (cmp @0 INTEGER_CST@1)
2013 (if (tree_int_cst_sgn (@1) == -1)
2014 (acmp @0 { wide_int_to_tree (TREE_TYPE (@1), wi::add (@1, 1)); }))))
2018 (cmp @0 INTEGER_CST@1)
2019 (if (tree_int_cst_sgn (@1) == 1)
2020 (acmp @0 { wide_int_to_tree (TREE_TYPE (@1), wi::sub (@1, 1)); }))))
2023 /* We can simplify a logical negation of a comparison to the
2024 inverted comparison. As we cannot compute an expression
2025 operator using invert_tree_comparison we have to simulate
2026 that with expression code iteration. */
2027 (for cmp (tcc_comparison)
2028 icmp (inverted_tcc_comparison)
2029 ncmp (inverted_tcc_comparison_with_nans)
2030 /* Ideally we'd like to combine the following two patterns
2031 and handle some more cases by using
2032 (logical_inverted_value (cmp @0 @1))
2033 here but for that genmatch would need to "inline" that.
2034 For now implement what forward_propagate_comparison did. */
2036 (bit_not (cmp @0 @1))
2037 (if (VECTOR_TYPE_P (type)
2038 || (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1))
2039 /* Comparison inversion may be impossible for trapping math,
2040 invert_tree_comparison will tell us. But we can't use
2041 a computed operator in the replacement tree thus we have
2042 to play the trick below. */
2043 (with { enum tree_code ic = invert_tree_comparison
2044 (cmp, HONOR_NANS (@0)); }
2050 (bit_xor (cmp @0 @1) integer_truep)
2051 (with { enum tree_code ic = invert_tree_comparison
2052 (cmp, HONOR_NANS (@0)); }
2058 /* Transform comparisons of the form X - Y CMP 0 to X CMP Y.
2059 ??? The transformation is valid for the other operators if overflow
2060 is undefined for the type, but performing it here badly interacts
2061 with the transformation in fold_cond_expr_with_comparison which
2062 attempts to synthetize ABS_EXPR. */
2065 (cmp (minus@2 @0 @1) integer_zerop)
2066 (if (single_use (@2))
2069 /* Transform comparisons of the form X * C1 CMP 0 to X CMP 0 in the
2070 signed arithmetic case. That form is created by the compiler
2071 often enough for folding it to be of value. One example is in
2072 computing loop trip counts after Operator Strength Reduction. */
2073 (for cmp (simple_comparison)
2074 scmp (swapped_simple_comparison)
2076 (cmp (mult@3 @0 INTEGER_CST@1) integer_zerop@2)
2077 /* Handle unfolded multiplication by zero. */
2078 (if (integer_zerop (@1))
2080 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2081 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2083 /* If @1 is negative we swap the sense of the comparison. */
2084 (if (tree_int_cst_sgn (@1) < 0)
2088 /* Simplify comparison of something with itself. For IEEE
2089 floating-point, we can only do some of these simplifications. */
2093 (if (! FLOAT_TYPE_P (TREE_TYPE (@0))
2094 || ! HONOR_NANS (@0))
2095 { constant_boolean_node (true, type); }
2096 (if (cmp != EQ_EXPR)
2102 || ! FLOAT_TYPE_P (TREE_TYPE (@0))
2103 || ! HONOR_NANS (@0))
2104 { constant_boolean_node (false, type); })))
2105 (for cmp (unle unge uneq)
2108 { constant_boolean_node (true, type); }))
2109 (for cmp (unlt ungt)
2115 (if (!flag_trapping_math)
2116 { constant_boolean_node (false, type); }))
2118 /* Fold ~X op ~Y as Y op X. */
2119 (for cmp (simple_comparison)
2121 (cmp (bit_not@2 @0) (bit_not@3 @1))
2122 (if (single_use (@2) && single_use (@3))
2125 /* Fold ~X op C as X op' ~C, where op' is the swapped comparison. */
2126 (for cmp (simple_comparison)
2127 scmp (swapped_simple_comparison)
2129 (cmp (bit_not@2 @0) CONSTANT_CLASS_P@1)
2130 (if (single_use (@2)
2131 && (TREE_CODE (@1) == INTEGER_CST || TREE_CODE (@1) == VECTOR_CST))
2132 (scmp @0 (bit_not @1)))))
2134 (for cmp (simple_comparison)
2135 /* Fold (double)float1 CMP (double)float2 into float1 CMP float2. */
2137 (cmp (convert@2 @0) (convert? @1))
2138 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
2139 && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@2))
2140 == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0)))
2141 && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@2))
2142 == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1))))
2145 tree type1 = TREE_TYPE (@1);
2146 if (TREE_CODE (@1) == REAL_CST && !DECIMAL_FLOAT_TYPE_P (type1))
2148 REAL_VALUE_TYPE orig = TREE_REAL_CST (@1);
2149 if (TYPE_PRECISION (type1) > TYPE_PRECISION (float_type_node)
2150 && exact_real_truncate (TYPE_MODE (float_type_node), &orig))
2151 type1 = float_type_node;
2152 if (TYPE_PRECISION (type1) > TYPE_PRECISION (double_type_node)
2153 && exact_real_truncate (TYPE_MODE (double_type_node), &orig))
2154 type1 = double_type_node;
2157 = (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type1)
2158 ? TREE_TYPE (@0) : type1);
2160 (if (TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (newtype))
2161 (cmp (convert:newtype @0) (convert:newtype @1))))))
2165 /* IEEE doesn't distinguish +0 and -0 in comparisons. */
2167 /* a CMP (-0) -> a CMP 0 */
2168 (if (REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@1)))
2169 (cmp @0 { build_real (TREE_TYPE (@1), dconst0); }))
2170 /* x != NaN is always true, other ops are always false. */
2171 (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
2172 && ! HONOR_SNANS (@1))
2173 { constant_boolean_node (cmp == NE_EXPR, type); })
2174 /* Fold comparisons against infinity. */
2175 (if (REAL_VALUE_ISINF (TREE_REAL_CST (@1))
2176 && MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (@1))))
2179 REAL_VALUE_TYPE max;
2180 enum tree_code code = cmp;
2181 bool neg = REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1));
2183 code = swap_tree_comparison (code);
2186 /* x > +Inf is always false, if with ignore sNANs. */
2187 (if (code == GT_EXPR
2188 && ! HONOR_SNANS (@0))
2189 { constant_boolean_node (false, type); })
2190 (if (code == LE_EXPR)
2191 /* x <= +Inf is always true, if we don't case about NaNs. */
2192 (if (! HONOR_NANS (@0))
2193 { constant_boolean_node (true, type); }
2194 /* x <= +Inf is the same as x == x, i.e. !isnan(x). */
2196 /* x == +Inf and x >= +Inf are always equal to x > DBL_MAX. */
2197 (if (code == EQ_EXPR || code == GE_EXPR)
2198 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
2200 (lt @0 { build_real (TREE_TYPE (@0), max); })
2201 (gt @0 { build_real (TREE_TYPE (@0), max); }))))
2202 /* x < +Inf is always equal to x <= DBL_MAX. */
2203 (if (code == LT_EXPR)
2204 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
2206 (ge @0 { build_real (TREE_TYPE (@0), max); })
2207 (le @0 { build_real (TREE_TYPE (@0), max); }))))
2208 /* x != +Inf is always equal to !(x > DBL_MAX). */
2209 (if (code == NE_EXPR)
2210 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
2211 (if (! HONOR_NANS (@0))
2213 (ge @0 { build_real (TREE_TYPE (@0), max); })
2214 (le @0 { build_real (TREE_TYPE (@0), max); }))
2216 (bit_xor (lt @0 { build_real (TREE_TYPE (@0), max); })
2217 { build_one_cst (type); })
2218 (bit_xor (gt @0 { build_real (TREE_TYPE (@0), max); })
2219 { build_one_cst (type); }))))))))))
2221 /* If this is a comparison of a real constant with a PLUS_EXPR
2222 or a MINUS_EXPR of a real constant, we can convert it into a
2223 comparison with a revised real constant as long as no overflow
2224 occurs when unsafe_math_optimizations are enabled. */
2225 (if (flag_unsafe_math_optimizations)
2226 (for op (plus minus)
2228 (cmp (op @0 REAL_CST@1) REAL_CST@2)
2231 tree tem = const_binop (op == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR,
2232 TREE_TYPE (@1), @2, @1);
2234 (if (tem && !TREE_OVERFLOW (tem))
2235 (cmp @0 { tem; }))))))
2237 /* Likewise, we can simplify a comparison of a real constant with
2238 a MINUS_EXPR whose first operand is also a real constant, i.e.
2239 (c1 - x) < c2 becomes x > c1-c2. Reordering is allowed on
2240 floating-point types only if -fassociative-math is set. */
2241 (if (flag_associative_math)
2243 (cmp (minus REAL_CST@0 @1) REAL_CST@2)
2244 (with { tree tem = const_binop (MINUS_EXPR, TREE_TYPE (@1), @0, @2); }
2245 (if (tem && !TREE_OVERFLOW (tem))
2246 (cmp { tem; } @1)))))
2248 /* Fold comparisons against built-in math functions. */
2249 (if (flag_unsafe_math_optimizations
2250 && ! flag_errno_math)
2253 (cmp (sq @0) REAL_CST@1)
2255 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
2257 /* sqrt(x) < y is always false, if y is negative. */
2258 (if (cmp == EQ_EXPR || cmp == LT_EXPR || cmp == LE_EXPR)
2259 { constant_boolean_node (false, type); })
2260 /* sqrt(x) > y is always true, if y is negative and we
2261 don't care about NaNs, i.e. negative values of x. */
2262 (if (cmp == NE_EXPR || !HONOR_NANS (@0))
2263 { constant_boolean_node (true, type); })
2264 /* sqrt(x) > y is the same as x >= 0, if y is negative. */
2265 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })))
2266 (if (real_equal (TREE_REAL_CST_PTR (@1), &dconst0))
2268 /* sqrt(x) < 0 is always false. */
2269 (if (cmp == LT_EXPR)
2270 { constant_boolean_node (false, type); })
2271 /* sqrt(x) >= 0 is always true if we don't care about NaNs. */
2272 (if (cmp == GE_EXPR && !HONOR_NANS (@0))
2273 { constant_boolean_node (true, type); })
2274 /* sqrt(x) <= 0 -> x == 0. */
2275 (if (cmp == LE_EXPR)
2277 /* Otherwise sqrt(x) cmp 0 -> x cmp 0. Here cmp can be >=, >,
2278 == or !=. In the last case:
2280 (sqrt(x) != 0) == (NaN != 0) == true == (x != 0)
2282 if x is negative or NaN. Due to -funsafe-math-optimizations,
2283 the results for other x follow from natural arithmetic. */
2285 (if (cmp == GT_EXPR || cmp == GE_EXPR)
2289 real_arithmetic (&c2, MULT_EXPR,
2290 &TREE_REAL_CST (@1), &TREE_REAL_CST (@1));
2291 real_convert (&c2, TYPE_MODE (TREE_TYPE (@0)), &c2);
2293 (if (REAL_VALUE_ISINF (c2))
2294 /* sqrt(x) > y is x == +Inf, when y is very large. */
2295 (if (HONOR_INFINITIES (@0))
2296 (eq @0 { build_real (TREE_TYPE (@0), c2); })
2297 { constant_boolean_node (false, type); })
2298 /* sqrt(x) > c is the same as x > c*c. */
2299 (cmp @0 { build_real (TREE_TYPE (@0), c2); }))))
2300 (if (cmp == LT_EXPR || cmp == LE_EXPR)
2304 real_arithmetic (&c2, MULT_EXPR,
2305 &TREE_REAL_CST (@1), &TREE_REAL_CST (@1));
2306 real_convert (&c2, TYPE_MODE (TREE_TYPE (@0)), &c2);
2308 (if (REAL_VALUE_ISINF (c2))
2310 /* sqrt(x) < y is always true, when y is a very large
2311 value and we don't care about NaNs or Infinities. */
2312 (if (! HONOR_NANS (@0) && ! HONOR_INFINITIES (@0))
2313 { constant_boolean_node (true, type); })
2314 /* sqrt(x) < y is x != +Inf when y is very large and we
2315 don't care about NaNs. */
2316 (if (! HONOR_NANS (@0))
2317 (ne @0 { build_real (TREE_TYPE (@0), c2); }))
2318 /* sqrt(x) < y is x >= 0 when y is very large and we
2319 don't care about Infinities. */
2320 (if (! HONOR_INFINITIES (@0))
2321 (ge @0 { build_real (TREE_TYPE (@0), dconst0); }))
2322 /* sqrt(x) < y is x >= 0 && x != +Inf, when y is large. */
2325 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
2326 (ne @0 { build_real (TREE_TYPE (@0), c2); }))))
2327 /* sqrt(x) < c is the same as x < c*c, if we ignore NaNs. */
2328 (if (! HONOR_NANS (@0))
2329 (cmp @0 { build_real (TREE_TYPE (@0), c2); })
2330 /* sqrt(x) < c is the same as x >= 0 && x < c*c. */
2333 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
2334 (cmp @0 { build_real (TREE_TYPE (@0), c2); }))))))))))))
2336 /* Unordered tests if either argument is a NaN. */
2338 (bit_ior (unordered @0 @0) (unordered @1 @1))
2339 (if (types_match (@0, @1))
2342 (bit_and (ordered @0 @0) (ordered @1 @1))
2343 (if (types_match (@0, @1))
2346 (bit_ior:c (unordered @0 @0) (unordered:c@2 @0 @1))
2349 (bit_and:c (ordered @0 @0) (ordered:c@2 @0 @1))
2352 /* Simple range test simplifications. */
2353 /* A < B || A >= B -> true. */
2354 (for test1 (lt le le le ne ge)
2355 test2 (ge gt ge ne eq ne)
2357 (bit_ior:c (test1 @0 @1) (test2 @0 @1))
2358 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2359 || VECTOR_INTEGER_TYPE_P (TREE_TYPE (@0)))
2360 { constant_boolean_node (true, type); })))
2361 /* A < B && A >= B -> false. */
2362 (for test1 (lt lt lt le ne eq)
2363 test2 (ge gt eq gt eq gt)
2365 (bit_and:c (test1 @0 @1) (test2 @0 @1))
2366 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2367 || VECTOR_INTEGER_TYPE_P (TREE_TYPE (@0)))
2368 { constant_boolean_node (false, type); })))
2370 /* -A CMP -B -> B CMP A. */
2371 (for cmp (tcc_comparison)
2372 scmp (swapped_tcc_comparison)
2374 (cmp (negate @0) (negate @1))
2375 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
2376 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2377 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
2380 (cmp (negate @0) CONSTANT_CLASS_P@1)
2381 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
2382 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2383 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
2384 (with { tree tem = const_unop (NEGATE_EXPR, TREE_TYPE (@0), @1); }
2385 (if (tem && !TREE_OVERFLOW (tem))
2386 (scmp @0 { tem; }))))))
2388 /* Convert ABS_EXPR<x> == 0 or ABS_EXPR<x> != 0 to x == 0 or x != 0. */
2391 (op (abs @0) zerop@1)
2394 /* From fold_sign_changed_comparison and fold_widened_comparison. */
2395 (for cmp (simple_comparison)
2397 (cmp (convert@0 @00) (convert?@1 @10))
2398 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2399 /* Disable this optimization if we're casting a function pointer
2400 type on targets that require function pointer canonicalization. */
2401 && !(targetm.have_canonicalize_funcptr_for_compare ()
2402 && TREE_CODE (TREE_TYPE (@00)) == POINTER_TYPE
2403 && TREE_CODE (TREE_TYPE (TREE_TYPE (@00))) == FUNCTION_TYPE)
2405 (if (TYPE_PRECISION (TREE_TYPE (@00)) == TYPE_PRECISION (TREE_TYPE (@0))
2406 && (TREE_CODE (@10) == INTEGER_CST
2407 || (@1 != @10 && types_match (TREE_TYPE (@10), TREE_TYPE (@00))))
2408 && (TYPE_UNSIGNED (TREE_TYPE (@00)) == TYPE_UNSIGNED (TREE_TYPE (@0))
2411 && (POINTER_TYPE_P (TREE_TYPE (@00)) == POINTER_TYPE_P (TREE_TYPE (@0))))
2412 /* ??? The special-casing of INTEGER_CST conversion was in the original
2413 code and here to avoid a spurious overflow flag on the resulting
2414 constant which fold_convert produces. */
2415 (if (TREE_CODE (@1) == INTEGER_CST)
2416 (cmp @00 { force_fit_type (TREE_TYPE (@00), wi::to_widest (@1), 0,
2417 TREE_OVERFLOW (@1)); })
2418 (cmp @00 (convert @1)))
2420 (if (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (TREE_TYPE (@00)))
2421 /* If possible, express the comparison in the shorter mode. */
2422 (if ((cmp == EQ_EXPR || cmp == NE_EXPR
2423 || TYPE_UNSIGNED (TREE_TYPE (@0)) == TYPE_UNSIGNED (TREE_TYPE (@00))
2424 || (!TYPE_UNSIGNED (TREE_TYPE (@0))
2425 && TYPE_UNSIGNED (TREE_TYPE (@00))))
2426 && (types_match (TREE_TYPE (@10), TREE_TYPE (@00))
2427 || ((TYPE_PRECISION (TREE_TYPE (@00))
2428 >= TYPE_PRECISION (TREE_TYPE (@10)))
2429 && (TYPE_UNSIGNED (TREE_TYPE (@00))
2430 == TYPE_UNSIGNED (TREE_TYPE (@10))))
2431 || (TREE_CODE (@10) == INTEGER_CST
2432 && INTEGRAL_TYPE_P (TREE_TYPE (@00))
2433 && int_fits_type_p (@10, TREE_TYPE (@00)))))
2434 (cmp @00 (convert @10))
2435 (if (TREE_CODE (@10) == INTEGER_CST
2436 && INTEGRAL_TYPE_P (TREE_TYPE (@00))
2437 && !int_fits_type_p (@10, TREE_TYPE (@00)))
2440 tree min = lower_bound_in_type (TREE_TYPE (@10), TREE_TYPE (@00));
2441 tree max = upper_bound_in_type (TREE_TYPE (@10), TREE_TYPE (@00));
2442 bool above = integer_nonzerop (const_binop (LT_EXPR, type, max, @10));
2443 bool below = integer_nonzerop (const_binop (LT_EXPR, type, @10, min));
2445 (if (above || below)
2446 (if (cmp == EQ_EXPR || cmp == NE_EXPR)
2447 { constant_boolean_node (cmp == EQ_EXPR ? false : true, type); }
2448 (if (cmp == LT_EXPR || cmp == LE_EXPR)
2449 { constant_boolean_node (above ? true : false, type); }
2450 (if (cmp == GT_EXPR || cmp == GE_EXPR)
2451 { constant_boolean_node (above ? false : true, type); }))))))))))))
2454 /* A local variable can never be pointed to by
2455 the default SSA name of an incoming parameter.
2456 SSA names are canonicalized to 2nd place. */
2458 (cmp addr@0 SSA_NAME@1)
2459 (if (SSA_NAME_IS_DEFAULT_DEF (@1)
2460 && TREE_CODE (SSA_NAME_VAR (@1)) == PARM_DECL)
2461 (with { tree base = get_base_address (TREE_OPERAND (@0, 0)); }
2462 (if (TREE_CODE (base) == VAR_DECL
2463 && auto_var_in_fn_p (base, current_function_decl))
2464 (if (cmp == NE_EXPR)
2465 { constant_boolean_node (true, type); }
2466 { constant_boolean_node (false, type); }))))))
2468 /* Equality compare simplifications from fold_binary */
2471 /* If we have (A | C) == D where C & ~D != 0, convert this into 0.
2472 Similarly for NE_EXPR. */
2474 (cmp (convert?@3 (bit_ior @0 INTEGER_CST@1)) INTEGER_CST@2)
2475 (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0))
2476 && wi::bit_and_not (@1, @2) != 0)
2477 { constant_boolean_node (cmp == NE_EXPR, type); }))
2479 /* (X ^ Y) == 0 becomes X == Y, and (X ^ Y) != 0 becomes X != Y. */
2481 (cmp (bit_xor @0 @1) integer_zerop)
2484 /* (X ^ Y) == Y becomes X == 0.
2485 Likewise (X ^ Y) == X becomes Y == 0. */
2487 (cmp:c (bit_xor:c @0 @1) @0)
2488 (cmp @1 { build_zero_cst (TREE_TYPE (@1)); }))
2490 /* (X ^ C1) op C2 can be rewritten as X op (C1 ^ C2). */
2492 (cmp (convert?@3 (bit_xor @0 INTEGER_CST@1)) INTEGER_CST@2)
2493 (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0)))
2494 (cmp @0 (bit_xor @1 (convert @2)))))
2497 (cmp (convert? addr@0) integer_zerop)
2498 (if (tree_single_nonzero_warnv_p (@0, NULL))
2499 { constant_boolean_node (cmp == NE_EXPR, type); })))
2501 /* If we have (A & C) == C where C is a power of 2, convert this into
2502 (A & C) != 0. Similarly for NE_EXPR. */
2506 (cmp (bit_and@2 @0 integer_pow2p@1) @1)
2507 (icmp @2 { build_zero_cst (TREE_TYPE (@0)); })))
2509 /* If we have (A & C) != 0 where C is the sign bit of A, convert
2510 this into A < 0. Similarly for (A & C) == 0 into A >= 0. */
2514 (cmp (bit_and (convert?@2 @0) integer_pow2p@1) integer_zerop)
2515 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2516 && (TYPE_PRECISION (TREE_TYPE (@0))
2517 == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (@0))))
2518 && element_precision (@2) >= element_precision (@0)
2519 && wi::only_sign_bit_p (@1, element_precision (@0)))
2520 (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
2521 (ncmp (convert:stype @0) { build_zero_cst (stype); })))))
2523 /* When the addresses are not directly of decls compare base and offset.
2524 This implements some remaining parts of fold_comparison address
2525 comparisons but still no complete part of it. Still it is good
2526 enough to make fold_stmt not regress when not dispatching to fold_binary. */
2527 (for cmp (simple_comparison)
2529 (cmp (convert1?@2 addr@0) (convert2? addr@1))
2532 HOST_WIDE_INT off0, off1;
2533 tree base0 = get_addr_base_and_unit_offset (TREE_OPERAND (@0, 0), &off0);
2534 tree base1 = get_addr_base_and_unit_offset (TREE_OPERAND (@1, 0), &off1);
2535 if (base0 && TREE_CODE (base0) == MEM_REF)
2537 off0 += mem_ref_offset (base0).to_short_addr ();
2538 base0 = TREE_OPERAND (base0, 0);
2540 if (base1 && TREE_CODE (base1) == MEM_REF)
2542 off1 += mem_ref_offset (base1).to_short_addr ();
2543 base1 = TREE_OPERAND (base1, 0);
2546 (if (base0 && base1)
2550 if (decl_in_symtab_p (base0)
2551 && decl_in_symtab_p (base1))
2552 equal = symtab_node::get_create (base0)
2553 ->equal_address_to (symtab_node::get_create (base1));
2554 else if ((DECL_P (base0)
2555 || TREE_CODE (base0) == SSA_NAME
2556 || TREE_CODE (base0) == STRING_CST)
2558 || TREE_CODE (base1) == SSA_NAME
2559 || TREE_CODE (base1) == STRING_CST))
2560 equal = (base0 == base1);
2563 && (cmp == EQ_EXPR || cmp == NE_EXPR
2564 /* If the offsets are equal we can ignore overflow. */
2566 || POINTER_TYPE_OVERFLOW_UNDEFINED
2567 /* Or if we compare using pointers to decls or strings. */
2568 || (POINTER_TYPE_P (TREE_TYPE (@2))
2569 && (DECL_P (base0) || TREE_CODE (base0) == STRING_CST))))
2571 (if (cmp == EQ_EXPR)
2572 { constant_boolean_node (off0 == off1, type); })
2573 (if (cmp == NE_EXPR)
2574 { constant_boolean_node (off0 != off1, type); })
2575 (if (cmp == LT_EXPR)
2576 { constant_boolean_node (off0 < off1, type); })
2577 (if (cmp == LE_EXPR)
2578 { constant_boolean_node (off0 <= off1, type); })
2579 (if (cmp == GE_EXPR)
2580 { constant_boolean_node (off0 >= off1, type); })
2581 (if (cmp == GT_EXPR)
2582 { constant_boolean_node (off0 > off1, type); }))
2584 && DECL_P (base0) && DECL_P (base1)
2585 /* If we compare this as integers require equal offset. */
2586 && (!INTEGRAL_TYPE_P (TREE_TYPE (@2))
2589 (if (cmp == EQ_EXPR)
2590 { constant_boolean_node (false, type); })
2591 (if (cmp == NE_EXPR)
2592 { constant_boolean_node (true, type); })))))))))
2594 /* Simplify pointer equality compares using PTA. */
2598 (if (POINTER_TYPE_P (TREE_TYPE (@0))
2599 && ptrs_compare_unequal (@0, @1))
2600 { neeq == EQ_EXPR ? boolean_false_node : boolean_true_node; })))
2602 /* PR70920: Transform (intptr_t)x eq/ne CST to x eq/ne (typeof x) CST.
2603 and (typeof ptr_cst) x eq/ne ptr_cst to x eq/ne (typeof x) CST.
2604 Disable the transform if either operand is pointer to function.
2605 This broke pr22051-2.c for arm where function pointer
2606 canonicalizaion is not wanted. */
2610 (cmp (convert @0) INTEGER_CST@1)
2611 (if ((POINTER_TYPE_P (TREE_TYPE (@0)) && !FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@0)))
2612 && INTEGRAL_TYPE_P (TREE_TYPE (@1)))
2613 || (INTEGRAL_TYPE_P (TREE_TYPE (@0)) && POINTER_TYPE_P (TREE_TYPE (@1))
2614 && !FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@1)))))
2615 (cmp @0 (convert @1)))))
2617 /* Non-equality compare simplifications from fold_binary */
2618 (for cmp (lt gt le ge)
2619 /* Comparisons with the highest or lowest possible integer of
2620 the specified precision will have known values. */
2622 (cmp (convert?@2 @0) INTEGER_CST@1)
2623 (if ((INTEGRAL_TYPE_P (TREE_TYPE (@1)) || POINTER_TYPE_P (TREE_TYPE (@1)))
2624 && tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@0)))
2627 tree arg1_type = TREE_TYPE (@1);
2628 unsigned int prec = TYPE_PRECISION (arg1_type);
2629 wide_int max = wi::max_value (arg1_type);
2630 wide_int signed_max = wi::max_value (prec, SIGNED);
2631 wide_int min = wi::min_value (arg1_type);
2634 (if (wi::eq_p (@1, max))
2636 (if (cmp == GT_EXPR)
2637 { constant_boolean_node (false, type); })
2638 (if (cmp == GE_EXPR)
2640 (if (cmp == LE_EXPR)
2641 { constant_boolean_node (true, type); })
2642 (if (cmp == LT_EXPR)
2644 (if (wi::eq_p (@1, min))
2646 (if (cmp == LT_EXPR)
2647 { constant_boolean_node (false, type); })
2648 (if (cmp == LE_EXPR)
2650 (if (cmp == GE_EXPR)
2651 { constant_boolean_node (true, type); })
2652 (if (cmp == GT_EXPR)
2654 (if (wi::eq_p (@1, max - 1))
2656 (if (cmp == GT_EXPR)
2657 (eq @2 { wide_int_to_tree (TREE_TYPE (@1), wi::add (@1, 1)); }))
2658 (if (cmp == LE_EXPR)
2659 (ne @2 { wide_int_to_tree (TREE_TYPE (@1), wi::add (@1, 1)); }))))
2660 (if (wi::eq_p (@1, min + 1))
2662 (if (cmp == GE_EXPR)
2663 (ne @2 { wide_int_to_tree (TREE_TYPE (@1), wi::sub (@1, 1)); }))
2664 (if (cmp == LT_EXPR)
2665 (eq @2 { wide_int_to_tree (TREE_TYPE (@1), wi::sub (@1, 1)); }))))
2666 (if (wi::eq_p (@1, signed_max)
2667 && TYPE_UNSIGNED (arg1_type)
2668 /* We will flip the signedness of the comparison operator
2669 associated with the mode of @1, so the sign bit is
2670 specified by this mode. Check that @1 is the signed
2671 max associated with this sign bit. */
2672 && prec == GET_MODE_PRECISION (TYPE_MODE (arg1_type))
2673 /* signed_type does not work on pointer types. */
2674 && INTEGRAL_TYPE_P (arg1_type))
2675 /* The following case also applies to X < signed_max+1
2676 and X >= signed_max+1 because previous transformations. */
2677 (if (cmp == LE_EXPR || cmp == GT_EXPR)
2678 (with { tree st = signed_type_for (arg1_type); }
2679 (if (cmp == LE_EXPR)
2680 (ge (convert:st @0) { build_zero_cst (st); })
2681 (lt (convert:st @0) { build_zero_cst (st); }))))))))))
2683 (for cmp (unordered ordered unlt unle ungt unge uneq ltgt)
2684 /* If the second operand is NaN, the result is constant. */
2687 (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
2688 && (cmp != LTGT_EXPR || ! flag_trapping_math))
2689 { constant_boolean_node (cmp == ORDERED_EXPR || cmp == LTGT_EXPR
2690 ? false : true, type); })))
2692 /* bool_var != 0 becomes bool_var. */
2694 (ne @0 integer_zerop)
2695 (if (TREE_CODE (TREE_TYPE (@0)) == BOOLEAN_TYPE
2696 && types_match (type, TREE_TYPE (@0)))
2698 /* bool_var == 1 becomes bool_var. */
2700 (eq @0 integer_onep)
2701 (if (TREE_CODE (TREE_TYPE (@0)) == BOOLEAN_TYPE
2702 && types_match (type, TREE_TYPE (@0)))
2705 bool_var == 0 becomes !bool_var or
2706 bool_var != 1 becomes !bool_var
2707 here because that only is good in assignment context as long
2708 as we require a tcc_comparison in GIMPLE_CONDs where we'd
2709 replace if (x == 0) with tem = ~x; if (tem != 0) which is
2710 clearly less optimal and which we'll transform again in forwprop. */
2712 /* When one argument is a constant, overflow detection can be simplified.
2713 Currently restricted to single use so as not to interfere too much with
2714 ADD_OVERFLOW detection in tree-ssa-math-opts.c.
2715 A + CST CMP A -> A CMP' CST' */
2716 (for cmp (lt le ge gt)
2719 (cmp:c (plus@2 @0 INTEGER_CST@1) @0)
2720 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
2721 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))
2724 (out @0 { wide_int_to_tree (TREE_TYPE (@0), wi::max_value
2725 (TYPE_PRECISION (TREE_TYPE (@0)), UNSIGNED) - @1); }))))
2727 /* To detect overflow in unsigned A - B, A < B is simpler than A - B > A.
2728 However, the detection logic for SUB_OVERFLOW in tree-ssa-math-opts.c
2729 expects the long form, so we restrict the transformation for now. */
2732 (cmp:c (minus@2 @0 @1) @0)
2733 (if (single_use (@2)
2734 && ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2735 && TYPE_UNSIGNED (TREE_TYPE (@0))
2736 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
2739 /* Testing for overflow is unnecessary if we already know the result. */
2744 (cmp:c (realpart (IFN_SUB_OVERFLOW@2 @0 @1)) @0)
2745 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
2746 && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
2747 (out (imagpart @2) { build_zero_cst (TREE_TYPE (@0)); }))))
2752 (cmp:c (realpart (IFN_ADD_OVERFLOW:c@2 @0 @1)) @0)
2753 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
2754 && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
2755 (out (imagpart @2) { build_zero_cst (TREE_TYPE (@0)); }))))
2757 /* For unsigned operands, -1 / B < A checks whether A * B would overflow.
2758 Simplify it to __builtin_mul_overflow (A, B, <unused>). */
2762 (cmp:c (trunc_div:s integer_all_onesp @1) @0)
2763 (if (TYPE_UNSIGNED (TREE_TYPE (@0)) && !VECTOR_TYPE_P (TREE_TYPE (@0)))
2764 (with { tree t = TREE_TYPE (@0), cpx = build_complex_type (t); }
2765 (out (imagpart (IFN_MUL_OVERFLOW:cpx @0 @1)) { build_zero_cst (t); })))))
2767 /* Simplification of math builtins. These rules must all be optimizations
2768 as well as IL simplifications. If there is a possibility that the new
2769 form could be a pessimization, the rule should go in the canonicalization
2770 section that follows this one.
2772 Rules can generally go in this section if they satisfy one of
2775 - the rule describes an identity
2777 - the rule replaces calls with something as simple as addition or
2780 - the rule contains unary calls only and simplifies the surrounding
2781 arithmetic. (The idea here is to exclude non-unary calls in which
2782 one operand is constant and in which the call is known to be cheap
2783 when the operand has that value.) */
2785 (if (flag_unsafe_math_optimizations)
2786 /* Simplify sqrt(x) * sqrt(x) -> x. */
2788 (mult (SQRT@1 @0) @1)
2789 (if (!HONOR_SNANS (type))
2792 /* Simplify sqrt(x) * sqrt(y) -> sqrt(x*y). */
2793 (for root (SQRT CBRT)
2795 (mult (root:s @0) (root:s @1))
2796 (root (mult @0 @1))))
2798 /* Simplify expN(x) * expN(y) -> expN(x+y). */
2799 (for exps (EXP EXP2 EXP10 POW10)
2801 (mult (exps:s @0) (exps:s @1))
2802 (exps (plus @0 @1))))
2804 /* Simplify a/root(b/c) into a*root(c/b). */
2805 (for root (SQRT CBRT)
2807 (rdiv @0 (root:s (rdiv:s @1 @2)))
2808 (mult @0 (root (rdiv @2 @1)))))
2810 /* Simplify x/expN(y) into x*expN(-y). */
2811 (for exps (EXP EXP2 EXP10 POW10)
2813 (rdiv @0 (exps:s @1))
2814 (mult @0 (exps (negate @1)))))
2816 (for logs (LOG LOG2 LOG10 LOG10)
2817 exps (EXP EXP2 EXP10 POW10)
2818 /* logN(expN(x)) -> x. */
2822 /* expN(logN(x)) -> x. */
2827 /* Optimize logN(func()) for various exponential functions. We
2828 want to determine the value "x" and the power "exponent" in
2829 order to transform logN(x**exponent) into exponent*logN(x). */
2830 (for logs (LOG LOG LOG LOG2 LOG2 LOG2 LOG10 LOG10)
2831 exps (EXP2 EXP10 POW10 EXP EXP10 POW10 EXP EXP2)
2834 (if (SCALAR_FLOAT_TYPE_P (type))
2840 /* Prepare to do logN(exp(exponent)) -> exponent*logN(e). */
2841 x = build_real_truncate (type, dconst_e ());
2844 /* Prepare to do logN(exp2(exponent)) -> exponent*logN(2). */
2845 x = build_real (type, dconst2);
2849 /* Prepare to do logN(exp10(exponent)) -> exponent*logN(10). */
2851 REAL_VALUE_TYPE dconst10;
2852 real_from_integer (&dconst10, VOIDmode, 10, SIGNED);
2853 x = build_real (type, dconst10);
2860 (mult (logs { x; }) @0)))))
2868 (if (SCALAR_FLOAT_TYPE_P (type))
2874 /* Prepare to do logN(sqrt(x)) -> 0.5*logN(x). */
2875 x = build_real (type, dconsthalf);
2878 /* Prepare to do logN(cbrt(x)) -> (1/3)*logN(x). */
2879 x = build_real_truncate (type, dconst_third ());
2885 (mult { x; } (logs @0))))))
2887 /* logN(pow(x,exponent)) -> exponent*logN(x). */
2888 (for logs (LOG LOG2 LOG10)
2892 (mult @1 (logs @0))))
2897 exps (EXP EXP2 EXP10 POW10)
2898 /* sqrt(expN(x)) -> expN(x*0.5). */
2901 (exps (mult @0 { build_real (type, dconsthalf); })))
2902 /* cbrt(expN(x)) -> expN(x/3). */
2905 (exps (mult @0 { build_real_truncate (type, dconst_third ()); })))
2906 /* pow(expN(x), y) -> expN(x*y). */
2909 (exps (mult @0 @1))))
2911 /* tan(atan(x)) -> x. */
2918 /* cabs(x+0i) or cabs(0+xi) -> abs(x). */
2920 (CABS (complex:C @0 real_zerop@1))
2923 /* trunc(trunc(x)) -> trunc(x), etc. */
2924 (for fns (TRUNC FLOOR CEIL ROUND NEARBYINT RINT)
2928 /* f(x) -> x if x is integer valued and f does nothing for such values. */
2929 (for fns (TRUNC FLOOR CEIL ROUND NEARBYINT RINT)
2931 (fns integer_valued_real_p@0)
2934 /* hypot(x,0) and hypot(0,x) -> abs(x). */
2936 (HYPOT:c @0 real_zerop@1)
2939 /* pow(1,x) -> 1. */
2941 (POW real_onep@0 @1)
2945 /* copysign(x,x) -> x. */
2950 /* copysign(x,y) -> fabs(x) if y is nonnegative. */
2951 (COPYSIGN @0 tree_expr_nonnegative_p@1)
2954 (for scale (LDEXP SCALBN SCALBLN)
2955 /* ldexp(0, x) -> 0. */
2957 (scale real_zerop@0 @1)
2959 /* ldexp(x, 0) -> x. */
2961 (scale @0 integer_zerop@1)
2963 /* ldexp(x, y) -> x if x is +-Inf or NaN. */
2965 (scale REAL_CST@0 @1)
2966 (if (!real_isfinite (TREE_REAL_CST_PTR (@0)))
2969 /* Canonicalization of sequences of math builtins. These rules represent
2970 IL simplifications but are not necessarily optimizations.
2972 The sincos pass is responsible for picking "optimal" implementations
2973 of math builtins, which may be more complicated and can sometimes go
2974 the other way, e.g. converting pow into a sequence of sqrts.
2975 We only want to do these canonicalizations before the pass has run. */
2977 (if (flag_unsafe_math_optimizations && canonicalize_math_p ())
2978 /* Simplify tan(x) * cos(x) -> sin(x). */
2980 (mult:c (TAN:s @0) (COS:s @0))
2983 /* Simplify x * pow(x,c) -> pow(x,c+1). */
2985 (mult:c @0 (POW:s @0 REAL_CST@1))
2986 (if (!TREE_OVERFLOW (@1))
2987 (POW @0 (plus @1 { build_one_cst (type); }))))
2989 /* Simplify sin(x) / cos(x) -> tan(x). */
2991 (rdiv (SIN:s @0) (COS:s @0))
2994 /* Simplify cos(x) / sin(x) -> 1 / tan(x). */
2996 (rdiv (COS:s @0) (SIN:s @0))
2997 (rdiv { build_one_cst (type); } (TAN @0)))
2999 /* Simplify sin(x) / tan(x) -> cos(x). */
3001 (rdiv (SIN:s @0) (TAN:s @0))
3002 (if (! HONOR_NANS (@0)
3003 && ! HONOR_INFINITIES (@0))
3006 /* Simplify tan(x) / sin(x) -> 1.0 / cos(x). */
3008 (rdiv (TAN:s @0) (SIN:s @0))
3009 (if (! HONOR_NANS (@0)
3010 && ! HONOR_INFINITIES (@0))
3011 (rdiv { build_one_cst (type); } (COS @0))))
3013 /* Simplify pow(x,y) * pow(x,z) -> pow(x,y+z). */
3015 (mult (POW:s @0 @1) (POW:s @0 @2))
3016 (POW @0 (plus @1 @2)))
3018 /* Simplify pow(x,y) * pow(z,y) -> pow(x*z,y). */
3020 (mult (POW:s @0 @1) (POW:s @2 @1))
3021 (POW (mult @0 @2) @1))
3023 /* Simplify powi(x,y) * powi(z,y) -> powi(x*z,y). */
3025 (mult (POWI:s @0 @1) (POWI:s @2 @1))
3026 (POWI (mult @0 @2) @1))
3028 /* Simplify pow(x,c) / x -> pow(x,c-1). */
3030 (rdiv (POW:s @0 REAL_CST@1) @0)
3031 (if (!TREE_OVERFLOW (@1))
3032 (POW @0 (minus @1 { build_one_cst (type); }))))
3034 /* Simplify x / pow (y,z) -> x * pow(y,-z). */
3036 (rdiv @0 (POW:s @1 @2))
3037 (mult @0 (POW @1 (negate @2))))
3042 /* sqrt(sqrt(x)) -> pow(x,1/4). */
3045 (pows @0 { build_real (type, dconst_quarter ()); }))
3046 /* sqrt(cbrt(x)) -> pow(x,1/6). */
3049 (pows @0 { build_real_truncate (type, dconst_sixth ()); }))
3050 /* cbrt(sqrt(x)) -> pow(x,1/6). */
3053 (pows @0 { build_real_truncate (type, dconst_sixth ()); }))
3054 /* cbrt(cbrt(x)) -> pow(x,1/9), iff x is nonnegative. */
3056 (cbrts (cbrts tree_expr_nonnegative_p@0))
3057 (pows @0 { build_real_truncate (type, dconst_ninth ()); }))
3058 /* sqrt(pow(x,y)) -> pow(|x|,y*0.5). */
3060 (sqrts (pows @0 @1))
3061 (pows (abs @0) (mult @1 { build_real (type, dconsthalf); })))
3062 /* cbrt(pow(x,y)) -> pow(x,y/3), iff x is nonnegative. */
3064 (cbrts (pows tree_expr_nonnegative_p@0 @1))
3065 (pows @0 (mult @1 { build_real_truncate (type, dconst_third ()); })))
3066 /* pow(sqrt(x),y) -> pow(x,y*0.5). */
3068 (pows (sqrts @0) @1)
3069 (pows @0 (mult @1 { build_real (type, dconsthalf); })))
3070 /* pow(cbrt(x),y) -> pow(x,y/3) iff x is nonnegative. */
3072 (pows (cbrts tree_expr_nonnegative_p@0) @1)
3073 (pows @0 (mult @1 { build_real_truncate (type, dconst_third ()); })))
3074 /* pow(pow(x,y),z) -> pow(x,y*z) iff x is nonnegative. */
3076 (pows (pows tree_expr_nonnegative_p@0 @1) @2)
3077 (pows @0 (mult @1 @2))))
3079 /* cabs(x+xi) -> fabs(x)*sqrt(2). */
3081 (CABS (complex @0 @0))
3082 (mult (abs @0) { build_real_truncate (type, dconst_sqrt2 ()); }))
3084 /* hypot(x,x) -> fabs(x)*sqrt(2). */
3087 (mult (abs @0) { build_real_truncate (type, dconst_sqrt2 ()); }))
3089 /* cexp(x+yi) -> exp(x)*cexpi(y). */
3094 (cexps compositional_complex@0)
3095 (if (targetm.libc_has_function (function_c99_math_complex))
3097 (mult (exps@1 (realpart @0)) (realpart (cexpis:type@2 (imagpart @0))))
3098 (mult @1 (imagpart @2)))))))
3100 (if (canonicalize_math_p ())
3101 /* floor(x) -> trunc(x) if x is nonnegative. */
3105 (floors tree_expr_nonnegative_p@0)
3108 (match double_value_p
3110 (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == double_type_node)))
3111 (for froms (BUILT_IN_TRUNCL
3123 /* truncl(extend(x)) -> extend(trunc(x)), etc., if x is a double. */
3124 (if (optimize && canonicalize_math_p ())
3126 (froms (convert double_value_p@0))
3127 (convert (tos @0)))))
3129 (match float_value_p
3131 (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == float_type_node)))
3132 (for froms (BUILT_IN_TRUNCL BUILT_IN_TRUNC
3133 BUILT_IN_FLOORL BUILT_IN_FLOOR
3134 BUILT_IN_CEILL BUILT_IN_CEIL
3135 BUILT_IN_ROUNDL BUILT_IN_ROUND
3136 BUILT_IN_NEARBYINTL BUILT_IN_NEARBYINT
3137 BUILT_IN_RINTL BUILT_IN_RINT)
3138 tos (BUILT_IN_TRUNCF BUILT_IN_TRUNCF
3139 BUILT_IN_FLOORF BUILT_IN_FLOORF
3140 BUILT_IN_CEILF BUILT_IN_CEILF
3141 BUILT_IN_ROUNDF BUILT_IN_ROUNDF
3142 BUILT_IN_NEARBYINTF BUILT_IN_NEARBYINTF
3143 BUILT_IN_RINTF BUILT_IN_RINTF)
3144 /* truncl(extend(x)) and trunc(extend(x)) -> extend(truncf(x)), etc.,
3146 (if (optimize && canonicalize_math_p ()
3147 && targetm.libc_has_function (function_c99_misc))
3149 (froms (convert float_value_p@0))
3150 (convert (tos @0)))))
3152 (for froms (XFLOORL XCEILL XROUNDL XRINTL)
3153 tos (XFLOOR XCEIL XROUND XRINT)
3154 /* llfloorl(extend(x)) -> llfloor(x), etc., if x is a double. */
3155 (if (optimize && canonicalize_math_p ())
3157 (froms (convert double_value_p@0))
3160 (for froms (XFLOORL XCEILL XROUNDL XRINTL
3161 XFLOOR XCEIL XROUND XRINT)
3162 tos (XFLOORF XCEILF XROUNDF XRINTF)
3163 /* llfloorl(extend(x)) and llfloor(extend(x)) -> llfloorf(x), etc.,
3165 (if (optimize && canonicalize_math_p ())
3167 (froms (convert float_value_p@0))
3170 (if (canonicalize_math_p ())
3171 /* xfloor(x) -> fix_trunc(x) if x is nonnegative. */
3172 (for floors (IFLOOR LFLOOR LLFLOOR)
3174 (floors tree_expr_nonnegative_p@0)
3177 (if (canonicalize_math_p ())
3178 /* xfloor(x) -> fix_trunc(x), etc., if x is integer valued. */
3179 (for fns (IFLOOR LFLOOR LLFLOOR
3181 IROUND LROUND LLROUND)
3183 (fns integer_valued_real_p@0)
3185 (if (!flag_errno_math)
3186 /* xrint(x) -> fix_trunc(x), etc., if x is integer valued. */
3187 (for rints (IRINT LRINT LLRINT)
3189 (rints integer_valued_real_p@0)
3192 (if (canonicalize_math_p ())
3193 (for ifn (IFLOOR ICEIL IROUND IRINT)
3194 lfn (LFLOOR LCEIL LROUND LRINT)
3195 llfn (LLFLOOR LLCEIL LLROUND LLRINT)
3196 /* Canonicalize iround (x) to lround (x) on ILP32 targets where
3197 sizeof (int) == sizeof (long). */
3198 (if (TYPE_PRECISION (integer_type_node)
3199 == TYPE_PRECISION (long_integer_type_node))
3202 (lfn:long_integer_type_node @0)))
3203 /* Canonicalize llround (x) to lround (x) on LP64 targets where
3204 sizeof (long long) == sizeof (long). */
3205 (if (TYPE_PRECISION (long_long_integer_type_node)
3206 == TYPE_PRECISION (long_integer_type_node))
3209 (lfn:long_integer_type_node @0)))))
3211 /* cproj(x) -> x if we're ignoring infinities. */
3214 (if (!HONOR_INFINITIES (type))
3217 /* If the real part is inf and the imag part is known to be
3218 nonnegative, return (inf + 0i). */
3220 (CPROJ (complex REAL_CST@0 tree_expr_nonnegative_p@1))
3221 (if (real_isinf (TREE_REAL_CST_PTR (@0)))
3222 { build_complex_inf (type, false); }))
3224 /* If the imag part is inf, return (inf+I*copysign(0,imag)). */
3226 (CPROJ (complex @0 REAL_CST@1))
3227 (if (real_isinf (TREE_REAL_CST_PTR (@1)))
3228 { build_complex_inf (type, TREE_REAL_CST_PTR (@1)->sign); }))
3234 (pows @0 REAL_CST@1)
3236 const REAL_VALUE_TYPE *value = TREE_REAL_CST_PTR (@1);
3237 REAL_VALUE_TYPE tmp;
3240 /* pow(x,0) -> 1. */
3241 (if (real_equal (value, &dconst0))
3242 { build_real (type, dconst1); })
3243 /* pow(x,1) -> x. */
3244 (if (real_equal (value, &dconst1))
3246 /* pow(x,-1) -> 1/x. */
3247 (if (real_equal (value, &dconstm1))
3248 (rdiv { build_real (type, dconst1); } @0))
3249 /* pow(x,0.5) -> sqrt(x). */
3250 (if (flag_unsafe_math_optimizations
3251 && canonicalize_math_p ()
3252 && real_equal (value, &dconsthalf))
3254 /* pow(x,1/3) -> cbrt(x). */
3255 (if (flag_unsafe_math_optimizations
3256 && canonicalize_math_p ()
3257 && (tmp = real_value_truncate (TYPE_MODE (type), dconst_third ()),
3258 real_equal (value, &tmp)))
3261 /* powi(1,x) -> 1. */
3263 (POWI real_onep@0 @1)
3267 (POWI @0 INTEGER_CST@1)
3269 /* powi(x,0) -> 1. */
3270 (if (wi::eq_p (@1, 0))
3271 { build_real (type, dconst1); })
3272 /* powi(x,1) -> x. */
3273 (if (wi::eq_p (@1, 1))
3275 /* powi(x,-1) -> 1/x. */
3276 (if (wi::eq_p (@1, -1))
3277 (rdiv { build_real (type, dconst1); } @0))))
3279 /* Narrowing of arithmetic and logical operations.
3281 These are conceptually similar to the transformations performed for
3282 the C/C++ front-ends by shorten_binary_op and shorten_compare. Long
3283 term we want to move all that code out of the front-ends into here. */
3285 /* If we have a narrowing conversion of an arithmetic operation where
3286 both operands are widening conversions from the same type as the outer
3287 narrowing conversion. Then convert the innermost operands to a suitable
3288 unsigned type (to avoid introducing undefined behavior), perform the
3289 operation and convert the result to the desired type. */
3290 (for op (plus minus)
3292 (convert (op:s (convert@2 @0) (convert?@3 @1)))
3293 (if (INTEGRAL_TYPE_P (type)
3294 /* We check for type compatibility between @0 and @1 below,
3295 so there's no need to check that @1/@3 are integral types. */
3296 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
3297 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
3298 /* The precision of the type of each operand must match the
3299 precision of the mode of each operand, similarly for the
3301 && (TYPE_PRECISION (TREE_TYPE (@0))
3302 == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (@0))))
3303 && (TYPE_PRECISION (TREE_TYPE (@1))
3304 == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (@1))))
3305 && TYPE_PRECISION (type) == GET_MODE_PRECISION (TYPE_MODE (type))
3306 /* The inner conversion must be a widening conversion. */
3307 && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
3308 && types_match (@0, type)
3309 && (types_match (@0, @1)
3310 /* Or the second operand is const integer or converted const
3311 integer from valueize. */
3312 || TREE_CODE (@1) == INTEGER_CST))
3313 (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
3314 (op @0 (convert @1))
3315 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
3316 (convert (op (convert:utype @0)
3317 (convert:utype @1))))))))
3319 /* This is another case of narrowing, specifically when there's an outer
3320 BIT_AND_EXPR which masks off bits outside the type of the innermost
3321 operands. Like the previous case we have to convert the operands
3322 to unsigned types to avoid introducing undefined behavior for the
3323 arithmetic operation. */
3324 (for op (minus plus)
3326 (bit_and (op:s (convert@2 @0) (convert@3 @1)) INTEGER_CST@4)
3327 (if (INTEGRAL_TYPE_P (type)
3328 /* We check for type compatibility between @0 and @1 below,
3329 so there's no need to check that @1/@3 are integral types. */
3330 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
3331 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
3332 /* The precision of the type of each operand must match the
3333 precision of the mode of each operand, similarly for the
3335 && (TYPE_PRECISION (TREE_TYPE (@0))
3336 == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (@0))))
3337 && (TYPE_PRECISION (TREE_TYPE (@1))
3338 == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (@1))))
3339 && TYPE_PRECISION (type) == GET_MODE_PRECISION (TYPE_MODE (type))
3340 /* The inner conversion must be a widening conversion. */
3341 && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
3342 && types_match (@0, @1)
3343 && (tree_int_cst_min_precision (@4, TYPE_SIGN (TREE_TYPE (@0)))
3344 <= TYPE_PRECISION (TREE_TYPE (@0)))
3345 && (wi::bit_and (@4, wi::mask (TYPE_PRECISION (TREE_TYPE (@0)),
3346 true, TYPE_PRECISION (type))) == 0))
3347 (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
3348 (with { tree ntype = TREE_TYPE (@0); }
3349 (convert (bit_and (op @0 @1) (convert:ntype @4))))
3350 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
3351 (convert (bit_and (op (convert:utype @0) (convert:utype @1))
3352 (convert:utype @4))))))))
3354 /* Transform (@0 < @1 and @0 < @2) to use min,
3355 (@0 > @1 and @0 > @2) to use max */
3356 (for op (lt le gt ge)
3357 ext (min min max max)
3359 (bit_and (op:cs @0 @1) (op:cs @0 @2))
3360 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
3361 && TREE_CODE (@0) != INTEGER_CST)
3362 (op @0 (ext @1 @2)))))
3365 /* signbit(x) -> 0 if x is nonnegative. */
3366 (SIGNBIT tree_expr_nonnegative_p@0)
3367 { integer_zero_node; })
3370 /* signbit(x) -> x<0 if x doesn't have signed zeros. */
3372 (if (!HONOR_SIGNED_ZEROS (@0))
3373 (convert (lt @0 { build_real (TREE_TYPE (@0), dconst0); }))))
3375 /* Transform comparisons of the form X +- C1 CMP C2 to X CMP C2 -+ C1. */
3377 (for op (plus minus)
3380 (cmp (op@3 @0 INTEGER_CST@1) INTEGER_CST@2)
3381 (if (!TREE_OVERFLOW (@1) && !TREE_OVERFLOW (@2)
3382 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0))
3383 && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@0))
3384 && !TYPE_SATURATING (TREE_TYPE (@0)))
3385 (with { tree res = int_const_binop (rop, @2, @1); }
3386 (if (TREE_OVERFLOW (res))
3387 { constant_boolean_node (cmp == NE_EXPR, type); }
3388 (if (single_use (@3))
3389 (cmp @0 { res; }))))))))
3390 (for cmp (lt le gt ge)
3391 (for op (plus minus)
3394 (cmp (op@3 @0 INTEGER_CST@1) INTEGER_CST@2)
3395 (if (!TREE_OVERFLOW (@1) && !TREE_OVERFLOW (@2)
3396 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
3397 (with { tree res = int_const_binop (rop, @2, @1); }
3398 (if (TREE_OVERFLOW (res))
3400 fold_overflow_warning (("assuming signed overflow does not occur "
3401 "when simplifying conditional to constant"),
3402 WARN_STRICT_OVERFLOW_CONDITIONAL);
3403 bool less = cmp == LE_EXPR || cmp == LT_EXPR;
3404 /* wi::ges_p (@2, 0) should be sufficient for a signed type. */
3405 bool ovf_high = wi::lt_p (@1, 0, TYPE_SIGN (TREE_TYPE (@1)))
3406 != (op == MINUS_EXPR);
3407 constant_boolean_node (less == ovf_high, type);
3409 (if (single_use (@3))
3412 fold_overflow_warning (("assuming signed overflow does not occur "
3413 "when changing X +- C1 cmp C2 to "
3415 WARN_STRICT_OVERFLOW_COMPARISON);
3417 (cmp @0 { res; })))))))))
3419 /* Canonicalizations of BIT_FIELD_REFs. */
3422 (BIT_FIELD_REF @0 @1 @2)
3424 (if (TREE_CODE (TREE_TYPE (@0)) == COMPLEX_TYPE
3425 && tree_int_cst_equal (@1, TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))))
3427 (if (integer_zerop (@2))
3428 (view_convert (realpart @0)))
3429 (if (tree_int_cst_equal (@2, TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))))
3430 (view_convert (imagpart @0)))))
3431 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
3432 && INTEGRAL_TYPE_P (type)
3433 /* On GIMPLE this should only apply to register arguments. */
3434 && (! GIMPLE || is_gimple_reg (@0))
3435 /* A bit-field-ref that referenced the full argument can be stripped. */
3436 && ((compare_tree_int (@1, TYPE_PRECISION (TREE_TYPE (@0))) == 0
3437 && integer_zerop (@2))
3438 /* Low-parts can be reduced to integral conversions.
3439 ??? The following doesn't work for PDP endian. */
3440 || (BYTES_BIG_ENDIAN == WORDS_BIG_ENDIAN
3441 /* Don't even think about BITS_BIG_ENDIAN. */
3442 && TYPE_PRECISION (TREE_TYPE (@0)) % BITS_PER_UNIT == 0
3443 && TYPE_PRECISION (type) % BITS_PER_UNIT == 0
3444 && compare_tree_int (@2, (BYTES_BIG_ENDIAN
3445 ? (TYPE_PRECISION (TREE_TYPE (@0))
3446 - TYPE_PRECISION (type))
3450 /* Simplify vector extracts. */
3453 (BIT_FIELD_REF CONSTRUCTOR@0 @1 @2)
3454 (if (VECTOR_TYPE_P (TREE_TYPE (@0))
3455 && (types_match (type, TREE_TYPE (TREE_TYPE (@0)))
3456 || (VECTOR_TYPE_P (type)
3457 && types_match (TREE_TYPE (type), TREE_TYPE (TREE_TYPE (@0))))))
3460 tree ctor = (TREE_CODE (@0) == SSA_NAME
3461 ? gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0)) : @0);
3462 tree eltype = TREE_TYPE (TREE_TYPE (ctor));
3463 unsigned HOST_WIDE_INT width = tree_to_uhwi (TYPE_SIZE (eltype));
3464 unsigned HOST_WIDE_INT n = tree_to_uhwi (@1);
3465 unsigned HOST_WIDE_INT idx = tree_to_uhwi (@2);
3468 && (idx % width) == 0
3470 && ((idx + n) / width) <= TYPE_VECTOR_SUBPARTS (TREE_TYPE (ctor)))
3475 /* Constructor elements can be subvectors. */
3476 unsigned HOST_WIDE_INT k = 1;
3477 if (CONSTRUCTOR_NELTS (ctor) != 0)
3479 tree cons_elem = TREE_TYPE (CONSTRUCTOR_ELT (ctor, 0)->value);
3480 if (TREE_CODE (cons_elem) == VECTOR_TYPE)
3481 k = TYPE_VECTOR_SUBPARTS (cons_elem);
3485 /* We keep an exact subset of the constructor elements. */
3486 (if ((idx % k) == 0 && (n % k) == 0)
3487 (if (CONSTRUCTOR_NELTS (ctor) == 0)
3488 { build_constructor (type, NULL); }
3495 (if (idx < CONSTRUCTOR_NELTS (ctor))
3496 { CONSTRUCTOR_ELT (ctor, idx)->value; }
3497 { build_zero_cst (type); })
3499 vec<constructor_elt, va_gc> *vals;
3500 vec_alloc (vals, n);
3501 for (unsigned i = 0;
3502 i < n && idx + i < CONSTRUCTOR_NELTS (ctor); ++i)
3503 CONSTRUCTOR_APPEND_ELT (vals, NULL_TREE,
3504 CONSTRUCTOR_ELT (ctor, idx + i)->value);
3505 build_constructor (type, vals);
3507 /* The bitfield references a single constructor element. */
3508 (if (idx + n <= (idx / k + 1) * k)
3510 (if (CONSTRUCTOR_NELTS (ctor) <= idx / k)
3511 { build_zero_cst (type); })
3513 { CONSTRUCTOR_ELT (ctor, idx / k)->value; })
3514 (BIT_FIELD_REF { CONSTRUCTOR_ELT (ctor, idx / k)->value; }
3515 @1 { bitsize_int ((idx % k) * width); })))))))))