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
81 mult trunc_div trunc_mod rdiv
83 bit_and bit_ior bit_xor)
84 (define_operator_list COND_BINARY
85 IFN_COND_ADD IFN_COND_SUB
86 IFN_COND_MUL IFN_COND_DIV IFN_COND_MOD IFN_COND_RDIV
87 IFN_COND_MIN IFN_COND_MAX
88 IFN_COND_AND IFN_COND_IOR IFN_COND_XOR)
90 /* As opposed to convert?, this still creates a single pattern, so
91 it is not a suitable replacement for convert? in all cases. */
92 (match (nop_convert @0)
94 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))))
95 (match (nop_convert @0)
97 (if (VECTOR_TYPE_P (type) && VECTOR_TYPE_P (TREE_TYPE (@0))
98 && known_eq (TYPE_VECTOR_SUBPARTS (type),
99 TYPE_VECTOR_SUBPARTS (TREE_TYPE (@0)))
100 && tree_nop_conversion_p (TREE_TYPE (type), TREE_TYPE (TREE_TYPE (@0))))))
101 /* This one has to be last, or it shadows the others. */
102 (match (nop_convert @0)
105 /* Transform likes of (char) ABS_EXPR <(int) x> into (char) ABSU_EXPR <x>
106 ABSU_EXPR returns unsigned absolute value of the operand and the operand
107 of the ABSU_EXPR will have the corresponding signed type. */
108 (simplify (abs (convert @0))
109 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
110 && !TYPE_UNSIGNED (TREE_TYPE (@0))
111 && element_precision (type) > element_precision (TREE_TYPE (@0)))
112 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
113 (convert (absu:utype @0)))))
116 /* Simplifications of operations with one constant operand and
117 simplifications to constants or single values. */
119 (for op (plus pointer_plus minus bit_ior bit_xor)
121 (op @0 integer_zerop)
124 /* 0 +p index -> (type)index */
126 (pointer_plus integer_zerop @1)
127 (non_lvalue (convert @1)))
129 /* ptr - 0 -> (type)ptr */
131 (pointer_diff @0 integer_zerop)
134 /* See if ARG1 is zero and X + ARG1 reduces to X.
135 Likewise if the operands are reversed. */
137 (plus:c @0 real_zerop@1)
138 (if (fold_real_zero_addition_p (type, @1, 0))
141 /* See if ARG1 is zero and X - ARG1 reduces to X. */
143 (minus @0 real_zerop@1)
144 (if (fold_real_zero_addition_p (type, @1, 1))
148 This is unsafe for certain floats even in non-IEEE formats.
149 In IEEE, it is unsafe because it does wrong for NaNs.
150 Also note that operand_equal_p is always false if an operand
154 (if (!FLOAT_TYPE_P (type) || !HONOR_NANS (type))
155 { build_zero_cst (type); }))
157 (pointer_diff @@0 @0)
158 { build_zero_cst (type); })
161 (mult @0 integer_zerop@1)
164 /* Maybe fold x * 0 to 0. The expressions aren't the same
165 when x is NaN, since x * 0 is also NaN. Nor are they the
166 same in modes with signed zeros, since multiplying a
167 negative value by 0 gives -0, not +0. */
169 (mult @0 real_zerop@1)
170 (if (!HONOR_NANS (type) && !HONOR_SIGNED_ZEROS (type))
173 /* In IEEE floating point, x*1 is not equivalent to x for snans.
174 Likewise for complex arithmetic with signed zeros. */
177 (if (!HONOR_SNANS (type)
178 && (!HONOR_SIGNED_ZEROS (type)
179 || !COMPLEX_FLOAT_TYPE_P (type)))
182 /* Transform x * -1.0 into -x. */
184 (mult @0 real_minus_onep)
185 (if (!HONOR_SNANS (type)
186 && (!HONOR_SIGNED_ZEROS (type)
187 || !COMPLEX_FLOAT_TYPE_P (type)))
190 (for cmp (gt ge lt le)
191 outp (convert convert negate negate)
192 outn (negate negate convert convert)
193 /* Transform (X > 0.0 ? 1.0 : -1.0) into copysign(1, X). */
194 /* Transform (X >= 0.0 ? 1.0 : -1.0) into copysign(1, X). */
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). */
198 (cond (cmp @0 real_zerop) real_onep@1 real_minus_onep)
199 (if (!HONOR_NANS (type) && !HONOR_SIGNED_ZEROS (type)
200 && types_match (type, TREE_TYPE (@0)))
202 (if (types_match (type, float_type_node))
203 (BUILT_IN_COPYSIGNF @1 (outp @0)))
204 (if (types_match (type, double_type_node))
205 (BUILT_IN_COPYSIGN @1 (outp @0)))
206 (if (types_match (type, long_double_type_node))
207 (BUILT_IN_COPYSIGNL @1 (outp @0))))))
208 /* Transform (X > 0.0 ? -1.0 : 1.0) into copysign(1,-X). */
209 /* Transform (X >= 0.0 ? -1.0 : 1.0) into copysign(1,-X). */
210 /* Transform (X < 0.0 ? -1.0 : 1.0) into copysign(1,X). */
211 /* Transform (X <= 0.0 ? -1.0 : 1.0) into copysign(1,X). */
213 (cond (cmp @0 real_zerop) real_minus_onep real_onep@1)
214 (if (!HONOR_NANS (type) && !HONOR_SIGNED_ZEROS (type)
215 && types_match (type, TREE_TYPE (@0)))
217 (if (types_match (type, float_type_node))
218 (BUILT_IN_COPYSIGNF @1 (outn @0)))
219 (if (types_match (type, double_type_node))
220 (BUILT_IN_COPYSIGN @1 (outn @0)))
221 (if (types_match (type, long_double_type_node))
222 (BUILT_IN_COPYSIGNL @1 (outn @0)))))))
224 /* Transform X * copysign (1.0, X) into abs(X). */
226 (mult:c @0 (COPYSIGN_ALL real_onep @0))
227 (if (!HONOR_NANS (type) && !HONOR_SIGNED_ZEROS (type))
230 /* Transform X * copysign (1.0, -X) into -abs(X). */
232 (mult:c @0 (COPYSIGN_ALL real_onep (negate @0)))
233 (if (!HONOR_NANS (type) && !HONOR_SIGNED_ZEROS (type))
236 /* Transform copysign (CST, X) into copysign (ABS(CST), X). */
238 (COPYSIGN_ALL REAL_CST@0 @1)
239 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@0)))
240 (COPYSIGN_ALL (negate @0) @1)))
242 /* X * 1, X / 1 -> X. */
243 (for op (mult trunc_div ceil_div floor_div round_div exact_div)
248 /* (A / (1 << B)) -> (A >> B).
249 Only for unsigned A. For signed A, this would not preserve rounding
251 For example: (-1 / ( 1 << B)) != -1 >> B. */
253 (trunc_div @0 (lshift integer_onep@1 @2))
254 (if ((TYPE_UNSIGNED (type) || tree_expr_nonnegative_p (@0))
255 && (!VECTOR_TYPE_P (type)
256 || target_supports_op_p (type, RSHIFT_EXPR, optab_vector)
257 || target_supports_op_p (type, RSHIFT_EXPR, optab_scalar)))
260 /* Preserve explicit divisions by 0: the C++ front-end wants to detect
261 undefined behavior in constexpr evaluation, and assuming that the division
262 traps enables better optimizations than these anyway. */
263 (for div (trunc_div ceil_div floor_div round_div exact_div)
264 /* 0 / X is always zero. */
266 (div integer_zerop@0 @1)
267 /* But not for 0 / 0 so that we can get the proper warnings and errors. */
268 (if (!integer_zerop (@1))
272 (div @0 integer_minus_onep@1)
273 (if (!TYPE_UNSIGNED (type))
278 /* But not for 0 / 0 so that we can get the proper warnings and errors.
279 And not for _Fract types where we can't build 1. */
280 (if (!integer_zerop (@0) && !ALL_FRACT_MODE_P (TYPE_MODE (type)))
281 { build_one_cst (type); }))
282 /* X / abs (X) is X < 0 ? -1 : 1. */
285 (if (INTEGRAL_TYPE_P (type)
286 && TYPE_OVERFLOW_UNDEFINED (type))
287 (cond (lt @0 { build_zero_cst (type); })
288 { build_minus_one_cst (type); } { build_one_cst (type); })))
291 (div:C @0 (negate @0))
292 (if ((INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
293 && TYPE_OVERFLOW_UNDEFINED (type))
294 { build_minus_one_cst (type); })))
296 /* For unsigned integral types, FLOOR_DIV_EXPR is the same as
297 TRUNC_DIV_EXPR. Rewrite into the latter in this case. */
300 (if ((INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
301 && TYPE_UNSIGNED (type))
304 /* Combine two successive divisions. Note that combining ceil_div
305 and floor_div is trickier and combining round_div even more so. */
306 (for div (trunc_div exact_div)
308 (div (div @0 INTEGER_CST@1) INTEGER_CST@2)
311 wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@2),
312 TYPE_SIGN (type), &overflow_p);
315 (div @0 { wide_int_to_tree (type, mul); })
316 (if (TYPE_UNSIGNED (type)
317 || mul != wi::min_value (TYPE_PRECISION (type), SIGNED))
318 { build_zero_cst (type); })))))
320 /* Combine successive multiplications. Similar to above, but handling
321 overflow is different. */
323 (mult (mult @0 INTEGER_CST@1) INTEGER_CST@2)
326 wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@2),
327 TYPE_SIGN (type), &overflow_p);
329 /* Skip folding on overflow: the only special case is @1 * @2 == -INT_MIN,
330 otherwise undefined overflow implies that @0 must be zero. */
331 (if (!overflow_p || TYPE_OVERFLOW_WRAPS (type))
332 (mult @0 { wide_int_to_tree (type, mul); }))))
334 /* Optimize A / A to 1.0 if we don't care about
335 NaNs or Infinities. */
338 (if (FLOAT_TYPE_P (type)
339 && ! HONOR_NANS (type)
340 && ! HONOR_INFINITIES (type))
341 { build_one_cst (type); }))
343 /* Optimize -A / A to -1.0 if we don't care about
344 NaNs or Infinities. */
346 (rdiv:C @0 (negate @0))
347 (if (FLOAT_TYPE_P (type)
348 && ! HONOR_NANS (type)
349 && ! HONOR_INFINITIES (type))
350 { build_minus_one_cst (type); }))
352 /* PR71078: x / abs(x) -> copysign (1.0, x) */
354 (rdiv:C (convert? @0) (convert? (abs @0)))
355 (if (SCALAR_FLOAT_TYPE_P (type)
356 && ! HONOR_NANS (type)
357 && ! HONOR_INFINITIES (type))
359 (if (types_match (type, float_type_node))
360 (BUILT_IN_COPYSIGNF { build_one_cst (type); } (convert @0)))
361 (if (types_match (type, double_type_node))
362 (BUILT_IN_COPYSIGN { build_one_cst (type); } (convert @0)))
363 (if (types_match (type, long_double_type_node))
364 (BUILT_IN_COPYSIGNL { build_one_cst (type); } (convert @0))))))
366 /* In IEEE floating point, x/1 is not equivalent to x for snans. */
369 (if (!HONOR_SNANS (type))
372 /* In IEEE floating point, x/-1 is not equivalent to -x for snans. */
374 (rdiv @0 real_minus_onep)
375 (if (!HONOR_SNANS (type))
378 (if (flag_reciprocal_math)
379 /* Convert (A/B)/C to A/(B*C). */
381 (rdiv (rdiv:s @0 @1) @2)
382 (rdiv @0 (mult @1 @2)))
384 /* Canonicalize x / (C1 * y) to (x * C2) / y. */
386 (rdiv @0 (mult:s @1 REAL_CST@2))
388 { tree tem = const_binop (RDIV_EXPR, type, build_one_cst (type), @2); }
390 (rdiv (mult @0 { tem; } ) @1))))
392 /* Convert A/(B/C) to (A/B)*C */
394 (rdiv @0 (rdiv:s @1 @2))
395 (mult (rdiv @0 @1) @2)))
397 /* Simplify x / (- y) to -x / y. */
399 (rdiv @0 (negate @1))
400 (rdiv (negate @0) @1))
402 /* Optimize (X & (-A)) / A where A is a power of 2, to X >> log2(A) */
403 (for div (trunc_div ceil_div floor_div round_div exact_div)
405 (div (convert? (bit_and @0 INTEGER_CST@1)) INTEGER_CST@2)
406 (if (integer_pow2p (@2)
407 && tree_int_cst_sgn (@2) > 0
408 && tree_nop_conversion_p (type, TREE_TYPE (@0))
409 && wi::to_wide (@2) + wi::to_wide (@1) == 0)
411 { build_int_cst (integer_type_node,
412 wi::exact_log2 (wi::to_wide (@2))); }))))
414 /* If ARG1 is a constant, we can convert this to a multiply by the
415 reciprocal. This does not have the same rounding properties,
416 so only do this if -freciprocal-math. We can actually
417 always safely do it if ARG1 is a power of two, but it's hard to
418 tell if it is or not in a portable manner. */
419 (for cst (REAL_CST COMPLEX_CST VECTOR_CST)
423 (if (flag_reciprocal_math
426 { tree tem = const_binop (RDIV_EXPR, type, build_one_cst (type), @1); }
428 (mult @0 { tem; } )))
429 (if (cst != COMPLEX_CST)
430 (with { tree inverse = exact_inverse (type, @1); }
432 (mult @0 { inverse; } ))))))))
434 (for mod (ceil_mod floor_mod round_mod trunc_mod)
435 /* 0 % X is always zero. */
437 (mod integer_zerop@0 @1)
438 /* But not for 0 % 0 so that we can get the proper warnings and errors. */
439 (if (!integer_zerop (@1))
441 /* X % 1 is always zero. */
443 (mod @0 integer_onep)
444 { build_zero_cst (type); })
445 /* X % -1 is zero. */
447 (mod @0 integer_minus_onep@1)
448 (if (!TYPE_UNSIGNED (type))
449 { build_zero_cst (type); }))
453 /* But not for 0 % 0 so that we can get the proper warnings and errors. */
454 (if (!integer_zerop (@0))
455 { build_zero_cst (type); }))
456 /* (X % Y) % Y is just X % Y. */
458 (mod (mod@2 @0 @1) @1)
460 /* From extract_muldiv_1: (X * C1) % C2 is zero if C1 is a multiple of C2. */
462 (mod (mult @0 INTEGER_CST@1) INTEGER_CST@2)
463 (if (ANY_INTEGRAL_TYPE_P (type)
464 && TYPE_OVERFLOW_UNDEFINED (type)
465 && wi::multiple_of_p (wi::to_wide (@1), wi::to_wide (@2),
467 { build_zero_cst (type); })))
469 /* X % -C is the same as X % C. */
471 (trunc_mod @0 INTEGER_CST@1)
472 (if (TYPE_SIGN (type) == SIGNED
473 && !TREE_OVERFLOW (@1)
474 && wi::neg_p (wi::to_wide (@1))
475 && !TYPE_OVERFLOW_TRAPS (type)
476 /* Avoid this transformation if C is INT_MIN, i.e. C == -C. */
477 && !sign_bit_p (@1, @1))
478 (trunc_mod @0 (negate @1))))
480 /* X % -Y is the same as X % Y. */
482 (trunc_mod @0 (convert? (negate @1)))
483 (if (INTEGRAL_TYPE_P (type)
484 && !TYPE_UNSIGNED (type)
485 && !TYPE_OVERFLOW_TRAPS (type)
486 && tree_nop_conversion_p (type, TREE_TYPE (@1))
487 /* Avoid this transformation if X might be INT_MIN or
488 Y might be -1, because we would then change valid
489 INT_MIN % -(-1) into invalid INT_MIN % -1. */
490 && (expr_not_equal_to (@0, wi::to_wide (TYPE_MIN_VALUE (type)))
491 || expr_not_equal_to (@1, wi::minus_one (TYPE_PRECISION
493 (trunc_mod @0 (convert @1))))
495 /* X - (X / Y) * Y is the same as X % Y. */
497 (minus (convert1? @0) (convert2? (mult:c (trunc_div @@0 @@1) @1)))
498 (if (INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
499 (convert (trunc_mod @0 @1))))
501 /* Optimize TRUNC_MOD_EXPR by a power of two into a BIT_AND_EXPR,
502 i.e. "X % C" into "X & (C - 1)", if X and C are positive.
503 Also optimize A % (C << N) where C is a power of 2,
504 to A & ((C << N) - 1). */
505 (match (power_of_two_cand @1)
507 (match (power_of_two_cand @1)
508 (lshift INTEGER_CST@1 @2))
509 (for mod (trunc_mod floor_mod)
511 (mod @0 (convert?@3 (power_of_two_cand@1 @2)))
512 (if ((TYPE_UNSIGNED (type)
513 || tree_expr_nonnegative_p (@0))
514 && tree_nop_conversion_p (type, TREE_TYPE (@3))
515 && integer_pow2p (@2) && tree_int_cst_sgn (@2) > 0)
516 (bit_and @0 (convert (minus @1 { build_int_cst (TREE_TYPE (@1), 1); }))))))
518 /* Simplify (unsigned t * 2)/2 -> unsigned t & 0x7FFFFFFF. */
520 (trunc_div (mult @0 integer_pow2p@1) @1)
521 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
522 (bit_and @0 { wide_int_to_tree
523 (type, wi::mask (TYPE_PRECISION (type)
524 - wi::exact_log2 (wi::to_wide (@1)),
525 false, TYPE_PRECISION (type))); })))
527 /* Simplify (unsigned t / 2) * 2 -> unsigned t & ~1. */
529 (mult (trunc_div @0 integer_pow2p@1) @1)
530 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
531 (bit_and @0 (negate @1))))
533 /* Simplify (t * 2) / 2) -> t. */
534 (for div (trunc_div ceil_div floor_div round_div exact_div)
536 (div (mult:c @0 @1) @1)
537 (if (ANY_INTEGRAL_TYPE_P (type)
538 && TYPE_OVERFLOW_UNDEFINED (type))
542 /* Simplify cos(-x) and cos(|x|) -> cos(x). Similarly for cosh. */
547 /* Simplify pow(-x, y) and pow(|x|,y) -> pow(x,y) if y is an even integer. */
550 (pows (op @0) REAL_CST@1)
551 (with { HOST_WIDE_INT n; }
552 (if (real_isinteger (&TREE_REAL_CST (@1), &n) && (n & 1) == 0)
554 /* Likewise for powi. */
557 (pows (op @0) INTEGER_CST@1)
558 (if ((wi::to_wide (@1) & 1) == 0)
560 /* Strip negate and abs from both operands of hypot. */
568 /* copysign(-x, y) and copysign(abs(x), y) -> copysign(x, y). */
569 (for copysigns (COPYSIGN_ALL)
571 (copysigns (op @0) @1)
574 /* abs(x)*abs(x) -> x*x. Should be valid for all types. */
579 /* cos(copysign(x, y)) -> cos(x). Similarly for cosh. */
583 (coss (copysigns @0 @1))
586 /* pow(copysign(x, y), z) -> pow(x, z) if z is an even integer. */
590 (pows (copysigns @0 @2) REAL_CST@1)
591 (with { HOST_WIDE_INT n; }
592 (if (real_isinteger (&TREE_REAL_CST (@1), &n) && (n & 1) == 0)
594 /* Likewise for powi. */
598 (pows (copysigns @0 @2) INTEGER_CST@1)
599 (if ((wi::to_wide (@1) & 1) == 0)
604 /* hypot(copysign(x, y), z) -> hypot(x, z). */
606 (hypots (copysigns @0 @1) @2)
608 /* hypot(x, copysign(y, z)) -> hypot(x, y). */
610 (hypots @0 (copysigns @1 @2))
613 /* copysign(x, CST) -> [-]abs (x). */
614 (for copysigns (COPYSIGN_ALL)
616 (copysigns @0 REAL_CST@1)
617 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
621 /* copysign(copysign(x, y), z) -> copysign(x, z). */
622 (for copysigns (COPYSIGN_ALL)
624 (copysigns (copysigns @0 @1) @2)
627 /* copysign(x,y)*copysign(x,y) -> x*x. */
628 (for copysigns (COPYSIGN_ALL)
630 (mult (copysigns@2 @0 @1) @2)
633 /* ccos(-x) -> ccos(x). Similarly for ccosh. */
634 (for ccoss (CCOS CCOSH)
639 /* cabs(-x) and cos(conj(x)) -> cabs(x). */
640 (for ops (conj negate)
646 /* Fold (a * (1 << b)) into (a << b) */
648 (mult:c @0 (convert? (lshift integer_onep@1 @2)))
649 (if (! FLOAT_TYPE_P (type)
650 && tree_nop_conversion_p (type, TREE_TYPE (@1)))
653 /* Fold (1 << (C - x)) where C = precision(type) - 1
654 into ((1 << C) >> x). */
656 (lshift integer_onep@0 (minus@1 INTEGER_CST@2 @3))
657 (if (INTEGRAL_TYPE_P (type)
658 && wi::eq_p (wi::to_wide (@2), TYPE_PRECISION (type) - 1)
660 (if (TYPE_UNSIGNED (type))
661 (rshift (lshift @0 @2) @3)
663 { tree utype = unsigned_type_for (type); }
664 (convert (rshift (lshift (convert:utype @0) @2) @3))))))
666 /* Fold (C1/X)*C2 into (C1*C2)/X. */
668 (mult (rdiv@3 REAL_CST@0 @1) REAL_CST@2)
669 (if (flag_associative_math
672 { tree tem = const_binop (MULT_EXPR, type, @0, @2); }
674 (rdiv { tem; } @1)))))
676 /* Simplify ~X & X as zero. */
678 (bit_and:c (convert? @0) (convert? (bit_not @0)))
679 { build_zero_cst (type); })
681 /* PR71636: Transform x & ((1U << b) - 1) -> x & ~(~0U << b); */
683 (bit_and:c @0 (plus:s (lshift:s integer_onep @1) integer_minus_onep))
684 (if (TYPE_UNSIGNED (type))
685 (bit_and @0 (bit_not (lshift { build_all_ones_cst (type); } @1)))))
687 (for bitop (bit_and bit_ior)
689 /* PR35691: Transform
690 (x == 0 & y == 0) -> (x | typeof(x)(y)) == 0.
691 (x != 0 | y != 0) -> (x | typeof(x)(y)) != 0. */
693 (bitop (cmp @0 integer_zerop@2) (cmp @1 integer_zerop))
694 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
695 && INTEGRAL_TYPE_P (TREE_TYPE (@1))
696 && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
697 (cmp (bit_ior @0 (convert @1)) @2)))
699 (x == -1 & y == -1) -> (x & typeof(x)(y)) == -1.
700 (x != -1 | y != -1) -> (x & typeof(x)(y)) != -1. */
702 (bitop (cmp @0 integer_all_onesp@2) (cmp @1 integer_all_onesp))
703 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
704 && INTEGRAL_TYPE_P (TREE_TYPE (@1))
705 && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
706 (cmp (bit_and @0 (convert @1)) @2))))
708 /* Fold (A & ~B) - (A & B) into (A ^ B) - B. */
710 (minus (bit_and:cs @0 (bit_not @1)) (bit_and:cs @0 @1))
711 (minus (bit_xor @0 @1) @1))
713 (minus (bit_and:s @0 INTEGER_CST@2) (bit_and:s @0 INTEGER_CST@1))
714 (if (~wi::to_wide (@2) == wi::to_wide (@1))
715 (minus (bit_xor @0 @1) @1)))
717 /* Fold (A & B) - (A & ~B) into B - (A ^ B). */
719 (minus (bit_and:cs @0 @1) (bit_and:cs @0 (bit_not @1)))
720 (minus @1 (bit_xor @0 @1)))
722 /* Simplify (X & ~Y) |^+ (~X & Y) -> X ^ Y. */
723 (for op (bit_ior bit_xor plus)
725 (op (bit_and:c @0 (bit_not @1)) (bit_and:c (bit_not @0) @1))
728 (op:c (bit_and @0 INTEGER_CST@2) (bit_and (bit_not @0) INTEGER_CST@1))
729 (if (~wi::to_wide (@2) == wi::to_wide (@1))
732 /* PR53979: Transform ((a ^ b) | a) -> (a | b) */
734 (bit_ior:c (bit_xor:c @0 @1) @0)
737 /* (a & ~b) | (a ^ b) --> a ^ b */
739 (bit_ior:c (bit_and:c @0 (bit_not @1)) (bit_xor:c@2 @0 @1))
742 /* (a & ~b) ^ ~a --> ~(a & b) */
744 (bit_xor:c (bit_and:cs @0 (bit_not @1)) (bit_not @0))
745 (bit_not (bit_and @0 @1)))
747 /* (a | b) & ~(a ^ b) --> a & b */
749 (bit_and:c (bit_ior @0 @1) (bit_not (bit_xor:c @0 @1)))
752 /* a | ~(a ^ b) --> a | ~b */
754 (bit_ior:c @0 (bit_not:s (bit_xor:c @0 @1)))
755 (bit_ior @0 (bit_not @1)))
757 /* (a | b) | (a &^ b) --> a | b */
758 (for op (bit_and bit_xor)
760 (bit_ior:c (bit_ior@2 @0 @1) (op:c @0 @1))
763 /* (a & b) | ~(a ^ b) --> ~(a ^ b) */
765 (bit_ior:c (bit_and:c @0 @1) (bit_not@2 (bit_xor @0 @1)))
768 /* ~(~a & b) --> a | ~b */
770 (bit_not (bit_and:cs (bit_not @0) @1))
771 (bit_ior @0 (bit_not @1)))
773 /* Simplify (~X & Y) to X ^ Y if we know that (X & ~Y) is 0. */
776 (bit_and (bit_not SSA_NAME@0) INTEGER_CST@1)
777 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
778 && wi::bit_and_not (get_nonzero_bits (@0), wi::to_wide (@1)) == 0)
782 /* X % Y is smaller than Y. */
785 (cmp (trunc_mod @0 @1) @1)
786 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
787 { constant_boolean_node (cmp == LT_EXPR, type); })))
790 (cmp @1 (trunc_mod @0 @1))
791 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
792 { constant_boolean_node (cmp == GT_EXPR, type); })))
796 (bit_ior @0 integer_all_onesp@1)
801 (bit_ior @0 integer_zerop)
806 (bit_and @0 integer_zerop@1)
812 (for op (bit_ior bit_xor plus)
814 (op:c (convert? @0) (convert? (bit_not @0)))
815 (convert { build_all_ones_cst (TREE_TYPE (@0)); })))
820 { build_zero_cst (type); })
822 /* Canonicalize X ^ ~0 to ~X. */
824 (bit_xor @0 integer_all_onesp@1)
829 (bit_and @0 integer_all_onesp)
832 /* x & x -> x, x | x -> x */
833 (for bitop (bit_and bit_ior)
838 /* x & C -> x if we know that x & ~C == 0. */
841 (bit_and SSA_NAME@0 INTEGER_CST@1)
842 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
843 && wi::bit_and_not (get_nonzero_bits (@0), wi::to_wide (@1)) == 0)
847 /* x + (x & 1) -> (x + 1) & ~1 */
849 (plus:c @0 (bit_and:s @0 integer_onep@1))
850 (bit_and (plus @0 @1) (bit_not @1)))
852 /* x & ~(x & y) -> x & ~y */
853 /* x | ~(x | y) -> x | ~y */
854 (for bitop (bit_and bit_ior)
856 (bitop:c @0 (bit_not (bitop:cs @0 @1)))
857 (bitop @0 (bit_not @1))))
859 /* (x | y) & ~x -> y & ~x */
860 /* (x & y) | ~x -> y | ~x */
861 (for bitop (bit_and bit_ior)
862 rbitop (bit_ior bit_and)
864 (bitop:c (rbitop:c @0 @1) (bit_not@2 @0))
867 /* (x & y) ^ (x | y) -> x ^ y */
869 (bit_xor:c (bit_and @0 @1) (bit_ior @0 @1))
872 /* (x ^ y) ^ (x | y) -> x & y */
874 (bit_xor:c (bit_xor @0 @1) (bit_ior @0 @1))
877 /* (x & y) + (x ^ y) -> x | y */
878 /* (x & y) | (x ^ y) -> x | y */
879 /* (x & y) ^ (x ^ y) -> x | y */
880 (for op (plus bit_ior bit_xor)
882 (op:c (bit_and @0 @1) (bit_xor @0 @1))
885 /* (x & y) + (x | y) -> x + y */
887 (plus:c (bit_and @0 @1) (bit_ior @0 @1))
890 /* (x + y) - (x | y) -> x & y */
892 (minus (plus @0 @1) (bit_ior @0 @1))
893 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
894 && !TYPE_SATURATING (type))
897 /* (x + y) - (x & y) -> x | y */
899 (minus (plus @0 @1) (bit_and @0 @1))
900 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
901 && !TYPE_SATURATING (type))
904 /* (x | y) - (x ^ y) -> x & y */
906 (minus (bit_ior @0 @1) (bit_xor @0 @1))
909 /* (x | y) - (x & y) -> x ^ y */
911 (minus (bit_ior @0 @1) (bit_and @0 @1))
914 /* (x | y) & ~(x & y) -> x ^ y */
916 (bit_and:c (bit_ior @0 @1) (bit_not (bit_and @0 @1)))
919 /* (x | y) & (~x ^ y) -> x & y */
921 (bit_and:c (bit_ior:c @0 @1) (bit_xor:c @1 (bit_not @0)))
924 /* ~x & ~y -> ~(x | y)
925 ~x | ~y -> ~(x & y) */
926 (for op (bit_and bit_ior)
927 rop (bit_ior bit_and)
929 (op (convert1? (bit_not @0)) (convert2? (bit_not @1)))
930 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
931 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
932 (bit_not (rop (convert @0) (convert @1))))))
934 /* If we are XORing or adding two BIT_AND_EXPR's, both of which are and'ing
935 with a constant, and the two constants have no bits in common,
936 we should treat this as a BIT_IOR_EXPR since this may produce more
938 (for op (bit_xor plus)
940 (op (convert1? (bit_and@4 @0 INTEGER_CST@1))
941 (convert2? (bit_and@5 @2 INTEGER_CST@3)))
942 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
943 && tree_nop_conversion_p (type, TREE_TYPE (@2))
944 && (wi::to_wide (@1) & wi::to_wide (@3)) == 0)
945 (bit_ior (convert @4) (convert @5)))))
947 /* (X | Y) ^ X -> Y & ~ X*/
949 (bit_xor:c (convert1? (bit_ior:c @@0 @1)) (convert2? @0))
950 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
951 (convert (bit_and @1 (bit_not @0)))))
953 /* Convert ~X ^ ~Y to X ^ Y. */
955 (bit_xor (convert1? (bit_not @0)) (convert2? (bit_not @1)))
956 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
957 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
958 (bit_xor (convert @0) (convert @1))))
960 /* Convert ~X ^ C to X ^ ~C. */
962 (bit_xor (convert? (bit_not @0)) INTEGER_CST@1)
963 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
964 (bit_xor (convert @0) (bit_not @1))))
966 /* Fold (X & Y) ^ Y and (X ^ Y) & Y as ~X & Y. */
967 (for opo (bit_and bit_xor)
968 opi (bit_xor bit_and)
970 (opo:c (opi:c @0 @1) @1)
971 (bit_and (bit_not @0) @1)))
973 /* Given a bit-wise operation CODE applied to ARG0 and ARG1, see if both
974 operands are another bit-wise operation with a common input. If so,
975 distribute the bit operations to save an operation and possibly two if
976 constants are involved. For example, convert
977 (A | B) & (A | C) into A | (B & C)
978 Further simplification will occur if B and C are constants. */
979 (for op (bit_and bit_ior bit_xor)
980 rop (bit_ior bit_and bit_and)
982 (op (convert? (rop:c @@0 @1)) (convert? (rop:c @0 @2)))
983 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
984 && tree_nop_conversion_p (type, TREE_TYPE (@2)))
985 (rop (convert @0) (op (convert @1) (convert @2))))))
987 /* Some simple reassociation for bit operations, also handled in reassoc. */
988 /* (X & Y) & Y -> X & Y
989 (X | Y) | Y -> X | Y */
990 (for op (bit_and bit_ior)
992 (op:c (convert1?@2 (op:c @0 @@1)) (convert2? @1))
994 /* (X ^ Y) ^ Y -> X */
996 (bit_xor:c (convert1? (bit_xor:c @0 @@1)) (convert2? @1))
998 /* (X & Y) & (X & Z) -> (X & Y) & Z
999 (X | Y) | (X | Z) -> (X | Y) | Z */
1000 (for op (bit_and bit_ior)
1002 (op (convert1?@3 (op:c@4 @0 @1)) (convert2?@5 (op:c@6 @0 @2)))
1003 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1004 && tree_nop_conversion_p (type, TREE_TYPE (@2)))
1005 (if (single_use (@5) && single_use (@6))
1006 (op @3 (convert @2))
1007 (if (single_use (@3) && single_use (@4))
1008 (op (convert @1) @5))))))
1009 /* (X ^ Y) ^ (X ^ Z) -> Y ^ Z */
1011 (bit_xor (convert1? (bit_xor:c @0 @1)) (convert2? (bit_xor:c @0 @2)))
1012 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1013 && tree_nop_conversion_p (type, TREE_TYPE (@2)))
1014 (bit_xor (convert @1) (convert @2))))
1023 (abs tree_expr_nonnegative_p@0)
1026 /* A few cases of fold-const.c negate_expr_p predicate. */
1027 (match negate_expr_p
1029 (if ((INTEGRAL_TYPE_P (type)
1030 && TYPE_UNSIGNED (type))
1031 || (!TYPE_OVERFLOW_SANITIZED (type)
1032 && may_negate_without_overflow_p (t)))))
1033 (match negate_expr_p
1035 (match negate_expr_p
1037 (if (!TYPE_OVERFLOW_SANITIZED (type))))
1038 (match negate_expr_p
1040 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (t)))))
1041 /* VECTOR_CST handling of non-wrapping types would recurse in unsupported
1043 (match negate_expr_p
1045 (if (FLOAT_TYPE_P (TREE_TYPE (type)) || TYPE_OVERFLOW_WRAPS (type))))
1046 (match negate_expr_p
1048 (if ((ANY_INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_WRAPS (type))
1049 || (FLOAT_TYPE_P (type)
1050 && !HONOR_SIGN_DEPENDENT_ROUNDING (type)
1051 && !HONOR_SIGNED_ZEROS (type)))))
1053 /* (-A) * (-B) -> A * B */
1055 (mult:c (convert1? (negate @0)) (convert2? negate_expr_p@1))
1056 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
1057 && tree_nop_conversion_p (type, TREE_TYPE (@1)))
1058 (mult (convert @0) (convert (negate @1)))))
1060 /* -(A + B) -> (-B) - A. */
1062 (negate (plus:c @0 negate_expr_p@1))
1063 (if (!HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type))
1064 && !HONOR_SIGNED_ZEROS (element_mode (type)))
1065 (minus (negate @1) @0)))
1067 /* -(A - B) -> B - A. */
1069 (negate (minus @0 @1))
1070 (if ((ANY_INTEGRAL_TYPE_P (type) && !TYPE_OVERFLOW_SANITIZED (type))
1071 || (FLOAT_TYPE_P (type)
1072 && !HONOR_SIGN_DEPENDENT_ROUNDING (type)
1073 && !HONOR_SIGNED_ZEROS (type)))
1076 (negate (pointer_diff @0 @1))
1077 (if (TYPE_OVERFLOW_UNDEFINED (type))
1078 (pointer_diff @1 @0)))
1080 /* A - B -> A + (-B) if B is easily negatable. */
1082 (minus @0 negate_expr_p@1)
1083 (if (!FIXED_POINT_TYPE_P (type))
1084 (plus @0 (negate @1))))
1086 /* Try to fold (type) X op CST -> (type) (X op ((type-x) CST))
1088 For bitwise binary operations apply operand conversions to the
1089 binary operation result instead of to the operands. This allows
1090 to combine successive conversions and bitwise binary operations.
1091 We combine the above two cases by using a conditional convert. */
1092 (for bitop (bit_and bit_ior bit_xor)
1094 (bitop (convert @0) (convert? @1))
1095 (if (((TREE_CODE (@1) == INTEGER_CST
1096 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
1097 && int_fits_type_p (@1, TREE_TYPE (@0)))
1098 || types_match (@0, @1))
1099 /* ??? This transform conflicts with fold-const.c doing
1100 Convert (T)(x & c) into (T)x & (T)c, if c is an integer
1101 constants (if x has signed type, the sign bit cannot be set
1102 in c). This folds extension into the BIT_AND_EXPR.
1103 Restrict it to GIMPLE to avoid endless recursions. */
1104 && (bitop != BIT_AND_EXPR || GIMPLE)
1105 && (/* That's a good idea if the conversion widens the operand, thus
1106 after hoisting the conversion the operation will be narrower. */
1107 TYPE_PRECISION (TREE_TYPE (@0)) < TYPE_PRECISION (type)
1108 /* It's also a good idea if the conversion is to a non-integer
1110 || GET_MODE_CLASS (TYPE_MODE (type)) != MODE_INT
1111 /* Or if the precision of TO is not the same as the precision
1113 || !type_has_mode_precision_p (type)))
1114 (convert (bitop @0 (convert @1))))))
1116 (for bitop (bit_and bit_ior)
1117 rbitop (bit_ior bit_and)
1118 /* (x | y) & x -> x */
1119 /* (x & y) | x -> x */
1121 (bitop:c (rbitop:c @0 @1) @0)
1123 /* (~x | y) & x -> x & y */
1124 /* (~x & y) | x -> x | y */
1126 (bitop:c (rbitop:c (bit_not @0) @1) @0)
1129 /* (x | CST1) & CST2 -> (x & CST2) | (CST1 & CST2) */
1131 (bit_and (bit_ior @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
1132 (bit_ior (bit_and @0 @2) (bit_and @1 @2)))
1134 /* Combine successive equal operations with constants. */
1135 (for bitop (bit_and bit_ior bit_xor)
1137 (bitop (bitop @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
1138 (if (!CONSTANT_CLASS_P (@0))
1139 /* This is the canonical form regardless of whether (bitop @1 @2) can be
1140 folded to a constant. */
1141 (bitop @0 (bitop @1 @2))
1142 /* In this case we have three constants and (bitop @0 @1) doesn't fold
1143 to a constant. This can happen if @0 or @1 is a POLY_INT_CST and if
1144 the values involved are such that the operation can't be decided at
1145 compile time. Try folding one of @0 or @1 with @2 to see whether
1146 that combination can be decided at compile time.
1148 Keep the existing form if both folds fail, to avoid endless
1150 (with { tree cst1 = const_binop (bitop, type, @0, @2); }
1152 (bitop @1 { cst1; })
1153 (with { tree cst2 = const_binop (bitop, type, @1, @2); }
1155 (bitop @0 { cst2; }))))))))
1157 /* Try simple folding for X op !X, and X op X with the help
1158 of the truth_valued_p and logical_inverted_value predicates. */
1159 (match truth_valued_p
1161 (if (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1)))
1162 (for op (tcc_comparison truth_and truth_andif truth_or truth_orif truth_xor)
1163 (match truth_valued_p
1165 (match truth_valued_p
1168 (match (logical_inverted_value @0)
1170 (match (logical_inverted_value @0)
1171 (bit_not truth_valued_p@0))
1172 (match (logical_inverted_value @0)
1173 (eq @0 integer_zerop))
1174 (match (logical_inverted_value @0)
1175 (ne truth_valued_p@0 integer_truep))
1176 (match (logical_inverted_value @0)
1177 (bit_xor truth_valued_p@0 integer_truep))
1181 (bit_and:c @0 (logical_inverted_value @0))
1182 { build_zero_cst (type); })
1183 /* X | !X and X ^ !X -> 1, , if X is truth-valued. */
1184 (for op (bit_ior bit_xor)
1186 (op:c truth_valued_p@0 (logical_inverted_value @0))
1187 { constant_boolean_node (true, type); }))
1188 /* X ==/!= !X is false/true. */
1191 (op:c truth_valued_p@0 (logical_inverted_value @0))
1192 { constant_boolean_node (op == NE_EXPR ? true : false, type); }))
1196 (bit_not (bit_not @0))
1199 /* Convert ~ (-A) to A - 1. */
1201 (bit_not (convert? (negate @0)))
1202 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1203 || !TYPE_UNSIGNED (TREE_TYPE (@0)))
1204 (convert (minus @0 { build_each_one_cst (TREE_TYPE (@0)); }))))
1206 /* Convert - (~A) to A + 1. */
1208 (negate (nop_convert (bit_not @0)))
1209 (plus (view_convert @0) { build_each_one_cst (type); }))
1211 /* Convert ~ (A - 1) or ~ (A + -1) to -A. */
1213 (bit_not (convert? (minus @0 integer_each_onep)))
1214 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1215 || !TYPE_UNSIGNED (TREE_TYPE (@0)))
1216 (convert (negate @0))))
1218 (bit_not (convert? (plus @0 integer_all_onesp)))
1219 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1220 || !TYPE_UNSIGNED (TREE_TYPE (@0)))
1221 (convert (negate @0))))
1223 /* Part of convert ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify. */
1225 (bit_not (convert? (bit_xor @0 INTEGER_CST@1)))
1226 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1227 (convert (bit_xor @0 (bit_not @1)))))
1229 (bit_not (convert? (bit_xor:c (bit_not @0) @1)))
1230 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1231 (convert (bit_xor @0 @1))))
1233 /* Otherwise prefer ~(X ^ Y) to ~X ^ Y as more canonical. */
1235 (bit_xor:c (nop_convert:s (bit_not:s @0)) @1)
1236 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1237 (bit_not (bit_xor (view_convert @0) @1))))
1239 /* (x & ~m) | (y & m) -> ((x ^ y) & m) ^ x */
1241 (bit_ior:c (bit_and:cs @0 (bit_not @2)) (bit_and:cs @1 @2))
1242 (bit_xor (bit_and (bit_xor @0 @1) @2) @0))
1244 /* Fold A - (A & B) into ~B & A. */
1246 (minus (convert1? @0) (convert2?:s (bit_and:cs @@0 @1)))
1247 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
1248 && tree_nop_conversion_p (type, TREE_TYPE (@1)))
1249 (convert (bit_and (bit_not @1) @0))))
1251 /* (m1 CMP m2) * d -> (m1 CMP m2) ? d : 0 */
1252 (for cmp (gt lt ge le)
1254 (mult (convert (cmp @0 @1)) @2)
1255 (cond (cmp @0 @1) @2 { build_zero_cst (type); })))
1257 /* For integral types with undefined overflow and C != 0 fold
1258 x * C EQ/NE y * C into x EQ/NE y. */
1261 (cmp (mult:c @0 @1) (mult:c @2 @1))
1262 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
1263 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1264 && tree_expr_nonzero_p (@1))
1267 /* For integral types with wrapping overflow and C odd fold
1268 x * C EQ/NE y * C into x EQ/NE y. */
1271 (cmp (mult @0 INTEGER_CST@1) (mult @2 @1))
1272 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
1273 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))
1274 && (TREE_INT_CST_LOW (@1) & 1) != 0)
1277 /* For integral types with undefined overflow and C != 0 fold
1278 x * C RELOP y * C into:
1280 x RELOP y for nonnegative C
1281 y RELOP x for negative C */
1282 (for cmp (lt gt le ge)
1284 (cmp (mult:c @0 @1) (mult:c @2 @1))
1285 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
1286 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1287 (if (tree_expr_nonnegative_p (@1) && tree_expr_nonzero_p (@1))
1289 (if (TREE_CODE (@1) == INTEGER_CST
1290 && wi::neg_p (wi::to_wide (@1), TYPE_SIGN (TREE_TYPE (@1))))
1293 /* (X - 1U) <= INT_MAX-1U into (int) X > 0. */
1297 (cmp (plus @0 integer_minus_onep@1) INTEGER_CST@2)
1298 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1299 && TYPE_UNSIGNED (TREE_TYPE (@0))
1300 && TYPE_PRECISION (TREE_TYPE (@0)) > 1
1301 && (wi::to_wide (@2)
1302 == wi::max_value (TYPE_PRECISION (TREE_TYPE (@0)), SIGNED) - 1))
1303 (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
1304 (icmp (convert:stype @0) { build_int_cst (stype, 0); })))))
1306 /* X / 4 < Y / 4 iff X < Y when the division is known to be exact. */
1307 (for cmp (simple_comparison)
1309 (cmp (exact_div @0 INTEGER_CST@2) (exact_div @1 @2))
1310 (if (wi::gt_p (wi::to_wide (@2), 0, TYPE_SIGN (TREE_TYPE (@2))))
1313 /* X / C1 op C2 into a simple range test. */
1314 (for cmp (simple_comparison)
1316 (cmp (trunc_div:s @0 INTEGER_CST@1) INTEGER_CST@2)
1317 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1318 && integer_nonzerop (@1)
1319 && !TREE_OVERFLOW (@1)
1320 && !TREE_OVERFLOW (@2))
1321 (with { tree lo, hi; bool neg_overflow;
1322 enum tree_code code = fold_div_compare (cmp, @1, @2, &lo, &hi,
1325 (if (code == LT_EXPR || code == GE_EXPR)
1326 (if (TREE_OVERFLOW (lo))
1327 { build_int_cst (type, (code == LT_EXPR) ^ neg_overflow); }
1328 (if (code == LT_EXPR)
1331 (if (code == LE_EXPR || code == GT_EXPR)
1332 (if (TREE_OVERFLOW (hi))
1333 { build_int_cst (type, (code == LE_EXPR) ^ neg_overflow); }
1334 (if (code == LE_EXPR)
1338 { build_int_cst (type, code == NE_EXPR); })
1339 (if (code == EQ_EXPR && !hi)
1341 (if (code == EQ_EXPR && !lo)
1343 (if (code == NE_EXPR && !hi)
1345 (if (code == NE_EXPR && !lo)
1348 { build_range_check (UNKNOWN_LOCATION, type, @0, code == EQ_EXPR,
1352 tree etype = range_check_type (TREE_TYPE (@0));
1355 if (! TYPE_UNSIGNED (etype))
1356 etype = unsigned_type_for (etype);
1357 hi = fold_convert (etype, hi);
1358 lo = fold_convert (etype, lo);
1359 hi = const_binop (MINUS_EXPR, etype, hi, lo);
1362 (if (etype && hi && !TREE_OVERFLOW (hi))
1363 (if (code == EQ_EXPR)
1364 (le (minus (convert:etype @0) { lo; }) { hi; })
1365 (gt (minus (convert:etype @0) { lo; }) { hi; })))))))))
1367 /* X + Z < Y + Z is the same as X < Y when there is no overflow. */
1368 (for op (lt le ge gt)
1370 (op (plus:c @0 @2) (plus:c @1 @2))
1371 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1372 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1374 /* For equality and subtraction, this is also true with wrapping overflow. */
1375 (for op (eq ne minus)
1377 (op (plus:c @0 @2) (plus:c @1 @2))
1378 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1379 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1380 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
1383 /* X - Z < Y - Z is the same as X < Y when there is no overflow. */
1384 (for op (lt le ge gt)
1386 (op (minus @0 @2) (minus @1 @2))
1387 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1388 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1390 /* For equality and subtraction, this is also true with wrapping overflow. */
1391 (for op (eq ne minus)
1393 (op (minus @0 @2) (minus @1 @2))
1394 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1395 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1396 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
1398 /* And for pointers... */
1399 (for op (simple_comparison)
1401 (op (pointer_diff@3 @0 @2) (pointer_diff @1 @2))
1402 (if (!TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
1405 (minus (pointer_diff@3 @0 @2) (pointer_diff @1 @2))
1406 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@3))
1407 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
1408 (pointer_diff @0 @1)))
1410 /* Z - X < Z - Y is the same as Y < X when there is no overflow. */
1411 (for op (lt le ge gt)
1413 (op (minus @2 @0) (minus @2 @1))
1414 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1415 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1417 /* For equality and subtraction, this is also true with wrapping overflow. */
1418 (for op (eq ne minus)
1420 (op (minus @2 @0) (minus @2 @1))
1421 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1422 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1423 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
1425 /* And for pointers... */
1426 (for op (simple_comparison)
1428 (op (pointer_diff@3 @2 @0) (pointer_diff @2 @1))
1429 (if (!TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
1432 (minus (pointer_diff@3 @2 @0) (pointer_diff @2 @1))
1433 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@3))
1434 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
1435 (pointer_diff @1 @0)))
1437 /* X + Y < Y is the same as X < 0 when there is no overflow. */
1438 (for op (lt le gt ge)
1440 (op:c (plus:c@2 @0 @1) @1)
1441 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1442 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1443 && (CONSTANT_CLASS_P (@0) || single_use (@2)))
1444 (op @0 { build_zero_cst (TREE_TYPE (@0)); }))))
1445 /* For equality, this is also true with wrapping overflow. */
1448 (op:c (nop_convert@3 (plus:c@2 @0 (convert1? @1))) (convert2? @1))
1449 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1450 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1451 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
1452 && (CONSTANT_CLASS_P (@0) || (single_use (@2) && single_use (@3)))
1453 && tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@2))
1454 && tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@1)))
1455 (op @0 { build_zero_cst (TREE_TYPE (@0)); })))
1457 (op:c (nop_convert@3 (pointer_plus@2 (convert1? @0) @1)) (convert2? @0))
1458 (if (tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@0))
1459 && tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0))
1460 && (CONSTANT_CLASS_P (@1) || (single_use (@2) && single_use (@3))))
1461 (op @1 { build_zero_cst (TREE_TYPE (@1)); }))))
1463 /* X - Y < X is the same as Y > 0 when there is no overflow.
1464 For equality, this is also true with wrapping overflow. */
1465 (for op (simple_comparison)
1467 (op:c @0 (minus@2 @0 @1))
1468 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1469 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1470 || ((op == EQ_EXPR || op == NE_EXPR)
1471 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
1472 && (CONSTANT_CLASS_P (@1) || single_use (@2)))
1473 (op @1 { build_zero_cst (TREE_TYPE (@1)); }))))
1476 (X / Y) == 0 -> X < Y if X, Y are unsigned.
1477 (X / Y) != 0 -> X >= Y, if X, Y are unsigned. */
1481 (cmp (trunc_div @0 @1) integer_zerop)
1482 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
1483 /* Complex ==/!= is allowed, but not </>=. */
1484 && TREE_CODE (TREE_TYPE (@0)) != COMPLEX_TYPE
1485 && (VECTOR_TYPE_P (type) || !VECTOR_TYPE_P (TREE_TYPE (@0))))
1488 /* X == C - X can never be true if C is odd. */
1491 (cmp:c (convert? @0) (convert1? (minus INTEGER_CST@1 (convert2? @0))))
1492 (if (TREE_INT_CST_LOW (@1) & 1)
1493 { constant_boolean_node (cmp == NE_EXPR, type); })))
1495 /* Arguments on which one can call get_nonzero_bits to get the bits
1497 (match with_possible_nonzero_bits
1499 (match with_possible_nonzero_bits
1501 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))))
1502 /* Slightly extended version, do not make it recursive to keep it cheap. */
1503 (match (with_possible_nonzero_bits2 @0)
1504 with_possible_nonzero_bits@0)
1505 (match (with_possible_nonzero_bits2 @0)
1506 (bit_and:c with_possible_nonzero_bits@0 @2))
1508 /* Same for bits that are known to be set, but we do not have
1509 an equivalent to get_nonzero_bits yet. */
1510 (match (with_certain_nonzero_bits2 @0)
1512 (match (with_certain_nonzero_bits2 @0)
1513 (bit_ior @1 INTEGER_CST@0))
1515 /* X == C (or X & Z == Y | C) is impossible if ~nonzero(X) & C != 0. */
1518 (cmp:c (with_possible_nonzero_bits2 @0) (with_certain_nonzero_bits2 @1))
1519 (if (wi::bit_and_not (wi::to_wide (@1), get_nonzero_bits (@0)) != 0)
1520 { constant_boolean_node (cmp == NE_EXPR, type); })))
1522 /* ((X inner_op C0) outer_op C1)
1523 With X being a tree where value_range has reasoned certain bits to always be
1524 zero throughout its computed value range,
1525 inner_op = {|,^}, outer_op = {|,^} and inner_op != outer_op
1526 where zero_mask has 1's for all bits that are sure to be 0 in
1528 if (inner_op == '^') C0 &= ~C1;
1529 if ((C0 & ~zero_mask) == 0) then emit (X outer_op (C0 outer_op C1)
1530 if ((C1 & ~zero_mask) == 0) then emit (X inner_op (C0 outer_op C1)
1532 (for inner_op (bit_ior bit_xor)
1533 outer_op (bit_xor bit_ior)
1536 (inner_op:s @2 INTEGER_CST@0) INTEGER_CST@1)
1540 wide_int zero_mask_not;
1544 if (TREE_CODE (@2) == SSA_NAME)
1545 zero_mask_not = get_nonzero_bits (@2);
1549 if (inner_op == BIT_XOR_EXPR)
1551 C0 = wi::bit_and_not (wi::to_wide (@0), wi::to_wide (@1));
1552 cst_emit = C0 | wi::to_wide (@1);
1556 C0 = wi::to_wide (@0);
1557 cst_emit = C0 ^ wi::to_wide (@1);
1560 (if (!fail && (C0 & zero_mask_not) == 0)
1561 (outer_op @2 { wide_int_to_tree (type, cst_emit); })
1562 (if (!fail && (wi::to_wide (@1) & zero_mask_not) == 0)
1563 (inner_op @2 { wide_int_to_tree (type, cst_emit); }))))))
1565 /* Associate (p +p off1) +p off2 as (p +p (off1 + off2)). */
1567 (pointer_plus (pointer_plus:s @0 @1) @3)
1568 (pointer_plus @0 (plus @1 @3)))
1574 tem4 = (unsigned long) tem3;
1579 (pointer_plus @0 (convert?@2 (minus@3 (convert @1) (convert @0))))
1580 /* Conditionally look through a sign-changing conversion. */
1581 (if (TYPE_PRECISION (TREE_TYPE (@2)) == TYPE_PRECISION (TREE_TYPE (@3))
1582 && ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@1)))
1583 || (GENERIC && type == TREE_TYPE (@1))))
1586 (pointer_plus @0 (convert?@2 (pointer_diff@3 @1 @@0)))
1587 (if (TYPE_PRECISION (TREE_TYPE (@2)) >= TYPE_PRECISION (TREE_TYPE (@3)))
1591 tem = (sizetype) ptr;
1595 and produce the simpler and easier to analyze with respect to alignment
1596 ... = ptr & ~algn; */
1598 (pointer_plus @0 (negate (bit_and (convert @0) INTEGER_CST@1)))
1599 (with { tree algn = wide_int_to_tree (TREE_TYPE (@0), ~wi::to_wide (@1)); }
1600 (bit_and @0 { algn; })))
1602 /* Try folding difference of addresses. */
1604 (minus (convert ADDR_EXPR@0) (convert @1))
1605 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1606 (with { poly_int64 diff; }
1607 (if (ptr_difference_const (@0, @1, &diff))
1608 { build_int_cst_type (type, diff); }))))
1610 (minus (convert @0) (convert ADDR_EXPR@1))
1611 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1612 (with { poly_int64 diff; }
1613 (if (ptr_difference_const (@0, @1, &diff))
1614 { build_int_cst_type (type, diff); }))))
1616 (pointer_diff (convert?@2 ADDR_EXPR@0) (convert?@3 @1))
1617 (if (tree_nop_conversion_p (TREE_TYPE(@2), TREE_TYPE (@0))
1618 && tree_nop_conversion_p (TREE_TYPE(@3), TREE_TYPE (@1)))
1619 (with { poly_int64 diff; }
1620 (if (ptr_difference_const (@0, @1, &diff))
1621 { build_int_cst_type (type, diff); }))))
1623 (pointer_diff (convert?@2 @0) (convert?@3 ADDR_EXPR@1))
1624 (if (tree_nop_conversion_p (TREE_TYPE(@2), TREE_TYPE (@0))
1625 && tree_nop_conversion_p (TREE_TYPE(@3), TREE_TYPE (@1)))
1626 (with { poly_int64 diff; }
1627 (if (ptr_difference_const (@0, @1, &diff))
1628 { build_int_cst_type (type, diff); }))))
1630 /* If arg0 is derived from the address of an object or function, we may
1631 be able to fold this expression using the object or function's
1634 (bit_and (convert? @0) INTEGER_CST@1)
1635 (if (POINTER_TYPE_P (TREE_TYPE (@0))
1636 && tree_nop_conversion_p (type, TREE_TYPE (@0)))
1640 unsigned HOST_WIDE_INT bitpos;
1641 get_pointer_alignment_1 (@0, &align, &bitpos);
1643 (if (wi::ltu_p (wi::to_wide (@1), align / BITS_PER_UNIT))
1644 { wide_int_to_tree (type, (wi::to_wide (@1)
1645 & (bitpos / BITS_PER_UNIT))); }))))
1648 /* We can't reassociate at all for saturating types. */
1649 (if (!TYPE_SATURATING (type))
1651 /* Contract negates. */
1652 /* A + (-B) -> A - B */
1654 (plus:c @0 (convert? (negate @1)))
1655 /* Apply STRIP_NOPS on the negate. */
1656 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1657 && !TYPE_OVERFLOW_SANITIZED (type))
1661 if (INTEGRAL_TYPE_P (type)
1662 && TYPE_OVERFLOW_WRAPS (type) != TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
1663 t1 = TYPE_OVERFLOW_WRAPS (type) ? type : TREE_TYPE (@1);
1665 (convert (minus (convert:t1 @0) (convert:t1 @1))))))
1666 /* A - (-B) -> A + B */
1668 (minus @0 (convert? (negate @1)))
1669 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1670 && !TYPE_OVERFLOW_SANITIZED (type))
1674 if (INTEGRAL_TYPE_P (type)
1675 && TYPE_OVERFLOW_WRAPS (type) != TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
1676 t1 = TYPE_OVERFLOW_WRAPS (type) ? type : TREE_TYPE (@1);
1678 (convert (plus (convert:t1 @0) (convert:t1 @1))))))
1680 Sign-extension is ok except for INT_MIN, which thankfully cannot
1681 happen without overflow. */
1683 (negate (convert (negate @1)))
1684 (if (INTEGRAL_TYPE_P (type)
1685 && (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@1))
1686 || (!TYPE_UNSIGNED (TREE_TYPE (@1))
1687 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))))
1688 && !TYPE_OVERFLOW_SANITIZED (type)
1689 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@1)))
1692 (negate (convert negate_expr_p@1))
1693 (if (SCALAR_FLOAT_TYPE_P (type)
1694 && ((DECIMAL_FLOAT_TYPE_P (type)
1695 == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1))
1696 && TYPE_PRECISION (type) >= TYPE_PRECISION (TREE_TYPE (@1)))
1697 || !HONOR_SIGN_DEPENDENT_ROUNDING (type)))
1698 (convert (negate @1))))
1700 (negate (nop_convert (negate @1)))
1701 (if (!TYPE_OVERFLOW_SANITIZED (type)
1702 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@1)))
1705 /* We can't reassociate floating-point unless -fassociative-math
1706 or fixed-point plus or minus because of saturation to +-Inf. */
1707 (if ((!FLOAT_TYPE_P (type) || flag_associative_math)
1708 && !FIXED_POINT_TYPE_P (type))
1710 /* Match patterns that allow contracting a plus-minus pair
1711 irrespective of overflow issues. */
1712 /* (A +- B) - A -> +- B */
1713 /* (A +- B) -+ B -> A */
1714 /* A - (A +- B) -> -+ B */
1715 /* A +- (B -+ A) -> +- B */
1717 (minus (plus:c @0 @1) @0)
1720 (minus (minus @0 @1) @0)
1723 (plus:c (minus @0 @1) @1)
1726 (minus @0 (plus:c @0 @1))
1729 (minus @0 (minus @0 @1))
1731 /* (A +- B) + (C - A) -> C +- B */
1732 /* (A + B) - (A - C) -> B + C */
1733 /* More cases are handled with comparisons. */
1735 (plus:c (plus:c @0 @1) (minus @2 @0))
1738 (plus:c (minus @0 @1) (minus @2 @0))
1741 (plus:c (pointer_diff @0 @1) (pointer_diff @2 @0))
1742 (if (TYPE_OVERFLOW_UNDEFINED (type)
1743 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0)))
1744 (pointer_diff @2 @1)))
1746 (minus (plus:c @0 @1) (minus @0 @2))
1749 /* (A +- CST1) +- CST2 -> A + CST3
1750 Use view_convert because it is safe for vectors and equivalent for
1752 (for outer_op (plus minus)
1753 (for inner_op (plus minus)
1754 neg_inner_op (minus plus)
1756 (outer_op (nop_convert (inner_op @0 CONSTANT_CLASS_P@1))
1758 /* If one of the types wraps, use that one. */
1759 (if (!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_WRAPS (type))
1760 /* If all 3 captures are CONSTANT_CLASS_P, punt, as we might recurse
1761 forever if something doesn't simplify into a constant. */
1762 (if (!CONSTANT_CLASS_P (@0))
1763 (if (outer_op == PLUS_EXPR)
1764 (plus (view_convert @0) (inner_op @2 (view_convert @1)))
1765 (minus (view_convert @0) (neg_inner_op @2 (view_convert @1)))))
1766 (if (!ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1767 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
1768 (if (outer_op == PLUS_EXPR)
1769 (view_convert (plus @0 (inner_op (view_convert @2) @1)))
1770 (view_convert (minus @0 (neg_inner_op (view_convert @2) @1))))
1771 /* If the constant operation overflows we cannot do the transform
1772 directly as we would introduce undefined overflow, for example
1773 with (a - 1) + INT_MIN. */
1774 (if (types_match (type, @0))
1775 (with { tree cst = const_binop (outer_op == inner_op
1776 ? PLUS_EXPR : MINUS_EXPR,
1778 (if (cst && !TREE_OVERFLOW (cst))
1779 (inner_op @0 { cst; } )
1780 /* X+INT_MAX+1 is X-INT_MIN. */
1781 (if (INTEGRAL_TYPE_P (type) && cst
1782 && wi::to_wide (cst) == wi::min_value (type))
1783 (neg_inner_op @0 { wide_int_to_tree (type, wi::to_wide (cst)); })
1784 /* Last resort, use some unsigned type. */
1785 (with { tree utype = unsigned_type_for (type); }
1787 (view_convert (inner_op
1788 (view_convert:utype @0)
1790 { drop_tree_overflow (cst); }))))))))))))))
1792 /* (CST1 - A) +- CST2 -> CST3 - A */
1793 (for outer_op (plus minus)
1795 (outer_op (minus CONSTANT_CLASS_P@1 @0) CONSTANT_CLASS_P@2)
1796 (with { tree cst = const_binop (outer_op, type, @1, @2); }
1797 (if (cst && !TREE_OVERFLOW (cst))
1798 (minus { cst; } @0)))))
1800 /* CST1 - (CST2 - A) -> CST3 + A */
1802 (minus CONSTANT_CLASS_P@1 (minus CONSTANT_CLASS_P@2 @0))
1803 (with { tree cst = const_binop (MINUS_EXPR, type, @1, @2); }
1804 (if (cst && !TREE_OVERFLOW (cst))
1805 (plus { cst; } @0))))
1809 (plus:c (bit_not @0) @0)
1810 (if (!TYPE_OVERFLOW_TRAPS (type))
1811 { build_all_ones_cst (type); }))
1815 (plus (convert? (bit_not @0)) integer_each_onep)
1816 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1817 (negate (convert @0))))
1821 (minus (convert? (negate @0)) integer_each_onep)
1822 (if (!TYPE_OVERFLOW_TRAPS (type)
1823 && tree_nop_conversion_p (type, TREE_TYPE (@0)))
1824 (bit_not (convert @0))))
1828 (minus integer_all_onesp @0)
1831 /* (T)(P + A) - (T)P -> (T) A */
1833 (minus (convert (plus:c @@0 @1))
1835 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
1836 /* For integer types, if A has a smaller type
1837 than T the result depends on the possible
1839 E.g. T=size_t, A=(unsigned)429497295, P>0.
1840 However, if an overflow in P + A would cause
1841 undefined behavior, we can assume that there
1843 || (INTEGRAL_TYPE_P (TREE_TYPE (@1))
1844 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))))
1847 (minus (convert (pointer_plus @@0 @1))
1849 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
1850 /* For pointer types, if the conversion of A to the
1851 final type requires a sign- or zero-extension,
1852 then we have to punt - it is not defined which
1854 || (POINTER_TYPE_P (TREE_TYPE (@0))
1855 && TREE_CODE (@1) == INTEGER_CST
1856 && tree_int_cst_sign_bit (@1) == 0))
1859 (pointer_diff (pointer_plus @@0 @1) @0)
1860 /* The second argument of pointer_plus must be interpreted as signed, and
1861 thus sign-extended if necessary. */
1862 (with { tree stype = signed_type_for (TREE_TYPE (@1)); }
1863 /* Use view_convert instead of convert here, as POINTER_PLUS_EXPR
1864 second arg is unsigned even when we need to consider it as signed,
1865 we don't want to diagnose overflow here. */
1866 (convert (view_convert:stype @1))))
1868 /* (T)P - (T)(P + A) -> -(T) A */
1870 (minus (convert? @0)
1871 (convert (plus:c @@0 @1)))
1872 (if (INTEGRAL_TYPE_P (type)
1873 && TYPE_OVERFLOW_UNDEFINED (type)
1874 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
1875 (with { tree utype = unsigned_type_for (type); }
1876 (convert (negate (convert:utype @1))))
1877 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
1878 /* For integer types, if A has a smaller type
1879 than T the result depends on the possible
1881 E.g. T=size_t, A=(unsigned)429497295, P>0.
1882 However, if an overflow in P + A would cause
1883 undefined behavior, we can assume that there
1885 || (INTEGRAL_TYPE_P (TREE_TYPE (@1))
1886 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))))
1887 (negate (convert @1)))))
1890 (convert (pointer_plus @@0 @1)))
1891 (if (INTEGRAL_TYPE_P (type)
1892 && TYPE_OVERFLOW_UNDEFINED (type)
1893 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
1894 (with { tree utype = unsigned_type_for (type); }
1895 (convert (negate (convert:utype @1))))
1896 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
1897 /* For pointer types, if the conversion of A to the
1898 final type requires a sign- or zero-extension,
1899 then we have to punt - it is not defined which
1901 || (POINTER_TYPE_P (TREE_TYPE (@0))
1902 && TREE_CODE (@1) == INTEGER_CST
1903 && tree_int_cst_sign_bit (@1) == 0))
1904 (negate (convert @1)))))
1906 (pointer_diff @0 (pointer_plus @@0 @1))
1907 /* The second argument of pointer_plus must be interpreted as signed, and
1908 thus sign-extended if necessary. */
1909 (with { tree stype = signed_type_for (TREE_TYPE (@1)); }
1910 /* Use view_convert instead of convert here, as POINTER_PLUS_EXPR
1911 second arg is unsigned even when we need to consider it as signed,
1912 we don't want to diagnose overflow here. */
1913 (negate (convert (view_convert:stype @1)))))
1915 /* (T)(P + A) - (T)(P + B) -> (T)A - (T)B */
1917 (minus (convert (plus:c @@0 @1))
1918 (convert (plus:c @0 @2)))
1919 (if (INTEGRAL_TYPE_P (type)
1920 && TYPE_OVERFLOW_UNDEFINED (type)
1921 && element_precision (type) <= element_precision (TREE_TYPE (@1))
1922 && element_precision (type) <= element_precision (TREE_TYPE (@2)))
1923 (with { tree utype = unsigned_type_for (type); }
1924 (convert (minus (convert:utype @1) (convert:utype @2))))
1925 (if (((element_precision (type) <= element_precision (TREE_TYPE (@1)))
1926 == (element_precision (type) <= element_precision (TREE_TYPE (@2))))
1927 && (element_precision (type) <= element_precision (TREE_TYPE (@1))
1928 /* For integer types, if A has a smaller type
1929 than T the result depends on the possible
1931 E.g. T=size_t, A=(unsigned)429497295, P>0.
1932 However, if an overflow in P + A would cause
1933 undefined behavior, we can assume that there
1935 || (INTEGRAL_TYPE_P (TREE_TYPE (@1))
1936 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
1937 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))
1938 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@2)))))
1939 (minus (convert @1) (convert @2)))))
1941 (minus (convert (pointer_plus @@0 @1))
1942 (convert (pointer_plus @0 @2)))
1943 (if (INTEGRAL_TYPE_P (type)
1944 && TYPE_OVERFLOW_UNDEFINED (type)
1945 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
1946 (with { tree utype = unsigned_type_for (type); }
1947 (convert (minus (convert:utype @1) (convert:utype @2))))
1948 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
1949 /* For pointer types, if the conversion of A to the
1950 final type requires a sign- or zero-extension,
1951 then we have to punt - it is not defined which
1953 || (POINTER_TYPE_P (TREE_TYPE (@0))
1954 && TREE_CODE (@1) == INTEGER_CST
1955 && tree_int_cst_sign_bit (@1) == 0
1956 && TREE_CODE (@2) == INTEGER_CST
1957 && tree_int_cst_sign_bit (@2) == 0))
1958 (minus (convert @1) (convert @2)))))
1960 (pointer_diff (pointer_plus @@0 @1) (pointer_plus @0 @2))
1961 /* The second argument of pointer_plus must be interpreted as signed, and
1962 thus sign-extended if necessary. */
1963 (with { tree stype = signed_type_for (TREE_TYPE (@1)); }
1964 /* Use view_convert instead of convert here, as POINTER_PLUS_EXPR
1965 second arg is unsigned even when we need to consider it as signed,
1966 we don't want to diagnose overflow here. */
1967 (minus (convert (view_convert:stype @1))
1968 (convert (view_convert:stype @2)))))))
1970 /* (A * C) +- (B * C) -> (A+-B) * C and (A * C) +- A -> A * (C+-1).
1971 Modeled after fold_plusminus_mult_expr. */
1972 (if (!TYPE_SATURATING (type)
1973 && (!FLOAT_TYPE_P (type) || flag_associative_math))
1974 (for plusminus (plus minus)
1976 (plusminus (mult:cs@3 @0 @1) (mult:cs@4 @0 @2))
1977 (if ((!ANY_INTEGRAL_TYPE_P (type)
1978 || TYPE_OVERFLOW_WRAPS (type)
1979 || (INTEGRAL_TYPE_P (type)
1980 && tree_expr_nonzero_p (@0)
1981 && expr_not_equal_to (@0, wi::minus_one (TYPE_PRECISION (type)))))
1982 /* If @1 +- @2 is constant require a hard single-use on either
1983 original operand (but not on both). */
1984 && (single_use (@3) || single_use (@4)))
1985 (mult (plusminus @1 @2) @0)))
1986 /* We cannot generate constant 1 for fract. */
1987 (if (!ALL_FRACT_MODE_P (TYPE_MODE (type)))
1989 (plusminus @0 (mult:c@3 @0 @2))
1990 (if ((!ANY_INTEGRAL_TYPE_P (type)
1991 || TYPE_OVERFLOW_WRAPS (type)
1992 || (INTEGRAL_TYPE_P (type)
1993 && tree_expr_nonzero_p (@0)
1994 && expr_not_equal_to (@0, wi::minus_one (TYPE_PRECISION (type)))))
1996 (mult (plusminus { build_one_cst (type); } @2) @0)))
1998 (plusminus (mult:c@3 @0 @2) @0)
1999 (if ((!ANY_INTEGRAL_TYPE_P (type)
2000 || TYPE_OVERFLOW_WRAPS (type)
2001 || (INTEGRAL_TYPE_P (type)
2002 && tree_expr_nonzero_p (@0)
2003 && expr_not_equal_to (@0, wi::minus_one (TYPE_PRECISION (type)))))
2005 (mult (plusminus @2 { build_one_cst (type); }) @0))))))
2007 /* Simplifications of MIN_EXPR, MAX_EXPR, fmin() and fmax(). */
2009 (for minmax (min max FMIN_ALL FMAX_ALL)
2013 /* min(max(x,y),y) -> y. */
2015 (min:c (max:c @0 @1) @1)
2017 /* max(min(x,y),y) -> y. */
2019 (max:c (min:c @0 @1) @1)
2021 /* max(a,-a) -> abs(a). */
2023 (max:c @0 (negate @0))
2024 (if (TREE_CODE (type) != COMPLEX_TYPE
2025 && (! ANY_INTEGRAL_TYPE_P (type)
2026 || TYPE_OVERFLOW_UNDEFINED (type)))
2028 /* min(a,-a) -> -abs(a). */
2030 (min:c @0 (negate @0))
2031 (if (TREE_CODE (type) != COMPLEX_TYPE
2032 && (! ANY_INTEGRAL_TYPE_P (type)
2033 || TYPE_OVERFLOW_UNDEFINED (type)))
2038 (if (INTEGRAL_TYPE_P (type)
2039 && TYPE_MIN_VALUE (type)
2040 && operand_equal_p (@1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
2042 (if (INTEGRAL_TYPE_P (type)
2043 && TYPE_MAX_VALUE (type)
2044 && operand_equal_p (@1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
2049 (if (INTEGRAL_TYPE_P (type)
2050 && TYPE_MAX_VALUE (type)
2051 && operand_equal_p (@1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
2053 (if (INTEGRAL_TYPE_P (type)
2054 && TYPE_MIN_VALUE (type)
2055 && operand_equal_p (@1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
2058 /* max (a, a + CST) -> a + CST where CST is positive. */
2059 /* max (a, a + CST) -> a where CST is negative. */
2061 (max:c @0 (plus@2 @0 INTEGER_CST@1))
2062 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
2063 (if (tree_int_cst_sgn (@1) > 0)
2067 /* min (a, a + CST) -> a where CST is positive. */
2068 /* min (a, a + CST) -> a + CST where CST is negative. */
2070 (min:c @0 (plus@2 @0 INTEGER_CST@1))
2071 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
2072 (if (tree_int_cst_sgn (@1) > 0)
2076 /* (convert (minmax ((convert (x) c)))) -> minmax (x c) if x is promoted
2077 and the outer convert demotes the expression back to x's type. */
2078 (for minmax (min max)
2080 (convert (minmax@0 (convert @1) INTEGER_CST@2))
2081 (if (INTEGRAL_TYPE_P (type)
2082 && types_match (@1, type) && int_fits_type_p (@2, type)
2083 && TYPE_SIGN (TREE_TYPE (@0)) == TYPE_SIGN (type)
2084 && TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type))
2085 (minmax @1 (convert @2)))))
2087 (for minmax (FMIN_ALL FMAX_ALL)
2088 /* If either argument is NaN, return the other one. Avoid the
2089 transformation if we get (and honor) a signalling NaN. */
2091 (minmax:c @0 REAL_CST@1)
2092 (if (real_isnan (TREE_REAL_CST_PTR (@1))
2093 && (!HONOR_SNANS (@1) || !TREE_REAL_CST (@1).signalling))
2095 /* Convert fmin/fmax to MIN_EXPR/MAX_EXPR. C99 requires these
2096 functions to return the numeric arg if the other one is NaN.
2097 MIN and MAX don't honor that, so only transform if -ffinite-math-only
2098 is set. C99 doesn't require -0.0 to be handled, so we don't have to
2099 worry about it either. */
2100 (if (flag_finite_math_only)
2107 /* min (-A, -B) -> -max (A, B) */
2108 (for minmax (min max FMIN_ALL FMAX_ALL)
2109 maxmin (max min FMAX_ALL FMIN_ALL)
2111 (minmax (negate:s@2 @0) (negate:s@3 @1))
2112 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
2113 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2114 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
2115 (negate (maxmin @0 @1)))))
2116 /* MIN (~X, ~Y) -> ~MAX (X, Y)
2117 MAX (~X, ~Y) -> ~MIN (X, Y) */
2118 (for minmax (min max)
2121 (minmax (bit_not:s@2 @0) (bit_not:s@3 @1))
2122 (bit_not (maxmin @0 @1))))
2124 /* MIN (X, Y) == X -> X <= Y */
2125 (for minmax (min min max max)
2129 (cmp:c (minmax:c @0 @1) @0)
2130 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0)))
2132 /* MIN (X, 5) == 0 -> X == 0
2133 MIN (X, 5) == 7 -> false */
2136 (cmp (min @0 INTEGER_CST@1) INTEGER_CST@2)
2137 (if (wi::lt_p (wi::to_wide (@1), wi::to_wide (@2),
2138 TYPE_SIGN (TREE_TYPE (@0))))
2139 { constant_boolean_node (cmp == NE_EXPR, type); }
2140 (if (wi::gt_p (wi::to_wide (@1), wi::to_wide (@2),
2141 TYPE_SIGN (TREE_TYPE (@0))))
2145 (cmp (max @0 INTEGER_CST@1) INTEGER_CST@2)
2146 (if (wi::gt_p (wi::to_wide (@1), wi::to_wide (@2),
2147 TYPE_SIGN (TREE_TYPE (@0))))
2148 { constant_boolean_node (cmp == NE_EXPR, type); }
2149 (if (wi::lt_p (wi::to_wide (@1), wi::to_wide (@2),
2150 TYPE_SIGN (TREE_TYPE (@0))))
2152 /* MIN (X, C1) < C2 -> X < C2 || C1 < C2 */
2153 (for minmax (min min max max min min max max )
2154 cmp (lt le gt ge gt ge lt le )
2155 comb (bit_ior bit_ior bit_ior bit_ior bit_and bit_and bit_and bit_and)
2157 (cmp (minmax @0 INTEGER_CST@1) INTEGER_CST@2)
2158 (comb (cmp @0 @2) (cmp @1 @2))))
2160 /* Simplifications of shift and rotates. */
2162 (for rotate (lrotate rrotate)
2164 (rotate integer_all_onesp@0 @1)
2167 /* Optimize -1 >> x for arithmetic right shifts. */
2169 (rshift integer_all_onesp@0 @1)
2170 (if (!TYPE_UNSIGNED (type)
2171 && tree_expr_nonnegative_p (@1))
2174 /* Optimize (x >> c) << c into x & (-1<<c). */
2176 (lshift (rshift @0 INTEGER_CST@1) @1)
2177 (if (wi::ltu_p (wi::to_wide (@1), element_precision (type)))
2178 (bit_and @0 (lshift { build_minus_one_cst (type); } @1))))
2180 /* Optimize (x << c) >> c into x & ((unsigned)-1 >> c) for unsigned
2183 (rshift (lshift @0 INTEGER_CST@1) @1)
2184 (if (TYPE_UNSIGNED (type)
2185 && (wi::ltu_p (wi::to_wide (@1), element_precision (type))))
2186 (bit_and @0 (rshift { build_minus_one_cst (type); } @1))))
2188 (for shiftrotate (lrotate rrotate lshift rshift)
2190 (shiftrotate @0 integer_zerop)
2193 (shiftrotate integer_zerop@0 @1)
2195 /* Prefer vector1 << scalar to vector1 << vector2
2196 if vector2 is uniform. */
2197 (for vec (VECTOR_CST CONSTRUCTOR)
2199 (shiftrotate @0 vec@1)
2200 (with { tree tem = uniform_vector_p (@1); }
2202 (shiftrotate @0 { tem; }))))))
2204 /* Simplify X << Y where Y's low width bits are 0 to X, as only valid
2205 Y is 0. Similarly for X >> Y. */
2207 (for shift (lshift rshift)
2209 (shift @0 SSA_NAME@1)
2210 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1)))
2212 int width = ceil_log2 (element_precision (TREE_TYPE (@0)));
2213 int prec = TYPE_PRECISION (TREE_TYPE (@1));
2215 (if ((get_nonzero_bits (@1) & wi::mask (width, false, prec)) == 0)
2219 /* Rewrite an LROTATE_EXPR by a constant into an
2220 RROTATE_EXPR by a new constant. */
2222 (lrotate @0 INTEGER_CST@1)
2223 (rrotate @0 { const_binop (MINUS_EXPR, TREE_TYPE (@1),
2224 build_int_cst (TREE_TYPE (@1),
2225 element_precision (type)), @1); }))
2227 /* Turn (a OP c1) OP c2 into a OP (c1+c2). */
2228 (for op (lrotate rrotate rshift lshift)
2230 (op (op @0 INTEGER_CST@1) INTEGER_CST@2)
2231 (with { unsigned int prec = element_precision (type); }
2232 (if (wi::ge_p (wi::to_wide (@1), 0, TYPE_SIGN (TREE_TYPE (@1)))
2233 && wi::lt_p (wi::to_wide (@1), prec, TYPE_SIGN (TREE_TYPE (@1)))
2234 && wi::ge_p (wi::to_wide (@2), 0, TYPE_SIGN (TREE_TYPE (@2)))
2235 && wi::lt_p (wi::to_wide (@2), prec, TYPE_SIGN (TREE_TYPE (@2))))
2236 (with { unsigned int low = (tree_to_uhwi (@1)
2237 + tree_to_uhwi (@2)); }
2238 /* Deal with a OP (c1 + c2) being undefined but (a OP c1) OP c2
2239 being well defined. */
2241 (if (op == LROTATE_EXPR || op == RROTATE_EXPR)
2242 (op @0 { build_int_cst (TREE_TYPE (@1), low % prec); })
2243 (if (TYPE_UNSIGNED (type) || op == LSHIFT_EXPR)
2244 { build_zero_cst (type); }
2245 (op @0 { build_int_cst (TREE_TYPE (@1), prec - 1); })))
2246 (op @0 { build_int_cst (TREE_TYPE (@1), low); })))))))
2249 /* ((1 << A) & 1) != 0 -> A == 0
2250 ((1 << A) & 1) == 0 -> A != 0 */
2254 (cmp (bit_and (lshift integer_onep @0) integer_onep) integer_zerop)
2255 (icmp @0 { build_zero_cst (TREE_TYPE (@0)); })))
2257 /* (CST1 << A) == CST2 -> A == ctz (CST2) - ctz (CST1)
2258 (CST1 << A) != CST2 -> A != ctz (CST2) - ctz (CST1)
2262 (cmp (lshift INTEGER_CST@0 @1) INTEGER_CST@2)
2263 (with { int cand = wi::ctz (wi::to_wide (@2)) - wi::ctz (wi::to_wide (@0)); }
2265 || (!integer_zerop (@2)
2266 && wi::lshift (wi::to_wide (@0), cand) != wi::to_wide (@2)))
2267 { constant_boolean_node (cmp == NE_EXPR, type); }
2268 (if (!integer_zerop (@2)
2269 && wi::lshift (wi::to_wide (@0), cand) == wi::to_wide (@2))
2270 (cmp @1 { build_int_cst (TREE_TYPE (@1), cand); }))))))
2272 /* Fold (X << C1) & C2 into (X << C1) & (C2 | ((1 << C1) - 1))
2273 (X >> C1) & C2 into (X >> C1) & (C2 | ~((type) -1 >> C1))
2274 if the new mask might be further optimized. */
2275 (for shift (lshift rshift)
2277 (bit_and (convert?:s@4 (shift:s@5 (convert1?@3 @0) INTEGER_CST@1))
2279 (if (tree_nop_conversion_p (TREE_TYPE (@4), TREE_TYPE (@5))
2280 && TYPE_PRECISION (type) <= HOST_BITS_PER_WIDE_INT
2281 && tree_fits_uhwi_p (@1)
2282 && tree_to_uhwi (@1) > 0
2283 && tree_to_uhwi (@1) < TYPE_PRECISION (type))
2286 unsigned int shiftc = tree_to_uhwi (@1);
2287 unsigned HOST_WIDE_INT mask = TREE_INT_CST_LOW (@2);
2288 unsigned HOST_WIDE_INT newmask, zerobits = 0;
2289 tree shift_type = TREE_TYPE (@3);
2292 if (shift == LSHIFT_EXPR)
2293 zerobits = ((HOST_WIDE_INT_1U << shiftc) - 1);
2294 else if (shift == RSHIFT_EXPR
2295 && type_has_mode_precision_p (shift_type))
2297 prec = TYPE_PRECISION (TREE_TYPE (@3));
2299 /* See if more bits can be proven as zero because of
2302 && TYPE_UNSIGNED (TREE_TYPE (@0)))
2304 tree inner_type = TREE_TYPE (@0);
2305 if (type_has_mode_precision_p (inner_type)
2306 && TYPE_PRECISION (inner_type) < prec)
2308 prec = TYPE_PRECISION (inner_type);
2309 /* See if we can shorten the right shift. */
2311 shift_type = inner_type;
2312 /* Otherwise X >> C1 is all zeros, so we'll optimize
2313 it into (X, 0) later on by making sure zerobits
2317 zerobits = HOST_WIDE_INT_M1U;
2320 zerobits >>= HOST_BITS_PER_WIDE_INT - shiftc;
2321 zerobits <<= prec - shiftc;
2323 /* For arithmetic shift if sign bit could be set, zerobits
2324 can contain actually sign bits, so no transformation is
2325 possible, unless MASK masks them all away. In that
2326 case the shift needs to be converted into logical shift. */
2327 if (!TYPE_UNSIGNED (TREE_TYPE (@3))
2328 && prec == TYPE_PRECISION (TREE_TYPE (@3)))
2330 if ((mask & zerobits) == 0)
2331 shift_type = unsigned_type_for (TREE_TYPE (@3));
2337 /* ((X << 16) & 0xff00) is (X, 0). */
2338 (if ((mask & zerobits) == mask)
2339 { build_int_cst (type, 0); }
2340 (with { newmask = mask | zerobits; }
2341 (if (newmask != mask && (newmask & (newmask + 1)) == 0)
2344 /* Only do the transformation if NEWMASK is some integer
2346 for (prec = BITS_PER_UNIT;
2347 prec < HOST_BITS_PER_WIDE_INT; prec <<= 1)
2348 if (newmask == (HOST_WIDE_INT_1U << prec) - 1)
2351 (if (prec < HOST_BITS_PER_WIDE_INT
2352 || newmask == HOST_WIDE_INT_M1U)
2354 { tree newmaskt = build_int_cst_type (TREE_TYPE (@2), newmask); }
2355 (if (!tree_int_cst_equal (newmaskt, @2))
2356 (if (shift_type != TREE_TYPE (@3))
2357 (bit_and (convert (shift:shift_type (convert @3) @1)) { newmaskt; })
2358 (bit_and @4 { newmaskt; })))))))))))))
2360 /* Fold (X {&,^,|} C2) << C1 into (X << C1) {&,^,|} (C2 << C1)
2361 (X {&,^,|} C2) >> C1 into (X >> C1) & (C2 >> C1). */
2362 (for shift (lshift rshift)
2363 (for bit_op (bit_and bit_xor bit_ior)
2365 (shift (convert?:s (bit_op:s @0 INTEGER_CST@2)) INTEGER_CST@1)
2366 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2367 (with { tree mask = int_const_binop (shift, fold_convert (type, @2), @1); }
2368 (bit_op (shift (convert @0) @1) { mask; }))))))
2370 /* ~(~X >> Y) -> X >> Y (for arithmetic shift). */
2372 (bit_not (convert1?:s (rshift:s (convert2?@0 (bit_not @1)) @2)))
2373 (if (!TYPE_UNSIGNED (TREE_TYPE (@0))
2374 && (element_precision (TREE_TYPE (@0))
2375 <= element_precision (TREE_TYPE (@1))
2376 || !TYPE_UNSIGNED (TREE_TYPE (@1))))
2378 { tree shift_type = TREE_TYPE (@0); }
2379 (convert (rshift (convert:shift_type @1) @2)))))
2381 /* ~(~X >>r Y) -> X >>r Y
2382 ~(~X <<r Y) -> X <<r Y */
2383 (for rotate (lrotate rrotate)
2385 (bit_not (convert1?:s (rotate:s (convert2?@0 (bit_not @1)) @2)))
2386 (if ((element_precision (TREE_TYPE (@0))
2387 <= element_precision (TREE_TYPE (@1))
2388 || !TYPE_UNSIGNED (TREE_TYPE (@1)))
2389 && (element_precision (type) <= element_precision (TREE_TYPE (@0))
2390 || !TYPE_UNSIGNED (TREE_TYPE (@0))))
2392 { tree rotate_type = TREE_TYPE (@0); }
2393 (convert (rotate (convert:rotate_type @1) @2))))))
2395 /* Simplifications of conversions. */
2397 /* Basic strip-useless-type-conversions / strip_nops. */
2398 (for cvt (convert view_convert float fix_trunc)
2401 (if ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@0)))
2402 || (GENERIC && type == TREE_TYPE (@0)))
2405 /* Contract view-conversions. */
2407 (view_convert (view_convert @0))
2410 /* For integral conversions with the same precision or pointer
2411 conversions use a NOP_EXPR instead. */
2414 (if ((INTEGRAL_TYPE_P (type) || POINTER_TYPE_P (type))
2415 && (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
2416 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (@0)))
2419 /* Strip inner integral conversions that do not change precision or size, or
2420 zero-extend while keeping the same size (for bool-to-char). */
2422 (view_convert (convert@0 @1))
2423 (if ((INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
2424 && (INTEGRAL_TYPE_P (TREE_TYPE (@1)) || POINTER_TYPE_P (TREE_TYPE (@1)))
2425 && TYPE_SIZE (TREE_TYPE (@0)) == TYPE_SIZE (TREE_TYPE (@1))
2426 && (TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1))
2427 || (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (TREE_TYPE (@1))
2428 && TYPE_UNSIGNED (TREE_TYPE (@1)))))
2431 /* Re-association barriers around constants and other re-association
2432 barriers can be removed. */
2434 (paren CONSTANT_CLASS_P@0)
2437 (paren (paren@1 @0))
2440 /* Handle cases of two conversions in a row. */
2441 (for ocvt (convert float fix_trunc)
2442 (for icvt (convert float)
2447 tree inside_type = TREE_TYPE (@0);
2448 tree inter_type = TREE_TYPE (@1);
2449 int inside_int = INTEGRAL_TYPE_P (inside_type);
2450 int inside_ptr = POINTER_TYPE_P (inside_type);
2451 int inside_float = FLOAT_TYPE_P (inside_type);
2452 int inside_vec = VECTOR_TYPE_P (inside_type);
2453 unsigned int inside_prec = TYPE_PRECISION (inside_type);
2454 int inside_unsignedp = TYPE_UNSIGNED (inside_type);
2455 int inter_int = INTEGRAL_TYPE_P (inter_type);
2456 int inter_ptr = POINTER_TYPE_P (inter_type);
2457 int inter_float = FLOAT_TYPE_P (inter_type);
2458 int inter_vec = VECTOR_TYPE_P (inter_type);
2459 unsigned int inter_prec = TYPE_PRECISION (inter_type);
2460 int inter_unsignedp = TYPE_UNSIGNED (inter_type);
2461 int final_int = INTEGRAL_TYPE_P (type);
2462 int final_ptr = POINTER_TYPE_P (type);
2463 int final_float = FLOAT_TYPE_P (type);
2464 int final_vec = VECTOR_TYPE_P (type);
2465 unsigned int final_prec = TYPE_PRECISION (type);
2466 int final_unsignedp = TYPE_UNSIGNED (type);
2469 /* In addition to the cases of two conversions in a row
2470 handled below, if we are converting something to its own
2471 type via an object of identical or wider precision, neither
2472 conversion is needed. */
2473 (if (((GIMPLE && useless_type_conversion_p (type, inside_type))
2475 && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (inside_type)))
2476 && (((inter_int || inter_ptr) && final_int)
2477 || (inter_float && final_float))
2478 && inter_prec >= final_prec)
2481 /* Likewise, if the intermediate and initial types are either both
2482 float or both integer, we don't need the middle conversion if the
2483 former is wider than the latter and doesn't change the signedness
2484 (for integers). Avoid this if the final type is a pointer since
2485 then we sometimes need the middle conversion. */
2486 (if (((inter_int && inside_int) || (inter_float && inside_float))
2487 && (final_int || final_float)
2488 && inter_prec >= inside_prec
2489 && (inter_float || inter_unsignedp == inside_unsignedp))
2492 /* If we have a sign-extension of a zero-extended value, we can
2493 replace that by a single zero-extension. Likewise if the
2494 final conversion does not change precision we can drop the
2495 intermediate conversion. */
2496 (if (inside_int && inter_int && final_int
2497 && ((inside_prec < inter_prec && inter_prec < final_prec
2498 && inside_unsignedp && !inter_unsignedp)
2499 || final_prec == inter_prec))
2502 /* Two conversions in a row are not needed unless:
2503 - some conversion is floating-point (overstrict for now), or
2504 - some conversion is a vector (overstrict for now), or
2505 - the intermediate type is narrower than both initial and
2507 - the intermediate type and innermost type differ in signedness,
2508 and the outermost type is wider than the intermediate, or
2509 - the initial type is a pointer type and the precisions of the
2510 intermediate and final types differ, or
2511 - the final type is a pointer type and the precisions of the
2512 initial and intermediate types differ. */
2513 (if (! inside_float && ! inter_float && ! final_float
2514 && ! inside_vec && ! inter_vec && ! final_vec
2515 && (inter_prec >= inside_prec || inter_prec >= final_prec)
2516 && ! (inside_int && inter_int
2517 && inter_unsignedp != inside_unsignedp
2518 && inter_prec < final_prec)
2519 && ((inter_unsignedp && inter_prec > inside_prec)
2520 == (final_unsignedp && final_prec > inter_prec))
2521 && ! (inside_ptr && inter_prec != final_prec)
2522 && ! (final_ptr && inside_prec != inter_prec))
2525 /* A truncation to an unsigned type (a zero-extension) should be
2526 canonicalized as bitwise and of a mask. */
2527 (if (GIMPLE /* PR70366: doing this in GENERIC breaks -Wconversion. */
2528 && final_int && inter_int && inside_int
2529 && final_prec == inside_prec
2530 && final_prec > inter_prec
2532 (convert (bit_and @0 { wide_int_to_tree
2534 wi::mask (inter_prec, false,
2535 TYPE_PRECISION (inside_type))); })))
2537 /* If we are converting an integer to a floating-point that can
2538 represent it exactly and back to an integer, we can skip the
2539 floating-point conversion. */
2540 (if (GIMPLE /* PR66211 */
2541 && inside_int && inter_float && final_int &&
2542 (unsigned) significand_size (TYPE_MODE (inter_type))
2543 >= inside_prec - !inside_unsignedp)
2546 /* If we have a narrowing conversion to an integral type that is fed by a
2547 BIT_AND_EXPR, we might be able to remove the BIT_AND_EXPR if it merely
2548 masks off bits outside the final type (and nothing else). */
2550 (convert (bit_and @0 INTEGER_CST@1))
2551 (if (INTEGRAL_TYPE_P (type)
2552 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
2553 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))
2554 && operand_equal_p (@1, build_low_bits_mask (TREE_TYPE (@1),
2555 TYPE_PRECISION (type)), 0))
2559 /* (X /[ex] A) * A -> X. */
2561 (mult (convert1? (exact_div @0 @@1)) (convert2? @1))
2564 /* Canonicalization of binary operations. */
2566 /* Convert X + -C into X - C. */
2568 (plus @0 REAL_CST@1)
2569 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
2570 (with { tree tem = const_unop (NEGATE_EXPR, type, @1); }
2571 (if (!TREE_OVERFLOW (tem) || !flag_trapping_math)
2572 (minus @0 { tem; })))))
2574 /* Convert x+x into x*2. */
2577 (if (SCALAR_FLOAT_TYPE_P (type))
2578 (mult @0 { build_real (type, dconst2); })
2579 (if (INTEGRAL_TYPE_P (type))
2580 (mult @0 { build_int_cst (type, 2); }))))
2584 (minus integer_zerop @1)
2587 (pointer_diff integer_zerop @1)
2588 (negate (convert @1)))
2590 /* (ARG0 - ARG1) is the same as (-ARG1 + ARG0). So check whether
2591 ARG0 is zero and X + ARG0 reduces to X, since that would mean
2592 (-ARG1 + ARG0) reduces to -ARG1. */
2594 (minus real_zerop@0 @1)
2595 (if (fold_real_zero_addition_p (type, @0, 0))
2598 /* Transform x * -1 into -x. */
2600 (mult @0 integer_minus_onep)
2603 /* Reassociate (X * CST) * Y to (X * Y) * CST. This does not introduce
2604 signed overflow for CST != 0 && CST != -1. */
2606 (mult:c (mult:s@3 @0 INTEGER_CST@1) @2)
2607 (if (TREE_CODE (@2) != INTEGER_CST
2609 && !integer_zerop (@1) && !integer_minus_onep (@1))
2610 (mult (mult @0 @2) @1)))
2612 /* True if we can easily extract the real and imaginary parts of a complex
2614 (match compositional_complex
2615 (convert? (complex @0 @1)))
2617 /* COMPLEX_EXPR and REALPART/IMAGPART_EXPR cancellations. */
2619 (complex (realpart @0) (imagpart @0))
2622 (realpart (complex @0 @1))
2625 (imagpart (complex @0 @1))
2628 /* Sometimes we only care about half of a complex expression. */
2630 (realpart (convert?:s (conj:s @0)))
2631 (convert (realpart @0)))
2633 (imagpart (convert?:s (conj:s @0)))
2634 (convert (negate (imagpart @0))))
2635 (for part (realpart imagpart)
2636 (for op (plus minus)
2638 (part (convert?:s@2 (op:s @0 @1)))
2639 (convert (op (part @0) (part @1))))))
2641 (realpart (convert?:s (CEXPI:s @0)))
2644 (imagpart (convert?:s (CEXPI:s @0)))
2647 /* conj(conj(x)) -> x */
2649 (conj (convert? (conj @0)))
2650 (if (tree_nop_conversion_p (TREE_TYPE (@0), type))
2653 /* conj({x,y}) -> {x,-y} */
2655 (conj (convert?:s (complex:s @0 @1)))
2656 (with { tree itype = TREE_TYPE (type); }
2657 (complex (convert:itype @0) (negate (convert:itype @1)))))
2659 /* BSWAP simplifications, transforms checked by gcc.dg/builtin-bswap-8.c. */
2660 (for bswap (BUILT_IN_BSWAP16 BUILT_IN_BSWAP32 BUILT_IN_BSWAP64)
2665 (bswap (bit_not (bswap @0)))
2667 (for bitop (bit_xor bit_ior bit_and)
2669 (bswap (bitop:c (bswap @0) @1))
2670 (bitop @0 (bswap @1)))))
2673 /* Combine COND_EXPRs and VEC_COND_EXPRs. */
2675 /* Simplify constant conditions.
2676 Only optimize constant conditions when the selected branch
2677 has the same type as the COND_EXPR. This avoids optimizing
2678 away "c ? x : throw", where the throw has a void type.
2679 Note that we cannot throw away the fold-const.c variant nor
2680 this one as we depend on doing this transform before possibly
2681 A ? B : B -> B triggers and the fold-const.c one can optimize
2682 0 ? A : B to B even if A has side-effects. Something
2683 genmatch cannot handle. */
2685 (cond INTEGER_CST@0 @1 @2)
2686 (if (integer_zerop (@0))
2687 (if (!VOID_TYPE_P (TREE_TYPE (@2)) || VOID_TYPE_P (type))
2689 (if (!VOID_TYPE_P (TREE_TYPE (@1)) || VOID_TYPE_P (type))
2692 (vec_cond VECTOR_CST@0 @1 @2)
2693 (if (integer_all_onesp (@0))
2695 (if (integer_zerop (@0))
2698 /* Simplification moved from fold_cond_expr_with_comparison. It may also
2700 /* This pattern implements two kinds simplification:
2703 (cond (cmp (convert1? x) c1) (convert2? x) c2) -> (minmax (x c)) if:
2704 1) Conversions are type widening from smaller type.
2705 2) Const c1 equals to c2 after canonicalizing comparison.
2706 3) Comparison has tree code LT, LE, GT or GE.
2707 This specific pattern is needed when (cmp (convert x) c) may not
2708 be simplified by comparison patterns because of multiple uses of
2709 x. It also makes sense here because simplifying across multiple
2710 referred var is always benefitial for complicated cases.
2713 (cond (eq (convert1? x) c1) (convert2? x) c2) -> (cond (eq x c1) c1 c2). */
2714 (for cmp (lt le gt ge eq)
2716 (cond (cmp (convert1? @1) INTEGER_CST@3) (convert2? @1) INTEGER_CST@2)
2719 tree from_type = TREE_TYPE (@1);
2720 tree c1_type = TREE_TYPE (@3), c2_type = TREE_TYPE (@2);
2721 enum tree_code code = ERROR_MARK;
2723 if (INTEGRAL_TYPE_P (from_type)
2724 && int_fits_type_p (@2, from_type)
2725 && (types_match (c1_type, from_type)
2726 || (TYPE_PRECISION (c1_type) > TYPE_PRECISION (from_type)
2727 && (TYPE_UNSIGNED (from_type)
2728 || TYPE_SIGN (c1_type) == TYPE_SIGN (from_type))))
2729 && (types_match (c2_type, from_type)
2730 || (TYPE_PRECISION (c2_type) > TYPE_PRECISION (from_type)
2731 && (TYPE_UNSIGNED (from_type)
2732 || TYPE_SIGN (c2_type) == TYPE_SIGN (from_type)))))
2736 if (wi::to_widest (@3) == (wi::to_widest (@2) - 1))
2738 /* X <= Y - 1 equals to X < Y. */
2741 /* X > Y - 1 equals to X >= Y. */
2745 if (wi::to_widest (@3) == (wi::to_widest (@2) + 1))
2747 /* X < Y + 1 equals to X <= Y. */
2750 /* X >= Y + 1 equals to X > Y. */
2754 if (code != ERROR_MARK
2755 || wi::to_widest (@2) == wi::to_widest (@3))
2757 if (cmp == LT_EXPR || cmp == LE_EXPR)
2759 if (cmp == GT_EXPR || cmp == GE_EXPR)
2763 /* Can do A == C1 ? A : C2 -> A == C1 ? C1 : C2? */
2764 else if (int_fits_type_p (@3, from_type))
2768 (if (code == MAX_EXPR)
2769 (convert (max @1 (convert @2)))
2770 (if (code == MIN_EXPR)
2771 (convert (min @1 (convert @2)))
2772 (if (code == EQ_EXPR)
2773 (convert (cond (eq @1 (convert @3))
2774 (convert:from_type @3) (convert:from_type @2)))))))))
2776 /* (cond (cmp (convert? x) c1) (op x c2) c3) -> (op (minmax x c1) c2) if:
2778 1) OP is PLUS or MINUS.
2779 2) CMP is LT, LE, GT or GE.
2780 3) C3 == (C1 op C2), and computation doesn't have undefined behavior.
2782 This pattern also handles special cases like:
2784 A) Operand x is a unsigned to signed type conversion and c1 is
2785 integer zero. In this case,
2786 (signed type)x < 0 <=> x > MAX_VAL(signed type)
2787 (signed type)x >= 0 <=> x <= MAX_VAL(signed type)
2788 B) Const c1 may not equal to (C3 op' C2). In this case we also
2789 check equality for (c1+1) and (c1-1) by adjusting comparison
2792 TODO: Though signed type is handled by this pattern, it cannot be
2793 simplified at the moment because C standard requires additional
2794 type promotion. In order to match&simplify it here, the IR needs
2795 to be cleaned up by other optimizers, i.e, VRP. */
2796 (for op (plus minus)
2797 (for cmp (lt le gt ge)
2799 (cond (cmp (convert? @X) INTEGER_CST@1) (op @X INTEGER_CST@2) INTEGER_CST@3)
2800 (with { tree from_type = TREE_TYPE (@X), to_type = TREE_TYPE (@1); }
2801 (if (types_match (from_type, to_type)
2802 /* Check if it is special case A). */
2803 || (TYPE_UNSIGNED (from_type)
2804 && !TYPE_UNSIGNED (to_type)
2805 && TYPE_PRECISION (from_type) == TYPE_PRECISION (to_type)
2806 && integer_zerop (@1)
2807 && (cmp == LT_EXPR || cmp == GE_EXPR)))
2810 bool overflow = false;
2811 enum tree_code code, cmp_code = cmp;
2813 wide_int c1 = wi::to_wide (@1);
2814 wide_int c2 = wi::to_wide (@2);
2815 wide_int c3 = wi::to_wide (@3);
2816 signop sgn = TYPE_SIGN (from_type);
2818 /* Handle special case A), given x of unsigned type:
2819 ((signed type)x < 0) <=> (x > MAX_VAL(signed type))
2820 ((signed type)x >= 0) <=> (x <= MAX_VAL(signed type)) */
2821 if (!types_match (from_type, to_type))
2823 if (cmp_code == LT_EXPR)
2825 if (cmp_code == GE_EXPR)
2827 c1 = wi::max_value (to_type);
2829 /* To simplify this pattern, we require c3 = (c1 op c2). Here we
2830 compute (c3 op' c2) and check if it equals to c1 with op' being
2831 the inverted operator of op. Make sure overflow doesn't happen
2832 if it is undefined. */
2833 if (op == PLUS_EXPR)
2834 real_c1 = wi::sub (c3, c2, sgn, &overflow);
2836 real_c1 = wi::add (c3, c2, sgn, &overflow);
2839 if (!overflow || !TYPE_OVERFLOW_UNDEFINED (from_type))
2841 /* Check if c1 equals to real_c1. Boundary condition is handled
2842 by adjusting comparison operation if necessary. */
2843 if (!wi::cmp (wi::sub (real_c1, 1, sgn, &overflow), c1, sgn)
2846 /* X <= Y - 1 equals to X < Y. */
2847 if (cmp_code == LE_EXPR)
2849 /* X > Y - 1 equals to X >= Y. */
2850 if (cmp_code == GT_EXPR)
2853 if (!wi::cmp (wi::add (real_c1, 1, sgn, &overflow), c1, sgn)
2856 /* X < Y + 1 equals to X <= Y. */
2857 if (cmp_code == LT_EXPR)
2859 /* X >= Y + 1 equals to X > Y. */
2860 if (cmp_code == GE_EXPR)
2863 if (code != cmp_code || !wi::cmp (real_c1, c1, sgn))
2865 if (cmp_code == LT_EXPR || cmp_code == LE_EXPR)
2867 if (cmp_code == GT_EXPR || cmp_code == GE_EXPR)
2872 (if (code == MAX_EXPR)
2873 (op (max @X { wide_int_to_tree (from_type, real_c1); })
2874 { wide_int_to_tree (from_type, c2); })
2875 (if (code == MIN_EXPR)
2876 (op (min @X { wide_int_to_tree (from_type, real_c1); })
2877 { wide_int_to_tree (from_type, c2); })))))))))
2879 (for cnd (cond vec_cond)
2880 /* A ? B : (A ? X : C) -> A ? B : C. */
2882 (cnd @0 (cnd @0 @1 @2) @3)
2885 (cnd @0 @1 (cnd @0 @2 @3))
2887 /* A ? B : (!A ? C : X) -> A ? B : C. */
2888 /* ??? This matches embedded conditions open-coded because genmatch
2889 would generate matching code for conditions in separate stmts only.
2890 The following is still important to merge then and else arm cases
2891 from if-conversion. */
2893 (cnd @0 @1 (cnd @2 @3 @4))
2894 (if (COMPARISON_CLASS_P (@0)
2895 && COMPARISON_CLASS_P (@2)
2896 && invert_tree_comparison
2897 (TREE_CODE (@0), HONOR_NANS (TREE_OPERAND (@0, 0))) == TREE_CODE (@2)
2898 && operand_equal_p (TREE_OPERAND (@0, 0), TREE_OPERAND (@2, 0), 0)
2899 && operand_equal_p (TREE_OPERAND (@0, 1), TREE_OPERAND (@2, 1), 0))
2902 (cnd @0 (cnd @1 @2 @3) @4)
2903 (if (COMPARISON_CLASS_P (@0)
2904 && COMPARISON_CLASS_P (@1)
2905 && invert_tree_comparison
2906 (TREE_CODE (@0), HONOR_NANS (TREE_OPERAND (@0, 0))) == TREE_CODE (@1)
2907 && operand_equal_p (TREE_OPERAND (@0, 0), TREE_OPERAND (@1, 0), 0)
2908 && operand_equal_p (TREE_OPERAND (@0, 1), TREE_OPERAND (@1, 1), 0))
2911 /* A ? B : B -> B. */
2916 /* !A ? B : C -> A ? C : B. */
2918 (cnd (logical_inverted_value truth_valued_p@0) @1 @2)
2921 /* A + (B vcmp C ? 1 : 0) -> A - (B vcmp C ? -1 : 0), since vector comparisons
2922 return all -1 or all 0 results. */
2923 /* ??? We could instead convert all instances of the vec_cond to negate,
2924 but that isn't necessarily a win on its own. */
2926 (plus:c @3 (view_convert? (vec_cond:s @0 integer_each_onep@1 integer_zerop@2)))
2927 (if (VECTOR_TYPE_P (type)
2928 && known_eq (TYPE_VECTOR_SUBPARTS (type),
2929 TYPE_VECTOR_SUBPARTS (TREE_TYPE (@1)))
2930 && (TYPE_MODE (TREE_TYPE (type))
2931 == TYPE_MODE (TREE_TYPE (TREE_TYPE (@1)))))
2932 (minus @3 (view_convert (vec_cond @0 (negate @1) @2)))))
2934 /* ... likewise A - (B vcmp C ? 1 : 0) -> A + (B vcmp C ? -1 : 0). */
2936 (minus @3 (view_convert? (vec_cond:s @0 integer_each_onep@1 integer_zerop@2)))
2937 (if (VECTOR_TYPE_P (type)
2938 && known_eq (TYPE_VECTOR_SUBPARTS (type),
2939 TYPE_VECTOR_SUBPARTS (TREE_TYPE (@1)))
2940 && (TYPE_MODE (TREE_TYPE (type))
2941 == TYPE_MODE (TREE_TYPE (TREE_TYPE (@1)))))
2942 (plus @3 (view_convert (vec_cond @0 (negate @1) @2)))))
2945 /* Simplifications of comparisons. */
2947 /* See if we can reduce the magnitude of a constant involved in a
2948 comparison by changing the comparison code. This is a canonicalization
2949 formerly done by maybe_canonicalize_comparison_1. */
2953 (cmp @0 INTEGER_CST@1)
2954 (if (tree_int_cst_sgn (@1) == -1)
2955 (acmp @0 { wide_int_to_tree (TREE_TYPE (@1), wi::to_wide (@1) + 1); }))))
2959 (cmp @0 INTEGER_CST@1)
2960 (if (tree_int_cst_sgn (@1) == 1)
2961 (acmp @0 { wide_int_to_tree (TREE_TYPE (@1), wi::to_wide (@1) - 1); }))))
2964 /* We can simplify a logical negation of a comparison to the
2965 inverted comparison. As we cannot compute an expression
2966 operator using invert_tree_comparison we have to simulate
2967 that with expression code iteration. */
2968 (for cmp (tcc_comparison)
2969 icmp (inverted_tcc_comparison)
2970 ncmp (inverted_tcc_comparison_with_nans)
2971 /* Ideally we'd like to combine the following two patterns
2972 and handle some more cases by using
2973 (logical_inverted_value (cmp @0 @1))
2974 here but for that genmatch would need to "inline" that.
2975 For now implement what forward_propagate_comparison did. */
2977 (bit_not (cmp @0 @1))
2978 (if (VECTOR_TYPE_P (type)
2979 || (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1))
2980 /* Comparison inversion may be impossible for trapping math,
2981 invert_tree_comparison will tell us. But we can't use
2982 a computed operator in the replacement tree thus we have
2983 to play the trick below. */
2984 (with { enum tree_code ic = invert_tree_comparison
2985 (cmp, HONOR_NANS (@0)); }
2991 (bit_xor (cmp @0 @1) integer_truep)
2992 (with { enum tree_code ic = invert_tree_comparison
2993 (cmp, HONOR_NANS (@0)); }
2999 /* Transform comparisons of the form X - Y CMP 0 to X CMP Y.
3000 ??? The transformation is valid for the other operators if overflow
3001 is undefined for the type, but performing it here badly interacts
3002 with the transformation in fold_cond_expr_with_comparison which
3003 attempts to synthetize ABS_EXPR. */
3005 (for sub (minus pointer_diff)
3007 (cmp (sub@2 @0 @1) integer_zerop)
3008 (if (single_use (@2))
3011 /* Transform comparisons of the form X * C1 CMP 0 to X CMP 0 in the
3012 signed arithmetic case. That form is created by the compiler
3013 often enough for folding it to be of value. One example is in
3014 computing loop trip counts after Operator Strength Reduction. */
3015 (for cmp (simple_comparison)
3016 scmp (swapped_simple_comparison)
3018 (cmp (mult@3 @0 INTEGER_CST@1) integer_zerop@2)
3019 /* Handle unfolded multiplication by zero. */
3020 (if (integer_zerop (@1))
3022 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3023 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
3025 /* If @1 is negative we swap the sense of the comparison. */
3026 (if (tree_int_cst_sgn (@1) < 0)
3030 /* Simplify comparison of something with itself. For IEEE
3031 floating-point, we can only do some of these simplifications. */
3035 (if (! FLOAT_TYPE_P (TREE_TYPE (@0))
3036 || ! HONOR_NANS (@0))
3037 { constant_boolean_node (true, type); }
3038 (if (cmp != EQ_EXPR)
3044 || ! FLOAT_TYPE_P (TREE_TYPE (@0))
3045 || ! HONOR_NANS (@0))
3046 { constant_boolean_node (false, type); })))
3047 (for cmp (unle unge uneq)
3050 { constant_boolean_node (true, type); }))
3051 (for cmp (unlt ungt)
3057 (if (!flag_trapping_math)
3058 { constant_boolean_node (false, type); }))
3060 /* Fold ~X op ~Y as Y op X. */
3061 (for cmp (simple_comparison)
3063 (cmp (bit_not@2 @0) (bit_not@3 @1))
3064 (if (single_use (@2) && single_use (@3))
3067 /* Fold ~X op C as X op' ~C, where op' is the swapped comparison. */
3068 (for cmp (simple_comparison)
3069 scmp (swapped_simple_comparison)
3071 (cmp (bit_not@2 @0) CONSTANT_CLASS_P@1)
3072 (if (single_use (@2)
3073 && (TREE_CODE (@1) == INTEGER_CST || TREE_CODE (@1) == VECTOR_CST))
3074 (scmp @0 (bit_not @1)))))
3076 (for cmp (simple_comparison)
3077 /* Fold (double)float1 CMP (double)float2 into float1 CMP float2. */
3079 (cmp (convert@2 @0) (convert? @1))
3080 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
3081 && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@2))
3082 == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0)))
3083 && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@2))
3084 == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1))))
3087 tree type1 = TREE_TYPE (@1);
3088 if (TREE_CODE (@1) == REAL_CST && !DECIMAL_FLOAT_TYPE_P (type1))
3090 REAL_VALUE_TYPE orig = TREE_REAL_CST (@1);
3091 if (TYPE_PRECISION (type1) > TYPE_PRECISION (float_type_node)
3092 && exact_real_truncate (TYPE_MODE (float_type_node), &orig))
3093 type1 = float_type_node;
3094 if (TYPE_PRECISION (type1) > TYPE_PRECISION (double_type_node)
3095 && exact_real_truncate (TYPE_MODE (double_type_node), &orig))
3096 type1 = double_type_node;
3099 = (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type1)
3100 ? TREE_TYPE (@0) : type1);
3102 (if (TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (newtype))
3103 (cmp (convert:newtype @0) (convert:newtype @1))))))
3107 /* IEEE doesn't distinguish +0 and -0 in comparisons. */
3109 /* a CMP (-0) -> a CMP 0 */
3110 (if (REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@1)))
3111 (cmp @0 { build_real (TREE_TYPE (@1), dconst0); }))
3112 /* x != NaN is always true, other ops are always false. */
3113 (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
3114 && ! HONOR_SNANS (@1))
3115 { constant_boolean_node (cmp == NE_EXPR, type); })
3116 /* Fold comparisons against infinity. */
3117 (if (REAL_VALUE_ISINF (TREE_REAL_CST (@1))
3118 && MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (@1))))
3121 REAL_VALUE_TYPE max;
3122 enum tree_code code = cmp;
3123 bool neg = REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1));
3125 code = swap_tree_comparison (code);
3128 /* x > +Inf is always false, if we ignore NaNs or exceptions. */
3129 (if (code == GT_EXPR
3130 && !(HONOR_NANS (@0) && flag_trapping_math))
3131 { constant_boolean_node (false, type); })
3132 (if (code == LE_EXPR)
3133 /* x <= +Inf is always true, if we don't care about NaNs. */
3134 (if (! HONOR_NANS (@0))
3135 { constant_boolean_node (true, type); }
3136 /* x <= +Inf is the same as x == x, i.e. !isnan(x), but this loses
3137 an "invalid" exception. */
3138 (if (!flag_trapping_math)
3140 /* x == +Inf and x >= +Inf are always equal to x > DBL_MAX, but
3141 for == this introduces an exception for x a NaN. */
3142 (if ((code == EQ_EXPR && !(HONOR_NANS (@0) && flag_trapping_math))
3144 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
3146 (lt @0 { build_real (TREE_TYPE (@0), max); })
3147 (gt @0 { build_real (TREE_TYPE (@0), max); }))))
3148 /* x < +Inf is always equal to x <= DBL_MAX. */
3149 (if (code == LT_EXPR)
3150 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
3152 (ge @0 { build_real (TREE_TYPE (@0), max); })
3153 (le @0 { build_real (TREE_TYPE (@0), max); }))))
3154 /* x != +Inf is always equal to !(x > DBL_MAX), but this introduces
3155 an exception for x a NaN so use an unordered comparison. */
3156 (if (code == NE_EXPR)
3157 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
3158 (if (! HONOR_NANS (@0))
3160 (ge @0 { build_real (TREE_TYPE (@0), max); })
3161 (le @0 { build_real (TREE_TYPE (@0), max); }))
3163 (unge @0 { build_real (TREE_TYPE (@0), max); })
3164 (unle @0 { build_real (TREE_TYPE (@0), max); }))))))))))
3166 /* If this is a comparison of a real constant with a PLUS_EXPR
3167 or a MINUS_EXPR of a real constant, we can convert it into a
3168 comparison with a revised real constant as long as no overflow
3169 occurs when unsafe_math_optimizations are enabled. */
3170 (if (flag_unsafe_math_optimizations)
3171 (for op (plus minus)
3173 (cmp (op @0 REAL_CST@1) REAL_CST@2)
3176 tree tem = const_binop (op == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR,
3177 TREE_TYPE (@1), @2, @1);
3179 (if (tem && !TREE_OVERFLOW (tem))
3180 (cmp @0 { tem; }))))))
3182 /* Likewise, we can simplify a comparison of a real constant with
3183 a MINUS_EXPR whose first operand is also a real constant, i.e.
3184 (c1 - x) < c2 becomes x > c1-c2. Reordering is allowed on
3185 floating-point types only if -fassociative-math is set. */
3186 (if (flag_associative_math)
3188 (cmp (minus REAL_CST@0 @1) REAL_CST@2)
3189 (with { tree tem = const_binop (MINUS_EXPR, TREE_TYPE (@1), @0, @2); }
3190 (if (tem && !TREE_OVERFLOW (tem))
3191 (cmp { tem; } @1)))))
3193 /* Fold comparisons against built-in math functions. */
3194 (if (flag_unsafe_math_optimizations
3195 && ! flag_errno_math)
3198 (cmp (sq @0) REAL_CST@1)
3200 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
3202 /* sqrt(x) < y is always false, if y is negative. */
3203 (if (cmp == EQ_EXPR || cmp == LT_EXPR || cmp == LE_EXPR)
3204 { constant_boolean_node (false, type); })
3205 /* sqrt(x) > y is always true, if y is negative and we
3206 don't care about NaNs, i.e. negative values of x. */
3207 (if (cmp == NE_EXPR || !HONOR_NANS (@0))
3208 { constant_boolean_node (true, type); })
3209 /* sqrt(x) > y is the same as x >= 0, if y is negative. */
3210 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })))
3211 (if (real_equal (TREE_REAL_CST_PTR (@1), &dconst0))
3213 /* sqrt(x) < 0 is always false. */
3214 (if (cmp == LT_EXPR)
3215 { constant_boolean_node (false, type); })
3216 /* sqrt(x) >= 0 is always true if we don't care about NaNs. */
3217 (if (cmp == GE_EXPR && !HONOR_NANS (@0))
3218 { constant_boolean_node (true, type); })
3219 /* sqrt(x) <= 0 -> x == 0. */
3220 (if (cmp == LE_EXPR)
3222 /* Otherwise sqrt(x) cmp 0 -> x cmp 0. Here cmp can be >=, >,
3223 == or !=. In the last case:
3225 (sqrt(x) != 0) == (NaN != 0) == true == (x != 0)
3227 if x is negative or NaN. Due to -funsafe-math-optimizations,
3228 the results for other x follow from natural arithmetic. */
3230 (if (cmp == GT_EXPR || cmp == GE_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))
3239 /* sqrt(x) > y is x == +Inf, when y is very large. */
3240 (if (HONOR_INFINITIES (@0))
3241 (eq @0 { build_real (TREE_TYPE (@0), c2); })
3242 { constant_boolean_node (false, type); })
3243 /* sqrt(x) > c is the same as x > c*c. */
3244 (cmp @0 { build_real (TREE_TYPE (@0), c2); }))))
3245 (if (cmp == LT_EXPR || cmp == LE_EXPR)
3249 real_arithmetic (&c2, MULT_EXPR,
3250 &TREE_REAL_CST (@1), &TREE_REAL_CST (@1));
3251 real_convert (&c2, TYPE_MODE (TREE_TYPE (@0)), &c2);
3253 (if (REAL_VALUE_ISINF (c2))
3255 /* sqrt(x) < y is always true, when y is a very large
3256 value and we don't care about NaNs or Infinities. */
3257 (if (! HONOR_NANS (@0) && ! HONOR_INFINITIES (@0))
3258 { constant_boolean_node (true, type); })
3259 /* sqrt(x) < y is x != +Inf when y is very large and we
3260 don't care about NaNs. */
3261 (if (! HONOR_NANS (@0))
3262 (ne @0 { build_real (TREE_TYPE (@0), c2); }))
3263 /* sqrt(x) < y is x >= 0 when y is very large and we
3264 don't care about Infinities. */
3265 (if (! HONOR_INFINITIES (@0))
3266 (ge @0 { build_real (TREE_TYPE (@0), dconst0); }))
3267 /* sqrt(x) < y is x >= 0 && x != +Inf, when y is large. */
3270 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
3271 (ne @0 { build_real (TREE_TYPE (@0), c2); }))))
3272 /* sqrt(x) < c is the same as x < c*c, if we ignore NaNs. */
3273 (if (! HONOR_NANS (@0))
3274 (cmp @0 { build_real (TREE_TYPE (@0), c2); })
3275 /* sqrt(x) < c is the same as x >= 0 && x < c*c. */
3278 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
3279 (cmp @0 { build_real (TREE_TYPE (@0), c2); })))))))))
3280 /* Transform sqrt(x) cmp sqrt(y) -> x cmp y. */
3282 (cmp (sq @0) (sq @1))
3283 (if (! HONOR_NANS (@0))
3286 /* Optimize various special cases of (FTYPE) N CMP CST. */
3287 (for cmp (lt le eq ne ge gt)
3288 icmp (le le eq ne ge ge)
3290 (cmp (float @0) REAL_CST@1)
3291 (if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (@1))
3292 && ! DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1)))
3295 tree itype = TREE_TYPE (@0);
3296 signop isign = TYPE_SIGN (itype);
3297 format_helper fmt (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (@1))));
3298 const REAL_VALUE_TYPE *cst = TREE_REAL_CST_PTR (@1);
3299 /* Be careful to preserve any potential exceptions due to
3300 NaNs. qNaNs are ok in == or != context.
3301 TODO: relax under -fno-trapping-math or
3302 -fno-signaling-nans. */
3304 = real_isnan (cst) && (cst->signalling
3305 || (cmp != EQ_EXPR && cmp != NE_EXPR));
3306 /* INT?_MIN is power-of-two so it takes
3307 only one mantissa bit. */
3308 bool signed_p = isign == SIGNED;
3309 bool itype_fits_ftype_p
3310 = TYPE_PRECISION (itype) - signed_p <= significand_size (fmt);
3312 /* TODO: allow non-fitting itype and SNaNs when
3313 -fno-trapping-math. */
3314 (if (itype_fits_ftype_p && ! exception_p)
3317 REAL_VALUE_TYPE imin, imax;
3318 real_from_integer (&imin, fmt, wi::min_value (itype), isign);
3319 real_from_integer (&imax, fmt, wi::max_value (itype), isign);
3321 REAL_VALUE_TYPE icst;
3322 if (cmp == GT_EXPR || cmp == GE_EXPR)
3323 real_ceil (&icst, fmt, cst);
3324 else if (cmp == LT_EXPR || cmp == LE_EXPR)
3325 real_floor (&icst, fmt, cst);
3327 real_trunc (&icst, fmt, cst);
3329 bool cst_int_p = !real_isnan (cst) && real_identical (&icst, cst);
3331 bool overflow_p = false;
3333 = real_to_integer (&icst, &overflow_p, TYPE_PRECISION (itype));
3336 /* Optimize cases when CST is outside of ITYPE's range. */
3337 (if (real_compare (LT_EXPR, cst, &imin))
3338 { constant_boolean_node (cmp == GT_EXPR || cmp == GE_EXPR || cmp == NE_EXPR,
3340 (if (real_compare (GT_EXPR, cst, &imax))
3341 { constant_boolean_node (cmp == LT_EXPR || cmp == LE_EXPR || cmp == NE_EXPR,
3343 /* Remove cast if CST is an integer representable by ITYPE. */
3345 (cmp @0 { gcc_assert (!overflow_p);
3346 wide_int_to_tree (itype, icst_val); })
3348 /* When CST is fractional, optimize
3349 (FTYPE) N == CST -> 0
3350 (FTYPE) N != CST -> 1. */
3351 (if (cmp == EQ_EXPR || cmp == NE_EXPR)
3352 { constant_boolean_node (cmp == NE_EXPR, type); })
3353 /* Otherwise replace with sensible integer constant. */
3356 gcc_checking_assert (!overflow_p);
3358 (icmp @0 { wide_int_to_tree (itype, icst_val); })))))))))
3360 /* Fold A /[ex] B CMP C to A CMP B * C. */
3363 (cmp (exact_div @0 @1) INTEGER_CST@2)
3364 (if (!integer_zerop (@1))
3365 (if (wi::to_wide (@2) == 0)
3367 (if (TREE_CODE (@1) == INTEGER_CST)
3371 wide_int prod = wi::mul (wi::to_wide (@2), wi::to_wide (@1),
3372 TYPE_SIGN (TREE_TYPE (@1)), &ovf);
3375 { constant_boolean_node (cmp == NE_EXPR, type); }
3376 (cmp @0 { wide_int_to_tree (TREE_TYPE (@0), prod); }))))))))
3377 (for cmp (lt le gt ge)
3379 (cmp (exact_div @0 INTEGER_CST@1) INTEGER_CST@2)
3380 (if (wi::gt_p (wi::to_wide (@1), 0, TYPE_SIGN (TREE_TYPE (@1))))
3384 wide_int prod = wi::mul (wi::to_wide (@2), wi::to_wide (@1),
3385 TYPE_SIGN (TREE_TYPE (@1)), &ovf);
3388 { constant_boolean_node (wi::lt_p (wi::to_wide (@2), 0,
3389 TYPE_SIGN (TREE_TYPE (@2)))
3390 != (cmp == LT_EXPR || cmp == LE_EXPR), type); }
3391 (cmp @0 { wide_int_to_tree (TREE_TYPE (@0), prod); }))))))
3393 /* Unordered tests if either argument is a NaN. */
3395 (bit_ior (unordered @0 @0) (unordered @1 @1))
3396 (if (types_match (@0, @1))
3399 (bit_and (ordered @0 @0) (ordered @1 @1))
3400 (if (types_match (@0, @1))
3403 (bit_ior:c (unordered @0 @0) (unordered:c@2 @0 @1))
3406 (bit_and:c (ordered @0 @0) (ordered:c@2 @0 @1))
3409 /* Simple range test simplifications. */
3410 /* A < B || A >= B -> true. */
3411 (for test1 (lt le le le ne ge)
3412 test2 (ge gt ge ne eq ne)
3414 (bit_ior:c (test1 @0 @1) (test2 @0 @1))
3415 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
3416 || VECTOR_INTEGER_TYPE_P (TREE_TYPE (@0)))
3417 { constant_boolean_node (true, type); })))
3418 /* A < B && A >= B -> false. */
3419 (for test1 (lt lt lt le ne eq)
3420 test2 (ge gt eq gt eq gt)
3422 (bit_and:c (test1 @0 @1) (test2 @0 @1))
3423 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
3424 || VECTOR_INTEGER_TYPE_P (TREE_TYPE (@0)))
3425 { constant_boolean_node (false, type); })))
3427 /* A & (2**N - 1) <= 2**K - 1 -> A & (2**N - 2**K) == 0
3428 A & (2**N - 1) > 2**K - 1 -> A & (2**N - 2**K) != 0
3430 Note that comparisons
3431 A & (2**N - 1) < 2**K -> A & (2**N - 2**K) == 0
3432 A & (2**N - 1) >= 2**K -> A & (2**N - 2**K) != 0
3433 will be canonicalized to above so there's no need to
3440 (cmp (bit_and@0 @1 INTEGER_CST@2) INTEGER_CST@3)
3441 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)))
3444 tree ty = TREE_TYPE (@0);
3445 unsigned prec = TYPE_PRECISION (ty);
3446 wide_int mask = wi::to_wide (@2, prec);
3447 wide_int rhs = wi::to_wide (@3, prec);
3448 signop sgn = TYPE_SIGN (ty);
3450 (if ((mask & (mask + 1)) == 0 && wi::gt_p (rhs, 0, sgn)
3451 && (rhs & (rhs + 1)) == 0 && wi::ge_p (mask, rhs, sgn))
3452 (eqcmp (bit_and @1 { wide_int_to_tree (ty, mask - rhs); })
3453 { build_zero_cst (ty); }))))))
3455 /* -A CMP -B -> B CMP A. */
3456 (for cmp (tcc_comparison)
3457 scmp (swapped_tcc_comparison)
3459 (cmp (negate @0) (negate @1))
3460 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
3461 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3462 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
3465 (cmp (negate @0) CONSTANT_CLASS_P@1)
3466 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
3467 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3468 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
3469 (with { tree tem = const_unop (NEGATE_EXPR, TREE_TYPE (@0), @1); }
3470 (if (tem && !TREE_OVERFLOW (tem))
3471 (scmp @0 { tem; }))))))
3473 /* Convert ABS_EXPR<x> == 0 or ABS_EXPR<x> != 0 to x == 0 or x != 0. */
3476 (op (abs @0) zerop@1)
3479 /* From fold_sign_changed_comparison and fold_widened_comparison.
3480 FIXME: the lack of symmetry is disturbing. */
3481 (for cmp (simple_comparison)
3483 (cmp (convert@0 @00) (convert?@1 @10))
3484 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
3485 /* Disable this optimization if we're casting a function pointer
3486 type on targets that require function pointer canonicalization. */
3487 && !(targetm.have_canonicalize_funcptr_for_compare ()
3488 && TREE_CODE (TREE_TYPE (@00)) == POINTER_TYPE
3489 && TREE_CODE (TREE_TYPE (TREE_TYPE (@00))) == FUNCTION_TYPE)
3491 (if (TYPE_PRECISION (TREE_TYPE (@00)) == TYPE_PRECISION (TREE_TYPE (@0))
3492 && (TREE_CODE (@10) == INTEGER_CST
3494 && (TYPE_UNSIGNED (TREE_TYPE (@00)) == TYPE_UNSIGNED (TREE_TYPE (@0))
3497 && !POINTER_TYPE_P (TREE_TYPE (@00)))
3498 /* ??? The special-casing of INTEGER_CST conversion was in the original
3499 code and here to avoid a spurious overflow flag on the resulting
3500 constant which fold_convert produces. */
3501 (if (TREE_CODE (@1) == INTEGER_CST)
3502 (cmp @00 { force_fit_type (TREE_TYPE (@00), wi::to_widest (@1), 0,
3503 TREE_OVERFLOW (@1)); })
3504 (cmp @00 (convert @1)))
3506 (if (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (TREE_TYPE (@00)))
3507 /* If possible, express the comparison in the shorter mode. */
3508 (if ((cmp == EQ_EXPR || cmp == NE_EXPR
3509 || TYPE_UNSIGNED (TREE_TYPE (@0)) == TYPE_UNSIGNED (TREE_TYPE (@00))
3510 || (!TYPE_UNSIGNED (TREE_TYPE (@0))
3511 && TYPE_UNSIGNED (TREE_TYPE (@00))))
3512 && (types_match (TREE_TYPE (@10), TREE_TYPE (@00))
3513 || ((TYPE_PRECISION (TREE_TYPE (@00))
3514 >= TYPE_PRECISION (TREE_TYPE (@10)))
3515 && (TYPE_UNSIGNED (TREE_TYPE (@00))
3516 == TYPE_UNSIGNED (TREE_TYPE (@10))))
3517 || (TREE_CODE (@10) == INTEGER_CST
3518 && INTEGRAL_TYPE_P (TREE_TYPE (@00))
3519 && int_fits_type_p (@10, TREE_TYPE (@00)))))
3520 (cmp @00 (convert @10))
3521 (if (TREE_CODE (@10) == INTEGER_CST
3522 && INTEGRAL_TYPE_P (TREE_TYPE (@00))
3523 && !int_fits_type_p (@10, TREE_TYPE (@00)))
3526 tree min = lower_bound_in_type (TREE_TYPE (@10), TREE_TYPE (@00));
3527 tree max = upper_bound_in_type (TREE_TYPE (@10), TREE_TYPE (@00));
3528 bool above = integer_nonzerop (const_binop (LT_EXPR, type, max, @10));
3529 bool below = integer_nonzerop (const_binop (LT_EXPR, type, @10, min));
3531 (if (above || below)
3532 (if (cmp == EQ_EXPR || cmp == NE_EXPR)
3533 { constant_boolean_node (cmp == EQ_EXPR ? false : true, type); }
3534 (if (cmp == LT_EXPR || cmp == LE_EXPR)
3535 { constant_boolean_node (above ? true : false, type); }
3536 (if (cmp == GT_EXPR || cmp == GE_EXPR)
3537 { constant_boolean_node (above ? false : true, type); }))))))))))))
3540 /* A local variable can never be pointed to by
3541 the default SSA name of an incoming parameter.
3542 SSA names are canonicalized to 2nd place. */
3544 (cmp addr@0 SSA_NAME@1)
3545 (if (SSA_NAME_IS_DEFAULT_DEF (@1)
3546 && TREE_CODE (SSA_NAME_VAR (@1)) == PARM_DECL)
3547 (with { tree base = get_base_address (TREE_OPERAND (@0, 0)); }
3548 (if (TREE_CODE (base) == VAR_DECL
3549 && auto_var_in_fn_p (base, current_function_decl))
3550 (if (cmp == NE_EXPR)
3551 { constant_boolean_node (true, type); }
3552 { constant_boolean_node (false, type); }))))))
3554 /* Equality compare simplifications from fold_binary */
3557 /* If we have (A | C) == D where C & ~D != 0, convert this into 0.
3558 Similarly for NE_EXPR. */
3560 (cmp (convert?@3 (bit_ior @0 INTEGER_CST@1)) INTEGER_CST@2)
3561 (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0))
3562 && wi::bit_and_not (wi::to_wide (@1), wi::to_wide (@2)) != 0)
3563 { constant_boolean_node (cmp == NE_EXPR, type); }))
3565 /* (X ^ Y) == 0 becomes X == Y, and (X ^ Y) != 0 becomes X != Y. */
3567 (cmp (bit_xor @0 @1) integer_zerop)
3570 /* (X ^ Y) == Y becomes X == 0.
3571 Likewise (X ^ Y) == X becomes Y == 0. */
3573 (cmp:c (bit_xor:c @0 @1) @0)
3574 (cmp @1 { build_zero_cst (TREE_TYPE (@1)); }))
3576 /* (X ^ C1) op C2 can be rewritten as X op (C1 ^ C2). */
3578 (cmp (convert?@3 (bit_xor @0 INTEGER_CST@1)) INTEGER_CST@2)
3579 (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0)))
3580 (cmp @0 (bit_xor @1 (convert @2)))))
3583 (cmp (convert? addr@0) integer_zerop)
3584 (if (tree_single_nonzero_warnv_p (@0, NULL))
3585 { constant_boolean_node (cmp == NE_EXPR, type); })))
3587 /* If we have (A & C) == C where C is a power of 2, convert this into
3588 (A & C) != 0. Similarly for NE_EXPR. */
3592 (cmp (bit_and@2 @0 integer_pow2p@1) @1)
3593 (icmp @2 { build_zero_cst (TREE_TYPE (@0)); })))
3595 /* If we have (A & C) != 0 ? D : 0 where C and D are powers of 2,
3596 convert this into a shift followed by ANDing with D. */
3599 (ne (bit_and @0 integer_pow2p@1) integer_zerop)
3600 INTEGER_CST@2 integer_zerop)
3601 (if (integer_pow2p (@2))
3603 int shift = (wi::exact_log2 (wi::to_wide (@2))
3604 - wi::exact_log2 (wi::to_wide (@1)));
3608 (lshift (convert @0) { build_int_cst (integer_type_node, shift); }) @2)
3610 (convert (rshift @0 { build_int_cst (integer_type_node, -shift); }))
3613 /* If we have (A & C) != 0 where C is the sign bit of A, convert
3614 this into A < 0. Similarly for (A & C) == 0 into A >= 0. */
3618 (cmp (bit_and (convert?@2 @0) integer_pow2p@1) integer_zerop)
3619 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
3620 && type_has_mode_precision_p (TREE_TYPE (@0))
3621 && element_precision (@2) >= element_precision (@0)
3622 && wi::only_sign_bit_p (wi::to_wide (@1), element_precision (@0)))
3623 (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
3624 (ncmp (convert:stype @0) { build_zero_cst (stype); })))))
3626 /* If we have A < 0 ? C : 0 where C is a power of 2, convert
3627 this into a right shift or sign extension followed by ANDing with C. */
3630 (lt @0 integer_zerop)
3631 INTEGER_CST@1 integer_zerop)
3632 (if (integer_pow2p (@1)
3633 && !TYPE_UNSIGNED (TREE_TYPE (@0)))
3635 int shift = element_precision (@0) - wi::exact_log2 (wi::to_wide (@1)) - 1;
3639 (convert (rshift @0 { build_int_cst (integer_type_node, shift); }))
3641 /* Otherwise ctype must be wider than TREE_TYPE (@0) and pure
3642 sign extension followed by AND with C will achieve the effect. */
3643 (bit_and (convert @0) @1)))))
3645 /* When the addresses are not directly of decls compare base and offset.
3646 This implements some remaining parts of fold_comparison address
3647 comparisons but still no complete part of it. Still it is good
3648 enough to make fold_stmt not regress when not dispatching to fold_binary. */
3649 (for cmp (simple_comparison)
3651 (cmp (convert1?@2 addr@0) (convert2? addr@1))
3654 poly_int64 off0, off1;
3655 tree base0 = get_addr_base_and_unit_offset (TREE_OPERAND (@0, 0), &off0);
3656 tree base1 = get_addr_base_and_unit_offset (TREE_OPERAND (@1, 0), &off1);
3657 if (base0 && TREE_CODE (base0) == MEM_REF)
3659 off0 += mem_ref_offset (base0).force_shwi ();
3660 base0 = TREE_OPERAND (base0, 0);
3662 if (base1 && TREE_CODE (base1) == MEM_REF)
3664 off1 += mem_ref_offset (base1).force_shwi ();
3665 base1 = TREE_OPERAND (base1, 0);
3668 (if (base0 && base1)
3672 /* Punt in GENERIC on variables with value expressions;
3673 the value expressions might point to fields/elements
3674 of other vars etc. */
3676 && ((VAR_P (base0) && DECL_HAS_VALUE_EXPR_P (base0))
3677 || (VAR_P (base1) && DECL_HAS_VALUE_EXPR_P (base1))))
3679 else if (decl_in_symtab_p (base0)
3680 && decl_in_symtab_p (base1))
3681 equal = symtab_node::get_create (base0)
3682 ->equal_address_to (symtab_node::get_create (base1));
3683 else if ((DECL_P (base0)
3684 || TREE_CODE (base0) == SSA_NAME
3685 || TREE_CODE (base0) == STRING_CST)
3687 || TREE_CODE (base1) == SSA_NAME
3688 || TREE_CODE (base1) == STRING_CST))
3689 equal = (base0 == base1);
3692 && (cmp == EQ_EXPR || cmp == NE_EXPR
3693 /* If the offsets are equal we can ignore overflow. */
3694 || known_eq (off0, off1)
3695 || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
3696 /* Or if we compare using pointers to decls or strings. */
3697 || (POINTER_TYPE_P (TREE_TYPE (@2))
3698 && (DECL_P (base0) || TREE_CODE (base0) == STRING_CST))))
3700 (if (cmp == EQ_EXPR && (known_eq (off0, off1) || known_ne (off0, off1)))
3701 { constant_boolean_node (known_eq (off0, off1), type); })
3702 (if (cmp == NE_EXPR && (known_eq (off0, off1) || known_ne (off0, off1)))
3703 { constant_boolean_node (known_ne (off0, off1), type); })
3704 (if (cmp == LT_EXPR && (known_lt (off0, off1) || known_ge (off0, off1)))
3705 { constant_boolean_node (known_lt (off0, off1), type); })
3706 (if (cmp == LE_EXPR && (known_le (off0, off1) || known_gt (off0, off1)))
3707 { constant_boolean_node (known_le (off0, off1), type); })
3708 (if (cmp == GE_EXPR && (known_ge (off0, off1) || known_lt (off0, off1)))
3709 { constant_boolean_node (known_ge (off0, off1), type); })
3710 (if (cmp == GT_EXPR && (known_gt (off0, off1) || known_le (off0, off1)))
3711 { constant_boolean_node (known_gt (off0, off1), type); }))
3713 && DECL_P (base0) && DECL_P (base1)
3714 /* If we compare this as integers require equal offset. */
3715 && (!INTEGRAL_TYPE_P (TREE_TYPE (@2))
3716 || known_eq (off0, off1)))
3718 (if (cmp == EQ_EXPR)
3719 { constant_boolean_node (false, type); })
3720 (if (cmp == NE_EXPR)
3721 { constant_boolean_node (true, type); })))))))))
3723 /* Simplify pointer equality compares using PTA. */
3727 (if (POINTER_TYPE_P (TREE_TYPE (@0))
3728 && ptrs_compare_unequal (@0, @1))
3729 { constant_boolean_node (neeq != EQ_EXPR, type); })))
3731 /* PR70920: Transform (intptr_t)x eq/ne CST to x eq/ne (typeof x) CST.
3732 and (typeof ptr_cst) x eq/ne ptr_cst to x eq/ne (typeof x) CST.
3733 Disable the transform if either operand is pointer to function.
3734 This broke pr22051-2.c for arm where function pointer
3735 canonicalizaion is not wanted. */
3739 (cmp (convert @0) INTEGER_CST@1)
3740 (if (((POINTER_TYPE_P (TREE_TYPE (@0))
3741 && !FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@0)))
3742 && INTEGRAL_TYPE_P (TREE_TYPE (@1)))
3743 || (INTEGRAL_TYPE_P (TREE_TYPE (@0))
3744 && POINTER_TYPE_P (TREE_TYPE (@1))
3745 && !FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@1)))))
3746 && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
3747 (cmp @0 (convert @1)))))
3749 /* Non-equality compare simplifications from fold_binary */
3750 (for cmp (lt gt le ge)
3751 /* Comparisons with the highest or lowest possible integer of
3752 the specified precision will have known values. */
3754 (cmp (convert?@2 @0) INTEGER_CST@1)
3755 (if ((INTEGRAL_TYPE_P (TREE_TYPE (@1)) || POINTER_TYPE_P (TREE_TYPE (@1)))
3756 && tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@0)))
3759 tree arg1_type = TREE_TYPE (@1);
3760 unsigned int prec = TYPE_PRECISION (arg1_type);
3761 wide_int max = wi::max_value (arg1_type);
3762 wide_int signed_max = wi::max_value (prec, SIGNED);
3763 wide_int min = wi::min_value (arg1_type);
3766 (if (wi::to_wide (@1) == max)
3768 (if (cmp == GT_EXPR)
3769 { constant_boolean_node (false, type); })
3770 (if (cmp == GE_EXPR)
3772 (if (cmp == LE_EXPR)
3773 { constant_boolean_node (true, type); })
3774 (if (cmp == LT_EXPR)
3776 (if (wi::to_wide (@1) == min)
3778 (if (cmp == LT_EXPR)
3779 { constant_boolean_node (false, type); })
3780 (if (cmp == LE_EXPR)
3782 (if (cmp == GE_EXPR)
3783 { constant_boolean_node (true, type); })
3784 (if (cmp == GT_EXPR)
3786 (if (wi::to_wide (@1) == max - 1)
3788 (if (cmp == GT_EXPR)
3789 (eq @2 { wide_int_to_tree (TREE_TYPE (@1), wi::to_wide (@1) + 1); }))
3790 (if (cmp == LE_EXPR)
3791 (ne @2 { wide_int_to_tree (TREE_TYPE (@1), wi::to_wide (@1) + 1); }))))
3792 (if (wi::to_wide (@1) == min + 1)
3794 (if (cmp == GE_EXPR)
3795 (ne @2 { wide_int_to_tree (TREE_TYPE (@1), wi::to_wide (@1) - 1); }))
3796 (if (cmp == LT_EXPR)
3797 (eq @2 { wide_int_to_tree (TREE_TYPE (@1), wi::to_wide (@1) - 1); }))))
3798 (if (wi::to_wide (@1) == signed_max
3799 && TYPE_UNSIGNED (arg1_type)
3800 /* We will flip the signedness of the comparison operator
3801 associated with the mode of @1, so the sign bit is
3802 specified by this mode. Check that @1 is the signed
3803 max associated with this sign bit. */
3804 && prec == GET_MODE_PRECISION (SCALAR_INT_TYPE_MODE (arg1_type))
3805 /* signed_type does not work on pointer types. */
3806 && INTEGRAL_TYPE_P (arg1_type))
3807 /* The following case also applies to X < signed_max+1
3808 and X >= signed_max+1 because previous transformations. */
3809 (if (cmp == LE_EXPR || cmp == GT_EXPR)
3810 (with { tree st = signed_type_for (arg1_type); }
3811 (if (cmp == LE_EXPR)
3812 (ge (convert:st @0) { build_zero_cst (st); })
3813 (lt (convert:st @0) { build_zero_cst (st); }))))))))))
3815 (for cmp (unordered ordered unlt unle ungt unge uneq ltgt)
3816 /* If the second operand is NaN, the result is constant. */
3819 (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
3820 && (cmp != LTGT_EXPR || ! flag_trapping_math))
3821 { constant_boolean_node (cmp == ORDERED_EXPR || cmp == LTGT_EXPR
3822 ? false : true, type); })))
3824 /* bool_var != 0 becomes bool_var. */
3826 (ne @0 integer_zerop)
3827 (if (TREE_CODE (TREE_TYPE (@0)) == BOOLEAN_TYPE
3828 && types_match (type, TREE_TYPE (@0)))
3830 /* bool_var == 1 becomes bool_var. */
3832 (eq @0 integer_onep)
3833 (if (TREE_CODE (TREE_TYPE (@0)) == BOOLEAN_TYPE
3834 && types_match (type, TREE_TYPE (@0)))
3837 bool_var == 0 becomes !bool_var or
3838 bool_var != 1 becomes !bool_var
3839 here because that only is good in assignment context as long
3840 as we require a tcc_comparison in GIMPLE_CONDs where we'd
3841 replace if (x == 0) with tem = ~x; if (tem != 0) which is
3842 clearly less optimal and which we'll transform again in forwprop. */
3844 /* When one argument is a constant, overflow detection can be simplified.
3845 Currently restricted to single use so as not to interfere too much with
3846 ADD_OVERFLOW detection in tree-ssa-math-opts.c.
3847 A + CST CMP A -> A CMP' CST' */
3848 (for cmp (lt le ge gt)
3851 (cmp:c (plus@2 @0 INTEGER_CST@1) @0)
3852 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
3853 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))
3854 && wi::to_wide (@1) != 0
3856 (with { unsigned int prec = TYPE_PRECISION (TREE_TYPE (@0)); }
3857 (out @0 { wide_int_to_tree (TREE_TYPE (@0),
3858 wi::max_value (prec, UNSIGNED)
3859 - wi::to_wide (@1)); })))))
3861 /* To detect overflow in unsigned A - B, A < B is simpler than A - B > A.
3862 However, the detection logic for SUB_OVERFLOW in tree-ssa-math-opts.c
3863 expects the long form, so we restrict the transformation for now. */
3866 (cmp:c (minus@2 @0 @1) @0)
3867 (if (single_use (@2)
3868 && ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3869 && TYPE_UNSIGNED (TREE_TYPE (@0))
3870 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
3873 /* Testing for overflow is unnecessary if we already know the result. */
3878 (cmp:c (realpart (IFN_SUB_OVERFLOW@2 @0 @1)) @0)
3879 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
3880 && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
3881 (out (imagpart @2) { build_zero_cst (TREE_TYPE (@0)); }))))
3886 (cmp:c (realpart (IFN_ADD_OVERFLOW:c@2 @0 @1)) @0)
3887 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
3888 && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
3889 (out (imagpart @2) { build_zero_cst (TREE_TYPE (@0)); }))))
3891 /* For unsigned operands, -1 / B < A checks whether A * B would overflow.
3892 Simplify it to __builtin_mul_overflow (A, B, <unused>). */
3896 (cmp:c (trunc_div:s integer_all_onesp @1) @0)
3897 (if (TYPE_UNSIGNED (TREE_TYPE (@0)) && !VECTOR_TYPE_P (TREE_TYPE (@0)))
3898 (with { tree t = TREE_TYPE (@0), cpx = build_complex_type (t); }
3899 (out (imagpart (IFN_MUL_OVERFLOW:cpx @0 @1)) { build_zero_cst (t); })))))
3901 /* Simplification of math builtins. These rules must all be optimizations
3902 as well as IL simplifications. If there is a possibility that the new
3903 form could be a pessimization, the rule should go in the canonicalization
3904 section that follows this one.
3906 Rules can generally go in this section if they satisfy one of
3909 - the rule describes an identity
3911 - the rule replaces calls with something as simple as addition or
3914 - the rule contains unary calls only and simplifies the surrounding
3915 arithmetic. (The idea here is to exclude non-unary calls in which
3916 one operand is constant and in which the call is known to be cheap
3917 when the operand has that value.) */
3919 (if (flag_unsafe_math_optimizations)
3920 /* Simplify sqrt(x) * sqrt(x) -> x. */
3922 (mult (SQRT_ALL@1 @0) @1)
3923 (if (!HONOR_SNANS (type))
3926 (for op (plus minus)
3927 /* Simplify (A / C) +- (B / C) -> (A +- B) / C. */
3931 (rdiv (op @0 @2) @1)))
3933 /* Simplify sqrt(x) * sqrt(y) -> sqrt(x*y). */
3934 (for root (SQRT CBRT)
3936 (mult (root:s @0) (root:s @1))
3937 (root (mult @0 @1))))
3939 /* Simplify expN(x) * expN(y) -> expN(x+y). */
3940 (for exps (EXP EXP2 EXP10 POW10)
3942 (mult (exps:s @0) (exps:s @1))
3943 (exps (plus @0 @1))))
3945 /* Simplify a/root(b/c) into a*root(c/b). */
3946 (for root (SQRT CBRT)
3948 (rdiv @0 (root:s (rdiv:s @1 @2)))
3949 (mult @0 (root (rdiv @2 @1)))))
3951 /* Simplify x/expN(y) into x*expN(-y). */
3952 (for exps (EXP EXP2 EXP10 POW10)
3954 (rdiv @0 (exps:s @1))
3955 (mult @0 (exps (negate @1)))))
3957 (for logs (LOG LOG2 LOG10 LOG10)
3958 exps (EXP EXP2 EXP10 POW10)
3959 /* logN(expN(x)) -> x. */
3963 /* expN(logN(x)) -> x. */
3968 /* Optimize logN(func()) for various exponential functions. We
3969 want to determine the value "x" and the power "exponent" in
3970 order to transform logN(x**exponent) into exponent*logN(x). */
3971 (for logs (LOG LOG LOG LOG2 LOG2 LOG2 LOG10 LOG10)
3972 exps (EXP2 EXP10 POW10 EXP EXP10 POW10 EXP EXP2)
3975 (if (SCALAR_FLOAT_TYPE_P (type))
3981 /* Prepare to do logN(exp(exponent)) -> exponent*logN(e). */
3982 x = build_real_truncate (type, dconst_e ());
3985 /* Prepare to do logN(exp2(exponent)) -> exponent*logN(2). */
3986 x = build_real (type, dconst2);
3990 /* Prepare to do logN(exp10(exponent)) -> exponent*logN(10). */
3992 REAL_VALUE_TYPE dconst10;
3993 real_from_integer (&dconst10, VOIDmode, 10, SIGNED);
3994 x = build_real (type, dconst10);
4001 (mult (logs { x; }) @0)))))
4009 (if (SCALAR_FLOAT_TYPE_P (type))
4015 /* Prepare to do logN(sqrt(x)) -> 0.5*logN(x). */
4016 x = build_real (type, dconsthalf);
4019 /* Prepare to do logN(cbrt(x)) -> (1/3)*logN(x). */
4020 x = build_real_truncate (type, dconst_third ());
4026 (mult { x; } (logs @0))))))
4028 /* logN(pow(x,exponent)) -> exponent*logN(x). */
4029 (for logs (LOG LOG2 LOG10)
4033 (mult @1 (logs @0))))
4035 /* pow(C,x) -> exp(log(C)*x) if C > 0,
4036 or if C is a positive power of 2,
4037 pow(C,x) -> exp2(log2(C)*x). */
4045 (pows REAL_CST@0 @1)
4046 (if (real_compare (GT_EXPR, TREE_REAL_CST_PTR (@0), &dconst0)
4047 && real_isfinite (TREE_REAL_CST_PTR (@0))
4048 /* As libmvec doesn't have a vectorized exp2, defer optimizing
4049 the use_exp2 case until after vectorization. It seems actually
4050 beneficial for all constants to postpone this until later,
4051 because exp(log(C)*x), while faster, will have worse precision
4052 and if x folds into a constant too, that is unnecessary
4054 && canonicalize_math_after_vectorization_p ())
4056 const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (@0);
4057 bool use_exp2 = false;
4058 if (targetm.libc_has_function (function_c99_misc)
4059 && value->cl == rvc_normal)
4061 REAL_VALUE_TYPE frac_rvt = *value;
4062 SET_REAL_EXP (&frac_rvt, 1);
4063 if (real_equal (&frac_rvt, &dconst1))
4068 (if (optimize_pow_to_exp (@0, @1))
4069 (exps (mult (logs @0) @1)))
4070 (exp2s (mult (log2s @0) @1)))))))
4073 /* pow(C,x)*expN(y) -> expN(logN(C)*x+y) if C > 0. */
4075 exps (EXP EXP2 EXP10 POW10)
4076 logs (LOG LOG2 LOG10 LOG10)
4078 (mult:c (pows:s REAL_CST@0 @1) (exps:s @2))
4079 (if (real_compare (GT_EXPR, TREE_REAL_CST_PTR (@0), &dconst0)
4080 && real_isfinite (TREE_REAL_CST_PTR (@0)))
4081 (exps (plus (mult (logs @0) @1) @2)))))
4086 exps (EXP EXP2 EXP10 POW10)
4087 /* sqrt(expN(x)) -> expN(x*0.5). */
4090 (exps (mult @0 { build_real (type, dconsthalf); })))
4091 /* cbrt(expN(x)) -> expN(x/3). */
4094 (exps (mult @0 { build_real_truncate (type, dconst_third ()); })))
4095 /* pow(expN(x), y) -> expN(x*y). */
4098 (exps (mult @0 @1))))
4100 /* tan(atan(x)) -> x. */
4107 /* cabs(x+0i) or cabs(0+xi) -> abs(x). */
4109 (CABS (complex:C @0 real_zerop@1))
4112 /* trunc(trunc(x)) -> trunc(x), etc. */
4113 (for fns (TRUNC_ALL FLOOR_ALL CEIL_ALL ROUND_ALL NEARBYINT_ALL RINT_ALL)
4117 /* f(x) -> x if x is integer valued and f does nothing for such values. */
4118 (for fns (TRUNC_ALL FLOOR_ALL CEIL_ALL ROUND_ALL NEARBYINT_ALL RINT_ALL)
4120 (fns integer_valued_real_p@0)
4123 /* hypot(x,0) and hypot(0,x) -> abs(x). */
4125 (HYPOT:c @0 real_zerop@1)
4128 /* pow(1,x) -> 1. */
4130 (POW real_onep@0 @1)
4134 /* copysign(x,x) -> x. */
4135 (COPYSIGN_ALL @0 @0)
4139 /* copysign(x,y) -> fabs(x) if y is nonnegative. */
4140 (COPYSIGN_ALL @0 tree_expr_nonnegative_p@1)
4143 (for scale (LDEXP SCALBN SCALBLN)
4144 /* ldexp(0, x) -> 0. */
4146 (scale real_zerop@0 @1)
4148 /* ldexp(x, 0) -> x. */
4150 (scale @0 integer_zerop@1)
4152 /* ldexp(x, y) -> x if x is +-Inf or NaN. */
4154 (scale REAL_CST@0 @1)
4155 (if (!real_isfinite (TREE_REAL_CST_PTR (@0)))
4158 /* Canonicalization of sequences of math builtins. These rules represent
4159 IL simplifications but are not necessarily optimizations.
4161 The sincos pass is responsible for picking "optimal" implementations
4162 of math builtins, which may be more complicated and can sometimes go
4163 the other way, e.g. converting pow into a sequence of sqrts.
4164 We only want to do these canonicalizations before the pass has run. */
4166 (if (flag_unsafe_math_optimizations && canonicalize_math_p ())
4167 /* Simplify tan(x) * cos(x) -> sin(x). */
4169 (mult:c (TAN:s @0) (COS:s @0))
4172 /* Simplify x * pow(x,c) -> pow(x,c+1). */
4174 (mult:c @0 (POW:s @0 REAL_CST@1))
4175 (if (!TREE_OVERFLOW (@1))
4176 (POW @0 (plus @1 { build_one_cst (type); }))))
4178 /* Simplify sin(x) / cos(x) -> tan(x). */
4180 (rdiv (SIN:s @0) (COS:s @0))
4183 /* Simplify cos(x) / sin(x) -> 1 / tan(x). */
4185 (rdiv (COS:s @0) (SIN:s @0))
4186 (rdiv { build_one_cst (type); } (TAN @0)))
4188 /* Simplify sin(x) / tan(x) -> cos(x). */
4190 (rdiv (SIN:s @0) (TAN:s @0))
4191 (if (! HONOR_NANS (@0)
4192 && ! HONOR_INFINITIES (@0))
4195 /* Simplify tan(x) / sin(x) -> 1.0 / cos(x). */
4197 (rdiv (TAN:s @0) (SIN:s @0))
4198 (if (! HONOR_NANS (@0)
4199 && ! HONOR_INFINITIES (@0))
4200 (rdiv { build_one_cst (type); } (COS @0))))
4202 /* Simplify pow(x,y) * pow(x,z) -> pow(x,y+z). */
4204 (mult (POW:s @0 @1) (POW:s @0 @2))
4205 (POW @0 (plus @1 @2)))
4207 /* Simplify pow(x,y) * pow(z,y) -> pow(x*z,y). */
4209 (mult (POW:s @0 @1) (POW:s @2 @1))
4210 (POW (mult @0 @2) @1))
4212 /* Simplify powi(x,y) * powi(z,y) -> powi(x*z,y). */
4214 (mult (POWI:s @0 @1) (POWI:s @2 @1))
4215 (POWI (mult @0 @2) @1))
4217 /* Simplify pow(x,c) / x -> pow(x,c-1). */
4219 (rdiv (POW:s @0 REAL_CST@1) @0)
4220 (if (!TREE_OVERFLOW (@1))
4221 (POW @0 (minus @1 { build_one_cst (type); }))))
4223 /* Simplify x / pow (y,z) -> x * pow(y,-z). */
4225 (rdiv @0 (POW:s @1 @2))
4226 (mult @0 (POW @1 (negate @2))))
4231 /* sqrt(sqrt(x)) -> pow(x,1/4). */
4234 (pows @0 { build_real (type, dconst_quarter ()); }))
4235 /* sqrt(cbrt(x)) -> pow(x,1/6). */
4238 (pows @0 { build_real_truncate (type, dconst_sixth ()); }))
4239 /* cbrt(sqrt(x)) -> pow(x,1/6). */
4242 (pows @0 { build_real_truncate (type, dconst_sixth ()); }))
4243 /* cbrt(cbrt(x)) -> pow(x,1/9), iff x is nonnegative. */
4245 (cbrts (cbrts tree_expr_nonnegative_p@0))
4246 (pows @0 { build_real_truncate (type, dconst_ninth ()); }))
4247 /* sqrt(pow(x,y)) -> pow(|x|,y*0.5). */
4249 (sqrts (pows @0 @1))
4250 (pows (abs @0) (mult @1 { build_real (type, dconsthalf); })))
4251 /* cbrt(pow(x,y)) -> pow(x,y/3), iff x is nonnegative. */
4253 (cbrts (pows tree_expr_nonnegative_p@0 @1))
4254 (pows @0 (mult @1 { build_real_truncate (type, dconst_third ()); })))
4255 /* pow(sqrt(x),y) -> pow(x,y*0.5). */
4257 (pows (sqrts @0) @1)
4258 (pows @0 (mult @1 { build_real (type, dconsthalf); })))
4259 /* pow(cbrt(x),y) -> pow(x,y/3) iff x is nonnegative. */
4261 (pows (cbrts tree_expr_nonnegative_p@0) @1)
4262 (pows @0 (mult @1 { build_real_truncate (type, dconst_third ()); })))
4263 /* pow(pow(x,y),z) -> pow(x,y*z) iff x is nonnegative. */
4265 (pows (pows tree_expr_nonnegative_p@0 @1) @2)
4266 (pows @0 (mult @1 @2))))
4268 /* cabs(x+xi) -> fabs(x)*sqrt(2). */
4270 (CABS (complex @0 @0))
4271 (mult (abs @0) { build_real_truncate (type, dconst_sqrt2 ()); }))
4273 /* hypot(x,x) -> fabs(x)*sqrt(2). */
4276 (mult (abs @0) { build_real_truncate (type, dconst_sqrt2 ()); }))
4278 /* cexp(x+yi) -> exp(x)*cexpi(y). */
4283 (cexps compositional_complex@0)
4284 (if (targetm.libc_has_function (function_c99_math_complex))
4286 (mult (exps@1 (realpart @0)) (realpart (cexpis:type@2 (imagpart @0))))
4287 (mult @1 (imagpart @2)))))))
4289 (if (canonicalize_math_p ())
4290 /* floor(x) -> trunc(x) if x is nonnegative. */
4291 (for floors (FLOOR_ALL)
4294 (floors tree_expr_nonnegative_p@0)
4297 (match double_value_p
4299 (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == double_type_node)))
4300 (for froms (BUILT_IN_TRUNCL
4312 /* truncl(extend(x)) -> extend(trunc(x)), etc., if x is a double. */
4313 (if (optimize && canonicalize_math_p ())
4315 (froms (convert double_value_p@0))
4316 (convert (tos @0)))))
4318 (match float_value_p
4320 (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == float_type_node)))
4321 (for froms (BUILT_IN_TRUNCL BUILT_IN_TRUNC
4322 BUILT_IN_FLOORL BUILT_IN_FLOOR
4323 BUILT_IN_CEILL BUILT_IN_CEIL
4324 BUILT_IN_ROUNDL BUILT_IN_ROUND
4325 BUILT_IN_NEARBYINTL BUILT_IN_NEARBYINT
4326 BUILT_IN_RINTL BUILT_IN_RINT)
4327 tos (BUILT_IN_TRUNCF BUILT_IN_TRUNCF
4328 BUILT_IN_FLOORF BUILT_IN_FLOORF
4329 BUILT_IN_CEILF BUILT_IN_CEILF
4330 BUILT_IN_ROUNDF BUILT_IN_ROUNDF
4331 BUILT_IN_NEARBYINTF BUILT_IN_NEARBYINTF
4332 BUILT_IN_RINTF BUILT_IN_RINTF)
4333 /* truncl(extend(x)) and trunc(extend(x)) -> extend(truncf(x)), etc.,
4335 (if (optimize && canonicalize_math_p ()
4336 && targetm.libc_has_function (function_c99_misc))
4338 (froms (convert float_value_p@0))
4339 (convert (tos @0)))))
4341 (for froms (XFLOORL XCEILL XROUNDL XRINTL)
4342 tos (XFLOOR XCEIL XROUND XRINT)
4343 /* llfloorl(extend(x)) -> llfloor(x), etc., if x is a double. */
4344 (if (optimize && canonicalize_math_p ())
4346 (froms (convert double_value_p@0))
4349 (for froms (XFLOORL XCEILL XROUNDL XRINTL
4350 XFLOOR XCEIL XROUND XRINT)
4351 tos (XFLOORF XCEILF XROUNDF XRINTF)
4352 /* llfloorl(extend(x)) and llfloor(extend(x)) -> llfloorf(x), etc.,
4354 (if (optimize && canonicalize_math_p ())
4356 (froms (convert float_value_p@0))
4359 (if (canonicalize_math_p ())
4360 /* xfloor(x) -> fix_trunc(x) if x is nonnegative. */
4361 (for floors (IFLOOR LFLOOR LLFLOOR)
4363 (floors tree_expr_nonnegative_p@0)
4366 (if (canonicalize_math_p ())
4367 /* xfloor(x) -> fix_trunc(x), etc., if x is integer valued. */
4368 (for fns (IFLOOR LFLOOR LLFLOOR
4370 IROUND LROUND LLROUND)
4372 (fns integer_valued_real_p@0)
4374 (if (!flag_errno_math)
4375 /* xrint(x) -> fix_trunc(x), etc., if x is integer valued. */
4376 (for rints (IRINT LRINT LLRINT)
4378 (rints integer_valued_real_p@0)
4381 (if (canonicalize_math_p ())
4382 (for ifn (IFLOOR ICEIL IROUND IRINT)
4383 lfn (LFLOOR LCEIL LROUND LRINT)
4384 llfn (LLFLOOR LLCEIL LLROUND LLRINT)
4385 /* Canonicalize iround (x) to lround (x) on ILP32 targets where
4386 sizeof (int) == sizeof (long). */
4387 (if (TYPE_PRECISION (integer_type_node)
4388 == TYPE_PRECISION (long_integer_type_node))
4391 (lfn:long_integer_type_node @0)))
4392 /* Canonicalize llround (x) to lround (x) on LP64 targets where
4393 sizeof (long long) == sizeof (long). */
4394 (if (TYPE_PRECISION (long_long_integer_type_node)
4395 == TYPE_PRECISION (long_integer_type_node))
4398 (lfn:long_integer_type_node @0)))))
4400 /* cproj(x) -> x if we're ignoring infinities. */
4403 (if (!HONOR_INFINITIES (type))
4406 /* If the real part is inf and the imag part is known to be
4407 nonnegative, return (inf + 0i). */
4409 (CPROJ (complex REAL_CST@0 tree_expr_nonnegative_p@1))
4410 (if (real_isinf (TREE_REAL_CST_PTR (@0)))
4411 { build_complex_inf (type, false); }))
4413 /* If the imag part is inf, return (inf+I*copysign(0,imag)). */
4415 (CPROJ (complex @0 REAL_CST@1))
4416 (if (real_isinf (TREE_REAL_CST_PTR (@1)))
4417 { build_complex_inf (type, TREE_REAL_CST_PTR (@1)->sign); }))
4423 (pows @0 REAL_CST@1)
4425 const REAL_VALUE_TYPE *value = TREE_REAL_CST_PTR (@1);
4426 REAL_VALUE_TYPE tmp;
4429 /* pow(x,0) -> 1. */
4430 (if (real_equal (value, &dconst0))
4431 { build_real (type, dconst1); })
4432 /* pow(x,1) -> x. */
4433 (if (real_equal (value, &dconst1))
4435 /* pow(x,-1) -> 1/x. */
4436 (if (real_equal (value, &dconstm1))
4437 (rdiv { build_real (type, dconst1); } @0))
4438 /* pow(x,0.5) -> sqrt(x). */
4439 (if (flag_unsafe_math_optimizations
4440 && canonicalize_math_p ()
4441 && real_equal (value, &dconsthalf))
4443 /* pow(x,1/3) -> cbrt(x). */
4444 (if (flag_unsafe_math_optimizations
4445 && canonicalize_math_p ()
4446 && (tmp = real_value_truncate (TYPE_MODE (type), dconst_third ()),
4447 real_equal (value, &tmp)))
4450 /* powi(1,x) -> 1. */
4452 (POWI real_onep@0 @1)
4456 (POWI @0 INTEGER_CST@1)
4458 /* powi(x,0) -> 1. */
4459 (if (wi::to_wide (@1) == 0)
4460 { build_real (type, dconst1); })
4461 /* powi(x,1) -> x. */
4462 (if (wi::to_wide (@1) == 1)
4464 /* powi(x,-1) -> 1/x. */
4465 (if (wi::to_wide (@1) == -1)
4466 (rdiv { build_real (type, dconst1); } @0))))
4468 /* Narrowing of arithmetic and logical operations.
4470 These are conceptually similar to the transformations performed for
4471 the C/C++ front-ends by shorten_binary_op and shorten_compare. Long
4472 term we want to move all that code out of the front-ends into here. */
4474 /* If we have a narrowing conversion of an arithmetic operation where
4475 both operands are widening conversions from the same type as the outer
4476 narrowing conversion. Then convert the innermost operands to a suitable
4477 unsigned type (to avoid introducing undefined behavior), perform the
4478 operation and convert the result to the desired type. */
4479 (for op (plus minus)
4481 (convert (op:s (convert@2 @0) (convert?@3 @1)))
4482 (if (INTEGRAL_TYPE_P (type)
4483 /* We check for type compatibility between @0 and @1 below,
4484 so there's no need to check that @1/@3 are integral types. */
4485 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
4486 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
4487 /* The precision of the type of each operand must match the
4488 precision of the mode of each operand, similarly for the
4490 && type_has_mode_precision_p (TREE_TYPE (@0))
4491 && type_has_mode_precision_p (TREE_TYPE (@1))
4492 && type_has_mode_precision_p (type)
4493 /* The inner conversion must be a widening conversion. */
4494 && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
4495 && types_match (@0, type)
4496 && (types_match (@0, @1)
4497 /* Or the second operand is const integer or converted const
4498 integer from valueize. */
4499 || TREE_CODE (@1) == INTEGER_CST))
4500 (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
4501 (op @0 (convert @1))
4502 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
4503 (convert (op (convert:utype @0)
4504 (convert:utype @1))))))))
4506 /* This is another case of narrowing, specifically when there's an outer
4507 BIT_AND_EXPR which masks off bits outside the type of the innermost
4508 operands. Like the previous case we have to convert the operands
4509 to unsigned types to avoid introducing undefined behavior for the
4510 arithmetic operation. */
4511 (for op (minus plus)
4513 (bit_and (op:s (convert@2 @0) (convert@3 @1)) INTEGER_CST@4)
4514 (if (INTEGRAL_TYPE_P (type)
4515 /* We check for type compatibility between @0 and @1 below,
4516 so there's no need to check that @1/@3 are integral types. */
4517 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
4518 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
4519 /* The precision of the type of each operand must match the
4520 precision of the mode of each operand, similarly for the
4522 && type_has_mode_precision_p (TREE_TYPE (@0))
4523 && type_has_mode_precision_p (TREE_TYPE (@1))
4524 && type_has_mode_precision_p (type)
4525 /* The inner conversion must be a widening conversion. */
4526 && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
4527 && types_match (@0, @1)
4528 && (tree_int_cst_min_precision (@4, TYPE_SIGN (TREE_TYPE (@0)))
4529 <= TYPE_PRECISION (TREE_TYPE (@0)))
4530 && (wi::to_wide (@4)
4531 & wi::mask (TYPE_PRECISION (TREE_TYPE (@0)),
4532 true, TYPE_PRECISION (type))) == 0)
4533 (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
4534 (with { tree ntype = TREE_TYPE (@0); }
4535 (convert (bit_and (op @0 @1) (convert:ntype @4))))
4536 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
4537 (convert (bit_and (op (convert:utype @0) (convert:utype @1))
4538 (convert:utype @4))))))))
4540 /* Transform (@0 < @1 and @0 < @2) to use min,
4541 (@0 > @1 and @0 > @2) to use max */
4542 (for logic (bit_and bit_and bit_and bit_and bit_ior bit_ior bit_ior bit_ior)
4543 op (lt le gt ge lt le gt ge )
4544 ext (min min max max max max min min )
4546 (logic (op:cs @0 @1) (op:cs @0 @2))
4547 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
4548 && TREE_CODE (@0) != INTEGER_CST)
4549 (op @0 (ext @1 @2)))))
4552 /* signbit(x) -> 0 if x is nonnegative. */
4553 (SIGNBIT tree_expr_nonnegative_p@0)
4554 { integer_zero_node; })
4557 /* signbit(x) -> x<0 if x doesn't have signed zeros. */
4559 (if (!HONOR_SIGNED_ZEROS (@0))
4560 (convert (lt @0 { build_real (TREE_TYPE (@0), dconst0); }))))
4562 /* Transform comparisons of the form X +- C1 CMP C2 to X CMP C2 -+ C1. */
4564 (for op (plus minus)
4567 (cmp (op@3 @0 INTEGER_CST@1) INTEGER_CST@2)
4568 (if (!TREE_OVERFLOW (@1) && !TREE_OVERFLOW (@2)
4569 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0))
4570 && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@0))
4571 && !TYPE_SATURATING (TREE_TYPE (@0)))
4572 (with { tree res = int_const_binop (rop, @2, @1); }
4573 (if (TREE_OVERFLOW (res)
4574 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
4575 { constant_boolean_node (cmp == NE_EXPR, type); }
4576 (if (single_use (@3))
4577 (cmp @0 { TREE_OVERFLOW (res)
4578 ? drop_tree_overflow (res) : res; }))))))))
4579 (for cmp (lt le gt ge)
4580 (for op (plus minus)
4583 (cmp (op@3 @0 INTEGER_CST@1) INTEGER_CST@2)
4584 (if (!TREE_OVERFLOW (@1) && !TREE_OVERFLOW (@2)
4585 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
4586 (with { tree res = int_const_binop (rop, @2, @1); }
4587 (if (TREE_OVERFLOW (res))
4589 fold_overflow_warning (("assuming signed overflow does not occur "
4590 "when simplifying conditional to constant"),
4591 WARN_STRICT_OVERFLOW_CONDITIONAL);
4592 bool less = cmp == LE_EXPR || cmp == LT_EXPR;
4593 /* wi::ges_p (@2, 0) should be sufficient for a signed type. */
4594 bool ovf_high = wi::lt_p (wi::to_wide (@1), 0,
4595 TYPE_SIGN (TREE_TYPE (@1)))
4596 != (op == MINUS_EXPR);
4597 constant_boolean_node (less == ovf_high, type);
4599 (if (single_use (@3))
4602 fold_overflow_warning (("assuming signed overflow does not occur "
4603 "when changing X +- C1 cmp C2 to "
4605 WARN_STRICT_OVERFLOW_COMPARISON);
4607 (cmp @0 { res; })))))))))
4609 /* Canonicalizations of BIT_FIELD_REFs. */
4612 (BIT_FIELD_REF @0 @1 @2)
4614 (if (TREE_CODE (TREE_TYPE (@0)) == COMPLEX_TYPE
4615 && tree_int_cst_equal (@1, TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))))
4617 (if (integer_zerop (@2))
4618 (view_convert (realpart @0)))
4619 (if (tree_int_cst_equal (@2, TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))))
4620 (view_convert (imagpart @0)))))
4621 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
4622 && INTEGRAL_TYPE_P (type)
4623 /* On GIMPLE this should only apply to register arguments. */
4624 && (! GIMPLE || is_gimple_reg (@0))
4625 /* A bit-field-ref that referenced the full argument can be stripped. */
4626 && ((compare_tree_int (@1, TYPE_PRECISION (TREE_TYPE (@0))) == 0
4627 && integer_zerop (@2))
4628 /* Low-parts can be reduced to integral conversions.
4629 ??? The following doesn't work for PDP endian. */
4630 || (BYTES_BIG_ENDIAN == WORDS_BIG_ENDIAN
4631 /* Don't even think about BITS_BIG_ENDIAN. */
4632 && TYPE_PRECISION (TREE_TYPE (@0)) % BITS_PER_UNIT == 0
4633 && TYPE_PRECISION (type) % BITS_PER_UNIT == 0
4634 && compare_tree_int (@2, (BYTES_BIG_ENDIAN
4635 ? (TYPE_PRECISION (TREE_TYPE (@0))
4636 - TYPE_PRECISION (type))
4640 /* Simplify vector extracts. */
4643 (BIT_FIELD_REF CONSTRUCTOR@0 @1 @2)
4644 (if (VECTOR_TYPE_P (TREE_TYPE (@0))
4645 && (types_match (type, TREE_TYPE (TREE_TYPE (@0)))
4646 || (VECTOR_TYPE_P (type)
4647 && types_match (TREE_TYPE (type), TREE_TYPE (TREE_TYPE (@0))))))
4650 tree ctor = (TREE_CODE (@0) == SSA_NAME
4651 ? gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0)) : @0);
4652 tree eltype = TREE_TYPE (TREE_TYPE (ctor));
4653 unsigned HOST_WIDE_INT width = tree_to_uhwi (TYPE_SIZE (eltype));
4654 unsigned HOST_WIDE_INT n = tree_to_uhwi (@1);
4655 unsigned HOST_WIDE_INT idx = tree_to_uhwi (@2);
4658 && (idx % width) == 0
4660 && known_le ((idx + n) / width,
4661 TYPE_VECTOR_SUBPARTS (TREE_TYPE (ctor))))
4666 /* Constructor elements can be subvectors. */
4668 if (CONSTRUCTOR_NELTS (ctor) != 0)
4670 tree cons_elem = TREE_TYPE (CONSTRUCTOR_ELT (ctor, 0)->value);
4671 if (TREE_CODE (cons_elem) == VECTOR_TYPE)
4672 k = TYPE_VECTOR_SUBPARTS (cons_elem);
4674 unsigned HOST_WIDE_INT elt, count, const_k;
4677 /* We keep an exact subset of the constructor elements. */
4678 (if (multiple_p (idx, k, &elt) && multiple_p (n, k, &count))
4679 (if (CONSTRUCTOR_NELTS (ctor) == 0)
4680 { build_constructor (type, NULL); }
4682 (if (elt < CONSTRUCTOR_NELTS (ctor))
4683 (view_convert { CONSTRUCTOR_ELT (ctor, elt)->value; })
4684 { build_zero_cst (type); })
4686 vec<constructor_elt, va_gc> *vals;
4687 vec_alloc (vals, count);
4688 for (unsigned i = 0;
4689 i < count && elt + i < CONSTRUCTOR_NELTS (ctor); ++i)
4690 CONSTRUCTOR_APPEND_ELT (vals, NULL_TREE,
4691 CONSTRUCTOR_ELT (ctor, elt + i)->value);
4692 build_constructor (type, vals);
4694 /* The bitfield references a single constructor element. */
4695 (if (k.is_constant (&const_k)
4696 && idx + n <= (idx / const_k + 1) * const_k)
4698 (if (CONSTRUCTOR_NELTS (ctor) <= idx / const_k)
4699 { build_zero_cst (type); })
4701 (view_convert { CONSTRUCTOR_ELT (ctor, idx / const_k)->value; }))
4702 (BIT_FIELD_REF { CONSTRUCTOR_ELT (ctor, idx / const_k)->value; }
4703 @1 { bitsize_int ((idx % const_k) * width); })))))))))
4705 /* Simplify a bit extraction from a bit insertion for the cases with
4706 the inserted element fully covering the extraction or the insertion
4707 not touching the extraction. */
4709 (BIT_FIELD_REF (bit_insert @0 @1 @ipos) @rsize @rpos)
4712 unsigned HOST_WIDE_INT isize;
4713 if (INTEGRAL_TYPE_P (TREE_TYPE (@1)))
4714 isize = TYPE_PRECISION (TREE_TYPE (@1));
4716 isize = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (@1)));
4719 (if (wi::leu_p (wi::to_wide (@ipos), wi::to_wide (@rpos))
4720 && wi::leu_p (wi::to_wide (@rpos) + wi::to_wide (@rsize),
4721 wi::to_wide (@ipos) + isize))
4722 (BIT_FIELD_REF @1 @rsize { wide_int_to_tree (bitsizetype,
4724 - wi::to_wide (@ipos)); }))
4725 (if (wi::geu_p (wi::to_wide (@ipos),
4726 wi::to_wide (@rpos) + wi::to_wide (@rsize))
4727 || wi::geu_p (wi::to_wide (@rpos),
4728 wi::to_wide (@ipos) + isize))
4729 (BIT_FIELD_REF @0 @rsize @rpos)))))
4731 (if (canonicalize_math_after_vectorization_p ())
4734 (fmas:c (negate @0) @1 @2)
4735 (IFN_FNMA @0 @1 @2))
4737 (fmas @0 @1 (negate @2))
4740 (fmas:c (negate @0) @1 (negate @2))
4741 (IFN_FNMS @0 @1 @2))
4743 (negate (fmas@3 @0 @1 @2))
4744 (if (single_use (@3))
4745 (IFN_FNMS @0 @1 @2))))
4748 (IFN_FMS:c (negate @0) @1 @2)
4749 (IFN_FNMS @0 @1 @2))
4751 (IFN_FMS @0 @1 (negate @2))
4754 (IFN_FMS:c (negate @0) @1 (negate @2))
4755 (IFN_FNMA @0 @1 @2))
4757 (negate (IFN_FMS@3 @0 @1 @2))
4758 (if (single_use (@3))
4759 (IFN_FNMA @0 @1 @2)))
4762 (IFN_FNMA:c (negate @0) @1 @2)
4765 (IFN_FNMA @0 @1 (negate @2))
4766 (IFN_FNMS @0 @1 @2))
4768 (IFN_FNMA:c (negate @0) @1 (negate @2))
4771 (negate (IFN_FNMA@3 @0 @1 @2))
4772 (if (single_use (@3))
4773 (IFN_FMS @0 @1 @2)))
4776 (IFN_FNMS:c (negate @0) @1 @2)
4779 (IFN_FNMS @0 @1 (negate @2))
4780 (IFN_FNMA @0 @1 @2))
4782 (IFN_FNMS:c (negate @0) @1 (negate @2))
4785 (negate (IFN_FNMS@3 @0 @1 @2))
4786 (if (single_use (@3))
4787 (IFN_FMA @0 @1 @2))))
4789 /* POPCOUNT simplifications. */
4790 (for popcount (BUILT_IN_POPCOUNT BUILT_IN_POPCOUNTL BUILT_IN_POPCOUNTLL
4791 BUILT_IN_POPCOUNTIMAX)
4792 /* popcount(X&1) is nop_expr(X&1). */
4795 (if (tree_nonzero_bits (@0) == 1)
4797 /* popcount(X) + popcount(Y) is popcount(X|Y) when X&Y must be zero. */
4799 (plus (popcount:s @0) (popcount:s @1))
4800 (if (wi::bit_and (tree_nonzero_bits (@0), tree_nonzero_bits (@1)) == 0)
4801 (popcount (bit_ior @0 @1))))
4802 /* popcount(X) == 0 is X == 0, and related (in)equalities. */
4803 (for cmp (le eq ne gt)
4806 (cmp (popcount @0) integer_zerop)
4807 (rep @0 { build_zero_cst (TREE_TYPE (@0)); }))))
4816 r = c ? a1 op a2 : b;
4818 if the target can do it in one go. This makes the operation conditional
4819 on c, so could drop potentially-trapping arithmetic, but that's a valid
4820 simplification if the result of the operation isn't needed. */
4821 (for uncond_op (UNCOND_BINARY)
4822 cond_op (COND_BINARY)
4824 (vec_cond @0 (view_convert? (uncond_op@4 @1 @2)) @3)
4825 (with { tree op_type = TREE_TYPE (@4); }
4826 (if (element_precision (type) == element_precision (op_type))
4827 (view_convert (cond_op @0 @1 @2 (view_convert:op_type @3))))))
4829 (vec_cond @0 @1 (view_convert? (uncond_op@4 @2 @3)))
4830 (with { tree op_type = TREE_TYPE (@4); }
4831 (if (element_precision (type) == element_precision (op_type))
4832 (view_convert (cond_op (bit_not @0) @2 @3 (view_convert:op_type @1)))))))