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 /* In IEEE floating point, x/1 is not equivalent to x for snans. */
201 (if (!HONOR_SNANS (type))
204 /* In IEEE floating point, x/-1 is not equivalent to -x for snans. */
206 (rdiv @0 real_minus_onep)
207 (if (!HONOR_SNANS (type))
210 (if (flag_reciprocal_math)
211 /* Convert (A/B)/C to A/(B*C) */
213 (rdiv (rdiv:s @0 @1) @2)
214 (rdiv @0 (mult @1 @2)))
216 /* Convert A/(B/C) to (A/B)*C */
218 (rdiv @0 (rdiv:s @1 @2))
219 (mult (rdiv @0 @1) @2)))
221 /* Optimize (X & (-A)) / A where A is a power of 2, to X >> log2(A) */
222 (for div (trunc_div ceil_div floor_div round_div exact_div)
224 (div (convert? (bit_and @0 INTEGER_CST@1)) INTEGER_CST@2)
225 (if (integer_pow2p (@2)
226 && tree_int_cst_sgn (@2) > 0
227 && wi::add (@2, @1) == 0
228 && tree_nop_conversion_p (type, TREE_TYPE (@0)))
229 (rshift (convert @0) { build_int_cst (integer_type_node,
230 wi::exact_log2 (@2)); }))))
232 /* If ARG1 is a constant, we can convert this to a multiply by the
233 reciprocal. This does not have the same rounding properties,
234 so only do this if -freciprocal-math. We can actually
235 always safely do it if ARG1 is a power of two, but it's hard to
236 tell if it is or not in a portable manner. */
237 (for cst (REAL_CST COMPLEX_CST VECTOR_CST)
241 (if (flag_reciprocal_math
244 { tree tem = const_binop (RDIV_EXPR, type, build_one_cst (type), @1); }
246 (mult @0 { tem; } )))
247 (if (cst != COMPLEX_CST)
248 (with { tree inverse = exact_inverse (type, @1); }
250 (mult @0 { inverse; } ))))))))
252 /* Same applies to modulo operations, but fold is inconsistent here
253 and simplifies 0 % x to 0, only preserving literal 0 % 0. */
254 (for mod (ceil_mod floor_mod round_mod trunc_mod)
255 /* 0 % X is always zero. */
257 (mod integer_zerop@0 @1)
258 /* But not for 0 % 0 so that we can get the proper warnings and errors. */
259 (if (!integer_zerop (@1))
261 /* X % 1 is always zero. */
263 (mod @0 integer_onep)
264 { build_zero_cst (type); })
265 /* X % -1 is zero. */
267 (mod @0 integer_minus_onep@1)
268 (if (!TYPE_UNSIGNED (type))
269 { build_zero_cst (type); }))
270 /* (X % Y) % Y is just X % Y. */
272 (mod (mod@2 @0 @1) @1)
274 /* From extract_muldiv_1: (X * C1) % C2 is zero if C1 is a multiple of C2. */
276 (mod (mult @0 INTEGER_CST@1) INTEGER_CST@2)
277 (if (ANY_INTEGRAL_TYPE_P (type)
278 && TYPE_OVERFLOW_UNDEFINED (type)
279 && wi::multiple_of_p (@1, @2, TYPE_SIGN (type)))
280 { build_zero_cst (type); })))
282 /* X % -C is the same as X % C. */
284 (trunc_mod @0 INTEGER_CST@1)
285 (if (TYPE_SIGN (type) == SIGNED
286 && !TREE_OVERFLOW (@1)
288 && !TYPE_OVERFLOW_TRAPS (type)
289 /* Avoid this transformation if C is INT_MIN, i.e. C == -C. */
290 && !sign_bit_p (@1, @1))
291 (trunc_mod @0 (negate @1))))
293 /* X % -Y is the same as X % Y. */
295 (trunc_mod @0 (convert? (negate @1)))
296 (if (INTEGRAL_TYPE_P (type)
297 && !TYPE_UNSIGNED (type)
298 && !TYPE_OVERFLOW_TRAPS (type)
299 && tree_nop_conversion_p (type, TREE_TYPE (@1))
300 /* Avoid this transformation if X might be INT_MIN or
301 Y might be -1, because we would then change valid
302 INT_MIN % -(-1) into invalid INT_MIN % -1. */
303 && (expr_not_equal_to (@0, TYPE_MIN_VALUE (type))
304 || expr_not_equal_to (@1, wi::minus_one (TYPE_PRECISION
306 (trunc_mod @0 (convert @1))))
308 /* X - (X / Y) * Y is the same as X % Y. */
310 (minus (convert1? @2) (convert2? (mult:c (trunc_div @0 @1) @1)))
311 /* We cannot use matching captures here, since in the case of
312 constants we really want the type of @0, not @2. */
313 (if (operand_equal_p (@0, @2, 0)
314 && (INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type)))
315 (convert (trunc_mod @0 @1))))
317 /* Optimize TRUNC_MOD_EXPR by a power of two into a BIT_AND_EXPR,
318 i.e. "X % C" into "X & (C - 1)", if X and C are positive.
319 Also optimize A % (C << N) where C is a power of 2,
320 to A & ((C << N) - 1). */
321 (match (power_of_two_cand @1)
323 (match (power_of_two_cand @1)
324 (lshift INTEGER_CST@1 @2))
325 (for mod (trunc_mod floor_mod)
327 (mod @0 (convert?@3 (power_of_two_cand@1 @2)))
328 (if ((TYPE_UNSIGNED (type)
329 || tree_expr_nonnegative_p (@0))
330 && tree_nop_conversion_p (type, TREE_TYPE (@3))
331 && integer_pow2p (@2) && tree_int_cst_sgn (@2) > 0)
332 (bit_and @0 (convert (minus @1 { build_int_cst (TREE_TYPE (@1), 1); }))))))
334 /* Simplify (unsigned t * 2)/2 -> unsigned t & 0x7FFFFFFF. */
336 (trunc_div (mult @0 integer_pow2p@1) @1)
337 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
338 (bit_and @0 { wide_int_to_tree
339 (type, wi::mask (TYPE_PRECISION (type) - wi::exact_log2 (@1),
340 false, TYPE_PRECISION (type))); })))
342 /* Simplify (unsigned t / 2) * 2 -> unsigned t & ~1. */
344 (mult (trunc_div @0 integer_pow2p@1) @1)
345 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
346 (bit_and @0 (negate @1))))
348 /* Simplify (t * 2) / 2) -> t. */
349 (for div (trunc_div ceil_div floor_div round_div exact_div)
351 (div (mult @0 @1) @1)
352 (if (ANY_INTEGRAL_TYPE_P (type)
353 && TYPE_OVERFLOW_UNDEFINED (type))
357 /* Simplify cos(-x) and cos(|x|) -> cos(x). Similarly for cosh. */
362 /* Simplify pow(-x, y) and pow(|x|,y) -> pow(x,y) if y is an even integer. */
365 (pows (op @0) REAL_CST@1)
366 (with { HOST_WIDE_INT n; }
367 (if (real_isinteger (&TREE_REAL_CST (@1), &n) && (n & 1) == 0)
369 /* Strip negate and abs from both operands of hypot. */
377 /* copysign(-x, y) and copysign(abs(x), y) -> copysign(x, y). */
378 (for copysigns (COPYSIGN)
380 (copysigns (op @0) @1)
383 /* abs(x)*abs(x) -> x*x. Should be valid for all types. */
388 /* cos(copysign(x, y)) -> cos(x). Similarly for cosh. */
392 (coss (copysigns @0 @1))
395 /* pow(copysign(x, y), z) -> pow(x, z) if z is an even integer. */
399 (pows (copysigns @0 @1) REAL_CST@1)
400 (with { HOST_WIDE_INT n; }
401 (if (real_isinteger (&TREE_REAL_CST (@1), &n) && (n & 1) == 0)
406 /* hypot(copysign(x, y), z) -> hypot(x, z). */
408 (hypots (copysigns @0 @1) @2)
410 /* hypot(x, copysign(y, z)) -> hypot(x, y). */
412 (hypots @0 (copysigns @1 @2))
415 /* copysign(copysign(x, y), z) -> copysign(x, z). */
416 (for copysigns (COPYSIGN)
418 (copysigns (copysigns @0 @1) @2)
421 /* copysign(x,y)*copysign(x,y) -> x*x. */
422 (for copysigns (COPYSIGN)
424 (mult (copysigns@2 @0 @1) @2)
427 /* ccos(-x) -> ccos(x). Similarly for ccosh. */
428 (for ccoss (CCOS CCOSH)
433 /* cabs(-x) and cos(conj(x)) -> cabs(x). */
434 (for ops (conj negate)
440 /* Fold (a * (1 << b)) into (a << b) */
442 (mult:c @0 (convert? (lshift integer_onep@1 @2)))
443 (if (! FLOAT_TYPE_P (type)
444 && tree_nop_conversion_p (type, TREE_TYPE (@1)))
447 /* Fold (C1/X)*C2 into (C1*C2)/X. */
449 (mult (rdiv@3 REAL_CST@0 @1) REAL_CST@2)
450 (if (flag_associative_math
453 { tree tem = const_binop (MULT_EXPR, type, @0, @2); }
455 (rdiv { tem; } @1)))))
457 /* Convert C1/(X*C2) into (C1/C2)/X */
459 (rdiv REAL_CST@0 (mult @1 REAL_CST@2))
460 (if (flag_reciprocal_math)
462 { tree tem = const_binop (RDIV_EXPR, type, @0, @2); }
464 (rdiv { tem; } @1)))))
466 /* Simplify ~X & X as zero. */
468 (bit_and:c (convert? @0) (convert? (bit_not @0)))
469 { build_zero_cst (type); })
471 /* Fold (A & ~B) - (A & B) into (A ^ B) - B. */
473 (minus (bit_and:cs @0 (bit_not @1)) (bit_and:cs @0 @1))
474 (minus (bit_xor @0 @1) @1))
476 (minus (bit_and:s @0 INTEGER_CST@2) (bit_and:s @0 INTEGER_CST@1))
477 (if (wi::bit_not (@2) == @1)
478 (minus (bit_xor @0 @1) @1)))
480 /* Fold (A & B) - (A & ~B) into B - (A ^ B). */
482 (minus (bit_and:s @0 @1) (bit_and:cs @0 (bit_not @1)))
483 (minus @1 (bit_xor @0 @1)))
485 /* Simplify (X & ~Y) | (~X & Y) -> X ^ Y. */
487 (bit_ior (bit_and:c @0 (bit_not @1)) (bit_and:c (bit_not @0) @1))
490 (bit_ior:c (bit_and @0 INTEGER_CST@2) (bit_and (bit_not @0) INTEGER_CST@1))
491 (if (wi::bit_not (@2) == @1)
494 /* X % Y is smaller than Y. */
497 (cmp (trunc_mod @0 @1) @1)
498 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
499 { constant_boolean_node (cmp == LT_EXPR, type); })))
502 (cmp @1 (trunc_mod @0 @1))
503 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
504 { constant_boolean_node (cmp == GT_EXPR, type); })))
508 (bit_ior @0 integer_all_onesp@1)
513 (bit_and @0 integer_zerop@1)
519 (for op (bit_ior bit_xor plus)
521 (op:c (convert? @0) (convert? (bit_not @0)))
522 (convert { build_all_ones_cst (TREE_TYPE (@0)); })))
527 { build_zero_cst (type); })
529 /* Canonicalize X ^ ~0 to ~X. */
531 (bit_xor @0 integer_all_onesp@1)
536 (bit_and @0 integer_all_onesp)
539 /* x & x -> x, x | x -> x */
540 (for bitop (bit_and bit_ior)
545 /* x + (x & 1) -> (x + 1) & ~1 */
547 (plus:c @0 (bit_and:s @0 integer_onep@1))
548 (bit_and (plus @0 @1) (bit_not @1)))
550 /* x & ~(x & y) -> x & ~y */
551 /* x | ~(x | y) -> x | ~y */
552 (for bitop (bit_and bit_ior)
554 (bitop:c @0 (bit_not (bitop:cs @0 @1)))
555 (bitop @0 (bit_not @1))))
557 /* (x | y) & ~x -> y & ~x */
558 /* (x & y) | ~x -> y | ~x */
559 (for bitop (bit_and bit_ior)
560 rbitop (bit_ior bit_and)
562 (bitop:c (rbitop:c @0 @1) (bit_not@2 @0))
565 /* (x & y) ^ (x | y) -> x ^ y */
567 (bit_xor:c (bit_and @0 @1) (bit_ior @0 @1))
570 /* (x ^ y) ^ (x | y) -> x & y */
572 (bit_xor:c (bit_xor @0 @1) (bit_ior @0 @1))
575 /* (x & y) + (x ^ y) -> x | y */
576 /* (x & y) | (x ^ y) -> x | y */
577 /* (x & y) ^ (x ^ y) -> x | y */
578 (for op (plus bit_ior bit_xor)
580 (op:c (bit_and @0 @1) (bit_xor @0 @1))
583 /* (x & y) + (x | y) -> x + y */
585 (plus:c (bit_and @0 @1) (bit_ior @0 @1))
588 /* (x + y) - (x | y) -> x & y */
590 (minus (plus @0 @1) (bit_ior @0 @1))
591 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
592 && !TYPE_SATURATING (type))
595 /* (x + y) - (x & y) -> x | y */
597 (minus (plus @0 @1) (bit_and @0 @1))
598 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
599 && !TYPE_SATURATING (type))
602 /* (x | y) - (x ^ y) -> x & y */
604 (minus (bit_ior @0 @1) (bit_xor @0 @1))
607 /* (x | y) - (x & y) -> x ^ y */
609 (minus (bit_ior @0 @1) (bit_and @0 @1))
612 /* (x | y) & ~(x & y) -> x ^ y */
614 (bit_and:c (bit_ior @0 @1) (bit_not (bit_and @0 @1)))
617 /* (x | y) & (~x ^ y) -> x & y */
619 (bit_and:c (bit_ior:c @0 @1) (bit_xor:c @1 (bit_not @0)))
622 /* ~x & ~y -> ~(x | y)
623 ~x | ~y -> ~(x & y) */
624 (for op (bit_and bit_ior)
625 rop (bit_ior bit_and)
627 (op (convert1? (bit_not @0)) (convert2? (bit_not @1)))
628 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
629 && tree_nop_conversion_p (type, TREE_TYPE (@1)))
630 (bit_not (rop (convert @0) (convert @1))))))
632 /* If we are XORing or adding two BIT_AND_EXPR's, both of which are and'ing
633 with a constant, and the two constants have no bits in common,
634 we should treat this as a BIT_IOR_EXPR since this may produce more
636 (for op (bit_xor plus)
638 (op (convert1? (bit_and@4 @0 INTEGER_CST@1))
639 (convert2? (bit_and@5 @2 INTEGER_CST@3)))
640 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
641 && tree_nop_conversion_p (type, TREE_TYPE (@2))
642 && wi::bit_and (@1, @3) == 0)
643 (bit_ior (convert @4) (convert @5)))))
645 /* (X | Y) ^ X -> Y & ~ X*/
647 (bit_xor:c (convert? (bit_ior:c @0 @1)) (convert? @0))
648 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
649 (convert (bit_and @1 (bit_not @0)))))
651 /* Convert ~X ^ ~Y to X ^ Y. */
653 (bit_xor (convert1? (bit_not @0)) (convert2? (bit_not @1)))
654 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
655 && tree_nop_conversion_p (type, TREE_TYPE (@1)))
656 (bit_xor (convert @0) (convert @1))))
658 /* Convert ~X ^ C to X ^ ~C. */
660 (bit_xor (convert? (bit_not @0)) INTEGER_CST@1)
661 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
662 (bit_xor (convert @0) (bit_not @1))))
664 /* Fold (X & Y) ^ Y as ~X & Y. */
666 (bit_xor:c (bit_and:c @0 @1) @1)
667 (bit_and (bit_not @0) @1))
669 /* Given a bit-wise operation CODE applied to ARG0 and ARG1, see if both
670 operands are another bit-wise operation with a common input. If so,
671 distribute the bit operations to save an operation and possibly two if
672 constants are involved. For example, convert
673 (A | B) & (A | C) into A | (B & C)
674 Further simplification will occur if B and C are constants. */
675 (for op (bit_and bit_ior)
676 rop (bit_ior bit_and)
678 (op (convert? (rop:c @0 @1)) (convert? (rop @0 @2)))
679 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
680 (rop (convert @0) (op (convert @1) (convert @2))))))
690 (abs tree_expr_nonnegative_p@0)
693 /* A few cases of fold-const.c negate_expr_p predicate. */
696 (if ((INTEGRAL_TYPE_P (type)
697 && TYPE_OVERFLOW_WRAPS (type))
698 || (!TYPE_OVERFLOW_SANITIZED (type)
699 && may_negate_without_overflow_p (t)))))
704 (if (!TYPE_OVERFLOW_SANITIZED (type))))
707 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (t)))))
708 /* VECTOR_CST handling of non-wrapping types would recurse in unsupported
712 (if (FLOAT_TYPE_P (TREE_TYPE (type)) || TYPE_OVERFLOW_WRAPS (type))))
714 /* (-A) * (-B) -> A * B */
716 (mult:c (convert1? (negate @0)) (convert2? negate_expr_p@1))
717 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
718 && tree_nop_conversion_p (type, TREE_TYPE (@1)))
719 (mult (convert @0) (convert (negate @1)))))
721 /* -(A + B) -> (-B) - A. */
723 (negate (plus:c @0 negate_expr_p@1))
724 (if (!HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type))
725 && !HONOR_SIGNED_ZEROS (element_mode (type)))
726 (minus (negate @1) @0)))
728 /* A - B -> A + (-B) if B is easily negatable. */
730 (minus @0 negate_expr_p@1)
731 (if (!FIXED_POINT_TYPE_P (type))
732 (plus @0 (negate @1))))
734 /* Try to fold (type) X op CST -> (type) (X op ((type-x) CST))
736 For bitwise binary operations apply operand conversions to the
737 binary operation result instead of to the operands. This allows
738 to combine successive conversions and bitwise binary operations.
739 We combine the above two cases by using a conditional convert. */
740 (for bitop (bit_and bit_ior bit_xor)
742 (bitop (convert @0) (convert? @1))
743 (if (((TREE_CODE (@1) == INTEGER_CST
744 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
745 && int_fits_type_p (@1, TREE_TYPE (@0)))
746 || types_match (@0, @1))
747 /* ??? This transform conflicts with fold-const.c doing
748 Convert (T)(x & c) into (T)x & (T)c, if c is an integer
749 constants (if x has signed type, the sign bit cannot be set
750 in c). This folds extension into the BIT_AND_EXPR.
751 Restrict it to GIMPLE to avoid endless recursions. */
752 && (bitop != BIT_AND_EXPR || GIMPLE)
753 && (/* That's a good idea if the conversion widens the operand, thus
754 after hoisting the conversion the operation will be narrower. */
755 TYPE_PRECISION (TREE_TYPE (@0)) < TYPE_PRECISION (type)
756 /* It's also a good idea if the conversion is to a non-integer
758 || GET_MODE_CLASS (TYPE_MODE (type)) != MODE_INT
759 /* Or if the precision of TO is not the same as the precision
761 || TYPE_PRECISION (type) != GET_MODE_PRECISION (TYPE_MODE (type))))
762 (convert (bitop @0 (convert @1))))))
764 (for bitop (bit_and bit_ior)
765 rbitop (bit_ior bit_and)
766 /* (x | y) & x -> x */
767 /* (x & y) | x -> x */
769 (bitop:c (rbitop:c @0 @1) @0)
771 /* (~x | y) & x -> x & y */
772 /* (~x & y) | x -> x | y */
774 (bitop:c (rbitop:c (bit_not @0) @1) @0)
777 /* Simplify (A & B) OP0 (C & B) to (A OP0 C) & B. */
778 (for bitop (bit_and bit_ior bit_xor)
780 (bitop (bit_and:c @0 @1) (bit_and @2 @1))
781 (bit_and (bitop @0 @2) @1)))
783 /* (x | CST1) & CST2 -> (x & CST2) | (CST1 & CST2) */
785 (bit_and (bit_ior @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
786 (bit_ior (bit_and @0 @2) (bit_and @1 @2)))
788 /* Combine successive equal operations with constants. */
789 (for bitop (bit_and bit_ior bit_xor)
791 (bitop (bitop @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
792 (bitop @0 (bitop @1 @2))))
794 /* Try simple folding for X op !X, and X op X with the help
795 of the truth_valued_p and logical_inverted_value predicates. */
796 (match truth_valued_p
798 (if (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1)))
799 (for op (tcc_comparison truth_and truth_andif truth_or truth_orif truth_xor)
800 (match truth_valued_p
802 (match truth_valued_p
805 (match (logical_inverted_value @0)
807 (match (logical_inverted_value @0)
808 (bit_not truth_valued_p@0))
809 (match (logical_inverted_value @0)
810 (eq @0 integer_zerop))
811 (match (logical_inverted_value @0)
812 (ne truth_valued_p@0 integer_truep))
813 (match (logical_inverted_value @0)
814 (bit_xor truth_valued_p@0 integer_truep))
818 (bit_and:c @0 (logical_inverted_value @0))
819 { build_zero_cst (type); })
820 /* X | !X and X ^ !X -> 1, , if X is truth-valued. */
821 (for op (bit_ior bit_xor)
823 (op:c truth_valued_p@0 (logical_inverted_value @0))
824 { constant_boolean_node (true, type); }))
825 /* X ==/!= !X is false/true. */
828 (op:c truth_valued_p@0 (logical_inverted_value @0))
829 { constant_boolean_node (op == NE_EXPR ? true : false, type); }))
831 /* If arg1 and arg2 are booleans (or any single bit type)
832 then try to simplify:
839 But only do this if our result feeds into a comparison as
840 this transformation is not always a win, particularly on
841 targets with and-not instructions.
842 -> simplify_bitwise_binary_boolean */
844 (ne (bit_and:c (bit_not @0) @1) integer_zerop)
845 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
846 && TYPE_PRECISION (TREE_TYPE (@1)) == 1)
847 (if (TYPE_UNSIGNED (TREE_TYPE (@1)))
851 (ne (bit_ior:c (bit_not @0) @1) integer_zerop)
852 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
853 && TYPE_PRECISION (TREE_TYPE (@1)) == 1)
854 (if (TYPE_UNSIGNED (TREE_TYPE (@1)))
860 (bit_not (bit_not @0))
863 /* Convert ~ (-A) to A - 1. */
865 (bit_not (convert? (negate @0)))
866 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
867 (convert (minus @0 { build_each_one_cst (TREE_TYPE (@0)); }))))
869 /* Convert ~ (A - 1) or ~ (A + -1) to -A. */
871 (bit_not (convert? (minus @0 integer_each_onep)))
872 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
873 (convert (negate @0))))
875 (bit_not (convert? (plus @0 integer_all_onesp)))
876 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
877 (convert (negate @0))))
879 /* Part of convert ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify. */
881 (bit_not (convert? (bit_xor @0 INTEGER_CST@1)))
882 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
883 (convert (bit_xor @0 (bit_not @1)))))
885 (bit_not (convert? (bit_xor:c (bit_not @0) @1)))
886 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
887 (convert (bit_xor @0 @1))))
889 /* (x & ~m) | (y & m) -> ((x ^ y) & m) ^ x */
891 (bit_ior:c (bit_and:cs @0 (bit_not @2)) (bit_and:cs @1 @2))
892 (bit_xor (bit_and (bit_xor @0 @1) @2) @0))
894 /* Fold A - (A & B) into ~B & A. */
896 (minus (convert? @0) (convert?:s (bit_and:cs @0 @1)))
897 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
898 && tree_nop_conversion_p (type, TREE_TYPE (@1)))
899 (convert (bit_and (bit_not @1) @0))))
903 /* ((X inner_op C0) outer_op C1)
904 With X being a tree where value_range has reasoned certain bits to always be
905 zero throughout its computed value range,
906 inner_op = {|,^}, outer_op = {|,^} and inner_op != outer_op
907 where zero_mask has 1's for all bits that are sure to be 0 in
909 if (inner_op == '^') C0 &= ~C1;
910 if ((C0 & ~zero_mask) == 0) then emit (X outer_op (C0 outer_op C1)
911 if ((C1 & ~zero_mask) == 0) then emit (X inner_op (C0 outer_op C1)
913 (for inner_op (bit_ior bit_xor)
914 outer_op (bit_xor bit_ior)
917 (inner_op:s @2 INTEGER_CST@0) INTEGER_CST@1)
921 wide_int zero_mask_not;
925 if (TREE_CODE (@2) == SSA_NAME)
926 zero_mask_not = get_nonzero_bits (@2);
930 if (inner_op == BIT_XOR_EXPR)
932 C0 = wi::bit_and_not (@0, @1);
933 cst_emit = wi::bit_or (C0, @1);
938 cst_emit = wi::bit_xor (@0, @1);
941 (if (!fail && wi::bit_and (C0, zero_mask_not) == 0)
942 (outer_op @2 { wide_int_to_tree (type, cst_emit); })
943 (if (!fail && wi::bit_and (@1, zero_mask_not) == 0)
944 (inner_op @2 { wide_int_to_tree (type, cst_emit); }))))))
946 /* Associate (p +p off1) +p off2 as (p +p (off1 + off2)). */
948 (pointer_plus (pointer_plus:s @0 @1) @3)
949 (pointer_plus @0 (plus @1 @3)))
955 tem4 = (unsigned long) tem3;
960 (pointer_plus @0 (convert?@2 (minus@3 (convert @1) (convert @0))))
961 /* Conditionally look through a sign-changing conversion. */
962 (if (TYPE_PRECISION (TREE_TYPE (@2)) == TYPE_PRECISION (TREE_TYPE (@3))
963 && ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@1)))
964 || (GENERIC && type == TREE_TYPE (@1))))
968 tem = (sizetype) ptr;
972 and produce the simpler and easier to analyze with respect to alignment
973 ... = ptr & ~algn; */
975 (pointer_plus @0 (negate (bit_and (convert @0) INTEGER_CST@1)))
976 (with { tree algn = wide_int_to_tree (TREE_TYPE (@0), wi::bit_not (@1)); }
977 (bit_and @0 { algn; })))
979 /* Try folding difference of addresses. */
981 (minus (convert ADDR_EXPR@0) (convert @1))
982 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
983 (with { HOST_WIDE_INT diff; }
984 (if (ptr_difference_const (@0, @1, &diff))
985 { build_int_cst_type (type, diff); }))))
987 (minus (convert @0) (convert ADDR_EXPR@1))
988 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
989 (with { HOST_WIDE_INT diff; }
990 (if (ptr_difference_const (@0, @1, &diff))
991 { build_int_cst_type (type, diff); }))))
993 /* If arg0 is derived from the address of an object or function, we may
994 be able to fold this expression using the object or function's
997 (bit_and (convert? @0) INTEGER_CST@1)
998 (if (POINTER_TYPE_P (TREE_TYPE (@0))
999 && tree_nop_conversion_p (type, TREE_TYPE (@0)))
1003 unsigned HOST_WIDE_INT bitpos;
1004 get_pointer_alignment_1 (@0, &align, &bitpos);
1006 (if (wi::ltu_p (@1, align / BITS_PER_UNIT))
1007 { wide_int_to_tree (type, wi::bit_and (@1, bitpos / BITS_PER_UNIT)); }))))
1010 /* We can't reassociate at all for saturating types. */
1011 (if (!TYPE_SATURATING (type))
1013 /* Contract negates. */
1014 /* A + (-B) -> A - B */
1016 (plus:c (convert1? @0) (convert2? (negate @1)))
1017 /* Apply STRIP_NOPS on @0 and the negate. */
1018 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
1019 && tree_nop_conversion_p (type, TREE_TYPE (@1))
1020 && !TYPE_OVERFLOW_SANITIZED (type))
1021 (minus (convert @0) (convert @1))))
1022 /* A - (-B) -> A + B */
1024 (minus (convert1? @0) (convert2? (negate @1)))
1025 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
1026 && tree_nop_conversion_p (type, TREE_TYPE (@1))
1027 && !TYPE_OVERFLOW_SANITIZED (type))
1028 (plus (convert @0) (convert @1))))
1031 (negate (convert? (negate @1)))
1032 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1033 && !TYPE_OVERFLOW_SANITIZED (type))
1036 /* We can't reassociate floating-point unless -fassociative-math
1037 or fixed-point plus or minus because of saturation to +-Inf. */
1038 (if ((!FLOAT_TYPE_P (type) || flag_associative_math)
1039 && !FIXED_POINT_TYPE_P (type))
1041 /* Match patterns that allow contracting a plus-minus pair
1042 irrespective of overflow issues. */
1043 /* (A +- B) - A -> +- B */
1044 /* (A +- B) -+ B -> A */
1045 /* A - (A +- B) -> -+ B */
1046 /* A +- (B -+ A) -> +- B */
1048 (minus (plus:c @0 @1) @0)
1051 (minus (minus @0 @1) @0)
1054 (plus:c (minus @0 @1) @1)
1057 (minus @0 (plus:c @0 @1))
1060 (minus @0 (minus @0 @1))
1063 /* (A +- CST) +- CST -> A + CST */
1064 (for outer_op (plus minus)
1065 (for inner_op (plus minus)
1067 (outer_op (inner_op @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
1068 /* If the constant operation overflows we cannot do the transform
1069 as we would introduce undefined overflow, for example
1070 with (a - 1) + INT_MIN. */
1071 (with { tree cst = const_binop (outer_op == inner_op
1072 ? PLUS_EXPR : MINUS_EXPR, type, @1, @2); }
1073 (if (cst && !TREE_OVERFLOW (cst))
1074 (inner_op @0 { cst; } ))))))
1076 /* (CST - A) +- CST -> CST - A */
1077 (for outer_op (plus minus)
1079 (outer_op (minus CONSTANT_CLASS_P@1 @0) CONSTANT_CLASS_P@2)
1080 (with { tree cst = const_binop (outer_op, type, @1, @2); }
1081 (if (cst && !TREE_OVERFLOW (cst))
1082 (minus { cst; } @0)))))
1086 (plus:c (bit_not @0) @0)
1087 (if (!TYPE_OVERFLOW_TRAPS (type))
1088 { build_all_ones_cst (type); }))
1092 (plus (convert? (bit_not @0)) integer_each_onep)
1093 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1094 (negate (convert @0))))
1098 (minus (convert? (negate @0)) integer_each_onep)
1099 (if (!TYPE_OVERFLOW_TRAPS (type)
1100 && tree_nop_conversion_p (type, TREE_TYPE (@0)))
1101 (bit_not (convert @0))))
1105 (minus integer_all_onesp @0)
1108 /* (T)(P + A) - (T)P -> (T) A */
1109 (for add (plus pointer_plus)
1111 (minus (convert (add @0 @1))
1113 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
1114 /* For integer types, if A has a smaller type
1115 than T the result depends on the possible
1117 E.g. T=size_t, A=(unsigned)429497295, P>0.
1118 However, if an overflow in P + A would cause
1119 undefined behavior, we can assume that there
1121 || (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1122 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1123 /* For pointer types, if the conversion of A to the
1124 final type requires a sign- or zero-extension,
1125 then we have to punt - it is not defined which
1127 || (POINTER_TYPE_P (TREE_TYPE (@0))
1128 && TREE_CODE (@1) == INTEGER_CST
1129 && tree_int_cst_sign_bit (@1) == 0))
1132 /* (T)P - (T)(P + A) -> -(T) A */
1133 (for add (plus pointer_plus)
1136 (convert (add @0 @1)))
1137 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
1138 /* For integer types, if A has a smaller type
1139 than T the result depends on the possible
1141 E.g. T=size_t, A=(unsigned)429497295, P>0.
1142 However, if an overflow in P + A would cause
1143 undefined behavior, we can assume that there
1145 || (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1146 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1147 /* For pointer types, if the conversion of A to the
1148 final type requires a sign- or zero-extension,
1149 then we have to punt - it is not defined which
1151 || (POINTER_TYPE_P (TREE_TYPE (@0))
1152 && TREE_CODE (@1) == INTEGER_CST
1153 && tree_int_cst_sign_bit (@1) == 0))
1154 (negate (convert @1)))))
1156 /* (T)(P + A) - (T)(P + B) -> (T)A - (T)B */
1157 (for add (plus pointer_plus)
1159 (minus (convert (add @0 @1))
1160 (convert (add @0 @2)))
1161 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
1162 /* For integer types, if A has a smaller type
1163 than T the result depends on the possible
1165 E.g. T=size_t, A=(unsigned)429497295, P>0.
1166 However, if an overflow in P + A would cause
1167 undefined behavior, we can assume that there
1169 || (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1170 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1171 /* For pointer types, if the conversion of A to the
1172 final type requires a sign- or zero-extension,
1173 then we have to punt - it is not defined which
1175 || (POINTER_TYPE_P (TREE_TYPE (@0))
1176 && TREE_CODE (@1) == INTEGER_CST
1177 && tree_int_cst_sign_bit (@1) == 0
1178 && TREE_CODE (@2) == INTEGER_CST
1179 && tree_int_cst_sign_bit (@2) == 0))
1180 (minus (convert @1) (convert @2)))))))
1183 /* Simplifications of MIN_EXPR, MAX_EXPR, fmin() and fmax(). */
1185 (for minmax (min max FMIN FMAX)
1189 /* min(max(x,y),y) -> y. */
1191 (min:c (max:c @0 @1) @1)
1193 /* max(min(x,y),y) -> y. */
1195 (max:c (min:c @0 @1) @1)
1199 (if (INTEGRAL_TYPE_P (type)
1200 && TYPE_MIN_VALUE (type)
1201 && operand_equal_p (@1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
1205 (if (INTEGRAL_TYPE_P (type)
1206 && TYPE_MAX_VALUE (type)
1207 && operand_equal_p (@1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
1209 (for minmax (FMIN FMAX)
1210 /* If either argument is NaN, return the other one. Avoid the
1211 transformation if we get (and honor) a signalling NaN. */
1213 (minmax:c @0 REAL_CST@1)
1214 (if (real_isnan (TREE_REAL_CST_PTR (@1))
1215 && (!HONOR_SNANS (@1) || !TREE_REAL_CST (@1).signalling))
1217 /* Convert fmin/fmax to MIN_EXPR/MAX_EXPR. C99 requires these
1218 functions to return the numeric arg if the other one is NaN.
1219 MIN and MAX don't honor that, so only transform if -ffinite-math-only
1220 is set. C99 doesn't require -0.0 to be handled, so we don't have to
1221 worry about it either. */
1222 (if (flag_finite_math_only)
1230 /* Simplifications of shift and rotates. */
1232 (for rotate (lrotate rrotate)
1234 (rotate integer_all_onesp@0 @1)
1237 /* Optimize -1 >> x for arithmetic right shifts. */
1239 (rshift integer_all_onesp@0 @1)
1240 (if (!TYPE_UNSIGNED (type)
1241 && tree_expr_nonnegative_p (@1))
1244 /* Optimize (x >> c) << c into x & (-1<<c). */
1246 (lshift (rshift @0 INTEGER_CST@1) @1)
1247 (if (wi::ltu_p (@1, element_precision (type)))
1248 (bit_and @0 (lshift { build_minus_one_cst (type); } @1))))
1250 /* Optimize (x << c) >> c into x & ((unsigned)-1 >> c) for unsigned
1253 (rshift (lshift @0 INTEGER_CST@1) @1)
1254 (if (TYPE_UNSIGNED (type)
1255 && (wi::ltu_p (@1, element_precision (type))))
1256 (bit_and @0 (rshift { build_minus_one_cst (type); } @1))))
1258 (for shiftrotate (lrotate rrotate lshift rshift)
1260 (shiftrotate @0 integer_zerop)
1263 (shiftrotate integer_zerop@0 @1)
1265 /* Prefer vector1 << scalar to vector1 << vector2
1266 if vector2 is uniform. */
1267 (for vec (VECTOR_CST CONSTRUCTOR)
1269 (shiftrotate @0 vec@1)
1270 (with { tree tem = uniform_vector_p (@1); }
1272 (shiftrotate @0 { tem; }))))))
1274 /* Rewrite an LROTATE_EXPR by a constant into an
1275 RROTATE_EXPR by a new constant. */
1277 (lrotate @0 INTEGER_CST@1)
1278 (rrotate @0 { const_binop (MINUS_EXPR, TREE_TYPE (@1),
1279 build_int_cst (TREE_TYPE (@1),
1280 element_precision (type)), @1); }))
1282 /* Turn (a OP c1) OP c2 into a OP (c1+c2). */
1283 (for op (lrotate rrotate rshift lshift)
1285 (op (op @0 INTEGER_CST@1) INTEGER_CST@2)
1286 (with { unsigned int prec = element_precision (type); }
1287 (if (wi::ge_p (@1, 0, TYPE_SIGN (TREE_TYPE (@1)))
1288 && wi::lt_p (@1, prec, TYPE_SIGN (TREE_TYPE (@1)))
1289 && wi::ge_p (@2, 0, TYPE_SIGN (TREE_TYPE (@2)))
1290 && wi::lt_p (@2, prec, TYPE_SIGN (TREE_TYPE (@2))))
1291 (with { unsigned int low = wi::add (@1, @2).to_uhwi (); }
1292 /* Deal with a OP (c1 + c2) being undefined but (a OP c1) OP c2
1293 being well defined. */
1295 (if (op == LROTATE_EXPR || op == RROTATE_EXPR)
1296 (op @0 { build_int_cst (TREE_TYPE (@1), low % prec); })
1297 (if (TYPE_UNSIGNED (type) || op == LSHIFT_EXPR)
1298 { build_zero_cst (type); }
1299 (op @0 { build_int_cst (TREE_TYPE (@1), prec - 1); })))
1300 (op @0 { build_int_cst (TREE_TYPE (@1), low); })))))))
1303 /* ((1 << A) & 1) != 0 -> A == 0
1304 ((1 << A) & 1) == 0 -> A != 0 */
1308 (cmp (bit_and (lshift integer_onep @0) integer_onep) integer_zerop)
1309 (icmp @0 { build_zero_cst (TREE_TYPE (@0)); })))
1311 /* (CST1 << A) == CST2 -> A == ctz (CST2) - ctz (CST1)
1312 (CST1 << A) != CST2 -> A != ctz (CST2) - ctz (CST1)
1316 (cmp (lshift INTEGER_CST@0 @1) INTEGER_CST@2)
1317 (with { int cand = wi::ctz (@2) - wi::ctz (@0); }
1319 || (!integer_zerop (@2)
1320 && wi::ne_p (wi::lshift (@0, cand), @2)))
1321 { constant_boolean_node (cmp == NE_EXPR, type); }
1322 (if (!integer_zerop (@2)
1323 && wi::eq_p (wi::lshift (@0, cand), @2))
1324 (cmp @1 { build_int_cst (TREE_TYPE (@1), cand); }))))))
1326 /* Fold (X << C1) & C2 into (X << C1) & (C2 | ((1 << C1) - 1))
1327 (X >> C1) & C2 into (X >> C1) & (C2 | ~((type) -1 >> C1))
1328 if the new mask might be further optimized. */
1329 (for shift (lshift rshift)
1331 (bit_and (convert?:s@4 (shift:s@5 (convert1?@3 @0) INTEGER_CST@1))
1333 (if (tree_nop_conversion_p (TREE_TYPE (@4), TREE_TYPE (@5))
1334 && TYPE_PRECISION (type) <= HOST_BITS_PER_WIDE_INT
1335 && tree_fits_uhwi_p (@1)
1336 && tree_to_uhwi (@1) > 0
1337 && tree_to_uhwi (@1) < TYPE_PRECISION (type))
1340 unsigned int shiftc = tree_to_uhwi (@1);
1341 unsigned HOST_WIDE_INT mask = TREE_INT_CST_LOW (@2);
1342 unsigned HOST_WIDE_INT newmask, zerobits = 0;
1343 tree shift_type = TREE_TYPE (@3);
1346 if (shift == LSHIFT_EXPR)
1347 zerobits = ((((unsigned HOST_WIDE_INT) 1) << shiftc) - 1);
1348 else if (shift == RSHIFT_EXPR
1349 && (TYPE_PRECISION (shift_type)
1350 == GET_MODE_PRECISION (TYPE_MODE (shift_type))))
1352 prec = TYPE_PRECISION (TREE_TYPE (@3));
1354 /* See if more bits can be proven as zero because of
1357 && TYPE_UNSIGNED (TREE_TYPE (@0)))
1359 tree inner_type = TREE_TYPE (@0);
1360 if ((TYPE_PRECISION (inner_type)
1361 == GET_MODE_PRECISION (TYPE_MODE (inner_type)))
1362 && TYPE_PRECISION (inner_type) < prec)
1364 prec = TYPE_PRECISION (inner_type);
1365 /* See if we can shorten the right shift. */
1367 shift_type = inner_type;
1368 /* Otherwise X >> C1 is all zeros, so we'll optimize
1369 it into (X, 0) later on by making sure zerobits
1373 zerobits = ~(unsigned HOST_WIDE_INT) 0;
1376 zerobits >>= HOST_BITS_PER_WIDE_INT - shiftc;
1377 zerobits <<= prec - shiftc;
1379 /* For arithmetic shift if sign bit could be set, zerobits
1380 can contain actually sign bits, so no transformation is
1381 possible, unless MASK masks them all away. In that
1382 case the shift needs to be converted into logical shift. */
1383 if (!TYPE_UNSIGNED (TREE_TYPE (@3))
1384 && prec == TYPE_PRECISION (TREE_TYPE (@3)))
1386 if ((mask & zerobits) == 0)
1387 shift_type = unsigned_type_for (TREE_TYPE (@3));
1393 /* ((X << 16) & 0xff00) is (X, 0). */
1394 (if ((mask & zerobits) == mask)
1395 { build_int_cst (type, 0); }
1396 (with { newmask = mask | zerobits; }
1397 (if (newmask != mask && (newmask & (newmask + 1)) == 0)
1400 /* Only do the transformation if NEWMASK is some integer
1402 for (prec = BITS_PER_UNIT;
1403 prec < HOST_BITS_PER_WIDE_INT; prec <<= 1)
1404 if (newmask == (((unsigned HOST_WIDE_INT) 1) << prec) - 1)
1407 (if (prec < HOST_BITS_PER_WIDE_INT
1408 || newmask == ~(unsigned HOST_WIDE_INT) 0)
1410 { tree newmaskt = build_int_cst_type (TREE_TYPE (@2), newmask); }
1411 (if (!tree_int_cst_equal (newmaskt, @2))
1412 (if (shift_type != TREE_TYPE (@3))
1413 (bit_and (convert (shift:shift_type (convert @3) @1)) { newmaskt; })
1414 (bit_and @4 { newmaskt; })))))))))))))
1416 /* Fold (X {&,^,|} C2) << C1 into (X << C1) {&,^,|} (C2 << C1)
1417 (X {&,^,|} C2) >> C1 into (X >> C1) & (C2 >> C1). */
1418 (for shift (lshift rshift)
1419 (for bit_op (bit_and bit_xor bit_ior)
1421 (shift (convert?:s (bit_op:s @0 INTEGER_CST@2)) INTEGER_CST@1)
1422 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1423 (with { tree mask = int_const_binop (shift, fold_convert (type, @2), @1); }
1424 (bit_op (shift (convert @0) @1) { mask; }))))))
1427 /* Simplifications of conversions. */
1429 /* Basic strip-useless-type-conversions / strip_nops. */
1430 (for cvt (convert view_convert float fix_trunc)
1433 (if ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@0)))
1434 || (GENERIC && type == TREE_TYPE (@0)))
1437 /* Contract view-conversions. */
1439 (view_convert (view_convert @0))
1442 /* For integral conversions with the same precision or pointer
1443 conversions use a NOP_EXPR instead. */
1446 (if ((INTEGRAL_TYPE_P (type) || POINTER_TYPE_P (type))
1447 && (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
1448 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (@0)))
1451 /* Strip inner integral conversions that do not change precision or size. */
1453 (view_convert (convert@0 @1))
1454 (if ((INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
1455 && (INTEGRAL_TYPE_P (TREE_TYPE (@1)) || POINTER_TYPE_P (TREE_TYPE (@1)))
1456 && (TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
1457 && (TYPE_SIZE (TREE_TYPE (@0)) == TYPE_SIZE (TREE_TYPE (@1))))
1460 /* Re-association barriers around constants and other re-association
1461 barriers can be removed. */
1463 (paren CONSTANT_CLASS_P@0)
1466 (paren (paren@1 @0))
1469 /* Handle cases of two conversions in a row. */
1470 (for ocvt (convert float fix_trunc)
1471 (for icvt (convert float)
1476 tree inside_type = TREE_TYPE (@0);
1477 tree inter_type = TREE_TYPE (@1);
1478 int inside_int = INTEGRAL_TYPE_P (inside_type);
1479 int inside_ptr = POINTER_TYPE_P (inside_type);
1480 int inside_float = FLOAT_TYPE_P (inside_type);
1481 int inside_vec = VECTOR_TYPE_P (inside_type);
1482 unsigned int inside_prec = TYPE_PRECISION (inside_type);
1483 int inside_unsignedp = TYPE_UNSIGNED (inside_type);
1484 int inter_int = INTEGRAL_TYPE_P (inter_type);
1485 int inter_ptr = POINTER_TYPE_P (inter_type);
1486 int inter_float = FLOAT_TYPE_P (inter_type);
1487 int inter_vec = VECTOR_TYPE_P (inter_type);
1488 unsigned int inter_prec = TYPE_PRECISION (inter_type);
1489 int inter_unsignedp = TYPE_UNSIGNED (inter_type);
1490 int final_int = INTEGRAL_TYPE_P (type);
1491 int final_ptr = POINTER_TYPE_P (type);
1492 int final_float = FLOAT_TYPE_P (type);
1493 int final_vec = VECTOR_TYPE_P (type);
1494 unsigned int final_prec = TYPE_PRECISION (type);
1495 int final_unsignedp = TYPE_UNSIGNED (type);
1498 /* In addition to the cases of two conversions in a row
1499 handled below, if we are converting something to its own
1500 type via an object of identical or wider precision, neither
1501 conversion is needed. */
1502 (if (((GIMPLE && useless_type_conversion_p (type, inside_type))
1504 && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (inside_type)))
1505 && (((inter_int || inter_ptr) && final_int)
1506 || (inter_float && final_float))
1507 && inter_prec >= final_prec)
1510 /* Likewise, if the intermediate and initial types are either both
1511 float or both integer, we don't need the middle conversion if the
1512 former is wider than the latter and doesn't change the signedness
1513 (for integers). Avoid this if the final type is a pointer since
1514 then we sometimes need the middle conversion. Likewise if the
1515 final type has a precision not equal to the size of its mode. */
1516 (if (((inter_int && inside_int) || (inter_float && inside_float))
1517 && (final_int || final_float)
1518 && inter_prec >= inside_prec
1519 && (inter_float || inter_unsignedp == inside_unsignedp)
1520 && ! (final_prec != GET_MODE_PRECISION (TYPE_MODE (type))
1521 && TYPE_MODE (type) == TYPE_MODE (inter_type)))
1524 /* If we have a sign-extension of a zero-extended value, we can
1525 replace that by a single zero-extension. Likewise if the
1526 final conversion does not change precision we can drop the
1527 intermediate conversion. */
1528 (if (inside_int && inter_int && final_int
1529 && ((inside_prec < inter_prec && inter_prec < final_prec
1530 && inside_unsignedp && !inter_unsignedp)
1531 || final_prec == inter_prec))
1534 /* Two conversions in a row are not needed unless:
1535 - some conversion is floating-point (overstrict for now), or
1536 - some conversion is a vector (overstrict for now), or
1537 - the intermediate type is narrower than both initial and
1539 - the intermediate type and innermost type differ in signedness,
1540 and the outermost type is wider than the intermediate, or
1541 - the initial type is a pointer type and the precisions of the
1542 intermediate and final types differ, or
1543 - the final type is a pointer type and the precisions of the
1544 initial and intermediate types differ. */
1545 (if (! inside_float && ! inter_float && ! final_float
1546 && ! inside_vec && ! inter_vec && ! final_vec
1547 && (inter_prec >= inside_prec || inter_prec >= final_prec)
1548 && ! (inside_int && inter_int
1549 && inter_unsignedp != inside_unsignedp
1550 && inter_prec < final_prec)
1551 && ((inter_unsignedp && inter_prec > inside_prec)
1552 == (final_unsignedp && final_prec > inter_prec))
1553 && ! (inside_ptr && inter_prec != final_prec)
1554 && ! (final_ptr && inside_prec != inter_prec)
1555 && ! (final_prec != GET_MODE_PRECISION (TYPE_MODE (type))
1556 && TYPE_MODE (type) == TYPE_MODE (inter_type)))
1559 /* A truncation to an unsigned type (a zero-extension) should be
1560 canonicalized as bitwise and of a mask. */
1561 (if (GIMPLE /* PR70366: doing this in GENERIC breaks -Wconversion. */
1562 && final_int && inter_int && inside_int
1563 && final_prec == inside_prec
1564 && final_prec > inter_prec
1566 (convert (bit_and @0 { wide_int_to_tree
1568 wi::mask (inter_prec, false,
1569 TYPE_PRECISION (inside_type))); })))
1571 /* If we are converting an integer to a floating-point that can
1572 represent it exactly and back to an integer, we can skip the
1573 floating-point conversion. */
1574 (if (GIMPLE /* PR66211 */
1575 && inside_int && inter_float && final_int &&
1576 (unsigned) significand_size (TYPE_MODE (inter_type))
1577 >= inside_prec - !inside_unsignedp)
1580 /* If we have a narrowing conversion to an integral type that is fed by a
1581 BIT_AND_EXPR, we might be able to remove the BIT_AND_EXPR if it merely
1582 masks off bits outside the final type (and nothing else). */
1584 (convert (bit_and @0 INTEGER_CST@1))
1585 (if (INTEGRAL_TYPE_P (type)
1586 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
1587 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))
1588 && operand_equal_p (@1, build_low_bits_mask (TREE_TYPE (@1),
1589 TYPE_PRECISION (type)), 0))
1593 /* (X /[ex] A) * A -> X. */
1595 (mult (convert? (exact_div @0 @1)) @1)
1596 /* Look through a sign-changing conversion. */
1599 /* Canonicalization of binary operations. */
1601 /* Convert X + -C into X - C. */
1603 (plus @0 REAL_CST@1)
1604 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
1605 (with { tree tem = const_unop (NEGATE_EXPR, type, @1); }
1606 (if (!TREE_OVERFLOW (tem) || !flag_trapping_math)
1607 (minus @0 { tem; })))))
1609 /* Convert x+x into x*2.0. */
1612 (if (SCALAR_FLOAT_TYPE_P (type))
1613 (mult @0 { build_real (type, dconst2); })))
1616 (minus integer_zerop @1)
1619 /* (ARG0 - ARG1) is the same as (-ARG1 + ARG0). So check whether
1620 ARG0 is zero and X + ARG0 reduces to X, since that would mean
1621 (-ARG1 + ARG0) reduces to -ARG1. */
1623 (minus real_zerop@0 @1)
1624 (if (fold_real_zero_addition_p (type, @0, 0))
1627 /* Transform x * -1 into -x. */
1629 (mult @0 integer_minus_onep)
1632 /* True if we can easily extract the real and imaginary parts of a complex
1634 (match compositional_complex
1635 (convert? (complex @0 @1)))
1637 /* COMPLEX_EXPR and REALPART/IMAGPART_EXPR cancellations. */
1639 (complex (realpart @0) (imagpart @0))
1642 (realpart (complex @0 @1))
1645 (imagpart (complex @0 @1))
1648 /* Sometimes we only care about half of a complex expression. */
1650 (realpart (convert?:s (conj:s @0)))
1651 (convert (realpart @0)))
1653 (imagpart (convert?:s (conj:s @0)))
1654 (convert (negate (imagpart @0))))
1655 (for part (realpart imagpart)
1656 (for op (plus minus)
1658 (part (convert?:s@2 (op:s @0 @1)))
1659 (convert (op (part @0) (part @1))))))
1661 (realpart (convert?:s (CEXPI:s @0)))
1664 (imagpart (convert?:s (CEXPI:s @0)))
1667 /* conj(conj(x)) -> x */
1669 (conj (convert? (conj @0)))
1670 (if (tree_nop_conversion_p (TREE_TYPE (@0), type))
1673 /* conj({x,y}) -> {x,-y} */
1675 (conj (convert?:s (complex:s @0 @1)))
1676 (with { tree itype = TREE_TYPE (type); }
1677 (complex (convert:itype @0) (negate (convert:itype @1)))))
1679 /* BSWAP simplifications, transforms checked by gcc.dg/builtin-bswap-8.c. */
1680 (for bswap (BUILT_IN_BSWAP16 BUILT_IN_BSWAP32 BUILT_IN_BSWAP64)
1685 (bswap (bit_not (bswap @0)))
1687 (for bitop (bit_xor bit_ior bit_and)
1689 (bswap (bitop:c (bswap @0) @1))
1690 (bitop @0 (bswap @1)))))
1693 /* Combine COND_EXPRs and VEC_COND_EXPRs. */
1695 /* Simplify constant conditions.
1696 Only optimize constant conditions when the selected branch
1697 has the same type as the COND_EXPR. This avoids optimizing
1698 away "c ? x : throw", where the throw has a void type.
1699 Note that we cannot throw away the fold-const.c variant nor
1700 this one as we depend on doing this transform before possibly
1701 A ? B : B -> B triggers and the fold-const.c one can optimize
1702 0 ? A : B to B even if A has side-effects. Something
1703 genmatch cannot handle. */
1705 (cond INTEGER_CST@0 @1 @2)
1706 (if (integer_zerop (@0))
1707 (if (!VOID_TYPE_P (TREE_TYPE (@2)) || VOID_TYPE_P (type))
1709 (if (!VOID_TYPE_P (TREE_TYPE (@1)) || VOID_TYPE_P (type))
1712 (vec_cond VECTOR_CST@0 @1 @2)
1713 (if (integer_all_onesp (@0))
1715 (if (integer_zerop (@0))
1718 (for cnd (cond vec_cond)
1719 /* A ? B : (A ? X : C) -> A ? B : C. */
1721 (cnd @0 (cnd @0 @1 @2) @3)
1724 (cnd @0 @1 (cnd @0 @2 @3))
1726 /* A ? B : (!A ? C : X) -> A ? B : C. */
1727 /* ??? This matches embedded conditions open-coded because genmatch
1728 would generate matching code for conditions in separate stmts only.
1729 The following is still important to merge then and else arm cases
1730 from if-conversion. */
1732 (cnd @0 @1 (cnd @2 @3 @4))
1733 (if (COMPARISON_CLASS_P (@0)
1734 && COMPARISON_CLASS_P (@2)
1735 && invert_tree_comparison
1736 (TREE_CODE (@0), HONOR_NANS (TREE_OPERAND (@0, 0))) == TREE_CODE (@2)
1737 && operand_equal_p (TREE_OPERAND (@0, 0), TREE_OPERAND (@2, 0), 0)
1738 && operand_equal_p (TREE_OPERAND (@0, 1), TREE_OPERAND (@2, 1), 0))
1741 (cnd @0 (cnd @1 @2 @3) @4)
1742 (if (COMPARISON_CLASS_P (@0)
1743 && COMPARISON_CLASS_P (@1)
1744 && invert_tree_comparison
1745 (TREE_CODE (@0), HONOR_NANS (TREE_OPERAND (@0, 0))) == TREE_CODE (@1)
1746 && operand_equal_p (TREE_OPERAND (@0, 0), TREE_OPERAND (@1, 0), 0)
1747 && operand_equal_p (TREE_OPERAND (@0, 1), TREE_OPERAND (@1, 1), 0))
1750 /* A ? B : B -> B. */
1755 /* !A ? B : C -> A ? C : B. */
1757 (cnd (logical_inverted_value truth_valued_p@0) @1 @2)
1760 /* A + (B vcmp C ? 1 : 0) -> A - (B vcmp C ? -1 : 0), since vector comparisons
1761 return all -1 or all 0 results. */
1762 /* ??? We could instead convert all instances of the vec_cond to negate,
1763 but that isn't necessarily a win on its own. */
1765 (plus:c @3 (view_convert? (vec_cond:s @0 integer_each_onep@1 integer_zerop@2)))
1766 (if (VECTOR_TYPE_P (type)
1767 && TYPE_VECTOR_SUBPARTS (type) == TYPE_VECTOR_SUBPARTS (TREE_TYPE (@1))
1768 && (TYPE_MODE (TREE_TYPE (type))
1769 == TYPE_MODE (TREE_TYPE (TREE_TYPE (@1)))))
1770 (minus @3 (view_convert (vec_cond @0 (negate @1) @2)))))
1772 /* ... likewise A - (B vcmp C ? 1 : 0) -> A + (B vcmp C ? -1 : 0). */
1774 (minus @3 (view_convert? (vec_cond:s @0 integer_each_onep@1 integer_zerop@2)))
1775 (if (VECTOR_TYPE_P (type)
1776 && TYPE_VECTOR_SUBPARTS (type) == TYPE_VECTOR_SUBPARTS (TREE_TYPE (@1))
1777 && (TYPE_MODE (TREE_TYPE (type))
1778 == TYPE_MODE (TREE_TYPE (TREE_TYPE (@1)))))
1779 (plus @3 (view_convert (vec_cond @0 (negate @1) @2)))))
1782 /* Simplifications of comparisons. */
1784 /* See if we can reduce the magnitude of a constant involved in a
1785 comparison by changing the comparison code. This is a canonicalization
1786 formerly done by maybe_canonicalize_comparison_1. */
1790 (cmp @0 INTEGER_CST@1)
1791 (if (tree_int_cst_sgn (@1) == -1)
1792 (acmp @0 { wide_int_to_tree (TREE_TYPE (@1), wi::add (@1, 1)); }))))
1796 (cmp @0 INTEGER_CST@1)
1797 (if (tree_int_cst_sgn (@1) == 1)
1798 (acmp @0 { wide_int_to_tree (TREE_TYPE (@1), wi::sub (@1, 1)); }))))
1801 /* We can simplify a logical negation of a comparison to the
1802 inverted comparison. As we cannot compute an expression
1803 operator using invert_tree_comparison we have to simulate
1804 that with expression code iteration. */
1805 (for cmp (tcc_comparison)
1806 icmp (inverted_tcc_comparison)
1807 ncmp (inverted_tcc_comparison_with_nans)
1808 /* Ideally we'd like to combine the following two patterns
1809 and handle some more cases by using
1810 (logical_inverted_value (cmp @0 @1))
1811 here but for that genmatch would need to "inline" that.
1812 For now implement what forward_propagate_comparison did. */
1814 (bit_not (cmp @0 @1))
1815 (if (VECTOR_TYPE_P (type)
1816 || (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1))
1817 /* Comparison inversion may be impossible for trapping math,
1818 invert_tree_comparison will tell us. But we can't use
1819 a computed operator in the replacement tree thus we have
1820 to play the trick below. */
1821 (with { enum tree_code ic = invert_tree_comparison
1822 (cmp, HONOR_NANS (@0)); }
1828 (bit_xor (cmp @0 @1) integer_truep)
1829 (with { enum tree_code ic = invert_tree_comparison
1830 (cmp, HONOR_NANS (@0)); }
1836 /* Transform comparisons of the form X - Y CMP 0 to X CMP Y.
1837 ??? The transformation is valid for the other operators if overflow
1838 is undefined for the type, but performing it here badly interacts
1839 with the transformation in fold_cond_expr_with_comparison which
1840 attempts to synthetize ABS_EXPR. */
1843 (cmp (minus@2 @0 @1) integer_zerop)
1844 (if (single_use (@2))
1847 /* Transform comparisons of the form X * C1 CMP 0 to X CMP 0 in the
1848 signed arithmetic case. That form is created by the compiler
1849 often enough for folding it to be of value. One example is in
1850 computing loop trip counts after Operator Strength Reduction. */
1851 (for cmp (simple_comparison)
1852 scmp (swapped_simple_comparison)
1854 (cmp (mult@3 @0 INTEGER_CST@1) integer_zerop@2)
1855 /* Handle unfolded multiplication by zero. */
1856 (if (integer_zerop (@1))
1858 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1859 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1861 /* If @1 is negative we swap the sense of the comparison. */
1862 (if (tree_int_cst_sgn (@1) < 0)
1866 /* Simplify comparison of something with itself. For IEEE
1867 floating-point, we can only do some of these simplifications. */
1871 (if (! FLOAT_TYPE_P (TREE_TYPE (@0))
1872 || ! HONOR_NANS (@0))
1873 { constant_boolean_node (true, type); }
1874 (if (cmp != EQ_EXPR)
1880 || ! FLOAT_TYPE_P (TREE_TYPE (@0))
1881 || ! HONOR_NANS (@0))
1882 { constant_boolean_node (false, type); })))
1883 (for cmp (unle unge uneq)
1886 { constant_boolean_node (true, type); }))
1889 (if (!flag_trapping_math)
1890 { constant_boolean_node (false, type); }))
1892 /* Fold ~X op ~Y as Y op X. */
1893 (for cmp (simple_comparison)
1895 (cmp (bit_not@2 @0) (bit_not@3 @1))
1896 (if (single_use (@2) && single_use (@3))
1899 /* Fold ~X op C as X op' ~C, where op' is the swapped comparison. */
1900 (for cmp (simple_comparison)
1901 scmp (swapped_simple_comparison)
1903 (cmp (bit_not@2 @0) CONSTANT_CLASS_P@1)
1904 (if (single_use (@2)
1905 && (TREE_CODE (@1) == INTEGER_CST || TREE_CODE (@1) == VECTOR_CST))
1906 (scmp @0 (bit_not @1)))))
1908 (for cmp (simple_comparison)
1909 /* Fold (double)float1 CMP (double)float2 into float1 CMP float2. */
1911 (cmp (convert@2 @0) (convert? @1))
1912 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
1913 && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@2))
1914 == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0)))
1915 && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@2))
1916 == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1))))
1919 tree type1 = TREE_TYPE (@1);
1920 if (TREE_CODE (@1) == REAL_CST && !DECIMAL_FLOAT_TYPE_P (type1))
1922 REAL_VALUE_TYPE orig = TREE_REAL_CST (@1);
1923 if (TYPE_PRECISION (type1) > TYPE_PRECISION (float_type_node)
1924 && exact_real_truncate (TYPE_MODE (float_type_node), &orig))
1925 type1 = float_type_node;
1926 if (TYPE_PRECISION (type1) > TYPE_PRECISION (double_type_node)
1927 && exact_real_truncate (TYPE_MODE (double_type_node), &orig))
1928 type1 = double_type_node;
1931 = (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type1)
1932 ? TREE_TYPE (@0) : type1);
1934 (if (TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (newtype))
1935 (cmp (convert:newtype @0) (convert:newtype @1))))))
1939 /* IEEE doesn't distinguish +0 and -0 in comparisons. */
1941 /* a CMP (-0) -> a CMP 0 */
1942 (if (REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@1)))
1943 (cmp @0 { build_real (TREE_TYPE (@1), dconst0); }))
1944 /* x != NaN is always true, other ops are always false. */
1945 (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
1946 && ! HONOR_SNANS (@1))
1947 { constant_boolean_node (cmp == NE_EXPR, type); })
1948 /* Fold comparisons against infinity. */
1949 (if (REAL_VALUE_ISINF (TREE_REAL_CST (@1))
1950 && MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (@1))))
1953 REAL_VALUE_TYPE max;
1954 enum tree_code code = cmp;
1955 bool neg = REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1));
1957 code = swap_tree_comparison (code);
1960 /* x > +Inf is always false, if with ignore sNANs. */
1961 (if (code == GT_EXPR
1962 && ! HONOR_SNANS (@0))
1963 { constant_boolean_node (false, type); })
1964 (if (code == LE_EXPR)
1965 /* x <= +Inf is always true, if we don't case about NaNs. */
1966 (if (! HONOR_NANS (@0))
1967 { constant_boolean_node (true, type); }
1968 /* x <= +Inf is the same as x == x, i.e. !isnan(x). */
1970 /* x == +Inf and x >= +Inf are always equal to x > DBL_MAX. */
1971 (if (code == EQ_EXPR || code == GE_EXPR)
1972 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
1974 (lt @0 { build_real (TREE_TYPE (@0), max); })
1975 (gt @0 { build_real (TREE_TYPE (@0), max); }))))
1976 /* x < +Inf is always equal to x <= DBL_MAX. */
1977 (if (code == LT_EXPR)
1978 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
1980 (ge @0 { build_real (TREE_TYPE (@0), max); })
1981 (le @0 { build_real (TREE_TYPE (@0), max); }))))
1982 /* x != +Inf is always equal to !(x > DBL_MAX). */
1983 (if (code == NE_EXPR)
1984 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
1985 (if (! HONOR_NANS (@0))
1987 (ge @0 { build_real (TREE_TYPE (@0), max); })
1988 (le @0 { build_real (TREE_TYPE (@0), max); }))
1990 (bit_xor (lt @0 { build_real (TREE_TYPE (@0), max); })
1991 { build_one_cst (type); })
1992 (bit_xor (gt @0 { build_real (TREE_TYPE (@0), max); })
1993 { build_one_cst (type); }))))))))))
1995 /* If this is a comparison of a real constant with a PLUS_EXPR
1996 or a MINUS_EXPR of a real constant, we can convert it into a
1997 comparison with a revised real constant as long as no overflow
1998 occurs when unsafe_math_optimizations are enabled. */
1999 (if (flag_unsafe_math_optimizations)
2000 (for op (plus minus)
2002 (cmp (op @0 REAL_CST@1) REAL_CST@2)
2005 tree tem = const_binop (op == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR,
2006 TREE_TYPE (@1), @2, @1);
2008 (if (tem && !TREE_OVERFLOW (tem))
2009 (cmp @0 { tem; }))))))
2011 /* Likewise, we can simplify a comparison of a real constant with
2012 a MINUS_EXPR whose first operand is also a real constant, i.e.
2013 (c1 - x) < c2 becomes x > c1-c2. Reordering is allowed on
2014 floating-point types only if -fassociative-math is set. */
2015 (if (flag_associative_math)
2017 (cmp (minus REAL_CST@0 @1) REAL_CST@2)
2018 (with { tree tem = const_binop (MINUS_EXPR, TREE_TYPE (@1), @0, @2); }
2019 (if (tem && !TREE_OVERFLOW (tem))
2020 (cmp { tem; } @1)))))
2022 /* Fold comparisons against built-in math functions. */
2023 (if (flag_unsafe_math_optimizations
2024 && ! flag_errno_math)
2027 (cmp (sq @0) REAL_CST@1)
2029 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
2031 /* sqrt(x) < y is always false, if y is negative. */
2032 (if (cmp == EQ_EXPR || cmp == LT_EXPR || cmp == LE_EXPR)
2033 { constant_boolean_node (false, type); })
2034 /* sqrt(x) > y is always true, if y is negative and we
2035 don't care about NaNs, i.e. negative values of x. */
2036 (if (cmp == NE_EXPR || !HONOR_NANS (@0))
2037 { constant_boolean_node (true, type); })
2038 /* sqrt(x) > y is the same as x >= 0, if y is negative. */
2039 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })))
2040 (if (real_equal (TREE_REAL_CST_PTR (@1), &dconst0))
2042 /* sqrt(x) < 0 is always false. */
2043 (if (cmp == LT_EXPR)
2044 { constant_boolean_node (false, type); })
2045 /* sqrt(x) >= 0 is always true if we don't care about NaNs. */
2046 (if (cmp == GE_EXPR && !HONOR_NANS (@0))
2047 { constant_boolean_node (true, type); })
2048 /* sqrt(x) <= 0 -> x == 0. */
2049 (if (cmp == LE_EXPR)
2051 /* Otherwise sqrt(x) cmp 0 -> x cmp 0. Here cmp can be >=, >,
2052 == or !=. In the last case:
2054 (sqrt(x) != 0) == (NaN != 0) == true == (x != 0)
2056 if x is negative or NaN. Due to -funsafe-math-optimizations,
2057 the results for other x follow from natural arithmetic. */
2059 (if (cmp == GT_EXPR || cmp == GE_EXPR)
2063 real_arithmetic (&c2, MULT_EXPR,
2064 &TREE_REAL_CST (@1), &TREE_REAL_CST (@1));
2065 real_convert (&c2, TYPE_MODE (TREE_TYPE (@0)), &c2);
2067 (if (REAL_VALUE_ISINF (c2))
2068 /* sqrt(x) > y is x == +Inf, when y is very large. */
2069 (if (HONOR_INFINITIES (@0))
2070 (eq @0 { build_real (TREE_TYPE (@0), c2); })
2071 { constant_boolean_node (false, type); })
2072 /* sqrt(x) > c is the same as x > c*c. */
2073 (cmp @0 { build_real (TREE_TYPE (@0), c2); }))))
2074 (if (cmp == LT_EXPR || cmp == LE_EXPR)
2078 real_arithmetic (&c2, MULT_EXPR,
2079 &TREE_REAL_CST (@1), &TREE_REAL_CST (@1));
2080 real_convert (&c2, TYPE_MODE (TREE_TYPE (@0)), &c2);
2082 (if (REAL_VALUE_ISINF (c2))
2084 /* sqrt(x) < y is always true, when y is a very large
2085 value and we don't care about NaNs or Infinities. */
2086 (if (! HONOR_NANS (@0) && ! HONOR_INFINITIES (@0))
2087 { constant_boolean_node (true, type); })
2088 /* sqrt(x) < y is x != +Inf when y is very large and we
2089 don't care about NaNs. */
2090 (if (! HONOR_NANS (@0))
2091 (ne @0 { build_real (TREE_TYPE (@0), c2); }))
2092 /* sqrt(x) < y is x >= 0 when y is very large and we
2093 don't care about Infinities. */
2094 (if (! HONOR_INFINITIES (@0))
2095 (ge @0 { build_real (TREE_TYPE (@0), dconst0); }))
2096 /* sqrt(x) < y is x >= 0 && x != +Inf, when y is large. */
2099 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
2100 (ne @0 { build_real (TREE_TYPE (@0), c2); }))))
2101 /* sqrt(x) < c is the same as x < c*c, if we ignore NaNs. */
2102 (if (! HONOR_NANS (@0))
2103 (cmp @0 { build_real (TREE_TYPE (@0), c2); })
2104 /* sqrt(x) < c is the same as x >= 0 && x < c*c. */
2107 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
2108 (cmp @0 { build_real (TREE_TYPE (@0), c2); }))))))))))))
2110 /* Unordered tests if either argument is a NaN. */
2112 (bit_ior (unordered @0 @0) (unordered @1 @1))
2113 (if (types_match (@0, @1))
2116 (bit_and (ordered @0 @0) (ordered @1 @1))
2117 (if (types_match (@0, @1))
2120 (bit_ior:c (unordered @0 @0) (unordered:c@2 @0 @1))
2123 (bit_and:c (ordered @0 @0) (ordered:c@2 @0 @1))
2126 /* Simple range test simplifications. */
2127 /* A < B || A >= B -> true. */
2128 (for test1 (lt le le le ne ge)
2129 test2 (ge gt ge ne eq ne)
2131 (bit_ior:c (test1 @0 @1) (test2 @0 @1))
2132 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2133 || VECTOR_INTEGER_TYPE_P (TREE_TYPE (@0)))
2134 { constant_boolean_node (true, type); })))
2135 /* A < B && A >= B -> false. */
2136 (for test1 (lt lt lt le ne eq)
2137 test2 (ge gt eq gt eq gt)
2139 (bit_and:c (test1 @0 @1) (test2 @0 @1))
2140 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2141 || VECTOR_INTEGER_TYPE_P (TREE_TYPE (@0)))
2142 { constant_boolean_node (false, type); })))
2144 /* -A CMP -B -> B CMP A. */
2145 (for cmp (tcc_comparison)
2146 scmp (swapped_tcc_comparison)
2148 (cmp (negate @0) (negate @1))
2149 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
2150 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2151 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
2154 (cmp (negate @0) CONSTANT_CLASS_P@1)
2155 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
2156 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2157 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
2158 (with { tree tem = const_unop (NEGATE_EXPR, TREE_TYPE (@0), @1); }
2159 (if (tem && !TREE_OVERFLOW (tem))
2160 (scmp @0 { tem; }))))))
2162 /* Convert ABS_EXPR<x> == 0 or ABS_EXPR<x> != 0 to x == 0 or x != 0. */
2165 (op (abs @0) zerop@1)
2168 /* From fold_sign_changed_comparison and fold_widened_comparison. */
2169 (for cmp (simple_comparison)
2171 (cmp (convert@0 @00) (convert?@1 @10))
2172 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2173 /* Disable this optimization if we're casting a function pointer
2174 type on targets that require function pointer canonicalization. */
2175 && !(targetm.have_canonicalize_funcptr_for_compare ()
2176 && TREE_CODE (TREE_TYPE (@00)) == POINTER_TYPE
2177 && TREE_CODE (TREE_TYPE (TREE_TYPE (@00))) == FUNCTION_TYPE)
2179 (if (TYPE_PRECISION (TREE_TYPE (@00)) == TYPE_PRECISION (TREE_TYPE (@0))
2180 && (TREE_CODE (@10) == INTEGER_CST
2181 || (@1 != @10 && types_match (TREE_TYPE (@10), TREE_TYPE (@00))))
2182 && (TYPE_UNSIGNED (TREE_TYPE (@00)) == TYPE_UNSIGNED (TREE_TYPE (@0))
2185 && (POINTER_TYPE_P (TREE_TYPE (@00)) == POINTER_TYPE_P (TREE_TYPE (@0))))
2186 /* ??? The special-casing of INTEGER_CST conversion was in the original
2187 code and here to avoid a spurious overflow flag on the resulting
2188 constant which fold_convert produces. */
2189 (if (TREE_CODE (@1) == INTEGER_CST)
2190 (cmp @00 { force_fit_type (TREE_TYPE (@00), wi::to_widest (@1), 0,
2191 TREE_OVERFLOW (@1)); })
2192 (cmp @00 (convert @1)))
2194 (if (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (TREE_TYPE (@00)))
2195 /* If possible, express the comparison in the shorter mode. */
2196 (if ((cmp == EQ_EXPR || cmp == NE_EXPR
2197 || TYPE_UNSIGNED (TREE_TYPE (@0)) == TYPE_UNSIGNED (TREE_TYPE (@00)))
2198 && (types_match (TREE_TYPE (@10), TREE_TYPE (@00))
2199 || ((TYPE_PRECISION (TREE_TYPE (@00))
2200 >= TYPE_PRECISION (TREE_TYPE (@10)))
2201 && (TYPE_UNSIGNED (TREE_TYPE (@00))
2202 == TYPE_UNSIGNED (TREE_TYPE (@10))))
2203 || (TREE_CODE (@10) == INTEGER_CST
2204 && INTEGRAL_TYPE_P (TREE_TYPE (@00))
2205 && int_fits_type_p (@10, TREE_TYPE (@00)))))
2206 (cmp @00 (convert @10))
2207 (if (TREE_CODE (@10) == INTEGER_CST
2208 && INTEGRAL_TYPE_P (TREE_TYPE (@00))
2209 && !int_fits_type_p (@10, TREE_TYPE (@00)))
2212 tree min = lower_bound_in_type (TREE_TYPE (@10), TREE_TYPE (@00));
2213 tree max = upper_bound_in_type (TREE_TYPE (@10), TREE_TYPE (@00));
2214 bool above = integer_nonzerop (const_binop (LT_EXPR, type, max, @10));
2215 bool below = integer_nonzerop (const_binop (LT_EXPR, type, @10, min));
2217 (if (above || below)
2218 (if (cmp == EQ_EXPR || cmp == NE_EXPR)
2219 { constant_boolean_node (cmp == EQ_EXPR ? false : true, type); }
2220 (if (cmp == LT_EXPR || cmp == LE_EXPR)
2221 { constant_boolean_node (above ? true : false, type); }
2222 (if (cmp == GT_EXPR || cmp == GE_EXPR)
2223 { constant_boolean_node (above ? false : true, type); }))))))))))))
2226 /* A local variable can never be pointed to by
2227 the default SSA name of an incoming parameter.
2228 SSA names are canonicalized to 2nd place. */
2230 (cmp addr@0 SSA_NAME@1)
2231 (if (SSA_NAME_IS_DEFAULT_DEF (@1)
2232 && TREE_CODE (SSA_NAME_VAR (@1)) == PARM_DECL)
2233 (with { tree base = get_base_address (TREE_OPERAND (@0, 0)); }
2234 (if (TREE_CODE (base) == VAR_DECL
2235 && auto_var_in_fn_p (base, current_function_decl))
2236 (if (cmp == NE_EXPR)
2237 { constant_boolean_node (true, type); }
2238 { constant_boolean_node (false, type); }))))))
2240 /* Equality compare simplifications from fold_binary */
2243 /* If we have (A | C) == D where C & ~D != 0, convert this into 0.
2244 Similarly for NE_EXPR. */
2246 (cmp (convert?@3 (bit_ior @0 INTEGER_CST@1)) INTEGER_CST@2)
2247 (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0))
2248 && wi::bit_and_not (@1, @2) != 0)
2249 { constant_boolean_node (cmp == NE_EXPR, type); }))
2251 /* (X ^ Y) == 0 becomes X == Y, and (X ^ Y) != 0 becomes X != Y. */
2253 (cmp (bit_xor @0 @1) integer_zerop)
2256 /* (X ^ Y) == Y becomes X == 0.
2257 Likewise (X ^ Y) == X becomes Y == 0. */
2259 (cmp:c (bit_xor:c @0 @1) @0)
2260 (cmp @1 { build_zero_cst (TREE_TYPE (@1)); }))
2262 /* (X ^ C1) op C2 can be rewritten as X op (C1 ^ C2). */
2264 (cmp (convert?@3 (bit_xor @0 INTEGER_CST@1)) INTEGER_CST@2)
2265 (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0)))
2266 (cmp @0 (bit_xor @1 (convert @2)))))
2269 (cmp (convert? addr@0) integer_zerop)
2270 (if (tree_single_nonzero_warnv_p (@0, NULL))
2271 { constant_boolean_node (cmp == NE_EXPR, type); })))
2273 /* If we have (A & C) == C where C is a power of 2, convert this into
2274 (A & C) != 0. Similarly for NE_EXPR. */
2278 (cmp (bit_and@2 @0 integer_pow2p@1) @1)
2279 (icmp @2 { build_zero_cst (TREE_TYPE (@0)); })))
2281 /* If we have (A & C) != 0 where C is the sign bit of A, convert
2282 this into A < 0. Similarly for (A & C) == 0 into A >= 0. */
2286 (cmp (bit_and (convert?@2 @0) integer_pow2p@1) integer_zerop)
2287 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2288 && (TYPE_PRECISION (TREE_TYPE (@0))
2289 == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (@0))))
2290 && element_precision (@2) >= element_precision (@0)
2291 && wi::only_sign_bit_p (@1, element_precision (@0)))
2292 (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
2293 (ncmp (convert:stype @0) { build_zero_cst (stype); })))))
2295 /* When the addresses are not directly of decls compare base and offset.
2296 This implements some remaining parts of fold_comparison address
2297 comparisons but still no complete part of it. Still it is good
2298 enough to make fold_stmt not regress when not dispatching to fold_binary. */
2299 (for cmp (simple_comparison)
2301 (cmp (convert1?@2 addr@0) (convert2? addr@1))
2304 HOST_WIDE_INT off0, off1;
2305 tree base0 = get_addr_base_and_unit_offset (TREE_OPERAND (@0, 0), &off0);
2306 tree base1 = get_addr_base_and_unit_offset (TREE_OPERAND (@1, 0), &off1);
2307 if (base0 && TREE_CODE (base0) == MEM_REF)
2309 off0 += mem_ref_offset (base0).to_short_addr ();
2310 base0 = TREE_OPERAND (base0, 0);
2312 if (base1 && TREE_CODE (base1) == MEM_REF)
2314 off1 += mem_ref_offset (base1).to_short_addr ();
2315 base1 = TREE_OPERAND (base1, 0);
2318 (if (base0 && base1)
2322 if (decl_in_symtab_p (base0)
2323 && decl_in_symtab_p (base1))
2324 equal = symtab_node::get_create (base0)
2325 ->equal_address_to (symtab_node::get_create (base1));
2326 else if ((DECL_P (base0)
2327 || TREE_CODE (base0) == SSA_NAME
2328 || TREE_CODE (base0) == STRING_CST)
2330 || TREE_CODE (base1) == SSA_NAME
2331 || TREE_CODE (base1) == STRING_CST))
2332 equal = (base0 == base1);
2335 && (cmp == EQ_EXPR || cmp == NE_EXPR
2336 /* If the offsets are equal we can ignore overflow. */
2338 || POINTER_TYPE_OVERFLOW_UNDEFINED
2339 /* Or if we compare using pointers to decls or strings. */
2340 || (POINTER_TYPE_P (TREE_TYPE (@2))
2341 && (DECL_P (base0) || TREE_CODE (base0) == STRING_CST))))
2343 (if (cmp == EQ_EXPR)
2344 { constant_boolean_node (off0 == off1, type); })
2345 (if (cmp == NE_EXPR)
2346 { constant_boolean_node (off0 != off1, type); })
2347 (if (cmp == LT_EXPR)
2348 { constant_boolean_node (off0 < off1, type); })
2349 (if (cmp == LE_EXPR)
2350 { constant_boolean_node (off0 <= off1, type); })
2351 (if (cmp == GE_EXPR)
2352 { constant_boolean_node (off0 >= off1, type); })
2353 (if (cmp == GT_EXPR)
2354 { constant_boolean_node (off0 > off1, type); }))
2356 && DECL_P (base0) && DECL_P (base1)
2357 /* If we compare this as integers require equal offset. */
2358 && (!INTEGRAL_TYPE_P (TREE_TYPE (@2))
2361 (if (cmp == EQ_EXPR)
2362 { constant_boolean_node (false, type); })
2363 (if (cmp == NE_EXPR)
2364 { constant_boolean_node (true, type); })))))))))
2366 /* Non-equality compare simplifications from fold_binary */
2367 (for cmp (lt gt le ge)
2368 /* Comparisons with the highest or lowest possible integer of
2369 the specified precision will have known values. */
2371 (cmp (convert?@2 @0) INTEGER_CST@1)
2372 (if ((INTEGRAL_TYPE_P (TREE_TYPE (@1)) || POINTER_TYPE_P (TREE_TYPE (@1)))
2373 && tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@0)))
2376 tree arg1_type = TREE_TYPE (@1);
2377 unsigned int prec = TYPE_PRECISION (arg1_type);
2378 wide_int max = wi::max_value (arg1_type);
2379 wide_int signed_max = wi::max_value (prec, SIGNED);
2380 wide_int min = wi::min_value (arg1_type);
2383 (if (wi::eq_p (@1, max))
2385 (if (cmp == GT_EXPR)
2386 { constant_boolean_node (false, type); })
2387 (if (cmp == GE_EXPR)
2389 (if (cmp == LE_EXPR)
2390 { constant_boolean_node (true, type); })
2391 (if (cmp == LT_EXPR)
2393 (if (wi::eq_p (@1, min))
2395 (if (cmp == LT_EXPR)
2396 { constant_boolean_node (false, type); })
2397 (if (cmp == LE_EXPR)
2399 (if (cmp == GE_EXPR)
2400 { constant_boolean_node (true, type); })
2401 (if (cmp == GT_EXPR)
2403 (if (wi::eq_p (@1, max - 1))
2405 (if (cmp == GT_EXPR)
2406 (eq @2 { wide_int_to_tree (TREE_TYPE (@1), wi::add (@1, 1)); }))
2407 (if (cmp == LE_EXPR)
2408 (ne @2 { wide_int_to_tree (TREE_TYPE (@1), wi::add (@1, 1)); }))))
2409 (if (wi::eq_p (@1, min + 1))
2411 (if (cmp == GE_EXPR)
2412 (ne @2 { wide_int_to_tree (TREE_TYPE (@1), wi::sub (@1, 1)); }))
2413 (if (cmp == LT_EXPR)
2414 (eq @2 { wide_int_to_tree (TREE_TYPE (@1), wi::sub (@1, 1)); }))))
2415 (if (wi::eq_p (@1, signed_max)
2416 && TYPE_UNSIGNED (arg1_type)
2417 /* We will flip the signedness of the comparison operator
2418 associated with the mode of @1, so the sign bit is
2419 specified by this mode. Check that @1 is the signed
2420 max associated with this sign bit. */
2421 && prec == GET_MODE_PRECISION (TYPE_MODE (arg1_type))
2422 /* signed_type does not work on pointer types. */
2423 && INTEGRAL_TYPE_P (arg1_type))
2424 /* The following case also applies to X < signed_max+1
2425 and X >= signed_max+1 because previous transformations. */
2426 (if (cmp == LE_EXPR || cmp == GT_EXPR)
2427 (with { tree st = signed_type_for (arg1_type); }
2428 (if (cmp == LE_EXPR)
2429 (ge (convert:st @0) { build_zero_cst (st); })
2430 (lt (convert:st @0) { build_zero_cst (st); }))))))))))
2432 (for cmp (unordered ordered unlt unle ungt unge uneq ltgt)
2433 /* If the second operand is NaN, the result is constant. */
2436 (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
2437 && (cmp != LTGT_EXPR || ! flag_trapping_math))
2438 { constant_boolean_node (cmp == ORDERED_EXPR || cmp == LTGT_EXPR
2439 ? false : true, type); })))
2441 /* bool_var != 0 becomes bool_var. */
2443 (ne @0 integer_zerop)
2444 (if (TREE_CODE (TREE_TYPE (@0)) == BOOLEAN_TYPE
2445 && types_match (type, TREE_TYPE (@0)))
2447 /* bool_var == 1 becomes bool_var. */
2449 (eq @0 integer_onep)
2450 (if (TREE_CODE (TREE_TYPE (@0)) == BOOLEAN_TYPE
2451 && types_match (type, TREE_TYPE (@0)))
2454 bool_var == 0 becomes !bool_var or
2455 bool_var != 1 becomes !bool_var
2456 here because that only is good in assignment context as long
2457 as we require a tcc_comparison in GIMPLE_CONDs where we'd
2458 replace if (x == 0) with tem = ~x; if (tem != 0) which is
2459 clearly less optimal and which we'll transform again in forwprop. */
2462 /* Simplification of math builtins. These rules must all be optimizations
2463 as well as IL simplifications. If there is a possibility that the new
2464 form could be a pessimization, the rule should go in the canonicalization
2465 section that follows this one.
2467 Rules can generally go in this section if they satisfy one of
2470 - the rule describes an identity
2472 - the rule replaces calls with something as simple as addition or
2475 - the rule contains unary calls only and simplifies the surrounding
2476 arithmetic. (The idea here is to exclude non-unary calls in which
2477 one operand is constant and in which the call is known to be cheap
2478 when the operand has that value.) */
2480 (if (flag_unsafe_math_optimizations)
2481 /* Simplify sqrt(x) * sqrt(x) -> x. */
2483 (mult (SQRT@1 @0) @1)
2484 (if (!HONOR_SNANS (type))
2487 /* Simplify sqrt(x) * sqrt(y) -> sqrt(x*y). */
2488 (for root (SQRT CBRT)
2490 (mult (root:s @0) (root:s @1))
2491 (root (mult @0 @1))))
2493 /* Simplify expN(x) * expN(y) -> expN(x+y). */
2494 (for exps (EXP EXP2 EXP10 POW10)
2496 (mult (exps:s @0) (exps:s @1))
2497 (exps (plus @0 @1))))
2499 /* Simplify a/root(b/c) into a*root(c/b). */
2500 (for root (SQRT CBRT)
2502 (rdiv @0 (root:s (rdiv:s @1 @2)))
2503 (mult @0 (root (rdiv @2 @1)))))
2505 /* Simplify x/expN(y) into x*expN(-y). */
2506 (for exps (EXP EXP2 EXP10 POW10)
2508 (rdiv @0 (exps:s @1))
2509 (mult @0 (exps (negate @1)))))
2511 (for logs (LOG LOG2 LOG10 LOG10)
2512 exps (EXP EXP2 EXP10 POW10)
2513 /* logN(expN(x)) -> x. */
2517 /* expN(logN(x)) -> x. */
2522 /* Optimize logN(func()) for various exponential functions. We
2523 want to determine the value "x" and the power "exponent" in
2524 order to transform logN(x**exponent) into exponent*logN(x). */
2525 (for logs (LOG LOG LOG LOG2 LOG2 LOG2 LOG10 LOG10)
2526 exps (EXP2 EXP10 POW10 EXP EXP10 POW10 EXP EXP2)
2529 (if (SCALAR_FLOAT_TYPE_P (type))
2535 /* Prepare to do logN(exp(exponent)) -> exponent*logN(e). */
2536 x = build_real_truncate (type, dconst_e ());
2539 /* Prepare to do logN(exp2(exponent)) -> exponent*logN(2). */
2540 x = build_real (type, dconst2);
2544 /* Prepare to do logN(exp10(exponent)) -> exponent*logN(10). */
2546 REAL_VALUE_TYPE dconst10;
2547 real_from_integer (&dconst10, VOIDmode, 10, SIGNED);
2548 x = build_real (type, dconst10);
2555 (mult (logs { x; }) @0)))))
2563 (if (SCALAR_FLOAT_TYPE_P (type))
2569 /* Prepare to do logN(sqrt(x)) -> 0.5*logN(x). */
2570 x = build_real (type, dconsthalf);
2573 /* Prepare to do logN(cbrt(x)) -> (1/3)*logN(x). */
2574 x = build_real_truncate (type, dconst_third ());
2580 (mult { x; } (logs @0))))))
2582 /* logN(pow(x,exponent)) -> exponent*logN(x). */
2583 (for logs (LOG LOG2 LOG10)
2587 (mult @1 (logs @0))))
2592 exps (EXP EXP2 EXP10 POW10)
2593 /* sqrt(expN(x)) -> expN(x*0.5). */
2596 (exps (mult @0 { build_real (type, dconsthalf); })))
2597 /* cbrt(expN(x)) -> expN(x/3). */
2600 (exps (mult @0 { build_real_truncate (type, dconst_third ()); })))
2601 /* pow(expN(x), y) -> expN(x*y). */
2604 (exps (mult @0 @1))))
2606 /* tan(atan(x)) -> x. */
2613 /* cabs(x+0i) or cabs(0+xi) -> abs(x). */
2615 (CABS (complex:c @0 real_zerop@1))
2618 /* trunc(trunc(x)) -> trunc(x), etc. */
2619 (for fns (TRUNC FLOOR CEIL ROUND NEARBYINT RINT)
2623 /* f(x) -> x if x is integer valued and f does nothing for such values. */
2624 (for fns (TRUNC FLOOR CEIL ROUND NEARBYINT RINT)
2626 (fns integer_valued_real_p@0)
2629 /* hypot(x,0) and hypot(0,x) -> abs(x). */
2631 (HYPOT:c @0 real_zerop@1)
2634 /* pow(1,x) -> 1. */
2636 (POW real_onep@0 @1)
2640 /* copysign(x,x) -> x. */
2645 /* copysign(x,y) -> fabs(x) if y is nonnegative. */
2646 (COPYSIGN @0 tree_expr_nonnegative_p@1)
2649 (for scale (LDEXP SCALBN SCALBLN)
2650 /* ldexp(0, x) -> 0. */
2652 (scale real_zerop@0 @1)
2654 /* ldexp(x, 0) -> x. */
2656 (scale @0 integer_zerop@1)
2658 /* ldexp(x, y) -> x if x is +-Inf or NaN. */
2660 (scale REAL_CST@0 @1)
2661 (if (!real_isfinite (TREE_REAL_CST_PTR (@0)))
2664 /* Canonicalization of sequences of math builtins. These rules represent
2665 IL simplifications but are not necessarily optimizations.
2667 The sincos pass is responsible for picking "optimal" implementations
2668 of math builtins, which may be more complicated and can sometimes go
2669 the other way, e.g. converting pow into a sequence of sqrts.
2670 We only want to do these canonicalizations before the pass has run. */
2672 (if (flag_unsafe_math_optimizations && canonicalize_math_p ())
2673 /* Simplify tan(x) * cos(x) -> sin(x). */
2675 (mult:c (TAN:s @0) (COS:s @0))
2678 /* Simplify x * pow(x,c) -> pow(x,c+1). */
2680 (mult @0 (POW:s @0 REAL_CST@1))
2681 (if (!TREE_OVERFLOW (@1))
2682 (POW @0 (plus @1 { build_one_cst (type); }))))
2684 /* Simplify sin(x) / cos(x) -> tan(x). */
2686 (rdiv (SIN:s @0) (COS:s @0))
2689 /* Simplify cos(x) / sin(x) -> 1 / tan(x). */
2691 (rdiv (COS:s @0) (SIN:s @0))
2692 (rdiv { build_one_cst (type); } (TAN @0)))
2694 /* Simplify sin(x) / tan(x) -> cos(x). */
2696 (rdiv (SIN:s @0) (TAN:s @0))
2697 (if (! HONOR_NANS (@0)
2698 && ! HONOR_INFINITIES (@0))
2701 /* Simplify tan(x) / sin(x) -> 1.0 / cos(x). */
2703 (rdiv (TAN:s @0) (SIN:s @0))
2704 (if (! HONOR_NANS (@0)
2705 && ! HONOR_INFINITIES (@0))
2706 (rdiv { build_one_cst (type); } (COS @0))))
2708 /* Simplify pow(x,y) * pow(x,z) -> pow(x,y+z). */
2710 (mult (POW:s @0 @1) (POW:s @0 @2))
2711 (POW @0 (plus @1 @2)))
2713 /* Simplify pow(x,y) * pow(z,y) -> pow(x*z,y). */
2715 (mult (POW:s @0 @1) (POW:s @2 @1))
2716 (POW (mult @0 @2) @1))
2718 /* Simplify pow(x,c) / x -> pow(x,c-1). */
2720 (rdiv (POW:s @0 REAL_CST@1) @0)
2721 (if (!TREE_OVERFLOW (@1))
2722 (POW @0 (minus @1 { build_one_cst (type); }))))
2724 /* Simplify x / pow (y,z) -> x * pow(y,-z). */
2726 (rdiv @0 (POW:s @1 @2))
2727 (mult @0 (POW @1 (negate @2))))
2732 /* sqrt(sqrt(x)) -> pow(x,1/4). */
2735 (pows @0 { build_real (type, dconst_quarter ()); }))
2736 /* sqrt(cbrt(x)) -> pow(x,1/6). */
2739 (pows @0 { build_real_truncate (type, dconst_sixth ()); }))
2740 /* cbrt(sqrt(x)) -> pow(x,1/6). */
2743 (pows @0 { build_real_truncate (type, dconst_sixth ()); }))
2744 /* cbrt(cbrt(x)) -> pow(x,1/9), iff x is nonnegative. */
2746 (cbrts (cbrts tree_expr_nonnegative_p@0))
2747 (pows @0 { build_real_truncate (type, dconst_ninth ()); }))
2748 /* sqrt(pow(x,y)) -> pow(|x|,y*0.5). */
2750 (sqrts (pows @0 @1))
2751 (pows (abs @0) (mult @1 { build_real (type, dconsthalf); })))
2752 /* cbrt(pow(x,y)) -> pow(x,y/3), iff x is nonnegative. */
2754 (cbrts (pows tree_expr_nonnegative_p@0 @1))
2755 (pows @0 (mult @1 { build_real_truncate (type, dconst_third ()); })))
2756 /* pow(sqrt(x),y) -> pow(x,y*0.5). */
2758 (pows (sqrts @0) @1)
2759 (pows @0 (mult @1 { build_real (type, dconsthalf); })))
2760 /* pow(cbrt(x),y) -> pow(x,y/3) iff x is nonnegative. */
2762 (pows (cbrts tree_expr_nonnegative_p@0) @1)
2763 (pows @0 (mult @1 { build_real_truncate (type, dconst_third ()); })))
2764 /* pow(pow(x,y),z) -> pow(x,y*z) iff x is nonnegative. */
2766 (pows (pows tree_expr_nonnegative_p@0 @1) @2)
2767 (pows @0 (mult @1 @2))))
2769 /* cabs(x+xi) -> fabs(x)*sqrt(2). */
2771 (CABS (complex @0 @0))
2772 (mult (abs @0) { build_real_truncate (type, dconst_sqrt2 ()); }))
2774 /* hypot(x,x) -> fabs(x)*sqrt(2). */
2777 (mult (abs @0) { build_real_truncate (type, dconst_sqrt2 ()); }))
2779 /* cexp(x+yi) -> exp(x)*cexpi(y). */
2784 (cexps compositional_complex@0)
2785 (if (targetm.libc_has_function (function_c99_math_complex))
2787 (mult (exps@1 (realpart @0)) (realpart (cexpis:type@2 (imagpart @0))))
2788 (mult @1 (imagpart @2)))))))
2790 (if (canonicalize_math_p ())
2791 /* floor(x) -> trunc(x) if x is nonnegative. */
2795 (floors tree_expr_nonnegative_p@0)
2798 (match double_value_p
2800 (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == double_type_node)))
2801 (for froms (BUILT_IN_TRUNCL
2813 /* truncl(extend(x)) -> extend(trunc(x)), etc., if x is a double. */
2814 (if (optimize && canonicalize_math_p ())
2816 (froms (convert double_value_p@0))
2817 (convert (tos @0)))))
2819 (match float_value_p
2821 (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == float_type_node)))
2822 (for froms (BUILT_IN_TRUNCL BUILT_IN_TRUNC
2823 BUILT_IN_FLOORL BUILT_IN_FLOOR
2824 BUILT_IN_CEILL BUILT_IN_CEIL
2825 BUILT_IN_ROUNDL BUILT_IN_ROUND
2826 BUILT_IN_NEARBYINTL BUILT_IN_NEARBYINT
2827 BUILT_IN_RINTL BUILT_IN_RINT)
2828 tos (BUILT_IN_TRUNCF BUILT_IN_TRUNCF
2829 BUILT_IN_FLOORF BUILT_IN_FLOORF
2830 BUILT_IN_CEILF BUILT_IN_CEILF
2831 BUILT_IN_ROUNDF BUILT_IN_ROUNDF
2832 BUILT_IN_NEARBYINTF BUILT_IN_NEARBYINTF
2833 BUILT_IN_RINTF BUILT_IN_RINTF)
2834 /* truncl(extend(x)) and trunc(extend(x)) -> extend(truncf(x)), etc.,
2836 (if (optimize && canonicalize_math_p ()
2837 && targetm.libc_has_function (function_c99_misc))
2839 (froms (convert float_value_p@0))
2840 (convert (tos @0)))))
2842 (for froms (XFLOORL XCEILL XROUNDL XRINTL)
2843 tos (XFLOOR XCEIL XROUND XRINT)
2844 /* llfloorl(extend(x)) -> llfloor(x), etc., if x is a double. */
2845 (if (optimize && canonicalize_math_p ())
2847 (froms (convert double_value_p@0))
2850 (for froms (XFLOORL XCEILL XROUNDL XRINTL
2851 XFLOOR XCEIL XROUND XRINT)
2852 tos (XFLOORF XCEILF XROUNDF XRINTF)
2853 /* llfloorl(extend(x)) and llfloor(extend(x)) -> llfloorf(x), etc.,
2855 (if (optimize && canonicalize_math_p ())
2857 (froms (convert float_value_p@0))
2860 (if (canonicalize_math_p ())
2861 /* xfloor(x) -> fix_trunc(x) if x is nonnegative. */
2862 (for floors (IFLOOR LFLOOR LLFLOOR)
2864 (floors tree_expr_nonnegative_p@0)
2867 (if (canonicalize_math_p ())
2868 /* xfloor(x) -> fix_trunc(x), etc., if x is integer valued. */
2869 (for fns (IFLOOR LFLOOR LLFLOOR
2871 IROUND LROUND LLROUND)
2873 (fns integer_valued_real_p@0)
2875 (if (!flag_errno_math)
2876 /* xrint(x) -> fix_trunc(x), etc., if x is integer valued. */
2877 (for rints (IRINT LRINT LLRINT)
2879 (rints integer_valued_real_p@0)
2882 (if (canonicalize_math_p ())
2883 (for ifn (IFLOOR ICEIL IROUND IRINT)
2884 lfn (LFLOOR LCEIL LROUND LRINT)
2885 llfn (LLFLOOR LLCEIL LLROUND LLRINT)
2886 /* Canonicalize iround (x) to lround (x) on ILP32 targets where
2887 sizeof (int) == sizeof (long). */
2888 (if (TYPE_PRECISION (integer_type_node)
2889 == TYPE_PRECISION (long_integer_type_node))
2892 (lfn:long_integer_type_node @0)))
2893 /* Canonicalize llround (x) to lround (x) on LP64 targets where
2894 sizeof (long long) == sizeof (long). */
2895 (if (TYPE_PRECISION (long_long_integer_type_node)
2896 == TYPE_PRECISION (long_integer_type_node))
2899 (lfn:long_integer_type_node @0)))))
2901 /* cproj(x) -> x if we're ignoring infinities. */
2904 (if (!HONOR_INFINITIES (type))
2907 /* If the real part is inf and the imag part is known to be
2908 nonnegative, return (inf + 0i). */
2910 (CPROJ (complex REAL_CST@0 tree_expr_nonnegative_p@1))
2911 (if (real_isinf (TREE_REAL_CST_PTR (@0)))
2912 { build_complex_inf (type, false); }))
2914 /* If the imag part is inf, return (inf+I*copysign(0,imag)). */
2916 (CPROJ (complex @0 REAL_CST@1))
2917 (if (real_isinf (TREE_REAL_CST_PTR (@1)))
2918 { build_complex_inf (type, TREE_REAL_CST_PTR (@1)->sign); }))
2924 (pows @0 REAL_CST@1)
2926 const REAL_VALUE_TYPE *value = TREE_REAL_CST_PTR (@1);
2927 REAL_VALUE_TYPE tmp;
2930 /* pow(x,0) -> 1. */
2931 (if (real_equal (value, &dconst0))
2932 { build_real (type, dconst1); })
2933 /* pow(x,1) -> x. */
2934 (if (real_equal (value, &dconst1))
2936 /* pow(x,-1) -> 1/x. */
2937 (if (real_equal (value, &dconstm1))
2938 (rdiv { build_real (type, dconst1); } @0))
2939 /* pow(x,0.5) -> sqrt(x). */
2940 (if (flag_unsafe_math_optimizations
2941 && canonicalize_math_p ()
2942 && real_equal (value, &dconsthalf))
2944 /* pow(x,1/3) -> cbrt(x). */
2945 (if (flag_unsafe_math_optimizations
2946 && canonicalize_math_p ()
2947 && (tmp = real_value_truncate (TYPE_MODE (type), dconst_third ()),
2948 real_equal (value, &tmp)))
2951 /* powi(1,x) -> 1. */
2953 (POWI real_onep@0 @1)
2957 (POWI @0 INTEGER_CST@1)
2959 /* powi(x,0) -> 1. */
2960 (if (wi::eq_p (@1, 0))
2961 { build_real (type, dconst1); })
2962 /* powi(x,1) -> x. */
2963 (if (wi::eq_p (@1, 1))
2965 /* powi(x,-1) -> 1/x. */
2966 (if (wi::eq_p (@1, -1))
2967 (rdiv { build_real (type, dconst1); } @0))))
2969 /* Narrowing of arithmetic and logical operations.
2971 These are conceptually similar to the transformations performed for
2972 the C/C++ front-ends by shorten_binary_op and shorten_compare. Long
2973 term we want to move all that code out of the front-ends into here. */
2975 /* If we have a narrowing conversion of an arithmetic operation where
2976 both operands are widening conversions from the same type as the outer
2977 narrowing conversion. Then convert the innermost operands to a suitable
2978 unsigned type (to avoid introducing undefined behavior), perform the
2979 operation and convert the result to the desired type. */
2980 (for op (plus minus)
2982 (convert (op:s (convert@2 @0) (convert@3 @1)))
2983 (if (INTEGRAL_TYPE_P (type)
2984 /* We check for type compatibility between @0 and @1 below,
2985 so there's no need to check that @1/@3 are integral types. */
2986 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
2987 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
2988 /* The precision of the type of each operand must match the
2989 precision of the mode of each operand, similarly for the
2991 && (TYPE_PRECISION (TREE_TYPE (@0))
2992 == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (@0))))
2993 && (TYPE_PRECISION (TREE_TYPE (@1))
2994 == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (@1))))
2995 && TYPE_PRECISION (type) == GET_MODE_PRECISION (TYPE_MODE (type))
2996 /* The inner conversion must be a widening conversion. */
2997 && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
2998 && types_match (@0, @1)
2999 && types_match (@0, type))
3000 (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
3001 (convert (op @0 @1))
3002 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
3003 (convert (op (convert:utype @0) (convert:utype @1))))))))
3005 /* This is another case of narrowing, specifically when there's an outer
3006 BIT_AND_EXPR which masks off bits outside the type of the innermost
3007 operands. Like the previous case we have to convert the operands
3008 to unsigned types to avoid introducing undefined behavior for the
3009 arithmetic operation. */
3010 (for op (minus plus)
3012 (bit_and (op:s (convert@2 @0) (convert@3 @1)) INTEGER_CST@4)
3013 (if (INTEGRAL_TYPE_P (type)
3014 /* We check for type compatibility between @0 and @1 below,
3015 so there's no need to check that @1/@3 are integral types. */
3016 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
3017 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
3018 /* The precision of the type of each operand must match the
3019 precision of the mode of each operand, similarly for the
3021 && (TYPE_PRECISION (TREE_TYPE (@0))
3022 == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (@0))))
3023 && (TYPE_PRECISION (TREE_TYPE (@1))
3024 == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (@1))))
3025 && TYPE_PRECISION (type) == GET_MODE_PRECISION (TYPE_MODE (type))
3026 /* The inner conversion must be a widening conversion. */
3027 && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
3028 && types_match (@0, @1)
3029 && (tree_int_cst_min_precision (@4, TYPE_SIGN (TREE_TYPE (@0)))
3030 <= TYPE_PRECISION (TREE_TYPE (@0)))
3031 && (wi::bit_and (@4, wi::mask (TYPE_PRECISION (TREE_TYPE (@0)),
3032 true, TYPE_PRECISION (type))) == 0))
3033 (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
3034 (with { tree ntype = TREE_TYPE (@0); }
3035 (convert (bit_and (op @0 @1) (convert:ntype @4))))
3036 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
3037 (convert (bit_and (op (convert:utype @0) (convert:utype @1))
3038 (convert:utype @4))))))))
3040 /* Transform (@0 < @1 and @0 < @2) to use min,
3041 (@0 > @1 and @0 > @2) to use max */
3042 (for op (lt le gt ge)
3043 ext (min min max max)
3045 (bit_and (op:s @0 @1) (op:s @0 @2))
3046 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)))
3047 (op @0 (ext @1 @2)))))
3050 /* signbit(x) -> 0 if x is nonnegative. */
3051 (SIGNBIT tree_expr_nonnegative_p@0)
3052 { integer_zero_node; })
3055 /* signbit(x) -> x<0 if x doesn't have signed zeros. */
3057 (if (!HONOR_SIGNED_ZEROS (@0))
3058 (convert (lt @0 { build_real (TREE_TYPE (@0), dconst0); }))))