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 /* As opposed to convert?, this still creates a single pattern, so
91 it is not a suitable replacement for convert? in all cases. */
92 (match (nop_convert @0)
94 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))))
95 (match (nop_convert @0)
97 (if (VECTOR_TYPE_P (type) && VECTOR_TYPE_P (TREE_TYPE (@0))
98 && known_eq (TYPE_VECTOR_SUBPARTS (type),
99 TYPE_VECTOR_SUBPARTS (TREE_TYPE (@0)))
100 && tree_nop_conversion_p (TREE_TYPE (type), TREE_TYPE (TREE_TYPE (@0))))))
101 /* This one has to be last, or it shadows the others. */
102 (match (nop_convert @0)
105 /* Transform likes of (char) ABS_EXPR <(int) x> into (char) ABSU_EXPR <x>
106 ABSU_EXPR returns unsigned absolute value of the operand and the operand
107 of the ABSU_EXPR will have the corresponding signed type. */
108 (simplify (abs (convert @0))
109 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
110 && !TYPE_UNSIGNED (TREE_TYPE (@0))
111 && element_precision (type) > element_precision (TREE_TYPE (@0)))
112 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
113 (convert (absu:utype @0)))))
116 /* Simplifications of operations with one constant operand and
117 simplifications to constants or single values. */
119 (for op (plus pointer_plus minus bit_ior bit_xor)
121 (op @0 integer_zerop)
124 /* 0 +p index -> (type)index */
126 (pointer_plus integer_zerop @1)
127 (non_lvalue (convert @1)))
129 /* ptr - 0 -> (type)ptr */
131 (pointer_diff @0 integer_zerop)
134 /* See if ARG1 is zero and X + ARG1 reduces to X.
135 Likewise if the operands are reversed. */
137 (plus:c @0 real_zerop@1)
138 (if (fold_real_zero_addition_p (type, @1, 0))
141 /* See if ARG1 is zero and X - ARG1 reduces to X. */
143 (minus @0 real_zerop@1)
144 (if (fold_real_zero_addition_p (type, @1, 1))
148 This is unsafe for certain floats even in non-IEEE formats.
149 In IEEE, it is unsafe because it does wrong for NaNs.
150 Also note that operand_equal_p is always false if an operand
154 (if (!FLOAT_TYPE_P (type) || !HONOR_NANS (type))
155 { build_zero_cst (type); }))
157 (pointer_diff @@0 @0)
158 { build_zero_cst (type); })
161 (mult @0 integer_zerop@1)
164 /* Maybe fold x * 0 to 0. The expressions aren't the same
165 when x is NaN, since x * 0 is also NaN. Nor are they the
166 same in modes with signed zeros, since multiplying a
167 negative value by 0 gives -0, not +0. */
169 (mult @0 real_zerop@1)
170 (if (!HONOR_NANS (type) && !HONOR_SIGNED_ZEROS (type))
173 /* In IEEE floating point, x*1 is not equivalent to x for snans.
174 Likewise for complex arithmetic with signed zeros. */
177 (if (!HONOR_SNANS (type)
178 && (!HONOR_SIGNED_ZEROS (type)
179 || !COMPLEX_FLOAT_TYPE_P (type)))
182 /* Transform x * -1.0 into -x. */
184 (mult @0 real_minus_onep)
185 (if (!HONOR_SNANS (type)
186 && (!HONOR_SIGNED_ZEROS (type)
187 || !COMPLEX_FLOAT_TYPE_P (type)))
190 (for cmp (gt ge lt le)
191 outp (convert convert negate negate)
192 outn (negate negate convert convert)
193 /* Transform (X > 0.0 ? 1.0 : -1.0) into copysign(1, X). */
194 /* Transform (X >= 0.0 ? 1.0 : -1.0) into copysign(1, X). */
195 /* Transform (X < 0.0 ? 1.0 : -1.0) into copysign(1,-X). */
196 /* Transform (X <= 0.0 ? 1.0 : -1.0) into copysign(1,-X). */
198 (cond (cmp @0 real_zerop) real_onep@1 real_minus_onep)
199 (if (!HONOR_NANS (type) && !HONOR_SIGNED_ZEROS (type)
200 && types_match (type, TREE_TYPE (@0)))
202 (if (types_match (type, float_type_node))
203 (BUILT_IN_COPYSIGNF @1 (outp @0)))
204 (if (types_match (type, double_type_node))
205 (BUILT_IN_COPYSIGN @1 (outp @0)))
206 (if (types_match (type, long_double_type_node))
207 (BUILT_IN_COPYSIGNL @1 (outp @0))))))
208 /* Transform (X > 0.0 ? -1.0 : 1.0) into copysign(1,-X). */
209 /* Transform (X >= 0.0 ? -1.0 : 1.0) into copysign(1,-X). */
210 /* Transform (X < 0.0 ? -1.0 : 1.0) into copysign(1,X). */
211 /* Transform (X <= 0.0 ? -1.0 : 1.0) into copysign(1,X). */
213 (cond (cmp @0 real_zerop) real_minus_onep real_onep@1)
214 (if (!HONOR_NANS (type) && !HONOR_SIGNED_ZEROS (type)
215 && types_match (type, TREE_TYPE (@0)))
217 (if (types_match (type, float_type_node))
218 (BUILT_IN_COPYSIGNF @1 (outn @0)))
219 (if (types_match (type, double_type_node))
220 (BUILT_IN_COPYSIGN @1 (outn @0)))
221 (if (types_match (type, long_double_type_node))
222 (BUILT_IN_COPYSIGNL @1 (outn @0)))))))
224 /* Transform X * copysign (1.0, X) into abs(X). */
226 (mult:c @0 (COPYSIGN_ALL real_onep @0))
227 (if (!HONOR_NANS (type) && !HONOR_SIGNED_ZEROS (type))
230 /* Transform X * copysign (1.0, -X) into -abs(X). */
232 (mult:c @0 (COPYSIGN_ALL real_onep (negate @0)))
233 (if (!HONOR_NANS (type) && !HONOR_SIGNED_ZEROS (type))
236 /* Transform copysign (CST, X) into copysign (ABS(CST), X). */
238 (COPYSIGN_ALL REAL_CST@0 @1)
239 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@0)))
240 (COPYSIGN_ALL (negate @0) @1)))
242 /* X * 1, X / 1 -> X. */
243 (for op (mult trunc_div ceil_div floor_div round_div exact_div)
248 /* (A / (1 << B)) -> (A >> B).
249 Only for unsigned A. For signed A, this would not preserve rounding
251 For example: (-1 / ( 1 << B)) != -1 >> B. */
253 (trunc_div @0 (lshift integer_onep@1 @2))
254 (if ((TYPE_UNSIGNED (type) || tree_expr_nonnegative_p (@0))
255 && (!VECTOR_TYPE_P (type)
256 || target_supports_op_p (type, RSHIFT_EXPR, optab_vector)
257 || target_supports_op_p (type, RSHIFT_EXPR, optab_scalar)))
260 /* Preserve explicit divisions by 0: the C++ front-end wants to detect
261 undefined behavior in constexpr evaluation, and assuming that the division
262 traps enables better optimizations than these anyway. */
263 (for div (trunc_div ceil_div floor_div round_div exact_div)
264 /* 0 / X is always zero. */
266 (div integer_zerop@0 @1)
267 /* But not for 0 / 0 so that we can get the proper warnings and errors. */
268 (if (!integer_zerop (@1))
272 (div @0 integer_minus_onep@1)
273 (if (!TYPE_UNSIGNED (type))
278 /* But not for 0 / 0 so that we can get the proper warnings and errors.
279 And not for _Fract types where we can't build 1. */
280 (if (!integer_zerop (@0) && !ALL_FRACT_MODE_P (TYPE_MODE (type)))
281 { build_one_cst (type); }))
282 /* X / abs (X) is X < 0 ? -1 : 1. */
285 (if (INTEGRAL_TYPE_P (type)
286 && TYPE_OVERFLOW_UNDEFINED (type))
287 (cond (lt @0 { build_zero_cst (type); })
288 { build_minus_one_cst (type); } { build_one_cst (type); })))
291 (div:C @0 (negate @0))
292 (if ((INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
293 && TYPE_OVERFLOW_UNDEFINED (type))
294 { build_minus_one_cst (type); })))
296 /* For unsigned integral types, FLOOR_DIV_EXPR is the same as
297 TRUNC_DIV_EXPR. Rewrite into the latter in this case. */
300 (if ((INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
301 && TYPE_UNSIGNED (type))
304 /* Combine two successive divisions. Note that combining ceil_div
305 and floor_div is trickier and combining round_div even more so. */
306 (for div (trunc_div exact_div)
308 (div (div @0 INTEGER_CST@1) INTEGER_CST@2)
310 wi::overflow_type overflow;
311 wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@2),
312 TYPE_SIGN (type), &overflow);
315 (div @0 { wide_int_to_tree (type, mul); })
316 (if (TYPE_UNSIGNED (type)
317 || mul != wi::min_value (TYPE_PRECISION (type), SIGNED))
318 { build_zero_cst (type); })))))
320 /* Combine successive multiplications. Similar to above, but handling
321 overflow is different. */
323 (mult (mult @0 INTEGER_CST@1) INTEGER_CST@2)
325 wi::overflow_type overflow;
326 wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@2),
327 TYPE_SIGN (type), &overflow);
329 /* Skip folding on overflow: the only special case is @1 * @2 == -INT_MIN,
330 otherwise undefined overflow implies that @0 must be zero. */
331 (if (!overflow || TYPE_OVERFLOW_WRAPS (type))
332 (mult @0 { wide_int_to_tree (type, mul); }))))
334 /* Optimize A / A to 1.0 if we don't care about
335 NaNs or Infinities. */
338 (if (FLOAT_TYPE_P (type)
339 && ! HONOR_NANS (type)
340 && ! HONOR_INFINITIES (type))
341 { build_one_cst (type); }))
343 /* Optimize -A / A to -1.0 if we don't care about
344 NaNs or Infinities. */
346 (rdiv:C @0 (negate @0))
347 (if (FLOAT_TYPE_P (type)
348 && ! HONOR_NANS (type)
349 && ! HONOR_INFINITIES (type))
350 { build_minus_one_cst (type); }))
352 /* PR71078: x / abs(x) -> copysign (1.0, x) */
354 (rdiv:C (convert? @0) (convert? (abs @0)))
355 (if (SCALAR_FLOAT_TYPE_P (type)
356 && ! HONOR_NANS (type)
357 && ! HONOR_INFINITIES (type))
359 (if (types_match (type, float_type_node))
360 (BUILT_IN_COPYSIGNF { build_one_cst (type); } (convert @0)))
361 (if (types_match (type, double_type_node))
362 (BUILT_IN_COPYSIGN { build_one_cst (type); } (convert @0)))
363 (if (types_match (type, long_double_type_node))
364 (BUILT_IN_COPYSIGNL { build_one_cst (type); } (convert @0))))))
366 /* In IEEE floating point, x/1 is not equivalent to x for snans. */
369 (if (!HONOR_SNANS (type))
372 /* In IEEE floating point, x/-1 is not equivalent to -x for snans. */
374 (rdiv @0 real_minus_onep)
375 (if (!HONOR_SNANS (type))
378 (if (flag_reciprocal_math)
379 /* Convert (A/B)/C to A/(B*C). */
381 (rdiv (rdiv:s @0 @1) @2)
382 (rdiv @0 (mult @1 @2)))
384 /* Canonicalize x / (C1 * y) to (x * C2) / y. */
386 (rdiv @0 (mult:s @1 REAL_CST@2))
388 { tree tem = const_binop (RDIV_EXPR, type, build_one_cst (type), @2); }
390 (rdiv (mult @0 { tem; } ) @1))))
392 /* Convert A/(B/C) to (A/B)*C */
394 (rdiv @0 (rdiv:s @1 @2))
395 (mult (rdiv @0 @1) @2)))
397 /* Simplify x / (- y) to -x / y. */
399 (rdiv @0 (negate @1))
400 (rdiv (negate @0) @1))
402 /* Optimize (X & (-A)) / A where A is a power of 2, to X >> log2(A) */
403 (for div (trunc_div ceil_div floor_div round_div exact_div)
405 (div (convert? (bit_and @0 INTEGER_CST@1)) INTEGER_CST@2)
406 (if (integer_pow2p (@2)
407 && tree_int_cst_sgn (@2) > 0
408 && tree_nop_conversion_p (type, TREE_TYPE (@0))
409 && wi::to_wide (@2) + wi::to_wide (@1) == 0)
411 { build_int_cst (integer_type_node,
412 wi::exact_log2 (wi::to_wide (@2))); }))))
414 /* If ARG1 is a constant, we can convert this to a multiply by the
415 reciprocal. This does not have the same rounding properties,
416 so only do this if -freciprocal-math. We can actually
417 always safely do it if ARG1 is a power of two, but it's hard to
418 tell if it is or not in a portable manner. */
419 (for cst (REAL_CST COMPLEX_CST VECTOR_CST)
423 (if (flag_reciprocal_math
426 { tree tem = const_binop (RDIV_EXPR, type, build_one_cst (type), @1); }
428 (mult @0 { tem; } )))
429 (if (cst != COMPLEX_CST)
430 (with { tree inverse = exact_inverse (type, @1); }
432 (mult @0 { inverse; } ))))))))
434 (for mod (ceil_mod floor_mod round_mod trunc_mod)
435 /* 0 % X is always zero. */
437 (mod integer_zerop@0 @1)
438 /* But not for 0 % 0 so that we can get the proper warnings and errors. */
439 (if (!integer_zerop (@1))
441 /* X % 1 is always zero. */
443 (mod @0 integer_onep)
444 { build_zero_cst (type); })
445 /* X % -1 is zero. */
447 (mod @0 integer_minus_onep@1)
448 (if (!TYPE_UNSIGNED (type))
449 { build_zero_cst (type); }))
453 /* But not for 0 % 0 so that we can get the proper warnings and errors. */
454 (if (!integer_zerop (@0))
455 { build_zero_cst (type); }))
456 /* (X % Y) % Y is just X % Y. */
458 (mod (mod@2 @0 @1) @1)
460 /* From extract_muldiv_1: (X * C1) % C2 is zero if C1 is a multiple of C2. */
462 (mod (mult @0 INTEGER_CST@1) INTEGER_CST@2)
463 (if (ANY_INTEGRAL_TYPE_P (type)
464 && TYPE_OVERFLOW_UNDEFINED (type)
465 && wi::multiple_of_p (wi::to_wide (@1), wi::to_wide (@2),
467 { build_zero_cst (type); })))
469 /* X % -C is the same as X % C. */
471 (trunc_mod @0 INTEGER_CST@1)
472 (if (TYPE_SIGN (type) == SIGNED
473 && !TREE_OVERFLOW (@1)
474 && wi::neg_p (wi::to_wide (@1))
475 && !TYPE_OVERFLOW_TRAPS (type)
476 /* Avoid this transformation if C is INT_MIN, i.e. C == -C. */
477 && !sign_bit_p (@1, @1))
478 (trunc_mod @0 (negate @1))))
480 /* X % -Y is the same as X % Y. */
482 (trunc_mod @0 (convert? (negate @1)))
483 (if (INTEGRAL_TYPE_P (type)
484 && !TYPE_UNSIGNED (type)
485 && !TYPE_OVERFLOW_TRAPS (type)
486 && tree_nop_conversion_p (type, TREE_TYPE (@1))
487 /* Avoid this transformation if X might be INT_MIN or
488 Y might be -1, because we would then change valid
489 INT_MIN % -(-1) into invalid INT_MIN % -1. */
490 && (expr_not_equal_to (@0, wi::to_wide (TYPE_MIN_VALUE (type)))
491 || expr_not_equal_to (@1, wi::minus_one (TYPE_PRECISION
493 (trunc_mod @0 (convert @1))))
495 /* X - (X / Y) * Y is the same as X % Y. */
497 (minus (convert1? @0) (convert2? (mult:c (trunc_div @@0 @@1) @1)))
498 (if (INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
499 (convert (trunc_mod @0 @1))))
501 /* Optimize TRUNC_MOD_EXPR by a power of two into a BIT_AND_EXPR,
502 i.e. "X % C" into "X & (C - 1)", if X and C are positive.
503 Also optimize A % (C << N) where C is a power of 2,
504 to A & ((C << N) - 1). */
505 (match (power_of_two_cand @1)
507 (match (power_of_two_cand @1)
508 (lshift INTEGER_CST@1 @2))
509 (for mod (trunc_mod floor_mod)
511 (mod @0 (convert?@3 (power_of_two_cand@1 @2)))
512 (if ((TYPE_UNSIGNED (type)
513 || tree_expr_nonnegative_p (@0))
514 && tree_nop_conversion_p (type, TREE_TYPE (@3))
515 && integer_pow2p (@2) && tree_int_cst_sgn (@2) > 0)
516 (bit_and @0 (convert (minus @1 { build_int_cst (TREE_TYPE (@1), 1); }))))))
518 /* Simplify (unsigned t * 2)/2 -> unsigned t & 0x7FFFFFFF. */
520 (trunc_div (mult @0 integer_pow2p@1) @1)
521 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
522 (bit_and @0 { wide_int_to_tree
523 (type, wi::mask (TYPE_PRECISION (type)
524 - wi::exact_log2 (wi::to_wide (@1)),
525 false, TYPE_PRECISION (type))); })))
527 /* Simplify (unsigned t / 2) * 2 -> unsigned t & ~1. */
529 (mult (trunc_div @0 integer_pow2p@1) @1)
530 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
531 (bit_and @0 (negate @1))))
533 /* Simplify (t * 2) / 2) -> t. */
534 (for div (trunc_div ceil_div floor_div round_div exact_div)
536 (div (mult:c @0 @1) @1)
537 (if (ANY_INTEGRAL_TYPE_P (type)
538 && TYPE_OVERFLOW_UNDEFINED (type))
542 /* Simplify cos(-x) and cos(|x|) -> cos(x). Similarly for cosh. */
547 /* Simplify pow(-x, y) and pow(|x|,y) -> pow(x,y) if y is an even integer. */
550 (pows (op @0) REAL_CST@1)
551 (with { HOST_WIDE_INT n; }
552 (if (real_isinteger (&TREE_REAL_CST (@1), &n) && (n & 1) == 0)
554 /* Likewise for powi. */
557 (pows (op @0) INTEGER_CST@1)
558 (if ((wi::to_wide (@1) & 1) == 0)
560 /* Strip negate and abs from both operands of hypot. */
568 /* copysign(-x, y) and copysign(abs(x), y) -> copysign(x, y). */
569 (for copysigns (COPYSIGN_ALL)
571 (copysigns (op @0) @1)
574 /* abs(x)*abs(x) -> x*x. Should be valid for all types. */
579 /* cos(copysign(x, y)) -> cos(x). Similarly for cosh. */
583 (coss (copysigns @0 @1))
586 /* pow(copysign(x, y), z) -> pow(x, z) if z is an even integer. */
590 (pows (copysigns @0 @2) REAL_CST@1)
591 (with { HOST_WIDE_INT n; }
592 (if (real_isinteger (&TREE_REAL_CST (@1), &n) && (n & 1) == 0)
594 /* Likewise for powi. */
598 (pows (copysigns @0 @2) INTEGER_CST@1)
599 (if ((wi::to_wide (@1) & 1) == 0)
604 /* hypot(copysign(x, y), z) -> hypot(x, z). */
606 (hypots (copysigns @0 @1) @2)
608 /* hypot(x, copysign(y, z)) -> hypot(x, y). */
610 (hypots @0 (copysigns @1 @2))
613 /* copysign(x, CST) -> [-]abs (x). */
614 (for copysigns (COPYSIGN_ALL)
616 (copysigns @0 REAL_CST@1)
617 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
621 /* copysign(copysign(x, y), z) -> copysign(x, z). */
622 (for copysigns (COPYSIGN_ALL)
624 (copysigns (copysigns @0 @1) @2)
627 /* copysign(x,y)*copysign(x,y) -> x*x. */
628 (for copysigns (COPYSIGN_ALL)
630 (mult (copysigns@2 @0 @1) @2)
633 /* ccos(-x) -> ccos(x). Similarly for ccosh. */
634 (for ccoss (CCOS CCOSH)
639 /* cabs(-x) and cos(conj(x)) -> cabs(x). */
640 (for ops (conj negate)
646 /* Fold (a * (1 << b)) into (a << b) */
648 (mult:c @0 (convert? (lshift integer_onep@1 @2)))
649 (if (! FLOAT_TYPE_P (type)
650 && tree_nop_conversion_p (type, TREE_TYPE (@1)))
653 /* Fold (1 << (C - x)) where C = precision(type) - 1
654 into ((1 << C) >> x). */
656 (lshift integer_onep@0 (minus@1 INTEGER_CST@2 @3))
657 (if (INTEGRAL_TYPE_P (type)
658 && wi::eq_p (wi::to_wide (@2), TYPE_PRECISION (type) - 1)
660 (if (TYPE_UNSIGNED (type))
661 (rshift (lshift @0 @2) @3)
663 { tree utype = unsigned_type_for (type); }
664 (convert (rshift (lshift (convert:utype @0) @2) @3))))))
666 /* Fold (C1/X)*C2 into (C1*C2)/X. */
668 (mult (rdiv@3 REAL_CST@0 @1) REAL_CST@2)
669 (if (flag_associative_math
672 { tree tem = const_binop (MULT_EXPR, type, @0, @2); }
674 (rdiv { tem; } @1)))))
676 /* Simplify ~X & X as zero. */
678 (bit_and:c (convert? @0) (convert? (bit_not @0)))
679 { build_zero_cst (type); })
681 /* PR71636: Transform x & ((1U << b) - 1) -> x & ~(~0U << b); */
683 (bit_and:c @0 (plus:s (lshift:s integer_onep @1) integer_minus_onep))
684 (if (TYPE_UNSIGNED (type))
685 (bit_and @0 (bit_not (lshift { build_all_ones_cst (type); } @1)))))
687 (for bitop (bit_and bit_ior)
689 /* PR35691: Transform
690 (x == 0 & y == 0) -> (x | typeof(x)(y)) == 0.
691 (x != 0 | y != 0) -> (x | typeof(x)(y)) != 0. */
693 (bitop (cmp @0 integer_zerop@2) (cmp @1 integer_zerop))
694 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
695 && INTEGRAL_TYPE_P (TREE_TYPE (@1))
696 && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
697 (cmp (bit_ior @0 (convert @1)) @2)))
699 (x == -1 & y == -1) -> (x & typeof(x)(y)) == -1.
700 (x != -1 | y != -1) -> (x & typeof(x)(y)) != -1. */
702 (bitop (cmp @0 integer_all_onesp@2) (cmp @1 integer_all_onesp))
703 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
704 && INTEGRAL_TYPE_P (TREE_TYPE (@1))
705 && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
706 (cmp (bit_and @0 (convert @1)) @2))))
708 /* Fold (A & ~B) - (A & B) into (A ^ B) - B. */
710 (minus (bit_and:cs @0 (bit_not @1)) (bit_and:cs @0 @1))
711 (minus (bit_xor @0 @1) @1))
713 (minus (bit_and:s @0 INTEGER_CST@2) (bit_and:s @0 INTEGER_CST@1))
714 (if (~wi::to_wide (@2) == wi::to_wide (@1))
715 (minus (bit_xor @0 @1) @1)))
717 /* Fold (A & B) - (A & ~B) into B - (A ^ B). */
719 (minus (bit_and:cs @0 @1) (bit_and:cs @0 (bit_not @1)))
720 (minus @1 (bit_xor @0 @1)))
722 /* Simplify (X & ~Y) |^+ (~X & Y) -> X ^ Y. */
723 (for op (bit_ior bit_xor plus)
725 (op (bit_and:c @0 (bit_not @1)) (bit_and:c (bit_not @0) @1))
728 (op:c (bit_and @0 INTEGER_CST@2) (bit_and (bit_not @0) INTEGER_CST@1))
729 (if (~wi::to_wide (@2) == wi::to_wide (@1))
732 /* PR53979: Transform ((a ^ b) | a) -> (a | b) */
734 (bit_ior:c (bit_xor:c @0 @1) @0)
737 /* (a & ~b) | (a ^ b) --> a ^ b */
739 (bit_ior:c (bit_and:c @0 (bit_not @1)) (bit_xor:c@2 @0 @1))
742 /* (a & ~b) ^ ~a --> ~(a & b) */
744 (bit_xor:c (bit_and:cs @0 (bit_not @1)) (bit_not @0))
745 (bit_not (bit_and @0 @1)))
747 /* (a | b) & ~(a ^ b) --> a & b */
749 (bit_and:c (bit_ior @0 @1) (bit_not (bit_xor:c @0 @1)))
752 /* a | ~(a ^ b) --> a | ~b */
754 (bit_ior:c @0 (bit_not:s (bit_xor:c @0 @1)))
755 (bit_ior @0 (bit_not @1)))
757 /* (a | b) | (a &^ b) --> a | b */
758 (for op (bit_and bit_xor)
760 (bit_ior:c (bit_ior@2 @0 @1) (op:c @0 @1))
763 /* (a & b) | ~(a ^ b) --> ~(a ^ b) */
765 (bit_ior:c (bit_and:c @0 @1) (bit_not@2 (bit_xor @0 @1)))
768 /* ~(~a & b) --> a | ~b */
770 (bit_not (bit_and:cs (bit_not @0) @1))
771 (bit_ior @0 (bit_not @1)))
773 /* Simplify (~X & Y) to X ^ Y if we know that (X & ~Y) is 0. */
776 (bit_and (bit_not SSA_NAME@0) INTEGER_CST@1)
777 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
778 && wi::bit_and_not (get_nonzero_bits (@0), wi::to_wide (@1)) == 0)
782 /* For constants M and N, if M == (1LL << cst) - 1 && (N & M) == M,
783 ((A & N) + B) & M -> (A + B) & M
784 Similarly if (N & M) == 0,
785 ((A | N) + B) & M -> (A + B) & M
786 and for - instead of + (or unary - instead of +)
787 and/or ^ instead of |.
788 If B is constant and (B & M) == 0, fold into A & M. */
790 (for bitop (bit_and bit_ior bit_xor)
792 (bit_and (op:s (bitop:s@0 @3 INTEGER_CST@4) @1) INTEGER_CST@2)
795 tree utype = fold_bit_and_mask (TREE_TYPE (@0), @2, op, @0, bitop,
796 @3, @4, @1, ERROR_MARK, NULL_TREE,
799 (convert (bit_and (op (convert:utype { pmop[0]; })
800 (convert:utype { pmop[1]; }))
801 (convert:utype @2))))))
803 (bit_and (op:s @0 (bitop:s@1 @3 INTEGER_CST@4)) INTEGER_CST@2)
806 tree utype = fold_bit_and_mask (TREE_TYPE (@0), @2, op, @0, ERROR_MARK,
807 NULL_TREE, NULL_TREE, @1, bitop, @3,
810 (convert (bit_and (op (convert:utype { pmop[0]; })
811 (convert:utype { pmop[1]; }))
812 (convert:utype @2)))))))
814 (bit_and (op:s @0 @1) INTEGER_CST@2)
817 tree utype = fold_bit_and_mask (TREE_TYPE (@0), @2, op, @0, ERROR_MARK,
818 NULL_TREE, NULL_TREE, @1, ERROR_MARK,
819 NULL_TREE, NULL_TREE, pmop); }
821 (convert (bit_and (op (convert:utype { pmop[0]; })
822 (convert:utype { pmop[1]; }))
823 (convert:utype @2)))))))
824 (for bitop (bit_and bit_ior bit_xor)
826 (bit_and (negate:s (bitop:s@0 @2 INTEGER_CST@3)) INTEGER_CST@1)
829 tree utype = fold_bit_and_mask (TREE_TYPE (@0), @1, NEGATE_EXPR, @0,
830 bitop, @2, @3, NULL_TREE, ERROR_MARK,
831 NULL_TREE, NULL_TREE, pmop); }
833 (convert (bit_and (negate (convert:utype { pmop[0]; }))
834 (convert:utype @1)))))))
836 /* X % Y is smaller than Y. */
839 (cmp (trunc_mod @0 @1) @1)
840 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
841 { constant_boolean_node (cmp == LT_EXPR, type); })))
844 (cmp @1 (trunc_mod @0 @1))
845 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
846 { constant_boolean_node (cmp == GT_EXPR, type); })))
850 (bit_ior @0 integer_all_onesp@1)
855 (bit_ior @0 integer_zerop)
860 (bit_and @0 integer_zerop@1)
866 (for op (bit_ior bit_xor plus)
868 (op:c (convert? @0) (convert? (bit_not @0)))
869 (convert { build_all_ones_cst (TREE_TYPE (@0)); })))
874 { build_zero_cst (type); })
876 /* Canonicalize X ^ ~0 to ~X. */
878 (bit_xor @0 integer_all_onesp@1)
883 (bit_and @0 integer_all_onesp)
886 /* x & x -> x, x | x -> x */
887 (for bitop (bit_and bit_ior)
892 /* x & C -> x if we know that x & ~C == 0. */
895 (bit_and SSA_NAME@0 INTEGER_CST@1)
896 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
897 && wi::bit_and_not (get_nonzero_bits (@0), wi::to_wide (@1)) == 0)
901 /* x + (x & 1) -> (x + 1) & ~1 */
903 (plus:c @0 (bit_and:s @0 integer_onep@1))
904 (bit_and (plus @0 @1) (bit_not @1)))
906 /* x & ~(x & y) -> x & ~y */
907 /* x | ~(x | y) -> x | ~y */
908 (for bitop (bit_and bit_ior)
910 (bitop:c @0 (bit_not (bitop:cs @0 @1)))
911 (bitop @0 (bit_not @1))))
913 /* (x | y) & ~x -> y & ~x */
914 /* (x & y) | ~x -> y | ~x */
915 (for bitop (bit_and bit_ior)
916 rbitop (bit_ior bit_and)
918 (bitop:c (rbitop:c @0 @1) (bit_not@2 @0))
921 /* (x & y) ^ (x | y) -> x ^ y */
923 (bit_xor:c (bit_and @0 @1) (bit_ior @0 @1))
926 /* (x ^ y) ^ (x | y) -> x & y */
928 (bit_xor:c (bit_xor @0 @1) (bit_ior @0 @1))
931 /* (x & y) + (x ^ y) -> x | y */
932 /* (x & y) | (x ^ y) -> x | y */
933 /* (x & y) ^ (x ^ y) -> x | y */
934 (for op (plus bit_ior bit_xor)
936 (op:c (bit_and @0 @1) (bit_xor @0 @1))
939 /* (x & y) + (x | y) -> x + y */
941 (plus:c (bit_and @0 @1) (bit_ior @0 @1))
944 /* (x + y) - (x | y) -> x & y */
946 (minus (plus @0 @1) (bit_ior @0 @1))
947 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
948 && !TYPE_SATURATING (type))
951 /* (x + y) - (x & y) -> x | y */
953 (minus (plus @0 @1) (bit_and @0 @1))
954 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
955 && !TYPE_SATURATING (type))
958 /* (x | y) - (x ^ y) -> x & y */
960 (minus (bit_ior @0 @1) (bit_xor @0 @1))
963 /* (x | y) - (x & y) -> x ^ y */
965 (minus (bit_ior @0 @1) (bit_and @0 @1))
968 /* (x | y) & ~(x & y) -> x ^ y */
970 (bit_and:c (bit_ior @0 @1) (bit_not (bit_and @0 @1)))
973 /* (x | y) & (~x ^ y) -> x & y */
975 (bit_and:c (bit_ior:c @0 @1) (bit_xor:c @1 (bit_not @0)))
978 /* ~x & ~y -> ~(x | y)
979 ~x | ~y -> ~(x & y) */
980 (for op (bit_and bit_ior)
981 rop (bit_ior bit_and)
983 (op (convert1? (bit_not @0)) (convert2? (bit_not @1)))
984 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
985 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
986 (bit_not (rop (convert @0) (convert @1))))))
988 /* If we are XORing or adding two BIT_AND_EXPR's, both of which are and'ing
989 with a constant, and the two constants have no bits in common,
990 we should treat this as a BIT_IOR_EXPR since this may produce more
992 (for op (bit_xor plus)
994 (op (convert1? (bit_and@4 @0 INTEGER_CST@1))
995 (convert2? (bit_and@5 @2 INTEGER_CST@3)))
996 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
997 && tree_nop_conversion_p (type, TREE_TYPE (@2))
998 && (wi::to_wide (@1) & wi::to_wide (@3)) == 0)
999 (bit_ior (convert @4) (convert @5)))))
1001 /* (X | Y) ^ X -> Y & ~ X*/
1003 (bit_xor:c (convert1? (bit_ior:c @@0 @1)) (convert2? @0))
1004 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1005 (convert (bit_and @1 (bit_not @0)))))
1007 /* Convert ~X ^ ~Y to X ^ Y. */
1009 (bit_xor (convert1? (bit_not @0)) (convert2? (bit_not @1)))
1010 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1011 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
1012 (bit_xor (convert @0) (convert @1))))
1014 /* Convert ~X ^ C to X ^ ~C. */
1016 (bit_xor (convert? (bit_not @0)) INTEGER_CST@1)
1017 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1018 (bit_xor (convert @0) (bit_not @1))))
1020 /* Fold (X & Y) ^ Y and (X ^ Y) & Y as ~X & Y. */
1021 (for opo (bit_and bit_xor)
1022 opi (bit_xor bit_and)
1024 (opo:c (opi:c @0 @1) @1)
1025 (bit_and (bit_not @0) @1)))
1027 /* Given a bit-wise operation CODE applied to ARG0 and ARG1, see if both
1028 operands are another bit-wise operation with a common input. If so,
1029 distribute the bit operations to save an operation and possibly two if
1030 constants are involved. For example, convert
1031 (A | B) & (A | C) into A | (B & C)
1032 Further simplification will occur if B and C are constants. */
1033 (for op (bit_and bit_ior bit_xor)
1034 rop (bit_ior bit_and bit_and)
1036 (op (convert? (rop:c @@0 @1)) (convert? (rop:c @0 @2)))
1037 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1038 && tree_nop_conversion_p (type, TREE_TYPE (@2)))
1039 (rop (convert @0) (op (convert @1) (convert @2))))))
1041 /* Some simple reassociation for bit operations, also handled in reassoc. */
1042 /* (X & Y) & Y -> X & Y
1043 (X | Y) | Y -> X | Y */
1044 (for op (bit_and bit_ior)
1046 (op:c (convert1?@2 (op:c @0 @@1)) (convert2? @1))
1048 /* (X ^ Y) ^ Y -> X */
1050 (bit_xor:c (convert1? (bit_xor:c @0 @@1)) (convert2? @1))
1052 /* (X & Y) & (X & Z) -> (X & Y) & Z
1053 (X | Y) | (X | Z) -> (X | Y) | Z */
1054 (for op (bit_and bit_ior)
1056 (op (convert1?@3 (op:c@4 @0 @1)) (convert2?@5 (op:c@6 @0 @2)))
1057 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1058 && tree_nop_conversion_p (type, TREE_TYPE (@2)))
1059 (if (single_use (@5) && single_use (@6))
1060 (op @3 (convert @2))
1061 (if (single_use (@3) && single_use (@4))
1062 (op (convert @1) @5))))))
1063 /* (X ^ Y) ^ (X ^ Z) -> Y ^ Z */
1065 (bit_xor (convert1? (bit_xor:c @0 @1)) (convert2? (bit_xor:c @0 @2)))
1066 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1067 && tree_nop_conversion_p (type, TREE_TYPE (@2)))
1068 (bit_xor (convert @1) (convert @2))))
1077 (abs tree_expr_nonnegative_p@0)
1080 /* A few cases of fold-const.c negate_expr_p predicate. */
1081 (match negate_expr_p
1083 (if ((INTEGRAL_TYPE_P (type)
1084 && TYPE_UNSIGNED (type))
1085 || (!TYPE_OVERFLOW_SANITIZED (type)
1086 && may_negate_without_overflow_p (t)))))
1087 (match negate_expr_p
1089 (match negate_expr_p
1091 (if (!TYPE_OVERFLOW_SANITIZED (type))))
1092 (match negate_expr_p
1094 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (t)))))
1095 /* VECTOR_CST handling of non-wrapping types would recurse in unsupported
1097 (match negate_expr_p
1099 (if (FLOAT_TYPE_P (TREE_TYPE (type)) || TYPE_OVERFLOW_WRAPS (type))))
1100 (match negate_expr_p
1102 (if ((ANY_INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_WRAPS (type))
1103 || (FLOAT_TYPE_P (type)
1104 && !HONOR_SIGN_DEPENDENT_ROUNDING (type)
1105 && !HONOR_SIGNED_ZEROS (type)))))
1107 /* (-A) * (-B) -> A * B */
1109 (mult:c (convert1? (negate @0)) (convert2? negate_expr_p@1))
1110 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
1111 && tree_nop_conversion_p (type, TREE_TYPE (@1)))
1112 (mult (convert @0) (convert (negate @1)))))
1114 /* -(A + B) -> (-B) - A. */
1116 (negate (plus:c @0 negate_expr_p@1))
1117 (if (!HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type))
1118 && !HONOR_SIGNED_ZEROS (element_mode (type)))
1119 (minus (negate @1) @0)))
1121 /* -(A - B) -> B - A. */
1123 (negate (minus @0 @1))
1124 (if ((ANY_INTEGRAL_TYPE_P (type) && !TYPE_OVERFLOW_SANITIZED (type))
1125 || (FLOAT_TYPE_P (type)
1126 && !HONOR_SIGN_DEPENDENT_ROUNDING (type)
1127 && !HONOR_SIGNED_ZEROS (type)))
1130 (negate (pointer_diff @0 @1))
1131 (if (TYPE_OVERFLOW_UNDEFINED (type))
1132 (pointer_diff @1 @0)))
1134 /* A - B -> A + (-B) if B is easily negatable. */
1136 (minus @0 negate_expr_p@1)
1137 (if (!FIXED_POINT_TYPE_P (type))
1138 (plus @0 (negate @1))))
1140 /* Try to fold (type) X op CST -> (type) (X op ((type-x) CST))
1142 For bitwise binary operations apply operand conversions to the
1143 binary operation result instead of to the operands. This allows
1144 to combine successive conversions and bitwise binary operations.
1145 We combine the above two cases by using a conditional convert. */
1146 (for bitop (bit_and bit_ior bit_xor)
1148 (bitop (convert @0) (convert? @1))
1149 (if (((TREE_CODE (@1) == INTEGER_CST
1150 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
1151 && int_fits_type_p (@1, TREE_TYPE (@0)))
1152 || types_match (@0, @1))
1153 /* ??? This transform conflicts with fold-const.c doing
1154 Convert (T)(x & c) into (T)x & (T)c, if c is an integer
1155 constants (if x has signed type, the sign bit cannot be set
1156 in c). This folds extension into the BIT_AND_EXPR.
1157 Restrict it to GIMPLE to avoid endless recursions. */
1158 && (bitop != BIT_AND_EXPR || GIMPLE)
1159 && (/* That's a good idea if the conversion widens the operand, thus
1160 after hoisting the conversion the operation will be narrower. */
1161 TYPE_PRECISION (TREE_TYPE (@0)) < TYPE_PRECISION (type)
1162 /* It's also a good idea if the conversion is to a non-integer
1164 || GET_MODE_CLASS (TYPE_MODE (type)) != MODE_INT
1165 /* Or if the precision of TO is not the same as the precision
1167 || !type_has_mode_precision_p (type)))
1168 (convert (bitop @0 (convert @1))))))
1170 (for bitop (bit_and bit_ior)
1171 rbitop (bit_ior bit_and)
1172 /* (x | y) & x -> x */
1173 /* (x & y) | x -> x */
1175 (bitop:c (rbitop:c @0 @1) @0)
1177 /* (~x | y) & x -> x & y */
1178 /* (~x & y) | x -> x | y */
1180 (bitop:c (rbitop:c (bit_not @0) @1) @0)
1183 /* (x | CST1) & CST2 -> (x & CST2) | (CST1 & CST2) */
1185 (bit_and (bit_ior @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
1186 (bit_ior (bit_and @0 @2) (bit_and @1 @2)))
1188 /* Combine successive equal operations with constants. */
1189 (for bitop (bit_and bit_ior bit_xor)
1191 (bitop (bitop @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
1192 (if (!CONSTANT_CLASS_P (@0))
1193 /* This is the canonical form regardless of whether (bitop @1 @2) can be
1194 folded to a constant. */
1195 (bitop @0 (bitop @1 @2))
1196 /* In this case we have three constants and (bitop @0 @1) doesn't fold
1197 to a constant. This can happen if @0 or @1 is a POLY_INT_CST and if
1198 the values involved are such that the operation can't be decided at
1199 compile time. Try folding one of @0 or @1 with @2 to see whether
1200 that combination can be decided at compile time.
1202 Keep the existing form if both folds fail, to avoid endless
1204 (with { tree cst1 = const_binop (bitop, type, @0, @2); }
1206 (bitop @1 { cst1; })
1207 (with { tree cst2 = const_binop (bitop, type, @1, @2); }
1209 (bitop @0 { cst2; }))))))))
1211 /* Try simple folding for X op !X, and X op X with the help
1212 of the truth_valued_p and logical_inverted_value predicates. */
1213 (match truth_valued_p
1215 (if (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1)))
1216 (for op (tcc_comparison truth_and truth_andif truth_or truth_orif truth_xor)
1217 (match truth_valued_p
1219 (match truth_valued_p
1222 (match (logical_inverted_value @0)
1224 (match (logical_inverted_value @0)
1225 (bit_not truth_valued_p@0))
1226 (match (logical_inverted_value @0)
1227 (eq @0 integer_zerop))
1228 (match (logical_inverted_value @0)
1229 (ne truth_valued_p@0 integer_truep))
1230 (match (logical_inverted_value @0)
1231 (bit_xor truth_valued_p@0 integer_truep))
1235 (bit_and:c @0 (logical_inverted_value @0))
1236 { build_zero_cst (type); })
1237 /* X | !X and X ^ !X -> 1, , if X is truth-valued. */
1238 (for op (bit_ior bit_xor)
1240 (op:c truth_valued_p@0 (logical_inverted_value @0))
1241 { constant_boolean_node (true, type); }))
1242 /* X ==/!= !X is false/true. */
1245 (op:c truth_valued_p@0 (logical_inverted_value @0))
1246 { constant_boolean_node (op == NE_EXPR ? true : false, type); }))
1250 (bit_not (bit_not @0))
1253 /* Convert ~ (-A) to A - 1. */
1255 (bit_not (convert? (negate @0)))
1256 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1257 || !TYPE_UNSIGNED (TREE_TYPE (@0)))
1258 (convert (minus @0 { build_each_one_cst (TREE_TYPE (@0)); }))))
1260 /* Convert - (~A) to A + 1. */
1262 (negate (nop_convert (bit_not @0)))
1263 (plus (view_convert @0) { build_each_one_cst (type); }))
1265 /* Convert ~ (A - 1) or ~ (A + -1) to -A. */
1267 (bit_not (convert? (minus @0 integer_each_onep)))
1268 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1269 || !TYPE_UNSIGNED (TREE_TYPE (@0)))
1270 (convert (negate @0))))
1272 (bit_not (convert? (plus @0 integer_all_onesp)))
1273 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1274 || !TYPE_UNSIGNED (TREE_TYPE (@0)))
1275 (convert (negate @0))))
1277 /* Part of convert ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify. */
1279 (bit_not (convert? (bit_xor @0 INTEGER_CST@1)))
1280 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1281 (convert (bit_xor @0 (bit_not @1)))))
1283 (bit_not (convert? (bit_xor:c (bit_not @0) @1)))
1284 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1285 (convert (bit_xor @0 @1))))
1287 /* Otherwise prefer ~(X ^ Y) to ~X ^ Y as more canonical. */
1289 (bit_xor:c (nop_convert:s (bit_not:s @0)) @1)
1290 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1291 (bit_not (bit_xor (view_convert @0) @1))))
1293 /* (x & ~m) | (y & m) -> ((x ^ y) & m) ^ x */
1295 (bit_ior:c (bit_and:cs @0 (bit_not @2)) (bit_and:cs @1 @2))
1296 (bit_xor (bit_and (bit_xor @0 @1) @2) @0))
1298 /* Fold A - (A & B) into ~B & A. */
1300 (minus (convert1? @0) (convert2?:s (bit_and:cs @@0 @1)))
1301 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
1302 && tree_nop_conversion_p (type, TREE_TYPE (@1)))
1303 (convert (bit_and (bit_not @1) @0))))
1305 /* (m1 CMP m2) * d -> (m1 CMP m2) ? d : 0 */
1306 (for cmp (gt lt ge le)
1308 (mult (convert (cmp @0 @1)) @2)
1309 (cond (cmp @0 @1) @2 { build_zero_cst (type); })))
1311 /* For integral types with undefined overflow and C != 0 fold
1312 x * C EQ/NE y * C into x EQ/NE y. */
1315 (cmp (mult:c @0 @1) (mult:c @2 @1))
1316 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
1317 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1318 && tree_expr_nonzero_p (@1))
1321 /* For integral types with wrapping overflow and C odd fold
1322 x * C EQ/NE y * C into x EQ/NE y. */
1325 (cmp (mult @0 INTEGER_CST@1) (mult @2 @1))
1326 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
1327 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))
1328 && (TREE_INT_CST_LOW (@1) & 1) != 0)
1331 /* For integral types with undefined overflow and C != 0 fold
1332 x * C RELOP y * C into:
1334 x RELOP y for nonnegative C
1335 y RELOP x for negative C */
1336 (for cmp (lt gt le ge)
1338 (cmp (mult:c @0 @1) (mult:c @2 @1))
1339 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
1340 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1341 (if (tree_expr_nonnegative_p (@1) && tree_expr_nonzero_p (@1))
1343 (if (TREE_CODE (@1) == INTEGER_CST
1344 && wi::neg_p (wi::to_wide (@1), TYPE_SIGN (TREE_TYPE (@1))))
1347 /* (X - 1U) <= INT_MAX-1U into (int) X > 0. */
1351 (cmp (plus @0 integer_minus_onep@1) INTEGER_CST@2)
1352 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1353 && TYPE_UNSIGNED (TREE_TYPE (@0))
1354 && TYPE_PRECISION (TREE_TYPE (@0)) > 1
1355 && (wi::to_wide (@2)
1356 == wi::max_value (TYPE_PRECISION (TREE_TYPE (@0)), SIGNED) - 1))
1357 (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
1358 (icmp (convert:stype @0) { build_int_cst (stype, 0); })))))
1360 /* X / 4 < Y / 4 iff X < Y when the division is known to be exact. */
1361 (for cmp (simple_comparison)
1363 (cmp (exact_div @0 INTEGER_CST@2) (exact_div @1 @2))
1364 (if (wi::gt_p (wi::to_wide (@2), 0, TYPE_SIGN (TREE_TYPE (@2))))
1367 /* X / C1 op C2 into a simple range test. */
1368 (for cmp (simple_comparison)
1370 (cmp (trunc_div:s @0 INTEGER_CST@1) INTEGER_CST@2)
1371 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1372 && integer_nonzerop (@1)
1373 && !TREE_OVERFLOW (@1)
1374 && !TREE_OVERFLOW (@2))
1375 (with { tree lo, hi; bool neg_overflow;
1376 enum tree_code code = fold_div_compare (cmp, @1, @2, &lo, &hi,
1379 (if (code == LT_EXPR || code == GE_EXPR)
1380 (if (TREE_OVERFLOW (lo))
1381 { build_int_cst (type, (code == LT_EXPR) ^ neg_overflow); }
1382 (if (code == LT_EXPR)
1385 (if (code == LE_EXPR || code == GT_EXPR)
1386 (if (TREE_OVERFLOW (hi))
1387 { build_int_cst (type, (code == LE_EXPR) ^ neg_overflow); }
1388 (if (code == LE_EXPR)
1392 { build_int_cst (type, code == NE_EXPR); })
1393 (if (code == EQ_EXPR && !hi)
1395 (if (code == EQ_EXPR && !lo)
1397 (if (code == NE_EXPR && !hi)
1399 (if (code == NE_EXPR && !lo)
1402 { build_range_check (UNKNOWN_LOCATION, type, @0, code == EQ_EXPR,
1406 tree etype = range_check_type (TREE_TYPE (@0));
1409 if (! TYPE_UNSIGNED (etype))
1410 etype = unsigned_type_for (etype);
1411 hi = fold_convert (etype, hi);
1412 lo = fold_convert (etype, lo);
1413 hi = const_binop (MINUS_EXPR, etype, hi, lo);
1416 (if (etype && hi && !TREE_OVERFLOW (hi))
1417 (if (code == EQ_EXPR)
1418 (le (minus (convert:etype @0) { lo; }) { hi; })
1419 (gt (minus (convert:etype @0) { lo; }) { hi; })))))))))
1421 /* X + Z < Y + Z is the same as X < Y when there is no overflow. */
1422 (for op (lt le ge gt)
1424 (op (plus:c @0 @2) (plus:c @1 @2))
1425 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1426 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1428 /* For equality and subtraction, this is also true with wrapping overflow. */
1429 (for op (eq ne minus)
1431 (op (plus:c @0 @2) (plus:c @1 @2))
1432 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1433 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1434 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
1437 /* X - Z < Y - Z is the same as X < Y when there is no overflow. */
1438 (for op (lt le ge gt)
1440 (op (minus @0 @2) (minus @1 @2))
1441 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1442 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1444 /* For equality and subtraction, this is also true with wrapping overflow. */
1445 (for op (eq ne minus)
1447 (op (minus @0 @2) (minus @1 @2))
1448 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1449 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1450 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
1452 /* And for pointers... */
1453 (for op (simple_comparison)
1455 (op (pointer_diff@3 @0 @2) (pointer_diff @1 @2))
1456 (if (!TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
1459 (minus (pointer_diff@3 @0 @2) (pointer_diff @1 @2))
1460 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@3))
1461 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
1462 (pointer_diff @0 @1)))
1464 /* Z - X < Z - Y is the same as Y < X when there is no overflow. */
1465 (for op (lt le ge gt)
1467 (op (minus @2 @0) (minus @2 @1))
1468 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1469 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1471 /* For equality and subtraction, this is also true with wrapping overflow. */
1472 (for op (eq ne minus)
1474 (op (minus @2 @0) (minus @2 @1))
1475 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1476 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1477 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
1479 /* And for pointers... */
1480 (for op (simple_comparison)
1482 (op (pointer_diff@3 @2 @0) (pointer_diff @2 @1))
1483 (if (!TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
1486 (minus (pointer_diff@3 @2 @0) (pointer_diff @2 @1))
1487 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@3))
1488 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
1489 (pointer_diff @1 @0)))
1491 /* X + Y < Y is the same as X < 0 when there is no overflow. */
1492 (for op (lt le gt ge)
1494 (op:c (plus:c@2 @0 @1) @1)
1495 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1496 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1497 && (CONSTANT_CLASS_P (@0) || single_use (@2)))
1498 (op @0 { build_zero_cst (TREE_TYPE (@0)); }))))
1499 /* For equality, this is also true with wrapping overflow. */
1502 (op:c (nop_convert@3 (plus:c@2 @0 (convert1? @1))) (convert2? @1))
1503 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1504 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1505 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
1506 && (CONSTANT_CLASS_P (@0) || (single_use (@2) && single_use (@3)))
1507 && tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@2))
1508 && tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@1)))
1509 (op @0 { build_zero_cst (TREE_TYPE (@0)); })))
1511 (op:c (nop_convert@3 (pointer_plus@2 (convert1? @0) @1)) (convert2? @0))
1512 (if (tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@0))
1513 && tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0))
1514 && (CONSTANT_CLASS_P (@1) || (single_use (@2) && single_use (@3))))
1515 (op @1 { build_zero_cst (TREE_TYPE (@1)); }))))
1517 /* X - Y < X is the same as Y > 0 when there is no overflow.
1518 For equality, this is also true with wrapping overflow. */
1519 (for op (simple_comparison)
1521 (op:c @0 (minus@2 @0 @1))
1522 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1523 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1524 || ((op == EQ_EXPR || op == NE_EXPR)
1525 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
1526 && (CONSTANT_CLASS_P (@1) || single_use (@2)))
1527 (op @1 { build_zero_cst (TREE_TYPE (@1)); }))))
1530 (X / Y) == 0 -> X < Y if X, Y are unsigned.
1531 (X / Y) != 0 -> X >= Y, if X, Y are unsigned. */
1535 (cmp (trunc_div @0 @1) integer_zerop)
1536 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
1537 /* Complex ==/!= is allowed, but not </>=. */
1538 && TREE_CODE (TREE_TYPE (@0)) != COMPLEX_TYPE
1539 && (VECTOR_TYPE_P (type) || !VECTOR_TYPE_P (TREE_TYPE (@0))))
1542 /* X == C - X can never be true if C is odd. */
1545 (cmp:c (convert? @0) (convert1? (minus INTEGER_CST@1 (convert2? @0))))
1546 (if (TREE_INT_CST_LOW (@1) & 1)
1547 { constant_boolean_node (cmp == NE_EXPR, type); })))
1549 /* Arguments on which one can call get_nonzero_bits to get the bits
1551 (match with_possible_nonzero_bits
1553 (match with_possible_nonzero_bits
1555 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))))
1556 /* Slightly extended version, do not make it recursive to keep it cheap. */
1557 (match (with_possible_nonzero_bits2 @0)
1558 with_possible_nonzero_bits@0)
1559 (match (with_possible_nonzero_bits2 @0)
1560 (bit_and:c with_possible_nonzero_bits@0 @2))
1562 /* Same for bits that are known to be set, but we do not have
1563 an equivalent to get_nonzero_bits yet. */
1564 (match (with_certain_nonzero_bits2 @0)
1566 (match (with_certain_nonzero_bits2 @0)
1567 (bit_ior @1 INTEGER_CST@0))
1569 /* X == C (or X & Z == Y | C) is impossible if ~nonzero(X) & C != 0. */
1572 (cmp:c (with_possible_nonzero_bits2 @0) (with_certain_nonzero_bits2 @1))
1573 (if (wi::bit_and_not (wi::to_wide (@1), get_nonzero_bits (@0)) != 0)
1574 { constant_boolean_node (cmp == NE_EXPR, type); })))
1576 /* ((X inner_op C0) outer_op C1)
1577 With X being a tree where value_range has reasoned certain bits to always be
1578 zero throughout its computed value range,
1579 inner_op = {|,^}, outer_op = {|,^} and inner_op != outer_op
1580 where zero_mask has 1's for all bits that are sure to be 0 in
1582 if (inner_op == '^') C0 &= ~C1;
1583 if ((C0 & ~zero_mask) == 0) then emit (X outer_op (C0 outer_op C1)
1584 if ((C1 & ~zero_mask) == 0) then emit (X inner_op (C0 outer_op C1)
1586 (for inner_op (bit_ior bit_xor)
1587 outer_op (bit_xor bit_ior)
1590 (inner_op:s @2 INTEGER_CST@0) INTEGER_CST@1)
1594 wide_int zero_mask_not;
1598 if (TREE_CODE (@2) == SSA_NAME)
1599 zero_mask_not = get_nonzero_bits (@2);
1603 if (inner_op == BIT_XOR_EXPR)
1605 C0 = wi::bit_and_not (wi::to_wide (@0), wi::to_wide (@1));
1606 cst_emit = C0 | wi::to_wide (@1);
1610 C0 = wi::to_wide (@0);
1611 cst_emit = C0 ^ wi::to_wide (@1);
1614 (if (!fail && (C0 & zero_mask_not) == 0)
1615 (outer_op @2 { wide_int_to_tree (type, cst_emit); })
1616 (if (!fail && (wi::to_wide (@1) & zero_mask_not) == 0)
1617 (inner_op @2 { wide_int_to_tree (type, cst_emit); }))))))
1619 /* Associate (p +p off1) +p off2 as (p +p (off1 + off2)). */
1621 (pointer_plus (pointer_plus:s @0 @1) @3)
1622 (pointer_plus @0 (plus @1 @3)))
1628 tem4 = (unsigned long) tem3;
1633 (pointer_plus @0 (convert?@2 (minus@3 (convert @1) (convert @0))))
1634 /* Conditionally look through a sign-changing conversion. */
1635 (if (TYPE_PRECISION (TREE_TYPE (@2)) == TYPE_PRECISION (TREE_TYPE (@3))
1636 && ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@1)))
1637 || (GENERIC && type == TREE_TYPE (@1))))
1640 (pointer_plus @0 (convert?@2 (pointer_diff@3 @1 @@0)))
1641 (if (TYPE_PRECISION (TREE_TYPE (@2)) >= TYPE_PRECISION (TREE_TYPE (@3)))
1645 tem = (sizetype) ptr;
1649 and produce the simpler and easier to analyze with respect to alignment
1650 ... = ptr & ~algn; */
1652 (pointer_plus @0 (negate (bit_and (convert @0) INTEGER_CST@1)))
1653 (with { tree algn = wide_int_to_tree (TREE_TYPE (@0), ~wi::to_wide (@1)); }
1654 (bit_and @0 { algn; })))
1656 /* Try folding difference of addresses. */
1658 (minus (convert ADDR_EXPR@0) (convert @1))
1659 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1660 (with { poly_int64 diff; }
1661 (if (ptr_difference_const (@0, @1, &diff))
1662 { build_int_cst_type (type, diff); }))))
1664 (minus (convert @0) (convert ADDR_EXPR@1))
1665 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1666 (with { poly_int64 diff; }
1667 (if (ptr_difference_const (@0, @1, &diff))
1668 { build_int_cst_type (type, diff); }))))
1670 (pointer_diff (convert?@2 ADDR_EXPR@0) (convert?@3 @1))
1671 (if (tree_nop_conversion_p (TREE_TYPE(@2), TREE_TYPE (@0))
1672 && tree_nop_conversion_p (TREE_TYPE(@3), TREE_TYPE (@1)))
1673 (with { poly_int64 diff; }
1674 (if (ptr_difference_const (@0, @1, &diff))
1675 { build_int_cst_type (type, diff); }))))
1677 (pointer_diff (convert?@2 @0) (convert?@3 ADDR_EXPR@1))
1678 (if (tree_nop_conversion_p (TREE_TYPE(@2), TREE_TYPE (@0))
1679 && tree_nop_conversion_p (TREE_TYPE(@3), TREE_TYPE (@1)))
1680 (with { poly_int64 diff; }
1681 (if (ptr_difference_const (@0, @1, &diff))
1682 { build_int_cst_type (type, diff); }))))
1684 /* If arg0 is derived from the address of an object or function, we may
1685 be able to fold this expression using the object or function's
1688 (bit_and (convert? @0) INTEGER_CST@1)
1689 (if (POINTER_TYPE_P (TREE_TYPE (@0))
1690 && tree_nop_conversion_p (type, TREE_TYPE (@0)))
1694 unsigned HOST_WIDE_INT bitpos;
1695 get_pointer_alignment_1 (@0, &align, &bitpos);
1697 (if (wi::ltu_p (wi::to_wide (@1), align / BITS_PER_UNIT))
1698 { wide_int_to_tree (type, (wi::to_wide (@1)
1699 & (bitpos / BITS_PER_UNIT))); }))))
1702 /* We can't reassociate at all for saturating types. */
1703 (if (!TYPE_SATURATING (type))
1705 /* Contract negates. */
1706 /* A + (-B) -> A - B */
1708 (plus:c @0 (convert? (negate @1)))
1709 /* Apply STRIP_NOPS on the negate. */
1710 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1711 && !TYPE_OVERFLOW_SANITIZED (type))
1715 if (INTEGRAL_TYPE_P (type)
1716 && TYPE_OVERFLOW_WRAPS (type) != TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
1717 t1 = TYPE_OVERFLOW_WRAPS (type) ? type : TREE_TYPE (@1);
1719 (convert (minus (convert:t1 @0) (convert:t1 @1))))))
1720 /* A - (-B) -> A + B */
1722 (minus @0 (convert? (negate @1)))
1723 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1724 && !TYPE_OVERFLOW_SANITIZED (type))
1728 if (INTEGRAL_TYPE_P (type)
1729 && TYPE_OVERFLOW_WRAPS (type) != TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
1730 t1 = TYPE_OVERFLOW_WRAPS (type) ? type : TREE_TYPE (@1);
1732 (convert (plus (convert:t1 @0) (convert:t1 @1))))))
1734 Sign-extension is ok except for INT_MIN, which thankfully cannot
1735 happen without overflow. */
1737 (negate (convert (negate @1)))
1738 (if (INTEGRAL_TYPE_P (type)
1739 && (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@1))
1740 || (!TYPE_UNSIGNED (TREE_TYPE (@1))
1741 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))))
1742 && !TYPE_OVERFLOW_SANITIZED (type)
1743 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@1)))
1746 (negate (convert negate_expr_p@1))
1747 (if (SCALAR_FLOAT_TYPE_P (type)
1748 && ((DECIMAL_FLOAT_TYPE_P (type)
1749 == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1))
1750 && TYPE_PRECISION (type) >= TYPE_PRECISION (TREE_TYPE (@1)))
1751 || !HONOR_SIGN_DEPENDENT_ROUNDING (type)))
1752 (convert (negate @1))))
1754 (negate (nop_convert (negate @1)))
1755 (if (!TYPE_OVERFLOW_SANITIZED (type)
1756 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@1)))
1759 /* We can't reassociate floating-point unless -fassociative-math
1760 or fixed-point plus or minus because of saturation to +-Inf. */
1761 (if ((!FLOAT_TYPE_P (type) || flag_associative_math)
1762 && !FIXED_POINT_TYPE_P (type))
1764 /* Match patterns that allow contracting a plus-minus pair
1765 irrespective of overflow issues. */
1766 /* (A +- B) - A -> +- B */
1767 /* (A +- B) -+ B -> A */
1768 /* A - (A +- B) -> -+ B */
1769 /* A +- (B -+ A) -> +- B */
1771 (minus (plus:c @0 @1) @0)
1774 (minus (minus @0 @1) @0)
1777 (plus:c (minus @0 @1) @1)
1780 (minus @0 (plus:c @0 @1))
1783 (minus @0 (minus @0 @1))
1785 /* (A +- B) + (C - A) -> C +- B */
1786 /* (A + B) - (A - C) -> B + C */
1787 /* More cases are handled with comparisons. */
1789 (plus:c (plus:c @0 @1) (minus @2 @0))
1792 (plus:c (minus @0 @1) (minus @2 @0))
1795 (plus:c (pointer_diff @0 @1) (pointer_diff @2 @0))
1796 (if (TYPE_OVERFLOW_UNDEFINED (type)
1797 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0)))
1798 (pointer_diff @2 @1)))
1800 (minus (plus:c @0 @1) (minus @0 @2))
1803 /* (A +- CST1) +- CST2 -> A + CST3
1804 Use view_convert because it is safe for vectors and equivalent for
1806 (for outer_op (plus minus)
1807 (for inner_op (plus minus)
1808 neg_inner_op (minus plus)
1810 (outer_op (nop_convert (inner_op @0 CONSTANT_CLASS_P@1))
1812 /* If one of the types wraps, use that one. */
1813 (if (!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_WRAPS (type))
1814 /* If all 3 captures are CONSTANT_CLASS_P, punt, as we might recurse
1815 forever if something doesn't simplify into a constant. */
1816 (if (!CONSTANT_CLASS_P (@0))
1817 (if (outer_op == PLUS_EXPR)
1818 (plus (view_convert @0) (inner_op @2 (view_convert @1)))
1819 (minus (view_convert @0) (neg_inner_op @2 (view_convert @1)))))
1820 (if (!ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1821 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
1822 (if (outer_op == PLUS_EXPR)
1823 (view_convert (plus @0 (inner_op (view_convert @2) @1)))
1824 (view_convert (minus @0 (neg_inner_op (view_convert @2) @1))))
1825 /* If the constant operation overflows we cannot do the transform
1826 directly as we would introduce undefined overflow, for example
1827 with (a - 1) + INT_MIN. */
1828 (if (types_match (type, @0))
1829 (with { tree cst = const_binop (outer_op == inner_op
1830 ? PLUS_EXPR : MINUS_EXPR,
1832 (if (cst && !TREE_OVERFLOW (cst))
1833 (inner_op @0 { cst; } )
1834 /* X+INT_MAX+1 is X-INT_MIN. */
1835 (if (INTEGRAL_TYPE_P (type) && cst
1836 && wi::to_wide (cst) == wi::min_value (type))
1837 (neg_inner_op @0 { wide_int_to_tree (type, wi::to_wide (cst)); })
1838 /* Last resort, use some unsigned type. */
1839 (with { tree utype = unsigned_type_for (type); }
1841 (view_convert (inner_op
1842 (view_convert:utype @0)
1844 { drop_tree_overflow (cst); }))))))))))))))
1846 /* (CST1 - A) +- CST2 -> CST3 - A */
1847 (for outer_op (plus minus)
1849 (outer_op (minus CONSTANT_CLASS_P@1 @0) CONSTANT_CLASS_P@2)
1850 (with { tree cst = const_binop (outer_op, type, @1, @2); }
1851 (if (cst && !TREE_OVERFLOW (cst))
1852 (minus { cst; } @0)))))
1854 /* CST1 - (CST2 - A) -> CST3 + A */
1856 (minus CONSTANT_CLASS_P@1 (minus CONSTANT_CLASS_P@2 @0))
1857 (with { tree cst = const_binop (MINUS_EXPR, type, @1, @2); }
1858 (if (cst && !TREE_OVERFLOW (cst))
1859 (plus { cst; } @0))))
1863 (plus:c (bit_not @0) @0)
1864 (if (!TYPE_OVERFLOW_TRAPS (type))
1865 { build_all_ones_cst (type); }))
1869 (plus (convert? (bit_not @0)) integer_each_onep)
1870 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1871 (negate (convert @0))))
1875 (minus (convert? (negate @0)) integer_each_onep)
1876 (if (!TYPE_OVERFLOW_TRAPS (type)
1877 && tree_nop_conversion_p (type, TREE_TYPE (@0)))
1878 (bit_not (convert @0))))
1882 (minus integer_all_onesp @0)
1885 /* (T)(P + A) - (T)P -> (T) A */
1887 (minus (convert (plus:c @@0 @1))
1889 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
1890 /* For integer types, if A has a smaller type
1891 than T the result depends on the possible
1893 E.g. T=size_t, A=(unsigned)429497295, P>0.
1894 However, if an overflow in P + A would cause
1895 undefined behavior, we can assume that there
1897 || (INTEGRAL_TYPE_P (TREE_TYPE (@1))
1898 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))))
1901 (minus (convert (pointer_plus @@0 @1))
1903 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
1904 /* For pointer types, if the conversion of A to the
1905 final type requires a sign- or zero-extension,
1906 then we have to punt - it is not defined which
1908 || (POINTER_TYPE_P (TREE_TYPE (@0))
1909 && TREE_CODE (@1) == INTEGER_CST
1910 && tree_int_cst_sign_bit (@1) == 0))
1913 (pointer_diff (pointer_plus @@0 @1) @0)
1914 /* The second argument of pointer_plus must be interpreted as signed, and
1915 thus sign-extended if necessary. */
1916 (with { tree stype = signed_type_for (TREE_TYPE (@1)); }
1917 /* Use view_convert instead of convert here, as POINTER_PLUS_EXPR
1918 second arg is unsigned even when we need to consider it as signed,
1919 we don't want to diagnose overflow here. */
1920 (convert (view_convert:stype @1))))
1922 /* (T)P - (T)(P + A) -> -(T) A */
1924 (minus (convert? @0)
1925 (convert (plus:c @@0 @1)))
1926 (if (INTEGRAL_TYPE_P (type)
1927 && TYPE_OVERFLOW_UNDEFINED (type)
1928 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
1929 (with { tree utype = unsigned_type_for (type); }
1930 (convert (negate (convert:utype @1))))
1931 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
1932 /* For integer types, if A has a smaller type
1933 than T the result depends on the possible
1935 E.g. T=size_t, A=(unsigned)429497295, P>0.
1936 However, if an overflow in P + A would cause
1937 undefined behavior, we can assume that there
1939 || (INTEGRAL_TYPE_P (TREE_TYPE (@1))
1940 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))))
1941 (negate (convert @1)))))
1944 (convert (pointer_plus @@0 @1)))
1945 (if (INTEGRAL_TYPE_P (type)
1946 && TYPE_OVERFLOW_UNDEFINED (type)
1947 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
1948 (with { tree utype = unsigned_type_for (type); }
1949 (convert (negate (convert:utype @1))))
1950 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
1951 /* For pointer types, if the conversion of A to the
1952 final type requires a sign- or zero-extension,
1953 then we have to punt - it is not defined which
1955 || (POINTER_TYPE_P (TREE_TYPE (@0))
1956 && TREE_CODE (@1) == INTEGER_CST
1957 && tree_int_cst_sign_bit (@1) == 0))
1958 (negate (convert @1)))))
1960 (pointer_diff @0 (pointer_plus @@0 @1))
1961 /* The second argument of pointer_plus must be interpreted as signed, and
1962 thus sign-extended if necessary. */
1963 (with { tree stype = signed_type_for (TREE_TYPE (@1)); }
1964 /* Use view_convert instead of convert here, as POINTER_PLUS_EXPR
1965 second arg is unsigned even when we need to consider it as signed,
1966 we don't want to diagnose overflow here. */
1967 (negate (convert (view_convert:stype @1)))))
1969 /* (T)(P + A) - (T)(P + B) -> (T)A - (T)B */
1971 (minus (convert (plus:c @@0 @1))
1972 (convert (plus:c @0 @2)))
1973 (if (INTEGRAL_TYPE_P (type)
1974 && TYPE_OVERFLOW_UNDEFINED (type)
1975 && element_precision (type) <= element_precision (TREE_TYPE (@1))
1976 && element_precision (type) <= element_precision (TREE_TYPE (@2)))
1977 (with { tree utype = unsigned_type_for (type); }
1978 (convert (minus (convert:utype @1) (convert:utype @2))))
1979 (if (((element_precision (type) <= element_precision (TREE_TYPE (@1)))
1980 == (element_precision (type) <= element_precision (TREE_TYPE (@2))))
1981 && (element_precision (type) <= element_precision (TREE_TYPE (@1))
1982 /* For integer types, if A has a smaller type
1983 than T the result depends on the possible
1985 E.g. T=size_t, A=(unsigned)429497295, P>0.
1986 However, if an overflow in P + A would cause
1987 undefined behavior, we can assume that there
1989 || (INTEGRAL_TYPE_P (TREE_TYPE (@1))
1990 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
1991 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))
1992 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@2)))))
1993 (minus (convert @1) (convert @2)))))
1995 (minus (convert (pointer_plus @@0 @1))
1996 (convert (pointer_plus @0 @2)))
1997 (if (INTEGRAL_TYPE_P (type)
1998 && TYPE_OVERFLOW_UNDEFINED (type)
1999 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
2000 (with { tree utype = unsigned_type_for (type); }
2001 (convert (minus (convert:utype @1) (convert:utype @2))))
2002 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
2003 /* For pointer types, if the conversion of A to the
2004 final type requires a sign- or zero-extension,
2005 then we have to punt - it is not defined which
2007 || (POINTER_TYPE_P (TREE_TYPE (@0))
2008 && TREE_CODE (@1) == INTEGER_CST
2009 && tree_int_cst_sign_bit (@1) == 0
2010 && TREE_CODE (@2) == INTEGER_CST
2011 && tree_int_cst_sign_bit (@2) == 0))
2012 (minus (convert @1) (convert @2)))))
2014 (pointer_diff (pointer_plus @@0 @1) (pointer_plus @0 @2))
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 (minus (convert (view_convert:stype @1))
2022 (convert (view_convert:stype @2)))))))
2024 /* (A * C) +- (B * C) -> (A+-B) * C and (A * C) +- A -> A * (C+-1).
2025 Modeled after fold_plusminus_mult_expr. */
2026 (if (!TYPE_SATURATING (type)
2027 && (!FLOAT_TYPE_P (type) || flag_associative_math))
2028 (for plusminus (plus minus)
2030 (plusminus (mult:cs@3 @0 @1) (mult:cs@4 @0 @2))
2031 (if ((!ANY_INTEGRAL_TYPE_P (type)
2032 || TYPE_OVERFLOW_WRAPS (type)
2033 || (INTEGRAL_TYPE_P (type)
2034 && tree_expr_nonzero_p (@0)
2035 && expr_not_equal_to (@0, wi::minus_one (TYPE_PRECISION (type)))))
2036 /* If @1 +- @2 is constant require a hard single-use on either
2037 original operand (but not on both). */
2038 && (single_use (@3) || single_use (@4)))
2039 (mult (plusminus @1 @2) @0)))
2040 /* We cannot generate constant 1 for fract. */
2041 (if (!ALL_FRACT_MODE_P (TYPE_MODE (type)))
2043 (plusminus @0 (mult:c@3 @0 @2))
2044 (if ((!ANY_INTEGRAL_TYPE_P (type)
2045 || TYPE_OVERFLOW_WRAPS (type)
2046 || (INTEGRAL_TYPE_P (type)
2047 && tree_expr_nonzero_p (@0)
2048 && expr_not_equal_to (@0, wi::minus_one (TYPE_PRECISION (type)))))
2050 (mult (plusminus { build_one_cst (type); } @2) @0)))
2052 (plusminus (mult:c@3 @0 @2) @0)
2053 (if ((!ANY_INTEGRAL_TYPE_P (type)
2054 || TYPE_OVERFLOW_WRAPS (type)
2055 || (INTEGRAL_TYPE_P (type)
2056 && tree_expr_nonzero_p (@0)
2057 && expr_not_equal_to (@0, wi::minus_one (TYPE_PRECISION (type)))))
2059 (mult (plusminus @2 { build_one_cst (type); }) @0))))))
2061 /* Simplifications of MIN_EXPR, MAX_EXPR, fmin() and fmax(). */
2063 (for minmax (min max FMIN_ALL FMAX_ALL)
2067 /* min(max(x,y),y) -> y. */
2069 (min:c (max:c @0 @1) @1)
2071 /* max(min(x,y),y) -> y. */
2073 (max:c (min:c @0 @1) @1)
2075 /* max(a,-a) -> abs(a). */
2077 (max:c @0 (negate @0))
2078 (if (TREE_CODE (type) != COMPLEX_TYPE
2079 && (! ANY_INTEGRAL_TYPE_P (type)
2080 || TYPE_OVERFLOW_UNDEFINED (type)))
2082 /* min(a,-a) -> -abs(a). */
2084 (min:c @0 (negate @0))
2085 (if (TREE_CODE (type) != COMPLEX_TYPE
2086 && (! ANY_INTEGRAL_TYPE_P (type)
2087 || TYPE_OVERFLOW_UNDEFINED (type)))
2092 (if (INTEGRAL_TYPE_P (type)
2093 && TYPE_MIN_VALUE (type)
2094 && operand_equal_p (@1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
2096 (if (INTEGRAL_TYPE_P (type)
2097 && TYPE_MAX_VALUE (type)
2098 && operand_equal_p (@1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
2103 (if (INTEGRAL_TYPE_P (type)
2104 && TYPE_MAX_VALUE (type)
2105 && operand_equal_p (@1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
2107 (if (INTEGRAL_TYPE_P (type)
2108 && TYPE_MIN_VALUE (type)
2109 && operand_equal_p (@1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
2112 /* max (a, a + CST) -> a + CST where CST is positive. */
2113 /* max (a, a + CST) -> a where CST is negative. */
2115 (max:c @0 (plus@2 @0 INTEGER_CST@1))
2116 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
2117 (if (tree_int_cst_sgn (@1) > 0)
2121 /* min (a, a + CST) -> a where CST is positive. */
2122 /* min (a, a + CST) -> a + CST where CST is negative. */
2124 (min:c @0 (plus@2 @0 INTEGER_CST@1))
2125 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
2126 (if (tree_int_cst_sgn (@1) > 0)
2130 /* (convert (minmax ((convert (x) c)))) -> minmax (x c) if x is promoted
2131 and the outer convert demotes the expression back to x's type. */
2132 (for minmax (min max)
2134 (convert (minmax@0 (convert @1) INTEGER_CST@2))
2135 (if (INTEGRAL_TYPE_P (type)
2136 && types_match (@1, type) && int_fits_type_p (@2, type)
2137 && TYPE_SIGN (TREE_TYPE (@0)) == TYPE_SIGN (type)
2138 && TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type))
2139 (minmax @1 (convert @2)))))
2141 (for minmax (FMIN_ALL FMAX_ALL)
2142 /* If either argument is NaN, return the other one. Avoid the
2143 transformation if we get (and honor) a signalling NaN. */
2145 (minmax:c @0 REAL_CST@1)
2146 (if (real_isnan (TREE_REAL_CST_PTR (@1))
2147 && (!HONOR_SNANS (@1) || !TREE_REAL_CST (@1).signalling))
2149 /* Convert fmin/fmax to MIN_EXPR/MAX_EXPR. C99 requires these
2150 functions to return the numeric arg if the other one is NaN.
2151 MIN and MAX don't honor that, so only transform if -ffinite-math-only
2152 is set. C99 doesn't require -0.0 to be handled, so we don't have to
2153 worry about it either. */
2154 (if (flag_finite_math_only)
2161 /* min (-A, -B) -> -max (A, B) */
2162 (for minmax (min max FMIN_ALL FMAX_ALL)
2163 maxmin (max min FMAX_ALL FMIN_ALL)
2165 (minmax (negate:s@2 @0) (negate:s@3 @1))
2166 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
2167 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2168 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
2169 (negate (maxmin @0 @1)))))
2170 /* MIN (~X, ~Y) -> ~MAX (X, Y)
2171 MAX (~X, ~Y) -> ~MIN (X, Y) */
2172 (for minmax (min max)
2175 (minmax (bit_not:s@2 @0) (bit_not:s@3 @1))
2176 (bit_not (maxmin @0 @1))))
2178 /* MIN (X, Y) == X -> X <= Y */
2179 (for minmax (min min max max)
2183 (cmp:c (minmax:c @0 @1) @0)
2184 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0)))
2186 /* MIN (X, 5) == 0 -> X == 0
2187 MIN (X, 5) == 7 -> false */
2190 (cmp (min @0 INTEGER_CST@1) INTEGER_CST@2)
2191 (if (wi::lt_p (wi::to_wide (@1), wi::to_wide (@2),
2192 TYPE_SIGN (TREE_TYPE (@0))))
2193 { constant_boolean_node (cmp == NE_EXPR, type); }
2194 (if (wi::gt_p (wi::to_wide (@1), wi::to_wide (@2),
2195 TYPE_SIGN (TREE_TYPE (@0))))
2199 (cmp (max @0 INTEGER_CST@1) INTEGER_CST@2)
2200 (if (wi::gt_p (wi::to_wide (@1), wi::to_wide (@2),
2201 TYPE_SIGN (TREE_TYPE (@0))))
2202 { constant_boolean_node (cmp == NE_EXPR, type); }
2203 (if (wi::lt_p (wi::to_wide (@1), wi::to_wide (@2),
2204 TYPE_SIGN (TREE_TYPE (@0))))
2206 /* MIN (X, C1) < C2 -> X < C2 || C1 < C2 */
2207 (for minmax (min min max max min min max max )
2208 cmp (lt le gt ge gt ge lt le )
2209 comb (bit_ior bit_ior bit_ior bit_ior bit_and bit_and bit_and bit_and)
2211 (cmp (minmax @0 INTEGER_CST@1) INTEGER_CST@2)
2212 (comb (cmp @0 @2) (cmp @1 @2))))
2214 /* Simplifications of shift and rotates. */
2216 (for rotate (lrotate rrotate)
2218 (rotate integer_all_onesp@0 @1)
2221 /* Optimize -1 >> x for arithmetic right shifts. */
2223 (rshift integer_all_onesp@0 @1)
2224 (if (!TYPE_UNSIGNED (type)
2225 && tree_expr_nonnegative_p (@1))
2228 /* Optimize (x >> c) << c into x & (-1<<c). */
2230 (lshift (rshift @0 INTEGER_CST@1) @1)
2231 (if (wi::ltu_p (wi::to_wide (@1), element_precision (type)))
2232 (bit_and @0 (lshift { build_minus_one_cst (type); } @1))))
2234 /* Optimize (x << c) >> c into x & ((unsigned)-1 >> c) for unsigned
2237 (rshift (lshift @0 INTEGER_CST@1) @1)
2238 (if (TYPE_UNSIGNED (type)
2239 && (wi::ltu_p (wi::to_wide (@1), element_precision (type))))
2240 (bit_and @0 (rshift { build_minus_one_cst (type); } @1))))
2242 (for shiftrotate (lrotate rrotate lshift rshift)
2244 (shiftrotate @0 integer_zerop)
2247 (shiftrotate integer_zerop@0 @1)
2249 /* Prefer vector1 << scalar to vector1 << vector2
2250 if vector2 is uniform. */
2251 (for vec (VECTOR_CST CONSTRUCTOR)
2253 (shiftrotate @0 vec@1)
2254 (with { tree tem = uniform_vector_p (@1); }
2256 (shiftrotate @0 { tem; }))))))
2258 /* Simplify X << Y where Y's low width bits are 0 to X, as only valid
2259 Y is 0. Similarly for X >> Y. */
2261 (for shift (lshift rshift)
2263 (shift @0 SSA_NAME@1)
2264 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1)))
2266 int width = ceil_log2 (element_precision (TREE_TYPE (@0)));
2267 int prec = TYPE_PRECISION (TREE_TYPE (@1));
2269 (if ((get_nonzero_bits (@1) & wi::mask (width, false, prec)) == 0)
2273 /* Rewrite an LROTATE_EXPR by a constant into an
2274 RROTATE_EXPR by a new constant. */
2276 (lrotate @0 INTEGER_CST@1)
2277 (rrotate @0 { const_binop (MINUS_EXPR, TREE_TYPE (@1),
2278 build_int_cst (TREE_TYPE (@1),
2279 element_precision (type)), @1); }))
2281 /* Turn (a OP c1) OP c2 into a OP (c1+c2). */
2282 (for op (lrotate rrotate rshift lshift)
2284 (op (op @0 INTEGER_CST@1) INTEGER_CST@2)
2285 (with { unsigned int prec = element_precision (type); }
2286 (if (wi::ge_p (wi::to_wide (@1), 0, TYPE_SIGN (TREE_TYPE (@1)))
2287 && wi::lt_p (wi::to_wide (@1), prec, TYPE_SIGN (TREE_TYPE (@1)))
2288 && wi::ge_p (wi::to_wide (@2), 0, TYPE_SIGN (TREE_TYPE (@2)))
2289 && wi::lt_p (wi::to_wide (@2), prec, TYPE_SIGN (TREE_TYPE (@2))))
2290 (with { unsigned int low = (tree_to_uhwi (@1)
2291 + tree_to_uhwi (@2)); }
2292 /* Deal with a OP (c1 + c2) being undefined but (a OP c1) OP c2
2293 being well defined. */
2295 (if (op == LROTATE_EXPR || op == RROTATE_EXPR)
2296 (op @0 { build_int_cst (TREE_TYPE (@1), low % prec); })
2297 (if (TYPE_UNSIGNED (type) || op == LSHIFT_EXPR)
2298 { build_zero_cst (type); }
2299 (op @0 { build_int_cst (TREE_TYPE (@1), prec - 1); })))
2300 (op @0 { build_int_cst (TREE_TYPE (@1), low); })))))))
2303 /* ((1 << A) & 1) != 0 -> A == 0
2304 ((1 << A) & 1) == 0 -> A != 0 */
2308 (cmp (bit_and (lshift integer_onep @0) integer_onep) integer_zerop)
2309 (icmp @0 { build_zero_cst (TREE_TYPE (@0)); })))
2311 /* (CST1 << A) == CST2 -> A == ctz (CST2) - ctz (CST1)
2312 (CST1 << A) != CST2 -> A != ctz (CST2) - ctz (CST1)
2316 (cmp (lshift INTEGER_CST@0 @1) INTEGER_CST@2)
2317 (with { int cand = wi::ctz (wi::to_wide (@2)) - wi::ctz (wi::to_wide (@0)); }
2319 || (!integer_zerop (@2)
2320 && wi::lshift (wi::to_wide (@0), cand) != wi::to_wide (@2)))
2321 { constant_boolean_node (cmp == NE_EXPR, type); }
2322 (if (!integer_zerop (@2)
2323 && wi::lshift (wi::to_wide (@0), cand) == wi::to_wide (@2))
2324 (cmp @1 { build_int_cst (TREE_TYPE (@1), cand); }))))))
2326 /* Fold (X << C1) & C2 into (X << C1) & (C2 | ((1 << C1) - 1))
2327 (X >> C1) & C2 into (X >> C1) & (C2 | ~((type) -1 >> C1))
2328 if the new mask might be further optimized. */
2329 (for shift (lshift rshift)
2331 (bit_and (convert?:s@4 (shift:s@5 (convert1?@3 @0) INTEGER_CST@1))
2333 (if (tree_nop_conversion_p (TREE_TYPE (@4), TREE_TYPE (@5))
2334 && TYPE_PRECISION (type) <= HOST_BITS_PER_WIDE_INT
2335 && tree_fits_uhwi_p (@1)
2336 && tree_to_uhwi (@1) > 0
2337 && tree_to_uhwi (@1) < TYPE_PRECISION (type))
2340 unsigned int shiftc = tree_to_uhwi (@1);
2341 unsigned HOST_WIDE_INT mask = TREE_INT_CST_LOW (@2);
2342 unsigned HOST_WIDE_INT newmask, zerobits = 0;
2343 tree shift_type = TREE_TYPE (@3);
2346 if (shift == LSHIFT_EXPR)
2347 zerobits = ((HOST_WIDE_INT_1U << shiftc) - 1);
2348 else if (shift == RSHIFT_EXPR
2349 && type_has_mode_precision_p (shift_type))
2351 prec = TYPE_PRECISION (TREE_TYPE (@3));
2353 /* See if more bits can be proven as zero because of
2356 && TYPE_UNSIGNED (TREE_TYPE (@0)))
2358 tree inner_type = TREE_TYPE (@0);
2359 if (type_has_mode_precision_p (inner_type)
2360 && TYPE_PRECISION (inner_type) < prec)
2362 prec = TYPE_PRECISION (inner_type);
2363 /* See if we can shorten the right shift. */
2365 shift_type = inner_type;
2366 /* Otherwise X >> C1 is all zeros, so we'll optimize
2367 it into (X, 0) later on by making sure zerobits
2371 zerobits = HOST_WIDE_INT_M1U;
2374 zerobits >>= HOST_BITS_PER_WIDE_INT - shiftc;
2375 zerobits <<= prec - shiftc;
2377 /* For arithmetic shift if sign bit could be set, zerobits
2378 can contain actually sign bits, so no transformation is
2379 possible, unless MASK masks them all away. In that
2380 case the shift needs to be converted into logical shift. */
2381 if (!TYPE_UNSIGNED (TREE_TYPE (@3))
2382 && prec == TYPE_PRECISION (TREE_TYPE (@3)))
2384 if ((mask & zerobits) == 0)
2385 shift_type = unsigned_type_for (TREE_TYPE (@3));
2391 /* ((X << 16) & 0xff00) is (X, 0). */
2392 (if ((mask & zerobits) == mask)
2393 { build_int_cst (type, 0); }
2394 (with { newmask = mask | zerobits; }
2395 (if (newmask != mask && (newmask & (newmask + 1)) == 0)
2398 /* Only do the transformation if NEWMASK is some integer
2400 for (prec = BITS_PER_UNIT;
2401 prec < HOST_BITS_PER_WIDE_INT; prec <<= 1)
2402 if (newmask == (HOST_WIDE_INT_1U << prec) - 1)
2405 (if (prec < HOST_BITS_PER_WIDE_INT
2406 || newmask == HOST_WIDE_INT_M1U)
2408 { tree newmaskt = build_int_cst_type (TREE_TYPE (@2), newmask); }
2409 (if (!tree_int_cst_equal (newmaskt, @2))
2410 (if (shift_type != TREE_TYPE (@3))
2411 (bit_and (convert (shift:shift_type (convert @3) @1)) { newmaskt; })
2412 (bit_and @4 { newmaskt; })))))))))))))
2414 /* Fold (X {&,^,|} C2) << C1 into (X << C1) {&,^,|} (C2 << C1)
2415 (X {&,^,|} C2) >> C1 into (X >> C1) & (C2 >> C1). */
2416 (for shift (lshift rshift)
2417 (for bit_op (bit_and bit_xor bit_ior)
2419 (shift (convert?:s (bit_op:s @0 INTEGER_CST@2)) INTEGER_CST@1)
2420 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2421 (with { tree mask = int_const_binop (shift, fold_convert (type, @2), @1); }
2422 (bit_op (shift (convert @0) @1) { mask; }))))))
2424 /* ~(~X >> Y) -> X >> Y (for arithmetic shift). */
2426 (bit_not (convert1?:s (rshift:s (convert2?@0 (bit_not @1)) @2)))
2427 (if (!TYPE_UNSIGNED (TREE_TYPE (@0))
2428 && (element_precision (TREE_TYPE (@0))
2429 <= element_precision (TREE_TYPE (@1))
2430 || !TYPE_UNSIGNED (TREE_TYPE (@1))))
2432 { tree shift_type = TREE_TYPE (@0); }
2433 (convert (rshift (convert:shift_type @1) @2)))))
2435 /* ~(~X >>r Y) -> X >>r Y
2436 ~(~X <<r Y) -> X <<r Y */
2437 (for rotate (lrotate rrotate)
2439 (bit_not (convert1?:s (rotate:s (convert2?@0 (bit_not @1)) @2)))
2440 (if ((element_precision (TREE_TYPE (@0))
2441 <= element_precision (TREE_TYPE (@1))
2442 || !TYPE_UNSIGNED (TREE_TYPE (@1)))
2443 && (element_precision (type) <= element_precision (TREE_TYPE (@0))
2444 || !TYPE_UNSIGNED (TREE_TYPE (@0))))
2446 { tree rotate_type = TREE_TYPE (@0); }
2447 (convert (rotate (convert:rotate_type @1) @2))))))
2449 /* Simplifications of conversions. */
2451 /* Basic strip-useless-type-conversions / strip_nops. */
2452 (for cvt (convert view_convert float fix_trunc)
2455 (if ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@0)))
2456 || (GENERIC && type == TREE_TYPE (@0)))
2459 /* Contract view-conversions. */
2461 (view_convert (view_convert @0))
2464 /* For integral conversions with the same precision or pointer
2465 conversions use a NOP_EXPR instead. */
2468 (if ((INTEGRAL_TYPE_P (type) || POINTER_TYPE_P (type))
2469 && (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
2470 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (@0)))
2473 /* Strip inner integral conversions that do not change precision or size, or
2474 zero-extend while keeping the same size (for bool-to-char). */
2476 (view_convert (convert@0 @1))
2477 (if ((INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
2478 && (INTEGRAL_TYPE_P (TREE_TYPE (@1)) || POINTER_TYPE_P (TREE_TYPE (@1)))
2479 && TYPE_SIZE (TREE_TYPE (@0)) == TYPE_SIZE (TREE_TYPE (@1))
2480 && (TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1))
2481 || (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (TREE_TYPE (@1))
2482 && TYPE_UNSIGNED (TREE_TYPE (@1)))))
2485 /* Re-association barriers around constants and other re-association
2486 barriers can be removed. */
2488 (paren CONSTANT_CLASS_P@0)
2491 (paren (paren@1 @0))
2494 /* Handle cases of two conversions in a row. */
2495 (for ocvt (convert float fix_trunc)
2496 (for icvt (convert float)
2501 tree inside_type = TREE_TYPE (@0);
2502 tree inter_type = TREE_TYPE (@1);
2503 int inside_int = INTEGRAL_TYPE_P (inside_type);
2504 int inside_ptr = POINTER_TYPE_P (inside_type);
2505 int inside_float = FLOAT_TYPE_P (inside_type);
2506 int inside_vec = VECTOR_TYPE_P (inside_type);
2507 unsigned int inside_prec = TYPE_PRECISION (inside_type);
2508 int inside_unsignedp = TYPE_UNSIGNED (inside_type);
2509 int inter_int = INTEGRAL_TYPE_P (inter_type);
2510 int inter_ptr = POINTER_TYPE_P (inter_type);
2511 int inter_float = FLOAT_TYPE_P (inter_type);
2512 int inter_vec = VECTOR_TYPE_P (inter_type);
2513 unsigned int inter_prec = TYPE_PRECISION (inter_type);
2514 int inter_unsignedp = TYPE_UNSIGNED (inter_type);
2515 int final_int = INTEGRAL_TYPE_P (type);
2516 int final_ptr = POINTER_TYPE_P (type);
2517 int final_float = FLOAT_TYPE_P (type);
2518 int final_vec = VECTOR_TYPE_P (type);
2519 unsigned int final_prec = TYPE_PRECISION (type);
2520 int final_unsignedp = TYPE_UNSIGNED (type);
2523 /* In addition to the cases of two conversions in a row
2524 handled below, if we are converting something to its own
2525 type via an object of identical or wider precision, neither
2526 conversion is needed. */
2527 (if (((GIMPLE && useless_type_conversion_p (type, inside_type))
2529 && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (inside_type)))
2530 && (((inter_int || inter_ptr) && final_int)
2531 || (inter_float && final_float))
2532 && inter_prec >= final_prec)
2535 /* Likewise, if the intermediate and initial types are either both
2536 float or both integer, we don't need the middle conversion if the
2537 former is wider than the latter and doesn't change the signedness
2538 (for integers). Avoid this if the final type is a pointer since
2539 then we sometimes need the middle conversion. */
2540 (if (((inter_int && inside_int) || (inter_float && inside_float))
2541 && (final_int || final_float)
2542 && inter_prec >= inside_prec
2543 && (inter_float || inter_unsignedp == inside_unsignedp))
2546 /* If we have a sign-extension of a zero-extended value, we can
2547 replace that by a single zero-extension. Likewise if the
2548 final conversion does not change precision we can drop the
2549 intermediate conversion. */
2550 (if (inside_int && inter_int && final_int
2551 && ((inside_prec < inter_prec && inter_prec < final_prec
2552 && inside_unsignedp && !inter_unsignedp)
2553 || final_prec == inter_prec))
2556 /* Two conversions in a row are not needed unless:
2557 - some conversion is floating-point (overstrict for now), or
2558 - some conversion is a vector (overstrict for now), or
2559 - the intermediate type is narrower than both initial and
2561 - the intermediate type and innermost type differ in signedness,
2562 and the outermost type is wider than the intermediate, or
2563 - the initial type is a pointer type and the precisions of the
2564 intermediate and final types differ, or
2565 - the final type is a pointer type and the precisions of the
2566 initial and intermediate types differ. */
2567 (if (! inside_float && ! inter_float && ! final_float
2568 && ! inside_vec && ! inter_vec && ! final_vec
2569 && (inter_prec >= inside_prec || inter_prec >= final_prec)
2570 && ! (inside_int && inter_int
2571 && inter_unsignedp != inside_unsignedp
2572 && inter_prec < final_prec)
2573 && ((inter_unsignedp && inter_prec > inside_prec)
2574 == (final_unsignedp && final_prec > inter_prec))
2575 && ! (inside_ptr && inter_prec != final_prec)
2576 && ! (final_ptr && inside_prec != inter_prec))
2579 /* A truncation to an unsigned type (a zero-extension) should be
2580 canonicalized as bitwise and of a mask. */
2581 (if (GIMPLE /* PR70366: doing this in GENERIC breaks -Wconversion. */
2582 && final_int && inter_int && inside_int
2583 && final_prec == inside_prec
2584 && final_prec > inter_prec
2586 (convert (bit_and @0 { wide_int_to_tree
2588 wi::mask (inter_prec, false,
2589 TYPE_PRECISION (inside_type))); })))
2591 /* If we are converting an integer to a floating-point that can
2592 represent it exactly and back to an integer, we can skip the
2593 floating-point conversion. */
2594 (if (GIMPLE /* PR66211 */
2595 && inside_int && inter_float && final_int &&
2596 (unsigned) significand_size (TYPE_MODE (inter_type))
2597 >= inside_prec - !inside_unsignedp)
2600 /* If we have a narrowing conversion to an integral type that is fed by a
2601 BIT_AND_EXPR, we might be able to remove the BIT_AND_EXPR if it merely
2602 masks off bits outside the final type (and nothing else). */
2604 (convert (bit_and @0 INTEGER_CST@1))
2605 (if (INTEGRAL_TYPE_P (type)
2606 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
2607 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))
2608 && operand_equal_p (@1, build_low_bits_mask (TREE_TYPE (@1),
2609 TYPE_PRECISION (type)), 0))
2613 /* (X /[ex] A) * A -> X. */
2615 (mult (convert1? (exact_div @0 @@1)) (convert2? @1))
2618 /* Canonicalization of binary operations. */
2620 /* Convert X + -C into X - C. */
2622 (plus @0 REAL_CST@1)
2623 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
2624 (with { tree tem = const_unop (NEGATE_EXPR, type, @1); }
2625 (if (!TREE_OVERFLOW (tem) || !flag_trapping_math)
2626 (minus @0 { tem; })))))
2628 /* Convert x+x into x*2. */
2631 (if (SCALAR_FLOAT_TYPE_P (type))
2632 (mult @0 { build_real (type, dconst2); })
2633 (if (INTEGRAL_TYPE_P (type))
2634 (mult @0 { build_int_cst (type, 2); }))))
2638 (minus integer_zerop @1)
2641 (pointer_diff integer_zerop @1)
2642 (negate (convert @1)))
2644 /* (ARG0 - ARG1) is the same as (-ARG1 + ARG0). So check whether
2645 ARG0 is zero and X + ARG0 reduces to X, since that would mean
2646 (-ARG1 + ARG0) reduces to -ARG1. */
2648 (minus real_zerop@0 @1)
2649 (if (fold_real_zero_addition_p (type, @0, 0))
2652 /* Transform x * -1 into -x. */
2654 (mult @0 integer_minus_onep)
2657 /* Reassociate (X * CST) * Y to (X * Y) * CST. This does not introduce
2658 signed overflow for CST != 0 && CST != -1. */
2660 (mult:c (mult:s@3 @0 INTEGER_CST@1) @2)
2661 (if (TREE_CODE (@2) != INTEGER_CST
2663 && !integer_zerop (@1) && !integer_minus_onep (@1))
2664 (mult (mult @0 @2) @1)))
2666 /* True if we can easily extract the real and imaginary parts of a complex
2668 (match compositional_complex
2669 (convert? (complex @0 @1)))
2671 /* COMPLEX_EXPR and REALPART/IMAGPART_EXPR cancellations. */
2673 (complex (realpart @0) (imagpart @0))
2676 (realpart (complex @0 @1))
2679 (imagpart (complex @0 @1))
2682 /* Sometimes we only care about half of a complex expression. */
2684 (realpart (convert?:s (conj:s @0)))
2685 (convert (realpart @0)))
2687 (imagpart (convert?:s (conj:s @0)))
2688 (convert (negate (imagpart @0))))
2689 (for part (realpart imagpart)
2690 (for op (plus minus)
2692 (part (convert?:s@2 (op:s @0 @1)))
2693 (convert (op (part @0) (part @1))))))
2695 (realpart (convert?:s (CEXPI:s @0)))
2698 (imagpart (convert?:s (CEXPI:s @0)))
2701 /* conj(conj(x)) -> x */
2703 (conj (convert? (conj @0)))
2704 (if (tree_nop_conversion_p (TREE_TYPE (@0), type))
2707 /* conj({x,y}) -> {x,-y} */
2709 (conj (convert?:s (complex:s @0 @1)))
2710 (with { tree itype = TREE_TYPE (type); }
2711 (complex (convert:itype @0) (negate (convert:itype @1)))))
2713 /* BSWAP simplifications, transforms checked by gcc.dg/builtin-bswap-8.c. */
2714 (for bswap (BUILT_IN_BSWAP16 BUILT_IN_BSWAP32 BUILT_IN_BSWAP64)
2719 (bswap (bit_not (bswap @0)))
2721 (for bitop (bit_xor bit_ior bit_and)
2723 (bswap (bitop:c (bswap @0) @1))
2724 (bitop @0 (bswap @1)))))
2727 /* Combine COND_EXPRs and VEC_COND_EXPRs. */
2729 /* Simplify constant conditions.
2730 Only optimize constant conditions when the selected branch
2731 has the same type as the COND_EXPR. This avoids optimizing
2732 away "c ? x : throw", where the throw has a void type.
2733 Note that we cannot throw away the fold-const.c variant nor
2734 this one as we depend on doing this transform before possibly
2735 A ? B : B -> B triggers and the fold-const.c one can optimize
2736 0 ? A : B to B even if A has side-effects. Something
2737 genmatch cannot handle. */
2739 (cond INTEGER_CST@0 @1 @2)
2740 (if (integer_zerop (@0))
2741 (if (!VOID_TYPE_P (TREE_TYPE (@2)) || VOID_TYPE_P (type))
2743 (if (!VOID_TYPE_P (TREE_TYPE (@1)) || VOID_TYPE_P (type))
2746 (vec_cond VECTOR_CST@0 @1 @2)
2747 (if (integer_all_onesp (@0))
2749 (if (integer_zerop (@0))
2752 /* Simplification moved from fold_cond_expr_with_comparison. It may also
2754 /* This pattern implements two kinds simplification:
2757 (cond (cmp (convert1? x) c1) (convert2? x) c2) -> (minmax (x c)) if:
2758 1) Conversions are type widening from smaller type.
2759 2) Const c1 equals to c2 after canonicalizing comparison.
2760 3) Comparison has tree code LT, LE, GT or GE.
2761 This specific pattern is needed when (cmp (convert x) c) may not
2762 be simplified by comparison patterns because of multiple uses of
2763 x. It also makes sense here because simplifying across multiple
2764 referred var is always benefitial for complicated cases.
2767 (cond (eq (convert1? x) c1) (convert2? x) c2) -> (cond (eq x c1) c1 c2). */
2768 (for cmp (lt le gt ge eq)
2770 (cond (cmp (convert1? @1) INTEGER_CST@3) (convert2? @1) INTEGER_CST@2)
2773 tree from_type = TREE_TYPE (@1);
2774 tree c1_type = TREE_TYPE (@3), c2_type = TREE_TYPE (@2);
2775 enum tree_code code = ERROR_MARK;
2777 if (INTEGRAL_TYPE_P (from_type)
2778 && int_fits_type_p (@2, from_type)
2779 && (types_match (c1_type, from_type)
2780 || (TYPE_PRECISION (c1_type) > TYPE_PRECISION (from_type)
2781 && (TYPE_UNSIGNED (from_type)
2782 || TYPE_SIGN (c1_type) == TYPE_SIGN (from_type))))
2783 && (types_match (c2_type, from_type)
2784 || (TYPE_PRECISION (c2_type) > TYPE_PRECISION (from_type)
2785 && (TYPE_UNSIGNED (from_type)
2786 || TYPE_SIGN (c2_type) == TYPE_SIGN (from_type)))))
2790 if (wi::to_widest (@3) == (wi::to_widest (@2) - 1))
2792 /* X <= Y - 1 equals to X < Y. */
2795 /* X > Y - 1 equals to X >= Y. */
2799 if (wi::to_widest (@3) == (wi::to_widest (@2) + 1))
2801 /* X < Y + 1 equals to X <= Y. */
2804 /* X >= Y + 1 equals to X > Y. */
2808 if (code != ERROR_MARK
2809 || wi::to_widest (@2) == wi::to_widest (@3))
2811 if (cmp == LT_EXPR || cmp == LE_EXPR)
2813 if (cmp == GT_EXPR || cmp == GE_EXPR)
2817 /* Can do A == C1 ? A : C2 -> A == C1 ? C1 : C2? */
2818 else if (int_fits_type_p (@3, from_type))
2822 (if (code == MAX_EXPR)
2823 (convert (max @1 (convert @2)))
2824 (if (code == MIN_EXPR)
2825 (convert (min @1 (convert @2)))
2826 (if (code == EQ_EXPR)
2827 (convert (cond (eq @1 (convert @3))
2828 (convert:from_type @3) (convert:from_type @2)))))))))
2830 /* (cond (cmp (convert? x) c1) (op x c2) c3) -> (op (minmax x c1) c2) if:
2832 1) OP is PLUS or MINUS.
2833 2) CMP is LT, LE, GT or GE.
2834 3) C3 == (C1 op C2), and computation doesn't have undefined behavior.
2836 This pattern also handles special cases like:
2838 A) Operand x is a unsigned to signed type conversion and c1 is
2839 integer zero. In this case,
2840 (signed type)x < 0 <=> x > MAX_VAL(signed type)
2841 (signed type)x >= 0 <=> x <= MAX_VAL(signed type)
2842 B) Const c1 may not equal to (C3 op' C2). In this case we also
2843 check equality for (c1+1) and (c1-1) by adjusting comparison
2846 TODO: Though signed type is handled by this pattern, it cannot be
2847 simplified at the moment because C standard requires additional
2848 type promotion. In order to match&simplify it here, the IR needs
2849 to be cleaned up by other optimizers, i.e, VRP. */
2850 (for op (plus minus)
2851 (for cmp (lt le gt ge)
2853 (cond (cmp (convert? @X) INTEGER_CST@1) (op @X INTEGER_CST@2) INTEGER_CST@3)
2854 (with { tree from_type = TREE_TYPE (@X), to_type = TREE_TYPE (@1); }
2855 (if (types_match (from_type, to_type)
2856 /* Check if it is special case A). */
2857 || (TYPE_UNSIGNED (from_type)
2858 && !TYPE_UNSIGNED (to_type)
2859 && TYPE_PRECISION (from_type) == TYPE_PRECISION (to_type)
2860 && integer_zerop (@1)
2861 && (cmp == LT_EXPR || cmp == GE_EXPR)))
2864 wi::overflow_type overflow = wi::OVF_NONE;
2865 enum tree_code code, cmp_code = cmp;
2867 wide_int c1 = wi::to_wide (@1);
2868 wide_int c2 = wi::to_wide (@2);
2869 wide_int c3 = wi::to_wide (@3);
2870 signop sgn = TYPE_SIGN (from_type);
2872 /* Handle special case A), given x of unsigned type:
2873 ((signed type)x < 0) <=> (x > MAX_VAL(signed type))
2874 ((signed type)x >= 0) <=> (x <= MAX_VAL(signed type)) */
2875 if (!types_match (from_type, to_type))
2877 if (cmp_code == LT_EXPR)
2879 if (cmp_code == GE_EXPR)
2881 c1 = wi::max_value (to_type);
2883 /* To simplify this pattern, we require c3 = (c1 op c2). Here we
2884 compute (c3 op' c2) and check if it equals to c1 with op' being
2885 the inverted operator of op. Make sure overflow doesn't happen
2886 if it is undefined. */
2887 if (op == PLUS_EXPR)
2888 real_c1 = wi::sub (c3, c2, sgn, &overflow);
2890 real_c1 = wi::add (c3, c2, sgn, &overflow);
2893 if (!overflow || !TYPE_OVERFLOW_UNDEFINED (from_type))
2895 /* Check if c1 equals to real_c1. Boundary condition is handled
2896 by adjusting comparison operation if necessary. */
2897 if (!wi::cmp (wi::sub (real_c1, 1, sgn, &overflow), c1, sgn)
2900 /* X <= Y - 1 equals to X < Y. */
2901 if (cmp_code == LE_EXPR)
2903 /* X > Y - 1 equals to X >= Y. */
2904 if (cmp_code == GT_EXPR)
2907 if (!wi::cmp (wi::add (real_c1, 1, sgn, &overflow), c1, sgn)
2910 /* X < Y + 1 equals to X <= Y. */
2911 if (cmp_code == LT_EXPR)
2913 /* X >= Y + 1 equals to X > Y. */
2914 if (cmp_code == GE_EXPR)
2917 if (code != cmp_code || !wi::cmp (real_c1, c1, sgn))
2919 if (cmp_code == LT_EXPR || cmp_code == LE_EXPR)
2921 if (cmp_code == GT_EXPR || cmp_code == GE_EXPR)
2926 (if (code == MAX_EXPR)
2927 (op (max @X { wide_int_to_tree (from_type, real_c1); })
2928 { wide_int_to_tree (from_type, c2); })
2929 (if (code == MIN_EXPR)
2930 (op (min @X { wide_int_to_tree (from_type, real_c1); })
2931 { wide_int_to_tree (from_type, c2); })))))))))
2933 (for cnd (cond vec_cond)
2934 /* A ? B : (A ? X : C) -> A ? B : C. */
2936 (cnd @0 (cnd @0 @1 @2) @3)
2939 (cnd @0 @1 (cnd @0 @2 @3))
2941 /* A ? B : (!A ? C : X) -> A ? B : C. */
2942 /* ??? This matches embedded conditions open-coded because genmatch
2943 would generate matching code for conditions in separate stmts only.
2944 The following is still important to merge then and else arm cases
2945 from if-conversion. */
2947 (cnd @0 @1 (cnd @2 @3 @4))
2948 (if (COMPARISON_CLASS_P (@0)
2949 && COMPARISON_CLASS_P (@2)
2950 && invert_tree_comparison
2951 (TREE_CODE (@0), HONOR_NANS (TREE_OPERAND (@0, 0))) == TREE_CODE (@2)
2952 && operand_equal_p (TREE_OPERAND (@0, 0), TREE_OPERAND (@2, 0), 0)
2953 && operand_equal_p (TREE_OPERAND (@0, 1), TREE_OPERAND (@2, 1), 0))
2956 (cnd @0 (cnd @1 @2 @3) @4)
2957 (if (COMPARISON_CLASS_P (@0)
2958 && COMPARISON_CLASS_P (@1)
2959 && invert_tree_comparison
2960 (TREE_CODE (@0), HONOR_NANS (TREE_OPERAND (@0, 0))) == TREE_CODE (@1)
2961 && operand_equal_p (TREE_OPERAND (@0, 0), TREE_OPERAND (@1, 0), 0)
2962 && operand_equal_p (TREE_OPERAND (@0, 1), TREE_OPERAND (@1, 1), 0))
2965 /* A ? B : B -> B. */
2970 /* !A ? B : C -> A ? C : B. */
2972 (cnd (logical_inverted_value truth_valued_p@0) @1 @2)
2975 /* A + (B vcmp C ? 1 : 0) -> A - (B vcmp C ? -1 : 0), since vector comparisons
2976 return all -1 or all 0 results. */
2977 /* ??? We could instead convert all instances of the vec_cond to negate,
2978 but that isn't necessarily a win on its own. */
2980 (plus:c @3 (view_convert? (vec_cond:s @0 integer_each_onep@1 integer_zerop@2)))
2981 (if (VECTOR_TYPE_P (type)
2982 && known_eq (TYPE_VECTOR_SUBPARTS (type),
2983 TYPE_VECTOR_SUBPARTS (TREE_TYPE (@1)))
2984 && (TYPE_MODE (TREE_TYPE (type))
2985 == TYPE_MODE (TREE_TYPE (TREE_TYPE (@1)))))
2986 (minus @3 (view_convert (vec_cond @0 (negate @1) @2)))))
2988 /* ... likewise A - (B vcmp C ? 1 : 0) -> A + (B vcmp C ? -1 : 0). */
2990 (minus @3 (view_convert? (vec_cond:s @0 integer_each_onep@1 integer_zerop@2)))
2991 (if (VECTOR_TYPE_P (type)
2992 && known_eq (TYPE_VECTOR_SUBPARTS (type),
2993 TYPE_VECTOR_SUBPARTS (TREE_TYPE (@1)))
2994 && (TYPE_MODE (TREE_TYPE (type))
2995 == TYPE_MODE (TREE_TYPE (TREE_TYPE (@1)))))
2996 (plus @3 (view_convert (vec_cond @0 (negate @1) @2)))))
2999 /* Simplifications of comparisons. */
3001 /* See if we can reduce the magnitude of a constant involved in a
3002 comparison by changing the comparison code. This is a canonicalization
3003 formerly done by maybe_canonicalize_comparison_1. */
3007 (cmp @0 INTEGER_CST@1)
3008 (if (tree_int_cst_sgn (@1) == -1)
3009 (acmp @0 { wide_int_to_tree (TREE_TYPE (@1), wi::to_wide (@1) + 1); }))))
3013 (cmp @0 INTEGER_CST@1)
3014 (if (tree_int_cst_sgn (@1) == 1)
3015 (acmp @0 { wide_int_to_tree (TREE_TYPE (@1), wi::to_wide (@1) - 1); }))))
3018 /* We can simplify a logical negation of a comparison to the
3019 inverted comparison. As we cannot compute an expression
3020 operator using invert_tree_comparison we have to simulate
3021 that with expression code iteration. */
3022 (for cmp (tcc_comparison)
3023 icmp (inverted_tcc_comparison)
3024 ncmp (inverted_tcc_comparison_with_nans)
3025 /* Ideally we'd like to combine the following two patterns
3026 and handle some more cases by using
3027 (logical_inverted_value (cmp @0 @1))
3028 here but for that genmatch would need to "inline" that.
3029 For now implement what forward_propagate_comparison did. */
3031 (bit_not (cmp @0 @1))
3032 (if (VECTOR_TYPE_P (type)
3033 || (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1))
3034 /* Comparison inversion may be impossible for trapping math,
3035 invert_tree_comparison will tell us. But we can't use
3036 a computed operator in the replacement tree thus we have
3037 to play the trick below. */
3038 (with { enum tree_code ic = invert_tree_comparison
3039 (cmp, HONOR_NANS (@0)); }
3045 (bit_xor (cmp @0 @1) integer_truep)
3046 (with { enum tree_code ic = invert_tree_comparison
3047 (cmp, HONOR_NANS (@0)); }
3053 /* Transform comparisons of the form X - Y CMP 0 to X CMP Y.
3054 ??? The transformation is valid for the other operators if overflow
3055 is undefined for the type, but performing it here badly interacts
3056 with the transformation in fold_cond_expr_with_comparison which
3057 attempts to synthetize ABS_EXPR. */
3059 (for sub (minus pointer_diff)
3061 (cmp (sub@2 @0 @1) integer_zerop)
3062 (if (single_use (@2))
3065 /* Transform comparisons of the form X * C1 CMP 0 to X CMP 0 in the
3066 signed arithmetic case. That form is created by the compiler
3067 often enough for folding it to be of value. One example is in
3068 computing loop trip counts after Operator Strength Reduction. */
3069 (for cmp (simple_comparison)
3070 scmp (swapped_simple_comparison)
3072 (cmp (mult@3 @0 INTEGER_CST@1) integer_zerop@2)
3073 /* Handle unfolded multiplication by zero. */
3074 (if (integer_zerop (@1))
3076 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3077 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
3079 /* If @1 is negative we swap the sense of the comparison. */
3080 (if (tree_int_cst_sgn (@1) < 0)
3084 /* Simplify comparison of something with itself. For IEEE
3085 floating-point, we can only do some of these simplifications. */
3089 (if (! FLOAT_TYPE_P (TREE_TYPE (@0))
3090 || ! HONOR_NANS (@0))
3091 { constant_boolean_node (true, type); }
3092 (if (cmp != EQ_EXPR)
3098 || ! FLOAT_TYPE_P (TREE_TYPE (@0))
3099 || ! HONOR_NANS (@0))
3100 { constant_boolean_node (false, type); })))
3101 (for cmp (unle unge uneq)
3104 { constant_boolean_node (true, type); }))
3105 (for cmp (unlt ungt)
3111 (if (!flag_trapping_math)
3112 { constant_boolean_node (false, type); }))
3114 /* Fold ~X op ~Y as Y op X. */
3115 (for cmp (simple_comparison)
3117 (cmp (bit_not@2 @0) (bit_not@3 @1))
3118 (if (single_use (@2) && single_use (@3))
3121 /* Fold ~X op C as X op' ~C, where op' is the swapped comparison. */
3122 (for cmp (simple_comparison)
3123 scmp (swapped_simple_comparison)
3125 (cmp (bit_not@2 @0) CONSTANT_CLASS_P@1)
3126 (if (single_use (@2)
3127 && (TREE_CODE (@1) == INTEGER_CST || TREE_CODE (@1) == VECTOR_CST))
3128 (scmp @0 (bit_not @1)))))
3130 (for cmp (simple_comparison)
3131 /* Fold (double)float1 CMP (double)float2 into float1 CMP float2. */
3133 (cmp (convert@2 @0) (convert? @1))
3134 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
3135 && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@2))
3136 == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0)))
3137 && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@2))
3138 == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1))))
3141 tree type1 = TREE_TYPE (@1);
3142 if (TREE_CODE (@1) == REAL_CST && !DECIMAL_FLOAT_TYPE_P (type1))
3144 REAL_VALUE_TYPE orig = TREE_REAL_CST (@1);
3145 if (TYPE_PRECISION (type1) > TYPE_PRECISION (float_type_node)
3146 && exact_real_truncate (TYPE_MODE (float_type_node), &orig))
3147 type1 = float_type_node;
3148 if (TYPE_PRECISION (type1) > TYPE_PRECISION (double_type_node)
3149 && exact_real_truncate (TYPE_MODE (double_type_node), &orig))
3150 type1 = double_type_node;
3153 = (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type1)
3154 ? TREE_TYPE (@0) : type1);
3156 (if (TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (newtype))
3157 (cmp (convert:newtype @0) (convert:newtype @1))))))
3161 /* IEEE doesn't distinguish +0 and -0 in comparisons. */
3163 /* a CMP (-0) -> a CMP 0 */
3164 (if (REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@1)))
3165 (cmp @0 { build_real (TREE_TYPE (@1), dconst0); }))
3166 /* x != NaN is always true, other ops are always false. */
3167 (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
3168 && ! HONOR_SNANS (@1))
3169 { constant_boolean_node (cmp == NE_EXPR, type); })
3170 /* Fold comparisons against infinity. */
3171 (if (REAL_VALUE_ISINF (TREE_REAL_CST (@1))
3172 && MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (@1))))
3175 REAL_VALUE_TYPE max;
3176 enum tree_code code = cmp;
3177 bool neg = REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1));
3179 code = swap_tree_comparison (code);
3182 /* x > +Inf is always false, if we ignore NaNs or exceptions. */
3183 (if (code == GT_EXPR
3184 && !(HONOR_NANS (@0) && flag_trapping_math))
3185 { constant_boolean_node (false, type); })
3186 (if (code == LE_EXPR)
3187 /* x <= +Inf is always true, if we don't care about NaNs. */
3188 (if (! HONOR_NANS (@0))
3189 { constant_boolean_node (true, type); }
3190 /* x <= +Inf is the same as x == x, i.e. !isnan(x), but this loses
3191 an "invalid" exception. */
3192 (if (!flag_trapping_math)
3194 /* x == +Inf and x >= +Inf are always equal to x > DBL_MAX, but
3195 for == this introduces an exception for x a NaN. */
3196 (if ((code == EQ_EXPR && !(HONOR_NANS (@0) && flag_trapping_math))
3198 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
3200 (lt @0 { build_real (TREE_TYPE (@0), max); })
3201 (gt @0 { build_real (TREE_TYPE (@0), max); }))))
3202 /* x < +Inf is always equal to x <= DBL_MAX. */
3203 (if (code == LT_EXPR)
3204 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
3206 (ge @0 { build_real (TREE_TYPE (@0), max); })
3207 (le @0 { build_real (TREE_TYPE (@0), max); }))))
3208 /* x != +Inf is always equal to !(x > DBL_MAX), but this introduces
3209 an exception for x a NaN so use an unordered comparison. */
3210 (if (code == NE_EXPR)
3211 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
3212 (if (! HONOR_NANS (@0))
3214 (ge @0 { build_real (TREE_TYPE (@0), max); })
3215 (le @0 { build_real (TREE_TYPE (@0), max); }))
3217 (unge @0 { build_real (TREE_TYPE (@0), max); })
3218 (unle @0 { build_real (TREE_TYPE (@0), max); }))))))))))
3220 /* If this is a comparison of a real constant with a PLUS_EXPR
3221 or a MINUS_EXPR of a real constant, we can convert it into a
3222 comparison with a revised real constant as long as no overflow
3223 occurs when unsafe_math_optimizations are enabled. */
3224 (if (flag_unsafe_math_optimizations)
3225 (for op (plus minus)
3227 (cmp (op @0 REAL_CST@1) REAL_CST@2)
3230 tree tem = const_binop (op == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR,
3231 TREE_TYPE (@1), @2, @1);
3233 (if (tem && !TREE_OVERFLOW (tem))
3234 (cmp @0 { tem; }))))))
3236 /* Likewise, we can simplify a comparison of a real constant with
3237 a MINUS_EXPR whose first operand is also a real constant, i.e.
3238 (c1 - x) < c2 becomes x > c1-c2. Reordering is allowed on
3239 floating-point types only if -fassociative-math is set. */
3240 (if (flag_associative_math)
3242 (cmp (minus REAL_CST@0 @1) REAL_CST@2)
3243 (with { tree tem = const_binop (MINUS_EXPR, TREE_TYPE (@1), @0, @2); }
3244 (if (tem && !TREE_OVERFLOW (tem))
3245 (cmp { tem; } @1)))))
3247 /* Fold comparisons against built-in math functions. */
3248 (if (flag_unsafe_math_optimizations
3249 && ! flag_errno_math)
3252 (cmp (sq @0) REAL_CST@1)
3254 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
3256 /* sqrt(x) < y is always false, if y is negative. */
3257 (if (cmp == EQ_EXPR || cmp == LT_EXPR || cmp == LE_EXPR)
3258 { constant_boolean_node (false, type); })
3259 /* sqrt(x) > y is always true, if y is negative and we
3260 don't care about NaNs, i.e. negative values of x. */
3261 (if (cmp == NE_EXPR || !HONOR_NANS (@0))
3262 { constant_boolean_node (true, type); })
3263 /* sqrt(x) > y is the same as x >= 0, if y is negative. */
3264 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })))
3265 (if (real_equal (TREE_REAL_CST_PTR (@1), &dconst0))
3267 /* sqrt(x) < 0 is always false. */
3268 (if (cmp == LT_EXPR)
3269 { constant_boolean_node (false, type); })
3270 /* sqrt(x) >= 0 is always true if we don't care about NaNs. */
3271 (if (cmp == GE_EXPR && !HONOR_NANS (@0))
3272 { constant_boolean_node (true, type); })
3273 /* sqrt(x) <= 0 -> x == 0. */
3274 (if (cmp == LE_EXPR)
3276 /* Otherwise sqrt(x) cmp 0 -> x cmp 0. Here cmp can be >=, >,
3277 == or !=. In the last case:
3279 (sqrt(x) != 0) == (NaN != 0) == true == (x != 0)
3281 if x is negative or NaN. Due to -funsafe-math-optimizations,
3282 the results for other x follow from natural arithmetic. */
3284 (if (cmp == GT_EXPR || cmp == GE_EXPR)
3288 real_arithmetic (&c2, MULT_EXPR,
3289 &TREE_REAL_CST (@1), &TREE_REAL_CST (@1));
3290 real_convert (&c2, TYPE_MODE (TREE_TYPE (@0)), &c2);
3292 (if (REAL_VALUE_ISINF (c2))
3293 /* sqrt(x) > y is x == +Inf, when y is very large. */
3294 (if (HONOR_INFINITIES (@0))
3295 (eq @0 { build_real (TREE_TYPE (@0), c2); })
3296 { constant_boolean_node (false, type); })
3297 /* sqrt(x) > c is the same as x > c*c. */
3298 (cmp @0 { build_real (TREE_TYPE (@0), c2); }))))
3299 (if (cmp == LT_EXPR || cmp == LE_EXPR)
3303 real_arithmetic (&c2, MULT_EXPR,
3304 &TREE_REAL_CST (@1), &TREE_REAL_CST (@1));
3305 real_convert (&c2, TYPE_MODE (TREE_TYPE (@0)), &c2);
3307 (if (REAL_VALUE_ISINF (c2))
3309 /* sqrt(x) < y is always true, when y is a very large
3310 value and we don't care about NaNs or Infinities. */
3311 (if (! HONOR_NANS (@0) && ! HONOR_INFINITIES (@0))
3312 { constant_boolean_node (true, type); })
3313 /* sqrt(x) < y is x != +Inf when y is very large and we
3314 don't care about NaNs. */
3315 (if (! HONOR_NANS (@0))
3316 (ne @0 { build_real (TREE_TYPE (@0), c2); }))
3317 /* sqrt(x) < y is x >= 0 when y is very large and we
3318 don't care about Infinities. */
3319 (if (! HONOR_INFINITIES (@0))
3320 (ge @0 { build_real (TREE_TYPE (@0), dconst0); }))
3321 /* sqrt(x) < y is x >= 0 && x != +Inf, when y is large. */
3324 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
3325 (ne @0 { build_real (TREE_TYPE (@0), c2); }))))
3326 /* sqrt(x) < c is the same as x < c*c, if we ignore NaNs. */
3327 (if (! HONOR_NANS (@0))
3328 (cmp @0 { build_real (TREE_TYPE (@0), c2); })
3329 /* sqrt(x) < c is the same as x >= 0 && x < c*c. */
3332 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
3333 (cmp @0 { build_real (TREE_TYPE (@0), c2); })))))))))
3334 /* Transform sqrt(x) cmp sqrt(y) -> x cmp y. */
3336 (cmp (sq @0) (sq @1))
3337 (if (! HONOR_NANS (@0))
3340 /* Optimize various special cases of (FTYPE) N CMP CST. */
3341 (for cmp (lt le eq ne ge gt)
3342 icmp (le le eq ne ge ge)
3344 (cmp (float @0) REAL_CST@1)
3345 (if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (@1))
3346 && ! DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1)))
3349 tree itype = TREE_TYPE (@0);
3350 signop isign = TYPE_SIGN (itype);
3351 format_helper fmt (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (@1))));
3352 const REAL_VALUE_TYPE *cst = TREE_REAL_CST_PTR (@1);
3353 /* Be careful to preserve any potential exceptions due to
3354 NaNs. qNaNs are ok in == or != context.
3355 TODO: relax under -fno-trapping-math or
3356 -fno-signaling-nans. */
3358 = real_isnan (cst) && (cst->signalling
3359 || (cmp != EQ_EXPR && cmp != NE_EXPR));
3360 /* INT?_MIN is power-of-two so it takes
3361 only one mantissa bit. */
3362 bool signed_p = isign == SIGNED;
3363 bool itype_fits_ftype_p
3364 = TYPE_PRECISION (itype) - signed_p <= significand_size (fmt);
3366 /* TODO: allow non-fitting itype and SNaNs when
3367 -fno-trapping-math. */
3368 (if (itype_fits_ftype_p && ! exception_p)
3371 REAL_VALUE_TYPE imin, imax;
3372 real_from_integer (&imin, fmt, wi::min_value (itype), isign);
3373 real_from_integer (&imax, fmt, wi::max_value (itype), isign);
3375 REAL_VALUE_TYPE icst;
3376 if (cmp == GT_EXPR || cmp == GE_EXPR)
3377 real_ceil (&icst, fmt, cst);
3378 else if (cmp == LT_EXPR || cmp == LE_EXPR)
3379 real_floor (&icst, fmt, cst);
3381 real_trunc (&icst, fmt, cst);
3383 bool cst_int_p = !real_isnan (cst) && real_identical (&icst, cst);
3385 bool overflow_p = false;
3387 = real_to_integer (&icst, &overflow_p, TYPE_PRECISION (itype));
3390 /* Optimize cases when CST is outside of ITYPE's range. */
3391 (if (real_compare (LT_EXPR, cst, &imin))
3392 { constant_boolean_node (cmp == GT_EXPR || cmp == GE_EXPR || cmp == NE_EXPR,
3394 (if (real_compare (GT_EXPR, cst, &imax))
3395 { constant_boolean_node (cmp == LT_EXPR || cmp == LE_EXPR || cmp == NE_EXPR,
3397 /* Remove cast if CST is an integer representable by ITYPE. */
3399 (cmp @0 { gcc_assert (!overflow_p);
3400 wide_int_to_tree (itype, icst_val); })
3402 /* When CST is fractional, optimize
3403 (FTYPE) N == CST -> 0
3404 (FTYPE) N != CST -> 1. */
3405 (if (cmp == EQ_EXPR || cmp == NE_EXPR)
3406 { constant_boolean_node (cmp == NE_EXPR, type); })
3407 /* Otherwise replace with sensible integer constant. */
3410 gcc_checking_assert (!overflow_p);
3412 (icmp @0 { wide_int_to_tree (itype, icst_val); })))))))))
3414 /* Fold A /[ex] B CMP C to A CMP B * C. */
3417 (cmp (exact_div @0 @1) INTEGER_CST@2)
3418 (if (!integer_zerop (@1))
3419 (if (wi::to_wide (@2) == 0)
3421 (if (TREE_CODE (@1) == INTEGER_CST)
3424 wi::overflow_type ovf;
3425 wide_int prod = wi::mul (wi::to_wide (@2), wi::to_wide (@1),
3426 TYPE_SIGN (TREE_TYPE (@1)), &ovf);
3429 { constant_boolean_node (cmp == NE_EXPR, type); }
3430 (cmp @0 { wide_int_to_tree (TREE_TYPE (@0), prod); }))))))))
3431 (for cmp (lt le gt ge)
3433 (cmp (exact_div @0 INTEGER_CST@1) INTEGER_CST@2)
3434 (if (wi::gt_p (wi::to_wide (@1), 0, TYPE_SIGN (TREE_TYPE (@1))))
3437 wi::overflow_type ovf;
3438 wide_int prod = wi::mul (wi::to_wide (@2), wi::to_wide (@1),
3439 TYPE_SIGN (TREE_TYPE (@1)), &ovf);
3442 { constant_boolean_node (wi::lt_p (wi::to_wide (@2), 0,
3443 TYPE_SIGN (TREE_TYPE (@2)))
3444 != (cmp == LT_EXPR || cmp == LE_EXPR), type); }
3445 (cmp @0 { wide_int_to_tree (TREE_TYPE (@0), prod); }))))))
3447 /* Unordered tests if either argument is a NaN. */
3449 (bit_ior (unordered @0 @0) (unordered @1 @1))
3450 (if (types_match (@0, @1))
3453 (bit_and (ordered @0 @0) (ordered @1 @1))
3454 (if (types_match (@0, @1))
3457 (bit_ior:c (unordered @0 @0) (unordered:c@2 @0 @1))
3460 (bit_and:c (ordered @0 @0) (ordered:c@2 @0 @1))
3463 /* Simple range test simplifications. */
3464 /* A < B || A >= B -> true. */
3465 (for test1 (lt le le le ne ge)
3466 test2 (ge gt ge ne eq ne)
3468 (bit_ior:c (test1 @0 @1) (test2 @0 @1))
3469 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
3470 || VECTOR_INTEGER_TYPE_P (TREE_TYPE (@0)))
3471 { constant_boolean_node (true, type); })))
3472 /* A < B && A >= B -> false. */
3473 (for test1 (lt lt lt le ne eq)
3474 test2 (ge gt eq gt eq gt)
3476 (bit_and:c (test1 @0 @1) (test2 @0 @1))
3477 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
3478 || VECTOR_INTEGER_TYPE_P (TREE_TYPE (@0)))
3479 { constant_boolean_node (false, type); })))
3481 /* A & (2**N - 1) <= 2**K - 1 -> A & (2**N - 2**K) == 0
3482 A & (2**N - 1) > 2**K - 1 -> A & (2**N - 2**K) != 0
3484 Note that comparisons
3485 A & (2**N - 1) < 2**K -> A & (2**N - 2**K) == 0
3486 A & (2**N - 1) >= 2**K -> A & (2**N - 2**K) != 0
3487 will be canonicalized to above so there's no need to
3494 (cmp (bit_and@0 @1 INTEGER_CST@2) INTEGER_CST@3)
3495 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)))
3498 tree ty = TREE_TYPE (@0);
3499 unsigned prec = TYPE_PRECISION (ty);
3500 wide_int mask = wi::to_wide (@2, prec);
3501 wide_int rhs = wi::to_wide (@3, prec);
3502 signop sgn = TYPE_SIGN (ty);
3504 (if ((mask & (mask + 1)) == 0 && wi::gt_p (rhs, 0, sgn)
3505 && (rhs & (rhs + 1)) == 0 && wi::ge_p (mask, rhs, sgn))
3506 (eqcmp (bit_and @1 { wide_int_to_tree (ty, mask - rhs); })
3507 { build_zero_cst (ty); }))))))
3509 /* -A CMP -B -> B CMP A. */
3510 (for cmp (tcc_comparison)
3511 scmp (swapped_tcc_comparison)
3513 (cmp (negate @0) (negate @1))
3514 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
3515 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3516 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
3519 (cmp (negate @0) CONSTANT_CLASS_P@1)
3520 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
3521 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3522 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
3523 (with { tree tem = const_unop (NEGATE_EXPR, TREE_TYPE (@0), @1); }
3524 (if (tem && !TREE_OVERFLOW (tem))
3525 (scmp @0 { tem; }))))))
3527 /* Convert ABS_EXPR<x> == 0 or ABS_EXPR<x> != 0 to x == 0 or x != 0. */
3530 (op (abs @0) zerop@1)
3533 /* From fold_sign_changed_comparison and fold_widened_comparison.
3534 FIXME: the lack of symmetry is disturbing. */
3535 (for cmp (simple_comparison)
3537 (cmp (convert@0 @00) (convert?@1 @10))
3538 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
3539 /* Disable this optimization if we're casting a function pointer
3540 type on targets that require function pointer canonicalization. */
3541 && !(targetm.have_canonicalize_funcptr_for_compare ()
3542 && TREE_CODE (TREE_TYPE (@00)) == POINTER_TYPE
3543 && TREE_CODE (TREE_TYPE (TREE_TYPE (@00))) == FUNCTION_TYPE)
3545 (if (TYPE_PRECISION (TREE_TYPE (@00)) == TYPE_PRECISION (TREE_TYPE (@0))
3546 && (TREE_CODE (@10) == INTEGER_CST
3548 && (TYPE_UNSIGNED (TREE_TYPE (@00)) == TYPE_UNSIGNED (TREE_TYPE (@0))
3551 && !POINTER_TYPE_P (TREE_TYPE (@00)))
3552 /* ??? The special-casing of INTEGER_CST conversion was in the original
3553 code and here to avoid a spurious overflow flag on the resulting
3554 constant which fold_convert produces. */
3555 (if (TREE_CODE (@1) == INTEGER_CST)
3556 (cmp @00 { force_fit_type (TREE_TYPE (@00), wi::to_widest (@1), 0,
3557 TREE_OVERFLOW (@1)); })
3558 (cmp @00 (convert @1)))
3560 (if (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (TREE_TYPE (@00)))
3561 /* If possible, express the comparison in the shorter mode. */
3562 (if ((cmp == EQ_EXPR || cmp == NE_EXPR
3563 || TYPE_UNSIGNED (TREE_TYPE (@0)) == TYPE_UNSIGNED (TREE_TYPE (@00))
3564 || (!TYPE_UNSIGNED (TREE_TYPE (@0))
3565 && TYPE_UNSIGNED (TREE_TYPE (@00))))
3566 && (types_match (TREE_TYPE (@10), TREE_TYPE (@00))
3567 || ((TYPE_PRECISION (TREE_TYPE (@00))
3568 >= TYPE_PRECISION (TREE_TYPE (@10)))
3569 && (TYPE_UNSIGNED (TREE_TYPE (@00))
3570 == TYPE_UNSIGNED (TREE_TYPE (@10))))
3571 || (TREE_CODE (@10) == INTEGER_CST
3572 && INTEGRAL_TYPE_P (TREE_TYPE (@00))
3573 && int_fits_type_p (@10, TREE_TYPE (@00)))))
3574 (cmp @00 (convert @10))
3575 (if (TREE_CODE (@10) == INTEGER_CST
3576 && INTEGRAL_TYPE_P (TREE_TYPE (@00))
3577 && !int_fits_type_p (@10, TREE_TYPE (@00)))
3580 tree min = lower_bound_in_type (TREE_TYPE (@10), TREE_TYPE (@00));
3581 tree max = upper_bound_in_type (TREE_TYPE (@10), TREE_TYPE (@00));
3582 bool above = integer_nonzerop (const_binop (LT_EXPR, type, max, @10));
3583 bool below = integer_nonzerop (const_binop (LT_EXPR, type, @10, min));
3585 (if (above || below)
3586 (if (cmp == EQ_EXPR || cmp == NE_EXPR)
3587 { constant_boolean_node (cmp == EQ_EXPR ? false : true, type); }
3588 (if (cmp == LT_EXPR || cmp == LE_EXPR)
3589 { constant_boolean_node (above ? true : false, type); }
3590 (if (cmp == GT_EXPR || cmp == GE_EXPR)
3591 { constant_boolean_node (above ? false : true, type); }))))))))))))
3594 /* A local variable can never be pointed to by
3595 the default SSA name of an incoming parameter.
3596 SSA names are canonicalized to 2nd place. */
3598 (cmp addr@0 SSA_NAME@1)
3599 (if (SSA_NAME_IS_DEFAULT_DEF (@1)
3600 && TREE_CODE (SSA_NAME_VAR (@1)) == PARM_DECL)
3601 (with { tree base = get_base_address (TREE_OPERAND (@0, 0)); }
3602 (if (TREE_CODE (base) == VAR_DECL
3603 && auto_var_in_fn_p (base, current_function_decl))
3604 (if (cmp == NE_EXPR)
3605 { constant_boolean_node (true, type); }
3606 { constant_boolean_node (false, type); }))))))
3608 /* Equality compare simplifications from fold_binary */
3611 /* If we have (A | C) == D where C & ~D != 0, convert this into 0.
3612 Similarly for NE_EXPR. */
3614 (cmp (convert?@3 (bit_ior @0 INTEGER_CST@1)) INTEGER_CST@2)
3615 (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0))
3616 && wi::bit_and_not (wi::to_wide (@1), wi::to_wide (@2)) != 0)
3617 { constant_boolean_node (cmp == NE_EXPR, type); }))
3619 /* (X ^ Y) == 0 becomes X == Y, and (X ^ Y) != 0 becomes X != Y. */
3621 (cmp (bit_xor @0 @1) integer_zerop)
3624 /* (X ^ Y) == Y becomes X == 0.
3625 Likewise (X ^ Y) == X becomes Y == 0. */
3627 (cmp:c (bit_xor:c @0 @1) @0)
3628 (cmp @1 { build_zero_cst (TREE_TYPE (@1)); }))
3630 /* (X ^ C1) op C2 can be rewritten as X op (C1 ^ C2). */
3632 (cmp (convert?@3 (bit_xor @0 INTEGER_CST@1)) INTEGER_CST@2)
3633 (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0)))
3634 (cmp @0 (bit_xor @1 (convert @2)))))
3637 (cmp (convert? addr@0) integer_zerop)
3638 (if (tree_single_nonzero_warnv_p (@0, NULL))
3639 { constant_boolean_node (cmp == NE_EXPR, type); })))
3641 /* If we have (A & C) == C where C is a power of 2, convert this into
3642 (A & C) != 0. Similarly for NE_EXPR. */
3646 (cmp (bit_and@2 @0 integer_pow2p@1) @1)
3647 (icmp @2 { build_zero_cst (TREE_TYPE (@0)); })))
3649 /* If we have (A & C) != 0 ? D : 0 where C and D are powers of 2,
3650 convert this into a shift followed by ANDing with D. */
3653 (ne (bit_and @0 integer_pow2p@1) integer_zerop)
3654 INTEGER_CST@2 integer_zerop)
3655 (if (integer_pow2p (@2))
3657 int shift = (wi::exact_log2 (wi::to_wide (@2))
3658 - wi::exact_log2 (wi::to_wide (@1)));
3662 (lshift (convert @0) { build_int_cst (integer_type_node, shift); }) @2)
3664 (convert (rshift @0 { build_int_cst (integer_type_node, -shift); }))
3667 /* If we have (A & C) != 0 where C is the sign bit of A, convert
3668 this into A < 0. Similarly for (A & C) == 0 into A >= 0. */
3672 (cmp (bit_and (convert?@2 @0) integer_pow2p@1) integer_zerop)
3673 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
3674 && type_has_mode_precision_p (TREE_TYPE (@0))
3675 && element_precision (@2) >= element_precision (@0)
3676 && wi::only_sign_bit_p (wi::to_wide (@1), element_precision (@0)))
3677 (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
3678 (ncmp (convert:stype @0) { build_zero_cst (stype); })))))
3680 /* If we have A < 0 ? C : 0 where C is a power of 2, convert
3681 this into a right shift or sign extension followed by ANDing with C. */
3684 (lt @0 integer_zerop)
3685 INTEGER_CST@1 integer_zerop)
3686 (if (integer_pow2p (@1)
3687 && !TYPE_UNSIGNED (TREE_TYPE (@0)))
3689 int shift = element_precision (@0) - wi::exact_log2 (wi::to_wide (@1)) - 1;
3693 (convert (rshift @0 { build_int_cst (integer_type_node, shift); }))
3695 /* Otherwise ctype must be wider than TREE_TYPE (@0) and pure
3696 sign extension followed by AND with C will achieve the effect. */
3697 (bit_and (convert @0) @1)))))
3699 /* When the addresses are not directly of decls compare base and offset.
3700 This implements some remaining parts of fold_comparison address
3701 comparisons but still no complete part of it. Still it is good
3702 enough to make fold_stmt not regress when not dispatching to fold_binary. */
3703 (for cmp (simple_comparison)
3705 (cmp (convert1?@2 addr@0) (convert2? addr@1))
3708 poly_int64 off0, off1;
3709 tree base0 = get_addr_base_and_unit_offset (TREE_OPERAND (@0, 0), &off0);
3710 tree base1 = get_addr_base_and_unit_offset (TREE_OPERAND (@1, 0), &off1);
3711 if (base0 && TREE_CODE (base0) == MEM_REF)
3713 off0 += mem_ref_offset (base0).force_shwi ();
3714 base0 = TREE_OPERAND (base0, 0);
3716 if (base1 && TREE_CODE (base1) == MEM_REF)
3718 off1 += mem_ref_offset (base1).force_shwi ();
3719 base1 = TREE_OPERAND (base1, 0);
3722 (if (base0 && base1)
3726 /* Punt in GENERIC on variables with value expressions;
3727 the value expressions might point to fields/elements
3728 of other vars etc. */
3730 && ((VAR_P (base0) && DECL_HAS_VALUE_EXPR_P (base0))
3731 || (VAR_P (base1) && DECL_HAS_VALUE_EXPR_P (base1))))
3733 else if (decl_in_symtab_p (base0)
3734 && decl_in_symtab_p (base1))
3735 equal = symtab_node::get_create (base0)
3736 ->equal_address_to (symtab_node::get_create (base1));
3737 else if ((DECL_P (base0)
3738 || TREE_CODE (base0) == SSA_NAME
3739 || TREE_CODE (base0) == STRING_CST)
3741 || TREE_CODE (base1) == SSA_NAME
3742 || TREE_CODE (base1) == STRING_CST))
3743 equal = (base0 == base1);
3746 && (cmp == EQ_EXPR || cmp == NE_EXPR
3747 /* If the offsets are equal we can ignore overflow. */
3748 || known_eq (off0, off1)
3749 || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
3750 /* Or if we compare using pointers to decls or strings. */
3751 || (POINTER_TYPE_P (TREE_TYPE (@2))
3752 && (DECL_P (base0) || TREE_CODE (base0) == STRING_CST))))
3754 (if (cmp == EQ_EXPR && (known_eq (off0, off1) || known_ne (off0, off1)))
3755 { constant_boolean_node (known_eq (off0, off1), type); })
3756 (if (cmp == NE_EXPR && (known_eq (off0, off1) || known_ne (off0, off1)))
3757 { constant_boolean_node (known_ne (off0, off1), type); })
3758 (if (cmp == LT_EXPR && (known_lt (off0, off1) || known_ge (off0, off1)))
3759 { constant_boolean_node (known_lt (off0, off1), type); })
3760 (if (cmp == LE_EXPR && (known_le (off0, off1) || known_gt (off0, off1)))
3761 { constant_boolean_node (known_le (off0, off1), type); })
3762 (if (cmp == GE_EXPR && (known_ge (off0, off1) || known_lt (off0, off1)))
3763 { constant_boolean_node (known_ge (off0, off1), type); })
3764 (if (cmp == GT_EXPR && (known_gt (off0, off1) || known_le (off0, off1)))
3765 { constant_boolean_node (known_gt (off0, off1), type); }))
3767 && DECL_P (base0) && DECL_P (base1)
3768 /* If we compare this as integers require equal offset. */
3769 && (!INTEGRAL_TYPE_P (TREE_TYPE (@2))
3770 || known_eq (off0, off1)))
3772 (if (cmp == EQ_EXPR)
3773 { constant_boolean_node (false, type); })
3774 (if (cmp == NE_EXPR)
3775 { constant_boolean_node (true, type); })))))))))
3777 /* Simplify pointer equality compares using PTA. */
3781 (if (POINTER_TYPE_P (TREE_TYPE (@0))
3782 && ptrs_compare_unequal (@0, @1))
3783 { constant_boolean_node (neeq != EQ_EXPR, type); })))
3785 /* PR70920: Transform (intptr_t)x eq/ne CST to x eq/ne (typeof x) CST.
3786 and (typeof ptr_cst) x eq/ne ptr_cst to x eq/ne (typeof x) CST.
3787 Disable the transform if either operand is pointer to function.
3788 This broke pr22051-2.c for arm where function pointer
3789 canonicalizaion is not wanted. */
3793 (cmp (convert @0) INTEGER_CST@1)
3794 (if (((POINTER_TYPE_P (TREE_TYPE (@0))
3795 && !FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@0)))
3796 && INTEGRAL_TYPE_P (TREE_TYPE (@1)))
3797 || (INTEGRAL_TYPE_P (TREE_TYPE (@0))
3798 && POINTER_TYPE_P (TREE_TYPE (@1))
3799 && !FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@1)))))
3800 && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
3801 (cmp @0 (convert @1)))))
3803 /* Non-equality compare simplifications from fold_binary */
3804 (for cmp (lt gt le ge)
3805 /* Comparisons with the highest or lowest possible integer of
3806 the specified precision will have known values. */
3808 (cmp (convert?@2 @0) INTEGER_CST@1)
3809 (if ((INTEGRAL_TYPE_P (TREE_TYPE (@1)) || POINTER_TYPE_P (TREE_TYPE (@1)))
3810 && tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@0)))
3813 tree arg1_type = TREE_TYPE (@1);
3814 unsigned int prec = TYPE_PRECISION (arg1_type);
3815 wide_int max = wi::max_value (arg1_type);
3816 wide_int signed_max = wi::max_value (prec, SIGNED);
3817 wide_int min = wi::min_value (arg1_type);
3820 (if (wi::to_wide (@1) == max)
3822 (if (cmp == GT_EXPR)
3823 { constant_boolean_node (false, type); })
3824 (if (cmp == GE_EXPR)
3826 (if (cmp == LE_EXPR)
3827 { constant_boolean_node (true, type); })
3828 (if (cmp == LT_EXPR)
3830 (if (wi::to_wide (@1) == min)
3832 (if (cmp == LT_EXPR)
3833 { constant_boolean_node (false, type); })
3834 (if (cmp == LE_EXPR)
3836 (if (cmp == GE_EXPR)
3837 { constant_boolean_node (true, type); })
3838 (if (cmp == GT_EXPR)
3840 (if (wi::to_wide (@1) == max - 1)
3842 (if (cmp == GT_EXPR)
3843 (eq @2 { wide_int_to_tree (TREE_TYPE (@1), wi::to_wide (@1) + 1); }))
3844 (if (cmp == LE_EXPR)
3845 (ne @2 { wide_int_to_tree (TREE_TYPE (@1), wi::to_wide (@1) + 1); }))))
3846 (if (wi::to_wide (@1) == min + 1)
3848 (if (cmp == GE_EXPR)
3849 (ne @2 { wide_int_to_tree (TREE_TYPE (@1), wi::to_wide (@1) - 1); }))
3850 (if (cmp == LT_EXPR)
3851 (eq @2 { wide_int_to_tree (TREE_TYPE (@1), wi::to_wide (@1) - 1); }))))
3852 (if (wi::to_wide (@1) == signed_max
3853 && TYPE_UNSIGNED (arg1_type)
3854 /* We will flip the signedness of the comparison operator
3855 associated with the mode of @1, so the sign bit is
3856 specified by this mode. Check that @1 is the signed
3857 max associated with this sign bit. */
3858 && prec == GET_MODE_PRECISION (SCALAR_INT_TYPE_MODE (arg1_type))
3859 /* signed_type does not work on pointer types. */
3860 && INTEGRAL_TYPE_P (arg1_type))
3861 /* The following case also applies to X < signed_max+1
3862 and X >= signed_max+1 because previous transformations. */
3863 (if (cmp == LE_EXPR || cmp == GT_EXPR)
3864 (with { tree st = signed_type_for (arg1_type); }
3865 (if (cmp == LE_EXPR)
3866 (ge (convert:st @0) { build_zero_cst (st); })
3867 (lt (convert:st @0) { build_zero_cst (st); }))))))))))
3869 (for cmp (unordered ordered unlt unle ungt unge uneq ltgt)
3870 /* If the second operand is NaN, the result is constant. */
3873 (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
3874 && (cmp != LTGT_EXPR || ! flag_trapping_math))
3875 { constant_boolean_node (cmp == ORDERED_EXPR || cmp == LTGT_EXPR
3876 ? false : true, type); })))
3878 /* bool_var != 0 becomes bool_var. */
3880 (ne @0 integer_zerop)
3881 (if (TREE_CODE (TREE_TYPE (@0)) == BOOLEAN_TYPE
3882 && types_match (type, TREE_TYPE (@0)))
3884 /* bool_var == 1 becomes bool_var. */
3886 (eq @0 integer_onep)
3887 (if (TREE_CODE (TREE_TYPE (@0)) == BOOLEAN_TYPE
3888 && types_match (type, TREE_TYPE (@0)))
3891 bool_var == 0 becomes !bool_var or
3892 bool_var != 1 becomes !bool_var
3893 here because that only is good in assignment context as long
3894 as we require a tcc_comparison in GIMPLE_CONDs where we'd
3895 replace if (x == 0) with tem = ~x; if (tem != 0) which is
3896 clearly less optimal and which we'll transform again in forwprop. */
3898 /* When one argument is a constant, overflow detection can be simplified.
3899 Currently restricted to single use so as not to interfere too much with
3900 ADD_OVERFLOW detection in tree-ssa-math-opts.c.
3901 A + CST CMP A -> A CMP' CST' */
3902 (for cmp (lt le ge gt)
3905 (cmp:c (plus@2 @0 INTEGER_CST@1) @0)
3906 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
3907 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))
3908 && wi::to_wide (@1) != 0
3910 (with { unsigned int prec = TYPE_PRECISION (TREE_TYPE (@0)); }
3911 (out @0 { wide_int_to_tree (TREE_TYPE (@0),
3912 wi::max_value (prec, UNSIGNED)
3913 - wi::to_wide (@1)); })))))
3915 /* To detect overflow in unsigned A - B, A < B is simpler than A - B > A.
3916 However, the detection logic for SUB_OVERFLOW in tree-ssa-math-opts.c
3917 expects the long form, so we restrict the transformation for now. */
3920 (cmp:c (minus@2 @0 @1) @0)
3921 (if (single_use (@2)
3922 && ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3923 && TYPE_UNSIGNED (TREE_TYPE (@0))
3924 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
3927 /* Testing for overflow is unnecessary if we already know the result. */
3932 (cmp:c (realpart (IFN_SUB_OVERFLOW@2 @0 @1)) @0)
3933 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
3934 && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
3935 (out (imagpart @2) { build_zero_cst (TREE_TYPE (@0)); }))))
3940 (cmp:c (realpart (IFN_ADD_OVERFLOW:c@2 @0 @1)) @0)
3941 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
3942 && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
3943 (out (imagpart @2) { build_zero_cst (TREE_TYPE (@0)); }))))
3945 /* For unsigned operands, -1 / B < A checks whether A * B would overflow.
3946 Simplify it to __builtin_mul_overflow (A, B, <unused>). */
3950 (cmp:c (trunc_div:s integer_all_onesp @1) @0)
3951 (if (TYPE_UNSIGNED (TREE_TYPE (@0)) && !VECTOR_TYPE_P (TREE_TYPE (@0)))
3952 (with { tree t = TREE_TYPE (@0), cpx = build_complex_type (t); }
3953 (out (imagpart (IFN_MUL_OVERFLOW:cpx @0 @1)) { build_zero_cst (t); })))))
3955 /* Simplification of math builtins. These rules must all be optimizations
3956 as well as IL simplifications. If there is a possibility that the new
3957 form could be a pessimization, the rule should go in the canonicalization
3958 section that follows this one.
3960 Rules can generally go in this section if they satisfy one of
3963 - the rule describes an identity
3965 - the rule replaces calls with something as simple as addition or
3968 - the rule contains unary calls only and simplifies the surrounding
3969 arithmetic. (The idea here is to exclude non-unary calls in which
3970 one operand is constant and in which the call is known to be cheap
3971 when the operand has that value.) */
3973 (if (flag_unsafe_math_optimizations)
3974 /* Simplify sqrt(x) * sqrt(x) -> x. */
3976 (mult (SQRT_ALL@1 @0) @1)
3977 (if (!HONOR_SNANS (type))
3980 (for op (plus minus)
3981 /* Simplify (A / C) +- (B / C) -> (A +- B) / C. */
3985 (rdiv (op @0 @2) @1)))
3987 /* Simplify sqrt(x) * sqrt(y) -> sqrt(x*y). */
3988 (for root (SQRT CBRT)
3990 (mult (root:s @0) (root:s @1))
3991 (root (mult @0 @1))))
3993 /* Simplify expN(x) * expN(y) -> expN(x+y). */
3994 (for exps (EXP EXP2 EXP10 POW10)
3996 (mult (exps:s @0) (exps:s @1))
3997 (exps (plus @0 @1))))
3999 /* Simplify a/root(b/c) into a*root(c/b). */
4000 (for root (SQRT CBRT)
4002 (rdiv @0 (root:s (rdiv:s @1 @2)))
4003 (mult @0 (root (rdiv @2 @1)))))
4005 /* Simplify x/expN(y) into x*expN(-y). */
4006 (for exps (EXP EXP2 EXP10 POW10)
4008 (rdiv @0 (exps:s @1))
4009 (mult @0 (exps (negate @1)))))
4011 (for logs (LOG LOG2 LOG10 LOG10)
4012 exps (EXP EXP2 EXP10 POW10)
4013 /* logN(expN(x)) -> x. */
4017 /* expN(logN(x)) -> x. */
4022 /* Optimize logN(func()) for various exponential functions. We
4023 want to determine the value "x" and the power "exponent" in
4024 order to transform logN(x**exponent) into exponent*logN(x). */
4025 (for logs (LOG LOG LOG LOG2 LOG2 LOG2 LOG10 LOG10)
4026 exps (EXP2 EXP10 POW10 EXP EXP10 POW10 EXP EXP2)
4029 (if (SCALAR_FLOAT_TYPE_P (type))
4035 /* Prepare to do logN(exp(exponent)) -> exponent*logN(e). */
4036 x = build_real_truncate (type, dconst_e ());
4039 /* Prepare to do logN(exp2(exponent)) -> exponent*logN(2). */
4040 x = build_real (type, dconst2);
4044 /* Prepare to do logN(exp10(exponent)) -> exponent*logN(10). */
4046 REAL_VALUE_TYPE dconst10;
4047 real_from_integer (&dconst10, VOIDmode, 10, SIGNED);
4048 x = build_real (type, dconst10);
4055 (mult (logs { x; }) @0)))))
4063 (if (SCALAR_FLOAT_TYPE_P (type))
4069 /* Prepare to do logN(sqrt(x)) -> 0.5*logN(x). */
4070 x = build_real (type, dconsthalf);
4073 /* Prepare to do logN(cbrt(x)) -> (1/3)*logN(x). */
4074 x = build_real_truncate (type, dconst_third ());
4080 (mult { x; } (logs @0))))))
4082 /* logN(pow(x,exponent)) -> exponent*logN(x). */
4083 (for logs (LOG LOG2 LOG10)
4087 (mult @1 (logs @0))))
4089 /* pow(C,x) -> exp(log(C)*x) if C > 0,
4090 or if C is a positive power of 2,
4091 pow(C,x) -> exp2(log2(C)*x). */
4099 (pows REAL_CST@0 @1)
4100 (if (real_compare (GT_EXPR, TREE_REAL_CST_PTR (@0), &dconst0)
4101 && real_isfinite (TREE_REAL_CST_PTR (@0))
4102 /* As libmvec doesn't have a vectorized exp2, defer optimizing
4103 the use_exp2 case until after vectorization. It seems actually
4104 beneficial for all constants to postpone this until later,
4105 because exp(log(C)*x), while faster, will have worse precision
4106 and if x folds into a constant too, that is unnecessary
4108 && canonicalize_math_after_vectorization_p ())
4110 const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (@0);
4111 bool use_exp2 = false;
4112 if (targetm.libc_has_function (function_c99_misc)
4113 && value->cl == rvc_normal)
4115 REAL_VALUE_TYPE frac_rvt = *value;
4116 SET_REAL_EXP (&frac_rvt, 1);
4117 if (real_equal (&frac_rvt, &dconst1))
4122 (if (optimize_pow_to_exp (@0, @1))
4123 (exps (mult (logs @0) @1)))
4124 (exp2s (mult (log2s @0) @1)))))))
4127 /* pow(C,x)*expN(y) -> expN(logN(C)*x+y) if C > 0. */
4129 exps (EXP EXP2 EXP10 POW10)
4130 logs (LOG LOG2 LOG10 LOG10)
4132 (mult:c (pows:s REAL_CST@0 @1) (exps:s @2))
4133 (if (real_compare (GT_EXPR, TREE_REAL_CST_PTR (@0), &dconst0)
4134 && real_isfinite (TREE_REAL_CST_PTR (@0)))
4135 (exps (plus (mult (logs @0) @1) @2)))))
4140 exps (EXP EXP2 EXP10 POW10)
4141 /* sqrt(expN(x)) -> expN(x*0.5). */
4144 (exps (mult @0 { build_real (type, dconsthalf); })))
4145 /* cbrt(expN(x)) -> expN(x/3). */
4148 (exps (mult @0 { build_real_truncate (type, dconst_third ()); })))
4149 /* pow(expN(x), y) -> expN(x*y). */
4152 (exps (mult @0 @1))))
4154 /* tan(atan(x)) -> x. */
4161 /* cabs(x+0i) or cabs(0+xi) -> abs(x). */
4163 (CABS (complex:C @0 real_zerop@1))
4166 /* trunc(trunc(x)) -> trunc(x), etc. */
4167 (for fns (TRUNC_ALL FLOOR_ALL CEIL_ALL ROUND_ALL NEARBYINT_ALL RINT_ALL)
4171 /* f(x) -> x if x is integer valued and f does nothing for such values. */
4172 (for fns (TRUNC_ALL FLOOR_ALL CEIL_ALL ROUND_ALL NEARBYINT_ALL RINT_ALL)
4174 (fns integer_valued_real_p@0)
4177 /* hypot(x,0) and hypot(0,x) -> abs(x). */
4179 (HYPOT:c @0 real_zerop@1)
4182 /* pow(1,x) -> 1. */
4184 (POW real_onep@0 @1)
4188 /* copysign(x,x) -> x. */
4189 (COPYSIGN_ALL @0 @0)
4193 /* copysign(x,y) -> fabs(x) if y is nonnegative. */
4194 (COPYSIGN_ALL @0 tree_expr_nonnegative_p@1)
4197 (for scale (LDEXP SCALBN SCALBLN)
4198 /* ldexp(0, x) -> 0. */
4200 (scale real_zerop@0 @1)
4202 /* ldexp(x, 0) -> x. */
4204 (scale @0 integer_zerop@1)
4206 /* ldexp(x, y) -> x if x is +-Inf or NaN. */
4208 (scale REAL_CST@0 @1)
4209 (if (!real_isfinite (TREE_REAL_CST_PTR (@0)))
4212 /* Canonicalization of sequences of math builtins. These rules represent
4213 IL simplifications but are not necessarily optimizations.
4215 The sincos pass is responsible for picking "optimal" implementations
4216 of math builtins, which may be more complicated and can sometimes go
4217 the other way, e.g. converting pow into a sequence of sqrts.
4218 We only want to do these canonicalizations before the pass has run. */
4220 (if (flag_unsafe_math_optimizations && canonicalize_math_p ())
4221 /* Simplify tan(x) * cos(x) -> sin(x). */
4223 (mult:c (TAN:s @0) (COS:s @0))
4226 /* Simplify x * pow(x,c) -> pow(x,c+1). */
4228 (mult:c @0 (POW:s @0 REAL_CST@1))
4229 (if (!TREE_OVERFLOW (@1))
4230 (POW @0 (plus @1 { build_one_cst (type); }))))
4232 /* Simplify sin(x) / cos(x) -> tan(x). */
4234 (rdiv (SIN:s @0) (COS:s @0))
4237 /* Simplify cos(x) / sin(x) -> 1 / tan(x). */
4239 (rdiv (COS:s @0) (SIN:s @0))
4240 (rdiv { build_one_cst (type); } (TAN @0)))
4242 /* Simplify sin(x) / tan(x) -> cos(x). */
4244 (rdiv (SIN:s @0) (TAN:s @0))
4245 (if (! HONOR_NANS (@0)
4246 && ! HONOR_INFINITIES (@0))
4249 /* Simplify tan(x) / sin(x) -> 1.0 / cos(x). */
4251 (rdiv (TAN:s @0) (SIN:s @0))
4252 (if (! HONOR_NANS (@0)
4253 && ! HONOR_INFINITIES (@0))
4254 (rdiv { build_one_cst (type); } (COS @0))))
4256 /* Simplify pow(x,y) * pow(x,z) -> pow(x,y+z). */
4258 (mult (POW:s @0 @1) (POW:s @0 @2))
4259 (POW @0 (plus @1 @2)))
4261 /* Simplify pow(x,y) * pow(z,y) -> pow(x*z,y). */
4263 (mult (POW:s @0 @1) (POW:s @2 @1))
4264 (POW (mult @0 @2) @1))
4266 /* Simplify powi(x,y) * powi(z,y) -> powi(x*z,y). */
4268 (mult (POWI:s @0 @1) (POWI:s @2 @1))
4269 (POWI (mult @0 @2) @1))
4271 /* Simplify pow(x,c) / x -> pow(x,c-1). */
4273 (rdiv (POW:s @0 REAL_CST@1) @0)
4274 (if (!TREE_OVERFLOW (@1))
4275 (POW @0 (minus @1 { build_one_cst (type); }))))
4277 /* Simplify x / pow (y,z) -> x * pow(y,-z). */
4279 (rdiv @0 (POW:s @1 @2))
4280 (mult @0 (POW @1 (negate @2))))
4285 /* sqrt(sqrt(x)) -> pow(x,1/4). */
4288 (pows @0 { build_real (type, dconst_quarter ()); }))
4289 /* sqrt(cbrt(x)) -> pow(x,1/6). */
4292 (pows @0 { build_real_truncate (type, dconst_sixth ()); }))
4293 /* cbrt(sqrt(x)) -> pow(x,1/6). */
4296 (pows @0 { build_real_truncate (type, dconst_sixth ()); }))
4297 /* cbrt(cbrt(x)) -> pow(x,1/9), iff x is nonnegative. */
4299 (cbrts (cbrts tree_expr_nonnegative_p@0))
4300 (pows @0 { build_real_truncate (type, dconst_ninth ()); }))
4301 /* sqrt(pow(x,y)) -> pow(|x|,y*0.5). */
4303 (sqrts (pows @0 @1))
4304 (pows (abs @0) (mult @1 { build_real (type, dconsthalf); })))
4305 /* cbrt(pow(x,y)) -> pow(x,y/3), iff x is nonnegative. */
4307 (cbrts (pows tree_expr_nonnegative_p@0 @1))
4308 (pows @0 (mult @1 { build_real_truncate (type, dconst_third ()); })))
4309 /* pow(sqrt(x),y) -> pow(x,y*0.5). */
4311 (pows (sqrts @0) @1)
4312 (pows @0 (mult @1 { build_real (type, dconsthalf); })))
4313 /* pow(cbrt(x),y) -> pow(x,y/3) iff x is nonnegative. */
4315 (pows (cbrts tree_expr_nonnegative_p@0) @1)
4316 (pows @0 (mult @1 { build_real_truncate (type, dconst_third ()); })))
4317 /* pow(pow(x,y),z) -> pow(x,y*z) iff x is nonnegative. */
4319 (pows (pows tree_expr_nonnegative_p@0 @1) @2)
4320 (pows @0 (mult @1 @2))))
4322 /* cabs(x+xi) -> fabs(x)*sqrt(2). */
4324 (CABS (complex @0 @0))
4325 (mult (abs @0) { build_real_truncate (type, dconst_sqrt2 ()); }))
4327 /* hypot(x,x) -> fabs(x)*sqrt(2). */
4330 (mult (abs @0) { build_real_truncate (type, dconst_sqrt2 ()); }))
4332 /* cexp(x+yi) -> exp(x)*cexpi(y). */
4337 (cexps compositional_complex@0)
4338 (if (targetm.libc_has_function (function_c99_math_complex))
4340 (mult (exps@1 (realpart @0)) (realpart (cexpis:type@2 (imagpart @0))))
4341 (mult @1 (imagpart @2)))))))
4343 (if (canonicalize_math_p ())
4344 /* floor(x) -> trunc(x) if x is nonnegative. */
4345 (for floors (FLOOR_ALL)
4348 (floors tree_expr_nonnegative_p@0)
4351 (match double_value_p
4353 (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == double_type_node)))
4354 (for froms (BUILT_IN_TRUNCL
4366 /* truncl(extend(x)) -> extend(trunc(x)), etc., if x is a double. */
4367 (if (optimize && canonicalize_math_p ())
4369 (froms (convert double_value_p@0))
4370 (convert (tos @0)))))
4372 (match float_value_p
4374 (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == float_type_node)))
4375 (for froms (BUILT_IN_TRUNCL BUILT_IN_TRUNC
4376 BUILT_IN_FLOORL BUILT_IN_FLOOR
4377 BUILT_IN_CEILL BUILT_IN_CEIL
4378 BUILT_IN_ROUNDL BUILT_IN_ROUND
4379 BUILT_IN_NEARBYINTL BUILT_IN_NEARBYINT
4380 BUILT_IN_RINTL BUILT_IN_RINT)
4381 tos (BUILT_IN_TRUNCF BUILT_IN_TRUNCF
4382 BUILT_IN_FLOORF BUILT_IN_FLOORF
4383 BUILT_IN_CEILF BUILT_IN_CEILF
4384 BUILT_IN_ROUNDF BUILT_IN_ROUNDF
4385 BUILT_IN_NEARBYINTF BUILT_IN_NEARBYINTF
4386 BUILT_IN_RINTF BUILT_IN_RINTF)
4387 /* truncl(extend(x)) and trunc(extend(x)) -> extend(truncf(x)), etc.,
4389 (if (optimize && canonicalize_math_p ()
4390 && targetm.libc_has_function (function_c99_misc))
4392 (froms (convert float_value_p@0))
4393 (convert (tos @0)))))
4395 (for froms (XFLOORL XCEILL XROUNDL XRINTL)
4396 tos (XFLOOR XCEIL XROUND XRINT)
4397 /* llfloorl(extend(x)) -> llfloor(x), etc., if x is a double. */
4398 (if (optimize && canonicalize_math_p ())
4400 (froms (convert double_value_p@0))
4403 (for froms (XFLOORL XCEILL XROUNDL XRINTL
4404 XFLOOR XCEIL XROUND XRINT)
4405 tos (XFLOORF XCEILF XROUNDF XRINTF)
4406 /* llfloorl(extend(x)) and llfloor(extend(x)) -> llfloorf(x), etc.,
4408 (if (optimize && canonicalize_math_p ())
4410 (froms (convert float_value_p@0))
4413 (if (canonicalize_math_p ())
4414 /* xfloor(x) -> fix_trunc(x) if x is nonnegative. */
4415 (for floors (IFLOOR LFLOOR LLFLOOR)
4417 (floors tree_expr_nonnegative_p@0)
4420 (if (canonicalize_math_p ())
4421 /* xfloor(x) -> fix_trunc(x), etc., if x is integer valued. */
4422 (for fns (IFLOOR LFLOOR LLFLOOR
4424 IROUND LROUND LLROUND)
4426 (fns integer_valued_real_p@0)
4428 (if (!flag_errno_math)
4429 /* xrint(x) -> fix_trunc(x), etc., if x is integer valued. */
4430 (for rints (IRINT LRINT LLRINT)
4432 (rints integer_valued_real_p@0)
4435 (if (canonicalize_math_p ())
4436 (for ifn (IFLOOR ICEIL IROUND IRINT)
4437 lfn (LFLOOR LCEIL LROUND LRINT)
4438 llfn (LLFLOOR LLCEIL LLROUND LLRINT)
4439 /* Canonicalize iround (x) to lround (x) on ILP32 targets where
4440 sizeof (int) == sizeof (long). */
4441 (if (TYPE_PRECISION (integer_type_node)
4442 == TYPE_PRECISION (long_integer_type_node))
4445 (lfn:long_integer_type_node @0)))
4446 /* Canonicalize llround (x) to lround (x) on LP64 targets where
4447 sizeof (long long) == sizeof (long). */
4448 (if (TYPE_PRECISION (long_long_integer_type_node)
4449 == TYPE_PRECISION (long_integer_type_node))
4452 (lfn:long_integer_type_node @0)))))
4454 /* cproj(x) -> x if we're ignoring infinities. */
4457 (if (!HONOR_INFINITIES (type))
4460 /* If the real part is inf and the imag part is known to be
4461 nonnegative, return (inf + 0i). */
4463 (CPROJ (complex REAL_CST@0 tree_expr_nonnegative_p@1))
4464 (if (real_isinf (TREE_REAL_CST_PTR (@0)))
4465 { build_complex_inf (type, false); }))
4467 /* If the imag part is inf, return (inf+I*copysign(0,imag)). */
4469 (CPROJ (complex @0 REAL_CST@1))
4470 (if (real_isinf (TREE_REAL_CST_PTR (@1)))
4471 { build_complex_inf (type, TREE_REAL_CST_PTR (@1)->sign); }))
4477 (pows @0 REAL_CST@1)
4479 const REAL_VALUE_TYPE *value = TREE_REAL_CST_PTR (@1);
4480 REAL_VALUE_TYPE tmp;
4483 /* pow(x,0) -> 1. */
4484 (if (real_equal (value, &dconst0))
4485 { build_real (type, dconst1); })
4486 /* pow(x,1) -> x. */
4487 (if (real_equal (value, &dconst1))
4489 /* pow(x,-1) -> 1/x. */
4490 (if (real_equal (value, &dconstm1))
4491 (rdiv { build_real (type, dconst1); } @0))
4492 /* pow(x,0.5) -> sqrt(x). */
4493 (if (flag_unsafe_math_optimizations
4494 && canonicalize_math_p ()
4495 && real_equal (value, &dconsthalf))
4497 /* pow(x,1/3) -> cbrt(x). */
4498 (if (flag_unsafe_math_optimizations
4499 && canonicalize_math_p ()
4500 && (tmp = real_value_truncate (TYPE_MODE (type), dconst_third ()),
4501 real_equal (value, &tmp)))
4504 /* powi(1,x) -> 1. */
4506 (POWI real_onep@0 @1)
4510 (POWI @0 INTEGER_CST@1)
4512 /* powi(x,0) -> 1. */
4513 (if (wi::to_wide (@1) == 0)
4514 { build_real (type, dconst1); })
4515 /* powi(x,1) -> x. */
4516 (if (wi::to_wide (@1) == 1)
4518 /* powi(x,-1) -> 1/x. */
4519 (if (wi::to_wide (@1) == -1)
4520 (rdiv { build_real (type, dconst1); } @0))))
4522 /* Narrowing of arithmetic and logical operations.
4524 These are conceptually similar to the transformations performed for
4525 the C/C++ front-ends by shorten_binary_op and shorten_compare. Long
4526 term we want to move all that code out of the front-ends into here. */
4528 /* If we have a narrowing conversion of an arithmetic operation where
4529 both operands are widening conversions from the same type as the outer
4530 narrowing conversion. Then convert the innermost operands to a suitable
4531 unsigned type (to avoid introducing undefined behavior), perform the
4532 operation and convert the result to the desired type. */
4533 (for op (plus minus)
4535 (convert (op:s (convert@2 @0) (convert?@3 @1)))
4536 (if (INTEGRAL_TYPE_P (type)
4537 /* We check for type compatibility between @0 and @1 below,
4538 so there's no need to check that @1/@3 are integral types. */
4539 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
4540 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
4541 /* The precision of the type of each operand must match the
4542 precision of the mode of each operand, similarly for the
4544 && type_has_mode_precision_p (TREE_TYPE (@0))
4545 && type_has_mode_precision_p (TREE_TYPE (@1))
4546 && type_has_mode_precision_p (type)
4547 /* The inner conversion must be a widening conversion. */
4548 && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
4549 && types_match (@0, type)
4550 && (types_match (@0, @1)
4551 /* Or the second operand is const integer or converted const
4552 integer from valueize. */
4553 || TREE_CODE (@1) == INTEGER_CST))
4554 (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
4555 (op @0 (convert @1))
4556 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
4557 (convert (op (convert:utype @0)
4558 (convert:utype @1))))))))
4560 /* This is another case of narrowing, specifically when there's an outer
4561 BIT_AND_EXPR which masks off bits outside the type of the innermost
4562 operands. Like the previous case we have to convert the operands
4563 to unsigned types to avoid introducing undefined behavior for the
4564 arithmetic operation. */
4565 (for op (minus plus)
4567 (bit_and (op:s (convert@2 @0) (convert@3 @1)) INTEGER_CST@4)
4568 (if (INTEGRAL_TYPE_P (type)
4569 /* We check for type compatibility between @0 and @1 below,
4570 so there's no need to check that @1/@3 are integral types. */
4571 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
4572 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
4573 /* The precision of the type of each operand must match the
4574 precision of the mode of each operand, similarly for the
4576 && type_has_mode_precision_p (TREE_TYPE (@0))
4577 && type_has_mode_precision_p (TREE_TYPE (@1))
4578 && type_has_mode_precision_p (type)
4579 /* The inner conversion must be a widening conversion. */
4580 && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
4581 && types_match (@0, @1)
4582 && (tree_int_cst_min_precision (@4, TYPE_SIGN (TREE_TYPE (@0)))
4583 <= TYPE_PRECISION (TREE_TYPE (@0)))
4584 && (wi::to_wide (@4)
4585 & wi::mask (TYPE_PRECISION (TREE_TYPE (@0)),
4586 true, TYPE_PRECISION (type))) == 0)
4587 (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
4588 (with { tree ntype = TREE_TYPE (@0); }
4589 (convert (bit_and (op @0 @1) (convert:ntype @4))))
4590 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
4591 (convert (bit_and (op (convert:utype @0) (convert:utype @1))
4592 (convert:utype @4))))))))
4594 /* Transform (@0 < @1 and @0 < @2) to use min,
4595 (@0 > @1 and @0 > @2) to use max */
4596 (for logic (bit_and bit_and bit_and bit_and bit_ior bit_ior bit_ior bit_ior)
4597 op (lt le gt ge lt le gt ge )
4598 ext (min min max max max max min min )
4600 (logic (op:cs @0 @1) (op:cs @0 @2))
4601 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
4602 && TREE_CODE (@0) != INTEGER_CST)
4603 (op @0 (ext @1 @2)))))
4606 /* signbit(x) -> 0 if x is nonnegative. */
4607 (SIGNBIT tree_expr_nonnegative_p@0)
4608 { integer_zero_node; })
4611 /* signbit(x) -> x<0 if x doesn't have signed zeros. */
4613 (if (!HONOR_SIGNED_ZEROS (@0))
4614 (convert (lt @0 { build_real (TREE_TYPE (@0), dconst0); }))))
4616 /* Transform comparisons of the form X +- C1 CMP C2 to X CMP C2 -+ C1. */
4618 (for op (plus minus)
4621 (cmp (op@3 @0 INTEGER_CST@1) INTEGER_CST@2)
4622 (if (!TREE_OVERFLOW (@1) && !TREE_OVERFLOW (@2)
4623 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0))
4624 && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@0))
4625 && !TYPE_SATURATING (TREE_TYPE (@0)))
4626 (with { tree res = int_const_binop (rop, @2, @1); }
4627 (if (TREE_OVERFLOW (res)
4628 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
4629 { constant_boolean_node (cmp == NE_EXPR, type); }
4630 (if (single_use (@3))
4631 (cmp @0 { TREE_OVERFLOW (res)
4632 ? drop_tree_overflow (res) : res; }))))))))
4633 (for cmp (lt le gt ge)
4634 (for op (plus minus)
4637 (cmp (op@3 @0 INTEGER_CST@1) INTEGER_CST@2)
4638 (if (!TREE_OVERFLOW (@1) && !TREE_OVERFLOW (@2)
4639 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
4640 (with { tree res = int_const_binop (rop, @2, @1); }
4641 (if (TREE_OVERFLOW (res))
4643 fold_overflow_warning (("assuming signed overflow does not occur "
4644 "when simplifying conditional to constant"),
4645 WARN_STRICT_OVERFLOW_CONDITIONAL);
4646 bool less = cmp == LE_EXPR || cmp == LT_EXPR;
4647 /* wi::ges_p (@2, 0) should be sufficient for a signed type. */
4648 bool ovf_high = wi::lt_p (wi::to_wide (@1), 0,
4649 TYPE_SIGN (TREE_TYPE (@1)))
4650 != (op == MINUS_EXPR);
4651 constant_boolean_node (less == ovf_high, type);
4653 (if (single_use (@3))
4656 fold_overflow_warning (("assuming signed overflow does not occur "
4657 "when changing X +- C1 cmp C2 to "
4659 WARN_STRICT_OVERFLOW_COMPARISON);
4661 (cmp @0 { res; })))))))))
4663 /* Canonicalizations of BIT_FIELD_REFs. */
4666 (BIT_FIELD_REF @0 @1 @2)
4668 (if (TREE_CODE (TREE_TYPE (@0)) == COMPLEX_TYPE
4669 && tree_int_cst_equal (@1, TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))))
4671 (if (integer_zerop (@2))
4672 (view_convert (realpart @0)))
4673 (if (tree_int_cst_equal (@2, TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))))
4674 (view_convert (imagpart @0)))))
4675 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
4676 && INTEGRAL_TYPE_P (type)
4677 /* On GIMPLE this should only apply to register arguments. */
4678 && (! GIMPLE || is_gimple_reg (@0))
4679 /* A bit-field-ref that referenced the full argument can be stripped. */
4680 && ((compare_tree_int (@1, TYPE_PRECISION (TREE_TYPE (@0))) == 0
4681 && integer_zerop (@2))
4682 /* Low-parts can be reduced to integral conversions.
4683 ??? The following doesn't work for PDP endian. */
4684 || (BYTES_BIG_ENDIAN == WORDS_BIG_ENDIAN
4685 /* Don't even think about BITS_BIG_ENDIAN. */
4686 && TYPE_PRECISION (TREE_TYPE (@0)) % BITS_PER_UNIT == 0
4687 && TYPE_PRECISION (type) % BITS_PER_UNIT == 0
4688 && compare_tree_int (@2, (BYTES_BIG_ENDIAN
4689 ? (TYPE_PRECISION (TREE_TYPE (@0))
4690 - TYPE_PRECISION (type))
4694 /* Simplify vector extracts. */
4697 (BIT_FIELD_REF CONSTRUCTOR@0 @1 @2)
4698 (if (VECTOR_TYPE_P (TREE_TYPE (@0))
4699 && (types_match (type, TREE_TYPE (TREE_TYPE (@0)))
4700 || (VECTOR_TYPE_P (type)
4701 && types_match (TREE_TYPE (type), TREE_TYPE (TREE_TYPE (@0))))))
4704 tree ctor = (TREE_CODE (@0) == SSA_NAME
4705 ? gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0)) : @0);
4706 tree eltype = TREE_TYPE (TREE_TYPE (ctor));
4707 unsigned HOST_WIDE_INT width = tree_to_uhwi (TYPE_SIZE (eltype));
4708 unsigned HOST_WIDE_INT n = tree_to_uhwi (@1);
4709 unsigned HOST_WIDE_INT idx = tree_to_uhwi (@2);
4712 && (idx % width) == 0
4714 && known_le ((idx + n) / width,
4715 TYPE_VECTOR_SUBPARTS (TREE_TYPE (ctor))))
4720 /* Constructor elements can be subvectors. */
4722 if (CONSTRUCTOR_NELTS (ctor) != 0)
4724 tree cons_elem = TREE_TYPE (CONSTRUCTOR_ELT (ctor, 0)->value);
4725 if (TREE_CODE (cons_elem) == VECTOR_TYPE)
4726 k = TYPE_VECTOR_SUBPARTS (cons_elem);
4728 unsigned HOST_WIDE_INT elt, count, const_k;
4731 /* We keep an exact subset of the constructor elements. */
4732 (if (multiple_p (idx, k, &elt) && multiple_p (n, k, &count))
4733 (if (CONSTRUCTOR_NELTS (ctor) == 0)
4734 { build_constructor (type, NULL); }
4736 (if (elt < CONSTRUCTOR_NELTS (ctor))
4737 (view_convert { CONSTRUCTOR_ELT (ctor, elt)->value; })
4738 { build_zero_cst (type); })
4740 vec<constructor_elt, va_gc> *vals;
4741 vec_alloc (vals, count);
4742 for (unsigned i = 0;
4743 i < count && elt + i < CONSTRUCTOR_NELTS (ctor); ++i)
4744 CONSTRUCTOR_APPEND_ELT (vals, NULL_TREE,
4745 CONSTRUCTOR_ELT (ctor, elt + i)->value);
4746 build_constructor (type, vals);
4748 /* The bitfield references a single constructor element. */
4749 (if (k.is_constant (&const_k)
4750 && idx + n <= (idx / const_k + 1) * const_k)
4752 (if (CONSTRUCTOR_NELTS (ctor) <= idx / const_k)
4753 { build_zero_cst (type); })
4755 (view_convert { CONSTRUCTOR_ELT (ctor, idx / const_k)->value; }))
4756 (BIT_FIELD_REF { CONSTRUCTOR_ELT (ctor, idx / const_k)->value; }
4757 @1 { bitsize_int ((idx % const_k) * width); })))))))))
4759 /* Simplify a bit extraction from a bit insertion for the cases with
4760 the inserted element fully covering the extraction or the insertion
4761 not touching the extraction. */
4763 (BIT_FIELD_REF (bit_insert @0 @1 @ipos) @rsize @rpos)
4766 unsigned HOST_WIDE_INT isize;
4767 if (INTEGRAL_TYPE_P (TREE_TYPE (@1)))
4768 isize = TYPE_PRECISION (TREE_TYPE (@1));
4770 isize = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (@1)));
4773 (if (wi::leu_p (wi::to_wide (@ipos), wi::to_wide (@rpos))
4774 && wi::leu_p (wi::to_wide (@rpos) + wi::to_wide (@rsize),
4775 wi::to_wide (@ipos) + isize))
4776 (BIT_FIELD_REF @1 @rsize { wide_int_to_tree (bitsizetype,
4778 - wi::to_wide (@ipos)); }))
4779 (if (wi::geu_p (wi::to_wide (@ipos),
4780 wi::to_wide (@rpos) + wi::to_wide (@rsize))
4781 || wi::geu_p (wi::to_wide (@rpos),
4782 wi::to_wide (@ipos) + isize))
4783 (BIT_FIELD_REF @0 @rsize @rpos)))))
4785 (if (canonicalize_math_after_vectorization_p ())
4788 (fmas:c (negate @0) @1 @2)
4789 (IFN_FNMA @0 @1 @2))
4791 (fmas @0 @1 (negate @2))
4794 (fmas:c (negate @0) @1 (negate @2))
4795 (IFN_FNMS @0 @1 @2))
4797 (negate (fmas@3 @0 @1 @2))
4798 (if (single_use (@3))
4799 (IFN_FNMS @0 @1 @2))))
4802 (IFN_FMS:c (negate @0) @1 @2)
4803 (IFN_FNMS @0 @1 @2))
4805 (IFN_FMS @0 @1 (negate @2))
4808 (IFN_FMS:c (negate @0) @1 (negate @2))
4809 (IFN_FNMA @0 @1 @2))
4811 (negate (IFN_FMS@3 @0 @1 @2))
4812 (if (single_use (@3))
4813 (IFN_FNMA @0 @1 @2)))
4816 (IFN_FNMA:c (negate @0) @1 @2)
4819 (IFN_FNMA @0 @1 (negate @2))
4820 (IFN_FNMS @0 @1 @2))
4822 (IFN_FNMA:c (negate @0) @1 (negate @2))
4825 (negate (IFN_FNMA@3 @0 @1 @2))
4826 (if (single_use (@3))
4827 (IFN_FMS @0 @1 @2)))
4830 (IFN_FNMS:c (negate @0) @1 @2)
4833 (IFN_FNMS @0 @1 (negate @2))
4834 (IFN_FNMA @0 @1 @2))
4836 (IFN_FNMS:c (negate @0) @1 (negate @2))
4839 (negate (IFN_FNMS@3 @0 @1 @2))
4840 (if (single_use (@3))
4841 (IFN_FMA @0 @1 @2))))
4843 /* POPCOUNT simplifications. */
4844 (for popcount (BUILT_IN_POPCOUNT BUILT_IN_POPCOUNTL BUILT_IN_POPCOUNTLL
4845 BUILT_IN_POPCOUNTIMAX)
4846 /* popcount(X&1) is nop_expr(X&1). */
4849 (if (tree_nonzero_bits (@0) == 1)
4851 /* popcount(X) + popcount(Y) is popcount(X|Y) when X&Y must be zero. */
4853 (plus (popcount:s @0) (popcount:s @1))
4854 (if (wi::bit_and (tree_nonzero_bits (@0), tree_nonzero_bits (@1)) == 0)
4855 (popcount (bit_ior @0 @1))))
4856 /* popcount(X) == 0 is X == 0, and related (in)equalities. */
4857 (for cmp (le eq ne gt)
4860 (cmp (popcount @0) integer_zerop)
4861 (rep @0 { build_zero_cst (TREE_TYPE (@0)); }))))
4870 r = c ? a1 op a2 : b;
4872 if the target can do it in one go. This makes the operation conditional
4873 on c, so could drop potentially-trapping arithmetic, but that's a valid
4874 simplification if the result of the operation isn't needed. */
4875 (for uncond_op (UNCOND_BINARY)
4876 cond_op (COND_BINARY)
4878 (vec_cond @0 (view_convert? (uncond_op@4 @1 @2)) @3)
4879 (with { tree op_type = TREE_TYPE (@4); }
4880 (if (element_precision (type) == element_precision (op_type))
4881 (view_convert (cond_op @0 @1 @2 (view_convert:op_type @3))))))
4883 (vec_cond @0 @1 (view_convert? (uncond_op@4 @2 @3)))
4884 (with { tree op_type = TREE_TYPE (@4); }
4885 (if (element_precision (type) == element_precision (op_type))
4886 (view_convert (cond_op (bit_not @0) @2 @3 (view_convert:op_type @1)))))))