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 && tree_nop_conversion_p (type, TREE_TYPE (@0))
362 && wi::add (@2, @1) == 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 (for bitop (bit_and bit_ior)
635 /* PR35691: Transform
636 (x == 0 & y == 0) -> (x | typeof(x)(y)) == 0.
637 (x != 0 | y != 0) -> (x | typeof(x)(y)) != 0. */
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 (x == -1 & y == -1) -> (x & typeof(x)(y)) == -1.
646 (x != -1 | y != -1) -> (x & typeof(x)(y)) != -1. */
648 (bitop (cmp @0 integer_all_onesp@2) (cmp @1 integer_all_onesp))
649 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
650 && INTEGRAL_TYPE_P (TREE_TYPE (@1))
651 && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
652 (cmp (bit_and @0 (convert @1)) @2))))
654 /* Fold (A & ~B) - (A & B) into (A ^ B) - B. */
656 (minus (bit_and:cs @0 (bit_not @1)) (bit_and:cs @0 @1))
657 (minus (bit_xor @0 @1) @1))
659 (minus (bit_and:s @0 INTEGER_CST@2) (bit_and:s @0 INTEGER_CST@1))
660 (if (wi::bit_not (@2) == @1)
661 (minus (bit_xor @0 @1) @1)))
663 /* Fold (A & B) - (A & ~B) into B - (A ^ B). */
665 (minus (bit_and:cs @0 @1) (bit_and:cs @0 (bit_not @1)))
666 (minus @1 (bit_xor @0 @1)))
668 /* Simplify (X & ~Y) |^+ (~X & Y) -> X ^ Y. */
669 (for op (bit_ior bit_xor plus)
671 (op (bit_and:c @0 (bit_not @1)) (bit_and:c (bit_not @0) @1))
674 (op:c (bit_and @0 INTEGER_CST@2) (bit_and (bit_not @0) INTEGER_CST@1))
675 (if (wi::bit_not (@2) == @1)
678 /* PR53979: Transform ((a ^ b) | a) -> (a | b) */
680 (bit_ior:c (bit_xor:c @0 @1) @0)
683 /* Simplify (~X & Y) to X ^ Y if we know that (X & ~Y) is 0. */
686 (bit_and (bit_not SSA_NAME@0) INTEGER_CST@1)
687 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
688 && (get_nonzero_bits (@0) & wi::bit_not (@1)) == 0)
692 /* X % Y is smaller than Y. */
695 (cmp (trunc_mod @0 @1) @1)
696 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
697 { constant_boolean_node (cmp == LT_EXPR, type); })))
700 (cmp @1 (trunc_mod @0 @1))
701 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
702 { constant_boolean_node (cmp == GT_EXPR, type); })))
706 (bit_ior @0 integer_all_onesp@1)
711 (bit_ior @0 integer_zerop)
716 (bit_and @0 integer_zerop@1)
722 (for op (bit_ior bit_xor plus)
724 (op:c (convert? @0) (convert? (bit_not @0)))
725 (convert { build_all_ones_cst (TREE_TYPE (@0)); })))
730 { build_zero_cst (type); })
732 /* Canonicalize X ^ ~0 to ~X. */
734 (bit_xor @0 integer_all_onesp@1)
739 (bit_and @0 integer_all_onesp)
742 /* x & x -> x, x | x -> x */
743 (for bitop (bit_and bit_ior)
748 /* x & C -> x if we know that x & ~C == 0. */
751 (bit_and SSA_NAME@0 INTEGER_CST@1)
752 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
753 && (get_nonzero_bits (@0) & wi::bit_not (@1)) == 0)
757 /* x + (x & 1) -> (x + 1) & ~1 */
759 (plus:c @0 (bit_and:s @0 integer_onep@1))
760 (bit_and (plus @0 @1) (bit_not @1)))
762 /* x & ~(x & y) -> x & ~y */
763 /* x | ~(x | y) -> x | ~y */
764 (for bitop (bit_and bit_ior)
766 (bitop:c @0 (bit_not (bitop:cs @0 @1)))
767 (bitop @0 (bit_not @1))))
769 /* (x | y) & ~x -> y & ~x */
770 /* (x & y) | ~x -> y | ~x */
771 (for bitop (bit_and bit_ior)
772 rbitop (bit_ior bit_and)
774 (bitop:c (rbitop:c @0 @1) (bit_not@2 @0))
777 /* (x & y) ^ (x | y) -> x ^ y */
779 (bit_xor:c (bit_and @0 @1) (bit_ior @0 @1))
782 /* (x ^ y) ^ (x | y) -> x & y */
784 (bit_xor:c (bit_xor @0 @1) (bit_ior @0 @1))
787 /* (x & y) + (x ^ y) -> x | y */
788 /* (x & y) | (x ^ y) -> x | y */
789 /* (x & y) ^ (x ^ y) -> x | y */
790 (for op (plus bit_ior bit_xor)
792 (op:c (bit_and @0 @1) (bit_xor @0 @1))
795 /* (x & y) + (x | y) -> x + y */
797 (plus:c (bit_and @0 @1) (bit_ior @0 @1))
800 /* (x + y) - (x | y) -> x & y */
802 (minus (plus @0 @1) (bit_ior @0 @1))
803 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
804 && !TYPE_SATURATING (type))
807 /* (x + y) - (x & y) -> x | y */
809 (minus (plus @0 @1) (bit_and @0 @1))
810 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
811 && !TYPE_SATURATING (type))
814 /* (x | y) - (x ^ y) -> x & y */
816 (minus (bit_ior @0 @1) (bit_xor @0 @1))
819 /* (x | y) - (x & y) -> x ^ y */
821 (minus (bit_ior @0 @1) (bit_and @0 @1))
824 /* (x | y) & ~(x & y) -> x ^ y */
826 (bit_and:c (bit_ior @0 @1) (bit_not (bit_and @0 @1)))
829 /* (x | y) & (~x ^ y) -> x & y */
831 (bit_and:c (bit_ior:c @0 @1) (bit_xor:c @1 (bit_not @0)))
834 /* ~x & ~y -> ~(x | y)
835 ~x | ~y -> ~(x & y) */
836 (for op (bit_and bit_ior)
837 rop (bit_ior bit_and)
839 (op (convert1? (bit_not @0)) (convert2? (bit_not @1)))
840 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
841 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
842 (bit_not (rop (convert @0) (convert @1))))))
844 /* If we are XORing or adding two BIT_AND_EXPR's, both of which are and'ing
845 with a constant, and the two constants have no bits in common,
846 we should treat this as a BIT_IOR_EXPR since this may produce more
848 (for op (bit_xor plus)
850 (op (convert1? (bit_and@4 @0 INTEGER_CST@1))
851 (convert2? (bit_and@5 @2 INTEGER_CST@3)))
852 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
853 && tree_nop_conversion_p (type, TREE_TYPE (@2))
854 && wi::bit_and (@1, @3) == 0)
855 (bit_ior (convert @4) (convert @5)))))
857 /* (X | Y) ^ X -> Y & ~ X*/
859 (bit_xor:c (convert1? (bit_ior:c @@0 @1)) (convert2? @0))
860 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
861 (convert (bit_and @1 (bit_not @0)))))
863 /* Convert ~X ^ ~Y to X ^ Y. */
865 (bit_xor (convert1? (bit_not @0)) (convert2? (bit_not @1)))
866 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
867 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
868 (bit_xor (convert @0) (convert @1))))
870 /* Convert ~X ^ C to X ^ ~C. */
872 (bit_xor (convert? (bit_not @0)) INTEGER_CST@1)
873 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
874 (bit_xor (convert @0) (bit_not @1))))
876 /* Fold (X & Y) ^ Y and (X ^ Y) & Y as ~X & Y. */
877 (for opo (bit_and bit_xor)
878 opi (bit_xor bit_and)
880 (opo:c (opi:c @0 @1) @1)
881 (bit_and (bit_not @0) @1)))
883 /* Given a bit-wise operation CODE applied to ARG0 and ARG1, see if both
884 operands are another bit-wise operation with a common input. If so,
885 distribute the bit operations to save an operation and possibly two if
886 constants are involved. For example, convert
887 (A | B) & (A | C) into A | (B & C)
888 Further simplification will occur if B and C are constants. */
889 (for op (bit_and bit_ior bit_xor)
890 rop (bit_ior bit_and bit_and)
892 (op (convert? (rop:c @@0 @1)) (convert? (rop:c @0 @2)))
893 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
894 && tree_nop_conversion_p (type, TREE_TYPE (@2)))
895 (rop (convert @0) (op (convert @1) (convert @2))))))
897 /* Some simple reassociation for bit operations, also handled in reassoc. */
898 /* (X & Y) & Y -> X & Y
899 (X | Y) | Y -> X | Y */
900 (for op (bit_and bit_ior)
902 (op:c (convert1?@2 (op:c @0 @@1)) (convert2? @1))
904 /* (X ^ Y) ^ Y -> X */
906 (bit_xor:c (convert1? (bit_xor:c @0 @@1)) (convert2? @1))
908 /* (X & Y) & (X & Z) -> (X & Y) & Z
909 (X | Y) | (X | Z) -> (X | Y) | Z */
910 (for op (bit_and bit_ior)
912 (op (convert1?@3 (op:c@4 @0 @1)) (convert2?@5 (op:c@6 @0 @2)))
913 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
914 && tree_nop_conversion_p (type, TREE_TYPE (@2)))
915 (if (single_use (@5) && single_use (@6))
917 (if (single_use (@3) && single_use (@4))
918 (op (convert @1) @5))))))
919 /* (X ^ Y) ^ (X ^ Z) -> Y ^ Z */
921 (bit_xor (convert1? (bit_xor:c @0 @1)) (convert2? (bit_xor:c @0 @2)))
922 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
923 && tree_nop_conversion_p (type, TREE_TYPE (@2)))
924 (bit_xor (convert @1) (convert @2))))
933 (abs tree_expr_nonnegative_p@0)
936 /* A few cases of fold-const.c negate_expr_p predicate. */
939 (if ((INTEGRAL_TYPE_P (type)
940 && TYPE_UNSIGNED (type))
941 || (!TYPE_OVERFLOW_SANITIZED (type)
942 && may_negate_without_overflow_p (t)))))
947 (if (!TYPE_OVERFLOW_SANITIZED (type))))
950 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (t)))))
951 /* VECTOR_CST handling of non-wrapping types would recurse in unsupported
955 (if (FLOAT_TYPE_P (TREE_TYPE (type)) || TYPE_OVERFLOW_WRAPS (type))))
957 /* (-A) * (-B) -> A * B */
959 (mult:c (convert1? (negate @0)) (convert2? negate_expr_p@1))
960 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
961 && tree_nop_conversion_p (type, TREE_TYPE (@1)))
962 (mult (convert @0) (convert (negate @1)))))
964 /* -(A + B) -> (-B) - A. */
966 (negate (plus:c @0 negate_expr_p@1))
967 (if (!HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type))
968 && !HONOR_SIGNED_ZEROS (element_mode (type)))
969 (minus (negate @1) @0)))
971 /* A - B -> A + (-B) if B is easily negatable. */
973 (minus @0 negate_expr_p@1)
974 (if (!FIXED_POINT_TYPE_P (type))
975 (plus @0 (negate @1))))
977 /* Try to fold (type) X op CST -> (type) (X op ((type-x) CST))
979 For bitwise binary operations apply operand conversions to the
980 binary operation result instead of to the operands. This allows
981 to combine successive conversions and bitwise binary operations.
982 We combine the above two cases by using a conditional convert. */
983 (for bitop (bit_and bit_ior bit_xor)
985 (bitop (convert @0) (convert? @1))
986 (if (((TREE_CODE (@1) == INTEGER_CST
987 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
988 && int_fits_type_p (@1, TREE_TYPE (@0)))
989 || types_match (@0, @1))
990 /* ??? This transform conflicts with fold-const.c doing
991 Convert (T)(x & c) into (T)x & (T)c, if c is an integer
992 constants (if x has signed type, the sign bit cannot be set
993 in c). This folds extension into the BIT_AND_EXPR.
994 Restrict it to GIMPLE to avoid endless recursions. */
995 && (bitop != BIT_AND_EXPR || GIMPLE)
996 && (/* That's a good idea if the conversion widens the operand, thus
997 after hoisting the conversion the operation will be narrower. */
998 TYPE_PRECISION (TREE_TYPE (@0)) < TYPE_PRECISION (type)
999 /* It's also a good idea if the conversion is to a non-integer
1001 || GET_MODE_CLASS (TYPE_MODE (type)) != MODE_INT
1002 /* Or if the precision of TO is not the same as the precision
1004 || !type_has_mode_precision_p (type)))
1005 (convert (bitop @0 (convert @1))))))
1007 (for bitop (bit_and bit_ior)
1008 rbitop (bit_ior bit_and)
1009 /* (x | y) & x -> x */
1010 /* (x & y) | x -> x */
1012 (bitop:c (rbitop:c @0 @1) @0)
1014 /* (~x | y) & x -> x & y */
1015 /* (~x & y) | x -> x | y */
1017 (bitop:c (rbitop:c (bit_not @0) @1) @0)
1020 /* (x | CST1) & CST2 -> (x & CST2) | (CST1 & CST2) */
1022 (bit_and (bit_ior @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
1023 (bit_ior (bit_and @0 @2) (bit_and @1 @2)))
1025 /* Combine successive equal operations with constants. */
1026 (for bitop (bit_and bit_ior bit_xor)
1028 (bitop (bitop @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
1029 (bitop @0 (bitop @1 @2))))
1031 /* Try simple folding for X op !X, and X op X with the help
1032 of the truth_valued_p and logical_inverted_value predicates. */
1033 (match truth_valued_p
1035 (if (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1)))
1036 (for op (tcc_comparison truth_and truth_andif truth_or truth_orif truth_xor)
1037 (match truth_valued_p
1039 (match truth_valued_p
1042 (match (logical_inverted_value @0)
1044 (match (logical_inverted_value @0)
1045 (bit_not truth_valued_p@0))
1046 (match (logical_inverted_value @0)
1047 (eq @0 integer_zerop))
1048 (match (logical_inverted_value @0)
1049 (ne truth_valued_p@0 integer_truep))
1050 (match (logical_inverted_value @0)
1051 (bit_xor truth_valued_p@0 integer_truep))
1055 (bit_and:c @0 (logical_inverted_value @0))
1056 { build_zero_cst (type); })
1057 /* X | !X and X ^ !X -> 1, , if X is truth-valued. */
1058 (for op (bit_ior bit_xor)
1060 (op:c truth_valued_p@0 (logical_inverted_value @0))
1061 { constant_boolean_node (true, type); }))
1062 /* X ==/!= !X is false/true. */
1065 (op:c truth_valued_p@0 (logical_inverted_value @0))
1066 { constant_boolean_node (op == NE_EXPR ? true : false, type); }))
1070 (bit_not (bit_not @0))
1073 /* Convert ~ (-A) to A - 1. */
1075 (bit_not (convert? (negate @0)))
1076 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1077 || !TYPE_UNSIGNED (TREE_TYPE (@0)))
1078 (convert (minus @0 { build_each_one_cst (TREE_TYPE (@0)); }))))
1080 /* Convert ~ (A - 1) or ~ (A + -1) to -A. */
1082 (bit_not (convert? (minus @0 integer_each_onep)))
1083 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1084 || !TYPE_UNSIGNED (TREE_TYPE (@0)))
1085 (convert (negate @0))))
1087 (bit_not (convert? (plus @0 integer_all_onesp)))
1088 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1089 || !TYPE_UNSIGNED (TREE_TYPE (@0)))
1090 (convert (negate @0))))
1092 /* Part of convert ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify. */
1094 (bit_not (convert? (bit_xor @0 INTEGER_CST@1)))
1095 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1096 (convert (bit_xor @0 (bit_not @1)))))
1098 (bit_not (convert? (bit_xor:c (bit_not @0) @1)))
1099 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1100 (convert (bit_xor @0 @1))))
1102 /* (x & ~m) | (y & m) -> ((x ^ y) & m) ^ x */
1104 (bit_ior:c (bit_and:cs @0 (bit_not @2)) (bit_and:cs @1 @2))
1105 (bit_xor (bit_and (bit_xor @0 @1) @2) @0))
1107 /* Fold A - (A & B) into ~B & A. */
1109 (minus (convert1? @0) (convert2?:s (bit_and:cs @@0 @1)))
1110 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
1111 && tree_nop_conversion_p (type, TREE_TYPE (@1)))
1112 (convert (bit_and (bit_not @1) @0))))
1114 /* (m1 CMP m2) * d -> (m1 CMP m2) ? d : 0 */
1115 (for cmp (gt lt ge le)
1117 (mult (convert (cmp @0 @1)) @2)
1118 (cond (cmp @0 @1) @2 { build_zero_cst (type); })))
1120 /* For integral types with undefined overflow and C != 0 fold
1121 x * C EQ/NE y * C into x EQ/NE y. */
1124 (cmp (mult:c @0 @1) (mult:c @2 @1))
1125 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
1126 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1127 && tree_expr_nonzero_p (@1))
1130 /* For integral types with wrapping overflow and C odd fold
1131 x * C EQ/NE y * C into x EQ/NE y. */
1134 (cmp (mult @0 INTEGER_CST@1) (mult @2 @1))
1135 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
1136 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))
1137 && (TREE_INT_CST_LOW (@1) & 1) != 0)
1140 /* For integral types with undefined overflow and C != 0 fold
1141 x * C RELOP y * C into:
1143 x RELOP y for nonnegative C
1144 y RELOP x for negative C */
1145 (for cmp (lt gt le ge)
1147 (cmp (mult:c @0 @1) (mult:c @2 @1))
1148 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
1149 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1150 (if (tree_expr_nonnegative_p (@1) && tree_expr_nonzero_p (@1))
1152 (if (TREE_CODE (@1) == INTEGER_CST
1153 && wi::neg_p (@1, TYPE_SIGN (TREE_TYPE (@1))))
1156 /* (X - 1U) <= INT_MAX-1U into (int) X > 0. */
1160 (cmp (plus @0 integer_minus_onep@1) INTEGER_CST@2)
1161 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1162 && TYPE_UNSIGNED (TREE_TYPE (@0))
1163 && TYPE_PRECISION (TREE_TYPE (@0)) > 1
1164 && wi::eq_p (@2, wi::max_value (TYPE_PRECISION (TREE_TYPE (@0)),
1166 (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
1167 (icmp (convert:stype @0) { build_int_cst (stype, 0); })))))
1169 /* X / 4 < Y / 4 iff X < Y when the division is known to be exact. */
1170 (for cmp (simple_comparison)
1172 (cmp (exact_div @0 INTEGER_CST@2) (exact_div @1 @2))
1173 (if (wi::gt_p(@2, 0, TYPE_SIGN (TREE_TYPE (@2))))
1176 /* X / C1 op C2 into a simple range test. */
1177 (for cmp (simple_comparison)
1179 (cmp (trunc_div:s @0 INTEGER_CST@1) INTEGER_CST@2)
1180 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1181 && integer_nonzerop (@1)
1182 && !TREE_OVERFLOW (@1)
1183 && !TREE_OVERFLOW (@2))
1184 (with { tree lo, hi; bool neg_overflow;
1185 enum tree_code code = fold_div_compare (cmp, @1, @2, &lo, &hi,
1188 (if (code == LT_EXPR || code == GE_EXPR)
1189 (if (TREE_OVERFLOW (lo))
1190 { build_int_cst (type, (code == LT_EXPR) ^ neg_overflow); }
1191 (if (code == LT_EXPR)
1194 (if (code == LE_EXPR || code == GT_EXPR)
1195 (if (TREE_OVERFLOW (hi))
1196 { build_int_cst (type, (code == LE_EXPR) ^ neg_overflow); }
1197 (if (code == LE_EXPR)
1201 { build_int_cst (type, code == NE_EXPR); })
1202 (if (code == EQ_EXPR && !hi)
1204 (if (code == EQ_EXPR && !lo)
1206 (if (code == NE_EXPR && !hi)
1208 (if (code == NE_EXPR && !lo)
1211 { build_range_check (UNKNOWN_LOCATION, type, @0, code == EQ_EXPR,
1215 tree etype = range_check_type (TREE_TYPE (@0));
1218 if (! TYPE_UNSIGNED (etype))
1219 etype = unsigned_type_for (etype);
1220 hi = fold_convert (etype, hi);
1221 lo = fold_convert (etype, lo);
1222 hi = const_binop (MINUS_EXPR, etype, hi, lo);
1225 (if (etype && hi && !TREE_OVERFLOW (hi))
1226 (if (code == EQ_EXPR)
1227 (le (minus (convert:etype @0) { lo; }) { hi; })
1228 (gt (minus (convert:etype @0) { lo; }) { hi; })))))))))
1230 /* X + Z < Y + Z is the same as X < Y when there is no overflow. */
1231 (for op (lt le ge gt)
1233 (op (plus:c @0 @2) (plus:c @1 @2))
1234 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1235 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1237 /* For equality and subtraction, this is also true with wrapping overflow. */
1238 (for op (eq ne minus)
1240 (op (plus:c @0 @2) (plus:c @1 @2))
1241 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1242 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1243 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
1246 /* X - Z < Y - Z is the same as X < Y when there is no overflow. */
1247 (for op (lt le ge gt)
1249 (op (minus @0 @2) (minus @1 @2))
1250 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1251 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1253 /* For equality and subtraction, this is also true with wrapping overflow. */
1254 (for op (eq ne minus)
1256 (op (minus @0 @2) (minus @1 @2))
1257 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1258 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1259 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
1262 /* Z - X < Z - Y is the same as Y < X when there is no overflow. */
1263 (for op (lt le ge gt)
1265 (op (minus @2 @0) (minus @2 @1))
1266 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1267 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1269 /* For equality and subtraction, this is also true with wrapping overflow. */
1270 (for op (eq ne minus)
1272 (op (minus @2 @0) (minus @2 @1))
1273 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1274 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1275 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
1279 * (X / Y) == 0 -> X < Y if X, Y are unsigned.
1280 * (X / Y) != 0 -> X >= Y, if X, Y are unsigned.
1285 (cmp (trunc_div @0 @1) integer_zerop)
1286 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
1287 && (VECTOR_TYPE_P (type) || !VECTOR_TYPE_P (TREE_TYPE (@0))))
1290 /* X == C - X can never be true if C is odd. */
1293 (cmp:c (convert? @0) (convert1? (minus INTEGER_CST@1 (convert2? @0))))
1294 (if (TREE_INT_CST_LOW (@1) & 1)
1295 { constant_boolean_node (cmp == NE_EXPR, type); })))
1297 /* Arguments on which one can call get_nonzero_bits to get the bits
1299 (match with_possible_nonzero_bits
1301 (match with_possible_nonzero_bits
1303 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))))
1304 /* Slightly extended version, do not make it recursive to keep it cheap. */
1305 (match (with_possible_nonzero_bits2 @0)
1306 with_possible_nonzero_bits@0)
1307 (match (with_possible_nonzero_bits2 @0)
1308 (bit_and:c with_possible_nonzero_bits@0 @2))
1310 /* Same for bits that are known to be set, but we do not have
1311 an equivalent to get_nonzero_bits yet. */
1312 (match (with_certain_nonzero_bits2 @0)
1314 (match (with_certain_nonzero_bits2 @0)
1315 (bit_ior @1 INTEGER_CST@0))
1317 /* X == C (or X & Z == Y | C) is impossible if ~nonzero(X) & C != 0. */
1320 (cmp:c (with_possible_nonzero_bits2 @0) (with_certain_nonzero_bits2 @1))
1321 (if ((~get_nonzero_bits (@0) & @1) != 0)
1322 { constant_boolean_node (cmp == NE_EXPR, type); })))
1324 /* ((X inner_op C0) outer_op C1)
1325 With X being a tree where value_range has reasoned certain bits to always be
1326 zero throughout its computed value range,
1327 inner_op = {|,^}, outer_op = {|,^} and inner_op != outer_op
1328 where zero_mask has 1's for all bits that are sure to be 0 in
1330 if (inner_op == '^') C0 &= ~C1;
1331 if ((C0 & ~zero_mask) == 0) then emit (X outer_op (C0 outer_op C1)
1332 if ((C1 & ~zero_mask) == 0) then emit (X inner_op (C0 outer_op C1)
1334 (for inner_op (bit_ior bit_xor)
1335 outer_op (bit_xor bit_ior)
1338 (inner_op:s @2 INTEGER_CST@0) INTEGER_CST@1)
1342 wide_int zero_mask_not;
1346 if (TREE_CODE (@2) == SSA_NAME)
1347 zero_mask_not = get_nonzero_bits (@2);
1351 if (inner_op == BIT_XOR_EXPR)
1353 C0 = wi::bit_and_not (@0, @1);
1354 cst_emit = wi::bit_or (C0, @1);
1359 cst_emit = wi::bit_xor (@0, @1);
1362 (if (!fail && wi::bit_and (C0, zero_mask_not) == 0)
1363 (outer_op @2 { wide_int_to_tree (type, cst_emit); })
1364 (if (!fail && wi::bit_and (@1, zero_mask_not) == 0)
1365 (inner_op @2 { wide_int_to_tree (type, cst_emit); }))))))
1367 /* Associate (p +p off1) +p off2 as (p +p (off1 + off2)). */
1369 (pointer_plus (pointer_plus:s @0 @1) @3)
1370 (pointer_plus @0 (plus @1 @3)))
1376 tem4 = (unsigned long) tem3;
1381 (pointer_plus @0 (convert?@2 (minus@3 (convert @1) (convert @0))))
1382 /* Conditionally look through a sign-changing conversion. */
1383 (if (TYPE_PRECISION (TREE_TYPE (@2)) == TYPE_PRECISION (TREE_TYPE (@3))
1384 && ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@1)))
1385 || (GENERIC && type == TREE_TYPE (@1))))
1389 tem = (sizetype) ptr;
1393 and produce the simpler and easier to analyze with respect to alignment
1394 ... = ptr & ~algn; */
1396 (pointer_plus @0 (negate (bit_and (convert @0) INTEGER_CST@1)))
1397 (with { tree algn = wide_int_to_tree (TREE_TYPE (@0), wi::bit_not (@1)); }
1398 (bit_and @0 { algn; })))
1400 /* Try folding difference of addresses. */
1402 (minus (convert ADDR_EXPR@0) (convert @1))
1403 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1404 (with { HOST_WIDE_INT diff; }
1405 (if (ptr_difference_const (@0, @1, &diff))
1406 { build_int_cst_type (type, diff); }))))
1408 (minus (convert @0) (convert ADDR_EXPR@1))
1409 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1410 (with { HOST_WIDE_INT diff; }
1411 (if (ptr_difference_const (@0, @1, &diff))
1412 { build_int_cst_type (type, diff); }))))
1414 /* If arg0 is derived from the address of an object or function, we may
1415 be able to fold this expression using the object or function's
1418 (bit_and (convert? @0) INTEGER_CST@1)
1419 (if (POINTER_TYPE_P (TREE_TYPE (@0))
1420 && tree_nop_conversion_p (type, TREE_TYPE (@0)))
1424 unsigned HOST_WIDE_INT bitpos;
1425 get_pointer_alignment_1 (@0, &align, &bitpos);
1427 (if (wi::ltu_p (@1, align / BITS_PER_UNIT))
1428 { wide_int_to_tree (type, wi::bit_and (@1, bitpos / BITS_PER_UNIT)); }))))
1431 /* We can't reassociate at all for saturating types. */
1432 (if (!TYPE_SATURATING (type))
1434 /* Contract negates. */
1435 /* A + (-B) -> A - B */
1437 (plus:c @0 (convert? (negate @1)))
1438 /* Apply STRIP_NOPS on the negate. */
1439 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1440 && !TYPE_OVERFLOW_SANITIZED (type))
1444 if (INTEGRAL_TYPE_P (type)
1445 && TYPE_OVERFLOW_WRAPS (type) != TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
1446 t1 = TYPE_OVERFLOW_WRAPS (type) ? type : TREE_TYPE (@1);
1448 (convert (minus (convert:t1 @0) (convert:t1 @1))))))
1449 /* A - (-B) -> A + B */
1451 (minus @0 (convert? (negate @1)))
1452 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1453 && !TYPE_OVERFLOW_SANITIZED (type))
1457 if (INTEGRAL_TYPE_P (type)
1458 && TYPE_OVERFLOW_WRAPS (type) != TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
1459 t1 = TYPE_OVERFLOW_WRAPS (type) ? type : TREE_TYPE (@1);
1461 (convert (plus (convert:t1 @0) (convert:t1 @1))))))
1464 (negate (convert? (negate @1)))
1465 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1466 && !TYPE_OVERFLOW_SANITIZED (type))
1469 /* We can't reassociate floating-point unless -fassociative-math
1470 or fixed-point plus or minus because of saturation to +-Inf. */
1471 (if ((!FLOAT_TYPE_P (type) || flag_associative_math)
1472 && !FIXED_POINT_TYPE_P (type))
1474 /* Match patterns that allow contracting a plus-minus pair
1475 irrespective of overflow issues. */
1476 /* (A +- B) - A -> +- B */
1477 /* (A +- B) -+ B -> A */
1478 /* A - (A +- B) -> -+ B */
1479 /* A +- (B -+ A) -> +- B */
1481 (minus (plus:c @0 @1) @0)
1484 (minus (minus @0 @1) @0)
1487 (plus:c (minus @0 @1) @1)
1490 (minus @0 (plus:c @0 @1))
1493 (minus @0 (minus @0 @1))
1495 /* (A +- B) + (C - A) -> C +- B */
1496 /* (A + B) - (A - C) -> B + C */
1497 /* More cases are handled with comparisons. */
1499 (plus:c (plus:c @0 @1) (minus @2 @0))
1502 (plus:c (minus @0 @1) (minus @2 @0))
1505 (minus (plus:c @0 @1) (minus @0 @2))
1508 /* (A +- CST1) +- CST2 -> A + CST3
1509 Use view_convert because it is safe for vectors and equivalent for
1511 (for outer_op (plus minus)
1512 (for inner_op (plus minus)
1513 neg_inner_op (minus plus)
1515 (outer_op (nop_convert (inner_op @0 CONSTANT_CLASS_P@1))
1517 /* If one of the types wraps, use that one. */
1518 (if (!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_WRAPS (type))
1519 (if (outer_op == PLUS_EXPR)
1520 (plus (view_convert @0) (inner_op @2 (view_convert @1)))
1521 (minus (view_convert @0) (neg_inner_op @2 (view_convert @1))))
1522 (if (!ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1523 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
1524 (if (outer_op == PLUS_EXPR)
1525 (view_convert (plus @0 (inner_op (view_convert @2) @1)))
1526 (view_convert (minus @0 (neg_inner_op (view_convert @2) @1))))
1527 /* If the constant operation overflows we cannot do the transform
1528 directly as we would introduce undefined overflow, for example
1529 with (a - 1) + INT_MIN. */
1530 (if (types_match (type, @0))
1531 (with { tree cst = const_binop (outer_op == inner_op
1532 ? PLUS_EXPR : MINUS_EXPR,
1534 (if (cst && !TREE_OVERFLOW (cst))
1535 (inner_op @0 { cst; } )
1536 /* X+INT_MAX+1 is X-INT_MIN. */
1537 (if (INTEGRAL_TYPE_P (type) && cst
1538 && wi::eq_p (cst, wi::min_value (type)))
1539 (neg_inner_op @0 { wide_int_to_tree (type, cst); })
1540 /* Last resort, use some unsigned type. */
1541 (with { tree utype = unsigned_type_for (type); }
1542 (view_convert (inner_op
1543 (view_convert:utype @0)
1545 { drop_tree_overflow (cst); })))))))))))))
1547 /* (CST1 - A) +- CST2 -> CST3 - A */
1548 (for outer_op (plus minus)
1550 (outer_op (minus CONSTANT_CLASS_P@1 @0) CONSTANT_CLASS_P@2)
1551 (with { tree cst = const_binop (outer_op, type, @1, @2); }
1552 (if (cst && !TREE_OVERFLOW (cst))
1553 (minus { cst; } @0)))))
1555 /* CST1 - (CST2 - A) -> CST3 + A */
1557 (minus CONSTANT_CLASS_P@1 (minus CONSTANT_CLASS_P@2 @0))
1558 (with { tree cst = const_binop (MINUS_EXPR, type, @1, @2); }
1559 (if (cst && !TREE_OVERFLOW (cst))
1560 (plus { cst; } @0))))
1564 (plus:c (bit_not @0) @0)
1565 (if (!TYPE_OVERFLOW_TRAPS (type))
1566 { build_all_ones_cst (type); }))
1570 (plus (convert? (bit_not @0)) integer_each_onep)
1571 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1572 (negate (convert @0))))
1576 (minus (convert? (negate @0)) integer_each_onep)
1577 (if (!TYPE_OVERFLOW_TRAPS (type)
1578 && tree_nop_conversion_p (type, TREE_TYPE (@0)))
1579 (bit_not (convert @0))))
1583 (minus integer_all_onesp @0)
1586 /* (T)(P + A) - (T)P -> (T) A */
1587 (for add (plus pointer_plus)
1589 (minus (convert (add @@0 @1))
1591 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
1592 /* For integer types, if A has a smaller type
1593 than T the result depends on the possible
1595 E.g. T=size_t, A=(unsigned)429497295, P>0.
1596 However, if an overflow in P + A would cause
1597 undefined behavior, we can assume that there
1599 || (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1600 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1601 /* For pointer types, if the conversion of A to the
1602 final type requires a sign- or zero-extension,
1603 then we have to punt - it is not defined which
1605 || (POINTER_TYPE_P (TREE_TYPE (@0))
1606 && TREE_CODE (@1) == INTEGER_CST
1607 && tree_int_cst_sign_bit (@1) == 0))
1610 /* (T)P - (T)(P + A) -> -(T) A */
1611 (for add (plus pointer_plus)
1614 (convert (add @@0 @1)))
1615 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
1616 /* For integer types, if A has a smaller type
1617 than T the result depends on the possible
1619 E.g. T=size_t, A=(unsigned)429497295, P>0.
1620 However, if an overflow in P + A would cause
1621 undefined behavior, we can assume that there
1623 || (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1624 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1625 /* For pointer types, if the conversion of A to the
1626 final type requires a sign- or zero-extension,
1627 then we have to punt - it is not defined which
1629 || (POINTER_TYPE_P (TREE_TYPE (@0))
1630 && TREE_CODE (@1) == INTEGER_CST
1631 && tree_int_cst_sign_bit (@1) == 0))
1632 (negate (convert @1)))))
1634 /* (T)(P + A) - (T)(P + B) -> (T)A - (T)B */
1635 (for add (plus pointer_plus)
1637 (minus (convert (add @@0 @1))
1638 (convert (add @0 @2)))
1639 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
1640 /* For integer types, if A has a smaller type
1641 than T the result depends on the possible
1643 E.g. T=size_t, A=(unsigned)429497295, P>0.
1644 However, if an overflow in P + A would cause
1645 undefined behavior, we can assume that there
1647 || (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1648 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1649 /* For pointer types, if the conversion of A to the
1650 final type requires a sign- or zero-extension,
1651 then we have to punt - it is not defined which
1653 || (POINTER_TYPE_P (TREE_TYPE (@0))
1654 && TREE_CODE (@1) == INTEGER_CST
1655 && tree_int_cst_sign_bit (@1) == 0
1656 && TREE_CODE (@2) == INTEGER_CST
1657 && tree_int_cst_sign_bit (@2) == 0))
1658 (minus (convert @1) (convert @2)))))))
1661 /* Simplifications of MIN_EXPR, MAX_EXPR, fmin() and fmax(). */
1663 (for minmax (min max FMIN FMAX)
1667 /* min(max(x,y),y) -> y. */
1669 (min:c (max:c @0 @1) @1)
1671 /* max(min(x,y),y) -> y. */
1673 (max:c (min:c @0 @1) @1)
1675 /* max(a,-a) -> abs(a). */
1677 (max:c @0 (negate @0))
1678 (if (TREE_CODE (type) != COMPLEX_TYPE
1679 && (! ANY_INTEGRAL_TYPE_P (type)
1680 || TYPE_OVERFLOW_UNDEFINED (type)))
1682 /* min(a,-a) -> -abs(a). */
1684 (min:c @0 (negate @0))
1685 (if (TREE_CODE (type) != COMPLEX_TYPE
1686 && (! ANY_INTEGRAL_TYPE_P (type)
1687 || TYPE_OVERFLOW_UNDEFINED (type)))
1692 (if (INTEGRAL_TYPE_P (type)
1693 && TYPE_MIN_VALUE (type)
1694 && operand_equal_p (@1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
1696 (if (INTEGRAL_TYPE_P (type)
1697 && TYPE_MAX_VALUE (type)
1698 && operand_equal_p (@1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
1703 (if (INTEGRAL_TYPE_P (type)
1704 && TYPE_MAX_VALUE (type)
1705 && operand_equal_p (@1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
1707 (if (INTEGRAL_TYPE_P (type)
1708 && TYPE_MIN_VALUE (type)
1709 && operand_equal_p (@1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
1712 /* max (a, a + CST) -> a + CST where CST is positive. */
1713 /* max (a, a + CST) -> a where CST is negative. */
1715 (max:c @0 (plus@2 @0 INTEGER_CST@1))
1716 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1717 (if (tree_int_cst_sgn (@1) > 0)
1721 /* min (a, a + CST) -> a where CST is positive. */
1722 /* min (a, a + CST) -> a + CST where CST is negative. */
1724 (min:c @0 (plus@2 @0 INTEGER_CST@1))
1725 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1726 (if (tree_int_cst_sgn (@1) > 0)
1730 /* (convert (minmax ((convert (x) c)))) -> minmax (x c) if x is promoted
1731 and the outer convert demotes the expression back to x's type. */
1732 (for minmax (min max)
1734 (convert (minmax@0 (convert @1) INTEGER_CST@2))
1735 (if (INTEGRAL_TYPE_P (type)
1736 && types_match (@1, type) && int_fits_type_p (@2, type)
1737 && TYPE_SIGN (TREE_TYPE (@0)) == TYPE_SIGN (type)
1738 && TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type))
1739 (minmax @1 (convert @2)))))
1741 (for minmax (FMIN FMAX)
1742 /* If either argument is NaN, return the other one. Avoid the
1743 transformation if we get (and honor) a signalling NaN. */
1745 (minmax:c @0 REAL_CST@1)
1746 (if (real_isnan (TREE_REAL_CST_PTR (@1))
1747 && (!HONOR_SNANS (@1) || !TREE_REAL_CST (@1).signalling))
1749 /* Convert fmin/fmax to MIN_EXPR/MAX_EXPR. C99 requires these
1750 functions to return the numeric arg if the other one is NaN.
1751 MIN and MAX don't honor that, so only transform if -ffinite-math-only
1752 is set. C99 doesn't require -0.0 to be handled, so we don't have to
1753 worry about it either. */
1754 (if (flag_finite_math_only)
1761 /* min (-A, -B) -> -max (A, B) */
1762 (for minmax (min max FMIN FMAX)
1763 maxmin (max min FMAX FMIN)
1765 (minmax (negate:s@2 @0) (negate:s@3 @1))
1766 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
1767 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1768 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
1769 (negate (maxmin @0 @1)))))
1770 /* MIN (~X, ~Y) -> ~MAX (X, Y)
1771 MAX (~X, ~Y) -> ~MIN (X, Y) */
1772 (for minmax (min max)
1775 (minmax (bit_not:s@2 @0) (bit_not:s@3 @1))
1776 (bit_not (maxmin @0 @1))))
1778 /* MIN (X, Y) == X -> X <= Y */
1779 (for minmax (min min max max)
1783 (cmp:c (minmax:c @0 @1) @0)
1784 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0)))
1786 /* MIN (X, 5) == 0 -> X == 0
1787 MIN (X, 5) == 7 -> false */
1790 (cmp (min @0 INTEGER_CST@1) INTEGER_CST@2)
1791 (if (wi::lt_p (@1, @2, TYPE_SIGN (TREE_TYPE (@0))))
1792 { constant_boolean_node (cmp == NE_EXPR, type); }
1793 (if (wi::gt_p (@1, @2, TYPE_SIGN (TREE_TYPE (@0))))
1797 (cmp (max @0 INTEGER_CST@1) INTEGER_CST@2)
1798 (if (wi::gt_p (@1, @2, TYPE_SIGN (TREE_TYPE (@0))))
1799 { constant_boolean_node (cmp == NE_EXPR, type); }
1800 (if (wi::lt_p (@1, @2, TYPE_SIGN (TREE_TYPE (@0))))
1802 /* MIN (X, C1) < C2 -> X < C2 || C1 < C2 */
1803 (for minmax (min min max max min min max max )
1804 cmp (lt le gt ge gt ge lt le )
1805 comb (bit_ior bit_ior bit_ior bit_ior bit_and bit_and bit_and bit_and)
1807 (cmp (minmax @0 INTEGER_CST@1) INTEGER_CST@2)
1808 (comb (cmp @0 @2) (cmp @1 @2))))
1810 /* Simplifications of shift and rotates. */
1812 (for rotate (lrotate rrotate)
1814 (rotate integer_all_onesp@0 @1)
1817 /* Optimize -1 >> x for arithmetic right shifts. */
1819 (rshift integer_all_onesp@0 @1)
1820 (if (!TYPE_UNSIGNED (type)
1821 && tree_expr_nonnegative_p (@1))
1824 /* Optimize (x >> c) << c into x & (-1<<c). */
1826 (lshift (rshift @0 INTEGER_CST@1) @1)
1827 (if (wi::ltu_p (@1, element_precision (type)))
1828 (bit_and @0 (lshift { build_minus_one_cst (type); } @1))))
1830 /* Optimize (x << c) >> c into x & ((unsigned)-1 >> c) for unsigned
1833 (rshift (lshift @0 INTEGER_CST@1) @1)
1834 (if (TYPE_UNSIGNED (type)
1835 && (wi::ltu_p (@1, element_precision (type))))
1836 (bit_and @0 (rshift { build_minus_one_cst (type); } @1))))
1838 (for shiftrotate (lrotate rrotate lshift rshift)
1840 (shiftrotate @0 integer_zerop)
1843 (shiftrotate integer_zerop@0 @1)
1845 /* Prefer vector1 << scalar to vector1 << vector2
1846 if vector2 is uniform. */
1847 (for vec (VECTOR_CST CONSTRUCTOR)
1849 (shiftrotate @0 vec@1)
1850 (with { tree tem = uniform_vector_p (@1); }
1852 (shiftrotate @0 { tem; }))))))
1854 /* Simplify X << Y where Y's low width bits are 0 to X, as only valid
1855 Y is 0. Similarly for X >> Y. */
1857 (for shift (lshift rshift)
1859 (shift @0 SSA_NAME@1)
1860 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1)))
1862 int width = ceil_log2 (element_precision (TREE_TYPE (@0)));
1863 int prec = TYPE_PRECISION (TREE_TYPE (@1));
1865 (if ((get_nonzero_bits (@1) & wi::mask (width, false, prec)) == 0)
1869 /* Rewrite an LROTATE_EXPR by a constant into an
1870 RROTATE_EXPR by a new constant. */
1872 (lrotate @0 INTEGER_CST@1)
1873 (rrotate @0 { const_binop (MINUS_EXPR, TREE_TYPE (@1),
1874 build_int_cst (TREE_TYPE (@1),
1875 element_precision (type)), @1); }))
1877 /* Turn (a OP c1) OP c2 into a OP (c1+c2). */
1878 (for op (lrotate rrotate rshift lshift)
1880 (op (op @0 INTEGER_CST@1) INTEGER_CST@2)
1881 (with { unsigned int prec = element_precision (type); }
1882 (if (wi::ge_p (@1, 0, TYPE_SIGN (TREE_TYPE (@1)))
1883 && wi::lt_p (@1, prec, TYPE_SIGN (TREE_TYPE (@1)))
1884 && wi::ge_p (@2, 0, TYPE_SIGN (TREE_TYPE (@2)))
1885 && wi::lt_p (@2, prec, TYPE_SIGN (TREE_TYPE (@2))))
1886 (with { unsigned int low = (tree_to_uhwi (@1)
1887 + tree_to_uhwi (@2)); }
1888 /* Deal with a OP (c1 + c2) being undefined but (a OP c1) OP c2
1889 being well defined. */
1891 (if (op == LROTATE_EXPR || op == RROTATE_EXPR)
1892 (op @0 { build_int_cst (TREE_TYPE (@1), low % prec); })
1893 (if (TYPE_UNSIGNED (type) || op == LSHIFT_EXPR)
1894 { build_zero_cst (type); }
1895 (op @0 { build_int_cst (TREE_TYPE (@1), prec - 1); })))
1896 (op @0 { build_int_cst (TREE_TYPE (@1), low); })))))))
1899 /* ((1 << A) & 1) != 0 -> A == 0
1900 ((1 << A) & 1) == 0 -> A != 0 */
1904 (cmp (bit_and (lshift integer_onep @0) integer_onep) integer_zerop)
1905 (icmp @0 { build_zero_cst (TREE_TYPE (@0)); })))
1907 /* (CST1 << A) == CST2 -> A == ctz (CST2) - ctz (CST1)
1908 (CST1 << A) != CST2 -> A != ctz (CST2) - ctz (CST1)
1912 (cmp (lshift INTEGER_CST@0 @1) INTEGER_CST@2)
1913 (with { int cand = wi::ctz (@2) - wi::ctz (@0); }
1915 || (!integer_zerop (@2)
1916 && wi::ne_p (wi::lshift (@0, cand), @2)))
1917 { constant_boolean_node (cmp == NE_EXPR, type); }
1918 (if (!integer_zerop (@2)
1919 && wi::eq_p (wi::lshift (@0, cand), @2))
1920 (cmp @1 { build_int_cst (TREE_TYPE (@1), cand); }))))))
1922 /* Fold (X << C1) & C2 into (X << C1) & (C2 | ((1 << C1) - 1))
1923 (X >> C1) & C2 into (X >> C1) & (C2 | ~((type) -1 >> C1))
1924 if the new mask might be further optimized. */
1925 (for shift (lshift rshift)
1927 (bit_and (convert?:s@4 (shift:s@5 (convert1?@3 @0) INTEGER_CST@1))
1929 (if (tree_nop_conversion_p (TREE_TYPE (@4), TREE_TYPE (@5))
1930 && TYPE_PRECISION (type) <= HOST_BITS_PER_WIDE_INT
1931 && tree_fits_uhwi_p (@1)
1932 && tree_to_uhwi (@1) > 0
1933 && tree_to_uhwi (@1) < TYPE_PRECISION (type))
1936 unsigned int shiftc = tree_to_uhwi (@1);
1937 unsigned HOST_WIDE_INT mask = TREE_INT_CST_LOW (@2);
1938 unsigned HOST_WIDE_INT newmask, zerobits = 0;
1939 tree shift_type = TREE_TYPE (@3);
1942 if (shift == LSHIFT_EXPR)
1943 zerobits = ((HOST_WIDE_INT_1U << shiftc) - 1);
1944 else if (shift == RSHIFT_EXPR
1945 && type_has_mode_precision_p (shift_type))
1947 prec = TYPE_PRECISION (TREE_TYPE (@3));
1949 /* See if more bits can be proven as zero because of
1952 && TYPE_UNSIGNED (TREE_TYPE (@0)))
1954 tree inner_type = TREE_TYPE (@0);
1955 if (type_has_mode_precision_p (inner_type)
1956 && TYPE_PRECISION (inner_type) < prec)
1958 prec = TYPE_PRECISION (inner_type);
1959 /* See if we can shorten the right shift. */
1961 shift_type = inner_type;
1962 /* Otherwise X >> C1 is all zeros, so we'll optimize
1963 it into (X, 0) later on by making sure zerobits
1967 zerobits = HOST_WIDE_INT_M1U;
1970 zerobits >>= HOST_BITS_PER_WIDE_INT - shiftc;
1971 zerobits <<= prec - shiftc;
1973 /* For arithmetic shift if sign bit could be set, zerobits
1974 can contain actually sign bits, so no transformation is
1975 possible, unless MASK masks them all away. In that
1976 case the shift needs to be converted into logical shift. */
1977 if (!TYPE_UNSIGNED (TREE_TYPE (@3))
1978 && prec == TYPE_PRECISION (TREE_TYPE (@3)))
1980 if ((mask & zerobits) == 0)
1981 shift_type = unsigned_type_for (TREE_TYPE (@3));
1987 /* ((X << 16) & 0xff00) is (X, 0). */
1988 (if ((mask & zerobits) == mask)
1989 { build_int_cst (type, 0); }
1990 (with { newmask = mask | zerobits; }
1991 (if (newmask != mask && (newmask & (newmask + 1)) == 0)
1994 /* Only do the transformation if NEWMASK is some integer
1996 for (prec = BITS_PER_UNIT;
1997 prec < HOST_BITS_PER_WIDE_INT; prec <<= 1)
1998 if (newmask == (HOST_WIDE_INT_1U << prec) - 1)
2001 (if (prec < HOST_BITS_PER_WIDE_INT
2002 || newmask == HOST_WIDE_INT_M1U)
2004 { tree newmaskt = build_int_cst_type (TREE_TYPE (@2), newmask); }
2005 (if (!tree_int_cst_equal (newmaskt, @2))
2006 (if (shift_type != TREE_TYPE (@3))
2007 (bit_and (convert (shift:shift_type (convert @3) @1)) { newmaskt; })
2008 (bit_and @4 { newmaskt; })))))))))))))
2010 /* Fold (X {&,^,|} C2) << C1 into (X << C1) {&,^,|} (C2 << C1)
2011 (X {&,^,|} C2) >> C1 into (X >> C1) & (C2 >> C1). */
2012 (for shift (lshift rshift)
2013 (for bit_op (bit_and bit_xor bit_ior)
2015 (shift (convert?:s (bit_op:s @0 INTEGER_CST@2)) INTEGER_CST@1)
2016 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2017 (with { tree mask = int_const_binop (shift, fold_convert (type, @2), @1); }
2018 (bit_op (shift (convert @0) @1) { mask; }))))))
2020 /* ~(~X >> Y) -> X >> Y (for arithmetic shift). */
2022 (bit_not (convert1?:s (rshift:s (convert2?@0 (bit_not @1)) @2)))
2023 (if (!TYPE_UNSIGNED (TREE_TYPE (@0))
2024 && (element_precision (TREE_TYPE (@0))
2025 <= element_precision (TREE_TYPE (@1))
2026 || !TYPE_UNSIGNED (TREE_TYPE (@1))))
2028 { tree shift_type = TREE_TYPE (@0); }
2029 (convert (rshift (convert:shift_type @1) @2)))))
2031 /* ~(~X >>r Y) -> X >>r Y
2032 ~(~X <<r Y) -> X <<r Y */
2033 (for rotate (lrotate rrotate)
2035 (bit_not (convert1?:s (rotate:s (convert2?@0 (bit_not @1)) @2)))
2036 (if ((element_precision (TREE_TYPE (@0))
2037 <= element_precision (TREE_TYPE (@1))
2038 || !TYPE_UNSIGNED (TREE_TYPE (@1)))
2039 && (element_precision (type) <= element_precision (TREE_TYPE (@0))
2040 || !TYPE_UNSIGNED (TREE_TYPE (@0))))
2042 { tree rotate_type = TREE_TYPE (@0); }
2043 (convert (rotate (convert:rotate_type @1) @2))))))
2045 /* Simplifications of conversions. */
2047 /* Basic strip-useless-type-conversions / strip_nops. */
2048 (for cvt (convert view_convert float fix_trunc)
2051 (if ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@0)))
2052 || (GENERIC && type == TREE_TYPE (@0)))
2055 /* Contract view-conversions. */
2057 (view_convert (view_convert @0))
2060 /* For integral conversions with the same precision or pointer
2061 conversions use a NOP_EXPR instead. */
2064 (if ((INTEGRAL_TYPE_P (type) || POINTER_TYPE_P (type))
2065 && (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
2066 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (@0)))
2069 /* Strip inner integral conversions that do not change precision or size, or
2070 zero-extend while keeping the same size (for bool-to-char). */
2072 (view_convert (convert@0 @1))
2073 (if ((INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
2074 && (INTEGRAL_TYPE_P (TREE_TYPE (@1)) || POINTER_TYPE_P (TREE_TYPE (@1)))
2075 && TYPE_SIZE (TREE_TYPE (@0)) == TYPE_SIZE (TREE_TYPE (@1))
2076 && (TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1))
2077 || (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (TREE_TYPE (@1))
2078 && TYPE_UNSIGNED (TREE_TYPE (@1)))))
2081 /* Re-association barriers around constants and other re-association
2082 barriers can be removed. */
2084 (paren CONSTANT_CLASS_P@0)
2087 (paren (paren@1 @0))
2090 /* Handle cases of two conversions in a row. */
2091 (for ocvt (convert float fix_trunc)
2092 (for icvt (convert float)
2097 tree inside_type = TREE_TYPE (@0);
2098 tree inter_type = TREE_TYPE (@1);
2099 int inside_int = INTEGRAL_TYPE_P (inside_type);
2100 int inside_ptr = POINTER_TYPE_P (inside_type);
2101 int inside_float = FLOAT_TYPE_P (inside_type);
2102 int inside_vec = VECTOR_TYPE_P (inside_type);
2103 unsigned int inside_prec = TYPE_PRECISION (inside_type);
2104 int inside_unsignedp = TYPE_UNSIGNED (inside_type);
2105 int inter_int = INTEGRAL_TYPE_P (inter_type);
2106 int inter_ptr = POINTER_TYPE_P (inter_type);
2107 int inter_float = FLOAT_TYPE_P (inter_type);
2108 int inter_vec = VECTOR_TYPE_P (inter_type);
2109 unsigned int inter_prec = TYPE_PRECISION (inter_type);
2110 int inter_unsignedp = TYPE_UNSIGNED (inter_type);
2111 int final_int = INTEGRAL_TYPE_P (type);
2112 int final_ptr = POINTER_TYPE_P (type);
2113 int final_float = FLOAT_TYPE_P (type);
2114 int final_vec = VECTOR_TYPE_P (type);
2115 unsigned int final_prec = TYPE_PRECISION (type);
2116 int final_unsignedp = TYPE_UNSIGNED (type);
2119 /* In addition to the cases of two conversions in a row
2120 handled below, if we are converting something to its own
2121 type via an object of identical or wider precision, neither
2122 conversion is needed. */
2123 (if (((GIMPLE && useless_type_conversion_p (type, inside_type))
2125 && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (inside_type)))
2126 && (((inter_int || inter_ptr) && final_int)
2127 || (inter_float && final_float))
2128 && inter_prec >= final_prec)
2131 /* Likewise, if the intermediate and initial types are either both
2132 float or both integer, we don't need the middle conversion if the
2133 former is wider than the latter and doesn't change the signedness
2134 (for integers). Avoid this if the final type is a pointer since
2135 then we sometimes need the middle conversion. */
2136 (if (((inter_int && inside_int) || (inter_float && inside_float))
2137 && (final_int || final_float)
2138 && inter_prec >= inside_prec
2139 && (inter_float || inter_unsignedp == inside_unsignedp))
2142 /* If we have a sign-extension of a zero-extended value, we can
2143 replace that by a single zero-extension. Likewise if the
2144 final conversion does not change precision we can drop the
2145 intermediate conversion. */
2146 (if (inside_int && inter_int && final_int
2147 && ((inside_prec < inter_prec && inter_prec < final_prec
2148 && inside_unsignedp && !inter_unsignedp)
2149 || final_prec == inter_prec))
2152 /* Two conversions in a row are not needed unless:
2153 - some conversion is floating-point (overstrict for now), or
2154 - some conversion is a vector (overstrict for now), or
2155 - the intermediate type is narrower than both initial and
2157 - the intermediate type and innermost type differ in signedness,
2158 and the outermost type is wider than the intermediate, or
2159 - the initial type is a pointer type and the precisions of the
2160 intermediate and final types differ, or
2161 - the final type is a pointer type and the precisions of the
2162 initial and intermediate types differ. */
2163 (if (! inside_float && ! inter_float && ! final_float
2164 && ! inside_vec && ! inter_vec && ! final_vec
2165 && (inter_prec >= inside_prec || inter_prec >= final_prec)
2166 && ! (inside_int && inter_int
2167 && inter_unsignedp != inside_unsignedp
2168 && inter_prec < final_prec)
2169 && ((inter_unsignedp && inter_prec > inside_prec)
2170 == (final_unsignedp && final_prec > inter_prec))
2171 && ! (inside_ptr && inter_prec != final_prec)
2172 && ! (final_ptr && inside_prec != inter_prec))
2175 /* A truncation to an unsigned type (a zero-extension) should be
2176 canonicalized as bitwise and of a mask. */
2177 (if (GIMPLE /* PR70366: doing this in GENERIC breaks -Wconversion. */
2178 && final_int && inter_int && inside_int
2179 && final_prec == inside_prec
2180 && final_prec > inter_prec
2182 (convert (bit_and @0 { wide_int_to_tree
2184 wi::mask (inter_prec, false,
2185 TYPE_PRECISION (inside_type))); })))
2187 /* If we are converting an integer to a floating-point that can
2188 represent it exactly and back to an integer, we can skip the
2189 floating-point conversion. */
2190 (if (GIMPLE /* PR66211 */
2191 && inside_int && inter_float && final_int &&
2192 (unsigned) significand_size (TYPE_MODE (inter_type))
2193 >= inside_prec - !inside_unsignedp)
2196 /* If we have a narrowing conversion to an integral type that is fed by a
2197 BIT_AND_EXPR, we might be able to remove the BIT_AND_EXPR if it merely
2198 masks off bits outside the final type (and nothing else). */
2200 (convert (bit_and @0 INTEGER_CST@1))
2201 (if (INTEGRAL_TYPE_P (type)
2202 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
2203 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))
2204 && operand_equal_p (@1, build_low_bits_mask (TREE_TYPE (@1),
2205 TYPE_PRECISION (type)), 0))
2209 /* (X /[ex] A) * A -> X. */
2211 (mult (convert1? (exact_div @0 @@1)) (convert2? @1))
2214 /* Canonicalization of binary operations. */
2216 /* Convert X + -C into X - C. */
2218 (plus @0 REAL_CST@1)
2219 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
2220 (with { tree tem = const_unop (NEGATE_EXPR, type, @1); }
2221 (if (!TREE_OVERFLOW (tem) || !flag_trapping_math)
2222 (minus @0 { tem; })))))
2224 /* Convert x+x into x*2. */
2227 (if (SCALAR_FLOAT_TYPE_P (type))
2228 (mult @0 { build_real (type, dconst2); })
2229 (if (INTEGRAL_TYPE_P (type))
2230 (mult @0 { build_int_cst (type, 2); }))))
2233 (minus integer_zerop @1)
2236 /* (ARG0 - ARG1) is the same as (-ARG1 + ARG0). So check whether
2237 ARG0 is zero and X + ARG0 reduces to X, since that would mean
2238 (-ARG1 + ARG0) reduces to -ARG1. */
2240 (minus real_zerop@0 @1)
2241 (if (fold_real_zero_addition_p (type, @0, 0))
2244 /* Transform x * -1 into -x. */
2246 (mult @0 integer_minus_onep)
2249 /* Reassociate (X * CST) * Y to (X * Y) * CST. This does not introduce
2250 signed overflow for CST != 0 && CST != -1. */
2252 (mult:c (mult:s @0 INTEGER_CST@1) @2)
2253 (if (TREE_CODE (@2) != INTEGER_CST
2254 && !integer_zerop (@1) && !integer_minus_onep (@1))
2255 (mult (mult @0 @2) @1)))
2257 /* True if we can easily extract the real and imaginary parts of a complex
2259 (match compositional_complex
2260 (convert? (complex @0 @1)))
2262 /* COMPLEX_EXPR and REALPART/IMAGPART_EXPR cancellations. */
2264 (complex (realpart @0) (imagpart @0))
2267 (realpart (complex @0 @1))
2270 (imagpart (complex @0 @1))
2273 /* Sometimes we only care about half of a complex expression. */
2275 (realpart (convert?:s (conj:s @0)))
2276 (convert (realpart @0)))
2278 (imagpart (convert?:s (conj:s @0)))
2279 (convert (negate (imagpart @0))))
2280 (for part (realpart imagpart)
2281 (for op (plus minus)
2283 (part (convert?:s@2 (op:s @0 @1)))
2284 (convert (op (part @0) (part @1))))))
2286 (realpart (convert?:s (CEXPI:s @0)))
2289 (imagpart (convert?:s (CEXPI:s @0)))
2292 /* conj(conj(x)) -> x */
2294 (conj (convert? (conj @0)))
2295 (if (tree_nop_conversion_p (TREE_TYPE (@0), type))
2298 /* conj({x,y}) -> {x,-y} */
2300 (conj (convert?:s (complex:s @0 @1)))
2301 (with { tree itype = TREE_TYPE (type); }
2302 (complex (convert:itype @0) (negate (convert:itype @1)))))
2304 /* BSWAP simplifications, transforms checked by gcc.dg/builtin-bswap-8.c. */
2305 (for bswap (BUILT_IN_BSWAP16 BUILT_IN_BSWAP32 BUILT_IN_BSWAP64)
2310 (bswap (bit_not (bswap @0)))
2312 (for bitop (bit_xor bit_ior bit_and)
2314 (bswap (bitop:c (bswap @0) @1))
2315 (bitop @0 (bswap @1)))))
2318 /* Combine COND_EXPRs and VEC_COND_EXPRs. */
2320 /* Simplify constant conditions.
2321 Only optimize constant conditions when the selected branch
2322 has the same type as the COND_EXPR. This avoids optimizing
2323 away "c ? x : throw", where the throw has a void type.
2324 Note that we cannot throw away the fold-const.c variant nor
2325 this one as we depend on doing this transform before possibly
2326 A ? B : B -> B triggers and the fold-const.c one can optimize
2327 0 ? A : B to B even if A has side-effects. Something
2328 genmatch cannot handle. */
2330 (cond INTEGER_CST@0 @1 @2)
2331 (if (integer_zerop (@0))
2332 (if (!VOID_TYPE_P (TREE_TYPE (@2)) || VOID_TYPE_P (type))
2334 (if (!VOID_TYPE_P (TREE_TYPE (@1)) || VOID_TYPE_P (type))
2337 (vec_cond VECTOR_CST@0 @1 @2)
2338 (if (integer_all_onesp (@0))
2340 (if (integer_zerop (@0))
2343 /* Simplification moved from fold_cond_expr_with_comparison. It may also
2345 /* This pattern implements two kinds simplification:
2348 (cond (cmp (convert1? x) c1) (convert2? x) c2) -> (minmax (x c)) if:
2349 1) Conversions are type widening from smaller type.
2350 2) Const c1 equals to c2 after canonicalizing comparison.
2351 3) Comparison has tree code LT, LE, GT or GE.
2352 This specific pattern is needed when (cmp (convert x) c) may not
2353 be simplified by comparison patterns because of multiple uses of
2354 x. It also makes sense here because simplifying across multiple
2355 referred var is always benefitial for complicated cases.
2358 (cond (eq (convert1? x) c1) (convert2? x) c2) -> (cond (eq x c1) c1 c2). */
2359 (for cmp (lt le gt ge eq)
2361 (cond (cmp (convert1? @1) INTEGER_CST@3) (convert2? @1) INTEGER_CST@2)
2364 tree from_type = TREE_TYPE (@1);
2365 tree c1_type = TREE_TYPE (@3), c2_type = TREE_TYPE (@2);
2366 enum tree_code code = ERROR_MARK;
2368 if (INTEGRAL_TYPE_P (from_type)
2369 && int_fits_type_p (@2, from_type)
2370 && (types_match (c1_type, from_type)
2371 || (TYPE_PRECISION (c1_type) > TYPE_PRECISION (from_type)
2372 && (TYPE_UNSIGNED (from_type)
2373 || TYPE_SIGN (c1_type) == TYPE_SIGN (from_type))))
2374 && (types_match (c2_type, from_type)
2375 || (TYPE_PRECISION (c2_type) > TYPE_PRECISION (from_type)
2376 && (TYPE_UNSIGNED (from_type)
2377 || TYPE_SIGN (c2_type) == TYPE_SIGN (from_type)))))
2381 if (wi::to_widest (@3) == (wi::to_widest (@2) - 1))
2383 /* X <= Y - 1 equals to X < Y. */
2386 /* X > Y - 1 equals to X >= Y. */
2390 if (wi::to_widest (@3) == (wi::to_widest (@2) + 1))
2392 /* X < Y + 1 equals to X <= Y. */
2395 /* X >= Y + 1 equals to X > Y. */
2399 if (code != ERROR_MARK
2400 || wi::to_widest (@2) == wi::to_widest (@3))
2402 if (cmp == LT_EXPR || cmp == LE_EXPR)
2404 if (cmp == GT_EXPR || cmp == GE_EXPR)
2408 /* Can do A == C1 ? A : C2 -> A == C1 ? C1 : C2? */
2409 else if (int_fits_type_p (@3, from_type))
2413 (if (code == MAX_EXPR)
2414 (convert (max @1 (convert @2)))
2415 (if (code == MIN_EXPR)
2416 (convert (min @1 (convert @2)))
2417 (if (code == EQ_EXPR)
2418 (convert (cond (eq @1 (convert @3))
2419 (convert:from_type @3) (convert:from_type @2)))))))))
2421 /* (cond (cmp (convert? x) c1) (op x c2) c3) -> (op (minmax x c1) c2) if:
2423 1) OP is PLUS or MINUS.
2424 2) CMP is LT, LE, GT or GE.
2425 3) C3 == (C1 op C2), and computation doesn't have undefined behavior.
2427 This pattern also handles special cases like:
2429 A) Operand x is a unsigned to signed type conversion and c1 is
2430 integer zero. In this case,
2431 (signed type)x < 0 <=> x > MAX_VAL(signed type)
2432 (signed type)x >= 0 <=> x <= MAX_VAL(signed type)
2433 B) Const c1 may not equal to (C3 op' C2). In this case we also
2434 check equality for (c1+1) and (c1-1) by adjusting comparison
2437 TODO: Though signed type is handled by this pattern, it cannot be
2438 simplified at the moment because C standard requires additional
2439 type promotion. In order to match&simplify it here, the IR needs
2440 to be cleaned up by other optimizers, i.e, VRP. */
2441 (for op (plus minus)
2442 (for cmp (lt le gt ge)
2444 (cond (cmp (convert? @X) INTEGER_CST@1) (op @X INTEGER_CST@2) INTEGER_CST@3)
2445 (with { tree from_type = TREE_TYPE (@X), to_type = TREE_TYPE (@1); }
2446 (if (types_match (from_type, to_type)
2447 /* Check if it is special case A). */
2448 || (TYPE_UNSIGNED (from_type)
2449 && !TYPE_UNSIGNED (to_type)
2450 && TYPE_PRECISION (from_type) == TYPE_PRECISION (to_type)
2451 && integer_zerop (@1)
2452 && (cmp == LT_EXPR || cmp == GE_EXPR)))
2455 bool overflow = false;
2456 enum tree_code code, cmp_code = cmp;
2457 wide_int real_c1, c1 = @1, c2 = @2, c3 = @3;
2458 signop sgn = TYPE_SIGN (from_type);
2460 /* Handle special case A), given x of unsigned type:
2461 ((signed type)x < 0) <=> (x > MAX_VAL(signed type))
2462 ((signed type)x >= 0) <=> (x <= MAX_VAL(signed type)) */
2463 if (!types_match (from_type, to_type))
2465 if (cmp_code == LT_EXPR)
2467 if (cmp_code == GE_EXPR)
2469 c1 = wi::max_value (to_type);
2471 /* To simplify this pattern, we require c3 = (c1 op c2). Here we
2472 compute (c3 op' c2) and check if it equals to c1 with op' being
2473 the inverted operator of op. Make sure overflow doesn't happen
2474 if it is undefined. */
2475 if (op == PLUS_EXPR)
2476 real_c1 = wi::sub (c3, c2, sgn, &overflow);
2478 real_c1 = wi::add (c3, c2, sgn, &overflow);
2481 if (!overflow || !TYPE_OVERFLOW_UNDEFINED (from_type))
2483 /* Check if c1 equals to real_c1. Boundary condition is handled
2484 by adjusting comparison operation if necessary. */
2485 if (!wi::cmp (wi::sub (real_c1, 1, sgn, &overflow), c1, sgn)
2488 /* X <= Y - 1 equals to X < Y. */
2489 if (cmp_code == LE_EXPR)
2491 /* X > Y - 1 equals to X >= Y. */
2492 if (cmp_code == GT_EXPR)
2495 if (!wi::cmp (wi::add (real_c1, 1, sgn, &overflow), c1, sgn)
2498 /* X < Y + 1 equals to X <= Y. */
2499 if (cmp_code == LT_EXPR)
2501 /* X >= Y + 1 equals to X > Y. */
2502 if (cmp_code == GE_EXPR)
2505 if (code != cmp_code || !wi::cmp (real_c1, c1, sgn))
2507 if (cmp_code == LT_EXPR || cmp_code == LE_EXPR)
2509 if (cmp_code == GT_EXPR || cmp_code == GE_EXPR)
2514 (if (code == MAX_EXPR)
2515 (op (max @X { wide_int_to_tree (from_type, real_c1); })
2516 { wide_int_to_tree (from_type, c2); })
2517 (if (code == MIN_EXPR)
2518 (op (min @X { wide_int_to_tree (from_type, real_c1); })
2519 { wide_int_to_tree (from_type, c2); })))))))))
2521 (for cnd (cond vec_cond)
2522 /* A ? B : (A ? X : C) -> A ? B : C. */
2524 (cnd @0 (cnd @0 @1 @2) @3)
2527 (cnd @0 @1 (cnd @0 @2 @3))
2529 /* A ? B : (!A ? C : X) -> A ? B : C. */
2530 /* ??? This matches embedded conditions open-coded because genmatch
2531 would generate matching code for conditions in separate stmts only.
2532 The following is still important to merge then and else arm cases
2533 from if-conversion. */
2535 (cnd @0 @1 (cnd @2 @3 @4))
2536 (if (COMPARISON_CLASS_P (@0)
2537 && COMPARISON_CLASS_P (@2)
2538 && invert_tree_comparison
2539 (TREE_CODE (@0), HONOR_NANS (TREE_OPERAND (@0, 0))) == TREE_CODE (@2)
2540 && operand_equal_p (TREE_OPERAND (@0, 0), TREE_OPERAND (@2, 0), 0)
2541 && operand_equal_p (TREE_OPERAND (@0, 1), TREE_OPERAND (@2, 1), 0))
2544 (cnd @0 (cnd @1 @2 @3) @4)
2545 (if (COMPARISON_CLASS_P (@0)
2546 && COMPARISON_CLASS_P (@1)
2547 && invert_tree_comparison
2548 (TREE_CODE (@0), HONOR_NANS (TREE_OPERAND (@0, 0))) == TREE_CODE (@1)
2549 && operand_equal_p (TREE_OPERAND (@0, 0), TREE_OPERAND (@1, 0), 0)
2550 && operand_equal_p (TREE_OPERAND (@0, 1), TREE_OPERAND (@1, 1), 0))
2553 /* A ? B : B -> B. */
2558 /* !A ? B : C -> A ? C : B. */
2560 (cnd (logical_inverted_value truth_valued_p@0) @1 @2)
2563 /* A + (B vcmp C ? 1 : 0) -> A - (B vcmp C ? -1 : 0), since vector comparisons
2564 return all -1 or all 0 results. */
2565 /* ??? We could instead convert all instances of the vec_cond to negate,
2566 but that isn't necessarily a win on its own. */
2568 (plus:c @3 (view_convert? (vec_cond:s @0 integer_each_onep@1 integer_zerop@2)))
2569 (if (VECTOR_TYPE_P (type)
2570 && TYPE_VECTOR_SUBPARTS (type) == TYPE_VECTOR_SUBPARTS (TREE_TYPE (@1))
2571 && (TYPE_MODE (TREE_TYPE (type))
2572 == TYPE_MODE (TREE_TYPE (TREE_TYPE (@1)))))
2573 (minus @3 (view_convert (vec_cond @0 (negate @1) @2)))))
2575 /* ... likewise A - (B vcmp C ? 1 : 0) -> A + (B vcmp C ? -1 : 0). */
2577 (minus @3 (view_convert? (vec_cond:s @0 integer_each_onep@1 integer_zerop@2)))
2578 (if (VECTOR_TYPE_P (type)
2579 && TYPE_VECTOR_SUBPARTS (type) == TYPE_VECTOR_SUBPARTS (TREE_TYPE (@1))
2580 && (TYPE_MODE (TREE_TYPE (type))
2581 == TYPE_MODE (TREE_TYPE (TREE_TYPE (@1)))))
2582 (plus @3 (view_convert (vec_cond @0 (negate @1) @2)))))
2585 /* Simplifications of comparisons. */
2587 /* See if we can reduce the magnitude of a constant involved in a
2588 comparison by changing the comparison code. This is a canonicalization
2589 formerly done by maybe_canonicalize_comparison_1. */
2593 (cmp @0 INTEGER_CST@1)
2594 (if (tree_int_cst_sgn (@1) == -1)
2595 (acmp @0 { wide_int_to_tree (TREE_TYPE (@1), wi::add (@1, 1)); }))))
2599 (cmp @0 INTEGER_CST@1)
2600 (if (tree_int_cst_sgn (@1) == 1)
2601 (acmp @0 { wide_int_to_tree (TREE_TYPE (@1), wi::sub (@1, 1)); }))))
2604 /* We can simplify a logical negation of a comparison to the
2605 inverted comparison. As we cannot compute an expression
2606 operator using invert_tree_comparison we have to simulate
2607 that with expression code iteration. */
2608 (for cmp (tcc_comparison)
2609 icmp (inverted_tcc_comparison)
2610 ncmp (inverted_tcc_comparison_with_nans)
2611 /* Ideally we'd like to combine the following two patterns
2612 and handle some more cases by using
2613 (logical_inverted_value (cmp @0 @1))
2614 here but for that genmatch would need to "inline" that.
2615 For now implement what forward_propagate_comparison did. */
2617 (bit_not (cmp @0 @1))
2618 (if (VECTOR_TYPE_P (type)
2619 || (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1))
2620 /* Comparison inversion may be impossible for trapping math,
2621 invert_tree_comparison will tell us. But we can't use
2622 a computed operator in the replacement tree thus we have
2623 to play the trick below. */
2624 (with { enum tree_code ic = invert_tree_comparison
2625 (cmp, HONOR_NANS (@0)); }
2631 (bit_xor (cmp @0 @1) integer_truep)
2632 (with { enum tree_code ic = invert_tree_comparison
2633 (cmp, HONOR_NANS (@0)); }
2639 /* Transform comparisons of the form X - Y CMP 0 to X CMP Y.
2640 ??? The transformation is valid for the other operators if overflow
2641 is undefined for the type, but performing it here badly interacts
2642 with the transformation in fold_cond_expr_with_comparison which
2643 attempts to synthetize ABS_EXPR. */
2646 (cmp (minus@2 @0 @1) integer_zerop)
2647 (if (single_use (@2))
2650 /* Transform comparisons of the form X * C1 CMP 0 to X CMP 0 in the
2651 signed arithmetic case. That form is created by the compiler
2652 often enough for folding it to be of value. One example is in
2653 computing loop trip counts after Operator Strength Reduction. */
2654 (for cmp (simple_comparison)
2655 scmp (swapped_simple_comparison)
2657 (cmp (mult@3 @0 INTEGER_CST@1) integer_zerop@2)
2658 /* Handle unfolded multiplication by zero. */
2659 (if (integer_zerop (@1))
2661 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2662 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2664 /* If @1 is negative we swap the sense of the comparison. */
2665 (if (tree_int_cst_sgn (@1) < 0)
2669 /* Simplify comparison of something with itself. For IEEE
2670 floating-point, we can only do some of these simplifications. */
2674 (if (! FLOAT_TYPE_P (TREE_TYPE (@0))
2675 || ! HONOR_NANS (@0))
2676 { constant_boolean_node (true, type); }
2677 (if (cmp != EQ_EXPR)
2683 || ! FLOAT_TYPE_P (TREE_TYPE (@0))
2684 || ! HONOR_NANS (@0))
2685 { constant_boolean_node (false, type); })))
2686 (for cmp (unle unge uneq)
2689 { constant_boolean_node (true, type); }))
2690 (for cmp (unlt ungt)
2696 (if (!flag_trapping_math)
2697 { constant_boolean_node (false, type); }))
2699 /* Fold ~X op ~Y as Y op X. */
2700 (for cmp (simple_comparison)
2702 (cmp (bit_not@2 @0) (bit_not@3 @1))
2703 (if (single_use (@2) && single_use (@3))
2706 /* Fold ~X op C as X op' ~C, where op' is the swapped comparison. */
2707 (for cmp (simple_comparison)
2708 scmp (swapped_simple_comparison)
2710 (cmp (bit_not@2 @0) CONSTANT_CLASS_P@1)
2711 (if (single_use (@2)
2712 && (TREE_CODE (@1) == INTEGER_CST || TREE_CODE (@1) == VECTOR_CST))
2713 (scmp @0 (bit_not @1)))))
2715 (for cmp (simple_comparison)
2716 /* Fold (double)float1 CMP (double)float2 into float1 CMP float2. */
2718 (cmp (convert@2 @0) (convert? @1))
2719 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
2720 && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@2))
2721 == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0)))
2722 && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@2))
2723 == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1))))
2726 tree type1 = TREE_TYPE (@1);
2727 if (TREE_CODE (@1) == REAL_CST && !DECIMAL_FLOAT_TYPE_P (type1))
2729 REAL_VALUE_TYPE orig = TREE_REAL_CST (@1);
2730 if (TYPE_PRECISION (type1) > TYPE_PRECISION (float_type_node)
2731 && exact_real_truncate (TYPE_MODE (float_type_node), &orig))
2732 type1 = float_type_node;
2733 if (TYPE_PRECISION (type1) > TYPE_PRECISION (double_type_node)
2734 && exact_real_truncate (TYPE_MODE (double_type_node), &orig))
2735 type1 = double_type_node;
2738 = (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type1)
2739 ? TREE_TYPE (@0) : type1);
2741 (if (TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (newtype))
2742 (cmp (convert:newtype @0) (convert:newtype @1))))))
2746 /* IEEE doesn't distinguish +0 and -0 in comparisons. */
2748 /* a CMP (-0) -> a CMP 0 */
2749 (if (REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@1)))
2750 (cmp @0 { build_real (TREE_TYPE (@1), dconst0); }))
2751 /* x != NaN is always true, other ops are always false. */
2752 (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
2753 && ! HONOR_SNANS (@1))
2754 { constant_boolean_node (cmp == NE_EXPR, type); })
2755 /* Fold comparisons against infinity. */
2756 (if (REAL_VALUE_ISINF (TREE_REAL_CST (@1))
2757 && MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (@1))))
2760 REAL_VALUE_TYPE max;
2761 enum tree_code code = cmp;
2762 bool neg = REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1));
2764 code = swap_tree_comparison (code);
2767 /* x > +Inf is always false, if with ignore sNANs. */
2768 (if (code == GT_EXPR
2769 && ! HONOR_SNANS (@0))
2770 { constant_boolean_node (false, type); })
2771 (if (code == LE_EXPR)
2772 /* x <= +Inf is always true, if we don't case about NaNs. */
2773 (if (! HONOR_NANS (@0))
2774 { constant_boolean_node (true, type); }
2775 /* x <= +Inf is the same as x == x, i.e. !isnan(x). */
2777 /* x == +Inf and x >= +Inf are always equal to x > DBL_MAX. */
2778 (if (code == EQ_EXPR || code == GE_EXPR)
2779 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
2781 (lt @0 { build_real (TREE_TYPE (@0), max); })
2782 (gt @0 { build_real (TREE_TYPE (@0), max); }))))
2783 /* x < +Inf is always equal to x <= DBL_MAX. */
2784 (if (code == LT_EXPR)
2785 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
2787 (ge @0 { build_real (TREE_TYPE (@0), max); })
2788 (le @0 { build_real (TREE_TYPE (@0), max); }))))
2789 /* x != +Inf is always equal to !(x > DBL_MAX). */
2790 (if (code == NE_EXPR)
2791 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
2792 (if (! HONOR_NANS (@0))
2794 (ge @0 { build_real (TREE_TYPE (@0), max); })
2795 (le @0 { build_real (TREE_TYPE (@0), max); }))
2797 (bit_xor (lt @0 { build_real (TREE_TYPE (@0), max); })
2798 { build_one_cst (type); })
2799 (bit_xor (gt @0 { build_real (TREE_TYPE (@0), max); })
2800 { build_one_cst (type); }))))))))))
2802 /* If this is a comparison of a real constant with a PLUS_EXPR
2803 or a MINUS_EXPR of a real constant, we can convert it into a
2804 comparison with a revised real constant as long as no overflow
2805 occurs when unsafe_math_optimizations are enabled. */
2806 (if (flag_unsafe_math_optimizations)
2807 (for op (plus minus)
2809 (cmp (op @0 REAL_CST@1) REAL_CST@2)
2812 tree tem = const_binop (op == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR,
2813 TREE_TYPE (@1), @2, @1);
2815 (if (tem && !TREE_OVERFLOW (tem))
2816 (cmp @0 { tem; }))))))
2818 /* Likewise, we can simplify a comparison of a real constant with
2819 a MINUS_EXPR whose first operand is also a real constant, i.e.
2820 (c1 - x) < c2 becomes x > c1-c2. Reordering is allowed on
2821 floating-point types only if -fassociative-math is set. */
2822 (if (flag_associative_math)
2824 (cmp (minus REAL_CST@0 @1) REAL_CST@2)
2825 (with { tree tem = const_binop (MINUS_EXPR, TREE_TYPE (@1), @0, @2); }
2826 (if (tem && !TREE_OVERFLOW (tem))
2827 (cmp { tem; } @1)))))
2829 /* Fold comparisons against built-in math functions. */
2830 (if (flag_unsafe_math_optimizations
2831 && ! flag_errno_math)
2834 (cmp (sq @0) REAL_CST@1)
2836 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
2838 /* sqrt(x) < y is always false, if y is negative. */
2839 (if (cmp == EQ_EXPR || cmp == LT_EXPR || cmp == LE_EXPR)
2840 { constant_boolean_node (false, type); })
2841 /* sqrt(x) > y is always true, if y is negative and we
2842 don't care about NaNs, i.e. negative values of x. */
2843 (if (cmp == NE_EXPR || !HONOR_NANS (@0))
2844 { constant_boolean_node (true, type); })
2845 /* sqrt(x) > y is the same as x >= 0, if y is negative. */
2846 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })))
2847 (if (real_equal (TREE_REAL_CST_PTR (@1), &dconst0))
2849 /* sqrt(x) < 0 is always false. */
2850 (if (cmp == LT_EXPR)
2851 { constant_boolean_node (false, type); })
2852 /* sqrt(x) >= 0 is always true if we don't care about NaNs. */
2853 (if (cmp == GE_EXPR && !HONOR_NANS (@0))
2854 { constant_boolean_node (true, type); })
2855 /* sqrt(x) <= 0 -> x == 0. */
2856 (if (cmp == LE_EXPR)
2858 /* Otherwise sqrt(x) cmp 0 -> x cmp 0. Here cmp can be >=, >,
2859 == or !=. In the last case:
2861 (sqrt(x) != 0) == (NaN != 0) == true == (x != 0)
2863 if x is negative or NaN. Due to -funsafe-math-optimizations,
2864 the results for other x follow from natural arithmetic. */
2866 (if (cmp == GT_EXPR || cmp == GE_EXPR)
2870 real_arithmetic (&c2, MULT_EXPR,
2871 &TREE_REAL_CST (@1), &TREE_REAL_CST (@1));
2872 real_convert (&c2, TYPE_MODE (TREE_TYPE (@0)), &c2);
2874 (if (REAL_VALUE_ISINF (c2))
2875 /* sqrt(x) > y is x == +Inf, when y is very large. */
2876 (if (HONOR_INFINITIES (@0))
2877 (eq @0 { build_real (TREE_TYPE (@0), c2); })
2878 { constant_boolean_node (false, type); })
2879 /* sqrt(x) > c is the same as x > c*c. */
2880 (cmp @0 { build_real (TREE_TYPE (@0), c2); }))))
2881 (if (cmp == LT_EXPR || cmp == LE_EXPR)
2885 real_arithmetic (&c2, MULT_EXPR,
2886 &TREE_REAL_CST (@1), &TREE_REAL_CST (@1));
2887 real_convert (&c2, TYPE_MODE (TREE_TYPE (@0)), &c2);
2889 (if (REAL_VALUE_ISINF (c2))
2891 /* sqrt(x) < y is always true, when y is a very large
2892 value and we don't care about NaNs or Infinities. */
2893 (if (! HONOR_NANS (@0) && ! HONOR_INFINITIES (@0))
2894 { constant_boolean_node (true, type); })
2895 /* sqrt(x) < y is x != +Inf when y is very large and we
2896 don't care about NaNs. */
2897 (if (! HONOR_NANS (@0))
2898 (ne @0 { build_real (TREE_TYPE (@0), c2); }))
2899 /* sqrt(x) < y is x >= 0 when y is very large and we
2900 don't care about Infinities. */
2901 (if (! HONOR_INFINITIES (@0))
2902 (ge @0 { build_real (TREE_TYPE (@0), dconst0); }))
2903 /* sqrt(x) < y is x >= 0 && x != +Inf, when y is large. */
2906 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
2907 (ne @0 { build_real (TREE_TYPE (@0), c2); }))))
2908 /* sqrt(x) < c is the same as x < c*c, if we ignore NaNs. */
2909 (if (! HONOR_NANS (@0))
2910 (cmp @0 { build_real (TREE_TYPE (@0), c2); })
2911 /* sqrt(x) < c is the same as x >= 0 && x < c*c. */
2914 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
2915 (cmp @0 { build_real (TREE_TYPE (@0), c2); })))))))))
2916 /* Transform sqrt(x) cmp sqrt(y) -> x cmp y. */
2918 (cmp (sq @0) (sq @1))
2919 (if (! HONOR_NANS (@0))
2922 /* Optimize various special cases of (FTYPE) N CMP CST. */
2923 (for cmp (lt le eq ne ge gt)
2924 icmp (le le eq ne ge ge)
2926 (cmp (float @0) REAL_CST@1)
2927 (if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (@1))
2928 && ! DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1)))
2931 tree itype = TREE_TYPE (@0);
2932 signop isign = TYPE_SIGN (itype);
2933 format_helper fmt (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (@1))));
2934 const REAL_VALUE_TYPE *cst = TREE_REAL_CST_PTR (@1);
2935 /* Be careful to preserve any potential exceptions due to
2936 NaNs. qNaNs are ok in == or != context.
2937 TODO: relax under -fno-trapping-math or
2938 -fno-signaling-nans. */
2940 = real_isnan (cst) && (cst->signalling
2941 || (cmp != EQ_EXPR && cmp != NE_EXPR));
2942 /* INT?_MIN is power-of-two so it takes
2943 only one mantissa bit. */
2944 bool signed_p = isign == SIGNED;
2945 bool itype_fits_ftype_p
2946 = TYPE_PRECISION (itype) - signed_p <= significand_size (fmt);
2948 /* TODO: allow non-fitting itype and SNaNs when
2949 -fno-trapping-math. */
2950 (if (itype_fits_ftype_p && ! exception_p)
2953 REAL_VALUE_TYPE imin, imax;
2954 real_from_integer (&imin, fmt, wi::min_value (itype), isign);
2955 real_from_integer (&imax, fmt, wi::max_value (itype), isign);
2957 REAL_VALUE_TYPE icst;
2958 if (cmp == GT_EXPR || cmp == GE_EXPR)
2959 real_ceil (&icst, fmt, cst);
2960 else if (cmp == LT_EXPR || cmp == LE_EXPR)
2961 real_floor (&icst, fmt, cst);
2963 real_trunc (&icst, fmt, cst);
2965 bool cst_int_p = !real_isnan (cst) && real_identical (&icst, cst);
2967 bool overflow_p = false;
2969 = real_to_integer (&icst, &overflow_p, TYPE_PRECISION (itype));
2972 /* Optimize cases when CST is outside of ITYPE's range. */
2973 (if (real_compare (LT_EXPR, cst, &imin))
2974 { constant_boolean_node (cmp == GT_EXPR || cmp == GE_EXPR || cmp == NE_EXPR,
2976 (if (real_compare (GT_EXPR, cst, &imax))
2977 { constant_boolean_node (cmp == LT_EXPR || cmp == LE_EXPR || cmp == NE_EXPR,
2979 /* Remove cast if CST is an integer representable by ITYPE. */
2981 (cmp @0 { gcc_assert (!overflow_p);
2982 wide_int_to_tree (itype, icst_val); })
2984 /* When CST is fractional, optimize
2985 (FTYPE) N == CST -> 0
2986 (FTYPE) N != CST -> 1. */
2987 (if (cmp == EQ_EXPR || cmp == NE_EXPR)
2988 { constant_boolean_node (cmp == NE_EXPR, type); })
2989 /* Otherwise replace with sensible integer constant. */
2992 gcc_checking_assert (!overflow_p);
2994 (icmp @0 { wide_int_to_tree (itype, icst_val); })))))))))
2996 /* Fold A /[ex] B CMP C to A CMP B * C. */
2999 (cmp (exact_div @0 @1) INTEGER_CST@2)
3000 (if (!integer_zerop (@1))
3001 (if (wi::eq_p (@2, 0))
3003 (if (TREE_CODE (@1) == INTEGER_CST)
3007 wide_int prod = wi::mul (@2, @1, TYPE_SIGN (TREE_TYPE (@1)), &ovf);
3010 { constant_boolean_node (cmp == NE_EXPR, type); }
3011 (cmp @0 { wide_int_to_tree (TREE_TYPE (@0), prod); }))))))))
3012 (for cmp (lt le gt ge)
3014 (cmp (exact_div @0 INTEGER_CST@1) INTEGER_CST@2)
3015 (if (wi::gt_p (@1, 0, TYPE_SIGN (TREE_TYPE (@1))))
3019 wide_int prod = wi::mul (@2, @1, TYPE_SIGN (TREE_TYPE (@1)), &ovf);
3022 { constant_boolean_node (wi::lt_p (@2, 0, TYPE_SIGN (TREE_TYPE (@2)))
3023 != (cmp == LT_EXPR || cmp == LE_EXPR), type); }
3024 (cmp @0 { wide_int_to_tree (TREE_TYPE (@0), prod); }))))))
3026 /* Unordered tests if either argument is a NaN. */
3028 (bit_ior (unordered @0 @0) (unordered @1 @1))
3029 (if (types_match (@0, @1))
3032 (bit_and (ordered @0 @0) (ordered @1 @1))
3033 (if (types_match (@0, @1))
3036 (bit_ior:c (unordered @0 @0) (unordered:c@2 @0 @1))
3039 (bit_and:c (ordered @0 @0) (ordered:c@2 @0 @1))
3042 /* Simple range test simplifications. */
3043 /* A < B || A >= B -> true. */
3044 (for test1 (lt le le le ne ge)
3045 test2 (ge gt ge ne eq ne)
3047 (bit_ior:c (test1 @0 @1) (test2 @0 @1))
3048 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
3049 || VECTOR_INTEGER_TYPE_P (TREE_TYPE (@0)))
3050 { constant_boolean_node (true, type); })))
3051 /* A < B && A >= B -> false. */
3052 (for test1 (lt lt lt le ne eq)
3053 test2 (ge gt eq gt eq gt)
3055 (bit_and:c (test1 @0 @1) (test2 @0 @1))
3056 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
3057 || VECTOR_INTEGER_TYPE_P (TREE_TYPE (@0)))
3058 { constant_boolean_node (false, type); })))
3060 /* A & (2**N - 1) <= 2**K - 1 -> A & (2**N - 2**K) == 0
3061 A & (2**N - 1) > 2**K - 1 -> A & (2**N - 2**K) != 0
3063 Note that comparisons
3064 A & (2**N - 1) < 2**K -> A & (2**N - 2**K) == 0
3065 A & (2**N - 1) >= 2**K -> A & (2**N - 2**K) != 0
3066 will be canonicalized to above so there's no need to
3073 (cmp (bit_and@0 @1 INTEGER_CST@2) INTEGER_CST@3)
3074 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)))
3077 tree ty = TREE_TYPE (@0);
3078 unsigned prec = TYPE_PRECISION (ty);
3079 wide_int mask = wi::to_wide (@2, prec);
3080 wide_int rhs = wi::to_wide (@3, prec);
3081 signop sgn = TYPE_SIGN (ty);
3083 (if ((mask & (mask + 1)) == 0 && wi::gt_p (rhs, 0, sgn)
3084 && (rhs & (rhs + 1)) == 0 && wi::ge_p (mask, rhs, sgn))
3085 (eqcmp (bit_and @1 { wide_int_to_tree (ty, mask - rhs); })
3086 { build_zero_cst (ty); }))))))
3088 /* -A CMP -B -> B CMP A. */
3089 (for cmp (tcc_comparison)
3090 scmp (swapped_tcc_comparison)
3092 (cmp (negate @0) (negate @1))
3093 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
3094 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3095 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
3098 (cmp (negate @0) CONSTANT_CLASS_P@1)
3099 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
3100 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3101 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
3102 (with { tree tem = const_unop (NEGATE_EXPR, TREE_TYPE (@0), @1); }
3103 (if (tem && !TREE_OVERFLOW (tem))
3104 (scmp @0 { tem; }))))))
3106 /* Convert ABS_EXPR<x> == 0 or ABS_EXPR<x> != 0 to x == 0 or x != 0. */
3109 (op (abs @0) zerop@1)
3112 /* From fold_sign_changed_comparison and fold_widened_comparison. */
3113 (for cmp (simple_comparison)
3115 (cmp (convert@0 @00) (convert?@1 @10))
3116 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
3117 /* Disable this optimization if we're casting a function pointer
3118 type on targets that require function pointer canonicalization. */
3119 && !(targetm.have_canonicalize_funcptr_for_compare ()
3120 && TREE_CODE (TREE_TYPE (@00)) == POINTER_TYPE
3121 && TREE_CODE (TREE_TYPE (TREE_TYPE (@00))) == FUNCTION_TYPE)
3123 (if (TYPE_PRECISION (TREE_TYPE (@00)) == TYPE_PRECISION (TREE_TYPE (@0))
3124 && (TREE_CODE (@10) == INTEGER_CST
3125 || (@1 != @10 && types_match (TREE_TYPE (@10), TREE_TYPE (@00))))
3126 && (TYPE_UNSIGNED (TREE_TYPE (@00)) == TYPE_UNSIGNED (TREE_TYPE (@0))
3129 && (POINTER_TYPE_P (TREE_TYPE (@00)) == POINTER_TYPE_P (TREE_TYPE (@0))))
3130 /* ??? The special-casing of INTEGER_CST conversion was in the original
3131 code and here to avoid a spurious overflow flag on the resulting
3132 constant which fold_convert produces. */
3133 (if (TREE_CODE (@1) == INTEGER_CST)
3134 (cmp @00 { force_fit_type (TREE_TYPE (@00), wi::to_widest (@1), 0,
3135 TREE_OVERFLOW (@1)); })
3136 (cmp @00 (convert @1)))
3138 (if (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (TREE_TYPE (@00)))
3139 /* If possible, express the comparison in the shorter mode. */
3140 (if ((cmp == EQ_EXPR || cmp == NE_EXPR
3141 || TYPE_UNSIGNED (TREE_TYPE (@0)) == TYPE_UNSIGNED (TREE_TYPE (@00))
3142 || (!TYPE_UNSIGNED (TREE_TYPE (@0))
3143 && TYPE_UNSIGNED (TREE_TYPE (@00))))
3144 && (types_match (TREE_TYPE (@10), TREE_TYPE (@00))
3145 || ((TYPE_PRECISION (TREE_TYPE (@00))
3146 >= TYPE_PRECISION (TREE_TYPE (@10)))
3147 && (TYPE_UNSIGNED (TREE_TYPE (@00))
3148 == TYPE_UNSIGNED (TREE_TYPE (@10))))
3149 || (TREE_CODE (@10) == INTEGER_CST
3150 && INTEGRAL_TYPE_P (TREE_TYPE (@00))
3151 && int_fits_type_p (@10, TREE_TYPE (@00)))))
3152 (cmp @00 (convert @10))
3153 (if (TREE_CODE (@10) == INTEGER_CST
3154 && INTEGRAL_TYPE_P (TREE_TYPE (@00))
3155 && !int_fits_type_p (@10, TREE_TYPE (@00)))
3158 tree min = lower_bound_in_type (TREE_TYPE (@10), TREE_TYPE (@00));
3159 tree max = upper_bound_in_type (TREE_TYPE (@10), TREE_TYPE (@00));
3160 bool above = integer_nonzerop (const_binop (LT_EXPR, type, max, @10));
3161 bool below = integer_nonzerop (const_binop (LT_EXPR, type, @10, min));
3163 (if (above || below)
3164 (if (cmp == EQ_EXPR || cmp == NE_EXPR)
3165 { constant_boolean_node (cmp == EQ_EXPR ? false : true, type); }
3166 (if (cmp == LT_EXPR || cmp == LE_EXPR)
3167 { constant_boolean_node (above ? true : false, type); }
3168 (if (cmp == GT_EXPR || cmp == GE_EXPR)
3169 { constant_boolean_node (above ? false : true, type); }))))))))))))
3172 /* A local variable can never be pointed to by
3173 the default SSA name of an incoming parameter.
3174 SSA names are canonicalized to 2nd place. */
3176 (cmp addr@0 SSA_NAME@1)
3177 (if (SSA_NAME_IS_DEFAULT_DEF (@1)
3178 && TREE_CODE (SSA_NAME_VAR (@1)) == PARM_DECL)
3179 (with { tree base = get_base_address (TREE_OPERAND (@0, 0)); }
3180 (if (TREE_CODE (base) == VAR_DECL
3181 && auto_var_in_fn_p (base, current_function_decl))
3182 (if (cmp == NE_EXPR)
3183 { constant_boolean_node (true, type); }
3184 { constant_boolean_node (false, type); }))))))
3186 /* Equality compare simplifications from fold_binary */
3189 /* If we have (A | C) == D where C & ~D != 0, convert this into 0.
3190 Similarly for NE_EXPR. */
3192 (cmp (convert?@3 (bit_ior @0 INTEGER_CST@1)) INTEGER_CST@2)
3193 (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0))
3194 && wi::bit_and_not (@1, @2) != 0)
3195 { constant_boolean_node (cmp == NE_EXPR, type); }))
3197 /* (X ^ Y) == 0 becomes X == Y, and (X ^ Y) != 0 becomes X != Y. */
3199 (cmp (bit_xor @0 @1) integer_zerop)
3202 /* (X ^ Y) == Y becomes X == 0.
3203 Likewise (X ^ Y) == X becomes Y == 0. */
3205 (cmp:c (bit_xor:c @0 @1) @0)
3206 (cmp @1 { build_zero_cst (TREE_TYPE (@1)); }))
3208 /* (X ^ C1) op C2 can be rewritten as X op (C1 ^ C2). */
3210 (cmp (convert?@3 (bit_xor @0 INTEGER_CST@1)) INTEGER_CST@2)
3211 (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0)))
3212 (cmp @0 (bit_xor @1 (convert @2)))))
3215 (cmp (convert? addr@0) integer_zerop)
3216 (if (tree_single_nonzero_warnv_p (@0, NULL))
3217 { constant_boolean_node (cmp == NE_EXPR, type); })))
3219 /* If we have (A & C) == C where C is a power of 2, convert this into
3220 (A & C) != 0. Similarly for NE_EXPR. */
3224 (cmp (bit_and@2 @0 integer_pow2p@1) @1)
3225 (icmp @2 { build_zero_cst (TREE_TYPE (@0)); })))
3227 /* If we have (A & C) != 0 ? D : 0 where C and D are powers of 2,
3228 convert this into a shift followed by ANDing with D. */
3231 (ne (bit_and @0 integer_pow2p@1) integer_zerop)
3232 integer_pow2p@2 integer_zerop)
3234 int shift = wi::exact_log2 (@2) - wi::exact_log2 (@1);
3238 (lshift (convert @0) { build_int_cst (integer_type_node, shift); }) @2)
3240 (convert (rshift @0 { build_int_cst (integer_type_node, -shift); })) @2))))
3242 /* If we have (A & C) != 0 where C is the sign bit of A, convert
3243 this into A < 0. Similarly for (A & C) == 0 into A >= 0. */
3247 (cmp (bit_and (convert?@2 @0) integer_pow2p@1) integer_zerop)
3248 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
3249 && type_has_mode_precision_p (TREE_TYPE (@0))
3250 && element_precision (@2) >= element_precision (@0)
3251 && wi::only_sign_bit_p (@1, element_precision (@0)))
3252 (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
3253 (ncmp (convert:stype @0) { build_zero_cst (stype); })))))
3255 /* If we have A < 0 ? C : 0 where C is a power of 2, convert
3256 this into a right shift or sign extension followed by ANDing with C. */
3259 (lt @0 integer_zerop)
3260 integer_pow2p@1 integer_zerop)
3261 (if (!TYPE_UNSIGNED (TREE_TYPE (@0)))
3263 int shift = element_precision (@0) - wi::exact_log2 (@1) - 1;
3267 (convert (rshift @0 { build_int_cst (integer_type_node, shift); }))
3269 /* Otherwise ctype must be wider than TREE_TYPE (@0) and pure
3270 sign extension followed by AND with C will achieve the effect. */
3271 (bit_and (convert @0) @1)))))
3273 /* When the addresses are not directly of decls compare base and offset.
3274 This implements some remaining parts of fold_comparison address
3275 comparisons but still no complete part of it. Still it is good
3276 enough to make fold_stmt not regress when not dispatching to fold_binary. */
3277 (for cmp (simple_comparison)
3279 (cmp (convert1?@2 addr@0) (convert2? addr@1))
3282 HOST_WIDE_INT off0, off1;
3283 tree base0 = get_addr_base_and_unit_offset (TREE_OPERAND (@0, 0), &off0);
3284 tree base1 = get_addr_base_and_unit_offset (TREE_OPERAND (@1, 0), &off1);
3285 if (base0 && TREE_CODE (base0) == MEM_REF)
3287 off0 += mem_ref_offset (base0).to_short_addr ();
3288 base0 = TREE_OPERAND (base0, 0);
3290 if (base1 && TREE_CODE (base1) == MEM_REF)
3292 off1 += mem_ref_offset (base1).to_short_addr ();
3293 base1 = TREE_OPERAND (base1, 0);
3296 (if (base0 && base1)
3300 /* Punt in GENERIC on variables with value expressions;
3301 the value expressions might point to fields/elements
3302 of other vars etc. */
3304 && ((VAR_P (base0) && DECL_HAS_VALUE_EXPR_P (base0))
3305 || (VAR_P (base1) && DECL_HAS_VALUE_EXPR_P (base1))))
3307 else if (decl_in_symtab_p (base0)
3308 && decl_in_symtab_p (base1))
3309 equal = symtab_node::get_create (base0)
3310 ->equal_address_to (symtab_node::get_create (base1));
3311 else if ((DECL_P (base0)
3312 || TREE_CODE (base0) == SSA_NAME
3313 || TREE_CODE (base0) == STRING_CST)
3315 || TREE_CODE (base1) == SSA_NAME
3316 || TREE_CODE (base1) == STRING_CST))
3317 equal = (base0 == base1);
3321 (if (cmp == EQ_EXPR)
3322 { constant_boolean_node (off0 == off1, type); })
3323 (if (cmp == NE_EXPR)
3324 { constant_boolean_node (off0 != off1, type); })
3325 (if (cmp == LT_EXPR)
3326 { constant_boolean_node (off0 < off1, type); })
3327 (if (cmp == LE_EXPR)
3328 { constant_boolean_node (off0 <= off1, type); })
3329 (if (cmp == GE_EXPR)
3330 { constant_boolean_node (off0 >= off1, type); })
3331 (if (cmp == GT_EXPR)
3332 { constant_boolean_node (off0 > off1, type); }))
3334 && DECL_P (base0) && DECL_P (base1)
3335 /* If we compare this as integers require equal offset. */
3336 && (!INTEGRAL_TYPE_P (TREE_TYPE (@2))
3339 (if (cmp == EQ_EXPR)
3340 { constant_boolean_node (false, type); })
3341 (if (cmp == NE_EXPR)
3342 { constant_boolean_node (true, type); })))))))))
3344 /* Simplify pointer equality compares using PTA. */
3348 (if (POINTER_TYPE_P (TREE_TYPE (@0))
3349 && ptrs_compare_unequal (@0, @1))
3350 { neeq == EQ_EXPR ? boolean_false_node : boolean_true_node; })))
3352 /* PR70920: Transform (intptr_t)x eq/ne CST to x eq/ne (typeof x) CST.
3353 and (typeof ptr_cst) x eq/ne ptr_cst to x eq/ne (typeof x) CST.
3354 Disable the transform if either operand is pointer to function.
3355 This broke pr22051-2.c for arm where function pointer
3356 canonicalizaion is not wanted. */
3360 (cmp (convert @0) INTEGER_CST@1)
3361 (if ((POINTER_TYPE_P (TREE_TYPE (@0)) && !FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@0)))
3362 && INTEGRAL_TYPE_P (TREE_TYPE (@1)))
3363 || (INTEGRAL_TYPE_P (TREE_TYPE (@0)) && POINTER_TYPE_P (TREE_TYPE (@1))
3364 && !FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@1)))))
3365 (cmp @0 (convert @1)))))
3367 /* Non-equality compare simplifications from fold_binary */
3368 (for cmp (lt gt le ge)
3369 /* Comparisons with the highest or lowest possible integer of
3370 the specified precision will have known values. */
3372 (cmp (convert?@2 @0) INTEGER_CST@1)
3373 (if ((INTEGRAL_TYPE_P (TREE_TYPE (@1)) || POINTER_TYPE_P (TREE_TYPE (@1)))
3374 && tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@0)))
3377 tree arg1_type = TREE_TYPE (@1);
3378 unsigned int prec = TYPE_PRECISION (arg1_type);
3379 wide_int max = wi::max_value (arg1_type);
3380 wide_int signed_max = wi::max_value (prec, SIGNED);
3381 wide_int min = wi::min_value (arg1_type);
3384 (if (wi::eq_p (@1, max))
3386 (if (cmp == GT_EXPR)
3387 { constant_boolean_node (false, type); })
3388 (if (cmp == GE_EXPR)
3390 (if (cmp == LE_EXPR)
3391 { constant_boolean_node (true, type); })
3392 (if (cmp == LT_EXPR)
3394 (if (wi::eq_p (@1, min))
3396 (if (cmp == LT_EXPR)
3397 { constant_boolean_node (false, type); })
3398 (if (cmp == LE_EXPR)
3400 (if (cmp == GE_EXPR)
3401 { constant_boolean_node (true, type); })
3402 (if (cmp == GT_EXPR)
3404 (if (wi::eq_p (@1, max - 1))
3406 (if (cmp == GT_EXPR)
3407 (eq @2 { wide_int_to_tree (TREE_TYPE (@1), wi::add (@1, 1)); }))
3408 (if (cmp == LE_EXPR)
3409 (ne @2 { wide_int_to_tree (TREE_TYPE (@1), wi::add (@1, 1)); }))))
3410 (if (wi::eq_p (@1, min + 1))
3412 (if (cmp == GE_EXPR)
3413 (ne @2 { wide_int_to_tree (TREE_TYPE (@1), wi::sub (@1, 1)); }))
3414 (if (cmp == LT_EXPR)
3415 (eq @2 { wide_int_to_tree (TREE_TYPE (@1), wi::sub (@1, 1)); }))))
3416 (if (wi::eq_p (@1, signed_max)
3417 && TYPE_UNSIGNED (arg1_type)
3418 /* We will flip the signedness of the comparison operator
3419 associated with the mode of @1, so the sign bit is
3420 specified by this mode. Check that @1 is the signed
3421 max associated with this sign bit. */
3422 && prec == GET_MODE_PRECISION (SCALAR_INT_TYPE_MODE (arg1_type))
3423 /* signed_type does not work on pointer types. */
3424 && INTEGRAL_TYPE_P (arg1_type))
3425 /* The following case also applies to X < signed_max+1
3426 and X >= signed_max+1 because previous transformations. */
3427 (if (cmp == LE_EXPR || cmp == GT_EXPR)
3428 (with { tree st = signed_type_for (arg1_type); }
3429 (if (cmp == LE_EXPR)
3430 (ge (convert:st @0) { build_zero_cst (st); })
3431 (lt (convert:st @0) { build_zero_cst (st); }))))))))))
3433 (for cmp (unordered ordered unlt unle ungt unge uneq ltgt)
3434 /* If the second operand is NaN, the result is constant. */
3437 (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
3438 && (cmp != LTGT_EXPR || ! flag_trapping_math))
3439 { constant_boolean_node (cmp == ORDERED_EXPR || cmp == LTGT_EXPR
3440 ? false : true, type); })))
3442 /* bool_var != 0 becomes bool_var. */
3444 (ne @0 integer_zerop)
3445 (if (TREE_CODE (TREE_TYPE (@0)) == BOOLEAN_TYPE
3446 && types_match (type, TREE_TYPE (@0)))
3448 /* bool_var == 1 becomes bool_var. */
3450 (eq @0 integer_onep)
3451 (if (TREE_CODE (TREE_TYPE (@0)) == BOOLEAN_TYPE
3452 && types_match (type, TREE_TYPE (@0)))
3455 bool_var == 0 becomes !bool_var or
3456 bool_var != 1 becomes !bool_var
3457 here because that only is good in assignment context as long
3458 as we require a tcc_comparison in GIMPLE_CONDs where we'd
3459 replace if (x == 0) with tem = ~x; if (tem != 0) which is
3460 clearly less optimal and which we'll transform again in forwprop. */
3462 /* When one argument is a constant, overflow detection can be simplified.
3463 Currently restricted to single use so as not to interfere too much with
3464 ADD_OVERFLOW detection in tree-ssa-math-opts.c.
3465 A + CST CMP A -> A CMP' CST' */
3466 (for cmp (lt le ge gt)
3469 (cmp:c (plus@2 @0 INTEGER_CST@1) @0)
3470 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
3471 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))
3474 (out @0 { wide_int_to_tree (TREE_TYPE (@0), wi::max_value
3475 (TYPE_PRECISION (TREE_TYPE (@0)), UNSIGNED) - @1); }))))
3477 /* To detect overflow in unsigned A - B, A < B is simpler than A - B > A.
3478 However, the detection logic for SUB_OVERFLOW in tree-ssa-math-opts.c
3479 expects the long form, so we restrict the transformation for now. */
3482 (cmp:c (minus@2 @0 @1) @0)
3483 (if (single_use (@2)
3484 && ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3485 && TYPE_UNSIGNED (TREE_TYPE (@0))
3486 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
3489 /* Testing for overflow is unnecessary if we already know the result. */
3494 (cmp:c (realpart (IFN_SUB_OVERFLOW@2 @0 @1)) @0)
3495 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
3496 && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
3497 (out (imagpart @2) { build_zero_cst (TREE_TYPE (@0)); }))))
3502 (cmp:c (realpart (IFN_ADD_OVERFLOW:c@2 @0 @1)) @0)
3503 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
3504 && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
3505 (out (imagpart @2) { build_zero_cst (TREE_TYPE (@0)); }))))
3507 /* For unsigned operands, -1 / B < A checks whether A * B would overflow.
3508 Simplify it to __builtin_mul_overflow (A, B, <unused>). */
3512 (cmp:c (trunc_div:s integer_all_onesp @1) @0)
3513 (if (TYPE_UNSIGNED (TREE_TYPE (@0)) && !VECTOR_TYPE_P (TREE_TYPE (@0)))
3514 (with { tree t = TREE_TYPE (@0), cpx = build_complex_type (t); }
3515 (out (imagpart (IFN_MUL_OVERFLOW:cpx @0 @1)) { build_zero_cst (t); })))))
3517 /* Simplification of math builtins. These rules must all be optimizations
3518 as well as IL simplifications. If there is a possibility that the new
3519 form could be a pessimization, the rule should go in the canonicalization
3520 section that follows this one.
3522 Rules can generally go in this section if they satisfy one of
3525 - the rule describes an identity
3527 - the rule replaces calls with something as simple as addition or
3530 - the rule contains unary calls only and simplifies the surrounding
3531 arithmetic. (The idea here is to exclude non-unary calls in which
3532 one operand is constant and in which the call is known to be cheap
3533 when the operand has that value.) */
3535 (if (flag_unsafe_math_optimizations)
3536 /* Simplify sqrt(x) * sqrt(x) -> x. */
3538 (mult (SQRT@1 @0) @1)
3539 (if (!HONOR_SNANS (type))
3542 (for op (plus minus)
3543 /* Simplify (A / C) +- (B / C) -> (A +- B) / C. */
3547 (rdiv (op @0 @2) @1)))
3549 /* Simplify sqrt(x) * sqrt(y) -> sqrt(x*y). */
3550 (for root (SQRT CBRT)
3552 (mult (root:s @0) (root:s @1))
3553 (root (mult @0 @1))))
3555 /* Simplify expN(x) * expN(y) -> expN(x+y). */
3556 (for exps (EXP EXP2 EXP10 POW10)
3558 (mult (exps:s @0) (exps:s @1))
3559 (exps (plus @0 @1))))
3561 /* Simplify a/root(b/c) into a*root(c/b). */
3562 (for root (SQRT CBRT)
3564 (rdiv @0 (root:s (rdiv:s @1 @2)))
3565 (mult @0 (root (rdiv @2 @1)))))
3567 /* Simplify x/expN(y) into x*expN(-y). */
3568 (for exps (EXP EXP2 EXP10 POW10)
3570 (rdiv @0 (exps:s @1))
3571 (mult @0 (exps (negate @1)))))
3573 (for logs (LOG LOG2 LOG10 LOG10)
3574 exps (EXP EXP2 EXP10 POW10)
3575 /* logN(expN(x)) -> x. */
3579 /* expN(logN(x)) -> x. */
3584 /* Optimize logN(func()) for various exponential functions. We
3585 want to determine the value "x" and the power "exponent" in
3586 order to transform logN(x**exponent) into exponent*logN(x). */
3587 (for logs (LOG LOG LOG LOG2 LOG2 LOG2 LOG10 LOG10)
3588 exps (EXP2 EXP10 POW10 EXP EXP10 POW10 EXP EXP2)
3591 (if (SCALAR_FLOAT_TYPE_P (type))
3597 /* Prepare to do logN(exp(exponent)) -> exponent*logN(e). */
3598 x = build_real_truncate (type, dconst_e ());
3601 /* Prepare to do logN(exp2(exponent)) -> exponent*logN(2). */
3602 x = build_real (type, dconst2);
3606 /* Prepare to do logN(exp10(exponent)) -> exponent*logN(10). */
3608 REAL_VALUE_TYPE dconst10;
3609 real_from_integer (&dconst10, VOIDmode, 10, SIGNED);
3610 x = build_real (type, dconst10);
3617 (mult (logs { x; }) @0)))))
3625 (if (SCALAR_FLOAT_TYPE_P (type))
3631 /* Prepare to do logN(sqrt(x)) -> 0.5*logN(x). */
3632 x = build_real (type, dconsthalf);
3635 /* Prepare to do logN(cbrt(x)) -> (1/3)*logN(x). */
3636 x = build_real_truncate (type, dconst_third ());
3642 (mult { x; } (logs @0))))))
3644 /* logN(pow(x,exponent)) -> exponent*logN(x). */
3645 (for logs (LOG LOG2 LOG10)
3649 (mult @1 (logs @0))))
3651 /* pow(C,x) -> exp(log(C)*x) if C > 0. */
3656 (pows REAL_CST@0 @1)
3657 (if (real_compare (GT_EXPR, TREE_REAL_CST_PTR (@0), &dconst0)
3658 && real_isfinite (TREE_REAL_CST_PTR (@0)))
3659 (exps (mult (logs @0) @1)))))
3664 exps (EXP EXP2 EXP10 POW10)
3665 /* sqrt(expN(x)) -> expN(x*0.5). */
3668 (exps (mult @0 { build_real (type, dconsthalf); })))
3669 /* cbrt(expN(x)) -> expN(x/3). */
3672 (exps (mult @0 { build_real_truncate (type, dconst_third ()); })))
3673 /* pow(expN(x), y) -> expN(x*y). */
3676 (exps (mult @0 @1))))
3678 /* tan(atan(x)) -> x. */
3685 /* cabs(x+0i) or cabs(0+xi) -> abs(x). */
3687 (CABS (complex:C @0 real_zerop@1))
3690 /* trunc(trunc(x)) -> trunc(x), etc. */
3691 (for fns (TRUNC FLOOR CEIL ROUND NEARBYINT RINT)
3695 /* f(x) -> x if x is integer valued and f does nothing for such values. */
3696 (for fns (TRUNC FLOOR CEIL ROUND NEARBYINT RINT)
3698 (fns integer_valued_real_p@0)
3701 /* hypot(x,0) and hypot(0,x) -> abs(x). */
3703 (HYPOT:c @0 real_zerop@1)
3706 /* pow(1,x) -> 1. */
3708 (POW real_onep@0 @1)
3712 /* copysign(x,x) -> x. */
3717 /* copysign(x,y) -> fabs(x) if y is nonnegative. */
3718 (COPYSIGN @0 tree_expr_nonnegative_p@1)
3721 (for scale (LDEXP SCALBN SCALBLN)
3722 /* ldexp(0, x) -> 0. */
3724 (scale real_zerop@0 @1)
3726 /* ldexp(x, 0) -> x. */
3728 (scale @0 integer_zerop@1)
3730 /* ldexp(x, y) -> x if x is +-Inf or NaN. */
3732 (scale REAL_CST@0 @1)
3733 (if (!real_isfinite (TREE_REAL_CST_PTR (@0)))
3736 /* Canonicalization of sequences of math builtins. These rules represent
3737 IL simplifications but are not necessarily optimizations.
3739 The sincos pass is responsible for picking "optimal" implementations
3740 of math builtins, which may be more complicated and can sometimes go
3741 the other way, e.g. converting pow into a sequence of sqrts.
3742 We only want to do these canonicalizations before the pass has run. */
3744 (if (flag_unsafe_math_optimizations && canonicalize_math_p ())
3745 /* Simplify tan(x) * cos(x) -> sin(x). */
3747 (mult:c (TAN:s @0) (COS:s @0))
3750 /* Simplify x * pow(x,c) -> pow(x,c+1). */
3752 (mult:c @0 (POW:s @0 REAL_CST@1))
3753 (if (!TREE_OVERFLOW (@1))
3754 (POW @0 (plus @1 { build_one_cst (type); }))))
3756 /* Simplify sin(x) / cos(x) -> tan(x). */
3758 (rdiv (SIN:s @0) (COS:s @0))
3761 /* Simplify cos(x) / sin(x) -> 1 / tan(x). */
3763 (rdiv (COS:s @0) (SIN:s @0))
3764 (rdiv { build_one_cst (type); } (TAN @0)))
3766 /* Simplify sin(x) / tan(x) -> cos(x). */
3768 (rdiv (SIN:s @0) (TAN:s @0))
3769 (if (! HONOR_NANS (@0)
3770 && ! HONOR_INFINITIES (@0))
3773 /* Simplify tan(x) / sin(x) -> 1.0 / cos(x). */
3775 (rdiv (TAN:s @0) (SIN:s @0))
3776 (if (! HONOR_NANS (@0)
3777 && ! HONOR_INFINITIES (@0))
3778 (rdiv { build_one_cst (type); } (COS @0))))
3780 /* Simplify pow(x,y) * pow(x,z) -> pow(x,y+z). */
3782 (mult (POW:s @0 @1) (POW:s @0 @2))
3783 (POW @0 (plus @1 @2)))
3785 /* Simplify pow(x,y) * pow(z,y) -> pow(x*z,y). */
3787 (mult (POW:s @0 @1) (POW:s @2 @1))
3788 (POW (mult @0 @2) @1))
3790 /* Simplify powi(x,y) * powi(z,y) -> powi(x*z,y). */
3792 (mult (POWI:s @0 @1) (POWI:s @2 @1))
3793 (POWI (mult @0 @2) @1))
3795 /* Simplify pow(x,c) / x -> pow(x,c-1). */
3797 (rdiv (POW:s @0 REAL_CST@1) @0)
3798 (if (!TREE_OVERFLOW (@1))
3799 (POW @0 (minus @1 { build_one_cst (type); }))))
3801 /* Simplify x / pow (y,z) -> x * pow(y,-z). */
3803 (rdiv @0 (POW:s @1 @2))
3804 (mult @0 (POW @1 (negate @2))))
3809 /* sqrt(sqrt(x)) -> pow(x,1/4). */
3812 (pows @0 { build_real (type, dconst_quarter ()); }))
3813 /* sqrt(cbrt(x)) -> pow(x,1/6). */
3816 (pows @0 { build_real_truncate (type, dconst_sixth ()); }))
3817 /* cbrt(sqrt(x)) -> pow(x,1/6). */
3820 (pows @0 { build_real_truncate (type, dconst_sixth ()); }))
3821 /* cbrt(cbrt(x)) -> pow(x,1/9), iff x is nonnegative. */
3823 (cbrts (cbrts tree_expr_nonnegative_p@0))
3824 (pows @0 { build_real_truncate (type, dconst_ninth ()); }))
3825 /* sqrt(pow(x,y)) -> pow(|x|,y*0.5). */
3827 (sqrts (pows @0 @1))
3828 (pows (abs @0) (mult @1 { build_real (type, dconsthalf); })))
3829 /* cbrt(pow(x,y)) -> pow(x,y/3), iff x is nonnegative. */
3831 (cbrts (pows tree_expr_nonnegative_p@0 @1))
3832 (pows @0 (mult @1 { build_real_truncate (type, dconst_third ()); })))
3833 /* pow(sqrt(x),y) -> pow(x,y*0.5). */
3835 (pows (sqrts @0) @1)
3836 (pows @0 (mult @1 { build_real (type, dconsthalf); })))
3837 /* pow(cbrt(x),y) -> pow(x,y/3) iff x is nonnegative. */
3839 (pows (cbrts tree_expr_nonnegative_p@0) @1)
3840 (pows @0 (mult @1 { build_real_truncate (type, dconst_third ()); })))
3841 /* pow(pow(x,y),z) -> pow(x,y*z) iff x is nonnegative. */
3843 (pows (pows tree_expr_nonnegative_p@0 @1) @2)
3844 (pows @0 (mult @1 @2))))
3846 /* cabs(x+xi) -> fabs(x)*sqrt(2). */
3848 (CABS (complex @0 @0))
3849 (mult (abs @0) { build_real_truncate (type, dconst_sqrt2 ()); }))
3851 /* hypot(x,x) -> fabs(x)*sqrt(2). */
3854 (mult (abs @0) { build_real_truncate (type, dconst_sqrt2 ()); }))
3856 /* cexp(x+yi) -> exp(x)*cexpi(y). */
3861 (cexps compositional_complex@0)
3862 (if (targetm.libc_has_function (function_c99_math_complex))
3864 (mult (exps@1 (realpart @0)) (realpart (cexpis:type@2 (imagpart @0))))
3865 (mult @1 (imagpart @2)))))))
3867 (if (canonicalize_math_p ())
3868 /* floor(x) -> trunc(x) if x is nonnegative. */
3872 (floors tree_expr_nonnegative_p@0)
3875 (match double_value_p
3877 (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == double_type_node)))
3878 (for froms (BUILT_IN_TRUNCL
3890 /* truncl(extend(x)) -> extend(trunc(x)), etc., if x is a double. */
3891 (if (optimize && canonicalize_math_p ())
3893 (froms (convert double_value_p@0))
3894 (convert (tos @0)))))
3896 (match float_value_p
3898 (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == float_type_node)))
3899 (for froms (BUILT_IN_TRUNCL BUILT_IN_TRUNC
3900 BUILT_IN_FLOORL BUILT_IN_FLOOR
3901 BUILT_IN_CEILL BUILT_IN_CEIL
3902 BUILT_IN_ROUNDL BUILT_IN_ROUND
3903 BUILT_IN_NEARBYINTL BUILT_IN_NEARBYINT
3904 BUILT_IN_RINTL BUILT_IN_RINT)
3905 tos (BUILT_IN_TRUNCF BUILT_IN_TRUNCF
3906 BUILT_IN_FLOORF BUILT_IN_FLOORF
3907 BUILT_IN_CEILF BUILT_IN_CEILF
3908 BUILT_IN_ROUNDF BUILT_IN_ROUNDF
3909 BUILT_IN_NEARBYINTF BUILT_IN_NEARBYINTF
3910 BUILT_IN_RINTF BUILT_IN_RINTF)
3911 /* truncl(extend(x)) and trunc(extend(x)) -> extend(truncf(x)), etc.,
3913 (if (optimize && canonicalize_math_p ()
3914 && targetm.libc_has_function (function_c99_misc))
3916 (froms (convert float_value_p@0))
3917 (convert (tos @0)))))
3919 (for froms (XFLOORL XCEILL XROUNDL XRINTL)
3920 tos (XFLOOR XCEIL XROUND XRINT)
3921 /* llfloorl(extend(x)) -> llfloor(x), etc., if x is a double. */
3922 (if (optimize && canonicalize_math_p ())
3924 (froms (convert double_value_p@0))
3927 (for froms (XFLOORL XCEILL XROUNDL XRINTL
3928 XFLOOR XCEIL XROUND XRINT)
3929 tos (XFLOORF XCEILF XROUNDF XRINTF)
3930 /* llfloorl(extend(x)) and llfloor(extend(x)) -> llfloorf(x), etc.,
3932 (if (optimize && canonicalize_math_p ())
3934 (froms (convert float_value_p@0))
3937 (if (canonicalize_math_p ())
3938 /* xfloor(x) -> fix_trunc(x) if x is nonnegative. */
3939 (for floors (IFLOOR LFLOOR LLFLOOR)
3941 (floors tree_expr_nonnegative_p@0)
3944 (if (canonicalize_math_p ())
3945 /* xfloor(x) -> fix_trunc(x), etc., if x is integer valued. */
3946 (for fns (IFLOOR LFLOOR LLFLOOR
3948 IROUND LROUND LLROUND)
3950 (fns integer_valued_real_p@0)
3952 (if (!flag_errno_math)
3953 /* xrint(x) -> fix_trunc(x), etc., if x is integer valued. */
3954 (for rints (IRINT LRINT LLRINT)
3956 (rints integer_valued_real_p@0)
3959 (if (canonicalize_math_p ())
3960 (for ifn (IFLOOR ICEIL IROUND IRINT)
3961 lfn (LFLOOR LCEIL LROUND LRINT)
3962 llfn (LLFLOOR LLCEIL LLROUND LLRINT)
3963 /* Canonicalize iround (x) to lround (x) on ILP32 targets where
3964 sizeof (int) == sizeof (long). */
3965 (if (TYPE_PRECISION (integer_type_node)
3966 == TYPE_PRECISION (long_integer_type_node))
3969 (lfn:long_integer_type_node @0)))
3970 /* Canonicalize llround (x) to lround (x) on LP64 targets where
3971 sizeof (long long) == sizeof (long). */
3972 (if (TYPE_PRECISION (long_long_integer_type_node)
3973 == TYPE_PRECISION (long_integer_type_node))
3976 (lfn:long_integer_type_node @0)))))
3978 /* cproj(x) -> x if we're ignoring infinities. */
3981 (if (!HONOR_INFINITIES (type))
3984 /* If the real part is inf and the imag part is known to be
3985 nonnegative, return (inf + 0i). */
3987 (CPROJ (complex REAL_CST@0 tree_expr_nonnegative_p@1))
3988 (if (real_isinf (TREE_REAL_CST_PTR (@0)))
3989 { build_complex_inf (type, false); }))
3991 /* If the imag part is inf, return (inf+I*copysign(0,imag)). */
3993 (CPROJ (complex @0 REAL_CST@1))
3994 (if (real_isinf (TREE_REAL_CST_PTR (@1)))
3995 { build_complex_inf (type, TREE_REAL_CST_PTR (@1)->sign); }))
4001 (pows @0 REAL_CST@1)
4003 const REAL_VALUE_TYPE *value = TREE_REAL_CST_PTR (@1);
4004 REAL_VALUE_TYPE tmp;
4007 /* pow(x,0) -> 1. */
4008 (if (real_equal (value, &dconst0))
4009 { build_real (type, dconst1); })
4010 /* pow(x,1) -> x. */
4011 (if (real_equal (value, &dconst1))
4013 /* pow(x,-1) -> 1/x. */
4014 (if (real_equal (value, &dconstm1))
4015 (rdiv { build_real (type, dconst1); } @0))
4016 /* pow(x,0.5) -> sqrt(x). */
4017 (if (flag_unsafe_math_optimizations
4018 && canonicalize_math_p ()
4019 && real_equal (value, &dconsthalf))
4021 /* pow(x,1/3) -> cbrt(x). */
4022 (if (flag_unsafe_math_optimizations
4023 && canonicalize_math_p ()
4024 && (tmp = real_value_truncate (TYPE_MODE (type), dconst_third ()),
4025 real_equal (value, &tmp)))
4028 /* powi(1,x) -> 1. */
4030 (POWI real_onep@0 @1)
4034 (POWI @0 INTEGER_CST@1)
4036 /* powi(x,0) -> 1. */
4037 (if (wi::eq_p (@1, 0))
4038 { build_real (type, dconst1); })
4039 /* powi(x,1) -> x. */
4040 (if (wi::eq_p (@1, 1))
4042 /* powi(x,-1) -> 1/x. */
4043 (if (wi::eq_p (@1, -1))
4044 (rdiv { build_real (type, dconst1); } @0))))
4046 /* Narrowing of arithmetic and logical operations.
4048 These are conceptually similar to the transformations performed for
4049 the C/C++ front-ends by shorten_binary_op and shorten_compare. Long
4050 term we want to move all that code out of the front-ends into here. */
4052 /* If we have a narrowing conversion of an arithmetic operation where
4053 both operands are widening conversions from the same type as the outer
4054 narrowing conversion. Then convert the innermost operands to a suitable
4055 unsigned type (to avoid introducing undefined behavior), perform the
4056 operation and convert the result to the desired type. */
4057 (for op (plus minus)
4059 (convert (op:s (convert@2 @0) (convert?@3 @1)))
4060 (if (INTEGRAL_TYPE_P (type)
4061 /* We check for type compatibility between @0 and @1 below,
4062 so there's no need to check that @1/@3 are integral types. */
4063 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
4064 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
4065 /* The precision of the type of each operand must match the
4066 precision of the mode of each operand, similarly for the
4068 && type_has_mode_precision_p (TREE_TYPE (@0))
4069 && type_has_mode_precision_p (TREE_TYPE (@1))
4070 && type_has_mode_precision_p (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, type)
4074 && (types_match (@0, @1)
4075 /* Or the second operand is const integer or converted const
4076 integer from valueize. */
4077 || TREE_CODE (@1) == INTEGER_CST))
4078 (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
4079 (op @0 (convert @1))
4080 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
4081 (convert (op (convert:utype @0)
4082 (convert:utype @1))))))))
4084 /* This is another case of narrowing, specifically when there's an outer
4085 BIT_AND_EXPR which masks off bits outside the type of the innermost
4086 operands. Like the previous case we have to convert the operands
4087 to unsigned types to avoid introducing undefined behavior for the
4088 arithmetic operation. */
4089 (for op (minus plus)
4091 (bit_and (op:s (convert@2 @0) (convert@3 @1)) INTEGER_CST@4)
4092 (if (INTEGRAL_TYPE_P (type)
4093 /* We check for type compatibility between @0 and @1 below,
4094 so there's no need to check that @1/@3 are integral types. */
4095 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
4096 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
4097 /* The precision of the type of each operand must match the
4098 precision of the mode of each operand, similarly for the
4100 && type_has_mode_precision_p (TREE_TYPE (@0))
4101 && type_has_mode_precision_p (TREE_TYPE (@1))
4102 && type_has_mode_precision_p (type)
4103 /* The inner conversion must be a widening conversion. */
4104 && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
4105 && types_match (@0, @1)
4106 && (tree_int_cst_min_precision (@4, TYPE_SIGN (TREE_TYPE (@0)))
4107 <= TYPE_PRECISION (TREE_TYPE (@0)))
4108 && (wi::bit_and (@4, wi::mask (TYPE_PRECISION (TREE_TYPE (@0)),
4109 true, TYPE_PRECISION (type))) == 0))
4110 (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
4111 (with { tree ntype = TREE_TYPE (@0); }
4112 (convert (bit_and (op @0 @1) (convert:ntype @4))))
4113 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
4114 (convert (bit_and (op (convert:utype @0) (convert:utype @1))
4115 (convert:utype @4))))))))
4117 /* Transform (@0 < @1 and @0 < @2) to use min,
4118 (@0 > @1 and @0 > @2) to use max */
4119 (for op (lt le gt ge)
4120 ext (min min max max)
4122 (bit_and (op:cs @0 @1) (op:cs @0 @2))
4123 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
4124 && TREE_CODE (@0) != INTEGER_CST)
4125 (op @0 (ext @1 @2)))))
4128 /* signbit(x) -> 0 if x is nonnegative. */
4129 (SIGNBIT tree_expr_nonnegative_p@0)
4130 { integer_zero_node; })
4133 /* signbit(x) -> x<0 if x doesn't have signed zeros. */
4135 (if (!HONOR_SIGNED_ZEROS (@0))
4136 (convert (lt @0 { build_real (TREE_TYPE (@0), dconst0); }))))
4138 /* Transform comparisons of the form X +- C1 CMP C2 to X CMP C2 -+ C1. */
4140 (for op (plus minus)
4143 (cmp (op@3 @0 INTEGER_CST@1) INTEGER_CST@2)
4144 (if (!TREE_OVERFLOW (@1) && !TREE_OVERFLOW (@2)
4145 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0))
4146 && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@0))
4147 && !TYPE_SATURATING (TREE_TYPE (@0)))
4148 (with { tree res = int_const_binop (rop, @2, @1); }
4149 (if (TREE_OVERFLOW (res)
4150 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
4151 { constant_boolean_node (cmp == NE_EXPR, type); }
4152 (if (single_use (@3))
4153 (cmp @0 { res; }))))))))
4154 (for cmp (lt le gt ge)
4155 (for op (plus minus)
4158 (cmp (op@3 @0 INTEGER_CST@1) INTEGER_CST@2)
4159 (if (!TREE_OVERFLOW (@1) && !TREE_OVERFLOW (@2)
4160 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
4161 (with { tree res = int_const_binop (rop, @2, @1); }
4162 (if (TREE_OVERFLOW (res))
4164 fold_overflow_warning (("assuming signed overflow does not occur "
4165 "when simplifying conditional to constant"),
4166 WARN_STRICT_OVERFLOW_CONDITIONAL);
4167 bool less = cmp == LE_EXPR || cmp == LT_EXPR;
4168 /* wi::ges_p (@2, 0) should be sufficient for a signed type. */
4169 bool ovf_high = wi::lt_p (@1, 0, TYPE_SIGN (TREE_TYPE (@1)))
4170 != (op == MINUS_EXPR);
4171 constant_boolean_node (less == ovf_high, type);
4173 (if (single_use (@3))
4176 fold_overflow_warning (("assuming signed overflow does not occur "
4177 "when changing X +- C1 cmp C2 to "
4179 WARN_STRICT_OVERFLOW_COMPARISON);
4181 (cmp @0 { res; })))))))))
4183 /* Canonicalizations of BIT_FIELD_REFs. */
4186 (BIT_FIELD_REF @0 @1 @2)
4188 (if (TREE_CODE (TREE_TYPE (@0)) == COMPLEX_TYPE
4189 && tree_int_cst_equal (@1, TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))))
4191 (if (integer_zerop (@2))
4192 (view_convert (realpart @0)))
4193 (if (tree_int_cst_equal (@2, TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))))
4194 (view_convert (imagpart @0)))))
4195 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
4196 && INTEGRAL_TYPE_P (type)
4197 /* On GIMPLE this should only apply to register arguments. */
4198 && (! GIMPLE || is_gimple_reg (@0))
4199 /* A bit-field-ref that referenced the full argument can be stripped. */
4200 && ((compare_tree_int (@1, TYPE_PRECISION (TREE_TYPE (@0))) == 0
4201 && integer_zerop (@2))
4202 /* Low-parts can be reduced to integral conversions.
4203 ??? The following doesn't work for PDP endian. */
4204 || (BYTES_BIG_ENDIAN == WORDS_BIG_ENDIAN
4205 /* Don't even think about BITS_BIG_ENDIAN. */
4206 && TYPE_PRECISION (TREE_TYPE (@0)) % BITS_PER_UNIT == 0
4207 && TYPE_PRECISION (type) % BITS_PER_UNIT == 0
4208 && compare_tree_int (@2, (BYTES_BIG_ENDIAN
4209 ? (TYPE_PRECISION (TREE_TYPE (@0))
4210 - TYPE_PRECISION (type))
4214 /* Simplify vector extracts. */
4217 (BIT_FIELD_REF CONSTRUCTOR@0 @1 @2)
4218 (if (VECTOR_TYPE_P (TREE_TYPE (@0))
4219 && (types_match (type, TREE_TYPE (TREE_TYPE (@0)))
4220 || (VECTOR_TYPE_P (type)
4221 && types_match (TREE_TYPE (type), TREE_TYPE (TREE_TYPE (@0))))))
4224 tree ctor = (TREE_CODE (@0) == SSA_NAME
4225 ? gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0)) : @0);
4226 tree eltype = TREE_TYPE (TREE_TYPE (ctor));
4227 unsigned HOST_WIDE_INT width = tree_to_uhwi (TYPE_SIZE (eltype));
4228 unsigned HOST_WIDE_INT n = tree_to_uhwi (@1);
4229 unsigned HOST_WIDE_INT idx = tree_to_uhwi (@2);
4232 && (idx % width) == 0
4234 && ((idx + n) / width) <= TYPE_VECTOR_SUBPARTS (TREE_TYPE (ctor)))
4239 /* Constructor elements can be subvectors. */
4240 unsigned HOST_WIDE_INT k = 1;
4241 if (CONSTRUCTOR_NELTS (ctor) != 0)
4243 tree cons_elem = TREE_TYPE (CONSTRUCTOR_ELT (ctor, 0)->value);
4244 if (TREE_CODE (cons_elem) == VECTOR_TYPE)
4245 k = TYPE_VECTOR_SUBPARTS (cons_elem);
4249 /* We keep an exact subset of the constructor elements. */
4250 (if ((idx % k) == 0 && (n % k) == 0)
4251 (if (CONSTRUCTOR_NELTS (ctor) == 0)
4252 { build_constructor (type, NULL); }
4259 (if (idx < CONSTRUCTOR_NELTS (ctor))
4260 { CONSTRUCTOR_ELT (ctor, idx)->value; }
4261 { build_zero_cst (type); })
4263 vec<constructor_elt, va_gc> *vals;
4264 vec_alloc (vals, n);
4265 for (unsigned i = 0;
4266 i < n && idx + i < CONSTRUCTOR_NELTS (ctor); ++i)
4267 CONSTRUCTOR_APPEND_ELT (vals, NULL_TREE,
4268 CONSTRUCTOR_ELT (ctor, idx + i)->value);
4269 build_constructor (type, vals);
4271 /* The bitfield references a single constructor element. */
4272 (if (idx + n <= (idx / k + 1) * k)
4274 (if (CONSTRUCTOR_NELTS (ctor) <= idx / k)
4275 { build_zero_cst (type); })
4277 { CONSTRUCTOR_ELT (ctor, idx / k)->value; })
4278 (BIT_FIELD_REF { CONSTRUCTOR_ELT (ctor, idx / k)->value; }
4279 @1 { bitsize_int ((idx % k) * width); })))))))))
4281 /* Simplify a bit extraction from a bit insertion for the cases with
4282 the inserted element fully covering the extraction or the insertion
4283 not touching the extraction. */
4285 (BIT_FIELD_REF (bit_insert @0 @1 @ipos) @rsize @rpos)
4288 unsigned HOST_WIDE_INT isize;
4289 if (INTEGRAL_TYPE_P (TREE_TYPE (@1)))
4290 isize = TYPE_PRECISION (TREE_TYPE (@1));
4292 isize = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (@1)));
4295 (if (wi::leu_p (@ipos, @rpos)
4296 && wi::leu_p (wi::add (@rpos, @rsize), wi::add (@ipos, isize)))
4297 (BIT_FIELD_REF @1 @rsize { wide_int_to_tree (bitsizetype,
4298 wi::sub (@rpos, @ipos)); }))
4299 (if (wi::geu_p (@ipos, wi::add (@rpos, @rsize))
4300 || wi::geu_p (@rpos, wi::add (@ipos, isize)))
4301 (BIT_FIELD_REF @0 @rsize @rpos)))))