1 /* Match-and-simplify patterns for shared GENERIC and GIMPLE folding.
2 This file is consumed by genmatch which produces gimple-match.c
3 and generic-match.c from it.
5 Copyright (C) 2014-2017 Free Software Foundation, Inc.
6 Contributed by Richard Biener <rguenther@suse.de>
7 and Prathamesh Kulkarni <bilbotheelffriend@gmail.com>
9 This file is part of GCC.
11 GCC is free software; you can redistribute it and/or modify it under
12 the terms of the GNU General Public License as published by the Free
13 Software Foundation; either version 3, or (at your option) any later
16 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
17 WARRANTY; without even the implied warranty of MERCHANTABILITY or
18 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21 You should have received a copy of the GNU General Public License
22 along with GCC; see the file COPYING3. If not see
23 <http://www.gnu.org/licenses/>. */
26 /* Generic tree predicates we inherit. */
28 integer_onep integer_zerop integer_all_onesp integer_minus_onep
29 integer_each_onep integer_truep integer_nonzerop
30 real_zerop real_onep real_minus_onep
33 tree_expr_nonnegative_p
40 (define_operator_list tcc_comparison
41 lt le eq ne ge gt unordered ordered unlt unle ungt unge uneq ltgt)
42 (define_operator_list inverted_tcc_comparison
43 ge gt ne eq lt le ordered unordered ge gt le lt ltgt uneq)
44 (define_operator_list inverted_tcc_comparison_with_nans
45 unge ungt ne eq unlt unle ordered unordered ge gt le lt ltgt uneq)
46 (define_operator_list swapped_tcc_comparison
47 gt ge eq ne le lt unordered ordered ungt unge unlt unle uneq ltgt)
48 (define_operator_list simple_comparison lt le eq ne ge gt)
49 (define_operator_list swapped_simple_comparison gt ge eq ne le lt)
51 #include "cfn-operators.pd"
53 /* Define operand lists for math rounding functions {,i,l,ll}FN,
54 where the versions prefixed with "i" return an int, those prefixed with
55 "l" return a long and those prefixed with "ll" return a long long.
57 Also define operand lists:
59 X<FN>F for all float functions, in the order i, l, ll
60 X<FN> for all double functions, in the same order
61 X<FN>L for all long double functions, in the same order. */
62 #define DEFINE_INT_AND_FLOAT_ROUND_FN(FN) \
63 (define_operator_list X##FN##F BUILT_IN_I##FN##F \
66 (define_operator_list X##FN BUILT_IN_I##FN \
69 (define_operator_list X##FN##L BUILT_IN_I##FN##L \
73 DEFINE_INT_AND_FLOAT_ROUND_FN (FLOOR)
74 DEFINE_INT_AND_FLOAT_ROUND_FN (CEIL)
75 DEFINE_INT_AND_FLOAT_ROUND_FN (ROUND)
76 DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
78 /* As opposed to convert?, this still creates a single pattern, so
79 it is not a suitable replacement for convert? in all cases. */
80 (match (nop_convert @0)
82 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))))
83 (match (nop_convert @0)
85 (if (VECTOR_TYPE_P (type) && VECTOR_TYPE_P (TREE_TYPE (@0))
86 && TYPE_VECTOR_SUBPARTS (type) == TYPE_VECTOR_SUBPARTS (TREE_TYPE (@0))
87 && tree_nop_conversion_p (TREE_TYPE (type), TREE_TYPE (TREE_TYPE (@0))))))
88 /* This one has to be last, or it shadows the others. */
89 (match (nop_convert @0)
92 /* Simplifications of operations with one constant operand and
93 simplifications to constants or single values. */
95 (for op (plus pointer_plus minus bit_ior bit_xor)
100 /* 0 +p index -> (type)index */
102 (pointer_plus integer_zerop @1)
103 (non_lvalue (convert @1)))
105 /* See if ARG1 is zero and X + ARG1 reduces to X.
106 Likewise if the operands are reversed. */
108 (plus:c @0 real_zerop@1)
109 (if (fold_real_zero_addition_p (type, @1, 0))
112 /* See if ARG1 is zero and X - ARG1 reduces to X. */
114 (minus @0 real_zerop@1)
115 (if (fold_real_zero_addition_p (type, @1, 1))
119 This is unsafe for certain floats even in non-IEEE formats.
120 In IEEE, it is unsafe because it does wrong for NaNs.
121 Also note that operand_equal_p is always false if an operand
125 (if (!FLOAT_TYPE_P (type) || !HONOR_NANS (type))
126 { build_zero_cst (type); }))
129 (mult @0 integer_zerop@1)
132 /* Maybe fold x * 0 to 0. The expressions aren't the same
133 when x is NaN, since x * 0 is also NaN. Nor are they the
134 same in modes with signed zeros, since multiplying a
135 negative value by 0 gives -0, not +0. */
137 (mult @0 real_zerop@1)
138 (if (!HONOR_NANS (type) && !HONOR_SIGNED_ZEROS (type))
141 /* In IEEE floating point, x*1 is not equivalent to x for snans.
142 Likewise for complex arithmetic with signed zeros. */
145 (if (!HONOR_SNANS (type)
146 && (!HONOR_SIGNED_ZEROS (type)
147 || !COMPLEX_FLOAT_TYPE_P (type)))
150 /* Transform x * -1.0 into -x. */
152 (mult @0 real_minus_onep)
153 (if (!HONOR_SNANS (type)
154 && (!HONOR_SIGNED_ZEROS (type)
155 || !COMPLEX_FLOAT_TYPE_P (type)))
158 (for cmp (gt ge lt le)
159 outp (convert convert negate negate)
160 outn (negate negate convert convert)
161 /* Transform (X > 0.0 ? 1.0 : -1.0) into copysign(1, X). */
162 /* Transform (X >= 0.0 ? 1.0 : -1.0) into copysign(1, X). */
163 /* Transform (X < 0.0 ? 1.0 : -1.0) into copysign(1,-X). */
164 /* Transform (X <= 0.0 ? 1.0 : -1.0) into copysign(1,-X). */
166 (cond (cmp @0 real_zerop) real_onep@1 real_minus_onep)
167 (if (!HONOR_NANS (type) && !HONOR_SIGNED_ZEROS (type)
168 && types_match (type, TREE_TYPE (@0)))
170 (if (types_match (type, float_type_node))
171 (BUILT_IN_COPYSIGNF @1 (outp @0)))
172 (if (types_match (type, double_type_node))
173 (BUILT_IN_COPYSIGN @1 (outp @0)))
174 (if (types_match (type, long_double_type_node))
175 (BUILT_IN_COPYSIGNL @1 (outp @0))))))
176 /* Transform (X > 0.0 ? -1.0 : 1.0) into copysign(1,-X). */
177 /* Transform (X >= 0.0 ? -1.0 : 1.0) into copysign(1,-X). */
178 /* Transform (X < 0.0 ? -1.0 : 1.0) into copysign(1,X). */
179 /* Transform (X <= 0.0 ? -1.0 : 1.0) into copysign(1,X). */
181 (cond (cmp @0 real_zerop) real_minus_onep real_onep@1)
182 (if (!HONOR_NANS (type) && !HONOR_SIGNED_ZEROS (type)
183 && types_match (type, TREE_TYPE (@0)))
185 (if (types_match (type, float_type_node))
186 (BUILT_IN_COPYSIGNF @1 (outn @0)))
187 (if (types_match (type, double_type_node))
188 (BUILT_IN_COPYSIGN @1 (outn @0)))
189 (if (types_match (type, long_double_type_node))
190 (BUILT_IN_COPYSIGNL @1 (outn @0)))))))
192 /* Transform X * copysign (1.0, X) into abs(X). */
194 (mult:c @0 (COPYSIGN real_onep @0))
195 (if (!HONOR_NANS (type) && !HONOR_SIGNED_ZEROS (type))
198 /* Transform X * copysign (1.0, -X) into -abs(X). */
200 (mult:c @0 (COPYSIGN real_onep (negate @0)))
201 (if (!HONOR_NANS (type) && !HONOR_SIGNED_ZEROS (type))
204 /* Transform copysign (CST, X) into copysign (ABS(CST), X). */
206 (COPYSIGN REAL_CST@0 @1)
207 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@0)))
208 (COPYSIGN (negate @0) @1)))
210 /* X * 1, X / 1 -> X. */
211 (for op (mult trunc_div ceil_div floor_div round_div exact_div)
216 /* (A / (1 << B)) -> (A >> B).
217 Only for unsigned A. For signed A, this would not preserve rounding
219 For example: (-1 / ( 1 << B)) != -1 >> B. */
221 (trunc_div @0 (lshift integer_onep@1 @2))
222 (if ((TYPE_UNSIGNED (type) || tree_expr_nonnegative_p (@0))
223 && (!VECTOR_TYPE_P (type)
224 || target_supports_op_p (type, RSHIFT_EXPR, optab_vector)
225 || target_supports_op_p (type, RSHIFT_EXPR, optab_scalar)))
228 /* Preserve explicit divisions by 0: the C++ front-end wants to detect
229 undefined behavior in constexpr evaluation, and assuming that the division
230 traps enables better optimizations than these anyway. */
231 (for div (trunc_div ceil_div floor_div round_div exact_div)
232 /* 0 / X is always zero. */
234 (div integer_zerop@0 @1)
235 /* But not for 0 / 0 so that we can get the proper warnings and errors. */
236 (if (!integer_zerop (@1))
240 (div @0 integer_minus_onep@1)
241 (if (!TYPE_UNSIGNED (type))
246 /* But not for 0 / 0 so that we can get the proper warnings and errors. */
247 (if (!integer_zerop (@0))
248 { build_one_cst (type); }))
249 /* X / abs (X) is X < 0 ? -1 : 1. */
252 (if (INTEGRAL_TYPE_P (type)
253 && TYPE_OVERFLOW_UNDEFINED (type))
254 (cond (lt @0 { build_zero_cst (type); })
255 { build_minus_one_cst (type); } { build_one_cst (type); })))
258 (div:C @0 (negate @0))
259 (if ((INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
260 && TYPE_OVERFLOW_UNDEFINED (type))
261 { build_minus_one_cst (type); })))
263 /* For unsigned integral types, FLOOR_DIV_EXPR is the same as
264 TRUNC_DIV_EXPR. Rewrite into the latter in this case. */
267 (if ((INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
268 && TYPE_UNSIGNED (type))
271 /* Combine two successive divisions. Note that combining ceil_div
272 and floor_div is trickier and combining round_div even more so. */
273 (for div (trunc_div exact_div)
275 (div (div @0 INTEGER_CST@1) INTEGER_CST@2)
278 wide_int mul = wi::mul (@1, @2, TYPE_SIGN (type), &overflow_p);
281 (div @0 { wide_int_to_tree (type, mul); })
282 (if (TYPE_UNSIGNED (type)
283 || mul != wi::min_value (TYPE_PRECISION (type), SIGNED))
284 { build_zero_cst (type); })))))
286 /* Optimize A / A to 1.0 if we don't care about
287 NaNs or Infinities. */
290 (if (FLOAT_TYPE_P (type)
291 && ! HONOR_NANS (type)
292 && ! HONOR_INFINITIES (type))
293 { build_one_cst (type); }))
295 /* Optimize -A / A to -1.0 if we don't care about
296 NaNs or Infinities. */
298 (rdiv:C @0 (negate @0))
299 (if (FLOAT_TYPE_P (type)
300 && ! HONOR_NANS (type)
301 && ! HONOR_INFINITIES (type))
302 { build_minus_one_cst (type); }))
304 /* PR71078: x / abs(x) -> copysign (1.0, x) */
306 (rdiv:C (convert? @0) (convert? (abs @0)))
307 (if (SCALAR_FLOAT_TYPE_P (type)
308 && ! HONOR_NANS (type)
309 && ! HONOR_INFINITIES (type))
311 (if (types_match (type, float_type_node))
312 (BUILT_IN_COPYSIGNF { build_one_cst (type); } (convert @0)))
313 (if (types_match (type, double_type_node))
314 (BUILT_IN_COPYSIGN { build_one_cst (type); } (convert @0)))
315 (if (types_match (type, long_double_type_node))
316 (BUILT_IN_COPYSIGNL { build_one_cst (type); } (convert @0))))))
318 /* In IEEE floating point, x/1 is not equivalent to x for snans. */
321 (if (!HONOR_SNANS (type))
324 /* In IEEE floating point, x/-1 is not equivalent to -x for snans. */
326 (rdiv @0 real_minus_onep)
327 (if (!HONOR_SNANS (type))
330 (if (flag_reciprocal_math)
331 /* Convert (A/B)/C to A/(B*C) */
333 (rdiv (rdiv:s @0 @1) @2)
334 (rdiv @0 (mult @1 @2)))
336 /* Convert A/(B/C) to (A/B)*C */
338 (rdiv @0 (rdiv:s @1 @2))
339 (mult (rdiv @0 @1) @2)))
341 /* Optimize (X & (-A)) / A where A is a power of 2, to X >> log2(A) */
342 (for div (trunc_div ceil_div floor_div round_div exact_div)
344 (div (convert? (bit_and @0 INTEGER_CST@1)) INTEGER_CST@2)
345 (if (integer_pow2p (@2)
346 && tree_int_cst_sgn (@2) > 0
347 && wi::add (@2, @1) == 0
348 && tree_nop_conversion_p (type, TREE_TYPE (@0)))
349 (rshift (convert @0) { build_int_cst (integer_type_node,
350 wi::exact_log2 (@2)); }))))
352 /* If ARG1 is a constant, we can convert this to a multiply by the
353 reciprocal. This does not have the same rounding properties,
354 so only do this if -freciprocal-math. We can actually
355 always safely do it if ARG1 is a power of two, but it's hard to
356 tell if it is or not in a portable manner. */
357 (for cst (REAL_CST COMPLEX_CST VECTOR_CST)
361 (if (flag_reciprocal_math
364 { tree tem = const_binop (RDIV_EXPR, type, build_one_cst (type), @1); }
366 (mult @0 { tem; } )))
367 (if (cst != COMPLEX_CST)
368 (with { tree inverse = exact_inverse (type, @1); }
370 (mult @0 { inverse; } ))))))))
372 (for mod (ceil_mod floor_mod round_mod trunc_mod)
373 /* 0 % X is always zero. */
375 (mod integer_zerop@0 @1)
376 /* But not for 0 % 0 so that we can get the proper warnings and errors. */
377 (if (!integer_zerop (@1))
379 /* X % 1 is always zero. */
381 (mod @0 integer_onep)
382 { build_zero_cst (type); })
383 /* X % -1 is zero. */
385 (mod @0 integer_minus_onep@1)
386 (if (!TYPE_UNSIGNED (type))
387 { build_zero_cst (type); }))
391 /* But not for 0 % 0 so that we can get the proper warnings and errors. */
392 (if (!integer_zerop (@0))
393 { build_zero_cst (type); }))
394 /* (X % Y) % Y is just X % Y. */
396 (mod (mod@2 @0 @1) @1)
398 /* From extract_muldiv_1: (X * C1) % C2 is zero if C1 is a multiple of C2. */
400 (mod (mult @0 INTEGER_CST@1) INTEGER_CST@2)
401 (if (ANY_INTEGRAL_TYPE_P (type)
402 && TYPE_OVERFLOW_UNDEFINED (type)
403 && wi::multiple_of_p (@1, @2, TYPE_SIGN (type)))
404 { build_zero_cst (type); })))
406 /* X % -C is the same as X % C. */
408 (trunc_mod @0 INTEGER_CST@1)
409 (if (TYPE_SIGN (type) == SIGNED
410 && !TREE_OVERFLOW (@1)
412 && !TYPE_OVERFLOW_TRAPS (type)
413 /* Avoid this transformation if C is INT_MIN, i.e. C == -C. */
414 && !sign_bit_p (@1, @1))
415 (trunc_mod @0 (negate @1))))
417 /* X % -Y is the same as X % Y. */
419 (trunc_mod @0 (convert? (negate @1)))
420 (if (INTEGRAL_TYPE_P (type)
421 && !TYPE_UNSIGNED (type)
422 && !TYPE_OVERFLOW_TRAPS (type)
423 && tree_nop_conversion_p (type, TREE_TYPE (@1))
424 /* Avoid this transformation if X might be INT_MIN or
425 Y might be -1, because we would then change valid
426 INT_MIN % -(-1) into invalid INT_MIN % -1. */
427 && (expr_not_equal_to (@0, TYPE_MIN_VALUE (type))
428 || expr_not_equal_to (@1, wi::minus_one (TYPE_PRECISION
430 (trunc_mod @0 (convert @1))))
432 /* X - (X / Y) * Y is the same as X % Y. */
434 (minus (convert1? @0) (convert2? (mult:c (trunc_div @@0 @@1) @1)))
435 (if (INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
436 (convert (trunc_mod @0 @1))))
438 /* Optimize TRUNC_MOD_EXPR by a power of two into a BIT_AND_EXPR,
439 i.e. "X % C" into "X & (C - 1)", if X and C are positive.
440 Also optimize A % (C << N) where C is a power of 2,
441 to A & ((C << N) - 1). */
442 (match (power_of_two_cand @1)
444 (match (power_of_two_cand @1)
445 (lshift INTEGER_CST@1 @2))
446 (for mod (trunc_mod floor_mod)
448 (mod @0 (convert?@3 (power_of_two_cand@1 @2)))
449 (if ((TYPE_UNSIGNED (type)
450 || tree_expr_nonnegative_p (@0))
451 && tree_nop_conversion_p (type, TREE_TYPE (@3))
452 && integer_pow2p (@2) && tree_int_cst_sgn (@2) > 0)
453 (bit_and @0 (convert (minus @1 { build_int_cst (TREE_TYPE (@1), 1); }))))))
455 /* Simplify (unsigned t * 2)/2 -> unsigned t & 0x7FFFFFFF. */
457 (trunc_div (mult @0 integer_pow2p@1) @1)
458 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
459 (bit_and @0 { wide_int_to_tree
460 (type, wi::mask (TYPE_PRECISION (type) - wi::exact_log2 (@1),
461 false, TYPE_PRECISION (type))); })))
463 /* Simplify (unsigned t / 2) * 2 -> unsigned t & ~1. */
465 (mult (trunc_div @0 integer_pow2p@1) @1)
466 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
467 (bit_and @0 (negate @1))))
469 /* Simplify (t * 2) / 2) -> t. */
470 (for div (trunc_div ceil_div floor_div round_div exact_div)
472 (div (mult @0 @1) @1)
473 (if (ANY_INTEGRAL_TYPE_P (type)
474 && TYPE_OVERFLOW_UNDEFINED (type))
478 /* Simplify cos(-x) and cos(|x|) -> cos(x). Similarly for cosh. */
483 /* Simplify pow(-x, y) and pow(|x|,y) -> pow(x,y) if y is an even integer. */
486 (pows (op @0) REAL_CST@1)
487 (with { HOST_WIDE_INT n; }
488 (if (real_isinteger (&TREE_REAL_CST (@1), &n) && (n & 1) == 0)
490 /* Likewise for powi. */
493 (pows (op @0) INTEGER_CST@1)
494 (if (wi::bit_and (@1, 1) == 0)
496 /* Strip negate and abs from both operands of hypot. */
504 /* copysign(-x, y) and copysign(abs(x), y) -> copysign(x, y). */
505 (for copysigns (COPYSIGN)
507 (copysigns (op @0) @1)
510 /* abs(x)*abs(x) -> x*x. Should be valid for all types. */
515 /* cos(copysign(x, y)) -> cos(x). Similarly for cosh. */
519 (coss (copysigns @0 @1))
522 /* pow(copysign(x, y), z) -> pow(x, z) if z is an even integer. */
526 (pows (copysigns @0 @2) REAL_CST@1)
527 (with { HOST_WIDE_INT n; }
528 (if (real_isinteger (&TREE_REAL_CST (@1), &n) && (n & 1) == 0)
530 /* Likewise for powi. */
534 (pows (copysigns @0 @2) INTEGER_CST@1)
535 (if (wi::bit_and (@1, 1) == 0)
540 /* hypot(copysign(x, y), z) -> hypot(x, z). */
542 (hypots (copysigns @0 @1) @2)
544 /* hypot(x, copysign(y, z)) -> hypot(x, y). */
546 (hypots @0 (copysigns @1 @2))
549 /* copysign(x, CST) -> [-]abs (x). */
550 (for copysigns (COPYSIGN)
552 (copysigns @0 REAL_CST@1)
553 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
557 /* copysign(copysign(x, y), z) -> copysign(x, z). */
558 (for copysigns (COPYSIGN)
560 (copysigns (copysigns @0 @1) @2)
563 /* copysign(x,y)*copysign(x,y) -> x*x. */
564 (for copysigns (COPYSIGN)
566 (mult (copysigns@2 @0 @1) @2)
569 /* ccos(-x) -> ccos(x). Similarly for ccosh. */
570 (for ccoss (CCOS CCOSH)
575 /* cabs(-x) and cos(conj(x)) -> cabs(x). */
576 (for ops (conj negate)
582 /* Fold (a * (1 << b)) into (a << b) */
584 (mult:c @0 (convert? (lshift integer_onep@1 @2)))
585 (if (! FLOAT_TYPE_P (type)
586 && tree_nop_conversion_p (type, TREE_TYPE (@1)))
589 /* Fold (C1/X)*C2 into (C1*C2)/X. */
591 (mult (rdiv@3 REAL_CST@0 @1) REAL_CST@2)
592 (if (flag_associative_math
595 { tree tem = const_binop (MULT_EXPR, type, @0, @2); }
597 (rdiv { tem; } @1)))))
599 /* Convert C1/(X*C2) into (C1/C2)/X */
601 (rdiv REAL_CST@0 (mult @1 REAL_CST@2))
602 (if (flag_reciprocal_math)
604 { tree tem = const_binop (RDIV_EXPR, type, @0, @2); }
606 (rdiv { tem; } @1)))))
608 /* Simplify ~X & X as zero. */
610 (bit_and:c (convert? @0) (convert? (bit_not @0)))
611 { build_zero_cst (type); })
613 /* PR71636: Transform x & ((1U << b) - 1) -> x & ~(~0U << b); */
615 (bit_and:c @0 (plus:s (lshift:s integer_onep @1) integer_minus_onep))
616 (if (TYPE_UNSIGNED (type))
617 (bit_and @0 (bit_not (lshift { build_all_ones_cst (type); } @1)))))
619 /* PR35691: Transform
620 (x == 0 & y == 0) -> (x | typeof(x)(y)) == 0.
621 (x != 0 | y != 0) -> (x | typeof(x)(y)) != 0. */
622 (for bitop (bit_and bit_ior)
625 (bitop (cmp @0 integer_zerop@2) (cmp @1 integer_zerop))
626 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
627 && INTEGRAL_TYPE_P (TREE_TYPE (@1))
628 && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
629 (cmp (bit_ior @0 (convert @1)) @2))))
631 /* Fold (A & ~B) - (A & B) into (A ^ B) - B. */
633 (minus (bit_and:cs @0 (bit_not @1)) (bit_and:cs @0 @1))
634 (minus (bit_xor @0 @1) @1))
636 (minus (bit_and:s @0 INTEGER_CST@2) (bit_and:s @0 INTEGER_CST@1))
637 (if (wi::bit_not (@2) == @1)
638 (minus (bit_xor @0 @1) @1)))
640 /* Fold (A & B) - (A & ~B) into B - (A ^ B). */
642 (minus (bit_and:cs @0 @1) (bit_and:cs @0 (bit_not @1)))
643 (minus @1 (bit_xor @0 @1)))
645 /* Simplify (X & ~Y) |^+ (~X & Y) -> X ^ Y. */
646 (for op (bit_ior bit_xor plus)
648 (op (bit_and:c @0 (bit_not @1)) (bit_and:c (bit_not @0) @1))
651 (op:c (bit_and @0 INTEGER_CST@2) (bit_and (bit_not @0) INTEGER_CST@1))
652 (if (wi::bit_not (@2) == @1)
655 /* PR53979: Transform ((a ^ b) | a) -> (a | b) */
657 (bit_ior:c (bit_xor:c @0 @1) @0)
660 /* Simplify (~X & Y) to X ^ Y if we know that (X & ~Y) is 0. */
663 (bit_and (bit_not SSA_NAME@0) INTEGER_CST@1)
664 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
665 && (get_nonzero_bits (@0) & wi::bit_not (@1)) == 0)
669 /* X % Y is smaller than Y. */
672 (cmp (trunc_mod @0 @1) @1)
673 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
674 { constant_boolean_node (cmp == LT_EXPR, type); })))
677 (cmp @1 (trunc_mod @0 @1))
678 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
679 { constant_boolean_node (cmp == GT_EXPR, type); })))
683 (bit_ior @0 integer_all_onesp@1)
688 (bit_ior @0 integer_zerop)
693 (bit_and @0 integer_zerop@1)
699 (for op (bit_ior bit_xor plus)
701 (op:c (convert? @0) (convert? (bit_not @0)))
702 (convert { build_all_ones_cst (TREE_TYPE (@0)); })))
707 { build_zero_cst (type); })
709 /* Canonicalize X ^ ~0 to ~X. */
711 (bit_xor @0 integer_all_onesp@1)
716 (bit_and @0 integer_all_onesp)
719 /* x & x -> x, x | x -> x */
720 (for bitop (bit_and bit_ior)
725 /* x & C -> x if we know that x & ~C == 0. */
728 (bit_and SSA_NAME@0 INTEGER_CST@1)
729 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
730 && (get_nonzero_bits (@0) & wi::bit_not (@1)) == 0)
734 /* x + (x & 1) -> (x + 1) & ~1 */
736 (plus:c @0 (bit_and:s @0 integer_onep@1))
737 (bit_and (plus @0 @1) (bit_not @1)))
739 /* x & ~(x & y) -> x & ~y */
740 /* x | ~(x | y) -> x | ~y */
741 (for bitop (bit_and bit_ior)
743 (bitop:c @0 (bit_not (bitop:cs @0 @1)))
744 (bitop @0 (bit_not @1))))
746 /* (x | y) & ~x -> y & ~x */
747 /* (x & y) | ~x -> y | ~x */
748 (for bitop (bit_and bit_ior)
749 rbitop (bit_ior bit_and)
751 (bitop:c (rbitop:c @0 @1) (bit_not@2 @0))
754 /* (x & y) ^ (x | y) -> x ^ y */
756 (bit_xor:c (bit_and @0 @1) (bit_ior @0 @1))
759 /* (x ^ y) ^ (x | y) -> x & y */
761 (bit_xor:c (bit_xor @0 @1) (bit_ior @0 @1))
764 /* (x & y) + (x ^ y) -> x | y */
765 /* (x & y) | (x ^ y) -> x | y */
766 /* (x & y) ^ (x ^ y) -> x | y */
767 (for op (plus bit_ior bit_xor)
769 (op:c (bit_and @0 @1) (bit_xor @0 @1))
772 /* (x & y) + (x | y) -> x + y */
774 (plus:c (bit_and @0 @1) (bit_ior @0 @1))
777 /* (x + y) - (x | y) -> x & y */
779 (minus (plus @0 @1) (bit_ior @0 @1))
780 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
781 && !TYPE_SATURATING (type))
784 /* (x + y) - (x & y) -> x | y */
786 (minus (plus @0 @1) (bit_and @0 @1))
787 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
788 && !TYPE_SATURATING (type))
791 /* (x | y) - (x ^ y) -> x & y */
793 (minus (bit_ior @0 @1) (bit_xor @0 @1))
796 /* (x | y) - (x & y) -> x ^ y */
798 (minus (bit_ior @0 @1) (bit_and @0 @1))
801 /* (x | y) & ~(x & y) -> x ^ y */
803 (bit_and:c (bit_ior @0 @1) (bit_not (bit_and @0 @1)))
806 /* (x | y) & (~x ^ y) -> x & y */
808 (bit_and:c (bit_ior:c @0 @1) (bit_xor:c @1 (bit_not @0)))
811 /* ~x & ~y -> ~(x | y)
812 ~x | ~y -> ~(x & y) */
813 (for op (bit_and bit_ior)
814 rop (bit_ior bit_and)
816 (op (convert1? (bit_not @0)) (convert2? (bit_not @1)))
817 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
818 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
819 (bit_not (rop (convert @0) (convert @1))))))
821 /* If we are XORing or adding two BIT_AND_EXPR's, both of which are and'ing
822 with a constant, and the two constants have no bits in common,
823 we should treat this as a BIT_IOR_EXPR since this may produce more
825 (for op (bit_xor plus)
827 (op (convert1? (bit_and@4 @0 INTEGER_CST@1))
828 (convert2? (bit_and@5 @2 INTEGER_CST@3)))
829 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
830 && tree_nop_conversion_p (type, TREE_TYPE (@2))
831 && wi::bit_and (@1, @3) == 0)
832 (bit_ior (convert @4) (convert @5)))))
834 /* (X | Y) ^ X -> Y & ~ X*/
836 (bit_xor:c (convert1? (bit_ior:c @@0 @1)) (convert2? @0))
837 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
838 (convert (bit_and @1 (bit_not @0)))))
840 /* Convert ~X ^ ~Y to X ^ Y. */
842 (bit_xor (convert1? (bit_not @0)) (convert2? (bit_not @1)))
843 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
844 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
845 (bit_xor (convert @0) (convert @1))))
847 /* Convert ~X ^ C to X ^ ~C. */
849 (bit_xor (convert? (bit_not @0)) INTEGER_CST@1)
850 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
851 (bit_xor (convert @0) (bit_not @1))))
853 /* Fold (X & Y) ^ Y and (X ^ Y) & Y as ~X & Y. */
854 (for opo (bit_and bit_xor)
855 opi (bit_xor bit_and)
857 (opo:c (opi:c @0 @1) @1)
858 (bit_and (bit_not @0) @1)))
860 /* Given a bit-wise operation CODE applied to ARG0 and ARG1, see if both
861 operands are another bit-wise operation with a common input. If so,
862 distribute the bit operations to save an operation and possibly two if
863 constants are involved. For example, convert
864 (A | B) & (A | C) into A | (B & C)
865 Further simplification will occur if B and C are constants. */
866 (for op (bit_and bit_ior bit_xor)
867 rop (bit_ior bit_and bit_and)
869 (op (convert? (rop:c @@0 @1)) (convert? (rop:c @0 @2)))
870 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
871 && tree_nop_conversion_p (type, TREE_TYPE (@2)))
872 (rop (convert @0) (op (convert @1) (convert @2))))))
874 /* Some simple reassociation for bit operations, also handled in reassoc. */
875 /* (X & Y) & Y -> X & Y
876 (X | Y) | Y -> X | Y */
877 (for op (bit_and bit_ior)
879 (op:c (convert1?@2 (op:c @0 @@1)) (convert2? @1))
881 /* (X ^ Y) ^ Y -> X */
883 (bit_xor:c (convert1? (bit_xor:c @0 @@1)) (convert2? @1))
885 /* (X & Y) & (X & Z) -> (X & Y) & Z
886 (X | Y) | (X | Z) -> (X | Y) | Z */
887 (for op (bit_and bit_ior)
889 (op:c (convert1?@3 (op:c@4 @0 @1)) (convert2?@5 (op:c@6 @0 @2)))
890 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
891 && tree_nop_conversion_p (type, TREE_TYPE (@2)))
892 (if (single_use (@5) && single_use (@6))
894 (if (single_use (@3) && single_use (@4))
895 (op (convert @1) @5))))))
896 /* (X ^ Y) ^ (X ^ Z) -> Y ^ Z */
898 (bit_xor (convert1? (bit_xor:c @0 @1)) (convert2? (bit_xor:c @0 @2)))
899 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
900 && tree_nop_conversion_p (type, TREE_TYPE (@2)))
901 (bit_xor (convert @1) (convert @2))))
910 (abs tree_expr_nonnegative_p@0)
913 /* A few cases of fold-const.c negate_expr_p predicate. */
916 (if ((INTEGRAL_TYPE_P (type)
917 && TYPE_OVERFLOW_WRAPS (type))
918 || (!TYPE_OVERFLOW_SANITIZED (type)
919 && may_negate_without_overflow_p (t)))))
924 (if (!TYPE_OVERFLOW_SANITIZED (type))))
927 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (t)))))
928 /* VECTOR_CST handling of non-wrapping types would recurse in unsupported
932 (if (FLOAT_TYPE_P (TREE_TYPE (type)) || TYPE_OVERFLOW_WRAPS (type))))
934 /* (-A) * (-B) -> A * B */
936 (mult:c (convert1? (negate @0)) (convert2? negate_expr_p@1))
937 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
938 && tree_nop_conversion_p (type, TREE_TYPE (@1)))
939 (mult (convert @0) (convert (negate @1)))))
941 /* -(A + B) -> (-B) - A. */
943 (negate (plus:c @0 negate_expr_p@1))
944 (if (!HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type))
945 && !HONOR_SIGNED_ZEROS (element_mode (type)))
946 (minus (negate @1) @0)))
948 /* A - B -> A + (-B) if B is easily negatable. */
950 (minus @0 negate_expr_p@1)
951 (if (!FIXED_POINT_TYPE_P (type))
952 (plus @0 (negate @1))))
954 /* Try to fold (type) X op CST -> (type) (X op ((type-x) CST))
956 For bitwise binary operations apply operand conversions to the
957 binary operation result instead of to the operands. This allows
958 to combine successive conversions and bitwise binary operations.
959 We combine the above two cases by using a conditional convert. */
960 (for bitop (bit_and bit_ior bit_xor)
962 (bitop (convert @0) (convert? @1))
963 (if (((TREE_CODE (@1) == INTEGER_CST
964 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
965 && int_fits_type_p (@1, TREE_TYPE (@0)))
966 || types_match (@0, @1))
967 /* ??? This transform conflicts with fold-const.c doing
968 Convert (T)(x & c) into (T)x & (T)c, if c is an integer
969 constants (if x has signed type, the sign bit cannot be set
970 in c). This folds extension into the BIT_AND_EXPR.
971 Restrict it to GIMPLE to avoid endless recursions. */
972 && (bitop != BIT_AND_EXPR || GIMPLE)
973 && (/* That's a good idea if the conversion widens the operand, thus
974 after hoisting the conversion the operation will be narrower. */
975 TYPE_PRECISION (TREE_TYPE (@0)) < TYPE_PRECISION (type)
976 /* It's also a good idea if the conversion is to a non-integer
978 || GET_MODE_CLASS (TYPE_MODE (type)) != MODE_INT
979 /* Or if the precision of TO is not the same as the precision
981 || TYPE_PRECISION (type) != GET_MODE_PRECISION (TYPE_MODE (type))))
982 (convert (bitop @0 (convert @1))))))
984 (for bitop (bit_and bit_ior)
985 rbitop (bit_ior bit_and)
986 /* (x | y) & x -> x */
987 /* (x & y) | x -> x */
989 (bitop:c (rbitop:c @0 @1) @0)
991 /* (~x | y) & x -> x & y */
992 /* (~x & y) | x -> x | y */
994 (bitop:c (rbitop:c (bit_not @0) @1) @0)
997 /* (x | CST1) & CST2 -> (x & CST2) | (CST1 & CST2) */
999 (bit_and (bit_ior @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
1000 (bit_ior (bit_and @0 @2) (bit_and @1 @2)))
1002 /* Combine successive equal operations with constants. */
1003 (for bitop (bit_and bit_ior bit_xor)
1005 (bitop (bitop @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
1006 (bitop @0 (bitop @1 @2))))
1008 /* Try simple folding for X op !X, and X op X with the help
1009 of the truth_valued_p and logical_inverted_value predicates. */
1010 (match truth_valued_p
1012 (if (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1)))
1013 (for op (tcc_comparison truth_and truth_andif truth_or truth_orif truth_xor)
1014 (match truth_valued_p
1016 (match truth_valued_p
1019 (match (logical_inverted_value @0)
1021 (match (logical_inverted_value @0)
1022 (bit_not truth_valued_p@0))
1023 (match (logical_inverted_value @0)
1024 (eq @0 integer_zerop))
1025 (match (logical_inverted_value @0)
1026 (ne truth_valued_p@0 integer_truep))
1027 (match (logical_inverted_value @0)
1028 (bit_xor truth_valued_p@0 integer_truep))
1032 (bit_and:c @0 (logical_inverted_value @0))
1033 { build_zero_cst (type); })
1034 /* X | !X and X ^ !X -> 1, , if X is truth-valued. */
1035 (for op (bit_ior bit_xor)
1037 (op:c truth_valued_p@0 (logical_inverted_value @0))
1038 { constant_boolean_node (true, type); }))
1039 /* X ==/!= !X is false/true. */
1042 (op:c truth_valued_p@0 (logical_inverted_value @0))
1043 { constant_boolean_node (op == NE_EXPR ? true : false, type); }))
1047 (bit_not (bit_not @0))
1050 /* Convert ~ (-A) to A - 1. */
1052 (bit_not (convert? (negate @0)))
1053 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1054 || !TYPE_UNSIGNED (TREE_TYPE (@0)))
1055 (convert (minus @0 { build_each_one_cst (TREE_TYPE (@0)); }))))
1057 /* Convert ~ (A - 1) or ~ (A + -1) to -A. */
1059 (bit_not (convert? (minus @0 integer_each_onep)))
1060 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1061 || !TYPE_UNSIGNED (TREE_TYPE (@0)))
1062 (convert (negate @0))))
1064 (bit_not (convert? (plus @0 integer_all_onesp)))
1065 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1066 || !TYPE_UNSIGNED (TREE_TYPE (@0)))
1067 (convert (negate @0))))
1069 /* Part of convert ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify. */
1071 (bit_not (convert? (bit_xor @0 INTEGER_CST@1)))
1072 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1073 (convert (bit_xor @0 (bit_not @1)))))
1075 (bit_not (convert? (bit_xor:c (bit_not @0) @1)))
1076 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1077 (convert (bit_xor @0 @1))))
1079 /* (x & ~m) | (y & m) -> ((x ^ y) & m) ^ x */
1081 (bit_ior:c (bit_and:cs @0 (bit_not @2)) (bit_and:cs @1 @2))
1082 (bit_xor (bit_and (bit_xor @0 @1) @2) @0))
1084 /* Fold A - (A & B) into ~B & A. */
1086 (minus (convert1? @0) (convert2?:s (bit_and:cs @@0 @1)))
1087 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
1088 && tree_nop_conversion_p (type, TREE_TYPE (@1)))
1089 (convert (bit_and (bit_not @1) @0))))
1091 /* For integral types with undefined overflow and C != 0 fold
1092 x * C EQ/NE y * C into x EQ/NE y. */
1095 (cmp (mult:c @0 @1) (mult:c @2 @1))
1096 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
1097 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1098 && tree_expr_nonzero_p (@1))
1101 /* For integral types with wrapping overflow and C odd fold
1102 x * C EQ/NE y * C into x EQ/NE y. */
1105 (cmp (mult @0 INTEGER_CST@1) (mult @2 @1))
1106 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
1107 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))
1108 && (TREE_INT_CST_LOW (@1) & 1) != 0)
1111 /* For integral types with undefined overflow and C != 0 fold
1112 x * C RELOP y * C into:
1114 x RELOP y for nonnegative C
1115 y RELOP x for negative C */
1116 (for cmp (lt gt le ge)
1118 (cmp (mult:c @0 @1) (mult:c @2 @1))
1119 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
1120 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1121 (if (tree_expr_nonnegative_p (@1) && tree_expr_nonzero_p (@1))
1123 (if (TREE_CODE (@1) == INTEGER_CST
1124 && wi::neg_p (@1, TYPE_SIGN (TREE_TYPE (@1))))
1127 /* X / 4 < Y / 4 iff X < Y when the division is known to be exact. */
1128 (for cmp (simple_comparison)
1130 (cmp (exact_div @0 INTEGER_CST@2) (exact_div @1 @2))
1131 (if (wi::gt_p(@2, 0, TYPE_SIGN (TREE_TYPE (@2))))
1134 /* X + Z < Y + Z is the same as X < Y when there is no overflow. */
1135 (for op (lt le ge gt)
1137 (op (plus:c @0 @2) (plus:c @1 @2))
1138 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1139 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1141 /* For equality and subtraction, this is also true with wrapping overflow. */
1142 (for op (eq ne minus)
1144 (op (plus:c @0 @2) (plus:c @1 @2))
1145 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1146 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1147 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
1150 /* X - Z < Y - Z is the same as X < Y when there is no overflow. */
1151 (for op (lt le ge gt)
1153 (op (minus @0 @2) (minus @1 @2))
1154 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1155 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1157 /* For equality and subtraction, this is also true with wrapping overflow. */
1158 (for op (eq ne minus)
1160 (op (minus @0 @2) (minus @1 @2))
1161 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1162 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1163 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
1166 /* Z - X < Z - Y is the same as Y < X when there is no overflow. */
1167 (for op (lt le ge gt)
1169 (op (minus @2 @0) (minus @2 @1))
1170 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1171 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1173 /* For equality and subtraction, this is also true with wrapping overflow. */
1174 (for op (eq ne minus)
1176 (op (minus @2 @0) (minus @2 @1))
1177 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1178 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1179 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
1182 /* X == C - X can never be true if C is odd. */
1185 (cmp:c (convert? @0) (convert1? (minus INTEGER_CST@1 (convert2? @0))))
1186 (if (TREE_INT_CST_LOW (@1) & 1)
1187 { constant_boolean_node (cmp == NE_EXPR, type); })))
1189 /* Arguments on which one can call get_nonzero_bits to get the bits
1191 (match with_possible_nonzero_bits
1193 (match with_possible_nonzero_bits
1195 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))))
1196 /* Slightly extended version, do not make it recursive to keep it cheap. */
1197 (match (with_possible_nonzero_bits2 @0)
1198 with_possible_nonzero_bits@0)
1199 (match (with_possible_nonzero_bits2 @0)
1200 (bit_and:c with_possible_nonzero_bits@0 @2))
1202 /* Same for bits that are known to be set, but we do not have
1203 an equivalent to get_nonzero_bits yet. */
1204 (match (with_certain_nonzero_bits2 @0)
1206 (match (with_certain_nonzero_bits2 @0)
1207 (bit_ior @1 INTEGER_CST@0))
1209 /* X == C (or X & Z == Y | C) is impossible if ~nonzero(X) & C != 0. */
1212 (cmp:c (with_possible_nonzero_bits2 @0) (with_certain_nonzero_bits2 @1))
1213 (if ((~get_nonzero_bits (@0) & @1) != 0)
1214 { constant_boolean_node (cmp == NE_EXPR, type); })))
1216 /* ((X inner_op C0) outer_op C1)
1217 With X being a tree where value_range has reasoned certain bits to always be
1218 zero throughout its computed value range,
1219 inner_op = {|,^}, outer_op = {|,^} and inner_op != outer_op
1220 where zero_mask has 1's for all bits that are sure to be 0 in
1222 if (inner_op == '^') C0 &= ~C1;
1223 if ((C0 & ~zero_mask) == 0) then emit (X outer_op (C0 outer_op C1)
1224 if ((C1 & ~zero_mask) == 0) then emit (X inner_op (C0 outer_op C1)
1226 (for inner_op (bit_ior bit_xor)
1227 outer_op (bit_xor bit_ior)
1230 (inner_op:s @2 INTEGER_CST@0) INTEGER_CST@1)
1234 wide_int zero_mask_not;
1238 if (TREE_CODE (@2) == SSA_NAME)
1239 zero_mask_not = get_nonzero_bits (@2);
1243 if (inner_op == BIT_XOR_EXPR)
1245 C0 = wi::bit_and_not (@0, @1);
1246 cst_emit = wi::bit_or (C0, @1);
1251 cst_emit = wi::bit_xor (@0, @1);
1254 (if (!fail && wi::bit_and (C0, zero_mask_not) == 0)
1255 (outer_op @2 { wide_int_to_tree (type, cst_emit); })
1256 (if (!fail && wi::bit_and (@1, zero_mask_not) == 0)
1257 (inner_op @2 { wide_int_to_tree (type, cst_emit); }))))))
1259 /* Associate (p +p off1) +p off2 as (p +p (off1 + off2)). */
1261 (pointer_plus (pointer_plus:s @0 @1) @3)
1262 (pointer_plus @0 (plus @1 @3)))
1268 tem4 = (unsigned long) tem3;
1273 (pointer_plus @0 (convert?@2 (minus@3 (convert @1) (convert @0))))
1274 /* Conditionally look through a sign-changing conversion. */
1275 (if (TYPE_PRECISION (TREE_TYPE (@2)) == TYPE_PRECISION (TREE_TYPE (@3))
1276 && ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@1)))
1277 || (GENERIC && type == TREE_TYPE (@1))))
1281 tem = (sizetype) ptr;
1285 and produce the simpler and easier to analyze with respect to alignment
1286 ... = ptr & ~algn; */
1288 (pointer_plus @0 (negate (bit_and (convert @0) INTEGER_CST@1)))
1289 (with { tree algn = wide_int_to_tree (TREE_TYPE (@0), wi::bit_not (@1)); }
1290 (bit_and @0 { algn; })))
1292 /* Try folding difference of addresses. */
1294 (minus (convert ADDR_EXPR@0) (convert @1))
1295 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1296 (with { HOST_WIDE_INT diff; }
1297 (if (ptr_difference_const (@0, @1, &diff))
1298 { build_int_cst_type (type, diff); }))))
1300 (minus (convert @0) (convert ADDR_EXPR@1))
1301 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1302 (with { HOST_WIDE_INT diff; }
1303 (if (ptr_difference_const (@0, @1, &diff))
1304 { build_int_cst_type (type, diff); }))))
1306 /* If arg0 is derived from the address of an object or function, we may
1307 be able to fold this expression using the object or function's
1310 (bit_and (convert? @0) INTEGER_CST@1)
1311 (if (POINTER_TYPE_P (TREE_TYPE (@0))
1312 && tree_nop_conversion_p (type, TREE_TYPE (@0)))
1316 unsigned HOST_WIDE_INT bitpos;
1317 get_pointer_alignment_1 (@0, &align, &bitpos);
1319 (if (wi::ltu_p (@1, align / BITS_PER_UNIT))
1320 { wide_int_to_tree (type, wi::bit_and (@1, bitpos / BITS_PER_UNIT)); }))))
1323 /* We can't reassociate at all for saturating types. */
1324 (if (!TYPE_SATURATING (type))
1326 /* Contract negates. */
1327 /* A + (-B) -> A - B */
1329 (plus:c @0 (convert? (negate @1)))
1330 /* Apply STRIP_NOPS on the negate. */
1331 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1332 && !TYPE_OVERFLOW_SANITIZED (type))
1336 if (INTEGRAL_TYPE_P (type)
1337 && TYPE_OVERFLOW_WRAPS (type) != TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
1338 t1 = TYPE_OVERFLOW_WRAPS (type) ? type : TREE_TYPE (@1);
1340 (convert (minus (convert:t1 @0) (convert:t1 @1))))))
1341 /* A - (-B) -> A + B */
1343 (minus @0 (convert? (negate @1)))
1344 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1345 && !TYPE_OVERFLOW_SANITIZED (type))
1349 if (INTEGRAL_TYPE_P (type)
1350 && TYPE_OVERFLOW_WRAPS (type) != TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
1351 t1 = TYPE_OVERFLOW_WRAPS (type) ? type : TREE_TYPE (@1);
1353 (convert (plus (convert:t1 @0) (convert:t1 @1))))))
1356 (negate (convert? (negate @1)))
1357 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1358 && !TYPE_OVERFLOW_SANITIZED (type))
1361 /* We can't reassociate floating-point unless -fassociative-math
1362 or fixed-point plus or minus because of saturation to +-Inf. */
1363 (if ((!FLOAT_TYPE_P (type) || flag_associative_math)
1364 && !FIXED_POINT_TYPE_P (type))
1366 /* Match patterns that allow contracting a plus-minus pair
1367 irrespective of overflow issues. */
1368 /* (A +- B) - A -> +- B */
1369 /* (A +- B) -+ B -> A */
1370 /* A - (A +- B) -> -+ B */
1371 /* A +- (B -+ A) -> +- B */
1373 (minus (plus:c @0 @1) @0)
1376 (minus (minus @0 @1) @0)
1379 (plus:c (minus @0 @1) @1)
1382 (minus @0 (plus:c @0 @1))
1385 (minus @0 (minus @0 @1))
1387 /* (A +- B) + (C - A) -> C +- B */
1388 /* (A + B) - (A - C) -> B + C */
1389 /* More cases are handled with comparisons. */
1391 (plus:c (plus:c @0 @1) (minus @2 @0))
1394 (plus:c (minus @0 @1) (minus @2 @0))
1397 (minus (plus:c @0 @1) (minus @0 @2))
1400 /* (A +- CST1) +- CST2 -> A + CST3
1401 Use view_convert because it is safe for vectors and equivalent for
1403 (for outer_op (plus minus)
1404 (for inner_op (plus minus)
1405 neg_inner_op (minus plus)
1407 (outer_op (nop_convert (inner_op @0 CONSTANT_CLASS_P@1))
1409 /* If one of the types wraps, use that one. */
1410 (if (!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_WRAPS (type))
1411 (if (outer_op == PLUS_EXPR)
1412 (plus (view_convert @0) (inner_op @2 (view_convert @1)))
1413 (minus (view_convert @0) (neg_inner_op @2 (view_convert @1))))
1414 (if (!ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1415 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
1416 (if (outer_op == PLUS_EXPR)
1417 (view_convert (plus @0 (inner_op (view_convert @2) @1)))
1418 (view_convert (minus @0 (neg_inner_op (view_convert @2) @1))))
1419 /* If the constant operation overflows we cannot do the transform
1420 directly as we would introduce undefined overflow, for example
1421 with (a - 1) + INT_MIN. */
1422 (if (types_match (type, @0))
1423 (with { tree cst = const_binop (outer_op == inner_op
1424 ? PLUS_EXPR : MINUS_EXPR,
1426 (if (cst && !TREE_OVERFLOW (cst))
1427 (inner_op @0 { cst; } )
1428 /* X+INT_MAX+1 is X-INT_MIN. */
1429 (if (INTEGRAL_TYPE_P (type) && cst
1430 && wi::eq_p (cst, wi::min_value (type)))
1431 (neg_inner_op @0 { wide_int_to_tree (type, cst); })
1432 /* Last resort, use some unsigned type. */
1433 (with { tree utype = unsigned_type_for (type); }
1434 (view_convert (inner_op
1435 (view_convert:utype @0)
1437 { drop_tree_overflow (cst); })))))))))))))
1439 /* (CST1 - A) +- CST2 -> CST3 - A */
1440 (for outer_op (plus minus)
1442 (outer_op (minus CONSTANT_CLASS_P@1 @0) CONSTANT_CLASS_P@2)
1443 (with { tree cst = const_binop (outer_op, type, @1, @2); }
1444 (if (cst && !TREE_OVERFLOW (cst))
1445 (minus { cst; } @0)))))
1447 /* CST1 - (CST2 - A) -> CST3 + A */
1449 (minus CONSTANT_CLASS_P@1 (minus CONSTANT_CLASS_P@2 @0))
1450 (with { tree cst = const_binop (MINUS_EXPR, type, @1, @2); }
1451 (if (cst && !TREE_OVERFLOW (cst))
1452 (plus { cst; } @0))))
1456 (plus:c (bit_not @0) @0)
1457 (if (!TYPE_OVERFLOW_TRAPS (type))
1458 { build_all_ones_cst (type); }))
1462 (plus (convert? (bit_not @0)) integer_each_onep)
1463 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1464 (negate (convert @0))))
1468 (minus (convert? (negate @0)) integer_each_onep)
1469 (if (!TYPE_OVERFLOW_TRAPS (type)
1470 && tree_nop_conversion_p (type, TREE_TYPE (@0)))
1471 (bit_not (convert @0))))
1475 (minus integer_all_onesp @0)
1478 /* (T)(P + A) - (T)P -> (T) A */
1479 (for add (plus pointer_plus)
1481 (minus (convert (add @@0 @1))
1483 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
1484 /* For integer types, if A has a smaller type
1485 than T the result depends on the possible
1487 E.g. T=size_t, A=(unsigned)429497295, P>0.
1488 However, if an overflow in P + A would cause
1489 undefined behavior, we can assume that there
1491 || (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1492 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1493 /* For pointer types, if the conversion of A to the
1494 final type requires a sign- or zero-extension,
1495 then we have to punt - it is not defined which
1497 || (POINTER_TYPE_P (TREE_TYPE (@0))
1498 && TREE_CODE (@1) == INTEGER_CST
1499 && tree_int_cst_sign_bit (@1) == 0))
1502 /* (T)P - (T)(P + A) -> -(T) A */
1503 (for add (plus pointer_plus)
1506 (convert (add @@0 @1)))
1507 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
1508 /* For integer types, if A has a smaller type
1509 than T the result depends on the possible
1511 E.g. T=size_t, A=(unsigned)429497295, P>0.
1512 However, if an overflow in P + A would cause
1513 undefined behavior, we can assume that there
1515 || (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1516 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1517 /* For pointer types, if the conversion of A to the
1518 final type requires a sign- or zero-extension,
1519 then we have to punt - it is not defined which
1521 || (POINTER_TYPE_P (TREE_TYPE (@0))
1522 && TREE_CODE (@1) == INTEGER_CST
1523 && tree_int_cst_sign_bit (@1) == 0))
1524 (negate (convert @1)))))
1526 /* (T)(P + A) - (T)(P + B) -> (T)A - (T)B */
1527 (for add (plus pointer_plus)
1529 (minus (convert (add @@0 @1))
1530 (convert (add @0 @2)))
1531 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
1532 /* For integer types, if A has a smaller type
1533 than T the result depends on the possible
1535 E.g. T=size_t, A=(unsigned)429497295, P>0.
1536 However, if an overflow in P + A would cause
1537 undefined behavior, we can assume that there
1539 || (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1540 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1541 /* For pointer types, if the conversion of A to the
1542 final type requires a sign- or zero-extension,
1543 then we have to punt - it is not defined which
1545 || (POINTER_TYPE_P (TREE_TYPE (@0))
1546 && TREE_CODE (@1) == INTEGER_CST
1547 && tree_int_cst_sign_bit (@1) == 0
1548 && TREE_CODE (@2) == INTEGER_CST
1549 && tree_int_cst_sign_bit (@2) == 0))
1550 (minus (convert @1) (convert @2)))))))
1553 /* Simplifications of MIN_EXPR, MAX_EXPR, fmin() and fmax(). */
1555 (for minmax (min max FMIN FMAX)
1559 /* min(max(x,y),y) -> y. */
1561 (min:c (max:c @0 @1) @1)
1563 /* max(min(x,y),y) -> y. */
1565 (max:c (min:c @0 @1) @1)
1567 /* max(a,-a) -> abs(a). */
1569 (max:c @0 (negate @0))
1570 (if (TREE_CODE (type) != COMPLEX_TYPE
1571 && (! ANY_INTEGRAL_TYPE_P (type)
1572 || TYPE_OVERFLOW_UNDEFINED (type)))
1574 /* min(a,-a) -> -abs(a). */
1576 (min:c @0 (negate @0))
1577 (if (TREE_CODE (type) != COMPLEX_TYPE
1578 && (! ANY_INTEGRAL_TYPE_P (type)
1579 || TYPE_OVERFLOW_UNDEFINED (type)))
1584 (if (INTEGRAL_TYPE_P (type)
1585 && TYPE_MIN_VALUE (type)
1586 && operand_equal_p (@1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
1588 (if (INTEGRAL_TYPE_P (type)
1589 && TYPE_MAX_VALUE (type)
1590 && operand_equal_p (@1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
1595 (if (INTEGRAL_TYPE_P (type)
1596 && TYPE_MAX_VALUE (type)
1597 && operand_equal_p (@1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
1599 (if (INTEGRAL_TYPE_P (type)
1600 && TYPE_MIN_VALUE (type)
1601 && operand_equal_p (@1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
1604 /* max (a, a + CST) -> a + CST where CST is positive. */
1605 /* max (a, a + CST) -> a where CST is negative. */
1607 (max:c @0 (plus@2 @0 INTEGER_CST@1))
1608 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1609 (if (tree_int_cst_sgn (@1) > 0)
1613 /* min (a, a + CST) -> a where CST is positive. */
1614 /* min (a, a + CST) -> a + CST where CST is negative. */
1616 (min:c @0 (plus@2 @0 INTEGER_CST@1))
1617 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1618 (if (tree_int_cst_sgn (@1) > 0)
1622 /* (convert (minmax ((convert (x) c)))) -> minmax (x c) if x is promoted
1623 and the outer convert demotes the expression back to x's type. */
1624 (for minmax (min max)
1626 (convert (minmax@0 (convert @1) INTEGER_CST@2))
1627 (if (INTEGRAL_TYPE_P (type)
1628 && types_match (@1, type) && int_fits_type_p (@2, type)
1629 && TYPE_SIGN (TREE_TYPE (@0)) == TYPE_SIGN (type)
1630 && TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type))
1631 (minmax @1 (convert @2)))))
1633 (for minmax (FMIN FMAX)
1634 /* If either argument is NaN, return the other one. Avoid the
1635 transformation if we get (and honor) a signalling NaN. */
1637 (minmax:c @0 REAL_CST@1)
1638 (if (real_isnan (TREE_REAL_CST_PTR (@1))
1639 && (!HONOR_SNANS (@1) || !TREE_REAL_CST (@1).signalling))
1641 /* Convert fmin/fmax to MIN_EXPR/MAX_EXPR. C99 requires these
1642 functions to return the numeric arg if the other one is NaN.
1643 MIN and MAX don't honor that, so only transform if -ffinite-math-only
1644 is set. C99 doesn't require -0.0 to be handled, so we don't have to
1645 worry about it either. */
1646 (if (flag_finite_math_only)
1653 /* min (-A, -B) -> -max (A, B) */
1654 (for minmax (min max FMIN FMAX)
1655 maxmin (max min FMAX FMIN)
1657 (minmax (negate:s@2 @0) (negate:s@3 @1))
1658 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
1659 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1660 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
1661 (negate (maxmin @0 @1)))))
1662 /* MIN (~X, ~Y) -> ~MAX (X, Y)
1663 MAX (~X, ~Y) -> ~MIN (X, Y) */
1664 (for minmax (min max)
1667 (minmax (bit_not:s@2 @0) (bit_not:s@3 @1))
1668 (bit_not (maxmin @0 @1))))
1670 /* MIN (X, Y) == X -> X <= Y */
1671 (for minmax (min min max max)
1675 (cmp:c (minmax:c @0 @1) @0)
1676 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0)))
1678 /* MIN (X, 5) == 0 -> X == 0
1679 MIN (X, 5) == 7 -> false */
1682 (cmp (min @0 INTEGER_CST@1) INTEGER_CST@2)
1683 (if (wi::lt_p (@1, @2, TYPE_SIGN (TREE_TYPE (@0))))
1684 { constant_boolean_node (cmp == NE_EXPR, type); }
1685 (if (wi::gt_p (@1, @2, TYPE_SIGN (TREE_TYPE (@0))))
1689 (cmp (max @0 INTEGER_CST@1) INTEGER_CST@2)
1690 (if (wi::gt_p (@1, @2, TYPE_SIGN (TREE_TYPE (@0))))
1691 { constant_boolean_node (cmp == NE_EXPR, type); }
1692 (if (wi::lt_p (@1, @2, TYPE_SIGN (TREE_TYPE (@0))))
1694 /* MIN (X, C1) < C2 -> X < C2 || C1 < C2 */
1695 (for minmax (min min max max min min max max )
1696 cmp (lt le gt ge gt ge lt le )
1697 comb (bit_ior bit_ior bit_ior bit_ior bit_and bit_and bit_and bit_and)
1699 (cmp (minmax @0 INTEGER_CST@1) INTEGER_CST@2)
1700 (comb (cmp @0 @2) (cmp @1 @2))))
1702 /* Simplifications of shift and rotates. */
1704 (for rotate (lrotate rrotate)
1706 (rotate integer_all_onesp@0 @1)
1709 /* Optimize -1 >> x for arithmetic right shifts. */
1711 (rshift integer_all_onesp@0 @1)
1712 (if (!TYPE_UNSIGNED (type)
1713 && tree_expr_nonnegative_p (@1))
1716 /* Optimize (x >> c) << c into x & (-1<<c). */
1718 (lshift (rshift @0 INTEGER_CST@1) @1)
1719 (if (wi::ltu_p (@1, element_precision (type)))
1720 (bit_and @0 (lshift { build_minus_one_cst (type); } @1))))
1722 /* Optimize (x << c) >> c into x & ((unsigned)-1 >> c) for unsigned
1725 (rshift (lshift @0 INTEGER_CST@1) @1)
1726 (if (TYPE_UNSIGNED (type)
1727 && (wi::ltu_p (@1, element_precision (type))))
1728 (bit_and @0 (rshift { build_minus_one_cst (type); } @1))))
1730 (for shiftrotate (lrotate rrotate lshift rshift)
1732 (shiftrotate @0 integer_zerop)
1735 (shiftrotate integer_zerop@0 @1)
1737 /* Prefer vector1 << scalar to vector1 << vector2
1738 if vector2 is uniform. */
1739 (for vec (VECTOR_CST CONSTRUCTOR)
1741 (shiftrotate @0 vec@1)
1742 (with { tree tem = uniform_vector_p (@1); }
1744 (shiftrotate @0 { tem; }))))))
1746 /* Simplify X << Y where Y's low width bits are 0 to X, as only valid
1747 Y is 0. Similarly for X >> Y. */
1749 (for shift (lshift rshift)
1751 (shift @0 SSA_NAME@1)
1752 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1)))
1754 int width = ceil_log2 (element_precision (TREE_TYPE (@0)));
1755 int prec = TYPE_PRECISION (TREE_TYPE (@1));
1757 (if ((get_nonzero_bits (@1) & wi::mask (width, false, prec)) == 0)
1761 /* Rewrite an LROTATE_EXPR by a constant into an
1762 RROTATE_EXPR by a new constant. */
1764 (lrotate @0 INTEGER_CST@1)
1765 (rrotate @0 { const_binop (MINUS_EXPR, TREE_TYPE (@1),
1766 build_int_cst (TREE_TYPE (@1),
1767 element_precision (type)), @1); }))
1769 /* Turn (a OP c1) OP c2 into a OP (c1+c2). */
1770 (for op (lrotate rrotate rshift lshift)
1772 (op (op @0 INTEGER_CST@1) INTEGER_CST@2)
1773 (with { unsigned int prec = element_precision (type); }
1774 (if (wi::ge_p (@1, 0, TYPE_SIGN (TREE_TYPE (@1)))
1775 && wi::lt_p (@1, prec, TYPE_SIGN (TREE_TYPE (@1)))
1776 && wi::ge_p (@2, 0, TYPE_SIGN (TREE_TYPE (@2)))
1777 && wi::lt_p (@2, prec, TYPE_SIGN (TREE_TYPE (@2))))
1778 (with { unsigned int low = wi::add (@1, @2).to_uhwi (); }
1779 /* Deal with a OP (c1 + c2) being undefined but (a OP c1) OP c2
1780 being well defined. */
1782 (if (op == LROTATE_EXPR || op == RROTATE_EXPR)
1783 (op @0 { build_int_cst (TREE_TYPE (@1), low % prec); })
1784 (if (TYPE_UNSIGNED (type) || op == LSHIFT_EXPR)
1785 { build_zero_cst (type); }
1786 (op @0 { build_int_cst (TREE_TYPE (@1), prec - 1); })))
1787 (op @0 { build_int_cst (TREE_TYPE (@1), low); })))))))
1790 /* ((1 << A) & 1) != 0 -> A == 0
1791 ((1 << A) & 1) == 0 -> A != 0 */
1795 (cmp (bit_and (lshift integer_onep @0) integer_onep) integer_zerop)
1796 (icmp @0 { build_zero_cst (TREE_TYPE (@0)); })))
1798 /* (CST1 << A) == CST2 -> A == ctz (CST2) - ctz (CST1)
1799 (CST1 << A) != CST2 -> A != ctz (CST2) - ctz (CST1)
1803 (cmp (lshift INTEGER_CST@0 @1) INTEGER_CST@2)
1804 (with { int cand = wi::ctz (@2) - wi::ctz (@0); }
1806 || (!integer_zerop (@2)
1807 && wi::ne_p (wi::lshift (@0, cand), @2)))
1808 { constant_boolean_node (cmp == NE_EXPR, type); }
1809 (if (!integer_zerop (@2)
1810 && wi::eq_p (wi::lshift (@0, cand), @2))
1811 (cmp @1 { build_int_cst (TREE_TYPE (@1), cand); }))))))
1813 /* Fold (X << C1) & C2 into (X << C1) & (C2 | ((1 << C1) - 1))
1814 (X >> C1) & C2 into (X >> C1) & (C2 | ~((type) -1 >> C1))
1815 if the new mask might be further optimized. */
1816 (for shift (lshift rshift)
1818 (bit_and (convert?:s@4 (shift:s@5 (convert1?@3 @0) INTEGER_CST@1))
1820 (if (tree_nop_conversion_p (TREE_TYPE (@4), TREE_TYPE (@5))
1821 && TYPE_PRECISION (type) <= HOST_BITS_PER_WIDE_INT
1822 && tree_fits_uhwi_p (@1)
1823 && tree_to_uhwi (@1) > 0
1824 && tree_to_uhwi (@1) < TYPE_PRECISION (type))
1827 unsigned int shiftc = tree_to_uhwi (@1);
1828 unsigned HOST_WIDE_INT mask = TREE_INT_CST_LOW (@2);
1829 unsigned HOST_WIDE_INT newmask, zerobits = 0;
1830 tree shift_type = TREE_TYPE (@3);
1833 if (shift == LSHIFT_EXPR)
1834 zerobits = ((HOST_WIDE_INT_1U << shiftc) - 1);
1835 else if (shift == RSHIFT_EXPR
1836 && (TYPE_PRECISION (shift_type)
1837 == GET_MODE_PRECISION (TYPE_MODE (shift_type))))
1839 prec = TYPE_PRECISION (TREE_TYPE (@3));
1841 /* See if more bits can be proven as zero because of
1844 && TYPE_UNSIGNED (TREE_TYPE (@0)))
1846 tree inner_type = TREE_TYPE (@0);
1847 if ((TYPE_PRECISION (inner_type)
1848 == GET_MODE_PRECISION (TYPE_MODE (inner_type)))
1849 && TYPE_PRECISION (inner_type) < prec)
1851 prec = TYPE_PRECISION (inner_type);
1852 /* See if we can shorten the right shift. */
1854 shift_type = inner_type;
1855 /* Otherwise X >> C1 is all zeros, so we'll optimize
1856 it into (X, 0) later on by making sure zerobits
1860 zerobits = HOST_WIDE_INT_M1U;
1863 zerobits >>= HOST_BITS_PER_WIDE_INT - shiftc;
1864 zerobits <<= prec - shiftc;
1866 /* For arithmetic shift if sign bit could be set, zerobits
1867 can contain actually sign bits, so no transformation is
1868 possible, unless MASK masks them all away. In that
1869 case the shift needs to be converted into logical shift. */
1870 if (!TYPE_UNSIGNED (TREE_TYPE (@3))
1871 && prec == TYPE_PRECISION (TREE_TYPE (@3)))
1873 if ((mask & zerobits) == 0)
1874 shift_type = unsigned_type_for (TREE_TYPE (@3));
1880 /* ((X << 16) & 0xff00) is (X, 0). */
1881 (if ((mask & zerobits) == mask)
1882 { build_int_cst (type, 0); }
1883 (with { newmask = mask | zerobits; }
1884 (if (newmask != mask && (newmask & (newmask + 1)) == 0)
1887 /* Only do the transformation if NEWMASK is some integer
1889 for (prec = BITS_PER_UNIT;
1890 prec < HOST_BITS_PER_WIDE_INT; prec <<= 1)
1891 if (newmask == (HOST_WIDE_INT_1U << prec) - 1)
1894 (if (prec < HOST_BITS_PER_WIDE_INT
1895 || newmask == HOST_WIDE_INT_M1U)
1897 { tree newmaskt = build_int_cst_type (TREE_TYPE (@2), newmask); }
1898 (if (!tree_int_cst_equal (newmaskt, @2))
1899 (if (shift_type != TREE_TYPE (@3))
1900 (bit_and (convert (shift:shift_type (convert @3) @1)) { newmaskt; })
1901 (bit_and @4 { newmaskt; })))))))))))))
1903 /* Fold (X {&,^,|} C2) << C1 into (X << C1) {&,^,|} (C2 << C1)
1904 (X {&,^,|} C2) >> C1 into (X >> C1) & (C2 >> C1). */
1905 (for shift (lshift rshift)
1906 (for bit_op (bit_and bit_xor bit_ior)
1908 (shift (convert?:s (bit_op:s @0 INTEGER_CST@2)) INTEGER_CST@1)
1909 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1910 (with { tree mask = int_const_binop (shift, fold_convert (type, @2), @1); }
1911 (bit_op (shift (convert @0) @1) { mask; }))))))
1913 /* ~(~X >> Y) -> X >> Y (for arithmetic shift). */
1915 (bit_not (convert1?:s (rshift:s (convert2?@0 (bit_not @1)) @2)))
1916 (if (!TYPE_UNSIGNED (TREE_TYPE (@0))
1917 && (element_precision (TREE_TYPE (@0))
1918 <= element_precision (TREE_TYPE (@1))
1919 || !TYPE_UNSIGNED (TREE_TYPE (@1))))
1921 { tree shift_type = TREE_TYPE (@0); }
1922 (convert (rshift (convert:shift_type @1) @2)))))
1924 /* ~(~X >>r Y) -> X >>r Y
1925 ~(~X <<r Y) -> X <<r Y */
1926 (for rotate (lrotate rrotate)
1928 (bit_not (convert1?:s (rotate:s (convert2?@0 (bit_not @1)) @2)))
1929 (if ((element_precision (TREE_TYPE (@0))
1930 <= element_precision (TREE_TYPE (@1))
1931 || !TYPE_UNSIGNED (TREE_TYPE (@1)))
1932 && (element_precision (type) <= element_precision (TREE_TYPE (@0))
1933 || !TYPE_UNSIGNED (TREE_TYPE (@0))))
1935 { tree rotate_type = TREE_TYPE (@0); }
1936 (convert (rotate (convert:rotate_type @1) @2))))))
1938 /* Simplifications of conversions. */
1940 /* Basic strip-useless-type-conversions / strip_nops. */
1941 (for cvt (convert view_convert float fix_trunc)
1944 (if ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@0)))
1945 || (GENERIC && type == TREE_TYPE (@0)))
1948 /* Contract view-conversions. */
1950 (view_convert (view_convert @0))
1953 /* For integral conversions with the same precision or pointer
1954 conversions use a NOP_EXPR instead. */
1957 (if ((INTEGRAL_TYPE_P (type) || POINTER_TYPE_P (type))
1958 && (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
1959 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (@0)))
1962 /* Strip inner integral conversions that do not change precision or size, or
1963 zero-extend while keeping the same size (for bool-to-char). */
1965 (view_convert (convert@0 @1))
1966 (if ((INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
1967 && (INTEGRAL_TYPE_P (TREE_TYPE (@1)) || POINTER_TYPE_P (TREE_TYPE (@1)))
1968 && TYPE_SIZE (TREE_TYPE (@0)) == TYPE_SIZE (TREE_TYPE (@1))
1969 && (TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1))
1970 || (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (TREE_TYPE (@1))
1971 && TYPE_UNSIGNED (TREE_TYPE (@1)))))
1974 /* Re-association barriers around constants and other re-association
1975 barriers can be removed. */
1977 (paren CONSTANT_CLASS_P@0)
1980 (paren (paren@1 @0))
1983 /* Handle cases of two conversions in a row. */
1984 (for ocvt (convert float fix_trunc)
1985 (for icvt (convert float)
1990 tree inside_type = TREE_TYPE (@0);
1991 tree inter_type = TREE_TYPE (@1);
1992 int inside_int = INTEGRAL_TYPE_P (inside_type);
1993 int inside_ptr = POINTER_TYPE_P (inside_type);
1994 int inside_float = FLOAT_TYPE_P (inside_type);
1995 int inside_vec = VECTOR_TYPE_P (inside_type);
1996 unsigned int inside_prec = TYPE_PRECISION (inside_type);
1997 int inside_unsignedp = TYPE_UNSIGNED (inside_type);
1998 int inter_int = INTEGRAL_TYPE_P (inter_type);
1999 int inter_ptr = POINTER_TYPE_P (inter_type);
2000 int inter_float = FLOAT_TYPE_P (inter_type);
2001 int inter_vec = VECTOR_TYPE_P (inter_type);
2002 unsigned int inter_prec = TYPE_PRECISION (inter_type);
2003 int inter_unsignedp = TYPE_UNSIGNED (inter_type);
2004 int final_int = INTEGRAL_TYPE_P (type);
2005 int final_ptr = POINTER_TYPE_P (type);
2006 int final_float = FLOAT_TYPE_P (type);
2007 int final_vec = VECTOR_TYPE_P (type);
2008 unsigned int final_prec = TYPE_PRECISION (type);
2009 int final_unsignedp = TYPE_UNSIGNED (type);
2012 /* In addition to the cases of two conversions in a row
2013 handled below, if we are converting something to its own
2014 type via an object of identical or wider precision, neither
2015 conversion is needed. */
2016 (if (((GIMPLE && useless_type_conversion_p (type, inside_type))
2018 && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (inside_type)))
2019 && (((inter_int || inter_ptr) && final_int)
2020 || (inter_float && final_float))
2021 && inter_prec >= final_prec)
2024 /* Likewise, if the intermediate and initial types are either both
2025 float or both integer, we don't need the middle conversion if the
2026 former is wider than the latter and doesn't change the signedness
2027 (for integers). Avoid this if the final type is a pointer since
2028 then we sometimes need the middle conversion. */
2029 (if (((inter_int && inside_int) || (inter_float && inside_float))
2030 && (final_int || final_float)
2031 && inter_prec >= inside_prec
2032 && (inter_float || inter_unsignedp == inside_unsignedp))
2035 /* If we have a sign-extension of a zero-extended value, we can
2036 replace that by a single zero-extension. Likewise if the
2037 final conversion does not change precision we can drop the
2038 intermediate conversion. */
2039 (if (inside_int && inter_int && final_int
2040 && ((inside_prec < inter_prec && inter_prec < final_prec
2041 && inside_unsignedp && !inter_unsignedp)
2042 || final_prec == inter_prec))
2045 /* Two conversions in a row are not needed unless:
2046 - some conversion is floating-point (overstrict for now), or
2047 - some conversion is a vector (overstrict for now), or
2048 - the intermediate type is narrower than both initial and
2050 - the intermediate type and innermost type differ in signedness,
2051 and the outermost type is wider than the intermediate, or
2052 - the initial type is a pointer type and the precisions of the
2053 intermediate and final types differ, or
2054 - the final type is a pointer type and the precisions of the
2055 initial and intermediate types differ. */
2056 (if (! inside_float && ! inter_float && ! final_float
2057 && ! inside_vec && ! inter_vec && ! final_vec
2058 && (inter_prec >= inside_prec || inter_prec >= final_prec)
2059 && ! (inside_int && inter_int
2060 && inter_unsignedp != inside_unsignedp
2061 && inter_prec < final_prec)
2062 && ((inter_unsignedp && inter_prec > inside_prec)
2063 == (final_unsignedp && final_prec > inter_prec))
2064 && ! (inside_ptr && inter_prec != final_prec)
2065 && ! (final_ptr && inside_prec != inter_prec))
2068 /* A truncation to an unsigned type (a zero-extension) should be
2069 canonicalized as bitwise and of a mask. */
2070 (if (GIMPLE /* PR70366: doing this in GENERIC breaks -Wconversion. */
2071 && final_int && inter_int && inside_int
2072 && final_prec == inside_prec
2073 && final_prec > inter_prec
2075 (convert (bit_and @0 { wide_int_to_tree
2077 wi::mask (inter_prec, false,
2078 TYPE_PRECISION (inside_type))); })))
2080 /* If we are converting an integer to a floating-point that can
2081 represent it exactly and back to an integer, we can skip the
2082 floating-point conversion. */
2083 (if (GIMPLE /* PR66211 */
2084 && inside_int && inter_float && final_int &&
2085 (unsigned) significand_size (TYPE_MODE (inter_type))
2086 >= inside_prec - !inside_unsignedp)
2089 /* If we have a narrowing conversion to an integral type that is fed by a
2090 BIT_AND_EXPR, we might be able to remove the BIT_AND_EXPR if it merely
2091 masks off bits outside the final type (and nothing else). */
2093 (convert (bit_and @0 INTEGER_CST@1))
2094 (if (INTEGRAL_TYPE_P (type)
2095 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
2096 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))
2097 && operand_equal_p (@1, build_low_bits_mask (TREE_TYPE (@1),
2098 TYPE_PRECISION (type)), 0))
2102 /* (X /[ex] A) * A -> X. */
2104 (mult (convert1? (exact_div @0 @@1)) (convert2? @1))
2107 /* Canonicalization of binary operations. */
2109 /* Convert X + -C into X - C. */
2111 (plus @0 REAL_CST@1)
2112 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
2113 (with { tree tem = const_unop (NEGATE_EXPR, type, @1); }
2114 (if (!TREE_OVERFLOW (tem) || !flag_trapping_math)
2115 (minus @0 { tem; })))))
2117 /* Convert x+x into x*2. */
2120 (if (SCALAR_FLOAT_TYPE_P (type))
2121 (mult @0 { build_real (type, dconst2); })
2122 (if (INTEGRAL_TYPE_P (type))
2123 (mult @0 { build_int_cst (type, 2); }))))
2126 (minus integer_zerop @1)
2129 /* (ARG0 - ARG1) is the same as (-ARG1 + ARG0). So check whether
2130 ARG0 is zero and X + ARG0 reduces to X, since that would mean
2131 (-ARG1 + ARG0) reduces to -ARG1. */
2133 (minus real_zerop@0 @1)
2134 (if (fold_real_zero_addition_p (type, @0, 0))
2137 /* Transform x * -1 into -x. */
2139 (mult @0 integer_minus_onep)
2142 /* True if we can easily extract the real and imaginary parts of a complex
2144 (match compositional_complex
2145 (convert? (complex @0 @1)))
2147 /* COMPLEX_EXPR and REALPART/IMAGPART_EXPR cancellations. */
2149 (complex (realpart @0) (imagpart @0))
2152 (realpart (complex @0 @1))
2155 (imagpart (complex @0 @1))
2158 /* Sometimes we only care about half of a complex expression. */
2160 (realpart (convert?:s (conj:s @0)))
2161 (convert (realpart @0)))
2163 (imagpart (convert?:s (conj:s @0)))
2164 (convert (negate (imagpart @0))))
2165 (for part (realpart imagpart)
2166 (for op (plus minus)
2168 (part (convert?:s@2 (op:s @0 @1)))
2169 (convert (op (part @0) (part @1))))))
2171 (realpart (convert?:s (CEXPI:s @0)))
2174 (imagpart (convert?:s (CEXPI:s @0)))
2177 /* conj(conj(x)) -> x */
2179 (conj (convert? (conj @0)))
2180 (if (tree_nop_conversion_p (TREE_TYPE (@0), type))
2183 /* conj({x,y}) -> {x,-y} */
2185 (conj (convert?:s (complex:s @0 @1)))
2186 (with { tree itype = TREE_TYPE (type); }
2187 (complex (convert:itype @0) (negate (convert:itype @1)))))
2189 /* BSWAP simplifications, transforms checked by gcc.dg/builtin-bswap-8.c. */
2190 (for bswap (BUILT_IN_BSWAP16 BUILT_IN_BSWAP32 BUILT_IN_BSWAP64)
2195 (bswap (bit_not (bswap @0)))
2197 (for bitop (bit_xor bit_ior bit_and)
2199 (bswap (bitop:c (bswap @0) @1))
2200 (bitop @0 (bswap @1)))))
2203 /* Combine COND_EXPRs and VEC_COND_EXPRs. */
2205 /* Simplify constant conditions.
2206 Only optimize constant conditions when the selected branch
2207 has the same type as the COND_EXPR. This avoids optimizing
2208 away "c ? x : throw", where the throw has a void type.
2209 Note that we cannot throw away the fold-const.c variant nor
2210 this one as we depend on doing this transform before possibly
2211 A ? B : B -> B triggers and the fold-const.c one can optimize
2212 0 ? A : B to B even if A has side-effects. Something
2213 genmatch cannot handle. */
2215 (cond INTEGER_CST@0 @1 @2)
2216 (if (integer_zerop (@0))
2217 (if (!VOID_TYPE_P (TREE_TYPE (@2)) || VOID_TYPE_P (type))
2219 (if (!VOID_TYPE_P (TREE_TYPE (@1)) || VOID_TYPE_P (type))
2222 (vec_cond VECTOR_CST@0 @1 @2)
2223 (if (integer_all_onesp (@0))
2225 (if (integer_zerop (@0))
2228 /* Simplification moved from fold_cond_expr_with_comparison. It may also
2230 /* This pattern implements two kinds simplification:
2233 (cond (cmp (convert1? x) c1) (convert2? x) c2) -> (minmax (x c)) if:
2234 1) Conversions are type widening from smaller type.
2235 2) Const c1 equals to c2 after canonicalizing comparison.
2236 3) Comparison has tree code LT, LE, GT or GE.
2237 This specific pattern is needed when (cmp (convert x) c) may not
2238 be simplified by comparison patterns because of multiple uses of
2239 x. It also makes sense here because simplifying across multiple
2240 referred var is always benefitial for complicated cases.
2243 (cond (eq (convert1? x) c1) (convert2? x) c2) -> (cond (eq x c1) c1 c2). */
2244 (for cmp (lt le gt ge eq)
2246 (cond (cmp (convert1? @1) INTEGER_CST@3) (convert2? @1) INTEGER_CST@2)
2249 tree from_type = TREE_TYPE (@1);
2250 tree c1_type = TREE_TYPE (@3), c2_type = TREE_TYPE (@2);
2251 enum tree_code code = ERROR_MARK;
2253 if (INTEGRAL_TYPE_P (from_type)
2254 && int_fits_type_p (@2, from_type)
2255 && (types_match (c1_type, from_type)
2256 || (TYPE_PRECISION (c1_type) > TYPE_PRECISION (from_type)
2257 && (TYPE_UNSIGNED (from_type)
2258 || TYPE_SIGN (c1_type) == TYPE_SIGN (from_type))))
2259 && (types_match (c2_type, from_type)
2260 || (TYPE_PRECISION (c2_type) > TYPE_PRECISION (from_type)
2261 && (TYPE_UNSIGNED (from_type)
2262 || TYPE_SIGN (c2_type) == TYPE_SIGN (from_type)))))
2266 if (wi::to_widest (@3) == (wi::to_widest (@2) - 1))
2268 /* X <= Y - 1 equals to X < Y. */
2271 /* X > Y - 1 equals to X >= Y. */
2275 if (wi::to_widest (@3) == (wi::to_widest (@2) + 1))
2277 /* X < Y + 1 equals to X <= Y. */
2280 /* X >= Y + 1 equals to X > Y. */
2284 if (code != ERROR_MARK
2285 || wi::to_widest (@2) == wi::to_widest (@3))
2287 if (cmp == LT_EXPR || cmp == LE_EXPR)
2289 if (cmp == GT_EXPR || cmp == GE_EXPR)
2293 /* Can do A == C1 ? A : C2 -> A == C1 ? C1 : C2? */
2294 else if (int_fits_type_p (@3, from_type))
2298 (if (code == MAX_EXPR)
2299 (convert (max @1 (convert @2)))
2300 (if (code == MIN_EXPR)
2301 (convert (min @1 (convert @2)))
2302 (if (code == EQ_EXPR)
2303 (convert (cond (eq @1 (convert @3))
2304 (convert:from_type @3) (convert:from_type @2)))))))))
2306 /* (cond (cmp (convert? x) c1) (op x c2) c3) -> (op (minmax x c1) c2) if:
2308 1) OP is PLUS or MINUS.
2309 2) CMP is LT, LE, GT or GE.
2310 3) C3 == (C1 op C2), and computation doesn't have undefined behavior.
2312 This pattern also handles special cases like:
2314 A) Operand x is a unsigned to signed type conversion and c1 is
2315 integer zero. In this case,
2316 (signed type)x < 0 <=> x > MAX_VAL(signed type)
2317 (signed type)x >= 0 <=> x <= MAX_VAL(signed type)
2318 B) Const c1 may not equal to (C3 op' C2). In this case we also
2319 check equality for (c1+1) and (c1-1) by adjusting comparison
2322 TODO: Though signed type is handled by this pattern, it cannot be
2323 simplified at the moment because C standard requires additional
2324 type promotion. In order to match&simplify it here, the IR needs
2325 to be cleaned up by other optimizers, i.e, VRP. */
2326 (for op (plus minus)
2327 (for cmp (lt le gt ge)
2329 (cond (cmp (convert? @X) INTEGER_CST@1) (op @X INTEGER_CST@2) INTEGER_CST@3)
2330 (with { tree from_type = TREE_TYPE (@X), to_type = TREE_TYPE (@1); }
2331 (if (types_match (from_type, to_type)
2332 /* Check if it is special case A). */
2333 || (TYPE_UNSIGNED (from_type)
2334 && !TYPE_UNSIGNED (to_type)
2335 && TYPE_PRECISION (from_type) == TYPE_PRECISION (to_type)
2336 && integer_zerop (@1)
2337 && (cmp == LT_EXPR || cmp == GE_EXPR)))
2340 bool overflow = false;
2341 enum tree_code code, cmp_code = cmp;
2342 wide_int real_c1, c1 = @1, c2 = @2, c3 = @3;
2343 signop sgn = TYPE_SIGN (from_type);
2345 /* Handle special case A), given x of unsigned type:
2346 ((signed type)x < 0) <=> (x > MAX_VAL(signed type))
2347 ((signed type)x >= 0) <=> (x <= MAX_VAL(signed type)) */
2348 if (!types_match (from_type, to_type))
2350 if (cmp_code == LT_EXPR)
2352 if (cmp_code == GE_EXPR)
2354 c1 = wi::max_value (to_type);
2356 /* To simplify this pattern, we require c3 = (c1 op c2). Here we
2357 compute (c3 op' c2) and check if it equals to c1 with op' being
2358 the inverted operator of op. Make sure overflow doesn't happen
2359 if it is undefined. */
2360 if (op == PLUS_EXPR)
2361 real_c1 = wi::sub (c3, c2, sgn, &overflow);
2363 real_c1 = wi::add (c3, c2, sgn, &overflow);
2366 if (!overflow || !TYPE_OVERFLOW_UNDEFINED (from_type))
2368 /* Check if c1 equals to real_c1. Boundary condition is handled
2369 by adjusting comparison operation if necessary. */
2370 if (!wi::cmp (wi::sub (real_c1, 1, sgn, &overflow), c1, sgn)
2373 /* X <= Y - 1 equals to X < Y. */
2374 if (cmp_code == LE_EXPR)
2376 /* X > Y - 1 equals to X >= Y. */
2377 if (cmp_code == GT_EXPR)
2380 if (!wi::cmp (wi::add (real_c1, 1, sgn, &overflow), c1, sgn)
2383 /* X < Y + 1 equals to X <= Y. */
2384 if (cmp_code == LT_EXPR)
2386 /* X >= Y + 1 equals to X > Y. */
2387 if (cmp_code == GE_EXPR)
2390 if (code != cmp_code || !wi::cmp (real_c1, c1, sgn))
2392 if (cmp_code == LT_EXPR || cmp_code == LE_EXPR)
2394 if (cmp_code == GT_EXPR || cmp_code == GE_EXPR)
2399 (if (code == MAX_EXPR)
2400 (op (max @X { wide_int_to_tree (from_type, real_c1); })
2401 { wide_int_to_tree (from_type, c2); })
2402 (if (code == MIN_EXPR)
2403 (op (min @X { wide_int_to_tree (from_type, real_c1); })
2404 { wide_int_to_tree (from_type, c2); })))))))))
2406 (for cnd (cond vec_cond)
2407 /* A ? B : (A ? X : C) -> A ? B : C. */
2409 (cnd @0 (cnd @0 @1 @2) @3)
2412 (cnd @0 @1 (cnd @0 @2 @3))
2414 /* A ? B : (!A ? C : X) -> A ? B : C. */
2415 /* ??? This matches embedded conditions open-coded because genmatch
2416 would generate matching code for conditions in separate stmts only.
2417 The following is still important to merge then and else arm cases
2418 from if-conversion. */
2420 (cnd @0 @1 (cnd @2 @3 @4))
2421 (if (COMPARISON_CLASS_P (@0)
2422 && COMPARISON_CLASS_P (@2)
2423 && invert_tree_comparison
2424 (TREE_CODE (@0), HONOR_NANS (TREE_OPERAND (@0, 0))) == TREE_CODE (@2)
2425 && operand_equal_p (TREE_OPERAND (@0, 0), TREE_OPERAND (@2, 0), 0)
2426 && operand_equal_p (TREE_OPERAND (@0, 1), TREE_OPERAND (@2, 1), 0))
2429 (cnd @0 (cnd @1 @2 @3) @4)
2430 (if (COMPARISON_CLASS_P (@0)
2431 && COMPARISON_CLASS_P (@1)
2432 && invert_tree_comparison
2433 (TREE_CODE (@0), HONOR_NANS (TREE_OPERAND (@0, 0))) == TREE_CODE (@1)
2434 && operand_equal_p (TREE_OPERAND (@0, 0), TREE_OPERAND (@1, 0), 0)
2435 && operand_equal_p (TREE_OPERAND (@0, 1), TREE_OPERAND (@1, 1), 0))
2438 /* A ? B : B -> B. */
2443 /* !A ? B : C -> A ? C : B. */
2445 (cnd (logical_inverted_value truth_valued_p@0) @1 @2)
2448 /* A + (B vcmp C ? 1 : 0) -> A - (B vcmp C ? -1 : 0), since vector comparisons
2449 return all -1 or all 0 results. */
2450 /* ??? We could instead convert all instances of the vec_cond to negate,
2451 but that isn't necessarily a win on its own. */
2453 (plus:c @3 (view_convert? (vec_cond:s @0 integer_each_onep@1 integer_zerop@2)))
2454 (if (VECTOR_TYPE_P (type)
2455 && TYPE_VECTOR_SUBPARTS (type) == TYPE_VECTOR_SUBPARTS (TREE_TYPE (@1))
2456 && (TYPE_MODE (TREE_TYPE (type))
2457 == TYPE_MODE (TREE_TYPE (TREE_TYPE (@1)))))
2458 (minus @3 (view_convert (vec_cond @0 (negate @1) @2)))))
2460 /* ... likewise A - (B vcmp C ? 1 : 0) -> A + (B vcmp C ? -1 : 0). */
2462 (minus @3 (view_convert? (vec_cond:s @0 integer_each_onep@1 integer_zerop@2)))
2463 (if (VECTOR_TYPE_P (type)
2464 && TYPE_VECTOR_SUBPARTS (type) == TYPE_VECTOR_SUBPARTS (TREE_TYPE (@1))
2465 && (TYPE_MODE (TREE_TYPE (type))
2466 == TYPE_MODE (TREE_TYPE (TREE_TYPE (@1)))))
2467 (plus @3 (view_convert (vec_cond @0 (negate @1) @2)))))
2470 /* Simplifications of comparisons. */
2472 /* See if we can reduce the magnitude of a constant involved in a
2473 comparison by changing the comparison code. This is a canonicalization
2474 formerly done by maybe_canonicalize_comparison_1. */
2478 (cmp @0 INTEGER_CST@1)
2479 (if (tree_int_cst_sgn (@1) == -1)
2480 (acmp @0 { wide_int_to_tree (TREE_TYPE (@1), wi::add (@1, 1)); }))))
2484 (cmp @0 INTEGER_CST@1)
2485 (if (tree_int_cst_sgn (@1) == 1)
2486 (acmp @0 { wide_int_to_tree (TREE_TYPE (@1), wi::sub (@1, 1)); }))))
2489 /* We can simplify a logical negation of a comparison to the
2490 inverted comparison. As we cannot compute an expression
2491 operator using invert_tree_comparison we have to simulate
2492 that with expression code iteration. */
2493 (for cmp (tcc_comparison)
2494 icmp (inverted_tcc_comparison)
2495 ncmp (inverted_tcc_comparison_with_nans)
2496 /* Ideally we'd like to combine the following two patterns
2497 and handle some more cases by using
2498 (logical_inverted_value (cmp @0 @1))
2499 here but for that genmatch would need to "inline" that.
2500 For now implement what forward_propagate_comparison did. */
2502 (bit_not (cmp @0 @1))
2503 (if (VECTOR_TYPE_P (type)
2504 || (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1))
2505 /* Comparison inversion may be impossible for trapping math,
2506 invert_tree_comparison will tell us. But we can't use
2507 a computed operator in the replacement tree thus we have
2508 to play the trick below. */
2509 (with { enum tree_code ic = invert_tree_comparison
2510 (cmp, HONOR_NANS (@0)); }
2516 (bit_xor (cmp @0 @1) integer_truep)
2517 (with { enum tree_code ic = invert_tree_comparison
2518 (cmp, HONOR_NANS (@0)); }
2524 /* Transform comparisons of the form X - Y CMP 0 to X CMP Y.
2525 ??? The transformation is valid for the other operators if overflow
2526 is undefined for the type, but performing it here badly interacts
2527 with the transformation in fold_cond_expr_with_comparison which
2528 attempts to synthetize ABS_EXPR. */
2531 (cmp (minus@2 @0 @1) integer_zerop)
2532 (if (single_use (@2))
2535 /* Transform comparisons of the form X * C1 CMP 0 to X CMP 0 in the
2536 signed arithmetic case. That form is created by the compiler
2537 often enough for folding it to be of value. One example is in
2538 computing loop trip counts after Operator Strength Reduction. */
2539 (for cmp (simple_comparison)
2540 scmp (swapped_simple_comparison)
2542 (cmp (mult@3 @0 INTEGER_CST@1) integer_zerop@2)
2543 /* Handle unfolded multiplication by zero. */
2544 (if (integer_zerop (@1))
2546 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2547 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2549 /* If @1 is negative we swap the sense of the comparison. */
2550 (if (tree_int_cst_sgn (@1) < 0)
2554 /* Simplify comparison of something with itself. For IEEE
2555 floating-point, we can only do some of these simplifications. */
2559 (if (! FLOAT_TYPE_P (TREE_TYPE (@0))
2560 || ! HONOR_NANS (@0))
2561 { constant_boolean_node (true, type); }
2562 (if (cmp != EQ_EXPR)
2568 || ! FLOAT_TYPE_P (TREE_TYPE (@0))
2569 || ! HONOR_NANS (@0))
2570 { constant_boolean_node (false, type); })))
2571 (for cmp (unle unge uneq)
2574 { constant_boolean_node (true, type); }))
2575 (for cmp (unlt ungt)
2581 (if (!flag_trapping_math)
2582 { constant_boolean_node (false, type); }))
2584 /* Fold ~X op ~Y as Y op X. */
2585 (for cmp (simple_comparison)
2587 (cmp (bit_not@2 @0) (bit_not@3 @1))
2588 (if (single_use (@2) && single_use (@3))
2591 /* Fold ~X op C as X op' ~C, where op' is the swapped comparison. */
2592 (for cmp (simple_comparison)
2593 scmp (swapped_simple_comparison)
2595 (cmp (bit_not@2 @0) CONSTANT_CLASS_P@1)
2596 (if (single_use (@2)
2597 && (TREE_CODE (@1) == INTEGER_CST || TREE_CODE (@1) == VECTOR_CST))
2598 (scmp @0 (bit_not @1)))))
2600 (for cmp (simple_comparison)
2601 /* Fold (double)float1 CMP (double)float2 into float1 CMP float2. */
2603 (cmp (convert@2 @0) (convert? @1))
2604 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
2605 && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@2))
2606 == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0)))
2607 && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@2))
2608 == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1))))
2611 tree type1 = TREE_TYPE (@1);
2612 if (TREE_CODE (@1) == REAL_CST && !DECIMAL_FLOAT_TYPE_P (type1))
2614 REAL_VALUE_TYPE orig = TREE_REAL_CST (@1);
2615 if (TYPE_PRECISION (type1) > TYPE_PRECISION (float_type_node)
2616 && exact_real_truncate (TYPE_MODE (float_type_node), &orig))
2617 type1 = float_type_node;
2618 if (TYPE_PRECISION (type1) > TYPE_PRECISION (double_type_node)
2619 && exact_real_truncate (TYPE_MODE (double_type_node), &orig))
2620 type1 = double_type_node;
2623 = (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type1)
2624 ? TREE_TYPE (@0) : type1);
2626 (if (TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (newtype))
2627 (cmp (convert:newtype @0) (convert:newtype @1))))))
2631 /* IEEE doesn't distinguish +0 and -0 in comparisons. */
2633 /* a CMP (-0) -> a CMP 0 */
2634 (if (REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@1)))
2635 (cmp @0 { build_real (TREE_TYPE (@1), dconst0); }))
2636 /* x != NaN is always true, other ops are always false. */
2637 (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
2638 && ! HONOR_SNANS (@1))
2639 { constant_boolean_node (cmp == NE_EXPR, type); })
2640 /* Fold comparisons against infinity. */
2641 (if (REAL_VALUE_ISINF (TREE_REAL_CST (@1))
2642 && MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (@1))))
2645 REAL_VALUE_TYPE max;
2646 enum tree_code code = cmp;
2647 bool neg = REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1));
2649 code = swap_tree_comparison (code);
2652 /* x > +Inf is always false, if with ignore sNANs. */
2653 (if (code == GT_EXPR
2654 && ! HONOR_SNANS (@0))
2655 { constant_boolean_node (false, type); })
2656 (if (code == LE_EXPR)
2657 /* x <= +Inf is always true, if we don't case about NaNs. */
2658 (if (! HONOR_NANS (@0))
2659 { constant_boolean_node (true, type); }
2660 /* x <= +Inf is the same as x == x, i.e. !isnan(x). */
2662 /* x == +Inf and x >= +Inf are always equal to x > DBL_MAX. */
2663 (if (code == EQ_EXPR || code == GE_EXPR)
2664 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
2666 (lt @0 { build_real (TREE_TYPE (@0), max); })
2667 (gt @0 { build_real (TREE_TYPE (@0), max); }))))
2668 /* x < +Inf is always equal to x <= DBL_MAX. */
2669 (if (code == LT_EXPR)
2670 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
2672 (ge @0 { build_real (TREE_TYPE (@0), max); })
2673 (le @0 { build_real (TREE_TYPE (@0), max); }))))
2674 /* x != +Inf is always equal to !(x > DBL_MAX). */
2675 (if (code == NE_EXPR)
2676 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
2677 (if (! HONOR_NANS (@0))
2679 (ge @0 { build_real (TREE_TYPE (@0), max); })
2680 (le @0 { build_real (TREE_TYPE (@0), max); }))
2682 (bit_xor (lt @0 { build_real (TREE_TYPE (@0), max); })
2683 { build_one_cst (type); })
2684 (bit_xor (gt @0 { build_real (TREE_TYPE (@0), max); })
2685 { build_one_cst (type); }))))))))))
2687 /* If this is a comparison of a real constant with a PLUS_EXPR
2688 or a MINUS_EXPR of a real constant, we can convert it into a
2689 comparison with a revised real constant as long as no overflow
2690 occurs when unsafe_math_optimizations are enabled. */
2691 (if (flag_unsafe_math_optimizations)
2692 (for op (plus minus)
2694 (cmp (op @0 REAL_CST@1) REAL_CST@2)
2697 tree tem = const_binop (op == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR,
2698 TREE_TYPE (@1), @2, @1);
2700 (if (tem && !TREE_OVERFLOW (tem))
2701 (cmp @0 { tem; }))))))
2703 /* Likewise, we can simplify a comparison of a real constant with
2704 a MINUS_EXPR whose first operand is also a real constant, i.e.
2705 (c1 - x) < c2 becomes x > c1-c2. Reordering is allowed on
2706 floating-point types only if -fassociative-math is set. */
2707 (if (flag_associative_math)
2709 (cmp (minus REAL_CST@0 @1) REAL_CST@2)
2710 (with { tree tem = const_binop (MINUS_EXPR, TREE_TYPE (@1), @0, @2); }
2711 (if (tem && !TREE_OVERFLOW (tem))
2712 (cmp { tem; } @1)))))
2714 /* Fold comparisons against built-in math functions. */
2715 (if (flag_unsafe_math_optimizations
2716 && ! flag_errno_math)
2719 (cmp (sq @0) REAL_CST@1)
2721 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
2723 /* sqrt(x) < y is always false, if y is negative. */
2724 (if (cmp == EQ_EXPR || cmp == LT_EXPR || cmp == LE_EXPR)
2725 { constant_boolean_node (false, type); })
2726 /* sqrt(x) > y is always true, if y is negative and we
2727 don't care about NaNs, i.e. negative values of x. */
2728 (if (cmp == NE_EXPR || !HONOR_NANS (@0))
2729 { constant_boolean_node (true, type); })
2730 /* sqrt(x) > y is the same as x >= 0, if y is negative. */
2731 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })))
2732 (if (real_equal (TREE_REAL_CST_PTR (@1), &dconst0))
2734 /* sqrt(x) < 0 is always false. */
2735 (if (cmp == LT_EXPR)
2736 { constant_boolean_node (false, type); })
2737 /* sqrt(x) >= 0 is always true if we don't care about NaNs. */
2738 (if (cmp == GE_EXPR && !HONOR_NANS (@0))
2739 { constant_boolean_node (true, type); })
2740 /* sqrt(x) <= 0 -> x == 0. */
2741 (if (cmp == LE_EXPR)
2743 /* Otherwise sqrt(x) cmp 0 -> x cmp 0. Here cmp can be >=, >,
2744 == or !=. In the last case:
2746 (sqrt(x) != 0) == (NaN != 0) == true == (x != 0)
2748 if x is negative or NaN. Due to -funsafe-math-optimizations,
2749 the results for other x follow from natural arithmetic. */
2751 (if (cmp == GT_EXPR || cmp == GE_EXPR)
2755 real_arithmetic (&c2, MULT_EXPR,
2756 &TREE_REAL_CST (@1), &TREE_REAL_CST (@1));
2757 real_convert (&c2, TYPE_MODE (TREE_TYPE (@0)), &c2);
2759 (if (REAL_VALUE_ISINF (c2))
2760 /* sqrt(x) > y is x == +Inf, when y is very large. */
2761 (if (HONOR_INFINITIES (@0))
2762 (eq @0 { build_real (TREE_TYPE (@0), c2); })
2763 { constant_boolean_node (false, type); })
2764 /* sqrt(x) > c is the same as x > c*c. */
2765 (cmp @0 { build_real (TREE_TYPE (@0), c2); }))))
2766 (if (cmp == LT_EXPR || cmp == LE_EXPR)
2770 real_arithmetic (&c2, MULT_EXPR,
2771 &TREE_REAL_CST (@1), &TREE_REAL_CST (@1));
2772 real_convert (&c2, TYPE_MODE (TREE_TYPE (@0)), &c2);
2774 (if (REAL_VALUE_ISINF (c2))
2776 /* sqrt(x) < y is always true, when y is a very large
2777 value and we don't care about NaNs or Infinities. */
2778 (if (! HONOR_NANS (@0) && ! HONOR_INFINITIES (@0))
2779 { constant_boolean_node (true, type); })
2780 /* sqrt(x) < y is x != +Inf when y is very large and we
2781 don't care about NaNs. */
2782 (if (! HONOR_NANS (@0))
2783 (ne @0 { build_real (TREE_TYPE (@0), c2); }))
2784 /* sqrt(x) < y is x >= 0 when y is very large and we
2785 don't care about Infinities. */
2786 (if (! HONOR_INFINITIES (@0))
2787 (ge @0 { build_real (TREE_TYPE (@0), dconst0); }))
2788 /* sqrt(x) < y is x >= 0 && x != +Inf, when y is large. */
2791 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
2792 (ne @0 { build_real (TREE_TYPE (@0), c2); }))))
2793 /* sqrt(x) < c is the same as x < c*c, if we ignore NaNs. */
2794 (if (! HONOR_NANS (@0))
2795 (cmp @0 { build_real (TREE_TYPE (@0), c2); })
2796 /* sqrt(x) < c is the same as x >= 0 && x < c*c. */
2799 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
2800 (cmp @0 { build_real (TREE_TYPE (@0), c2); })))))))))
2801 /* Transform sqrt(x) cmp sqrt(y) -> x cmp y. */
2803 (cmp (sq @0) (sq @1))
2804 (if (! HONOR_NANS (@0))
2807 /* Fold A /[ex] B CMP C to A CMP B * C. */
2810 (cmp (exact_div @0 @1) INTEGER_CST@2)
2811 (if (!integer_zerop (@1))
2812 (if (wi::eq_p (@2, 0))
2814 (if (TREE_CODE (@1) == INTEGER_CST)
2818 wide_int prod = wi::mul (@2, @1, TYPE_SIGN (TREE_TYPE (@1)), &ovf);
2821 { constant_boolean_node (cmp == NE_EXPR, type); }
2822 (cmp @0 { wide_int_to_tree (TREE_TYPE (@0), prod); }))))))))
2823 (for cmp (lt le gt ge)
2825 (cmp (exact_div @0 INTEGER_CST@1) INTEGER_CST@2)
2826 (if (wi::gt_p (@1, 0, TYPE_SIGN (TREE_TYPE (@1))))
2830 wide_int prod = wi::mul (@2, @1, TYPE_SIGN (TREE_TYPE (@1)), &ovf);
2833 { constant_boolean_node (wi::lt_p (@2, 0, TYPE_SIGN (TREE_TYPE (@2)))
2834 != (cmp == LT_EXPR || cmp == LE_EXPR), type); }
2835 (cmp @0 { wide_int_to_tree (TREE_TYPE (@0), prod); }))))))
2837 /* Unordered tests if either argument is a NaN. */
2839 (bit_ior (unordered @0 @0) (unordered @1 @1))
2840 (if (types_match (@0, @1))
2843 (bit_and (ordered @0 @0) (ordered @1 @1))
2844 (if (types_match (@0, @1))
2847 (bit_ior:c (unordered @0 @0) (unordered:c@2 @0 @1))
2850 (bit_and:c (ordered @0 @0) (ordered:c@2 @0 @1))
2853 /* Simple range test simplifications. */
2854 /* A < B || A >= B -> true. */
2855 (for test1 (lt le le le ne ge)
2856 test2 (ge gt ge ne eq ne)
2858 (bit_ior:c (test1 @0 @1) (test2 @0 @1))
2859 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2860 || VECTOR_INTEGER_TYPE_P (TREE_TYPE (@0)))
2861 { constant_boolean_node (true, type); })))
2862 /* A < B && A >= B -> false. */
2863 (for test1 (lt lt lt le ne eq)
2864 test2 (ge gt eq gt eq gt)
2866 (bit_and:c (test1 @0 @1) (test2 @0 @1))
2867 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2868 || VECTOR_INTEGER_TYPE_P (TREE_TYPE (@0)))
2869 { constant_boolean_node (false, type); })))
2871 /* A & (2**N - 1) <= 2**K - 1 -> A & (2**N - 2**K) == 0
2872 A & (2**N - 1) > 2**K - 1 -> A & (2**N - 2**K) != 0
2874 Note that comparisons
2875 A & (2**N - 1) < 2**K -> A & (2**N - 2**K) == 0
2876 A & (2**N - 1) >= 2**K -> A & (2**N - 2**K) != 0
2877 will be canonicalized to above so there's no need to
2884 (cmp (bit_and@0 @1 INTEGER_CST@2) INTEGER_CST@3)
2885 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)))
2888 tree ty = TREE_TYPE (@0);
2889 unsigned prec = TYPE_PRECISION (ty);
2890 wide_int mask = wi::to_wide (@2, prec);
2891 wide_int rhs = wi::to_wide (@3, prec);
2892 signop sgn = TYPE_SIGN (ty);
2894 (if ((mask & (mask + 1)) == 0 && wi::gt_p (rhs, 0, sgn)
2895 && (rhs & (rhs + 1)) == 0 && wi::ge_p (mask, rhs, sgn))
2896 (eqcmp (bit_and @1 { wide_int_to_tree (ty, mask - rhs); })
2897 { build_zero_cst (ty); }))))))
2899 /* -A CMP -B -> B CMP A. */
2900 (for cmp (tcc_comparison)
2901 scmp (swapped_tcc_comparison)
2903 (cmp (negate @0) (negate @1))
2904 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
2905 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2906 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
2909 (cmp (negate @0) CONSTANT_CLASS_P@1)
2910 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
2911 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2912 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
2913 (with { tree tem = const_unop (NEGATE_EXPR, TREE_TYPE (@0), @1); }
2914 (if (tem && !TREE_OVERFLOW (tem))
2915 (scmp @0 { tem; }))))))
2917 /* Convert ABS_EXPR<x> == 0 or ABS_EXPR<x> != 0 to x == 0 or x != 0. */
2920 (op (abs @0) zerop@1)
2923 /* From fold_sign_changed_comparison and fold_widened_comparison. */
2924 (for cmp (simple_comparison)
2926 (cmp (convert@0 @00) (convert?@1 @10))
2927 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2928 /* Disable this optimization if we're casting a function pointer
2929 type on targets that require function pointer canonicalization. */
2930 && !(targetm.have_canonicalize_funcptr_for_compare ()
2931 && TREE_CODE (TREE_TYPE (@00)) == POINTER_TYPE
2932 && TREE_CODE (TREE_TYPE (TREE_TYPE (@00))) == FUNCTION_TYPE)
2934 (if (TYPE_PRECISION (TREE_TYPE (@00)) == TYPE_PRECISION (TREE_TYPE (@0))
2935 && (TREE_CODE (@10) == INTEGER_CST
2936 || (@1 != @10 && types_match (TREE_TYPE (@10), TREE_TYPE (@00))))
2937 && (TYPE_UNSIGNED (TREE_TYPE (@00)) == TYPE_UNSIGNED (TREE_TYPE (@0))
2940 && (POINTER_TYPE_P (TREE_TYPE (@00)) == POINTER_TYPE_P (TREE_TYPE (@0))))
2941 /* ??? The special-casing of INTEGER_CST conversion was in the original
2942 code and here to avoid a spurious overflow flag on the resulting
2943 constant which fold_convert produces. */
2944 (if (TREE_CODE (@1) == INTEGER_CST)
2945 (cmp @00 { force_fit_type (TREE_TYPE (@00), wi::to_widest (@1), 0,
2946 TREE_OVERFLOW (@1)); })
2947 (cmp @00 (convert @1)))
2949 (if (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (TREE_TYPE (@00)))
2950 /* If possible, express the comparison in the shorter mode. */
2951 (if ((cmp == EQ_EXPR || cmp == NE_EXPR
2952 || TYPE_UNSIGNED (TREE_TYPE (@0)) == TYPE_UNSIGNED (TREE_TYPE (@00))
2953 || (!TYPE_UNSIGNED (TREE_TYPE (@0))
2954 && TYPE_UNSIGNED (TREE_TYPE (@00))))
2955 && (types_match (TREE_TYPE (@10), TREE_TYPE (@00))
2956 || ((TYPE_PRECISION (TREE_TYPE (@00))
2957 >= TYPE_PRECISION (TREE_TYPE (@10)))
2958 && (TYPE_UNSIGNED (TREE_TYPE (@00))
2959 == TYPE_UNSIGNED (TREE_TYPE (@10))))
2960 || (TREE_CODE (@10) == INTEGER_CST
2961 && INTEGRAL_TYPE_P (TREE_TYPE (@00))
2962 && int_fits_type_p (@10, TREE_TYPE (@00)))))
2963 (cmp @00 (convert @10))
2964 (if (TREE_CODE (@10) == INTEGER_CST
2965 && INTEGRAL_TYPE_P (TREE_TYPE (@00))
2966 && !int_fits_type_p (@10, TREE_TYPE (@00)))
2969 tree min = lower_bound_in_type (TREE_TYPE (@10), TREE_TYPE (@00));
2970 tree max = upper_bound_in_type (TREE_TYPE (@10), TREE_TYPE (@00));
2971 bool above = integer_nonzerop (const_binop (LT_EXPR, type, max, @10));
2972 bool below = integer_nonzerop (const_binop (LT_EXPR, type, @10, min));
2974 (if (above || below)
2975 (if (cmp == EQ_EXPR || cmp == NE_EXPR)
2976 { constant_boolean_node (cmp == EQ_EXPR ? false : true, type); }
2977 (if (cmp == LT_EXPR || cmp == LE_EXPR)
2978 { constant_boolean_node (above ? true : false, type); }
2979 (if (cmp == GT_EXPR || cmp == GE_EXPR)
2980 { constant_boolean_node (above ? false : true, type); }))))))))))))
2983 /* A local variable can never be pointed to by
2984 the default SSA name of an incoming parameter.
2985 SSA names are canonicalized to 2nd place. */
2987 (cmp addr@0 SSA_NAME@1)
2988 (if (SSA_NAME_IS_DEFAULT_DEF (@1)
2989 && TREE_CODE (SSA_NAME_VAR (@1)) == PARM_DECL)
2990 (with { tree base = get_base_address (TREE_OPERAND (@0, 0)); }
2991 (if (TREE_CODE (base) == VAR_DECL
2992 && auto_var_in_fn_p (base, current_function_decl))
2993 (if (cmp == NE_EXPR)
2994 { constant_boolean_node (true, type); }
2995 { constant_boolean_node (false, type); }))))))
2997 /* Equality compare simplifications from fold_binary */
3000 /* If we have (A | C) == D where C & ~D != 0, convert this into 0.
3001 Similarly for NE_EXPR. */
3003 (cmp (convert?@3 (bit_ior @0 INTEGER_CST@1)) INTEGER_CST@2)
3004 (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0))
3005 && wi::bit_and_not (@1, @2) != 0)
3006 { constant_boolean_node (cmp == NE_EXPR, type); }))
3008 /* (X ^ Y) == 0 becomes X == Y, and (X ^ Y) != 0 becomes X != Y. */
3010 (cmp (bit_xor @0 @1) integer_zerop)
3013 /* (X ^ Y) == Y becomes X == 0.
3014 Likewise (X ^ Y) == X becomes Y == 0. */
3016 (cmp:c (bit_xor:c @0 @1) @0)
3017 (cmp @1 { build_zero_cst (TREE_TYPE (@1)); }))
3019 /* (X ^ C1) op C2 can be rewritten as X op (C1 ^ C2). */
3021 (cmp (convert?@3 (bit_xor @0 INTEGER_CST@1)) INTEGER_CST@2)
3022 (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0)))
3023 (cmp @0 (bit_xor @1 (convert @2)))))
3026 (cmp (convert? addr@0) integer_zerop)
3027 (if (tree_single_nonzero_warnv_p (@0, NULL))
3028 { constant_boolean_node (cmp == NE_EXPR, type); })))
3030 /* If we have (A & C) == C where C is a power of 2, convert this into
3031 (A & C) != 0. Similarly for NE_EXPR. */
3035 (cmp (bit_and@2 @0 integer_pow2p@1) @1)
3036 (icmp @2 { build_zero_cst (TREE_TYPE (@0)); })))
3038 /* If we have (A & C) != 0 ? D : 0 where C and D are powers of 2,
3039 convert this into a shift followed by ANDing with D. */
3042 (ne (bit_and @0 integer_pow2p@1) integer_zerop)
3043 integer_pow2p@2 integer_zerop)
3045 int shift = wi::exact_log2 (@2) - wi::exact_log2 (@1);
3049 (lshift (convert @0) { build_int_cst (integer_type_node, shift); }) @2)
3051 (convert (rshift @0 { build_int_cst (integer_type_node, -shift); })) @2))))
3053 /* If we have (A & C) != 0 where C is the sign bit of A, convert
3054 this into A < 0. Similarly for (A & C) == 0 into A >= 0. */
3058 (cmp (bit_and (convert?@2 @0) integer_pow2p@1) integer_zerop)
3059 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
3060 && (TYPE_PRECISION (TREE_TYPE (@0))
3061 == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (@0))))
3062 && element_precision (@2) >= element_precision (@0)
3063 && wi::only_sign_bit_p (@1, element_precision (@0)))
3064 (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
3065 (ncmp (convert:stype @0) { build_zero_cst (stype); })))))
3067 /* If we have A < 0 ? C : 0 where C is a power of 2, convert
3068 this into a right shift or sign extension followed by ANDing with C. */
3071 (lt @0 integer_zerop)
3072 integer_pow2p@1 integer_zerop)
3073 (if (!TYPE_UNSIGNED (TREE_TYPE (@0)))
3075 int shift = element_precision (@0) - wi::exact_log2 (@1) - 1;
3079 (convert (rshift @0 { build_int_cst (integer_type_node, shift); }))
3081 /* Otherwise ctype must be wider than TREE_TYPE (@0) and pure
3082 sign extension followed by AND with C will achieve the effect. */
3083 (bit_and (convert @0) @1)))))
3085 /* When the addresses are not directly of decls compare base and offset.
3086 This implements some remaining parts of fold_comparison address
3087 comparisons but still no complete part of it. Still it is good
3088 enough to make fold_stmt not regress when not dispatching to fold_binary. */
3089 (for cmp (simple_comparison)
3091 (cmp (convert1?@2 addr@0) (convert2? addr@1))
3094 HOST_WIDE_INT off0, off1;
3095 tree base0 = get_addr_base_and_unit_offset (TREE_OPERAND (@0, 0), &off0);
3096 tree base1 = get_addr_base_and_unit_offset (TREE_OPERAND (@1, 0), &off1);
3097 if (base0 && TREE_CODE (base0) == MEM_REF)
3099 off0 += mem_ref_offset (base0).to_short_addr ();
3100 base0 = TREE_OPERAND (base0, 0);
3102 if (base1 && TREE_CODE (base1) == MEM_REF)
3104 off1 += mem_ref_offset (base1).to_short_addr ();
3105 base1 = TREE_OPERAND (base1, 0);
3108 (if (base0 && base1)
3112 /* Punt in GENERIC on variables with value expressions;
3113 the value expressions might point to fields/elements
3114 of other vars etc. */
3116 && ((VAR_P (base0) && DECL_HAS_VALUE_EXPR_P (base0))
3117 || (VAR_P (base1) && DECL_HAS_VALUE_EXPR_P (base1))))
3119 else if (decl_in_symtab_p (base0)
3120 && decl_in_symtab_p (base1))
3121 equal = symtab_node::get_create (base0)
3122 ->equal_address_to (symtab_node::get_create (base1));
3123 else if ((DECL_P (base0)
3124 || TREE_CODE (base0) == SSA_NAME
3125 || TREE_CODE (base0) == STRING_CST)
3127 || TREE_CODE (base1) == SSA_NAME
3128 || TREE_CODE (base1) == STRING_CST))
3129 equal = (base0 == base1);
3132 && (cmp == EQ_EXPR || cmp == NE_EXPR
3133 /* If the offsets are equal we can ignore overflow. */
3135 || POINTER_TYPE_OVERFLOW_UNDEFINED
3136 /* Or if we compare using pointers to decls or strings. */
3137 || (POINTER_TYPE_P (TREE_TYPE (@2))
3138 && (DECL_P (base0) || TREE_CODE (base0) == STRING_CST))))
3140 (if (cmp == EQ_EXPR)
3141 { constant_boolean_node (off0 == off1, type); })
3142 (if (cmp == NE_EXPR)
3143 { constant_boolean_node (off0 != off1, type); })
3144 (if (cmp == LT_EXPR)
3145 { constant_boolean_node (off0 < off1, type); })
3146 (if (cmp == LE_EXPR)
3147 { constant_boolean_node (off0 <= off1, type); })
3148 (if (cmp == GE_EXPR)
3149 { constant_boolean_node (off0 >= off1, type); })
3150 (if (cmp == GT_EXPR)
3151 { constant_boolean_node (off0 > off1, type); }))
3153 && DECL_P (base0) && DECL_P (base1)
3154 /* If we compare this as integers require equal offset. */
3155 && (!INTEGRAL_TYPE_P (TREE_TYPE (@2))
3158 (if (cmp == EQ_EXPR)
3159 { constant_boolean_node (false, type); })
3160 (if (cmp == NE_EXPR)
3161 { constant_boolean_node (true, type); })))))))))
3163 /* Simplify pointer equality compares using PTA. */
3167 (if (POINTER_TYPE_P (TREE_TYPE (@0))
3168 && ptrs_compare_unequal (@0, @1))
3169 { neeq == EQ_EXPR ? boolean_false_node : boolean_true_node; })))
3171 /* PR70920: Transform (intptr_t)x eq/ne CST to x eq/ne (typeof x) CST.
3172 and (typeof ptr_cst) x eq/ne ptr_cst to x eq/ne (typeof x) CST.
3173 Disable the transform if either operand is pointer to function.
3174 This broke pr22051-2.c for arm where function pointer
3175 canonicalizaion is not wanted. */
3179 (cmp (convert @0) INTEGER_CST@1)
3180 (if ((POINTER_TYPE_P (TREE_TYPE (@0)) && !FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@0)))
3181 && INTEGRAL_TYPE_P (TREE_TYPE (@1)))
3182 || (INTEGRAL_TYPE_P (TREE_TYPE (@0)) && POINTER_TYPE_P (TREE_TYPE (@1))
3183 && !FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@1)))))
3184 (cmp @0 (convert @1)))))
3186 /* Non-equality compare simplifications from fold_binary */
3187 (for cmp (lt gt le ge)
3188 /* Comparisons with the highest or lowest possible integer of
3189 the specified precision will have known values. */
3191 (cmp (convert?@2 @0) INTEGER_CST@1)
3192 (if ((INTEGRAL_TYPE_P (TREE_TYPE (@1)) || POINTER_TYPE_P (TREE_TYPE (@1)))
3193 && tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@0)))
3196 tree arg1_type = TREE_TYPE (@1);
3197 unsigned int prec = TYPE_PRECISION (arg1_type);
3198 wide_int max = wi::max_value (arg1_type);
3199 wide_int signed_max = wi::max_value (prec, SIGNED);
3200 wide_int min = wi::min_value (arg1_type);
3203 (if (wi::eq_p (@1, max))
3205 (if (cmp == GT_EXPR)
3206 { constant_boolean_node (false, type); })
3207 (if (cmp == GE_EXPR)
3209 (if (cmp == LE_EXPR)
3210 { constant_boolean_node (true, type); })
3211 (if (cmp == LT_EXPR)
3213 (if (wi::eq_p (@1, min))
3215 (if (cmp == LT_EXPR)
3216 { constant_boolean_node (false, type); })
3217 (if (cmp == LE_EXPR)
3219 (if (cmp == GE_EXPR)
3220 { constant_boolean_node (true, type); })
3221 (if (cmp == GT_EXPR)
3223 (if (wi::eq_p (@1, max - 1))
3225 (if (cmp == GT_EXPR)
3226 (eq @2 { wide_int_to_tree (TREE_TYPE (@1), wi::add (@1, 1)); }))
3227 (if (cmp == LE_EXPR)
3228 (ne @2 { wide_int_to_tree (TREE_TYPE (@1), wi::add (@1, 1)); }))))
3229 (if (wi::eq_p (@1, min + 1))
3231 (if (cmp == GE_EXPR)
3232 (ne @2 { wide_int_to_tree (TREE_TYPE (@1), wi::sub (@1, 1)); }))
3233 (if (cmp == LT_EXPR)
3234 (eq @2 { wide_int_to_tree (TREE_TYPE (@1), wi::sub (@1, 1)); }))))
3235 (if (wi::eq_p (@1, signed_max)
3236 && TYPE_UNSIGNED (arg1_type)
3237 /* We will flip the signedness of the comparison operator
3238 associated with the mode of @1, so the sign bit is
3239 specified by this mode. Check that @1 is the signed
3240 max associated with this sign bit. */
3241 && prec == GET_MODE_PRECISION (TYPE_MODE (arg1_type))
3242 /* signed_type does not work on pointer types. */
3243 && INTEGRAL_TYPE_P (arg1_type))
3244 /* The following case also applies to X < signed_max+1
3245 and X >= signed_max+1 because previous transformations. */
3246 (if (cmp == LE_EXPR || cmp == GT_EXPR)
3247 (with { tree st = signed_type_for (arg1_type); }
3248 (if (cmp == LE_EXPR)
3249 (ge (convert:st @0) { build_zero_cst (st); })
3250 (lt (convert:st @0) { build_zero_cst (st); }))))))))))
3252 (for cmp (unordered ordered unlt unle ungt unge uneq ltgt)
3253 /* If the second operand is NaN, the result is constant. */
3256 (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
3257 && (cmp != LTGT_EXPR || ! flag_trapping_math))
3258 { constant_boolean_node (cmp == ORDERED_EXPR || cmp == LTGT_EXPR
3259 ? false : true, type); })))
3261 /* bool_var != 0 becomes bool_var. */
3263 (ne @0 integer_zerop)
3264 (if (TREE_CODE (TREE_TYPE (@0)) == BOOLEAN_TYPE
3265 && types_match (type, TREE_TYPE (@0)))
3267 /* bool_var == 1 becomes bool_var. */
3269 (eq @0 integer_onep)
3270 (if (TREE_CODE (TREE_TYPE (@0)) == BOOLEAN_TYPE
3271 && types_match (type, TREE_TYPE (@0)))
3274 bool_var == 0 becomes !bool_var or
3275 bool_var != 1 becomes !bool_var
3276 here because that only is good in assignment context as long
3277 as we require a tcc_comparison in GIMPLE_CONDs where we'd
3278 replace if (x == 0) with tem = ~x; if (tem != 0) which is
3279 clearly less optimal and which we'll transform again in forwprop. */
3281 /* When one argument is a constant, overflow detection can be simplified.
3282 Currently restricted to single use so as not to interfere too much with
3283 ADD_OVERFLOW detection in tree-ssa-math-opts.c.
3284 A + CST CMP A -> A CMP' CST' */
3285 (for cmp (lt le ge gt)
3288 (cmp:c (plus@2 @0 INTEGER_CST@1) @0)
3289 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
3290 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))
3293 (out @0 { wide_int_to_tree (TREE_TYPE (@0), wi::max_value
3294 (TYPE_PRECISION (TREE_TYPE (@0)), UNSIGNED) - @1); }))))
3296 /* To detect overflow in unsigned A - B, A < B is simpler than A - B > A.
3297 However, the detection logic for SUB_OVERFLOW in tree-ssa-math-opts.c
3298 expects the long form, so we restrict the transformation for now. */
3301 (cmp:c (minus@2 @0 @1) @0)
3302 (if (single_use (@2)
3303 && ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3304 && TYPE_UNSIGNED (TREE_TYPE (@0))
3305 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
3308 /* Testing for overflow is unnecessary if we already know the result. */
3313 (cmp:c (realpart (IFN_SUB_OVERFLOW@2 @0 @1)) @0)
3314 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
3315 && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
3316 (out (imagpart @2) { build_zero_cst (TREE_TYPE (@0)); }))))
3321 (cmp:c (realpart (IFN_ADD_OVERFLOW:c@2 @0 @1)) @0)
3322 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
3323 && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
3324 (out (imagpart @2) { build_zero_cst (TREE_TYPE (@0)); }))))
3326 /* For unsigned operands, -1 / B < A checks whether A * B would overflow.
3327 Simplify it to __builtin_mul_overflow (A, B, <unused>). */
3331 (cmp:c (trunc_div:s integer_all_onesp @1) @0)
3332 (if (TYPE_UNSIGNED (TREE_TYPE (@0)) && !VECTOR_TYPE_P (TREE_TYPE (@0)))
3333 (with { tree t = TREE_TYPE (@0), cpx = build_complex_type (t); }
3334 (out (imagpart (IFN_MUL_OVERFLOW:cpx @0 @1)) { build_zero_cst (t); })))))
3336 /* Simplification of math builtins. These rules must all be optimizations
3337 as well as IL simplifications. If there is a possibility that the new
3338 form could be a pessimization, the rule should go in the canonicalization
3339 section that follows this one.
3341 Rules can generally go in this section if they satisfy one of
3344 - the rule describes an identity
3346 - the rule replaces calls with something as simple as addition or
3349 - the rule contains unary calls only and simplifies the surrounding
3350 arithmetic. (The idea here is to exclude non-unary calls in which
3351 one operand is constant and in which the call is known to be cheap
3352 when the operand has that value.) */
3354 (if (flag_unsafe_math_optimizations)
3355 /* Simplify sqrt(x) * sqrt(x) -> x. */
3357 (mult (SQRT@1 @0) @1)
3358 (if (!HONOR_SNANS (type))
3361 /* Simplify sqrt(x) * sqrt(y) -> sqrt(x*y). */
3362 (for root (SQRT CBRT)
3364 (mult (root:s @0) (root:s @1))
3365 (root (mult @0 @1))))
3367 /* Simplify expN(x) * expN(y) -> expN(x+y). */
3368 (for exps (EXP EXP2 EXP10 POW10)
3370 (mult (exps:s @0) (exps:s @1))
3371 (exps (plus @0 @1))))
3373 /* Simplify a/root(b/c) into a*root(c/b). */
3374 (for root (SQRT CBRT)
3376 (rdiv @0 (root:s (rdiv:s @1 @2)))
3377 (mult @0 (root (rdiv @2 @1)))))
3379 /* Simplify x/expN(y) into x*expN(-y). */
3380 (for exps (EXP EXP2 EXP10 POW10)
3382 (rdiv @0 (exps:s @1))
3383 (mult @0 (exps (negate @1)))))
3385 (for logs (LOG LOG2 LOG10 LOG10)
3386 exps (EXP EXP2 EXP10 POW10)
3387 /* logN(expN(x)) -> x. */
3391 /* expN(logN(x)) -> x. */
3396 /* Optimize logN(func()) for various exponential functions. We
3397 want to determine the value "x" and the power "exponent" in
3398 order to transform logN(x**exponent) into exponent*logN(x). */
3399 (for logs (LOG LOG LOG LOG2 LOG2 LOG2 LOG10 LOG10)
3400 exps (EXP2 EXP10 POW10 EXP EXP10 POW10 EXP EXP2)
3403 (if (SCALAR_FLOAT_TYPE_P (type))
3409 /* Prepare to do logN(exp(exponent)) -> exponent*logN(e). */
3410 x = build_real_truncate (type, dconst_e ());
3413 /* Prepare to do logN(exp2(exponent)) -> exponent*logN(2). */
3414 x = build_real (type, dconst2);
3418 /* Prepare to do logN(exp10(exponent)) -> exponent*logN(10). */
3420 REAL_VALUE_TYPE dconst10;
3421 real_from_integer (&dconst10, VOIDmode, 10, SIGNED);
3422 x = build_real (type, dconst10);
3429 (mult (logs { x; }) @0)))))
3437 (if (SCALAR_FLOAT_TYPE_P (type))
3443 /* Prepare to do logN(sqrt(x)) -> 0.5*logN(x). */
3444 x = build_real (type, dconsthalf);
3447 /* Prepare to do logN(cbrt(x)) -> (1/3)*logN(x). */
3448 x = build_real_truncate (type, dconst_third ());
3454 (mult { x; } (logs @0))))))
3456 /* logN(pow(x,exponent)) -> exponent*logN(x). */
3457 (for logs (LOG LOG2 LOG10)
3461 (mult @1 (logs @0))))
3466 exps (EXP EXP2 EXP10 POW10)
3467 /* sqrt(expN(x)) -> expN(x*0.5). */
3470 (exps (mult @0 { build_real (type, dconsthalf); })))
3471 /* cbrt(expN(x)) -> expN(x/3). */
3474 (exps (mult @0 { build_real_truncate (type, dconst_third ()); })))
3475 /* pow(expN(x), y) -> expN(x*y). */
3478 (exps (mult @0 @1))))
3480 /* tan(atan(x)) -> x. */
3487 /* cabs(x+0i) or cabs(0+xi) -> abs(x). */
3489 (CABS (complex:C @0 real_zerop@1))
3492 /* trunc(trunc(x)) -> trunc(x), etc. */
3493 (for fns (TRUNC FLOOR CEIL ROUND NEARBYINT RINT)
3497 /* f(x) -> x if x is integer valued and f does nothing for such values. */
3498 (for fns (TRUNC FLOOR CEIL ROUND NEARBYINT RINT)
3500 (fns integer_valued_real_p@0)
3503 /* hypot(x,0) and hypot(0,x) -> abs(x). */
3505 (HYPOT:c @0 real_zerop@1)
3508 /* pow(1,x) -> 1. */
3510 (POW real_onep@0 @1)
3514 /* copysign(x,x) -> x. */
3519 /* copysign(x,y) -> fabs(x) if y is nonnegative. */
3520 (COPYSIGN @0 tree_expr_nonnegative_p@1)
3523 (for scale (LDEXP SCALBN SCALBLN)
3524 /* ldexp(0, x) -> 0. */
3526 (scale real_zerop@0 @1)
3528 /* ldexp(x, 0) -> x. */
3530 (scale @0 integer_zerop@1)
3532 /* ldexp(x, y) -> x if x is +-Inf or NaN. */
3534 (scale REAL_CST@0 @1)
3535 (if (!real_isfinite (TREE_REAL_CST_PTR (@0)))
3538 /* Canonicalization of sequences of math builtins. These rules represent
3539 IL simplifications but are not necessarily optimizations.
3541 The sincos pass is responsible for picking "optimal" implementations
3542 of math builtins, which may be more complicated and can sometimes go
3543 the other way, e.g. converting pow into a sequence of sqrts.
3544 We only want to do these canonicalizations before the pass has run. */
3546 (if (flag_unsafe_math_optimizations && canonicalize_math_p ())
3547 /* Simplify tan(x) * cos(x) -> sin(x). */
3549 (mult:c (TAN:s @0) (COS:s @0))
3552 /* Simplify x * pow(x,c) -> pow(x,c+1). */
3554 (mult:c @0 (POW:s @0 REAL_CST@1))
3555 (if (!TREE_OVERFLOW (@1))
3556 (POW @0 (plus @1 { build_one_cst (type); }))))
3558 /* Simplify sin(x) / cos(x) -> tan(x). */
3560 (rdiv (SIN:s @0) (COS:s @0))
3563 /* Simplify cos(x) / sin(x) -> 1 / tan(x). */
3565 (rdiv (COS:s @0) (SIN:s @0))
3566 (rdiv { build_one_cst (type); } (TAN @0)))
3568 /* Simplify sin(x) / tan(x) -> cos(x). */
3570 (rdiv (SIN:s @0) (TAN:s @0))
3571 (if (! HONOR_NANS (@0)
3572 && ! HONOR_INFINITIES (@0))
3575 /* Simplify tan(x) / sin(x) -> 1.0 / cos(x). */
3577 (rdiv (TAN:s @0) (SIN:s @0))
3578 (if (! HONOR_NANS (@0)
3579 && ! HONOR_INFINITIES (@0))
3580 (rdiv { build_one_cst (type); } (COS @0))))
3582 /* Simplify pow(x,y) * pow(x,z) -> pow(x,y+z). */
3584 (mult (POW:s @0 @1) (POW:s @0 @2))
3585 (POW @0 (plus @1 @2)))
3587 /* Simplify pow(x,y) * pow(z,y) -> pow(x*z,y). */
3589 (mult (POW:s @0 @1) (POW:s @2 @1))
3590 (POW (mult @0 @2) @1))
3592 /* Simplify powi(x,y) * powi(z,y) -> powi(x*z,y). */
3594 (mult (POWI:s @0 @1) (POWI:s @2 @1))
3595 (POWI (mult @0 @2) @1))
3597 /* Simplify pow(x,c) / x -> pow(x,c-1). */
3599 (rdiv (POW:s @0 REAL_CST@1) @0)
3600 (if (!TREE_OVERFLOW (@1))
3601 (POW @0 (minus @1 { build_one_cst (type); }))))
3603 /* Simplify x / pow (y,z) -> x * pow(y,-z). */
3605 (rdiv @0 (POW:s @1 @2))
3606 (mult @0 (POW @1 (negate @2))))
3611 /* sqrt(sqrt(x)) -> pow(x,1/4). */
3614 (pows @0 { build_real (type, dconst_quarter ()); }))
3615 /* sqrt(cbrt(x)) -> pow(x,1/6). */
3618 (pows @0 { build_real_truncate (type, dconst_sixth ()); }))
3619 /* cbrt(sqrt(x)) -> pow(x,1/6). */
3622 (pows @0 { build_real_truncate (type, dconst_sixth ()); }))
3623 /* cbrt(cbrt(x)) -> pow(x,1/9), iff x is nonnegative. */
3625 (cbrts (cbrts tree_expr_nonnegative_p@0))
3626 (pows @0 { build_real_truncate (type, dconst_ninth ()); }))
3627 /* sqrt(pow(x,y)) -> pow(|x|,y*0.5). */
3629 (sqrts (pows @0 @1))
3630 (pows (abs @0) (mult @1 { build_real (type, dconsthalf); })))
3631 /* cbrt(pow(x,y)) -> pow(x,y/3), iff x is nonnegative. */
3633 (cbrts (pows tree_expr_nonnegative_p@0 @1))
3634 (pows @0 (mult @1 { build_real_truncate (type, dconst_third ()); })))
3635 /* pow(sqrt(x),y) -> pow(x,y*0.5). */
3637 (pows (sqrts @0) @1)
3638 (pows @0 (mult @1 { build_real (type, dconsthalf); })))
3639 /* pow(cbrt(x),y) -> pow(x,y/3) iff x is nonnegative. */
3641 (pows (cbrts tree_expr_nonnegative_p@0) @1)
3642 (pows @0 (mult @1 { build_real_truncate (type, dconst_third ()); })))
3643 /* pow(pow(x,y),z) -> pow(x,y*z) iff x is nonnegative. */
3645 (pows (pows tree_expr_nonnegative_p@0 @1) @2)
3646 (pows @0 (mult @1 @2))))
3648 /* cabs(x+xi) -> fabs(x)*sqrt(2). */
3650 (CABS (complex @0 @0))
3651 (mult (abs @0) { build_real_truncate (type, dconst_sqrt2 ()); }))
3653 /* hypot(x,x) -> fabs(x)*sqrt(2). */
3656 (mult (abs @0) { build_real_truncate (type, dconst_sqrt2 ()); }))
3658 /* cexp(x+yi) -> exp(x)*cexpi(y). */
3663 (cexps compositional_complex@0)
3664 (if (targetm.libc_has_function (function_c99_math_complex))
3666 (mult (exps@1 (realpart @0)) (realpart (cexpis:type@2 (imagpart @0))))
3667 (mult @1 (imagpart @2)))))))
3669 (if (canonicalize_math_p ())
3670 /* floor(x) -> trunc(x) if x is nonnegative. */
3674 (floors tree_expr_nonnegative_p@0)
3677 (match double_value_p
3679 (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == double_type_node)))
3680 (for froms (BUILT_IN_TRUNCL
3692 /* truncl(extend(x)) -> extend(trunc(x)), etc., if x is a double. */
3693 (if (optimize && canonicalize_math_p ())
3695 (froms (convert double_value_p@0))
3696 (convert (tos @0)))))
3698 (match float_value_p
3700 (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == float_type_node)))
3701 (for froms (BUILT_IN_TRUNCL BUILT_IN_TRUNC
3702 BUILT_IN_FLOORL BUILT_IN_FLOOR
3703 BUILT_IN_CEILL BUILT_IN_CEIL
3704 BUILT_IN_ROUNDL BUILT_IN_ROUND
3705 BUILT_IN_NEARBYINTL BUILT_IN_NEARBYINT
3706 BUILT_IN_RINTL BUILT_IN_RINT)
3707 tos (BUILT_IN_TRUNCF BUILT_IN_TRUNCF
3708 BUILT_IN_FLOORF BUILT_IN_FLOORF
3709 BUILT_IN_CEILF BUILT_IN_CEILF
3710 BUILT_IN_ROUNDF BUILT_IN_ROUNDF
3711 BUILT_IN_NEARBYINTF BUILT_IN_NEARBYINTF
3712 BUILT_IN_RINTF BUILT_IN_RINTF)
3713 /* truncl(extend(x)) and trunc(extend(x)) -> extend(truncf(x)), etc.,
3715 (if (optimize && canonicalize_math_p ()
3716 && targetm.libc_has_function (function_c99_misc))
3718 (froms (convert float_value_p@0))
3719 (convert (tos @0)))))
3721 (for froms (XFLOORL XCEILL XROUNDL XRINTL)
3722 tos (XFLOOR XCEIL XROUND XRINT)
3723 /* llfloorl(extend(x)) -> llfloor(x), etc., if x is a double. */
3724 (if (optimize && canonicalize_math_p ())
3726 (froms (convert double_value_p@0))
3729 (for froms (XFLOORL XCEILL XROUNDL XRINTL
3730 XFLOOR XCEIL XROUND XRINT)
3731 tos (XFLOORF XCEILF XROUNDF XRINTF)
3732 /* llfloorl(extend(x)) and llfloor(extend(x)) -> llfloorf(x), etc.,
3734 (if (optimize && canonicalize_math_p ())
3736 (froms (convert float_value_p@0))
3739 (if (canonicalize_math_p ())
3740 /* xfloor(x) -> fix_trunc(x) if x is nonnegative. */
3741 (for floors (IFLOOR LFLOOR LLFLOOR)
3743 (floors tree_expr_nonnegative_p@0)
3746 (if (canonicalize_math_p ())
3747 /* xfloor(x) -> fix_trunc(x), etc., if x is integer valued. */
3748 (for fns (IFLOOR LFLOOR LLFLOOR
3750 IROUND LROUND LLROUND)
3752 (fns integer_valued_real_p@0)
3754 (if (!flag_errno_math)
3755 /* xrint(x) -> fix_trunc(x), etc., if x is integer valued. */
3756 (for rints (IRINT LRINT LLRINT)
3758 (rints integer_valued_real_p@0)
3761 (if (canonicalize_math_p ())
3762 (for ifn (IFLOOR ICEIL IROUND IRINT)
3763 lfn (LFLOOR LCEIL LROUND LRINT)
3764 llfn (LLFLOOR LLCEIL LLROUND LLRINT)
3765 /* Canonicalize iround (x) to lround (x) on ILP32 targets where
3766 sizeof (int) == sizeof (long). */
3767 (if (TYPE_PRECISION (integer_type_node)
3768 == TYPE_PRECISION (long_integer_type_node))
3771 (lfn:long_integer_type_node @0)))
3772 /* Canonicalize llround (x) to lround (x) on LP64 targets where
3773 sizeof (long long) == sizeof (long). */
3774 (if (TYPE_PRECISION (long_long_integer_type_node)
3775 == TYPE_PRECISION (long_integer_type_node))
3778 (lfn:long_integer_type_node @0)))))
3780 /* cproj(x) -> x if we're ignoring infinities. */
3783 (if (!HONOR_INFINITIES (type))
3786 /* If the real part is inf and the imag part is known to be
3787 nonnegative, return (inf + 0i). */
3789 (CPROJ (complex REAL_CST@0 tree_expr_nonnegative_p@1))
3790 (if (real_isinf (TREE_REAL_CST_PTR (@0)))
3791 { build_complex_inf (type, false); }))
3793 /* If the imag part is inf, return (inf+I*copysign(0,imag)). */
3795 (CPROJ (complex @0 REAL_CST@1))
3796 (if (real_isinf (TREE_REAL_CST_PTR (@1)))
3797 { build_complex_inf (type, TREE_REAL_CST_PTR (@1)->sign); }))
3803 (pows @0 REAL_CST@1)
3805 const REAL_VALUE_TYPE *value = TREE_REAL_CST_PTR (@1);
3806 REAL_VALUE_TYPE tmp;
3809 /* pow(x,0) -> 1. */
3810 (if (real_equal (value, &dconst0))
3811 { build_real (type, dconst1); })
3812 /* pow(x,1) -> x. */
3813 (if (real_equal (value, &dconst1))
3815 /* pow(x,-1) -> 1/x. */
3816 (if (real_equal (value, &dconstm1))
3817 (rdiv { build_real (type, dconst1); } @0))
3818 /* pow(x,0.5) -> sqrt(x). */
3819 (if (flag_unsafe_math_optimizations
3820 && canonicalize_math_p ()
3821 && real_equal (value, &dconsthalf))
3823 /* pow(x,1/3) -> cbrt(x). */
3824 (if (flag_unsafe_math_optimizations
3825 && canonicalize_math_p ()
3826 && (tmp = real_value_truncate (TYPE_MODE (type), dconst_third ()),
3827 real_equal (value, &tmp)))
3830 /* powi(1,x) -> 1. */
3832 (POWI real_onep@0 @1)
3836 (POWI @0 INTEGER_CST@1)
3838 /* powi(x,0) -> 1. */
3839 (if (wi::eq_p (@1, 0))
3840 { build_real (type, dconst1); })
3841 /* powi(x,1) -> x. */
3842 (if (wi::eq_p (@1, 1))
3844 /* powi(x,-1) -> 1/x. */
3845 (if (wi::eq_p (@1, -1))
3846 (rdiv { build_real (type, dconst1); } @0))))
3848 /* Narrowing of arithmetic and logical operations.
3850 These are conceptually similar to the transformations performed for
3851 the C/C++ front-ends by shorten_binary_op and shorten_compare. Long
3852 term we want to move all that code out of the front-ends into here. */
3854 /* If we have a narrowing conversion of an arithmetic operation where
3855 both operands are widening conversions from the same type as the outer
3856 narrowing conversion. Then convert the innermost operands to a suitable
3857 unsigned type (to avoid introducing undefined behavior), perform the
3858 operation and convert the result to the desired type. */
3859 (for op (plus minus)
3861 (convert (op:s (convert@2 @0) (convert?@3 @1)))
3862 (if (INTEGRAL_TYPE_P (type)
3863 /* We check for type compatibility between @0 and @1 below,
3864 so there's no need to check that @1/@3 are integral types. */
3865 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
3866 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
3867 /* The precision of the type of each operand must match the
3868 precision of the mode of each operand, similarly for the
3870 && (TYPE_PRECISION (TREE_TYPE (@0))
3871 == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (@0))))
3872 && (TYPE_PRECISION (TREE_TYPE (@1))
3873 == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (@1))))
3874 && TYPE_PRECISION (type) == GET_MODE_PRECISION (TYPE_MODE (type))
3875 /* The inner conversion must be a widening conversion. */
3876 && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
3877 && types_match (@0, type)
3878 && (types_match (@0, @1)
3879 /* Or the second operand is const integer or converted const
3880 integer from valueize. */
3881 || TREE_CODE (@1) == INTEGER_CST))
3882 (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
3883 (op @0 (convert @1))
3884 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
3885 (convert (op (convert:utype @0)
3886 (convert:utype @1))))))))
3888 /* This is another case of narrowing, specifically when there's an outer
3889 BIT_AND_EXPR which masks off bits outside the type of the innermost
3890 operands. Like the previous case we have to convert the operands
3891 to unsigned types to avoid introducing undefined behavior for the
3892 arithmetic operation. */
3893 (for op (minus plus)
3895 (bit_and (op:s (convert@2 @0) (convert@3 @1)) INTEGER_CST@4)
3896 (if (INTEGRAL_TYPE_P (type)
3897 /* We check for type compatibility between @0 and @1 below,
3898 so there's no need to check that @1/@3 are integral types. */
3899 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
3900 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
3901 /* The precision of the type of each operand must match the
3902 precision of the mode of each operand, similarly for the
3904 && (TYPE_PRECISION (TREE_TYPE (@0))
3905 == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (@0))))
3906 && (TYPE_PRECISION (TREE_TYPE (@1))
3907 == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (@1))))
3908 && TYPE_PRECISION (type) == GET_MODE_PRECISION (TYPE_MODE (type))
3909 /* The inner conversion must be a widening conversion. */
3910 && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
3911 && types_match (@0, @1)
3912 && (tree_int_cst_min_precision (@4, TYPE_SIGN (TREE_TYPE (@0)))
3913 <= TYPE_PRECISION (TREE_TYPE (@0)))
3914 && (wi::bit_and (@4, wi::mask (TYPE_PRECISION (TREE_TYPE (@0)),
3915 true, TYPE_PRECISION (type))) == 0))
3916 (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
3917 (with { tree ntype = TREE_TYPE (@0); }
3918 (convert (bit_and (op @0 @1) (convert:ntype @4))))
3919 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
3920 (convert (bit_and (op (convert:utype @0) (convert:utype @1))
3921 (convert:utype @4))))))))
3923 /* Transform (@0 < @1 and @0 < @2) to use min,
3924 (@0 > @1 and @0 > @2) to use max */
3925 (for op (lt le gt ge)
3926 ext (min min max max)
3928 (bit_and (op:cs @0 @1) (op:cs @0 @2))
3929 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
3930 && TREE_CODE (@0) != INTEGER_CST)
3931 (op @0 (ext @1 @2)))))
3934 /* signbit(x) -> 0 if x is nonnegative. */
3935 (SIGNBIT tree_expr_nonnegative_p@0)
3936 { integer_zero_node; })
3939 /* signbit(x) -> x<0 if x doesn't have signed zeros. */
3941 (if (!HONOR_SIGNED_ZEROS (@0))
3942 (convert (lt @0 { build_real (TREE_TYPE (@0), dconst0); }))))
3944 /* Transform comparisons of the form X +- C1 CMP C2 to X CMP C2 -+ C1. */
3946 (for op (plus minus)
3949 (cmp (op@3 @0 INTEGER_CST@1) INTEGER_CST@2)
3950 (if (!TREE_OVERFLOW (@1) && !TREE_OVERFLOW (@2)
3951 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0))
3952 && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@0))
3953 && !TYPE_SATURATING (TREE_TYPE (@0)))
3954 (with { tree res = int_const_binop (rop, @2, @1); }
3955 (if (TREE_OVERFLOW (res)
3956 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
3957 { constant_boolean_node (cmp == NE_EXPR, type); }
3958 (if (single_use (@3))
3959 (cmp @0 { res; }))))))))
3960 (for cmp (lt le gt ge)
3961 (for op (plus minus)
3964 (cmp (op@3 @0 INTEGER_CST@1) INTEGER_CST@2)
3965 (if (!TREE_OVERFLOW (@1) && !TREE_OVERFLOW (@2)
3966 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
3967 (with { tree res = int_const_binop (rop, @2, @1); }
3968 (if (TREE_OVERFLOW (res))
3970 fold_overflow_warning (("assuming signed overflow does not occur "
3971 "when simplifying conditional to constant"),
3972 WARN_STRICT_OVERFLOW_CONDITIONAL);
3973 bool less = cmp == LE_EXPR || cmp == LT_EXPR;
3974 /* wi::ges_p (@2, 0) should be sufficient for a signed type. */
3975 bool ovf_high = wi::lt_p (@1, 0, TYPE_SIGN (TREE_TYPE (@1)))
3976 != (op == MINUS_EXPR);
3977 constant_boolean_node (less == ovf_high, type);
3979 (if (single_use (@3))
3982 fold_overflow_warning (("assuming signed overflow does not occur "
3983 "when changing X +- C1 cmp C2 to "
3985 WARN_STRICT_OVERFLOW_COMPARISON);
3987 (cmp @0 { res; })))))))))
3989 /* Canonicalizations of BIT_FIELD_REFs. */
3992 (BIT_FIELD_REF @0 @1 @2)
3994 (if (TREE_CODE (TREE_TYPE (@0)) == COMPLEX_TYPE
3995 && tree_int_cst_equal (@1, TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))))
3997 (if (integer_zerop (@2))
3998 (view_convert (realpart @0)))
3999 (if (tree_int_cst_equal (@2, TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))))
4000 (view_convert (imagpart @0)))))
4001 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
4002 && INTEGRAL_TYPE_P (type)
4003 /* On GIMPLE this should only apply to register arguments. */
4004 && (! GIMPLE || is_gimple_reg (@0))
4005 /* A bit-field-ref that referenced the full argument can be stripped. */
4006 && ((compare_tree_int (@1, TYPE_PRECISION (TREE_TYPE (@0))) == 0
4007 && integer_zerop (@2))
4008 /* Low-parts can be reduced to integral conversions.
4009 ??? The following doesn't work for PDP endian. */
4010 || (BYTES_BIG_ENDIAN == WORDS_BIG_ENDIAN
4011 /* Don't even think about BITS_BIG_ENDIAN. */
4012 && TYPE_PRECISION (TREE_TYPE (@0)) % BITS_PER_UNIT == 0
4013 && TYPE_PRECISION (type) % BITS_PER_UNIT == 0
4014 && compare_tree_int (@2, (BYTES_BIG_ENDIAN
4015 ? (TYPE_PRECISION (TREE_TYPE (@0))
4016 - TYPE_PRECISION (type))
4020 /* Simplify vector extracts. */
4023 (BIT_FIELD_REF CONSTRUCTOR@0 @1 @2)
4024 (if (VECTOR_TYPE_P (TREE_TYPE (@0))
4025 && (types_match (type, TREE_TYPE (TREE_TYPE (@0)))
4026 || (VECTOR_TYPE_P (type)
4027 && types_match (TREE_TYPE (type), TREE_TYPE (TREE_TYPE (@0))))))
4030 tree ctor = (TREE_CODE (@0) == SSA_NAME
4031 ? gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0)) : @0);
4032 tree eltype = TREE_TYPE (TREE_TYPE (ctor));
4033 unsigned HOST_WIDE_INT width = tree_to_uhwi (TYPE_SIZE (eltype));
4034 unsigned HOST_WIDE_INT n = tree_to_uhwi (@1);
4035 unsigned HOST_WIDE_INT idx = tree_to_uhwi (@2);
4038 && (idx % width) == 0
4040 && ((idx + n) / width) <= TYPE_VECTOR_SUBPARTS (TREE_TYPE (ctor)))
4045 /* Constructor elements can be subvectors. */
4046 unsigned HOST_WIDE_INT k = 1;
4047 if (CONSTRUCTOR_NELTS (ctor) != 0)
4049 tree cons_elem = TREE_TYPE (CONSTRUCTOR_ELT (ctor, 0)->value);
4050 if (TREE_CODE (cons_elem) == VECTOR_TYPE)
4051 k = TYPE_VECTOR_SUBPARTS (cons_elem);
4055 /* We keep an exact subset of the constructor elements. */
4056 (if ((idx % k) == 0 && (n % k) == 0)
4057 (if (CONSTRUCTOR_NELTS (ctor) == 0)
4058 { build_constructor (type, NULL); }
4065 (if (idx < CONSTRUCTOR_NELTS (ctor))
4066 { CONSTRUCTOR_ELT (ctor, idx)->value; }
4067 { build_zero_cst (type); })
4069 vec<constructor_elt, va_gc> *vals;
4070 vec_alloc (vals, n);
4071 for (unsigned i = 0;
4072 i < n && idx + i < CONSTRUCTOR_NELTS (ctor); ++i)
4073 CONSTRUCTOR_APPEND_ELT (vals, NULL_TREE,
4074 CONSTRUCTOR_ELT (ctor, idx + i)->value);
4075 build_constructor (type, vals);
4077 /* The bitfield references a single constructor element. */
4078 (if (idx + n <= (idx / k + 1) * k)
4080 (if (CONSTRUCTOR_NELTS (ctor) <= idx / k)
4081 { build_zero_cst (type); })
4083 { CONSTRUCTOR_ELT (ctor, idx / k)->value; })
4084 (BIT_FIELD_REF { CONSTRUCTOR_ELT (ctor, idx / k)->value; }
4085 @1 { bitsize_int ((idx % k) * width); })))))))))