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 (for cmp (gt ge lt le)
159 outp (convert convert negate negate)
160 outn (negate negate convert convert)
161 /* Transform (X > 0.0 ? 1.0 : -1.0) into copysign(1, X). */
162 /* Transform (X >= 0.0 ? 1.0 : -1.0) into copysign(1, X). */
163 /* Transform (X < 0.0 ? 1.0 : -1.0) into copysign(1,-X). */
164 /* Transform (X <= 0.0 ? 1.0 : -1.0) into copysign(1,-X). */
166 (cond (cmp @0 real_zerop) real_onep@1 real_minus_onep)
167 (if (!HONOR_NANS (type) && !HONOR_SIGNED_ZEROS (type)
168 && types_match (type, TREE_TYPE (@0)))
170 (if (types_match (type, float_type_node))
171 (BUILT_IN_COPYSIGNF @1 (outp @0)))
172 (if (types_match (type, double_type_node))
173 (BUILT_IN_COPYSIGN @1 (outp @0)))
174 (if (types_match (type, long_double_type_node))
175 (BUILT_IN_COPYSIGNL @1 (outp @0))))))
176 /* Transform (X > 0.0 ? -1.0 : 1.0) into copysign(1,-X). */
177 /* Transform (X >= 0.0 ? -1.0 : 1.0) into copysign(1,-X). */
178 /* Transform (X < 0.0 ? -1.0 : 1.0) into copysign(1,X). */
179 /* Transform (X <= 0.0 ? -1.0 : 1.0) into copysign(1,X). */
181 (cond (cmp @0 real_zerop) real_minus_onep real_onep@1)
182 (if (!HONOR_NANS (type) && !HONOR_SIGNED_ZEROS (type)
183 && types_match (type, TREE_TYPE (@0)))
185 (if (types_match (type, float_type_node))
186 (BUILT_IN_COPYSIGNF @1 (outn @0)))
187 (if (types_match (type, double_type_node))
188 (BUILT_IN_COPYSIGN @1 (outn @0)))
189 (if (types_match (type, long_double_type_node))
190 (BUILT_IN_COPYSIGNL @1 (outn @0)))))))
192 /* Transform X * copysign (1.0, X) into abs(X). */
194 (mult:c @0 (COPYSIGN real_onep @0))
195 (if (!HONOR_NANS (type) && !HONOR_SIGNED_ZEROS (type))
198 /* Transform X * copysign (1.0, -X) into -abs(X). */
200 (mult:c @0 (COPYSIGN real_onep (negate @0)))
201 (if (!HONOR_NANS (type) && !HONOR_SIGNED_ZEROS (type))
204 /* Transform copysign (CST, X) into copysign (ABS(CST), X). */
206 (COPYSIGN REAL_CST@0 @1)
207 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@0)))
208 (COPYSIGN (negate @0) @1)))
210 /* X * 1, X / 1 -> X. */
211 (for op (mult trunc_div ceil_div floor_div round_div exact_div)
216 /* (A / (1 << B)) -> (A >> B).
217 Only for unsigned A. For signed A, this would not preserve rounding
219 For example: (-1 / ( 1 << B)) != -1 >> B. */
221 (trunc_div @0 (lshift integer_onep@1 @2))
222 (if ((TYPE_UNSIGNED (type) || tree_expr_nonnegative_p (@0))
223 && (!VECTOR_TYPE_P (type)
224 || target_supports_op_p (type, RSHIFT_EXPR, optab_vector)
225 || target_supports_op_p (type, RSHIFT_EXPR, optab_scalar)))
228 /* Preserve explicit divisions by 0: the C++ front-end wants to detect
229 undefined behavior in constexpr evaluation, and assuming that the division
230 traps enables better optimizations than these anyway. */
231 (for div (trunc_div ceil_div floor_div round_div exact_div)
232 /* 0 / X is always zero. */
234 (div integer_zerop@0 @1)
235 /* But not for 0 / 0 so that we can get the proper warnings and errors. */
236 (if (!integer_zerop (@1))
240 (div @0 integer_minus_onep@1)
241 (if (!TYPE_UNSIGNED (type))
246 /* But not for 0 / 0 so that we can get the proper warnings and errors.
247 And not for _Fract types where we can't build 1. */
248 (if (!integer_zerop (@0) && !ALL_FRACT_MODE_P (TYPE_MODE (type)))
249 { build_one_cst (type); }))
250 /* X / abs (X) is X < 0 ? -1 : 1. */
253 (if (INTEGRAL_TYPE_P (type)
254 && TYPE_OVERFLOW_UNDEFINED (type))
255 (cond (lt @0 { build_zero_cst (type); })
256 { build_minus_one_cst (type); } { build_one_cst (type); })))
259 (div:C @0 (negate @0))
260 (if ((INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
261 && TYPE_OVERFLOW_UNDEFINED (type))
262 { build_minus_one_cst (type); })))
264 /* For unsigned integral types, FLOOR_DIV_EXPR is the same as
265 TRUNC_DIV_EXPR. Rewrite into the latter in this case. */
268 (if ((INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
269 && TYPE_UNSIGNED (type))
272 /* Combine two successive divisions. Note that combining ceil_div
273 and floor_div is trickier and combining round_div even more so. */
274 (for div (trunc_div exact_div)
276 (div (div @0 INTEGER_CST@1) INTEGER_CST@2)
279 wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@2),
280 TYPE_SIGN (type), &overflow_p);
283 (div @0 { wide_int_to_tree (type, mul); })
284 (if (TYPE_UNSIGNED (type)
285 || mul != wi::min_value (TYPE_PRECISION (type), SIGNED))
286 { build_zero_cst (type); })))))
288 /* Combine successive multiplications. Similar to above, but handling
289 overflow is different. */
291 (mult (mult @0 INTEGER_CST@1) INTEGER_CST@2)
294 wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@2),
295 TYPE_SIGN (type), &overflow_p);
297 /* Skip folding on overflow: the only special case is @1 * @2 == -INT_MIN,
298 otherwise undefined overflow implies that @0 must be zero. */
299 (if (!overflow_p || TYPE_OVERFLOW_WRAPS (type))
300 (mult @0 { wide_int_to_tree (type, mul); }))))
302 /* Optimize A / A to 1.0 if we don't care about
303 NaNs or Infinities. */
306 (if (FLOAT_TYPE_P (type)
307 && ! HONOR_NANS (type)
308 && ! HONOR_INFINITIES (type))
309 { build_one_cst (type); }))
311 /* Optimize -A / A to -1.0 if we don't care about
312 NaNs or Infinities. */
314 (rdiv:C @0 (negate @0))
315 (if (FLOAT_TYPE_P (type)
316 && ! HONOR_NANS (type)
317 && ! HONOR_INFINITIES (type))
318 { build_minus_one_cst (type); }))
320 /* PR71078: x / abs(x) -> copysign (1.0, x) */
322 (rdiv:C (convert? @0) (convert? (abs @0)))
323 (if (SCALAR_FLOAT_TYPE_P (type)
324 && ! HONOR_NANS (type)
325 && ! HONOR_INFINITIES (type))
327 (if (types_match (type, float_type_node))
328 (BUILT_IN_COPYSIGNF { build_one_cst (type); } (convert @0)))
329 (if (types_match (type, double_type_node))
330 (BUILT_IN_COPYSIGN { build_one_cst (type); } (convert @0)))
331 (if (types_match (type, long_double_type_node))
332 (BUILT_IN_COPYSIGNL { build_one_cst (type); } (convert @0))))))
334 /* In IEEE floating point, x/1 is not equivalent to x for snans. */
337 (if (!HONOR_SNANS (type))
340 /* In IEEE floating point, x/-1 is not equivalent to -x for snans. */
342 (rdiv @0 real_minus_onep)
343 (if (!HONOR_SNANS (type))
346 (if (flag_reciprocal_math)
347 /* Convert (A/B)/C to A/(B*C) */
349 (rdiv (rdiv:s @0 @1) @2)
350 (rdiv @0 (mult @1 @2)))
352 /* Convert A/(B/C) to (A/B)*C */
354 (rdiv @0 (rdiv:s @1 @2))
355 (mult (rdiv @0 @1) @2)))
357 /* Optimize (X & (-A)) / A where A is a power of 2, to X >> log2(A) */
358 (for div (trunc_div ceil_div floor_div round_div exact_div)
360 (div (convert? (bit_and @0 INTEGER_CST@1)) INTEGER_CST@2)
361 (if (integer_pow2p (@2)
362 && tree_int_cst_sgn (@2) > 0
363 && tree_nop_conversion_p (type, TREE_TYPE (@0))
364 && wi::to_wide (@2) + wi::to_wide (@1) == 0)
366 { build_int_cst (integer_type_node,
367 wi::exact_log2 (wi::to_wide (@2))); }))))
369 /* If ARG1 is a constant, we can convert this to a multiply by the
370 reciprocal. This does not have the same rounding properties,
371 so only do this if -freciprocal-math. We can actually
372 always safely do it if ARG1 is a power of two, but it's hard to
373 tell if it is or not in a portable manner. */
374 (for cst (REAL_CST COMPLEX_CST VECTOR_CST)
378 (if (flag_reciprocal_math
381 { tree tem = const_binop (RDIV_EXPR, type, build_one_cst (type), @1); }
383 (mult @0 { tem; } )))
384 (if (cst != COMPLEX_CST)
385 (with { tree inverse = exact_inverse (type, @1); }
387 (mult @0 { inverse; } ))))))))
389 (for mod (ceil_mod floor_mod round_mod trunc_mod)
390 /* 0 % X is always zero. */
392 (mod integer_zerop@0 @1)
393 /* But not for 0 % 0 so that we can get the proper warnings and errors. */
394 (if (!integer_zerop (@1))
396 /* X % 1 is always zero. */
398 (mod @0 integer_onep)
399 { build_zero_cst (type); })
400 /* X % -1 is zero. */
402 (mod @0 integer_minus_onep@1)
403 (if (!TYPE_UNSIGNED (type))
404 { build_zero_cst (type); }))
408 /* But not for 0 % 0 so that we can get the proper warnings and errors. */
409 (if (!integer_zerop (@0))
410 { build_zero_cst (type); }))
411 /* (X % Y) % Y is just X % Y. */
413 (mod (mod@2 @0 @1) @1)
415 /* From extract_muldiv_1: (X * C1) % C2 is zero if C1 is a multiple of C2. */
417 (mod (mult @0 INTEGER_CST@1) INTEGER_CST@2)
418 (if (ANY_INTEGRAL_TYPE_P (type)
419 && TYPE_OVERFLOW_UNDEFINED (type)
420 && wi::multiple_of_p (wi::to_wide (@1), wi::to_wide (@2),
422 { build_zero_cst (type); })))
424 /* X % -C is the same as X % C. */
426 (trunc_mod @0 INTEGER_CST@1)
427 (if (TYPE_SIGN (type) == SIGNED
428 && !TREE_OVERFLOW (@1)
429 && wi::neg_p (wi::to_wide (@1))
430 && !TYPE_OVERFLOW_TRAPS (type)
431 /* Avoid this transformation if C is INT_MIN, i.e. C == -C. */
432 && !sign_bit_p (@1, @1))
433 (trunc_mod @0 (negate @1))))
435 /* X % -Y is the same as X % Y. */
437 (trunc_mod @0 (convert? (negate @1)))
438 (if (INTEGRAL_TYPE_P (type)
439 && !TYPE_UNSIGNED (type)
440 && !TYPE_OVERFLOW_TRAPS (type)
441 && tree_nop_conversion_p (type, TREE_TYPE (@1))
442 /* Avoid this transformation if X might be INT_MIN or
443 Y might be -1, because we would then change valid
444 INT_MIN % -(-1) into invalid INT_MIN % -1. */
445 && (expr_not_equal_to (@0, wi::to_wide (TYPE_MIN_VALUE (type)))
446 || expr_not_equal_to (@1, wi::minus_one (TYPE_PRECISION
448 (trunc_mod @0 (convert @1))))
450 /* X - (X / Y) * Y is the same as X % Y. */
452 (minus (convert1? @0) (convert2? (mult:c (trunc_div @@0 @@1) @1)))
453 (if (INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
454 (convert (trunc_mod @0 @1))))
456 /* Optimize TRUNC_MOD_EXPR by a power of two into a BIT_AND_EXPR,
457 i.e. "X % C" into "X & (C - 1)", if X and C are positive.
458 Also optimize A % (C << N) where C is a power of 2,
459 to A & ((C << N) - 1). */
460 (match (power_of_two_cand @1)
462 (match (power_of_two_cand @1)
463 (lshift INTEGER_CST@1 @2))
464 (for mod (trunc_mod floor_mod)
466 (mod @0 (convert?@3 (power_of_two_cand@1 @2)))
467 (if ((TYPE_UNSIGNED (type)
468 || tree_expr_nonnegative_p (@0))
469 && tree_nop_conversion_p (type, TREE_TYPE (@3))
470 && integer_pow2p (@2) && tree_int_cst_sgn (@2) > 0)
471 (bit_and @0 (convert (minus @1 { build_int_cst (TREE_TYPE (@1), 1); }))))))
473 /* Simplify (unsigned t * 2)/2 -> unsigned t & 0x7FFFFFFF. */
475 (trunc_div (mult @0 integer_pow2p@1) @1)
476 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
477 (bit_and @0 { wide_int_to_tree
478 (type, wi::mask (TYPE_PRECISION (type)
479 - wi::exact_log2 (wi::to_wide (@1)),
480 false, TYPE_PRECISION (type))); })))
482 /* Simplify (unsigned t / 2) * 2 -> unsigned t & ~1. */
484 (mult (trunc_div @0 integer_pow2p@1) @1)
485 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
486 (bit_and @0 (negate @1))))
488 /* Simplify (t * 2) / 2) -> t. */
489 (for div (trunc_div ceil_div floor_div round_div exact_div)
491 (div (mult @0 @1) @1)
492 (if (ANY_INTEGRAL_TYPE_P (type)
493 && TYPE_OVERFLOW_UNDEFINED (type))
497 /* Simplify cos(-x) and cos(|x|) -> cos(x). Similarly for cosh. */
502 /* Simplify pow(-x, y) and pow(|x|,y) -> pow(x,y) if y is an even integer. */
505 (pows (op @0) REAL_CST@1)
506 (with { HOST_WIDE_INT n; }
507 (if (real_isinteger (&TREE_REAL_CST (@1), &n) && (n & 1) == 0)
509 /* Likewise for powi. */
512 (pows (op @0) INTEGER_CST@1)
513 (if ((wi::to_wide (@1) & 1) == 0)
515 /* Strip negate and abs from both operands of hypot. */
523 /* copysign(-x, y) and copysign(abs(x), y) -> copysign(x, y). */
524 (for copysigns (COPYSIGN)
526 (copysigns (op @0) @1)
529 /* abs(x)*abs(x) -> x*x. Should be valid for all types. */
534 /* cos(copysign(x, y)) -> cos(x). Similarly for cosh. */
538 (coss (copysigns @0 @1))
541 /* pow(copysign(x, y), z) -> pow(x, z) if z is an even integer. */
545 (pows (copysigns @0 @2) REAL_CST@1)
546 (with { HOST_WIDE_INT n; }
547 (if (real_isinteger (&TREE_REAL_CST (@1), &n) && (n & 1) == 0)
549 /* Likewise for powi. */
553 (pows (copysigns @0 @2) INTEGER_CST@1)
554 (if ((wi::to_wide (@1) & 1) == 0)
559 /* hypot(copysign(x, y), z) -> hypot(x, z). */
561 (hypots (copysigns @0 @1) @2)
563 /* hypot(x, copysign(y, z)) -> hypot(x, y). */
565 (hypots @0 (copysigns @1 @2))
568 /* copysign(x, CST) -> [-]abs (x). */
569 (for copysigns (COPYSIGN)
571 (copysigns @0 REAL_CST@1)
572 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
576 /* copysign(copysign(x, y), z) -> copysign(x, z). */
577 (for copysigns (COPYSIGN)
579 (copysigns (copysigns @0 @1) @2)
582 /* copysign(x,y)*copysign(x,y) -> x*x. */
583 (for copysigns (COPYSIGN)
585 (mult (copysigns@2 @0 @1) @2)
588 /* ccos(-x) -> ccos(x). Similarly for ccosh. */
589 (for ccoss (CCOS CCOSH)
594 /* cabs(-x) and cos(conj(x)) -> cabs(x). */
595 (for ops (conj negate)
601 /* Fold (a * (1 << b)) into (a << b) */
603 (mult:c @0 (convert? (lshift integer_onep@1 @2)))
604 (if (! FLOAT_TYPE_P (type)
605 && tree_nop_conversion_p (type, TREE_TYPE (@1)))
608 /* Fold (C1/X)*C2 into (C1*C2)/X. */
610 (mult (rdiv@3 REAL_CST@0 @1) REAL_CST@2)
611 (if (flag_associative_math
614 { tree tem = const_binop (MULT_EXPR, type, @0, @2); }
616 (rdiv { tem; } @1)))))
618 /* Convert C1/(X*C2) into (C1/C2)/X */
620 (rdiv REAL_CST@0 (mult @1 REAL_CST@2))
621 (if (flag_reciprocal_math)
623 { tree tem = const_binop (RDIV_EXPR, type, @0, @2); }
625 (rdiv { tem; } @1)))))
627 /* Simplify ~X & X as zero. */
629 (bit_and:c (convert? @0) (convert? (bit_not @0)))
630 { build_zero_cst (type); })
632 /* PR71636: Transform x & ((1U << b) - 1) -> x & ~(~0U << b); */
634 (bit_and:c @0 (plus:s (lshift:s integer_onep @1) integer_minus_onep))
635 (if (TYPE_UNSIGNED (type))
636 (bit_and @0 (bit_not (lshift { build_all_ones_cst (type); } @1)))))
638 (for bitop (bit_and bit_ior)
640 /* PR35691: Transform
641 (x == 0 & y == 0) -> (x | typeof(x)(y)) == 0.
642 (x != 0 | y != 0) -> (x | typeof(x)(y)) != 0. */
644 (bitop (cmp @0 integer_zerop@2) (cmp @1 integer_zerop))
645 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
646 && INTEGRAL_TYPE_P (TREE_TYPE (@1))
647 && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
648 (cmp (bit_ior @0 (convert @1)) @2)))
650 (x == -1 & y == -1) -> (x & typeof(x)(y)) == -1.
651 (x != -1 | y != -1) -> (x & typeof(x)(y)) != -1. */
653 (bitop (cmp @0 integer_all_onesp@2) (cmp @1 integer_all_onesp))
654 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
655 && INTEGRAL_TYPE_P (TREE_TYPE (@1))
656 && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
657 (cmp (bit_and @0 (convert @1)) @2))))
659 /* Fold (A & ~B) - (A & B) into (A ^ B) - B. */
661 (minus (bit_and:cs @0 (bit_not @1)) (bit_and:cs @0 @1))
662 (minus (bit_xor @0 @1) @1))
664 (minus (bit_and:s @0 INTEGER_CST@2) (bit_and:s @0 INTEGER_CST@1))
665 (if (~wi::to_wide (@2) == wi::to_wide (@1))
666 (minus (bit_xor @0 @1) @1)))
668 /* Fold (A & B) - (A & ~B) into B - (A ^ B). */
670 (minus (bit_and:cs @0 @1) (bit_and:cs @0 (bit_not @1)))
671 (minus @1 (bit_xor @0 @1)))
673 /* Simplify (X & ~Y) |^+ (~X & Y) -> X ^ Y. */
674 (for op (bit_ior bit_xor plus)
676 (op (bit_and:c @0 (bit_not @1)) (bit_and:c (bit_not @0) @1))
679 (op:c (bit_and @0 INTEGER_CST@2) (bit_and (bit_not @0) INTEGER_CST@1))
680 (if (~wi::to_wide (@2) == wi::to_wide (@1))
683 /* PR53979: Transform ((a ^ b) | a) -> (a | b) */
685 (bit_ior:c (bit_xor:c @0 @1) @0)
688 /* Simplify (~X & Y) to X ^ Y if we know that (X & ~Y) is 0. */
691 (bit_and (bit_not SSA_NAME@0) INTEGER_CST@1)
692 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
693 && wi::bit_and_not (get_nonzero_bits (@0), wi::to_wide (@1)) == 0)
697 /* X % Y is smaller than Y. */
700 (cmp (trunc_mod @0 @1) @1)
701 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
702 { constant_boolean_node (cmp == LT_EXPR, type); })))
705 (cmp @1 (trunc_mod @0 @1))
706 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
707 { constant_boolean_node (cmp == GT_EXPR, type); })))
711 (bit_ior @0 integer_all_onesp@1)
716 (bit_ior @0 integer_zerop)
721 (bit_and @0 integer_zerop@1)
727 (for op (bit_ior bit_xor plus)
729 (op:c (convert? @0) (convert? (bit_not @0)))
730 (convert { build_all_ones_cst (TREE_TYPE (@0)); })))
735 { build_zero_cst (type); })
737 /* Canonicalize X ^ ~0 to ~X. */
739 (bit_xor @0 integer_all_onesp@1)
744 (bit_and @0 integer_all_onesp)
747 /* x & x -> x, x | x -> x */
748 (for bitop (bit_and bit_ior)
753 /* x & C -> x if we know that x & ~C == 0. */
756 (bit_and SSA_NAME@0 INTEGER_CST@1)
757 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
758 && wi::bit_and_not (get_nonzero_bits (@0), wi::to_wide (@1)) == 0)
762 /* x + (x & 1) -> (x + 1) & ~1 */
764 (plus:c @0 (bit_and:s @0 integer_onep@1))
765 (bit_and (plus @0 @1) (bit_not @1)))
767 /* x & ~(x & y) -> x & ~y */
768 /* x | ~(x | y) -> x | ~y */
769 (for bitop (bit_and bit_ior)
771 (bitop:c @0 (bit_not (bitop:cs @0 @1)))
772 (bitop @0 (bit_not @1))))
774 /* (x | y) & ~x -> y & ~x */
775 /* (x & y) | ~x -> y | ~x */
776 (for bitop (bit_and bit_ior)
777 rbitop (bit_ior bit_and)
779 (bitop:c (rbitop:c @0 @1) (bit_not@2 @0))
782 /* (x & y) ^ (x | y) -> x ^ y */
784 (bit_xor:c (bit_and @0 @1) (bit_ior @0 @1))
787 /* (x ^ y) ^ (x | y) -> x & y */
789 (bit_xor:c (bit_xor @0 @1) (bit_ior @0 @1))
792 /* (x & y) + (x ^ y) -> x | y */
793 /* (x & y) | (x ^ y) -> x | y */
794 /* (x & y) ^ (x ^ y) -> x | y */
795 (for op (plus bit_ior bit_xor)
797 (op:c (bit_and @0 @1) (bit_xor @0 @1))
800 /* (x & y) + (x | y) -> x + y */
802 (plus:c (bit_and @0 @1) (bit_ior @0 @1))
805 /* (x + y) - (x | y) -> x & y */
807 (minus (plus @0 @1) (bit_ior @0 @1))
808 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
809 && !TYPE_SATURATING (type))
812 /* (x + y) - (x & y) -> x | y */
814 (minus (plus @0 @1) (bit_and @0 @1))
815 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
816 && !TYPE_SATURATING (type))
819 /* (x | y) - (x ^ y) -> x & y */
821 (minus (bit_ior @0 @1) (bit_xor @0 @1))
824 /* (x | y) - (x & y) -> x ^ y */
826 (minus (bit_ior @0 @1) (bit_and @0 @1))
829 /* (x | y) & ~(x & y) -> x ^ y */
831 (bit_and:c (bit_ior @0 @1) (bit_not (bit_and @0 @1)))
834 /* (x | y) & (~x ^ y) -> x & y */
836 (bit_and:c (bit_ior:c @0 @1) (bit_xor:c @1 (bit_not @0)))
839 /* ~x & ~y -> ~(x | y)
840 ~x | ~y -> ~(x & y) */
841 (for op (bit_and bit_ior)
842 rop (bit_ior bit_and)
844 (op (convert1? (bit_not @0)) (convert2? (bit_not @1)))
845 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
846 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
847 (bit_not (rop (convert @0) (convert @1))))))
849 /* If we are XORing or adding two BIT_AND_EXPR's, both of which are and'ing
850 with a constant, and the two constants have no bits in common,
851 we should treat this as a BIT_IOR_EXPR since this may produce more
853 (for op (bit_xor plus)
855 (op (convert1? (bit_and@4 @0 INTEGER_CST@1))
856 (convert2? (bit_and@5 @2 INTEGER_CST@3)))
857 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
858 && tree_nop_conversion_p (type, TREE_TYPE (@2))
859 && (wi::to_wide (@1) & wi::to_wide (@3)) == 0)
860 (bit_ior (convert @4) (convert @5)))))
862 /* (X | Y) ^ X -> Y & ~ X*/
864 (bit_xor:c (convert1? (bit_ior:c @@0 @1)) (convert2? @0))
865 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
866 (convert (bit_and @1 (bit_not @0)))))
868 /* Convert ~X ^ ~Y to X ^ Y. */
870 (bit_xor (convert1? (bit_not @0)) (convert2? (bit_not @1)))
871 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
872 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
873 (bit_xor (convert @0) (convert @1))))
875 /* Convert ~X ^ C to X ^ ~C. */
877 (bit_xor (convert? (bit_not @0)) INTEGER_CST@1)
878 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
879 (bit_xor (convert @0) (bit_not @1))))
881 /* Fold (X & Y) ^ Y and (X ^ Y) & Y as ~X & Y. */
882 (for opo (bit_and bit_xor)
883 opi (bit_xor bit_and)
885 (opo:c (opi:c @0 @1) @1)
886 (bit_and (bit_not @0) @1)))
888 /* Given a bit-wise operation CODE applied to ARG0 and ARG1, see if both
889 operands are another bit-wise operation with a common input. If so,
890 distribute the bit operations to save an operation and possibly two if
891 constants are involved. For example, convert
892 (A | B) & (A | C) into A | (B & C)
893 Further simplification will occur if B and C are constants. */
894 (for op (bit_and bit_ior bit_xor)
895 rop (bit_ior bit_and bit_and)
897 (op (convert? (rop:c @@0 @1)) (convert? (rop:c @0 @2)))
898 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
899 && tree_nop_conversion_p (type, TREE_TYPE (@2)))
900 (rop (convert @0) (op (convert @1) (convert @2))))))
902 /* Some simple reassociation for bit operations, also handled in reassoc. */
903 /* (X & Y) & Y -> X & Y
904 (X | Y) | Y -> X | Y */
905 (for op (bit_and bit_ior)
907 (op:c (convert1?@2 (op:c @0 @@1)) (convert2? @1))
909 /* (X ^ Y) ^ Y -> X */
911 (bit_xor:c (convert1? (bit_xor:c @0 @@1)) (convert2? @1))
913 /* (X & Y) & (X & Z) -> (X & Y) & Z
914 (X | Y) | (X | Z) -> (X | Y) | Z */
915 (for op (bit_and bit_ior)
917 (op (convert1?@3 (op:c@4 @0 @1)) (convert2?@5 (op:c@6 @0 @2)))
918 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
919 && tree_nop_conversion_p (type, TREE_TYPE (@2)))
920 (if (single_use (@5) && single_use (@6))
922 (if (single_use (@3) && single_use (@4))
923 (op (convert @1) @5))))))
924 /* (X ^ Y) ^ (X ^ Z) -> Y ^ Z */
926 (bit_xor (convert1? (bit_xor:c @0 @1)) (convert2? (bit_xor:c @0 @2)))
927 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
928 && tree_nop_conversion_p (type, TREE_TYPE (@2)))
929 (bit_xor (convert @1) (convert @2))))
938 (abs tree_expr_nonnegative_p@0)
941 /* A few cases of fold-const.c negate_expr_p predicate. */
944 (if ((INTEGRAL_TYPE_P (type)
945 && TYPE_UNSIGNED (type))
946 || (!TYPE_OVERFLOW_SANITIZED (type)
947 && may_negate_without_overflow_p (t)))))
952 (if (!TYPE_OVERFLOW_SANITIZED (type))))
955 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (t)))))
956 /* VECTOR_CST handling of non-wrapping types would recurse in unsupported
960 (if (FLOAT_TYPE_P (TREE_TYPE (type)) || TYPE_OVERFLOW_WRAPS (type))))
962 /* (-A) * (-B) -> A * B */
964 (mult:c (convert1? (negate @0)) (convert2? negate_expr_p@1))
965 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
966 && tree_nop_conversion_p (type, TREE_TYPE (@1)))
967 (mult (convert @0) (convert (negate @1)))))
969 /* -(A + B) -> (-B) - A. */
971 (negate (plus:c @0 negate_expr_p@1))
972 (if (!HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type))
973 && !HONOR_SIGNED_ZEROS (element_mode (type)))
974 (minus (negate @1) @0)))
976 /* A - B -> A + (-B) if B is easily negatable. */
978 (minus @0 negate_expr_p@1)
979 (if (!FIXED_POINT_TYPE_P (type))
980 (plus @0 (negate @1))))
982 /* Try to fold (type) X op CST -> (type) (X op ((type-x) CST))
984 For bitwise binary operations apply operand conversions to the
985 binary operation result instead of to the operands. This allows
986 to combine successive conversions and bitwise binary operations.
987 We combine the above two cases by using a conditional convert. */
988 (for bitop (bit_and bit_ior bit_xor)
990 (bitop (convert @0) (convert? @1))
991 (if (((TREE_CODE (@1) == INTEGER_CST
992 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
993 && int_fits_type_p (@1, TREE_TYPE (@0)))
994 || types_match (@0, @1))
995 /* ??? This transform conflicts with fold-const.c doing
996 Convert (T)(x & c) into (T)x & (T)c, if c is an integer
997 constants (if x has signed type, the sign bit cannot be set
998 in c). This folds extension into the BIT_AND_EXPR.
999 Restrict it to GIMPLE to avoid endless recursions. */
1000 && (bitop != BIT_AND_EXPR || GIMPLE)
1001 && (/* That's a good idea if the conversion widens the operand, thus
1002 after hoisting the conversion the operation will be narrower. */
1003 TYPE_PRECISION (TREE_TYPE (@0)) < TYPE_PRECISION (type)
1004 /* It's also a good idea if the conversion is to a non-integer
1006 || GET_MODE_CLASS (TYPE_MODE (type)) != MODE_INT
1007 /* Or if the precision of TO is not the same as the precision
1009 || !type_has_mode_precision_p (type)))
1010 (convert (bitop @0 (convert @1))))))
1012 (for bitop (bit_and bit_ior)
1013 rbitop (bit_ior bit_and)
1014 /* (x | y) & x -> x */
1015 /* (x & y) | x -> x */
1017 (bitop:c (rbitop:c @0 @1) @0)
1019 /* (~x | y) & x -> x & y */
1020 /* (~x & y) | x -> x | y */
1022 (bitop:c (rbitop:c (bit_not @0) @1) @0)
1025 /* (x | CST1) & CST2 -> (x & CST2) | (CST1 & CST2) */
1027 (bit_and (bit_ior @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
1028 (bit_ior (bit_and @0 @2) (bit_and @1 @2)))
1030 /* Combine successive equal operations with constants. */
1031 (for bitop (bit_and bit_ior bit_xor)
1033 (bitop (bitop @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
1034 (bitop @0 (bitop @1 @2))))
1036 /* Try simple folding for X op !X, and X op X with the help
1037 of the truth_valued_p and logical_inverted_value predicates. */
1038 (match truth_valued_p
1040 (if (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1)))
1041 (for op (tcc_comparison truth_and truth_andif truth_or truth_orif truth_xor)
1042 (match truth_valued_p
1044 (match truth_valued_p
1047 (match (logical_inverted_value @0)
1049 (match (logical_inverted_value @0)
1050 (bit_not truth_valued_p@0))
1051 (match (logical_inverted_value @0)
1052 (eq @0 integer_zerop))
1053 (match (logical_inverted_value @0)
1054 (ne truth_valued_p@0 integer_truep))
1055 (match (logical_inverted_value @0)
1056 (bit_xor truth_valued_p@0 integer_truep))
1060 (bit_and:c @0 (logical_inverted_value @0))
1061 { build_zero_cst (type); })
1062 /* X | !X and X ^ !X -> 1, , if X is truth-valued. */
1063 (for op (bit_ior bit_xor)
1065 (op:c truth_valued_p@0 (logical_inverted_value @0))
1066 { constant_boolean_node (true, type); }))
1067 /* X ==/!= !X is false/true. */
1070 (op:c truth_valued_p@0 (logical_inverted_value @0))
1071 { constant_boolean_node (op == NE_EXPR ? true : false, type); }))
1075 (bit_not (bit_not @0))
1078 /* Convert ~ (-A) to A - 1. */
1080 (bit_not (convert? (negate @0)))
1081 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1082 || !TYPE_UNSIGNED (TREE_TYPE (@0)))
1083 (convert (minus @0 { build_each_one_cst (TREE_TYPE (@0)); }))))
1085 /* Convert ~ (A - 1) or ~ (A + -1) to -A. */
1087 (bit_not (convert? (minus @0 integer_each_onep)))
1088 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1089 || !TYPE_UNSIGNED (TREE_TYPE (@0)))
1090 (convert (negate @0))))
1092 (bit_not (convert? (plus @0 integer_all_onesp)))
1093 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1094 || !TYPE_UNSIGNED (TREE_TYPE (@0)))
1095 (convert (negate @0))))
1097 /* Part of convert ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify. */
1099 (bit_not (convert? (bit_xor @0 INTEGER_CST@1)))
1100 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1101 (convert (bit_xor @0 (bit_not @1)))))
1103 (bit_not (convert? (bit_xor:c (bit_not @0) @1)))
1104 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1105 (convert (bit_xor @0 @1))))
1107 /* (x & ~m) | (y & m) -> ((x ^ y) & m) ^ x */
1109 (bit_ior:c (bit_and:cs @0 (bit_not @2)) (bit_and:cs @1 @2))
1110 (bit_xor (bit_and (bit_xor @0 @1) @2) @0))
1112 /* Fold A - (A & B) into ~B & A. */
1114 (minus (convert1? @0) (convert2?:s (bit_and:cs @@0 @1)))
1115 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
1116 && tree_nop_conversion_p (type, TREE_TYPE (@1)))
1117 (convert (bit_and (bit_not @1) @0))))
1119 /* (m1 CMP m2) * d -> (m1 CMP m2) ? d : 0 */
1120 (for cmp (gt lt ge le)
1122 (mult (convert (cmp @0 @1)) @2)
1123 (cond (cmp @0 @1) @2 { build_zero_cst (type); })))
1125 /* For integral types with undefined overflow and C != 0 fold
1126 x * C EQ/NE y * C into x EQ/NE y. */
1129 (cmp (mult:c @0 @1) (mult:c @2 @1))
1130 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
1131 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1132 && tree_expr_nonzero_p (@1))
1135 /* For integral types with wrapping overflow and C odd fold
1136 x * C EQ/NE y * C into x EQ/NE y. */
1139 (cmp (mult @0 INTEGER_CST@1) (mult @2 @1))
1140 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
1141 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))
1142 && (TREE_INT_CST_LOW (@1) & 1) != 0)
1145 /* For integral types with undefined overflow and C != 0 fold
1146 x * C RELOP y * C into:
1148 x RELOP y for nonnegative C
1149 y RELOP x for negative C */
1150 (for cmp (lt gt le ge)
1152 (cmp (mult:c @0 @1) (mult:c @2 @1))
1153 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
1154 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1155 (if (tree_expr_nonnegative_p (@1) && tree_expr_nonzero_p (@1))
1157 (if (TREE_CODE (@1) == INTEGER_CST
1158 && wi::neg_p (wi::to_wide (@1), TYPE_SIGN (TREE_TYPE (@1))))
1161 /* (X - 1U) <= INT_MAX-1U into (int) X > 0. */
1165 (cmp (plus @0 integer_minus_onep@1) INTEGER_CST@2)
1166 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1167 && TYPE_UNSIGNED (TREE_TYPE (@0))
1168 && TYPE_PRECISION (TREE_TYPE (@0)) > 1
1169 && (wi::to_wide (@2)
1170 == wi::max_value (TYPE_PRECISION (TREE_TYPE (@0)), SIGNED) - 1))
1171 (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
1172 (icmp (convert:stype @0) { build_int_cst (stype, 0); })))))
1174 /* X / 4 < Y / 4 iff X < Y when the division is known to be exact. */
1175 (for cmp (simple_comparison)
1177 (cmp (exact_div @0 INTEGER_CST@2) (exact_div @1 @2))
1178 (if (wi::gt_p (wi::to_wide (@2), 0, TYPE_SIGN (TREE_TYPE (@2))))
1181 /* X / C1 op C2 into a simple range test. */
1182 (for cmp (simple_comparison)
1184 (cmp (trunc_div:s @0 INTEGER_CST@1) INTEGER_CST@2)
1185 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1186 && integer_nonzerop (@1)
1187 && !TREE_OVERFLOW (@1)
1188 && !TREE_OVERFLOW (@2))
1189 (with { tree lo, hi; bool neg_overflow;
1190 enum tree_code code = fold_div_compare (cmp, @1, @2, &lo, &hi,
1193 (if (code == LT_EXPR || code == GE_EXPR)
1194 (if (TREE_OVERFLOW (lo))
1195 { build_int_cst (type, (code == LT_EXPR) ^ neg_overflow); }
1196 (if (code == LT_EXPR)
1199 (if (code == LE_EXPR || code == GT_EXPR)
1200 (if (TREE_OVERFLOW (hi))
1201 { build_int_cst (type, (code == LE_EXPR) ^ neg_overflow); }
1202 (if (code == LE_EXPR)
1206 { build_int_cst (type, code == NE_EXPR); })
1207 (if (code == EQ_EXPR && !hi)
1209 (if (code == EQ_EXPR && !lo)
1211 (if (code == NE_EXPR && !hi)
1213 (if (code == NE_EXPR && !lo)
1216 { build_range_check (UNKNOWN_LOCATION, type, @0, code == EQ_EXPR,
1220 tree etype = range_check_type (TREE_TYPE (@0));
1223 if (! TYPE_UNSIGNED (etype))
1224 etype = unsigned_type_for (etype);
1225 hi = fold_convert (etype, hi);
1226 lo = fold_convert (etype, lo);
1227 hi = const_binop (MINUS_EXPR, etype, hi, lo);
1230 (if (etype && hi && !TREE_OVERFLOW (hi))
1231 (if (code == EQ_EXPR)
1232 (le (minus (convert:etype @0) { lo; }) { hi; })
1233 (gt (minus (convert:etype @0) { lo; }) { hi; })))))))))
1235 /* X + Z < Y + Z is the same as X < Y when there is no overflow. */
1236 (for op (lt le ge gt)
1238 (op (plus:c @0 @2) (plus:c @1 @2))
1239 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1240 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1242 /* For equality and subtraction, this is also true with wrapping overflow. */
1243 (for op (eq ne minus)
1245 (op (plus:c @0 @2) (plus:c @1 @2))
1246 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1247 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1248 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
1251 /* X - Z < Y - Z is the same as X < Y when there is no overflow. */
1252 (for op (lt le ge gt)
1254 (op (minus @0 @2) (minus @1 @2))
1255 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1256 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1258 /* For equality and subtraction, this is also true with wrapping overflow. */
1259 (for op (eq ne minus)
1261 (op (minus @0 @2) (minus @1 @2))
1262 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1263 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1264 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
1267 /* Z - X < Z - Y is the same as Y < X when there is no overflow. */
1268 (for op (lt le ge gt)
1270 (op (minus @2 @0) (minus @2 @1))
1271 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1272 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1274 /* For equality and subtraction, this is also true with wrapping overflow. */
1275 (for op (eq ne minus)
1277 (op (minus @2 @0) (minus @2 @1))
1278 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1279 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1280 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
1283 /* X + Y < Y is the same as X < 0 when there is no overflow. */
1284 (for op (lt le gt ge)
1286 (op:c (plus:c@2 @0 @1) @1)
1287 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1288 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1289 && (CONSTANT_CLASS_P (@0) || single_use (@2)))
1290 (op @0 { build_zero_cst (TREE_TYPE (@0)); }))))
1291 /* For equality, this is also true with wrapping overflow. */
1294 (op:c (nop_convert@3 (plus:c@2 @0 (convert1? @1))) (convert2? @1))
1295 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1296 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1297 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
1298 && (CONSTANT_CLASS_P (@0) || (single_use (@2) && single_use (@3)))
1299 && tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@2))
1300 && tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@1)))
1301 (op @0 { build_zero_cst (TREE_TYPE (@0)); })))
1303 (op:c (nop_convert@3 (pointer_plus@2 (convert1? @0) @1)) (convert2? @0))
1304 (if (tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@0))
1305 && tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0))
1306 && (CONSTANT_CLASS_P (@1) || (single_use (@2) && single_use (@3))))
1307 (op @1 { build_zero_cst (TREE_TYPE (@1)); }))))
1309 /* X - Y < X is the same as Y > 0 when there is no overflow.
1310 For equality, this is also true with wrapping overflow. */
1311 (for op (simple_comparison)
1313 (op:c @0 (minus@2 @0 @1))
1314 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1315 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1316 || ((op == EQ_EXPR || op == NE_EXPR)
1317 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
1318 && (CONSTANT_CLASS_P (@1) || single_use (@2)))
1319 (op @1 { build_zero_cst (TREE_TYPE (@1)); }))))
1322 * (X / Y) == 0 -> X < Y if X, Y are unsigned.
1323 * (X / Y) != 0 -> X >= Y, if X, Y are unsigned.
1328 (cmp (trunc_div @0 @1) integer_zerop)
1329 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
1330 && (VECTOR_TYPE_P (type) || !VECTOR_TYPE_P (TREE_TYPE (@0))))
1333 /* X == C - X can never be true if C is odd. */
1336 (cmp:c (convert? @0) (convert1? (minus INTEGER_CST@1 (convert2? @0))))
1337 (if (TREE_INT_CST_LOW (@1) & 1)
1338 { constant_boolean_node (cmp == NE_EXPR, type); })))
1340 /* Arguments on which one can call get_nonzero_bits to get the bits
1342 (match with_possible_nonzero_bits
1344 (match with_possible_nonzero_bits
1346 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))))
1347 /* Slightly extended version, do not make it recursive to keep it cheap. */
1348 (match (with_possible_nonzero_bits2 @0)
1349 with_possible_nonzero_bits@0)
1350 (match (with_possible_nonzero_bits2 @0)
1351 (bit_and:c with_possible_nonzero_bits@0 @2))
1353 /* Same for bits that are known to be set, but we do not have
1354 an equivalent to get_nonzero_bits yet. */
1355 (match (with_certain_nonzero_bits2 @0)
1357 (match (with_certain_nonzero_bits2 @0)
1358 (bit_ior @1 INTEGER_CST@0))
1360 /* X == C (or X & Z == Y | C) is impossible if ~nonzero(X) & C != 0. */
1363 (cmp:c (with_possible_nonzero_bits2 @0) (with_certain_nonzero_bits2 @1))
1364 (if (wi::bit_and_not (wi::to_wide (@1), get_nonzero_bits (@0)) != 0)
1365 { constant_boolean_node (cmp == NE_EXPR, type); })))
1367 /* ((X inner_op C0) outer_op C1)
1368 With X being a tree where value_range has reasoned certain bits to always be
1369 zero throughout its computed value range,
1370 inner_op = {|,^}, outer_op = {|,^} and inner_op != outer_op
1371 where zero_mask has 1's for all bits that are sure to be 0 in
1373 if (inner_op == '^') C0 &= ~C1;
1374 if ((C0 & ~zero_mask) == 0) then emit (X outer_op (C0 outer_op C1)
1375 if ((C1 & ~zero_mask) == 0) then emit (X inner_op (C0 outer_op C1)
1377 (for inner_op (bit_ior bit_xor)
1378 outer_op (bit_xor bit_ior)
1381 (inner_op:s @2 INTEGER_CST@0) INTEGER_CST@1)
1385 wide_int zero_mask_not;
1389 if (TREE_CODE (@2) == SSA_NAME)
1390 zero_mask_not = get_nonzero_bits (@2);
1394 if (inner_op == BIT_XOR_EXPR)
1396 C0 = wi::bit_and_not (wi::to_wide (@0), wi::to_wide (@1));
1397 cst_emit = C0 | wi::to_wide (@1);
1401 C0 = wi::to_wide (@0);
1402 cst_emit = C0 ^ wi::to_wide (@1);
1405 (if (!fail && (C0 & zero_mask_not) == 0)
1406 (outer_op @2 { wide_int_to_tree (type, cst_emit); })
1407 (if (!fail && (wi::to_wide (@1) & zero_mask_not) == 0)
1408 (inner_op @2 { wide_int_to_tree (type, cst_emit); }))))))
1410 /* Associate (p +p off1) +p off2 as (p +p (off1 + off2)). */
1412 (pointer_plus (pointer_plus:s @0 @1) @3)
1413 (pointer_plus @0 (plus @1 @3)))
1419 tem4 = (unsigned long) tem3;
1424 (pointer_plus @0 (convert?@2 (minus@3 (convert @1) (convert @0))))
1425 /* Conditionally look through a sign-changing conversion. */
1426 (if (TYPE_PRECISION (TREE_TYPE (@2)) == TYPE_PRECISION (TREE_TYPE (@3))
1427 && ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@1)))
1428 || (GENERIC && type == TREE_TYPE (@1))))
1432 tem = (sizetype) ptr;
1436 and produce the simpler and easier to analyze with respect to alignment
1437 ... = ptr & ~algn; */
1439 (pointer_plus @0 (negate (bit_and (convert @0) INTEGER_CST@1)))
1440 (with { tree algn = wide_int_to_tree (TREE_TYPE (@0), ~wi::to_wide (@1)); }
1441 (bit_and @0 { algn; })))
1443 /* Try folding difference of addresses. */
1445 (minus (convert ADDR_EXPR@0) (convert @1))
1446 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1447 (with { HOST_WIDE_INT diff; }
1448 (if (ptr_difference_const (@0, @1, &diff))
1449 { build_int_cst_type (type, diff); }))))
1451 (minus (convert @0) (convert ADDR_EXPR@1))
1452 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1453 (with { HOST_WIDE_INT diff; }
1454 (if (ptr_difference_const (@0, @1, &diff))
1455 { build_int_cst_type (type, diff); }))))
1457 /* If arg0 is derived from the address of an object or function, we may
1458 be able to fold this expression using the object or function's
1461 (bit_and (convert? @0) INTEGER_CST@1)
1462 (if (POINTER_TYPE_P (TREE_TYPE (@0))
1463 && tree_nop_conversion_p (type, TREE_TYPE (@0)))
1467 unsigned HOST_WIDE_INT bitpos;
1468 get_pointer_alignment_1 (@0, &align, &bitpos);
1470 (if (wi::ltu_p (wi::to_wide (@1), align / BITS_PER_UNIT))
1471 { wide_int_to_tree (type, (wi::to_wide (@1)
1472 & (bitpos / BITS_PER_UNIT))); }))))
1475 /* We can't reassociate at all for saturating types. */
1476 (if (!TYPE_SATURATING (type))
1478 /* Contract negates. */
1479 /* A + (-B) -> A - B */
1481 (plus:c @0 (convert? (negate @1)))
1482 /* Apply STRIP_NOPS on the negate. */
1483 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1484 && !TYPE_OVERFLOW_SANITIZED (type))
1488 if (INTEGRAL_TYPE_P (type)
1489 && TYPE_OVERFLOW_WRAPS (type) != TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
1490 t1 = TYPE_OVERFLOW_WRAPS (type) ? type : TREE_TYPE (@1);
1492 (convert (minus (convert:t1 @0) (convert:t1 @1))))))
1493 /* A - (-B) -> A + B */
1495 (minus @0 (convert? (negate @1)))
1496 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1497 && !TYPE_OVERFLOW_SANITIZED (type))
1501 if (INTEGRAL_TYPE_P (type)
1502 && TYPE_OVERFLOW_WRAPS (type) != TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
1503 t1 = TYPE_OVERFLOW_WRAPS (type) ? type : TREE_TYPE (@1);
1505 (convert (plus (convert:t1 @0) (convert:t1 @1))))))
1508 (negate (convert? (negate @1)))
1509 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1510 && !TYPE_OVERFLOW_SANITIZED (type))
1513 /* We can't reassociate floating-point unless -fassociative-math
1514 or fixed-point plus or minus because of saturation to +-Inf. */
1515 (if ((!FLOAT_TYPE_P (type) || flag_associative_math)
1516 && !FIXED_POINT_TYPE_P (type))
1518 /* Match patterns that allow contracting a plus-minus pair
1519 irrespective of overflow issues. */
1520 /* (A +- B) - A -> +- B */
1521 /* (A +- B) -+ B -> A */
1522 /* A - (A +- B) -> -+ B */
1523 /* A +- (B -+ A) -> +- B */
1525 (minus (plus:c @0 @1) @0)
1528 (minus (minus @0 @1) @0)
1531 (plus:c (minus @0 @1) @1)
1534 (minus @0 (plus:c @0 @1))
1537 (minus @0 (minus @0 @1))
1539 /* (A +- B) + (C - A) -> C +- B */
1540 /* (A + B) - (A - C) -> B + C */
1541 /* More cases are handled with comparisons. */
1543 (plus:c (plus:c @0 @1) (minus @2 @0))
1546 (plus:c (minus @0 @1) (minus @2 @0))
1549 (minus (plus:c @0 @1) (minus @0 @2))
1552 /* (A +- CST1) +- CST2 -> A + CST3
1553 Use view_convert because it is safe for vectors and equivalent for
1555 (for outer_op (plus minus)
1556 (for inner_op (plus minus)
1557 neg_inner_op (minus plus)
1559 (outer_op (nop_convert (inner_op @0 CONSTANT_CLASS_P@1))
1561 /* If one of the types wraps, use that one. */
1562 (if (!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_WRAPS (type))
1563 (if (outer_op == PLUS_EXPR)
1564 (plus (view_convert @0) (inner_op @2 (view_convert @1)))
1565 (minus (view_convert @0) (neg_inner_op @2 (view_convert @1))))
1566 (if (!ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1567 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
1568 (if (outer_op == PLUS_EXPR)
1569 (view_convert (plus @0 (inner_op (view_convert @2) @1)))
1570 (view_convert (minus @0 (neg_inner_op (view_convert @2) @1))))
1571 /* If the constant operation overflows we cannot do the transform
1572 directly as we would introduce undefined overflow, for example
1573 with (a - 1) + INT_MIN. */
1574 (if (types_match (type, @0))
1575 (with { tree cst = const_binop (outer_op == inner_op
1576 ? PLUS_EXPR : MINUS_EXPR,
1578 (if (cst && !TREE_OVERFLOW (cst))
1579 (inner_op @0 { cst; } )
1580 /* X+INT_MAX+1 is X-INT_MIN. */
1581 (if (INTEGRAL_TYPE_P (type) && cst
1582 && wi::to_wide (cst) == wi::min_value (type))
1583 (neg_inner_op @0 { wide_int_to_tree (type, wi::to_wide (cst)); })
1584 /* Last resort, use some unsigned type. */
1585 (with { tree utype = unsigned_type_for (type); }
1586 (view_convert (inner_op
1587 (view_convert:utype @0)
1589 { drop_tree_overflow (cst); })))))))))))))
1591 /* (CST1 - A) +- CST2 -> CST3 - A */
1592 (for outer_op (plus minus)
1594 (outer_op (minus CONSTANT_CLASS_P@1 @0) CONSTANT_CLASS_P@2)
1595 (with { tree cst = const_binop (outer_op, type, @1, @2); }
1596 (if (cst && !TREE_OVERFLOW (cst))
1597 (minus { cst; } @0)))))
1599 /* CST1 - (CST2 - A) -> CST3 + A */
1601 (minus CONSTANT_CLASS_P@1 (minus CONSTANT_CLASS_P@2 @0))
1602 (with { tree cst = const_binop (MINUS_EXPR, type, @1, @2); }
1603 (if (cst && !TREE_OVERFLOW (cst))
1604 (plus { cst; } @0))))
1608 (plus:c (bit_not @0) @0)
1609 (if (!TYPE_OVERFLOW_TRAPS (type))
1610 { build_all_ones_cst (type); }))
1614 (plus (convert? (bit_not @0)) integer_each_onep)
1615 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1616 (negate (convert @0))))
1620 (minus (convert? (negate @0)) integer_each_onep)
1621 (if (!TYPE_OVERFLOW_TRAPS (type)
1622 && tree_nop_conversion_p (type, TREE_TYPE (@0)))
1623 (bit_not (convert @0))))
1627 (minus integer_all_onesp @0)
1630 /* (T)(P + A) - (T)P -> (T) A */
1631 (for add (plus pointer_plus)
1633 (minus (convert (add @@0 @1))
1635 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
1636 /* For integer types, if A has a smaller type
1637 than T the result depends on the possible
1639 E.g. T=size_t, A=(unsigned)429497295, P>0.
1640 However, if an overflow in P + A would cause
1641 undefined behavior, we can assume that there
1643 || (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1644 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1645 /* For pointer types, if the conversion of A to the
1646 final type requires a sign- or zero-extension,
1647 then we have to punt - it is not defined which
1649 || (POINTER_TYPE_P (TREE_TYPE (@0))
1650 && TREE_CODE (@1) == INTEGER_CST
1651 && tree_int_cst_sign_bit (@1) == 0))
1654 /* (T)P - (T)(P + A) -> -(T) A */
1655 (for add (plus pointer_plus)
1658 (convert (add @@0 @1)))
1659 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
1660 /* For integer types, if A has a smaller type
1661 than T the result depends on the possible
1663 E.g. T=size_t, A=(unsigned)429497295, P>0.
1664 However, if an overflow in P + A would cause
1665 undefined behavior, we can assume that there
1667 || (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1668 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1669 /* For pointer types, if the conversion of A to the
1670 final type requires a sign- or zero-extension,
1671 then we have to punt - it is not defined which
1673 || (POINTER_TYPE_P (TREE_TYPE (@0))
1674 && TREE_CODE (@1) == INTEGER_CST
1675 && tree_int_cst_sign_bit (@1) == 0))
1676 (negate (convert @1)))))
1678 /* (T)(P + A) - (T)(P + B) -> (T)A - (T)B */
1679 (for add (plus pointer_plus)
1681 (minus (convert (add @@0 @1))
1682 (convert (add @0 @2)))
1683 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
1684 /* For integer types, if A has a smaller type
1685 than T the result depends on the possible
1687 E.g. T=size_t, A=(unsigned)429497295, P>0.
1688 However, if an overflow in P + A would cause
1689 undefined behavior, we can assume that there
1691 || (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1692 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1693 /* For pointer types, if the conversion of A to the
1694 final type requires a sign- or zero-extension,
1695 then we have to punt - it is not defined which
1697 || (POINTER_TYPE_P (TREE_TYPE (@0))
1698 && TREE_CODE (@1) == INTEGER_CST
1699 && tree_int_cst_sign_bit (@1) == 0
1700 && TREE_CODE (@2) == INTEGER_CST
1701 && tree_int_cst_sign_bit (@2) == 0))
1702 (minus (convert @1) (convert @2)))))))
1705 /* Simplifications of MIN_EXPR, MAX_EXPR, fmin() and fmax(). */
1707 (for minmax (min max FMIN FMAX)
1711 /* min(max(x,y),y) -> y. */
1713 (min:c (max:c @0 @1) @1)
1715 /* max(min(x,y),y) -> y. */
1717 (max:c (min:c @0 @1) @1)
1719 /* max(a,-a) -> abs(a). */
1721 (max:c @0 (negate @0))
1722 (if (TREE_CODE (type) != COMPLEX_TYPE
1723 && (! ANY_INTEGRAL_TYPE_P (type)
1724 || TYPE_OVERFLOW_UNDEFINED (type)))
1726 /* min(a,-a) -> -abs(a). */
1728 (min:c @0 (negate @0))
1729 (if (TREE_CODE (type) != COMPLEX_TYPE
1730 && (! ANY_INTEGRAL_TYPE_P (type)
1731 || TYPE_OVERFLOW_UNDEFINED (type)))
1736 (if (INTEGRAL_TYPE_P (type)
1737 && TYPE_MIN_VALUE (type)
1738 && operand_equal_p (@1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
1740 (if (INTEGRAL_TYPE_P (type)
1741 && TYPE_MAX_VALUE (type)
1742 && operand_equal_p (@1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
1747 (if (INTEGRAL_TYPE_P (type)
1748 && TYPE_MAX_VALUE (type)
1749 && operand_equal_p (@1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
1751 (if (INTEGRAL_TYPE_P (type)
1752 && TYPE_MIN_VALUE (type)
1753 && operand_equal_p (@1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
1756 /* max (a, a + CST) -> a + CST where CST is positive. */
1757 /* max (a, a + CST) -> a where CST is negative. */
1759 (max:c @0 (plus@2 @0 INTEGER_CST@1))
1760 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1761 (if (tree_int_cst_sgn (@1) > 0)
1765 /* min (a, a + CST) -> a where CST is positive. */
1766 /* min (a, a + CST) -> a + CST where CST is negative. */
1768 (min:c @0 (plus@2 @0 INTEGER_CST@1))
1769 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1770 (if (tree_int_cst_sgn (@1) > 0)
1774 /* (convert (minmax ((convert (x) c)))) -> minmax (x c) if x is promoted
1775 and the outer convert demotes the expression back to x's type. */
1776 (for minmax (min max)
1778 (convert (minmax@0 (convert @1) INTEGER_CST@2))
1779 (if (INTEGRAL_TYPE_P (type)
1780 && types_match (@1, type) && int_fits_type_p (@2, type)
1781 && TYPE_SIGN (TREE_TYPE (@0)) == TYPE_SIGN (type)
1782 && TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type))
1783 (minmax @1 (convert @2)))))
1785 (for minmax (FMIN FMAX)
1786 /* If either argument is NaN, return the other one. Avoid the
1787 transformation if we get (and honor) a signalling NaN. */
1789 (minmax:c @0 REAL_CST@1)
1790 (if (real_isnan (TREE_REAL_CST_PTR (@1))
1791 && (!HONOR_SNANS (@1) || !TREE_REAL_CST (@1).signalling))
1793 /* Convert fmin/fmax to MIN_EXPR/MAX_EXPR. C99 requires these
1794 functions to return the numeric arg if the other one is NaN.
1795 MIN and MAX don't honor that, so only transform if -ffinite-math-only
1796 is set. C99 doesn't require -0.0 to be handled, so we don't have to
1797 worry about it either. */
1798 (if (flag_finite_math_only)
1805 /* min (-A, -B) -> -max (A, B) */
1806 (for minmax (min max FMIN FMAX)
1807 maxmin (max min FMAX FMIN)
1809 (minmax (negate:s@2 @0) (negate:s@3 @1))
1810 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
1811 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1812 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
1813 (negate (maxmin @0 @1)))))
1814 /* MIN (~X, ~Y) -> ~MAX (X, Y)
1815 MAX (~X, ~Y) -> ~MIN (X, Y) */
1816 (for minmax (min max)
1819 (minmax (bit_not:s@2 @0) (bit_not:s@3 @1))
1820 (bit_not (maxmin @0 @1))))
1822 /* MIN (X, Y) == X -> X <= Y */
1823 (for minmax (min min max max)
1827 (cmp:c (minmax:c @0 @1) @0)
1828 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0)))
1830 /* MIN (X, 5) == 0 -> X == 0
1831 MIN (X, 5) == 7 -> false */
1834 (cmp (min @0 INTEGER_CST@1) INTEGER_CST@2)
1835 (if (wi::lt_p (wi::to_wide (@1), wi::to_wide (@2),
1836 TYPE_SIGN (TREE_TYPE (@0))))
1837 { constant_boolean_node (cmp == NE_EXPR, type); }
1838 (if (wi::gt_p (wi::to_wide (@1), wi::to_wide (@2),
1839 TYPE_SIGN (TREE_TYPE (@0))))
1843 (cmp (max @0 INTEGER_CST@1) INTEGER_CST@2)
1844 (if (wi::gt_p (wi::to_wide (@1), wi::to_wide (@2),
1845 TYPE_SIGN (TREE_TYPE (@0))))
1846 { constant_boolean_node (cmp == NE_EXPR, type); }
1847 (if (wi::lt_p (wi::to_wide (@1), wi::to_wide (@2),
1848 TYPE_SIGN (TREE_TYPE (@0))))
1850 /* MIN (X, C1) < C2 -> X < C2 || C1 < C2 */
1851 (for minmax (min min max max min min max max )
1852 cmp (lt le gt ge gt ge lt le )
1853 comb (bit_ior bit_ior bit_ior bit_ior bit_and bit_and bit_and bit_and)
1855 (cmp (minmax @0 INTEGER_CST@1) INTEGER_CST@2)
1856 (comb (cmp @0 @2) (cmp @1 @2))))
1858 /* Simplifications of shift and rotates. */
1860 (for rotate (lrotate rrotate)
1862 (rotate integer_all_onesp@0 @1)
1865 /* Optimize -1 >> x for arithmetic right shifts. */
1867 (rshift integer_all_onesp@0 @1)
1868 (if (!TYPE_UNSIGNED (type)
1869 && tree_expr_nonnegative_p (@1))
1872 /* Optimize (x >> c) << c into x & (-1<<c). */
1874 (lshift (rshift @0 INTEGER_CST@1) @1)
1875 (if (wi::ltu_p (wi::to_wide (@1), element_precision (type)))
1876 (bit_and @0 (lshift { build_minus_one_cst (type); } @1))))
1878 /* Optimize (x << c) >> c into x & ((unsigned)-1 >> c) for unsigned
1881 (rshift (lshift @0 INTEGER_CST@1) @1)
1882 (if (TYPE_UNSIGNED (type)
1883 && (wi::ltu_p (wi::to_wide (@1), element_precision (type))))
1884 (bit_and @0 (rshift { build_minus_one_cst (type); } @1))))
1886 (for shiftrotate (lrotate rrotate lshift rshift)
1888 (shiftrotate @0 integer_zerop)
1891 (shiftrotate integer_zerop@0 @1)
1893 /* Prefer vector1 << scalar to vector1 << vector2
1894 if vector2 is uniform. */
1895 (for vec (VECTOR_CST CONSTRUCTOR)
1897 (shiftrotate @0 vec@1)
1898 (with { tree tem = uniform_vector_p (@1); }
1900 (shiftrotate @0 { tem; }))))))
1902 /* Simplify X << Y where Y's low width bits are 0 to X, as only valid
1903 Y is 0. Similarly for X >> Y. */
1905 (for shift (lshift rshift)
1907 (shift @0 SSA_NAME@1)
1908 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1)))
1910 int width = ceil_log2 (element_precision (TREE_TYPE (@0)));
1911 int prec = TYPE_PRECISION (TREE_TYPE (@1));
1913 (if ((get_nonzero_bits (@1) & wi::mask (width, false, prec)) == 0)
1917 /* Rewrite an LROTATE_EXPR by a constant into an
1918 RROTATE_EXPR by a new constant. */
1920 (lrotate @0 INTEGER_CST@1)
1921 (rrotate @0 { const_binop (MINUS_EXPR, TREE_TYPE (@1),
1922 build_int_cst (TREE_TYPE (@1),
1923 element_precision (type)), @1); }))
1925 /* Turn (a OP c1) OP c2 into a OP (c1+c2). */
1926 (for op (lrotate rrotate rshift lshift)
1928 (op (op @0 INTEGER_CST@1) INTEGER_CST@2)
1929 (with { unsigned int prec = element_precision (type); }
1930 (if (wi::ge_p (wi::to_wide (@1), 0, TYPE_SIGN (TREE_TYPE (@1)))
1931 && wi::lt_p (wi::to_wide (@1), prec, TYPE_SIGN (TREE_TYPE (@1)))
1932 && wi::ge_p (wi::to_wide (@2), 0, TYPE_SIGN (TREE_TYPE (@2)))
1933 && wi::lt_p (wi::to_wide (@2), prec, TYPE_SIGN (TREE_TYPE (@2))))
1934 (with { unsigned int low = (tree_to_uhwi (@1)
1935 + tree_to_uhwi (@2)); }
1936 /* Deal with a OP (c1 + c2) being undefined but (a OP c1) OP c2
1937 being well defined. */
1939 (if (op == LROTATE_EXPR || op == RROTATE_EXPR)
1940 (op @0 { build_int_cst (TREE_TYPE (@1), low % prec); })
1941 (if (TYPE_UNSIGNED (type) || op == LSHIFT_EXPR)
1942 { build_zero_cst (type); }
1943 (op @0 { build_int_cst (TREE_TYPE (@1), prec - 1); })))
1944 (op @0 { build_int_cst (TREE_TYPE (@1), low); })))))))
1947 /* ((1 << A) & 1) != 0 -> A == 0
1948 ((1 << A) & 1) == 0 -> A != 0 */
1952 (cmp (bit_and (lshift integer_onep @0) integer_onep) integer_zerop)
1953 (icmp @0 { build_zero_cst (TREE_TYPE (@0)); })))
1955 /* (CST1 << A) == CST2 -> A == ctz (CST2) - ctz (CST1)
1956 (CST1 << A) != CST2 -> A != ctz (CST2) - ctz (CST1)
1960 (cmp (lshift INTEGER_CST@0 @1) INTEGER_CST@2)
1961 (with { int cand = wi::ctz (wi::to_wide (@2)) - wi::ctz (wi::to_wide (@0)); }
1963 || (!integer_zerop (@2)
1964 && wi::lshift (wi::to_wide (@0), cand) != wi::to_wide (@2)))
1965 { constant_boolean_node (cmp == NE_EXPR, type); }
1966 (if (!integer_zerop (@2)
1967 && wi::lshift (wi::to_wide (@0), cand) == wi::to_wide (@2))
1968 (cmp @1 { build_int_cst (TREE_TYPE (@1), cand); }))))))
1970 /* Fold (X << C1) & C2 into (X << C1) & (C2 | ((1 << C1) - 1))
1971 (X >> C1) & C2 into (X >> C1) & (C2 | ~((type) -1 >> C1))
1972 if the new mask might be further optimized. */
1973 (for shift (lshift rshift)
1975 (bit_and (convert?:s@4 (shift:s@5 (convert1?@3 @0) INTEGER_CST@1))
1977 (if (tree_nop_conversion_p (TREE_TYPE (@4), TREE_TYPE (@5))
1978 && TYPE_PRECISION (type) <= HOST_BITS_PER_WIDE_INT
1979 && tree_fits_uhwi_p (@1)
1980 && tree_to_uhwi (@1) > 0
1981 && tree_to_uhwi (@1) < TYPE_PRECISION (type))
1984 unsigned int shiftc = tree_to_uhwi (@1);
1985 unsigned HOST_WIDE_INT mask = TREE_INT_CST_LOW (@2);
1986 unsigned HOST_WIDE_INT newmask, zerobits = 0;
1987 tree shift_type = TREE_TYPE (@3);
1990 if (shift == LSHIFT_EXPR)
1991 zerobits = ((HOST_WIDE_INT_1U << shiftc) - 1);
1992 else if (shift == RSHIFT_EXPR
1993 && type_has_mode_precision_p (shift_type))
1995 prec = TYPE_PRECISION (TREE_TYPE (@3));
1997 /* See if more bits can be proven as zero because of
2000 && TYPE_UNSIGNED (TREE_TYPE (@0)))
2002 tree inner_type = TREE_TYPE (@0);
2003 if (type_has_mode_precision_p (inner_type)
2004 && TYPE_PRECISION (inner_type) < prec)
2006 prec = TYPE_PRECISION (inner_type);
2007 /* See if we can shorten the right shift. */
2009 shift_type = inner_type;
2010 /* Otherwise X >> C1 is all zeros, so we'll optimize
2011 it into (X, 0) later on by making sure zerobits
2015 zerobits = HOST_WIDE_INT_M1U;
2018 zerobits >>= HOST_BITS_PER_WIDE_INT - shiftc;
2019 zerobits <<= prec - shiftc;
2021 /* For arithmetic shift if sign bit could be set, zerobits
2022 can contain actually sign bits, so no transformation is
2023 possible, unless MASK masks them all away. In that
2024 case the shift needs to be converted into logical shift. */
2025 if (!TYPE_UNSIGNED (TREE_TYPE (@3))
2026 && prec == TYPE_PRECISION (TREE_TYPE (@3)))
2028 if ((mask & zerobits) == 0)
2029 shift_type = unsigned_type_for (TREE_TYPE (@3));
2035 /* ((X << 16) & 0xff00) is (X, 0). */
2036 (if ((mask & zerobits) == mask)
2037 { build_int_cst (type, 0); }
2038 (with { newmask = mask | zerobits; }
2039 (if (newmask != mask && (newmask & (newmask + 1)) == 0)
2042 /* Only do the transformation if NEWMASK is some integer
2044 for (prec = BITS_PER_UNIT;
2045 prec < HOST_BITS_PER_WIDE_INT; prec <<= 1)
2046 if (newmask == (HOST_WIDE_INT_1U << prec) - 1)
2049 (if (prec < HOST_BITS_PER_WIDE_INT
2050 || newmask == HOST_WIDE_INT_M1U)
2052 { tree newmaskt = build_int_cst_type (TREE_TYPE (@2), newmask); }
2053 (if (!tree_int_cst_equal (newmaskt, @2))
2054 (if (shift_type != TREE_TYPE (@3))
2055 (bit_and (convert (shift:shift_type (convert @3) @1)) { newmaskt; })
2056 (bit_and @4 { newmaskt; })))))))))))))
2058 /* Fold (X {&,^,|} C2) << C1 into (X << C1) {&,^,|} (C2 << C1)
2059 (X {&,^,|} C2) >> C1 into (X >> C1) & (C2 >> C1). */
2060 (for shift (lshift rshift)
2061 (for bit_op (bit_and bit_xor bit_ior)
2063 (shift (convert?:s (bit_op:s @0 INTEGER_CST@2)) INTEGER_CST@1)
2064 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2065 (with { tree mask = int_const_binop (shift, fold_convert (type, @2), @1); }
2066 (bit_op (shift (convert @0) @1) { mask; }))))))
2068 /* ~(~X >> Y) -> X >> Y (for arithmetic shift). */
2070 (bit_not (convert1?:s (rshift:s (convert2?@0 (bit_not @1)) @2)))
2071 (if (!TYPE_UNSIGNED (TREE_TYPE (@0))
2072 && (element_precision (TREE_TYPE (@0))
2073 <= element_precision (TREE_TYPE (@1))
2074 || !TYPE_UNSIGNED (TREE_TYPE (@1))))
2076 { tree shift_type = TREE_TYPE (@0); }
2077 (convert (rshift (convert:shift_type @1) @2)))))
2079 /* ~(~X >>r Y) -> X >>r Y
2080 ~(~X <<r Y) -> X <<r Y */
2081 (for rotate (lrotate rrotate)
2083 (bit_not (convert1?:s (rotate:s (convert2?@0 (bit_not @1)) @2)))
2084 (if ((element_precision (TREE_TYPE (@0))
2085 <= element_precision (TREE_TYPE (@1))
2086 || !TYPE_UNSIGNED (TREE_TYPE (@1)))
2087 && (element_precision (type) <= element_precision (TREE_TYPE (@0))
2088 || !TYPE_UNSIGNED (TREE_TYPE (@0))))
2090 { tree rotate_type = TREE_TYPE (@0); }
2091 (convert (rotate (convert:rotate_type @1) @2))))))
2093 /* Simplifications of conversions. */
2095 /* Basic strip-useless-type-conversions / strip_nops. */
2096 (for cvt (convert view_convert float fix_trunc)
2099 (if ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@0)))
2100 || (GENERIC && type == TREE_TYPE (@0)))
2103 /* Contract view-conversions. */
2105 (view_convert (view_convert @0))
2108 /* For integral conversions with the same precision or pointer
2109 conversions use a NOP_EXPR instead. */
2112 (if ((INTEGRAL_TYPE_P (type) || POINTER_TYPE_P (type))
2113 && (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
2114 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (@0)))
2117 /* Strip inner integral conversions that do not change precision or size, or
2118 zero-extend while keeping the same size (for bool-to-char). */
2120 (view_convert (convert@0 @1))
2121 (if ((INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
2122 && (INTEGRAL_TYPE_P (TREE_TYPE (@1)) || POINTER_TYPE_P (TREE_TYPE (@1)))
2123 && TYPE_SIZE (TREE_TYPE (@0)) == TYPE_SIZE (TREE_TYPE (@1))
2124 && (TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1))
2125 || (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (TREE_TYPE (@1))
2126 && TYPE_UNSIGNED (TREE_TYPE (@1)))))
2129 /* Re-association barriers around constants and other re-association
2130 barriers can be removed. */
2132 (paren CONSTANT_CLASS_P@0)
2135 (paren (paren@1 @0))
2138 /* Handle cases of two conversions in a row. */
2139 (for ocvt (convert float fix_trunc)
2140 (for icvt (convert float)
2145 tree inside_type = TREE_TYPE (@0);
2146 tree inter_type = TREE_TYPE (@1);
2147 int inside_int = INTEGRAL_TYPE_P (inside_type);
2148 int inside_ptr = POINTER_TYPE_P (inside_type);
2149 int inside_float = FLOAT_TYPE_P (inside_type);
2150 int inside_vec = VECTOR_TYPE_P (inside_type);
2151 unsigned int inside_prec = TYPE_PRECISION (inside_type);
2152 int inside_unsignedp = TYPE_UNSIGNED (inside_type);
2153 int inter_int = INTEGRAL_TYPE_P (inter_type);
2154 int inter_ptr = POINTER_TYPE_P (inter_type);
2155 int inter_float = FLOAT_TYPE_P (inter_type);
2156 int inter_vec = VECTOR_TYPE_P (inter_type);
2157 unsigned int inter_prec = TYPE_PRECISION (inter_type);
2158 int inter_unsignedp = TYPE_UNSIGNED (inter_type);
2159 int final_int = INTEGRAL_TYPE_P (type);
2160 int final_ptr = POINTER_TYPE_P (type);
2161 int final_float = FLOAT_TYPE_P (type);
2162 int final_vec = VECTOR_TYPE_P (type);
2163 unsigned int final_prec = TYPE_PRECISION (type);
2164 int final_unsignedp = TYPE_UNSIGNED (type);
2167 /* In addition to the cases of two conversions in a row
2168 handled below, if we are converting something to its own
2169 type via an object of identical or wider precision, neither
2170 conversion is needed. */
2171 (if (((GIMPLE && useless_type_conversion_p (type, inside_type))
2173 && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (inside_type)))
2174 && (((inter_int || inter_ptr) && final_int)
2175 || (inter_float && final_float))
2176 && inter_prec >= final_prec)
2179 /* Likewise, if the intermediate and initial types are either both
2180 float or both integer, we don't need the middle conversion if the
2181 former is wider than the latter and doesn't change the signedness
2182 (for integers). Avoid this if the final type is a pointer since
2183 then we sometimes need the middle conversion. */
2184 (if (((inter_int && inside_int) || (inter_float && inside_float))
2185 && (final_int || final_float)
2186 && inter_prec >= inside_prec
2187 && (inter_float || inter_unsignedp == inside_unsignedp))
2190 /* If we have a sign-extension of a zero-extended value, we can
2191 replace that by a single zero-extension. Likewise if the
2192 final conversion does not change precision we can drop the
2193 intermediate conversion. */
2194 (if (inside_int && inter_int && final_int
2195 && ((inside_prec < inter_prec && inter_prec < final_prec
2196 && inside_unsignedp && !inter_unsignedp)
2197 || final_prec == inter_prec))
2200 /* Two conversions in a row are not needed unless:
2201 - some conversion is floating-point (overstrict for now), or
2202 - some conversion is a vector (overstrict for now), or
2203 - the intermediate type is narrower than both initial and
2205 - the intermediate type and innermost type differ in signedness,
2206 and the outermost type is wider than the intermediate, or
2207 - the initial type is a pointer type and the precisions of the
2208 intermediate and final types differ, or
2209 - the final type is a pointer type and the precisions of the
2210 initial and intermediate types differ. */
2211 (if (! inside_float && ! inter_float && ! final_float
2212 && ! inside_vec && ! inter_vec && ! final_vec
2213 && (inter_prec >= inside_prec || inter_prec >= final_prec)
2214 && ! (inside_int && inter_int
2215 && inter_unsignedp != inside_unsignedp
2216 && inter_prec < final_prec)
2217 && ((inter_unsignedp && inter_prec > inside_prec)
2218 == (final_unsignedp && final_prec > inter_prec))
2219 && ! (inside_ptr && inter_prec != final_prec)
2220 && ! (final_ptr && inside_prec != inter_prec))
2223 /* A truncation to an unsigned type (a zero-extension) should be
2224 canonicalized as bitwise and of a mask. */
2225 (if (GIMPLE /* PR70366: doing this in GENERIC breaks -Wconversion. */
2226 && final_int && inter_int && inside_int
2227 && final_prec == inside_prec
2228 && final_prec > inter_prec
2230 (convert (bit_and @0 { wide_int_to_tree
2232 wi::mask (inter_prec, false,
2233 TYPE_PRECISION (inside_type))); })))
2235 /* If we are converting an integer to a floating-point that can
2236 represent it exactly and back to an integer, we can skip the
2237 floating-point conversion. */
2238 (if (GIMPLE /* PR66211 */
2239 && inside_int && inter_float && final_int &&
2240 (unsigned) significand_size (TYPE_MODE (inter_type))
2241 >= inside_prec - !inside_unsignedp)
2244 /* If we have a narrowing conversion to an integral type that is fed by a
2245 BIT_AND_EXPR, we might be able to remove the BIT_AND_EXPR if it merely
2246 masks off bits outside the final type (and nothing else). */
2248 (convert (bit_and @0 INTEGER_CST@1))
2249 (if (INTEGRAL_TYPE_P (type)
2250 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
2251 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))
2252 && operand_equal_p (@1, build_low_bits_mask (TREE_TYPE (@1),
2253 TYPE_PRECISION (type)), 0))
2257 /* (X /[ex] A) * A -> X. */
2259 (mult (convert1? (exact_div @0 @@1)) (convert2? @1))
2262 /* Canonicalization of binary operations. */
2264 /* Convert X + -C into X - C. */
2266 (plus @0 REAL_CST@1)
2267 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
2268 (with { tree tem = const_unop (NEGATE_EXPR, type, @1); }
2269 (if (!TREE_OVERFLOW (tem) || !flag_trapping_math)
2270 (minus @0 { tem; })))))
2272 /* Convert x+x into x*2. */
2275 (if (SCALAR_FLOAT_TYPE_P (type))
2276 (mult @0 { build_real (type, dconst2); })
2277 (if (INTEGRAL_TYPE_P (type))
2278 (mult @0 { build_int_cst (type, 2); }))))
2281 (minus integer_zerop @1)
2284 /* (ARG0 - ARG1) is the same as (-ARG1 + ARG0). So check whether
2285 ARG0 is zero and X + ARG0 reduces to X, since that would mean
2286 (-ARG1 + ARG0) reduces to -ARG1. */
2288 (minus real_zerop@0 @1)
2289 (if (fold_real_zero_addition_p (type, @0, 0))
2292 /* Transform x * -1 into -x. */
2294 (mult @0 integer_minus_onep)
2297 /* Reassociate (X * CST) * Y to (X * Y) * CST. This does not introduce
2298 signed overflow for CST != 0 && CST != -1. */
2300 (mult:c (mult:s @0 INTEGER_CST@1) @2)
2301 (if (TREE_CODE (@2) != INTEGER_CST
2302 && !integer_zerop (@1) && !integer_minus_onep (@1))
2303 (mult (mult @0 @2) @1)))
2305 /* True if we can easily extract the real and imaginary parts of a complex
2307 (match compositional_complex
2308 (convert? (complex @0 @1)))
2310 /* COMPLEX_EXPR and REALPART/IMAGPART_EXPR cancellations. */
2312 (complex (realpart @0) (imagpart @0))
2315 (realpart (complex @0 @1))
2318 (imagpart (complex @0 @1))
2321 /* Sometimes we only care about half of a complex expression. */
2323 (realpart (convert?:s (conj:s @0)))
2324 (convert (realpart @0)))
2326 (imagpart (convert?:s (conj:s @0)))
2327 (convert (negate (imagpart @0))))
2328 (for part (realpart imagpart)
2329 (for op (plus minus)
2331 (part (convert?:s@2 (op:s @0 @1)))
2332 (convert (op (part @0) (part @1))))))
2334 (realpart (convert?:s (CEXPI:s @0)))
2337 (imagpart (convert?:s (CEXPI:s @0)))
2340 /* conj(conj(x)) -> x */
2342 (conj (convert? (conj @0)))
2343 (if (tree_nop_conversion_p (TREE_TYPE (@0), type))
2346 /* conj({x,y}) -> {x,-y} */
2348 (conj (convert?:s (complex:s @0 @1)))
2349 (with { tree itype = TREE_TYPE (type); }
2350 (complex (convert:itype @0) (negate (convert:itype @1)))))
2352 /* BSWAP simplifications, transforms checked by gcc.dg/builtin-bswap-8.c. */
2353 (for bswap (BUILT_IN_BSWAP16 BUILT_IN_BSWAP32 BUILT_IN_BSWAP64)
2358 (bswap (bit_not (bswap @0)))
2360 (for bitop (bit_xor bit_ior bit_and)
2362 (bswap (bitop:c (bswap @0) @1))
2363 (bitop @0 (bswap @1)))))
2366 /* Combine COND_EXPRs and VEC_COND_EXPRs. */
2368 /* Simplify constant conditions.
2369 Only optimize constant conditions when the selected branch
2370 has the same type as the COND_EXPR. This avoids optimizing
2371 away "c ? x : throw", where the throw has a void type.
2372 Note that we cannot throw away the fold-const.c variant nor
2373 this one as we depend on doing this transform before possibly
2374 A ? B : B -> B triggers and the fold-const.c one can optimize
2375 0 ? A : B to B even if A has side-effects. Something
2376 genmatch cannot handle. */
2378 (cond INTEGER_CST@0 @1 @2)
2379 (if (integer_zerop (@0))
2380 (if (!VOID_TYPE_P (TREE_TYPE (@2)) || VOID_TYPE_P (type))
2382 (if (!VOID_TYPE_P (TREE_TYPE (@1)) || VOID_TYPE_P (type))
2385 (vec_cond VECTOR_CST@0 @1 @2)
2386 (if (integer_all_onesp (@0))
2388 (if (integer_zerop (@0))
2391 /* Simplification moved from fold_cond_expr_with_comparison. It may also
2393 /* This pattern implements two kinds simplification:
2396 (cond (cmp (convert1? x) c1) (convert2? x) c2) -> (minmax (x c)) if:
2397 1) Conversions are type widening from smaller type.
2398 2) Const c1 equals to c2 after canonicalizing comparison.
2399 3) Comparison has tree code LT, LE, GT or GE.
2400 This specific pattern is needed when (cmp (convert x) c) may not
2401 be simplified by comparison patterns because of multiple uses of
2402 x. It also makes sense here because simplifying across multiple
2403 referred var is always benefitial for complicated cases.
2406 (cond (eq (convert1? x) c1) (convert2? x) c2) -> (cond (eq x c1) c1 c2). */
2407 (for cmp (lt le gt ge eq)
2409 (cond (cmp (convert1? @1) INTEGER_CST@3) (convert2? @1) INTEGER_CST@2)
2412 tree from_type = TREE_TYPE (@1);
2413 tree c1_type = TREE_TYPE (@3), c2_type = TREE_TYPE (@2);
2414 enum tree_code code = ERROR_MARK;
2416 if (INTEGRAL_TYPE_P (from_type)
2417 && int_fits_type_p (@2, from_type)
2418 && (types_match (c1_type, from_type)
2419 || (TYPE_PRECISION (c1_type) > TYPE_PRECISION (from_type)
2420 && (TYPE_UNSIGNED (from_type)
2421 || TYPE_SIGN (c1_type) == TYPE_SIGN (from_type))))
2422 && (types_match (c2_type, from_type)
2423 || (TYPE_PRECISION (c2_type) > TYPE_PRECISION (from_type)
2424 && (TYPE_UNSIGNED (from_type)
2425 || TYPE_SIGN (c2_type) == TYPE_SIGN (from_type)))))
2429 if (wi::to_widest (@3) == (wi::to_widest (@2) - 1))
2431 /* X <= Y - 1 equals to X < Y. */
2434 /* X > Y - 1 equals to X >= Y. */
2438 if (wi::to_widest (@3) == (wi::to_widest (@2) + 1))
2440 /* X < Y + 1 equals to X <= Y. */
2443 /* X >= Y + 1 equals to X > Y. */
2447 if (code != ERROR_MARK
2448 || wi::to_widest (@2) == wi::to_widest (@3))
2450 if (cmp == LT_EXPR || cmp == LE_EXPR)
2452 if (cmp == GT_EXPR || cmp == GE_EXPR)
2456 /* Can do A == C1 ? A : C2 -> A == C1 ? C1 : C2? */
2457 else if (int_fits_type_p (@3, from_type))
2461 (if (code == MAX_EXPR)
2462 (convert (max @1 (convert @2)))
2463 (if (code == MIN_EXPR)
2464 (convert (min @1 (convert @2)))
2465 (if (code == EQ_EXPR)
2466 (convert (cond (eq @1 (convert @3))
2467 (convert:from_type @3) (convert:from_type @2)))))))))
2469 /* (cond (cmp (convert? x) c1) (op x c2) c3) -> (op (minmax x c1) c2) if:
2471 1) OP is PLUS or MINUS.
2472 2) CMP is LT, LE, GT or GE.
2473 3) C3 == (C1 op C2), and computation doesn't have undefined behavior.
2475 This pattern also handles special cases like:
2477 A) Operand x is a unsigned to signed type conversion and c1 is
2478 integer zero. In this case,
2479 (signed type)x < 0 <=> x > MAX_VAL(signed type)
2480 (signed type)x >= 0 <=> x <= MAX_VAL(signed type)
2481 B) Const c1 may not equal to (C3 op' C2). In this case we also
2482 check equality for (c1+1) and (c1-1) by adjusting comparison
2485 TODO: Though signed type is handled by this pattern, it cannot be
2486 simplified at the moment because C standard requires additional
2487 type promotion. In order to match&simplify it here, the IR needs
2488 to be cleaned up by other optimizers, i.e, VRP. */
2489 (for op (plus minus)
2490 (for cmp (lt le gt ge)
2492 (cond (cmp (convert? @X) INTEGER_CST@1) (op @X INTEGER_CST@2) INTEGER_CST@3)
2493 (with { tree from_type = TREE_TYPE (@X), to_type = TREE_TYPE (@1); }
2494 (if (types_match (from_type, to_type)
2495 /* Check if it is special case A). */
2496 || (TYPE_UNSIGNED (from_type)
2497 && !TYPE_UNSIGNED (to_type)
2498 && TYPE_PRECISION (from_type) == TYPE_PRECISION (to_type)
2499 && integer_zerop (@1)
2500 && (cmp == LT_EXPR || cmp == GE_EXPR)))
2503 bool overflow = false;
2504 enum tree_code code, cmp_code = cmp;
2506 wide_int c1 = wi::to_wide (@1);
2507 wide_int c2 = wi::to_wide (@2);
2508 wide_int c3 = wi::to_wide (@3);
2509 signop sgn = TYPE_SIGN (from_type);
2511 /* Handle special case A), given x of unsigned type:
2512 ((signed type)x < 0) <=> (x > MAX_VAL(signed type))
2513 ((signed type)x >= 0) <=> (x <= MAX_VAL(signed type)) */
2514 if (!types_match (from_type, to_type))
2516 if (cmp_code == LT_EXPR)
2518 if (cmp_code == GE_EXPR)
2520 c1 = wi::max_value (to_type);
2522 /* To simplify this pattern, we require c3 = (c1 op c2). Here we
2523 compute (c3 op' c2) and check if it equals to c1 with op' being
2524 the inverted operator of op. Make sure overflow doesn't happen
2525 if it is undefined. */
2526 if (op == PLUS_EXPR)
2527 real_c1 = wi::sub (c3, c2, sgn, &overflow);
2529 real_c1 = wi::add (c3, c2, sgn, &overflow);
2532 if (!overflow || !TYPE_OVERFLOW_UNDEFINED (from_type))
2534 /* Check if c1 equals to real_c1. Boundary condition is handled
2535 by adjusting comparison operation if necessary. */
2536 if (!wi::cmp (wi::sub (real_c1, 1, sgn, &overflow), c1, sgn)
2539 /* X <= Y - 1 equals to X < Y. */
2540 if (cmp_code == LE_EXPR)
2542 /* X > Y - 1 equals to X >= Y. */
2543 if (cmp_code == GT_EXPR)
2546 if (!wi::cmp (wi::add (real_c1, 1, sgn, &overflow), c1, sgn)
2549 /* X < Y + 1 equals to X <= Y. */
2550 if (cmp_code == LT_EXPR)
2552 /* X >= Y + 1 equals to X > Y. */
2553 if (cmp_code == GE_EXPR)
2556 if (code != cmp_code || !wi::cmp (real_c1, c1, sgn))
2558 if (cmp_code == LT_EXPR || cmp_code == LE_EXPR)
2560 if (cmp_code == GT_EXPR || cmp_code == GE_EXPR)
2565 (if (code == MAX_EXPR)
2566 (op (max @X { wide_int_to_tree (from_type, real_c1); })
2567 { wide_int_to_tree (from_type, c2); })
2568 (if (code == MIN_EXPR)
2569 (op (min @X { wide_int_to_tree (from_type, real_c1); })
2570 { wide_int_to_tree (from_type, c2); })))))))))
2572 (for cnd (cond vec_cond)
2573 /* A ? B : (A ? X : C) -> A ? B : C. */
2575 (cnd @0 (cnd @0 @1 @2) @3)
2578 (cnd @0 @1 (cnd @0 @2 @3))
2580 /* A ? B : (!A ? C : X) -> A ? B : C. */
2581 /* ??? This matches embedded conditions open-coded because genmatch
2582 would generate matching code for conditions in separate stmts only.
2583 The following is still important to merge then and else arm cases
2584 from if-conversion. */
2586 (cnd @0 @1 (cnd @2 @3 @4))
2587 (if (COMPARISON_CLASS_P (@0)
2588 && COMPARISON_CLASS_P (@2)
2589 && invert_tree_comparison
2590 (TREE_CODE (@0), HONOR_NANS (TREE_OPERAND (@0, 0))) == TREE_CODE (@2)
2591 && operand_equal_p (TREE_OPERAND (@0, 0), TREE_OPERAND (@2, 0), 0)
2592 && operand_equal_p (TREE_OPERAND (@0, 1), TREE_OPERAND (@2, 1), 0))
2595 (cnd @0 (cnd @1 @2 @3) @4)
2596 (if (COMPARISON_CLASS_P (@0)
2597 && COMPARISON_CLASS_P (@1)
2598 && invert_tree_comparison
2599 (TREE_CODE (@0), HONOR_NANS (TREE_OPERAND (@0, 0))) == TREE_CODE (@1)
2600 && operand_equal_p (TREE_OPERAND (@0, 0), TREE_OPERAND (@1, 0), 0)
2601 && operand_equal_p (TREE_OPERAND (@0, 1), TREE_OPERAND (@1, 1), 0))
2604 /* A ? B : B -> B. */
2609 /* !A ? B : C -> A ? C : B. */
2611 (cnd (logical_inverted_value truth_valued_p@0) @1 @2)
2614 /* A + (B vcmp C ? 1 : 0) -> A - (B vcmp C ? -1 : 0), since vector comparisons
2615 return all -1 or all 0 results. */
2616 /* ??? We could instead convert all instances of the vec_cond to negate,
2617 but that isn't necessarily a win on its own. */
2619 (plus:c @3 (view_convert? (vec_cond:s @0 integer_each_onep@1 integer_zerop@2)))
2620 (if (VECTOR_TYPE_P (type)
2621 && TYPE_VECTOR_SUBPARTS (type) == TYPE_VECTOR_SUBPARTS (TREE_TYPE (@1))
2622 && (TYPE_MODE (TREE_TYPE (type))
2623 == TYPE_MODE (TREE_TYPE (TREE_TYPE (@1)))))
2624 (minus @3 (view_convert (vec_cond @0 (negate @1) @2)))))
2626 /* ... likewise A - (B vcmp C ? 1 : 0) -> A + (B vcmp C ? -1 : 0). */
2628 (minus @3 (view_convert? (vec_cond:s @0 integer_each_onep@1 integer_zerop@2)))
2629 (if (VECTOR_TYPE_P (type)
2630 && TYPE_VECTOR_SUBPARTS (type) == TYPE_VECTOR_SUBPARTS (TREE_TYPE (@1))
2631 && (TYPE_MODE (TREE_TYPE (type))
2632 == TYPE_MODE (TREE_TYPE (TREE_TYPE (@1)))))
2633 (plus @3 (view_convert (vec_cond @0 (negate @1) @2)))))
2636 /* Simplifications of comparisons. */
2638 /* See if we can reduce the magnitude of a constant involved in a
2639 comparison by changing the comparison code. This is a canonicalization
2640 formerly done by maybe_canonicalize_comparison_1. */
2644 (cmp @0 INTEGER_CST@1)
2645 (if (tree_int_cst_sgn (@1) == -1)
2646 (acmp @0 { wide_int_to_tree (TREE_TYPE (@1), wi::to_wide (@1) + 1); }))))
2650 (cmp @0 INTEGER_CST@1)
2651 (if (tree_int_cst_sgn (@1) == 1)
2652 (acmp @0 { wide_int_to_tree (TREE_TYPE (@1), wi::to_wide (@1) - 1); }))))
2655 /* We can simplify a logical negation of a comparison to the
2656 inverted comparison. As we cannot compute an expression
2657 operator using invert_tree_comparison we have to simulate
2658 that with expression code iteration. */
2659 (for cmp (tcc_comparison)
2660 icmp (inverted_tcc_comparison)
2661 ncmp (inverted_tcc_comparison_with_nans)
2662 /* Ideally we'd like to combine the following two patterns
2663 and handle some more cases by using
2664 (logical_inverted_value (cmp @0 @1))
2665 here but for that genmatch would need to "inline" that.
2666 For now implement what forward_propagate_comparison did. */
2668 (bit_not (cmp @0 @1))
2669 (if (VECTOR_TYPE_P (type)
2670 || (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1))
2671 /* Comparison inversion may be impossible for trapping math,
2672 invert_tree_comparison will tell us. But we can't use
2673 a computed operator in the replacement tree thus we have
2674 to play the trick below. */
2675 (with { enum tree_code ic = invert_tree_comparison
2676 (cmp, HONOR_NANS (@0)); }
2682 (bit_xor (cmp @0 @1) integer_truep)
2683 (with { enum tree_code ic = invert_tree_comparison
2684 (cmp, HONOR_NANS (@0)); }
2690 /* Transform comparisons of the form X - Y CMP 0 to X CMP Y.
2691 ??? The transformation is valid for the other operators if overflow
2692 is undefined for the type, but performing it here badly interacts
2693 with the transformation in fold_cond_expr_with_comparison which
2694 attempts to synthetize ABS_EXPR. */
2697 (cmp (minus@2 @0 @1) integer_zerop)
2698 (if (single_use (@2))
2701 /* Transform comparisons of the form X * C1 CMP 0 to X CMP 0 in the
2702 signed arithmetic case. That form is created by the compiler
2703 often enough for folding it to be of value. One example is in
2704 computing loop trip counts after Operator Strength Reduction. */
2705 (for cmp (simple_comparison)
2706 scmp (swapped_simple_comparison)
2708 (cmp (mult@3 @0 INTEGER_CST@1) integer_zerop@2)
2709 /* Handle unfolded multiplication by zero. */
2710 (if (integer_zerop (@1))
2712 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2713 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2715 /* If @1 is negative we swap the sense of the comparison. */
2716 (if (tree_int_cst_sgn (@1) < 0)
2720 /* Simplify comparison of something with itself. For IEEE
2721 floating-point, we can only do some of these simplifications. */
2725 (if (! FLOAT_TYPE_P (TREE_TYPE (@0))
2726 || ! HONOR_NANS (@0))
2727 { constant_boolean_node (true, type); }
2728 (if (cmp != EQ_EXPR)
2734 || ! FLOAT_TYPE_P (TREE_TYPE (@0))
2735 || ! HONOR_NANS (@0))
2736 { constant_boolean_node (false, type); })))
2737 (for cmp (unle unge uneq)
2740 { constant_boolean_node (true, type); }))
2741 (for cmp (unlt ungt)
2747 (if (!flag_trapping_math)
2748 { constant_boolean_node (false, type); }))
2750 /* Fold ~X op ~Y as Y op X. */
2751 (for cmp (simple_comparison)
2753 (cmp (bit_not@2 @0) (bit_not@3 @1))
2754 (if (single_use (@2) && single_use (@3))
2757 /* Fold ~X op C as X op' ~C, where op' is the swapped comparison. */
2758 (for cmp (simple_comparison)
2759 scmp (swapped_simple_comparison)
2761 (cmp (bit_not@2 @0) CONSTANT_CLASS_P@1)
2762 (if (single_use (@2)
2763 && (TREE_CODE (@1) == INTEGER_CST || TREE_CODE (@1) == VECTOR_CST))
2764 (scmp @0 (bit_not @1)))))
2766 (for cmp (simple_comparison)
2767 /* Fold (double)float1 CMP (double)float2 into float1 CMP float2. */
2769 (cmp (convert@2 @0) (convert? @1))
2770 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
2771 && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@2))
2772 == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0)))
2773 && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@2))
2774 == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1))))
2777 tree type1 = TREE_TYPE (@1);
2778 if (TREE_CODE (@1) == REAL_CST && !DECIMAL_FLOAT_TYPE_P (type1))
2780 REAL_VALUE_TYPE orig = TREE_REAL_CST (@1);
2781 if (TYPE_PRECISION (type1) > TYPE_PRECISION (float_type_node)
2782 && exact_real_truncate (TYPE_MODE (float_type_node), &orig))
2783 type1 = float_type_node;
2784 if (TYPE_PRECISION (type1) > TYPE_PRECISION (double_type_node)
2785 && exact_real_truncate (TYPE_MODE (double_type_node), &orig))
2786 type1 = double_type_node;
2789 = (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type1)
2790 ? TREE_TYPE (@0) : type1);
2792 (if (TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (newtype))
2793 (cmp (convert:newtype @0) (convert:newtype @1))))))
2797 /* IEEE doesn't distinguish +0 and -0 in comparisons. */
2799 /* a CMP (-0) -> a CMP 0 */
2800 (if (REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@1)))
2801 (cmp @0 { build_real (TREE_TYPE (@1), dconst0); }))
2802 /* x != NaN is always true, other ops are always false. */
2803 (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
2804 && ! HONOR_SNANS (@1))
2805 { constant_boolean_node (cmp == NE_EXPR, type); })
2806 /* Fold comparisons against infinity. */
2807 (if (REAL_VALUE_ISINF (TREE_REAL_CST (@1))
2808 && MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (@1))))
2811 REAL_VALUE_TYPE max;
2812 enum tree_code code = cmp;
2813 bool neg = REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1));
2815 code = swap_tree_comparison (code);
2818 /* x > +Inf is always false, if with ignore sNANs. */
2819 (if (code == GT_EXPR
2820 && ! HONOR_SNANS (@0))
2821 { constant_boolean_node (false, type); })
2822 (if (code == LE_EXPR)
2823 /* x <= +Inf is always true, if we don't case about NaNs. */
2824 (if (! HONOR_NANS (@0))
2825 { constant_boolean_node (true, type); }
2826 /* x <= +Inf is the same as x == x, i.e. !isnan(x). */
2828 /* x == +Inf and x >= +Inf are always equal to x > DBL_MAX. */
2829 (if (code == EQ_EXPR || code == GE_EXPR)
2830 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
2832 (lt @0 { build_real (TREE_TYPE (@0), max); })
2833 (gt @0 { build_real (TREE_TYPE (@0), max); }))))
2834 /* x < +Inf is always equal to x <= DBL_MAX. */
2835 (if (code == LT_EXPR)
2836 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
2838 (ge @0 { build_real (TREE_TYPE (@0), max); })
2839 (le @0 { build_real (TREE_TYPE (@0), max); }))))
2840 /* x != +Inf is always equal to !(x > DBL_MAX). */
2841 (if (code == NE_EXPR)
2842 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
2843 (if (! HONOR_NANS (@0))
2845 (ge @0 { build_real (TREE_TYPE (@0), max); })
2846 (le @0 { build_real (TREE_TYPE (@0), max); }))
2848 (bit_xor (lt @0 { build_real (TREE_TYPE (@0), max); })
2849 { build_one_cst (type); })
2850 (bit_xor (gt @0 { build_real (TREE_TYPE (@0), max); })
2851 { build_one_cst (type); }))))))))))
2853 /* If this is a comparison of a real constant with a PLUS_EXPR
2854 or a MINUS_EXPR of a real constant, we can convert it into a
2855 comparison with a revised real constant as long as no overflow
2856 occurs when unsafe_math_optimizations are enabled. */
2857 (if (flag_unsafe_math_optimizations)
2858 (for op (plus minus)
2860 (cmp (op @0 REAL_CST@1) REAL_CST@2)
2863 tree tem = const_binop (op == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR,
2864 TREE_TYPE (@1), @2, @1);
2866 (if (tem && !TREE_OVERFLOW (tem))
2867 (cmp @0 { tem; }))))))
2869 /* Likewise, we can simplify a comparison of a real constant with
2870 a MINUS_EXPR whose first operand is also a real constant, i.e.
2871 (c1 - x) < c2 becomes x > c1-c2. Reordering is allowed on
2872 floating-point types only if -fassociative-math is set. */
2873 (if (flag_associative_math)
2875 (cmp (minus REAL_CST@0 @1) REAL_CST@2)
2876 (with { tree tem = const_binop (MINUS_EXPR, TREE_TYPE (@1), @0, @2); }
2877 (if (tem && !TREE_OVERFLOW (tem))
2878 (cmp { tem; } @1)))))
2880 /* Fold comparisons against built-in math functions. */
2881 (if (flag_unsafe_math_optimizations
2882 && ! flag_errno_math)
2885 (cmp (sq @0) REAL_CST@1)
2887 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
2889 /* sqrt(x) < y is always false, if y is negative. */
2890 (if (cmp == EQ_EXPR || cmp == LT_EXPR || cmp == LE_EXPR)
2891 { constant_boolean_node (false, type); })
2892 /* sqrt(x) > y is always true, if y is negative and we
2893 don't care about NaNs, i.e. negative values of x. */
2894 (if (cmp == NE_EXPR || !HONOR_NANS (@0))
2895 { constant_boolean_node (true, type); })
2896 /* sqrt(x) > y is the same as x >= 0, if y is negative. */
2897 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })))
2898 (if (real_equal (TREE_REAL_CST_PTR (@1), &dconst0))
2900 /* sqrt(x) < 0 is always false. */
2901 (if (cmp == LT_EXPR)
2902 { constant_boolean_node (false, type); })
2903 /* sqrt(x) >= 0 is always true if we don't care about NaNs. */
2904 (if (cmp == GE_EXPR && !HONOR_NANS (@0))
2905 { constant_boolean_node (true, type); })
2906 /* sqrt(x) <= 0 -> x == 0. */
2907 (if (cmp == LE_EXPR)
2909 /* Otherwise sqrt(x) cmp 0 -> x cmp 0. Here cmp can be >=, >,
2910 == or !=. In the last case:
2912 (sqrt(x) != 0) == (NaN != 0) == true == (x != 0)
2914 if x is negative or NaN. Due to -funsafe-math-optimizations,
2915 the results for other x follow from natural arithmetic. */
2917 (if (cmp == GT_EXPR || cmp == GE_EXPR)
2921 real_arithmetic (&c2, MULT_EXPR,
2922 &TREE_REAL_CST (@1), &TREE_REAL_CST (@1));
2923 real_convert (&c2, TYPE_MODE (TREE_TYPE (@0)), &c2);
2925 (if (REAL_VALUE_ISINF (c2))
2926 /* sqrt(x) > y is x == +Inf, when y is very large. */
2927 (if (HONOR_INFINITIES (@0))
2928 (eq @0 { build_real (TREE_TYPE (@0), c2); })
2929 { constant_boolean_node (false, type); })
2930 /* sqrt(x) > c is the same as x > c*c. */
2931 (cmp @0 { build_real (TREE_TYPE (@0), c2); }))))
2932 (if (cmp == LT_EXPR || cmp == LE_EXPR)
2936 real_arithmetic (&c2, MULT_EXPR,
2937 &TREE_REAL_CST (@1), &TREE_REAL_CST (@1));
2938 real_convert (&c2, TYPE_MODE (TREE_TYPE (@0)), &c2);
2940 (if (REAL_VALUE_ISINF (c2))
2942 /* sqrt(x) < y is always true, when y is a very large
2943 value and we don't care about NaNs or Infinities. */
2944 (if (! HONOR_NANS (@0) && ! HONOR_INFINITIES (@0))
2945 { constant_boolean_node (true, type); })
2946 /* sqrt(x) < y is x != +Inf when y is very large and we
2947 don't care about NaNs. */
2948 (if (! HONOR_NANS (@0))
2949 (ne @0 { build_real (TREE_TYPE (@0), c2); }))
2950 /* sqrt(x) < y is x >= 0 when y is very large and we
2951 don't care about Infinities. */
2952 (if (! HONOR_INFINITIES (@0))
2953 (ge @0 { build_real (TREE_TYPE (@0), dconst0); }))
2954 /* sqrt(x) < y is x >= 0 && x != +Inf, when y is large. */
2957 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
2958 (ne @0 { build_real (TREE_TYPE (@0), c2); }))))
2959 /* sqrt(x) < c is the same as x < c*c, if we ignore NaNs. */
2960 (if (! HONOR_NANS (@0))
2961 (cmp @0 { build_real (TREE_TYPE (@0), c2); })
2962 /* sqrt(x) < c is the same as x >= 0 && x < c*c. */
2965 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
2966 (cmp @0 { build_real (TREE_TYPE (@0), c2); })))))))))
2967 /* Transform sqrt(x) cmp sqrt(y) -> x cmp y. */
2969 (cmp (sq @0) (sq @1))
2970 (if (! HONOR_NANS (@0))
2973 /* Optimize various special cases of (FTYPE) N CMP CST. */
2974 (for cmp (lt le eq ne ge gt)
2975 icmp (le le eq ne ge ge)
2977 (cmp (float @0) REAL_CST@1)
2978 (if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (@1))
2979 && ! DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1)))
2982 tree itype = TREE_TYPE (@0);
2983 signop isign = TYPE_SIGN (itype);
2984 format_helper fmt (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (@1))));
2985 const REAL_VALUE_TYPE *cst = TREE_REAL_CST_PTR (@1);
2986 /* Be careful to preserve any potential exceptions due to
2987 NaNs. qNaNs are ok in == or != context.
2988 TODO: relax under -fno-trapping-math or
2989 -fno-signaling-nans. */
2991 = real_isnan (cst) && (cst->signalling
2992 || (cmp != EQ_EXPR && cmp != NE_EXPR));
2993 /* INT?_MIN is power-of-two so it takes
2994 only one mantissa bit. */
2995 bool signed_p = isign == SIGNED;
2996 bool itype_fits_ftype_p
2997 = TYPE_PRECISION (itype) - signed_p <= significand_size (fmt);
2999 /* TODO: allow non-fitting itype and SNaNs when
3000 -fno-trapping-math. */
3001 (if (itype_fits_ftype_p && ! exception_p)
3004 REAL_VALUE_TYPE imin, imax;
3005 real_from_integer (&imin, fmt, wi::min_value (itype), isign);
3006 real_from_integer (&imax, fmt, wi::max_value (itype), isign);
3008 REAL_VALUE_TYPE icst;
3009 if (cmp == GT_EXPR || cmp == GE_EXPR)
3010 real_ceil (&icst, fmt, cst);
3011 else if (cmp == LT_EXPR || cmp == LE_EXPR)
3012 real_floor (&icst, fmt, cst);
3014 real_trunc (&icst, fmt, cst);
3016 bool cst_int_p = !real_isnan (cst) && real_identical (&icst, cst);
3018 bool overflow_p = false;
3020 = real_to_integer (&icst, &overflow_p, TYPE_PRECISION (itype));
3023 /* Optimize cases when CST is outside of ITYPE's range. */
3024 (if (real_compare (LT_EXPR, cst, &imin))
3025 { constant_boolean_node (cmp == GT_EXPR || cmp == GE_EXPR || cmp == NE_EXPR,
3027 (if (real_compare (GT_EXPR, cst, &imax))
3028 { constant_boolean_node (cmp == LT_EXPR || cmp == LE_EXPR || cmp == NE_EXPR,
3030 /* Remove cast if CST is an integer representable by ITYPE. */
3032 (cmp @0 { gcc_assert (!overflow_p);
3033 wide_int_to_tree (itype, icst_val); })
3035 /* When CST is fractional, optimize
3036 (FTYPE) N == CST -> 0
3037 (FTYPE) N != CST -> 1. */
3038 (if (cmp == EQ_EXPR || cmp == NE_EXPR)
3039 { constant_boolean_node (cmp == NE_EXPR, type); })
3040 /* Otherwise replace with sensible integer constant. */
3043 gcc_checking_assert (!overflow_p);
3045 (icmp @0 { wide_int_to_tree (itype, icst_val); })))))))))
3047 /* Fold A /[ex] B CMP C to A CMP B * C. */
3050 (cmp (exact_div @0 @1) INTEGER_CST@2)
3051 (if (!integer_zerop (@1))
3052 (if (wi::to_wide (@2) == 0)
3054 (if (TREE_CODE (@1) == INTEGER_CST)
3058 wide_int prod = wi::mul (wi::to_wide (@2), wi::to_wide (@1),
3059 TYPE_SIGN (TREE_TYPE (@1)), &ovf);
3062 { constant_boolean_node (cmp == NE_EXPR, type); }
3063 (cmp @0 { wide_int_to_tree (TREE_TYPE (@0), prod); }))))))))
3064 (for cmp (lt le gt ge)
3066 (cmp (exact_div @0 INTEGER_CST@1) INTEGER_CST@2)
3067 (if (wi::gt_p (wi::to_wide (@1), 0, TYPE_SIGN (TREE_TYPE (@1))))
3071 wide_int prod = wi::mul (wi::to_wide (@2), wi::to_wide (@1),
3072 TYPE_SIGN (TREE_TYPE (@1)), &ovf);
3075 { constant_boolean_node (wi::lt_p (wi::to_wide (@2), 0,
3076 TYPE_SIGN (TREE_TYPE (@2)))
3077 != (cmp == LT_EXPR || cmp == LE_EXPR), type); }
3078 (cmp @0 { wide_int_to_tree (TREE_TYPE (@0), prod); }))))))
3080 /* Unordered tests if either argument is a NaN. */
3082 (bit_ior (unordered @0 @0) (unordered @1 @1))
3083 (if (types_match (@0, @1))
3086 (bit_and (ordered @0 @0) (ordered @1 @1))
3087 (if (types_match (@0, @1))
3090 (bit_ior:c (unordered @0 @0) (unordered:c@2 @0 @1))
3093 (bit_and:c (ordered @0 @0) (ordered:c@2 @0 @1))
3096 /* Simple range test simplifications. */
3097 /* A < B || A >= B -> true. */
3098 (for test1 (lt le le le ne ge)
3099 test2 (ge gt ge ne eq ne)
3101 (bit_ior:c (test1 @0 @1) (test2 @0 @1))
3102 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
3103 || VECTOR_INTEGER_TYPE_P (TREE_TYPE (@0)))
3104 { constant_boolean_node (true, type); })))
3105 /* A < B && A >= B -> false. */
3106 (for test1 (lt lt lt le ne eq)
3107 test2 (ge gt eq gt eq gt)
3109 (bit_and:c (test1 @0 @1) (test2 @0 @1))
3110 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
3111 || VECTOR_INTEGER_TYPE_P (TREE_TYPE (@0)))
3112 { constant_boolean_node (false, type); })))
3114 /* A & (2**N - 1) <= 2**K - 1 -> A & (2**N - 2**K) == 0
3115 A & (2**N - 1) > 2**K - 1 -> A & (2**N - 2**K) != 0
3117 Note that comparisons
3118 A & (2**N - 1) < 2**K -> A & (2**N - 2**K) == 0
3119 A & (2**N - 1) >= 2**K -> A & (2**N - 2**K) != 0
3120 will be canonicalized to above so there's no need to
3127 (cmp (bit_and@0 @1 INTEGER_CST@2) INTEGER_CST@3)
3128 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)))
3131 tree ty = TREE_TYPE (@0);
3132 unsigned prec = TYPE_PRECISION (ty);
3133 wide_int mask = wi::to_wide (@2, prec);
3134 wide_int rhs = wi::to_wide (@3, prec);
3135 signop sgn = TYPE_SIGN (ty);
3137 (if ((mask & (mask + 1)) == 0 && wi::gt_p (rhs, 0, sgn)
3138 && (rhs & (rhs + 1)) == 0 && wi::ge_p (mask, rhs, sgn))
3139 (eqcmp (bit_and @1 { wide_int_to_tree (ty, mask - rhs); })
3140 { build_zero_cst (ty); }))))))
3142 /* -A CMP -B -> B CMP A. */
3143 (for cmp (tcc_comparison)
3144 scmp (swapped_tcc_comparison)
3146 (cmp (negate @0) (negate @1))
3147 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
3148 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3149 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
3152 (cmp (negate @0) CONSTANT_CLASS_P@1)
3153 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
3154 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3155 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
3156 (with { tree tem = const_unop (NEGATE_EXPR, TREE_TYPE (@0), @1); }
3157 (if (tem && !TREE_OVERFLOW (tem))
3158 (scmp @0 { tem; }))))))
3160 /* Convert ABS_EXPR<x> == 0 or ABS_EXPR<x> != 0 to x == 0 or x != 0. */
3163 (op (abs @0) zerop@1)
3166 /* From fold_sign_changed_comparison and fold_widened_comparison.
3167 FIXME: the lack of symmetry is disturbing. */
3168 (for cmp (simple_comparison)
3170 (cmp (convert@0 @00) (convert?@1 @10))
3171 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
3172 /* Disable this optimization if we're casting a function pointer
3173 type on targets that require function pointer canonicalization. */
3174 && !(targetm.have_canonicalize_funcptr_for_compare ()
3175 && TREE_CODE (TREE_TYPE (@00)) == POINTER_TYPE
3176 && TREE_CODE (TREE_TYPE (TREE_TYPE (@00))) == FUNCTION_TYPE)
3178 (if (TYPE_PRECISION (TREE_TYPE (@00)) == TYPE_PRECISION (TREE_TYPE (@0))
3179 && (TREE_CODE (@10) == INTEGER_CST
3181 && (TYPE_UNSIGNED (TREE_TYPE (@00)) == TYPE_UNSIGNED (TREE_TYPE (@0))
3184 && !POINTER_TYPE_P (TREE_TYPE (@00)))
3185 /* ??? The special-casing of INTEGER_CST conversion was in the original
3186 code and here to avoid a spurious overflow flag on the resulting
3187 constant which fold_convert produces. */
3188 (if (TREE_CODE (@1) == INTEGER_CST)
3189 (cmp @00 { force_fit_type (TREE_TYPE (@00), wi::to_widest (@1), 0,
3190 TREE_OVERFLOW (@1)); })
3191 (cmp @00 (convert @1)))
3193 (if (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (TREE_TYPE (@00)))
3194 /* If possible, express the comparison in the shorter mode. */
3195 (if ((cmp == EQ_EXPR || cmp == NE_EXPR
3196 || TYPE_UNSIGNED (TREE_TYPE (@0)) == TYPE_UNSIGNED (TREE_TYPE (@00))
3197 || (!TYPE_UNSIGNED (TREE_TYPE (@0))
3198 && TYPE_UNSIGNED (TREE_TYPE (@00))))
3199 && (types_match (TREE_TYPE (@10), TREE_TYPE (@00))
3200 || ((TYPE_PRECISION (TREE_TYPE (@00))
3201 >= TYPE_PRECISION (TREE_TYPE (@10)))
3202 && (TYPE_UNSIGNED (TREE_TYPE (@00))
3203 == TYPE_UNSIGNED (TREE_TYPE (@10))))
3204 || (TREE_CODE (@10) == INTEGER_CST
3205 && INTEGRAL_TYPE_P (TREE_TYPE (@00))
3206 && int_fits_type_p (@10, TREE_TYPE (@00)))))
3207 (cmp @00 (convert @10))
3208 (if (TREE_CODE (@10) == INTEGER_CST
3209 && INTEGRAL_TYPE_P (TREE_TYPE (@00))
3210 && !int_fits_type_p (@10, TREE_TYPE (@00)))
3213 tree min = lower_bound_in_type (TREE_TYPE (@10), TREE_TYPE (@00));
3214 tree max = upper_bound_in_type (TREE_TYPE (@10), TREE_TYPE (@00));
3215 bool above = integer_nonzerop (const_binop (LT_EXPR, type, max, @10));
3216 bool below = integer_nonzerop (const_binop (LT_EXPR, type, @10, min));
3218 (if (above || below)
3219 (if (cmp == EQ_EXPR || cmp == NE_EXPR)
3220 { constant_boolean_node (cmp == EQ_EXPR ? false : true, type); }
3221 (if (cmp == LT_EXPR || cmp == LE_EXPR)
3222 { constant_boolean_node (above ? true : false, type); }
3223 (if (cmp == GT_EXPR || cmp == GE_EXPR)
3224 { constant_boolean_node (above ? false : true, type); }))))))))))))
3227 /* A local variable can never be pointed to by
3228 the default SSA name of an incoming parameter.
3229 SSA names are canonicalized to 2nd place. */
3231 (cmp addr@0 SSA_NAME@1)
3232 (if (SSA_NAME_IS_DEFAULT_DEF (@1)
3233 && TREE_CODE (SSA_NAME_VAR (@1)) == PARM_DECL)
3234 (with { tree base = get_base_address (TREE_OPERAND (@0, 0)); }
3235 (if (TREE_CODE (base) == VAR_DECL
3236 && auto_var_in_fn_p (base, current_function_decl))
3237 (if (cmp == NE_EXPR)
3238 { constant_boolean_node (true, type); }
3239 { constant_boolean_node (false, type); }))))))
3241 /* Equality compare simplifications from fold_binary */
3244 /* If we have (A | C) == D where C & ~D != 0, convert this into 0.
3245 Similarly for NE_EXPR. */
3247 (cmp (convert?@3 (bit_ior @0 INTEGER_CST@1)) INTEGER_CST@2)
3248 (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0))
3249 && wi::bit_and_not (wi::to_wide (@1), wi::to_wide (@2)) != 0)
3250 { constant_boolean_node (cmp == NE_EXPR, type); }))
3252 /* (X ^ Y) == 0 becomes X == Y, and (X ^ Y) != 0 becomes X != Y. */
3254 (cmp (bit_xor @0 @1) integer_zerop)
3257 /* (X ^ Y) == Y becomes X == 0.
3258 Likewise (X ^ Y) == X becomes Y == 0. */
3260 (cmp:c (bit_xor:c @0 @1) @0)
3261 (cmp @1 { build_zero_cst (TREE_TYPE (@1)); }))
3263 /* (X ^ C1) op C2 can be rewritten as X op (C1 ^ C2). */
3265 (cmp (convert?@3 (bit_xor @0 INTEGER_CST@1)) INTEGER_CST@2)
3266 (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0)))
3267 (cmp @0 (bit_xor @1 (convert @2)))))
3270 (cmp (convert? addr@0) integer_zerop)
3271 (if (tree_single_nonzero_warnv_p (@0, NULL))
3272 { constant_boolean_node (cmp == NE_EXPR, type); })))
3274 /* If we have (A & C) == C where C is a power of 2, convert this into
3275 (A & C) != 0. Similarly for NE_EXPR. */
3279 (cmp (bit_and@2 @0 integer_pow2p@1) @1)
3280 (icmp @2 { build_zero_cst (TREE_TYPE (@0)); })))
3282 /* If we have (A & C) != 0 ? D : 0 where C and D are powers of 2,
3283 convert this into a shift followed by ANDing with D. */
3286 (ne (bit_and @0 integer_pow2p@1) integer_zerop)
3287 integer_pow2p@2 integer_zerop)
3289 int shift = (wi::exact_log2 (wi::to_wide (@2))
3290 - wi::exact_log2 (wi::to_wide (@1)));
3294 (lshift (convert @0) { build_int_cst (integer_type_node, shift); }) @2)
3296 (convert (rshift @0 { build_int_cst (integer_type_node, -shift); })) @2))))
3298 /* If we have (A & C) != 0 where C is the sign bit of A, convert
3299 this into A < 0. Similarly for (A & C) == 0 into A >= 0. */
3303 (cmp (bit_and (convert?@2 @0) integer_pow2p@1) integer_zerop)
3304 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
3305 && type_has_mode_precision_p (TREE_TYPE (@0))
3306 && element_precision (@2) >= element_precision (@0)
3307 && wi::only_sign_bit_p (wi::to_wide (@1), element_precision (@0)))
3308 (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
3309 (ncmp (convert:stype @0) { build_zero_cst (stype); })))))
3311 /* If we have A < 0 ? C : 0 where C is a power of 2, convert
3312 this into a right shift or sign extension followed by ANDing with C. */
3315 (lt @0 integer_zerop)
3316 integer_pow2p@1 integer_zerop)
3317 (if (!TYPE_UNSIGNED (TREE_TYPE (@0)))
3319 int shift = element_precision (@0) - wi::exact_log2 (wi::to_wide (@1)) - 1;
3323 (convert (rshift @0 { build_int_cst (integer_type_node, shift); }))
3325 /* Otherwise ctype must be wider than TREE_TYPE (@0) and pure
3326 sign extension followed by AND with C will achieve the effect. */
3327 (bit_and (convert @0) @1)))))
3329 /* When the addresses are not directly of decls compare base and offset.
3330 This implements some remaining parts of fold_comparison address
3331 comparisons but still no complete part of it. Still it is good
3332 enough to make fold_stmt not regress when not dispatching to fold_binary. */
3333 (for cmp (simple_comparison)
3335 (cmp (convert1?@2 addr@0) (convert2? addr@1))
3338 HOST_WIDE_INT off0, off1;
3339 tree base0 = get_addr_base_and_unit_offset (TREE_OPERAND (@0, 0), &off0);
3340 tree base1 = get_addr_base_and_unit_offset (TREE_OPERAND (@1, 0), &off1);
3341 if (base0 && TREE_CODE (base0) == MEM_REF)
3343 off0 += mem_ref_offset (base0).to_short_addr ();
3344 base0 = TREE_OPERAND (base0, 0);
3346 if (base1 && TREE_CODE (base1) == MEM_REF)
3348 off1 += mem_ref_offset (base1).to_short_addr ();
3349 base1 = TREE_OPERAND (base1, 0);
3352 (if (base0 && base1)
3356 /* Punt in GENERIC on variables with value expressions;
3357 the value expressions might point to fields/elements
3358 of other vars etc. */
3360 && ((VAR_P (base0) && DECL_HAS_VALUE_EXPR_P (base0))
3361 || (VAR_P (base1) && DECL_HAS_VALUE_EXPR_P (base1))))
3363 else if (decl_in_symtab_p (base0)
3364 && decl_in_symtab_p (base1))
3365 equal = symtab_node::get_create (base0)
3366 ->equal_address_to (symtab_node::get_create (base1));
3367 else if ((DECL_P (base0)
3368 || TREE_CODE (base0) == SSA_NAME
3369 || TREE_CODE (base0) == STRING_CST)
3371 || TREE_CODE (base1) == SSA_NAME
3372 || TREE_CODE (base1) == STRING_CST))
3373 equal = (base0 == base1);
3377 (if (cmp == EQ_EXPR)
3378 { constant_boolean_node (off0 == off1, type); })
3379 (if (cmp == NE_EXPR)
3380 { constant_boolean_node (off0 != off1, type); })
3381 (if (cmp == LT_EXPR)
3382 { constant_boolean_node (off0 < off1, type); })
3383 (if (cmp == LE_EXPR)
3384 { constant_boolean_node (off0 <= off1, type); })
3385 (if (cmp == GE_EXPR)
3386 { constant_boolean_node (off0 >= off1, type); })
3387 (if (cmp == GT_EXPR)
3388 { constant_boolean_node (off0 > off1, type); }))
3390 && DECL_P (base0) && DECL_P (base1)
3391 /* If we compare this as integers require equal offset. */
3392 && (!INTEGRAL_TYPE_P (TREE_TYPE (@2))
3395 (if (cmp == EQ_EXPR)
3396 { constant_boolean_node (false, type); })
3397 (if (cmp == NE_EXPR)
3398 { constant_boolean_node (true, type); })))))))))
3400 /* Simplify pointer equality compares using PTA. */
3404 (if (POINTER_TYPE_P (TREE_TYPE (@0))
3405 && ptrs_compare_unequal (@0, @1))
3406 { neeq == EQ_EXPR ? boolean_false_node : boolean_true_node; })))
3408 /* PR70920: Transform (intptr_t)x eq/ne CST to x eq/ne (typeof x) CST.
3409 and (typeof ptr_cst) x eq/ne ptr_cst to x eq/ne (typeof x) CST.
3410 Disable the transform if either operand is pointer to function.
3411 This broke pr22051-2.c for arm where function pointer
3412 canonicalizaion is not wanted. */
3416 (cmp (convert @0) INTEGER_CST@1)
3417 (if ((POINTER_TYPE_P (TREE_TYPE (@0)) && !FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@0)))
3418 && INTEGRAL_TYPE_P (TREE_TYPE (@1)))
3419 || (INTEGRAL_TYPE_P (TREE_TYPE (@0)) && POINTER_TYPE_P (TREE_TYPE (@1))
3420 && !FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@1)))))
3421 (cmp @0 (convert @1)))))
3423 /* Non-equality compare simplifications from fold_binary */
3424 (for cmp (lt gt le ge)
3425 /* Comparisons with the highest or lowest possible integer of
3426 the specified precision will have known values. */
3428 (cmp (convert?@2 @0) INTEGER_CST@1)
3429 (if ((INTEGRAL_TYPE_P (TREE_TYPE (@1)) || POINTER_TYPE_P (TREE_TYPE (@1)))
3430 && tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@0)))
3433 tree arg1_type = TREE_TYPE (@1);
3434 unsigned int prec = TYPE_PRECISION (arg1_type);
3435 wide_int max = wi::max_value (arg1_type);
3436 wide_int signed_max = wi::max_value (prec, SIGNED);
3437 wide_int min = wi::min_value (arg1_type);
3440 (if (wi::to_wide (@1) == max)
3442 (if (cmp == GT_EXPR)
3443 { constant_boolean_node (false, type); })
3444 (if (cmp == GE_EXPR)
3446 (if (cmp == LE_EXPR)
3447 { constant_boolean_node (true, type); })
3448 (if (cmp == LT_EXPR)
3450 (if (wi::to_wide (@1) == min)
3452 (if (cmp == LT_EXPR)
3453 { constant_boolean_node (false, type); })
3454 (if (cmp == LE_EXPR)
3456 (if (cmp == GE_EXPR)
3457 { constant_boolean_node (true, type); })
3458 (if (cmp == GT_EXPR)
3460 (if (wi::to_wide (@1) == max - 1)
3462 (if (cmp == GT_EXPR)
3463 (eq @2 { wide_int_to_tree (TREE_TYPE (@1), wi::to_wide (@1) + 1); }))
3464 (if (cmp == LE_EXPR)
3465 (ne @2 { wide_int_to_tree (TREE_TYPE (@1), wi::to_wide (@1) + 1); }))))
3466 (if (wi::to_wide (@1) == min + 1)
3468 (if (cmp == GE_EXPR)
3469 (ne @2 { wide_int_to_tree (TREE_TYPE (@1), wi::to_wide (@1) - 1); }))
3470 (if (cmp == LT_EXPR)
3471 (eq @2 { wide_int_to_tree (TREE_TYPE (@1), wi::to_wide (@1) - 1); }))))
3472 (if (wi::to_wide (@1) == signed_max
3473 && TYPE_UNSIGNED (arg1_type)
3474 /* We will flip the signedness of the comparison operator
3475 associated with the mode of @1, so the sign bit is
3476 specified by this mode. Check that @1 is the signed
3477 max associated with this sign bit. */
3478 && prec == GET_MODE_PRECISION (SCALAR_INT_TYPE_MODE (arg1_type))
3479 /* signed_type does not work on pointer types. */
3480 && INTEGRAL_TYPE_P (arg1_type))
3481 /* The following case also applies to X < signed_max+1
3482 and X >= signed_max+1 because previous transformations. */
3483 (if (cmp == LE_EXPR || cmp == GT_EXPR)
3484 (with { tree st = signed_type_for (arg1_type); }
3485 (if (cmp == LE_EXPR)
3486 (ge (convert:st @0) { build_zero_cst (st); })
3487 (lt (convert:st @0) { build_zero_cst (st); }))))))))))
3489 (for cmp (unordered ordered unlt unle ungt unge uneq ltgt)
3490 /* If the second operand is NaN, the result is constant. */
3493 (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
3494 && (cmp != LTGT_EXPR || ! flag_trapping_math))
3495 { constant_boolean_node (cmp == ORDERED_EXPR || cmp == LTGT_EXPR
3496 ? false : true, type); })))
3498 /* bool_var != 0 becomes bool_var. */
3500 (ne @0 integer_zerop)
3501 (if (TREE_CODE (TREE_TYPE (@0)) == BOOLEAN_TYPE
3502 && types_match (type, TREE_TYPE (@0)))
3504 /* bool_var == 1 becomes bool_var. */
3506 (eq @0 integer_onep)
3507 (if (TREE_CODE (TREE_TYPE (@0)) == BOOLEAN_TYPE
3508 && types_match (type, TREE_TYPE (@0)))
3511 bool_var == 0 becomes !bool_var or
3512 bool_var != 1 becomes !bool_var
3513 here because that only is good in assignment context as long
3514 as we require a tcc_comparison in GIMPLE_CONDs where we'd
3515 replace if (x == 0) with tem = ~x; if (tem != 0) which is
3516 clearly less optimal and which we'll transform again in forwprop. */
3518 /* When one argument is a constant, overflow detection can be simplified.
3519 Currently restricted to single use so as not to interfere too much with
3520 ADD_OVERFLOW detection in tree-ssa-math-opts.c.
3521 A + CST CMP A -> A CMP' CST' */
3522 (for cmp (lt le ge gt)
3525 (cmp:c (plus@2 @0 INTEGER_CST@1) @0)
3526 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
3527 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))
3528 && wi::to_wide (@1) != 0
3530 (with { unsigned int prec = TYPE_PRECISION (TREE_TYPE (@0)); }
3531 (out @0 { wide_int_to_tree (TREE_TYPE (@0),
3532 wi::max_value (prec, UNSIGNED)
3533 - wi::to_wide (@1)); })))))
3535 /* To detect overflow in unsigned A - B, A < B is simpler than A - B > A.
3536 However, the detection logic for SUB_OVERFLOW in tree-ssa-math-opts.c
3537 expects the long form, so we restrict the transformation for now. */
3540 (cmp:c (minus@2 @0 @1) @0)
3541 (if (single_use (@2)
3542 && ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3543 && TYPE_UNSIGNED (TREE_TYPE (@0))
3544 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
3547 /* Testing for overflow is unnecessary if we already know the result. */
3552 (cmp:c (realpart (IFN_SUB_OVERFLOW@2 @0 @1)) @0)
3553 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
3554 && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
3555 (out (imagpart @2) { build_zero_cst (TREE_TYPE (@0)); }))))
3560 (cmp:c (realpart (IFN_ADD_OVERFLOW:c@2 @0 @1)) @0)
3561 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
3562 && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
3563 (out (imagpart @2) { build_zero_cst (TREE_TYPE (@0)); }))))
3565 /* For unsigned operands, -1 / B < A checks whether A * B would overflow.
3566 Simplify it to __builtin_mul_overflow (A, B, <unused>). */
3570 (cmp:c (trunc_div:s integer_all_onesp @1) @0)
3571 (if (TYPE_UNSIGNED (TREE_TYPE (@0)) && !VECTOR_TYPE_P (TREE_TYPE (@0)))
3572 (with { tree t = TREE_TYPE (@0), cpx = build_complex_type (t); }
3573 (out (imagpart (IFN_MUL_OVERFLOW:cpx @0 @1)) { build_zero_cst (t); })))))
3575 /* Simplification of math builtins. These rules must all be optimizations
3576 as well as IL simplifications. If there is a possibility that the new
3577 form could be a pessimization, the rule should go in the canonicalization
3578 section that follows this one.
3580 Rules can generally go in this section if they satisfy one of
3583 - the rule describes an identity
3585 - the rule replaces calls with something as simple as addition or
3588 - the rule contains unary calls only and simplifies the surrounding
3589 arithmetic. (The idea here is to exclude non-unary calls in which
3590 one operand is constant and in which the call is known to be cheap
3591 when the operand has that value.) */
3593 (if (flag_unsafe_math_optimizations)
3594 /* Simplify sqrt(x) * sqrt(x) -> x. */
3596 (mult (SQRT@1 @0) @1)
3597 (if (!HONOR_SNANS (type))
3600 (for op (plus minus)
3601 /* Simplify (A / C) +- (B / C) -> (A +- B) / C. */
3605 (rdiv (op @0 @2) @1)))
3607 /* Simplify sqrt(x) * sqrt(y) -> sqrt(x*y). */
3608 (for root (SQRT CBRT)
3610 (mult (root:s @0) (root:s @1))
3611 (root (mult @0 @1))))
3613 /* Simplify expN(x) * expN(y) -> expN(x+y). */
3614 (for exps (EXP EXP2 EXP10 POW10)
3616 (mult (exps:s @0) (exps:s @1))
3617 (exps (plus @0 @1))))
3619 /* Simplify a/root(b/c) into a*root(c/b). */
3620 (for root (SQRT CBRT)
3622 (rdiv @0 (root:s (rdiv:s @1 @2)))
3623 (mult @0 (root (rdiv @2 @1)))))
3625 /* Simplify x/expN(y) into x*expN(-y). */
3626 (for exps (EXP EXP2 EXP10 POW10)
3628 (rdiv @0 (exps:s @1))
3629 (mult @0 (exps (negate @1)))))
3631 (for logs (LOG LOG2 LOG10 LOG10)
3632 exps (EXP EXP2 EXP10 POW10)
3633 /* logN(expN(x)) -> x. */
3637 /* expN(logN(x)) -> x. */
3642 /* Optimize logN(func()) for various exponential functions. We
3643 want to determine the value "x" and the power "exponent" in
3644 order to transform logN(x**exponent) into exponent*logN(x). */
3645 (for logs (LOG LOG LOG LOG2 LOG2 LOG2 LOG10 LOG10)
3646 exps (EXP2 EXP10 POW10 EXP EXP10 POW10 EXP EXP2)
3649 (if (SCALAR_FLOAT_TYPE_P (type))
3655 /* Prepare to do logN(exp(exponent)) -> exponent*logN(e). */
3656 x = build_real_truncate (type, dconst_e ());
3659 /* Prepare to do logN(exp2(exponent)) -> exponent*logN(2). */
3660 x = build_real (type, dconst2);
3664 /* Prepare to do logN(exp10(exponent)) -> exponent*logN(10). */
3666 REAL_VALUE_TYPE dconst10;
3667 real_from_integer (&dconst10, VOIDmode, 10, SIGNED);
3668 x = build_real (type, dconst10);
3675 (mult (logs { x; }) @0)))))
3683 (if (SCALAR_FLOAT_TYPE_P (type))
3689 /* Prepare to do logN(sqrt(x)) -> 0.5*logN(x). */
3690 x = build_real (type, dconsthalf);
3693 /* Prepare to do logN(cbrt(x)) -> (1/3)*logN(x). */
3694 x = build_real_truncate (type, dconst_third ());
3700 (mult { x; } (logs @0))))))
3702 /* logN(pow(x,exponent)) -> exponent*logN(x). */
3703 (for logs (LOG LOG2 LOG10)
3707 (mult @1 (logs @0))))
3709 /* pow(C,x) -> exp(log(C)*x) if C > 0. */
3714 (pows REAL_CST@0 @1)
3715 (if (real_compare (GT_EXPR, TREE_REAL_CST_PTR (@0), &dconst0)
3716 && real_isfinite (TREE_REAL_CST_PTR (@0)))
3717 (exps (mult (logs @0) @1)))))
3722 exps (EXP EXP2 EXP10 POW10)
3723 /* sqrt(expN(x)) -> expN(x*0.5). */
3726 (exps (mult @0 { build_real (type, dconsthalf); })))
3727 /* cbrt(expN(x)) -> expN(x/3). */
3730 (exps (mult @0 { build_real_truncate (type, dconst_third ()); })))
3731 /* pow(expN(x), y) -> expN(x*y). */
3734 (exps (mult @0 @1))))
3736 /* tan(atan(x)) -> x. */
3743 /* cabs(x+0i) or cabs(0+xi) -> abs(x). */
3745 (CABS (complex:C @0 real_zerop@1))
3748 /* trunc(trunc(x)) -> trunc(x), etc. */
3749 (for fns (TRUNC FLOOR CEIL ROUND NEARBYINT RINT)
3753 /* f(x) -> x if x is integer valued and f does nothing for such values. */
3754 (for fns (TRUNC FLOOR CEIL ROUND NEARBYINT RINT)
3756 (fns integer_valued_real_p@0)
3759 /* hypot(x,0) and hypot(0,x) -> abs(x). */
3761 (HYPOT:c @0 real_zerop@1)
3764 /* pow(1,x) -> 1. */
3766 (POW real_onep@0 @1)
3770 /* copysign(x,x) -> x. */
3775 /* copysign(x,y) -> fabs(x) if y is nonnegative. */
3776 (COPYSIGN @0 tree_expr_nonnegative_p@1)
3779 (for scale (LDEXP SCALBN SCALBLN)
3780 /* ldexp(0, x) -> 0. */
3782 (scale real_zerop@0 @1)
3784 /* ldexp(x, 0) -> x. */
3786 (scale @0 integer_zerop@1)
3788 /* ldexp(x, y) -> x if x is +-Inf or NaN. */
3790 (scale REAL_CST@0 @1)
3791 (if (!real_isfinite (TREE_REAL_CST_PTR (@0)))
3794 /* Canonicalization of sequences of math builtins. These rules represent
3795 IL simplifications but are not necessarily optimizations.
3797 The sincos pass is responsible for picking "optimal" implementations
3798 of math builtins, which may be more complicated and can sometimes go
3799 the other way, e.g. converting pow into a sequence of sqrts.
3800 We only want to do these canonicalizations before the pass has run. */
3802 (if (flag_unsafe_math_optimizations && canonicalize_math_p ())
3803 /* Simplify tan(x) * cos(x) -> sin(x). */
3805 (mult:c (TAN:s @0) (COS:s @0))
3808 /* Simplify x * pow(x,c) -> pow(x,c+1). */
3810 (mult:c @0 (POW:s @0 REAL_CST@1))
3811 (if (!TREE_OVERFLOW (@1))
3812 (POW @0 (plus @1 { build_one_cst (type); }))))
3814 /* Simplify sin(x) / cos(x) -> tan(x). */
3816 (rdiv (SIN:s @0) (COS:s @0))
3819 /* Simplify cos(x) / sin(x) -> 1 / tan(x). */
3821 (rdiv (COS:s @0) (SIN:s @0))
3822 (rdiv { build_one_cst (type); } (TAN @0)))
3824 /* Simplify sin(x) / tan(x) -> cos(x). */
3826 (rdiv (SIN:s @0) (TAN:s @0))
3827 (if (! HONOR_NANS (@0)
3828 && ! HONOR_INFINITIES (@0))
3831 /* Simplify tan(x) / sin(x) -> 1.0 / cos(x). */
3833 (rdiv (TAN:s @0) (SIN:s @0))
3834 (if (! HONOR_NANS (@0)
3835 && ! HONOR_INFINITIES (@0))
3836 (rdiv { build_one_cst (type); } (COS @0))))
3838 /* Simplify pow(x,y) * pow(x,z) -> pow(x,y+z). */
3840 (mult (POW:s @0 @1) (POW:s @0 @2))
3841 (POW @0 (plus @1 @2)))
3843 /* Simplify pow(x,y) * pow(z,y) -> pow(x*z,y). */
3845 (mult (POW:s @0 @1) (POW:s @2 @1))
3846 (POW (mult @0 @2) @1))
3848 /* Simplify powi(x,y) * powi(z,y) -> powi(x*z,y). */
3850 (mult (POWI:s @0 @1) (POWI:s @2 @1))
3851 (POWI (mult @0 @2) @1))
3853 /* Simplify pow(x,c) / x -> pow(x,c-1). */
3855 (rdiv (POW:s @0 REAL_CST@1) @0)
3856 (if (!TREE_OVERFLOW (@1))
3857 (POW @0 (minus @1 { build_one_cst (type); }))))
3859 /* Simplify x / pow (y,z) -> x * pow(y,-z). */
3861 (rdiv @0 (POW:s @1 @2))
3862 (mult @0 (POW @1 (negate @2))))
3867 /* sqrt(sqrt(x)) -> pow(x,1/4). */
3870 (pows @0 { build_real (type, dconst_quarter ()); }))
3871 /* sqrt(cbrt(x)) -> pow(x,1/6). */
3874 (pows @0 { build_real_truncate (type, dconst_sixth ()); }))
3875 /* cbrt(sqrt(x)) -> pow(x,1/6). */
3878 (pows @0 { build_real_truncate (type, dconst_sixth ()); }))
3879 /* cbrt(cbrt(x)) -> pow(x,1/9), iff x is nonnegative. */
3881 (cbrts (cbrts tree_expr_nonnegative_p@0))
3882 (pows @0 { build_real_truncate (type, dconst_ninth ()); }))
3883 /* sqrt(pow(x,y)) -> pow(|x|,y*0.5). */
3885 (sqrts (pows @0 @1))
3886 (pows (abs @0) (mult @1 { build_real (type, dconsthalf); })))
3887 /* cbrt(pow(x,y)) -> pow(x,y/3), iff x is nonnegative. */
3889 (cbrts (pows tree_expr_nonnegative_p@0 @1))
3890 (pows @0 (mult @1 { build_real_truncate (type, dconst_third ()); })))
3891 /* pow(sqrt(x),y) -> pow(x,y*0.5). */
3893 (pows (sqrts @0) @1)
3894 (pows @0 (mult @1 { build_real (type, dconsthalf); })))
3895 /* pow(cbrt(x),y) -> pow(x,y/3) iff x is nonnegative. */
3897 (pows (cbrts tree_expr_nonnegative_p@0) @1)
3898 (pows @0 (mult @1 { build_real_truncate (type, dconst_third ()); })))
3899 /* pow(pow(x,y),z) -> pow(x,y*z) iff x is nonnegative. */
3901 (pows (pows tree_expr_nonnegative_p@0 @1) @2)
3902 (pows @0 (mult @1 @2))))
3904 /* cabs(x+xi) -> fabs(x)*sqrt(2). */
3906 (CABS (complex @0 @0))
3907 (mult (abs @0) { build_real_truncate (type, dconst_sqrt2 ()); }))
3909 /* hypot(x,x) -> fabs(x)*sqrt(2). */
3912 (mult (abs @0) { build_real_truncate (type, dconst_sqrt2 ()); }))
3914 /* cexp(x+yi) -> exp(x)*cexpi(y). */
3919 (cexps compositional_complex@0)
3920 (if (targetm.libc_has_function (function_c99_math_complex))
3922 (mult (exps@1 (realpart @0)) (realpart (cexpis:type@2 (imagpart @0))))
3923 (mult @1 (imagpart @2)))))))
3925 (if (canonicalize_math_p ())
3926 /* floor(x) -> trunc(x) if x is nonnegative. */
3930 (floors tree_expr_nonnegative_p@0)
3933 (match double_value_p
3935 (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == double_type_node)))
3936 (for froms (BUILT_IN_TRUNCL
3948 /* truncl(extend(x)) -> extend(trunc(x)), etc., if x is a double. */
3949 (if (optimize && canonicalize_math_p ())
3951 (froms (convert double_value_p@0))
3952 (convert (tos @0)))))
3954 (match float_value_p
3956 (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == float_type_node)))
3957 (for froms (BUILT_IN_TRUNCL BUILT_IN_TRUNC
3958 BUILT_IN_FLOORL BUILT_IN_FLOOR
3959 BUILT_IN_CEILL BUILT_IN_CEIL
3960 BUILT_IN_ROUNDL BUILT_IN_ROUND
3961 BUILT_IN_NEARBYINTL BUILT_IN_NEARBYINT
3962 BUILT_IN_RINTL BUILT_IN_RINT)
3963 tos (BUILT_IN_TRUNCF BUILT_IN_TRUNCF
3964 BUILT_IN_FLOORF BUILT_IN_FLOORF
3965 BUILT_IN_CEILF BUILT_IN_CEILF
3966 BUILT_IN_ROUNDF BUILT_IN_ROUNDF
3967 BUILT_IN_NEARBYINTF BUILT_IN_NEARBYINTF
3968 BUILT_IN_RINTF BUILT_IN_RINTF)
3969 /* truncl(extend(x)) and trunc(extend(x)) -> extend(truncf(x)), etc.,
3971 (if (optimize && canonicalize_math_p ()
3972 && targetm.libc_has_function (function_c99_misc))
3974 (froms (convert float_value_p@0))
3975 (convert (tos @0)))))
3977 (for froms (XFLOORL XCEILL XROUNDL XRINTL)
3978 tos (XFLOOR XCEIL XROUND XRINT)
3979 /* llfloorl(extend(x)) -> llfloor(x), etc., if x is a double. */
3980 (if (optimize && canonicalize_math_p ())
3982 (froms (convert double_value_p@0))
3985 (for froms (XFLOORL XCEILL XROUNDL XRINTL
3986 XFLOOR XCEIL XROUND XRINT)
3987 tos (XFLOORF XCEILF XROUNDF XRINTF)
3988 /* llfloorl(extend(x)) and llfloor(extend(x)) -> llfloorf(x), etc.,
3990 (if (optimize && canonicalize_math_p ())
3992 (froms (convert float_value_p@0))
3995 (if (canonicalize_math_p ())
3996 /* xfloor(x) -> fix_trunc(x) if x is nonnegative. */
3997 (for floors (IFLOOR LFLOOR LLFLOOR)
3999 (floors tree_expr_nonnegative_p@0)
4002 (if (canonicalize_math_p ())
4003 /* xfloor(x) -> fix_trunc(x), etc., if x is integer valued. */
4004 (for fns (IFLOOR LFLOOR LLFLOOR
4006 IROUND LROUND LLROUND)
4008 (fns integer_valued_real_p@0)
4010 (if (!flag_errno_math)
4011 /* xrint(x) -> fix_trunc(x), etc., if x is integer valued. */
4012 (for rints (IRINT LRINT LLRINT)
4014 (rints integer_valued_real_p@0)
4017 (if (canonicalize_math_p ())
4018 (for ifn (IFLOOR ICEIL IROUND IRINT)
4019 lfn (LFLOOR LCEIL LROUND LRINT)
4020 llfn (LLFLOOR LLCEIL LLROUND LLRINT)
4021 /* Canonicalize iround (x) to lround (x) on ILP32 targets where
4022 sizeof (int) == sizeof (long). */
4023 (if (TYPE_PRECISION (integer_type_node)
4024 == TYPE_PRECISION (long_integer_type_node))
4027 (lfn:long_integer_type_node @0)))
4028 /* Canonicalize llround (x) to lround (x) on LP64 targets where
4029 sizeof (long long) == sizeof (long). */
4030 (if (TYPE_PRECISION (long_long_integer_type_node)
4031 == TYPE_PRECISION (long_integer_type_node))
4034 (lfn:long_integer_type_node @0)))))
4036 /* cproj(x) -> x if we're ignoring infinities. */
4039 (if (!HONOR_INFINITIES (type))
4042 /* If the real part is inf and the imag part is known to be
4043 nonnegative, return (inf + 0i). */
4045 (CPROJ (complex REAL_CST@0 tree_expr_nonnegative_p@1))
4046 (if (real_isinf (TREE_REAL_CST_PTR (@0)))
4047 { build_complex_inf (type, false); }))
4049 /* If the imag part is inf, return (inf+I*copysign(0,imag)). */
4051 (CPROJ (complex @0 REAL_CST@1))
4052 (if (real_isinf (TREE_REAL_CST_PTR (@1)))
4053 { build_complex_inf (type, TREE_REAL_CST_PTR (@1)->sign); }))
4059 (pows @0 REAL_CST@1)
4061 const REAL_VALUE_TYPE *value = TREE_REAL_CST_PTR (@1);
4062 REAL_VALUE_TYPE tmp;
4065 /* pow(x,0) -> 1. */
4066 (if (real_equal (value, &dconst0))
4067 { build_real (type, dconst1); })
4068 /* pow(x,1) -> x. */
4069 (if (real_equal (value, &dconst1))
4071 /* pow(x,-1) -> 1/x. */
4072 (if (real_equal (value, &dconstm1))
4073 (rdiv { build_real (type, dconst1); } @0))
4074 /* pow(x,0.5) -> sqrt(x). */
4075 (if (flag_unsafe_math_optimizations
4076 && canonicalize_math_p ()
4077 && real_equal (value, &dconsthalf))
4079 /* pow(x,1/3) -> cbrt(x). */
4080 (if (flag_unsafe_math_optimizations
4081 && canonicalize_math_p ()
4082 && (tmp = real_value_truncate (TYPE_MODE (type), dconst_third ()),
4083 real_equal (value, &tmp)))
4086 /* powi(1,x) -> 1. */
4088 (POWI real_onep@0 @1)
4092 (POWI @0 INTEGER_CST@1)
4094 /* powi(x,0) -> 1. */
4095 (if (wi::to_wide (@1) == 0)
4096 { build_real (type, dconst1); })
4097 /* powi(x,1) -> x. */
4098 (if (wi::to_wide (@1) == 1)
4100 /* powi(x,-1) -> 1/x. */
4101 (if (wi::to_wide (@1) == -1)
4102 (rdiv { build_real (type, dconst1); } @0))))
4104 /* Narrowing of arithmetic and logical operations.
4106 These are conceptually similar to the transformations performed for
4107 the C/C++ front-ends by shorten_binary_op and shorten_compare. Long
4108 term we want to move all that code out of the front-ends into here. */
4110 /* If we have a narrowing conversion of an arithmetic operation where
4111 both operands are widening conversions from the same type as the outer
4112 narrowing conversion. Then convert the innermost operands to a suitable
4113 unsigned type (to avoid introducing undefined behavior), perform the
4114 operation and convert the result to the desired type. */
4115 (for op (plus minus)
4117 (convert (op:s (convert@2 @0) (convert?@3 @1)))
4118 (if (INTEGRAL_TYPE_P (type)
4119 /* We check for type compatibility between @0 and @1 below,
4120 so there's no need to check that @1/@3 are integral types. */
4121 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
4122 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
4123 /* The precision of the type of each operand must match the
4124 precision of the mode of each operand, similarly for the
4126 && type_has_mode_precision_p (TREE_TYPE (@0))
4127 && type_has_mode_precision_p (TREE_TYPE (@1))
4128 && type_has_mode_precision_p (type)
4129 /* The inner conversion must be a widening conversion. */
4130 && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
4131 && types_match (@0, type)
4132 && (types_match (@0, @1)
4133 /* Or the second operand is const integer or converted const
4134 integer from valueize. */
4135 || TREE_CODE (@1) == INTEGER_CST))
4136 (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
4137 (op @0 (convert @1))
4138 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
4139 (convert (op (convert:utype @0)
4140 (convert:utype @1))))))))
4142 /* This is another case of narrowing, specifically when there's an outer
4143 BIT_AND_EXPR which masks off bits outside the type of the innermost
4144 operands. Like the previous case we have to convert the operands
4145 to unsigned types to avoid introducing undefined behavior for the
4146 arithmetic operation. */
4147 (for op (minus plus)
4149 (bit_and (op:s (convert@2 @0) (convert@3 @1)) INTEGER_CST@4)
4150 (if (INTEGRAL_TYPE_P (type)
4151 /* We check for type compatibility between @0 and @1 below,
4152 so there's no need to check that @1/@3 are integral types. */
4153 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
4154 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
4155 /* The precision of the type of each operand must match the
4156 precision of the mode of each operand, similarly for the
4158 && type_has_mode_precision_p (TREE_TYPE (@0))
4159 && type_has_mode_precision_p (TREE_TYPE (@1))
4160 && type_has_mode_precision_p (type)
4161 /* The inner conversion must be a widening conversion. */
4162 && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
4163 && types_match (@0, @1)
4164 && (tree_int_cst_min_precision (@4, TYPE_SIGN (TREE_TYPE (@0)))
4165 <= TYPE_PRECISION (TREE_TYPE (@0)))
4166 && (wi::to_wide (@4)
4167 & wi::mask (TYPE_PRECISION (TREE_TYPE (@0)),
4168 true, TYPE_PRECISION (type))) == 0)
4169 (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
4170 (with { tree ntype = TREE_TYPE (@0); }
4171 (convert (bit_and (op @0 @1) (convert:ntype @4))))
4172 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
4173 (convert (bit_and (op (convert:utype @0) (convert:utype @1))
4174 (convert:utype @4))))))))
4176 /* Transform (@0 < @1 and @0 < @2) to use min,
4177 (@0 > @1 and @0 > @2) to use max */
4178 (for op (lt le gt ge)
4179 ext (min min max max)
4181 (bit_and (op:cs @0 @1) (op:cs @0 @2))
4182 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
4183 && TREE_CODE (@0) != INTEGER_CST)
4184 (op @0 (ext @1 @2)))))
4187 /* signbit(x) -> 0 if x is nonnegative. */
4188 (SIGNBIT tree_expr_nonnegative_p@0)
4189 { integer_zero_node; })
4192 /* signbit(x) -> x<0 if x doesn't have signed zeros. */
4194 (if (!HONOR_SIGNED_ZEROS (@0))
4195 (convert (lt @0 { build_real (TREE_TYPE (@0), dconst0); }))))
4197 /* Transform comparisons of the form X +- C1 CMP C2 to X CMP C2 -+ C1. */
4199 (for op (plus minus)
4202 (cmp (op@3 @0 INTEGER_CST@1) INTEGER_CST@2)
4203 (if (!TREE_OVERFLOW (@1) && !TREE_OVERFLOW (@2)
4204 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0))
4205 && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@0))
4206 && !TYPE_SATURATING (TREE_TYPE (@0)))
4207 (with { tree res = int_const_binop (rop, @2, @1); }
4208 (if (TREE_OVERFLOW (res)
4209 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
4210 { constant_boolean_node (cmp == NE_EXPR, type); }
4211 (if (single_use (@3))
4212 (cmp @0 { res; }))))))))
4213 (for cmp (lt le gt ge)
4214 (for op (plus minus)
4217 (cmp (op@3 @0 INTEGER_CST@1) INTEGER_CST@2)
4218 (if (!TREE_OVERFLOW (@1) && !TREE_OVERFLOW (@2)
4219 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
4220 (with { tree res = int_const_binop (rop, @2, @1); }
4221 (if (TREE_OVERFLOW (res))
4223 fold_overflow_warning (("assuming signed overflow does not occur "
4224 "when simplifying conditional to constant"),
4225 WARN_STRICT_OVERFLOW_CONDITIONAL);
4226 bool less = cmp == LE_EXPR || cmp == LT_EXPR;
4227 /* wi::ges_p (@2, 0) should be sufficient for a signed type. */
4228 bool ovf_high = wi::lt_p (wi::to_wide (@1), 0,
4229 TYPE_SIGN (TREE_TYPE (@1)))
4230 != (op == MINUS_EXPR);
4231 constant_boolean_node (less == ovf_high, type);
4233 (if (single_use (@3))
4236 fold_overflow_warning (("assuming signed overflow does not occur "
4237 "when changing X +- C1 cmp C2 to "
4239 WARN_STRICT_OVERFLOW_COMPARISON);
4241 (cmp @0 { res; })))))))))
4243 /* Canonicalizations of BIT_FIELD_REFs. */
4246 (BIT_FIELD_REF @0 @1 @2)
4248 (if (TREE_CODE (TREE_TYPE (@0)) == COMPLEX_TYPE
4249 && tree_int_cst_equal (@1, TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))))
4251 (if (integer_zerop (@2))
4252 (view_convert (realpart @0)))
4253 (if (tree_int_cst_equal (@2, TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))))
4254 (view_convert (imagpart @0)))))
4255 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
4256 && INTEGRAL_TYPE_P (type)
4257 /* On GIMPLE this should only apply to register arguments. */
4258 && (! GIMPLE || is_gimple_reg (@0))
4259 /* A bit-field-ref that referenced the full argument can be stripped. */
4260 && ((compare_tree_int (@1, TYPE_PRECISION (TREE_TYPE (@0))) == 0
4261 && integer_zerop (@2))
4262 /* Low-parts can be reduced to integral conversions.
4263 ??? The following doesn't work for PDP endian. */
4264 || (BYTES_BIG_ENDIAN == WORDS_BIG_ENDIAN
4265 /* Don't even think about BITS_BIG_ENDIAN. */
4266 && TYPE_PRECISION (TREE_TYPE (@0)) % BITS_PER_UNIT == 0
4267 && TYPE_PRECISION (type) % BITS_PER_UNIT == 0
4268 && compare_tree_int (@2, (BYTES_BIG_ENDIAN
4269 ? (TYPE_PRECISION (TREE_TYPE (@0))
4270 - TYPE_PRECISION (type))
4274 /* Simplify vector extracts. */
4277 (BIT_FIELD_REF CONSTRUCTOR@0 @1 @2)
4278 (if (VECTOR_TYPE_P (TREE_TYPE (@0))
4279 && (types_match (type, TREE_TYPE (TREE_TYPE (@0)))
4280 || (VECTOR_TYPE_P (type)
4281 && types_match (TREE_TYPE (type), TREE_TYPE (TREE_TYPE (@0))))))
4284 tree ctor = (TREE_CODE (@0) == SSA_NAME
4285 ? gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0)) : @0);
4286 tree eltype = TREE_TYPE (TREE_TYPE (ctor));
4287 unsigned HOST_WIDE_INT width = tree_to_uhwi (TYPE_SIZE (eltype));
4288 unsigned HOST_WIDE_INT n = tree_to_uhwi (@1);
4289 unsigned HOST_WIDE_INT idx = tree_to_uhwi (@2);
4292 && (idx % width) == 0
4294 && ((idx + n) / width) <= TYPE_VECTOR_SUBPARTS (TREE_TYPE (ctor)))
4299 /* Constructor elements can be subvectors. */
4300 unsigned HOST_WIDE_INT k = 1;
4301 if (CONSTRUCTOR_NELTS (ctor) != 0)
4303 tree cons_elem = TREE_TYPE (CONSTRUCTOR_ELT (ctor, 0)->value);
4304 if (TREE_CODE (cons_elem) == VECTOR_TYPE)
4305 k = TYPE_VECTOR_SUBPARTS (cons_elem);
4309 /* We keep an exact subset of the constructor elements. */
4310 (if ((idx % k) == 0 && (n % k) == 0)
4311 (if (CONSTRUCTOR_NELTS (ctor) == 0)
4312 { build_constructor (type, NULL); }
4319 (if (idx < CONSTRUCTOR_NELTS (ctor))
4320 { CONSTRUCTOR_ELT (ctor, idx)->value; }
4321 { build_zero_cst (type); })
4323 vec<constructor_elt, va_gc> *vals;
4324 vec_alloc (vals, n);
4325 for (unsigned i = 0;
4326 i < n && idx + i < CONSTRUCTOR_NELTS (ctor); ++i)
4327 CONSTRUCTOR_APPEND_ELT (vals, NULL_TREE,
4328 CONSTRUCTOR_ELT (ctor, idx + i)->value);
4329 build_constructor (type, vals);
4331 /* The bitfield references a single constructor element. */
4332 (if (idx + n <= (idx / k + 1) * k)
4334 (if (CONSTRUCTOR_NELTS (ctor) <= idx / k)
4335 { build_zero_cst (type); })
4337 { CONSTRUCTOR_ELT (ctor, idx / k)->value; })
4338 (BIT_FIELD_REF { CONSTRUCTOR_ELT (ctor, idx / k)->value; }
4339 @1 { bitsize_int ((idx % k) * width); })))))))))
4341 /* Simplify a bit extraction from a bit insertion for the cases with
4342 the inserted element fully covering the extraction or the insertion
4343 not touching the extraction. */
4345 (BIT_FIELD_REF (bit_insert @0 @1 @ipos) @rsize @rpos)
4348 unsigned HOST_WIDE_INT isize;
4349 if (INTEGRAL_TYPE_P (TREE_TYPE (@1)))
4350 isize = TYPE_PRECISION (TREE_TYPE (@1));
4352 isize = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (@1)));
4355 (if (wi::leu_p (wi::to_wide (@ipos), wi::to_wide (@rpos))
4356 && wi::leu_p (wi::to_wide (@rpos) + wi::to_wide (@rsize),
4357 wi::to_wide (@ipos) + isize))
4358 (BIT_FIELD_REF @1 @rsize { wide_int_to_tree (bitsizetype,
4360 - wi::to_wide (@ipos)); }))
4361 (if (wi::geu_p (wi::to_wide (@ipos),
4362 wi::to_wide (@rpos) + wi::to_wide (@rsize))
4363 || wi::geu_p (wi::to_wide (@rpos),
4364 wi::to_wide (@ipos) + isize))
4365 (BIT_FIELD_REF @0 @rsize @rpos)))))