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 FMIN_FN FMAX FMAX_FN)
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 FMIN_FN FMAX FMAX_FN)
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)
1910 /* min (-A, -B) -> -max (A, B) */
1911 (for minmax (min max FMIN FMIN_FN FMAX FMAX_FN)
1912 maxmin (max min FMAX FMAX_FN FMIN FMAX_FN)
1914 (minmax (negate:s@2 @0) (negate:s@3 @1))
1915 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
1916 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1917 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
1918 (negate (maxmin @0 @1)))))
1919 /* MIN (~X, ~Y) -> ~MAX (X, Y)
1920 MAX (~X, ~Y) -> ~MIN (X, Y) */
1921 (for minmax (min max)
1924 (minmax (bit_not:s@2 @0) (bit_not:s@3 @1))
1925 (bit_not (maxmin @0 @1))))
1927 /* MIN (X, Y) == X -> X <= Y */
1928 (for minmax (min min max max)
1932 (cmp:c (minmax:c @0 @1) @0)
1933 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0)))
1935 /* MIN (X, 5) == 0 -> X == 0
1936 MIN (X, 5) == 7 -> false */
1939 (cmp (min @0 INTEGER_CST@1) INTEGER_CST@2)
1940 (if (wi::lt_p (wi::to_wide (@1), wi::to_wide (@2),
1941 TYPE_SIGN (TREE_TYPE (@0))))
1942 { constant_boolean_node (cmp == NE_EXPR, type); }
1943 (if (wi::gt_p (wi::to_wide (@1), wi::to_wide (@2),
1944 TYPE_SIGN (TREE_TYPE (@0))))
1948 (cmp (max @0 INTEGER_CST@1) INTEGER_CST@2)
1949 (if (wi::gt_p (wi::to_wide (@1), wi::to_wide (@2),
1950 TYPE_SIGN (TREE_TYPE (@0))))
1951 { constant_boolean_node (cmp == NE_EXPR, type); }
1952 (if (wi::lt_p (wi::to_wide (@1), wi::to_wide (@2),
1953 TYPE_SIGN (TREE_TYPE (@0))))
1955 /* MIN (X, C1) < C2 -> X < C2 || C1 < C2 */
1956 (for minmax (min min max max min min max max )
1957 cmp (lt le gt ge gt ge lt le )
1958 comb (bit_ior bit_ior bit_ior bit_ior bit_and bit_and bit_and bit_and)
1960 (cmp (minmax @0 INTEGER_CST@1) INTEGER_CST@2)
1961 (comb (cmp @0 @2) (cmp @1 @2))))
1963 /* Simplifications of shift and rotates. */
1965 (for rotate (lrotate rrotate)
1967 (rotate integer_all_onesp@0 @1)
1970 /* Optimize -1 >> x for arithmetic right shifts. */
1972 (rshift integer_all_onesp@0 @1)
1973 (if (!TYPE_UNSIGNED (type)
1974 && tree_expr_nonnegative_p (@1))
1977 /* Optimize (x >> c) << c into x & (-1<<c). */
1979 (lshift (rshift @0 INTEGER_CST@1) @1)
1980 (if (wi::ltu_p (wi::to_wide (@1), element_precision (type)))
1981 (bit_and @0 (lshift { build_minus_one_cst (type); } @1))))
1983 /* Optimize (x << c) >> c into x & ((unsigned)-1 >> c) for unsigned
1986 (rshift (lshift @0 INTEGER_CST@1) @1)
1987 (if (TYPE_UNSIGNED (type)
1988 && (wi::ltu_p (wi::to_wide (@1), element_precision (type))))
1989 (bit_and @0 (rshift { build_minus_one_cst (type); } @1))))
1991 (for shiftrotate (lrotate rrotate lshift rshift)
1993 (shiftrotate @0 integer_zerop)
1996 (shiftrotate integer_zerop@0 @1)
1998 /* Prefer vector1 << scalar to vector1 << vector2
1999 if vector2 is uniform. */
2000 (for vec (VECTOR_CST CONSTRUCTOR)
2002 (shiftrotate @0 vec@1)
2003 (with { tree tem = uniform_vector_p (@1); }
2005 (shiftrotate @0 { tem; }))))))
2007 /* Simplify X << Y where Y's low width bits are 0 to X, as only valid
2008 Y is 0. Similarly for X >> Y. */
2010 (for shift (lshift rshift)
2012 (shift @0 SSA_NAME@1)
2013 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1)))
2015 int width = ceil_log2 (element_precision (TREE_TYPE (@0)));
2016 int prec = TYPE_PRECISION (TREE_TYPE (@1));
2018 (if ((get_nonzero_bits (@1) & wi::mask (width, false, prec)) == 0)
2022 /* Rewrite an LROTATE_EXPR by a constant into an
2023 RROTATE_EXPR by a new constant. */
2025 (lrotate @0 INTEGER_CST@1)
2026 (rrotate @0 { const_binop (MINUS_EXPR, TREE_TYPE (@1),
2027 build_int_cst (TREE_TYPE (@1),
2028 element_precision (type)), @1); }))
2030 /* Turn (a OP c1) OP c2 into a OP (c1+c2). */
2031 (for op (lrotate rrotate rshift lshift)
2033 (op (op @0 INTEGER_CST@1) INTEGER_CST@2)
2034 (with { unsigned int prec = element_precision (type); }
2035 (if (wi::ge_p (wi::to_wide (@1), 0, TYPE_SIGN (TREE_TYPE (@1)))
2036 && wi::lt_p (wi::to_wide (@1), prec, TYPE_SIGN (TREE_TYPE (@1)))
2037 && wi::ge_p (wi::to_wide (@2), 0, TYPE_SIGN (TREE_TYPE (@2)))
2038 && wi::lt_p (wi::to_wide (@2), prec, TYPE_SIGN (TREE_TYPE (@2))))
2039 (with { unsigned int low = (tree_to_uhwi (@1)
2040 + tree_to_uhwi (@2)); }
2041 /* Deal with a OP (c1 + c2) being undefined but (a OP c1) OP c2
2042 being well defined. */
2044 (if (op == LROTATE_EXPR || op == RROTATE_EXPR)
2045 (op @0 { build_int_cst (TREE_TYPE (@1), low % prec); })
2046 (if (TYPE_UNSIGNED (type) || op == LSHIFT_EXPR)
2047 { build_zero_cst (type); }
2048 (op @0 { build_int_cst (TREE_TYPE (@1), prec - 1); })))
2049 (op @0 { build_int_cst (TREE_TYPE (@1), low); })))))))
2052 /* ((1 << A) & 1) != 0 -> A == 0
2053 ((1 << A) & 1) == 0 -> A != 0 */
2057 (cmp (bit_and (lshift integer_onep @0) integer_onep) integer_zerop)
2058 (icmp @0 { build_zero_cst (TREE_TYPE (@0)); })))
2060 /* (CST1 << A) == CST2 -> A == ctz (CST2) - ctz (CST1)
2061 (CST1 << A) != CST2 -> A != ctz (CST2) - ctz (CST1)
2065 (cmp (lshift INTEGER_CST@0 @1) INTEGER_CST@2)
2066 (with { int cand = wi::ctz (wi::to_wide (@2)) - wi::ctz (wi::to_wide (@0)); }
2068 || (!integer_zerop (@2)
2069 && wi::lshift (wi::to_wide (@0), cand) != wi::to_wide (@2)))
2070 { constant_boolean_node (cmp == NE_EXPR, type); }
2071 (if (!integer_zerop (@2)
2072 && wi::lshift (wi::to_wide (@0), cand) == wi::to_wide (@2))
2073 (cmp @1 { build_int_cst (TREE_TYPE (@1), cand); }))))))
2075 /* Fold (X << C1) & C2 into (X << C1) & (C2 | ((1 << C1) - 1))
2076 (X >> C1) & C2 into (X >> C1) & (C2 | ~((type) -1 >> C1))
2077 if the new mask might be further optimized. */
2078 (for shift (lshift rshift)
2080 (bit_and (convert?:s@4 (shift:s@5 (convert1?@3 @0) INTEGER_CST@1))
2082 (if (tree_nop_conversion_p (TREE_TYPE (@4), TREE_TYPE (@5))
2083 && TYPE_PRECISION (type) <= HOST_BITS_PER_WIDE_INT
2084 && tree_fits_uhwi_p (@1)
2085 && tree_to_uhwi (@1) > 0
2086 && tree_to_uhwi (@1) < TYPE_PRECISION (type))
2089 unsigned int shiftc = tree_to_uhwi (@1);
2090 unsigned HOST_WIDE_INT mask = TREE_INT_CST_LOW (@2);
2091 unsigned HOST_WIDE_INT newmask, zerobits = 0;
2092 tree shift_type = TREE_TYPE (@3);
2095 if (shift == LSHIFT_EXPR)
2096 zerobits = ((HOST_WIDE_INT_1U << shiftc) - 1);
2097 else if (shift == RSHIFT_EXPR
2098 && type_has_mode_precision_p (shift_type))
2100 prec = TYPE_PRECISION (TREE_TYPE (@3));
2102 /* See if more bits can be proven as zero because of
2105 && TYPE_UNSIGNED (TREE_TYPE (@0)))
2107 tree inner_type = TREE_TYPE (@0);
2108 if (type_has_mode_precision_p (inner_type)
2109 && TYPE_PRECISION (inner_type) < prec)
2111 prec = TYPE_PRECISION (inner_type);
2112 /* See if we can shorten the right shift. */
2114 shift_type = inner_type;
2115 /* Otherwise X >> C1 is all zeros, so we'll optimize
2116 it into (X, 0) later on by making sure zerobits
2120 zerobits = HOST_WIDE_INT_M1U;
2123 zerobits >>= HOST_BITS_PER_WIDE_INT - shiftc;
2124 zerobits <<= prec - shiftc;
2126 /* For arithmetic shift if sign bit could be set, zerobits
2127 can contain actually sign bits, so no transformation is
2128 possible, unless MASK masks them all away. In that
2129 case the shift needs to be converted into logical shift. */
2130 if (!TYPE_UNSIGNED (TREE_TYPE (@3))
2131 && prec == TYPE_PRECISION (TREE_TYPE (@3)))
2133 if ((mask & zerobits) == 0)
2134 shift_type = unsigned_type_for (TREE_TYPE (@3));
2140 /* ((X << 16) & 0xff00) is (X, 0). */
2141 (if ((mask & zerobits) == mask)
2142 { build_int_cst (type, 0); }
2143 (with { newmask = mask | zerobits; }
2144 (if (newmask != mask && (newmask & (newmask + 1)) == 0)
2147 /* Only do the transformation if NEWMASK is some integer
2149 for (prec = BITS_PER_UNIT;
2150 prec < HOST_BITS_PER_WIDE_INT; prec <<= 1)
2151 if (newmask == (HOST_WIDE_INT_1U << prec) - 1)
2154 (if (prec < HOST_BITS_PER_WIDE_INT
2155 || newmask == HOST_WIDE_INT_M1U)
2157 { tree newmaskt = build_int_cst_type (TREE_TYPE (@2), newmask); }
2158 (if (!tree_int_cst_equal (newmaskt, @2))
2159 (if (shift_type != TREE_TYPE (@3))
2160 (bit_and (convert (shift:shift_type (convert @3) @1)) { newmaskt; })
2161 (bit_and @4 { newmaskt; })))))))))))))
2163 /* Fold (X {&,^,|} C2) << C1 into (X << C1) {&,^,|} (C2 << C1)
2164 (X {&,^,|} C2) >> C1 into (X >> C1) & (C2 >> C1). */
2165 (for shift (lshift rshift)
2166 (for bit_op (bit_and bit_xor bit_ior)
2168 (shift (convert?:s (bit_op:s @0 INTEGER_CST@2)) INTEGER_CST@1)
2169 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2170 (with { tree mask = int_const_binop (shift, fold_convert (type, @2), @1); }
2171 (bit_op (shift (convert @0) @1) { mask; }))))))
2173 /* ~(~X >> Y) -> X >> Y (for arithmetic shift). */
2175 (bit_not (convert1?:s (rshift:s (convert2?@0 (bit_not @1)) @2)))
2176 (if (!TYPE_UNSIGNED (TREE_TYPE (@0))
2177 && (element_precision (TREE_TYPE (@0))
2178 <= element_precision (TREE_TYPE (@1))
2179 || !TYPE_UNSIGNED (TREE_TYPE (@1))))
2181 { tree shift_type = TREE_TYPE (@0); }
2182 (convert (rshift (convert:shift_type @1) @2)))))
2184 /* ~(~X >>r Y) -> X >>r Y
2185 ~(~X <<r Y) -> X <<r Y */
2186 (for rotate (lrotate rrotate)
2188 (bit_not (convert1?:s (rotate:s (convert2?@0 (bit_not @1)) @2)))
2189 (if ((element_precision (TREE_TYPE (@0))
2190 <= element_precision (TREE_TYPE (@1))
2191 || !TYPE_UNSIGNED (TREE_TYPE (@1)))
2192 && (element_precision (type) <= element_precision (TREE_TYPE (@0))
2193 || !TYPE_UNSIGNED (TREE_TYPE (@0))))
2195 { tree rotate_type = TREE_TYPE (@0); }
2196 (convert (rotate (convert:rotate_type @1) @2))))))
2198 /* Simplifications of conversions. */
2200 /* Basic strip-useless-type-conversions / strip_nops. */
2201 (for cvt (convert view_convert float fix_trunc)
2204 (if ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@0)))
2205 || (GENERIC && type == TREE_TYPE (@0)))
2208 /* Contract view-conversions. */
2210 (view_convert (view_convert @0))
2213 /* For integral conversions with the same precision or pointer
2214 conversions use a NOP_EXPR instead. */
2217 (if ((INTEGRAL_TYPE_P (type) || POINTER_TYPE_P (type))
2218 && (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
2219 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (@0)))
2222 /* Strip inner integral conversions that do not change precision or size, or
2223 zero-extend while keeping the same size (for bool-to-char). */
2225 (view_convert (convert@0 @1))
2226 (if ((INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
2227 && (INTEGRAL_TYPE_P (TREE_TYPE (@1)) || POINTER_TYPE_P (TREE_TYPE (@1)))
2228 && TYPE_SIZE (TREE_TYPE (@0)) == TYPE_SIZE (TREE_TYPE (@1))
2229 && (TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1))
2230 || (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (TREE_TYPE (@1))
2231 && TYPE_UNSIGNED (TREE_TYPE (@1)))))
2234 /* Re-association barriers around constants and other re-association
2235 barriers can be removed. */
2237 (paren CONSTANT_CLASS_P@0)
2240 (paren (paren@1 @0))
2243 /* Handle cases of two conversions in a row. */
2244 (for ocvt (convert float fix_trunc)
2245 (for icvt (convert float)
2250 tree inside_type = TREE_TYPE (@0);
2251 tree inter_type = TREE_TYPE (@1);
2252 int inside_int = INTEGRAL_TYPE_P (inside_type);
2253 int inside_ptr = POINTER_TYPE_P (inside_type);
2254 int inside_float = FLOAT_TYPE_P (inside_type);
2255 int inside_vec = VECTOR_TYPE_P (inside_type);
2256 unsigned int inside_prec = TYPE_PRECISION (inside_type);
2257 int inside_unsignedp = TYPE_UNSIGNED (inside_type);
2258 int inter_int = INTEGRAL_TYPE_P (inter_type);
2259 int inter_ptr = POINTER_TYPE_P (inter_type);
2260 int inter_float = FLOAT_TYPE_P (inter_type);
2261 int inter_vec = VECTOR_TYPE_P (inter_type);
2262 unsigned int inter_prec = TYPE_PRECISION (inter_type);
2263 int inter_unsignedp = TYPE_UNSIGNED (inter_type);
2264 int final_int = INTEGRAL_TYPE_P (type);
2265 int final_ptr = POINTER_TYPE_P (type);
2266 int final_float = FLOAT_TYPE_P (type);
2267 int final_vec = VECTOR_TYPE_P (type);
2268 unsigned int final_prec = TYPE_PRECISION (type);
2269 int final_unsignedp = TYPE_UNSIGNED (type);
2272 /* In addition to the cases of two conversions in a row
2273 handled below, if we are converting something to its own
2274 type via an object of identical or wider precision, neither
2275 conversion is needed. */
2276 (if (((GIMPLE && useless_type_conversion_p (type, inside_type))
2278 && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (inside_type)))
2279 && (((inter_int || inter_ptr) && final_int)
2280 || (inter_float && final_float))
2281 && inter_prec >= final_prec)
2284 /* Likewise, if the intermediate and initial types are either both
2285 float or both integer, we don't need the middle conversion if the
2286 former is wider than the latter and doesn't change the signedness
2287 (for integers). Avoid this if the final type is a pointer since
2288 then we sometimes need the middle conversion. */
2289 (if (((inter_int && inside_int) || (inter_float && inside_float))
2290 && (final_int || final_float)
2291 && inter_prec >= inside_prec
2292 && (inter_float || inter_unsignedp == inside_unsignedp))
2295 /* If we have a sign-extension of a zero-extended value, we can
2296 replace that by a single zero-extension. Likewise if the
2297 final conversion does not change precision we can drop the
2298 intermediate conversion. */
2299 (if (inside_int && inter_int && final_int
2300 && ((inside_prec < inter_prec && inter_prec < final_prec
2301 && inside_unsignedp && !inter_unsignedp)
2302 || final_prec == inter_prec))
2305 /* Two conversions in a row are not needed unless:
2306 - some conversion is floating-point (overstrict for now), or
2307 - some conversion is a vector (overstrict for now), or
2308 - the intermediate type is narrower than both initial and
2310 - the intermediate type and innermost type differ in signedness,
2311 and the outermost type is wider than the intermediate, or
2312 - the initial type is a pointer type and the precisions of the
2313 intermediate and final types differ, or
2314 - the final type is a pointer type and the precisions of the
2315 initial and intermediate types differ. */
2316 (if (! inside_float && ! inter_float && ! final_float
2317 && ! inside_vec && ! inter_vec && ! final_vec
2318 && (inter_prec >= inside_prec || inter_prec >= final_prec)
2319 && ! (inside_int && inter_int
2320 && inter_unsignedp != inside_unsignedp
2321 && inter_prec < final_prec)
2322 && ((inter_unsignedp && inter_prec > inside_prec)
2323 == (final_unsignedp && final_prec > inter_prec))
2324 && ! (inside_ptr && inter_prec != final_prec)
2325 && ! (final_ptr && inside_prec != inter_prec))
2328 /* A truncation to an unsigned type (a zero-extension) should be
2329 canonicalized as bitwise and of a mask. */
2330 (if (GIMPLE /* PR70366: doing this in GENERIC breaks -Wconversion. */
2331 && final_int && inter_int && inside_int
2332 && final_prec == inside_prec
2333 && final_prec > inter_prec
2335 (convert (bit_and @0 { wide_int_to_tree
2337 wi::mask (inter_prec, false,
2338 TYPE_PRECISION (inside_type))); })))
2340 /* If we are converting an integer to a floating-point that can
2341 represent it exactly and back to an integer, we can skip the
2342 floating-point conversion. */
2343 (if (GIMPLE /* PR66211 */
2344 && inside_int && inter_float && final_int &&
2345 (unsigned) significand_size (TYPE_MODE (inter_type))
2346 >= inside_prec - !inside_unsignedp)
2349 /* If we have a narrowing conversion to an integral type that is fed by a
2350 BIT_AND_EXPR, we might be able to remove the BIT_AND_EXPR if it merely
2351 masks off bits outside the final type (and nothing else). */
2353 (convert (bit_and @0 INTEGER_CST@1))
2354 (if (INTEGRAL_TYPE_P (type)
2355 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
2356 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))
2357 && operand_equal_p (@1, build_low_bits_mask (TREE_TYPE (@1),
2358 TYPE_PRECISION (type)), 0))
2362 /* (X /[ex] A) * A -> X. */
2364 (mult (convert1? (exact_div @0 @@1)) (convert2? @1))
2367 /* Canonicalization of binary operations. */
2369 /* Convert X + -C into X - C. */
2371 (plus @0 REAL_CST@1)
2372 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
2373 (with { tree tem = const_unop (NEGATE_EXPR, type, @1); }
2374 (if (!TREE_OVERFLOW (tem) || !flag_trapping_math)
2375 (minus @0 { tem; })))))
2377 /* Convert x+x into x*2. */
2380 (if (SCALAR_FLOAT_TYPE_P (type))
2381 (mult @0 { build_real (type, dconst2); })
2382 (if (INTEGRAL_TYPE_P (type))
2383 (mult @0 { build_int_cst (type, 2); }))))
2386 (minus integer_zerop @1)
2389 /* (ARG0 - ARG1) is the same as (-ARG1 + ARG0). So check whether
2390 ARG0 is zero and X + ARG0 reduces to X, since that would mean
2391 (-ARG1 + ARG0) reduces to -ARG1. */
2393 (minus real_zerop@0 @1)
2394 (if (fold_real_zero_addition_p (type, @0, 0))
2397 /* Transform x * -1 into -x. */
2399 (mult @0 integer_minus_onep)
2402 /* Reassociate (X * CST) * Y to (X * Y) * CST. This does not introduce
2403 signed overflow for CST != 0 && CST != -1. */
2405 (mult:c (mult:s @0 INTEGER_CST@1) @2)
2406 (if (TREE_CODE (@2) != INTEGER_CST
2407 && !integer_zerop (@1) && !integer_minus_onep (@1))
2408 (mult (mult @0 @2) @1)))
2410 /* True if we can easily extract the real and imaginary parts of a complex
2412 (match compositional_complex
2413 (convert? (complex @0 @1)))
2415 /* COMPLEX_EXPR and REALPART/IMAGPART_EXPR cancellations. */
2417 (complex (realpart @0) (imagpart @0))
2420 (realpart (complex @0 @1))
2423 (imagpart (complex @0 @1))
2426 /* Sometimes we only care about half of a complex expression. */
2428 (realpart (convert?:s (conj:s @0)))
2429 (convert (realpart @0)))
2431 (imagpart (convert?:s (conj:s @0)))
2432 (convert (negate (imagpart @0))))
2433 (for part (realpart imagpart)
2434 (for op (plus minus)
2436 (part (convert?:s@2 (op:s @0 @1)))
2437 (convert (op (part @0) (part @1))))))
2439 (realpart (convert?:s (CEXPI:s @0)))
2442 (imagpart (convert?:s (CEXPI:s @0)))
2445 /* conj(conj(x)) -> x */
2447 (conj (convert? (conj @0)))
2448 (if (tree_nop_conversion_p (TREE_TYPE (@0), type))
2451 /* conj({x,y}) -> {x,-y} */
2453 (conj (convert?:s (complex:s @0 @1)))
2454 (with { tree itype = TREE_TYPE (type); }
2455 (complex (convert:itype @0) (negate (convert:itype @1)))))
2457 /* BSWAP simplifications, transforms checked by gcc.dg/builtin-bswap-8.c. */
2458 (for bswap (BUILT_IN_BSWAP16 BUILT_IN_BSWAP32 BUILT_IN_BSWAP64)
2463 (bswap (bit_not (bswap @0)))
2465 (for bitop (bit_xor bit_ior bit_and)
2467 (bswap (bitop:c (bswap @0) @1))
2468 (bitop @0 (bswap @1)))))
2471 /* Combine COND_EXPRs and VEC_COND_EXPRs. */
2473 /* Simplify constant conditions.
2474 Only optimize constant conditions when the selected branch
2475 has the same type as the COND_EXPR. This avoids optimizing
2476 away "c ? x : throw", where the throw has a void type.
2477 Note that we cannot throw away the fold-const.c variant nor
2478 this one as we depend on doing this transform before possibly
2479 A ? B : B -> B triggers and the fold-const.c one can optimize
2480 0 ? A : B to B even if A has side-effects. Something
2481 genmatch cannot handle. */
2483 (cond INTEGER_CST@0 @1 @2)
2484 (if (integer_zerop (@0))
2485 (if (!VOID_TYPE_P (TREE_TYPE (@2)) || VOID_TYPE_P (type))
2487 (if (!VOID_TYPE_P (TREE_TYPE (@1)) || VOID_TYPE_P (type))
2490 (vec_cond VECTOR_CST@0 @1 @2)
2491 (if (integer_all_onesp (@0))
2493 (if (integer_zerop (@0))
2496 /* Simplification moved from fold_cond_expr_with_comparison. It may also
2498 /* This pattern implements two kinds simplification:
2501 (cond (cmp (convert1? x) c1) (convert2? x) c2) -> (minmax (x c)) if:
2502 1) Conversions are type widening from smaller type.
2503 2) Const c1 equals to c2 after canonicalizing comparison.
2504 3) Comparison has tree code LT, LE, GT or GE.
2505 This specific pattern is needed when (cmp (convert x) c) may not
2506 be simplified by comparison patterns because of multiple uses of
2507 x. It also makes sense here because simplifying across multiple
2508 referred var is always benefitial for complicated cases.
2511 (cond (eq (convert1? x) c1) (convert2? x) c2) -> (cond (eq x c1) c1 c2). */
2512 (for cmp (lt le gt ge eq)
2514 (cond (cmp (convert1? @1) INTEGER_CST@3) (convert2? @1) INTEGER_CST@2)
2517 tree from_type = TREE_TYPE (@1);
2518 tree c1_type = TREE_TYPE (@3), c2_type = TREE_TYPE (@2);
2519 enum tree_code code = ERROR_MARK;
2521 if (INTEGRAL_TYPE_P (from_type)
2522 && int_fits_type_p (@2, from_type)
2523 && (types_match (c1_type, from_type)
2524 || (TYPE_PRECISION (c1_type) > TYPE_PRECISION (from_type)
2525 && (TYPE_UNSIGNED (from_type)
2526 || TYPE_SIGN (c1_type) == TYPE_SIGN (from_type))))
2527 && (types_match (c2_type, from_type)
2528 || (TYPE_PRECISION (c2_type) > TYPE_PRECISION (from_type)
2529 && (TYPE_UNSIGNED (from_type)
2530 || TYPE_SIGN (c2_type) == TYPE_SIGN (from_type)))))
2534 if (wi::to_widest (@3) == (wi::to_widest (@2) - 1))
2536 /* X <= Y - 1 equals to X < Y. */
2539 /* X > Y - 1 equals to X >= Y. */
2543 if (wi::to_widest (@3) == (wi::to_widest (@2) + 1))
2545 /* X < Y + 1 equals to X <= Y. */
2548 /* X >= Y + 1 equals to X > Y. */
2552 if (code != ERROR_MARK
2553 || wi::to_widest (@2) == wi::to_widest (@3))
2555 if (cmp == LT_EXPR || cmp == LE_EXPR)
2557 if (cmp == GT_EXPR || cmp == GE_EXPR)
2561 /* Can do A == C1 ? A : C2 -> A == C1 ? C1 : C2? */
2562 else if (int_fits_type_p (@3, from_type))
2566 (if (code == MAX_EXPR)
2567 (convert (max @1 (convert @2)))
2568 (if (code == MIN_EXPR)
2569 (convert (min @1 (convert @2)))
2570 (if (code == EQ_EXPR)
2571 (convert (cond (eq @1 (convert @3))
2572 (convert:from_type @3) (convert:from_type @2)))))))))
2574 /* (cond (cmp (convert? x) c1) (op x c2) c3) -> (op (minmax x c1) c2) if:
2576 1) OP is PLUS or MINUS.
2577 2) CMP is LT, LE, GT or GE.
2578 3) C3 == (C1 op C2), and computation doesn't have undefined behavior.
2580 This pattern also handles special cases like:
2582 A) Operand x is a unsigned to signed type conversion and c1 is
2583 integer zero. In this case,
2584 (signed type)x < 0 <=> x > MAX_VAL(signed type)
2585 (signed type)x >= 0 <=> x <= MAX_VAL(signed type)
2586 B) Const c1 may not equal to (C3 op' C2). In this case we also
2587 check equality for (c1+1) and (c1-1) by adjusting comparison
2590 TODO: Though signed type is handled by this pattern, it cannot be
2591 simplified at the moment because C standard requires additional
2592 type promotion. In order to match&simplify it here, the IR needs
2593 to be cleaned up by other optimizers, i.e, VRP. */
2594 (for op (plus minus)
2595 (for cmp (lt le gt ge)
2597 (cond (cmp (convert? @X) INTEGER_CST@1) (op @X INTEGER_CST@2) INTEGER_CST@3)
2598 (with { tree from_type = TREE_TYPE (@X), to_type = TREE_TYPE (@1); }
2599 (if (types_match (from_type, to_type)
2600 /* Check if it is special case A). */
2601 || (TYPE_UNSIGNED (from_type)
2602 && !TYPE_UNSIGNED (to_type)
2603 && TYPE_PRECISION (from_type) == TYPE_PRECISION (to_type)
2604 && integer_zerop (@1)
2605 && (cmp == LT_EXPR || cmp == GE_EXPR)))
2608 bool overflow = false;
2609 enum tree_code code, cmp_code = cmp;
2611 wide_int c1 = wi::to_wide (@1);
2612 wide_int c2 = wi::to_wide (@2);
2613 wide_int c3 = wi::to_wide (@3);
2614 signop sgn = TYPE_SIGN (from_type);
2616 /* Handle special case A), given x of unsigned type:
2617 ((signed type)x < 0) <=> (x > MAX_VAL(signed type))
2618 ((signed type)x >= 0) <=> (x <= MAX_VAL(signed type)) */
2619 if (!types_match (from_type, to_type))
2621 if (cmp_code == LT_EXPR)
2623 if (cmp_code == GE_EXPR)
2625 c1 = wi::max_value (to_type);
2627 /* To simplify this pattern, we require c3 = (c1 op c2). Here we
2628 compute (c3 op' c2) and check if it equals to c1 with op' being
2629 the inverted operator of op. Make sure overflow doesn't happen
2630 if it is undefined. */
2631 if (op == PLUS_EXPR)
2632 real_c1 = wi::sub (c3, c2, sgn, &overflow);
2634 real_c1 = wi::add (c3, c2, sgn, &overflow);
2637 if (!overflow || !TYPE_OVERFLOW_UNDEFINED (from_type))
2639 /* Check if c1 equals to real_c1. Boundary condition is handled
2640 by adjusting comparison operation if necessary. */
2641 if (!wi::cmp (wi::sub (real_c1, 1, sgn, &overflow), c1, sgn)
2644 /* X <= Y - 1 equals to X < Y. */
2645 if (cmp_code == LE_EXPR)
2647 /* X > Y - 1 equals to X >= Y. */
2648 if (cmp_code == GT_EXPR)
2651 if (!wi::cmp (wi::add (real_c1, 1, sgn, &overflow), c1, sgn)
2654 /* X < Y + 1 equals to X <= Y. */
2655 if (cmp_code == LT_EXPR)
2657 /* X >= Y + 1 equals to X > Y. */
2658 if (cmp_code == GE_EXPR)
2661 if (code != cmp_code || !wi::cmp (real_c1, c1, sgn))
2663 if (cmp_code == LT_EXPR || cmp_code == LE_EXPR)
2665 if (cmp_code == GT_EXPR || cmp_code == GE_EXPR)
2670 (if (code == MAX_EXPR)
2671 (op (max @X { wide_int_to_tree (from_type, real_c1); })
2672 { wide_int_to_tree (from_type, c2); })
2673 (if (code == MIN_EXPR)
2674 (op (min @X { wide_int_to_tree (from_type, real_c1); })
2675 { wide_int_to_tree (from_type, c2); })))))))))
2677 (for cnd (cond vec_cond)
2678 /* A ? B : (A ? X : C) -> A ? B : C. */
2680 (cnd @0 (cnd @0 @1 @2) @3)
2683 (cnd @0 @1 (cnd @0 @2 @3))
2685 /* A ? B : (!A ? C : X) -> A ? B : C. */
2686 /* ??? This matches embedded conditions open-coded because genmatch
2687 would generate matching code for conditions in separate stmts only.
2688 The following is still important to merge then and else arm cases
2689 from if-conversion. */
2691 (cnd @0 @1 (cnd @2 @3 @4))
2692 (if (COMPARISON_CLASS_P (@0)
2693 && COMPARISON_CLASS_P (@2)
2694 && invert_tree_comparison
2695 (TREE_CODE (@0), HONOR_NANS (TREE_OPERAND (@0, 0))) == TREE_CODE (@2)
2696 && operand_equal_p (TREE_OPERAND (@0, 0), TREE_OPERAND (@2, 0), 0)
2697 && operand_equal_p (TREE_OPERAND (@0, 1), TREE_OPERAND (@2, 1), 0))
2700 (cnd @0 (cnd @1 @2 @3) @4)
2701 (if (COMPARISON_CLASS_P (@0)
2702 && COMPARISON_CLASS_P (@1)
2703 && invert_tree_comparison
2704 (TREE_CODE (@0), HONOR_NANS (TREE_OPERAND (@0, 0))) == TREE_CODE (@1)
2705 && operand_equal_p (TREE_OPERAND (@0, 0), TREE_OPERAND (@1, 0), 0)
2706 && operand_equal_p (TREE_OPERAND (@0, 1), TREE_OPERAND (@1, 1), 0))
2709 /* A ? B : B -> B. */
2714 /* !A ? B : C -> A ? C : B. */
2716 (cnd (logical_inverted_value truth_valued_p@0) @1 @2)
2719 /* A + (B vcmp C ? 1 : 0) -> A - (B vcmp C ? -1 : 0), since vector comparisons
2720 return all -1 or all 0 results. */
2721 /* ??? We could instead convert all instances of the vec_cond to negate,
2722 but that isn't necessarily a win on its own. */
2724 (plus:c @3 (view_convert? (vec_cond:s @0 integer_each_onep@1 integer_zerop@2)))
2725 (if (VECTOR_TYPE_P (type)
2726 && TYPE_VECTOR_SUBPARTS (type) == TYPE_VECTOR_SUBPARTS (TREE_TYPE (@1))
2727 && (TYPE_MODE (TREE_TYPE (type))
2728 == TYPE_MODE (TREE_TYPE (TREE_TYPE (@1)))))
2729 (minus @3 (view_convert (vec_cond @0 (negate @1) @2)))))
2731 /* ... likewise A - (B vcmp C ? 1 : 0) -> A + (B vcmp C ? -1 : 0). */
2733 (minus @3 (view_convert? (vec_cond:s @0 integer_each_onep@1 integer_zerop@2)))
2734 (if (VECTOR_TYPE_P (type)
2735 && TYPE_VECTOR_SUBPARTS (type) == TYPE_VECTOR_SUBPARTS (TREE_TYPE (@1))
2736 && (TYPE_MODE (TREE_TYPE (type))
2737 == TYPE_MODE (TREE_TYPE (TREE_TYPE (@1)))))
2738 (plus @3 (view_convert (vec_cond @0 (negate @1) @2)))))
2741 /* Simplifications of comparisons. */
2743 /* See if we can reduce the magnitude of a constant involved in a
2744 comparison by changing the comparison code. This is a canonicalization
2745 formerly done by maybe_canonicalize_comparison_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); }))))
2755 (cmp @0 INTEGER_CST@1)
2756 (if (tree_int_cst_sgn (@1) == 1)
2757 (acmp @0 { wide_int_to_tree (TREE_TYPE (@1), wi::to_wide (@1) - 1); }))))
2760 /* We can simplify a logical negation of a comparison to the
2761 inverted comparison. As we cannot compute an expression
2762 operator using invert_tree_comparison we have to simulate
2763 that with expression code iteration. */
2764 (for cmp (tcc_comparison)
2765 icmp (inverted_tcc_comparison)
2766 ncmp (inverted_tcc_comparison_with_nans)
2767 /* Ideally we'd like to combine the following two patterns
2768 and handle some more cases by using
2769 (logical_inverted_value (cmp @0 @1))
2770 here but for that genmatch would need to "inline" that.
2771 For now implement what forward_propagate_comparison did. */
2773 (bit_not (cmp @0 @1))
2774 (if (VECTOR_TYPE_P (type)
2775 || (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1))
2776 /* Comparison inversion may be impossible for trapping math,
2777 invert_tree_comparison will tell us. But we can't use
2778 a computed operator in the replacement tree thus we have
2779 to play the trick below. */
2780 (with { enum tree_code ic = invert_tree_comparison
2781 (cmp, HONOR_NANS (@0)); }
2787 (bit_xor (cmp @0 @1) integer_truep)
2788 (with { enum tree_code ic = invert_tree_comparison
2789 (cmp, HONOR_NANS (@0)); }
2795 /* Transform comparisons of the form X - Y CMP 0 to X CMP Y.
2796 ??? The transformation is valid for the other operators if overflow
2797 is undefined for the type, but performing it here badly interacts
2798 with the transformation in fold_cond_expr_with_comparison which
2799 attempts to synthetize ABS_EXPR. */
2802 (cmp (minus@2 @0 @1) integer_zerop)
2803 (if (single_use (@2))
2806 /* Transform comparisons of the form X * C1 CMP 0 to X CMP 0 in the
2807 signed arithmetic case. That form is created by the compiler
2808 often enough for folding it to be of value. One example is in
2809 computing loop trip counts after Operator Strength Reduction. */
2810 (for cmp (simple_comparison)
2811 scmp (swapped_simple_comparison)
2813 (cmp (mult@3 @0 INTEGER_CST@1) integer_zerop@2)
2814 /* Handle unfolded multiplication by zero. */
2815 (if (integer_zerop (@1))
2817 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2818 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2820 /* If @1 is negative we swap the sense of the comparison. */
2821 (if (tree_int_cst_sgn (@1) < 0)
2825 /* Simplify comparison of something with itself. For IEEE
2826 floating-point, we can only do some of these simplifications. */
2830 (if (! FLOAT_TYPE_P (TREE_TYPE (@0))
2831 || ! HONOR_NANS (@0))
2832 { constant_boolean_node (true, type); }
2833 (if (cmp != EQ_EXPR)
2839 || ! FLOAT_TYPE_P (TREE_TYPE (@0))
2840 || ! HONOR_NANS (@0))
2841 { constant_boolean_node (false, type); })))
2842 (for cmp (unle unge uneq)
2845 { constant_boolean_node (true, type); }))
2846 (for cmp (unlt ungt)
2852 (if (!flag_trapping_math)
2853 { constant_boolean_node (false, type); }))
2855 /* Fold ~X op ~Y as Y op X. */
2856 (for cmp (simple_comparison)
2858 (cmp (bit_not@2 @0) (bit_not@3 @1))
2859 (if (single_use (@2) && single_use (@3))
2862 /* Fold ~X op C as X op' ~C, where op' is the swapped comparison. */
2863 (for cmp (simple_comparison)
2864 scmp (swapped_simple_comparison)
2866 (cmp (bit_not@2 @0) CONSTANT_CLASS_P@1)
2867 (if (single_use (@2)
2868 && (TREE_CODE (@1) == INTEGER_CST || TREE_CODE (@1) == VECTOR_CST))
2869 (scmp @0 (bit_not @1)))))
2871 (for cmp (simple_comparison)
2872 /* Fold (double)float1 CMP (double)float2 into float1 CMP float2. */
2874 (cmp (convert@2 @0) (convert? @1))
2875 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
2876 && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@2))
2877 == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0)))
2878 && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@2))
2879 == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1))))
2882 tree type1 = TREE_TYPE (@1);
2883 if (TREE_CODE (@1) == REAL_CST && !DECIMAL_FLOAT_TYPE_P (type1))
2885 REAL_VALUE_TYPE orig = TREE_REAL_CST (@1);
2886 if (TYPE_PRECISION (type1) > TYPE_PRECISION (float_type_node)
2887 && exact_real_truncate (TYPE_MODE (float_type_node), &orig))
2888 type1 = float_type_node;
2889 if (TYPE_PRECISION (type1) > TYPE_PRECISION (double_type_node)
2890 && exact_real_truncate (TYPE_MODE (double_type_node), &orig))
2891 type1 = double_type_node;
2894 = (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type1)
2895 ? TREE_TYPE (@0) : type1);
2897 (if (TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (newtype))
2898 (cmp (convert:newtype @0) (convert:newtype @1))))))
2902 /* IEEE doesn't distinguish +0 and -0 in comparisons. */
2904 /* a CMP (-0) -> a CMP 0 */
2905 (if (REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@1)))
2906 (cmp @0 { build_real (TREE_TYPE (@1), dconst0); }))
2907 /* x != NaN is always true, other ops are always false. */
2908 (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
2909 && ! HONOR_SNANS (@1))
2910 { constant_boolean_node (cmp == NE_EXPR, type); })
2911 /* Fold comparisons against infinity. */
2912 (if (REAL_VALUE_ISINF (TREE_REAL_CST (@1))
2913 && MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (@1))))
2916 REAL_VALUE_TYPE max;
2917 enum tree_code code = cmp;
2918 bool neg = REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1));
2920 code = swap_tree_comparison (code);
2923 /* x > +Inf is always false, if with ignore sNANs. */
2924 (if (code == GT_EXPR
2925 && ! HONOR_SNANS (@0))
2926 { constant_boolean_node (false, type); })
2927 (if (code == LE_EXPR)
2928 /* x <= +Inf is always true, if we don't case about NaNs. */
2929 (if (! HONOR_NANS (@0))
2930 { constant_boolean_node (true, type); }
2931 /* x <= +Inf is the same as x == x, i.e. !isnan(x). */
2933 /* x == +Inf and x >= +Inf are always equal to x > DBL_MAX. */
2934 (if (code == EQ_EXPR || code == GE_EXPR)
2935 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
2937 (lt @0 { build_real (TREE_TYPE (@0), max); })
2938 (gt @0 { build_real (TREE_TYPE (@0), max); }))))
2939 /* x < +Inf is always equal to x <= DBL_MAX. */
2940 (if (code == LT_EXPR)
2941 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
2943 (ge @0 { build_real (TREE_TYPE (@0), max); })
2944 (le @0 { build_real (TREE_TYPE (@0), max); }))))
2945 /* x != +Inf is always equal to !(x > DBL_MAX). */
2946 (if (code == NE_EXPR)
2947 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
2948 (if (! HONOR_NANS (@0))
2950 (ge @0 { build_real (TREE_TYPE (@0), max); })
2951 (le @0 { build_real (TREE_TYPE (@0), max); }))
2953 (bit_xor (lt @0 { build_real (TREE_TYPE (@0), max); })
2954 { build_one_cst (type); })
2955 (bit_xor (gt @0 { build_real (TREE_TYPE (@0), max); })
2956 { build_one_cst (type); }))))))))))
2958 /* If this is a comparison of a real constant with a PLUS_EXPR
2959 or a MINUS_EXPR of a real constant, we can convert it into a
2960 comparison with a revised real constant as long as no overflow
2961 occurs when unsafe_math_optimizations are enabled. */
2962 (if (flag_unsafe_math_optimizations)
2963 (for op (plus minus)
2965 (cmp (op @0 REAL_CST@1) REAL_CST@2)
2968 tree tem = const_binop (op == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR,
2969 TREE_TYPE (@1), @2, @1);
2971 (if (tem && !TREE_OVERFLOW (tem))
2972 (cmp @0 { tem; }))))))
2974 /* Likewise, we can simplify a comparison of a real constant with
2975 a MINUS_EXPR whose first operand is also a real constant, i.e.
2976 (c1 - x) < c2 becomes x > c1-c2. Reordering is allowed on
2977 floating-point types only if -fassociative-math is set. */
2978 (if (flag_associative_math)
2980 (cmp (minus REAL_CST@0 @1) REAL_CST@2)
2981 (with { tree tem = const_binop (MINUS_EXPR, TREE_TYPE (@1), @0, @2); }
2982 (if (tem && !TREE_OVERFLOW (tem))
2983 (cmp { tem; } @1)))))
2985 /* Fold comparisons against built-in math functions. */
2986 (if (flag_unsafe_math_optimizations
2987 && ! flag_errno_math)
2990 (cmp (sq @0) REAL_CST@1)
2992 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
2994 /* sqrt(x) < y is always false, if y is negative. */
2995 (if (cmp == EQ_EXPR || cmp == LT_EXPR || cmp == LE_EXPR)
2996 { constant_boolean_node (false, type); })
2997 /* sqrt(x) > y is always true, if y is negative and we
2998 don't care about NaNs, i.e. negative values of x. */
2999 (if (cmp == NE_EXPR || !HONOR_NANS (@0))
3000 { constant_boolean_node (true, type); })
3001 /* sqrt(x) > y is the same as x >= 0, if y is negative. */
3002 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })))
3003 (if (real_equal (TREE_REAL_CST_PTR (@1), &dconst0))
3005 /* sqrt(x) < 0 is always false. */
3006 (if (cmp == LT_EXPR)
3007 { constant_boolean_node (false, type); })
3008 /* sqrt(x) >= 0 is always true if we don't care about NaNs. */
3009 (if (cmp == GE_EXPR && !HONOR_NANS (@0))
3010 { constant_boolean_node (true, type); })
3011 /* sqrt(x) <= 0 -> x == 0. */
3012 (if (cmp == LE_EXPR)
3014 /* Otherwise sqrt(x) cmp 0 -> x cmp 0. Here cmp can be >=, >,
3015 == or !=. In the last case:
3017 (sqrt(x) != 0) == (NaN != 0) == true == (x != 0)
3019 if x is negative or NaN. Due to -funsafe-math-optimizations,
3020 the results for other x follow from natural arithmetic. */
3022 (if (cmp == GT_EXPR || cmp == GE_EXPR)
3026 real_arithmetic (&c2, MULT_EXPR,
3027 &TREE_REAL_CST (@1), &TREE_REAL_CST (@1));
3028 real_convert (&c2, TYPE_MODE (TREE_TYPE (@0)), &c2);
3030 (if (REAL_VALUE_ISINF (c2))
3031 /* sqrt(x) > y is x == +Inf, when y is very large. */
3032 (if (HONOR_INFINITIES (@0))
3033 (eq @0 { build_real (TREE_TYPE (@0), c2); })
3034 { constant_boolean_node (false, type); })
3035 /* sqrt(x) > c is the same as x > c*c. */
3036 (cmp @0 { build_real (TREE_TYPE (@0), c2); }))))
3037 (if (cmp == LT_EXPR || cmp == LE_EXPR)
3041 real_arithmetic (&c2, MULT_EXPR,
3042 &TREE_REAL_CST (@1), &TREE_REAL_CST (@1));
3043 real_convert (&c2, TYPE_MODE (TREE_TYPE (@0)), &c2);
3045 (if (REAL_VALUE_ISINF (c2))
3047 /* sqrt(x) < y is always true, when y is a very large
3048 value and we don't care about NaNs or Infinities. */
3049 (if (! HONOR_NANS (@0) && ! HONOR_INFINITIES (@0))
3050 { constant_boolean_node (true, type); })
3051 /* sqrt(x) < y is x != +Inf when y is very large and we
3052 don't care about NaNs. */
3053 (if (! HONOR_NANS (@0))
3054 (ne @0 { build_real (TREE_TYPE (@0), c2); }))
3055 /* sqrt(x) < y is x >= 0 when y is very large and we
3056 don't care about Infinities. */
3057 (if (! HONOR_INFINITIES (@0))
3058 (ge @0 { build_real (TREE_TYPE (@0), dconst0); }))
3059 /* sqrt(x) < y is x >= 0 && x != +Inf, when y is large. */
3062 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
3063 (ne @0 { build_real (TREE_TYPE (@0), c2); }))))
3064 /* sqrt(x) < c is the same as x < c*c, if we ignore NaNs. */
3065 (if (! HONOR_NANS (@0))
3066 (cmp @0 { build_real (TREE_TYPE (@0), c2); })
3067 /* sqrt(x) < c is the same as x >= 0 && x < c*c. */
3070 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
3071 (cmp @0 { build_real (TREE_TYPE (@0), c2); })))))))))
3072 /* Transform sqrt(x) cmp sqrt(y) -> x cmp y. */
3074 (cmp (sq @0) (sq @1))
3075 (if (! HONOR_NANS (@0))
3078 /* Optimize various special cases of (FTYPE) N CMP CST. */
3079 (for cmp (lt le eq ne ge gt)
3080 icmp (le le eq ne ge ge)
3082 (cmp (float @0) REAL_CST@1)
3083 (if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (@1))
3084 && ! DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1)))
3087 tree itype = TREE_TYPE (@0);
3088 signop isign = TYPE_SIGN (itype);
3089 format_helper fmt (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (@1))));
3090 const REAL_VALUE_TYPE *cst = TREE_REAL_CST_PTR (@1);
3091 /* Be careful to preserve any potential exceptions due to
3092 NaNs. qNaNs are ok in == or != context.
3093 TODO: relax under -fno-trapping-math or
3094 -fno-signaling-nans. */
3096 = real_isnan (cst) && (cst->signalling
3097 || (cmp != EQ_EXPR && cmp != NE_EXPR));
3098 /* INT?_MIN is power-of-two so it takes
3099 only one mantissa bit. */
3100 bool signed_p = isign == SIGNED;
3101 bool itype_fits_ftype_p
3102 = TYPE_PRECISION (itype) - signed_p <= significand_size (fmt);
3104 /* TODO: allow non-fitting itype and SNaNs when
3105 -fno-trapping-math. */
3106 (if (itype_fits_ftype_p && ! exception_p)
3109 REAL_VALUE_TYPE imin, imax;
3110 real_from_integer (&imin, fmt, wi::min_value (itype), isign);
3111 real_from_integer (&imax, fmt, wi::max_value (itype), isign);
3113 REAL_VALUE_TYPE icst;
3114 if (cmp == GT_EXPR || cmp == GE_EXPR)
3115 real_ceil (&icst, fmt, cst);
3116 else if (cmp == LT_EXPR || cmp == LE_EXPR)
3117 real_floor (&icst, fmt, cst);
3119 real_trunc (&icst, fmt, cst);
3121 bool cst_int_p = !real_isnan (cst) && real_identical (&icst, cst);
3123 bool overflow_p = false;
3125 = real_to_integer (&icst, &overflow_p, TYPE_PRECISION (itype));
3128 /* Optimize cases when CST is outside of ITYPE's range. */
3129 (if (real_compare (LT_EXPR, cst, &imin))
3130 { constant_boolean_node (cmp == GT_EXPR || cmp == GE_EXPR || cmp == NE_EXPR,
3132 (if (real_compare (GT_EXPR, cst, &imax))
3133 { constant_boolean_node (cmp == LT_EXPR || cmp == LE_EXPR || cmp == NE_EXPR,
3135 /* Remove cast if CST is an integer representable by ITYPE. */
3137 (cmp @0 { gcc_assert (!overflow_p);
3138 wide_int_to_tree (itype, icst_val); })
3140 /* When CST is fractional, optimize
3141 (FTYPE) N == CST -> 0
3142 (FTYPE) N != CST -> 1. */
3143 (if (cmp == EQ_EXPR || cmp == NE_EXPR)
3144 { constant_boolean_node (cmp == NE_EXPR, type); })
3145 /* Otherwise replace with sensible integer constant. */
3148 gcc_checking_assert (!overflow_p);
3150 (icmp @0 { wide_int_to_tree (itype, icst_val); })))))))))
3152 /* Fold A /[ex] B CMP C to A CMP B * C. */
3155 (cmp (exact_div @0 @1) INTEGER_CST@2)
3156 (if (!integer_zerop (@1))
3157 (if (wi::to_wide (@2) == 0)
3159 (if (TREE_CODE (@1) == INTEGER_CST)
3163 wide_int prod = wi::mul (wi::to_wide (@2), wi::to_wide (@1),
3164 TYPE_SIGN (TREE_TYPE (@1)), &ovf);
3167 { constant_boolean_node (cmp == NE_EXPR, type); }
3168 (cmp @0 { wide_int_to_tree (TREE_TYPE (@0), prod); }))))))))
3169 (for cmp (lt le gt ge)
3171 (cmp (exact_div @0 INTEGER_CST@1) INTEGER_CST@2)
3172 (if (wi::gt_p (wi::to_wide (@1), 0, TYPE_SIGN (TREE_TYPE (@1))))
3176 wide_int prod = wi::mul (wi::to_wide (@2), wi::to_wide (@1),
3177 TYPE_SIGN (TREE_TYPE (@1)), &ovf);
3180 { constant_boolean_node (wi::lt_p (wi::to_wide (@2), 0,
3181 TYPE_SIGN (TREE_TYPE (@2)))
3182 != (cmp == LT_EXPR || cmp == LE_EXPR), type); }
3183 (cmp @0 { wide_int_to_tree (TREE_TYPE (@0), prod); }))))))
3185 /* Unordered tests if either argument is a NaN. */
3187 (bit_ior (unordered @0 @0) (unordered @1 @1))
3188 (if (types_match (@0, @1))
3191 (bit_and (ordered @0 @0) (ordered @1 @1))
3192 (if (types_match (@0, @1))
3195 (bit_ior:c (unordered @0 @0) (unordered:c@2 @0 @1))
3198 (bit_and:c (ordered @0 @0) (ordered:c@2 @0 @1))
3201 /* Simple range test simplifications. */
3202 /* A < B || A >= B -> true. */
3203 (for test1 (lt le le le ne ge)
3204 test2 (ge gt ge ne eq ne)
3206 (bit_ior:c (test1 @0 @1) (test2 @0 @1))
3207 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
3208 || VECTOR_INTEGER_TYPE_P (TREE_TYPE (@0)))
3209 { constant_boolean_node (true, type); })))
3210 /* A < B && A >= B -> false. */
3211 (for test1 (lt lt lt le ne eq)
3212 test2 (ge gt eq gt eq gt)
3214 (bit_and:c (test1 @0 @1) (test2 @0 @1))
3215 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
3216 || VECTOR_INTEGER_TYPE_P (TREE_TYPE (@0)))
3217 { constant_boolean_node (false, type); })))
3219 /* A & (2**N - 1) <= 2**K - 1 -> A & (2**N - 2**K) == 0
3220 A & (2**N - 1) > 2**K - 1 -> A & (2**N - 2**K) != 0
3222 Note that comparisons
3223 A & (2**N - 1) < 2**K -> A & (2**N - 2**K) == 0
3224 A & (2**N - 1) >= 2**K -> A & (2**N - 2**K) != 0
3225 will be canonicalized to above so there's no need to
3232 (cmp (bit_and@0 @1 INTEGER_CST@2) INTEGER_CST@3)
3233 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)))
3236 tree ty = TREE_TYPE (@0);
3237 unsigned prec = TYPE_PRECISION (ty);
3238 wide_int mask = wi::to_wide (@2, prec);
3239 wide_int rhs = wi::to_wide (@3, prec);
3240 signop sgn = TYPE_SIGN (ty);
3242 (if ((mask & (mask + 1)) == 0 && wi::gt_p (rhs, 0, sgn)
3243 && (rhs & (rhs + 1)) == 0 && wi::ge_p (mask, rhs, sgn))
3244 (eqcmp (bit_and @1 { wide_int_to_tree (ty, mask - rhs); })
3245 { build_zero_cst (ty); }))))))
3247 /* -A CMP -B -> B CMP A. */
3248 (for cmp (tcc_comparison)
3249 scmp (swapped_tcc_comparison)
3251 (cmp (negate @0) (negate @1))
3252 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
3253 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3254 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
3257 (cmp (negate @0) CONSTANT_CLASS_P@1)
3258 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
3259 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3260 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
3261 (with { tree tem = const_unop (NEGATE_EXPR, TREE_TYPE (@0), @1); }
3262 (if (tem && !TREE_OVERFLOW (tem))
3263 (scmp @0 { tem; }))))))
3265 /* Convert ABS_EXPR<x> == 0 or ABS_EXPR<x> != 0 to x == 0 or x != 0. */
3268 (op (abs @0) zerop@1)
3271 /* From fold_sign_changed_comparison and fold_widened_comparison.
3272 FIXME: the lack of symmetry is disturbing. */
3273 (for cmp (simple_comparison)
3275 (cmp (convert@0 @00) (convert?@1 @10))
3276 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
3277 /* Disable this optimization if we're casting a function pointer
3278 type on targets that require function pointer canonicalization. */
3279 && !(targetm.have_canonicalize_funcptr_for_compare ()
3280 && TREE_CODE (TREE_TYPE (@00)) == POINTER_TYPE
3281 && TREE_CODE (TREE_TYPE (TREE_TYPE (@00))) == FUNCTION_TYPE)
3283 (if (TYPE_PRECISION (TREE_TYPE (@00)) == TYPE_PRECISION (TREE_TYPE (@0))
3284 && (TREE_CODE (@10) == INTEGER_CST
3286 && (TYPE_UNSIGNED (TREE_TYPE (@00)) == TYPE_UNSIGNED (TREE_TYPE (@0))
3289 && !POINTER_TYPE_P (TREE_TYPE (@00)))
3290 /* ??? The special-casing of INTEGER_CST conversion was in the original
3291 code and here to avoid a spurious overflow flag on the resulting
3292 constant which fold_convert produces. */
3293 (if (TREE_CODE (@1) == INTEGER_CST)
3294 (cmp @00 { force_fit_type (TREE_TYPE (@00), wi::to_widest (@1), 0,
3295 TREE_OVERFLOW (@1)); })
3296 (cmp @00 (convert @1)))
3298 (if (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (TREE_TYPE (@00)))
3299 /* If possible, express the comparison in the shorter mode. */
3300 (if ((cmp == EQ_EXPR || cmp == NE_EXPR
3301 || TYPE_UNSIGNED (TREE_TYPE (@0)) == TYPE_UNSIGNED (TREE_TYPE (@00))
3302 || (!TYPE_UNSIGNED (TREE_TYPE (@0))
3303 && TYPE_UNSIGNED (TREE_TYPE (@00))))
3304 && (types_match (TREE_TYPE (@10), TREE_TYPE (@00))
3305 || ((TYPE_PRECISION (TREE_TYPE (@00))
3306 >= TYPE_PRECISION (TREE_TYPE (@10)))
3307 && (TYPE_UNSIGNED (TREE_TYPE (@00))
3308 == TYPE_UNSIGNED (TREE_TYPE (@10))))
3309 || (TREE_CODE (@10) == INTEGER_CST
3310 && INTEGRAL_TYPE_P (TREE_TYPE (@00))
3311 && int_fits_type_p (@10, TREE_TYPE (@00)))))
3312 (cmp @00 (convert @10))
3313 (if (TREE_CODE (@10) == INTEGER_CST
3314 && INTEGRAL_TYPE_P (TREE_TYPE (@00))
3315 && !int_fits_type_p (@10, TREE_TYPE (@00)))
3318 tree min = lower_bound_in_type (TREE_TYPE (@10), TREE_TYPE (@00));
3319 tree max = upper_bound_in_type (TREE_TYPE (@10), TREE_TYPE (@00));
3320 bool above = integer_nonzerop (const_binop (LT_EXPR, type, max, @10));
3321 bool below = integer_nonzerop (const_binop (LT_EXPR, type, @10, min));
3323 (if (above || below)
3324 (if (cmp == EQ_EXPR || cmp == NE_EXPR)
3325 { constant_boolean_node (cmp == EQ_EXPR ? false : true, type); }
3326 (if (cmp == LT_EXPR || cmp == LE_EXPR)
3327 { constant_boolean_node (above ? true : false, type); }
3328 (if (cmp == GT_EXPR || cmp == GE_EXPR)
3329 { constant_boolean_node (above ? false : true, type); }))))))))))))
3332 /* A local variable can never be pointed to by
3333 the default SSA name of an incoming parameter.
3334 SSA names are canonicalized to 2nd place. */
3336 (cmp addr@0 SSA_NAME@1)
3337 (if (SSA_NAME_IS_DEFAULT_DEF (@1)
3338 && TREE_CODE (SSA_NAME_VAR (@1)) == PARM_DECL)
3339 (with { tree base = get_base_address (TREE_OPERAND (@0, 0)); }
3340 (if (TREE_CODE (base) == VAR_DECL
3341 && auto_var_in_fn_p (base, current_function_decl))
3342 (if (cmp == NE_EXPR)
3343 { constant_boolean_node (true, type); }
3344 { constant_boolean_node (false, type); }))))))
3346 /* Equality compare simplifications from fold_binary */
3349 /* If we have (A | C) == D where C & ~D != 0, convert this into 0.
3350 Similarly for NE_EXPR. */
3352 (cmp (convert?@3 (bit_ior @0 INTEGER_CST@1)) INTEGER_CST@2)
3353 (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0))
3354 && wi::bit_and_not (wi::to_wide (@1), wi::to_wide (@2)) != 0)
3355 { constant_boolean_node (cmp == NE_EXPR, type); }))
3357 /* (X ^ Y) == 0 becomes X == Y, and (X ^ Y) != 0 becomes X != Y. */
3359 (cmp (bit_xor @0 @1) integer_zerop)
3362 /* (X ^ Y) == Y becomes X == 0.
3363 Likewise (X ^ Y) == X becomes Y == 0. */
3365 (cmp:c (bit_xor:c @0 @1) @0)
3366 (cmp @1 { build_zero_cst (TREE_TYPE (@1)); }))
3368 /* (X ^ C1) op C2 can be rewritten as X op (C1 ^ C2). */
3370 (cmp (convert?@3 (bit_xor @0 INTEGER_CST@1)) INTEGER_CST@2)
3371 (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0)))
3372 (cmp @0 (bit_xor @1 (convert @2)))))
3375 (cmp (convert? addr@0) integer_zerop)
3376 (if (tree_single_nonzero_warnv_p (@0, NULL))
3377 { constant_boolean_node (cmp == NE_EXPR, type); })))
3379 /* If we have (A & C) == C where C is a power of 2, convert this into
3380 (A & C) != 0. Similarly for NE_EXPR. */
3384 (cmp (bit_and@2 @0 integer_pow2p@1) @1)
3385 (icmp @2 { build_zero_cst (TREE_TYPE (@0)); })))
3387 /* If we have (A & C) != 0 ? D : 0 where C and D are powers of 2,
3388 convert this into a shift followed by ANDing with D. */
3391 (ne (bit_and @0 integer_pow2p@1) integer_zerop)
3392 integer_pow2p@2 integer_zerop)
3394 int shift = (wi::exact_log2 (wi::to_wide (@2))
3395 - wi::exact_log2 (wi::to_wide (@1)));
3399 (lshift (convert @0) { build_int_cst (integer_type_node, shift); }) @2)
3401 (convert (rshift @0 { build_int_cst (integer_type_node, -shift); })) @2))))
3403 /* If we have (A & C) != 0 where C is the sign bit of A, convert
3404 this into A < 0. Similarly for (A & C) == 0 into A >= 0. */
3408 (cmp (bit_and (convert?@2 @0) integer_pow2p@1) integer_zerop)
3409 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
3410 && type_has_mode_precision_p (TREE_TYPE (@0))
3411 && element_precision (@2) >= element_precision (@0)
3412 && wi::only_sign_bit_p (wi::to_wide (@1), element_precision (@0)))
3413 (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
3414 (ncmp (convert:stype @0) { build_zero_cst (stype); })))))
3416 /* If we have A < 0 ? C : 0 where C is a power of 2, convert
3417 this into a right shift or sign extension followed by ANDing with C. */
3420 (lt @0 integer_zerop)
3421 integer_pow2p@1 integer_zerop)
3422 (if (!TYPE_UNSIGNED (TREE_TYPE (@0)))
3424 int shift = element_precision (@0) - wi::exact_log2 (wi::to_wide (@1)) - 1;
3428 (convert (rshift @0 { build_int_cst (integer_type_node, shift); }))
3430 /* Otherwise ctype must be wider than TREE_TYPE (@0) and pure
3431 sign extension followed by AND with C will achieve the effect. */
3432 (bit_and (convert @0) @1)))))
3434 /* When the addresses are not directly of decls compare base and offset.
3435 This implements some remaining parts of fold_comparison address
3436 comparisons but still no complete part of it. Still it is good
3437 enough to make fold_stmt not regress when not dispatching to fold_binary. */
3438 (for cmp (simple_comparison)
3440 (cmp (convert1?@2 addr@0) (convert2? addr@1))
3443 HOST_WIDE_INT off0, off1;
3444 tree base0 = get_addr_base_and_unit_offset (TREE_OPERAND (@0, 0), &off0);
3445 tree base1 = get_addr_base_and_unit_offset (TREE_OPERAND (@1, 0), &off1);
3446 if (base0 && TREE_CODE (base0) == MEM_REF)
3448 off0 += mem_ref_offset (base0).to_short_addr ();
3449 base0 = TREE_OPERAND (base0, 0);
3451 if (base1 && TREE_CODE (base1) == MEM_REF)
3453 off1 += mem_ref_offset (base1).to_short_addr ();
3454 base1 = TREE_OPERAND (base1, 0);
3457 (if (base0 && base1)
3461 /* Punt in GENERIC on variables with value expressions;
3462 the value expressions might point to fields/elements
3463 of other vars etc. */
3465 && ((VAR_P (base0) && DECL_HAS_VALUE_EXPR_P (base0))
3466 || (VAR_P (base1) && DECL_HAS_VALUE_EXPR_P (base1))))
3468 else if (decl_in_symtab_p (base0)
3469 && decl_in_symtab_p (base1))
3470 equal = symtab_node::get_create (base0)
3471 ->equal_address_to (symtab_node::get_create (base1));
3472 else if ((DECL_P (base0)
3473 || TREE_CODE (base0) == SSA_NAME
3474 || TREE_CODE (base0) == STRING_CST)
3476 || TREE_CODE (base1) == SSA_NAME
3477 || TREE_CODE (base1) == STRING_CST))
3478 equal = (base0 == base1);
3482 (if (cmp == EQ_EXPR)
3483 { constant_boolean_node (off0 == off1, type); })
3484 (if (cmp == NE_EXPR)
3485 { constant_boolean_node (off0 != off1, type); })
3486 (if (cmp == LT_EXPR)
3487 { constant_boolean_node (off0 < off1, type); })
3488 (if (cmp == LE_EXPR)
3489 { constant_boolean_node (off0 <= off1, type); })
3490 (if (cmp == GE_EXPR)
3491 { constant_boolean_node (off0 >= off1, type); })
3492 (if (cmp == GT_EXPR)
3493 { constant_boolean_node (off0 > off1, type); }))
3495 && DECL_P (base0) && DECL_P (base1)
3496 /* If we compare this as integers require equal offset. */
3497 && (!INTEGRAL_TYPE_P (TREE_TYPE (@2))
3500 (if (cmp == EQ_EXPR)
3501 { constant_boolean_node (false, type); })
3502 (if (cmp == NE_EXPR)
3503 { constant_boolean_node (true, type); })))))))))
3505 /* Simplify pointer equality compares using PTA. */
3509 (if (POINTER_TYPE_P (TREE_TYPE (@0))
3510 && ptrs_compare_unequal (@0, @1))
3511 { neeq == EQ_EXPR ? boolean_false_node : boolean_true_node; })))
3513 /* PR70920: Transform (intptr_t)x eq/ne CST to x eq/ne (typeof x) CST.
3514 and (typeof ptr_cst) x eq/ne ptr_cst to x eq/ne (typeof x) CST.
3515 Disable the transform if either operand is pointer to function.
3516 This broke pr22051-2.c for arm where function pointer
3517 canonicalizaion is not wanted. */
3521 (cmp (convert @0) INTEGER_CST@1)
3522 (if ((POINTER_TYPE_P (TREE_TYPE (@0)) && !FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@0)))
3523 && INTEGRAL_TYPE_P (TREE_TYPE (@1)))
3524 || (INTEGRAL_TYPE_P (TREE_TYPE (@0)) && POINTER_TYPE_P (TREE_TYPE (@1))
3525 && !FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@1)))))
3526 (cmp @0 (convert @1)))))
3528 /* Non-equality compare simplifications from fold_binary */
3529 (for cmp (lt gt le ge)
3530 /* Comparisons with the highest or lowest possible integer of
3531 the specified precision will have known values. */
3533 (cmp (convert?@2 @0) INTEGER_CST@1)
3534 (if ((INTEGRAL_TYPE_P (TREE_TYPE (@1)) || POINTER_TYPE_P (TREE_TYPE (@1)))
3535 && tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@0)))
3538 tree arg1_type = TREE_TYPE (@1);
3539 unsigned int prec = TYPE_PRECISION (arg1_type);
3540 wide_int max = wi::max_value (arg1_type);
3541 wide_int signed_max = wi::max_value (prec, SIGNED);
3542 wide_int min = wi::min_value (arg1_type);
3545 (if (wi::to_wide (@1) == max)
3547 (if (cmp == GT_EXPR)
3548 { constant_boolean_node (false, type); })
3549 (if (cmp == GE_EXPR)
3551 (if (cmp == LE_EXPR)
3552 { constant_boolean_node (true, type); })
3553 (if (cmp == LT_EXPR)
3555 (if (wi::to_wide (@1) == min)
3557 (if (cmp == LT_EXPR)
3558 { constant_boolean_node (false, type); })
3559 (if (cmp == LE_EXPR)
3561 (if (cmp == GE_EXPR)
3562 { constant_boolean_node (true, type); })
3563 (if (cmp == GT_EXPR)
3565 (if (wi::to_wide (@1) == max - 1)
3567 (if (cmp == GT_EXPR)
3568 (eq @2 { wide_int_to_tree (TREE_TYPE (@1), wi::to_wide (@1) + 1); }))
3569 (if (cmp == LE_EXPR)
3570 (ne @2 { wide_int_to_tree (TREE_TYPE (@1), wi::to_wide (@1) + 1); }))))
3571 (if (wi::to_wide (@1) == min + 1)
3573 (if (cmp == GE_EXPR)
3574 (ne @2 { wide_int_to_tree (TREE_TYPE (@1), wi::to_wide (@1) - 1); }))
3575 (if (cmp == LT_EXPR)
3576 (eq @2 { wide_int_to_tree (TREE_TYPE (@1), wi::to_wide (@1) - 1); }))))
3577 (if (wi::to_wide (@1) == signed_max
3578 && TYPE_UNSIGNED (arg1_type)
3579 /* We will flip the signedness of the comparison operator
3580 associated with the mode of @1, so the sign bit is
3581 specified by this mode. Check that @1 is the signed
3582 max associated with this sign bit. */
3583 && prec == GET_MODE_PRECISION (SCALAR_INT_TYPE_MODE (arg1_type))
3584 /* signed_type does not work on pointer types. */
3585 && INTEGRAL_TYPE_P (arg1_type))
3586 /* The following case also applies to X < signed_max+1
3587 and X >= signed_max+1 because previous transformations. */
3588 (if (cmp == LE_EXPR || cmp == GT_EXPR)
3589 (with { tree st = signed_type_for (arg1_type); }
3590 (if (cmp == LE_EXPR)
3591 (ge (convert:st @0) { build_zero_cst (st); })
3592 (lt (convert:st @0) { build_zero_cst (st); }))))))))))
3594 (for cmp (unordered ordered unlt unle ungt unge uneq ltgt)
3595 /* If the second operand is NaN, the result is constant. */
3598 (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
3599 && (cmp != LTGT_EXPR || ! flag_trapping_math))
3600 { constant_boolean_node (cmp == ORDERED_EXPR || cmp == LTGT_EXPR
3601 ? false : true, type); })))
3603 /* bool_var != 0 becomes bool_var. */
3605 (ne @0 integer_zerop)
3606 (if (TREE_CODE (TREE_TYPE (@0)) == BOOLEAN_TYPE
3607 && types_match (type, TREE_TYPE (@0)))
3609 /* bool_var == 1 becomes bool_var. */
3611 (eq @0 integer_onep)
3612 (if (TREE_CODE (TREE_TYPE (@0)) == BOOLEAN_TYPE
3613 && types_match (type, TREE_TYPE (@0)))
3616 bool_var == 0 becomes !bool_var or
3617 bool_var != 1 becomes !bool_var
3618 here because that only is good in assignment context as long
3619 as we require a tcc_comparison in GIMPLE_CONDs where we'd
3620 replace if (x == 0) with tem = ~x; if (tem != 0) which is
3621 clearly less optimal and which we'll transform again in forwprop. */
3623 /* When one argument is a constant, overflow detection can be simplified.
3624 Currently restricted to single use so as not to interfere too much with
3625 ADD_OVERFLOW detection in tree-ssa-math-opts.c.
3626 A + CST CMP A -> A CMP' CST' */
3627 (for cmp (lt le ge gt)
3630 (cmp:c (plus@2 @0 INTEGER_CST@1) @0)
3631 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
3632 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))
3633 && wi::to_wide (@1) != 0
3635 (with { unsigned int prec = TYPE_PRECISION (TREE_TYPE (@0)); }
3636 (out @0 { wide_int_to_tree (TREE_TYPE (@0),
3637 wi::max_value (prec, UNSIGNED)
3638 - wi::to_wide (@1)); })))))
3640 /* To detect overflow in unsigned A - B, A < B is simpler than A - B > A.
3641 However, the detection logic for SUB_OVERFLOW in tree-ssa-math-opts.c
3642 expects the long form, so we restrict the transformation for now. */
3645 (cmp:c (minus@2 @0 @1) @0)
3646 (if (single_use (@2)
3647 && ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3648 && TYPE_UNSIGNED (TREE_TYPE (@0))
3649 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
3652 /* Testing for overflow is unnecessary if we already know the result. */
3657 (cmp:c (realpart (IFN_SUB_OVERFLOW@2 @0 @1)) @0)
3658 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
3659 && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
3660 (out (imagpart @2) { build_zero_cst (TREE_TYPE (@0)); }))))
3665 (cmp:c (realpart (IFN_ADD_OVERFLOW:c@2 @0 @1)) @0)
3666 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
3667 && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
3668 (out (imagpart @2) { build_zero_cst (TREE_TYPE (@0)); }))))
3670 /* For unsigned operands, -1 / B < A checks whether A * B would overflow.
3671 Simplify it to __builtin_mul_overflow (A, B, <unused>). */
3675 (cmp:c (trunc_div:s integer_all_onesp @1) @0)
3676 (if (TYPE_UNSIGNED (TREE_TYPE (@0)) && !VECTOR_TYPE_P (TREE_TYPE (@0)))
3677 (with { tree t = TREE_TYPE (@0), cpx = build_complex_type (t); }
3678 (out (imagpart (IFN_MUL_OVERFLOW:cpx @0 @1)) { build_zero_cst (t); })))))
3680 /* Simplification of math builtins. These rules must all be optimizations
3681 as well as IL simplifications. If there is a possibility that the new
3682 form could be a pessimization, the rule should go in the canonicalization
3683 section that follows this one.
3685 Rules can generally go in this section if they satisfy one of
3688 - the rule describes an identity
3690 - the rule replaces calls with something as simple as addition or
3693 - the rule contains unary calls only and simplifies the surrounding
3694 arithmetic. (The idea here is to exclude non-unary calls in which
3695 one operand is constant and in which the call is known to be cheap
3696 when the operand has that value.) */
3698 (if (flag_unsafe_math_optimizations)
3699 /* Simplify sqrt(x) * sqrt(x) -> x. */
3701 (mult (SQRT@1 @0) @1)
3702 (if (!HONOR_SNANS (type))
3705 (for op (plus minus)
3706 /* Simplify (A / C) +- (B / C) -> (A +- B) / C. */
3710 (rdiv (op @0 @2) @1)))
3712 /* Simplify sqrt(x) * sqrt(y) -> sqrt(x*y). */
3713 (for root (SQRT CBRT)
3715 (mult (root:s @0) (root:s @1))
3716 (root (mult @0 @1))))
3718 /* Simplify expN(x) * expN(y) -> expN(x+y). */
3719 (for exps (EXP EXP2 EXP10 POW10)
3721 (mult (exps:s @0) (exps:s @1))
3722 (exps (plus @0 @1))))
3724 /* Simplify a/root(b/c) into a*root(c/b). */
3725 (for root (SQRT CBRT)
3727 (rdiv @0 (root:s (rdiv:s @1 @2)))
3728 (mult @0 (root (rdiv @2 @1)))))
3730 /* Simplify x/expN(y) into x*expN(-y). */
3731 (for exps (EXP EXP2 EXP10 POW10)
3733 (rdiv @0 (exps:s @1))
3734 (mult @0 (exps (negate @1)))))
3736 (for logs (LOG LOG2 LOG10 LOG10)
3737 exps (EXP EXP2 EXP10 POW10)
3738 /* logN(expN(x)) -> x. */
3742 /* expN(logN(x)) -> x. */
3747 /* Optimize logN(func()) for various exponential functions. We
3748 want to determine the value "x" and the power "exponent" in
3749 order to transform logN(x**exponent) into exponent*logN(x). */
3750 (for logs (LOG LOG LOG LOG2 LOG2 LOG2 LOG10 LOG10)
3751 exps (EXP2 EXP10 POW10 EXP EXP10 POW10 EXP EXP2)
3754 (if (SCALAR_FLOAT_TYPE_P (type))
3760 /* Prepare to do logN(exp(exponent)) -> exponent*logN(e). */
3761 x = build_real_truncate (type, dconst_e ());
3764 /* Prepare to do logN(exp2(exponent)) -> exponent*logN(2). */
3765 x = build_real (type, dconst2);
3769 /* Prepare to do logN(exp10(exponent)) -> exponent*logN(10). */
3771 REAL_VALUE_TYPE dconst10;
3772 real_from_integer (&dconst10, VOIDmode, 10, SIGNED);
3773 x = build_real (type, dconst10);
3780 (mult (logs { x; }) @0)))))
3788 (if (SCALAR_FLOAT_TYPE_P (type))
3794 /* Prepare to do logN(sqrt(x)) -> 0.5*logN(x). */
3795 x = build_real (type, dconsthalf);
3798 /* Prepare to do logN(cbrt(x)) -> (1/3)*logN(x). */
3799 x = build_real_truncate (type, dconst_third ());
3805 (mult { x; } (logs @0))))))
3807 /* logN(pow(x,exponent)) -> exponent*logN(x). */
3808 (for logs (LOG LOG2 LOG10)
3812 (mult @1 (logs @0))))
3814 /* pow(C,x) -> exp(log(C)*x) if C > 0. */
3819 (pows REAL_CST@0 @1)
3820 (if (real_compare (GT_EXPR, TREE_REAL_CST_PTR (@0), &dconst0)
3821 && real_isfinite (TREE_REAL_CST_PTR (@0)))
3822 (exps (mult (logs @0) @1)))))
3827 exps (EXP EXP2 EXP10 POW10)
3828 /* sqrt(expN(x)) -> expN(x*0.5). */
3831 (exps (mult @0 { build_real (type, dconsthalf); })))
3832 /* cbrt(expN(x)) -> expN(x/3). */
3835 (exps (mult @0 { build_real_truncate (type, dconst_third ()); })))
3836 /* pow(expN(x), y) -> expN(x*y). */
3839 (exps (mult @0 @1))))
3841 /* tan(atan(x)) -> x. */
3848 /* cabs(x+0i) or cabs(0+xi) -> abs(x). */
3850 (CABS (complex:C @0 real_zerop@1))
3853 /* trunc(trunc(x)) -> trunc(x), etc. */
3854 (for fns (TRUNC FLOOR CEIL ROUND NEARBYINT RINT)
3858 /* f(x) -> x if x is integer valued and f does nothing for such values. */
3859 (for fns (TRUNC FLOOR CEIL ROUND NEARBYINT RINT)
3861 (fns integer_valued_real_p@0)
3864 /* hypot(x,0) and hypot(0,x) -> abs(x). */
3866 (HYPOT:c @0 real_zerop@1)
3869 /* pow(1,x) -> 1. */
3871 (POW real_onep@0 @1)
3875 /* copysign(x,x) -> x. */
3880 /* copysign(x,y) -> fabs(x) if y is nonnegative. */
3881 (COPYSIGN @0 tree_expr_nonnegative_p@1)
3884 (for scale (LDEXP SCALBN SCALBLN)
3885 /* ldexp(0, x) -> 0. */
3887 (scale real_zerop@0 @1)
3889 /* ldexp(x, 0) -> x. */
3891 (scale @0 integer_zerop@1)
3893 /* ldexp(x, y) -> x if x is +-Inf or NaN. */
3895 (scale REAL_CST@0 @1)
3896 (if (!real_isfinite (TREE_REAL_CST_PTR (@0)))
3899 /* Canonicalization of sequences of math builtins. These rules represent
3900 IL simplifications but are not necessarily optimizations.
3902 The sincos pass is responsible for picking "optimal" implementations
3903 of math builtins, which may be more complicated and can sometimes go
3904 the other way, e.g. converting pow into a sequence of sqrts.
3905 We only want to do these canonicalizations before the pass has run. */
3907 (if (flag_unsafe_math_optimizations && canonicalize_math_p ())
3908 /* Simplify tan(x) * cos(x) -> sin(x). */
3910 (mult:c (TAN:s @0) (COS:s @0))
3913 /* Simplify x * pow(x,c) -> pow(x,c+1). */
3915 (mult:c @0 (POW:s @0 REAL_CST@1))
3916 (if (!TREE_OVERFLOW (@1))
3917 (POW @0 (plus @1 { build_one_cst (type); }))))
3919 /* Simplify sin(x) / cos(x) -> tan(x). */
3921 (rdiv (SIN:s @0) (COS:s @0))
3924 /* Simplify cos(x) / sin(x) -> 1 / tan(x). */
3926 (rdiv (COS:s @0) (SIN:s @0))
3927 (rdiv { build_one_cst (type); } (TAN @0)))
3929 /* Simplify sin(x) / tan(x) -> cos(x). */
3931 (rdiv (SIN:s @0) (TAN:s @0))
3932 (if (! HONOR_NANS (@0)
3933 && ! HONOR_INFINITIES (@0))
3936 /* Simplify tan(x) / sin(x) -> 1.0 / cos(x). */
3938 (rdiv (TAN:s @0) (SIN:s @0))
3939 (if (! HONOR_NANS (@0)
3940 && ! HONOR_INFINITIES (@0))
3941 (rdiv { build_one_cst (type); } (COS @0))))
3943 /* Simplify pow(x,y) * pow(x,z) -> pow(x,y+z). */
3945 (mult (POW:s @0 @1) (POW:s @0 @2))
3946 (POW @0 (plus @1 @2)))
3948 /* Simplify pow(x,y) * pow(z,y) -> pow(x*z,y). */
3950 (mult (POW:s @0 @1) (POW:s @2 @1))
3951 (POW (mult @0 @2) @1))
3953 /* Simplify powi(x,y) * powi(z,y) -> powi(x*z,y). */
3955 (mult (POWI:s @0 @1) (POWI:s @2 @1))
3956 (POWI (mult @0 @2) @1))
3958 /* Simplify pow(x,c) / x -> pow(x,c-1). */
3960 (rdiv (POW:s @0 REAL_CST@1) @0)
3961 (if (!TREE_OVERFLOW (@1))
3962 (POW @0 (minus @1 { build_one_cst (type); }))))
3964 /* Simplify x / pow (y,z) -> x * pow(y,-z). */
3966 (rdiv @0 (POW:s @1 @2))
3967 (mult @0 (POW @1 (negate @2))))
3972 /* sqrt(sqrt(x)) -> pow(x,1/4). */
3975 (pows @0 { build_real (type, dconst_quarter ()); }))
3976 /* sqrt(cbrt(x)) -> pow(x,1/6). */
3979 (pows @0 { build_real_truncate (type, dconst_sixth ()); }))
3980 /* cbrt(sqrt(x)) -> pow(x,1/6). */
3983 (pows @0 { build_real_truncate (type, dconst_sixth ()); }))
3984 /* cbrt(cbrt(x)) -> pow(x,1/9), iff x is nonnegative. */
3986 (cbrts (cbrts tree_expr_nonnegative_p@0))
3987 (pows @0 { build_real_truncate (type, dconst_ninth ()); }))
3988 /* sqrt(pow(x,y)) -> pow(|x|,y*0.5). */
3990 (sqrts (pows @0 @1))
3991 (pows (abs @0) (mult @1 { build_real (type, dconsthalf); })))
3992 /* cbrt(pow(x,y)) -> pow(x,y/3), iff x is nonnegative. */
3994 (cbrts (pows tree_expr_nonnegative_p@0 @1))
3995 (pows @0 (mult @1 { build_real_truncate (type, dconst_third ()); })))
3996 /* pow(sqrt(x),y) -> pow(x,y*0.5). */
3998 (pows (sqrts @0) @1)
3999 (pows @0 (mult @1 { build_real (type, dconsthalf); })))
4000 /* pow(cbrt(x),y) -> pow(x,y/3) iff x is nonnegative. */
4002 (pows (cbrts tree_expr_nonnegative_p@0) @1)
4003 (pows @0 (mult @1 { build_real_truncate (type, dconst_third ()); })))
4004 /* pow(pow(x,y),z) -> pow(x,y*z) iff x is nonnegative. */
4006 (pows (pows tree_expr_nonnegative_p@0 @1) @2)
4007 (pows @0 (mult @1 @2))))
4009 /* cabs(x+xi) -> fabs(x)*sqrt(2). */
4011 (CABS (complex @0 @0))
4012 (mult (abs @0) { build_real_truncate (type, dconst_sqrt2 ()); }))
4014 /* hypot(x,x) -> fabs(x)*sqrt(2). */
4017 (mult (abs @0) { build_real_truncate (type, dconst_sqrt2 ()); }))
4019 /* cexp(x+yi) -> exp(x)*cexpi(y). */
4024 (cexps compositional_complex@0)
4025 (if (targetm.libc_has_function (function_c99_math_complex))
4027 (mult (exps@1 (realpart @0)) (realpart (cexpis:type@2 (imagpart @0))))
4028 (mult @1 (imagpart @2)))))))
4030 (if (canonicalize_math_p ())
4031 /* floor(x) -> trunc(x) if x is nonnegative. */
4035 (floors tree_expr_nonnegative_p@0)
4038 (match double_value_p
4040 (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == double_type_node)))
4041 (for froms (BUILT_IN_TRUNCL
4053 /* truncl(extend(x)) -> extend(trunc(x)), etc., if x is a double. */
4054 (if (optimize && canonicalize_math_p ())
4056 (froms (convert double_value_p@0))
4057 (convert (tos @0)))))
4059 (match float_value_p
4061 (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == float_type_node)))
4062 (for froms (BUILT_IN_TRUNCL BUILT_IN_TRUNC
4063 BUILT_IN_FLOORL BUILT_IN_FLOOR
4064 BUILT_IN_CEILL BUILT_IN_CEIL
4065 BUILT_IN_ROUNDL BUILT_IN_ROUND
4066 BUILT_IN_NEARBYINTL BUILT_IN_NEARBYINT
4067 BUILT_IN_RINTL BUILT_IN_RINT)
4068 tos (BUILT_IN_TRUNCF BUILT_IN_TRUNCF
4069 BUILT_IN_FLOORF BUILT_IN_FLOORF
4070 BUILT_IN_CEILF BUILT_IN_CEILF
4071 BUILT_IN_ROUNDF BUILT_IN_ROUNDF
4072 BUILT_IN_NEARBYINTF BUILT_IN_NEARBYINTF
4073 BUILT_IN_RINTF BUILT_IN_RINTF)
4074 /* truncl(extend(x)) and trunc(extend(x)) -> extend(truncf(x)), etc.,
4076 (if (optimize && canonicalize_math_p ()
4077 && targetm.libc_has_function (function_c99_misc))
4079 (froms (convert float_value_p@0))
4080 (convert (tos @0)))))
4082 (for froms (XFLOORL XCEILL XROUNDL XRINTL)
4083 tos (XFLOOR XCEIL XROUND XRINT)
4084 /* llfloorl(extend(x)) -> llfloor(x), etc., if x is a double. */
4085 (if (optimize && canonicalize_math_p ())
4087 (froms (convert double_value_p@0))
4090 (for froms (XFLOORL XCEILL XROUNDL XRINTL
4091 XFLOOR XCEIL XROUND XRINT)
4092 tos (XFLOORF XCEILF XROUNDF XRINTF)
4093 /* llfloorl(extend(x)) and llfloor(extend(x)) -> llfloorf(x), etc.,
4095 (if (optimize && canonicalize_math_p ())
4097 (froms (convert float_value_p@0))
4100 (if (canonicalize_math_p ())
4101 /* xfloor(x) -> fix_trunc(x) if x is nonnegative. */
4102 (for floors (IFLOOR LFLOOR LLFLOOR)
4104 (floors tree_expr_nonnegative_p@0)
4107 (if (canonicalize_math_p ())
4108 /* xfloor(x) -> fix_trunc(x), etc., if x is integer valued. */
4109 (for fns (IFLOOR LFLOOR LLFLOOR
4111 IROUND LROUND LLROUND)
4113 (fns integer_valued_real_p@0)
4115 (if (!flag_errno_math)
4116 /* xrint(x) -> fix_trunc(x), etc., if x is integer valued. */
4117 (for rints (IRINT LRINT LLRINT)
4119 (rints integer_valued_real_p@0)
4122 (if (canonicalize_math_p ())
4123 (for ifn (IFLOOR ICEIL IROUND IRINT)
4124 lfn (LFLOOR LCEIL LROUND LRINT)
4125 llfn (LLFLOOR LLCEIL LLROUND LLRINT)
4126 /* Canonicalize iround (x) to lround (x) on ILP32 targets where
4127 sizeof (int) == sizeof (long). */
4128 (if (TYPE_PRECISION (integer_type_node)
4129 == TYPE_PRECISION (long_integer_type_node))
4132 (lfn:long_integer_type_node @0)))
4133 /* Canonicalize llround (x) to lround (x) on LP64 targets where
4134 sizeof (long long) == sizeof (long). */
4135 (if (TYPE_PRECISION (long_long_integer_type_node)
4136 == TYPE_PRECISION (long_integer_type_node))
4139 (lfn:long_integer_type_node @0)))))
4141 /* cproj(x) -> x if we're ignoring infinities. */
4144 (if (!HONOR_INFINITIES (type))
4147 /* If the real part is inf and the imag part is known to be
4148 nonnegative, return (inf + 0i). */
4150 (CPROJ (complex REAL_CST@0 tree_expr_nonnegative_p@1))
4151 (if (real_isinf (TREE_REAL_CST_PTR (@0)))
4152 { build_complex_inf (type, false); }))
4154 /* If the imag part is inf, return (inf+I*copysign(0,imag)). */
4156 (CPROJ (complex @0 REAL_CST@1))
4157 (if (real_isinf (TREE_REAL_CST_PTR (@1)))
4158 { build_complex_inf (type, TREE_REAL_CST_PTR (@1)->sign); }))
4164 (pows @0 REAL_CST@1)
4166 const REAL_VALUE_TYPE *value = TREE_REAL_CST_PTR (@1);
4167 REAL_VALUE_TYPE tmp;
4170 /* pow(x,0) -> 1. */
4171 (if (real_equal (value, &dconst0))
4172 { build_real (type, dconst1); })
4173 /* pow(x,1) -> x. */
4174 (if (real_equal (value, &dconst1))
4176 /* pow(x,-1) -> 1/x. */
4177 (if (real_equal (value, &dconstm1))
4178 (rdiv { build_real (type, dconst1); } @0))
4179 /* pow(x,0.5) -> sqrt(x). */
4180 (if (flag_unsafe_math_optimizations
4181 && canonicalize_math_p ()
4182 && real_equal (value, &dconsthalf))
4184 /* pow(x,1/3) -> cbrt(x). */
4185 (if (flag_unsafe_math_optimizations
4186 && canonicalize_math_p ()
4187 && (tmp = real_value_truncate (TYPE_MODE (type), dconst_third ()),
4188 real_equal (value, &tmp)))
4191 /* powi(1,x) -> 1. */
4193 (POWI real_onep@0 @1)
4197 (POWI @0 INTEGER_CST@1)
4199 /* powi(x,0) -> 1. */
4200 (if (wi::to_wide (@1) == 0)
4201 { build_real (type, dconst1); })
4202 /* powi(x,1) -> x. */
4203 (if (wi::to_wide (@1) == 1)
4205 /* powi(x,-1) -> 1/x. */
4206 (if (wi::to_wide (@1) == -1)
4207 (rdiv { build_real (type, dconst1); } @0))))
4209 /* Narrowing of arithmetic and logical operations.
4211 These are conceptually similar to the transformations performed for
4212 the C/C++ front-ends by shorten_binary_op and shorten_compare. Long
4213 term we want to move all that code out of the front-ends into here. */
4215 /* If we have a narrowing conversion of an arithmetic operation where
4216 both operands are widening conversions from the same type as the outer
4217 narrowing conversion. Then convert the innermost operands to a suitable
4218 unsigned type (to avoid introducing undefined behavior), perform the
4219 operation and convert the result to the desired type. */
4220 (for op (plus minus)
4222 (convert (op:s (convert@2 @0) (convert?@3 @1)))
4223 (if (INTEGRAL_TYPE_P (type)
4224 /* We check for type compatibility between @0 and @1 below,
4225 so there's no need to check that @1/@3 are integral types. */
4226 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
4227 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
4228 /* The precision of the type of each operand must match the
4229 precision of the mode of each operand, similarly for the
4231 && type_has_mode_precision_p (TREE_TYPE (@0))
4232 && type_has_mode_precision_p (TREE_TYPE (@1))
4233 && type_has_mode_precision_p (type)
4234 /* The inner conversion must be a widening conversion. */
4235 && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
4236 && types_match (@0, type)
4237 && (types_match (@0, @1)
4238 /* Or the second operand is const integer or converted const
4239 integer from valueize. */
4240 || TREE_CODE (@1) == INTEGER_CST))
4241 (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
4242 (op @0 (convert @1))
4243 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
4244 (convert (op (convert:utype @0)
4245 (convert:utype @1))))))))
4247 /* This is another case of narrowing, specifically when there's an outer
4248 BIT_AND_EXPR which masks off bits outside the type of the innermost
4249 operands. Like the previous case we have to convert the operands
4250 to unsigned types to avoid introducing undefined behavior for the
4251 arithmetic operation. */
4252 (for op (minus plus)
4254 (bit_and (op:s (convert@2 @0) (convert@3 @1)) INTEGER_CST@4)
4255 (if (INTEGRAL_TYPE_P (type)
4256 /* We check for type compatibility between @0 and @1 below,
4257 so there's no need to check that @1/@3 are integral types. */
4258 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
4259 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
4260 /* The precision of the type of each operand must match the
4261 precision of the mode of each operand, similarly for the
4263 && type_has_mode_precision_p (TREE_TYPE (@0))
4264 && type_has_mode_precision_p (TREE_TYPE (@1))
4265 && type_has_mode_precision_p (type)
4266 /* The inner conversion must be a widening conversion. */
4267 && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
4268 && types_match (@0, @1)
4269 && (tree_int_cst_min_precision (@4, TYPE_SIGN (TREE_TYPE (@0)))
4270 <= TYPE_PRECISION (TREE_TYPE (@0)))
4271 && (wi::to_wide (@4)
4272 & wi::mask (TYPE_PRECISION (TREE_TYPE (@0)),
4273 true, TYPE_PRECISION (type))) == 0)
4274 (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
4275 (with { tree ntype = TREE_TYPE (@0); }
4276 (convert (bit_and (op @0 @1) (convert:ntype @4))))
4277 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
4278 (convert (bit_and (op (convert:utype @0) (convert:utype @1))
4279 (convert:utype @4))))))))
4281 /* Transform (@0 < @1 and @0 < @2) to use min,
4282 (@0 > @1 and @0 > @2) to use max */
4283 (for op (lt le gt ge)
4284 ext (min min max max)
4286 (bit_and (op:cs @0 @1) (op:cs @0 @2))
4287 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
4288 && TREE_CODE (@0) != INTEGER_CST)
4289 (op @0 (ext @1 @2)))))
4292 /* signbit(x) -> 0 if x is nonnegative. */
4293 (SIGNBIT tree_expr_nonnegative_p@0)
4294 { integer_zero_node; })
4297 /* signbit(x) -> x<0 if x doesn't have signed zeros. */
4299 (if (!HONOR_SIGNED_ZEROS (@0))
4300 (convert (lt @0 { build_real (TREE_TYPE (@0), dconst0); }))))
4302 /* Transform comparisons of the form X +- C1 CMP C2 to X CMP C2 -+ C1. */
4304 (for op (plus minus)
4307 (cmp (op@3 @0 INTEGER_CST@1) INTEGER_CST@2)
4308 (if (!TREE_OVERFLOW (@1) && !TREE_OVERFLOW (@2)
4309 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0))
4310 && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@0))
4311 && !TYPE_SATURATING (TREE_TYPE (@0)))
4312 (with { tree res = int_const_binop (rop, @2, @1); }
4313 (if (TREE_OVERFLOW (res)
4314 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
4315 { constant_boolean_node (cmp == NE_EXPR, type); }
4316 (if (single_use (@3))
4317 (cmp @0 { res; }))))))))
4318 (for cmp (lt le gt ge)
4319 (for op (plus minus)
4322 (cmp (op@3 @0 INTEGER_CST@1) INTEGER_CST@2)
4323 (if (!TREE_OVERFLOW (@1) && !TREE_OVERFLOW (@2)
4324 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
4325 (with { tree res = int_const_binop (rop, @2, @1); }
4326 (if (TREE_OVERFLOW (res))
4328 fold_overflow_warning (("assuming signed overflow does not occur "
4329 "when simplifying conditional to constant"),
4330 WARN_STRICT_OVERFLOW_CONDITIONAL);
4331 bool less = cmp == LE_EXPR || cmp == LT_EXPR;
4332 /* wi::ges_p (@2, 0) should be sufficient for a signed type. */
4333 bool ovf_high = wi::lt_p (wi::to_wide (@1), 0,
4334 TYPE_SIGN (TREE_TYPE (@1)))
4335 != (op == MINUS_EXPR);
4336 constant_boolean_node (less == ovf_high, type);
4338 (if (single_use (@3))
4341 fold_overflow_warning (("assuming signed overflow does not occur "
4342 "when changing X +- C1 cmp C2 to "
4344 WARN_STRICT_OVERFLOW_COMPARISON);
4346 (cmp @0 { res; })))))))))
4348 /* Canonicalizations of BIT_FIELD_REFs. */
4351 (BIT_FIELD_REF @0 @1 @2)
4353 (if (TREE_CODE (TREE_TYPE (@0)) == COMPLEX_TYPE
4354 && tree_int_cst_equal (@1, TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))))
4356 (if (integer_zerop (@2))
4357 (view_convert (realpart @0)))
4358 (if (tree_int_cst_equal (@2, TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))))
4359 (view_convert (imagpart @0)))))
4360 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
4361 && INTEGRAL_TYPE_P (type)
4362 /* On GIMPLE this should only apply to register arguments. */
4363 && (! GIMPLE || is_gimple_reg (@0))
4364 /* A bit-field-ref that referenced the full argument can be stripped. */
4365 && ((compare_tree_int (@1, TYPE_PRECISION (TREE_TYPE (@0))) == 0
4366 && integer_zerop (@2))
4367 /* Low-parts can be reduced to integral conversions.
4368 ??? The following doesn't work for PDP endian. */
4369 || (BYTES_BIG_ENDIAN == WORDS_BIG_ENDIAN
4370 /* Don't even think about BITS_BIG_ENDIAN. */
4371 && TYPE_PRECISION (TREE_TYPE (@0)) % BITS_PER_UNIT == 0
4372 && TYPE_PRECISION (type) % BITS_PER_UNIT == 0
4373 && compare_tree_int (@2, (BYTES_BIG_ENDIAN
4374 ? (TYPE_PRECISION (TREE_TYPE (@0))
4375 - TYPE_PRECISION (type))
4379 /* Simplify vector extracts. */
4382 (BIT_FIELD_REF CONSTRUCTOR@0 @1 @2)
4383 (if (VECTOR_TYPE_P (TREE_TYPE (@0))
4384 && (types_match (type, TREE_TYPE (TREE_TYPE (@0)))
4385 || (VECTOR_TYPE_P (type)
4386 && types_match (TREE_TYPE (type), TREE_TYPE (TREE_TYPE (@0))))))
4389 tree ctor = (TREE_CODE (@0) == SSA_NAME
4390 ? gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0)) : @0);
4391 tree eltype = TREE_TYPE (TREE_TYPE (ctor));
4392 unsigned HOST_WIDE_INT width = tree_to_uhwi (TYPE_SIZE (eltype));
4393 unsigned HOST_WIDE_INT n = tree_to_uhwi (@1);
4394 unsigned HOST_WIDE_INT idx = tree_to_uhwi (@2);
4397 && (idx % width) == 0
4399 && ((idx + n) / width) <= TYPE_VECTOR_SUBPARTS (TREE_TYPE (ctor)))
4404 /* Constructor elements can be subvectors. */
4405 unsigned HOST_WIDE_INT k = 1;
4406 if (CONSTRUCTOR_NELTS (ctor) != 0)
4408 tree cons_elem = TREE_TYPE (CONSTRUCTOR_ELT (ctor, 0)->value);
4409 if (TREE_CODE (cons_elem) == VECTOR_TYPE)
4410 k = TYPE_VECTOR_SUBPARTS (cons_elem);
4414 /* We keep an exact subset of the constructor elements. */
4415 (if ((idx % k) == 0 && (n % k) == 0)
4416 (if (CONSTRUCTOR_NELTS (ctor) == 0)
4417 { build_constructor (type, NULL); }
4424 (if (idx < CONSTRUCTOR_NELTS (ctor))
4425 { CONSTRUCTOR_ELT (ctor, idx)->value; }
4426 { build_zero_cst (type); })
4428 vec<constructor_elt, va_gc> *vals;
4429 vec_alloc (vals, n);
4430 for (unsigned i = 0;
4431 i < n && idx + i < CONSTRUCTOR_NELTS (ctor); ++i)
4432 CONSTRUCTOR_APPEND_ELT (vals, NULL_TREE,
4433 CONSTRUCTOR_ELT (ctor, idx + i)->value);
4434 build_constructor (type, vals);
4436 /* The bitfield references a single constructor element. */
4437 (if (idx + n <= (idx / k + 1) * k)
4439 (if (CONSTRUCTOR_NELTS (ctor) <= idx / k)
4440 { build_zero_cst (type); })
4442 { CONSTRUCTOR_ELT (ctor, idx / k)->value; })
4443 (BIT_FIELD_REF { CONSTRUCTOR_ELT (ctor, idx / k)->value; }
4444 @1 { bitsize_int ((idx % k) * width); })))))))))
4446 /* Simplify a bit extraction from a bit insertion for the cases with
4447 the inserted element fully covering the extraction or the insertion
4448 not touching the extraction. */
4450 (BIT_FIELD_REF (bit_insert @0 @1 @ipos) @rsize @rpos)
4453 unsigned HOST_WIDE_INT isize;
4454 if (INTEGRAL_TYPE_P (TREE_TYPE (@1)))
4455 isize = TYPE_PRECISION (TREE_TYPE (@1));
4457 isize = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (@1)));
4460 (if (wi::leu_p (wi::to_wide (@ipos), wi::to_wide (@rpos))
4461 && wi::leu_p (wi::to_wide (@rpos) + wi::to_wide (@rsize),
4462 wi::to_wide (@ipos) + isize))
4463 (BIT_FIELD_REF @1 @rsize { wide_int_to_tree (bitsizetype,
4465 - wi::to_wide (@ipos)); }))
4466 (if (wi::geu_p (wi::to_wide (@ipos),
4467 wi::to_wide (@rpos) + wi::to_wide (@rsize))
4468 || wi::geu_p (wi::to_wide (@rpos),
4469 wi::to_wide (@ipos) + isize))
4470 (BIT_FIELD_REF @0 @rsize @rpos)))))