1 /* Match-and-simplify patterns for shared GENERIC and GIMPLE folding.
2 This file is consumed by genmatch which produces gimple-match.c
3 and generic-match.c from it.
5 Copyright (C) 2014-2016 Free Software Foundation, Inc.
6 Contributed by Richard Biener <rguenther@suse.de>
7 and Prathamesh Kulkarni <bilbotheelffriend@gmail.com>
9 This file is part of GCC.
11 GCC is free software; you can redistribute it and/or modify it under
12 the terms of the GNU General Public License as published by the Free
13 Software Foundation; either version 3, or (at your option) any later
16 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
17 WARRANTY; without even the implied warranty of MERCHANTABILITY or
18 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21 You should have received a copy of the GNU General Public License
22 along with GCC; see the file COPYING3. If not see
23 <http://www.gnu.org/licenses/>. */
26 /* Generic tree predicates we inherit. */
28 integer_onep integer_zerop integer_all_onesp integer_minus_onep
29 integer_each_onep integer_truep integer_nonzerop
30 real_zerop real_onep real_minus_onep
33 tree_expr_nonnegative_p
39 (define_operator_list tcc_comparison
40 lt le eq ne ge gt unordered ordered unlt unle ungt unge uneq ltgt)
41 (define_operator_list inverted_tcc_comparison
42 ge gt ne eq lt le ordered unordered ge gt le lt ltgt uneq)
43 (define_operator_list inverted_tcc_comparison_with_nans
44 unge ungt ne eq unlt unle ordered unordered ge gt le lt ltgt uneq)
45 (define_operator_list swapped_tcc_comparison
46 gt ge eq ne le lt unordered ordered ungt unge unlt unle uneq ltgt)
47 (define_operator_list simple_comparison lt le eq ne ge gt)
48 (define_operator_list swapped_simple_comparison gt ge eq ne le lt)
50 #include "cfn-operators.pd"
52 /* Define operand lists for math rounding functions {,i,l,ll}FN,
53 where the versions prefixed with "i" return an int, those prefixed with
54 "l" return a long and those prefixed with "ll" return a long long.
56 Also define operand lists:
58 X<FN>F for all float functions, in the order i, l, ll
59 X<FN> for all double functions, in the same order
60 X<FN>L for all long double functions, in the same order. */
61 #define DEFINE_INT_AND_FLOAT_ROUND_FN(FN) \
62 (define_operator_list X##FN##F BUILT_IN_I##FN##F \
65 (define_operator_list X##FN BUILT_IN_I##FN \
68 (define_operator_list X##FN##L BUILT_IN_I##FN##L \
72 DEFINE_INT_AND_FLOAT_ROUND_FN (FLOOR)
73 DEFINE_INT_AND_FLOAT_ROUND_FN (CEIL)
74 DEFINE_INT_AND_FLOAT_ROUND_FN (ROUND)
75 DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
77 /* Simplifications of operations with one constant operand and
78 simplifications to constants or single values. */
80 (for op (plus pointer_plus minus bit_ior bit_xor)
85 /* 0 +p index -> (type)index */
87 (pointer_plus integer_zerop @1)
88 (non_lvalue (convert @1)))
90 /* See if ARG1 is zero and X + ARG1 reduces to X.
91 Likewise if the operands are reversed. */
93 (plus:c @0 real_zerop@1)
94 (if (fold_real_zero_addition_p (type, @1, 0))
97 /* See if ARG1 is zero and X - ARG1 reduces to X. */
99 (minus @0 real_zerop@1)
100 (if (fold_real_zero_addition_p (type, @1, 1))
104 This is unsafe for certain floats even in non-IEEE formats.
105 In IEEE, it is unsafe because it does wrong for NaNs.
106 Also note that operand_equal_p is always false if an operand
110 (if (!FLOAT_TYPE_P (type) || !HONOR_NANS (type))
111 { build_zero_cst (type); }))
114 (mult @0 integer_zerop@1)
117 /* Maybe fold x * 0 to 0. The expressions aren't the same
118 when x is NaN, since x * 0 is also NaN. Nor are they the
119 same in modes with signed zeros, since multiplying a
120 negative value by 0 gives -0, not +0. */
122 (mult @0 real_zerop@1)
123 (if (!HONOR_NANS (type) && !HONOR_SIGNED_ZEROS (type))
126 /* In IEEE floating point, x*1 is not equivalent to x for snans.
127 Likewise for complex arithmetic with signed zeros. */
130 (if (!HONOR_SNANS (type)
131 && (!HONOR_SIGNED_ZEROS (type)
132 || !COMPLEX_FLOAT_TYPE_P (type)))
135 /* Transform x * -1.0 into -x. */
137 (mult @0 real_minus_onep)
138 (if (!HONOR_SNANS (type)
139 && (!HONOR_SIGNED_ZEROS (type)
140 || !COMPLEX_FLOAT_TYPE_P (type)))
143 /* X * 1, X / 1 -> X. */
144 (for op (mult trunc_div ceil_div floor_div round_div exact_div)
149 /* Preserve explicit divisions by 0: the C++ front-end wants to detect
150 undefined behavior in constexpr evaluation, and assuming that the division
151 traps enables better optimizations than these anyway. */
152 (for div (trunc_div ceil_div floor_div round_div exact_div)
153 /* 0 / X is always zero. */
155 (div integer_zerop@0 @1)
156 /* But not for 0 / 0 so that we can get the proper warnings and errors. */
157 (if (!integer_zerop (@1))
161 (div @0 integer_minus_onep@1)
162 (if (!TYPE_UNSIGNED (type))
167 /* But not for 0 / 0 so that we can get the proper warnings and errors. */
168 (if (!integer_zerop (@0))
169 { build_one_cst (type); }))
170 /* X / abs (X) is X < 0 ? -1 : 1. */
173 (if (INTEGRAL_TYPE_P (type)
174 && TYPE_OVERFLOW_UNDEFINED (type))
175 (cond (lt @0 { build_zero_cst (type); })
176 { build_minus_one_cst (type); } { build_one_cst (type); })))
179 (div:C @0 (negate @0))
180 (if ((INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
181 && TYPE_OVERFLOW_UNDEFINED (type))
182 { build_minus_one_cst (type); })))
184 /* For unsigned integral types, FLOOR_DIV_EXPR is the same as
185 TRUNC_DIV_EXPR. Rewrite into the latter in this case. */
188 (if ((INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
189 && TYPE_UNSIGNED (type))
192 /* Combine two successive divisions. Note that combining ceil_div
193 and floor_div is trickier and combining round_div even more so. */
194 (for div (trunc_div exact_div)
196 (div (div @0 INTEGER_CST@1) INTEGER_CST@2)
199 wide_int mul = wi::mul (@1, @2, TYPE_SIGN (type), &overflow_p);
202 (div @0 { wide_int_to_tree (type, mul); })
203 (if (TYPE_UNSIGNED (type)
204 || mul != wi::min_value (TYPE_PRECISION (type), SIGNED))
205 { build_zero_cst (type); })))))
207 /* Optimize A / A to 1.0 if we don't care about
208 NaNs or Infinities. */
211 (if (FLOAT_TYPE_P (type)
212 && ! HONOR_NANS (type)
213 && ! HONOR_INFINITIES (type))
214 { build_one_cst (type); }))
216 /* Optimize -A / A to -1.0 if we don't care about
217 NaNs or Infinities. */
219 (rdiv:C @0 (negate @0))
220 (if (FLOAT_TYPE_P (type)
221 && ! HONOR_NANS (type)
222 && ! HONOR_INFINITIES (type))
223 { build_minus_one_cst (type); }))
225 /* PR71078: x / abs(x) -> copysign (1.0, x) */
227 (rdiv:C (convert? @0) (convert? (abs @0)))
228 (if (SCALAR_FLOAT_TYPE_P (type)
229 && ! HONOR_NANS (type)
230 && ! HONOR_INFINITIES (type))
232 (if (types_match (type, float_type_node))
233 (BUILT_IN_COPYSIGNF { build_one_cst (type); } (convert @0)))
234 (if (types_match (type, double_type_node))
235 (BUILT_IN_COPYSIGN { build_one_cst (type); } (convert @0)))
236 (if (types_match (type, long_double_type_node))
237 (BUILT_IN_COPYSIGNL { build_one_cst (type); } (convert @0))))))
239 /* In IEEE floating point, x/1 is not equivalent to x for snans. */
242 (if (!HONOR_SNANS (type))
245 /* In IEEE floating point, x/-1 is not equivalent to -x for snans. */
247 (rdiv @0 real_minus_onep)
248 (if (!HONOR_SNANS (type))
251 (if (flag_reciprocal_math)
252 /* Convert (A/B)/C to A/(B*C) */
254 (rdiv (rdiv:s @0 @1) @2)
255 (rdiv @0 (mult @1 @2)))
257 /* Convert A/(B/C) to (A/B)*C */
259 (rdiv @0 (rdiv:s @1 @2))
260 (mult (rdiv @0 @1) @2)))
262 /* Optimize (X & (-A)) / A where A is a power of 2, to X >> log2(A) */
263 (for div (trunc_div ceil_div floor_div round_div exact_div)
265 (div (convert? (bit_and @0 INTEGER_CST@1)) INTEGER_CST@2)
266 (if (integer_pow2p (@2)
267 && tree_int_cst_sgn (@2) > 0
268 && wi::add (@2, @1) == 0
269 && tree_nop_conversion_p (type, TREE_TYPE (@0)))
270 (rshift (convert @0) { build_int_cst (integer_type_node,
271 wi::exact_log2 (@2)); }))))
273 /* If ARG1 is a constant, we can convert this to a multiply by the
274 reciprocal. This does not have the same rounding properties,
275 so only do this if -freciprocal-math. We can actually
276 always safely do it if ARG1 is a power of two, but it's hard to
277 tell if it is or not in a portable manner. */
278 (for cst (REAL_CST COMPLEX_CST VECTOR_CST)
282 (if (flag_reciprocal_math
285 { tree tem = const_binop (RDIV_EXPR, type, build_one_cst (type), @1); }
287 (mult @0 { tem; } )))
288 (if (cst != COMPLEX_CST)
289 (with { tree inverse = exact_inverse (type, @1); }
291 (mult @0 { inverse; } ))))))))
293 (for mod (ceil_mod floor_mod round_mod trunc_mod)
294 /* 0 % X is always zero. */
296 (mod integer_zerop@0 @1)
297 /* But not for 0 % 0 so that we can get the proper warnings and errors. */
298 (if (!integer_zerop (@1))
300 /* X % 1 is always zero. */
302 (mod @0 integer_onep)
303 { build_zero_cst (type); })
304 /* X % -1 is zero. */
306 (mod @0 integer_minus_onep@1)
307 (if (!TYPE_UNSIGNED (type))
308 { build_zero_cst (type); }))
312 /* But not for 0 % 0 so that we can get the proper warnings and errors. */
313 (if (!integer_zerop (@0))
314 { build_zero_cst (type); }))
315 /* (X % Y) % Y is just X % Y. */
317 (mod (mod@2 @0 @1) @1)
319 /* From extract_muldiv_1: (X * C1) % C2 is zero if C1 is a multiple of C2. */
321 (mod (mult @0 INTEGER_CST@1) INTEGER_CST@2)
322 (if (ANY_INTEGRAL_TYPE_P (type)
323 && TYPE_OVERFLOW_UNDEFINED (type)
324 && wi::multiple_of_p (@1, @2, TYPE_SIGN (type)))
325 { build_zero_cst (type); })))
327 /* X % -C is the same as X % C. */
329 (trunc_mod @0 INTEGER_CST@1)
330 (if (TYPE_SIGN (type) == SIGNED
331 && !TREE_OVERFLOW (@1)
333 && !TYPE_OVERFLOW_TRAPS (type)
334 /* Avoid this transformation if C is INT_MIN, i.e. C == -C. */
335 && !sign_bit_p (@1, @1))
336 (trunc_mod @0 (negate @1))))
338 /* X % -Y is the same as X % Y. */
340 (trunc_mod @0 (convert? (negate @1)))
341 (if (INTEGRAL_TYPE_P (type)
342 && !TYPE_UNSIGNED (type)
343 && !TYPE_OVERFLOW_TRAPS (type)
344 && tree_nop_conversion_p (type, TREE_TYPE (@1))
345 /* Avoid this transformation if X might be INT_MIN or
346 Y might be -1, because we would then change valid
347 INT_MIN % -(-1) into invalid INT_MIN % -1. */
348 && (expr_not_equal_to (@0, TYPE_MIN_VALUE (type))
349 || expr_not_equal_to (@1, wi::minus_one (TYPE_PRECISION
351 (trunc_mod @0 (convert @1))))
353 /* X - (X / Y) * Y is the same as X % Y. */
355 (minus (convert1? @0) (convert2? (mult:c (trunc_div @@0 @@1) @1)))
356 (if (INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
357 (convert (trunc_mod @0 @1))))
359 /* Optimize TRUNC_MOD_EXPR by a power of two into a BIT_AND_EXPR,
360 i.e. "X % C" into "X & (C - 1)", if X and C are positive.
361 Also optimize A % (C << N) where C is a power of 2,
362 to A & ((C << N) - 1). */
363 (match (power_of_two_cand @1)
365 (match (power_of_two_cand @1)
366 (lshift INTEGER_CST@1 @2))
367 (for mod (trunc_mod floor_mod)
369 (mod @0 (convert?@3 (power_of_two_cand@1 @2)))
370 (if ((TYPE_UNSIGNED (type)
371 || tree_expr_nonnegative_p (@0))
372 && tree_nop_conversion_p (type, TREE_TYPE (@3))
373 && integer_pow2p (@2) && tree_int_cst_sgn (@2) > 0)
374 (bit_and @0 (convert (minus @1 { build_int_cst (TREE_TYPE (@1), 1); }))))))
376 /* Simplify (unsigned t * 2)/2 -> unsigned t & 0x7FFFFFFF. */
378 (trunc_div (mult @0 integer_pow2p@1) @1)
379 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
380 (bit_and @0 { wide_int_to_tree
381 (type, wi::mask (TYPE_PRECISION (type) - wi::exact_log2 (@1),
382 false, TYPE_PRECISION (type))); })))
384 /* Simplify (unsigned t / 2) * 2 -> unsigned t & ~1. */
386 (mult (trunc_div @0 integer_pow2p@1) @1)
387 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
388 (bit_and @0 (negate @1))))
390 /* Simplify (t * 2) / 2) -> t. */
391 (for div (trunc_div ceil_div floor_div round_div exact_div)
393 (div (mult @0 @1) @1)
394 (if (ANY_INTEGRAL_TYPE_P (type)
395 && TYPE_OVERFLOW_UNDEFINED (type))
399 /* Simplify cos(-x) and cos(|x|) -> cos(x). Similarly for cosh. */
404 /* Simplify pow(-x, y) and pow(|x|,y) -> pow(x,y) if y is an even integer. */
407 (pows (op @0) REAL_CST@1)
408 (with { HOST_WIDE_INT n; }
409 (if (real_isinteger (&TREE_REAL_CST (@1), &n) && (n & 1) == 0)
411 /* Likewise for powi. */
414 (pows (op @0) INTEGER_CST@1)
415 (if (wi::bit_and (@1, 1) == 0)
417 /* Strip negate and abs from both operands of hypot. */
425 /* copysign(-x, y) and copysign(abs(x), y) -> copysign(x, y). */
426 (for copysigns (COPYSIGN)
428 (copysigns (op @0) @1)
431 /* abs(x)*abs(x) -> x*x. Should be valid for all types. */
436 /* cos(copysign(x, y)) -> cos(x). Similarly for cosh. */
440 (coss (copysigns @0 @1))
443 /* pow(copysign(x, y), z) -> pow(x, z) if z is an even integer. */
447 (pows (copysigns @0 @2) REAL_CST@1)
448 (with { HOST_WIDE_INT n; }
449 (if (real_isinteger (&TREE_REAL_CST (@1), &n) && (n & 1) == 0)
451 /* Likewise for powi. */
455 (pows (copysigns @0 @2) INTEGER_CST@1)
456 (if (wi::bit_and (@1, 1) == 0)
461 /* hypot(copysign(x, y), z) -> hypot(x, z). */
463 (hypots (copysigns @0 @1) @2)
465 /* hypot(x, copysign(y, z)) -> hypot(x, y). */
467 (hypots @0 (copysigns @1 @2))
470 /* copysign(x, CST) -> [-]abs (x). */
471 (for copysigns (COPYSIGN)
473 (copysigns @0 REAL_CST@1)
474 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
478 /* copysign(copysign(x, y), z) -> copysign(x, z). */
479 (for copysigns (COPYSIGN)
481 (copysigns (copysigns @0 @1) @2)
484 /* copysign(x,y)*copysign(x,y) -> x*x. */
485 (for copysigns (COPYSIGN)
487 (mult (copysigns@2 @0 @1) @2)
490 /* ccos(-x) -> ccos(x). Similarly for ccosh. */
491 (for ccoss (CCOS CCOSH)
496 /* cabs(-x) and cos(conj(x)) -> cabs(x). */
497 (for ops (conj negate)
503 /* Fold (a * (1 << b)) into (a << b) */
505 (mult:c @0 (convert? (lshift integer_onep@1 @2)))
506 (if (! FLOAT_TYPE_P (type)
507 && tree_nop_conversion_p (type, TREE_TYPE (@1)))
510 /* Fold (C1/X)*C2 into (C1*C2)/X. */
512 (mult (rdiv@3 REAL_CST@0 @1) REAL_CST@2)
513 (if (flag_associative_math
516 { tree tem = const_binop (MULT_EXPR, type, @0, @2); }
518 (rdiv { tem; } @1)))))
520 /* Convert C1/(X*C2) into (C1/C2)/X */
522 (rdiv REAL_CST@0 (mult @1 REAL_CST@2))
523 (if (flag_reciprocal_math)
525 { tree tem = const_binop (RDIV_EXPR, type, @0, @2); }
527 (rdiv { tem; } @1)))))
529 /* Simplify ~X & X as zero. */
531 (bit_and:c (convert? @0) (convert? (bit_not @0)))
532 { build_zero_cst (type); })
534 /* PR71636: Transform x & ((1U << b) - 1) -> x & ~(~0U << b); */
536 (bit_and:c @0 (plus:s (lshift:s integer_onep @1) integer_minus_onep))
537 (if (TYPE_UNSIGNED (type))
538 (bit_and @0 (bit_not (lshift { build_all_ones_cst (type); } @1)))))
540 /* PR35691: Transform
541 (x == 0 & y == 0) -> (x | typeof(x)(y)) == 0.
542 (x != 0 | y != 0) -> (x | typeof(x)(y)) != 0. */
543 (for bitop (bit_and bit_ior)
546 (bitop (cmp @0 integer_zerop@2) (cmp @1 integer_zerop))
547 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
548 && INTEGRAL_TYPE_P (TREE_TYPE (@1))
549 && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
550 (cmp (bit_ior @0 (convert @1)) @2))))
552 /* Fold (A & ~B) - (A & B) into (A ^ B) - B. */
554 (minus (bit_and:cs @0 (bit_not @1)) (bit_and:cs @0 @1))
555 (minus (bit_xor @0 @1) @1))
557 (minus (bit_and:s @0 INTEGER_CST@2) (bit_and:s @0 INTEGER_CST@1))
558 (if (wi::bit_not (@2) == @1)
559 (minus (bit_xor @0 @1) @1)))
561 /* Fold (A & B) - (A & ~B) into B - (A ^ B). */
563 (minus (bit_and:cs @0 @1) (bit_and:cs @0 (bit_not @1)))
564 (minus @1 (bit_xor @0 @1)))
566 /* Simplify (X & ~Y) | (~X & Y) -> X ^ Y. */
568 (bit_ior (bit_and:c @0 (bit_not @1)) (bit_and:c (bit_not @0) @1))
571 (bit_ior:c (bit_and @0 INTEGER_CST@2) (bit_and (bit_not @0) INTEGER_CST@1))
572 (if (wi::bit_not (@2) == @1)
575 /* PR53979: Transform ((a ^ b) | a) -> (a | b) */
577 (bit_ior:c (bit_xor:c @0 @1) @0)
580 /* Simplify (~X & Y) to X ^ Y if we know that (X & ~Y) is 0. */
583 (bit_and (bit_not SSA_NAME@0) INTEGER_CST@1)
584 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
585 && (get_nonzero_bits (@0) & wi::bit_not (@1)) == 0)
589 /* X % Y is smaller than Y. */
592 (cmp (trunc_mod @0 @1) @1)
593 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
594 { constant_boolean_node (cmp == LT_EXPR, type); })))
597 (cmp @1 (trunc_mod @0 @1))
598 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
599 { constant_boolean_node (cmp == GT_EXPR, type); })))
603 (bit_ior @0 integer_all_onesp@1)
608 (bit_ior @0 integer_zerop)
613 (bit_and @0 integer_zerop@1)
619 (for op (bit_ior bit_xor plus)
621 (op:c (convert? @0) (convert? (bit_not @0)))
622 (convert { build_all_ones_cst (TREE_TYPE (@0)); })))
627 { build_zero_cst (type); })
629 /* Canonicalize X ^ ~0 to ~X. */
631 (bit_xor @0 integer_all_onesp@1)
636 (bit_and @0 integer_all_onesp)
639 /* x & x -> x, x | x -> x */
640 (for bitop (bit_and bit_ior)
645 /* x & C -> x if we know that x & ~C == 0. */
648 (bit_and SSA_NAME@0 INTEGER_CST@1)
649 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
650 && (get_nonzero_bits (@0) & wi::bit_not (@1)) == 0)
654 /* x + (x & 1) -> (x + 1) & ~1 */
656 (plus:c @0 (bit_and:s @0 integer_onep@1))
657 (bit_and (plus @0 @1) (bit_not @1)))
659 /* x & ~(x & y) -> x & ~y */
660 /* x | ~(x | y) -> x | ~y */
661 (for bitop (bit_and bit_ior)
663 (bitop:c @0 (bit_not (bitop:cs @0 @1)))
664 (bitop @0 (bit_not @1))))
666 /* (x | y) & ~x -> y & ~x */
667 /* (x & y) | ~x -> y | ~x */
668 (for bitop (bit_and bit_ior)
669 rbitop (bit_ior bit_and)
671 (bitop:c (rbitop:c @0 @1) (bit_not@2 @0))
674 /* (x & y) ^ (x | y) -> x ^ y */
676 (bit_xor:c (bit_and @0 @1) (bit_ior @0 @1))
679 /* (x ^ y) ^ (x | y) -> x & y */
681 (bit_xor:c (bit_xor @0 @1) (bit_ior @0 @1))
684 /* (x & y) + (x ^ y) -> x | y */
685 /* (x & y) | (x ^ y) -> x | y */
686 /* (x & y) ^ (x ^ y) -> x | y */
687 (for op (plus bit_ior bit_xor)
689 (op:c (bit_and @0 @1) (bit_xor @0 @1))
692 /* (x & y) + (x | y) -> x + y */
694 (plus:c (bit_and @0 @1) (bit_ior @0 @1))
697 /* (x + y) - (x | y) -> x & y */
699 (minus (plus @0 @1) (bit_ior @0 @1))
700 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
701 && !TYPE_SATURATING (type))
704 /* (x + y) - (x & y) -> x | y */
706 (minus (plus @0 @1) (bit_and @0 @1))
707 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
708 && !TYPE_SATURATING (type))
711 /* (x | y) - (x ^ y) -> x & y */
713 (minus (bit_ior @0 @1) (bit_xor @0 @1))
716 /* (x | y) - (x & y) -> x ^ y */
718 (minus (bit_ior @0 @1) (bit_and @0 @1))
721 /* (x | y) & ~(x & y) -> x ^ y */
723 (bit_and:c (bit_ior @0 @1) (bit_not (bit_and @0 @1)))
726 /* (x | y) & (~x ^ y) -> x & y */
728 (bit_and:c (bit_ior:c @0 @1) (bit_xor:c @1 (bit_not @0)))
731 /* ~x & ~y -> ~(x | y)
732 ~x | ~y -> ~(x & y) */
733 (for op (bit_and bit_ior)
734 rop (bit_ior bit_and)
736 (op (convert1? (bit_not @0)) (convert2? (bit_not @1)))
737 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
738 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
739 (bit_not (rop (convert @0) (convert @1))))))
741 /* If we are XORing or adding two BIT_AND_EXPR's, both of which are and'ing
742 with a constant, and the two constants have no bits in common,
743 we should treat this as a BIT_IOR_EXPR since this may produce more
745 (for op (bit_xor plus)
747 (op (convert1? (bit_and@4 @0 INTEGER_CST@1))
748 (convert2? (bit_and@5 @2 INTEGER_CST@3)))
749 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
750 && tree_nop_conversion_p (type, TREE_TYPE (@2))
751 && wi::bit_and (@1, @3) == 0)
752 (bit_ior (convert @4) (convert @5)))))
754 /* (X | Y) ^ X -> Y & ~ X*/
756 (bit_xor:c (convert1? (bit_ior:c @@0 @1)) (convert2? @0))
757 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
758 (convert (bit_and @1 (bit_not @0)))))
760 /* Convert ~X ^ ~Y to X ^ Y. */
762 (bit_xor (convert1? (bit_not @0)) (convert2? (bit_not @1)))
763 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
764 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
765 (bit_xor (convert @0) (convert @1))))
767 /* Convert ~X ^ C to X ^ ~C. */
769 (bit_xor (convert? (bit_not @0)) INTEGER_CST@1)
770 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
771 (bit_xor (convert @0) (bit_not @1))))
773 /* Fold (X & Y) ^ Y and (X ^ Y) & Y as ~X & Y. */
774 (for opo (bit_and bit_xor)
775 opi (bit_xor bit_and)
777 (opo:c (opi:c @0 @1) @1)
778 (bit_and (bit_not @0) @1)))
780 /* Given a bit-wise operation CODE applied to ARG0 and ARG1, see if both
781 operands are another bit-wise operation with a common input. If so,
782 distribute the bit operations to save an operation and possibly two if
783 constants are involved. For example, convert
784 (A | B) & (A | C) into A | (B & C)
785 Further simplification will occur if B and C are constants. */
786 (for op (bit_and bit_ior bit_xor)
787 rop (bit_ior bit_and bit_and)
789 (op (convert? (rop:c @@0 @1)) (convert? (rop:c @0 @2)))
790 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
791 && tree_nop_conversion_p (type, TREE_TYPE (@2)))
792 (rop (convert @0) (op (convert @1) (convert @2))))))
794 /* Some simple reassociation for bit operations, also handled in reassoc. */
795 /* (X & Y) & Y -> X & Y
796 (X | Y) | Y -> X | Y */
797 (for op (bit_and bit_ior)
799 (op:c (convert1?@2 (op:c @0 @@1)) (convert2? @1))
801 /* (X ^ Y) ^ Y -> X */
803 (bit_xor:c (convert1? (bit_xor:c @0 @@1)) (convert2? @1))
805 /* (X & Y) & (X & Z) -> (X & Y) & Z
806 (X | Y) | (X | Z) -> (X | Y) | Z */
807 (for op (bit_and bit_ior)
809 (op:c (convert1?@3 (op:c@4 @0 @1)) (convert2?@5 (op:c@6 @0 @2)))
810 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
811 && tree_nop_conversion_p (type, TREE_TYPE (@2)))
812 (if (single_use (@5) && single_use (@6))
814 (if (single_use (@3) && single_use (@4))
815 (op (convert @1) @5))))))
816 /* (X ^ Y) ^ (X ^ Z) -> Y ^ Z */
818 (bit_xor (convert1? (bit_xor:c @0 @1)) (convert2? (bit_xor:c @0 @2)))
819 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
820 && tree_nop_conversion_p (type, TREE_TYPE (@2)))
821 (bit_xor (convert @1) (convert @2))))
830 (abs tree_expr_nonnegative_p@0)
833 /* A few cases of fold-const.c negate_expr_p predicate. */
836 (if ((INTEGRAL_TYPE_P (type)
837 && TYPE_OVERFLOW_WRAPS (type))
838 || (!TYPE_OVERFLOW_SANITIZED (type)
839 && may_negate_without_overflow_p (t)))))
844 (if (!TYPE_OVERFLOW_SANITIZED (type))))
847 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (t)))))
848 /* VECTOR_CST handling of non-wrapping types would recurse in unsupported
852 (if (FLOAT_TYPE_P (TREE_TYPE (type)) || TYPE_OVERFLOW_WRAPS (type))))
854 /* (-A) * (-B) -> A * B */
856 (mult:c (convert1? (negate @0)) (convert2? negate_expr_p@1))
857 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
858 && tree_nop_conversion_p (type, TREE_TYPE (@1)))
859 (mult (convert @0) (convert (negate @1)))))
861 /* -(A + B) -> (-B) - A. */
863 (negate (plus:c @0 negate_expr_p@1))
864 (if (!HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type))
865 && !HONOR_SIGNED_ZEROS (element_mode (type)))
866 (minus (negate @1) @0)))
868 /* A - B -> A + (-B) if B is easily negatable. */
870 (minus @0 negate_expr_p@1)
871 (if (!FIXED_POINT_TYPE_P (type))
872 (plus @0 (negate @1))))
874 /* Try to fold (type) X op CST -> (type) (X op ((type-x) CST))
876 For bitwise binary operations apply operand conversions to the
877 binary operation result instead of to the operands. This allows
878 to combine successive conversions and bitwise binary operations.
879 We combine the above two cases by using a conditional convert. */
880 (for bitop (bit_and bit_ior bit_xor)
882 (bitop (convert @0) (convert? @1))
883 (if (((TREE_CODE (@1) == INTEGER_CST
884 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
885 && int_fits_type_p (@1, TREE_TYPE (@0)))
886 || types_match (@0, @1))
887 /* ??? This transform conflicts with fold-const.c doing
888 Convert (T)(x & c) into (T)x & (T)c, if c is an integer
889 constants (if x has signed type, the sign bit cannot be set
890 in c). This folds extension into the BIT_AND_EXPR.
891 Restrict it to GIMPLE to avoid endless recursions. */
892 && (bitop != BIT_AND_EXPR || GIMPLE)
893 && (/* That's a good idea if the conversion widens the operand, thus
894 after hoisting the conversion the operation will be narrower. */
895 TYPE_PRECISION (TREE_TYPE (@0)) < TYPE_PRECISION (type)
896 /* It's also a good idea if the conversion is to a non-integer
898 || GET_MODE_CLASS (TYPE_MODE (type)) != MODE_INT
899 /* Or if the precision of TO is not the same as the precision
901 || TYPE_PRECISION (type) != GET_MODE_PRECISION (TYPE_MODE (type))))
902 (convert (bitop @0 (convert @1))))))
904 (for bitop (bit_and bit_ior)
905 rbitop (bit_ior bit_and)
906 /* (x | y) & x -> x */
907 /* (x & y) | x -> x */
909 (bitop:c (rbitop:c @0 @1) @0)
911 /* (~x | y) & x -> x & y */
912 /* (~x & y) | x -> x | y */
914 (bitop:c (rbitop:c (bit_not @0) @1) @0)
917 /* (x | CST1) & CST2 -> (x & CST2) | (CST1 & CST2) */
919 (bit_and (bit_ior @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
920 (bit_ior (bit_and @0 @2) (bit_and @1 @2)))
922 /* Combine successive equal operations with constants. */
923 (for bitop (bit_and bit_ior bit_xor)
925 (bitop (bitop @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
926 (bitop @0 (bitop @1 @2))))
928 /* Try simple folding for X op !X, and X op X with the help
929 of the truth_valued_p and logical_inverted_value predicates. */
930 (match truth_valued_p
932 (if (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1)))
933 (for op (tcc_comparison truth_and truth_andif truth_or truth_orif truth_xor)
934 (match truth_valued_p
936 (match truth_valued_p
939 (match (logical_inverted_value @0)
941 (match (logical_inverted_value @0)
942 (bit_not truth_valued_p@0))
943 (match (logical_inverted_value @0)
944 (eq @0 integer_zerop))
945 (match (logical_inverted_value @0)
946 (ne truth_valued_p@0 integer_truep))
947 (match (logical_inverted_value @0)
948 (bit_xor truth_valued_p@0 integer_truep))
952 (bit_and:c @0 (logical_inverted_value @0))
953 { build_zero_cst (type); })
954 /* X | !X and X ^ !X -> 1, , if X is truth-valued. */
955 (for op (bit_ior bit_xor)
957 (op:c truth_valued_p@0 (logical_inverted_value @0))
958 { constant_boolean_node (true, type); }))
959 /* X ==/!= !X is false/true. */
962 (op:c truth_valued_p@0 (logical_inverted_value @0))
963 { constant_boolean_node (op == NE_EXPR ? true : false, type); }))
965 /* If arg1 and arg2 are booleans (or any single bit type)
966 then try to simplify:
973 But only do this if our result feeds into a comparison as
974 this transformation is not always a win, particularly on
975 targets with and-not instructions.
976 -> simplify_bitwise_binary_boolean */
978 (ne (bit_and:c (bit_not @0) @1) integer_zerop)
979 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
980 && TYPE_PRECISION (TREE_TYPE (@1)) == 1)
981 (if (TYPE_UNSIGNED (TREE_TYPE (@1)))
985 (ne (bit_ior:c (bit_not @0) @1) integer_zerop)
986 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
987 && TYPE_PRECISION (TREE_TYPE (@1)) == 1)
988 (if (TYPE_UNSIGNED (TREE_TYPE (@1)))
994 (bit_not (bit_not @0))
997 /* Convert ~ (-A) to A - 1. */
999 (bit_not (convert? (negate @0)))
1000 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1001 || !TYPE_UNSIGNED (TREE_TYPE (@0)))
1002 (convert (minus @0 { build_each_one_cst (TREE_TYPE (@0)); }))))
1004 /* Convert ~ (A - 1) or ~ (A + -1) to -A. */
1006 (bit_not (convert? (minus @0 integer_each_onep)))
1007 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1008 || !TYPE_UNSIGNED (TREE_TYPE (@0)))
1009 (convert (negate @0))))
1011 (bit_not (convert? (plus @0 integer_all_onesp)))
1012 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1013 || !TYPE_UNSIGNED (TREE_TYPE (@0)))
1014 (convert (negate @0))))
1016 /* Part of convert ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify. */
1018 (bit_not (convert? (bit_xor @0 INTEGER_CST@1)))
1019 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1020 (convert (bit_xor @0 (bit_not @1)))))
1022 (bit_not (convert? (bit_xor:c (bit_not @0) @1)))
1023 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1024 (convert (bit_xor @0 @1))))
1026 /* (x & ~m) | (y & m) -> ((x ^ y) & m) ^ x */
1028 (bit_ior:c (bit_and:cs @0 (bit_not @2)) (bit_and:cs @1 @2))
1029 (bit_xor (bit_and (bit_xor @0 @1) @2) @0))
1031 /* Fold A - (A & B) into ~B & A. */
1033 (minus (convert1? @0) (convert2?:s (bit_and:cs @@0 @1)))
1034 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
1035 && tree_nop_conversion_p (type, TREE_TYPE (@1)))
1036 (convert (bit_and (bit_not @1) @0))))
1040 /* ((X inner_op C0) outer_op C1)
1041 With X being a tree where value_range has reasoned certain bits to always be
1042 zero throughout its computed value range,
1043 inner_op = {|,^}, outer_op = {|,^} and inner_op != outer_op
1044 where zero_mask has 1's for all bits that are sure to be 0 in
1046 if (inner_op == '^') C0 &= ~C1;
1047 if ((C0 & ~zero_mask) == 0) then emit (X outer_op (C0 outer_op C1)
1048 if ((C1 & ~zero_mask) == 0) then emit (X inner_op (C0 outer_op C1)
1050 (for inner_op (bit_ior bit_xor)
1051 outer_op (bit_xor bit_ior)
1054 (inner_op:s @2 INTEGER_CST@0) INTEGER_CST@1)
1058 wide_int zero_mask_not;
1062 if (TREE_CODE (@2) == SSA_NAME)
1063 zero_mask_not = get_nonzero_bits (@2);
1067 if (inner_op == BIT_XOR_EXPR)
1069 C0 = wi::bit_and_not (@0, @1);
1070 cst_emit = wi::bit_or (C0, @1);
1075 cst_emit = wi::bit_xor (@0, @1);
1078 (if (!fail && wi::bit_and (C0, zero_mask_not) == 0)
1079 (outer_op @2 { wide_int_to_tree (type, cst_emit); })
1080 (if (!fail && wi::bit_and (@1, zero_mask_not) == 0)
1081 (inner_op @2 { wide_int_to_tree (type, cst_emit); }))))))
1083 /* Associate (p +p off1) +p off2 as (p +p (off1 + off2)). */
1085 (pointer_plus (pointer_plus:s @0 @1) @3)
1086 (pointer_plus @0 (plus @1 @3)))
1092 tem4 = (unsigned long) tem3;
1097 (pointer_plus @0 (convert?@2 (minus@3 (convert @1) (convert @0))))
1098 /* Conditionally look through a sign-changing conversion. */
1099 (if (TYPE_PRECISION (TREE_TYPE (@2)) == TYPE_PRECISION (TREE_TYPE (@3))
1100 && ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@1)))
1101 || (GENERIC && type == TREE_TYPE (@1))))
1105 tem = (sizetype) ptr;
1109 and produce the simpler and easier to analyze with respect to alignment
1110 ... = ptr & ~algn; */
1112 (pointer_plus @0 (negate (bit_and (convert @0) INTEGER_CST@1)))
1113 (with { tree algn = wide_int_to_tree (TREE_TYPE (@0), wi::bit_not (@1)); }
1114 (bit_and @0 { algn; })))
1116 /* Try folding difference of addresses. */
1118 (minus (convert ADDR_EXPR@0) (convert @1))
1119 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1120 (with { HOST_WIDE_INT diff; }
1121 (if (ptr_difference_const (@0, @1, &diff))
1122 { build_int_cst_type (type, diff); }))))
1124 (minus (convert @0) (convert ADDR_EXPR@1))
1125 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1126 (with { HOST_WIDE_INT diff; }
1127 (if (ptr_difference_const (@0, @1, &diff))
1128 { build_int_cst_type (type, diff); }))))
1130 /* If arg0 is derived from the address of an object or function, we may
1131 be able to fold this expression using the object or function's
1134 (bit_and (convert? @0) INTEGER_CST@1)
1135 (if (POINTER_TYPE_P (TREE_TYPE (@0))
1136 && tree_nop_conversion_p (type, TREE_TYPE (@0)))
1140 unsigned HOST_WIDE_INT bitpos;
1141 get_pointer_alignment_1 (@0, &align, &bitpos);
1143 (if (wi::ltu_p (@1, align / BITS_PER_UNIT))
1144 { wide_int_to_tree (type, wi::bit_and (@1, bitpos / BITS_PER_UNIT)); }))))
1147 /* We can't reassociate at all for saturating types. */
1148 (if (!TYPE_SATURATING (type))
1150 /* Contract negates. */
1151 /* A + (-B) -> A - B */
1153 (plus:c (convert1? @0) (convert2? (negate @1)))
1154 /* Apply STRIP_NOPS on @0 and the negate. */
1155 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
1156 && tree_nop_conversion_p (type, TREE_TYPE (@1))
1157 && !TYPE_OVERFLOW_SANITIZED (type))
1158 (minus (convert @0) (convert @1))))
1159 /* A - (-B) -> A + B */
1161 (minus (convert1? @0) (convert2? (negate @1)))
1162 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
1163 && tree_nop_conversion_p (type, TREE_TYPE (@1))
1164 && !TYPE_OVERFLOW_SANITIZED (type))
1165 (plus (convert @0) (convert @1))))
1168 (negate (convert? (negate @1)))
1169 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1170 && !TYPE_OVERFLOW_SANITIZED (type))
1173 /* We can't reassociate floating-point unless -fassociative-math
1174 or fixed-point plus or minus because of saturation to +-Inf. */
1175 (if ((!FLOAT_TYPE_P (type) || flag_associative_math)
1176 && !FIXED_POINT_TYPE_P (type))
1178 /* Match patterns that allow contracting a plus-minus pair
1179 irrespective of overflow issues. */
1180 /* (A +- B) - A -> +- B */
1181 /* (A +- B) -+ B -> A */
1182 /* A - (A +- B) -> -+ B */
1183 /* A +- (B -+ A) -> +- B */
1185 (minus (plus:c @0 @1) @0)
1188 (minus (minus @0 @1) @0)
1191 (plus:c (minus @0 @1) @1)
1194 (minus @0 (plus:c @0 @1))
1197 (minus @0 (minus @0 @1))
1200 /* (A +- CST) +- CST -> A + CST */
1201 (for outer_op (plus minus)
1202 (for inner_op (plus minus)
1204 (outer_op (inner_op @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
1205 /* If the constant operation overflows we cannot do the transform
1206 as we would introduce undefined overflow, for example
1207 with (a - 1) + INT_MIN. */
1208 (with { tree cst = const_binop (outer_op == inner_op
1209 ? PLUS_EXPR : MINUS_EXPR, type, @1, @2); }
1210 (if (cst && !TREE_OVERFLOW (cst))
1211 (inner_op @0 { cst; } ))))))
1213 /* (CST - A) +- CST -> CST - A */
1214 (for outer_op (plus minus)
1216 (outer_op (minus CONSTANT_CLASS_P@1 @0) CONSTANT_CLASS_P@2)
1217 (with { tree cst = const_binop (outer_op, type, @1, @2); }
1218 (if (cst && !TREE_OVERFLOW (cst))
1219 (minus { cst; } @0)))))
1223 (plus:c (bit_not @0) @0)
1224 (if (!TYPE_OVERFLOW_TRAPS (type))
1225 { build_all_ones_cst (type); }))
1229 (plus (convert? (bit_not @0)) integer_each_onep)
1230 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1231 (negate (convert @0))))
1235 (minus (convert? (negate @0)) integer_each_onep)
1236 (if (!TYPE_OVERFLOW_TRAPS (type)
1237 && tree_nop_conversion_p (type, TREE_TYPE (@0)))
1238 (bit_not (convert @0))))
1242 (minus integer_all_onesp @0)
1245 /* (T)(P + A) - (T)P -> (T) A */
1246 (for add (plus pointer_plus)
1248 (minus (convert (add @@0 @1))
1250 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
1251 /* For integer types, if A has a smaller type
1252 than T the result depends on the possible
1254 E.g. T=size_t, A=(unsigned)429497295, P>0.
1255 However, if an overflow in P + A would cause
1256 undefined behavior, we can assume that there
1258 || (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1259 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1260 /* For pointer types, if the conversion of A to the
1261 final type requires a sign- or zero-extension,
1262 then we have to punt - it is not defined which
1264 || (POINTER_TYPE_P (TREE_TYPE (@0))
1265 && TREE_CODE (@1) == INTEGER_CST
1266 && tree_int_cst_sign_bit (@1) == 0))
1269 /* (T)P - (T)(P + A) -> -(T) A */
1270 (for add (plus pointer_plus)
1273 (convert (add @@0 @1)))
1274 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
1275 /* For integer types, if A has a smaller type
1276 than T the result depends on the possible
1278 E.g. T=size_t, A=(unsigned)429497295, P>0.
1279 However, if an overflow in P + A would cause
1280 undefined behavior, we can assume that there
1282 || (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1283 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1284 /* For pointer types, if the conversion of A to the
1285 final type requires a sign- or zero-extension,
1286 then we have to punt - it is not defined which
1288 || (POINTER_TYPE_P (TREE_TYPE (@0))
1289 && TREE_CODE (@1) == INTEGER_CST
1290 && tree_int_cst_sign_bit (@1) == 0))
1291 (negate (convert @1)))))
1293 /* (T)(P + A) - (T)(P + B) -> (T)A - (T)B */
1294 (for add (plus pointer_plus)
1296 (minus (convert (add @@0 @1))
1297 (convert (add @0 @2)))
1298 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
1299 /* For integer types, if A has a smaller type
1300 than T the result depends on the possible
1302 E.g. T=size_t, A=(unsigned)429497295, P>0.
1303 However, if an overflow in P + A would cause
1304 undefined behavior, we can assume that there
1306 || (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1307 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1308 /* For pointer types, if the conversion of A to the
1309 final type requires a sign- or zero-extension,
1310 then we have to punt - it is not defined which
1312 || (POINTER_TYPE_P (TREE_TYPE (@0))
1313 && TREE_CODE (@1) == INTEGER_CST
1314 && tree_int_cst_sign_bit (@1) == 0
1315 && TREE_CODE (@2) == INTEGER_CST
1316 && tree_int_cst_sign_bit (@2) == 0))
1317 (minus (convert @1) (convert @2)))))))
1320 /* Simplifications of MIN_EXPR, MAX_EXPR, fmin() and fmax(). */
1322 (for minmax (min max FMIN FMAX)
1326 /* min(max(x,y),y) -> y. */
1328 (min:c (max:c @0 @1) @1)
1330 /* max(min(x,y),y) -> y. */
1332 (max:c (min:c @0 @1) @1)
1334 /* max(a,-a) -> abs(a). */
1336 (max:c @0 (negate @0))
1337 (if (TREE_CODE (type) != COMPLEX_TYPE
1338 && (! ANY_INTEGRAL_TYPE_P (type)
1339 || TYPE_OVERFLOW_UNDEFINED (type)))
1341 /* min(a,-a) -> -abs(a). */
1343 (min:c @0 (negate @0))
1344 (if (TREE_CODE (type) != COMPLEX_TYPE
1345 && (! ANY_INTEGRAL_TYPE_P (type)
1346 || TYPE_OVERFLOW_UNDEFINED (type)))
1351 (if (INTEGRAL_TYPE_P (type)
1352 && TYPE_MIN_VALUE (type)
1353 && operand_equal_p (@1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
1355 (if (INTEGRAL_TYPE_P (type)
1356 && TYPE_MAX_VALUE (type)
1357 && operand_equal_p (@1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
1362 (if (INTEGRAL_TYPE_P (type)
1363 && TYPE_MAX_VALUE (type)
1364 && operand_equal_p (@1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
1366 (if (INTEGRAL_TYPE_P (type)
1367 && TYPE_MIN_VALUE (type)
1368 && operand_equal_p (@1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
1371 /* (convert (minmax ((convert (x) c)))) -> minmax (x c) if x is promoted
1372 and the outer convert demotes the expression back to x's type. */
1373 (for minmax (min max)
1375 (convert (minmax@0 (convert @1) INTEGER_CST@2))
1376 (if (types_match (@1, type) && int_fits_type_p (@2, type)
1377 && TYPE_SIGN (TREE_TYPE (@0)) == TYPE_SIGN (type)
1378 && TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type))
1379 (minmax @1 (convert @2)))))
1381 (for minmax (FMIN FMAX)
1382 /* If either argument is NaN, return the other one. Avoid the
1383 transformation if we get (and honor) a signalling NaN. */
1385 (minmax:c @0 REAL_CST@1)
1386 (if (real_isnan (TREE_REAL_CST_PTR (@1))
1387 && (!HONOR_SNANS (@1) || !TREE_REAL_CST (@1).signalling))
1389 /* Convert fmin/fmax to MIN_EXPR/MAX_EXPR. C99 requires these
1390 functions to return the numeric arg if the other one is NaN.
1391 MIN and MAX don't honor that, so only transform if -ffinite-math-only
1392 is set. C99 doesn't require -0.0 to be handled, so we don't have to
1393 worry about it either. */
1394 (if (flag_finite_math_only)
1401 /* min (-A, -B) -> -max (A, B) */
1402 (for minmax (min max FMIN FMAX)
1403 maxmin (max min FMAX FMIN)
1405 (minmax (negate:s@2 @0) (negate:s@3 @1))
1406 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
1407 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1408 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
1409 (negate (maxmin @0 @1)))))
1410 /* MIN (~X, ~Y) -> ~MAX (X, Y)
1411 MAX (~X, ~Y) -> ~MIN (X, Y) */
1412 (for minmax (min max)
1415 (minmax (bit_not:s@2 @0) (bit_not:s@3 @1))
1416 (bit_not (maxmin @0 @1))))
1418 /* MIN (X, Y) == X -> X <= Y */
1419 (for minmax (min min max max)
1423 (cmp:c (minmax:c @0 @1) @0)
1424 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0)))
1426 /* MIN (X, 5) == 0 -> X == 0
1427 MIN (X, 5) == 7 -> false */
1430 (cmp (min @0 INTEGER_CST@1) INTEGER_CST@2)
1431 (if (wi::lt_p (@1, @2, TYPE_SIGN (TREE_TYPE (@0))))
1432 { constant_boolean_node (cmp == NE_EXPR, type); }
1433 (if (wi::gt_p (@1, @2, TYPE_SIGN (TREE_TYPE (@0))))
1437 (cmp (max @0 INTEGER_CST@1) INTEGER_CST@2)
1438 (if (wi::gt_p (@1, @2, TYPE_SIGN (TREE_TYPE (@0))))
1439 { constant_boolean_node (cmp == NE_EXPR, type); }
1440 (if (wi::lt_p (@1, @2, TYPE_SIGN (TREE_TYPE (@0))))
1442 /* MIN (X, C1) < C2 -> X < C2 || C1 < C2 */
1443 (for minmax (min min max max min min max max )
1444 cmp (lt le gt ge gt ge lt le )
1445 comb (bit_ior bit_ior bit_ior bit_ior bit_and bit_and bit_and bit_and)
1447 (cmp (minmax @0 INTEGER_CST@1) INTEGER_CST@2)
1448 (comb (cmp @0 @2) (cmp @1 @2))))
1450 /* Simplifications of shift and rotates. */
1452 (for rotate (lrotate rrotate)
1454 (rotate integer_all_onesp@0 @1)
1457 /* Optimize -1 >> x for arithmetic right shifts. */
1459 (rshift integer_all_onesp@0 @1)
1460 (if (!TYPE_UNSIGNED (type)
1461 && tree_expr_nonnegative_p (@1))
1464 /* Optimize (x >> c) << c into x & (-1<<c). */
1466 (lshift (rshift @0 INTEGER_CST@1) @1)
1467 (if (wi::ltu_p (@1, element_precision (type)))
1468 (bit_and @0 (lshift { build_minus_one_cst (type); } @1))))
1470 /* Optimize (x << c) >> c into x & ((unsigned)-1 >> c) for unsigned
1473 (rshift (lshift @0 INTEGER_CST@1) @1)
1474 (if (TYPE_UNSIGNED (type)
1475 && (wi::ltu_p (@1, element_precision (type))))
1476 (bit_and @0 (rshift { build_minus_one_cst (type); } @1))))
1478 (for shiftrotate (lrotate rrotate lshift rshift)
1480 (shiftrotate @0 integer_zerop)
1483 (shiftrotate integer_zerop@0 @1)
1485 /* Prefer vector1 << scalar to vector1 << vector2
1486 if vector2 is uniform. */
1487 (for vec (VECTOR_CST CONSTRUCTOR)
1489 (shiftrotate @0 vec@1)
1490 (with { tree tem = uniform_vector_p (@1); }
1492 (shiftrotate @0 { tem; }))))))
1494 /* Rewrite an LROTATE_EXPR by a constant into an
1495 RROTATE_EXPR by a new constant. */
1497 (lrotate @0 INTEGER_CST@1)
1498 (rrotate @0 { const_binop (MINUS_EXPR, TREE_TYPE (@1),
1499 build_int_cst (TREE_TYPE (@1),
1500 element_precision (type)), @1); }))
1502 /* Turn (a OP c1) OP c2 into a OP (c1+c2). */
1503 (for op (lrotate rrotate rshift lshift)
1505 (op (op @0 INTEGER_CST@1) INTEGER_CST@2)
1506 (with { unsigned int prec = element_precision (type); }
1507 (if (wi::ge_p (@1, 0, TYPE_SIGN (TREE_TYPE (@1)))
1508 && wi::lt_p (@1, prec, TYPE_SIGN (TREE_TYPE (@1)))
1509 && wi::ge_p (@2, 0, TYPE_SIGN (TREE_TYPE (@2)))
1510 && wi::lt_p (@2, prec, TYPE_SIGN (TREE_TYPE (@2))))
1511 (with { unsigned int low = wi::add (@1, @2).to_uhwi (); }
1512 /* Deal with a OP (c1 + c2) being undefined but (a OP c1) OP c2
1513 being well defined. */
1515 (if (op == LROTATE_EXPR || op == RROTATE_EXPR)
1516 (op @0 { build_int_cst (TREE_TYPE (@1), low % prec); })
1517 (if (TYPE_UNSIGNED (type) || op == LSHIFT_EXPR)
1518 { build_zero_cst (type); }
1519 (op @0 { build_int_cst (TREE_TYPE (@1), prec - 1); })))
1520 (op @0 { build_int_cst (TREE_TYPE (@1), low); })))))))
1523 /* ((1 << A) & 1) != 0 -> A == 0
1524 ((1 << A) & 1) == 0 -> A != 0 */
1528 (cmp (bit_and (lshift integer_onep @0) integer_onep) integer_zerop)
1529 (icmp @0 { build_zero_cst (TREE_TYPE (@0)); })))
1531 /* (CST1 << A) == CST2 -> A == ctz (CST2) - ctz (CST1)
1532 (CST1 << A) != CST2 -> A != ctz (CST2) - ctz (CST1)
1536 (cmp (lshift INTEGER_CST@0 @1) INTEGER_CST@2)
1537 (with { int cand = wi::ctz (@2) - wi::ctz (@0); }
1539 || (!integer_zerop (@2)
1540 && wi::ne_p (wi::lshift (@0, cand), @2)))
1541 { constant_boolean_node (cmp == NE_EXPR, type); }
1542 (if (!integer_zerop (@2)
1543 && wi::eq_p (wi::lshift (@0, cand), @2))
1544 (cmp @1 { build_int_cst (TREE_TYPE (@1), cand); }))))))
1546 /* Fold (X << C1) & C2 into (X << C1) & (C2 | ((1 << C1) - 1))
1547 (X >> C1) & C2 into (X >> C1) & (C2 | ~((type) -1 >> C1))
1548 if the new mask might be further optimized. */
1549 (for shift (lshift rshift)
1551 (bit_and (convert?:s@4 (shift:s@5 (convert1?@3 @0) INTEGER_CST@1))
1553 (if (tree_nop_conversion_p (TREE_TYPE (@4), TREE_TYPE (@5))
1554 && TYPE_PRECISION (type) <= HOST_BITS_PER_WIDE_INT
1555 && tree_fits_uhwi_p (@1)
1556 && tree_to_uhwi (@1) > 0
1557 && tree_to_uhwi (@1) < TYPE_PRECISION (type))
1560 unsigned int shiftc = tree_to_uhwi (@1);
1561 unsigned HOST_WIDE_INT mask = TREE_INT_CST_LOW (@2);
1562 unsigned HOST_WIDE_INT newmask, zerobits = 0;
1563 tree shift_type = TREE_TYPE (@3);
1566 if (shift == LSHIFT_EXPR)
1567 zerobits = ((HOST_WIDE_INT_1U << shiftc) - 1);
1568 else if (shift == RSHIFT_EXPR
1569 && (TYPE_PRECISION (shift_type)
1570 == GET_MODE_PRECISION (TYPE_MODE (shift_type))))
1572 prec = TYPE_PRECISION (TREE_TYPE (@3));
1574 /* See if more bits can be proven as zero because of
1577 && TYPE_UNSIGNED (TREE_TYPE (@0)))
1579 tree inner_type = TREE_TYPE (@0);
1580 if ((TYPE_PRECISION (inner_type)
1581 == GET_MODE_PRECISION (TYPE_MODE (inner_type)))
1582 && TYPE_PRECISION (inner_type) < prec)
1584 prec = TYPE_PRECISION (inner_type);
1585 /* See if we can shorten the right shift. */
1587 shift_type = inner_type;
1588 /* Otherwise X >> C1 is all zeros, so we'll optimize
1589 it into (X, 0) later on by making sure zerobits
1593 zerobits = HOST_WIDE_INT_M1U;
1596 zerobits >>= HOST_BITS_PER_WIDE_INT - shiftc;
1597 zerobits <<= prec - shiftc;
1599 /* For arithmetic shift if sign bit could be set, zerobits
1600 can contain actually sign bits, so no transformation is
1601 possible, unless MASK masks them all away. In that
1602 case the shift needs to be converted into logical shift. */
1603 if (!TYPE_UNSIGNED (TREE_TYPE (@3))
1604 && prec == TYPE_PRECISION (TREE_TYPE (@3)))
1606 if ((mask & zerobits) == 0)
1607 shift_type = unsigned_type_for (TREE_TYPE (@3));
1613 /* ((X << 16) & 0xff00) is (X, 0). */
1614 (if ((mask & zerobits) == mask)
1615 { build_int_cst (type, 0); }
1616 (with { newmask = mask | zerobits; }
1617 (if (newmask != mask && (newmask & (newmask + 1)) == 0)
1620 /* Only do the transformation if NEWMASK is some integer
1622 for (prec = BITS_PER_UNIT;
1623 prec < HOST_BITS_PER_WIDE_INT; prec <<= 1)
1624 if (newmask == (HOST_WIDE_INT_1U << prec) - 1)
1627 (if (prec < HOST_BITS_PER_WIDE_INT
1628 || newmask == HOST_WIDE_INT_M1U)
1630 { tree newmaskt = build_int_cst_type (TREE_TYPE (@2), newmask); }
1631 (if (!tree_int_cst_equal (newmaskt, @2))
1632 (if (shift_type != TREE_TYPE (@3))
1633 (bit_and (convert (shift:shift_type (convert @3) @1)) { newmaskt; })
1634 (bit_and @4 { newmaskt; })))))))))))))
1636 /* Fold (X {&,^,|} C2) << C1 into (X << C1) {&,^,|} (C2 << C1)
1637 (X {&,^,|} C2) >> C1 into (X >> C1) & (C2 >> C1). */
1638 (for shift (lshift rshift)
1639 (for bit_op (bit_and bit_xor bit_ior)
1641 (shift (convert?:s (bit_op:s @0 INTEGER_CST@2)) INTEGER_CST@1)
1642 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1643 (with { tree mask = int_const_binop (shift, fold_convert (type, @2), @1); }
1644 (bit_op (shift (convert @0) @1) { mask; }))))))
1646 /* ~(~X >> Y) -> X >> Y (for arithmetic shift). */
1648 (bit_not (convert1?:s (rshift:s (convert2?@0 (bit_not @1)) @2)))
1649 (if (!TYPE_UNSIGNED (TREE_TYPE (@0))
1650 && (element_precision (TREE_TYPE (@0))
1651 <= element_precision (TREE_TYPE (@1))
1652 || !TYPE_UNSIGNED (TREE_TYPE (@1))))
1654 { tree shift_type = TREE_TYPE (@0); }
1655 (convert (rshift (convert:shift_type @1) @2)))))
1657 /* ~(~X >>r Y) -> X >>r Y
1658 ~(~X <<r Y) -> X <<r Y */
1659 (for rotate (lrotate rrotate)
1661 (bit_not (convert1?:s (rotate:s (convert2?@0 (bit_not @1)) @2)))
1662 (if ((element_precision (TREE_TYPE (@0))
1663 <= element_precision (TREE_TYPE (@1))
1664 || !TYPE_UNSIGNED (TREE_TYPE (@1)))
1665 && (element_precision (type) <= element_precision (TREE_TYPE (@0))
1666 || !TYPE_UNSIGNED (TREE_TYPE (@0))))
1668 { tree rotate_type = TREE_TYPE (@0); }
1669 (convert (rotate (convert:rotate_type @1) @2))))))
1671 /* Simplifications of conversions. */
1673 /* Basic strip-useless-type-conversions / strip_nops. */
1674 (for cvt (convert view_convert float fix_trunc)
1677 (if ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@0)))
1678 || (GENERIC && type == TREE_TYPE (@0)))
1681 /* Contract view-conversions. */
1683 (view_convert (view_convert @0))
1686 /* For integral conversions with the same precision or pointer
1687 conversions use a NOP_EXPR instead. */
1690 (if ((INTEGRAL_TYPE_P (type) || POINTER_TYPE_P (type))
1691 && (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
1692 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (@0)))
1695 /* Strip inner integral conversions that do not change precision or size. */
1697 (view_convert (convert@0 @1))
1698 (if ((INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
1699 && (INTEGRAL_TYPE_P (TREE_TYPE (@1)) || POINTER_TYPE_P (TREE_TYPE (@1)))
1700 && (TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
1701 && (TYPE_SIZE (TREE_TYPE (@0)) == TYPE_SIZE (TREE_TYPE (@1))))
1704 /* Re-association barriers around constants and other re-association
1705 barriers can be removed. */
1707 (paren CONSTANT_CLASS_P@0)
1710 (paren (paren@1 @0))
1713 /* Handle cases of two conversions in a row. */
1714 (for ocvt (convert float fix_trunc)
1715 (for icvt (convert float)
1720 tree inside_type = TREE_TYPE (@0);
1721 tree inter_type = TREE_TYPE (@1);
1722 int inside_int = INTEGRAL_TYPE_P (inside_type);
1723 int inside_ptr = POINTER_TYPE_P (inside_type);
1724 int inside_float = FLOAT_TYPE_P (inside_type);
1725 int inside_vec = VECTOR_TYPE_P (inside_type);
1726 unsigned int inside_prec = TYPE_PRECISION (inside_type);
1727 int inside_unsignedp = TYPE_UNSIGNED (inside_type);
1728 int inter_int = INTEGRAL_TYPE_P (inter_type);
1729 int inter_ptr = POINTER_TYPE_P (inter_type);
1730 int inter_float = FLOAT_TYPE_P (inter_type);
1731 int inter_vec = VECTOR_TYPE_P (inter_type);
1732 unsigned int inter_prec = TYPE_PRECISION (inter_type);
1733 int inter_unsignedp = TYPE_UNSIGNED (inter_type);
1734 int final_int = INTEGRAL_TYPE_P (type);
1735 int final_ptr = POINTER_TYPE_P (type);
1736 int final_float = FLOAT_TYPE_P (type);
1737 int final_vec = VECTOR_TYPE_P (type);
1738 unsigned int final_prec = TYPE_PRECISION (type);
1739 int final_unsignedp = TYPE_UNSIGNED (type);
1742 /* In addition to the cases of two conversions in a row
1743 handled below, if we are converting something to its own
1744 type via an object of identical or wider precision, neither
1745 conversion is needed. */
1746 (if (((GIMPLE && useless_type_conversion_p (type, inside_type))
1748 && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (inside_type)))
1749 && (((inter_int || inter_ptr) && final_int)
1750 || (inter_float && final_float))
1751 && inter_prec >= final_prec)
1754 /* Likewise, if the intermediate and initial types are either both
1755 float or both integer, we don't need the middle conversion if the
1756 former is wider than the latter and doesn't change the signedness
1757 (for integers). Avoid this if the final type is a pointer since
1758 then we sometimes need the middle conversion. */
1759 (if (((inter_int && inside_int) || (inter_float && inside_float))
1760 && (final_int || final_float)
1761 && inter_prec >= inside_prec
1762 && (inter_float || inter_unsignedp == inside_unsignedp))
1765 /* If we have a sign-extension of a zero-extended value, we can
1766 replace that by a single zero-extension. Likewise if the
1767 final conversion does not change precision we can drop the
1768 intermediate conversion. */
1769 (if (inside_int && inter_int && final_int
1770 && ((inside_prec < inter_prec && inter_prec < final_prec
1771 && inside_unsignedp && !inter_unsignedp)
1772 || final_prec == inter_prec))
1775 /* Two conversions in a row are not needed unless:
1776 - some conversion is floating-point (overstrict for now), or
1777 - some conversion is a vector (overstrict for now), or
1778 - the intermediate type is narrower than both initial and
1780 - the intermediate type and innermost type differ in signedness,
1781 and the outermost type is wider than the intermediate, or
1782 - the initial type is a pointer type and the precisions of the
1783 intermediate and final types differ, or
1784 - the final type is a pointer type and the precisions of the
1785 initial and intermediate types differ. */
1786 (if (! inside_float && ! inter_float && ! final_float
1787 && ! inside_vec && ! inter_vec && ! final_vec
1788 && (inter_prec >= inside_prec || inter_prec >= final_prec)
1789 && ! (inside_int && inter_int
1790 && inter_unsignedp != inside_unsignedp
1791 && inter_prec < final_prec)
1792 && ((inter_unsignedp && inter_prec > inside_prec)
1793 == (final_unsignedp && final_prec > inter_prec))
1794 && ! (inside_ptr && inter_prec != final_prec)
1795 && ! (final_ptr && inside_prec != inter_prec))
1798 /* A truncation to an unsigned type (a zero-extension) should be
1799 canonicalized as bitwise and of a mask. */
1800 (if (GIMPLE /* PR70366: doing this in GENERIC breaks -Wconversion. */
1801 && final_int && inter_int && inside_int
1802 && final_prec == inside_prec
1803 && final_prec > inter_prec
1805 (convert (bit_and @0 { wide_int_to_tree
1807 wi::mask (inter_prec, false,
1808 TYPE_PRECISION (inside_type))); })))
1810 /* If we are converting an integer to a floating-point that can
1811 represent it exactly and back to an integer, we can skip the
1812 floating-point conversion. */
1813 (if (GIMPLE /* PR66211 */
1814 && inside_int && inter_float && final_int &&
1815 (unsigned) significand_size (TYPE_MODE (inter_type))
1816 >= inside_prec - !inside_unsignedp)
1819 /* If we have a narrowing conversion to an integral type that is fed by a
1820 BIT_AND_EXPR, we might be able to remove the BIT_AND_EXPR if it merely
1821 masks off bits outside the final type (and nothing else). */
1823 (convert (bit_and @0 INTEGER_CST@1))
1824 (if (INTEGRAL_TYPE_P (type)
1825 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
1826 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))
1827 && operand_equal_p (@1, build_low_bits_mask (TREE_TYPE (@1),
1828 TYPE_PRECISION (type)), 0))
1832 /* (X /[ex] A) * A -> X. */
1834 (mult (convert1? (exact_div @0 @@1)) (convert2? @1))
1837 /* Canonicalization of binary operations. */
1839 /* Convert X + -C into X - C. */
1841 (plus @0 REAL_CST@1)
1842 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
1843 (with { tree tem = const_unop (NEGATE_EXPR, type, @1); }
1844 (if (!TREE_OVERFLOW (tem) || !flag_trapping_math)
1845 (minus @0 { tem; })))))
1847 /* Convert x+x into x*2. */
1850 (if (SCALAR_FLOAT_TYPE_P (type))
1851 (mult @0 { build_real (type, dconst2); })
1852 (if (INTEGRAL_TYPE_P (type))
1853 (mult @0 { build_int_cst (type, 2); }))))
1856 (minus integer_zerop @1)
1859 /* (ARG0 - ARG1) is the same as (-ARG1 + ARG0). So check whether
1860 ARG0 is zero and X + ARG0 reduces to X, since that would mean
1861 (-ARG1 + ARG0) reduces to -ARG1. */
1863 (minus real_zerop@0 @1)
1864 (if (fold_real_zero_addition_p (type, @0, 0))
1867 /* Transform x * -1 into -x. */
1869 (mult @0 integer_minus_onep)
1872 /* True if we can easily extract the real and imaginary parts of a complex
1874 (match compositional_complex
1875 (convert? (complex @0 @1)))
1877 /* COMPLEX_EXPR and REALPART/IMAGPART_EXPR cancellations. */
1879 (complex (realpart @0) (imagpart @0))
1882 (realpart (complex @0 @1))
1885 (imagpart (complex @0 @1))
1888 /* Sometimes we only care about half of a complex expression. */
1890 (realpart (convert?:s (conj:s @0)))
1891 (convert (realpart @0)))
1893 (imagpart (convert?:s (conj:s @0)))
1894 (convert (negate (imagpart @0))))
1895 (for part (realpart imagpart)
1896 (for op (plus minus)
1898 (part (convert?:s@2 (op:s @0 @1)))
1899 (convert (op (part @0) (part @1))))))
1901 (realpart (convert?:s (CEXPI:s @0)))
1904 (imagpart (convert?:s (CEXPI:s @0)))
1907 /* conj(conj(x)) -> x */
1909 (conj (convert? (conj @0)))
1910 (if (tree_nop_conversion_p (TREE_TYPE (@0), type))
1913 /* conj({x,y}) -> {x,-y} */
1915 (conj (convert?:s (complex:s @0 @1)))
1916 (with { tree itype = TREE_TYPE (type); }
1917 (complex (convert:itype @0) (negate (convert:itype @1)))))
1919 /* BSWAP simplifications, transforms checked by gcc.dg/builtin-bswap-8.c. */
1920 (for bswap (BUILT_IN_BSWAP16 BUILT_IN_BSWAP32 BUILT_IN_BSWAP64)
1925 (bswap (bit_not (bswap @0)))
1927 (for bitop (bit_xor bit_ior bit_and)
1929 (bswap (bitop:c (bswap @0) @1))
1930 (bitop @0 (bswap @1)))))
1933 /* Combine COND_EXPRs and VEC_COND_EXPRs. */
1935 /* Simplify constant conditions.
1936 Only optimize constant conditions when the selected branch
1937 has the same type as the COND_EXPR. This avoids optimizing
1938 away "c ? x : throw", where the throw has a void type.
1939 Note that we cannot throw away the fold-const.c variant nor
1940 this one as we depend on doing this transform before possibly
1941 A ? B : B -> B triggers and the fold-const.c one can optimize
1942 0 ? A : B to B even if A has side-effects. Something
1943 genmatch cannot handle. */
1945 (cond INTEGER_CST@0 @1 @2)
1946 (if (integer_zerop (@0))
1947 (if (!VOID_TYPE_P (TREE_TYPE (@2)) || VOID_TYPE_P (type))
1949 (if (!VOID_TYPE_P (TREE_TYPE (@1)) || VOID_TYPE_P (type))
1952 (vec_cond VECTOR_CST@0 @1 @2)
1953 (if (integer_all_onesp (@0))
1955 (if (integer_zerop (@0))
1958 (for cnd (cond vec_cond)
1959 /* A ? B : (A ? X : C) -> A ? B : C. */
1961 (cnd @0 (cnd @0 @1 @2) @3)
1964 (cnd @0 @1 (cnd @0 @2 @3))
1966 /* A ? B : (!A ? C : X) -> A ? B : C. */
1967 /* ??? This matches embedded conditions open-coded because genmatch
1968 would generate matching code for conditions in separate stmts only.
1969 The following is still important to merge then and else arm cases
1970 from if-conversion. */
1972 (cnd @0 @1 (cnd @2 @3 @4))
1973 (if (COMPARISON_CLASS_P (@0)
1974 && COMPARISON_CLASS_P (@2)
1975 && invert_tree_comparison
1976 (TREE_CODE (@0), HONOR_NANS (TREE_OPERAND (@0, 0))) == TREE_CODE (@2)
1977 && operand_equal_p (TREE_OPERAND (@0, 0), TREE_OPERAND (@2, 0), 0)
1978 && operand_equal_p (TREE_OPERAND (@0, 1), TREE_OPERAND (@2, 1), 0))
1981 (cnd @0 (cnd @1 @2 @3) @4)
1982 (if (COMPARISON_CLASS_P (@0)
1983 && COMPARISON_CLASS_P (@1)
1984 && invert_tree_comparison
1985 (TREE_CODE (@0), HONOR_NANS (TREE_OPERAND (@0, 0))) == TREE_CODE (@1)
1986 && operand_equal_p (TREE_OPERAND (@0, 0), TREE_OPERAND (@1, 0), 0)
1987 && operand_equal_p (TREE_OPERAND (@0, 1), TREE_OPERAND (@1, 1), 0))
1990 /* A ? B : B -> B. */
1995 /* !A ? B : C -> A ? C : B. */
1997 (cnd (logical_inverted_value truth_valued_p@0) @1 @2)
2000 /* A + (B vcmp C ? 1 : 0) -> A - (B vcmp C ? -1 : 0), since vector comparisons
2001 return all -1 or all 0 results. */
2002 /* ??? We could instead convert all instances of the vec_cond to negate,
2003 but that isn't necessarily a win on its own. */
2005 (plus:c @3 (view_convert? (vec_cond:s @0 integer_each_onep@1 integer_zerop@2)))
2006 (if (VECTOR_TYPE_P (type)
2007 && TYPE_VECTOR_SUBPARTS (type) == TYPE_VECTOR_SUBPARTS (TREE_TYPE (@1))
2008 && (TYPE_MODE (TREE_TYPE (type))
2009 == TYPE_MODE (TREE_TYPE (TREE_TYPE (@1)))))
2010 (minus @3 (view_convert (vec_cond @0 (negate @1) @2)))))
2012 /* ... likewise A - (B vcmp C ? 1 : 0) -> A + (B vcmp C ? -1 : 0). */
2014 (minus @3 (view_convert? (vec_cond:s @0 integer_each_onep@1 integer_zerop@2)))
2015 (if (VECTOR_TYPE_P (type)
2016 && TYPE_VECTOR_SUBPARTS (type) == TYPE_VECTOR_SUBPARTS (TREE_TYPE (@1))
2017 && (TYPE_MODE (TREE_TYPE (type))
2018 == TYPE_MODE (TREE_TYPE (TREE_TYPE (@1)))))
2019 (plus @3 (view_convert (vec_cond @0 (negate @1) @2)))))
2022 /* Simplifications of comparisons. */
2024 /* See if we can reduce the magnitude of a constant involved in a
2025 comparison by changing the comparison code. This is a canonicalization
2026 formerly done by maybe_canonicalize_comparison_1. */
2030 (cmp @0 INTEGER_CST@1)
2031 (if (tree_int_cst_sgn (@1) == -1)
2032 (acmp @0 { wide_int_to_tree (TREE_TYPE (@1), wi::add (@1, 1)); }))))
2036 (cmp @0 INTEGER_CST@1)
2037 (if (tree_int_cst_sgn (@1) == 1)
2038 (acmp @0 { wide_int_to_tree (TREE_TYPE (@1), wi::sub (@1, 1)); }))))
2041 /* We can simplify a logical negation of a comparison to the
2042 inverted comparison. As we cannot compute an expression
2043 operator using invert_tree_comparison we have to simulate
2044 that with expression code iteration. */
2045 (for cmp (tcc_comparison)
2046 icmp (inverted_tcc_comparison)
2047 ncmp (inverted_tcc_comparison_with_nans)
2048 /* Ideally we'd like to combine the following two patterns
2049 and handle some more cases by using
2050 (logical_inverted_value (cmp @0 @1))
2051 here but for that genmatch would need to "inline" that.
2052 For now implement what forward_propagate_comparison did. */
2054 (bit_not (cmp @0 @1))
2055 (if (VECTOR_TYPE_P (type)
2056 || (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1))
2057 /* Comparison inversion may be impossible for trapping math,
2058 invert_tree_comparison will tell us. But we can't use
2059 a computed operator in the replacement tree thus we have
2060 to play the trick below. */
2061 (with { enum tree_code ic = invert_tree_comparison
2062 (cmp, HONOR_NANS (@0)); }
2068 (bit_xor (cmp @0 @1) integer_truep)
2069 (with { enum tree_code ic = invert_tree_comparison
2070 (cmp, HONOR_NANS (@0)); }
2076 /* Transform comparisons of the form X - Y CMP 0 to X CMP Y.
2077 ??? The transformation is valid for the other operators if overflow
2078 is undefined for the type, but performing it here badly interacts
2079 with the transformation in fold_cond_expr_with_comparison which
2080 attempts to synthetize ABS_EXPR. */
2083 (cmp (minus@2 @0 @1) integer_zerop)
2084 (if (single_use (@2))
2087 /* Transform comparisons of the form X * C1 CMP 0 to X CMP 0 in the
2088 signed arithmetic case. That form is created by the compiler
2089 often enough for folding it to be of value. One example is in
2090 computing loop trip counts after Operator Strength Reduction. */
2091 (for cmp (simple_comparison)
2092 scmp (swapped_simple_comparison)
2094 (cmp (mult@3 @0 INTEGER_CST@1) integer_zerop@2)
2095 /* Handle unfolded multiplication by zero. */
2096 (if (integer_zerop (@1))
2098 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2099 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2101 /* If @1 is negative we swap the sense of the comparison. */
2102 (if (tree_int_cst_sgn (@1) < 0)
2106 /* Simplify comparison of something with itself. For IEEE
2107 floating-point, we can only do some of these simplifications. */
2111 (if (! FLOAT_TYPE_P (TREE_TYPE (@0))
2112 || ! HONOR_NANS (@0))
2113 { constant_boolean_node (true, type); }
2114 (if (cmp != EQ_EXPR)
2120 || ! FLOAT_TYPE_P (TREE_TYPE (@0))
2121 || ! HONOR_NANS (@0))
2122 { constant_boolean_node (false, type); })))
2123 (for cmp (unle unge uneq)
2126 { constant_boolean_node (true, type); }))
2127 (for cmp (unlt ungt)
2133 (if (!flag_trapping_math)
2134 { constant_boolean_node (false, type); }))
2136 /* Fold ~X op ~Y as Y op X. */
2137 (for cmp (simple_comparison)
2139 (cmp (bit_not@2 @0) (bit_not@3 @1))
2140 (if (single_use (@2) && single_use (@3))
2143 /* Fold ~X op C as X op' ~C, where op' is the swapped comparison. */
2144 (for cmp (simple_comparison)
2145 scmp (swapped_simple_comparison)
2147 (cmp (bit_not@2 @0) CONSTANT_CLASS_P@1)
2148 (if (single_use (@2)
2149 && (TREE_CODE (@1) == INTEGER_CST || TREE_CODE (@1) == VECTOR_CST))
2150 (scmp @0 (bit_not @1)))))
2152 (for cmp (simple_comparison)
2153 /* Fold (double)float1 CMP (double)float2 into float1 CMP float2. */
2155 (cmp (convert@2 @0) (convert? @1))
2156 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
2157 && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@2))
2158 == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0)))
2159 && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@2))
2160 == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1))))
2163 tree type1 = TREE_TYPE (@1);
2164 if (TREE_CODE (@1) == REAL_CST && !DECIMAL_FLOAT_TYPE_P (type1))
2166 REAL_VALUE_TYPE orig = TREE_REAL_CST (@1);
2167 if (TYPE_PRECISION (type1) > TYPE_PRECISION (float_type_node)
2168 && exact_real_truncate (TYPE_MODE (float_type_node), &orig))
2169 type1 = float_type_node;
2170 if (TYPE_PRECISION (type1) > TYPE_PRECISION (double_type_node)
2171 && exact_real_truncate (TYPE_MODE (double_type_node), &orig))
2172 type1 = double_type_node;
2175 = (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type1)
2176 ? TREE_TYPE (@0) : type1);
2178 (if (TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (newtype))
2179 (cmp (convert:newtype @0) (convert:newtype @1))))))
2183 /* IEEE doesn't distinguish +0 and -0 in comparisons. */
2185 /* a CMP (-0) -> a CMP 0 */
2186 (if (REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@1)))
2187 (cmp @0 { build_real (TREE_TYPE (@1), dconst0); }))
2188 /* x != NaN is always true, other ops are always false. */
2189 (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
2190 && ! HONOR_SNANS (@1))
2191 { constant_boolean_node (cmp == NE_EXPR, type); })
2192 /* Fold comparisons against infinity. */
2193 (if (REAL_VALUE_ISINF (TREE_REAL_CST (@1))
2194 && MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (@1))))
2197 REAL_VALUE_TYPE max;
2198 enum tree_code code = cmp;
2199 bool neg = REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1));
2201 code = swap_tree_comparison (code);
2204 /* x > +Inf is always false, if with ignore sNANs. */
2205 (if (code == GT_EXPR
2206 && ! HONOR_SNANS (@0))
2207 { constant_boolean_node (false, type); })
2208 (if (code == LE_EXPR)
2209 /* x <= +Inf is always true, if we don't case about NaNs. */
2210 (if (! HONOR_NANS (@0))
2211 { constant_boolean_node (true, type); }
2212 /* x <= +Inf is the same as x == x, i.e. !isnan(x). */
2214 /* x == +Inf and x >= +Inf are always equal to x > DBL_MAX. */
2215 (if (code == EQ_EXPR || code == GE_EXPR)
2216 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
2218 (lt @0 { build_real (TREE_TYPE (@0), max); })
2219 (gt @0 { build_real (TREE_TYPE (@0), max); }))))
2220 /* x < +Inf is always equal to x <= DBL_MAX. */
2221 (if (code == LT_EXPR)
2222 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
2224 (ge @0 { build_real (TREE_TYPE (@0), max); })
2225 (le @0 { build_real (TREE_TYPE (@0), max); }))))
2226 /* x != +Inf is always equal to !(x > DBL_MAX). */
2227 (if (code == NE_EXPR)
2228 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
2229 (if (! HONOR_NANS (@0))
2231 (ge @0 { build_real (TREE_TYPE (@0), max); })
2232 (le @0 { build_real (TREE_TYPE (@0), max); }))
2234 (bit_xor (lt @0 { build_real (TREE_TYPE (@0), max); })
2235 { build_one_cst (type); })
2236 (bit_xor (gt @0 { build_real (TREE_TYPE (@0), max); })
2237 { build_one_cst (type); }))))))))))
2239 /* If this is a comparison of a real constant with a PLUS_EXPR
2240 or a MINUS_EXPR of a real constant, we can convert it into a
2241 comparison with a revised real constant as long as no overflow
2242 occurs when unsafe_math_optimizations are enabled. */
2243 (if (flag_unsafe_math_optimizations)
2244 (for op (plus minus)
2246 (cmp (op @0 REAL_CST@1) REAL_CST@2)
2249 tree tem = const_binop (op == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR,
2250 TREE_TYPE (@1), @2, @1);
2252 (if (tem && !TREE_OVERFLOW (tem))
2253 (cmp @0 { tem; }))))))
2255 /* Likewise, we can simplify a comparison of a real constant with
2256 a MINUS_EXPR whose first operand is also a real constant, i.e.
2257 (c1 - x) < c2 becomes x > c1-c2. Reordering is allowed on
2258 floating-point types only if -fassociative-math is set. */
2259 (if (flag_associative_math)
2261 (cmp (minus REAL_CST@0 @1) REAL_CST@2)
2262 (with { tree tem = const_binop (MINUS_EXPR, TREE_TYPE (@1), @0, @2); }
2263 (if (tem && !TREE_OVERFLOW (tem))
2264 (cmp { tem; } @1)))))
2266 /* Fold comparisons against built-in math functions. */
2267 (if (flag_unsafe_math_optimizations
2268 && ! flag_errno_math)
2271 (cmp (sq @0) REAL_CST@1)
2273 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
2275 /* sqrt(x) < y is always false, if y is negative. */
2276 (if (cmp == EQ_EXPR || cmp == LT_EXPR || cmp == LE_EXPR)
2277 { constant_boolean_node (false, type); })
2278 /* sqrt(x) > y is always true, if y is negative and we
2279 don't care about NaNs, i.e. negative values of x. */
2280 (if (cmp == NE_EXPR || !HONOR_NANS (@0))
2281 { constant_boolean_node (true, type); })
2282 /* sqrt(x) > y is the same as x >= 0, if y is negative. */
2283 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })))
2284 (if (real_equal (TREE_REAL_CST_PTR (@1), &dconst0))
2286 /* sqrt(x) < 0 is always false. */
2287 (if (cmp == LT_EXPR)
2288 { constant_boolean_node (false, type); })
2289 /* sqrt(x) >= 0 is always true if we don't care about NaNs. */
2290 (if (cmp == GE_EXPR && !HONOR_NANS (@0))
2291 { constant_boolean_node (true, type); })
2292 /* sqrt(x) <= 0 -> x == 0. */
2293 (if (cmp == LE_EXPR)
2295 /* Otherwise sqrt(x) cmp 0 -> x cmp 0. Here cmp can be >=, >,
2296 == or !=. In the last case:
2298 (sqrt(x) != 0) == (NaN != 0) == true == (x != 0)
2300 if x is negative or NaN. Due to -funsafe-math-optimizations,
2301 the results for other x follow from natural arithmetic. */
2303 (if (cmp == GT_EXPR || cmp == GE_EXPR)
2307 real_arithmetic (&c2, MULT_EXPR,
2308 &TREE_REAL_CST (@1), &TREE_REAL_CST (@1));
2309 real_convert (&c2, TYPE_MODE (TREE_TYPE (@0)), &c2);
2311 (if (REAL_VALUE_ISINF (c2))
2312 /* sqrt(x) > y is x == +Inf, when y is very large. */
2313 (if (HONOR_INFINITIES (@0))
2314 (eq @0 { build_real (TREE_TYPE (@0), c2); })
2315 { constant_boolean_node (false, type); })
2316 /* sqrt(x) > c is the same as x > c*c. */
2317 (cmp @0 { build_real (TREE_TYPE (@0), c2); }))))
2318 (if (cmp == LT_EXPR || cmp == LE_EXPR)
2322 real_arithmetic (&c2, MULT_EXPR,
2323 &TREE_REAL_CST (@1), &TREE_REAL_CST (@1));
2324 real_convert (&c2, TYPE_MODE (TREE_TYPE (@0)), &c2);
2326 (if (REAL_VALUE_ISINF (c2))
2328 /* sqrt(x) < y is always true, when y is a very large
2329 value and we don't care about NaNs or Infinities. */
2330 (if (! HONOR_NANS (@0) && ! HONOR_INFINITIES (@0))
2331 { constant_boolean_node (true, type); })
2332 /* sqrt(x) < y is x != +Inf when y is very large and we
2333 don't care about NaNs. */
2334 (if (! HONOR_NANS (@0))
2335 (ne @0 { build_real (TREE_TYPE (@0), c2); }))
2336 /* sqrt(x) < y is x >= 0 when y is very large and we
2337 don't care about Infinities. */
2338 (if (! HONOR_INFINITIES (@0))
2339 (ge @0 { build_real (TREE_TYPE (@0), dconst0); }))
2340 /* sqrt(x) < y is x >= 0 && x != +Inf, when y is large. */
2343 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
2344 (ne @0 { build_real (TREE_TYPE (@0), c2); }))))
2345 /* sqrt(x) < c is the same as x < c*c, if we ignore NaNs. */
2346 (if (! HONOR_NANS (@0))
2347 (cmp @0 { build_real (TREE_TYPE (@0), c2); })
2348 /* sqrt(x) < c is the same as x >= 0 && x < c*c. */
2351 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
2352 (cmp @0 { build_real (TREE_TYPE (@0), c2); }))))))))))))
2354 /* Fold A /[ex] B CMP C to A CMP B * C. */
2357 (cmp (exact_div @0 @1) INTEGER_CST@2)
2358 (if (!integer_zerop (@1))
2359 (if (wi::eq_p (@2, 0))
2361 (if (TREE_CODE (@1) == INTEGER_CST)
2365 wide_int prod = wi::mul (@2, @1, TYPE_SIGN (TREE_TYPE (@1)), &ovf);
2368 { constant_boolean_node (cmp == NE_EXPR, type); }
2369 (cmp @0 { wide_int_to_tree (TREE_TYPE (@0), prod); }))))))))
2370 (for cmp (lt le gt ge)
2372 (cmp (exact_div @0 INTEGER_CST@1) INTEGER_CST@2)
2373 (if (wi::gt_p (@1, 0, TYPE_SIGN (TREE_TYPE (@1))))
2377 wide_int prod = wi::mul (@2, @1, TYPE_SIGN (TREE_TYPE (@1)), &ovf);
2380 { constant_boolean_node (wi::lt_p (@2, 0, TYPE_SIGN (TREE_TYPE (@2)))
2381 != (cmp == LT_EXPR || cmp == LE_EXPR), type); }
2382 (cmp @0 { wide_int_to_tree (TREE_TYPE (@0), prod); }))))))
2384 /* Unordered tests if either argument is a NaN. */
2386 (bit_ior (unordered @0 @0) (unordered @1 @1))
2387 (if (types_match (@0, @1))
2390 (bit_and (ordered @0 @0) (ordered @1 @1))
2391 (if (types_match (@0, @1))
2394 (bit_ior:c (unordered @0 @0) (unordered:c@2 @0 @1))
2397 (bit_and:c (ordered @0 @0) (ordered:c@2 @0 @1))
2400 /* Simple range test simplifications. */
2401 /* A < B || A >= B -> true. */
2402 (for test1 (lt le le le ne ge)
2403 test2 (ge gt ge ne eq ne)
2405 (bit_ior:c (test1 @0 @1) (test2 @0 @1))
2406 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2407 || VECTOR_INTEGER_TYPE_P (TREE_TYPE (@0)))
2408 { constant_boolean_node (true, type); })))
2409 /* A < B && A >= B -> false. */
2410 (for test1 (lt lt lt le ne eq)
2411 test2 (ge gt eq gt eq gt)
2413 (bit_and:c (test1 @0 @1) (test2 @0 @1))
2414 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2415 || VECTOR_INTEGER_TYPE_P (TREE_TYPE (@0)))
2416 { constant_boolean_node (false, type); })))
2418 /* -A CMP -B -> B CMP A. */
2419 (for cmp (tcc_comparison)
2420 scmp (swapped_tcc_comparison)
2422 (cmp (negate @0) (negate @1))
2423 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
2424 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2425 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
2428 (cmp (negate @0) CONSTANT_CLASS_P@1)
2429 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
2430 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2431 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
2432 (with { tree tem = const_unop (NEGATE_EXPR, TREE_TYPE (@0), @1); }
2433 (if (tem && !TREE_OVERFLOW (tem))
2434 (scmp @0 { tem; }))))))
2436 /* Convert ABS_EXPR<x> == 0 or ABS_EXPR<x> != 0 to x == 0 or x != 0. */
2439 (op (abs @0) zerop@1)
2442 /* From fold_sign_changed_comparison and fold_widened_comparison. */
2443 (for cmp (simple_comparison)
2445 (cmp (convert@0 @00) (convert?@1 @10))
2446 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2447 /* Disable this optimization if we're casting a function pointer
2448 type on targets that require function pointer canonicalization. */
2449 && !(targetm.have_canonicalize_funcptr_for_compare ()
2450 && TREE_CODE (TREE_TYPE (@00)) == POINTER_TYPE
2451 && TREE_CODE (TREE_TYPE (TREE_TYPE (@00))) == FUNCTION_TYPE)
2453 (if (TYPE_PRECISION (TREE_TYPE (@00)) == TYPE_PRECISION (TREE_TYPE (@0))
2454 && (TREE_CODE (@10) == INTEGER_CST
2455 || (@1 != @10 && types_match (TREE_TYPE (@10), TREE_TYPE (@00))))
2456 && (TYPE_UNSIGNED (TREE_TYPE (@00)) == TYPE_UNSIGNED (TREE_TYPE (@0))
2459 && (POINTER_TYPE_P (TREE_TYPE (@00)) == POINTER_TYPE_P (TREE_TYPE (@0))))
2460 /* ??? The special-casing of INTEGER_CST conversion was in the original
2461 code and here to avoid a spurious overflow flag on the resulting
2462 constant which fold_convert produces. */
2463 (if (TREE_CODE (@1) == INTEGER_CST)
2464 (cmp @00 { force_fit_type (TREE_TYPE (@00), wi::to_widest (@1), 0,
2465 TREE_OVERFLOW (@1)); })
2466 (cmp @00 (convert @1)))
2468 (if (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (TREE_TYPE (@00)))
2469 /* If possible, express the comparison in the shorter mode. */
2470 (if ((cmp == EQ_EXPR || cmp == NE_EXPR
2471 || TYPE_UNSIGNED (TREE_TYPE (@0)) == TYPE_UNSIGNED (TREE_TYPE (@00))
2472 || (!TYPE_UNSIGNED (TREE_TYPE (@0))
2473 && TYPE_UNSIGNED (TREE_TYPE (@00))))
2474 && (types_match (TREE_TYPE (@10), TREE_TYPE (@00))
2475 || ((TYPE_PRECISION (TREE_TYPE (@00))
2476 >= TYPE_PRECISION (TREE_TYPE (@10)))
2477 && (TYPE_UNSIGNED (TREE_TYPE (@00))
2478 == TYPE_UNSIGNED (TREE_TYPE (@10))))
2479 || (TREE_CODE (@10) == INTEGER_CST
2480 && INTEGRAL_TYPE_P (TREE_TYPE (@00))
2481 && int_fits_type_p (@10, TREE_TYPE (@00)))))
2482 (cmp @00 (convert @10))
2483 (if (TREE_CODE (@10) == INTEGER_CST
2484 && INTEGRAL_TYPE_P (TREE_TYPE (@00))
2485 && !int_fits_type_p (@10, TREE_TYPE (@00)))
2488 tree min = lower_bound_in_type (TREE_TYPE (@10), TREE_TYPE (@00));
2489 tree max = upper_bound_in_type (TREE_TYPE (@10), TREE_TYPE (@00));
2490 bool above = integer_nonzerop (const_binop (LT_EXPR, type, max, @10));
2491 bool below = integer_nonzerop (const_binop (LT_EXPR, type, @10, min));
2493 (if (above || below)
2494 (if (cmp == EQ_EXPR || cmp == NE_EXPR)
2495 { constant_boolean_node (cmp == EQ_EXPR ? false : true, type); }
2496 (if (cmp == LT_EXPR || cmp == LE_EXPR)
2497 { constant_boolean_node (above ? true : false, type); }
2498 (if (cmp == GT_EXPR || cmp == GE_EXPR)
2499 { constant_boolean_node (above ? false : true, type); }))))))))))))
2502 /* A local variable can never be pointed to by
2503 the default SSA name of an incoming parameter.
2504 SSA names are canonicalized to 2nd place. */
2506 (cmp addr@0 SSA_NAME@1)
2507 (if (SSA_NAME_IS_DEFAULT_DEF (@1)
2508 && TREE_CODE (SSA_NAME_VAR (@1)) == PARM_DECL)
2509 (with { tree base = get_base_address (TREE_OPERAND (@0, 0)); }
2510 (if (TREE_CODE (base) == VAR_DECL
2511 && auto_var_in_fn_p (base, current_function_decl))
2512 (if (cmp == NE_EXPR)
2513 { constant_boolean_node (true, type); }
2514 { constant_boolean_node (false, type); }))))))
2516 /* Equality compare simplifications from fold_binary */
2519 /* If we have (A | C) == D where C & ~D != 0, convert this into 0.
2520 Similarly for NE_EXPR. */
2522 (cmp (convert?@3 (bit_ior @0 INTEGER_CST@1)) INTEGER_CST@2)
2523 (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0))
2524 && wi::bit_and_not (@1, @2) != 0)
2525 { constant_boolean_node (cmp == NE_EXPR, type); }))
2527 /* (X ^ Y) == 0 becomes X == Y, and (X ^ Y) != 0 becomes X != Y. */
2529 (cmp (bit_xor @0 @1) integer_zerop)
2532 /* (X ^ Y) == Y becomes X == 0.
2533 Likewise (X ^ Y) == X becomes Y == 0. */
2535 (cmp:c (bit_xor:c @0 @1) @0)
2536 (cmp @1 { build_zero_cst (TREE_TYPE (@1)); }))
2538 /* (X ^ C1) op C2 can be rewritten as X op (C1 ^ C2). */
2540 (cmp (convert?@3 (bit_xor @0 INTEGER_CST@1)) INTEGER_CST@2)
2541 (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0)))
2542 (cmp @0 (bit_xor @1 (convert @2)))))
2545 (cmp (convert? addr@0) integer_zerop)
2546 (if (tree_single_nonzero_warnv_p (@0, NULL))
2547 { constant_boolean_node (cmp == NE_EXPR, type); })))
2549 /* If we have (A & C) == C where C is a power of 2, convert this into
2550 (A & C) != 0. Similarly for NE_EXPR. */
2554 (cmp (bit_and@2 @0 integer_pow2p@1) @1)
2555 (icmp @2 { build_zero_cst (TREE_TYPE (@0)); })))
2557 /* If we have (A & C) != 0 where C is the sign bit of A, convert
2558 this into A < 0. Similarly for (A & C) == 0 into A >= 0. */
2562 (cmp (bit_and (convert?@2 @0) integer_pow2p@1) integer_zerop)
2563 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2564 && (TYPE_PRECISION (TREE_TYPE (@0))
2565 == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (@0))))
2566 && element_precision (@2) >= element_precision (@0)
2567 && wi::only_sign_bit_p (@1, element_precision (@0)))
2568 (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
2569 (ncmp (convert:stype @0) { build_zero_cst (stype); })))))
2571 /* When the addresses are not directly of decls compare base and offset.
2572 This implements some remaining parts of fold_comparison address
2573 comparisons but still no complete part of it. Still it is good
2574 enough to make fold_stmt not regress when not dispatching to fold_binary. */
2575 (for cmp (simple_comparison)
2577 (cmp (convert1?@2 addr@0) (convert2? addr@1))
2580 HOST_WIDE_INT off0, off1;
2581 tree base0 = get_addr_base_and_unit_offset (TREE_OPERAND (@0, 0), &off0);
2582 tree base1 = get_addr_base_and_unit_offset (TREE_OPERAND (@1, 0), &off1);
2583 if (base0 && TREE_CODE (base0) == MEM_REF)
2585 off0 += mem_ref_offset (base0).to_short_addr ();
2586 base0 = TREE_OPERAND (base0, 0);
2588 if (base1 && TREE_CODE (base1) == MEM_REF)
2590 off1 += mem_ref_offset (base1).to_short_addr ();
2591 base1 = TREE_OPERAND (base1, 0);
2594 (if (base0 && base1)
2598 /* Punt in GENERIC on variables with value expressions;
2599 the value expressions might point to fields/elements
2600 of other vars etc. */
2602 && ((VAR_P (base0) && DECL_HAS_VALUE_EXPR_P (base0))
2603 || (VAR_P (base1) && DECL_HAS_VALUE_EXPR_P (base1))))
2605 else if (decl_in_symtab_p (base0)
2606 && decl_in_symtab_p (base1))
2607 equal = symtab_node::get_create (base0)
2608 ->equal_address_to (symtab_node::get_create (base1));
2609 else if ((DECL_P (base0)
2610 || TREE_CODE (base0) == SSA_NAME
2611 || TREE_CODE (base0) == STRING_CST)
2613 || TREE_CODE (base1) == SSA_NAME
2614 || TREE_CODE (base1) == STRING_CST))
2615 equal = (base0 == base1);
2618 && (cmp == EQ_EXPR || cmp == NE_EXPR
2619 /* If the offsets are equal we can ignore overflow. */
2621 || POINTER_TYPE_OVERFLOW_UNDEFINED
2622 /* Or if we compare using pointers to decls or strings. */
2623 || (POINTER_TYPE_P (TREE_TYPE (@2))
2624 && (DECL_P (base0) || TREE_CODE (base0) == STRING_CST))))
2626 (if (cmp == EQ_EXPR)
2627 { constant_boolean_node (off0 == off1, type); })
2628 (if (cmp == NE_EXPR)
2629 { constant_boolean_node (off0 != off1, type); })
2630 (if (cmp == LT_EXPR)
2631 { constant_boolean_node (off0 < off1, type); })
2632 (if (cmp == LE_EXPR)
2633 { constant_boolean_node (off0 <= off1, type); })
2634 (if (cmp == GE_EXPR)
2635 { constant_boolean_node (off0 >= off1, type); })
2636 (if (cmp == GT_EXPR)
2637 { constant_boolean_node (off0 > off1, type); }))
2639 && DECL_P (base0) && DECL_P (base1)
2640 /* If we compare this as integers require equal offset. */
2641 && (!INTEGRAL_TYPE_P (TREE_TYPE (@2))
2644 (if (cmp == EQ_EXPR)
2645 { constant_boolean_node (false, type); })
2646 (if (cmp == NE_EXPR)
2647 { constant_boolean_node (true, type); })))))))))
2649 /* Simplify pointer equality compares using PTA. */
2653 (if (POINTER_TYPE_P (TREE_TYPE (@0))
2654 && ptrs_compare_unequal (@0, @1))
2655 { neeq == EQ_EXPR ? boolean_false_node : boolean_true_node; })))
2657 /* PR70920: Transform (intptr_t)x eq/ne CST to x eq/ne (typeof x) CST.
2658 and (typeof ptr_cst) x eq/ne ptr_cst to x eq/ne (typeof x) CST.
2659 Disable the transform if either operand is pointer to function.
2660 This broke pr22051-2.c for arm where function pointer
2661 canonicalizaion is not wanted. */
2665 (cmp (convert @0) INTEGER_CST@1)
2666 (if ((POINTER_TYPE_P (TREE_TYPE (@0)) && !FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@0)))
2667 && INTEGRAL_TYPE_P (TREE_TYPE (@1)))
2668 || (INTEGRAL_TYPE_P (TREE_TYPE (@0)) && POINTER_TYPE_P (TREE_TYPE (@1))
2669 && !FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@1)))))
2670 (cmp @0 (convert @1)))))
2672 /* Non-equality compare simplifications from fold_binary */
2673 (for cmp (lt gt le ge)
2674 /* Comparisons with the highest or lowest possible integer of
2675 the specified precision will have known values. */
2677 (cmp (convert?@2 @0) INTEGER_CST@1)
2678 (if ((INTEGRAL_TYPE_P (TREE_TYPE (@1)) || POINTER_TYPE_P (TREE_TYPE (@1)))
2679 && tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@0)))
2682 tree arg1_type = TREE_TYPE (@1);
2683 unsigned int prec = TYPE_PRECISION (arg1_type);
2684 wide_int max = wi::max_value (arg1_type);
2685 wide_int signed_max = wi::max_value (prec, SIGNED);
2686 wide_int min = wi::min_value (arg1_type);
2689 (if (wi::eq_p (@1, max))
2691 (if (cmp == GT_EXPR)
2692 { constant_boolean_node (false, type); })
2693 (if (cmp == GE_EXPR)
2695 (if (cmp == LE_EXPR)
2696 { constant_boolean_node (true, type); })
2697 (if (cmp == LT_EXPR)
2699 (if (wi::eq_p (@1, min))
2701 (if (cmp == LT_EXPR)
2702 { constant_boolean_node (false, type); })
2703 (if (cmp == LE_EXPR)
2705 (if (cmp == GE_EXPR)
2706 { constant_boolean_node (true, type); })
2707 (if (cmp == GT_EXPR)
2709 (if (wi::eq_p (@1, max - 1))
2711 (if (cmp == GT_EXPR)
2712 (eq @2 { wide_int_to_tree (TREE_TYPE (@1), wi::add (@1, 1)); }))
2713 (if (cmp == LE_EXPR)
2714 (ne @2 { wide_int_to_tree (TREE_TYPE (@1), wi::add (@1, 1)); }))))
2715 (if (wi::eq_p (@1, min + 1))
2717 (if (cmp == GE_EXPR)
2718 (ne @2 { wide_int_to_tree (TREE_TYPE (@1), wi::sub (@1, 1)); }))
2719 (if (cmp == LT_EXPR)
2720 (eq @2 { wide_int_to_tree (TREE_TYPE (@1), wi::sub (@1, 1)); }))))
2721 (if (wi::eq_p (@1, signed_max)
2722 && TYPE_UNSIGNED (arg1_type)
2723 /* We will flip the signedness of the comparison operator
2724 associated with the mode of @1, so the sign bit is
2725 specified by this mode. Check that @1 is the signed
2726 max associated with this sign bit. */
2727 && prec == GET_MODE_PRECISION (TYPE_MODE (arg1_type))
2728 /* signed_type does not work on pointer types. */
2729 && INTEGRAL_TYPE_P (arg1_type))
2730 /* The following case also applies to X < signed_max+1
2731 and X >= signed_max+1 because previous transformations. */
2732 (if (cmp == LE_EXPR || cmp == GT_EXPR)
2733 (with { tree st = signed_type_for (arg1_type); }
2734 (if (cmp == LE_EXPR)
2735 (ge (convert:st @0) { build_zero_cst (st); })
2736 (lt (convert:st @0) { build_zero_cst (st); }))))))))))
2738 (for cmp (unordered ordered unlt unle ungt unge uneq ltgt)
2739 /* If the second operand is NaN, the result is constant. */
2742 (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
2743 && (cmp != LTGT_EXPR || ! flag_trapping_math))
2744 { constant_boolean_node (cmp == ORDERED_EXPR || cmp == LTGT_EXPR
2745 ? false : true, type); })))
2747 /* bool_var != 0 becomes bool_var. */
2749 (ne @0 integer_zerop)
2750 (if (TREE_CODE (TREE_TYPE (@0)) == BOOLEAN_TYPE
2751 && types_match (type, TREE_TYPE (@0)))
2753 /* bool_var == 1 becomes bool_var. */
2755 (eq @0 integer_onep)
2756 (if (TREE_CODE (TREE_TYPE (@0)) == BOOLEAN_TYPE
2757 && types_match (type, TREE_TYPE (@0)))
2760 bool_var == 0 becomes !bool_var or
2761 bool_var != 1 becomes !bool_var
2762 here because that only is good in assignment context as long
2763 as we require a tcc_comparison in GIMPLE_CONDs where we'd
2764 replace if (x == 0) with tem = ~x; if (tem != 0) which is
2765 clearly less optimal and which we'll transform again in forwprop. */
2767 /* When one argument is a constant, overflow detection can be simplified.
2768 Currently restricted to single use so as not to interfere too much with
2769 ADD_OVERFLOW detection in tree-ssa-math-opts.c.
2770 A + CST CMP A -> A CMP' CST' */
2771 (for cmp (lt le ge gt)
2774 (cmp:c (plus@2 @0 INTEGER_CST@1) @0)
2775 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
2776 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))
2779 (out @0 { wide_int_to_tree (TREE_TYPE (@0), wi::max_value
2780 (TYPE_PRECISION (TREE_TYPE (@0)), UNSIGNED) - @1); }))))
2782 /* To detect overflow in unsigned A - B, A < B is simpler than A - B > A.
2783 However, the detection logic for SUB_OVERFLOW in tree-ssa-math-opts.c
2784 expects the long form, so we restrict the transformation for now. */
2787 (cmp:c (minus@2 @0 @1) @0)
2788 (if (single_use (@2)
2789 && ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2790 && TYPE_UNSIGNED (TREE_TYPE (@0))
2791 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
2794 /* Testing for overflow is unnecessary if we already know the result. */
2799 (cmp:c (realpart (IFN_SUB_OVERFLOW@2 @0 @1)) @0)
2800 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
2801 && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
2802 (out (imagpart @2) { build_zero_cst (TREE_TYPE (@0)); }))))
2807 (cmp:c (realpart (IFN_ADD_OVERFLOW:c@2 @0 @1)) @0)
2808 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
2809 && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
2810 (out (imagpart @2) { build_zero_cst (TREE_TYPE (@0)); }))))
2812 /* For unsigned operands, -1 / B < A checks whether A * B would overflow.
2813 Simplify it to __builtin_mul_overflow (A, B, <unused>). */
2817 (cmp:c (trunc_div:s integer_all_onesp @1) @0)
2818 (if (TYPE_UNSIGNED (TREE_TYPE (@0)) && !VECTOR_TYPE_P (TREE_TYPE (@0)))
2819 (with { tree t = TREE_TYPE (@0), cpx = build_complex_type (t); }
2820 (out (imagpart (IFN_MUL_OVERFLOW:cpx @0 @1)) { build_zero_cst (t); })))))
2822 /* Simplification of math builtins. These rules must all be optimizations
2823 as well as IL simplifications. If there is a possibility that the new
2824 form could be a pessimization, the rule should go in the canonicalization
2825 section that follows this one.
2827 Rules can generally go in this section if they satisfy one of
2830 - the rule describes an identity
2832 - the rule replaces calls with something as simple as addition or
2835 - the rule contains unary calls only and simplifies the surrounding
2836 arithmetic. (The idea here is to exclude non-unary calls in which
2837 one operand is constant and in which the call is known to be cheap
2838 when the operand has that value.) */
2840 (if (flag_unsafe_math_optimizations)
2841 /* Simplify sqrt(x) * sqrt(x) -> x. */
2843 (mult (SQRT@1 @0) @1)
2844 (if (!HONOR_SNANS (type))
2847 /* Simplify sqrt(x) * sqrt(y) -> sqrt(x*y). */
2848 (for root (SQRT CBRT)
2850 (mult (root:s @0) (root:s @1))
2851 (root (mult @0 @1))))
2853 /* Simplify expN(x) * expN(y) -> expN(x+y). */
2854 (for exps (EXP EXP2 EXP10 POW10)
2856 (mult (exps:s @0) (exps:s @1))
2857 (exps (plus @0 @1))))
2859 /* Simplify a/root(b/c) into a*root(c/b). */
2860 (for root (SQRT CBRT)
2862 (rdiv @0 (root:s (rdiv:s @1 @2)))
2863 (mult @0 (root (rdiv @2 @1)))))
2865 /* Simplify x/expN(y) into x*expN(-y). */
2866 (for exps (EXP EXP2 EXP10 POW10)
2868 (rdiv @0 (exps:s @1))
2869 (mult @0 (exps (negate @1)))))
2871 (for logs (LOG LOG2 LOG10 LOG10)
2872 exps (EXP EXP2 EXP10 POW10)
2873 /* logN(expN(x)) -> x. */
2877 /* expN(logN(x)) -> x. */
2882 /* Optimize logN(func()) for various exponential functions. We
2883 want to determine the value "x" and the power "exponent" in
2884 order to transform logN(x**exponent) into exponent*logN(x). */
2885 (for logs (LOG LOG LOG LOG2 LOG2 LOG2 LOG10 LOG10)
2886 exps (EXP2 EXP10 POW10 EXP EXP10 POW10 EXP EXP2)
2889 (if (SCALAR_FLOAT_TYPE_P (type))
2895 /* Prepare to do logN(exp(exponent)) -> exponent*logN(e). */
2896 x = build_real_truncate (type, dconst_e ());
2899 /* Prepare to do logN(exp2(exponent)) -> exponent*logN(2). */
2900 x = build_real (type, dconst2);
2904 /* Prepare to do logN(exp10(exponent)) -> exponent*logN(10). */
2906 REAL_VALUE_TYPE dconst10;
2907 real_from_integer (&dconst10, VOIDmode, 10, SIGNED);
2908 x = build_real (type, dconst10);
2915 (mult (logs { x; }) @0)))))
2923 (if (SCALAR_FLOAT_TYPE_P (type))
2929 /* Prepare to do logN(sqrt(x)) -> 0.5*logN(x). */
2930 x = build_real (type, dconsthalf);
2933 /* Prepare to do logN(cbrt(x)) -> (1/3)*logN(x). */
2934 x = build_real_truncate (type, dconst_third ());
2940 (mult { x; } (logs @0))))))
2942 /* logN(pow(x,exponent)) -> exponent*logN(x). */
2943 (for logs (LOG LOG2 LOG10)
2947 (mult @1 (logs @0))))
2952 exps (EXP EXP2 EXP10 POW10)
2953 /* sqrt(expN(x)) -> expN(x*0.5). */
2956 (exps (mult @0 { build_real (type, dconsthalf); })))
2957 /* cbrt(expN(x)) -> expN(x/3). */
2960 (exps (mult @0 { build_real_truncate (type, dconst_third ()); })))
2961 /* pow(expN(x), y) -> expN(x*y). */
2964 (exps (mult @0 @1))))
2966 /* tan(atan(x)) -> x. */
2973 /* cabs(x+0i) or cabs(0+xi) -> abs(x). */
2975 (CABS (complex:C @0 real_zerop@1))
2978 /* trunc(trunc(x)) -> trunc(x), etc. */
2979 (for fns (TRUNC FLOOR CEIL ROUND NEARBYINT RINT)
2983 /* f(x) -> x if x is integer valued and f does nothing for such values. */
2984 (for fns (TRUNC FLOOR CEIL ROUND NEARBYINT RINT)
2986 (fns integer_valued_real_p@0)
2989 /* hypot(x,0) and hypot(0,x) -> abs(x). */
2991 (HYPOT:c @0 real_zerop@1)
2994 /* pow(1,x) -> 1. */
2996 (POW real_onep@0 @1)
3000 /* copysign(x,x) -> x. */
3005 /* copysign(x,y) -> fabs(x) if y is nonnegative. */
3006 (COPYSIGN @0 tree_expr_nonnegative_p@1)
3009 (for scale (LDEXP SCALBN SCALBLN)
3010 /* ldexp(0, x) -> 0. */
3012 (scale real_zerop@0 @1)
3014 /* ldexp(x, 0) -> x. */
3016 (scale @0 integer_zerop@1)
3018 /* ldexp(x, y) -> x if x is +-Inf or NaN. */
3020 (scale REAL_CST@0 @1)
3021 (if (!real_isfinite (TREE_REAL_CST_PTR (@0)))
3024 /* Canonicalization of sequences of math builtins. These rules represent
3025 IL simplifications but are not necessarily optimizations.
3027 The sincos pass is responsible for picking "optimal" implementations
3028 of math builtins, which may be more complicated and can sometimes go
3029 the other way, e.g. converting pow into a sequence of sqrts.
3030 We only want to do these canonicalizations before the pass has run. */
3032 (if (flag_unsafe_math_optimizations && canonicalize_math_p ())
3033 /* Simplify tan(x) * cos(x) -> sin(x). */
3035 (mult:c (TAN:s @0) (COS:s @0))
3038 /* Simplify x * pow(x,c) -> pow(x,c+1). */
3040 (mult:c @0 (POW:s @0 REAL_CST@1))
3041 (if (!TREE_OVERFLOW (@1))
3042 (POW @0 (plus @1 { build_one_cst (type); }))))
3044 /* Simplify sin(x) / cos(x) -> tan(x). */
3046 (rdiv (SIN:s @0) (COS:s @0))
3049 /* Simplify cos(x) / sin(x) -> 1 / tan(x). */
3051 (rdiv (COS:s @0) (SIN:s @0))
3052 (rdiv { build_one_cst (type); } (TAN @0)))
3054 /* Simplify sin(x) / tan(x) -> cos(x). */
3056 (rdiv (SIN:s @0) (TAN:s @0))
3057 (if (! HONOR_NANS (@0)
3058 && ! HONOR_INFINITIES (@0))
3061 /* Simplify tan(x) / sin(x) -> 1.0 / cos(x). */
3063 (rdiv (TAN:s @0) (SIN:s @0))
3064 (if (! HONOR_NANS (@0)
3065 && ! HONOR_INFINITIES (@0))
3066 (rdiv { build_one_cst (type); } (COS @0))))
3068 /* Simplify pow(x,y) * pow(x,z) -> pow(x,y+z). */
3070 (mult (POW:s @0 @1) (POW:s @0 @2))
3071 (POW @0 (plus @1 @2)))
3073 /* Simplify pow(x,y) * pow(z,y) -> pow(x*z,y). */
3075 (mult (POW:s @0 @1) (POW:s @2 @1))
3076 (POW (mult @0 @2) @1))
3078 /* Simplify powi(x,y) * powi(z,y) -> powi(x*z,y). */
3080 (mult (POWI:s @0 @1) (POWI:s @2 @1))
3081 (POWI (mult @0 @2) @1))
3083 /* Simplify pow(x,c) / x -> pow(x,c-1). */
3085 (rdiv (POW:s @0 REAL_CST@1) @0)
3086 (if (!TREE_OVERFLOW (@1))
3087 (POW @0 (minus @1 { build_one_cst (type); }))))
3089 /* Simplify x / pow (y,z) -> x * pow(y,-z). */
3091 (rdiv @0 (POW:s @1 @2))
3092 (mult @0 (POW @1 (negate @2))))
3097 /* sqrt(sqrt(x)) -> pow(x,1/4). */
3100 (pows @0 { build_real (type, dconst_quarter ()); }))
3101 /* sqrt(cbrt(x)) -> pow(x,1/6). */
3104 (pows @0 { build_real_truncate (type, dconst_sixth ()); }))
3105 /* cbrt(sqrt(x)) -> pow(x,1/6). */
3108 (pows @0 { build_real_truncate (type, dconst_sixth ()); }))
3109 /* cbrt(cbrt(x)) -> pow(x,1/9), iff x is nonnegative. */
3111 (cbrts (cbrts tree_expr_nonnegative_p@0))
3112 (pows @0 { build_real_truncate (type, dconst_ninth ()); }))
3113 /* sqrt(pow(x,y)) -> pow(|x|,y*0.5). */
3115 (sqrts (pows @0 @1))
3116 (pows (abs @0) (mult @1 { build_real (type, dconsthalf); })))
3117 /* cbrt(pow(x,y)) -> pow(x,y/3), iff x is nonnegative. */
3119 (cbrts (pows tree_expr_nonnegative_p@0 @1))
3120 (pows @0 (mult @1 { build_real_truncate (type, dconst_third ()); })))
3121 /* pow(sqrt(x),y) -> pow(x,y*0.5). */
3123 (pows (sqrts @0) @1)
3124 (pows @0 (mult @1 { build_real (type, dconsthalf); })))
3125 /* pow(cbrt(x),y) -> pow(x,y/3) iff x is nonnegative. */
3127 (pows (cbrts tree_expr_nonnegative_p@0) @1)
3128 (pows @0 (mult @1 { build_real_truncate (type, dconst_third ()); })))
3129 /* pow(pow(x,y),z) -> pow(x,y*z) iff x is nonnegative. */
3131 (pows (pows tree_expr_nonnegative_p@0 @1) @2)
3132 (pows @0 (mult @1 @2))))
3134 /* cabs(x+xi) -> fabs(x)*sqrt(2). */
3136 (CABS (complex @0 @0))
3137 (mult (abs @0) { build_real_truncate (type, dconst_sqrt2 ()); }))
3139 /* hypot(x,x) -> fabs(x)*sqrt(2). */
3142 (mult (abs @0) { build_real_truncate (type, dconst_sqrt2 ()); }))
3144 /* cexp(x+yi) -> exp(x)*cexpi(y). */
3149 (cexps compositional_complex@0)
3150 (if (targetm.libc_has_function (function_c99_math_complex))
3152 (mult (exps@1 (realpart @0)) (realpart (cexpis:type@2 (imagpart @0))))
3153 (mult @1 (imagpart @2)))))))
3155 (if (canonicalize_math_p ())
3156 /* floor(x) -> trunc(x) if x is nonnegative. */
3160 (floors tree_expr_nonnegative_p@0)
3163 (match double_value_p
3165 (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == double_type_node)))
3166 (for froms (BUILT_IN_TRUNCL
3178 /* truncl(extend(x)) -> extend(trunc(x)), etc., if x is a double. */
3179 (if (optimize && canonicalize_math_p ())
3181 (froms (convert double_value_p@0))
3182 (convert (tos @0)))))
3184 (match float_value_p
3186 (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == float_type_node)))
3187 (for froms (BUILT_IN_TRUNCL BUILT_IN_TRUNC
3188 BUILT_IN_FLOORL BUILT_IN_FLOOR
3189 BUILT_IN_CEILL BUILT_IN_CEIL
3190 BUILT_IN_ROUNDL BUILT_IN_ROUND
3191 BUILT_IN_NEARBYINTL BUILT_IN_NEARBYINT
3192 BUILT_IN_RINTL BUILT_IN_RINT)
3193 tos (BUILT_IN_TRUNCF BUILT_IN_TRUNCF
3194 BUILT_IN_FLOORF BUILT_IN_FLOORF
3195 BUILT_IN_CEILF BUILT_IN_CEILF
3196 BUILT_IN_ROUNDF BUILT_IN_ROUNDF
3197 BUILT_IN_NEARBYINTF BUILT_IN_NEARBYINTF
3198 BUILT_IN_RINTF BUILT_IN_RINTF)
3199 /* truncl(extend(x)) and trunc(extend(x)) -> extend(truncf(x)), etc.,
3201 (if (optimize && canonicalize_math_p ()
3202 && targetm.libc_has_function (function_c99_misc))
3204 (froms (convert float_value_p@0))
3205 (convert (tos @0)))))
3207 (for froms (XFLOORL XCEILL XROUNDL XRINTL)
3208 tos (XFLOOR XCEIL XROUND XRINT)
3209 /* llfloorl(extend(x)) -> llfloor(x), etc., if x is a double. */
3210 (if (optimize && canonicalize_math_p ())
3212 (froms (convert double_value_p@0))
3215 (for froms (XFLOORL XCEILL XROUNDL XRINTL
3216 XFLOOR XCEIL XROUND XRINT)
3217 tos (XFLOORF XCEILF XROUNDF XRINTF)
3218 /* llfloorl(extend(x)) and llfloor(extend(x)) -> llfloorf(x), etc.,
3220 (if (optimize && canonicalize_math_p ())
3222 (froms (convert float_value_p@0))
3225 (if (canonicalize_math_p ())
3226 /* xfloor(x) -> fix_trunc(x) if x is nonnegative. */
3227 (for floors (IFLOOR LFLOOR LLFLOOR)
3229 (floors tree_expr_nonnegative_p@0)
3232 (if (canonicalize_math_p ())
3233 /* xfloor(x) -> fix_trunc(x), etc., if x is integer valued. */
3234 (for fns (IFLOOR LFLOOR LLFLOOR
3236 IROUND LROUND LLROUND)
3238 (fns integer_valued_real_p@0)
3240 (if (!flag_errno_math)
3241 /* xrint(x) -> fix_trunc(x), etc., if x is integer valued. */
3242 (for rints (IRINT LRINT LLRINT)
3244 (rints integer_valued_real_p@0)
3247 (if (canonicalize_math_p ())
3248 (for ifn (IFLOOR ICEIL IROUND IRINT)
3249 lfn (LFLOOR LCEIL LROUND LRINT)
3250 llfn (LLFLOOR LLCEIL LLROUND LLRINT)
3251 /* Canonicalize iround (x) to lround (x) on ILP32 targets where
3252 sizeof (int) == sizeof (long). */
3253 (if (TYPE_PRECISION (integer_type_node)
3254 == TYPE_PRECISION (long_integer_type_node))
3257 (lfn:long_integer_type_node @0)))
3258 /* Canonicalize llround (x) to lround (x) on LP64 targets where
3259 sizeof (long long) == sizeof (long). */
3260 (if (TYPE_PRECISION (long_long_integer_type_node)
3261 == TYPE_PRECISION (long_integer_type_node))
3264 (lfn:long_integer_type_node @0)))))
3266 /* cproj(x) -> x if we're ignoring infinities. */
3269 (if (!HONOR_INFINITIES (type))
3272 /* If the real part is inf and the imag part is known to be
3273 nonnegative, return (inf + 0i). */
3275 (CPROJ (complex REAL_CST@0 tree_expr_nonnegative_p@1))
3276 (if (real_isinf (TREE_REAL_CST_PTR (@0)))
3277 { build_complex_inf (type, false); }))
3279 /* If the imag part is inf, return (inf+I*copysign(0,imag)). */
3281 (CPROJ (complex @0 REAL_CST@1))
3282 (if (real_isinf (TREE_REAL_CST_PTR (@1)))
3283 { build_complex_inf (type, TREE_REAL_CST_PTR (@1)->sign); }))
3289 (pows @0 REAL_CST@1)
3291 const REAL_VALUE_TYPE *value = TREE_REAL_CST_PTR (@1);
3292 REAL_VALUE_TYPE tmp;
3295 /* pow(x,0) -> 1. */
3296 (if (real_equal (value, &dconst0))
3297 { build_real (type, dconst1); })
3298 /* pow(x,1) -> x. */
3299 (if (real_equal (value, &dconst1))
3301 /* pow(x,-1) -> 1/x. */
3302 (if (real_equal (value, &dconstm1))
3303 (rdiv { build_real (type, dconst1); } @0))
3304 /* pow(x,0.5) -> sqrt(x). */
3305 (if (flag_unsafe_math_optimizations
3306 && canonicalize_math_p ()
3307 && real_equal (value, &dconsthalf))
3309 /* pow(x,1/3) -> cbrt(x). */
3310 (if (flag_unsafe_math_optimizations
3311 && canonicalize_math_p ()
3312 && (tmp = real_value_truncate (TYPE_MODE (type), dconst_third ()),
3313 real_equal (value, &tmp)))
3316 /* powi(1,x) -> 1. */
3318 (POWI real_onep@0 @1)
3322 (POWI @0 INTEGER_CST@1)
3324 /* powi(x,0) -> 1. */
3325 (if (wi::eq_p (@1, 0))
3326 { build_real (type, dconst1); })
3327 /* powi(x,1) -> x. */
3328 (if (wi::eq_p (@1, 1))
3330 /* powi(x,-1) -> 1/x. */
3331 (if (wi::eq_p (@1, -1))
3332 (rdiv { build_real (type, dconst1); } @0))))
3334 /* Narrowing of arithmetic and logical operations.
3336 These are conceptually similar to the transformations performed for
3337 the C/C++ front-ends by shorten_binary_op and shorten_compare. Long
3338 term we want to move all that code out of the front-ends into here. */
3340 /* If we have a narrowing conversion of an arithmetic operation where
3341 both operands are widening conversions from the same type as the outer
3342 narrowing conversion. Then convert the innermost operands to a suitable
3343 unsigned type (to avoid introducing undefined behavior), perform the
3344 operation and convert the result to the desired type. */
3345 (for op (plus minus)
3347 (convert (op:s (convert@2 @0) (convert?@3 @1)))
3348 (if (INTEGRAL_TYPE_P (type)
3349 /* We check for type compatibility between @0 and @1 below,
3350 so there's no need to check that @1/@3 are integral types. */
3351 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
3352 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
3353 /* The precision of the type of each operand must match the
3354 precision of the mode of each operand, similarly for the
3356 && (TYPE_PRECISION (TREE_TYPE (@0))
3357 == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (@0))))
3358 && (TYPE_PRECISION (TREE_TYPE (@1))
3359 == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (@1))))
3360 && TYPE_PRECISION (type) == GET_MODE_PRECISION (TYPE_MODE (type))
3361 /* The inner conversion must be a widening conversion. */
3362 && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
3363 && types_match (@0, type)
3364 && (types_match (@0, @1)
3365 /* Or the second operand is const integer or converted const
3366 integer from valueize. */
3367 || TREE_CODE (@1) == INTEGER_CST))
3368 (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
3369 (op @0 (convert @1))
3370 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
3371 (convert (op (convert:utype @0)
3372 (convert:utype @1))))))))
3374 /* This is another case of narrowing, specifically when there's an outer
3375 BIT_AND_EXPR which masks off bits outside the type of the innermost
3376 operands. Like the previous case we have to convert the operands
3377 to unsigned types to avoid introducing undefined behavior for the
3378 arithmetic operation. */
3379 (for op (minus plus)
3381 (bit_and (op:s (convert@2 @0) (convert@3 @1)) INTEGER_CST@4)
3382 (if (INTEGRAL_TYPE_P (type)
3383 /* We check for type compatibility between @0 and @1 below,
3384 so there's no need to check that @1/@3 are integral types. */
3385 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
3386 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
3387 /* The precision of the type of each operand must match the
3388 precision of the mode of each operand, similarly for the
3390 && (TYPE_PRECISION (TREE_TYPE (@0))
3391 == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (@0))))
3392 && (TYPE_PRECISION (TREE_TYPE (@1))
3393 == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (@1))))
3394 && TYPE_PRECISION (type) == GET_MODE_PRECISION (TYPE_MODE (type))
3395 /* The inner conversion must be a widening conversion. */
3396 && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
3397 && types_match (@0, @1)
3398 && (tree_int_cst_min_precision (@4, TYPE_SIGN (TREE_TYPE (@0)))
3399 <= TYPE_PRECISION (TREE_TYPE (@0)))
3400 && (wi::bit_and (@4, wi::mask (TYPE_PRECISION (TREE_TYPE (@0)),
3401 true, TYPE_PRECISION (type))) == 0))
3402 (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
3403 (with { tree ntype = TREE_TYPE (@0); }
3404 (convert (bit_and (op @0 @1) (convert:ntype @4))))
3405 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
3406 (convert (bit_and (op (convert:utype @0) (convert:utype @1))
3407 (convert:utype @4))))))))
3409 /* Transform (@0 < @1 and @0 < @2) to use min,
3410 (@0 > @1 and @0 > @2) to use max */
3411 (for op (lt le gt ge)
3412 ext (min min max max)
3414 (bit_and (op:cs @0 @1) (op:cs @0 @2))
3415 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
3416 && TREE_CODE (@0) != INTEGER_CST)
3417 (op @0 (ext @1 @2)))))
3420 /* signbit(x) -> 0 if x is nonnegative. */
3421 (SIGNBIT tree_expr_nonnegative_p@0)
3422 { integer_zero_node; })
3425 /* signbit(x) -> x<0 if x doesn't have signed zeros. */
3427 (if (!HONOR_SIGNED_ZEROS (@0))
3428 (convert (lt @0 { build_real (TREE_TYPE (@0), dconst0); }))))
3430 /* Transform comparisons of the form X +- C1 CMP C2 to X CMP C2 -+ C1. */
3432 (for op (plus minus)
3435 (cmp (op@3 @0 INTEGER_CST@1) INTEGER_CST@2)
3436 (if (!TREE_OVERFLOW (@1) && !TREE_OVERFLOW (@2)
3437 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0))
3438 && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@0))
3439 && !TYPE_SATURATING (TREE_TYPE (@0)))
3440 (with { tree res = int_const_binop (rop, @2, @1); }
3441 (if (TREE_OVERFLOW (res))
3442 { constant_boolean_node (cmp == NE_EXPR, type); }
3443 (if (single_use (@3))
3444 (cmp @0 { res; }))))))))
3445 (for cmp (lt le gt ge)
3446 (for op (plus minus)
3449 (cmp (op@3 @0 INTEGER_CST@1) INTEGER_CST@2)
3450 (if (!TREE_OVERFLOW (@1) && !TREE_OVERFLOW (@2)
3451 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
3452 (with { tree res = int_const_binop (rop, @2, @1); }
3453 (if (TREE_OVERFLOW (res))
3455 fold_overflow_warning (("assuming signed overflow does not occur "
3456 "when simplifying conditional to constant"),
3457 WARN_STRICT_OVERFLOW_CONDITIONAL);
3458 bool less = cmp == LE_EXPR || cmp == LT_EXPR;
3459 /* wi::ges_p (@2, 0) should be sufficient for a signed type. */
3460 bool ovf_high = wi::lt_p (@1, 0, TYPE_SIGN (TREE_TYPE (@1)))
3461 != (op == MINUS_EXPR);
3462 constant_boolean_node (less == ovf_high, type);
3464 (if (single_use (@3))
3467 fold_overflow_warning (("assuming signed overflow does not occur "
3468 "when changing X +- C1 cmp C2 to "
3470 WARN_STRICT_OVERFLOW_COMPARISON);
3472 (cmp @0 { res; })))))))))
3474 /* Canonicalizations of BIT_FIELD_REFs. */
3477 (BIT_FIELD_REF @0 @1 @2)
3479 (if (TREE_CODE (TREE_TYPE (@0)) == COMPLEX_TYPE
3480 && tree_int_cst_equal (@1, TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))))
3482 (if (integer_zerop (@2))
3483 (view_convert (realpart @0)))
3484 (if (tree_int_cst_equal (@2, TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))))
3485 (view_convert (imagpart @0)))))
3486 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
3487 && INTEGRAL_TYPE_P (type)
3488 /* On GIMPLE this should only apply to register arguments. */
3489 && (! GIMPLE || is_gimple_reg (@0))
3490 /* A bit-field-ref that referenced the full argument can be stripped. */
3491 && ((compare_tree_int (@1, TYPE_PRECISION (TREE_TYPE (@0))) == 0
3492 && integer_zerop (@2))
3493 /* Low-parts can be reduced to integral conversions.
3494 ??? The following doesn't work for PDP endian. */
3495 || (BYTES_BIG_ENDIAN == WORDS_BIG_ENDIAN
3496 /* Don't even think about BITS_BIG_ENDIAN. */
3497 && TYPE_PRECISION (TREE_TYPE (@0)) % BITS_PER_UNIT == 0
3498 && TYPE_PRECISION (type) % BITS_PER_UNIT == 0
3499 && compare_tree_int (@2, (BYTES_BIG_ENDIAN
3500 ? (TYPE_PRECISION (TREE_TYPE (@0))
3501 - TYPE_PRECISION (type))
3505 /* Simplify vector extracts. */
3508 (BIT_FIELD_REF CONSTRUCTOR@0 @1 @2)
3509 (if (VECTOR_TYPE_P (TREE_TYPE (@0))
3510 && (types_match (type, TREE_TYPE (TREE_TYPE (@0)))
3511 || (VECTOR_TYPE_P (type)
3512 && types_match (TREE_TYPE (type), TREE_TYPE (TREE_TYPE (@0))))))
3515 tree ctor = (TREE_CODE (@0) == SSA_NAME
3516 ? gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0)) : @0);
3517 tree eltype = TREE_TYPE (TREE_TYPE (ctor));
3518 unsigned HOST_WIDE_INT width = tree_to_uhwi (TYPE_SIZE (eltype));
3519 unsigned HOST_WIDE_INT n = tree_to_uhwi (@1);
3520 unsigned HOST_WIDE_INT idx = tree_to_uhwi (@2);
3523 && (idx % width) == 0
3525 && ((idx + n) / width) <= TYPE_VECTOR_SUBPARTS (TREE_TYPE (ctor)))
3530 /* Constructor elements can be subvectors. */
3531 unsigned HOST_WIDE_INT k = 1;
3532 if (CONSTRUCTOR_NELTS (ctor) != 0)
3534 tree cons_elem = TREE_TYPE (CONSTRUCTOR_ELT (ctor, 0)->value);
3535 if (TREE_CODE (cons_elem) == VECTOR_TYPE)
3536 k = TYPE_VECTOR_SUBPARTS (cons_elem);
3540 /* We keep an exact subset of the constructor elements. */
3541 (if ((idx % k) == 0 && (n % k) == 0)
3542 (if (CONSTRUCTOR_NELTS (ctor) == 0)
3543 { build_constructor (type, NULL); }
3550 (if (idx < CONSTRUCTOR_NELTS (ctor))
3551 { CONSTRUCTOR_ELT (ctor, idx)->value; }
3552 { build_zero_cst (type); })
3554 vec<constructor_elt, va_gc> *vals;
3555 vec_alloc (vals, n);
3556 for (unsigned i = 0;
3557 i < n && idx + i < CONSTRUCTOR_NELTS (ctor); ++i)
3558 CONSTRUCTOR_APPEND_ELT (vals, NULL_TREE,
3559 CONSTRUCTOR_ELT (ctor, idx + i)->value);
3560 build_constructor (type, vals);
3562 /* The bitfield references a single constructor element. */
3563 (if (idx + n <= (idx / k + 1) * k)
3565 (if (CONSTRUCTOR_NELTS (ctor) <= idx / k)
3566 { build_zero_cst (type); })
3568 { CONSTRUCTOR_ELT (ctor, idx / k)->value; })
3569 (BIT_FIELD_REF { CONSTRUCTOR_ELT (ctor, idx / k)->value; }
3570 @1 { bitsize_int ((idx % k) * width); })))))))))