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 /* Simplify x / (- y) to -x / y. */
359 (rdiv @0 (negate @1))
360 (rdiv (negate @0) @1))
362 /* Optimize (X & (-A)) / A where A is a power of 2, to X >> log2(A) */
363 (for div (trunc_div ceil_div floor_div round_div exact_div)
365 (div (convert? (bit_and @0 INTEGER_CST@1)) INTEGER_CST@2)
366 (if (integer_pow2p (@2)
367 && tree_int_cst_sgn (@2) > 0
368 && tree_nop_conversion_p (type, TREE_TYPE (@0))
369 && wi::to_wide (@2) + wi::to_wide (@1) == 0)
371 { build_int_cst (integer_type_node,
372 wi::exact_log2 (wi::to_wide (@2))); }))))
374 /* If ARG1 is a constant, we can convert this to a multiply by the
375 reciprocal. This does not have the same rounding properties,
376 so only do this if -freciprocal-math. We can actually
377 always safely do it if ARG1 is a power of two, but it's hard to
378 tell if it is or not in a portable manner. */
379 (for cst (REAL_CST COMPLEX_CST VECTOR_CST)
383 (if (flag_reciprocal_math
386 { tree tem = const_binop (RDIV_EXPR, type, build_one_cst (type), @1); }
388 (mult @0 { tem; } )))
389 (if (cst != COMPLEX_CST)
390 (with { tree inverse = exact_inverse (type, @1); }
392 (mult @0 { inverse; } ))))))))
394 (for mod (ceil_mod floor_mod round_mod trunc_mod)
395 /* 0 % X is always zero. */
397 (mod integer_zerop@0 @1)
398 /* But not for 0 % 0 so that we can get the proper warnings and errors. */
399 (if (!integer_zerop (@1))
401 /* X % 1 is always zero. */
403 (mod @0 integer_onep)
404 { build_zero_cst (type); })
405 /* X % -1 is zero. */
407 (mod @0 integer_minus_onep@1)
408 (if (!TYPE_UNSIGNED (type))
409 { build_zero_cst (type); }))
413 /* But not for 0 % 0 so that we can get the proper warnings and errors. */
414 (if (!integer_zerop (@0))
415 { build_zero_cst (type); }))
416 /* (X % Y) % Y is just X % Y. */
418 (mod (mod@2 @0 @1) @1)
420 /* From extract_muldiv_1: (X * C1) % C2 is zero if C1 is a multiple of C2. */
422 (mod (mult @0 INTEGER_CST@1) INTEGER_CST@2)
423 (if (ANY_INTEGRAL_TYPE_P (type)
424 && TYPE_OVERFLOW_UNDEFINED (type)
425 && wi::multiple_of_p (wi::to_wide (@1), wi::to_wide (@2),
427 { build_zero_cst (type); })))
429 /* X % -C is the same as X % C. */
431 (trunc_mod @0 INTEGER_CST@1)
432 (if (TYPE_SIGN (type) == SIGNED
433 && !TREE_OVERFLOW (@1)
434 && wi::neg_p (wi::to_wide (@1))
435 && !TYPE_OVERFLOW_TRAPS (type)
436 /* Avoid this transformation if C is INT_MIN, i.e. C == -C. */
437 && !sign_bit_p (@1, @1))
438 (trunc_mod @0 (negate @1))))
440 /* X % -Y is the same as X % Y. */
442 (trunc_mod @0 (convert? (negate @1)))
443 (if (INTEGRAL_TYPE_P (type)
444 && !TYPE_UNSIGNED (type)
445 && !TYPE_OVERFLOW_TRAPS (type)
446 && tree_nop_conversion_p (type, TREE_TYPE (@1))
447 /* Avoid this transformation if X might be INT_MIN or
448 Y might be -1, because we would then change valid
449 INT_MIN % -(-1) into invalid INT_MIN % -1. */
450 && (expr_not_equal_to (@0, wi::to_wide (TYPE_MIN_VALUE (type)))
451 || expr_not_equal_to (@1, wi::minus_one (TYPE_PRECISION
453 (trunc_mod @0 (convert @1))))
455 /* X - (X / Y) * Y is the same as X % Y. */
457 (minus (convert1? @0) (convert2? (mult:c (trunc_div @@0 @@1) @1)))
458 (if (INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
459 (convert (trunc_mod @0 @1))))
461 /* Optimize TRUNC_MOD_EXPR by a power of two into a BIT_AND_EXPR,
462 i.e. "X % C" into "X & (C - 1)", if X and C are positive.
463 Also optimize A % (C << N) where C is a power of 2,
464 to A & ((C << N) - 1). */
465 (match (power_of_two_cand @1)
467 (match (power_of_two_cand @1)
468 (lshift INTEGER_CST@1 @2))
469 (for mod (trunc_mod floor_mod)
471 (mod @0 (convert?@3 (power_of_two_cand@1 @2)))
472 (if ((TYPE_UNSIGNED (type)
473 || tree_expr_nonnegative_p (@0))
474 && tree_nop_conversion_p (type, TREE_TYPE (@3))
475 && integer_pow2p (@2) && tree_int_cst_sgn (@2) > 0)
476 (bit_and @0 (convert (minus @1 { build_int_cst (TREE_TYPE (@1), 1); }))))))
478 /* Simplify (unsigned t * 2)/2 -> unsigned t & 0x7FFFFFFF. */
480 (trunc_div (mult @0 integer_pow2p@1) @1)
481 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
482 (bit_and @0 { wide_int_to_tree
483 (type, wi::mask (TYPE_PRECISION (type)
484 - wi::exact_log2 (wi::to_wide (@1)),
485 false, TYPE_PRECISION (type))); })))
487 /* Simplify (unsigned t / 2) * 2 -> unsigned t & ~1. */
489 (mult (trunc_div @0 integer_pow2p@1) @1)
490 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
491 (bit_and @0 (negate @1))))
493 /* Simplify (t * 2) / 2) -> t. */
494 (for div (trunc_div ceil_div floor_div round_div exact_div)
496 (div (mult @0 @1) @1)
497 (if (ANY_INTEGRAL_TYPE_P (type)
498 && TYPE_OVERFLOW_UNDEFINED (type))
502 /* Simplify cos(-x) and cos(|x|) -> cos(x). Similarly for cosh. */
507 /* Simplify pow(-x, y) and pow(|x|,y) -> pow(x,y) if y is an even integer. */
510 (pows (op @0) REAL_CST@1)
511 (with { HOST_WIDE_INT n; }
512 (if (real_isinteger (&TREE_REAL_CST (@1), &n) && (n & 1) == 0)
514 /* Likewise for powi. */
517 (pows (op @0) INTEGER_CST@1)
518 (if ((wi::to_wide (@1) & 1) == 0)
520 /* Strip negate and abs from both operands of hypot. */
528 /* copysign(-x, y) and copysign(abs(x), y) -> copysign(x, y). */
529 (for copysigns (COPYSIGN)
531 (copysigns (op @0) @1)
534 /* abs(x)*abs(x) -> x*x. Should be valid for all types. */
539 /* cos(copysign(x, y)) -> cos(x). Similarly for cosh. */
543 (coss (copysigns @0 @1))
546 /* pow(copysign(x, y), z) -> pow(x, z) if z is an even integer. */
550 (pows (copysigns @0 @2) REAL_CST@1)
551 (with { HOST_WIDE_INT n; }
552 (if (real_isinteger (&TREE_REAL_CST (@1), &n) && (n & 1) == 0)
554 /* Likewise for powi. */
558 (pows (copysigns @0 @2) INTEGER_CST@1)
559 (if ((wi::to_wide (@1) & 1) == 0)
564 /* hypot(copysign(x, y), z) -> hypot(x, z). */
566 (hypots (copysigns @0 @1) @2)
568 /* hypot(x, copysign(y, z)) -> hypot(x, y). */
570 (hypots @0 (copysigns @1 @2))
573 /* copysign(x, CST) -> [-]abs (x). */
574 (for copysigns (COPYSIGN)
576 (copysigns @0 REAL_CST@1)
577 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
581 /* copysign(copysign(x, y), z) -> copysign(x, z). */
582 (for copysigns (COPYSIGN)
584 (copysigns (copysigns @0 @1) @2)
587 /* copysign(x,y)*copysign(x,y) -> x*x. */
588 (for copysigns (COPYSIGN)
590 (mult (copysigns@2 @0 @1) @2)
593 /* ccos(-x) -> ccos(x). Similarly for ccosh. */
594 (for ccoss (CCOS CCOSH)
599 /* cabs(-x) and cos(conj(x)) -> cabs(x). */
600 (for ops (conj negate)
606 /* Fold (a * (1 << b)) into (a << b) */
608 (mult:c @0 (convert? (lshift integer_onep@1 @2)))
609 (if (! FLOAT_TYPE_P (type)
610 && tree_nop_conversion_p (type, TREE_TYPE (@1)))
613 /* Fold (1 << (C - x)) where C = precision(type) - 1
614 into ((1 << C) >> x). */
616 (lshift integer_onep@0 (minus@1 INTEGER_CST@2 @3))
617 (if (INTEGRAL_TYPE_P (type)
618 && wi::eq_p (wi::to_wide (@2), TYPE_PRECISION (type) - 1)
620 (if (TYPE_UNSIGNED (type))
621 (rshift (lshift @0 @2) @3)
623 { tree utype = unsigned_type_for (type); }
624 (convert (rshift (lshift (convert:utype @0) @2) @3))))))
626 /* Fold (C1/X)*C2 into (C1*C2)/X. */
628 (mult (rdiv@3 REAL_CST@0 @1) REAL_CST@2)
629 (if (flag_associative_math
632 { tree tem = const_binop (MULT_EXPR, type, @0, @2); }
634 (rdiv { tem; } @1)))))
636 /* Convert C1/(X*C2) into (C1/C2)/X */
638 (rdiv REAL_CST@0 (mult @1 REAL_CST@2))
639 (if (flag_reciprocal_math)
641 { tree tem = const_binop (RDIV_EXPR, type, @0, @2); }
643 (rdiv { tem; } @1)))))
645 /* Simplify ~X & X as zero. */
647 (bit_and:c (convert? @0) (convert? (bit_not @0)))
648 { build_zero_cst (type); })
650 /* PR71636: Transform x & ((1U << b) - 1) -> x & ~(~0U << b); */
652 (bit_and:c @0 (plus:s (lshift:s integer_onep @1) integer_minus_onep))
653 (if (TYPE_UNSIGNED (type))
654 (bit_and @0 (bit_not (lshift { build_all_ones_cst (type); } @1)))))
656 (for bitop (bit_and bit_ior)
658 /* PR35691: Transform
659 (x == 0 & y == 0) -> (x | typeof(x)(y)) == 0.
660 (x != 0 | y != 0) -> (x | typeof(x)(y)) != 0. */
662 (bitop (cmp @0 integer_zerop@2) (cmp @1 integer_zerop))
663 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
664 && INTEGRAL_TYPE_P (TREE_TYPE (@1))
665 && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
666 (cmp (bit_ior @0 (convert @1)) @2)))
668 (x == -1 & y == -1) -> (x & typeof(x)(y)) == -1.
669 (x != -1 | y != -1) -> (x & typeof(x)(y)) != -1. */
671 (bitop (cmp @0 integer_all_onesp@2) (cmp @1 integer_all_onesp))
672 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
673 && INTEGRAL_TYPE_P (TREE_TYPE (@1))
674 && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
675 (cmp (bit_and @0 (convert @1)) @2))))
677 /* Fold (A & ~B) - (A & B) into (A ^ B) - B. */
679 (minus (bit_and:cs @0 (bit_not @1)) (bit_and:cs @0 @1))
680 (minus (bit_xor @0 @1) @1))
682 (minus (bit_and:s @0 INTEGER_CST@2) (bit_and:s @0 INTEGER_CST@1))
683 (if (~wi::to_wide (@2) == wi::to_wide (@1))
684 (minus (bit_xor @0 @1) @1)))
686 /* Fold (A & B) - (A & ~B) into B - (A ^ B). */
688 (minus (bit_and:cs @0 @1) (bit_and:cs @0 (bit_not @1)))
689 (minus @1 (bit_xor @0 @1)))
691 /* Simplify (X & ~Y) |^+ (~X & Y) -> X ^ Y. */
692 (for op (bit_ior bit_xor plus)
694 (op (bit_and:c @0 (bit_not @1)) (bit_and:c (bit_not @0) @1))
697 (op:c (bit_and @0 INTEGER_CST@2) (bit_and (bit_not @0) INTEGER_CST@1))
698 (if (~wi::to_wide (@2) == wi::to_wide (@1))
701 /* PR53979: Transform ((a ^ b) | a) -> (a | b) */
703 (bit_ior:c (bit_xor:c @0 @1) @0)
706 /* (a & ~b) | (a ^ b) --> a ^ b */
708 (bit_ior:c (bit_and:c @0 (bit_not @1)) (bit_xor:c@2 @0 @1))
711 /* (a & ~b) ^ ~a --> ~(a & b) */
713 (bit_xor:c (bit_and:cs @0 (bit_not @1)) (bit_not @0))
714 (bit_not (bit_and @0 @1)))
716 /* (a | b) & ~(a ^ b) --> a & b */
718 (bit_and:c (bit_ior @0 @1) (bit_not (bit_xor:c @0 @1)))
721 /* a | ~(a ^ b) --> a | ~b */
723 (bit_ior:c @0 (bit_not:s (bit_xor:c @0 @1)))
724 (bit_ior @0 (bit_not @1)))
726 /* (a | b) | (a &^ b) --> a | b */
727 (for op (bit_and bit_xor)
729 (bit_ior:c (bit_ior@2 @0 @1) (op:c @0 @1))
732 /* (a & b) | ~(a ^ b) --> ~(a ^ b) */
734 (bit_ior:c (bit_and:c @0 @1) (bit_not@2 (bit_xor @0 @1)))
737 /* ~(~a & b) --> a | ~b */
739 (bit_not (bit_and:cs (bit_not @0) @1))
740 (bit_ior @0 (bit_not @1)))
742 /* Simplify (~X & Y) to X ^ Y if we know that (X & ~Y) is 0. */
745 (bit_and (bit_not SSA_NAME@0) INTEGER_CST@1)
746 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
747 && wi::bit_and_not (get_nonzero_bits (@0), wi::to_wide (@1)) == 0)
751 /* X % Y is smaller than Y. */
754 (cmp (trunc_mod @0 @1) @1)
755 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
756 { constant_boolean_node (cmp == LT_EXPR, type); })))
759 (cmp @1 (trunc_mod @0 @1))
760 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
761 { constant_boolean_node (cmp == GT_EXPR, type); })))
765 (bit_ior @0 integer_all_onesp@1)
770 (bit_ior @0 integer_zerop)
775 (bit_and @0 integer_zerop@1)
781 (for op (bit_ior bit_xor plus)
783 (op:c (convert? @0) (convert? (bit_not @0)))
784 (convert { build_all_ones_cst (TREE_TYPE (@0)); })))
789 { build_zero_cst (type); })
791 /* Canonicalize X ^ ~0 to ~X. */
793 (bit_xor @0 integer_all_onesp@1)
798 (bit_and @0 integer_all_onesp)
801 /* x & x -> x, x | x -> x */
802 (for bitop (bit_and bit_ior)
807 /* x & C -> x if we know that x & ~C == 0. */
810 (bit_and SSA_NAME@0 INTEGER_CST@1)
811 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
812 && wi::bit_and_not (get_nonzero_bits (@0), wi::to_wide (@1)) == 0)
816 /* x + (x & 1) -> (x + 1) & ~1 */
818 (plus:c @0 (bit_and:s @0 integer_onep@1))
819 (bit_and (plus @0 @1) (bit_not @1)))
821 /* x & ~(x & y) -> x & ~y */
822 /* x | ~(x | y) -> x | ~y */
823 (for bitop (bit_and bit_ior)
825 (bitop:c @0 (bit_not (bitop:cs @0 @1)))
826 (bitop @0 (bit_not @1))))
828 /* (x | y) & ~x -> y & ~x */
829 /* (x & y) | ~x -> y | ~x */
830 (for bitop (bit_and bit_ior)
831 rbitop (bit_ior bit_and)
833 (bitop:c (rbitop:c @0 @1) (bit_not@2 @0))
836 /* (x & y) ^ (x | y) -> x ^ y */
838 (bit_xor:c (bit_and @0 @1) (bit_ior @0 @1))
841 /* (x ^ y) ^ (x | y) -> x & y */
843 (bit_xor:c (bit_xor @0 @1) (bit_ior @0 @1))
846 /* (x & y) + (x ^ y) -> x | y */
847 /* (x & y) | (x ^ y) -> x | y */
848 /* (x & y) ^ (x ^ y) -> x | y */
849 (for op (plus bit_ior bit_xor)
851 (op:c (bit_and @0 @1) (bit_xor @0 @1))
854 /* (x & y) + (x | y) -> x + y */
856 (plus:c (bit_and @0 @1) (bit_ior @0 @1))
859 /* (x + y) - (x | y) -> x & y */
861 (minus (plus @0 @1) (bit_ior @0 @1))
862 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
863 && !TYPE_SATURATING (type))
866 /* (x + y) - (x & y) -> x | y */
868 (minus (plus @0 @1) (bit_and @0 @1))
869 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
870 && !TYPE_SATURATING (type))
873 /* (x | y) - (x ^ y) -> x & y */
875 (minus (bit_ior @0 @1) (bit_xor @0 @1))
878 /* (x | y) - (x & y) -> x ^ y */
880 (minus (bit_ior @0 @1) (bit_and @0 @1))
883 /* (x | y) & ~(x & y) -> x ^ y */
885 (bit_and:c (bit_ior @0 @1) (bit_not (bit_and @0 @1)))
888 /* (x | y) & (~x ^ y) -> x & y */
890 (bit_and:c (bit_ior:c @0 @1) (bit_xor:c @1 (bit_not @0)))
893 /* ~x & ~y -> ~(x | y)
894 ~x | ~y -> ~(x & y) */
895 (for op (bit_and bit_ior)
896 rop (bit_ior bit_and)
898 (op (convert1? (bit_not @0)) (convert2? (bit_not @1)))
899 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
900 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
901 (bit_not (rop (convert @0) (convert @1))))))
903 /* If we are XORing or adding two BIT_AND_EXPR's, both of which are and'ing
904 with a constant, and the two constants have no bits in common,
905 we should treat this as a BIT_IOR_EXPR since this may produce more
907 (for op (bit_xor plus)
909 (op (convert1? (bit_and@4 @0 INTEGER_CST@1))
910 (convert2? (bit_and@5 @2 INTEGER_CST@3)))
911 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
912 && tree_nop_conversion_p (type, TREE_TYPE (@2))
913 && (wi::to_wide (@1) & wi::to_wide (@3)) == 0)
914 (bit_ior (convert @4) (convert @5)))))
916 /* (X | Y) ^ X -> Y & ~ X*/
918 (bit_xor:c (convert1? (bit_ior:c @@0 @1)) (convert2? @0))
919 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
920 (convert (bit_and @1 (bit_not @0)))))
922 /* Convert ~X ^ ~Y to X ^ Y. */
924 (bit_xor (convert1? (bit_not @0)) (convert2? (bit_not @1)))
925 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
926 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
927 (bit_xor (convert @0) (convert @1))))
929 /* Convert ~X ^ C to X ^ ~C. */
931 (bit_xor (convert? (bit_not @0)) INTEGER_CST@1)
932 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
933 (bit_xor (convert @0) (bit_not @1))))
935 /* Fold (X & Y) ^ Y and (X ^ Y) & Y as ~X & Y. */
936 (for opo (bit_and bit_xor)
937 opi (bit_xor bit_and)
939 (opo:c (opi:c @0 @1) @1)
940 (bit_and (bit_not @0) @1)))
942 /* Given a bit-wise operation CODE applied to ARG0 and ARG1, see if both
943 operands are another bit-wise operation with a common input. If so,
944 distribute the bit operations to save an operation and possibly two if
945 constants are involved. For example, convert
946 (A | B) & (A | C) into A | (B & C)
947 Further simplification will occur if B and C are constants. */
948 (for op (bit_and bit_ior bit_xor)
949 rop (bit_ior bit_and bit_and)
951 (op (convert? (rop:c @@0 @1)) (convert? (rop:c @0 @2)))
952 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
953 && tree_nop_conversion_p (type, TREE_TYPE (@2)))
954 (rop (convert @0) (op (convert @1) (convert @2))))))
956 /* Some simple reassociation for bit operations, also handled in reassoc. */
957 /* (X & Y) & Y -> X & Y
958 (X | Y) | Y -> X | Y */
959 (for op (bit_and bit_ior)
961 (op:c (convert1?@2 (op:c @0 @@1)) (convert2? @1))
963 /* (X ^ Y) ^ Y -> X */
965 (bit_xor:c (convert1? (bit_xor:c @0 @@1)) (convert2? @1))
967 /* (X & Y) & (X & Z) -> (X & Y) & Z
968 (X | Y) | (X | Z) -> (X | Y) | Z */
969 (for op (bit_and bit_ior)
971 (op (convert1?@3 (op:c@4 @0 @1)) (convert2?@5 (op:c@6 @0 @2)))
972 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
973 && tree_nop_conversion_p (type, TREE_TYPE (@2)))
974 (if (single_use (@5) && single_use (@6))
976 (if (single_use (@3) && single_use (@4))
977 (op (convert @1) @5))))))
978 /* (X ^ Y) ^ (X ^ Z) -> Y ^ Z */
980 (bit_xor (convert1? (bit_xor:c @0 @1)) (convert2? (bit_xor:c @0 @2)))
981 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
982 && tree_nop_conversion_p (type, TREE_TYPE (@2)))
983 (bit_xor (convert @1) (convert @2))))
992 (abs tree_expr_nonnegative_p@0)
995 /* A few cases of fold-const.c negate_expr_p predicate. */
998 (if ((INTEGRAL_TYPE_P (type)
999 && TYPE_UNSIGNED (type))
1000 || (!TYPE_OVERFLOW_SANITIZED (type)
1001 && may_negate_without_overflow_p (t)))))
1002 (match negate_expr_p
1004 (match negate_expr_p
1006 (if (!TYPE_OVERFLOW_SANITIZED (type))))
1007 (match negate_expr_p
1009 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (t)))))
1010 /* VECTOR_CST handling of non-wrapping types would recurse in unsupported
1012 (match negate_expr_p
1014 (if (FLOAT_TYPE_P (TREE_TYPE (type)) || TYPE_OVERFLOW_WRAPS (type))))
1015 (match negate_expr_p
1017 (if ((ANY_INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_WRAPS (type))
1018 || (FLOAT_TYPE_P (type)
1019 && !HONOR_SIGN_DEPENDENT_ROUNDING (type)
1020 && !HONOR_SIGNED_ZEROS (type)))))
1022 /* (-A) * (-B) -> A * B */
1024 (mult:c (convert1? (negate @0)) (convert2? negate_expr_p@1))
1025 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
1026 && tree_nop_conversion_p (type, TREE_TYPE (@1)))
1027 (mult (convert @0) (convert (negate @1)))))
1029 /* -(A + B) -> (-B) - A. */
1031 (negate (plus:c @0 negate_expr_p@1))
1032 (if (!HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type))
1033 && !HONOR_SIGNED_ZEROS (element_mode (type)))
1034 (minus (negate @1) @0)))
1036 /* -(A - B) -> B - A. */
1038 (negate (minus @0 @1))
1039 (if ((ANY_INTEGRAL_TYPE_P (type) && !TYPE_OVERFLOW_SANITIZED (type))
1040 || (FLOAT_TYPE_P (type)
1041 && !HONOR_SIGN_DEPENDENT_ROUNDING (type)
1042 && !HONOR_SIGNED_ZEROS (type)))
1045 /* A - B -> A + (-B) if B is easily negatable. */
1047 (minus @0 negate_expr_p@1)
1048 (if (!FIXED_POINT_TYPE_P (type))
1049 (plus @0 (negate @1))))
1051 /* Try to fold (type) X op CST -> (type) (X op ((type-x) CST))
1053 For bitwise binary operations apply operand conversions to the
1054 binary operation result instead of to the operands. This allows
1055 to combine successive conversions and bitwise binary operations.
1056 We combine the above two cases by using a conditional convert. */
1057 (for bitop (bit_and bit_ior bit_xor)
1059 (bitop (convert @0) (convert? @1))
1060 (if (((TREE_CODE (@1) == INTEGER_CST
1061 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
1062 && int_fits_type_p (@1, TREE_TYPE (@0)))
1063 || types_match (@0, @1))
1064 /* ??? This transform conflicts with fold-const.c doing
1065 Convert (T)(x & c) into (T)x & (T)c, if c is an integer
1066 constants (if x has signed type, the sign bit cannot be set
1067 in c). This folds extension into the BIT_AND_EXPR.
1068 Restrict it to GIMPLE to avoid endless recursions. */
1069 && (bitop != BIT_AND_EXPR || GIMPLE)
1070 && (/* That's a good idea if the conversion widens the operand, thus
1071 after hoisting the conversion the operation will be narrower. */
1072 TYPE_PRECISION (TREE_TYPE (@0)) < TYPE_PRECISION (type)
1073 /* It's also a good idea if the conversion is to a non-integer
1075 || GET_MODE_CLASS (TYPE_MODE (type)) != MODE_INT
1076 /* Or if the precision of TO is not the same as the precision
1078 || !type_has_mode_precision_p (type)))
1079 (convert (bitop @0 (convert @1))))))
1081 (for bitop (bit_and bit_ior)
1082 rbitop (bit_ior bit_and)
1083 /* (x | y) & x -> x */
1084 /* (x & y) | x -> x */
1086 (bitop:c (rbitop:c @0 @1) @0)
1088 /* (~x | y) & x -> x & y */
1089 /* (~x & y) | x -> x | y */
1091 (bitop:c (rbitop:c (bit_not @0) @1) @0)
1094 /* (x | CST1) & CST2 -> (x & CST2) | (CST1 & CST2) */
1096 (bit_and (bit_ior @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
1097 (bit_ior (bit_and @0 @2) (bit_and @1 @2)))
1099 /* Combine successive equal operations with constants. */
1100 (for bitop (bit_and bit_ior bit_xor)
1102 (bitop (bitop @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
1103 (bitop @0 (bitop @1 @2))))
1105 /* Try simple folding for X op !X, and X op X with the help
1106 of the truth_valued_p and logical_inverted_value predicates. */
1107 (match truth_valued_p
1109 (if (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1)))
1110 (for op (tcc_comparison truth_and truth_andif truth_or truth_orif truth_xor)
1111 (match truth_valued_p
1113 (match truth_valued_p
1116 (match (logical_inverted_value @0)
1118 (match (logical_inverted_value @0)
1119 (bit_not truth_valued_p@0))
1120 (match (logical_inverted_value @0)
1121 (eq @0 integer_zerop))
1122 (match (logical_inverted_value @0)
1123 (ne truth_valued_p@0 integer_truep))
1124 (match (logical_inverted_value @0)
1125 (bit_xor truth_valued_p@0 integer_truep))
1129 (bit_and:c @0 (logical_inverted_value @0))
1130 { build_zero_cst (type); })
1131 /* X | !X and X ^ !X -> 1, , if X is truth-valued. */
1132 (for op (bit_ior bit_xor)
1134 (op:c truth_valued_p@0 (logical_inverted_value @0))
1135 { constant_boolean_node (true, type); }))
1136 /* X ==/!= !X is false/true. */
1139 (op:c truth_valued_p@0 (logical_inverted_value @0))
1140 { constant_boolean_node (op == NE_EXPR ? true : false, type); }))
1144 (bit_not (bit_not @0))
1147 /* Convert ~ (-A) to A - 1. */
1149 (bit_not (convert? (negate @0)))
1150 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1151 || !TYPE_UNSIGNED (TREE_TYPE (@0)))
1152 (convert (minus @0 { build_each_one_cst (TREE_TYPE (@0)); }))))
1154 /* Convert - (~A) to A + 1. */
1156 (negate (nop_convert (bit_not @0)))
1157 (plus (view_convert @0) { build_each_one_cst (type); }))
1159 /* Convert ~ (A - 1) or ~ (A + -1) to -A. */
1161 (bit_not (convert? (minus @0 integer_each_onep)))
1162 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1163 || !TYPE_UNSIGNED (TREE_TYPE (@0)))
1164 (convert (negate @0))))
1166 (bit_not (convert? (plus @0 integer_all_onesp)))
1167 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1168 || !TYPE_UNSIGNED (TREE_TYPE (@0)))
1169 (convert (negate @0))))
1171 /* Part of convert ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify. */
1173 (bit_not (convert? (bit_xor @0 INTEGER_CST@1)))
1174 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1175 (convert (bit_xor @0 (bit_not @1)))))
1177 (bit_not (convert? (bit_xor:c (bit_not @0) @1)))
1178 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1179 (convert (bit_xor @0 @1))))
1181 /* Otherwise prefer ~(X ^ Y) to ~X ^ Y as more canonical. */
1183 (bit_xor:c (nop_convert:s (bit_not:s @0)) @1)
1184 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1185 (bit_not (bit_xor (view_convert @0) @1))))
1187 /* (x & ~m) | (y & m) -> ((x ^ y) & m) ^ x */
1189 (bit_ior:c (bit_and:cs @0 (bit_not @2)) (bit_and:cs @1 @2))
1190 (bit_xor (bit_and (bit_xor @0 @1) @2) @0))
1192 /* Fold A - (A & B) into ~B & A. */
1194 (minus (convert1? @0) (convert2?:s (bit_and:cs @@0 @1)))
1195 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
1196 && tree_nop_conversion_p (type, TREE_TYPE (@1)))
1197 (convert (bit_and (bit_not @1) @0))))
1199 /* (m1 CMP m2) * d -> (m1 CMP m2) ? d : 0 */
1200 (for cmp (gt lt ge le)
1202 (mult (convert (cmp @0 @1)) @2)
1203 (cond (cmp @0 @1) @2 { build_zero_cst (type); })))
1205 /* For integral types with undefined overflow and C != 0 fold
1206 x * C EQ/NE y * C into x EQ/NE y. */
1209 (cmp (mult:c @0 @1) (mult:c @2 @1))
1210 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
1211 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1212 && tree_expr_nonzero_p (@1))
1215 /* For integral types with wrapping overflow and C odd fold
1216 x * C EQ/NE y * C into x EQ/NE y. */
1219 (cmp (mult @0 INTEGER_CST@1) (mult @2 @1))
1220 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
1221 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))
1222 && (TREE_INT_CST_LOW (@1) & 1) != 0)
1225 /* For integral types with undefined overflow and C != 0 fold
1226 x * C RELOP y * C into:
1228 x RELOP y for nonnegative C
1229 y RELOP x for negative C */
1230 (for cmp (lt gt le ge)
1232 (cmp (mult:c @0 @1) (mult:c @2 @1))
1233 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
1234 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1235 (if (tree_expr_nonnegative_p (@1) && tree_expr_nonzero_p (@1))
1237 (if (TREE_CODE (@1) == INTEGER_CST
1238 && wi::neg_p (wi::to_wide (@1), TYPE_SIGN (TREE_TYPE (@1))))
1241 /* (X - 1U) <= INT_MAX-1U into (int) X > 0. */
1245 (cmp (plus @0 integer_minus_onep@1) INTEGER_CST@2)
1246 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1247 && TYPE_UNSIGNED (TREE_TYPE (@0))
1248 && TYPE_PRECISION (TREE_TYPE (@0)) > 1
1249 && (wi::to_wide (@2)
1250 == wi::max_value (TYPE_PRECISION (TREE_TYPE (@0)), SIGNED) - 1))
1251 (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
1252 (icmp (convert:stype @0) { build_int_cst (stype, 0); })))))
1254 /* X / 4 < Y / 4 iff X < Y when the division is known to be exact. */
1255 (for cmp (simple_comparison)
1257 (cmp (exact_div @0 INTEGER_CST@2) (exact_div @1 @2))
1258 (if (wi::gt_p (wi::to_wide (@2), 0, TYPE_SIGN (TREE_TYPE (@2))))
1261 /* X / C1 op C2 into a simple range test. */
1262 (for cmp (simple_comparison)
1264 (cmp (trunc_div:s @0 INTEGER_CST@1) INTEGER_CST@2)
1265 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1266 && integer_nonzerop (@1)
1267 && !TREE_OVERFLOW (@1)
1268 && !TREE_OVERFLOW (@2))
1269 (with { tree lo, hi; bool neg_overflow;
1270 enum tree_code code = fold_div_compare (cmp, @1, @2, &lo, &hi,
1273 (if (code == LT_EXPR || code == GE_EXPR)
1274 (if (TREE_OVERFLOW (lo))
1275 { build_int_cst (type, (code == LT_EXPR) ^ neg_overflow); }
1276 (if (code == LT_EXPR)
1279 (if (code == LE_EXPR || code == GT_EXPR)
1280 (if (TREE_OVERFLOW (hi))
1281 { build_int_cst (type, (code == LE_EXPR) ^ neg_overflow); }
1282 (if (code == LE_EXPR)
1286 { build_int_cst (type, code == NE_EXPR); })
1287 (if (code == EQ_EXPR && !hi)
1289 (if (code == EQ_EXPR && !lo)
1291 (if (code == NE_EXPR && !hi)
1293 (if (code == NE_EXPR && !lo)
1296 { build_range_check (UNKNOWN_LOCATION, type, @0, code == EQ_EXPR,
1300 tree etype = range_check_type (TREE_TYPE (@0));
1303 if (! TYPE_UNSIGNED (etype))
1304 etype = unsigned_type_for (etype);
1305 hi = fold_convert (etype, hi);
1306 lo = fold_convert (etype, lo);
1307 hi = const_binop (MINUS_EXPR, etype, hi, lo);
1310 (if (etype && hi && !TREE_OVERFLOW (hi))
1311 (if (code == EQ_EXPR)
1312 (le (minus (convert:etype @0) { lo; }) { hi; })
1313 (gt (minus (convert:etype @0) { lo; }) { hi; })))))))))
1315 /* X + Z < Y + Z is the same as X < Y when there is no overflow. */
1316 (for op (lt le ge gt)
1318 (op (plus:c @0 @2) (plus:c @1 @2))
1319 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1320 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1322 /* For equality and subtraction, this is also true with wrapping overflow. */
1323 (for op (eq ne minus)
1325 (op (plus:c @0 @2) (plus:c @1 @2))
1326 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1327 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1328 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
1331 /* X - Z < Y - Z is the same as X < Y when there is no overflow. */
1332 (for op (lt le ge gt)
1334 (op (minus @0 @2) (minus @1 @2))
1335 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1336 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1338 /* For equality and subtraction, this is also true with wrapping overflow. */
1339 (for op (eq ne minus)
1341 (op (minus @0 @2) (minus @1 @2))
1342 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1343 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1344 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
1347 /* Z - X < Z - Y is the same as Y < X when there is no overflow. */
1348 (for op (lt le ge gt)
1350 (op (minus @2 @0) (minus @2 @1))
1351 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1352 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1354 /* For equality and subtraction, this is also true with wrapping overflow. */
1355 (for op (eq ne minus)
1357 (op (minus @2 @0) (minus @2 @1))
1358 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1359 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1360 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
1363 /* X + Y < Y is the same as X < 0 when there is no overflow. */
1364 (for op (lt le gt ge)
1366 (op:c (plus:c@2 @0 @1) @1)
1367 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1368 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1369 && (CONSTANT_CLASS_P (@0) || single_use (@2)))
1370 (op @0 { build_zero_cst (TREE_TYPE (@0)); }))))
1371 /* For equality, this is also true with wrapping overflow. */
1374 (op:c (nop_convert@3 (plus:c@2 @0 (convert1? @1))) (convert2? @1))
1375 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1376 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1377 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
1378 && (CONSTANT_CLASS_P (@0) || (single_use (@2) && single_use (@3)))
1379 && tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@2))
1380 && tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@1)))
1381 (op @0 { build_zero_cst (TREE_TYPE (@0)); })))
1383 (op:c (nop_convert@3 (pointer_plus@2 (convert1? @0) @1)) (convert2? @0))
1384 (if (tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@0))
1385 && tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0))
1386 && (CONSTANT_CLASS_P (@1) || (single_use (@2) && single_use (@3))))
1387 (op @1 { build_zero_cst (TREE_TYPE (@1)); }))))
1389 /* X - Y < X is the same as Y > 0 when there is no overflow.
1390 For equality, this is also true with wrapping overflow. */
1391 (for op (simple_comparison)
1393 (op:c @0 (minus@2 @0 @1))
1394 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1395 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1396 || ((op == EQ_EXPR || op == NE_EXPR)
1397 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
1398 && (CONSTANT_CLASS_P (@1) || single_use (@2)))
1399 (op @1 { build_zero_cst (TREE_TYPE (@1)); }))))
1402 * (X / Y) == 0 -> X < Y if X, Y are unsigned.
1403 * (X / Y) != 0 -> X >= Y, if X, Y are unsigned.
1408 (cmp (trunc_div @0 @1) integer_zerop)
1409 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
1410 && (VECTOR_TYPE_P (type) || !VECTOR_TYPE_P (TREE_TYPE (@0))))
1413 /* X == C - X can never be true if C is odd. */
1416 (cmp:c (convert? @0) (convert1? (minus INTEGER_CST@1 (convert2? @0))))
1417 (if (TREE_INT_CST_LOW (@1) & 1)
1418 { constant_boolean_node (cmp == NE_EXPR, type); })))
1420 /* Arguments on which one can call get_nonzero_bits to get the bits
1422 (match with_possible_nonzero_bits
1424 (match with_possible_nonzero_bits
1426 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))))
1427 /* Slightly extended version, do not make it recursive to keep it cheap. */
1428 (match (with_possible_nonzero_bits2 @0)
1429 with_possible_nonzero_bits@0)
1430 (match (with_possible_nonzero_bits2 @0)
1431 (bit_and:c with_possible_nonzero_bits@0 @2))
1433 /* Same for bits that are known to be set, but we do not have
1434 an equivalent to get_nonzero_bits yet. */
1435 (match (with_certain_nonzero_bits2 @0)
1437 (match (with_certain_nonzero_bits2 @0)
1438 (bit_ior @1 INTEGER_CST@0))
1440 /* X == C (or X & Z == Y | C) is impossible if ~nonzero(X) & C != 0. */
1443 (cmp:c (with_possible_nonzero_bits2 @0) (with_certain_nonzero_bits2 @1))
1444 (if (wi::bit_and_not (wi::to_wide (@1), get_nonzero_bits (@0)) != 0)
1445 { constant_boolean_node (cmp == NE_EXPR, type); })))
1447 /* ((X inner_op C0) outer_op C1)
1448 With X being a tree where value_range has reasoned certain bits to always be
1449 zero throughout its computed value range,
1450 inner_op = {|,^}, outer_op = {|,^} and inner_op != outer_op
1451 where zero_mask has 1's for all bits that are sure to be 0 in
1453 if (inner_op == '^') C0 &= ~C1;
1454 if ((C0 & ~zero_mask) == 0) then emit (X outer_op (C0 outer_op C1)
1455 if ((C1 & ~zero_mask) == 0) then emit (X inner_op (C0 outer_op C1)
1457 (for inner_op (bit_ior bit_xor)
1458 outer_op (bit_xor bit_ior)
1461 (inner_op:s @2 INTEGER_CST@0) INTEGER_CST@1)
1465 wide_int zero_mask_not;
1469 if (TREE_CODE (@2) == SSA_NAME)
1470 zero_mask_not = get_nonzero_bits (@2);
1474 if (inner_op == BIT_XOR_EXPR)
1476 C0 = wi::bit_and_not (wi::to_wide (@0), wi::to_wide (@1));
1477 cst_emit = C0 | wi::to_wide (@1);
1481 C0 = wi::to_wide (@0);
1482 cst_emit = C0 ^ wi::to_wide (@1);
1485 (if (!fail && (C0 & zero_mask_not) == 0)
1486 (outer_op @2 { wide_int_to_tree (type, cst_emit); })
1487 (if (!fail && (wi::to_wide (@1) & zero_mask_not) == 0)
1488 (inner_op @2 { wide_int_to_tree (type, cst_emit); }))))))
1490 /* Associate (p +p off1) +p off2 as (p +p (off1 + off2)). */
1492 (pointer_plus (pointer_plus:s @0 @1) @3)
1493 (pointer_plus @0 (plus @1 @3)))
1499 tem4 = (unsigned long) tem3;
1504 (pointer_plus @0 (convert?@2 (minus@3 (convert @1) (convert @0))))
1505 /* Conditionally look through a sign-changing conversion. */
1506 (if (TYPE_PRECISION (TREE_TYPE (@2)) == TYPE_PRECISION (TREE_TYPE (@3))
1507 && ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@1)))
1508 || (GENERIC && type == TREE_TYPE (@1))))
1512 tem = (sizetype) ptr;
1516 and produce the simpler and easier to analyze with respect to alignment
1517 ... = ptr & ~algn; */
1519 (pointer_plus @0 (negate (bit_and (convert @0) INTEGER_CST@1)))
1520 (with { tree algn = wide_int_to_tree (TREE_TYPE (@0), ~wi::to_wide (@1)); }
1521 (bit_and @0 { algn; })))
1523 /* Try folding difference of addresses. */
1525 (minus (convert ADDR_EXPR@0) (convert @1))
1526 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1527 (with { HOST_WIDE_INT diff; }
1528 (if (ptr_difference_const (@0, @1, &diff))
1529 { build_int_cst_type (type, diff); }))))
1531 (minus (convert @0) (convert ADDR_EXPR@1))
1532 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1533 (with { HOST_WIDE_INT diff; }
1534 (if (ptr_difference_const (@0, @1, &diff))
1535 { build_int_cst_type (type, diff); }))))
1537 /* If arg0 is derived from the address of an object or function, we may
1538 be able to fold this expression using the object or function's
1541 (bit_and (convert? @0) INTEGER_CST@1)
1542 (if (POINTER_TYPE_P (TREE_TYPE (@0))
1543 && tree_nop_conversion_p (type, TREE_TYPE (@0)))
1547 unsigned HOST_WIDE_INT bitpos;
1548 get_pointer_alignment_1 (@0, &align, &bitpos);
1550 (if (wi::ltu_p (wi::to_wide (@1), align / BITS_PER_UNIT))
1551 { wide_int_to_tree (type, (wi::to_wide (@1)
1552 & (bitpos / BITS_PER_UNIT))); }))))
1555 /* We can't reassociate at all for saturating types. */
1556 (if (!TYPE_SATURATING (type))
1558 /* Contract negates. */
1559 /* A + (-B) -> A - B */
1561 (plus:c @0 (convert? (negate @1)))
1562 /* Apply STRIP_NOPS on the negate. */
1563 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1564 && !TYPE_OVERFLOW_SANITIZED (type))
1568 if (INTEGRAL_TYPE_P (type)
1569 && TYPE_OVERFLOW_WRAPS (type) != TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
1570 t1 = TYPE_OVERFLOW_WRAPS (type) ? type : TREE_TYPE (@1);
1572 (convert (minus (convert:t1 @0) (convert:t1 @1))))))
1573 /* A - (-B) -> A + B */
1575 (minus @0 (convert? (negate @1)))
1576 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1577 && !TYPE_OVERFLOW_SANITIZED (type))
1581 if (INTEGRAL_TYPE_P (type)
1582 && TYPE_OVERFLOW_WRAPS (type) != TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
1583 t1 = TYPE_OVERFLOW_WRAPS (type) ? type : TREE_TYPE (@1);
1585 (convert (plus (convert:t1 @0) (convert:t1 @1))))))
1587 Sign-extension is ok except for INT_MIN, which thankfully cannot
1588 happen without overflow. */
1590 (negate (convert (negate @1)))
1591 (if (INTEGRAL_TYPE_P (type)
1592 && (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@1))
1593 || (!TYPE_UNSIGNED (TREE_TYPE (@1))
1594 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))))
1595 && !TYPE_OVERFLOW_SANITIZED (type)
1596 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@1)))
1599 (negate (convert negate_expr_p@1))
1600 (if (SCALAR_FLOAT_TYPE_P (type)
1601 && ((DECIMAL_FLOAT_TYPE_P (type)
1602 == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1))
1603 && TYPE_PRECISION (type) >= TYPE_PRECISION (TREE_TYPE (@1)))
1604 || !HONOR_SIGN_DEPENDENT_ROUNDING (type)))
1605 (convert (negate @1))))
1607 (negate (nop_convert (negate @1)))
1608 (if (!TYPE_OVERFLOW_SANITIZED (type)
1609 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@1)))
1612 /* We can't reassociate floating-point unless -fassociative-math
1613 or fixed-point plus or minus because of saturation to +-Inf. */
1614 (if ((!FLOAT_TYPE_P (type) || flag_associative_math)
1615 && !FIXED_POINT_TYPE_P (type))
1617 /* Match patterns that allow contracting a plus-minus pair
1618 irrespective of overflow issues. */
1619 /* (A +- B) - A -> +- B */
1620 /* (A +- B) -+ B -> A */
1621 /* A - (A +- B) -> -+ B */
1622 /* A +- (B -+ A) -> +- B */
1624 (minus (plus:c @0 @1) @0)
1627 (minus (minus @0 @1) @0)
1630 (plus:c (minus @0 @1) @1)
1633 (minus @0 (plus:c @0 @1))
1636 (minus @0 (minus @0 @1))
1638 /* (A +- B) + (C - A) -> C +- B */
1639 /* (A + B) - (A - C) -> B + C */
1640 /* More cases are handled with comparisons. */
1642 (plus:c (plus:c @0 @1) (minus @2 @0))
1645 (plus:c (minus @0 @1) (minus @2 @0))
1648 (minus (plus:c @0 @1) (minus @0 @2))
1651 /* (A +- CST1) +- CST2 -> A + CST3
1652 Use view_convert because it is safe for vectors and equivalent for
1654 (for outer_op (plus minus)
1655 (for inner_op (plus minus)
1656 neg_inner_op (minus plus)
1658 (outer_op (nop_convert (inner_op @0 CONSTANT_CLASS_P@1))
1660 /* If one of the types wraps, use that one. */
1661 (if (!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_WRAPS (type))
1662 (if (outer_op == PLUS_EXPR)
1663 (plus (view_convert @0) (inner_op @2 (view_convert @1)))
1664 (minus (view_convert @0) (neg_inner_op @2 (view_convert @1))))
1665 (if (!ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1666 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
1667 (if (outer_op == PLUS_EXPR)
1668 (view_convert (plus @0 (inner_op (view_convert @2) @1)))
1669 (view_convert (minus @0 (neg_inner_op (view_convert @2) @1))))
1670 /* If the constant operation overflows we cannot do the transform
1671 directly as we would introduce undefined overflow, for example
1672 with (a - 1) + INT_MIN. */
1673 (if (types_match (type, @0))
1674 (with { tree cst = const_binop (outer_op == inner_op
1675 ? PLUS_EXPR : MINUS_EXPR,
1677 (if (cst && !TREE_OVERFLOW (cst))
1678 (inner_op @0 { cst; } )
1679 /* X+INT_MAX+1 is X-INT_MIN. */
1680 (if (INTEGRAL_TYPE_P (type) && cst
1681 && wi::to_wide (cst) == wi::min_value (type))
1682 (neg_inner_op @0 { wide_int_to_tree (type, wi::to_wide (cst)); })
1683 /* Last resort, use some unsigned type. */
1684 (with { tree utype = unsigned_type_for (type); }
1685 (view_convert (inner_op
1686 (view_convert:utype @0)
1688 { drop_tree_overflow (cst); })))))))))))))
1690 /* (CST1 - A) +- CST2 -> CST3 - A */
1691 (for outer_op (plus minus)
1693 (outer_op (minus CONSTANT_CLASS_P@1 @0) CONSTANT_CLASS_P@2)
1694 (with { tree cst = const_binop (outer_op, type, @1, @2); }
1695 (if (cst && !TREE_OVERFLOW (cst))
1696 (minus { cst; } @0)))))
1698 /* CST1 - (CST2 - A) -> CST3 + A */
1700 (minus CONSTANT_CLASS_P@1 (minus CONSTANT_CLASS_P@2 @0))
1701 (with { tree cst = const_binop (MINUS_EXPR, type, @1, @2); }
1702 (if (cst && !TREE_OVERFLOW (cst))
1703 (plus { cst; } @0))))
1707 (plus:c (bit_not @0) @0)
1708 (if (!TYPE_OVERFLOW_TRAPS (type))
1709 { build_all_ones_cst (type); }))
1713 (plus (convert? (bit_not @0)) integer_each_onep)
1714 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1715 (negate (convert @0))))
1719 (minus (convert? (negate @0)) integer_each_onep)
1720 (if (!TYPE_OVERFLOW_TRAPS (type)
1721 && tree_nop_conversion_p (type, TREE_TYPE (@0)))
1722 (bit_not (convert @0))))
1726 (minus integer_all_onesp @0)
1729 /* (T)(P + A) - (T)P -> (T) A */
1730 (for add (plus pointer_plus)
1732 (minus (convert (add @@0 @1))
1734 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
1735 /* For integer types, if A has a smaller type
1736 than T the result depends on the possible
1738 E.g. T=size_t, A=(unsigned)429497295, P>0.
1739 However, if an overflow in P + A would cause
1740 undefined behavior, we can assume that there
1742 || (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1743 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1744 /* For pointer types, if the conversion of A to the
1745 final type requires a sign- or zero-extension,
1746 then we have to punt - it is not defined which
1748 || (POINTER_TYPE_P (TREE_TYPE (@0))
1749 && TREE_CODE (@1) == INTEGER_CST
1750 && tree_int_cst_sign_bit (@1) == 0))
1753 /* (T)P - (T)(P + A) -> -(T) A */
1754 (for add (plus pointer_plus)
1757 (convert (add @@0 @1)))
1758 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
1759 /* For integer types, if A has a smaller type
1760 than T the result depends on the possible
1762 E.g. T=size_t, A=(unsigned)429497295, P>0.
1763 However, if an overflow in P + A would cause
1764 undefined behavior, we can assume that there
1766 || (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1767 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1768 /* For pointer types, if the conversion of A to the
1769 final type requires a sign- or zero-extension,
1770 then we have to punt - it is not defined which
1772 || (POINTER_TYPE_P (TREE_TYPE (@0))
1773 && TREE_CODE (@1) == INTEGER_CST
1774 && tree_int_cst_sign_bit (@1) == 0))
1775 (negate (convert @1)))))
1777 /* (T)(P + A) - (T)(P + B) -> (T)A - (T)B */
1778 (for add (plus pointer_plus)
1780 (minus (convert (add @@0 @1))
1781 (convert (add @0 @2)))
1782 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
1783 /* For integer types, if A has a smaller type
1784 than T the result depends on the possible
1786 E.g. T=size_t, A=(unsigned)429497295, P>0.
1787 However, if an overflow in P + A would cause
1788 undefined behavior, we can assume that there
1790 || (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1791 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1792 /* For pointer types, if the conversion of A to the
1793 final type requires a sign- or zero-extension,
1794 then we have to punt - it is not defined which
1796 || (POINTER_TYPE_P (TREE_TYPE (@0))
1797 && TREE_CODE (@1) == INTEGER_CST
1798 && tree_int_cst_sign_bit (@1) == 0
1799 && TREE_CODE (@2) == INTEGER_CST
1800 && tree_int_cst_sign_bit (@2) == 0))
1801 (minus (convert @1) (convert @2)))))))
1804 /* Simplifications of MIN_EXPR, MAX_EXPR, fmin() and fmax(). */
1806 (for minmax (min max FMIN FMAX)
1810 /* min(max(x,y),y) -> y. */
1812 (min:c (max:c @0 @1) @1)
1814 /* max(min(x,y),y) -> y. */
1816 (max:c (min:c @0 @1) @1)
1818 /* max(a,-a) -> abs(a). */
1820 (max:c @0 (negate @0))
1821 (if (TREE_CODE (type) != COMPLEX_TYPE
1822 && (! ANY_INTEGRAL_TYPE_P (type)
1823 || TYPE_OVERFLOW_UNDEFINED (type)))
1825 /* min(a,-a) -> -abs(a). */
1827 (min:c @0 (negate @0))
1828 (if (TREE_CODE (type) != COMPLEX_TYPE
1829 && (! ANY_INTEGRAL_TYPE_P (type)
1830 || TYPE_OVERFLOW_UNDEFINED (type)))
1835 (if (INTEGRAL_TYPE_P (type)
1836 && TYPE_MIN_VALUE (type)
1837 && operand_equal_p (@1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
1839 (if (INTEGRAL_TYPE_P (type)
1840 && TYPE_MAX_VALUE (type)
1841 && operand_equal_p (@1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
1846 (if (INTEGRAL_TYPE_P (type)
1847 && TYPE_MAX_VALUE (type)
1848 && operand_equal_p (@1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
1850 (if (INTEGRAL_TYPE_P (type)
1851 && TYPE_MIN_VALUE (type)
1852 && operand_equal_p (@1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
1855 /* max (a, a + CST) -> a + CST where CST is positive. */
1856 /* max (a, a + CST) -> a where CST is negative. */
1858 (max:c @0 (plus@2 @0 INTEGER_CST@1))
1859 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1860 (if (tree_int_cst_sgn (@1) > 0)
1864 /* min (a, a + CST) -> a where CST is positive. */
1865 /* min (a, a + CST) -> a + CST where CST is negative. */
1867 (min:c @0 (plus@2 @0 INTEGER_CST@1))
1868 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1869 (if (tree_int_cst_sgn (@1) > 0)
1873 /* (convert (minmax ((convert (x) c)))) -> minmax (x c) if x is promoted
1874 and the outer convert demotes the expression back to x's type. */
1875 (for minmax (min max)
1877 (convert (minmax@0 (convert @1) INTEGER_CST@2))
1878 (if (INTEGRAL_TYPE_P (type)
1879 && types_match (@1, type) && int_fits_type_p (@2, type)
1880 && TYPE_SIGN (TREE_TYPE (@0)) == TYPE_SIGN (type)
1881 && TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type))
1882 (minmax @1 (convert @2)))))
1884 (for minmax (FMIN FMAX)
1885 /* If either argument is NaN, return the other one. Avoid the
1886 transformation if we get (and honor) a signalling NaN. */
1888 (minmax:c @0 REAL_CST@1)
1889 (if (real_isnan (TREE_REAL_CST_PTR (@1))
1890 && (!HONOR_SNANS (@1) || !TREE_REAL_CST (@1).signalling))
1892 /* Convert fmin/fmax to MIN_EXPR/MAX_EXPR. C99 requires these
1893 functions to return the numeric arg if the other one is NaN.
1894 MIN and MAX don't honor that, so only transform if -ffinite-math-only
1895 is set. C99 doesn't require -0.0 to be handled, so we don't have to
1896 worry about it either. */
1897 (if (flag_finite_math_only)
1904 /* min (-A, -B) -> -max (A, B) */
1905 (for minmax (min max FMIN FMAX)
1906 maxmin (max min FMAX FMIN)
1908 (minmax (negate:s@2 @0) (negate:s@3 @1))
1909 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
1910 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1911 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
1912 (negate (maxmin @0 @1)))))
1913 /* MIN (~X, ~Y) -> ~MAX (X, Y)
1914 MAX (~X, ~Y) -> ~MIN (X, Y) */
1915 (for minmax (min max)
1918 (minmax (bit_not:s@2 @0) (bit_not:s@3 @1))
1919 (bit_not (maxmin @0 @1))))
1921 /* MIN (X, Y) == X -> X <= Y */
1922 (for minmax (min min max max)
1926 (cmp:c (minmax:c @0 @1) @0)
1927 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0)))
1929 /* MIN (X, 5) == 0 -> X == 0
1930 MIN (X, 5) == 7 -> false */
1933 (cmp (min @0 INTEGER_CST@1) INTEGER_CST@2)
1934 (if (wi::lt_p (wi::to_wide (@1), wi::to_wide (@2),
1935 TYPE_SIGN (TREE_TYPE (@0))))
1936 { constant_boolean_node (cmp == NE_EXPR, type); }
1937 (if (wi::gt_p (wi::to_wide (@1), wi::to_wide (@2),
1938 TYPE_SIGN (TREE_TYPE (@0))))
1942 (cmp (max @0 INTEGER_CST@1) INTEGER_CST@2)
1943 (if (wi::gt_p (wi::to_wide (@1), wi::to_wide (@2),
1944 TYPE_SIGN (TREE_TYPE (@0))))
1945 { constant_boolean_node (cmp == NE_EXPR, type); }
1946 (if (wi::lt_p (wi::to_wide (@1), wi::to_wide (@2),
1947 TYPE_SIGN (TREE_TYPE (@0))))
1949 /* MIN (X, C1) < C2 -> X < C2 || C1 < C2 */
1950 (for minmax (min min max max min min max max )
1951 cmp (lt le gt ge gt ge lt le )
1952 comb (bit_ior bit_ior bit_ior bit_ior bit_and bit_and bit_and bit_and)
1954 (cmp (minmax @0 INTEGER_CST@1) INTEGER_CST@2)
1955 (comb (cmp @0 @2) (cmp @1 @2))))
1957 /* Simplifications of shift and rotates. */
1959 (for rotate (lrotate rrotate)
1961 (rotate integer_all_onesp@0 @1)
1964 /* Optimize -1 >> x for arithmetic right shifts. */
1966 (rshift integer_all_onesp@0 @1)
1967 (if (!TYPE_UNSIGNED (type)
1968 && tree_expr_nonnegative_p (@1))
1971 /* Optimize (x >> c) << c into x & (-1<<c). */
1973 (lshift (rshift @0 INTEGER_CST@1) @1)
1974 (if (wi::ltu_p (wi::to_wide (@1), element_precision (type)))
1975 (bit_and @0 (lshift { build_minus_one_cst (type); } @1))))
1977 /* Optimize (x << c) >> c into x & ((unsigned)-1 >> c) for unsigned
1980 (rshift (lshift @0 INTEGER_CST@1) @1)
1981 (if (TYPE_UNSIGNED (type)
1982 && (wi::ltu_p (wi::to_wide (@1), element_precision (type))))
1983 (bit_and @0 (rshift { build_minus_one_cst (type); } @1))))
1985 (for shiftrotate (lrotate rrotate lshift rshift)
1987 (shiftrotate @0 integer_zerop)
1990 (shiftrotate integer_zerop@0 @1)
1992 /* Prefer vector1 << scalar to vector1 << vector2
1993 if vector2 is uniform. */
1994 (for vec (VECTOR_CST CONSTRUCTOR)
1996 (shiftrotate @0 vec@1)
1997 (with { tree tem = uniform_vector_p (@1); }
1999 (shiftrotate @0 { tem; }))))))
2001 /* Simplify X << Y where Y's low width bits are 0 to X, as only valid
2002 Y is 0. Similarly for X >> Y. */
2004 (for shift (lshift rshift)
2006 (shift @0 SSA_NAME@1)
2007 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1)))
2009 int width = ceil_log2 (element_precision (TREE_TYPE (@0)));
2010 int prec = TYPE_PRECISION (TREE_TYPE (@1));
2012 (if ((get_nonzero_bits (@1) & wi::mask (width, false, prec)) == 0)
2016 /* Rewrite an LROTATE_EXPR by a constant into an
2017 RROTATE_EXPR by a new constant. */
2019 (lrotate @0 INTEGER_CST@1)
2020 (rrotate @0 { const_binop (MINUS_EXPR, TREE_TYPE (@1),
2021 build_int_cst (TREE_TYPE (@1),
2022 element_precision (type)), @1); }))
2024 /* Turn (a OP c1) OP c2 into a OP (c1+c2). */
2025 (for op (lrotate rrotate rshift lshift)
2027 (op (op @0 INTEGER_CST@1) INTEGER_CST@2)
2028 (with { unsigned int prec = element_precision (type); }
2029 (if (wi::ge_p (wi::to_wide (@1), 0, TYPE_SIGN (TREE_TYPE (@1)))
2030 && wi::lt_p (wi::to_wide (@1), prec, TYPE_SIGN (TREE_TYPE (@1)))
2031 && wi::ge_p (wi::to_wide (@2), 0, TYPE_SIGN (TREE_TYPE (@2)))
2032 && wi::lt_p (wi::to_wide (@2), prec, TYPE_SIGN (TREE_TYPE (@2))))
2033 (with { unsigned int low = (tree_to_uhwi (@1)
2034 + tree_to_uhwi (@2)); }
2035 /* Deal with a OP (c1 + c2) being undefined but (a OP c1) OP c2
2036 being well defined. */
2038 (if (op == LROTATE_EXPR || op == RROTATE_EXPR)
2039 (op @0 { build_int_cst (TREE_TYPE (@1), low % prec); })
2040 (if (TYPE_UNSIGNED (type) || op == LSHIFT_EXPR)
2041 { build_zero_cst (type); }
2042 (op @0 { build_int_cst (TREE_TYPE (@1), prec - 1); })))
2043 (op @0 { build_int_cst (TREE_TYPE (@1), low); })))))))
2046 /* ((1 << A) & 1) != 0 -> A == 0
2047 ((1 << A) & 1) == 0 -> A != 0 */
2051 (cmp (bit_and (lshift integer_onep @0) integer_onep) integer_zerop)
2052 (icmp @0 { build_zero_cst (TREE_TYPE (@0)); })))
2054 /* (CST1 << A) == CST2 -> A == ctz (CST2) - ctz (CST1)
2055 (CST1 << A) != CST2 -> A != ctz (CST2) - ctz (CST1)
2059 (cmp (lshift INTEGER_CST@0 @1) INTEGER_CST@2)
2060 (with { int cand = wi::ctz (wi::to_wide (@2)) - wi::ctz (wi::to_wide (@0)); }
2062 || (!integer_zerop (@2)
2063 && wi::lshift (wi::to_wide (@0), cand) != wi::to_wide (@2)))
2064 { constant_boolean_node (cmp == NE_EXPR, type); }
2065 (if (!integer_zerop (@2)
2066 && wi::lshift (wi::to_wide (@0), cand) == wi::to_wide (@2))
2067 (cmp @1 { build_int_cst (TREE_TYPE (@1), cand); }))))))
2069 /* Fold (X << C1) & C2 into (X << C1) & (C2 | ((1 << C1) - 1))
2070 (X >> C1) & C2 into (X >> C1) & (C2 | ~((type) -1 >> C1))
2071 if the new mask might be further optimized. */
2072 (for shift (lshift rshift)
2074 (bit_and (convert?:s@4 (shift:s@5 (convert1?@3 @0) INTEGER_CST@1))
2076 (if (tree_nop_conversion_p (TREE_TYPE (@4), TREE_TYPE (@5))
2077 && TYPE_PRECISION (type) <= HOST_BITS_PER_WIDE_INT
2078 && tree_fits_uhwi_p (@1)
2079 && tree_to_uhwi (@1) > 0
2080 && tree_to_uhwi (@1) < TYPE_PRECISION (type))
2083 unsigned int shiftc = tree_to_uhwi (@1);
2084 unsigned HOST_WIDE_INT mask = TREE_INT_CST_LOW (@2);
2085 unsigned HOST_WIDE_INT newmask, zerobits = 0;
2086 tree shift_type = TREE_TYPE (@3);
2089 if (shift == LSHIFT_EXPR)
2090 zerobits = ((HOST_WIDE_INT_1U << shiftc) - 1);
2091 else if (shift == RSHIFT_EXPR
2092 && type_has_mode_precision_p (shift_type))
2094 prec = TYPE_PRECISION (TREE_TYPE (@3));
2096 /* See if more bits can be proven as zero because of
2099 && TYPE_UNSIGNED (TREE_TYPE (@0)))
2101 tree inner_type = TREE_TYPE (@0);
2102 if (type_has_mode_precision_p (inner_type)
2103 && TYPE_PRECISION (inner_type) < prec)
2105 prec = TYPE_PRECISION (inner_type);
2106 /* See if we can shorten the right shift. */
2108 shift_type = inner_type;
2109 /* Otherwise X >> C1 is all zeros, so we'll optimize
2110 it into (X, 0) later on by making sure zerobits
2114 zerobits = HOST_WIDE_INT_M1U;
2117 zerobits >>= HOST_BITS_PER_WIDE_INT - shiftc;
2118 zerobits <<= prec - shiftc;
2120 /* For arithmetic shift if sign bit could be set, zerobits
2121 can contain actually sign bits, so no transformation is
2122 possible, unless MASK masks them all away. In that
2123 case the shift needs to be converted into logical shift. */
2124 if (!TYPE_UNSIGNED (TREE_TYPE (@3))
2125 && prec == TYPE_PRECISION (TREE_TYPE (@3)))
2127 if ((mask & zerobits) == 0)
2128 shift_type = unsigned_type_for (TREE_TYPE (@3));
2134 /* ((X << 16) & 0xff00) is (X, 0). */
2135 (if ((mask & zerobits) == mask)
2136 { build_int_cst (type, 0); }
2137 (with { newmask = mask | zerobits; }
2138 (if (newmask != mask && (newmask & (newmask + 1)) == 0)
2141 /* Only do the transformation if NEWMASK is some integer
2143 for (prec = BITS_PER_UNIT;
2144 prec < HOST_BITS_PER_WIDE_INT; prec <<= 1)
2145 if (newmask == (HOST_WIDE_INT_1U << prec) - 1)
2148 (if (prec < HOST_BITS_PER_WIDE_INT
2149 || newmask == HOST_WIDE_INT_M1U)
2151 { tree newmaskt = build_int_cst_type (TREE_TYPE (@2), newmask); }
2152 (if (!tree_int_cst_equal (newmaskt, @2))
2153 (if (shift_type != TREE_TYPE (@3))
2154 (bit_and (convert (shift:shift_type (convert @3) @1)) { newmaskt; })
2155 (bit_and @4 { newmaskt; })))))))))))))
2157 /* Fold (X {&,^,|} C2) << C1 into (X << C1) {&,^,|} (C2 << C1)
2158 (X {&,^,|} C2) >> C1 into (X >> C1) & (C2 >> C1). */
2159 (for shift (lshift rshift)
2160 (for bit_op (bit_and bit_xor bit_ior)
2162 (shift (convert?:s (bit_op:s @0 INTEGER_CST@2)) INTEGER_CST@1)
2163 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2164 (with { tree mask = int_const_binop (shift, fold_convert (type, @2), @1); }
2165 (bit_op (shift (convert @0) @1) { mask; }))))))
2167 /* ~(~X >> Y) -> X >> Y (for arithmetic shift). */
2169 (bit_not (convert1?:s (rshift:s (convert2?@0 (bit_not @1)) @2)))
2170 (if (!TYPE_UNSIGNED (TREE_TYPE (@0))
2171 && (element_precision (TREE_TYPE (@0))
2172 <= element_precision (TREE_TYPE (@1))
2173 || !TYPE_UNSIGNED (TREE_TYPE (@1))))
2175 { tree shift_type = TREE_TYPE (@0); }
2176 (convert (rshift (convert:shift_type @1) @2)))))
2178 /* ~(~X >>r Y) -> X >>r Y
2179 ~(~X <<r Y) -> X <<r Y */
2180 (for rotate (lrotate rrotate)
2182 (bit_not (convert1?:s (rotate:s (convert2?@0 (bit_not @1)) @2)))
2183 (if ((element_precision (TREE_TYPE (@0))
2184 <= element_precision (TREE_TYPE (@1))
2185 || !TYPE_UNSIGNED (TREE_TYPE (@1)))
2186 && (element_precision (type) <= element_precision (TREE_TYPE (@0))
2187 || !TYPE_UNSIGNED (TREE_TYPE (@0))))
2189 { tree rotate_type = TREE_TYPE (@0); }
2190 (convert (rotate (convert:rotate_type @1) @2))))))
2192 /* Simplifications of conversions. */
2194 /* Basic strip-useless-type-conversions / strip_nops. */
2195 (for cvt (convert view_convert float fix_trunc)
2198 (if ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@0)))
2199 || (GENERIC && type == TREE_TYPE (@0)))
2202 /* Contract view-conversions. */
2204 (view_convert (view_convert @0))
2207 /* For integral conversions with the same precision or pointer
2208 conversions use a NOP_EXPR instead. */
2211 (if ((INTEGRAL_TYPE_P (type) || POINTER_TYPE_P (type))
2212 && (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
2213 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (@0)))
2216 /* Strip inner integral conversions that do not change precision or size, or
2217 zero-extend while keeping the same size (for bool-to-char). */
2219 (view_convert (convert@0 @1))
2220 (if ((INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
2221 && (INTEGRAL_TYPE_P (TREE_TYPE (@1)) || POINTER_TYPE_P (TREE_TYPE (@1)))
2222 && TYPE_SIZE (TREE_TYPE (@0)) == TYPE_SIZE (TREE_TYPE (@1))
2223 && (TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1))
2224 || (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (TREE_TYPE (@1))
2225 && TYPE_UNSIGNED (TREE_TYPE (@1)))))
2228 /* Re-association barriers around constants and other re-association
2229 barriers can be removed. */
2231 (paren CONSTANT_CLASS_P@0)
2234 (paren (paren@1 @0))
2237 /* Handle cases of two conversions in a row. */
2238 (for ocvt (convert float fix_trunc)
2239 (for icvt (convert float)
2244 tree inside_type = TREE_TYPE (@0);
2245 tree inter_type = TREE_TYPE (@1);
2246 int inside_int = INTEGRAL_TYPE_P (inside_type);
2247 int inside_ptr = POINTER_TYPE_P (inside_type);
2248 int inside_float = FLOAT_TYPE_P (inside_type);
2249 int inside_vec = VECTOR_TYPE_P (inside_type);
2250 unsigned int inside_prec = TYPE_PRECISION (inside_type);
2251 int inside_unsignedp = TYPE_UNSIGNED (inside_type);
2252 int inter_int = INTEGRAL_TYPE_P (inter_type);
2253 int inter_ptr = POINTER_TYPE_P (inter_type);
2254 int inter_float = FLOAT_TYPE_P (inter_type);
2255 int inter_vec = VECTOR_TYPE_P (inter_type);
2256 unsigned int inter_prec = TYPE_PRECISION (inter_type);
2257 int inter_unsignedp = TYPE_UNSIGNED (inter_type);
2258 int final_int = INTEGRAL_TYPE_P (type);
2259 int final_ptr = POINTER_TYPE_P (type);
2260 int final_float = FLOAT_TYPE_P (type);
2261 int final_vec = VECTOR_TYPE_P (type);
2262 unsigned int final_prec = TYPE_PRECISION (type);
2263 int final_unsignedp = TYPE_UNSIGNED (type);
2266 /* In addition to the cases of two conversions in a row
2267 handled below, if we are converting something to its own
2268 type via an object of identical or wider precision, neither
2269 conversion is needed. */
2270 (if (((GIMPLE && useless_type_conversion_p (type, inside_type))
2272 && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (inside_type)))
2273 && (((inter_int || inter_ptr) && final_int)
2274 || (inter_float && final_float))
2275 && inter_prec >= final_prec)
2278 /* Likewise, if the intermediate and initial types are either both
2279 float or both integer, we don't need the middle conversion if the
2280 former is wider than the latter and doesn't change the signedness
2281 (for integers). Avoid this if the final type is a pointer since
2282 then we sometimes need the middle conversion. */
2283 (if (((inter_int && inside_int) || (inter_float && inside_float))
2284 && (final_int || final_float)
2285 && inter_prec >= inside_prec
2286 && (inter_float || inter_unsignedp == inside_unsignedp))
2289 /* If we have a sign-extension of a zero-extended value, we can
2290 replace that by a single zero-extension. Likewise if the
2291 final conversion does not change precision we can drop the
2292 intermediate conversion. */
2293 (if (inside_int && inter_int && final_int
2294 && ((inside_prec < inter_prec && inter_prec < final_prec
2295 && inside_unsignedp && !inter_unsignedp)
2296 || final_prec == inter_prec))
2299 /* Two conversions in a row are not needed unless:
2300 - some conversion is floating-point (overstrict for now), or
2301 - some conversion is a vector (overstrict for now), or
2302 - the intermediate type is narrower than both initial and
2304 - the intermediate type and innermost type differ in signedness,
2305 and the outermost type is wider than the intermediate, or
2306 - the initial type is a pointer type and the precisions of the
2307 intermediate and final types differ, or
2308 - the final type is a pointer type and the precisions of the
2309 initial and intermediate types differ. */
2310 (if (! inside_float && ! inter_float && ! final_float
2311 && ! inside_vec && ! inter_vec && ! final_vec
2312 && (inter_prec >= inside_prec || inter_prec >= final_prec)
2313 && ! (inside_int && inter_int
2314 && inter_unsignedp != inside_unsignedp
2315 && inter_prec < final_prec)
2316 && ((inter_unsignedp && inter_prec > inside_prec)
2317 == (final_unsignedp && final_prec > inter_prec))
2318 && ! (inside_ptr && inter_prec != final_prec)
2319 && ! (final_ptr && inside_prec != inter_prec))
2322 /* A truncation to an unsigned type (a zero-extension) should be
2323 canonicalized as bitwise and of a mask. */
2324 (if (GIMPLE /* PR70366: doing this in GENERIC breaks -Wconversion. */
2325 && final_int && inter_int && inside_int
2326 && final_prec == inside_prec
2327 && final_prec > inter_prec
2329 (convert (bit_and @0 { wide_int_to_tree
2331 wi::mask (inter_prec, false,
2332 TYPE_PRECISION (inside_type))); })))
2334 /* If we are converting an integer to a floating-point that can
2335 represent it exactly and back to an integer, we can skip the
2336 floating-point conversion. */
2337 (if (GIMPLE /* PR66211 */
2338 && inside_int && inter_float && final_int &&
2339 (unsigned) significand_size (TYPE_MODE (inter_type))
2340 >= inside_prec - !inside_unsignedp)
2343 /* If we have a narrowing conversion to an integral type that is fed by a
2344 BIT_AND_EXPR, we might be able to remove the BIT_AND_EXPR if it merely
2345 masks off bits outside the final type (and nothing else). */
2347 (convert (bit_and @0 INTEGER_CST@1))
2348 (if (INTEGRAL_TYPE_P (type)
2349 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
2350 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))
2351 && operand_equal_p (@1, build_low_bits_mask (TREE_TYPE (@1),
2352 TYPE_PRECISION (type)), 0))
2356 /* (X /[ex] A) * A -> X. */
2358 (mult (convert1? (exact_div @0 @@1)) (convert2? @1))
2361 /* Canonicalization of binary operations. */
2363 /* Convert X + -C into X - C. */
2365 (plus @0 REAL_CST@1)
2366 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
2367 (with { tree tem = const_unop (NEGATE_EXPR, type, @1); }
2368 (if (!TREE_OVERFLOW (tem) || !flag_trapping_math)
2369 (minus @0 { tem; })))))
2371 /* Convert x+x into x*2. */
2374 (if (SCALAR_FLOAT_TYPE_P (type))
2375 (mult @0 { build_real (type, dconst2); })
2376 (if (INTEGRAL_TYPE_P (type))
2377 (mult @0 { build_int_cst (type, 2); }))))
2380 (minus integer_zerop @1)
2383 /* (ARG0 - ARG1) is the same as (-ARG1 + ARG0). So check whether
2384 ARG0 is zero and X + ARG0 reduces to X, since that would mean
2385 (-ARG1 + ARG0) reduces to -ARG1. */
2387 (minus real_zerop@0 @1)
2388 (if (fold_real_zero_addition_p (type, @0, 0))
2391 /* Transform x * -1 into -x. */
2393 (mult @0 integer_minus_onep)
2396 /* Reassociate (X * CST) * Y to (X * Y) * CST. This does not introduce
2397 signed overflow for CST != 0 && CST != -1. */
2399 (mult:c (mult:s @0 INTEGER_CST@1) @2)
2400 (if (TREE_CODE (@2) != INTEGER_CST
2401 && !integer_zerop (@1) && !integer_minus_onep (@1))
2402 (mult (mult @0 @2) @1)))
2404 /* True if we can easily extract the real and imaginary parts of a complex
2406 (match compositional_complex
2407 (convert? (complex @0 @1)))
2409 /* COMPLEX_EXPR and REALPART/IMAGPART_EXPR cancellations. */
2411 (complex (realpart @0) (imagpart @0))
2414 (realpart (complex @0 @1))
2417 (imagpart (complex @0 @1))
2420 /* Sometimes we only care about half of a complex expression. */
2422 (realpart (convert?:s (conj:s @0)))
2423 (convert (realpart @0)))
2425 (imagpart (convert?:s (conj:s @0)))
2426 (convert (negate (imagpart @0))))
2427 (for part (realpart imagpart)
2428 (for op (plus minus)
2430 (part (convert?:s@2 (op:s @0 @1)))
2431 (convert (op (part @0) (part @1))))))
2433 (realpart (convert?:s (CEXPI:s @0)))
2436 (imagpart (convert?:s (CEXPI:s @0)))
2439 /* conj(conj(x)) -> x */
2441 (conj (convert? (conj @0)))
2442 (if (tree_nop_conversion_p (TREE_TYPE (@0), type))
2445 /* conj({x,y}) -> {x,-y} */
2447 (conj (convert?:s (complex:s @0 @1)))
2448 (with { tree itype = TREE_TYPE (type); }
2449 (complex (convert:itype @0) (negate (convert:itype @1)))))
2451 /* BSWAP simplifications, transforms checked by gcc.dg/builtin-bswap-8.c. */
2452 (for bswap (BUILT_IN_BSWAP16 BUILT_IN_BSWAP32 BUILT_IN_BSWAP64)
2457 (bswap (bit_not (bswap @0)))
2459 (for bitop (bit_xor bit_ior bit_and)
2461 (bswap (bitop:c (bswap @0) @1))
2462 (bitop @0 (bswap @1)))))
2465 /* Combine COND_EXPRs and VEC_COND_EXPRs. */
2467 /* Simplify constant conditions.
2468 Only optimize constant conditions when the selected branch
2469 has the same type as the COND_EXPR. This avoids optimizing
2470 away "c ? x : throw", where the throw has a void type.
2471 Note that we cannot throw away the fold-const.c variant nor
2472 this one as we depend on doing this transform before possibly
2473 A ? B : B -> B triggers and the fold-const.c one can optimize
2474 0 ? A : B to B even if A has side-effects. Something
2475 genmatch cannot handle. */
2477 (cond INTEGER_CST@0 @1 @2)
2478 (if (integer_zerop (@0))
2479 (if (!VOID_TYPE_P (TREE_TYPE (@2)) || VOID_TYPE_P (type))
2481 (if (!VOID_TYPE_P (TREE_TYPE (@1)) || VOID_TYPE_P (type))
2484 (vec_cond VECTOR_CST@0 @1 @2)
2485 (if (integer_all_onesp (@0))
2487 (if (integer_zerop (@0))
2490 /* Simplification moved from fold_cond_expr_with_comparison. It may also
2492 /* This pattern implements two kinds simplification:
2495 (cond (cmp (convert1? x) c1) (convert2? x) c2) -> (minmax (x c)) if:
2496 1) Conversions are type widening from smaller type.
2497 2) Const c1 equals to c2 after canonicalizing comparison.
2498 3) Comparison has tree code LT, LE, GT or GE.
2499 This specific pattern is needed when (cmp (convert x) c) may not
2500 be simplified by comparison patterns because of multiple uses of
2501 x. It also makes sense here because simplifying across multiple
2502 referred var is always benefitial for complicated cases.
2505 (cond (eq (convert1? x) c1) (convert2? x) c2) -> (cond (eq x c1) c1 c2). */
2506 (for cmp (lt le gt ge eq)
2508 (cond (cmp (convert1? @1) INTEGER_CST@3) (convert2? @1) INTEGER_CST@2)
2511 tree from_type = TREE_TYPE (@1);
2512 tree c1_type = TREE_TYPE (@3), c2_type = TREE_TYPE (@2);
2513 enum tree_code code = ERROR_MARK;
2515 if (INTEGRAL_TYPE_P (from_type)
2516 && int_fits_type_p (@2, from_type)
2517 && (types_match (c1_type, from_type)
2518 || (TYPE_PRECISION (c1_type) > TYPE_PRECISION (from_type)
2519 && (TYPE_UNSIGNED (from_type)
2520 || TYPE_SIGN (c1_type) == TYPE_SIGN (from_type))))
2521 && (types_match (c2_type, from_type)
2522 || (TYPE_PRECISION (c2_type) > TYPE_PRECISION (from_type)
2523 && (TYPE_UNSIGNED (from_type)
2524 || TYPE_SIGN (c2_type) == TYPE_SIGN (from_type)))))
2528 if (wi::to_widest (@3) == (wi::to_widest (@2) - 1))
2530 /* X <= Y - 1 equals to X < Y. */
2533 /* X > Y - 1 equals to X >= Y. */
2537 if (wi::to_widest (@3) == (wi::to_widest (@2) + 1))
2539 /* X < Y + 1 equals to X <= Y. */
2542 /* X >= Y + 1 equals to X > Y. */
2546 if (code != ERROR_MARK
2547 || wi::to_widest (@2) == wi::to_widest (@3))
2549 if (cmp == LT_EXPR || cmp == LE_EXPR)
2551 if (cmp == GT_EXPR || cmp == GE_EXPR)
2555 /* Can do A == C1 ? A : C2 -> A == C1 ? C1 : C2? */
2556 else if (int_fits_type_p (@3, from_type))
2560 (if (code == MAX_EXPR)
2561 (convert (max @1 (convert @2)))
2562 (if (code == MIN_EXPR)
2563 (convert (min @1 (convert @2)))
2564 (if (code == EQ_EXPR)
2565 (convert (cond (eq @1 (convert @3))
2566 (convert:from_type @3) (convert:from_type @2)))))))))
2568 /* (cond (cmp (convert? x) c1) (op x c2) c3) -> (op (minmax x c1) c2) if:
2570 1) OP is PLUS or MINUS.
2571 2) CMP is LT, LE, GT or GE.
2572 3) C3 == (C1 op C2), and computation doesn't have undefined behavior.
2574 This pattern also handles special cases like:
2576 A) Operand x is a unsigned to signed type conversion and c1 is
2577 integer zero. In this case,
2578 (signed type)x < 0 <=> x > MAX_VAL(signed type)
2579 (signed type)x >= 0 <=> x <= MAX_VAL(signed type)
2580 B) Const c1 may not equal to (C3 op' C2). In this case we also
2581 check equality for (c1+1) and (c1-1) by adjusting comparison
2584 TODO: Though signed type is handled by this pattern, it cannot be
2585 simplified at the moment because C standard requires additional
2586 type promotion. In order to match&simplify it here, the IR needs
2587 to be cleaned up by other optimizers, i.e, VRP. */
2588 (for op (plus minus)
2589 (for cmp (lt le gt ge)
2591 (cond (cmp (convert? @X) INTEGER_CST@1) (op @X INTEGER_CST@2) INTEGER_CST@3)
2592 (with { tree from_type = TREE_TYPE (@X), to_type = TREE_TYPE (@1); }
2593 (if (types_match (from_type, to_type)
2594 /* Check if it is special case A). */
2595 || (TYPE_UNSIGNED (from_type)
2596 && !TYPE_UNSIGNED (to_type)
2597 && TYPE_PRECISION (from_type) == TYPE_PRECISION (to_type)
2598 && integer_zerop (@1)
2599 && (cmp == LT_EXPR || cmp == GE_EXPR)))
2602 bool overflow = false;
2603 enum tree_code code, cmp_code = cmp;
2605 wide_int c1 = wi::to_wide (@1);
2606 wide_int c2 = wi::to_wide (@2);
2607 wide_int c3 = wi::to_wide (@3);
2608 signop sgn = TYPE_SIGN (from_type);
2610 /* Handle special case A), given x of unsigned type:
2611 ((signed type)x < 0) <=> (x > MAX_VAL(signed type))
2612 ((signed type)x >= 0) <=> (x <= MAX_VAL(signed type)) */
2613 if (!types_match (from_type, to_type))
2615 if (cmp_code == LT_EXPR)
2617 if (cmp_code == GE_EXPR)
2619 c1 = wi::max_value (to_type);
2621 /* To simplify this pattern, we require c3 = (c1 op c2). Here we
2622 compute (c3 op' c2) and check if it equals to c1 with op' being
2623 the inverted operator of op. Make sure overflow doesn't happen
2624 if it is undefined. */
2625 if (op == PLUS_EXPR)
2626 real_c1 = wi::sub (c3, c2, sgn, &overflow);
2628 real_c1 = wi::add (c3, c2, sgn, &overflow);
2631 if (!overflow || !TYPE_OVERFLOW_UNDEFINED (from_type))
2633 /* Check if c1 equals to real_c1. Boundary condition is handled
2634 by adjusting comparison operation if necessary. */
2635 if (!wi::cmp (wi::sub (real_c1, 1, sgn, &overflow), c1, sgn)
2638 /* X <= Y - 1 equals to X < Y. */
2639 if (cmp_code == LE_EXPR)
2641 /* X > Y - 1 equals to X >= Y. */
2642 if (cmp_code == GT_EXPR)
2645 if (!wi::cmp (wi::add (real_c1, 1, sgn, &overflow), c1, sgn)
2648 /* X < Y + 1 equals to X <= Y. */
2649 if (cmp_code == LT_EXPR)
2651 /* X >= Y + 1 equals to X > Y. */
2652 if (cmp_code == GE_EXPR)
2655 if (code != cmp_code || !wi::cmp (real_c1, c1, sgn))
2657 if (cmp_code == LT_EXPR || cmp_code == LE_EXPR)
2659 if (cmp_code == GT_EXPR || cmp_code == GE_EXPR)
2664 (if (code == MAX_EXPR)
2665 (op (max @X { wide_int_to_tree (from_type, real_c1); })
2666 { wide_int_to_tree (from_type, c2); })
2667 (if (code == MIN_EXPR)
2668 (op (min @X { wide_int_to_tree (from_type, real_c1); })
2669 { wide_int_to_tree (from_type, c2); })))))))))
2671 (for cnd (cond vec_cond)
2672 /* A ? B : (A ? X : C) -> A ? B : C. */
2674 (cnd @0 (cnd @0 @1 @2) @3)
2677 (cnd @0 @1 (cnd @0 @2 @3))
2679 /* A ? B : (!A ? C : X) -> A ? B : C. */
2680 /* ??? This matches embedded conditions open-coded because genmatch
2681 would generate matching code for conditions in separate stmts only.
2682 The following is still important to merge then and else arm cases
2683 from if-conversion. */
2685 (cnd @0 @1 (cnd @2 @3 @4))
2686 (if (COMPARISON_CLASS_P (@0)
2687 && COMPARISON_CLASS_P (@2)
2688 && invert_tree_comparison
2689 (TREE_CODE (@0), HONOR_NANS (TREE_OPERAND (@0, 0))) == TREE_CODE (@2)
2690 && operand_equal_p (TREE_OPERAND (@0, 0), TREE_OPERAND (@2, 0), 0)
2691 && operand_equal_p (TREE_OPERAND (@0, 1), TREE_OPERAND (@2, 1), 0))
2694 (cnd @0 (cnd @1 @2 @3) @4)
2695 (if (COMPARISON_CLASS_P (@0)
2696 && COMPARISON_CLASS_P (@1)
2697 && invert_tree_comparison
2698 (TREE_CODE (@0), HONOR_NANS (TREE_OPERAND (@0, 0))) == TREE_CODE (@1)
2699 && operand_equal_p (TREE_OPERAND (@0, 0), TREE_OPERAND (@1, 0), 0)
2700 && operand_equal_p (TREE_OPERAND (@0, 1), TREE_OPERAND (@1, 1), 0))
2703 /* A ? B : B -> B. */
2708 /* !A ? B : C -> A ? C : B. */
2710 (cnd (logical_inverted_value truth_valued_p@0) @1 @2)
2713 /* A + (B vcmp C ? 1 : 0) -> A - (B vcmp C ? -1 : 0), since vector comparisons
2714 return all -1 or all 0 results. */
2715 /* ??? We could instead convert all instances of the vec_cond to negate,
2716 but that isn't necessarily a win on its own. */
2718 (plus:c @3 (view_convert? (vec_cond:s @0 integer_each_onep@1 integer_zerop@2)))
2719 (if (VECTOR_TYPE_P (type)
2720 && TYPE_VECTOR_SUBPARTS (type) == TYPE_VECTOR_SUBPARTS (TREE_TYPE (@1))
2721 && (TYPE_MODE (TREE_TYPE (type))
2722 == TYPE_MODE (TREE_TYPE (TREE_TYPE (@1)))))
2723 (minus @3 (view_convert (vec_cond @0 (negate @1) @2)))))
2725 /* ... likewise A - (B vcmp C ? 1 : 0) -> A + (B vcmp C ? -1 : 0). */
2727 (minus @3 (view_convert? (vec_cond:s @0 integer_each_onep@1 integer_zerop@2)))
2728 (if (VECTOR_TYPE_P (type)
2729 && TYPE_VECTOR_SUBPARTS (type) == TYPE_VECTOR_SUBPARTS (TREE_TYPE (@1))
2730 && (TYPE_MODE (TREE_TYPE (type))
2731 == TYPE_MODE (TREE_TYPE (TREE_TYPE (@1)))))
2732 (plus @3 (view_convert (vec_cond @0 (negate @1) @2)))))
2735 /* Simplifications of comparisons. */
2737 /* See if we can reduce the magnitude of a constant involved in a
2738 comparison by changing the comparison code. This is a canonicalization
2739 formerly done by maybe_canonicalize_comparison_1. */
2743 (cmp @0 INTEGER_CST@1)
2744 (if (tree_int_cst_sgn (@1) == -1)
2745 (acmp @0 { wide_int_to_tree (TREE_TYPE (@1), wi::to_wide (@1) + 1); }))))
2749 (cmp @0 INTEGER_CST@1)
2750 (if (tree_int_cst_sgn (@1) == 1)
2751 (acmp @0 { wide_int_to_tree (TREE_TYPE (@1), wi::to_wide (@1) - 1); }))))
2754 /* We can simplify a logical negation of a comparison to the
2755 inverted comparison. As we cannot compute an expression
2756 operator using invert_tree_comparison we have to simulate
2757 that with expression code iteration. */
2758 (for cmp (tcc_comparison)
2759 icmp (inverted_tcc_comparison)
2760 ncmp (inverted_tcc_comparison_with_nans)
2761 /* Ideally we'd like to combine the following two patterns
2762 and handle some more cases by using
2763 (logical_inverted_value (cmp @0 @1))
2764 here but for that genmatch would need to "inline" that.
2765 For now implement what forward_propagate_comparison did. */
2767 (bit_not (cmp @0 @1))
2768 (if (VECTOR_TYPE_P (type)
2769 || (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1))
2770 /* Comparison inversion may be impossible for trapping math,
2771 invert_tree_comparison will tell us. But we can't use
2772 a computed operator in the replacement tree thus we have
2773 to play the trick below. */
2774 (with { enum tree_code ic = invert_tree_comparison
2775 (cmp, HONOR_NANS (@0)); }
2781 (bit_xor (cmp @0 @1) integer_truep)
2782 (with { enum tree_code ic = invert_tree_comparison
2783 (cmp, HONOR_NANS (@0)); }
2789 /* Transform comparisons of the form X - Y CMP 0 to X CMP Y.
2790 ??? The transformation is valid for the other operators if overflow
2791 is undefined for the type, but performing it here badly interacts
2792 with the transformation in fold_cond_expr_with_comparison which
2793 attempts to synthetize ABS_EXPR. */
2796 (cmp (minus@2 @0 @1) integer_zerop)
2797 (if (single_use (@2))
2800 /* Transform comparisons of the form X * C1 CMP 0 to X CMP 0 in the
2801 signed arithmetic case. That form is created by the compiler
2802 often enough for folding it to be of value. One example is in
2803 computing loop trip counts after Operator Strength Reduction. */
2804 (for cmp (simple_comparison)
2805 scmp (swapped_simple_comparison)
2807 (cmp (mult@3 @0 INTEGER_CST@1) integer_zerop@2)
2808 /* Handle unfolded multiplication by zero. */
2809 (if (integer_zerop (@1))
2811 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2812 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2814 /* If @1 is negative we swap the sense of the comparison. */
2815 (if (tree_int_cst_sgn (@1) < 0)
2819 /* Simplify comparison of something with itself. For IEEE
2820 floating-point, we can only do some of these simplifications. */
2824 (if (! FLOAT_TYPE_P (TREE_TYPE (@0))
2825 || ! HONOR_NANS (@0))
2826 { constant_boolean_node (true, type); }
2827 (if (cmp != EQ_EXPR)
2833 || ! FLOAT_TYPE_P (TREE_TYPE (@0))
2834 || ! HONOR_NANS (@0))
2835 { constant_boolean_node (false, type); })))
2836 (for cmp (unle unge uneq)
2839 { constant_boolean_node (true, type); }))
2840 (for cmp (unlt ungt)
2846 (if (!flag_trapping_math)
2847 { constant_boolean_node (false, type); }))
2849 /* Fold ~X op ~Y as Y op X. */
2850 (for cmp (simple_comparison)
2852 (cmp (bit_not@2 @0) (bit_not@3 @1))
2853 (if (single_use (@2) && single_use (@3))
2856 /* Fold ~X op C as X op' ~C, where op' is the swapped comparison. */
2857 (for cmp (simple_comparison)
2858 scmp (swapped_simple_comparison)
2860 (cmp (bit_not@2 @0) CONSTANT_CLASS_P@1)
2861 (if (single_use (@2)
2862 && (TREE_CODE (@1) == INTEGER_CST || TREE_CODE (@1) == VECTOR_CST))
2863 (scmp @0 (bit_not @1)))))
2865 (for cmp (simple_comparison)
2866 /* Fold (double)float1 CMP (double)float2 into float1 CMP float2. */
2868 (cmp (convert@2 @0) (convert? @1))
2869 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
2870 && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@2))
2871 == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0)))
2872 && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@2))
2873 == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1))))
2876 tree type1 = TREE_TYPE (@1);
2877 if (TREE_CODE (@1) == REAL_CST && !DECIMAL_FLOAT_TYPE_P (type1))
2879 REAL_VALUE_TYPE orig = TREE_REAL_CST (@1);
2880 if (TYPE_PRECISION (type1) > TYPE_PRECISION (float_type_node)
2881 && exact_real_truncate (TYPE_MODE (float_type_node), &orig))
2882 type1 = float_type_node;
2883 if (TYPE_PRECISION (type1) > TYPE_PRECISION (double_type_node)
2884 && exact_real_truncate (TYPE_MODE (double_type_node), &orig))
2885 type1 = double_type_node;
2888 = (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type1)
2889 ? TREE_TYPE (@0) : type1);
2891 (if (TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (newtype))
2892 (cmp (convert:newtype @0) (convert:newtype @1))))))
2896 /* IEEE doesn't distinguish +0 and -0 in comparisons. */
2898 /* a CMP (-0) -> a CMP 0 */
2899 (if (REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@1)))
2900 (cmp @0 { build_real (TREE_TYPE (@1), dconst0); }))
2901 /* x != NaN is always true, other ops are always false. */
2902 (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
2903 && ! HONOR_SNANS (@1))
2904 { constant_boolean_node (cmp == NE_EXPR, type); })
2905 /* Fold comparisons against infinity. */
2906 (if (REAL_VALUE_ISINF (TREE_REAL_CST (@1))
2907 && MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (@1))))
2910 REAL_VALUE_TYPE max;
2911 enum tree_code code = cmp;
2912 bool neg = REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1));
2914 code = swap_tree_comparison (code);
2917 /* x > +Inf is always false, if with ignore sNANs. */
2918 (if (code == GT_EXPR
2919 && ! HONOR_SNANS (@0))
2920 { constant_boolean_node (false, type); })
2921 (if (code == LE_EXPR)
2922 /* x <= +Inf is always true, if we don't case about NaNs. */
2923 (if (! HONOR_NANS (@0))
2924 { constant_boolean_node (true, type); }
2925 /* x <= +Inf is the same as x == x, i.e. !isnan(x). */
2927 /* x == +Inf and x >= +Inf are always equal to x > DBL_MAX. */
2928 (if (code == EQ_EXPR || code == GE_EXPR)
2929 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
2931 (lt @0 { build_real (TREE_TYPE (@0), max); })
2932 (gt @0 { build_real (TREE_TYPE (@0), max); }))))
2933 /* x < +Inf is always equal to x <= DBL_MAX. */
2934 (if (code == LT_EXPR)
2935 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
2937 (ge @0 { build_real (TREE_TYPE (@0), max); })
2938 (le @0 { build_real (TREE_TYPE (@0), max); }))))
2939 /* x != +Inf is always equal to !(x > DBL_MAX). */
2940 (if (code == NE_EXPR)
2941 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
2942 (if (! HONOR_NANS (@0))
2944 (ge @0 { build_real (TREE_TYPE (@0), max); })
2945 (le @0 { build_real (TREE_TYPE (@0), max); }))
2947 (bit_xor (lt @0 { build_real (TREE_TYPE (@0), max); })
2948 { build_one_cst (type); })
2949 (bit_xor (gt @0 { build_real (TREE_TYPE (@0), max); })
2950 { build_one_cst (type); }))))))))))
2952 /* If this is a comparison of a real constant with a PLUS_EXPR
2953 or a MINUS_EXPR of a real constant, we can convert it into a
2954 comparison with a revised real constant as long as no overflow
2955 occurs when unsafe_math_optimizations are enabled. */
2956 (if (flag_unsafe_math_optimizations)
2957 (for op (plus minus)
2959 (cmp (op @0 REAL_CST@1) REAL_CST@2)
2962 tree tem = const_binop (op == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR,
2963 TREE_TYPE (@1), @2, @1);
2965 (if (tem && !TREE_OVERFLOW (tem))
2966 (cmp @0 { tem; }))))))
2968 /* Likewise, we can simplify a comparison of a real constant with
2969 a MINUS_EXPR whose first operand is also a real constant, i.e.
2970 (c1 - x) < c2 becomes x > c1-c2. Reordering is allowed on
2971 floating-point types only if -fassociative-math is set. */
2972 (if (flag_associative_math)
2974 (cmp (minus REAL_CST@0 @1) REAL_CST@2)
2975 (with { tree tem = const_binop (MINUS_EXPR, TREE_TYPE (@1), @0, @2); }
2976 (if (tem && !TREE_OVERFLOW (tem))
2977 (cmp { tem; } @1)))))
2979 /* Fold comparisons against built-in math functions. */
2980 (if (flag_unsafe_math_optimizations
2981 && ! flag_errno_math)
2984 (cmp (sq @0) REAL_CST@1)
2986 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
2988 /* sqrt(x) < y is always false, if y is negative. */
2989 (if (cmp == EQ_EXPR || cmp == LT_EXPR || cmp == LE_EXPR)
2990 { constant_boolean_node (false, type); })
2991 /* sqrt(x) > y is always true, if y is negative and we
2992 don't care about NaNs, i.e. negative values of x. */
2993 (if (cmp == NE_EXPR || !HONOR_NANS (@0))
2994 { constant_boolean_node (true, type); })
2995 /* sqrt(x) > y is the same as x >= 0, if y is negative. */
2996 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })))
2997 (if (real_equal (TREE_REAL_CST_PTR (@1), &dconst0))
2999 /* sqrt(x) < 0 is always false. */
3000 (if (cmp == LT_EXPR)
3001 { constant_boolean_node (false, type); })
3002 /* sqrt(x) >= 0 is always true if we don't care about NaNs. */
3003 (if (cmp == GE_EXPR && !HONOR_NANS (@0))
3004 { constant_boolean_node (true, type); })
3005 /* sqrt(x) <= 0 -> x == 0. */
3006 (if (cmp == LE_EXPR)
3008 /* Otherwise sqrt(x) cmp 0 -> x cmp 0. Here cmp can be >=, >,
3009 == or !=. In the last case:
3011 (sqrt(x) != 0) == (NaN != 0) == true == (x != 0)
3013 if x is negative or NaN. Due to -funsafe-math-optimizations,
3014 the results for other x follow from natural arithmetic. */
3016 (if (cmp == GT_EXPR || cmp == GE_EXPR)
3020 real_arithmetic (&c2, MULT_EXPR,
3021 &TREE_REAL_CST (@1), &TREE_REAL_CST (@1));
3022 real_convert (&c2, TYPE_MODE (TREE_TYPE (@0)), &c2);
3024 (if (REAL_VALUE_ISINF (c2))
3025 /* sqrt(x) > y is x == +Inf, when y is very large. */
3026 (if (HONOR_INFINITIES (@0))
3027 (eq @0 { build_real (TREE_TYPE (@0), c2); })
3028 { constant_boolean_node (false, type); })
3029 /* sqrt(x) > c is the same as x > c*c. */
3030 (cmp @0 { build_real (TREE_TYPE (@0), c2); }))))
3031 (if (cmp == LT_EXPR || cmp == LE_EXPR)
3035 real_arithmetic (&c2, MULT_EXPR,
3036 &TREE_REAL_CST (@1), &TREE_REAL_CST (@1));
3037 real_convert (&c2, TYPE_MODE (TREE_TYPE (@0)), &c2);
3039 (if (REAL_VALUE_ISINF (c2))
3041 /* sqrt(x) < y is always true, when y is a very large
3042 value and we don't care about NaNs or Infinities. */
3043 (if (! HONOR_NANS (@0) && ! HONOR_INFINITIES (@0))
3044 { constant_boolean_node (true, type); })
3045 /* sqrt(x) < y is x != +Inf when y is very large and we
3046 don't care about NaNs. */
3047 (if (! HONOR_NANS (@0))
3048 (ne @0 { build_real (TREE_TYPE (@0), c2); }))
3049 /* sqrt(x) < y is x >= 0 when y is very large and we
3050 don't care about Infinities. */
3051 (if (! HONOR_INFINITIES (@0))
3052 (ge @0 { build_real (TREE_TYPE (@0), dconst0); }))
3053 /* sqrt(x) < y is x >= 0 && x != +Inf, when y is large. */
3056 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
3057 (ne @0 { build_real (TREE_TYPE (@0), c2); }))))
3058 /* sqrt(x) < c is the same as x < c*c, if we ignore NaNs. */
3059 (if (! HONOR_NANS (@0))
3060 (cmp @0 { build_real (TREE_TYPE (@0), c2); })
3061 /* sqrt(x) < c is the same as x >= 0 && x < c*c. */
3064 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
3065 (cmp @0 { build_real (TREE_TYPE (@0), c2); })))))))))
3066 /* Transform sqrt(x) cmp sqrt(y) -> x cmp y. */
3068 (cmp (sq @0) (sq @1))
3069 (if (! HONOR_NANS (@0))
3072 /* Optimize various special cases of (FTYPE) N CMP CST. */
3073 (for cmp (lt le eq ne ge gt)
3074 icmp (le le eq ne ge ge)
3076 (cmp (float @0) REAL_CST@1)
3077 (if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (@1))
3078 && ! DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1)))
3081 tree itype = TREE_TYPE (@0);
3082 signop isign = TYPE_SIGN (itype);
3083 format_helper fmt (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (@1))));
3084 const REAL_VALUE_TYPE *cst = TREE_REAL_CST_PTR (@1);
3085 /* Be careful to preserve any potential exceptions due to
3086 NaNs. qNaNs are ok in == or != context.
3087 TODO: relax under -fno-trapping-math or
3088 -fno-signaling-nans. */
3090 = real_isnan (cst) && (cst->signalling
3091 || (cmp != EQ_EXPR && cmp != NE_EXPR));
3092 /* INT?_MIN is power-of-two so it takes
3093 only one mantissa bit. */
3094 bool signed_p = isign == SIGNED;
3095 bool itype_fits_ftype_p
3096 = TYPE_PRECISION (itype) - signed_p <= significand_size (fmt);
3098 /* TODO: allow non-fitting itype and SNaNs when
3099 -fno-trapping-math. */
3100 (if (itype_fits_ftype_p && ! exception_p)
3103 REAL_VALUE_TYPE imin, imax;
3104 real_from_integer (&imin, fmt, wi::min_value (itype), isign);
3105 real_from_integer (&imax, fmt, wi::max_value (itype), isign);
3107 REAL_VALUE_TYPE icst;
3108 if (cmp == GT_EXPR || cmp == GE_EXPR)
3109 real_ceil (&icst, fmt, cst);
3110 else if (cmp == LT_EXPR || cmp == LE_EXPR)
3111 real_floor (&icst, fmt, cst);
3113 real_trunc (&icst, fmt, cst);
3115 bool cst_int_p = !real_isnan (cst) && real_identical (&icst, cst);
3117 bool overflow_p = false;
3119 = real_to_integer (&icst, &overflow_p, TYPE_PRECISION (itype));
3122 /* Optimize cases when CST is outside of ITYPE's range. */
3123 (if (real_compare (LT_EXPR, cst, &imin))
3124 { constant_boolean_node (cmp == GT_EXPR || cmp == GE_EXPR || cmp == NE_EXPR,
3126 (if (real_compare (GT_EXPR, cst, &imax))
3127 { constant_boolean_node (cmp == LT_EXPR || cmp == LE_EXPR || cmp == NE_EXPR,
3129 /* Remove cast if CST is an integer representable by ITYPE. */
3131 (cmp @0 { gcc_assert (!overflow_p);
3132 wide_int_to_tree (itype, icst_val); })
3134 /* When CST is fractional, optimize
3135 (FTYPE) N == CST -> 0
3136 (FTYPE) N != CST -> 1. */
3137 (if (cmp == EQ_EXPR || cmp == NE_EXPR)
3138 { constant_boolean_node (cmp == NE_EXPR, type); })
3139 /* Otherwise replace with sensible integer constant. */
3142 gcc_checking_assert (!overflow_p);
3144 (icmp @0 { wide_int_to_tree (itype, icst_val); })))))))))
3146 /* Fold A /[ex] B CMP C to A CMP B * C. */
3149 (cmp (exact_div @0 @1) INTEGER_CST@2)
3150 (if (!integer_zerop (@1))
3151 (if (wi::to_wide (@2) == 0)
3153 (if (TREE_CODE (@1) == INTEGER_CST)
3157 wide_int prod = wi::mul (wi::to_wide (@2), wi::to_wide (@1),
3158 TYPE_SIGN (TREE_TYPE (@1)), &ovf);
3161 { constant_boolean_node (cmp == NE_EXPR, type); }
3162 (cmp @0 { wide_int_to_tree (TREE_TYPE (@0), prod); }))))))))
3163 (for cmp (lt le gt ge)
3165 (cmp (exact_div @0 INTEGER_CST@1) INTEGER_CST@2)
3166 (if (wi::gt_p (wi::to_wide (@1), 0, TYPE_SIGN (TREE_TYPE (@1))))
3170 wide_int prod = wi::mul (wi::to_wide (@2), wi::to_wide (@1),
3171 TYPE_SIGN (TREE_TYPE (@1)), &ovf);
3174 { constant_boolean_node (wi::lt_p (wi::to_wide (@2), 0,
3175 TYPE_SIGN (TREE_TYPE (@2)))
3176 != (cmp == LT_EXPR || cmp == LE_EXPR), type); }
3177 (cmp @0 { wide_int_to_tree (TREE_TYPE (@0), prod); }))))))
3179 /* Unordered tests if either argument is a NaN. */
3181 (bit_ior (unordered @0 @0) (unordered @1 @1))
3182 (if (types_match (@0, @1))
3185 (bit_and (ordered @0 @0) (ordered @1 @1))
3186 (if (types_match (@0, @1))
3189 (bit_ior:c (unordered @0 @0) (unordered:c@2 @0 @1))
3192 (bit_and:c (ordered @0 @0) (ordered:c@2 @0 @1))
3195 /* Simple range test simplifications. */
3196 /* A < B || A >= B -> true. */
3197 (for test1 (lt le le le ne ge)
3198 test2 (ge gt ge ne eq ne)
3200 (bit_ior:c (test1 @0 @1) (test2 @0 @1))
3201 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
3202 || VECTOR_INTEGER_TYPE_P (TREE_TYPE (@0)))
3203 { constant_boolean_node (true, type); })))
3204 /* A < B && A >= B -> false. */
3205 (for test1 (lt lt lt le ne eq)
3206 test2 (ge gt eq gt eq gt)
3208 (bit_and:c (test1 @0 @1) (test2 @0 @1))
3209 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
3210 || VECTOR_INTEGER_TYPE_P (TREE_TYPE (@0)))
3211 { constant_boolean_node (false, type); })))
3213 /* A & (2**N - 1) <= 2**K - 1 -> A & (2**N - 2**K) == 0
3214 A & (2**N - 1) > 2**K - 1 -> A & (2**N - 2**K) != 0
3216 Note that comparisons
3217 A & (2**N - 1) < 2**K -> A & (2**N - 2**K) == 0
3218 A & (2**N - 1) >= 2**K -> A & (2**N - 2**K) != 0
3219 will be canonicalized to above so there's no need to
3226 (cmp (bit_and@0 @1 INTEGER_CST@2) INTEGER_CST@3)
3227 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)))
3230 tree ty = TREE_TYPE (@0);
3231 unsigned prec = TYPE_PRECISION (ty);
3232 wide_int mask = wi::to_wide (@2, prec);
3233 wide_int rhs = wi::to_wide (@3, prec);
3234 signop sgn = TYPE_SIGN (ty);
3236 (if ((mask & (mask + 1)) == 0 && wi::gt_p (rhs, 0, sgn)
3237 && (rhs & (rhs + 1)) == 0 && wi::ge_p (mask, rhs, sgn))
3238 (eqcmp (bit_and @1 { wide_int_to_tree (ty, mask - rhs); })
3239 { build_zero_cst (ty); }))))))
3241 /* -A CMP -B -> B CMP A. */
3242 (for cmp (tcc_comparison)
3243 scmp (swapped_tcc_comparison)
3245 (cmp (negate @0) (negate @1))
3246 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
3247 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3248 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
3251 (cmp (negate @0) CONSTANT_CLASS_P@1)
3252 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
3253 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3254 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
3255 (with { tree tem = const_unop (NEGATE_EXPR, TREE_TYPE (@0), @1); }
3256 (if (tem && !TREE_OVERFLOW (tem))
3257 (scmp @0 { tem; }))))))
3259 /* Convert ABS_EXPR<x> == 0 or ABS_EXPR<x> != 0 to x == 0 or x != 0. */
3262 (op (abs @0) zerop@1)
3265 /* From fold_sign_changed_comparison and fold_widened_comparison.
3266 FIXME: the lack of symmetry is disturbing. */
3267 (for cmp (simple_comparison)
3269 (cmp (convert@0 @00) (convert?@1 @10))
3270 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
3271 /* Disable this optimization if we're casting a function pointer
3272 type on targets that require function pointer canonicalization. */
3273 && !(targetm.have_canonicalize_funcptr_for_compare ()
3274 && TREE_CODE (TREE_TYPE (@00)) == POINTER_TYPE
3275 && TREE_CODE (TREE_TYPE (TREE_TYPE (@00))) == FUNCTION_TYPE)
3277 (if (TYPE_PRECISION (TREE_TYPE (@00)) == TYPE_PRECISION (TREE_TYPE (@0))
3278 && (TREE_CODE (@10) == INTEGER_CST
3280 && (TYPE_UNSIGNED (TREE_TYPE (@00)) == TYPE_UNSIGNED (TREE_TYPE (@0))
3283 && !POINTER_TYPE_P (TREE_TYPE (@00)))
3284 /* ??? The special-casing of INTEGER_CST conversion was in the original
3285 code and here to avoid a spurious overflow flag on the resulting
3286 constant which fold_convert produces. */
3287 (if (TREE_CODE (@1) == INTEGER_CST)
3288 (cmp @00 { force_fit_type (TREE_TYPE (@00), wi::to_widest (@1), 0,
3289 TREE_OVERFLOW (@1)); })
3290 (cmp @00 (convert @1)))
3292 (if (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (TREE_TYPE (@00)))
3293 /* If possible, express the comparison in the shorter mode. */
3294 (if ((cmp == EQ_EXPR || cmp == NE_EXPR
3295 || TYPE_UNSIGNED (TREE_TYPE (@0)) == TYPE_UNSIGNED (TREE_TYPE (@00))
3296 || (!TYPE_UNSIGNED (TREE_TYPE (@0))
3297 && TYPE_UNSIGNED (TREE_TYPE (@00))))
3298 && (types_match (TREE_TYPE (@10), TREE_TYPE (@00))
3299 || ((TYPE_PRECISION (TREE_TYPE (@00))
3300 >= TYPE_PRECISION (TREE_TYPE (@10)))
3301 && (TYPE_UNSIGNED (TREE_TYPE (@00))
3302 == TYPE_UNSIGNED (TREE_TYPE (@10))))
3303 || (TREE_CODE (@10) == INTEGER_CST
3304 && INTEGRAL_TYPE_P (TREE_TYPE (@00))
3305 && int_fits_type_p (@10, TREE_TYPE (@00)))))
3306 (cmp @00 (convert @10))
3307 (if (TREE_CODE (@10) == INTEGER_CST
3308 && INTEGRAL_TYPE_P (TREE_TYPE (@00))
3309 && !int_fits_type_p (@10, TREE_TYPE (@00)))
3312 tree min = lower_bound_in_type (TREE_TYPE (@10), TREE_TYPE (@00));
3313 tree max = upper_bound_in_type (TREE_TYPE (@10), TREE_TYPE (@00));
3314 bool above = integer_nonzerop (const_binop (LT_EXPR, type, max, @10));
3315 bool below = integer_nonzerop (const_binop (LT_EXPR, type, @10, min));
3317 (if (above || below)
3318 (if (cmp == EQ_EXPR || cmp == NE_EXPR)
3319 { constant_boolean_node (cmp == EQ_EXPR ? false : true, type); }
3320 (if (cmp == LT_EXPR || cmp == LE_EXPR)
3321 { constant_boolean_node (above ? true : false, type); }
3322 (if (cmp == GT_EXPR || cmp == GE_EXPR)
3323 { constant_boolean_node (above ? false : true, type); }))))))))))))
3326 /* A local variable can never be pointed to by
3327 the default SSA name of an incoming parameter.
3328 SSA names are canonicalized to 2nd place. */
3330 (cmp addr@0 SSA_NAME@1)
3331 (if (SSA_NAME_IS_DEFAULT_DEF (@1)
3332 && TREE_CODE (SSA_NAME_VAR (@1)) == PARM_DECL)
3333 (with { tree base = get_base_address (TREE_OPERAND (@0, 0)); }
3334 (if (TREE_CODE (base) == VAR_DECL
3335 && auto_var_in_fn_p (base, current_function_decl))
3336 (if (cmp == NE_EXPR)
3337 { constant_boolean_node (true, type); }
3338 { constant_boolean_node (false, type); }))))))
3340 /* Equality compare simplifications from fold_binary */
3343 /* If we have (A | C) == D where C & ~D != 0, convert this into 0.
3344 Similarly for NE_EXPR. */
3346 (cmp (convert?@3 (bit_ior @0 INTEGER_CST@1)) INTEGER_CST@2)
3347 (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0))
3348 && wi::bit_and_not (wi::to_wide (@1), wi::to_wide (@2)) != 0)
3349 { constant_boolean_node (cmp == NE_EXPR, type); }))
3351 /* (X ^ Y) == 0 becomes X == Y, and (X ^ Y) != 0 becomes X != Y. */
3353 (cmp (bit_xor @0 @1) integer_zerop)
3356 /* (X ^ Y) == Y becomes X == 0.
3357 Likewise (X ^ Y) == X becomes Y == 0. */
3359 (cmp:c (bit_xor:c @0 @1) @0)
3360 (cmp @1 { build_zero_cst (TREE_TYPE (@1)); }))
3362 /* (X ^ C1) op C2 can be rewritten as X op (C1 ^ C2). */
3364 (cmp (convert?@3 (bit_xor @0 INTEGER_CST@1)) INTEGER_CST@2)
3365 (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0)))
3366 (cmp @0 (bit_xor @1 (convert @2)))))
3369 (cmp (convert? addr@0) integer_zerop)
3370 (if (tree_single_nonzero_warnv_p (@0, NULL))
3371 { constant_boolean_node (cmp == NE_EXPR, type); })))
3373 /* If we have (A & C) == C where C is a power of 2, convert this into
3374 (A & C) != 0. Similarly for NE_EXPR. */
3378 (cmp (bit_and@2 @0 integer_pow2p@1) @1)
3379 (icmp @2 { build_zero_cst (TREE_TYPE (@0)); })))
3381 /* If we have (A & C) != 0 ? D : 0 where C and D are powers of 2,
3382 convert this into a shift followed by ANDing with D. */
3385 (ne (bit_and @0 integer_pow2p@1) integer_zerop)
3386 integer_pow2p@2 integer_zerop)
3388 int shift = (wi::exact_log2 (wi::to_wide (@2))
3389 - wi::exact_log2 (wi::to_wide (@1)));
3393 (lshift (convert @0) { build_int_cst (integer_type_node, shift); }) @2)
3395 (convert (rshift @0 { build_int_cst (integer_type_node, -shift); })) @2))))
3397 /* If we have (A & C) != 0 where C is the sign bit of A, convert
3398 this into A < 0. Similarly for (A & C) == 0 into A >= 0. */
3402 (cmp (bit_and (convert?@2 @0) integer_pow2p@1) integer_zerop)
3403 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
3404 && type_has_mode_precision_p (TREE_TYPE (@0))
3405 && element_precision (@2) >= element_precision (@0)
3406 && wi::only_sign_bit_p (wi::to_wide (@1), element_precision (@0)))
3407 (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
3408 (ncmp (convert:stype @0) { build_zero_cst (stype); })))))
3410 /* If we have A < 0 ? C : 0 where C is a power of 2, convert
3411 this into a right shift or sign extension followed by ANDing with C. */
3414 (lt @0 integer_zerop)
3415 integer_pow2p@1 integer_zerop)
3416 (if (!TYPE_UNSIGNED (TREE_TYPE (@0)))
3418 int shift = element_precision (@0) - wi::exact_log2 (wi::to_wide (@1)) - 1;
3422 (convert (rshift @0 { build_int_cst (integer_type_node, shift); }))
3424 /* Otherwise ctype must be wider than TREE_TYPE (@0) and pure
3425 sign extension followed by AND with C will achieve the effect. */
3426 (bit_and (convert @0) @1)))))
3428 /* When the addresses are not directly of decls compare base and offset.
3429 This implements some remaining parts of fold_comparison address
3430 comparisons but still no complete part of it. Still it is good
3431 enough to make fold_stmt not regress when not dispatching to fold_binary. */
3432 (for cmp (simple_comparison)
3434 (cmp (convert1?@2 addr@0) (convert2? addr@1))
3437 HOST_WIDE_INT off0, off1;
3438 tree base0 = get_addr_base_and_unit_offset (TREE_OPERAND (@0, 0), &off0);
3439 tree base1 = get_addr_base_and_unit_offset (TREE_OPERAND (@1, 0), &off1);
3440 if (base0 && TREE_CODE (base0) == MEM_REF)
3442 off0 += mem_ref_offset (base0).to_short_addr ();
3443 base0 = TREE_OPERAND (base0, 0);
3445 if (base1 && TREE_CODE (base1) == MEM_REF)
3447 off1 += mem_ref_offset (base1).to_short_addr ();
3448 base1 = TREE_OPERAND (base1, 0);
3451 (if (base0 && base1)
3455 /* Punt in GENERIC on variables with value expressions;
3456 the value expressions might point to fields/elements
3457 of other vars etc. */
3459 && ((VAR_P (base0) && DECL_HAS_VALUE_EXPR_P (base0))
3460 || (VAR_P (base1) && DECL_HAS_VALUE_EXPR_P (base1))))
3462 else if (decl_in_symtab_p (base0)
3463 && decl_in_symtab_p (base1))
3464 equal = symtab_node::get_create (base0)
3465 ->equal_address_to (symtab_node::get_create (base1));
3466 else if ((DECL_P (base0)
3467 || TREE_CODE (base0) == SSA_NAME
3468 || TREE_CODE (base0) == STRING_CST)
3470 || TREE_CODE (base1) == SSA_NAME
3471 || TREE_CODE (base1) == STRING_CST))
3472 equal = (base0 == base1);
3476 (if (cmp == EQ_EXPR)
3477 { constant_boolean_node (off0 == off1, type); })
3478 (if (cmp == NE_EXPR)
3479 { constant_boolean_node (off0 != off1, type); })
3480 (if (cmp == LT_EXPR)
3481 { constant_boolean_node (off0 < off1, type); })
3482 (if (cmp == LE_EXPR)
3483 { constant_boolean_node (off0 <= off1, type); })
3484 (if (cmp == GE_EXPR)
3485 { constant_boolean_node (off0 >= off1, type); })
3486 (if (cmp == GT_EXPR)
3487 { constant_boolean_node (off0 > off1, type); }))
3489 && DECL_P (base0) && DECL_P (base1)
3490 /* If we compare this as integers require equal offset. */
3491 && (!INTEGRAL_TYPE_P (TREE_TYPE (@2))
3494 (if (cmp == EQ_EXPR)
3495 { constant_boolean_node (false, type); })
3496 (if (cmp == NE_EXPR)
3497 { constant_boolean_node (true, type); })))))))))
3499 /* Simplify pointer equality compares using PTA. */
3503 (if (POINTER_TYPE_P (TREE_TYPE (@0))
3504 && ptrs_compare_unequal (@0, @1))
3505 { neeq == EQ_EXPR ? boolean_false_node : boolean_true_node; })))
3507 /* PR70920: Transform (intptr_t)x eq/ne CST to x eq/ne (typeof x) CST.
3508 and (typeof ptr_cst) x eq/ne ptr_cst to x eq/ne (typeof x) CST.
3509 Disable the transform if either operand is pointer to function.
3510 This broke pr22051-2.c for arm where function pointer
3511 canonicalizaion is not wanted. */
3515 (cmp (convert @0) INTEGER_CST@1)
3516 (if ((POINTER_TYPE_P (TREE_TYPE (@0)) && !FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@0)))
3517 && INTEGRAL_TYPE_P (TREE_TYPE (@1)))
3518 || (INTEGRAL_TYPE_P (TREE_TYPE (@0)) && POINTER_TYPE_P (TREE_TYPE (@1))
3519 && !FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@1)))))
3520 (cmp @0 (convert @1)))))
3522 /* Non-equality compare simplifications from fold_binary */
3523 (for cmp (lt gt le ge)
3524 /* Comparisons with the highest or lowest possible integer of
3525 the specified precision will have known values. */
3527 (cmp (convert?@2 @0) INTEGER_CST@1)
3528 (if ((INTEGRAL_TYPE_P (TREE_TYPE (@1)) || POINTER_TYPE_P (TREE_TYPE (@1)))
3529 && tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@0)))
3532 tree arg1_type = TREE_TYPE (@1);
3533 unsigned int prec = TYPE_PRECISION (arg1_type);
3534 wide_int max = wi::max_value (arg1_type);
3535 wide_int signed_max = wi::max_value (prec, SIGNED);
3536 wide_int min = wi::min_value (arg1_type);
3539 (if (wi::to_wide (@1) == max)
3541 (if (cmp == GT_EXPR)
3542 { constant_boolean_node (false, type); })
3543 (if (cmp == GE_EXPR)
3545 (if (cmp == LE_EXPR)
3546 { constant_boolean_node (true, type); })
3547 (if (cmp == LT_EXPR)
3549 (if (wi::to_wide (@1) == min)
3551 (if (cmp == LT_EXPR)
3552 { constant_boolean_node (false, type); })
3553 (if (cmp == LE_EXPR)
3555 (if (cmp == GE_EXPR)
3556 { constant_boolean_node (true, type); })
3557 (if (cmp == GT_EXPR)
3559 (if (wi::to_wide (@1) == max - 1)
3561 (if (cmp == GT_EXPR)
3562 (eq @2 { wide_int_to_tree (TREE_TYPE (@1), wi::to_wide (@1) + 1); }))
3563 (if (cmp == LE_EXPR)
3564 (ne @2 { wide_int_to_tree (TREE_TYPE (@1), wi::to_wide (@1) + 1); }))))
3565 (if (wi::to_wide (@1) == min + 1)
3567 (if (cmp == GE_EXPR)
3568 (ne @2 { wide_int_to_tree (TREE_TYPE (@1), wi::to_wide (@1) - 1); }))
3569 (if (cmp == LT_EXPR)
3570 (eq @2 { wide_int_to_tree (TREE_TYPE (@1), wi::to_wide (@1) - 1); }))))
3571 (if (wi::to_wide (@1) == signed_max
3572 && TYPE_UNSIGNED (arg1_type)
3573 /* We will flip the signedness of the comparison operator
3574 associated with the mode of @1, so the sign bit is
3575 specified by this mode. Check that @1 is the signed
3576 max associated with this sign bit. */
3577 && prec == GET_MODE_PRECISION (SCALAR_INT_TYPE_MODE (arg1_type))
3578 /* signed_type does not work on pointer types. */
3579 && INTEGRAL_TYPE_P (arg1_type))
3580 /* The following case also applies to X < signed_max+1
3581 and X >= signed_max+1 because previous transformations. */
3582 (if (cmp == LE_EXPR || cmp == GT_EXPR)
3583 (with { tree st = signed_type_for (arg1_type); }
3584 (if (cmp == LE_EXPR)
3585 (ge (convert:st @0) { build_zero_cst (st); })
3586 (lt (convert:st @0) { build_zero_cst (st); }))))))))))
3588 (for cmp (unordered ordered unlt unle ungt unge uneq ltgt)
3589 /* If the second operand is NaN, the result is constant. */
3592 (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
3593 && (cmp != LTGT_EXPR || ! flag_trapping_math))
3594 { constant_boolean_node (cmp == ORDERED_EXPR || cmp == LTGT_EXPR
3595 ? false : true, type); })))
3597 /* bool_var != 0 becomes bool_var. */
3599 (ne @0 integer_zerop)
3600 (if (TREE_CODE (TREE_TYPE (@0)) == BOOLEAN_TYPE
3601 && types_match (type, TREE_TYPE (@0)))
3603 /* bool_var == 1 becomes bool_var. */
3605 (eq @0 integer_onep)
3606 (if (TREE_CODE (TREE_TYPE (@0)) == BOOLEAN_TYPE
3607 && types_match (type, TREE_TYPE (@0)))
3610 bool_var == 0 becomes !bool_var or
3611 bool_var != 1 becomes !bool_var
3612 here because that only is good in assignment context as long
3613 as we require a tcc_comparison in GIMPLE_CONDs where we'd
3614 replace if (x == 0) with tem = ~x; if (tem != 0) which is
3615 clearly less optimal and which we'll transform again in forwprop. */
3617 /* When one argument is a constant, overflow detection can be simplified.
3618 Currently restricted to single use so as not to interfere too much with
3619 ADD_OVERFLOW detection in tree-ssa-math-opts.c.
3620 A + CST CMP A -> A CMP' CST' */
3621 (for cmp (lt le ge gt)
3624 (cmp:c (plus@2 @0 INTEGER_CST@1) @0)
3625 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
3626 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))
3627 && wi::to_wide (@1) != 0
3629 (with { unsigned int prec = TYPE_PRECISION (TREE_TYPE (@0)); }
3630 (out @0 { wide_int_to_tree (TREE_TYPE (@0),
3631 wi::max_value (prec, UNSIGNED)
3632 - wi::to_wide (@1)); })))))
3634 /* To detect overflow in unsigned A - B, A < B is simpler than A - B > A.
3635 However, the detection logic for SUB_OVERFLOW in tree-ssa-math-opts.c
3636 expects the long form, so we restrict the transformation for now. */
3639 (cmp:c (minus@2 @0 @1) @0)
3640 (if (single_use (@2)
3641 && ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3642 && TYPE_UNSIGNED (TREE_TYPE (@0))
3643 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
3646 /* Testing for overflow is unnecessary if we already know the result. */
3651 (cmp:c (realpart (IFN_SUB_OVERFLOW@2 @0 @1)) @0)
3652 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
3653 && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
3654 (out (imagpart @2) { build_zero_cst (TREE_TYPE (@0)); }))))
3659 (cmp:c (realpart (IFN_ADD_OVERFLOW:c@2 @0 @1)) @0)
3660 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
3661 && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
3662 (out (imagpart @2) { build_zero_cst (TREE_TYPE (@0)); }))))
3664 /* For unsigned operands, -1 / B < A checks whether A * B would overflow.
3665 Simplify it to __builtin_mul_overflow (A, B, <unused>). */
3669 (cmp:c (trunc_div:s integer_all_onesp @1) @0)
3670 (if (TYPE_UNSIGNED (TREE_TYPE (@0)) && !VECTOR_TYPE_P (TREE_TYPE (@0)))
3671 (with { tree t = TREE_TYPE (@0), cpx = build_complex_type (t); }
3672 (out (imagpart (IFN_MUL_OVERFLOW:cpx @0 @1)) { build_zero_cst (t); })))))
3674 /* Simplification of math builtins. These rules must all be optimizations
3675 as well as IL simplifications. If there is a possibility that the new
3676 form could be a pessimization, the rule should go in the canonicalization
3677 section that follows this one.
3679 Rules can generally go in this section if they satisfy one of
3682 - the rule describes an identity
3684 - the rule replaces calls with something as simple as addition or
3687 - the rule contains unary calls only and simplifies the surrounding
3688 arithmetic. (The idea here is to exclude non-unary calls in which
3689 one operand is constant and in which the call is known to be cheap
3690 when the operand has that value.) */
3692 (if (flag_unsafe_math_optimizations)
3693 /* Simplify sqrt(x) * sqrt(x) -> x. */
3695 (mult (SQRT@1 @0) @1)
3696 (if (!HONOR_SNANS (type))
3699 (for op (plus minus)
3700 /* Simplify (A / C) +- (B / C) -> (A +- B) / C. */
3704 (rdiv (op @0 @2) @1)))
3706 /* Simplify sqrt(x) * sqrt(y) -> sqrt(x*y). */
3707 (for root (SQRT CBRT)
3709 (mult (root:s @0) (root:s @1))
3710 (root (mult @0 @1))))
3712 /* Simplify expN(x) * expN(y) -> expN(x+y). */
3713 (for exps (EXP EXP2 EXP10 POW10)
3715 (mult (exps:s @0) (exps:s @1))
3716 (exps (plus @0 @1))))
3718 /* Simplify a/root(b/c) into a*root(c/b). */
3719 (for root (SQRT CBRT)
3721 (rdiv @0 (root:s (rdiv:s @1 @2)))
3722 (mult @0 (root (rdiv @2 @1)))))
3724 /* Simplify x/expN(y) into x*expN(-y). */
3725 (for exps (EXP EXP2 EXP10 POW10)
3727 (rdiv @0 (exps:s @1))
3728 (mult @0 (exps (negate @1)))))
3730 (for logs (LOG LOG2 LOG10 LOG10)
3731 exps (EXP EXP2 EXP10 POW10)
3732 /* logN(expN(x)) -> x. */
3736 /* expN(logN(x)) -> x. */
3741 /* Optimize logN(func()) for various exponential functions. We
3742 want to determine the value "x" and the power "exponent" in
3743 order to transform logN(x**exponent) into exponent*logN(x). */
3744 (for logs (LOG LOG LOG LOG2 LOG2 LOG2 LOG10 LOG10)
3745 exps (EXP2 EXP10 POW10 EXP EXP10 POW10 EXP EXP2)
3748 (if (SCALAR_FLOAT_TYPE_P (type))
3754 /* Prepare to do logN(exp(exponent)) -> exponent*logN(e). */
3755 x = build_real_truncate (type, dconst_e ());
3758 /* Prepare to do logN(exp2(exponent)) -> exponent*logN(2). */
3759 x = build_real (type, dconst2);
3763 /* Prepare to do logN(exp10(exponent)) -> exponent*logN(10). */
3765 REAL_VALUE_TYPE dconst10;
3766 real_from_integer (&dconst10, VOIDmode, 10, SIGNED);
3767 x = build_real (type, dconst10);
3774 (mult (logs { x; }) @0)))))
3782 (if (SCALAR_FLOAT_TYPE_P (type))
3788 /* Prepare to do logN(sqrt(x)) -> 0.5*logN(x). */
3789 x = build_real (type, dconsthalf);
3792 /* Prepare to do logN(cbrt(x)) -> (1/3)*logN(x). */
3793 x = build_real_truncate (type, dconst_third ());
3799 (mult { x; } (logs @0))))))
3801 /* logN(pow(x,exponent)) -> exponent*logN(x). */
3802 (for logs (LOG LOG2 LOG10)
3806 (mult @1 (logs @0))))
3808 /* pow(C,x) -> exp(log(C)*x) if C > 0. */
3813 (pows REAL_CST@0 @1)
3814 (if (real_compare (GT_EXPR, TREE_REAL_CST_PTR (@0), &dconst0)
3815 && real_isfinite (TREE_REAL_CST_PTR (@0)))
3816 (exps (mult (logs @0) @1)))))
3821 exps (EXP EXP2 EXP10 POW10)
3822 /* sqrt(expN(x)) -> expN(x*0.5). */
3825 (exps (mult @0 { build_real (type, dconsthalf); })))
3826 /* cbrt(expN(x)) -> expN(x/3). */
3829 (exps (mult @0 { build_real_truncate (type, dconst_third ()); })))
3830 /* pow(expN(x), y) -> expN(x*y). */
3833 (exps (mult @0 @1))))
3835 /* tan(atan(x)) -> x. */
3842 /* cabs(x+0i) or cabs(0+xi) -> abs(x). */
3844 (CABS (complex:C @0 real_zerop@1))
3847 /* trunc(trunc(x)) -> trunc(x), etc. */
3848 (for fns (TRUNC FLOOR CEIL ROUND NEARBYINT RINT)
3852 /* f(x) -> x if x is integer valued and f does nothing for such values. */
3853 (for fns (TRUNC FLOOR CEIL ROUND NEARBYINT RINT)
3855 (fns integer_valued_real_p@0)
3858 /* hypot(x,0) and hypot(0,x) -> abs(x). */
3860 (HYPOT:c @0 real_zerop@1)
3863 /* pow(1,x) -> 1. */
3865 (POW real_onep@0 @1)
3869 /* copysign(x,x) -> x. */
3874 /* copysign(x,y) -> fabs(x) if y is nonnegative. */
3875 (COPYSIGN @0 tree_expr_nonnegative_p@1)
3878 (for scale (LDEXP SCALBN SCALBLN)
3879 /* ldexp(0, x) -> 0. */
3881 (scale real_zerop@0 @1)
3883 /* ldexp(x, 0) -> x. */
3885 (scale @0 integer_zerop@1)
3887 /* ldexp(x, y) -> x if x is +-Inf or NaN. */
3889 (scale REAL_CST@0 @1)
3890 (if (!real_isfinite (TREE_REAL_CST_PTR (@0)))
3893 /* Canonicalization of sequences of math builtins. These rules represent
3894 IL simplifications but are not necessarily optimizations.
3896 The sincos pass is responsible for picking "optimal" implementations
3897 of math builtins, which may be more complicated and can sometimes go
3898 the other way, e.g. converting pow into a sequence of sqrts.
3899 We only want to do these canonicalizations before the pass has run. */
3901 (if (flag_unsafe_math_optimizations && canonicalize_math_p ())
3902 /* Simplify tan(x) * cos(x) -> sin(x). */
3904 (mult:c (TAN:s @0) (COS:s @0))
3907 /* Simplify x * pow(x,c) -> pow(x,c+1). */
3909 (mult:c @0 (POW:s @0 REAL_CST@1))
3910 (if (!TREE_OVERFLOW (@1))
3911 (POW @0 (plus @1 { build_one_cst (type); }))))
3913 /* Simplify sin(x) / cos(x) -> tan(x). */
3915 (rdiv (SIN:s @0) (COS:s @0))
3918 /* Simplify cos(x) / sin(x) -> 1 / tan(x). */
3920 (rdiv (COS:s @0) (SIN:s @0))
3921 (rdiv { build_one_cst (type); } (TAN @0)))
3923 /* Simplify sin(x) / tan(x) -> cos(x). */
3925 (rdiv (SIN:s @0) (TAN:s @0))
3926 (if (! HONOR_NANS (@0)
3927 && ! HONOR_INFINITIES (@0))
3930 /* Simplify tan(x) / sin(x) -> 1.0 / cos(x). */
3932 (rdiv (TAN:s @0) (SIN:s @0))
3933 (if (! HONOR_NANS (@0)
3934 && ! HONOR_INFINITIES (@0))
3935 (rdiv { build_one_cst (type); } (COS @0))))
3937 /* Simplify pow(x,y) * pow(x,z) -> pow(x,y+z). */
3939 (mult (POW:s @0 @1) (POW:s @0 @2))
3940 (POW @0 (plus @1 @2)))
3942 /* Simplify pow(x,y) * pow(z,y) -> pow(x*z,y). */
3944 (mult (POW:s @0 @1) (POW:s @2 @1))
3945 (POW (mult @0 @2) @1))
3947 /* Simplify powi(x,y) * powi(z,y) -> powi(x*z,y). */
3949 (mult (POWI:s @0 @1) (POWI:s @2 @1))
3950 (POWI (mult @0 @2) @1))
3952 /* Simplify pow(x,c) / x -> pow(x,c-1). */
3954 (rdiv (POW:s @0 REAL_CST@1) @0)
3955 (if (!TREE_OVERFLOW (@1))
3956 (POW @0 (minus @1 { build_one_cst (type); }))))
3958 /* Simplify x / pow (y,z) -> x * pow(y,-z). */
3960 (rdiv @0 (POW:s @1 @2))
3961 (mult @0 (POW @1 (negate @2))))
3966 /* sqrt(sqrt(x)) -> pow(x,1/4). */
3969 (pows @0 { build_real (type, dconst_quarter ()); }))
3970 /* sqrt(cbrt(x)) -> pow(x,1/6). */
3973 (pows @0 { build_real_truncate (type, dconst_sixth ()); }))
3974 /* cbrt(sqrt(x)) -> pow(x,1/6). */
3977 (pows @0 { build_real_truncate (type, dconst_sixth ()); }))
3978 /* cbrt(cbrt(x)) -> pow(x,1/9), iff x is nonnegative. */
3980 (cbrts (cbrts tree_expr_nonnegative_p@0))
3981 (pows @0 { build_real_truncate (type, dconst_ninth ()); }))
3982 /* sqrt(pow(x,y)) -> pow(|x|,y*0.5). */
3984 (sqrts (pows @0 @1))
3985 (pows (abs @0) (mult @1 { build_real (type, dconsthalf); })))
3986 /* cbrt(pow(x,y)) -> pow(x,y/3), iff x is nonnegative. */
3988 (cbrts (pows tree_expr_nonnegative_p@0 @1))
3989 (pows @0 (mult @1 { build_real_truncate (type, dconst_third ()); })))
3990 /* pow(sqrt(x),y) -> pow(x,y*0.5). */
3992 (pows (sqrts @0) @1)
3993 (pows @0 (mult @1 { build_real (type, dconsthalf); })))
3994 /* pow(cbrt(x),y) -> pow(x,y/3) iff x is nonnegative. */
3996 (pows (cbrts tree_expr_nonnegative_p@0) @1)
3997 (pows @0 (mult @1 { build_real_truncate (type, dconst_third ()); })))
3998 /* pow(pow(x,y),z) -> pow(x,y*z) iff x is nonnegative. */
4000 (pows (pows tree_expr_nonnegative_p@0 @1) @2)
4001 (pows @0 (mult @1 @2))))
4003 /* cabs(x+xi) -> fabs(x)*sqrt(2). */
4005 (CABS (complex @0 @0))
4006 (mult (abs @0) { build_real_truncate (type, dconst_sqrt2 ()); }))
4008 /* hypot(x,x) -> fabs(x)*sqrt(2). */
4011 (mult (abs @0) { build_real_truncate (type, dconst_sqrt2 ()); }))
4013 /* cexp(x+yi) -> exp(x)*cexpi(y). */
4018 (cexps compositional_complex@0)
4019 (if (targetm.libc_has_function (function_c99_math_complex))
4021 (mult (exps@1 (realpart @0)) (realpart (cexpis:type@2 (imagpart @0))))
4022 (mult @1 (imagpart @2)))))))
4024 (if (canonicalize_math_p ())
4025 /* floor(x) -> trunc(x) if x is nonnegative. */
4029 (floors tree_expr_nonnegative_p@0)
4032 (match double_value_p
4034 (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == double_type_node)))
4035 (for froms (BUILT_IN_TRUNCL
4047 /* truncl(extend(x)) -> extend(trunc(x)), etc., if x is a double. */
4048 (if (optimize && canonicalize_math_p ())
4050 (froms (convert double_value_p@0))
4051 (convert (tos @0)))))
4053 (match float_value_p
4055 (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == float_type_node)))
4056 (for froms (BUILT_IN_TRUNCL BUILT_IN_TRUNC
4057 BUILT_IN_FLOORL BUILT_IN_FLOOR
4058 BUILT_IN_CEILL BUILT_IN_CEIL
4059 BUILT_IN_ROUNDL BUILT_IN_ROUND
4060 BUILT_IN_NEARBYINTL BUILT_IN_NEARBYINT
4061 BUILT_IN_RINTL BUILT_IN_RINT)
4062 tos (BUILT_IN_TRUNCF BUILT_IN_TRUNCF
4063 BUILT_IN_FLOORF BUILT_IN_FLOORF
4064 BUILT_IN_CEILF BUILT_IN_CEILF
4065 BUILT_IN_ROUNDF BUILT_IN_ROUNDF
4066 BUILT_IN_NEARBYINTF BUILT_IN_NEARBYINTF
4067 BUILT_IN_RINTF BUILT_IN_RINTF)
4068 /* truncl(extend(x)) and trunc(extend(x)) -> extend(truncf(x)), etc.,
4070 (if (optimize && canonicalize_math_p ()
4071 && targetm.libc_has_function (function_c99_misc))
4073 (froms (convert float_value_p@0))
4074 (convert (tos @0)))))
4076 (for froms (XFLOORL XCEILL XROUNDL XRINTL)
4077 tos (XFLOOR XCEIL XROUND XRINT)
4078 /* llfloorl(extend(x)) -> llfloor(x), etc., if x is a double. */
4079 (if (optimize && canonicalize_math_p ())
4081 (froms (convert double_value_p@0))
4084 (for froms (XFLOORL XCEILL XROUNDL XRINTL
4085 XFLOOR XCEIL XROUND XRINT)
4086 tos (XFLOORF XCEILF XROUNDF XRINTF)
4087 /* llfloorl(extend(x)) and llfloor(extend(x)) -> llfloorf(x), etc.,
4089 (if (optimize && canonicalize_math_p ())
4091 (froms (convert float_value_p@0))
4094 (if (canonicalize_math_p ())
4095 /* xfloor(x) -> fix_trunc(x) if x is nonnegative. */
4096 (for floors (IFLOOR LFLOOR LLFLOOR)
4098 (floors tree_expr_nonnegative_p@0)
4101 (if (canonicalize_math_p ())
4102 /* xfloor(x) -> fix_trunc(x), etc., if x is integer valued. */
4103 (for fns (IFLOOR LFLOOR LLFLOOR
4105 IROUND LROUND LLROUND)
4107 (fns integer_valued_real_p@0)
4109 (if (!flag_errno_math)
4110 /* xrint(x) -> fix_trunc(x), etc., if x is integer valued. */
4111 (for rints (IRINT LRINT LLRINT)
4113 (rints integer_valued_real_p@0)
4116 (if (canonicalize_math_p ())
4117 (for ifn (IFLOOR ICEIL IROUND IRINT)
4118 lfn (LFLOOR LCEIL LROUND LRINT)
4119 llfn (LLFLOOR LLCEIL LLROUND LLRINT)
4120 /* Canonicalize iround (x) to lround (x) on ILP32 targets where
4121 sizeof (int) == sizeof (long). */
4122 (if (TYPE_PRECISION (integer_type_node)
4123 == TYPE_PRECISION (long_integer_type_node))
4126 (lfn:long_integer_type_node @0)))
4127 /* Canonicalize llround (x) to lround (x) on LP64 targets where
4128 sizeof (long long) == sizeof (long). */
4129 (if (TYPE_PRECISION (long_long_integer_type_node)
4130 == TYPE_PRECISION (long_integer_type_node))
4133 (lfn:long_integer_type_node @0)))))
4135 /* cproj(x) -> x if we're ignoring infinities. */
4138 (if (!HONOR_INFINITIES (type))
4141 /* If the real part is inf and the imag part is known to be
4142 nonnegative, return (inf + 0i). */
4144 (CPROJ (complex REAL_CST@0 tree_expr_nonnegative_p@1))
4145 (if (real_isinf (TREE_REAL_CST_PTR (@0)))
4146 { build_complex_inf (type, false); }))
4148 /* If the imag part is inf, return (inf+I*copysign(0,imag)). */
4150 (CPROJ (complex @0 REAL_CST@1))
4151 (if (real_isinf (TREE_REAL_CST_PTR (@1)))
4152 { build_complex_inf (type, TREE_REAL_CST_PTR (@1)->sign); }))
4158 (pows @0 REAL_CST@1)
4160 const REAL_VALUE_TYPE *value = TREE_REAL_CST_PTR (@1);
4161 REAL_VALUE_TYPE tmp;
4164 /* pow(x,0) -> 1. */
4165 (if (real_equal (value, &dconst0))
4166 { build_real (type, dconst1); })
4167 /* pow(x,1) -> x. */
4168 (if (real_equal (value, &dconst1))
4170 /* pow(x,-1) -> 1/x. */
4171 (if (real_equal (value, &dconstm1))
4172 (rdiv { build_real (type, dconst1); } @0))
4173 /* pow(x,0.5) -> sqrt(x). */
4174 (if (flag_unsafe_math_optimizations
4175 && canonicalize_math_p ()
4176 && real_equal (value, &dconsthalf))
4178 /* pow(x,1/3) -> cbrt(x). */
4179 (if (flag_unsafe_math_optimizations
4180 && canonicalize_math_p ()
4181 && (tmp = real_value_truncate (TYPE_MODE (type), dconst_third ()),
4182 real_equal (value, &tmp)))
4185 /* powi(1,x) -> 1. */
4187 (POWI real_onep@0 @1)
4191 (POWI @0 INTEGER_CST@1)
4193 /* powi(x,0) -> 1. */
4194 (if (wi::to_wide (@1) == 0)
4195 { build_real (type, dconst1); })
4196 /* powi(x,1) -> x. */
4197 (if (wi::to_wide (@1) == 1)
4199 /* powi(x,-1) -> 1/x. */
4200 (if (wi::to_wide (@1) == -1)
4201 (rdiv { build_real (type, dconst1); } @0))))
4203 /* Narrowing of arithmetic and logical operations.
4205 These are conceptually similar to the transformations performed for
4206 the C/C++ front-ends by shorten_binary_op and shorten_compare. Long
4207 term we want to move all that code out of the front-ends into here. */
4209 /* If we have a narrowing conversion of an arithmetic operation where
4210 both operands are widening conversions from the same type as the outer
4211 narrowing conversion. Then convert the innermost operands to a suitable
4212 unsigned type (to avoid introducing undefined behavior), perform the
4213 operation and convert the result to the desired type. */
4214 (for op (plus minus)
4216 (convert (op:s (convert@2 @0) (convert?@3 @1)))
4217 (if (INTEGRAL_TYPE_P (type)
4218 /* We check for type compatibility between @0 and @1 below,
4219 so there's no need to check that @1/@3 are integral types. */
4220 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
4221 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
4222 /* The precision of the type of each operand must match the
4223 precision of the mode of each operand, similarly for the
4225 && type_has_mode_precision_p (TREE_TYPE (@0))
4226 && type_has_mode_precision_p (TREE_TYPE (@1))
4227 && type_has_mode_precision_p (type)
4228 /* The inner conversion must be a widening conversion. */
4229 && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
4230 && types_match (@0, type)
4231 && (types_match (@0, @1)
4232 /* Or the second operand is const integer or converted const
4233 integer from valueize. */
4234 || TREE_CODE (@1) == INTEGER_CST))
4235 (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
4236 (op @0 (convert @1))
4237 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
4238 (convert (op (convert:utype @0)
4239 (convert:utype @1))))))))
4241 /* This is another case of narrowing, specifically when there's an outer
4242 BIT_AND_EXPR which masks off bits outside the type of the innermost
4243 operands. Like the previous case we have to convert the operands
4244 to unsigned types to avoid introducing undefined behavior for the
4245 arithmetic operation. */
4246 (for op (minus plus)
4248 (bit_and (op:s (convert@2 @0) (convert@3 @1)) INTEGER_CST@4)
4249 (if (INTEGRAL_TYPE_P (type)
4250 /* We check for type compatibility between @0 and @1 below,
4251 so there's no need to check that @1/@3 are integral types. */
4252 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
4253 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
4254 /* The precision of the type of each operand must match the
4255 precision of the mode of each operand, similarly for the
4257 && type_has_mode_precision_p (TREE_TYPE (@0))
4258 && type_has_mode_precision_p (TREE_TYPE (@1))
4259 && type_has_mode_precision_p (type)
4260 /* The inner conversion must be a widening conversion. */
4261 && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
4262 && types_match (@0, @1)
4263 && (tree_int_cst_min_precision (@4, TYPE_SIGN (TREE_TYPE (@0)))
4264 <= TYPE_PRECISION (TREE_TYPE (@0)))
4265 && (wi::to_wide (@4)
4266 & wi::mask (TYPE_PRECISION (TREE_TYPE (@0)),
4267 true, TYPE_PRECISION (type))) == 0)
4268 (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
4269 (with { tree ntype = TREE_TYPE (@0); }
4270 (convert (bit_and (op @0 @1) (convert:ntype @4))))
4271 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
4272 (convert (bit_and (op (convert:utype @0) (convert:utype @1))
4273 (convert:utype @4))))))))
4275 /* Transform (@0 < @1 and @0 < @2) to use min,
4276 (@0 > @1 and @0 > @2) to use max */
4277 (for op (lt le gt ge)
4278 ext (min min max max)
4280 (bit_and (op:cs @0 @1) (op:cs @0 @2))
4281 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
4282 && TREE_CODE (@0) != INTEGER_CST)
4283 (op @0 (ext @1 @2)))))
4286 /* signbit(x) -> 0 if x is nonnegative. */
4287 (SIGNBIT tree_expr_nonnegative_p@0)
4288 { integer_zero_node; })
4291 /* signbit(x) -> x<0 if x doesn't have signed zeros. */
4293 (if (!HONOR_SIGNED_ZEROS (@0))
4294 (convert (lt @0 { build_real (TREE_TYPE (@0), dconst0); }))))
4296 /* Transform comparisons of the form X +- C1 CMP C2 to X CMP C2 -+ C1. */
4298 (for op (plus minus)
4301 (cmp (op@3 @0 INTEGER_CST@1) INTEGER_CST@2)
4302 (if (!TREE_OVERFLOW (@1) && !TREE_OVERFLOW (@2)
4303 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0))
4304 && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@0))
4305 && !TYPE_SATURATING (TREE_TYPE (@0)))
4306 (with { tree res = int_const_binop (rop, @2, @1); }
4307 (if (TREE_OVERFLOW (res)
4308 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
4309 { constant_boolean_node (cmp == NE_EXPR, type); }
4310 (if (single_use (@3))
4311 (cmp @0 { res; }))))))))
4312 (for cmp (lt le gt ge)
4313 (for op (plus minus)
4316 (cmp (op@3 @0 INTEGER_CST@1) INTEGER_CST@2)
4317 (if (!TREE_OVERFLOW (@1) && !TREE_OVERFLOW (@2)
4318 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
4319 (with { tree res = int_const_binop (rop, @2, @1); }
4320 (if (TREE_OVERFLOW (res))
4322 fold_overflow_warning (("assuming signed overflow does not occur "
4323 "when simplifying conditional to constant"),
4324 WARN_STRICT_OVERFLOW_CONDITIONAL);
4325 bool less = cmp == LE_EXPR || cmp == LT_EXPR;
4326 /* wi::ges_p (@2, 0) should be sufficient for a signed type. */
4327 bool ovf_high = wi::lt_p (wi::to_wide (@1), 0,
4328 TYPE_SIGN (TREE_TYPE (@1)))
4329 != (op == MINUS_EXPR);
4330 constant_boolean_node (less == ovf_high, type);
4332 (if (single_use (@3))
4335 fold_overflow_warning (("assuming signed overflow does not occur "
4336 "when changing X +- C1 cmp C2 to "
4338 WARN_STRICT_OVERFLOW_COMPARISON);
4340 (cmp @0 { res; })))))))))
4342 /* Canonicalizations of BIT_FIELD_REFs. */
4345 (BIT_FIELD_REF @0 @1 @2)
4347 (if (TREE_CODE (TREE_TYPE (@0)) == COMPLEX_TYPE
4348 && tree_int_cst_equal (@1, TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))))
4350 (if (integer_zerop (@2))
4351 (view_convert (realpart @0)))
4352 (if (tree_int_cst_equal (@2, TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))))
4353 (view_convert (imagpart @0)))))
4354 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
4355 && INTEGRAL_TYPE_P (type)
4356 /* On GIMPLE this should only apply to register arguments. */
4357 && (! GIMPLE || is_gimple_reg (@0))
4358 /* A bit-field-ref that referenced the full argument can be stripped. */
4359 && ((compare_tree_int (@1, TYPE_PRECISION (TREE_TYPE (@0))) == 0
4360 && integer_zerop (@2))
4361 /* Low-parts can be reduced to integral conversions.
4362 ??? The following doesn't work for PDP endian. */
4363 || (BYTES_BIG_ENDIAN == WORDS_BIG_ENDIAN
4364 /* Don't even think about BITS_BIG_ENDIAN. */
4365 && TYPE_PRECISION (TREE_TYPE (@0)) % BITS_PER_UNIT == 0
4366 && TYPE_PRECISION (type) % BITS_PER_UNIT == 0
4367 && compare_tree_int (@2, (BYTES_BIG_ENDIAN
4368 ? (TYPE_PRECISION (TREE_TYPE (@0))
4369 - TYPE_PRECISION (type))
4373 /* Simplify vector extracts. */
4376 (BIT_FIELD_REF CONSTRUCTOR@0 @1 @2)
4377 (if (VECTOR_TYPE_P (TREE_TYPE (@0))
4378 && (types_match (type, TREE_TYPE (TREE_TYPE (@0)))
4379 || (VECTOR_TYPE_P (type)
4380 && types_match (TREE_TYPE (type), TREE_TYPE (TREE_TYPE (@0))))))
4383 tree ctor = (TREE_CODE (@0) == SSA_NAME
4384 ? gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0)) : @0);
4385 tree eltype = TREE_TYPE (TREE_TYPE (ctor));
4386 unsigned HOST_WIDE_INT width = tree_to_uhwi (TYPE_SIZE (eltype));
4387 unsigned HOST_WIDE_INT n = tree_to_uhwi (@1);
4388 unsigned HOST_WIDE_INT idx = tree_to_uhwi (@2);
4391 && (idx % width) == 0
4393 && ((idx + n) / width) <= TYPE_VECTOR_SUBPARTS (TREE_TYPE (ctor)))
4398 /* Constructor elements can be subvectors. */
4399 unsigned HOST_WIDE_INT k = 1;
4400 if (CONSTRUCTOR_NELTS (ctor) != 0)
4402 tree cons_elem = TREE_TYPE (CONSTRUCTOR_ELT (ctor, 0)->value);
4403 if (TREE_CODE (cons_elem) == VECTOR_TYPE)
4404 k = TYPE_VECTOR_SUBPARTS (cons_elem);
4408 /* We keep an exact subset of the constructor elements. */
4409 (if ((idx % k) == 0 && (n % k) == 0)
4410 (if (CONSTRUCTOR_NELTS (ctor) == 0)
4411 { build_constructor (type, NULL); }
4418 (if (idx < CONSTRUCTOR_NELTS (ctor))
4419 { CONSTRUCTOR_ELT (ctor, idx)->value; }
4420 { build_zero_cst (type); })
4422 vec<constructor_elt, va_gc> *vals;
4423 vec_alloc (vals, n);
4424 for (unsigned i = 0;
4425 i < n && idx + i < CONSTRUCTOR_NELTS (ctor); ++i)
4426 CONSTRUCTOR_APPEND_ELT (vals, NULL_TREE,
4427 CONSTRUCTOR_ELT (ctor, idx + i)->value);
4428 build_constructor (type, vals);
4430 /* The bitfield references a single constructor element. */
4431 (if (idx + n <= (idx / k + 1) * k)
4433 (if (CONSTRUCTOR_NELTS (ctor) <= idx / k)
4434 { build_zero_cst (type); })
4436 { CONSTRUCTOR_ELT (ctor, idx / k)->value; })
4437 (BIT_FIELD_REF { CONSTRUCTOR_ELT (ctor, idx / k)->value; }
4438 @1 { bitsize_int ((idx % k) * width); })))))))))
4440 /* Simplify a bit extraction from a bit insertion for the cases with
4441 the inserted element fully covering the extraction or the insertion
4442 not touching the extraction. */
4444 (BIT_FIELD_REF (bit_insert @0 @1 @ipos) @rsize @rpos)
4447 unsigned HOST_WIDE_INT isize;
4448 if (INTEGRAL_TYPE_P (TREE_TYPE (@1)))
4449 isize = TYPE_PRECISION (TREE_TYPE (@1));
4451 isize = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (@1)));
4454 (if (wi::leu_p (wi::to_wide (@ipos), wi::to_wide (@rpos))
4455 && wi::leu_p (wi::to_wide (@rpos) + wi::to_wide (@rsize),
4456 wi::to_wide (@ipos) + isize))
4457 (BIT_FIELD_REF @1 @rsize { wide_int_to_tree (bitsizetype,
4459 - wi::to_wide (@ipos)); }))
4460 (if (wi::geu_p (wi::to_wide (@ipos),
4461 wi::to_wide (@rpos) + wi::to_wide (@rsize))
4462 || wi::geu_p (wi::to_wide (@rpos),
4463 wi::to_wide (@ipos) + isize))
4464 (BIT_FIELD_REF @0 @rsize @rpos)))))