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 /* As opposed to convert?, this still creates a single pattern, so
79 it is not a suitable replacement for convert? in all cases. */
80 (match (nop_convert @0)
82 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))))
83 (match (nop_convert @0)
85 (if (VECTOR_TYPE_P (type) && VECTOR_TYPE_P (TREE_TYPE (@0))
86 && known_eq (TYPE_VECTOR_SUBPARTS (type),
87 TYPE_VECTOR_SUBPARTS (TREE_TYPE (@0)))
88 && tree_nop_conversion_p (TREE_TYPE (type), TREE_TYPE (TREE_TYPE (@0))))))
89 /* This one has to be last, or it shadows the others. */
90 (match (nop_convert @0)
93 /* Simplifications of operations with one constant operand and
94 simplifications to constants or single values. */
96 (for op (plus pointer_plus minus bit_ior bit_xor)
101 /* 0 +p index -> (type)index */
103 (pointer_plus integer_zerop @1)
104 (non_lvalue (convert @1)))
106 /* ptr - 0 -> (type)ptr */
108 (pointer_diff @0 integer_zerop)
111 /* See if ARG1 is zero and X + ARG1 reduces to X.
112 Likewise if the operands are reversed. */
114 (plus:c @0 real_zerop@1)
115 (if (fold_real_zero_addition_p (type, @1, 0))
118 /* See if ARG1 is zero and X - ARG1 reduces to X. */
120 (minus @0 real_zerop@1)
121 (if (fold_real_zero_addition_p (type, @1, 1))
125 This is unsafe for certain floats even in non-IEEE formats.
126 In IEEE, it is unsafe because it does wrong for NaNs.
127 Also note that operand_equal_p is always false if an operand
131 (if (!FLOAT_TYPE_P (type) || !HONOR_NANS (type))
132 { build_zero_cst (type); }))
134 (pointer_diff @@0 @0)
135 { build_zero_cst (type); })
138 (mult @0 integer_zerop@1)
141 /* Maybe fold x * 0 to 0. The expressions aren't the same
142 when x is NaN, since x * 0 is also NaN. Nor are they the
143 same in modes with signed zeros, since multiplying a
144 negative value by 0 gives -0, not +0. */
146 (mult @0 real_zerop@1)
147 (if (!HONOR_NANS (type) && !HONOR_SIGNED_ZEROS (type))
150 /* In IEEE floating point, x*1 is not equivalent to x for snans.
151 Likewise for complex arithmetic with signed zeros. */
154 (if (!HONOR_SNANS (type)
155 && (!HONOR_SIGNED_ZEROS (type)
156 || !COMPLEX_FLOAT_TYPE_P (type)))
159 /* Transform x * -1.0 into -x. */
161 (mult @0 real_minus_onep)
162 (if (!HONOR_SNANS (type)
163 && (!HONOR_SIGNED_ZEROS (type)
164 || !COMPLEX_FLOAT_TYPE_P (type)))
167 (for cmp (gt ge lt le)
168 outp (convert convert negate negate)
169 outn (negate negate convert convert)
170 /* Transform (X > 0.0 ? 1.0 : -1.0) into copysign(1, X). */
171 /* Transform (X >= 0.0 ? 1.0 : -1.0) into copysign(1, X). */
172 /* Transform (X < 0.0 ? 1.0 : -1.0) into copysign(1,-X). */
173 /* Transform (X <= 0.0 ? 1.0 : -1.0) into copysign(1,-X). */
175 (cond (cmp @0 real_zerop) real_onep@1 real_minus_onep)
176 (if (!HONOR_NANS (type) && !HONOR_SIGNED_ZEROS (type)
177 && types_match (type, TREE_TYPE (@0)))
179 (if (types_match (type, float_type_node))
180 (BUILT_IN_COPYSIGNF @1 (outp @0)))
181 (if (types_match (type, double_type_node))
182 (BUILT_IN_COPYSIGN @1 (outp @0)))
183 (if (types_match (type, long_double_type_node))
184 (BUILT_IN_COPYSIGNL @1 (outp @0))))))
185 /* Transform (X > 0.0 ? -1.0 : 1.0) into copysign(1,-X). */
186 /* Transform (X >= 0.0 ? -1.0 : 1.0) into copysign(1,-X). */
187 /* Transform (X < 0.0 ? -1.0 : 1.0) into copysign(1,X). */
188 /* Transform (X <= 0.0 ? -1.0 : 1.0) into copysign(1,X). */
190 (cond (cmp @0 real_zerop) real_minus_onep real_onep@1)
191 (if (!HONOR_NANS (type) && !HONOR_SIGNED_ZEROS (type)
192 && types_match (type, TREE_TYPE (@0)))
194 (if (types_match (type, float_type_node))
195 (BUILT_IN_COPYSIGNF @1 (outn @0)))
196 (if (types_match (type, double_type_node))
197 (BUILT_IN_COPYSIGN @1 (outn @0)))
198 (if (types_match (type, long_double_type_node))
199 (BUILT_IN_COPYSIGNL @1 (outn @0)))))))
201 /* Transform X * copysign (1.0, X) into abs(X). */
203 (mult:c @0 (COPYSIGN_ALL real_onep @0))
204 (if (!HONOR_NANS (type) && !HONOR_SIGNED_ZEROS (type))
207 /* Transform X * copysign (1.0, -X) into -abs(X). */
209 (mult:c @0 (COPYSIGN_ALL real_onep (negate @0)))
210 (if (!HONOR_NANS (type) && !HONOR_SIGNED_ZEROS (type))
213 /* Transform copysign (CST, X) into copysign (ABS(CST), X). */
215 (COPYSIGN_ALL REAL_CST@0 @1)
216 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@0)))
217 (COPYSIGN_ALL (negate @0) @1)))
219 /* X * 1, X / 1 -> X. */
220 (for op (mult trunc_div ceil_div floor_div round_div exact_div)
225 /* (A / (1 << B)) -> (A >> B).
226 Only for unsigned A. For signed A, this would not preserve rounding
228 For example: (-1 / ( 1 << B)) != -1 >> B. */
230 (trunc_div @0 (lshift integer_onep@1 @2))
231 (if ((TYPE_UNSIGNED (type) || tree_expr_nonnegative_p (@0))
232 && (!VECTOR_TYPE_P (type)
233 || target_supports_op_p (type, RSHIFT_EXPR, optab_vector)
234 || target_supports_op_p (type, RSHIFT_EXPR, optab_scalar)))
237 /* Preserve explicit divisions by 0: the C++ front-end wants to detect
238 undefined behavior in constexpr evaluation, and assuming that the division
239 traps enables better optimizations than these anyway. */
240 (for div (trunc_div ceil_div floor_div round_div exact_div)
241 /* 0 / X is always zero. */
243 (div integer_zerop@0 @1)
244 /* But not for 0 / 0 so that we can get the proper warnings and errors. */
245 (if (!integer_zerop (@1))
249 (div @0 integer_minus_onep@1)
250 (if (!TYPE_UNSIGNED (type))
255 /* But not for 0 / 0 so that we can get the proper warnings and errors.
256 And not for _Fract types where we can't build 1. */
257 (if (!integer_zerop (@0) && !ALL_FRACT_MODE_P (TYPE_MODE (type)))
258 { build_one_cst (type); }))
259 /* X / abs (X) is X < 0 ? -1 : 1. */
262 (if (INTEGRAL_TYPE_P (type)
263 && TYPE_OVERFLOW_UNDEFINED (type))
264 (cond (lt @0 { build_zero_cst (type); })
265 { build_minus_one_cst (type); } { build_one_cst (type); })))
268 (div:C @0 (negate @0))
269 (if ((INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
270 && TYPE_OVERFLOW_UNDEFINED (type))
271 { build_minus_one_cst (type); })))
273 /* For unsigned integral types, FLOOR_DIV_EXPR is the same as
274 TRUNC_DIV_EXPR. Rewrite into the latter in this case. */
277 (if ((INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
278 && TYPE_UNSIGNED (type))
281 /* Combine two successive divisions. Note that combining ceil_div
282 and floor_div is trickier and combining round_div even more so. */
283 (for div (trunc_div exact_div)
285 (div (div @0 INTEGER_CST@1) INTEGER_CST@2)
288 wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@2),
289 TYPE_SIGN (type), &overflow_p);
292 (div @0 { wide_int_to_tree (type, mul); })
293 (if (TYPE_UNSIGNED (type)
294 || mul != wi::min_value (TYPE_PRECISION (type), SIGNED))
295 { build_zero_cst (type); })))))
297 /* Combine successive multiplications. Similar to above, but handling
298 overflow is different. */
300 (mult (mult @0 INTEGER_CST@1) INTEGER_CST@2)
303 wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@2),
304 TYPE_SIGN (type), &overflow_p);
306 /* Skip folding on overflow: the only special case is @1 * @2 == -INT_MIN,
307 otherwise undefined overflow implies that @0 must be zero. */
308 (if (!overflow_p || TYPE_OVERFLOW_WRAPS (type))
309 (mult @0 { wide_int_to_tree (type, mul); }))))
311 /* Optimize A / A to 1.0 if we don't care about
312 NaNs or Infinities. */
315 (if (FLOAT_TYPE_P (type)
316 && ! HONOR_NANS (type)
317 && ! HONOR_INFINITIES (type))
318 { build_one_cst (type); }))
320 /* Optimize -A / A to -1.0 if we don't care about
321 NaNs or Infinities. */
323 (rdiv:C @0 (negate @0))
324 (if (FLOAT_TYPE_P (type)
325 && ! HONOR_NANS (type)
326 && ! HONOR_INFINITIES (type))
327 { build_minus_one_cst (type); }))
329 /* PR71078: x / abs(x) -> copysign (1.0, x) */
331 (rdiv:C (convert? @0) (convert? (abs @0)))
332 (if (SCALAR_FLOAT_TYPE_P (type)
333 && ! HONOR_NANS (type)
334 && ! HONOR_INFINITIES (type))
336 (if (types_match (type, float_type_node))
337 (BUILT_IN_COPYSIGNF { build_one_cst (type); } (convert @0)))
338 (if (types_match (type, double_type_node))
339 (BUILT_IN_COPYSIGN { build_one_cst (type); } (convert @0)))
340 (if (types_match (type, long_double_type_node))
341 (BUILT_IN_COPYSIGNL { build_one_cst (type); } (convert @0))))))
343 /* In IEEE floating point, x/1 is not equivalent to x for snans. */
346 (if (!HONOR_SNANS (type))
349 /* In IEEE floating point, x/-1 is not equivalent to -x for snans. */
351 (rdiv @0 real_minus_onep)
352 (if (!HONOR_SNANS (type))
355 (if (flag_reciprocal_math)
356 /* Convert (A/B)/C to A/(B*C). */
358 (rdiv (rdiv:s @0 @1) @2)
359 (rdiv @0 (mult @1 @2)))
361 /* Canonicalize x / (C1 * y) to (x * C2) / y. */
363 (rdiv @0 (mult:s @1 REAL_CST@2))
365 { tree tem = const_binop (RDIV_EXPR, type, build_one_cst (type), @2); }
367 (rdiv (mult @0 { tem; } ) @1))))
369 /* Convert A/(B/C) to (A/B)*C */
371 (rdiv @0 (rdiv:s @1 @2))
372 (mult (rdiv @0 @1) @2)))
374 /* Simplify x / (- y) to -x / y. */
376 (rdiv @0 (negate @1))
377 (rdiv (negate @0) @1))
379 /* Optimize (X & (-A)) / A where A is a power of 2, to X >> log2(A) */
380 (for div (trunc_div ceil_div floor_div round_div exact_div)
382 (div (convert? (bit_and @0 INTEGER_CST@1)) INTEGER_CST@2)
383 (if (integer_pow2p (@2)
384 && tree_int_cst_sgn (@2) > 0
385 && tree_nop_conversion_p (type, TREE_TYPE (@0))
386 && wi::to_wide (@2) + wi::to_wide (@1) == 0)
388 { build_int_cst (integer_type_node,
389 wi::exact_log2 (wi::to_wide (@2))); }))))
391 /* If ARG1 is a constant, we can convert this to a multiply by the
392 reciprocal. This does not have the same rounding properties,
393 so only do this if -freciprocal-math. We can actually
394 always safely do it if ARG1 is a power of two, but it's hard to
395 tell if it is or not in a portable manner. */
396 (for cst (REAL_CST COMPLEX_CST VECTOR_CST)
400 (if (flag_reciprocal_math
403 { tree tem = const_binop (RDIV_EXPR, type, build_one_cst (type), @1); }
405 (mult @0 { tem; } )))
406 (if (cst != COMPLEX_CST)
407 (with { tree inverse = exact_inverse (type, @1); }
409 (mult @0 { inverse; } ))))))))
411 (for mod (ceil_mod floor_mod round_mod trunc_mod)
412 /* 0 % X is always zero. */
414 (mod integer_zerop@0 @1)
415 /* But not for 0 % 0 so that we can get the proper warnings and errors. */
416 (if (!integer_zerop (@1))
418 /* X % 1 is always zero. */
420 (mod @0 integer_onep)
421 { build_zero_cst (type); })
422 /* X % -1 is zero. */
424 (mod @0 integer_minus_onep@1)
425 (if (!TYPE_UNSIGNED (type))
426 { build_zero_cst (type); }))
430 /* But not for 0 % 0 so that we can get the proper warnings and errors. */
431 (if (!integer_zerop (@0))
432 { build_zero_cst (type); }))
433 /* (X % Y) % Y is just X % Y. */
435 (mod (mod@2 @0 @1) @1)
437 /* From extract_muldiv_1: (X * C1) % C2 is zero if C1 is a multiple of C2. */
439 (mod (mult @0 INTEGER_CST@1) INTEGER_CST@2)
440 (if (ANY_INTEGRAL_TYPE_P (type)
441 && TYPE_OVERFLOW_UNDEFINED (type)
442 && wi::multiple_of_p (wi::to_wide (@1), wi::to_wide (@2),
444 { build_zero_cst (type); })))
446 /* X % -C is the same as X % C. */
448 (trunc_mod @0 INTEGER_CST@1)
449 (if (TYPE_SIGN (type) == SIGNED
450 && !TREE_OVERFLOW (@1)
451 && wi::neg_p (wi::to_wide (@1))
452 && !TYPE_OVERFLOW_TRAPS (type)
453 /* Avoid this transformation if C is INT_MIN, i.e. C == -C. */
454 && !sign_bit_p (@1, @1))
455 (trunc_mod @0 (negate @1))))
457 /* X % -Y is the same as X % Y. */
459 (trunc_mod @0 (convert? (negate @1)))
460 (if (INTEGRAL_TYPE_P (type)
461 && !TYPE_UNSIGNED (type)
462 && !TYPE_OVERFLOW_TRAPS (type)
463 && tree_nop_conversion_p (type, TREE_TYPE (@1))
464 /* Avoid this transformation if X might be INT_MIN or
465 Y might be -1, because we would then change valid
466 INT_MIN % -(-1) into invalid INT_MIN % -1. */
467 && (expr_not_equal_to (@0, wi::to_wide (TYPE_MIN_VALUE (type)))
468 || expr_not_equal_to (@1, wi::minus_one (TYPE_PRECISION
470 (trunc_mod @0 (convert @1))))
472 /* X - (X / Y) * Y is the same as X % Y. */
474 (minus (convert1? @0) (convert2? (mult:c (trunc_div @@0 @@1) @1)))
475 (if (INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
476 (convert (trunc_mod @0 @1))))
478 /* Optimize TRUNC_MOD_EXPR by a power of two into a BIT_AND_EXPR,
479 i.e. "X % C" into "X & (C - 1)", if X and C are positive.
480 Also optimize A % (C << N) where C is a power of 2,
481 to A & ((C << N) - 1). */
482 (match (power_of_two_cand @1)
484 (match (power_of_two_cand @1)
485 (lshift INTEGER_CST@1 @2))
486 (for mod (trunc_mod floor_mod)
488 (mod @0 (convert?@3 (power_of_two_cand@1 @2)))
489 (if ((TYPE_UNSIGNED (type)
490 || tree_expr_nonnegative_p (@0))
491 && tree_nop_conversion_p (type, TREE_TYPE (@3))
492 && integer_pow2p (@2) && tree_int_cst_sgn (@2) > 0)
493 (bit_and @0 (convert (minus @1 { build_int_cst (TREE_TYPE (@1), 1); }))))))
495 /* Simplify (unsigned t * 2)/2 -> unsigned t & 0x7FFFFFFF. */
497 (trunc_div (mult @0 integer_pow2p@1) @1)
498 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
499 (bit_and @0 { wide_int_to_tree
500 (type, wi::mask (TYPE_PRECISION (type)
501 - wi::exact_log2 (wi::to_wide (@1)),
502 false, TYPE_PRECISION (type))); })))
504 /* Simplify (unsigned t / 2) * 2 -> unsigned t & ~1. */
506 (mult (trunc_div @0 integer_pow2p@1) @1)
507 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
508 (bit_and @0 (negate @1))))
510 /* Simplify (t * 2) / 2) -> t. */
511 (for div (trunc_div ceil_div floor_div round_div exact_div)
513 (div (mult:c @0 @1) @1)
514 (if (ANY_INTEGRAL_TYPE_P (type)
515 && TYPE_OVERFLOW_UNDEFINED (type))
519 /* Simplify cos(-x) and cos(|x|) -> cos(x). Similarly for cosh. */
524 /* Simplify pow(-x, y) and pow(|x|,y) -> pow(x,y) if y is an even integer. */
527 (pows (op @0) REAL_CST@1)
528 (with { HOST_WIDE_INT n; }
529 (if (real_isinteger (&TREE_REAL_CST (@1), &n) && (n & 1) == 0)
531 /* Likewise for powi. */
534 (pows (op @0) INTEGER_CST@1)
535 (if ((wi::to_wide (@1) & 1) == 0)
537 /* Strip negate and abs from both operands of hypot. */
545 /* copysign(-x, y) and copysign(abs(x), y) -> copysign(x, y). */
546 (for copysigns (COPYSIGN_ALL)
548 (copysigns (op @0) @1)
551 /* abs(x)*abs(x) -> x*x. Should be valid for all types. */
556 /* cos(copysign(x, y)) -> cos(x). Similarly for cosh. */
560 (coss (copysigns @0 @1))
563 /* pow(copysign(x, y), z) -> pow(x, z) if z is an even integer. */
567 (pows (copysigns @0 @2) REAL_CST@1)
568 (with { HOST_WIDE_INT n; }
569 (if (real_isinteger (&TREE_REAL_CST (@1), &n) && (n & 1) == 0)
571 /* Likewise for powi. */
575 (pows (copysigns @0 @2) INTEGER_CST@1)
576 (if ((wi::to_wide (@1) & 1) == 0)
581 /* hypot(copysign(x, y), z) -> hypot(x, z). */
583 (hypots (copysigns @0 @1) @2)
585 /* hypot(x, copysign(y, z)) -> hypot(x, y). */
587 (hypots @0 (copysigns @1 @2))
590 /* copysign(x, CST) -> [-]abs (x). */
591 (for copysigns (COPYSIGN_ALL)
593 (copysigns @0 REAL_CST@1)
594 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
598 /* copysign(copysign(x, y), z) -> copysign(x, z). */
599 (for copysigns (COPYSIGN_ALL)
601 (copysigns (copysigns @0 @1) @2)
604 /* copysign(x,y)*copysign(x,y) -> x*x. */
605 (for copysigns (COPYSIGN_ALL)
607 (mult (copysigns@2 @0 @1) @2)
610 /* ccos(-x) -> ccos(x). Similarly for ccosh. */
611 (for ccoss (CCOS CCOSH)
616 /* cabs(-x) and cos(conj(x)) -> cabs(x). */
617 (for ops (conj negate)
623 /* Fold (a * (1 << b)) into (a << b) */
625 (mult:c @0 (convert? (lshift integer_onep@1 @2)))
626 (if (! FLOAT_TYPE_P (type)
627 && tree_nop_conversion_p (type, TREE_TYPE (@1)))
630 /* Fold (1 << (C - x)) where C = precision(type) - 1
631 into ((1 << C) >> x). */
633 (lshift integer_onep@0 (minus@1 INTEGER_CST@2 @3))
634 (if (INTEGRAL_TYPE_P (type)
635 && wi::eq_p (wi::to_wide (@2), TYPE_PRECISION (type) - 1)
637 (if (TYPE_UNSIGNED (type))
638 (rshift (lshift @0 @2) @3)
640 { tree utype = unsigned_type_for (type); }
641 (convert (rshift (lshift (convert:utype @0) @2) @3))))))
643 /* Fold (C1/X)*C2 into (C1*C2)/X. */
645 (mult (rdiv@3 REAL_CST@0 @1) REAL_CST@2)
646 (if (flag_associative_math
649 { tree tem = const_binop (MULT_EXPR, type, @0, @2); }
651 (rdiv { tem; } @1)))))
653 /* Simplify ~X & X as zero. */
655 (bit_and:c (convert? @0) (convert? (bit_not @0)))
656 { build_zero_cst (type); })
658 /* PR71636: Transform x & ((1U << b) - 1) -> x & ~(~0U << b); */
660 (bit_and:c @0 (plus:s (lshift:s integer_onep @1) integer_minus_onep))
661 (if (TYPE_UNSIGNED (type))
662 (bit_and @0 (bit_not (lshift { build_all_ones_cst (type); } @1)))))
664 (for bitop (bit_and bit_ior)
666 /* PR35691: Transform
667 (x == 0 & y == 0) -> (x | typeof(x)(y)) == 0.
668 (x != 0 | y != 0) -> (x | typeof(x)(y)) != 0. */
670 (bitop (cmp @0 integer_zerop@2) (cmp @1 integer_zerop))
671 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
672 && INTEGRAL_TYPE_P (TREE_TYPE (@1))
673 && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
674 (cmp (bit_ior @0 (convert @1)) @2)))
676 (x == -1 & y == -1) -> (x & typeof(x)(y)) == -1.
677 (x != -1 | y != -1) -> (x & typeof(x)(y)) != -1. */
679 (bitop (cmp @0 integer_all_onesp@2) (cmp @1 integer_all_onesp))
680 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
681 && INTEGRAL_TYPE_P (TREE_TYPE (@1))
682 && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
683 (cmp (bit_and @0 (convert @1)) @2))))
685 /* Fold (A & ~B) - (A & B) into (A ^ B) - B. */
687 (minus (bit_and:cs @0 (bit_not @1)) (bit_and:cs @0 @1))
688 (minus (bit_xor @0 @1) @1))
690 (minus (bit_and:s @0 INTEGER_CST@2) (bit_and:s @0 INTEGER_CST@1))
691 (if (~wi::to_wide (@2) == wi::to_wide (@1))
692 (minus (bit_xor @0 @1) @1)))
694 /* Fold (A & B) - (A & ~B) into B - (A ^ B). */
696 (minus (bit_and:cs @0 @1) (bit_and:cs @0 (bit_not @1)))
697 (minus @1 (bit_xor @0 @1)))
699 /* Simplify (X & ~Y) |^+ (~X & Y) -> X ^ Y. */
700 (for op (bit_ior bit_xor plus)
702 (op (bit_and:c @0 (bit_not @1)) (bit_and:c (bit_not @0) @1))
705 (op:c (bit_and @0 INTEGER_CST@2) (bit_and (bit_not @0) INTEGER_CST@1))
706 (if (~wi::to_wide (@2) == wi::to_wide (@1))
709 /* PR53979: Transform ((a ^ b) | a) -> (a | b) */
711 (bit_ior:c (bit_xor:c @0 @1) @0)
714 /* (a & ~b) | (a ^ b) --> a ^ b */
716 (bit_ior:c (bit_and:c @0 (bit_not @1)) (bit_xor:c@2 @0 @1))
719 /* (a & ~b) ^ ~a --> ~(a & b) */
721 (bit_xor:c (bit_and:cs @0 (bit_not @1)) (bit_not @0))
722 (bit_not (bit_and @0 @1)))
724 /* (a | b) & ~(a ^ b) --> a & b */
726 (bit_and:c (bit_ior @0 @1) (bit_not (bit_xor:c @0 @1)))
729 /* a | ~(a ^ b) --> a | ~b */
731 (bit_ior:c @0 (bit_not:s (bit_xor:c @0 @1)))
732 (bit_ior @0 (bit_not @1)))
734 /* (a | b) | (a &^ b) --> a | b */
735 (for op (bit_and bit_xor)
737 (bit_ior:c (bit_ior@2 @0 @1) (op:c @0 @1))
740 /* (a & b) | ~(a ^ b) --> ~(a ^ b) */
742 (bit_ior:c (bit_and:c @0 @1) (bit_not@2 (bit_xor @0 @1)))
745 /* ~(~a & b) --> a | ~b */
747 (bit_not (bit_and:cs (bit_not @0) @1))
748 (bit_ior @0 (bit_not @1)))
750 /* Simplify (~X & Y) to X ^ Y if we know that (X & ~Y) is 0. */
753 (bit_and (bit_not SSA_NAME@0) INTEGER_CST@1)
754 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
755 && wi::bit_and_not (get_nonzero_bits (@0), wi::to_wide (@1)) == 0)
759 /* X % Y is smaller than Y. */
762 (cmp (trunc_mod @0 @1) @1)
763 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
764 { constant_boolean_node (cmp == LT_EXPR, type); })))
767 (cmp @1 (trunc_mod @0 @1))
768 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
769 { constant_boolean_node (cmp == GT_EXPR, type); })))
773 (bit_ior @0 integer_all_onesp@1)
778 (bit_ior @0 integer_zerop)
783 (bit_and @0 integer_zerop@1)
789 (for op (bit_ior bit_xor plus)
791 (op:c (convert? @0) (convert? (bit_not @0)))
792 (convert { build_all_ones_cst (TREE_TYPE (@0)); })))
797 { build_zero_cst (type); })
799 /* Canonicalize X ^ ~0 to ~X. */
801 (bit_xor @0 integer_all_onesp@1)
806 (bit_and @0 integer_all_onesp)
809 /* x & x -> x, x | x -> x */
810 (for bitop (bit_and bit_ior)
815 /* x & C -> x if we know that x & ~C == 0. */
818 (bit_and SSA_NAME@0 INTEGER_CST@1)
819 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
820 && wi::bit_and_not (get_nonzero_bits (@0), wi::to_wide (@1)) == 0)
824 /* x + (x & 1) -> (x + 1) & ~1 */
826 (plus:c @0 (bit_and:s @0 integer_onep@1))
827 (bit_and (plus @0 @1) (bit_not @1)))
829 /* x & ~(x & y) -> x & ~y */
830 /* x | ~(x | y) -> x | ~y */
831 (for bitop (bit_and bit_ior)
833 (bitop:c @0 (bit_not (bitop:cs @0 @1)))
834 (bitop @0 (bit_not @1))))
836 /* (x | y) & ~x -> y & ~x */
837 /* (x & y) | ~x -> y | ~x */
838 (for bitop (bit_and bit_ior)
839 rbitop (bit_ior bit_and)
841 (bitop:c (rbitop:c @0 @1) (bit_not@2 @0))
844 /* (x & y) ^ (x | y) -> x ^ y */
846 (bit_xor:c (bit_and @0 @1) (bit_ior @0 @1))
849 /* (x ^ y) ^ (x | y) -> x & y */
851 (bit_xor:c (bit_xor @0 @1) (bit_ior @0 @1))
854 /* (x & y) + (x ^ y) -> x | y */
855 /* (x & y) | (x ^ y) -> x | y */
856 /* (x & y) ^ (x ^ y) -> x | y */
857 (for op (plus bit_ior bit_xor)
859 (op:c (bit_and @0 @1) (bit_xor @0 @1))
862 /* (x & y) + (x | y) -> x + y */
864 (plus:c (bit_and @0 @1) (bit_ior @0 @1))
867 /* (x + y) - (x | y) -> x & y */
869 (minus (plus @0 @1) (bit_ior @0 @1))
870 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
871 && !TYPE_SATURATING (type))
874 /* (x + y) - (x & y) -> x | y */
876 (minus (plus @0 @1) (bit_and @0 @1))
877 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
878 && !TYPE_SATURATING (type))
881 /* (x | y) - (x ^ y) -> x & y */
883 (minus (bit_ior @0 @1) (bit_xor @0 @1))
886 /* (x | y) - (x & y) -> x ^ y */
888 (minus (bit_ior @0 @1) (bit_and @0 @1))
891 /* (x | y) & ~(x & y) -> x ^ y */
893 (bit_and:c (bit_ior @0 @1) (bit_not (bit_and @0 @1)))
896 /* (x | y) & (~x ^ y) -> x & y */
898 (bit_and:c (bit_ior:c @0 @1) (bit_xor:c @1 (bit_not @0)))
901 /* ~x & ~y -> ~(x | y)
902 ~x | ~y -> ~(x & y) */
903 (for op (bit_and bit_ior)
904 rop (bit_ior bit_and)
906 (op (convert1? (bit_not @0)) (convert2? (bit_not @1)))
907 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
908 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
909 (bit_not (rop (convert @0) (convert @1))))))
911 /* If we are XORing or adding two BIT_AND_EXPR's, both of which are and'ing
912 with a constant, and the two constants have no bits in common,
913 we should treat this as a BIT_IOR_EXPR since this may produce more
915 (for op (bit_xor plus)
917 (op (convert1? (bit_and@4 @0 INTEGER_CST@1))
918 (convert2? (bit_and@5 @2 INTEGER_CST@3)))
919 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
920 && tree_nop_conversion_p (type, TREE_TYPE (@2))
921 && (wi::to_wide (@1) & wi::to_wide (@3)) == 0)
922 (bit_ior (convert @4) (convert @5)))))
924 /* (X | Y) ^ X -> Y & ~ X*/
926 (bit_xor:c (convert1? (bit_ior:c @@0 @1)) (convert2? @0))
927 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
928 (convert (bit_and @1 (bit_not @0)))))
930 /* Convert ~X ^ ~Y to X ^ Y. */
932 (bit_xor (convert1? (bit_not @0)) (convert2? (bit_not @1)))
933 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
934 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
935 (bit_xor (convert @0) (convert @1))))
937 /* Convert ~X ^ C to X ^ ~C. */
939 (bit_xor (convert? (bit_not @0)) INTEGER_CST@1)
940 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
941 (bit_xor (convert @0) (bit_not @1))))
943 /* Fold (X & Y) ^ Y and (X ^ Y) & Y as ~X & Y. */
944 (for opo (bit_and bit_xor)
945 opi (bit_xor bit_and)
947 (opo:c (opi:c @0 @1) @1)
948 (bit_and (bit_not @0) @1)))
950 /* Given a bit-wise operation CODE applied to ARG0 and ARG1, see if both
951 operands are another bit-wise operation with a common input. If so,
952 distribute the bit operations to save an operation and possibly two if
953 constants are involved. For example, convert
954 (A | B) & (A | C) into A | (B & C)
955 Further simplification will occur if B and C are constants. */
956 (for op (bit_and bit_ior bit_xor)
957 rop (bit_ior bit_and bit_and)
959 (op (convert? (rop:c @@0 @1)) (convert? (rop:c @0 @2)))
960 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
961 && tree_nop_conversion_p (type, TREE_TYPE (@2)))
962 (rop (convert @0) (op (convert @1) (convert @2))))))
964 /* Some simple reassociation for bit operations, also handled in reassoc. */
965 /* (X & Y) & Y -> X & Y
966 (X | Y) | Y -> X | Y */
967 (for op (bit_and bit_ior)
969 (op:c (convert1?@2 (op:c @0 @@1)) (convert2? @1))
971 /* (X ^ Y) ^ Y -> X */
973 (bit_xor:c (convert1? (bit_xor:c @0 @@1)) (convert2? @1))
975 /* (X & Y) & (X & Z) -> (X & Y) & Z
976 (X | Y) | (X | Z) -> (X | Y) | Z */
977 (for op (bit_and bit_ior)
979 (op (convert1?@3 (op:c@4 @0 @1)) (convert2?@5 (op:c@6 @0 @2)))
980 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
981 && tree_nop_conversion_p (type, TREE_TYPE (@2)))
982 (if (single_use (@5) && single_use (@6))
984 (if (single_use (@3) && single_use (@4))
985 (op (convert @1) @5))))))
986 /* (X ^ Y) ^ (X ^ Z) -> Y ^ Z */
988 (bit_xor (convert1? (bit_xor:c @0 @1)) (convert2? (bit_xor:c @0 @2)))
989 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
990 && tree_nop_conversion_p (type, TREE_TYPE (@2)))
991 (bit_xor (convert @1) (convert @2))))
1000 (abs tree_expr_nonnegative_p@0)
1003 /* A few cases of fold-const.c negate_expr_p predicate. */
1004 (match negate_expr_p
1006 (if ((INTEGRAL_TYPE_P (type)
1007 && TYPE_UNSIGNED (type))
1008 || (!TYPE_OVERFLOW_SANITIZED (type)
1009 && may_negate_without_overflow_p (t)))))
1010 (match negate_expr_p
1012 (match negate_expr_p
1014 (if (!TYPE_OVERFLOW_SANITIZED (type))))
1015 (match negate_expr_p
1017 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (t)))))
1018 /* VECTOR_CST handling of non-wrapping types would recurse in unsupported
1020 (match negate_expr_p
1022 (if (FLOAT_TYPE_P (TREE_TYPE (type)) || TYPE_OVERFLOW_WRAPS (type))))
1023 (match negate_expr_p
1025 (if ((ANY_INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_WRAPS (type))
1026 || (FLOAT_TYPE_P (type)
1027 && !HONOR_SIGN_DEPENDENT_ROUNDING (type)
1028 && !HONOR_SIGNED_ZEROS (type)))))
1030 /* (-A) * (-B) -> A * B */
1032 (mult:c (convert1? (negate @0)) (convert2? negate_expr_p@1))
1033 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
1034 && tree_nop_conversion_p (type, TREE_TYPE (@1)))
1035 (mult (convert @0) (convert (negate @1)))))
1037 /* -(A + B) -> (-B) - A. */
1039 (negate (plus:c @0 negate_expr_p@1))
1040 (if (!HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type))
1041 && !HONOR_SIGNED_ZEROS (element_mode (type)))
1042 (minus (negate @1) @0)))
1044 /* -(A - B) -> B - A. */
1046 (negate (minus @0 @1))
1047 (if ((ANY_INTEGRAL_TYPE_P (type) && !TYPE_OVERFLOW_SANITIZED (type))
1048 || (FLOAT_TYPE_P (type)
1049 && !HONOR_SIGN_DEPENDENT_ROUNDING (type)
1050 && !HONOR_SIGNED_ZEROS (type)))
1053 (negate (pointer_diff @0 @1))
1054 (if (TYPE_OVERFLOW_UNDEFINED (type))
1055 (pointer_diff @1 @0)))
1057 /* A - B -> A + (-B) if B is easily negatable. */
1059 (minus @0 negate_expr_p@1)
1060 (if (!FIXED_POINT_TYPE_P (type))
1061 (plus @0 (negate @1))))
1063 /* Try to fold (type) X op CST -> (type) (X op ((type-x) CST))
1065 For bitwise binary operations apply operand conversions to the
1066 binary operation result instead of to the operands. This allows
1067 to combine successive conversions and bitwise binary operations.
1068 We combine the above two cases by using a conditional convert. */
1069 (for bitop (bit_and bit_ior bit_xor)
1071 (bitop (convert @0) (convert? @1))
1072 (if (((TREE_CODE (@1) == INTEGER_CST
1073 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
1074 && int_fits_type_p (@1, TREE_TYPE (@0)))
1075 || types_match (@0, @1))
1076 /* ??? This transform conflicts with fold-const.c doing
1077 Convert (T)(x & c) into (T)x & (T)c, if c is an integer
1078 constants (if x has signed type, the sign bit cannot be set
1079 in c). This folds extension into the BIT_AND_EXPR.
1080 Restrict it to GIMPLE to avoid endless recursions. */
1081 && (bitop != BIT_AND_EXPR || GIMPLE)
1082 && (/* That's a good idea if the conversion widens the operand, thus
1083 after hoisting the conversion the operation will be narrower. */
1084 TYPE_PRECISION (TREE_TYPE (@0)) < TYPE_PRECISION (type)
1085 /* It's also a good idea if the conversion is to a non-integer
1087 || GET_MODE_CLASS (TYPE_MODE (type)) != MODE_INT
1088 /* Or if the precision of TO is not the same as the precision
1090 || !type_has_mode_precision_p (type)))
1091 (convert (bitop @0 (convert @1))))))
1093 (for bitop (bit_and bit_ior)
1094 rbitop (bit_ior bit_and)
1095 /* (x | y) & x -> x */
1096 /* (x & y) | x -> x */
1098 (bitop:c (rbitop:c @0 @1) @0)
1100 /* (~x | y) & x -> x & y */
1101 /* (~x & y) | x -> x | y */
1103 (bitop:c (rbitop:c (bit_not @0) @1) @0)
1106 /* (x | CST1) & CST2 -> (x & CST2) | (CST1 & CST2) */
1108 (bit_and (bit_ior @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
1109 (bit_ior (bit_and @0 @2) (bit_and @1 @2)))
1111 /* Combine successive equal operations with constants. */
1112 (for bitop (bit_and bit_ior bit_xor)
1114 (bitop (bitop @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
1115 (if (!CONSTANT_CLASS_P (@0))
1116 /* This is the canonical form regardless of whether (bitop @1 @2) can be
1117 folded to a constant. */
1118 (bitop @0 (bitop @1 @2))
1119 /* In this case we have three constants and (bitop @0 @1) doesn't fold
1120 to a constant. This can happen if @0 or @1 is a POLY_INT_CST and if
1121 the values involved are such that the operation can't be decided at
1122 compile time. Try folding one of @0 or @1 with @2 to see whether
1123 that combination can be decided at compile time.
1125 Keep the existing form if both folds fail, to avoid endless
1127 (with { tree cst1 = const_binop (bitop, type, @0, @2); }
1129 (bitop @1 { cst1; })
1130 (with { tree cst2 = const_binop (bitop, type, @1, @2); }
1132 (bitop @0 { cst2; }))))))))
1134 /* Try simple folding for X op !X, and X op X with the help
1135 of the truth_valued_p and logical_inverted_value predicates. */
1136 (match truth_valued_p
1138 (if (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1)))
1139 (for op (tcc_comparison truth_and truth_andif truth_or truth_orif truth_xor)
1140 (match truth_valued_p
1142 (match truth_valued_p
1145 (match (logical_inverted_value @0)
1147 (match (logical_inverted_value @0)
1148 (bit_not truth_valued_p@0))
1149 (match (logical_inverted_value @0)
1150 (eq @0 integer_zerop))
1151 (match (logical_inverted_value @0)
1152 (ne truth_valued_p@0 integer_truep))
1153 (match (logical_inverted_value @0)
1154 (bit_xor truth_valued_p@0 integer_truep))
1158 (bit_and:c @0 (logical_inverted_value @0))
1159 { build_zero_cst (type); })
1160 /* X | !X and X ^ !X -> 1, , if X is truth-valued. */
1161 (for op (bit_ior bit_xor)
1163 (op:c truth_valued_p@0 (logical_inverted_value @0))
1164 { constant_boolean_node (true, type); }))
1165 /* X ==/!= !X is false/true. */
1168 (op:c truth_valued_p@0 (logical_inverted_value @0))
1169 { constant_boolean_node (op == NE_EXPR ? true : false, type); }))
1173 (bit_not (bit_not @0))
1176 /* Convert ~ (-A) to A - 1. */
1178 (bit_not (convert? (negate @0)))
1179 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1180 || !TYPE_UNSIGNED (TREE_TYPE (@0)))
1181 (convert (minus @0 { build_each_one_cst (TREE_TYPE (@0)); }))))
1183 /* Convert - (~A) to A + 1. */
1185 (negate (nop_convert (bit_not @0)))
1186 (plus (view_convert @0) { build_each_one_cst (type); }))
1188 /* Convert ~ (A - 1) or ~ (A + -1) to -A. */
1190 (bit_not (convert? (minus @0 integer_each_onep)))
1191 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1192 || !TYPE_UNSIGNED (TREE_TYPE (@0)))
1193 (convert (negate @0))))
1195 (bit_not (convert? (plus @0 integer_all_onesp)))
1196 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1197 || !TYPE_UNSIGNED (TREE_TYPE (@0)))
1198 (convert (negate @0))))
1200 /* Part of convert ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify. */
1202 (bit_not (convert? (bit_xor @0 INTEGER_CST@1)))
1203 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1204 (convert (bit_xor @0 (bit_not @1)))))
1206 (bit_not (convert? (bit_xor:c (bit_not @0) @1)))
1207 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1208 (convert (bit_xor @0 @1))))
1210 /* Otherwise prefer ~(X ^ Y) to ~X ^ Y as more canonical. */
1212 (bit_xor:c (nop_convert:s (bit_not:s @0)) @1)
1213 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1214 (bit_not (bit_xor (view_convert @0) @1))))
1216 /* (x & ~m) | (y & m) -> ((x ^ y) & m) ^ x */
1218 (bit_ior:c (bit_and:cs @0 (bit_not @2)) (bit_and:cs @1 @2))
1219 (bit_xor (bit_and (bit_xor @0 @1) @2) @0))
1221 /* Fold A - (A & B) into ~B & A. */
1223 (minus (convert1? @0) (convert2?:s (bit_and:cs @@0 @1)))
1224 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
1225 && tree_nop_conversion_p (type, TREE_TYPE (@1)))
1226 (convert (bit_and (bit_not @1) @0))))
1228 /* (m1 CMP m2) * d -> (m1 CMP m2) ? d : 0 */
1229 (for cmp (gt lt ge le)
1231 (mult (convert (cmp @0 @1)) @2)
1232 (cond (cmp @0 @1) @2 { build_zero_cst (type); })))
1234 /* For integral types with undefined overflow and C != 0 fold
1235 x * C EQ/NE y * C into x EQ/NE y. */
1238 (cmp (mult:c @0 @1) (mult:c @2 @1))
1239 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
1240 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1241 && tree_expr_nonzero_p (@1))
1244 /* For integral types with wrapping overflow and C odd fold
1245 x * C EQ/NE y * C into x EQ/NE y. */
1248 (cmp (mult @0 INTEGER_CST@1) (mult @2 @1))
1249 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
1250 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))
1251 && (TREE_INT_CST_LOW (@1) & 1) != 0)
1254 /* For integral types with undefined overflow and C != 0 fold
1255 x * C RELOP y * C into:
1257 x RELOP y for nonnegative C
1258 y RELOP x for negative C */
1259 (for cmp (lt gt le ge)
1261 (cmp (mult:c @0 @1) (mult:c @2 @1))
1262 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
1263 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1264 (if (tree_expr_nonnegative_p (@1) && tree_expr_nonzero_p (@1))
1266 (if (TREE_CODE (@1) == INTEGER_CST
1267 && wi::neg_p (wi::to_wide (@1), TYPE_SIGN (TREE_TYPE (@1))))
1270 /* (X - 1U) <= INT_MAX-1U into (int) X > 0. */
1274 (cmp (plus @0 integer_minus_onep@1) INTEGER_CST@2)
1275 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1276 && TYPE_UNSIGNED (TREE_TYPE (@0))
1277 && TYPE_PRECISION (TREE_TYPE (@0)) > 1
1278 && (wi::to_wide (@2)
1279 == wi::max_value (TYPE_PRECISION (TREE_TYPE (@0)), SIGNED) - 1))
1280 (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
1281 (icmp (convert:stype @0) { build_int_cst (stype, 0); })))))
1283 /* X / 4 < Y / 4 iff X < Y when the division is known to be exact. */
1284 (for cmp (simple_comparison)
1286 (cmp (exact_div @0 INTEGER_CST@2) (exact_div @1 @2))
1287 (if (wi::gt_p (wi::to_wide (@2), 0, TYPE_SIGN (TREE_TYPE (@2))))
1290 /* X / C1 op C2 into a simple range test. */
1291 (for cmp (simple_comparison)
1293 (cmp (trunc_div:s @0 INTEGER_CST@1) INTEGER_CST@2)
1294 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1295 && integer_nonzerop (@1)
1296 && !TREE_OVERFLOW (@1)
1297 && !TREE_OVERFLOW (@2))
1298 (with { tree lo, hi; bool neg_overflow;
1299 enum tree_code code = fold_div_compare (cmp, @1, @2, &lo, &hi,
1302 (if (code == LT_EXPR || code == GE_EXPR)
1303 (if (TREE_OVERFLOW (lo))
1304 { build_int_cst (type, (code == LT_EXPR) ^ neg_overflow); }
1305 (if (code == LT_EXPR)
1308 (if (code == LE_EXPR || code == GT_EXPR)
1309 (if (TREE_OVERFLOW (hi))
1310 { build_int_cst (type, (code == LE_EXPR) ^ neg_overflow); }
1311 (if (code == LE_EXPR)
1315 { build_int_cst (type, code == NE_EXPR); })
1316 (if (code == EQ_EXPR && !hi)
1318 (if (code == EQ_EXPR && !lo)
1320 (if (code == NE_EXPR && !hi)
1322 (if (code == NE_EXPR && !lo)
1325 { build_range_check (UNKNOWN_LOCATION, type, @0, code == EQ_EXPR,
1329 tree etype = range_check_type (TREE_TYPE (@0));
1332 if (! TYPE_UNSIGNED (etype))
1333 etype = unsigned_type_for (etype);
1334 hi = fold_convert (etype, hi);
1335 lo = fold_convert (etype, lo);
1336 hi = const_binop (MINUS_EXPR, etype, hi, lo);
1339 (if (etype && hi && !TREE_OVERFLOW (hi))
1340 (if (code == EQ_EXPR)
1341 (le (minus (convert:etype @0) { lo; }) { hi; })
1342 (gt (minus (convert:etype @0) { lo; }) { hi; })))))))))
1344 /* X + Z < Y + Z is the same as X < Y when there is no overflow. */
1345 (for op (lt le ge gt)
1347 (op (plus:c @0 @2) (plus:c @1 @2))
1348 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1349 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1351 /* For equality and subtraction, this is also true with wrapping overflow. */
1352 (for op (eq ne minus)
1354 (op (plus:c @0 @2) (plus:c @1 @2))
1355 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1356 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1357 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
1360 /* X - Z < Y - Z is the same as X < Y when there is no overflow. */
1361 (for op (lt le ge gt)
1363 (op (minus @0 @2) (minus @1 @2))
1364 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1365 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1367 /* For equality and subtraction, this is also true with wrapping overflow. */
1368 (for op (eq ne minus)
1370 (op (minus @0 @2) (minus @1 @2))
1371 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1372 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1373 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
1375 /* And for pointers... */
1376 (for op (simple_comparison)
1378 (op (pointer_diff@3 @0 @2) (pointer_diff @1 @2))
1379 (if (!TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
1382 (minus (pointer_diff@3 @0 @2) (pointer_diff @1 @2))
1383 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@3))
1384 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
1385 (pointer_diff @0 @1)))
1387 /* Z - X < Z - Y is the same as Y < X when there is no overflow. */
1388 (for op (lt le ge gt)
1390 (op (minus @2 @0) (minus @2 @1))
1391 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1392 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1394 /* For equality and subtraction, this is also true with wrapping overflow. */
1395 (for op (eq ne minus)
1397 (op (minus @2 @0) (minus @2 @1))
1398 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1399 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1400 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
1402 /* And for pointers... */
1403 (for op (simple_comparison)
1405 (op (pointer_diff@3 @2 @0) (pointer_diff @2 @1))
1406 (if (!TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
1409 (minus (pointer_diff@3 @2 @0) (pointer_diff @2 @1))
1410 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@3))
1411 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
1412 (pointer_diff @1 @0)))
1414 /* X + Y < Y is the same as X < 0 when there is no overflow. */
1415 (for op (lt le gt ge)
1417 (op:c (plus:c@2 @0 @1) @1)
1418 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1419 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1420 && (CONSTANT_CLASS_P (@0) || single_use (@2)))
1421 (op @0 { build_zero_cst (TREE_TYPE (@0)); }))))
1422 /* For equality, this is also true with wrapping overflow. */
1425 (op:c (nop_convert@3 (plus:c@2 @0 (convert1? @1))) (convert2? @1))
1426 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1427 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1428 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
1429 && (CONSTANT_CLASS_P (@0) || (single_use (@2) && single_use (@3)))
1430 && tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@2))
1431 && tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@1)))
1432 (op @0 { build_zero_cst (TREE_TYPE (@0)); })))
1434 (op:c (nop_convert@3 (pointer_plus@2 (convert1? @0) @1)) (convert2? @0))
1435 (if (tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@0))
1436 && tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0))
1437 && (CONSTANT_CLASS_P (@1) || (single_use (@2) && single_use (@3))))
1438 (op @1 { build_zero_cst (TREE_TYPE (@1)); }))))
1440 /* X - Y < X is the same as Y > 0 when there is no overflow.
1441 For equality, this is also true with wrapping overflow. */
1442 (for op (simple_comparison)
1444 (op:c @0 (minus@2 @0 @1))
1445 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1446 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1447 || ((op == EQ_EXPR || op == NE_EXPR)
1448 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
1449 && (CONSTANT_CLASS_P (@1) || single_use (@2)))
1450 (op @1 { build_zero_cst (TREE_TYPE (@1)); }))))
1453 * (X / Y) == 0 -> X < Y if X, Y are unsigned.
1454 * (X / Y) != 0 -> X >= Y, if X, Y are unsigned.
1459 (cmp (trunc_div @0 @1) integer_zerop)
1460 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
1461 && (VECTOR_TYPE_P (type) || !VECTOR_TYPE_P (TREE_TYPE (@0))))
1464 /* X == C - X can never be true if C is odd. */
1467 (cmp:c (convert? @0) (convert1? (minus INTEGER_CST@1 (convert2? @0))))
1468 (if (TREE_INT_CST_LOW (@1) & 1)
1469 { constant_boolean_node (cmp == NE_EXPR, type); })))
1471 /* Arguments on which one can call get_nonzero_bits to get the bits
1473 (match with_possible_nonzero_bits
1475 (match with_possible_nonzero_bits
1477 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))))
1478 /* Slightly extended version, do not make it recursive to keep it cheap. */
1479 (match (with_possible_nonzero_bits2 @0)
1480 with_possible_nonzero_bits@0)
1481 (match (with_possible_nonzero_bits2 @0)
1482 (bit_and:c with_possible_nonzero_bits@0 @2))
1484 /* Same for bits that are known to be set, but we do not have
1485 an equivalent to get_nonzero_bits yet. */
1486 (match (with_certain_nonzero_bits2 @0)
1488 (match (with_certain_nonzero_bits2 @0)
1489 (bit_ior @1 INTEGER_CST@0))
1491 /* X == C (or X & Z == Y | C) is impossible if ~nonzero(X) & C != 0. */
1494 (cmp:c (with_possible_nonzero_bits2 @0) (with_certain_nonzero_bits2 @1))
1495 (if (wi::bit_and_not (wi::to_wide (@1), get_nonzero_bits (@0)) != 0)
1496 { constant_boolean_node (cmp == NE_EXPR, type); })))
1498 /* ((X inner_op C0) outer_op C1)
1499 With X being a tree where value_range has reasoned certain bits to always be
1500 zero throughout its computed value range,
1501 inner_op = {|,^}, outer_op = {|,^} and inner_op != outer_op
1502 where zero_mask has 1's for all bits that are sure to be 0 in
1504 if (inner_op == '^') C0 &= ~C1;
1505 if ((C0 & ~zero_mask) == 0) then emit (X outer_op (C0 outer_op C1)
1506 if ((C1 & ~zero_mask) == 0) then emit (X inner_op (C0 outer_op C1)
1508 (for inner_op (bit_ior bit_xor)
1509 outer_op (bit_xor bit_ior)
1512 (inner_op:s @2 INTEGER_CST@0) INTEGER_CST@1)
1516 wide_int zero_mask_not;
1520 if (TREE_CODE (@2) == SSA_NAME)
1521 zero_mask_not = get_nonzero_bits (@2);
1525 if (inner_op == BIT_XOR_EXPR)
1527 C0 = wi::bit_and_not (wi::to_wide (@0), wi::to_wide (@1));
1528 cst_emit = C0 | wi::to_wide (@1);
1532 C0 = wi::to_wide (@0);
1533 cst_emit = C0 ^ wi::to_wide (@1);
1536 (if (!fail && (C0 & zero_mask_not) == 0)
1537 (outer_op @2 { wide_int_to_tree (type, cst_emit); })
1538 (if (!fail && (wi::to_wide (@1) & zero_mask_not) == 0)
1539 (inner_op @2 { wide_int_to_tree (type, cst_emit); }))))))
1541 /* Associate (p +p off1) +p off2 as (p +p (off1 + off2)). */
1543 (pointer_plus (pointer_plus:s @0 @1) @3)
1544 (pointer_plus @0 (plus @1 @3)))
1550 tem4 = (unsigned long) tem3;
1555 (pointer_plus @0 (convert?@2 (minus@3 (convert @1) (convert @0))))
1556 /* Conditionally look through a sign-changing conversion. */
1557 (if (TYPE_PRECISION (TREE_TYPE (@2)) == TYPE_PRECISION (TREE_TYPE (@3))
1558 && ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@1)))
1559 || (GENERIC && type == TREE_TYPE (@1))))
1562 (pointer_plus @0 (convert?@2 (pointer_diff@3 @1 @@0)))
1563 (if (TYPE_PRECISION (TREE_TYPE (@2)) >= TYPE_PRECISION (TREE_TYPE (@3)))
1567 tem = (sizetype) ptr;
1571 and produce the simpler and easier to analyze with respect to alignment
1572 ... = ptr & ~algn; */
1574 (pointer_plus @0 (negate (bit_and (convert @0) INTEGER_CST@1)))
1575 (with { tree algn = wide_int_to_tree (TREE_TYPE (@0), ~wi::to_wide (@1)); }
1576 (bit_and @0 { algn; })))
1578 /* Try folding difference of addresses. */
1580 (minus (convert ADDR_EXPR@0) (convert @1))
1581 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1582 (with { poly_int64 diff; }
1583 (if (ptr_difference_const (@0, @1, &diff))
1584 { build_int_cst_type (type, diff); }))))
1586 (minus (convert @0) (convert ADDR_EXPR@1))
1587 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1588 (with { poly_int64 diff; }
1589 (if (ptr_difference_const (@0, @1, &diff))
1590 { build_int_cst_type (type, diff); }))))
1592 (pointer_diff (convert?@2 ADDR_EXPR@0) (convert?@3 @1))
1593 (if (tree_nop_conversion_p (TREE_TYPE(@2), TREE_TYPE (@0))
1594 && tree_nop_conversion_p (TREE_TYPE(@3), TREE_TYPE (@1)))
1595 (with { poly_int64 diff; }
1596 (if (ptr_difference_const (@0, @1, &diff))
1597 { build_int_cst_type (type, diff); }))))
1599 (pointer_diff (convert?@2 @0) (convert?@3 ADDR_EXPR@1))
1600 (if (tree_nop_conversion_p (TREE_TYPE(@2), TREE_TYPE (@0))
1601 && tree_nop_conversion_p (TREE_TYPE(@3), TREE_TYPE (@1)))
1602 (with { poly_int64 diff; }
1603 (if (ptr_difference_const (@0, @1, &diff))
1604 { build_int_cst_type (type, diff); }))))
1606 /* If arg0 is derived from the address of an object or function, we may
1607 be able to fold this expression using the object or function's
1610 (bit_and (convert? @0) INTEGER_CST@1)
1611 (if (POINTER_TYPE_P (TREE_TYPE (@0))
1612 && tree_nop_conversion_p (type, TREE_TYPE (@0)))
1616 unsigned HOST_WIDE_INT bitpos;
1617 get_pointer_alignment_1 (@0, &align, &bitpos);
1619 (if (wi::ltu_p (wi::to_wide (@1), align / BITS_PER_UNIT))
1620 { wide_int_to_tree (type, (wi::to_wide (@1)
1621 & (bitpos / BITS_PER_UNIT))); }))))
1624 /* We can't reassociate at all for saturating types. */
1625 (if (!TYPE_SATURATING (type))
1627 /* Contract negates. */
1628 /* A + (-B) -> A - B */
1630 (plus:c @0 (convert? (negate @1)))
1631 /* Apply STRIP_NOPS on the negate. */
1632 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1633 && !TYPE_OVERFLOW_SANITIZED (type))
1637 if (INTEGRAL_TYPE_P (type)
1638 && TYPE_OVERFLOW_WRAPS (type) != TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
1639 t1 = TYPE_OVERFLOW_WRAPS (type) ? type : TREE_TYPE (@1);
1641 (convert (minus (convert:t1 @0) (convert:t1 @1))))))
1642 /* A - (-B) -> A + B */
1644 (minus @0 (convert? (negate @1)))
1645 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1646 && !TYPE_OVERFLOW_SANITIZED (type))
1650 if (INTEGRAL_TYPE_P (type)
1651 && TYPE_OVERFLOW_WRAPS (type) != TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
1652 t1 = TYPE_OVERFLOW_WRAPS (type) ? type : TREE_TYPE (@1);
1654 (convert (plus (convert:t1 @0) (convert:t1 @1))))))
1656 Sign-extension is ok except for INT_MIN, which thankfully cannot
1657 happen without overflow. */
1659 (negate (convert (negate @1)))
1660 (if (INTEGRAL_TYPE_P (type)
1661 && (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@1))
1662 || (!TYPE_UNSIGNED (TREE_TYPE (@1))
1663 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))))
1664 && !TYPE_OVERFLOW_SANITIZED (type)
1665 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@1)))
1668 (negate (convert negate_expr_p@1))
1669 (if (SCALAR_FLOAT_TYPE_P (type)
1670 && ((DECIMAL_FLOAT_TYPE_P (type)
1671 == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1))
1672 && TYPE_PRECISION (type) >= TYPE_PRECISION (TREE_TYPE (@1)))
1673 || !HONOR_SIGN_DEPENDENT_ROUNDING (type)))
1674 (convert (negate @1))))
1676 (negate (nop_convert (negate @1)))
1677 (if (!TYPE_OVERFLOW_SANITIZED (type)
1678 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@1)))
1681 /* We can't reassociate floating-point unless -fassociative-math
1682 or fixed-point plus or minus because of saturation to +-Inf. */
1683 (if ((!FLOAT_TYPE_P (type) || flag_associative_math)
1684 && !FIXED_POINT_TYPE_P (type))
1686 /* Match patterns that allow contracting a plus-minus pair
1687 irrespective of overflow issues. */
1688 /* (A +- B) - A -> +- B */
1689 /* (A +- B) -+ B -> A */
1690 /* A - (A +- B) -> -+ B */
1691 /* A +- (B -+ A) -> +- B */
1693 (minus (plus:c @0 @1) @0)
1696 (minus (minus @0 @1) @0)
1699 (plus:c (minus @0 @1) @1)
1702 (minus @0 (plus:c @0 @1))
1705 (minus @0 (minus @0 @1))
1707 /* (A +- B) + (C - A) -> C +- B */
1708 /* (A + B) - (A - C) -> B + C */
1709 /* More cases are handled with comparisons. */
1711 (plus:c (plus:c @0 @1) (minus @2 @0))
1714 (plus:c (minus @0 @1) (minus @2 @0))
1717 (plus:c (pointer_diff @0 @1) (pointer_diff @2 @0))
1718 (if (TYPE_OVERFLOW_UNDEFINED (type)
1719 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0)))
1720 (pointer_diff @2 @1)))
1722 (minus (plus:c @0 @1) (minus @0 @2))
1725 /* (A +- CST1) +- CST2 -> A + CST3
1726 Use view_convert because it is safe for vectors and equivalent for
1728 (for outer_op (plus minus)
1729 (for inner_op (plus minus)
1730 neg_inner_op (minus plus)
1732 (outer_op (nop_convert (inner_op @0 CONSTANT_CLASS_P@1))
1734 /* If one of the types wraps, use that one. */
1735 (if (!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_WRAPS (type))
1736 /* If all 3 captures are CONSTANT_CLASS_P, punt, as we might recurse
1737 forever if something doesn't simplify into a constant. */
1738 (if (!CONSTANT_CLASS_P (@0))
1739 (if (outer_op == PLUS_EXPR)
1740 (plus (view_convert @0) (inner_op @2 (view_convert @1)))
1741 (minus (view_convert @0) (neg_inner_op @2 (view_convert @1)))))
1742 (if (!ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1743 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
1744 (if (outer_op == PLUS_EXPR)
1745 (view_convert (plus @0 (inner_op (view_convert @2) @1)))
1746 (view_convert (minus @0 (neg_inner_op (view_convert @2) @1))))
1747 /* If the constant operation overflows we cannot do the transform
1748 directly as we would introduce undefined overflow, for example
1749 with (a - 1) + INT_MIN. */
1750 (if (types_match (type, @0))
1751 (with { tree cst = const_binop (outer_op == inner_op
1752 ? PLUS_EXPR : MINUS_EXPR,
1754 (if (cst && !TREE_OVERFLOW (cst))
1755 (inner_op @0 { cst; } )
1756 /* X+INT_MAX+1 is X-INT_MIN. */
1757 (if (INTEGRAL_TYPE_P (type) && cst
1758 && wi::to_wide (cst) == wi::min_value (type))
1759 (neg_inner_op @0 { wide_int_to_tree (type, wi::to_wide (cst)); })
1760 /* Last resort, use some unsigned type. */
1761 (with { tree utype = unsigned_type_for (type); }
1762 (view_convert (inner_op
1763 (view_convert:utype @0)
1765 { drop_tree_overflow (cst); })))))))))))))
1767 /* (CST1 - A) +- CST2 -> CST3 - A */
1768 (for outer_op (plus minus)
1770 (outer_op (minus CONSTANT_CLASS_P@1 @0) CONSTANT_CLASS_P@2)
1771 (with { tree cst = const_binop (outer_op, type, @1, @2); }
1772 (if (cst && !TREE_OVERFLOW (cst))
1773 (minus { cst; } @0)))))
1775 /* CST1 - (CST2 - A) -> CST3 + A */
1777 (minus CONSTANT_CLASS_P@1 (minus CONSTANT_CLASS_P@2 @0))
1778 (with { tree cst = const_binop (MINUS_EXPR, type, @1, @2); }
1779 (if (cst && !TREE_OVERFLOW (cst))
1780 (plus { cst; } @0))))
1784 (plus:c (bit_not @0) @0)
1785 (if (!TYPE_OVERFLOW_TRAPS (type))
1786 { build_all_ones_cst (type); }))
1790 (plus (convert? (bit_not @0)) integer_each_onep)
1791 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1792 (negate (convert @0))))
1796 (minus (convert? (negate @0)) integer_each_onep)
1797 (if (!TYPE_OVERFLOW_TRAPS (type)
1798 && tree_nop_conversion_p (type, TREE_TYPE (@0)))
1799 (bit_not (convert @0))))
1803 (minus integer_all_onesp @0)
1806 /* (T)(P + A) - (T)P -> (T) A */
1808 (minus (convert (plus:c @@0 @1))
1810 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
1811 /* For integer types, if A has a smaller type
1812 than T the result depends on the possible
1814 E.g. T=size_t, A=(unsigned)429497295, P>0.
1815 However, if an overflow in P + A would cause
1816 undefined behavior, we can assume that there
1818 || (INTEGRAL_TYPE_P (TREE_TYPE (@1))
1819 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))))
1822 (minus (convert (pointer_plus @@0 @1))
1824 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
1825 /* For pointer types, if the conversion of A to the
1826 final type requires a sign- or zero-extension,
1827 then we have to punt - it is not defined which
1829 || (POINTER_TYPE_P (TREE_TYPE (@0))
1830 && TREE_CODE (@1) == INTEGER_CST
1831 && tree_int_cst_sign_bit (@1) == 0))
1834 (pointer_diff (pointer_plus @@0 @1) @0)
1835 /* The second argument of pointer_plus must be interpreted as signed, and
1836 thus sign-extended if necessary. */
1837 (with { tree stype = signed_type_for (TREE_TYPE (@1)); }
1838 /* Use view_convert instead of convert here, as POINTER_PLUS_EXPR
1839 second arg is unsigned even when we need to consider it as signed,
1840 we don't want to diagnose overflow here. */
1841 (convert (view_convert:stype @1))))
1843 /* (T)P - (T)(P + A) -> -(T) A */
1845 (minus (convert? @0)
1846 (convert (plus:c @@0 @1)))
1847 (if (INTEGRAL_TYPE_P (type)
1848 && TYPE_OVERFLOW_UNDEFINED (type)
1849 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
1850 (with { tree utype = unsigned_type_for (type); }
1851 (convert (negate (convert:utype @1))))
1852 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
1853 /* For integer types, if A has a smaller type
1854 than T the result depends on the possible
1856 E.g. T=size_t, A=(unsigned)429497295, P>0.
1857 However, if an overflow in P + A would cause
1858 undefined behavior, we can assume that there
1860 || (INTEGRAL_TYPE_P (TREE_TYPE (@1))
1861 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))))
1862 (negate (convert @1)))))
1865 (convert (pointer_plus @@0 @1)))
1866 (if (INTEGRAL_TYPE_P (type)
1867 && TYPE_OVERFLOW_UNDEFINED (type)
1868 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
1869 (with { tree utype = unsigned_type_for (type); }
1870 (convert (negate (convert:utype @1))))
1871 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
1872 /* For pointer types, if the conversion of A to the
1873 final type requires a sign- or zero-extension,
1874 then we have to punt - it is not defined which
1876 || (POINTER_TYPE_P (TREE_TYPE (@0))
1877 && TREE_CODE (@1) == INTEGER_CST
1878 && tree_int_cst_sign_bit (@1) == 0))
1879 (negate (convert @1)))))
1881 (pointer_diff @0 (pointer_plus @@0 @1))
1882 /* The second argument of pointer_plus must be interpreted as signed, and
1883 thus sign-extended if necessary. */
1884 (with { tree stype = signed_type_for (TREE_TYPE (@1)); }
1885 /* Use view_convert instead of convert here, as POINTER_PLUS_EXPR
1886 second arg is unsigned even when we need to consider it as signed,
1887 we don't want to diagnose overflow here. */
1888 (negate (convert (view_convert:stype @1)))))
1890 /* (T)(P + A) - (T)(P + B) -> (T)A - (T)B */
1892 (minus (convert (plus:c @@0 @1))
1893 (convert (plus:c @0 @2)))
1894 (if (INTEGRAL_TYPE_P (type)
1895 && TYPE_OVERFLOW_UNDEFINED (type)
1896 && element_precision (type) <= element_precision (TREE_TYPE (@1))
1897 && element_precision (type) <= element_precision (TREE_TYPE (@2)))
1898 (with { tree utype = unsigned_type_for (type); }
1899 (convert (minus (convert:utype @1) (convert:utype @2))))
1900 (if (((element_precision (type) <= element_precision (TREE_TYPE (@1)))
1901 == (element_precision (type) <= element_precision (TREE_TYPE (@2))))
1902 && (element_precision (type) <= element_precision (TREE_TYPE (@1))
1903 /* For integer types, if A has a smaller type
1904 than T the result depends on the possible
1906 E.g. T=size_t, A=(unsigned)429497295, P>0.
1907 However, if an overflow in P + A would cause
1908 undefined behavior, we can assume that there
1910 || (INTEGRAL_TYPE_P (TREE_TYPE (@1))
1911 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
1912 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))
1913 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@2)))))
1914 (minus (convert @1) (convert @2)))))
1916 (minus (convert (pointer_plus @@0 @1))
1917 (convert (pointer_plus @0 @2)))
1918 (if (INTEGRAL_TYPE_P (type)
1919 && TYPE_OVERFLOW_UNDEFINED (type)
1920 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
1921 (with { tree utype = unsigned_type_for (type); }
1922 (convert (minus (convert:utype @1) (convert:utype @2))))
1923 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
1924 /* For pointer types, if the conversion of A to the
1925 final type requires a sign- or zero-extension,
1926 then we have to punt - it is not defined which
1928 || (POINTER_TYPE_P (TREE_TYPE (@0))
1929 && TREE_CODE (@1) == INTEGER_CST
1930 && tree_int_cst_sign_bit (@1) == 0
1931 && TREE_CODE (@2) == INTEGER_CST
1932 && tree_int_cst_sign_bit (@2) == 0))
1933 (minus (convert @1) (convert @2)))))
1935 (pointer_diff (pointer_plus @@0 @1) (pointer_plus @0 @2))
1936 /* The second argument of pointer_plus must be interpreted as signed, and
1937 thus sign-extended if necessary. */
1938 (with { tree stype = signed_type_for (TREE_TYPE (@1)); }
1939 /* Use view_convert instead of convert here, as POINTER_PLUS_EXPR
1940 second arg is unsigned even when we need to consider it as signed,
1941 we don't want to diagnose overflow here. */
1942 (minus (convert (view_convert:stype @1))
1943 (convert (view_convert:stype @2)))))))
1945 /* (A * C) +- (B * C) -> (A+-B) * C and (A * C) +- A -> A * (C+-1).
1946 Modeled after fold_plusminus_mult_expr. */
1947 (if (!TYPE_SATURATING (type)
1948 && (!FLOAT_TYPE_P (type) || flag_associative_math))
1949 (for plusminus (plus minus)
1951 (plusminus (mult:cs @0 @1) (mult:cs @0 @2))
1952 (if (!ANY_INTEGRAL_TYPE_P (type)
1953 || TYPE_OVERFLOW_WRAPS (type)
1954 || (INTEGRAL_TYPE_P (type)
1955 && tree_expr_nonzero_p (@0)
1956 && expr_not_equal_to (@0, wi::minus_one (TYPE_PRECISION (type)))))
1957 (mult (plusminus @1 @2) @0)))
1958 /* We cannot generate constant 1 for fract. */
1959 (if (!ALL_FRACT_MODE_P (TYPE_MODE (type)))
1961 (plusminus @0 (mult:cs @0 @2))
1962 (if (!ANY_INTEGRAL_TYPE_P (type)
1963 || TYPE_OVERFLOW_WRAPS (type)
1964 || (INTEGRAL_TYPE_P (type)
1965 && tree_expr_nonzero_p (@0)
1966 && expr_not_equal_to (@0, wi::minus_one (TYPE_PRECISION (type)))))
1967 (mult (plusminus { build_one_cst (type); } @2) @0)))
1969 (plusminus (mult:cs @0 @2) @0)
1970 (if (!ANY_INTEGRAL_TYPE_P (type)
1971 || TYPE_OVERFLOW_WRAPS (type)
1972 || (INTEGRAL_TYPE_P (type)
1973 && tree_expr_nonzero_p (@0)
1974 && expr_not_equal_to (@0, wi::minus_one (TYPE_PRECISION (type)))))
1975 (mult (plusminus @2 { build_one_cst (type); }) @0))))))
1977 /* Simplifications of MIN_EXPR, MAX_EXPR, fmin() and fmax(). */
1979 (for minmax (min max FMIN_ALL FMAX_ALL)
1983 /* min(max(x,y),y) -> y. */
1985 (min:c (max:c @0 @1) @1)
1987 /* max(min(x,y),y) -> y. */
1989 (max:c (min:c @0 @1) @1)
1991 /* max(a,-a) -> abs(a). */
1993 (max:c @0 (negate @0))
1994 (if (TREE_CODE (type) != COMPLEX_TYPE
1995 && (! ANY_INTEGRAL_TYPE_P (type)
1996 || TYPE_OVERFLOW_UNDEFINED (type)))
1998 /* min(a,-a) -> -abs(a). */
2000 (min:c @0 (negate @0))
2001 (if (TREE_CODE (type) != COMPLEX_TYPE
2002 && (! ANY_INTEGRAL_TYPE_P (type)
2003 || TYPE_OVERFLOW_UNDEFINED (type)))
2008 (if (INTEGRAL_TYPE_P (type)
2009 && TYPE_MIN_VALUE (type)
2010 && operand_equal_p (@1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
2012 (if (INTEGRAL_TYPE_P (type)
2013 && TYPE_MAX_VALUE (type)
2014 && operand_equal_p (@1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
2019 (if (INTEGRAL_TYPE_P (type)
2020 && TYPE_MAX_VALUE (type)
2021 && operand_equal_p (@1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
2023 (if (INTEGRAL_TYPE_P (type)
2024 && TYPE_MIN_VALUE (type)
2025 && operand_equal_p (@1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
2028 /* max (a, a + CST) -> a + CST where CST is positive. */
2029 /* max (a, a + CST) -> a where CST is negative. */
2031 (max:c @0 (plus@2 @0 INTEGER_CST@1))
2032 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
2033 (if (tree_int_cst_sgn (@1) > 0)
2037 /* min (a, a + CST) -> a where CST is positive. */
2038 /* min (a, a + CST) -> a + CST where CST is negative. */
2040 (min:c @0 (plus@2 @0 INTEGER_CST@1))
2041 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
2042 (if (tree_int_cst_sgn (@1) > 0)
2046 /* (convert (minmax ((convert (x) c)))) -> minmax (x c) if x is promoted
2047 and the outer convert demotes the expression back to x's type. */
2048 (for minmax (min max)
2050 (convert (minmax@0 (convert @1) INTEGER_CST@2))
2051 (if (INTEGRAL_TYPE_P (type)
2052 && types_match (@1, type) && int_fits_type_p (@2, type)
2053 && TYPE_SIGN (TREE_TYPE (@0)) == TYPE_SIGN (type)
2054 && TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type))
2055 (minmax @1 (convert @2)))))
2057 (for minmax (FMIN_ALL FMAX_ALL)
2058 /* If either argument is NaN, return the other one. Avoid the
2059 transformation if we get (and honor) a signalling NaN. */
2061 (minmax:c @0 REAL_CST@1)
2062 (if (real_isnan (TREE_REAL_CST_PTR (@1))
2063 && (!HONOR_SNANS (@1) || !TREE_REAL_CST (@1).signalling))
2065 /* Convert fmin/fmax to MIN_EXPR/MAX_EXPR. C99 requires these
2066 functions to return the numeric arg if the other one is NaN.
2067 MIN and MAX don't honor that, so only transform if -ffinite-math-only
2068 is set. C99 doesn't require -0.0 to be handled, so we don't have to
2069 worry about it either. */
2070 (if (flag_finite_math_only)
2077 /* min (-A, -B) -> -max (A, B) */
2078 (for minmax (min max FMIN_ALL FMAX_ALL)
2079 maxmin (max min FMAX_ALL FMIN_ALL)
2081 (minmax (negate:s@2 @0) (negate:s@3 @1))
2082 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
2083 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2084 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
2085 (negate (maxmin @0 @1)))))
2086 /* MIN (~X, ~Y) -> ~MAX (X, Y)
2087 MAX (~X, ~Y) -> ~MIN (X, Y) */
2088 (for minmax (min max)
2091 (minmax (bit_not:s@2 @0) (bit_not:s@3 @1))
2092 (bit_not (maxmin @0 @1))))
2094 /* MIN (X, Y) == X -> X <= Y */
2095 (for minmax (min min max max)
2099 (cmp:c (minmax:c @0 @1) @0)
2100 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0)))
2102 /* MIN (X, 5) == 0 -> X == 0
2103 MIN (X, 5) == 7 -> false */
2106 (cmp (min @0 INTEGER_CST@1) INTEGER_CST@2)
2107 (if (wi::lt_p (wi::to_wide (@1), wi::to_wide (@2),
2108 TYPE_SIGN (TREE_TYPE (@0))))
2109 { constant_boolean_node (cmp == NE_EXPR, type); }
2110 (if (wi::gt_p (wi::to_wide (@1), wi::to_wide (@2),
2111 TYPE_SIGN (TREE_TYPE (@0))))
2115 (cmp (max @0 INTEGER_CST@1) INTEGER_CST@2)
2116 (if (wi::gt_p (wi::to_wide (@1), wi::to_wide (@2),
2117 TYPE_SIGN (TREE_TYPE (@0))))
2118 { constant_boolean_node (cmp == NE_EXPR, type); }
2119 (if (wi::lt_p (wi::to_wide (@1), wi::to_wide (@2),
2120 TYPE_SIGN (TREE_TYPE (@0))))
2122 /* MIN (X, C1) < C2 -> X < C2 || C1 < C2 */
2123 (for minmax (min min max max min min max max )
2124 cmp (lt le gt ge gt ge lt le )
2125 comb (bit_ior bit_ior bit_ior bit_ior bit_and bit_and bit_and bit_and)
2127 (cmp (minmax @0 INTEGER_CST@1) INTEGER_CST@2)
2128 (comb (cmp @0 @2) (cmp @1 @2))))
2130 /* Simplifications of shift and rotates. */
2132 (for rotate (lrotate rrotate)
2134 (rotate integer_all_onesp@0 @1)
2137 /* Optimize -1 >> x for arithmetic right shifts. */
2139 (rshift integer_all_onesp@0 @1)
2140 (if (!TYPE_UNSIGNED (type)
2141 && tree_expr_nonnegative_p (@1))
2144 /* Optimize (x >> c) << c into x & (-1<<c). */
2146 (lshift (rshift @0 INTEGER_CST@1) @1)
2147 (if (wi::ltu_p (wi::to_wide (@1), element_precision (type)))
2148 (bit_and @0 (lshift { build_minus_one_cst (type); } @1))))
2150 /* Optimize (x << c) >> c into x & ((unsigned)-1 >> c) for unsigned
2153 (rshift (lshift @0 INTEGER_CST@1) @1)
2154 (if (TYPE_UNSIGNED (type)
2155 && (wi::ltu_p (wi::to_wide (@1), element_precision (type))))
2156 (bit_and @0 (rshift { build_minus_one_cst (type); } @1))))
2158 (for shiftrotate (lrotate rrotate lshift rshift)
2160 (shiftrotate @0 integer_zerop)
2163 (shiftrotate integer_zerop@0 @1)
2165 /* Prefer vector1 << scalar to vector1 << vector2
2166 if vector2 is uniform. */
2167 (for vec (VECTOR_CST CONSTRUCTOR)
2169 (shiftrotate @0 vec@1)
2170 (with { tree tem = uniform_vector_p (@1); }
2172 (shiftrotate @0 { tem; }))))))
2174 /* Simplify X << Y where Y's low width bits are 0 to X, as only valid
2175 Y is 0. Similarly for X >> Y. */
2177 (for shift (lshift rshift)
2179 (shift @0 SSA_NAME@1)
2180 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1)))
2182 int width = ceil_log2 (element_precision (TREE_TYPE (@0)));
2183 int prec = TYPE_PRECISION (TREE_TYPE (@1));
2185 (if ((get_nonzero_bits (@1) & wi::mask (width, false, prec)) == 0)
2189 /* Rewrite an LROTATE_EXPR by a constant into an
2190 RROTATE_EXPR by a new constant. */
2192 (lrotate @0 INTEGER_CST@1)
2193 (rrotate @0 { const_binop (MINUS_EXPR, TREE_TYPE (@1),
2194 build_int_cst (TREE_TYPE (@1),
2195 element_precision (type)), @1); }))
2197 /* Turn (a OP c1) OP c2 into a OP (c1+c2). */
2198 (for op (lrotate rrotate rshift lshift)
2200 (op (op @0 INTEGER_CST@1) INTEGER_CST@2)
2201 (with { unsigned int prec = element_precision (type); }
2202 (if (wi::ge_p (wi::to_wide (@1), 0, TYPE_SIGN (TREE_TYPE (@1)))
2203 && wi::lt_p (wi::to_wide (@1), prec, TYPE_SIGN (TREE_TYPE (@1)))
2204 && wi::ge_p (wi::to_wide (@2), 0, TYPE_SIGN (TREE_TYPE (@2)))
2205 && wi::lt_p (wi::to_wide (@2), prec, TYPE_SIGN (TREE_TYPE (@2))))
2206 (with { unsigned int low = (tree_to_uhwi (@1)
2207 + tree_to_uhwi (@2)); }
2208 /* Deal with a OP (c1 + c2) being undefined but (a OP c1) OP c2
2209 being well defined. */
2211 (if (op == LROTATE_EXPR || op == RROTATE_EXPR)
2212 (op @0 { build_int_cst (TREE_TYPE (@1), low % prec); })
2213 (if (TYPE_UNSIGNED (type) || op == LSHIFT_EXPR)
2214 { build_zero_cst (type); }
2215 (op @0 { build_int_cst (TREE_TYPE (@1), prec - 1); })))
2216 (op @0 { build_int_cst (TREE_TYPE (@1), low); })))))))
2219 /* ((1 << A) & 1) != 0 -> A == 0
2220 ((1 << A) & 1) == 0 -> A != 0 */
2224 (cmp (bit_and (lshift integer_onep @0) integer_onep) integer_zerop)
2225 (icmp @0 { build_zero_cst (TREE_TYPE (@0)); })))
2227 /* (CST1 << A) == CST2 -> A == ctz (CST2) - ctz (CST1)
2228 (CST1 << A) != CST2 -> A != ctz (CST2) - ctz (CST1)
2232 (cmp (lshift INTEGER_CST@0 @1) INTEGER_CST@2)
2233 (with { int cand = wi::ctz (wi::to_wide (@2)) - wi::ctz (wi::to_wide (@0)); }
2235 || (!integer_zerop (@2)
2236 && wi::lshift (wi::to_wide (@0), cand) != wi::to_wide (@2)))
2237 { constant_boolean_node (cmp == NE_EXPR, type); }
2238 (if (!integer_zerop (@2)
2239 && wi::lshift (wi::to_wide (@0), cand) == wi::to_wide (@2))
2240 (cmp @1 { build_int_cst (TREE_TYPE (@1), cand); }))))))
2242 /* Fold (X << C1) & C2 into (X << C1) & (C2 | ((1 << C1) - 1))
2243 (X >> C1) & C2 into (X >> C1) & (C2 | ~((type) -1 >> C1))
2244 if the new mask might be further optimized. */
2245 (for shift (lshift rshift)
2247 (bit_and (convert?:s@4 (shift:s@5 (convert1?@3 @0) INTEGER_CST@1))
2249 (if (tree_nop_conversion_p (TREE_TYPE (@4), TREE_TYPE (@5))
2250 && TYPE_PRECISION (type) <= HOST_BITS_PER_WIDE_INT
2251 && tree_fits_uhwi_p (@1)
2252 && tree_to_uhwi (@1) > 0
2253 && tree_to_uhwi (@1) < TYPE_PRECISION (type))
2256 unsigned int shiftc = tree_to_uhwi (@1);
2257 unsigned HOST_WIDE_INT mask = TREE_INT_CST_LOW (@2);
2258 unsigned HOST_WIDE_INT newmask, zerobits = 0;
2259 tree shift_type = TREE_TYPE (@3);
2262 if (shift == LSHIFT_EXPR)
2263 zerobits = ((HOST_WIDE_INT_1U << shiftc) - 1);
2264 else if (shift == RSHIFT_EXPR
2265 && type_has_mode_precision_p (shift_type))
2267 prec = TYPE_PRECISION (TREE_TYPE (@3));
2269 /* See if more bits can be proven as zero because of
2272 && TYPE_UNSIGNED (TREE_TYPE (@0)))
2274 tree inner_type = TREE_TYPE (@0);
2275 if (type_has_mode_precision_p (inner_type)
2276 && TYPE_PRECISION (inner_type) < prec)
2278 prec = TYPE_PRECISION (inner_type);
2279 /* See if we can shorten the right shift. */
2281 shift_type = inner_type;
2282 /* Otherwise X >> C1 is all zeros, so we'll optimize
2283 it into (X, 0) later on by making sure zerobits
2287 zerobits = HOST_WIDE_INT_M1U;
2290 zerobits >>= HOST_BITS_PER_WIDE_INT - shiftc;
2291 zerobits <<= prec - shiftc;
2293 /* For arithmetic shift if sign bit could be set, zerobits
2294 can contain actually sign bits, so no transformation is
2295 possible, unless MASK masks them all away. In that
2296 case the shift needs to be converted into logical shift. */
2297 if (!TYPE_UNSIGNED (TREE_TYPE (@3))
2298 && prec == TYPE_PRECISION (TREE_TYPE (@3)))
2300 if ((mask & zerobits) == 0)
2301 shift_type = unsigned_type_for (TREE_TYPE (@3));
2307 /* ((X << 16) & 0xff00) is (X, 0). */
2308 (if ((mask & zerobits) == mask)
2309 { build_int_cst (type, 0); }
2310 (with { newmask = mask | zerobits; }
2311 (if (newmask != mask && (newmask & (newmask + 1)) == 0)
2314 /* Only do the transformation if NEWMASK is some integer
2316 for (prec = BITS_PER_UNIT;
2317 prec < HOST_BITS_PER_WIDE_INT; prec <<= 1)
2318 if (newmask == (HOST_WIDE_INT_1U << prec) - 1)
2321 (if (prec < HOST_BITS_PER_WIDE_INT
2322 || newmask == HOST_WIDE_INT_M1U)
2324 { tree newmaskt = build_int_cst_type (TREE_TYPE (@2), newmask); }
2325 (if (!tree_int_cst_equal (newmaskt, @2))
2326 (if (shift_type != TREE_TYPE (@3))
2327 (bit_and (convert (shift:shift_type (convert @3) @1)) { newmaskt; })
2328 (bit_and @4 { newmaskt; })))))))))))))
2330 /* Fold (X {&,^,|} C2) << C1 into (X << C1) {&,^,|} (C2 << C1)
2331 (X {&,^,|} C2) >> C1 into (X >> C1) & (C2 >> C1). */
2332 (for shift (lshift rshift)
2333 (for bit_op (bit_and bit_xor bit_ior)
2335 (shift (convert?:s (bit_op:s @0 INTEGER_CST@2)) INTEGER_CST@1)
2336 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2337 (with { tree mask = int_const_binop (shift, fold_convert (type, @2), @1); }
2338 (bit_op (shift (convert @0) @1) { mask; }))))))
2340 /* ~(~X >> Y) -> X >> Y (for arithmetic shift). */
2342 (bit_not (convert1?:s (rshift:s (convert2?@0 (bit_not @1)) @2)))
2343 (if (!TYPE_UNSIGNED (TREE_TYPE (@0))
2344 && (element_precision (TREE_TYPE (@0))
2345 <= element_precision (TREE_TYPE (@1))
2346 || !TYPE_UNSIGNED (TREE_TYPE (@1))))
2348 { tree shift_type = TREE_TYPE (@0); }
2349 (convert (rshift (convert:shift_type @1) @2)))))
2351 /* ~(~X >>r Y) -> X >>r Y
2352 ~(~X <<r Y) -> X <<r Y */
2353 (for rotate (lrotate rrotate)
2355 (bit_not (convert1?:s (rotate:s (convert2?@0 (bit_not @1)) @2)))
2356 (if ((element_precision (TREE_TYPE (@0))
2357 <= element_precision (TREE_TYPE (@1))
2358 || !TYPE_UNSIGNED (TREE_TYPE (@1)))
2359 && (element_precision (type) <= element_precision (TREE_TYPE (@0))
2360 || !TYPE_UNSIGNED (TREE_TYPE (@0))))
2362 { tree rotate_type = TREE_TYPE (@0); }
2363 (convert (rotate (convert:rotate_type @1) @2))))))
2365 /* Simplifications of conversions. */
2367 /* Basic strip-useless-type-conversions / strip_nops. */
2368 (for cvt (convert view_convert float fix_trunc)
2371 (if ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@0)))
2372 || (GENERIC && type == TREE_TYPE (@0)))
2375 /* Contract view-conversions. */
2377 (view_convert (view_convert @0))
2380 /* For integral conversions with the same precision or pointer
2381 conversions use a NOP_EXPR instead. */
2384 (if ((INTEGRAL_TYPE_P (type) || POINTER_TYPE_P (type))
2385 && (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
2386 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (@0)))
2389 /* Strip inner integral conversions that do not change precision or size, or
2390 zero-extend while keeping the same size (for bool-to-char). */
2392 (view_convert (convert@0 @1))
2393 (if ((INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
2394 && (INTEGRAL_TYPE_P (TREE_TYPE (@1)) || POINTER_TYPE_P (TREE_TYPE (@1)))
2395 && TYPE_SIZE (TREE_TYPE (@0)) == TYPE_SIZE (TREE_TYPE (@1))
2396 && (TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1))
2397 || (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (TREE_TYPE (@1))
2398 && TYPE_UNSIGNED (TREE_TYPE (@1)))))
2401 /* Re-association barriers around constants and other re-association
2402 barriers can be removed. */
2404 (paren CONSTANT_CLASS_P@0)
2407 (paren (paren@1 @0))
2410 /* Handle cases of two conversions in a row. */
2411 (for ocvt (convert float fix_trunc)
2412 (for icvt (convert float)
2417 tree inside_type = TREE_TYPE (@0);
2418 tree inter_type = TREE_TYPE (@1);
2419 int inside_int = INTEGRAL_TYPE_P (inside_type);
2420 int inside_ptr = POINTER_TYPE_P (inside_type);
2421 int inside_float = FLOAT_TYPE_P (inside_type);
2422 int inside_vec = VECTOR_TYPE_P (inside_type);
2423 unsigned int inside_prec = TYPE_PRECISION (inside_type);
2424 int inside_unsignedp = TYPE_UNSIGNED (inside_type);
2425 int inter_int = INTEGRAL_TYPE_P (inter_type);
2426 int inter_ptr = POINTER_TYPE_P (inter_type);
2427 int inter_float = FLOAT_TYPE_P (inter_type);
2428 int inter_vec = VECTOR_TYPE_P (inter_type);
2429 unsigned int inter_prec = TYPE_PRECISION (inter_type);
2430 int inter_unsignedp = TYPE_UNSIGNED (inter_type);
2431 int final_int = INTEGRAL_TYPE_P (type);
2432 int final_ptr = POINTER_TYPE_P (type);
2433 int final_float = FLOAT_TYPE_P (type);
2434 int final_vec = VECTOR_TYPE_P (type);
2435 unsigned int final_prec = TYPE_PRECISION (type);
2436 int final_unsignedp = TYPE_UNSIGNED (type);
2439 /* In addition to the cases of two conversions in a row
2440 handled below, if we are converting something to its own
2441 type via an object of identical or wider precision, neither
2442 conversion is needed. */
2443 (if (((GIMPLE && useless_type_conversion_p (type, inside_type))
2445 && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (inside_type)))
2446 && (((inter_int || inter_ptr) && final_int)
2447 || (inter_float && final_float))
2448 && inter_prec >= final_prec)
2451 /* Likewise, if the intermediate and initial types are either both
2452 float or both integer, we don't need the middle conversion if the
2453 former is wider than the latter and doesn't change the signedness
2454 (for integers). Avoid this if the final type is a pointer since
2455 then we sometimes need the middle conversion. */
2456 (if (((inter_int && inside_int) || (inter_float && inside_float))
2457 && (final_int || final_float)
2458 && inter_prec >= inside_prec
2459 && (inter_float || inter_unsignedp == inside_unsignedp))
2462 /* If we have a sign-extension of a zero-extended value, we can
2463 replace that by a single zero-extension. Likewise if the
2464 final conversion does not change precision we can drop the
2465 intermediate conversion. */
2466 (if (inside_int && inter_int && final_int
2467 && ((inside_prec < inter_prec && inter_prec < final_prec
2468 && inside_unsignedp && !inter_unsignedp)
2469 || final_prec == inter_prec))
2472 /* Two conversions in a row are not needed unless:
2473 - some conversion is floating-point (overstrict for now), or
2474 - some conversion is a vector (overstrict for now), or
2475 - the intermediate type is narrower than both initial and
2477 - the intermediate type and innermost type differ in signedness,
2478 and the outermost type is wider than the intermediate, or
2479 - the initial type is a pointer type and the precisions of the
2480 intermediate and final types differ, or
2481 - the final type is a pointer type and the precisions of the
2482 initial and intermediate types differ. */
2483 (if (! inside_float && ! inter_float && ! final_float
2484 && ! inside_vec && ! inter_vec && ! final_vec
2485 && (inter_prec >= inside_prec || inter_prec >= final_prec)
2486 && ! (inside_int && inter_int
2487 && inter_unsignedp != inside_unsignedp
2488 && inter_prec < final_prec)
2489 && ((inter_unsignedp && inter_prec > inside_prec)
2490 == (final_unsignedp && final_prec > inter_prec))
2491 && ! (inside_ptr && inter_prec != final_prec)
2492 && ! (final_ptr && inside_prec != inter_prec))
2495 /* A truncation to an unsigned type (a zero-extension) should be
2496 canonicalized as bitwise and of a mask. */
2497 (if (GIMPLE /* PR70366: doing this in GENERIC breaks -Wconversion. */
2498 && final_int && inter_int && inside_int
2499 && final_prec == inside_prec
2500 && final_prec > inter_prec
2502 (convert (bit_and @0 { wide_int_to_tree
2504 wi::mask (inter_prec, false,
2505 TYPE_PRECISION (inside_type))); })))
2507 /* If we are converting an integer to a floating-point that can
2508 represent it exactly and back to an integer, we can skip the
2509 floating-point conversion. */
2510 (if (GIMPLE /* PR66211 */
2511 && inside_int && inter_float && final_int &&
2512 (unsigned) significand_size (TYPE_MODE (inter_type))
2513 >= inside_prec - !inside_unsignedp)
2516 /* If we have a narrowing conversion to an integral type that is fed by a
2517 BIT_AND_EXPR, we might be able to remove the BIT_AND_EXPR if it merely
2518 masks off bits outside the final type (and nothing else). */
2520 (convert (bit_and @0 INTEGER_CST@1))
2521 (if (INTEGRAL_TYPE_P (type)
2522 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
2523 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))
2524 && operand_equal_p (@1, build_low_bits_mask (TREE_TYPE (@1),
2525 TYPE_PRECISION (type)), 0))
2529 /* (X /[ex] A) * A -> X. */
2531 (mult (convert1? (exact_div @0 @@1)) (convert2? @1))
2534 /* Canonicalization of binary operations. */
2536 /* Convert X + -C into X - C. */
2538 (plus @0 REAL_CST@1)
2539 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
2540 (with { tree tem = const_unop (NEGATE_EXPR, type, @1); }
2541 (if (!TREE_OVERFLOW (tem) || !flag_trapping_math)
2542 (minus @0 { tem; })))))
2544 /* Convert x+x into x*2. */
2547 (if (SCALAR_FLOAT_TYPE_P (type))
2548 (mult @0 { build_real (type, dconst2); })
2549 (if (INTEGRAL_TYPE_P (type))
2550 (mult @0 { build_int_cst (type, 2); }))))
2554 (minus integer_zerop @1)
2557 (pointer_diff integer_zerop @1)
2558 (negate (convert @1)))
2560 /* (ARG0 - ARG1) is the same as (-ARG1 + ARG0). So check whether
2561 ARG0 is zero and X + ARG0 reduces to X, since that would mean
2562 (-ARG1 + ARG0) reduces to -ARG1. */
2564 (minus real_zerop@0 @1)
2565 (if (fold_real_zero_addition_p (type, @0, 0))
2568 /* Transform x * -1 into -x. */
2570 (mult @0 integer_minus_onep)
2573 /* Reassociate (X * CST) * Y to (X * Y) * CST. This does not introduce
2574 signed overflow for CST != 0 && CST != -1. */
2576 (mult:c (mult:s @0 INTEGER_CST@1) @2)
2577 (if (TREE_CODE (@2) != INTEGER_CST
2578 && !integer_zerop (@1) && !integer_minus_onep (@1))
2579 (mult (mult @0 @2) @1)))
2581 /* True if we can easily extract the real and imaginary parts of a complex
2583 (match compositional_complex
2584 (convert? (complex @0 @1)))
2586 /* COMPLEX_EXPR and REALPART/IMAGPART_EXPR cancellations. */
2588 (complex (realpart @0) (imagpart @0))
2591 (realpart (complex @0 @1))
2594 (imagpart (complex @0 @1))
2597 /* Sometimes we only care about half of a complex expression. */
2599 (realpart (convert?:s (conj:s @0)))
2600 (convert (realpart @0)))
2602 (imagpart (convert?:s (conj:s @0)))
2603 (convert (negate (imagpart @0))))
2604 (for part (realpart imagpart)
2605 (for op (plus minus)
2607 (part (convert?:s@2 (op:s @0 @1)))
2608 (convert (op (part @0) (part @1))))))
2610 (realpart (convert?:s (CEXPI:s @0)))
2613 (imagpart (convert?:s (CEXPI:s @0)))
2616 /* conj(conj(x)) -> x */
2618 (conj (convert? (conj @0)))
2619 (if (tree_nop_conversion_p (TREE_TYPE (@0), type))
2622 /* conj({x,y}) -> {x,-y} */
2624 (conj (convert?:s (complex:s @0 @1)))
2625 (with { tree itype = TREE_TYPE (type); }
2626 (complex (convert:itype @0) (negate (convert:itype @1)))))
2628 /* BSWAP simplifications, transforms checked by gcc.dg/builtin-bswap-8.c. */
2629 (for bswap (BUILT_IN_BSWAP16 BUILT_IN_BSWAP32 BUILT_IN_BSWAP64)
2634 (bswap (bit_not (bswap @0)))
2636 (for bitop (bit_xor bit_ior bit_and)
2638 (bswap (bitop:c (bswap @0) @1))
2639 (bitop @0 (bswap @1)))))
2642 /* Combine COND_EXPRs and VEC_COND_EXPRs. */
2644 /* Simplify constant conditions.
2645 Only optimize constant conditions when the selected branch
2646 has the same type as the COND_EXPR. This avoids optimizing
2647 away "c ? x : throw", where the throw has a void type.
2648 Note that we cannot throw away the fold-const.c variant nor
2649 this one as we depend on doing this transform before possibly
2650 A ? B : B -> B triggers and the fold-const.c one can optimize
2651 0 ? A : B to B even if A has side-effects. Something
2652 genmatch cannot handle. */
2654 (cond INTEGER_CST@0 @1 @2)
2655 (if (integer_zerop (@0))
2656 (if (!VOID_TYPE_P (TREE_TYPE (@2)) || VOID_TYPE_P (type))
2658 (if (!VOID_TYPE_P (TREE_TYPE (@1)) || VOID_TYPE_P (type))
2661 (vec_cond VECTOR_CST@0 @1 @2)
2662 (if (integer_all_onesp (@0))
2664 (if (integer_zerop (@0))
2667 /* Simplification moved from fold_cond_expr_with_comparison. It may also
2669 /* This pattern implements two kinds simplification:
2672 (cond (cmp (convert1? x) c1) (convert2? x) c2) -> (minmax (x c)) if:
2673 1) Conversions are type widening from smaller type.
2674 2) Const c1 equals to c2 after canonicalizing comparison.
2675 3) Comparison has tree code LT, LE, GT or GE.
2676 This specific pattern is needed when (cmp (convert x) c) may not
2677 be simplified by comparison patterns because of multiple uses of
2678 x. It also makes sense here because simplifying across multiple
2679 referred var is always benefitial for complicated cases.
2682 (cond (eq (convert1? x) c1) (convert2? x) c2) -> (cond (eq x c1) c1 c2). */
2683 (for cmp (lt le gt ge eq)
2685 (cond (cmp (convert1? @1) INTEGER_CST@3) (convert2? @1) INTEGER_CST@2)
2688 tree from_type = TREE_TYPE (@1);
2689 tree c1_type = TREE_TYPE (@3), c2_type = TREE_TYPE (@2);
2690 enum tree_code code = ERROR_MARK;
2692 if (INTEGRAL_TYPE_P (from_type)
2693 && int_fits_type_p (@2, from_type)
2694 && (types_match (c1_type, from_type)
2695 || (TYPE_PRECISION (c1_type) > TYPE_PRECISION (from_type)
2696 && (TYPE_UNSIGNED (from_type)
2697 || TYPE_SIGN (c1_type) == TYPE_SIGN (from_type))))
2698 && (types_match (c2_type, from_type)
2699 || (TYPE_PRECISION (c2_type) > TYPE_PRECISION (from_type)
2700 && (TYPE_UNSIGNED (from_type)
2701 || TYPE_SIGN (c2_type) == TYPE_SIGN (from_type)))))
2705 if (wi::to_widest (@3) == (wi::to_widest (@2) - 1))
2707 /* X <= Y - 1 equals to X < Y. */
2710 /* X > Y - 1 equals to X >= Y. */
2714 if (wi::to_widest (@3) == (wi::to_widest (@2) + 1))
2716 /* X < Y + 1 equals to X <= Y. */
2719 /* X >= Y + 1 equals to X > Y. */
2723 if (code != ERROR_MARK
2724 || wi::to_widest (@2) == wi::to_widest (@3))
2726 if (cmp == LT_EXPR || cmp == LE_EXPR)
2728 if (cmp == GT_EXPR || cmp == GE_EXPR)
2732 /* Can do A == C1 ? A : C2 -> A == C1 ? C1 : C2? */
2733 else if (int_fits_type_p (@3, from_type))
2737 (if (code == MAX_EXPR)
2738 (convert (max @1 (convert @2)))
2739 (if (code == MIN_EXPR)
2740 (convert (min @1 (convert @2)))
2741 (if (code == EQ_EXPR)
2742 (convert (cond (eq @1 (convert @3))
2743 (convert:from_type @3) (convert:from_type @2)))))))))
2745 /* (cond (cmp (convert? x) c1) (op x c2) c3) -> (op (minmax x c1) c2) if:
2747 1) OP is PLUS or MINUS.
2748 2) CMP is LT, LE, GT or GE.
2749 3) C3 == (C1 op C2), and computation doesn't have undefined behavior.
2751 This pattern also handles special cases like:
2753 A) Operand x is a unsigned to signed type conversion and c1 is
2754 integer zero. In this case,
2755 (signed type)x < 0 <=> x > MAX_VAL(signed type)
2756 (signed type)x >= 0 <=> x <= MAX_VAL(signed type)
2757 B) Const c1 may not equal to (C3 op' C2). In this case we also
2758 check equality for (c1+1) and (c1-1) by adjusting comparison
2761 TODO: Though signed type is handled by this pattern, it cannot be
2762 simplified at the moment because C standard requires additional
2763 type promotion. In order to match&simplify it here, the IR needs
2764 to be cleaned up by other optimizers, i.e, VRP. */
2765 (for op (plus minus)
2766 (for cmp (lt le gt ge)
2768 (cond (cmp (convert? @X) INTEGER_CST@1) (op @X INTEGER_CST@2) INTEGER_CST@3)
2769 (with { tree from_type = TREE_TYPE (@X), to_type = TREE_TYPE (@1); }
2770 (if (types_match (from_type, to_type)
2771 /* Check if it is special case A). */
2772 || (TYPE_UNSIGNED (from_type)
2773 && !TYPE_UNSIGNED (to_type)
2774 && TYPE_PRECISION (from_type) == TYPE_PRECISION (to_type)
2775 && integer_zerop (@1)
2776 && (cmp == LT_EXPR || cmp == GE_EXPR)))
2779 bool overflow = false;
2780 enum tree_code code, cmp_code = cmp;
2782 wide_int c1 = wi::to_wide (@1);
2783 wide_int c2 = wi::to_wide (@2);
2784 wide_int c3 = wi::to_wide (@3);
2785 signop sgn = TYPE_SIGN (from_type);
2787 /* Handle special case A), given x of unsigned type:
2788 ((signed type)x < 0) <=> (x > MAX_VAL(signed type))
2789 ((signed type)x >= 0) <=> (x <= MAX_VAL(signed type)) */
2790 if (!types_match (from_type, to_type))
2792 if (cmp_code == LT_EXPR)
2794 if (cmp_code == GE_EXPR)
2796 c1 = wi::max_value (to_type);
2798 /* To simplify this pattern, we require c3 = (c1 op c2). Here we
2799 compute (c3 op' c2) and check if it equals to c1 with op' being
2800 the inverted operator of op. Make sure overflow doesn't happen
2801 if it is undefined. */
2802 if (op == PLUS_EXPR)
2803 real_c1 = wi::sub (c3, c2, sgn, &overflow);
2805 real_c1 = wi::add (c3, c2, sgn, &overflow);
2808 if (!overflow || !TYPE_OVERFLOW_UNDEFINED (from_type))
2810 /* Check if c1 equals to real_c1. Boundary condition is handled
2811 by adjusting comparison operation if necessary. */
2812 if (!wi::cmp (wi::sub (real_c1, 1, sgn, &overflow), c1, sgn)
2815 /* X <= Y - 1 equals to X < Y. */
2816 if (cmp_code == LE_EXPR)
2818 /* X > Y - 1 equals to X >= Y. */
2819 if (cmp_code == GT_EXPR)
2822 if (!wi::cmp (wi::add (real_c1, 1, sgn, &overflow), c1, sgn)
2825 /* X < Y + 1 equals to X <= Y. */
2826 if (cmp_code == LT_EXPR)
2828 /* X >= Y + 1 equals to X > Y. */
2829 if (cmp_code == GE_EXPR)
2832 if (code != cmp_code || !wi::cmp (real_c1, c1, sgn))
2834 if (cmp_code == LT_EXPR || cmp_code == LE_EXPR)
2836 if (cmp_code == GT_EXPR || cmp_code == GE_EXPR)
2841 (if (code == MAX_EXPR)
2842 (op (max @X { wide_int_to_tree (from_type, real_c1); })
2843 { wide_int_to_tree (from_type, c2); })
2844 (if (code == MIN_EXPR)
2845 (op (min @X { wide_int_to_tree (from_type, real_c1); })
2846 { wide_int_to_tree (from_type, c2); })))))))))
2848 (for cnd (cond vec_cond)
2849 /* A ? B : (A ? X : C) -> A ? B : C. */
2851 (cnd @0 (cnd @0 @1 @2) @3)
2854 (cnd @0 @1 (cnd @0 @2 @3))
2856 /* A ? B : (!A ? C : X) -> A ? B : C. */
2857 /* ??? This matches embedded conditions open-coded because genmatch
2858 would generate matching code for conditions in separate stmts only.
2859 The following is still important to merge then and else arm cases
2860 from if-conversion. */
2862 (cnd @0 @1 (cnd @2 @3 @4))
2863 (if (COMPARISON_CLASS_P (@0)
2864 && COMPARISON_CLASS_P (@2)
2865 && invert_tree_comparison
2866 (TREE_CODE (@0), HONOR_NANS (TREE_OPERAND (@0, 0))) == TREE_CODE (@2)
2867 && operand_equal_p (TREE_OPERAND (@0, 0), TREE_OPERAND (@2, 0), 0)
2868 && operand_equal_p (TREE_OPERAND (@0, 1), TREE_OPERAND (@2, 1), 0))
2871 (cnd @0 (cnd @1 @2 @3) @4)
2872 (if (COMPARISON_CLASS_P (@0)
2873 && COMPARISON_CLASS_P (@1)
2874 && invert_tree_comparison
2875 (TREE_CODE (@0), HONOR_NANS (TREE_OPERAND (@0, 0))) == TREE_CODE (@1)
2876 && operand_equal_p (TREE_OPERAND (@0, 0), TREE_OPERAND (@1, 0), 0)
2877 && operand_equal_p (TREE_OPERAND (@0, 1), TREE_OPERAND (@1, 1), 0))
2880 /* A ? B : B -> B. */
2885 /* !A ? B : C -> A ? C : B. */
2887 (cnd (logical_inverted_value truth_valued_p@0) @1 @2)
2890 /* A + (B vcmp C ? 1 : 0) -> A - (B vcmp C ? -1 : 0), since vector comparisons
2891 return all -1 or all 0 results. */
2892 /* ??? We could instead convert all instances of the vec_cond to negate,
2893 but that isn't necessarily a win on its own. */
2895 (plus:c @3 (view_convert? (vec_cond:s @0 integer_each_onep@1 integer_zerop@2)))
2896 (if (VECTOR_TYPE_P (type)
2897 && known_eq (TYPE_VECTOR_SUBPARTS (type),
2898 TYPE_VECTOR_SUBPARTS (TREE_TYPE (@1)))
2899 && (TYPE_MODE (TREE_TYPE (type))
2900 == TYPE_MODE (TREE_TYPE (TREE_TYPE (@1)))))
2901 (minus @3 (view_convert (vec_cond @0 (negate @1) @2)))))
2903 /* ... likewise A - (B vcmp C ? 1 : 0) -> A + (B vcmp C ? -1 : 0). */
2905 (minus @3 (view_convert? (vec_cond:s @0 integer_each_onep@1 integer_zerop@2)))
2906 (if (VECTOR_TYPE_P (type)
2907 && known_eq (TYPE_VECTOR_SUBPARTS (type),
2908 TYPE_VECTOR_SUBPARTS (TREE_TYPE (@1)))
2909 && (TYPE_MODE (TREE_TYPE (type))
2910 == TYPE_MODE (TREE_TYPE (TREE_TYPE (@1)))))
2911 (plus @3 (view_convert (vec_cond @0 (negate @1) @2)))))
2914 /* Simplifications of comparisons. */
2916 /* See if we can reduce the magnitude of a constant involved in a
2917 comparison by changing the comparison code. This is a canonicalization
2918 formerly done by maybe_canonicalize_comparison_1. */
2922 (cmp @0 INTEGER_CST@1)
2923 (if (tree_int_cst_sgn (@1) == -1)
2924 (acmp @0 { wide_int_to_tree (TREE_TYPE (@1), wi::to_wide (@1) + 1); }))))
2928 (cmp @0 INTEGER_CST@1)
2929 (if (tree_int_cst_sgn (@1) == 1)
2930 (acmp @0 { wide_int_to_tree (TREE_TYPE (@1), wi::to_wide (@1) - 1); }))))
2933 /* We can simplify a logical negation of a comparison to the
2934 inverted comparison. As we cannot compute an expression
2935 operator using invert_tree_comparison we have to simulate
2936 that with expression code iteration. */
2937 (for cmp (tcc_comparison)
2938 icmp (inverted_tcc_comparison)
2939 ncmp (inverted_tcc_comparison_with_nans)
2940 /* Ideally we'd like to combine the following two patterns
2941 and handle some more cases by using
2942 (logical_inverted_value (cmp @0 @1))
2943 here but for that genmatch would need to "inline" that.
2944 For now implement what forward_propagate_comparison did. */
2946 (bit_not (cmp @0 @1))
2947 (if (VECTOR_TYPE_P (type)
2948 || (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1))
2949 /* Comparison inversion may be impossible for trapping math,
2950 invert_tree_comparison will tell us. But we can't use
2951 a computed operator in the replacement tree thus we have
2952 to play the trick below. */
2953 (with { enum tree_code ic = invert_tree_comparison
2954 (cmp, HONOR_NANS (@0)); }
2960 (bit_xor (cmp @0 @1) integer_truep)
2961 (with { enum tree_code ic = invert_tree_comparison
2962 (cmp, HONOR_NANS (@0)); }
2968 /* Transform comparisons of the form X - Y CMP 0 to X CMP Y.
2969 ??? The transformation is valid for the other operators if overflow
2970 is undefined for the type, but performing it here badly interacts
2971 with the transformation in fold_cond_expr_with_comparison which
2972 attempts to synthetize ABS_EXPR. */
2974 (for sub (minus pointer_diff)
2976 (cmp (sub@2 @0 @1) integer_zerop)
2977 (if (single_use (@2))
2980 /* Transform comparisons of the form X * C1 CMP 0 to X CMP 0 in the
2981 signed arithmetic case. That form is created by the compiler
2982 often enough for folding it to be of value. One example is in
2983 computing loop trip counts after Operator Strength Reduction. */
2984 (for cmp (simple_comparison)
2985 scmp (swapped_simple_comparison)
2987 (cmp (mult@3 @0 INTEGER_CST@1) integer_zerop@2)
2988 /* Handle unfolded multiplication by zero. */
2989 (if (integer_zerop (@1))
2991 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2992 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2994 /* If @1 is negative we swap the sense of the comparison. */
2995 (if (tree_int_cst_sgn (@1) < 0)
2999 /* Simplify comparison of something with itself. For IEEE
3000 floating-point, we can only do some of these simplifications. */
3004 (if (! FLOAT_TYPE_P (TREE_TYPE (@0))
3005 || ! HONOR_NANS (@0))
3006 { constant_boolean_node (true, type); }
3007 (if (cmp != EQ_EXPR)
3013 || ! FLOAT_TYPE_P (TREE_TYPE (@0))
3014 || ! HONOR_NANS (@0))
3015 { constant_boolean_node (false, type); })))
3016 (for cmp (unle unge uneq)
3019 { constant_boolean_node (true, type); }))
3020 (for cmp (unlt ungt)
3026 (if (!flag_trapping_math)
3027 { constant_boolean_node (false, type); }))
3029 /* Fold ~X op ~Y as Y op X. */
3030 (for cmp (simple_comparison)
3032 (cmp (bit_not@2 @0) (bit_not@3 @1))
3033 (if (single_use (@2) && single_use (@3))
3036 /* Fold ~X op C as X op' ~C, where op' is the swapped comparison. */
3037 (for cmp (simple_comparison)
3038 scmp (swapped_simple_comparison)
3040 (cmp (bit_not@2 @0) CONSTANT_CLASS_P@1)
3041 (if (single_use (@2)
3042 && (TREE_CODE (@1) == INTEGER_CST || TREE_CODE (@1) == VECTOR_CST))
3043 (scmp @0 (bit_not @1)))))
3045 (for cmp (simple_comparison)
3046 /* Fold (double)float1 CMP (double)float2 into float1 CMP float2. */
3048 (cmp (convert@2 @0) (convert? @1))
3049 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
3050 && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@2))
3051 == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0)))
3052 && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@2))
3053 == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1))))
3056 tree type1 = TREE_TYPE (@1);
3057 if (TREE_CODE (@1) == REAL_CST && !DECIMAL_FLOAT_TYPE_P (type1))
3059 REAL_VALUE_TYPE orig = TREE_REAL_CST (@1);
3060 if (TYPE_PRECISION (type1) > TYPE_PRECISION (float_type_node)
3061 && exact_real_truncate (TYPE_MODE (float_type_node), &orig))
3062 type1 = float_type_node;
3063 if (TYPE_PRECISION (type1) > TYPE_PRECISION (double_type_node)
3064 && exact_real_truncate (TYPE_MODE (double_type_node), &orig))
3065 type1 = double_type_node;
3068 = (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type1)
3069 ? TREE_TYPE (@0) : type1);
3071 (if (TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (newtype))
3072 (cmp (convert:newtype @0) (convert:newtype @1))))))
3076 /* IEEE doesn't distinguish +0 and -0 in comparisons. */
3078 /* a CMP (-0) -> a CMP 0 */
3079 (if (REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@1)))
3080 (cmp @0 { build_real (TREE_TYPE (@1), dconst0); }))
3081 /* x != NaN is always true, other ops are always false. */
3082 (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
3083 && ! HONOR_SNANS (@1))
3084 { constant_boolean_node (cmp == NE_EXPR, type); })
3085 /* Fold comparisons against infinity. */
3086 (if (REAL_VALUE_ISINF (TREE_REAL_CST (@1))
3087 && MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (@1))))
3090 REAL_VALUE_TYPE max;
3091 enum tree_code code = cmp;
3092 bool neg = REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1));
3094 code = swap_tree_comparison (code);
3097 /* x > +Inf is always false, if we ignore NaNs or exceptions. */
3098 (if (code == GT_EXPR
3099 && !(HONOR_NANS (@0) && flag_trapping_math))
3100 { constant_boolean_node (false, type); })
3101 (if (code == LE_EXPR)
3102 /* x <= +Inf is always true, if we don't care about NaNs. */
3103 (if (! HONOR_NANS (@0))
3104 { constant_boolean_node (true, type); }
3105 /* x <= +Inf is the same as x == x, i.e. !isnan(x), but this loses
3106 an "invalid" exception. */
3107 (if (!flag_trapping_math)
3109 /* x == +Inf and x >= +Inf are always equal to x > DBL_MAX, but
3110 for == this introduces an exception for x a NaN. */
3111 (if ((code == EQ_EXPR && !(HONOR_NANS (@0) && flag_trapping_math))
3113 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
3115 (lt @0 { build_real (TREE_TYPE (@0), max); })
3116 (gt @0 { build_real (TREE_TYPE (@0), max); }))))
3117 /* x < +Inf is always equal to x <= DBL_MAX. */
3118 (if (code == LT_EXPR)
3119 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
3121 (ge @0 { build_real (TREE_TYPE (@0), max); })
3122 (le @0 { build_real (TREE_TYPE (@0), max); }))))
3123 /* x != +Inf is always equal to !(x > DBL_MAX), but this introduces
3124 an exception for x a NaN so use an unordered comparison. */
3125 (if (code == NE_EXPR)
3126 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
3127 (if (! HONOR_NANS (@0))
3129 (ge @0 { build_real (TREE_TYPE (@0), max); })
3130 (le @0 { build_real (TREE_TYPE (@0), max); }))
3132 (unge @0 { build_real (TREE_TYPE (@0), max); })
3133 (unle @0 { build_real (TREE_TYPE (@0), max); }))))))))))
3135 /* If this is a comparison of a real constant with a PLUS_EXPR
3136 or a MINUS_EXPR of a real constant, we can convert it into a
3137 comparison with a revised real constant as long as no overflow
3138 occurs when unsafe_math_optimizations are enabled. */
3139 (if (flag_unsafe_math_optimizations)
3140 (for op (plus minus)
3142 (cmp (op @0 REAL_CST@1) REAL_CST@2)
3145 tree tem = const_binop (op == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR,
3146 TREE_TYPE (@1), @2, @1);
3148 (if (tem && !TREE_OVERFLOW (tem))
3149 (cmp @0 { tem; }))))))
3151 /* Likewise, we can simplify a comparison of a real constant with
3152 a MINUS_EXPR whose first operand is also a real constant, i.e.
3153 (c1 - x) < c2 becomes x > c1-c2. Reordering is allowed on
3154 floating-point types only if -fassociative-math is set. */
3155 (if (flag_associative_math)
3157 (cmp (minus REAL_CST@0 @1) REAL_CST@2)
3158 (with { tree tem = const_binop (MINUS_EXPR, TREE_TYPE (@1), @0, @2); }
3159 (if (tem && !TREE_OVERFLOW (tem))
3160 (cmp { tem; } @1)))))
3162 /* Fold comparisons against built-in math functions. */
3163 (if (flag_unsafe_math_optimizations
3164 && ! flag_errno_math)
3167 (cmp (sq @0) REAL_CST@1)
3169 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
3171 /* sqrt(x) < y is always false, if y is negative. */
3172 (if (cmp == EQ_EXPR || cmp == LT_EXPR || cmp == LE_EXPR)
3173 { constant_boolean_node (false, type); })
3174 /* sqrt(x) > y is always true, if y is negative and we
3175 don't care about NaNs, i.e. negative values of x. */
3176 (if (cmp == NE_EXPR || !HONOR_NANS (@0))
3177 { constant_boolean_node (true, type); })
3178 /* sqrt(x) > y is the same as x >= 0, if y is negative. */
3179 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })))
3180 (if (real_equal (TREE_REAL_CST_PTR (@1), &dconst0))
3182 /* sqrt(x) < 0 is always false. */
3183 (if (cmp == LT_EXPR)
3184 { constant_boolean_node (false, type); })
3185 /* sqrt(x) >= 0 is always true if we don't care about NaNs. */
3186 (if (cmp == GE_EXPR && !HONOR_NANS (@0))
3187 { constant_boolean_node (true, type); })
3188 /* sqrt(x) <= 0 -> x == 0. */
3189 (if (cmp == LE_EXPR)
3191 /* Otherwise sqrt(x) cmp 0 -> x cmp 0. Here cmp can be >=, >,
3192 == or !=. In the last case:
3194 (sqrt(x) != 0) == (NaN != 0) == true == (x != 0)
3196 if x is negative or NaN. Due to -funsafe-math-optimizations,
3197 the results for other x follow from natural arithmetic. */
3199 (if (cmp == GT_EXPR || cmp == GE_EXPR)
3203 real_arithmetic (&c2, MULT_EXPR,
3204 &TREE_REAL_CST (@1), &TREE_REAL_CST (@1));
3205 real_convert (&c2, TYPE_MODE (TREE_TYPE (@0)), &c2);
3207 (if (REAL_VALUE_ISINF (c2))
3208 /* sqrt(x) > y is x == +Inf, when y is very large. */
3209 (if (HONOR_INFINITIES (@0))
3210 (eq @0 { build_real (TREE_TYPE (@0), c2); })
3211 { constant_boolean_node (false, type); })
3212 /* sqrt(x) > c is the same as x > c*c. */
3213 (cmp @0 { build_real (TREE_TYPE (@0), c2); }))))
3214 (if (cmp == LT_EXPR || cmp == LE_EXPR)
3218 real_arithmetic (&c2, MULT_EXPR,
3219 &TREE_REAL_CST (@1), &TREE_REAL_CST (@1));
3220 real_convert (&c2, TYPE_MODE (TREE_TYPE (@0)), &c2);
3222 (if (REAL_VALUE_ISINF (c2))
3224 /* sqrt(x) < y is always true, when y is a very large
3225 value and we don't care about NaNs or Infinities. */
3226 (if (! HONOR_NANS (@0) && ! HONOR_INFINITIES (@0))
3227 { constant_boolean_node (true, type); })
3228 /* sqrt(x) < y is x != +Inf when y is very large and we
3229 don't care about NaNs. */
3230 (if (! HONOR_NANS (@0))
3231 (ne @0 { build_real (TREE_TYPE (@0), c2); }))
3232 /* sqrt(x) < y is x >= 0 when y is very large and we
3233 don't care about Infinities. */
3234 (if (! HONOR_INFINITIES (@0))
3235 (ge @0 { build_real (TREE_TYPE (@0), dconst0); }))
3236 /* sqrt(x) < y is x >= 0 && x != +Inf, when y is large. */
3239 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
3240 (ne @0 { build_real (TREE_TYPE (@0), c2); }))))
3241 /* sqrt(x) < c is the same as x < c*c, if we ignore NaNs. */
3242 (if (! HONOR_NANS (@0))
3243 (cmp @0 { build_real (TREE_TYPE (@0), c2); })
3244 /* sqrt(x) < c is the same as x >= 0 && x < c*c. */
3247 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
3248 (cmp @0 { build_real (TREE_TYPE (@0), c2); })))))))))
3249 /* Transform sqrt(x) cmp sqrt(y) -> x cmp y. */
3251 (cmp (sq @0) (sq @1))
3252 (if (! HONOR_NANS (@0))
3255 /* Optimize various special cases of (FTYPE) N CMP CST. */
3256 (for cmp (lt le eq ne ge gt)
3257 icmp (le le eq ne ge ge)
3259 (cmp (float @0) REAL_CST@1)
3260 (if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (@1))
3261 && ! DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1)))
3264 tree itype = TREE_TYPE (@0);
3265 signop isign = TYPE_SIGN (itype);
3266 format_helper fmt (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (@1))));
3267 const REAL_VALUE_TYPE *cst = TREE_REAL_CST_PTR (@1);
3268 /* Be careful to preserve any potential exceptions due to
3269 NaNs. qNaNs are ok in == or != context.
3270 TODO: relax under -fno-trapping-math or
3271 -fno-signaling-nans. */
3273 = real_isnan (cst) && (cst->signalling
3274 || (cmp != EQ_EXPR && cmp != NE_EXPR));
3275 /* INT?_MIN is power-of-two so it takes
3276 only one mantissa bit. */
3277 bool signed_p = isign == SIGNED;
3278 bool itype_fits_ftype_p
3279 = TYPE_PRECISION (itype) - signed_p <= significand_size (fmt);
3281 /* TODO: allow non-fitting itype and SNaNs when
3282 -fno-trapping-math. */
3283 (if (itype_fits_ftype_p && ! exception_p)
3286 REAL_VALUE_TYPE imin, imax;
3287 real_from_integer (&imin, fmt, wi::min_value (itype), isign);
3288 real_from_integer (&imax, fmt, wi::max_value (itype), isign);
3290 REAL_VALUE_TYPE icst;
3291 if (cmp == GT_EXPR || cmp == GE_EXPR)
3292 real_ceil (&icst, fmt, cst);
3293 else if (cmp == LT_EXPR || cmp == LE_EXPR)
3294 real_floor (&icst, fmt, cst);
3296 real_trunc (&icst, fmt, cst);
3298 bool cst_int_p = !real_isnan (cst) && real_identical (&icst, cst);
3300 bool overflow_p = false;
3302 = real_to_integer (&icst, &overflow_p, TYPE_PRECISION (itype));
3305 /* Optimize cases when CST is outside of ITYPE's range. */
3306 (if (real_compare (LT_EXPR, cst, &imin))
3307 { constant_boolean_node (cmp == GT_EXPR || cmp == GE_EXPR || cmp == NE_EXPR,
3309 (if (real_compare (GT_EXPR, cst, &imax))
3310 { constant_boolean_node (cmp == LT_EXPR || cmp == LE_EXPR || cmp == NE_EXPR,
3312 /* Remove cast if CST is an integer representable by ITYPE. */
3314 (cmp @0 { gcc_assert (!overflow_p);
3315 wide_int_to_tree (itype, icst_val); })
3317 /* When CST is fractional, optimize
3318 (FTYPE) N == CST -> 0
3319 (FTYPE) N != CST -> 1. */
3320 (if (cmp == EQ_EXPR || cmp == NE_EXPR)
3321 { constant_boolean_node (cmp == NE_EXPR, type); })
3322 /* Otherwise replace with sensible integer constant. */
3325 gcc_checking_assert (!overflow_p);
3327 (icmp @0 { wide_int_to_tree (itype, icst_val); })))))))))
3329 /* Fold A /[ex] B CMP C to A CMP B * C. */
3332 (cmp (exact_div @0 @1) INTEGER_CST@2)
3333 (if (!integer_zerop (@1))
3334 (if (wi::to_wide (@2) == 0)
3336 (if (TREE_CODE (@1) == INTEGER_CST)
3340 wide_int prod = wi::mul (wi::to_wide (@2), wi::to_wide (@1),
3341 TYPE_SIGN (TREE_TYPE (@1)), &ovf);
3344 { constant_boolean_node (cmp == NE_EXPR, type); }
3345 (cmp @0 { wide_int_to_tree (TREE_TYPE (@0), prod); }))))))))
3346 (for cmp (lt le gt ge)
3348 (cmp (exact_div @0 INTEGER_CST@1) INTEGER_CST@2)
3349 (if (wi::gt_p (wi::to_wide (@1), 0, TYPE_SIGN (TREE_TYPE (@1))))
3353 wide_int prod = wi::mul (wi::to_wide (@2), wi::to_wide (@1),
3354 TYPE_SIGN (TREE_TYPE (@1)), &ovf);
3357 { constant_boolean_node (wi::lt_p (wi::to_wide (@2), 0,
3358 TYPE_SIGN (TREE_TYPE (@2)))
3359 != (cmp == LT_EXPR || cmp == LE_EXPR), type); }
3360 (cmp @0 { wide_int_to_tree (TREE_TYPE (@0), prod); }))))))
3362 /* Unordered tests if either argument is a NaN. */
3364 (bit_ior (unordered @0 @0) (unordered @1 @1))
3365 (if (types_match (@0, @1))
3368 (bit_and (ordered @0 @0) (ordered @1 @1))
3369 (if (types_match (@0, @1))
3372 (bit_ior:c (unordered @0 @0) (unordered:c@2 @0 @1))
3375 (bit_and:c (ordered @0 @0) (ordered:c@2 @0 @1))
3378 /* Simple range test simplifications. */
3379 /* A < B || A >= B -> true. */
3380 (for test1 (lt le le le ne ge)
3381 test2 (ge gt ge ne eq ne)
3383 (bit_ior:c (test1 @0 @1) (test2 @0 @1))
3384 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
3385 || VECTOR_INTEGER_TYPE_P (TREE_TYPE (@0)))
3386 { constant_boolean_node (true, type); })))
3387 /* A < B && A >= B -> false. */
3388 (for test1 (lt lt lt le ne eq)
3389 test2 (ge gt eq gt eq gt)
3391 (bit_and:c (test1 @0 @1) (test2 @0 @1))
3392 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
3393 || VECTOR_INTEGER_TYPE_P (TREE_TYPE (@0)))
3394 { constant_boolean_node (false, type); })))
3396 /* A & (2**N - 1) <= 2**K - 1 -> A & (2**N - 2**K) == 0
3397 A & (2**N - 1) > 2**K - 1 -> A & (2**N - 2**K) != 0
3399 Note that comparisons
3400 A & (2**N - 1) < 2**K -> A & (2**N - 2**K) == 0
3401 A & (2**N - 1) >= 2**K -> A & (2**N - 2**K) != 0
3402 will be canonicalized to above so there's no need to
3409 (cmp (bit_and@0 @1 INTEGER_CST@2) INTEGER_CST@3)
3410 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)))
3413 tree ty = TREE_TYPE (@0);
3414 unsigned prec = TYPE_PRECISION (ty);
3415 wide_int mask = wi::to_wide (@2, prec);
3416 wide_int rhs = wi::to_wide (@3, prec);
3417 signop sgn = TYPE_SIGN (ty);
3419 (if ((mask & (mask + 1)) == 0 && wi::gt_p (rhs, 0, sgn)
3420 && (rhs & (rhs + 1)) == 0 && wi::ge_p (mask, rhs, sgn))
3421 (eqcmp (bit_and @1 { wide_int_to_tree (ty, mask - rhs); })
3422 { build_zero_cst (ty); }))))))
3424 /* -A CMP -B -> B CMP A. */
3425 (for cmp (tcc_comparison)
3426 scmp (swapped_tcc_comparison)
3428 (cmp (negate @0) (negate @1))
3429 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
3430 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3431 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
3434 (cmp (negate @0) CONSTANT_CLASS_P@1)
3435 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
3436 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3437 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
3438 (with { tree tem = const_unop (NEGATE_EXPR, TREE_TYPE (@0), @1); }
3439 (if (tem && !TREE_OVERFLOW (tem))
3440 (scmp @0 { tem; }))))))
3442 /* Convert ABS_EXPR<x> == 0 or ABS_EXPR<x> != 0 to x == 0 or x != 0. */
3445 (op (abs @0) zerop@1)
3448 /* From fold_sign_changed_comparison and fold_widened_comparison.
3449 FIXME: the lack of symmetry is disturbing. */
3450 (for cmp (simple_comparison)
3452 (cmp (convert@0 @00) (convert?@1 @10))
3453 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
3454 /* Disable this optimization if we're casting a function pointer
3455 type on targets that require function pointer canonicalization. */
3456 && !(targetm.have_canonicalize_funcptr_for_compare ()
3457 && TREE_CODE (TREE_TYPE (@00)) == POINTER_TYPE
3458 && TREE_CODE (TREE_TYPE (TREE_TYPE (@00))) == FUNCTION_TYPE)
3460 (if (TYPE_PRECISION (TREE_TYPE (@00)) == TYPE_PRECISION (TREE_TYPE (@0))
3461 && (TREE_CODE (@10) == INTEGER_CST
3463 && (TYPE_UNSIGNED (TREE_TYPE (@00)) == TYPE_UNSIGNED (TREE_TYPE (@0))
3466 && !POINTER_TYPE_P (TREE_TYPE (@00)))
3467 /* ??? The special-casing of INTEGER_CST conversion was in the original
3468 code and here to avoid a spurious overflow flag on the resulting
3469 constant which fold_convert produces. */
3470 (if (TREE_CODE (@1) == INTEGER_CST)
3471 (cmp @00 { force_fit_type (TREE_TYPE (@00), wi::to_widest (@1), 0,
3472 TREE_OVERFLOW (@1)); })
3473 (cmp @00 (convert @1)))
3475 (if (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (TREE_TYPE (@00)))
3476 /* If possible, express the comparison in the shorter mode. */
3477 (if ((cmp == EQ_EXPR || cmp == NE_EXPR
3478 || TYPE_UNSIGNED (TREE_TYPE (@0)) == TYPE_UNSIGNED (TREE_TYPE (@00))
3479 || (!TYPE_UNSIGNED (TREE_TYPE (@0))
3480 && TYPE_UNSIGNED (TREE_TYPE (@00))))
3481 && (types_match (TREE_TYPE (@10), TREE_TYPE (@00))
3482 || ((TYPE_PRECISION (TREE_TYPE (@00))
3483 >= TYPE_PRECISION (TREE_TYPE (@10)))
3484 && (TYPE_UNSIGNED (TREE_TYPE (@00))
3485 == TYPE_UNSIGNED (TREE_TYPE (@10))))
3486 || (TREE_CODE (@10) == INTEGER_CST
3487 && INTEGRAL_TYPE_P (TREE_TYPE (@00))
3488 && int_fits_type_p (@10, TREE_TYPE (@00)))))
3489 (cmp @00 (convert @10))
3490 (if (TREE_CODE (@10) == INTEGER_CST
3491 && INTEGRAL_TYPE_P (TREE_TYPE (@00))
3492 && !int_fits_type_p (@10, TREE_TYPE (@00)))
3495 tree min = lower_bound_in_type (TREE_TYPE (@10), TREE_TYPE (@00));
3496 tree max = upper_bound_in_type (TREE_TYPE (@10), TREE_TYPE (@00));
3497 bool above = integer_nonzerop (const_binop (LT_EXPR, type, max, @10));
3498 bool below = integer_nonzerop (const_binop (LT_EXPR, type, @10, min));
3500 (if (above || below)
3501 (if (cmp == EQ_EXPR || cmp == NE_EXPR)
3502 { constant_boolean_node (cmp == EQ_EXPR ? false : true, type); }
3503 (if (cmp == LT_EXPR || cmp == LE_EXPR)
3504 { constant_boolean_node (above ? true : false, type); }
3505 (if (cmp == GT_EXPR || cmp == GE_EXPR)
3506 { constant_boolean_node (above ? false : true, type); }))))))))))))
3509 /* A local variable can never be pointed to by
3510 the default SSA name of an incoming parameter.
3511 SSA names are canonicalized to 2nd place. */
3513 (cmp addr@0 SSA_NAME@1)
3514 (if (SSA_NAME_IS_DEFAULT_DEF (@1)
3515 && TREE_CODE (SSA_NAME_VAR (@1)) == PARM_DECL)
3516 (with { tree base = get_base_address (TREE_OPERAND (@0, 0)); }
3517 (if (TREE_CODE (base) == VAR_DECL
3518 && auto_var_in_fn_p (base, current_function_decl))
3519 (if (cmp == NE_EXPR)
3520 { constant_boolean_node (true, type); }
3521 { constant_boolean_node (false, type); }))))))
3523 /* Equality compare simplifications from fold_binary */
3526 /* If we have (A | C) == D where C & ~D != 0, convert this into 0.
3527 Similarly for NE_EXPR. */
3529 (cmp (convert?@3 (bit_ior @0 INTEGER_CST@1)) INTEGER_CST@2)
3530 (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0))
3531 && wi::bit_and_not (wi::to_wide (@1), wi::to_wide (@2)) != 0)
3532 { constant_boolean_node (cmp == NE_EXPR, type); }))
3534 /* (X ^ Y) == 0 becomes X == Y, and (X ^ Y) != 0 becomes X != Y. */
3536 (cmp (bit_xor @0 @1) integer_zerop)
3539 /* (X ^ Y) == Y becomes X == 0.
3540 Likewise (X ^ Y) == X becomes Y == 0. */
3542 (cmp:c (bit_xor:c @0 @1) @0)
3543 (cmp @1 { build_zero_cst (TREE_TYPE (@1)); }))
3545 /* (X ^ C1) op C2 can be rewritten as X op (C1 ^ C2). */
3547 (cmp (convert?@3 (bit_xor @0 INTEGER_CST@1)) INTEGER_CST@2)
3548 (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0)))
3549 (cmp @0 (bit_xor @1 (convert @2)))))
3552 (cmp (convert? addr@0) integer_zerop)
3553 (if (tree_single_nonzero_warnv_p (@0, NULL))
3554 { constant_boolean_node (cmp == NE_EXPR, type); })))
3556 /* If we have (A & C) == C where C is a power of 2, convert this into
3557 (A & C) != 0. Similarly for NE_EXPR. */
3561 (cmp (bit_and@2 @0 integer_pow2p@1) @1)
3562 (icmp @2 { build_zero_cst (TREE_TYPE (@0)); })))
3564 /* If we have (A & C) != 0 ? D : 0 where C and D are powers of 2,
3565 convert this into a shift followed by ANDing with D. */
3568 (ne (bit_and @0 integer_pow2p@1) integer_zerop)
3569 integer_pow2p@2 integer_zerop)
3571 int shift = (wi::exact_log2 (wi::to_wide (@2))
3572 - wi::exact_log2 (wi::to_wide (@1)));
3576 (lshift (convert @0) { build_int_cst (integer_type_node, shift); }) @2)
3578 (convert (rshift @0 { build_int_cst (integer_type_node, -shift); })) @2))))
3580 /* If we have (A & C) != 0 where C is the sign bit of A, convert
3581 this into A < 0. Similarly for (A & C) == 0 into A >= 0. */
3585 (cmp (bit_and (convert?@2 @0) integer_pow2p@1) integer_zerop)
3586 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
3587 && type_has_mode_precision_p (TREE_TYPE (@0))
3588 && element_precision (@2) >= element_precision (@0)
3589 && wi::only_sign_bit_p (wi::to_wide (@1), element_precision (@0)))
3590 (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
3591 (ncmp (convert:stype @0) { build_zero_cst (stype); })))))
3593 /* If we have A < 0 ? C : 0 where C is a power of 2, convert
3594 this into a right shift or sign extension followed by ANDing with C. */
3597 (lt @0 integer_zerop)
3598 integer_pow2p@1 integer_zerop)
3599 (if (!TYPE_UNSIGNED (TREE_TYPE (@0)))
3601 int shift = element_precision (@0) - wi::exact_log2 (wi::to_wide (@1)) - 1;
3605 (convert (rshift @0 { build_int_cst (integer_type_node, shift); }))
3607 /* Otherwise ctype must be wider than TREE_TYPE (@0) and pure
3608 sign extension followed by AND with C will achieve the effect. */
3609 (bit_and (convert @0) @1)))))
3611 /* When the addresses are not directly of decls compare base and offset.
3612 This implements some remaining parts of fold_comparison address
3613 comparisons but still no complete part of it. Still it is good
3614 enough to make fold_stmt not regress when not dispatching to fold_binary. */
3615 (for cmp (simple_comparison)
3617 (cmp (convert1?@2 addr@0) (convert2? addr@1))
3620 poly_int64 off0, off1;
3621 tree base0 = get_addr_base_and_unit_offset (TREE_OPERAND (@0, 0), &off0);
3622 tree base1 = get_addr_base_and_unit_offset (TREE_OPERAND (@1, 0), &off1);
3623 if (base0 && TREE_CODE (base0) == MEM_REF)
3625 off0 += mem_ref_offset (base0).force_shwi ();
3626 base0 = TREE_OPERAND (base0, 0);
3628 if (base1 && TREE_CODE (base1) == MEM_REF)
3630 off1 += mem_ref_offset (base1).force_shwi ();
3631 base1 = TREE_OPERAND (base1, 0);
3634 (if (base0 && base1)
3638 /* Punt in GENERIC on variables with value expressions;
3639 the value expressions might point to fields/elements
3640 of other vars etc. */
3642 && ((VAR_P (base0) && DECL_HAS_VALUE_EXPR_P (base0))
3643 || (VAR_P (base1) && DECL_HAS_VALUE_EXPR_P (base1))))
3645 else if (decl_in_symtab_p (base0)
3646 && decl_in_symtab_p (base1))
3647 equal = symtab_node::get_create (base0)
3648 ->equal_address_to (symtab_node::get_create (base1));
3649 else if ((DECL_P (base0)
3650 || TREE_CODE (base0) == SSA_NAME
3651 || TREE_CODE (base0) == STRING_CST)
3653 || TREE_CODE (base1) == SSA_NAME
3654 || TREE_CODE (base1) == STRING_CST))
3655 equal = (base0 == base1);
3658 && (cmp == EQ_EXPR || cmp == NE_EXPR
3659 /* If the offsets are equal we can ignore overflow. */
3660 || known_eq (off0, off1)
3661 || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
3662 /* Or if we compare using pointers to decls or strings. */
3663 || (POINTER_TYPE_P (TREE_TYPE (@2))
3664 && (DECL_P (base0) || TREE_CODE (base0) == STRING_CST))))
3666 (if (cmp == EQ_EXPR && (known_eq (off0, off1) || known_ne (off0, off1)))
3667 { constant_boolean_node (known_eq (off0, off1), type); })
3668 (if (cmp == NE_EXPR && (known_eq (off0, off1) || known_ne (off0, off1)))
3669 { constant_boolean_node (known_ne (off0, off1), type); })
3670 (if (cmp == LT_EXPR && (known_lt (off0, off1) || known_ge (off0, off1)))
3671 { constant_boolean_node (known_lt (off0, off1), type); })
3672 (if (cmp == LE_EXPR && (known_le (off0, off1) || known_gt (off0, off1)))
3673 { constant_boolean_node (known_le (off0, off1), type); })
3674 (if (cmp == GE_EXPR && (known_ge (off0, off1) || known_lt (off0, off1)))
3675 { constant_boolean_node (known_ge (off0, off1), type); })
3676 (if (cmp == GT_EXPR && (known_gt (off0, off1) || known_le (off0, off1)))
3677 { constant_boolean_node (known_gt (off0, off1), type); }))
3679 && DECL_P (base0) && DECL_P (base1)
3680 /* If we compare this as integers require equal offset. */
3681 && (!INTEGRAL_TYPE_P (TREE_TYPE (@2))
3682 || known_eq (off0, off1)))
3684 (if (cmp == EQ_EXPR)
3685 { constant_boolean_node (false, type); })
3686 (if (cmp == NE_EXPR)
3687 { constant_boolean_node (true, type); })))))))))
3689 /* Simplify pointer equality compares using PTA. */
3693 (if (POINTER_TYPE_P (TREE_TYPE (@0))
3694 && ptrs_compare_unequal (@0, @1))
3695 { neeq == EQ_EXPR ? boolean_false_node : boolean_true_node; })))
3697 /* PR70920: Transform (intptr_t)x eq/ne CST to x eq/ne (typeof x) CST.
3698 and (typeof ptr_cst) x eq/ne ptr_cst to x eq/ne (typeof x) CST.
3699 Disable the transform if either operand is pointer to function.
3700 This broke pr22051-2.c for arm where function pointer
3701 canonicalizaion is not wanted. */
3705 (cmp (convert @0) INTEGER_CST@1)
3706 (if ((POINTER_TYPE_P (TREE_TYPE (@0)) && !FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@0)))
3707 && INTEGRAL_TYPE_P (TREE_TYPE (@1)))
3708 || (INTEGRAL_TYPE_P (TREE_TYPE (@0)) && POINTER_TYPE_P (TREE_TYPE (@1))
3709 && !FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@1)))))
3710 (cmp @0 (convert @1)))))
3712 /* Non-equality compare simplifications from fold_binary */
3713 (for cmp (lt gt le ge)
3714 /* Comparisons with the highest or lowest possible integer of
3715 the specified precision will have known values. */
3717 (cmp (convert?@2 @0) INTEGER_CST@1)
3718 (if ((INTEGRAL_TYPE_P (TREE_TYPE (@1)) || POINTER_TYPE_P (TREE_TYPE (@1)))
3719 && tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@0)))
3722 tree arg1_type = TREE_TYPE (@1);
3723 unsigned int prec = TYPE_PRECISION (arg1_type);
3724 wide_int max = wi::max_value (arg1_type);
3725 wide_int signed_max = wi::max_value (prec, SIGNED);
3726 wide_int min = wi::min_value (arg1_type);
3729 (if (wi::to_wide (@1) == max)
3731 (if (cmp == GT_EXPR)
3732 { constant_boolean_node (false, type); })
3733 (if (cmp == GE_EXPR)
3735 (if (cmp == LE_EXPR)
3736 { constant_boolean_node (true, type); })
3737 (if (cmp == LT_EXPR)
3739 (if (wi::to_wide (@1) == min)
3741 (if (cmp == LT_EXPR)
3742 { constant_boolean_node (false, type); })
3743 (if (cmp == LE_EXPR)
3745 (if (cmp == GE_EXPR)
3746 { constant_boolean_node (true, type); })
3747 (if (cmp == GT_EXPR)
3749 (if (wi::to_wide (@1) == max - 1)
3751 (if (cmp == GT_EXPR)
3752 (eq @2 { wide_int_to_tree (TREE_TYPE (@1), wi::to_wide (@1) + 1); }))
3753 (if (cmp == LE_EXPR)
3754 (ne @2 { wide_int_to_tree (TREE_TYPE (@1), wi::to_wide (@1) + 1); }))))
3755 (if (wi::to_wide (@1) == min + 1)
3757 (if (cmp == GE_EXPR)
3758 (ne @2 { wide_int_to_tree (TREE_TYPE (@1), wi::to_wide (@1) - 1); }))
3759 (if (cmp == LT_EXPR)
3760 (eq @2 { wide_int_to_tree (TREE_TYPE (@1), wi::to_wide (@1) - 1); }))))
3761 (if (wi::to_wide (@1) == signed_max
3762 && TYPE_UNSIGNED (arg1_type)
3763 /* We will flip the signedness of the comparison operator
3764 associated with the mode of @1, so the sign bit is
3765 specified by this mode. Check that @1 is the signed
3766 max associated with this sign bit. */
3767 && prec == GET_MODE_PRECISION (SCALAR_INT_TYPE_MODE (arg1_type))
3768 /* signed_type does not work on pointer types. */
3769 && INTEGRAL_TYPE_P (arg1_type))
3770 /* The following case also applies to X < signed_max+1
3771 and X >= signed_max+1 because previous transformations. */
3772 (if (cmp == LE_EXPR || cmp == GT_EXPR)
3773 (with { tree st = signed_type_for (arg1_type); }
3774 (if (cmp == LE_EXPR)
3775 (ge (convert:st @0) { build_zero_cst (st); })
3776 (lt (convert:st @0) { build_zero_cst (st); }))))))))))
3778 (for cmp (unordered ordered unlt unle ungt unge uneq ltgt)
3779 /* If the second operand is NaN, the result is constant. */
3782 (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
3783 && (cmp != LTGT_EXPR || ! flag_trapping_math))
3784 { constant_boolean_node (cmp == ORDERED_EXPR || cmp == LTGT_EXPR
3785 ? false : true, type); })))
3787 /* bool_var != 0 becomes bool_var. */
3789 (ne @0 integer_zerop)
3790 (if (TREE_CODE (TREE_TYPE (@0)) == BOOLEAN_TYPE
3791 && types_match (type, TREE_TYPE (@0)))
3793 /* bool_var == 1 becomes bool_var. */
3795 (eq @0 integer_onep)
3796 (if (TREE_CODE (TREE_TYPE (@0)) == BOOLEAN_TYPE
3797 && types_match (type, TREE_TYPE (@0)))
3800 bool_var == 0 becomes !bool_var or
3801 bool_var != 1 becomes !bool_var
3802 here because that only is good in assignment context as long
3803 as we require a tcc_comparison in GIMPLE_CONDs where we'd
3804 replace if (x == 0) with tem = ~x; if (tem != 0) which is
3805 clearly less optimal and which we'll transform again in forwprop. */
3807 /* When one argument is a constant, overflow detection can be simplified.
3808 Currently restricted to single use so as not to interfere too much with
3809 ADD_OVERFLOW detection in tree-ssa-math-opts.c.
3810 A + CST CMP A -> A CMP' CST' */
3811 (for cmp (lt le ge gt)
3814 (cmp:c (plus@2 @0 INTEGER_CST@1) @0)
3815 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
3816 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))
3817 && wi::to_wide (@1) != 0
3819 (with { unsigned int prec = TYPE_PRECISION (TREE_TYPE (@0)); }
3820 (out @0 { wide_int_to_tree (TREE_TYPE (@0),
3821 wi::max_value (prec, UNSIGNED)
3822 - wi::to_wide (@1)); })))))
3824 /* To detect overflow in unsigned A - B, A < B is simpler than A - B > A.
3825 However, the detection logic for SUB_OVERFLOW in tree-ssa-math-opts.c
3826 expects the long form, so we restrict the transformation for now. */
3829 (cmp:c (minus@2 @0 @1) @0)
3830 (if (single_use (@2)
3831 && ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3832 && TYPE_UNSIGNED (TREE_TYPE (@0))
3833 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
3836 /* Testing for overflow is unnecessary if we already know the result. */
3841 (cmp:c (realpart (IFN_SUB_OVERFLOW@2 @0 @1)) @0)
3842 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
3843 && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
3844 (out (imagpart @2) { build_zero_cst (TREE_TYPE (@0)); }))))
3849 (cmp:c (realpart (IFN_ADD_OVERFLOW:c@2 @0 @1)) @0)
3850 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
3851 && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
3852 (out (imagpart @2) { build_zero_cst (TREE_TYPE (@0)); }))))
3854 /* For unsigned operands, -1 / B < A checks whether A * B would overflow.
3855 Simplify it to __builtin_mul_overflow (A, B, <unused>). */
3859 (cmp:c (trunc_div:s integer_all_onesp @1) @0)
3860 (if (TYPE_UNSIGNED (TREE_TYPE (@0)) && !VECTOR_TYPE_P (TREE_TYPE (@0)))
3861 (with { tree t = TREE_TYPE (@0), cpx = build_complex_type (t); }
3862 (out (imagpart (IFN_MUL_OVERFLOW:cpx @0 @1)) { build_zero_cst (t); })))))
3864 /* Simplification of math builtins. These rules must all be optimizations
3865 as well as IL simplifications. If there is a possibility that the new
3866 form could be a pessimization, the rule should go in the canonicalization
3867 section that follows this one.
3869 Rules can generally go in this section if they satisfy one of
3872 - the rule describes an identity
3874 - the rule replaces calls with something as simple as addition or
3877 - the rule contains unary calls only and simplifies the surrounding
3878 arithmetic. (The idea here is to exclude non-unary calls in which
3879 one operand is constant and in which the call is known to be cheap
3880 when the operand has that value.) */
3882 (if (flag_unsafe_math_optimizations)
3883 /* Simplify sqrt(x) * sqrt(x) -> x. */
3885 (mult (SQRT_ALL@1 @0) @1)
3886 (if (!HONOR_SNANS (type))
3889 (for op (plus minus)
3890 /* Simplify (A / C) +- (B / C) -> (A +- B) / C. */
3894 (rdiv (op @0 @2) @1)))
3896 /* Simplify sqrt(x) * sqrt(y) -> sqrt(x*y). */
3897 (for root (SQRT CBRT)
3899 (mult (root:s @0) (root:s @1))
3900 (root (mult @0 @1))))
3902 /* Simplify expN(x) * expN(y) -> expN(x+y). */
3903 (for exps (EXP EXP2 EXP10 POW10)
3905 (mult (exps:s @0) (exps:s @1))
3906 (exps (plus @0 @1))))
3908 /* Simplify a/root(b/c) into a*root(c/b). */
3909 (for root (SQRT CBRT)
3911 (rdiv @0 (root:s (rdiv:s @1 @2)))
3912 (mult @0 (root (rdiv @2 @1)))))
3914 /* Simplify x/expN(y) into x*expN(-y). */
3915 (for exps (EXP EXP2 EXP10 POW10)
3917 (rdiv @0 (exps:s @1))
3918 (mult @0 (exps (negate @1)))))
3920 (for logs (LOG LOG2 LOG10 LOG10)
3921 exps (EXP EXP2 EXP10 POW10)
3922 /* logN(expN(x)) -> x. */
3926 /* expN(logN(x)) -> x. */
3931 /* Optimize logN(func()) for various exponential functions. We
3932 want to determine the value "x" and the power "exponent" in
3933 order to transform logN(x**exponent) into exponent*logN(x). */
3934 (for logs (LOG LOG LOG LOG2 LOG2 LOG2 LOG10 LOG10)
3935 exps (EXP2 EXP10 POW10 EXP EXP10 POW10 EXP EXP2)
3938 (if (SCALAR_FLOAT_TYPE_P (type))
3944 /* Prepare to do logN(exp(exponent)) -> exponent*logN(e). */
3945 x = build_real_truncate (type, dconst_e ());
3948 /* Prepare to do logN(exp2(exponent)) -> exponent*logN(2). */
3949 x = build_real (type, dconst2);
3953 /* Prepare to do logN(exp10(exponent)) -> exponent*logN(10). */
3955 REAL_VALUE_TYPE dconst10;
3956 real_from_integer (&dconst10, VOIDmode, 10, SIGNED);
3957 x = build_real (type, dconst10);
3964 (mult (logs { x; }) @0)))))
3972 (if (SCALAR_FLOAT_TYPE_P (type))
3978 /* Prepare to do logN(sqrt(x)) -> 0.5*logN(x). */
3979 x = build_real (type, dconsthalf);
3982 /* Prepare to do logN(cbrt(x)) -> (1/3)*logN(x). */
3983 x = build_real_truncate (type, dconst_third ());
3989 (mult { x; } (logs @0))))))
3991 /* logN(pow(x,exponent)) -> exponent*logN(x). */
3992 (for logs (LOG LOG2 LOG10)
3996 (mult @1 (logs @0))))
3998 /* pow(C,x) -> exp(log(C)*x) if C > 0,
3999 or if C is a positive power of 2,
4000 pow(C,x) -> exp2(log2(C)*x). */
4007 (pows REAL_CST@0 @1)
4008 (if (real_compare (GT_EXPR, TREE_REAL_CST_PTR (@0), &dconst0)
4009 && real_isfinite (TREE_REAL_CST_PTR (@0))
4010 /* As libmvec doesn't have a vectorized exp2, defer optimizing
4011 the use_exp2 case until after vectorization. It seems actually
4012 beneficial for all constants to postpone this until later,
4013 because exp(log(C)*x), while faster, will have worse precision
4014 and if x folds into a constant too, that is unnecessary
4016 && canonicalize_math_after_vectorization_p ())
4018 const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (@0);
4019 bool use_exp2 = false;
4020 if (targetm.libc_has_function (function_c99_misc)
4021 && value->cl == rvc_normal)
4023 REAL_VALUE_TYPE frac_rvt = *value;
4024 SET_REAL_EXP (&frac_rvt, 1);
4025 if (real_equal (&frac_rvt, &dconst1))
4030 (exps (mult (logs @0) @1))
4031 (exp2s (mult (log2s @0) @1)))))))
4033 /* pow(C,x)*expN(y) -> expN(logN(C)*x+y) if C > 0. */
4035 exps (EXP EXP2 EXP10 POW10)
4036 logs (LOG LOG2 LOG10 LOG10)
4038 (mult:c (pows:s REAL_CST@0 @1) (exps:s @2))
4039 (if (real_compare (GT_EXPR, TREE_REAL_CST_PTR (@0), &dconst0)
4040 && real_isfinite (TREE_REAL_CST_PTR (@0)))
4041 (exps (plus (mult (logs @0) @1) @2)))))
4046 exps (EXP EXP2 EXP10 POW10)
4047 /* sqrt(expN(x)) -> expN(x*0.5). */
4050 (exps (mult @0 { build_real (type, dconsthalf); })))
4051 /* cbrt(expN(x)) -> expN(x/3). */
4054 (exps (mult @0 { build_real_truncate (type, dconst_third ()); })))
4055 /* pow(expN(x), y) -> expN(x*y). */
4058 (exps (mult @0 @1))))
4060 /* tan(atan(x)) -> x. */
4067 /* cabs(x+0i) or cabs(0+xi) -> abs(x). */
4069 (CABS (complex:C @0 real_zerop@1))
4072 /* trunc(trunc(x)) -> trunc(x), etc. */
4073 (for fns (TRUNC_ALL FLOOR_ALL CEIL_ALL ROUND_ALL NEARBYINT_ALL RINT_ALL)
4077 /* f(x) -> x if x is integer valued and f does nothing for such values. */
4078 (for fns (TRUNC_ALL FLOOR_ALL CEIL_ALL ROUND_ALL NEARBYINT_ALL RINT_ALL)
4080 (fns integer_valued_real_p@0)
4083 /* hypot(x,0) and hypot(0,x) -> abs(x). */
4085 (HYPOT:c @0 real_zerop@1)
4088 /* pow(1,x) -> 1. */
4090 (POW real_onep@0 @1)
4094 /* copysign(x,x) -> x. */
4095 (COPYSIGN_ALL @0 @0)
4099 /* copysign(x,y) -> fabs(x) if y is nonnegative. */
4100 (COPYSIGN_ALL @0 tree_expr_nonnegative_p@1)
4103 (for scale (LDEXP SCALBN SCALBLN)
4104 /* ldexp(0, x) -> 0. */
4106 (scale real_zerop@0 @1)
4108 /* ldexp(x, 0) -> x. */
4110 (scale @0 integer_zerop@1)
4112 /* ldexp(x, y) -> x if x is +-Inf or NaN. */
4114 (scale REAL_CST@0 @1)
4115 (if (!real_isfinite (TREE_REAL_CST_PTR (@0)))
4118 /* Canonicalization of sequences of math builtins. These rules represent
4119 IL simplifications but are not necessarily optimizations.
4121 The sincos pass is responsible for picking "optimal" implementations
4122 of math builtins, which may be more complicated and can sometimes go
4123 the other way, e.g. converting pow into a sequence of sqrts.
4124 We only want to do these canonicalizations before the pass has run. */
4126 (if (flag_unsafe_math_optimizations && canonicalize_math_p ())
4127 /* Simplify tan(x) * cos(x) -> sin(x). */
4129 (mult:c (TAN:s @0) (COS:s @0))
4132 /* Simplify x * pow(x,c) -> pow(x,c+1). */
4134 (mult:c @0 (POW:s @0 REAL_CST@1))
4135 (if (!TREE_OVERFLOW (@1))
4136 (POW @0 (plus @1 { build_one_cst (type); }))))
4138 /* Simplify sin(x) / cos(x) -> tan(x). */
4140 (rdiv (SIN:s @0) (COS:s @0))
4143 /* Simplify cos(x) / sin(x) -> 1 / tan(x). */
4145 (rdiv (COS:s @0) (SIN:s @0))
4146 (rdiv { build_one_cst (type); } (TAN @0)))
4148 /* Simplify sin(x) / tan(x) -> cos(x). */
4150 (rdiv (SIN:s @0) (TAN:s @0))
4151 (if (! HONOR_NANS (@0)
4152 && ! HONOR_INFINITIES (@0))
4155 /* Simplify tan(x) / sin(x) -> 1.0 / cos(x). */
4157 (rdiv (TAN:s @0) (SIN:s @0))
4158 (if (! HONOR_NANS (@0)
4159 && ! HONOR_INFINITIES (@0))
4160 (rdiv { build_one_cst (type); } (COS @0))))
4162 /* Simplify pow(x,y) * pow(x,z) -> pow(x,y+z). */
4164 (mult (POW:s @0 @1) (POW:s @0 @2))
4165 (POW @0 (plus @1 @2)))
4167 /* Simplify pow(x,y) * pow(z,y) -> pow(x*z,y). */
4169 (mult (POW:s @0 @1) (POW:s @2 @1))
4170 (POW (mult @0 @2) @1))
4172 /* Simplify powi(x,y) * powi(z,y) -> powi(x*z,y). */
4174 (mult (POWI:s @0 @1) (POWI:s @2 @1))
4175 (POWI (mult @0 @2) @1))
4177 /* Simplify pow(x,c) / x -> pow(x,c-1). */
4179 (rdiv (POW:s @0 REAL_CST@1) @0)
4180 (if (!TREE_OVERFLOW (@1))
4181 (POW @0 (minus @1 { build_one_cst (type); }))))
4183 /* Simplify x / pow (y,z) -> x * pow(y,-z). */
4185 (rdiv @0 (POW:s @1 @2))
4186 (mult @0 (POW @1 (negate @2))))
4191 /* sqrt(sqrt(x)) -> pow(x,1/4). */
4194 (pows @0 { build_real (type, dconst_quarter ()); }))
4195 /* sqrt(cbrt(x)) -> pow(x,1/6). */
4198 (pows @0 { build_real_truncate (type, dconst_sixth ()); }))
4199 /* cbrt(sqrt(x)) -> pow(x,1/6). */
4202 (pows @0 { build_real_truncate (type, dconst_sixth ()); }))
4203 /* cbrt(cbrt(x)) -> pow(x,1/9), iff x is nonnegative. */
4205 (cbrts (cbrts tree_expr_nonnegative_p@0))
4206 (pows @0 { build_real_truncate (type, dconst_ninth ()); }))
4207 /* sqrt(pow(x,y)) -> pow(|x|,y*0.5). */
4209 (sqrts (pows @0 @1))
4210 (pows (abs @0) (mult @1 { build_real (type, dconsthalf); })))
4211 /* cbrt(pow(x,y)) -> pow(x,y/3), iff x is nonnegative. */
4213 (cbrts (pows tree_expr_nonnegative_p@0 @1))
4214 (pows @0 (mult @1 { build_real_truncate (type, dconst_third ()); })))
4215 /* pow(sqrt(x),y) -> pow(x,y*0.5). */
4217 (pows (sqrts @0) @1)
4218 (pows @0 (mult @1 { build_real (type, dconsthalf); })))
4219 /* pow(cbrt(x),y) -> pow(x,y/3) iff x is nonnegative. */
4221 (pows (cbrts tree_expr_nonnegative_p@0) @1)
4222 (pows @0 (mult @1 { build_real_truncate (type, dconst_third ()); })))
4223 /* pow(pow(x,y),z) -> pow(x,y*z) iff x is nonnegative. */
4225 (pows (pows tree_expr_nonnegative_p@0 @1) @2)
4226 (pows @0 (mult @1 @2))))
4228 /* cabs(x+xi) -> fabs(x)*sqrt(2). */
4230 (CABS (complex @0 @0))
4231 (mult (abs @0) { build_real_truncate (type, dconst_sqrt2 ()); }))
4233 /* hypot(x,x) -> fabs(x)*sqrt(2). */
4236 (mult (abs @0) { build_real_truncate (type, dconst_sqrt2 ()); }))
4238 /* cexp(x+yi) -> exp(x)*cexpi(y). */
4243 (cexps compositional_complex@0)
4244 (if (targetm.libc_has_function (function_c99_math_complex))
4246 (mult (exps@1 (realpart @0)) (realpart (cexpis:type@2 (imagpart @0))))
4247 (mult @1 (imagpart @2)))))))
4249 (if (canonicalize_math_p ())
4250 /* floor(x) -> trunc(x) if x is nonnegative. */
4251 (for floors (FLOOR_ALL)
4254 (floors tree_expr_nonnegative_p@0)
4257 (match double_value_p
4259 (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == double_type_node)))
4260 (for froms (BUILT_IN_TRUNCL
4272 /* truncl(extend(x)) -> extend(trunc(x)), etc., if x is a double. */
4273 (if (optimize && canonicalize_math_p ())
4275 (froms (convert double_value_p@0))
4276 (convert (tos @0)))))
4278 (match float_value_p
4280 (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == float_type_node)))
4281 (for froms (BUILT_IN_TRUNCL BUILT_IN_TRUNC
4282 BUILT_IN_FLOORL BUILT_IN_FLOOR
4283 BUILT_IN_CEILL BUILT_IN_CEIL
4284 BUILT_IN_ROUNDL BUILT_IN_ROUND
4285 BUILT_IN_NEARBYINTL BUILT_IN_NEARBYINT
4286 BUILT_IN_RINTL BUILT_IN_RINT)
4287 tos (BUILT_IN_TRUNCF BUILT_IN_TRUNCF
4288 BUILT_IN_FLOORF BUILT_IN_FLOORF
4289 BUILT_IN_CEILF BUILT_IN_CEILF
4290 BUILT_IN_ROUNDF BUILT_IN_ROUNDF
4291 BUILT_IN_NEARBYINTF BUILT_IN_NEARBYINTF
4292 BUILT_IN_RINTF BUILT_IN_RINTF)
4293 /* truncl(extend(x)) and trunc(extend(x)) -> extend(truncf(x)), etc.,
4295 (if (optimize && canonicalize_math_p ()
4296 && targetm.libc_has_function (function_c99_misc))
4298 (froms (convert float_value_p@0))
4299 (convert (tos @0)))))
4301 (for froms (XFLOORL XCEILL XROUNDL XRINTL)
4302 tos (XFLOOR XCEIL XROUND XRINT)
4303 /* llfloorl(extend(x)) -> llfloor(x), etc., if x is a double. */
4304 (if (optimize && canonicalize_math_p ())
4306 (froms (convert double_value_p@0))
4309 (for froms (XFLOORL XCEILL XROUNDL XRINTL
4310 XFLOOR XCEIL XROUND XRINT)
4311 tos (XFLOORF XCEILF XROUNDF XRINTF)
4312 /* llfloorl(extend(x)) and llfloor(extend(x)) -> llfloorf(x), etc.,
4314 (if (optimize && canonicalize_math_p ())
4316 (froms (convert float_value_p@0))
4319 (if (canonicalize_math_p ())
4320 /* xfloor(x) -> fix_trunc(x) if x is nonnegative. */
4321 (for floors (IFLOOR LFLOOR LLFLOOR)
4323 (floors tree_expr_nonnegative_p@0)
4326 (if (canonicalize_math_p ())
4327 /* xfloor(x) -> fix_trunc(x), etc., if x is integer valued. */
4328 (for fns (IFLOOR LFLOOR LLFLOOR
4330 IROUND LROUND LLROUND)
4332 (fns integer_valued_real_p@0)
4334 (if (!flag_errno_math)
4335 /* xrint(x) -> fix_trunc(x), etc., if x is integer valued. */
4336 (for rints (IRINT LRINT LLRINT)
4338 (rints integer_valued_real_p@0)
4341 (if (canonicalize_math_p ())
4342 (for ifn (IFLOOR ICEIL IROUND IRINT)
4343 lfn (LFLOOR LCEIL LROUND LRINT)
4344 llfn (LLFLOOR LLCEIL LLROUND LLRINT)
4345 /* Canonicalize iround (x) to lround (x) on ILP32 targets where
4346 sizeof (int) == sizeof (long). */
4347 (if (TYPE_PRECISION (integer_type_node)
4348 == TYPE_PRECISION (long_integer_type_node))
4351 (lfn:long_integer_type_node @0)))
4352 /* Canonicalize llround (x) to lround (x) on LP64 targets where
4353 sizeof (long long) == sizeof (long). */
4354 (if (TYPE_PRECISION (long_long_integer_type_node)
4355 == TYPE_PRECISION (long_integer_type_node))
4358 (lfn:long_integer_type_node @0)))))
4360 /* cproj(x) -> x if we're ignoring infinities. */
4363 (if (!HONOR_INFINITIES (type))
4366 /* If the real part is inf and the imag part is known to be
4367 nonnegative, return (inf + 0i). */
4369 (CPROJ (complex REAL_CST@0 tree_expr_nonnegative_p@1))
4370 (if (real_isinf (TREE_REAL_CST_PTR (@0)))
4371 { build_complex_inf (type, false); }))
4373 /* If the imag part is inf, return (inf+I*copysign(0,imag)). */
4375 (CPROJ (complex @0 REAL_CST@1))
4376 (if (real_isinf (TREE_REAL_CST_PTR (@1)))
4377 { build_complex_inf (type, TREE_REAL_CST_PTR (@1)->sign); }))
4383 (pows @0 REAL_CST@1)
4385 const REAL_VALUE_TYPE *value = TREE_REAL_CST_PTR (@1);
4386 REAL_VALUE_TYPE tmp;
4389 /* pow(x,0) -> 1. */
4390 (if (real_equal (value, &dconst0))
4391 { build_real (type, dconst1); })
4392 /* pow(x,1) -> x. */
4393 (if (real_equal (value, &dconst1))
4395 /* pow(x,-1) -> 1/x. */
4396 (if (real_equal (value, &dconstm1))
4397 (rdiv { build_real (type, dconst1); } @0))
4398 /* pow(x,0.5) -> sqrt(x). */
4399 (if (flag_unsafe_math_optimizations
4400 && canonicalize_math_p ()
4401 && real_equal (value, &dconsthalf))
4403 /* pow(x,1/3) -> cbrt(x). */
4404 (if (flag_unsafe_math_optimizations
4405 && canonicalize_math_p ()
4406 && (tmp = real_value_truncate (TYPE_MODE (type), dconst_third ()),
4407 real_equal (value, &tmp)))
4410 /* powi(1,x) -> 1. */
4412 (POWI real_onep@0 @1)
4416 (POWI @0 INTEGER_CST@1)
4418 /* powi(x,0) -> 1. */
4419 (if (wi::to_wide (@1) == 0)
4420 { build_real (type, dconst1); })
4421 /* powi(x,1) -> x. */
4422 (if (wi::to_wide (@1) == 1)
4424 /* powi(x,-1) -> 1/x. */
4425 (if (wi::to_wide (@1) == -1)
4426 (rdiv { build_real (type, dconst1); } @0))))
4428 /* Narrowing of arithmetic and logical operations.
4430 These are conceptually similar to the transformations performed for
4431 the C/C++ front-ends by shorten_binary_op and shorten_compare. Long
4432 term we want to move all that code out of the front-ends into here. */
4434 /* If we have a narrowing conversion of an arithmetic operation where
4435 both operands are widening conversions from the same type as the outer
4436 narrowing conversion. Then convert the innermost operands to a suitable
4437 unsigned type (to avoid introducing undefined behavior), perform the
4438 operation and convert the result to the desired type. */
4439 (for op (plus minus)
4441 (convert (op:s (convert@2 @0) (convert?@3 @1)))
4442 (if (INTEGRAL_TYPE_P (type)
4443 /* We check for type compatibility between @0 and @1 below,
4444 so there's no need to check that @1/@3 are integral types. */
4445 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
4446 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
4447 /* The precision of the type of each operand must match the
4448 precision of the mode of each operand, similarly for the
4450 && type_has_mode_precision_p (TREE_TYPE (@0))
4451 && type_has_mode_precision_p (TREE_TYPE (@1))
4452 && type_has_mode_precision_p (type)
4453 /* The inner conversion must be a widening conversion. */
4454 && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
4455 && types_match (@0, type)
4456 && (types_match (@0, @1)
4457 /* Or the second operand is const integer or converted const
4458 integer from valueize. */
4459 || TREE_CODE (@1) == INTEGER_CST))
4460 (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
4461 (op @0 (convert @1))
4462 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
4463 (convert (op (convert:utype @0)
4464 (convert:utype @1))))))))
4466 /* This is another case of narrowing, specifically when there's an outer
4467 BIT_AND_EXPR which masks off bits outside the type of the innermost
4468 operands. Like the previous case we have to convert the operands
4469 to unsigned types to avoid introducing undefined behavior for the
4470 arithmetic operation. */
4471 (for op (minus plus)
4473 (bit_and (op:s (convert@2 @0) (convert@3 @1)) INTEGER_CST@4)
4474 (if (INTEGRAL_TYPE_P (type)
4475 /* We check for type compatibility between @0 and @1 below,
4476 so there's no need to check that @1/@3 are integral types. */
4477 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
4478 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
4479 /* The precision of the type of each operand must match the
4480 precision of the mode of each operand, similarly for the
4482 && type_has_mode_precision_p (TREE_TYPE (@0))
4483 && type_has_mode_precision_p (TREE_TYPE (@1))
4484 && type_has_mode_precision_p (type)
4485 /* The inner conversion must be a widening conversion. */
4486 && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
4487 && types_match (@0, @1)
4488 && (tree_int_cst_min_precision (@4, TYPE_SIGN (TREE_TYPE (@0)))
4489 <= TYPE_PRECISION (TREE_TYPE (@0)))
4490 && (wi::to_wide (@4)
4491 & wi::mask (TYPE_PRECISION (TREE_TYPE (@0)),
4492 true, TYPE_PRECISION (type))) == 0)
4493 (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
4494 (with { tree ntype = TREE_TYPE (@0); }
4495 (convert (bit_and (op @0 @1) (convert:ntype @4))))
4496 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
4497 (convert (bit_and (op (convert:utype @0) (convert:utype @1))
4498 (convert:utype @4))))))))
4500 /* Transform (@0 < @1 and @0 < @2) to use min,
4501 (@0 > @1 and @0 > @2) to use max */
4502 (for op (lt le gt ge)
4503 ext (min min max max)
4505 (bit_and (op:cs @0 @1) (op:cs @0 @2))
4506 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
4507 && TREE_CODE (@0) != INTEGER_CST)
4508 (op @0 (ext @1 @2)))))
4511 /* signbit(x) -> 0 if x is nonnegative. */
4512 (SIGNBIT tree_expr_nonnegative_p@0)
4513 { integer_zero_node; })
4516 /* signbit(x) -> x<0 if x doesn't have signed zeros. */
4518 (if (!HONOR_SIGNED_ZEROS (@0))
4519 (convert (lt @0 { build_real (TREE_TYPE (@0), dconst0); }))))
4521 /* Transform comparisons of the form X +- C1 CMP C2 to X CMP C2 -+ C1. */
4523 (for op (plus minus)
4526 (cmp (op@3 @0 INTEGER_CST@1) INTEGER_CST@2)
4527 (if (!TREE_OVERFLOW (@1) && !TREE_OVERFLOW (@2)
4528 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0))
4529 && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@0))
4530 && !TYPE_SATURATING (TREE_TYPE (@0)))
4531 (with { tree res = int_const_binop (rop, @2, @1); }
4532 (if (TREE_OVERFLOW (res)
4533 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
4534 { constant_boolean_node (cmp == NE_EXPR, type); }
4535 (if (single_use (@3))
4536 (cmp @0 { TREE_OVERFLOW (res)
4537 ? drop_tree_overflow (res) : res; }))))))))
4538 (for cmp (lt le gt ge)
4539 (for op (plus minus)
4542 (cmp (op@3 @0 INTEGER_CST@1) INTEGER_CST@2)
4543 (if (!TREE_OVERFLOW (@1) && !TREE_OVERFLOW (@2)
4544 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
4545 (with { tree res = int_const_binop (rop, @2, @1); }
4546 (if (TREE_OVERFLOW (res))
4548 fold_overflow_warning (("assuming signed overflow does not occur "
4549 "when simplifying conditional to constant"),
4550 WARN_STRICT_OVERFLOW_CONDITIONAL);
4551 bool less = cmp == LE_EXPR || cmp == LT_EXPR;
4552 /* wi::ges_p (@2, 0) should be sufficient for a signed type. */
4553 bool ovf_high = wi::lt_p (wi::to_wide (@1), 0,
4554 TYPE_SIGN (TREE_TYPE (@1)))
4555 != (op == MINUS_EXPR);
4556 constant_boolean_node (less == ovf_high, type);
4558 (if (single_use (@3))
4561 fold_overflow_warning (("assuming signed overflow does not occur "
4562 "when changing X +- C1 cmp C2 to "
4564 WARN_STRICT_OVERFLOW_COMPARISON);
4566 (cmp @0 { res; })))))))))
4568 /* Canonicalizations of BIT_FIELD_REFs. */
4571 (BIT_FIELD_REF @0 @1 @2)
4573 (if (TREE_CODE (TREE_TYPE (@0)) == COMPLEX_TYPE
4574 && tree_int_cst_equal (@1, TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))))
4576 (if (integer_zerop (@2))
4577 (view_convert (realpart @0)))
4578 (if (tree_int_cst_equal (@2, TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))))
4579 (view_convert (imagpart @0)))))
4580 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
4581 && INTEGRAL_TYPE_P (type)
4582 /* On GIMPLE this should only apply to register arguments. */
4583 && (! GIMPLE || is_gimple_reg (@0))
4584 /* A bit-field-ref that referenced the full argument can be stripped. */
4585 && ((compare_tree_int (@1, TYPE_PRECISION (TREE_TYPE (@0))) == 0
4586 && integer_zerop (@2))
4587 /* Low-parts can be reduced to integral conversions.
4588 ??? The following doesn't work for PDP endian. */
4589 || (BYTES_BIG_ENDIAN == WORDS_BIG_ENDIAN
4590 /* Don't even think about BITS_BIG_ENDIAN. */
4591 && TYPE_PRECISION (TREE_TYPE (@0)) % BITS_PER_UNIT == 0
4592 && TYPE_PRECISION (type) % BITS_PER_UNIT == 0
4593 && compare_tree_int (@2, (BYTES_BIG_ENDIAN
4594 ? (TYPE_PRECISION (TREE_TYPE (@0))
4595 - TYPE_PRECISION (type))
4599 /* Simplify vector extracts. */
4602 (BIT_FIELD_REF CONSTRUCTOR@0 @1 @2)
4603 (if (VECTOR_TYPE_P (TREE_TYPE (@0))
4604 && (types_match (type, TREE_TYPE (TREE_TYPE (@0)))
4605 || (VECTOR_TYPE_P (type)
4606 && types_match (TREE_TYPE (type), TREE_TYPE (TREE_TYPE (@0))))))
4609 tree ctor = (TREE_CODE (@0) == SSA_NAME
4610 ? gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0)) : @0);
4611 tree eltype = TREE_TYPE (TREE_TYPE (ctor));
4612 unsigned HOST_WIDE_INT width = tree_to_uhwi (TYPE_SIZE (eltype));
4613 unsigned HOST_WIDE_INT n = tree_to_uhwi (@1);
4614 unsigned HOST_WIDE_INT idx = tree_to_uhwi (@2);
4617 && (idx % width) == 0
4619 && known_le ((idx + n) / width,
4620 TYPE_VECTOR_SUBPARTS (TREE_TYPE (ctor))))
4625 /* Constructor elements can be subvectors. */
4627 if (CONSTRUCTOR_NELTS (ctor) != 0)
4629 tree cons_elem = TREE_TYPE (CONSTRUCTOR_ELT (ctor, 0)->value);
4630 if (TREE_CODE (cons_elem) == VECTOR_TYPE)
4631 k = TYPE_VECTOR_SUBPARTS (cons_elem);
4633 unsigned HOST_WIDE_INT elt, count, const_k;
4636 /* We keep an exact subset of the constructor elements. */
4637 (if (multiple_p (idx, k, &elt) && multiple_p (n, k, &count))
4638 (if (CONSTRUCTOR_NELTS (ctor) == 0)
4639 { build_constructor (type, NULL); }
4641 (if (elt < CONSTRUCTOR_NELTS (ctor))
4642 { CONSTRUCTOR_ELT (ctor, elt)->value; }
4643 { build_zero_cst (type); })
4645 vec<constructor_elt, va_gc> *vals;
4646 vec_alloc (vals, count);
4647 for (unsigned i = 0;
4648 i < count && elt + i < CONSTRUCTOR_NELTS (ctor); ++i)
4649 CONSTRUCTOR_APPEND_ELT (vals, NULL_TREE,
4650 CONSTRUCTOR_ELT (ctor, elt + i)->value);
4651 build_constructor (type, vals);
4653 /* The bitfield references a single constructor element. */
4654 (if (k.is_constant (&const_k)
4655 && idx + n <= (idx / const_k + 1) * const_k)
4657 (if (CONSTRUCTOR_NELTS (ctor) <= idx / const_k)
4658 { build_zero_cst (type); })
4660 { CONSTRUCTOR_ELT (ctor, idx / const_k)->value; })
4661 (BIT_FIELD_REF { CONSTRUCTOR_ELT (ctor, idx / const_k)->value; }
4662 @1 { bitsize_int ((idx % const_k) * width); })))))))))
4664 /* Simplify a bit extraction from a bit insertion for the cases with
4665 the inserted element fully covering the extraction or the insertion
4666 not touching the extraction. */
4668 (BIT_FIELD_REF (bit_insert @0 @1 @ipos) @rsize @rpos)
4671 unsigned HOST_WIDE_INT isize;
4672 if (INTEGRAL_TYPE_P (TREE_TYPE (@1)))
4673 isize = TYPE_PRECISION (TREE_TYPE (@1));
4675 isize = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (@1)));
4678 (if (wi::leu_p (wi::to_wide (@ipos), wi::to_wide (@rpos))
4679 && wi::leu_p (wi::to_wide (@rpos) + wi::to_wide (@rsize),
4680 wi::to_wide (@ipos) + isize))
4681 (BIT_FIELD_REF @1 @rsize { wide_int_to_tree (bitsizetype,
4683 - wi::to_wide (@ipos)); }))
4684 (if (wi::geu_p (wi::to_wide (@ipos),
4685 wi::to_wide (@rpos) + wi::to_wide (@rsize))
4686 || wi::geu_p (wi::to_wide (@rpos),
4687 wi::to_wide (@ipos) + isize))
4688 (BIT_FIELD_REF @0 @rsize @rpos)))))