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)
65 /* Simplifications of operations with one constant operand and
66 simplifications to constants or single values. */
68 (for op (plus pointer_plus minus bit_ior bit_xor)
73 /* 0 +p index -> (type)index */
75 (pointer_plus integer_zerop @1)
76 (non_lvalue (convert @1)))
78 /* See if ARG1 is zero and X + ARG1 reduces to X.
79 Likewise if the operands are reversed. */
81 (plus:c @0 real_zerop@1)
82 (if (fold_real_zero_addition_p (type, @1, 0))
85 /* See if ARG1 is zero and X - ARG1 reduces to X. */
87 (minus @0 real_zerop@1)
88 (if (fold_real_zero_addition_p (type, @1, 1))
92 This is unsafe for certain floats even in non-IEEE formats.
93 In IEEE, it is unsafe because it does wrong for NaNs.
94 Also note that operand_equal_p is always false if an operand
98 (if (!FLOAT_TYPE_P (type) || !HONOR_NANS (type))
99 { build_zero_cst (type); }))
102 (mult @0 integer_zerop@1)
105 /* Maybe fold x * 0 to 0. The expressions aren't the same
106 when x is NaN, since x * 0 is also NaN. Nor are they the
107 same in modes with signed zeros, since multiplying a
108 negative value by 0 gives -0, not +0. */
110 (mult @0 real_zerop@1)
111 (if (!HONOR_NANS (type) && !HONOR_SIGNED_ZEROS (type))
114 /* In IEEE floating point, x*1 is not equivalent to x for snans.
115 Likewise for complex arithmetic with signed zeros. */
118 (if (!HONOR_SNANS (type)
119 && (!HONOR_SIGNED_ZEROS (type)
120 || !COMPLEX_FLOAT_TYPE_P (type)))
123 /* Transform x * -1.0 into -x. */
125 (mult @0 real_minus_onep)
126 (if (!HONOR_SNANS (type)
127 && (!HONOR_SIGNED_ZEROS (type)
128 || !COMPLEX_FLOAT_TYPE_P (type)))
131 /* Make sure to preserve divisions by zero. This is the reason why
132 we don't simplify x / x to 1 or 0 / x to 0. */
133 (for op (mult trunc_div ceil_div floor_div round_div exact_div)
139 (for div (trunc_div ceil_div floor_div round_div exact_div)
141 (div @0 integer_minus_onep@1)
142 (if (!TYPE_UNSIGNED (type))
145 /* For unsigned integral types, FLOOR_DIV_EXPR is the same as
146 TRUNC_DIV_EXPR. Rewrite into the latter in this case. */
149 (if ((INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
150 && TYPE_UNSIGNED (type))
153 /* Combine two successive divisions. Note that combining ceil_div
154 and floor_div is trickier and combining round_div even more so. */
155 (for div (trunc_div exact_div)
157 (div (div @0 INTEGER_CST@1) INTEGER_CST@2)
160 wide_int mul = wi::mul (@1, @2, TYPE_SIGN (type), &overflow_p);
163 (div @0 { wide_int_to_tree (type, mul); })
164 (if (TYPE_UNSIGNED (type)
165 || mul != wi::min_value (TYPE_PRECISION (type), SIGNED))
166 { build_zero_cst (type); })))))
168 /* Optimize A / A to 1.0 if we don't care about
169 NaNs or Infinities. */
172 (if (FLOAT_TYPE_P (type)
173 && ! HONOR_NANS (type)
174 && ! HONOR_INFINITIES (type))
175 { build_one_cst (type); }))
177 /* Optimize -A / A to -1.0 if we don't care about
178 NaNs or Infinities. */
180 (rdiv:c @0 (negate @0))
181 (if (FLOAT_TYPE_P (type)
182 && ! HONOR_NANS (type)
183 && ! HONOR_INFINITIES (type))
184 { build_minus_one_cst (type); }))
186 /* In IEEE floating point, x/1 is not equivalent to x for snans. */
189 (if (!HONOR_SNANS (type))
192 /* In IEEE floating point, x/-1 is not equivalent to -x for snans. */
194 (rdiv @0 real_minus_onep)
195 (if (!HONOR_SNANS (type))
198 /* If ARG1 is a constant, we can convert this to a multiply by the
199 reciprocal. This does not have the same rounding properties,
200 so only do this if -freciprocal-math. We can actually
201 always safely do it if ARG1 is a power of two, but it's hard to
202 tell if it is or not in a portable manner. */
203 (for cst (REAL_CST COMPLEX_CST VECTOR_CST)
207 (if (flag_reciprocal_math
210 { tree tem = const_binop (RDIV_EXPR, type, build_one_cst (type), @1); }
212 (mult @0 { tem; } )))
213 (if (cst != COMPLEX_CST)
214 (with { tree inverse = exact_inverse (type, @1); }
216 (mult @0 { inverse; } ))))))))
218 /* Same applies to modulo operations, but fold is inconsistent here
219 and simplifies 0 % x to 0, only preserving literal 0 % 0. */
220 (for mod (ceil_mod floor_mod round_mod trunc_mod)
221 /* 0 % X is always zero. */
223 (mod integer_zerop@0 @1)
224 /* But not for 0 % 0 so that we can get the proper warnings and errors. */
225 (if (!integer_zerop (@1))
227 /* X % 1 is always zero. */
229 (mod @0 integer_onep)
230 { build_zero_cst (type); })
231 /* X % -1 is zero. */
233 (mod @0 integer_minus_onep@1)
234 (if (!TYPE_UNSIGNED (type))
235 { build_zero_cst (type); }))
236 /* (X % Y) % Y is just X % Y. */
238 (mod (mod@2 @0 @1) @1)
240 /* From extract_muldiv_1: (X * C1) % C2 is zero if C1 is a multiple of C2. */
242 (mod (mult @0 INTEGER_CST@1) INTEGER_CST@2)
243 (if (ANY_INTEGRAL_TYPE_P (type)
244 && TYPE_OVERFLOW_UNDEFINED (type)
245 && wi::multiple_of_p (@1, @2, TYPE_SIGN (type)))
246 { build_zero_cst (type); })))
248 /* X % -C is the same as X % C. */
250 (trunc_mod @0 INTEGER_CST@1)
251 (if (TYPE_SIGN (type) == SIGNED
252 && !TREE_OVERFLOW (@1)
254 && !TYPE_OVERFLOW_TRAPS (type)
255 /* Avoid this transformation if C is INT_MIN, i.e. C == -C. */
256 && !sign_bit_p (@1, @1))
257 (trunc_mod @0 (negate @1))))
259 /* X % -Y is the same as X % Y. */
261 (trunc_mod @0 (convert? (negate @1)))
262 (if (!TYPE_UNSIGNED (type)
263 && !TYPE_OVERFLOW_TRAPS (type)
264 && tree_nop_conversion_p (type, TREE_TYPE (@1)))
265 (trunc_mod @0 (convert @1))))
267 /* X - (X / Y) * Y is the same as X % Y. */
269 (minus (convert1? @0) (convert2? (mult (trunc_div @0 @1) @1)))
270 (if (INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
271 (trunc_mod (convert @0) (convert @1))))
273 /* Optimize TRUNC_MOD_EXPR by a power of two into a BIT_AND_EXPR,
274 i.e. "X % C" into "X & (C - 1)", if X and C are positive.
275 Also optimize A % (C << N) where C is a power of 2,
276 to A & ((C << N) - 1). */
277 (match (power_of_two_cand @1)
279 (match (power_of_two_cand @1)
280 (lshift INTEGER_CST@1 @2))
281 (for mod (trunc_mod floor_mod)
283 (mod @0 (convert?@3 (power_of_two_cand@1 @2)))
284 (if ((TYPE_UNSIGNED (type)
285 || tree_expr_nonnegative_p (@0))
286 && tree_nop_conversion_p (type, TREE_TYPE (@3))
287 && integer_pow2p (@2) && tree_int_cst_sgn (@2) > 0)
288 (bit_and @0 (convert (minus @1 { build_int_cst (TREE_TYPE (@1), 1); }))))))
290 /* Simplify (unsigned t * 2)/2 -> unsigned t & 0x7FFFFFFF. */
292 (trunc_div (mult @0 integer_pow2p@1) @1)
293 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
294 (bit_and @0 { wide_int_to_tree
295 (type, wi::mask (TYPE_PRECISION (type) - wi::exact_log2 (@1),
296 false, TYPE_PRECISION (type))); })))
298 /* Simplify (unsigned t / 2) * 2 -> unsigned t & ~1. */
300 (mult (trunc_div @0 integer_pow2p@1) @1)
301 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
302 (bit_and @0 (negate @1))))
304 /* Simplify (t * 2) / 2) -> t. */
305 (for div (trunc_div ceil_div floor_div round_div exact_div)
307 (div (mult @0 @1) @1)
308 (if (ANY_INTEGRAL_TYPE_P (type)
309 && TYPE_OVERFLOW_UNDEFINED (type))
312 /* Simplify cos (-x) -> cos (x). */
319 /* X % Y is smaller than Y. */
322 (cmp (trunc_mod @0 @1) @1)
323 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
324 { constant_boolean_node (cmp == LT_EXPR, type); })))
327 (cmp @1 (trunc_mod @0 @1))
328 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
329 { constant_boolean_node (cmp == GT_EXPR, type); })))
333 (bit_ior @0 integer_all_onesp@1)
338 (bit_and @0 integer_zerop@1)
344 (for op (bit_ior bit_xor plus)
346 (op:c (convert? @0) (convert? (bit_not @0)))
347 (convert { build_all_ones_cst (TREE_TYPE (@0)); })))
352 { build_zero_cst (type); })
354 /* Canonicalize X ^ ~0 to ~X. */
356 (bit_xor @0 integer_all_onesp@1)
361 (bit_and @0 integer_all_onesp)
364 /* x & x -> x, x | x -> x */
365 (for bitop (bit_and bit_ior)
370 /* x + (x & 1) -> (x + 1) & ~1 */
372 (plus:c @0 (bit_and:s @0 integer_onep@1))
373 (bit_and (plus @0 @1) (bit_not @1)))
375 /* x & ~(x & y) -> x & ~y */
376 /* x | ~(x | y) -> x | ~y */
377 (for bitop (bit_and bit_ior)
379 (bitop:c @0 (bit_not (bitop:cs @0 @1)))
380 (bitop @0 (bit_not @1))))
382 /* (x | y) & ~x -> y & ~x */
383 /* (x & y) | ~x -> y | ~x */
384 (for bitop (bit_and bit_ior)
385 rbitop (bit_ior bit_and)
387 (bitop:c (rbitop:c @0 @1) (bit_not@2 @0))
390 /* (x & y) ^ (x | y) -> x ^ y */
392 (bit_xor:c (bit_and @0 @1) (bit_ior @0 @1))
395 /* (x ^ y) ^ (x | y) -> x & y */
397 (bit_xor:c (bit_xor @0 @1) (bit_ior @0 @1))
400 /* (x & y) + (x ^ y) -> x | y */
401 /* (x & y) | (x ^ y) -> x | y */
402 /* (x & y) ^ (x ^ y) -> x | y */
403 (for op (plus bit_ior bit_xor)
405 (op:c (bit_and @0 @1) (bit_xor @0 @1))
408 /* (x & y) + (x | y) -> x + y */
410 (plus:c (bit_and @0 @1) (bit_ior @0 @1))
413 /* (x + y) - (x | y) -> x & y */
415 (minus (plus @0 @1) (bit_ior @0 @1))
416 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
417 && !TYPE_SATURATING (type))
420 /* (x + y) - (x & y) -> x | y */
422 (minus (plus @0 @1) (bit_and @0 @1))
423 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
424 && !TYPE_SATURATING (type))
427 /* (x | y) - (x ^ y) -> x & y */
429 (minus (bit_ior @0 @1) (bit_xor @0 @1))
432 /* (x | y) - (x & y) -> x ^ y */
434 (minus (bit_ior @0 @1) (bit_and @0 @1))
437 /* (x | y) & ~(x & y) -> x ^ y */
439 (bit_and:c (bit_ior @0 @1) (bit_not (bit_and @0 @1)))
442 /* (x | y) & (~x ^ y) -> x & y */
444 (bit_and:c (bit_ior:c @0 @1) (bit_xor:c @1 (bit_not @0)))
447 /* ~x & ~y -> ~(x | y)
448 ~x | ~y -> ~(x & y) */
449 (for op (bit_and bit_ior)
450 rop (bit_ior bit_and)
452 (op (convert1? (bit_not @0)) (convert2? (bit_not @1)))
453 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
454 && tree_nop_conversion_p (type, TREE_TYPE (@1)))
455 (bit_not (rop (convert @0) (convert @1))))))
457 /* If we are XORing or adding two BIT_AND_EXPR's, both of which are and'ing
458 with a constant, and the two constants have no bits in common,
459 we should treat this as a BIT_IOR_EXPR since this may produce more
461 (for op (bit_xor plus)
463 (op (convert1? (bit_and@4 @0 INTEGER_CST@1))
464 (convert2? (bit_and@5 @2 INTEGER_CST@3)))
465 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
466 && tree_nop_conversion_p (type, TREE_TYPE (@2))
467 && wi::bit_and (@1, @3) == 0)
468 (bit_ior (convert @4) (convert @5)))))
470 /* (X | Y) ^ X -> Y & ~ X*/
472 (bit_xor:c (convert? (bit_ior:c @0 @1)) (convert? @0))
473 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
474 (convert (bit_and @1 (bit_not @0)))))
476 /* Convert ~X ^ ~Y to X ^ Y. */
478 (bit_xor (convert1? (bit_not @0)) (convert2? (bit_not @1)))
479 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
480 && tree_nop_conversion_p (type, TREE_TYPE (@1)))
481 (bit_xor (convert @0) (convert @1))))
483 /* Convert ~X ^ C to X ^ ~C. */
485 (bit_xor (convert? (bit_not @0)) INTEGER_CST@1)
486 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
487 (bit_xor (convert @0) (bit_not @1))))
489 /* Fold (X & Y) ^ Y as ~X & Y. */
491 (bit_xor:c (bit_and:c @0 @1) @1)
492 (bit_and (bit_not @0) @1))
494 /* Given a bit-wise operation CODE applied to ARG0 and ARG1, see if both
495 operands are another bit-wise operation with a common input. If so,
496 distribute the bit operations to save an operation and possibly two if
497 constants are involved. For example, convert
498 (A | B) & (A | C) into A | (B & C)
499 Further simplification will occur if B and C are constants. */
500 (for op (bit_and bit_ior)
501 rop (bit_ior bit_and)
503 (op (convert? (rop:c @0 @1)) (convert? (rop @0 @2)))
504 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
505 (rop (convert @0) (op (convert @1) (convert @2))))))
515 (abs tree_expr_nonnegative_p@0)
518 /* A few cases of fold-const.c negate_expr_p predicate. */
521 (if ((INTEGRAL_TYPE_P (type)
522 && TYPE_OVERFLOW_WRAPS (type))
523 || (!TYPE_OVERFLOW_SANITIZED (type)
524 && may_negate_without_overflow_p (t)))))
529 (if (!TYPE_OVERFLOW_SANITIZED (type))))
532 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (t)))))
533 /* VECTOR_CST handling of non-wrapping types would recurse in unsupported
537 (if (FLOAT_TYPE_P (TREE_TYPE (type)) || TYPE_OVERFLOW_WRAPS (type))))
539 /* -(A + B) -> (-B) - A. */
541 (negate (plus:c @0 negate_expr_p@1))
542 (if (!HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type))
543 && !HONOR_SIGNED_ZEROS (element_mode (type)))
544 (minus (negate @1) @0)))
546 /* A - B -> A + (-B) if B is easily negatable. */
548 (minus @0 negate_expr_p@1)
549 (if (!FIXED_POINT_TYPE_P (type))
550 (plus @0 (negate @1))))
552 /* Try to fold (type) X op CST -> (type) (X op ((type-x) CST))
554 For bitwise binary operations apply operand conversions to the
555 binary operation result instead of to the operands. This allows
556 to combine successive conversions and bitwise binary operations.
557 We combine the above two cases by using a conditional convert. */
558 (for bitop (bit_and bit_ior bit_xor)
560 (bitop (convert @0) (convert? @1))
561 (if (((TREE_CODE (@1) == INTEGER_CST
562 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
563 && int_fits_type_p (@1, TREE_TYPE (@0)))
564 || types_match (@0, @1))
565 /* ??? This transform conflicts with fold-const.c doing
566 Convert (T)(x & c) into (T)x & (T)c, if c is an integer
567 constants (if x has signed type, the sign bit cannot be set
568 in c). This folds extension into the BIT_AND_EXPR.
569 Restrict it to GIMPLE to avoid endless recursions. */
570 && (bitop != BIT_AND_EXPR || GIMPLE)
571 && (/* That's a good idea if the conversion widens the operand, thus
572 after hoisting the conversion the operation will be narrower. */
573 TYPE_PRECISION (TREE_TYPE (@0)) < TYPE_PRECISION (type)
574 /* It's also a good idea if the conversion is to a non-integer
576 || GET_MODE_CLASS (TYPE_MODE (type)) != MODE_INT
577 /* Or if the precision of TO is not the same as the precision
579 || TYPE_PRECISION (type) != GET_MODE_PRECISION (TYPE_MODE (type))))
580 (convert (bitop @0 (convert @1))))))
582 (for bitop (bit_and bit_ior)
583 rbitop (bit_ior bit_and)
584 /* (x | y) & x -> x */
585 /* (x & y) | x -> x */
587 (bitop:c (rbitop:c @0 @1) @0)
589 /* (~x | y) & x -> x & y */
590 /* (~x & y) | x -> x | y */
592 (bitop:c (rbitop:c (bit_not @0) @1) @0)
595 /* Simplify (A & B) OP0 (C & B) to (A OP0 C) & B. */
596 (for bitop (bit_and bit_ior bit_xor)
598 (bitop (bit_and:c @0 @1) (bit_and @2 @1))
599 (bit_and (bitop @0 @2) @1)))
601 /* (x | CST1) & CST2 -> (x & CST2) | (CST1 & CST2) */
603 (bit_and (bit_ior @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
604 (bit_ior (bit_and @0 @2) (bit_and @1 @2)))
606 /* Combine successive equal operations with constants. */
607 (for bitop (bit_and bit_ior bit_xor)
609 (bitop (bitop @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
610 (bitop @0 (bitop @1 @2))))
612 /* Try simple folding for X op !X, and X op X with the help
613 of the truth_valued_p and logical_inverted_value predicates. */
614 (match truth_valued_p
616 (if (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1)))
617 (for op (tcc_comparison truth_and truth_andif truth_or truth_orif truth_xor)
618 (match truth_valued_p
620 (match truth_valued_p
623 (match (logical_inverted_value @0)
624 (bit_not truth_valued_p@0))
625 (match (logical_inverted_value @0)
626 (eq @0 integer_zerop))
627 (match (logical_inverted_value @0)
628 (ne truth_valued_p@0 integer_truep))
629 (match (logical_inverted_value @0)
630 (bit_xor truth_valued_p@0 integer_truep))
634 (bit_and:c @0 (logical_inverted_value @0))
635 { build_zero_cst (type); })
636 /* X | !X and X ^ !X -> 1, , if X is truth-valued. */
637 (for op (bit_ior bit_xor)
639 (op:c truth_valued_p@0 (logical_inverted_value @0))
640 { constant_boolean_node (true, type); }))
641 /* X ==/!= !X is false/true. */
644 (op:c truth_valued_p@0 (logical_inverted_value @0))
645 { constant_boolean_node (op == NE_EXPR ? true : false, type); }))
647 /* If arg1 and arg2 are booleans (or any single bit type)
648 then try to simplify:
655 But only do this if our result feeds into a comparison as
656 this transformation is not always a win, particularly on
657 targets with and-not instructions.
658 -> simplify_bitwise_binary_boolean */
660 (ne (bit_and:c (bit_not @0) @1) integer_zerop)
661 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
662 && TYPE_PRECISION (TREE_TYPE (@1)) == 1)
665 (ne (bit_ior:c (bit_not @0) @1) integer_zerop)
666 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
667 && TYPE_PRECISION (TREE_TYPE (@1)) == 1)
672 (bit_not (bit_not @0))
675 /* Convert ~ (-A) to A - 1. */
677 (bit_not (convert? (negate @0)))
678 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
679 (convert (minus @0 { build_each_one_cst (TREE_TYPE (@0)); }))))
681 /* Convert ~ (A - 1) or ~ (A + -1) to -A. */
683 (bit_not (convert? (minus @0 integer_each_onep)))
684 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
685 (convert (negate @0))))
687 (bit_not (convert? (plus @0 integer_all_onesp)))
688 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
689 (convert (negate @0))))
691 /* Part of convert ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify. */
693 (bit_not (convert? (bit_xor @0 INTEGER_CST@1)))
694 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
695 (convert (bit_xor @0 (bit_not @1)))))
697 (bit_not (convert? (bit_xor:c (bit_not @0) @1)))
698 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
699 (convert (bit_xor @0 @1))))
701 /* (x & ~m) | (y & m) -> ((x ^ y) & m) ^ x */
703 (bit_ior:c (bit_and:cs @0 (bit_not @2)) (bit_and:cs @1 @2))
704 (bit_xor (bit_and (bit_xor @0 @1) @2) @0))
706 /* Fold A - (A & B) into ~B & A. */
708 (minus (convert? @0) (convert?:s (bit_and:cs @0 @1)))
709 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
710 && tree_nop_conversion_p (type, TREE_TYPE (@1)))
711 (convert (bit_and (bit_not @1) @0))))
713 /* Associate (p +p off1) +p off2 as (p +p (off1 + off2)). */
715 (pointer_plus (pointer_plus:s @0 @1) @3)
716 (pointer_plus @0 (plus @1 @3)))
722 tem4 = (unsigned long) tem3;
727 (pointer_plus @0 (convert?@2 (minus@3 (convert @1) (convert @0))))
728 /* Conditionally look through a sign-changing conversion. */
729 (if (TYPE_PRECISION (TREE_TYPE (@2)) == TYPE_PRECISION (TREE_TYPE (@3))
730 && ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@1)))
731 || (GENERIC && type == TREE_TYPE (@1))))
735 tem = (sizetype) ptr;
739 and produce the simpler and easier to analyze with respect to alignment
740 ... = ptr & ~algn; */
742 (pointer_plus @0 (negate (bit_and (convert @0) INTEGER_CST@1)))
743 (with { tree algn = wide_int_to_tree (TREE_TYPE (@0), wi::bit_not (@1)); }
744 (bit_and @0 { algn; })))
746 /* Try folding difference of addresses. */
748 (minus (convert ADDR_EXPR@0) (convert @1))
749 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
750 (with { HOST_WIDE_INT diff; }
751 (if (ptr_difference_const (@0, @1, &diff))
752 { build_int_cst_type (type, diff); }))))
754 (minus (convert @0) (convert ADDR_EXPR@1))
755 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
756 (with { HOST_WIDE_INT diff; }
757 (if (ptr_difference_const (@0, @1, &diff))
758 { build_int_cst_type (type, diff); }))))
760 /* If arg0 is derived from the address of an object or function, we may
761 be able to fold this expression using the object or function's
764 (bit_and (convert? @0) INTEGER_CST@1)
765 (if (POINTER_TYPE_P (TREE_TYPE (@0))
766 && tree_nop_conversion_p (type, TREE_TYPE (@0)))
770 unsigned HOST_WIDE_INT bitpos;
771 get_pointer_alignment_1 (@0, &align, &bitpos);
773 (if (wi::ltu_p (@1, align / BITS_PER_UNIT))
774 { wide_int_to_tree (type, wi::bit_and (@1, bitpos / BITS_PER_UNIT)); }))))
777 /* We can't reassociate at all for saturating types. */
778 (if (!TYPE_SATURATING (type))
780 /* Contract negates. */
781 /* A + (-B) -> A - B */
783 (plus:c (convert1? @0) (convert2? (negate @1)))
784 /* Apply STRIP_NOPS on @0 and the negate. */
785 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
786 && tree_nop_conversion_p (type, TREE_TYPE (@1))
787 && !TYPE_OVERFLOW_SANITIZED (type))
788 (minus (convert @0) (convert @1))))
789 /* A - (-B) -> A + B */
791 (minus (convert1? @0) (convert2? (negate @1)))
792 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
793 && tree_nop_conversion_p (type, TREE_TYPE (@1))
794 && !TYPE_OVERFLOW_SANITIZED (type))
795 (plus (convert @0) (convert @1))))
798 (negate (convert? (negate @1)))
799 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
800 && !TYPE_OVERFLOW_SANITIZED (type))
803 /* We can't reassociate floating-point unless -fassociative-math
804 or fixed-point plus or minus because of saturation to +-Inf. */
805 (if ((!FLOAT_TYPE_P (type) || flag_associative_math)
806 && !FIXED_POINT_TYPE_P (type))
808 /* Match patterns that allow contracting a plus-minus pair
809 irrespective of overflow issues. */
810 /* (A +- B) - A -> +- B */
811 /* (A +- B) -+ B -> A */
812 /* A - (A +- B) -> -+ B */
813 /* A +- (B -+ A) -> +- B */
815 (minus (plus:c @0 @1) @0)
818 (minus (minus @0 @1) @0)
821 (plus:c (minus @0 @1) @1)
824 (minus @0 (plus:c @0 @1))
827 (minus @0 (minus @0 @1))
830 /* (A +- CST) +- CST -> A + CST */
831 (for outer_op (plus minus)
832 (for inner_op (plus minus)
834 (outer_op (inner_op @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
835 /* If the constant operation overflows we cannot do the transform
836 as we would introduce undefined overflow, for example
837 with (a - 1) + INT_MIN. */
838 (with { tree cst = fold_binary (outer_op == inner_op
839 ? PLUS_EXPR : MINUS_EXPR, type, @1, @2); }
840 (if (cst && !TREE_OVERFLOW (cst))
841 (inner_op @0 { cst; } ))))))
843 /* (CST - A) +- CST -> CST - A */
844 (for outer_op (plus minus)
846 (outer_op (minus CONSTANT_CLASS_P@1 @0) CONSTANT_CLASS_P@2)
847 (with { tree cst = fold_binary (outer_op, type, @1, @2); }
848 (if (cst && !TREE_OVERFLOW (cst))
849 (minus { cst; } @0)))))
853 (plus:c (bit_not @0) @0)
854 (if (!TYPE_OVERFLOW_TRAPS (type))
855 { build_all_ones_cst (type); }))
859 (plus (convert? (bit_not @0)) integer_each_onep)
860 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
861 (negate (convert @0))))
865 (minus (convert? (negate @0)) integer_each_onep)
866 (if (!TYPE_OVERFLOW_TRAPS (type)
867 && tree_nop_conversion_p (type, TREE_TYPE (@0)))
868 (bit_not (convert @0))))
872 (minus integer_all_onesp @0)
875 /* (T)(P + A) - (T)P -> (T) A */
876 (for add (plus pointer_plus)
878 (minus (convert (add @0 @1))
880 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
881 /* For integer types, if A has a smaller type
882 than T the result depends on the possible
884 E.g. T=size_t, A=(unsigned)429497295, P>0.
885 However, if an overflow in P + A would cause
886 undefined behavior, we can assume that there
888 || (INTEGRAL_TYPE_P (TREE_TYPE (@0))
889 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
890 /* For pointer types, if the conversion of A to the
891 final type requires a sign- or zero-extension,
892 then we have to punt - it is not defined which
894 || (POINTER_TYPE_P (TREE_TYPE (@0))
895 && TREE_CODE (@1) == INTEGER_CST
896 && tree_int_cst_sign_bit (@1) == 0))
900 /* Simplifications of MIN_EXPR and MAX_EXPR. */
902 (for minmax (min max)
908 (if (INTEGRAL_TYPE_P (type)
909 && TYPE_MIN_VALUE (type)
910 && operand_equal_p (@1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
914 (if (INTEGRAL_TYPE_P (type)
915 && TYPE_MAX_VALUE (type)
916 && operand_equal_p (@1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
920 /* Simplifications of shift and rotates. */
922 (for rotate (lrotate rrotate)
924 (rotate integer_all_onesp@0 @1)
927 /* Optimize -1 >> x for arithmetic right shifts. */
929 (rshift integer_all_onesp@0 @1)
930 (if (!TYPE_UNSIGNED (type)
931 && tree_expr_nonnegative_p (@1))
934 (for shiftrotate (lrotate rrotate lshift rshift)
936 (shiftrotate @0 integer_zerop)
939 (shiftrotate integer_zerop@0 @1)
941 /* Prefer vector1 << scalar to vector1 << vector2
942 if vector2 is uniform. */
943 (for vec (VECTOR_CST CONSTRUCTOR)
945 (shiftrotate @0 vec@1)
946 (with { tree tem = uniform_vector_p (@1); }
948 (shiftrotate @0 { tem; }))))))
950 /* Rewrite an LROTATE_EXPR by a constant into an
951 RROTATE_EXPR by a new constant. */
953 (lrotate @0 INTEGER_CST@1)
954 (rrotate @0 { fold_binary (MINUS_EXPR, TREE_TYPE (@1),
955 build_int_cst (TREE_TYPE (@1),
956 element_precision (type)), @1); }))
958 /* Turn (a OP c1) OP c2 into a OP (c1+c2). */
959 (for op (lrotate rrotate rshift lshift)
961 (op (op @0 INTEGER_CST@1) INTEGER_CST@2)
962 (with { unsigned int prec = element_precision (type); }
963 (if (wi::ge_p (@1, 0, TYPE_SIGN (TREE_TYPE (@1)))
964 && wi::lt_p (@1, prec, TYPE_SIGN (TREE_TYPE (@1)))
965 && wi::ge_p (@2, 0, TYPE_SIGN (TREE_TYPE (@2)))
966 && wi::lt_p (@2, prec, TYPE_SIGN (TREE_TYPE (@2))))
967 (with { unsigned int low = wi::add (@1, @2).to_uhwi (); }
968 /* Deal with a OP (c1 + c2) being undefined but (a OP c1) OP c2
969 being well defined. */
971 (if (op == LROTATE_EXPR || op == RROTATE_EXPR)
972 (op @0 { build_int_cst (TREE_TYPE (@1), low % prec); })
973 (if (TYPE_UNSIGNED (type) || op == LSHIFT_EXPR)
974 { build_zero_cst (type); }
975 (op @0 { build_int_cst (TREE_TYPE (@1), prec - 1); })))
976 (op @0 { build_int_cst (TREE_TYPE (@1), low); })))))))
979 /* ((1 << A) & 1) != 0 -> A == 0
980 ((1 << A) & 1) == 0 -> A != 0 */
984 (cmp (bit_and (lshift integer_onep @0) integer_onep) integer_zerop)
985 (icmp @0 { build_zero_cst (TREE_TYPE (@0)); })))
987 /* (CST1 << A) == CST2 -> A == ctz (CST2) - ctz (CST1)
988 (CST1 << A) != CST2 -> A != ctz (CST2) - ctz (CST1)
992 (cmp (lshift INTEGER_CST@0 @1) INTEGER_CST@2)
993 (with { int cand = wi::ctz (@2) - wi::ctz (@0); }
995 || (!integer_zerop (@2)
996 && wi::ne_p (wi::lshift (@0, cand), @2)))
997 { constant_boolean_node (cmp == NE_EXPR, type); }
998 (if (!integer_zerop (@2)
999 && wi::eq_p (wi::lshift (@0, cand), @2))
1000 (cmp @1 { build_int_cst (TREE_TYPE (@1), cand); }))))))
1002 /* Fold (X << C1) & C2 into (X << C1) & (C2 | ((1 << C1) - 1))
1003 (X >> C1) & C2 into (X >> C1) & (C2 | ~((type) -1 >> C1))
1004 if the new mask might be further optimized. */
1005 (for shift (lshift rshift)
1007 (bit_and (convert?:s@4 (shift:s@5 (convert1?@3 @0) INTEGER_CST@1))
1009 (if (tree_nop_conversion_p (TREE_TYPE (@4), TREE_TYPE (@5))
1010 && TYPE_PRECISION (type) <= HOST_BITS_PER_WIDE_INT
1011 && tree_fits_uhwi_p (@1)
1012 && tree_to_uhwi (@1) > 0
1013 && tree_to_uhwi (@1) < TYPE_PRECISION (type))
1016 unsigned int shiftc = tree_to_uhwi (@1);
1017 unsigned HOST_WIDE_INT mask = TREE_INT_CST_LOW (@2);
1018 unsigned HOST_WIDE_INT newmask, zerobits = 0;
1019 tree shift_type = TREE_TYPE (@3);
1022 if (shift == LSHIFT_EXPR)
1023 zerobits = ((((unsigned HOST_WIDE_INT) 1) << shiftc) - 1);
1024 else if (shift == RSHIFT_EXPR
1025 && (TYPE_PRECISION (shift_type)
1026 == GET_MODE_PRECISION (TYPE_MODE (shift_type))))
1028 prec = TYPE_PRECISION (TREE_TYPE (@3));
1030 /* See if more bits can be proven as zero because of
1033 && TYPE_UNSIGNED (TREE_TYPE (@0)))
1035 tree inner_type = TREE_TYPE (@0);
1036 if ((TYPE_PRECISION (inner_type)
1037 == GET_MODE_PRECISION (TYPE_MODE (inner_type)))
1038 && TYPE_PRECISION (inner_type) < prec)
1040 prec = TYPE_PRECISION (inner_type);
1041 /* See if we can shorten the right shift. */
1043 shift_type = inner_type;
1044 /* Otherwise X >> C1 is all zeros, so we'll optimize
1045 it into (X, 0) later on by making sure zerobits
1049 zerobits = ~(unsigned HOST_WIDE_INT) 0;
1052 zerobits >>= HOST_BITS_PER_WIDE_INT - shiftc;
1053 zerobits <<= prec - shiftc;
1055 /* For arithmetic shift if sign bit could be set, zerobits
1056 can contain actually sign bits, so no transformation is
1057 possible, unless MASK masks them all away. In that
1058 case the shift needs to be converted into logical shift. */
1059 if (!TYPE_UNSIGNED (TREE_TYPE (@3))
1060 && prec == TYPE_PRECISION (TREE_TYPE (@3)))
1062 if ((mask & zerobits) == 0)
1063 shift_type = unsigned_type_for (TREE_TYPE (@3));
1069 /* ((X << 16) & 0xff00) is (X, 0). */
1070 (if ((mask & zerobits) == mask)
1071 { build_int_cst (type, 0); }
1072 (with { newmask = mask | zerobits; }
1073 (if (newmask != mask && (newmask & (newmask + 1)) == 0)
1076 /* Only do the transformation if NEWMASK is some integer
1078 for (prec = BITS_PER_UNIT;
1079 prec < HOST_BITS_PER_WIDE_INT; prec <<= 1)
1080 if (newmask == (((unsigned HOST_WIDE_INT) 1) << prec) - 1)
1083 (if (prec < HOST_BITS_PER_WIDE_INT
1084 || newmask == ~(unsigned HOST_WIDE_INT) 0)
1086 { tree newmaskt = build_int_cst_type (TREE_TYPE (@2), newmask); }
1087 (if (!tree_int_cst_equal (newmaskt, @2))
1088 (if (shift_type != TREE_TYPE (@3))
1089 (bit_and (convert (shift:shift_type (convert @3) @1)) { newmaskt; })
1090 (bit_and @4 { newmaskt; })))))))))))))
1092 /* Fold (X & C2) << C1 into (X << C1) & (C2 << C1)
1093 (X & C2) >> C1 into (X >> C1) & (C2 >> C1). */
1094 (for shift (lshift rshift)
1096 (shift (convert?:s (bit_and:s @0 INTEGER_CST@2)) INTEGER_CST@1)
1097 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1098 (with { tree mask = int_const_binop (shift, fold_convert (type, @2), @1); }
1099 (bit_and (shift (convert @0) @1) { mask; })))))
1102 /* Simplifications of conversions. */
1104 /* Basic strip-useless-type-conversions / strip_nops. */
1105 (for cvt (convert view_convert float fix_trunc)
1108 (if ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@0)))
1109 || (GENERIC && type == TREE_TYPE (@0)))
1112 /* Contract view-conversions. */
1114 (view_convert (view_convert @0))
1117 /* For integral conversions with the same precision or pointer
1118 conversions use a NOP_EXPR instead. */
1121 (if ((INTEGRAL_TYPE_P (type) || POINTER_TYPE_P (type))
1122 && (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
1123 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (@0)))
1126 /* Strip inner integral conversions that do not change precision or size. */
1128 (view_convert (convert@0 @1))
1129 (if ((INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
1130 && (INTEGRAL_TYPE_P (TREE_TYPE (@1)) || POINTER_TYPE_P (TREE_TYPE (@1)))
1131 && (TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
1132 && (TYPE_SIZE (TREE_TYPE (@0)) == TYPE_SIZE (TREE_TYPE (@1))))
1135 /* Re-association barriers around constants and other re-association
1136 barriers can be removed. */
1138 (paren CONSTANT_CLASS_P@0)
1141 (paren (paren@1 @0))
1144 /* Handle cases of two conversions in a row. */
1145 (for ocvt (convert float fix_trunc)
1146 (for icvt (convert float)
1151 tree inside_type = TREE_TYPE (@0);
1152 tree inter_type = TREE_TYPE (@1);
1153 int inside_int = INTEGRAL_TYPE_P (inside_type);
1154 int inside_ptr = POINTER_TYPE_P (inside_type);
1155 int inside_float = FLOAT_TYPE_P (inside_type);
1156 int inside_vec = VECTOR_TYPE_P (inside_type);
1157 unsigned int inside_prec = TYPE_PRECISION (inside_type);
1158 int inside_unsignedp = TYPE_UNSIGNED (inside_type);
1159 int inter_int = INTEGRAL_TYPE_P (inter_type);
1160 int inter_ptr = POINTER_TYPE_P (inter_type);
1161 int inter_float = FLOAT_TYPE_P (inter_type);
1162 int inter_vec = VECTOR_TYPE_P (inter_type);
1163 unsigned int inter_prec = TYPE_PRECISION (inter_type);
1164 int inter_unsignedp = TYPE_UNSIGNED (inter_type);
1165 int final_int = INTEGRAL_TYPE_P (type);
1166 int final_ptr = POINTER_TYPE_P (type);
1167 int final_float = FLOAT_TYPE_P (type);
1168 int final_vec = VECTOR_TYPE_P (type);
1169 unsigned int final_prec = TYPE_PRECISION (type);
1170 int final_unsignedp = TYPE_UNSIGNED (type);
1173 /* In addition to the cases of two conversions in a row
1174 handled below, if we are converting something to its own
1175 type via an object of identical or wider precision, neither
1176 conversion is needed. */
1177 (if (((GIMPLE && useless_type_conversion_p (type, inside_type))
1179 && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (inside_type)))
1180 && (((inter_int || inter_ptr) && final_int)
1181 || (inter_float && final_float))
1182 && inter_prec >= final_prec)
1185 /* Likewise, if the intermediate and initial types are either both
1186 float or both integer, we don't need the middle conversion if the
1187 former is wider than the latter and doesn't change the signedness
1188 (for integers). Avoid this if the final type is a pointer since
1189 then we sometimes need the middle conversion. Likewise if the
1190 final type has a precision not equal to the size of its mode. */
1191 (if (((inter_int && inside_int) || (inter_float && inside_float))
1192 && (final_int || final_float)
1193 && inter_prec >= inside_prec
1194 && (inter_float || inter_unsignedp == inside_unsignedp)
1195 && ! (final_prec != GET_MODE_PRECISION (TYPE_MODE (type))
1196 && TYPE_MODE (type) == TYPE_MODE (inter_type)))
1199 /* If we have a sign-extension of a zero-extended value, we can
1200 replace that by a single zero-extension. Likewise if the
1201 final conversion does not change precision we can drop the
1202 intermediate conversion. */
1203 (if (inside_int && inter_int && final_int
1204 && ((inside_prec < inter_prec && inter_prec < final_prec
1205 && inside_unsignedp && !inter_unsignedp)
1206 || final_prec == inter_prec))
1209 /* Two conversions in a row are not needed unless:
1210 - some conversion is floating-point (overstrict for now), or
1211 - some conversion is a vector (overstrict for now), or
1212 - the intermediate type is narrower than both initial and
1214 - the intermediate type and innermost type differ in signedness,
1215 and the outermost type is wider than the intermediate, or
1216 - the initial type is a pointer type and the precisions of the
1217 intermediate and final types differ, or
1218 - the final type is a pointer type and the precisions of the
1219 initial and intermediate types differ. */
1220 (if (! inside_float && ! inter_float && ! final_float
1221 && ! inside_vec && ! inter_vec && ! final_vec
1222 && (inter_prec >= inside_prec || inter_prec >= final_prec)
1223 && ! (inside_int && inter_int
1224 && inter_unsignedp != inside_unsignedp
1225 && inter_prec < final_prec)
1226 && ((inter_unsignedp && inter_prec > inside_prec)
1227 == (final_unsignedp && final_prec > inter_prec))
1228 && ! (inside_ptr && inter_prec != final_prec)
1229 && ! (final_ptr && inside_prec != inter_prec)
1230 && ! (final_prec != GET_MODE_PRECISION (TYPE_MODE (type))
1231 && TYPE_MODE (type) == TYPE_MODE (inter_type)))
1234 /* A truncation to an unsigned type (a zero-extension) should be
1235 canonicalized as bitwise and of a mask. */
1236 (if (final_int && inter_int && inside_int
1237 && final_prec == inside_prec
1238 && final_prec > inter_prec
1240 (convert (bit_and @0 { wide_int_to_tree
1242 wi::mask (inter_prec, false,
1243 TYPE_PRECISION (inside_type))); })))
1245 /* If we are converting an integer to a floating-point that can
1246 represent it exactly and back to an integer, we can skip the
1247 floating-point conversion. */
1248 (if (GIMPLE /* PR66211 */
1249 && inside_int && inter_float && final_int &&
1250 (unsigned) significand_size (TYPE_MODE (inter_type))
1251 >= inside_prec - !inside_unsignedp)
1254 /* If we have a narrowing conversion to an integral type that is fed by a
1255 BIT_AND_EXPR, we might be able to remove the BIT_AND_EXPR if it merely
1256 masks off bits outside the final type (and nothing else). */
1258 (convert (bit_and @0 INTEGER_CST@1))
1259 (if (INTEGRAL_TYPE_P (type)
1260 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
1261 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))
1262 && operand_equal_p (@1, build_low_bits_mask (TREE_TYPE (@1),
1263 TYPE_PRECISION (type)), 0))
1267 /* (X /[ex] A) * A -> X. */
1269 (mult (convert? (exact_div @0 @1)) @1)
1270 /* Look through a sign-changing conversion. */
1273 /* Canonicalization of binary operations. */
1275 /* Convert X + -C into X - C. */
1277 (plus @0 REAL_CST@1)
1278 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
1279 (with { tree tem = fold_unary (NEGATE_EXPR, type, @1); }
1280 (if (!TREE_OVERFLOW (tem) || !flag_trapping_math)
1281 (minus @0 { tem; })))))
1283 /* Convert x+x into x*2.0. */
1286 (if (SCALAR_FLOAT_TYPE_P (type))
1287 (mult @0 { build_real (type, dconst2); })))
1290 (minus integer_zerop @1)
1293 /* (ARG0 - ARG1) is the same as (-ARG1 + ARG0). So check whether
1294 ARG0 is zero and X + ARG0 reduces to X, since that would mean
1295 (-ARG1 + ARG0) reduces to -ARG1. */
1297 (minus real_zerop@0 @1)
1298 (if (fold_real_zero_addition_p (type, @0, 0))
1301 /* Transform x * -1 into -x. */
1303 (mult @0 integer_minus_onep)
1306 /* COMPLEX_EXPR and REALPART/IMAGPART_EXPR cancellations. */
1308 (complex (realpart @0) (imagpart @0))
1311 (realpart (complex @0 @1))
1314 (imagpart (complex @0 @1))
1317 /* Sometimes we only care about half of a complex expression. */
1319 (realpart (convert?:s (conj:s @0)))
1320 (convert (realpart @0)))
1322 (imagpart (convert?:s (conj:s @0)))
1323 (convert (negate (imagpart @0))))
1324 (for part (realpart imagpart)
1325 (for op (plus minus)
1327 (part (convert?:s@2 (op:s @0 @1)))
1328 (convert (op (part @0) (part @1))))))
1330 (realpart (convert?:s (CEXPI:s @0)))
1333 (imagpart (convert?:s (CEXPI:s @0)))
1336 /* conj(conj(x)) -> x */
1338 (conj (convert? (conj @0)))
1339 (if (tree_nop_conversion_p (TREE_TYPE (@0), type))
1342 /* conj({x,y}) -> {x,-y} */
1344 (conj (convert?:s (complex:s @0 @1)))
1345 (with { tree itype = TREE_TYPE (type); }
1346 (complex (convert:itype @0) (negate (convert:itype @1)))))
1348 /* BSWAP simplifications, transforms checked by gcc.dg/builtin-bswap-8.c. */
1349 (for bswap (BUILT_IN_BSWAP16 BUILT_IN_BSWAP32 BUILT_IN_BSWAP64)
1354 (bswap (bit_not (bswap @0)))
1356 (for bitop (bit_xor bit_ior bit_and)
1358 (bswap (bitop:c (bswap @0) @1))
1359 (bitop @0 (bswap @1)))))
1362 /* Combine COND_EXPRs and VEC_COND_EXPRs. */
1364 /* Simplify constant conditions.
1365 Only optimize constant conditions when the selected branch
1366 has the same type as the COND_EXPR. This avoids optimizing
1367 away "c ? x : throw", where the throw has a void type.
1368 Note that we cannot throw away the fold-const.c variant nor
1369 this one as we depend on doing this transform before possibly
1370 A ? B : B -> B triggers and the fold-const.c one can optimize
1371 0 ? A : B to B even if A has side-effects. Something
1372 genmatch cannot handle. */
1374 (cond INTEGER_CST@0 @1 @2)
1375 (if (integer_zerop (@0))
1376 (if (!VOID_TYPE_P (TREE_TYPE (@2)) || VOID_TYPE_P (type))
1378 (if (!VOID_TYPE_P (TREE_TYPE (@1)) || VOID_TYPE_P (type))
1381 (vec_cond VECTOR_CST@0 @1 @2)
1382 (if (integer_all_onesp (@0))
1384 (if (integer_zerop (@0))
1387 (for cnd (cond vec_cond)
1388 /* A ? B : (A ? X : C) -> A ? B : C. */
1390 (cnd @0 (cnd @0 @1 @2) @3)
1393 (cnd @0 @1 (cnd @0 @2 @3))
1396 /* A ? B : B -> B. */
1401 /* !A ? B : C -> A ? C : B. */
1403 (cnd (logical_inverted_value truth_valued_p@0) @1 @2)
1406 /* A + (B vcmp C ? 1 : 0) -> A - (B vcmp C), since vector comparisons
1407 return all-1 or all-0 results. */
1408 /* ??? We could instead convert all instances of the vec_cond to negate,
1409 but that isn't necessarily a win on its own. */
1411 (plus:c @3 (view_convert? (vec_cond @0 integer_each_onep@1 integer_zerop@2)))
1412 (if (VECTOR_TYPE_P (type)
1413 && TYPE_VECTOR_SUBPARTS (type) == TYPE_VECTOR_SUBPARTS (TREE_TYPE (@0))
1414 && (TYPE_MODE (TREE_TYPE (type))
1415 == TYPE_MODE (TREE_TYPE (TREE_TYPE (@0)))))
1416 (minus @3 (view_convert @0))))
1418 /* ... likewise A - (B vcmp C ? 1 : 0) -> A + (B vcmp C). */
1420 (minus @3 (view_convert? (vec_cond @0 integer_each_onep@1 integer_zerop@2)))
1421 (if (VECTOR_TYPE_P (type)
1422 && TYPE_VECTOR_SUBPARTS (type) == TYPE_VECTOR_SUBPARTS (TREE_TYPE (@0))
1423 && (TYPE_MODE (TREE_TYPE (type))
1424 == TYPE_MODE (TREE_TYPE (TREE_TYPE (@0)))))
1425 (plus @3 (view_convert @0))))
1428 /* Simplifications of comparisons. */
1430 /* See if we can reduce the magnitude of a constant involved in a
1431 comparison by changing the comparison code. This is a canonicalization
1432 formerly done by maybe_canonicalize_comparison_1. */
1436 (cmp @0 INTEGER_CST@1)
1437 (if (tree_int_cst_sgn (@1) == -1)
1438 (acmp @0 { wide_int_to_tree (TREE_TYPE (@1), wi::add (@1, 1)); }))))
1442 (cmp @0 INTEGER_CST@1)
1443 (if (tree_int_cst_sgn (@1) == 1)
1444 (acmp @0 { wide_int_to_tree (TREE_TYPE (@1), wi::sub (@1, 1)); }))))
1447 /* We can simplify a logical negation of a comparison to the
1448 inverted comparison. As we cannot compute an expression
1449 operator using invert_tree_comparison we have to simulate
1450 that with expression code iteration. */
1451 (for cmp (tcc_comparison)
1452 icmp (inverted_tcc_comparison)
1453 ncmp (inverted_tcc_comparison_with_nans)
1454 /* Ideally we'd like to combine the following two patterns
1455 and handle some more cases by using
1456 (logical_inverted_value (cmp @0 @1))
1457 here but for that genmatch would need to "inline" that.
1458 For now implement what forward_propagate_comparison did. */
1460 (bit_not (cmp @0 @1))
1461 (if (VECTOR_TYPE_P (type)
1462 || (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1))
1463 /* Comparison inversion may be impossible for trapping math,
1464 invert_tree_comparison will tell us. But we can't use
1465 a computed operator in the replacement tree thus we have
1466 to play the trick below. */
1467 (with { enum tree_code ic = invert_tree_comparison
1468 (cmp, HONOR_NANS (@0)); }
1474 (bit_xor (cmp @0 @1) integer_truep)
1475 (with { enum tree_code ic = invert_tree_comparison
1476 (cmp, HONOR_NANS (@0)); }
1482 /* Transform comparisons of the form X - Y CMP 0 to X CMP Y.
1483 ??? The transformation is valid for the other operators if overflow
1484 is undefined for the type, but performing it here badly interacts
1485 with the transformation in fold_cond_expr_with_comparison which
1486 attempts to synthetize ABS_EXPR. */
1489 (cmp (minus@2 @0 @1) integer_zerop)
1490 (if (single_use (@2))
1493 /* Transform comparisons of the form X * C1 CMP 0 to X CMP 0 in the
1494 signed arithmetic case. That form is created by the compiler
1495 often enough for folding it to be of value. One example is in
1496 computing loop trip counts after Operator Strength Reduction. */
1497 (for cmp (simple_comparison)
1498 scmp (swapped_simple_comparison)
1500 (cmp (mult @0 INTEGER_CST@1) integer_zerop@2)
1501 /* Handle unfolded multiplication by zero. */
1502 (if (integer_zerop (@1))
1504 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1505 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1506 /* If @1 is negative we swap the sense of the comparison. */
1507 (if (tree_int_cst_sgn (@1) < 0)
1511 /* Simplify comparison of something with itself. For IEEE
1512 floating-point, we can only do some of these simplifications. */
1515 (if (! FLOAT_TYPE_P (TREE_TYPE (@0))
1516 || ! HONOR_NANS (TYPE_MODE (TREE_TYPE (@0))))
1517 { constant_boolean_node (true, type); }))
1526 || ! FLOAT_TYPE_P (TREE_TYPE (@0))
1527 || ! HONOR_NANS (TYPE_MODE (TREE_TYPE (@0))))
1528 { constant_boolean_node (false, type); })))
1529 (for cmp (unle unge uneq)
1532 { constant_boolean_node (true, type); }))
1535 (if (!flag_trapping_math)
1536 { constant_boolean_node (false, type); }))
1538 /* Fold ~X op ~Y as Y op X. */
1539 (for cmp (simple_comparison)
1541 (cmp (bit_not @0) (bit_not @1))
1544 /* Fold ~X op C as X op' ~C, where op' is the swapped comparison. */
1545 (for cmp (simple_comparison)
1546 scmp (swapped_simple_comparison)
1548 (cmp (bit_not @0) CONSTANT_CLASS_P@1)
1549 (if (TREE_CODE (@1) == INTEGER_CST || TREE_CODE (@1) == VECTOR_CST)
1550 (scmp @0 (bit_not @1)))))
1552 (for cmp (simple_comparison)
1553 /* Fold (double)float1 CMP (double)float2 into float1 CMP float2. */
1555 (cmp (convert@2 @0) (convert? @1))
1556 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
1557 && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@2))
1558 == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0)))
1559 && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@2))
1560 == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1))))
1563 tree type1 = TREE_TYPE (@1);
1564 if (TREE_CODE (@1) == REAL_CST && !DECIMAL_FLOAT_TYPE_P (type1))
1566 REAL_VALUE_TYPE orig = TREE_REAL_CST (@1);
1567 if (TYPE_PRECISION (type1) > TYPE_PRECISION (float_type_node)
1568 && exact_real_truncate (TYPE_MODE (float_type_node), &orig))
1569 type1 = float_type_node;
1570 if (TYPE_PRECISION (type1) > TYPE_PRECISION (double_type_node)
1571 && exact_real_truncate (TYPE_MODE (double_type_node), &orig))
1572 type1 = double_type_node;
1575 = (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type1)
1576 ? TREE_TYPE (@0) : type1);
1578 (if (TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (newtype))
1579 (cmp (convert:newtype @0) (convert:newtype @1))))))
1583 /* IEEE doesn't distinguish +0 and -0 in comparisons. */
1585 /* a CMP (-0) -> a CMP 0 */
1586 (if (REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@1)))
1587 (cmp @0 { build_real (TREE_TYPE (@1), dconst0); }))
1588 /* x != NaN is always true, other ops are always false. */
1589 (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
1590 && ! HONOR_SNANS (@1))
1591 { constant_boolean_node (cmp == NE_EXPR, type); })
1592 /* Fold comparisons against infinity. */
1593 (if (REAL_VALUE_ISINF (TREE_REAL_CST (@1))
1594 && MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (@1))))
1597 REAL_VALUE_TYPE max;
1598 enum tree_code code = cmp;
1599 bool neg = REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1));
1601 code = swap_tree_comparison (code);
1604 /* x > +Inf is always false, if with ignore sNANs. */
1605 (if (code == GT_EXPR
1606 && ! HONOR_SNANS (@0))
1607 { constant_boolean_node (false, type); })
1608 (if (code == LE_EXPR)
1609 /* x <= +Inf is always true, if we don't case about NaNs. */
1610 (if (! HONOR_NANS (@0))
1611 { constant_boolean_node (true, type); }
1612 /* x <= +Inf is the same as x == x, i.e. !isnan(x). */
1614 /* x == +Inf and x >= +Inf are always equal to x > DBL_MAX. */
1615 (if (code == EQ_EXPR || code == GE_EXPR)
1616 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
1618 (lt @0 { build_real (TREE_TYPE (@0), max); })
1619 (gt @0 { build_real (TREE_TYPE (@0), max); }))))
1620 /* x < +Inf is always equal to x <= DBL_MAX. */
1621 (if (code == LT_EXPR)
1622 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
1624 (ge @0 { build_real (TREE_TYPE (@0), max); })
1625 (le @0 { build_real (TREE_TYPE (@0), max); }))))
1626 /* x != +Inf is always equal to !(x > DBL_MAX). */
1627 (if (code == NE_EXPR)
1628 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
1629 (if (! HONOR_NANS (@0))
1631 (ge @0 { build_real (TREE_TYPE (@0), max); })
1632 (le @0 { build_real (TREE_TYPE (@0), max); }))
1634 (bit_xor (lt @0 { build_real (TREE_TYPE (@0), max); })
1635 { build_one_cst (type); })
1636 (bit_xor (gt @0 { build_real (TREE_TYPE (@0), max); })
1637 { build_one_cst (type); }))))))))))
1639 /* If this is a comparison of a real constant with a PLUS_EXPR
1640 or a MINUS_EXPR of a real constant, we can convert it into a
1641 comparison with a revised real constant as long as no overflow
1642 occurs when unsafe_math_optimizations are enabled. */
1643 (if (flag_unsafe_math_optimizations)
1644 (for op (plus minus)
1646 (cmp (op @0 REAL_CST@1) REAL_CST@2)
1649 tree tem = const_binop (op == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR,
1650 TREE_TYPE (@1), @2, @1);
1652 (if (tem && !TREE_OVERFLOW (tem))
1653 (cmp @0 { tem; }))))))
1655 /* Likewise, we can simplify a comparison of a real constant with
1656 a MINUS_EXPR whose first operand is also a real constant, i.e.
1657 (c1 - x) < c2 becomes x > c1-c2. Reordering is allowed on
1658 floating-point types only if -fassociative-math is set. */
1659 (if (flag_associative_math)
1661 (cmp (minus REAL_CST@0 @1) REAL_CST@2)
1662 (with { tree tem = const_binop (MINUS_EXPR, TREE_TYPE (@1), @0, @2); }
1663 (if (tem && !TREE_OVERFLOW (tem))
1664 (cmp { tem; } @1)))))
1666 /* Fold comparisons against built-in math functions. */
1667 (if (flag_unsafe_math_optimizations
1668 && ! flag_errno_math)
1671 (cmp (sq @0) REAL_CST@1)
1673 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
1675 /* sqrt(x) < y is always false, if y is negative. */
1676 (if (cmp == EQ_EXPR || cmp == LT_EXPR || cmp == LE_EXPR)
1677 { constant_boolean_node (false, type); })
1678 /* sqrt(x) > y is always true, if y is negative and we
1679 don't care about NaNs, i.e. negative values of x. */
1680 (if (cmp == NE_EXPR || !HONOR_NANS (@0))
1681 { constant_boolean_node (true, type); })
1682 /* sqrt(x) > y is the same as x >= 0, if y is negative. */
1683 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })))
1684 (if (cmp == GT_EXPR || cmp == GE_EXPR)
1688 REAL_ARITHMETIC (c2, MULT_EXPR,
1689 TREE_REAL_CST (@1), TREE_REAL_CST (@1));
1690 real_convert (&c2, TYPE_MODE (TREE_TYPE (@0)), &c2);
1692 (if (REAL_VALUE_ISINF (c2))
1693 /* sqrt(x) > y is x == +Inf, when y is very large. */
1694 (if (HONOR_INFINITIES (@0))
1695 (eq @0 { build_real (TREE_TYPE (@0), c2); })
1696 { constant_boolean_node (false, type); })
1697 /* sqrt(x) > c is the same as x > c*c. */
1698 (cmp @0 { build_real (TREE_TYPE (@0), c2); }))))
1699 (if (cmp == LT_EXPR || cmp == LE_EXPR)
1703 REAL_ARITHMETIC (c2, MULT_EXPR,
1704 TREE_REAL_CST (@1), TREE_REAL_CST (@1));
1705 real_convert (&c2, TYPE_MODE (TREE_TYPE (@0)), &c2);
1707 (if (REAL_VALUE_ISINF (c2))
1709 /* sqrt(x) < y is always true, when y is a very large
1710 value and we don't care about NaNs or Infinities. */
1711 (if (! HONOR_NANS (@0) && ! HONOR_INFINITIES (@0))
1712 { constant_boolean_node (true, type); })
1713 /* sqrt(x) < y is x != +Inf when y is very large and we
1714 don't care about NaNs. */
1715 (if (! HONOR_NANS (@0))
1716 (ne @0 { build_real (TREE_TYPE (@0), c2); }))
1717 /* sqrt(x) < y is x >= 0 when y is very large and we
1718 don't care about Infinities. */
1719 (if (! HONOR_INFINITIES (@0))
1720 (ge @0 { build_real (TREE_TYPE (@0), dconst0); }))
1721 /* sqrt(x) < y is x >= 0 && x != +Inf, when y is large. */
1724 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
1725 (ne @0 { build_real (TREE_TYPE (@0), c2); }))))
1726 /* sqrt(x) < c is the same as x < c*c, if we ignore NaNs. */
1727 (if (! HONOR_NANS (@0))
1728 (cmp @0 { build_real (TREE_TYPE (@0), c2); })
1729 /* sqrt(x) < c is the same as x >= 0 && x < c*c. */
1732 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
1733 (cmp @0 { build_real (TREE_TYPE (@0), c2); }))))))))))))
1735 /* Unordered tests if either argument is a NaN. */
1737 (bit_ior (unordered @0 @0) (unordered @1 @1))
1738 (if (types_match (@0, @1))
1741 (bit_and (ordered @0 @0) (ordered @1 @1))
1742 (if (types_match (@0, @1))
1745 (bit_ior:c (unordered @0 @0) (unordered:c@2 @0 @1))
1748 (bit_and:c (ordered @0 @0) (ordered:c@2 @0 @1))
1751 /* -A CMP -B -> B CMP A. */
1752 (for cmp (tcc_comparison)
1753 scmp (swapped_tcc_comparison)
1755 (cmp (negate @0) (negate @1))
1756 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
1757 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1758 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
1761 (cmp (negate @0) CONSTANT_CLASS_P@1)
1762 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
1763 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1764 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
1765 (with { tree tem = fold_unary (NEGATE_EXPR, TREE_TYPE (@0), @1); }
1766 (if (tem && !TREE_OVERFLOW (tem))
1767 (scmp @0 { tem; }))))))
1769 /* Convert ABS_EXPR<x> == 0 or ABS_EXPR<x> != 0 to x == 0 or x != 0. */
1772 (op (abs @0) zerop@1)
1775 /* From fold_sign_changed_comparison and fold_widened_comparison. */
1776 (for cmp (simple_comparison)
1778 (cmp (convert@0 @00) (convert?@1 @10))
1779 (if (TREE_CODE (TREE_TYPE (@0)) == INTEGER_TYPE
1780 /* Disable this optimization if we're casting a function pointer
1781 type on targets that require function pointer canonicalization. */
1782 && !(targetm.have_canonicalize_funcptr_for_compare ()
1783 && TREE_CODE (TREE_TYPE (@00)) == POINTER_TYPE
1784 && TREE_CODE (TREE_TYPE (TREE_TYPE (@00))) == FUNCTION_TYPE)
1786 (if (TYPE_PRECISION (TREE_TYPE (@00)) == TYPE_PRECISION (TREE_TYPE (@0))
1787 && (TREE_CODE (@10) == INTEGER_CST
1788 || (@1 != @10 && types_match (TREE_TYPE (@10), TREE_TYPE (@00))))
1789 && (TYPE_UNSIGNED (TREE_TYPE (@00)) == TYPE_UNSIGNED (TREE_TYPE (@0))
1792 && (POINTER_TYPE_P (TREE_TYPE (@00)) == POINTER_TYPE_P (TREE_TYPE (@0))))
1793 /* ??? The special-casing of INTEGER_CST conversion was in the original
1794 code and here to avoid a spurious overflow flag on the resulting
1795 constant which fold_convert produces. */
1796 (if (TREE_CODE (@1) == INTEGER_CST)
1797 (cmp @00 { force_fit_type (TREE_TYPE (@00), wi::to_widest (@1), 0,
1798 TREE_OVERFLOW (@1)); })
1799 (cmp @00 (convert @1)))
1801 (if (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (TREE_TYPE (@00)))
1802 /* If possible, express the comparison in the shorter mode. */
1803 (if ((cmp == EQ_EXPR || cmp == NE_EXPR
1804 || TYPE_UNSIGNED (TREE_TYPE (@0)) == TYPE_UNSIGNED (TREE_TYPE (@00)))
1805 && (types_match (TREE_TYPE (@10), TREE_TYPE (@00))
1806 || ((TYPE_PRECISION (TREE_TYPE (@00))
1807 >= TYPE_PRECISION (TREE_TYPE (@10)))
1808 && (TYPE_UNSIGNED (TREE_TYPE (@00))
1809 == TYPE_UNSIGNED (TREE_TYPE (@10))))
1810 || (TREE_CODE (@10) == INTEGER_CST
1811 && (TREE_CODE (TREE_TYPE (@00)) == INTEGER_TYPE
1812 || TREE_CODE (TREE_TYPE (@00)) == BOOLEAN_TYPE)
1813 && int_fits_type_p (@10, TREE_TYPE (@00)))))
1814 (cmp @00 (convert @10))
1815 (if (TREE_CODE (@10) == INTEGER_CST
1816 && TREE_CODE (TREE_TYPE (@00)) == INTEGER_TYPE
1817 && !int_fits_type_p (@10, TREE_TYPE (@00)))
1820 tree min = lower_bound_in_type (TREE_TYPE (@10), TREE_TYPE (@00));
1821 tree max = upper_bound_in_type (TREE_TYPE (@10), TREE_TYPE (@00));
1822 bool above = integer_nonzerop (const_binop (LT_EXPR, type, max, @10));
1823 bool below = integer_nonzerop (const_binop (LT_EXPR, type, @10, min));
1825 (if (above || below)
1826 (if (cmp == EQ_EXPR || cmp == NE_EXPR)
1827 { constant_boolean_node (cmp == EQ_EXPR ? false : true, type); }
1828 (if (cmp == LT_EXPR || cmp == LE_EXPR)
1829 { constant_boolean_node (above ? true : false, type); }
1830 (if (cmp == GT_EXPR || cmp == GE_EXPR)
1831 { constant_boolean_node (above ? false : true, type); }))))))))))))
1834 /* A local variable can never be pointed to by
1835 the default SSA name of an incoming parameter.
1836 SSA names are canonicalized to 2nd place. */
1838 (cmp addr@0 SSA_NAME@1)
1839 (if (SSA_NAME_IS_DEFAULT_DEF (@1)
1840 && TREE_CODE (SSA_NAME_VAR (@1)) == PARM_DECL)
1841 (with { tree base = get_base_address (TREE_OPERAND (@0, 0)); }
1842 (if (TREE_CODE (base) == VAR_DECL
1843 && auto_var_in_fn_p (base, current_function_decl))
1844 (if (cmp == NE_EXPR)
1845 { constant_boolean_node (true, type); }
1846 { constant_boolean_node (false, type); }))))))
1848 /* Equality compare simplifications from fold_binary */
1851 /* If we have (A | C) == D where C & ~D != 0, convert this into 0.
1852 Similarly for NE_EXPR. */
1854 (cmp (convert?@3 (bit_ior @0 INTEGER_CST@1)) INTEGER_CST@2)
1855 (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0))
1856 && wi::bit_and_not (@1, @2) != 0)
1857 { constant_boolean_node (cmp == NE_EXPR, type); }))
1859 /* (X ^ Y) == 0 becomes X == Y, and (X ^ Y) != 0 becomes X != Y. */
1861 (cmp (bit_xor @0 @1) integer_zerop)
1864 /* (X ^ Y) == Y becomes X == 0.
1865 Likewise (X ^ Y) == X becomes Y == 0. */
1867 (cmp:c (bit_xor:c @0 @1) @0)
1868 (cmp @1 { build_zero_cst (TREE_TYPE (@1)); }))
1870 /* (X ^ C1) op C2 can be rewritten as X op (C1 ^ C2). */
1872 (cmp (convert?@3 (bit_xor @0 INTEGER_CST@1)) INTEGER_CST@2)
1873 (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0)))
1874 (cmp @0 (bit_xor @1 (convert @2)))))
1877 (cmp (convert? addr@0) integer_zerop)
1878 (if (tree_single_nonzero_warnv_p (@0, NULL))
1879 { constant_boolean_node (cmp == NE_EXPR, type); })))
1881 /* If we have (A & C) == C where C is a power of 2, convert this into
1882 (A & C) != 0. Similarly for NE_EXPR. */
1886 (cmp (bit_and@2 @0 integer_pow2p@1) @1)
1887 (icmp @2 { build_zero_cst (TREE_TYPE (@0)); })))
1889 /* If we have (A & C) != 0 where C is the sign bit of A, convert
1890 this into A < 0. Similarly for (A & C) == 0 into A >= 0. */
1894 (cmp (bit_and (convert?@2 @0) integer_pow2p@1) integer_zerop)
1895 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1896 && (TYPE_PRECISION (TREE_TYPE (@0))
1897 == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (@0))))
1898 && element_precision (@2) >= element_precision (@0)
1899 && wi::only_sign_bit_p (@1, element_precision (@0)))
1900 (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
1901 (ncmp (convert:stype @0) { build_zero_cst (stype); })))))
1903 /* When the addresses are not directly of decls compare base and offset.
1904 This implements some remaining parts of fold_comparison address
1905 comparisons but still no complete part of it. Still it is good
1906 enough to make fold_stmt not regress when not dispatching to fold_binary. */
1907 (for cmp (simple_comparison)
1909 (cmp (convert1?@2 addr@0) (convert2? addr@1))
1912 HOST_WIDE_INT off0, off1;
1913 tree base0 = get_addr_base_and_unit_offset (TREE_OPERAND (@0, 0), &off0);
1914 tree base1 = get_addr_base_and_unit_offset (TREE_OPERAND (@1, 0), &off1);
1915 if (base0 && TREE_CODE (base0) == MEM_REF)
1917 off0 += mem_ref_offset (base0).to_short_addr ();
1918 base0 = TREE_OPERAND (base0, 0);
1920 if (base1 && TREE_CODE (base1) == MEM_REF)
1922 off1 += mem_ref_offset (base1).to_short_addr ();
1923 base1 = TREE_OPERAND (base1, 0);
1926 (if (base0 && base1)
1930 if (decl_in_symtab_p (base0)
1931 && decl_in_symtab_p (base1))
1932 equal = symtab_node::get_create (base0)
1933 ->equal_address_to (symtab_node::get_create (base1));
1934 else if ((DECL_P (base0) || TREE_CODE (base0) == SSA_NAME)
1935 && (DECL_P (base1) || TREE_CODE (base1) == SSA_NAME))
1936 equal = (base0 == base1);
1939 && (cmp == EQ_EXPR || cmp == NE_EXPR
1940 /* If the offsets are equal we can ignore overflow. */
1942 || POINTER_TYPE_OVERFLOW_UNDEFINED
1943 /* Or if we compare using pointers to decls. */
1944 || (POINTER_TYPE_P (TREE_TYPE (@2))
1945 && DECL_P (base0))))
1947 (if (cmp == EQ_EXPR)
1948 { constant_boolean_node (off0 == off1, type); })
1949 (if (cmp == NE_EXPR)
1950 { constant_boolean_node (off0 != off1, type); })
1951 (if (cmp == LT_EXPR)
1952 { constant_boolean_node (off0 < off1, type); })
1953 (if (cmp == LE_EXPR)
1954 { constant_boolean_node (off0 <= off1, type); })
1955 (if (cmp == GE_EXPR)
1956 { constant_boolean_node (off0 >= off1, type); })
1957 (if (cmp == GT_EXPR)
1958 { constant_boolean_node (off0 > off1, type); }))
1960 && DECL_P (base0) && DECL_P (base1)
1961 /* If we compare this as integers require equal offset. */
1962 && (!INTEGRAL_TYPE_P (TREE_TYPE (@2))
1965 (if (cmp == EQ_EXPR)
1966 { constant_boolean_node (false, type); })
1967 (if (cmp == NE_EXPR)
1968 { constant_boolean_node (true, type); })))))))))
1970 /* Non-equality compare simplifications from fold_binary */
1971 (for cmp (lt gt le ge)
1972 /* Comparisons with the highest or lowest possible integer of
1973 the specified precision will have known values. */
1975 (cmp (convert?@2 @0) INTEGER_CST@1)
1976 (if ((INTEGRAL_TYPE_P (TREE_TYPE (@1)) || POINTER_TYPE_P (TREE_TYPE (@1)))
1977 && tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@0)))
1980 tree arg1_type = TREE_TYPE (@1);
1981 unsigned int prec = TYPE_PRECISION (arg1_type);
1982 wide_int max = wi::max_value (arg1_type);
1983 wide_int signed_max = wi::max_value (prec, SIGNED);
1984 wide_int min = wi::min_value (arg1_type);
1987 (if (wi::eq_p (@1, max))
1989 (if (cmp == GT_EXPR)
1990 { constant_boolean_node (false, type); })
1991 (if (cmp == GE_EXPR)
1993 (if (cmp == LE_EXPR)
1994 { constant_boolean_node (true, type); })
1995 (if (cmp == LT_EXPR)
1997 (if (wi::eq_p (@1, min))
1999 (if (cmp == LT_EXPR)
2000 { constant_boolean_node (false, type); })
2001 (if (cmp == LE_EXPR)
2003 (if (cmp == GE_EXPR)
2004 { constant_boolean_node (true, type); })
2005 (if (cmp == GT_EXPR)
2007 (if (wi::eq_p (@1, max - 1))
2009 (if (cmp == GT_EXPR)
2010 (eq @2 { wide_int_to_tree (TREE_TYPE (@1), wi::add (@1, 1)); }))
2011 (if (cmp == LE_EXPR)
2012 (ne @2 { wide_int_to_tree (TREE_TYPE (@1), wi::add (@1, 1)); }))))
2013 (if (wi::eq_p (@1, min + 1))
2015 (if (cmp == GE_EXPR)
2016 (ne @2 { wide_int_to_tree (TREE_TYPE (@1), wi::sub (@1, 1)); }))
2017 (if (cmp == LT_EXPR)
2018 (eq @2 { wide_int_to_tree (TREE_TYPE (@1), wi::sub (@1, 1)); }))))
2019 (if (wi::eq_p (@1, signed_max)
2020 && TYPE_UNSIGNED (arg1_type)
2021 /* We will flip the signedness of the comparison operator
2022 associated with the mode of @1, so the sign bit is
2023 specified by this mode. Check that @1 is the signed
2024 max associated with this sign bit. */
2025 && prec == GET_MODE_PRECISION (TYPE_MODE (arg1_type))
2026 /* signed_type does not work on pointer types. */
2027 && INTEGRAL_TYPE_P (arg1_type))
2028 /* The following case also applies to X < signed_max+1
2029 and X >= signed_max+1 because previous transformations. */
2030 (if (cmp == LE_EXPR || cmp == GT_EXPR)
2031 (with { tree st = signed_type_for (arg1_type); }
2032 (if (cmp == LE_EXPR)
2033 (ge (convert:st @0) { build_zero_cst (st); })
2034 (lt (convert:st @0) { build_zero_cst (st); }))))))))))
2036 (for cmp (unordered ordered unlt unle ungt unge uneq ltgt)
2037 /* If the second operand is NaN, the result is constant. */
2040 (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
2041 && (cmp != LTGT_EXPR || ! flag_trapping_math))
2042 { constant_boolean_node (cmp == ORDERED_EXPR || cmp == LTGT_EXPR
2043 ? false : true, type); })))
2045 /* bool_var != 0 becomes bool_var. */
2047 (ne @0 integer_zerop)
2048 (if (TREE_CODE (TREE_TYPE (@0)) == BOOLEAN_TYPE
2049 && types_match (type, TREE_TYPE (@0)))
2051 /* bool_var == 1 becomes bool_var. */
2053 (eq @0 integer_onep)
2054 (if (TREE_CODE (TREE_TYPE (@0)) == BOOLEAN_TYPE
2055 && types_match (type, TREE_TYPE (@0)))
2058 bool_var == 0 becomes !bool_var or
2059 bool_var != 1 becomes !bool_var
2060 here because that only is good in assignment context as long
2061 as we require a tcc_comparison in GIMPLE_CONDs where we'd
2062 replace if (x == 0) with tem = ~x; if (tem != 0) which is
2063 clearly less optimal and which we'll transform again in forwprop. */
2066 /* Simplification of math builtins. */
2068 /* fold_builtin_logarithm */
2069 (if (flag_unsafe_math_optimizations)
2071 /* Simplify sqrt(x) * sqrt(x) -> x. */
2073 (mult (SQRT@1 @0) @1)
2074 (if (!HONOR_SNANS (type))
2077 /* Simplify sqrt(x) * sqrt(y) -> sqrt(x*y). */
2078 (for root (SQRT CBRT)
2080 (mult (root:s @0) (root:s @1))
2081 (root (mult @0 @1))))
2083 /* Simplify pow(x,y) * pow(x,z) -> pow(x,y+z). */
2085 (mult (POW:s @0 @1) (POW:s @0 @2))
2086 (POW @0 (plus @1 @2)))
2088 /* Simplify pow(x,y) * pow(z,y) -> pow(x*z,y). */
2090 (mult (POW:s @0 @1) (POW:s @2 @1))
2091 (POW (mult @0 @2) @1))
2093 /* Simplify expN(x) * expN(y) -> expN(x+y). */
2094 (for exps (EXP EXP2 EXP10 POW10)
2096 (mult (exps:s @0) (exps:s @1))
2097 (exps (plus @0 @1))))
2099 /* Simplify tan(x) * cos(x) -> sin(x). */
2101 (mult:c (TAN:s @0) (COS:s @0))
2104 /* Simplify x * pow(x,c) -> pow(x,c+1). */
2106 (mult @0 (POW:s @0 REAL_CST@1))
2107 (if (!TREE_OVERFLOW (@1))
2108 (POW @0 (plus @1 { build_one_cst (type); }))))
2110 /* Simplify sin(x) / cos(x) -> tan(x). */
2112 (rdiv (SIN:s @0) (COS:s @0))
2115 /* Simplify cos(x) / sin(x) -> 1 / tan(x). */
2117 (rdiv (COS:s @0) (SIN:s @0))
2118 (rdiv { build_one_cst (type); } (TAN @0)))
2120 /* Simplify sin(x) / tan(x) -> cos(x). */
2122 (rdiv (SIN:s @0) (TAN:s @0))
2123 (if (! HONOR_NANS (@0)
2124 && ! HONOR_INFINITIES (@0))
2127 /* Simplify tan(x) / sin(x) -> 1.0 / cos(x). */
2129 (rdiv (TAN:s @0) (SIN:s @0))
2130 (if (! HONOR_NANS (@0)
2131 && ! HONOR_INFINITIES (@0))
2132 (rdiv { build_one_cst (type); } (COS @0))))
2134 /* Simplify pow(x,c) / x -> pow(x,c-1). */
2136 (rdiv (POW:s @0 REAL_CST@1) @0)
2137 (if (!TREE_OVERFLOW (@1))
2138 (POW @0 (minus @1 { build_one_cst (type); }))))
2140 /* Simplify a/root(b/c) into a*root(c/b). */
2141 (for root (SQRT CBRT)
2143 (rdiv @0 (root:s (rdiv:s @1 @2)))
2144 (mult @0 (root (rdiv @2 @1)))))
2146 /* Simplify x/expN(y) into x*expN(-y). */
2147 (for exps (EXP EXP2 EXP10 POW10)
2149 (rdiv @0 (exps:s @1))
2150 (mult @0 (exps (negate @1)))))
2152 /* Simplify x / pow (y,z) -> x * pow(y,-z). */
2154 (rdiv @0 (POW:s @1 @2))
2155 (mult @0 (POW @1 (negate @2))))
2157 /* Special case, optimize logN(expN(x)) = x. */
2158 (for logs (LOG LOG2 LOG10)
2159 exps (EXP EXP2 EXP10)
2163 /* Optimize logN(func()) for various exponential functions. We
2164 want to determine the value "x" and the power "exponent" in
2165 order to transform logN(x**exponent) into exponent*logN(x). */
2166 (for logs (LOG LOG LOG LOG
2168 LOG10 LOG10 LOG10 LOG10)
2169 exps (EXP EXP2 EXP10 POW10)
2176 CASE_FLT_FN (BUILT_IN_EXP):
2177 /* Prepare to do logN(exp(exponent) -> exponent*logN(e). */
2178 x = build_real (type, real_value_truncate (TYPE_MODE (type),
2181 CASE_FLT_FN (BUILT_IN_EXP2):
2182 /* Prepare to do logN(exp2(exponent) -> exponent*logN(2). */
2183 x = build_real (type, dconst2);
2185 CASE_FLT_FN (BUILT_IN_EXP10):
2186 CASE_FLT_FN (BUILT_IN_POW10):
2187 /* Prepare to do logN(exp10(exponent) -> exponent*logN(10). */
2189 REAL_VALUE_TYPE dconst10;
2190 real_from_integer (&dconst10, VOIDmode, 10, SIGNED);
2191 x = build_real (type, dconst10);
2198 (mult (logs { x; }) @0))))
2209 CASE_FLT_FN (BUILT_IN_SQRT):
2210 /* Prepare to do logN(sqrt(x) -> 0.5*logN(x). */
2211 x = build_real (type, dconsthalf);
2213 CASE_FLT_FN (BUILT_IN_CBRT):
2214 /* Prepare to do logN(cbrt(x) -> (1/3)*logN(x). */
2215 x = build_real (type, real_value_truncate (TYPE_MODE (type),
2222 (mult { x; } (logs @0)))))
2223 /* logN(pow(x,exponent) -> exponent*logN(x). */
2224 (for logs (LOG LOG2 LOG10)
2228 (mult @1 (logs @0)))))
2230 /* Narrowing of arithmetic and logical operations.
2232 These are conceptually similar to the transformations performed for
2233 the C/C++ front-ends by shorten_binary_op and shorten_compare. Long
2234 term we want to move all that code out of the front-ends into here. */
2236 /* If we have a narrowing conversion of an arithmetic operation where
2237 both operands are widening conversions from the same type as the outer
2238 narrowing conversion. Then convert the innermost operands to a suitable
2239 unsigned type (to avoid introducing undefined behaviour), perform the
2240 operation and convert the result to the desired type. */
2241 (for op (plus minus)
2243 (convert (op:s (convert@2 @0) (convert@3 @1)))
2244 (if (INTEGRAL_TYPE_P (type)
2245 /* We check for type compatibility between @0 and @1 below,
2246 so there's no need to check that @1/@3 are integral types. */
2247 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
2248 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
2249 /* The precision of the type of each operand must match the
2250 precision of the mode of each operand, similarly for the
2252 && (TYPE_PRECISION (TREE_TYPE (@0))
2253 == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (@0))))
2254 && (TYPE_PRECISION (TREE_TYPE (@1))
2255 == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (@1))))
2256 && TYPE_PRECISION (type) == GET_MODE_PRECISION (TYPE_MODE (type))
2257 /* The inner conversion must be a widening conversion. */
2258 && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
2259 && types_match (@0, @1)
2260 && types_match (@0, type))
2261 (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
2262 (convert (op @0 @1))
2263 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
2264 (convert (op (convert:utype @0) (convert:utype @1))))))))
2266 /* This is another case of narrowing, specifically when there's an outer
2267 BIT_AND_EXPR which masks off bits outside the type of the innermost
2268 operands. Like the previous case we have to convert the operands
2269 to unsigned types to avoid introducing undefined behaviour for the
2270 arithmetic operation. */
2271 (for op (minus plus)
2273 (bit_and (op:s (convert@2 @0) (convert@3 @1)) INTEGER_CST@4)
2274 (if (INTEGRAL_TYPE_P (type)
2275 /* We check for type compatibility between @0 and @1 below,
2276 so there's no need to check that @1/@3 are integral types. */
2277 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
2278 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
2279 /* The precision of the type of each operand must match the
2280 precision of the mode of each operand, similarly for the
2282 && (TYPE_PRECISION (TREE_TYPE (@0))
2283 == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (@0))))
2284 && (TYPE_PRECISION (TREE_TYPE (@1))
2285 == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (@1))))
2286 && TYPE_PRECISION (type) == GET_MODE_PRECISION (TYPE_MODE (type))
2287 /* The inner conversion must be a widening conversion. */
2288 && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
2289 && types_match (@0, @1)
2290 && (tree_int_cst_min_precision (@4, TYPE_SIGN (TREE_TYPE (@0)))
2291 <= TYPE_PRECISION (TREE_TYPE (@0)))
2292 && (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))
2293 || tree_int_cst_sgn (@4) >= 0))
2294 (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
2295 (with { tree ntype = TREE_TYPE (@0); }
2296 (convert (bit_and (op @0 @1) (convert:ntype @4))))
2297 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
2298 (convert (bit_and (op (convert:utype @0) (convert:utype @1))
2299 (convert:utype @4))))))))