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-2017 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
40 (define_operator_list tcc_comparison
41 lt le eq ne ge gt unordered ordered unlt unle ungt unge uneq ltgt)
42 (define_operator_list inverted_tcc_comparison
43 ge gt ne eq lt le ordered unordered ge gt le lt ltgt uneq)
44 (define_operator_list inverted_tcc_comparison_with_nans
45 unge ungt ne eq unlt unle ordered unordered ge gt le lt ltgt uneq)
46 (define_operator_list swapped_tcc_comparison
47 gt ge eq ne le lt unordered ordered ungt unge unlt unle uneq ltgt)
48 (define_operator_list simple_comparison lt le eq ne ge gt)
49 (define_operator_list swapped_simple_comparison gt ge eq ne le lt)
51 #include "cfn-operators.pd"
53 /* Define operand lists for math rounding functions {,i,l,ll}FN,
54 where the versions prefixed with "i" return an int, those prefixed with
55 "l" return a long and those prefixed with "ll" return a long long.
57 Also define operand lists:
59 X<FN>F for all float functions, in the order i, l, ll
60 X<FN> for all double functions, in the same order
61 X<FN>L for all long double functions, in the same order. */
62 #define DEFINE_INT_AND_FLOAT_ROUND_FN(FN) \
63 (define_operator_list X##FN##F BUILT_IN_I##FN##F \
66 (define_operator_list X##FN BUILT_IN_I##FN \
69 (define_operator_list X##FN##L BUILT_IN_I##FN##L \
73 DEFINE_INT_AND_FLOAT_ROUND_FN (FLOOR)
74 DEFINE_INT_AND_FLOAT_ROUND_FN (CEIL)
75 DEFINE_INT_AND_FLOAT_ROUND_FN (ROUND)
76 DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
78 /* Simplifications of operations with one constant operand and
79 simplifications to constants or single values. */
81 (for op (plus pointer_plus minus bit_ior bit_xor)
86 /* 0 +p index -> (type)index */
88 (pointer_plus integer_zerop @1)
89 (non_lvalue (convert @1)))
91 /* See if ARG1 is zero and X + ARG1 reduces to X.
92 Likewise if the operands are reversed. */
94 (plus:c @0 real_zerop@1)
95 (if (fold_real_zero_addition_p (type, @1, 0))
98 /* See if ARG1 is zero and X - ARG1 reduces to X. */
100 (minus @0 real_zerop@1)
101 (if (fold_real_zero_addition_p (type, @1, 1))
105 This is unsafe for certain floats even in non-IEEE formats.
106 In IEEE, it is unsafe because it does wrong for NaNs.
107 Also note that operand_equal_p is always false if an operand
111 (if (!FLOAT_TYPE_P (type) || !HONOR_NANS (type))
112 { build_zero_cst (type); }))
115 (mult @0 integer_zerop@1)
118 /* Maybe fold x * 0 to 0. The expressions aren't the same
119 when x is NaN, since x * 0 is also NaN. Nor are they the
120 same in modes with signed zeros, since multiplying a
121 negative value by 0 gives -0, not +0. */
123 (mult @0 real_zerop@1)
124 (if (!HONOR_NANS (type) && !HONOR_SIGNED_ZEROS (type))
127 /* In IEEE floating point, x*1 is not equivalent to x for snans.
128 Likewise for complex arithmetic with signed zeros. */
131 (if (!HONOR_SNANS (type)
132 && (!HONOR_SIGNED_ZEROS (type)
133 || !COMPLEX_FLOAT_TYPE_P (type)))
136 /* Transform x * -1.0 into -x. */
138 (mult @0 real_minus_onep)
139 (if (!HONOR_SNANS (type)
140 && (!HONOR_SIGNED_ZEROS (type)
141 || !COMPLEX_FLOAT_TYPE_P (type)))
144 /* X * 1, X / 1 -> X. */
145 (for op (mult trunc_div ceil_div floor_div round_div exact_div)
150 /* Preserve explicit divisions by 0: the C++ front-end wants to detect
151 undefined behavior in constexpr evaluation, and assuming that the division
152 traps enables better optimizations than these anyway. */
153 (for div (trunc_div ceil_div floor_div round_div exact_div)
154 /* 0 / X is always zero. */
156 (div integer_zerop@0 @1)
157 /* But not for 0 / 0 so that we can get the proper warnings and errors. */
158 (if (!integer_zerop (@1))
162 (div @0 integer_minus_onep@1)
163 (if (!TYPE_UNSIGNED (type))
168 /* But not for 0 / 0 so that we can get the proper warnings and errors. */
169 (if (!integer_zerop (@0))
170 { build_one_cst (type); }))
171 /* X / abs (X) is X < 0 ? -1 : 1. */
174 (if (INTEGRAL_TYPE_P (type)
175 && TYPE_OVERFLOW_UNDEFINED (type))
176 (cond (lt @0 { build_zero_cst (type); })
177 { build_minus_one_cst (type); } { build_one_cst (type); })))
180 (div:C @0 (negate @0))
181 (if ((INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
182 && TYPE_OVERFLOW_UNDEFINED (type))
183 { build_minus_one_cst (type); })))
185 /* For unsigned integral types, FLOOR_DIV_EXPR is the same as
186 TRUNC_DIV_EXPR. Rewrite into the latter in this case. */
189 (if ((INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
190 && TYPE_UNSIGNED (type))
193 /* Combine two successive divisions. Note that combining ceil_div
194 and floor_div is trickier and combining round_div even more so. */
195 (for div (trunc_div exact_div)
197 (div (div @0 INTEGER_CST@1) INTEGER_CST@2)
200 wide_int mul = wi::mul (@1, @2, TYPE_SIGN (type), &overflow_p);
203 (div @0 { wide_int_to_tree (type, mul); })
204 (if (TYPE_UNSIGNED (type)
205 || mul != wi::min_value (TYPE_PRECISION (type), SIGNED))
206 { build_zero_cst (type); })))))
208 /* Optimize A / A to 1.0 if we don't care about
209 NaNs or Infinities. */
212 (if (FLOAT_TYPE_P (type)
213 && ! HONOR_NANS (type)
214 && ! HONOR_INFINITIES (type))
215 { build_one_cst (type); }))
217 /* Optimize -A / A to -1.0 if we don't care about
218 NaNs or Infinities. */
220 (rdiv:C @0 (negate @0))
221 (if (FLOAT_TYPE_P (type)
222 && ! HONOR_NANS (type)
223 && ! HONOR_INFINITIES (type))
224 { build_minus_one_cst (type); }))
226 /* PR71078: x / abs(x) -> copysign (1.0, x) */
228 (rdiv:C (convert? @0) (convert? (abs @0)))
229 (if (SCALAR_FLOAT_TYPE_P (type)
230 && ! HONOR_NANS (type)
231 && ! HONOR_INFINITIES (type))
233 (if (types_match (type, float_type_node))
234 (BUILT_IN_COPYSIGNF { build_one_cst (type); } (convert @0)))
235 (if (types_match (type, double_type_node))
236 (BUILT_IN_COPYSIGN { build_one_cst (type); } (convert @0)))
237 (if (types_match (type, long_double_type_node))
238 (BUILT_IN_COPYSIGNL { build_one_cst (type); } (convert @0))))))
240 /* In IEEE floating point, x/1 is not equivalent to x for snans. */
243 (if (!HONOR_SNANS (type))
246 /* In IEEE floating point, x/-1 is not equivalent to -x for snans. */
248 (rdiv @0 real_minus_onep)
249 (if (!HONOR_SNANS (type))
252 (if (flag_reciprocal_math)
253 /* Convert (A/B)/C to A/(B*C) */
255 (rdiv (rdiv:s @0 @1) @2)
256 (rdiv @0 (mult @1 @2)))
258 /* Convert A/(B/C) to (A/B)*C */
260 (rdiv @0 (rdiv:s @1 @2))
261 (mult (rdiv @0 @1) @2)))
263 /* Optimize (X & (-A)) / A where A is a power of 2, to X >> log2(A) */
264 (for div (trunc_div ceil_div floor_div round_div exact_div)
266 (div (convert? (bit_and @0 INTEGER_CST@1)) INTEGER_CST@2)
267 (if (integer_pow2p (@2)
268 && tree_int_cst_sgn (@2) > 0
269 && wi::add (@2, @1) == 0
270 && tree_nop_conversion_p (type, TREE_TYPE (@0)))
271 (rshift (convert @0) { build_int_cst (integer_type_node,
272 wi::exact_log2 (@2)); }))))
274 /* If ARG1 is a constant, we can convert this to a multiply by the
275 reciprocal. This does not have the same rounding properties,
276 so only do this if -freciprocal-math. We can actually
277 always safely do it if ARG1 is a power of two, but it's hard to
278 tell if it is or not in a portable manner. */
279 (for cst (REAL_CST COMPLEX_CST VECTOR_CST)
283 (if (flag_reciprocal_math
286 { tree tem = const_binop (RDIV_EXPR, type, build_one_cst (type), @1); }
288 (mult @0 { tem; } )))
289 (if (cst != COMPLEX_CST)
290 (with { tree inverse = exact_inverse (type, @1); }
292 (mult @0 { inverse; } ))))))))
294 (for mod (ceil_mod floor_mod round_mod trunc_mod)
295 /* 0 % X is always zero. */
297 (mod integer_zerop@0 @1)
298 /* But not for 0 % 0 so that we can get the proper warnings and errors. */
299 (if (!integer_zerop (@1))
301 /* X % 1 is always zero. */
303 (mod @0 integer_onep)
304 { build_zero_cst (type); })
305 /* X % -1 is zero. */
307 (mod @0 integer_minus_onep@1)
308 (if (!TYPE_UNSIGNED (type))
309 { build_zero_cst (type); }))
313 /* But not for 0 % 0 so that we can get the proper warnings and errors. */
314 (if (!integer_zerop (@0))
315 { build_zero_cst (type); }))
316 /* (X % Y) % Y is just X % Y. */
318 (mod (mod@2 @0 @1) @1)
320 /* From extract_muldiv_1: (X * C1) % C2 is zero if C1 is a multiple of C2. */
322 (mod (mult @0 INTEGER_CST@1) INTEGER_CST@2)
323 (if (ANY_INTEGRAL_TYPE_P (type)
324 && TYPE_OVERFLOW_UNDEFINED (type)
325 && wi::multiple_of_p (@1, @2, TYPE_SIGN (type)))
326 { build_zero_cst (type); })))
328 /* X % -C is the same as X % C. */
330 (trunc_mod @0 INTEGER_CST@1)
331 (if (TYPE_SIGN (type) == SIGNED
332 && !TREE_OVERFLOW (@1)
334 && !TYPE_OVERFLOW_TRAPS (type)
335 /* Avoid this transformation if C is INT_MIN, i.e. C == -C. */
336 && !sign_bit_p (@1, @1))
337 (trunc_mod @0 (negate @1))))
339 /* X % -Y is the same as X % Y. */
341 (trunc_mod @0 (convert? (negate @1)))
342 (if (INTEGRAL_TYPE_P (type)
343 && !TYPE_UNSIGNED (type)
344 && !TYPE_OVERFLOW_TRAPS (type)
345 && tree_nop_conversion_p (type, TREE_TYPE (@1))
346 /* Avoid this transformation if X might be INT_MIN or
347 Y might be -1, because we would then change valid
348 INT_MIN % -(-1) into invalid INT_MIN % -1. */
349 && (expr_not_equal_to (@0, TYPE_MIN_VALUE (type))
350 || expr_not_equal_to (@1, wi::minus_one (TYPE_PRECISION
352 (trunc_mod @0 (convert @1))))
354 /* X - (X / Y) * Y is the same as X % Y. */
356 (minus (convert1? @0) (convert2? (mult:c (trunc_div @@0 @@1) @1)))
357 (if (INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
358 (convert (trunc_mod @0 @1))))
360 /* Optimize TRUNC_MOD_EXPR by a power of two into a BIT_AND_EXPR,
361 i.e. "X % C" into "X & (C - 1)", if X and C are positive.
362 Also optimize A % (C << N) where C is a power of 2,
363 to A & ((C << N) - 1). */
364 (match (power_of_two_cand @1)
366 (match (power_of_two_cand @1)
367 (lshift INTEGER_CST@1 @2))
368 (for mod (trunc_mod floor_mod)
370 (mod @0 (convert?@3 (power_of_two_cand@1 @2)))
371 (if ((TYPE_UNSIGNED (type)
372 || tree_expr_nonnegative_p (@0))
373 && tree_nop_conversion_p (type, TREE_TYPE (@3))
374 && integer_pow2p (@2) && tree_int_cst_sgn (@2) > 0)
375 (bit_and @0 (convert (minus @1 { build_int_cst (TREE_TYPE (@1), 1); }))))))
377 /* Simplify (unsigned t * 2)/2 -> unsigned t & 0x7FFFFFFF. */
379 (trunc_div (mult @0 integer_pow2p@1) @1)
380 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
381 (bit_and @0 { wide_int_to_tree
382 (type, wi::mask (TYPE_PRECISION (type) - wi::exact_log2 (@1),
383 false, TYPE_PRECISION (type))); })))
385 /* Simplify (unsigned t / 2) * 2 -> unsigned t & ~1. */
387 (mult (trunc_div @0 integer_pow2p@1) @1)
388 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
389 (bit_and @0 (negate @1))))
391 /* Simplify (t * 2) / 2) -> t. */
392 (for div (trunc_div ceil_div floor_div round_div exact_div)
394 (div (mult @0 @1) @1)
395 (if (ANY_INTEGRAL_TYPE_P (type)
396 && TYPE_OVERFLOW_UNDEFINED (type))
400 /* Simplify cos(-x) and cos(|x|) -> cos(x). Similarly for cosh. */
405 /* Simplify pow(-x, y) and pow(|x|,y) -> pow(x,y) if y is an even integer. */
408 (pows (op @0) REAL_CST@1)
409 (with { HOST_WIDE_INT n; }
410 (if (real_isinteger (&TREE_REAL_CST (@1), &n) && (n & 1) == 0)
412 /* Likewise for powi. */
415 (pows (op @0) INTEGER_CST@1)
416 (if (wi::bit_and (@1, 1) == 0)
418 /* Strip negate and abs from both operands of hypot. */
426 /* copysign(-x, y) and copysign(abs(x), y) -> copysign(x, y). */
427 (for copysigns (COPYSIGN)
429 (copysigns (op @0) @1)
432 /* abs(x)*abs(x) -> x*x. Should be valid for all types. */
437 /* cos(copysign(x, y)) -> cos(x). Similarly for cosh. */
441 (coss (copysigns @0 @1))
444 /* pow(copysign(x, y), z) -> pow(x, z) if z is an even integer. */
448 (pows (copysigns @0 @2) REAL_CST@1)
449 (with { HOST_WIDE_INT n; }
450 (if (real_isinteger (&TREE_REAL_CST (@1), &n) && (n & 1) == 0)
452 /* Likewise for powi. */
456 (pows (copysigns @0 @2) INTEGER_CST@1)
457 (if (wi::bit_and (@1, 1) == 0)
462 /* hypot(copysign(x, y), z) -> hypot(x, z). */
464 (hypots (copysigns @0 @1) @2)
466 /* hypot(x, copysign(y, z)) -> hypot(x, y). */
468 (hypots @0 (copysigns @1 @2))
471 /* copysign(x, CST) -> [-]abs (x). */
472 (for copysigns (COPYSIGN)
474 (copysigns @0 REAL_CST@1)
475 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
479 /* copysign(copysign(x, y), z) -> copysign(x, z). */
480 (for copysigns (COPYSIGN)
482 (copysigns (copysigns @0 @1) @2)
485 /* copysign(x,y)*copysign(x,y) -> x*x. */
486 (for copysigns (COPYSIGN)
488 (mult (copysigns@2 @0 @1) @2)
491 /* ccos(-x) -> ccos(x). Similarly for ccosh. */
492 (for ccoss (CCOS CCOSH)
497 /* cabs(-x) and cos(conj(x)) -> cabs(x). */
498 (for ops (conj negate)
504 /* Fold (a * (1 << b)) into (a << b) */
506 (mult:c @0 (convert? (lshift integer_onep@1 @2)))
507 (if (! FLOAT_TYPE_P (type)
508 && tree_nop_conversion_p (type, TREE_TYPE (@1)))
511 /* Fold (C1/X)*C2 into (C1*C2)/X. */
513 (mult (rdiv@3 REAL_CST@0 @1) REAL_CST@2)
514 (if (flag_associative_math
517 { tree tem = const_binop (MULT_EXPR, type, @0, @2); }
519 (rdiv { tem; } @1)))))
521 /* Convert C1/(X*C2) into (C1/C2)/X */
523 (rdiv REAL_CST@0 (mult @1 REAL_CST@2))
524 (if (flag_reciprocal_math)
526 { tree tem = const_binop (RDIV_EXPR, type, @0, @2); }
528 (rdiv { tem; } @1)))))
530 /* Simplify ~X & X as zero. */
532 (bit_and:c (convert? @0) (convert? (bit_not @0)))
533 { build_zero_cst (type); })
535 /* PR71636: Transform x & ((1U << b) - 1) -> x & ~(~0U << b); */
537 (bit_and:c @0 (plus:s (lshift:s integer_onep @1) integer_minus_onep))
538 (if (TYPE_UNSIGNED (type))
539 (bit_and @0 (bit_not (lshift { build_all_ones_cst (type); } @1)))))
541 /* PR35691: Transform
542 (x == 0 & y == 0) -> (x | typeof(x)(y)) == 0.
543 (x != 0 | y != 0) -> (x | typeof(x)(y)) != 0. */
544 (for bitop (bit_and bit_ior)
547 (bitop (cmp @0 integer_zerop@2) (cmp @1 integer_zerop))
548 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
549 && INTEGRAL_TYPE_P (TREE_TYPE (@1))
550 && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
551 (cmp (bit_ior @0 (convert @1)) @2))))
553 /* Fold (A & ~B) - (A & B) into (A ^ B) - B. */
555 (minus (bit_and:cs @0 (bit_not @1)) (bit_and:cs @0 @1))
556 (minus (bit_xor @0 @1) @1))
558 (minus (bit_and:s @0 INTEGER_CST@2) (bit_and:s @0 INTEGER_CST@1))
559 (if (wi::bit_not (@2) == @1)
560 (minus (bit_xor @0 @1) @1)))
562 /* Fold (A & B) - (A & ~B) into B - (A ^ B). */
564 (minus (bit_and:cs @0 @1) (bit_and:cs @0 (bit_not @1)))
565 (minus @1 (bit_xor @0 @1)))
567 /* Simplify (X & ~Y) | (~X & Y) -> X ^ Y. */
569 (bit_ior (bit_and:c @0 (bit_not @1)) (bit_and:c (bit_not @0) @1))
572 (bit_ior:c (bit_and @0 INTEGER_CST@2) (bit_and (bit_not @0) INTEGER_CST@1))
573 (if (wi::bit_not (@2) == @1)
576 /* PR53979: Transform ((a ^ b) | a) -> (a | b) */
578 (bit_ior:c (bit_xor:c @0 @1) @0)
581 /* Simplify (~X & Y) to X ^ Y if we know that (X & ~Y) is 0. */
584 (bit_and (bit_not 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 % Y is smaller than Y. */
593 (cmp (trunc_mod @0 @1) @1)
594 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
595 { constant_boolean_node (cmp == LT_EXPR, type); })))
598 (cmp @1 (trunc_mod @0 @1))
599 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
600 { constant_boolean_node (cmp == GT_EXPR, type); })))
604 (bit_ior @0 integer_all_onesp@1)
609 (bit_ior @0 integer_zerop)
614 (bit_and @0 integer_zerop@1)
620 (for op (bit_ior bit_xor plus)
622 (op:c (convert? @0) (convert? (bit_not @0)))
623 (convert { build_all_ones_cst (TREE_TYPE (@0)); })))
628 { build_zero_cst (type); })
630 /* Canonicalize X ^ ~0 to ~X. */
632 (bit_xor @0 integer_all_onesp@1)
637 (bit_and @0 integer_all_onesp)
640 /* x & x -> x, x | x -> x */
641 (for bitop (bit_and bit_ior)
646 /* x & C -> x if we know that x & ~C == 0. */
649 (bit_and SSA_NAME@0 INTEGER_CST@1)
650 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
651 && (get_nonzero_bits (@0) & wi::bit_not (@1)) == 0)
655 /* x + (x & 1) -> (x + 1) & ~1 */
657 (plus:c @0 (bit_and:s @0 integer_onep@1))
658 (bit_and (plus @0 @1) (bit_not @1)))
660 /* x & ~(x & y) -> x & ~y */
661 /* x | ~(x | y) -> x | ~y */
662 (for bitop (bit_and bit_ior)
664 (bitop:c @0 (bit_not (bitop:cs @0 @1)))
665 (bitop @0 (bit_not @1))))
667 /* (x | y) & ~x -> y & ~x */
668 /* (x & y) | ~x -> y | ~x */
669 (for bitop (bit_and bit_ior)
670 rbitop (bit_ior bit_and)
672 (bitop:c (rbitop:c @0 @1) (bit_not@2 @0))
675 /* (x & y) ^ (x | y) -> x ^ y */
677 (bit_xor:c (bit_and @0 @1) (bit_ior @0 @1))
680 /* (x ^ y) ^ (x | y) -> x & y */
682 (bit_xor:c (bit_xor @0 @1) (bit_ior @0 @1))
685 /* (x & y) + (x ^ y) -> x | y */
686 /* (x & y) | (x ^ y) -> x | y */
687 /* (x & y) ^ (x ^ y) -> x | y */
688 (for op (plus bit_ior bit_xor)
690 (op:c (bit_and @0 @1) (bit_xor @0 @1))
693 /* (x & y) + (x | y) -> x + y */
695 (plus:c (bit_and @0 @1) (bit_ior @0 @1))
698 /* (x + y) - (x | y) -> x & y */
700 (minus (plus @0 @1) (bit_ior @0 @1))
701 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
702 && !TYPE_SATURATING (type))
705 /* (x + y) - (x & y) -> x | y */
707 (minus (plus @0 @1) (bit_and @0 @1))
708 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
709 && !TYPE_SATURATING (type))
712 /* (x | y) - (x ^ y) -> x & y */
714 (minus (bit_ior @0 @1) (bit_xor @0 @1))
717 /* (x | y) - (x & y) -> x ^ y */
719 (minus (bit_ior @0 @1) (bit_and @0 @1))
722 /* (x | y) & ~(x & y) -> x ^ y */
724 (bit_and:c (bit_ior @0 @1) (bit_not (bit_and @0 @1)))
727 /* (x | y) & (~x ^ y) -> x & y */
729 (bit_and:c (bit_ior:c @0 @1) (bit_xor:c @1 (bit_not @0)))
732 /* ~x & ~y -> ~(x | y)
733 ~x | ~y -> ~(x & y) */
734 (for op (bit_and bit_ior)
735 rop (bit_ior bit_and)
737 (op (convert1? (bit_not @0)) (convert2? (bit_not @1)))
738 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
739 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
740 (bit_not (rop (convert @0) (convert @1))))))
742 /* If we are XORing or adding two BIT_AND_EXPR's, both of which are and'ing
743 with a constant, and the two constants have no bits in common,
744 we should treat this as a BIT_IOR_EXPR since this may produce more
746 (for op (bit_xor plus)
748 (op (convert1? (bit_and@4 @0 INTEGER_CST@1))
749 (convert2? (bit_and@5 @2 INTEGER_CST@3)))
750 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
751 && tree_nop_conversion_p (type, TREE_TYPE (@2))
752 && wi::bit_and (@1, @3) == 0)
753 (bit_ior (convert @4) (convert @5)))))
755 /* (X | Y) ^ X -> Y & ~ X*/
757 (bit_xor:c (convert1? (bit_ior:c @@0 @1)) (convert2? @0))
758 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
759 (convert (bit_and @1 (bit_not @0)))))
761 /* Convert ~X ^ ~Y to X ^ Y. */
763 (bit_xor (convert1? (bit_not @0)) (convert2? (bit_not @1)))
764 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
765 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
766 (bit_xor (convert @0) (convert @1))))
768 /* Convert ~X ^ C to X ^ ~C. */
770 (bit_xor (convert? (bit_not @0)) INTEGER_CST@1)
771 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
772 (bit_xor (convert @0) (bit_not @1))))
774 /* Fold (X & Y) ^ Y and (X ^ Y) & Y as ~X & Y. */
775 (for opo (bit_and bit_xor)
776 opi (bit_xor bit_and)
778 (opo:c (opi:c @0 @1) @1)
779 (bit_and (bit_not @0) @1)))
781 /* Given a bit-wise operation CODE applied to ARG0 and ARG1, see if both
782 operands are another bit-wise operation with a common input. If so,
783 distribute the bit operations to save an operation and possibly two if
784 constants are involved. For example, convert
785 (A | B) & (A | C) into A | (B & C)
786 Further simplification will occur if B and C are constants. */
787 (for op (bit_and bit_ior bit_xor)
788 rop (bit_ior bit_and bit_and)
790 (op (convert? (rop:c @@0 @1)) (convert? (rop:c @0 @2)))
791 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
792 && tree_nop_conversion_p (type, TREE_TYPE (@2)))
793 (rop (convert @0) (op (convert @1) (convert @2))))))
795 /* Some simple reassociation for bit operations, also handled in reassoc. */
796 /* (X & Y) & Y -> X & Y
797 (X | Y) | Y -> X | Y */
798 (for op (bit_and bit_ior)
800 (op:c (convert1?@2 (op:c @0 @@1)) (convert2? @1))
802 /* (X ^ Y) ^ Y -> X */
804 (bit_xor:c (convert1? (bit_xor:c @0 @@1)) (convert2? @1))
806 /* (X & Y) & (X & Z) -> (X & Y) & Z
807 (X | Y) | (X | Z) -> (X | Y) | Z */
808 (for op (bit_and bit_ior)
810 (op:c (convert1?@3 (op:c@4 @0 @1)) (convert2?@5 (op:c@6 @0 @2)))
811 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
812 && tree_nop_conversion_p (type, TREE_TYPE (@2)))
813 (if (single_use (@5) && single_use (@6))
815 (if (single_use (@3) && single_use (@4))
816 (op (convert @1) @5))))))
817 /* (X ^ Y) ^ (X ^ Z) -> Y ^ Z */
819 (bit_xor (convert1? (bit_xor:c @0 @1)) (convert2? (bit_xor:c @0 @2)))
820 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
821 && tree_nop_conversion_p (type, TREE_TYPE (@2)))
822 (bit_xor (convert @1) (convert @2))))
831 (abs tree_expr_nonnegative_p@0)
834 /* A few cases of fold-const.c negate_expr_p predicate. */
837 (if ((INTEGRAL_TYPE_P (type)
838 && TYPE_OVERFLOW_WRAPS (type))
839 || (!TYPE_OVERFLOW_SANITIZED (type)
840 && may_negate_without_overflow_p (t)))))
845 (if (!TYPE_OVERFLOW_SANITIZED (type))))
848 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (t)))))
849 /* VECTOR_CST handling of non-wrapping types would recurse in unsupported
853 (if (FLOAT_TYPE_P (TREE_TYPE (type)) || TYPE_OVERFLOW_WRAPS (type))))
855 /* (-A) * (-B) -> A * B */
857 (mult:c (convert1? (negate @0)) (convert2? negate_expr_p@1))
858 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
859 && tree_nop_conversion_p (type, TREE_TYPE (@1)))
860 (mult (convert @0) (convert (negate @1)))))
862 /* -(A + B) -> (-B) - A. */
864 (negate (plus:c @0 negate_expr_p@1))
865 (if (!HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type))
866 && !HONOR_SIGNED_ZEROS (element_mode (type)))
867 (minus (negate @1) @0)))
869 /* A - B -> A + (-B) if B is easily negatable. */
871 (minus @0 negate_expr_p@1)
872 (if (!FIXED_POINT_TYPE_P (type))
873 (plus @0 (negate @1))))
875 /* Try to fold (type) X op CST -> (type) (X op ((type-x) CST))
877 For bitwise binary operations apply operand conversions to the
878 binary operation result instead of to the operands. This allows
879 to combine successive conversions and bitwise binary operations.
880 We combine the above two cases by using a conditional convert. */
881 (for bitop (bit_and bit_ior bit_xor)
883 (bitop (convert @0) (convert? @1))
884 (if (((TREE_CODE (@1) == INTEGER_CST
885 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
886 && int_fits_type_p (@1, TREE_TYPE (@0)))
887 || types_match (@0, @1))
888 /* ??? This transform conflicts with fold-const.c doing
889 Convert (T)(x & c) into (T)x & (T)c, if c is an integer
890 constants (if x has signed type, the sign bit cannot be set
891 in c). This folds extension into the BIT_AND_EXPR.
892 Restrict it to GIMPLE to avoid endless recursions. */
893 && (bitop != BIT_AND_EXPR || GIMPLE)
894 && (/* That's a good idea if the conversion widens the operand, thus
895 after hoisting the conversion the operation will be narrower. */
896 TYPE_PRECISION (TREE_TYPE (@0)) < TYPE_PRECISION (type)
897 /* It's also a good idea if the conversion is to a non-integer
899 || GET_MODE_CLASS (TYPE_MODE (type)) != MODE_INT
900 /* Or if the precision of TO is not the same as the precision
902 || TYPE_PRECISION (type) != GET_MODE_PRECISION (TYPE_MODE (type))))
903 (convert (bitop @0 (convert @1))))))
905 (for bitop (bit_and bit_ior)
906 rbitop (bit_ior bit_and)
907 /* (x | y) & x -> x */
908 /* (x & y) | x -> x */
910 (bitop:c (rbitop:c @0 @1) @0)
912 /* (~x | y) & x -> x & y */
913 /* (~x & y) | x -> x | y */
915 (bitop:c (rbitop:c (bit_not @0) @1) @0)
918 /* (x | CST1) & CST2 -> (x & CST2) | (CST1 & CST2) */
920 (bit_and (bit_ior @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
921 (bit_ior (bit_and @0 @2) (bit_and @1 @2)))
923 /* Combine successive equal operations with constants. */
924 (for bitop (bit_and bit_ior bit_xor)
926 (bitop (bitop @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
927 (bitop @0 (bitop @1 @2))))
929 /* Try simple folding for X op !X, and X op X with the help
930 of the truth_valued_p and logical_inverted_value predicates. */
931 (match truth_valued_p
933 (if (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1)))
934 (for op (tcc_comparison truth_and truth_andif truth_or truth_orif truth_xor)
935 (match truth_valued_p
937 (match truth_valued_p
940 (match (logical_inverted_value @0)
942 (match (logical_inverted_value @0)
943 (bit_not truth_valued_p@0))
944 (match (logical_inverted_value @0)
945 (eq @0 integer_zerop))
946 (match (logical_inverted_value @0)
947 (ne truth_valued_p@0 integer_truep))
948 (match (logical_inverted_value @0)
949 (bit_xor truth_valued_p@0 integer_truep))
953 (bit_and:c @0 (logical_inverted_value @0))
954 { build_zero_cst (type); })
955 /* X | !X and X ^ !X -> 1, , if X is truth-valued. */
956 (for op (bit_ior bit_xor)
958 (op:c truth_valued_p@0 (logical_inverted_value @0))
959 { constant_boolean_node (true, type); }))
960 /* X ==/!= !X is false/true. */
963 (op:c truth_valued_p@0 (logical_inverted_value @0))
964 { constant_boolean_node (op == NE_EXPR ? true : false, type); }))
968 (bit_not (bit_not @0))
971 /* Convert ~ (-A) to A - 1. */
973 (bit_not (convert? (negate @0)))
974 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
975 || !TYPE_UNSIGNED (TREE_TYPE (@0)))
976 (convert (minus @0 { build_each_one_cst (TREE_TYPE (@0)); }))))
978 /* Convert ~ (A - 1) or ~ (A + -1) to -A. */
980 (bit_not (convert? (minus @0 integer_each_onep)))
981 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
982 || !TYPE_UNSIGNED (TREE_TYPE (@0)))
983 (convert (negate @0))))
985 (bit_not (convert? (plus @0 integer_all_onesp)))
986 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
987 || !TYPE_UNSIGNED (TREE_TYPE (@0)))
988 (convert (negate @0))))
990 /* Part of convert ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify. */
992 (bit_not (convert? (bit_xor @0 INTEGER_CST@1)))
993 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
994 (convert (bit_xor @0 (bit_not @1)))))
996 (bit_not (convert? (bit_xor:c (bit_not @0) @1)))
997 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
998 (convert (bit_xor @0 @1))))
1000 /* (x & ~m) | (y & m) -> ((x ^ y) & m) ^ x */
1002 (bit_ior:c (bit_and:cs @0 (bit_not @2)) (bit_and:cs @1 @2))
1003 (bit_xor (bit_and (bit_xor @0 @1) @2) @0))
1005 /* Fold A - (A & B) into ~B & A. */
1007 (minus (convert1? @0) (convert2?:s (bit_and:cs @@0 @1)))
1008 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
1009 && tree_nop_conversion_p (type, TREE_TYPE (@1)))
1010 (convert (bit_and (bit_not @1) @0))))
1012 /* For integral types with undefined overflow and C != 0 fold
1013 x * C EQ/NE y * C into x EQ/NE y. */
1016 (cmp (mult:c @0 @1) (mult:c @2 @1))
1017 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
1018 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1019 && tree_expr_nonzero_p (@1))
1022 /* For integral types with undefined overflow and C != 0 fold
1023 x * C RELOP y * C into:
1025 x RELOP y for nonnegative C
1026 y RELOP x for negative C */
1027 (for cmp (lt gt le ge)
1029 (cmp (mult:c @0 @1) (mult:c @2 @1))
1030 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
1031 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1032 (if (tree_expr_nonnegative_p (@1) && tree_expr_nonzero_p (@1))
1034 (if (TREE_CODE (@1) == INTEGER_CST
1035 && wi::neg_p (@1, TYPE_SIGN (TREE_TYPE (@1))))
1038 /* ((X inner_op C0) outer_op C1)
1039 With X being a tree where value_range has reasoned certain bits to always be
1040 zero throughout its computed value range,
1041 inner_op = {|,^}, outer_op = {|,^} and inner_op != outer_op
1042 where zero_mask has 1's for all bits that are sure to be 0 in
1044 if (inner_op == '^') C0 &= ~C1;
1045 if ((C0 & ~zero_mask) == 0) then emit (X outer_op (C0 outer_op C1)
1046 if ((C1 & ~zero_mask) == 0) then emit (X inner_op (C0 outer_op C1)
1048 (for inner_op (bit_ior bit_xor)
1049 outer_op (bit_xor bit_ior)
1052 (inner_op:s @2 INTEGER_CST@0) INTEGER_CST@1)
1056 wide_int zero_mask_not;
1060 if (TREE_CODE (@2) == SSA_NAME)
1061 zero_mask_not = get_nonzero_bits (@2);
1065 if (inner_op == BIT_XOR_EXPR)
1067 C0 = wi::bit_and_not (@0, @1);
1068 cst_emit = wi::bit_or (C0, @1);
1073 cst_emit = wi::bit_xor (@0, @1);
1076 (if (!fail && wi::bit_and (C0, zero_mask_not) == 0)
1077 (outer_op @2 { wide_int_to_tree (type, cst_emit); })
1078 (if (!fail && wi::bit_and (@1, zero_mask_not) == 0)
1079 (inner_op @2 { wide_int_to_tree (type, cst_emit); }))))))
1081 /* Associate (p +p off1) +p off2 as (p +p (off1 + off2)). */
1083 (pointer_plus (pointer_plus:s @0 @1) @3)
1084 (pointer_plus @0 (plus @1 @3)))
1090 tem4 = (unsigned long) tem3;
1095 (pointer_plus @0 (convert?@2 (minus@3 (convert @1) (convert @0))))
1096 /* Conditionally look through a sign-changing conversion. */
1097 (if (TYPE_PRECISION (TREE_TYPE (@2)) == TYPE_PRECISION (TREE_TYPE (@3))
1098 && ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@1)))
1099 || (GENERIC && type == TREE_TYPE (@1))))
1103 tem = (sizetype) ptr;
1107 and produce the simpler and easier to analyze with respect to alignment
1108 ... = ptr & ~algn; */
1110 (pointer_plus @0 (negate (bit_and (convert @0) INTEGER_CST@1)))
1111 (with { tree algn = wide_int_to_tree (TREE_TYPE (@0), wi::bit_not (@1)); }
1112 (bit_and @0 { algn; })))
1114 /* Try folding difference of addresses. */
1116 (minus (convert ADDR_EXPR@0) (convert @1))
1117 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1118 (with { HOST_WIDE_INT diff; }
1119 (if (ptr_difference_const (@0, @1, &diff))
1120 { build_int_cst_type (type, diff); }))))
1122 (minus (convert @0) (convert ADDR_EXPR@1))
1123 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1124 (with { HOST_WIDE_INT diff; }
1125 (if (ptr_difference_const (@0, @1, &diff))
1126 { build_int_cst_type (type, diff); }))))
1128 /* If arg0 is derived from the address of an object or function, we may
1129 be able to fold this expression using the object or function's
1132 (bit_and (convert? @0) INTEGER_CST@1)
1133 (if (POINTER_TYPE_P (TREE_TYPE (@0))
1134 && tree_nop_conversion_p (type, TREE_TYPE (@0)))
1138 unsigned HOST_WIDE_INT bitpos;
1139 get_pointer_alignment_1 (@0, &align, &bitpos);
1141 (if (wi::ltu_p (@1, align / BITS_PER_UNIT))
1142 { wide_int_to_tree (type, wi::bit_and (@1, bitpos / BITS_PER_UNIT)); }))))
1145 /* We can't reassociate at all for saturating types. */
1146 (if (!TYPE_SATURATING (type))
1148 /* Contract negates. */
1149 /* A + (-B) -> A - B */
1151 (plus:c (convert1? @0) (convert2? (negate @1)))
1152 /* Apply STRIP_NOPS on @0 and the negate. */
1153 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
1154 && tree_nop_conversion_p (type, TREE_TYPE (@1))
1155 && !TYPE_OVERFLOW_SANITIZED (type))
1156 (minus (convert @0) (convert @1))))
1157 /* A - (-B) -> A + B */
1159 (minus (convert1? @0) (convert2? (negate @1)))
1160 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
1161 && tree_nop_conversion_p (type, TREE_TYPE (@1))
1162 && !TYPE_OVERFLOW_SANITIZED (type))
1163 (plus (convert @0) (convert @1))))
1166 (negate (convert? (negate @1)))
1167 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1168 && !TYPE_OVERFLOW_SANITIZED (type))
1171 /* We can't reassociate floating-point unless -fassociative-math
1172 or fixed-point plus or minus because of saturation to +-Inf. */
1173 (if ((!FLOAT_TYPE_P (type) || flag_associative_math)
1174 && !FIXED_POINT_TYPE_P (type))
1176 /* Match patterns that allow contracting a plus-minus pair
1177 irrespective of overflow issues. */
1178 /* (A +- B) - A -> +- B */
1179 /* (A +- B) -+ B -> A */
1180 /* A - (A +- B) -> -+ B */
1181 /* A +- (B -+ A) -> +- B */
1183 (minus (plus:c @0 @1) @0)
1186 (minus (minus @0 @1) @0)
1189 (plus:c (minus @0 @1) @1)
1192 (minus @0 (plus:c @0 @1))
1195 (minus @0 (minus @0 @1))
1198 /* (A +- CST1) +- CST2 -> A + CST3 */
1199 (for outer_op (plus minus)
1200 (for inner_op (plus minus)
1202 (outer_op (inner_op @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
1203 /* If the constant operation overflows we cannot do the transform
1204 as we would introduce undefined overflow, for example
1205 with (a - 1) + INT_MIN. */
1206 (with { tree cst = const_binop (outer_op == inner_op
1207 ? PLUS_EXPR : MINUS_EXPR, type, @1, @2); }
1208 (if (cst && !TREE_OVERFLOW (cst))
1209 (inner_op @0 { cst; } ))))))
1211 /* (CST1 - A) +- CST2 -> CST3 - A */
1212 (for outer_op (plus minus)
1214 (outer_op (minus CONSTANT_CLASS_P@1 @0) CONSTANT_CLASS_P@2)
1215 (with { tree cst = const_binop (outer_op, type, @1, @2); }
1216 (if (cst && !TREE_OVERFLOW (cst))
1217 (minus { cst; } @0)))))
1219 /* CST1 - (CST2 - A) -> CST3 + A */
1221 (minus CONSTANT_CLASS_P@1 (minus CONSTANT_CLASS_P@2 @0))
1222 (with { tree cst = const_binop (MINUS_EXPR, type, @1, @2); }
1223 (if (cst && !TREE_OVERFLOW (cst))
1224 (plus { cst; } @0))))
1228 (plus:c (bit_not @0) @0)
1229 (if (!TYPE_OVERFLOW_TRAPS (type))
1230 { build_all_ones_cst (type); }))
1234 (plus (convert? (bit_not @0)) integer_each_onep)
1235 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1236 (negate (convert @0))))
1240 (minus (convert? (negate @0)) integer_each_onep)
1241 (if (!TYPE_OVERFLOW_TRAPS (type)
1242 && tree_nop_conversion_p (type, TREE_TYPE (@0)))
1243 (bit_not (convert @0))))
1247 (minus integer_all_onesp @0)
1250 /* (T)(P + A) - (T)P -> (T) A */
1251 (for add (plus pointer_plus)
1253 (minus (convert (add @@0 @1))
1255 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
1256 /* For integer types, if A has a smaller type
1257 than T the result depends on the possible
1259 E.g. T=size_t, A=(unsigned)429497295, P>0.
1260 However, if an overflow in P + A would cause
1261 undefined behavior, we can assume that there
1263 || (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1264 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1265 /* For pointer types, if the conversion of A to the
1266 final type requires a sign- or zero-extension,
1267 then we have to punt - it is not defined which
1269 || (POINTER_TYPE_P (TREE_TYPE (@0))
1270 && TREE_CODE (@1) == INTEGER_CST
1271 && tree_int_cst_sign_bit (@1) == 0))
1274 /* (T)P - (T)(P + A) -> -(T) A */
1275 (for add (plus pointer_plus)
1278 (convert (add @@0 @1)))
1279 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
1280 /* For integer types, if A has a smaller type
1281 than T the result depends on the possible
1283 E.g. T=size_t, A=(unsigned)429497295, P>0.
1284 However, if an overflow in P + A would cause
1285 undefined behavior, we can assume that there
1287 || (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1288 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1289 /* For pointer types, if the conversion of A to the
1290 final type requires a sign- or zero-extension,
1291 then we have to punt - it is not defined which
1293 || (POINTER_TYPE_P (TREE_TYPE (@0))
1294 && TREE_CODE (@1) == INTEGER_CST
1295 && tree_int_cst_sign_bit (@1) == 0))
1296 (negate (convert @1)))))
1298 /* (T)(P + A) - (T)(P + B) -> (T)A - (T)B */
1299 (for add (plus pointer_plus)
1301 (minus (convert (add @@0 @1))
1302 (convert (add @0 @2)))
1303 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
1304 /* For integer types, if A has a smaller type
1305 than T the result depends on the possible
1307 E.g. T=size_t, A=(unsigned)429497295, P>0.
1308 However, if an overflow in P + A would cause
1309 undefined behavior, we can assume that there
1311 || (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1312 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1313 /* For pointer types, if the conversion of A to the
1314 final type requires a sign- or zero-extension,
1315 then we have to punt - it is not defined which
1317 || (POINTER_TYPE_P (TREE_TYPE (@0))
1318 && TREE_CODE (@1) == INTEGER_CST
1319 && tree_int_cst_sign_bit (@1) == 0
1320 && TREE_CODE (@2) == INTEGER_CST
1321 && tree_int_cst_sign_bit (@2) == 0))
1322 (minus (convert @1) (convert @2)))))))
1325 /* Simplifications of MIN_EXPR, MAX_EXPR, fmin() and fmax(). */
1327 (for minmax (min max FMIN FMAX)
1331 /* min(max(x,y),y) -> y. */
1333 (min:c (max:c @0 @1) @1)
1335 /* max(min(x,y),y) -> y. */
1337 (max:c (min:c @0 @1) @1)
1339 /* max(a,-a) -> abs(a). */
1341 (max:c @0 (negate @0))
1342 (if (TREE_CODE (type) != COMPLEX_TYPE
1343 && (! ANY_INTEGRAL_TYPE_P (type)
1344 || TYPE_OVERFLOW_UNDEFINED (type)))
1346 /* min(a,-a) -> -abs(a). */
1348 (min:c @0 (negate @0))
1349 (if (TREE_CODE (type) != COMPLEX_TYPE
1350 && (! ANY_INTEGRAL_TYPE_P (type)
1351 || TYPE_OVERFLOW_UNDEFINED (type)))
1356 (if (INTEGRAL_TYPE_P (type)
1357 && TYPE_MIN_VALUE (type)
1358 && operand_equal_p (@1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
1360 (if (INTEGRAL_TYPE_P (type)
1361 && TYPE_MAX_VALUE (type)
1362 && operand_equal_p (@1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
1367 (if (INTEGRAL_TYPE_P (type)
1368 && TYPE_MAX_VALUE (type)
1369 && operand_equal_p (@1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
1371 (if (INTEGRAL_TYPE_P (type)
1372 && TYPE_MIN_VALUE (type)
1373 && operand_equal_p (@1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
1376 /* max (a, a + CST) -> a + CST where CST is positive. */
1377 /* max (a, a + CST) -> a where CST is negative. */
1379 (max:c @0 (plus@2 @0 INTEGER_CST@1))
1380 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1381 (if (tree_int_cst_sgn (@1) > 0)
1385 /* min (a, a + CST) -> a where CST is positive. */
1386 /* min (a, a + CST) -> a + CST where CST is negative. */
1388 (min:c @0 (plus@2 @0 INTEGER_CST@1))
1389 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1390 (if (tree_int_cst_sgn (@1) > 0)
1394 /* (convert (minmax ((convert (x) c)))) -> minmax (x c) if x is promoted
1395 and the outer convert demotes the expression back to x's type. */
1396 (for minmax (min max)
1398 (convert (minmax@0 (convert @1) INTEGER_CST@2))
1399 (if (INTEGRAL_TYPE_P (type)
1400 && types_match (@1, type) && int_fits_type_p (@2, type)
1401 && TYPE_SIGN (TREE_TYPE (@0)) == TYPE_SIGN (type)
1402 && TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type))
1403 (minmax @1 (convert @2)))))
1405 (for minmax (FMIN FMAX)
1406 /* If either argument is NaN, return the other one. Avoid the
1407 transformation if we get (and honor) a signalling NaN. */
1409 (minmax:c @0 REAL_CST@1)
1410 (if (real_isnan (TREE_REAL_CST_PTR (@1))
1411 && (!HONOR_SNANS (@1) || !TREE_REAL_CST (@1).signalling))
1413 /* Convert fmin/fmax to MIN_EXPR/MAX_EXPR. C99 requires these
1414 functions to return the numeric arg if the other one is NaN.
1415 MIN and MAX don't honor that, so only transform if -ffinite-math-only
1416 is set. C99 doesn't require -0.0 to be handled, so we don't have to
1417 worry about it either. */
1418 (if (flag_finite_math_only)
1425 /* min (-A, -B) -> -max (A, B) */
1426 (for minmax (min max FMIN FMAX)
1427 maxmin (max min FMAX FMIN)
1429 (minmax (negate:s@2 @0) (negate:s@3 @1))
1430 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
1431 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1432 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
1433 (negate (maxmin @0 @1)))))
1434 /* MIN (~X, ~Y) -> ~MAX (X, Y)
1435 MAX (~X, ~Y) -> ~MIN (X, Y) */
1436 (for minmax (min max)
1439 (minmax (bit_not:s@2 @0) (bit_not:s@3 @1))
1440 (bit_not (maxmin @0 @1))))
1442 /* MIN (X, Y) == X -> X <= Y */
1443 (for minmax (min min max max)
1447 (cmp:c (minmax:c @0 @1) @0)
1448 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0)))
1450 /* MIN (X, 5) == 0 -> X == 0
1451 MIN (X, 5) == 7 -> false */
1454 (cmp (min @0 INTEGER_CST@1) INTEGER_CST@2)
1455 (if (wi::lt_p (@1, @2, TYPE_SIGN (TREE_TYPE (@0))))
1456 { constant_boolean_node (cmp == NE_EXPR, type); }
1457 (if (wi::gt_p (@1, @2, TYPE_SIGN (TREE_TYPE (@0))))
1461 (cmp (max @0 INTEGER_CST@1) INTEGER_CST@2)
1462 (if (wi::gt_p (@1, @2, TYPE_SIGN (TREE_TYPE (@0))))
1463 { constant_boolean_node (cmp == NE_EXPR, type); }
1464 (if (wi::lt_p (@1, @2, TYPE_SIGN (TREE_TYPE (@0))))
1466 /* MIN (X, C1) < C2 -> X < C2 || C1 < C2 */
1467 (for minmax (min min max max min min max max )
1468 cmp (lt le gt ge gt ge lt le )
1469 comb (bit_ior bit_ior bit_ior bit_ior bit_and bit_and bit_and bit_and)
1471 (cmp (minmax @0 INTEGER_CST@1) INTEGER_CST@2)
1472 (comb (cmp @0 @2) (cmp @1 @2))))
1474 /* Simplifications of shift and rotates. */
1476 (for rotate (lrotate rrotate)
1478 (rotate integer_all_onesp@0 @1)
1481 /* Optimize -1 >> x for arithmetic right shifts. */
1483 (rshift integer_all_onesp@0 @1)
1484 (if (!TYPE_UNSIGNED (type)
1485 && tree_expr_nonnegative_p (@1))
1488 /* Optimize (x >> c) << c into x & (-1<<c). */
1490 (lshift (rshift @0 INTEGER_CST@1) @1)
1491 (if (wi::ltu_p (@1, element_precision (type)))
1492 (bit_and @0 (lshift { build_minus_one_cst (type); } @1))))
1494 /* Optimize (x << c) >> c into x & ((unsigned)-1 >> c) for unsigned
1497 (rshift (lshift @0 INTEGER_CST@1) @1)
1498 (if (TYPE_UNSIGNED (type)
1499 && (wi::ltu_p (@1, element_precision (type))))
1500 (bit_and @0 (rshift { build_minus_one_cst (type); } @1))))
1502 (for shiftrotate (lrotate rrotate lshift rshift)
1504 (shiftrotate @0 integer_zerop)
1507 (shiftrotate integer_zerop@0 @1)
1509 /* Prefer vector1 << scalar to vector1 << vector2
1510 if vector2 is uniform. */
1511 (for vec (VECTOR_CST CONSTRUCTOR)
1513 (shiftrotate @0 vec@1)
1514 (with { tree tem = uniform_vector_p (@1); }
1516 (shiftrotate @0 { tem; }))))))
1518 /* Rewrite an LROTATE_EXPR by a constant into an
1519 RROTATE_EXPR by a new constant. */
1521 (lrotate @0 INTEGER_CST@1)
1522 (rrotate @0 { const_binop (MINUS_EXPR, TREE_TYPE (@1),
1523 build_int_cst (TREE_TYPE (@1),
1524 element_precision (type)), @1); }))
1526 /* Turn (a OP c1) OP c2 into a OP (c1+c2). */
1527 (for op (lrotate rrotate rshift lshift)
1529 (op (op @0 INTEGER_CST@1) INTEGER_CST@2)
1530 (with { unsigned int prec = element_precision (type); }
1531 (if (wi::ge_p (@1, 0, TYPE_SIGN (TREE_TYPE (@1)))
1532 && wi::lt_p (@1, prec, TYPE_SIGN (TREE_TYPE (@1)))
1533 && wi::ge_p (@2, 0, TYPE_SIGN (TREE_TYPE (@2)))
1534 && wi::lt_p (@2, prec, TYPE_SIGN (TREE_TYPE (@2))))
1535 (with { unsigned int low = wi::add (@1, @2).to_uhwi (); }
1536 /* Deal with a OP (c1 + c2) being undefined but (a OP c1) OP c2
1537 being well defined. */
1539 (if (op == LROTATE_EXPR || op == RROTATE_EXPR)
1540 (op @0 { build_int_cst (TREE_TYPE (@1), low % prec); })
1541 (if (TYPE_UNSIGNED (type) || op == LSHIFT_EXPR)
1542 { build_zero_cst (type); }
1543 (op @0 { build_int_cst (TREE_TYPE (@1), prec - 1); })))
1544 (op @0 { build_int_cst (TREE_TYPE (@1), low); })))))))
1547 /* ((1 << A) & 1) != 0 -> A == 0
1548 ((1 << A) & 1) == 0 -> A != 0 */
1552 (cmp (bit_and (lshift integer_onep @0) integer_onep) integer_zerop)
1553 (icmp @0 { build_zero_cst (TREE_TYPE (@0)); })))
1555 /* (CST1 << A) == CST2 -> A == ctz (CST2) - ctz (CST1)
1556 (CST1 << A) != CST2 -> A != ctz (CST2) - ctz (CST1)
1560 (cmp (lshift INTEGER_CST@0 @1) INTEGER_CST@2)
1561 (with { int cand = wi::ctz (@2) - wi::ctz (@0); }
1563 || (!integer_zerop (@2)
1564 && wi::ne_p (wi::lshift (@0, cand), @2)))
1565 { constant_boolean_node (cmp == NE_EXPR, type); }
1566 (if (!integer_zerop (@2)
1567 && wi::eq_p (wi::lshift (@0, cand), @2))
1568 (cmp @1 { build_int_cst (TREE_TYPE (@1), cand); }))))))
1570 /* Fold (X << C1) & C2 into (X << C1) & (C2 | ((1 << C1) - 1))
1571 (X >> C1) & C2 into (X >> C1) & (C2 | ~((type) -1 >> C1))
1572 if the new mask might be further optimized. */
1573 (for shift (lshift rshift)
1575 (bit_and (convert?:s@4 (shift:s@5 (convert1?@3 @0) INTEGER_CST@1))
1577 (if (tree_nop_conversion_p (TREE_TYPE (@4), TREE_TYPE (@5))
1578 && TYPE_PRECISION (type) <= HOST_BITS_PER_WIDE_INT
1579 && tree_fits_uhwi_p (@1)
1580 && tree_to_uhwi (@1) > 0
1581 && tree_to_uhwi (@1) < TYPE_PRECISION (type))
1584 unsigned int shiftc = tree_to_uhwi (@1);
1585 unsigned HOST_WIDE_INT mask = TREE_INT_CST_LOW (@2);
1586 unsigned HOST_WIDE_INT newmask, zerobits = 0;
1587 tree shift_type = TREE_TYPE (@3);
1590 if (shift == LSHIFT_EXPR)
1591 zerobits = ((HOST_WIDE_INT_1U << shiftc) - 1);
1592 else if (shift == RSHIFT_EXPR
1593 && (TYPE_PRECISION (shift_type)
1594 == GET_MODE_PRECISION (TYPE_MODE (shift_type))))
1596 prec = TYPE_PRECISION (TREE_TYPE (@3));
1598 /* See if more bits can be proven as zero because of
1601 && TYPE_UNSIGNED (TREE_TYPE (@0)))
1603 tree inner_type = TREE_TYPE (@0);
1604 if ((TYPE_PRECISION (inner_type)
1605 == GET_MODE_PRECISION (TYPE_MODE (inner_type)))
1606 && TYPE_PRECISION (inner_type) < prec)
1608 prec = TYPE_PRECISION (inner_type);
1609 /* See if we can shorten the right shift. */
1611 shift_type = inner_type;
1612 /* Otherwise X >> C1 is all zeros, so we'll optimize
1613 it into (X, 0) later on by making sure zerobits
1617 zerobits = HOST_WIDE_INT_M1U;
1620 zerobits >>= HOST_BITS_PER_WIDE_INT - shiftc;
1621 zerobits <<= prec - shiftc;
1623 /* For arithmetic shift if sign bit could be set, zerobits
1624 can contain actually sign bits, so no transformation is
1625 possible, unless MASK masks them all away. In that
1626 case the shift needs to be converted into logical shift. */
1627 if (!TYPE_UNSIGNED (TREE_TYPE (@3))
1628 && prec == TYPE_PRECISION (TREE_TYPE (@3)))
1630 if ((mask & zerobits) == 0)
1631 shift_type = unsigned_type_for (TREE_TYPE (@3));
1637 /* ((X << 16) & 0xff00) is (X, 0). */
1638 (if ((mask & zerobits) == mask)
1639 { build_int_cst (type, 0); }
1640 (with { newmask = mask | zerobits; }
1641 (if (newmask != mask && (newmask & (newmask + 1)) == 0)
1644 /* Only do the transformation if NEWMASK is some integer
1646 for (prec = BITS_PER_UNIT;
1647 prec < HOST_BITS_PER_WIDE_INT; prec <<= 1)
1648 if (newmask == (HOST_WIDE_INT_1U << prec) - 1)
1651 (if (prec < HOST_BITS_PER_WIDE_INT
1652 || newmask == HOST_WIDE_INT_M1U)
1654 { tree newmaskt = build_int_cst_type (TREE_TYPE (@2), newmask); }
1655 (if (!tree_int_cst_equal (newmaskt, @2))
1656 (if (shift_type != TREE_TYPE (@3))
1657 (bit_and (convert (shift:shift_type (convert @3) @1)) { newmaskt; })
1658 (bit_and @4 { newmaskt; })))))))))))))
1660 /* Fold (X {&,^,|} C2) << C1 into (X << C1) {&,^,|} (C2 << C1)
1661 (X {&,^,|} C2) >> C1 into (X >> C1) & (C2 >> C1). */
1662 (for shift (lshift rshift)
1663 (for bit_op (bit_and bit_xor bit_ior)
1665 (shift (convert?:s (bit_op:s @0 INTEGER_CST@2)) INTEGER_CST@1)
1666 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1667 (with { tree mask = int_const_binop (shift, fold_convert (type, @2), @1); }
1668 (bit_op (shift (convert @0) @1) { mask; }))))))
1670 /* ~(~X >> Y) -> X >> Y (for arithmetic shift). */
1672 (bit_not (convert1?:s (rshift:s (convert2?@0 (bit_not @1)) @2)))
1673 (if (!TYPE_UNSIGNED (TREE_TYPE (@0))
1674 && (element_precision (TREE_TYPE (@0))
1675 <= element_precision (TREE_TYPE (@1))
1676 || !TYPE_UNSIGNED (TREE_TYPE (@1))))
1678 { tree shift_type = TREE_TYPE (@0); }
1679 (convert (rshift (convert:shift_type @1) @2)))))
1681 /* ~(~X >>r Y) -> X >>r Y
1682 ~(~X <<r Y) -> X <<r Y */
1683 (for rotate (lrotate rrotate)
1685 (bit_not (convert1?:s (rotate:s (convert2?@0 (bit_not @1)) @2)))
1686 (if ((element_precision (TREE_TYPE (@0))
1687 <= element_precision (TREE_TYPE (@1))
1688 || !TYPE_UNSIGNED (TREE_TYPE (@1)))
1689 && (element_precision (type) <= element_precision (TREE_TYPE (@0))
1690 || !TYPE_UNSIGNED (TREE_TYPE (@0))))
1692 { tree rotate_type = TREE_TYPE (@0); }
1693 (convert (rotate (convert:rotate_type @1) @2))))))
1695 /* Simplifications of conversions. */
1697 /* Basic strip-useless-type-conversions / strip_nops. */
1698 (for cvt (convert view_convert float fix_trunc)
1701 (if ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@0)))
1702 || (GENERIC && type == TREE_TYPE (@0)))
1705 /* Contract view-conversions. */
1707 (view_convert (view_convert @0))
1710 /* For integral conversions with the same precision or pointer
1711 conversions use a NOP_EXPR instead. */
1714 (if ((INTEGRAL_TYPE_P (type) || POINTER_TYPE_P (type))
1715 && (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
1716 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (@0)))
1719 /* Strip inner integral conversions that do not change precision or size. */
1721 (view_convert (convert@0 @1))
1722 (if ((INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
1723 && (INTEGRAL_TYPE_P (TREE_TYPE (@1)) || POINTER_TYPE_P (TREE_TYPE (@1)))
1724 && (TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
1725 && (TYPE_SIZE (TREE_TYPE (@0)) == TYPE_SIZE (TREE_TYPE (@1))))
1728 /* Re-association barriers around constants and other re-association
1729 barriers can be removed. */
1731 (paren CONSTANT_CLASS_P@0)
1734 (paren (paren@1 @0))
1737 /* Handle cases of two conversions in a row. */
1738 (for ocvt (convert float fix_trunc)
1739 (for icvt (convert float)
1744 tree inside_type = TREE_TYPE (@0);
1745 tree inter_type = TREE_TYPE (@1);
1746 int inside_int = INTEGRAL_TYPE_P (inside_type);
1747 int inside_ptr = POINTER_TYPE_P (inside_type);
1748 int inside_float = FLOAT_TYPE_P (inside_type);
1749 int inside_vec = VECTOR_TYPE_P (inside_type);
1750 unsigned int inside_prec = TYPE_PRECISION (inside_type);
1751 int inside_unsignedp = TYPE_UNSIGNED (inside_type);
1752 int inter_int = INTEGRAL_TYPE_P (inter_type);
1753 int inter_ptr = POINTER_TYPE_P (inter_type);
1754 int inter_float = FLOAT_TYPE_P (inter_type);
1755 int inter_vec = VECTOR_TYPE_P (inter_type);
1756 unsigned int inter_prec = TYPE_PRECISION (inter_type);
1757 int inter_unsignedp = TYPE_UNSIGNED (inter_type);
1758 int final_int = INTEGRAL_TYPE_P (type);
1759 int final_ptr = POINTER_TYPE_P (type);
1760 int final_float = FLOAT_TYPE_P (type);
1761 int final_vec = VECTOR_TYPE_P (type);
1762 unsigned int final_prec = TYPE_PRECISION (type);
1763 int final_unsignedp = TYPE_UNSIGNED (type);
1766 /* In addition to the cases of two conversions in a row
1767 handled below, if we are converting something to its own
1768 type via an object of identical or wider precision, neither
1769 conversion is needed. */
1770 (if (((GIMPLE && useless_type_conversion_p (type, inside_type))
1772 && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (inside_type)))
1773 && (((inter_int || inter_ptr) && final_int)
1774 || (inter_float && final_float))
1775 && inter_prec >= final_prec)
1778 /* Likewise, if the intermediate and initial types are either both
1779 float or both integer, we don't need the middle conversion if the
1780 former is wider than the latter and doesn't change the signedness
1781 (for integers). Avoid this if the final type is a pointer since
1782 then we sometimes need the middle conversion. */
1783 (if (((inter_int && inside_int) || (inter_float && inside_float))
1784 && (final_int || final_float)
1785 && inter_prec >= inside_prec
1786 && (inter_float || inter_unsignedp == inside_unsignedp))
1789 /* If we have a sign-extension of a zero-extended value, we can
1790 replace that by a single zero-extension. Likewise if the
1791 final conversion does not change precision we can drop the
1792 intermediate conversion. */
1793 (if (inside_int && inter_int && final_int
1794 && ((inside_prec < inter_prec && inter_prec < final_prec
1795 && inside_unsignedp && !inter_unsignedp)
1796 || final_prec == inter_prec))
1799 /* Two conversions in a row are not needed unless:
1800 - some conversion is floating-point (overstrict for now), or
1801 - some conversion is a vector (overstrict for now), or
1802 - the intermediate type is narrower than both initial and
1804 - the intermediate type and innermost type differ in signedness,
1805 and the outermost type is wider than the intermediate, or
1806 - the initial type is a pointer type and the precisions of the
1807 intermediate and final types differ, or
1808 - the final type is a pointer type and the precisions of the
1809 initial and intermediate types differ. */
1810 (if (! inside_float && ! inter_float && ! final_float
1811 && ! inside_vec && ! inter_vec && ! final_vec
1812 && (inter_prec >= inside_prec || inter_prec >= final_prec)
1813 && ! (inside_int && inter_int
1814 && inter_unsignedp != inside_unsignedp
1815 && inter_prec < final_prec)
1816 && ((inter_unsignedp && inter_prec > inside_prec)
1817 == (final_unsignedp && final_prec > inter_prec))
1818 && ! (inside_ptr && inter_prec != final_prec)
1819 && ! (final_ptr && inside_prec != inter_prec))
1822 /* A truncation to an unsigned type (a zero-extension) should be
1823 canonicalized as bitwise and of a mask. */
1824 (if (GIMPLE /* PR70366: doing this in GENERIC breaks -Wconversion. */
1825 && final_int && inter_int && inside_int
1826 && final_prec == inside_prec
1827 && final_prec > inter_prec
1829 (convert (bit_and @0 { wide_int_to_tree
1831 wi::mask (inter_prec, false,
1832 TYPE_PRECISION (inside_type))); })))
1834 /* If we are converting an integer to a floating-point that can
1835 represent it exactly and back to an integer, we can skip the
1836 floating-point conversion. */
1837 (if (GIMPLE /* PR66211 */
1838 && inside_int && inter_float && final_int &&
1839 (unsigned) significand_size (TYPE_MODE (inter_type))
1840 >= inside_prec - !inside_unsignedp)
1843 /* If we have a narrowing conversion to an integral type that is fed by a
1844 BIT_AND_EXPR, we might be able to remove the BIT_AND_EXPR if it merely
1845 masks off bits outside the final type (and nothing else). */
1847 (convert (bit_and @0 INTEGER_CST@1))
1848 (if (INTEGRAL_TYPE_P (type)
1849 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
1850 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))
1851 && operand_equal_p (@1, build_low_bits_mask (TREE_TYPE (@1),
1852 TYPE_PRECISION (type)), 0))
1856 /* (X /[ex] A) * A -> X. */
1858 (mult (convert1? (exact_div @0 @@1)) (convert2? @1))
1861 /* Canonicalization of binary operations. */
1863 /* Convert X + -C into X - C. */
1865 (plus @0 REAL_CST@1)
1866 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
1867 (with { tree tem = const_unop (NEGATE_EXPR, type, @1); }
1868 (if (!TREE_OVERFLOW (tem) || !flag_trapping_math)
1869 (minus @0 { tem; })))))
1871 /* Convert x+x into x*2. */
1874 (if (SCALAR_FLOAT_TYPE_P (type))
1875 (mult @0 { build_real (type, dconst2); })
1876 (if (INTEGRAL_TYPE_P (type))
1877 (mult @0 { build_int_cst (type, 2); }))))
1880 (minus integer_zerop @1)
1883 /* (ARG0 - ARG1) is the same as (-ARG1 + ARG0). So check whether
1884 ARG0 is zero and X + ARG0 reduces to X, since that would mean
1885 (-ARG1 + ARG0) reduces to -ARG1. */
1887 (minus real_zerop@0 @1)
1888 (if (fold_real_zero_addition_p (type, @0, 0))
1891 /* Transform x * -1 into -x. */
1893 (mult @0 integer_minus_onep)
1896 /* True if we can easily extract the real and imaginary parts of a complex
1898 (match compositional_complex
1899 (convert? (complex @0 @1)))
1901 /* COMPLEX_EXPR and REALPART/IMAGPART_EXPR cancellations. */
1903 (complex (realpart @0) (imagpart @0))
1906 (realpart (complex @0 @1))
1909 (imagpart (complex @0 @1))
1912 /* Sometimes we only care about half of a complex expression. */
1914 (realpart (convert?:s (conj:s @0)))
1915 (convert (realpart @0)))
1917 (imagpart (convert?:s (conj:s @0)))
1918 (convert (negate (imagpart @0))))
1919 (for part (realpart imagpart)
1920 (for op (plus minus)
1922 (part (convert?:s@2 (op:s @0 @1)))
1923 (convert (op (part @0) (part @1))))))
1925 (realpart (convert?:s (CEXPI:s @0)))
1928 (imagpart (convert?:s (CEXPI:s @0)))
1931 /* conj(conj(x)) -> x */
1933 (conj (convert? (conj @0)))
1934 (if (tree_nop_conversion_p (TREE_TYPE (@0), type))
1937 /* conj({x,y}) -> {x,-y} */
1939 (conj (convert?:s (complex:s @0 @1)))
1940 (with { tree itype = TREE_TYPE (type); }
1941 (complex (convert:itype @0) (negate (convert:itype @1)))))
1943 /* BSWAP simplifications, transforms checked by gcc.dg/builtin-bswap-8.c. */
1944 (for bswap (BUILT_IN_BSWAP16 BUILT_IN_BSWAP32 BUILT_IN_BSWAP64)
1949 (bswap (bit_not (bswap @0)))
1951 (for bitop (bit_xor bit_ior bit_and)
1953 (bswap (bitop:c (bswap @0) @1))
1954 (bitop @0 (bswap @1)))))
1957 /* Combine COND_EXPRs and VEC_COND_EXPRs. */
1959 /* Simplify constant conditions.
1960 Only optimize constant conditions when the selected branch
1961 has the same type as the COND_EXPR. This avoids optimizing
1962 away "c ? x : throw", where the throw has a void type.
1963 Note that we cannot throw away the fold-const.c variant nor
1964 this one as we depend on doing this transform before possibly
1965 A ? B : B -> B triggers and the fold-const.c one can optimize
1966 0 ? A : B to B even if A has side-effects. Something
1967 genmatch cannot handle. */
1969 (cond INTEGER_CST@0 @1 @2)
1970 (if (integer_zerop (@0))
1971 (if (!VOID_TYPE_P (TREE_TYPE (@2)) || VOID_TYPE_P (type))
1973 (if (!VOID_TYPE_P (TREE_TYPE (@1)) || VOID_TYPE_P (type))
1976 (vec_cond VECTOR_CST@0 @1 @2)
1977 (if (integer_all_onesp (@0))
1979 (if (integer_zerop (@0))
1982 /* Simplification moved from fold_cond_expr_with_comparison. It may also
1984 /* This pattern implements two kinds simplification:
1987 (cond (cmp (convert1? x) c1) (convert2? x) c2) -> (minmax (x c)) if:
1988 1) Conversions are type widening from smaller type.
1989 2) Const c1 equals to c2 after canonicalizing comparison.
1990 3) Comparison has tree code LT, LE, GT or GE.
1991 This specific pattern is needed when (cmp (convert x) c) may not
1992 be simplified by comparison patterns because of multiple uses of
1993 x. It also makes sense here because simplifying across multiple
1994 referred var is always benefitial for complicated cases.
1997 (cond (eq (convert1? x) c1) (convert2? x) c2) -> (cond (eq x c1) c1 c2). */
1998 (for cmp (lt le gt ge eq)
2000 (cond (cmp (convert1? @1) INTEGER_CST@3) (convert2? @1) INTEGER_CST@2)
2003 tree from_type = TREE_TYPE (@1);
2004 tree c1_type = TREE_TYPE (@3), c2_type = TREE_TYPE (@2);
2005 enum tree_code code = ERROR_MARK;
2007 if (INTEGRAL_TYPE_P (from_type)
2008 && int_fits_type_p (@2, from_type)
2009 && (types_match (c1_type, from_type)
2010 || (TYPE_PRECISION (c1_type) > TYPE_PRECISION (from_type)
2011 && (TYPE_UNSIGNED (from_type)
2012 || TYPE_SIGN (c1_type) == TYPE_SIGN (from_type))))
2013 && (types_match (c2_type, from_type)
2014 || (TYPE_PRECISION (c2_type) > TYPE_PRECISION (from_type)
2015 && (TYPE_UNSIGNED (from_type)
2016 || TYPE_SIGN (c2_type) == TYPE_SIGN (from_type)))))
2020 if (wi::to_widest (@3) == (wi::to_widest (@2) - 1))
2022 /* X <= Y - 1 equals to X < Y. */
2025 /* X > Y - 1 equals to X >= Y. */
2029 if (wi::to_widest (@3) == (wi::to_widest (@2) + 1))
2031 /* X < Y + 1 equals to X <= Y. */
2034 /* X >= Y + 1 equals to X > Y. */
2038 if (code != ERROR_MARK
2039 || wi::to_widest (@2) == wi::to_widest (@3))
2041 if (cmp == LT_EXPR || cmp == LE_EXPR)
2043 if (cmp == GT_EXPR || cmp == GE_EXPR)
2047 /* Can do A == C1 ? A : C2 -> A == C1 ? C1 : C2? */
2048 else if (int_fits_type_p (@3, from_type))
2052 (if (code == MAX_EXPR)
2053 (convert (max @1 (convert @2)))
2054 (if (code == MIN_EXPR)
2055 (convert (min @1 (convert @2)))
2056 (if (code == EQ_EXPR)
2057 (convert (cond (eq @1 (convert @3))
2058 (convert:from_type @3) (convert:from_type @2)))))))))
2060 /* (cond (cmp (convert? x) c1) (op x c2) c3) -> (op (minmax x c1) c2) if:
2062 1) OP is PLUS or MINUS.
2063 2) CMP is LT, LE, GT or GE.
2064 3) C3 == (C1 op C2), and computation doesn't have undefined behavior.
2066 This pattern also handles special cases like:
2068 A) Operand x is a unsigned to signed type conversion and c1 is
2069 integer zero. In this case,
2070 (signed type)x < 0 <=> x > MAX_VAL(signed type)
2071 (signed type)x >= 0 <=> x <= MAX_VAL(signed type)
2072 B) Const c1 may not equal to (C3 op' C2). In this case we also
2073 check equality for (c1+1) and (c1-1) by adjusting comparison
2076 TODO: Though signed type is handled by this pattern, it cannot be
2077 simplified at the moment because C standard requires additional
2078 type promotion. In order to match&simplify it here, the IR needs
2079 to be cleaned up by other optimizers, i.e, VRP. */
2080 (for op (plus minus)
2081 (for cmp (lt le gt ge)
2083 (cond (cmp (convert? @X) INTEGER_CST@1) (op @X INTEGER_CST@2) INTEGER_CST@3)
2084 (with { tree from_type = TREE_TYPE (@X), to_type = TREE_TYPE (@1); }
2085 (if (types_match (from_type, to_type)
2086 /* Check if it is special case A). */
2087 || (TYPE_UNSIGNED (from_type)
2088 && !TYPE_UNSIGNED (to_type)
2089 && TYPE_PRECISION (from_type) == TYPE_PRECISION (to_type)
2090 && integer_zerop (@1)
2091 && (cmp == LT_EXPR || cmp == GE_EXPR)))
2094 bool overflow = false;
2095 enum tree_code code, cmp_code = cmp;
2096 wide_int real_c1, c1 = @1, c2 = @2, c3 = @3;
2097 signop sgn = TYPE_SIGN (from_type);
2099 /* Handle special case A), given x of unsigned type:
2100 ((signed type)x < 0) <=> (x > MAX_VAL(signed type))
2101 ((signed type)x >= 0) <=> (x <= MAX_VAL(signed type)) */
2102 if (!types_match (from_type, to_type))
2104 if (cmp_code == LT_EXPR)
2106 if (cmp_code == GE_EXPR)
2108 c1 = wi::max_value (to_type);
2110 /* To simplify this pattern, we require c3 = (c1 op c2). Here we
2111 compute (c3 op' c2) and check if it equals to c1 with op' being
2112 the inverted operator of op. Make sure overflow doesn't happen
2113 if it is undefined. */
2114 if (op == PLUS_EXPR)
2115 real_c1 = wi::sub (c3, c2, sgn, &overflow);
2117 real_c1 = wi::add (c3, c2, sgn, &overflow);
2120 if (!overflow || !TYPE_OVERFLOW_UNDEFINED (from_type))
2122 /* Check if c1 equals to real_c1. Boundary condition is handled
2123 by adjusting comparison operation if necessary. */
2124 if (!wi::cmp (wi::sub (real_c1, 1, sgn, &overflow), c1, sgn)
2127 /* X <= Y - 1 equals to X < Y. */
2128 if (cmp_code == LE_EXPR)
2130 /* X > Y - 1 equals to X >= Y. */
2131 if (cmp_code == GT_EXPR)
2134 if (!wi::cmp (wi::add (real_c1, 1, sgn, &overflow), c1, sgn)
2137 /* X < Y + 1 equals to X <= Y. */
2138 if (cmp_code == LT_EXPR)
2140 /* X >= Y + 1 equals to X > Y. */
2141 if (cmp_code == GE_EXPR)
2144 if (code != cmp_code || !wi::cmp (real_c1, c1, sgn))
2146 if (cmp_code == LT_EXPR || cmp_code == LE_EXPR)
2148 if (cmp_code == GT_EXPR || cmp_code == GE_EXPR)
2153 (if (code == MAX_EXPR)
2154 (op (max @X { wide_int_to_tree (from_type, real_c1); })
2155 { wide_int_to_tree (from_type, c2); })
2156 (if (code == MIN_EXPR)
2157 (op (min @X { wide_int_to_tree (from_type, real_c1); })
2158 { wide_int_to_tree (from_type, c2); })))))))))
2160 (for cnd (cond vec_cond)
2161 /* A ? B : (A ? X : C) -> A ? B : C. */
2163 (cnd @0 (cnd @0 @1 @2) @3)
2166 (cnd @0 @1 (cnd @0 @2 @3))
2168 /* A ? B : (!A ? C : X) -> A ? B : C. */
2169 /* ??? This matches embedded conditions open-coded because genmatch
2170 would generate matching code for conditions in separate stmts only.
2171 The following is still important to merge then and else arm cases
2172 from if-conversion. */
2174 (cnd @0 @1 (cnd @2 @3 @4))
2175 (if (COMPARISON_CLASS_P (@0)
2176 && COMPARISON_CLASS_P (@2)
2177 && invert_tree_comparison
2178 (TREE_CODE (@0), HONOR_NANS (TREE_OPERAND (@0, 0))) == TREE_CODE (@2)
2179 && operand_equal_p (TREE_OPERAND (@0, 0), TREE_OPERAND (@2, 0), 0)
2180 && operand_equal_p (TREE_OPERAND (@0, 1), TREE_OPERAND (@2, 1), 0))
2183 (cnd @0 (cnd @1 @2 @3) @4)
2184 (if (COMPARISON_CLASS_P (@0)
2185 && COMPARISON_CLASS_P (@1)
2186 && invert_tree_comparison
2187 (TREE_CODE (@0), HONOR_NANS (TREE_OPERAND (@0, 0))) == TREE_CODE (@1)
2188 && operand_equal_p (TREE_OPERAND (@0, 0), TREE_OPERAND (@1, 0), 0)
2189 && operand_equal_p (TREE_OPERAND (@0, 1), TREE_OPERAND (@1, 1), 0))
2192 /* A ? B : B -> B. */
2197 /* !A ? B : C -> A ? C : B. */
2199 (cnd (logical_inverted_value truth_valued_p@0) @1 @2)
2202 /* A + (B vcmp C ? 1 : 0) -> A - (B vcmp C ? -1 : 0), since vector comparisons
2203 return all -1 or all 0 results. */
2204 /* ??? We could instead convert all instances of the vec_cond to negate,
2205 but that isn't necessarily a win on its own. */
2207 (plus:c @3 (view_convert? (vec_cond:s @0 integer_each_onep@1 integer_zerop@2)))
2208 (if (VECTOR_TYPE_P (type)
2209 && TYPE_VECTOR_SUBPARTS (type) == TYPE_VECTOR_SUBPARTS (TREE_TYPE (@1))
2210 && (TYPE_MODE (TREE_TYPE (type))
2211 == TYPE_MODE (TREE_TYPE (TREE_TYPE (@1)))))
2212 (minus @3 (view_convert (vec_cond @0 (negate @1) @2)))))
2214 /* ... likewise A - (B vcmp C ? 1 : 0) -> A + (B vcmp C ? -1 : 0). */
2216 (minus @3 (view_convert? (vec_cond:s @0 integer_each_onep@1 integer_zerop@2)))
2217 (if (VECTOR_TYPE_P (type)
2218 && TYPE_VECTOR_SUBPARTS (type) == TYPE_VECTOR_SUBPARTS (TREE_TYPE (@1))
2219 && (TYPE_MODE (TREE_TYPE (type))
2220 == TYPE_MODE (TREE_TYPE (TREE_TYPE (@1)))))
2221 (plus @3 (view_convert (vec_cond @0 (negate @1) @2)))))
2224 /* Simplifications of comparisons. */
2226 /* See if we can reduce the magnitude of a constant involved in a
2227 comparison by changing the comparison code. This is a canonicalization
2228 formerly done by maybe_canonicalize_comparison_1. */
2232 (cmp @0 INTEGER_CST@1)
2233 (if (tree_int_cst_sgn (@1) == -1)
2234 (acmp @0 { wide_int_to_tree (TREE_TYPE (@1), wi::add (@1, 1)); }))))
2238 (cmp @0 INTEGER_CST@1)
2239 (if (tree_int_cst_sgn (@1) == 1)
2240 (acmp @0 { wide_int_to_tree (TREE_TYPE (@1), wi::sub (@1, 1)); }))))
2243 /* We can simplify a logical negation of a comparison to the
2244 inverted comparison. As we cannot compute an expression
2245 operator using invert_tree_comparison we have to simulate
2246 that with expression code iteration. */
2247 (for cmp (tcc_comparison)
2248 icmp (inverted_tcc_comparison)
2249 ncmp (inverted_tcc_comparison_with_nans)
2250 /* Ideally we'd like to combine the following two patterns
2251 and handle some more cases by using
2252 (logical_inverted_value (cmp @0 @1))
2253 here but for that genmatch would need to "inline" that.
2254 For now implement what forward_propagate_comparison did. */
2256 (bit_not (cmp @0 @1))
2257 (if (VECTOR_TYPE_P (type)
2258 || (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1))
2259 /* Comparison inversion may be impossible for trapping math,
2260 invert_tree_comparison will tell us. But we can't use
2261 a computed operator in the replacement tree thus we have
2262 to play the trick below. */
2263 (with { enum tree_code ic = invert_tree_comparison
2264 (cmp, HONOR_NANS (@0)); }
2270 (bit_xor (cmp @0 @1) integer_truep)
2271 (with { enum tree_code ic = invert_tree_comparison
2272 (cmp, HONOR_NANS (@0)); }
2278 /* Transform comparisons of the form X - Y CMP 0 to X CMP Y.
2279 ??? The transformation is valid for the other operators if overflow
2280 is undefined for the type, but performing it here badly interacts
2281 with the transformation in fold_cond_expr_with_comparison which
2282 attempts to synthetize ABS_EXPR. */
2285 (cmp (minus@2 @0 @1) integer_zerop)
2286 (if (single_use (@2))
2289 /* Transform comparisons of the form X * C1 CMP 0 to X CMP 0 in the
2290 signed arithmetic case. That form is created by the compiler
2291 often enough for folding it to be of value. One example is in
2292 computing loop trip counts after Operator Strength Reduction. */
2293 (for cmp (simple_comparison)
2294 scmp (swapped_simple_comparison)
2296 (cmp (mult@3 @0 INTEGER_CST@1) integer_zerop@2)
2297 /* Handle unfolded multiplication by zero. */
2298 (if (integer_zerop (@1))
2300 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2301 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2303 /* If @1 is negative we swap the sense of the comparison. */
2304 (if (tree_int_cst_sgn (@1) < 0)
2308 /* Simplify comparison of something with itself. For IEEE
2309 floating-point, we can only do some of these simplifications. */
2313 (if (! FLOAT_TYPE_P (TREE_TYPE (@0))
2314 || ! HONOR_NANS (@0))
2315 { constant_boolean_node (true, type); }
2316 (if (cmp != EQ_EXPR)
2322 || ! FLOAT_TYPE_P (TREE_TYPE (@0))
2323 || ! HONOR_NANS (@0))
2324 { constant_boolean_node (false, type); })))
2325 (for cmp (unle unge uneq)
2328 { constant_boolean_node (true, type); }))
2329 (for cmp (unlt ungt)
2335 (if (!flag_trapping_math)
2336 { constant_boolean_node (false, type); }))
2338 /* Fold ~X op ~Y as Y op X. */
2339 (for cmp (simple_comparison)
2341 (cmp (bit_not@2 @0) (bit_not@3 @1))
2342 (if (single_use (@2) && single_use (@3))
2345 /* Fold ~X op C as X op' ~C, where op' is the swapped comparison. */
2346 (for cmp (simple_comparison)
2347 scmp (swapped_simple_comparison)
2349 (cmp (bit_not@2 @0) CONSTANT_CLASS_P@1)
2350 (if (single_use (@2)
2351 && (TREE_CODE (@1) == INTEGER_CST || TREE_CODE (@1) == VECTOR_CST))
2352 (scmp @0 (bit_not @1)))))
2354 (for cmp (simple_comparison)
2355 /* Fold (double)float1 CMP (double)float2 into float1 CMP float2. */
2357 (cmp (convert@2 @0) (convert? @1))
2358 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
2359 && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@2))
2360 == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0)))
2361 && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@2))
2362 == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1))))
2365 tree type1 = TREE_TYPE (@1);
2366 if (TREE_CODE (@1) == REAL_CST && !DECIMAL_FLOAT_TYPE_P (type1))
2368 REAL_VALUE_TYPE orig = TREE_REAL_CST (@1);
2369 if (TYPE_PRECISION (type1) > TYPE_PRECISION (float_type_node)
2370 && exact_real_truncate (TYPE_MODE (float_type_node), &orig))
2371 type1 = float_type_node;
2372 if (TYPE_PRECISION (type1) > TYPE_PRECISION (double_type_node)
2373 && exact_real_truncate (TYPE_MODE (double_type_node), &orig))
2374 type1 = double_type_node;
2377 = (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type1)
2378 ? TREE_TYPE (@0) : type1);
2380 (if (TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (newtype))
2381 (cmp (convert:newtype @0) (convert:newtype @1))))))
2385 /* IEEE doesn't distinguish +0 and -0 in comparisons. */
2387 /* a CMP (-0) -> a CMP 0 */
2388 (if (REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@1)))
2389 (cmp @0 { build_real (TREE_TYPE (@1), dconst0); }))
2390 /* x != NaN is always true, other ops are always false. */
2391 (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
2392 && ! HONOR_SNANS (@1))
2393 { constant_boolean_node (cmp == NE_EXPR, type); })
2394 /* Fold comparisons against infinity. */
2395 (if (REAL_VALUE_ISINF (TREE_REAL_CST (@1))
2396 && MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (@1))))
2399 REAL_VALUE_TYPE max;
2400 enum tree_code code = cmp;
2401 bool neg = REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1));
2403 code = swap_tree_comparison (code);
2406 /* x > +Inf is always false, if with ignore sNANs. */
2407 (if (code == GT_EXPR
2408 && ! HONOR_SNANS (@0))
2409 { constant_boolean_node (false, type); })
2410 (if (code == LE_EXPR)
2411 /* x <= +Inf is always true, if we don't case about NaNs. */
2412 (if (! HONOR_NANS (@0))
2413 { constant_boolean_node (true, type); }
2414 /* x <= +Inf is the same as x == x, i.e. !isnan(x). */
2416 /* x == +Inf and x >= +Inf are always equal to x > DBL_MAX. */
2417 (if (code == EQ_EXPR || code == GE_EXPR)
2418 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
2420 (lt @0 { build_real (TREE_TYPE (@0), max); })
2421 (gt @0 { build_real (TREE_TYPE (@0), max); }))))
2422 /* x < +Inf is always equal to x <= DBL_MAX. */
2423 (if (code == LT_EXPR)
2424 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
2426 (ge @0 { build_real (TREE_TYPE (@0), max); })
2427 (le @0 { build_real (TREE_TYPE (@0), max); }))))
2428 /* x != +Inf is always equal to !(x > DBL_MAX). */
2429 (if (code == NE_EXPR)
2430 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
2431 (if (! HONOR_NANS (@0))
2433 (ge @0 { build_real (TREE_TYPE (@0), max); })
2434 (le @0 { build_real (TREE_TYPE (@0), max); }))
2436 (bit_xor (lt @0 { build_real (TREE_TYPE (@0), max); })
2437 { build_one_cst (type); })
2438 (bit_xor (gt @0 { build_real (TREE_TYPE (@0), max); })
2439 { build_one_cst (type); }))))))))))
2441 /* If this is a comparison of a real constant with a PLUS_EXPR
2442 or a MINUS_EXPR of a real constant, we can convert it into a
2443 comparison with a revised real constant as long as no overflow
2444 occurs when unsafe_math_optimizations are enabled. */
2445 (if (flag_unsafe_math_optimizations)
2446 (for op (plus minus)
2448 (cmp (op @0 REAL_CST@1) REAL_CST@2)
2451 tree tem = const_binop (op == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR,
2452 TREE_TYPE (@1), @2, @1);
2454 (if (tem && !TREE_OVERFLOW (tem))
2455 (cmp @0 { tem; }))))))
2457 /* Likewise, we can simplify a comparison of a real constant with
2458 a MINUS_EXPR whose first operand is also a real constant, i.e.
2459 (c1 - x) < c2 becomes x > c1-c2. Reordering is allowed on
2460 floating-point types only if -fassociative-math is set. */
2461 (if (flag_associative_math)
2463 (cmp (minus REAL_CST@0 @1) REAL_CST@2)
2464 (with { tree tem = const_binop (MINUS_EXPR, TREE_TYPE (@1), @0, @2); }
2465 (if (tem && !TREE_OVERFLOW (tem))
2466 (cmp { tem; } @1)))))
2468 /* Fold comparisons against built-in math functions. */
2469 (if (flag_unsafe_math_optimizations
2470 && ! flag_errno_math)
2473 (cmp (sq @0) REAL_CST@1)
2475 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
2477 /* sqrt(x) < y is always false, if y is negative. */
2478 (if (cmp == EQ_EXPR || cmp == LT_EXPR || cmp == LE_EXPR)
2479 { constant_boolean_node (false, type); })
2480 /* sqrt(x) > y is always true, if y is negative and we
2481 don't care about NaNs, i.e. negative values of x. */
2482 (if (cmp == NE_EXPR || !HONOR_NANS (@0))
2483 { constant_boolean_node (true, type); })
2484 /* sqrt(x) > y is the same as x >= 0, if y is negative. */
2485 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })))
2486 (if (real_equal (TREE_REAL_CST_PTR (@1), &dconst0))
2488 /* sqrt(x) < 0 is always false. */
2489 (if (cmp == LT_EXPR)
2490 { constant_boolean_node (false, type); })
2491 /* sqrt(x) >= 0 is always true if we don't care about NaNs. */
2492 (if (cmp == GE_EXPR && !HONOR_NANS (@0))
2493 { constant_boolean_node (true, type); })
2494 /* sqrt(x) <= 0 -> x == 0. */
2495 (if (cmp == LE_EXPR)
2497 /* Otherwise sqrt(x) cmp 0 -> x cmp 0. Here cmp can be >=, >,
2498 == or !=. In the last case:
2500 (sqrt(x) != 0) == (NaN != 0) == true == (x != 0)
2502 if x is negative or NaN. Due to -funsafe-math-optimizations,
2503 the results for other x follow from natural arithmetic. */
2505 (if (cmp == GT_EXPR || cmp == GE_EXPR)
2509 real_arithmetic (&c2, MULT_EXPR,
2510 &TREE_REAL_CST (@1), &TREE_REAL_CST (@1));
2511 real_convert (&c2, TYPE_MODE (TREE_TYPE (@0)), &c2);
2513 (if (REAL_VALUE_ISINF (c2))
2514 /* sqrt(x) > y is x == +Inf, when y is very large. */
2515 (if (HONOR_INFINITIES (@0))
2516 (eq @0 { build_real (TREE_TYPE (@0), c2); })
2517 { constant_boolean_node (false, type); })
2518 /* sqrt(x) > c is the same as x > c*c. */
2519 (cmp @0 { build_real (TREE_TYPE (@0), c2); }))))
2520 (if (cmp == LT_EXPR || cmp == LE_EXPR)
2524 real_arithmetic (&c2, MULT_EXPR,
2525 &TREE_REAL_CST (@1), &TREE_REAL_CST (@1));
2526 real_convert (&c2, TYPE_MODE (TREE_TYPE (@0)), &c2);
2528 (if (REAL_VALUE_ISINF (c2))
2530 /* sqrt(x) < y is always true, when y is a very large
2531 value and we don't care about NaNs or Infinities. */
2532 (if (! HONOR_NANS (@0) && ! HONOR_INFINITIES (@0))
2533 { constant_boolean_node (true, type); })
2534 /* sqrt(x) < y is x != +Inf when y is very large and we
2535 don't care about NaNs. */
2536 (if (! HONOR_NANS (@0))
2537 (ne @0 { build_real (TREE_TYPE (@0), c2); }))
2538 /* sqrt(x) < y is x >= 0 when y is very large and we
2539 don't care about Infinities. */
2540 (if (! HONOR_INFINITIES (@0))
2541 (ge @0 { build_real (TREE_TYPE (@0), dconst0); }))
2542 /* sqrt(x) < y is x >= 0 && x != +Inf, when y is large. */
2545 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
2546 (ne @0 { build_real (TREE_TYPE (@0), c2); }))))
2547 /* sqrt(x) < c is the same as x < c*c, if we ignore NaNs. */
2548 (if (! HONOR_NANS (@0))
2549 (cmp @0 { build_real (TREE_TYPE (@0), c2); })
2550 /* sqrt(x) < c is the same as x >= 0 && x < c*c. */
2553 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
2554 (cmp @0 { build_real (TREE_TYPE (@0), c2); }))))))))))))
2556 /* Fold A /[ex] B CMP C to A CMP B * C. */
2559 (cmp (exact_div @0 @1) INTEGER_CST@2)
2560 (if (!integer_zerop (@1))
2561 (if (wi::eq_p (@2, 0))
2563 (if (TREE_CODE (@1) == INTEGER_CST)
2567 wide_int prod = wi::mul (@2, @1, TYPE_SIGN (TREE_TYPE (@1)), &ovf);
2570 { constant_boolean_node (cmp == NE_EXPR, type); }
2571 (cmp @0 { wide_int_to_tree (TREE_TYPE (@0), prod); }))))))))
2572 (for cmp (lt le gt ge)
2574 (cmp (exact_div @0 INTEGER_CST@1) INTEGER_CST@2)
2575 (if (wi::gt_p (@1, 0, TYPE_SIGN (TREE_TYPE (@1))))
2579 wide_int prod = wi::mul (@2, @1, TYPE_SIGN (TREE_TYPE (@1)), &ovf);
2582 { constant_boolean_node (wi::lt_p (@2, 0, TYPE_SIGN (TREE_TYPE (@2)))
2583 != (cmp == LT_EXPR || cmp == LE_EXPR), type); }
2584 (cmp @0 { wide_int_to_tree (TREE_TYPE (@0), prod); }))))))
2586 /* Unordered tests if either argument is a NaN. */
2588 (bit_ior (unordered @0 @0) (unordered @1 @1))
2589 (if (types_match (@0, @1))
2592 (bit_and (ordered @0 @0) (ordered @1 @1))
2593 (if (types_match (@0, @1))
2596 (bit_ior:c (unordered @0 @0) (unordered:c@2 @0 @1))
2599 (bit_and:c (ordered @0 @0) (ordered:c@2 @0 @1))
2602 /* Simple range test simplifications. */
2603 /* A < B || A >= B -> true. */
2604 (for test1 (lt le le le ne ge)
2605 test2 (ge gt ge ne eq ne)
2607 (bit_ior:c (test1 @0 @1) (test2 @0 @1))
2608 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2609 || VECTOR_INTEGER_TYPE_P (TREE_TYPE (@0)))
2610 { constant_boolean_node (true, type); })))
2611 /* A < B && A >= B -> false. */
2612 (for test1 (lt lt lt le ne eq)
2613 test2 (ge gt eq gt eq gt)
2615 (bit_and:c (test1 @0 @1) (test2 @0 @1))
2616 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2617 || VECTOR_INTEGER_TYPE_P (TREE_TYPE (@0)))
2618 { constant_boolean_node (false, type); })))
2620 /* -A CMP -B -> B CMP A. */
2621 (for cmp (tcc_comparison)
2622 scmp (swapped_tcc_comparison)
2624 (cmp (negate @0) (negate @1))
2625 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
2626 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2627 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
2630 (cmp (negate @0) CONSTANT_CLASS_P@1)
2631 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
2632 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2633 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
2634 (with { tree tem = const_unop (NEGATE_EXPR, TREE_TYPE (@0), @1); }
2635 (if (tem && !TREE_OVERFLOW (tem))
2636 (scmp @0 { tem; }))))))
2638 /* Convert ABS_EXPR<x> == 0 or ABS_EXPR<x> != 0 to x == 0 or x != 0. */
2641 (op (abs @0) zerop@1)
2644 /* From fold_sign_changed_comparison and fold_widened_comparison. */
2645 (for cmp (simple_comparison)
2647 (cmp (convert@0 @00) (convert?@1 @10))
2648 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2649 /* Disable this optimization if we're casting a function pointer
2650 type on targets that require function pointer canonicalization. */
2651 && !(targetm.have_canonicalize_funcptr_for_compare ()
2652 && TREE_CODE (TREE_TYPE (@00)) == POINTER_TYPE
2653 && TREE_CODE (TREE_TYPE (TREE_TYPE (@00))) == FUNCTION_TYPE)
2655 (if (TYPE_PRECISION (TREE_TYPE (@00)) == TYPE_PRECISION (TREE_TYPE (@0))
2656 && (TREE_CODE (@10) == INTEGER_CST
2657 || (@1 != @10 && types_match (TREE_TYPE (@10), TREE_TYPE (@00))))
2658 && (TYPE_UNSIGNED (TREE_TYPE (@00)) == TYPE_UNSIGNED (TREE_TYPE (@0))
2661 && (POINTER_TYPE_P (TREE_TYPE (@00)) == POINTER_TYPE_P (TREE_TYPE (@0))))
2662 /* ??? The special-casing of INTEGER_CST conversion was in the original
2663 code and here to avoid a spurious overflow flag on the resulting
2664 constant which fold_convert produces. */
2665 (if (TREE_CODE (@1) == INTEGER_CST)
2666 (cmp @00 { force_fit_type (TREE_TYPE (@00), wi::to_widest (@1), 0,
2667 TREE_OVERFLOW (@1)); })
2668 (cmp @00 (convert @1)))
2670 (if (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (TREE_TYPE (@00)))
2671 /* If possible, express the comparison in the shorter mode. */
2672 (if ((cmp == EQ_EXPR || cmp == NE_EXPR
2673 || TYPE_UNSIGNED (TREE_TYPE (@0)) == TYPE_UNSIGNED (TREE_TYPE (@00))
2674 || (!TYPE_UNSIGNED (TREE_TYPE (@0))
2675 && TYPE_UNSIGNED (TREE_TYPE (@00))))
2676 && (types_match (TREE_TYPE (@10), TREE_TYPE (@00))
2677 || ((TYPE_PRECISION (TREE_TYPE (@00))
2678 >= TYPE_PRECISION (TREE_TYPE (@10)))
2679 && (TYPE_UNSIGNED (TREE_TYPE (@00))
2680 == TYPE_UNSIGNED (TREE_TYPE (@10))))
2681 || (TREE_CODE (@10) == INTEGER_CST
2682 && INTEGRAL_TYPE_P (TREE_TYPE (@00))
2683 && int_fits_type_p (@10, TREE_TYPE (@00)))))
2684 (cmp @00 (convert @10))
2685 (if (TREE_CODE (@10) == INTEGER_CST
2686 && INTEGRAL_TYPE_P (TREE_TYPE (@00))
2687 && !int_fits_type_p (@10, TREE_TYPE (@00)))
2690 tree min = lower_bound_in_type (TREE_TYPE (@10), TREE_TYPE (@00));
2691 tree max = upper_bound_in_type (TREE_TYPE (@10), TREE_TYPE (@00));
2692 bool above = integer_nonzerop (const_binop (LT_EXPR, type, max, @10));
2693 bool below = integer_nonzerop (const_binop (LT_EXPR, type, @10, min));
2695 (if (above || below)
2696 (if (cmp == EQ_EXPR || cmp == NE_EXPR)
2697 { constant_boolean_node (cmp == EQ_EXPR ? false : true, type); }
2698 (if (cmp == LT_EXPR || cmp == LE_EXPR)
2699 { constant_boolean_node (above ? true : false, type); }
2700 (if (cmp == GT_EXPR || cmp == GE_EXPR)
2701 { constant_boolean_node (above ? false : true, type); }))))))))))))
2704 /* A local variable can never be pointed to by
2705 the default SSA name of an incoming parameter.
2706 SSA names are canonicalized to 2nd place. */
2708 (cmp addr@0 SSA_NAME@1)
2709 (if (SSA_NAME_IS_DEFAULT_DEF (@1)
2710 && TREE_CODE (SSA_NAME_VAR (@1)) == PARM_DECL)
2711 (with { tree base = get_base_address (TREE_OPERAND (@0, 0)); }
2712 (if (TREE_CODE (base) == VAR_DECL
2713 && auto_var_in_fn_p (base, current_function_decl))
2714 (if (cmp == NE_EXPR)
2715 { constant_boolean_node (true, type); }
2716 { constant_boolean_node (false, type); }))))))
2718 /* Equality compare simplifications from fold_binary */
2721 /* If we have (A | C) == D where C & ~D != 0, convert this into 0.
2722 Similarly for NE_EXPR. */
2724 (cmp (convert?@3 (bit_ior @0 INTEGER_CST@1)) INTEGER_CST@2)
2725 (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0))
2726 && wi::bit_and_not (@1, @2) != 0)
2727 { constant_boolean_node (cmp == NE_EXPR, type); }))
2729 /* (X ^ Y) == 0 becomes X == Y, and (X ^ Y) != 0 becomes X != Y. */
2731 (cmp (bit_xor @0 @1) integer_zerop)
2734 /* (X ^ Y) == Y becomes X == 0.
2735 Likewise (X ^ Y) == X becomes Y == 0. */
2737 (cmp:c (bit_xor:c @0 @1) @0)
2738 (cmp @1 { build_zero_cst (TREE_TYPE (@1)); }))
2740 /* (X ^ C1) op C2 can be rewritten as X op (C1 ^ C2). */
2742 (cmp (convert?@3 (bit_xor @0 INTEGER_CST@1)) INTEGER_CST@2)
2743 (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0)))
2744 (cmp @0 (bit_xor @1 (convert @2)))))
2747 (cmp (convert? addr@0) integer_zerop)
2748 (if (tree_single_nonzero_warnv_p (@0, NULL))
2749 { constant_boolean_node (cmp == NE_EXPR, type); })))
2751 /* If we have (A & C) == C where C is a power of 2, convert this into
2752 (A & C) != 0. Similarly for NE_EXPR. */
2756 (cmp (bit_and@2 @0 integer_pow2p@1) @1)
2757 (icmp @2 { build_zero_cst (TREE_TYPE (@0)); })))
2759 /* If we have (A & C) != 0 ? D : 0 where C and D are powers of 2,
2760 convert this into a shift followed by ANDing with D. */
2763 (ne (bit_and @0 integer_pow2p@1) integer_zerop)
2764 integer_pow2p@2 integer_zerop)
2766 int shift = wi::exact_log2 (@2) - wi::exact_log2 (@1);
2770 (lshift (convert @0) { build_int_cst (integer_type_node, shift); }) @2)
2772 (convert (rshift @0 { build_int_cst (integer_type_node, -shift); })) @2))))
2774 /* If we have (A & C) != 0 where C is the sign bit of A, convert
2775 this into A < 0. Similarly for (A & C) == 0 into A >= 0. */
2779 (cmp (bit_and (convert?@2 @0) integer_pow2p@1) integer_zerop)
2780 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2781 && (TYPE_PRECISION (TREE_TYPE (@0))
2782 == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (@0))))
2783 && element_precision (@2) >= element_precision (@0)
2784 && wi::only_sign_bit_p (@1, element_precision (@0)))
2785 (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
2786 (ncmp (convert:stype @0) { build_zero_cst (stype); })))))
2788 /* If we have A < 0 ? C : 0 where C is a power of 2, convert
2789 this into a right shift or sign extension followed by ANDing with C. */
2792 (lt @0 integer_zerop)
2793 integer_pow2p@1 integer_zerop)
2794 (if (!TYPE_UNSIGNED (TREE_TYPE (@0)))
2796 int shift = element_precision (@0) - wi::exact_log2 (@1) - 1;
2800 (convert (rshift @0 { build_int_cst (integer_type_node, shift); }))
2802 /* Otherwise ctype must be wider than TREE_TYPE (@0) and pure
2803 sign extension followed by AND with C will achieve the effect. */
2804 (bit_and (convert @0) @1)))))
2806 /* When the addresses are not directly of decls compare base and offset.
2807 This implements some remaining parts of fold_comparison address
2808 comparisons but still no complete part of it. Still it is good
2809 enough to make fold_stmt not regress when not dispatching to fold_binary. */
2810 (for cmp (simple_comparison)
2812 (cmp (convert1?@2 addr@0) (convert2? addr@1))
2815 HOST_WIDE_INT off0, off1;
2816 tree base0 = get_addr_base_and_unit_offset (TREE_OPERAND (@0, 0), &off0);
2817 tree base1 = get_addr_base_and_unit_offset (TREE_OPERAND (@1, 0), &off1);
2818 if (base0 && TREE_CODE (base0) == MEM_REF)
2820 off0 += mem_ref_offset (base0).to_short_addr ();
2821 base0 = TREE_OPERAND (base0, 0);
2823 if (base1 && TREE_CODE (base1) == MEM_REF)
2825 off1 += mem_ref_offset (base1).to_short_addr ();
2826 base1 = TREE_OPERAND (base1, 0);
2829 (if (base0 && base1)
2833 /* Punt in GENERIC on variables with value expressions;
2834 the value expressions might point to fields/elements
2835 of other vars etc. */
2837 && ((VAR_P (base0) && DECL_HAS_VALUE_EXPR_P (base0))
2838 || (VAR_P (base1) && DECL_HAS_VALUE_EXPR_P (base1))))
2840 else if (decl_in_symtab_p (base0)
2841 && decl_in_symtab_p (base1))
2842 equal = symtab_node::get_create (base0)
2843 ->equal_address_to (symtab_node::get_create (base1));
2844 else if ((DECL_P (base0)
2845 || TREE_CODE (base0) == SSA_NAME
2846 || TREE_CODE (base0) == STRING_CST)
2848 || TREE_CODE (base1) == SSA_NAME
2849 || TREE_CODE (base1) == STRING_CST))
2850 equal = (base0 == base1);
2853 && (cmp == EQ_EXPR || cmp == NE_EXPR
2854 /* If the offsets are equal we can ignore overflow. */
2856 || POINTER_TYPE_OVERFLOW_UNDEFINED
2857 /* Or if we compare using pointers to decls or strings. */
2858 || (POINTER_TYPE_P (TREE_TYPE (@2))
2859 && (DECL_P (base0) || TREE_CODE (base0) == STRING_CST))))
2861 (if (cmp == EQ_EXPR)
2862 { constant_boolean_node (off0 == off1, type); })
2863 (if (cmp == NE_EXPR)
2864 { constant_boolean_node (off0 != off1, type); })
2865 (if (cmp == LT_EXPR)
2866 { constant_boolean_node (off0 < off1, type); })
2867 (if (cmp == LE_EXPR)
2868 { constant_boolean_node (off0 <= off1, type); })
2869 (if (cmp == GE_EXPR)
2870 { constant_boolean_node (off0 >= off1, type); })
2871 (if (cmp == GT_EXPR)
2872 { constant_boolean_node (off0 > off1, type); }))
2874 && DECL_P (base0) && DECL_P (base1)
2875 /* If we compare this as integers require equal offset. */
2876 && (!INTEGRAL_TYPE_P (TREE_TYPE (@2))
2879 (if (cmp == EQ_EXPR)
2880 { constant_boolean_node (false, type); })
2881 (if (cmp == NE_EXPR)
2882 { constant_boolean_node (true, type); })))))))))
2884 /* Simplify pointer equality compares using PTA. */
2888 (if (POINTER_TYPE_P (TREE_TYPE (@0))
2889 && ptrs_compare_unequal (@0, @1))
2890 { neeq == EQ_EXPR ? boolean_false_node : boolean_true_node; })))
2892 /* PR70920: Transform (intptr_t)x eq/ne CST to x eq/ne (typeof x) CST.
2893 and (typeof ptr_cst) x eq/ne ptr_cst to x eq/ne (typeof x) CST.
2894 Disable the transform if either operand is pointer to function.
2895 This broke pr22051-2.c for arm where function pointer
2896 canonicalizaion is not wanted. */
2900 (cmp (convert @0) INTEGER_CST@1)
2901 (if ((POINTER_TYPE_P (TREE_TYPE (@0)) && !FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@0)))
2902 && INTEGRAL_TYPE_P (TREE_TYPE (@1)))
2903 || (INTEGRAL_TYPE_P (TREE_TYPE (@0)) && POINTER_TYPE_P (TREE_TYPE (@1))
2904 && !FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@1)))))
2905 (cmp @0 (convert @1)))))
2907 /* Non-equality compare simplifications from fold_binary */
2908 (for cmp (lt gt le ge)
2909 /* Comparisons with the highest or lowest possible integer of
2910 the specified precision will have known values. */
2912 (cmp (convert?@2 @0) INTEGER_CST@1)
2913 (if ((INTEGRAL_TYPE_P (TREE_TYPE (@1)) || POINTER_TYPE_P (TREE_TYPE (@1)))
2914 && tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@0)))
2917 tree arg1_type = TREE_TYPE (@1);
2918 unsigned int prec = TYPE_PRECISION (arg1_type);
2919 wide_int max = wi::max_value (arg1_type);
2920 wide_int signed_max = wi::max_value (prec, SIGNED);
2921 wide_int min = wi::min_value (arg1_type);
2924 (if (wi::eq_p (@1, max))
2926 (if (cmp == GT_EXPR)
2927 { constant_boolean_node (false, type); })
2928 (if (cmp == GE_EXPR)
2930 (if (cmp == LE_EXPR)
2931 { constant_boolean_node (true, type); })
2932 (if (cmp == LT_EXPR)
2934 (if (wi::eq_p (@1, min))
2936 (if (cmp == LT_EXPR)
2937 { constant_boolean_node (false, type); })
2938 (if (cmp == LE_EXPR)
2940 (if (cmp == GE_EXPR)
2941 { constant_boolean_node (true, type); })
2942 (if (cmp == GT_EXPR)
2944 (if (wi::eq_p (@1, max - 1))
2946 (if (cmp == GT_EXPR)
2947 (eq @2 { wide_int_to_tree (TREE_TYPE (@1), wi::add (@1, 1)); }))
2948 (if (cmp == LE_EXPR)
2949 (ne @2 { wide_int_to_tree (TREE_TYPE (@1), wi::add (@1, 1)); }))))
2950 (if (wi::eq_p (@1, min + 1))
2952 (if (cmp == GE_EXPR)
2953 (ne @2 { wide_int_to_tree (TREE_TYPE (@1), wi::sub (@1, 1)); }))
2954 (if (cmp == LT_EXPR)
2955 (eq @2 { wide_int_to_tree (TREE_TYPE (@1), wi::sub (@1, 1)); }))))
2956 (if (wi::eq_p (@1, signed_max)
2957 && TYPE_UNSIGNED (arg1_type)
2958 /* We will flip the signedness of the comparison operator
2959 associated with the mode of @1, so the sign bit is
2960 specified by this mode. Check that @1 is the signed
2961 max associated with this sign bit. */
2962 && prec == GET_MODE_PRECISION (TYPE_MODE (arg1_type))
2963 /* signed_type does not work on pointer types. */
2964 && INTEGRAL_TYPE_P (arg1_type))
2965 /* The following case also applies to X < signed_max+1
2966 and X >= signed_max+1 because previous transformations. */
2967 (if (cmp == LE_EXPR || cmp == GT_EXPR)
2968 (with { tree st = signed_type_for (arg1_type); }
2969 (if (cmp == LE_EXPR)
2970 (ge (convert:st @0) { build_zero_cst (st); })
2971 (lt (convert:st @0) { build_zero_cst (st); }))))))))))
2973 (for cmp (unordered ordered unlt unle ungt unge uneq ltgt)
2974 /* If the second operand is NaN, the result is constant. */
2977 (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
2978 && (cmp != LTGT_EXPR || ! flag_trapping_math))
2979 { constant_boolean_node (cmp == ORDERED_EXPR || cmp == LTGT_EXPR
2980 ? false : true, type); })))
2982 /* bool_var != 0 becomes bool_var. */
2984 (ne @0 integer_zerop)
2985 (if (TREE_CODE (TREE_TYPE (@0)) == BOOLEAN_TYPE
2986 && types_match (type, TREE_TYPE (@0)))
2988 /* bool_var == 1 becomes bool_var. */
2990 (eq @0 integer_onep)
2991 (if (TREE_CODE (TREE_TYPE (@0)) == BOOLEAN_TYPE
2992 && types_match (type, TREE_TYPE (@0)))
2995 bool_var == 0 becomes !bool_var or
2996 bool_var != 1 becomes !bool_var
2997 here because that only is good in assignment context as long
2998 as we require a tcc_comparison in GIMPLE_CONDs where we'd
2999 replace if (x == 0) with tem = ~x; if (tem != 0) which is
3000 clearly less optimal and which we'll transform again in forwprop. */
3002 /* When one argument is a constant, overflow detection can be simplified.
3003 Currently restricted to single use so as not to interfere too much with
3004 ADD_OVERFLOW detection in tree-ssa-math-opts.c.
3005 A + CST CMP A -> A CMP' CST' */
3006 (for cmp (lt le ge gt)
3009 (cmp:c (plus@2 @0 INTEGER_CST@1) @0)
3010 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
3011 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))
3014 (out @0 { wide_int_to_tree (TREE_TYPE (@0), wi::max_value
3015 (TYPE_PRECISION (TREE_TYPE (@0)), UNSIGNED) - @1); }))))
3017 /* To detect overflow in unsigned A - B, A < B is simpler than A - B > A.
3018 However, the detection logic for SUB_OVERFLOW in tree-ssa-math-opts.c
3019 expects the long form, so we restrict the transformation for now. */
3022 (cmp:c (minus@2 @0 @1) @0)
3023 (if (single_use (@2)
3024 && ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3025 && TYPE_UNSIGNED (TREE_TYPE (@0))
3026 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
3029 /* Testing for overflow is unnecessary if we already know the result. */
3034 (cmp:c (realpart (IFN_SUB_OVERFLOW@2 @0 @1)) @0)
3035 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
3036 && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
3037 (out (imagpart @2) { build_zero_cst (TREE_TYPE (@0)); }))))
3042 (cmp:c (realpart (IFN_ADD_OVERFLOW:c@2 @0 @1)) @0)
3043 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
3044 && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
3045 (out (imagpart @2) { build_zero_cst (TREE_TYPE (@0)); }))))
3047 /* For unsigned operands, -1 / B < A checks whether A * B would overflow.
3048 Simplify it to __builtin_mul_overflow (A, B, <unused>). */
3052 (cmp:c (trunc_div:s integer_all_onesp @1) @0)
3053 (if (TYPE_UNSIGNED (TREE_TYPE (@0)) && !VECTOR_TYPE_P (TREE_TYPE (@0)))
3054 (with { tree t = TREE_TYPE (@0), cpx = build_complex_type (t); }
3055 (out (imagpart (IFN_MUL_OVERFLOW:cpx @0 @1)) { build_zero_cst (t); })))))
3057 /* Simplification of math builtins. These rules must all be optimizations
3058 as well as IL simplifications. If there is a possibility that the new
3059 form could be a pessimization, the rule should go in the canonicalization
3060 section that follows this one.
3062 Rules can generally go in this section if they satisfy one of
3065 - the rule describes an identity
3067 - the rule replaces calls with something as simple as addition or
3070 - the rule contains unary calls only and simplifies the surrounding
3071 arithmetic. (The idea here is to exclude non-unary calls in which
3072 one operand is constant and in which the call is known to be cheap
3073 when the operand has that value.) */
3075 (if (flag_unsafe_math_optimizations)
3076 /* Simplify sqrt(x) * sqrt(x) -> x. */
3078 (mult (SQRT@1 @0) @1)
3079 (if (!HONOR_SNANS (type))
3082 /* Simplify sqrt(x) * sqrt(y) -> sqrt(x*y). */
3083 (for root (SQRT CBRT)
3085 (mult (root:s @0) (root:s @1))
3086 (root (mult @0 @1))))
3088 /* Simplify expN(x) * expN(y) -> expN(x+y). */
3089 (for exps (EXP EXP2 EXP10 POW10)
3091 (mult (exps:s @0) (exps:s @1))
3092 (exps (plus @0 @1))))
3094 /* Simplify a/root(b/c) into a*root(c/b). */
3095 (for root (SQRT CBRT)
3097 (rdiv @0 (root:s (rdiv:s @1 @2)))
3098 (mult @0 (root (rdiv @2 @1)))))
3100 /* Simplify x/expN(y) into x*expN(-y). */
3101 (for exps (EXP EXP2 EXP10 POW10)
3103 (rdiv @0 (exps:s @1))
3104 (mult @0 (exps (negate @1)))))
3106 (for logs (LOG LOG2 LOG10 LOG10)
3107 exps (EXP EXP2 EXP10 POW10)
3108 /* logN(expN(x)) -> x. */
3112 /* expN(logN(x)) -> x. */
3117 /* Optimize logN(func()) for various exponential functions. We
3118 want to determine the value "x" and the power "exponent" in
3119 order to transform logN(x**exponent) into exponent*logN(x). */
3120 (for logs (LOG LOG LOG LOG2 LOG2 LOG2 LOG10 LOG10)
3121 exps (EXP2 EXP10 POW10 EXP EXP10 POW10 EXP EXP2)
3124 (if (SCALAR_FLOAT_TYPE_P (type))
3130 /* Prepare to do logN(exp(exponent)) -> exponent*logN(e). */
3131 x = build_real_truncate (type, dconst_e ());
3134 /* Prepare to do logN(exp2(exponent)) -> exponent*logN(2). */
3135 x = build_real (type, dconst2);
3139 /* Prepare to do logN(exp10(exponent)) -> exponent*logN(10). */
3141 REAL_VALUE_TYPE dconst10;
3142 real_from_integer (&dconst10, VOIDmode, 10, SIGNED);
3143 x = build_real (type, dconst10);
3150 (mult (logs { x; }) @0)))))
3158 (if (SCALAR_FLOAT_TYPE_P (type))
3164 /* Prepare to do logN(sqrt(x)) -> 0.5*logN(x). */
3165 x = build_real (type, dconsthalf);
3168 /* Prepare to do logN(cbrt(x)) -> (1/3)*logN(x). */
3169 x = build_real_truncate (type, dconst_third ());
3175 (mult { x; } (logs @0))))))
3177 /* logN(pow(x,exponent)) -> exponent*logN(x). */
3178 (for logs (LOG LOG2 LOG10)
3182 (mult @1 (logs @0))))
3187 exps (EXP EXP2 EXP10 POW10)
3188 /* sqrt(expN(x)) -> expN(x*0.5). */
3191 (exps (mult @0 { build_real (type, dconsthalf); })))
3192 /* cbrt(expN(x)) -> expN(x/3). */
3195 (exps (mult @0 { build_real_truncate (type, dconst_third ()); })))
3196 /* pow(expN(x), y) -> expN(x*y). */
3199 (exps (mult @0 @1))))
3201 /* tan(atan(x)) -> x. */
3208 /* cabs(x+0i) or cabs(0+xi) -> abs(x). */
3210 (CABS (complex:C @0 real_zerop@1))
3213 /* trunc(trunc(x)) -> trunc(x), etc. */
3214 (for fns (TRUNC FLOOR CEIL ROUND NEARBYINT RINT)
3218 /* f(x) -> x if x is integer valued and f does nothing for such values. */
3219 (for fns (TRUNC FLOOR CEIL ROUND NEARBYINT RINT)
3221 (fns integer_valued_real_p@0)
3224 /* hypot(x,0) and hypot(0,x) -> abs(x). */
3226 (HYPOT:c @0 real_zerop@1)
3229 /* pow(1,x) -> 1. */
3231 (POW real_onep@0 @1)
3235 /* copysign(x,x) -> x. */
3240 /* copysign(x,y) -> fabs(x) if y is nonnegative. */
3241 (COPYSIGN @0 tree_expr_nonnegative_p@1)
3244 (for scale (LDEXP SCALBN SCALBLN)
3245 /* ldexp(0, x) -> 0. */
3247 (scale real_zerop@0 @1)
3249 /* ldexp(x, 0) -> x. */
3251 (scale @0 integer_zerop@1)
3253 /* ldexp(x, y) -> x if x is +-Inf or NaN. */
3255 (scale REAL_CST@0 @1)
3256 (if (!real_isfinite (TREE_REAL_CST_PTR (@0)))
3259 /* Canonicalization of sequences of math builtins. These rules represent
3260 IL simplifications but are not necessarily optimizations.
3262 The sincos pass is responsible for picking "optimal" implementations
3263 of math builtins, which may be more complicated and can sometimes go
3264 the other way, e.g. converting pow into a sequence of sqrts.
3265 We only want to do these canonicalizations before the pass has run. */
3267 (if (flag_unsafe_math_optimizations && canonicalize_math_p ())
3268 /* Simplify tan(x) * cos(x) -> sin(x). */
3270 (mult:c (TAN:s @0) (COS:s @0))
3273 /* Simplify x * pow(x,c) -> pow(x,c+1). */
3275 (mult:c @0 (POW:s @0 REAL_CST@1))
3276 (if (!TREE_OVERFLOW (@1))
3277 (POW @0 (plus @1 { build_one_cst (type); }))))
3279 /* Simplify sin(x) / cos(x) -> tan(x). */
3281 (rdiv (SIN:s @0) (COS:s @0))
3284 /* Simplify cos(x) / sin(x) -> 1 / tan(x). */
3286 (rdiv (COS:s @0) (SIN:s @0))
3287 (rdiv { build_one_cst (type); } (TAN @0)))
3289 /* Simplify sin(x) / tan(x) -> cos(x). */
3291 (rdiv (SIN:s @0) (TAN:s @0))
3292 (if (! HONOR_NANS (@0)
3293 && ! HONOR_INFINITIES (@0))
3296 /* Simplify tan(x) / sin(x) -> 1.0 / cos(x). */
3298 (rdiv (TAN:s @0) (SIN:s @0))
3299 (if (! HONOR_NANS (@0)
3300 && ! HONOR_INFINITIES (@0))
3301 (rdiv { build_one_cst (type); } (COS @0))))
3303 /* Simplify pow(x,y) * pow(x,z) -> pow(x,y+z). */
3305 (mult (POW:s @0 @1) (POW:s @0 @2))
3306 (POW @0 (plus @1 @2)))
3308 /* Simplify pow(x,y) * pow(z,y) -> pow(x*z,y). */
3310 (mult (POW:s @0 @1) (POW:s @2 @1))
3311 (POW (mult @0 @2) @1))
3313 /* Simplify powi(x,y) * powi(z,y) -> powi(x*z,y). */
3315 (mult (POWI:s @0 @1) (POWI:s @2 @1))
3316 (POWI (mult @0 @2) @1))
3318 /* Simplify pow(x,c) / x -> pow(x,c-1). */
3320 (rdiv (POW:s @0 REAL_CST@1) @0)
3321 (if (!TREE_OVERFLOW (@1))
3322 (POW @0 (minus @1 { build_one_cst (type); }))))
3324 /* Simplify x / pow (y,z) -> x * pow(y,-z). */
3326 (rdiv @0 (POW:s @1 @2))
3327 (mult @0 (POW @1 (negate @2))))
3332 /* sqrt(sqrt(x)) -> pow(x,1/4). */
3335 (pows @0 { build_real (type, dconst_quarter ()); }))
3336 /* sqrt(cbrt(x)) -> pow(x,1/6). */
3339 (pows @0 { build_real_truncate (type, dconst_sixth ()); }))
3340 /* cbrt(sqrt(x)) -> pow(x,1/6). */
3343 (pows @0 { build_real_truncate (type, dconst_sixth ()); }))
3344 /* cbrt(cbrt(x)) -> pow(x,1/9), iff x is nonnegative. */
3346 (cbrts (cbrts tree_expr_nonnegative_p@0))
3347 (pows @0 { build_real_truncate (type, dconst_ninth ()); }))
3348 /* sqrt(pow(x,y)) -> pow(|x|,y*0.5). */
3350 (sqrts (pows @0 @1))
3351 (pows (abs @0) (mult @1 { build_real (type, dconsthalf); })))
3352 /* cbrt(pow(x,y)) -> pow(x,y/3), iff x is nonnegative. */
3354 (cbrts (pows tree_expr_nonnegative_p@0 @1))
3355 (pows @0 (mult @1 { build_real_truncate (type, dconst_third ()); })))
3356 /* pow(sqrt(x),y) -> pow(x,y*0.5). */
3358 (pows (sqrts @0) @1)
3359 (pows @0 (mult @1 { build_real (type, dconsthalf); })))
3360 /* pow(cbrt(x),y) -> pow(x,y/3) iff x is nonnegative. */
3362 (pows (cbrts tree_expr_nonnegative_p@0) @1)
3363 (pows @0 (mult @1 { build_real_truncate (type, dconst_third ()); })))
3364 /* pow(pow(x,y),z) -> pow(x,y*z) iff x is nonnegative. */
3366 (pows (pows tree_expr_nonnegative_p@0 @1) @2)
3367 (pows @0 (mult @1 @2))))
3369 /* cabs(x+xi) -> fabs(x)*sqrt(2). */
3371 (CABS (complex @0 @0))
3372 (mult (abs @0) { build_real_truncate (type, dconst_sqrt2 ()); }))
3374 /* hypot(x,x) -> fabs(x)*sqrt(2). */
3377 (mult (abs @0) { build_real_truncate (type, dconst_sqrt2 ()); }))
3379 /* cexp(x+yi) -> exp(x)*cexpi(y). */
3384 (cexps compositional_complex@0)
3385 (if (targetm.libc_has_function (function_c99_math_complex))
3387 (mult (exps@1 (realpart @0)) (realpart (cexpis:type@2 (imagpart @0))))
3388 (mult @1 (imagpart @2)))))))
3390 (if (canonicalize_math_p ())
3391 /* floor(x) -> trunc(x) if x is nonnegative. */
3395 (floors tree_expr_nonnegative_p@0)
3398 (match double_value_p
3400 (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == double_type_node)))
3401 (for froms (BUILT_IN_TRUNCL
3413 /* truncl(extend(x)) -> extend(trunc(x)), etc., if x is a double. */
3414 (if (optimize && canonicalize_math_p ())
3416 (froms (convert double_value_p@0))
3417 (convert (tos @0)))))
3419 (match float_value_p
3421 (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == float_type_node)))
3422 (for froms (BUILT_IN_TRUNCL BUILT_IN_TRUNC
3423 BUILT_IN_FLOORL BUILT_IN_FLOOR
3424 BUILT_IN_CEILL BUILT_IN_CEIL
3425 BUILT_IN_ROUNDL BUILT_IN_ROUND
3426 BUILT_IN_NEARBYINTL BUILT_IN_NEARBYINT
3427 BUILT_IN_RINTL BUILT_IN_RINT)
3428 tos (BUILT_IN_TRUNCF BUILT_IN_TRUNCF
3429 BUILT_IN_FLOORF BUILT_IN_FLOORF
3430 BUILT_IN_CEILF BUILT_IN_CEILF
3431 BUILT_IN_ROUNDF BUILT_IN_ROUNDF
3432 BUILT_IN_NEARBYINTF BUILT_IN_NEARBYINTF
3433 BUILT_IN_RINTF BUILT_IN_RINTF)
3434 /* truncl(extend(x)) and trunc(extend(x)) -> extend(truncf(x)), etc.,
3436 (if (optimize && canonicalize_math_p ()
3437 && targetm.libc_has_function (function_c99_misc))
3439 (froms (convert float_value_p@0))
3440 (convert (tos @0)))))
3442 (for froms (XFLOORL XCEILL XROUNDL XRINTL)
3443 tos (XFLOOR XCEIL XROUND XRINT)
3444 /* llfloorl(extend(x)) -> llfloor(x), etc., if x is a double. */
3445 (if (optimize && canonicalize_math_p ())
3447 (froms (convert double_value_p@0))
3450 (for froms (XFLOORL XCEILL XROUNDL XRINTL
3451 XFLOOR XCEIL XROUND XRINT)
3452 tos (XFLOORF XCEILF XROUNDF XRINTF)
3453 /* llfloorl(extend(x)) and llfloor(extend(x)) -> llfloorf(x), etc.,
3455 (if (optimize && canonicalize_math_p ())
3457 (froms (convert float_value_p@0))
3460 (if (canonicalize_math_p ())
3461 /* xfloor(x) -> fix_trunc(x) if x is nonnegative. */
3462 (for floors (IFLOOR LFLOOR LLFLOOR)
3464 (floors tree_expr_nonnegative_p@0)
3467 (if (canonicalize_math_p ())
3468 /* xfloor(x) -> fix_trunc(x), etc., if x is integer valued. */
3469 (for fns (IFLOOR LFLOOR LLFLOOR
3471 IROUND LROUND LLROUND)
3473 (fns integer_valued_real_p@0)
3475 (if (!flag_errno_math)
3476 /* xrint(x) -> fix_trunc(x), etc., if x is integer valued. */
3477 (for rints (IRINT LRINT LLRINT)
3479 (rints integer_valued_real_p@0)
3482 (if (canonicalize_math_p ())
3483 (for ifn (IFLOOR ICEIL IROUND IRINT)
3484 lfn (LFLOOR LCEIL LROUND LRINT)
3485 llfn (LLFLOOR LLCEIL LLROUND LLRINT)
3486 /* Canonicalize iround (x) to lround (x) on ILP32 targets where
3487 sizeof (int) == sizeof (long). */
3488 (if (TYPE_PRECISION (integer_type_node)
3489 == TYPE_PRECISION (long_integer_type_node))
3492 (lfn:long_integer_type_node @0)))
3493 /* Canonicalize llround (x) to lround (x) on LP64 targets where
3494 sizeof (long long) == sizeof (long). */
3495 (if (TYPE_PRECISION (long_long_integer_type_node)
3496 == TYPE_PRECISION (long_integer_type_node))
3499 (lfn:long_integer_type_node @0)))))
3501 /* cproj(x) -> x if we're ignoring infinities. */
3504 (if (!HONOR_INFINITIES (type))
3507 /* If the real part is inf and the imag part is known to be
3508 nonnegative, return (inf + 0i). */
3510 (CPROJ (complex REAL_CST@0 tree_expr_nonnegative_p@1))
3511 (if (real_isinf (TREE_REAL_CST_PTR (@0)))
3512 { build_complex_inf (type, false); }))
3514 /* If the imag part is inf, return (inf+I*copysign(0,imag)). */
3516 (CPROJ (complex @0 REAL_CST@1))
3517 (if (real_isinf (TREE_REAL_CST_PTR (@1)))
3518 { build_complex_inf (type, TREE_REAL_CST_PTR (@1)->sign); }))
3524 (pows @0 REAL_CST@1)
3526 const REAL_VALUE_TYPE *value = TREE_REAL_CST_PTR (@1);
3527 REAL_VALUE_TYPE tmp;
3530 /* pow(x,0) -> 1. */
3531 (if (real_equal (value, &dconst0))
3532 { build_real (type, dconst1); })
3533 /* pow(x,1) -> x. */
3534 (if (real_equal (value, &dconst1))
3536 /* pow(x,-1) -> 1/x. */
3537 (if (real_equal (value, &dconstm1))
3538 (rdiv { build_real (type, dconst1); } @0))
3539 /* pow(x,0.5) -> sqrt(x). */
3540 (if (flag_unsafe_math_optimizations
3541 && canonicalize_math_p ()
3542 && real_equal (value, &dconsthalf))
3544 /* pow(x,1/3) -> cbrt(x). */
3545 (if (flag_unsafe_math_optimizations
3546 && canonicalize_math_p ()
3547 && (tmp = real_value_truncate (TYPE_MODE (type), dconst_third ()),
3548 real_equal (value, &tmp)))
3551 /* powi(1,x) -> 1. */
3553 (POWI real_onep@0 @1)
3557 (POWI @0 INTEGER_CST@1)
3559 /* powi(x,0) -> 1. */
3560 (if (wi::eq_p (@1, 0))
3561 { build_real (type, dconst1); })
3562 /* powi(x,1) -> x. */
3563 (if (wi::eq_p (@1, 1))
3565 /* powi(x,-1) -> 1/x. */
3566 (if (wi::eq_p (@1, -1))
3567 (rdiv { build_real (type, dconst1); } @0))))
3569 /* Narrowing of arithmetic and logical operations.
3571 These are conceptually similar to the transformations performed for
3572 the C/C++ front-ends by shorten_binary_op and shorten_compare. Long
3573 term we want to move all that code out of the front-ends into here. */
3575 /* If we have a narrowing conversion of an arithmetic operation where
3576 both operands are widening conversions from the same type as the outer
3577 narrowing conversion. Then convert the innermost operands to a suitable
3578 unsigned type (to avoid introducing undefined behavior), perform the
3579 operation and convert the result to the desired type. */
3580 (for op (plus minus)
3582 (convert (op:s (convert@2 @0) (convert?@3 @1)))
3583 (if (INTEGRAL_TYPE_P (type)
3584 /* We check for type compatibility between @0 and @1 below,
3585 so there's no need to check that @1/@3 are integral types. */
3586 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
3587 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
3588 /* The precision of the type of each operand must match the
3589 precision of the mode of each operand, similarly for the
3591 && (TYPE_PRECISION (TREE_TYPE (@0))
3592 == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (@0))))
3593 && (TYPE_PRECISION (TREE_TYPE (@1))
3594 == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (@1))))
3595 && TYPE_PRECISION (type) == GET_MODE_PRECISION (TYPE_MODE (type))
3596 /* The inner conversion must be a widening conversion. */
3597 && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
3598 && types_match (@0, type)
3599 && (types_match (@0, @1)
3600 /* Or the second operand is const integer or converted const
3601 integer from valueize. */
3602 || TREE_CODE (@1) == INTEGER_CST))
3603 (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
3604 (op @0 (convert @1))
3605 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
3606 (convert (op (convert:utype @0)
3607 (convert:utype @1))))))))
3609 /* This is another case of narrowing, specifically when there's an outer
3610 BIT_AND_EXPR which masks off bits outside the type of the innermost
3611 operands. Like the previous case we have to convert the operands
3612 to unsigned types to avoid introducing undefined behavior for the
3613 arithmetic operation. */
3614 (for op (minus plus)
3616 (bit_and (op:s (convert@2 @0) (convert@3 @1)) INTEGER_CST@4)
3617 (if (INTEGRAL_TYPE_P (type)
3618 /* We check for type compatibility between @0 and @1 below,
3619 so there's no need to check that @1/@3 are integral types. */
3620 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
3621 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
3622 /* The precision of the type of each operand must match the
3623 precision of the mode of each operand, similarly for the
3625 && (TYPE_PRECISION (TREE_TYPE (@0))
3626 == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (@0))))
3627 && (TYPE_PRECISION (TREE_TYPE (@1))
3628 == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (@1))))
3629 && TYPE_PRECISION (type) == GET_MODE_PRECISION (TYPE_MODE (type))
3630 /* The inner conversion must be a widening conversion. */
3631 && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
3632 && types_match (@0, @1)
3633 && (tree_int_cst_min_precision (@4, TYPE_SIGN (TREE_TYPE (@0)))
3634 <= TYPE_PRECISION (TREE_TYPE (@0)))
3635 && (wi::bit_and (@4, wi::mask (TYPE_PRECISION (TREE_TYPE (@0)),
3636 true, TYPE_PRECISION (type))) == 0))
3637 (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
3638 (with { tree ntype = TREE_TYPE (@0); }
3639 (convert (bit_and (op @0 @1) (convert:ntype @4))))
3640 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
3641 (convert (bit_and (op (convert:utype @0) (convert:utype @1))
3642 (convert:utype @4))))))))
3644 /* Transform (@0 < @1 and @0 < @2) to use min,
3645 (@0 > @1 and @0 > @2) to use max */
3646 (for op (lt le gt ge)
3647 ext (min min max max)
3649 (bit_and (op:cs @0 @1) (op:cs @0 @2))
3650 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
3651 && TREE_CODE (@0) != INTEGER_CST)
3652 (op @0 (ext @1 @2)))))
3655 /* signbit(x) -> 0 if x is nonnegative. */
3656 (SIGNBIT tree_expr_nonnegative_p@0)
3657 { integer_zero_node; })
3660 /* signbit(x) -> x<0 if x doesn't have signed zeros. */
3662 (if (!HONOR_SIGNED_ZEROS (@0))
3663 (convert (lt @0 { build_real (TREE_TYPE (@0), dconst0); }))))
3665 /* Transform comparisons of the form X +- C1 CMP C2 to X CMP C2 -+ C1. */
3667 (for op (plus minus)
3670 (cmp (op@3 @0 INTEGER_CST@1) INTEGER_CST@2)
3671 (if (!TREE_OVERFLOW (@1) && !TREE_OVERFLOW (@2)
3672 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0))
3673 && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@0))
3674 && !TYPE_SATURATING (TREE_TYPE (@0)))
3675 (with { tree res = int_const_binop (rop, @2, @1); }
3676 (if (TREE_OVERFLOW (res))
3677 { constant_boolean_node (cmp == NE_EXPR, type); }
3678 (if (single_use (@3))
3679 (cmp @0 { res; }))))))))
3680 (for cmp (lt le gt ge)
3681 (for op (plus minus)
3684 (cmp (op@3 @0 INTEGER_CST@1) INTEGER_CST@2)
3685 (if (!TREE_OVERFLOW (@1) && !TREE_OVERFLOW (@2)
3686 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
3687 (with { tree res = int_const_binop (rop, @2, @1); }
3688 (if (TREE_OVERFLOW (res))
3690 fold_overflow_warning (("assuming signed overflow does not occur "
3691 "when simplifying conditional to constant"),
3692 WARN_STRICT_OVERFLOW_CONDITIONAL);
3693 bool less = cmp == LE_EXPR || cmp == LT_EXPR;
3694 /* wi::ges_p (@2, 0) should be sufficient for a signed type. */
3695 bool ovf_high = wi::lt_p (@1, 0, TYPE_SIGN (TREE_TYPE (@1)))
3696 != (op == MINUS_EXPR);
3697 constant_boolean_node (less == ovf_high, type);
3699 (if (single_use (@3))
3702 fold_overflow_warning (("assuming signed overflow does not occur "
3703 "when changing X +- C1 cmp C2 to "
3705 WARN_STRICT_OVERFLOW_COMPARISON);
3707 (cmp @0 { res; })))))))))
3709 /* Canonicalizations of BIT_FIELD_REFs. */
3712 (BIT_FIELD_REF @0 @1 @2)
3714 (if (TREE_CODE (TREE_TYPE (@0)) == COMPLEX_TYPE
3715 && tree_int_cst_equal (@1, TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))))
3717 (if (integer_zerop (@2))
3718 (view_convert (realpart @0)))
3719 (if (tree_int_cst_equal (@2, TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))))
3720 (view_convert (imagpart @0)))))
3721 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
3722 && INTEGRAL_TYPE_P (type)
3723 /* On GIMPLE this should only apply to register arguments. */
3724 && (! GIMPLE || is_gimple_reg (@0))
3725 /* A bit-field-ref that referenced the full argument can be stripped. */
3726 && ((compare_tree_int (@1, TYPE_PRECISION (TREE_TYPE (@0))) == 0
3727 && integer_zerop (@2))
3728 /* Low-parts can be reduced to integral conversions.
3729 ??? The following doesn't work for PDP endian. */
3730 || (BYTES_BIG_ENDIAN == WORDS_BIG_ENDIAN
3731 /* Don't even think about BITS_BIG_ENDIAN. */
3732 && TYPE_PRECISION (TREE_TYPE (@0)) % BITS_PER_UNIT == 0
3733 && TYPE_PRECISION (type) % BITS_PER_UNIT == 0
3734 && compare_tree_int (@2, (BYTES_BIG_ENDIAN
3735 ? (TYPE_PRECISION (TREE_TYPE (@0))
3736 - TYPE_PRECISION (type))
3740 /* Simplify vector extracts. */
3743 (BIT_FIELD_REF CONSTRUCTOR@0 @1 @2)
3744 (if (VECTOR_TYPE_P (TREE_TYPE (@0))
3745 && (types_match (type, TREE_TYPE (TREE_TYPE (@0)))
3746 || (VECTOR_TYPE_P (type)
3747 && types_match (TREE_TYPE (type), TREE_TYPE (TREE_TYPE (@0))))))
3750 tree ctor = (TREE_CODE (@0) == SSA_NAME
3751 ? gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0)) : @0);
3752 tree eltype = TREE_TYPE (TREE_TYPE (ctor));
3753 unsigned HOST_WIDE_INT width = tree_to_uhwi (TYPE_SIZE (eltype));
3754 unsigned HOST_WIDE_INT n = tree_to_uhwi (@1);
3755 unsigned HOST_WIDE_INT idx = tree_to_uhwi (@2);
3758 && (idx % width) == 0
3760 && ((idx + n) / width) <= TYPE_VECTOR_SUBPARTS (TREE_TYPE (ctor)))
3765 /* Constructor elements can be subvectors. */
3766 unsigned HOST_WIDE_INT k = 1;
3767 if (CONSTRUCTOR_NELTS (ctor) != 0)
3769 tree cons_elem = TREE_TYPE (CONSTRUCTOR_ELT (ctor, 0)->value);
3770 if (TREE_CODE (cons_elem) == VECTOR_TYPE)
3771 k = TYPE_VECTOR_SUBPARTS (cons_elem);
3775 /* We keep an exact subset of the constructor elements. */
3776 (if ((idx % k) == 0 && (n % k) == 0)
3777 (if (CONSTRUCTOR_NELTS (ctor) == 0)
3778 { build_constructor (type, NULL); }
3785 (if (idx < CONSTRUCTOR_NELTS (ctor))
3786 { CONSTRUCTOR_ELT (ctor, idx)->value; }
3787 { build_zero_cst (type); })
3789 vec<constructor_elt, va_gc> *vals;
3790 vec_alloc (vals, n);
3791 for (unsigned i = 0;
3792 i < n && idx + i < CONSTRUCTOR_NELTS (ctor); ++i)
3793 CONSTRUCTOR_APPEND_ELT (vals, NULL_TREE,
3794 CONSTRUCTOR_ELT (ctor, idx + i)->value);
3795 build_constructor (type, vals);
3797 /* The bitfield references a single constructor element. */
3798 (if (idx + n <= (idx / k + 1) * k)
3800 (if (CONSTRUCTOR_NELTS (ctor) <= idx / k)
3801 { build_zero_cst (type); })
3803 { CONSTRUCTOR_ELT (ctor, idx / k)->value; })
3804 (BIT_FIELD_REF { CONSTRUCTOR_ELT (ctor, idx / k)->value; }
3805 @1 { bitsize_int ((idx % k) * width); })))))))))