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-2018 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 /* Binary operations and their associated IFN_COND_* function. */
79 (define_operator_list UNCOND_BINARY
81 mult trunc_div trunc_mod rdiv
83 bit_and bit_ior bit_xor)
84 (define_operator_list COND_BINARY
85 IFN_COND_ADD IFN_COND_SUB
86 IFN_COND_MUL IFN_COND_DIV IFN_COND_MOD IFN_COND_RDIV
87 IFN_COND_MIN IFN_COND_MAX
88 IFN_COND_AND IFN_COND_IOR IFN_COND_XOR)
90 /* Same for ternary operations. */
91 (define_operator_list UNCOND_TERNARY
92 IFN_FMA IFN_FMS IFN_FNMA IFN_FNMS)
93 (define_operator_list COND_TERNARY
94 IFN_COND_FMA IFN_COND_FMS IFN_COND_FNMA IFN_COND_FNMS)
96 /* As opposed to convert?, this still creates a single pattern, so
97 it is not a suitable replacement for convert? in all cases. */
98 (match (nop_convert @0)
100 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))))
101 (match (nop_convert @0)
103 (if (VECTOR_TYPE_P (type) && VECTOR_TYPE_P (TREE_TYPE (@0))
104 && known_eq (TYPE_VECTOR_SUBPARTS (type),
105 TYPE_VECTOR_SUBPARTS (TREE_TYPE (@0)))
106 && tree_nop_conversion_p (TREE_TYPE (type), TREE_TYPE (TREE_TYPE (@0))))))
107 /* This one has to be last, or it shadows the others. */
108 (match (nop_convert @0)
111 /* Transform likes of (char) ABS_EXPR <(int) x> into (char) ABSU_EXPR <x>
112 ABSU_EXPR returns unsigned absolute value of the operand and the operand
113 of the ABSU_EXPR will have the corresponding signed type. */
114 (simplify (abs (convert @0))
115 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
116 && !TYPE_UNSIGNED (TREE_TYPE (@0))
117 && element_precision (type) > element_precision (TREE_TYPE (@0)))
118 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
119 (convert (absu:utype @0)))))
122 /* Simplifications of operations with one constant operand and
123 simplifications to constants or single values. */
125 (for op (plus pointer_plus minus bit_ior bit_xor)
127 (op @0 integer_zerop)
130 /* 0 +p index -> (type)index */
132 (pointer_plus integer_zerop @1)
133 (non_lvalue (convert @1)))
135 /* ptr - 0 -> (type)ptr */
137 (pointer_diff @0 integer_zerop)
140 /* See if ARG1 is zero and X + ARG1 reduces to X.
141 Likewise if the operands are reversed. */
143 (plus:c @0 real_zerop@1)
144 (if (fold_real_zero_addition_p (type, @1, 0))
147 /* See if ARG1 is zero and X - ARG1 reduces to X. */
149 (minus @0 real_zerop@1)
150 (if (fold_real_zero_addition_p (type, @1, 1))
154 This is unsafe for certain floats even in non-IEEE formats.
155 In IEEE, it is unsafe because it does wrong for NaNs.
156 Also note that operand_equal_p is always false if an operand
160 (if (!FLOAT_TYPE_P (type) || !HONOR_NANS (type))
161 { build_zero_cst (type); }))
163 (pointer_diff @@0 @0)
164 { build_zero_cst (type); })
167 (mult @0 integer_zerop@1)
170 /* Maybe fold x * 0 to 0. The expressions aren't the same
171 when x is NaN, since x * 0 is also NaN. Nor are they the
172 same in modes with signed zeros, since multiplying a
173 negative value by 0 gives -0, not +0. */
175 (mult @0 real_zerop@1)
176 (if (!HONOR_NANS (type) && !HONOR_SIGNED_ZEROS (type))
179 /* In IEEE floating point, x*1 is not equivalent to x for snans.
180 Likewise for complex arithmetic with signed zeros. */
183 (if (!HONOR_SNANS (type)
184 && (!HONOR_SIGNED_ZEROS (type)
185 || !COMPLEX_FLOAT_TYPE_P (type)))
188 /* Transform x * -1.0 into -x. */
190 (mult @0 real_minus_onep)
191 (if (!HONOR_SNANS (type)
192 && (!HONOR_SIGNED_ZEROS (type)
193 || !COMPLEX_FLOAT_TYPE_P (type)))
196 (for cmp (gt ge lt le)
197 outp (convert convert negate negate)
198 outn (negate negate convert convert)
199 /* Transform (X > 0.0 ? 1.0 : -1.0) into copysign(1, X). */
200 /* Transform (X >= 0.0 ? 1.0 : -1.0) into copysign(1, X). */
201 /* Transform (X < 0.0 ? 1.0 : -1.0) into copysign(1,-X). */
202 /* Transform (X <= 0.0 ? 1.0 : -1.0) into copysign(1,-X). */
204 (cond (cmp @0 real_zerop) real_onep@1 real_minus_onep)
205 (if (!HONOR_NANS (type) && !HONOR_SIGNED_ZEROS (type)
206 && types_match (type, TREE_TYPE (@0)))
208 (if (types_match (type, float_type_node))
209 (BUILT_IN_COPYSIGNF @1 (outp @0)))
210 (if (types_match (type, double_type_node))
211 (BUILT_IN_COPYSIGN @1 (outp @0)))
212 (if (types_match (type, long_double_type_node))
213 (BUILT_IN_COPYSIGNL @1 (outp @0))))))
214 /* Transform (X > 0.0 ? -1.0 : 1.0) into copysign(1,-X). */
215 /* Transform (X >= 0.0 ? -1.0 : 1.0) into copysign(1,-X). */
216 /* Transform (X < 0.0 ? -1.0 : 1.0) into copysign(1,X). */
217 /* Transform (X <= 0.0 ? -1.0 : 1.0) into copysign(1,X). */
219 (cond (cmp @0 real_zerop) real_minus_onep real_onep@1)
220 (if (!HONOR_NANS (type) && !HONOR_SIGNED_ZEROS (type)
221 && types_match (type, TREE_TYPE (@0)))
223 (if (types_match (type, float_type_node))
224 (BUILT_IN_COPYSIGNF @1 (outn @0)))
225 (if (types_match (type, double_type_node))
226 (BUILT_IN_COPYSIGN @1 (outn @0)))
227 (if (types_match (type, long_double_type_node))
228 (BUILT_IN_COPYSIGNL @1 (outn @0)))))))
230 /* Transform X * copysign (1.0, X) into abs(X). */
232 (mult:c @0 (COPYSIGN_ALL real_onep @0))
233 (if (!HONOR_NANS (type) && !HONOR_SIGNED_ZEROS (type))
236 /* Transform X * copysign (1.0, -X) into -abs(X). */
238 (mult:c @0 (COPYSIGN_ALL real_onep (negate @0)))
239 (if (!HONOR_NANS (type) && !HONOR_SIGNED_ZEROS (type))
242 /* Transform copysign (CST, X) into copysign (ABS(CST), X). */
244 (COPYSIGN_ALL REAL_CST@0 @1)
245 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@0)))
246 (COPYSIGN_ALL (negate @0) @1)))
248 /* X * 1, X / 1 -> X. */
249 (for op (mult trunc_div ceil_div floor_div round_div exact_div)
254 /* (A / (1 << B)) -> (A >> B).
255 Only for unsigned A. For signed A, this would not preserve rounding
257 For example: (-1 / ( 1 << B)) != -1 >> B. */
259 (trunc_div @0 (lshift integer_onep@1 @2))
260 (if ((TYPE_UNSIGNED (type) || tree_expr_nonnegative_p (@0))
261 && (!VECTOR_TYPE_P (type)
262 || target_supports_op_p (type, RSHIFT_EXPR, optab_vector)
263 || target_supports_op_p (type, RSHIFT_EXPR, optab_scalar)))
266 /* Preserve explicit divisions by 0: the C++ front-end wants to detect
267 undefined behavior in constexpr evaluation, and assuming that the division
268 traps enables better optimizations than these anyway. */
269 (for div (trunc_div ceil_div floor_div round_div exact_div)
270 /* 0 / X is always zero. */
272 (div integer_zerop@0 @1)
273 /* But not for 0 / 0 so that we can get the proper warnings and errors. */
274 (if (!integer_zerop (@1))
278 (div @0 integer_minus_onep@1)
279 (if (!TYPE_UNSIGNED (type))
284 /* But not for 0 / 0 so that we can get the proper warnings and errors.
285 And not for _Fract types where we can't build 1. */
286 (if (!integer_zerop (@0) && !ALL_FRACT_MODE_P (TYPE_MODE (type)))
287 { build_one_cst (type); }))
288 /* X / abs (X) is X < 0 ? -1 : 1. */
291 (if (INTEGRAL_TYPE_P (type)
292 && TYPE_OVERFLOW_UNDEFINED (type))
293 (cond (lt @0 { build_zero_cst (type); })
294 { build_minus_one_cst (type); } { build_one_cst (type); })))
297 (div:C @0 (negate @0))
298 (if ((INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
299 && TYPE_OVERFLOW_UNDEFINED (type))
300 { build_minus_one_cst (type); })))
302 /* For unsigned integral types, FLOOR_DIV_EXPR is the same as
303 TRUNC_DIV_EXPR. Rewrite into the latter in this case. */
306 (if ((INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
307 && TYPE_UNSIGNED (type))
310 /* Combine two successive divisions. Note that combining ceil_div
311 and floor_div is trickier and combining round_div even more so. */
312 (for div (trunc_div exact_div)
314 (div (div @0 INTEGER_CST@1) INTEGER_CST@2)
316 wi::overflow_type overflow;
317 wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@2),
318 TYPE_SIGN (type), &overflow);
321 (div @0 { wide_int_to_tree (type, mul); })
322 (if (TYPE_UNSIGNED (type)
323 || mul != wi::min_value (TYPE_PRECISION (type), SIGNED))
324 { build_zero_cst (type); })))))
326 /* Combine successive multiplications. Similar to above, but handling
327 overflow is different. */
329 (mult (mult @0 INTEGER_CST@1) INTEGER_CST@2)
331 wi::overflow_type overflow;
332 wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@2),
333 TYPE_SIGN (type), &overflow);
335 /* Skip folding on overflow: the only special case is @1 * @2 == -INT_MIN,
336 otherwise undefined overflow implies that @0 must be zero. */
337 (if (!overflow || TYPE_OVERFLOW_WRAPS (type))
338 (mult @0 { wide_int_to_tree (type, mul); }))))
340 /* Optimize A / A to 1.0 if we don't care about
341 NaNs or Infinities. */
344 (if (FLOAT_TYPE_P (type)
345 && ! HONOR_NANS (type)
346 && ! HONOR_INFINITIES (type))
347 { build_one_cst (type); }))
349 /* Optimize -A / A to -1.0 if we don't care about
350 NaNs or Infinities. */
352 (rdiv:C @0 (negate @0))
353 (if (FLOAT_TYPE_P (type)
354 && ! HONOR_NANS (type)
355 && ! HONOR_INFINITIES (type))
356 { build_minus_one_cst (type); }))
358 /* PR71078: x / abs(x) -> copysign (1.0, x) */
360 (rdiv:C (convert? @0) (convert? (abs @0)))
361 (if (SCALAR_FLOAT_TYPE_P (type)
362 && ! HONOR_NANS (type)
363 && ! HONOR_INFINITIES (type))
365 (if (types_match (type, float_type_node))
366 (BUILT_IN_COPYSIGNF { build_one_cst (type); } (convert @0)))
367 (if (types_match (type, double_type_node))
368 (BUILT_IN_COPYSIGN { build_one_cst (type); } (convert @0)))
369 (if (types_match (type, long_double_type_node))
370 (BUILT_IN_COPYSIGNL { build_one_cst (type); } (convert @0))))))
372 /* In IEEE floating point, x/1 is not equivalent to x for snans. */
375 (if (!HONOR_SNANS (type))
378 /* In IEEE floating point, x/-1 is not equivalent to -x for snans. */
380 (rdiv @0 real_minus_onep)
381 (if (!HONOR_SNANS (type))
384 (if (flag_reciprocal_math)
385 /* Convert (A/B)/C to A/(B*C). */
387 (rdiv (rdiv:s @0 @1) @2)
388 (rdiv @0 (mult @1 @2)))
390 /* Canonicalize x / (C1 * y) to (x * C2) / y. */
392 (rdiv @0 (mult:s @1 REAL_CST@2))
394 { tree tem = const_binop (RDIV_EXPR, type, build_one_cst (type), @2); }
396 (rdiv (mult @0 { tem; } ) @1))))
398 /* Convert A/(B/C) to (A/B)*C */
400 (rdiv @0 (rdiv:s @1 @2))
401 (mult (rdiv @0 @1) @2)))
403 /* Simplify x / (- y) to -x / y. */
405 (rdiv @0 (negate @1))
406 (rdiv (negate @0) @1))
408 /* Optimize (X & (-A)) / A where A is a power of 2, to X >> log2(A) */
409 (for div (trunc_div ceil_div floor_div round_div exact_div)
411 (div (convert? (bit_and @0 INTEGER_CST@1)) INTEGER_CST@2)
412 (if (integer_pow2p (@2)
413 && tree_int_cst_sgn (@2) > 0
414 && tree_nop_conversion_p (type, TREE_TYPE (@0))
415 && wi::to_wide (@2) + wi::to_wide (@1) == 0)
417 { build_int_cst (integer_type_node,
418 wi::exact_log2 (wi::to_wide (@2))); }))))
420 /* If ARG1 is a constant, we can convert this to a multiply by the
421 reciprocal. This does not have the same rounding properties,
422 so only do this if -freciprocal-math. We can actually
423 always safely do it if ARG1 is a power of two, but it's hard to
424 tell if it is or not in a portable manner. */
425 (for cst (REAL_CST COMPLEX_CST VECTOR_CST)
429 (if (flag_reciprocal_math
432 { tree tem = const_binop (RDIV_EXPR, type, build_one_cst (type), @1); }
434 (mult @0 { tem; } )))
435 (if (cst != COMPLEX_CST)
436 (with { tree inverse = exact_inverse (type, @1); }
438 (mult @0 { inverse; } ))))))))
440 (for mod (ceil_mod floor_mod round_mod trunc_mod)
441 /* 0 % X is always zero. */
443 (mod integer_zerop@0 @1)
444 /* But not for 0 % 0 so that we can get the proper warnings and errors. */
445 (if (!integer_zerop (@1))
447 /* X % 1 is always zero. */
449 (mod @0 integer_onep)
450 { build_zero_cst (type); })
451 /* X % -1 is zero. */
453 (mod @0 integer_minus_onep@1)
454 (if (!TYPE_UNSIGNED (type))
455 { build_zero_cst (type); }))
459 /* But not for 0 % 0 so that we can get the proper warnings and errors. */
460 (if (!integer_zerop (@0))
461 { build_zero_cst (type); }))
462 /* (X % Y) % Y is just X % Y. */
464 (mod (mod@2 @0 @1) @1)
466 /* From extract_muldiv_1: (X * C1) % C2 is zero if C1 is a multiple of C2. */
468 (mod (mult @0 INTEGER_CST@1) INTEGER_CST@2)
469 (if (ANY_INTEGRAL_TYPE_P (type)
470 && TYPE_OVERFLOW_UNDEFINED (type)
471 && wi::multiple_of_p (wi::to_wide (@1), wi::to_wide (@2),
473 { build_zero_cst (type); }))
474 /* For (X % C) == 0, if X is signed and C is power of 2, use unsigned
475 modulo and comparison, since it is simpler and equivalent. */
478 (cmp (mod @0 integer_pow2p@2) integer_zerop@1)
479 (if (!TYPE_UNSIGNED (TREE_TYPE (@0)))
480 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
481 (cmp (mod (convert:utype @0) (convert:utype @2)) (convert:utype @1)))))))
483 /* X % -C is the same as X % C. */
485 (trunc_mod @0 INTEGER_CST@1)
486 (if (TYPE_SIGN (type) == SIGNED
487 && !TREE_OVERFLOW (@1)
488 && wi::neg_p (wi::to_wide (@1))
489 && !TYPE_OVERFLOW_TRAPS (type)
490 /* Avoid this transformation if C is INT_MIN, i.e. C == -C. */
491 && !sign_bit_p (@1, @1))
492 (trunc_mod @0 (negate @1))))
494 /* X % -Y is the same as X % Y. */
496 (trunc_mod @0 (convert? (negate @1)))
497 (if (INTEGRAL_TYPE_P (type)
498 && !TYPE_UNSIGNED (type)
499 && !TYPE_OVERFLOW_TRAPS (type)
500 && tree_nop_conversion_p (type, TREE_TYPE (@1))
501 /* Avoid this transformation if X might be INT_MIN or
502 Y might be -1, because we would then change valid
503 INT_MIN % -(-1) into invalid INT_MIN % -1. */
504 && (expr_not_equal_to (@0, wi::to_wide (TYPE_MIN_VALUE (type)))
505 || expr_not_equal_to (@1, wi::minus_one (TYPE_PRECISION
507 (trunc_mod @0 (convert @1))))
509 /* X - (X / Y) * Y is the same as X % Y. */
511 (minus (convert1? @0) (convert2? (mult:c (trunc_div @@0 @@1) @1)))
512 (if (INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
513 (convert (trunc_mod @0 @1))))
515 /* Optimize TRUNC_MOD_EXPR by a power of two into a BIT_AND_EXPR,
516 i.e. "X % C" into "X & (C - 1)", if X and C are positive.
517 Also optimize A % (C << N) where C is a power of 2,
518 to A & ((C << N) - 1). */
519 (match (power_of_two_cand @1)
521 (match (power_of_two_cand @1)
522 (lshift INTEGER_CST@1 @2))
523 (for mod (trunc_mod floor_mod)
525 (mod @0 (convert?@3 (power_of_two_cand@1 @2)))
526 (if ((TYPE_UNSIGNED (type)
527 || tree_expr_nonnegative_p (@0))
528 && tree_nop_conversion_p (type, TREE_TYPE (@3))
529 && integer_pow2p (@2) && tree_int_cst_sgn (@2) > 0)
530 (bit_and @0 (convert (minus @1 { build_int_cst (TREE_TYPE (@1), 1); }))))))
532 /* Simplify (unsigned t * 2)/2 -> unsigned t & 0x7FFFFFFF. */
534 (trunc_div (mult @0 integer_pow2p@1) @1)
535 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
536 (bit_and @0 { wide_int_to_tree
537 (type, wi::mask (TYPE_PRECISION (type)
538 - wi::exact_log2 (wi::to_wide (@1)),
539 false, TYPE_PRECISION (type))); })))
541 /* Simplify (unsigned t / 2) * 2 -> unsigned t & ~1. */
543 (mult (trunc_div @0 integer_pow2p@1) @1)
544 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
545 (bit_and @0 (negate @1))))
547 /* Simplify (t * 2) / 2) -> t. */
548 (for div (trunc_div ceil_div floor_div round_div exact_div)
550 (div (mult:c @0 @1) @1)
551 (if (ANY_INTEGRAL_TYPE_P (type)
552 && TYPE_OVERFLOW_UNDEFINED (type))
556 /* Simplify cos(-x) and cos(|x|) -> cos(x). Similarly for cosh. */
561 /* Simplify pow(-x, y) and pow(|x|,y) -> pow(x,y) if y is an even integer. */
564 (pows (op @0) REAL_CST@1)
565 (with { HOST_WIDE_INT n; }
566 (if (real_isinteger (&TREE_REAL_CST (@1), &n) && (n & 1) == 0)
568 /* Likewise for powi. */
571 (pows (op @0) INTEGER_CST@1)
572 (if ((wi::to_wide (@1) & 1) == 0)
574 /* Strip negate and abs from both operands of hypot. */
582 /* copysign(-x, y) and copysign(abs(x), y) -> copysign(x, y). */
583 (for copysigns (COPYSIGN_ALL)
585 (copysigns (op @0) @1)
588 /* abs(x)*abs(x) -> x*x. Should be valid for all types. */
593 /* cos(copysign(x, y)) -> cos(x). Similarly for cosh. */
597 (coss (copysigns @0 @1))
600 /* pow(copysign(x, y), z) -> pow(x, z) if z is an even integer. */
604 (pows (copysigns @0 @2) REAL_CST@1)
605 (with { HOST_WIDE_INT n; }
606 (if (real_isinteger (&TREE_REAL_CST (@1), &n) && (n & 1) == 0)
608 /* Likewise for powi. */
612 (pows (copysigns @0 @2) INTEGER_CST@1)
613 (if ((wi::to_wide (@1) & 1) == 0)
618 /* hypot(copysign(x, y), z) -> hypot(x, z). */
620 (hypots (copysigns @0 @1) @2)
622 /* hypot(x, copysign(y, z)) -> hypot(x, y). */
624 (hypots @0 (copysigns @1 @2))
627 /* copysign(x, CST) -> [-]abs (x). */
628 (for copysigns (COPYSIGN_ALL)
630 (copysigns @0 REAL_CST@1)
631 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
635 /* copysign(copysign(x, y), z) -> copysign(x, z). */
636 (for copysigns (COPYSIGN_ALL)
638 (copysigns (copysigns @0 @1) @2)
641 /* copysign(x,y)*copysign(x,y) -> x*x. */
642 (for copysigns (COPYSIGN_ALL)
644 (mult (copysigns@2 @0 @1) @2)
647 /* ccos(-x) -> ccos(x). Similarly for ccosh. */
648 (for ccoss (CCOS CCOSH)
653 /* cabs(-x) and cos(conj(x)) -> cabs(x). */
654 (for ops (conj negate)
660 /* Fold (a * (1 << b)) into (a << b) */
662 (mult:c @0 (convert? (lshift integer_onep@1 @2)))
663 (if (! FLOAT_TYPE_P (type)
664 && tree_nop_conversion_p (type, TREE_TYPE (@1)))
667 /* Fold (1 << (C - x)) where C = precision(type) - 1
668 into ((1 << C) >> x). */
670 (lshift integer_onep@0 (minus@1 INTEGER_CST@2 @3))
671 (if (INTEGRAL_TYPE_P (type)
672 && wi::eq_p (wi::to_wide (@2), TYPE_PRECISION (type) - 1)
674 (if (TYPE_UNSIGNED (type))
675 (rshift (lshift @0 @2) @3)
677 { tree utype = unsigned_type_for (type); }
678 (convert (rshift (lshift (convert:utype @0) @2) @3))))))
680 /* Fold (C1/X)*C2 into (C1*C2)/X. */
682 (mult (rdiv@3 REAL_CST@0 @1) REAL_CST@2)
683 (if (flag_associative_math
686 { tree tem = const_binop (MULT_EXPR, type, @0, @2); }
688 (rdiv { tem; } @1)))))
690 /* Simplify ~X & X as zero. */
692 (bit_and:c (convert? @0) (convert? (bit_not @0)))
693 { build_zero_cst (type); })
695 /* PR71636: Transform x & ((1U << b) - 1) -> x & ~(~0U << b); */
697 (bit_and:c @0 (plus:s (lshift:s integer_onep @1) integer_minus_onep))
698 (if (TYPE_UNSIGNED (type))
699 (bit_and @0 (bit_not (lshift { build_all_ones_cst (type); } @1)))))
701 (for bitop (bit_and bit_ior)
703 /* PR35691: Transform
704 (x == 0 & y == 0) -> (x | typeof(x)(y)) == 0.
705 (x != 0 | y != 0) -> (x | typeof(x)(y)) != 0. */
707 (bitop (cmp @0 integer_zerop@2) (cmp @1 integer_zerop))
708 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
709 && INTEGRAL_TYPE_P (TREE_TYPE (@1))
710 && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
711 (cmp (bit_ior @0 (convert @1)) @2)))
713 (x == -1 & y == -1) -> (x & typeof(x)(y)) == -1.
714 (x != -1 | y != -1) -> (x & typeof(x)(y)) != -1. */
716 (bitop (cmp @0 integer_all_onesp@2) (cmp @1 integer_all_onesp))
717 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
718 && INTEGRAL_TYPE_P (TREE_TYPE (@1))
719 && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
720 (cmp (bit_and @0 (convert @1)) @2))))
722 /* Fold (A & ~B) - (A & B) into (A ^ B) - B. */
724 (minus (bit_and:cs @0 (bit_not @1)) (bit_and:cs @0 @1))
725 (minus (bit_xor @0 @1) @1))
727 (minus (bit_and:s @0 INTEGER_CST@2) (bit_and:s @0 INTEGER_CST@1))
728 (if (~wi::to_wide (@2) == wi::to_wide (@1))
729 (minus (bit_xor @0 @1) @1)))
731 /* Fold (A & B) - (A & ~B) into B - (A ^ B). */
733 (minus (bit_and:cs @0 @1) (bit_and:cs @0 (bit_not @1)))
734 (minus @1 (bit_xor @0 @1)))
736 /* Simplify (X & ~Y) |^+ (~X & Y) -> X ^ Y. */
737 (for op (bit_ior bit_xor plus)
739 (op (bit_and:c @0 (bit_not @1)) (bit_and:c (bit_not @0) @1))
742 (op:c (bit_and @0 INTEGER_CST@2) (bit_and (bit_not @0) INTEGER_CST@1))
743 (if (~wi::to_wide (@2) == wi::to_wide (@1))
746 /* PR53979: Transform ((a ^ b) | a) -> (a | b) */
748 (bit_ior:c (bit_xor:c @0 @1) @0)
751 /* (a & ~b) | (a ^ b) --> a ^ b */
753 (bit_ior:c (bit_and:c @0 (bit_not @1)) (bit_xor:c@2 @0 @1))
756 /* (a & ~b) ^ ~a --> ~(a & b) */
758 (bit_xor:c (bit_and:cs @0 (bit_not @1)) (bit_not @0))
759 (bit_not (bit_and @0 @1)))
761 /* (a | b) & ~(a ^ b) --> a & b */
763 (bit_and:c (bit_ior @0 @1) (bit_not (bit_xor:c @0 @1)))
766 /* a | ~(a ^ b) --> a | ~b */
768 (bit_ior:c @0 (bit_not:s (bit_xor:c @0 @1)))
769 (bit_ior @0 (bit_not @1)))
771 /* (a | b) | (a &^ b) --> a | b */
772 (for op (bit_and bit_xor)
774 (bit_ior:c (bit_ior@2 @0 @1) (op:c @0 @1))
777 /* (a & b) | ~(a ^ b) --> ~(a ^ b) */
779 (bit_ior:c (bit_and:c @0 @1) (bit_not@2 (bit_xor @0 @1)))
782 /* ~(~a & b) --> a | ~b */
784 (bit_not (bit_and:cs (bit_not @0) @1))
785 (bit_ior @0 (bit_not @1)))
787 /* ~(~a | b) --> a & ~b */
789 (bit_not (bit_ior:cs (bit_not @0) @1))
790 (bit_and @0 (bit_not @1)))
792 /* Simplify (~X & Y) to X ^ Y if we know that (X & ~Y) is 0. */
795 (bit_and (bit_not SSA_NAME@0) INTEGER_CST@1)
796 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
797 && wi::bit_and_not (get_nonzero_bits (@0), wi::to_wide (@1)) == 0)
801 /* For constants M and N, if M == (1LL << cst) - 1 && (N & M) == M,
802 ((A & N) + B) & M -> (A + B) & M
803 Similarly if (N & M) == 0,
804 ((A | N) + B) & M -> (A + B) & M
805 and for - instead of + (or unary - instead of +)
806 and/or ^ instead of |.
807 If B is constant and (B & M) == 0, fold into A & M. */
809 (for bitop (bit_and bit_ior bit_xor)
811 (bit_and (op:s (bitop:s@0 @3 INTEGER_CST@4) @1) INTEGER_CST@2)
814 tree utype = fold_bit_and_mask (TREE_TYPE (@0), @2, op, @0, bitop,
815 @3, @4, @1, ERROR_MARK, NULL_TREE,
818 (convert (bit_and (op (convert:utype { pmop[0]; })
819 (convert:utype { pmop[1]; }))
820 (convert:utype @2))))))
822 (bit_and (op:s @0 (bitop:s@1 @3 INTEGER_CST@4)) INTEGER_CST@2)
825 tree utype = fold_bit_and_mask (TREE_TYPE (@0), @2, op, @0, ERROR_MARK,
826 NULL_TREE, NULL_TREE, @1, bitop, @3,
829 (convert (bit_and (op (convert:utype { pmop[0]; })
830 (convert:utype { pmop[1]; }))
831 (convert:utype @2)))))))
833 (bit_and (op:s @0 @1) INTEGER_CST@2)
836 tree utype = fold_bit_and_mask (TREE_TYPE (@0), @2, op, @0, ERROR_MARK,
837 NULL_TREE, NULL_TREE, @1, ERROR_MARK,
838 NULL_TREE, NULL_TREE, pmop); }
840 (convert (bit_and (op (convert:utype { pmop[0]; })
841 (convert:utype { pmop[1]; }))
842 (convert:utype @2)))))))
843 (for bitop (bit_and bit_ior bit_xor)
845 (bit_and (negate:s (bitop:s@0 @2 INTEGER_CST@3)) INTEGER_CST@1)
848 tree utype = fold_bit_and_mask (TREE_TYPE (@0), @1, NEGATE_EXPR, @0,
849 bitop, @2, @3, NULL_TREE, ERROR_MARK,
850 NULL_TREE, NULL_TREE, pmop); }
852 (convert (bit_and (negate (convert:utype { pmop[0]; }))
853 (convert:utype @1)))))))
855 /* X % Y is smaller than Y. */
858 (cmp (trunc_mod @0 @1) @1)
859 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
860 { constant_boolean_node (cmp == LT_EXPR, type); })))
863 (cmp @1 (trunc_mod @0 @1))
864 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
865 { constant_boolean_node (cmp == GT_EXPR, type); })))
869 (bit_ior @0 integer_all_onesp@1)
874 (bit_ior @0 integer_zerop)
879 (bit_and @0 integer_zerop@1)
885 (for op (bit_ior bit_xor plus)
887 (op:c (convert? @0) (convert? (bit_not @0)))
888 (convert { build_all_ones_cst (TREE_TYPE (@0)); })))
893 { build_zero_cst (type); })
895 /* Canonicalize X ^ ~0 to ~X. */
897 (bit_xor @0 integer_all_onesp@1)
902 (bit_and @0 integer_all_onesp)
905 /* x & x -> x, x | x -> x */
906 (for bitop (bit_and bit_ior)
911 /* x & C -> x if we know that x & ~C == 0. */
914 (bit_and SSA_NAME@0 INTEGER_CST@1)
915 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
916 && wi::bit_and_not (get_nonzero_bits (@0), wi::to_wide (@1)) == 0)
920 /* x + (x & 1) -> (x + 1) & ~1 */
922 (plus:c @0 (bit_and:s @0 integer_onep@1))
923 (bit_and (plus @0 @1) (bit_not @1)))
925 /* x & ~(x & y) -> x & ~y */
926 /* x | ~(x | y) -> x | ~y */
927 (for bitop (bit_and bit_ior)
929 (bitop:c @0 (bit_not (bitop:cs @0 @1)))
930 (bitop @0 (bit_not @1))))
932 /* (~x & y) | ~(x | y) -> ~x */
934 (bit_ior:c (bit_and:c (bit_not@2 @0) @1) (bit_not (bit_ior:c @0 @1)))
937 /* (x | y) ^ (x | ~y) -> ~x */
939 (bit_xor:c (bit_ior:c @0 @1) (bit_ior:c @0 (bit_not @1)))
942 /* (x & y) | ~(x | y) -> ~(x ^ y) */
944 (bit_ior:c (bit_and:s @0 @1) (bit_not:s (bit_ior:s @0 @1)))
945 (bit_not (bit_xor @0 @1)))
947 /* (~x | y) ^ (x ^ y) -> x | ~y */
949 (bit_xor:c (bit_ior:cs (bit_not @0) @1) (bit_xor:s @0 @1))
950 (bit_ior @0 (bit_not @1)))
952 /* (x ^ y) | ~(x | y) -> ~(x & y) */
954 (bit_ior:c (bit_xor:s @0 @1) (bit_not:s (bit_ior:s @0 @1)))
955 (bit_not (bit_and @0 @1)))
957 /* (x | y) & ~x -> y & ~x */
958 /* (x & y) | ~x -> y | ~x */
959 (for bitop (bit_and bit_ior)
960 rbitop (bit_ior bit_and)
962 (bitop:c (rbitop:c @0 @1) (bit_not@2 @0))
965 /* (x & y) ^ (x | y) -> x ^ y */
967 (bit_xor:c (bit_and @0 @1) (bit_ior @0 @1))
970 /* (x ^ y) ^ (x | y) -> x & y */
972 (bit_xor:c (bit_xor @0 @1) (bit_ior @0 @1))
975 /* (x & y) + (x ^ y) -> x | y */
976 /* (x & y) | (x ^ y) -> x | y */
977 /* (x & y) ^ (x ^ y) -> x | y */
978 (for op (plus bit_ior bit_xor)
980 (op:c (bit_and @0 @1) (bit_xor @0 @1))
983 /* (x & y) + (x | y) -> x + y */
985 (plus:c (bit_and @0 @1) (bit_ior @0 @1))
988 /* (x + y) - (x | y) -> x & y */
990 (minus (plus @0 @1) (bit_ior @0 @1))
991 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
992 && !TYPE_SATURATING (type))
995 /* (x + y) - (x & y) -> x | y */
997 (minus (plus @0 @1) (bit_and @0 @1))
998 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
999 && !TYPE_SATURATING (type))
1002 /* (x | y) - (x ^ y) -> x & y */
1004 (minus (bit_ior @0 @1) (bit_xor @0 @1))
1007 /* (x | y) - (x & y) -> x ^ y */
1009 (minus (bit_ior @0 @1) (bit_and @0 @1))
1012 /* (x | y) & ~(x & y) -> x ^ y */
1014 (bit_and:c (bit_ior @0 @1) (bit_not (bit_and @0 @1)))
1017 /* (x | y) & (~x ^ y) -> x & y */
1019 (bit_and:c (bit_ior:c @0 @1) (bit_xor:c @1 (bit_not @0)))
1022 /* (~x | y) & (x | ~y) -> ~(x ^ y) */
1024 (bit_and (bit_ior:cs (bit_not @0) @1) (bit_ior:cs @0 (bit_not @1)))
1025 (bit_not (bit_xor @0 @1)))
1027 /* (~x | y) ^ (x | ~y) -> x ^ y */
1029 (bit_xor (bit_ior:c (bit_not @0) @1) (bit_ior:c @0 (bit_not @1)))
1032 /* ~x & ~y -> ~(x | y)
1033 ~x | ~y -> ~(x & y) */
1034 (for op (bit_and bit_ior)
1035 rop (bit_ior bit_and)
1037 (op (convert1? (bit_not @0)) (convert2? (bit_not @1)))
1038 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1039 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
1040 (bit_not (rop (convert @0) (convert @1))))))
1042 /* If we are XORing or adding two BIT_AND_EXPR's, both of which are and'ing
1043 with a constant, and the two constants have no bits in common,
1044 we should treat this as a BIT_IOR_EXPR since this may produce more
1046 (for op (bit_xor plus)
1048 (op (convert1? (bit_and@4 @0 INTEGER_CST@1))
1049 (convert2? (bit_and@5 @2 INTEGER_CST@3)))
1050 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
1051 && tree_nop_conversion_p (type, TREE_TYPE (@2))
1052 && (wi::to_wide (@1) & wi::to_wide (@3)) == 0)
1053 (bit_ior (convert @4) (convert @5)))))
1055 /* (X | Y) ^ X -> Y & ~ X*/
1057 (bit_xor:c (convert1? (bit_ior:c @@0 @1)) (convert2? @0))
1058 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1059 (convert (bit_and @1 (bit_not @0)))))
1061 /* Convert ~X ^ ~Y to X ^ Y. */
1063 (bit_xor (convert1? (bit_not @0)) (convert2? (bit_not @1)))
1064 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1065 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
1066 (bit_xor (convert @0) (convert @1))))
1068 /* Convert ~X ^ C to X ^ ~C. */
1070 (bit_xor (convert? (bit_not @0)) INTEGER_CST@1)
1071 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1072 (bit_xor (convert @0) (bit_not @1))))
1074 /* Fold (X & Y) ^ Y and (X ^ Y) & Y as ~X & Y. */
1075 (for opo (bit_and bit_xor)
1076 opi (bit_xor bit_and)
1078 (opo:c (opi:cs @0 @1) @1)
1079 (bit_and (bit_not @0) @1)))
1081 /* Given a bit-wise operation CODE applied to ARG0 and ARG1, see if both
1082 operands are another bit-wise operation with a common input. If so,
1083 distribute the bit operations to save an operation and possibly two if
1084 constants are involved. For example, convert
1085 (A | B) & (A | C) into A | (B & C)
1086 Further simplification will occur if B and C are constants. */
1087 (for op (bit_and bit_ior bit_xor)
1088 rop (bit_ior bit_and bit_and)
1090 (op (convert? (rop:c @@0 @1)) (convert? (rop:c @0 @2)))
1091 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1092 && tree_nop_conversion_p (type, TREE_TYPE (@2)))
1093 (rop (convert @0) (op (convert @1) (convert @2))))))
1095 /* Some simple reassociation for bit operations, also handled in reassoc. */
1096 /* (X & Y) & Y -> X & Y
1097 (X | Y) | Y -> X | Y */
1098 (for op (bit_and bit_ior)
1100 (op:c (convert1?@2 (op:c @0 @@1)) (convert2? @1))
1102 /* (X ^ Y) ^ Y -> X */
1104 (bit_xor:c (convert1? (bit_xor:c @0 @@1)) (convert2? @1))
1106 /* (X & Y) & (X & Z) -> (X & Y) & Z
1107 (X | Y) | (X | Z) -> (X | Y) | Z */
1108 (for op (bit_and bit_ior)
1110 (op (convert1?@3 (op:c@4 @0 @1)) (convert2?@5 (op:c@6 @0 @2)))
1111 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1112 && tree_nop_conversion_p (type, TREE_TYPE (@2)))
1113 (if (single_use (@5) && single_use (@6))
1114 (op @3 (convert @2))
1115 (if (single_use (@3) && single_use (@4))
1116 (op (convert @1) @5))))))
1117 /* (X ^ Y) ^ (X ^ Z) -> Y ^ Z */
1119 (bit_xor (convert1? (bit_xor:c @0 @1)) (convert2? (bit_xor:c @0 @2)))
1120 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1121 && tree_nop_conversion_p (type, TREE_TYPE (@2)))
1122 (bit_xor (convert @1) (convert @2))))
1131 (abs tree_expr_nonnegative_p@0)
1134 /* A few cases of fold-const.c negate_expr_p predicate. */
1135 (match negate_expr_p
1137 (if ((INTEGRAL_TYPE_P (type)
1138 && TYPE_UNSIGNED (type))
1139 || (!TYPE_OVERFLOW_SANITIZED (type)
1140 && may_negate_without_overflow_p (t)))))
1141 (match negate_expr_p
1143 (match negate_expr_p
1145 (if (!TYPE_OVERFLOW_SANITIZED (type))))
1146 (match negate_expr_p
1148 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (t)))))
1149 /* VECTOR_CST handling of non-wrapping types would recurse in unsupported
1151 (match negate_expr_p
1153 (if (FLOAT_TYPE_P (TREE_TYPE (type)) || TYPE_OVERFLOW_WRAPS (type))))
1154 (match negate_expr_p
1156 (if ((ANY_INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_WRAPS (type))
1157 || (FLOAT_TYPE_P (type)
1158 && !HONOR_SIGN_DEPENDENT_ROUNDING (type)
1159 && !HONOR_SIGNED_ZEROS (type)))))
1161 /* (-A) * (-B) -> A * B */
1163 (mult:c (convert1? (negate @0)) (convert2? negate_expr_p@1))
1164 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
1165 && tree_nop_conversion_p (type, TREE_TYPE (@1)))
1166 (mult (convert @0) (convert (negate @1)))))
1168 /* -(A + B) -> (-B) - A. */
1170 (negate (plus:c @0 negate_expr_p@1))
1171 (if (!HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type))
1172 && !HONOR_SIGNED_ZEROS (element_mode (type)))
1173 (minus (negate @1) @0)))
1175 /* -(A - B) -> B - A. */
1177 (negate (minus @0 @1))
1178 (if ((ANY_INTEGRAL_TYPE_P (type) && !TYPE_OVERFLOW_SANITIZED (type))
1179 || (FLOAT_TYPE_P (type)
1180 && !HONOR_SIGN_DEPENDENT_ROUNDING (type)
1181 && !HONOR_SIGNED_ZEROS (type)))
1184 (negate (pointer_diff @0 @1))
1185 (if (TYPE_OVERFLOW_UNDEFINED (type))
1186 (pointer_diff @1 @0)))
1188 /* A - B -> A + (-B) if B is easily negatable. */
1190 (minus @0 negate_expr_p@1)
1191 (if (!FIXED_POINT_TYPE_P (type))
1192 (plus @0 (negate @1))))
1194 /* Try to fold (type) X op CST -> (type) (X op ((type-x) CST))
1196 For bitwise binary operations apply operand conversions to the
1197 binary operation result instead of to the operands. This allows
1198 to combine successive conversions and bitwise binary operations.
1199 We combine the above two cases by using a conditional convert. */
1200 (for bitop (bit_and bit_ior bit_xor)
1202 (bitop (convert @0) (convert? @1))
1203 (if (((TREE_CODE (@1) == INTEGER_CST
1204 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
1205 && int_fits_type_p (@1, TREE_TYPE (@0)))
1206 || types_match (@0, @1))
1207 /* ??? This transform conflicts with fold-const.c doing
1208 Convert (T)(x & c) into (T)x & (T)c, if c is an integer
1209 constants (if x has signed type, the sign bit cannot be set
1210 in c). This folds extension into the BIT_AND_EXPR.
1211 Restrict it to GIMPLE to avoid endless recursions. */
1212 && (bitop != BIT_AND_EXPR || GIMPLE)
1213 && (/* That's a good idea if the conversion widens the operand, thus
1214 after hoisting the conversion the operation will be narrower. */
1215 TYPE_PRECISION (TREE_TYPE (@0)) < TYPE_PRECISION (type)
1216 /* It's also a good idea if the conversion is to a non-integer
1218 || GET_MODE_CLASS (TYPE_MODE (type)) != MODE_INT
1219 /* Or if the precision of TO is not the same as the precision
1221 || !type_has_mode_precision_p (type)))
1222 (convert (bitop @0 (convert @1))))))
1224 (for bitop (bit_and bit_ior)
1225 rbitop (bit_ior bit_and)
1226 /* (x | y) & x -> x */
1227 /* (x & y) | x -> x */
1229 (bitop:c (rbitop:c @0 @1) @0)
1231 /* (~x | y) & x -> x & y */
1232 /* (~x & y) | x -> x | y */
1234 (bitop:c (rbitop:c (bit_not @0) @1) @0)
1237 /* (x | CST1) & CST2 -> (x & CST2) | (CST1 & CST2) */
1239 (bit_and (bit_ior @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
1240 (bit_ior (bit_and @0 @2) (bit_and @1 @2)))
1242 /* Combine successive equal operations with constants. */
1243 (for bitop (bit_and bit_ior bit_xor)
1245 (bitop (bitop @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
1246 (if (!CONSTANT_CLASS_P (@0))
1247 /* This is the canonical form regardless of whether (bitop @1 @2) can be
1248 folded to a constant. */
1249 (bitop @0 (bitop @1 @2))
1250 /* In this case we have three constants and (bitop @0 @1) doesn't fold
1251 to a constant. This can happen if @0 or @1 is a POLY_INT_CST and if
1252 the values involved are such that the operation can't be decided at
1253 compile time. Try folding one of @0 or @1 with @2 to see whether
1254 that combination can be decided at compile time.
1256 Keep the existing form if both folds fail, to avoid endless
1258 (with { tree cst1 = const_binop (bitop, type, @0, @2); }
1260 (bitop @1 { cst1; })
1261 (with { tree cst2 = const_binop (bitop, type, @1, @2); }
1263 (bitop @0 { cst2; }))))))))
1265 /* Try simple folding for X op !X, and X op X with the help
1266 of the truth_valued_p and logical_inverted_value predicates. */
1267 (match truth_valued_p
1269 (if (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1)))
1270 (for op (tcc_comparison truth_and truth_andif truth_or truth_orif truth_xor)
1271 (match truth_valued_p
1273 (match truth_valued_p
1276 (match (logical_inverted_value @0)
1278 (match (logical_inverted_value @0)
1279 (bit_not truth_valued_p@0))
1280 (match (logical_inverted_value @0)
1281 (eq @0 integer_zerop))
1282 (match (logical_inverted_value @0)
1283 (ne truth_valued_p@0 integer_truep))
1284 (match (logical_inverted_value @0)
1285 (bit_xor truth_valued_p@0 integer_truep))
1289 (bit_and:c @0 (logical_inverted_value @0))
1290 { build_zero_cst (type); })
1291 /* X | !X and X ^ !X -> 1, , if X is truth-valued. */
1292 (for op (bit_ior bit_xor)
1294 (op:c truth_valued_p@0 (logical_inverted_value @0))
1295 { constant_boolean_node (true, type); }))
1296 /* X ==/!= !X is false/true. */
1299 (op:c truth_valued_p@0 (logical_inverted_value @0))
1300 { constant_boolean_node (op == NE_EXPR ? true : false, type); }))
1304 (bit_not (bit_not @0))
1307 /* Convert ~ (-A) to A - 1. */
1309 (bit_not (convert? (negate @0)))
1310 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1311 || !TYPE_UNSIGNED (TREE_TYPE (@0)))
1312 (convert (minus @0 { build_each_one_cst (TREE_TYPE (@0)); }))))
1314 /* Convert - (~A) to A + 1. */
1316 (negate (nop_convert (bit_not @0)))
1317 (plus (view_convert @0) { build_each_one_cst (type); }))
1319 /* Convert ~ (A - 1) or ~ (A + -1) to -A. */
1321 (bit_not (convert? (minus @0 integer_each_onep)))
1322 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1323 || !TYPE_UNSIGNED (TREE_TYPE (@0)))
1324 (convert (negate @0))))
1326 (bit_not (convert? (plus @0 integer_all_onesp)))
1327 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1328 || !TYPE_UNSIGNED (TREE_TYPE (@0)))
1329 (convert (negate @0))))
1331 /* Part of convert ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify. */
1333 (bit_not (convert? (bit_xor @0 INTEGER_CST@1)))
1334 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1335 (convert (bit_xor @0 (bit_not @1)))))
1337 (bit_not (convert? (bit_xor:c (bit_not @0) @1)))
1338 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1339 (convert (bit_xor @0 @1))))
1341 /* Otherwise prefer ~(X ^ Y) to ~X ^ Y as more canonical. */
1343 (bit_xor:c (nop_convert:s (bit_not:s @0)) @1)
1344 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1345 (bit_not (bit_xor (view_convert @0) @1))))
1347 /* (x & ~m) | (y & m) -> ((x ^ y) & m) ^ x */
1349 (bit_ior:c (bit_and:cs @0 (bit_not @2)) (bit_and:cs @1 @2))
1350 (bit_xor (bit_and (bit_xor @0 @1) @2) @0))
1352 /* Fold A - (A & B) into ~B & A. */
1354 (minus (convert1? @0) (convert2?:s (bit_and:cs @@0 @1)))
1355 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
1356 && tree_nop_conversion_p (type, TREE_TYPE (@1)))
1357 (convert (bit_and (bit_not @1) @0))))
1359 /* (m1 CMP m2) * d -> (m1 CMP m2) ? d : 0 */
1360 (for cmp (gt lt ge le)
1362 (mult (convert (cmp @0 @1)) @2)
1363 (cond (cmp @0 @1) @2 { build_zero_cst (type); })))
1365 /* For integral types with undefined overflow and C != 0 fold
1366 x * C EQ/NE y * C into x EQ/NE y. */
1369 (cmp (mult:c @0 @1) (mult:c @2 @1))
1370 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
1371 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1372 && tree_expr_nonzero_p (@1))
1375 /* For integral types with wrapping overflow and C odd fold
1376 x * C EQ/NE y * C into x EQ/NE y. */
1379 (cmp (mult @0 INTEGER_CST@1) (mult @2 @1))
1380 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
1381 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))
1382 && (TREE_INT_CST_LOW (@1) & 1) != 0)
1385 /* For integral types with undefined overflow and C != 0 fold
1386 x * C RELOP y * C into:
1388 x RELOP y for nonnegative C
1389 y RELOP x for negative C */
1390 (for cmp (lt gt le ge)
1392 (cmp (mult:c @0 @1) (mult:c @2 @1))
1393 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
1394 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1395 (if (tree_expr_nonnegative_p (@1) && tree_expr_nonzero_p (@1))
1397 (if (TREE_CODE (@1) == INTEGER_CST
1398 && wi::neg_p (wi::to_wide (@1), TYPE_SIGN (TREE_TYPE (@1))))
1401 /* (X - 1U) <= INT_MAX-1U into (int) X > 0. */
1405 (cmp (plus @0 integer_minus_onep@1) INTEGER_CST@2)
1406 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1407 && TYPE_UNSIGNED (TREE_TYPE (@0))
1408 && TYPE_PRECISION (TREE_TYPE (@0)) > 1
1409 && (wi::to_wide (@2)
1410 == wi::max_value (TYPE_PRECISION (TREE_TYPE (@0)), SIGNED) - 1))
1411 (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
1412 (icmp (convert:stype @0) { build_int_cst (stype, 0); })))))
1414 /* X / 4 < Y / 4 iff X < Y when the division is known to be exact. */
1415 (for cmp (simple_comparison)
1417 (cmp (exact_div @0 INTEGER_CST@2) (exact_div @1 @2))
1418 (if (wi::gt_p (wi::to_wide (@2), 0, TYPE_SIGN (TREE_TYPE (@2))))
1421 /* X / C1 op C2 into a simple range test. */
1422 (for cmp (simple_comparison)
1424 (cmp (trunc_div:s @0 INTEGER_CST@1) INTEGER_CST@2)
1425 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1426 && integer_nonzerop (@1)
1427 && !TREE_OVERFLOW (@1)
1428 && !TREE_OVERFLOW (@2))
1429 (with { tree lo, hi; bool neg_overflow;
1430 enum tree_code code = fold_div_compare (cmp, @1, @2, &lo, &hi,
1433 (if (code == LT_EXPR || code == GE_EXPR)
1434 (if (TREE_OVERFLOW (lo))
1435 { build_int_cst (type, (code == LT_EXPR) ^ neg_overflow); }
1436 (if (code == LT_EXPR)
1439 (if (code == LE_EXPR || code == GT_EXPR)
1440 (if (TREE_OVERFLOW (hi))
1441 { build_int_cst (type, (code == LE_EXPR) ^ neg_overflow); }
1442 (if (code == LE_EXPR)
1446 { build_int_cst (type, code == NE_EXPR); })
1447 (if (code == EQ_EXPR && !hi)
1449 (if (code == EQ_EXPR && !lo)
1451 (if (code == NE_EXPR && !hi)
1453 (if (code == NE_EXPR && !lo)
1456 { build_range_check (UNKNOWN_LOCATION, type, @0, code == EQ_EXPR,
1460 tree etype = range_check_type (TREE_TYPE (@0));
1463 if (! TYPE_UNSIGNED (etype))
1464 etype = unsigned_type_for (etype);
1465 hi = fold_convert (etype, hi);
1466 lo = fold_convert (etype, lo);
1467 hi = const_binop (MINUS_EXPR, etype, hi, lo);
1470 (if (etype && hi && !TREE_OVERFLOW (hi))
1471 (if (code == EQ_EXPR)
1472 (le (minus (convert:etype @0) { lo; }) { hi; })
1473 (gt (minus (convert:etype @0) { lo; }) { hi; })))))))))
1475 /* X + Z < Y + Z is the same as X < Y when there is no overflow. */
1476 (for op (lt le ge gt)
1478 (op (plus:c @0 @2) (plus:c @1 @2))
1479 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1480 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1482 /* For equality and subtraction, this is also true with wrapping overflow. */
1483 (for op (eq ne minus)
1485 (op (plus:c @0 @2) (plus:c @1 @2))
1486 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1487 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1488 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
1491 /* X - Z < Y - Z is the same as X < Y when there is no overflow. */
1492 (for op (lt le ge gt)
1494 (op (minus @0 @2) (minus @1 @2))
1495 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1496 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1498 /* For equality and subtraction, this is also true with wrapping overflow. */
1499 (for op (eq ne minus)
1501 (op (minus @0 @2) (minus @1 @2))
1502 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1503 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1504 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
1506 /* And for pointers... */
1507 (for op (simple_comparison)
1509 (op (pointer_diff@3 @0 @2) (pointer_diff @1 @2))
1510 (if (!TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
1513 (minus (pointer_diff@3 @0 @2) (pointer_diff @1 @2))
1514 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@3))
1515 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
1516 (pointer_diff @0 @1)))
1518 /* Z - X < Z - Y is the same as Y < X when there is no overflow. */
1519 (for op (lt le ge gt)
1521 (op (minus @2 @0) (minus @2 @1))
1522 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1523 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1525 /* For equality and subtraction, this is also true with wrapping overflow. */
1526 (for op (eq ne minus)
1528 (op (minus @2 @0) (minus @2 @1))
1529 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1530 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1531 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
1533 /* And for pointers... */
1534 (for op (simple_comparison)
1536 (op (pointer_diff@3 @2 @0) (pointer_diff @2 @1))
1537 (if (!TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
1540 (minus (pointer_diff@3 @2 @0) (pointer_diff @2 @1))
1541 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@3))
1542 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
1543 (pointer_diff @1 @0)))
1545 /* X + Y < Y is the same as X < 0 when there is no overflow. */
1546 (for op (lt le gt ge)
1548 (op:c (plus:c@2 @0 @1) @1)
1549 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1550 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1551 && (CONSTANT_CLASS_P (@0) || single_use (@2)))
1552 (op @0 { build_zero_cst (TREE_TYPE (@0)); }))))
1553 /* For equality, this is also true with wrapping overflow. */
1556 (op:c (nop_convert@3 (plus:c@2 @0 (convert1? @1))) (convert2? @1))
1557 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1558 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1559 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
1560 && (CONSTANT_CLASS_P (@0) || (single_use (@2) && single_use (@3)))
1561 && tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@2))
1562 && tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@1)))
1563 (op @0 { build_zero_cst (TREE_TYPE (@0)); })))
1565 (op:c (nop_convert@3 (pointer_plus@2 (convert1? @0) @1)) (convert2? @0))
1566 (if (tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@0))
1567 && tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0))
1568 && (CONSTANT_CLASS_P (@1) || (single_use (@2) && single_use (@3))))
1569 (op @1 { build_zero_cst (TREE_TYPE (@1)); }))))
1571 /* X - Y < X is the same as Y > 0 when there is no overflow.
1572 For equality, this is also true with wrapping overflow. */
1573 (for op (simple_comparison)
1575 (op:c @0 (minus@2 @0 @1))
1576 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1577 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1578 || ((op == EQ_EXPR || op == NE_EXPR)
1579 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
1580 && (CONSTANT_CLASS_P (@1) || single_use (@2)))
1581 (op @1 { build_zero_cst (TREE_TYPE (@1)); }))))
1584 (X / Y) == 0 -> X < Y if X, Y are unsigned.
1585 (X / Y) != 0 -> X >= Y, if X, Y are unsigned. */
1589 (cmp (trunc_div @0 @1) integer_zerop)
1590 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
1591 /* Complex ==/!= is allowed, but not </>=. */
1592 && TREE_CODE (TREE_TYPE (@0)) != COMPLEX_TYPE
1593 && (VECTOR_TYPE_P (type) || !VECTOR_TYPE_P (TREE_TYPE (@0))))
1596 /* X == C - X can never be true if C is odd. */
1599 (cmp:c (convert? @0) (convert1? (minus INTEGER_CST@1 (convert2? @0))))
1600 (if (TREE_INT_CST_LOW (@1) & 1)
1601 { constant_boolean_node (cmp == NE_EXPR, type); })))
1603 /* Arguments on which one can call get_nonzero_bits to get the bits
1605 (match with_possible_nonzero_bits
1607 (match with_possible_nonzero_bits
1609 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))))
1610 /* Slightly extended version, do not make it recursive to keep it cheap. */
1611 (match (with_possible_nonzero_bits2 @0)
1612 with_possible_nonzero_bits@0)
1613 (match (with_possible_nonzero_bits2 @0)
1614 (bit_and:c with_possible_nonzero_bits@0 @2))
1616 /* Same for bits that are known to be set, but we do not have
1617 an equivalent to get_nonzero_bits yet. */
1618 (match (with_certain_nonzero_bits2 @0)
1620 (match (with_certain_nonzero_bits2 @0)
1621 (bit_ior @1 INTEGER_CST@0))
1623 /* X == C (or X & Z == Y | C) is impossible if ~nonzero(X) & C != 0. */
1626 (cmp:c (with_possible_nonzero_bits2 @0) (with_certain_nonzero_bits2 @1))
1627 (if (wi::bit_and_not (wi::to_wide (@1), get_nonzero_bits (@0)) != 0)
1628 { constant_boolean_node (cmp == NE_EXPR, type); })))
1630 /* ((X inner_op C0) outer_op C1)
1631 With X being a tree where value_range has reasoned certain bits to always be
1632 zero throughout its computed value range,
1633 inner_op = {|,^}, outer_op = {|,^} and inner_op != outer_op
1634 where zero_mask has 1's for all bits that are sure to be 0 in
1636 if (inner_op == '^') C0 &= ~C1;
1637 if ((C0 & ~zero_mask) == 0) then emit (X outer_op (C0 outer_op C1)
1638 if ((C1 & ~zero_mask) == 0) then emit (X inner_op (C0 outer_op C1)
1640 (for inner_op (bit_ior bit_xor)
1641 outer_op (bit_xor bit_ior)
1644 (inner_op:s @2 INTEGER_CST@0) INTEGER_CST@1)
1648 wide_int zero_mask_not;
1652 if (TREE_CODE (@2) == SSA_NAME)
1653 zero_mask_not = get_nonzero_bits (@2);
1657 if (inner_op == BIT_XOR_EXPR)
1659 C0 = wi::bit_and_not (wi::to_wide (@0), wi::to_wide (@1));
1660 cst_emit = C0 | wi::to_wide (@1);
1664 C0 = wi::to_wide (@0);
1665 cst_emit = C0 ^ wi::to_wide (@1);
1668 (if (!fail && (C0 & zero_mask_not) == 0)
1669 (outer_op @2 { wide_int_to_tree (type, cst_emit); })
1670 (if (!fail && (wi::to_wide (@1) & zero_mask_not) == 0)
1671 (inner_op @2 { wide_int_to_tree (type, cst_emit); }))))))
1673 /* Associate (p +p off1) +p off2 as (p +p (off1 + off2)). */
1675 (pointer_plus (pointer_plus:s @0 @1) @3)
1676 (pointer_plus @0 (plus @1 @3)))
1682 tem4 = (unsigned long) tem3;
1687 (pointer_plus @0 (convert?@2 (minus@3 (convert @1) (convert @0))))
1688 /* Conditionally look through a sign-changing conversion. */
1689 (if (TYPE_PRECISION (TREE_TYPE (@2)) == TYPE_PRECISION (TREE_TYPE (@3))
1690 && ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@1)))
1691 || (GENERIC && type == TREE_TYPE (@1))))
1694 (pointer_plus @0 (convert?@2 (pointer_diff@3 @1 @@0)))
1695 (if (TYPE_PRECISION (TREE_TYPE (@2)) >= TYPE_PRECISION (TREE_TYPE (@3)))
1699 tem = (sizetype) ptr;
1703 and produce the simpler and easier to analyze with respect to alignment
1704 ... = ptr & ~algn; */
1706 (pointer_plus @0 (negate (bit_and (convert @0) INTEGER_CST@1)))
1707 (with { tree algn = wide_int_to_tree (TREE_TYPE (@0), ~wi::to_wide (@1)); }
1708 (bit_and @0 { algn; })))
1710 /* Try folding difference of addresses. */
1712 (minus (convert ADDR_EXPR@0) (convert @1))
1713 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1714 (with { poly_int64 diff; }
1715 (if (ptr_difference_const (@0, @1, &diff))
1716 { build_int_cst_type (type, diff); }))))
1718 (minus (convert @0) (convert ADDR_EXPR@1))
1719 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1720 (with { poly_int64 diff; }
1721 (if (ptr_difference_const (@0, @1, &diff))
1722 { build_int_cst_type (type, diff); }))))
1724 (pointer_diff (convert?@2 ADDR_EXPR@0) (convert1?@3 @1))
1725 (if (tree_nop_conversion_p (TREE_TYPE(@2), TREE_TYPE (@0))
1726 && tree_nop_conversion_p (TREE_TYPE(@3), TREE_TYPE (@1)))
1727 (with { poly_int64 diff; }
1728 (if (ptr_difference_const (@0, @1, &diff))
1729 { build_int_cst_type (type, diff); }))))
1731 (pointer_diff (convert?@2 @0) (convert1?@3 ADDR_EXPR@1))
1732 (if (tree_nop_conversion_p (TREE_TYPE(@2), TREE_TYPE (@0))
1733 && tree_nop_conversion_p (TREE_TYPE(@3), TREE_TYPE (@1)))
1734 (with { poly_int64 diff; }
1735 (if (ptr_difference_const (@0, @1, &diff))
1736 { build_int_cst_type (type, diff); }))))
1738 /* If arg0 is derived from the address of an object or function, we may
1739 be able to fold this expression using the object or function's
1742 (bit_and (convert? @0) INTEGER_CST@1)
1743 (if (POINTER_TYPE_P (TREE_TYPE (@0))
1744 && tree_nop_conversion_p (type, TREE_TYPE (@0)))
1748 unsigned HOST_WIDE_INT bitpos;
1749 get_pointer_alignment_1 (@0, &align, &bitpos);
1751 (if (wi::ltu_p (wi::to_wide (@1), align / BITS_PER_UNIT))
1752 { wide_int_to_tree (type, (wi::to_wide (@1)
1753 & (bitpos / BITS_PER_UNIT))); }))))
1756 /* We can't reassociate at all for saturating types. */
1757 (if (!TYPE_SATURATING (type))
1759 /* Contract negates. */
1760 /* A + (-B) -> A - B */
1762 (plus:c @0 (convert? (negate @1)))
1763 /* Apply STRIP_NOPS on the negate. */
1764 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1765 && !TYPE_OVERFLOW_SANITIZED (type))
1769 if (INTEGRAL_TYPE_P (type)
1770 && TYPE_OVERFLOW_WRAPS (type) != TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
1771 t1 = TYPE_OVERFLOW_WRAPS (type) ? type : TREE_TYPE (@1);
1773 (convert (minus (convert:t1 @0) (convert:t1 @1))))))
1774 /* A - (-B) -> A + B */
1776 (minus @0 (convert? (negate @1)))
1777 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1778 && !TYPE_OVERFLOW_SANITIZED (type))
1782 if (INTEGRAL_TYPE_P (type)
1783 && TYPE_OVERFLOW_WRAPS (type) != TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
1784 t1 = TYPE_OVERFLOW_WRAPS (type) ? type : TREE_TYPE (@1);
1786 (convert (plus (convert:t1 @0) (convert:t1 @1))))))
1788 Sign-extension is ok except for INT_MIN, which thankfully cannot
1789 happen without overflow. */
1791 (negate (convert (negate @1)))
1792 (if (INTEGRAL_TYPE_P (type)
1793 && (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@1))
1794 || (!TYPE_UNSIGNED (TREE_TYPE (@1))
1795 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))))
1796 && !TYPE_OVERFLOW_SANITIZED (type)
1797 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@1)))
1800 (negate (convert negate_expr_p@1))
1801 (if (SCALAR_FLOAT_TYPE_P (type)
1802 && ((DECIMAL_FLOAT_TYPE_P (type)
1803 == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1))
1804 && TYPE_PRECISION (type) >= TYPE_PRECISION (TREE_TYPE (@1)))
1805 || !HONOR_SIGN_DEPENDENT_ROUNDING (type)))
1806 (convert (negate @1))))
1808 (negate (nop_convert (negate @1)))
1809 (if (!TYPE_OVERFLOW_SANITIZED (type)
1810 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@1)))
1813 /* We can't reassociate floating-point unless -fassociative-math
1814 or fixed-point plus or minus because of saturation to +-Inf. */
1815 (if ((!FLOAT_TYPE_P (type) || flag_associative_math)
1816 && !FIXED_POINT_TYPE_P (type))
1818 /* Match patterns that allow contracting a plus-minus pair
1819 irrespective of overflow issues. */
1820 /* (A +- B) - A -> +- B */
1821 /* (A +- B) -+ B -> A */
1822 /* A - (A +- B) -> -+ B */
1823 /* A +- (B -+ A) -> +- B */
1825 (minus (plus:c @0 @1) @0)
1828 (minus (minus @0 @1) @0)
1831 (plus:c (minus @0 @1) @1)
1834 (minus @0 (plus:c @0 @1))
1837 (minus @0 (minus @0 @1))
1839 /* (A +- B) + (C - A) -> C +- B */
1840 /* (A + B) - (A - C) -> B + C */
1841 /* More cases are handled with comparisons. */
1843 (plus:c (plus:c @0 @1) (minus @2 @0))
1846 (plus:c (minus @0 @1) (minus @2 @0))
1849 (plus:c (pointer_diff @0 @1) (pointer_diff @2 @0))
1850 (if (TYPE_OVERFLOW_UNDEFINED (type)
1851 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0)))
1852 (pointer_diff @2 @1)))
1854 (minus (plus:c @0 @1) (minus @0 @2))
1857 /* (A +- CST1) +- CST2 -> A + CST3
1858 Use view_convert because it is safe for vectors and equivalent for
1860 (for outer_op (plus minus)
1861 (for inner_op (plus minus)
1862 neg_inner_op (minus plus)
1864 (outer_op (nop_convert (inner_op @0 CONSTANT_CLASS_P@1))
1866 /* If one of the types wraps, use that one. */
1867 (if (!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_WRAPS (type))
1868 /* If all 3 captures are CONSTANT_CLASS_P, punt, as we might recurse
1869 forever if something doesn't simplify into a constant. */
1870 (if (!CONSTANT_CLASS_P (@0))
1871 (if (outer_op == PLUS_EXPR)
1872 (plus (view_convert @0) (inner_op @2 (view_convert @1)))
1873 (minus (view_convert @0) (neg_inner_op @2 (view_convert @1)))))
1874 (if (!ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1875 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
1876 (if (outer_op == PLUS_EXPR)
1877 (view_convert (plus @0 (inner_op (view_convert @2) @1)))
1878 (view_convert (minus @0 (neg_inner_op (view_convert @2) @1))))
1879 /* If the constant operation overflows we cannot do the transform
1880 directly as we would introduce undefined overflow, for example
1881 with (a - 1) + INT_MIN. */
1882 (if (types_match (type, @0))
1883 (with { tree cst = const_binop (outer_op == inner_op
1884 ? PLUS_EXPR : MINUS_EXPR,
1886 (if (cst && !TREE_OVERFLOW (cst))
1887 (inner_op @0 { cst; } )
1888 /* X+INT_MAX+1 is X-INT_MIN. */
1889 (if (INTEGRAL_TYPE_P (type) && cst
1890 && wi::to_wide (cst) == wi::min_value (type))
1891 (neg_inner_op @0 { wide_int_to_tree (type, wi::to_wide (cst)); })
1892 /* Last resort, use some unsigned type. */
1893 (with { tree utype = unsigned_type_for (type); }
1895 (view_convert (inner_op
1896 (view_convert:utype @0)
1898 { drop_tree_overflow (cst); }))))))))))))))
1900 /* (CST1 - A) +- CST2 -> CST3 - A */
1901 (for outer_op (plus minus)
1903 (outer_op (minus CONSTANT_CLASS_P@1 @0) CONSTANT_CLASS_P@2)
1904 (with { tree cst = const_binop (outer_op, type, @1, @2); }
1905 (if (cst && !TREE_OVERFLOW (cst))
1906 (minus { cst; } @0)))))
1908 /* CST1 - (CST2 - A) -> CST3 + A */
1910 (minus CONSTANT_CLASS_P@1 (minus CONSTANT_CLASS_P@2 @0))
1911 (with { tree cst = const_binop (MINUS_EXPR, type, @1, @2); }
1912 (if (cst && !TREE_OVERFLOW (cst))
1913 (plus { cst; } @0))))
1917 (plus:c (bit_not @0) @0)
1918 (if (!TYPE_OVERFLOW_TRAPS (type))
1919 { build_all_ones_cst (type); }))
1923 (plus (convert? (bit_not @0)) integer_each_onep)
1924 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1925 (negate (convert @0))))
1929 (minus (convert? (negate @0)) integer_each_onep)
1930 (if (!TYPE_OVERFLOW_TRAPS (type)
1931 && tree_nop_conversion_p (type, TREE_TYPE (@0)))
1932 (bit_not (convert @0))))
1936 (minus integer_all_onesp @0)
1939 /* (T)(P + A) - (T)P -> (T) A */
1941 (minus (convert (plus:c @@0 @1))
1943 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
1944 /* For integer types, if A has a smaller type
1945 than T the result depends on the possible
1947 E.g. T=size_t, A=(unsigned)429497295, P>0.
1948 However, if an overflow in P + A would cause
1949 undefined behavior, we can assume that there
1951 || (INTEGRAL_TYPE_P (TREE_TYPE (@1))
1952 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))))
1955 (minus (convert (pointer_plus @@0 @1))
1957 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
1958 /* For pointer types, if the conversion of A to the
1959 final type requires a sign- or zero-extension,
1960 then we have to punt - it is not defined which
1962 || (POINTER_TYPE_P (TREE_TYPE (@0))
1963 && TREE_CODE (@1) == INTEGER_CST
1964 && tree_int_cst_sign_bit (@1) == 0))
1967 (pointer_diff (pointer_plus @@0 @1) @0)
1968 /* The second argument of pointer_plus must be interpreted as signed, and
1969 thus sign-extended if necessary. */
1970 (with { tree stype = signed_type_for (TREE_TYPE (@1)); }
1971 /* Use view_convert instead of convert here, as POINTER_PLUS_EXPR
1972 second arg is unsigned even when we need to consider it as signed,
1973 we don't want to diagnose overflow here. */
1974 (convert (view_convert:stype @1))))
1976 /* (T)P - (T)(P + A) -> -(T) A */
1978 (minus (convert? @0)
1979 (convert (plus:c @@0 @1)))
1980 (if (INTEGRAL_TYPE_P (type)
1981 && TYPE_OVERFLOW_UNDEFINED (type)
1982 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
1983 (with { tree utype = unsigned_type_for (type); }
1984 (convert (negate (convert:utype @1))))
1985 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
1986 /* For integer types, if A has a smaller type
1987 than T the result depends on the possible
1989 E.g. T=size_t, A=(unsigned)429497295, P>0.
1990 However, if an overflow in P + A would cause
1991 undefined behavior, we can assume that there
1993 || (INTEGRAL_TYPE_P (TREE_TYPE (@1))
1994 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))))
1995 (negate (convert @1)))))
1998 (convert (pointer_plus @@0 @1)))
1999 (if (INTEGRAL_TYPE_P (type)
2000 && TYPE_OVERFLOW_UNDEFINED (type)
2001 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
2002 (with { tree utype = unsigned_type_for (type); }
2003 (convert (negate (convert:utype @1))))
2004 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
2005 /* For pointer types, if the conversion of A to the
2006 final type requires a sign- or zero-extension,
2007 then we have to punt - it is not defined which
2009 || (POINTER_TYPE_P (TREE_TYPE (@0))
2010 && TREE_CODE (@1) == INTEGER_CST
2011 && tree_int_cst_sign_bit (@1) == 0))
2012 (negate (convert @1)))))
2014 (pointer_diff @0 (pointer_plus @@0 @1))
2015 /* The second argument of pointer_plus must be interpreted as signed, and
2016 thus sign-extended if necessary. */
2017 (with { tree stype = signed_type_for (TREE_TYPE (@1)); }
2018 /* Use view_convert instead of convert here, as POINTER_PLUS_EXPR
2019 second arg is unsigned even when we need to consider it as signed,
2020 we don't want to diagnose overflow here. */
2021 (negate (convert (view_convert:stype @1)))))
2023 /* (T)(P + A) - (T)(P + B) -> (T)A - (T)B */
2025 (minus (convert (plus:c @@0 @1))
2026 (convert (plus:c @0 @2)))
2027 (if (INTEGRAL_TYPE_P (type)
2028 && TYPE_OVERFLOW_UNDEFINED (type)
2029 && element_precision (type) <= element_precision (TREE_TYPE (@1))
2030 && element_precision (type) <= element_precision (TREE_TYPE (@2)))
2031 (with { tree utype = unsigned_type_for (type); }
2032 (convert (minus (convert:utype @1) (convert:utype @2))))
2033 (if (((element_precision (type) <= element_precision (TREE_TYPE (@1)))
2034 == (element_precision (type) <= element_precision (TREE_TYPE (@2))))
2035 && (element_precision (type) <= element_precision (TREE_TYPE (@1))
2036 /* For integer types, if A has a smaller type
2037 than T the result depends on the possible
2039 E.g. T=size_t, A=(unsigned)429497295, P>0.
2040 However, if an overflow in P + A would cause
2041 undefined behavior, we can assume that there
2043 || (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2044 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
2045 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))
2046 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@2)))))
2047 (minus (convert @1) (convert @2)))))
2049 (minus (convert (pointer_plus @@0 @1))
2050 (convert (pointer_plus @0 @2)))
2051 (if (INTEGRAL_TYPE_P (type)
2052 && TYPE_OVERFLOW_UNDEFINED (type)
2053 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
2054 (with { tree utype = unsigned_type_for (type); }
2055 (convert (minus (convert:utype @1) (convert:utype @2))))
2056 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
2057 /* For pointer types, if the conversion of A to the
2058 final type requires a sign- or zero-extension,
2059 then we have to punt - it is not defined which
2061 || (POINTER_TYPE_P (TREE_TYPE (@0))
2062 && TREE_CODE (@1) == INTEGER_CST
2063 && tree_int_cst_sign_bit (@1) == 0
2064 && TREE_CODE (@2) == INTEGER_CST
2065 && tree_int_cst_sign_bit (@2) == 0))
2066 (minus (convert @1) (convert @2)))))
2068 (pointer_diff (pointer_plus @@0 @1) (pointer_plus @0 @2))
2069 /* The second argument of pointer_plus must be interpreted as signed, and
2070 thus sign-extended if necessary. */
2071 (with { tree stype = signed_type_for (TREE_TYPE (@1)); }
2072 /* Use view_convert instead of convert here, as POINTER_PLUS_EXPR
2073 second arg is unsigned even when we need to consider it as signed,
2074 we don't want to diagnose overflow here. */
2075 (minus (convert (view_convert:stype @1))
2076 (convert (view_convert:stype @2)))))))
2078 /* (A * C) +- (B * C) -> (A+-B) * C and (A * C) +- A -> A * (C+-1).
2079 Modeled after fold_plusminus_mult_expr. */
2080 (if (!TYPE_SATURATING (type)
2081 && (!FLOAT_TYPE_P (type) || flag_associative_math))
2082 (for plusminus (plus minus)
2084 (plusminus (mult:cs@3 @0 @1) (mult:cs@4 @0 @2))
2085 (if ((!ANY_INTEGRAL_TYPE_P (type)
2086 || TYPE_OVERFLOW_WRAPS (type)
2087 || (INTEGRAL_TYPE_P (type)
2088 && tree_expr_nonzero_p (@0)
2089 && expr_not_equal_to (@0, wi::minus_one (TYPE_PRECISION (type)))))
2090 /* If @1 +- @2 is constant require a hard single-use on either
2091 original operand (but not on both). */
2092 && (single_use (@3) || single_use (@4)))
2093 (mult (plusminus @1 @2) @0)))
2094 /* We cannot generate constant 1 for fract. */
2095 (if (!ALL_FRACT_MODE_P (TYPE_MODE (type)))
2097 (plusminus @0 (mult:c@3 @0 @2))
2098 (if ((!ANY_INTEGRAL_TYPE_P (type)
2099 || TYPE_OVERFLOW_WRAPS (type)
2100 || (INTEGRAL_TYPE_P (type)
2101 && tree_expr_nonzero_p (@0)
2102 && expr_not_equal_to (@0, wi::minus_one (TYPE_PRECISION (type)))))
2104 (mult (plusminus { build_one_cst (type); } @2) @0)))
2106 (plusminus (mult:c@3 @0 @2) @0)
2107 (if ((!ANY_INTEGRAL_TYPE_P (type)
2108 || TYPE_OVERFLOW_WRAPS (type)
2109 || (INTEGRAL_TYPE_P (type)
2110 && tree_expr_nonzero_p (@0)
2111 && expr_not_equal_to (@0, wi::minus_one (TYPE_PRECISION (type)))))
2113 (mult (plusminus @2 { build_one_cst (type); }) @0))))))
2115 /* Simplifications of MIN_EXPR, MAX_EXPR, fmin() and fmax(). */
2117 (for minmax (min max FMIN_ALL FMAX_ALL)
2121 /* min(max(x,y),y) -> y. */
2123 (min:c (max:c @0 @1) @1)
2125 /* max(min(x,y),y) -> y. */
2127 (max:c (min:c @0 @1) @1)
2129 /* max(a,-a) -> abs(a). */
2131 (max:c @0 (negate @0))
2132 (if (TREE_CODE (type) != COMPLEX_TYPE
2133 && (! ANY_INTEGRAL_TYPE_P (type)
2134 || TYPE_OVERFLOW_UNDEFINED (type)))
2136 /* min(a,-a) -> -abs(a). */
2138 (min:c @0 (negate @0))
2139 (if (TREE_CODE (type) != COMPLEX_TYPE
2140 && (! ANY_INTEGRAL_TYPE_P (type)
2141 || TYPE_OVERFLOW_UNDEFINED (type)))
2146 (if (INTEGRAL_TYPE_P (type)
2147 && TYPE_MIN_VALUE (type)
2148 && operand_equal_p (@1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
2150 (if (INTEGRAL_TYPE_P (type)
2151 && TYPE_MAX_VALUE (type)
2152 && operand_equal_p (@1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
2157 (if (INTEGRAL_TYPE_P (type)
2158 && TYPE_MAX_VALUE (type)
2159 && operand_equal_p (@1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
2161 (if (INTEGRAL_TYPE_P (type)
2162 && TYPE_MIN_VALUE (type)
2163 && operand_equal_p (@1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
2166 /* max (a, a + CST) -> a + CST where CST is positive. */
2167 /* max (a, a + CST) -> a where CST is negative. */
2169 (max:c @0 (plus@2 @0 INTEGER_CST@1))
2170 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
2171 (if (tree_int_cst_sgn (@1) > 0)
2175 /* min (a, a + CST) -> a where CST is positive. */
2176 /* min (a, a + CST) -> a + CST where CST is negative. */
2178 (min:c @0 (plus@2 @0 INTEGER_CST@1))
2179 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
2180 (if (tree_int_cst_sgn (@1) > 0)
2184 /* (convert (minmax ((convert (x) c)))) -> minmax (x c) if x is promoted
2185 and the outer convert demotes the expression back to x's type. */
2186 (for minmax (min max)
2188 (convert (minmax@0 (convert @1) INTEGER_CST@2))
2189 (if (INTEGRAL_TYPE_P (type)
2190 && types_match (@1, type) && int_fits_type_p (@2, type)
2191 && TYPE_SIGN (TREE_TYPE (@0)) == TYPE_SIGN (type)
2192 && TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type))
2193 (minmax @1 (convert @2)))))
2195 (for minmax (FMIN_ALL FMAX_ALL)
2196 /* If either argument is NaN, return the other one. Avoid the
2197 transformation if we get (and honor) a signalling NaN. */
2199 (minmax:c @0 REAL_CST@1)
2200 (if (real_isnan (TREE_REAL_CST_PTR (@1))
2201 && (!HONOR_SNANS (@1) || !TREE_REAL_CST (@1).signalling))
2203 /* Convert fmin/fmax to MIN_EXPR/MAX_EXPR. C99 requires these
2204 functions to return the numeric arg if the other one is NaN.
2205 MIN and MAX don't honor that, so only transform if -ffinite-math-only
2206 is set. C99 doesn't require -0.0 to be handled, so we don't have to
2207 worry about it either. */
2208 (if (flag_finite_math_only)
2215 /* min (-A, -B) -> -max (A, B) */
2216 (for minmax (min max FMIN_ALL FMAX_ALL)
2217 maxmin (max min FMAX_ALL FMIN_ALL)
2219 (minmax (negate:s@2 @0) (negate:s@3 @1))
2220 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
2221 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2222 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
2223 (negate (maxmin @0 @1)))))
2224 /* MIN (~X, ~Y) -> ~MAX (X, Y)
2225 MAX (~X, ~Y) -> ~MIN (X, Y) */
2226 (for minmax (min max)
2229 (minmax (bit_not:s@2 @0) (bit_not:s@3 @1))
2230 (bit_not (maxmin @0 @1))))
2232 /* MIN (X, Y) == X -> X <= Y */
2233 (for minmax (min min max max)
2237 (cmp:c (minmax:c @0 @1) @0)
2238 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0)))
2240 /* MIN (X, 5) == 0 -> X == 0
2241 MIN (X, 5) == 7 -> false */
2244 (cmp (min @0 INTEGER_CST@1) INTEGER_CST@2)
2245 (if (wi::lt_p (wi::to_wide (@1), wi::to_wide (@2),
2246 TYPE_SIGN (TREE_TYPE (@0))))
2247 { constant_boolean_node (cmp == NE_EXPR, type); }
2248 (if (wi::gt_p (wi::to_wide (@1), wi::to_wide (@2),
2249 TYPE_SIGN (TREE_TYPE (@0))))
2253 (cmp (max @0 INTEGER_CST@1) INTEGER_CST@2)
2254 (if (wi::gt_p (wi::to_wide (@1), wi::to_wide (@2),
2255 TYPE_SIGN (TREE_TYPE (@0))))
2256 { constant_boolean_node (cmp == NE_EXPR, type); }
2257 (if (wi::lt_p (wi::to_wide (@1), wi::to_wide (@2),
2258 TYPE_SIGN (TREE_TYPE (@0))))
2260 /* MIN (X, C1) < C2 -> X < C2 || C1 < C2 */
2261 (for minmax (min min max max min min max max )
2262 cmp (lt le gt ge gt ge lt le )
2263 comb (bit_ior bit_ior bit_ior bit_ior bit_and bit_and bit_and bit_and)
2265 (cmp (minmax @0 INTEGER_CST@1) INTEGER_CST@2)
2266 (comb (cmp @0 @2) (cmp @1 @2))))
2268 /* Simplifications of shift and rotates. */
2270 (for rotate (lrotate rrotate)
2272 (rotate integer_all_onesp@0 @1)
2275 /* Optimize -1 >> x for arithmetic right shifts. */
2277 (rshift integer_all_onesp@0 @1)
2278 (if (!TYPE_UNSIGNED (type)
2279 && tree_expr_nonnegative_p (@1))
2282 /* Optimize (x >> c) << c into x & (-1<<c). */
2284 (lshift (rshift @0 INTEGER_CST@1) @1)
2285 (if (wi::ltu_p (wi::to_wide (@1), element_precision (type)))
2286 (bit_and @0 (lshift { build_minus_one_cst (type); } @1))))
2288 /* Optimize (x << c) >> c into x & ((unsigned)-1 >> c) for unsigned
2291 (rshift (lshift @0 INTEGER_CST@1) @1)
2292 (if (TYPE_UNSIGNED (type)
2293 && (wi::ltu_p (wi::to_wide (@1), element_precision (type))))
2294 (bit_and @0 (rshift { build_minus_one_cst (type); } @1))))
2296 (for shiftrotate (lrotate rrotate lshift rshift)
2298 (shiftrotate @0 integer_zerop)
2301 (shiftrotate integer_zerop@0 @1)
2303 /* Prefer vector1 << scalar to vector1 << vector2
2304 if vector2 is uniform. */
2305 (for vec (VECTOR_CST CONSTRUCTOR)
2307 (shiftrotate @0 vec@1)
2308 (with { tree tem = uniform_vector_p (@1); }
2310 (shiftrotate @0 { tem; }))))))
2312 /* Simplify X << Y where Y's low width bits are 0 to X, as only valid
2313 Y is 0. Similarly for X >> Y. */
2315 (for shift (lshift rshift)
2317 (shift @0 SSA_NAME@1)
2318 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1)))
2320 int width = ceil_log2 (element_precision (TREE_TYPE (@0)));
2321 int prec = TYPE_PRECISION (TREE_TYPE (@1));
2323 (if ((get_nonzero_bits (@1) & wi::mask (width, false, prec)) == 0)
2327 /* Rewrite an LROTATE_EXPR by a constant into an
2328 RROTATE_EXPR by a new constant. */
2330 (lrotate @0 INTEGER_CST@1)
2331 (rrotate @0 { const_binop (MINUS_EXPR, TREE_TYPE (@1),
2332 build_int_cst (TREE_TYPE (@1),
2333 element_precision (type)), @1); }))
2335 /* Turn (a OP c1) OP c2 into a OP (c1+c2). */
2336 (for op (lrotate rrotate rshift lshift)
2338 (op (op @0 INTEGER_CST@1) INTEGER_CST@2)
2339 (with { unsigned int prec = element_precision (type); }
2340 (if (wi::ge_p (wi::to_wide (@1), 0, TYPE_SIGN (TREE_TYPE (@1)))
2341 && wi::lt_p (wi::to_wide (@1), prec, TYPE_SIGN (TREE_TYPE (@1)))
2342 && wi::ge_p (wi::to_wide (@2), 0, TYPE_SIGN (TREE_TYPE (@2)))
2343 && wi::lt_p (wi::to_wide (@2), prec, TYPE_SIGN (TREE_TYPE (@2))))
2344 (with { unsigned int low = (tree_to_uhwi (@1)
2345 + tree_to_uhwi (@2)); }
2346 /* Deal with a OP (c1 + c2) being undefined but (a OP c1) OP c2
2347 being well defined. */
2349 (if (op == LROTATE_EXPR || op == RROTATE_EXPR)
2350 (op @0 { build_int_cst (TREE_TYPE (@1), low % prec); })
2351 (if (TYPE_UNSIGNED (type) || op == LSHIFT_EXPR)
2352 { build_zero_cst (type); }
2353 (op @0 { build_int_cst (TREE_TYPE (@1), prec - 1); })))
2354 (op @0 { build_int_cst (TREE_TYPE (@1), low); })))))))
2357 /* ((1 << A) & 1) != 0 -> A == 0
2358 ((1 << A) & 1) == 0 -> A != 0 */
2362 (cmp (bit_and (lshift integer_onep @0) integer_onep) integer_zerop)
2363 (icmp @0 { build_zero_cst (TREE_TYPE (@0)); })))
2365 /* (CST1 << A) == CST2 -> A == ctz (CST2) - ctz (CST1)
2366 (CST1 << A) != CST2 -> A != ctz (CST2) - ctz (CST1)
2370 (cmp (lshift INTEGER_CST@0 @1) INTEGER_CST@2)
2371 (with { int cand = wi::ctz (wi::to_wide (@2)) - wi::ctz (wi::to_wide (@0)); }
2373 || (!integer_zerop (@2)
2374 && wi::lshift (wi::to_wide (@0), cand) != wi::to_wide (@2)))
2375 { constant_boolean_node (cmp == NE_EXPR, type); }
2376 (if (!integer_zerop (@2)
2377 && wi::lshift (wi::to_wide (@0), cand) == wi::to_wide (@2))
2378 (cmp @1 { build_int_cst (TREE_TYPE (@1), cand); }))))))
2380 /* Fold (X << C1) & C2 into (X << C1) & (C2 | ((1 << C1) - 1))
2381 (X >> C1) & C2 into (X >> C1) & (C2 | ~((type) -1 >> C1))
2382 if the new mask might be further optimized. */
2383 (for shift (lshift rshift)
2385 (bit_and (convert?:s@4 (shift:s@5 (convert1?@3 @0) INTEGER_CST@1))
2387 (if (tree_nop_conversion_p (TREE_TYPE (@4), TREE_TYPE (@5))
2388 && TYPE_PRECISION (type) <= HOST_BITS_PER_WIDE_INT
2389 && tree_fits_uhwi_p (@1)
2390 && tree_to_uhwi (@1) > 0
2391 && tree_to_uhwi (@1) < TYPE_PRECISION (type))
2394 unsigned int shiftc = tree_to_uhwi (@1);
2395 unsigned HOST_WIDE_INT mask = TREE_INT_CST_LOW (@2);
2396 unsigned HOST_WIDE_INT newmask, zerobits = 0;
2397 tree shift_type = TREE_TYPE (@3);
2400 if (shift == LSHIFT_EXPR)
2401 zerobits = ((HOST_WIDE_INT_1U << shiftc) - 1);
2402 else if (shift == RSHIFT_EXPR
2403 && type_has_mode_precision_p (shift_type))
2405 prec = TYPE_PRECISION (TREE_TYPE (@3));
2407 /* See if more bits can be proven as zero because of
2410 && TYPE_UNSIGNED (TREE_TYPE (@0)))
2412 tree inner_type = TREE_TYPE (@0);
2413 if (type_has_mode_precision_p (inner_type)
2414 && TYPE_PRECISION (inner_type) < prec)
2416 prec = TYPE_PRECISION (inner_type);
2417 /* See if we can shorten the right shift. */
2419 shift_type = inner_type;
2420 /* Otherwise X >> C1 is all zeros, so we'll optimize
2421 it into (X, 0) later on by making sure zerobits
2425 zerobits = HOST_WIDE_INT_M1U;
2428 zerobits >>= HOST_BITS_PER_WIDE_INT - shiftc;
2429 zerobits <<= prec - shiftc;
2431 /* For arithmetic shift if sign bit could be set, zerobits
2432 can contain actually sign bits, so no transformation is
2433 possible, unless MASK masks them all away. In that
2434 case the shift needs to be converted into logical shift. */
2435 if (!TYPE_UNSIGNED (TREE_TYPE (@3))
2436 && prec == TYPE_PRECISION (TREE_TYPE (@3)))
2438 if ((mask & zerobits) == 0)
2439 shift_type = unsigned_type_for (TREE_TYPE (@3));
2445 /* ((X << 16) & 0xff00) is (X, 0). */
2446 (if ((mask & zerobits) == mask)
2447 { build_int_cst (type, 0); }
2448 (with { newmask = mask | zerobits; }
2449 (if (newmask != mask && (newmask & (newmask + 1)) == 0)
2452 /* Only do the transformation if NEWMASK is some integer
2454 for (prec = BITS_PER_UNIT;
2455 prec < HOST_BITS_PER_WIDE_INT; prec <<= 1)
2456 if (newmask == (HOST_WIDE_INT_1U << prec) - 1)
2459 (if (prec < HOST_BITS_PER_WIDE_INT
2460 || newmask == HOST_WIDE_INT_M1U)
2462 { tree newmaskt = build_int_cst_type (TREE_TYPE (@2), newmask); }
2463 (if (!tree_int_cst_equal (newmaskt, @2))
2464 (if (shift_type != TREE_TYPE (@3))
2465 (bit_and (convert (shift:shift_type (convert @3) @1)) { newmaskt; })
2466 (bit_and @4 { newmaskt; })))))))))))))
2468 /* Fold (X {&,^,|} C2) << C1 into (X << C1) {&,^,|} (C2 << C1)
2469 (X {&,^,|} C2) >> C1 into (X >> C1) & (C2 >> C1). */
2470 (for shift (lshift rshift)
2471 (for bit_op (bit_and bit_xor bit_ior)
2473 (shift (convert?:s (bit_op:s @0 INTEGER_CST@2)) INTEGER_CST@1)
2474 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2475 (with { tree mask = int_const_binop (shift, fold_convert (type, @2), @1); }
2476 (bit_op (shift (convert @0) @1) { mask; }))))))
2478 /* ~(~X >> Y) -> X >> Y (for arithmetic shift). */
2480 (bit_not (convert1?:s (rshift:s (convert2?@0 (bit_not @1)) @2)))
2481 (if (!TYPE_UNSIGNED (TREE_TYPE (@0))
2482 && (element_precision (TREE_TYPE (@0))
2483 <= element_precision (TREE_TYPE (@1))
2484 || !TYPE_UNSIGNED (TREE_TYPE (@1))))
2486 { tree shift_type = TREE_TYPE (@0); }
2487 (convert (rshift (convert:shift_type @1) @2)))))
2489 /* ~(~X >>r Y) -> X >>r Y
2490 ~(~X <<r Y) -> X <<r Y */
2491 (for rotate (lrotate rrotate)
2493 (bit_not (convert1?:s (rotate:s (convert2?@0 (bit_not @1)) @2)))
2494 (if ((element_precision (TREE_TYPE (@0))
2495 <= element_precision (TREE_TYPE (@1))
2496 || !TYPE_UNSIGNED (TREE_TYPE (@1)))
2497 && (element_precision (type) <= element_precision (TREE_TYPE (@0))
2498 || !TYPE_UNSIGNED (TREE_TYPE (@0))))
2500 { tree rotate_type = TREE_TYPE (@0); }
2501 (convert (rotate (convert:rotate_type @1) @2))))))
2503 /* Simplifications of conversions. */
2505 /* Basic strip-useless-type-conversions / strip_nops. */
2506 (for cvt (convert view_convert float fix_trunc)
2509 (if ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@0)))
2510 || (GENERIC && type == TREE_TYPE (@0)))
2513 /* Contract view-conversions. */
2515 (view_convert (view_convert @0))
2518 /* For integral conversions with the same precision or pointer
2519 conversions use a NOP_EXPR instead. */
2522 (if ((INTEGRAL_TYPE_P (type) || POINTER_TYPE_P (type))
2523 && (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
2524 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (@0)))
2527 /* Strip inner integral conversions that do not change precision or size, or
2528 zero-extend while keeping the same size (for bool-to-char). */
2530 (view_convert (convert@0 @1))
2531 (if ((INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
2532 && (INTEGRAL_TYPE_P (TREE_TYPE (@1)) || POINTER_TYPE_P (TREE_TYPE (@1)))
2533 && TYPE_SIZE (TREE_TYPE (@0)) == TYPE_SIZE (TREE_TYPE (@1))
2534 && (TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1))
2535 || (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (TREE_TYPE (@1))
2536 && TYPE_UNSIGNED (TREE_TYPE (@1)))))
2539 /* Re-association barriers around constants and other re-association
2540 barriers can be removed. */
2542 (paren CONSTANT_CLASS_P@0)
2545 (paren (paren@1 @0))
2548 /* Handle cases of two conversions in a row. */
2549 (for ocvt (convert float fix_trunc)
2550 (for icvt (convert float)
2555 tree inside_type = TREE_TYPE (@0);
2556 tree inter_type = TREE_TYPE (@1);
2557 int inside_int = INTEGRAL_TYPE_P (inside_type);
2558 int inside_ptr = POINTER_TYPE_P (inside_type);
2559 int inside_float = FLOAT_TYPE_P (inside_type);
2560 int inside_vec = VECTOR_TYPE_P (inside_type);
2561 unsigned int inside_prec = TYPE_PRECISION (inside_type);
2562 int inside_unsignedp = TYPE_UNSIGNED (inside_type);
2563 int inter_int = INTEGRAL_TYPE_P (inter_type);
2564 int inter_ptr = POINTER_TYPE_P (inter_type);
2565 int inter_float = FLOAT_TYPE_P (inter_type);
2566 int inter_vec = VECTOR_TYPE_P (inter_type);
2567 unsigned int inter_prec = TYPE_PRECISION (inter_type);
2568 int inter_unsignedp = TYPE_UNSIGNED (inter_type);
2569 int final_int = INTEGRAL_TYPE_P (type);
2570 int final_ptr = POINTER_TYPE_P (type);
2571 int final_float = FLOAT_TYPE_P (type);
2572 int final_vec = VECTOR_TYPE_P (type);
2573 unsigned int final_prec = TYPE_PRECISION (type);
2574 int final_unsignedp = TYPE_UNSIGNED (type);
2577 /* In addition to the cases of two conversions in a row
2578 handled below, if we are converting something to its own
2579 type via an object of identical or wider precision, neither
2580 conversion is needed. */
2581 (if (((GIMPLE && useless_type_conversion_p (type, inside_type))
2583 && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (inside_type)))
2584 && (((inter_int || inter_ptr) && final_int)
2585 || (inter_float && final_float))
2586 && inter_prec >= final_prec)
2589 /* Likewise, if the intermediate and initial types are either both
2590 float or both integer, we don't need the middle conversion if the
2591 former is wider than the latter and doesn't change the signedness
2592 (for integers). Avoid this if the final type is a pointer since
2593 then we sometimes need the middle conversion. */
2594 (if (((inter_int && inside_int) || (inter_float && inside_float))
2595 && (final_int || final_float)
2596 && inter_prec >= inside_prec
2597 && (inter_float || inter_unsignedp == inside_unsignedp))
2600 /* If we have a sign-extension of a zero-extended value, we can
2601 replace that by a single zero-extension. Likewise if the
2602 final conversion does not change precision we can drop the
2603 intermediate conversion. */
2604 (if (inside_int && inter_int && final_int
2605 && ((inside_prec < inter_prec && inter_prec < final_prec
2606 && inside_unsignedp && !inter_unsignedp)
2607 || final_prec == inter_prec))
2610 /* Two conversions in a row are not needed unless:
2611 - some conversion is floating-point (overstrict for now), or
2612 - some conversion is a vector (overstrict for now), or
2613 - the intermediate type is narrower than both initial and
2615 - the intermediate type and innermost type differ in signedness,
2616 and the outermost type is wider than the intermediate, or
2617 - the initial type is a pointer type and the precisions of the
2618 intermediate and final types differ, or
2619 - the final type is a pointer type and the precisions of the
2620 initial and intermediate types differ. */
2621 (if (! inside_float && ! inter_float && ! final_float
2622 && ! inside_vec && ! inter_vec && ! final_vec
2623 && (inter_prec >= inside_prec || inter_prec >= final_prec)
2624 && ! (inside_int && inter_int
2625 && inter_unsignedp != inside_unsignedp
2626 && inter_prec < final_prec)
2627 && ((inter_unsignedp && inter_prec > inside_prec)
2628 == (final_unsignedp && final_prec > inter_prec))
2629 && ! (inside_ptr && inter_prec != final_prec)
2630 && ! (final_ptr && inside_prec != inter_prec))
2633 /* A truncation to an unsigned type (a zero-extension) should be
2634 canonicalized as bitwise and of a mask. */
2635 (if (GIMPLE /* PR70366: doing this in GENERIC breaks -Wconversion. */
2636 && final_int && inter_int && inside_int
2637 && final_prec == inside_prec
2638 && final_prec > inter_prec
2640 (convert (bit_and @0 { wide_int_to_tree
2642 wi::mask (inter_prec, false,
2643 TYPE_PRECISION (inside_type))); })))
2645 /* If we are converting an integer to a floating-point that can
2646 represent it exactly and back to an integer, we can skip the
2647 floating-point conversion. */
2648 (if (GIMPLE /* PR66211 */
2649 && inside_int && inter_float && final_int &&
2650 (unsigned) significand_size (TYPE_MODE (inter_type))
2651 >= inside_prec - !inside_unsignedp)
2654 /* If we have a narrowing conversion to an integral type that is fed by a
2655 BIT_AND_EXPR, we might be able to remove the BIT_AND_EXPR if it merely
2656 masks off bits outside the final type (and nothing else). */
2658 (convert (bit_and @0 INTEGER_CST@1))
2659 (if (INTEGRAL_TYPE_P (type)
2660 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
2661 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))
2662 && operand_equal_p (@1, build_low_bits_mask (TREE_TYPE (@1),
2663 TYPE_PRECISION (type)), 0))
2667 /* (X /[ex] A) * A -> X. */
2669 (mult (convert1? (exact_div @0 @@1)) (convert2? @1))
2672 /* ((X /[ex] A) +- B) * A --> X +- A * B. */
2673 (for op (plus minus)
2675 (mult (convert1? (op (convert2? (exact_div @0 INTEGER_CST@@1)) INTEGER_CST@2)) @1)
2676 (if (tree_nop_conversion_p (type, TREE_TYPE (@2))
2677 && tree_nop_conversion_p (TREE_TYPE (@0), TREE_TYPE (@2)))
2680 wi::overflow_type overflow;
2681 wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@2),
2682 TYPE_SIGN (type), &overflow);
2684 (if (types_match (type, TREE_TYPE (@2))
2685 && types_match (TREE_TYPE (@0), TREE_TYPE (@2)) && !overflow)
2686 (op @0 { wide_int_to_tree (type, mul); })
2687 (with { tree utype = unsigned_type_for (type); }
2688 (convert (op (convert:utype @0)
2689 (mult (convert:utype @1) (convert:utype @2))))))))))
2691 /* Canonicalization of binary operations. */
2693 /* Convert X + -C into X - C. */
2695 (plus @0 REAL_CST@1)
2696 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
2697 (with { tree tem = const_unop (NEGATE_EXPR, type, @1); }
2698 (if (!TREE_OVERFLOW (tem) || !flag_trapping_math)
2699 (minus @0 { tem; })))))
2701 /* Convert x+x into x*2. */
2704 (if (SCALAR_FLOAT_TYPE_P (type))
2705 (mult @0 { build_real (type, dconst2); })
2706 (if (INTEGRAL_TYPE_P (type))
2707 (mult @0 { build_int_cst (type, 2); }))))
2711 (minus integer_zerop @1)
2714 (pointer_diff integer_zerop @1)
2715 (negate (convert @1)))
2717 /* (ARG0 - ARG1) is the same as (-ARG1 + ARG0). So check whether
2718 ARG0 is zero and X + ARG0 reduces to X, since that would mean
2719 (-ARG1 + ARG0) reduces to -ARG1. */
2721 (minus real_zerop@0 @1)
2722 (if (fold_real_zero_addition_p (type, @0, 0))
2725 /* Transform x * -1 into -x. */
2727 (mult @0 integer_minus_onep)
2730 /* Reassociate (X * CST) * Y to (X * Y) * CST. This does not introduce
2731 signed overflow for CST != 0 && CST != -1. */
2733 (mult:c (mult:s@3 @0 INTEGER_CST@1) @2)
2734 (if (TREE_CODE (@2) != INTEGER_CST
2736 && !integer_zerop (@1) && !integer_minus_onep (@1))
2737 (mult (mult @0 @2) @1)))
2739 /* True if we can easily extract the real and imaginary parts of a complex
2741 (match compositional_complex
2742 (convert? (complex @0 @1)))
2744 /* COMPLEX_EXPR and REALPART/IMAGPART_EXPR cancellations. */
2746 (complex (realpart @0) (imagpart @0))
2749 (realpart (complex @0 @1))
2752 (imagpart (complex @0 @1))
2755 /* Sometimes we only care about half of a complex expression. */
2757 (realpart (convert?:s (conj:s @0)))
2758 (convert (realpart @0)))
2760 (imagpart (convert?:s (conj:s @0)))
2761 (convert (negate (imagpart @0))))
2762 (for part (realpart imagpart)
2763 (for op (plus minus)
2765 (part (convert?:s@2 (op:s @0 @1)))
2766 (convert (op (part @0) (part @1))))))
2768 (realpart (convert?:s (CEXPI:s @0)))
2771 (imagpart (convert?:s (CEXPI:s @0)))
2774 /* conj(conj(x)) -> x */
2776 (conj (convert? (conj @0)))
2777 (if (tree_nop_conversion_p (TREE_TYPE (@0), type))
2780 /* conj({x,y}) -> {x,-y} */
2782 (conj (convert?:s (complex:s @0 @1)))
2783 (with { tree itype = TREE_TYPE (type); }
2784 (complex (convert:itype @0) (negate (convert:itype @1)))))
2786 /* BSWAP simplifications, transforms checked by gcc.dg/builtin-bswap-8.c. */
2787 (for bswap (BUILT_IN_BSWAP16 BUILT_IN_BSWAP32 BUILT_IN_BSWAP64)
2792 (bswap (bit_not (bswap @0)))
2794 (for bitop (bit_xor bit_ior bit_and)
2796 (bswap (bitop:c (bswap @0) @1))
2797 (bitop @0 (bswap @1)))))
2800 /* Combine COND_EXPRs and VEC_COND_EXPRs. */
2802 /* Simplify constant conditions.
2803 Only optimize constant conditions when the selected branch
2804 has the same type as the COND_EXPR. This avoids optimizing
2805 away "c ? x : throw", where the throw has a void type.
2806 Note that we cannot throw away the fold-const.c variant nor
2807 this one as we depend on doing this transform before possibly
2808 A ? B : B -> B triggers and the fold-const.c one can optimize
2809 0 ? A : B to B even if A has side-effects. Something
2810 genmatch cannot handle. */
2812 (cond INTEGER_CST@0 @1 @2)
2813 (if (integer_zerop (@0))
2814 (if (!VOID_TYPE_P (TREE_TYPE (@2)) || VOID_TYPE_P (type))
2816 (if (!VOID_TYPE_P (TREE_TYPE (@1)) || VOID_TYPE_P (type))
2819 (vec_cond VECTOR_CST@0 @1 @2)
2820 (if (integer_all_onesp (@0))
2822 (if (integer_zerop (@0))
2825 /* Simplification moved from fold_cond_expr_with_comparison. It may also
2827 /* This pattern implements two kinds simplification:
2830 (cond (cmp (convert1? x) c1) (convert2? x) c2) -> (minmax (x c)) if:
2831 1) Conversions are type widening from smaller type.
2832 2) Const c1 equals to c2 after canonicalizing comparison.
2833 3) Comparison has tree code LT, LE, GT or GE.
2834 This specific pattern is needed when (cmp (convert x) c) may not
2835 be simplified by comparison patterns because of multiple uses of
2836 x. It also makes sense here because simplifying across multiple
2837 referred var is always benefitial for complicated cases.
2840 (cond (eq (convert1? x) c1) (convert2? x) c2) -> (cond (eq x c1) c1 c2). */
2841 (for cmp (lt le gt ge eq)
2843 (cond (cmp (convert1? @1) INTEGER_CST@3) (convert2? @1) INTEGER_CST@2)
2846 tree from_type = TREE_TYPE (@1);
2847 tree c1_type = TREE_TYPE (@3), c2_type = TREE_TYPE (@2);
2848 enum tree_code code = ERROR_MARK;
2850 if (INTEGRAL_TYPE_P (from_type)
2851 && int_fits_type_p (@2, from_type)
2852 && (types_match (c1_type, from_type)
2853 || (TYPE_PRECISION (c1_type) > TYPE_PRECISION (from_type)
2854 && (TYPE_UNSIGNED (from_type)
2855 || TYPE_SIGN (c1_type) == TYPE_SIGN (from_type))))
2856 && (types_match (c2_type, from_type)
2857 || (TYPE_PRECISION (c2_type) > TYPE_PRECISION (from_type)
2858 && (TYPE_UNSIGNED (from_type)
2859 || TYPE_SIGN (c2_type) == TYPE_SIGN (from_type)))))
2863 if (wi::to_widest (@3) == (wi::to_widest (@2) - 1))
2865 /* X <= Y - 1 equals to X < Y. */
2868 /* X > Y - 1 equals to X >= Y. */
2872 if (wi::to_widest (@3) == (wi::to_widest (@2) + 1))
2874 /* X < Y + 1 equals to X <= Y. */
2877 /* X >= Y + 1 equals to X > Y. */
2881 if (code != ERROR_MARK
2882 || wi::to_widest (@2) == wi::to_widest (@3))
2884 if (cmp == LT_EXPR || cmp == LE_EXPR)
2886 if (cmp == GT_EXPR || cmp == GE_EXPR)
2890 /* Can do A == C1 ? A : C2 -> A == C1 ? C1 : C2? */
2891 else if (int_fits_type_p (@3, from_type))
2895 (if (code == MAX_EXPR)
2896 (convert (max @1 (convert @2)))
2897 (if (code == MIN_EXPR)
2898 (convert (min @1 (convert @2)))
2899 (if (code == EQ_EXPR)
2900 (convert (cond (eq @1 (convert @3))
2901 (convert:from_type @3) (convert:from_type @2)))))))))
2903 /* (cond (cmp (convert? x) c1) (op x c2) c3) -> (op (minmax x c1) c2) if:
2905 1) OP is PLUS or MINUS.
2906 2) CMP is LT, LE, GT or GE.
2907 3) C3 == (C1 op C2), and computation doesn't have undefined behavior.
2909 This pattern also handles special cases like:
2911 A) Operand x is a unsigned to signed type conversion and c1 is
2912 integer zero. In this case,
2913 (signed type)x < 0 <=> x > MAX_VAL(signed type)
2914 (signed type)x >= 0 <=> x <= MAX_VAL(signed type)
2915 B) Const c1 may not equal to (C3 op' C2). In this case we also
2916 check equality for (c1+1) and (c1-1) by adjusting comparison
2919 TODO: Though signed type is handled by this pattern, it cannot be
2920 simplified at the moment because C standard requires additional
2921 type promotion. In order to match&simplify it here, the IR needs
2922 to be cleaned up by other optimizers, i.e, VRP. */
2923 (for op (plus minus)
2924 (for cmp (lt le gt ge)
2926 (cond (cmp (convert? @X) INTEGER_CST@1) (op @X INTEGER_CST@2) INTEGER_CST@3)
2927 (with { tree from_type = TREE_TYPE (@X), to_type = TREE_TYPE (@1); }
2928 (if (types_match (from_type, to_type)
2929 /* Check if it is special case A). */
2930 || (TYPE_UNSIGNED (from_type)
2931 && !TYPE_UNSIGNED (to_type)
2932 && TYPE_PRECISION (from_type) == TYPE_PRECISION (to_type)
2933 && integer_zerop (@1)
2934 && (cmp == LT_EXPR || cmp == GE_EXPR)))
2937 wi::overflow_type overflow = wi::OVF_NONE;
2938 enum tree_code code, cmp_code = cmp;
2940 wide_int c1 = wi::to_wide (@1);
2941 wide_int c2 = wi::to_wide (@2);
2942 wide_int c3 = wi::to_wide (@3);
2943 signop sgn = TYPE_SIGN (from_type);
2945 /* Handle special case A), given x of unsigned type:
2946 ((signed type)x < 0) <=> (x > MAX_VAL(signed type))
2947 ((signed type)x >= 0) <=> (x <= MAX_VAL(signed type)) */
2948 if (!types_match (from_type, to_type))
2950 if (cmp_code == LT_EXPR)
2952 if (cmp_code == GE_EXPR)
2954 c1 = wi::max_value (to_type);
2956 /* To simplify this pattern, we require c3 = (c1 op c2). Here we
2957 compute (c3 op' c2) and check if it equals to c1 with op' being
2958 the inverted operator of op. Make sure overflow doesn't happen
2959 if it is undefined. */
2960 if (op == PLUS_EXPR)
2961 real_c1 = wi::sub (c3, c2, sgn, &overflow);
2963 real_c1 = wi::add (c3, c2, sgn, &overflow);
2966 if (!overflow || !TYPE_OVERFLOW_UNDEFINED (from_type))
2968 /* Check if c1 equals to real_c1. Boundary condition is handled
2969 by adjusting comparison operation if necessary. */
2970 if (!wi::cmp (wi::sub (real_c1, 1, sgn, &overflow), c1, sgn)
2973 /* X <= Y - 1 equals to X < Y. */
2974 if (cmp_code == LE_EXPR)
2976 /* X > Y - 1 equals to X >= Y. */
2977 if (cmp_code == GT_EXPR)
2980 if (!wi::cmp (wi::add (real_c1, 1, sgn, &overflow), c1, sgn)
2983 /* X < Y + 1 equals to X <= Y. */
2984 if (cmp_code == LT_EXPR)
2986 /* X >= Y + 1 equals to X > Y. */
2987 if (cmp_code == GE_EXPR)
2990 if (code != cmp_code || !wi::cmp (real_c1, c1, sgn))
2992 if (cmp_code == LT_EXPR || cmp_code == LE_EXPR)
2994 if (cmp_code == GT_EXPR || cmp_code == GE_EXPR)
2999 (if (code == MAX_EXPR)
3000 (op (max @X { wide_int_to_tree (from_type, real_c1); })
3001 { wide_int_to_tree (from_type, c2); })
3002 (if (code == MIN_EXPR)
3003 (op (min @X { wide_int_to_tree (from_type, real_c1); })
3004 { wide_int_to_tree (from_type, c2); })))))))))
3006 (for cnd (cond vec_cond)
3007 /* A ? B : (A ? X : C) -> A ? B : C. */
3009 (cnd @0 (cnd @0 @1 @2) @3)
3012 (cnd @0 @1 (cnd @0 @2 @3))
3014 /* A ? B : (!A ? C : X) -> A ? B : C. */
3015 /* ??? This matches embedded conditions open-coded because genmatch
3016 would generate matching code for conditions in separate stmts only.
3017 The following is still important to merge then and else arm cases
3018 from if-conversion. */
3020 (cnd @0 @1 (cnd @2 @3 @4))
3021 (if (inverse_conditions_p (@0, @2))
3024 (cnd @0 (cnd @1 @2 @3) @4)
3025 (if (inverse_conditions_p (@0, @1))
3028 /* A ? B : B -> B. */
3033 /* !A ? B : C -> A ? C : B. */
3035 (cnd (logical_inverted_value truth_valued_p@0) @1 @2)
3038 /* A + (B vcmp C ? 1 : 0) -> A - (B vcmp C ? -1 : 0), since vector comparisons
3039 return all -1 or all 0 results. */
3040 /* ??? We could instead convert all instances of the vec_cond to negate,
3041 but that isn't necessarily a win on its own. */
3043 (plus:c @3 (view_convert? (vec_cond:s @0 integer_each_onep@1 integer_zerop@2)))
3044 (if (VECTOR_TYPE_P (type)
3045 && known_eq (TYPE_VECTOR_SUBPARTS (type),
3046 TYPE_VECTOR_SUBPARTS (TREE_TYPE (@1)))
3047 && (TYPE_MODE (TREE_TYPE (type))
3048 == TYPE_MODE (TREE_TYPE (TREE_TYPE (@1)))))
3049 (minus @3 (view_convert (vec_cond @0 (negate @1) @2)))))
3051 /* ... likewise A - (B vcmp C ? 1 : 0) -> A + (B vcmp C ? -1 : 0). */
3053 (minus @3 (view_convert? (vec_cond:s @0 integer_each_onep@1 integer_zerop@2)))
3054 (if (VECTOR_TYPE_P (type)
3055 && known_eq (TYPE_VECTOR_SUBPARTS (type),
3056 TYPE_VECTOR_SUBPARTS (TREE_TYPE (@1)))
3057 && (TYPE_MODE (TREE_TYPE (type))
3058 == TYPE_MODE (TREE_TYPE (TREE_TYPE (@1)))))
3059 (plus @3 (view_convert (vec_cond @0 (negate @1) @2)))))
3062 /* Simplifications of comparisons. */
3064 /* See if we can reduce the magnitude of a constant involved in a
3065 comparison by changing the comparison code. This is a canonicalization
3066 formerly done by maybe_canonicalize_comparison_1. */
3070 (cmp @0 INTEGER_CST@1)
3071 (if (tree_int_cst_sgn (@1) == -1)
3072 (acmp @0 { wide_int_to_tree (TREE_TYPE (@1), wi::to_wide (@1) + 1); }))))
3076 (cmp @0 INTEGER_CST@1)
3077 (if (tree_int_cst_sgn (@1) == 1)
3078 (acmp @0 { wide_int_to_tree (TREE_TYPE (@1), wi::to_wide (@1) - 1); }))))
3081 /* We can simplify a logical negation of a comparison to the
3082 inverted comparison. As we cannot compute an expression
3083 operator using invert_tree_comparison we have to simulate
3084 that with expression code iteration. */
3085 (for cmp (tcc_comparison)
3086 icmp (inverted_tcc_comparison)
3087 ncmp (inverted_tcc_comparison_with_nans)
3088 /* Ideally we'd like to combine the following two patterns
3089 and handle some more cases by using
3090 (logical_inverted_value (cmp @0 @1))
3091 here but for that genmatch would need to "inline" that.
3092 For now implement what forward_propagate_comparison did. */
3094 (bit_not (cmp @0 @1))
3095 (if (VECTOR_TYPE_P (type)
3096 || (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1))
3097 /* Comparison inversion may be impossible for trapping math,
3098 invert_tree_comparison will tell us. But we can't use
3099 a computed operator in the replacement tree thus we have
3100 to play the trick below. */
3101 (with { enum tree_code ic = invert_tree_comparison
3102 (cmp, HONOR_NANS (@0)); }
3108 (bit_xor (cmp @0 @1) integer_truep)
3109 (with { enum tree_code ic = invert_tree_comparison
3110 (cmp, HONOR_NANS (@0)); }
3116 /* Transform comparisons of the form X - Y CMP 0 to X CMP Y.
3117 ??? The transformation is valid for the other operators if overflow
3118 is undefined for the type, but performing it here badly interacts
3119 with the transformation in fold_cond_expr_with_comparison which
3120 attempts to synthetize ABS_EXPR. */
3122 (for sub (minus pointer_diff)
3124 (cmp (sub@2 @0 @1) integer_zerop)
3125 (if (single_use (@2))
3128 /* Transform comparisons of the form X * C1 CMP 0 to X CMP 0 in the
3129 signed arithmetic case. That form is created by the compiler
3130 often enough for folding it to be of value. One example is in
3131 computing loop trip counts after Operator Strength Reduction. */
3132 (for cmp (simple_comparison)
3133 scmp (swapped_simple_comparison)
3135 (cmp (mult@3 @0 INTEGER_CST@1) integer_zerop@2)
3136 /* Handle unfolded multiplication by zero. */
3137 (if (integer_zerop (@1))
3139 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3140 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
3142 /* If @1 is negative we swap the sense of the comparison. */
3143 (if (tree_int_cst_sgn (@1) < 0)
3147 /* Simplify comparison of something with itself. For IEEE
3148 floating-point, we can only do some of these simplifications. */
3152 (if (! FLOAT_TYPE_P (TREE_TYPE (@0))
3153 || ! HONOR_NANS (@0))
3154 { constant_boolean_node (true, type); }
3155 (if (cmp != EQ_EXPR)
3161 || ! FLOAT_TYPE_P (TREE_TYPE (@0))
3162 || ! HONOR_NANS (@0))
3163 { constant_boolean_node (false, type); })))
3164 (for cmp (unle unge uneq)
3167 { constant_boolean_node (true, type); }))
3168 (for cmp (unlt ungt)
3174 (if (!flag_trapping_math)
3175 { constant_boolean_node (false, type); }))
3177 /* Fold ~X op ~Y as Y op X. */
3178 (for cmp (simple_comparison)
3180 (cmp (bit_not@2 @0) (bit_not@3 @1))
3181 (if (single_use (@2) && single_use (@3))
3184 /* Fold ~X op C as X op' ~C, where op' is the swapped comparison. */
3185 (for cmp (simple_comparison)
3186 scmp (swapped_simple_comparison)
3188 (cmp (bit_not@2 @0) CONSTANT_CLASS_P@1)
3189 (if (single_use (@2)
3190 && (TREE_CODE (@1) == INTEGER_CST || TREE_CODE (@1) == VECTOR_CST))
3191 (scmp @0 (bit_not @1)))))
3193 (for cmp (simple_comparison)
3194 /* Fold (double)float1 CMP (double)float2 into float1 CMP float2. */
3196 (cmp (convert@2 @0) (convert? @1))
3197 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
3198 && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@2))
3199 == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0)))
3200 && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@2))
3201 == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1))))
3204 tree type1 = TREE_TYPE (@1);
3205 if (TREE_CODE (@1) == REAL_CST && !DECIMAL_FLOAT_TYPE_P (type1))
3207 REAL_VALUE_TYPE orig = TREE_REAL_CST (@1);
3208 if (TYPE_PRECISION (type1) > TYPE_PRECISION (float_type_node)
3209 && exact_real_truncate (TYPE_MODE (float_type_node), &orig))
3210 type1 = float_type_node;
3211 if (TYPE_PRECISION (type1) > TYPE_PRECISION (double_type_node)
3212 && exact_real_truncate (TYPE_MODE (double_type_node), &orig))
3213 type1 = double_type_node;
3216 = (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type1)
3217 ? TREE_TYPE (@0) : type1);
3219 (if (TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (newtype))
3220 (cmp (convert:newtype @0) (convert:newtype @1))))))
3224 /* IEEE doesn't distinguish +0 and -0 in comparisons. */
3226 /* a CMP (-0) -> a CMP 0 */
3227 (if (REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@1)))
3228 (cmp @0 { build_real (TREE_TYPE (@1), dconst0); }))
3229 /* x != NaN is always true, other ops are always false. */
3230 (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
3231 && ! HONOR_SNANS (@1))
3232 { constant_boolean_node (cmp == NE_EXPR, type); })
3233 /* Fold comparisons against infinity. */
3234 (if (REAL_VALUE_ISINF (TREE_REAL_CST (@1))
3235 && MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (@1))))
3238 REAL_VALUE_TYPE max;
3239 enum tree_code code = cmp;
3240 bool neg = REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1));
3242 code = swap_tree_comparison (code);
3245 /* x > +Inf is always false, if we ignore NaNs or exceptions. */
3246 (if (code == GT_EXPR
3247 && !(HONOR_NANS (@0) && flag_trapping_math))
3248 { constant_boolean_node (false, type); })
3249 (if (code == LE_EXPR)
3250 /* x <= +Inf is always true, if we don't care about NaNs. */
3251 (if (! HONOR_NANS (@0))
3252 { constant_boolean_node (true, type); }
3253 /* x <= +Inf is the same as x == x, i.e. !isnan(x), but this loses
3254 an "invalid" exception. */
3255 (if (!flag_trapping_math)
3257 /* x == +Inf and x >= +Inf are always equal to x > DBL_MAX, but
3258 for == this introduces an exception for x a NaN. */
3259 (if ((code == EQ_EXPR && !(HONOR_NANS (@0) && flag_trapping_math))
3261 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
3263 (lt @0 { build_real (TREE_TYPE (@0), max); })
3264 (gt @0 { build_real (TREE_TYPE (@0), max); }))))
3265 /* x < +Inf is always equal to x <= DBL_MAX. */
3266 (if (code == LT_EXPR)
3267 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
3269 (ge @0 { build_real (TREE_TYPE (@0), max); })
3270 (le @0 { build_real (TREE_TYPE (@0), max); }))))
3271 /* x != +Inf is always equal to !(x > DBL_MAX), but this introduces
3272 an exception for x a NaN so use an unordered comparison. */
3273 (if (code == NE_EXPR)
3274 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
3275 (if (! HONOR_NANS (@0))
3277 (ge @0 { build_real (TREE_TYPE (@0), max); })
3278 (le @0 { build_real (TREE_TYPE (@0), max); }))
3280 (unge @0 { build_real (TREE_TYPE (@0), max); })
3281 (unle @0 { build_real (TREE_TYPE (@0), max); }))))))))))
3283 /* If this is a comparison of a real constant with a PLUS_EXPR
3284 or a MINUS_EXPR of a real constant, we can convert it into a
3285 comparison with a revised real constant as long as no overflow
3286 occurs when unsafe_math_optimizations are enabled. */
3287 (if (flag_unsafe_math_optimizations)
3288 (for op (plus minus)
3290 (cmp (op @0 REAL_CST@1) REAL_CST@2)
3293 tree tem = const_binop (op == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR,
3294 TREE_TYPE (@1), @2, @1);
3296 (if (tem && !TREE_OVERFLOW (tem))
3297 (cmp @0 { tem; }))))))
3299 /* Likewise, we can simplify a comparison of a real constant with
3300 a MINUS_EXPR whose first operand is also a real constant, i.e.
3301 (c1 - x) < c2 becomes x > c1-c2. Reordering is allowed on
3302 floating-point types only if -fassociative-math is set. */
3303 (if (flag_associative_math)
3305 (cmp (minus REAL_CST@0 @1) REAL_CST@2)
3306 (with { tree tem = const_binop (MINUS_EXPR, TREE_TYPE (@1), @0, @2); }
3307 (if (tem && !TREE_OVERFLOW (tem))
3308 (cmp { tem; } @1)))))
3310 /* Fold comparisons against built-in math functions. */
3311 (if (flag_unsafe_math_optimizations
3312 && ! flag_errno_math)
3315 (cmp (sq @0) REAL_CST@1)
3317 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
3319 /* sqrt(x) < y is always false, if y is negative. */
3320 (if (cmp == EQ_EXPR || cmp == LT_EXPR || cmp == LE_EXPR)
3321 { constant_boolean_node (false, type); })
3322 /* sqrt(x) > y is always true, if y is negative and we
3323 don't care about NaNs, i.e. negative values of x. */
3324 (if (cmp == NE_EXPR || !HONOR_NANS (@0))
3325 { constant_boolean_node (true, type); })
3326 /* sqrt(x) > y is the same as x >= 0, if y is negative. */
3327 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })))
3328 (if (real_equal (TREE_REAL_CST_PTR (@1), &dconst0))
3330 /* sqrt(x) < 0 is always false. */
3331 (if (cmp == LT_EXPR)
3332 { constant_boolean_node (false, type); })
3333 /* sqrt(x) >= 0 is always true if we don't care about NaNs. */
3334 (if (cmp == GE_EXPR && !HONOR_NANS (@0))
3335 { constant_boolean_node (true, type); })
3336 /* sqrt(x) <= 0 -> x == 0. */
3337 (if (cmp == LE_EXPR)
3339 /* Otherwise sqrt(x) cmp 0 -> x cmp 0. Here cmp can be >=, >,
3340 == or !=. In the last case:
3342 (sqrt(x) != 0) == (NaN != 0) == true == (x != 0)
3344 if x is negative or NaN. Due to -funsafe-math-optimizations,
3345 the results for other x follow from natural arithmetic. */
3347 (if (cmp == GT_EXPR || cmp == GE_EXPR)
3351 real_arithmetic (&c2, MULT_EXPR,
3352 &TREE_REAL_CST (@1), &TREE_REAL_CST (@1));
3353 real_convert (&c2, TYPE_MODE (TREE_TYPE (@0)), &c2);
3355 (if (REAL_VALUE_ISINF (c2))
3356 /* sqrt(x) > y is x == +Inf, when y is very large. */
3357 (if (HONOR_INFINITIES (@0))
3358 (eq @0 { build_real (TREE_TYPE (@0), c2); })
3359 { constant_boolean_node (false, type); })
3360 /* sqrt(x) > c is the same as x > c*c. */
3361 (cmp @0 { build_real (TREE_TYPE (@0), c2); }))))
3362 (if (cmp == LT_EXPR || cmp == LE_EXPR)
3366 real_arithmetic (&c2, MULT_EXPR,
3367 &TREE_REAL_CST (@1), &TREE_REAL_CST (@1));
3368 real_convert (&c2, TYPE_MODE (TREE_TYPE (@0)), &c2);
3370 (if (REAL_VALUE_ISINF (c2))
3372 /* sqrt(x) < y is always true, when y is a very large
3373 value and we don't care about NaNs or Infinities. */
3374 (if (! HONOR_NANS (@0) && ! HONOR_INFINITIES (@0))
3375 { constant_boolean_node (true, type); })
3376 /* sqrt(x) < y is x != +Inf when y is very large and we
3377 don't care about NaNs. */
3378 (if (! HONOR_NANS (@0))
3379 (ne @0 { build_real (TREE_TYPE (@0), c2); }))
3380 /* sqrt(x) < y is x >= 0 when y is very large and we
3381 don't care about Infinities. */
3382 (if (! HONOR_INFINITIES (@0))
3383 (ge @0 { build_real (TREE_TYPE (@0), dconst0); }))
3384 /* sqrt(x) < y is x >= 0 && x != +Inf, when y is large. */
3387 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
3388 (ne @0 { build_real (TREE_TYPE (@0), c2); }))))
3389 /* sqrt(x) < c is the same as x < c*c, if we ignore NaNs. */
3390 (if (! HONOR_NANS (@0))
3391 (cmp @0 { build_real (TREE_TYPE (@0), c2); })
3392 /* sqrt(x) < c is the same as x >= 0 && x < c*c. */
3395 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
3396 (cmp @0 { build_real (TREE_TYPE (@0), c2); })))))))))
3397 /* Transform sqrt(x) cmp sqrt(y) -> x cmp y. */
3399 (cmp (sq @0) (sq @1))
3400 (if (! HONOR_NANS (@0))
3403 /* Optimize various special cases of (FTYPE) N CMP CST. */
3404 (for cmp (lt le eq ne ge gt)
3405 icmp (le le eq ne ge ge)
3407 (cmp (float @0) REAL_CST@1)
3408 (if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (@1))
3409 && ! DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1)))
3412 tree itype = TREE_TYPE (@0);
3413 signop isign = TYPE_SIGN (itype);
3414 format_helper fmt (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (@1))));
3415 const REAL_VALUE_TYPE *cst = TREE_REAL_CST_PTR (@1);
3416 /* Be careful to preserve any potential exceptions due to
3417 NaNs. qNaNs are ok in == or != context.
3418 TODO: relax under -fno-trapping-math or
3419 -fno-signaling-nans. */
3421 = real_isnan (cst) && (cst->signalling
3422 || (cmp != EQ_EXPR && cmp != NE_EXPR));
3423 /* INT?_MIN is power-of-two so it takes
3424 only one mantissa bit. */
3425 bool signed_p = isign == SIGNED;
3426 bool itype_fits_ftype_p
3427 = TYPE_PRECISION (itype) - signed_p <= significand_size (fmt);
3429 /* TODO: allow non-fitting itype and SNaNs when
3430 -fno-trapping-math. */
3431 (if (itype_fits_ftype_p && ! exception_p)
3434 REAL_VALUE_TYPE imin, imax;
3435 real_from_integer (&imin, fmt, wi::min_value (itype), isign);
3436 real_from_integer (&imax, fmt, wi::max_value (itype), isign);
3438 REAL_VALUE_TYPE icst;
3439 if (cmp == GT_EXPR || cmp == GE_EXPR)
3440 real_ceil (&icst, fmt, cst);
3441 else if (cmp == LT_EXPR || cmp == LE_EXPR)
3442 real_floor (&icst, fmt, cst);
3444 real_trunc (&icst, fmt, cst);
3446 bool cst_int_p = !real_isnan (cst) && real_identical (&icst, cst);
3448 bool overflow_p = false;
3450 = real_to_integer (&icst, &overflow_p, TYPE_PRECISION (itype));
3453 /* Optimize cases when CST is outside of ITYPE's range. */
3454 (if (real_compare (LT_EXPR, cst, &imin))
3455 { constant_boolean_node (cmp == GT_EXPR || cmp == GE_EXPR || cmp == NE_EXPR,
3457 (if (real_compare (GT_EXPR, cst, &imax))
3458 { constant_boolean_node (cmp == LT_EXPR || cmp == LE_EXPR || cmp == NE_EXPR,
3460 /* Remove cast if CST is an integer representable by ITYPE. */
3462 (cmp @0 { gcc_assert (!overflow_p);
3463 wide_int_to_tree (itype, icst_val); })
3465 /* When CST is fractional, optimize
3466 (FTYPE) N == CST -> 0
3467 (FTYPE) N != CST -> 1. */
3468 (if (cmp == EQ_EXPR || cmp == NE_EXPR)
3469 { constant_boolean_node (cmp == NE_EXPR, type); })
3470 /* Otherwise replace with sensible integer constant. */
3473 gcc_checking_assert (!overflow_p);
3475 (icmp @0 { wide_int_to_tree (itype, icst_val); })))))))))
3477 /* Fold A /[ex] B CMP C to A CMP B * C. */
3480 (cmp (exact_div @0 @1) INTEGER_CST@2)
3481 (if (!integer_zerop (@1))
3482 (if (wi::to_wide (@2) == 0)
3484 (if (TREE_CODE (@1) == INTEGER_CST)
3487 wi::overflow_type ovf;
3488 wide_int prod = wi::mul (wi::to_wide (@2), wi::to_wide (@1),
3489 TYPE_SIGN (TREE_TYPE (@1)), &ovf);
3492 { constant_boolean_node (cmp == NE_EXPR, type); }
3493 (cmp @0 { wide_int_to_tree (TREE_TYPE (@0), prod); }))))))))
3494 (for cmp (lt le gt ge)
3496 (cmp (exact_div @0 INTEGER_CST@1) INTEGER_CST@2)
3497 (if (wi::gt_p (wi::to_wide (@1), 0, TYPE_SIGN (TREE_TYPE (@1))))
3500 wi::overflow_type ovf;
3501 wide_int prod = wi::mul (wi::to_wide (@2), wi::to_wide (@1),
3502 TYPE_SIGN (TREE_TYPE (@1)), &ovf);
3505 { constant_boolean_node (wi::lt_p (wi::to_wide (@2), 0,
3506 TYPE_SIGN (TREE_TYPE (@2)))
3507 != (cmp == LT_EXPR || cmp == LE_EXPR), type); }
3508 (cmp @0 { wide_int_to_tree (TREE_TYPE (@0), prod); }))))))
3510 /* Unordered tests if either argument is a NaN. */
3512 (bit_ior (unordered @0 @0) (unordered @1 @1))
3513 (if (types_match (@0, @1))
3516 (bit_and (ordered @0 @0) (ordered @1 @1))
3517 (if (types_match (@0, @1))
3520 (bit_ior:c (unordered @0 @0) (unordered:c@2 @0 @1))
3523 (bit_and:c (ordered @0 @0) (ordered:c@2 @0 @1))
3526 /* Simple range test simplifications. */
3527 /* A < B || A >= B -> true. */
3528 (for test1 (lt le le le ne ge)
3529 test2 (ge gt ge ne eq ne)
3531 (bit_ior:c (test1 @0 @1) (test2 @0 @1))
3532 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
3533 || VECTOR_INTEGER_TYPE_P (TREE_TYPE (@0)))
3534 { constant_boolean_node (true, type); })))
3535 /* A < B && A >= B -> false. */
3536 (for test1 (lt lt lt le ne eq)
3537 test2 (ge gt eq gt eq gt)
3539 (bit_and:c (test1 @0 @1) (test2 @0 @1))
3540 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
3541 || VECTOR_INTEGER_TYPE_P (TREE_TYPE (@0)))
3542 { constant_boolean_node (false, type); })))
3544 /* A & (2**N - 1) <= 2**K - 1 -> A & (2**N - 2**K) == 0
3545 A & (2**N - 1) > 2**K - 1 -> A & (2**N - 2**K) != 0
3547 Note that comparisons
3548 A & (2**N - 1) < 2**K -> A & (2**N - 2**K) == 0
3549 A & (2**N - 1) >= 2**K -> A & (2**N - 2**K) != 0
3550 will be canonicalized to above so there's no need to
3557 (cmp (bit_and@0 @1 INTEGER_CST@2) INTEGER_CST@3)
3558 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)))
3561 tree ty = TREE_TYPE (@0);
3562 unsigned prec = TYPE_PRECISION (ty);
3563 wide_int mask = wi::to_wide (@2, prec);
3564 wide_int rhs = wi::to_wide (@3, prec);
3565 signop sgn = TYPE_SIGN (ty);
3567 (if ((mask & (mask + 1)) == 0 && wi::gt_p (rhs, 0, sgn)
3568 && (rhs & (rhs + 1)) == 0 && wi::ge_p (mask, rhs, sgn))
3569 (eqcmp (bit_and @1 { wide_int_to_tree (ty, mask - rhs); })
3570 { build_zero_cst (ty); }))))))
3572 /* -A CMP -B -> B CMP A. */
3573 (for cmp (tcc_comparison)
3574 scmp (swapped_tcc_comparison)
3576 (cmp (negate @0) (negate @1))
3577 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
3578 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3579 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
3582 (cmp (negate @0) CONSTANT_CLASS_P@1)
3583 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
3584 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3585 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
3586 (with { tree tem = const_unop (NEGATE_EXPR, TREE_TYPE (@0), @1); }
3587 (if (tem && !TREE_OVERFLOW (tem))
3588 (scmp @0 { tem; }))))))
3590 /* Convert ABS_EXPR<x> == 0 or ABS_EXPR<x> != 0 to x == 0 or x != 0. */
3593 (op (abs @0) zerop@1)
3596 /* From fold_sign_changed_comparison and fold_widened_comparison.
3597 FIXME: the lack of symmetry is disturbing. */
3598 (for cmp (simple_comparison)
3600 (cmp (convert@0 @00) (convert?@1 @10))
3601 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
3602 /* Disable this optimization if we're casting a function pointer
3603 type on targets that require function pointer canonicalization. */
3604 && !(targetm.have_canonicalize_funcptr_for_compare ()
3605 && ((POINTER_TYPE_P (TREE_TYPE (@00))
3606 && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@00))))
3607 || (POINTER_TYPE_P (TREE_TYPE (@10))
3608 && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@10))))))
3610 (if (TYPE_PRECISION (TREE_TYPE (@00)) == TYPE_PRECISION (TREE_TYPE (@0))
3611 && (TREE_CODE (@10) == INTEGER_CST
3613 && (TYPE_UNSIGNED (TREE_TYPE (@00)) == TYPE_UNSIGNED (TREE_TYPE (@0))
3616 && !POINTER_TYPE_P (TREE_TYPE (@00)))
3617 /* ??? The special-casing of INTEGER_CST conversion was in the original
3618 code and here to avoid a spurious overflow flag on the resulting
3619 constant which fold_convert produces. */
3620 (if (TREE_CODE (@1) == INTEGER_CST)
3621 (cmp @00 { force_fit_type (TREE_TYPE (@00), wi::to_widest (@1), 0,
3622 TREE_OVERFLOW (@1)); })
3623 (cmp @00 (convert @1)))
3625 (if (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (TREE_TYPE (@00)))
3626 /* If possible, express the comparison in the shorter mode. */
3627 (if ((cmp == EQ_EXPR || cmp == NE_EXPR
3628 || TYPE_UNSIGNED (TREE_TYPE (@0)) == TYPE_UNSIGNED (TREE_TYPE (@00))
3629 || (!TYPE_UNSIGNED (TREE_TYPE (@0))
3630 && TYPE_UNSIGNED (TREE_TYPE (@00))))
3631 && (types_match (TREE_TYPE (@10), TREE_TYPE (@00))
3632 || ((TYPE_PRECISION (TREE_TYPE (@00))
3633 >= TYPE_PRECISION (TREE_TYPE (@10)))
3634 && (TYPE_UNSIGNED (TREE_TYPE (@00))
3635 == TYPE_UNSIGNED (TREE_TYPE (@10))))
3636 || (TREE_CODE (@10) == INTEGER_CST
3637 && INTEGRAL_TYPE_P (TREE_TYPE (@00))
3638 && int_fits_type_p (@10, TREE_TYPE (@00)))))
3639 (cmp @00 (convert @10))
3640 (if (TREE_CODE (@10) == INTEGER_CST
3641 && INTEGRAL_TYPE_P (TREE_TYPE (@00))
3642 && !int_fits_type_p (@10, TREE_TYPE (@00)))
3645 tree min = lower_bound_in_type (TREE_TYPE (@10), TREE_TYPE (@00));
3646 tree max = upper_bound_in_type (TREE_TYPE (@10), TREE_TYPE (@00));
3647 bool above = integer_nonzerop (const_binop (LT_EXPR, type, max, @10));
3648 bool below = integer_nonzerop (const_binop (LT_EXPR, type, @10, min));
3650 (if (above || below)
3651 (if (cmp == EQ_EXPR || cmp == NE_EXPR)
3652 { constant_boolean_node (cmp == EQ_EXPR ? false : true, type); }
3653 (if (cmp == LT_EXPR || cmp == LE_EXPR)
3654 { constant_boolean_node (above ? true : false, type); }
3655 (if (cmp == GT_EXPR || cmp == GE_EXPR)
3656 { constant_boolean_node (above ? false : true, type); }))))))))))))
3659 /* A local variable can never be pointed to by
3660 the default SSA name of an incoming parameter.
3661 SSA names are canonicalized to 2nd place. */
3663 (cmp addr@0 SSA_NAME@1)
3664 (if (SSA_NAME_IS_DEFAULT_DEF (@1)
3665 && TREE_CODE (SSA_NAME_VAR (@1)) == PARM_DECL)
3666 (with { tree base = get_base_address (TREE_OPERAND (@0, 0)); }
3667 (if (TREE_CODE (base) == VAR_DECL
3668 && auto_var_in_fn_p (base, current_function_decl))
3669 (if (cmp == NE_EXPR)
3670 { constant_boolean_node (true, type); }
3671 { constant_boolean_node (false, type); }))))))
3673 /* Equality compare simplifications from fold_binary */
3676 /* If we have (A | C) == D where C & ~D != 0, convert this into 0.
3677 Similarly for NE_EXPR. */
3679 (cmp (convert?@3 (bit_ior @0 INTEGER_CST@1)) INTEGER_CST@2)
3680 (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0))
3681 && wi::bit_and_not (wi::to_wide (@1), wi::to_wide (@2)) != 0)
3682 { constant_boolean_node (cmp == NE_EXPR, type); }))
3684 /* (X ^ Y) == 0 becomes X == Y, and (X ^ Y) != 0 becomes X != Y. */
3686 (cmp (bit_xor @0 @1) integer_zerop)
3689 /* (X ^ Y) == Y becomes X == 0.
3690 Likewise (X ^ Y) == X becomes Y == 0. */
3692 (cmp:c (bit_xor:c @0 @1) @0)
3693 (cmp @1 { build_zero_cst (TREE_TYPE (@1)); }))
3695 /* (X ^ C1) op C2 can be rewritten as X op (C1 ^ C2). */
3697 (cmp (convert?@3 (bit_xor @0 INTEGER_CST@1)) INTEGER_CST@2)
3698 (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0)))
3699 (cmp @0 (bit_xor @1 (convert @2)))))
3702 (cmp (convert? addr@0) integer_zerop)
3703 (if (tree_single_nonzero_warnv_p (@0, NULL))
3704 { constant_boolean_node (cmp == NE_EXPR, type); })))
3706 /* If we have (A & C) == C where C is a power of 2, convert this into
3707 (A & C) != 0. Similarly for NE_EXPR. */
3711 (cmp (bit_and@2 @0 integer_pow2p@1) @1)
3712 (icmp @2 { build_zero_cst (TREE_TYPE (@0)); })))
3714 /* If we have (A & C) != 0 ? D : 0 where C and D are powers of 2,
3715 convert this into a shift followed by ANDing with D. */
3718 (ne (bit_and @0 integer_pow2p@1) integer_zerop)
3719 INTEGER_CST@2 integer_zerop)
3720 (if (integer_pow2p (@2))
3722 int shift = (wi::exact_log2 (wi::to_wide (@2))
3723 - wi::exact_log2 (wi::to_wide (@1)));
3727 (lshift (convert @0) { build_int_cst (integer_type_node, shift); }) @2)
3729 (convert (rshift @0 { build_int_cst (integer_type_node, -shift); }))
3732 /* If we have (A & C) != 0 where C is the sign bit of A, convert
3733 this into A < 0. Similarly for (A & C) == 0 into A >= 0. */
3737 (cmp (bit_and (convert?@2 @0) integer_pow2p@1) integer_zerop)
3738 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
3739 && type_has_mode_precision_p (TREE_TYPE (@0))
3740 && element_precision (@2) >= element_precision (@0)
3741 && wi::only_sign_bit_p (wi::to_wide (@1), element_precision (@0)))
3742 (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
3743 (ncmp (convert:stype @0) { build_zero_cst (stype); })))))
3745 /* If we have A < 0 ? C : 0 where C is a power of 2, convert
3746 this into a right shift or sign extension followed by ANDing with C. */
3749 (lt @0 integer_zerop)
3750 INTEGER_CST@1 integer_zerop)
3751 (if (integer_pow2p (@1)
3752 && !TYPE_UNSIGNED (TREE_TYPE (@0)))
3754 int shift = element_precision (@0) - wi::exact_log2 (wi::to_wide (@1)) - 1;
3758 (convert (rshift @0 { build_int_cst (integer_type_node, shift); }))
3760 /* Otherwise ctype must be wider than TREE_TYPE (@0) and pure
3761 sign extension followed by AND with C will achieve the effect. */
3762 (bit_and (convert @0) @1)))))
3764 /* When the addresses are not directly of decls compare base and offset.
3765 This implements some remaining parts of fold_comparison address
3766 comparisons but still no complete part of it. Still it is good
3767 enough to make fold_stmt not regress when not dispatching to fold_binary. */
3768 (for cmp (simple_comparison)
3770 (cmp (convert1?@2 addr@0) (convert2? addr@1))
3773 poly_int64 off0, off1;
3774 tree base0 = get_addr_base_and_unit_offset (TREE_OPERAND (@0, 0), &off0);
3775 tree base1 = get_addr_base_and_unit_offset (TREE_OPERAND (@1, 0), &off1);
3776 if (base0 && TREE_CODE (base0) == MEM_REF)
3778 off0 += mem_ref_offset (base0).force_shwi ();
3779 base0 = TREE_OPERAND (base0, 0);
3781 if (base1 && TREE_CODE (base1) == MEM_REF)
3783 off1 += mem_ref_offset (base1).force_shwi ();
3784 base1 = TREE_OPERAND (base1, 0);
3787 (if (base0 && base1)
3791 /* Punt in GENERIC on variables with value expressions;
3792 the value expressions might point to fields/elements
3793 of other vars etc. */
3795 && ((VAR_P (base0) && DECL_HAS_VALUE_EXPR_P (base0))
3796 || (VAR_P (base1) && DECL_HAS_VALUE_EXPR_P (base1))))
3798 else if (decl_in_symtab_p (base0)
3799 && decl_in_symtab_p (base1))
3800 equal = symtab_node::get_create (base0)
3801 ->equal_address_to (symtab_node::get_create (base1));
3802 else if ((DECL_P (base0)
3803 || TREE_CODE (base0) == SSA_NAME
3804 || TREE_CODE (base0) == STRING_CST)
3806 || TREE_CODE (base1) == SSA_NAME
3807 || TREE_CODE (base1) == STRING_CST))
3808 equal = (base0 == base1);
3811 && (cmp == EQ_EXPR || cmp == NE_EXPR
3812 /* If the offsets are equal we can ignore overflow. */
3813 || known_eq (off0, off1)
3814 || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
3815 /* Or if we compare using pointers to decls or strings. */
3816 || (POINTER_TYPE_P (TREE_TYPE (@2))
3817 && (DECL_P (base0) || TREE_CODE (base0) == STRING_CST))))
3819 (if (cmp == EQ_EXPR && (known_eq (off0, off1) || known_ne (off0, off1)))
3820 { constant_boolean_node (known_eq (off0, off1), type); })
3821 (if (cmp == NE_EXPR && (known_eq (off0, off1) || known_ne (off0, off1)))
3822 { constant_boolean_node (known_ne (off0, off1), type); })
3823 (if (cmp == LT_EXPR && (known_lt (off0, off1) || known_ge (off0, off1)))
3824 { constant_boolean_node (known_lt (off0, off1), type); })
3825 (if (cmp == LE_EXPR && (known_le (off0, off1) || known_gt (off0, off1)))
3826 { constant_boolean_node (known_le (off0, off1), type); })
3827 (if (cmp == GE_EXPR && (known_ge (off0, off1) || known_lt (off0, off1)))
3828 { constant_boolean_node (known_ge (off0, off1), type); })
3829 (if (cmp == GT_EXPR && (known_gt (off0, off1) || known_le (off0, off1)))
3830 { constant_boolean_node (known_gt (off0, off1), type); }))
3832 && DECL_P (base0) && DECL_P (base1)
3833 /* If we compare this as integers require equal offset. */
3834 && (!INTEGRAL_TYPE_P (TREE_TYPE (@2))
3835 || known_eq (off0, off1)))
3837 (if (cmp == EQ_EXPR)
3838 { constant_boolean_node (false, type); })
3839 (if (cmp == NE_EXPR)
3840 { constant_boolean_node (true, type); })))))))))
3842 /* Simplify pointer equality compares using PTA. */
3846 (if (POINTER_TYPE_P (TREE_TYPE (@0))
3847 && ptrs_compare_unequal (@0, @1))
3848 { constant_boolean_node (neeq != EQ_EXPR, type); })))
3850 /* PR70920: Transform (intptr_t)x eq/ne CST to x eq/ne (typeof x) CST.
3851 and (typeof ptr_cst) x eq/ne ptr_cst to x eq/ne (typeof x) CST.
3852 Disable the transform if either operand is pointer to function.
3853 This broke pr22051-2.c for arm where function pointer
3854 canonicalizaion is not wanted. */
3858 (cmp (convert @0) INTEGER_CST@1)
3859 (if (((POINTER_TYPE_P (TREE_TYPE (@0))
3860 && !FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@0)))
3861 && INTEGRAL_TYPE_P (TREE_TYPE (@1)))
3862 || (INTEGRAL_TYPE_P (TREE_TYPE (@0))
3863 && POINTER_TYPE_P (TREE_TYPE (@1))
3864 && !FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@1)))))
3865 && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
3866 (cmp @0 (convert @1)))))
3868 /* Non-equality compare simplifications from fold_binary */
3869 (for cmp (lt gt le ge)
3870 /* Comparisons with the highest or lowest possible integer of
3871 the specified precision will have known values. */
3873 (cmp (convert?@2 @0) INTEGER_CST@1)
3874 (if ((INTEGRAL_TYPE_P (TREE_TYPE (@1)) || POINTER_TYPE_P (TREE_TYPE (@1)))
3875 && tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@0)))
3878 tree arg1_type = TREE_TYPE (@1);
3879 unsigned int prec = TYPE_PRECISION (arg1_type);
3880 wide_int max = wi::max_value (arg1_type);
3881 wide_int signed_max = wi::max_value (prec, SIGNED);
3882 wide_int min = wi::min_value (arg1_type);
3885 (if (wi::to_wide (@1) == max)
3887 (if (cmp == GT_EXPR)
3888 { constant_boolean_node (false, type); })
3889 (if (cmp == GE_EXPR)
3891 (if (cmp == LE_EXPR)
3892 { constant_boolean_node (true, type); })
3893 (if (cmp == LT_EXPR)
3895 (if (wi::to_wide (@1) == min)
3897 (if (cmp == LT_EXPR)
3898 { constant_boolean_node (false, type); })
3899 (if (cmp == LE_EXPR)
3901 (if (cmp == GE_EXPR)
3902 { constant_boolean_node (true, type); })
3903 (if (cmp == GT_EXPR)
3905 (if (wi::to_wide (@1) == max - 1)
3907 (if (cmp == GT_EXPR)
3908 (eq @2 { wide_int_to_tree (TREE_TYPE (@1), wi::to_wide (@1) + 1); }))
3909 (if (cmp == LE_EXPR)
3910 (ne @2 { wide_int_to_tree (TREE_TYPE (@1), wi::to_wide (@1) + 1); }))))
3911 (if (wi::to_wide (@1) == min + 1)
3913 (if (cmp == GE_EXPR)
3914 (ne @2 { wide_int_to_tree (TREE_TYPE (@1), wi::to_wide (@1) - 1); }))
3915 (if (cmp == LT_EXPR)
3916 (eq @2 { wide_int_to_tree (TREE_TYPE (@1), wi::to_wide (@1) - 1); }))))
3917 (if (wi::to_wide (@1) == signed_max
3918 && TYPE_UNSIGNED (arg1_type)
3919 /* We will flip the signedness of the comparison operator
3920 associated with the mode of @1, so the sign bit is
3921 specified by this mode. Check that @1 is the signed
3922 max associated with this sign bit. */
3923 && prec == GET_MODE_PRECISION (SCALAR_INT_TYPE_MODE (arg1_type))
3924 /* signed_type does not work on pointer types. */
3925 && INTEGRAL_TYPE_P (arg1_type))
3926 /* The following case also applies to X < signed_max+1
3927 and X >= signed_max+1 because previous transformations. */
3928 (if (cmp == LE_EXPR || cmp == GT_EXPR)
3929 (with { tree st = signed_type_for (arg1_type); }
3930 (if (cmp == LE_EXPR)
3931 (ge (convert:st @0) { build_zero_cst (st); })
3932 (lt (convert:st @0) { build_zero_cst (st); }))))))))))
3934 (for cmp (unordered ordered unlt unle ungt unge uneq ltgt)
3935 /* If the second operand is NaN, the result is constant. */
3938 (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
3939 && (cmp != LTGT_EXPR || ! flag_trapping_math))
3940 { constant_boolean_node (cmp == ORDERED_EXPR || cmp == LTGT_EXPR
3941 ? false : true, type); })))
3943 /* bool_var != 0 becomes bool_var. */
3945 (ne @0 integer_zerop)
3946 (if (TREE_CODE (TREE_TYPE (@0)) == BOOLEAN_TYPE
3947 && types_match (type, TREE_TYPE (@0)))
3949 /* bool_var == 1 becomes bool_var. */
3951 (eq @0 integer_onep)
3952 (if (TREE_CODE (TREE_TYPE (@0)) == BOOLEAN_TYPE
3953 && types_match (type, TREE_TYPE (@0)))
3956 bool_var == 0 becomes !bool_var or
3957 bool_var != 1 becomes !bool_var
3958 here because that only is good in assignment context as long
3959 as we require a tcc_comparison in GIMPLE_CONDs where we'd
3960 replace if (x == 0) with tem = ~x; if (tem != 0) which is
3961 clearly less optimal and which we'll transform again in forwprop. */
3963 /* When one argument is a constant, overflow detection can be simplified.
3964 Currently restricted to single use so as not to interfere too much with
3965 ADD_OVERFLOW detection in tree-ssa-math-opts.c.
3966 A + CST CMP A -> A CMP' CST' */
3967 (for cmp (lt le ge gt)
3970 (cmp:c (plus@2 @0 INTEGER_CST@1) @0)
3971 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
3972 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))
3973 && wi::to_wide (@1) != 0
3975 (with { unsigned int prec = TYPE_PRECISION (TREE_TYPE (@0)); }
3976 (out @0 { wide_int_to_tree (TREE_TYPE (@0),
3977 wi::max_value (prec, UNSIGNED)
3978 - wi::to_wide (@1)); })))))
3980 /* To detect overflow in unsigned A - B, A < B is simpler than A - B > A.
3981 However, the detection logic for SUB_OVERFLOW in tree-ssa-math-opts.c
3982 expects the long form, so we restrict the transformation for now. */
3985 (cmp:c (minus@2 @0 @1) @0)
3986 (if (single_use (@2)
3987 && ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3988 && TYPE_UNSIGNED (TREE_TYPE (@0))
3989 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
3992 /* Testing for overflow is unnecessary if we already know the result. */
3997 (cmp:c (realpart (IFN_SUB_OVERFLOW@2 @0 @1)) @0)
3998 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
3999 && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
4000 (out (imagpart @2) { build_zero_cst (TREE_TYPE (@0)); }))))
4005 (cmp:c (realpart (IFN_ADD_OVERFLOW:c@2 @0 @1)) @0)
4006 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
4007 && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
4008 (out (imagpart @2) { build_zero_cst (TREE_TYPE (@0)); }))))
4010 /* For unsigned operands, -1 / B < A checks whether A * B would overflow.
4011 Simplify it to __builtin_mul_overflow (A, B, <unused>). */
4015 (cmp:c (trunc_div:s integer_all_onesp @1) @0)
4016 (if (TYPE_UNSIGNED (TREE_TYPE (@0)) && !VECTOR_TYPE_P (TREE_TYPE (@0)))
4017 (with { tree t = TREE_TYPE (@0), cpx = build_complex_type (t); }
4018 (out (imagpart (IFN_MUL_OVERFLOW:cpx @0 @1)) { build_zero_cst (t); })))))
4020 /* Simplification of math builtins. These rules must all be optimizations
4021 as well as IL simplifications. If there is a possibility that the new
4022 form could be a pessimization, the rule should go in the canonicalization
4023 section that follows this one.
4025 Rules can generally go in this section if they satisfy one of
4028 - the rule describes an identity
4030 - the rule replaces calls with something as simple as addition or
4033 - the rule contains unary calls only and simplifies the surrounding
4034 arithmetic. (The idea here is to exclude non-unary calls in which
4035 one operand is constant and in which the call is known to be cheap
4036 when the operand has that value.) */
4038 (if (flag_unsafe_math_optimizations)
4039 /* Simplify sqrt(x) * sqrt(x) -> x. */
4041 (mult (SQRT_ALL@1 @0) @1)
4042 (if (!HONOR_SNANS (type))
4045 (for op (plus minus)
4046 /* Simplify (A / C) +- (B / C) -> (A +- B) / C. */
4050 (rdiv (op @0 @2) @1)))
4052 /* Simplify sqrt(x) * sqrt(y) -> sqrt(x*y). */
4053 (for root (SQRT CBRT)
4055 (mult (root:s @0) (root:s @1))
4056 (root (mult @0 @1))))
4058 /* Simplify expN(x) * expN(y) -> expN(x+y). */
4059 (for exps (EXP EXP2 EXP10 POW10)
4061 (mult (exps:s @0) (exps:s @1))
4062 (exps (plus @0 @1))))
4064 /* Simplify a/root(b/c) into a*root(c/b). */
4065 (for root (SQRT CBRT)
4067 (rdiv @0 (root:s (rdiv:s @1 @2)))
4068 (mult @0 (root (rdiv @2 @1)))))
4070 /* Simplify x/expN(y) into x*expN(-y). */
4071 (for exps (EXP EXP2 EXP10 POW10)
4073 (rdiv @0 (exps:s @1))
4074 (mult @0 (exps (negate @1)))))
4076 (for logs (LOG LOG2 LOG10 LOG10)
4077 exps (EXP EXP2 EXP10 POW10)
4078 /* logN(expN(x)) -> x. */
4082 /* expN(logN(x)) -> x. */
4087 /* Optimize logN(func()) for various exponential functions. We
4088 want to determine the value "x" and the power "exponent" in
4089 order to transform logN(x**exponent) into exponent*logN(x). */
4090 (for logs (LOG LOG LOG LOG2 LOG2 LOG2 LOG10 LOG10)
4091 exps (EXP2 EXP10 POW10 EXP EXP10 POW10 EXP EXP2)
4094 (if (SCALAR_FLOAT_TYPE_P (type))
4100 /* Prepare to do logN(exp(exponent)) -> exponent*logN(e). */
4101 x = build_real_truncate (type, dconst_e ());
4104 /* Prepare to do logN(exp2(exponent)) -> exponent*logN(2). */
4105 x = build_real (type, dconst2);
4109 /* Prepare to do logN(exp10(exponent)) -> exponent*logN(10). */
4111 REAL_VALUE_TYPE dconst10;
4112 real_from_integer (&dconst10, VOIDmode, 10, SIGNED);
4113 x = build_real (type, dconst10);
4120 (mult (logs { x; }) @0)))))
4128 (if (SCALAR_FLOAT_TYPE_P (type))
4134 /* Prepare to do logN(sqrt(x)) -> 0.5*logN(x). */
4135 x = build_real (type, dconsthalf);
4138 /* Prepare to do logN(cbrt(x)) -> (1/3)*logN(x). */
4139 x = build_real_truncate (type, dconst_third ());
4145 (mult { x; } (logs @0))))))
4147 /* logN(pow(x,exponent)) -> exponent*logN(x). */
4148 (for logs (LOG LOG2 LOG10)
4152 (mult @1 (logs @0))))
4154 /* pow(C,x) -> exp(log(C)*x) if C > 0,
4155 or if C is a positive power of 2,
4156 pow(C,x) -> exp2(log2(C)*x). */
4164 (pows REAL_CST@0 @1)
4165 (if (real_compare (GT_EXPR, TREE_REAL_CST_PTR (@0), &dconst0)
4166 && real_isfinite (TREE_REAL_CST_PTR (@0))
4167 /* As libmvec doesn't have a vectorized exp2, defer optimizing
4168 the use_exp2 case until after vectorization. It seems actually
4169 beneficial for all constants to postpone this until later,
4170 because exp(log(C)*x), while faster, will have worse precision
4171 and if x folds into a constant too, that is unnecessary
4173 && canonicalize_math_after_vectorization_p ())
4175 const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (@0);
4176 bool use_exp2 = false;
4177 if (targetm.libc_has_function (function_c99_misc)
4178 && value->cl == rvc_normal)
4180 REAL_VALUE_TYPE frac_rvt = *value;
4181 SET_REAL_EXP (&frac_rvt, 1);
4182 if (real_equal (&frac_rvt, &dconst1))
4187 (if (optimize_pow_to_exp (@0, @1))
4188 (exps (mult (logs @0) @1)))
4189 (exp2s (mult (log2s @0) @1)))))))
4192 /* pow(C,x)*expN(y) -> expN(logN(C)*x+y) if C > 0. */
4194 exps (EXP EXP2 EXP10 POW10)
4195 logs (LOG LOG2 LOG10 LOG10)
4197 (mult:c (pows:s REAL_CST@0 @1) (exps:s @2))
4198 (if (real_compare (GT_EXPR, TREE_REAL_CST_PTR (@0), &dconst0)
4199 && real_isfinite (TREE_REAL_CST_PTR (@0)))
4200 (exps (plus (mult (logs @0) @1) @2)))))
4205 exps (EXP EXP2 EXP10 POW10)
4206 /* sqrt(expN(x)) -> expN(x*0.5). */
4209 (exps (mult @0 { build_real (type, dconsthalf); })))
4210 /* cbrt(expN(x)) -> expN(x/3). */
4213 (exps (mult @0 { build_real_truncate (type, dconst_third ()); })))
4214 /* pow(expN(x), y) -> expN(x*y). */
4217 (exps (mult @0 @1))))
4219 /* tan(atan(x)) -> x. */
4226 /* cabs(x+0i) or cabs(0+xi) -> abs(x). */
4228 (CABS (complex:C @0 real_zerop@1))
4231 /* trunc(trunc(x)) -> trunc(x), etc. */
4232 (for fns (TRUNC_ALL FLOOR_ALL CEIL_ALL ROUND_ALL NEARBYINT_ALL RINT_ALL)
4236 /* f(x) -> x if x is integer valued and f does nothing for such values. */
4237 (for fns (TRUNC_ALL FLOOR_ALL CEIL_ALL ROUND_ALL NEARBYINT_ALL RINT_ALL)
4239 (fns integer_valued_real_p@0)
4242 /* hypot(x,0) and hypot(0,x) -> abs(x). */
4244 (HYPOT:c @0 real_zerop@1)
4247 /* pow(1,x) -> 1. */
4249 (POW real_onep@0 @1)
4253 /* copysign(x,x) -> x. */
4254 (COPYSIGN_ALL @0 @0)
4258 /* copysign(x,y) -> fabs(x) if y is nonnegative. */
4259 (COPYSIGN_ALL @0 tree_expr_nonnegative_p@1)
4262 (for scale (LDEXP SCALBN SCALBLN)
4263 /* ldexp(0, x) -> 0. */
4265 (scale real_zerop@0 @1)
4267 /* ldexp(x, 0) -> x. */
4269 (scale @0 integer_zerop@1)
4271 /* ldexp(x, y) -> x if x is +-Inf or NaN. */
4273 (scale REAL_CST@0 @1)
4274 (if (!real_isfinite (TREE_REAL_CST_PTR (@0)))
4277 /* Canonicalization of sequences of math builtins. These rules represent
4278 IL simplifications but are not necessarily optimizations.
4280 The sincos pass is responsible for picking "optimal" implementations
4281 of math builtins, which may be more complicated and can sometimes go
4282 the other way, e.g. converting pow into a sequence of sqrts.
4283 We only want to do these canonicalizations before the pass has run. */
4285 (if (flag_unsafe_math_optimizations && canonicalize_math_p ())
4286 /* Simplify tan(x) * cos(x) -> sin(x). */
4288 (mult:c (TAN:s @0) (COS:s @0))
4291 /* Simplify x * pow(x,c) -> pow(x,c+1). */
4293 (mult:c @0 (POW:s @0 REAL_CST@1))
4294 (if (!TREE_OVERFLOW (@1))
4295 (POW @0 (plus @1 { build_one_cst (type); }))))
4297 /* Simplify sin(x) / cos(x) -> tan(x). */
4299 (rdiv (SIN:s @0) (COS:s @0))
4302 /* Simplify cos(x) / sin(x) -> 1 / tan(x). */
4304 (rdiv (COS:s @0) (SIN:s @0))
4305 (rdiv { build_one_cst (type); } (TAN @0)))
4307 /* Simplify sin(x) / tan(x) -> cos(x). */
4309 (rdiv (SIN:s @0) (TAN:s @0))
4310 (if (! HONOR_NANS (@0)
4311 && ! HONOR_INFINITIES (@0))
4314 /* Simplify tan(x) / sin(x) -> 1.0 / cos(x). */
4316 (rdiv (TAN:s @0) (SIN:s @0))
4317 (if (! HONOR_NANS (@0)
4318 && ! HONOR_INFINITIES (@0))
4319 (rdiv { build_one_cst (type); } (COS @0))))
4321 /* Simplify pow(x,y) * pow(x,z) -> pow(x,y+z). */
4323 (mult (POW:s @0 @1) (POW:s @0 @2))
4324 (POW @0 (plus @1 @2)))
4326 /* Simplify pow(x,y) * pow(z,y) -> pow(x*z,y). */
4328 (mult (POW:s @0 @1) (POW:s @2 @1))
4329 (POW (mult @0 @2) @1))
4331 /* Simplify powi(x,y) * powi(z,y) -> powi(x*z,y). */
4333 (mult (POWI:s @0 @1) (POWI:s @2 @1))
4334 (POWI (mult @0 @2) @1))
4336 /* Simplify pow(x,c) / x -> pow(x,c-1). */
4338 (rdiv (POW:s @0 REAL_CST@1) @0)
4339 (if (!TREE_OVERFLOW (@1))
4340 (POW @0 (minus @1 { build_one_cst (type); }))))
4342 /* Simplify x / pow (y,z) -> x * pow(y,-z). */
4344 (rdiv @0 (POW:s @1 @2))
4345 (mult @0 (POW @1 (negate @2))))
4350 /* sqrt(sqrt(x)) -> pow(x,1/4). */
4353 (pows @0 { build_real (type, dconst_quarter ()); }))
4354 /* sqrt(cbrt(x)) -> pow(x,1/6). */
4357 (pows @0 { build_real_truncate (type, dconst_sixth ()); }))
4358 /* cbrt(sqrt(x)) -> pow(x,1/6). */
4361 (pows @0 { build_real_truncate (type, dconst_sixth ()); }))
4362 /* cbrt(cbrt(x)) -> pow(x,1/9), iff x is nonnegative. */
4364 (cbrts (cbrts tree_expr_nonnegative_p@0))
4365 (pows @0 { build_real_truncate (type, dconst_ninth ()); }))
4366 /* sqrt(pow(x,y)) -> pow(|x|,y*0.5). */
4368 (sqrts (pows @0 @1))
4369 (pows (abs @0) (mult @1 { build_real (type, dconsthalf); })))
4370 /* cbrt(pow(x,y)) -> pow(x,y/3), iff x is nonnegative. */
4372 (cbrts (pows tree_expr_nonnegative_p@0 @1))
4373 (pows @0 (mult @1 { build_real_truncate (type, dconst_third ()); })))
4374 /* pow(sqrt(x),y) -> pow(x,y*0.5). */
4376 (pows (sqrts @0) @1)
4377 (pows @0 (mult @1 { build_real (type, dconsthalf); })))
4378 /* pow(cbrt(x),y) -> pow(x,y/3) iff x is nonnegative. */
4380 (pows (cbrts tree_expr_nonnegative_p@0) @1)
4381 (pows @0 (mult @1 { build_real_truncate (type, dconst_third ()); })))
4382 /* pow(pow(x,y),z) -> pow(x,y*z) iff x is nonnegative. */
4384 (pows (pows tree_expr_nonnegative_p@0 @1) @2)
4385 (pows @0 (mult @1 @2))))
4387 /* cabs(x+xi) -> fabs(x)*sqrt(2). */
4389 (CABS (complex @0 @0))
4390 (mult (abs @0) { build_real_truncate (type, dconst_sqrt2 ()); }))
4392 /* hypot(x,x) -> fabs(x)*sqrt(2). */
4395 (mult (abs @0) { build_real_truncate (type, dconst_sqrt2 ()); }))
4397 /* cexp(x+yi) -> exp(x)*cexpi(y). */
4402 (cexps compositional_complex@0)
4403 (if (targetm.libc_has_function (function_c99_math_complex))
4405 (mult (exps@1 (realpart @0)) (realpart (cexpis:type@2 (imagpart @0))))
4406 (mult @1 (imagpart @2)))))))
4408 (if (canonicalize_math_p ())
4409 /* floor(x) -> trunc(x) if x is nonnegative. */
4410 (for floors (FLOOR_ALL)
4413 (floors tree_expr_nonnegative_p@0)
4416 (match double_value_p
4418 (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == double_type_node)))
4419 (for froms (BUILT_IN_TRUNCL
4431 /* truncl(extend(x)) -> extend(trunc(x)), etc., if x is a double. */
4432 (if (optimize && canonicalize_math_p ())
4434 (froms (convert double_value_p@0))
4435 (convert (tos @0)))))
4437 (match float_value_p
4439 (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == float_type_node)))
4440 (for froms (BUILT_IN_TRUNCL BUILT_IN_TRUNC
4441 BUILT_IN_FLOORL BUILT_IN_FLOOR
4442 BUILT_IN_CEILL BUILT_IN_CEIL
4443 BUILT_IN_ROUNDL BUILT_IN_ROUND
4444 BUILT_IN_NEARBYINTL BUILT_IN_NEARBYINT
4445 BUILT_IN_RINTL BUILT_IN_RINT)
4446 tos (BUILT_IN_TRUNCF BUILT_IN_TRUNCF
4447 BUILT_IN_FLOORF BUILT_IN_FLOORF
4448 BUILT_IN_CEILF BUILT_IN_CEILF
4449 BUILT_IN_ROUNDF BUILT_IN_ROUNDF
4450 BUILT_IN_NEARBYINTF BUILT_IN_NEARBYINTF
4451 BUILT_IN_RINTF BUILT_IN_RINTF)
4452 /* truncl(extend(x)) and trunc(extend(x)) -> extend(truncf(x)), etc.,
4454 (if (optimize && canonicalize_math_p ()
4455 && targetm.libc_has_function (function_c99_misc))
4457 (froms (convert float_value_p@0))
4458 (convert (tos @0)))))
4460 (for froms (XFLOORL XCEILL XROUNDL XRINTL)
4461 tos (XFLOOR XCEIL XROUND XRINT)
4462 /* llfloorl(extend(x)) -> llfloor(x), etc., if x is a double. */
4463 (if (optimize && canonicalize_math_p ())
4465 (froms (convert double_value_p@0))
4468 (for froms (XFLOORL XCEILL XROUNDL XRINTL
4469 XFLOOR XCEIL XROUND XRINT)
4470 tos (XFLOORF XCEILF XROUNDF XRINTF)
4471 /* llfloorl(extend(x)) and llfloor(extend(x)) -> llfloorf(x), etc.,
4473 (if (optimize && canonicalize_math_p ())
4475 (froms (convert float_value_p@0))
4478 (if (canonicalize_math_p ())
4479 /* xfloor(x) -> fix_trunc(x) if x is nonnegative. */
4480 (for floors (IFLOOR LFLOOR LLFLOOR)
4482 (floors tree_expr_nonnegative_p@0)
4485 (if (canonicalize_math_p ())
4486 /* xfloor(x) -> fix_trunc(x), etc., if x is integer valued. */
4487 (for fns (IFLOOR LFLOOR LLFLOOR
4489 IROUND LROUND LLROUND)
4491 (fns integer_valued_real_p@0)
4493 (if (!flag_errno_math)
4494 /* xrint(x) -> fix_trunc(x), etc., if x is integer valued. */
4495 (for rints (IRINT LRINT LLRINT)
4497 (rints integer_valued_real_p@0)
4500 (if (canonicalize_math_p ())
4501 (for ifn (IFLOOR ICEIL IROUND IRINT)
4502 lfn (LFLOOR LCEIL LROUND LRINT)
4503 llfn (LLFLOOR LLCEIL LLROUND LLRINT)
4504 /* Canonicalize iround (x) to lround (x) on ILP32 targets where
4505 sizeof (int) == sizeof (long). */
4506 (if (TYPE_PRECISION (integer_type_node)
4507 == TYPE_PRECISION (long_integer_type_node))
4510 (lfn:long_integer_type_node @0)))
4511 /* Canonicalize llround (x) to lround (x) on LP64 targets where
4512 sizeof (long long) == sizeof (long). */
4513 (if (TYPE_PRECISION (long_long_integer_type_node)
4514 == TYPE_PRECISION (long_integer_type_node))
4517 (lfn:long_integer_type_node @0)))))
4519 /* cproj(x) -> x if we're ignoring infinities. */
4522 (if (!HONOR_INFINITIES (type))
4525 /* If the real part is inf and the imag part is known to be
4526 nonnegative, return (inf + 0i). */
4528 (CPROJ (complex REAL_CST@0 tree_expr_nonnegative_p@1))
4529 (if (real_isinf (TREE_REAL_CST_PTR (@0)))
4530 { build_complex_inf (type, false); }))
4532 /* If the imag part is inf, return (inf+I*copysign(0,imag)). */
4534 (CPROJ (complex @0 REAL_CST@1))
4535 (if (real_isinf (TREE_REAL_CST_PTR (@1)))
4536 { build_complex_inf (type, TREE_REAL_CST_PTR (@1)->sign); }))
4542 (pows @0 REAL_CST@1)
4544 const REAL_VALUE_TYPE *value = TREE_REAL_CST_PTR (@1);
4545 REAL_VALUE_TYPE tmp;
4548 /* pow(x,0) -> 1. */
4549 (if (real_equal (value, &dconst0))
4550 { build_real (type, dconst1); })
4551 /* pow(x,1) -> x. */
4552 (if (real_equal (value, &dconst1))
4554 /* pow(x,-1) -> 1/x. */
4555 (if (real_equal (value, &dconstm1))
4556 (rdiv { build_real (type, dconst1); } @0))
4557 /* pow(x,0.5) -> sqrt(x). */
4558 (if (flag_unsafe_math_optimizations
4559 && canonicalize_math_p ()
4560 && real_equal (value, &dconsthalf))
4562 /* pow(x,1/3) -> cbrt(x). */
4563 (if (flag_unsafe_math_optimizations
4564 && canonicalize_math_p ()
4565 && (tmp = real_value_truncate (TYPE_MODE (type), dconst_third ()),
4566 real_equal (value, &tmp)))
4569 /* powi(1,x) -> 1. */
4571 (POWI real_onep@0 @1)
4575 (POWI @0 INTEGER_CST@1)
4577 /* powi(x,0) -> 1. */
4578 (if (wi::to_wide (@1) == 0)
4579 { build_real (type, dconst1); })
4580 /* powi(x,1) -> x. */
4581 (if (wi::to_wide (@1) == 1)
4583 /* powi(x,-1) -> 1/x. */
4584 (if (wi::to_wide (@1) == -1)
4585 (rdiv { build_real (type, dconst1); } @0))))
4587 /* Narrowing of arithmetic and logical operations.
4589 These are conceptually similar to the transformations performed for
4590 the C/C++ front-ends by shorten_binary_op and shorten_compare. Long
4591 term we want to move all that code out of the front-ends into here. */
4593 /* If we have a narrowing conversion of an arithmetic operation where
4594 both operands are widening conversions from the same type as the outer
4595 narrowing conversion. Then convert the innermost operands to a suitable
4596 unsigned type (to avoid introducing undefined behavior), perform the
4597 operation and convert the result to the desired type. */
4598 (for op (plus minus)
4600 (convert (op:s (convert@2 @0) (convert?@3 @1)))
4601 (if (INTEGRAL_TYPE_P (type)
4602 /* We check for type compatibility between @0 and @1 below,
4603 so there's no need to check that @1/@3 are integral types. */
4604 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
4605 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
4606 /* The precision of the type of each operand must match the
4607 precision of the mode of each operand, similarly for the
4609 && type_has_mode_precision_p (TREE_TYPE (@0))
4610 && type_has_mode_precision_p (TREE_TYPE (@1))
4611 && type_has_mode_precision_p (type)
4612 /* The inner conversion must be a widening conversion. */
4613 && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
4614 && types_match (@0, type)
4615 && (types_match (@0, @1)
4616 /* Or the second operand is const integer or converted const
4617 integer from valueize. */
4618 || TREE_CODE (@1) == INTEGER_CST))
4619 (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
4620 (op @0 (convert @1))
4621 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
4622 (convert (op (convert:utype @0)
4623 (convert:utype @1))))))))
4625 /* This is another case of narrowing, specifically when there's an outer
4626 BIT_AND_EXPR which masks off bits outside the type of the innermost
4627 operands. Like the previous case we have to convert the operands
4628 to unsigned types to avoid introducing undefined behavior for the
4629 arithmetic operation. */
4630 (for op (minus plus)
4632 (bit_and (op:s (convert@2 @0) (convert@3 @1)) INTEGER_CST@4)
4633 (if (INTEGRAL_TYPE_P (type)
4634 /* We check for type compatibility between @0 and @1 below,
4635 so there's no need to check that @1/@3 are integral types. */
4636 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
4637 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
4638 /* The precision of the type of each operand must match the
4639 precision of the mode of each operand, similarly for the
4641 && type_has_mode_precision_p (TREE_TYPE (@0))
4642 && type_has_mode_precision_p (TREE_TYPE (@1))
4643 && type_has_mode_precision_p (type)
4644 /* The inner conversion must be a widening conversion. */
4645 && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
4646 && types_match (@0, @1)
4647 && (tree_int_cst_min_precision (@4, TYPE_SIGN (TREE_TYPE (@0)))
4648 <= TYPE_PRECISION (TREE_TYPE (@0)))
4649 && (wi::to_wide (@4)
4650 & wi::mask (TYPE_PRECISION (TREE_TYPE (@0)),
4651 true, TYPE_PRECISION (type))) == 0)
4652 (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
4653 (with { tree ntype = TREE_TYPE (@0); }
4654 (convert (bit_and (op @0 @1) (convert:ntype @4))))
4655 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
4656 (convert (bit_and (op (convert:utype @0) (convert:utype @1))
4657 (convert:utype @4))))))))
4659 /* Transform (@0 < @1 and @0 < @2) to use min,
4660 (@0 > @1 and @0 > @2) to use max */
4661 (for logic (bit_and bit_and bit_and bit_and bit_ior bit_ior bit_ior bit_ior)
4662 op (lt le gt ge lt le gt ge )
4663 ext (min min max max max max min min )
4665 (logic (op:cs @0 @1) (op:cs @0 @2))
4666 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
4667 && TREE_CODE (@0) != INTEGER_CST)
4668 (op @0 (ext @1 @2)))))
4671 /* signbit(x) -> 0 if x is nonnegative. */
4672 (SIGNBIT tree_expr_nonnegative_p@0)
4673 { integer_zero_node; })
4676 /* signbit(x) -> x<0 if x doesn't have signed zeros. */
4678 (if (!HONOR_SIGNED_ZEROS (@0))
4679 (convert (lt @0 { build_real (TREE_TYPE (@0), dconst0); }))))
4681 /* Transform comparisons of the form X +- C1 CMP C2 to X CMP C2 -+ C1. */
4683 (for op (plus minus)
4686 (cmp (op@3 @0 INTEGER_CST@1) INTEGER_CST@2)
4687 (if (!TREE_OVERFLOW (@1) && !TREE_OVERFLOW (@2)
4688 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0))
4689 && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@0))
4690 && !TYPE_SATURATING (TREE_TYPE (@0)))
4691 (with { tree res = int_const_binop (rop, @2, @1); }
4692 (if (TREE_OVERFLOW (res)
4693 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
4694 { constant_boolean_node (cmp == NE_EXPR, type); }
4695 (if (single_use (@3))
4696 (cmp @0 { TREE_OVERFLOW (res)
4697 ? drop_tree_overflow (res) : res; }))))))))
4698 (for cmp (lt le gt ge)
4699 (for op (plus minus)
4702 (cmp (op@3 @0 INTEGER_CST@1) INTEGER_CST@2)
4703 (if (!TREE_OVERFLOW (@1) && !TREE_OVERFLOW (@2)
4704 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
4705 (with { tree res = int_const_binop (rop, @2, @1); }
4706 (if (TREE_OVERFLOW (res))
4708 fold_overflow_warning (("assuming signed overflow does not occur "
4709 "when simplifying conditional to constant"),
4710 WARN_STRICT_OVERFLOW_CONDITIONAL);
4711 bool less = cmp == LE_EXPR || cmp == LT_EXPR;
4712 /* wi::ges_p (@2, 0) should be sufficient for a signed type. */
4713 bool ovf_high = wi::lt_p (wi::to_wide (@1), 0,
4714 TYPE_SIGN (TREE_TYPE (@1)))
4715 != (op == MINUS_EXPR);
4716 constant_boolean_node (less == ovf_high, type);
4718 (if (single_use (@3))
4721 fold_overflow_warning (("assuming signed overflow does not occur "
4722 "when changing X +- C1 cmp C2 to "
4724 WARN_STRICT_OVERFLOW_COMPARISON);
4726 (cmp @0 { res; })))))))))
4728 /* Canonicalizations of BIT_FIELD_REFs. */
4731 (BIT_FIELD_REF (BIT_FIELD_REF @0 @1 @2) @3 @4)
4732 (BIT_FIELD_REF @0 @3 { const_binop (PLUS_EXPR, bitsizetype, @2, @4); }))
4735 (BIT_FIELD_REF (view_convert @0) @1 @2)
4736 (BIT_FIELD_REF @0 @1 @2))
4739 (BIT_FIELD_REF @0 @1 integer_zerop)
4740 (if (tree_int_cst_equal (@1, TYPE_SIZE (TREE_TYPE (@0))))
4744 (BIT_FIELD_REF @0 @1 @2)
4746 (if (TREE_CODE (TREE_TYPE (@0)) == COMPLEX_TYPE
4747 && tree_int_cst_equal (@1, TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))))
4749 (if (integer_zerop (@2))
4750 (view_convert (realpart @0)))
4751 (if (tree_int_cst_equal (@2, TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))))
4752 (view_convert (imagpart @0)))))
4753 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
4754 && INTEGRAL_TYPE_P (type)
4755 /* On GIMPLE this should only apply to register arguments. */
4756 && (! GIMPLE || is_gimple_reg (@0))
4757 /* A bit-field-ref that referenced the full argument can be stripped. */
4758 && ((compare_tree_int (@1, TYPE_PRECISION (TREE_TYPE (@0))) == 0
4759 && integer_zerop (@2))
4760 /* Low-parts can be reduced to integral conversions.
4761 ??? The following doesn't work for PDP endian. */
4762 || (BYTES_BIG_ENDIAN == WORDS_BIG_ENDIAN
4763 /* Don't even think about BITS_BIG_ENDIAN. */
4764 && TYPE_PRECISION (TREE_TYPE (@0)) % BITS_PER_UNIT == 0
4765 && TYPE_PRECISION (type) % BITS_PER_UNIT == 0
4766 && compare_tree_int (@2, (BYTES_BIG_ENDIAN
4767 ? (TYPE_PRECISION (TREE_TYPE (@0))
4768 - TYPE_PRECISION (type))
4772 /* Simplify vector extracts. */
4775 (BIT_FIELD_REF CONSTRUCTOR@0 @1 @2)
4776 (if (VECTOR_TYPE_P (TREE_TYPE (@0))
4777 && (types_match (type, TREE_TYPE (TREE_TYPE (@0)))
4778 || (VECTOR_TYPE_P (type)
4779 && types_match (TREE_TYPE (type), TREE_TYPE (TREE_TYPE (@0))))))
4782 tree ctor = (TREE_CODE (@0) == SSA_NAME
4783 ? gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0)) : @0);
4784 tree eltype = TREE_TYPE (TREE_TYPE (ctor));
4785 unsigned HOST_WIDE_INT width = tree_to_uhwi (TYPE_SIZE (eltype));
4786 unsigned HOST_WIDE_INT n = tree_to_uhwi (@1);
4787 unsigned HOST_WIDE_INT idx = tree_to_uhwi (@2);
4790 && (idx % width) == 0
4792 && known_le ((idx + n) / width,
4793 TYPE_VECTOR_SUBPARTS (TREE_TYPE (ctor))))
4798 /* Constructor elements can be subvectors. */
4800 if (CONSTRUCTOR_NELTS (ctor) != 0)
4802 tree cons_elem = TREE_TYPE (CONSTRUCTOR_ELT (ctor, 0)->value);
4803 if (TREE_CODE (cons_elem) == VECTOR_TYPE)
4804 k = TYPE_VECTOR_SUBPARTS (cons_elem);
4806 unsigned HOST_WIDE_INT elt, count, const_k;
4809 /* We keep an exact subset of the constructor elements. */
4810 (if (multiple_p (idx, k, &elt) && multiple_p (n, k, &count))
4811 (if (CONSTRUCTOR_NELTS (ctor) == 0)
4812 { build_constructor (type, NULL); }
4814 (if (elt < CONSTRUCTOR_NELTS (ctor))
4815 (view_convert { CONSTRUCTOR_ELT (ctor, elt)->value; })
4816 { build_zero_cst (type); })
4818 vec<constructor_elt, va_gc> *vals;
4819 vec_alloc (vals, count);
4820 for (unsigned i = 0;
4821 i < count && elt + i < CONSTRUCTOR_NELTS (ctor); ++i)
4822 CONSTRUCTOR_APPEND_ELT (vals, NULL_TREE,
4823 CONSTRUCTOR_ELT (ctor, elt + i)->value);
4824 build_constructor (type, vals);
4826 /* The bitfield references a single constructor element. */
4827 (if (k.is_constant (&const_k)
4828 && idx + n <= (idx / const_k + 1) * const_k)
4830 (if (CONSTRUCTOR_NELTS (ctor) <= idx / const_k)
4831 { build_zero_cst (type); })
4833 (view_convert { CONSTRUCTOR_ELT (ctor, idx / const_k)->value; }))
4834 (BIT_FIELD_REF { CONSTRUCTOR_ELT (ctor, idx / const_k)->value; }
4835 @1 { bitsize_int ((idx % const_k) * width); })))))))))
4837 /* Simplify a bit extraction from a bit insertion for the cases with
4838 the inserted element fully covering the extraction or the insertion
4839 not touching the extraction. */
4841 (BIT_FIELD_REF (bit_insert @0 @1 @ipos) @rsize @rpos)
4844 unsigned HOST_WIDE_INT isize;
4845 if (INTEGRAL_TYPE_P (TREE_TYPE (@1)))
4846 isize = TYPE_PRECISION (TREE_TYPE (@1));
4848 isize = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (@1)));
4851 (if (wi::leu_p (wi::to_wide (@ipos), wi::to_wide (@rpos))
4852 && wi::leu_p (wi::to_wide (@rpos) + wi::to_wide (@rsize),
4853 wi::to_wide (@ipos) + isize))
4854 (BIT_FIELD_REF @1 @rsize { wide_int_to_tree (bitsizetype,
4856 - wi::to_wide (@ipos)); }))
4857 (if (wi::geu_p (wi::to_wide (@ipos),
4858 wi::to_wide (@rpos) + wi::to_wide (@rsize))
4859 || wi::geu_p (wi::to_wide (@rpos),
4860 wi::to_wide (@ipos) + isize))
4861 (BIT_FIELD_REF @0 @rsize @rpos)))))
4863 (if (canonicalize_math_after_vectorization_p ())
4866 (fmas:c (negate @0) @1 @2)
4867 (IFN_FNMA @0 @1 @2))
4869 (fmas @0 @1 (negate @2))
4872 (fmas:c (negate @0) @1 (negate @2))
4873 (IFN_FNMS @0 @1 @2))
4875 (negate (fmas@3 @0 @1 @2))
4876 (if (single_use (@3))
4877 (IFN_FNMS @0 @1 @2))))
4880 (IFN_FMS:c (negate @0) @1 @2)
4881 (IFN_FNMS @0 @1 @2))
4883 (IFN_FMS @0 @1 (negate @2))
4886 (IFN_FMS:c (negate @0) @1 (negate @2))
4887 (IFN_FNMA @0 @1 @2))
4889 (negate (IFN_FMS@3 @0 @1 @2))
4890 (if (single_use (@3))
4891 (IFN_FNMA @0 @1 @2)))
4894 (IFN_FNMA:c (negate @0) @1 @2)
4897 (IFN_FNMA @0 @1 (negate @2))
4898 (IFN_FNMS @0 @1 @2))
4900 (IFN_FNMA:c (negate @0) @1 (negate @2))
4903 (negate (IFN_FNMA@3 @0 @1 @2))
4904 (if (single_use (@3))
4905 (IFN_FMS @0 @1 @2)))
4908 (IFN_FNMS:c (negate @0) @1 @2)
4911 (IFN_FNMS @0 @1 (negate @2))
4912 (IFN_FNMA @0 @1 @2))
4914 (IFN_FNMS:c (negate @0) @1 (negate @2))
4917 (negate (IFN_FNMS@3 @0 @1 @2))
4918 (if (single_use (@3))
4919 (IFN_FMA @0 @1 @2))))
4921 /* POPCOUNT simplifications. */
4922 (for popcount (BUILT_IN_POPCOUNT BUILT_IN_POPCOUNTL BUILT_IN_POPCOUNTLL
4923 BUILT_IN_POPCOUNTIMAX)
4924 /* popcount(X&1) is nop_expr(X&1). */
4927 (if (tree_nonzero_bits (@0) == 1)
4929 /* popcount(X) + popcount(Y) is popcount(X|Y) when X&Y must be zero. */
4931 (plus (popcount:s @0) (popcount:s @1))
4932 (if (wi::bit_and (tree_nonzero_bits (@0), tree_nonzero_bits (@1)) == 0)
4933 (popcount (bit_ior @0 @1))))
4934 /* popcount(X) == 0 is X == 0, and related (in)equalities. */
4935 (for cmp (le eq ne gt)
4938 (cmp (popcount @0) integer_zerop)
4939 (rep @0 { build_zero_cst (TREE_TYPE (@0)); }))))
4948 r = c ? a1 op a2 : b;
4950 if the target can do it in one go. This makes the operation conditional
4951 on c, so could drop potentially-trapping arithmetic, but that's a valid
4952 simplification if the result of the operation isn't needed. */
4953 (for uncond_op (UNCOND_BINARY)
4954 cond_op (COND_BINARY)
4956 (vec_cond @0 (view_convert? (uncond_op@4 @1 @2)) @3)
4957 (with { tree op_type = TREE_TYPE (@4); }
4958 (if (element_precision (type) == element_precision (op_type))
4959 (view_convert (cond_op @0 @1 @2 (view_convert:op_type @3))))))
4961 (vec_cond @0 @1 (view_convert? (uncond_op@4 @2 @3)))
4962 (with { tree op_type = TREE_TYPE (@4); }
4963 (if (element_precision (type) == element_precision (op_type))
4964 (view_convert (cond_op (bit_not @0) @2 @3 (view_convert:op_type @1)))))))
4966 /* Same for ternary operations. */
4967 (for uncond_op (UNCOND_TERNARY)
4968 cond_op (COND_TERNARY)
4970 (vec_cond @0 (view_convert? (uncond_op@5 @1 @2 @3)) @4)
4971 (with { tree op_type = TREE_TYPE (@5); }
4972 (if (element_precision (type) == element_precision (op_type))
4973 (view_convert (cond_op @0 @1 @2 @3 (view_convert:op_type @4))))))
4975 (vec_cond @0 @1 (view_convert? (uncond_op@5 @2 @3 @4)))
4976 (with { tree op_type = TREE_TYPE (@5); }
4977 (if (element_precision (type) == element_precision (op_type))
4978 (view_convert (cond_op (bit_not @0) @2 @3 @4
4979 (view_convert:op_type @1)))))))
4981 /* Detect cases in which a VEC_COND_EXPR effectively replaces the
4982 "else" value of an IFN_COND_*. */
4983 (for cond_op (COND_BINARY)
4985 (vec_cond @0 (view_convert? (cond_op @0 @1 @2 @3)) @4)
4986 (with { tree op_type = TREE_TYPE (@3); }
4987 (if (element_precision (type) == element_precision (op_type))
4988 (view_convert (cond_op @0 @1 @2 (view_convert:op_type @4))))))
4990 (vec_cond @0 @1 (view_convert? (cond_op @2 @3 @4 @5)))
4991 (with { tree op_type = TREE_TYPE (@5); }
4992 (if (inverse_conditions_p (@0, @2)
4993 && element_precision (type) == element_precision (op_type))
4994 (view_convert (cond_op @2 @3 @4 (view_convert:op_type @1)))))))
4996 /* Same for ternary operations. */
4997 (for cond_op (COND_TERNARY)
4999 (vec_cond @0 (view_convert? (cond_op @0 @1 @2 @3 @4)) @5)
5000 (with { tree op_type = TREE_TYPE (@4); }
5001 (if (element_precision (type) == element_precision (op_type))
5002 (view_convert (cond_op @0 @1 @2 @3 (view_convert:op_type @5))))))
5004 (vec_cond @0 @1 (view_convert? (cond_op @2 @3 @4 @5 @6)))
5005 (with { tree op_type = TREE_TYPE (@6); }
5006 (if (inverse_conditions_p (@0, @2)
5007 && element_precision (type) == element_precision (op_type))
5008 (view_convert (cond_op @2 @3 @4 @5 (view_convert:op_type @1)))))))
5010 /* For pointers @0 and @2 and nonnegative constant offset @1, look for
5013 A: (@0 + @1 < @2) | (@2 + @1 < @0)
5014 B: (@0 + @1 <= @2) | (@2 + @1 <= @0)
5016 If pointers are known not to wrap, B checks whether @1 bytes starting
5017 at @0 and @2 do not overlap, while A tests the same thing for @1 + 1
5018 bytes. A is more efficiently tested as:
5020 A: (sizetype) (@0 + @1 - @2) > @1 * 2
5022 The equivalent expression for B is given by replacing @1 with @1 - 1:
5024 B: (sizetype) (@0 + (@1 - 1) - @2) > (@1 - 1) * 2
5026 @0 and @2 can be swapped in both expressions without changing the result.
5028 The folds rely on sizetype's being unsigned (which is always true)
5029 and on its being the same width as the pointer (which we have to check).
5031 The fold replaces two pointer_plus expressions, two comparisons and
5032 an IOR with a pointer_plus, a pointer_diff, and a comparison, so in
5033 the best case it's a saving of two operations. The A fold retains one
5034 of the original pointer_pluses, so is a win even if both pointer_pluses
5035 are used elsewhere. The B fold is a wash if both pointer_pluses are
5036 used elsewhere, since all we end up doing is replacing a comparison with
5037 a pointer_plus. We do still apply the fold under those circumstances
5038 though, in case applying it to other conditions eventually makes one of the
5039 pointer_pluses dead. */
5040 (for ior (truth_orif truth_or bit_ior)
5043 (ior (cmp:cs (pointer_plus@3 @0 INTEGER_CST@1) @2)
5044 (cmp:cs (pointer_plus@4 @2 @1) @0))
5045 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
5046 && TYPE_OVERFLOW_WRAPS (sizetype)
5047 && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (sizetype))
5048 /* Calculate the rhs constant. */
5049 (with { offset_int off = wi::to_offset (@1) - (cmp == LE_EXPR ? 1 : 0);
5050 offset_int rhs = off * 2; }
5051 /* Always fails for negative values. */
5052 (if (wi::min_precision (rhs, UNSIGNED) <= TYPE_PRECISION (sizetype))
5053 /* Since the order of @0 and @2 doesn't matter, let tree_swap_operands_p
5054 pick a canonical order. This increases the chances of using the
5055 same pointer_plus in multiple checks. */
5056 (with { bool swap_p = tree_swap_operands_p (@0, @2);
5057 tree rhs_tree = wide_int_to_tree (sizetype, rhs); }
5058 (if (cmp == LT_EXPR)
5059 (gt (convert:sizetype
5060 (pointer_diff:ssizetype { swap_p ? @4 : @3; }
5061 { swap_p ? @0 : @2; }))
5063 (gt (convert:sizetype
5064 (pointer_diff:ssizetype
5065 (pointer_plus { swap_p ? @2 : @0; }
5066 { wide_int_to_tree (sizetype, off); })
5067 { swap_p ? @0 : @2; }))
5068 { rhs_tree; })))))))))