1 /* Match-and-simplify patterns for shared GENERIC and GIMPLE folding.
2 This file is consumed by genmatch which produces gimple-match.c
3 and generic-match.c from it.
5 Copyright (C) 2014-2017 Free Software Foundation, Inc.
6 Contributed by Richard Biener <rguenther@suse.de>
7 and Prathamesh Kulkarni <bilbotheelffriend@gmail.com>
9 This file is part of GCC.
11 GCC is free software; you can redistribute it and/or modify it under
12 the terms of the GNU General Public License as published by the Free
13 Software Foundation; either version 3, or (at your option) any later
16 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
17 WARRANTY; without even the implied warranty of MERCHANTABILITY or
18 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21 You should have received a copy of the GNU General Public License
22 along with GCC; see the file COPYING3. If not see
23 <http://www.gnu.org/licenses/>. */
26 /* Generic tree predicates we inherit. */
28 integer_onep integer_zerop integer_all_onesp integer_minus_onep
29 integer_each_onep integer_truep integer_nonzerop
30 real_zerop real_onep real_minus_onep
33 tree_expr_nonnegative_p
40 (define_operator_list tcc_comparison
41 lt le eq ne ge gt unordered ordered unlt unle ungt unge uneq ltgt)
42 (define_operator_list inverted_tcc_comparison
43 ge gt ne eq lt le ordered unordered ge gt le lt ltgt uneq)
44 (define_operator_list inverted_tcc_comparison_with_nans
45 unge ungt ne eq unlt unle ordered unordered ge gt le lt ltgt uneq)
46 (define_operator_list swapped_tcc_comparison
47 gt ge eq ne le lt unordered ordered ungt unge unlt unle uneq ltgt)
48 (define_operator_list simple_comparison lt le eq ne ge gt)
49 (define_operator_list swapped_simple_comparison gt ge eq ne le lt)
51 #include "cfn-operators.pd"
53 /* Define operand lists for math rounding functions {,i,l,ll}FN,
54 where the versions prefixed with "i" return an int, those prefixed with
55 "l" return a long and those prefixed with "ll" return a long long.
57 Also define operand lists:
59 X<FN>F for all float functions, in the order i, l, ll
60 X<FN> for all double functions, in the same order
61 X<FN>L for all long double functions, in the same order. */
62 #define DEFINE_INT_AND_FLOAT_ROUND_FN(FN) \
63 (define_operator_list X##FN##F BUILT_IN_I##FN##F \
66 (define_operator_list X##FN BUILT_IN_I##FN \
69 (define_operator_list X##FN##L BUILT_IN_I##FN##L \
73 DEFINE_INT_AND_FLOAT_ROUND_FN (FLOOR)
74 DEFINE_INT_AND_FLOAT_ROUND_FN (CEIL)
75 DEFINE_INT_AND_FLOAT_ROUND_FN (ROUND)
76 DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
78 /* As opposed to convert?, this still creates a single pattern, so
79 it is not a suitable replacement for convert? in all cases. */
80 (match (nop_convert @0)
82 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))))
83 (match (nop_convert @0)
85 (if (VECTOR_TYPE_P (type) && VECTOR_TYPE_P (TREE_TYPE (@0))
86 && TYPE_VECTOR_SUBPARTS (type) == TYPE_VECTOR_SUBPARTS (TREE_TYPE (@0))
87 && tree_nop_conversion_p (TREE_TYPE (type), TREE_TYPE (TREE_TYPE (@0))))))
88 /* This one has to be last, or it shadows the others. */
89 (match (nop_convert @0)
92 /* Simplifications of operations with one constant operand and
93 simplifications to constants or single values. */
95 (for op (plus pointer_plus minus bit_ior bit_xor)
100 /* 0 +p index -> (type)index */
102 (pointer_plus integer_zerop @1)
103 (non_lvalue (convert @1)))
105 /* See if ARG1 is zero and X + ARG1 reduces to X.
106 Likewise if the operands are reversed. */
108 (plus:c @0 real_zerop@1)
109 (if (fold_real_zero_addition_p (type, @1, 0))
112 /* See if ARG1 is zero and X - ARG1 reduces to X. */
114 (minus @0 real_zerop@1)
115 (if (fold_real_zero_addition_p (type, @1, 1))
119 This is unsafe for certain floats even in non-IEEE formats.
120 In IEEE, it is unsafe because it does wrong for NaNs.
121 Also note that operand_equal_p is always false if an operand
125 (if (!FLOAT_TYPE_P (type) || !HONOR_NANS (type))
126 { build_zero_cst (type); }))
129 (mult @0 integer_zerop@1)
132 /* Maybe fold x * 0 to 0. The expressions aren't the same
133 when x is NaN, since x * 0 is also NaN. Nor are they the
134 same in modes with signed zeros, since multiplying a
135 negative value by 0 gives -0, not +0. */
137 (mult @0 real_zerop@1)
138 (if (!HONOR_NANS (type) && !HONOR_SIGNED_ZEROS (type))
141 /* In IEEE floating point, x*1 is not equivalent to x for snans.
142 Likewise for complex arithmetic with signed zeros. */
145 (if (!HONOR_SNANS (type)
146 && (!HONOR_SIGNED_ZEROS (type)
147 || !COMPLEX_FLOAT_TYPE_P (type)))
150 /* Transform x * -1.0 into -x. */
152 (mult @0 real_minus_onep)
153 (if (!HONOR_SNANS (type)
154 && (!HONOR_SIGNED_ZEROS (type)
155 || !COMPLEX_FLOAT_TYPE_P (type)))
158 (for cmp (gt ge lt le)
159 outp (convert convert negate negate)
160 outn (negate negate convert convert)
161 /* Transform (X > 0.0 ? 1.0 : -1.0) into copysign(1, X). */
162 /* Transform (X >= 0.0 ? 1.0 : -1.0) into copysign(1, X). */
163 /* Transform (X < 0.0 ? 1.0 : -1.0) into copysign(1,-X). */
164 /* Transform (X <= 0.0 ? 1.0 : -1.0) into copysign(1,-X). */
166 (cond (cmp @0 real_zerop) real_onep@1 real_minus_onep)
167 (if (!HONOR_NANS (type) && !HONOR_SIGNED_ZEROS (type)
168 && types_match (type, TREE_TYPE (@0)))
170 (if (types_match (type, float_type_node))
171 (BUILT_IN_COPYSIGNF @1 (outp @0)))
172 (if (types_match (type, double_type_node))
173 (BUILT_IN_COPYSIGN @1 (outp @0)))
174 (if (types_match (type, long_double_type_node))
175 (BUILT_IN_COPYSIGNL @1 (outp @0))))))
176 /* Transform (X > 0.0 ? -1.0 : 1.0) into copysign(1,-X). */
177 /* Transform (X >= 0.0 ? -1.0 : 1.0) into copysign(1,-X). */
178 /* Transform (X < 0.0 ? -1.0 : 1.0) into copysign(1,X). */
179 /* Transform (X <= 0.0 ? -1.0 : 1.0) into copysign(1,X). */
181 (cond (cmp @0 real_zerop) real_minus_onep real_onep@1)
182 (if (!HONOR_NANS (type) && !HONOR_SIGNED_ZEROS (type)
183 && types_match (type, TREE_TYPE (@0)))
185 (if (types_match (type, float_type_node))
186 (BUILT_IN_COPYSIGNF @1 (outn @0)))
187 (if (types_match (type, double_type_node))
188 (BUILT_IN_COPYSIGN @1 (outn @0)))
189 (if (types_match (type, long_double_type_node))
190 (BUILT_IN_COPYSIGNL @1 (outn @0)))))))
192 /* Transform X * copysign (1.0, X) into abs(X). */
194 (mult:c @0 (COPYSIGN real_onep @0))
195 (if (!HONOR_NANS (type) && !HONOR_SIGNED_ZEROS (type))
198 /* Transform X * copysign (1.0, -X) into -abs(X). */
200 (mult:c @0 (COPYSIGN real_onep (negate @0)))
201 (if (!HONOR_NANS (type) && !HONOR_SIGNED_ZEROS (type))
204 /* Transform copysign (CST, X) into copysign (ABS(CST), X). */
206 (COPYSIGN REAL_CST@0 @1)
207 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@0)))
208 (COPYSIGN (negate @0) @1)))
210 /* X * 1, X / 1 -> X. */
211 (for op (mult trunc_div ceil_div floor_div round_div exact_div)
216 /* (A / (1 << B)) -> (A >> B).
217 Only for unsigned A. For signed A, this would not preserve rounding
219 For example: (-1 / ( 1 << B)) != -1 >> B. */
221 (trunc_div @0 (lshift integer_onep@1 @2))
222 (if ((TYPE_UNSIGNED (type) || tree_expr_nonnegative_p (@0))
223 && (!VECTOR_TYPE_P (type)
224 || target_supports_op_p (type, RSHIFT_EXPR, optab_vector)
225 || target_supports_op_p (type, RSHIFT_EXPR, optab_scalar)))
228 /* Preserve explicit divisions by 0: the C++ front-end wants to detect
229 undefined behavior in constexpr evaluation, and assuming that the division
230 traps enables better optimizations than these anyway. */
231 (for div (trunc_div ceil_div floor_div round_div exact_div)
232 /* 0 / X is always zero. */
234 (div integer_zerop@0 @1)
235 /* But not for 0 / 0 so that we can get the proper warnings and errors. */
236 (if (!integer_zerop (@1))
240 (div @0 integer_minus_onep@1)
241 (if (!TYPE_UNSIGNED (type))
246 /* But not for 0 / 0 so that we can get the proper warnings and errors.
247 And not for _Fract types where we can't build 1. */
248 (if (!integer_zerop (@0) && !ALL_FRACT_MODE_P (TYPE_MODE (type)))
249 { build_one_cst (type); }))
250 /* X / abs (X) is X < 0 ? -1 : 1. */
253 (if (INTEGRAL_TYPE_P (type)
254 && TYPE_OVERFLOW_UNDEFINED (type))
255 (cond (lt @0 { build_zero_cst (type); })
256 { build_minus_one_cst (type); } { build_one_cst (type); })))
259 (div:C @0 (negate @0))
260 (if ((INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
261 && TYPE_OVERFLOW_UNDEFINED (type))
262 { build_minus_one_cst (type); })))
264 /* For unsigned integral types, FLOOR_DIV_EXPR is the same as
265 TRUNC_DIV_EXPR. Rewrite into the latter in this case. */
268 (if ((INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
269 && TYPE_UNSIGNED (type))
272 /* Combine two successive divisions. Note that combining ceil_div
273 and floor_div is trickier and combining round_div even more so. */
274 (for div (trunc_div exact_div)
276 (div (div @0 INTEGER_CST@1) INTEGER_CST@2)
279 wide_int mul = wi::mul (@1, @2, TYPE_SIGN (type), &overflow_p);
282 (div @0 { wide_int_to_tree (type, mul); })
283 (if (TYPE_UNSIGNED (type)
284 || mul != wi::min_value (TYPE_PRECISION (type), SIGNED))
285 { build_zero_cst (type); })))))
287 /* Combine successive multiplications. Similar to above, but handling
288 overflow is different. */
290 (mult (mult @0 INTEGER_CST@1) INTEGER_CST@2)
293 wide_int mul = wi::mul (@1, @2, TYPE_SIGN (type), &overflow_p);
295 /* Skip folding on overflow: the only special case is @1 * @2 == -INT_MIN,
296 otherwise undefined overflow implies that @0 must be zero. */
297 (if (!overflow_p || TYPE_OVERFLOW_WRAPS (type))
298 (mult @0 { wide_int_to_tree (type, mul); }))))
300 /* Optimize A / A to 1.0 if we don't care about
301 NaNs or Infinities. */
304 (if (FLOAT_TYPE_P (type)
305 && ! HONOR_NANS (type)
306 && ! HONOR_INFINITIES (type))
307 { build_one_cst (type); }))
309 /* Optimize -A / A to -1.0 if we don't care about
310 NaNs or Infinities. */
312 (rdiv:C @0 (negate @0))
313 (if (FLOAT_TYPE_P (type)
314 && ! HONOR_NANS (type)
315 && ! HONOR_INFINITIES (type))
316 { build_minus_one_cst (type); }))
318 /* PR71078: x / abs(x) -> copysign (1.0, x) */
320 (rdiv:C (convert? @0) (convert? (abs @0)))
321 (if (SCALAR_FLOAT_TYPE_P (type)
322 && ! HONOR_NANS (type)
323 && ! HONOR_INFINITIES (type))
325 (if (types_match (type, float_type_node))
326 (BUILT_IN_COPYSIGNF { build_one_cst (type); } (convert @0)))
327 (if (types_match (type, double_type_node))
328 (BUILT_IN_COPYSIGN { build_one_cst (type); } (convert @0)))
329 (if (types_match (type, long_double_type_node))
330 (BUILT_IN_COPYSIGNL { build_one_cst (type); } (convert @0))))))
332 /* In IEEE floating point, x/1 is not equivalent to x for snans. */
335 (if (!HONOR_SNANS (type))
338 /* In IEEE floating point, x/-1 is not equivalent to -x for snans. */
340 (rdiv @0 real_minus_onep)
341 (if (!HONOR_SNANS (type))
344 (if (flag_reciprocal_math)
345 /* Convert (A/B)/C to A/(B*C) */
347 (rdiv (rdiv:s @0 @1) @2)
348 (rdiv @0 (mult @1 @2)))
350 /* Convert A/(B/C) to (A/B)*C */
352 (rdiv @0 (rdiv:s @1 @2))
353 (mult (rdiv @0 @1) @2)))
355 /* Optimize (X & (-A)) / A where A is a power of 2, to X >> log2(A) */
356 (for div (trunc_div ceil_div floor_div round_div exact_div)
358 (div (convert? (bit_and @0 INTEGER_CST@1)) INTEGER_CST@2)
359 (if (integer_pow2p (@2)
360 && tree_int_cst_sgn (@2) > 0
361 && wi::add (@2, @1) == 0
362 && tree_nop_conversion_p (type, TREE_TYPE (@0)))
363 (rshift (convert @0) { build_int_cst (integer_type_node,
364 wi::exact_log2 (@2)); }))))
366 /* If ARG1 is a constant, we can convert this to a multiply by the
367 reciprocal. This does not have the same rounding properties,
368 so only do this if -freciprocal-math. We can actually
369 always safely do it if ARG1 is a power of two, but it's hard to
370 tell if it is or not in a portable manner. */
371 (for cst (REAL_CST COMPLEX_CST VECTOR_CST)
375 (if (flag_reciprocal_math
378 { tree tem = const_binop (RDIV_EXPR, type, build_one_cst (type), @1); }
380 (mult @0 { tem; } )))
381 (if (cst != COMPLEX_CST)
382 (with { tree inverse = exact_inverse (type, @1); }
384 (mult @0 { inverse; } ))))))))
386 (for mod (ceil_mod floor_mod round_mod trunc_mod)
387 /* 0 % X is always zero. */
389 (mod integer_zerop@0 @1)
390 /* But not for 0 % 0 so that we can get the proper warnings and errors. */
391 (if (!integer_zerop (@1))
393 /* X % 1 is always zero. */
395 (mod @0 integer_onep)
396 { build_zero_cst (type); })
397 /* X % -1 is zero. */
399 (mod @0 integer_minus_onep@1)
400 (if (!TYPE_UNSIGNED (type))
401 { build_zero_cst (type); }))
405 /* But not for 0 % 0 so that we can get the proper warnings and errors. */
406 (if (!integer_zerop (@0))
407 { build_zero_cst (type); }))
408 /* (X % Y) % Y is just X % Y. */
410 (mod (mod@2 @0 @1) @1)
412 /* From extract_muldiv_1: (X * C1) % C2 is zero if C1 is a multiple of C2. */
414 (mod (mult @0 INTEGER_CST@1) INTEGER_CST@2)
415 (if (ANY_INTEGRAL_TYPE_P (type)
416 && TYPE_OVERFLOW_UNDEFINED (type)
417 && wi::multiple_of_p (@1, @2, TYPE_SIGN (type)))
418 { build_zero_cst (type); })))
420 /* X % -C is the same as X % C. */
422 (trunc_mod @0 INTEGER_CST@1)
423 (if (TYPE_SIGN (type) == SIGNED
424 && !TREE_OVERFLOW (@1)
426 && !TYPE_OVERFLOW_TRAPS (type)
427 /* Avoid this transformation if C is INT_MIN, i.e. C == -C. */
428 && !sign_bit_p (@1, @1))
429 (trunc_mod @0 (negate @1))))
431 /* X % -Y is the same as X % Y. */
433 (trunc_mod @0 (convert? (negate @1)))
434 (if (INTEGRAL_TYPE_P (type)
435 && !TYPE_UNSIGNED (type)
436 && !TYPE_OVERFLOW_TRAPS (type)
437 && tree_nop_conversion_p (type, TREE_TYPE (@1))
438 /* Avoid this transformation if X might be INT_MIN or
439 Y might be -1, because we would then change valid
440 INT_MIN % -(-1) into invalid INT_MIN % -1. */
441 && (expr_not_equal_to (@0, TYPE_MIN_VALUE (type))
442 || expr_not_equal_to (@1, wi::minus_one (TYPE_PRECISION
444 (trunc_mod @0 (convert @1))))
446 /* X - (X / Y) * Y is the same as X % Y. */
448 (minus (convert1? @0) (convert2? (mult:c (trunc_div @@0 @@1) @1)))
449 (if (INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
450 (convert (trunc_mod @0 @1))))
452 /* Optimize TRUNC_MOD_EXPR by a power of two into a BIT_AND_EXPR,
453 i.e. "X % C" into "X & (C - 1)", if X and C are positive.
454 Also optimize A % (C << N) where C is a power of 2,
455 to A & ((C << N) - 1). */
456 (match (power_of_two_cand @1)
458 (match (power_of_two_cand @1)
459 (lshift INTEGER_CST@1 @2))
460 (for mod (trunc_mod floor_mod)
462 (mod @0 (convert?@3 (power_of_two_cand@1 @2)))
463 (if ((TYPE_UNSIGNED (type)
464 || tree_expr_nonnegative_p (@0))
465 && tree_nop_conversion_p (type, TREE_TYPE (@3))
466 && integer_pow2p (@2) && tree_int_cst_sgn (@2) > 0)
467 (bit_and @0 (convert (minus @1 { build_int_cst (TREE_TYPE (@1), 1); }))))))
469 /* Simplify (unsigned t * 2)/2 -> unsigned t & 0x7FFFFFFF. */
471 (trunc_div (mult @0 integer_pow2p@1) @1)
472 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
473 (bit_and @0 { wide_int_to_tree
474 (type, wi::mask (TYPE_PRECISION (type) - wi::exact_log2 (@1),
475 false, TYPE_PRECISION (type))); })))
477 /* Simplify (unsigned t / 2) * 2 -> unsigned t & ~1. */
479 (mult (trunc_div @0 integer_pow2p@1) @1)
480 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
481 (bit_and @0 (negate @1))))
483 /* Simplify (t * 2) / 2) -> t. */
484 (for div (trunc_div ceil_div floor_div round_div exact_div)
486 (div (mult @0 @1) @1)
487 (if (ANY_INTEGRAL_TYPE_P (type)
488 && TYPE_OVERFLOW_UNDEFINED (type))
492 /* Simplify cos(-x) and cos(|x|) -> cos(x). Similarly for cosh. */
497 /* Simplify pow(-x, y) and pow(|x|,y) -> pow(x,y) if y is an even integer. */
500 (pows (op @0) REAL_CST@1)
501 (with { HOST_WIDE_INT n; }
502 (if (real_isinteger (&TREE_REAL_CST (@1), &n) && (n & 1) == 0)
504 /* Likewise for powi. */
507 (pows (op @0) INTEGER_CST@1)
508 (if (wi::bit_and (@1, 1) == 0)
510 /* Strip negate and abs from both operands of hypot. */
518 /* copysign(-x, y) and copysign(abs(x), y) -> copysign(x, y). */
519 (for copysigns (COPYSIGN)
521 (copysigns (op @0) @1)
524 /* abs(x)*abs(x) -> x*x. Should be valid for all types. */
529 /* cos(copysign(x, y)) -> cos(x). Similarly for cosh. */
533 (coss (copysigns @0 @1))
536 /* pow(copysign(x, y), z) -> pow(x, z) if z is an even integer. */
540 (pows (copysigns @0 @2) REAL_CST@1)
541 (with { HOST_WIDE_INT n; }
542 (if (real_isinteger (&TREE_REAL_CST (@1), &n) && (n & 1) == 0)
544 /* Likewise for powi. */
548 (pows (copysigns @0 @2) INTEGER_CST@1)
549 (if (wi::bit_and (@1, 1) == 0)
554 /* hypot(copysign(x, y), z) -> hypot(x, z). */
556 (hypots (copysigns @0 @1) @2)
558 /* hypot(x, copysign(y, z)) -> hypot(x, y). */
560 (hypots @0 (copysigns @1 @2))
563 /* copysign(x, CST) -> [-]abs (x). */
564 (for copysigns (COPYSIGN)
566 (copysigns @0 REAL_CST@1)
567 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
571 /* copysign(copysign(x, y), z) -> copysign(x, z). */
572 (for copysigns (COPYSIGN)
574 (copysigns (copysigns @0 @1) @2)
577 /* copysign(x,y)*copysign(x,y) -> x*x. */
578 (for copysigns (COPYSIGN)
580 (mult (copysigns@2 @0 @1) @2)
583 /* ccos(-x) -> ccos(x). Similarly for ccosh. */
584 (for ccoss (CCOS CCOSH)
589 /* cabs(-x) and cos(conj(x)) -> cabs(x). */
590 (for ops (conj negate)
596 /* Fold (a * (1 << b)) into (a << b) */
598 (mult:c @0 (convert? (lshift integer_onep@1 @2)))
599 (if (! FLOAT_TYPE_P (type)
600 && tree_nop_conversion_p (type, TREE_TYPE (@1)))
603 /* Fold (C1/X)*C2 into (C1*C2)/X. */
605 (mult (rdiv@3 REAL_CST@0 @1) REAL_CST@2)
606 (if (flag_associative_math
609 { tree tem = const_binop (MULT_EXPR, type, @0, @2); }
611 (rdiv { tem; } @1)))))
613 /* Convert C1/(X*C2) into (C1/C2)/X */
615 (rdiv REAL_CST@0 (mult @1 REAL_CST@2))
616 (if (flag_reciprocal_math)
618 { tree tem = const_binop (RDIV_EXPR, type, @0, @2); }
620 (rdiv { tem; } @1)))))
622 /* Simplify ~X & X as zero. */
624 (bit_and:c (convert? @0) (convert? (bit_not @0)))
625 { build_zero_cst (type); })
627 /* PR71636: Transform x & ((1U << b) - 1) -> x & ~(~0U << b); */
629 (bit_and:c @0 (plus:s (lshift:s integer_onep @1) integer_minus_onep))
630 (if (TYPE_UNSIGNED (type))
631 (bit_and @0 (bit_not (lshift { build_all_ones_cst (type); } @1)))))
633 (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 = wi::add (@1, @2).to_uhwi (); }
1887 /* Deal with a OP (c1 + c2) being undefined but (a OP c1) OP c2
1888 being well defined. */
1890 (if (op == LROTATE_EXPR || op == RROTATE_EXPR)
1891 (op @0 { build_int_cst (TREE_TYPE (@1), low % prec); })
1892 (if (TYPE_UNSIGNED (type) || op == LSHIFT_EXPR)
1893 { build_zero_cst (type); }
1894 (op @0 { build_int_cst (TREE_TYPE (@1), prec - 1); })))
1895 (op @0 { build_int_cst (TREE_TYPE (@1), low); })))))))
1898 /* ((1 << A) & 1) != 0 -> A == 0
1899 ((1 << A) & 1) == 0 -> A != 0 */
1903 (cmp (bit_and (lshift integer_onep @0) integer_onep) integer_zerop)
1904 (icmp @0 { build_zero_cst (TREE_TYPE (@0)); })))
1906 /* (CST1 << A) == CST2 -> A == ctz (CST2) - ctz (CST1)
1907 (CST1 << A) != CST2 -> A != ctz (CST2) - ctz (CST1)
1911 (cmp (lshift INTEGER_CST@0 @1) INTEGER_CST@2)
1912 (with { int cand = wi::ctz (@2) - wi::ctz (@0); }
1914 || (!integer_zerop (@2)
1915 && wi::ne_p (wi::lshift (@0, cand), @2)))
1916 { constant_boolean_node (cmp == NE_EXPR, type); }
1917 (if (!integer_zerop (@2)
1918 && wi::eq_p (wi::lshift (@0, cand), @2))
1919 (cmp @1 { build_int_cst (TREE_TYPE (@1), cand); }))))))
1921 /* Fold (X << C1) & C2 into (X << C1) & (C2 | ((1 << C1) - 1))
1922 (X >> C1) & C2 into (X >> C1) & (C2 | ~((type) -1 >> C1))
1923 if the new mask might be further optimized. */
1924 (for shift (lshift rshift)
1926 (bit_and (convert?:s@4 (shift:s@5 (convert1?@3 @0) INTEGER_CST@1))
1928 (if (tree_nop_conversion_p (TREE_TYPE (@4), TREE_TYPE (@5))
1929 && TYPE_PRECISION (type) <= HOST_BITS_PER_WIDE_INT
1930 && tree_fits_uhwi_p (@1)
1931 && tree_to_uhwi (@1) > 0
1932 && tree_to_uhwi (@1) < TYPE_PRECISION (type))
1935 unsigned int shiftc = tree_to_uhwi (@1);
1936 unsigned HOST_WIDE_INT mask = TREE_INT_CST_LOW (@2);
1937 unsigned HOST_WIDE_INT newmask, zerobits = 0;
1938 tree shift_type = TREE_TYPE (@3);
1941 if (shift == LSHIFT_EXPR)
1942 zerobits = ((HOST_WIDE_INT_1U << shiftc) - 1);
1943 else if (shift == RSHIFT_EXPR
1944 && type_has_mode_precision_p (shift_type))
1946 prec = TYPE_PRECISION (TREE_TYPE (@3));
1948 /* See if more bits can be proven as zero because of
1951 && TYPE_UNSIGNED (TREE_TYPE (@0)))
1953 tree inner_type = TREE_TYPE (@0);
1954 if (type_has_mode_precision_p (inner_type)
1955 && TYPE_PRECISION (inner_type) < prec)
1957 prec = TYPE_PRECISION (inner_type);
1958 /* See if we can shorten the right shift. */
1960 shift_type = inner_type;
1961 /* Otherwise X >> C1 is all zeros, so we'll optimize
1962 it into (X, 0) later on by making sure zerobits
1966 zerobits = HOST_WIDE_INT_M1U;
1969 zerobits >>= HOST_BITS_PER_WIDE_INT - shiftc;
1970 zerobits <<= prec - shiftc;
1972 /* For arithmetic shift if sign bit could be set, zerobits
1973 can contain actually sign bits, so no transformation is
1974 possible, unless MASK masks them all away. In that
1975 case the shift needs to be converted into logical shift. */
1976 if (!TYPE_UNSIGNED (TREE_TYPE (@3))
1977 && prec == TYPE_PRECISION (TREE_TYPE (@3)))
1979 if ((mask & zerobits) == 0)
1980 shift_type = unsigned_type_for (TREE_TYPE (@3));
1986 /* ((X << 16) & 0xff00) is (X, 0). */
1987 (if ((mask & zerobits) == mask)
1988 { build_int_cst (type, 0); }
1989 (with { newmask = mask | zerobits; }
1990 (if (newmask != mask && (newmask & (newmask + 1)) == 0)
1993 /* Only do the transformation if NEWMASK is some integer
1995 for (prec = BITS_PER_UNIT;
1996 prec < HOST_BITS_PER_WIDE_INT; prec <<= 1)
1997 if (newmask == (HOST_WIDE_INT_1U << prec) - 1)
2000 (if (prec < HOST_BITS_PER_WIDE_INT
2001 || newmask == HOST_WIDE_INT_M1U)
2003 { tree newmaskt = build_int_cst_type (TREE_TYPE (@2), newmask); }
2004 (if (!tree_int_cst_equal (newmaskt, @2))
2005 (if (shift_type != TREE_TYPE (@3))
2006 (bit_and (convert (shift:shift_type (convert @3) @1)) { newmaskt; })
2007 (bit_and @4 { newmaskt; })))))))))))))
2009 /* Fold (X {&,^,|} C2) << C1 into (X << C1) {&,^,|} (C2 << C1)
2010 (X {&,^,|} C2) >> C1 into (X >> C1) & (C2 >> C1). */
2011 (for shift (lshift rshift)
2012 (for bit_op (bit_and bit_xor bit_ior)
2014 (shift (convert?:s (bit_op:s @0 INTEGER_CST@2)) INTEGER_CST@1)
2015 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2016 (with { tree mask = int_const_binop (shift, fold_convert (type, @2), @1); }
2017 (bit_op (shift (convert @0) @1) { mask; }))))))
2019 /* ~(~X >> Y) -> X >> Y (for arithmetic shift). */
2021 (bit_not (convert1?:s (rshift:s (convert2?@0 (bit_not @1)) @2)))
2022 (if (!TYPE_UNSIGNED (TREE_TYPE (@0))
2023 && (element_precision (TREE_TYPE (@0))
2024 <= element_precision (TREE_TYPE (@1))
2025 || !TYPE_UNSIGNED (TREE_TYPE (@1))))
2027 { tree shift_type = TREE_TYPE (@0); }
2028 (convert (rshift (convert:shift_type @1) @2)))))
2030 /* ~(~X >>r Y) -> X >>r Y
2031 ~(~X <<r Y) -> X <<r Y */
2032 (for rotate (lrotate rrotate)
2034 (bit_not (convert1?:s (rotate:s (convert2?@0 (bit_not @1)) @2)))
2035 (if ((element_precision (TREE_TYPE (@0))
2036 <= element_precision (TREE_TYPE (@1))
2037 || !TYPE_UNSIGNED (TREE_TYPE (@1)))
2038 && (element_precision (type) <= element_precision (TREE_TYPE (@0))
2039 || !TYPE_UNSIGNED (TREE_TYPE (@0))))
2041 { tree rotate_type = TREE_TYPE (@0); }
2042 (convert (rotate (convert:rotate_type @1) @2))))))
2044 /* Simplifications of conversions. */
2046 /* Basic strip-useless-type-conversions / strip_nops. */
2047 (for cvt (convert view_convert float fix_trunc)
2050 (if ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@0)))
2051 || (GENERIC && type == TREE_TYPE (@0)))
2054 /* Contract view-conversions. */
2056 (view_convert (view_convert @0))
2059 /* For integral conversions with the same precision or pointer
2060 conversions use a NOP_EXPR instead. */
2063 (if ((INTEGRAL_TYPE_P (type) || POINTER_TYPE_P (type))
2064 && (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
2065 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (@0)))
2068 /* Strip inner integral conversions that do not change precision or size, or
2069 zero-extend while keeping the same size (for bool-to-char). */
2071 (view_convert (convert@0 @1))
2072 (if ((INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
2073 && (INTEGRAL_TYPE_P (TREE_TYPE (@1)) || POINTER_TYPE_P (TREE_TYPE (@1)))
2074 && TYPE_SIZE (TREE_TYPE (@0)) == TYPE_SIZE (TREE_TYPE (@1))
2075 && (TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1))
2076 || (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (TREE_TYPE (@1))
2077 && TYPE_UNSIGNED (TREE_TYPE (@1)))))
2080 /* Re-association barriers around constants and other re-association
2081 barriers can be removed. */
2083 (paren CONSTANT_CLASS_P@0)
2086 (paren (paren@1 @0))
2089 /* Handle cases of two conversions in a row. */
2090 (for ocvt (convert float fix_trunc)
2091 (for icvt (convert float)
2096 tree inside_type = TREE_TYPE (@0);
2097 tree inter_type = TREE_TYPE (@1);
2098 int inside_int = INTEGRAL_TYPE_P (inside_type);
2099 int inside_ptr = POINTER_TYPE_P (inside_type);
2100 int inside_float = FLOAT_TYPE_P (inside_type);
2101 int inside_vec = VECTOR_TYPE_P (inside_type);
2102 unsigned int inside_prec = TYPE_PRECISION (inside_type);
2103 int inside_unsignedp = TYPE_UNSIGNED (inside_type);
2104 int inter_int = INTEGRAL_TYPE_P (inter_type);
2105 int inter_ptr = POINTER_TYPE_P (inter_type);
2106 int inter_float = FLOAT_TYPE_P (inter_type);
2107 int inter_vec = VECTOR_TYPE_P (inter_type);
2108 unsigned int inter_prec = TYPE_PRECISION (inter_type);
2109 int inter_unsignedp = TYPE_UNSIGNED (inter_type);
2110 int final_int = INTEGRAL_TYPE_P (type);
2111 int final_ptr = POINTER_TYPE_P (type);
2112 int final_float = FLOAT_TYPE_P (type);
2113 int final_vec = VECTOR_TYPE_P (type);
2114 unsigned int final_prec = TYPE_PRECISION (type);
2115 int final_unsignedp = TYPE_UNSIGNED (type);
2118 /* In addition to the cases of two conversions in a row
2119 handled below, if we are converting something to its own
2120 type via an object of identical or wider precision, neither
2121 conversion is needed. */
2122 (if (((GIMPLE && useless_type_conversion_p (type, inside_type))
2124 && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (inside_type)))
2125 && (((inter_int || inter_ptr) && final_int)
2126 || (inter_float && final_float))
2127 && inter_prec >= final_prec)
2130 /* Likewise, if the intermediate and initial types are either both
2131 float or both integer, we don't need the middle conversion if the
2132 former is wider than the latter and doesn't change the signedness
2133 (for integers). Avoid this if the final type is a pointer since
2134 then we sometimes need the middle conversion. */
2135 (if (((inter_int && inside_int) || (inter_float && inside_float))
2136 && (final_int || final_float)
2137 && inter_prec >= inside_prec
2138 && (inter_float || inter_unsignedp == inside_unsignedp))
2141 /* If we have a sign-extension of a zero-extended value, we can
2142 replace that by a single zero-extension. Likewise if the
2143 final conversion does not change precision we can drop the
2144 intermediate conversion. */
2145 (if (inside_int && inter_int && final_int
2146 && ((inside_prec < inter_prec && inter_prec < final_prec
2147 && inside_unsignedp && !inter_unsignedp)
2148 || final_prec == inter_prec))
2151 /* Two conversions in a row are not needed unless:
2152 - some conversion is floating-point (overstrict for now), or
2153 - some conversion is a vector (overstrict for now), or
2154 - the intermediate type is narrower than both initial and
2156 - the intermediate type and innermost type differ in signedness,
2157 and the outermost type is wider than the intermediate, or
2158 - the initial type is a pointer type and the precisions of the
2159 intermediate and final types differ, or
2160 - the final type is a pointer type and the precisions of the
2161 initial and intermediate types differ. */
2162 (if (! inside_float && ! inter_float && ! final_float
2163 && ! inside_vec && ! inter_vec && ! final_vec
2164 && (inter_prec >= inside_prec || inter_prec >= final_prec)
2165 && ! (inside_int && inter_int
2166 && inter_unsignedp != inside_unsignedp
2167 && inter_prec < final_prec)
2168 && ((inter_unsignedp && inter_prec > inside_prec)
2169 == (final_unsignedp && final_prec > inter_prec))
2170 && ! (inside_ptr && inter_prec != final_prec)
2171 && ! (final_ptr && inside_prec != inter_prec))
2174 /* A truncation to an unsigned type (a zero-extension) should be
2175 canonicalized as bitwise and of a mask. */
2176 (if (GIMPLE /* PR70366: doing this in GENERIC breaks -Wconversion. */
2177 && final_int && inter_int && inside_int
2178 && final_prec == inside_prec
2179 && final_prec > inter_prec
2181 (convert (bit_and @0 { wide_int_to_tree
2183 wi::mask (inter_prec, false,
2184 TYPE_PRECISION (inside_type))); })))
2186 /* If we are converting an integer to a floating-point that can
2187 represent it exactly and back to an integer, we can skip the
2188 floating-point conversion. */
2189 (if (GIMPLE /* PR66211 */
2190 && inside_int && inter_float && final_int &&
2191 (unsigned) significand_size (TYPE_MODE (inter_type))
2192 >= inside_prec - !inside_unsignedp)
2195 /* If we have a narrowing conversion to an integral type that is fed by a
2196 BIT_AND_EXPR, we might be able to remove the BIT_AND_EXPR if it merely
2197 masks off bits outside the final type (and nothing else). */
2199 (convert (bit_and @0 INTEGER_CST@1))
2200 (if (INTEGRAL_TYPE_P (type)
2201 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
2202 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))
2203 && operand_equal_p (@1, build_low_bits_mask (TREE_TYPE (@1),
2204 TYPE_PRECISION (type)), 0))
2208 /* (X /[ex] A) * A -> X. */
2210 (mult (convert1? (exact_div @0 @@1)) (convert2? @1))
2213 /* Canonicalization of binary operations. */
2215 /* Convert X + -C into X - C. */
2217 (plus @0 REAL_CST@1)
2218 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
2219 (with { tree tem = const_unop (NEGATE_EXPR, type, @1); }
2220 (if (!TREE_OVERFLOW (tem) || !flag_trapping_math)
2221 (minus @0 { tem; })))))
2223 /* Convert x+x into x*2. */
2226 (if (SCALAR_FLOAT_TYPE_P (type))
2227 (mult @0 { build_real (type, dconst2); })
2228 (if (INTEGRAL_TYPE_P (type))
2229 (mult @0 { build_int_cst (type, 2); }))))
2232 (minus integer_zerop @1)
2235 /* (ARG0 - ARG1) is the same as (-ARG1 + ARG0). So check whether
2236 ARG0 is zero and X + ARG0 reduces to X, since that would mean
2237 (-ARG1 + ARG0) reduces to -ARG1. */
2239 (minus real_zerop@0 @1)
2240 (if (fold_real_zero_addition_p (type, @0, 0))
2243 /* Transform x * -1 into -x. */
2245 (mult @0 integer_minus_onep)
2248 /* Reassociate (X * CST) * Y to (X * Y) * CST. This does not introduce
2249 signed overflow for CST != 0 && CST != -1. */
2251 (mult:c (mult:s @0 INTEGER_CST@1) @2)
2252 (if (TREE_CODE (@2) != INTEGER_CST
2253 && !integer_zerop (@1) && !integer_minus_onep (@1))
2254 (mult (mult @0 @2) @1)))
2256 /* True if we can easily extract the real and imaginary parts of a complex
2258 (match compositional_complex
2259 (convert? (complex @0 @1)))
2261 /* COMPLEX_EXPR and REALPART/IMAGPART_EXPR cancellations. */
2263 (complex (realpart @0) (imagpart @0))
2266 (realpart (complex @0 @1))
2269 (imagpart (complex @0 @1))
2272 /* Sometimes we only care about half of a complex expression. */
2274 (realpart (convert?:s (conj:s @0)))
2275 (convert (realpart @0)))
2277 (imagpart (convert?:s (conj:s @0)))
2278 (convert (negate (imagpart @0))))
2279 (for part (realpart imagpart)
2280 (for op (plus minus)
2282 (part (convert?:s@2 (op:s @0 @1)))
2283 (convert (op (part @0) (part @1))))))
2285 (realpart (convert?:s (CEXPI:s @0)))
2288 (imagpart (convert?:s (CEXPI:s @0)))
2291 /* conj(conj(x)) -> x */
2293 (conj (convert? (conj @0)))
2294 (if (tree_nop_conversion_p (TREE_TYPE (@0), type))
2297 /* conj({x,y}) -> {x,-y} */
2299 (conj (convert?:s (complex:s @0 @1)))
2300 (with { tree itype = TREE_TYPE (type); }
2301 (complex (convert:itype @0) (negate (convert:itype @1)))))
2303 /* BSWAP simplifications, transforms checked by gcc.dg/builtin-bswap-8.c. */
2304 (for bswap (BUILT_IN_BSWAP16 BUILT_IN_BSWAP32 BUILT_IN_BSWAP64)
2309 (bswap (bit_not (bswap @0)))
2311 (for bitop (bit_xor bit_ior bit_and)
2313 (bswap (bitop:c (bswap @0) @1))
2314 (bitop @0 (bswap @1)))))
2317 /* Combine COND_EXPRs and VEC_COND_EXPRs. */
2319 /* Simplify constant conditions.
2320 Only optimize constant conditions when the selected branch
2321 has the same type as the COND_EXPR. This avoids optimizing
2322 away "c ? x : throw", where the throw has a void type.
2323 Note that we cannot throw away the fold-const.c variant nor
2324 this one as we depend on doing this transform before possibly
2325 A ? B : B -> B triggers and the fold-const.c one can optimize
2326 0 ? A : B to B even if A has side-effects. Something
2327 genmatch cannot handle. */
2329 (cond INTEGER_CST@0 @1 @2)
2330 (if (integer_zerop (@0))
2331 (if (!VOID_TYPE_P (TREE_TYPE (@2)) || VOID_TYPE_P (type))
2333 (if (!VOID_TYPE_P (TREE_TYPE (@1)) || VOID_TYPE_P (type))
2336 (vec_cond VECTOR_CST@0 @1 @2)
2337 (if (integer_all_onesp (@0))
2339 (if (integer_zerop (@0))
2342 /* Simplification moved from fold_cond_expr_with_comparison. It may also
2344 /* This pattern implements two kinds simplification:
2347 (cond (cmp (convert1? x) c1) (convert2? x) c2) -> (minmax (x c)) if:
2348 1) Conversions are type widening from smaller type.
2349 2) Const c1 equals to c2 after canonicalizing comparison.
2350 3) Comparison has tree code LT, LE, GT or GE.
2351 This specific pattern is needed when (cmp (convert x) c) may not
2352 be simplified by comparison patterns because of multiple uses of
2353 x. It also makes sense here because simplifying across multiple
2354 referred var is always benefitial for complicated cases.
2357 (cond (eq (convert1? x) c1) (convert2? x) c2) -> (cond (eq x c1) c1 c2). */
2358 (for cmp (lt le gt ge eq)
2360 (cond (cmp (convert1? @1) INTEGER_CST@3) (convert2? @1) INTEGER_CST@2)
2363 tree from_type = TREE_TYPE (@1);
2364 tree c1_type = TREE_TYPE (@3), c2_type = TREE_TYPE (@2);
2365 enum tree_code code = ERROR_MARK;
2367 if (INTEGRAL_TYPE_P (from_type)
2368 && int_fits_type_p (@2, from_type)
2369 && (types_match (c1_type, from_type)
2370 || (TYPE_PRECISION (c1_type) > TYPE_PRECISION (from_type)
2371 && (TYPE_UNSIGNED (from_type)
2372 || TYPE_SIGN (c1_type) == TYPE_SIGN (from_type))))
2373 && (types_match (c2_type, from_type)
2374 || (TYPE_PRECISION (c2_type) > TYPE_PRECISION (from_type)
2375 && (TYPE_UNSIGNED (from_type)
2376 || TYPE_SIGN (c2_type) == TYPE_SIGN (from_type)))))
2380 if (wi::to_widest (@3) == (wi::to_widest (@2) - 1))
2382 /* X <= Y - 1 equals to X < Y. */
2385 /* X > Y - 1 equals to X >= Y. */
2389 if (wi::to_widest (@3) == (wi::to_widest (@2) + 1))
2391 /* X < Y + 1 equals to X <= Y. */
2394 /* X >= Y + 1 equals to X > Y. */
2398 if (code != ERROR_MARK
2399 || wi::to_widest (@2) == wi::to_widest (@3))
2401 if (cmp == LT_EXPR || cmp == LE_EXPR)
2403 if (cmp == GT_EXPR || cmp == GE_EXPR)
2407 /* Can do A == C1 ? A : C2 -> A == C1 ? C1 : C2? */
2408 else if (int_fits_type_p (@3, from_type))
2412 (if (code == MAX_EXPR)
2413 (convert (max @1 (convert @2)))
2414 (if (code == MIN_EXPR)
2415 (convert (min @1 (convert @2)))
2416 (if (code == EQ_EXPR)
2417 (convert (cond (eq @1 (convert @3))
2418 (convert:from_type @3) (convert:from_type @2)))))))))
2420 /* (cond (cmp (convert? x) c1) (op x c2) c3) -> (op (minmax x c1) c2) if:
2422 1) OP is PLUS or MINUS.
2423 2) CMP is LT, LE, GT or GE.
2424 3) C3 == (C1 op C2), and computation doesn't have undefined behavior.
2426 This pattern also handles special cases like:
2428 A) Operand x is a unsigned to signed type conversion and c1 is
2429 integer zero. In this case,
2430 (signed type)x < 0 <=> x > MAX_VAL(signed type)
2431 (signed type)x >= 0 <=> x <= MAX_VAL(signed type)
2432 B) Const c1 may not equal to (C3 op' C2). In this case we also
2433 check equality for (c1+1) and (c1-1) by adjusting comparison
2436 TODO: Though signed type is handled by this pattern, it cannot be
2437 simplified at the moment because C standard requires additional
2438 type promotion. In order to match&simplify it here, the IR needs
2439 to be cleaned up by other optimizers, i.e, VRP. */
2440 (for op (plus minus)
2441 (for cmp (lt le gt ge)
2443 (cond (cmp (convert? @X) INTEGER_CST@1) (op @X INTEGER_CST@2) INTEGER_CST@3)
2444 (with { tree from_type = TREE_TYPE (@X), to_type = TREE_TYPE (@1); }
2445 (if (types_match (from_type, to_type)
2446 /* Check if it is special case A). */
2447 || (TYPE_UNSIGNED (from_type)
2448 && !TYPE_UNSIGNED (to_type)
2449 && TYPE_PRECISION (from_type) == TYPE_PRECISION (to_type)
2450 && integer_zerop (@1)
2451 && (cmp == LT_EXPR || cmp == GE_EXPR)))
2454 bool overflow = false;
2455 enum tree_code code, cmp_code = cmp;
2456 wide_int real_c1, c1 = @1, c2 = @2, c3 = @3;
2457 signop sgn = TYPE_SIGN (from_type);
2459 /* Handle special case A), given x of unsigned type:
2460 ((signed type)x < 0) <=> (x > MAX_VAL(signed type))
2461 ((signed type)x >= 0) <=> (x <= MAX_VAL(signed type)) */
2462 if (!types_match (from_type, to_type))
2464 if (cmp_code == LT_EXPR)
2466 if (cmp_code == GE_EXPR)
2468 c1 = wi::max_value (to_type);
2470 /* To simplify this pattern, we require c3 = (c1 op c2). Here we
2471 compute (c3 op' c2) and check if it equals to c1 with op' being
2472 the inverted operator of op. Make sure overflow doesn't happen
2473 if it is undefined. */
2474 if (op == PLUS_EXPR)
2475 real_c1 = wi::sub (c3, c2, sgn, &overflow);
2477 real_c1 = wi::add (c3, c2, sgn, &overflow);
2480 if (!overflow || !TYPE_OVERFLOW_UNDEFINED (from_type))
2482 /* Check if c1 equals to real_c1. Boundary condition is handled
2483 by adjusting comparison operation if necessary. */
2484 if (!wi::cmp (wi::sub (real_c1, 1, sgn, &overflow), c1, sgn)
2487 /* X <= Y - 1 equals to X < Y. */
2488 if (cmp_code == LE_EXPR)
2490 /* X > Y - 1 equals to X >= Y. */
2491 if (cmp_code == GT_EXPR)
2494 if (!wi::cmp (wi::add (real_c1, 1, sgn, &overflow), c1, sgn)
2497 /* X < Y + 1 equals to X <= Y. */
2498 if (cmp_code == LT_EXPR)
2500 /* X >= Y + 1 equals to X > Y. */
2501 if (cmp_code == GE_EXPR)
2504 if (code != cmp_code || !wi::cmp (real_c1, c1, sgn))
2506 if (cmp_code == LT_EXPR || cmp_code == LE_EXPR)
2508 if (cmp_code == GT_EXPR || cmp_code == GE_EXPR)
2513 (if (code == MAX_EXPR)
2514 (op (max @X { wide_int_to_tree (from_type, real_c1); })
2515 { wide_int_to_tree (from_type, c2); })
2516 (if (code == MIN_EXPR)
2517 (op (min @X { wide_int_to_tree (from_type, real_c1); })
2518 { wide_int_to_tree (from_type, c2); })))))))))
2520 (for cnd (cond vec_cond)
2521 /* A ? B : (A ? X : C) -> A ? B : C. */
2523 (cnd @0 (cnd @0 @1 @2) @3)
2526 (cnd @0 @1 (cnd @0 @2 @3))
2528 /* A ? B : (!A ? C : X) -> A ? B : C. */
2529 /* ??? This matches embedded conditions open-coded because genmatch
2530 would generate matching code for conditions in separate stmts only.
2531 The following is still important to merge then and else arm cases
2532 from if-conversion. */
2534 (cnd @0 @1 (cnd @2 @3 @4))
2535 (if (COMPARISON_CLASS_P (@0)
2536 && COMPARISON_CLASS_P (@2)
2537 && invert_tree_comparison
2538 (TREE_CODE (@0), HONOR_NANS (TREE_OPERAND (@0, 0))) == TREE_CODE (@2)
2539 && operand_equal_p (TREE_OPERAND (@0, 0), TREE_OPERAND (@2, 0), 0)
2540 && operand_equal_p (TREE_OPERAND (@0, 1), TREE_OPERAND (@2, 1), 0))
2543 (cnd @0 (cnd @1 @2 @3) @4)
2544 (if (COMPARISON_CLASS_P (@0)
2545 && COMPARISON_CLASS_P (@1)
2546 && invert_tree_comparison
2547 (TREE_CODE (@0), HONOR_NANS (TREE_OPERAND (@0, 0))) == TREE_CODE (@1)
2548 && operand_equal_p (TREE_OPERAND (@0, 0), TREE_OPERAND (@1, 0), 0)
2549 && operand_equal_p (TREE_OPERAND (@0, 1), TREE_OPERAND (@1, 1), 0))
2552 /* A ? B : B -> B. */
2557 /* !A ? B : C -> A ? C : B. */
2559 (cnd (logical_inverted_value truth_valued_p@0) @1 @2)
2562 /* A + (B vcmp C ? 1 : 0) -> A - (B vcmp C ? -1 : 0), since vector comparisons
2563 return all -1 or all 0 results. */
2564 /* ??? We could instead convert all instances of the vec_cond to negate,
2565 but that isn't necessarily a win on its own. */
2567 (plus:c @3 (view_convert? (vec_cond:s @0 integer_each_onep@1 integer_zerop@2)))
2568 (if (VECTOR_TYPE_P (type)
2569 && TYPE_VECTOR_SUBPARTS (type) == TYPE_VECTOR_SUBPARTS (TREE_TYPE (@1))
2570 && (TYPE_MODE (TREE_TYPE (type))
2571 == TYPE_MODE (TREE_TYPE (TREE_TYPE (@1)))))
2572 (minus @3 (view_convert (vec_cond @0 (negate @1) @2)))))
2574 /* ... likewise A - (B vcmp C ? 1 : 0) -> A + (B vcmp C ? -1 : 0). */
2576 (minus @3 (view_convert? (vec_cond:s @0 integer_each_onep@1 integer_zerop@2)))
2577 (if (VECTOR_TYPE_P (type)
2578 && TYPE_VECTOR_SUBPARTS (type) == TYPE_VECTOR_SUBPARTS (TREE_TYPE (@1))
2579 && (TYPE_MODE (TREE_TYPE (type))
2580 == TYPE_MODE (TREE_TYPE (TREE_TYPE (@1)))))
2581 (plus @3 (view_convert (vec_cond @0 (negate @1) @2)))))
2584 /* Simplifications of comparisons. */
2586 /* See if we can reduce the magnitude of a constant involved in a
2587 comparison by changing the comparison code. This is a canonicalization
2588 formerly done by maybe_canonicalize_comparison_1. */
2592 (cmp @0 INTEGER_CST@1)
2593 (if (tree_int_cst_sgn (@1) == -1)
2594 (acmp @0 { wide_int_to_tree (TREE_TYPE (@1), wi::add (@1, 1)); }))))
2598 (cmp @0 INTEGER_CST@1)
2599 (if (tree_int_cst_sgn (@1) == 1)
2600 (acmp @0 { wide_int_to_tree (TREE_TYPE (@1), wi::sub (@1, 1)); }))))
2603 /* We can simplify a logical negation of a comparison to the
2604 inverted comparison. As we cannot compute an expression
2605 operator using invert_tree_comparison we have to simulate
2606 that with expression code iteration. */
2607 (for cmp (tcc_comparison)
2608 icmp (inverted_tcc_comparison)
2609 ncmp (inverted_tcc_comparison_with_nans)
2610 /* Ideally we'd like to combine the following two patterns
2611 and handle some more cases by using
2612 (logical_inverted_value (cmp @0 @1))
2613 here but for that genmatch would need to "inline" that.
2614 For now implement what forward_propagate_comparison did. */
2616 (bit_not (cmp @0 @1))
2617 (if (VECTOR_TYPE_P (type)
2618 || (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1))
2619 /* Comparison inversion may be impossible for trapping math,
2620 invert_tree_comparison will tell us. But we can't use
2621 a computed operator in the replacement tree thus we have
2622 to play the trick below. */
2623 (with { enum tree_code ic = invert_tree_comparison
2624 (cmp, HONOR_NANS (@0)); }
2630 (bit_xor (cmp @0 @1) integer_truep)
2631 (with { enum tree_code ic = invert_tree_comparison
2632 (cmp, HONOR_NANS (@0)); }
2638 /* Transform comparisons of the form X - Y CMP 0 to X CMP Y.
2639 ??? The transformation is valid for the other operators if overflow
2640 is undefined for the type, but performing it here badly interacts
2641 with the transformation in fold_cond_expr_with_comparison which
2642 attempts to synthetize ABS_EXPR. */
2645 (cmp (minus@2 @0 @1) integer_zerop)
2646 (if (single_use (@2))
2649 /* Transform comparisons of the form X * C1 CMP 0 to X CMP 0 in the
2650 signed arithmetic case. That form is created by the compiler
2651 often enough for folding it to be of value. One example is in
2652 computing loop trip counts after Operator Strength Reduction. */
2653 (for cmp (simple_comparison)
2654 scmp (swapped_simple_comparison)
2656 (cmp (mult@3 @0 INTEGER_CST@1) integer_zerop@2)
2657 /* Handle unfolded multiplication by zero. */
2658 (if (integer_zerop (@1))
2660 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2661 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2663 /* If @1 is negative we swap the sense of the comparison. */
2664 (if (tree_int_cst_sgn (@1) < 0)
2668 /* Simplify comparison of something with itself. For IEEE
2669 floating-point, we can only do some of these simplifications. */
2673 (if (! FLOAT_TYPE_P (TREE_TYPE (@0))
2674 || ! HONOR_NANS (@0))
2675 { constant_boolean_node (true, type); }
2676 (if (cmp != EQ_EXPR)
2682 || ! FLOAT_TYPE_P (TREE_TYPE (@0))
2683 || ! HONOR_NANS (@0))
2684 { constant_boolean_node (false, type); })))
2685 (for cmp (unle unge uneq)
2688 { constant_boolean_node (true, type); }))
2689 (for cmp (unlt ungt)
2695 (if (!flag_trapping_math)
2696 { constant_boolean_node (false, type); }))
2698 /* Fold ~X op ~Y as Y op X. */
2699 (for cmp (simple_comparison)
2701 (cmp (bit_not@2 @0) (bit_not@3 @1))
2702 (if (single_use (@2) && single_use (@3))
2705 /* Fold ~X op C as X op' ~C, where op' is the swapped comparison. */
2706 (for cmp (simple_comparison)
2707 scmp (swapped_simple_comparison)
2709 (cmp (bit_not@2 @0) CONSTANT_CLASS_P@1)
2710 (if (single_use (@2)
2711 && (TREE_CODE (@1) == INTEGER_CST || TREE_CODE (@1) == VECTOR_CST))
2712 (scmp @0 (bit_not @1)))))
2714 (for cmp (simple_comparison)
2715 /* Fold (double)float1 CMP (double)float2 into float1 CMP float2. */
2717 (cmp (convert@2 @0) (convert? @1))
2718 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
2719 && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@2))
2720 == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0)))
2721 && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@2))
2722 == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1))))
2725 tree type1 = TREE_TYPE (@1);
2726 if (TREE_CODE (@1) == REAL_CST && !DECIMAL_FLOAT_TYPE_P (type1))
2728 REAL_VALUE_TYPE orig = TREE_REAL_CST (@1);
2729 if (TYPE_PRECISION (type1) > TYPE_PRECISION (float_type_node)
2730 && exact_real_truncate (TYPE_MODE (float_type_node), &orig))
2731 type1 = float_type_node;
2732 if (TYPE_PRECISION (type1) > TYPE_PRECISION (double_type_node)
2733 && exact_real_truncate (TYPE_MODE (double_type_node), &orig))
2734 type1 = double_type_node;
2737 = (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type1)
2738 ? TREE_TYPE (@0) : type1);
2740 (if (TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (newtype))
2741 (cmp (convert:newtype @0) (convert:newtype @1))))))
2745 /* IEEE doesn't distinguish +0 and -0 in comparisons. */
2747 /* a CMP (-0) -> a CMP 0 */
2748 (if (REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@1)))
2749 (cmp @0 { build_real (TREE_TYPE (@1), dconst0); }))
2750 /* x != NaN is always true, other ops are always false. */
2751 (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
2752 && ! HONOR_SNANS (@1))
2753 { constant_boolean_node (cmp == NE_EXPR, type); })
2754 /* Fold comparisons against infinity. */
2755 (if (REAL_VALUE_ISINF (TREE_REAL_CST (@1))
2756 && MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (@1))))
2759 REAL_VALUE_TYPE max;
2760 enum tree_code code = cmp;
2761 bool neg = REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1));
2763 code = swap_tree_comparison (code);
2766 /* x > +Inf is always false, if with ignore sNANs. */
2767 (if (code == GT_EXPR
2768 && ! HONOR_SNANS (@0))
2769 { constant_boolean_node (false, type); })
2770 (if (code == LE_EXPR)
2771 /* x <= +Inf is always true, if we don't case about NaNs. */
2772 (if (! HONOR_NANS (@0))
2773 { constant_boolean_node (true, type); }
2774 /* x <= +Inf is the same as x == x, i.e. !isnan(x). */
2776 /* x == +Inf and x >= +Inf are always equal to x > DBL_MAX. */
2777 (if (code == EQ_EXPR || code == GE_EXPR)
2778 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
2780 (lt @0 { build_real (TREE_TYPE (@0), max); })
2781 (gt @0 { build_real (TREE_TYPE (@0), max); }))))
2782 /* x < +Inf is always equal to x <= DBL_MAX. */
2783 (if (code == LT_EXPR)
2784 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
2786 (ge @0 { build_real (TREE_TYPE (@0), max); })
2787 (le @0 { build_real (TREE_TYPE (@0), max); }))))
2788 /* x != +Inf is always equal to !(x > DBL_MAX). */
2789 (if (code == NE_EXPR)
2790 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
2791 (if (! HONOR_NANS (@0))
2793 (ge @0 { build_real (TREE_TYPE (@0), max); })
2794 (le @0 { build_real (TREE_TYPE (@0), max); }))
2796 (bit_xor (lt @0 { build_real (TREE_TYPE (@0), max); })
2797 { build_one_cst (type); })
2798 (bit_xor (gt @0 { build_real (TREE_TYPE (@0), max); })
2799 { build_one_cst (type); }))))))))))
2801 /* If this is a comparison of a real constant with a PLUS_EXPR
2802 or a MINUS_EXPR of a real constant, we can convert it into a
2803 comparison with a revised real constant as long as no overflow
2804 occurs when unsafe_math_optimizations are enabled. */
2805 (if (flag_unsafe_math_optimizations)
2806 (for op (plus minus)
2808 (cmp (op @0 REAL_CST@1) REAL_CST@2)
2811 tree tem = const_binop (op == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR,
2812 TREE_TYPE (@1), @2, @1);
2814 (if (tem && !TREE_OVERFLOW (tem))
2815 (cmp @0 { tem; }))))))
2817 /* Likewise, we can simplify a comparison of a real constant with
2818 a MINUS_EXPR whose first operand is also a real constant, i.e.
2819 (c1 - x) < c2 becomes x > c1-c2. Reordering is allowed on
2820 floating-point types only if -fassociative-math is set. */
2821 (if (flag_associative_math)
2823 (cmp (minus REAL_CST@0 @1) REAL_CST@2)
2824 (with { tree tem = const_binop (MINUS_EXPR, TREE_TYPE (@1), @0, @2); }
2825 (if (tem && !TREE_OVERFLOW (tem))
2826 (cmp { tem; } @1)))))
2828 /* Fold comparisons against built-in math functions. */
2829 (if (flag_unsafe_math_optimizations
2830 && ! flag_errno_math)
2833 (cmp (sq @0) REAL_CST@1)
2835 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
2837 /* sqrt(x) < y is always false, if y is negative. */
2838 (if (cmp == EQ_EXPR || cmp == LT_EXPR || cmp == LE_EXPR)
2839 { constant_boolean_node (false, type); })
2840 /* sqrt(x) > y is always true, if y is negative and we
2841 don't care about NaNs, i.e. negative values of x. */
2842 (if (cmp == NE_EXPR || !HONOR_NANS (@0))
2843 { constant_boolean_node (true, type); })
2844 /* sqrt(x) > y is the same as x >= 0, if y is negative. */
2845 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })))
2846 (if (real_equal (TREE_REAL_CST_PTR (@1), &dconst0))
2848 /* sqrt(x) < 0 is always false. */
2849 (if (cmp == LT_EXPR)
2850 { constant_boolean_node (false, type); })
2851 /* sqrt(x) >= 0 is always true if we don't care about NaNs. */
2852 (if (cmp == GE_EXPR && !HONOR_NANS (@0))
2853 { constant_boolean_node (true, type); })
2854 /* sqrt(x) <= 0 -> x == 0. */
2855 (if (cmp == LE_EXPR)
2857 /* Otherwise sqrt(x) cmp 0 -> x cmp 0. Here cmp can be >=, >,
2858 == or !=. In the last case:
2860 (sqrt(x) != 0) == (NaN != 0) == true == (x != 0)
2862 if x is negative or NaN. Due to -funsafe-math-optimizations,
2863 the results for other x follow from natural arithmetic. */
2865 (if (cmp == GT_EXPR || cmp == GE_EXPR)
2869 real_arithmetic (&c2, MULT_EXPR,
2870 &TREE_REAL_CST (@1), &TREE_REAL_CST (@1));
2871 real_convert (&c2, TYPE_MODE (TREE_TYPE (@0)), &c2);
2873 (if (REAL_VALUE_ISINF (c2))
2874 /* sqrt(x) > y is x == +Inf, when y is very large. */
2875 (if (HONOR_INFINITIES (@0))
2876 (eq @0 { build_real (TREE_TYPE (@0), c2); })
2877 { constant_boolean_node (false, type); })
2878 /* sqrt(x) > c is the same as x > c*c. */
2879 (cmp @0 { build_real (TREE_TYPE (@0), c2); }))))
2880 (if (cmp == LT_EXPR || cmp == LE_EXPR)
2884 real_arithmetic (&c2, MULT_EXPR,
2885 &TREE_REAL_CST (@1), &TREE_REAL_CST (@1));
2886 real_convert (&c2, TYPE_MODE (TREE_TYPE (@0)), &c2);
2888 (if (REAL_VALUE_ISINF (c2))
2890 /* sqrt(x) < y is always true, when y is a very large
2891 value and we don't care about NaNs or Infinities. */
2892 (if (! HONOR_NANS (@0) && ! HONOR_INFINITIES (@0))
2893 { constant_boolean_node (true, type); })
2894 /* sqrt(x) < y is x != +Inf when y is very large and we
2895 don't care about NaNs. */
2896 (if (! HONOR_NANS (@0))
2897 (ne @0 { build_real (TREE_TYPE (@0), c2); }))
2898 /* sqrt(x) < y is x >= 0 when y is very large and we
2899 don't care about Infinities. */
2900 (if (! HONOR_INFINITIES (@0))
2901 (ge @0 { build_real (TREE_TYPE (@0), dconst0); }))
2902 /* sqrt(x) < y is x >= 0 && x != +Inf, when y is large. */
2905 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
2906 (ne @0 { build_real (TREE_TYPE (@0), c2); }))))
2907 /* sqrt(x) < c is the same as x < c*c, if we ignore NaNs. */
2908 (if (! HONOR_NANS (@0))
2909 (cmp @0 { build_real (TREE_TYPE (@0), c2); })
2910 /* sqrt(x) < c is the same as x >= 0 && x < c*c. */
2913 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
2914 (cmp @0 { build_real (TREE_TYPE (@0), c2); })))))))))
2915 /* Transform sqrt(x) cmp sqrt(y) -> x cmp y. */
2917 (cmp (sq @0) (sq @1))
2918 (if (! HONOR_NANS (@0))
2921 /* Optimize various special cases of (FTYPE) N CMP CST. */
2922 (for cmp (lt le eq ne ge gt)
2923 icmp (le le eq ne ge ge)
2925 (cmp (float @0) REAL_CST@1)
2926 (if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (@1))
2927 && ! DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1)))
2930 tree itype = TREE_TYPE (@0);
2931 signop isign = TYPE_SIGN (itype);
2932 format_helper fmt (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (@1))));
2933 const REAL_VALUE_TYPE *cst = TREE_REAL_CST_PTR (@1);
2934 /* Be careful to preserve any potential exceptions due to
2935 NaNs. qNaNs are ok in == or != context.
2936 TODO: relax under -fno-trapping-math or
2937 -fno-signaling-nans. */
2939 = real_isnan (cst) && (cst->signalling
2940 || (cmp != EQ_EXPR && cmp != NE_EXPR));
2941 /* INT?_MIN is power-of-two so it takes
2942 only one mantissa bit. */
2943 bool signed_p = isign == SIGNED;
2944 bool itype_fits_ftype_p
2945 = TYPE_PRECISION (itype) - signed_p <= significand_size (fmt);
2947 /* TODO: allow non-fitting itype and SNaNs when
2948 -fno-trapping-math. */
2949 (if (itype_fits_ftype_p && ! exception_p)
2952 REAL_VALUE_TYPE imin, imax;
2953 real_from_integer (&imin, fmt, wi::min_value (itype), isign);
2954 real_from_integer (&imax, fmt, wi::max_value (itype), isign);
2956 REAL_VALUE_TYPE icst;
2957 if (cmp == GT_EXPR || cmp == GE_EXPR)
2958 real_ceil (&icst, fmt, cst);
2959 else if (cmp == LT_EXPR || cmp == LE_EXPR)
2960 real_floor (&icst, fmt, cst);
2962 real_trunc (&icst, fmt, cst);
2964 bool cst_int_p = real_identical (&icst, cst);
2966 bool overflow_p = false;
2968 = real_to_integer (&icst, &overflow_p, TYPE_PRECISION (itype));
2971 /* Optimize cases when CST is outside of ITYPE's range. */
2972 (if (real_compare (LT_EXPR, cst, &imin))
2973 { constant_boolean_node (cmp == GT_EXPR || cmp == GE_EXPR || cmp == NE_EXPR,
2975 (if (real_compare (GT_EXPR, cst, &imax))
2976 { constant_boolean_node (cmp == LT_EXPR || cmp == LE_EXPR || cmp == NE_EXPR,
2978 /* Remove cast if CST is an integer representable by ITYPE. */
2980 (cmp @0 { gcc_assert (!overflow_p);
2981 wide_int_to_tree (itype, icst_val); })
2983 /* When CST is fractional, optimize
2984 (FTYPE) N == CST -> 0
2985 (FTYPE) N != CST -> 1. */
2986 (if (cmp == EQ_EXPR || cmp == NE_EXPR)
2987 { constant_boolean_node (cmp == NE_EXPR, type); })
2988 /* Otherwise replace with sensible integer constant. */
2991 gcc_checking_assert (!overflow_p);
2993 (icmp @0 { wide_int_to_tree (itype, icst_val); })))))))))
2995 /* Fold A /[ex] B CMP C to A CMP B * C. */
2998 (cmp (exact_div @0 @1) INTEGER_CST@2)
2999 (if (!integer_zerop (@1))
3000 (if (wi::eq_p (@2, 0))
3002 (if (TREE_CODE (@1) == INTEGER_CST)
3006 wide_int prod = wi::mul (@2, @1, TYPE_SIGN (TREE_TYPE (@1)), &ovf);
3009 { constant_boolean_node (cmp == NE_EXPR, type); }
3010 (cmp @0 { wide_int_to_tree (TREE_TYPE (@0), prod); }))))))))
3011 (for cmp (lt le gt ge)
3013 (cmp (exact_div @0 INTEGER_CST@1) INTEGER_CST@2)
3014 (if (wi::gt_p (@1, 0, TYPE_SIGN (TREE_TYPE (@1))))
3018 wide_int prod = wi::mul (@2, @1, TYPE_SIGN (TREE_TYPE (@1)), &ovf);
3021 { constant_boolean_node (wi::lt_p (@2, 0, TYPE_SIGN (TREE_TYPE (@2)))
3022 != (cmp == LT_EXPR || cmp == LE_EXPR), type); }
3023 (cmp @0 { wide_int_to_tree (TREE_TYPE (@0), prod); }))))))
3025 /* Unordered tests if either argument is a NaN. */
3027 (bit_ior (unordered @0 @0) (unordered @1 @1))
3028 (if (types_match (@0, @1))
3031 (bit_and (ordered @0 @0) (ordered @1 @1))
3032 (if (types_match (@0, @1))
3035 (bit_ior:c (unordered @0 @0) (unordered:c@2 @0 @1))
3038 (bit_and:c (ordered @0 @0) (ordered:c@2 @0 @1))
3041 /* Simple range test simplifications. */
3042 /* A < B || A >= B -> true. */
3043 (for test1 (lt le le le ne ge)
3044 test2 (ge gt ge ne eq ne)
3046 (bit_ior:c (test1 @0 @1) (test2 @0 @1))
3047 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
3048 || VECTOR_INTEGER_TYPE_P (TREE_TYPE (@0)))
3049 { constant_boolean_node (true, type); })))
3050 /* A < B && A >= B -> false. */
3051 (for test1 (lt lt lt le ne eq)
3052 test2 (ge gt eq gt eq gt)
3054 (bit_and:c (test1 @0 @1) (test2 @0 @1))
3055 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
3056 || VECTOR_INTEGER_TYPE_P (TREE_TYPE (@0)))
3057 { constant_boolean_node (false, type); })))
3059 /* A & (2**N - 1) <= 2**K - 1 -> A & (2**N - 2**K) == 0
3060 A & (2**N - 1) > 2**K - 1 -> A & (2**N - 2**K) != 0
3062 Note that comparisons
3063 A & (2**N - 1) < 2**K -> A & (2**N - 2**K) == 0
3064 A & (2**N - 1) >= 2**K -> A & (2**N - 2**K) != 0
3065 will be canonicalized to above so there's no need to
3072 (cmp (bit_and@0 @1 INTEGER_CST@2) INTEGER_CST@3)
3073 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)))
3076 tree ty = TREE_TYPE (@0);
3077 unsigned prec = TYPE_PRECISION (ty);
3078 wide_int mask = wi::to_wide (@2, prec);
3079 wide_int rhs = wi::to_wide (@3, prec);
3080 signop sgn = TYPE_SIGN (ty);
3082 (if ((mask & (mask + 1)) == 0 && wi::gt_p (rhs, 0, sgn)
3083 && (rhs & (rhs + 1)) == 0 && wi::ge_p (mask, rhs, sgn))
3084 (eqcmp (bit_and @1 { wide_int_to_tree (ty, mask - rhs); })
3085 { build_zero_cst (ty); }))))))
3087 /* -A CMP -B -> B CMP A. */
3088 (for cmp (tcc_comparison)
3089 scmp (swapped_tcc_comparison)
3091 (cmp (negate @0) (negate @1))
3092 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
3093 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3094 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
3097 (cmp (negate @0) CONSTANT_CLASS_P@1)
3098 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
3099 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3100 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
3101 (with { tree tem = const_unop (NEGATE_EXPR, TREE_TYPE (@0), @1); }
3102 (if (tem && !TREE_OVERFLOW (tem))
3103 (scmp @0 { tem; }))))))
3105 /* Convert ABS_EXPR<x> == 0 or ABS_EXPR<x> != 0 to x == 0 or x != 0. */
3108 (op (abs @0) zerop@1)
3111 /* From fold_sign_changed_comparison and fold_widened_comparison. */
3112 (for cmp (simple_comparison)
3114 (cmp (convert@0 @00) (convert?@1 @10))
3115 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
3116 /* Disable this optimization if we're casting a function pointer
3117 type on targets that require function pointer canonicalization. */
3118 && !(targetm.have_canonicalize_funcptr_for_compare ()
3119 && TREE_CODE (TREE_TYPE (@00)) == POINTER_TYPE
3120 && TREE_CODE (TREE_TYPE (TREE_TYPE (@00))) == FUNCTION_TYPE)
3122 (if (TYPE_PRECISION (TREE_TYPE (@00)) == TYPE_PRECISION (TREE_TYPE (@0))
3123 && (TREE_CODE (@10) == INTEGER_CST
3124 || (@1 != @10 && types_match (TREE_TYPE (@10), TREE_TYPE (@00))))
3125 && (TYPE_UNSIGNED (TREE_TYPE (@00)) == TYPE_UNSIGNED (TREE_TYPE (@0))
3128 && (POINTER_TYPE_P (TREE_TYPE (@00)) == POINTER_TYPE_P (TREE_TYPE (@0))))
3129 /* ??? The special-casing of INTEGER_CST conversion was in the original
3130 code and here to avoid a spurious overflow flag on the resulting
3131 constant which fold_convert produces. */
3132 (if (TREE_CODE (@1) == INTEGER_CST)
3133 (cmp @00 { force_fit_type (TREE_TYPE (@00), wi::to_widest (@1), 0,
3134 TREE_OVERFLOW (@1)); })
3135 (cmp @00 (convert @1)))
3137 (if (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (TREE_TYPE (@00)))
3138 /* If possible, express the comparison in the shorter mode. */
3139 (if ((cmp == EQ_EXPR || cmp == NE_EXPR
3140 || TYPE_UNSIGNED (TREE_TYPE (@0)) == TYPE_UNSIGNED (TREE_TYPE (@00))
3141 || (!TYPE_UNSIGNED (TREE_TYPE (@0))
3142 && TYPE_UNSIGNED (TREE_TYPE (@00))))
3143 && (types_match (TREE_TYPE (@10), TREE_TYPE (@00))
3144 || ((TYPE_PRECISION (TREE_TYPE (@00))
3145 >= TYPE_PRECISION (TREE_TYPE (@10)))
3146 && (TYPE_UNSIGNED (TREE_TYPE (@00))
3147 == TYPE_UNSIGNED (TREE_TYPE (@10))))
3148 || (TREE_CODE (@10) == INTEGER_CST
3149 && INTEGRAL_TYPE_P (TREE_TYPE (@00))
3150 && int_fits_type_p (@10, TREE_TYPE (@00)))))
3151 (cmp @00 (convert @10))
3152 (if (TREE_CODE (@10) == INTEGER_CST
3153 && INTEGRAL_TYPE_P (TREE_TYPE (@00))
3154 && !int_fits_type_p (@10, TREE_TYPE (@00)))
3157 tree min = lower_bound_in_type (TREE_TYPE (@10), TREE_TYPE (@00));
3158 tree max = upper_bound_in_type (TREE_TYPE (@10), TREE_TYPE (@00));
3159 bool above = integer_nonzerop (const_binop (LT_EXPR, type, max, @10));
3160 bool below = integer_nonzerop (const_binop (LT_EXPR, type, @10, min));
3162 (if (above || below)
3163 (if (cmp == EQ_EXPR || cmp == NE_EXPR)
3164 { constant_boolean_node (cmp == EQ_EXPR ? false : true, type); }
3165 (if (cmp == LT_EXPR || cmp == LE_EXPR)
3166 { constant_boolean_node (above ? true : false, type); }
3167 (if (cmp == GT_EXPR || cmp == GE_EXPR)
3168 { constant_boolean_node (above ? false : true, type); }))))))))))))
3171 /* A local variable can never be pointed to by
3172 the default SSA name of an incoming parameter.
3173 SSA names are canonicalized to 2nd place. */
3175 (cmp addr@0 SSA_NAME@1)
3176 (if (SSA_NAME_IS_DEFAULT_DEF (@1)
3177 && TREE_CODE (SSA_NAME_VAR (@1)) == PARM_DECL)
3178 (with { tree base = get_base_address (TREE_OPERAND (@0, 0)); }
3179 (if (TREE_CODE (base) == VAR_DECL
3180 && auto_var_in_fn_p (base, current_function_decl))
3181 (if (cmp == NE_EXPR)
3182 { constant_boolean_node (true, type); }
3183 { constant_boolean_node (false, type); }))))))
3185 /* Equality compare simplifications from fold_binary */
3188 /* If we have (A | C) == D where C & ~D != 0, convert this into 0.
3189 Similarly for NE_EXPR. */
3191 (cmp (convert?@3 (bit_ior @0 INTEGER_CST@1)) INTEGER_CST@2)
3192 (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0))
3193 && wi::bit_and_not (@1, @2) != 0)
3194 { constant_boolean_node (cmp == NE_EXPR, type); }))
3196 /* (X ^ Y) == 0 becomes X == Y, and (X ^ Y) != 0 becomes X != Y. */
3198 (cmp (bit_xor @0 @1) integer_zerop)
3201 /* (X ^ Y) == Y becomes X == 0.
3202 Likewise (X ^ Y) == X becomes Y == 0. */
3204 (cmp:c (bit_xor:c @0 @1) @0)
3205 (cmp @1 { build_zero_cst (TREE_TYPE (@1)); }))
3207 /* (X ^ C1) op C2 can be rewritten as X op (C1 ^ C2). */
3209 (cmp (convert?@3 (bit_xor @0 INTEGER_CST@1)) INTEGER_CST@2)
3210 (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0)))
3211 (cmp @0 (bit_xor @1 (convert @2)))))
3214 (cmp (convert? addr@0) integer_zerop)
3215 (if (tree_single_nonzero_warnv_p (@0, NULL))
3216 { constant_boolean_node (cmp == NE_EXPR, type); })))
3218 /* If we have (A & C) == C where C is a power of 2, convert this into
3219 (A & C) != 0. Similarly for NE_EXPR. */
3223 (cmp (bit_and@2 @0 integer_pow2p@1) @1)
3224 (icmp @2 { build_zero_cst (TREE_TYPE (@0)); })))
3226 /* If we have (A & C) != 0 ? D : 0 where C and D are powers of 2,
3227 convert this into a shift followed by ANDing with D. */
3230 (ne (bit_and @0 integer_pow2p@1) integer_zerop)
3231 integer_pow2p@2 integer_zerop)
3233 int shift = wi::exact_log2 (@2) - wi::exact_log2 (@1);
3237 (lshift (convert @0) { build_int_cst (integer_type_node, shift); }) @2)
3239 (convert (rshift @0 { build_int_cst (integer_type_node, -shift); })) @2))))
3241 /* If we have (A & C) != 0 where C is the sign bit of A, convert
3242 this into A < 0. Similarly for (A & C) == 0 into A >= 0. */
3246 (cmp (bit_and (convert?@2 @0) integer_pow2p@1) integer_zerop)
3247 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
3248 && type_has_mode_precision_p (TREE_TYPE (@0))
3249 && element_precision (@2) >= element_precision (@0)
3250 && wi::only_sign_bit_p (@1, element_precision (@0)))
3251 (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
3252 (ncmp (convert:stype @0) { build_zero_cst (stype); })))))
3254 /* If we have A < 0 ? C : 0 where C is a power of 2, convert
3255 this into a right shift or sign extension followed by ANDing with C. */
3258 (lt @0 integer_zerop)
3259 integer_pow2p@1 integer_zerop)
3260 (if (!TYPE_UNSIGNED (TREE_TYPE (@0)))
3262 int shift = element_precision (@0) - wi::exact_log2 (@1) - 1;
3266 (convert (rshift @0 { build_int_cst (integer_type_node, shift); }))
3268 /* Otherwise ctype must be wider than TREE_TYPE (@0) and pure
3269 sign extension followed by AND with C will achieve the effect. */
3270 (bit_and (convert @0) @1)))))
3272 /* When the addresses are not directly of decls compare base and offset.
3273 This implements some remaining parts of fold_comparison address
3274 comparisons but still no complete part of it. Still it is good
3275 enough to make fold_stmt not regress when not dispatching to fold_binary. */
3276 (for cmp (simple_comparison)
3278 (cmp (convert1?@2 addr@0) (convert2? addr@1))
3281 HOST_WIDE_INT off0, off1;
3282 tree base0 = get_addr_base_and_unit_offset (TREE_OPERAND (@0, 0), &off0);
3283 tree base1 = get_addr_base_and_unit_offset (TREE_OPERAND (@1, 0), &off1);
3284 if (base0 && TREE_CODE (base0) == MEM_REF)
3286 off0 += mem_ref_offset (base0).to_short_addr ();
3287 base0 = TREE_OPERAND (base0, 0);
3289 if (base1 && TREE_CODE (base1) == MEM_REF)
3291 off1 += mem_ref_offset (base1).to_short_addr ();
3292 base1 = TREE_OPERAND (base1, 0);
3295 (if (base0 && base1)
3299 /* Punt in GENERIC on variables with value expressions;
3300 the value expressions might point to fields/elements
3301 of other vars etc. */
3303 && ((VAR_P (base0) && DECL_HAS_VALUE_EXPR_P (base0))
3304 || (VAR_P (base1) && DECL_HAS_VALUE_EXPR_P (base1))))
3306 else if (decl_in_symtab_p (base0)
3307 && decl_in_symtab_p (base1))
3308 equal = symtab_node::get_create (base0)
3309 ->equal_address_to (symtab_node::get_create (base1));
3310 else if ((DECL_P (base0)
3311 || TREE_CODE (base0) == SSA_NAME
3312 || TREE_CODE (base0) == STRING_CST)
3314 || TREE_CODE (base1) == SSA_NAME
3315 || TREE_CODE (base1) == STRING_CST))
3316 equal = (base0 == base1);
3320 (if (cmp == EQ_EXPR)
3321 { constant_boolean_node (off0 == off1, type); })
3322 (if (cmp == NE_EXPR)
3323 { constant_boolean_node (off0 != off1, type); })
3324 (if (cmp == LT_EXPR)
3325 { constant_boolean_node (off0 < off1, type); })
3326 (if (cmp == LE_EXPR)
3327 { constant_boolean_node (off0 <= off1, type); })
3328 (if (cmp == GE_EXPR)
3329 { constant_boolean_node (off0 >= off1, type); })
3330 (if (cmp == GT_EXPR)
3331 { constant_boolean_node (off0 > off1, type); }))
3333 && DECL_P (base0) && DECL_P (base1)
3334 /* If we compare this as integers require equal offset. */
3335 && (!INTEGRAL_TYPE_P (TREE_TYPE (@2))
3338 (if (cmp == EQ_EXPR)
3339 { constant_boolean_node (false, type); })
3340 (if (cmp == NE_EXPR)
3341 { constant_boolean_node (true, type); })))))))))
3343 /* Simplify pointer equality compares using PTA. */
3347 (if (POINTER_TYPE_P (TREE_TYPE (@0))
3348 && ptrs_compare_unequal (@0, @1))
3349 { neeq == EQ_EXPR ? boolean_false_node : boolean_true_node; })))
3351 /* PR70920: Transform (intptr_t)x eq/ne CST to x eq/ne (typeof x) CST.
3352 and (typeof ptr_cst) x eq/ne ptr_cst to x eq/ne (typeof x) CST.
3353 Disable the transform if either operand is pointer to function.
3354 This broke pr22051-2.c for arm where function pointer
3355 canonicalizaion is not wanted. */
3359 (cmp (convert @0) INTEGER_CST@1)
3360 (if ((POINTER_TYPE_P (TREE_TYPE (@0)) && !FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@0)))
3361 && INTEGRAL_TYPE_P (TREE_TYPE (@1)))
3362 || (INTEGRAL_TYPE_P (TREE_TYPE (@0)) && POINTER_TYPE_P (TREE_TYPE (@1))
3363 && !FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@1)))))
3364 (cmp @0 (convert @1)))))
3366 /* Non-equality compare simplifications from fold_binary */
3367 (for cmp (lt gt le ge)
3368 /* Comparisons with the highest or lowest possible integer of
3369 the specified precision will have known values. */
3371 (cmp (convert?@2 @0) INTEGER_CST@1)
3372 (if ((INTEGRAL_TYPE_P (TREE_TYPE (@1)) || POINTER_TYPE_P (TREE_TYPE (@1)))
3373 && tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@0)))
3376 tree arg1_type = TREE_TYPE (@1);
3377 unsigned int prec = TYPE_PRECISION (arg1_type);
3378 wide_int max = wi::max_value (arg1_type);
3379 wide_int signed_max = wi::max_value (prec, SIGNED);
3380 wide_int min = wi::min_value (arg1_type);
3383 (if (wi::eq_p (@1, max))
3385 (if (cmp == GT_EXPR)
3386 { constant_boolean_node (false, type); })
3387 (if (cmp == GE_EXPR)
3389 (if (cmp == LE_EXPR)
3390 { constant_boolean_node (true, type); })
3391 (if (cmp == LT_EXPR)
3393 (if (wi::eq_p (@1, min))
3395 (if (cmp == LT_EXPR)
3396 { constant_boolean_node (false, type); })
3397 (if (cmp == LE_EXPR)
3399 (if (cmp == GE_EXPR)
3400 { constant_boolean_node (true, type); })
3401 (if (cmp == GT_EXPR)
3403 (if (wi::eq_p (@1, max - 1))
3405 (if (cmp == GT_EXPR)
3406 (eq @2 { wide_int_to_tree (TREE_TYPE (@1), wi::add (@1, 1)); }))
3407 (if (cmp == LE_EXPR)
3408 (ne @2 { wide_int_to_tree (TREE_TYPE (@1), wi::add (@1, 1)); }))))
3409 (if (wi::eq_p (@1, min + 1))
3411 (if (cmp == GE_EXPR)
3412 (ne @2 { wide_int_to_tree (TREE_TYPE (@1), wi::sub (@1, 1)); }))
3413 (if (cmp == LT_EXPR)
3414 (eq @2 { wide_int_to_tree (TREE_TYPE (@1), wi::sub (@1, 1)); }))))
3415 (if (wi::eq_p (@1, signed_max)
3416 && TYPE_UNSIGNED (arg1_type)
3417 /* We will flip the signedness of the comparison operator
3418 associated with the mode of @1, so the sign bit is
3419 specified by this mode. Check that @1 is the signed
3420 max associated with this sign bit. */
3421 && prec == GET_MODE_PRECISION (SCALAR_INT_TYPE_MODE (arg1_type))
3422 /* signed_type does not work on pointer types. */
3423 && INTEGRAL_TYPE_P (arg1_type))
3424 /* The following case also applies to X < signed_max+1
3425 and X >= signed_max+1 because previous transformations. */
3426 (if (cmp == LE_EXPR || cmp == GT_EXPR)
3427 (with { tree st = signed_type_for (arg1_type); }
3428 (if (cmp == LE_EXPR)
3429 (ge (convert:st @0) { build_zero_cst (st); })
3430 (lt (convert:st @0) { build_zero_cst (st); }))))))))))
3432 (for cmp (unordered ordered unlt unle ungt unge uneq ltgt)
3433 /* If the second operand is NaN, the result is constant. */
3436 (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
3437 && (cmp != LTGT_EXPR || ! flag_trapping_math))
3438 { constant_boolean_node (cmp == ORDERED_EXPR || cmp == LTGT_EXPR
3439 ? false : true, type); })))
3441 /* bool_var != 0 becomes bool_var. */
3443 (ne @0 integer_zerop)
3444 (if (TREE_CODE (TREE_TYPE (@0)) == BOOLEAN_TYPE
3445 && types_match (type, TREE_TYPE (@0)))
3447 /* bool_var == 1 becomes bool_var. */
3449 (eq @0 integer_onep)
3450 (if (TREE_CODE (TREE_TYPE (@0)) == BOOLEAN_TYPE
3451 && types_match (type, TREE_TYPE (@0)))
3454 bool_var == 0 becomes !bool_var or
3455 bool_var != 1 becomes !bool_var
3456 here because that only is good in assignment context as long
3457 as we require a tcc_comparison in GIMPLE_CONDs where we'd
3458 replace if (x == 0) with tem = ~x; if (tem != 0) which is
3459 clearly less optimal and which we'll transform again in forwprop. */
3461 /* When one argument is a constant, overflow detection can be simplified.
3462 Currently restricted to single use so as not to interfere too much with
3463 ADD_OVERFLOW detection in tree-ssa-math-opts.c.
3464 A + CST CMP A -> A CMP' CST' */
3465 (for cmp (lt le ge gt)
3468 (cmp:c (plus@2 @0 INTEGER_CST@1) @0)
3469 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
3470 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))
3473 (out @0 { wide_int_to_tree (TREE_TYPE (@0), wi::max_value
3474 (TYPE_PRECISION (TREE_TYPE (@0)), UNSIGNED) - @1); }))))
3476 /* To detect overflow in unsigned A - B, A < B is simpler than A - B > A.
3477 However, the detection logic for SUB_OVERFLOW in tree-ssa-math-opts.c
3478 expects the long form, so we restrict the transformation for now. */
3481 (cmp:c (minus@2 @0 @1) @0)
3482 (if (single_use (@2)
3483 && ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3484 && TYPE_UNSIGNED (TREE_TYPE (@0))
3485 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
3488 /* Testing for overflow is unnecessary if we already know the result. */
3493 (cmp:c (realpart (IFN_SUB_OVERFLOW@2 @0 @1)) @0)
3494 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
3495 && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
3496 (out (imagpart @2) { build_zero_cst (TREE_TYPE (@0)); }))))
3501 (cmp:c (realpart (IFN_ADD_OVERFLOW:c@2 @0 @1)) @0)
3502 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
3503 && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
3504 (out (imagpart @2) { build_zero_cst (TREE_TYPE (@0)); }))))
3506 /* For unsigned operands, -1 / B < A checks whether A * B would overflow.
3507 Simplify it to __builtin_mul_overflow (A, B, <unused>). */
3511 (cmp:c (trunc_div:s integer_all_onesp @1) @0)
3512 (if (TYPE_UNSIGNED (TREE_TYPE (@0)) && !VECTOR_TYPE_P (TREE_TYPE (@0)))
3513 (with { tree t = TREE_TYPE (@0), cpx = build_complex_type (t); }
3514 (out (imagpart (IFN_MUL_OVERFLOW:cpx @0 @1)) { build_zero_cst (t); })))))
3516 /* Simplification of math builtins. These rules must all be optimizations
3517 as well as IL simplifications. If there is a possibility that the new
3518 form could be a pessimization, the rule should go in the canonicalization
3519 section that follows this one.
3521 Rules can generally go in this section if they satisfy one of
3524 - the rule describes an identity
3526 - the rule replaces calls with something as simple as addition or
3529 - the rule contains unary calls only and simplifies the surrounding
3530 arithmetic. (The idea here is to exclude non-unary calls in which
3531 one operand is constant and in which the call is known to be cheap
3532 when the operand has that value.) */
3534 (if (flag_unsafe_math_optimizations)
3535 /* Simplify sqrt(x) * sqrt(x) -> x. */
3537 (mult (SQRT@1 @0) @1)
3538 (if (!HONOR_SNANS (type))
3541 (for op (plus minus)
3542 /* Simplify (A / C) +- (B / C) -> (A +- B) / C. */
3546 (rdiv (op @0 @2) @1)))
3548 /* Simplify sqrt(x) * sqrt(y) -> sqrt(x*y). */
3549 (for root (SQRT CBRT)
3551 (mult (root:s @0) (root:s @1))
3552 (root (mult @0 @1))))
3554 /* Simplify expN(x) * expN(y) -> expN(x+y). */
3555 (for exps (EXP EXP2 EXP10 POW10)
3557 (mult (exps:s @0) (exps:s @1))
3558 (exps (plus @0 @1))))
3560 /* Simplify a/root(b/c) into a*root(c/b). */
3561 (for root (SQRT CBRT)
3563 (rdiv @0 (root:s (rdiv:s @1 @2)))
3564 (mult @0 (root (rdiv @2 @1)))))
3566 /* Simplify x/expN(y) into x*expN(-y). */
3567 (for exps (EXP EXP2 EXP10 POW10)
3569 (rdiv @0 (exps:s @1))
3570 (mult @0 (exps (negate @1)))))
3572 (for logs (LOG LOG2 LOG10 LOG10)
3573 exps (EXP EXP2 EXP10 POW10)
3574 /* logN(expN(x)) -> x. */
3578 /* expN(logN(x)) -> x. */
3583 /* Optimize logN(func()) for various exponential functions. We
3584 want to determine the value "x" and the power "exponent" in
3585 order to transform logN(x**exponent) into exponent*logN(x). */
3586 (for logs (LOG LOG LOG LOG2 LOG2 LOG2 LOG10 LOG10)
3587 exps (EXP2 EXP10 POW10 EXP EXP10 POW10 EXP EXP2)
3590 (if (SCALAR_FLOAT_TYPE_P (type))
3596 /* Prepare to do logN(exp(exponent)) -> exponent*logN(e). */
3597 x = build_real_truncate (type, dconst_e ());
3600 /* Prepare to do logN(exp2(exponent)) -> exponent*logN(2). */
3601 x = build_real (type, dconst2);
3605 /* Prepare to do logN(exp10(exponent)) -> exponent*logN(10). */
3607 REAL_VALUE_TYPE dconst10;
3608 real_from_integer (&dconst10, VOIDmode, 10, SIGNED);
3609 x = build_real (type, dconst10);
3616 (mult (logs { x; }) @0)))))
3624 (if (SCALAR_FLOAT_TYPE_P (type))
3630 /* Prepare to do logN(sqrt(x)) -> 0.5*logN(x). */
3631 x = build_real (type, dconsthalf);
3634 /* Prepare to do logN(cbrt(x)) -> (1/3)*logN(x). */
3635 x = build_real_truncate (type, dconst_third ());
3641 (mult { x; } (logs @0))))))
3643 /* logN(pow(x,exponent)) -> exponent*logN(x). */
3644 (for logs (LOG LOG2 LOG10)
3648 (mult @1 (logs @0))))
3650 /* pow(C,x) -> exp(log(C)*x) if C > 0. */
3655 (pows REAL_CST@0 @1)
3656 (if (real_compare (GT_EXPR, TREE_REAL_CST_PTR (@0), &dconst0)
3657 && real_isfinite (TREE_REAL_CST_PTR (@0)))
3658 (exps (mult (logs @0) @1)))))
3663 exps (EXP EXP2 EXP10 POW10)
3664 /* sqrt(expN(x)) -> expN(x*0.5). */
3667 (exps (mult @0 { build_real (type, dconsthalf); })))
3668 /* cbrt(expN(x)) -> expN(x/3). */
3671 (exps (mult @0 { build_real_truncate (type, dconst_third ()); })))
3672 /* pow(expN(x), y) -> expN(x*y). */
3675 (exps (mult @0 @1))))
3677 /* tan(atan(x)) -> x. */
3684 /* cabs(x+0i) or cabs(0+xi) -> abs(x). */
3686 (CABS (complex:C @0 real_zerop@1))
3689 /* trunc(trunc(x)) -> trunc(x), etc. */
3690 (for fns (TRUNC FLOOR CEIL ROUND NEARBYINT RINT)
3694 /* f(x) -> x if x is integer valued and f does nothing for such values. */
3695 (for fns (TRUNC FLOOR CEIL ROUND NEARBYINT RINT)
3697 (fns integer_valued_real_p@0)
3700 /* hypot(x,0) and hypot(0,x) -> abs(x). */
3702 (HYPOT:c @0 real_zerop@1)
3705 /* pow(1,x) -> 1. */
3707 (POW real_onep@0 @1)
3711 /* copysign(x,x) -> x. */
3716 /* copysign(x,y) -> fabs(x) if y is nonnegative. */
3717 (COPYSIGN @0 tree_expr_nonnegative_p@1)
3720 (for scale (LDEXP SCALBN SCALBLN)
3721 /* ldexp(0, x) -> 0. */
3723 (scale real_zerop@0 @1)
3725 /* ldexp(x, 0) -> x. */
3727 (scale @0 integer_zerop@1)
3729 /* ldexp(x, y) -> x if x is +-Inf or NaN. */
3731 (scale REAL_CST@0 @1)
3732 (if (!real_isfinite (TREE_REAL_CST_PTR (@0)))
3735 /* Canonicalization of sequences of math builtins. These rules represent
3736 IL simplifications but are not necessarily optimizations.
3738 The sincos pass is responsible for picking "optimal" implementations
3739 of math builtins, which may be more complicated and can sometimes go
3740 the other way, e.g. converting pow into a sequence of sqrts.
3741 We only want to do these canonicalizations before the pass has run. */
3743 (if (flag_unsafe_math_optimizations && canonicalize_math_p ())
3744 /* Simplify tan(x) * cos(x) -> sin(x). */
3746 (mult:c (TAN:s @0) (COS:s @0))
3749 /* Simplify x * pow(x,c) -> pow(x,c+1). */
3751 (mult:c @0 (POW:s @0 REAL_CST@1))
3752 (if (!TREE_OVERFLOW (@1))
3753 (POW @0 (plus @1 { build_one_cst (type); }))))
3755 /* Simplify sin(x) / cos(x) -> tan(x). */
3757 (rdiv (SIN:s @0) (COS:s @0))
3760 /* Simplify cos(x) / sin(x) -> 1 / tan(x). */
3762 (rdiv (COS:s @0) (SIN:s @0))
3763 (rdiv { build_one_cst (type); } (TAN @0)))
3765 /* Simplify sin(x) / tan(x) -> cos(x). */
3767 (rdiv (SIN:s @0) (TAN:s @0))
3768 (if (! HONOR_NANS (@0)
3769 && ! HONOR_INFINITIES (@0))
3772 /* Simplify tan(x) / sin(x) -> 1.0 / cos(x). */
3774 (rdiv (TAN:s @0) (SIN:s @0))
3775 (if (! HONOR_NANS (@0)
3776 && ! HONOR_INFINITIES (@0))
3777 (rdiv { build_one_cst (type); } (COS @0))))
3779 /* Simplify pow(x,y) * pow(x,z) -> pow(x,y+z). */
3781 (mult (POW:s @0 @1) (POW:s @0 @2))
3782 (POW @0 (plus @1 @2)))
3784 /* Simplify pow(x,y) * pow(z,y) -> pow(x*z,y). */
3786 (mult (POW:s @0 @1) (POW:s @2 @1))
3787 (POW (mult @0 @2) @1))
3789 /* Simplify powi(x,y) * powi(z,y) -> powi(x*z,y). */
3791 (mult (POWI:s @0 @1) (POWI:s @2 @1))
3792 (POWI (mult @0 @2) @1))
3794 /* Simplify pow(x,c) / x -> pow(x,c-1). */
3796 (rdiv (POW:s @0 REAL_CST@1) @0)
3797 (if (!TREE_OVERFLOW (@1))
3798 (POW @0 (minus @1 { build_one_cst (type); }))))
3800 /* Simplify x / pow (y,z) -> x * pow(y,-z). */
3802 (rdiv @0 (POW:s @1 @2))
3803 (mult @0 (POW @1 (negate @2))))
3808 /* sqrt(sqrt(x)) -> pow(x,1/4). */
3811 (pows @0 { build_real (type, dconst_quarter ()); }))
3812 /* sqrt(cbrt(x)) -> pow(x,1/6). */
3815 (pows @0 { build_real_truncate (type, dconst_sixth ()); }))
3816 /* cbrt(sqrt(x)) -> pow(x,1/6). */
3819 (pows @0 { build_real_truncate (type, dconst_sixth ()); }))
3820 /* cbrt(cbrt(x)) -> pow(x,1/9), iff x is nonnegative. */
3822 (cbrts (cbrts tree_expr_nonnegative_p@0))
3823 (pows @0 { build_real_truncate (type, dconst_ninth ()); }))
3824 /* sqrt(pow(x,y)) -> pow(|x|,y*0.5). */
3826 (sqrts (pows @0 @1))
3827 (pows (abs @0) (mult @1 { build_real (type, dconsthalf); })))
3828 /* cbrt(pow(x,y)) -> pow(x,y/3), iff x is nonnegative. */
3830 (cbrts (pows tree_expr_nonnegative_p@0 @1))
3831 (pows @0 (mult @1 { build_real_truncate (type, dconst_third ()); })))
3832 /* pow(sqrt(x),y) -> pow(x,y*0.5). */
3834 (pows (sqrts @0) @1)
3835 (pows @0 (mult @1 { build_real (type, dconsthalf); })))
3836 /* pow(cbrt(x),y) -> pow(x,y/3) iff x is nonnegative. */
3838 (pows (cbrts tree_expr_nonnegative_p@0) @1)
3839 (pows @0 (mult @1 { build_real_truncate (type, dconst_third ()); })))
3840 /* pow(pow(x,y),z) -> pow(x,y*z) iff x is nonnegative. */
3842 (pows (pows tree_expr_nonnegative_p@0 @1) @2)
3843 (pows @0 (mult @1 @2))))
3845 /* cabs(x+xi) -> fabs(x)*sqrt(2). */
3847 (CABS (complex @0 @0))
3848 (mult (abs @0) { build_real_truncate (type, dconst_sqrt2 ()); }))
3850 /* hypot(x,x) -> fabs(x)*sqrt(2). */
3853 (mult (abs @0) { build_real_truncate (type, dconst_sqrt2 ()); }))
3855 /* cexp(x+yi) -> exp(x)*cexpi(y). */
3860 (cexps compositional_complex@0)
3861 (if (targetm.libc_has_function (function_c99_math_complex))
3863 (mult (exps@1 (realpart @0)) (realpart (cexpis:type@2 (imagpart @0))))
3864 (mult @1 (imagpart @2)))))))
3866 (if (canonicalize_math_p ())
3867 /* floor(x) -> trunc(x) if x is nonnegative. */
3871 (floors tree_expr_nonnegative_p@0)
3874 (match double_value_p
3876 (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == double_type_node)))
3877 (for froms (BUILT_IN_TRUNCL
3889 /* truncl(extend(x)) -> extend(trunc(x)), etc., if x is a double. */
3890 (if (optimize && canonicalize_math_p ())
3892 (froms (convert double_value_p@0))
3893 (convert (tos @0)))))
3895 (match float_value_p
3897 (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == float_type_node)))
3898 (for froms (BUILT_IN_TRUNCL BUILT_IN_TRUNC
3899 BUILT_IN_FLOORL BUILT_IN_FLOOR
3900 BUILT_IN_CEILL BUILT_IN_CEIL
3901 BUILT_IN_ROUNDL BUILT_IN_ROUND
3902 BUILT_IN_NEARBYINTL BUILT_IN_NEARBYINT
3903 BUILT_IN_RINTL BUILT_IN_RINT)
3904 tos (BUILT_IN_TRUNCF BUILT_IN_TRUNCF
3905 BUILT_IN_FLOORF BUILT_IN_FLOORF
3906 BUILT_IN_CEILF BUILT_IN_CEILF
3907 BUILT_IN_ROUNDF BUILT_IN_ROUNDF
3908 BUILT_IN_NEARBYINTF BUILT_IN_NEARBYINTF
3909 BUILT_IN_RINTF BUILT_IN_RINTF)
3910 /* truncl(extend(x)) and trunc(extend(x)) -> extend(truncf(x)), etc.,
3912 (if (optimize && canonicalize_math_p ()
3913 && targetm.libc_has_function (function_c99_misc))
3915 (froms (convert float_value_p@0))
3916 (convert (tos @0)))))
3918 (for froms (XFLOORL XCEILL XROUNDL XRINTL)
3919 tos (XFLOOR XCEIL XROUND XRINT)
3920 /* llfloorl(extend(x)) -> llfloor(x), etc., if x is a double. */
3921 (if (optimize && canonicalize_math_p ())
3923 (froms (convert double_value_p@0))
3926 (for froms (XFLOORL XCEILL XROUNDL XRINTL
3927 XFLOOR XCEIL XROUND XRINT)
3928 tos (XFLOORF XCEILF XROUNDF XRINTF)
3929 /* llfloorl(extend(x)) and llfloor(extend(x)) -> llfloorf(x), etc.,
3931 (if (optimize && canonicalize_math_p ())
3933 (froms (convert float_value_p@0))
3936 (if (canonicalize_math_p ())
3937 /* xfloor(x) -> fix_trunc(x) if x is nonnegative. */
3938 (for floors (IFLOOR LFLOOR LLFLOOR)
3940 (floors tree_expr_nonnegative_p@0)
3943 (if (canonicalize_math_p ())
3944 /* xfloor(x) -> fix_trunc(x), etc., if x is integer valued. */
3945 (for fns (IFLOOR LFLOOR LLFLOOR
3947 IROUND LROUND LLROUND)
3949 (fns integer_valued_real_p@0)
3951 (if (!flag_errno_math)
3952 /* xrint(x) -> fix_trunc(x), etc., if x is integer valued. */
3953 (for rints (IRINT LRINT LLRINT)
3955 (rints integer_valued_real_p@0)
3958 (if (canonicalize_math_p ())
3959 (for ifn (IFLOOR ICEIL IROUND IRINT)
3960 lfn (LFLOOR LCEIL LROUND LRINT)
3961 llfn (LLFLOOR LLCEIL LLROUND LLRINT)
3962 /* Canonicalize iround (x) to lround (x) on ILP32 targets where
3963 sizeof (int) == sizeof (long). */
3964 (if (TYPE_PRECISION (integer_type_node)
3965 == TYPE_PRECISION (long_integer_type_node))
3968 (lfn:long_integer_type_node @0)))
3969 /* Canonicalize llround (x) to lround (x) on LP64 targets where
3970 sizeof (long long) == sizeof (long). */
3971 (if (TYPE_PRECISION (long_long_integer_type_node)
3972 == TYPE_PRECISION (long_integer_type_node))
3975 (lfn:long_integer_type_node @0)))))
3977 /* cproj(x) -> x if we're ignoring infinities. */
3980 (if (!HONOR_INFINITIES (type))
3983 /* If the real part is inf and the imag part is known to be
3984 nonnegative, return (inf + 0i). */
3986 (CPROJ (complex REAL_CST@0 tree_expr_nonnegative_p@1))
3987 (if (real_isinf (TREE_REAL_CST_PTR (@0)))
3988 { build_complex_inf (type, false); }))
3990 /* If the imag part is inf, return (inf+I*copysign(0,imag)). */
3992 (CPROJ (complex @0 REAL_CST@1))
3993 (if (real_isinf (TREE_REAL_CST_PTR (@1)))
3994 { build_complex_inf (type, TREE_REAL_CST_PTR (@1)->sign); }))
4000 (pows @0 REAL_CST@1)
4002 const REAL_VALUE_TYPE *value = TREE_REAL_CST_PTR (@1);
4003 REAL_VALUE_TYPE tmp;
4006 /* pow(x,0) -> 1. */
4007 (if (real_equal (value, &dconst0))
4008 { build_real (type, dconst1); })
4009 /* pow(x,1) -> x. */
4010 (if (real_equal (value, &dconst1))
4012 /* pow(x,-1) -> 1/x. */
4013 (if (real_equal (value, &dconstm1))
4014 (rdiv { build_real (type, dconst1); } @0))
4015 /* pow(x,0.5) -> sqrt(x). */
4016 (if (flag_unsafe_math_optimizations
4017 && canonicalize_math_p ()
4018 && real_equal (value, &dconsthalf))
4020 /* pow(x,1/3) -> cbrt(x). */
4021 (if (flag_unsafe_math_optimizations
4022 && canonicalize_math_p ()
4023 && (tmp = real_value_truncate (TYPE_MODE (type), dconst_third ()),
4024 real_equal (value, &tmp)))
4027 /* powi(1,x) -> 1. */
4029 (POWI real_onep@0 @1)
4033 (POWI @0 INTEGER_CST@1)
4035 /* powi(x,0) -> 1. */
4036 (if (wi::eq_p (@1, 0))
4037 { build_real (type, dconst1); })
4038 /* powi(x,1) -> x. */
4039 (if (wi::eq_p (@1, 1))
4041 /* powi(x,-1) -> 1/x. */
4042 (if (wi::eq_p (@1, -1))
4043 (rdiv { build_real (type, dconst1); } @0))))
4045 /* Narrowing of arithmetic and logical operations.
4047 These are conceptually similar to the transformations performed for
4048 the C/C++ front-ends by shorten_binary_op and shorten_compare. Long
4049 term we want to move all that code out of the front-ends into here. */
4051 /* If we have a narrowing conversion of an arithmetic operation where
4052 both operands are widening conversions from the same type as the outer
4053 narrowing conversion. Then convert the innermost operands to a suitable
4054 unsigned type (to avoid introducing undefined behavior), perform the
4055 operation and convert the result to the desired type. */
4056 (for op (plus minus)
4058 (convert (op:s (convert@2 @0) (convert?@3 @1)))
4059 (if (INTEGRAL_TYPE_P (type)
4060 /* We check for type compatibility between @0 and @1 below,
4061 so there's no need to check that @1/@3 are integral types. */
4062 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
4063 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
4064 /* The precision of the type of each operand must match the
4065 precision of the mode of each operand, similarly for the
4067 && type_has_mode_precision_p (TREE_TYPE (@0))
4068 && type_has_mode_precision_p (TREE_TYPE (@1))
4069 && type_has_mode_precision_p (type)
4070 /* The inner conversion must be a widening conversion. */
4071 && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
4072 && types_match (@0, type)
4073 && (types_match (@0, @1)
4074 /* Or the second operand is const integer or converted const
4075 integer from valueize. */
4076 || TREE_CODE (@1) == INTEGER_CST))
4077 (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
4078 (op @0 (convert @1))
4079 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
4080 (convert (op (convert:utype @0)
4081 (convert:utype @1))))))))
4083 /* This is another case of narrowing, specifically when there's an outer
4084 BIT_AND_EXPR which masks off bits outside the type of the innermost
4085 operands. Like the previous case we have to convert the operands
4086 to unsigned types to avoid introducing undefined behavior for the
4087 arithmetic operation. */
4088 (for op (minus plus)
4090 (bit_and (op:s (convert@2 @0) (convert@3 @1)) INTEGER_CST@4)
4091 (if (INTEGRAL_TYPE_P (type)
4092 /* We check for type compatibility between @0 and @1 below,
4093 so there's no need to check that @1/@3 are integral types. */
4094 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
4095 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
4096 /* The precision of the type of each operand must match the
4097 precision of the mode of each operand, similarly for the
4099 && type_has_mode_precision_p (TREE_TYPE (@0))
4100 && type_has_mode_precision_p (TREE_TYPE (@1))
4101 && type_has_mode_precision_p (type)
4102 /* The inner conversion must be a widening conversion. */
4103 && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
4104 && types_match (@0, @1)
4105 && (tree_int_cst_min_precision (@4, TYPE_SIGN (TREE_TYPE (@0)))
4106 <= TYPE_PRECISION (TREE_TYPE (@0)))
4107 && (wi::bit_and (@4, wi::mask (TYPE_PRECISION (TREE_TYPE (@0)),
4108 true, TYPE_PRECISION (type))) == 0))
4109 (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
4110 (with { tree ntype = TREE_TYPE (@0); }
4111 (convert (bit_and (op @0 @1) (convert:ntype @4))))
4112 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
4113 (convert (bit_and (op (convert:utype @0) (convert:utype @1))
4114 (convert:utype @4))))))))
4116 /* Transform (@0 < @1 and @0 < @2) to use min,
4117 (@0 > @1 and @0 > @2) to use max */
4118 (for op (lt le gt ge)
4119 ext (min min max max)
4121 (bit_and (op:cs @0 @1) (op:cs @0 @2))
4122 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
4123 && TREE_CODE (@0) != INTEGER_CST)
4124 (op @0 (ext @1 @2)))))
4127 /* signbit(x) -> 0 if x is nonnegative. */
4128 (SIGNBIT tree_expr_nonnegative_p@0)
4129 { integer_zero_node; })
4132 /* signbit(x) -> x<0 if x doesn't have signed zeros. */
4134 (if (!HONOR_SIGNED_ZEROS (@0))
4135 (convert (lt @0 { build_real (TREE_TYPE (@0), dconst0); }))))
4137 /* Transform comparisons of the form X +- C1 CMP C2 to X CMP C2 -+ C1. */
4139 (for op (plus minus)
4142 (cmp (op@3 @0 INTEGER_CST@1) INTEGER_CST@2)
4143 (if (!TREE_OVERFLOW (@1) && !TREE_OVERFLOW (@2)
4144 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0))
4145 && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@0))
4146 && !TYPE_SATURATING (TREE_TYPE (@0)))
4147 (with { tree res = int_const_binop (rop, @2, @1); }
4148 (if (TREE_OVERFLOW (res)
4149 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
4150 { constant_boolean_node (cmp == NE_EXPR, type); }
4151 (if (single_use (@3))
4152 (cmp @0 { res; }))))))))
4153 (for cmp (lt le gt ge)
4154 (for op (plus minus)
4157 (cmp (op@3 @0 INTEGER_CST@1) INTEGER_CST@2)
4158 (if (!TREE_OVERFLOW (@1) && !TREE_OVERFLOW (@2)
4159 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
4160 (with { tree res = int_const_binop (rop, @2, @1); }
4161 (if (TREE_OVERFLOW (res))
4163 fold_overflow_warning (("assuming signed overflow does not occur "
4164 "when simplifying conditional to constant"),
4165 WARN_STRICT_OVERFLOW_CONDITIONAL);
4166 bool less = cmp == LE_EXPR || cmp == LT_EXPR;
4167 /* wi::ges_p (@2, 0) should be sufficient for a signed type. */
4168 bool ovf_high = wi::lt_p (@1, 0, TYPE_SIGN (TREE_TYPE (@1)))
4169 != (op == MINUS_EXPR);
4170 constant_boolean_node (less == ovf_high, type);
4172 (if (single_use (@3))
4175 fold_overflow_warning (("assuming signed overflow does not occur "
4176 "when changing X +- C1 cmp C2 to "
4178 WARN_STRICT_OVERFLOW_COMPARISON);
4180 (cmp @0 { res; })))))))))
4182 /* Canonicalizations of BIT_FIELD_REFs. */
4185 (BIT_FIELD_REF @0 @1 @2)
4187 (if (TREE_CODE (TREE_TYPE (@0)) == COMPLEX_TYPE
4188 && tree_int_cst_equal (@1, TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))))
4190 (if (integer_zerop (@2))
4191 (view_convert (realpart @0)))
4192 (if (tree_int_cst_equal (@2, TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))))
4193 (view_convert (imagpart @0)))))
4194 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
4195 && INTEGRAL_TYPE_P (type)
4196 /* On GIMPLE this should only apply to register arguments. */
4197 && (! GIMPLE || is_gimple_reg (@0))
4198 /* A bit-field-ref that referenced the full argument can be stripped. */
4199 && ((compare_tree_int (@1, TYPE_PRECISION (TREE_TYPE (@0))) == 0
4200 && integer_zerop (@2))
4201 /* Low-parts can be reduced to integral conversions.
4202 ??? The following doesn't work for PDP endian. */
4203 || (BYTES_BIG_ENDIAN == WORDS_BIG_ENDIAN
4204 /* Don't even think about BITS_BIG_ENDIAN. */
4205 && TYPE_PRECISION (TREE_TYPE (@0)) % BITS_PER_UNIT == 0
4206 && TYPE_PRECISION (type) % BITS_PER_UNIT == 0
4207 && compare_tree_int (@2, (BYTES_BIG_ENDIAN
4208 ? (TYPE_PRECISION (TREE_TYPE (@0))
4209 - TYPE_PRECISION (type))
4213 /* Simplify vector extracts. */
4216 (BIT_FIELD_REF CONSTRUCTOR@0 @1 @2)
4217 (if (VECTOR_TYPE_P (TREE_TYPE (@0))
4218 && (types_match (type, TREE_TYPE (TREE_TYPE (@0)))
4219 || (VECTOR_TYPE_P (type)
4220 && types_match (TREE_TYPE (type), TREE_TYPE (TREE_TYPE (@0))))))
4223 tree ctor = (TREE_CODE (@0) == SSA_NAME
4224 ? gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0)) : @0);
4225 tree eltype = TREE_TYPE (TREE_TYPE (ctor));
4226 unsigned HOST_WIDE_INT width = tree_to_uhwi (TYPE_SIZE (eltype));
4227 unsigned HOST_WIDE_INT n = tree_to_uhwi (@1);
4228 unsigned HOST_WIDE_INT idx = tree_to_uhwi (@2);
4231 && (idx % width) == 0
4233 && ((idx + n) / width) <= TYPE_VECTOR_SUBPARTS (TREE_TYPE (ctor)))
4238 /* Constructor elements can be subvectors. */
4239 unsigned HOST_WIDE_INT k = 1;
4240 if (CONSTRUCTOR_NELTS (ctor) != 0)
4242 tree cons_elem = TREE_TYPE (CONSTRUCTOR_ELT (ctor, 0)->value);
4243 if (TREE_CODE (cons_elem) == VECTOR_TYPE)
4244 k = TYPE_VECTOR_SUBPARTS (cons_elem);
4248 /* We keep an exact subset of the constructor elements. */
4249 (if ((idx % k) == 0 && (n % k) == 0)
4250 (if (CONSTRUCTOR_NELTS (ctor) == 0)
4251 { build_constructor (type, NULL); }
4258 (if (idx < CONSTRUCTOR_NELTS (ctor))
4259 { CONSTRUCTOR_ELT (ctor, idx)->value; }
4260 { build_zero_cst (type); })
4262 vec<constructor_elt, va_gc> *vals;
4263 vec_alloc (vals, n);
4264 for (unsigned i = 0;
4265 i < n && idx + i < CONSTRUCTOR_NELTS (ctor); ++i)
4266 CONSTRUCTOR_APPEND_ELT (vals, NULL_TREE,
4267 CONSTRUCTOR_ELT (ctor, idx + i)->value);
4268 build_constructor (type, vals);
4270 /* The bitfield references a single constructor element. */
4271 (if (idx + n <= (idx / k + 1) * k)
4273 (if (CONSTRUCTOR_NELTS (ctor) <= idx / k)
4274 { build_zero_cst (type); })
4276 { CONSTRUCTOR_ELT (ctor, idx / k)->value; })
4277 (BIT_FIELD_REF { CONSTRUCTOR_ELT (ctor, idx / k)->value; }
4278 @1 { bitsize_int ((idx % k) * width); })))))))))
4280 /* Simplify a bit extraction from a bit insertion for the cases with
4281 the inserted element fully covering the extraction or the insertion
4282 not touching the extraction. */
4284 (BIT_FIELD_REF (bit_insert @0 @1 @ipos) @rsize @rpos)
4287 unsigned HOST_WIDE_INT isize;
4288 if (INTEGRAL_TYPE_P (TREE_TYPE (@1)))
4289 isize = TYPE_PRECISION (TREE_TYPE (@1));
4291 isize = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (@1)));
4294 (if (wi::leu_p (@ipos, @rpos)
4295 && wi::leu_p (wi::add (@rpos, @rsize), wi::add (@ipos, isize)))
4296 (BIT_FIELD_REF @1 @rsize { wide_int_to_tree (bitsizetype,
4297 wi::sub (@rpos, @ipos)); }))
4298 (if (wi::geu_p (@ipos, wi::add (@rpos, @rsize))
4299 || wi::geu_p (@rpos, wi::add (@ipos, isize)))
4300 (BIT_FIELD_REF @0 @rsize @rpos)))))