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-2015 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
38 (define_operator_list tcc_comparison
39 lt le eq ne ge gt unordered ordered unlt unle ungt unge uneq ltgt)
40 (define_operator_list inverted_tcc_comparison
41 ge gt ne eq lt le ordered unordered ge gt le lt ltgt uneq)
42 (define_operator_list inverted_tcc_comparison_with_nans
43 unge ungt ne eq unlt unle ordered unordered ge gt le lt ltgt uneq)
44 (define_operator_list swapped_tcc_comparison
45 gt ge eq ne le lt unordered ordered ungt unge unlt unle uneq ltgt)
46 (define_operator_list simple_comparison lt le eq ne ge gt)
47 (define_operator_list swapped_simple_comparison gt ge eq ne le lt)
49 (define_operator_list LOG BUILT_IN_LOGF BUILT_IN_LOG BUILT_IN_LOGL)
50 (define_operator_list EXP BUILT_IN_EXPF BUILT_IN_EXP BUILT_IN_EXPL)
51 (define_operator_list LOG2 BUILT_IN_LOG2F BUILT_IN_LOG2 BUILT_IN_LOG2L)
52 (define_operator_list EXP2 BUILT_IN_EXP2F BUILT_IN_EXP2 BUILT_IN_EXP2L)
53 (define_operator_list LOG10 BUILT_IN_LOG10F BUILT_IN_LOG10 BUILT_IN_LOG10L)
54 (define_operator_list EXP10 BUILT_IN_EXP10F BUILT_IN_EXP10 BUILT_IN_EXP10L)
55 (define_operator_list POW BUILT_IN_POWF BUILT_IN_POW BUILT_IN_POWL)
56 (define_operator_list POW10 BUILT_IN_POW10F BUILT_IN_POW10 BUILT_IN_POW10L)
57 (define_operator_list SQRT BUILT_IN_SQRTF BUILT_IN_SQRT BUILT_IN_SQRTL)
58 (define_operator_list CBRT BUILT_IN_CBRTF BUILT_IN_CBRT BUILT_IN_CBRTL)
59 (define_operator_list SIN BUILT_IN_SINF BUILT_IN_SIN BUILT_IN_SINL)
60 (define_operator_list COS BUILT_IN_COSF BUILT_IN_COS BUILT_IN_COSL)
61 (define_operator_list TAN BUILT_IN_TANF BUILT_IN_TAN BUILT_IN_TANL)
62 (define_operator_list COSH BUILT_IN_COSHF BUILT_IN_COSH BUILT_IN_COSHL)
63 (define_operator_list CEXPI BUILT_IN_CEXPIF BUILT_IN_CEXPI BUILT_IN_CEXPIL)
64 (define_operator_list CPROJ BUILT_IN_CPROJF BUILT_IN_CPROJ BUILT_IN_CPROJL)
66 /* Simplifications of operations with one constant operand and
67 simplifications to constants or single values. */
69 (for op (plus pointer_plus minus bit_ior bit_xor)
74 /* 0 +p index -> (type)index */
76 (pointer_plus integer_zerop @1)
77 (non_lvalue (convert @1)))
79 /* See if ARG1 is zero and X + ARG1 reduces to X.
80 Likewise if the operands are reversed. */
82 (plus:c @0 real_zerop@1)
83 (if (fold_real_zero_addition_p (type, @1, 0))
86 /* See if ARG1 is zero and X - ARG1 reduces to X. */
88 (minus @0 real_zerop@1)
89 (if (fold_real_zero_addition_p (type, @1, 1))
93 This is unsafe for certain floats even in non-IEEE formats.
94 In IEEE, it is unsafe because it does wrong for NaNs.
95 Also note that operand_equal_p is always false if an operand
99 (if (!FLOAT_TYPE_P (type) || !HONOR_NANS (type))
100 { build_zero_cst (type); }))
103 (mult @0 integer_zerop@1)
106 /* Maybe fold x * 0 to 0. The expressions aren't the same
107 when x is NaN, since x * 0 is also NaN. Nor are they the
108 same in modes with signed zeros, since multiplying a
109 negative value by 0 gives -0, not +0. */
111 (mult @0 real_zerop@1)
112 (if (!HONOR_NANS (type) && !HONOR_SIGNED_ZEROS (type))
115 /* In IEEE floating point, x*1 is not equivalent to x for snans.
116 Likewise for complex arithmetic with signed zeros. */
119 (if (!HONOR_SNANS (type)
120 && (!HONOR_SIGNED_ZEROS (type)
121 || !COMPLEX_FLOAT_TYPE_P (type)))
124 /* Transform x * -1.0 into -x. */
126 (mult @0 real_minus_onep)
127 (if (!HONOR_SNANS (type)
128 && (!HONOR_SIGNED_ZEROS (type)
129 || !COMPLEX_FLOAT_TYPE_P (type)))
132 /* Make sure to preserve divisions by zero. This is the reason why
133 we don't simplify x / x to 1 or 0 / x to 0. */
134 (for op (mult trunc_div ceil_div floor_div round_div exact_div)
140 (for div (trunc_div ceil_div floor_div round_div exact_div)
142 (div @0 integer_minus_onep@1)
143 (if (!TYPE_UNSIGNED (type))
146 /* For unsigned integral types, FLOOR_DIV_EXPR is the same as
147 TRUNC_DIV_EXPR. Rewrite into the latter in this case. */
150 (if ((INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
151 && TYPE_UNSIGNED (type))
154 /* Combine two successive divisions. Note that combining ceil_div
155 and floor_div is trickier and combining round_div even more so. */
156 (for div (trunc_div exact_div)
158 (div (div @0 INTEGER_CST@1) INTEGER_CST@2)
161 wide_int mul = wi::mul (@1, @2, TYPE_SIGN (type), &overflow_p);
164 (div @0 { wide_int_to_tree (type, mul); })
165 (if (TYPE_UNSIGNED (type)
166 || mul != wi::min_value (TYPE_PRECISION (type), SIGNED))
167 { build_zero_cst (type); })))))
169 /* Optimize A / A to 1.0 if we don't care about
170 NaNs or Infinities. */
173 (if (FLOAT_TYPE_P (type)
174 && ! HONOR_NANS (type)
175 && ! HONOR_INFINITIES (type))
176 { build_one_cst (type); }))
178 /* Optimize -A / A to -1.0 if we don't care about
179 NaNs or Infinities. */
181 (rdiv:c @0 (negate @0))
182 (if (FLOAT_TYPE_P (type)
183 && ! HONOR_NANS (type)
184 && ! HONOR_INFINITIES (type))
185 { build_minus_one_cst (type); }))
187 /* In IEEE floating point, x/1 is not equivalent to x for snans. */
190 (if (!HONOR_SNANS (type))
193 /* In IEEE floating point, x/-1 is not equivalent to -x for snans. */
195 (rdiv @0 real_minus_onep)
196 (if (!HONOR_SNANS (type))
199 /* If ARG1 is a constant, we can convert this to a multiply by the
200 reciprocal. This does not have the same rounding properties,
201 so only do this if -freciprocal-math. We can actually
202 always safely do it if ARG1 is a power of two, but it's hard to
203 tell if it is or not in a portable manner. */
204 (for cst (REAL_CST COMPLEX_CST VECTOR_CST)
208 (if (flag_reciprocal_math
211 { tree tem = const_binop (RDIV_EXPR, type, build_one_cst (type), @1); }
213 (mult @0 { tem; } )))
214 (if (cst != COMPLEX_CST)
215 (with { tree inverse = exact_inverse (type, @1); }
217 (mult @0 { inverse; } ))))))))
219 /* Same applies to modulo operations, but fold is inconsistent here
220 and simplifies 0 % x to 0, only preserving literal 0 % 0. */
221 (for mod (ceil_mod floor_mod round_mod trunc_mod)
222 /* 0 % X is always zero. */
224 (mod integer_zerop@0 @1)
225 /* But not for 0 % 0 so that we can get the proper warnings and errors. */
226 (if (!integer_zerop (@1))
228 /* X % 1 is always zero. */
230 (mod @0 integer_onep)
231 { build_zero_cst (type); })
232 /* X % -1 is zero. */
234 (mod @0 integer_minus_onep@1)
235 (if (!TYPE_UNSIGNED (type))
236 { build_zero_cst (type); }))
237 /* (X % Y) % Y is just X % Y. */
239 (mod (mod@2 @0 @1) @1)
241 /* From extract_muldiv_1: (X * C1) % C2 is zero if C1 is a multiple of C2. */
243 (mod (mult @0 INTEGER_CST@1) INTEGER_CST@2)
244 (if (ANY_INTEGRAL_TYPE_P (type)
245 && TYPE_OVERFLOW_UNDEFINED (type)
246 && wi::multiple_of_p (@1, @2, TYPE_SIGN (type)))
247 { build_zero_cst (type); })))
249 /* X % -C is the same as X % C. */
251 (trunc_mod @0 INTEGER_CST@1)
252 (if (TYPE_SIGN (type) == SIGNED
253 && !TREE_OVERFLOW (@1)
255 && !TYPE_OVERFLOW_TRAPS (type)
256 /* Avoid this transformation if C is INT_MIN, i.e. C == -C. */
257 && !sign_bit_p (@1, @1))
258 (trunc_mod @0 (negate @1))))
260 /* X % -Y is the same as X % Y. */
262 (trunc_mod @0 (convert? (negate @1)))
263 (if (!TYPE_UNSIGNED (type)
264 && !TYPE_OVERFLOW_TRAPS (type)
265 && tree_nop_conversion_p (type, TREE_TYPE (@1)))
266 (trunc_mod @0 (convert @1))))
268 /* X - (X / Y) * Y is the same as X % Y. */
270 (minus (convert1? @0) (convert2? (mult (trunc_div @0 @1) @1)))
271 (if ((INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
272 && TYPE_UNSIGNED (TREE_TYPE (@0)) == TYPE_UNSIGNED (type))
273 (trunc_mod (convert @0) (convert @1))))
275 /* Optimize TRUNC_MOD_EXPR by a power of two into a BIT_AND_EXPR,
276 i.e. "X % C" into "X & (C - 1)", if X and C are positive.
277 Also optimize A % (C << N) where C is a power of 2,
278 to A & ((C << N) - 1). */
279 (match (power_of_two_cand @1)
281 (match (power_of_two_cand @1)
282 (lshift INTEGER_CST@1 @2))
283 (for mod (trunc_mod floor_mod)
285 (mod @0 (convert?@3 (power_of_two_cand@1 @2)))
286 (if ((TYPE_UNSIGNED (type)
287 || tree_expr_nonnegative_p (@0))
288 && tree_nop_conversion_p (type, TREE_TYPE (@3))
289 && integer_pow2p (@2) && tree_int_cst_sgn (@2) > 0)
290 (bit_and @0 (convert (minus @1 { build_int_cst (TREE_TYPE (@1), 1); }))))))
292 /* Simplify (unsigned t * 2)/2 -> unsigned t & 0x7FFFFFFF. */
294 (trunc_div (mult @0 integer_pow2p@1) @1)
295 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
296 (bit_and @0 { wide_int_to_tree
297 (type, wi::mask (TYPE_PRECISION (type) - wi::exact_log2 (@1),
298 false, TYPE_PRECISION (type))); })))
300 /* Simplify (unsigned t / 2) * 2 -> unsigned t & ~1. */
302 (mult (trunc_div @0 integer_pow2p@1) @1)
303 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
304 (bit_and @0 (negate @1))))
306 /* Simplify (t * 2) / 2) -> t. */
307 (for div (trunc_div ceil_div floor_div round_div exact_div)
309 (div (mult @0 @1) @1)
310 (if (ANY_INTEGRAL_TYPE_P (type)
311 && TYPE_OVERFLOW_UNDEFINED (type))
315 /* Simplify cos(-x) and cos(|x|) -> cos(x). Similarly for cosh. */
320 /* Simplify pow(-x, y) and pow(|x|,y) -> pow(x,y) if y is an even integer. */
323 (pows (op @0) REAL_CST@1)
324 (with { HOST_WIDE_INT n; }
325 (if (real_isinteger (&TREE_REAL_CST (@1), &n) && (n & 1) == 0)
328 /* Fold (a * (1 << b)) into (a << b) */
330 (mult:c @0 (convert? (lshift integer_onep@1 @2)))
331 (if (! FLOAT_TYPE_P (type)
332 && tree_nop_conversion_p (type, TREE_TYPE (@1)))
335 /* Fold (C1/X)*C2 into (C1*C2)/X. */
337 (mult (rdiv:s REAL_CST@0 @1) REAL_CST@2)
338 (if (flag_associative_math)
340 { tree tem = const_binop (MULT_EXPR, type, @0, @2); }
342 (rdiv { tem; } @1)))))
344 /* Simplify ~X & X as zero. */
346 (bit_and:c (convert? @0) (convert? (bit_not @0)))
347 { build_zero_cst (type); })
349 /* X % Y is smaller than Y. */
352 (cmp (trunc_mod @0 @1) @1)
353 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
354 { constant_boolean_node (cmp == LT_EXPR, type); })))
357 (cmp @1 (trunc_mod @0 @1))
358 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
359 { constant_boolean_node (cmp == GT_EXPR, type); })))
363 (bit_ior @0 integer_all_onesp@1)
368 (bit_and @0 integer_zerop@1)
374 (for op (bit_ior bit_xor plus)
376 (op:c (convert? @0) (convert? (bit_not @0)))
377 (convert { build_all_ones_cst (TREE_TYPE (@0)); })))
382 { build_zero_cst (type); })
384 /* Canonicalize X ^ ~0 to ~X. */
386 (bit_xor @0 integer_all_onesp@1)
391 (bit_and @0 integer_all_onesp)
394 /* x & x -> x, x | x -> x */
395 (for bitop (bit_and bit_ior)
400 /* x + (x & 1) -> (x + 1) & ~1 */
402 (plus:c @0 (bit_and:s @0 integer_onep@1))
403 (bit_and (plus @0 @1) (bit_not @1)))
405 /* x & ~(x & y) -> x & ~y */
406 /* x | ~(x | y) -> x | ~y */
407 (for bitop (bit_and bit_ior)
409 (bitop:c @0 (bit_not (bitop:cs @0 @1)))
410 (bitop @0 (bit_not @1))))
412 /* (x | y) & ~x -> y & ~x */
413 /* (x & y) | ~x -> y | ~x */
414 (for bitop (bit_and bit_ior)
415 rbitop (bit_ior bit_and)
417 (bitop:c (rbitop:c @0 @1) (bit_not@2 @0))
420 /* (x & y) ^ (x | y) -> x ^ y */
422 (bit_xor:c (bit_and @0 @1) (bit_ior @0 @1))
425 /* (x ^ y) ^ (x | y) -> x & y */
427 (bit_xor:c (bit_xor @0 @1) (bit_ior @0 @1))
430 /* (x & y) + (x ^ y) -> x | y */
431 /* (x & y) | (x ^ y) -> x | y */
432 /* (x & y) ^ (x ^ y) -> x | y */
433 (for op (plus bit_ior bit_xor)
435 (op:c (bit_and @0 @1) (bit_xor @0 @1))
438 /* (x & y) + (x | y) -> x + y */
440 (plus:c (bit_and @0 @1) (bit_ior @0 @1))
443 /* (x + y) - (x | y) -> x & y */
445 (minus (plus @0 @1) (bit_ior @0 @1))
446 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
447 && !TYPE_SATURATING (type))
450 /* (x + y) - (x & y) -> x | y */
452 (minus (plus @0 @1) (bit_and @0 @1))
453 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
454 && !TYPE_SATURATING (type))
457 /* (x | y) - (x ^ y) -> x & y */
459 (minus (bit_ior @0 @1) (bit_xor @0 @1))
462 /* (x | y) - (x & y) -> x ^ y */
464 (minus (bit_ior @0 @1) (bit_and @0 @1))
467 /* (x | y) & ~(x & y) -> x ^ y */
469 (bit_and:c (bit_ior @0 @1) (bit_not (bit_and @0 @1)))
472 /* (x | y) & (~x ^ y) -> x & y */
474 (bit_and:c (bit_ior:c @0 @1) (bit_xor:c @1 (bit_not @0)))
477 /* ~x & ~y -> ~(x | y)
478 ~x | ~y -> ~(x & y) */
479 (for op (bit_and bit_ior)
480 rop (bit_ior bit_and)
482 (op (convert1? (bit_not @0)) (convert2? (bit_not @1)))
483 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
484 && tree_nop_conversion_p (type, TREE_TYPE (@1)))
485 (bit_not (rop (convert @0) (convert @1))))))
487 /* If we are XORing or adding two BIT_AND_EXPR's, both of which are and'ing
488 with a constant, and the two constants have no bits in common,
489 we should treat this as a BIT_IOR_EXPR since this may produce more
491 (for op (bit_xor plus)
493 (op (convert1? (bit_and@4 @0 INTEGER_CST@1))
494 (convert2? (bit_and@5 @2 INTEGER_CST@3)))
495 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
496 && tree_nop_conversion_p (type, TREE_TYPE (@2))
497 && wi::bit_and (@1, @3) == 0)
498 (bit_ior (convert @4) (convert @5)))))
500 /* (X | Y) ^ X -> Y & ~ X*/
502 (bit_xor:c (convert? (bit_ior:c @0 @1)) (convert? @0))
503 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
504 (convert (bit_and @1 (bit_not @0)))))
506 /* Convert ~X ^ ~Y to X ^ Y. */
508 (bit_xor (convert1? (bit_not @0)) (convert2? (bit_not @1)))
509 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
510 && tree_nop_conversion_p (type, TREE_TYPE (@1)))
511 (bit_xor (convert @0) (convert @1))))
513 /* Convert ~X ^ C to X ^ ~C. */
515 (bit_xor (convert? (bit_not @0)) INTEGER_CST@1)
516 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
517 (bit_xor (convert @0) (bit_not @1))))
519 /* Fold (X & Y) ^ Y as ~X & Y. */
521 (bit_xor:c (bit_and:c @0 @1) @1)
522 (bit_and (bit_not @0) @1))
524 /* Given a bit-wise operation CODE applied to ARG0 and ARG1, see if both
525 operands are another bit-wise operation with a common input. If so,
526 distribute the bit operations to save an operation and possibly two if
527 constants are involved. For example, convert
528 (A | B) & (A | C) into A | (B & C)
529 Further simplification will occur if B and C are constants. */
530 (for op (bit_and bit_ior)
531 rop (bit_ior bit_and)
533 (op (convert? (rop:c @0 @1)) (convert? (rop @0 @2)))
534 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
535 (rop (convert @0) (op (convert @1) (convert @2))))))
545 (abs tree_expr_nonnegative_p@0)
548 /* A few cases of fold-const.c negate_expr_p predicate. */
551 (if ((INTEGRAL_TYPE_P (type)
552 && TYPE_OVERFLOW_WRAPS (type))
553 || (!TYPE_OVERFLOW_SANITIZED (type)
554 && may_negate_without_overflow_p (t)))))
559 (if (!TYPE_OVERFLOW_SANITIZED (type))))
562 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (t)))))
563 /* VECTOR_CST handling of non-wrapping types would recurse in unsupported
567 (if (FLOAT_TYPE_P (TREE_TYPE (type)) || TYPE_OVERFLOW_WRAPS (type))))
569 /* (-A) * (-B) -> A * B */
571 (mult:c (convert1? (negate @0)) (convert2? negate_expr_p@1))
572 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
573 && tree_nop_conversion_p (type, TREE_TYPE (@1)))
574 (mult (convert @0) (convert (negate @1)))))
576 /* -(A + B) -> (-B) - A. */
578 (negate (plus:c @0 negate_expr_p@1))
579 (if (!HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type))
580 && !HONOR_SIGNED_ZEROS (element_mode (type)))
581 (minus (negate @1) @0)))
583 /* A - B -> A + (-B) if B is easily negatable. */
585 (minus @0 negate_expr_p@1)
586 (if (!FIXED_POINT_TYPE_P (type))
587 (plus @0 (negate @1))))
589 /* Try to fold (type) X op CST -> (type) (X op ((type-x) CST))
591 For bitwise binary operations apply operand conversions to the
592 binary operation result instead of to the operands. This allows
593 to combine successive conversions and bitwise binary operations.
594 We combine the above two cases by using a conditional convert. */
595 (for bitop (bit_and bit_ior bit_xor)
597 (bitop (convert @0) (convert? @1))
598 (if (((TREE_CODE (@1) == INTEGER_CST
599 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
600 && int_fits_type_p (@1, TREE_TYPE (@0)))
601 || types_match (@0, @1))
602 /* ??? This transform conflicts with fold-const.c doing
603 Convert (T)(x & c) into (T)x & (T)c, if c is an integer
604 constants (if x has signed type, the sign bit cannot be set
605 in c). This folds extension into the BIT_AND_EXPR.
606 Restrict it to GIMPLE to avoid endless recursions. */
607 && (bitop != BIT_AND_EXPR || GIMPLE)
608 && (/* That's a good idea if the conversion widens the operand, thus
609 after hoisting the conversion the operation will be narrower. */
610 TYPE_PRECISION (TREE_TYPE (@0)) < TYPE_PRECISION (type)
611 /* It's also a good idea if the conversion is to a non-integer
613 || GET_MODE_CLASS (TYPE_MODE (type)) != MODE_INT
614 /* Or if the precision of TO is not the same as the precision
616 || TYPE_PRECISION (type) != GET_MODE_PRECISION (TYPE_MODE (type))))
617 (convert (bitop @0 (convert @1))))))
619 (for bitop (bit_and bit_ior)
620 rbitop (bit_ior bit_and)
621 /* (x | y) & x -> x */
622 /* (x & y) | x -> x */
624 (bitop:c (rbitop:c @0 @1) @0)
626 /* (~x | y) & x -> x & y */
627 /* (~x & y) | x -> x | y */
629 (bitop:c (rbitop:c (bit_not @0) @1) @0)
632 /* Simplify (A & B) OP0 (C & B) to (A OP0 C) & B. */
633 (for bitop (bit_and bit_ior bit_xor)
635 (bitop (bit_and:c @0 @1) (bit_and @2 @1))
636 (bit_and (bitop @0 @2) @1)))
638 /* (x | CST1) & CST2 -> (x & CST2) | (CST1 & CST2) */
640 (bit_and (bit_ior @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
641 (bit_ior (bit_and @0 @2) (bit_and @1 @2)))
643 /* Combine successive equal operations with constants. */
644 (for bitop (bit_and bit_ior bit_xor)
646 (bitop (bitop @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
647 (bitop @0 (bitop @1 @2))))
649 /* Try simple folding for X op !X, and X op X with the help
650 of the truth_valued_p and logical_inverted_value predicates. */
651 (match truth_valued_p
653 (if (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1)))
654 (for op (tcc_comparison truth_and truth_andif truth_or truth_orif truth_xor)
655 (match truth_valued_p
657 (match truth_valued_p
660 (match (logical_inverted_value @0)
662 (match (logical_inverted_value @0)
663 (bit_not truth_valued_p@0))
664 (match (logical_inverted_value @0)
665 (eq @0 integer_zerop))
666 (match (logical_inverted_value @0)
667 (ne truth_valued_p@0 integer_truep))
668 (match (logical_inverted_value @0)
669 (bit_xor truth_valued_p@0 integer_truep))
673 (bit_and:c @0 (logical_inverted_value @0))
674 { build_zero_cst (type); })
675 /* X | !X and X ^ !X -> 1, , if X is truth-valued. */
676 (for op (bit_ior bit_xor)
678 (op:c truth_valued_p@0 (logical_inverted_value @0))
679 { constant_boolean_node (true, type); }))
680 /* X ==/!= !X is false/true. */
683 (op:c truth_valued_p@0 (logical_inverted_value @0))
684 { constant_boolean_node (op == NE_EXPR ? true : false, type); }))
686 /* If arg1 and arg2 are booleans (or any single bit type)
687 then try to simplify:
694 But only do this if our result feeds into a comparison as
695 this transformation is not always a win, particularly on
696 targets with and-not instructions.
697 -> simplify_bitwise_binary_boolean */
699 (ne (bit_and:c (bit_not @0) @1) integer_zerop)
700 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
701 && TYPE_PRECISION (TREE_TYPE (@1)) == 1)
704 (ne (bit_ior:c (bit_not @0) @1) integer_zerop)
705 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
706 && TYPE_PRECISION (TREE_TYPE (@1)) == 1)
711 (bit_not (bit_not @0))
714 /* Convert ~ (-A) to A - 1. */
716 (bit_not (convert? (negate @0)))
717 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
718 (convert (minus @0 { build_each_one_cst (TREE_TYPE (@0)); }))))
720 /* Convert ~ (A - 1) or ~ (A + -1) to -A. */
722 (bit_not (convert? (minus @0 integer_each_onep)))
723 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
724 (convert (negate @0))))
726 (bit_not (convert? (plus @0 integer_all_onesp)))
727 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
728 (convert (negate @0))))
730 /* Part of convert ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify. */
732 (bit_not (convert? (bit_xor @0 INTEGER_CST@1)))
733 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
734 (convert (bit_xor @0 (bit_not @1)))))
736 (bit_not (convert? (bit_xor:c (bit_not @0) @1)))
737 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
738 (convert (bit_xor @0 @1))))
740 /* (x & ~m) | (y & m) -> ((x ^ y) & m) ^ x */
742 (bit_ior:c (bit_and:cs @0 (bit_not @2)) (bit_and:cs @1 @2))
743 (bit_xor (bit_and (bit_xor @0 @1) @2) @0))
745 /* Fold A - (A & B) into ~B & A. */
747 (minus (convert? @0) (convert?:s (bit_and:cs @0 @1)))
748 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
749 && tree_nop_conversion_p (type, TREE_TYPE (@1)))
750 (convert (bit_and (bit_not @1) @0))))
754 /* ((X inner_op C0) outer_op C1)
755 With X being a tree where value_range has reasoned certain bits to always be
756 zero throughout its computed value range,
757 inner_op = {|,^}, outer_op = {|,^} and inner_op != outer_op
758 where zero_mask has 1's for all bits that are sure to be 0 in
760 if (inner_op == '^') C0 &= ~C1;
761 if ((C0 & ~zero_mask) == 0) then emit (X outer_op (C0 outer_op C1)
762 if ((C1 & ~zero_mask) == 0) then emit (X inner_op (C0 outer_op C1)
764 (for inner_op (bit_ior bit_xor)
765 outer_op (bit_xor bit_ior)
768 (inner_op:s @2 INTEGER_CST@0) INTEGER_CST@1)
772 wide_int zero_mask_not;
776 if (TREE_CODE (@2) == SSA_NAME)
777 zero_mask_not = get_nonzero_bits (@2);
781 if (inner_op == BIT_XOR_EXPR)
783 C0 = wi::bit_and_not (@0, @1);
784 cst_emit = wi::bit_or (C0, @1);
789 cst_emit = wi::bit_xor (@0, @1);
792 (if (!fail && wi::bit_and (C0, zero_mask_not) == 0)
793 (outer_op @2 { wide_int_to_tree (type, cst_emit); })
794 (if (!fail && wi::bit_and (@1, zero_mask_not) == 0)
795 (inner_op @2 { wide_int_to_tree (type, cst_emit); }))))))
797 /* Associate (p +p off1) +p off2 as (p +p (off1 + off2)). */
799 (pointer_plus (pointer_plus:s @0 @1) @3)
800 (pointer_plus @0 (plus @1 @3)))
806 tem4 = (unsigned long) tem3;
811 (pointer_plus @0 (convert?@2 (minus@3 (convert @1) (convert @0))))
812 /* Conditionally look through a sign-changing conversion. */
813 (if (TYPE_PRECISION (TREE_TYPE (@2)) == TYPE_PRECISION (TREE_TYPE (@3))
814 && ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@1)))
815 || (GENERIC && type == TREE_TYPE (@1))))
819 tem = (sizetype) ptr;
823 and produce the simpler and easier to analyze with respect to alignment
824 ... = ptr & ~algn; */
826 (pointer_plus @0 (negate (bit_and (convert @0) INTEGER_CST@1)))
827 (with { tree algn = wide_int_to_tree (TREE_TYPE (@0), wi::bit_not (@1)); }
828 (bit_and @0 { algn; })))
830 /* Try folding difference of addresses. */
832 (minus (convert ADDR_EXPR@0) (convert @1))
833 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
834 (with { HOST_WIDE_INT diff; }
835 (if (ptr_difference_const (@0, @1, &diff))
836 { build_int_cst_type (type, diff); }))))
838 (minus (convert @0) (convert ADDR_EXPR@1))
839 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
840 (with { HOST_WIDE_INT diff; }
841 (if (ptr_difference_const (@0, @1, &diff))
842 { build_int_cst_type (type, diff); }))))
844 /* If arg0 is derived from the address of an object or function, we may
845 be able to fold this expression using the object or function's
848 (bit_and (convert? @0) INTEGER_CST@1)
849 (if (POINTER_TYPE_P (TREE_TYPE (@0))
850 && tree_nop_conversion_p (type, TREE_TYPE (@0)))
854 unsigned HOST_WIDE_INT bitpos;
855 get_pointer_alignment_1 (@0, &align, &bitpos);
857 (if (wi::ltu_p (@1, align / BITS_PER_UNIT))
858 { wide_int_to_tree (type, wi::bit_and (@1, bitpos / BITS_PER_UNIT)); }))))
861 /* We can't reassociate at all for saturating types. */
862 (if (!TYPE_SATURATING (type))
864 /* Contract negates. */
865 /* A + (-B) -> A - B */
867 (plus:c (convert1? @0) (convert2? (negate @1)))
868 /* Apply STRIP_NOPS on @0 and the negate. */
869 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
870 && tree_nop_conversion_p (type, TREE_TYPE (@1))
871 && !TYPE_OVERFLOW_SANITIZED (type))
872 (minus (convert @0) (convert @1))))
873 /* A - (-B) -> A + B */
875 (minus (convert1? @0) (convert2? (negate @1)))
876 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
877 && tree_nop_conversion_p (type, TREE_TYPE (@1))
878 && !TYPE_OVERFLOW_SANITIZED (type))
879 (plus (convert @0) (convert @1))))
882 (negate (convert? (negate @1)))
883 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
884 && !TYPE_OVERFLOW_SANITIZED (type))
887 /* We can't reassociate floating-point unless -fassociative-math
888 or fixed-point plus or minus because of saturation to +-Inf. */
889 (if ((!FLOAT_TYPE_P (type) || flag_associative_math)
890 && !FIXED_POINT_TYPE_P (type))
892 /* Match patterns that allow contracting a plus-minus pair
893 irrespective of overflow issues. */
894 /* (A +- B) - A -> +- B */
895 /* (A +- B) -+ B -> A */
896 /* A - (A +- B) -> -+ B */
897 /* A +- (B -+ A) -> +- B */
899 (minus (plus:c @0 @1) @0)
902 (minus (minus @0 @1) @0)
905 (plus:c (minus @0 @1) @1)
908 (minus @0 (plus:c @0 @1))
911 (minus @0 (minus @0 @1))
914 /* (A +- CST) +- CST -> A + CST */
915 (for outer_op (plus minus)
916 (for inner_op (plus minus)
918 (outer_op (inner_op @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
919 /* If the constant operation overflows we cannot do the transform
920 as we would introduce undefined overflow, for example
921 with (a - 1) + INT_MIN. */
922 (with { tree cst = fold_binary (outer_op == inner_op
923 ? PLUS_EXPR : MINUS_EXPR, type, @1, @2); }
924 (if (cst && !TREE_OVERFLOW (cst))
925 (inner_op @0 { cst; } ))))))
927 /* (CST - A) +- CST -> CST - A */
928 (for outer_op (plus minus)
930 (outer_op (minus CONSTANT_CLASS_P@1 @0) CONSTANT_CLASS_P@2)
931 (with { tree cst = fold_binary (outer_op, type, @1, @2); }
932 (if (cst && !TREE_OVERFLOW (cst))
933 (minus { cst; } @0)))))
937 (plus:c (bit_not @0) @0)
938 (if (!TYPE_OVERFLOW_TRAPS (type))
939 { build_all_ones_cst (type); }))
943 (plus (convert? (bit_not @0)) integer_each_onep)
944 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
945 (negate (convert @0))))
949 (minus (convert? (negate @0)) integer_each_onep)
950 (if (!TYPE_OVERFLOW_TRAPS (type)
951 && tree_nop_conversion_p (type, TREE_TYPE (@0)))
952 (bit_not (convert @0))))
956 (minus integer_all_onesp @0)
959 /* (T)(P + A) - (T)P -> (T) A */
960 (for add (plus pointer_plus)
962 (minus (convert (add @0 @1))
964 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
965 /* For integer types, if A has a smaller type
966 than T the result depends on the possible
968 E.g. T=size_t, A=(unsigned)429497295, P>0.
969 However, if an overflow in P + A would cause
970 undefined behavior, we can assume that there
972 || (INTEGRAL_TYPE_P (TREE_TYPE (@0))
973 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
974 /* For pointer types, if the conversion of A to the
975 final type requires a sign- or zero-extension,
976 then we have to punt - it is not defined which
978 || (POINTER_TYPE_P (TREE_TYPE (@0))
979 && TREE_CODE (@1) == INTEGER_CST
980 && tree_int_cst_sign_bit (@1) == 0))
984 /* Simplifications of MIN_EXPR and MAX_EXPR. */
986 (for minmax (min max)
992 (if (INTEGRAL_TYPE_P (type)
993 && TYPE_MIN_VALUE (type)
994 && operand_equal_p (@1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
998 (if (INTEGRAL_TYPE_P (type)
999 && TYPE_MAX_VALUE (type)
1000 && operand_equal_p (@1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
1004 /* Simplifications of shift and rotates. */
1006 (for rotate (lrotate rrotate)
1008 (rotate integer_all_onesp@0 @1)
1011 /* Optimize -1 >> x for arithmetic right shifts. */
1013 (rshift integer_all_onesp@0 @1)
1014 (if (!TYPE_UNSIGNED (type)
1015 && tree_expr_nonnegative_p (@1))
1018 /* Optimize (x >> c) << c into x & (-1<<c). */
1020 (lshift (rshift @0 INTEGER_CST@1) @1)
1021 (if (wi::ltu_p (@1, element_precision (type)))
1022 (bit_and @0 (lshift { build_minus_one_cst (type); } @1))))
1024 /* Optimize (x << c) >> c into x & ((unsigned)-1 >> c) for unsigned
1027 (rshift (lshift @0 INTEGER_CST@1) @1)
1028 (if (TYPE_UNSIGNED (type)
1029 && (wi::ltu_p (@1, element_precision (type))))
1030 (bit_and @0 (rshift { build_minus_one_cst (type); } @1))))
1032 (for shiftrotate (lrotate rrotate lshift rshift)
1034 (shiftrotate @0 integer_zerop)
1037 (shiftrotate integer_zerop@0 @1)
1039 /* Prefer vector1 << scalar to vector1 << vector2
1040 if vector2 is uniform. */
1041 (for vec (VECTOR_CST CONSTRUCTOR)
1043 (shiftrotate @0 vec@1)
1044 (with { tree tem = uniform_vector_p (@1); }
1046 (shiftrotate @0 { tem; }))))))
1048 /* Rewrite an LROTATE_EXPR by a constant into an
1049 RROTATE_EXPR by a new constant. */
1051 (lrotate @0 INTEGER_CST@1)
1052 (rrotate @0 { fold_binary (MINUS_EXPR, TREE_TYPE (@1),
1053 build_int_cst (TREE_TYPE (@1),
1054 element_precision (type)), @1); }))
1056 /* Turn (a OP c1) OP c2 into a OP (c1+c2). */
1057 (for op (lrotate rrotate rshift lshift)
1059 (op (op @0 INTEGER_CST@1) INTEGER_CST@2)
1060 (with { unsigned int prec = element_precision (type); }
1061 (if (wi::ge_p (@1, 0, TYPE_SIGN (TREE_TYPE (@1)))
1062 && wi::lt_p (@1, prec, TYPE_SIGN (TREE_TYPE (@1)))
1063 && wi::ge_p (@2, 0, TYPE_SIGN (TREE_TYPE (@2)))
1064 && wi::lt_p (@2, prec, TYPE_SIGN (TREE_TYPE (@2))))
1065 (with { unsigned int low = wi::add (@1, @2).to_uhwi (); }
1066 /* Deal with a OP (c1 + c2) being undefined but (a OP c1) OP c2
1067 being well defined. */
1069 (if (op == LROTATE_EXPR || op == RROTATE_EXPR)
1070 (op @0 { build_int_cst (TREE_TYPE (@1), low % prec); })
1071 (if (TYPE_UNSIGNED (type) || op == LSHIFT_EXPR)
1072 { build_zero_cst (type); }
1073 (op @0 { build_int_cst (TREE_TYPE (@1), prec - 1); })))
1074 (op @0 { build_int_cst (TREE_TYPE (@1), low); })))))))
1077 /* ((1 << A) & 1) != 0 -> A == 0
1078 ((1 << A) & 1) == 0 -> A != 0 */
1082 (cmp (bit_and (lshift integer_onep @0) integer_onep) integer_zerop)
1083 (icmp @0 { build_zero_cst (TREE_TYPE (@0)); })))
1085 /* (CST1 << A) == CST2 -> A == ctz (CST2) - ctz (CST1)
1086 (CST1 << A) != CST2 -> A != ctz (CST2) - ctz (CST1)
1090 (cmp (lshift INTEGER_CST@0 @1) INTEGER_CST@2)
1091 (with { int cand = wi::ctz (@2) - wi::ctz (@0); }
1093 || (!integer_zerop (@2)
1094 && wi::ne_p (wi::lshift (@0, cand), @2)))
1095 { constant_boolean_node (cmp == NE_EXPR, type); }
1096 (if (!integer_zerop (@2)
1097 && wi::eq_p (wi::lshift (@0, cand), @2))
1098 (cmp @1 { build_int_cst (TREE_TYPE (@1), cand); }))))))
1100 /* Fold (X << C1) & C2 into (X << C1) & (C2 | ((1 << C1) - 1))
1101 (X >> C1) & C2 into (X >> C1) & (C2 | ~((type) -1 >> C1))
1102 if the new mask might be further optimized. */
1103 (for shift (lshift rshift)
1105 (bit_and (convert?:s@4 (shift:s@5 (convert1?@3 @0) INTEGER_CST@1))
1107 (if (tree_nop_conversion_p (TREE_TYPE (@4), TREE_TYPE (@5))
1108 && TYPE_PRECISION (type) <= HOST_BITS_PER_WIDE_INT
1109 && tree_fits_uhwi_p (@1)
1110 && tree_to_uhwi (@1) > 0
1111 && tree_to_uhwi (@1) < TYPE_PRECISION (type))
1114 unsigned int shiftc = tree_to_uhwi (@1);
1115 unsigned HOST_WIDE_INT mask = TREE_INT_CST_LOW (@2);
1116 unsigned HOST_WIDE_INT newmask, zerobits = 0;
1117 tree shift_type = TREE_TYPE (@3);
1120 if (shift == LSHIFT_EXPR)
1121 zerobits = ((((unsigned HOST_WIDE_INT) 1) << shiftc) - 1);
1122 else if (shift == RSHIFT_EXPR
1123 && (TYPE_PRECISION (shift_type)
1124 == GET_MODE_PRECISION (TYPE_MODE (shift_type))))
1126 prec = TYPE_PRECISION (TREE_TYPE (@3));
1128 /* See if more bits can be proven as zero because of
1131 && TYPE_UNSIGNED (TREE_TYPE (@0)))
1133 tree inner_type = TREE_TYPE (@0);
1134 if ((TYPE_PRECISION (inner_type)
1135 == GET_MODE_PRECISION (TYPE_MODE (inner_type)))
1136 && TYPE_PRECISION (inner_type) < prec)
1138 prec = TYPE_PRECISION (inner_type);
1139 /* See if we can shorten the right shift. */
1141 shift_type = inner_type;
1142 /* Otherwise X >> C1 is all zeros, so we'll optimize
1143 it into (X, 0) later on by making sure zerobits
1147 zerobits = ~(unsigned HOST_WIDE_INT) 0;
1150 zerobits >>= HOST_BITS_PER_WIDE_INT - shiftc;
1151 zerobits <<= prec - shiftc;
1153 /* For arithmetic shift if sign bit could be set, zerobits
1154 can contain actually sign bits, so no transformation is
1155 possible, unless MASK masks them all away. In that
1156 case the shift needs to be converted into logical shift. */
1157 if (!TYPE_UNSIGNED (TREE_TYPE (@3))
1158 && prec == TYPE_PRECISION (TREE_TYPE (@3)))
1160 if ((mask & zerobits) == 0)
1161 shift_type = unsigned_type_for (TREE_TYPE (@3));
1167 /* ((X << 16) & 0xff00) is (X, 0). */
1168 (if ((mask & zerobits) == mask)
1169 { build_int_cst (type, 0); }
1170 (with { newmask = mask | zerobits; }
1171 (if (newmask != mask && (newmask & (newmask + 1)) == 0)
1174 /* Only do the transformation if NEWMASK is some integer
1176 for (prec = BITS_PER_UNIT;
1177 prec < HOST_BITS_PER_WIDE_INT; prec <<= 1)
1178 if (newmask == (((unsigned HOST_WIDE_INT) 1) << prec) - 1)
1181 (if (prec < HOST_BITS_PER_WIDE_INT
1182 || newmask == ~(unsigned HOST_WIDE_INT) 0)
1184 { tree newmaskt = build_int_cst_type (TREE_TYPE (@2), newmask); }
1185 (if (!tree_int_cst_equal (newmaskt, @2))
1186 (if (shift_type != TREE_TYPE (@3))
1187 (bit_and (convert (shift:shift_type (convert @3) @1)) { newmaskt; })
1188 (bit_and @4 { newmaskt; })))))))))))))
1190 /* Fold (X {&,^,|} C2) << C1 into (X << C1) {&,^,|} (C2 << C1)
1191 (X {&,^,|} C2) >> C1 into (X >> C1) & (C2 >> C1). */
1192 (for shift (lshift rshift)
1193 (for bit_op (bit_and bit_xor bit_ior)
1195 (shift (convert?:s (bit_op:s @0 INTEGER_CST@2)) INTEGER_CST@1)
1196 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1197 (with { tree mask = int_const_binop (shift, fold_convert (type, @2), @1); }
1198 (bit_op (shift (convert @0) @1) { mask; }))))))
1201 /* Simplifications of conversions. */
1203 /* Basic strip-useless-type-conversions / strip_nops. */
1204 (for cvt (convert view_convert float fix_trunc)
1207 (if ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@0)))
1208 || (GENERIC && type == TREE_TYPE (@0)))
1211 /* Contract view-conversions. */
1213 (view_convert (view_convert @0))
1216 /* For integral conversions with the same precision or pointer
1217 conversions use a NOP_EXPR instead. */
1220 (if ((INTEGRAL_TYPE_P (type) || POINTER_TYPE_P (type))
1221 && (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
1222 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (@0)))
1225 /* Strip inner integral conversions that do not change precision or size. */
1227 (view_convert (convert@0 @1))
1228 (if ((INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
1229 && (INTEGRAL_TYPE_P (TREE_TYPE (@1)) || POINTER_TYPE_P (TREE_TYPE (@1)))
1230 && (TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
1231 && (TYPE_SIZE (TREE_TYPE (@0)) == TYPE_SIZE (TREE_TYPE (@1))))
1234 /* Re-association barriers around constants and other re-association
1235 barriers can be removed. */
1237 (paren CONSTANT_CLASS_P@0)
1240 (paren (paren@1 @0))
1243 /* Handle cases of two conversions in a row. */
1244 (for ocvt (convert float fix_trunc)
1245 (for icvt (convert float)
1250 tree inside_type = TREE_TYPE (@0);
1251 tree inter_type = TREE_TYPE (@1);
1252 int inside_int = INTEGRAL_TYPE_P (inside_type);
1253 int inside_ptr = POINTER_TYPE_P (inside_type);
1254 int inside_float = FLOAT_TYPE_P (inside_type);
1255 int inside_vec = VECTOR_TYPE_P (inside_type);
1256 unsigned int inside_prec = TYPE_PRECISION (inside_type);
1257 int inside_unsignedp = TYPE_UNSIGNED (inside_type);
1258 int inter_int = INTEGRAL_TYPE_P (inter_type);
1259 int inter_ptr = POINTER_TYPE_P (inter_type);
1260 int inter_float = FLOAT_TYPE_P (inter_type);
1261 int inter_vec = VECTOR_TYPE_P (inter_type);
1262 unsigned int inter_prec = TYPE_PRECISION (inter_type);
1263 int inter_unsignedp = TYPE_UNSIGNED (inter_type);
1264 int final_int = INTEGRAL_TYPE_P (type);
1265 int final_ptr = POINTER_TYPE_P (type);
1266 int final_float = FLOAT_TYPE_P (type);
1267 int final_vec = VECTOR_TYPE_P (type);
1268 unsigned int final_prec = TYPE_PRECISION (type);
1269 int final_unsignedp = TYPE_UNSIGNED (type);
1272 /* In addition to the cases of two conversions in a row
1273 handled below, if we are converting something to its own
1274 type via an object of identical or wider precision, neither
1275 conversion is needed. */
1276 (if (((GIMPLE && useless_type_conversion_p (type, inside_type))
1278 && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (inside_type)))
1279 && (((inter_int || inter_ptr) && final_int)
1280 || (inter_float && final_float))
1281 && inter_prec >= final_prec)
1284 /* Likewise, if the intermediate and initial types are either both
1285 float or both integer, we don't need the middle conversion if the
1286 former is wider than the latter and doesn't change the signedness
1287 (for integers). Avoid this if the final type is a pointer since
1288 then we sometimes need the middle conversion. Likewise if the
1289 final type has a precision not equal to the size of its mode. */
1290 (if (((inter_int && inside_int) || (inter_float && inside_float))
1291 && (final_int || final_float)
1292 && inter_prec >= inside_prec
1293 && (inter_float || inter_unsignedp == inside_unsignedp)
1294 && ! (final_prec != GET_MODE_PRECISION (TYPE_MODE (type))
1295 && TYPE_MODE (type) == TYPE_MODE (inter_type)))
1298 /* If we have a sign-extension of a zero-extended value, we can
1299 replace that by a single zero-extension. Likewise if the
1300 final conversion does not change precision we can drop the
1301 intermediate conversion. */
1302 (if (inside_int && inter_int && final_int
1303 && ((inside_prec < inter_prec && inter_prec < final_prec
1304 && inside_unsignedp && !inter_unsignedp)
1305 || final_prec == inter_prec))
1308 /* Two conversions in a row are not needed unless:
1309 - some conversion is floating-point (overstrict for now), or
1310 - some conversion is a vector (overstrict for now), or
1311 - the intermediate type is narrower than both initial and
1313 - the intermediate type and innermost type differ in signedness,
1314 and the outermost type is wider than the intermediate, or
1315 - the initial type is a pointer type and the precisions of the
1316 intermediate and final types differ, or
1317 - the final type is a pointer type and the precisions of the
1318 initial and intermediate types differ. */
1319 (if (! inside_float && ! inter_float && ! final_float
1320 && ! inside_vec && ! inter_vec && ! final_vec
1321 && (inter_prec >= inside_prec || inter_prec >= final_prec)
1322 && ! (inside_int && inter_int
1323 && inter_unsignedp != inside_unsignedp
1324 && inter_prec < final_prec)
1325 && ((inter_unsignedp && inter_prec > inside_prec)
1326 == (final_unsignedp && final_prec > inter_prec))
1327 && ! (inside_ptr && inter_prec != final_prec)
1328 && ! (final_ptr && inside_prec != inter_prec)
1329 && ! (final_prec != GET_MODE_PRECISION (TYPE_MODE (type))
1330 && TYPE_MODE (type) == TYPE_MODE (inter_type)))
1333 /* A truncation to an unsigned type (a zero-extension) should be
1334 canonicalized as bitwise and of a mask. */
1335 (if (final_int && inter_int && inside_int
1336 && final_prec == inside_prec
1337 && final_prec > inter_prec
1339 (convert (bit_and @0 { wide_int_to_tree
1341 wi::mask (inter_prec, false,
1342 TYPE_PRECISION (inside_type))); })))
1344 /* If we are converting an integer to a floating-point that can
1345 represent it exactly and back to an integer, we can skip the
1346 floating-point conversion. */
1347 (if (GIMPLE /* PR66211 */
1348 && inside_int && inter_float && final_int &&
1349 (unsigned) significand_size (TYPE_MODE (inter_type))
1350 >= inside_prec - !inside_unsignedp)
1353 /* If we have a narrowing conversion to an integral type that is fed by a
1354 BIT_AND_EXPR, we might be able to remove the BIT_AND_EXPR if it merely
1355 masks off bits outside the final type (and nothing else). */
1357 (convert (bit_and @0 INTEGER_CST@1))
1358 (if (INTEGRAL_TYPE_P (type)
1359 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
1360 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))
1361 && operand_equal_p (@1, build_low_bits_mask (TREE_TYPE (@1),
1362 TYPE_PRECISION (type)), 0))
1366 /* (X /[ex] A) * A -> X. */
1368 (mult (convert? (exact_div @0 @1)) @1)
1369 /* Look through a sign-changing conversion. */
1372 /* Canonicalization of binary operations. */
1374 /* Convert X + -C into X - C. */
1376 (plus @0 REAL_CST@1)
1377 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
1378 (with { tree tem = fold_unary (NEGATE_EXPR, type, @1); }
1379 (if (!TREE_OVERFLOW (tem) || !flag_trapping_math)
1380 (minus @0 { tem; })))))
1382 /* Convert x+x into x*2.0. */
1385 (if (SCALAR_FLOAT_TYPE_P (type))
1386 (mult @0 { build_real (type, dconst2); })))
1389 (minus integer_zerop @1)
1392 /* (ARG0 - ARG1) is the same as (-ARG1 + ARG0). So check whether
1393 ARG0 is zero and X + ARG0 reduces to X, since that would mean
1394 (-ARG1 + ARG0) reduces to -ARG1. */
1396 (minus real_zerop@0 @1)
1397 (if (fold_real_zero_addition_p (type, @0, 0))
1400 /* Transform x * -1 into -x. */
1402 (mult @0 integer_minus_onep)
1405 /* COMPLEX_EXPR and REALPART/IMAGPART_EXPR cancellations. */
1407 (complex (realpart @0) (imagpart @0))
1410 (realpart (complex @0 @1))
1413 (imagpart (complex @0 @1))
1416 /* Sometimes we only care about half of a complex expression. */
1418 (realpart (convert?:s (conj:s @0)))
1419 (convert (realpart @0)))
1421 (imagpart (convert?:s (conj:s @0)))
1422 (convert (negate (imagpart @0))))
1423 (for part (realpart imagpart)
1424 (for op (plus minus)
1426 (part (convert?:s@2 (op:s @0 @1)))
1427 (convert (op (part @0) (part @1))))))
1429 (realpart (convert?:s (CEXPI:s @0)))
1432 (imagpart (convert?:s (CEXPI:s @0)))
1435 /* conj(conj(x)) -> x */
1437 (conj (convert? (conj @0)))
1438 (if (tree_nop_conversion_p (TREE_TYPE (@0), type))
1441 /* conj({x,y}) -> {x,-y} */
1443 (conj (convert?:s (complex:s @0 @1)))
1444 (with { tree itype = TREE_TYPE (type); }
1445 (complex (convert:itype @0) (negate (convert:itype @1)))))
1447 /* BSWAP simplifications, transforms checked by gcc.dg/builtin-bswap-8.c. */
1448 (for bswap (BUILT_IN_BSWAP16 BUILT_IN_BSWAP32 BUILT_IN_BSWAP64)
1453 (bswap (bit_not (bswap @0)))
1455 (for bitop (bit_xor bit_ior bit_and)
1457 (bswap (bitop:c (bswap @0) @1))
1458 (bitop @0 (bswap @1)))))
1461 /* Combine COND_EXPRs and VEC_COND_EXPRs. */
1463 /* Simplify constant conditions.
1464 Only optimize constant conditions when the selected branch
1465 has the same type as the COND_EXPR. This avoids optimizing
1466 away "c ? x : throw", where the throw has a void type.
1467 Note that we cannot throw away the fold-const.c variant nor
1468 this one as we depend on doing this transform before possibly
1469 A ? B : B -> B triggers and the fold-const.c one can optimize
1470 0 ? A : B to B even if A has side-effects. Something
1471 genmatch cannot handle. */
1473 (cond INTEGER_CST@0 @1 @2)
1474 (if (integer_zerop (@0))
1475 (if (!VOID_TYPE_P (TREE_TYPE (@2)) || VOID_TYPE_P (type))
1477 (if (!VOID_TYPE_P (TREE_TYPE (@1)) || VOID_TYPE_P (type))
1480 (vec_cond VECTOR_CST@0 @1 @2)
1481 (if (integer_all_onesp (@0))
1483 (if (integer_zerop (@0))
1486 (for cnd (cond vec_cond)
1487 /* A ? B : (A ? X : C) -> A ? B : C. */
1489 (cnd @0 (cnd @0 @1 @2) @3)
1492 (cnd @0 @1 (cnd @0 @2 @3))
1495 /* A ? B : B -> B. */
1500 /* !A ? B : C -> A ? C : B. */
1502 (cnd (logical_inverted_value truth_valued_p@0) @1 @2)
1505 /* A + (B vcmp C ? 1 : 0) -> A - (B vcmp C), since vector comparisons
1506 return all-1 or all-0 results. */
1507 /* ??? We could instead convert all instances of the vec_cond to negate,
1508 but that isn't necessarily a win on its own. */
1510 (plus:c @3 (view_convert? (vec_cond @0 integer_each_onep@1 integer_zerop@2)))
1511 (if (VECTOR_TYPE_P (type)
1512 && TYPE_VECTOR_SUBPARTS (type) == TYPE_VECTOR_SUBPARTS (TREE_TYPE (@0))
1513 && (TYPE_MODE (TREE_TYPE (type))
1514 == TYPE_MODE (TREE_TYPE (TREE_TYPE (@0)))))
1515 (minus @3 (view_convert @0))))
1517 /* ... likewise A - (B vcmp C ? 1 : 0) -> A + (B vcmp C). */
1519 (minus @3 (view_convert? (vec_cond @0 integer_each_onep@1 integer_zerop@2)))
1520 (if (VECTOR_TYPE_P (type)
1521 && TYPE_VECTOR_SUBPARTS (type) == TYPE_VECTOR_SUBPARTS (TREE_TYPE (@0))
1522 && (TYPE_MODE (TREE_TYPE (type))
1523 == TYPE_MODE (TREE_TYPE (TREE_TYPE (@0)))))
1524 (plus @3 (view_convert @0))))
1527 /* Simplifications of comparisons. */
1529 /* See if we can reduce the magnitude of a constant involved in a
1530 comparison by changing the comparison code. This is a canonicalization
1531 formerly done by maybe_canonicalize_comparison_1. */
1535 (cmp @0 INTEGER_CST@1)
1536 (if (tree_int_cst_sgn (@1) == -1)
1537 (acmp @0 { wide_int_to_tree (TREE_TYPE (@1), wi::add (@1, 1)); }))))
1541 (cmp @0 INTEGER_CST@1)
1542 (if (tree_int_cst_sgn (@1) == 1)
1543 (acmp @0 { wide_int_to_tree (TREE_TYPE (@1), wi::sub (@1, 1)); }))))
1546 /* We can simplify a logical negation of a comparison to the
1547 inverted comparison. As we cannot compute an expression
1548 operator using invert_tree_comparison we have to simulate
1549 that with expression code iteration. */
1550 (for cmp (tcc_comparison)
1551 icmp (inverted_tcc_comparison)
1552 ncmp (inverted_tcc_comparison_with_nans)
1553 /* Ideally we'd like to combine the following two patterns
1554 and handle some more cases by using
1555 (logical_inverted_value (cmp @0 @1))
1556 here but for that genmatch would need to "inline" that.
1557 For now implement what forward_propagate_comparison did. */
1559 (bit_not (cmp @0 @1))
1560 (if (VECTOR_TYPE_P (type)
1561 || (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1))
1562 /* Comparison inversion may be impossible for trapping math,
1563 invert_tree_comparison will tell us. But we can't use
1564 a computed operator in the replacement tree thus we have
1565 to play the trick below. */
1566 (with { enum tree_code ic = invert_tree_comparison
1567 (cmp, HONOR_NANS (@0)); }
1573 (bit_xor (cmp @0 @1) integer_truep)
1574 (with { enum tree_code ic = invert_tree_comparison
1575 (cmp, HONOR_NANS (@0)); }
1581 /* Transform comparisons of the form X - Y CMP 0 to X CMP Y.
1582 ??? The transformation is valid for the other operators if overflow
1583 is undefined for the type, but performing it here badly interacts
1584 with the transformation in fold_cond_expr_with_comparison which
1585 attempts to synthetize ABS_EXPR. */
1588 (cmp (minus@2 @0 @1) integer_zerop)
1589 (if (single_use (@2))
1592 /* Transform comparisons of the form X * C1 CMP 0 to X CMP 0 in the
1593 signed arithmetic case. That form is created by the compiler
1594 often enough for folding it to be of value. One example is in
1595 computing loop trip counts after Operator Strength Reduction. */
1596 (for cmp (simple_comparison)
1597 scmp (swapped_simple_comparison)
1599 (cmp (mult @0 INTEGER_CST@1) integer_zerop@2)
1600 /* Handle unfolded multiplication by zero. */
1601 (if (integer_zerop (@1))
1603 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1604 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1605 /* If @1 is negative we swap the sense of the comparison. */
1606 (if (tree_int_cst_sgn (@1) < 0)
1610 /* Simplify comparison of something with itself. For IEEE
1611 floating-point, we can only do some of these simplifications. */
1614 (if (! FLOAT_TYPE_P (TREE_TYPE (@0))
1615 || ! HONOR_NANS (TYPE_MODE (TREE_TYPE (@0))))
1616 { constant_boolean_node (true, type); }))
1625 || ! FLOAT_TYPE_P (TREE_TYPE (@0))
1626 || ! HONOR_NANS (TYPE_MODE (TREE_TYPE (@0))))
1627 { constant_boolean_node (false, type); })))
1628 (for cmp (unle unge uneq)
1631 { constant_boolean_node (true, type); }))
1634 (if (!flag_trapping_math)
1635 { constant_boolean_node (false, type); }))
1637 /* Fold ~X op ~Y as Y op X. */
1638 (for cmp (simple_comparison)
1640 (cmp (bit_not @0) (bit_not @1))
1643 /* Fold ~X op C as X op' ~C, where op' is the swapped comparison. */
1644 (for cmp (simple_comparison)
1645 scmp (swapped_simple_comparison)
1647 (cmp (bit_not @0) CONSTANT_CLASS_P@1)
1648 (if (TREE_CODE (@1) == INTEGER_CST || TREE_CODE (@1) == VECTOR_CST)
1649 (scmp @0 (bit_not @1)))))
1651 (for cmp (simple_comparison)
1652 /* Fold (double)float1 CMP (double)float2 into float1 CMP float2. */
1654 (cmp (convert@2 @0) (convert? @1))
1655 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
1656 && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@2))
1657 == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0)))
1658 && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@2))
1659 == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1))))
1662 tree type1 = TREE_TYPE (@1);
1663 if (TREE_CODE (@1) == REAL_CST && !DECIMAL_FLOAT_TYPE_P (type1))
1665 REAL_VALUE_TYPE orig = TREE_REAL_CST (@1);
1666 if (TYPE_PRECISION (type1) > TYPE_PRECISION (float_type_node)
1667 && exact_real_truncate (TYPE_MODE (float_type_node), &orig))
1668 type1 = float_type_node;
1669 if (TYPE_PRECISION (type1) > TYPE_PRECISION (double_type_node)
1670 && exact_real_truncate (TYPE_MODE (double_type_node), &orig))
1671 type1 = double_type_node;
1674 = (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type1)
1675 ? TREE_TYPE (@0) : type1);
1677 (if (TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (newtype))
1678 (cmp (convert:newtype @0) (convert:newtype @1))))))
1682 /* IEEE doesn't distinguish +0 and -0 in comparisons. */
1684 /* a CMP (-0) -> a CMP 0 */
1685 (if (REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@1)))
1686 (cmp @0 { build_real (TREE_TYPE (@1), dconst0); }))
1687 /* x != NaN is always true, other ops are always false. */
1688 (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
1689 && ! HONOR_SNANS (@1))
1690 { constant_boolean_node (cmp == NE_EXPR, type); })
1691 /* Fold comparisons against infinity. */
1692 (if (REAL_VALUE_ISINF (TREE_REAL_CST (@1))
1693 && MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (@1))))
1696 REAL_VALUE_TYPE max;
1697 enum tree_code code = cmp;
1698 bool neg = REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1));
1700 code = swap_tree_comparison (code);
1703 /* x > +Inf is always false, if with ignore sNANs. */
1704 (if (code == GT_EXPR
1705 && ! HONOR_SNANS (@0))
1706 { constant_boolean_node (false, type); })
1707 (if (code == LE_EXPR)
1708 /* x <= +Inf is always true, if we don't case about NaNs. */
1709 (if (! HONOR_NANS (@0))
1710 { constant_boolean_node (true, type); }
1711 /* x <= +Inf is the same as x == x, i.e. !isnan(x). */
1713 /* x == +Inf and x >= +Inf are always equal to x > DBL_MAX. */
1714 (if (code == EQ_EXPR || code == GE_EXPR)
1715 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
1717 (lt @0 { build_real (TREE_TYPE (@0), max); })
1718 (gt @0 { build_real (TREE_TYPE (@0), max); }))))
1719 /* x < +Inf is always equal to x <= DBL_MAX. */
1720 (if (code == LT_EXPR)
1721 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
1723 (ge @0 { build_real (TREE_TYPE (@0), max); })
1724 (le @0 { build_real (TREE_TYPE (@0), max); }))))
1725 /* x != +Inf is always equal to !(x > DBL_MAX). */
1726 (if (code == NE_EXPR)
1727 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
1728 (if (! HONOR_NANS (@0))
1730 (ge @0 { build_real (TREE_TYPE (@0), max); })
1731 (le @0 { build_real (TREE_TYPE (@0), max); }))
1733 (bit_xor (lt @0 { build_real (TREE_TYPE (@0), max); })
1734 { build_one_cst (type); })
1735 (bit_xor (gt @0 { build_real (TREE_TYPE (@0), max); })
1736 { build_one_cst (type); }))))))))))
1738 /* If this is a comparison of a real constant with a PLUS_EXPR
1739 or a MINUS_EXPR of a real constant, we can convert it into a
1740 comparison with a revised real constant as long as no overflow
1741 occurs when unsafe_math_optimizations are enabled. */
1742 (if (flag_unsafe_math_optimizations)
1743 (for op (plus minus)
1745 (cmp (op @0 REAL_CST@1) REAL_CST@2)
1748 tree tem = const_binop (op == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR,
1749 TREE_TYPE (@1), @2, @1);
1751 (if (tem && !TREE_OVERFLOW (tem))
1752 (cmp @0 { tem; }))))))
1754 /* Likewise, we can simplify a comparison of a real constant with
1755 a MINUS_EXPR whose first operand is also a real constant, i.e.
1756 (c1 - x) < c2 becomes x > c1-c2. Reordering is allowed on
1757 floating-point types only if -fassociative-math is set. */
1758 (if (flag_associative_math)
1760 (cmp (minus REAL_CST@0 @1) REAL_CST@2)
1761 (with { tree tem = const_binop (MINUS_EXPR, TREE_TYPE (@1), @0, @2); }
1762 (if (tem && !TREE_OVERFLOW (tem))
1763 (cmp { tem; } @1)))))
1765 /* Fold comparisons against built-in math functions. */
1766 (if (flag_unsafe_math_optimizations
1767 && ! flag_errno_math)
1770 (cmp (sq @0) REAL_CST@1)
1772 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
1774 /* sqrt(x) < y is always false, if y is negative. */
1775 (if (cmp == EQ_EXPR || cmp == LT_EXPR || cmp == LE_EXPR)
1776 { constant_boolean_node (false, type); })
1777 /* sqrt(x) > y is always true, if y is negative and we
1778 don't care about NaNs, i.e. negative values of x. */
1779 (if (cmp == NE_EXPR || !HONOR_NANS (@0))
1780 { constant_boolean_node (true, type); })
1781 /* sqrt(x) > y is the same as x >= 0, if y is negative. */
1782 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })))
1783 (if (cmp == GT_EXPR || cmp == GE_EXPR)
1787 real_arithmetic (&c2, MULT_EXPR,
1788 &TREE_REAL_CST (@1), &TREE_REAL_CST (@1));
1789 real_convert (&c2, TYPE_MODE (TREE_TYPE (@0)), &c2);
1791 (if (REAL_VALUE_ISINF (c2))
1792 /* sqrt(x) > y is x == +Inf, when y is very large. */
1793 (if (HONOR_INFINITIES (@0))
1794 (eq @0 { build_real (TREE_TYPE (@0), c2); })
1795 { constant_boolean_node (false, type); })
1796 /* sqrt(x) > c is the same as x > c*c. */
1797 (cmp @0 { build_real (TREE_TYPE (@0), c2); }))))
1798 (if (cmp == LT_EXPR || cmp == LE_EXPR)
1802 real_arithmetic (&c2, MULT_EXPR,
1803 &TREE_REAL_CST (@1), &TREE_REAL_CST (@1));
1804 real_convert (&c2, TYPE_MODE (TREE_TYPE (@0)), &c2);
1806 (if (REAL_VALUE_ISINF (c2))
1808 /* sqrt(x) < y is always true, when y is a very large
1809 value and we don't care about NaNs or Infinities. */
1810 (if (! HONOR_NANS (@0) && ! HONOR_INFINITIES (@0))
1811 { constant_boolean_node (true, type); })
1812 /* sqrt(x) < y is x != +Inf when y is very large and we
1813 don't care about NaNs. */
1814 (if (! HONOR_NANS (@0))
1815 (ne @0 { build_real (TREE_TYPE (@0), c2); }))
1816 /* sqrt(x) < y is x >= 0 when y is very large and we
1817 don't care about Infinities. */
1818 (if (! HONOR_INFINITIES (@0))
1819 (ge @0 { build_real (TREE_TYPE (@0), dconst0); }))
1820 /* sqrt(x) < y is x >= 0 && x != +Inf, when y is large. */
1823 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
1824 (ne @0 { build_real (TREE_TYPE (@0), c2); }))))
1825 /* sqrt(x) < c is the same as x < c*c, if we ignore NaNs. */
1826 (if (! HONOR_NANS (@0))
1827 (cmp @0 { build_real (TREE_TYPE (@0), c2); })
1828 /* sqrt(x) < c is the same as x >= 0 && x < c*c. */
1831 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
1832 (cmp @0 { build_real (TREE_TYPE (@0), c2); }))))))))))))
1834 /* Unordered tests if either argument is a NaN. */
1836 (bit_ior (unordered @0 @0) (unordered @1 @1))
1837 (if (types_match (@0, @1))
1840 (bit_and (ordered @0 @0) (ordered @1 @1))
1841 (if (types_match (@0, @1))
1844 (bit_ior:c (unordered @0 @0) (unordered:c@2 @0 @1))
1847 (bit_and:c (ordered @0 @0) (ordered:c@2 @0 @1))
1850 /* -A CMP -B -> B CMP A. */
1851 (for cmp (tcc_comparison)
1852 scmp (swapped_tcc_comparison)
1854 (cmp (negate @0) (negate @1))
1855 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
1856 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1857 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
1860 (cmp (negate @0) CONSTANT_CLASS_P@1)
1861 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
1862 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1863 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
1864 (with { tree tem = fold_unary (NEGATE_EXPR, TREE_TYPE (@0), @1); }
1865 (if (tem && !TREE_OVERFLOW (tem))
1866 (scmp @0 { tem; }))))))
1868 /* Convert ABS_EXPR<x> == 0 or ABS_EXPR<x> != 0 to x == 0 or x != 0. */
1871 (op (abs @0) zerop@1)
1874 /* From fold_sign_changed_comparison and fold_widened_comparison. */
1875 (for cmp (simple_comparison)
1877 (cmp (convert@0 @00) (convert?@1 @10))
1878 (if (TREE_CODE (TREE_TYPE (@0)) == INTEGER_TYPE
1879 /* Disable this optimization if we're casting a function pointer
1880 type on targets that require function pointer canonicalization. */
1881 && !(targetm.have_canonicalize_funcptr_for_compare ()
1882 && TREE_CODE (TREE_TYPE (@00)) == POINTER_TYPE
1883 && TREE_CODE (TREE_TYPE (TREE_TYPE (@00))) == FUNCTION_TYPE)
1885 (if (TYPE_PRECISION (TREE_TYPE (@00)) == TYPE_PRECISION (TREE_TYPE (@0))
1886 && (TREE_CODE (@10) == INTEGER_CST
1887 || (@1 != @10 && types_match (TREE_TYPE (@10), TREE_TYPE (@00))))
1888 && (TYPE_UNSIGNED (TREE_TYPE (@00)) == TYPE_UNSIGNED (TREE_TYPE (@0))
1891 && (POINTER_TYPE_P (TREE_TYPE (@00)) == POINTER_TYPE_P (TREE_TYPE (@0))))
1892 /* ??? The special-casing of INTEGER_CST conversion was in the original
1893 code and here to avoid a spurious overflow flag on the resulting
1894 constant which fold_convert produces. */
1895 (if (TREE_CODE (@1) == INTEGER_CST)
1896 (cmp @00 { force_fit_type (TREE_TYPE (@00), wi::to_widest (@1), 0,
1897 TREE_OVERFLOW (@1)); })
1898 (cmp @00 (convert @1)))
1900 (if (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (TREE_TYPE (@00)))
1901 /* If possible, express the comparison in the shorter mode. */
1902 (if ((cmp == EQ_EXPR || cmp == NE_EXPR
1903 || TYPE_UNSIGNED (TREE_TYPE (@0)) == TYPE_UNSIGNED (TREE_TYPE (@00)))
1904 && (types_match (TREE_TYPE (@10), TREE_TYPE (@00))
1905 || ((TYPE_PRECISION (TREE_TYPE (@00))
1906 >= TYPE_PRECISION (TREE_TYPE (@10)))
1907 && (TYPE_UNSIGNED (TREE_TYPE (@00))
1908 == TYPE_UNSIGNED (TREE_TYPE (@10))))
1909 || (TREE_CODE (@10) == INTEGER_CST
1910 && (TREE_CODE (TREE_TYPE (@00)) == INTEGER_TYPE
1911 || TREE_CODE (TREE_TYPE (@00)) == BOOLEAN_TYPE)
1912 && int_fits_type_p (@10, TREE_TYPE (@00)))))
1913 (cmp @00 (convert @10))
1914 (if (TREE_CODE (@10) == INTEGER_CST
1915 && TREE_CODE (TREE_TYPE (@00)) == INTEGER_TYPE
1916 && !int_fits_type_p (@10, TREE_TYPE (@00)))
1919 tree min = lower_bound_in_type (TREE_TYPE (@10), TREE_TYPE (@00));
1920 tree max = upper_bound_in_type (TREE_TYPE (@10), TREE_TYPE (@00));
1921 bool above = integer_nonzerop (const_binop (LT_EXPR, type, max, @10));
1922 bool below = integer_nonzerop (const_binop (LT_EXPR, type, @10, min));
1924 (if (above || below)
1925 (if (cmp == EQ_EXPR || cmp == NE_EXPR)
1926 { constant_boolean_node (cmp == EQ_EXPR ? false : true, type); }
1927 (if (cmp == LT_EXPR || cmp == LE_EXPR)
1928 { constant_boolean_node (above ? true : false, type); }
1929 (if (cmp == GT_EXPR || cmp == GE_EXPR)
1930 { constant_boolean_node (above ? false : true, type); }))))))))))))
1933 /* A local variable can never be pointed to by
1934 the default SSA name of an incoming parameter.
1935 SSA names are canonicalized to 2nd place. */
1937 (cmp addr@0 SSA_NAME@1)
1938 (if (SSA_NAME_IS_DEFAULT_DEF (@1)
1939 && TREE_CODE (SSA_NAME_VAR (@1)) == PARM_DECL)
1940 (with { tree base = get_base_address (TREE_OPERAND (@0, 0)); }
1941 (if (TREE_CODE (base) == VAR_DECL
1942 && auto_var_in_fn_p (base, current_function_decl))
1943 (if (cmp == NE_EXPR)
1944 { constant_boolean_node (true, type); }
1945 { constant_boolean_node (false, type); }))))))
1947 /* Equality compare simplifications from fold_binary */
1950 /* If we have (A | C) == D where C & ~D != 0, convert this into 0.
1951 Similarly for NE_EXPR. */
1953 (cmp (convert?@3 (bit_ior @0 INTEGER_CST@1)) INTEGER_CST@2)
1954 (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0))
1955 && wi::bit_and_not (@1, @2) != 0)
1956 { constant_boolean_node (cmp == NE_EXPR, type); }))
1958 /* (X ^ Y) == 0 becomes X == Y, and (X ^ Y) != 0 becomes X != Y. */
1960 (cmp (bit_xor @0 @1) integer_zerop)
1963 /* (X ^ Y) == Y becomes X == 0.
1964 Likewise (X ^ Y) == X becomes Y == 0. */
1966 (cmp:c (bit_xor:c @0 @1) @0)
1967 (cmp @1 { build_zero_cst (TREE_TYPE (@1)); }))
1969 /* (X ^ C1) op C2 can be rewritten as X op (C1 ^ C2). */
1971 (cmp (convert?@3 (bit_xor @0 INTEGER_CST@1)) INTEGER_CST@2)
1972 (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0)))
1973 (cmp @0 (bit_xor @1 (convert @2)))))
1976 (cmp (convert? addr@0) integer_zerop)
1977 (if (tree_single_nonzero_warnv_p (@0, NULL))
1978 { constant_boolean_node (cmp == NE_EXPR, type); })))
1980 /* If we have (A & C) == C where C is a power of 2, convert this into
1981 (A & C) != 0. Similarly for NE_EXPR. */
1985 (cmp (bit_and@2 @0 integer_pow2p@1) @1)
1986 (icmp @2 { build_zero_cst (TREE_TYPE (@0)); })))
1988 /* If we have (A & C) != 0 where C is the sign bit of A, convert
1989 this into A < 0. Similarly for (A & C) == 0 into A >= 0. */
1993 (cmp (bit_and (convert?@2 @0) integer_pow2p@1) integer_zerop)
1994 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1995 && (TYPE_PRECISION (TREE_TYPE (@0))
1996 == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (@0))))
1997 && element_precision (@2) >= element_precision (@0)
1998 && wi::only_sign_bit_p (@1, element_precision (@0)))
1999 (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
2000 (ncmp (convert:stype @0) { build_zero_cst (stype); })))))
2002 /* When the addresses are not directly of decls compare base and offset.
2003 This implements some remaining parts of fold_comparison address
2004 comparisons but still no complete part of it. Still it is good
2005 enough to make fold_stmt not regress when not dispatching to fold_binary. */
2006 (for cmp (simple_comparison)
2008 (cmp (convert1?@2 addr@0) (convert2? addr@1))
2011 HOST_WIDE_INT off0, off1;
2012 tree base0 = get_addr_base_and_unit_offset (TREE_OPERAND (@0, 0), &off0);
2013 tree base1 = get_addr_base_and_unit_offset (TREE_OPERAND (@1, 0), &off1);
2014 if (base0 && TREE_CODE (base0) == MEM_REF)
2016 off0 += mem_ref_offset (base0).to_short_addr ();
2017 base0 = TREE_OPERAND (base0, 0);
2019 if (base1 && TREE_CODE (base1) == MEM_REF)
2021 off1 += mem_ref_offset (base1).to_short_addr ();
2022 base1 = TREE_OPERAND (base1, 0);
2025 (if (base0 && base1)
2029 if (decl_in_symtab_p (base0)
2030 && decl_in_symtab_p (base1))
2031 equal = symtab_node::get_create (base0)
2032 ->equal_address_to (symtab_node::get_create (base1));
2033 else if ((DECL_P (base0)
2034 || TREE_CODE (base0) == SSA_NAME
2035 || TREE_CODE (base0) == STRING_CST)
2037 || TREE_CODE (base1) == SSA_NAME
2038 || TREE_CODE (base1) == STRING_CST))
2039 equal = (base0 == base1);
2042 && (cmp == EQ_EXPR || cmp == NE_EXPR
2043 /* If the offsets are equal we can ignore overflow. */
2045 || POINTER_TYPE_OVERFLOW_UNDEFINED
2046 /* Or if we compare using pointers to decls or strings. */
2047 || (POINTER_TYPE_P (TREE_TYPE (@2))
2048 && (DECL_P (base0) || TREE_CODE (base0) == STRING_CST))))
2050 (if (cmp == EQ_EXPR)
2051 { constant_boolean_node (off0 == off1, type); })
2052 (if (cmp == NE_EXPR)
2053 { constant_boolean_node (off0 != off1, type); })
2054 (if (cmp == LT_EXPR)
2055 { constant_boolean_node (off0 < off1, type); })
2056 (if (cmp == LE_EXPR)
2057 { constant_boolean_node (off0 <= off1, type); })
2058 (if (cmp == GE_EXPR)
2059 { constant_boolean_node (off0 >= off1, type); })
2060 (if (cmp == GT_EXPR)
2061 { constant_boolean_node (off0 > off1, type); }))
2063 && DECL_P (base0) && DECL_P (base1)
2064 /* If we compare this as integers require equal offset. */
2065 && (!INTEGRAL_TYPE_P (TREE_TYPE (@2))
2068 (if (cmp == EQ_EXPR)
2069 { constant_boolean_node (false, type); })
2070 (if (cmp == NE_EXPR)
2071 { constant_boolean_node (true, type); })))))))))
2073 /* Non-equality compare simplifications from fold_binary */
2074 (for cmp (lt gt le ge)
2075 /* Comparisons with the highest or lowest possible integer of
2076 the specified precision will have known values. */
2078 (cmp (convert?@2 @0) INTEGER_CST@1)
2079 (if ((INTEGRAL_TYPE_P (TREE_TYPE (@1)) || POINTER_TYPE_P (TREE_TYPE (@1)))
2080 && tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@0)))
2083 tree arg1_type = TREE_TYPE (@1);
2084 unsigned int prec = TYPE_PRECISION (arg1_type);
2085 wide_int max = wi::max_value (arg1_type);
2086 wide_int signed_max = wi::max_value (prec, SIGNED);
2087 wide_int min = wi::min_value (arg1_type);
2090 (if (wi::eq_p (@1, max))
2092 (if (cmp == GT_EXPR)
2093 { constant_boolean_node (false, type); })
2094 (if (cmp == GE_EXPR)
2096 (if (cmp == LE_EXPR)
2097 { constant_boolean_node (true, type); })
2098 (if (cmp == LT_EXPR)
2100 (if (wi::eq_p (@1, min))
2102 (if (cmp == LT_EXPR)
2103 { constant_boolean_node (false, type); })
2104 (if (cmp == LE_EXPR)
2106 (if (cmp == GE_EXPR)
2107 { constant_boolean_node (true, type); })
2108 (if (cmp == GT_EXPR)
2110 (if (wi::eq_p (@1, max - 1))
2112 (if (cmp == GT_EXPR)
2113 (eq @2 { wide_int_to_tree (TREE_TYPE (@1), wi::add (@1, 1)); }))
2114 (if (cmp == LE_EXPR)
2115 (ne @2 { wide_int_to_tree (TREE_TYPE (@1), wi::add (@1, 1)); }))))
2116 (if (wi::eq_p (@1, min + 1))
2118 (if (cmp == GE_EXPR)
2119 (ne @2 { wide_int_to_tree (TREE_TYPE (@1), wi::sub (@1, 1)); }))
2120 (if (cmp == LT_EXPR)
2121 (eq @2 { wide_int_to_tree (TREE_TYPE (@1), wi::sub (@1, 1)); }))))
2122 (if (wi::eq_p (@1, signed_max)
2123 && TYPE_UNSIGNED (arg1_type)
2124 /* We will flip the signedness of the comparison operator
2125 associated with the mode of @1, so the sign bit is
2126 specified by this mode. Check that @1 is the signed
2127 max associated with this sign bit. */
2128 && prec == GET_MODE_PRECISION (TYPE_MODE (arg1_type))
2129 /* signed_type does not work on pointer types. */
2130 && INTEGRAL_TYPE_P (arg1_type))
2131 /* The following case also applies to X < signed_max+1
2132 and X >= signed_max+1 because previous transformations. */
2133 (if (cmp == LE_EXPR || cmp == GT_EXPR)
2134 (with { tree st = signed_type_for (arg1_type); }
2135 (if (cmp == LE_EXPR)
2136 (ge (convert:st @0) { build_zero_cst (st); })
2137 (lt (convert:st @0) { build_zero_cst (st); }))))))))))
2139 (for cmp (unordered ordered unlt unle ungt unge uneq ltgt)
2140 /* If the second operand is NaN, the result is constant. */
2143 (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
2144 && (cmp != LTGT_EXPR || ! flag_trapping_math))
2145 { constant_boolean_node (cmp == ORDERED_EXPR || cmp == LTGT_EXPR
2146 ? false : true, type); })))
2148 /* bool_var != 0 becomes bool_var. */
2150 (ne @0 integer_zerop)
2151 (if (TREE_CODE (TREE_TYPE (@0)) == BOOLEAN_TYPE
2152 && types_match (type, TREE_TYPE (@0)))
2154 /* bool_var == 1 becomes bool_var. */
2156 (eq @0 integer_onep)
2157 (if (TREE_CODE (TREE_TYPE (@0)) == BOOLEAN_TYPE
2158 && types_match (type, TREE_TYPE (@0)))
2161 bool_var == 0 becomes !bool_var or
2162 bool_var != 1 becomes !bool_var
2163 here because that only is good in assignment context as long
2164 as we require a tcc_comparison in GIMPLE_CONDs where we'd
2165 replace if (x == 0) with tem = ~x; if (tem != 0) which is
2166 clearly less optimal and which we'll transform again in forwprop. */
2169 /* Simplification of math builtins. These rules must all be optimizations
2170 as well as IL simplifications. If there is a possibility that the new
2171 form could be a pessimization, the rule should go in the canonicalization
2172 section that follows this one.
2174 Rules can generally go in this section if they satisfy one of
2177 - the rule describes an identity
2179 - the rule replaces calls with something as simple as addition or
2182 - the rule contains unary calls only and simplifies the surrounding
2183 arithmetic. (The idea here is to exclude non-unary calls in which
2184 one operand is constant and in which the call is known to be cheap
2185 when the operand has that value.) */
2187 (if (flag_unsafe_math_optimizations)
2188 /* Simplify sqrt(x) * sqrt(x) -> x. */
2190 (mult (SQRT@1 @0) @1)
2191 (if (!HONOR_SNANS (type))
2194 /* Simplify sqrt(x) * sqrt(y) -> sqrt(x*y). */
2195 (for root (SQRT CBRT)
2197 (mult (root:s @0) (root:s @1))
2198 (root (mult @0 @1))))
2200 /* Simplify expN(x) * expN(y) -> expN(x+y). */
2201 (for exps (EXP EXP2 EXP10 POW10)
2203 (mult (exps:s @0) (exps:s @1))
2204 (exps (plus @0 @1))))
2206 /* Simplify a/root(b/c) into a*root(c/b). */
2207 (for root (SQRT CBRT)
2209 (rdiv @0 (root:s (rdiv:s @1 @2)))
2210 (mult @0 (root (rdiv @2 @1)))))
2212 /* Simplify x/expN(y) into x*expN(-y). */
2213 (for exps (EXP EXP2 EXP10 POW10)
2215 (rdiv @0 (exps:s @1))
2216 (mult @0 (exps (negate @1)))))
2218 /* Special case, optimize logN(expN(x)) = x. */
2219 (for logs (LOG LOG2 LOG10 LOG10)
2220 exps (EXP EXP2 EXP10 POW10)
2225 /* Optimize logN(func()) for various exponential functions. We
2226 want to determine the value "x" and the power "exponent" in
2227 order to transform logN(x**exponent) into exponent*logN(x). */
2228 (for logs (LOG LOG LOG LOG2 LOG2 LOG2 LOG10 LOG10)
2229 exps (EXP2 EXP10 POW10 EXP EXP10 POW10 EXP EXP2)
2236 CASE_FLT_FN (BUILT_IN_EXP):
2237 /* Prepare to do logN(exp(exponent)) -> exponent*logN(e). */
2238 x = build_real_truncate (type, dconst_e ());
2240 CASE_FLT_FN (BUILT_IN_EXP2):
2241 /* Prepare to do logN(exp2(exponent)) -> exponent*logN(2). */
2242 x = build_real (type, dconst2);
2244 CASE_FLT_FN (BUILT_IN_EXP10):
2245 CASE_FLT_FN (BUILT_IN_POW10):
2246 /* Prepare to do logN(exp10(exponent)) -> exponent*logN(10). */
2248 REAL_VALUE_TYPE dconst10;
2249 real_from_integer (&dconst10, VOIDmode, 10, SIGNED);
2250 x = build_real (type, dconst10);
2257 (mult (logs { x; }) @0))))
2269 CASE_FLT_FN (BUILT_IN_SQRT):
2270 /* Prepare to do logN(sqrt(x)) -> 0.5*logN(x). */
2271 x = build_real (type, dconsthalf);
2273 CASE_FLT_FN (BUILT_IN_CBRT):
2274 /* Prepare to do logN(cbrt(x)) -> (1/3)*logN(x). */
2275 x = build_real_truncate (type, dconst_third ());
2281 (mult { x; } (logs @0)))))
2283 /* logN(pow(x,exponent)) -> exponent*logN(x). */
2284 (for logs (LOG LOG2 LOG10)
2288 (mult @1 (logs @0))))
2292 exps (EXP EXP2 EXP10 POW10)
2293 /* sqrt(expN(x)) -> expN(x*0.5). */
2296 (exps (mult @0 { build_real (type, dconsthalf); })))
2297 /* cbrt(expN(x)) -> expN(x/3). */
2300 (exps (mult @0 { build_real_truncate (type, dconst_third ()); })))))
2302 /* Canonicalization of sequences of math builtins. These rules represent
2303 IL simplifications but are not necessarily optimizations.
2305 The sincos pass is responsible for picking "optimal" implementations
2306 of math builtins, which may be more complicated and can sometimes go
2307 the other way, e.g. converting pow into a sequence of sqrts.
2308 We only want to do these canonicalizations before the pass has run. */
2310 (if (flag_unsafe_math_optimizations && canonicalize_math_p ())
2311 /* Simplify tan(x) * cos(x) -> sin(x). */
2313 (mult:c (TAN:s @0) (COS:s @0))
2316 /* Simplify x * pow(x,c) -> pow(x,c+1). */
2318 (mult @0 (POW:s @0 REAL_CST@1))
2319 (if (!TREE_OVERFLOW (@1))
2320 (POW @0 (plus @1 { build_one_cst (type); }))))
2322 /* Simplify sin(x) / cos(x) -> tan(x). */
2324 (rdiv (SIN:s @0) (COS:s @0))
2327 /* Simplify cos(x) / sin(x) -> 1 / tan(x). */
2329 (rdiv (COS:s @0) (SIN:s @0))
2330 (rdiv { build_one_cst (type); } (TAN @0)))
2332 /* Simplify sin(x) / tan(x) -> cos(x). */
2334 (rdiv (SIN:s @0) (TAN:s @0))
2335 (if (! HONOR_NANS (@0)
2336 && ! HONOR_INFINITIES (@0))
2339 /* Simplify tan(x) / sin(x) -> 1.0 / cos(x). */
2341 (rdiv (TAN:s @0) (SIN:s @0))
2342 (if (! HONOR_NANS (@0)
2343 && ! HONOR_INFINITIES (@0))
2344 (rdiv { build_one_cst (type); } (COS @0))))
2346 /* Simplify pow(x,y) * pow(x,z) -> pow(x,y+z). */
2348 (mult (POW:s @0 @1) (POW:s @0 @2))
2349 (POW @0 (plus @1 @2)))
2351 /* Simplify pow(x,y) * pow(z,y) -> pow(x*z,y). */
2353 (mult (POW:s @0 @1) (POW:s @2 @1))
2354 (POW (mult @0 @2) @1))
2356 /* Simplify pow(x,c) / x -> pow(x,c-1). */
2358 (rdiv (POW:s @0 REAL_CST@1) @0)
2359 (if (!TREE_OVERFLOW (@1))
2360 (POW @0 (minus @1 { build_one_cst (type); }))))
2362 /* Simplify x / pow (y,z) -> x * pow(y,-z). */
2364 (rdiv @0 (POW:s @1 @2))
2365 (mult @0 (POW @1 (negate @2))))
2370 /* sqrt(sqrt(x)) -> pow(x,1/4). */
2373 (pows @0 { build_real (type, dconst_quarter ()); }))
2374 /* sqrt(cbrt(x)) -> pow(x,1/6). */
2377 (pows @0 { build_real_truncate (type, dconst_sixth ()); }))
2378 /* cbrt(sqrt(x)) -> pow(x,1/6). */
2381 (pows @0 { build_real_truncate (type, dconst_sixth ()); }))
2382 /* cbrt(cbrt(x)) -> pow(x,1/9), iff x is nonnegative. */
2384 (cbrts (cbrts tree_expr_nonnegative_p@0))
2385 (pows @0 { build_real_truncate (type, dconst_ninth ()); }))
2386 /* sqrt(pow(x,y)) -> pow(|x|,y*0.5). */
2388 (sqrts (pows @0 @1))
2389 (pows (abs @0) (mult @1 { build_real (type, dconsthalf); })))
2390 /* cbrt(pow(x,y)) -> pow(x,y/3), iff x is nonnegative. */
2392 (cbrts (pows tree_expr_nonnegative_p@0 @1))
2393 (pows @0 (mult @1 { build_real_truncate (type, dconst_third ()); })))))
2395 /* If the real part is inf and the imag part is known to be
2396 nonnegative, return (inf + 0i). */
2398 (CPROJ (complex REAL_CST@0 tree_expr_nonnegative_p@1))
2399 (if (real_isinf (TREE_REAL_CST_PTR (@0)))
2402 REAL_VALUE_TYPE rinf;
2405 { build_complex (type, build_real (TREE_TYPE (type), rinf),
2406 build_zero_cst (TREE_TYPE (type))); })))
2407 /* If the imag part is inf, return (inf+I*copysign(0,imag)). */
2409 (CPROJ (complex @0 REAL_CST@1))
2410 (if (real_isinf (TREE_REAL_CST_PTR (@1)))
2413 REAL_VALUE_TYPE rinf, rzero = dconst0;
2415 rzero.sign = TREE_REAL_CST_PTR (@1)->sign;
2417 { build_complex (type, build_real (TREE_TYPE (type), rinf),
2418 build_real (TREE_TYPE (type), rzero)); })))
2421 /* Narrowing of arithmetic and logical operations.
2423 These are conceptually similar to the transformations performed for
2424 the C/C++ front-ends by shorten_binary_op and shorten_compare. Long
2425 term we want to move all that code out of the front-ends into here. */
2427 /* If we have a narrowing conversion of an arithmetic operation where
2428 both operands are widening conversions from the same type as the outer
2429 narrowing conversion. Then convert the innermost operands to a suitable
2430 unsigned type (to avoid introducing undefined behaviour), perform the
2431 operation and convert the result to the desired type. */
2432 (for op (plus minus)
2434 (convert (op:s (convert@2 @0) (convert@3 @1)))
2435 (if (INTEGRAL_TYPE_P (type)
2436 /* We check for type compatibility between @0 and @1 below,
2437 so there's no need to check that @1/@3 are integral types. */
2438 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
2439 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
2440 /* The precision of the type of each operand must match the
2441 precision of the mode of each operand, similarly for the
2443 && (TYPE_PRECISION (TREE_TYPE (@0))
2444 == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (@0))))
2445 && (TYPE_PRECISION (TREE_TYPE (@1))
2446 == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (@1))))
2447 && TYPE_PRECISION (type) == GET_MODE_PRECISION (TYPE_MODE (type))
2448 /* The inner conversion must be a widening conversion. */
2449 && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
2450 && types_match (@0, @1)
2451 && types_match (@0, type))
2452 (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
2453 (convert (op @0 @1))
2454 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
2455 (convert (op (convert:utype @0) (convert:utype @1))))))))
2457 /* This is another case of narrowing, specifically when there's an outer
2458 BIT_AND_EXPR which masks off bits outside the type of the innermost
2459 operands. Like the previous case we have to convert the operands
2460 to unsigned types to avoid introducing undefined behaviour for the
2461 arithmetic operation. */
2462 (for op (minus plus)
2464 (bit_and (op:s (convert@2 @0) (convert@3 @1)) INTEGER_CST@4)
2465 (if (INTEGRAL_TYPE_P (type)
2466 /* We check for type compatibility between @0 and @1 below,
2467 so there's no need to check that @1/@3 are integral types. */
2468 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
2469 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
2470 /* The precision of the type of each operand must match the
2471 precision of the mode of each operand, similarly for the
2473 && (TYPE_PRECISION (TREE_TYPE (@0))
2474 == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (@0))))
2475 && (TYPE_PRECISION (TREE_TYPE (@1))
2476 == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (@1))))
2477 && TYPE_PRECISION (type) == GET_MODE_PRECISION (TYPE_MODE (type))
2478 /* The inner conversion must be a widening conversion. */
2479 && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
2480 && types_match (@0, @1)
2481 && (tree_int_cst_min_precision (@4, TYPE_SIGN (TREE_TYPE (@0)))
2482 <= TYPE_PRECISION (TREE_TYPE (@0)))
2483 && (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))
2484 || tree_int_cst_sgn (@4) >= 0))
2485 (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
2486 (with { tree ntype = TREE_TYPE (@0); }
2487 (convert (bit_and (op @0 @1) (convert:ntype @4))))
2488 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
2489 (convert (bit_and (op (convert:utype @0) (convert:utype @1))
2490 (convert:utype @4))))))))
2492 /* Transform (@0 < @1 and @0 < @2) to use min,
2493 (@0 > @1 and @0 > @2) to use max */
2494 (for op (lt le gt ge)
2495 ext (min min max max)
2497 (bit_and (op:s @0 @1) (op:s @0 @2))
2498 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)))
2499 (op @0 (ext @1 @2)))))