1 /* Match-and-simplify patterns for shared GENERIC and GIMPLE folding.
2 This file is consumed by genmatch which produces gimple-match.c
3 and generic-match.c from it.
5 Copyright (C) 2014-2016 Free Software Foundation, Inc.
6 Contributed by Richard Biener <rguenther@suse.de>
7 and Prathamesh Kulkarni <bilbotheelffriend@gmail.com>
9 This file is part of GCC.
11 GCC is free software; you can redistribute it and/or modify it under
12 the terms of the GNU General Public License as published by the Free
13 Software Foundation; either version 3, or (at your option) any later
16 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
17 WARRANTY; without even the implied warranty of MERCHANTABILITY or
18 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21 You should have received a copy of the GNU General Public License
22 along with GCC; see the file COPYING3. If not see
23 <http://www.gnu.org/licenses/>. */
26 /* Generic tree predicates we inherit. */
28 integer_onep integer_zerop integer_all_onesp integer_minus_onep
29 integer_each_onep integer_truep integer_nonzerop
30 real_zerop real_onep real_minus_onep
33 tree_expr_nonnegative_p
39 (define_operator_list tcc_comparison
40 lt le eq ne ge gt unordered ordered unlt unle ungt unge uneq ltgt)
41 (define_operator_list inverted_tcc_comparison
42 ge gt ne eq lt le ordered unordered ge gt le lt ltgt uneq)
43 (define_operator_list inverted_tcc_comparison_with_nans
44 unge ungt ne eq unlt unle ordered unordered ge gt le lt ltgt uneq)
45 (define_operator_list swapped_tcc_comparison
46 gt ge eq ne le lt unordered ordered ungt unge unlt unle uneq ltgt)
47 (define_operator_list simple_comparison lt le eq ne ge gt)
48 (define_operator_list swapped_simple_comparison gt ge eq ne le lt)
50 #include "cfn-operators.pd"
52 /* Define operand lists for math rounding functions {,i,l,ll}FN,
53 where the versions prefixed with "i" return an int, those prefixed with
54 "l" return a long and those prefixed with "ll" return a long long.
56 Also define operand lists:
58 X<FN>F for all float functions, in the order i, l, ll
59 X<FN> for all double functions, in the same order
60 X<FN>L for all long double functions, in the same order. */
61 #define DEFINE_INT_AND_FLOAT_ROUND_FN(FN) \
62 (define_operator_list X##FN##F BUILT_IN_I##FN##F \
65 (define_operator_list X##FN BUILT_IN_I##FN \
68 (define_operator_list X##FN##L BUILT_IN_I##FN##L \
72 DEFINE_INT_AND_FLOAT_ROUND_FN (FLOOR)
73 DEFINE_INT_AND_FLOAT_ROUND_FN (CEIL)
74 DEFINE_INT_AND_FLOAT_ROUND_FN (ROUND)
75 DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
77 /* Simplifications of operations with one constant operand and
78 simplifications to constants or single values. */
80 (for op (plus pointer_plus minus bit_ior bit_xor)
85 /* 0 +p index -> (type)index */
87 (pointer_plus integer_zerop @1)
88 (non_lvalue (convert @1)))
90 /* See if ARG1 is zero and X + ARG1 reduces to X.
91 Likewise if the operands are reversed. */
93 (plus:c @0 real_zerop@1)
94 (if (fold_real_zero_addition_p (type, @1, 0))
97 /* See if ARG1 is zero and X - ARG1 reduces to X. */
99 (minus @0 real_zerop@1)
100 (if (fold_real_zero_addition_p (type, @1, 1))
104 This is unsafe for certain floats even in non-IEEE formats.
105 In IEEE, it is unsafe because it does wrong for NaNs.
106 Also note that operand_equal_p is always false if an operand
110 (if (!FLOAT_TYPE_P (type) || !HONOR_NANS (type))
111 { build_zero_cst (type); }))
114 (mult @0 integer_zerop@1)
117 /* Maybe fold x * 0 to 0. The expressions aren't the same
118 when x is NaN, since x * 0 is also NaN. Nor are they the
119 same in modes with signed zeros, since multiplying a
120 negative value by 0 gives -0, not +0. */
122 (mult @0 real_zerop@1)
123 (if (!HONOR_NANS (type) && !HONOR_SIGNED_ZEROS (type))
126 /* In IEEE floating point, x*1 is not equivalent to x for snans.
127 Likewise for complex arithmetic with signed zeros. */
130 (if (!HONOR_SNANS (type)
131 && (!HONOR_SIGNED_ZEROS (type)
132 || !COMPLEX_FLOAT_TYPE_P (type)))
135 /* Transform x * -1.0 into -x. */
137 (mult @0 real_minus_onep)
138 (if (!HONOR_SNANS (type)
139 && (!HONOR_SIGNED_ZEROS (type)
140 || !COMPLEX_FLOAT_TYPE_P (type)))
143 /* Make sure to preserve divisions by zero. This is the reason why
144 we don't simplify x / x to 1 or 0 / x to 0. */
145 (for op (mult trunc_div ceil_div floor_div round_div exact_div)
151 (for div (trunc_div ceil_div floor_div round_div exact_div)
153 (div @0 integer_minus_onep@1)
154 (if (!TYPE_UNSIGNED (type))
157 /* For unsigned integral types, FLOOR_DIV_EXPR is the same as
158 TRUNC_DIV_EXPR. Rewrite into the latter in this case. */
161 (if ((INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
162 && TYPE_UNSIGNED (type))
165 /* Combine two successive divisions. Note that combining ceil_div
166 and floor_div is trickier and combining round_div even more so. */
167 (for div (trunc_div exact_div)
169 (div (div @0 INTEGER_CST@1) INTEGER_CST@2)
172 wide_int mul = wi::mul (@1, @2, TYPE_SIGN (type), &overflow_p);
175 (div @0 { wide_int_to_tree (type, mul); })
176 (if (TYPE_UNSIGNED (type)
177 || mul != wi::min_value (TYPE_PRECISION (type), SIGNED))
178 { build_zero_cst (type); })))))
180 /* Optimize A / A to 1.0 if we don't care about
181 NaNs or Infinities. */
184 (if (FLOAT_TYPE_P (type)
185 && ! HONOR_NANS (type)
186 && ! HONOR_INFINITIES (type))
187 { build_one_cst (type); }))
189 /* Optimize -A / A to -1.0 if we don't care about
190 NaNs or Infinities. */
192 (rdiv:C @0 (negate @0))
193 (if (FLOAT_TYPE_P (type)
194 && ! HONOR_NANS (type)
195 && ! HONOR_INFINITIES (type))
196 { build_minus_one_cst (type); }))
198 /* PR71078: x / abs(x) -> copysign (1.0, x) */
200 (rdiv:C (convert? @0) (convert? (abs @0)))
201 (if (SCALAR_FLOAT_TYPE_P (type)
202 && ! HONOR_NANS (type)
203 && ! HONOR_INFINITIES (type))
205 (if (types_match (type, float_type_node))
206 (BUILT_IN_COPYSIGNF { build_one_cst (type); } (convert @0)))
207 (if (types_match (type, double_type_node))
208 (BUILT_IN_COPYSIGN { build_one_cst (type); } (convert @0)))
209 (if (types_match (type, long_double_type_node))
210 (BUILT_IN_COPYSIGNL { build_one_cst (type); } (convert @0))))))
212 /* In IEEE floating point, x/1 is not equivalent to x for snans. */
215 (if (!HONOR_SNANS (type))
218 /* In IEEE floating point, x/-1 is not equivalent to -x for snans. */
220 (rdiv @0 real_minus_onep)
221 (if (!HONOR_SNANS (type))
224 (if (flag_reciprocal_math)
225 /* Convert (A/B)/C to A/(B*C) */
227 (rdiv (rdiv:s @0 @1) @2)
228 (rdiv @0 (mult @1 @2)))
230 /* Convert A/(B/C) to (A/B)*C */
232 (rdiv @0 (rdiv:s @1 @2))
233 (mult (rdiv @0 @1) @2)))
235 /* Optimize (X & (-A)) / A where A is a power of 2, to X >> log2(A) */
236 (for div (trunc_div ceil_div floor_div round_div exact_div)
238 (div (convert? (bit_and @0 INTEGER_CST@1)) INTEGER_CST@2)
239 (if (integer_pow2p (@2)
240 && tree_int_cst_sgn (@2) > 0
241 && wi::add (@2, @1) == 0
242 && tree_nop_conversion_p (type, TREE_TYPE (@0)))
243 (rshift (convert @0) { build_int_cst (integer_type_node,
244 wi::exact_log2 (@2)); }))))
246 /* If ARG1 is a constant, we can convert this to a multiply by the
247 reciprocal. This does not have the same rounding properties,
248 so only do this if -freciprocal-math. We can actually
249 always safely do it if ARG1 is a power of two, but it's hard to
250 tell if it is or not in a portable manner. */
251 (for cst (REAL_CST COMPLEX_CST VECTOR_CST)
255 (if (flag_reciprocal_math
258 { tree tem = const_binop (RDIV_EXPR, type, build_one_cst (type), @1); }
260 (mult @0 { tem; } )))
261 (if (cst != COMPLEX_CST)
262 (with { tree inverse = exact_inverse (type, @1); }
264 (mult @0 { inverse; } ))))))))
266 /* Same applies to modulo operations, but fold is inconsistent here
267 and simplifies 0 % x to 0, only preserving literal 0 % 0. */
268 (for mod (ceil_mod floor_mod round_mod trunc_mod)
269 /* 0 % X is always zero. */
271 (mod integer_zerop@0 @1)
272 /* But not for 0 % 0 so that we can get the proper warnings and errors. */
273 (if (!integer_zerop (@1))
275 /* X % 1 is always zero. */
277 (mod @0 integer_onep)
278 { build_zero_cst (type); })
279 /* X % -1 is zero. */
281 (mod @0 integer_minus_onep@1)
282 (if (!TYPE_UNSIGNED (type))
283 { build_zero_cst (type); }))
284 /* (X % Y) % Y is just X % Y. */
286 (mod (mod@2 @0 @1) @1)
288 /* From extract_muldiv_1: (X * C1) % C2 is zero if C1 is a multiple of C2. */
290 (mod (mult @0 INTEGER_CST@1) INTEGER_CST@2)
291 (if (ANY_INTEGRAL_TYPE_P (type)
292 && TYPE_OVERFLOW_UNDEFINED (type)
293 && wi::multiple_of_p (@1, @2, TYPE_SIGN (type)))
294 { build_zero_cst (type); })))
296 /* X % -C is the same as X % C. */
298 (trunc_mod @0 INTEGER_CST@1)
299 (if (TYPE_SIGN (type) == SIGNED
300 && !TREE_OVERFLOW (@1)
302 && !TYPE_OVERFLOW_TRAPS (type)
303 /* Avoid this transformation if C is INT_MIN, i.e. C == -C. */
304 && !sign_bit_p (@1, @1))
305 (trunc_mod @0 (negate @1))))
307 /* X % -Y is the same as X % Y. */
309 (trunc_mod @0 (convert? (negate @1)))
310 (if (INTEGRAL_TYPE_P (type)
311 && !TYPE_UNSIGNED (type)
312 && !TYPE_OVERFLOW_TRAPS (type)
313 && tree_nop_conversion_p (type, TREE_TYPE (@1))
314 /* Avoid this transformation if X might be INT_MIN or
315 Y might be -1, because we would then change valid
316 INT_MIN % -(-1) into invalid INT_MIN % -1. */
317 && (expr_not_equal_to (@0, TYPE_MIN_VALUE (type))
318 || expr_not_equal_to (@1, wi::minus_one (TYPE_PRECISION
320 (trunc_mod @0 (convert @1))))
322 /* X - (X / Y) * Y is the same as X % Y. */
324 (minus (convert1? @2) (convert2? (mult:c (trunc_div @0 @1) @1)))
325 /* We cannot use matching captures here, since in the case of
326 constants we really want the type of @0, not @2. */
327 (if (operand_equal_p (@0, @2, 0)
328 && (INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type)))
329 (convert (trunc_mod @0 @1))))
331 /* Optimize TRUNC_MOD_EXPR by a power of two into a BIT_AND_EXPR,
332 i.e. "X % C" into "X & (C - 1)", if X and C are positive.
333 Also optimize A % (C << N) where C is a power of 2,
334 to A & ((C << N) - 1). */
335 (match (power_of_two_cand @1)
337 (match (power_of_two_cand @1)
338 (lshift INTEGER_CST@1 @2))
339 (for mod (trunc_mod floor_mod)
341 (mod @0 (convert?@3 (power_of_two_cand@1 @2)))
342 (if ((TYPE_UNSIGNED (type)
343 || tree_expr_nonnegative_p (@0))
344 && tree_nop_conversion_p (type, TREE_TYPE (@3))
345 && integer_pow2p (@2) && tree_int_cst_sgn (@2) > 0)
346 (bit_and @0 (convert (minus @1 { build_int_cst (TREE_TYPE (@1), 1); }))))))
348 /* Simplify (unsigned t * 2)/2 -> unsigned t & 0x7FFFFFFF. */
350 (trunc_div (mult @0 integer_pow2p@1) @1)
351 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
352 (bit_and @0 { wide_int_to_tree
353 (type, wi::mask (TYPE_PRECISION (type) - wi::exact_log2 (@1),
354 false, TYPE_PRECISION (type))); })))
356 /* Simplify (unsigned t / 2) * 2 -> unsigned t & ~1. */
358 (mult (trunc_div @0 integer_pow2p@1) @1)
359 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
360 (bit_and @0 (negate @1))))
362 /* Simplify (t * 2) / 2) -> t. */
363 (for div (trunc_div ceil_div floor_div round_div exact_div)
365 (div (mult @0 @1) @1)
366 (if (ANY_INTEGRAL_TYPE_P (type)
367 && TYPE_OVERFLOW_UNDEFINED (type))
371 /* Simplify cos(-x) and cos(|x|) -> cos(x). Similarly for cosh. */
376 /* Simplify pow(-x, y) and pow(|x|,y) -> pow(x,y) if y is an even integer. */
379 (pows (op @0) REAL_CST@1)
380 (with { HOST_WIDE_INT n; }
381 (if (real_isinteger (&TREE_REAL_CST (@1), &n) && (n & 1) == 0)
383 /* Likewise for powi. */
386 (pows (op @0) INTEGER_CST@1)
387 (if (wi::bit_and (@1, 1) == 0)
389 /* Strip negate and abs from both operands of hypot. */
397 /* copysign(-x, y) and copysign(abs(x), y) -> copysign(x, y). */
398 (for copysigns (COPYSIGN)
400 (copysigns (op @0) @1)
403 /* abs(x)*abs(x) -> x*x. Should be valid for all types. */
408 /* cos(copysign(x, y)) -> cos(x). Similarly for cosh. */
412 (coss (copysigns @0 @1))
415 /* pow(copysign(x, y), z) -> pow(x, z) if z is an even integer. */
419 (pows (copysigns @0 @2) REAL_CST@1)
420 (with { HOST_WIDE_INT n; }
421 (if (real_isinteger (&TREE_REAL_CST (@1), &n) && (n & 1) == 0)
423 /* Likewise for powi. */
427 (pows (copysigns @0 @2) INTEGER_CST@1)
428 (if (wi::bit_and (@1, 1) == 0)
433 /* hypot(copysign(x, y), z) -> hypot(x, z). */
435 (hypots (copysigns @0 @1) @2)
437 /* hypot(x, copysign(y, z)) -> hypot(x, y). */
439 (hypots @0 (copysigns @1 @2))
442 /* copysign(copysign(x, y), z) -> copysign(x, z). */
443 (for copysigns (COPYSIGN)
445 (copysigns (copysigns @0 @1) @2)
448 /* copysign(x,y)*copysign(x,y) -> x*x. */
449 (for copysigns (COPYSIGN)
451 (mult (copysigns@2 @0 @1) @2)
454 /* ccos(-x) -> ccos(x). Similarly for ccosh. */
455 (for ccoss (CCOS CCOSH)
460 /* cabs(-x) and cos(conj(x)) -> cabs(x). */
461 (for ops (conj negate)
467 /* Fold (a * (1 << b)) into (a << b) */
469 (mult:c @0 (convert? (lshift integer_onep@1 @2)))
470 (if (! FLOAT_TYPE_P (type)
471 && (element_precision (type) <= element_precision (TREE_TYPE (@1))
472 || TYPE_UNSIGNED (TREE_TYPE (@1))))
475 /* Fold (C1/X)*C2 into (C1*C2)/X. */
477 (mult (rdiv@3 REAL_CST@0 @1) REAL_CST@2)
478 (if (flag_associative_math
481 { tree tem = const_binop (MULT_EXPR, type, @0, @2); }
483 (rdiv { tem; } @1)))))
485 /* Convert C1/(X*C2) into (C1/C2)/X */
487 (rdiv REAL_CST@0 (mult @1 REAL_CST@2))
488 (if (flag_reciprocal_math)
490 { tree tem = const_binop (RDIV_EXPR, type, @0, @2); }
492 (rdiv { tem; } @1)))))
494 /* Simplify ~X & X as zero. */
496 (bit_and:c (convert? @0) (convert? (bit_not @0)))
497 { build_zero_cst (type); })
499 /* Fold (A & ~B) - (A & B) into (A ^ B) - B. */
501 (minus (bit_and:cs @0 (bit_not @1)) (bit_and:cs @0 @1))
502 (minus (bit_xor @0 @1) @1))
504 (minus (bit_and:s @0 INTEGER_CST@2) (bit_and:s @0 INTEGER_CST@1))
505 (if (wi::bit_not (@2) == @1)
506 (minus (bit_xor @0 @1) @1)))
508 /* Fold (A & B) - (A & ~B) into B - (A ^ B). */
510 (minus (bit_and:cs @0 @1) (bit_and:cs @0 (bit_not @1)))
511 (minus @1 (bit_xor @0 @1)))
513 /* Simplify (X & ~Y) | (~X & Y) -> X ^ Y. */
515 (bit_ior (bit_and:c @0 (bit_not @1)) (bit_and:c (bit_not @0) @1))
518 (bit_ior:c (bit_and @0 INTEGER_CST@2) (bit_and (bit_not @0) INTEGER_CST@1))
519 (if (wi::bit_not (@2) == @1)
521 /* Simplify (~X & Y) to X ^ Y if we know that (X & ~Y) is 0. */
524 (bit_and (bit_not SSA_NAME@0) INTEGER_CST@1)
525 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
526 && (get_nonzero_bits (@0) & wi::bit_not (@1)) == 0)
530 /* X % Y is smaller than Y. */
533 (cmp (trunc_mod @0 @1) @1)
534 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
535 { constant_boolean_node (cmp == LT_EXPR, type); })))
538 (cmp @1 (trunc_mod @0 @1))
539 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
540 { constant_boolean_node (cmp == GT_EXPR, type); })))
544 (bit_ior @0 integer_all_onesp@1)
549 (bit_and @0 integer_zerop@1)
555 (for op (bit_ior bit_xor plus)
557 (op:c (convert? @0) (convert? (bit_not @0)))
558 (convert { build_all_ones_cst (TREE_TYPE (@0)); })))
563 { build_zero_cst (type); })
565 /* Canonicalize X ^ ~0 to ~X. */
567 (bit_xor @0 integer_all_onesp@1)
572 (bit_and @0 integer_all_onesp)
575 /* x & x -> x, x | x -> x */
576 (for bitop (bit_and bit_ior)
581 /* x & C -> x if we know that x & ~C == 0. */
584 (bit_and SSA_NAME@0 INTEGER_CST@1)
585 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
586 && (get_nonzero_bits (@0) & wi::bit_not (@1)) == 0)
590 /* x + (x & 1) -> (x + 1) & ~1 */
592 (plus:c @0 (bit_and:s @0 integer_onep@1))
593 (bit_and (plus @0 @1) (bit_not @1)))
595 /* x & ~(x & y) -> x & ~y */
596 /* x | ~(x | y) -> x | ~y */
597 (for bitop (bit_and bit_ior)
599 (bitop:c @0 (bit_not (bitop:cs @0 @1)))
600 (bitop @0 (bit_not @1))))
602 /* (x | y) & ~x -> y & ~x */
603 /* (x & y) | ~x -> y | ~x */
604 (for bitop (bit_and bit_ior)
605 rbitop (bit_ior bit_and)
607 (bitop:c (rbitop:c @0 @1) (bit_not@2 @0))
610 /* (x & y) ^ (x | y) -> x ^ y */
612 (bit_xor:c (bit_and @0 @1) (bit_ior @0 @1))
615 /* (x ^ y) ^ (x | y) -> x & y */
617 (bit_xor:c (bit_xor @0 @1) (bit_ior @0 @1))
620 /* (x & y) + (x ^ y) -> x | y */
621 /* (x & y) | (x ^ y) -> x | y */
622 /* (x & y) ^ (x ^ y) -> x | y */
623 (for op (plus bit_ior bit_xor)
625 (op:c (bit_and @0 @1) (bit_xor @0 @1))
628 /* (x & y) + (x | y) -> x + y */
630 (plus:c (bit_and @0 @1) (bit_ior @0 @1))
633 /* (x + y) - (x | y) -> x & y */
635 (minus (plus @0 @1) (bit_ior @0 @1))
636 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
637 && !TYPE_SATURATING (type))
640 /* (x + y) - (x & y) -> x | y */
642 (minus (plus @0 @1) (bit_and @0 @1))
643 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
644 && !TYPE_SATURATING (type))
647 /* (x | y) - (x ^ y) -> x & y */
649 (minus (bit_ior @0 @1) (bit_xor @0 @1))
652 /* (x | y) - (x & y) -> x ^ y */
654 (minus (bit_ior @0 @1) (bit_and @0 @1))
657 /* (x | y) & ~(x & y) -> x ^ y */
659 (bit_and:c (bit_ior @0 @1) (bit_not (bit_and @0 @1)))
662 /* (x | y) & (~x ^ y) -> x & y */
664 (bit_and:c (bit_ior:c @0 @1) (bit_xor:c @1 (bit_not @0)))
667 /* ~x & ~y -> ~(x | y)
668 ~x | ~y -> ~(x & y) */
669 (for op (bit_and bit_ior)
670 rop (bit_ior bit_and)
672 (op (convert1? (bit_not @0)) (convert2? (bit_not @1)))
673 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
674 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
675 (bit_not (rop (convert @0) (convert @1))))))
677 /* If we are XORing or adding two BIT_AND_EXPR's, both of which are and'ing
678 with a constant, and the two constants have no bits in common,
679 we should treat this as a BIT_IOR_EXPR since this may produce more
681 (for op (bit_xor plus)
683 (op (convert1? (bit_and@4 @0 INTEGER_CST@1))
684 (convert2? (bit_and@5 @2 INTEGER_CST@3)))
685 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
686 && tree_nop_conversion_p (type, TREE_TYPE (@2))
687 && wi::bit_and (@1, @3) == 0)
688 (bit_ior (convert @4) (convert @5)))))
690 /* (X | Y) ^ X -> Y & ~ X*/
692 (bit_xor:c (convert? (bit_ior:c @0 @1)) (convert? @0))
693 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
694 (convert (bit_and @1 (bit_not @0)))))
696 /* Convert ~X ^ ~Y to X ^ Y. */
698 (bit_xor (convert1? (bit_not @0)) (convert2? (bit_not @1)))
699 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
700 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
701 (bit_xor (convert @0) (convert @1))))
703 /* Convert ~X ^ C to X ^ ~C. */
705 (bit_xor (convert? (bit_not @0)) INTEGER_CST@1)
706 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
707 (bit_xor (convert @0) (bit_not @1))))
709 /* Fold (X & Y) ^ Y and (X ^ Y) & Y as ~X & Y. */
710 (for opo (bit_and bit_xor)
711 opi (bit_xor bit_and)
713 (opo:c (opi:c @0 @1) @1)
714 (bit_and (bit_not @0) @1)))
716 /* Given a bit-wise operation CODE applied to ARG0 and ARG1, see if both
717 operands are another bit-wise operation with a common input. If so,
718 distribute the bit operations to save an operation and possibly two if
719 constants are involved. For example, convert
720 (A | B) & (A | C) into A | (B & C)
721 Further simplification will occur if B and C are constants. */
722 (for op (bit_and bit_ior bit_xor)
723 rop (bit_ior bit_and bit_and)
725 (op (convert? (rop:c @0 @1)) (convert? (rop:c @0 @2)))
726 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
727 && tree_nop_conversion_p (type, TREE_TYPE (@2)))
728 (rop (convert @0) (op (convert @1) (convert @2))))))
730 /* Some simple reassociation for bit operations, also handled in reassoc. */
731 /* (X & Y) & Y -> X & Y
732 (X | Y) | Y -> X | Y */
733 (for op (bit_and bit_ior)
735 (op:c (convert?@2 (op:c @0 @1)) (convert? @1))
737 /* (X ^ Y) ^ Y -> X */
739 (bit_xor:c (convert? (bit_xor:c @0 @1)) (convert? @1))
741 /* (X & Y) & (X & Z) -> (X & Y) & Z
742 (X | Y) | (X | Z) -> (X | Y) | Z */
743 (for op (bit_and bit_ior)
745 (op:c (convert1?@3 (op:c@4 @0 @1)) (convert2?@5 (op:c@6 @0 @2)))
746 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
747 && tree_nop_conversion_p (type, TREE_TYPE (@2)))
748 (if (single_use (@5) && single_use (@6))
750 (if (single_use (@3) && single_use (@4))
751 (op (convert @1) @5))))))
752 /* (X ^ Y) ^ (X ^ Z) -> Y ^ Z */
754 (bit_xor (convert1? (bit_xor:c @0 @1)) (convert2? (bit_xor:c @0 @2)))
755 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
756 && tree_nop_conversion_p (type, TREE_TYPE (@2)))
757 (bit_xor (convert @1) (convert @2))))
766 (abs tree_expr_nonnegative_p@0)
769 /* A few cases of fold-const.c negate_expr_p predicate. */
772 (if ((INTEGRAL_TYPE_P (type)
773 && TYPE_OVERFLOW_WRAPS (type))
774 || (!TYPE_OVERFLOW_SANITIZED (type)
775 && may_negate_without_overflow_p (t)))))
780 (if (!TYPE_OVERFLOW_SANITIZED (type))))
783 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (t)))))
784 /* VECTOR_CST handling of non-wrapping types would recurse in unsupported
788 (if (FLOAT_TYPE_P (TREE_TYPE (type)) || TYPE_OVERFLOW_WRAPS (type))))
790 /* (-A) * (-B) -> A * B */
792 (mult:c (convert1? (negate @0)) (convert2? negate_expr_p@1))
793 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
794 && tree_nop_conversion_p (type, TREE_TYPE (@1)))
795 (mult (convert @0) (convert (negate @1)))))
797 /* -(A + B) -> (-B) - A. */
799 (negate (plus:c @0 negate_expr_p@1))
800 (if (!HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type))
801 && !HONOR_SIGNED_ZEROS (element_mode (type)))
802 (minus (negate @1) @0)))
804 /* A - B -> A + (-B) if B is easily negatable. */
806 (minus @0 negate_expr_p@1)
807 (if (!FIXED_POINT_TYPE_P (type))
808 (plus @0 (negate @1))))
810 /* Try to fold (type) X op CST -> (type) (X op ((type-x) CST))
812 For bitwise binary operations apply operand conversions to the
813 binary operation result instead of to the operands. This allows
814 to combine successive conversions and bitwise binary operations.
815 We combine the above two cases by using a conditional convert. */
816 (for bitop (bit_and bit_ior bit_xor)
818 (bitop (convert @0) (convert? @1))
819 (if (((TREE_CODE (@1) == INTEGER_CST
820 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
821 && int_fits_type_p (@1, TREE_TYPE (@0)))
822 || types_match (@0, @1))
823 /* ??? This transform conflicts with fold-const.c doing
824 Convert (T)(x & c) into (T)x & (T)c, if c is an integer
825 constants (if x has signed type, the sign bit cannot be set
826 in c). This folds extension into the BIT_AND_EXPR.
827 Restrict it to GIMPLE to avoid endless recursions. */
828 && (bitop != BIT_AND_EXPR || GIMPLE)
829 && (/* That's a good idea if the conversion widens the operand, thus
830 after hoisting the conversion the operation will be narrower. */
831 TYPE_PRECISION (TREE_TYPE (@0)) < TYPE_PRECISION (type)
832 /* It's also a good idea if the conversion is to a non-integer
834 || GET_MODE_CLASS (TYPE_MODE (type)) != MODE_INT
835 /* Or if the precision of TO is not the same as the precision
837 || TYPE_PRECISION (type) != GET_MODE_PRECISION (TYPE_MODE (type))))
838 (convert (bitop @0 (convert @1))))))
840 (for bitop (bit_and bit_ior)
841 rbitop (bit_ior bit_and)
842 /* (x | y) & x -> x */
843 /* (x & y) | x -> x */
845 (bitop:c (rbitop:c @0 @1) @0)
847 /* (~x | y) & x -> x & y */
848 /* (~x & y) | x -> x | y */
850 (bitop:c (rbitop:c (bit_not @0) @1) @0)
853 /* (x | CST1) & CST2 -> (x & CST2) | (CST1 & CST2) */
855 (bit_and (bit_ior @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
856 (bit_ior (bit_and @0 @2) (bit_and @1 @2)))
858 /* Combine successive equal operations with constants. */
859 (for bitop (bit_and bit_ior bit_xor)
861 (bitop (bitop @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
862 (bitop @0 (bitop @1 @2))))
864 /* Try simple folding for X op !X, and X op X with the help
865 of the truth_valued_p and logical_inverted_value predicates. */
866 (match truth_valued_p
868 (if (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1)))
869 (for op (tcc_comparison truth_and truth_andif truth_or truth_orif truth_xor)
870 (match truth_valued_p
872 (match truth_valued_p
875 (match (logical_inverted_value @0)
877 (match (logical_inverted_value @0)
878 (bit_not truth_valued_p@0))
879 (match (logical_inverted_value @0)
880 (eq @0 integer_zerop))
881 (match (logical_inverted_value @0)
882 (ne truth_valued_p@0 integer_truep))
883 (match (logical_inverted_value @0)
884 (bit_xor truth_valued_p@0 integer_truep))
888 (bit_and:c @0 (logical_inverted_value @0))
889 { build_zero_cst (type); })
890 /* X | !X and X ^ !X -> 1, , if X is truth-valued. */
891 (for op (bit_ior bit_xor)
893 (op:c truth_valued_p@0 (logical_inverted_value @0))
894 { constant_boolean_node (true, type); }))
895 /* X ==/!= !X is false/true. */
898 (op:c truth_valued_p@0 (logical_inverted_value @0))
899 { constant_boolean_node (op == NE_EXPR ? true : false, type); }))
901 /* If arg1 and arg2 are booleans (or any single bit type)
902 then try to simplify:
909 But only do this if our result feeds into a comparison as
910 this transformation is not always a win, particularly on
911 targets with and-not instructions.
912 -> simplify_bitwise_binary_boolean */
914 (ne (bit_and:c (bit_not @0) @1) integer_zerop)
915 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
916 && TYPE_PRECISION (TREE_TYPE (@1)) == 1)
917 (if (TYPE_UNSIGNED (TREE_TYPE (@1)))
921 (ne (bit_ior:c (bit_not @0) @1) integer_zerop)
922 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
923 && TYPE_PRECISION (TREE_TYPE (@1)) == 1)
924 (if (TYPE_UNSIGNED (TREE_TYPE (@1)))
930 (bit_not (bit_not @0))
933 /* Convert ~ (-A) to A - 1. */
935 (bit_not (convert? (negate @0)))
936 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
937 || !TYPE_UNSIGNED (TREE_TYPE (@0)))
938 (convert (minus @0 { build_each_one_cst (TREE_TYPE (@0)); }))))
940 /* Convert ~ (A - 1) or ~ (A + -1) to -A. */
942 (bit_not (convert? (minus @0 integer_each_onep)))
943 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
944 || !TYPE_UNSIGNED (TREE_TYPE (@0)))
945 (convert (negate @0))))
947 (bit_not (convert? (plus @0 integer_all_onesp)))
948 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
949 || !TYPE_UNSIGNED (TREE_TYPE (@0)))
950 (convert (negate @0))))
952 /* Part of convert ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify. */
954 (bit_not (convert? (bit_xor @0 INTEGER_CST@1)))
955 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
956 (convert (bit_xor @0 (bit_not @1)))))
958 (bit_not (convert? (bit_xor:c (bit_not @0) @1)))
959 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
960 (convert (bit_xor @0 @1))))
962 /* (x & ~m) | (y & m) -> ((x ^ y) & m) ^ x */
964 (bit_ior:c (bit_and:cs @0 (bit_not @2)) (bit_and:cs @1 @2))
965 (bit_xor (bit_and (bit_xor @0 @1) @2) @0))
967 /* Fold A - (A & B) into ~B & A. */
969 (minus (convert? @0) (convert?:s (bit_and:cs @0 @1)))
970 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
971 && tree_nop_conversion_p (type, TREE_TYPE (@1)))
972 (convert (bit_and (bit_not @1) @0))))
976 /* ((X inner_op C0) outer_op C1)
977 With X being a tree where value_range has reasoned certain bits to always be
978 zero throughout its computed value range,
979 inner_op = {|,^}, outer_op = {|,^} and inner_op != outer_op
980 where zero_mask has 1's for all bits that are sure to be 0 in
982 if (inner_op == '^') C0 &= ~C1;
983 if ((C0 & ~zero_mask) == 0) then emit (X outer_op (C0 outer_op C1)
984 if ((C1 & ~zero_mask) == 0) then emit (X inner_op (C0 outer_op C1)
986 (for inner_op (bit_ior bit_xor)
987 outer_op (bit_xor bit_ior)
990 (inner_op:s @2 INTEGER_CST@0) INTEGER_CST@1)
994 wide_int zero_mask_not;
998 if (TREE_CODE (@2) == SSA_NAME)
999 zero_mask_not = get_nonzero_bits (@2);
1003 if (inner_op == BIT_XOR_EXPR)
1005 C0 = wi::bit_and_not (@0, @1);
1006 cst_emit = wi::bit_or (C0, @1);
1011 cst_emit = wi::bit_xor (@0, @1);
1014 (if (!fail && wi::bit_and (C0, zero_mask_not) == 0)
1015 (outer_op @2 { wide_int_to_tree (type, cst_emit); })
1016 (if (!fail && wi::bit_and (@1, zero_mask_not) == 0)
1017 (inner_op @2 { wide_int_to_tree (type, cst_emit); }))))))
1019 /* Associate (p +p off1) +p off2 as (p +p (off1 + off2)). */
1021 (pointer_plus (pointer_plus:s @0 @1) @3)
1022 (pointer_plus @0 (plus @1 @3)))
1028 tem4 = (unsigned long) tem3;
1033 (pointer_plus @0 (convert?@2 (minus@3 (convert @1) (convert @0))))
1034 /* Conditionally look through a sign-changing conversion. */
1035 (if (TYPE_PRECISION (TREE_TYPE (@2)) == TYPE_PRECISION (TREE_TYPE (@3))
1036 && ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@1)))
1037 || (GENERIC && type == TREE_TYPE (@1))))
1041 tem = (sizetype) ptr;
1045 and produce the simpler and easier to analyze with respect to alignment
1046 ... = ptr & ~algn; */
1048 (pointer_plus @0 (negate (bit_and (convert @0) INTEGER_CST@1)))
1049 (with { tree algn = wide_int_to_tree (TREE_TYPE (@0), wi::bit_not (@1)); }
1050 (bit_and @0 { algn; })))
1052 /* Try folding difference of addresses. */
1054 (minus (convert ADDR_EXPR@0) (convert @1))
1055 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1056 (with { HOST_WIDE_INT diff; }
1057 (if (ptr_difference_const (@0, @1, &diff))
1058 { build_int_cst_type (type, diff); }))))
1060 (minus (convert @0) (convert ADDR_EXPR@1))
1061 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1062 (with { HOST_WIDE_INT diff; }
1063 (if (ptr_difference_const (@0, @1, &diff))
1064 { build_int_cst_type (type, diff); }))))
1066 /* If arg0 is derived from the address of an object or function, we may
1067 be able to fold this expression using the object or function's
1070 (bit_and (convert? @0) INTEGER_CST@1)
1071 (if (POINTER_TYPE_P (TREE_TYPE (@0))
1072 && tree_nop_conversion_p (type, TREE_TYPE (@0)))
1076 unsigned HOST_WIDE_INT bitpos;
1077 get_pointer_alignment_1 (@0, &align, &bitpos);
1079 (if (wi::ltu_p (@1, align / BITS_PER_UNIT))
1080 { wide_int_to_tree (type, wi::bit_and (@1, bitpos / BITS_PER_UNIT)); }))))
1083 /* We can't reassociate at all for saturating types. */
1084 (if (!TYPE_SATURATING (type))
1086 /* Contract negates. */
1087 /* A + (-B) -> A - B */
1089 (plus:c (convert1? @0) (convert2? (negate @1)))
1090 /* Apply STRIP_NOPS on @0 and the negate. */
1091 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
1092 && tree_nop_conversion_p (type, TREE_TYPE (@1))
1093 && !TYPE_OVERFLOW_SANITIZED (type))
1094 (minus (convert @0) (convert @1))))
1095 /* A - (-B) -> A + B */
1097 (minus (convert1? @0) (convert2? (negate @1)))
1098 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
1099 && tree_nop_conversion_p (type, TREE_TYPE (@1))
1100 && !TYPE_OVERFLOW_SANITIZED (type))
1101 (plus (convert @0) (convert @1))))
1104 (negate (convert? (negate @1)))
1105 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1106 && !TYPE_OVERFLOW_SANITIZED (type))
1109 /* We can't reassociate floating-point unless -fassociative-math
1110 or fixed-point plus or minus because of saturation to +-Inf. */
1111 (if ((!FLOAT_TYPE_P (type) || flag_associative_math)
1112 && !FIXED_POINT_TYPE_P (type))
1114 /* Match patterns that allow contracting a plus-minus pair
1115 irrespective of overflow issues. */
1116 /* (A +- B) - A -> +- B */
1117 /* (A +- B) -+ B -> A */
1118 /* A - (A +- B) -> -+ B */
1119 /* A +- (B -+ A) -> +- B */
1121 (minus (plus:c @0 @1) @0)
1124 (minus (minus @0 @1) @0)
1127 (plus:c (minus @0 @1) @1)
1130 (minus @0 (plus:c @0 @1))
1133 (minus @0 (minus @0 @1))
1136 /* (A +- CST) +- CST -> A + CST */
1137 (for outer_op (plus minus)
1138 (for inner_op (plus minus)
1140 (outer_op (inner_op @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
1141 /* If the constant operation overflows we cannot do the transform
1142 as we would introduce undefined overflow, for example
1143 with (a - 1) + INT_MIN. */
1144 (with { tree cst = const_binop (outer_op == inner_op
1145 ? PLUS_EXPR : MINUS_EXPR, type, @1, @2); }
1146 (if (cst && !TREE_OVERFLOW (cst))
1147 (inner_op @0 { cst; } ))))))
1149 /* (CST - A) +- CST -> CST - A */
1150 (for outer_op (plus minus)
1152 (outer_op (minus CONSTANT_CLASS_P@1 @0) CONSTANT_CLASS_P@2)
1153 (with { tree cst = const_binop (outer_op, type, @1, @2); }
1154 (if (cst && !TREE_OVERFLOW (cst))
1155 (minus { cst; } @0)))))
1159 (plus:c (bit_not @0) @0)
1160 (if (!TYPE_OVERFLOW_TRAPS (type))
1161 { build_all_ones_cst (type); }))
1165 (plus (convert? (bit_not @0)) integer_each_onep)
1166 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1167 (negate (convert @0))))
1171 (minus (convert? (negate @0)) integer_each_onep)
1172 (if (!TYPE_OVERFLOW_TRAPS (type)
1173 && tree_nop_conversion_p (type, TREE_TYPE (@0)))
1174 (bit_not (convert @0))))
1178 (minus integer_all_onesp @0)
1181 /* (T)(P + A) - (T)P -> (T) A */
1182 (for add (plus pointer_plus)
1184 (minus (convert (add @0 @1))
1186 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
1187 /* For integer types, if A has a smaller type
1188 than T the result depends on the possible
1190 E.g. T=size_t, A=(unsigned)429497295, P>0.
1191 However, if an overflow in P + A would cause
1192 undefined behavior, we can assume that there
1194 || (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1195 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1196 /* For pointer types, if the conversion of A to the
1197 final type requires a sign- or zero-extension,
1198 then we have to punt - it is not defined which
1200 || (POINTER_TYPE_P (TREE_TYPE (@0))
1201 && TREE_CODE (@1) == INTEGER_CST
1202 && tree_int_cst_sign_bit (@1) == 0))
1205 /* (T)P - (T)(P + A) -> -(T) A */
1206 (for add (plus pointer_plus)
1209 (convert (add @0 @1)))
1210 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
1211 /* For integer types, if A has a smaller type
1212 than T the result depends on the possible
1214 E.g. T=size_t, A=(unsigned)429497295, P>0.
1215 However, if an overflow in P + A would cause
1216 undefined behavior, we can assume that there
1218 || (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1219 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1220 /* For pointer types, if the conversion of A to the
1221 final type requires a sign- or zero-extension,
1222 then we have to punt - it is not defined which
1224 || (POINTER_TYPE_P (TREE_TYPE (@0))
1225 && TREE_CODE (@1) == INTEGER_CST
1226 && tree_int_cst_sign_bit (@1) == 0))
1227 (negate (convert @1)))))
1229 /* (T)(P + A) - (T)(P + B) -> (T)A - (T)B */
1230 (for add (plus pointer_plus)
1232 (minus (convert (add @0 @1))
1233 (convert (add @0 @2)))
1234 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
1235 /* For integer types, if A has a smaller type
1236 than T the result depends on the possible
1238 E.g. T=size_t, A=(unsigned)429497295, P>0.
1239 However, if an overflow in P + A would cause
1240 undefined behavior, we can assume that there
1242 || (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1243 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1244 /* For pointer types, if the conversion of A to the
1245 final type requires a sign- or zero-extension,
1246 then we have to punt - it is not defined which
1248 || (POINTER_TYPE_P (TREE_TYPE (@0))
1249 && TREE_CODE (@1) == INTEGER_CST
1250 && tree_int_cst_sign_bit (@1) == 0
1251 && TREE_CODE (@2) == INTEGER_CST
1252 && tree_int_cst_sign_bit (@2) == 0))
1253 (minus (convert @1) (convert @2)))))))
1256 /* Simplifications of MIN_EXPR, MAX_EXPR, fmin() and fmax(). */
1258 (for minmax (min max FMIN FMAX)
1262 /* min(max(x,y),y) -> y. */
1264 (min:c (max:c @0 @1) @1)
1266 /* max(min(x,y),y) -> y. */
1268 (max:c (min:c @0 @1) @1)
1273 (if (INTEGRAL_TYPE_P (type)
1274 && TYPE_MIN_VALUE (type)
1275 && operand_equal_p (@1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
1277 (if (INTEGRAL_TYPE_P (type)
1278 && TYPE_MAX_VALUE (type)
1279 && operand_equal_p (@1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
1284 (if (INTEGRAL_TYPE_P (type)
1285 && TYPE_MAX_VALUE (type)
1286 && operand_equal_p (@1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
1288 (if (INTEGRAL_TYPE_P (type)
1289 && TYPE_MIN_VALUE (type)
1290 && operand_equal_p (@1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
1292 (for minmax (FMIN FMAX)
1293 /* If either argument is NaN, return the other one. Avoid the
1294 transformation if we get (and honor) a signalling NaN. */
1296 (minmax:c @0 REAL_CST@1)
1297 (if (real_isnan (TREE_REAL_CST_PTR (@1))
1298 && (!HONOR_SNANS (@1) || !TREE_REAL_CST (@1).signalling))
1300 /* Convert fmin/fmax to MIN_EXPR/MAX_EXPR. C99 requires these
1301 functions to return the numeric arg if the other one is NaN.
1302 MIN and MAX don't honor that, so only transform if -ffinite-math-only
1303 is set. C99 doesn't require -0.0 to be handled, so we don't have to
1304 worry about it either. */
1305 (if (flag_finite_math_only)
1312 /* min (-A, -B) -> -max (A, B) */
1313 (for minmax (min max FMIN FMAX)
1314 maxmin (max min FMAX FMIN)
1316 (minmax (negate:s@2 @0) (negate:s@3 @1))
1317 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
1318 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1319 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
1320 (negate (maxmin @0 @1)))))
1321 /* MIN (~X, ~Y) -> ~MAX (X, Y)
1322 MAX (~X, ~Y) -> ~MIN (X, Y) */
1323 (for minmax (min max)
1326 (minmax (bit_not:s@2 @0) (bit_not:s@3 @1))
1327 (bit_not (maxmin @0 @1))))
1329 /* MIN (X, Y) == X -> X <= Y */
1330 (for minmax (min min max max)
1334 (cmp:c (minmax:c @0 @1) @0)
1335 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0)))
1337 /* MIN (X, 5) == 0 -> X == 0
1338 MIN (X, 5) == 7 -> false */
1341 (cmp (min @0 INTEGER_CST@1) INTEGER_CST@2)
1342 (if (wi::lt_p (@1, @2, TYPE_SIGN (TREE_TYPE (@0))))
1343 { constant_boolean_node (cmp == NE_EXPR, type); }
1344 (if (wi::gt_p (@1, @2, TYPE_SIGN (TREE_TYPE (@0))))
1348 (cmp (max @0 INTEGER_CST@1) INTEGER_CST@2)
1349 (if (wi::gt_p (@1, @2, TYPE_SIGN (TREE_TYPE (@0))))
1350 { constant_boolean_node (cmp == NE_EXPR, type); }
1351 (if (wi::lt_p (@1, @2, TYPE_SIGN (TREE_TYPE (@0))))
1353 /* MIN (X, C1) < C2 -> X < C2 || C1 < C2 */
1354 (for minmax (min min max max min min max max )
1355 cmp (lt le gt ge gt ge lt le )
1356 comb (bit_ior bit_ior bit_ior bit_ior bit_and bit_and bit_and bit_and)
1358 (cmp (minmax @0 INTEGER_CST@1) INTEGER_CST@2)
1359 (comb (cmp @0 @2) (cmp @1 @2))))
1361 /* Simplifications of shift and rotates. */
1363 (for rotate (lrotate rrotate)
1365 (rotate integer_all_onesp@0 @1)
1368 /* Optimize -1 >> x for arithmetic right shifts. */
1370 (rshift integer_all_onesp@0 @1)
1371 (if (!TYPE_UNSIGNED (type)
1372 && tree_expr_nonnegative_p (@1))
1375 /* Optimize (x >> c) << c into x & (-1<<c). */
1377 (lshift (rshift @0 INTEGER_CST@1) @1)
1378 (if (wi::ltu_p (@1, element_precision (type)))
1379 (bit_and @0 (lshift { build_minus_one_cst (type); } @1))))
1381 /* Optimize (x << c) >> c into x & ((unsigned)-1 >> c) for unsigned
1384 (rshift (lshift @0 INTEGER_CST@1) @1)
1385 (if (TYPE_UNSIGNED (type)
1386 && (wi::ltu_p (@1, element_precision (type))))
1387 (bit_and @0 (rshift { build_minus_one_cst (type); } @1))))
1389 (for shiftrotate (lrotate rrotate lshift rshift)
1391 (shiftrotate @0 integer_zerop)
1394 (shiftrotate integer_zerop@0 @1)
1396 /* Prefer vector1 << scalar to vector1 << vector2
1397 if vector2 is uniform. */
1398 (for vec (VECTOR_CST CONSTRUCTOR)
1400 (shiftrotate @0 vec@1)
1401 (with { tree tem = uniform_vector_p (@1); }
1403 (shiftrotate @0 { tem; }))))))
1405 /* Rewrite an LROTATE_EXPR by a constant into an
1406 RROTATE_EXPR by a new constant. */
1408 (lrotate @0 INTEGER_CST@1)
1409 (rrotate @0 { const_binop (MINUS_EXPR, TREE_TYPE (@1),
1410 build_int_cst (TREE_TYPE (@1),
1411 element_precision (type)), @1); }))
1413 /* Turn (a OP c1) OP c2 into a OP (c1+c2). */
1414 (for op (lrotate rrotate rshift lshift)
1416 (op (op @0 INTEGER_CST@1) INTEGER_CST@2)
1417 (with { unsigned int prec = element_precision (type); }
1418 (if (wi::ge_p (@1, 0, TYPE_SIGN (TREE_TYPE (@1)))
1419 && wi::lt_p (@1, prec, TYPE_SIGN (TREE_TYPE (@1)))
1420 && wi::ge_p (@2, 0, TYPE_SIGN (TREE_TYPE (@2)))
1421 && wi::lt_p (@2, prec, TYPE_SIGN (TREE_TYPE (@2))))
1422 (with { unsigned int low = wi::add (@1, @2).to_uhwi (); }
1423 /* Deal with a OP (c1 + c2) being undefined but (a OP c1) OP c2
1424 being well defined. */
1426 (if (op == LROTATE_EXPR || op == RROTATE_EXPR)
1427 (op @0 { build_int_cst (TREE_TYPE (@1), low % prec); })
1428 (if (TYPE_UNSIGNED (type) || op == LSHIFT_EXPR)
1429 { build_zero_cst (type); }
1430 (op @0 { build_int_cst (TREE_TYPE (@1), prec - 1); })))
1431 (op @0 { build_int_cst (TREE_TYPE (@1), low); })))))))
1434 /* ((1 << A) & 1) != 0 -> A == 0
1435 ((1 << A) & 1) == 0 -> A != 0 */
1439 (cmp (bit_and (lshift integer_onep @0) integer_onep) integer_zerop)
1440 (icmp @0 { build_zero_cst (TREE_TYPE (@0)); })))
1442 /* (CST1 << A) == CST2 -> A == ctz (CST2) - ctz (CST1)
1443 (CST1 << A) != CST2 -> A != ctz (CST2) - ctz (CST1)
1447 (cmp (lshift INTEGER_CST@0 @1) INTEGER_CST@2)
1448 (with { int cand = wi::ctz (@2) - wi::ctz (@0); }
1450 || (!integer_zerop (@2)
1451 && wi::ne_p (wi::lshift (@0, cand), @2)))
1452 { constant_boolean_node (cmp == NE_EXPR, type); }
1453 (if (!integer_zerop (@2)
1454 && wi::eq_p (wi::lshift (@0, cand), @2))
1455 (cmp @1 { build_int_cst (TREE_TYPE (@1), cand); }))))))
1457 /* Fold (X << C1) & C2 into (X << C1) & (C2 | ((1 << C1) - 1))
1458 (X >> C1) & C2 into (X >> C1) & (C2 | ~((type) -1 >> C1))
1459 if the new mask might be further optimized. */
1460 (for shift (lshift rshift)
1462 (bit_and (convert?:s@4 (shift:s@5 (convert1?@3 @0) INTEGER_CST@1))
1464 (if (tree_nop_conversion_p (TREE_TYPE (@4), TREE_TYPE (@5))
1465 && TYPE_PRECISION (type) <= HOST_BITS_PER_WIDE_INT
1466 && tree_fits_uhwi_p (@1)
1467 && tree_to_uhwi (@1) > 0
1468 && tree_to_uhwi (@1) < TYPE_PRECISION (type))
1471 unsigned int shiftc = tree_to_uhwi (@1);
1472 unsigned HOST_WIDE_INT mask = TREE_INT_CST_LOW (@2);
1473 unsigned HOST_WIDE_INT newmask, zerobits = 0;
1474 tree shift_type = TREE_TYPE (@3);
1477 if (shift == LSHIFT_EXPR)
1478 zerobits = ((HOST_WIDE_INT_1U << shiftc) - 1);
1479 else if (shift == RSHIFT_EXPR
1480 && (TYPE_PRECISION (shift_type)
1481 == GET_MODE_PRECISION (TYPE_MODE (shift_type))))
1483 prec = TYPE_PRECISION (TREE_TYPE (@3));
1485 /* See if more bits can be proven as zero because of
1488 && TYPE_UNSIGNED (TREE_TYPE (@0)))
1490 tree inner_type = TREE_TYPE (@0);
1491 if ((TYPE_PRECISION (inner_type)
1492 == GET_MODE_PRECISION (TYPE_MODE (inner_type)))
1493 && TYPE_PRECISION (inner_type) < prec)
1495 prec = TYPE_PRECISION (inner_type);
1496 /* See if we can shorten the right shift. */
1498 shift_type = inner_type;
1499 /* Otherwise X >> C1 is all zeros, so we'll optimize
1500 it into (X, 0) later on by making sure zerobits
1504 zerobits = HOST_WIDE_INT_M1U;
1507 zerobits >>= HOST_BITS_PER_WIDE_INT - shiftc;
1508 zerobits <<= prec - shiftc;
1510 /* For arithmetic shift if sign bit could be set, zerobits
1511 can contain actually sign bits, so no transformation is
1512 possible, unless MASK masks them all away. In that
1513 case the shift needs to be converted into logical shift. */
1514 if (!TYPE_UNSIGNED (TREE_TYPE (@3))
1515 && prec == TYPE_PRECISION (TREE_TYPE (@3)))
1517 if ((mask & zerobits) == 0)
1518 shift_type = unsigned_type_for (TREE_TYPE (@3));
1524 /* ((X << 16) & 0xff00) is (X, 0). */
1525 (if ((mask & zerobits) == mask)
1526 { build_int_cst (type, 0); }
1527 (with { newmask = mask | zerobits; }
1528 (if (newmask != mask && (newmask & (newmask + 1)) == 0)
1531 /* Only do the transformation if NEWMASK is some integer
1533 for (prec = BITS_PER_UNIT;
1534 prec < HOST_BITS_PER_WIDE_INT; prec <<= 1)
1535 if (newmask == (HOST_WIDE_INT_1U << prec) - 1)
1538 (if (prec < HOST_BITS_PER_WIDE_INT
1539 || newmask == HOST_WIDE_INT_M1U)
1541 { tree newmaskt = build_int_cst_type (TREE_TYPE (@2), newmask); }
1542 (if (!tree_int_cst_equal (newmaskt, @2))
1543 (if (shift_type != TREE_TYPE (@3))
1544 (bit_and (convert (shift:shift_type (convert @3) @1)) { newmaskt; })
1545 (bit_and @4 { newmaskt; })))))))))))))
1547 /* Fold (X {&,^,|} C2) << C1 into (X << C1) {&,^,|} (C2 << C1)
1548 (X {&,^,|} C2) >> C1 into (X >> C1) & (C2 >> C1). */
1549 (for shift (lshift rshift)
1550 (for bit_op (bit_and bit_xor bit_ior)
1552 (shift (convert?:s (bit_op:s @0 INTEGER_CST@2)) INTEGER_CST@1)
1553 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1554 (with { tree mask = int_const_binop (shift, fold_convert (type, @2), @1); }
1555 (bit_op (shift (convert @0) @1) { mask; }))))))
1557 /* ~(~X >> Y) -> X >> Y (for arithmetic shift). */
1559 (bit_not (convert1?:s (rshift:s (convert2?@0 (bit_not @1)) @2)))
1560 (if (!TYPE_UNSIGNED (TREE_TYPE (@0))
1561 && (element_precision (TREE_TYPE (@0))
1562 <= element_precision (TREE_TYPE (@1))
1563 || !TYPE_UNSIGNED (TREE_TYPE (@1))))
1565 { tree shift_type = TREE_TYPE (@0); }
1566 (convert (rshift (convert:shift_type @1) @2)))))
1568 /* ~(~X >>r Y) -> X >>r Y
1569 ~(~X <<r Y) -> X <<r Y */
1570 (for rotate (lrotate rrotate)
1572 (bit_not (convert1?:s (rotate:s (convert2?@0 (bit_not @1)) @2)))
1573 (if ((element_precision (TREE_TYPE (@0))
1574 <= element_precision (TREE_TYPE (@1))
1575 || !TYPE_UNSIGNED (TREE_TYPE (@1)))
1576 && (element_precision (type) <= element_precision (TREE_TYPE (@0))
1577 || !TYPE_UNSIGNED (TREE_TYPE (@0))))
1579 { tree rotate_type = TREE_TYPE (@0); }
1580 (convert (rotate (convert:rotate_type @1) @2))))))
1582 /* Simplifications of conversions. */
1584 /* Basic strip-useless-type-conversions / strip_nops. */
1585 (for cvt (convert view_convert float fix_trunc)
1588 (if ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@0)))
1589 || (GENERIC && type == TREE_TYPE (@0)))
1592 /* Contract view-conversions. */
1594 (view_convert (view_convert @0))
1597 /* For integral conversions with the same precision or pointer
1598 conversions use a NOP_EXPR instead. */
1601 (if ((INTEGRAL_TYPE_P (type) || POINTER_TYPE_P (type))
1602 && (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
1603 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (@0)))
1606 /* Strip inner integral conversions that do not change precision or size. */
1608 (view_convert (convert@0 @1))
1609 (if ((INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
1610 && (INTEGRAL_TYPE_P (TREE_TYPE (@1)) || POINTER_TYPE_P (TREE_TYPE (@1)))
1611 && (TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
1612 && (TYPE_SIZE (TREE_TYPE (@0)) == TYPE_SIZE (TREE_TYPE (@1))))
1615 /* Re-association barriers around constants and other re-association
1616 barriers can be removed. */
1618 (paren CONSTANT_CLASS_P@0)
1621 (paren (paren@1 @0))
1624 /* Handle cases of two conversions in a row. */
1625 (for ocvt (convert float fix_trunc)
1626 (for icvt (convert float)
1631 tree inside_type = TREE_TYPE (@0);
1632 tree inter_type = TREE_TYPE (@1);
1633 int inside_int = INTEGRAL_TYPE_P (inside_type);
1634 int inside_ptr = POINTER_TYPE_P (inside_type);
1635 int inside_float = FLOAT_TYPE_P (inside_type);
1636 int inside_vec = VECTOR_TYPE_P (inside_type);
1637 unsigned int inside_prec = TYPE_PRECISION (inside_type);
1638 int inside_unsignedp = TYPE_UNSIGNED (inside_type);
1639 int inter_int = INTEGRAL_TYPE_P (inter_type);
1640 int inter_ptr = POINTER_TYPE_P (inter_type);
1641 int inter_float = FLOAT_TYPE_P (inter_type);
1642 int inter_vec = VECTOR_TYPE_P (inter_type);
1643 unsigned int inter_prec = TYPE_PRECISION (inter_type);
1644 int inter_unsignedp = TYPE_UNSIGNED (inter_type);
1645 int final_int = INTEGRAL_TYPE_P (type);
1646 int final_ptr = POINTER_TYPE_P (type);
1647 int final_float = FLOAT_TYPE_P (type);
1648 int final_vec = VECTOR_TYPE_P (type);
1649 unsigned int final_prec = TYPE_PRECISION (type);
1650 int final_unsignedp = TYPE_UNSIGNED (type);
1653 /* In addition to the cases of two conversions in a row
1654 handled below, if we are converting something to its own
1655 type via an object of identical or wider precision, neither
1656 conversion is needed. */
1657 (if (((GIMPLE && useless_type_conversion_p (type, inside_type))
1659 && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (inside_type)))
1660 && (((inter_int || inter_ptr) && final_int)
1661 || (inter_float && final_float))
1662 && inter_prec >= final_prec)
1665 /* Likewise, if the intermediate and initial types are either both
1666 float or both integer, we don't need the middle conversion if the
1667 former is wider than the latter and doesn't change the signedness
1668 (for integers). Avoid this if the final type is a pointer since
1669 then we sometimes need the middle conversion. */
1670 (if (((inter_int && inside_int) || (inter_float && inside_float))
1671 && (final_int || final_float)
1672 && inter_prec >= inside_prec
1673 && (inter_float || inter_unsignedp == inside_unsignedp))
1676 /* If we have a sign-extension of a zero-extended value, we can
1677 replace that by a single zero-extension. Likewise if the
1678 final conversion does not change precision we can drop the
1679 intermediate conversion. */
1680 (if (inside_int && inter_int && final_int
1681 && ((inside_prec < inter_prec && inter_prec < final_prec
1682 && inside_unsignedp && !inter_unsignedp)
1683 || final_prec == inter_prec))
1686 /* Two conversions in a row are not needed unless:
1687 - some conversion is floating-point (overstrict for now), or
1688 - some conversion is a vector (overstrict for now), or
1689 - the intermediate type is narrower than both initial and
1691 - the intermediate type and innermost type differ in signedness,
1692 and the outermost type is wider than the intermediate, or
1693 - the initial type is a pointer type and the precisions of the
1694 intermediate and final types differ, or
1695 - the final type is a pointer type and the precisions of the
1696 initial and intermediate types differ. */
1697 (if (! inside_float && ! inter_float && ! final_float
1698 && ! inside_vec && ! inter_vec && ! final_vec
1699 && (inter_prec >= inside_prec || inter_prec >= final_prec)
1700 && ! (inside_int && inter_int
1701 && inter_unsignedp != inside_unsignedp
1702 && inter_prec < final_prec)
1703 && ((inter_unsignedp && inter_prec > inside_prec)
1704 == (final_unsignedp && final_prec > inter_prec))
1705 && ! (inside_ptr && inter_prec != final_prec)
1706 && ! (final_ptr && inside_prec != inter_prec))
1709 /* A truncation to an unsigned type (a zero-extension) should be
1710 canonicalized as bitwise and of a mask. */
1711 (if (GIMPLE /* PR70366: doing this in GENERIC breaks -Wconversion. */
1712 && final_int && inter_int && inside_int
1713 && final_prec == inside_prec
1714 && final_prec > inter_prec
1716 (convert (bit_and @0 { wide_int_to_tree
1718 wi::mask (inter_prec, false,
1719 TYPE_PRECISION (inside_type))); })))
1721 /* If we are converting an integer to a floating-point that can
1722 represent it exactly and back to an integer, we can skip the
1723 floating-point conversion. */
1724 (if (GIMPLE /* PR66211 */
1725 && inside_int && inter_float && final_int &&
1726 (unsigned) significand_size (TYPE_MODE (inter_type))
1727 >= inside_prec - !inside_unsignedp)
1730 /* If we have a narrowing conversion to an integral type that is fed by a
1731 BIT_AND_EXPR, we might be able to remove the BIT_AND_EXPR if it merely
1732 masks off bits outside the final type (and nothing else). */
1734 (convert (bit_and @0 INTEGER_CST@1))
1735 (if (INTEGRAL_TYPE_P (type)
1736 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
1737 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))
1738 && operand_equal_p (@1, build_low_bits_mask (TREE_TYPE (@1),
1739 TYPE_PRECISION (type)), 0))
1743 /* (X /[ex] A) * A -> X. */
1745 (mult (convert? (exact_div @0 @1)) @1)
1746 /* Look through a sign-changing conversion. */
1749 /* Canonicalization of binary operations. */
1751 /* Convert X + -C into X - C. */
1753 (plus @0 REAL_CST@1)
1754 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
1755 (with { tree tem = const_unop (NEGATE_EXPR, type, @1); }
1756 (if (!TREE_OVERFLOW (tem) || !flag_trapping_math)
1757 (minus @0 { tem; })))))
1759 /* Convert x+x into x*2. */
1762 (if (SCALAR_FLOAT_TYPE_P (type))
1763 (mult @0 { build_real (type, dconst2); })
1764 (if (INTEGRAL_TYPE_P (type))
1765 (mult @0 { build_int_cst (type, 2); }))))
1768 (minus integer_zerop @1)
1771 /* (ARG0 - ARG1) is the same as (-ARG1 + ARG0). So check whether
1772 ARG0 is zero and X + ARG0 reduces to X, since that would mean
1773 (-ARG1 + ARG0) reduces to -ARG1. */
1775 (minus real_zerop@0 @1)
1776 (if (fold_real_zero_addition_p (type, @0, 0))
1779 /* Transform x * -1 into -x. */
1781 (mult @0 integer_minus_onep)
1784 /* True if we can easily extract the real and imaginary parts of a complex
1786 (match compositional_complex
1787 (convert? (complex @0 @1)))
1789 /* COMPLEX_EXPR and REALPART/IMAGPART_EXPR cancellations. */
1791 (complex (realpart @0) (imagpart @0))
1794 (realpart (complex @0 @1))
1797 (imagpart (complex @0 @1))
1800 /* Sometimes we only care about half of a complex expression. */
1802 (realpart (convert?:s (conj:s @0)))
1803 (convert (realpart @0)))
1805 (imagpart (convert?:s (conj:s @0)))
1806 (convert (negate (imagpart @0))))
1807 (for part (realpart imagpart)
1808 (for op (plus minus)
1810 (part (convert?:s@2 (op:s @0 @1)))
1811 (convert (op (part @0) (part @1))))))
1813 (realpart (convert?:s (CEXPI:s @0)))
1816 (imagpart (convert?:s (CEXPI:s @0)))
1819 /* conj(conj(x)) -> x */
1821 (conj (convert? (conj @0)))
1822 (if (tree_nop_conversion_p (TREE_TYPE (@0), type))
1825 /* conj({x,y}) -> {x,-y} */
1827 (conj (convert?:s (complex:s @0 @1)))
1828 (with { tree itype = TREE_TYPE (type); }
1829 (complex (convert:itype @0) (negate (convert:itype @1)))))
1831 /* BSWAP simplifications, transforms checked by gcc.dg/builtin-bswap-8.c. */
1832 (for bswap (BUILT_IN_BSWAP16 BUILT_IN_BSWAP32 BUILT_IN_BSWAP64)
1837 (bswap (bit_not (bswap @0)))
1839 (for bitop (bit_xor bit_ior bit_and)
1841 (bswap (bitop:c (bswap @0) @1))
1842 (bitop @0 (bswap @1)))))
1845 /* Combine COND_EXPRs and VEC_COND_EXPRs. */
1847 /* Simplify constant conditions.
1848 Only optimize constant conditions when the selected branch
1849 has the same type as the COND_EXPR. This avoids optimizing
1850 away "c ? x : throw", where the throw has a void type.
1851 Note that we cannot throw away the fold-const.c variant nor
1852 this one as we depend on doing this transform before possibly
1853 A ? B : B -> B triggers and the fold-const.c one can optimize
1854 0 ? A : B to B even if A has side-effects. Something
1855 genmatch cannot handle. */
1857 (cond INTEGER_CST@0 @1 @2)
1858 (if (integer_zerop (@0))
1859 (if (!VOID_TYPE_P (TREE_TYPE (@2)) || VOID_TYPE_P (type))
1861 (if (!VOID_TYPE_P (TREE_TYPE (@1)) || VOID_TYPE_P (type))
1864 (vec_cond VECTOR_CST@0 @1 @2)
1865 (if (integer_all_onesp (@0))
1867 (if (integer_zerop (@0))
1870 (for cnd (cond vec_cond)
1871 /* A ? B : (A ? X : C) -> A ? B : C. */
1873 (cnd @0 (cnd @0 @1 @2) @3)
1876 (cnd @0 @1 (cnd @0 @2 @3))
1878 /* A ? B : (!A ? C : X) -> A ? B : C. */
1879 /* ??? This matches embedded conditions open-coded because genmatch
1880 would generate matching code for conditions in separate stmts only.
1881 The following is still important to merge then and else arm cases
1882 from if-conversion. */
1884 (cnd @0 @1 (cnd @2 @3 @4))
1885 (if (COMPARISON_CLASS_P (@0)
1886 && COMPARISON_CLASS_P (@2)
1887 && invert_tree_comparison
1888 (TREE_CODE (@0), HONOR_NANS (TREE_OPERAND (@0, 0))) == TREE_CODE (@2)
1889 && operand_equal_p (TREE_OPERAND (@0, 0), TREE_OPERAND (@2, 0), 0)
1890 && operand_equal_p (TREE_OPERAND (@0, 1), TREE_OPERAND (@2, 1), 0))
1893 (cnd @0 (cnd @1 @2 @3) @4)
1894 (if (COMPARISON_CLASS_P (@0)
1895 && COMPARISON_CLASS_P (@1)
1896 && invert_tree_comparison
1897 (TREE_CODE (@0), HONOR_NANS (TREE_OPERAND (@0, 0))) == TREE_CODE (@1)
1898 && operand_equal_p (TREE_OPERAND (@0, 0), TREE_OPERAND (@1, 0), 0)
1899 && operand_equal_p (TREE_OPERAND (@0, 1), TREE_OPERAND (@1, 1), 0))
1902 /* A ? B : B -> B. */
1907 /* !A ? B : C -> A ? C : B. */
1909 (cnd (logical_inverted_value truth_valued_p@0) @1 @2)
1912 /* A + (B vcmp C ? 1 : 0) -> A - (B vcmp C ? -1 : 0), since vector comparisons
1913 return all -1 or all 0 results. */
1914 /* ??? We could instead convert all instances of the vec_cond to negate,
1915 but that isn't necessarily a win on its own. */
1917 (plus:c @3 (view_convert? (vec_cond:s @0 integer_each_onep@1 integer_zerop@2)))
1918 (if (VECTOR_TYPE_P (type)
1919 && TYPE_VECTOR_SUBPARTS (type) == TYPE_VECTOR_SUBPARTS (TREE_TYPE (@1))
1920 && (TYPE_MODE (TREE_TYPE (type))
1921 == TYPE_MODE (TREE_TYPE (TREE_TYPE (@1)))))
1922 (minus @3 (view_convert (vec_cond @0 (negate @1) @2)))))
1924 /* ... likewise A - (B vcmp C ? 1 : 0) -> A + (B vcmp C ? -1 : 0). */
1926 (minus @3 (view_convert? (vec_cond:s @0 integer_each_onep@1 integer_zerop@2)))
1927 (if (VECTOR_TYPE_P (type)
1928 && TYPE_VECTOR_SUBPARTS (type) == TYPE_VECTOR_SUBPARTS (TREE_TYPE (@1))
1929 && (TYPE_MODE (TREE_TYPE (type))
1930 == TYPE_MODE (TREE_TYPE (TREE_TYPE (@1)))))
1931 (plus @3 (view_convert (vec_cond @0 (negate @1) @2)))))
1934 /* Simplifications of comparisons. */
1936 /* See if we can reduce the magnitude of a constant involved in a
1937 comparison by changing the comparison code. This is a canonicalization
1938 formerly done by maybe_canonicalize_comparison_1. */
1942 (cmp @0 INTEGER_CST@1)
1943 (if (tree_int_cst_sgn (@1) == -1)
1944 (acmp @0 { wide_int_to_tree (TREE_TYPE (@1), wi::add (@1, 1)); }))))
1948 (cmp @0 INTEGER_CST@1)
1949 (if (tree_int_cst_sgn (@1) == 1)
1950 (acmp @0 { wide_int_to_tree (TREE_TYPE (@1), wi::sub (@1, 1)); }))))
1953 /* We can simplify a logical negation of a comparison to the
1954 inverted comparison. As we cannot compute an expression
1955 operator using invert_tree_comparison we have to simulate
1956 that with expression code iteration. */
1957 (for cmp (tcc_comparison)
1958 icmp (inverted_tcc_comparison)
1959 ncmp (inverted_tcc_comparison_with_nans)
1960 /* Ideally we'd like to combine the following two patterns
1961 and handle some more cases by using
1962 (logical_inverted_value (cmp @0 @1))
1963 here but for that genmatch would need to "inline" that.
1964 For now implement what forward_propagate_comparison did. */
1966 (bit_not (cmp @0 @1))
1967 (if (VECTOR_TYPE_P (type)
1968 || (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1))
1969 /* Comparison inversion may be impossible for trapping math,
1970 invert_tree_comparison will tell us. But we can't use
1971 a computed operator in the replacement tree thus we have
1972 to play the trick below. */
1973 (with { enum tree_code ic = invert_tree_comparison
1974 (cmp, HONOR_NANS (@0)); }
1980 (bit_xor (cmp @0 @1) integer_truep)
1981 (with { enum tree_code ic = invert_tree_comparison
1982 (cmp, HONOR_NANS (@0)); }
1988 /* Transform comparisons of the form X - Y CMP 0 to X CMP Y.
1989 ??? The transformation is valid for the other operators if overflow
1990 is undefined for the type, but performing it here badly interacts
1991 with the transformation in fold_cond_expr_with_comparison which
1992 attempts to synthetize ABS_EXPR. */
1995 (cmp (minus@2 @0 @1) integer_zerop)
1996 (if (single_use (@2))
1999 /* Transform comparisons of the form X * C1 CMP 0 to X CMP 0 in the
2000 signed arithmetic case. That form is created by the compiler
2001 often enough for folding it to be of value. One example is in
2002 computing loop trip counts after Operator Strength Reduction. */
2003 (for cmp (simple_comparison)
2004 scmp (swapped_simple_comparison)
2006 (cmp (mult@3 @0 INTEGER_CST@1) integer_zerop@2)
2007 /* Handle unfolded multiplication by zero. */
2008 (if (integer_zerop (@1))
2010 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2011 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2013 /* If @1 is negative we swap the sense of the comparison. */
2014 (if (tree_int_cst_sgn (@1) < 0)
2018 /* Simplify comparison of something with itself. For IEEE
2019 floating-point, we can only do some of these simplifications. */
2023 (if (! FLOAT_TYPE_P (TREE_TYPE (@0))
2024 || ! HONOR_NANS (@0))
2025 { constant_boolean_node (true, type); }
2026 (if (cmp != EQ_EXPR)
2032 || ! FLOAT_TYPE_P (TREE_TYPE (@0))
2033 || ! HONOR_NANS (@0))
2034 { constant_boolean_node (false, type); })))
2035 (for cmp (unle unge uneq)
2038 { constant_boolean_node (true, type); }))
2039 (for cmp (unlt ungt)
2045 (if (!flag_trapping_math)
2046 { constant_boolean_node (false, type); }))
2048 /* Fold ~X op ~Y as Y op X. */
2049 (for cmp (simple_comparison)
2051 (cmp (bit_not@2 @0) (bit_not@3 @1))
2052 (if (single_use (@2) && single_use (@3))
2055 /* Fold ~X op C as X op' ~C, where op' is the swapped comparison. */
2056 (for cmp (simple_comparison)
2057 scmp (swapped_simple_comparison)
2059 (cmp (bit_not@2 @0) CONSTANT_CLASS_P@1)
2060 (if (single_use (@2)
2061 && (TREE_CODE (@1) == INTEGER_CST || TREE_CODE (@1) == VECTOR_CST))
2062 (scmp @0 (bit_not @1)))))
2064 (for cmp (simple_comparison)
2065 /* Fold (double)float1 CMP (double)float2 into float1 CMP float2. */
2067 (cmp (convert@2 @0) (convert? @1))
2068 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
2069 && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@2))
2070 == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0)))
2071 && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@2))
2072 == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1))))
2075 tree type1 = TREE_TYPE (@1);
2076 if (TREE_CODE (@1) == REAL_CST && !DECIMAL_FLOAT_TYPE_P (type1))
2078 REAL_VALUE_TYPE orig = TREE_REAL_CST (@1);
2079 if (TYPE_PRECISION (type1) > TYPE_PRECISION (float_type_node)
2080 && exact_real_truncate (TYPE_MODE (float_type_node), &orig))
2081 type1 = float_type_node;
2082 if (TYPE_PRECISION (type1) > TYPE_PRECISION (double_type_node)
2083 && exact_real_truncate (TYPE_MODE (double_type_node), &orig))
2084 type1 = double_type_node;
2087 = (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type1)
2088 ? TREE_TYPE (@0) : type1);
2090 (if (TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (newtype))
2091 (cmp (convert:newtype @0) (convert:newtype @1))))))
2095 /* IEEE doesn't distinguish +0 and -0 in comparisons. */
2097 /* a CMP (-0) -> a CMP 0 */
2098 (if (REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@1)))
2099 (cmp @0 { build_real (TREE_TYPE (@1), dconst0); }))
2100 /* x != NaN is always true, other ops are always false. */
2101 (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
2102 && ! HONOR_SNANS (@1))
2103 { constant_boolean_node (cmp == NE_EXPR, type); })
2104 /* Fold comparisons against infinity. */
2105 (if (REAL_VALUE_ISINF (TREE_REAL_CST (@1))
2106 && MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (@1))))
2109 REAL_VALUE_TYPE max;
2110 enum tree_code code = cmp;
2111 bool neg = REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1));
2113 code = swap_tree_comparison (code);
2116 /* x > +Inf is always false, if with ignore sNANs. */
2117 (if (code == GT_EXPR
2118 && ! HONOR_SNANS (@0))
2119 { constant_boolean_node (false, type); })
2120 (if (code == LE_EXPR)
2121 /* x <= +Inf is always true, if we don't case about NaNs. */
2122 (if (! HONOR_NANS (@0))
2123 { constant_boolean_node (true, type); }
2124 /* x <= +Inf is the same as x == x, i.e. !isnan(x). */
2126 /* x == +Inf and x >= +Inf are always equal to x > DBL_MAX. */
2127 (if (code == EQ_EXPR || code == GE_EXPR)
2128 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
2130 (lt @0 { build_real (TREE_TYPE (@0), max); })
2131 (gt @0 { build_real (TREE_TYPE (@0), max); }))))
2132 /* x < +Inf is always equal to x <= DBL_MAX. */
2133 (if (code == LT_EXPR)
2134 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
2136 (ge @0 { build_real (TREE_TYPE (@0), max); })
2137 (le @0 { build_real (TREE_TYPE (@0), max); }))))
2138 /* x != +Inf is always equal to !(x > DBL_MAX). */
2139 (if (code == NE_EXPR)
2140 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
2141 (if (! HONOR_NANS (@0))
2143 (ge @0 { build_real (TREE_TYPE (@0), max); })
2144 (le @0 { build_real (TREE_TYPE (@0), max); }))
2146 (bit_xor (lt @0 { build_real (TREE_TYPE (@0), max); })
2147 { build_one_cst (type); })
2148 (bit_xor (gt @0 { build_real (TREE_TYPE (@0), max); })
2149 { build_one_cst (type); }))))))))))
2151 /* If this is a comparison of a real constant with a PLUS_EXPR
2152 or a MINUS_EXPR of a real constant, we can convert it into a
2153 comparison with a revised real constant as long as no overflow
2154 occurs when unsafe_math_optimizations are enabled. */
2155 (if (flag_unsafe_math_optimizations)
2156 (for op (plus minus)
2158 (cmp (op @0 REAL_CST@1) REAL_CST@2)
2161 tree tem = const_binop (op == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR,
2162 TREE_TYPE (@1), @2, @1);
2164 (if (tem && !TREE_OVERFLOW (tem))
2165 (cmp @0 { tem; }))))))
2167 /* Likewise, we can simplify a comparison of a real constant with
2168 a MINUS_EXPR whose first operand is also a real constant, i.e.
2169 (c1 - x) < c2 becomes x > c1-c2. Reordering is allowed on
2170 floating-point types only if -fassociative-math is set. */
2171 (if (flag_associative_math)
2173 (cmp (minus REAL_CST@0 @1) REAL_CST@2)
2174 (with { tree tem = const_binop (MINUS_EXPR, TREE_TYPE (@1), @0, @2); }
2175 (if (tem && !TREE_OVERFLOW (tem))
2176 (cmp { tem; } @1)))))
2178 /* Fold comparisons against built-in math functions. */
2179 (if (flag_unsafe_math_optimizations
2180 && ! flag_errno_math)
2183 (cmp (sq @0) REAL_CST@1)
2185 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
2187 /* sqrt(x) < y is always false, if y is negative. */
2188 (if (cmp == EQ_EXPR || cmp == LT_EXPR || cmp == LE_EXPR)
2189 { constant_boolean_node (false, type); })
2190 /* sqrt(x) > y is always true, if y is negative and we
2191 don't care about NaNs, i.e. negative values of x. */
2192 (if (cmp == NE_EXPR || !HONOR_NANS (@0))
2193 { constant_boolean_node (true, type); })
2194 /* sqrt(x) > y is the same as x >= 0, if y is negative. */
2195 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })))
2196 (if (real_equal (TREE_REAL_CST_PTR (@1), &dconst0))
2198 /* sqrt(x) < 0 is always false. */
2199 (if (cmp == LT_EXPR)
2200 { constant_boolean_node (false, type); })
2201 /* sqrt(x) >= 0 is always true if we don't care about NaNs. */
2202 (if (cmp == GE_EXPR && !HONOR_NANS (@0))
2203 { constant_boolean_node (true, type); })
2204 /* sqrt(x) <= 0 -> x == 0. */
2205 (if (cmp == LE_EXPR)
2207 /* Otherwise sqrt(x) cmp 0 -> x cmp 0. Here cmp can be >=, >,
2208 == or !=. In the last case:
2210 (sqrt(x) != 0) == (NaN != 0) == true == (x != 0)
2212 if x is negative or NaN. Due to -funsafe-math-optimizations,
2213 the results for other x follow from natural arithmetic. */
2215 (if (cmp == GT_EXPR || cmp == GE_EXPR)
2219 real_arithmetic (&c2, MULT_EXPR,
2220 &TREE_REAL_CST (@1), &TREE_REAL_CST (@1));
2221 real_convert (&c2, TYPE_MODE (TREE_TYPE (@0)), &c2);
2223 (if (REAL_VALUE_ISINF (c2))
2224 /* sqrt(x) > y is x == +Inf, when y is very large. */
2225 (if (HONOR_INFINITIES (@0))
2226 (eq @0 { build_real (TREE_TYPE (@0), c2); })
2227 { constant_boolean_node (false, type); })
2228 /* sqrt(x) > c is the same as x > c*c. */
2229 (cmp @0 { build_real (TREE_TYPE (@0), c2); }))))
2230 (if (cmp == LT_EXPR || cmp == LE_EXPR)
2234 real_arithmetic (&c2, MULT_EXPR,
2235 &TREE_REAL_CST (@1), &TREE_REAL_CST (@1));
2236 real_convert (&c2, TYPE_MODE (TREE_TYPE (@0)), &c2);
2238 (if (REAL_VALUE_ISINF (c2))
2240 /* sqrt(x) < y is always true, when y is a very large
2241 value and we don't care about NaNs or Infinities. */
2242 (if (! HONOR_NANS (@0) && ! HONOR_INFINITIES (@0))
2243 { constant_boolean_node (true, type); })
2244 /* sqrt(x) < y is x != +Inf when y is very large and we
2245 don't care about NaNs. */
2246 (if (! HONOR_NANS (@0))
2247 (ne @0 { build_real (TREE_TYPE (@0), c2); }))
2248 /* sqrt(x) < y is x >= 0 when y is very large and we
2249 don't care about Infinities. */
2250 (if (! HONOR_INFINITIES (@0))
2251 (ge @0 { build_real (TREE_TYPE (@0), dconst0); }))
2252 /* sqrt(x) < y is x >= 0 && x != +Inf, when y is large. */
2255 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
2256 (ne @0 { build_real (TREE_TYPE (@0), c2); }))))
2257 /* sqrt(x) < c is the same as x < c*c, if we ignore NaNs. */
2258 (if (! HONOR_NANS (@0))
2259 (cmp @0 { build_real (TREE_TYPE (@0), c2); })
2260 /* sqrt(x) < c is the same as x >= 0 && x < c*c. */
2263 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
2264 (cmp @0 { build_real (TREE_TYPE (@0), c2); }))))))))))))
2266 /* Unordered tests if either argument is a NaN. */
2268 (bit_ior (unordered @0 @0) (unordered @1 @1))
2269 (if (types_match (@0, @1))
2272 (bit_and (ordered @0 @0) (ordered @1 @1))
2273 (if (types_match (@0, @1))
2276 (bit_ior:c (unordered @0 @0) (unordered:c@2 @0 @1))
2279 (bit_and:c (ordered @0 @0) (ordered:c@2 @0 @1))
2282 /* Simple range test simplifications. */
2283 /* A < B || A >= B -> true. */
2284 (for test1 (lt le le le ne ge)
2285 test2 (ge gt ge ne eq ne)
2287 (bit_ior:c (test1 @0 @1) (test2 @0 @1))
2288 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2289 || VECTOR_INTEGER_TYPE_P (TREE_TYPE (@0)))
2290 { constant_boolean_node (true, type); })))
2291 /* A < B && A >= B -> false. */
2292 (for test1 (lt lt lt le ne eq)
2293 test2 (ge gt eq gt eq gt)
2295 (bit_and:c (test1 @0 @1) (test2 @0 @1))
2296 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2297 || VECTOR_INTEGER_TYPE_P (TREE_TYPE (@0)))
2298 { constant_boolean_node (false, type); })))
2300 /* -A CMP -B -> B CMP A. */
2301 (for cmp (tcc_comparison)
2302 scmp (swapped_tcc_comparison)
2304 (cmp (negate @0) (negate @1))
2305 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
2306 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2307 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
2310 (cmp (negate @0) CONSTANT_CLASS_P@1)
2311 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
2312 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2313 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
2314 (with { tree tem = const_unop (NEGATE_EXPR, TREE_TYPE (@0), @1); }
2315 (if (tem && !TREE_OVERFLOW (tem))
2316 (scmp @0 { tem; }))))))
2318 /* Convert ABS_EXPR<x> == 0 or ABS_EXPR<x> != 0 to x == 0 or x != 0. */
2321 (op (abs @0) zerop@1)
2324 /* From fold_sign_changed_comparison and fold_widened_comparison. */
2325 (for cmp (simple_comparison)
2327 (cmp (convert@0 @00) (convert?@1 @10))
2328 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2329 /* Disable this optimization if we're casting a function pointer
2330 type on targets that require function pointer canonicalization. */
2331 && !(targetm.have_canonicalize_funcptr_for_compare ()
2332 && TREE_CODE (TREE_TYPE (@00)) == POINTER_TYPE
2333 && TREE_CODE (TREE_TYPE (TREE_TYPE (@00))) == FUNCTION_TYPE)
2335 (if (TYPE_PRECISION (TREE_TYPE (@00)) == TYPE_PRECISION (TREE_TYPE (@0))
2336 && (TREE_CODE (@10) == INTEGER_CST
2337 || (@1 != @10 && types_match (TREE_TYPE (@10), TREE_TYPE (@00))))
2338 && (TYPE_UNSIGNED (TREE_TYPE (@00)) == TYPE_UNSIGNED (TREE_TYPE (@0))
2341 && (POINTER_TYPE_P (TREE_TYPE (@00)) == POINTER_TYPE_P (TREE_TYPE (@0))))
2342 /* ??? The special-casing of INTEGER_CST conversion was in the original
2343 code and here to avoid a spurious overflow flag on the resulting
2344 constant which fold_convert produces. */
2345 (if (TREE_CODE (@1) == INTEGER_CST)
2346 (cmp @00 { force_fit_type (TREE_TYPE (@00), wi::to_widest (@1), 0,
2347 TREE_OVERFLOW (@1)); })
2348 (cmp @00 (convert @1)))
2350 (if (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (TREE_TYPE (@00)))
2351 /* If possible, express the comparison in the shorter mode. */
2352 (if ((cmp == EQ_EXPR || cmp == NE_EXPR
2353 || TYPE_UNSIGNED (TREE_TYPE (@0)) == TYPE_UNSIGNED (TREE_TYPE (@00)))
2354 && (types_match (TREE_TYPE (@10), TREE_TYPE (@00))
2355 || ((TYPE_PRECISION (TREE_TYPE (@00))
2356 >= TYPE_PRECISION (TREE_TYPE (@10)))
2357 && (TYPE_UNSIGNED (TREE_TYPE (@00))
2358 == TYPE_UNSIGNED (TREE_TYPE (@10))))
2359 || (TREE_CODE (@10) == INTEGER_CST
2360 && INTEGRAL_TYPE_P (TREE_TYPE (@00))
2361 && int_fits_type_p (@10, TREE_TYPE (@00)))))
2362 (cmp @00 (convert @10))
2363 (if (TREE_CODE (@10) == INTEGER_CST
2364 && INTEGRAL_TYPE_P (TREE_TYPE (@00))
2365 && !int_fits_type_p (@10, TREE_TYPE (@00)))
2368 tree min = lower_bound_in_type (TREE_TYPE (@10), TREE_TYPE (@00));
2369 tree max = upper_bound_in_type (TREE_TYPE (@10), TREE_TYPE (@00));
2370 bool above = integer_nonzerop (const_binop (LT_EXPR, type, max, @10));
2371 bool below = integer_nonzerop (const_binop (LT_EXPR, type, @10, min));
2373 (if (above || below)
2374 (if (cmp == EQ_EXPR || cmp == NE_EXPR)
2375 { constant_boolean_node (cmp == EQ_EXPR ? false : true, type); }
2376 (if (cmp == LT_EXPR || cmp == LE_EXPR)
2377 { constant_boolean_node (above ? true : false, type); }
2378 (if (cmp == GT_EXPR || cmp == GE_EXPR)
2379 { constant_boolean_node (above ? false : true, type); }))))))))))))
2382 /* A local variable can never be pointed to by
2383 the default SSA name of an incoming parameter.
2384 SSA names are canonicalized to 2nd place. */
2386 (cmp addr@0 SSA_NAME@1)
2387 (if (SSA_NAME_IS_DEFAULT_DEF (@1)
2388 && TREE_CODE (SSA_NAME_VAR (@1)) == PARM_DECL)
2389 (with { tree base = get_base_address (TREE_OPERAND (@0, 0)); }
2390 (if (TREE_CODE (base) == VAR_DECL
2391 && auto_var_in_fn_p (base, current_function_decl))
2392 (if (cmp == NE_EXPR)
2393 { constant_boolean_node (true, type); }
2394 { constant_boolean_node (false, type); }))))))
2396 /* Equality compare simplifications from fold_binary */
2399 /* If we have (A | C) == D where C & ~D != 0, convert this into 0.
2400 Similarly for NE_EXPR. */
2402 (cmp (convert?@3 (bit_ior @0 INTEGER_CST@1)) INTEGER_CST@2)
2403 (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0))
2404 && wi::bit_and_not (@1, @2) != 0)
2405 { constant_boolean_node (cmp == NE_EXPR, type); }))
2407 /* (X ^ Y) == 0 becomes X == Y, and (X ^ Y) != 0 becomes X != Y. */
2409 (cmp (bit_xor @0 @1) integer_zerop)
2412 /* (X ^ Y) == Y becomes X == 0.
2413 Likewise (X ^ Y) == X becomes Y == 0. */
2415 (cmp:c (bit_xor:c @0 @1) @0)
2416 (cmp @1 { build_zero_cst (TREE_TYPE (@1)); }))
2418 /* (X ^ C1) op C2 can be rewritten as X op (C1 ^ C2). */
2420 (cmp (convert?@3 (bit_xor @0 INTEGER_CST@1)) INTEGER_CST@2)
2421 (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0)))
2422 (cmp @0 (bit_xor @1 (convert @2)))))
2425 (cmp (convert? addr@0) integer_zerop)
2426 (if (tree_single_nonzero_warnv_p (@0, NULL))
2427 { constant_boolean_node (cmp == NE_EXPR, type); })))
2429 /* If we have (A & C) == C where C is a power of 2, convert this into
2430 (A & C) != 0. Similarly for NE_EXPR. */
2434 (cmp (bit_and@2 @0 integer_pow2p@1) @1)
2435 (icmp @2 { build_zero_cst (TREE_TYPE (@0)); })))
2437 /* If we have (A & C) != 0 where C is the sign bit of A, convert
2438 this into A < 0. Similarly for (A & C) == 0 into A >= 0. */
2442 (cmp (bit_and (convert?@2 @0) integer_pow2p@1) integer_zerop)
2443 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2444 && (TYPE_PRECISION (TREE_TYPE (@0))
2445 == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (@0))))
2446 && element_precision (@2) >= element_precision (@0)
2447 && wi::only_sign_bit_p (@1, element_precision (@0)))
2448 (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
2449 (ncmp (convert:stype @0) { build_zero_cst (stype); })))))
2451 /* When the addresses are not directly of decls compare base and offset.
2452 This implements some remaining parts of fold_comparison address
2453 comparisons but still no complete part of it. Still it is good
2454 enough to make fold_stmt not regress when not dispatching to fold_binary. */
2455 (for cmp (simple_comparison)
2457 (cmp (convert1?@2 addr@0) (convert2? addr@1))
2460 HOST_WIDE_INT off0, off1;
2461 tree base0 = get_addr_base_and_unit_offset (TREE_OPERAND (@0, 0), &off0);
2462 tree base1 = get_addr_base_and_unit_offset (TREE_OPERAND (@1, 0), &off1);
2463 if (base0 && TREE_CODE (base0) == MEM_REF)
2465 off0 += mem_ref_offset (base0).to_short_addr ();
2466 base0 = TREE_OPERAND (base0, 0);
2468 if (base1 && TREE_CODE (base1) == MEM_REF)
2470 off1 += mem_ref_offset (base1).to_short_addr ();
2471 base1 = TREE_OPERAND (base1, 0);
2474 (if (base0 && base1)
2478 if (decl_in_symtab_p (base0)
2479 && decl_in_symtab_p (base1))
2480 equal = symtab_node::get_create (base0)
2481 ->equal_address_to (symtab_node::get_create (base1));
2482 else if ((DECL_P (base0)
2483 || TREE_CODE (base0) == SSA_NAME
2484 || TREE_CODE (base0) == STRING_CST)
2486 || TREE_CODE (base1) == SSA_NAME
2487 || TREE_CODE (base1) == STRING_CST))
2488 equal = (base0 == base1);
2491 && (cmp == EQ_EXPR || cmp == NE_EXPR
2492 /* If the offsets are equal we can ignore overflow. */
2494 || POINTER_TYPE_OVERFLOW_UNDEFINED
2495 /* Or if we compare using pointers to decls or strings. */
2496 || (POINTER_TYPE_P (TREE_TYPE (@2))
2497 && (DECL_P (base0) || TREE_CODE (base0) == STRING_CST))))
2499 (if (cmp == EQ_EXPR)
2500 { constant_boolean_node (off0 == off1, type); })
2501 (if (cmp == NE_EXPR)
2502 { constant_boolean_node (off0 != off1, type); })
2503 (if (cmp == LT_EXPR)
2504 { constant_boolean_node (off0 < off1, type); })
2505 (if (cmp == LE_EXPR)
2506 { constant_boolean_node (off0 <= off1, type); })
2507 (if (cmp == GE_EXPR)
2508 { constant_boolean_node (off0 >= off1, type); })
2509 (if (cmp == GT_EXPR)
2510 { constant_boolean_node (off0 > off1, type); }))
2512 && DECL_P (base0) && DECL_P (base1)
2513 /* If we compare this as integers require equal offset. */
2514 && (!INTEGRAL_TYPE_P (TREE_TYPE (@2))
2517 (if (cmp == EQ_EXPR)
2518 { constant_boolean_node (false, type); })
2519 (if (cmp == NE_EXPR)
2520 { constant_boolean_node (true, type); })))))))))
2522 /* Simplify pointer equality compares using PTA. */
2526 (if (POINTER_TYPE_P (TREE_TYPE (@0))
2527 && ptrs_compare_unequal (@0, @1))
2528 { neeq == EQ_EXPR ? boolean_false_node : boolean_true_node; })))
2530 /* PR70920: Transform (intptr_t)x eq/ne CST to x eq/ne (typeof x) CST.
2531 and (typeof ptr_cst) x eq/ne ptr_cst to x eq/ne (typeof x) CST.
2532 Disable the transform if either operand is pointer to function.
2533 This broke pr22051-2.c for arm where function pointer
2534 canonicalizaion is not wanted. */
2538 (cmp (convert @0) INTEGER_CST@1)
2539 (if ((POINTER_TYPE_P (TREE_TYPE (@0)) && !FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@0)))
2540 && INTEGRAL_TYPE_P (TREE_TYPE (@1)))
2541 || (INTEGRAL_TYPE_P (TREE_TYPE (@0)) && POINTER_TYPE_P (TREE_TYPE (@1))
2542 && !FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@1)))))
2543 (cmp @0 (convert @1)))))
2545 /* Non-equality compare simplifications from fold_binary */
2546 (for cmp (lt gt le ge)
2547 /* Comparisons with the highest or lowest possible integer of
2548 the specified precision will have known values. */
2550 (cmp (convert?@2 @0) INTEGER_CST@1)
2551 (if ((INTEGRAL_TYPE_P (TREE_TYPE (@1)) || POINTER_TYPE_P (TREE_TYPE (@1)))
2552 && tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@0)))
2555 tree arg1_type = TREE_TYPE (@1);
2556 unsigned int prec = TYPE_PRECISION (arg1_type);
2557 wide_int max = wi::max_value (arg1_type);
2558 wide_int signed_max = wi::max_value (prec, SIGNED);
2559 wide_int min = wi::min_value (arg1_type);
2562 (if (wi::eq_p (@1, max))
2564 (if (cmp == GT_EXPR)
2565 { constant_boolean_node (false, type); })
2566 (if (cmp == GE_EXPR)
2568 (if (cmp == LE_EXPR)
2569 { constant_boolean_node (true, type); })
2570 (if (cmp == LT_EXPR)
2572 (if (wi::eq_p (@1, min))
2574 (if (cmp == LT_EXPR)
2575 { constant_boolean_node (false, type); })
2576 (if (cmp == LE_EXPR)
2578 (if (cmp == GE_EXPR)
2579 { constant_boolean_node (true, type); })
2580 (if (cmp == GT_EXPR)
2582 (if (wi::eq_p (@1, max - 1))
2584 (if (cmp == GT_EXPR)
2585 (eq @2 { wide_int_to_tree (TREE_TYPE (@1), wi::add (@1, 1)); }))
2586 (if (cmp == LE_EXPR)
2587 (ne @2 { wide_int_to_tree (TREE_TYPE (@1), wi::add (@1, 1)); }))))
2588 (if (wi::eq_p (@1, min + 1))
2590 (if (cmp == GE_EXPR)
2591 (ne @2 { wide_int_to_tree (TREE_TYPE (@1), wi::sub (@1, 1)); }))
2592 (if (cmp == LT_EXPR)
2593 (eq @2 { wide_int_to_tree (TREE_TYPE (@1), wi::sub (@1, 1)); }))))
2594 (if (wi::eq_p (@1, signed_max)
2595 && TYPE_UNSIGNED (arg1_type)
2596 /* We will flip the signedness of the comparison operator
2597 associated with the mode of @1, so the sign bit is
2598 specified by this mode. Check that @1 is the signed
2599 max associated with this sign bit. */
2600 && prec == GET_MODE_PRECISION (TYPE_MODE (arg1_type))
2601 /* signed_type does not work on pointer types. */
2602 && INTEGRAL_TYPE_P (arg1_type))
2603 /* The following case also applies to X < signed_max+1
2604 and X >= signed_max+1 because previous transformations. */
2605 (if (cmp == LE_EXPR || cmp == GT_EXPR)
2606 (with { tree st = signed_type_for (arg1_type); }
2607 (if (cmp == LE_EXPR)
2608 (ge (convert:st @0) { build_zero_cst (st); })
2609 (lt (convert:st @0) { build_zero_cst (st); }))))))))))
2611 (for cmp (unordered ordered unlt unle ungt unge uneq ltgt)
2612 /* If the second operand is NaN, the result is constant. */
2615 (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
2616 && (cmp != LTGT_EXPR || ! flag_trapping_math))
2617 { constant_boolean_node (cmp == ORDERED_EXPR || cmp == LTGT_EXPR
2618 ? false : true, type); })))
2620 /* bool_var != 0 becomes bool_var. */
2622 (ne @0 integer_zerop)
2623 (if (TREE_CODE (TREE_TYPE (@0)) == BOOLEAN_TYPE
2624 && types_match (type, TREE_TYPE (@0)))
2626 /* bool_var == 1 becomes bool_var. */
2628 (eq @0 integer_onep)
2629 (if (TREE_CODE (TREE_TYPE (@0)) == BOOLEAN_TYPE
2630 && types_match (type, TREE_TYPE (@0)))
2633 bool_var == 0 becomes !bool_var or
2634 bool_var != 1 becomes !bool_var
2635 here because that only is good in assignment context as long
2636 as we require a tcc_comparison in GIMPLE_CONDs where we'd
2637 replace if (x == 0) with tem = ~x; if (tem != 0) which is
2638 clearly less optimal and which we'll transform again in forwprop. */
2640 /* When one argument is a constant, overflow detection can be simplified.
2641 Currently restricted to single use so as not to interfere too much with
2642 ADD_OVERFLOW detection in tree-ssa-math-opts.c.
2643 A + CST CMP A -> A CMP' CST' */
2644 (for cmp (lt le ge gt)
2647 (cmp:c (plus@2 @0 INTEGER_CST@1) @0)
2648 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
2649 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))
2652 (out @0 { wide_int_to_tree (TREE_TYPE (@0), wi::max_value
2653 (TYPE_PRECISION (TREE_TYPE (@0)), UNSIGNED) - @1); }))))
2655 /* To detect overflow in unsigned A - B, A < B is simpler than A - B > A.
2656 However, the detection logic for SUB_OVERFLOW in tree-ssa-math-opts.c
2657 expects the long form, so we restrict the transformation for now. */
2660 (cmp:c (minus@2 @0 @1) @0)
2661 (if (single_use (@2)
2662 && ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2663 && TYPE_UNSIGNED (TREE_TYPE (@0))
2664 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
2667 /* Testing for overflow is unnecessary if we already know the result. */
2672 (cmp:c (realpart (IFN_SUB_OVERFLOW@2 @0 @1)) @0)
2673 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
2674 && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
2675 (out (imagpart @2) { build_zero_cst (TREE_TYPE (@0)); }))))
2680 (cmp:c (realpart (IFN_ADD_OVERFLOW:c@2 @0 @1)) @0)
2681 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
2682 && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
2683 (out (imagpart @2) { build_zero_cst (TREE_TYPE (@0)); }))))
2685 /* For unsigned operands, -1 / B < A checks whether A * B would overflow.
2686 Simplify it to __builtin_mul_overflow (A, B, <unused>). */
2690 (cmp:c (trunc_div:s integer_all_onesp @1) @0)
2691 (if (TYPE_UNSIGNED (TREE_TYPE (@0)) && !VECTOR_TYPE_P (TREE_TYPE (@0)))
2692 (with { tree t = TREE_TYPE (@0), cpx = build_complex_type (t); }
2693 (out (imagpart (IFN_MUL_OVERFLOW:cpx @0 @1)) { build_zero_cst (t); })))))
2695 /* Simplification of math builtins. These rules must all be optimizations
2696 as well as IL simplifications. If there is a possibility that the new
2697 form could be a pessimization, the rule should go in the canonicalization
2698 section that follows this one.
2700 Rules can generally go in this section if they satisfy one of
2703 - the rule describes an identity
2705 - the rule replaces calls with something as simple as addition or
2708 - the rule contains unary calls only and simplifies the surrounding
2709 arithmetic. (The idea here is to exclude non-unary calls in which
2710 one operand is constant and in which the call is known to be cheap
2711 when the operand has that value.) */
2713 (if (flag_unsafe_math_optimizations)
2714 /* Simplify sqrt(x) * sqrt(x) -> x. */
2716 (mult (SQRT@1 @0) @1)
2717 (if (!HONOR_SNANS (type))
2720 /* Simplify sqrt(x) * sqrt(y) -> sqrt(x*y). */
2721 (for root (SQRT CBRT)
2723 (mult (root:s @0) (root:s @1))
2724 (root (mult @0 @1))))
2726 /* Simplify expN(x) * expN(y) -> expN(x+y). */
2727 (for exps (EXP EXP2 EXP10 POW10)
2729 (mult (exps:s @0) (exps:s @1))
2730 (exps (plus @0 @1))))
2732 /* Simplify a/root(b/c) into a*root(c/b). */
2733 (for root (SQRT CBRT)
2735 (rdiv @0 (root:s (rdiv:s @1 @2)))
2736 (mult @0 (root (rdiv @2 @1)))))
2738 /* Simplify x/expN(y) into x*expN(-y). */
2739 (for exps (EXP EXP2 EXP10 POW10)
2741 (rdiv @0 (exps:s @1))
2742 (mult @0 (exps (negate @1)))))
2744 (for logs (LOG LOG2 LOG10 LOG10)
2745 exps (EXP EXP2 EXP10 POW10)
2746 /* logN(expN(x)) -> x. */
2750 /* expN(logN(x)) -> x. */
2755 /* Optimize logN(func()) for various exponential functions. We
2756 want to determine the value "x" and the power "exponent" in
2757 order to transform logN(x**exponent) into exponent*logN(x). */
2758 (for logs (LOG LOG LOG LOG2 LOG2 LOG2 LOG10 LOG10)
2759 exps (EXP2 EXP10 POW10 EXP EXP10 POW10 EXP EXP2)
2762 (if (SCALAR_FLOAT_TYPE_P (type))
2768 /* Prepare to do logN(exp(exponent)) -> exponent*logN(e). */
2769 x = build_real_truncate (type, dconst_e ());
2772 /* Prepare to do logN(exp2(exponent)) -> exponent*logN(2). */
2773 x = build_real (type, dconst2);
2777 /* Prepare to do logN(exp10(exponent)) -> exponent*logN(10). */
2779 REAL_VALUE_TYPE dconst10;
2780 real_from_integer (&dconst10, VOIDmode, 10, SIGNED);
2781 x = build_real (type, dconst10);
2788 (mult (logs { x; }) @0)))))
2796 (if (SCALAR_FLOAT_TYPE_P (type))
2802 /* Prepare to do logN(sqrt(x)) -> 0.5*logN(x). */
2803 x = build_real (type, dconsthalf);
2806 /* Prepare to do logN(cbrt(x)) -> (1/3)*logN(x). */
2807 x = build_real_truncate (type, dconst_third ());
2813 (mult { x; } (logs @0))))))
2815 /* logN(pow(x,exponent)) -> exponent*logN(x). */
2816 (for logs (LOG LOG2 LOG10)
2820 (mult @1 (logs @0))))
2825 exps (EXP EXP2 EXP10 POW10)
2826 /* sqrt(expN(x)) -> expN(x*0.5). */
2829 (exps (mult @0 { build_real (type, dconsthalf); })))
2830 /* cbrt(expN(x)) -> expN(x/3). */
2833 (exps (mult @0 { build_real_truncate (type, dconst_third ()); })))
2834 /* pow(expN(x), y) -> expN(x*y). */
2837 (exps (mult @0 @1))))
2839 /* tan(atan(x)) -> x. */
2846 /* cabs(x+0i) or cabs(0+xi) -> abs(x). */
2848 (CABS (complex:C @0 real_zerop@1))
2851 /* trunc(trunc(x)) -> trunc(x), etc. */
2852 (for fns (TRUNC FLOOR CEIL ROUND NEARBYINT RINT)
2856 /* f(x) -> x if x is integer valued and f does nothing for such values. */
2857 (for fns (TRUNC FLOOR CEIL ROUND NEARBYINT RINT)
2859 (fns integer_valued_real_p@0)
2862 /* hypot(x,0) and hypot(0,x) -> abs(x). */
2864 (HYPOT:c @0 real_zerop@1)
2867 /* pow(1,x) -> 1. */
2869 (POW real_onep@0 @1)
2873 /* copysign(x,x) -> x. */
2878 /* copysign(x,y) -> fabs(x) if y is nonnegative. */
2879 (COPYSIGN @0 tree_expr_nonnegative_p@1)
2882 (for scale (LDEXP SCALBN SCALBLN)
2883 /* ldexp(0, x) -> 0. */
2885 (scale real_zerop@0 @1)
2887 /* ldexp(x, 0) -> x. */
2889 (scale @0 integer_zerop@1)
2891 /* ldexp(x, y) -> x if x is +-Inf or NaN. */
2893 (scale REAL_CST@0 @1)
2894 (if (!real_isfinite (TREE_REAL_CST_PTR (@0)))
2897 /* Canonicalization of sequences of math builtins. These rules represent
2898 IL simplifications but are not necessarily optimizations.
2900 The sincos pass is responsible for picking "optimal" implementations
2901 of math builtins, which may be more complicated and can sometimes go
2902 the other way, e.g. converting pow into a sequence of sqrts.
2903 We only want to do these canonicalizations before the pass has run. */
2905 (if (flag_unsafe_math_optimizations && canonicalize_math_p ())
2906 /* Simplify tan(x) * cos(x) -> sin(x). */
2908 (mult:c (TAN:s @0) (COS:s @0))
2911 /* Simplify x * pow(x,c) -> pow(x,c+1). */
2913 (mult:c @0 (POW:s @0 REAL_CST@1))
2914 (if (!TREE_OVERFLOW (@1))
2915 (POW @0 (plus @1 { build_one_cst (type); }))))
2917 /* Simplify sin(x) / cos(x) -> tan(x). */
2919 (rdiv (SIN:s @0) (COS:s @0))
2922 /* Simplify cos(x) / sin(x) -> 1 / tan(x). */
2924 (rdiv (COS:s @0) (SIN:s @0))
2925 (rdiv { build_one_cst (type); } (TAN @0)))
2927 /* Simplify sin(x) / tan(x) -> cos(x). */
2929 (rdiv (SIN:s @0) (TAN:s @0))
2930 (if (! HONOR_NANS (@0)
2931 && ! HONOR_INFINITIES (@0))
2934 /* Simplify tan(x) / sin(x) -> 1.0 / cos(x). */
2936 (rdiv (TAN:s @0) (SIN:s @0))
2937 (if (! HONOR_NANS (@0)
2938 && ! HONOR_INFINITIES (@0))
2939 (rdiv { build_one_cst (type); } (COS @0))))
2941 /* Simplify pow(x,y) * pow(x,z) -> pow(x,y+z). */
2943 (mult (POW:s @0 @1) (POW:s @0 @2))
2944 (POW @0 (plus @1 @2)))
2946 /* Simplify pow(x,y) * pow(z,y) -> pow(x*z,y). */
2948 (mult (POW:s @0 @1) (POW:s @2 @1))
2949 (POW (mult @0 @2) @1))
2951 /* Simplify powi(x,y) * powi(z,y) -> powi(x*z,y). */
2953 (mult (POWI:s @0 @1) (POWI:s @2 @1))
2954 (POWI (mult @0 @2) @1))
2956 /* Simplify pow(x,c) / x -> pow(x,c-1). */
2958 (rdiv (POW:s @0 REAL_CST@1) @0)
2959 (if (!TREE_OVERFLOW (@1))
2960 (POW @0 (minus @1 { build_one_cst (type); }))))
2962 /* Simplify x / pow (y,z) -> x * pow(y,-z). */
2964 (rdiv @0 (POW:s @1 @2))
2965 (mult @0 (POW @1 (negate @2))))
2970 /* sqrt(sqrt(x)) -> pow(x,1/4). */
2973 (pows @0 { build_real (type, dconst_quarter ()); }))
2974 /* sqrt(cbrt(x)) -> pow(x,1/6). */
2977 (pows @0 { build_real_truncate (type, dconst_sixth ()); }))
2978 /* cbrt(sqrt(x)) -> pow(x,1/6). */
2981 (pows @0 { build_real_truncate (type, dconst_sixth ()); }))
2982 /* cbrt(cbrt(x)) -> pow(x,1/9), iff x is nonnegative. */
2984 (cbrts (cbrts tree_expr_nonnegative_p@0))
2985 (pows @0 { build_real_truncate (type, dconst_ninth ()); }))
2986 /* sqrt(pow(x,y)) -> pow(|x|,y*0.5). */
2988 (sqrts (pows @0 @1))
2989 (pows (abs @0) (mult @1 { build_real (type, dconsthalf); })))
2990 /* cbrt(pow(x,y)) -> pow(x,y/3), iff x is nonnegative. */
2992 (cbrts (pows tree_expr_nonnegative_p@0 @1))
2993 (pows @0 (mult @1 { build_real_truncate (type, dconst_third ()); })))
2994 /* pow(sqrt(x),y) -> pow(x,y*0.5). */
2996 (pows (sqrts @0) @1)
2997 (pows @0 (mult @1 { build_real (type, dconsthalf); })))
2998 /* pow(cbrt(x),y) -> pow(x,y/3) iff x is nonnegative. */
3000 (pows (cbrts tree_expr_nonnegative_p@0) @1)
3001 (pows @0 (mult @1 { build_real_truncate (type, dconst_third ()); })))
3002 /* pow(pow(x,y),z) -> pow(x,y*z) iff x is nonnegative. */
3004 (pows (pows tree_expr_nonnegative_p@0 @1) @2)
3005 (pows @0 (mult @1 @2))))
3007 /* cabs(x+xi) -> fabs(x)*sqrt(2). */
3009 (CABS (complex @0 @0))
3010 (mult (abs @0) { build_real_truncate (type, dconst_sqrt2 ()); }))
3012 /* hypot(x,x) -> fabs(x)*sqrt(2). */
3015 (mult (abs @0) { build_real_truncate (type, dconst_sqrt2 ()); }))
3017 /* cexp(x+yi) -> exp(x)*cexpi(y). */
3022 (cexps compositional_complex@0)
3023 (if (targetm.libc_has_function (function_c99_math_complex))
3025 (mult (exps@1 (realpart @0)) (realpart (cexpis:type@2 (imagpart @0))))
3026 (mult @1 (imagpart @2)))))))
3028 (if (canonicalize_math_p ())
3029 /* floor(x) -> trunc(x) if x is nonnegative. */
3033 (floors tree_expr_nonnegative_p@0)
3036 (match double_value_p
3038 (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == double_type_node)))
3039 (for froms (BUILT_IN_TRUNCL
3051 /* truncl(extend(x)) -> extend(trunc(x)), etc., if x is a double. */
3052 (if (optimize && canonicalize_math_p ())
3054 (froms (convert double_value_p@0))
3055 (convert (tos @0)))))
3057 (match float_value_p
3059 (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == float_type_node)))
3060 (for froms (BUILT_IN_TRUNCL BUILT_IN_TRUNC
3061 BUILT_IN_FLOORL BUILT_IN_FLOOR
3062 BUILT_IN_CEILL BUILT_IN_CEIL
3063 BUILT_IN_ROUNDL BUILT_IN_ROUND
3064 BUILT_IN_NEARBYINTL BUILT_IN_NEARBYINT
3065 BUILT_IN_RINTL BUILT_IN_RINT)
3066 tos (BUILT_IN_TRUNCF BUILT_IN_TRUNCF
3067 BUILT_IN_FLOORF BUILT_IN_FLOORF
3068 BUILT_IN_CEILF BUILT_IN_CEILF
3069 BUILT_IN_ROUNDF BUILT_IN_ROUNDF
3070 BUILT_IN_NEARBYINTF BUILT_IN_NEARBYINTF
3071 BUILT_IN_RINTF BUILT_IN_RINTF)
3072 /* truncl(extend(x)) and trunc(extend(x)) -> extend(truncf(x)), etc.,
3074 (if (optimize && canonicalize_math_p ()
3075 && targetm.libc_has_function (function_c99_misc))
3077 (froms (convert float_value_p@0))
3078 (convert (tos @0)))))
3080 (for froms (XFLOORL XCEILL XROUNDL XRINTL)
3081 tos (XFLOOR XCEIL XROUND XRINT)
3082 /* llfloorl(extend(x)) -> llfloor(x), etc., if x is a double. */
3083 (if (optimize && canonicalize_math_p ())
3085 (froms (convert double_value_p@0))
3088 (for froms (XFLOORL XCEILL XROUNDL XRINTL
3089 XFLOOR XCEIL XROUND XRINT)
3090 tos (XFLOORF XCEILF XROUNDF XRINTF)
3091 /* llfloorl(extend(x)) and llfloor(extend(x)) -> llfloorf(x), etc.,
3093 (if (optimize && canonicalize_math_p ())
3095 (froms (convert float_value_p@0))
3098 (if (canonicalize_math_p ())
3099 /* xfloor(x) -> fix_trunc(x) if x is nonnegative. */
3100 (for floors (IFLOOR LFLOOR LLFLOOR)
3102 (floors tree_expr_nonnegative_p@0)
3105 (if (canonicalize_math_p ())
3106 /* xfloor(x) -> fix_trunc(x), etc., if x is integer valued. */
3107 (for fns (IFLOOR LFLOOR LLFLOOR
3109 IROUND LROUND LLROUND)
3111 (fns integer_valued_real_p@0)
3113 (if (!flag_errno_math)
3114 /* xrint(x) -> fix_trunc(x), etc., if x is integer valued. */
3115 (for rints (IRINT LRINT LLRINT)
3117 (rints integer_valued_real_p@0)
3120 (if (canonicalize_math_p ())
3121 (for ifn (IFLOOR ICEIL IROUND IRINT)
3122 lfn (LFLOOR LCEIL LROUND LRINT)
3123 llfn (LLFLOOR LLCEIL LLROUND LLRINT)
3124 /* Canonicalize iround (x) to lround (x) on ILP32 targets where
3125 sizeof (int) == sizeof (long). */
3126 (if (TYPE_PRECISION (integer_type_node)
3127 == TYPE_PRECISION (long_integer_type_node))
3130 (lfn:long_integer_type_node @0)))
3131 /* Canonicalize llround (x) to lround (x) on LP64 targets where
3132 sizeof (long long) == sizeof (long). */
3133 (if (TYPE_PRECISION (long_long_integer_type_node)
3134 == TYPE_PRECISION (long_integer_type_node))
3137 (lfn:long_integer_type_node @0)))))
3139 /* cproj(x) -> x if we're ignoring infinities. */
3142 (if (!HONOR_INFINITIES (type))
3145 /* If the real part is inf and the imag part is known to be
3146 nonnegative, return (inf + 0i). */
3148 (CPROJ (complex REAL_CST@0 tree_expr_nonnegative_p@1))
3149 (if (real_isinf (TREE_REAL_CST_PTR (@0)))
3150 { build_complex_inf (type, false); }))
3152 /* If the imag part is inf, return (inf+I*copysign(0,imag)). */
3154 (CPROJ (complex @0 REAL_CST@1))
3155 (if (real_isinf (TREE_REAL_CST_PTR (@1)))
3156 { build_complex_inf (type, TREE_REAL_CST_PTR (@1)->sign); }))
3162 (pows @0 REAL_CST@1)
3164 const REAL_VALUE_TYPE *value = TREE_REAL_CST_PTR (@1);
3165 REAL_VALUE_TYPE tmp;
3168 /* pow(x,0) -> 1. */
3169 (if (real_equal (value, &dconst0))
3170 { build_real (type, dconst1); })
3171 /* pow(x,1) -> x. */
3172 (if (real_equal (value, &dconst1))
3174 /* pow(x,-1) -> 1/x. */
3175 (if (real_equal (value, &dconstm1))
3176 (rdiv { build_real (type, dconst1); } @0))
3177 /* pow(x,0.5) -> sqrt(x). */
3178 (if (flag_unsafe_math_optimizations
3179 && canonicalize_math_p ()
3180 && real_equal (value, &dconsthalf))
3182 /* pow(x,1/3) -> cbrt(x). */
3183 (if (flag_unsafe_math_optimizations
3184 && canonicalize_math_p ()
3185 && (tmp = real_value_truncate (TYPE_MODE (type), dconst_third ()),
3186 real_equal (value, &tmp)))
3189 /* powi(1,x) -> 1. */
3191 (POWI real_onep@0 @1)
3195 (POWI @0 INTEGER_CST@1)
3197 /* powi(x,0) -> 1. */
3198 (if (wi::eq_p (@1, 0))
3199 { build_real (type, dconst1); })
3200 /* powi(x,1) -> x. */
3201 (if (wi::eq_p (@1, 1))
3203 /* powi(x,-1) -> 1/x. */
3204 (if (wi::eq_p (@1, -1))
3205 (rdiv { build_real (type, dconst1); } @0))))
3207 /* Narrowing of arithmetic and logical operations.
3209 These are conceptually similar to the transformations performed for
3210 the C/C++ front-ends by shorten_binary_op and shorten_compare. Long
3211 term we want to move all that code out of the front-ends into here. */
3213 /* If we have a narrowing conversion of an arithmetic operation where
3214 both operands are widening conversions from the same type as the outer
3215 narrowing conversion. Then convert the innermost operands to a suitable
3216 unsigned type (to avoid introducing undefined behavior), perform the
3217 operation and convert the result to the desired type. */
3218 (for op (plus minus)
3220 (convert (op:s (convert@2 @0) (convert@3 @1)))
3221 (if (INTEGRAL_TYPE_P (type)
3222 /* We check for type compatibility between @0 and @1 below,
3223 so there's no need to check that @1/@3 are integral types. */
3224 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
3225 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
3226 /* The precision of the type of each operand must match the
3227 precision of the mode of each operand, similarly for the
3229 && (TYPE_PRECISION (TREE_TYPE (@0))
3230 == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (@0))))
3231 && (TYPE_PRECISION (TREE_TYPE (@1))
3232 == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (@1))))
3233 && TYPE_PRECISION (type) == GET_MODE_PRECISION (TYPE_MODE (type))
3234 /* The inner conversion must be a widening conversion. */
3235 && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
3236 && types_match (@0, @1)
3237 && types_match (@0, type))
3238 (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
3239 (convert (op @0 @1))
3240 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
3241 (convert (op (convert:utype @0) (convert:utype @1))))))))
3243 /* This is another case of narrowing, specifically when there's an outer
3244 BIT_AND_EXPR which masks off bits outside the type of the innermost
3245 operands. Like the previous case we have to convert the operands
3246 to unsigned types to avoid introducing undefined behavior for the
3247 arithmetic operation. */
3248 (for op (minus plus)
3250 (bit_and (op:s (convert@2 @0) (convert@3 @1)) INTEGER_CST@4)
3251 (if (INTEGRAL_TYPE_P (type)
3252 /* We check for type compatibility between @0 and @1 below,
3253 so there's no need to check that @1/@3 are integral types. */
3254 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
3255 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
3256 /* The precision of the type of each operand must match the
3257 precision of the mode of each operand, similarly for the
3259 && (TYPE_PRECISION (TREE_TYPE (@0))
3260 == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (@0))))
3261 && (TYPE_PRECISION (TREE_TYPE (@1))
3262 == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (@1))))
3263 && TYPE_PRECISION (type) == GET_MODE_PRECISION (TYPE_MODE (type))
3264 /* The inner conversion must be a widening conversion. */
3265 && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
3266 && types_match (@0, @1)
3267 && (tree_int_cst_min_precision (@4, TYPE_SIGN (TREE_TYPE (@0)))
3268 <= TYPE_PRECISION (TREE_TYPE (@0)))
3269 && (wi::bit_and (@4, wi::mask (TYPE_PRECISION (TREE_TYPE (@0)),
3270 true, TYPE_PRECISION (type))) == 0))
3271 (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
3272 (with { tree ntype = TREE_TYPE (@0); }
3273 (convert (bit_and (op @0 @1) (convert:ntype @4))))
3274 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
3275 (convert (bit_and (op (convert:utype @0) (convert:utype @1))
3276 (convert:utype @4))))))))
3278 /* Transform (@0 < @1 and @0 < @2) to use min,
3279 (@0 > @1 and @0 > @2) to use max */
3280 (for op (lt le gt ge)
3281 ext (min min max max)
3283 (bit_and (op:cs @0 @1) (op:cs @0 @2))
3284 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
3285 && TREE_CODE (@0) != INTEGER_CST)
3286 (op @0 (ext @1 @2)))))
3289 /* signbit(x) -> 0 if x is nonnegative. */
3290 (SIGNBIT tree_expr_nonnegative_p@0)
3291 { integer_zero_node; })
3294 /* signbit(x) -> x<0 if x doesn't have signed zeros. */
3296 (if (!HONOR_SIGNED_ZEROS (@0))
3297 (convert (lt @0 { build_real (TREE_TYPE (@0), dconst0); }))))
3299 /* Transform comparisons of the form X +- C1 CMP C2 to X CMP C2 -+ C1. */
3301 (for op (plus minus)
3304 (cmp (op@3 @0 INTEGER_CST@1) INTEGER_CST@2)
3305 (if (!TREE_OVERFLOW (@1) && !TREE_OVERFLOW (@2)
3306 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0))
3307 && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@0))
3308 && !TYPE_SATURATING (TREE_TYPE (@0)))
3309 (with { tree res = int_const_binop (rop, @2, @1); }
3310 (if (TREE_OVERFLOW (res))
3311 { constant_boolean_node (cmp == NE_EXPR, type); }
3312 (if (single_use (@3))
3313 (cmp @0 { res; }))))))))
3314 (for cmp (lt le gt ge)
3315 (for op (plus minus)
3318 (cmp (op@3 @0 INTEGER_CST@1) INTEGER_CST@2)
3319 (if (!TREE_OVERFLOW (@1) && !TREE_OVERFLOW (@2)
3320 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
3321 (with { tree res = int_const_binop (rop, @2, @1); }
3322 (if (TREE_OVERFLOW (res))
3324 fold_overflow_warning (("assuming signed overflow does not occur "
3325 "when simplifying conditional to constant"),
3326 WARN_STRICT_OVERFLOW_CONDITIONAL);
3327 bool less = cmp == LE_EXPR || cmp == LT_EXPR;
3328 /* wi::ges_p (@2, 0) should be sufficient for a signed type. */
3329 bool ovf_high = wi::lt_p (@1, 0, TYPE_SIGN (TREE_TYPE (@1)))
3330 != (op == MINUS_EXPR);
3331 constant_boolean_node (less == ovf_high, type);
3333 (if (single_use (@3))
3336 fold_overflow_warning (("assuming signed overflow does not occur "
3337 "when changing X +- C1 cmp C2 to "
3339 WARN_STRICT_OVERFLOW_COMPARISON);
3341 (cmp @0 { res; })))))))))
3343 /* Canonicalizations of BIT_FIELD_REFs. */
3346 (BIT_FIELD_REF @0 @1 @2)
3348 (if (TREE_CODE (TREE_TYPE (@0)) == COMPLEX_TYPE
3349 && tree_int_cst_equal (@1, TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))))
3351 (if (integer_zerop (@2))
3352 (view_convert (realpart @0)))
3353 (if (tree_int_cst_equal (@2, TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))))
3354 (view_convert (imagpart @0)))))
3355 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
3356 && INTEGRAL_TYPE_P (type)
3357 /* On GIMPLE this should only apply to register arguments. */
3358 && (! GIMPLE || is_gimple_reg (@0))
3359 /* A bit-field-ref that referenced the full argument can be stripped. */
3360 && ((compare_tree_int (@1, TYPE_PRECISION (TREE_TYPE (@0))) == 0
3361 && integer_zerop (@2))
3362 /* Low-parts can be reduced to integral conversions.
3363 ??? The following doesn't work for PDP endian. */
3364 || (BYTES_BIG_ENDIAN == WORDS_BIG_ENDIAN
3365 /* Don't even think about BITS_BIG_ENDIAN. */
3366 && TYPE_PRECISION (TREE_TYPE (@0)) % BITS_PER_UNIT == 0
3367 && TYPE_PRECISION (type) % BITS_PER_UNIT == 0
3368 && compare_tree_int (@2, (BYTES_BIG_ENDIAN
3369 ? (TYPE_PRECISION (TREE_TYPE (@0))
3370 - TYPE_PRECISION (type))
3374 /* Simplify vector extracts. */
3377 (BIT_FIELD_REF CONSTRUCTOR@0 @1 @2)
3378 (if (VECTOR_TYPE_P (TREE_TYPE (@0))
3379 && (types_match (type, TREE_TYPE (TREE_TYPE (@0)))
3380 || (VECTOR_TYPE_P (type)
3381 && types_match (TREE_TYPE (type), TREE_TYPE (TREE_TYPE (@0))))))
3384 tree ctor = (TREE_CODE (@0) == SSA_NAME
3385 ? gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0)) : @0);
3386 tree eltype = TREE_TYPE (TREE_TYPE (ctor));
3387 unsigned HOST_WIDE_INT width = tree_to_uhwi (TYPE_SIZE (eltype));
3388 unsigned HOST_WIDE_INT n = tree_to_uhwi (@1);
3389 unsigned HOST_WIDE_INT idx = tree_to_uhwi (@2);
3392 && (idx % width) == 0
3394 && ((idx + n) / width) <= TYPE_VECTOR_SUBPARTS (TREE_TYPE (ctor)))
3399 /* Constructor elements can be subvectors. */
3400 unsigned HOST_WIDE_INT k = 1;
3401 if (CONSTRUCTOR_NELTS (ctor) != 0)
3403 tree cons_elem = TREE_TYPE (CONSTRUCTOR_ELT (ctor, 0)->value);
3404 if (TREE_CODE (cons_elem) == VECTOR_TYPE)
3405 k = TYPE_VECTOR_SUBPARTS (cons_elem);
3409 /* We keep an exact subset of the constructor elements. */
3410 (if ((idx % k) == 0 && (n % k) == 0)
3411 (if (CONSTRUCTOR_NELTS (ctor) == 0)
3412 { build_constructor (type, NULL); }
3419 (if (idx < CONSTRUCTOR_NELTS (ctor))
3420 { CONSTRUCTOR_ELT (ctor, idx)->value; }
3421 { build_zero_cst (type); })
3423 vec<constructor_elt, va_gc> *vals;
3424 vec_alloc (vals, n);
3425 for (unsigned i = 0;
3426 i < n && idx + i < CONSTRUCTOR_NELTS (ctor); ++i)
3427 CONSTRUCTOR_APPEND_ELT (vals, NULL_TREE,
3428 CONSTRUCTOR_ELT (ctor, idx + i)->value);
3429 build_constructor (type, vals);
3431 /* The bitfield references a single constructor element. */
3432 (if (idx + n <= (idx / k + 1) * k)
3434 (if (CONSTRUCTOR_NELTS (ctor) <= idx / k)
3435 { build_zero_cst (type); })
3437 { CONSTRUCTOR_ELT (ctor, idx / k)->value; })
3438 (BIT_FIELD_REF { CONSTRUCTOR_ELT (ctor, idx / k)->value; }
3439 @1 { bitsize_int ((idx % k) * width); })))))))))