1 /* Match-and-simplify patterns for shared GENERIC and GIMPLE folding.
2 This file is consumed by genmatch which produces gimple-match.c
3 and generic-match.c from it.
5 Copyright (C) 2014-2017 Free Software Foundation, Inc.
6 Contributed by Richard Biener <rguenther@suse.de>
7 and Prathamesh Kulkarni <bilbotheelffriend@gmail.com>
9 This file is part of GCC.
11 GCC is free software; you can redistribute it and/or modify it under
12 the terms of the GNU General Public License as published by the Free
13 Software Foundation; either version 3, or (at your option) any later
16 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
17 WARRANTY; without even the implied warranty of MERCHANTABILITY or
18 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21 You should have received a copy of the GNU General Public License
22 along with GCC; see the file COPYING3. If not see
23 <http://www.gnu.org/licenses/>. */
26 /* Generic tree predicates we inherit. */
28 integer_onep integer_zerop integer_all_onesp integer_minus_onep
29 integer_each_onep integer_truep integer_nonzerop
30 real_zerop real_onep real_minus_onep
33 tree_expr_nonnegative_p
40 (define_operator_list tcc_comparison
41 lt le eq ne ge gt unordered ordered unlt unle ungt unge uneq ltgt)
42 (define_operator_list inverted_tcc_comparison
43 ge gt ne eq lt le ordered unordered ge gt le lt ltgt uneq)
44 (define_operator_list inverted_tcc_comparison_with_nans
45 unge ungt ne eq unlt unle ordered unordered ge gt le lt ltgt uneq)
46 (define_operator_list swapped_tcc_comparison
47 gt ge eq ne le lt unordered ordered ungt unge unlt unle uneq ltgt)
48 (define_operator_list simple_comparison lt le eq ne ge gt)
49 (define_operator_list swapped_simple_comparison gt ge eq ne le lt)
51 #include "cfn-operators.pd"
53 /* Define operand lists for math rounding functions {,i,l,ll}FN,
54 where the versions prefixed with "i" return an int, those prefixed with
55 "l" return a long and those prefixed with "ll" return a long long.
57 Also define operand lists:
59 X<FN>F for all float functions, in the order i, l, ll
60 X<FN> for all double functions, in the same order
61 X<FN>L for all long double functions, in the same order. */
62 #define DEFINE_INT_AND_FLOAT_ROUND_FN(FN) \
63 (define_operator_list X##FN##F BUILT_IN_I##FN##F \
66 (define_operator_list X##FN BUILT_IN_I##FN \
69 (define_operator_list X##FN##L BUILT_IN_I##FN##L \
73 DEFINE_INT_AND_FLOAT_ROUND_FN (FLOOR)
74 DEFINE_INT_AND_FLOAT_ROUND_FN (CEIL)
75 DEFINE_INT_AND_FLOAT_ROUND_FN (ROUND)
76 DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
78 /* As opposed to convert?, this still creates a single pattern, so
79 it is not a suitable replacement for convert? in all cases. */
80 (match (nop_convert @0)
82 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))))
83 (match (nop_convert @0)
85 (if (VECTOR_TYPE_P (type) && VECTOR_TYPE_P (TREE_TYPE (@0))
86 && TYPE_VECTOR_SUBPARTS (type) == TYPE_VECTOR_SUBPARTS (TREE_TYPE (@0))
87 && tree_nop_conversion_p (TREE_TYPE (type), TREE_TYPE (TREE_TYPE (@0))))))
88 /* This one has to be last, or it shadows the others. */
89 (match (nop_convert @0)
92 /* Simplifications of operations with one constant operand and
93 simplifications to constants or single values. */
95 (for op (plus pointer_plus minus bit_ior bit_xor)
100 /* 0 +p index -> (type)index */
102 (pointer_plus integer_zerop @1)
103 (non_lvalue (convert @1)))
105 /* See if ARG1 is zero and X + ARG1 reduces to X.
106 Likewise if the operands are reversed. */
108 (plus:c @0 real_zerop@1)
109 (if (fold_real_zero_addition_p (type, @1, 0))
112 /* See if ARG1 is zero and X - ARG1 reduces to X. */
114 (minus @0 real_zerop@1)
115 (if (fold_real_zero_addition_p (type, @1, 1))
119 This is unsafe for certain floats even in non-IEEE formats.
120 In IEEE, it is unsafe because it does wrong for NaNs.
121 Also note that operand_equal_p is always false if an operand
125 (if (!FLOAT_TYPE_P (type) || !HONOR_NANS (type))
126 { build_zero_cst (type); }))
129 (mult @0 integer_zerop@1)
132 /* Maybe fold x * 0 to 0. The expressions aren't the same
133 when x is NaN, since x * 0 is also NaN. Nor are they the
134 same in modes with signed zeros, since multiplying a
135 negative value by 0 gives -0, not +0. */
137 (mult @0 real_zerop@1)
138 (if (!HONOR_NANS (type) && !HONOR_SIGNED_ZEROS (type))
141 /* In IEEE floating point, x*1 is not equivalent to x for snans.
142 Likewise for complex arithmetic with signed zeros. */
145 (if (!HONOR_SNANS (type)
146 && (!HONOR_SIGNED_ZEROS (type)
147 || !COMPLEX_FLOAT_TYPE_P (type)))
150 /* Transform x * -1.0 into -x. */
152 (mult @0 real_minus_onep)
153 (if (!HONOR_SNANS (type)
154 && (!HONOR_SIGNED_ZEROS (type)
155 || !COMPLEX_FLOAT_TYPE_P (type)))
158 (for cmp (gt ge lt le)
159 outp (convert convert negate negate)
160 outn (negate negate convert convert)
161 /* Transform (X > 0.0 ? 1.0 : -1.0) into copysign(1, X). */
162 /* Transform (X >= 0.0 ? 1.0 : -1.0) into copysign(1, X). */
163 /* Transform (X < 0.0 ? 1.0 : -1.0) into copysign(1,-X). */
164 /* Transform (X <= 0.0 ? 1.0 : -1.0) into copysign(1,-X). */
166 (cond (cmp @0 real_zerop) real_onep@1 real_minus_onep)
167 (if (!HONOR_NANS (type) && !HONOR_SIGNED_ZEROS (type)
168 && types_match (type, TREE_TYPE (@0)))
170 (if (types_match (type, float_type_node))
171 (BUILT_IN_COPYSIGNF @1 (outp @0)))
172 (if (types_match (type, double_type_node))
173 (BUILT_IN_COPYSIGN @1 (outp @0)))
174 (if (types_match (type, long_double_type_node))
175 (BUILT_IN_COPYSIGNL @1 (outp @0))))))
176 /* Transform (X > 0.0 ? -1.0 : 1.0) into copysign(1,-X). */
177 /* Transform (X >= 0.0 ? -1.0 : 1.0) into copysign(1,-X). */
178 /* Transform (X < 0.0 ? -1.0 : 1.0) into copysign(1,X). */
179 /* Transform (X <= 0.0 ? -1.0 : 1.0) into copysign(1,X). */
181 (cond (cmp @0 real_zerop) real_minus_onep real_onep@1)
182 (if (!HONOR_NANS (type) && !HONOR_SIGNED_ZEROS (type)
183 && types_match (type, TREE_TYPE (@0)))
185 (if (types_match (type, float_type_node))
186 (BUILT_IN_COPYSIGNF @1 (outn @0)))
187 (if (types_match (type, double_type_node))
188 (BUILT_IN_COPYSIGN @1 (outn @0)))
189 (if (types_match (type, long_double_type_node))
190 (BUILT_IN_COPYSIGNL @1 (outn @0)))))))
192 /* Transform X * copysign (1.0, X) into abs(X). */
194 (mult:c @0 (COPYSIGN real_onep @0))
195 (if (!HONOR_NANS (type) && !HONOR_SIGNED_ZEROS (type))
198 /* Transform X * copysign (1.0, -X) into -abs(X). */
200 (mult:c @0 (COPYSIGN real_onep (negate @0)))
201 (if (!HONOR_NANS (type) && !HONOR_SIGNED_ZEROS (type))
204 /* Transform copysign (CST, X) into copysign (ABS(CST), X). */
206 (COPYSIGN REAL_CST@0 @1)
207 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@0)))
208 (COPYSIGN (negate @0) @1)))
210 /* X * 1, X / 1 -> X. */
211 (for op (mult trunc_div ceil_div floor_div round_div exact_div)
216 /* (A / (1 << B)) -> (A >> B).
217 Only for unsigned A. For signed A, this would not preserve rounding
219 For example: (-1 / ( 1 << B)) != -1 >> B. */
221 (trunc_div @0 (lshift integer_onep@1 @2))
222 (if ((TYPE_UNSIGNED (type) || tree_expr_nonnegative_p (@0))
223 && (!VECTOR_TYPE_P (type)
224 || target_supports_op_p (type, RSHIFT_EXPR, optab_vector)
225 || target_supports_op_p (type, RSHIFT_EXPR, optab_scalar)))
228 /* Preserve explicit divisions by 0: the C++ front-end wants to detect
229 undefined behavior in constexpr evaluation, and assuming that the division
230 traps enables better optimizations than these anyway. */
231 (for div (trunc_div ceil_div floor_div round_div exact_div)
232 /* 0 / X is always zero. */
234 (div integer_zerop@0 @1)
235 /* But not for 0 / 0 so that we can get the proper warnings and errors. */
236 (if (!integer_zerop (@1))
240 (div @0 integer_minus_onep@1)
241 (if (!TYPE_UNSIGNED (type))
246 /* But not for 0 / 0 so that we can get the proper warnings and errors.
247 And not for _Fract types where we can't build 1. */
248 (if (!integer_zerop (@0) && !ALL_FRACT_MODE_P (TYPE_MODE (type)))
249 { build_one_cst (type); }))
250 /* X / abs (X) is X < 0 ? -1 : 1. */
253 (if (INTEGRAL_TYPE_P (type)
254 && TYPE_OVERFLOW_UNDEFINED (type))
255 (cond (lt @0 { build_zero_cst (type); })
256 { build_minus_one_cst (type); } { build_one_cst (type); })))
259 (div:C @0 (negate @0))
260 (if ((INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
261 && TYPE_OVERFLOW_UNDEFINED (type))
262 { build_minus_one_cst (type); })))
264 /* For unsigned integral types, FLOOR_DIV_EXPR is the same as
265 TRUNC_DIV_EXPR. Rewrite into the latter in this case. */
268 (if ((INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
269 && TYPE_UNSIGNED (type))
272 /* Combine two successive divisions. Note that combining ceil_div
273 and floor_div is trickier and combining round_div even more so. */
274 (for div (trunc_div exact_div)
276 (div (div @0 INTEGER_CST@1) INTEGER_CST@2)
279 wide_int mul = wi::mul (@1, @2, TYPE_SIGN (type), &overflow_p);
282 (div @0 { wide_int_to_tree (type, mul); })
283 (if (TYPE_UNSIGNED (type)
284 || mul != wi::min_value (TYPE_PRECISION (type), SIGNED))
285 { build_zero_cst (type); })))))
287 /* Combine successive multiplications. Similar to above, but handling
288 overflow is different. */
290 (mult (mult @0 INTEGER_CST@1) INTEGER_CST@2)
293 wide_int mul = wi::mul (@1, @2, TYPE_SIGN (type), &overflow_p);
295 /* Skip folding on overflow: the only special case is @1 * @2 == -INT_MIN,
296 otherwise undefined overflow implies that @0 must be zero. */
297 (if (!overflow_p || TYPE_OVERFLOW_WRAPS (type))
298 (mult @0 { wide_int_to_tree (type, mul); }))))
300 /* Optimize A / A to 1.0 if we don't care about
301 NaNs or Infinities. */
304 (if (FLOAT_TYPE_P (type)
305 && ! HONOR_NANS (type)
306 && ! HONOR_INFINITIES (type))
307 { build_one_cst (type); }))
309 /* Optimize -A / A to -1.0 if we don't care about
310 NaNs or Infinities. */
312 (rdiv:C @0 (negate @0))
313 (if (FLOAT_TYPE_P (type)
314 && ! HONOR_NANS (type)
315 && ! HONOR_INFINITIES (type))
316 { build_minus_one_cst (type); }))
318 /* PR71078: x / abs(x) -> copysign (1.0, x) */
320 (rdiv:C (convert? @0) (convert? (abs @0)))
321 (if (SCALAR_FLOAT_TYPE_P (type)
322 && ! HONOR_NANS (type)
323 && ! HONOR_INFINITIES (type))
325 (if (types_match (type, float_type_node))
326 (BUILT_IN_COPYSIGNF { build_one_cst (type); } (convert @0)))
327 (if (types_match (type, double_type_node))
328 (BUILT_IN_COPYSIGN { build_one_cst (type); } (convert @0)))
329 (if (types_match (type, long_double_type_node))
330 (BUILT_IN_COPYSIGNL { build_one_cst (type); } (convert @0))))))
332 /* In IEEE floating point, x/1 is not equivalent to x for snans. */
335 (if (!HONOR_SNANS (type))
338 /* In IEEE floating point, x/-1 is not equivalent to -x for snans. */
340 (rdiv @0 real_minus_onep)
341 (if (!HONOR_SNANS (type))
344 (if (flag_reciprocal_math)
345 /* Convert (A/B)/C to A/(B*C) */
347 (rdiv (rdiv:s @0 @1) @2)
348 (rdiv @0 (mult @1 @2)))
350 /* Convert A/(B/C) to (A/B)*C */
352 (rdiv @0 (rdiv:s @1 @2))
353 (mult (rdiv @0 @1) @2)))
355 /* Optimize (X & (-A)) / A where A is a power of 2, to X >> log2(A) */
356 (for div (trunc_div ceil_div floor_div round_div exact_div)
358 (div (convert? (bit_and @0 INTEGER_CST@1)) INTEGER_CST@2)
359 (if (integer_pow2p (@2)
360 && tree_int_cst_sgn (@2) > 0
361 && wi::add (@2, @1) == 0
362 && tree_nop_conversion_p (type, TREE_TYPE (@0)))
363 (rshift (convert @0) { build_int_cst (integer_type_node,
364 wi::exact_log2 (@2)); }))))
366 /* If ARG1 is a constant, we can convert this to a multiply by the
367 reciprocal. This does not have the same rounding properties,
368 so only do this if -freciprocal-math. We can actually
369 always safely do it if ARG1 is a power of two, but it's hard to
370 tell if it is or not in a portable manner. */
371 (for cst (REAL_CST COMPLEX_CST VECTOR_CST)
375 (if (flag_reciprocal_math
378 { tree tem = const_binop (RDIV_EXPR, type, build_one_cst (type), @1); }
380 (mult @0 { tem; } )))
381 (if (cst != COMPLEX_CST)
382 (with { tree inverse = exact_inverse (type, @1); }
384 (mult @0 { inverse; } ))))))))
386 (for mod (ceil_mod floor_mod round_mod trunc_mod)
387 /* 0 % X is always zero. */
389 (mod integer_zerop@0 @1)
390 /* But not for 0 % 0 so that we can get the proper warnings and errors. */
391 (if (!integer_zerop (@1))
393 /* X % 1 is always zero. */
395 (mod @0 integer_onep)
396 { build_zero_cst (type); })
397 /* X % -1 is zero. */
399 (mod @0 integer_minus_onep@1)
400 (if (!TYPE_UNSIGNED (type))
401 { build_zero_cst (type); }))
405 /* But not for 0 % 0 so that we can get the proper warnings and errors. */
406 (if (!integer_zerop (@0))
407 { build_zero_cst (type); }))
408 /* (X % Y) % Y is just X % Y. */
410 (mod (mod@2 @0 @1) @1)
412 /* From extract_muldiv_1: (X * C1) % C2 is zero if C1 is a multiple of C2. */
414 (mod (mult @0 INTEGER_CST@1) INTEGER_CST@2)
415 (if (ANY_INTEGRAL_TYPE_P (type)
416 && TYPE_OVERFLOW_UNDEFINED (type)
417 && wi::multiple_of_p (@1, @2, TYPE_SIGN (type)))
418 { build_zero_cst (type); })))
420 /* X % -C is the same as X % C. */
422 (trunc_mod @0 INTEGER_CST@1)
423 (if (TYPE_SIGN (type) == SIGNED
424 && !TREE_OVERFLOW (@1)
426 && !TYPE_OVERFLOW_TRAPS (type)
427 /* Avoid this transformation if C is INT_MIN, i.e. C == -C. */
428 && !sign_bit_p (@1, @1))
429 (trunc_mod @0 (negate @1))))
431 /* X % -Y is the same as X % Y. */
433 (trunc_mod @0 (convert? (negate @1)))
434 (if (INTEGRAL_TYPE_P (type)
435 && !TYPE_UNSIGNED (type)
436 && !TYPE_OVERFLOW_TRAPS (type)
437 && tree_nop_conversion_p (type, TREE_TYPE (@1))
438 /* Avoid this transformation if X might be INT_MIN or
439 Y might be -1, because we would then change valid
440 INT_MIN % -(-1) into invalid INT_MIN % -1. */
441 && (expr_not_equal_to (@0, TYPE_MIN_VALUE (type))
442 || expr_not_equal_to (@1, wi::minus_one (TYPE_PRECISION
444 (trunc_mod @0 (convert @1))))
446 /* X - (X / Y) * Y is the same as X % Y. */
448 (minus (convert1? @0) (convert2? (mult:c (trunc_div @@0 @@1) @1)))
449 (if (INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
450 (convert (trunc_mod @0 @1))))
452 /* Optimize TRUNC_MOD_EXPR by a power of two into a BIT_AND_EXPR,
453 i.e. "X % C" into "X & (C - 1)", if X and C are positive.
454 Also optimize A % (C << N) where C is a power of 2,
455 to A & ((C << N) - 1). */
456 (match (power_of_two_cand @1)
458 (match (power_of_two_cand @1)
459 (lshift INTEGER_CST@1 @2))
460 (for mod (trunc_mod floor_mod)
462 (mod @0 (convert?@3 (power_of_two_cand@1 @2)))
463 (if ((TYPE_UNSIGNED (type)
464 || tree_expr_nonnegative_p (@0))
465 && tree_nop_conversion_p (type, TREE_TYPE (@3))
466 && integer_pow2p (@2) && tree_int_cst_sgn (@2) > 0)
467 (bit_and @0 (convert (minus @1 { build_int_cst (TREE_TYPE (@1), 1); }))))))
469 /* Simplify (unsigned t * 2)/2 -> unsigned t & 0x7FFFFFFF. */
471 (trunc_div (mult @0 integer_pow2p@1) @1)
472 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
473 (bit_and @0 { wide_int_to_tree
474 (type, wi::mask (TYPE_PRECISION (type) - wi::exact_log2 (@1),
475 false, TYPE_PRECISION (type))); })))
477 /* Simplify (unsigned t / 2) * 2 -> unsigned t & ~1. */
479 (mult (trunc_div @0 integer_pow2p@1) @1)
480 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
481 (bit_and @0 (negate @1))))
483 /* Simplify (t * 2) / 2) -> t. */
484 (for div (trunc_div ceil_div floor_div round_div exact_div)
486 (div (mult @0 @1) @1)
487 (if (ANY_INTEGRAL_TYPE_P (type)
488 && TYPE_OVERFLOW_UNDEFINED (type))
492 /* Simplify cos(-x) and cos(|x|) -> cos(x). Similarly for cosh. */
497 /* Simplify pow(-x, y) and pow(|x|,y) -> pow(x,y) if y is an even integer. */
500 (pows (op @0) REAL_CST@1)
501 (with { HOST_WIDE_INT n; }
502 (if (real_isinteger (&TREE_REAL_CST (@1), &n) && (n & 1) == 0)
504 /* Likewise for powi. */
507 (pows (op @0) INTEGER_CST@1)
508 (if (wi::bit_and (@1, 1) == 0)
510 /* Strip negate and abs from both operands of hypot. */
518 /* copysign(-x, y) and copysign(abs(x), y) -> copysign(x, y). */
519 (for copysigns (COPYSIGN)
521 (copysigns (op @0) @1)
524 /* abs(x)*abs(x) -> x*x. Should be valid for all types. */
529 /* cos(copysign(x, y)) -> cos(x). Similarly for cosh. */
533 (coss (copysigns @0 @1))
536 /* pow(copysign(x, y), z) -> pow(x, z) if z is an even integer. */
540 (pows (copysigns @0 @2) REAL_CST@1)
541 (with { HOST_WIDE_INT n; }
542 (if (real_isinteger (&TREE_REAL_CST (@1), &n) && (n & 1) == 0)
544 /* Likewise for powi. */
548 (pows (copysigns @0 @2) INTEGER_CST@1)
549 (if (wi::bit_and (@1, 1) == 0)
554 /* hypot(copysign(x, y), z) -> hypot(x, z). */
556 (hypots (copysigns @0 @1) @2)
558 /* hypot(x, copysign(y, z)) -> hypot(x, y). */
560 (hypots @0 (copysigns @1 @2))
563 /* copysign(x, CST) -> [-]abs (x). */
564 (for copysigns (COPYSIGN)
566 (copysigns @0 REAL_CST@1)
567 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
571 /* copysign(copysign(x, y), z) -> copysign(x, z). */
572 (for copysigns (COPYSIGN)
574 (copysigns (copysigns @0 @1) @2)
577 /* copysign(x,y)*copysign(x,y) -> x*x. */
578 (for copysigns (COPYSIGN)
580 (mult (copysigns@2 @0 @1) @2)
583 /* ccos(-x) -> ccos(x). Similarly for ccosh. */
584 (for ccoss (CCOS CCOSH)
589 /* cabs(-x) and cos(conj(x)) -> cabs(x). */
590 (for ops (conj negate)
596 /* Fold (a * (1 << b)) into (a << b) */
598 (mult:c @0 (convert? (lshift integer_onep@1 @2)))
599 (if (! FLOAT_TYPE_P (type)
600 && tree_nop_conversion_p (type, TREE_TYPE (@1)))
603 /* Fold (C1/X)*C2 into (C1*C2)/X. */
605 (mult (rdiv@3 REAL_CST@0 @1) REAL_CST@2)
606 (if (flag_associative_math
609 { tree tem = const_binop (MULT_EXPR, type, @0, @2); }
611 (rdiv { tem; } @1)))))
613 /* Convert C1/(X*C2) into (C1/C2)/X */
615 (rdiv REAL_CST@0 (mult @1 REAL_CST@2))
616 (if (flag_reciprocal_math)
618 { tree tem = const_binop (RDIV_EXPR, type, @0, @2); }
620 (rdiv { tem; } @1)))))
622 /* Simplify ~X & X as zero. */
624 (bit_and:c (convert? @0) (convert? (bit_not @0)))
625 { build_zero_cst (type); })
627 /* PR71636: Transform x & ((1U << b) - 1) -> x & ~(~0U << b); */
629 (bit_and:c @0 (plus:s (lshift:s integer_onep @1) integer_minus_onep))
630 (if (TYPE_UNSIGNED (type))
631 (bit_and @0 (bit_not (lshift { build_all_ones_cst (type); } @1)))))
633 /* PR35691: Transform
634 (x == 0 & y == 0) -> (x | typeof(x)(y)) == 0.
635 (x != 0 | y != 0) -> (x | typeof(x)(y)) != 0. */
636 (for bitop (bit_and bit_ior)
639 (bitop (cmp @0 integer_zerop@2) (cmp @1 integer_zerop))
640 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
641 && INTEGRAL_TYPE_P (TREE_TYPE (@1))
642 && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
643 (cmp (bit_ior @0 (convert @1)) @2))))
645 /* Fold (A & ~B) - (A & B) into (A ^ B) - B. */
647 (minus (bit_and:cs @0 (bit_not @1)) (bit_and:cs @0 @1))
648 (minus (bit_xor @0 @1) @1))
650 (minus (bit_and:s @0 INTEGER_CST@2) (bit_and:s @0 INTEGER_CST@1))
651 (if (wi::bit_not (@2) == @1)
652 (minus (bit_xor @0 @1) @1)))
654 /* Fold (A & B) - (A & ~B) into B - (A ^ B). */
656 (minus (bit_and:cs @0 @1) (bit_and:cs @0 (bit_not @1)))
657 (minus @1 (bit_xor @0 @1)))
659 /* Simplify (X & ~Y) |^+ (~X & Y) -> X ^ Y. */
660 (for op (bit_ior bit_xor plus)
662 (op (bit_and:c @0 (bit_not @1)) (bit_and:c (bit_not @0) @1))
665 (op:c (bit_and @0 INTEGER_CST@2) (bit_and (bit_not @0) INTEGER_CST@1))
666 (if (wi::bit_not (@2) == @1)
669 /* PR53979: Transform ((a ^ b) | a) -> (a | b) */
671 (bit_ior:c (bit_xor:c @0 @1) @0)
674 /* Simplify (~X & Y) to X ^ Y if we know that (X & ~Y) is 0. */
677 (bit_and (bit_not SSA_NAME@0) INTEGER_CST@1)
678 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
679 && (get_nonzero_bits (@0) & wi::bit_not (@1)) == 0)
683 /* X % Y is smaller than Y. */
686 (cmp (trunc_mod @0 @1) @1)
687 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
688 { constant_boolean_node (cmp == LT_EXPR, type); })))
691 (cmp @1 (trunc_mod @0 @1))
692 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
693 { constant_boolean_node (cmp == GT_EXPR, type); })))
697 (bit_ior @0 integer_all_onesp@1)
702 (bit_ior @0 integer_zerop)
707 (bit_and @0 integer_zerop@1)
713 (for op (bit_ior bit_xor plus)
715 (op:c (convert? @0) (convert? (bit_not @0)))
716 (convert { build_all_ones_cst (TREE_TYPE (@0)); })))
721 { build_zero_cst (type); })
723 /* Canonicalize X ^ ~0 to ~X. */
725 (bit_xor @0 integer_all_onesp@1)
730 (bit_and @0 integer_all_onesp)
733 /* x & x -> x, x | x -> x */
734 (for bitop (bit_and bit_ior)
739 /* x & C -> x if we know that x & ~C == 0. */
742 (bit_and SSA_NAME@0 INTEGER_CST@1)
743 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
744 && (get_nonzero_bits (@0) & wi::bit_not (@1)) == 0)
748 /* x + (x & 1) -> (x + 1) & ~1 */
750 (plus:c @0 (bit_and:s @0 integer_onep@1))
751 (bit_and (plus @0 @1) (bit_not @1)))
753 /* x & ~(x & y) -> x & ~y */
754 /* x | ~(x | y) -> x | ~y */
755 (for bitop (bit_and bit_ior)
757 (bitop:c @0 (bit_not (bitop:cs @0 @1)))
758 (bitop @0 (bit_not @1))))
760 /* (x | y) & ~x -> y & ~x */
761 /* (x & y) | ~x -> y | ~x */
762 (for bitop (bit_and bit_ior)
763 rbitop (bit_ior bit_and)
765 (bitop:c (rbitop:c @0 @1) (bit_not@2 @0))
768 /* (x & y) ^ (x | y) -> x ^ y */
770 (bit_xor:c (bit_and @0 @1) (bit_ior @0 @1))
773 /* (x ^ y) ^ (x | y) -> x & y */
775 (bit_xor:c (bit_xor @0 @1) (bit_ior @0 @1))
778 /* (x & y) + (x ^ y) -> x | y */
779 /* (x & y) | (x ^ y) -> x | y */
780 /* (x & y) ^ (x ^ y) -> x | y */
781 (for op (plus bit_ior bit_xor)
783 (op:c (bit_and @0 @1) (bit_xor @0 @1))
786 /* (x & y) + (x | y) -> x + y */
788 (plus:c (bit_and @0 @1) (bit_ior @0 @1))
791 /* (x + y) - (x | y) -> x & y */
793 (minus (plus @0 @1) (bit_ior @0 @1))
794 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
795 && !TYPE_SATURATING (type))
798 /* (x + y) - (x & y) -> x | y */
800 (minus (plus @0 @1) (bit_and @0 @1))
801 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
802 && !TYPE_SATURATING (type))
805 /* (x | y) - (x ^ y) -> x & y */
807 (minus (bit_ior @0 @1) (bit_xor @0 @1))
810 /* (x | y) - (x & y) -> x ^ y */
812 (minus (bit_ior @0 @1) (bit_and @0 @1))
815 /* (x | y) & ~(x & y) -> x ^ y */
817 (bit_and:c (bit_ior @0 @1) (bit_not (bit_and @0 @1)))
820 /* (x | y) & (~x ^ y) -> x & y */
822 (bit_and:c (bit_ior:c @0 @1) (bit_xor:c @1 (bit_not @0)))
825 /* ~x & ~y -> ~(x | y)
826 ~x | ~y -> ~(x & y) */
827 (for op (bit_and bit_ior)
828 rop (bit_ior bit_and)
830 (op (convert1? (bit_not @0)) (convert2? (bit_not @1)))
831 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
832 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
833 (bit_not (rop (convert @0) (convert @1))))))
835 /* If we are XORing or adding two BIT_AND_EXPR's, both of which are and'ing
836 with a constant, and the two constants have no bits in common,
837 we should treat this as a BIT_IOR_EXPR since this may produce more
839 (for op (bit_xor plus)
841 (op (convert1? (bit_and@4 @0 INTEGER_CST@1))
842 (convert2? (bit_and@5 @2 INTEGER_CST@3)))
843 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
844 && tree_nop_conversion_p (type, TREE_TYPE (@2))
845 && wi::bit_and (@1, @3) == 0)
846 (bit_ior (convert @4) (convert @5)))))
848 /* (X | Y) ^ X -> Y & ~ X*/
850 (bit_xor:c (convert1? (bit_ior:c @@0 @1)) (convert2? @0))
851 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
852 (convert (bit_and @1 (bit_not @0)))))
854 /* Convert ~X ^ ~Y to X ^ Y. */
856 (bit_xor (convert1? (bit_not @0)) (convert2? (bit_not @1)))
857 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
858 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
859 (bit_xor (convert @0) (convert @1))))
861 /* Convert ~X ^ C to X ^ ~C. */
863 (bit_xor (convert? (bit_not @0)) INTEGER_CST@1)
864 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
865 (bit_xor (convert @0) (bit_not @1))))
867 /* Fold (X & Y) ^ Y and (X ^ Y) & Y as ~X & Y. */
868 (for opo (bit_and bit_xor)
869 opi (bit_xor bit_and)
871 (opo:c (opi:c @0 @1) @1)
872 (bit_and (bit_not @0) @1)))
874 /* Given a bit-wise operation CODE applied to ARG0 and ARG1, see if both
875 operands are another bit-wise operation with a common input. If so,
876 distribute the bit operations to save an operation and possibly two if
877 constants are involved. For example, convert
878 (A | B) & (A | C) into A | (B & C)
879 Further simplification will occur if B and C are constants. */
880 (for op (bit_and bit_ior bit_xor)
881 rop (bit_ior bit_and bit_and)
883 (op (convert? (rop:c @@0 @1)) (convert? (rop:c @0 @2)))
884 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
885 && tree_nop_conversion_p (type, TREE_TYPE (@2)))
886 (rop (convert @0) (op (convert @1) (convert @2))))))
888 /* Some simple reassociation for bit operations, also handled in reassoc. */
889 /* (X & Y) & Y -> X & Y
890 (X | Y) | Y -> X | Y */
891 (for op (bit_and bit_ior)
893 (op:c (convert1?@2 (op:c @0 @@1)) (convert2? @1))
895 /* (X ^ Y) ^ Y -> X */
897 (bit_xor:c (convert1? (bit_xor:c @0 @@1)) (convert2? @1))
899 /* (X & Y) & (X & Z) -> (X & Y) & Z
900 (X | Y) | (X | Z) -> (X | Y) | Z */
901 (for op (bit_and bit_ior)
903 (op (convert1?@3 (op:c@4 @0 @1)) (convert2?@5 (op:c@6 @0 @2)))
904 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
905 && tree_nop_conversion_p (type, TREE_TYPE (@2)))
906 (if (single_use (@5) && single_use (@6))
908 (if (single_use (@3) && single_use (@4))
909 (op (convert @1) @5))))))
910 /* (X ^ Y) ^ (X ^ Z) -> Y ^ Z */
912 (bit_xor (convert1? (bit_xor:c @0 @1)) (convert2? (bit_xor:c @0 @2)))
913 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
914 && tree_nop_conversion_p (type, TREE_TYPE (@2)))
915 (bit_xor (convert @1) (convert @2))))
924 (abs tree_expr_nonnegative_p@0)
927 /* A few cases of fold-const.c negate_expr_p predicate. */
930 (if ((INTEGRAL_TYPE_P (type)
931 && TYPE_UNSIGNED (type))
932 || (!TYPE_OVERFLOW_SANITIZED (type)
933 && may_negate_without_overflow_p (t)))))
938 (if (!TYPE_OVERFLOW_SANITIZED (type))))
941 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (t)))))
942 /* VECTOR_CST handling of non-wrapping types would recurse in unsupported
946 (if (FLOAT_TYPE_P (TREE_TYPE (type)) || TYPE_OVERFLOW_WRAPS (type))))
948 /* (-A) * (-B) -> A * B */
950 (mult:c (convert1? (negate @0)) (convert2? negate_expr_p@1))
951 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
952 && tree_nop_conversion_p (type, TREE_TYPE (@1)))
953 (mult (convert @0) (convert (negate @1)))))
955 /* -(A + B) -> (-B) - A. */
957 (negate (plus:c @0 negate_expr_p@1))
958 (if (!HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type))
959 && !HONOR_SIGNED_ZEROS (element_mode (type)))
960 (minus (negate @1) @0)))
962 /* A - B -> A + (-B) if B is easily negatable. */
964 (minus @0 negate_expr_p@1)
965 (if (!FIXED_POINT_TYPE_P (type))
966 (plus @0 (negate @1))))
968 /* Try to fold (type) X op CST -> (type) (X op ((type-x) CST))
970 For bitwise binary operations apply operand conversions to the
971 binary operation result instead of to the operands. This allows
972 to combine successive conversions and bitwise binary operations.
973 We combine the above two cases by using a conditional convert. */
974 (for bitop (bit_and bit_ior bit_xor)
976 (bitop (convert @0) (convert? @1))
977 (if (((TREE_CODE (@1) == INTEGER_CST
978 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
979 && int_fits_type_p (@1, TREE_TYPE (@0)))
980 || types_match (@0, @1))
981 /* ??? This transform conflicts with fold-const.c doing
982 Convert (T)(x & c) into (T)x & (T)c, if c is an integer
983 constants (if x has signed type, the sign bit cannot be set
984 in c). This folds extension into the BIT_AND_EXPR.
985 Restrict it to GIMPLE to avoid endless recursions. */
986 && (bitop != BIT_AND_EXPR || GIMPLE)
987 && (/* That's a good idea if the conversion widens the operand, thus
988 after hoisting the conversion the operation will be narrower. */
989 TYPE_PRECISION (TREE_TYPE (@0)) < TYPE_PRECISION (type)
990 /* It's also a good idea if the conversion is to a non-integer
992 || GET_MODE_CLASS (TYPE_MODE (type)) != MODE_INT
993 /* Or if the precision of TO is not the same as the precision
995 || TYPE_PRECISION (type) != GET_MODE_PRECISION (TYPE_MODE (type))))
996 (convert (bitop @0 (convert @1))))))
998 (for bitop (bit_and bit_ior)
999 rbitop (bit_ior bit_and)
1000 /* (x | y) & x -> x */
1001 /* (x & y) | x -> x */
1003 (bitop:c (rbitop:c @0 @1) @0)
1005 /* (~x | y) & x -> x & y */
1006 /* (~x & y) | x -> x | y */
1008 (bitop:c (rbitop:c (bit_not @0) @1) @0)
1011 /* (x | CST1) & CST2 -> (x & CST2) | (CST1 & CST2) */
1013 (bit_and (bit_ior @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
1014 (bit_ior (bit_and @0 @2) (bit_and @1 @2)))
1016 /* Combine successive equal operations with constants. */
1017 (for bitop (bit_and bit_ior bit_xor)
1019 (bitop (bitop @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
1020 (bitop @0 (bitop @1 @2))))
1022 /* Try simple folding for X op !X, and X op X with the help
1023 of the truth_valued_p and logical_inverted_value predicates. */
1024 (match truth_valued_p
1026 (if (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1)))
1027 (for op (tcc_comparison truth_and truth_andif truth_or truth_orif truth_xor)
1028 (match truth_valued_p
1030 (match truth_valued_p
1033 (match (logical_inverted_value @0)
1035 (match (logical_inverted_value @0)
1036 (bit_not truth_valued_p@0))
1037 (match (logical_inverted_value @0)
1038 (eq @0 integer_zerop))
1039 (match (logical_inverted_value @0)
1040 (ne truth_valued_p@0 integer_truep))
1041 (match (logical_inverted_value @0)
1042 (bit_xor truth_valued_p@0 integer_truep))
1046 (bit_and:c @0 (logical_inverted_value @0))
1047 { build_zero_cst (type); })
1048 /* X | !X and X ^ !X -> 1, , if X is truth-valued. */
1049 (for op (bit_ior bit_xor)
1051 (op:c truth_valued_p@0 (logical_inverted_value @0))
1052 { constant_boolean_node (true, type); }))
1053 /* X ==/!= !X is false/true. */
1056 (op:c truth_valued_p@0 (logical_inverted_value @0))
1057 { constant_boolean_node (op == NE_EXPR ? true : false, type); }))
1061 (bit_not (bit_not @0))
1064 /* Convert ~ (-A) to A - 1. */
1066 (bit_not (convert? (negate @0)))
1067 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1068 || !TYPE_UNSIGNED (TREE_TYPE (@0)))
1069 (convert (minus @0 { build_each_one_cst (TREE_TYPE (@0)); }))))
1071 /* Convert ~ (A - 1) or ~ (A + -1) to -A. */
1073 (bit_not (convert? (minus @0 integer_each_onep)))
1074 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1075 || !TYPE_UNSIGNED (TREE_TYPE (@0)))
1076 (convert (negate @0))))
1078 (bit_not (convert? (plus @0 integer_all_onesp)))
1079 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1080 || !TYPE_UNSIGNED (TREE_TYPE (@0)))
1081 (convert (negate @0))))
1083 /* Part of convert ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify. */
1085 (bit_not (convert? (bit_xor @0 INTEGER_CST@1)))
1086 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1087 (convert (bit_xor @0 (bit_not @1)))))
1089 (bit_not (convert? (bit_xor:c (bit_not @0) @1)))
1090 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1091 (convert (bit_xor @0 @1))))
1093 /* (x & ~m) | (y & m) -> ((x ^ y) & m) ^ x */
1095 (bit_ior:c (bit_and:cs @0 (bit_not @2)) (bit_and:cs @1 @2))
1096 (bit_xor (bit_and (bit_xor @0 @1) @2) @0))
1098 /* Fold A - (A & B) into ~B & A. */
1100 (minus (convert1? @0) (convert2?:s (bit_and:cs @@0 @1)))
1101 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
1102 && tree_nop_conversion_p (type, TREE_TYPE (@1)))
1103 (convert (bit_and (bit_not @1) @0))))
1105 /* (m1 CMP m2) * d -> (m1 CMP m2) ? d : 0 */
1106 (for cmp (gt lt ge le)
1108 (mult (convert (cmp @0 @1)) @2)
1109 (cond (cmp @0 @1) @2 { build_zero_cst (type); })))
1111 /* For integral types with undefined overflow and C != 0 fold
1112 x * C EQ/NE y * C into x EQ/NE y. */
1115 (cmp (mult:c @0 @1) (mult:c @2 @1))
1116 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
1117 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1118 && tree_expr_nonzero_p (@1))
1121 /* For integral types with wrapping overflow and C odd fold
1122 x * C EQ/NE y * C into x EQ/NE y. */
1125 (cmp (mult @0 INTEGER_CST@1) (mult @2 @1))
1126 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
1127 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))
1128 && (TREE_INT_CST_LOW (@1) & 1) != 0)
1131 /* For integral types with undefined overflow and C != 0 fold
1132 x * C RELOP y * C into:
1134 x RELOP y for nonnegative C
1135 y RELOP x for negative C */
1136 (for cmp (lt gt le ge)
1138 (cmp (mult:c @0 @1) (mult:c @2 @1))
1139 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
1140 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1141 (if (tree_expr_nonnegative_p (@1) && tree_expr_nonzero_p (@1))
1143 (if (TREE_CODE (@1) == INTEGER_CST
1144 && wi::neg_p (@1, TYPE_SIGN (TREE_TYPE (@1))))
1147 /* (X - 1U) <= INT_MAX-1U into (int) X > 0. */
1151 (cmp (plus @0 integer_minus_onep@1) INTEGER_CST@2)
1152 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1153 && TYPE_UNSIGNED (TREE_TYPE (@0))
1154 && TYPE_PRECISION (TREE_TYPE (@0)) > 1
1155 && wi::eq_p (@2, wi::max_value (TYPE_PRECISION (TREE_TYPE (@0)),
1157 (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
1158 (icmp (convert:stype @0) { build_int_cst (stype, 0); })))))
1160 /* X / 4 < Y / 4 iff X < Y when the division is known to be exact. */
1161 (for cmp (simple_comparison)
1163 (cmp (exact_div @0 INTEGER_CST@2) (exact_div @1 @2))
1164 (if (wi::gt_p(@2, 0, TYPE_SIGN (TREE_TYPE (@2))))
1167 /* X / C1 op C2 into a simple range test. */
1168 (for cmp (simple_comparison)
1170 (cmp (trunc_div:s @0 INTEGER_CST@1) INTEGER_CST@2)
1171 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1172 && integer_nonzerop (@1)
1173 && !TREE_OVERFLOW (@1)
1174 && !TREE_OVERFLOW (@2))
1175 (with { tree lo, hi; bool neg_overflow;
1176 enum tree_code code = fold_div_compare (cmp, @1, @2, &lo, &hi,
1179 (if (code == LT_EXPR || code == GE_EXPR)
1180 (if (TREE_OVERFLOW (lo))
1181 { build_int_cst (type, (code == LT_EXPR) ^ neg_overflow); }
1182 (if (code == LT_EXPR)
1185 (if (code == LE_EXPR || code == GT_EXPR)
1186 (if (TREE_OVERFLOW (hi))
1187 { build_int_cst (type, (code == LE_EXPR) ^ neg_overflow); }
1188 (if (code == LE_EXPR)
1192 { build_int_cst (type, code == NE_EXPR); })
1193 (if (code == EQ_EXPR && !hi)
1195 (if (code == EQ_EXPR && !lo)
1197 (if (code == NE_EXPR && !hi)
1199 (if (code == NE_EXPR && !lo)
1202 { build_range_check (UNKNOWN_LOCATION, type, @0, code == EQ_EXPR,
1206 tree etype = range_check_type (TREE_TYPE (@0));
1209 if (! TYPE_UNSIGNED (etype))
1210 etype = unsigned_type_for (etype);
1211 hi = fold_convert (etype, hi);
1212 lo = fold_convert (etype, lo);
1213 hi = const_binop (MINUS_EXPR, etype, hi, lo);
1216 (if (etype && hi && !TREE_OVERFLOW (hi))
1217 (if (code == EQ_EXPR)
1218 (le (minus (convert:etype @0) { lo; }) { hi; })
1219 (gt (minus (convert:etype @0) { lo; }) { hi; })))))))))
1221 /* X + Z < Y + Z is the same as X < Y when there is no overflow. */
1222 (for op (lt le ge gt)
1224 (op (plus:c @0 @2) (plus:c @1 @2))
1225 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1226 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1228 /* For equality and subtraction, this is also true with wrapping overflow. */
1229 (for op (eq ne minus)
1231 (op (plus:c @0 @2) (plus:c @1 @2))
1232 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1233 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1234 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
1237 /* X - Z < Y - Z is the same as X < Y when there is no overflow. */
1238 (for op (lt le ge gt)
1240 (op (minus @0 @2) (minus @1 @2))
1241 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1242 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1244 /* For equality and subtraction, this is also true with wrapping overflow. */
1245 (for op (eq ne minus)
1247 (op (minus @0 @2) (minus @1 @2))
1248 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1249 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1250 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
1253 /* Z - X < Z - Y is the same as Y < X when there is no overflow. */
1254 (for op (lt le ge gt)
1256 (op (minus @2 @0) (minus @2 @1))
1257 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1258 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1260 /* For equality and subtraction, this is also true with wrapping overflow. */
1261 (for op (eq ne minus)
1263 (op (minus @2 @0) (minus @2 @1))
1264 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1265 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1266 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
1269 /* X == C - X can never be true if C is odd. */
1272 (cmp:c (convert? @0) (convert1? (minus INTEGER_CST@1 (convert2? @0))))
1273 (if (TREE_INT_CST_LOW (@1) & 1)
1274 { constant_boolean_node (cmp == NE_EXPR, type); })))
1276 /* Arguments on which one can call get_nonzero_bits to get the bits
1278 (match with_possible_nonzero_bits
1280 (match with_possible_nonzero_bits
1282 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))))
1283 /* Slightly extended version, do not make it recursive to keep it cheap. */
1284 (match (with_possible_nonzero_bits2 @0)
1285 with_possible_nonzero_bits@0)
1286 (match (with_possible_nonzero_bits2 @0)
1287 (bit_and:c with_possible_nonzero_bits@0 @2))
1289 /* Same for bits that are known to be set, but we do not have
1290 an equivalent to get_nonzero_bits yet. */
1291 (match (with_certain_nonzero_bits2 @0)
1293 (match (with_certain_nonzero_bits2 @0)
1294 (bit_ior @1 INTEGER_CST@0))
1296 /* X == C (or X & Z == Y | C) is impossible if ~nonzero(X) & C != 0. */
1299 (cmp:c (with_possible_nonzero_bits2 @0) (with_certain_nonzero_bits2 @1))
1300 (if ((~get_nonzero_bits (@0) & @1) != 0)
1301 { constant_boolean_node (cmp == NE_EXPR, type); })))
1303 /* ((X inner_op C0) outer_op C1)
1304 With X being a tree where value_range has reasoned certain bits to always be
1305 zero throughout its computed value range,
1306 inner_op = {|,^}, outer_op = {|,^} and inner_op != outer_op
1307 where zero_mask has 1's for all bits that are sure to be 0 in
1309 if (inner_op == '^') C0 &= ~C1;
1310 if ((C0 & ~zero_mask) == 0) then emit (X outer_op (C0 outer_op C1)
1311 if ((C1 & ~zero_mask) == 0) then emit (X inner_op (C0 outer_op C1)
1313 (for inner_op (bit_ior bit_xor)
1314 outer_op (bit_xor bit_ior)
1317 (inner_op:s @2 INTEGER_CST@0) INTEGER_CST@1)
1321 wide_int zero_mask_not;
1325 if (TREE_CODE (@2) == SSA_NAME)
1326 zero_mask_not = get_nonzero_bits (@2);
1330 if (inner_op == BIT_XOR_EXPR)
1332 C0 = wi::bit_and_not (@0, @1);
1333 cst_emit = wi::bit_or (C0, @1);
1338 cst_emit = wi::bit_xor (@0, @1);
1341 (if (!fail && wi::bit_and (C0, zero_mask_not) == 0)
1342 (outer_op @2 { wide_int_to_tree (type, cst_emit); })
1343 (if (!fail && wi::bit_and (@1, zero_mask_not) == 0)
1344 (inner_op @2 { wide_int_to_tree (type, cst_emit); }))))))
1346 /* Associate (p +p off1) +p off2 as (p +p (off1 + off2)). */
1348 (pointer_plus (pointer_plus:s @0 @1) @3)
1349 (pointer_plus @0 (plus @1 @3)))
1355 tem4 = (unsigned long) tem3;
1360 (pointer_plus @0 (convert?@2 (minus@3 (convert @1) (convert @0))))
1361 /* Conditionally look through a sign-changing conversion. */
1362 (if (TYPE_PRECISION (TREE_TYPE (@2)) == TYPE_PRECISION (TREE_TYPE (@3))
1363 && ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@1)))
1364 || (GENERIC && type == TREE_TYPE (@1))))
1368 tem = (sizetype) ptr;
1372 and produce the simpler and easier to analyze with respect to alignment
1373 ... = ptr & ~algn; */
1375 (pointer_plus @0 (negate (bit_and (convert @0) INTEGER_CST@1)))
1376 (with { tree algn = wide_int_to_tree (TREE_TYPE (@0), wi::bit_not (@1)); }
1377 (bit_and @0 { algn; })))
1379 /* Try folding difference of addresses. */
1381 (minus (convert ADDR_EXPR@0) (convert @1))
1382 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1383 (with { HOST_WIDE_INT diff; }
1384 (if (ptr_difference_const (@0, @1, &diff))
1385 { build_int_cst_type (type, diff); }))))
1387 (minus (convert @0) (convert ADDR_EXPR@1))
1388 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1389 (with { HOST_WIDE_INT diff; }
1390 (if (ptr_difference_const (@0, @1, &diff))
1391 { build_int_cst_type (type, diff); }))))
1393 /* If arg0 is derived from the address of an object or function, we may
1394 be able to fold this expression using the object or function's
1397 (bit_and (convert? @0) INTEGER_CST@1)
1398 (if (POINTER_TYPE_P (TREE_TYPE (@0))
1399 && tree_nop_conversion_p (type, TREE_TYPE (@0)))
1403 unsigned HOST_WIDE_INT bitpos;
1404 get_pointer_alignment_1 (@0, &align, &bitpos);
1406 (if (wi::ltu_p (@1, align / BITS_PER_UNIT))
1407 { wide_int_to_tree (type, wi::bit_and (@1, bitpos / BITS_PER_UNIT)); }))))
1410 /* We can't reassociate at all for saturating types. */
1411 (if (!TYPE_SATURATING (type))
1413 /* Contract negates. */
1414 /* A + (-B) -> A - B */
1416 (plus:c @0 (convert? (negate @1)))
1417 /* Apply STRIP_NOPS on the negate. */
1418 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1419 && !TYPE_OVERFLOW_SANITIZED (type))
1423 if (INTEGRAL_TYPE_P (type)
1424 && TYPE_OVERFLOW_WRAPS (type) != TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
1425 t1 = TYPE_OVERFLOW_WRAPS (type) ? type : TREE_TYPE (@1);
1427 (convert (minus (convert:t1 @0) (convert:t1 @1))))))
1428 /* A - (-B) -> A + B */
1430 (minus @0 (convert? (negate @1)))
1431 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1432 && !TYPE_OVERFLOW_SANITIZED (type))
1436 if (INTEGRAL_TYPE_P (type)
1437 && TYPE_OVERFLOW_WRAPS (type) != TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
1438 t1 = TYPE_OVERFLOW_WRAPS (type) ? type : TREE_TYPE (@1);
1440 (convert (plus (convert:t1 @0) (convert:t1 @1))))))
1443 (negate (convert? (negate @1)))
1444 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1445 && !TYPE_OVERFLOW_SANITIZED (type))
1448 /* We can't reassociate floating-point unless -fassociative-math
1449 or fixed-point plus or minus because of saturation to +-Inf. */
1450 (if ((!FLOAT_TYPE_P (type) || flag_associative_math)
1451 && !FIXED_POINT_TYPE_P (type))
1453 /* Match patterns that allow contracting a plus-minus pair
1454 irrespective of overflow issues. */
1455 /* (A +- B) - A -> +- B */
1456 /* (A +- B) -+ B -> A */
1457 /* A - (A +- B) -> -+ B */
1458 /* A +- (B -+ A) -> +- B */
1460 (minus (plus:c @0 @1) @0)
1463 (minus (minus @0 @1) @0)
1466 (plus:c (minus @0 @1) @1)
1469 (minus @0 (plus:c @0 @1))
1472 (minus @0 (minus @0 @1))
1474 /* (A +- B) + (C - A) -> C +- B */
1475 /* (A + B) - (A - C) -> B + C */
1476 /* More cases are handled with comparisons. */
1478 (plus:c (plus:c @0 @1) (minus @2 @0))
1481 (plus:c (minus @0 @1) (minus @2 @0))
1484 (minus (plus:c @0 @1) (minus @0 @2))
1487 /* (A +- CST1) +- CST2 -> A + CST3
1488 Use view_convert because it is safe for vectors and equivalent for
1490 (for outer_op (plus minus)
1491 (for inner_op (plus minus)
1492 neg_inner_op (minus plus)
1494 (outer_op (nop_convert (inner_op @0 CONSTANT_CLASS_P@1))
1496 /* If one of the types wraps, use that one. */
1497 (if (!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_WRAPS (type))
1498 (if (outer_op == PLUS_EXPR)
1499 (plus (view_convert @0) (inner_op @2 (view_convert @1)))
1500 (minus (view_convert @0) (neg_inner_op @2 (view_convert @1))))
1501 (if (!ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1502 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
1503 (if (outer_op == PLUS_EXPR)
1504 (view_convert (plus @0 (inner_op (view_convert @2) @1)))
1505 (view_convert (minus @0 (neg_inner_op (view_convert @2) @1))))
1506 /* If the constant operation overflows we cannot do the transform
1507 directly as we would introduce undefined overflow, for example
1508 with (a - 1) + INT_MIN. */
1509 (if (types_match (type, @0))
1510 (with { tree cst = const_binop (outer_op == inner_op
1511 ? PLUS_EXPR : MINUS_EXPR,
1513 (if (cst && !TREE_OVERFLOW (cst))
1514 (inner_op @0 { cst; } )
1515 /* X+INT_MAX+1 is X-INT_MIN. */
1516 (if (INTEGRAL_TYPE_P (type) && cst
1517 && wi::eq_p (cst, wi::min_value (type)))
1518 (neg_inner_op @0 { wide_int_to_tree (type, cst); })
1519 /* Last resort, use some unsigned type. */
1520 (with { tree utype = unsigned_type_for (type); }
1521 (view_convert (inner_op
1522 (view_convert:utype @0)
1524 { drop_tree_overflow (cst); })))))))))))))
1526 /* (CST1 - A) +- CST2 -> CST3 - A */
1527 (for outer_op (plus minus)
1529 (outer_op (minus CONSTANT_CLASS_P@1 @0) CONSTANT_CLASS_P@2)
1530 (with { tree cst = const_binop (outer_op, type, @1, @2); }
1531 (if (cst && !TREE_OVERFLOW (cst))
1532 (minus { cst; } @0)))))
1534 /* CST1 - (CST2 - A) -> CST3 + A */
1536 (minus CONSTANT_CLASS_P@1 (minus CONSTANT_CLASS_P@2 @0))
1537 (with { tree cst = const_binop (MINUS_EXPR, type, @1, @2); }
1538 (if (cst && !TREE_OVERFLOW (cst))
1539 (plus { cst; } @0))))
1543 (plus:c (bit_not @0) @0)
1544 (if (!TYPE_OVERFLOW_TRAPS (type))
1545 { build_all_ones_cst (type); }))
1549 (plus (convert? (bit_not @0)) integer_each_onep)
1550 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1551 (negate (convert @0))))
1555 (minus (convert? (negate @0)) integer_each_onep)
1556 (if (!TYPE_OVERFLOW_TRAPS (type)
1557 && tree_nop_conversion_p (type, TREE_TYPE (@0)))
1558 (bit_not (convert @0))))
1562 (minus integer_all_onesp @0)
1565 /* (T)(P + A) - (T)P -> (T) A */
1566 (for add (plus pointer_plus)
1568 (minus (convert (add @@0 @1))
1570 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
1571 /* For integer types, if A has a smaller type
1572 than T the result depends on the possible
1574 E.g. T=size_t, A=(unsigned)429497295, P>0.
1575 However, if an overflow in P + A would cause
1576 undefined behavior, we can assume that there
1578 || (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1579 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1580 /* For pointer types, if the conversion of A to the
1581 final type requires a sign- or zero-extension,
1582 then we have to punt - it is not defined which
1584 || (POINTER_TYPE_P (TREE_TYPE (@0))
1585 && TREE_CODE (@1) == INTEGER_CST
1586 && tree_int_cst_sign_bit (@1) == 0))
1589 /* (T)P - (T)(P + A) -> -(T) A */
1590 (for add (plus pointer_plus)
1593 (convert (add @@0 @1)))
1594 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
1595 /* For integer types, if A has a smaller type
1596 than T the result depends on the possible
1598 E.g. T=size_t, A=(unsigned)429497295, P>0.
1599 However, if an overflow in P + A would cause
1600 undefined behavior, we can assume that there
1602 || (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1603 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1604 /* For pointer types, if the conversion of A to the
1605 final type requires a sign- or zero-extension,
1606 then we have to punt - it is not defined which
1608 || (POINTER_TYPE_P (TREE_TYPE (@0))
1609 && TREE_CODE (@1) == INTEGER_CST
1610 && tree_int_cst_sign_bit (@1) == 0))
1611 (negate (convert @1)))))
1613 /* (T)(P + A) - (T)(P + B) -> (T)A - (T)B */
1614 (for add (plus pointer_plus)
1616 (minus (convert (add @@0 @1))
1617 (convert (add @0 @2)))
1618 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
1619 /* For integer types, if A has a smaller type
1620 than T the result depends on the possible
1622 E.g. T=size_t, A=(unsigned)429497295, P>0.
1623 However, if an overflow in P + A would cause
1624 undefined behavior, we can assume that there
1626 || (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1627 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1628 /* For pointer types, if the conversion of A to the
1629 final type requires a sign- or zero-extension,
1630 then we have to punt - it is not defined which
1632 || (POINTER_TYPE_P (TREE_TYPE (@0))
1633 && TREE_CODE (@1) == INTEGER_CST
1634 && tree_int_cst_sign_bit (@1) == 0
1635 && TREE_CODE (@2) == INTEGER_CST
1636 && tree_int_cst_sign_bit (@2) == 0))
1637 (minus (convert @1) (convert @2)))))))
1640 /* Simplifications of MIN_EXPR, MAX_EXPR, fmin() and fmax(). */
1642 (for minmax (min max FMIN FMAX)
1646 /* min(max(x,y),y) -> y. */
1648 (min:c (max:c @0 @1) @1)
1650 /* max(min(x,y),y) -> y. */
1652 (max:c (min:c @0 @1) @1)
1654 /* max(a,-a) -> abs(a). */
1656 (max:c @0 (negate @0))
1657 (if (TREE_CODE (type) != COMPLEX_TYPE
1658 && (! ANY_INTEGRAL_TYPE_P (type)
1659 || TYPE_OVERFLOW_UNDEFINED (type)))
1661 /* min(a,-a) -> -abs(a). */
1663 (min:c @0 (negate @0))
1664 (if (TREE_CODE (type) != COMPLEX_TYPE
1665 && (! ANY_INTEGRAL_TYPE_P (type)
1666 || TYPE_OVERFLOW_UNDEFINED (type)))
1671 (if (INTEGRAL_TYPE_P (type)
1672 && TYPE_MIN_VALUE (type)
1673 && operand_equal_p (@1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
1675 (if (INTEGRAL_TYPE_P (type)
1676 && TYPE_MAX_VALUE (type)
1677 && operand_equal_p (@1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
1682 (if (INTEGRAL_TYPE_P (type)
1683 && TYPE_MAX_VALUE (type)
1684 && operand_equal_p (@1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
1686 (if (INTEGRAL_TYPE_P (type)
1687 && TYPE_MIN_VALUE (type)
1688 && operand_equal_p (@1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
1691 /* max (a, a + CST) -> a + CST where CST is positive. */
1692 /* max (a, a + CST) -> a where CST is negative. */
1694 (max:c @0 (plus@2 @0 INTEGER_CST@1))
1695 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1696 (if (tree_int_cst_sgn (@1) > 0)
1700 /* min (a, a + CST) -> a where CST is positive. */
1701 /* min (a, a + CST) -> a + CST where CST is negative. */
1703 (min:c @0 (plus@2 @0 INTEGER_CST@1))
1704 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1705 (if (tree_int_cst_sgn (@1) > 0)
1709 /* (convert (minmax ((convert (x) c)))) -> minmax (x c) if x is promoted
1710 and the outer convert demotes the expression back to x's type. */
1711 (for minmax (min max)
1713 (convert (minmax@0 (convert @1) INTEGER_CST@2))
1714 (if (INTEGRAL_TYPE_P (type)
1715 && types_match (@1, type) && int_fits_type_p (@2, type)
1716 && TYPE_SIGN (TREE_TYPE (@0)) == TYPE_SIGN (type)
1717 && TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type))
1718 (minmax @1 (convert @2)))))
1720 (for minmax (FMIN FMAX)
1721 /* If either argument is NaN, return the other one. Avoid the
1722 transformation if we get (and honor) a signalling NaN. */
1724 (minmax:c @0 REAL_CST@1)
1725 (if (real_isnan (TREE_REAL_CST_PTR (@1))
1726 && (!HONOR_SNANS (@1) || !TREE_REAL_CST (@1).signalling))
1728 /* Convert fmin/fmax to MIN_EXPR/MAX_EXPR. C99 requires these
1729 functions to return the numeric arg if the other one is NaN.
1730 MIN and MAX don't honor that, so only transform if -ffinite-math-only
1731 is set. C99 doesn't require -0.0 to be handled, so we don't have to
1732 worry about it either. */
1733 (if (flag_finite_math_only)
1740 /* min (-A, -B) -> -max (A, B) */
1741 (for minmax (min max FMIN FMAX)
1742 maxmin (max min FMAX FMIN)
1744 (minmax (negate:s@2 @0) (negate:s@3 @1))
1745 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
1746 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1747 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
1748 (negate (maxmin @0 @1)))))
1749 /* MIN (~X, ~Y) -> ~MAX (X, Y)
1750 MAX (~X, ~Y) -> ~MIN (X, Y) */
1751 (for minmax (min max)
1754 (minmax (bit_not:s@2 @0) (bit_not:s@3 @1))
1755 (bit_not (maxmin @0 @1))))
1757 /* MIN (X, Y) == X -> X <= Y */
1758 (for minmax (min min max max)
1762 (cmp:c (minmax:c @0 @1) @0)
1763 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0)))
1765 /* MIN (X, 5) == 0 -> X == 0
1766 MIN (X, 5) == 7 -> false */
1769 (cmp (min @0 INTEGER_CST@1) INTEGER_CST@2)
1770 (if (wi::lt_p (@1, @2, TYPE_SIGN (TREE_TYPE (@0))))
1771 { constant_boolean_node (cmp == NE_EXPR, type); }
1772 (if (wi::gt_p (@1, @2, TYPE_SIGN (TREE_TYPE (@0))))
1776 (cmp (max @0 INTEGER_CST@1) INTEGER_CST@2)
1777 (if (wi::gt_p (@1, @2, TYPE_SIGN (TREE_TYPE (@0))))
1778 { constant_boolean_node (cmp == NE_EXPR, type); }
1779 (if (wi::lt_p (@1, @2, TYPE_SIGN (TREE_TYPE (@0))))
1781 /* MIN (X, C1) < C2 -> X < C2 || C1 < C2 */
1782 (for minmax (min min max max min min max max )
1783 cmp (lt le gt ge gt ge lt le )
1784 comb (bit_ior bit_ior bit_ior bit_ior bit_and bit_and bit_and bit_and)
1786 (cmp (minmax @0 INTEGER_CST@1) INTEGER_CST@2)
1787 (comb (cmp @0 @2) (cmp @1 @2))))
1789 /* Simplifications of shift and rotates. */
1791 (for rotate (lrotate rrotate)
1793 (rotate integer_all_onesp@0 @1)
1796 /* Optimize -1 >> x for arithmetic right shifts. */
1798 (rshift integer_all_onesp@0 @1)
1799 (if (!TYPE_UNSIGNED (type)
1800 && tree_expr_nonnegative_p (@1))
1803 /* Optimize (x >> c) << c into x & (-1<<c). */
1805 (lshift (rshift @0 INTEGER_CST@1) @1)
1806 (if (wi::ltu_p (@1, element_precision (type)))
1807 (bit_and @0 (lshift { build_minus_one_cst (type); } @1))))
1809 /* Optimize (x << c) >> c into x & ((unsigned)-1 >> c) for unsigned
1812 (rshift (lshift @0 INTEGER_CST@1) @1)
1813 (if (TYPE_UNSIGNED (type)
1814 && (wi::ltu_p (@1, element_precision (type))))
1815 (bit_and @0 (rshift { build_minus_one_cst (type); } @1))))
1817 (for shiftrotate (lrotate rrotate lshift rshift)
1819 (shiftrotate @0 integer_zerop)
1822 (shiftrotate integer_zerop@0 @1)
1824 /* Prefer vector1 << scalar to vector1 << vector2
1825 if vector2 is uniform. */
1826 (for vec (VECTOR_CST CONSTRUCTOR)
1828 (shiftrotate @0 vec@1)
1829 (with { tree tem = uniform_vector_p (@1); }
1831 (shiftrotate @0 { tem; }))))))
1833 /* Simplify X << Y where Y's low width bits are 0 to X, as only valid
1834 Y is 0. Similarly for X >> Y. */
1836 (for shift (lshift rshift)
1838 (shift @0 SSA_NAME@1)
1839 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1)))
1841 int width = ceil_log2 (element_precision (TREE_TYPE (@0)));
1842 int prec = TYPE_PRECISION (TREE_TYPE (@1));
1844 (if ((get_nonzero_bits (@1) & wi::mask (width, false, prec)) == 0)
1848 /* Rewrite an LROTATE_EXPR by a constant into an
1849 RROTATE_EXPR by a new constant. */
1851 (lrotate @0 INTEGER_CST@1)
1852 (rrotate @0 { const_binop (MINUS_EXPR, TREE_TYPE (@1),
1853 build_int_cst (TREE_TYPE (@1),
1854 element_precision (type)), @1); }))
1856 /* Turn (a OP c1) OP c2 into a OP (c1+c2). */
1857 (for op (lrotate rrotate rshift lshift)
1859 (op (op @0 INTEGER_CST@1) INTEGER_CST@2)
1860 (with { unsigned int prec = element_precision (type); }
1861 (if (wi::ge_p (@1, 0, TYPE_SIGN (TREE_TYPE (@1)))
1862 && wi::lt_p (@1, prec, TYPE_SIGN (TREE_TYPE (@1)))
1863 && wi::ge_p (@2, 0, TYPE_SIGN (TREE_TYPE (@2)))
1864 && wi::lt_p (@2, prec, TYPE_SIGN (TREE_TYPE (@2))))
1865 (with { unsigned int low = wi::add (@1, @2).to_uhwi (); }
1866 /* Deal with a OP (c1 + c2) being undefined but (a OP c1) OP c2
1867 being well defined. */
1869 (if (op == LROTATE_EXPR || op == RROTATE_EXPR)
1870 (op @0 { build_int_cst (TREE_TYPE (@1), low % prec); })
1871 (if (TYPE_UNSIGNED (type) || op == LSHIFT_EXPR)
1872 { build_zero_cst (type); }
1873 (op @0 { build_int_cst (TREE_TYPE (@1), prec - 1); })))
1874 (op @0 { build_int_cst (TREE_TYPE (@1), low); })))))))
1877 /* ((1 << A) & 1) != 0 -> A == 0
1878 ((1 << A) & 1) == 0 -> A != 0 */
1882 (cmp (bit_and (lshift integer_onep @0) integer_onep) integer_zerop)
1883 (icmp @0 { build_zero_cst (TREE_TYPE (@0)); })))
1885 /* (CST1 << A) == CST2 -> A == ctz (CST2) - ctz (CST1)
1886 (CST1 << A) != CST2 -> A != ctz (CST2) - ctz (CST1)
1890 (cmp (lshift INTEGER_CST@0 @1) INTEGER_CST@2)
1891 (with { int cand = wi::ctz (@2) - wi::ctz (@0); }
1893 || (!integer_zerop (@2)
1894 && wi::ne_p (wi::lshift (@0, cand), @2)))
1895 { constant_boolean_node (cmp == NE_EXPR, type); }
1896 (if (!integer_zerop (@2)
1897 && wi::eq_p (wi::lshift (@0, cand), @2))
1898 (cmp @1 { build_int_cst (TREE_TYPE (@1), cand); }))))))
1900 /* Fold (X << C1) & C2 into (X << C1) & (C2 | ((1 << C1) - 1))
1901 (X >> C1) & C2 into (X >> C1) & (C2 | ~((type) -1 >> C1))
1902 if the new mask might be further optimized. */
1903 (for shift (lshift rshift)
1905 (bit_and (convert?:s@4 (shift:s@5 (convert1?@3 @0) INTEGER_CST@1))
1907 (if (tree_nop_conversion_p (TREE_TYPE (@4), TREE_TYPE (@5))
1908 && TYPE_PRECISION (type) <= HOST_BITS_PER_WIDE_INT
1909 && tree_fits_uhwi_p (@1)
1910 && tree_to_uhwi (@1) > 0
1911 && tree_to_uhwi (@1) < TYPE_PRECISION (type))
1914 unsigned int shiftc = tree_to_uhwi (@1);
1915 unsigned HOST_WIDE_INT mask = TREE_INT_CST_LOW (@2);
1916 unsigned HOST_WIDE_INT newmask, zerobits = 0;
1917 tree shift_type = TREE_TYPE (@3);
1920 if (shift == LSHIFT_EXPR)
1921 zerobits = ((HOST_WIDE_INT_1U << shiftc) - 1);
1922 else if (shift == RSHIFT_EXPR
1923 && (TYPE_PRECISION (shift_type)
1924 == GET_MODE_PRECISION (TYPE_MODE (shift_type))))
1926 prec = TYPE_PRECISION (TREE_TYPE (@3));
1928 /* See if more bits can be proven as zero because of
1931 && TYPE_UNSIGNED (TREE_TYPE (@0)))
1933 tree inner_type = TREE_TYPE (@0);
1934 if ((TYPE_PRECISION (inner_type)
1935 == GET_MODE_PRECISION (TYPE_MODE (inner_type)))
1936 && TYPE_PRECISION (inner_type) < prec)
1938 prec = TYPE_PRECISION (inner_type);
1939 /* See if we can shorten the right shift. */
1941 shift_type = inner_type;
1942 /* Otherwise X >> C1 is all zeros, so we'll optimize
1943 it into (X, 0) later on by making sure zerobits
1947 zerobits = HOST_WIDE_INT_M1U;
1950 zerobits >>= HOST_BITS_PER_WIDE_INT - shiftc;
1951 zerobits <<= prec - shiftc;
1953 /* For arithmetic shift if sign bit could be set, zerobits
1954 can contain actually sign bits, so no transformation is
1955 possible, unless MASK masks them all away. In that
1956 case the shift needs to be converted into logical shift. */
1957 if (!TYPE_UNSIGNED (TREE_TYPE (@3))
1958 && prec == TYPE_PRECISION (TREE_TYPE (@3)))
1960 if ((mask & zerobits) == 0)
1961 shift_type = unsigned_type_for (TREE_TYPE (@3));
1967 /* ((X << 16) & 0xff00) is (X, 0). */
1968 (if ((mask & zerobits) == mask)
1969 { build_int_cst (type, 0); }
1970 (with { newmask = mask | zerobits; }
1971 (if (newmask != mask && (newmask & (newmask + 1)) == 0)
1974 /* Only do the transformation if NEWMASK is some integer
1976 for (prec = BITS_PER_UNIT;
1977 prec < HOST_BITS_PER_WIDE_INT; prec <<= 1)
1978 if (newmask == (HOST_WIDE_INT_1U << prec) - 1)
1981 (if (prec < HOST_BITS_PER_WIDE_INT
1982 || newmask == HOST_WIDE_INT_M1U)
1984 { tree newmaskt = build_int_cst_type (TREE_TYPE (@2), newmask); }
1985 (if (!tree_int_cst_equal (newmaskt, @2))
1986 (if (shift_type != TREE_TYPE (@3))
1987 (bit_and (convert (shift:shift_type (convert @3) @1)) { newmaskt; })
1988 (bit_and @4 { newmaskt; })))))))))))))
1990 /* Fold (X {&,^,|} C2) << C1 into (X << C1) {&,^,|} (C2 << C1)
1991 (X {&,^,|} C2) >> C1 into (X >> C1) & (C2 >> C1). */
1992 (for shift (lshift rshift)
1993 (for bit_op (bit_and bit_xor bit_ior)
1995 (shift (convert?:s (bit_op:s @0 INTEGER_CST@2)) INTEGER_CST@1)
1996 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1997 (with { tree mask = int_const_binop (shift, fold_convert (type, @2), @1); }
1998 (bit_op (shift (convert @0) @1) { mask; }))))))
2000 /* ~(~X >> Y) -> X >> Y (for arithmetic shift). */
2002 (bit_not (convert1?:s (rshift:s (convert2?@0 (bit_not @1)) @2)))
2003 (if (!TYPE_UNSIGNED (TREE_TYPE (@0))
2004 && (element_precision (TREE_TYPE (@0))
2005 <= element_precision (TREE_TYPE (@1))
2006 || !TYPE_UNSIGNED (TREE_TYPE (@1))))
2008 { tree shift_type = TREE_TYPE (@0); }
2009 (convert (rshift (convert:shift_type @1) @2)))))
2011 /* ~(~X >>r Y) -> X >>r Y
2012 ~(~X <<r Y) -> X <<r Y */
2013 (for rotate (lrotate rrotate)
2015 (bit_not (convert1?:s (rotate:s (convert2?@0 (bit_not @1)) @2)))
2016 (if ((element_precision (TREE_TYPE (@0))
2017 <= element_precision (TREE_TYPE (@1))
2018 || !TYPE_UNSIGNED (TREE_TYPE (@1)))
2019 && (element_precision (type) <= element_precision (TREE_TYPE (@0))
2020 || !TYPE_UNSIGNED (TREE_TYPE (@0))))
2022 { tree rotate_type = TREE_TYPE (@0); }
2023 (convert (rotate (convert:rotate_type @1) @2))))))
2025 /* Simplifications of conversions. */
2027 /* Basic strip-useless-type-conversions / strip_nops. */
2028 (for cvt (convert view_convert float fix_trunc)
2031 (if ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@0)))
2032 || (GENERIC && type == TREE_TYPE (@0)))
2035 /* Contract view-conversions. */
2037 (view_convert (view_convert @0))
2040 /* For integral conversions with the same precision or pointer
2041 conversions use a NOP_EXPR instead. */
2044 (if ((INTEGRAL_TYPE_P (type) || POINTER_TYPE_P (type))
2045 && (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
2046 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (@0)))
2049 /* Strip inner integral conversions that do not change precision or size, or
2050 zero-extend while keeping the same size (for bool-to-char). */
2052 (view_convert (convert@0 @1))
2053 (if ((INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
2054 && (INTEGRAL_TYPE_P (TREE_TYPE (@1)) || POINTER_TYPE_P (TREE_TYPE (@1)))
2055 && TYPE_SIZE (TREE_TYPE (@0)) == TYPE_SIZE (TREE_TYPE (@1))
2056 && (TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1))
2057 || (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (TREE_TYPE (@1))
2058 && TYPE_UNSIGNED (TREE_TYPE (@1)))))
2061 /* Re-association barriers around constants and other re-association
2062 barriers can be removed. */
2064 (paren CONSTANT_CLASS_P@0)
2067 (paren (paren@1 @0))
2070 /* Handle cases of two conversions in a row. */
2071 (for ocvt (convert float fix_trunc)
2072 (for icvt (convert float)
2077 tree inside_type = TREE_TYPE (@0);
2078 tree inter_type = TREE_TYPE (@1);
2079 int inside_int = INTEGRAL_TYPE_P (inside_type);
2080 int inside_ptr = POINTER_TYPE_P (inside_type);
2081 int inside_float = FLOAT_TYPE_P (inside_type);
2082 int inside_vec = VECTOR_TYPE_P (inside_type);
2083 unsigned int inside_prec = TYPE_PRECISION (inside_type);
2084 int inside_unsignedp = TYPE_UNSIGNED (inside_type);
2085 int inter_int = INTEGRAL_TYPE_P (inter_type);
2086 int inter_ptr = POINTER_TYPE_P (inter_type);
2087 int inter_float = FLOAT_TYPE_P (inter_type);
2088 int inter_vec = VECTOR_TYPE_P (inter_type);
2089 unsigned int inter_prec = TYPE_PRECISION (inter_type);
2090 int inter_unsignedp = TYPE_UNSIGNED (inter_type);
2091 int final_int = INTEGRAL_TYPE_P (type);
2092 int final_ptr = POINTER_TYPE_P (type);
2093 int final_float = FLOAT_TYPE_P (type);
2094 int final_vec = VECTOR_TYPE_P (type);
2095 unsigned int final_prec = TYPE_PRECISION (type);
2096 int final_unsignedp = TYPE_UNSIGNED (type);
2099 /* In addition to the cases of two conversions in a row
2100 handled below, if we are converting something to its own
2101 type via an object of identical or wider precision, neither
2102 conversion is needed. */
2103 (if (((GIMPLE && useless_type_conversion_p (type, inside_type))
2105 && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (inside_type)))
2106 && (((inter_int || inter_ptr) && final_int)
2107 || (inter_float && final_float))
2108 && inter_prec >= final_prec)
2111 /* Likewise, if the intermediate and initial types are either both
2112 float or both integer, we don't need the middle conversion if the
2113 former is wider than the latter and doesn't change the signedness
2114 (for integers). Avoid this if the final type is a pointer since
2115 then we sometimes need the middle conversion. */
2116 (if (((inter_int && inside_int) || (inter_float && inside_float))
2117 && (final_int || final_float)
2118 && inter_prec >= inside_prec
2119 && (inter_float || inter_unsignedp == inside_unsignedp))
2122 /* If we have a sign-extension of a zero-extended value, we can
2123 replace that by a single zero-extension. Likewise if the
2124 final conversion does not change precision we can drop the
2125 intermediate conversion. */
2126 (if (inside_int && inter_int && final_int
2127 && ((inside_prec < inter_prec && inter_prec < final_prec
2128 && inside_unsignedp && !inter_unsignedp)
2129 || final_prec == inter_prec))
2132 /* Two conversions in a row are not needed unless:
2133 - some conversion is floating-point (overstrict for now), or
2134 - some conversion is a vector (overstrict for now), or
2135 - the intermediate type is narrower than both initial and
2137 - the intermediate type and innermost type differ in signedness,
2138 and the outermost type is wider than the intermediate, or
2139 - the initial type is a pointer type and the precisions of the
2140 intermediate and final types differ, or
2141 - the final type is a pointer type and the precisions of the
2142 initial and intermediate types differ. */
2143 (if (! inside_float && ! inter_float && ! final_float
2144 && ! inside_vec && ! inter_vec && ! final_vec
2145 && (inter_prec >= inside_prec || inter_prec >= final_prec)
2146 && ! (inside_int && inter_int
2147 && inter_unsignedp != inside_unsignedp
2148 && inter_prec < final_prec)
2149 && ((inter_unsignedp && inter_prec > inside_prec)
2150 == (final_unsignedp && final_prec > inter_prec))
2151 && ! (inside_ptr && inter_prec != final_prec)
2152 && ! (final_ptr && inside_prec != inter_prec))
2155 /* A truncation to an unsigned type (a zero-extension) should be
2156 canonicalized as bitwise and of a mask. */
2157 (if (GIMPLE /* PR70366: doing this in GENERIC breaks -Wconversion. */
2158 && final_int && inter_int && inside_int
2159 && final_prec == inside_prec
2160 && final_prec > inter_prec
2162 (convert (bit_and @0 { wide_int_to_tree
2164 wi::mask (inter_prec, false,
2165 TYPE_PRECISION (inside_type))); })))
2167 /* If we are converting an integer to a floating-point that can
2168 represent it exactly and back to an integer, we can skip the
2169 floating-point conversion. */
2170 (if (GIMPLE /* PR66211 */
2171 && inside_int && inter_float && final_int &&
2172 (unsigned) significand_size (TYPE_MODE (inter_type))
2173 >= inside_prec - !inside_unsignedp)
2176 /* If we have a narrowing conversion to an integral type that is fed by a
2177 BIT_AND_EXPR, we might be able to remove the BIT_AND_EXPR if it merely
2178 masks off bits outside the final type (and nothing else). */
2180 (convert (bit_and @0 INTEGER_CST@1))
2181 (if (INTEGRAL_TYPE_P (type)
2182 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
2183 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))
2184 && operand_equal_p (@1, build_low_bits_mask (TREE_TYPE (@1),
2185 TYPE_PRECISION (type)), 0))
2189 /* (X /[ex] A) * A -> X. */
2191 (mult (convert1? (exact_div @0 @@1)) (convert2? @1))
2194 /* Canonicalization of binary operations. */
2196 /* Convert X + -C into X - C. */
2198 (plus @0 REAL_CST@1)
2199 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
2200 (with { tree tem = const_unop (NEGATE_EXPR, type, @1); }
2201 (if (!TREE_OVERFLOW (tem) || !flag_trapping_math)
2202 (minus @0 { tem; })))))
2204 /* Convert x+x into x*2. */
2207 (if (SCALAR_FLOAT_TYPE_P (type))
2208 (mult @0 { build_real (type, dconst2); })
2209 (if (INTEGRAL_TYPE_P (type))
2210 (mult @0 { build_int_cst (type, 2); }))))
2213 (minus integer_zerop @1)
2216 /* (ARG0 - ARG1) is the same as (-ARG1 + ARG0). So check whether
2217 ARG0 is zero and X + ARG0 reduces to X, since that would mean
2218 (-ARG1 + ARG0) reduces to -ARG1. */
2220 (minus real_zerop@0 @1)
2221 (if (fold_real_zero_addition_p (type, @0, 0))
2224 /* Transform x * -1 into -x. */
2226 (mult @0 integer_minus_onep)
2229 /* Reassociate (X * CST) * Y to (X * Y) * CST. This does not introduce
2230 signed overflow for CST != 0 && CST != -1. */
2232 (mult:c (mult:s @0 INTEGER_CST@1) @2)
2233 (if (TREE_CODE (@2) != INTEGER_CST
2234 && !integer_zerop (@1) && !integer_minus_onep (@1))
2235 (mult (mult @0 @2) @1)))
2237 /* True if we can easily extract the real and imaginary parts of a complex
2239 (match compositional_complex
2240 (convert? (complex @0 @1)))
2242 /* COMPLEX_EXPR and REALPART/IMAGPART_EXPR cancellations. */
2244 (complex (realpart @0) (imagpart @0))
2247 (realpart (complex @0 @1))
2250 (imagpart (complex @0 @1))
2253 /* Sometimes we only care about half of a complex expression. */
2255 (realpart (convert?:s (conj:s @0)))
2256 (convert (realpart @0)))
2258 (imagpart (convert?:s (conj:s @0)))
2259 (convert (negate (imagpart @0))))
2260 (for part (realpart imagpart)
2261 (for op (plus minus)
2263 (part (convert?:s@2 (op:s @0 @1)))
2264 (convert (op (part @0) (part @1))))))
2266 (realpart (convert?:s (CEXPI:s @0)))
2269 (imagpart (convert?:s (CEXPI:s @0)))
2272 /* conj(conj(x)) -> x */
2274 (conj (convert? (conj @0)))
2275 (if (tree_nop_conversion_p (TREE_TYPE (@0), type))
2278 /* conj({x,y}) -> {x,-y} */
2280 (conj (convert?:s (complex:s @0 @1)))
2281 (with { tree itype = TREE_TYPE (type); }
2282 (complex (convert:itype @0) (negate (convert:itype @1)))))
2284 /* BSWAP simplifications, transforms checked by gcc.dg/builtin-bswap-8.c. */
2285 (for bswap (BUILT_IN_BSWAP16 BUILT_IN_BSWAP32 BUILT_IN_BSWAP64)
2290 (bswap (bit_not (bswap @0)))
2292 (for bitop (bit_xor bit_ior bit_and)
2294 (bswap (bitop:c (bswap @0) @1))
2295 (bitop @0 (bswap @1)))))
2298 /* Combine COND_EXPRs and VEC_COND_EXPRs. */
2300 /* Simplify constant conditions.
2301 Only optimize constant conditions when the selected branch
2302 has the same type as the COND_EXPR. This avoids optimizing
2303 away "c ? x : throw", where the throw has a void type.
2304 Note that we cannot throw away the fold-const.c variant nor
2305 this one as we depend on doing this transform before possibly
2306 A ? B : B -> B triggers and the fold-const.c one can optimize
2307 0 ? A : B to B even if A has side-effects. Something
2308 genmatch cannot handle. */
2310 (cond INTEGER_CST@0 @1 @2)
2311 (if (integer_zerop (@0))
2312 (if (!VOID_TYPE_P (TREE_TYPE (@2)) || VOID_TYPE_P (type))
2314 (if (!VOID_TYPE_P (TREE_TYPE (@1)) || VOID_TYPE_P (type))
2317 (vec_cond VECTOR_CST@0 @1 @2)
2318 (if (integer_all_onesp (@0))
2320 (if (integer_zerop (@0))
2323 /* Simplification moved from fold_cond_expr_with_comparison. It may also
2325 /* This pattern implements two kinds simplification:
2328 (cond (cmp (convert1? x) c1) (convert2? x) c2) -> (minmax (x c)) if:
2329 1) Conversions are type widening from smaller type.
2330 2) Const c1 equals to c2 after canonicalizing comparison.
2331 3) Comparison has tree code LT, LE, GT or GE.
2332 This specific pattern is needed when (cmp (convert x) c) may not
2333 be simplified by comparison patterns because of multiple uses of
2334 x. It also makes sense here because simplifying across multiple
2335 referred var is always benefitial for complicated cases.
2338 (cond (eq (convert1? x) c1) (convert2? x) c2) -> (cond (eq x c1) c1 c2). */
2339 (for cmp (lt le gt ge eq)
2341 (cond (cmp (convert1? @1) INTEGER_CST@3) (convert2? @1) INTEGER_CST@2)
2344 tree from_type = TREE_TYPE (@1);
2345 tree c1_type = TREE_TYPE (@3), c2_type = TREE_TYPE (@2);
2346 enum tree_code code = ERROR_MARK;
2348 if (INTEGRAL_TYPE_P (from_type)
2349 && int_fits_type_p (@2, from_type)
2350 && (types_match (c1_type, from_type)
2351 || (TYPE_PRECISION (c1_type) > TYPE_PRECISION (from_type)
2352 && (TYPE_UNSIGNED (from_type)
2353 || TYPE_SIGN (c1_type) == TYPE_SIGN (from_type))))
2354 && (types_match (c2_type, from_type)
2355 || (TYPE_PRECISION (c2_type) > TYPE_PRECISION (from_type)
2356 && (TYPE_UNSIGNED (from_type)
2357 || TYPE_SIGN (c2_type) == TYPE_SIGN (from_type)))))
2361 if (wi::to_widest (@3) == (wi::to_widest (@2) - 1))
2363 /* X <= Y - 1 equals to X < Y. */
2366 /* X > Y - 1 equals to X >= Y. */
2370 if (wi::to_widest (@3) == (wi::to_widest (@2) + 1))
2372 /* X < Y + 1 equals to X <= Y. */
2375 /* X >= Y + 1 equals to X > Y. */
2379 if (code != ERROR_MARK
2380 || wi::to_widest (@2) == wi::to_widest (@3))
2382 if (cmp == LT_EXPR || cmp == LE_EXPR)
2384 if (cmp == GT_EXPR || cmp == GE_EXPR)
2388 /* Can do A == C1 ? A : C2 -> A == C1 ? C1 : C2? */
2389 else if (int_fits_type_p (@3, from_type))
2393 (if (code == MAX_EXPR)
2394 (convert (max @1 (convert @2)))
2395 (if (code == MIN_EXPR)
2396 (convert (min @1 (convert @2)))
2397 (if (code == EQ_EXPR)
2398 (convert (cond (eq @1 (convert @3))
2399 (convert:from_type @3) (convert:from_type @2)))))))))
2401 /* (cond (cmp (convert? x) c1) (op x c2) c3) -> (op (minmax x c1) c2) if:
2403 1) OP is PLUS or MINUS.
2404 2) CMP is LT, LE, GT or GE.
2405 3) C3 == (C1 op C2), and computation doesn't have undefined behavior.
2407 This pattern also handles special cases like:
2409 A) Operand x is a unsigned to signed type conversion and c1 is
2410 integer zero. In this case,
2411 (signed type)x < 0 <=> x > MAX_VAL(signed type)
2412 (signed type)x >= 0 <=> x <= MAX_VAL(signed type)
2413 B) Const c1 may not equal to (C3 op' C2). In this case we also
2414 check equality for (c1+1) and (c1-1) by adjusting comparison
2417 TODO: Though signed type is handled by this pattern, it cannot be
2418 simplified at the moment because C standard requires additional
2419 type promotion. In order to match&simplify it here, the IR needs
2420 to be cleaned up by other optimizers, i.e, VRP. */
2421 (for op (plus minus)
2422 (for cmp (lt le gt ge)
2424 (cond (cmp (convert? @X) INTEGER_CST@1) (op @X INTEGER_CST@2) INTEGER_CST@3)
2425 (with { tree from_type = TREE_TYPE (@X), to_type = TREE_TYPE (@1); }
2426 (if (types_match (from_type, to_type)
2427 /* Check if it is special case A). */
2428 || (TYPE_UNSIGNED (from_type)
2429 && !TYPE_UNSIGNED (to_type)
2430 && TYPE_PRECISION (from_type) == TYPE_PRECISION (to_type)
2431 && integer_zerop (@1)
2432 && (cmp == LT_EXPR || cmp == GE_EXPR)))
2435 bool overflow = false;
2436 enum tree_code code, cmp_code = cmp;
2437 wide_int real_c1, c1 = @1, c2 = @2, c3 = @3;
2438 signop sgn = TYPE_SIGN (from_type);
2440 /* Handle special case A), given x of unsigned type:
2441 ((signed type)x < 0) <=> (x > MAX_VAL(signed type))
2442 ((signed type)x >= 0) <=> (x <= MAX_VAL(signed type)) */
2443 if (!types_match (from_type, to_type))
2445 if (cmp_code == LT_EXPR)
2447 if (cmp_code == GE_EXPR)
2449 c1 = wi::max_value (to_type);
2451 /* To simplify this pattern, we require c3 = (c1 op c2). Here we
2452 compute (c3 op' c2) and check if it equals to c1 with op' being
2453 the inverted operator of op. Make sure overflow doesn't happen
2454 if it is undefined. */
2455 if (op == PLUS_EXPR)
2456 real_c1 = wi::sub (c3, c2, sgn, &overflow);
2458 real_c1 = wi::add (c3, c2, sgn, &overflow);
2461 if (!overflow || !TYPE_OVERFLOW_UNDEFINED (from_type))
2463 /* Check if c1 equals to real_c1. Boundary condition is handled
2464 by adjusting comparison operation if necessary. */
2465 if (!wi::cmp (wi::sub (real_c1, 1, sgn, &overflow), c1, sgn)
2468 /* X <= Y - 1 equals to X < Y. */
2469 if (cmp_code == LE_EXPR)
2471 /* X > Y - 1 equals to X >= Y. */
2472 if (cmp_code == GT_EXPR)
2475 if (!wi::cmp (wi::add (real_c1, 1, sgn, &overflow), c1, sgn)
2478 /* X < Y + 1 equals to X <= Y. */
2479 if (cmp_code == LT_EXPR)
2481 /* X >= Y + 1 equals to X > Y. */
2482 if (cmp_code == GE_EXPR)
2485 if (code != cmp_code || !wi::cmp (real_c1, c1, sgn))
2487 if (cmp_code == LT_EXPR || cmp_code == LE_EXPR)
2489 if (cmp_code == GT_EXPR || cmp_code == GE_EXPR)
2494 (if (code == MAX_EXPR)
2495 (op (max @X { wide_int_to_tree (from_type, real_c1); })
2496 { wide_int_to_tree (from_type, c2); })
2497 (if (code == MIN_EXPR)
2498 (op (min @X { wide_int_to_tree (from_type, real_c1); })
2499 { wide_int_to_tree (from_type, c2); })))))))))
2501 (for cnd (cond vec_cond)
2502 /* A ? B : (A ? X : C) -> A ? B : C. */
2504 (cnd @0 (cnd @0 @1 @2) @3)
2507 (cnd @0 @1 (cnd @0 @2 @3))
2509 /* A ? B : (!A ? C : X) -> A ? B : C. */
2510 /* ??? This matches embedded conditions open-coded because genmatch
2511 would generate matching code for conditions in separate stmts only.
2512 The following is still important to merge then and else arm cases
2513 from if-conversion. */
2515 (cnd @0 @1 (cnd @2 @3 @4))
2516 (if (COMPARISON_CLASS_P (@0)
2517 && COMPARISON_CLASS_P (@2)
2518 && invert_tree_comparison
2519 (TREE_CODE (@0), HONOR_NANS (TREE_OPERAND (@0, 0))) == TREE_CODE (@2)
2520 && operand_equal_p (TREE_OPERAND (@0, 0), TREE_OPERAND (@2, 0), 0)
2521 && operand_equal_p (TREE_OPERAND (@0, 1), TREE_OPERAND (@2, 1), 0))
2524 (cnd @0 (cnd @1 @2 @3) @4)
2525 (if (COMPARISON_CLASS_P (@0)
2526 && COMPARISON_CLASS_P (@1)
2527 && invert_tree_comparison
2528 (TREE_CODE (@0), HONOR_NANS (TREE_OPERAND (@0, 0))) == TREE_CODE (@1)
2529 && operand_equal_p (TREE_OPERAND (@0, 0), TREE_OPERAND (@1, 0), 0)
2530 && operand_equal_p (TREE_OPERAND (@0, 1), TREE_OPERAND (@1, 1), 0))
2533 /* A ? B : B -> B. */
2538 /* !A ? B : C -> A ? C : B. */
2540 (cnd (logical_inverted_value truth_valued_p@0) @1 @2)
2543 /* A + (B vcmp C ? 1 : 0) -> A - (B vcmp C ? -1 : 0), since vector comparisons
2544 return all -1 or all 0 results. */
2545 /* ??? We could instead convert all instances of the vec_cond to negate,
2546 but that isn't necessarily a win on its own. */
2548 (plus:c @3 (view_convert? (vec_cond:s @0 integer_each_onep@1 integer_zerop@2)))
2549 (if (VECTOR_TYPE_P (type)
2550 && TYPE_VECTOR_SUBPARTS (type) == TYPE_VECTOR_SUBPARTS (TREE_TYPE (@1))
2551 && (TYPE_MODE (TREE_TYPE (type))
2552 == TYPE_MODE (TREE_TYPE (TREE_TYPE (@1)))))
2553 (minus @3 (view_convert (vec_cond @0 (negate @1) @2)))))
2555 /* ... likewise A - (B vcmp C ? 1 : 0) -> A + (B vcmp C ? -1 : 0). */
2557 (minus @3 (view_convert? (vec_cond:s @0 integer_each_onep@1 integer_zerop@2)))
2558 (if (VECTOR_TYPE_P (type)
2559 && TYPE_VECTOR_SUBPARTS (type) == TYPE_VECTOR_SUBPARTS (TREE_TYPE (@1))
2560 && (TYPE_MODE (TREE_TYPE (type))
2561 == TYPE_MODE (TREE_TYPE (TREE_TYPE (@1)))))
2562 (plus @3 (view_convert (vec_cond @0 (negate @1) @2)))))
2565 /* Simplifications of comparisons. */
2567 /* See if we can reduce the magnitude of a constant involved in a
2568 comparison by changing the comparison code. This is a canonicalization
2569 formerly done by maybe_canonicalize_comparison_1. */
2573 (cmp @0 INTEGER_CST@1)
2574 (if (tree_int_cst_sgn (@1) == -1)
2575 (acmp @0 { wide_int_to_tree (TREE_TYPE (@1), wi::add (@1, 1)); }))))
2579 (cmp @0 INTEGER_CST@1)
2580 (if (tree_int_cst_sgn (@1) == 1)
2581 (acmp @0 { wide_int_to_tree (TREE_TYPE (@1), wi::sub (@1, 1)); }))))
2584 /* We can simplify a logical negation of a comparison to the
2585 inverted comparison. As we cannot compute an expression
2586 operator using invert_tree_comparison we have to simulate
2587 that with expression code iteration. */
2588 (for cmp (tcc_comparison)
2589 icmp (inverted_tcc_comparison)
2590 ncmp (inverted_tcc_comparison_with_nans)
2591 /* Ideally we'd like to combine the following two patterns
2592 and handle some more cases by using
2593 (logical_inverted_value (cmp @0 @1))
2594 here but for that genmatch would need to "inline" that.
2595 For now implement what forward_propagate_comparison did. */
2597 (bit_not (cmp @0 @1))
2598 (if (VECTOR_TYPE_P (type)
2599 || (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1))
2600 /* Comparison inversion may be impossible for trapping math,
2601 invert_tree_comparison will tell us. But we can't use
2602 a computed operator in the replacement tree thus we have
2603 to play the trick below. */
2604 (with { enum tree_code ic = invert_tree_comparison
2605 (cmp, HONOR_NANS (@0)); }
2611 (bit_xor (cmp @0 @1) integer_truep)
2612 (with { enum tree_code ic = invert_tree_comparison
2613 (cmp, HONOR_NANS (@0)); }
2619 /* Transform comparisons of the form X - Y CMP 0 to X CMP Y.
2620 ??? The transformation is valid for the other operators if overflow
2621 is undefined for the type, but performing it here badly interacts
2622 with the transformation in fold_cond_expr_with_comparison which
2623 attempts to synthetize ABS_EXPR. */
2626 (cmp (minus@2 @0 @1) integer_zerop)
2627 (if (single_use (@2))
2630 /* Transform comparisons of the form X * C1 CMP 0 to X CMP 0 in the
2631 signed arithmetic case. That form is created by the compiler
2632 often enough for folding it to be of value. One example is in
2633 computing loop trip counts after Operator Strength Reduction. */
2634 (for cmp (simple_comparison)
2635 scmp (swapped_simple_comparison)
2637 (cmp (mult@3 @0 INTEGER_CST@1) integer_zerop@2)
2638 /* Handle unfolded multiplication by zero. */
2639 (if (integer_zerop (@1))
2641 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2642 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2644 /* If @1 is negative we swap the sense of the comparison. */
2645 (if (tree_int_cst_sgn (@1) < 0)
2649 /* Simplify comparison of something with itself. For IEEE
2650 floating-point, we can only do some of these simplifications. */
2654 (if (! FLOAT_TYPE_P (TREE_TYPE (@0))
2655 || ! HONOR_NANS (@0))
2656 { constant_boolean_node (true, type); }
2657 (if (cmp != EQ_EXPR)
2663 || ! FLOAT_TYPE_P (TREE_TYPE (@0))
2664 || ! HONOR_NANS (@0))
2665 { constant_boolean_node (false, type); })))
2666 (for cmp (unle unge uneq)
2669 { constant_boolean_node (true, type); }))
2670 (for cmp (unlt ungt)
2676 (if (!flag_trapping_math)
2677 { constant_boolean_node (false, type); }))
2679 /* Fold ~X op ~Y as Y op X. */
2680 (for cmp (simple_comparison)
2682 (cmp (bit_not@2 @0) (bit_not@3 @1))
2683 (if (single_use (@2) && single_use (@3))
2686 /* Fold ~X op C as X op' ~C, where op' is the swapped comparison. */
2687 (for cmp (simple_comparison)
2688 scmp (swapped_simple_comparison)
2690 (cmp (bit_not@2 @0) CONSTANT_CLASS_P@1)
2691 (if (single_use (@2)
2692 && (TREE_CODE (@1) == INTEGER_CST || TREE_CODE (@1) == VECTOR_CST))
2693 (scmp @0 (bit_not @1)))))
2695 (for cmp (simple_comparison)
2696 /* Fold (double)float1 CMP (double)float2 into float1 CMP float2. */
2698 (cmp (convert@2 @0) (convert? @1))
2699 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
2700 && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@2))
2701 == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0)))
2702 && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@2))
2703 == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1))))
2706 tree type1 = TREE_TYPE (@1);
2707 if (TREE_CODE (@1) == REAL_CST && !DECIMAL_FLOAT_TYPE_P (type1))
2709 REAL_VALUE_TYPE orig = TREE_REAL_CST (@1);
2710 if (TYPE_PRECISION (type1) > TYPE_PRECISION (float_type_node)
2711 && exact_real_truncate (TYPE_MODE (float_type_node), &orig))
2712 type1 = float_type_node;
2713 if (TYPE_PRECISION (type1) > TYPE_PRECISION (double_type_node)
2714 && exact_real_truncate (TYPE_MODE (double_type_node), &orig))
2715 type1 = double_type_node;
2718 = (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type1)
2719 ? TREE_TYPE (@0) : type1);
2721 (if (TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (newtype))
2722 (cmp (convert:newtype @0) (convert:newtype @1))))))
2726 /* IEEE doesn't distinguish +0 and -0 in comparisons. */
2728 /* a CMP (-0) -> a CMP 0 */
2729 (if (REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@1)))
2730 (cmp @0 { build_real (TREE_TYPE (@1), dconst0); }))
2731 /* x != NaN is always true, other ops are always false. */
2732 (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
2733 && ! HONOR_SNANS (@1))
2734 { constant_boolean_node (cmp == NE_EXPR, type); })
2735 /* Fold comparisons against infinity. */
2736 (if (REAL_VALUE_ISINF (TREE_REAL_CST (@1))
2737 && MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (@1))))
2740 REAL_VALUE_TYPE max;
2741 enum tree_code code = cmp;
2742 bool neg = REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1));
2744 code = swap_tree_comparison (code);
2747 /* x > +Inf is always false, if with ignore sNANs. */
2748 (if (code == GT_EXPR
2749 && ! HONOR_SNANS (@0))
2750 { constant_boolean_node (false, type); })
2751 (if (code == LE_EXPR)
2752 /* x <= +Inf is always true, if we don't case about NaNs. */
2753 (if (! HONOR_NANS (@0))
2754 { constant_boolean_node (true, type); }
2755 /* x <= +Inf is the same as x == x, i.e. !isnan(x). */
2757 /* x == +Inf and x >= +Inf are always equal to x > DBL_MAX. */
2758 (if (code == EQ_EXPR || code == GE_EXPR)
2759 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
2761 (lt @0 { build_real (TREE_TYPE (@0), max); })
2762 (gt @0 { build_real (TREE_TYPE (@0), max); }))))
2763 /* x < +Inf is always equal to x <= DBL_MAX. */
2764 (if (code == LT_EXPR)
2765 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
2767 (ge @0 { build_real (TREE_TYPE (@0), max); })
2768 (le @0 { build_real (TREE_TYPE (@0), max); }))))
2769 /* x != +Inf is always equal to !(x > DBL_MAX). */
2770 (if (code == NE_EXPR)
2771 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
2772 (if (! HONOR_NANS (@0))
2774 (ge @0 { build_real (TREE_TYPE (@0), max); })
2775 (le @0 { build_real (TREE_TYPE (@0), max); }))
2777 (bit_xor (lt @0 { build_real (TREE_TYPE (@0), max); })
2778 { build_one_cst (type); })
2779 (bit_xor (gt @0 { build_real (TREE_TYPE (@0), max); })
2780 { build_one_cst (type); }))))))))))
2782 /* If this is a comparison of a real constant with a PLUS_EXPR
2783 or a MINUS_EXPR of a real constant, we can convert it into a
2784 comparison with a revised real constant as long as no overflow
2785 occurs when unsafe_math_optimizations are enabled. */
2786 (if (flag_unsafe_math_optimizations)
2787 (for op (plus minus)
2789 (cmp (op @0 REAL_CST@1) REAL_CST@2)
2792 tree tem = const_binop (op == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR,
2793 TREE_TYPE (@1), @2, @1);
2795 (if (tem && !TREE_OVERFLOW (tem))
2796 (cmp @0 { tem; }))))))
2798 /* Likewise, we can simplify a comparison of a real constant with
2799 a MINUS_EXPR whose first operand is also a real constant, i.e.
2800 (c1 - x) < c2 becomes x > c1-c2. Reordering is allowed on
2801 floating-point types only if -fassociative-math is set. */
2802 (if (flag_associative_math)
2804 (cmp (minus REAL_CST@0 @1) REAL_CST@2)
2805 (with { tree tem = const_binop (MINUS_EXPR, TREE_TYPE (@1), @0, @2); }
2806 (if (tem && !TREE_OVERFLOW (tem))
2807 (cmp { tem; } @1)))))
2809 /* Fold comparisons against built-in math functions. */
2810 (if (flag_unsafe_math_optimizations
2811 && ! flag_errno_math)
2814 (cmp (sq @0) REAL_CST@1)
2816 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
2818 /* sqrt(x) < y is always false, if y is negative. */
2819 (if (cmp == EQ_EXPR || cmp == LT_EXPR || cmp == LE_EXPR)
2820 { constant_boolean_node (false, type); })
2821 /* sqrt(x) > y is always true, if y is negative and we
2822 don't care about NaNs, i.e. negative values of x. */
2823 (if (cmp == NE_EXPR || !HONOR_NANS (@0))
2824 { constant_boolean_node (true, type); })
2825 /* sqrt(x) > y is the same as x >= 0, if y is negative. */
2826 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })))
2827 (if (real_equal (TREE_REAL_CST_PTR (@1), &dconst0))
2829 /* sqrt(x) < 0 is always false. */
2830 (if (cmp == LT_EXPR)
2831 { constant_boolean_node (false, type); })
2832 /* sqrt(x) >= 0 is always true if we don't care about NaNs. */
2833 (if (cmp == GE_EXPR && !HONOR_NANS (@0))
2834 { constant_boolean_node (true, type); })
2835 /* sqrt(x) <= 0 -> x == 0. */
2836 (if (cmp == LE_EXPR)
2838 /* Otherwise sqrt(x) cmp 0 -> x cmp 0. Here cmp can be >=, >,
2839 == or !=. In the last case:
2841 (sqrt(x) != 0) == (NaN != 0) == true == (x != 0)
2843 if x is negative or NaN. Due to -funsafe-math-optimizations,
2844 the results for other x follow from natural arithmetic. */
2846 (if (cmp == GT_EXPR || cmp == GE_EXPR)
2850 real_arithmetic (&c2, MULT_EXPR,
2851 &TREE_REAL_CST (@1), &TREE_REAL_CST (@1));
2852 real_convert (&c2, TYPE_MODE (TREE_TYPE (@0)), &c2);
2854 (if (REAL_VALUE_ISINF (c2))
2855 /* sqrt(x) > y is x == +Inf, when y is very large. */
2856 (if (HONOR_INFINITIES (@0))
2857 (eq @0 { build_real (TREE_TYPE (@0), c2); })
2858 { constant_boolean_node (false, type); })
2859 /* sqrt(x) > c is the same as x > c*c. */
2860 (cmp @0 { build_real (TREE_TYPE (@0), c2); }))))
2861 (if (cmp == LT_EXPR || cmp == LE_EXPR)
2865 real_arithmetic (&c2, MULT_EXPR,
2866 &TREE_REAL_CST (@1), &TREE_REAL_CST (@1));
2867 real_convert (&c2, TYPE_MODE (TREE_TYPE (@0)), &c2);
2869 (if (REAL_VALUE_ISINF (c2))
2871 /* sqrt(x) < y is always true, when y is a very large
2872 value and we don't care about NaNs or Infinities. */
2873 (if (! HONOR_NANS (@0) && ! HONOR_INFINITIES (@0))
2874 { constant_boolean_node (true, type); })
2875 /* sqrt(x) < y is x != +Inf when y is very large and we
2876 don't care about NaNs. */
2877 (if (! HONOR_NANS (@0))
2878 (ne @0 { build_real (TREE_TYPE (@0), c2); }))
2879 /* sqrt(x) < y is x >= 0 when y is very large and we
2880 don't care about Infinities. */
2881 (if (! HONOR_INFINITIES (@0))
2882 (ge @0 { build_real (TREE_TYPE (@0), dconst0); }))
2883 /* sqrt(x) < y is x >= 0 && x != +Inf, when y is large. */
2886 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
2887 (ne @0 { build_real (TREE_TYPE (@0), c2); }))))
2888 /* sqrt(x) < c is the same as x < c*c, if we ignore NaNs. */
2889 (if (! HONOR_NANS (@0))
2890 (cmp @0 { build_real (TREE_TYPE (@0), c2); })
2891 /* sqrt(x) < c is the same as x >= 0 && x < c*c. */
2894 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
2895 (cmp @0 { build_real (TREE_TYPE (@0), c2); })))))))))
2896 /* Transform sqrt(x) cmp sqrt(y) -> x cmp y. */
2898 (cmp (sq @0) (sq @1))
2899 (if (! HONOR_NANS (@0))
2902 /* Optimize various special cases of (FTYPE) N CMP CST. */
2903 (for cmp (lt le eq ne ge gt)
2904 icmp (le le eq ne ge ge)
2906 (cmp (float @0) REAL_CST@1)
2907 (if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (@1))
2908 && ! DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1)))
2911 tree itype = TREE_TYPE (@0);
2912 signop isign = TYPE_SIGN (itype);
2913 format_helper fmt (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (@1))));
2914 const REAL_VALUE_TYPE *cst = TREE_REAL_CST_PTR (@1);
2915 /* Be careful to preserve any potential exceptions due to
2916 NaNs. qNaNs are ok in == or != context.
2917 TODO: relax under -fno-trapping-math or
2918 -fno-signaling-nans. */
2920 = real_isnan (cst) && (cst->signalling
2921 || (cmp != EQ_EXPR || cmp != NE_EXPR));
2922 /* INT?_MIN is power-of-two so it takes
2923 only one mantissa bit. */
2924 bool signed_p = isign == SIGNED;
2925 bool itype_fits_ftype_p
2926 = TYPE_PRECISION (itype) - signed_p <= significand_size (fmt);
2928 /* TODO: allow non-fitting itype and SNaNs when
2929 -fno-trapping-math. */
2930 (if (itype_fits_ftype_p && ! exception_p)
2933 REAL_VALUE_TYPE imin, imax;
2934 real_from_integer (&imin, fmt, wi::min_value (itype), isign);
2935 real_from_integer (&imax, fmt, wi::max_value (itype), isign);
2937 REAL_VALUE_TYPE icst;
2938 if (cmp == GT_EXPR || cmp == GE_EXPR)
2939 real_ceil (&icst, fmt, cst);
2940 else if (cmp == LT_EXPR || cmp == LE_EXPR)
2941 real_floor (&icst, fmt, cst);
2943 real_trunc (&icst, fmt, cst);
2945 bool cst_int_p = real_identical (&icst, cst);
2947 bool overflow_p = false;
2949 = real_to_integer (&icst, &overflow_p, TYPE_PRECISION (itype));
2952 /* Optimize cases when CST is outside of ITYPE's range. */
2953 (if (real_compare (LT_EXPR, cst, &imin))
2954 { constant_boolean_node (cmp == GT_EXPR || cmp == GE_EXPR || cmp == NE_EXPR,
2956 (if (real_compare (GT_EXPR, cst, &imax))
2957 { constant_boolean_node (cmp == LT_EXPR || cmp == LE_EXPR || cmp == NE_EXPR,
2959 /* Remove cast if CST is an integer representable by ITYPE. */
2961 (cmp @0 { gcc_assert (!overflow_p);
2962 wide_int_to_tree (itype, icst_val); })
2964 /* When CST is fractional, optimize
2965 (FTYPE) N == CST -> 0
2966 (FTYPE) N != CST -> 1. */
2967 (if (cmp == EQ_EXPR || cmp == NE_EXPR)
2968 { constant_boolean_node (cmp == NE_EXPR, type); })
2969 /* Otherwise replace with sensible integer constant. */
2972 gcc_checking_assert (!overflow_p);
2974 (icmp @0 { wide_int_to_tree (itype, icst_val); })))))))))
2976 /* Fold A /[ex] B CMP C to A CMP B * C. */
2979 (cmp (exact_div @0 @1) INTEGER_CST@2)
2980 (if (!integer_zerop (@1))
2981 (if (wi::eq_p (@2, 0))
2983 (if (TREE_CODE (@1) == INTEGER_CST)
2987 wide_int prod = wi::mul (@2, @1, TYPE_SIGN (TREE_TYPE (@1)), &ovf);
2990 { constant_boolean_node (cmp == NE_EXPR, type); }
2991 (cmp @0 { wide_int_to_tree (TREE_TYPE (@0), prod); }))))))))
2992 (for cmp (lt le gt ge)
2994 (cmp (exact_div @0 INTEGER_CST@1) INTEGER_CST@2)
2995 (if (wi::gt_p (@1, 0, TYPE_SIGN (TREE_TYPE (@1))))
2999 wide_int prod = wi::mul (@2, @1, TYPE_SIGN (TREE_TYPE (@1)), &ovf);
3002 { constant_boolean_node (wi::lt_p (@2, 0, TYPE_SIGN (TREE_TYPE (@2)))
3003 != (cmp == LT_EXPR || cmp == LE_EXPR), type); }
3004 (cmp @0 { wide_int_to_tree (TREE_TYPE (@0), prod); }))))))
3006 /* Unordered tests if either argument is a NaN. */
3008 (bit_ior (unordered @0 @0) (unordered @1 @1))
3009 (if (types_match (@0, @1))
3012 (bit_and (ordered @0 @0) (ordered @1 @1))
3013 (if (types_match (@0, @1))
3016 (bit_ior:c (unordered @0 @0) (unordered:c@2 @0 @1))
3019 (bit_and:c (ordered @0 @0) (ordered:c@2 @0 @1))
3022 /* Simple range test simplifications. */
3023 /* A < B || A >= B -> true. */
3024 (for test1 (lt le le le ne ge)
3025 test2 (ge gt ge ne eq ne)
3027 (bit_ior:c (test1 @0 @1) (test2 @0 @1))
3028 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
3029 || VECTOR_INTEGER_TYPE_P (TREE_TYPE (@0)))
3030 { constant_boolean_node (true, type); })))
3031 /* A < B && A >= B -> false. */
3032 (for test1 (lt lt lt le ne eq)
3033 test2 (ge gt eq gt eq gt)
3035 (bit_and:c (test1 @0 @1) (test2 @0 @1))
3036 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
3037 || VECTOR_INTEGER_TYPE_P (TREE_TYPE (@0)))
3038 { constant_boolean_node (false, type); })))
3040 /* A & (2**N - 1) <= 2**K - 1 -> A & (2**N - 2**K) == 0
3041 A & (2**N - 1) > 2**K - 1 -> A & (2**N - 2**K) != 0
3043 Note that comparisons
3044 A & (2**N - 1) < 2**K -> A & (2**N - 2**K) == 0
3045 A & (2**N - 1) >= 2**K -> A & (2**N - 2**K) != 0
3046 will be canonicalized to above so there's no need to
3053 (cmp (bit_and@0 @1 INTEGER_CST@2) INTEGER_CST@3)
3054 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)))
3057 tree ty = TREE_TYPE (@0);
3058 unsigned prec = TYPE_PRECISION (ty);
3059 wide_int mask = wi::to_wide (@2, prec);
3060 wide_int rhs = wi::to_wide (@3, prec);
3061 signop sgn = TYPE_SIGN (ty);
3063 (if ((mask & (mask + 1)) == 0 && wi::gt_p (rhs, 0, sgn)
3064 && (rhs & (rhs + 1)) == 0 && wi::ge_p (mask, rhs, sgn))
3065 (eqcmp (bit_and @1 { wide_int_to_tree (ty, mask - rhs); })
3066 { build_zero_cst (ty); }))))))
3068 /* -A CMP -B -> B CMP A. */
3069 (for cmp (tcc_comparison)
3070 scmp (swapped_tcc_comparison)
3072 (cmp (negate @0) (negate @1))
3073 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
3074 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3075 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
3078 (cmp (negate @0) CONSTANT_CLASS_P@1)
3079 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
3080 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3081 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
3082 (with { tree tem = const_unop (NEGATE_EXPR, TREE_TYPE (@0), @1); }
3083 (if (tem && !TREE_OVERFLOW (tem))
3084 (scmp @0 { tem; }))))))
3086 /* Convert ABS_EXPR<x> == 0 or ABS_EXPR<x> != 0 to x == 0 or x != 0. */
3089 (op (abs @0) zerop@1)
3092 /* From fold_sign_changed_comparison and fold_widened_comparison. */
3093 (for cmp (simple_comparison)
3095 (cmp (convert@0 @00) (convert?@1 @10))
3096 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
3097 /* Disable this optimization if we're casting a function pointer
3098 type on targets that require function pointer canonicalization. */
3099 && !(targetm.have_canonicalize_funcptr_for_compare ()
3100 && TREE_CODE (TREE_TYPE (@00)) == POINTER_TYPE
3101 && TREE_CODE (TREE_TYPE (TREE_TYPE (@00))) == FUNCTION_TYPE)
3103 (if (TYPE_PRECISION (TREE_TYPE (@00)) == TYPE_PRECISION (TREE_TYPE (@0))
3104 && (TREE_CODE (@10) == INTEGER_CST
3105 || (@1 != @10 && types_match (TREE_TYPE (@10), TREE_TYPE (@00))))
3106 && (TYPE_UNSIGNED (TREE_TYPE (@00)) == TYPE_UNSIGNED (TREE_TYPE (@0))
3109 && (POINTER_TYPE_P (TREE_TYPE (@00)) == POINTER_TYPE_P (TREE_TYPE (@0))))
3110 /* ??? The special-casing of INTEGER_CST conversion was in the original
3111 code and here to avoid a spurious overflow flag on the resulting
3112 constant which fold_convert produces. */
3113 (if (TREE_CODE (@1) == INTEGER_CST)
3114 (cmp @00 { force_fit_type (TREE_TYPE (@00), wi::to_widest (@1), 0,
3115 TREE_OVERFLOW (@1)); })
3116 (cmp @00 (convert @1)))
3118 (if (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (TREE_TYPE (@00)))
3119 /* If possible, express the comparison in the shorter mode. */
3120 (if ((cmp == EQ_EXPR || cmp == NE_EXPR
3121 || TYPE_UNSIGNED (TREE_TYPE (@0)) == TYPE_UNSIGNED (TREE_TYPE (@00))
3122 || (!TYPE_UNSIGNED (TREE_TYPE (@0))
3123 && TYPE_UNSIGNED (TREE_TYPE (@00))))
3124 && (types_match (TREE_TYPE (@10), TREE_TYPE (@00))
3125 || ((TYPE_PRECISION (TREE_TYPE (@00))
3126 >= TYPE_PRECISION (TREE_TYPE (@10)))
3127 && (TYPE_UNSIGNED (TREE_TYPE (@00))
3128 == TYPE_UNSIGNED (TREE_TYPE (@10))))
3129 || (TREE_CODE (@10) == INTEGER_CST
3130 && INTEGRAL_TYPE_P (TREE_TYPE (@00))
3131 && int_fits_type_p (@10, TREE_TYPE (@00)))))
3132 (cmp @00 (convert @10))
3133 (if (TREE_CODE (@10) == INTEGER_CST
3134 && INTEGRAL_TYPE_P (TREE_TYPE (@00))
3135 && !int_fits_type_p (@10, TREE_TYPE (@00)))
3138 tree min = lower_bound_in_type (TREE_TYPE (@10), TREE_TYPE (@00));
3139 tree max = upper_bound_in_type (TREE_TYPE (@10), TREE_TYPE (@00));
3140 bool above = integer_nonzerop (const_binop (LT_EXPR, type, max, @10));
3141 bool below = integer_nonzerop (const_binop (LT_EXPR, type, @10, min));
3143 (if (above || below)
3144 (if (cmp == EQ_EXPR || cmp == NE_EXPR)
3145 { constant_boolean_node (cmp == EQ_EXPR ? false : true, type); }
3146 (if (cmp == LT_EXPR || cmp == LE_EXPR)
3147 { constant_boolean_node (above ? true : false, type); }
3148 (if (cmp == GT_EXPR || cmp == GE_EXPR)
3149 { constant_boolean_node (above ? false : true, type); }))))))))))))
3152 /* A local variable can never be pointed to by
3153 the default SSA name of an incoming parameter.
3154 SSA names are canonicalized to 2nd place. */
3156 (cmp addr@0 SSA_NAME@1)
3157 (if (SSA_NAME_IS_DEFAULT_DEF (@1)
3158 && TREE_CODE (SSA_NAME_VAR (@1)) == PARM_DECL)
3159 (with { tree base = get_base_address (TREE_OPERAND (@0, 0)); }
3160 (if (TREE_CODE (base) == VAR_DECL
3161 && auto_var_in_fn_p (base, current_function_decl))
3162 (if (cmp == NE_EXPR)
3163 { constant_boolean_node (true, type); }
3164 { constant_boolean_node (false, type); }))))))
3166 /* Equality compare simplifications from fold_binary */
3169 /* If we have (A | C) == D where C & ~D != 0, convert this into 0.
3170 Similarly for NE_EXPR. */
3172 (cmp (convert?@3 (bit_ior @0 INTEGER_CST@1)) INTEGER_CST@2)
3173 (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0))
3174 && wi::bit_and_not (@1, @2) != 0)
3175 { constant_boolean_node (cmp == NE_EXPR, type); }))
3177 /* (X ^ Y) == 0 becomes X == Y, and (X ^ Y) != 0 becomes X != Y. */
3179 (cmp (bit_xor @0 @1) integer_zerop)
3182 /* (X ^ Y) == Y becomes X == 0.
3183 Likewise (X ^ Y) == X becomes Y == 0. */
3185 (cmp:c (bit_xor:c @0 @1) @0)
3186 (cmp @1 { build_zero_cst (TREE_TYPE (@1)); }))
3188 /* (X ^ C1) op C2 can be rewritten as X op (C1 ^ C2). */
3190 (cmp (convert?@3 (bit_xor @0 INTEGER_CST@1)) INTEGER_CST@2)
3191 (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0)))
3192 (cmp @0 (bit_xor @1 (convert @2)))))
3195 (cmp (convert? addr@0) integer_zerop)
3196 (if (tree_single_nonzero_warnv_p (@0, NULL))
3197 { constant_boolean_node (cmp == NE_EXPR, type); })))
3199 /* If we have (A & C) == C where C is a power of 2, convert this into
3200 (A & C) != 0. Similarly for NE_EXPR. */
3204 (cmp (bit_and@2 @0 integer_pow2p@1) @1)
3205 (icmp @2 { build_zero_cst (TREE_TYPE (@0)); })))
3207 /* If we have (A & C) != 0 ? D : 0 where C and D are powers of 2,
3208 convert this into a shift followed by ANDing with D. */
3211 (ne (bit_and @0 integer_pow2p@1) integer_zerop)
3212 integer_pow2p@2 integer_zerop)
3214 int shift = wi::exact_log2 (@2) - wi::exact_log2 (@1);
3218 (lshift (convert @0) { build_int_cst (integer_type_node, shift); }) @2)
3220 (convert (rshift @0 { build_int_cst (integer_type_node, -shift); })) @2))))
3222 /* If we have (A & C) != 0 where C is the sign bit of A, convert
3223 this into A < 0. Similarly for (A & C) == 0 into A >= 0. */
3227 (cmp (bit_and (convert?@2 @0) integer_pow2p@1) integer_zerop)
3228 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
3229 && (TYPE_PRECISION (TREE_TYPE (@0))
3230 == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (@0))))
3231 && element_precision (@2) >= element_precision (@0)
3232 && wi::only_sign_bit_p (@1, element_precision (@0)))
3233 (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
3234 (ncmp (convert:stype @0) { build_zero_cst (stype); })))))
3236 /* If we have A < 0 ? C : 0 where C is a power of 2, convert
3237 this into a right shift or sign extension followed by ANDing with C. */
3240 (lt @0 integer_zerop)
3241 integer_pow2p@1 integer_zerop)
3242 (if (!TYPE_UNSIGNED (TREE_TYPE (@0)))
3244 int shift = element_precision (@0) - wi::exact_log2 (@1) - 1;
3248 (convert (rshift @0 { build_int_cst (integer_type_node, shift); }))
3250 /* Otherwise ctype must be wider than TREE_TYPE (@0) and pure
3251 sign extension followed by AND with C will achieve the effect. */
3252 (bit_and (convert @0) @1)))))
3254 /* When the addresses are not directly of decls compare base and offset.
3255 This implements some remaining parts of fold_comparison address
3256 comparisons but still no complete part of it. Still it is good
3257 enough to make fold_stmt not regress when not dispatching to fold_binary. */
3258 (for cmp (simple_comparison)
3260 (cmp (convert1?@2 addr@0) (convert2? addr@1))
3263 HOST_WIDE_INT off0, off1;
3264 tree base0 = get_addr_base_and_unit_offset (TREE_OPERAND (@0, 0), &off0);
3265 tree base1 = get_addr_base_and_unit_offset (TREE_OPERAND (@1, 0), &off1);
3266 if (base0 && TREE_CODE (base0) == MEM_REF)
3268 off0 += mem_ref_offset (base0).to_short_addr ();
3269 base0 = TREE_OPERAND (base0, 0);
3271 if (base1 && TREE_CODE (base1) == MEM_REF)
3273 off1 += mem_ref_offset (base1).to_short_addr ();
3274 base1 = TREE_OPERAND (base1, 0);
3277 (if (base0 && base1)
3281 /* Punt in GENERIC on variables with value expressions;
3282 the value expressions might point to fields/elements
3283 of other vars etc. */
3285 && ((VAR_P (base0) && DECL_HAS_VALUE_EXPR_P (base0))
3286 || (VAR_P (base1) && DECL_HAS_VALUE_EXPR_P (base1))))
3288 else if (decl_in_symtab_p (base0)
3289 && decl_in_symtab_p (base1))
3290 equal = symtab_node::get_create (base0)
3291 ->equal_address_to (symtab_node::get_create (base1));
3292 else if ((DECL_P (base0)
3293 || TREE_CODE (base0) == SSA_NAME
3294 || TREE_CODE (base0) == STRING_CST)
3296 || TREE_CODE (base1) == SSA_NAME
3297 || TREE_CODE (base1) == STRING_CST))
3298 equal = (base0 == base1);
3302 (if (cmp == EQ_EXPR)
3303 { constant_boolean_node (off0 == off1, type); })
3304 (if (cmp == NE_EXPR)
3305 { constant_boolean_node (off0 != off1, type); })
3306 (if (cmp == LT_EXPR)
3307 { constant_boolean_node (off0 < off1, type); })
3308 (if (cmp == LE_EXPR)
3309 { constant_boolean_node (off0 <= off1, type); })
3310 (if (cmp == GE_EXPR)
3311 { constant_boolean_node (off0 >= off1, type); })
3312 (if (cmp == GT_EXPR)
3313 { constant_boolean_node (off0 > off1, type); }))
3315 && DECL_P (base0) && DECL_P (base1)
3316 /* If we compare this as integers require equal offset. */
3317 && (!INTEGRAL_TYPE_P (TREE_TYPE (@2))
3320 (if (cmp == EQ_EXPR)
3321 { constant_boolean_node (false, type); })
3322 (if (cmp == NE_EXPR)
3323 { constant_boolean_node (true, type); })))))))))
3325 /* Simplify pointer equality compares using PTA. */
3329 (if (POINTER_TYPE_P (TREE_TYPE (@0))
3330 && ptrs_compare_unequal (@0, @1))
3331 { neeq == EQ_EXPR ? boolean_false_node : boolean_true_node; })))
3333 /* PR70920: Transform (intptr_t)x eq/ne CST to x eq/ne (typeof x) CST.
3334 and (typeof ptr_cst) x eq/ne ptr_cst to x eq/ne (typeof x) CST.
3335 Disable the transform if either operand is pointer to function.
3336 This broke pr22051-2.c for arm where function pointer
3337 canonicalizaion is not wanted. */
3341 (cmp (convert @0) INTEGER_CST@1)
3342 (if ((POINTER_TYPE_P (TREE_TYPE (@0)) && !FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@0)))
3343 && INTEGRAL_TYPE_P (TREE_TYPE (@1)))
3344 || (INTEGRAL_TYPE_P (TREE_TYPE (@0)) && POINTER_TYPE_P (TREE_TYPE (@1))
3345 && !FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@1)))))
3346 (cmp @0 (convert @1)))))
3348 /* Non-equality compare simplifications from fold_binary */
3349 (for cmp (lt gt le ge)
3350 /* Comparisons with the highest or lowest possible integer of
3351 the specified precision will have known values. */
3353 (cmp (convert?@2 @0) INTEGER_CST@1)
3354 (if ((INTEGRAL_TYPE_P (TREE_TYPE (@1)) || POINTER_TYPE_P (TREE_TYPE (@1)))
3355 && tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@0)))
3358 tree arg1_type = TREE_TYPE (@1);
3359 unsigned int prec = TYPE_PRECISION (arg1_type);
3360 wide_int max = wi::max_value (arg1_type);
3361 wide_int signed_max = wi::max_value (prec, SIGNED);
3362 wide_int min = wi::min_value (arg1_type);
3365 (if (wi::eq_p (@1, max))
3367 (if (cmp == GT_EXPR)
3368 { constant_boolean_node (false, type); })
3369 (if (cmp == GE_EXPR)
3371 (if (cmp == LE_EXPR)
3372 { constant_boolean_node (true, type); })
3373 (if (cmp == LT_EXPR)
3375 (if (wi::eq_p (@1, min))
3377 (if (cmp == LT_EXPR)
3378 { constant_boolean_node (false, type); })
3379 (if (cmp == LE_EXPR)
3381 (if (cmp == GE_EXPR)
3382 { constant_boolean_node (true, type); })
3383 (if (cmp == GT_EXPR)
3385 (if (wi::eq_p (@1, max - 1))
3387 (if (cmp == GT_EXPR)
3388 (eq @2 { wide_int_to_tree (TREE_TYPE (@1), wi::add (@1, 1)); }))
3389 (if (cmp == LE_EXPR)
3390 (ne @2 { wide_int_to_tree (TREE_TYPE (@1), wi::add (@1, 1)); }))))
3391 (if (wi::eq_p (@1, min + 1))
3393 (if (cmp == GE_EXPR)
3394 (ne @2 { wide_int_to_tree (TREE_TYPE (@1), wi::sub (@1, 1)); }))
3395 (if (cmp == LT_EXPR)
3396 (eq @2 { wide_int_to_tree (TREE_TYPE (@1), wi::sub (@1, 1)); }))))
3397 (if (wi::eq_p (@1, signed_max)
3398 && TYPE_UNSIGNED (arg1_type)
3399 /* We will flip the signedness of the comparison operator
3400 associated with the mode of @1, so the sign bit is
3401 specified by this mode. Check that @1 is the signed
3402 max associated with this sign bit. */
3403 && prec == GET_MODE_PRECISION (TYPE_MODE (arg1_type))
3404 /* signed_type does not work on pointer types. */
3405 && INTEGRAL_TYPE_P (arg1_type))
3406 /* The following case also applies to X < signed_max+1
3407 and X >= signed_max+1 because previous transformations. */
3408 (if (cmp == LE_EXPR || cmp == GT_EXPR)
3409 (with { tree st = signed_type_for (arg1_type); }
3410 (if (cmp == LE_EXPR)
3411 (ge (convert:st @0) { build_zero_cst (st); })
3412 (lt (convert:st @0) { build_zero_cst (st); }))))))))))
3414 (for cmp (unordered ordered unlt unle ungt unge uneq ltgt)
3415 /* If the second operand is NaN, the result is constant. */
3418 (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
3419 && (cmp != LTGT_EXPR || ! flag_trapping_math))
3420 { constant_boolean_node (cmp == ORDERED_EXPR || cmp == LTGT_EXPR
3421 ? false : true, type); })))
3423 /* bool_var != 0 becomes bool_var. */
3425 (ne @0 integer_zerop)
3426 (if (TREE_CODE (TREE_TYPE (@0)) == BOOLEAN_TYPE
3427 && types_match (type, TREE_TYPE (@0)))
3429 /* bool_var == 1 becomes bool_var. */
3431 (eq @0 integer_onep)
3432 (if (TREE_CODE (TREE_TYPE (@0)) == BOOLEAN_TYPE
3433 && types_match (type, TREE_TYPE (@0)))
3436 bool_var == 0 becomes !bool_var or
3437 bool_var != 1 becomes !bool_var
3438 here because that only is good in assignment context as long
3439 as we require a tcc_comparison in GIMPLE_CONDs where we'd
3440 replace if (x == 0) with tem = ~x; if (tem != 0) which is
3441 clearly less optimal and which we'll transform again in forwprop. */
3443 /* When one argument is a constant, overflow detection can be simplified.
3444 Currently restricted to single use so as not to interfere too much with
3445 ADD_OVERFLOW detection in tree-ssa-math-opts.c.
3446 A + CST CMP A -> A CMP' CST' */
3447 (for cmp (lt le ge gt)
3450 (cmp:c (plus@2 @0 INTEGER_CST@1) @0)
3451 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
3452 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))
3455 (out @0 { wide_int_to_tree (TREE_TYPE (@0), wi::max_value
3456 (TYPE_PRECISION (TREE_TYPE (@0)), UNSIGNED) - @1); }))))
3458 /* To detect overflow in unsigned A - B, A < B is simpler than A - B > A.
3459 However, the detection logic for SUB_OVERFLOW in tree-ssa-math-opts.c
3460 expects the long form, so we restrict the transformation for now. */
3463 (cmp:c (minus@2 @0 @1) @0)
3464 (if (single_use (@2)
3465 && ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3466 && TYPE_UNSIGNED (TREE_TYPE (@0))
3467 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
3470 /* Testing for overflow is unnecessary if we already know the result. */
3475 (cmp:c (realpart (IFN_SUB_OVERFLOW@2 @0 @1)) @0)
3476 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
3477 && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
3478 (out (imagpart @2) { build_zero_cst (TREE_TYPE (@0)); }))))
3483 (cmp:c (realpart (IFN_ADD_OVERFLOW:c@2 @0 @1)) @0)
3484 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
3485 && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
3486 (out (imagpart @2) { build_zero_cst (TREE_TYPE (@0)); }))))
3488 /* For unsigned operands, -1 / B < A checks whether A * B would overflow.
3489 Simplify it to __builtin_mul_overflow (A, B, <unused>). */
3493 (cmp:c (trunc_div:s integer_all_onesp @1) @0)
3494 (if (TYPE_UNSIGNED (TREE_TYPE (@0)) && !VECTOR_TYPE_P (TREE_TYPE (@0)))
3495 (with { tree t = TREE_TYPE (@0), cpx = build_complex_type (t); }
3496 (out (imagpart (IFN_MUL_OVERFLOW:cpx @0 @1)) { build_zero_cst (t); })))))
3498 /* Simplification of math builtins. These rules must all be optimizations
3499 as well as IL simplifications. If there is a possibility that the new
3500 form could be a pessimization, the rule should go in the canonicalization
3501 section that follows this one.
3503 Rules can generally go in this section if they satisfy one of
3506 - the rule describes an identity
3508 - the rule replaces calls with something as simple as addition or
3511 - the rule contains unary calls only and simplifies the surrounding
3512 arithmetic. (The idea here is to exclude non-unary calls in which
3513 one operand is constant and in which the call is known to be cheap
3514 when the operand has that value.) */
3516 (if (flag_unsafe_math_optimizations)
3517 /* Simplify sqrt(x) * sqrt(x) -> x. */
3519 (mult (SQRT@1 @0) @1)
3520 (if (!HONOR_SNANS (type))
3523 /* Simplify sqrt(x) * sqrt(y) -> sqrt(x*y). */
3524 (for root (SQRT CBRT)
3526 (mult (root:s @0) (root:s @1))
3527 (root (mult @0 @1))))
3529 /* Simplify expN(x) * expN(y) -> expN(x+y). */
3530 (for exps (EXP EXP2 EXP10 POW10)
3532 (mult (exps:s @0) (exps:s @1))
3533 (exps (plus @0 @1))))
3535 /* Simplify a/root(b/c) into a*root(c/b). */
3536 (for root (SQRT CBRT)
3538 (rdiv @0 (root:s (rdiv:s @1 @2)))
3539 (mult @0 (root (rdiv @2 @1)))))
3541 /* Simplify x/expN(y) into x*expN(-y). */
3542 (for exps (EXP EXP2 EXP10 POW10)
3544 (rdiv @0 (exps:s @1))
3545 (mult @0 (exps (negate @1)))))
3547 (for logs (LOG LOG2 LOG10 LOG10)
3548 exps (EXP EXP2 EXP10 POW10)
3549 /* logN(expN(x)) -> x. */
3553 /* expN(logN(x)) -> x. */
3558 /* Optimize logN(func()) for various exponential functions. We
3559 want to determine the value "x" and the power "exponent" in
3560 order to transform logN(x**exponent) into exponent*logN(x). */
3561 (for logs (LOG LOG LOG LOG2 LOG2 LOG2 LOG10 LOG10)
3562 exps (EXP2 EXP10 POW10 EXP EXP10 POW10 EXP EXP2)
3565 (if (SCALAR_FLOAT_TYPE_P (type))
3571 /* Prepare to do logN(exp(exponent)) -> exponent*logN(e). */
3572 x = build_real_truncate (type, dconst_e ());
3575 /* Prepare to do logN(exp2(exponent)) -> exponent*logN(2). */
3576 x = build_real (type, dconst2);
3580 /* Prepare to do logN(exp10(exponent)) -> exponent*logN(10). */
3582 REAL_VALUE_TYPE dconst10;
3583 real_from_integer (&dconst10, VOIDmode, 10, SIGNED);
3584 x = build_real (type, dconst10);
3591 (mult (logs { x; }) @0)))))
3599 (if (SCALAR_FLOAT_TYPE_P (type))
3605 /* Prepare to do logN(sqrt(x)) -> 0.5*logN(x). */
3606 x = build_real (type, dconsthalf);
3609 /* Prepare to do logN(cbrt(x)) -> (1/3)*logN(x). */
3610 x = build_real_truncate (type, dconst_third ());
3616 (mult { x; } (logs @0))))))
3618 /* logN(pow(x,exponent)) -> exponent*logN(x). */
3619 (for logs (LOG LOG2 LOG10)
3623 (mult @1 (logs @0))))
3628 exps (EXP EXP2 EXP10 POW10)
3629 /* sqrt(expN(x)) -> expN(x*0.5). */
3632 (exps (mult @0 { build_real (type, dconsthalf); })))
3633 /* cbrt(expN(x)) -> expN(x/3). */
3636 (exps (mult @0 { build_real_truncate (type, dconst_third ()); })))
3637 /* pow(expN(x), y) -> expN(x*y). */
3640 (exps (mult @0 @1))))
3642 /* tan(atan(x)) -> x. */
3649 /* cabs(x+0i) or cabs(0+xi) -> abs(x). */
3651 (CABS (complex:C @0 real_zerop@1))
3654 /* trunc(trunc(x)) -> trunc(x), etc. */
3655 (for fns (TRUNC FLOOR CEIL ROUND NEARBYINT RINT)
3659 /* f(x) -> x if x is integer valued and f does nothing for such values. */
3660 (for fns (TRUNC FLOOR CEIL ROUND NEARBYINT RINT)
3662 (fns integer_valued_real_p@0)
3665 /* hypot(x,0) and hypot(0,x) -> abs(x). */
3667 (HYPOT:c @0 real_zerop@1)
3670 /* pow(1,x) -> 1. */
3672 (POW real_onep@0 @1)
3676 /* copysign(x,x) -> x. */
3681 /* copysign(x,y) -> fabs(x) if y is nonnegative. */
3682 (COPYSIGN @0 tree_expr_nonnegative_p@1)
3685 (for scale (LDEXP SCALBN SCALBLN)
3686 /* ldexp(0, x) -> 0. */
3688 (scale real_zerop@0 @1)
3690 /* ldexp(x, 0) -> x. */
3692 (scale @0 integer_zerop@1)
3694 /* ldexp(x, y) -> x if x is +-Inf or NaN. */
3696 (scale REAL_CST@0 @1)
3697 (if (!real_isfinite (TREE_REAL_CST_PTR (@0)))
3700 /* Canonicalization of sequences of math builtins. These rules represent
3701 IL simplifications but are not necessarily optimizations.
3703 The sincos pass is responsible for picking "optimal" implementations
3704 of math builtins, which may be more complicated and can sometimes go
3705 the other way, e.g. converting pow into a sequence of sqrts.
3706 We only want to do these canonicalizations before the pass has run. */
3708 (if (flag_unsafe_math_optimizations && canonicalize_math_p ())
3709 /* Simplify tan(x) * cos(x) -> sin(x). */
3711 (mult:c (TAN:s @0) (COS:s @0))
3714 /* Simplify x * pow(x,c) -> pow(x,c+1). */
3716 (mult:c @0 (POW:s @0 REAL_CST@1))
3717 (if (!TREE_OVERFLOW (@1))
3718 (POW @0 (plus @1 { build_one_cst (type); }))))
3720 /* Simplify sin(x) / cos(x) -> tan(x). */
3722 (rdiv (SIN:s @0) (COS:s @0))
3725 /* Simplify cos(x) / sin(x) -> 1 / tan(x). */
3727 (rdiv (COS:s @0) (SIN:s @0))
3728 (rdiv { build_one_cst (type); } (TAN @0)))
3730 /* Simplify sin(x) / tan(x) -> cos(x). */
3732 (rdiv (SIN:s @0) (TAN:s @0))
3733 (if (! HONOR_NANS (@0)
3734 && ! HONOR_INFINITIES (@0))
3737 /* Simplify tan(x) / sin(x) -> 1.0 / cos(x). */
3739 (rdiv (TAN:s @0) (SIN:s @0))
3740 (if (! HONOR_NANS (@0)
3741 && ! HONOR_INFINITIES (@0))
3742 (rdiv { build_one_cst (type); } (COS @0))))
3744 /* Simplify pow(x,y) * pow(x,z) -> pow(x,y+z). */
3746 (mult (POW:s @0 @1) (POW:s @0 @2))
3747 (POW @0 (plus @1 @2)))
3749 /* Simplify pow(x,y) * pow(z,y) -> pow(x*z,y). */
3751 (mult (POW:s @0 @1) (POW:s @2 @1))
3752 (POW (mult @0 @2) @1))
3754 /* Simplify powi(x,y) * powi(z,y) -> powi(x*z,y). */
3756 (mult (POWI:s @0 @1) (POWI:s @2 @1))
3757 (POWI (mult @0 @2) @1))
3759 /* Simplify pow(x,c) / x -> pow(x,c-1). */
3761 (rdiv (POW:s @0 REAL_CST@1) @0)
3762 (if (!TREE_OVERFLOW (@1))
3763 (POW @0 (minus @1 { build_one_cst (type); }))))
3765 /* Simplify x / pow (y,z) -> x * pow(y,-z). */
3767 (rdiv @0 (POW:s @1 @2))
3768 (mult @0 (POW @1 (negate @2))))
3773 /* sqrt(sqrt(x)) -> pow(x,1/4). */
3776 (pows @0 { build_real (type, dconst_quarter ()); }))
3777 /* sqrt(cbrt(x)) -> pow(x,1/6). */
3780 (pows @0 { build_real_truncate (type, dconst_sixth ()); }))
3781 /* cbrt(sqrt(x)) -> pow(x,1/6). */
3784 (pows @0 { build_real_truncate (type, dconst_sixth ()); }))
3785 /* cbrt(cbrt(x)) -> pow(x,1/9), iff x is nonnegative. */
3787 (cbrts (cbrts tree_expr_nonnegative_p@0))
3788 (pows @0 { build_real_truncate (type, dconst_ninth ()); }))
3789 /* sqrt(pow(x,y)) -> pow(|x|,y*0.5). */
3791 (sqrts (pows @0 @1))
3792 (pows (abs @0) (mult @1 { build_real (type, dconsthalf); })))
3793 /* cbrt(pow(x,y)) -> pow(x,y/3), iff x is nonnegative. */
3795 (cbrts (pows tree_expr_nonnegative_p@0 @1))
3796 (pows @0 (mult @1 { build_real_truncate (type, dconst_third ()); })))
3797 /* pow(sqrt(x),y) -> pow(x,y*0.5). */
3799 (pows (sqrts @0) @1)
3800 (pows @0 (mult @1 { build_real (type, dconsthalf); })))
3801 /* pow(cbrt(x),y) -> pow(x,y/3) iff x is nonnegative. */
3803 (pows (cbrts tree_expr_nonnegative_p@0) @1)
3804 (pows @0 (mult @1 { build_real_truncate (type, dconst_third ()); })))
3805 /* pow(pow(x,y),z) -> pow(x,y*z) iff x is nonnegative. */
3807 (pows (pows tree_expr_nonnegative_p@0 @1) @2)
3808 (pows @0 (mult @1 @2))))
3810 /* cabs(x+xi) -> fabs(x)*sqrt(2). */
3812 (CABS (complex @0 @0))
3813 (mult (abs @0) { build_real_truncate (type, dconst_sqrt2 ()); }))
3815 /* hypot(x,x) -> fabs(x)*sqrt(2). */
3818 (mult (abs @0) { build_real_truncate (type, dconst_sqrt2 ()); }))
3820 /* cexp(x+yi) -> exp(x)*cexpi(y). */
3825 (cexps compositional_complex@0)
3826 (if (targetm.libc_has_function (function_c99_math_complex))
3828 (mult (exps@1 (realpart @0)) (realpart (cexpis:type@2 (imagpart @0))))
3829 (mult @1 (imagpart @2)))))))
3831 (if (canonicalize_math_p ())
3832 /* floor(x) -> trunc(x) if x is nonnegative. */
3836 (floors tree_expr_nonnegative_p@0)
3839 (match double_value_p
3841 (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == double_type_node)))
3842 (for froms (BUILT_IN_TRUNCL
3854 /* truncl(extend(x)) -> extend(trunc(x)), etc., if x is a double. */
3855 (if (optimize && canonicalize_math_p ())
3857 (froms (convert double_value_p@0))
3858 (convert (tos @0)))))
3860 (match float_value_p
3862 (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == float_type_node)))
3863 (for froms (BUILT_IN_TRUNCL BUILT_IN_TRUNC
3864 BUILT_IN_FLOORL BUILT_IN_FLOOR
3865 BUILT_IN_CEILL BUILT_IN_CEIL
3866 BUILT_IN_ROUNDL BUILT_IN_ROUND
3867 BUILT_IN_NEARBYINTL BUILT_IN_NEARBYINT
3868 BUILT_IN_RINTL BUILT_IN_RINT)
3869 tos (BUILT_IN_TRUNCF BUILT_IN_TRUNCF
3870 BUILT_IN_FLOORF BUILT_IN_FLOORF
3871 BUILT_IN_CEILF BUILT_IN_CEILF
3872 BUILT_IN_ROUNDF BUILT_IN_ROUNDF
3873 BUILT_IN_NEARBYINTF BUILT_IN_NEARBYINTF
3874 BUILT_IN_RINTF BUILT_IN_RINTF)
3875 /* truncl(extend(x)) and trunc(extend(x)) -> extend(truncf(x)), etc.,
3877 (if (optimize && canonicalize_math_p ()
3878 && targetm.libc_has_function (function_c99_misc))
3880 (froms (convert float_value_p@0))
3881 (convert (tos @0)))))
3883 (for froms (XFLOORL XCEILL XROUNDL XRINTL)
3884 tos (XFLOOR XCEIL XROUND XRINT)
3885 /* llfloorl(extend(x)) -> llfloor(x), etc., if x is a double. */
3886 (if (optimize && canonicalize_math_p ())
3888 (froms (convert double_value_p@0))
3891 (for froms (XFLOORL XCEILL XROUNDL XRINTL
3892 XFLOOR XCEIL XROUND XRINT)
3893 tos (XFLOORF XCEILF XROUNDF XRINTF)
3894 /* llfloorl(extend(x)) and llfloor(extend(x)) -> llfloorf(x), etc.,
3896 (if (optimize && canonicalize_math_p ())
3898 (froms (convert float_value_p@0))
3901 (if (canonicalize_math_p ())
3902 /* xfloor(x) -> fix_trunc(x) if x is nonnegative. */
3903 (for floors (IFLOOR LFLOOR LLFLOOR)
3905 (floors tree_expr_nonnegative_p@0)
3908 (if (canonicalize_math_p ())
3909 /* xfloor(x) -> fix_trunc(x), etc., if x is integer valued. */
3910 (for fns (IFLOOR LFLOOR LLFLOOR
3912 IROUND LROUND LLROUND)
3914 (fns integer_valued_real_p@0)
3916 (if (!flag_errno_math)
3917 /* xrint(x) -> fix_trunc(x), etc., if x is integer valued. */
3918 (for rints (IRINT LRINT LLRINT)
3920 (rints integer_valued_real_p@0)
3923 (if (canonicalize_math_p ())
3924 (for ifn (IFLOOR ICEIL IROUND IRINT)
3925 lfn (LFLOOR LCEIL LROUND LRINT)
3926 llfn (LLFLOOR LLCEIL LLROUND LLRINT)
3927 /* Canonicalize iround (x) to lround (x) on ILP32 targets where
3928 sizeof (int) == sizeof (long). */
3929 (if (TYPE_PRECISION (integer_type_node)
3930 == TYPE_PRECISION (long_integer_type_node))
3933 (lfn:long_integer_type_node @0)))
3934 /* Canonicalize llround (x) to lround (x) on LP64 targets where
3935 sizeof (long long) == sizeof (long). */
3936 (if (TYPE_PRECISION (long_long_integer_type_node)
3937 == TYPE_PRECISION (long_integer_type_node))
3940 (lfn:long_integer_type_node @0)))))
3942 /* cproj(x) -> x if we're ignoring infinities. */
3945 (if (!HONOR_INFINITIES (type))
3948 /* If the real part is inf and the imag part is known to be
3949 nonnegative, return (inf + 0i). */
3951 (CPROJ (complex REAL_CST@0 tree_expr_nonnegative_p@1))
3952 (if (real_isinf (TREE_REAL_CST_PTR (@0)))
3953 { build_complex_inf (type, false); }))
3955 /* If the imag part is inf, return (inf+I*copysign(0,imag)). */
3957 (CPROJ (complex @0 REAL_CST@1))
3958 (if (real_isinf (TREE_REAL_CST_PTR (@1)))
3959 { build_complex_inf (type, TREE_REAL_CST_PTR (@1)->sign); }))
3965 (pows @0 REAL_CST@1)
3967 const REAL_VALUE_TYPE *value = TREE_REAL_CST_PTR (@1);
3968 REAL_VALUE_TYPE tmp;
3971 /* pow(x,0) -> 1. */
3972 (if (real_equal (value, &dconst0))
3973 { build_real (type, dconst1); })
3974 /* pow(x,1) -> x. */
3975 (if (real_equal (value, &dconst1))
3977 /* pow(x,-1) -> 1/x. */
3978 (if (real_equal (value, &dconstm1))
3979 (rdiv { build_real (type, dconst1); } @0))
3980 /* pow(x,0.5) -> sqrt(x). */
3981 (if (flag_unsafe_math_optimizations
3982 && canonicalize_math_p ()
3983 && real_equal (value, &dconsthalf))
3985 /* pow(x,1/3) -> cbrt(x). */
3986 (if (flag_unsafe_math_optimizations
3987 && canonicalize_math_p ()
3988 && (tmp = real_value_truncate (TYPE_MODE (type), dconst_third ()),
3989 real_equal (value, &tmp)))
3992 /* powi(1,x) -> 1. */
3994 (POWI real_onep@0 @1)
3998 (POWI @0 INTEGER_CST@1)
4000 /* powi(x,0) -> 1. */
4001 (if (wi::eq_p (@1, 0))
4002 { build_real (type, dconst1); })
4003 /* powi(x,1) -> x. */
4004 (if (wi::eq_p (@1, 1))
4006 /* powi(x,-1) -> 1/x. */
4007 (if (wi::eq_p (@1, -1))
4008 (rdiv { build_real (type, dconst1); } @0))))
4010 /* Narrowing of arithmetic and logical operations.
4012 These are conceptually similar to the transformations performed for
4013 the C/C++ front-ends by shorten_binary_op and shorten_compare. Long
4014 term we want to move all that code out of the front-ends into here. */
4016 /* If we have a narrowing conversion of an arithmetic operation where
4017 both operands are widening conversions from the same type as the outer
4018 narrowing conversion. Then convert the innermost operands to a suitable
4019 unsigned type (to avoid introducing undefined behavior), perform the
4020 operation and convert the result to the desired type. */
4021 (for op (plus minus)
4023 (convert (op:s (convert@2 @0) (convert?@3 @1)))
4024 (if (INTEGRAL_TYPE_P (type)
4025 /* We check for type compatibility between @0 and @1 below,
4026 so there's no need to check that @1/@3 are integral types. */
4027 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
4028 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
4029 /* The precision of the type of each operand must match the
4030 precision of the mode of each operand, similarly for the
4032 && (TYPE_PRECISION (TREE_TYPE (@0))
4033 == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (@0))))
4034 && (TYPE_PRECISION (TREE_TYPE (@1))
4035 == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (@1))))
4036 && TYPE_PRECISION (type) == GET_MODE_PRECISION (TYPE_MODE (type))
4037 /* The inner conversion must be a widening conversion. */
4038 && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
4039 && types_match (@0, type)
4040 && (types_match (@0, @1)
4041 /* Or the second operand is const integer or converted const
4042 integer from valueize. */
4043 || TREE_CODE (@1) == INTEGER_CST))
4044 (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
4045 (op @0 (convert @1))
4046 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
4047 (convert (op (convert:utype @0)
4048 (convert:utype @1))))))))
4050 /* This is another case of narrowing, specifically when there's an outer
4051 BIT_AND_EXPR which masks off bits outside the type of the innermost
4052 operands. Like the previous case we have to convert the operands
4053 to unsigned types to avoid introducing undefined behavior for the
4054 arithmetic operation. */
4055 (for op (minus plus)
4057 (bit_and (op:s (convert@2 @0) (convert@3 @1)) INTEGER_CST@4)
4058 (if (INTEGRAL_TYPE_P (type)
4059 /* We check for type compatibility between @0 and @1 below,
4060 so there's no need to check that @1/@3 are integral types. */
4061 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
4062 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
4063 /* The precision of the type of each operand must match the
4064 precision of the mode of each operand, similarly for the
4066 && (TYPE_PRECISION (TREE_TYPE (@0))
4067 == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (@0))))
4068 && (TYPE_PRECISION (TREE_TYPE (@1))
4069 == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (@1))))
4070 && TYPE_PRECISION (type) == GET_MODE_PRECISION (TYPE_MODE (type))
4071 /* The inner conversion must be a widening conversion. */
4072 && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
4073 && types_match (@0, @1)
4074 && (tree_int_cst_min_precision (@4, TYPE_SIGN (TREE_TYPE (@0)))
4075 <= TYPE_PRECISION (TREE_TYPE (@0)))
4076 && (wi::bit_and (@4, wi::mask (TYPE_PRECISION (TREE_TYPE (@0)),
4077 true, TYPE_PRECISION (type))) == 0))
4078 (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
4079 (with { tree ntype = TREE_TYPE (@0); }
4080 (convert (bit_and (op @0 @1) (convert:ntype @4))))
4081 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
4082 (convert (bit_and (op (convert:utype @0) (convert:utype @1))
4083 (convert:utype @4))))))))
4085 /* Transform (@0 < @1 and @0 < @2) to use min,
4086 (@0 > @1 and @0 > @2) to use max */
4087 (for op (lt le gt ge)
4088 ext (min min max max)
4090 (bit_and (op:cs @0 @1) (op:cs @0 @2))
4091 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
4092 && TREE_CODE (@0) != INTEGER_CST)
4093 (op @0 (ext @1 @2)))))
4096 /* signbit(x) -> 0 if x is nonnegative. */
4097 (SIGNBIT tree_expr_nonnegative_p@0)
4098 { integer_zero_node; })
4101 /* signbit(x) -> x<0 if x doesn't have signed zeros. */
4103 (if (!HONOR_SIGNED_ZEROS (@0))
4104 (convert (lt @0 { build_real (TREE_TYPE (@0), dconst0); }))))
4106 /* Transform comparisons of the form X +- C1 CMP C2 to X CMP C2 -+ C1. */
4108 (for op (plus minus)
4111 (cmp (op@3 @0 INTEGER_CST@1) INTEGER_CST@2)
4112 (if (!TREE_OVERFLOW (@1) && !TREE_OVERFLOW (@2)
4113 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0))
4114 && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@0))
4115 && !TYPE_SATURATING (TREE_TYPE (@0)))
4116 (with { tree res = int_const_binop (rop, @2, @1); }
4117 (if (TREE_OVERFLOW (res)
4118 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
4119 { constant_boolean_node (cmp == NE_EXPR, type); }
4120 (if (single_use (@3))
4121 (cmp @0 { res; }))))))))
4122 (for cmp (lt le gt ge)
4123 (for op (plus minus)
4126 (cmp (op@3 @0 INTEGER_CST@1) INTEGER_CST@2)
4127 (if (!TREE_OVERFLOW (@1) && !TREE_OVERFLOW (@2)
4128 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
4129 (with { tree res = int_const_binop (rop, @2, @1); }
4130 (if (TREE_OVERFLOW (res))
4132 fold_overflow_warning (("assuming signed overflow does not occur "
4133 "when simplifying conditional to constant"),
4134 WARN_STRICT_OVERFLOW_CONDITIONAL);
4135 bool less = cmp == LE_EXPR || cmp == LT_EXPR;
4136 /* wi::ges_p (@2, 0) should be sufficient for a signed type. */
4137 bool ovf_high = wi::lt_p (@1, 0, TYPE_SIGN (TREE_TYPE (@1)))
4138 != (op == MINUS_EXPR);
4139 constant_boolean_node (less == ovf_high, type);
4141 (if (single_use (@3))
4144 fold_overflow_warning (("assuming signed overflow does not occur "
4145 "when changing X +- C1 cmp C2 to "
4147 WARN_STRICT_OVERFLOW_COMPARISON);
4149 (cmp @0 { res; })))))))))
4151 /* Canonicalizations of BIT_FIELD_REFs. */
4154 (BIT_FIELD_REF @0 @1 @2)
4156 (if (TREE_CODE (TREE_TYPE (@0)) == COMPLEX_TYPE
4157 && tree_int_cst_equal (@1, TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))))
4159 (if (integer_zerop (@2))
4160 (view_convert (realpart @0)))
4161 (if (tree_int_cst_equal (@2, TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))))
4162 (view_convert (imagpart @0)))))
4163 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
4164 && INTEGRAL_TYPE_P (type)
4165 /* On GIMPLE this should only apply to register arguments. */
4166 && (! GIMPLE || is_gimple_reg (@0))
4167 /* A bit-field-ref that referenced the full argument can be stripped. */
4168 && ((compare_tree_int (@1, TYPE_PRECISION (TREE_TYPE (@0))) == 0
4169 && integer_zerop (@2))
4170 /* Low-parts can be reduced to integral conversions.
4171 ??? The following doesn't work for PDP endian. */
4172 || (BYTES_BIG_ENDIAN == WORDS_BIG_ENDIAN
4173 /* Don't even think about BITS_BIG_ENDIAN. */
4174 && TYPE_PRECISION (TREE_TYPE (@0)) % BITS_PER_UNIT == 0
4175 && TYPE_PRECISION (type) % BITS_PER_UNIT == 0
4176 && compare_tree_int (@2, (BYTES_BIG_ENDIAN
4177 ? (TYPE_PRECISION (TREE_TYPE (@0))
4178 - TYPE_PRECISION (type))
4182 /* Simplify vector extracts. */
4185 (BIT_FIELD_REF CONSTRUCTOR@0 @1 @2)
4186 (if (VECTOR_TYPE_P (TREE_TYPE (@0))
4187 && (types_match (type, TREE_TYPE (TREE_TYPE (@0)))
4188 || (VECTOR_TYPE_P (type)
4189 && types_match (TREE_TYPE (type), TREE_TYPE (TREE_TYPE (@0))))))
4192 tree ctor = (TREE_CODE (@0) == SSA_NAME
4193 ? gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0)) : @0);
4194 tree eltype = TREE_TYPE (TREE_TYPE (ctor));
4195 unsigned HOST_WIDE_INT width = tree_to_uhwi (TYPE_SIZE (eltype));
4196 unsigned HOST_WIDE_INT n = tree_to_uhwi (@1);
4197 unsigned HOST_WIDE_INT idx = tree_to_uhwi (@2);
4200 && (idx % width) == 0
4202 && ((idx + n) / width) <= TYPE_VECTOR_SUBPARTS (TREE_TYPE (ctor)))
4207 /* Constructor elements can be subvectors. */
4208 unsigned HOST_WIDE_INT k = 1;
4209 if (CONSTRUCTOR_NELTS (ctor) != 0)
4211 tree cons_elem = TREE_TYPE (CONSTRUCTOR_ELT (ctor, 0)->value);
4212 if (TREE_CODE (cons_elem) == VECTOR_TYPE)
4213 k = TYPE_VECTOR_SUBPARTS (cons_elem);
4217 /* We keep an exact subset of the constructor elements. */
4218 (if ((idx % k) == 0 && (n % k) == 0)
4219 (if (CONSTRUCTOR_NELTS (ctor) == 0)
4220 { build_constructor (type, NULL); }
4227 (if (idx < CONSTRUCTOR_NELTS (ctor))
4228 { CONSTRUCTOR_ELT (ctor, idx)->value; }
4229 { build_zero_cst (type); })
4231 vec<constructor_elt, va_gc> *vals;
4232 vec_alloc (vals, n);
4233 for (unsigned i = 0;
4234 i < n && idx + i < CONSTRUCTOR_NELTS (ctor); ++i)
4235 CONSTRUCTOR_APPEND_ELT (vals, NULL_TREE,
4236 CONSTRUCTOR_ELT (ctor, idx + i)->value);
4237 build_constructor (type, vals);
4239 /* The bitfield references a single constructor element. */
4240 (if (idx + n <= (idx / k + 1) * k)
4242 (if (CONSTRUCTOR_NELTS (ctor) <= idx / k)
4243 { build_zero_cst (type); })
4245 { CONSTRUCTOR_ELT (ctor, idx / k)->value; })
4246 (BIT_FIELD_REF { CONSTRUCTOR_ELT (ctor, idx / k)->value; }
4247 @1 { bitsize_int ((idx % k) * width); })))))))))
4249 /* Simplify a bit extraction from a bit insertion for the cases with
4250 the inserted element fully covering the extraction or the insertion
4251 not touching the extraction. */
4253 (BIT_FIELD_REF (bit_insert @0 @1 @ipos) @rsize @rpos)
4256 unsigned HOST_WIDE_INT isize;
4257 if (INTEGRAL_TYPE_P (TREE_TYPE (@1)))
4258 isize = TYPE_PRECISION (TREE_TYPE (@1));
4260 isize = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (@1)));
4263 (if (wi::leu_p (@ipos, @rpos)
4264 && wi::leu_p (wi::add (@rpos, @rsize), wi::add (@ipos, isize)))
4265 (BIT_FIELD_REF @1 @rsize { wide_int_to_tree (bitsizetype,
4266 wi::sub (@rpos, @ipos)); }))
4267 (if (wi::geu_p (@ipos, wi::add (@rpos, @rsize))
4268 || wi::geu_p (@rpos, wi::add (@ipos, isize)))
4269 (BIT_FIELD_REF @0 @rsize @rpos)))))