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-2018 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 /* Binary operations and their associated IFN_COND_* function. */
79 (define_operator_list UNCOND_BINARY
82 bit_and bit_ior bit_xor)
83 (define_operator_list COND_BINARY
84 IFN_COND_ADD IFN_COND_SUB
85 IFN_COND_MIN IFN_COND_MAX
86 IFN_COND_AND IFN_COND_IOR IFN_COND_XOR)
88 /* As opposed to convert?, this still creates a single pattern, so
89 it is not a suitable replacement for convert? in all cases. */
90 (match (nop_convert @0)
92 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))))
93 (match (nop_convert @0)
95 (if (VECTOR_TYPE_P (type) && VECTOR_TYPE_P (TREE_TYPE (@0))
96 && known_eq (TYPE_VECTOR_SUBPARTS (type),
97 TYPE_VECTOR_SUBPARTS (TREE_TYPE (@0)))
98 && tree_nop_conversion_p (TREE_TYPE (type), TREE_TYPE (TREE_TYPE (@0))))))
99 /* This one has to be last, or it shadows the others. */
100 (match (nop_convert @0)
103 /* Simplifications of operations with one constant operand and
104 simplifications to constants or single values. */
106 (for op (plus pointer_plus minus bit_ior bit_xor)
108 (op @0 integer_zerop)
111 /* 0 +p index -> (type)index */
113 (pointer_plus integer_zerop @1)
114 (non_lvalue (convert @1)))
116 /* ptr - 0 -> (type)ptr */
118 (pointer_diff @0 integer_zerop)
121 /* See if ARG1 is zero and X + ARG1 reduces to X.
122 Likewise if the operands are reversed. */
124 (plus:c @0 real_zerop@1)
125 (if (fold_real_zero_addition_p (type, @1, 0))
128 /* See if ARG1 is zero and X - ARG1 reduces to X. */
130 (minus @0 real_zerop@1)
131 (if (fold_real_zero_addition_p (type, @1, 1))
135 This is unsafe for certain floats even in non-IEEE formats.
136 In IEEE, it is unsafe because it does wrong for NaNs.
137 Also note that operand_equal_p is always false if an operand
141 (if (!FLOAT_TYPE_P (type) || !HONOR_NANS (type))
142 { build_zero_cst (type); }))
144 (pointer_diff @@0 @0)
145 { build_zero_cst (type); })
148 (mult @0 integer_zerop@1)
151 /* Maybe fold x * 0 to 0. The expressions aren't the same
152 when x is NaN, since x * 0 is also NaN. Nor are they the
153 same in modes with signed zeros, since multiplying a
154 negative value by 0 gives -0, not +0. */
156 (mult @0 real_zerop@1)
157 (if (!HONOR_NANS (type) && !HONOR_SIGNED_ZEROS (type))
160 /* In IEEE floating point, x*1 is not equivalent to x for snans.
161 Likewise for complex arithmetic with signed zeros. */
164 (if (!HONOR_SNANS (type)
165 && (!HONOR_SIGNED_ZEROS (type)
166 || !COMPLEX_FLOAT_TYPE_P (type)))
169 /* Transform x * -1.0 into -x. */
171 (mult @0 real_minus_onep)
172 (if (!HONOR_SNANS (type)
173 && (!HONOR_SIGNED_ZEROS (type)
174 || !COMPLEX_FLOAT_TYPE_P (type)))
177 (for cmp (gt ge lt le)
178 outp (convert convert negate negate)
179 outn (negate negate convert convert)
180 /* Transform (X > 0.0 ? 1.0 : -1.0) into copysign(1, X). */
181 /* Transform (X >= 0.0 ? 1.0 : -1.0) into copysign(1, X). */
182 /* Transform (X < 0.0 ? 1.0 : -1.0) into copysign(1,-X). */
183 /* Transform (X <= 0.0 ? 1.0 : -1.0) into copysign(1,-X). */
185 (cond (cmp @0 real_zerop) real_onep@1 real_minus_onep)
186 (if (!HONOR_NANS (type) && !HONOR_SIGNED_ZEROS (type)
187 && types_match (type, TREE_TYPE (@0)))
189 (if (types_match (type, float_type_node))
190 (BUILT_IN_COPYSIGNF @1 (outp @0)))
191 (if (types_match (type, double_type_node))
192 (BUILT_IN_COPYSIGN @1 (outp @0)))
193 (if (types_match (type, long_double_type_node))
194 (BUILT_IN_COPYSIGNL @1 (outp @0))))))
195 /* Transform (X > 0.0 ? -1.0 : 1.0) into copysign(1,-X). */
196 /* Transform (X >= 0.0 ? -1.0 : 1.0) into copysign(1,-X). */
197 /* Transform (X < 0.0 ? -1.0 : 1.0) into copysign(1,X). */
198 /* Transform (X <= 0.0 ? -1.0 : 1.0) into copysign(1,X). */
200 (cond (cmp @0 real_zerop) real_minus_onep real_onep@1)
201 (if (!HONOR_NANS (type) && !HONOR_SIGNED_ZEROS (type)
202 && types_match (type, TREE_TYPE (@0)))
204 (if (types_match (type, float_type_node))
205 (BUILT_IN_COPYSIGNF @1 (outn @0)))
206 (if (types_match (type, double_type_node))
207 (BUILT_IN_COPYSIGN @1 (outn @0)))
208 (if (types_match (type, long_double_type_node))
209 (BUILT_IN_COPYSIGNL @1 (outn @0)))))))
211 /* Transform X * copysign (1.0, X) into abs(X). */
213 (mult:c @0 (COPYSIGN_ALL real_onep @0))
214 (if (!HONOR_NANS (type) && !HONOR_SIGNED_ZEROS (type))
217 /* Transform X * copysign (1.0, -X) into -abs(X). */
219 (mult:c @0 (COPYSIGN_ALL real_onep (negate @0)))
220 (if (!HONOR_NANS (type) && !HONOR_SIGNED_ZEROS (type))
223 /* Transform copysign (CST, X) into copysign (ABS(CST), X). */
225 (COPYSIGN_ALL REAL_CST@0 @1)
226 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@0)))
227 (COPYSIGN_ALL (negate @0) @1)))
229 /* X * 1, X / 1 -> X. */
230 (for op (mult trunc_div ceil_div floor_div round_div exact_div)
235 /* (A / (1 << B)) -> (A >> B).
236 Only for unsigned A. For signed A, this would not preserve rounding
238 For example: (-1 / ( 1 << B)) != -1 >> B. */
240 (trunc_div @0 (lshift integer_onep@1 @2))
241 (if ((TYPE_UNSIGNED (type) || tree_expr_nonnegative_p (@0))
242 && (!VECTOR_TYPE_P (type)
243 || target_supports_op_p (type, RSHIFT_EXPR, optab_vector)
244 || target_supports_op_p (type, RSHIFT_EXPR, optab_scalar)))
247 /* Preserve explicit divisions by 0: the C++ front-end wants to detect
248 undefined behavior in constexpr evaluation, and assuming that the division
249 traps enables better optimizations than these anyway. */
250 (for div (trunc_div ceil_div floor_div round_div exact_div)
251 /* 0 / X is always zero. */
253 (div integer_zerop@0 @1)
254 /* But not for 0 / 0 so that we can get the proper warnings and errors. */
255 (if (!integer_zerop (@1))
259 (div @0 integer_minus_onep@1)
260 (if (!TYPE_UNSIGNED (type))
265 /* But not for 0 / 0 so that we can get the proper warnings and errors.
266 And not for _Fract types where we can't build 1. */
267 (if (!integer_zerop (@0) && !ALL_FRACT_MODE_P (TYPE_MODE (type)))
268 { build_one_cst (type); }))
269 /* X / abs (X) is X < 0 ? -1 : 1. */
272 (if (INTEGRAL_TYPE_P (type)
273 && TYPE_OVERFLOW_UNDEFINED (type))
274 (cond (lt @0 { build_zero_cst (type); })
275 { build_minus_one_cst (type); } { build_one_cst (type); })))
278 (div:C @0 (negate @0))
279 (if ((INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
280 && TYPE_OVERFLOW_UNDEFINED (type))
281 { build_minus_one_cst (type); })))
283 /* For unsigned integral types, FLOOR_DIV_EXPR is the same as
284 TRUNC_DIV_EXPR. Rewrite into the latter in this case. */
287 (if ((INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
288 && TYPE_UNSIGNED (type))
291 /* Combine two successive divisions. Note that combining ceil_div
292 and floor_div is trickier and combining round_div even more so. */
293 (for div (trunc_div exact_div)
295 (div (div @0 INTEGER_CST@1) INTEGER_CST@2)
298 wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@2),
299 TYPE_SIGN (type), &overflow_p);
302 (div @0 { wide_int_to_tree (type, mul); })
303 (if (TYPE_UNSIGNED (type)
304 || mul != wi::min_value (TYPE_PRECISION (type), SIGNED))
305 { build_zero_cst (type); })))))
307 /* Combine successive multiplications. Similar to above, but handling
308 overflow is different. */
310 (mult (mult @0 INTEGER_CST@1) INTEGER_CST@2)
313 wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@2),
314 TYPE_SIGN (type), &overflow_p);
316 /* Skip folding on overflow: the only special case is @1 * @2 == -INT_MIN,
317 otherwise undefined overflow implies that @0 must be zero. */
318 (if (!overflow_p || TYPE_OVERFLOW_WRAPS (type))
319 (mult @0 { wide_int_to_tree (type, mul); }))))
321 /* Optimize A / A to 1.0 if we don't care about
322 NaNs or Infinities. */
325 (if (FLOAT_TYPE_P (type)
326 && ! HONOR_NANS (type)
327 && ! HONOR_INFINITIES (type))
328 { build_one_cst (type); }))
330 /* Optimize -A / A to -1.0 if we don't care about
331 NaNs or Infinities. */
333 (rdiv:C @0 (negate @0))
334 (if (FLOAT_TYPE_P (type)
335 && ! HONOR_NANS (type)
336 && ! HONOR_INFINITIES (type))
337 { build_minus_one_cst (type); }))
339 /* PR71078: x / abs(x) -> copysign (1.0, x) */
341 (rdiv:C (convert? @0) (convert? (abs @0)))
342 (if (SCALAR_FLOAT_TYPE_P (type)
343 && ! HONOR_NANS (type)
344 && ! HONOR_INFINITIES (type))
346 (if (types_match (type, float_type_node))
347 (BUILT_IN_COPYSIGNF { build_one_cst (type); } (convert @0)))
348 (if (types_match (type, double_type_node))
349 (BUILT_IN_COPYSIGN { build_one_cst (type); } (convert @0)))
350 (if (types_match (type, long_double_type_node))
351 (BUILT_IN_COPYSIGNL { build_one_cst (type); } (convert @0))))))
353 /* In IEEE floating point, x/1 is not equivalent to x for snans. */
356 (if (!HONOR_SNANS (type))
359 /* In IEEE floating point, x/-1 is not equivalent to -x for snans. */
361 (rdiv @0 real_minus_onep)
362 (if (!HONOR_SNANS (type))
365 (if (flag_reciprocal_math)
366 /* Convert (A/B)/C to A/(B*C). */
368 (rdiv (rdiv:s @0 @1) @2)
369 (rdiv @0 (mult @1 @2)))
371 /* Canonicalize x / (C1 * y) to (x * C2) / y. */
373 (rdiv @0 (mult:s @1 REAL_CST@2))
375 { tree tem = const_binop (RDIV_EXPR, type, build_one_cst (type), @2); }
377 (rdiv (mult @0 { tem; } ) @1))))
379 /* Convert A/(B/C) to (A/B)*C */
381 (rdiv @0 (rdiv:s @1 @2))
382 (mult (rdiv @0 @1) @2)))
384 /* Simplify x / (- y) to -x / y. */
386 (rdiv @0 (negate @1))
387 (rdiv (negate @0) @1))
389 /* Optimize (X & (-A)) / A where A is a power of 2, to X >> log2(A) */
390 (for div (trunc_div ceil_div floor_div round_div exact_div)
392 (div (convert? (bit_and @0 INTEGER_CST@1)) INTEGER_CST@2)
393 (if (integer_pow2p (@2)
394 && tree_int_cst_sgn (@2) > 0
395 && tree_nop_conversion_p (type, TREE_TYPE (@0))
396 && wi::to_wide (@2) + wi::to_wide (@1) == 0)
398 { build_int_cst (integer_type_node,
399 wi::exact_log2 (wi::to_wide (@2))); }))))
401 /* If ARG1 is a constant, we can convert this to a multiply by the
402 reciprocal. This does not have the same rounding properties,
403 so only do this if -freciprocal-math. We can actually
404 always safely do it if ARG1 is a power of two, but it's hard to
405 tell if it is or not in a portable manner. */
406 (for cst (REAL_CST COMPLEX_CST VECTOR_CST)
410 (if (flag_reciprocal_math
413 { tree tem = const_binop (RDIV_EXPR, type, build_one_cst (type), @1); }
415 (mult @0 { tem; } )))
416 (if (cst != COMPLEX_CST)
417 (with { tree inverse = exact_inverse (type, @1); }
419 (mult @0 { inverse; } ))))))))
421 (for mod (ceil_mod floor_mod round_mod trunc_mod)
422 /* 0 % X is always zero. */
424 (mod integer_zerop@0 @1)
425 /* But not for 0 % 0 so that we can get the proper warnings and errors. */
426 (if (!integer_zerop (@1))
428 /* X % 1 is always zero. */
430 (mod @0 integer_onep)
431 { build_zero_cst (type); })
432 /* X % -1 is zero. */
434 (mod @0 integer_minus_onep@1)
435 (if (!TYPE_UNSIGNED (type))
436 { build_zero_cst (type); }))
440 /* But not for 0 % 0 so that we can get the proper warnings and errors. */
441 (if (!integer_zerop (@0))
442 { build_zero_cst (type); }))
443 /* (X % Y) % Y is just X % Y. */
445 (mod (mod@2 @0 @1) @1)
447 /* From extract_muldiv_1: (X * C1) % C2 is zero if C1 is a multiple of C2. */
449 (mod (mult @0 INTEGER_CST@1) INTEGER_CST@2)
450 (if (ANY_INTEGRAL_TYPE_P (type)
451 && TYPE_OVERFLOW_UNDEFINED (type)
452 && wi::multiple_of_p (wi::to_wide (@1), wi::to_wide (@2),
454 { build_zero_cst (type); })))
456 /* X % -C is the same as X % C. */
458 (trunc_mod @0 INTEGER_CST@1)
459 (if (TYPE_SIGN (type) == SIGNED
460 && !TREE_OVERFLOW (@1)
461 && wi::neg_p (wi::to_wide (@1))
462 && !TYPE_OVERFLOW_TRAPS (type)
463 /* Avoid this transformation if C is INT_MIN, i.e. C == -C. */
464 && !sign_bit_p (@1, @1))
465 (trunc_mod @0 (negate @1))))
467 /* X % -Y is the same as X % Y. */
469 (trunc_mod @0 (convert? (negate @1)))
470 (if (INTEGRAL_TYPE_P (type)
471 && !TYPE_UNSIGNED (type)
472 && !TYPE_OVERFLOW_TRAPS (type)
473 && tree_nop_conversion_p (type, TREE_TYPE (@1))
474 /* Avoid this transformation if X might be INT_MIN or
475 Y might be -1, because we would then change valid
476 INT_MIN % -(-1) into invalid INT_MIN % -1. */
477 && (expr_not_equal_to (@0, wi::to_wide (TYPE_MIN_VALUE (type)))
478 || expr_not_equal_to (@1, wi::minus_one (TYPE_PRECISION
480 (trunc_mod @0 (convert @1))))
482 /* X - (X / Y) * Y is the same as X % Y. */
484 (minus (convert1? @0) (convert2? (mult:c (trunc_div @@0 @@1) @1)))
485 (if (INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
486 (convert (trunc_mod @0 @1))))
488 /* Optimize TRUNC_MOD_EXPR by a power of two into a BIT_AND_EXPR,
489 i.e. "X % C" into "X & (C - 1)", if X and C are positive.
490 Also optimize A % (C << N) where C is a power of 2,
491 to A & ((C << N) - 1). */
492 (match (power_of_two_cand @1)
494 (match (power_of_two_cand @1)
495 (lshift INTEGER_CST@1 @2))
496 (for mod (trunc_mod floor_mod)
498 (mod @0 (convert?@3 (power_of_two_cand@1 @2)))
499 (if ((TYPE_UNSIGNED (type)
500 || tree_expr_nonnegative_p (@0))
501 && tree_nop_conversion_p (type, TREE_TYPE (@3))
502 && integer_pow2p (@2) && tree_int_cst_sgn (@2) > 0)
503 (bit_and @0 (convert (minus @1 { build_int_cst (TREE_TYPE (@1), 1); }))))))
505 /* Simplify (unsigned t * 2)/2 -> unsigned t & 0x7FFFFFFF. */
507 (trunc_div (mult @0 integer_pow2p@1) @1)
508 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
509 (bit_and @0 { wide_int_to_tree
510 (type, wi::mask (TYPE_PRECISION (type)
511 - wi::exact_log2 (wi::to_wide (@1)),
512 false, TYPE_PRECISION (type))); })))
514 /* Simplify (unsigned t / 2) * 2 -> unsigned t & ~1. */
516 (mult (trunc_div @0 integer_pow2p@1) @1)
517 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
518 (bit_and @0 (negate @1))))
520 /* Simplify (t * 2) / 2) -> t. */
521 (for div (trunc_div ceil_div floor_div round_div exact_div)
523 (div (mult:c @0 @1) @1)
524 (if (ANY_INTEGRAL_TYPE_P (type)
525 && TYPE_OVERFLOW_UNDEFINED (type))
529 /* Simplify cos(-x) and cos(|x|) -> cos(x). Similarly for cosh. */
534 /* Simplify pow(-x, y) and pow(|x|,y) -> pow(x,y) if y is an even integer. */
537 (pows (op @0) REAL_CST@1)
538 (with { HOST_WIDE_INT n; }
539 (if (real_isinteger (&TREE_REAL_CST (@1), &n) && (n & 1) == 0)
541 /* Likewise for powi. */
544 (pows (op @0) INTEGER_CST@1)
545 (if ((wi::to_wide (@1) & 1) == 0)
547 /* Strip negate and abs from both operands of hypot. */
555 /* copysign(-x, y) and copysign(abs(x), y) -> copysign(x, y). */
556 (for copysigns (COPYSIGN_ALL)
558 (copysigns (op @0) @1)
561 /* abs(x)*abs(x) -> x*x. Should be valid for all types. */
566 /* cos(copysign(x, y)) -> cos(x). Similarly for cosh. */
570 (coss (copysigns @0 @1))
573 /* pow(copysign(x, y), z) -> pow(x, z) if z is an even integer. */
577 (pows (copysigns @0 @2) REAL_CST@1)
578 (with { HOST_WIDE_INT n; }
579 (if (real_isinteger (&TREE_REAL_CST (@1), &n) && (n & 1) == 0)
581 /* Likewise for powi. */
585 (pows (copysigns @0 @2) INTEGER_CST@1)
586 (if ((wi::to_wide (@1) & 1) == 0)
591 /* hypot(copysign(x, y), z) -> hypot(x, z). */
593 (hypots (copysigns @0 @1) @2)
595 /* hypot(x, copysign(y, z)) -> hypot(x, y). */
597 (hypots @0 (copysigns @1 @2))
600 /* copysign(x, CST) -> [-]abs (x). */
601 (for copysigns (COPYSIGN_ALL)
603 (copysigns @0 REAL_CST@1)
604 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
608 /* copysign(copysign(x, y), z) -> copysign(x, z). */
609 (for copysigns (COPYSIGN_ALL)
611 (copysigns (copysigns @0 @1) @2)
614 /* copysign(x,y)*copysign(x,y) -> x*x. */
615 (for copysigns (COPYSIGN_ALL)
617 (mult (copysigns@2 @0 @1) @2)
620 /* ccos(-x) -> ccos(x). Similarly for ccosh. */
621 (for ccoss (CCOS CCOSH)
626 /* cabs(-x) and cos(conj(x)) -> cabs(x). */
627 (for ops (conj negate)
633 /* Fold (a * (1 << b)) into (a << b) */
635 (mult:c @0 (convert? (lshift integer_onep@1 @2)))
636 (if (! FLOAT_TYPE_P (type)
637 && tree_nop_conversion_p (type, TREE_TYPE (@1)))
640 /* Fold (1 << (C - x)) where C = precision(type) - 1
641 into ((1 << C) >> x). */
643 (lshift integer_onep@0 (minus@1 INTEGER_CST@2 @3))
644 (if (INTEGRAL_TYPE_P (type)
645 && wi::eq_p (wi::to_wide (@2), TYPE_PRECISION (type) - 1)
647 (if (TYPE_UNSIGNED (type))
648 (rshift (lshift @0 @2) @3)
650 { tree utype = unsigned_type_for (type); }
651 (convert (rshift (lshift (convert:utype @0) @2) @3))))))
653 /* Fold (C1/X)*C2 into (C1*C2)/X. */
655 (mult (rdiv@3 REAL_CST@0 @1) REAL_CST@2)
656 (if (flag_associative_math
659 { tree tem = const_binop (MULT_EXPR, type, @0, @2); }
661 (rdiv { tem; } @1)))))
663 /* Simplify ~X & X as zero. */
665 (bit_and:c (convert? @0) (convert? (bit_not @0)))
666 { build_zero_cst (type); })
668 /* PR71636: Transform x & ((1U << b) - 1) -> x & ~(~0U << b); */
670 (bit_and:c @0 (plus:s (lshift:s integer_onep @1) integer_minus_onep))
671 (if (TYPE_UNSIGNED (type))
672 (bit_and @0 (bit_not (lshift { build_all_ones_cst (type); } @1)))))
674 (for bitop (bit_and bit_ior)
676 /* PR35691: Transform
677 (x == 0 & y == 0) -> (x | typeof(x)(y)) == 0.
678 (x != 0 | y != 0) -> (x | typeof(x)(y)) != 0. */
680 (bitop (cmp @0 integer_zerop@2) (cmp @1 integer_zerop))
681 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
682 && INTEGRAL_TYPE_P (TREE_TYPE (@1))
683 && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
684 (cmp (bit_ior @0 (convert @1)) @2)))
686 (x == -1 & y == -1) -> (x & typeof(x)(y)) == -1.
687 (x != -1 | y != -1) -> (x & typeof(x)(y)) != -1. */
689 (bitop (cmp @0 integer_all_onesp@2) (cmp @1 integer_all_onesp))
690 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
691 && INTEGRAL_TYPE_P (TREE_TYPE (@1))
692 && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
693 (cmp (bit_and @0 (convert @1)) @2))))
695 /* Fold (A & ~B) - (A & B) into (A ^ B) - B. */
697 (minus (bit_and:cs @0 (bit_not @1)) (bit_and:cs @0 @1))
698 (minus (bit_xor @0 @1) @1))
700 (minus (bit_and:s @0 INTEGER_CST@2) (bit_and:s @0 INTEGER_CST@1))
701 (if (~wi::to_wide (@2) == wi::to_wide (@1))
702 (minus (bit_xor @0 @1) @1)))
704 /* Fold (A & B) - (A & ~B) into B - (A ^ B). */
706 (minus (bit_and:cs @0 @1) (bit_and:cs @0 (bit_not @1)))
707 (minus @1 (bit_xor @0 @1)))
709 /* Simplify (X & ~Y) |^+ (~X & Y) -> X ^ Y. */
710 (for op (bit_ior bit_xor plus)
712 (op (bit_and:c @0 (bit_not @1)) (bit_and:c (bit_not @0) @1))
715 (op:c (bit_and @0 INTEGER_CST@2) (bit_and (bit_not @0) INTEGER_CST@1))
716 (if (~wi::to_wide (@2) == wi::to_wide (@1))
719 /* PR53979: Transform ((a ^ b) | a) -> (a | b) */
721 (bit_ior:c (bit_xor:c @0 @1) @0)
724 /* (a & ~b) | (a ^ b) --> a ^ b */
726 (bit_ior:c (bit_and:c @0 (bit_not @1)) (bit_xor:c@2 @0 @1))
729 /* (a & ~b) ^ ~a --> ~(a & b) */
731 (bit_xor:c (bit_and:cs @0 (bit_not @1)) (bit_not @0))
732 (bit_not (bit_and @0 @1)))
734 /* (a | b) & ~(a ^ b) --> a & b */
736 (bit_and:c (bit_ior @0 @1) (bit_not (bit_xor:c @0 @1)))
739 /* a | ~(a ^ b) --> a | ~b */
741 (bit_ior:c @0 (bit_not:s (bit_xor:c @0 @1)))
742 (bit_ior @0 (bit_not @1)))
744 /* (a | b) | (a &^ b) --> a | b */
745 (for op (bit_and bit_xor)
747 (bit_ior:c (bit_ior@2 @0 @1) (op:c @0 @1))
750 /* (a & b) | ~(a ^ b) --> ~(a ^ b) */
752 (bit_ior:c (bit_and:c @0 @1) (bit_not@2 (bit_xor @0 @1)))
755 /* ~(~a & b) --> a | ~b */
757 (bit_not (bit_and:cs (bit_not @0) @1))
758 (bit_ior @0 (bit_not @1)))
760 /* Simplify (~X & Y) to X ^ Y if we know that (X & ~Y) is 0. */
763 (bit_and (bit_not SSA_NAME@0) INTEGER_CST@1)
764 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
765 && wi::bit_and_not (get_nonzero_bits (@0), wi::to_wide (@1)) == 0)
769 /* X % Y is smaller than Y. */
772 (cmp (trunc_mod @0 @1) @1)
773 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
774 { constant_boolean_node (cmp == LT_EXPR, type); })))
777 (cmp @1 (trunc_mod @0 @1))
778 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
779 { constant_boolean_node (cmp == GT_EXPR, type); })))
783 (bit_ior @0 integer_all_onesp@1)
788 (bit_ior @0 integer_zerop)
793 (bit_and @0 integer_zerop@1)
799 (for op (bit_ior bit_xor plus)
801 (op:c (convert? @0) (convert? (bit_not @0)))
802 (convert { build_all_ones_cst (TREE_TYPE (@0)); })))
807 { build_zero_cst (type); })
809 /* Canonicalize X ^ ~0 to ~X. */
811 (bit_xor @0 integer_all_onesp@1)
816 (bit_and @0 integer_all_onesp)
819 /* x & x -> x, x | x -> x */
820 (for bitop (bit_and bit_ior)
825 /* x & C -> x if we know that x & ~C == 0. */
828 (bit_and SSA_NAME@0 INTEGER_CST@1)
829 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
830 && wi::bit_and_not (get_nonzero_bits (@0), wi::to_wide (@1)) == 0)
834 /* x + (x & 1) -> (x + 1) & ~1 */
836 (plus:c @0 (bit_and:s @0 integer_onep@1))
837 (bit_and (plus @0 @1) (bit_not @1)))
839 /* x & ~(x & y) -> x & ~y */
840 /* x | ~(x | y) -> x | ~y */
841 (for bitop (bit_and bit_ior)
843 (bitop:c @0 (bit_not (bitop:cs @0 @1)))
844 (bitop @0 (bit_not @1))))
846 /* (x | y) & ~x -> y & ~x */
847 /* (x & y) | ~x -> y | ~x */
848 (for bitop (bit_and bit_ior)
849 rbitop (bit_ior bit_and)
851 (bitop:c (rbitop:c @0 @1) (bit_not@2 @0))
854 /* (x & y) ^ (x | y) -> x ^ y */
856 (bit_xor:c (bit_and @0 @1) (bit_ior @0 @1))
859 /* (x ^ y) ^ (x | y) -> x & y */
861 (bit_xor:c (bit_xor @0 @1) (bit_ior @0 @1))
864 /* (x & y) + (x ^ y) -> x | y */
865 /* (x & y) | (x ^ y) -> x | y */
866 /* (x & y) ^ (x ^ y) -> x | y */
867 (for op (plus bit_ior bit_xor)
869 (op:c (bit_and @0 @1) (bit_xor @0 @1))
872 /* (x & y) + (x | y) -> x + y */
874 (plus:c (bit_and @0 @1) (bit_ior @0 @1))
877 /* (x + y) - (x | y) -> x & y */
879 (minus (plus @0 @1) (bit_ior @0 @1))
880 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
881 && !TYPE_SATURATING (type))
884 /* (x + y) - (x & y) -> x | y */
886 (minus (plus @0 @1) (bit_and @0 @1))
887 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
888 && !TYPE_SATURATING (type))
891 /* (x | y) - (x ^ y) -> x & y */
893 (minus (bit_ior @0 @1) (bit_xor @0 @1))
896 /* (x | y) - (x & y) -> x ^ y */
898 (minus (bit_ior @0 @1) (bit_and @0 @1))
901 /* (x | y) & ~(x & y) -> x ^ y */
903 (bit_and:c (bit_ior @0 @1) (bit_not (bit_and @0 @1)))
906 /* (x | y) & (~x ^ y) -> x & y */
908 (bit_and:c (bit_ior:c @0 @1) (bit_xor:c @1 (bit_not @0)))
911 /* ~x & ~y -> ~(x | y)
912 ~x | ~y -> ~(x & y) */
913 (for op (bit_and bit_ior)
914 rop (bit_ior bit_and)
916 (op (convert1? (bit_not @0)) (convert2? (bit_not @1)))
917 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
918 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
919 (bit_not (rop (convert @0) (convert @1))))))
921 /* If we are XORing or adding two BIT_AND_EXPR's, both of which are and'ing
922 with a constant, and the two constants have no bits in common,
923 we should treat this as a BIT_IOR_EXPR since this may produce more
925 (for op (bit_xor plus)
927 (op (convert1? (bit_and@4 @0 INTEGER_CST@1))
928 (convert2? (bit_and@5 @2 INTEGER_CST@3)))
929 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
930 && tree_nop_conversion_p (type, TREE_TYPE (@2))
931 && (wi::to_wide (@1) & wi::to_wide (@3)) == 0)
932 (bit_ior (convert @4) (convert @5)))))
934 /* (X | Y) ^ X -> Y & ~ X*/
936 (bit_xor:c (convert1? (bit_ior:c @@0 @1)) (convert2? @0))
937 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
938 (convert (bit_and @1 (bit_not @0)))))
940 /* Convert ~X ^ ~Y to X ^ Y. */
942 (bit_xor (convert1? (bit_not @0)) (convert2? (bit_not @1)))
943 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
944 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
945 (bit_xor (convert @0) (convert @1))))
947 /* Convert ~X ^ C to X ^ ~C. */
949 (bit_xor (convert? (bit_not @0)) INTEGER_CST@1)
950 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
951 (bit_xor (convert @0) (bit_not @1))))
953 /* Fold (X & Y) ^ Y and (X ^ Y) & Y as ~X & Y. */
954 (for opo (bit_and bit_xor)
955 opi (bit_xor bit_and)
957 (opo:c (opi:c @0 @1) @1)
958 (bit_and (bit_not @0) @1)))
960 /* Given a bit-wise operation CODE applied to ARG0 and ARG1, see if both
961 operands are another bit-wise operation with a common input. If so,
962 distribute the bit operations to save an operation and possibly two if
963 constants are involved. For example, convert
964 (A | B) & (A | C) into A | (B & C)
965 Further simplification will occur if B and C are constants. */
966 (for op (bit_and bit_ior bit_xor)
967 rop (bit_ior bit_and bit_and)
969 (op (convert? (rop:c @@0 @1)) (convert? (rop:c @0 @2)))
970 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
971 && tree_nop_conversion_p (type, TREE_TYPE (@2)))
972 (rop (convert @0) (op (convert @1) (convert @2))))))
974 /* Some simple reassociation for bit operations, also handled in reassoc. */
975 /* (X & Y) & Y -> X & Y
976 (X | Y) | Y -> X | Y */
977 (for op (bit_and bit_ior)
979 (op:c (convert1?@2 (op:c @0 @@1)) (convert2? @1))
981 /* (X ^ Y) ^ Y -> X */
983 (bit_xor:c (convert1? (bit_xor:c @0 @@1)) (convert2? @1))
985 /* (X & Y) & (X & Z) -> (X & Y) & Z
986 (X | Y) | (X | Z) -> (X | Y) | Z */
987 (for op (bit_and bit_ior)
989 (op (convert1?@3 (op:c@4 @0 @1)) (convert2?@5 (op:c@6 @0 @2)))
990 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
991 && tree_nop_conversion_p (type, TREE_TYPE (@2)))
992 (if (single_use (@5) && single_use (@6))
994 (if (single_use (@3) && single_use (@4))
995 (op (convert @1) @5))))))
996 /* (X ^ Y) ^ (X ^ Z) -> Y ^ Z */
998 (bit_xor (convert1? (bit_xor:c @0 @1)) (convert2? (bit_xor:c @0 @2)))
999 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1000 && tree_nop_conversion_p (type, TREE_TYPE (@2)))
1001 (bit_xor (convert @1) (convert @2))))
1010 (abs tree_expr_nonnegative_p@0)
1013 /* A few cases of fold-const.c negate_expr_p predicate. */
1014 (match negate_expr_p
1016 (if ((INTEGRAL_TYPE_P (type)
1017 && TYPE_UNSIGNED (type))
1018 || (!TYPE_OVERFLOW_SANITIZED (type)
1019 && may_negate_without_overflow_p (t)))))
1020 (match negate_expr_p
1022 (match negate_expr_p
1024 (if (!TYPE_OVERFLOW_SANITIZED (type))))
1025 (match negate_expr_p
1027 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (t)))))
1028 /* VECTOR_CST handling of non-wrapping types would recurse in unsupported
1030 (match negate_expr_p
1032 (if (FLOAT_TYPE_P (TREE_TYPE (type)) || TYPE_OVERFLOW_WRAPS (type))))
1033 (match negate_expr_p
1035 (if ((ANY_INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_WRAPS (type))
1036 || (FLOAT_TYPE_P (type)
1037 && !HONOR_SIGN_DEPENDENT_ROUNDING (type)
1038 && !HONOR_SIGNED_ZEROS (type)))))
1040 /* (-A) * (-B) -> A * B */
1042 (mult:c (convert1? (negate @0)) (convert2? negate_expr_p@1))
1043 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
1044 && tree_nop_conversion_p (type, TREE_TYPE (@1)))
1045 (mult (convert @0) (convert (negate @1)))))
1047 /* -(A + B) -> (-B) - A. */
1049 (negate (plus:c @0 negate_expr_p@1))
1050 (if (!HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type))
1051 && !HONOR_SIGNED_ZEROS (element_mode (type)))
1052 (minus (negate @1) @0)))
1054 /* -(A - B) -> B - A. */
1056 (negate (minus @0 @1))
1057 (if ((ANY_INTEGRAL_TYPE_P (type) && !TYPE_OVERFLOW_SANITIZED (type))
1058 || (FLOAT_TYPE_P (type)
1059 && !HONOR_SIGN_DEPENDENT_ROUNDING (type)
1060 && !HONOR_SIGNED_ZEROS (type)))
1063 (negate (pointer_diff @0 @1))
1064 (if (TYPE_OVERFLOW_UNDEFINED (type))
1065 (pointer_diff @1 @0)))
1067 /* A - B -> A + (-B) if B is easily negatable. */
1069 (minus @0 negate_expr_p@1)
1070 (if (!FIXED_POINT_TYPE_P (type))
1071 (plus @0 (negate @1))))
1073 /* Try to fold (type) X op CST -> (type) (X op ((type-x) CST))
1075 For bitwise binary operations apply operand conversions to the
1076 binary operation result instead of to the operands. This allows
1077 to combine successive conversions and bitwise binary operations.
1078 We combine the above two cases by using a conditional convert. */
1079 (for bitop (bit_and bit_ior bit_xor)
1081 (bitop (convert @0) (convert? @1))
1082 (if (((TREE_CODE (@1) == INTEGER_CST
1083 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
1084 && int_fits_type_p (@1, TREE_TYPE (@0)))
1085 || types_match (@0, @1))
1086 /* ??? This transform conflicts with fold-const.c doing
1087 Convert (T)(x & c) into (T)x & (T)c, if c is an integer
1088 constants (if x has signed type, the sign bit cannot be set
1089 in c). This folds extension into the BIT_AND_EXPR.
1090 Restrict it to GIMPLE to avoid endless recursions. */
1091 && (bitop != BIT_AND_EXPR || GIMPLE)
1092 && (/* That's a good idea if the conversion widens the operand, thus
1093 after hoisting the conversion the operation will be narrower. */
1094 TYPE_PRECISION (TREE_TYPE (@0)) < TYPE_PRECISION (type)
1095 /* It's also a good idea if the conversion is to a non-integer
1097 || GET_MODE_CLASS (TYPE_MODE (type)) != MODE_INT
1098 /* Or if the precision of TO is not the same as the precision
1100 || !type_has_mode_precision_p (type)))
1101 (convert (bitop @0 (convert @1))))))
1103 (for bitop (bit_and bit_ior)
1104 rbitop (bit_ior bit_and)
1105 /* (x | y) & x -> x */
1106 /* (x & y) | x -> x */
1108 (bitop:c (rbitop:c @0 @1) @0)
1110 /* (~x | y) & x -> x & y */
1111 /* (~x & y) | x -> x | y */
1113 (bitop:c (rbitop:c (bit_not @0) @1) @0)
1116 /* (x | CST1) & CST2 -> (x & CST2) | (CST1 & CST2) */
1118 (bit_and (bit_ior @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
1119 (bit_ior (bit_and @0 @2) (bit_and @1 @2)))
1121 /* Combine successive equal operations with constants. */
1122 (for bitop (bit_and bit_ior bit_xor)
1124 (bitop (bitop @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
1125 (if (!CONSTANT_CLASS_P (@0))
1126 /* This is the canonical form regardless of whether (bitop @1 @2) can be
1127 folded to a constant. */
1128 (bitop @0 (bitop @1 @2))
1129 /* In this case we have three constants and (bitop @0 @1) doesn't fold
1130 to a constant. This can happen if @0 or @1 is a POLY_INT_CST and if
1131 the values involved are such that the operation can't be decided at
1132 compile time. Try folding one of @0 or @1 with @2 to see whether
1133 that combination can be decided at compile time.
1135 Keep the existing form if both folds fail, to avoid endless
1137 (with { tree cst1 = const_binop (bitop, type, @0, @2); }
1139 (bitop @1 { cst1; })
1140 (with { tree cst2 = const_binop (bitop, type, @1, @2); }
1142 (bitop @0 { cst2; }))))))))
1144 /* Try simple folding for X op !X, and X op X with the help
1145 of the truth_valued_p and logical_inverted_value predicates. */
1146 (match truth_valued_p
1148 (if (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1)))
1149 (for op (tcc_comparison truth_and truth_andif truth_or truth_orif truth_xor)
1150 (match truth_valued_p
1152 (match truth_valued_p
1155 (match (logical_inverted_value @0)
1157 (match (logical_inverted_value @0)
1158 (bit_not truth_valued_p@0))
1159 (match (logical_inverted_value @0)
1160 (eq @0 integer_zerop))
1161 (match (logical_inverted_value @0)
1162 (ne truth_valued_p@0 integer_truep))
1163 (match (logical_inverted_value @0)
1164 (bit_xor truth_valued_p@0 integer_truep))
1168 (bit_and:c @0 (logical_inverted_value @0))
1169 { build_zero_cst (type); })
1170 /* X | !X and X ^ !X -> 1, , if X is truth-valued. */
1171 (for op (bit_ior bit_xor)
1173 (op:c truth_valued_p@0 (logical_inverted_value @0))
1174 { constant_boolean_node (true, type); }))
1175 /* X ==/!= !X is false/true. */
1178 (op:c truth_valued_p@0 (logical_inverted_value @0))
1179 { constant_boolean_node (op == NE_EXPR ? true : false, type); }))
1183 (bit_not (bit_not @0))
1186 /* Convert ~ (-A) to A - 1. */
1188 (bit_not (convert? (negate @0)))
1189 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1190 || !TYPE_UNSIGNED (TREE_TYPE (@0)))
1191 (convert (minus @0 { build_each_one_cst (TREE_TYPE (@0)); }))))
1193 /* Convert - (~A) to A + 1. */
1195 (negate (nop_convert (bit_not @0)))
1196 (plus (view_convert @0) { build_each_one_cst (type); }))
1198 /* Convert ~ (A - 1) or ~ (A + -1) to -A. */
1200 (bit_not (convert? (minus @0 integer_each_onep)))
1201 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1202 || !TYPE_UNSIGNED (TREE_TYPE (@0)))
1203 (convert (negate @0))))
1205 (bit_not (convert? (plus @0 integer_all_onesp)))
1206 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1207 || !TYPE_UNSIGNED (TREE_TYPE (@0)))
1208 (convert (negate @0))))
1210 /* Part of convert ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify. */
1212 (bit_not (convert? (bit_xor @0 INTEGER_CST@1)))
1213 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1214 (convert (bit_xor @0 (bit_not @1)))))
1216 (bit_not (convert? (bit_xor:c (bit_not @0) @1)))
1217 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1218 (convert (bit_xor @0 @1))))
1220 /* Otherwise prefer ~(X ^ Y) to ~X ^ Y as more canonical. */
1222 (bit_xor:c (nop_convert:s (bit_not:s @0)) @1)
1223 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1224 (bit_not (bit_xor (view_convert @0) @1))))
1226 /* (x & ~m) | (y & m) -> ((x ^ y) & m) ^ x */
1228 (bit_ior:c (bit_and:cs @0 (bit_not @2)) (bit_and:cs @1 @2))
1229 (bit_xor (bit_and (bit_xor @0 @1) @2) @0))
1231 /* Fold A - (A & B) into ~B & A. */
1233 (minus (convert1? @0) (convert2?:s (bit_and:cs @@0 @1)))
1234 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
1235 && tree_nop_conversion_p (type, TREE_TYPE (@1)))
1236 (convert (bit_and (bit_not @1) @0))))
1238 /* (m1 CMP m2) * d -> (m1 CMP m2) ? d : 0 */
1239 (for cmp (gt lt ge le)
1241 (mult (convert (cmp @0 @1)) @2)
1242 (cond (cmp @0 @1) @2 { build_zero_cst (type); })))
1244 /* For integral types with undefined overflow and C != 0 fold
1245 x * C EQ/NE y * C into x EQ/NE y. */
1248 (cmp (mult:c @0 @1) (mult:c @2 @1))
1249 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
1250 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1251 && tree_expr_nonzero_p (@1))
1254 /* For integral types with wrapping overflow and C odd fold
1255 x * C EQ/NE y * C into x EQ/NE y. */
1258 (cmp (mult @0 INTEGER_CST@1) (mult @2 @1))
1259 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
1260 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))
1261 && (TREE_INT_CST_LOW (@1) & 1) != 0)
1264 /* For integral types with undefined overflow and C != 0 fold
1265 x * C RELOP y * C into:
1267 x RELOP y for nonnegative C
1268 y RELOP x for negative C */
1269 (for cmp (lt gt le ge)
1271 (cmp (mult:c @0 @1) (mult:c @2 @1))
1272 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
1273 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1274 (if (tree_expr_nonnegative_p (@1) && tree_expr_nonzero_p (@1))
1276 (if (TREE_CODE (@1) == INTEGER_CST
1277 && wi::neg_p (wi::to_wide (@1), TYPE_SIGN (TREE_TYPE (@1))))
1280 /* (X - 1U) <= INT_MAX-1U into (int) X > 0. */
1284 (cmp (plus @0 integer_minus_onep@1) INTEGER_CST@2)
1285 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1286 && TYPE_UNSIGNED (TREE_TYPE (@0))
1287 && TYPE_PRECISION (TREE_TYPE (@0)) > 1
1288 && (wi::to_wide (@2)
1289 == wi::max_value (TYPE_PRECISION (TREE_TYPE (@0)), SIGNED) - 1))
1290 (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
1291 (icmp (convert:stype @0) { build_int_cst (stype, 0); })))))
1293 /* X / 4 < Y / 4 iff X < Y when the division is known to be exact. */
1294 (for cmp (simple_comparison)
1296 (cmp (exact_div @0 INTEGER_CST@2) (exact_div @1 @2))
1297 (if (wi::gt_p (wi::to_wide (@2), 0, TYPE_SIGN (TREE_TYPE (@2))))
1300 /* X / C1 op C2 into a simple range test. */
1301 (for cmp (simple_comparison)
1303 (cmp (trunc_div:s @0 INTEGER_CST@1) INTEGER_CST@2)
1304 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1305 && integer_nonzerop (@1)
1306 && !TREE_OVERFLOW (@1)
1307 && !TREE_OVERFLOW (@2))
1308 (with { tree lo, hi; bool neg_overflow;
1309 enum tree_code code = fold_div_compare (cmp, @1, @2, &lo, &hi,
1312 (if (code == LT_EXPR || code == GE_EXPR)
1313 (if (TREE_OVERFLOW (lo))
1314 { build_int_cst (type, (code == LT_EXPR) ^ neg_overflow); }
1315 (if (code == LT_EXPR)
1318 (if (code == LE_EXPR || code == GT_EXPR)
1319 (if (TREE_OVERFLOW (hi))
1320 { build_int_cst (type, (code == LE_EXPR) ^ neg_overflow); }
1321 (if (code == LE_EXPR)
1325 { build_int_cst (type, code == NE_EXPR); })
1326 (if (code == EQ_EXPR && !hi)
1328 (if (code == EQ_EXPR && !lo)
1330 (if (code == NE_EXPR && !hi)
1332 (if (code == NE_EXPR && !lo)
1335 { build_range_check (UNKNOWN_LOCATION, type, @0, code == EQ_EXPR,
1339 tree etype = range_check_type (TREE_TYPE (@0));
1342 if (! TYPE_UNSIGNED (etype))
1343 etype = unsigned_type_for (etype);
1344 hi = fold_convert (etype, hi);
1345 lo = fold_convert (etype, lo);
1346 hi = const_binop (MINUS_EXPR, etype, hi, lo);
1349 (if (etype && hi && !TREE_OVERFLOW (hi))
1350 (if (code == EQ_EXPR)
1351 (le (minus (convert:etype @0) { lo; }) { hi; })
1352 (gt (minus (convert:etype @0) { lo; }) { hi; })))))))))
1354 /* X + Z < Y + Z is the same as X < Y when there is no overflow. */
1355 (for op (lt le ge gt)
1357 (op (plus:c @0 @2) (plus:c @1 @2))
1358 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1359 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1361 /* For equality and subtraction, this is also true with wrapping overflow. */
1362 (for op (eq ne minus)
1364 (op (plus:c @0 @2) (plus:c @1 @2))
1365 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1366 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1367 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
1370 /* X - Z < Y - Z is the same as X < Y when there is no overflow. */
1371 (for op (lt le ge gt)
1373 (op (minus @0 @2) (minus @1 @2))
1374 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1375 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1377 /* For equality and subtraction, this is also true with wrapping overflow. */
1378 (for op (eq ne minus)
1380 (op (minus @0 @2) (minus @1 @2))
1381 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1382 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1383 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
1385 /* And for pointers... */
1386 (for op (simple_comparison)
1388 (op (pointer_diff@3 @0 @2) (pointer_diff @1 @2))
1389 (if (!TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
1392 (minus (pointer_diff@3 @0 @2) (pointer_diff @1 @2))
1393 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@3))
1394 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
1395 (pointer_diff @0 @1)))
1397 /* Z - X < Z - Y is the same as Y < X when there is no overflow. */
1398 (for op (lt le ge gt)
1400 (op (minus @2 @0) (minus @2 @1))
1401 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1402 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1404 /* For equality and subtraction, this is also true with wrapping overflow. */
1405 (for op (eq ne minus)
1407 (op (minus @2 @0) (minus @2 @1))
1408 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1409 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1410 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
1412 /* And for pointers... */
1413 (for op (simple_comparison)
1415 (op (pointer_diff@3 @2 @0) (pointer_diff @2 @1))
1416 (if (!TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
1419 (minus (pointer_diff@3 @2 @0) (pointer_diff @2 @1))
1420 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@3))
1421 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
1422 (pointer_diff @1 @0)))
1424 /* X + Y < Y is the same as X < 0 when there is no overflow. */
1425 (for op (lt le gt ge)
1427 (op:c (plus:c@2 @0 @1) @1)
1428 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1429 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1430 && (CONSTANT_CLASS_P (@0) || single_use (@2)))
1431 (op @0 { build_zero_cst (TREE_TYPE (@0)); }))))
1432 /* For equality, this is also true with wrapping overflow. */
1435 (op:c (nop_convert@3 (plus:c@2 @0 (convert1? @1))) (convert2? @1))
1436 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1437 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1438 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
1439 && (CONSTANT_CLASS_P (@0) || (single_use (@2) && single_use (@3)))
1440 && tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@2))
1441 && tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@1)))
1442 (op @0 { build_zero_cst (TREE_TYPE (@0)); })))
1444 (op:c (nop_convert@3 (pointer_plus@2 (convert1? @0) @1)) (convert2? @0))
1445 (if (tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@0))
1446 && tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0))
1447 && (CONSTANT_CLASS_P (@1) || (single_use (@2) && single_use (@3))))
1448 (op @1 { build_zero_cst (TREE_TYPE (@1)); }))))
1450 /* X - Y < X is the same as Y > 0 when there is no overflow.
1451 For equality, this is also true with wrapping overflow. */
1452 (for op (simple_comparison)
1454 (op:c @0 (minus@2 @0 @1))
1455 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1456 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1457 || ((op == EQ_EXPR || op == NE_EXPR)
1458 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
1459 && (CONSTANT_CLASS_P (@1) || single_use (@2)))
1460 (op @1 { build_zero_cst (TREE_TYPE (@1)); }))))
1463 * (X / Y) == 0 -> X < Y if X, Y are unsigned.
1464 * (X / Y) != 0 -> X >= Y, if X, Y are unsigned.
1469 (cmp (trunc_div @0 @1) integer_zerop)
1470 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
1471 && (VECTOR_TYPE_P (type) || !VECTOR_TYPE_P (TREE_TYPE (@0))))
1474 /* X == C - X can never be true if C is odd. */
1477 (cmp:c (convert? @0) (convert1? (minus INTEGER_CST@1 (convert2? @0))))
1478 (if (TREE_INT_CST_LOW (@1) & 1)
1479 { constant_boolean_node (cmp == NE_EXPR, type); })))
1481 /* Arguments on which one can call get_nonzero_bits to get the bits
1483 (match with_possible_nonzero_bits
1485 (match with_possible_nonzero_bits
1487 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))))
1488 /* Slightly extended version, do not make it recursive to keep it cheap. */
1489 (match (with_possible_nonzero_bits2 @0)
1490 with_possible_nonzero_bits@0)
1491 (match (with_possible_nonzero_bits2 @0)
1492 (bit_and:c with_possible_nonzero_bits@0 @2))
1494 /* Same for bits that are known to be set, but we do not have
1495 an equivalent to get_nonzero_bits yet. */
1496 (match (with_certain_nonzero_bits2 @0)
1498 (match (with_certain_nonzero_bits2 @0)
1499 (bit_ior @1 INTEGER_CST@0))
1501 /* X == C (or X & Z == Y | C) is impossible if ~nonzero(X) & C != 0. */
1504 (cmp:c (with_possible_nonzero_bits2 @0) (with_certain_nonzero_bits2 @1))
1505 (if (wi::bit_and_not (wi::to_wide (@1), get_nonzero_bits (@0)) != 0)
1506 { constant_boolean_node (cmp == NE_EXPR, type); })))
1508 /* ((X inner_op C0) outer_op C1)
1509 With X being a tree where value_range has reasoned certain bits to always be
1510 zero throughout its computed value range,
1511 inner_op = {|,^}, outer_op = {|,^} and inner_op != outer_op
1512 where zero_mask has 1's for all bits that are sure to be 0 in
1514 if (inner_op == '^') C0 &= ~C1;
1515 if ((C0 & ~zero_mask) == 0) then emit (X outer_op (C0 outer_op C1)
1516 if ((C1 & ~zero_mask) == 0) then emit (X inner_op (C0 outer_op C1)
1518 (for inner_op (bit_ior bit_xor)
1519 outer_op (bit_xor bit_ior)
1522 (inner_op:s @2 INTEGER_CST@0) INTEGER_CST@1)
1526 wide_int zero_mask_not;
1530 if (TREE_CODE (@2) == SSA_NAME)
1531 zero_mask_not = get_nonzero_bits (@2);
1535 if (inner_op == BIT_XOR_EXPR)
1537 C0 = wi::bit_and_not (wi::to_wide (@0), wi::to_wide (@1));
1538 cst_emit = C0 | wi::to_wide (@1);
1542 C0 = wi::to_wide (@0);
1543 cst_emit = C0 ^ wi::to_wide (@1);
1546 (if (!fail && (C0 & zero_mask_not) == 0)
1547 (outer_op @2 { wide_int_to_tree (type, cst_emit); })
1548 (if (!fail && (wi::to_wide (@1) & zero_mask_not) == 0)
1549 (inner_op @2 { wide_int_to_tree (type, cst_emit); }))))))
1551 /* Associate (p +p off1) +p off2 as (p +p (off1 + off2)). */
1553 (pointer_plus (pointer_plus:s @0 @1) @3)
1554 (pointer_plus @0 (plus @1 @3)))
1560 tem4 = (unsigned long) tem3;
1565 (pointer_plus @0 (convert?@2 (minus@3 (convert @1) (convert @0))))
1566 /* Conditionally look through a sign-changing conversion. */
1567 (if (TYPE_PRECISION (TREE_TYPE (@2)) == TYPE_PRECISION (TREE_TYPE (@3))
1568 && ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@1)))
1569 || (GENERIC && type == TREE_TYPE (@1))))
1572 (pointer_plus @0 (convert?@2 (pointer_diff@3 @1 @@0)))
1573 (if (TYPE_PRECISION (TREE_TYPE (@2)) >= TYPE_PRECISION (TREE_TYPE (@3)))
1577 tem = (sizetype) ptr;
1581 and produce the simpler and easier to analyze with respect to alignment
1582 ... = ptr & ~algn; */
1584 (pointer_plus @0 (negate (bit_and (convert @0) INTEGER_CST@1)))
1585 (with { tree algn = wide_int_to_tree (TREE_TYPE (@0), ~wi::to_wide (@1)); }
1586 (bit_and @0 { algn; })))
1588 /* Try folding difference of addresses. */
1590 (minus (convert ADDR_EXPR@0) (convert @1))
1591 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1592 (with { poly_int64 diff; }
1593 (if (ptr_difference_const (@0, @1, &diff))
1594 { build_int_cst_type (type, diff); }))))
1596 (minus (convert @0) (convert ADDR_EXPR@1))
1597 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1598 (with { poly_int64 diff; }
1599 (if (ptr_difference_const (@0, @1, &diff))
1600 { build_int_cst_type (type, diff); }))))
1602 (pointer_diff (convert?@2 ADDR_EXPR@0) (convert?@3 @1))
1603 (if (tree_nop_conversion_p (TREE_TYPE(@2), TREE_TYPE (@0))
1604 && tree_nop_conversion_p (TREE_TYPE(@3), TREE_TYPE (@1)))
1605 (with { poly_int64 diff; }
1606 (if (ptr_difference_const (@0, @1, &diff))
1607 { build_int_cst_type (type, diff); }))))
1609 (pointer_diff (convert?@2 @0) (convert?@3 ADDR_EXPR@1))
1610 (if (tree_nop_conversion_p (TREE_TYPE(@2), TREE_TYPE (@0))
1611 && tree_nop_conversion_p (TREE_TYPE(@3), TREE_TYPE (@1)))
1612 (with { poly_int64 diff; }
1613 (if (ptr_difference_const (@0, @1, &diff))
1614 { build_int_cst_type (type, diff); }))))
1616 /* If arg0 is derived from the address of an object or function, we may
1617 be able to fold this expression using the object or function's
1620 (bit_and (convert? @0) INTEGER_CST@1)
1621 (if (POINTER_TYPE_P (TREE_TYPE (@0))
1622 && tree_nop_conversion_p (type, TREE_TYPE (@0)))
1626 unsigned HOST_WIDE_INT bitpos;
1627 get_pointer_alignment_1 (@0, &align, &bitpos);
1629 (if (wi::ltu_p (wi::to_wide (@1), align / BITS_PER_UNIT))
1630 { wide_int_to_tree (type, (wi::to_wide (@1)
1631 & (bitpos / BITS_PER_UNIT))); }))))
1634 /* We can't reassociate at all for saturating types. */
1635 (if (!TYPE_SATURATING (type))
1637 /* Contract negates. */
1638 /* A + (-B) -> A - B */
1640 (plus:c @0 (convert? (negate @1)))
1641 /* Apply STRIP_NOPS on the negate. */
1642 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1643 && !TYPE_OVERFLOW_SANITIZED (type))
1647 if (INTEGRAL_TYPE_P (type)
1648 && TYPE_OVERFLOW_WRAPS (type) != TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
1649 t1 = TYPE_OVERFLOW_WRAPS (type) ? type : TREE_TYPE (@1);
1651 (convert (minus (convert:t1 @0) (convert:t1 @1))))))
1652 /* A - (-B) -> A + B */
1654 (minus @0 (convert? (negate @1)))
1655 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1656 && !TYPE_OVERFLOW_SANITIZED (type))
1660 if (INTEGRAL_TYPE_P (type)
1661 && TYPE_OVERFLOW_WRAPS (type) != TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
1662 t1 = TYPE_OVERFLOW_WRAPS (type) ? type : TREE_TYPE (@1);
1664 (convert (plus (convert:t1 @0) (convert:t1 @1))))))
1666 Sign-extension is ok except for INT_MIN, which thankfully cannot
1667 happen without overflow. */
1669 (negate (convert (negate @1)))
1670 (if (INTEGRAL_TYPE_P (type)
1671 && (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@1))
1672 || (!TYPE_UNSIGNED (TREE_TYPE (@1))
1673 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))))
1674 && !TYPE_OVERFLOW_SANITIZED (type)
1675 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@1)))
1678 (negate (convert negate_expr_p@1))
1679 (if (SCALAR_FLOAT_TYPE_P (type)
1680 && ((DECIMAL_FLOAT_TYPE_P (type)
1681 == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1))
1682 && TYPE_PRECISION (type) >= TYPE_PRECISION (TREE_TYPE (@1)))
1683 || !HONOR_SIGN_DEPENDENT_ROUNDING (type)))
1684 (convert (negate @1))))
1686 (negate (nop_convert (negate @1)))
1687 (if (!TYPE_OVERFLOW_SANITIZED (type)
1688 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@1)))
1691 /* We can't reassociate floating-point unless -fassociative-math
1692 or fixed-point plus or minus because of saturation to +-Inf. */
1693 (if ((!FLOAT_TYPE_P (type) || flag_associative_math)
1694 && !FIXED_POINT_TYPE_P (type))
1696 /* Match patterns that allow contracting a plus-minus pair
1697 irrespective of overflow issues. */
1698 /* (A +- B) - A -> +- B */
1699 /* (A +- B) -+ B -> A */
1700 /* A - (A +- B) -> -+ B */
1701 /* A +- (B -+ A) -> +- B */
1703 (minus (plus:c @0 @1) @0)
1706 (minus (minus @0 @1) @0)
1709 (plus:c (minus @0 @1) @1)
1712 (minus @0 (plus:c @0 @1))
1715 (minus @0 (minus @0 @1))
1717 /* (A +- B) + (C - A) -> C +- B */
1718 /* (A + B) - (A - C) -> B + C */
1719 /* More cases are handled with comparisons. */
1721 (plus:c (plus:c @0 @1) (minus @2 @0))
1724 (plus:c (minus @0 @1) (minus @2 @0))
1727 (plus:c (pointer_diff @0 @1) (pointer_diff @2 @0))
1728 (if (TYPE_OVERFLOW_UNDEFINED (type)
1729 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0)))
1730 (pointer_diff @2 @1)))
1732 (minus (plus:c @0 @1) (minus @0 @2))
1735 /* (A +- CST1) +- CST2 -> A + CST3
1736 Use view_convert because it is safe for vectors and equivalent for
1738 (for outer_op (plus minus)
1739 (for inner_op (plus minus)
1740 neg_inner_op (minus plus)
1742 (outer_op (nop_convert (inner_op @0 CONSTANT_CLASS_P@1))
1744 /* If one of the types wraps, use that one. */
1745 (if (!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_WRAPS (type))
1746 /* If all 3 captures are CONSTANT_CLASS_P, punt, as we might recurse
1747 forever if something doesn't simplify into a constant. */
1748 (if (!CONSTANT_CLASS_P (@0))
1749 (if (outer_op == PLUS_EXPR)
1750 (plus (view_convert @0) (inner_op @2 (view_convert @1)))
1751 (minus (view_convert @0) (neg_inner_op @2 (view_convert @1)))))
1752 (if (!ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1753 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
1754 (if (outer_op == PLUS_EXPR)
1755 (view_convert (plus @0 (inner_op (view_convert @2) @1)))
1756 (view_convert (minus @0 (neg_inner_op (view_convert @2) @1))))
1757 /* If the constant operation overflows we cannot do the transform
1758 directly as we would introduce undefined overflow, for example
1759 with (a - 1) + INT_MIN. */
1760 (if (types_match (type, @0))
1761 (with { tree cst = const_binop (outer_op == inner_op
1762 ? PLUS_EXPR : MINUS_EXPR,
1764 (if (cst && !TREE_OVERFLOW (cst))
1765 (inner_op @0 { cst; } )
1766 /* X+INT_MAX+1 is X-INT_MIN. */
1767 (if (INTEGRAL_TYPE_P (type) && cst
1768 && wi::to_wide (cst) == wi::min_value (type))
1769 (neg_inner_op @0 { wide_int_to_tree (type, wi::to_wide (cst)); })
1770 /* Last resort, use some unsigned type. */
1771 (with { tree utype = unsigned_type_for (type); }
1772 (view_convert (inner_op
1773 (view_convert:utype @0)
1775 { drop_tree_overflow (cst); })))))))))))))
1777 /* (CST1 - A) +- CST2 -> CST3 - A */
1778 (for outer_op (plus minus)
1780 (outer_op (minus CONSTANT_CLASS_P@1 @0) CONSTANT_CLASS_P@2)
1781 (with { tree cst = const_binop (outer_op, type, @1, @2); }
1782 (if (cst && !TREE_OVERFLOW (cst))
1783 (minus { cst; } @0)))))
1785 /* CST1 - (CST2 - A) -> CST3 + A */
1787 (minus CONSTANT_CLASS_P@1 (minus CONSTANT_CLASS_P@2 @0))
1788 (with { tree cst = const_binop (MINUS_EXPR, type, @1, @2); }
1789 (if (cst && !TREE_OVERFLOW (cst))
1790 (plus { cst; } @0))))
1794 (plus:c (bit_not @0) @0)
1795 (if (!TYPE_OVERFLOW_TRAPS (type))
1796 { build_all_ones_cst (type); }))
1800 (plus (convert? (bit_not @0)) integer_each_onep)
1801 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1802 (negate (convert @0))))
1806 (minus (convert? (negate @0)) integer_each_onep)
1807 (if (!TYPE_OVERFLOW_TRAPS (type)
1808 && tree_nop_conversion_p (type, TREE_TYPE (@0)))
1809 (bit_not (convert @0))))
1813 (minus integer_all_onesp @0)
1816 /* (T)(P + A) - (T)P -> (T) A */
1818 (minus (convert (plus:c @@0 @1))
1820 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
1821 /* For integer types, if A has a smaller type
1822 than T the result depends on the possible
1824 E.g. T=size_t, A=(unsigned)429497295, P>0.
1825 However, if an overflow in P + A would cause
1826 undefined behavior, we can assume that there
1828 || (INTEGRAL_TYPE_P (TREE_TYPE (@1))
1829 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))))
1832 (minus (convert (pointer_plus @@0 @1))
1834 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
1835 /* For pointer types, if the conversion of A to the
1836 final type requires a sign- or zero-extension,
1837 then we have to punt - it is not defined which
1839 || (POINTER_TYPE_P (TREE_TYPE (@0))
1840 && TREE_CODE (@1) == INTEGER_CST
1841 && tree_int_cst_sign_bit (@1) == 0))
1844 (pointer_diff (pointer_plus @@0 @1) @0)
1845 /* The second argument of pointer_plus must be interpreted as signed, and
1846 thus sign-extended if necessary. */
1847 (with { tree stype = signed_type_for (TREE_TYPE (@1)); }
1848 /* Use view_convert instead of convert here, as POINTER_PLUS_EXPR
1849 second arg is unsigned even when we need to consider it as signed,
1850 we don't want to diagnose overflow here. */
1851 (convert (view_convert:stype @1))))
1853 /* (T)P - (T)(P + A) -> -(T) A */
1855 (minus (convert? @0)
1856 (convert (plus:c @@0 @1)))
1857 (if (INTEGRAL_TYPE_P (type)
1858 && TYPE_OVERFLOW_UNDEFINED (type)
1859 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
1860 (with { tree utype = unsigned_type_for (type); }
1861 (convert (negate (convert:utype @1))))
1862 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
1863 /* For integer types, if A has a smaller type
1864 than T the result depends on the possible
1866 E.g. T=size_t, A=(unsigned)429497295, P>0.
1867 However, if an overflow in P + A would cause
1868 undefined behavior, we can assume that there
1870 || (INTEGRAL_TYPE_P (TREE_TYPE (@1))
1871 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))))
1872 (negate (convert @1)))))
1875 (convert (pointer_plus @@0 @1)))
1876 (if (INTEGRAL_TYPE_P (type)
1877 && TYPE_OVERFLOW_UNDEFINED (type)
1878 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
1879 (with { tree utype = unsigned_type_for (type); }
1880 (convert (negate (convert:utype @1))))
1881 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
1882 /* For pointer types, if the conversion of A to the
1883 final type requires a sign- or zero-extension,
1884 then we have to punt - it is not defined which
1886 || (POINTER_TYPE_P (TREE_TYPE (@0))
1887 && TREE_CODE (@1) == INTEGER_CST
1888 && tree_int_cst_sign_bit (@1) == 0))
1889 (negate (convert @1)))))
1891 (pointer_diff @0 (pointer_plus @@0 @1))
1892 /* The second argument of pointer_plus must be interpreted as signed, and
1893 thus sign-extended if necessary. */
1894 (with { tree stype = signed_type_for (TREE_TYPE (@1)); }
1895 /* Use view_convert instead of convert here, as POINTER_PLUS_EXPR
1896 second arg is unsigned even when we need to consider it as signed,
1897 we don't want to diagnose overflow here. */
1898 (negate (convert (view_convert:stype @1)))))
1900 /* (T)(P + A) - (T)(P + B) -> (T)A - (T)B */
1902 (minus (convert (plus:c @@0 @1))
1903 (convert (plus:c @0 @2)))
1904 (if (INTEGRAL_TYPE_P (type)
1905 && TYPE_OVERFLOW_UNDEFINED (type)
1906 && element_precision (type) <= element_precision (TREE_TYPE (@1))
1907 && element_precision (type) <= element_precision (TREE_TYPE (@2)))
1908 (with { tree utype = unsigned_type_for (type); }
1909 (convert (minus (convert:utype @1) (convert:utype @2))))
1910 (if (((element_precision (type) <= element_precision (TREE_TYPE (@1)))
1911 == (element_precision (type) <= element_precision (TREE_TYPE (@2))))
1912 && (element_precision (type) <= element_precision (TREE_TYPE (@1))
1913 /* For integer types, if A has a smaller type
1914 than T the result depends on the possible
1916 E.g. T=size_t, A=(unsigned)429497295, P>0.
1917 However, if an overflow in P + A would cause
1918 undefined behavior, we can assume that there
1920 || (INTEGRAL_TYPE_P (TREE_TYPE (@1))
1921 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
1922 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))
1923 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@2)))))
1924 (minus (convert @1) (convert @2)))))
1926 (minus (convert (pointer_plus @@0 @1))
1927 (convert (pointer_plus @0 @2)))
1928 (if (INTEGRAL_TYPE_P (type)
1929 && TYPE_OVERFLOW_UNDEFINED (type)
1930 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
1931 (with { tree utype = unsigned_type_for (type); }
1932 (convert (minus (convert:utype @1) (convert:utype @2))))
1933 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
1934 /* For pointer types, if the conversion of A to the
1935 final type requires a sign- or zero-extension,
1936 then we have to punt - it is not defined which
1938 || (POINTER_TYPE_P (TREE_TYPE (@0))
1939 && TREE_CODE (@1) == INTEGER_CST
1940 && tree_int_cst_sign_bit (@1) == 0
1941 && TREE_CODE (@2) == INTEGER_CST
1942 && tree_int_cst_sign_bit (@2) == 0))
1943 (minus (convert @1) (convert @2)))))
1945 (pointer_diff (pointer_plus @@0 @1) (pointer_plus @0 @2))
1946 /* The second argument of pointer_plus must be interpreted as signed, and
1947 thus sign-extended if necessary. */
1948 (with { tree stype = signed_type_for (TREE_TYPE (@1)); }
1949 /* Use view_convert instead of convert here, as POINTER_PLUS_EXPR
1950 second arg is unsigned even when we need to consider it as signed,
1951 we don't want to diagnose overflow here. */
1952 (minus (convert (view_convert:stype @1))
1953 (convert (view_convert:stype @2)))))))
1955 /* (A * C) +- (B * C) -> (A+-B) * C and (A * C) +- A -> A * (C+-1).
1956 Modeled after fold_plusminus_mult_expr. */
1957 (if (!TYPE_SATURATING (type)
1958 && (!FLOAT_TYPE_P (type) || flag_associative_math))
1959 (for plusminus (plus minus)
1961 (plusminus (mult:cs@3 @0 @1) (mult:cs@4 @0 @2))
1962 (if ((!ANY_INTEGRAL_TYPE_P (type)
1963 || TYPE_OVERFLOW_WRAPS (type)
1964 || (INTEGRAL_TYPE_P (type)
1965 && tree_expr_nonzero_p (@0)
1966 && expr_not_equal_to (@0, wi::minus_one (TYPE_PRECISION (type)))))
1967 /* If @1 +- @2 is constant require a hard single-use on either
1968 original operand (but not on both). */
1969 && (single_use (@3) || single_use (@4)))
1970 (mult (plusminus @1 @2) @0)))
1971 /* We cannot generate constant 1 for fract. */
1972 (if (!ALL_FRACT_MODE_P (TYPE_MODE (type)))
1974 (plusminus @0 (mult:c@3 @0 @2))
1975 (if ((!ANY_INTEGRAL_TYPE_P (type)
1976 || TYPE_OVERFLOW_WRAPS (type)
1977 || (INTEGRAL_TYPE_P (type)
1978 && tree_expr_nonzero_p (@0)
1979 && expr_not_equal_to (@0, wi::minus_one (TYPE_PRECISION (type)))))
1981 (mult (plusminus { build_one_cst (type); } @2) @0)))
1983 (plusminus (mult:c@3 @0 @2) @0)
1984 (if ((!ANY_INTEGRAL_TYPE_P (type)
1985 || TYPE_OVERFLOW_WRAPS (type)
1986 || (INTEGRAL_TYPE_P (type)
1987 && tree_expr_nonzero_p (@0)
1988 && expr_not_equal_to (@0, wi::minus_one (TYPE_PRECISION (type)))))
1990 (mult (plusminus @2 { build_one_cst (type); }) @0))))))
1992 /* Simplifications of MIN_EXPR, MAX_EXPR, fmin() and fmax(). */
1994 (for minmax (min max FMIN_ALL FMAX_ALL)
1998 /* min(max(x,y),y) -> y. */
2000 (min:c (max:c @0 @1) @1)
2002 /* max(min(x,y),y) -> y. */
2004 (max:c (min:c @0 @1) @1)
2006 /* max(a,-a) -> abs(a). */
2008 (max:c @0 (negate @0))
2009 (if (TREE_CODE (type) != COMPLEX_TYPE
2010 && (! ANY_INTEGRAL_TYPE_P (type)
2011 || TYPE_OVERFLOW_UNDEFINED (type)))
2013 /* min(a,-a) -> -abs(a). */
2015 (min:c @0 (negate @0))
2016 (if (TREE_CODE (type) != COMPLEX_TYPE
2017 && (! ANY_INTEGRAL_TYPE_P (type)
2018 || TYPE_OVERFLOW_UNDEFINED (type)))
2023 (if (INTEGRAL_TYPE_P (type)
2024 && TYPE_MIN_VALUE (type)
2025 && operand_equal_p (@1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
2027 (if (INTEGRAL_TYPE_P (type)
2028 && TYPE_MAX_VALUE (type)
2029 && operand_equal_p (@1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
2034 (if (INTEGRAL_TYPE_P (type)
2035 && TYPE_MAX_VALUE (type)
2036 && operand_equal_p (@1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
2038 (if (INTEGRAL_TYPE_P (type)
2039 && TYPE_MIN_VALUE (type)
2040 && operand_equal_p (@1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
2043 /* max (a, a + CST) -> a + CST where CST is positive. */
2044 /* max (a, a + CST) -> a where CST is negative. */
2046 (max:c @0 (plus@2 @0 INTEGER_CST@1))
2047 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
2048 (if (tree_int_cst_sgn (@1) > 0)
2052 /* min (a, a + CST) -> a where CST is positive. */
2053 /* min (a, a + CST) -> a + CST where CST is negative. */
2055 (min:c @0 (plus@2 @0 INTEGER_CST@1))
2056 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
2057 (if (tree_int_cst_sgn (@1) > 0)
2061 /* (convert (minmax ((convert (x) c)))) -> minmax (x c) if x is promoted
2062 and the outer convert demotes the expression back to x's type. */
2063 (for minmax (min max)
2065 (convert (minmax@0 (convert @1) INTEGER_CST@2))
2066 (if (INTEGRAL_TYPE_P (type)
2067 && types_match (@1, type) && int_fits_type_p (@2, type)
2068 && TYPE_SIGN (TREE_TYPE (@0)) == TYPE_SIGN (type)
2069 && TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type))
2070 (minmax @1 (convert @2)))))
2072 (for minmax (FMIN_ALL FMAX_ALL)
2073 /* If either argument is NaN, return the other one. Avoid the
2074 transformation if we get (and honor) a signalling NaN. */
2076 (minmax:c @0 REAL_CST@1)
2077 (if (real_isnan (TREE_REAL_CST_PTR (@1))
2078 && (!HONOR_SNANS (@1) || !TREE_REAL_CST (@1).signalling))
2080 /* Convert fmin/fmax to MIN_EXPR/MAX_EXPR. C99 requires these
2081 functions to return the numeric arg if the other one is NaN.
2082 MIN and MAX don't honor that, so only transform if -ffinite-math-only
2083 is set. C99 doesn't require -0.0 to be handled, so we don't have to
2084 worry about it either. */
2085 (if (flag_finite_math_only)
2092 /* min (-A, -B) -> -max (A, B) */
2093 (for minmax (min max FMIN_ALL FMAX_ALL)
2094 maxmin (max min FMAX_ALL FMIN_ALL)
2096 (minmax (negate:s@2 @0) (negate:s@3 @1))
2097 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
2098 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2099 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
2100 (negate (maxmin @0 @1)))))
2101 /* MIN (~X, ~Y) -> ~MAX (X, Y)
2102 MAX (~X, ~Y) -> ~MIN (X, Y) */
2103 (for minmax (min max)
2106 (minmax (bit_not:s@2 @0) (bit_not:s@3 @1))
2107 (bit_not (maxmin @0 @1))))
2109 /* MIN (X, Y) == X -> X <= Y */
2110 (for minmax (min min max max)
2114 (cmp:c (minmax:c @0 @1) @0)
2115 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0)))
2117 /* MIN (X, 5) == 0 -> X == 0
2118 MIN (X, 5) == 7 -> false */
2121 (cmp (min @0 INTEGER_CST@1) INTEGER_CST@2)
2122 (if (wi::lt_p (wi::to_wide (@1), wi::to_wide (@2),
2123 TYPE_SIGN (TREE_TYPE (@0))))
2124 { constant_boolean_node (cmp == NE_EXPR, type); }
2125 (if (wi::gt_p (wi::to_wide (@1), wi::to_wide (@2),
2126 TYPE_SIGN (TREE_TYPE (@0))))
2130 (cmp (max @0 INTEGER_CST@1) INTEGER_CST@2)
2131 (if (wi::gt_p (wi::to_wide (@1), wi::to_wide (@2),
2132 TYPE_SIGN (TREE_TYPE (@0))))
2133 { constant_boolean_node (cmp == NE_EXPR, type); }
2134 (if (wi::lt_p (wi::to_wide (@1), wi::to_wide (@2),
2135 TYPE_SIGN (TREE_TYPE (@0))))
2137 /* MIN (X, C1) < C2 -> X < C2 || C1 < C2 */
2138 (for minmax (min min max max min min max max )
2139 cmp (lt le gt ge gt ge lt le )
2140 comb (bit_ior bit_ior bit_ior bit_ior bit_and bit_and bit_and bit_and)
2142 (cmp (minmax @0 INTEGER_CST@1) INTEGER_CST@2)
2143 (comb (cmp @0 @2) (cmp @1 @2))))
2145 /* Simplifications of shift and rotates. */
2147 (for rotate (lrotate rrotate)
2149 (rotate integer_all_onesp@0 @1)
2152 /* Optimize -1 >> x for arithmetic right shifts. */
2154 (rshift integer_all_onesp@0 @1)
2155 (if (!TYPE_UNSIGNED (type)
2156 && tree_expr_nonnegative_p (@1))
2159 /* Optimize (x >> c) << c into x & (-1<<c). */
2161 (lshift (rshift @0 INTEGER_CST@1) @1)
2162 (if (wi::ltu_p (wi::to_wide (@1), element_precision (type)))
2163 (bit_and @0 (lshift { build_minus_one_cst (type); } @1))))
2165 /* Optimize (x << c) >> c into x & ((unsigned)-1 >> c) for unsigned
2168 (rshift (lshift @0 INTEGER_CST@1) @1)
2169 (if (TYPE_UNSIGNED (type)
2170 && (wi::ltu_p (wi::to_wide (@1), element_precision (type))))
2171 (bit_and @0 (rshift { build_minus_one_cst (type); } @1))))
2173 (for shiftrotate (lrotate rrotate lshift rshift)
2175 (shiftrotate @0 integer_zerop)
2178 (shiftrotate integer_zerop@0 @1)
2180 /* Prefer vector1 << scalar to vector1 << vector2
2181 if vector2 is uniform. */
2182 (for vec (VECTOR_CST CONSTRUCTOR)
2184 (shiftrotate @0 vec@1)
2185 (with { tree tem = uniform_vector_p (@1); }
2187 (shiftrotate @0 { tem; }))))))
2189 /* Simplify X << Y where Y's low width bits are 0 to X, as only valid
2190 Y is 0. Similarly for X >> Y. */
2192 (for shift (lshift rshift)
2194 (shift @0 SSA_NAME@1)
2195 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1)))
2197 int width = ceil_log2 (element_precision (TREE_TYPE (@0)));
2198 int prec = TYPE_PRECISION (TREE_TYPE (@1));
2200 (if ((get_nonzero_bits (@1) & wi::mask (width, false, prec)) == 0)
2204 /* Rewrite an LROTATE_EXPR by a constant into an
2205 RROTATE_EXPR by a new constant. */
2207 (lrotate @0 INTEGER_CST@1)
2208 (rrotate @0 { const_binop (MINUS_EXPR, TREE_TYPE (@1),
2209 build_int_cst (TREE_TYPE (@1),
2210 element_precision (type)), @1); }))
2212 /* Turn (a OP c1) OP c2 into a OP (c1+c2). */
2213 (for op (lrotate rrotate rshift lshift)
2215 (op (op @0 INTEGER_CST@1) INTEGER_CST@2)
2216 (with { unsigned int prec = element_precision (type); }
2217 (if (wi::ge_p (wi::to_wide (@1), 0, TYPE_SIGN (TREE_TYPE (@1)))
2218 && wi::lt_p (wi::to_wide (@1), prec, TYPE_SIGN (TREE_TYPE (@1)))
2219 && wi::ge_p (wi::to_wide (@2), 0, TYPE_SIGN (TREE_TYPE (@2)))
2220 && wi::lt_p (wi::to_wide (@2), prec, TYPE_SIGN (TREE_TYPE (@2))))
2221 (with { unsigned int low = (tree_to_uhwi (@1)
2222 + tree_to_uhwi (@2)); }
2223 /* Deal with a OP (c1 + c2) being undefined but (a OP c1) OP c2
2224 being well defined. */
2226 (if (op == LROTATE_EXPR || op == RROTATE_EXPR)
2227 (op @0 { build_int_cst (TREE_TYPE (@1), low % prec); })
2228 (if (TYPE_UNSIGNED (type) || op == LSHIFT_EXPR)
2229 { build_zero_cst (type); }
2230 (op @0 { build_int_cst (TREE_TYPE (@1), prec - 1); })))
2231 (op @0 { build_int_cst (TREE_TYPE (@1), low); })))))))
2234 /* ((1 << A) & 1) != 0 -> A == 0
2235 ((1 << A) & 1) == 0 -> A != 0 */
2239 (cmp (bit_and (lshift integer_onep @0) integer_onep) integer_zerop)
2240 (icmp @0 { build_zero_cst (TREE_TYPE (@0)); })))
2242 /* (CST1 << A) == CST2 -> A == ctz (CST2) - ctz (CST1)
2243 (CST1 << A) != CST2 -> A != ctz (CST2) - ctz (CST1)
2247 (cmp (lshift INTEGER_CST@0 @1) INTEGER_CST@2)
2248 (with { int cand = wi::ctz (wi::to_wide (@2)) - wi::ctz (wi::to_wide (@0)); }
2250 || (!integer_zerop (@2)
2251 && wi::lshift (wi::to_wide (@0), cand) != wi::to_wide (@2)))
2252 { constant_boolean_node (cmp == NE_EXPR, type); }
2253 (if (!integer_zerop (@2)
2254 && wi::lshift (wi::to_wide (@0), cand) == wi::to_wide (@2))
2255 (cmp @1 { build_int_cst (TREE_TYPE (@1), cand); }))))))
2257 /* Fold (X << C1) & C2 into (X << C1) & (C2 | ((1 << C1) - 1))
2258 (X >> C1) & C2 into (X >> C1) & (C2 | ~((type) -1 >> C1))
2259 if the new mask might be further optimized. */
2260 (for shift (lshift rshift)
2262 (bit_and (convert?:s@4 (shift:s@5 (convert1?@3 @0) INTEGER_CST@1))
2264 (if (tree_nop_conversion_p (TREE_TYPE (@4), TREE_TYPE (@5))
2265 && TYPE_PRECISION (type) <= HOST_BITS_PER_WIDE_INT
2266 && tree_fits_uhwi_p (@1)
2267 && tree_to_uhwi (@1) > 0
2268 && tree_to_uhwi (@1) < TYPE_PRECISION (type))
2271 unsigned int shiftc = tree_to_uhwi (@1);
2272 unsigned HOST_WIDE_INT mask = TREE_INT_CST_LOW (@2);
2273 unsigned HOST_WIDE_INT newmask, zerobits = 0;
2274 tree shift_type = TREE_TYPE (@3);
2277 if (shift == LSHIFT_EXPR)
2278 zerobits = ((HOST_WIDE_INT_1U << shiftc) - 1);
2279 else if (shift == RSHIFT_EXPR
2280 && type_has_mode_precision_p (shift_type))
2282 prec = TYPE_PRECISION (TREE_TYPE (@3));
2284 /* See if more bits can be proven as zero because of
2287 && TYPE_UNSIGNED (TREE_TYPE (@0)))
2289 tree inner_type = TREE_TYPE (@0);
2290 if (type_has_mode_precision_p (inner_type)
2291 && TYPE_PRECISION (inner_type) < prec)
2293 prec = TYPE_PRECISION (inner_type);
2294 /* See if we can shorten the right shift. */
2296 shift_type = inner_type;
2297 /* Otherwise X >> C1 is all zeros, so we'll optimize
2298 it into (X, 0) later on by making sure zerobits
2302 zerobits = HOST_WIDE_INT_M1U;
2305 zerobits >>= HOST_BITS_PER_WIDE_INT - shiftc;
2306 zerobits <<= prec - shiftc;
2308 /* For arithmetic shift if sign bit could be set, zerobits
2309 can contain actually sign bits, so no transformation is
2310 possible, unless MASK masks them all away. In that
2311 case the shift needs to be converted into logical shift. */
2312 if (!TYPE_UNSIGNED (TREE_TYPE (@3))
2313 && prec == TYPE_PRECISION (TREE_TYPE (@3)))
2315 if ((mask & zerobits) == 0)
2316 shift_type = unsigned_type_for (TREE_TYPE (@3));
2322 /* ((X << 16) & 0xff00) is (X, 0). */
2323 (if ((mask & zerobits) == mask)
2324 { build_int_cst (type, 0); }
2325 (with { newmask = mask | zerobits; }
2326 (if (newmask != mask && (newmask & (newmask + 1)) == 0)
2329 /* Only do the transformation if NEWMASK is some integer
2331 for (prec = BITS_PER_UNIT;
2332 prec < HOST_BITS_PER_WIDE_INT; prec <<= 1)
2333 if (newmask == (HOST_WIDE_INT_1U << prec) - 1)
2336 (if (prec < HOST_BITS_PER_WIDE_INT
2337 || newmask == HOST_WIDE_INT_M1U)
2339 { tree newmaskt = build_int_cst_type (TREE_TYPE (@2), newmask); }
2340 (if (!tree_int_cst_equal (newmaskt, @2))
2341 (if (shift_type != TREE_TYPE (@3))
2342 (bit_and (convert (shift:shift_type (convert @3) @1)) { newmaskt; })
2343 (bit_and @4 { newmaskt; })))))))))))))
2345 /* Fold (X {&,^,|} C2) << C1 into (X << C1) {&,^,|} (C2 << C1)
2346 (X {&,^,|} C2) >> C1 into (X >> C1) & (C2 >> C1). */
2347 (for shift (lshift rshift)
2348 (for bit_op (bit_and bit_xor bit_ior)
2350 (shift (convert?:s (bit_op:s @0 INTEGER_CST@2)) INTEGER_CST@1)
2351 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2352 (with { tree mask = int_const_binop (shift, fold_convert (type, @2), @1); }
2353 (bit_op (shift (convert @0) @1) { mask; }))))))
2355 /* ~(~X >> Y) -> X >> Y (for arithmetic shift). */
2357 (bit_not (convert1?:s (rshift:s (convert2?@0 (bit_not @1)) @2)))
2358 (if (!TYPE_UNSIGNED (TREE_TYPE (@0))
2359 && (element_precision (TREE_TYPE (@0))
2360 <= element_precision (TREE_TYPE (@1))
2361 || !TYPE_UNSIGNED (TREE_TYPE (@1))))
2363 { tree shift_type = TREE_TYPE (@0); }
2364 (convert (rshift (convert:shift_type @1) @2)))))
2366 /* ~(~X >>r Y) -> X >>r Y
2367 ~(~X <<r Y) -> X <<r Y */
2368 (for rotate (lrotate rrotate)
2370 (bit_not (convert1?:s (rotate:s (convert2?@0 (bit_not @1)) @2)))
2371 (if ((element_precision (TREE_TYPE (@0))
2372 <= element_precision (TREE_TYPE (@1))
2373 || !TYPE_UNSIGNED (TREE_TYPE (@1)))
2374 && (element_precision (type) <= element_precision (TREE_TYPE (@0))
2375 || !TYPE_UNSIGNED (TREE_TYPE (@0))))
2377 { tree rotate_type = TREE_TYPE (@0); }
2378 (convert (rotate (convert:rotate_type @1) @2))))))
2380 /* Simplifications of conversions. */
2382 /* Basic strip-useless-type-conversions / strip_nops. */
2383 (for cvt (convert view_convert float fix_trunc)
2386 (if ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@0)))
2387 || (GENERIC && type == TREE_TYPE (@0)))
2390 /* Contract view-conversions. */
2392 (view_convert (view_convert @0))
2395 /* For integral conversions with the same precision or pointer
2396 conversions use a NOP_EXPR instead. */
2399 (if ((INTEGRAL_TYPE_P (type) || POINTER_TYPE_P (type))
2400 && (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
2401 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (@0)))
2404 /* Strip inner integral conversions that do not change precision or size, or
2405 zero-extend while keeping the same size (for bool-to-char). */
2407 (view_convert (convert@0 @1))
2408 (if ((INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
2409 && (INTEGRAL_TYPE_P (TREE_TYPE (@1)) || POINTER_TYPE_P (TREE_TYPE (@1)))
2410 && TYPE_SIZE (TREE_TYPE (@0)) == TYPE_SIZE (TREE_TYPE (@1))
2411 && (TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1))
2412 || (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (TREE_TYPE (@1))
2413 && TYPE_UNSIGNED (TREE_TYPE (@1)))))
2416 /* Re-association barriers around constants and other re-association
2417 barriers can be removed. */
2419 (paren CONSTANT_CLASS_P@0)
2422 (paren (paren@1 @0))
2425 /* Handle cases of two conversions in a row. */
2426 (for ocvt (convert float fix_trunc)
2427 (for icvt (convert float)
2432 tree inside_type = TREE_TYPE (@0);
2433 tree inter_type = TREE_TYPE (@1);
2434 int inside_int = INTEGRAL_TYPE_P (inside_type);
2435 int inside_ptr = POINTER_TYPE_P (inside_type);
2436 int inside_float = FLOAT_TYPE_P (inside_type);
2437 int inside_vec = VECTOR_TYPE_P (inside_type);
2438 unsigned int inside_prec = TYPE_PRECISION (inside_type);
2439 int inside_unsignedp = TYPE_UNSIGNED (inside_type);
2440 int inter_int = INTEGRAL_TYPE_P (inter_type);
2441 int inter_ptr = POINTER_TYPE_P (inter_type);
2442 int inter_float = FLOAT_TYPE_P (inter_type);
2443 int inter_vec = VECTOR_TYPE_P (inter_type);
2444 unsigned int inter_prec = TYPE_PRECISION (inter_type);
2445 int inter_unsignedp = TYPE_UNSIGNED (inter_type);
2446 int final_int = INTEGRAL_TYPE_P (type);
2447 int final_ptr = POINTER_TYPE_P (type);
2448 int final_float = FLOAT_TYPE_P (type);
2449 int final_vec = VECTOR_TYPE_P (type);
2450 unsigned int final_prec = TYPE_PRECISION (type);
2451 int final_unsignedp = TYPE_UNSIGNED (type);
2454 /* In addition to the cases of two conversions in a row
2455 handled below, if we are converting something to its own
2456 type via an object of identical or wider precision, neither
2457 conversion is needed. */
2458 (if (((GIMPLE && useless_type_conversion_p (type, inside_type))
2460 && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (inside_type)))
2461 && (((inter_int || inter_ptr) && final_int)
2462 || (inter_float && final_float))
2463 && inter_prec >= final_prec)
2466 /* Likewise, if the intermediate and initial types are either both
2467 float or both integer, we don't need the middle conversion if the
2468 former is wider than the latter and doesn't change the signedness
2469 (for integers). Avoid this if the final type is a pointer since
2470 then we sometimes need the middle conversion. */
2471 (if (((inter_int && inside_int) || (inter_float && inside_float))
2472 && (final_int || final_float)
2473 && inter_prec >= inside_prec
2474 && (inter_float || inter_unsignedp == inside_unsignedp))
2477 /* If we have a sign-extension of a zero-extended value, we can
2478 replace that by a single zero-extension. Likewise if the
2479 final conversion does not change precision we can drop the
2480 intermediate conversion. */
2481 (if (inside_int && inter_int && final_int
2482 && ((inside_prec < inter_prec && inter_prec < final_prec
2483 && inside_unsignedp && !inter_unsignedp)
2484 || final_prec == inter_prec))
2487 /* Two conversions in a row are not needed unless:
2488 - some conversion is floating-point (overstrict for now), or
2489 - some conversion is a vector (overstrict for now), or
2490 - the intermediate type is narrower than both initial and
2492 - the intermediate type and innermost type differ in signedness,
2493 and the outermost type is wider than the intermediate, or
2494 - the initial type is a pointer type and the precisions of the
2495 intermediate and final types differ, or
2496 - the final type is a pointer type and the precisions of the
2497 initial and intermediate types differ. */
2498 (if (! inside_float && ! inter_float && ! final_float
2499 && ! inside_vec && ! inter_vec && ! final_vec
2500 && (inter_prec >= inside_prec || inter_prec >= final_prec)
2501 && ! (inside_int && inter_int
2502 && inter_unsignedp != inside_unsignedp
2503 && inter_prec < final_prec)
2504 && ((inter_unsignedp && inter_prec > inside_prec)
2505 == (final_unsignedp && final_prec > inter_prec))
2506 && ! (inside_ptr && inter_prec != final_prec)
2507 && ! (final_ptr && inside_prec != inter_prec))
2510 /* A truncation to an unsigned type (a zero-extension) should be
2511 canonicalized as bitwise and of a mask. */
2512 (if (GIMPLE /* PR70366: doing this in GENERIC breaks -Wconversion. */
2513 && final_int && inter_int && inside_int
2514 && final_prec == inside_prec
2515 && final_prec > inter_prec
2517 (convert (bit_and @0 { wide_int_to_tree
2519 wi::mask (inter_prec, false,
2520 TYPE_PRECISION (inside_type))); })))
2522 /* If we are converting an integer to a floating-point that can
2523 represent it exactly and back to an integer, we can skip the
2524 floating-point conversion. */
2525 (if (GIMPLE /* PR66211 */
2526 && inside_int && inter_float && final_int &&
2527 (unsigned) significand_size (TYPE_MODE (inter_type))
2528 >= inside_prec - !inside_unsignedp)
2531 /* If we have a narrowing conversion to an integral type that is fed by a
2532 BIT_AND_EXPR, we might be able to remove the BIT_AND_EXPR if it merely
2533 masks off bits outside the final type (and nothing else). */
2535 (convert (bit_and @0 INTEGER_CST@1))
2536 (if (INTEGRAL_TYPE_P (type)
2537 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
2538 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))
2539 && operand_equal_p (@1, build_low_bits_mask (TREE_TYPE (@1),
2540 TYPE_PRECISION (type)), 0))
2544 /* (X /[ex] A) * A -> X. */
2546 (mult (convert1? (exact_div @0 @@1)) (convert2? @1))
2549 /* Canonicalization of binary operations. */
2551 /* Convert X + -C into X - C. */
2553 (plus @0 REAL_CST@1)
2554 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
2555 (with { tree tem = const_unop (NEGATE_EXPR, type, @1); }
2556 (if (!TREE_OVERFLOW (tem) || !flag_trapping_math)
2557 (minus @0 { tem; })))))
2559 /* Convert x+x into x*2. */
2562 (if (SCALAR_FLOAT_TYPE_P (type))
2563 (mult @0 { build_real (type, dconst2); })
2564 (if (INTEGRAL_TYPE_P (type))
2565 (mult @0 { build_int_cst (type, 2); }))))
2569 (minus integer_zerop @1)
2572 (pointer_diff integer_zerop @1)
2573 (negate (convert @1)))
2575 /* (ARG0 - ARG1) is the same as (-ARG1 + ARG0). So check whether
2576 ARG0 is zero and X + ARG0 reduces to X, since that would mean
2577 (-ARG1 + ARG0) reduces to -ARG1. */
2579 (minus real_zerop@0 @1)
2580 (if (fold_real_zero_addition_p (type, @0, 0))
2583 /* Transform x * -1 into -x. */
2585 (mult @0 integer_minus_onep)
2588 /* Reassociate (X * CST) * Y to (X * Y) * CST. This does not introduce
2589 signed overflow for CST != 0 && CST != -1. */
2591 (mult:c (mult:s@3 @0 INTEGER_CST@1) @2)
2592 (if (TREE_CODE (@2) != INTEGER_CST
2594 && !integer_zerop (@1) && !integer_minus_onep (@1))
2595 (mult (mult @0 @2) @1)))
2597 /* True if we can easily extract the real and imaginary parts of a complex
2599 (match compositional_complex
2600 (convert? (complex @0 @1)))
2602 /* COMPLEX_EXPR and REALPART/IMAGPART_EXPR cancellations. */
2604 (complex (realpart @0) (imagpart @0))
2607 (realpart (complex @0 @1))
2610 (imagpart (complex @0 @1))
2613 /* Sometimes we only care about half of a complex expression. */
2615 (realpart (convert?:s (conj:s @0)))
2616 (convert (realpart @0)))
2618 (imagpart (convert?:s (conj:s @0)))
2619 (convert (negate (imagpart @0))))
2620 (for part (realpart imagpart)
2621 (for op (plus minus)
2623 (part (convert?:s@2 (op:s @0 @1)))
2624 (convert (op (part @0) (part @1))))))
2626 (realpart (convert?:s (CEXPI:s @0)))
2629 (imagpart (convert?:s (CEXPI:s @0)))
2632 /* conj(conj(x)) -> x */
2634 (conj (convert? (conj @0)))
2635 (if (tree_nop_conversion_p (TREE_TYPE (@0), type))
2638 /* conj({x,y}) -> {x,-y} */
2640 (conj (convert?:s (complex:s @0 @1)))
2641 (with { tree itype = TREE_TYPE (type); }
2642 (complex (convert:itype @0) (negate (convert:itype @1)))))
2644 /* BSWAP simplifications, transforms checked by gcc.dg/builtin-bswap-8.c. */
2645 (for bswap (BUILT_IN_BSWAP16 BUILT_IN_BSWAP32 BUILT_IN_BSWAP64)
2650 (bswap (bit_not (bswap @0)))
2652 (for bitop (bit_xor bit_ior bit_and)
2654 (bswap (bitop:c (bswap @0) @1))
2655 (bitop @0 (bswap @1)))))
2658 /* Combine COND_EXPRs and VEC_COND_EXPRs. */
2660 /* Simplify constant conditions.
2661 Only optimize constant conditions when the selected branch
2662 has the same type as the COND_EXPR. This avoids optimizing
2663 away "c ? x : throw", where the throw has a void type.
2664 Note that we cannot throw away the fold-const.c variant nor
2665 this one as we depend on doing this transform before possibly
2666 A ? B : B -> B triggers and the fold-const.c one can optimize
2667 0 ? A : B to B even if A has side-effects. Something
2668 genmatch cannot handle. */
2670 (cond INTEGER_CST@0 @1 @2)
2671 (if (integer_zerop (@0))
2672 (if (!VOID_TYPE_P (TREE_TYPE (@2)) || VOID_TYPE_P (type))
2674 (if (!VOID_TYPE_P (TREE_TYPE (@1)) || VOID_TYPE_P (type))
2677 (vec_cond VECTOR_CST@0 @1 @2)
2678 (if (integer_all_onesp (@0))
2680 (if (integer_zerop (@0))
2683 /* Simplification moved from fold_cond_expr_with_comparison. It may also
2685 /* This pattern implements two kinds simplification:
2688 (cond (cmp (convert1? x) c1) (convert2? x) c2) -> (minmax (x c)) if:
2689 1) Conversions are type widening from smaller type.
2690 2) Const c1 equals to c2 after canonicalizing comparison.
2691 3) Comparison has tree code LT, LE, GT or GE.
2692 This specific pattern is needed when (cmp (convert x) c) may not
2693 be simplified by comparison patterns because of multiple uses of
2694 x. It also makes sense here because simplifying across multiple
2695 referred var is always benefitial for complicated cases.
2698 (cond (eq (convert1? x) c1) (convert2? x) c2) -> (cond (eq x c1) c1 c2). */
2699 (for cmp (lt le gt ge eq)
2701 (cond (cmp (convert1? @1) INTEGER_CST@3) (convert2? @1) INTEGER_CST@2)
2704 tree from_type = TREE_TYPE (@1);
2705 tree c1_type = TREE_TYPE (@3), c2_type = TREE_TYPE (@2);
2706 enum tree_code code = ERROR_MARK;
2708 if (INTEGRAL_TYPE_P (from_type)
2709 && int_fits_type_p (@2, from_type)
2710 && (types_match (c1_type, from_type)
2711 || (TYPE_PRECISION (c1_type) > TYPE_PRECISION (from_type)
2712 && (TYPE_UNSIGNED (from_type)
2713 || TYPE_SIGN (c1_type) == TYPE_SIGN (from_type))))
2714 && (types_match (c2_type, from_type)
2715 || (TYPE_PRECISION (c2_type) > TYPE_PRECISION (from_type)
2716 && (TYPE_UNSIGNED (from_type)
2717 || TYPE_SIGN (c2_type) == TYPE_SIGN (from_type)))))
2721 if (wi::to_widest (@3) == (wi::to_widest (@2) - 1))
2723 /* X <= Y - 1 equals to X < Y. */
2726 /* X > Y - 1 equals to X >= Y. */
2730 if (wi::to_widest (@3) == (wi::to_widest (@2) + 1))
2732 /* X < Y + 1 equals to X <= Y. */
2735 /* X >= Y + 1 equals to X > Y. */
2739 if (code != ERROR_MARK
2740 || wi::to_widest (@2) == wi::to_widest (@3))
2742 if (cmp == LT_EXPR || cmp == LE_EXPR)
2744 if (cmp == GT_EXPR || cmp == GE_EXPR)
2748 /* Can do A == C1 ? A : C2 -> A == C1 ? C1 : C2? */
2749 else if (int_fits_type_p (@3, from_type))
2753 (if (code == MAX_EXPR)
2754 (convert (max @1 (convert @2)))
2755 (if (code == MIN_EXPR)
2756 (convert (min @1 (convert @2)))
2757 (if (code == EQ_EXPR)
2758 (convert (cond (eq @1 (convert @3))
2759 (convert:from_type @3) (convert:from_type @2)))))))))
2761 /* (cond (cmp (convert? x) c1) (op x c2) c3) -> (op (minmax x c1) c2) if:
2763 1) OP is PLUS or MINUS.
2764 2) CMP is LT, LE, GT or GE.
2765 3) C3 == (C1 op C2), and computation doesn't have undefined behavior.
2767 This pattern also handles special cases like:
2769 A) Operand x is a unsigned to signed type conversion and c1 is
2770 integer zero. In this case,
2771 (signed type)x < 0 <=> x > MAX_VAL(signed type)
2772 (signed type)x >= 0 <=> x <= MAX_VAL(signed type)
2773 B) Const c1 may not equal to (C3 op' C2). In this case we also
2774 check equality for (c1+1) and (c1-1) by adjusting comparison
2777 TODO: Though signed type is handled by this pattern, it cannot be
2778 simplified at the moment because C standard requires additional
2779 type promotion. In order to match&simplify it here, the IR needs
2780 to be cleaned up by other optimizers, i.e, VRP. */
2781 (for op (plus minus)
2782 (for cmp (lt le gt ge)
2784 (cond (cmp (convert? @X) INTEGER_CST@1) (op @X INTEGER_CST@2) INTEGER_CST@3)
2785 (with { tree from_type = TREE_TYPE (@X), to_type = TREE_TYPE (@1); }
2786 (if (types_match (from_type, to_type)
2787 /* Check if it is special case A). */
2788 || (TYPE_UNSIGNED (from_type)
2789 && !TYPE_UNSIGNED (to_type)
2790 && TYPE_PRECISION (from_type) == TYPE_PRECISION (to_type)
2791 && integer_zerop (@1)
2792 && (cmp == LT_EXPR || cmp == GE_EXPR)))
2795 bool overflow = false;
2796 enum tree_code code, cmp_code = cmp;
2798 wide_int c1 = wi::to_wide (@1);
2799 wide_int c2 = wi::to_wide (@2);
2800 wide_int c3 = wi::to_wide (@3);
2801 signop sgn = TYPE_SIGN (from_type);
2803 /* Handle special case A), given x of unsigned type:
2804 ((signed type)x < 0) <=> (x > MAX_VAL(signed type))
2805 ((signed type)x >= 0) <=> (x <= MAX_VAL(signed type)) */
2806 if (!types_match (from_type, to_type))
2808 if (cmp_code == LT_EXPR)
2810 if (cmp_code == GE_EXPR)
2812 c1 = wi::max_value (to_type);
2814 /* To simplify this pattern, we require c3 = (c1 op c2). Here we
2815 compute (c3 op' c2) and check if it equals to c1 with op' being
2816 the inverted operator of op. Make sure overflow doesn't happen
2817 if it is undefined. */
2818 if (op == PLUS_EXPR)
2819 real_c1 = wi::sub (c3, c2, sgn, &overflow);
2821 real_c1 = wi::add (c3, c2, sgn, &overflow);
2824 if (!overflow || !TYPE_OVERFLOW_UNDEFINED (from_type))
2826 /* Check if c1 equals to real_c1. Boundary condition is handled
2827 by adjusting comparison operation if necessary. */
2828 if (!wi::cmp (wi::sub (real_c1, 1, sgn, &overflow), c1, sgn)
2831 /* X <= Y - 1 equals to X < Y. */
2832 if (cmp_code == LE_EXPR)
2834 /* X > Y - 1 equals to X >= Y. */
2835 if (cmp_code == GT_EXPR)
2838 if (!wi::cmp (wi::add (real_c1, 1, sgn, &overflow), c1, sgn)
2841 /* X < Y + 1 equals to X <= Y. */
2842 if (cmp_code == LT_EXPR)
2844 /* X >= Y + 1 equals to X > Y. */
2845 if (cmp_code == GE_EXPR)
2848 if (code != cmp_code || !wi::cmp (real_c1, c1, sgn))
2850 if (cmp_code == LT_EXPR || cmp_code == LE_EXPR)
2852 if (cmp_code == GT_EXPR || cmp_code == GE_EXPR)
2857 (if (code == MAX_EXPR)
2858 (op (max @X { wide_int_to_tree (from_type, real_c1); })
2859 { wide_int_to_tree (from_type, c2); })
2860 (if (code == MIN_EXPR)
2861 (op (min @X { wide_int_to_tree (from_type, real_c1); })
2862 { wide_int_to_tree (from_type, c2); })))))))))
2864 (for cnd (cond vec_cond)
2865 /* A ? B : (A ? X : C) -> A ? B : C. */
2867 (cnd @0 (cnd @0 @1 @2) @3)
2870 (cnd @0 @1 (cnd @0 @2 @3))
2872 /* A ? B : (!A ? C : X) -> A ? B : C. */
2873 /* ??? This matches embedded conditions open-coded because genmatch
2874 would generate matching code for conditions in separate stmts only.
2875 The following is still important to merge then and else arm cases
2876 from if-conversion. */
2878 (cnd @0 @1 (cnd @2 @3 @4))
2879 (if (COMPARISON_CLASS_P (@0)
2880 && COMPARISON_CLASS_P (@2)
2881 && invert_tree_comparison
2882 (TREE_CODE (@0), HONOR_NANS (TREE_OPERAND (@0, 0))) == TREE_CODE (@2)
2883 && operand_equal_p (TREE_OPERAND (@0, 0), TREE_OPERAND (@2, 0), 0)
2884 && operand_equal_p (TREE_OPERAND (@0, 1), TREE_OPERAND (@2, 1), 0))
2887 (cnd @0 (cnd @1 @2 @3) @4)
2888 (if (COMPARISON_CLASS_P (@0)
2889 && COMPARISON_CLASS_P (@1)
2890 && invert_tree_comparison
2891 (TREE_CODE (@0), HONOR_NANS (TREE_OPERAND (@0, 0))) == TREE_CODE (@1)
2892 && operand_equal_p (TREE_OPERAND (@0, 0), TREE_OPERAND (@1, 0), 0)
2893 && operand_equal_p (TREE_OPERAND (@0, 1), TREE_OPERAND (@1, 1), 0))
2896 /* A ? B : B -> B. */
2901 /* !A ? B : C -> A ? C : B. */
2903 (cnd (logical_inverted_value truth_valued_p@0) @1 @2)
2906 /* A + (B vcmp C ? 1 : 0) -> A - (B vcmp C ? -1 : 0), since vector comparisons
2907 return all -1 or all 0 results. */
2908 /* ??? We could instead convert all instances of the vec_cond to negate,
2909 but that isn't necessarily a win on its own. */
2911 (plus:c @3 (view_convert? (vec_cond:s @0 integer_each_onep@1 integer_zerop@2)))
2912 (if (VECTOR_TYPE_P (type)
2913 && known_eq (TYPE_VECTOR_SUBPARTS (type),
2914 TYPE_VECTOR_SUBPARTS (TREE_TYPE (@1)))
2915 && (TYPE_MODE (TREE_TYPE (type))
2916 == TYPE_MODE (TREE_TYPE (TREE_TYPE (@1)))))
2917 (minus @3 (view_convert (vec_cond @0 (negate @1) @2)))))
2919 /* ... likewise A - (B vcmp C ? 1 : 0) -> A + (B vcmp C ? -1 : 0). */
2921 (minus @3 (view_convert? (vec_cond:s @0 integer_each_onep@1 integer_zerop@2)))
2922 (if (VECTOR_TYPE_P (type)
2923 && known_eq (TYPE_VECTOR_SUBPARTS (type),
2924 TYPE_VECTOR_SUBPARTS (TREE_TYPE (@1)))
2925 && (TYPE_MODE (TREE_TYPE (type))
2926 == TYPE_MODE (TREE_TYPE (TREE_TYPE (@1)))))
2927 (plus @3 (view_convert (vec_cond @0 (negate @1) @2)))))
2930 /* Simplifications of comparisons. */
2932 /* See if we can reduce the magnitude of a constant involved in a
2933 comparison by changing the comparison code. This is a canonicalization
2934 formerly done by maybe_canonicalize_comparison_1. */
2938 (cmp @0 INTEGER_CST@1)
2939 (if (tree_int_cst_sgn (@1) == -1)
2940 (acmp @0 { wide_int_to_tree (TREE_TYPE (@1), wi::to_wide (@1) + 1); }))))
2944 (cmp @0 INTEGER_CST@1)
2945 (if (tree_int_cst_sgn (@1) == 1)
2946 (acmp @0 { wide_int_to_tree (TREE_TYPE (@1), wi::to_wide (@1) - 1); }))))
2949 /* We can simplify a logical negation of a comparison to the
2950 inverted comparison. As we cannot compute an expression
2951 operator using invert_tree_comparison we have to simulate
2952 that with expression code iteration. */
2953 (for cmp (tcc_comparison)
2954 icmp (inverted_tcc_comparison)
2955 ncmp (inverted_tcc_comparison_with_nans)
2956 /* Ideally we'd like to combine the following two patterns
2957 and handle some more cases by using
2958 (logical_inverted_value (cmp @0 @1))
2959 here but for that genmatch would need to "inline" that.
2960 For now implement what forward_propagate_comparison did. */
2962 (bit_not (cmp @0 @1))
2963 (if (VECTOR_TYPE_P (type)
2964 || (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1))
2965 /* Comparison inversion may be impossible for trapping math,
2966 invert_tree_comparison will tell us. But we can't use
2967 a computed operator in the replacement tree thus we have
2968 to play the trick below. */
2969 (with { enum tree_code ic = invert_tree_comparison
2970 (cmp, HONOR_NANS (@0)); }
2976 (bit_xor (cmp @0 @1) integer_truep)
2977 (with { enum tree_code ic = invert_tree_comparison
2978 (cmp, HONOR_NANS (@0)); }
2984 /* Transform comparisons of the form X - Y CMP 0 to X CMP Y.
2985 ??? The transformation is valid for the other operators if overflow
2986 is undefined for the type, but performing it here badly interacts
2987 with the transformation in fold_cond_expr_with_comparison which
2988 attempts to synthetize ABS_EXPR. */
2990 (for sub (minus pointer_diff)
2992 (cmp (sub@2 @0 @1) integer_zerop)
2993 (if (single_use (@2))
2996 /* Transform comparisons of the form X * C1 CMP 0 to X CMP 0 in the
2997 signed arithmetic case. That form is created by the compiler
2998 often enough for folding it to be of value. One example is in
2999 computing loop trip counts after Operator Strength Reduction. */
3000 (for cmp (simple_comparison)
3001 scmp (swapped_simple_comparison)
3003 (cmp (mult@3 @0 INTEGER_CST@1) integer_zerop@2)
3004 /* Handle unfolded multiplication by zero. */
3005 (if (integer_zerop (@1))
3007 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3008 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
3010 /* If @1 is negative we swap the sense of the comparison. */
3011 (if (tree_int_cst_sgn (@1) < 0)
3015 /* Simplify comparison of something with itself. For IEEE
3016 floating-point, we can only do some of these simplifications. */
3020 (if (! FLOAT_TYPE_P (TREE_TYPE (@0))
3021 || ! HONOR_NANS (@0))
3022 { constant_boolean_node (true, type); }
3023 (if (cmp != EQ_EXPR)
3029 || ! FLOAT_TYPE_P (TREE_TYPE (@0))
3030 || ! HONOR_NANS (@0))
3031 { constant_boolean_node (false, type); })))
3032 (for cmp (unle unge uneq)
3035 { constant_boolean_node (true, type); }))
3036 (for cmp (unlt ungt)
3042 (if (!flag_trapping_math)
3043 { constant_boolean_node (false, type); }))
3045 /* Fold ~X op ~Y as Y op X. */
3046 (for cmp (simple_comparison)
3048 (cmp (bit_not@2 @0) (bit_not@3 @1))
3049 (if (single_use (@2) && single_use (@3))
3052 /* Fold ~X op C as X op' ~C, where op' is the swapped comparison. */
3053 (for cmp (simple_comparison)
3054 scmp (swapped_simple_comparison)
3056 (cmp (bit_not@2 @0) CONSTANT_CLASS_P@1)
3057 (if (single_use (@2)
3058 && (TREE_CODE (@1) == INTEGER_CST || TREE_CODE (@1) == VECTOR_CST))
3059 (scmp @0 (bit_not @1)))))
3061 (for cmp (simple_comparison)
3062 /* Fold (double)float1 CMP (double)float2 into float1 CMP float2. */
3064 (cmp (convert@2 @0) (convert? @1))
3065 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
3066 && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@2))
3067 == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0)))
3068 && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@2))
3069 == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1))))
3072 tree type1 = TREE_TYPE (@1);
3073 if (TREE_CODE (@1) == REAL_CST && !DECIMAL_FLOAT_TYPE_P (type1))
3075 REAL_VALUE_TYPE orig = TREE_REAL_CST (@1);
3076 if (TYPE_PRECISION (type1) > TYPE_PRECISION (float_type_node)
3077 && exact_real_truncate (TYPE_MODE (float_type_node), &orig))
3078 type1 = float_type_node;
3079 if (TYPE_PRECISION (type1) > TYPE_PRECISION (double_type_node)
3080 && exact_real_truncate (TYPE_MODE (double_type_node), &orig))
3081 type1 = double_type_node;
3084 = (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type1)
3085 ? TREE_TYPE (@0) : type1);
3087 (if (TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (newtype))
3088 (cmp (convert:newtype @0) (convert:newtype @1))))))
3092 /* IEEE doesn't distinguish +0 and -0 in comparisons. */
3094 /* a CMP (-0) -> a CMP 0 */
3095 (if (REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@1)))
3096 (cmp @0 { build_real (TREE_TYPE (@1), dconst0); }))
3097 /* x != NaN is always true, other ops are always false. */
3098 (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
3099 && ! HONOR_SNANS (@1))
3100 { constant_boolean_node (cmp == NE_EXPR, type); })
3101 /* Fold comparisons against infinity. */
3102 (if (REAL_VALUE_ISINF (TREE_REAL_CST (@1))
3103 && MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (@1))))
3106 REAL_VALUE_TYPE max;
3107 enum tree_code code = cmp;
3108 bool neg = REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1));
3110 code = swap_tree_comparison (code);
3113 /* x > +Inf is always false, if we ignore NaNs or exceptions. */
3114 (if (code == GT_EXPR
3115 && !(HONOR_NANS (@0) && flag_trapping_math))
3116 { constant_boolean_node (false, type); })
3117 (if (code == LE_EXPR)
3118 /* x <= +Inf is always true, if we don't care about NaNs. */
3119 (if (! HONOR_NANS (@0))
3120 { constant_boolean_node (true, type); }
3121 /* x <= +Inf is the same as x == x, i.e. !isnan(x), but this loses
3122 an "invalid" exception. */
3123 (if (!flag_trapping_math)
3125 /* x == +Inf and x >= +Inf are always equal to x > DBL_MAX, but
3126 for == this introduces an exception for x a NaN. */
3127 (if ((code == EQ_EXPR && !(HONOR_NANS (@0) && flag_trapping_math))
3129 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
3131 (lt @0 { build_real (TREE_TYPE (@0), max); })
3132 (gt @0 { build_real (TREE_TYPE (@0), max); }))))
3133 /* x < +Inf is always equal to x <= DBL_MAX. */
3134 (if (code == LT_EXPR)
3135 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
3137 (ge @0 { build_real (TREE_TYPE (@0), max); })
3138 (le @0 { build_real (TREE_TYPE (@0), max); }))))
3139 /* x != +Inf is always equal to !(x > DBL_MAX), but this introduces
3140 an exception for x a NaN so use an unordered comparison. */
3141 (if (code == NE_EXPR)
3142 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
3143 (if (! HONOR_NANS (@0))
3145 (ge @0 { build_real (TREE_TYPE (@0), max); })
3146 (le @0 { build_real (TREE_TYPE (@0), max); }))
3148 (unge @0 { build_real (TREE_TYPE (@0), max); })
3149 (unle @0 { build_real (TREE_TYPE (@0), max); }))))))))))
3151 /* If this is a comparison of a real constant with a PLUS_EXPR
3152 or a MINUS_EXPR of a real constant, we can convert it into a
3153 comparison with a revised real constant as long as no overflow
3154 occurs when unsafe_math_optimizations are enabled. */
3155 (if (flag_unsafe_math_optimizations)
3156 (for op (plus minus)
3158 (cmp (op @0 REAL_CST@1) REAL_CST@2)
3161 tree tem = const_binop (op == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR,
3162 TREE_TYPE (@1), @2, @1);
3164 (if (tem && !TREE_OVERFLOW (tem))
3165 (cmp @0 { tem; }))))))
3167 /* Likewise, we can simplify a comparison of a real constant with
3168 a MINUS_EXPR whose first operand is also a real constant, i.e.
3169 (c1 - x) < c2 becomes x > c1-c2. Reordering is allowed on
3170 floating-point types only if -fassociative-math is set. */
3171 (if (flag_associative_math)
3173 (cmp (minus REAL_CST@0 @1) REAL_CST@2)
3174 (with { tree tem = const_binop (MINUS_EXPR, TREE_TYPE (@1), @0, @2); }
3175 (if (tem && !TREE_OVERFLOW (tem))
3176 (cmp { tem; } @1)))))
3178 /* Fold comparisons against built-in math functions. */
3179 (if (flag_unsafe_math_optimizations
3180 && ! flag_errno_math)
3183 (cmp (sq @0) REAL_CST@1)
3185 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
3187 /* sqrt(x) < y is always false, if y is negative. */
3188 (if (cmp == EQ_EXPR || cmp == LT_EXPR || cmp == LE_EXPR)
3189 { constant_boolean_node (false, type); })
3190 /* sqrt(x) > y is always true, if y is negative and we
3191 don't care about NaNs, i.e. negative values of x. */
3192 (if (cmp == NE_EXPR || !HONOR_NANS (@0))
3193 { constant_boolean_node (true, type); })
3194 /* sqrt(x) > y is the same as x >= 0, if y is negative. */
3195 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })))
3196 (if (real_equal (TREE_REAL_CST_PTR (@1), &dconst0))
3198 /* sqrt(x) < 0 is always false. */
3199 (if (cmp == LT_EXPR)
3200 { constant_boolean_node (false, type); })
3201 /* sqrt(x) >= 0 is always true if we don't care about NaNs. */
3202 (if (cmp == GE_EXPR && !HONOR_NANS (@0))
3203 { constant_boolean_node (true, type); })
3204 /* sqrt(x) <= 0 -> x == 0. */
3205 (if (cmp == LE_EXPR)
3207 /* Otherwise sqrt(x) cmp 0 -> x cmp 0. Here cmp can be >=, >,
3208 == or !=. In the last case:
3210 (sqrt(x) != 0) == (NaN != 0) == true == (x != 0)
3212 if x is negative or NaN. Due to -funsafe-math-optimizations,
3213 the results for other x follow from natural arithmetic. */
3215 (if (cmp == GT_EXPR || cmp == GE_EXPR)
3219 real_arithmetic (&c2, MULT_EXPR,
3220 &TREE_REAL_CST (@1), &TREE_REAL_CST (@1));
3221 real_convert (&c2, TYPE_MODE (TREE_TYPE (@0)), &c2);
3223 (if (REAL_VALUE_ISINF (c2))
3224 /* sqrt(x) > y is x == +Inf, when y is very large. */
3225 (if (HONOR_INFINITIES (@0))
3226 (eq @0 { build_real (TREE_TYPE (@0), c2); })
3227 { constant_boolean_node (false, type); })
3228 /* sqrt(x) > c is the same as x > c*c. */
3229 (cmp @0 { build_real (TREE_TYPE (@0), c2); }))))
3230 (if (cmp == LT_EXPR || cmp == LE_EXPR)
3234 real_arithmetic (&c2, MULT_EXPR,
3235 &TREE_REAL_CST (@1), &TREE_REAL_CST (@1));
3236 real_convert (&c2, TYPE_MODE (TREE_TYPE (@0)), &c2);
3238 (if (REAL_VALUE_ISINF (c2))
3240 /* sqrt(x) < y is always true, when y is a very large
3241 value and we don't care about NaNs or Infinities. */
3242 (if (! HONOR_NANS (@0) && ! HONOR_INFINITIES (@0))
3243 { constant_boolean_node (true, type); })
3244 /* sqrt(x) < y is x != +Inf when y is very large and we
3245 don't care about NaNs. */
3246 (if (! HONOR_NANS (@0))
3247 (ne @0 { build_real (TREE_TYPE (@0), c2); }))
3248 /* sqrt(x) < y is x >= 0 when y is very large and we
3249 don't care about Infinities. */
3250 (if (! HONOR_INFINITIES (@0))
3251 (ge @0 { build_real (TREE_TYPE (@0), dconst0); }))
3252 /* sqrt(x) < y is x >= 0 && x != +Inf, when y is large. */
3255 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
3256 (ne @0 { build_real (TREE_TYPE (@0), c2); }))))
3257 /* sqrt(x) < c is the same as x < c*c, if we ignore NaNs. */
3258 (if (! HONOR_NANS (@0))
3259 (cmp @0 { build_real (TREE_TYPE (@0), c2); })
3260 /* sqrt(x) < c is the same as x >= 0 && x < c*c. */
3263 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
3264 (cmp @0 { build_real (TREE_TYPE (@0), c2); })))))))))
3265 /* Transform sqrt(x) cmp sqrt(y) -> x cmp y. */
3267 (cmp (sq @0) (sq @1))
3268 (if (! HONOR_NANS (@0))
3271 /* Optimize various special cases of (FTYPE) N CMP CST. */
3272 (for cmp (lt le eq ne ge gt)
3273 icmp (le le eq ne ge ge)
3275 (cmp (float @0) REAL_CST@1)
3276 (if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (@1))
3277 && ! DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1)))
3280 tree itype = TREE_TYPE (@0);
3281 signop isign = TYPE_SIGN (itype);
3282 format_helper fmt (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (@1))));
3283 const REAL_VALUE_TYPE *cst = TREE_REAL_CST_PTR (@1);
3284 /* Be careful to preserve any potential exceptions due to
3285 NaNs. qNaNs are ok in == or != context.
3286 TODO: relax under -fno-trapping-math or
3287 -fno-signaling-nans. */
3289 = real_isnan (cst) && (cst->signalling
3290 || (cmp != EQ_EXPR && cmp != NE_EXPR));
3291 /* INT?_MIN is power-of-two so it takes
3292 only one mantissa bit. */
3293 bool signed_p = isign == SIGNED;
3294 bool itype_fits_ftype_p
3295 = TYPE_PRECISION (itype) - signed_p <= significand_size (fmt);
3297 /* TODO: allow non-fitting itype and SNaNs when
3298 -fno-trapping-math. */
3299 (if (itype_fits_ftype_p && ! exception_p)
3302 REAL_VALUE_TYPE imin, imax;
3303 real_from_integer (&imin, fmt, wi::min_value (itype), isign);
3304 real_from_integer (&imax, fmt, wi::max_value (itype), isign);
3306 REAL_VALUE_TYPE icst;
3307 if (cmp == GT_EXPR || cmp == GE_EXPR)
3308 real_ceil (&icst, fmt, cst);
3309 else if (cmp == LT_EXPR || cmp == LE_EXPR)
3310 real_floor (&icst, fmt, cst);
3312 real_trunc (&icst, fmt, cst);
3314 bool cst_int_p = !real_isnan (cst) && real_identical (&icst, cst);
3316 bool overflow_p = false;
3318 = real_to_integer (&icst, &overflow_p, TYPE_PRECISION (itype));
3321 /* Optimize cases when CST is outside of ITYPE's range. */
3322 (if (real_compare (LT_EXPR, cst, &imin))
3323 { constant_boolean_node (cmp == GT_EXPR || cmp == GE_EXPR || cmp == NE_EXPR,
3325 (if (real_compare (GT_EXPR, cst, &imax))
3326 { constant_boolean_node (cmp == LT_EXPR || cmp == LE_EXPR || cmp == NE_EXPR,
3328 /* Remove cast if CST is an integer representable by ITYPE. */
3330 (cmp @0 { gcc_assert (!overflow_p);
3331 wide_int_to_tree (itype, icst_val); })
3333 /* When CST is fractional, optimize
3334 (FTYPE) N == CST -> 0
3335 (FTYPE) N != CST -> 1. */
3336 (if (cmp == EQ_EXPR || cmp == NE_EXPR)
3337 { constant_boolean_node (cmp == NE_EXPR, type); })
3338 /* Otherwise replace with sensible integer constant. */
3341 gcc_checking_assert (!overflow_p);
3343 (icmp @0 { wide_int_to_tree (itype, icst_val); })))))))))
3345 /* Fold A /[ex] B CMP C to A CMP B * C. */
3348 (cmp (exact_div @0 @1) INTEGER_CST@2)
3349 (if (!integer_zerop (@1))
3350 (if (wi::to_wide (@2) == 0)
3352 (if (TREE_CODE (@1) == INTEGER_CST)
3356 wide_int prod = wi::mul (wi::to_wide (@2), wi::to_wide (@1),
3357 TYPE_SIGN (TREE_TYPE (@1)), &ovf);
3360 { constant_boolean_node (cmp == NE_EXPR, type); }
3361 (cmp @0 { wide_int_to_tree (TREE_TYPE (@0), prod); }))))))))
3362 (for cmp (lt le gt ge)
3364 (cmp (exact_div @0 INTEGER_CST@1) INTEGER_CST@2)
3365 (if (wi::gt_p (wi::to_wide (@1), 0, TYPE_SIGN (TREE_TYPE (@1))))
3369 wide_int prod = wi::mul (wi::to_wide (@2), wi::to_wide (@1),
3370 TYPE_SIGN (TREE_TYPE (@1)), &ovf);
3373 { constant_boolean_node (wi::lt_p (wi::to_wide (@2), 0,
3374 TYPE_SIGN (TREE_TYPE (@2)))
3375 != (cmp == LT_EXPR || cmp == LE_EXPR), type); }
3376 (cmp @0 { wide_int_to_tree (TREE_TYPE (@0), prod); }))))))
3378 /* Unordered tests if either argument is a NaN. */
3380 (bit_ior (unordered @0 @0) (unordered @1 @1))
3381 (if (types_match (@0, @1))
3384 (bit_and (ordered @0 @0) (ordered @1 @1))
3385 (if (types_match (@0, @1))
3388 (bit_ior:c (unordered @0 @0) (unordered:c@2 @0 @1))
3391 (bit_and:c (ordered @0 @0) (ordered:c@2 @0 @1))
3394 /* Simple range test simplifications. */
3395 /* A < B || A >= B -> true. */
3396 (for test1 (lt le le le ne ge)
3397 test2 (ge gt ge ne eq ne)
3399 (bit_ior:c (test1 @0 @1) (test2 @0 @1))
3400 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
3401 || VECTOR_INTEGER_TYPE_P (TREE_TYPE (@0)))
3402 { constant_boolean_node (true, type); })))
3403 /* A < B && A >= B -> false. */
3404 (for test1 (lt lt lt le ne eq)
3405 test2 (ge gt eq gt eq gt)
3407 (bit_and:c (test1 @0 @1) (test2 @0 @1))
3408 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
3409 || VECTOR_INTEGER_TYPE_P (TREE_TYPE (@0)))
3410 { constant_boolean_node (false, type); })))
3412 /* A & (2**N - 1) <= 2**K - 1 -> A & (2**N - 2**K) == 0
3413 A & (2**N - 1) > 2**K - 1 -> A & (2**N - 2**K) != 0
3415 Note that comparisons
3416 A & (2**N - 1) < 2**K -> A & (2**N - 2**K) == 0
3417 A & (2**N - 1) >= 2**K -> A & (2**N - 2**K) != 0
3418 will be canonicalized to above so there's no need to
3425 (cmp (bit_and@0 @1 INTEGER_CST@2) INTEGER_CST@3)
3426 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)))
3429 tree ty = TREE_TYPE (@0);
3430 unsigned prec = TYPE_PRECISION (ty);
3431 wide_int mask = wi::to_wide (@2, prec);
3432 wide_int rhs = wi::to_wide (@3, prec);
3433 signop sgn = TYPE_SIGN (ty);
3435 (if ((mask & (mask + 1)) == 0 && wi::gt_p (rhs, 0, sgn)
3436 && (rhs & (rhs + 1)) == 0 && wi::ge_p (mask, rhs, sgn))
3437 (eqcmp (bit_and @1 { wide_int_to_tree (ty, mask - rhs); })
3438 { build_zero_cst (ty); }))))))
3440 /* -A CMP -B -> B CMP A. */
3441 (for cmp (tcc_comparison)
3442 scmp (swapped_tcc_comparison)
3444 (cmp (negate @0) (negate @1))
3445 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
3446 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3447 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
3450 (cmp (negate @0) CONSTANT_CLASS_P@1)
3451 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
3452 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3453 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
3454 (with { tree tem = const_unop (NEGATE_EXPR, TREE_TYPE (@0), @1); }
3455 (if (tem && !TREE_OVERFLOW (tem))
3456 (scmp @0 { tem; }))))))
3458 /* Convert ABS_EXPR<x> == 0 or ABS_EXPR<x> != 0 to x == 0 or x != 0. */
3461 (op (abs @0) zerop@1)
3464 /* From fold_sign_changed_comparison and fold_widened_comparison.
3465 FIXME: the lack of symmetry is disturbing. */
3466 (for cmp (simple_comparison)
3468 (cmp (convert@0 @00) (convert?@1 @10))
3469 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
3470 /* Disable this optimization if we're casting a function pointer
3471 type on targets that require function pointer canonicalization. */
3472 && !(targetm.have_canonicalize_funcptr_for_compare ()
3473 && TREE_CODE (TREE_TYPE (@00)) == POINTER_TYPE
3474 && TREE_CODE (TREE_TYPE (TREE_TYPE (@00))) == FUNCTION_TYPE)
3476 (if (TYPE_PRECISION (TREE_TYPE (@00)) == TYPE_PRECISION (TREE_TYPE (@0))
3477 && (TREE_CODE (@10) == INTEGER_CST
3479 && (TYPE_UNSIGNED (TREE_TYPE (@00)) == TYPE_UNSIGNED (TREE_TYPE (@0))
3482 && !POINTER_TYPE_P (TREE_TYPE (@00)))
3483 /* ??? The special-casing of INTEGER_CST conversion was in the original
3484 code and here to avoid a spurious overflow flag on the resulting
3485 constant which fold_convert produces. */
3486 (if (TREE_CODE (@1) == INTEGER_CST)
3487 (cmp @00 { force_fit_type (TREE_TYPE (@00), wi::to_widest (@1), 0,
3488 TREE_OVERFLOW (@1)); })
3489 (cmp @00 (convert @1)))
3491 (if (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (TREE_TYPE (@00)))
3492 /* If possible, express the comparison in the shorter mode. */
3493 (if ((cmp == EQ_EXPR || cmp == NE_EXPR
3494 || TYPE_UNSIGNED (TREE_TYPE (@0)) == TYPE_UNSIGNED (TREE_TYPE (@00))
3495 || (!TYPE_UNSIGNED (TREE_TYPE (@0))
3496 && TYPE_UNSIGNED (TREE_TYPE (@00))))
3497 && (types_match (TREE_TYPE (@10), TREE_TYPE (@00))
3498 || ((TYPE_PRECISION (TREE_TYPE (@00))
3499 >= TYPE_PRECISION (TREE_TYPE (@10)))
3500 && (TYPE_UNSIGNED (TREE_TYPE (@00))
3501 == TYPE_UNSIGNED (TREE_TYPE (@10))))
3502 || (TREE_CODE (@10) == INTEGER_CST
3503 && INTEGRAL_TYPE_P (TREE_TYPE (@00))
3504 && int_fits_type_p (@10, TREE_TYPE (@00)))))
3505 (cmp @00 (convert @10))
3506 (if (TREE_CODE (@10) == INTEGER_CST
3507 && INTEGRAL_TYPE_P (TREE_TYPE (@00))
3508 && !int_fits_type_p (@10, TREE_TYPE (@00)))
3511 tree min = lower_bound_in_type (TREE_TYPE (@10), TREE_TYPE (@00));
3512 tree max = upper_bound_in_type (TREE_TYPE (@10), TREE_TYPE (@00));
3513 bool above = integer_nonzerop (const_binop (LT_EXPR, type, max, @10));
3514 bool below = integer_nonzerop (const_binop (LT_EXPR, type, @10, min));
3516 (if (above || below)
3517 (if (cmp == EQ_EXPR || cmp == NE_EXPR)
3518 { constant_boolean_node (cmp == EQ_EXPR ? false : true, type); }
3519 (if (cmp == LT_EXPR || cmp == LE_EXPR)
3520 { constant_boolean_node (above ? true : false, type); }
3521 (if (cmp == GT_EXPR || cmp == GE_EXPR)
3522 { constant_boolean_node (above ? false : true, type); }))))))))))))
3525 /* A local variable can never be pointed to by
3526 the default SSA name of an incoming parameter.
3527 SSA names are canonicalized to 2nd place. */
3529 (cmp addr@0 SSA_NAME@1)
3530 (if (SSA_NAME_IS_DEFAULT_DEF (@1)
3531 && TREE_CODE (SSA_NAME_VAR (@1)) == PARM_DECL)
3532 (with { tree base = get_base_address (TREE_OPERAND (@0, 0)); }
3533 (if (TREE_CODE (base) == VAR_DECL
3534 && auto_var_in_fn_p (base, current_function_decl))
3535 (if (cmp == NE_EXPR)
3536 { constant_boolean_node (true, type); }
3537 { constant_boolean_node (false, type); }))))))
3539 /* Equality compare simplifications from fold_binary */
3542 /* If we have (A | C) == D where C & ~D != 0, convert this into 0.
3543 Similarly for NE_EXPR. */
3545 (cmp (convert?@3 (bit_ior @0 INTEGER_CST@1)) INTEGER_CST@2)
3546 (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0))
3547 && wi::bit_and_not (wi::to_wide (@1), wi::to_wide (@2)) != 0)
3548 { constant_boolean_node (cmp == NE_EXPR, type); }))
3550 /* (X ^ Y) == 0 becomes X == Y, and (X ^ Y) != 0 becomes X != Y. */
3552 (cmp (bit_xor @0 @1) integer_zerop)
3555 /* (X ^ Y) == Y becomes X == 0.
3556 Likewise (X ^ Y) == X becomes Y == 0. */
3558 (cmp:c (bit_xor:c @0 @1) @0)
3559 (cmp @1 { build_zero_cst (TREE_TYPE (@1)); }))
3561 /* (X ^ C1) op C2 can be rewritten as X op (C1 ^ C2). */
3563 (cmp (convert?@3 (bit_xor @0 INTEGER_CST@1)) INTEGER_CST@2)
3564 (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0)))
3565 (cmp @0 (bit_xor @1 (convert @2)))))
3568 (cmp (convert? addr@0) integer_zerop)
3569 (if (tree_single_nonzero_warnv_p (@0, NULL))
3570 { constant_boolean_node (cmp == NE_EXPR, type); })))
3572 /* If we have (A & C) == C where C is a power of 2, convert this into
3573 (A & C) != 0. Similarly for NE_EXPR. */
3577 (cmp (bit_and@2 @0 integer_pow2p@1) @1)
3578 (icmp @2 { build_zero_cst (TREE_TYPE (@0)); })))
3580 /* If we have (A & C) != 0 ? D : 0 where C and D are powers of 2,
3581 convert this into a shift followed by ANDing with D. */
3584 (ne (bit_and @0 integer_pow2p@1) integer_zerop)
3585 INTEGER_CST@2 integer_zerop)
3586 (if (integer_pow2p (@2))
3588 int shift = (wi::exact_log2 (wi::to_wide (@2))
3589 - wi::exact_log2 (wi::to_wide (@1)));
3593 (lshift (convert @0) { build_int_cst (integer_type_node, shift); }) @2)
3595 (convert (rshift @0 { build_int_cst (integer_type_node, -shift); }))
3598 /* If we have (A & C) != 0 where C is the sign bit of A, convert
3599 this into A < 0. Similarly for (A & C) == 0 into A >= 0. */
3603 (cmp (bit_and (convert?@2 @0) integer_pow2p@1) integer_zerop)
3604 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
3605 && type_has_mode_precision_p (TREE_TYPE (@0))
3606 && element_precision (@2) >= element_precision (@0)
3607 && wi::only_sign_bit_p (wi::to_wide (@1), element_precision (@0)))
3608 (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
3609 (ncmp (convert:stype @0) { build_zero_cst (stype); })))))
3611 /* If we have A < 0 ? C : 0 where C is a power of 2, convert
3612 this into a right shift or sign extension followed by ANDing with C. */
3615 (lt @0 integer_zerop)
3616 INTEGER_CST@1 integer_zerop)
3617 (if (integer_pow2p (@1)
3618 && !TYPE_UNSIGNED (TREE_TYPE (@0)))
3620 int shift = element_precision (@0) - wi::exact_log2 (wi::to_wide (@1)) - 1;
3624 (convert (rshift @0 { build_int_cst (integer_type_node, shift); }))
3626 /* Otherwise ctype must be wider than TREE_TYPE (@0) and pure
3627 sign extension followed by AND with C will achieve the effect. */
3628 (bit_and (convert @0) @1)))))
3630 /* When the addresses are not directly of decls compare base and offset.
3631 This implements some remaining parts of fold_comparison address
3632 comparisons but still no complete part of it. Still it is good
3633 enough to make fold_stmt not regress when not dispatching to fold_binary. */
3634 (for cmp (simple_comparison)
3636 (cmp (convert1?@2 addr@0) (convert2? addr@1))
3639 poly_int64 off0, off1;
3640 tree base0 = get_addr_base_and_unit_offset (TREE_OPERAND (@0, 0), &off0);
3641 tree base1 = get_addr_base_and_unit_offset (TREE_OPERAND (@1, 0), &off1);
3642 if (base0 && TREE_CODE (base0) == MEM_REF)
3644 off0 += mem_ref_offset (base0).force_shwi ();
3645 base0 = TREE_OPERAND (base0, 0);
3647 if (base1 && TREE_CODE (base1) == MEM_REF)
3649 off1 += mem_ref_offset (base1).force_shwi ();
3650 base1 = TREE_OPERAND (base1, 0);
3653 (if (base0 && base1)
3657 /* Punt in GENERIC on variables with value expressions;
3658 the value expressions might point to fields/elements
3659 of other vars etc. */
3661 && ((VAR_P (base0) && DECL_HAS_VALUE_EXPR_P (base0))
3662 || (VAR_P (base1) && DECL_HAS_VALUE_EXPR_P (base1))))
3664 else if (decl_in_symtab_p (base0)
3665 && decl_in_symtab_p (base1))
3666 equal = symtab_node::get_create (base0)
3667 ->equal_address_to (symtab_node::get_create (base1));
3668 else if ((DECL_P (base0)
3669 || TREE_CODE (base0) == SSA_NAME
3670 || TREE_CODE (base0) == STRING_CST)
3672 || TREE_CODE (base1) == SSA_NAME
3673 || TREE_CODE (base1) == STRING_CST))
3674 equal = (base0 == base1);
3677 && (cmp == EQ_EXPR || cmp == NE_EXPR
3678 /* If the offsets are equal we can ignore overflow. */
3679 || known_eq (off0, off1)
3680 || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
3681 /* Or if we compare using pointers to decls or strings. */
3682 || (POINTER_TYPE_P (TREE_TYPE (@2))
3683 && (DECL_P (base0) || TREE_CODE (base0) == STRING_CST))))
3685 (if (cmp == EQ_EXPR && (known_eq (off0, off1) || known_ne (off0, off1)))
3686 { constant_boolean_node (known_eq (off0, off1), type); })
3687 (if (cmp == NE_EXPR && (known_eq (off0, off1) || known_ne (off0, off1)))
3688 { constant_boolean_node (known_ne (off0, off1), type); })
3689 (if (cmp == LT_EXPR && (known_lt (off0, off1) || known_ge (off0, off1)))
3690 { constant_boolean_node (known_lt (off0, off1), type); })
3691 (if (cmp == LE_EXPR && (known_le (off0, off1) || known_gt (off0, off1)))
3692 { constant_boolean_node (known_le (off0, off1), type); })
3693 (if (cmp == GE_EXPR && (known_ge (off0, off1) || known_lt (off0, off1)))
3694 { constant_boolean_node (known_ge (off0, off1), type); })
3695 (if (cmp == GT_EXPR && (known_gt (off0, off1) || known_le (off0, off1)))
3696 { constant_boolean_node (known_gt (off0, off1), type); }))
3698 && DECL_P (base0) && DECL_P (base1)
3699 /* If we compare this as integers require equal offset. */
3700 && (!INTEGRAL_TYPE_P (TREE_TYPE (@2))
3701 || known_eq (off0, off1)))
3703 (if (cmp == EQ_EXPR)
3704 { constant_boolean_node (false, type); })
3705 (if (cmp == NE_EXPR)
3706 { constant_boolean_node (true, type); })))))))))
3708 /* Simplify pointer equality compares using PTA. */
3712 (if (POINTER_TYPE_P (TREE_TYPE (@0))
3713 && ptrs_compare_unequal (@0, @1))
3714 { constant_boolean_node (neeq != EQ_EXPR, type); })))
3716 /* PR70920: Transform (intptr_t)x eq/ne CST to x eq/ne (typeof x) CST.
3717 and (typeof ptr_cst) x eq/ne ptr_cst to x eq/ne (typeof x) CST.
3718 Disable the transform if either operand is pointer to function.
3719 This broke pr22051-2.c for arm where function pointer
3720 canonicalizaion is not wanted. */
3724 (cmp (convert @0) INTEGER_CST@1)
3725 (if (((POINTER_TYPE_P (TREE_TYPE (@0))
3726 && !FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@0)))
3727 && INTEGRAL_TYPE_P (TREE_TYPE (@1)))
3728 || (INTEGRAL_TYPE_P (TREE_TYPE (@0))
3729 && POINTER_TYPE_P (TREE_TYPE (@1))
3730 && !FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@1)))))
3731 && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
3732 (cmp @0 (convert @1)))))
3734 /* Non-equality compare simplifications from fold_binary */
3735 (for cmp (lt gt le ge)
3736 /* Comparisons with the highest or lowest possible integer of
3737 the specified precision will have known values. */
3739 (cmp (convert?@2 @0) INTEGER_CST@1)
3740 (if ((INTEGRAL_TYPE_P (TREE_TYPE (@1)) || POINTER_TYPE_P (TREE_TYPE (@1)))
3741 && tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@0)))
3744 tree arg1_type = TREE_TYPE (@1);
3745 unsigned int prec = TYPE_PRECISION (arg1_type);
3746 wide_int max = wi::max_value (arg1_type);
3747 wide_int signed_max = wi::max_value (prec, SIGNED);
3748 wide_int min = wi::min_value (arg1_type);
3751 (if (wi::to_wide (@1) == max)
3753 (if (cmp == GT_EXPR)
3754 { constant_boolean_node (false, type); })
3755 (if (cmp == GE_EXPR)
3757 (if (cmp == LE_EXPR)
3758 { constant_boolean_node (true, type); })
3759 (if (cmp == LT_EXPR)
3761 (if (wi::to_wide (@1) == min)
3763 (if (cmp == LT_EXPR)
3764 { constant_boolean_node (false, type); })
3765 (if (cmp == LE_EXPR)
3767 (if (cmp == GE_EXPR)
3768 { constant_boolean_node (true, type); })
3769 (if (cmp == GT_EXPR)
3771 (if (wi::to_wide (@1) == max - 1)
3773 (if (cmp == GT_EXPR)
3774 (eq @2 { wide_int_to_tree (TREE_TYPE (@1), wi::to_wide (@1) + 1); }))
3775 (if (cmp == LE_EXPR)
3776 (ne @2 { wide_int_to_tree (TREE_TYPE (@1), wi::to_wide (@1) + 1); }))))
3777 (if (wi::to_wide (@1) == min + 1)
3779 (if (cmp == GE_EXPR)
3780 (ne @2 { wide_int_to_tree (TREE_TYPE (@1), wi::to_wide (@1) - 1); }))
3781 (if (cmp == LT_EXPR)
3782 (eq @2 { wide_int_to_tree (TREE_TYPE (@1), wi::to_wide (@1) - 1); }))))
3783 (if (wi::to_wide (@1) == signed_max
3784 && TYPE_UNSIGNED (arg1_type)
3785 /* We will flip the signedness of the comparison operator
3786 associated with the mode of @1, so the sign bit is
3787 specified by this mode. Check that @1 is the signed
3788 max associated with this sign bit. */
3789 && prec == GET_MODE_PRECISION (SCALAR_INT_TYPE_MODE (arg1_type))
3790 /* signed_type does not work on pointer types. */
3791 && INTEGRAL_TYPE_P (arg1_type))
3792 /* The following case also applies to X < signed_max+1
3793 and X >= signed_max+1 because previous transformations. */
3794 (if (cmp == LE_EXPR || cmp == GT_EXPR)
3795 (with { tree st = signed_type_for (arg1_type); }
3796 (if (cmp == LE_EXPR)
3797 (ge (convert:st @0) { build_zero_cst (st); })
3798 (lt (convert:st @0) { build_zero_cst (st); }))))))))))
3800 (for cmp (unordered ordered unlt unle ungt unge uneq ltgt)
3801 /* If the second operand is NaN, the result is constant. */
3804 (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
3805 && (cmp != LTGT_EXPR || ! flag_trapping_math))
3806 { constant_boolean_node (cmp == ORDERED_EXPR || cmp == LTGT_EXPR
3807 ? false : true, type); })))
3809 /* bool_var != 0 becomes bool_var. */
3811 (ne @0 integer_zerop)
3812 (if (TREE_CODE (TREE_TYPE (@0)) == BOOLEAN_TYPE
3813 && types_match (type, TREE_TYPE (@0)))
3815 /* bool_var == 1 becomes bool_var. */
3817 (eq @0 integer_onep)
3818 (if (TREE_CODE (TREE_TYPE (@0)) == BOOLEAN_TYPE
3819 && types_match (type, TREE_TYPE (@0)))
3822 bool_var == 0 becomes !bool_var or
3823 bool_var != 1 becomes !bool_var
3824 here because that only is good in assignment context as long
3825 as we require a tcc_comparison in GIMPLE_CONDs where we'd
3826 replace if (x == 0) with tem = ~x; if (tem != 0) which is
3827 clearly less optimal and which we'll transform again in forwprop. */
3829 /* When one argument is a constant, overflow detection can be simplified.
3830 Currently restricted to single use so as not to interfere too much with
3831 ADD_OVERFLOW detection in tree-ssa-math-opts.c.
3832 A + CST CMP A -> A CMP' CST' */
3833 (for cmp (lt le ge gt)
3836 (cmp:c (plus@2 @0 INTEGER_CST@1) @0)
3837 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
3838 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))
3839 && wi::to_wide (@1) != 0
3841 (with { unsigned int prec = TYPE_PRECISION (TREE_TYPE (@0)); }
3842 (out @0 { wide_int_to_tree (TREE_TYPE (@0),
3843 wi::max_value (prec, UNSIGNED)
3844 - wi::to_wide (@1)); })))))
3846 /* To detect overflow in unsigned A - B, A < B is simpler than A - B > A.
3847 However, the detection logic for SUB_OVERFLOW in tree-ssa-math-opts.c
3848 expects the long form, so we restrict the transformation for now. */
3851 (cmp:c (minus@2 @0 @1) @0)
3852 (if (single_use (@2)
3853 && ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3854 && TYPE_UNSIGNED (TREE_TYPE (@0))
3855 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
3858 /* Testing for overflow is unnecessary if we already know the result. */
3863 (cmp:c (realpart (IFN_SUB_OVERFLOW@2 @0 @1)) @0)
3864 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
3865 && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
3866 (out (imagpart @2) { build_zero_cst (TREE_TYPE (@0)); }))))
3871 (cmp:c (realpart (IFN_ADD_OVERFLOW:c@2 @0 @1)) @0)
3872 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
3873 && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
3874 (out (imagpart @2) { build_zero_cst (TREE_TYPE (@0)); }))))
3876 /* For unsigned operands, -1 / B < A checks whether A * B would overflow.
3877 Simplify it to __builtin_mul_overflow (A, B, <unused>). */
3881 (cmp:c (trunc_div:s integer_all_onesp @1) @0)
3882 (if (TYPE_UNSIGNED (TREE_TYPE (@0)) && !VECTOR_TYPE_P (TREE_TYPE (@0)))
3883 (with { tree t = TREE_TYPE (@0), cpx = build_complex_type (t); }
3884 (out (imagpart (IFN_MUL_OVERFLOW:cpx @0 @1)) { build_zero_cst (t); })))))
3886 /* Simplification of math builtins. These rules must all be optimizations
3887 as well as IL simplifications. If there is a possibility that the new
3888 form could be a pessimization, the rule should go in the canonicalization
3889 section that follows this one.
3891 Rules can generally go in this section if they satisfy one of
3894 - the rule describes an identity
3896 - the rule replaces calls with something as simple as addition or
3899 - the rule contains unary calls only and simplifies the surrounding
3900 arithmetic. (The idea here is to exclude non-unary calls in which
3901 one operand is constant and in which the call is known to be cheap
3902 when the operand has that value.) */
3904 (if (flag_unsafe_math_optimizations)
3905 /* Simplify sqrt(x) * sqrt(x) -> x. */
3907 (mult (SQRT_ALL@1 @0) @1)
3908 (if (!HONOR_SNANS (type))
3911 (for op (plus minus)
3912 /* Simplify (A / C) +- (B / C) -> (A +- B) / C. */
3916 (rdiv (op @0 @2) @1)))
3918 /* Simplify sqrt(x) * sqrt(y) -> sqrt(x*y). */
3919 (for root (SQRT CBRT)
3921 (mult (root:s @0) (root:s @1))
3922 (root (mult @0 @1))))
3924 /* Simplify expN(x) * expN(y) -> expN(x+y). */
3925 (for exps (EXP EXP2 EXP10 POW10)
3927 (mult (exps:s @0) (exps:s @1))
3928 (exps (plus @0 @1))))
3930 /* Simplify a/root(b/c) into a*root(c/b). */
3931 (for root (SQRT CBRT)
3933 (rdiv @0 (root:s (rdiv:s @1 @2)))
3934 (mult @0 (root (rdiv @2 @1)))))
3936 /* Simplify x/expN(y) into x*expN(-y). */
3937 (for exps (EXP EXP2 EXP10 POW10)
3939 (rdiv @0 (exps:s @1))
3940 (mult @0 (exps (negate @1)))))
3942 (for logs (LOG LOG2 LOG10 LOG10)
3943 exps (EXP EXP2 EXP10 POW10)
3944 /* logN(expN(x)) -> x. */
3948 /* expN(logN(x)) -> x. */
3953 /* Optimize logN(func()) for various exponential functions. We
3954 want to determine the value "x" and the power "exponent" in
3955 order to transform logN(x**exponent) into exponent*logN(x). */
3956 (for logs (LOG LOG LOG LOG2 LOG2 LOG2 LOG10 LOG10)
3957 exps (EXP2 EXP10 POW10 EXP EXP10 POW10 EXP EXP2)
3960 (if (SCALAR_FLOAT_TYPE_P (type))
3966 /* Prepare to do logN(exp(exponent)) -> exponent*logN(e). */
3967 x = build_real_truncate (type, dconst_e ());
3970 /* Prepare to do logN(exp2(exponent)) -> exponent*logN(2). */
3971 x = build_real (type, dconst2);
3975 /* Prepare to do logN(exp10(exponent)) -> exponent*logN(10). */
3977 REAL_VALUE_TYPE dconst10;
3978 real_from_integer (&dconst10, VOIDmode, 10, SIGNED);
3979 x = build_real (type, dconst10);
3986 (mult (logs { x; }) @0)))))
3994 (if (SCALAR_FLOAT_TYPE_P (type))
4000 /* Prepare to do logN(sqrt(x)) -> 0.5*logN(x). */
4001 x = build_real (type, dconsthalf);
4004 /* Prepare to do logN(cbrt(x)) -> (1/3)*logN(x). */
4005 x = build_real_truncate (type, dconst_third ());
4011 (mult { x; } (logs @0))))))
4013 /* logN(pow(x,exponent)) -> exponent*logN(x). */
4014 (for logs (LOG LOG2 LOG10)
4018 (mult @1 (logs @0))))
4020 /* pow(C,x) -> exp(log(C)*x) if C > 0,
4021 or if C is a positive power of 2,
4022 pow(C,x) -> exp2(log2(C)*x). */
4030 (pows REAL_CST@0 @1)
4031 (if (real_compare (GT_EXPR, TREE_REAL_CST_PTR (@0), &dconst0)
4032 && real_isfinite (TREE_REAL_CST_PTR (@0))
4033 /* As libmvec doesn't have a vectorized exp2, defer optimizing
4034 the use_exp2 case until after vectorization. It seems actually
4035 beneficial for all constants to postpone this until later,
4036 because exp(log(C)*x), while faster, will have worse precision
4037 and if x folds into a constant too, that is unnecessary
4039 && canonicalize_math_after_vectorization_p ())
4041 const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (@0);
4042 bool use_exp2 = false;
4043 if (targetm.libc_has_function (function_c99_misc)
4044 && value->cl == rvc_normal)
4046 REAL_VALUE_TYPE frac_rvt = *value;
4047 SET_REAL_EXP (&frac_rvt, 1);
4048 if (real_equal (&frac_rvt, &dconst1))
4053 (if (optimize_pow_to_exp (@0, @1))
4054 (exps (mult (logs @0) @1)))
4055 (exp2s (mult (log2s @0) @1)))))))
4058 /* pow(C,x)*expN(y) -> expN(logN(C)*x+y) if C > 0. */
4060 exps (EXP EXP2 EXP10 POW10)
4061 logs (LOG LOG2 LOG10 LOG10)
4063 (mult:c (pows:s REAL_CST@0 @1) (exps:s @2))
4064 (if (real_compare (GT_EXPR, TREE_REAL_CST_PTR (@0), &dconst0)
4065 && real_isfinite (TREE_REAL_CST_PTR (@0)))
4066 (exps (plus (mult (logs @0) @1) @2)))))
4071 exps (EXP EXP2 EXP10 POW10)
4072 /* sqrt(expN(x)) -> expN(x*0.5). */
4075 (exps (mult @0 { build_real (type, dconsthalf); })))
4076 /* cbrt(expN(x)) -> expN(x/3). */
4079 (exps (mult @0 { build_real_truncate (type, dconst_third ()); })))
4080 /* pow(expN(x), y) -> expN(x*y). */
4083 (exps (mult @0 @1))))
4085 /* tan(atan(x)) -> x. */
4092 /* cabs(x+0i) or cabs(0+xi) -> abs(x). */
4094 (CABS (complex:C @0 real_zerop@1))
4097 /* trunc(trunc(x)) -> trunc(x), etc. */
4098 (for fns (TRUNC_ALL FLOOR_ALL CEIL_ALL ROUND_ALL NEARBYINT_ALL RINT_ALL)
4102 /* f(x) -> x if x is integer valued and f does nothing for such values. */
4103 (for fns (TRUNC_ALL FLOOR_ALL CEIL_ALL ROUND_ALL NEARBYINT_ALL RINT_ALL)
4105 (fns integer_valued_real_p@0)
4108 /* hypot(x,0) and hypot(0,x) -> abs(x). */
4110 (HYPOT:c @0 real_zerop@1)
4113 /* pow(1,x) -> 1. */
4115 (POW real_onep@0 @1)
4119 /* copysign(x,x) -> x. */
4120 (COPYSIGN_ALL @0 @0)
4124 /* copysign(x,y) -> fabs(x) if y is nonnegative. */
4125 (COPYSIGN_ALL @0 tree_expr_nonnegative_p@1)
4128 (for scale (LDEXP SCALBN SCALBLN)
4129 /* ldexp(0, x) -> 0. */
4131 (scale real_zerop@0 @1)
4133 /* ldexp(x, 0) -> x. */
4135 (scale @0 integer_zerop@1)
4137 /* ldexp(x, y) -> x if x is +-Inf or NaN. */
4139 (scale REAL_CST@0 @1)
4140 (if (!real_isfinite (TREE_REAL_CST_PTR (@0)))
4143 /* Canonicalization of sequences of math builtins. These rules represent
4144 IL simplifications but are not necessarily optimizations.
4146 The sincos pass is responsible for picking "optimal" implementations
4147 of math builtins, which may be more complicated and can sometimes go
4148 the other way, e.g. converting pow into a sequence of sqrts.
4149 We only want to do these canonicalizations before the pass has run. */
4151 (if (flag_unsafe_math_optimizations && canonicalize_math_p ())
4152 /* Simplify tan(x) * cos(x) -> sin(x). */
4154 (mult:c (TAN:s @0) (COS:s @0))
4157 /* Simplify x * pow(x,c) -> pow(x,c+1). */
4159 (mult:c @0 (POW:s @0 REAL_CST@1))
4160 (if (!TREE_OVERFLOW (@1))
4161 (POW @0 (plus @1 { build_one_cst (type); }))))
4163 /* Simplify sin(x) / cos(x) -> tan(x). */
4165 (rdiv (SIN:s @0) (COS:s @0))
4168 /* Simplify cos(x) / sin(x) -> 1 / tan(x). */
4170 (rdiv (COS:s @0) (SIN:s @0))
4171 (rdiv { build_one_cst (type); } (TAN @0)))
4173 /* Simplify sin(x) / tan(x) -> cos(x). */
4175 (rdiv (SIN:s @0) (TAN:s @0))
4176 (if (! HONOR_NANS (@0)
4177 && ! HONOR_INFINITIES (@0))
4180 /* Simplify tan(x) / sin(x) -> 1.0 / cos(x). */
4182 (rdiv (TAN:s @0) (SIN:s @0))
4183 (if (! HONOR_NANS (@0)
4184 && ! HONOR_INFINITIES (@0))
4185 (rdiv { build_one_cst (type); } (COS @0))))
4187 /* Simplify pow(x,y) * pow(x,z) -> pow(x,y+z). */
4189 (mult (POW:s @0 @1) (POW:s @0 @2))
4190 (POW @0 (plus @1 @2)))
4192 /* Simplify pow(x,y) * pow(z,y) -> pow(x*z,y). */
4194 (mult (POW:s @0 @1) (POW:s @2 @1))
4195 (POW (mult @0 @2) @1))
4197 /* Simplify powi(x,y) * powi(z,y) -> powi(x*z,y). */
4199 (mult (POWI:s @0 @1) (POWI:s @2 @1))
4200 (POWI (mult @0 @2) @1))
4202 /* Simplify pow(x,c) / x -> pow(x,c-1). */
4204 (rdiv (POW:s @0 REAL_CST@1) @0)
4205 (if (!TREE_OVERFLOW (@1))
4206 (POW @0 (minus @1 { build_one_cst (type); }))))
4208 /* Simplify x / pow (y,z) -> x * pow(y,-z). */
4210 (rdiv @0 (POW:s @1 @2))
4211 (mult @0 (POW @1 (negate @2))))
4216 /* sqrt(sqrt(x)) -> pow(x,1/4). */
4219 (pows @0 { build_real (type, dconst_quarter ()); }))
4220 /* sqrt(cbrt(x)) -> pow(x,1/6). */
4223 (pows @0 { build_real_truncate (type, dconst_sixth ()); }))
4224 /* cbrt(sqrt(x)) -> pow(x,1/6). */
4227 (pows @0 { build_real_truncate (type, dconst_sixth ()); }))
4228 /* cbrt(cbrt(x)) -> pow(x,1/9), iff x is nonnegative. */
4230 (cbrts (cbrts tree_expr_nonnegative_p@0))
4231 (pows @0 { build_real_truncate (type, dconst_ninth ()); }))
4232 /* sqrt(pow(x,y)) -> pow(|x|,y*0.5). */
4234 (sqrts (pows @0 @1))
4235 (pows (abs @0) (mult @1 { build_real (type, dconsthalf); })))
4236 /* cbrt(pow(x,y)) -> pow(x,y/3), iff x is nonnegative. */
4238 (cbrts (pows tree_expr_nonnegative_p@0 @1))
4239 (pows @0 (mult @1 { build_real_truncate (type, dconst_third ()); })))
4240 /* pow(sqrt(x),y) -> pow(x,y*0.5). */
4242 (pows (sqrts @0) @1)
4243 (pows @0 (mult @1 { build_real (type, dconsthalf); })))
4244 /* pow(cbrt(x),y) -> pow(x,y/3) iff x is nonnegative. */
4246 (pows (cbrts tree_expr_nonnegative_p@0) @1)
4247 (pows @0 (mult @1 { build_real_truncate (type, dconst_third ()); })))
4248 /* pow(pow(x,y),z) -> pow(x,y*z) iff x is nonnegative. */
4250 (pows (pows tree_expr_nonnegative_p@0 @1) @2)
4251 (pows @0 (mult @1 @2))))
4253 /* cabs(x+xi) -> fabs(x)*sqrt(2). */
4255 (CABS (complex @0 @0))
4256 (mult (abs @0) { build_real_truncate (type, dconst_sqrt2 ()); }))
4258 /* hypot(x,x) -> fabs(x)*sqrt(2). */
4261 (mult (abs @0) { build_real_truncate (type, dconst_sqrt2 ()); }))
4263 /* cexp(x+yi) -> exp(x)*cexpi(y). */
4268 (cexps compositional_complex@0)
4269 (if (targetm.libc_has_function (function_c99_math_complex))
4271 (mult (exps@1 (realpart @0)) (realpart (cexpis:type@2 (imagpart @0))))
4272 (mult @1 (imagpart @2)))))))
4274 (if (canonicalize_math_p ())
4275 /* floor(x) -> trunc(x) if x is nonnegative. */
4276 (for floors (FLOOR_ALL)
4279 (floors tree_expr_nonnegative_p@0)
4282 (match double_value_p
4284 (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == double_type_node)))
4285 (for froms (BUILT_IN_TRUNCL
4297 /* truncl(extend(x)) -> extend(trunc(x)), etc., if x is a double. */
4298 (if (optimize && canonicalize_math_p ())
4300 (froms (convert double_value_p@0))
4301 (convert (tos @0)))))
4303 (match float_value_p
4305 (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == float_type_node)))
4306 (for froms (BUILT_IN_TRUNCL BUILT_IN_TRUNC
4307 BUILT_IN_FLOORL BUILT_IN_FLOOR
4308 BUILT_IN_CEILL BUILT_IN_CEIL
4309 BUILT_IN_ROUNDL BUILT_IN_ROUND
4310 BUILT_IN_NEARBYINTL BUILT_IN_NEARBYINT
4311 BUILT_IN_RINTL BUILT_IN_RINT)
4312 tos (BUILT_IN_TRUNCF BUILT_IN_TRUNCF
4313 BUILT_IN_FLOORF BUILT_IN_FLOORF
4314 BUILT_IN_CEILF BUILT_IN_CEILF
4315 BUILT_IN_ROUNDF BUILT_IN_ROUNDF
4316 BUILT_IN_NEARBYINTF BUILT_IN_NEARBYINTF
4317 BUILT_IN_RINTF BUILT_IN_RINTF)
4318 /* truncl(extend(x)) and trunc(extend(x)) -> extend(truncf(x)), etc.,
4320 (if (optimize && canonicalize_math_p ()
4321 && targetm.libc_has_function (function_c99_misc))
4323 (froms (convert float_value_p@0))
4324 (convert (tos @0)))))
4326 (for froms (XFLOORL XCEILL XROUNDL XRINTL)
4327 tos (XFLOOR XCEIL XROUND XRINT)
4328 /* llfloorl(extend(x)) -> llfloor(x), etc., if x is a double. */
4329 (if (optimize && canonicalize_math_p ())
4331 (froms (convert double_value_p@0))
4334 (for froms (XFLOORL XCEILL XROUNDL XRINTL
4335 XFLOOR XCEIL XROUND XRINT)
4336 tos (XFLOORF XCEILF XROUNDF XRINTF)
4337 /* llfloorl(extend(x)) and llfloor(extend(x)) -> llfloorf(x), etc.,
4339 (if (optimize && canonicalize_math_p ())
4341 (froms (convert float_value_p@0))
4344 (if (canonicalize_math_p ())
4345 /* xfloor(x) -> fix_trunc(x) if x is nonnegative. */
4346 (for floors (IFLOOR LFLOOR LLFLOOR)
4348 (floors tree_expr_nonnegative_p@0)
4351 (if (canonicalize_math_p ())
4352 /* xfloor(x) -> fix_trunc(x), etc., if x is integer valued. */
4353 (for fns (IFLOOR LFLOOR LLFLOOR
4355 IROUND LROUND LLROUND)
4357 (fns integer_valued_real_p@0)
4359 (if (!flag_errno_math)
4360 /* xrint(x) -> fix_trunc(x), etc., if x is integer valued. */
4361 (for rints (IRINT LRINT LLRINT)
4363 (rints integer_valued_real_p@0)
4366 (if (canonicalize_math_p ())
4367 (for ifn (IFLOOR ICEIL IROUND IRINT)
4368 lfn (LFLOOR LCEIL LROUND LRINT)
4369 llfn (LLFLOOR LLCEIL LLROUND LLRINT)
4370 /* Canonicalize iround (x) to lround (x) on ILP32 targets where
4371 sizeof (int) == sizeof (long). */
4372 (if (TYPE_PRECISION (integer_type_node)
4373 == TYPE_PRECISION (long_integer_type_node))
4376 (lfn:long_integer_type_node @0)))
4377 /* Canonicalize llround (x) to lround (x) on LP64 targets where
4378 sizeof (long long) == sizeof (long). */
4379 (if (TYPE_PRECISION (long_long_integer_type_node)
4380 == TYPE_PRECISION (long_integer_type_node))
4383 (lfn:long_integer_type_node @0)))))
4385 /* cproj(x) -> x if we're ignoring infinities. */
4388 (if (!HONOR_INFINITIES (type))
4391 /* If the real part is inf and the imag part is known to be
4392 nonnegative, return (inf + 0i). */
4394 (CPROJ (complex REAL_CST@0 tree_expr_nonnegative_p@1))
4395 (if (real_isinf (TREE_REAL_CST_PTR (@0)))
4396 { build_complex_inf (type, false); }))
4398 /* If the imag part is inf, return (inf+I*copysign(0,imag)). */
4400 (CPROJ (complex @0 REAL_CST@1))
4401 (if (real_isinf (TREE_REAL_CST_PTR (@1)))
4402 { build_complex_inf (type, TREE_REAL_CST_PTR (@1)->sign); }))
4408 (pows @0 REAL_CST@1)
4410 const REAL_VALUE_TYPE *value = TREE_REAL_CST_PTR (@1);
4411 REAL_VALUE_TYPE tmp;
4414 /* pow(x,0) -> 1. */
4415 (if (real_equal (value, &dconst0))
4416 { build_real (type, dconst1); })
4417 /* pow(x,1) -> x. */
4418 (if (real_equal (value, &dconst1))
4420 /* pow(x,-1) -> 1/x. */
4421 (if (real_equal (value, &dconstm1))
4422 (rdiv { build_real (type, dconst1); } @0))
4423 /* pow(x,0.5) -> sqrt(x). */
4424 (if (flag_unsafe_math_optimizations
4425 && canonicalize_math_p ()
4426 && real_equal (value, &dconsthalf))
4428 /* pow(x,1/3) -> cbrt(x). */
4429 (if (flag_unsafe_math_optimizations
4430 && canonicalize_math_p ()
4431 && (tmp = real_value_truncate (TYPE_MODE (type), dconst_third ()),
4432 real_equal (value, &tmp)))
4435 /* powi(1,x) -> 1. */
4437 (POWI real_onep@0 @1)
4441 (POWI @0 INTEGER_CST@1)
4443 /* powi(x,0) -> 1. */
4444 (if (wi::to_wide (@1) == 0)
4445 { build_real (type, dconst1); })
4446 /* powi(x,1) -> x. */
4447 (if (wi::to_wide (@1) == 1)
4449 /* powi(x,-1) -> 1/x. */
4450 (if (wi::to_wide (@1) == -1)
4451 (rdiv { build_real (type, dconst1); } @0))))
4453 /* Narrowing of arithmetic and logical operations.
4455 These are conceptually similar to the transformations performed for
4456 the C/C++ front-ends by shorten_binary_op and shorten_compare. Long
4457 term we want to move all that code out of the front-ends into here. */
4459 /* If we have a narrowing conversion of an arithmetic operation where
4460 both operands are widening conversions from the same type as the outer
4461 narrowing conversion. Then convert the innermost operands to a suitable
4462 unsigned type (to avoid introducing undefined behavior), perform the
4463 operation and convert the result to the desired type. */
4464 (for op (plus minus)
4466 (convert (op:s (convert@2 @0) (convert?@3 @1)))
4467 (if (INTEGRAL_TYPE_P (type)
4468 /* We check for type compatibility between @0 and @1 below,
4469 so there's no need to check that @1/@3 are integral types. */
4470 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
4471 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
4472 /* The precision of the type of each operand must match the
4473 precision of the mode of each operand, similarly for the
4475 && type_has_mode_precision_p (TREE_TYPE (@0))
4476 && type_has_mode_precision_p (TREE_TYPE (@1))
4477 && type_has_mode_precision_p (type)
4478 /* The inner conversion must be a widening conversion. */
4479 && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
4480 && types_match (@0, type)
4481 && (types_match (@0, @1)
4482 /* Or the second operand is const integer or converted const
4483 integer from valueize. */
4484 || TREE_CODE (@1) == INTEGER_CST))
4485 (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
4486 (op @0 (convert @1))
4487 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
4488 (convert (op (convert:utype @0)
4489 (convert:utype @1))))))))
4491 /* This is another case of narrowing, specifically when there's an outer
4492 BIT_AND_EXPR which masks off bits outside the type of the innermost
4493 operands. Like the previous case we have to convert the operands
4494 to unsigned types to avoid introducing undefined behavior for the
4495 arithmetic operation. */
4496 (for op (minus plus)
4498 (bit_and (op:s (convert@2 @0) (convert@3 @1)) INTEGER_CST@4)
4499 (if (INTEGRAL_TYPE_P (type)
4500 /* We check for type compatibility between @0 and @1 below,
4501 so there's no need to check that @1/@3 are integral types. */
4502 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
4503 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
4504 /* The precision of the type of each operand must match the
4505 precision of the mode of each operand, similarly for the
4507 && type_has_mode_precision_p (TREE_TYPE (@0))
4508 && type_has_mode_precision_p (TREE_TYPE (@1))
4509 && type_has_mode_precision_p (type)
4510 /* The inner conversion must be a widening conversion. */
4511 && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
4512 && types_match (@0, @1)
4513 && (tree_int_cst_min_precision (@4, TYPE_SIGN (TREE_TYPE (@0)))
4514 <= TYPE_PRECISION (TREE_TYPE (@0)))
4515 && (wi::to_wide (@4)
4516 & wi::mask (TYPE_PRECISION (TREE_TYPE (@0)),
4517 true, TYPE_PRECISION (type))) == 0)
4518 (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
4519 (with { tree ntype = TREE_TYPE (@0); }
4520 (convert (bit_and (op @0 @1) (convert:ntype @4))))
4521 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
4522 (convert (bit_and (op (convert:utype @0) (convert:utype @1))
4523 (convert:utype @4))))))))
4525 /* Transform (@0 < @1 and @0 < @2) to use min,
4526 (@0 > @1 and @0 > @2) to use max */
4527 (for logic (bit_and bit_and bit_and bit_and bit_ior bit_ior bit_ior bit_ior)
4528 op (lt le gt ge lt le gt ge )
4529 ext (min min max max max max min min )
4531 (logic (op:cs @0 @1) (op:cs @0 @2))
4532 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
4533 && TREE_CODE (@0) != INTEGER_CST)
4534 (op @0 (ext @1 @2)))))
4537 /* signbit(x) -> 0 if x is nonnegative. */
4538 (SIGNBIT tree_expr_nonnegative_p@0)
4539 { integer_zero_node; })
4542 /* signbit(x) -> x<0 if x doesn't have signed zeros. */
4544 (if (!HONOR_SIGNED_ZEROS (@0))
4545 (convert (lt @0 { build_real (TREE_TYPE (@0), dconst0); }))))
4547 /* Transform comparisons of the form X +- C1 CMP C2 to X CMP C2 -+ C1. */
4549 (for op (plus minus)
4552 (cmp (op@3 @0 INTEGER_CST@1) INTEGER_CST@2)
4553 (if (!TREE_OVERFLOW (@1) && !TREE_OVERFLOW (@2)
4554 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0))
4555 && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@0))
4556 && !TYPE_SATURATING (TREE_TYPE (@0)))
4557 (with { tree res = int_const_binop (rop, @2, @1); }
4558 (if (TREE_OVERFLOW (res)
4559 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
4560 { constant_boolean_node (cmp == NE_EXPR, type); }
4561 (if (single_use (@3))
4562 (cmp @0 { TREE_OVERFLOW (res)
4563 ? drop_tree_overflow (res) : res; }))))))))
4564 (for cmp (lt le gt ge)
4565 (for op (plus minus)
4568 (cmp (op@3 @0 INTEGER_CST@1) INTEGER_CST@2)
4569 (if (!TREE_OVERFLOW (@1) && !TREE_OVERFLOW (@2)
4570 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
4571 (with { tree res = int_const_binop (rop, @2, @1); }
4572 (if (TREE_OVERFLOW (res))
4574 fold_overflow_warning (("assuming signed overflow does not occur "
4575 "when simplifying conditional to constant"),
4576 WARN_STRICT_OVERFLOW_CONDITIONAL);
4577 bool less = cmp == LE_EXPR || cmp == LT_EXPR;
4578 /* wi::ges_p (@2, 0) should be sufficient for a signed type. */
4579 bool ovf_high = wi::lt_p (wi::to_wide (@1), 0,
4580 TYPE_SIGN (TREE_TYPE (@1)))
4581 != (op == MINUS_EXPR);
4582 constant_boolean_node (less == ovf_high, type);
4584 (if (single_use (@3))
4587 fold_overflow_warning (("assuming signed overflow does not occur "
4588 "when changing X +- C1 cmp C2 to "
4590 WARN_STRICT_OVERFLOW_COMPARISON);
4592 (cmp @0 { res; })))))))))
4594 /* Canonicalizations of BIT_FIELD_REFs. */
4597 (BIT_FIELD_REF @0 @1 @2)
4599 (if (TREE_CODE (TREE_TYPE (@0)) == COMPLEX_TYPE
4600 && tree_int_cst_equal (@1, TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))))
4602 (if (integer_zerop (@2))
4603 (view_convert (realpart @0)))
4604 (if (tree_int_cst_equal (@2, TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))))
4605 (view_convert (imagpart @0)))))
4606 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
4607 && INTEGRAL_TYPE_P (type)
4608 /* On GIMPLE this should only apply to register arguments. */
4609 && (! GIMPLE || is_gimple_reg (@0))
4610 /* A bit-field-ref that referenced the full argument can be stripped. */
4611 && ((compare_tree_int (@1, TYPE_PRECISION (TREE_TYPE (@0))) == 0
4612 && integer_zerop (@2))
4613 /* Low-parts can be reduced to integral conversions.
4614 ??? The following doesn't work for PDP endian. */
4615 || (BYTES_BIG_ENDIAN == WORDS_BIG_ENDIAN
4616 /* Don't even think about BITS_BIG_ENDIAN. */
4617 && TYPE_PRECISION (TREE_TYPE (@0)) % BITS_PER_UNIT == 0
4618 && TYPE_PRECISION (type) % BITS_PER_UNIT == 0
4619 && compare_tree_int (@2, (BYTES_BIG_ENDIAN
4620 ? (TYPE_PRECISION (TREE_TYPE (@0))
4621 - TYPE_PRECISION (type))
4625 /* Simplify vector extracts. */
4628 (BIT_FIELD_REF CONSTRUCTOR@0 @1 @2)
4629 (if (VECTOR_TYPE_P (TREE_TYPE (@0))
4630 && (types_match (type, TREE_TYPE (TREE_TYPE (@0)))
4631 || (VECTOR_TYPE_P (type)
4632 && types_match (TREE_TYPE (type), TREE_TYPE (TREE_TYPE (@0))))))
4635 tree ctor = (TREE_CODE (@0) == SSA_NAME
4636 ? gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0)) : @0);
4637 tree eltype = TREE_TYPE (TREE_TYPE (ctor));
4638 unsigned HOST_WIDE_INT width = tree_to_uhwi (TYPE_SIZE (eltype));
4639 unsigned HOST_WIDE_INT n = tree_to_uhwi (@1);
4640 unsigned HOST_WIDE_INT idx = tree_to_uhwi (@2);
4643 && (idx % width) == 0
4645 && known_le ((idx + n) / width,
4646 TYPE_VECTOR_SUBPARTS (TREE_TYPE (ctor))))
4651 /* Constructor elements can be subvectors. */
4653 if (CONSTRUCTOR_NELTS (ctor) != 0)
4655 tree cons_elem = TREE_TYPE (CONSTRUCTOR_ELT (ctor, 0)->value);
4656 if (TREE_CODE (cons_elem) == VECTOR_TYPE)
4657 k = TYPE_VECTOR_SUBPARTS (cons_elem);
4659 unsigned HOST_WIDE_INT elt, count, const_k;
4662 /* We keep an exact subset of the constructor elements. */
4663 (if (multiple_p (idx, k, &elt) && multiple_p (n, k, &count))
4664 (if (CONSTRUCTOR_NELTS (ctor) == 0)
4665 { build_constructor (type, NULL); }
4667 (if (elt < CONSTRUCTOR_NELTS (ctor))
4668 (view_convert { CONSTRUCTOR_ELT (ctor, elt)->value; })
4669 { build_zero_cst (type); })
4671 vec<constructor_elt, va_gc> *vals;
4672 vec_alloc (vals, count);
4673 for (unsigned i = 0;
4674 i < count && elt + i < CONSTRUCTOR_NELTS (ctor); ++i)
4675 CONSTRUCTOR_APPEND_ELT (vals, NULL_TREE,
4676 CONSTRUCTOR_ELT (ctor, elt + i)->value);
4677 build_constructor (type, vals);
4679 /* The bitfield references a single constructor element. */
4680 (if (k.is_constant (&const_k)
4681 && idx + n <= (idx / const_k + 1) * const_k)
4683 (if (CONSTRUCTOR_NELTS (ctor) <= idx / const_k)
4684 { build_zero_cst (type); })
4686 (view_convert { CONSTRUCTOR_ELT (ctor, idx / const_k)->value; }))
4687 (BIT_FIELD_REF { CONSTRUCTOR_ELT (ctor, idx / const_k)->value; }
4688 @1 { bitsize_int ((idx % const_k) * width); })))))))))
4690 /* Simplify a bit extraction from a bit insertion for the cases with
4691 the inserted element fully covering the extraction or the insertion
4692 not touching the extraction. */
4694 (BIT_FIELD_REF (bit_insert @0 @1 @ipos) @rsize @rpos)
4697 unsigned HOST_WIDE_INT isize;
4698 if (INTEGRAL_TYPE_P (TREE_TYPE (@1)))
4699 isize = TYPE_PRECISION (TREE_TYPE (@1));
4701 isize = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (@1)));
4704 (if (wi::leu_p (wi::to_wide (@ipos), wi::to_wide (@rpos))
4705 && wi::leu_p (wi::to_wide (@rpos) + wi::to_wide (@rsize),
4706 wi::to_wide (@ipos) + isize))
4707 (BIT_FIELD_REF @1 @rsize { wide_int_to_tree (bitsizetype,
4709 - wi::to_wide (@ipos)); }))
4710 (if (wi::geu_p (wi::to_wide (@ipos),
4711 wi::to_wide (@rpos) + wi::to_wide (@rsize))
4712 || wi::geu_p (wi::to_wide (@rpos),
4713 wi::to_wide (@ipos) + isize))
4714 (BIT_FIELD_REF @0 @rsize @rpos)))))
4716 (if (canonicalize_math_after_vectorization_p ())
4719 (fmas:c (negate @0) @1 @2)
4720 (IFN_FNMA @0 @1 @2))
4722 (fmas @0 @1 (negate @2))
4725 (fmas:c (negate @0) @1 (negate @2))
4726 (IFN_FNMS @0 @1 @2))
4728 (negate (fmas@3 @0 @1 @2))
4729 (if (single_use (@3))
4730 (IFN_FNMS @0 @1 @2))))
4733 (IFN_FMS:c (negate @0) @1 @2)
4734 (IFN_FNMS @0 @1 @2))
4736 (IFN_FMS @0 @1 (negate @2))
4739 (IFN_FMS:c (negate @0) @1 (negate @2))
4740 (IFN_FNMA @0 @1 @2))
4742 (negate (IFN_FMS@3 @0 @1 @2))
4743 (if (single_use (@3))
4744 (IFN_FNMA @0 @1 @2)))
4747 (IFN_FNMA:c (negate @0) @1 @2)
4750 (IFN_FNMA @0 @1 (negate @2))
4751 (IFN_FNMS @0 @1 @2))
4753 (IFN_FNMA:c (negate @0) @1 (negate @2))
4756 (negate (IFN_FNMA@3 @0 @1 @2))
4757 (if (single_use (@3))
4758 (IFN_FMS @0 @1 @2)))
4761 (IFN_FNMS:c (negate @0) @1 @2)
4764 (IFN_FNMS @0 @1 (negate @2))
4765 (IFN_FNMA @0 @1 @2))
4767 (IFN_FNMS:c (negate @0) @1 (negate @2))
4770 (negate (IFN_FNMS@3 @0 @1 @2))
4771 (if (single_use (@3))
4772 (IFN_FMA @0 @1 @2))))
4774 /* POPCOUNT simplifications. */
4775 (for popcount (BUILT_IN_POPCOUNT BUILT_IN_POPCOUNTL BUILT_IN_POPCOUNTLL
4776 BUILT_IN_POPCOUNTIMAX)
4777 /* popcount(X&1) is nop_expr(X&1). */
4780 (if (tree_nonzero_bits (@0) == 1)
4782 /* popcount(X) + popcount(Y) is popcount(X|Y) when X&Y must be zero. */
4784 (plus (popcount:s @0) (popcount:s @1))
4785 (if (wi::bit_and (tree_nonzero_bits (@0), tree_nonzero_bits (@1)) == 0)
4786 (popcount (bit_ior @0 @1))))
4787 /* popcount(X) == 0 is X == 0, and related (in)equalities. */
4788 (for cmp (le eq ne gt)
4791 (cmp (popcount @0) integer_zerop)
4792 (rep @0 { build_zero_cst (TREE_TYPE (@0)); }))))
4801 r = c ? a1 op a2 : b;
4803 if the target can do it in one go. This makes the operation conditional
4804 on c, so could drop potentially-trapping arithmetic, but that's a valid
4805 simplification if the result of the operation isn't needed. */
4806 (for uncond_op (UNCOND_BINARY)
4807 cond_op (COND_BINARY)
4809 (vec_cond @0 (view_convert? (uncond_op@4 @1 @2)) @3)
4810 (with { tree op_type = TREE_TYPE (@4); }
4811 (if (element_precision (type) == element_precision (op_type))
4812 (view_convert (cond_op @0 @1 @2 (view_convert:op_type @3))))))
4814 (vec_cond @0 @1 (view_convert? (uncond_op@4 @2 @3)))
4815 (with { tree op_type = TREE_TYPE (@4); }
4816 (if (element_precision (type) == element_precision (op_type))
4817 (view_convert (cond_op (bit_not @0) @2 @3 (view_convert:op_type @1)))))))