1 /* Match-and-simplify patterns for shared GENERIC and GIMPLE folding.
2 This file is consumed by genmatch which produces gimple-match.c
3 and generic-match.c from it.
5 Copyright (C) 2014-2017 Free Software Foundation, Inc.
6 Contributed by Richard Biener <rguenther@suse.de>
7 and Prathamesh Kulkarni <bilbotheelffriend@gmail.com>
9 This file is part of GCC.
11 GCC is free software; you can redistribute it and/or modify it under
12 the terms of the GNU General Public License as published by the Free
13 Software Foundation; either version 3, or (at your option) any later
16 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
17 WARRANTY; without even the implied warranty of MERCHANTABILITY or
18 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21 You should have received a copy of the GNU General Public License
22 along with GCC; see the file COPYING3. If not see
23 <http://www.gnu.org/licenses/>. */
26 /* Generic tree predicates we inherit. */
28 integer_onep integer_zerop integer_all_onesp integer_minus_onep
29 integer_each_onep integer_truep integer_nonzerop
30 real_zerop real_onep real_minus_onep
33 tree_expr_nonnegative_p
40 (define_operator_list tcc_comparison
41 lt le eq ne ge gt unordered ordered unlt unle ungt unge uneq ltgt)
42 (define_operator_list inverted_tcc_comparison
43 ge gt ne eq lt le ordered unordered ge gt le lt ltgt uneq)
44 (define_operator_list inverted_tcc_comparison_with_nans
45 unge ungt ne eq unlt unle ordered unordered ge gt le lt ltgt uneq)
46 (define_operator_list swapped_tcc_comparison
47 gt ge eq ne le lt unordered ordered ungt unge unlt unle uneq ltgt)
48 (define_operator_list simple_comparison lt le eq ne ge gt)
49 (define_operator_list swapped_simple_comparison gt ge eq ne le lt)
51 #include "cfn-operators.pd"
53 /* Define operand lists for math rounding functions {,i,l,ll}FN,
54 where the versions prefixed with "i" return an int, those prefixed with
55 "l" return a long and those prefixed with "ll" return a long long.
57 Also define operand lists:
59 X<FN>F for all float functions, in the order i, l, ll
60 X<FN> for all double functions, in the same order
61 X<FN>L for all long double functions, in the same order. */
62 #define DEFINE_INT_AND_FLOAT_ROUND_FN(FN) \
63 (define_operator_list X##FN##F BUILT_IN_I##FN##F \
66 (define_operator_list X##FN BUILT_IN_I##FN \
69 (define_operator_list X##FN##L BUILT_IN_I##FN##L \
73 DEFINE_INT_AND_FLOAT_ROUND_FN (FLOOR)
74 DEFINE_INT_AND_FLOAT_ROUND_FN (CEIL)
75 DEFINE_INT_AND_FLOAT_ROUND_FN (ROUND)
76 DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
78 /* As opposed to convert?, this still creates a single pattern, so
79 it is not a suitable replacement for convert? in all cases. */
80 (match (nop_convert @0)
82 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))))
83 (match (nop_convert @0)
85 (if (VECTOR_TYPE_P (type) && VECTOR_TYPE_P (TREE_TYPE (@0))
86 && TYPE_VECTOR_SUBPARTS (type) == TYPE_VECTOR_SUBPARTS (TREE_TYPE (@0))
87 && tree_nop_conversion_p (TREE_TYPE (type), TREE_TYPE (TREE_TYPE (@0))))))
88 /* This one has to be last, or it shadows the others. */
89 (match (nop_convert @0)
92 /* Simplifications of operations with one constant operand and
93 simplifications to constants or single values. */
95 (for op (plus pointer_plus minus bit_ior bit_xor)
100 /* 0 +p index -> (type)index */
102 (pointer_plus integer_zerop @1)
103 (non_lvalue (convert @1)))
105 /* See if ARG1 is zero and X + ARG1 reduces to X.
106 Likewise if the operands are reversed. */
108 (plus:c @0 real_zerop@1)
109 (if (fold_real_zero_addition_p (type, @1, 0))
112 /* See if ARG1 is zero and X - ARG1 reduces to X. */
114 (minus @0 real_zerop@1)
115 (if (fold_real_zero_addition_p (type, @1, 1))
119 This is unsafe for certain floats even in non-IEEE formats.
120 In IEEE, it is unsafe because it does wrong for NaNs.
121 Also note that operand_equal_p is always false if an operand
125 (if (!FLOAT_TYPE_P (type) || !HONOR_NANS (type))
126 { build_zero_cst (type); }))
129 (mult @0 integer_zerop@1)
132 /* Maybe fold x * 0 to 0. The expressions aren't the same
133 when x is NaN, since x * 0 is also NaN. Nor are they the
134 same in modes with signed zeros, since multiplying a
135 negative value by 0 gives -0, not +0. */
137 (mult @0 real_zerop@1)
138 (if (!HONOR_NANS (type) && !HONOR_SIGNED_ZEROS (type))
141 /* In IEEE floating point, x*1 is not equivalent to x for snans.
142 Likewise for complex arithmetic with signed zeros. */
145 (if (!HONOR_SNANS (type)
146 && (!HONOR_SIGNED_ZEROS (type)
147 || !COMPLEX_FLOAT_TYPE_P (type)))
150 /* Transform x * -1.0 into -x. */
152 (mult @0 real_minus_onep)
153 (if (!HONOR_SNANS (type)
154 && (!HONOR_SIGNED_ZEROS (type)
155 || !COMPLEX_FLOAT_TYPE_P (type)))
158 /* X * 1, X / 1 -> X. */
159 (for op (mult trunc_div ceil_div floor_div round_div exact_div)
164 /* (A / (1 << B)) -> (A >> B).
165 Only for unsigned A. For signed A, this would not preserve rounding
167 For example: (-1 / ( 1 << B)) != -1 >> B. */
169 (trunc_div @0 (lshift integer_onep@1 @2))
170 (if ((TYPE_UNSIGNED (type) || tree_expr_nonnegative_p (@0))
171 && (!VECTOR_TYPE_P (type)
172 || target_supports_op_p (type, RSHIFT_EXPR, optab_vector)
173 || target_supports_op_p (type, RSHIFT_EXPR, optab_scalar)))
176 /* Preserve explicit divisions by 0: the C++ front-end wants to detect
177 undefined behavior in constexpr evaluation, and assuming that the division
178 traps enables better optimizations than these anyway. */
179 (for div (trunc_div ceil_div floor_div round_div exact_div)
180 /* 0 / X is always zero. */
182 (div integer_zerop@0 @1)
183 /* But not for 0 / 0 so that we can get the proper warnings and errors. */
184 (if (!integer_zerop (@1))
188 (div @0 integer_minus_onep@1)
189 (if (!TYPE_UNSIGNED (type))
194 /* But not for 0 / 0 so that we can get the proper warnings and errors. */
195 (if (!integer_zerop (@0))
196 { build_one_cst (type); }))
197 /* X / abs (X) is X < 0 ? -1 : 1. */
200 (if (INTEGRAL_TYPE_P (type)
201 && TYPE_OVERFLOW_UNDEFINED (type))
202 (cond (lt @0 { build_zero_cst (type); })
203 { build_minus_one_cst (type); } { build_one_cst (type); })))
206 (div:C @0 (negate @0))
207 (if ((INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
208 && TYPE_OVERFLOW_UNDEFINED (type))
209 { build_minus_one_cst (type); })))
211 /* For unsigned integral types, FLOOR_DIV_EXPR is the same as
212 TRUNC_DIV_EXPR. Rewrite into the latter in this case. */
215 (if ((INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
216 && TYPE_UNSIGNED (type))
219 /* Combine two successive divisions. Note that combining ceil_div
220 and floor_div is trickier and combining round_div even more so. */
221 (for div (trunc_div exact_div)
223 (div (div @0 INTEGER_CST@1) INTEGER_CST@2)
226 wide_int mul = wi::mul (@1, @2, TYPE_SIGN (type), &overflow_p);
229 (div @0 { wide_int_to_tree (type, mul); })
230 (if (TYPE_UNSIGNED (type)
231 || mul != wi::min_value (TYPE_PRECISION (type), SIGNED))
232 { build_zero_cst (type); })))))
234 /* Optimize A / A to 1.0 if we don't care about
235 NaNs or Infinities. */
238 (if (FLOAT_TYPE_P (type)
239 && ! HONOR_NANS (type)
240 && ! HONOR_INFINITIES (type))
241 { build_one_cst (type); }))
243 /* Optimize -A / A to -1.0 if we don't care about
244 NaNs or Infinities. */
246 (rdiv:C @0 (negate @0))
247 (if (FLOAT_TYPE_P (type)
248 && ! HONOR_NANS (type)
249 && ! HONOR_INFINITIES (type))
250 { build_minus_one_cst (type); }))
252 /* PR71078: x / abs(x) -> copysign (1.0, x) */
254 (rdiv:C (convert? @0) (convert? (abs @0)))
255 (if (SCALAR_FLOAT_TYPE_P (type)
256 && ! HONOR_NANS (type)
257 && ! HONOR_INFINITIES (type))
259 (if (types_match (type, float_type_node))
260 (BUILT_IN_COPYSIGNF { build_one_cst (type); } (convert @0)))
261 (if (types_match (type, double_type_node))
262 (BUILT_IN_COPYSIGN { build_one_cst (type); } (convert @0)))
263 (if (types_match (type, long_double_type_node))
264 (BUILT_IN_COPYSIGNL { build_one_cst (type); } (convert @0))))))
266 /* In IEEE floating point, x/1 is not equivalent to x for snans. */
269 (if (!HONOR_SNANS (type))
272 /* In IEEE floating point, x/-1 is not equivalent to -x for snans. */
274 (rdiv @0 real_minus_onep)
275 (if (!HONOR_SNANS (type))
278 (if (flag_reciprocal_math)
279 /* Convert (A/B)/C to A/(B*C) */
281 (rdiv (rdiv:s @0 @1) @2)
282 (rdiv @0 (mult @1 @2)))
284 /* Convert A/(B/C) to (A/B)*C */
286 (rdiv @0 (rdiv:s @1 @2))
287 (mult (rdiv @0 @1) @2)))
289 /* Optimize (X & (-A)) / A where A is a power of 2, to X >> log2(A) */
290 (for div (trunc_div ceil_div floor_div round_div exact_div)
292 (div (convert? (bit_and @0 INTEGER_CST@1)) INTEGER_CST@2)
293 (if (integer_pow2p (@2)
294 && tree_int_cst_sgn (@2) > 0
295 && wi::add (@2, @1) == 0
296 && tree_nop_conversion_p (type, TREE_TYPE (@0)))
297 (rshift (convert @0) { build_int_cst (integer_type_node,
298 wi::exact_log2 (@2)); }))))
300 /* If ARG1 is a constant, we can convert this to a multiply by the
301 reciprocal. This does not have the same rounding properties,
302 so only do this if -freciprocal-math. We can actually
303 always safely do it if ARG1 is a power of two, but it's hard to
304 tell if it is or not in a portable manner. */
305 (for cst (REAL_CST COMPLEX_CST VECTOR_CST)
309 (if (flag_reciprocal_math
312 { tree tem = const_binop (RDIV_EXPR, type, build_one_cst (type), @1); }
314 (mult @0 { tem; } )))
315 (if (cst != COMPLEX_CST)
316 (with { tree inverse = exact_inverse (type, @1); }
318 (mult @0 { inverse; } ))))))))
320 (for mod (ceil_mod floor_mod round_mod trunc_mod)
321 /* 0 % X is always zero. */
323 (mod integer_zerop@0 @1)
324 /* But not for 0 % 0 so that we can get the proper warnings and errors. */
325 (if (!integer_zerop (@1))
327 /* X % 1 is always zero. */
329 (mod @0 integer_onep)
330 { build_zero_cst (type); })
331 /* X % -1 is zero. */
333 (mod @0 integer_minus_onep@1)
334 (if (!TYPE_UNSIGNED (type))
335 { build_zero_cst (type); }))
339 /* But not for 0 % 0 so that we can get the proper warnings and errors. */
340 (if (!integer_zerop (@0))
341 { build_zero_cst (type); }))
342 /* (X % Y) % Y is just X % Y. */
344 (mod (mod@2 @0 @1) @1)
346 /* From extract_muldiv_1: (X * C1) % C2 is zero if C1 is a multiple of C2. */
348 (mod (mult @0 INTEGER_CST@1) INTEGER_CST@2)
349 (if (ANY_INTEGRAL_TYPE_P (type)
350 && TYPE_OVERFLOW_UNDEFINED (type)
351 && wi::multiple_of_p (@1, @2, TYPE_SIGN (type)))
352 { build_zero_cst (type); })))
354 /* X % -C is the same as X % C. */
356 (trunc_mod @0 INTEGER_CST@1)
357 (if (TYPE_SIGN (type) == SIGNED
358 && !TREE_OVERFLOW (@1)
360 && !TYPE_OVERFLOW_TRAPS (type)
361 /* Avoid this transformation if C is INT_MIN, i.e. C == -C. */
362 && !sign_bit_p (@1, @1))
363 (trunc_mod @0 (negate @1))))
365 /* X % -Y is the same as X % Y. */
367 (trunc_mod @0 (convert? (negate @1)))
368 (if (INTEGRAL_TYPE_P (type)
369 && !TYPE_UNSIGNED (type)
370 && !TYPE_OVERFLOW_TRAPS (type)
371 && tree_nop_conversion_p (type, TREE_TYPE (@1))
372 /* Avoid this transformation if X might be INT_MIN or
373 Y might be -1, because we would then change valid
374 INT_MIN % -(-1) into invalid INT_MIN % -1. */
375 && (expr_not_equal_to (@0, TYPE_MIN_VALUE (type))
376 || expr_not_equal_to (@1, wi::minus_one (TYPE_PRECISION
378 (trunc_mod @0 (convert @1))))
380 /* X - (X / Y) * Y is the same as X % Y. */
382 (minus (convert1? @0) (convert2? (mult:c (trunc_div @@0 @@1) @1)))
383 (if (INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
384 (convert (trunc_mod @0 @1))))
386 /* Optimize TRUNC_MOD_EXPR by a power of two into a BIT_AND_EXPR,
387 i.e. "X % C" into "X & (C - 1)", if X and C are positive.
388 Also optimize A % (C << N) where C is a power of 2,
389 to A & ((C << N) - 1). */
390 (match (power_of_two_cand @1)
392 (match (power_of_two_cand @1)
393 (lshift INTEGER_CST@1 @2))
394 (for mod (trunc_mod floor_mod)
396 (mod @0 (convert?@3 (power_of_two_cand@1 @2)))
397 (if ((TYPE_UNSIGNED (type)
398 || tree_expr_nonnegative_p (@0))
399 && tree_nop_conversion_p (type, TREE_TYPE (@3))
400 && integer_pow2p (@2) && tree_int_cst_sgn (@2) > 0)
401 (bit_and @0 (convert (minus @1 { build_int_cst (TREE_TYPE (@1), 1); }))))))
403 /* Simplify (unsigned t * 2)/2 -> unsigned t & 0x7FFFFFFF. */
405 (trunc_div (mult @0 integer_pow2p@1) @1)
406 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
407 (bit_and @0 { wide_int_to_tree
408 (type, wi::mask (TYPE_PRECISION (type) - wi::exact_log2 (@1),
409 false, TYPE_PRECISION (type))); })))
411 /* Simplify (unsigned t / 2) * 2 -> unsigned t & ~1. */
413 (mult (trunc_div @0 integer_pow2p@1) @1)
414 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
415 (bit_and @0 (negate @1))))
417 /* Simplify (t * 2) / 2) -> t. */
418 (for div (trunc_div ceil_div floor_div round_div exact_div)
420 (div (mult @0 @1) @1)
421 (if (ANY_INTEGRAL_TYPE_P (type)
422 && TYPE_OVERFLOW_UNDEFINED (type))
426 /* Simplify cos(-x) and cos(|x|) -> cos(x). Similarly for cosh. */
431 /* Simplify pow(-x, y) and pow(|x|,y) -> pow(x,y) if y is an even integer. */
434 (pows (op @0) REAL_CST@1)
435 (with { HOST_WIDE_INT n; }
436 (if (real_isinteger (&TREE_REAL_CST (@1), &n) && (n & 1) == 0)
438 /* Likewise for powi. */
441 (pows (op @0) INTEGER_CST@1)
442 (if (wi::bit_and (@1, 1) == 0)
444 /* Strip negate and abs from both operands of hypot. */
452 /* copysign(-x, y) and copysign(abs(x), y) -> copysign(x, y). */
453 (for copysigns (COPYSIGN)
455 (copysigns (op @0) @1)
458 /* abs(x)*abs(x) -> x*x. Should be valid for all types. */
463 /* cos(copysign(x, y)) -> cos(x). Similarly for cosh. */
467 (coss (copysigns @0 @1))
470 /* pow(copysign(x, y), z) -> pow(x, z) if z is an even integer. */
474 (pows (copysigns @0 @2) REAL_CST@1)
475 (with { HOST_WIDE_INT n; }
476 (if (real_isinteger (&TREE_REAL_CST (@1), &n) && (n & 1) == 0)
478 /* Likewise for powi. */
482 (pows (copysigns @0 @2) INTEGER_CST@1)
483 (if (wi::bit_and (@1, 1) == 0)
488 /* hypot(copysign(x, y), z) -> hypot(x, z). */
490 (hypots (copysigns @0 @1) @2)
492 /* hypot(x, copysign(y, z)) -> hypot(x, y). */
494 (hypots @0 (copysigns @1 @2))
497 /* copysign(x, CST) -> [-]abs (x). */
498 (for copysigns (COPYSIGN)
500 (copysigns @0 REAL_CST@1)
501 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
505 /* copysign(copysign(x, y), z) -> copysign(x, z). */
506 (for copysigns (COPYSIGN)
508 (copysigns (copysigns @0 @1) @2)
511 /* copysign(x,y)*copysign(x,y) -> x*x. */
512 (for copysigns (COPYSIGN)
514 (mult (copysigns@2 @0 @1) @2)
517 /* ccos(-x) -> ccos(x). Similarly for ccosh. */
518 (for ccoss (CCOS CCOSH)
523 /* cabs(-x) and cos(conj(x)) -> cabs(x). */
524 (for ops (conj negate)
530 /* Fold (a * (1 << b)) into (a << b) */
532 (mult:c @0 (convert? (lshift integer_onep@1 @2)))
533 (if (! FLOAT_TYPE_P (type)
534 && tree_nop_conversion_p (type, TREE_TYPE (@1)))
537 /* Fold (C1/X)*C2 into (C1*C2)/X. */
539 (mult (rdiv@3 REAL_CST@0 @1) REAL_CST@2)
540 (if (flag_associative_math
543 { tree tem = const_binop (MULT_EXPR, type, @0, @2); }
545 (rdiv { tem; } @1)))))
547 /* Convert C1/(X*C2) into (C1/C2)/X */
549 (rdiv REAL_CST@0 (mult @1 REAL_CST@2))
550 (if (flag_reciprocal_math)
552 { tree tem = const_binop (RDIV_EXPR, type, @0, @2); }
554 (rdiv { tem; } @1)))))
556 /* Simplify ~X & X as zero. */
558 (bit_and:c (convert? @0) (convert? (bit_not @0)))
559 { build_zero_cst (type); })
561 /* PR71636: Transform x & ((1U << b) - 1) -> x & ~(~0U << b); */
563 (bit_and:c @0 (plus:s (lshift:s integer_onep @1) integer_minus_onep))
564 (if (TYPE_UNSIGNED (type))
565 (bit_and @0 (bit_not (lshift { build_all_ones_cst (type); } @1)))))
567 /* PR35691: Transform
568 (x == 0 & y == 0) -> (x | typeof(x)(y)) == 0.
569 (x != 0 | y != 0) -> (x | typeof(x)(y)) != 0. */
570 (for bitop (bit_and bit_ior)
573 (bitop (cmp @0 integer_zerop@2) (cmp @1 integer_zerop))
574 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
575 && INTEGRAL_TYPE_P (TREE_TYPE (@1))
576 && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
577 (cmp (bit_ior @0 (convert @1)) @2))))
579 /* Fold (A & ~B) - (A & B) into (A ^ B) - B. */
581 (minus (bit_and:cs @0 (bit_not @1)) (bit_and:cs @0 @1))
582 (minus (bit_xor @0 @1) @1))
584 (minus (bit_and:s @0 INTEGER_CST@2) (bit_and:s @0 INTEGER_CST@1))
585 (if (wi::bit_not (@2) == @1)
586 (minus (bit_xor @0 @1) @1)))
588 /* Fold (A & B) - (A & ~B) into B - (A ^ B). */
590 (minus (bit_and:cs @0 @1) (bit_and:cs @0 (bit_not @1)))
591 (minus @1 (bit_xor @0 @1)))
593 /* Simplify (X & ~Y) | (~X & Y) -> X ^ Y. */
595 (bit_ior (bit_and:c @0 (bit_not @1)) (bit_and:c (bit_not @0) @1))
598 (bit_ior:c (bit_and @0 INTEGER_CST@2) (bit_and (bit_not @0) INTEGER_CST@1))
599 (if (wi::bit_not (@2) == @1)
602 /* PR53979: Transform ((a ^ b) | a) -> (a | b) */
604 (bit_ior:c (bit_xor:c @0 @1) @0)
607 /* Simplify (~X & Y) to X ^ Y if we know that (X & ~Y) is 0. */
610 (bit_and (bit_not SSA_NAME@0) INTEGER_CST@1)
611 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
612 && (get_nonzero_bits (@0) & wi::bit_not (@1)) == 0)
616 /* X % Y is smaller than Y. */
619 (cmp (trunc_mod @0 @1) @1)
620 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
621 { constant_boolean_node (cmp == LT_EXPR, type); })))
624 (cmp @1 (trunc_mod @0 @1))
625 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
626 { constant_boolean_node (cmp == GT_EXPR, type); })))
630 (bit_ior @0 integer_all_onesp@1)
635 (bit_ior @0 integer_zerop)
640 (bit_and @0 integer_zerop@1)
646 (for op (bit_ior bit_xor plus)
648 (op:c (convert? @0) (convert? (bit_not @0)))
649 (convert { build_all_ones_cst (TREE_TYPE (@0)); })))
654 { build_zero_cst (type); })
656 /* Canonicalize X ^ ~0 to ~X. */
658 (bit_xor @0 integer_all_onesp@1)
663 (bit_and @0 integer_all_onesp)
666 /* x & x -> x, x | x -> x */
667 (for bitop (bit_and bit_ior)
672 /* x & C -> x if we know that x & ~C == 0. */
675 (bit_and SSA_NAME@0 INTEGER_CST@1)
676 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
677 && (get_nonzero_bits (@0) & wi::bit_not (@1)) == 0)
681 /* x + (x & 1) -> (x + 1) & ~1 */
683 (plus:c @0 (bit_and:s @0 integer_onep@1))
684 (bit_and (plus @0 @1) (bit_not @1)))
686 /* x & ~(x & y) -> x & ~y */
687 /* x | ~(x | y) -> x | ~y */
688 (for bitop (bit_and bit_ior)
690 (bitop:c @0 (bit_not (bitop:cs @0 @1)))
691 (bitop @0 (bit_not @1))))
693 /* (x | y) & ~x -> y & ~x */
694 /* (x & y) | ~x -> y | ~x */
695 (for bitop (bit_and bit_ior)
696 rbitop (bit_ior bit_and)
698 (bitop:c (rbitop:c @0 @1) (bit_not@2 @0))
701 /* (x & y) ^ (x | y) -> x ^ y */
703 (bit_xor:c (bit_and @0 @1) (bit_ior @0 @1))
706 /* (x ^ y) ^ (x | y) -> x & y */
708 (bit_xor:c (bit_xor @0 @1) (bit_ior @0 @1))
711 /* (x & y) + (x ^ y) -> x | y */
712 /* (x & y) | (x ^ y) -> x | y */
713 /* (x & y) ^ (x ^ y) -> x | y */
714 (for op (plus bit_ior bit_xor)
716 (op:c (bit_and @0 @1) (bit_xor @0 @1))
719 /* (x & y) + (x | y) -> x + y */
721 (plus:c (bit_and @0 @1) (bit_ior @0 @1))
724 /* (x + y) - (x | y) -> x & y */
726 (minus (plus @0 @1) (bit_ior @0 @1))
727 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
728 && !TYPE_SATURATING (type))
731 /* (x + y) - (x & y) -> x | y */
733 (minus (plus @0 @1) (bit_and @0 @1))
734 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
735 && !TYPE_SATURATING (type))
738 /* (x | y) - (x ^ y) -> x & y */
740 (minus (bit_ior @0 @1) (bit_xor @0 @1))
743 /* (x | y) - (x & y) -> x ^ y */
745 (minus (bit_ior @0 @1) (bit_and @0 @1))
748 /* (x | y) & ~(x & y) -> x ^ y */
750 (bit_and:c (bit_ior @0 @1) (bit_not (bit_and @0 @1)))
753 /* (x | y) & (~x ^ y) -> x & y */
755 (bit_and:c (bit_ior:c @0 @1) (bit_xor:c @1 (bit_not @0)))
758 /* ~x & ~y -> ~(x | y)
759 ~x | ~y -> ~(x & y) */
760 (for op (bit_and bit_ior)
761 rop (bit_ior bit_and)
763 (op (convert1? (bit_not @0)) (convert2? (bit_not @1)))
764 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
765 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
766 (bit_not (rop (convert @0) (convert @1))))))
768 /* If we are XORing or adding two BIT_AND_EXPR's, both of which are and'ing
769 with a constant, and the two constants have no bits in common,
770 we should treat this as a BIT_IOR_EXPR since this may produce more
772 (for op (bit_xor plus)
774 (op (convert1? (bit_and@4 @0 INTEGER_CST@1))
775 (convert2? (bit_and@5 @2 INTEGER_CST@3)))
776 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
777 && tree_nop_conversion_p (type, TREE_TYPE (@2))
778 && wi::bit_and (@1, @3) == 0)
779 (bit_ior (convert @4) (convert @5)))))
781 /* (X | Y) ^ X -> Y & ~ X*/
783 (bit_xor:c (convert1? (bit_ior:c @@0 @1)) (convert2? @0))
784 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
785 (convert (bit_and @1 (bit_not @0)))))
787 /* Convert ~X ^ ~Y to X ^ Y. */
789 (bit_xor (convert1? (bit_not @0)) (convert2? (bit_not @1)))
790 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
791 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
792 (bit_xor (convert @0) (convert @1))))
794 /* Convert ~X ^ C to X ^ ~C. */
796 (bit_xor (convert? (bit_not @0)) INTEGER_CST@1)
797 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
798 (bit_xor (convert @0) (bit_not @1))))
800 /* Fold (X & Y) ^ Y and (X ^ Y) & Y as ~X & Y. */
801 (for opo (bit_and bit_xor)
802 opi (bit_xor bit_and)
804 (opo:c (opi:c @0 @1) @1)
805 (bit_and (bit_not @0) @1)))
807 /* Given a bit-wise operation CODE applied to ARG0 and ARG1, see if both
808 operands are another bit-wise operation with a common input. If so,
809 distribute the bit operations to save an operation and possibly two if
810 constants are involved. For example, convert
811 (A | B) & (A | C) into A | (B & C)
812 Further simplification will occur if B and C are constants. */
813 (for op (bit_and bit_ior bit_xor)
814 rop (bit_ior bit_and bit_and)
816 (op (convert? (rop:c @@0 @1)) (convert? (rop:c @0 @2)))
817 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
818 && tree_nop_conversion_p (type, TREE_TYPE (@2)))
819 (rop (convert @0) (op (convert @1) (convert @2))))))
821 /* Some simple reassociation for bit operations, also handled in reassoc. */
822 /* (X & Y) & Y -> X & Y
823 (X | Y) | Y -> X | Y */
824 (for op (bit_and bit_ior)
826 (op:c (convert1?@2 (op:c @0 @@1)) (convert2? @1))
828 /* (X ^ Y) ^ Y -> X */
830 (bit_xor:c (convert1? (bit_xor:c @0 @@1)) (convert2? @1))
832 /* (X & Y) & (X & Z) -> (X & Y) & Z
833 (X | Y) | (X | Z) -> (X | Y) | Z */
834 (for op (bit_and bit_ior)
836 (op:c (convert1?@3 (op:c@4 @0 @1)) (convert2?@5 (op:c@6 @0 @2)))
837 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
838 && tree_nop_conversion_p (type, TREE_TYPE (@2)))
839 (if (single_use (@5) && single_use (@6))
841 (if (single_use (@3) && single_use (@4))
842 (op (convert @1) @5))))))
843 /* (X ^ Y) ^ (X ^ Z) -> Y ^ Z */
845 (bit_xor (convert1? (bit_xor:c @0 @1)) (convert2? (bit_xor:c @0 @2)))
846 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
847 && tree_nop_conversion_p (type, TREE_TYPE (@2)))
848 (bit_xor (convert @1) (convert @2))))
857 (abs tree_expr_nonnegative_p@0)
860 /* A few cases of fold-const.c negate_expr_p predicate. */
863 (if ((INTEGRAL_TYPE_P (type)
864 && TYPE_OVERFLOW_WRAPS (type))
865 || (!TYPE_OVERFLOW_SANITIZED (type)
866 && may_negate_without_overflow_p (t)))))
871 (if (!TYPE_OVERFLOW_SANITIZED (type))))
874 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (t)))))
875 /* VECTOR_CST handling of non-wrapping types would recurse in unsupported
879 (if (FLOAT_TYPE_P (TREE_TYPE (type)) || TYPE_OVERFLOW_WRAPS (type))))
881 /* (-A) * (-B) -> A * B */
883 (mult:c (convert1? (negate @0)) (convert2? negate_expr_p@1))
884 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
885 && tree_nop_conversion_p (type, TREE_TYPE (@1)))
886 (mult (convert @0) (convert (negate @1)))))
888 /* -(A + B) -> (-B) - A. */
890 (negate (plus:c @0 negate_expr_p@1))
891 (if (!HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type))
892 && !HONOR_SIGNED_ZEROS (element_mode (type)))
893 (minus (negate @1) @0)))
895 /* A - B -> A + (-B) if B is easily negatable. */
897 (minus @0 negate_expr_p@1)
898 (if (!FIXED_POINT_TYPE_P (type))
899 (plus @0 (negate @1))))
901 /* Try to fold (type) X op CST -> (type) (X op ((type-x) CST))
903 For bitwise binary operations apply operand conversions to the
904 binary operation result instead of to the operands. This allows
905 to combine successive conversions and bitwise binary operations.
906 We combine the above two cases by using a conditional convert. */
907 (for bitop (bit_and bit_ior bit_xor)
909 (bitop (convert @0) (convert? @1))
910 (if (((TREE_CODE (@1) == INTEGER_CST
911 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
912 && int_fits_type_p (@1, TREE_TYPE (@0)))
913 || types_match (@0, @1))
914 /* ??? This transform conflicts with fold-const.c doing
915 Convert (T)(x & c) into (T)x & (T)c, if c is an integer
916 constants (if x has signed type, the sign bit cannot be set
917 in c). This folds extension into the BIT_AND_EXPR.
918 Restrict it to GIMPLE to avoid endless recursions. */
919 && (bitop != BIT_AND_EXPR || GIMPLE)
920 && (/* That's a good idea if the conversion widens the operand, thus
921 after hoisting the conversion the operation will be narrower. */
922 TYPE_PRECISION (TREE_TYPE (@0)) < TYPE_PRECISION (type)
923 /* It's also a good idea if the conversion is to a non-integer
925 || GET_MODE_CLASS (TYPE_MODE (type)) != MODE_INT
926 /* Or if the precision of TO is not the same as the precision
928 || TYPE_PRECISION (type) != GET_MODE_PRECISION (TYPE_MODE (type))))
929 (convert (bitop @0 (convert @1))))))
931 (for bitop (bit_and bit_ior)
932 rbitop (bit_ior bit_and)
933 /* (x | y) & x -> x */
934 /* (x & y) | x -> x */
936 (bitop:c (rbitop:c @0 @1) @0)
938 /* (~x | y) & x -> x & y */
939 /* (~x & y) | x -> x | y */
941 (bitop:c (rbitop:c (bit_not @0) @1) @0)
944 /* (x | CST1) & CST2 -> (x & CST2) | (CST1 & CST2) */
946 (bit_and (bit_ior @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
947 (bit_ior (bit_and @0 @2) (bit_and @1 @2)))
949 /* Combine successive equal operations with constants. */
950 (for bitop (bit_and bit_ior bit_xor)
952 (bitop (bitop @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
953 (bitop @0 (bitop @1 @2))))
955 /* Try simple folding for X op !X, and X op X with the help
956 of the truth_valued_p and logical_inverted_value predicates. */
957 (match truth_valued_p
959 (if (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1)))
960 (for op (tcc_comparison truth_and truth_andif truth_or truth_orif truth_xor)
961 (match truth_valued_p
963 (match truth_valued_p
966 (match (logical_inverted_value @0)
968 (match (logical_inverted_value @0)
969 (bit_not truth_valued_p@0))
970 (match (logical_inverted_value @0)
971 (eq @0 integer_zerop))
972 (match (logical_inverted_value @0)
973 (ne truth_valued_p@0 integer_truep))
974 (match (logical_inverted_value @0)
975 (bit_xor truth_valued_p@0 integer_truep))
979 (bit_and:c @0 (logical_inverted_value @0))
980 { build_zero_cst (type); })
981 /* X | !X and X ^ !X -> 1, , if X is truth-valued. */
982 (for op (bit_ior bit_xor)
984 (op:c truth_valued_p@0 (logical_inverted_value @0))
985 { constant_boolean_node (true, type); }))
986 /* X ==/!= !X is false/true. */
989 (op:c truth_valued_p@0 (logical_inverted_value @0))
990 { constant_boolean_node (op == NE_EXPR ? true : false, type); }))
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))))
1038 /* For integral types with undefined overflow and C != 0 fold
1039 x * C EQ/NE y * C into x EQ/NE y. */
1042 (cmp (mult:c @0 @1) (mult:c @2 @1))
1043 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
1044 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1045 && tree_expr_nonzero_p (@1))
1048 /* For integral types with undefined overflow and C != 0 fold
1049 x * C RELOP y * C into:
1051 x RELOP y for nonnegative C
1052 y RELOP x for negative C */
1053 (for cmp (lt gt le ge)
1055 (cmp (mult:c @0 @1) (mult:c @2 @1))
1056 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
1057 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1058 (if (tree_expr_nonnegative_p (@1) && tree_expr_nonzero_p (@1))
1060 (if (TREE_CODE (@1) == INTEGER_CST
1061 && wi::neg_p (@1, TYPE_SIGN (TREE_TYPE (@1))))
1064 /* X / 4 < Y / 4 iff X < Y when the division is known to be exact. */
1065 (for cmp (simple_comparison)
1067 (cmp (exact_div @0 INTEGER_CST@2) (exact_div @1 @2))
1068 (if (wi::gt_p(@2, 0, TYPE_SIGN (TREE_TYPE (@2))))
1071 /* X + Z < Y + Z is the same as X < Y when there is no overflow. */
1072 (for op (lt le ge gt)
1074 (op (plus:c @0 @2) (plus:c @1 @2))
1075 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1076 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1078 /* For equality and subtraction, this is also true with wrapping overflow. */
1079 (for op (eq ne minus)
1081 (op (plus:c @0 @2) (plus:c @1 @2))
1082 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1083 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1084 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
1087 /* X - Z < Y - Z is the same as X < Y when there is no overflow. */
1088 (for op (lt le ge gt)
1090 (op (minus @0 @2) (minus @1 @2))
1091 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1092 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1094 /* For equality and subtraction, this is also true with wrapping overflow. */
1095 (for op (eq ne minus)
1097 (op (minus @0 @2) (minus @1 @2))
1098 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1099 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1100 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
1103 /* Z - X < Z - Y is the same as Y < X when there is no overflow. */
1104 (for op (lt le ge gt)
1106 (op (minus @2 @0) (minus @2 @1))
1107 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1108 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1110 /* For equality and subtraction, this is also true with wrapping overflow. */
1111 (for op (eq ne minus)
1113 (op (minus @2 @0) (minus @2 @1))
1114 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1115 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1116 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
1119 /* X == C - X can never be true if C is odd. */
1122 (cmp:c (convert? @0) (convert1? (minus INTEGER_CST@1 (convert2? @0))))
1123 (if (TREE_INT_CST_LOW (@1) & 1)
1124 { constant_boolean_node (cmp == NE_EXPR, type); })))
1126 /* Arguments on which one can call get_nonzero_bits to get the bits
1128 (match with_possible_nonzero_bits
1130 (match with_possible_nonzero_bits
1132 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))))
1133 /* Slightly extended version, do not make it recursive to keep it cheap. */
1134 (match (with_possible_nonzero_bits2 @0)
1135 with_possible_nonzero_bits@0)
1136 (match (with_possible_nonzero_bits2 @0)
1137 (bit_and:c with_possible_nonzero_bits@0 @2))
1139 /* Same for bits that are known to be set, but we do not have
1140 an equivalent to get_nonzero_bits yet. */
1141 (match (with_certain_nonzero_bits2 @0)
1143 (match (with_certain_nonzero_bits2 @0)
1144 (bit_ior @1 INTEGER_CST@0))
1146 /* X == C (or X & Z == Y | C) is impossible if ~nonzero(X) & C != 0. */
1149 (cmp:c (with_possible_nonzero_bits2 @0) (with_certain_nonzero_bits2 @1))
1150 (if ((~get_nonzero_bits (@0) & @1) != 0)
1151 { constant_boolean_node (cmp == NE_EXPR, type); })))
1153 /* ((X inner_op C0) outer_op C1)
1154 With X being a tree where value_range has reasoned certain bits to always be
1155 zero throughout its computed value range,
1156 inner_op = {|,^}, outer_op = {|,^} and inner_op != outer_op
1157 where zero_mask has 1's for all bits that are sure to be 0 in
1159 if (inner_op == '^') C0 &= ~C1;
1160 if ((C0 & ~zero_mask) == 0) then emit (X outer_op (C0 outer_op C1)
1161 if ((C1 & ~zero_mask) == 0) then emit (X inner_op (C0 outer_op C1)
1163 (for inner_op (bit_ior bit_xor)
1164 outer_op (bit_xor bit_ior)
1167 (inner_op:s @2 INTEGER_CST@0) INTEGER_CST@1)
1171 wide_int zero_mask_not;
1175 if (TREE_CODE (@2) == SSA_NAME)
1176 zero_mask_not = get_nonzero_bits (@2);
1180 if (inner_op == BIT_XOR_EXPR)
1182 C0 = wi::bit_and_not (@0, @1);
1183 cst_emit = wi::bit_or (C0, @1);
1188 cst_emit = wi::bit_xor (@0, @1);
1191 (if (!fail && wi::bit_and (C0, zero_mask_not) == 0)
1192 (outer_op @2 { wide_int_to_tree (type, cst_emit); })
1193 (if (!fail && wi::bit_and (@1, zero_mask_not) == 0)
1194 (inner_op @2 { wide_int_to_tree (type, cst_emit); }))))))
1196 /* Associate (p +p off1) +p off2 as (p +p (off1 + off2)). */
1198 (pointer_plus (pointer_plus:s @0 @1) @3)
1199 (pointer_plus @0 (plus @1 @3)))
1205 tem4 = (unsigned long) tem3;
1210 (pointer_plus @0 (convert?@2 (minus@3 (convert @1) (convert @0))))
1211 /* Conditionally look through a sign-changing conversion. */
1212 (if (TYPE_PRECISION (TREE_TYPE (@2)) == TYPE_PRECISION (TREE_TYPE (@3))
1213 && ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@1)))
1214 || (GENERIC && type == TREE_TYPE (@1))))
1218 tem = (sizetype) ptr;
1222 and produce the simpler and easier to analyze with respect to alignment
1223 ... = ptr & ~algn; */
1225 (pointer_plus @0 (negate (bit_and (convert @0) INTEGER_CST@1)))
1226 (with { tree algn = wide_int_to_tree (TREE_TYPE (@0), wi::bit_not (@1)); }
1227 (bit_and @0 { algn; })))
1229 /* Try folding difference of addresses. */
1231 (minus (convert ADDR_EXPR@0) (convert @1))
1232 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1233 (with { HOST_WIDE_INT diff; }
1234 (if (ptr_difference_const (@0, @1, &diff))
1235 { build_int_cst_type (type, diff); }))))
1237 (minus (convert @0) (convert ADDR_EXPR@1))
1238 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1239 (with { HOST_WIDE_INT diff; }
1240 (if (ptr_difference_const (@0, @1, &diff))
1241 { build_int_cst_type (type, diff); }))))
1243 /* If arg0 is derived from the address of an object or function, we may
1244 be able to fold this expression using the object or function's
1247 (bit_and (convert? @0) INTEGER_CST@1)
1248 (if (POINTER_TYPE_P (TREE_TYPE (@0))
1249 && tree_nop_conversion_p (type, TREE_TYPE (@0)))
1253 unsigned HOST_WIDE_INT bitpos;
1254 get_pointer_alignment_1 (@0, &align, &bitpos);
1256 (if (wi::ltu_p (@1, align / BITS_PER_UNIT))
1257 { wide_int_to_tree (type, wi::bit_and (@1, bitpos / BITS_PER_UNIT)); }))))
1260 /* We can't reassociate at all for saturating types. */
1261 (if (!TYPE_SATURATING (type))
1263 /* Contract negates. */
1264 /* A + (-B) -> A - B */
1266 (plus:c @0 (convert? (negate @1)))
1267 /* Apply STRIP_NOPS on the negate. */
1268 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1269 && !TYPE_OVERFLOW_SANITIZED (type))
1273 if (INTEGRAL_TYPE_P (type)
1274 && TYPE_OVERFLOW_WRAPS (type) != TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
1275 t1 = TYPE_OVERFLOW_WRAPS (type) ? type : TREE_TYPE (@1);
1277 (convert (minus (convert:t1 @0) (convert:t1 @1))))))
1278 /* A - (-B) -> A + B */
1280 (minus @0 (convert? (negate @1)))
1281 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1282 && !TYPE_OVERFLOW_SANITIZED (type))
1286 if (INTEGRAL_TYPE_P (type)
1287 && TYPE_OVERFLOW_WRAPS (type) != TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
1288 t1 = TYPE_OVERFLOW_WRAPS (type) ? type : TREE_TYPE (@1);
1290 (convert (plus (convert:t1 @0) (convert:t1 @1))))))
1293 (negate (convert? (negate @1)))
1294 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1295 && !TYPE_OVERFLOW_SANITIZED (type))
1298 /* We can't reassociate floating-point unless -fassociative-math
1299 or fixed-point plus or minus because of saturation to +-Inf. */
1300 (if ((!FLOAT_TYPE_P (type) || flag_associative_math)
1301 && !FIXED_POINT_TYPE_P (type))
1303 /* Match patterns that allow contracting a plus-minus pair
1304 irrespective of overflow issues. */
1305 /* (A +- B) - A -> +- B */
1306 /* (A +- B) -+ B -> A */
1307 /* A - (A +- B) -> -+ B */
1308 /* A +- (B -+ A) -> +- B */
1310 (minus (plus:c @0 @1) @0)
1313 (minus (minus @0 @1) @0)
1316 (plus:c (minus @0 @1) @1)
1319 (minus @0 (plus:c @0 @1))
1322 (minus @0 (minus @0 @1))
1325 /* (A +- CST1) +- CST2 -> A + CST3
1326 Use view_convert because it is safe for vectors and equivalent for
1328 (for outer_op (plus minus)
1329 (for inner_op (plus minus)
1330 neg_inner_op (minus plus)
1332 (outer_op (nop_convert (inner_op @0 CONSTANT_CLASS_P@1))
1334 /* If one of the types wraps, use that one. */
1335 (if (!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_WRAPS (type))
1336 (if (outer_op == PLUS_EXPR)
1337 (plus (view_convert @0) (inner_op @2 (view_convert @1)))
1338 (minus (view_convert @0) (neg_inner_op @2 (view_convert @1))))
1339 (if (!ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1340 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
1341 (if (outer_op == PLUS_EXPR)
1342 (view_convert (plus @0 (inner_op (view_convert @2) @1)))
1343 (view_convert (minus @0 (neg_inner_op (view_convert @2) @1))))
1344 /* If the constant operation overflows we cannot do the transform
1345 directly as we would introduce undefined overflow, for example
1346 with (a - 1) + INT_MIN. */
1347 (if (types_match (type, @0))
1348 (with { tree cst = const_binop (outer_op == inner_op
1349 ? PLUS_EXPR : MINUS_EXPR,
1351 (if (cst && !TREE_OVERFLOW (cst))
1352 (inner_op @0 { cst; } )
1353 /* X+INT_MAX+1 is X-INT_MIN. */
1354 (if (INTEGRAL_TYPE_P (type) && cst
1355 && wi::eq_p (cst, wi::min_value (type)))
1356 (neg_inner_op @0 { wide_int_to_tree (type, cst); })
1357 /* Last resort, use some unsigned type. */
1358 (with { tree utype = unsigned_type_for (type); }
1359 (view_convert (inner_op
1360 (view_convert:utype @0)
1362 { drop_tree_overflow (cst); })))))))))))))
1364 /* (CST1 - A) +- CST2 -> CST3 - A */
1365 (for outer_op (plus minus)
1367 (outer_op (minus CONSTANT_CLASS_P@1 @0) CONSTANT_CLASS_P@2)
1368 (with { tree cst = const_binop (outer_op, type, @1, @2); }
1369 (if (cst && !TREE_OVERFLOW (cst))
1370 (minus { cst; } @0)))))
1372 /* CST1 - (CST2 - A) -> CST3 + A */
1374 (minus CONSTANT_CLASS_P@1 (minus CONSTANT_CLASS_P@2 @0))
1375 (with { tree cst = const_binop (MINUS_EXPR, type, @1, @2); }
1376 (if (cst && !TREE_OVERFLOW (cst))
1377 (plus { cst; } @0))))
1381 (plus:c (bit_not @0) @0)
1382 (if (!TYPE_OVERFLOW_TRAPS (type))
1383 { build_all_ones_cst (type); }))
1387 (plus (convert? (bit_not @0)) integer_each_onep)
1388 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1389 (negate (convert @0))))
1393 (minus (convert? (negate @0)) integer_each_onep)
1394 (if (!TYPE_OVERFLOW_TRAPS (type)
1395 && tree_nop_conversion_p (type, TREE_TYPE (@0)))
1396 (bit_not (convert @0))))
1400 (minus integer_all_onesp @0)
1403 /* (T)(P + A) - (T)P -> (T) A */
1404 (for add (plus pointer_plus)
1406 (minus (convert (add @@0 @1))
1408 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
1409 /* For integer types, if A has a smaller type
1410 than T the result depends on the possible
1412 E.g. T=size_t, A=(unsigned)429497295, P>0.
1413 However, if an overflow in P + A would cause
1414 undefined behavior, we can assume that there
1416 || (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1417 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1418 /* For pointer types, if the conversion of A to the
1419 final type requires a sign- or zero-extension,
1420 then we have to punt - it is not defined which
1422 || (POINTER_TYPE_P (TREE_TYPE (@0))
1423 && TREE_CODE (@1) == INTEGER_CST
1424 && tree_int_cst_sign_bit (@1) == 0))
1427 /* (T)P - (T)(P + A) -> -(T) A */
1428 (for add (plus pointer_plus)
1431 (convert (add @@0 @1)))
1432 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
1433 /* For integer types, if A has a smaller type
1434 than T the result depends on the possible
1436 E.g. T=size_t, A=(unsigned)429497295, P>0.
1437 However, if an overflow in P + A would cause
1438 undefined behavior, we can assume that there
1440 || (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1441 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1442 /* For pointer types, if the conversion of A to the
1443 final type requires a sign- or zero-extension,
1444 then we have to punt - it is not defined which
1446 || (POINTER_TYPE_P (TREE_TYPE (@0))
1447 && TREE_CODE (@1) == INTEGER_CST
1448 && tree_int_cst_sign_bit (@1) == 0))
1449 (negate (convert @1)))))
1451 /* (T)(P + A) - (T)(P + B) -> (T)A - (T)B */
1452 (for add (plus pointer_plus)
1454 (minus (convert (add @@0 @1))
1455 (convert (add @0 @2)))
1456 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
1457 /* For integer types, if A has a smaller type
1458 than T the result depends on the possible
1460 E.g. T=size_t, A=(unsigned)429497295, P>0.
1461 However, if an overflow in P + A would cause
1462 undefined behavior, we can assume that there
1464 || (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1465 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1466 /* For pointer types, if the conversion of A to the
1467 final type requires a sign- or zero-extension,
1468 then we have to punt - it is not defined which
1470 || (POINTER_TYPE_P (TREE_TYPE (@0))
1471 && TREE_CODE (@1) == INTEGER_CST
1472 && tree_int_cst_sign_bit (@1) == 0
1473 && TREE_CODE (@2) == INTEGER_CST
1474 && tree_int_cst_sign_bit (@2) == 0))
1475 (minus (convert @1) (convert @2)))))))
1478 /* Simplifications of MIN_EXPR, MAX_EXPR, fmin() and fmax(). */
1480 (for minmax (min max FMIN FMAX)
1484 /* min(max(x,y),y) -> y. */
1486 (min:c (max:c @0 @1) @1)
1488 /* max(min(x,y),y) -> y. */
1490 (max:c (min:c @0 @1) @1)
1492 /* max(a,-a) -> abs(a). */
1494 (max:c @0 (negate @0))
1495 (if (TREE_CODE (type) != COMPLEX_TYPE
1496 && (! ANY_INTEGRAL_TYPE_P (type)
1497 || TYPE_OVERFLOW_UNDEFINED (type)))
1499 /* min(a,-a) -> -abs(a). */
1501 (min:c @0 (negate @0))
1502 (if (TREE_CODE (type) != COMPLEX_TYPE
1503 && (! ANY_INTEGRAL_TYPE_P (type)
1504 || TYPE_OVERFLOW_UNDEFINED (type)))
1509 (if (INTEGRAL_TYPE_P (type)
1510 && TYPE_MIN_VALUE (type)
1511 && operand_equal_p (@1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
1513 (if (INTEGRAL_TYPE_P (type)
1514 && TYPE_MAX_VALUE (type)
1515 && operand_equal_p (@1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
1520 (if (INTEGRAL_TYPE_P (type)
1521 && TYPE_MAX_VALUE (type)
1522 && operand_equal_p (@1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
1524 (if (INTEGRAL_TYPE_P (type)
1525 && TYPE_MIN_VALUE (type)
1526 && operand_equal_p (@1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
1529 /* max (a, a + CST) -> a + CST where CST is positive. */
1530 /* max (a, a + CST) -> a where CST is negative. */
1532 (max:c @0 (plus@2 @0 INTEGER_CST@1))
1533 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1534 (if (tree_int_cst_sgn (@1) > 0)
1538 /* min (a, a + CST) -> a where CST is positive. */
1539 /* min (a, a + CST) -> a + CST where CST is negative. */
1541 (min:c @0 (plus@2 @0 INTEGER_CST@1))
1542 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1543 (if (tree_int_cst_sgn (@1) > 0)
1547 /* (convert (minmax ((convert (x) c)))) -> minmax (x c) if x is promoted
1548 and the outer convert demotes the expression back to x's type. */
1549 (for minmax (min max)
1551 (convert (minmax@0 (convert @1) INTEGER_CST@2))
1552 (if (INTEGRAL_TYPE_P (type)
1553 && types_match (@1, type) && int_fits_type_p (@2, type)
1554 && TYPE_SIGN (TREE_TYPE (@0)) == TYPE_SIGN (type)
1555 && TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type))
1556 (minmax @1 (convert @2)))))
1558 (for minmax (FMIN FMAX)
1559 /* If either argument is NaN, return the other one. Avoid the
1560 transformation if we get (and honor) a signalling NaN. */
1562 (minmax:c @0 REAL_CST@1)
1563 (if (real_isnan (TREE_REAL_CST_PTR (@1))
1564 && (!HONOR_SNANS (@1) || !TREE_REAL_CST (@1).signalling))
1566 /* Convert fmin/fmax to MIN_EXPR/MAX_EXPR. C99 requires these
1567 functions to return the numeric arg if the other one is NaN.
1568 MIN and MAX don't honor that, so only transform if -ffinite-math-only
1569 is set. C99 doesn't require -0.0 to be handled, so we don't have to
1570 worry about it either. */
1571 (if (flag_finite_math_only)
1578 /* min (-A, -B) -> -max (A, B) */
1579 (for minmax (min max FMIN FMAX)
1580 maxmin (max min FMAX FMIN)
1582 (minmax (negate:s@2 @0) (negate:s@3 @1))
1583 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
1584 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1585 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
1586 (negate (maxmin @0 @1)))))
1587 /* MIN (~X, ~Y) -> ~MAX (X, Y)
1588 MAX (~X, ~Y) -> ~MIN (X, Y) */
1589 (for minmax (min max)
1592 (minmax (bit_not:s@2 @0) (bit_not:s@3 @1))
1593 (bit_not (maxmin @0 @1))))
1595 /* MIN (X, Y) == X -> X <= Y */
1596 (for minmax (min min max max)
1600 (cmp:c (minmax:c @0 @1) @0)
1601 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0)))
1603 /* MIN (X, 5) == 0 -> X == 0
1604 MIN (X, 5) == 7 -> false */
1607 (cmp (min @0 INTEGER_CST@1) INTEGER_CST@2)
1608 (if (wi::lt_p (@1, @2, TYPE_SIGN (TREE_TYPE (@0))))
1609 { constant_boolean_node (cmp == NE_EXPR, type); }
1610 (if (wi::gt_p (@1, @2, TYPE_SIGN (TREE_TYPE (@0))))
1614 (cmp (max @0 INTEGER_CST@1) INTEGER_CST@2)
1615 (if (wi::gt_p (@1, @2, TYPE_SIGN (TREE_TYPE (@0))))
1616 { constant_boolean_node (cmp == NE_EXPR, type); }
1617 (if (wi::lt_p (@1, @2, TYPE_SIGN (TREE_TYPE (@0))))
1619 /* MIN (X, C1) < C2 -> X < C2 || C1 < C2 */
1620 (for minmax (min min max max min min max max )
1621 cmp (lt le gt ge gt ge lt le )
1622 comb (bit_ior bit_ior bit_ior bit_ior bit_and bit_and bit_and bit_and)
1624 (cmp (minmax @0 INTEGER_CST@1) INTEGER_CST@2)
1625 (comb (cmp @0 @2) (cmp @1 @2))))
1627 /* Simplifications of shift and rotates. */
1629 (for rotate (lrotate rrotate)
1631 (rotate integer_all_onesp@0 @1)
1634 /* Optimize -1 >> x for arithmetic right shifts. */
1636 (rshift integer_all_onesp@0 @1)
1637 (if (!TYPE_UNSIGNED (type)
1638 && tree_expr_nonnegative_p (@1))
1641 /* Optimize (x >> c) << c into x & (-1<<c). */
1643 (lshift (rshift @0 INTEGER_CST@1) @1)
1644 (if (wi::ltu_p (@1, element_precision (type)))
1645 (bit_and @0 (lshift { build_minus_one_cst (type); } @1))))
1647 /* Optimize (x << c) >> c into x & ((unsigned)-1 >> c) for unsigned
1650 (rshift (lshift @0 INTEGER_CST@1) @1)
1651 (if (TYPE_UNSIGNED (type)
1652 && (wi::ltu_p (@1, element_precision (type))))
1653 (bit_and @0 (rshift { build_minus_one_cst (type); } @1))))
1655 (for shiftrotate (lrotate rrotate lshift rshift)
1657 (shiftrotate @0 integer_zerop)
1660 (shiftrotate integer_zerop@0 @1)
1662 /* Prefer vector1 << scalar to vector1 << vector2
1663 if vector2 is uniform. */
1664 (for vec (VECTOR_CST CONSTRUCTOR)
1666 (shiftrotate @0 vec@1)
1667 (with { tree tem = uniform_vector_p (@1); }
1669 (shiftrotate @0 { tem; }))))))
1671 /* Simplify X << Y where Y's low width bits are 0 to X, as only valid
1672 Y is 0. Similarly for X >> Y. */
1674 (for shift (lshift rshift)
1676 (shift @0 SSA_NAME@1)
1677 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1)))
1679 int width = ceil_log2 (element_precision (TREE_TYPE (@0)));
1680 int prec = TYPE_PRECISION (TREE_TYPE (@1));
1682 (if ((get_nonzero_bits (@1) & wi::mask (width, false, prec)) == 0)
1686 /* Rewrite an LROTATE_EXPR by a constant into an
1687 RROTATE_EXPR by a new constant. */
1689 (lrotate @0 INTEGER_CST@1)
1690 (rrotate @0 { const_binop (MINUS_EXPR, TREE_TYPE (@1),
1691 build_int_cst (TREE_TYPE (@1),
1692 element_precision (type)), @1); }))
1694 /* Turn (a OP c1) OP c2 into a OP (c1+c2). */
1695 (for op (lrotate rrotate rshift lshift)
1697 (op (op @0 INTEGER_CST@1) INTEGER_CST@2)
1698 (with { unsigned int prec = element_precision (type); }
1699 (if (wi::ge_p (@1, 0, TYPE_SIGN (TREE_TYPE (@1)))
1700 && wi::lt_p (@1, prec, TYPE_SIGN (TREE_TYPE (@1)))
1701 && wi::ge_p (@2, 0, TYPE_SIGN (TREE_TYPE (@2)))
1702 && wi::lt_p (@2, prec, TYPE_SIGN (TREE_TYPE (@2))))
1703 (with { unsigned int low = wi::add (@1, @2).to_uhwi (); }
1704 /* Deal with a OP (c1 + c2) being undefined but (a OP c1) OP c2
1705 being well defined. */
1707 (if (op == LROTATE_EXPR || op == RROTATE_EXPR)
1708 (op @0 { build_int_cst (TREE_TYPE (@1), low % prec); })
1709 (if (TYPE_UNSIGNED (type) || op == LSHIFT_EXPR)
1710 { build_zero_cst (type); }
1711 (op @0 { build_int_cst (TREE_TYPE (@1), prec - 1); })))
1712 (op @0 { build_int_cst (TREE_TYPE (@1), low); })))))))
1715 /* ((1 << A) & 1) != 0 -> A == 0
1716 ((1 << A) & 1) == 0 -> A != 0 */
1720 (cmp (bit_and (lshift integer_onep @0) integer_onep) integer_zerop)
1721 (icmp @0 { build_zero_cst (TREE_TYPE (@0)); })))
1723 /* (CST1 << A) == CST2 -> A == ctz (CST2) - ctz (CST1)
1724 (CST1 << A) != CST2 -> A != ctz (CST2) - ctz (CST1)
1728 (cmp (lshift INTEGER_CST@0 @1) INTEGER_CST@2)
1729 (with { int cand = wi::ctz (@2) - wi::ctz (@0); }
1731 || (!integer_zerop (@2)
1732 && wi::ne_p (wi::lshift (@0, cand), @2)))
1733 { constant_boolean_node (cmp == NE_EXPR, type); }
1734 (if (!integer_zerop (@2)
1735 && wi::eq_p (wi::lshift (@0, cand), @2))
1736 (cmp @1 { build_int_cst (TREE_TYPE (@1), cand); }))))))
1738 /* Fold (X << C1) & C2 into (X << C1) & (C2 | ((1 << C1) - 1))
1739 (X >> C1) & C2 into (X >> C1) & (C2 | ~((type) -1 >> C1))
1740 if the new mask might be further optimized. */
1741 (for shift (lshift rshift)
1743 (bit_and (convert?:s@4 (shift:s@5 (convert1?@3 @0) INTEGER_CST@1))
1745 (if (tree_nop_conversion_p (TREE_TYPE (@4), TREE_TYPE (@5))
1746 && TYPE_PRECISION (type) <= HOST_BITS_PER_WIDE_INT
1747 && tree_fits_uhwi_p (@1)
1748 && tree_to_uhwi (@1) > 0
1749 && tree_to_uhwi (@1) < TYPE_PRECISION (type))
1752 unsigned int shiftc = tree_to_uhwi (@1);
1753 unsigned HOST_WIDE_INT mask = TREE_INT_CST_LOW (@2);
1754 unsigned HOST_WIDE_INT newmask, zerobits = 0;
1755 tree shift_type = TREE_TYPE (@3);
1758 if (shift == LSHIFT_EXPR)
1759 zerobits = ((HOST_WIDE_INT_1U << shiftc) - 1);
1760 else if (shift == RSHIFT_EXPR
1761 && (TYPE_PRECISION (shift_type)
1762 == GET_MODE_PRECISION (TYPE_MODE (shift_type))))
1764 prec = TYPE_PRECISION (TREE_TYPE (@3));
1766 /* See if more bits can be proven as zero because of
1769 && TYPE_UNSIGNED (TREE_TYPE (@0)))
1771 tree inner_type = TREE_TYPE (@0);
1772 if ((TYPE_PRECISION (inner_type)
1773 == GET_MODE_PRECISION (TYPE_MODE (inner_type)))
1774 && TYPE_PRECISION (inner_type) < prec)
1776 prec = TYPE_PRECISION (inner_type);
1777 /* See if we can shorten the right shift. */
1779 shift_type = inner_type;
1780 /* Otherwise X >> C1 is all zeros, so we'll optimize
1781 it into (X, 0) later on by making sure zerobits
1785 zerobits = HOST_WIDE_INT_M1U;
1788 zerobits >>= HOST_BITS_PER_WIDE_INT - shiftc;
1789 zerobits <<= prec - shiftc;
1791 /* For arithmetic shift if sign bit could be set, zerobits
1792 can contain actually sign bits, so no transformation is
1793 possible, unless MASK masks them all away. In that
1794 case the shift needs to be converted into logical shift. */
1795 if (!TYPE_UNSIGNED (TREE_TYPE (@3))
1796 && prec == TYPE_PRECISION (TREE_TYPE (@3)))
1798 if ((mask & zerobits) == 0)
1799 shift_type = unsigned_type_for (TREE_TYPE (@3));
1805 /* ((X << 16) & 0xff00) is (X, 0). */
1806 (if ((mask & zerobits) == mask)
1807 { build_int_cst (type, 0); }
1808 (with { newmask = mask | zerobits; }
1809 (if (newmask != mask && (newmask & (newmask + 1)) == 0)
1812 /* Only do the transformation if NEWMASK is some integer
1814 for (prec = BITS_PER_UNIT;
1815 prec < HOST_BITS_PER_WIDE_INT; prec <<= 1)
1816 if (newmask == (HOST_WIDE_INT_1U << prec) - 1)
1819 (if (prec < HOST_BITS_PER_WIDE_INT
1820 || newmask == HOST_WIDE_INT_M1U)
1822 { tree newmaskt = build_int_cst_type (TREE_TYPE (@2), newmask); }
1823 (if (!tree_int_cst_equal (newmaskt, @2))
1824 (if (shift_type != TREE_TYPE (@3))
1825 (bit_and (convert (shift:shift_type (convert @3) @1)) { newmaskt; })
1826 (bit_and @4 { newmaskt; })))))))))))))
1828 /* Fold (X {&,^,|} C2) << C1 into (X << C1) {&,^,|} (C2 << C1)
1829 (X {&,^,|} C2) >> C1 into (X >> C1) & (C2 >> C1). */
1830 (for shift (lshift rshift)
1831 (for bit_op (bit_and bit_xor bit_ior)
1833 (shift (convert?:s (bit_op:s @0 INTEGER_CST@2)) INTEGER_CST@1)
1834 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1835 (with { tree mask = int_const_binop (shift, fold_convert (type, @2), @1); }
1836 (bit_op (shift (convert @0) @1) { mask; }))))))
1838 /* ~(~X >> Y) -> X >> Y (for arithmetic shift). */
1840 (bit_not (convert1?:s (rshift:s (convert2?@0 (bit_not @1)) @2)))
1841 (if (!TYPE_UNSIGNED (TREE_TYPE (@0))
1842 && (element_precision (TREE_TYPE (@0))
1843 <= element_precision (TREE_TYPE (@1))
1844 || !TYPE_UNSIGNED (TREE_TYPE (@1))))
1846 { tree shift_type = TREE_TYPE (@0); }
1847 (convert (rshift (convert:shift_type @1) @2)))))
1849 /* ~(~X >>r Y) -> X >>r Y
1850 ~(~X <<r Y) -> X <<r Y */
1851 (for rotate (lrotate rrotate)
1853 (bit_not (convert1?:s (rotate:s (convert2?@0 (bit_not @1)) @2)))
1854 (if ((element_precision (TREE_TYPE (@0))
1855 <= element_precision (TREE_TYPE (@1))
1856 || !TYPE_UNSIGNED (TREE_TYPE (@1)))
1857 && (element_precision (type) <= element_precision (TREE_TYPE (@0))
1858 || !TYPE_UNSIGNED (TREE_TYPE (@0))))
1860 { tree rotate_type = TREE_TYPE (@0); }
1861 (convert (rotate (convert:rotate_type @1) @2))))))
1863 /* Simplifications of conversions. */
1865 /* Basic strip-useless-type-conversions / strip_nops. */
1866 (for cvt (convert view_convert float fix_trunc)
1869 (if ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@0)))
1870 || (GENERIC && type == TREE_TYPE (@0)))
1873 /* Contract view-conversions. */
1875 (view_convert (view_convert @0))
1878 /* For integral conversions with the same precision or pointer
1879 conversions use a NOP_EXPR instead. */
1882 (if ((INTEGRAL_TYPE_P (type) || POINTER_TYPE_P (type))
1883 && (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
1884 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (@0)))
1887 /* Strip inner integral conversions that do not change precision or size, or
1888 zero-extend while keeping the same size (for bool-to-char). */
1890 (view_convert (convert@0 @1))
1891 (if ((INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
1892 && (INTEGRAL_TYPE_P (TREE_TYPE (@1)) || POINTER_TYPE_P (TREE_TYPE (@1)))
1893 && TYPE_SIZE (TREE_TYPE (@0)) == TYPE_SIZE (TREE_TYPE (@1))
1894 && (TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1))
1895 || (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (TREE_TYPE (@1))
1896 && TYPE_UNSIGNED (TREE_TYPE (@1)))))
1899 /* Re-association barriers around constants and other re-association
1900 barriers can be removed. */
1902 (paren CONSTANT_CLASS_P@0)
1905 (paren (paren@1 @0))
1908 /* Handle cases of two conversions in a row. */
1909 (for ocvt (convert float fix_trunc)
1910 (for icvt (convert float)
1915 tree inside_type = TREE_TYPE (@0);
1916 tree inter_type = TREE_TYPE (@1);
1917 int inside_int = INTEGRAL_TYPE_P (inside_type);
1918 int inside_ptr = POINTER_TYPE_P (inside_type);
1919 int inside_float = FLOAT_TYPE_P (inside_type);
1920 int inside_vec = VECTOR_TYPE_P (inside_type);
1921 unsigned int inside_prec = TYPE_PRECISION (inside_type);
1922 int inside_unsignedp = TYPE_UNSIGNED (inside_type);
1923 int inter_int = INTEGRAL_TYPE_P (inter_type);
1924 int inter_ptr = POINTER_TYPE_P (inter_type);
1925 int inter_float = FLOAT_TYPE_P (inter_type);
1926 int inter_vec = VECTOR_TYPE_P (inter_type);
1927 unsigned int inter_prec = TYPE_PRECISION (inter_type);
1928 int inter_unsignedp = TYPE_UNSIGNED (inter_type);
1929 int final_int = INTEGRAL_TYPE_P (type);
1930 int final_ptr = POINTER_TYPE_P (type);
1931 int final_float = FLOAT_TYPE_P (type);
1932 int final_vec = VECTOR_TYPE_P (type);
1933 unsigned int final_prec = TYPE_PRECISION (type);
1934 int final_unsignedp = TYPE_UNSIGNED (type);
1937 /* In addition to the cases of two conversions in a row
1938 handled below, if we are converting something to its own
1939 type via an object of identical or wider precision, neither
1940 conversion is needed. */
1941 (if (((GIMPLE && useless_type_conversion_p (type, inside_type))
1943 && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (inside_type)))
1944 && (((inter_int || inter_ptr) && final_int)
1945 || (inter_float && final_float))
1946 && inter_prec >= final_prec)
1949 /* Likewise, if the intermediate and initial types are either both
1950 float or both integer, we don't need the middle conversion if the
1951 former is wider than the latter and doesn't change the signedness
1952 (for integers). Avoid this if the final type is a pointer since
1953 then we sometimes need the middle conversion. */
1954 (if (((inter_int && inside_int) || (inter_float && inside_float))
1955 && (final_int || final_float)
1956 && inter_prec >= inside_prec
1957 && (inter_float || inter_unsignedp == inside_unsignedp))
1960 /* If we have a sign-extension of a zero-extended value, we can
1961 replace that by a single zero-extension. Likewise if the
1962 final conversion does not change precision we can drop the
1963 intermediate conversion. */
1964 (if (inside_int && inter_int && final_int
1965 && ((inside_prec < inter_prec && inter_prec < final_prec
1966 && inside_unsignedp && !inter_unsignedp)
1967 || final_prec == inter_prec))
1970 /* Two conversions in a row are not needed unless:
1971 - some conversion is floating-point (overstrict for now), or
1972 - some conversion is a vector (overstrict for now), or
1973 - the intermediate type is narrower than both initial and
1975 - the intermediate type and innermost type differ in signedness,
1976 and the outermost type is wider than the intermediate, or
1977 - the initial type is a pointer type and the precisions of the
1978 intermediate and final types differ, or
1979 - the final type is a pointer type and the precisions of the
1980 initial and intermediate types differ. */
1981 (if (! inside_float && ! inter_float && ! final_float
1982 && ! inside_vec && ! inter_vec && ! final_vec
1983 && (inter_prec >= inside_prec || inter_prec >= final_prec)
1984 && ! (inside_int && inter_int
1985 && inter_unsignedp != inside_unsignedp
1986 && inter_prec < final_prec)
1987 && ((inter_unsignedp && inter_prec > inside_prec)
1988 == (final_unsignedp && final_prec > inter_prec))
1989 && ! (inside_ptr && inter_prec != final_prec)
1990 && ! (final_ptr && inside_prec != inter_prec))
1993 /* A truncation to an unsigned type (a zero-extension) should be
1994 canonicalized as bitwise and of a mask. */
1995 (if (GIMPLE /* PR70366: doing this in GENERIC breaks -Wconversion. */
1996 && final_int && inter_int && inside_int
1997 && final_prec == inside_prec
1998 && final_prec > inter_prec
2000 (convert (bit_and @0 { wide_int_to_tree
2002 wi::mask (inter_prec, false,
2003 TYPE_PRECISION (inside_type))); })))
2005 /* If we are converting an integer to a floating-point that can
2006 represent it exactly and back to an integer, we can skip the
2007 floating-point conversion. */
2008 (if (GIMPLE /* PR66211 */
2009 && inside_int && inter_float && final_int &&
2010 (unsigned) significand_size (TYPE_MODE (inter_type))
2011 >= inside_prec - !inside_unsignedp)
2014 /* If we have a narrowing conversion to an integral type that is fed by a
2015 BIT_AND_EXPR, we might be able to remove the BIT_AND_EXPR if it merely
2016 masks off bits outside the final type (and nothing else). */
2018 (convert (bit_and @0 INTEGER_CST@1))
2019 (if (INTEGRAL_TYPE_P (type)
2020 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
2021 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))
2022 && operand_equal_p (@1, build_low_bits_mask (TREE_TYPE (@1),
2023 TYPE_PRECISION (type)), 0))
2027 /* (X /[ex] A) * A -> X. */
2029 (mult (convert1? (exact_div @0 @@1)) (convert2? @1))
2032 /* Canonicalization of binary operations. */
2034 /* Convert X + -C into X - C. */
2036 (plus @0 REAL_CST@1)
2037 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
2038 (with { tree tem = const_unop (NEGATE_EXPR, type, @1); }
2039 (if (!TREE_OVERFLOW (tem) || !flag_trapping_math)
2040 (minus @0 { tem; })))))
2042 /* Convert x+x into x*2. */
2045 (if (SCALAR_FLOAT_TYPE_P (type))
2046 (mult @0 { build_real (type, dconst2); })
2047 (if (INTEGRAL_TYPE_P (type))
2048 (mult @0 { build_int_cst (type, 2); }))))
2051 (minus integer_zerop @1)
2054 /* (ARG0 - ARG1) is the same as (-ARG1 + ARG0). So check whether
2055 ARG0 is zero and X + ARG0 reduces to X, since that would mean
2056 (-ARG1 + ARG0) reduces to -ARG1. */
2058 (minus real_zerop@0 @1)
2059 (if (fold_real_zero_addition_p (type, @0, 0))
2062 /* Transform x * -1 into -x. */
2064 (mult @0 integer_minus_onep)
2067 /* True if we can easily extract the real and imaginary parts of a complex
2069 (match compositional_complex
2070 (convert? (complex @0 @1)))
2072 /* COMPLEX_EXPR and REALPART/IMAGPART_EXPR cancellations. */
2074 (complex (realpart @0) (imagpart @0))
2077 (realpart (complex @0 @1))
2080 (imagpart (complex @0 @1))
2083 /* Sometimes we only care about half of a complex expression. */
2085 (realpart (convert?:s (conj:s @0)))
2086 (convert (realpart @0)))
2088 (imagpart (convert?:s (conj:s @0)))
2089 (convert (negate (imagpart @0))))
2090 (for part (realpart imagpart)
2091 (for op (plus minus)
2093 (part (convert?:s@2 (op:s @0 @1)))
2094 (convert (op (part @0) (part @1))))))
2096 (realpart (convert?:s (CEXPI:s @0)))
2099 (imagpart (convert?:s (CEXPI:s @0)))
2102 /* conj(conj(x)) -> x */
2104 (conj (convert? (conj @0)))
2105 (if (tree_nop_conversion_p (TREE_TYPE (@0), type))
2108 /* conj({x,y}) -> {x,-y} */
2110 (conj (convert?:s (complex:s @0 @1)))
2111 (with { tree itype = TREE_TYPE (type); }
2112 (complex (convert:itype @0) (negate (convert:itype @1)))))
2114 /* BSWAP simplifications, transforms checked by gcc.dg/builtin-bswap-8.c. */
2115 (for bswap (BUILT_IN_BSWAP16 BUILT_IN_BSWAP32 BUILT_IN_BSWAP64)
2120 (bswap (bit_not (bswap @0)))
2122 (for bitop (bit_xor bit_ior bit_and)
2124 (bswap (bitop:c (bswap @0) @1))
2125 (bitop @0 (bswap @1)))))
2128 /* Combine COND_EXPRs and VEC_COND_EXPRs. */
2130 /* Simplify constant conditions.
2131 Only optimize constant conditions when the selected branch
2132 has the same type as the COND_EXPR. This avoids optimizing
2133 away "c ? x : throw", where the throw has a void type.
2134 Note that we cannot throw away the fold-const.c variant nor
2135 this one as we depend on doing this transform before possibly
2136 A ? B : B -> B triggers and the fold-const.c one can optimize
2137 0 ? A : B to B even if A has side-effects. Something
2138 genmatch cannot handle. */
2140 (cond INTEGER_CST@0 @1 @2)
2141 (if (integer_zerop (@0))
2142 (if (!VOID_TYPE_P (TREE_TYPE (@2)) || VOID_TYPE_P (type))
2144 (if (!VOID_TYPE_P (TREE_TYPE (@1)) || VOID_TYPE_P (type))
2147 (vec_cond VECTOR_CST@0 @1 @2)
2148 (if (integer_all_onesp (@0))
2150 (if (integer_zerop (@0))
2153 /* Simplification moved from fold_cond_expr_with_comparison. It may also
2155 /* This pattern implements two kinds simplification:
2158 (cond (cmp (convert1? x) c1) (convert2? x) c2) -> (minmax (x c)) if:
2159 1) Conversions are type widening from smaller type.
2160 2) Const c1 equals to c2 after canonicalizing comparison.
2161 3) Comparison has tree code LT, LE, GT or GE.
2162 This specific pattern is needed when (cmp (convert x) c) may not
2163 be simplified by comparison patterns because of multiple uses of
2164 x. It also makes sense here because simplifying across multiple
2165 referred var is always benefitial for complicated cases.
2168 (cond (eq (convert1? x) c1) (convert2? x) c2) -> (cond (eq x c1) c1 c2). */
2169 (for cmp (lt le gt ge eq)
2171 (cond (cmp (convert1? @1) INTEGER_CST@3) (convert2? @1) INTEGER_CST@2)
2174 tree from_type = TREE_TYPE (@1);
2175 tree c1_type = TREE_TYPE (@3), c2_type = TREE_TYPE (@2);
2176 enum tree_code code = ERROR_MARK;
2178 if (INTEGRAL_TYPE_P (from_type)
2179 && int_fits_type_p (@2, from_type)
2180 && (types_match (c1_type, from_type)
2181 || (TYPE_PRECISION (c1_type) > TYPE_PRECISION (from_type)
2182 && (TYPE_UNSIGNED (from_type)
2183 || TYPE_SIGN (c1_type) == TYPE_SIGN (from_type))))
2184 && (types_match (c2_type, from_type)
2185 || (TYPE_PRECISION (c2_type) > TYPE_PRECISION (from_type)
2186 && (TYPE_UNSIGNED (from_type)
2187 || TYPE_SIGN (c2_type) == TYPE_SIGN (from_type)))))
2191 if (wi::to_widest (@3) == (wi::to_widest (@2) - 1))
2193 /* X <= Y - 1 equals to X < Y. */
2196 /* X > Y - 1 equals to X >= Y. */
2200 if (wi::to_widest (@3) == (wi::to_widest (@2) + 1))
2202 /* X < Y + 1 equals to X <= Y. */
2205 /* X >= Y + 1 equals to X > Y. */
2209 if (code != ERROR_MARK
2210 || wi::to_widest (@2) == wi::to_widest (@3))
2212 if (cmp == LT_EXPR || cmp == LE_EXPR)
2214 if (cmp == GT_EXPR || cmp == GE_EXPR)
2218 /* Can do A == C1 ? A : C2 -> A == C1 ? C1 : C2? */
2219 else if (int_fits_type_p (@3, from_type))
2223 (if (code == MAX_EXPR)
2224 (convert (max @1 (convert @2)))
2225 (if (code == MIN_EXPR)
2226 (convert (min @1 (convert @2)))
2227 (if (code == EQ_EXPR)
2228 (convert (cond (eq @1 (convert @3))
2229 (convert:from_type @3) (convert:from_type @2)))))))))
2231 /* (cond (cmp (convert? x) c1) (op x c2) c3) -> (op (minmax x c1) c2) if:
2233 1) OP is PLUS or MINUS.
2234 2) CMP is LT, LE, GT or GE.
2235 3) C3 == (C1 op C2), and computation doesn't have undefined behavior.
2237 This pattern also handles special cases like:
2239 A) Operand x is a unsigned to signed type conversion and c1 is
2240 integer zero. In this case,
2241 (signed type)x < 0 <=> x > MAX_VAL(signed type)
2242 (signed type)x >= 0 <=> x <= MAX_VAL(signed type)
2243 B) Const c1 may not equal to (C3 op' C2). In this case we also
2244 check equality for (c1+1) and (c1-1) by adjusting comparison
2247 TODO: Though signed type is handled by this pattern, it cannot be
2248 simplified at the moment because C standard requires additional
2249 type promotion. In order to match&simplify it here, the IR needs
2250 to be cleaned up by other optimizers, i.e, VRP. */
2251 (for op (plus minus)
2252 (for cmp (lt le gt ge)
2254 (cond (cmp (convert? @X) INTEGER_CST@1) (op @X INTEGER_CST@2) INTEGER_CST@3)
2255 (with { tree from_type = TREE_TYPE (@X), to_type = TREE_TYPE (@1); }
2256 (if (types_match (from_type, to_type)
2257 /* Check if it is special case A). */
2258 || (TYPE_UNSIGNED (from_type)
2259 && !TYPE_UNSIGNED (to_type)
2260 && TYPE_PRECISION (from_type) == TYPE_PRECISION (to_type)
2261 && integer_zerop (@1)
2262 && (cmp == LT_EXPR || cmp == GE_EXPR)))
2265 bool overflow = false;
2266 enum tree_code code, cmp_code = cmp;
2267 wide_int real_c1, c1 = @1, c2 = @2, c3 = @3;
2268 signop sgn = TYPE_SIGN (from_type);
2270 /* Handle special case A), given x of unsigned type:
2271 ((signed type)x < 0) <=> (x > MAX_VAL(signed type))
2272 ((signed type)x >= 0) <=> (x <= MAX_VAL(signed type)) */
2273 if (!types_match (from_type, to_type))
2275 if (cmp_code == LT_EXPR)
2277 if (cmp_code == GE_EXPR)
2279 c1 = wi::max_value (to_type);
2281 /* To simplify this pattern, we require c3 = (c1 op c2). Here we
2282 compute (c3 op' c2) and check if it equals to c1 with op' being
2283 the inverted operator of op. Make sure overflow doesn't happen
2284 if it is undefined. */
2285 if (op == PLUS_EXPR)
2286 real_c1 = wi::sub (c3, c2, sgn, &overflow);
2288 real_c1 = wi::add (c3, c2, sgn, &overflow);
2291 if (!overflow || !TYPE_OVERFLOW_UNDEFINED (from_type))
2293 /* Check if c1 equals to real_c1. Boundary condition is handled
2294 by adjusting comparison operation if necessary. */
2295 if (!wi::cmp (wi::sub (real_c1, 1, sgn, &overflow), c1, sgn)
2298 /* X <= Y - 1 equals to X < Y. */
2299 if (cmp_code == LE_EXPR)
2301 /* X > Y - 1 equals to X >= Y. */
2302 if (cmp_code == GT_EXPR)
2305 if (!wi::cmp (wi::add (real_c1, 1, sgn, &overflow), c1, sgn)
2308 /* X < Y + 1 equals to X <= Y. */
2309 if (cmp_code == LT_EXPR)
2311 /* X >= Y + 1 equals to X > Y. */
2312 if (cmp_code == GE_EXPR)
2315 if (code != cmp_code || !wi::cmp (real_c1, c1, sgn))
2317 if (cmp_code == LT_EXPR || cmp_code == LE_EXPR)
2319 if (cmp_code == GT_EXPR || cmp_code == GE_EXPR)
2324 (if (code == MAX_EXPR)
2325 (op (max @X { wide_int_to_tree (from_type, real_c1); })
2326 { wide_int_to_tree (from_type, c2); })
2327 (if (code == MIN_EXPR)
2328 (op (min @X { wide_int_to_tree (from_type, real_c1); })
2329 { wide_int_to_tree (from_type, c2); })))))))))
2331 (for cnd (cond vec_cond)
2332 /* A ? B : (A ? X : C) -> A ? B : C. */
2334 (cnd @0 (cnd @0 @1 @2) @3)
2337 (cnd @0 @1 (cnd @0 @2 @3))
2339 /* A ? B : (!A ? C : X) -> A ? B : C. */
2340 /* ??? This matches embedded conditions open-coded because genmatch
2341 would generate matching code for conditions in separate stmts only.
2342 The following is still important to merge then and else arm cases
2343 from if-conversion. */
2345 (cnd @0 @1 (cnd @2 @3 @4))
2346 (if (COMPARISON_CLASS_P (@0)
2347 && COMPARISON_CLASS_P (@2)
2348 && invert_tree_comparison
2349 (TREE_CODE (@0), HONOR_NANS (TREE_OPERAND (@0, 0))) == TREE_CODE (@2)
2350 && operand_equal_p (TREE_OPERAND (@0, 0), TREE_OPERAND (@2, 0), 0)
2351 && operand_equal_p (TREE_OPERAND (@0, 1), TREE_OPERAND (@2, 1), 0))
2354 (cnd @0 (cnd @1 @2 @3) @4)
2355 (if (COMPARISON_CLASS_P (@0)
2356 && COMPARISON_CLASS_P (@1)
2357 && invert_tree_comparison
2358 (TREE_CODE (@0), HONOR_NANS (TREE_OPERAND (@0, 0))) == TREE_CODE (@1)
2359 && operand_equal_p (TREE_OPERAND (@0, 0), TREE_OPERAND (@1, 0), 0)
2360 && operand_equal_p (TREE_OPERAND (@0, 1), TREE_OPERAND (@1, 1), 0))
2363 /* A ? B : B -> B. */
2368 /* !A ? B : C -> A ? C : B. */
2370 (cnd (logical_inverted_value truth_valued_p@0) @1 @2)
2373 /* A + (B vcmp C ? 1 : 0) -> A - (B vcmp C ? -1 : 0), since vector comparisons
2374 return all -1 or all 0 results. */
2375 /* ??? We could instead convert all instances of the vec_cond to negate,
2376 but that isn't necessarily a win on its own. */
2378 (plus:c @3 (view_convert? (vec_cond:s @0 integer_each_onep@1 integer_zerop@2)))
2379 (if (VECTOR_TYPE_P (type)
2380 && TYPE_VECTOR_SUBPARTS (type) == TYPE_VECTOR_SUBPARTS (TREE_TYPE (@1))
2381 && (TYPE_MODE (TREE_TYPE (type))
2382 == TYPE_MODE (TREE_TYPE (TREE_TYPE (@1)))))
2383 (minus @3 (view_convert (vec_cond @0 (negate @1) @2)))))
2385 /* ... likewise A - (B vcmp C ? 1 : 0) -> A + (B vcmp C ? -1 : 0). */
2387 (minus @3 (view_convert? (vec_cond:s @0 integer_each_onep@1 integer_zerop@2)))
2388 (if (VECTOR_TYPE_P (type)
2389 && TYPE_VECTOR_SUBPARTS (type) == TYPE_VECTOR_SUBPARTS (TREE_TYPE (@1))
2390 && (TYPE_MODE (TREE_TYPE (type))
2391 == TYPE_MODE (TREE_TYPE (TREE_TYPE (@1)))))
2392 (plus @3 (view_convert (vec_cond @0 (negate @1) @2)))))
2395 /* Simplifications of comparisons. */
2397 /* See if we can reduce the magnitude of a constant involved in a
2398 comparison by changing the comparison code. This is a canonicalization
2399 formerly done by maybe_canonicalize_comparison_1. */
2403 (cmp @0 INTEGER_CST@1)
2404 (if (tree_int_cst_sgn (@1) == -1)
2405 (acmp @0 { wide_int_to_tree (TREE_TYPE (@1), wi::add (@1, 1)); }))))
2409 (cmp @0 INTEGER_CST@1)
2410 (if (tree_int_cst_sgn (@1) == 1)
2411 (acmp @0 { wide_int_to_tree (TREE_TYPE (@1), wi::sub (@1, 1)); }))))
2414 /* We can simplify a logical negation of a comparison to the
2415 inverted comparison. As we cannot compute an expression
2416 operator using invert_tree_comparison we have to simulate
2417 that with expression code iteration. */
2418 (for cmp (tcc_comparison)
2419 icmp (inverted_tcc_comparison)
2420 ncmp (inverted_tcc_comparison_with_nans)
2421 /* Ideally we'd like to combine the following two patterns
2422 and handle some more cases by using
2423 (logical_inverted_value (cmp @0 @1))
2424 here but for that genmatch would need to "inline" that.
2425 For now implement what forward_propagate_comparison did. */
2427 (bit_not (cmp @0 @1))
2428 (if (VECTOR_TYPE_P (type)
2429 || (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1))
2430 /* Comparison inversion may be impossible for trapping math,
2431 invert_tree_comparison will tell us. But we can't use
2432 a computed operator in the replacement tree thus we have
2433 to play the trick below. */
2434 (with { enum tree_code ic = invert_tree_comparison
2435 (cmp, HONOR_NANS (@0)); }
2441 (bit_xor (cmp @0 @1) integer_truep)
2442 (with { enum tree_code ic = invert_tree_comparison
2443 (cmp, HONOR_NANS (@0)); }
2449 /* Transform comparisons of the form X - Y CMP 0 to X CMP Y.
2450 ??? The transformation is valid for the other operators if overflow
2451 is undefined for the type, but performing it here badly interacts
2452 with the transformation in fold_cond_expr_with_comparison which
2453 attempts to synthetize ABS_EXPR. */
2456 (cmp (minus@2 @0 @1) integer_zerop)
2457 (if (single_use (@2))
2460 /* Transform comparisons of the form X * C1 CMP 0 to X CMP 0 in the
2461 signed arithmetic case. That form is created by the compiler
2462 often enough for folding it to be of value. One example is in
2463 computing loop trip counts after Operator Strength Reduction. */
2464 (for cmp (simple_comparison)
2465 scmp (swapped_simple_comparison)
2467 (cmp (mult@3 @0 INTEGER_CST@1) integer_zerop@2)
2468 /* Handle unfolded multiplication by zero. */
2469 (if (integer_zerop (@1))
2471 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2472 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2474 /* If @1 is negative we swap the sense of the comparison. */
2475 (if (tree_int_cst_sgn (@1) < 0)
2479 /* Simplify comparison of something with itself. For IEEE
2480 floating-point, we can only do some of these simplifications. */
2484 (if (! FLOAT_TYPE_P (TREE_TYPE (@0))
2485 || ! HONOR_NANS (@0))
2486 { constant_boolean_node (true, type); }
2487 (if (cmp != EQ_EXPR)
2493 || ! FLOAT_TYPE_P (TREE_TYPE (@0))
2494 || ! HONOR_NANS (@0))
2495 { constant_boolean_node (false, type); })))
2496 (for cmp (unle unge uneq)
2499 { constant_boolean_node (true, type); }))
2500 (for cmp (unlt ungt)
2506 (if (!flag_trapping_math)
2507 { constant_boolean_node (false, type); }))
2509 /* Fold ~X op ~Y as Y op X. */
2510 (for cmp (simple_comparison)
2512 (cmp (bit_not@2 @0) (bit_not@3 @1))
2513 (if (single_use (@2) && single_use (@3))
2516 /* Fold ~X op C as X op' ~C, where op' is the swapped comparison. */
2517 (for cmp (simple_comparison)
2518 scmp (swapped_simple_comparison)
2520 (cmp (bit_not@2 @0) CONSTANT_CLASS_P@1)
2521 (if (single_use (@2)
2522 && (TREE_CODE (@1) == INTEGER_CST || TREE_CODE (@1) == VECTOR_CST))
2523 (scmp @0 (bit_not @1)))))
2525 (for cmp (simple_comparison)
2526 /* Fold (double)float1 CMP (double)float2 into float1 CMP float2. */
2528 (cmp (convert@2 @0) (convert? @1))
2529 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
2530 && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@2))
2531 == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0)))
2532 && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@2))
2533 == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1))))
2536 tree type1 = TREE_TYPE (@1);
2537 if (TREE_CODE (@1) == REAL_CST && !DECIMAL_FLOAT_TYPE_P (type1))
2539 REAL_VALUE_TYPE orig = TREE_REAL_CST (@1);
2540 if (TYPE_PRECISION (type1) > TYPE_PRECISION (float_type_node)
2541 && exact_real_truncate (TYPE_MODE (float_type_node), &orig))
2542 type1 = float_type_node;
2543 if (TYPE_PRECISION (type1) > TYPE_PRECISION (double_type_node)
2544 && exact_real_truncate (TYPE_MODE (double_type_node), &orig))
2545 type1 = double_type_node;
2548 = (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type1)
2549 ? TREE_TYPE (@0) : type1);
2551 (if (TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (newtype))
2552 (cmp (convert:newtype @0) (convert:newtype @1))))))
2556 /* IEEE doesn't distinguish +0 and -0 in comparisons. */
2558 /* a CMP (-0) -> a CMP 0 */
2559 (if (REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@1)))
2560 (cmp @0 { build_real (TREE_TYPE (@1), dconst0); }))
2561 /* x != NaN is always true, other ops are always false. */
2562 (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
2563 && ! HONOR_SNANS (@1))
2564 { constant_boolean_node (cmp == NE_EXPR, type); })
2565 /* Fold comparisons against infinity. */
2566 (if (REAL_VALUE_ISINF (TREE_REAL_CST (@1))
2567 && MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (@1))))
2570 REAL_VALUE_TYPE max;
2571 enum tree_code code = cmp;
2572 bool neg = REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1));
2574 code = swap_tree_comparison (code);
2577 /* x > +Inf is always false, if with ignore sNANs. */
2578 (if (code == GT_EXPR
2579 && ! HONOR_SNANS (@0))
2580 { constant_boolean_node (false, type); })
2581 (if (code == LE_EXPR)
2582 /* x <= +Inf is always true, if we don't case about NaNs. */
2583 (if (! HONOR_NANS (@0))
2584 { constant_boolean_node (true, type); }
2585 /* x <= +Inf is the same as x == x, i.e. !isnan(x). */
2587 /* x == +Inf and x >= +Inf are always equal to x > DBL_MAX. */
2588 (if (code == EQ_EXPR || code == GE_EXPR)
2589 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
2591 (lt @0 { build_real (TREE_TYPE (@0), max); })
2592 (gt @0 { build_real (TREE_TYPE (@0), max); }))))
2593 /* x < +Inf is always equal to x <= DBL_MAX. */
2594 (if (code == LT_EXPR)
2595 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
2597 (ge @0 { build_real (TREE_TYPE (@0), max); })
2598 (le @0 { build_real (TREE_TYPE (@0), max); }))))
2599 /* x != +Inf is always equal to !(x > DBL_MAX). */
2600 (if (code == NE_EXPR)
2601 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
2602 (if (! HONOR_NANS (@0))
2604 (ge @0 { build_real (TREE_TYPE (@0), max); })
2605 (le @0 { build_real (TREE_TYPE (@0), max); }))
2607 (bit_xor (lt @0 { build_real (TREE_TYPE (@0), max); })
2608 { build_one_cst (type); })
2609 (bit_xor (gt @0 { build_real (TREE_TYPE (@0), max); })
2610 { build_one_cst (type); }))))))))))
2612 /* If this is a comparison of a real constant with a PLUS_EXPR
2613 or a MINUS_EXPR of a real constant, we can convert it into a
2614 comparison with a revised real constant as long as no overflow
2615 occurs when unsafe_math_optimizations are enabled. */
2616 (if (flag_unsafe_math_optimizations)
2617 (for op (plus minus)
2619 (cmp (op @0 REAL_CST@1) REAL_CST@2)
2622 tree tem = const_binop (op == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR,
2623 TREE_TYPE (@1), @2, @1);
2625 (if (tem && !TREE_OVERFLOW (tem))
2626 (cmp @0 { tem; }))))))
2628 /* Likewise, we can simplify a comparison of a real constant with
2629 a MINUS_EXPR whose first operand is also a real constant, i.e.
2630 (c1 - x) < c2 becomes x > c1-c2. Reordering is allowed on
2631 floating-point types only if -fassociative-math is set. */
2632 (if (flag_associative_math)
2634 (cmp (minus REAL_CST@0 @1) REAL_CST@2)
2635 (with { tree tem = const_binop (MINUS_EXPR, TREE_TYPE (@1), @0, @2); }
2636 (if (tem && !TREE_OVERFLOW (tem))
2637 (cmp { tem; } @1)))))
2639 /* Fold comparisons against built-in math functions. */
2640 (if (flag_unsafe_math_optimizations
2641 && ! flag_errno_math)
2644 (cmp (sq @0) REAL_CST@1)
2646 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
2648 /* sqrt(x) < y is always false, if y is negative. */
2649 (if (cmp == EQ_EXPR || cmp == LT_EXPR || cmp == LE_EXPR)
2650 { constant_boolean_node (false, type); })
2651 /* sqrt(x) > y is always true, if y is negative and we
2652 don't care about NaNs, i.e. negative values of x. */
2653 (if (cmp == NE_EXPR || !HONOR_NANS (@0))
2654 { constant_boolean_node (true, type); })
2655 /* sqrt(x) > y is the same as x >= 0, if y is negative. */
2656 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })))
2657 (if (real_equal (TREE_REAL_CST_PTR (@1), &dconst0))
2659 /* sqrt(x) < 0 is always false. */
2660 (if (cmp == LT_EXPR)
2661 { constant_boolean_node (false, type); })
2662 /* sqrt(x) >= 0 is always true if we don't care about NaNs. */
2663 (if (cmp == GE_EXPR && !HONOR_NANS (@0))
2664 { constant_boolean_node (true, type); })
2665 /* sqrt(x) <= 0 -> x == 0. */
2666 (if (cmp == LE_EXPR)
2668 /* Otherwise sqrt(x) cmp 0 -> x cmp 0. Here cmp can be >=, >,
2669 == or !=. In the last case:
2671 (sqrt(x) != 0) == (NaN != 0) == true == (x != 0)
2673 if x is negative or NaN. Due to -funsafe-math-optimizations,
2674 the results for other x follow from natural arithmetic. */
2676 (if (cmp == GT_EXPR || cmp == GE_EXPR)
2680 real_arithmetic (&c2, MULT_EXPR,
2681 &TREE_REAL_CST (@1), &TREE_REAL_CST (@1));
2682 real_convert (&c2, TYPE_MODE (TREE_TYPE (@0)), &c2);
2684 (if (REAL_VALUE_ISINF (c2))
2685 /* sqrt(x) > y is x == +Inf, when y is very large. */
2686 (if (HONOR_INFINITIES (@0))
2687 (eq @0 { build_real (TREE_TYPE (@0), c2); })
2688 { constant_boolean_node (false, type); })
2689 /* sqrt(x) > c is the same as x > c*c. */
2690 (cmp @0 { build_real (TREE_TYPE (@0), c2); }))))
2691 (if (cmp == LT_EXPR || cmp == LE_EXPR)
2695 real_arithmetic (&c2, MULT_EXPR,
2696 &TREE_REAL_CST (@1), &TREE_REAL_CST (@1));
2697 real_convert (&c2, TYPE_MODE (TREE_TYPE (@0)), &c2);
2699 (if (REAL_VALUE_ISINF (c2))
2701 /* sqrt(x) < y is always true, when y is a very large
2702 value and we don't care about NaNs or Infinities. */
2703 (if (! HONOR_NANS (@0) && ! HONOR_INFINITIES (@0))
2704 { constant_boolean_node (true, type); })
2705 /* sqrt(x) < y is x != +Inf when y is very large and we
2706 don't care about NaNs. */
2707 (if (! HONOR_NANS (@0))
2708 (ne @0 { build_real (TREE_TYPE (@0), c2); }))
2709 /* sqrt(x) < y is x >= 0 when y is very large and we
2710 don't care about Infinities. */
2711 (if (! HONOR_INFINITIES (@0))
2712 (ge @0 { build_real (TREE_TYPE (@0), dconst0); }))
2713 /* sqrt(x) < y is x >= 0 && x != +Inf, when y is large. */
2716 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
2717 (ne @0 { build_real (TREE_TYPE (@0), c2); }))))
2718 /* sqrt(x) < c is the same as x < c*c, if we ignore NaNs. */
2719 (if (! HONOR_NANS (@0))
2720 (cmp @0 { build_real (TREE_TYPE (@0), c2); })
2721 /* sqrt(x) < c is the same as x >= 0 && x < c*c. */
2724 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
2725 (cmp @0 { build_real (TREE_TYPE (@0), c2); })))))))))
2726 /* Transform sqrt(x) cmp sqrt(y) -> x cmp y. */
2728 (cmp (sq @0) (sq @1))
2729 (if (! HONOR_NANS (@0))
2732 /* Fold A /[ex] B CMP C to A CMP B * C. */
2735 (cmp (exact_div @0 @1) INTEGER_CST@2)
2736 (if (!integer_zerop (@1))
2737 (if (wi::eq_p (@2, 0))
2739 (if (TREE_CODE (@1) == INTEGER_CST)
2743 wide_int prod = wi::mul (@2, @1, TYPE_SIGN (TREE_TYPE (@1)), &ovf);
2746 { constant_boolean_node (cmp == NE_EXPR, type); }
2747 (cmp @0 { wide_int_to_tree (TREE_TYPE (@0), prod); }))))))))
2748 (for cmp (lt le gt ge)
2750 (cmp (exact_div @0 INTEGER_CST@1) INTEGER_CST@2)
2751 (if (wi::gt_p (@1, 0, TYPE_SIGN (TREE_TYPE (@1))))
2755 wide_int prod = wi::mul (@2, @1, TYPE_SIGN (TREE_TYPE (@1)), &ovf);
2758 { constant_boolean_node (wi::lt_p (@2, 0, TYPE_SIGN (TREE_TYPE (@2)))
2759 != (cmp == LT_EXPR || cmp == LE_EXPR), type); }
2760 (cmp @0 { wide_int_to_tree (TREE_TYPE (@0), prod); }))))))
2762 /* Unordered tests if either argument is a NaN. */
2764 (bit_ior (unordered @0 @0) (unordered @1 @1))
2765 (if (types_match (@0, @1))
2768 (bit_and (ordered @0 @0) (ordered @1 @1))
2769 (if (types_match (@0, @1))
2772 (bit_ior:c (unordered @0 @0) (unordered:c@2 @0 @1))
2775 (bit_and:c (ordered @0 @0) (ordered:c@2 @0 @1))
2778 /* Simple range test simplifications. */
2779 /* A < B || A >= B -> true. */
2780 (for test1 (lt le le le ne ge)
2781 test2 (ge gt ge ne eq ne)
2783 (bit_ior:c (test1 @0 @1) (test2 @0 @1))
2784 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2785 || VECTOR_INTEGER_TYPE_P (TREE_TYPE (@0)))
2786 { constant_boolean_node (true, type); })))
2787 /* A < B && A >= B -> false. */
2788 (for test1 (lt lt lt le ne eq)
2789 test2 (ge gt eq gt eq gt)
2791 (bit_and:c (test1 @0 @1) (test2 @0 @1))
2792 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2793 || VECTOR_INTEGER_TYPE_P (TREE_TYPE (@0)))
2794 { constant_boolean_node (false, type); })))
2796 /* A & (2**N - 1) <= 2**K - 1 -> A & (2**N - 2**K) == 0
2797 A & (2**N - 1) > 2**K - 1 -> A & (2**N - 2**K) != 0
2799 Note that comparisons
2800 A & (2**N - 1) < 2**K -> A & (2**N - 2**K) == 0
2801 A & (2**N - 1) >= 2**K -> A & (2**N - 2**K) != 0
2802 will be canonicalized to above so there's no need to
2809 (cmp (bit_and@0 @1 INTEGER_CST@2) INTEGER_CST@3)
2810 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)))
2813 tree ty = TREE_TYPE (@0);
2814 unsigned prec = TYPE_PRECISION (ty);
2815 wide_int mask = wi::to_wide (@2, prec);
2816 wide_int rhs = wi::to_wide (@3, prec);
2817 signop sgn = TYPE_SIGN (ty);
2819 (if ((mask & (mask + 1)) == 0 && wi::gt_p (rhs, 0, sgn)
2820 && (rhs & (rhs + 1)) == 0 && wi::ge_p (mask, rhs, sgn))
2821 (eqcmp (bit_and @1 { wide_int_to_tree (ty, mask - rhs); })
2822 { build_zero_cst (ty); }))))))
2824 /* -A CMP -B -> B CMP A. */
2825 (for cmp (tcc_comparison)
2826 scmp (swapped_tcc_comparison)
2828 (cmp (negate @0) (negate @1))
2829 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
2830 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2831 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
2834 (cmp (negate @0) CONSTANT_CLASS_P@1)
2835 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
2836 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2837 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
2838 (with { tree tem = const_unop (NEGATE_EXPR, TREE_TYPE (@0), @1); }
2839 (if (tem && !TREE_OVERFLOW (tem))
2840 (scmp @0 { tem; }))))))
2842 /* Convert ABS_EXPR<x> == 0 or ABS_EXPR<x> != 0 to x == 0 or x != 0. */
2845 (op (abs @0) zerop@1)
2848 /* From fold_sign_changed_comparison and fold_widened_comparison. */
2849 (for cmp (simple_comparison)
2851 (cmp (convert@0 @00) (convert?@1 @10))
2852 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2853 /* Disable this optimization if we're casting a function pointer
2854 type on targets that require function pointer canonicalization. */
2855 && !(targetm.have_canonicalize_funcptr_for_compare ()
2856 && TREE_CODE (TREE_TYPE (@00)) == POINTER_TYPE
2857 && TREE_CODE (TREE_TYPE (TREE_TYPE (@00))) == FUNCTION_TYPE)
2859 (if (TYPE_PRECISION (TREE_TYPE (@00)) == TYPE_PRECISION (TREE_TYPE (@0))
2860 && (TREE_CODE (@10) == INTEGER_CST
2861 || (@1 != @10 && types_match (TREE_TYPE (@10), TREE_TYPE (@00))))
2862 && (TYPE_UNSIGNED (TREE_TYPE (@00)) == TYPE_UNSIGNED (TREE_TYPE (@0))
2865 && (POINTER_TYPE_P (TREE_TYPE (@00)) == POINTER_TYPE_P (TREE_TYPE (@0))))
2866 /* ??? The special-casing of INTEGER_CST conversion was in the original
2867 code and here to avoid a spurious overflow flag on the resulting
2868 constant which fold_convert produces. */
2869 (if (TREE_CODE (@1) == INTEGER_CST)
2870 (cmp @00 { force_fit_type (TREE_TYPE (@00), wi::to_widest (@1), 0,
2871 TREE_OVERFLOW (@1)); })
2872 (cmp @00 (convert @1)))
2874 (if (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (TREE_TYPE (@00)))
2875 /* If possible, express the comparison in the shorter mode. */
2876 (if ((cmp == EQ_EXPR || cmp == NE_EXPR
2877 || TYPE_UNSIGNED (TREE_TYPE (@0)) == TYPE_UNSIGNED (TREE_TYPE (@00))
2878 || (!TYPE_UNSIGNED (TREE_TYPE (@0))
2879 && TYPE_UNSIGNED (TREE_TYPE (@00))))
2880 && (types_match (TREE_TYPE (@10), TREE_TYPE (@00))
2881 || ((TYPE_PRECISION (TREE_TYPE (@00))
2882 >= TYPE_PRECISION (TREE_TYPE (@10)))
2883 && (TYPE_UNSIGNED (TREE_TYPE (@00))
2884 == TYPE_UNSIGNED (TREE_TYPE (@10))))
2885 || (TREE_CODE (@10) == INTEGER_CST
2886 && INTEGRAL_TYPE_P (TREE_TYPE (@00))
2887 && int_fits_type_p (@10, TREE_TYPE (@00)))))
2888 (cmp @00 (convert @10))
2889 (if (TREE_CODE (@10) == INTEGER_CST
2890 && INTEGRAL_TYPE_P (TREE_TYPE (@00))
2891 && !int_fits_type_p (@10, TREE_TYPE (@00)))
2894 tree min = lower_bound_in_type (TREE_TYPE (@10), TREE_TYPE (@00));
2895 tree max = upper_bound_in_type (TREE_TYPE (@10), TREE_TYPE (@00));
2896 bool above = integer_nonzerop (const_binop (LT_EXPR, type, max, @10));
2897 bool below = integer_nonzerop (const_binop (LT_EXPR, type, @10, min));
2899 (if (above || below)
2900 (if (cmp == EQ_EXPR || cmp == NE_EXPR)
2901 { constant_boolean_node (cmp == EQ_EXPR ? false : true, type); }
2902 (if (cmp == LT_EXPR || cmp == LE_EXPR)
2903 { constant_boolean_node (above ? true : false, type); }
2904 (if (cmp == GT_EXPR || cmp == GE_EXPR)
2905 { constant_boolean_node (above ? false : true, type); }))))))))))))
2908 /* A local variable can never be pointed to by
2909 the default SSA name of an incoming parameter.
2910 SSA names are canonicalized to 2nd place. */
2912 (cmp addr@0 SSA_NAME@1)
2913 (if (SSA_NAME_IS_DEFAULT_DEF (@1)
2914 && TREE_CODE (SSA_NAME_VAR (@1)) == PARM_DECL)
2915 (with { tree base = get_base_address (TREE_OPERAND (@0, 0)); }
2916 (if (TREE_CODE (base) == VAR_DECL
2917 && auto_var_in_fn_p (base, current_function_decl))
2918 (if (cmp == NE_EXPR)
2919 { constant_boolean_node (true, type); }
2920 { constant_boolean_node (false, type); }))))))
2922 /* Equality compare simplifications from fold_binary */
2925 /* If we have (A | C) == D where C & ~D != 0, convert this into 0.
2926 Similarly for NE_EXPR. */
2928 (cmp (convert?@3 (bit_ior @0 INTEGER_CST@1)) INTEGER_CST@2)
2929 (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0))
2930 && wi::bit_and_not (@1, @2) != 0)
2931 { constant_boolean_node (cmp == NE_EXPR, type); }))
2933 /* (X ^ Y) == 0 becomes X == Y, and (X ^ Y) != 0 becomes X != Y. */
2935 (cmp (bit_xor @0 @1) integer_zerop)
2938 /* (X ^ Y) == Y becomes X == 0.
2939 Likewise (X ^ Y) == X becomes Y == 0. */
2941 (cmp:c (bit_xor:c @0 @1) @0)
2942 (cmp @1 { build_zero_cst (TREE_TYPE (@1)); }))
2944 /* (X ^ C1) op C2 can be rewritten as X op (C1 ^ C2). */
2946 (cmp (convert?@3 (bit_xor @0 INTEGER_CST@1)) INTEGER_CST@2)
2947 (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0)))
2948 (cmp @0 (bit_xor @1 (convert @2)))))
2951 (cmp (convert? addr@0) integer_zerop)
2952 (if (tree_single_nonzero_warnv_p (@0, NULL))
2953 { constant_boolean_node (cmp == NE_EXPR, type); })))
2955 /* If we have (A & C) == C where C is a power of 2, convert this into
2956 (A & C) != 0. Similarly for NE_EXPR. */
2960 (cmp (bit_and@2 @0 integer_pow2p@1) @1)
2961 (icmp @2 { build_zero_cst (TREE_TYPE (@0)); })))
2963 /* If we have (A & C) != 0 ? D : 0 where C and D are powers of 2,
2964 convert this into a shift followed by ANDing with D. */
2967 (ne (bit_and @0 integer_pow2p@1) integer_zerop)
2968 integer_pow2p@2 integer_zerop)
2970 int shift = wi::exact_log2 (@2) - wi::exact_log2 (@1);
2974 (lshift (convert @0) { build_int_cst (integer_type_node, shift); }) @2)
2976 (convert (rshift @0 { build_int_cst (integer_type_node, -shift); })) @2))))
2978 /* If we have (A & C) != 0 where C is the sign bit of A, convert
2979 this into A < 0. Similarly for (A & C) == 0 into A >= 0. */
2983 (cmp (bit_and (convert?@2 @0) integer_pow2p@1) integer_zerop)
2984 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2985 && (TYPE_PRECISION (TREE_TYPE (@0))
2986 == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (@0))))
2987 && element_precision (@2) >= element_precision (@0)
2988 && wi::only_sign_bit_p (@1, element_precision (@0)))
2989 (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
2990 (ncmp (convert:stype @0) { build_zero_cst (stype); })))))
2992 /* If we have A < 0 ? C : 0 where C is a power of 2, convert
2993 this into a right shift or sign extension followed by ANDing with C. */
2996 (lt @0 integer_zerop)
2997 integer_pow2p@1 integer_zerop)
2998 (if (!TYPE_UNSIGNED (TREE_TYPE (@0)))
3000 int shift = element_precision (@0) - wi::exact_log2 (@1) - 1;
3004 (convert (rshift @0 { build_int_cst (integer_type_node, shift); }))
3006 /* Otherwise ctype must be wider than TREE_TYPE (@0) and pure
3007 sign extension followed by AND with C will achieve the effect. */
3008 (bit_and (convert @0) @1)))))
3010 /* When the addresses are not directly of decls compare base and offset.
3011 This implements some remaining parts of fold_comparison address
3012 comparisons but still no complete part of it. Still it is good
3013 enough to make fold_stmt not regress when not dispatching to fold_binary. */
3014 (for cmp (simple_comparison)
3016 (cmp (convert1?@2 addr@0) (convert2? addr@1))
3019 HOST_WIDE_INT off0, off1;
3020 tree base0 = get_addr_base_and_unit_offset (TREE_OPERAND (@0, 0), &off0);
3021 tree base1 = get_addr_base_and_unit_offset (TREE_OPERAND (@1, 0), &off1);
3022 if (base0 && TREE_CODE (base0) == MEM_REF)
3024 off0 += mem_ref_offset (base0).to_short_addr ();
3025 base0 = TREE_OPERAND (base0, 0);
3027 if (base1 && TREE_CODE (base1) == MEM_REF)
3029 off1 += mem_ref_offset (base1).to_short_addr ();
3030 base1 = TREE_OPERAND (base1, 0);
3033 (if (base0 && base1)
3037 /* Punt in GENERIC on variables with value expressions;
3038 the value expressions might point to fields/elements
3039 of other vars etc. */
3041 && ((VAR_P (base0) && DECL_HAS_VALUE_EXPR_P (base0))
3042 || (VAR_P (base1) && DECL_HAS_VALUE_EXPR_P (base1))))
3044 else if (decl_in_symtab_p (base0)
3045 && decl_in_symtab_p (base1))
3046 equal = symtab_node::get_create (base0)
3047 ->equal_address_to (symtab_node::get_create (base1));
3048 else if ((DECL_P (base0)
3049 || TREE_CODE (base0) == SSA_NAME
3050 || TREE_CODE (base0) == STRING_CST)
3052 || TREE_CODE (base1) == SSA_NAME
3053 || TREE_CODE (base1) == STRING_CST))
3054 equal = (base0 == base1);
3057 && (cmp == EQ_EXPR || cmp == NE_EXPR
3058 /* If the offsets are equal we can ignore overflow. */
3060 || POINTER_TYPE_OVERFLOW_UNDEFINED
3061 /* Or if we compare using pointers to decls or strings. */
3062 || (POINTER_TYPE_P (TREE_TYPE (@2))
3063 && (DECL_P (base0) || TREE_CODE (base0) == STRING_CST))))
3065 (if (cmp == EQ_EXPR)
3066 { constant_boolean_node (off0 == off1, type); })
3067 (if (cmp == NE_EXPR)
3068 { constant_boolean_node (off0 != off1, type); })
3069 (if (cmp == LT_EXPR)
3070 { constant_boolean_node (off0 < off1, type); })
3071 (if (cmp == LE_EXPR)
3072 { constant_boolean_node (off0 <= off1, type); })
3073 (if (cmp == GE_EXPR)
3074 { constant_boolean_node (off0 >= off1, type); })
3075 (if (cmp == GT_EXPR)
3076 { constant_boolean_node (off0 > off1, type); }))
3078 && DECL_P (base0) && DECL_P (base1)
3079 /* If we compare this as integers require equal offset. */
3080 && (!INTEGRAL_TYPE_P (TREE_TYPE (@2))
3083 (if (cmp == EQ_EXPR)
3084 { constant_boolean_node (false, type); })
3085 (if (cmp == NE_EXPR)
3086 { constant_boolean_node (true, type); })))))))))
3088 /* Simplify pointer equality compares using PTA. */
3092 (if (POINTER_TYPE_P (TREE_TYPE (@0))
3093 && ptrs_compare_unequal (@0, @1))
3094 { neeq == EQ_EXPR ? boolean_false_node : boolean_true_node; })))
3096 /* PR70920: Transform (intptr_t)x eq/ne CST to x eq/ne (typeof x) CST.
3097 and (typeof ptr_cst) x eq/ne ptr_cst to x eq/ne (typeof x) CST.
3098 Disable the transform if either operand is pointer to function.
3099 This broke pr22051-2.c for arm where function pointer
3100 canonicalizaion is not wanted. */
3104 (cmp (convert @0) INTEGER_CST@1)
3105 (if ((POINTER_TYPE_P (TREE_TYPE (@0)) && !FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@0)))
3106 && INTEGRAL_TYPE_P (TREE_TYPE (@1)))
3107 || (INTEGRAL_TYPE_P (TREE_TYPE (@0)) && POINTER_TYPE_P (TREE_TYPE (@1))
3108 && !FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@1)))))
3109 (cmp @0 (convert @1)))))
3111 /* Non-equality compare simplifications from fold_binary */
3112 (for cmp (lt gt le ge)
3113 /* Comparisons with the highest or lowest possible integer of
3114 the specified precision will have known values. */
3116 (cmp (convert?@2 @0) INTEGER_CST@1)
3117 (if ((INTEGRAL_TYPE_P (TREE_TYPE (@1)) || POINTER_TYPE_P (TREE_TYPE (@1)))
3118 && tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@0)))
3121 tree arg1_type = TREE_TYPE (@1);
3122 unsigned int prec = TYPE_PRECISION (arg1_type);
3123 wide_int max = wi::max_value (arg1_type);
3124 wide_int signed_max = wi::max_value (prec, SIGNED);
3125 wide_int min = wi::min_value (arg1_type);
3128 (if (wi::eq_p (@1, max))
3130 (if (cmp == GT_EXPR)
3131 { constant_boolean_node (false, type); })
3132 (if (cmp == GE_EXPR)
3134 (if (cmp == LE_EXPR)
3135 { constant_boolean_node (true, type); })
3136 (if (cmp == LT_EXPR)
3138 (if (wi::eq_p (@1, min))
3140 (if (cmp == LT_EXPR)
3141 { constant_boolean_node (false, type); })
3142 (if (cmp == LE_EXPR)
3144 (if (cmp == GE_EXPR)
3145 { constant_boolean_node (true, type); })
3146 (if (cmp == GT_EXPR)
3148 (if (wi::eq_p (@1, max - 1))
3150 (if (cmp == GT_EXPR)
3151 (eq @2 { wide_int_to_tree (TREE_TYPE (@1), wi::add (@1, 1)); }))
3152 (if (cmp == LE_EXPR)
3153 (ne @2 { wide_int_to_tree (TREE_TYPE (@1), wi::add (@1, 1)); }))))
3154 (if (wi::eq_p (@1, min + 1))
3156 (if (cmp == GE_EXPR)
3157 (ne @2 { wide_int_to_tree (TREE_TYPE (@1), wi::sub (@1, 1)); }))
3158 (if (cmp == LT_EXPR)
3159 (eq @2 { wide_int_to_tree (TREE_TYPE (@1), wi::sub (@1, 1)); }))))
3160 (if (wi::eq_p (@1, signed_max)
3161 && TYPE_UNSIGNED (arg1_type)
3162 /* We will flip the signedness of the comparison operator
3163 associated with the mode of @1, so the sign bit is
3164 specified by this mode. Check that @1 is the signed
3165 max associated with this sign bit. */
3166 && prec == GET_MODE_PRECISION (TYPE_MODE (arg1_type))
3167 /* signed_type does not work on pointer types. */
3168 && INTEGRAL_TYPE_P (arg1_type))
3169 /* The following case also applies to X < signed_max+1
3170 and X >= signed_max+1 because previous transformations. */
3171 (if (cmp == LE_EXPR || cmp == GT_EXPR)
3172 (with { tree st = signed_type_for (arg1_type); }
3173 (if (cmp == LE_EXPR)
3174 (ge (convert:st @0) { build_zero_cst (st); })
3175 (lt (convert:st @0) { build_zero_cst (st); }))))))))))
3177 (for cmp (unordered ordered unlt unle ungt unge uneq ltgt)
3178 /* If the second operand is NaN, the result is constant. */
3181 (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
3182 && (cmp != LTGT_EXPR || ! flag_trapping_math))
3183 { constant_boolean_node (cmp == ORDERED_EXPR || cmp == LTGT_EXPR
3184 ? false : true, type); })))
3186 /* bool_var != 0 becomes bool_var. */
3188 (ne @0 integer_zerop)
3189 (if (TREE_CODE (TREE_TYPE (@0)) == BOOLEAN_TYPE
3190 && types_match (type, TREE_TYPE (@0)))
3192 /* bool_var == 1 becomes bool_var. */
3194 (eq @0 integer_onep)
3195 (if (TREE_CODE (TREE_TYPE (@0)) == BOOLEAN_TYPE
3196 && types_match (type, TREE_TYPE (@0)))
3199 bool_var == 0 becomes !bool_var or
3200 bool_var != 1 becomes !bool_var
3201 here because that only is good in assignment context as long
3202 as we require a tcc_comparison in GIMPLE_CONDs where we'd
3203 replace if (x == 0) with tem = ~x; if (tem != 0) which is
3204 clearly less optimal and which we'll transform again in forwprop. */
3206 /* When one argument is a constant, overflow detection can be simplified.
3207 Currently restricted to single use so as not to interfere too much with
3208 ADD_OVERFLOW detection in tree-ssa-math-opts.c.
3209 A + CST CMP A -> A CMP' CST' */
3210 (for cmp (lt le ge gt)
3213 (cmp:c (plus@2 @0 INTEGER_CST@1) @0)
3214 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
3215 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))
3218 (out @0 { wide_int_to_tree (TREE_TYPE (@0), wi::max_value
3219 (TYPE_PRECISION (TREE_TYPE (@0)), UNSIGNED) - @1); }))))
3221 /* To detect overflow in unsigned A - B, A < B is simpler than A - B > A.
3222 However, the detection logic for SUB_OVERFLOW in tree-ssa-math-opts.c
3223 expects the long form, so we restrict the transformation for now. */
3226 (cmp:c (minus@2 @0 @1) @0)
3227 (if (single_use (@2)
3228 && ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3229 && TYPE_UNSIGNED (TREE_TYPE (@0))
3230 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
3233 /* Testing for overflow is unnecessary if we already know the result. */
3238 (cmp:c (realpart (IFN_SUB_OVERFLOW@2 @0 @1)) @0)
3239 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
3240 && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
3241 (out (imagpart @2) { build_zero_cst (TREE_TYPE (@0)); }))))
3246 (cmp:c (realpart (IFN_ADD_OVERFLOW:c@2 @0 @1)) @0)
3247 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
3248 && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
3249 (out (imagpart @2) { build_zero_cst (TREE_TYPE (@0)); }))))
3251 /* For unsigned operands, -1 / B < A checks whether A * B would overflow.
3252 Simplify it to __builtin_mul_overflow (A, B, <unused>). */
3256 (cmp:c (trunc_div:s integer_all_onesp @1) @0)
3257 (if (TYPE_UNSIGNED (TREE_TYPE (@0)) && !VECTOR_TYPE_P (TREE_TYPE (@0)))
3258 (with { tree t = TREE_TYPE (@0), cpx = build_complex_type (t); }
3259 (out (imagpart (IFN_MUL_OVERFLOW:cpx @0 @1)) { build_zero_cst (t); })))))
3261 /* Simplification of math builtins. These rules must all be optimizations
3262 as well as IL simplifications. If there is a possibility that the new
3263 form could be a pessimization, the rule should go in the canonicalization
3264 section that follows this one.
3266 Rules can generally go in this section if they satisfy one of
3269 - the rule describes an identity
3271 - the rule replaces calls with something as simple as addition or
3274 - the rule contains unary calls only and simplifies the surrounding
3275 arithmetic. (The idea here is to exclude non-unary calls in which
3276 one operand is constant and in which the call is known to be cheap
3277 when the operand has that value.) */
3279 (if (flag_unsafe_math_optimizations)
3280 /* Simplify sqrt(x) * sqrt(x) -> x. */
3282 (mult (SQRT@1 @0) @1)
3283 (if (!HONOR_SNANS (type))
3286 /* Simplify sqrt(x) * sqrt(y) -> sqrt(x*y). */
3287 (for root (SQRT CBRT)
3289 (mult (root:s @0) (root:s @1))
3290 (root (mult @0 @1))))
3292 /* Simplify expN(x) * expN(y) -> expN(x+y). */
3293 (for exps (EXP EXP2 EXP10 POW10)
3295 (mult (exps:s @0) (exps:s @1))
3296 (exps (plus @0 @1))))
3298 /* Simplify a/root(b/c) into a*root(c/b). */
3299 (for root (SQRT CBRT)
3301 (rdiv @0 (root:s (rdiv:s @1 @2)))
3302 (mult @0 (root (rdiv @2 @1)))))
3304 /* Simplify x/expN(y) into x*expN(-y). */
3305 (for exps (EXP EXP2 EXP10 POW10)
3307 (rdiv @0 (exps:s @1))
3308 (mult @0 (exps (negate @1)))))
3310 (for logs (LOG LOG2 LOG10 LOG10)
3311 exps (EXP EXP2 EXP10 POW10)
3312 /* logN(expN(x)) -> x. */
3316 /* expN(logN(x)) -> x. */
3321 /* Optimize logN(func()) for various exponential functions. We
3322 want to determine the value "x" and the power "exponent" in
3323 order to transform logN(x**exponent) into exponent*logN(x). */
3324 (for logs (LOG LOG LOG LOG2 LOG2 LOG2 LOG10 LOG10)
3325 exps (EXP2 EXP10 POW10 EXP EXP10 POW10 EXP EXP2)
3328 (if (SCALAR_FLOAT_TYPE_P (type))
3334 /* Prepare to do logN(exp(exponent)) -> exponent*logN(e). */
3335 x = build_real_truncate (type, dconst_e ());
3338 /* Prepare to do logN(exp2(exponent)) -> exponent*logN(2). */
3339 x = build_real (type, dconst2);
3343 /* Prepare to do logN(exp10(exponent)) -> exponent*logN(10). */
3345 REAL_VALUE_TYPE dconst10;
3346 real_from_integer (&dconst10, VOIDmode, 10, SIGNED);
3347 x = build_real (type, dconst10);
3354 (mult (logs { x; }) @0)))))
3362 (if (SCALAR_FLOAT_TYPE_P (type))
3368 /* Prepare to do logN(sqrt(x)) -> 0.5*logN(x). */
3369 x = build_real (type, dconsthalf);
3372 /* Prepare to do logN(cbrt(x)) -> (1/3)*logN(x). */
3373 x = build_real_truncate (type, dconst_third ());
3379 (mult { x; } (logs @0))))))
3381 /* logN(pow(x,exponent)) -> exponent*logN(x). */
3382 (for logs (LOG LOG2 LOG10)
3386 (mult @1 (logs @0))))
3391 exps (EXP EXP2 EXP10 POW10)
3392 /* sqrt(expN(x)) -> expN(x*0.5). */
3395 (exps (mult @0 { build_real (type, dconsthalf); })))
3396 /* cbrt(expN(x)) -> expN(x/3). */
3399 (exps (mult @0 { build_real_truncate (type, dconst_third ()); })))
3400 /* pow(expN(x), y) -> expN(x*y). */
3403 (exps (mult @0 @1))))
3405 /* tan(atan(x)) -> x. */
3412 /* cabs(x+0i) or cabs(0+xi) -> abs(x). */
3414 (CABS (complex:C @0 real_zerop@1))
3417 /* trunc(trunc(x)) -> trunc(x), etc. */
3418 (for fns (TRUNC FLOOR CEIL ROUND NEARBYINT RINT)
3422 /* f(x) -> x if x is integer valued and f does nothing for such values. */
3423 (for fns (TRUNC FLOOR CEIL ROUND NEARBYINT RINT)
3425 (fns integer_valued_real_p@0)
3428 /* hypot(x,0) and hypot(0,x) -> abs(x). */
3430 (HYPOT:c @0 real_zerop@1)
3433 /* pow(1,x) -> 1. */
3435 (POW real_onep@0 @1)
3439 /* copysign(x,x) -> x. */
3444 /* copysign(x,y) -> fabs(x) if y is nonnegative. */
3445 (COPYSIGN @0 tree_expr_nonnegative_p@1)
3448 (for scale (LDEXP SCALBN SCALBLN)
3449 /* ldexp(0, x) -> 0. */
3451 (scale real_zerop@0 @1)
3453 /* ldexp(x, 0) -> x. */
3455 (scale @0 integer_zerop@1)
3457 /* ldexp(x, y) -> x if x is +-Inf or NaN. */
3459 (scale REAL_CST@0 @1)
3460 (if (!real_isfinite (TREE_REAL_CST_PTR (@0)))
3463 /* Canonicalization of sequences of math builtins. These rules represent
3464 IL simplifications but are not necessarily optimizations.
3466 The sincos pass is responsible for picking "optimal" implementations
3467 of math builtins, which may be more complicated and can sometimes go
3468 the other way, e.g. converting pow into a sequence of sqrts.
3469 We only want to do these canonicalizations before the pass has run. */
3471 (if (flag_unsafe_math_optimizations && canonicalize_math_p ())
3472 /* Simplify tan(x) * cos(x) -> sin(x). */
3474 (mult:c (TAN:s @0) (COS:s @0))
3477 /* Simplify x * pow(x,c) -> pow(x,c+1). */
3479 (mult:c @0 (POW:s @0 REAL_CST@1))
3480 (if (!TREE_OVERFLOW (@1))
3481 (POW @0 (plus @1 { build_one_cst (type); }))))
3483 /* Simplify sin(x) / cos(x) -> tan(x). */
3485 (rdiv (SIN:s @0) (COS:s @0))
3488 /* Simplify cos(x) / sin(x) -> 1 / tan(x). */
3490 (rdiv (COS:s @0) (SIN:s @0))
3491 (rdiv { build_one_cst (type); } (TAN @0)))
3493 /* Simplify sin(x) / tan(x) -> cos(x). */
3495 (rdiv (SIN:s @0) (TAN:s @0))
3496 (if (! HONOR_NANS (@0)
3497 && ! HONOR_INFINITIES (@0))
3500 /* Simplify tan(x) / sin(x) -> 1.0 / cos(x). */
3502 (rdiv (TAN:s @0) (SIN:s @0))
3503 (if (! HONOR_NANS (@0)
3504 && ! HONOR_INFINITIES (@0))
3505 (rdiv { build_one_cst (type); } (COS @0))))
3507 /* Simplify pow(x,y) * pow(x,z) -> pow(x,y+z). */
3509 (mult (POW:s @0 @1) (POW:s @0 @2))
3510 (POW @0 (plus @1 @2)))
3512 /* Simplify pow(x,y) * pow(z,y) -> pow(x*z,y). */
3514 (mult (POW:s @0 @1) (POW:s @2 @1))
3515 (POW (mult @0 @2) @1))
3517 /* Simplify powi(x,y) * powi(z,y) -> powi(x*z,y). */
3519 (mult (POWI:s @0 @1) (POWI:s @2 @1))
3520 (POWI (mult @0 @2) @1))
3522 /* Simplify pow(x,c) / x -> pow(x,c-1). */
3524 (rdiv (POW:s @0 REAL_CST@1) @0)
3525 (if (!TREE_OVERFLOW (@1))
3526 (POW @0 (minus @1 { build_one_cst (type); }))))
3528 /* Simplify x / pow (y,z) -> x * pow(y,-z). */
3530 (rdiv @0 (POW:s @1 @2))
3531 (mult @0 (POW @1 (negate @2))))
3536 /* sqrt(sqrt(x)) -> pow(x,1/4). */
3539 (pows @0 { build_real (type, dconst_quarter ()); }))
3540 /* sqrt(cbrt(x)) -> pow(x,1/6). */
3543 (pows @0 { build_real_truncate (type, dconst_sixth ()); }))
3544 /* cbrt(sqrt(x)) -> pow(x,1/6). */
3547 (pows @0 { build_real_truncate (type, dconst_sixth ()); }))
3548 /* cbrt(cbrt(x)) -> pow(x,1/9), iff x is nonnegative. */
3550 (cbrts (cbrts tree_expr_nonnegative_p@0))
3551 (pows @0 { build_real_truncate (type, dconst_ninth ()); }))
3552 /* sqrt(pow(x,y)) -> pow(|x|,y*0.5). */
3554 (sqrts (pows @0 @1))
3555 (pows (abs @0) (mult @1 { build_real (type, dconsthalf); })))
3556 /* cbrt(pow(x,y)) -> pow(x,y/3), iff x is nonnegative. */
3558 (cbrts (pows tree_expr_nonnegative_p@0 @1))
3559 (pows @0 (mult @1 { build_real_truncate (type, dconst_third ()); })))
3560 /* pow(sqrt(x),y) -> pow(x,y*0.5). */
3562 (pows (sqrts @0) @1)
3563 (pows @0 (mult @1 { build_real (type, dconsthalf); })))
3564 /* pow(cbrt(x),y) -> pow(x,y/3) iff x is nonnegative. */
3566 (pows (cbrts tree_expr_nonnegative_p@0) @1)
3567 (pows @0 (mult @1 { build_real_truncate (type, dconst_third ()); })))
3568 /* pow(pow(x,y),z) -> pow(x,y*z) iff x is nonnegative. */
3570 (pows (pows tree_expr_nonnegative_p@0 @1) @2)
3571 (pows @0 (mult @1 @2))))
3573 /* cabs(x+xi) -> fabs(x)*sqrt(2). */
3575 (CABS (complex @0 @0))
3576 (mult (abs @0) { build_real_truncate (type, dconst_sqrt2 ()); }))
3578 /* hypot(x,x) -> fabs(x)*sqrt(2). */
3581 (mult (abs @0) { build_real_truncate (type, dconst_sqrt2 ()); }))
3583 /* cexp(x+yi) -> exp(x)*cexpi(y). */
3588 (cexps compositional_complex@0)
3589 (if (targetm.libc_has_function (function_c99_math_complex))
3591 (mult (exps@1 (realpart @0)) (realpart (cexpis:type@2 (imagpart @0))))
3592 (mult @1 (imagpart @2)))))))
3594 (if (canonicalize_math_p ())
3595 /* floor(x) -> trunc(x) if x is nonnegative. */
3599 (floors tree_expr_nonnegative_p@0)
3602 (match double_value_p
3604 (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == double_type_node)))
3605 (for froms (BUILT_IN_TRUNCL
3617 /* truncl(extend(x)) -> extend(trunc(x)), etc., if x is a double. */
3618 (if (optimize && canonicalize_math_p ())
3620 (froms (convert double_value_p@0))
3621 (convert (tos @0)))))
3623 (match float_value_p
3625 (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == float_type_node)))
3626 (for froms (BUILT_IN_TRUNCL BUILT_IN_TRUNC
3627 BUILT_IN_FLOORL BUILT_IN_FLOOR
3628 BUILT_IN_CEILL BUILT_IN_CEIL
3629 BUILT_IN_ROUNDL BUILT_IN_ROUND
3630 BUILT_IN_NEARBYINTL BUILT_IN_NEARBYINT
3631 BUILT_IN_RINTL BUILT_IN_RINT)
3632 tos (BUILT_IN_TRUNCF BUILT_IN_TRUNCF
3633 BUILT_IN_FLOORF BUILT_IN_FLOORF
3634 BUILT_IN_CEILF BUILT_IN_CEILF
3635 BUILT_IN_ROUNDF BUILT_IN_ROUNDF
3636 BUILT_IN_NEARBYINTF BUILT_IN_NEARBYINTF
3637 BUILT_IN_RINTF BUILT_IN_RINTF)
3638 /* truncl(extend(x)) and trunc(extend(x)) -> extend(truncf(x)), etc.,
3640 (if (optimize && canonicalize_math_p ()
3641 && targetm.libc_has_function (function_c99_misc))
3643 (froms (convert float_value_p@0))
3644 (convert (tos @0)))))
3646 (for froms (XFLOORL XCEILL XROUNDL XRINTL)
3647 tos (XFLOOR XCEIL XROUND XRINT)
3648 /* llfloorl(extend(x)) -> llfloor(x), etc., if x is a double. */
3649 (if (optimize && canonicalize_math_p ())
3651 (froms (convert double_value_p@0))
3654 (for froms (XFLOORL XCEILL XROUNDL XRINTL
3655 XFLOOR XCEIL XROUND XRINT)
3656 tos (XFLOORF XCEILF XROUNDF XRINTF)
3657 /* llfloorl(extend(x)) and llfloor(extend(x)) -> llfloorf(x), etc.,
3659 (if (optimize && canonicalize_math_p ())
3661 (froms (convert float_value_p@0))
3664 (if (canonicalize_math_p ())
3665 /* xfloor(x) -> fix_trunc(x) if x is nonnegative. */
3666 (for floors (IFLOOR LFLOOR LLFLOOR)
3668 (floors tree_expr_nonnegative_p@0)
3671 (if (canonicalize_math_p ())
3672 /* xfloor(x) -> fix_trunc(x), etc., if x is integer valued. */
3673 (for fns (IFLOOR LFLOOR LLFLOOR
3675 IROUND LROUND LLROUND)
3677 (fns integer_valued_real_p@0)
3679 (if (!flag_errno_math)
3680 /* xrint(x) -> fix_trunc(x), etc., if x is integer valued. */
3681 (for rints (IRINT LRINT LLRINT)
3683 (rints integer_valued_real_p@0)
3686 (if (canonicalize_math_p ())
3687 (for ifn (IFLOOR ICEIL IROUND IRINT)
3688 lfn (LFLOOR LCEIL LROUND LRINT)
3689 llfn (LLFLOOR LLCEIL LLROUND LLRINT)
3690 /* Canonicalize iround (x) to lround (x) on ILP32 targets where
3691 sizeof (int) == sizeof (long). */
3692 (if (TYPE_PRECISION (integer_type_node)
3693 == TYPE_PRECISION (long_integer_type_node))
3696 (lfn:long_integer_type_node @0)))
3697 /* Canonicalize llround (x) to lround (x) on LP64 targets where
3698 sizeof (long long) == sizeof (long). */
3699 (if (TYPE_PRECISION (long_long_integer_type_node)
3700 == TYPE_PRECISION (long_integer_type_node))
3703 (lfn:long_integer_type_node @0)))))
3705 /* cproj(x) -> x if we're ignoring infinities. */
3708 (if (!HONOR_INFINITIES (type))
3711 /* If the real part is inf and the imag part is known to be
3712 nonnegative, return (inf + 0i). */
3714 (CPROJ (complex REAL_CST@0 tree_expr_nonnegative_p@1))
3715 (if (real_isinf (TREE_REAL_CST_PTR (@0)))
3716 { build_complex_inf (type, false); }))
3718 /* If the imag part is inf, return (inf+I*copysign(0,imag)). */
3720 (CPROJ (complex @0 REAL_CST@1))
3721 (if (real_isinf (TREE_REAL_CST_PTR (@1)))
3722 { build_complex_inf (type, TREE_REAL_CST_PTR (@1)->sign); }))
3728 (pows @0 REAL_CST@1)
3730 const REAL_VALUE_TYPE *value = TREE_REAL_CST_PTR (@1);
3731 REAL_VALUE_TYPE tmp;
3734 /* pow(x,0) -> 1. */
3735 (if (real_equal (value, &dconst0))
3736 { build_real (type, dconst1); })
3737 /* pow(x,1) -> x. */
3738 (if (real_equal (value, &dconst1))
3740 /* pow(x,-1) -> 1/x. */
3741 (if (real_equal (value, &dconstm1))
3742 (rdiv { build_real (type, dconst1); } @0))
3743 /* pow(x,0.5) -> sqrt(x). */
3744 (if (flag_unsafe_math_optimizations
3745 && canonicalize_math_p ()
3746 && real_equal (value, &dconsthalf))
3748 /* pow(x,1/3) -> cbrt(x). */
3749 (if (flag_unsafe_math_optimizations
3750 && canonicalize_math_p ()
3751 && (tmp = real_value_truncate (TYPE_MODE (type), dconst_third ()),
3752 real_equal (value, &tmp)))
3755 /* powi(1,x) -> 1. */
3757 (POWI real_onep@0 @1)
3761 (POWI @0 INTEGER_CST@1)
3763 /* powi(x,0) -> 1. */
3764 (if (wi::eq_p (@1, 0))
3765 { build_real (type, dconst1); })
3766 /* powi(x,1) -> x. */
3767 (if (wi::eq_p (@1, 1))
3769 /* powi(x,-1) -> 1/x. */
3770 (if (wi::eq_p (@1, -1))
3771 (rdiv { build_real (type, dconst1); } @0))))
3773 /* Narrowing of arithmetic and logical operations.
3775 These are conceptually similar to the transformations performed for
3776 the C/C++ front-ends by shorten_binary_op and shorten_compare. Long
3777 term we want to move all that code out of the front-ends into here. */
3779 /* If we have a narrowing conversion of an arithmetic operation where
3780 both operands are widening conversions from the same type as the outer
3781 narrowing conversion. Then convert the innermost operands to a suitable
3782 unsigned type (to avoid introducing undefined behavior), perform the
3783 operation and convert the result to the desired type. */
3784 (for op (plus minus)
3786 (convert (op:s (convert@2 @0) (convert?@3 @1)))
3787 (if (INTEGRAL_TYPE_P (type)
3788 /* We check for type compatibility between @0 and @1 below,
3789 so there's no need to check that @1/@3 are integral types. */
3790 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
3791 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
3792 /* The precision of the type of each operand must match the
3793 precision of the mode of each operand, similarly for the
3795 && (TYPE_PRECISION (TREE_TYPE (@0))
3796 == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (@0))))
3797 && (TYPE_PRECISION (TREE_TYPE (@1))
3798 == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (@1))))
3799 && TYPE_PRECISION (type) == GET_MODE_PRECISION (TYPE_MODE (type))
3800 /* The inner conversion must be a widening conversion. */
3801 && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
3802 && types_match (@0, type)
3803 && (types_match (@0, @1)
3804 /* Or the second operand is const integer or converted const
3805 integer from valueize. */
3806 || TREE_CODE (@1) == INTEGER_CST))
3807 (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
3808 (op @0 (convert @1))
3809 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
3810 (convert (op (convert:utype @0)
3811 (convert:utype @1))))))))
3813 /* This is another case of narrowing, specifically when there's an outer
3814 BIT_AND_EXPR which masks off bits outside the type of the innermost
3815 operands. Like the previous case we have to convert the operands
3816 to unsigned types to avoid introducing undefined behavior for the
3817 arithmetic operation. */
3818 (for op (minus plus)
3820 (bit_and (op:s (convert@2 @0) (convert@3 @1)) INTEGER_CST@4)
3821 (if (INTEGRAL_TYPE_P (type)
3822 /* We check for type compatibility between @0 and @1 below,
3823 so there's no need to check that @1/@3 are integral types. */
3824 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
3825 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
3826 /* The precision of the type of each operand must match the
3827 precision of the mode of each operand, similarly for the
3829 && (TYPE_PRECISION (TREE_TYPE (@0))
3830 == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (@0))))
3831 && (TYPE_PRECISION (TREE_TYPE (@1))
3832 == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (@1))))
3833 && TYPE_PRECISION (type) == GET_MODE_PRECISION (TYPE_MODE (type))
3834 /* The inner conversion must be a widening conversion. */
3835 && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
3836 && types_match (@0, @1)
3837 && (tree_int_cst_min_precision (@4, TYPE_SIGN (TREE_TYPE (@0)))
3838 <= TYPE_PRECISION (TREE_TYPE (@0)))
3839 && (wi::bit_and (@4, wi::mask (TYPE_PRECISION (TREE_TYPE (@0)),
3840 true, TYPE_PRECISION (type))) == 0))
3841 (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
3842 (with { tree ntype = TREE_TYPE (@0); }
3843 (convert (bit_and (op @0 @1) (convert:ntype @4))))
3844 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
3845 (convert (bit_and (op (convert:utype @0) (convert:utype @1))
3846 (convert:utype @4))))))))
3848 /* Transform (@0 < @1 and @0 < @2) to use min,
3849 (@0 > @1 and @0 > @2) to use max */
3850 (for op (lt le gt ge)
3851 ext (min min max max)
3853 (bit_and (op:cs @0 @1) (op:cs @0 @2))
3854 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
3855 && TREE_CODE (@0) != INTEGER_CST)
3856 (op @0 (ext @1 @2)))))
3859 /* signbit(x) -> 0 if x is nonnegative. */
3860 (SIGNBIT tree_expr_nonnegative_p@0)
3861 { integer_zero_node; })
3864 /* signbit(x) -> x<0 if x doesn't have signed zeros. */
3866 (if (!HONOR_SIGNED_ZEROS (@0))
3867 (convert (lt @0 { build_real (TREE_TYPE (@0), dconst0); }))))
3869 /* Transform comparisons of the form X +- C1 CMP C2 to X CMP C2 -+ C1. */
3871 (for op (plus minus)
3874 (cmp (op@3 @0 INTEGER_CST@1) INTEGER_CST@2)
3875 (if (!TREE_OVERFLOW (@1) && !TREE_OVERFLOW (@2)
3876 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0))
3877 && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@0))
3878 && !TYPE_SATURATING (TREE_TYPE (@0)))
3879 (with { tree res = int_const_binop (rop, @2, @1); }
3880 (if (TREE_OVERFLOW (res)
3881 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
3882 { constant_boolean_node (cmp == NE_EXPR, type); }
3883 (if (single_use (@3))
3884 (cmp @0 { res; }))))))))
3885 (for cmp (lt le gt ge)
3886 (for op (plus minus)
3889 (cmp (op@3 @0 INTEGER_CST@1) INTEGER_CST@2)
3890 (if (!TREE_OVERFLOW (@1) && !TREE_OVERFLOW (@2)
3891 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
3892 (with { tree res = int_const_binop (rop, @2, @1); }
3893 (if (TREE_OVERFLOW (res))
3895 fold_overflow_warning (("assuming signed overflow does not occur "
3896 "when simplifying conditional to constant"),
3897 WARN_STRICT_OVERFLOW_CONDITIONAL);
3898 bool less = cmp == LE_EXPR || cmp == LT_EXPR;
3899 /* wi::ges_p (@2, 0) should be sufficient for a signed type. */
3900 bool ovf_high = wi::lt_p (@1, 0, TYPE_SIGN (TREE_TYPE (@1)))
3901 != (op == MINUS_EXPR);
3902 constant_boolean_node (less == ovf_high, type);
3904 (if (single_use (@3))
3907 fold_overflow_warning (("assuming signed overflow does not occur "
3908 "when changing X +- C1 cmp C2 to "
3910 WARN_STRICT_OVERFLOW_COMPARISON);
3912 (cmp @0 { res; })))))))))
3914 /* Canonicalizations of BIT_FIELD_REFs. */
3917 (BIT_FIELD_REF @0 @1 @2)
3919 (if (TREE_CODE (TREE_TYPE (@0)) == COMPLEX_TYPE
3920 && tree_int_cst_equal (@1, TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))))
3922 (if (integer_zerop (@2))
3923 (view_convert (realpart @0)))
3924 (if (tree_int_cst_equal (@2, TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))))
3925 (view_convert (imagpart @0)))))
3926 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
3927 && INTEGRAL_TYPE_P (type)
3928 /* On GIMPLE this should only apply to register arguments. */
3929 && (! GIMPLE || is_gimple_reg (@0))
3930 /* A bit-field-ref that referenced the full argument can be stripped. */
3931 && ((compare_tree_int (@1, TYPE_PRECISION (TREE_TYPE (@0))) == 0
3932 && integer_zerop (@2))
3933 /* Low-parts can be reduced to integral conversions.
3934 ??? The following doesn't work for PDP endian. */
3935 || (BYTES_BIG_ENDIAN == WORDS_BIG_ENDIAN
3936 /* Don't even think about BITS_BIG_ENDIAN. */
3937 && TYPE_PRECISION (TREE_TYPE (@0)) % BITS_PER_UNIT == 0
3938 && TYPE_PRECISION (type) % BITS_PER_UNIT == 0
3939 && compare_tree_int (@2, (BYTES_BIG_ENDIAN
3940 ? (TYPE_PRECISION (TREE_TYPE (@0))
3941 - TYPE_PRECISION (type))
3945 /* Simplify vector extracts. */
3948 (BIT_FIELD_REF CONSTRUCTOR@0 @1 @2)
3949 (if (VECTOR_TYPE_P (TREE_TYPE (@0))
3950 && (types_match (type, TREE_TYPE (TREE_TYPE (@0)))
3951 || (VECTOR_TYPE_P (type)
3952 && types_match (TREE_TYPE (type), TREE_TYPE (TREE_TYPE (@0))))))
3955 tree ctor = (TREE_CODE (@0) == SSA_NAME
3956 ? gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0)) : @0);
3957 tree eltype = TREE_TYPE (TREE_TYPE (ctor));
3958 unsigned HOST_WIDE_INT width = tree_to_uhwi (TYPE_SIZE (eltype));
3959 unsigned HOST_WIDE_INT n = tree_to_uhwi (@1);
3960 unsigned HOST_WIDE_INT idx = tree_to_uhwi (@2);
3963 && (idx % width) == 0
3965 && ((idx + n) / width) <= TYPE_VECTOR_SUBPARTS (TREE_TYPE (ctor)))
3970 /* Constructor elements can be subvectors. */
3971 unsigned HOST_WIDE_INT k = 1;
3972 if (CONSTRUCTOR_NELTS (ctor) != 0)
3974 tree cons_elem = TREE_TYPE (CONSTRUCTOR_ELT (ctor, 0)->value);
3975 if (TREE_CODE (cons_elem) == VECTOR_TYPE)
3976 k = TYPE_VECTOR_SUBPARTS (cons_elem);
3980 /* We keep an exact subset of the constructor elements. */
3981 (if ((idx % k) == 0 && (n % k) == 0)
3982 (if (CONSTRUCTOR_NELTS (ctor) == 0)
3983 { build_constructor (type, NULL); }
3990 (if (idx < CONSTRUCTOR_NELTS (ctor))
3991 { CONSTRUCTOR_ELT (ctor, idx)->value; }
3992 { build_zero_cst (type); })
3994 vec<constructor_elt, va_gc> *vals;
3995 vec_alloc (vals, n);
3996 for (unsigned i = 0;
3997 i < n && idx + i < CONSTRUCTOR_NELTS (ctor); ++i)
3998 CONSTRUCTOR_APPEND_ELT (vals, NULL_TREE,
3999 CONSTRUCTOR_ELT (ctor, idx + i)->value);
4000 build_constructor (type, vals);
4002 /* The bitfield references a single constructor element. */
4003 (if (idx + n <= (idx / k + 1) * k)
4005 (if (CONSTRUCTOR_NELTS (ctor) <= idx / k)
4006 { build_zero_cst (type); })
4008 { CONSTRUCTOR_ELT (ctor, idx / k)->value; })
4009 (BIT_FIELD_REF { CONSTRUCTOR_ELT (ctor, idx / k)->value; }
4010 @1 { bitsize_int ((idx % k) * width); })))))))))