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-2019 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
32 initializer_each_zero_or_onep
34 tree_expr_nonnegative_p
42 (define_operator_list tcc_comparison
43 lt le eq ne ge gt unordered ordered unlt unle ungt unge uneq ltgt)
44 (define_operator_list inverted_tcc_comparison
45 ge gt ne eq lt le ordered unordered ge gt le lt ltgt uneq)
46 (define_operator_list inverted_tcc_comparison_with_nans
47 unge ungt ne eq unlt unle ordered unordered ge gt le lt ltgt uneq)
48 (define_operator_list swapped_tcc_comparison
49 gt ge eq ne le lt unordered ordered ungt unge unlt unle uneq ltgt)
50 (define_operator_list simple_comparison lt le eq ne ge gt)
51 (define_operator_list swapped_simple_comparison gt ge eq ne le lt)
53 #include "cfn-operators.pd"
55 /* Define operand lists for math rounding functions {,i,l,ll}FN,
56 where the versions prefixed with "i" return an int, those prefixed with
57 "l" return a long and those prefixed with "ll" return a long long.
59 Also define operand lists:
61 X<FN>F for all float functions, in the order i, l, ll
62 X<FN> for all double functions, in the same order
63 X<FN>L for all long double functions, in the same order. */
64 #define DEFINE_INT_AND_FLOAT_ROUND_FN(FN) \
65 (define_operator_list X##FN##F BUILT_IN_I##FN##F \
68 (define_operator_list X##FN BUILT_IN_I##FN \
71 (define_operator_list X##FN##L BUILT_IN_I##FN##L \
75 DEFINE_INT_AND_FLOAT_ROUND_FN (FLOOR)
76 DEFINE_INT_AND_FLOAT_ROUND_FN (CEIL)
77 DEFINE_INT_AND_FLOAT_ROUND_FN (ROUND)
78 DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
80 /* Binary operations and their associated IFN_COND_* function. */
81 (define_operator_list UNCOND_BINARY
83 mult trunc_div trunc_mod rdiv
85 bit_and bit_ior bit_xor)
86 (define_operator_list COND_BINARY
87 IFN_COND_ADD IFN_COND_SUB
88 IFN_COND_MUL IFN_COND_DIV IFN_COND_MOD IFN_COND_RDIV
89 IFN_COND_MIN IFN_COND_MAX
90 IFN_COND_AND IFN_COND_IOR IFN_COND_XOR)
92 /* Same for ternary operations. */
93 (define_operator_list UNCOND_TERNARY
94 IFN_FMA IFN_FMS IFN_FNMA IFN_FNMS)
95 (define_operator_list COND_TERNARY
96 IFN_COND_FMA IFN_COND_FMS IFN_COND_FNMA IFN_COND_FNMS)
98 /* As opposed to convert?, this still creates a single pattern, so
99 it is not a suitable replacement for convert? in all cases. */
100 (match (nop_convert @0)
102 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))))
103 (match (nop_convert @0)
105 (if (VECTOR_TYPE_P (type) && VECTOR_TYPE_P (TREE_TYPE (@0))
106 && known_eq (TYPE_VECTOR_SUBPARTS (type),
107 TYPE_VECTOR_SUBPARTS (TREE_TYPE (@0)))
108 && tree_nop_conversion_p (TREE_TYPE (type), TREE_TYPE (TREE_TYPE (@0))))))
109 /* This one has to be last, or it shadows the others. */
110 (match (nop_convert @0)
113 /* Transform likes of (char) ABS_EXPR <(int) x> into (char) ABSU_EXPR <x>
114 ABSU_EXPR returns unsigned absolute value of the operand and the operand
115 of the ABSU_EXPR will have the corresponding signed type. */
116 (simplify (abs (convert @0))
117 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
118 && !TYPE_UNSIGNED (TREE_TYPE (@0))
119 && element_precision (type) > element_precision (TREE_TYPE (@0)))
120 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
121 (convert (absu:utype @0)))))
124 /* Simplifications of operations with one constant operand and
125 simplifications to constants or single values. */
127 (for op (plus pointer_plus minus bit_ior bit_xor)
129 (op @0 integer_zerop)
132 /* 0 +p index -> (type)index */
134 (pointer_plus integer_zerop @1)
135 (non_lvalue (convert @1)))
137 /* ptr - 0 -> (type)ptr */
139 (pointer_diff @0 integer_zerop)
142 /* See if ARG1 is zero and X + ARG1 reduces to X.
143 Likewise if the operands are reversed. */
145 (plus:c @0 real_zerop@1)
146 (if (fold_real_zero_addition_p (type, @1, 0))
149 /* See if ARG1 is zero and X - ARG1 reduces to X. */
151 (minus @0 real_zerop@1)
152 (if (fold_real_zero_addition_p (type, @1, 1))
156 This is unsafe for certain floats even in non-IEEE formats.
157 In IEEE, it is unsafe because it does wrong for NaNs.
158 Also note that operand_equal_p is always false if an operand
162 (if (!FLOAT_TYPE_P (type) || !HONOR_NANS (type))
163 { build_zero_cst (type); }))
165 (pointer_diff @@0 @0)
166 { build_zero_cst (type); })
169 (mult @0 integer_zerop@1)
172 /* Maybe fold x * 0 to 0. The expressions aren't the same
173 when x is NaN, since x * 0 is also NaN. Nor are they the
174 same in modes with signed zeros, since multiplying a
175 negative value by 0 gives -0, not +0. */
177 (mult @0 real_zerop@1)
178 (if (!HONOR_NANS (type) && !HONOR_SIGNED_ZEROS (type))
181 /* In IEEE floating point, x*1 is not equivalent to x for snans.
182 Likewise for complex arithmetic with signed zeros. */
185 (if (!HONOR_SNANS (type)
186 && (!HONOR_SIGNED_ZEROS (type)
187 || !COMPLEX_FLOAT_TYPE_P (type)))
190 /* Transform x * -1.0 into -x. */
192 (mult @0 real_minus_onep)
193 (if (!HONOR_SNANS (type)
194 && (!HONOR_SIGNED_ZEROS (type)
195 || !COMPLEX_FLOAT_TYPE_P (type)))
198 /* Transform x * { 0 or 1, 0 or 1, ... } into x & { 0 or -1, 0 or -1, ...},
199 unless the target has native support for the former but not the latter. */
201 (mult @0 VECTOR_CST@1)
202 (if (initializer_each_zero_or_onep (@1)
203 && !HONOR_SNANS (type)
204 && !HONOR_SIGNED_ZEROS (type))
205 (with { tree itype = FLOAT_TYPE_P (type) ? unsigned_type_for (type) : type; }
207 && (!VECTOR_MODE_P (TYPE_MODE (type))
208 || (VECTOR_MODE_P (TYPE_MODE (itype))
209 && optab_handler (and_optab,
210 TYPE_MODE (itype)) != CODE_FOR_nothing)))
211 (view_convert (bit_and:itype (view_convert @0)
212 (ne @1 { build_zero_cst (type); })))))))
214 (for cmp (gt ge lt le)
215 outp (convert convert negate negate)
216 outn (negate negate convert convert)
217 /* Transform (X > 0.0 ? 1.0 : -1.0) into copysign(1, X). */
218 /* Transform (X >= 0.0 ? 1.0 : -1.0) into copysign(1, X). */
219 /* Transform (X < 0.0 ? 1.0 : -1.0) into copysign(1,-X). */
220 /* Transform (X <= 0.0 ? 1.0 : -1.0) into copysign(1,-X). */
222 (cond (cmp @0 real_zerop) real_onep@1 real_minus_onep)
223 (if (!HONOR_NANS (type) && !HONOR_SIGNED_ZEROS (type)
224 && types_match (type, TREE_TYPE (@0)))
226 (if (types_match (type, float_type_node))
227 (BUILT_IN_COPYSIGNF @1 (outp @0)))
228 (if (types_match (type, double_type_node))
229 (BUILT_IN_COPYSIGN @1 (outp @0)))
230 (if (types_match (type, long_double_type_node))
231 (BUILT_IN_COPYSIGNL @1 (outp @0))))))
232 /* Transform (X > 0.0 ? -1.0 : 1.0) into copysign(1,-X). */
233 /* Transform (X >= 0.0 ? -1.0 : 1.0) into copysign(1,-X). */
234 /* Transform (X < 0.0 ? -1.0 : 1.0) into copysign(1,X). */
235 /* Transform (X <= 0.0 ? -1.0 : 1.0) into copysign(1,X). */
237 (cond (cmp @0 real_zerop) real_minus_onep real_onep@1)
238 (if (!HONOR_NANS (type) && !HONOR_SIGNED_ZEROS (type)
239 && types_match (type, TREE_TYPE (@0)))
241 (if (types_match (type, float_type_node))
242 (BUILT_IN_COPYSIGNF @1 (outn @0)))
243 (if (types_match (type, double_type_node))
244 (BUILT_IN_COPYSIGN @1 (outn @0)))
245 (if (types_match (type, long_double_type_node))
246 (BUILT_IN_COPYSIGNL @1 (outn @0)))))))
248 /* Transform X * copysign (1.0, X) into abs(X). */
250 (mult:c @0 (COPYSIGN_ALL real_onep @0))
251 (if (!HONOR_NANS (type) && !HONOR_SIGNED_ZEROS (type))
254 /* Transform X * copysign (1.0, -X) into -abs(X). */
256 (mult:c @0 (COPYSIGN_ALL real_onep (negate @0)))
257 (if (!HONOR_NANS (type) && !HONOR_SIGNED_ZEROS (type))
260 /* Transform copysign (CST, X) into copysign (ABS(CST), X). */
262 (COPYSIGN_ALL REAL_CST@0 @1)
263 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@0)))
264 (COPYSIGN_ALL (negate @0) @1)))
266 /* X * 1, X / 1 -> X. */
267 (for op (mult trunc_div ceil_div floor_div round_div exact_div)
272 /* (A / (1 << B)) -> (A >> B).
273 Only for unsigned A. For signed A, this would not preserve rounding
275 For example: (-1 / ( 1 << B)) != -1 >> B. */
277 (trunc_div @0 (lshift integer_onep@1 @2))
278 (if ((TYPE_UNSIGNED (type) || tree_expr_nonnegative_p (@0))
279 && (!VECTOR_TYPE_P (type)
280 || target_supports_op_p (type, RSHIFT_EXPR, optab_vector)
281 || target_supports_op_p (type, RSHIFT_EXPR, optab_scalar)))
284 /* Preserve explicit divisions by 0: the C++ front-end wants to detect
285 undefined behavior in constexpr evaluation, and assuming that the division
286 traps enables better optimizations than these anyway. */
287 (for div (trunc_div ceil_div floor_div round_div exact_div)
288 /* 0 / X is always zero. */
290 (div integer_zerop@0 @1)
291 /* But not for 0 / 0 so that we can get the proper warnings and errors. */
292 (if (!integer_zerop (@1))
296 (div @0 integer_minus_onep@1)
297 (if (!TYPE_UNSIGNED (type))
302 /* But not for 0 / 0 so that we can get the proper warnings and errors.
303 And not for _Fract types where we can't build 1. */
304 (if (!integer_zerop (@0) && !ALL_FRACT_MODE_P (TYPE_MODE (type)))
305 { build_one_cst (type); }))
306 /* X / abs (X) is X < 0 ? -1 : 1. */
309 (if (INTEGRAL_TYPE_P (type)
310 && TYPE_OVERFLOW_UNDEFINED (type))
311 (cond (lt @0 { build_zero_cst (type); })
312 { build_minus_one_cst (type); } { build_one_cst (type); })))
315 (div:C @0 (negate @0))
316 (if ((INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
317 && TYPE_OVERFLOW_UNDEFINED (type))
318 { build_minus_one_cst (type); })))
320 /* For unsigned integral types, FLOOR_DIV_EXPR is the same as
321 TRUNC_DIV_EXPR. Rewrite into the latter in this case. */
324 (if ((INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
325 && TYPE_UNSIGNED (type))
328 /* Combine two successive divisions. Note that combining ceil_div
329 and floor_div is trickier and combining round_div even more so. */
330 (for div (trunc_div exact_div)
332 (div (div@3 @0 INTEGER_CST@1) INTEGER_CST@2)
334 wi::overflow_type overflow;
335 wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@2),
336 TYPE_SIGN (type), &overflow);
338 (if (div == EXACT_DIV_EXPR
339 || optimize_successive_divisions_p (@2, @3))
341 (div @0 { wide_int_to_tree (type, mul); })
342 (if (TYPE_UNSIGNED (type)
343 || mul != wi::min_value (TYPE_PRECISION (type), SIGNED))
344 { build_zero_cst (type); }))))))
346 /* Combine successive multiplications. Similar to above, but handling
347 overflow is different. */
349 (mult (mult @0 INTEGER_CST@1) INTEGER_CST@2)
351 wi::overflow_type overflow;
352 wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@2),
353 TYPE_SIGN (type), &overflow);
355 /* Skip folding on overflow: the only special case is @1 * @2 == -INT_MIN,
356 otherwise undefined overflow implies that @0 must be zero. */
357 (if (!overflow || TYPE_OVERFLOW_WRAPS (type))
358 (mult @0 { wide_int_to_tree (type, mul); }))))
360 /* Optimize A / A to 1.0 if we don't care about
361 NaNs or Infinities. */
364 (if (FLOAT_TYPE_P (type)
365 && ! HONOR_NANS (type)
366 && ! HONOR_INFINITIES (type))
367 { build_one_cst (type); }))
369 /* Optimize -A / A to -1.0 if we don't care about
370 NaNs or Infinities. */
372 (rdiv:C @0 (negate @0))
373 (if (FLOAT_TYPE_P (type)
374 && ! HONOR_NANS (type)
375 && ! HONOR_INFINITIES (type))
376 { build_minus_one_cst (type); }))
378 /* PR71078: x / abs(x) -> copysign (1.0, x) */
380 (rdiv:C (convert? @0) (convert? (abs @0)))
381 (if (SCALAR_FLOAT_TYPE_P (type)
382 && ! HONOR_NANS (type)
383 && ! HONOR_INFINITIES (type))
385 (if (types_match (type, float_type_node))
386 (BUILT_IN_COPYSIGNF { build_one_cst (type); } (convert @0)))
387 (if (types_match (type, double_type_node))
388 (BUILT_IN_COPYSIGN { build_one_cst (type); } (convert @0)))
389 (if (types_match (type, long_double_type_node))
390 (BUILT_IN_COPYSIGNL { build_one_cst (type); } (convert @0))))))
392 /* In IEEE floating point, x/1 is not equivalent to x for snans. */
395 (if (!HONOR_SNANS (type))
398 /* In IEEE floating point, x/-1 is not equivalent to -x for snans. */
400 (rdiv @0 real_minus_onep)
401 (if (!HONOR_SNANS (type))
404 (if (flag_reciprocal_math)
405 /* Convert (A/B)/C to A/(B*C). */
407 (rdiv (rdiv:s @0 @1) @2)
408 (rdiv @0 (mult @1 @2)))
410 /* Canonicalize x / (C1 * y) to (x * C2) / y. */
412 (rdiv @0 (mult:s @1 REAL_CST@2))
414 { tree tem = const_binop (RDIV_EXPR, type, build_one_cst (type), @2); }
416 (rdiv (mult @0 { tem; } ) @1))))
418 /* Convert A/(B/C) to (A/B)*C */
420 (rdiv @0 (rdiv:s @1 @2))
421 (mult (rdiv @0 @1) @2)))
423 /* Simplify x / (- y) to -x / y. */
425 (rdiv @0 (negate @1))
426 (rdiv (negate @0) @1))
428 (if (flag_unsafe_math_optimizations)
429 /* Simplify (C / x op 0.0) to x op 0.0 for C != 0, C != Inf/Nan.
430 Since C / x may underflow to zero, do this only for unsafe math. */
431 (for op (lt le gt ge)
434 (op (rdiv REAL_CST@0 @1) real_zerop@2)
435 (if (!HONOR_SIGNED_ZEROS (@1) && !HONOR_INFINITIES (@1))
437 (if (real_less (&dconst0, TREE_REAL_CST_PTR (@0)))
439 /* For C < 0, use the inverted operator. */
440 (if (real_less (TREE_REAL_CST_PTR (@0), &dconst0))
443 /* Optimize (X & (-A)) / A where A is a power of 2, to X >> log2(A) */
444 (for div (trunc_div ceil_div floor_div round_div exact_div)
446 (div (convert? (bit_and @0 INTEGER_CST@1)) INTEGER_CST@2)
447 (if (integer_pow2p (@2)
448 && tree_int_cst_sgn (@2) > 0
449 && tree_nop_conversion_p (type, TREE_TYPE (@0))
450 && wi::to_wide (@2) + wi::to_wide (@1) == 0)
452 { build_int_cst (integer_type_node,
453 wi::exact_log2 (wi::to_wide (@2))); }))))
455 /* If ARG1 is a constant, we can convert this to a multiply by the
456 reciprocal. This does not have the same rounding properties,
457 so only do this if -freciprocal-math. We can actually
458 always safely do it if ARG1 is a power of two, but it's hard to
459 tell if it is or not in a portable manner. */
460 (for cst (REAL_CST COMPLEX_CST VECTOR_CST)
464 (if (flag_reciprocal_math
467 { tree tem = const_binop (RDIV_EXPR, type, build_one_cst (type), @1); }
469 (mult @0 { tem; } )))
470 (if (cst != COMPLEX_CST)
471 (with { tree inverse = exact_inverse (type, @1); }
473 (mult @0 { inverse; } ))))))))
475 (for mod (ceil_mod floor_mod round_mod trunc_mod)
476 /* 0 % X is always zero. */
478 (mod integer_zerop@0 @1)
479 /* But not for 0 % 0 so that we can get the proper warnings and errors. */
480 (if (!integer_zerop (@1))
482 /* X % 1 is always zero. */
484 (mod @0 integer_onep)
485 { build_zero_cst (type); })
486 /* X % -1 is zero. */
488 (mod @0 integer_minus_onep@1)
489 (if (!TYPE_UNSIGNED (type))
490 { build_zero_cst (type); }))
494 /* But not for 0 % 0 so that we can get the proper warnings and errors. */
495 (if (!integer_zerop (@0))
496 { build_zero_cst (type); }))
497 /* (X % Y) % Y is just X % Y. */
499 (mod (mod@2 @0 @1) @1)
501 /* From extract_muldiv_1: (X * C1) % C2 is zero if C1 is a multiple of C2. */
503 (mod (mult @0 INTEGER_CST@1) INTEGER_CST@2)
504 (if (ANY_INTEGRAL_TYPE_P (type)
505 && TYPE_OVERFLOW_UNDEFINED (type)
506 && wi::multiple_of_p (wi::to_wide (@1), wi::to_wide (@2),
508 { build_zero_cst (type); }))
509 /* For (X % C) == 0, if X is signed and C is power of 2, use unsigned
510 modulo and comparison, since it is simpler and equivalent. */
513 (cmp (mod @0 integer_pow2p@2) integer_zerop@1)
514 (if (!TYPE_UNSIGNED (TREE_TYPE (@0)))
515 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
516 (cmp (mod (convert:utype @0) (convert:utype @2)) (convert:utype @1)))))))
518 /* X % -C is the same as X % C. */
520 (trunc_mod @0 INTEGER_CST@1)
521 (if (TYPE_SIGN (type) == SIGNED
522 && !TREE_OVERFLOW (@1)
523 && wi::neg_p (wi::to_wide (@1))
524 && !TYPE_OVERFLOW_TRAPS (type)
525 /* Avoid this transformation if C is INT_MIN, i.e. C == -C. */
526 && !sign_bit_p (@1, @1))
527 (trunc_mod @0 (negate @1))))
529 /* X % -Y is the same as X % Y. */
531 (trunc_mod @0 (convert? (negate @1)))
532 (if (INTEGRAL_TYPE_P (type)
533 && !TYPE_UNSIGNED (type)
534 && !TYPE_OVERFLOW_TRAPS (type)
535 && tree_nop_conversion_p (type, TREE_TYPE (@1))
536 /* Avoid this transformation if X might be INT_MIN or
537 Y might be -1, because we would then change valid
538 INT_MIN % -(-1) into invalid INT_MIN % -1. */
539 && (expr_not_equal_to (@0, wi::to_wide (TYPE_MIN_VALUE (type)))
540 || expr_not_equal_to (@1, wi::minus_one (TYPE_PRECISION
542 (trunc_mod @0 (convert @1))))
544 /* X - (X / Y) * Y is the same as X % Y. */
546 (minus (convert1? @0) (convert2? (mult:c (trunc_div @@0 @@1) @1)))
547 (if (INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
548 (convert (trunc_mod @0 @1))))
550 /* Optimize TRUNC_MOD_EXPR by a power of two into a BIT_AND_EXPR,
551 i.e. "X % C" into "X & (C - 1)", if X and C are positive.
552 Also optimize A % (C << N) where C is a power of 2,
553 to A & ((C << N) - 1). */
554 (match (power_of_two_cand @1)
556 (match (power_of_two_cand @1)
557 (lshift INTEGER_CST@1 @2))
558 (for mod (trunc_mod floor_mod)
560 (mod @0 (convert?@3 (power_of_two_cand@1 @2)))
561 (if ((TYPE_UNSIGNED (type)
562 || tree_expr_nonnegative_p (@0))
563 && tree_nop_conversion_p (type, TREE_TYPE (@3))
564 && integer_pow2p (@2) && tree_int_cst_sgn (@2) > 0)
565 (bit_and @0 (convert (minus @1 { build_int_cst (TREE_TYPE (@1), 1); }))))))
567 /* Simplify (unsigned t * 2)/2 -> unsigned t & 0x7FFFFFFF. */
569 (trunc_div (mult @0 integer_pow2p@1) @1)
570 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
571 (bit_and @0 { wide_int_to_tree
572 (type, wi::mask (TYPE_PRECISION (type)
573 - wi::exact_log2 (wi::to_wide (@1)),
574 false, TYPE_PRECISION (type))); })))
576 /* Simplify (unsigned t / 2) * 2 -> unsigned t & ~1. */
578 (mult (trunc_div @0 integer_pow2p@1) @1)
579 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
580 (bit_and @0 (negate @1))))
582 /* Simplify (t * 2) / 2) -> t. */
583 (for div (trunc_div ceil_div floor_div round_div exact_div)
585 (div (mult:c @0 @1) @1)
586 (if (ANY_INTEGRAL_TYPE_P (type)
587 && TYPE_OVERFLOW_UNDEFINED (type))
591 /* Simplify cos(-x) and cos(|x|) -> cos(x). Similarly for cosh. */
596 /* Simplify pow(-x, y) and pow(|x|,y) -> pow(x,y) if y is an even integer. */
599 (pows (op @0) REAL_CST@1)
600 (with { HOST_WIDE_INT n; }
601 (if (real_isinteger (&TREE_REAL_CST (@1), &n) && (n & 1) == 0)
603 /* Likewise for powi. */
606 (pows (op @0) INTEGER_CST@1)
607 (if ((wi::to_wide (@1) & 1) == 0)
609 /* Strip negate and abs from both operands of hypot. */
617 /* copysign(-x, y) and copysign(abs(x), y) -> copysign(x, y). */
618 (for copysigns (COPYSIGN_ALL)
620 (copysigns (op @0) @1)
623 /* abs(x)*abs(x) -> x*x. Should be valid for all types. */
628 /* Convert absu(x)*absu(x) -> x*x. */
630 (mult (absu@1 @0) @1)
631 (mult (convert@2 @0) @2))
633 /* cos(copysign(x, y)) -> cos(x). Similarly for cosh. */
637 (coss (copysigns @0 @1))
640 /* pow(copysign(x, y), z) -> pow(x, z) if z is an even integer. */
644 (pows (copysigns @0 @2) REAL_CST@1)
645 (with { HOST_WIDE_INT n; }
646 (if (real_isinteger (&TREE_REAL_CST (@1), &n) && (n & 1) == 0)
648 /* Likewise for powi. */
652 (pows (copysigns @0 @2) INTEGER_CST@1)
653 (if ((wi::to_wide (@1) & 1) == 0)
658 /* hypot(copysign(x, y), z) -> hypot(x, z). */
660 (hypots (copysigns @0 @1) @2)
662 /* hypot(x, copysign(y, z)) -> hypot(x, y). */
664 (hypots @0 (copysigns @1 @2))
667 /* copysign(x, CST) -> [-]abs (x). */
668 (for copysigns (COPYSIGN_ALL)
670 (copysigns @0 REAL_CST@1)
671 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
675 /* copysign(copysign(x, y), z) -> copysign(x, z). */
676 (for copysigns (COPYSIGN_ALL)
678 (copysigns (copysigns @0 @1) @2)
681 /* copysign(x,y)*copysign(x,y) -> x*x. */
682 (for copysigns (COPYSIGN_ALL)
684 (mult (copysigns@2 @0 @1) @2)
687 /* ccos(-x) -> ccos(x). Similarly for ccosh. */
688 (for ccoss (CCOS CCOSH)
693 /* cabs(-x) and cos(conj(x)) -> cabs(x). */
694 (for ops (conj negate)
700 /* Fold (a * (1 << b)) into (a << b) */
702 (mult:c @0 (convert? (lshift integer_onep@1 @2)))
703 (if (! FLOAT_TYPE_P (type)
704 && tree_nop_conversion_p (type, TREE_TYPE (@1)))
707 /* Fold (1 << (C - x)) where C = precision(type) - 1
708 into ((1 << C) >> x). */
710 (lshift integer_onep@0 (minus@1 INTEGER_CST@2 @3))
711 (if (INTEGRAL_TYPE_P (type)
712 && wi::eq_p (wi::to_wide (@2), TYPE_PRECISION (type) - 1)
714 (if (TYPE_UNSIGNED (type))
715 (rshift (lshift @0 @2) @3)
717 { tree utype = unsigned_type_for (type); }
718 (convert (rshift (lshift (convert:utype @0) @2) @3))))))
720 /* Fold (C1/X)*C2 into (C1*C2)/X. */
722 (mult (rdiv@3 REAL_CST@0 @1) REAL_CST@2)
723 (if (flag_associative_math
726 { tree tem = const_binop (MULT_EXPR, type, @0, @2); }
728 (rdiv { tem; } @1)))))
730 /* Simplify ~X & X as zero. */
732 (bit_and:c (convert? @0) (convert? (bit_not @0)))
733 { build_zero_cst (type); })
735 /* PR71636: Transform x & ((1U << b) - 1) -> x & ~(~0U << b); */
737 (bit_and:c @0 (plus:s (lshift:s integer_onep @1) integer_minus_onep))
738 (if (TYPE_UNSIGNED (type))
739 (bit_and @0 (bit_not (lshift { build_all_ones_cst (type); } @1)))))
741 (for bitop (bit_and bit_ior)
743 /* PR35691: Transform
744 (x == 0 & y == 0) -> (x | typeof(x)(y)) == 0.
745 (x != 0 | y != 0) -> (x | typeof(x)(y)) != 0. */
747 (bitop (cmp @0 integer_zerop@2) (cmp @1 integer_zerop))
748 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
749 && INTEGRAL_TYPE_P (TREE_TYPE (@1))
750 && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
751 (cmp (bit_ior @0 (convert @1)) @2)))
753 (x == -1 & y == -1) -> (x & typeof(x)(y)) == -1.
754 (x != -1 | y != -1) -> (x & typeof(x)(y)) != -1. */
756 (bitop (cmp @0 integer_all_onesp@2) (cmp @1 integer_all_onesp))
757 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
758 && INTEGRAL_TYPE_P (TREE_TYPE (@1))
759 && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
760 (cmp (bit_and @0 (convert @1)) @2))))
762 /* Fold (A & ~B) - (A & B) into (A ^ B) - B. */
764 (minus (bit_and:cs @0 (bit_not @1)) (bit_and:cs @0 @1))
765 (minus (bit_xor @0 @1) @1))
767 (minus (bit_and:s @0 INTEGER_CST@2) (bit_and:s @0 INTEGER_CST@1))
768 (if (~wi::to_wide (@2) == wi::to_wide (@1))
769 (minus (bit_xor @0 @1) @1)))
771 /* Fold (A & B) - (A & ~B) into B - (A ^ B). */
773 (minus (bit_and:cs @0 @1) (bit_and:cs @0 (bit_not @1)))
774 (minus @1 (bit_xor @0 @1)))
776 /* Simplify (X & ~Y) |^+ (~X & Y) -> X ^ Y. */
777 (for op (bit_ior bit_xor plus)
779 (op (bit_and:c @0 (bit_not @1)) (bit_and:c (bit_not @0) @1))
782 (op:c (bit_and @0 INTEGER_CST@2) (bit_and (bit_not @0) INTEGER_CST@1))
783 (if (~wi::to_wide (@2) == wi::to_wide (@1))
786 /* PR53979: Transform ((a ^ b) | a) -> (a | b) */
788 (bit_ior:c (bit_xor:c @0 @1) @0)
791 /* (a & ~b) | (a ^ b) --> a ^ b */
793 (bit_ior:c (bit_and:c @0 (bit_not @1)) (bit_xor:c@2 @0 @1))
796 /* (a & ~b) ^ ~a --> ~(a & b) */
798 (bit_xor:c (bit_and:cs @0 (bit_not @1)) (bit_not @0))
799 (bit_not (bit_and @0 @1)))
801 /* (a | b) & ~(a ^ b) --> a & b */
803 (bit_and:c (bit_ior @0 @1) (bit_not (bit_xor:c @0 @1)))
806 /* a | ~(a ^ b) --> a | ~b */
808 (bit_ior:c @0 (bit_not:s (bit_xor:c @0 @1)))
809 (bit_ior @0 (bit_not @1)))
811 /* (a | b) | (a &^ b) --> a | b */
812 (for op (bit_and bit_xor)
814 (bit_ior:c (bit_ior@2 @0 @1) (op:c @0 @1))
817 /* (a & b) | ~(a ^ b) --> ~(a ^ b) */
819 (bit_ior:c (bit_and:c @0 @1) (bit_not@2 (bit_xor @0 @1)))
822 /* ~(~a & b) --> a | ~b */
824 (bit_not (bit_and:cs (bit_not @0) @1))
825 (bit_ior @0 (bit_not @1)))
827 /* ~(~a | b) --> a & ~b */
829 (bit_not (bit_ior:cs (bit_not @0) @1))
830 (bit_and @0 (bit_not @1)))
832 /* Simplify (~X & Y) to X ^ Y if we know that (X & ~Y) is 0. */
835 (bit_and (bit_not SSA_NAME@0) INTEGER_CST@1)
836 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
837 && wi::bit_and_not (get_nonzero_bits (@0), wi::to_wide (@1)) == 0)
841 /* For constants M and N, if M == (1LL << cst) - 1 && (N & M) == M,
842 ((A & N) + B) & M -> (A + B) & M
843 Similarly if (N & M) == 0,
844 ((A | N) + B) & M -> (A + B) & M
845 and for - instead of + (or unary - instead of +)
846 and/or ^ instead of |.
847 If B is constant and (B & M) == 0, fold into A & M. */
849 (for bitop (bit_and bit_ior bit_xor)
851 (bit_and (op:s (bitop:s@0 @3 INTEGER_CST@4) @1) INTEGER_CST@2)
854 tree utype = fold_bit_and_mask (TREE_TYPE (@0), @2, op, @0, bitop,
855 @3, @4, @1, ERROR_MARK, NULL_TREE,
858 (convert (bit_and (op (convert:utype { pmop[0]; })
859 (convert:utype { pmop[1]; }))
860 (convert:utype @2))))))
862 (bit_and (op:s @0 (bitop:s@1 @3 INTEGER_CST@4)) INTEGER_CST@2)
865 tree utype = fold_bit_and_mask (TREE_TYPE (@0), @2, op, @0, ERROR_MARK,
866 NULL_TREE, NULL_TREE, @1, bitop, @3,
869 (convert (bit_and (op (convert:utype { pmop[0]; })
870 (convert:utype { pmop[1]; }))
871 (convert:utype @2)))))))
873 (bit_and (op:s @0 @1) INTEGER_CST@2)
876 tree utype = fold_bit_and_mask (TREE_TYPE (@0), @2, op, @0, ERROR_MARK,
877 NULL_TREE, NULL_TREE, @1, ERROR_MARK,
878 NULL_TREE, NULL_TREE, pmop); }
880 (convert (bit_and (op (convert:utype { pmop[0]; })
881 (convert:utype { pmop[1]; }))
882 (convert:utype @2)))))))
883 (for bitop (bit_and bit_ior bit_xor)
885 (bit_and (negate:s (bitop:s@0 @2 INTEGER_CST@3)) INTEGER_CST@1)
888 tree utype = fold_bit_and_mask (TREE_TYPE (@0), @1, NEGATE_EXPR, @0,
889 bitop, @2, @3, NULL_TREE, ERROR_MARK,
890 NULL_TREE, NULL_TREE, pmop); }
892 (convert (bit_and (negate (convert:utype { pmop[0]; }))
893 (convert:utype @1)))))))
895 /* X % Y is smaller than Y. */
898 (cmp (trunc_mod @0 @1) @1)
899 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
900 { constant_boolean_node (cmp == LT_EXPR, type); })))
903 (cmp @1 (trunc_mod @0 @1))
904 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
905 { constant_boolean_node (cmp == GT_EXPR, type); })))
909 (bit_ior @0 integer_all_onesp@1)
914 (bit_ior @0 integer_zerop)
919 (bit_and @0 integer_zerop@1)
925 (for op (bit_ior bit_xor plus)
927 (op:c (convert? @0) (convert? (bit_not @0)))
928 (convert { build_all_ones_cst (TREE_TYPE (@0)); })))
933 { build_zero_cst (type); })
935 /* Canonicalize X ^ ~0 to ~X. */
937 (bit_xor @0 integer_all_onesp@1)
942 (bit_and @0 integer_all_onesp)
945 /* x & x -> x, x | x -> x */
946 (for bitop (bit_and bit_ior)
951 /* x & C -> x if we know that x & ~C == 0. */
954 (bit_and SSA_NAME@0 INTEGER_CST@1)
955 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
956 && wi::bit_and_not (get_nonzero_bits (@0), wi::to_wide (@1)) == 0)
960 /* x + (x & 1) -> (x + 1) & ~1 */
962 (plus:c @0 (bit_and:s @0 integer_onep@1))
963 (bit_and (plus @0 @1) (bit_not @1)))
965 /* x & ~(x & y) -> x & ~y */
966 /* x | ~(x | y) -> x | ~y */
967 (for bitop (bit_and bit_ior)
969 (bitop:c @0 (bit_not (bitop:cs @0 @1)))
970 (bitop @0 (bit_not @1))))
972 /* (~x & y) | ~(x | y) -> ~x */
974 (bit_ior:c (bit_and:c (bit_not@2 @0) @1) (bit_not (bit_ior:c @0 @1)))
977 /* (x | y) ^ (x | ~y) -> ~x */
979 (bit_xor:c (bit_ior:c @0 @1) (bit_ior:c @0 (bit_not @1)))
982 /* (x & y) | ~(x | y) -> ~(x ^ y) */
984 (bit_ior:c (bit_and:s @0 @1) (bit_not:s (bit_ior:s @0 @1)))
985 (bit_not (bit_xor @0 @1)))
987 /* (~x | y) ^ (x ^ y) -> x | ~y */
989 (bit_xor:c (bit_ior:cs (bit_not @0) @1) (bit_xor:s @0 @1))
990 (bit_ior @0 (bit_not @1)))
992 /* (x ^ y) | ~(x | y) -> ~(x & y) */
994 (bit_ior:c (bit_xor:s @0 @1) (bit_not:s (bit_ior:s @0 @1)))
995 (bit_not (bit_and @0 @1)))
997 /* (x | y) & ~x -> y & ~x */
998 /* (x & y) | ~x -> y | ~x */
999 (for bitop (bit_and bit_ior)
1000 rbitop (bit_ior bit_and)
1002 (bitop:c (rbitop:c @0 @1) (bit_not@2 @0))
1005 /* (x & y) ^ (x | y) -> x ^ y */
1007 (bit_xor:c (bit_and @0 @1) (bit_ior @0 @1))
1010 /* (x ^ y) ^ (x | y) -> x & y */
1012 (bit_xor:c (bit_xor @0 @1) (bit_ior @0 @1))
1015 /* (x & y) + (x ^ y) -> x | y */
1016 /* (x & y) | (x ^ y) -> x | y */
1017 /* (x & y) ^ (x ^ y) -> x | y */
1018 (for op (plus bit_ior bit_xor)
1020 (op:c (bit_and @0 @1) (bit_xor @0 @1))
1023 /* (x & y) + (x | y) -> x + y */
1025 (plus:c (bit_and @0 @1) (bit_ior @0 @1))
1028 /* (x + y) - (x | y) -> x & y */
1030 (minus (plus @0 @1) (bit_ior @0 @1))
1031 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1032 && !TYPE_SATURATING (type))
1035 /* (x + y) - (x & y) -> x | y */
1037 (minus (plus @0 @1) (bit_and @0 @1))
1038 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1039 && !TYPE_SATURATING (type))
1042 /* (x | y) - (x ^ y) -> x & y */
1044 (minus (bit_ior @0 @1) (bit_xor @0 @1))
1047 /* (x | y) - (x & y) -> x ^ y */
1049 (minus (bit_ior @0 @1) (bit_and @0 @1))
1052 /* (x | y) & ~(x & y) -> x ^ y */
1054 (bit_and:c (bit_ior @0 @1) (bit_not (bit_and @0 @1)))
1057 /* (x | y) & (~x ^ y) -> x & y */
1059 (bit_and:c (bit_ior:c @0 @1) (bit_xor:c @1 (bit_not @0)))
1062 /* (~x | y) & (x | ~y) -> ~(x ^ y) */
1064 (bit_and (bit_ior:cs (bit_not @0) @1) (bit_ior:cs @0 (bit_not @1)))
1065 (bit_not (bit_xor @0 @1)))
1067 /* (~x | y) ^ (x | ~y) -> x ^ y */
1069 (bit_xor (bit_ior:c (bit_not @0) @1) (bit_ior:c @0 (bit_not @1)))
1072 /* ~x & ~y -> ~(x | y)
1073 ~x | ~y -> ~(x & y) */
1074 (for op (bit_and bit_ior)
1075 rop (bit_ior bit_and)
1077 (op (convert1? (bit_not @0)) (convert2? (bit_not @1)))
1078 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1079 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
1080 (bit_not (rop (convert @0) (convert @1))))))
1082 /* If we are XORing or adding two BIT_AND_EXPR's, both of which are and'ing
1083 with a constant, and the two constants have no bits in common,
1084 we should treat this as a BIT_IOR_EXPR since this may produce more
1086 (for op (bit_xor plus)
1088 (op (convert1? (bit_and@4 @0 INTEGER_CST@1))
1089 (convert2? (bit_and@5 @2 INTEGER_CST@3)))
1090 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
1091 && tree_nop_conversion_p (type, TREE_TYPE (@2))
1092 && (wi::to_wide (@1) & wi::to_wide (@3)) == 0)
1093 (bit_ior (convert @4) (convert @5)))))
1095 /* (X | Y) ^ X -> Y & ~ X*/
1097 (bit_xor:c (convert1? (bit_ior:c @@0 @1)) (convert2? @0))
1098 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1099 (convert (bit_and @1 (bit_not @0)))))
1101 /* Convert ~X ^ ~Y to X ^ Y. */
1103 (bit_xor (convert1? (bit_not @0)) (convert2? (bit_not @1)))
1104 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1105 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
1106 (bit_xor (convert @0) (convert @1))))
1108 /* Convert ~X ^ C to X ^ ~C. */
1110 (bit_xor (convert? (bit_not @0)) INTEGER_CST@1)
1111 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1112 (bit_xor (convert @0) (bit_not @1))))
1114 /* Fold (X & Y) ^ Y and (X ^ Y) & Y as ~X & Y. */
1115 (for opo (bit_and bit_xor)
1116 opi (bit_xor bit_and)
1118 (opo:c (opi:cs @0 @1) @1)
1119 (bit_and (bit_not @0) @1)))
1121 /* Given a bit-wise operation CODE applied to ARG0 and ARG1, see if both
1122 operands are another bit-wise operation with a common input. If so,
1123 distribute the bit operations to save an operation and possibly two if
1124 constants are involved. For example, convert
1125 (A | B) & (A | C) into A | (B & C)
1126 Further simplification will occur if B and C are constants. */
1127 (for op (bit_and bit_ior bit_xor)
1128 rop (bit_ior bit_and bit_and)
1130 (op (convert? (rop:c @@0 @1)) (convert? (rop:c @0 @2)))
1131 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1132 && tree_nop_conversion_p (type, TREE_TYPE (@2)))
1133 (rop (convert @0) (op (convert @1) (convert @2))))))
1135 /* Some simple reassociation for bit operations, also handled in reassoc. */
1136 /* (X & Y) & Y -> X & Y
1137 (X | Y) | Y -> X | Y */
1138 (for op (bit_and bit_ior)
1140 (op:c (convert1?@2 (op:c @0 @@1)) (convert2? @1))
1142 /* (X ^ Y) ^ Y -> X */
1144 (bit_xor:c (convert1? (bit_xor:c @0 @@1)) (convert2? @1))
1146 /* (X & Y) & (X & Z) -> (X & Y) & Z
1147 (X | Y) | (X | Z) -> (X | Y) | Z */
1148 (for op (bit_and bit_ior)
1150 (op (convert1?@3 (op:c@4 @0 @1)) (convert2?@5 (op:c@6 @0 @2)))
1151 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1152 && tree_nop_conversion_p (type, TREE_TYPE (@2)))
1153 (if (single_use (@5) && single_use (@6))
1154 (op @3 (convert @2))
1155 (if (single_use (@3) && single_use (@4))
1156 (op (convert @1) @5))))))
1157 /* (X ^ Y) ^ (X ^ Z) -> Y ^ Z */
1159 (bit_xor (convert1? (bit_xor:c @0 @1)) (convert2? (bit_xor:c @0 @2)))
1160 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1161 && tree_nop_conversion_p (type, TREE_TYPE (@2)))
1162 (bit_xor (convert @1) (convert @2))))
1164 /* Convert abs (abs (X)) into abs (X).
1165 also absu (absu (X)) into absu (X). */
1171 (absu (convert@2 (absu@1 @0)))
1172 (if (tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@1)))
1175 /* Convert abs[u] (-X) -> abs[u] (X). */
1184 /* Convert abs[u] (X) where X is nonnegative -> (X). */
1186 (abs tree_expr_nonnegative_p@0)
1190 (absu tree_expr_nonnegative_p@0)
1193 /* A few cases of fold-const.c negate_expr_p predicate. */
1194 (match negate_expr_p
1196 (if ((INTEGRAL_TYPE_P (type)
1197 && TYPE_UNSIGNED (type))
1198 || (!TYPE_OVERFLOW_SANITIZED (type)
1199 && may_negate_without_overflow_p (t)))))
1200 (match negate_expr_p
1202 (match negate_expr_p
1204 (if (!TYPE_OVERFLOW_SANITIZED (type))))
1205 (match negate_expr_p
1207 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (t)))))
1208 /* VECTOR_CST handling of non-wrapping types would recurse in unsupported
1210 (match negate_expr_p
1212 (if (FLOAT_TYPE_P (TREE_TYPE (type)) || TYPE_OVERFLOW_WRAPS (type))))
1213 (match negate_expr_p
1215 (if ((ANY_INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_WRAPS (type))
1216 || (FLOAT_TYPE_P (type)
1217 && !HONOR_SIGN_DEPENDENT_ROUNDING (type)
1218 && !HONOR_SIGNED_ZEROS (type)))))
1220 /* (-A) * (-B) -> A * B */
1222 (mult:c (convert1? (negate @0)) (convert2? negate_expr_p@1))
1223 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
1224 && tree_nop_conversion_p (type, TREE_TYPE (@1)))
1225 (mult (convert @0) (convert (negate @1)))))
1227 /* -(A + B) -> (-B) - A. */
1229 (negate (plus:c @0 negate_expr_p@1))
1230 (if (!HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type))
1231 && !HONOR_SIGNED_ZEROS (element_mode (type)))
1232 (minus (negate @1) @0)))
1234 /* -(A - B) -> B - A. */
1236 (negate (minus @0 @1))
1237 (if ((ANY_INTEGRAL_TYPE_P (type) && !TYPE_OVERFLOW_SANITIZED (type))
1238 || (FLOAT_TYPE_P (type)
1239 && !HONOR_SIGN_DEPENDENT_ROUNDING (type)
1240 && !HONOR_SIGNED_ZEROS (type)))
1243 (negate (pointer_diff @0 @1))
1244 (if (TYPE_OVERFLOW_UNDEFINED (type))
1245 (pointer_diff @1 @0)))
1247 /* A - B -> A + (-B) if B is easily negatable. */
1249 (minus @0 negate_expr_p@1)
1250 (if (!FIXED_POINT_TYPE_P (type))
1251 (plus @0 (negate @1))))
1253 /* Try to fold (type) X op CST -> (type) (X op ((type-x) CST))
1255 For bitwise binary operations apply operand conversions to the
1256 binary operation result instead of to the operands. This allows
1257 to combine successive conversions and bitwise binary operations.
1258 We combine the above two cases by using a conditional convert. */
1259 (for bitop (bit_and bit_ior bit_xor)
1261 (bitop (convert @0) (convert? @1))
1262 (if (((TREE_CODE (@1) == INTEGER_CST
1263 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
1264 && int_fits_type_p (@1, TREE_TYPE (@0)))
1265 || types_match (@0, @1))
1266 /* ??? This transform conflicts with fold-const.c doing
1267 Convert (T)(x & c) into (T)x & (T)c, if c is an integer
1268 constants (if x has signed type, the sign bit cannot be set
1269 in c). This folds extension into the BIT_AND_EXPR.
1270 Restrict it to GIMPLE to avoid endless recursions. */
1271 && (bitop != BIT_AND_EXPR || GIMPLE)
1272 && (/* That's a good idea if the conversion widens the operand, thus
1273 after hoisting the conversion the operation will be narrower. */
1274 TYPE_PRECISION (TREE_TYPE (@0)) < TYPE_PRECISION (type)
1275 /* It's also a good idea if the conversion is to a non-integer
1277 || GET_MODE_CLASS (TYPE_MODE (type)) != MODE_INT
1278 /* Or if the precision of TO is not the same as the precision
1280 || !type_has_mode_precision_p (type)))
1281 (convert (bitop @0 (convert @1))))))
1283 (for bitop (bit_and bit_ior)
1284 rbitop (bit_ior bit_and)
1285 /* (x | y) & x -> x */
1286 /* (x & y) | x -> x */
1288 (bitop:c (rbitop:c @0 @1) @0)
1290 /* (~x | y) & x -> x & y */
1291 /* (~x & y) | x -> x | y */
1293 (bitop:c (rbitop:c (bit_not @0) @1) @0)
1296 /* (x | CST1) & CST2 -> (x & CST2) | (CST1 & CST2) */
1298 (bit_and (bit_ior @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
1299 (bit_ior (bit_and @0 @2) (bit_and @1 @2)))
1301 /* Combine successive equal operations with constants. */
1302 (for bitop (bit_and bit_ior bit_xor)
1304 (bitop (bitop @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
1305 (if (!CONSTANT_CLASS_P (@0))
1306 /* This is the canonical form regardless of whether (bitop @1 @2) can be
1307 folded to a constant. */
1308 (bitop @0 (bitop @1 @2))
1309 /* In this case we have three constants and (bitop @0 @1) doesn't fold
1310 to a constant. This can happen if @0 or @1 is a POLY_INT_CST and if
1311 the values involved are such that the operation can't be decided at
1312 compile time. Try folding one of @0 or @1 with @2 to see whether
1313 that combination can be decided at compile time.
1315 Keep the existing form if both folds fail, to avoid endless
1317 (with { tree cst1 = const_binop (bitop, type, @0, @2); }
1319 (bitop @1 { cst1; })
1320 (with { tree cst2 = const_binop (bitop, type, @1, @2); }
1322 (bitop @0 { cst2; }))))))))
1324 /* Try simple folding for X op !X, and X op X with the help
1325 of the truth_valued_p and logical_inverted_value predicates. */
1326 (match truth_valued_p
1328 (if (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1)))
1329 (for op (tcc_comparison truth_and truth_andif truth_or truth_orif truth_xor)
1330 (match truth_valued_p
1332 (match truth_valued_p
1335 (match (logical_inverted_value @0)
1337 (match (logical_inverted_value @0)
1338 (bit_not truth_valued_p@0))
1339 (match (logical_inverted_value @0)
1340 (eq @0 integer_zerop))
1341 (match (logical_inverted_value @0)
1342 (ne truth_valued_p@0 integer_truep))
1343 (match (logical_inverted_value @0)
1344 (bit_xor truth_valued_p@0 integer_truep))
1348 (bit_and:c @0 (logical_inverted_value @0))
1349 { build_zero_cst (type); })
1350 /* X | !X and X ^ !X -> 1, , if X is truth-valued. */
1351 (for op (bit_ior bit_xor)
1353 (op:c truth_valued_p@0 (logical_inverted_value @0))
1354 { constant_boolean_node (true, type); }))
1355 /* X ==/!= !X is false/true. */
1358 (op:c truth_valued_p@0 (logical_inverted_value @0))
1359 { constant_boolean_node (op == NE_EXPR ? true : false, type); }))
1363 (bit_not (bit_not @0))
1366 /* Convert ~ (-A) to A - 1. */
1368 (bit_not (convert? (negate @0)))
1369 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1370 || !TYPE_UNSIGNED (TREE_TYPE (@0)))
1371 (convert (minus @0 { build_each_one_cst (TREE_TYPE (@0)); }))))
1373 /* Convert - (~A) to A + 1. */
1375 (negate (nop_convert (bit_not @0)))
1376 (plus (view_convert @0) { build_each_one_cst (type); }))
1378 /* Convert ~ (A - 1) or ~ (A + -1) to -A. */
1380 (bit_not (convert? (minus @0 integer_each_onep)))
1381 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1382 || !TYPE_UNSIGNED (TREE_TYPE (@0)))
1383 (convert (negate @0))))
1385 (bit_not (convert? (plus @0 integer_all_onesp)))
1386 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1387 || !TYPE_UNSIGNED (TREE_TYPE (@0)))
1388 (convert (negate @0))))
1390 /* Part of convert ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify. */
1392 (bit_not (convert? (bit_xor @0 INTEGER_CST@1)))
1393 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1394 (convert (bit_xor @0 (bit_not @1)))))
1396 (bit_not (convert? (bit_xor:c (bit_not @0) @1)))
1397 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1398 (convert (bit_xor @0 @1))))
1400 /* Otherwise prefer ~(X ^ Y) to ~X ^ Y as more canonical. */
1402 (bit_xor:c (nop_convert:s (bit_not:s @0)) @1)
1403 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1404 (bit_not (bit_xor (view_convert @0) @1))))
1406 /* (x & ~m) | (y & m) -> ((x ^ y) & m) ^ x */
1408 (bit_ior:c (bit_and:cs @0 (bit_not @2)) (bit_and:cs @1 @2))
1409 (bit_xor (bit_and (bit_xor @0 @1) @2) @0))
1411 /* Fold A - (A & B) into ~B & A. */
1413 (minus (convert1? @0) (convert2?:s (bit_and:cs @@0 @1)))
1414 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
1415 && tree_nop_conversion_p (type, TREE_TYPE (@1)))
1416 (convert (bit_and (bit_not @1) @0))))
1418 /* (m1 CMP m2) * d -> (m1 CMP m2) ? d : 0 */
1419 (for cmp (gt lt ge le)
1421 (mult (convert (cmp @0 @1)) @2)
1422 (cond (cmp @0 @1) @2 { build_zero_cst (type); })))
1424 /* For integral types with undefined overflow and C != 0 fold
1425 x * C EQ/NE y * C into x EQ/NE y. */
1428 (cmp (mult:c @0 @1) (mult:c @2 @1))
1429 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
1430 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1431 && tree_expr_nonzero_p (@1))
1434 /* For integral types with wrapping overflow and C odd fold
1435 x * C EQ/NE y * C into x EQ/NE y. */
1438 (cmp (mult @0 INTEGER_CST@1) (mult @2 @1))
1439 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
1440 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))
1441 && (TREE_INT_CST_LOW (@1) & 1) != 0)
1444 /* For integral types with undefined overflow and C != 0 fold
1445 x * C RELOP y * C into:
1447 x RELOP y for nonnegative C
1448 y RELOP x for negative C */
1449 (for cmp (lt gt le ge)
1451 (cmp (mult:c @0 @1) (mult:c @2 @1))
1452 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
1453 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1454 (if (tree_expr_nonnegative_p (@1) && tree_expr_nonzero_p (@1))
1456 (if (TREE_CODE (@1) == INTEGER_CST
1457 && wi::neg_p (wi::to_wide (@1), TYPE_SIGN (TREE_TYPE (@1))))
1460 /* (X - 1U) <= INT_MAX-1U into (int) X > 0. */
1464 (cmp (plus @0 integer_minus_onep@1) INTEGER_CST@2)
1465 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1466 && TYPE_UNSIGNED (TREE_TYPE (@0))
1467 && TYPE_PRECISION (TREE_TYPE (@0)) > 1
1468 && (wi::to_wide (@2)
1469 == wi::max_value (TYPE_PRECISION (TREE_TYPE (@0)), SIGNED) - 1))
1470 (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
1471 (icmp (convert:stype @0) { build_int_cst (stype, 0); })))))
1473 /* X / 4 < Y / 4 iff X < Y when the division is known to be exact. */
1474 (for cmp (simple_comparison)
1476 (cmp (exact_div @0 INTEGER_CST@2) (exact_div @1 @2))
1477 (if (wi::gt_p (wi::to_wide (@2), 0, TYPE_SIGN (TREE_TYPE (@2))))
1480 /* X / C1 op C2 into a simple range test. */
1481 (for cmp (simple_comparison)
1483 (cmp (trunc_div:s @0 INTEGER_CST@1) INTEGER_CST@2)
1484 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1485 && integer_nonzerop (@1)
1486 && !TREE_OVERFLOW (@1)
1487 && !TREE_OVERFLOW (@2))
1488 (with { tree lo, hi; bool neg_overflow;
1489 enum tree_code code = fold_div_compare (cmp, @1, @2, &lo, &hi,
1492 (if (code == LT_EXPR || code == GE_EXPR)
1493 (if (TREE_OVERFLOW (lo))
1494 { build_int_cst (type, (code == LT_EXPR) ^ neg_overflow); }
1495 (if (code == LT_EXPR)
1498 (if (code == LE_EXPR || code == GT_EXPR)
1499 (if (TREE_OVERFLOW (hi))
1500 { build_int_cst (type, (code == LE_EXPR) ^ neg_overflow); }
1501 (if (code == LE_EXPR)
1505 { build_int_cst (type, code == NE_EXPR); })
1506 (if (code == EQ_EXPR && !hi)
1508 (if (code == EQ_EXPR && !lo)
1510 (if (code == NE_EXPR && !hi)
1512 (if (code == NE_EXPR && !lo)
1515 { build_range_check (UNKNOWN_LOCATION, type, @0, code == EQ_EXPR,
1519 tree etype = range_check_type (TREE_TYPE (@0));
1522 if (! TYPE_UNSIGNED (etype))
1523 etype = unsigned_type_for (etype);
1524 hi = fold_convert (etype, hi);
1525 lo = fold_convert (etype, lo);
1526 hi = const_binop (MINUS_EXPR, etype, hi, lo);
1529 (if (etype && hi && !TREE_OVERFLOW (hi))
1530 (if (code == EQ_EXPR)
1531 (le (minus (convert:etype @0) { lo; }) { hi; })
1532 (gt (minus (convert:etype @0) { lo; }) { hi; })))))))))
1534 /* X + Z < Y + Z is the same as X < Y when there is no overflow. */
1535 (for op (lt le ge gt)
1537 (op (plus:c @0 @2) (plus:c @1 @2))
1538 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1539 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1541 /* For equality and subtraction, this is also true with wrapping overflow. */
1542 (for op (eq ne minus)
1544 (op (plus:c @0 @2) (plus:c @1 @2))
1545 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1546 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1547 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
1550 /* X - Z < Y - Z is the same as X < Y when there is no overflow. */
1551 (for op (lt le ge gt)
1553 (op (minus @0 @2) (minus @1 @2))
1554 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1555 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1557 /* For equality and subtraction, this is also true with wrapping overflow. */
1558 (for op (eq ne minus)
1560 (op (minus @0 @2) (minus @1 @2))
1561 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1562 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1563 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
1565 /* And for pointers... */
1566 (for op (simple_comparison)
1568 (op (pointer_diff@3 @0 @2) (pointer_diff @1 @2))
1569 (if (!TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
1572 (minus (pointer_diff@3 @0 @2) (pointer_diff @1 @2))
1573 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@3))
1574 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
1575 (pointer_diff @0 @1)))
1577 /* Z - X < Z - Y is the same as Y < X when there is no overflow. */
1578 (for op (lt le ge gt)
1580 (op (minus @2 @0) (minus @2 @1))
1581 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1582 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1584 /* For equality and subtraction, this is also true with wrapping overflow. */
1585 (for op (eq ne minus)
1587 (op (minus @2 @0) (minus @2 @1))
1588 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1589 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1590 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
1592 /* And for pointers... */
1593 (for op (simple_comparison)
1595 (op (pointer_diff@3 @2 @0) (pointer_diff @2 @1))
1596 (if (!TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
1599 (minus (pointer_diff@3 @2 @0) (pointer_diff @2 @1))
1600 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@3))
1601 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
1602 (pointer_diff @1 @0)))
1604 /* X + Y < Y is the same as X < 0 when there is no overflow. */
1605 (for op (lt le gt ge)
1607 (op:c (plus:c@2 @0 @1) @1)
1608 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1609 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1610 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0))
1611 && (CONSTANT_CLASS_P (@0) || single_use (@2)))
1612 (op @0 { build_zero_cst (TREE_TYPE (@0)); }))))
1613 /* For equality, this is also true with wrapping overflow. */
1616 (op:c (nop_convert@3 (plus:c@2 @0 (convert1? @1))) (convert2? @1))
1617 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1618 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1619 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
1620 && (CONSTANT_CLASS_P (@0) || (single_use (@2) && single_use (@3)))
1621 && tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@2))
1622 && tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@1)))
1623 (op @0 { build_zero_cst (TREE_TYPE (@0)); })))
1625 (op:c (nop_convert@3 (pointer_plus@2 (convert1? @0) @1)) (convert2? @0))
1626 (if (tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@0))
1627 && tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0))
1628 && (CONSTANT_CLASS_P (@1) || (single_use (@2) && single_use (@3))))
1629 (op @1 { build_zero_cst (TREE_TYPE (@1)); }))))
1631 /* X - Y < X is the same as Y > 0 when there is no overflow.
1632 For equality, this is also true with wrapping overflow. */
1633 (for op (simple_comparison)
1635 (op:c @0 (minus@2 @0 @1))
1636 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1637 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1638 || ((op == EQ_EXPR || op == NE_EXPR)
1639 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
1640 && (CONSTANT_CLASS_P (@1) || single_use (@2)))
1641 (op @1 { build_zero_cst (TREE_TYPE (@1)); }))))
1644 (X / Y) == 0 -> X < Y if X, Y are unsigned.
1645 (X / Y) != 0 -> X >= Y, if X, Y are unsigned. */
1649 (cmp (trunc_div @0 @1) integer_zerop)
1650 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
1651 /* Complex ==/!= is allowed, but not </>=. */
1652 && TREE_CODE (TREE_TYPE (@0)) != COMPLEX_TYPE
1653 && (VECTOR_TYPE_P (type) || !VECTOR_TYPE_P (TREE_TYPE (@0))))
1656 /* X == C - X can never be true if C is odd. */
1659 (cmp:c (convert? @0) (convert1? (minus INTEGER_CST@1 (convert2? @0))))
1660 (if (TREE_INT_CST_LOW (@1) & 1)
1661 { constant_boolean_node (cmp == NE_EXPR, type); })))
1663 /* Arguments on which one can call get_nonzero_bits to get the bits
1665 (match with_possible_nonzero_bits
1667 (match with_possible_nonzero_bits
1669 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))))
1670 /* Slightly extended version, do not make it recursive to keep it cheap. */
1671 (match (with_possible_nonzero_bits2 @0)
1672 with_possible_nonzero_bits@0)
1673 (match (with_possible_nonzero_bits2 @0)
1674 (bit_and:c with_possible_nonzero_bits@0 @2))
1676 /* Same for bits that are known to be set, but we do not have
1677 an equivalent to get_nonzero_bits yet. */
1678 (match (with_certain_nonzero_bits2 @0)
1680 (match (with_certain_nonzero_bits2 @0)
1681 (bit_ior @1 INTEGER_CST@0))
1683 /* X == C (or X & Z == Y | C) is impossible if ~nonzero(X) & C != 0. */
1686 (cmp:c (with_possible_nonzero_bits2 @0) (with_certain_nonzero_bits2 @1))
1687 (if (wi::bit_and_not (wi::to_wide (@1), get_nonzero_bits (@0)) != 0)
1688 { constant_boolean_node (cmp == NE_EXPR, type); })))
1690 /* ((X inner_op C0) outer_op C1)
1691 With X being a tree where value_range has reasoned certain bits to always be
1692 zero throughout its computed value range,
1693 inner_op = {|,^}, outer_op = {|,^} and inner_op != outer_op
1694 where zero_mask has 1's for all bits that are sure to be 0 in
1696 if (inner_op == '^') C0 &= ~C1;
1697 if ((C0 & ~zero_mask) == 0) then emit (X outer_op (C0 outer_op C1)
1698 if ((C1 & ~zero_mask) == 0) then emit (X inner_op (C0 outer_op C1)
1700 (for inner_op (bit_ior bit_xor)
1701 outer_op (bit_xor bit_ior)
1704 (inner_op:s @2 INTEGER_CST@0) INTEGER_CST@1)
1708 wide_int zero_mask_not;
1712 if (TREE_CODE (@2) == SSA_NAME)
1713 zero_mask_not = get_nonzero_bits (@2);
1717 if (inner_op == BIT_XOR_EXPR)
1719 C0 = wi::bit_and_not (wi::to_wide (@0), wi::to_wide (@1));
1720 cst_emit = C0 | wi::to_wide (@1);
1724 C0 = wi::to_wide (@0);
1725 cst_emit = C0 ^ wi::to_wide (@1);
1728 (if (!fail && (C0 & zero_mask_not) == 0)
1729 (outer_op @2 { wide_int_to_tree (type, cst_emit); })
1730 (if (!fail && (wi::to_wide (@1) & zero_mask_not) == 0)
1731 (inner_op @2 { wide_int_to_tree (type, cst_emit); }))))))
1733 /* Associate (p +p off1) +p off2 as (p +p (off1 + off2)). */
1735 (pointer_plus (pointer_plus:s @0 @1) @3)
1736 (pointer_plus @0 (plus @1 @3)))
1742 tem4 = (unsigned long) tem3;
1747 (pointer_plus @0 (convert?@2 (minus@3 (convert @1) (convert @0))))
1748 /* Conditionally look through a sign-changing conversion. */
1749 (if (TYPE_PRECISION (TREE_TYPE (@2)) == TYPE_PRECISION (TREE_TYPE (@3))
1750 && ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@1)))
1751 || (GENERIC && type == TREE_TYPE (@1))))
1754 (pointer_plus @0 (convert?@2 (pointer_diff@3 @1 @@0)))
1755 (if (TYPE_PRECISION (TREE_TYPE (@2)) >= TYPE_PRECISION (TREE_TYPE (@3)))
1759 tem = (sizetype) ptr;
1763 and produce the simpler and easier to analyze with respect to alignment
1764 ... = ptr & ~algn; */
1766 (pointer_plus @0 (negate (bit_and (convert @0) INTEGER_CST@1)))
1767 (with { tree algn = wide_int_to_tree (TREE_TYPE (@0), ~wi::to_wide (@1)); }
1768 (bit_and @0 { algn; })))
1770 /* Try folding difference of addresses. */
1772 (minus (convert ADDR_EXPR@0) (convert @1))
1773 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1774 (with { poly_int64 diff; }
1775 (if (ptr_difference_const (@0, @1, &diff))
1776 { build_int_cst_type (type, diff); }))))
1778 (minus (convert @0) (convert ADDR_EXPR@1))
1779 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1780 (with { poly_int64 diff; }
1781 (if (ptr_difference_const (@0, @1, &diff))
1782 { build_int_cst_type (type, diff); }))))
1784 (pointer_diff (convert?@2 ADDR_EXPR@0) (convert1?@3 @1))
1785 (if (tree_nop_conversion_p (TREE_TYPE(@2), TREE_TYPE (@0))
1786 && tree_nop_conversion_p (TREE_TYPE(@3), TREE_TYPE (@1)))
1787 (with { poly_int64 diff; }
1788 (if (ptr_difference_const (@0, @1, &diff))
1789 { build_int_cst_type (type, diff); }))))
1791 (pointer_diff (convert?@2 @0) (convert1?@3 ADDR_EXPR@1))
1792 (if (tree_nop_conversion_p (TREE_TYPE(@2), TREE_TYPE (@0))
1793 && tree_nop_conversion_p (TREE_TYPE(@3), TREE_TYPE (@1)))
1794 (with { poly_int64 diff; }
1795 (if (ptr_difference_const (@0, @1, &diff))
1796 { build_int_cst_type (type, diff); }))))
1798 /* If arg0 is derived from the address of an object or function, we may
1799 be able to fold this expression using the object or function's
1802 (bit_and (convert? @0) INTEGER_CST@1)
1803 (if (POINTER_TYPE_P (TREE_TYPE (@0))
1804 && tree_nop_conversion_p (type, TREE_TYPE (@0)))
1808 unsigned HOST_WIDE_INT bitpos;
1809 get_pointer_alignment_1 (@0, &align, &bitpos);
1811 (if (wi::ltu_p (wi::to_wide (@1), align / BITS_PER_UNIT))
1812 { wide_int_to_tree (type, (wi::to_wide (@1)
1813 & (bitpos / BITS_PER_UNIT))); }))))
1816 /* We can't reassociate at all for saturating types. */
1817 (if (!TYPE_SATURATING (type))
1819 /* Contract negates. */
1820 /* A + (-B) -> A - B */
1822 (plus:c @0 (convert? (negate @1)))
1823 /* Apply STRIP_NOPS on the negate. */
1824 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1825 && !TYPE_OVERFLOW_SANITIZED (type))
1829 if (INTEGRAL_TYPE_P (type)
1830 && TYPE_OVERFLOW_WRAPS (type) != TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
1831 t1 = TYPE_OVERFLOW_WRAPS (type) ? type : TREE_TYPE (@1);
1833 (convert (minus (convert:t1 @0) (convert:t1 @1))))))
1834 /* A - (-B) -> A + B */
1836 (minus @0 (convert? (negate @1)))
1837 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1838 && !TYPE_OVERFLOW_SANITIZED (type))
1842 if (INTEGRAL_TYPE_P (type)
1843 && TYPE_OVERFLOW_WRAPS (type) != TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
1844 t1 = TYPE_OVERFLOW_WRAPS (type) ? type : TREE_TYPE (@1);
1846 (convert (plus (convert:t1 @0) (convert:t1 @1))))))
1848 Sign-extension is ok except for INT_MIN, which thankfully cannot
1849 happen without overflow. */
1851 (negate (convert (negate @1)))
1852 (if (INTEGRAL_TYPE_P (type)
1853 && (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@1))
1854 || (!TYPE_UNSIGNED (TREE_TYPE (@1))
1855 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))))
1856 && !TYPE_OVERFLOW_SANITIZED (type)
1857 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@1)))
1860 (negate (convert negate_expr_p@1))
1861 (if (SCALAR_FLOAT_TYPE_P (type)
1862 && ((DECIMAL_FLOAT_TYPE_P (type)
1863 == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1))
1864 && TYPE_PRECISION (type) >= TYPE_PRECISION (TREE_TYPE (@1)))
1865 || !HONOR_SIGN_DEPENDENT_ROUNDING (type)))
1866 (convert (negate @1))))
1868 (negate (nop_convert (negate @1)))
1869 (if (!TYPE_OVERFLOW_SANITIZED (type)
1870 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@1)))
1873 /* We can't reassociate floating-point unless -fassociative-math
1874 or fixed-point plus or minus because of saturation to +-Inf. */
1875 (if ((!FLOAT_TYPE_P (type) || flag_associative_math)
1876 && !FIXED_POINT_TYPE_P (type))
1878 /* Match patterns that allow contracting a plus-minus pair
1879 irrespective of overflow issues. */
1880 /* (A +- B) - A -> +- B */
1881 /* (A +- B) -+ B -> A */
1882 /* A - (A +- B) -> -+ B */
1883 /* A +- (B -+ A) -> +- B */
1885 (minus (plus:c @0 @1) @0)
1888 (minus (minus @0 @1) @0)
1891 (plus:c (minus @0 @1) @1)
1894 (minus @0 (plus:c @0 @1))
1897 (minus @0 (minus @0 @1))
1899 /* (A +- B) + (C - A) -> C +- B */
1900 /* (A + B) - (A - C) -> B + C */
1901 /* More cases are handled with comparisons. */
1903 (plus:c (plus:c @0 @1) (minus @2 @0))
1906 (plus:c (minus @0 @1) (minus @2 @0))
1909 (plus:c (pointer_diff @0 @1) (pointer_diff @2 @0))
1910 (if (TYPE_OVERFLOW_UNDEFINED (type)
1911 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0)))
1912 (pointer_diff @2 @1)))
1914 (minus (plus:c @0 @1) (minus @0 @2))
1917 /* (A +- CST1) +- CST2 -> A + CST3
1918 Use view_convert because it is safe for vectors and equivalent for
1920 (for outer_op (plus minus)
1921 (for inner_op (plus minus)
1922 neg_inner_op (minus plus)
1924 (outer_op (nop_convert (inner_op @0 CONSTANT_CLASS_P@1))
1926 /* If one of the types wraps, use that one. */
1927 (if (!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_WRAPS (type))
1928 /* If all 3 captures are CONSTANT_CLASS_P, punt, as we might recurse
1929 forever if something doesn't simplify into a constant. */
1930 (if (!CONSTANT_CLASS_P (@0))
1931 (if (outer_op == PLUS_EXPR)
1932 (plus (view_convert @0) (inner_op @2 (view_convert @1)))
1933 (minus (view_convert @0) (neg_inner_op @2 (view_convert @1)))))
1934 (if (!ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1935 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
1936 (if (outer_op == PLUS_EXPR)
1937 (view_convert (plus @0 (inner_op (view_convert @2) @1)))
1938 (view_convert (minus @0 (neg_inner_op (view_convert @2) @1))))
1939 /* If the constant operation overflows we cannot do the transform
1940 directly as we would introduce undefined overflow, for example
1941 with (a - 1) + INT_MIN. */
1942 (if (types_match (type, @0))
1943 (with { tree cst = const_binop (outer_op == inner_op
1944 ? PLUS_EXPR : MINUS_EXPR,
1946 (if (cst && !TREE_OVERFLOW (cst))
1947 (inner_op @0 { cst; } )
1948 /* X+INT_MAX+1 is X-INT_MIN. */
1949 (if (INTEGRAL_TYPE_P (type) && cst
1950 && wi::to_wide (cst) == wi::min_value (type))
1951 (neg_inner_op @0 { wide_int_to_tree (type, wi::to_wide (cst)); })
1952 /* Last resort, use some unsigned type. */
1953 (with { tree utype = unsigned_type_for (type); }
1955 (view_convert (inner_op
1956 (view_convert:utype @0)
1958 { drop_tree_overflow (cst); }))))))))))))))
1960 /* (CST1 - A) +- CST2 -> CST3 - A */
1961 (for outer_op (plus minus)
1963 (outer_op (minus CONSTANT_CLASS_P@1 @0) CONSTANT_CLASS_P@2)
1964 (with { tree cst = const_binop (outer_op, type, @1, @2); }
1965 (if (cst && !TREE_OVERFLOW (cst))
1966 (minus { cst; } @0)))))
1968 /* CST1 - (CST2 - A) -> CST3 + A */
1970 (minus CONSTANT_CLASS_P@1 (minus CONSTANT_CLASS_P@2 @0))
1971 (with { tree cst = const_binop (MINUS_EXPR, type, @1, @2); }
1972 (if (cst && !TREE_OVERFLOW (cst))
1973 (plus { cst; } @0))))
1977 (plus:c (bit_not @0) @0)
1978 (if (!TYPE_OVERFLOW_TRAPS (type))
1979 { build_all_ones_cst (type); }))
1983 (plus (convert? (bit_not @0)) integer_each_onep)
1984 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1985 (negate (convert @0))))
1989 (minus (convert? (negate @0)) integer_each_onep)
1990 (if (!TYPE_OVERFLOW_TRAPS (type)
1991 && tree_nop_conversion_p (type, TREE_TYPE (@0)))
1992 (bit_not (convert @0))))
1996 (minus integer_all_onesp @0)
1999 /* (T)(P + A) - (T)P -> (T) A */
2001 (minus (convert (plus:c @@0 @1))
2003 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
2004 /* For integer types, if A has a smaller type
2005 than T the result depends on the possible
2007 E.g. T=size_t, A=(unsigned)429497295, P>0.
2008 However, if an overflow in P + A would cause
2009 undefined behavior, we can assume that there
2011 || (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2012 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))))
2015 (minus (convert (pointer_plus @@0 @1))
2017 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
2018 /* For pointer types, if the conversion of A to the
2019 final type requires a sign- or zero-extension,
2020 then we have to punt - it is not defined which
2022 || (POINTER_TYPE_P (TREE_TYPE (@0))
2023 && TREE_CODE (@1) == INTEGER_CST
2024 && tree_int_cst_sign_bit (@1) == 0))
2027 (pointer_diff (pointer_plus @@0 @1) @0)
2028 /* The second argument of pointer_plus must be interpreted as signed, and
2029 thus sign-extended if necessary. */
2030 (with { tree stype = signed_type_for (TREE_TYPE (@1)); }
2031 /* Use view_convert instead of convert here, as POINTER_PLUS_EXPR
2032 second arg is unsigned even when we need to consider it as signed,
2033 we don't want to diagnose overflow here. */
2034 (convert (view_convert:stype @1))))
2036 /* (T)P - (T)(P + A) -> -(T) A */
2038 (minus (convert? @0)
2039 (convert (plus:c @@0 @1)))
2040 (if (INTEGRAL_TYPE_P (type)
2041 && TYPE_OVERFLOW_UNDEFINED (type)
2042 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
2043 (with { tree utype = unsigned_type_for (type); }
2044 (convert (negate (convert:utype @1))))
2045 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
2046 /* For integer types, if A has a smaller type
2047 than T the result depends on the possible
2049 E.g. T=size_t, A=(unsigned)429497295, P>0.
2050 However, if an overflow in P + A would cause
2051 undefined behavior, we can assume that there
2053 || (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2054 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))))
2055 (negate (convert @1)))))
2058 (convert (pointer_plus @@0 @1)))
2059 (if (INTEGRAL_TYPE_P (type)
2060 && TYPE_OVERFLOW_UNDEFINED (type)
2061 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
2062 (with { tree utype = unsigned_type_for (type); }
2063 (convert (negate (convert:utype @1))))
2064 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
2065 /* For pointer types, if the conversion of A to the
2066 final type requires a sign- or zero-extension,
2067 then we have to punt - it is not defined which
2069 || (POINTER_TYPE_P (TREE_TYPE (@0))
2070 && TREE_CODE (@1) == INTEGER_CST
2071 && tree_int_cst_sign_bit (@1) == 0))
2072 (negate (convert @1)))))
2074 (pointer_diff @0 (pointer_plus @@0 @1))
2075 /* The second argument of pointer_plus must be interpreted as signed, and
2076 thus sign-extended if necessary. */
2077 (with { tree stype = signed_type_for (TREE_TYPE (@1)); }
2078 /* Use view_convert instead of convert here, as POINTER_PLUS_EXPR
2079 second arg is unsigned even when we need to consider it as signed,
2080 we don't want to diagnose overflow here. */
2081 (negate (convert (view_convert:stype @1)))))
2083 /* (T)(P + A) - (T)(P + B) -> (T)A - (T)B */
2085 (minus (convert (plus:c @@0 @1))
2086 (convert (plus:c @0 @2)))
2087 (if (INTEGRAL_TYPE_P (type)
2088 && TYPE_OVERFLOW_UNDEFINED (type)
2089 && element_precision (type) <= element_precision (TREE_TYPE (@1))
2090 && element_precision (type) <= element_precision (TREE_TYPE (@2)))
2091 (with { tree utype = unsigned_type_for (type); }
2092 (convert (minus (convert:utype @1) (convert:utype @2))))
2093 (if (((element_precision (type) <= element_precision (TREE_TYPE (@1)))
2094 == (element_precision (type) <= element_precision (TREE_TYPE (@2))))
2095 && (element_precision (type) <= element_precision (TREE_TYPE (@1))
2096 /* For integer types, if A has a smaller type
2097 than T the result depends on the possible
2099 E.g. T=size_t, A=(unsigned)429497295, P>0.
2100 However, if an overflow in P + A would cause
2101 undefined behavior, we can assume that there
2103 || (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2104 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
2105 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))
2106 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@2)))))
2107 (minus (convert @1) (convert @2)))))
2109 (minus (convert (pointer_plus @@0 @1))
2110 (convert (pointer_plus @0 @2)))
2111 (if (INTEGRAL_TYPE_P (type)
2112 && TYPE_OVERFLOW_UNDEFINED (type)
2113 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
2114 (with { tree utype = unsigned_type_for (type); }
2115 (convert (minus (convert:utype @1) (convert:utype @2))))
2116 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
2117 /* For pointer types, if the conversion of A to the
2118 final type requires a sign- or zero-extension,
2119 then we have to punt - it is not defined which
2121 || (POINTER_TYPE_P (TREE_TYPE (@0))
2122 && TREE_CODE (@1) == INTEGER_CST
2123 && tree_int_cst_sign_bit (@1) == 0
2124 && TREE_CODE (@2) == INTEGER_CST
2125 && tree_int_cst_sign_bit (@2) == 0))
2126 (minus (convert @1) (convert @2)))))
2128 (pointer_diff (pointer_plus @@0 @1) (pointer_plus @0 @2))
2129 /* The second argument of pointer_plus must be interpreted as signed, and
2130 thus sign-extended if necessary. */
2131 (with { tree stype = signed_type_for (TREE_TYPE (@1)); }
2132 /* Use view_convert instead of convert here, as POINTER_PLUS_EXPR
2133 second arg is unsigned even when we need to consider it as signed,
2134 we don't want to diagnose overflow here. */
2135 (minus (convert (view_convert:stype @1))
2136 (convert (view_convert:stype @2)))))))
2138 /* (A * C) +- (B * C) -> (A+-B) * C and (A * C) +- A -> A * (C+-1).
2139 Modeled after fold_plusminus_mult_expr. */
2140 (if (!TYPE_SATURATING (type)
2141 && (!FLOAT_TYPE_P (type) || flag_associative_math))
2142 (for plusminus (plus minus)
2144 (plusminus (mult:cs@3 @0 @1) (mult:cs@4 @0 @2))
2145 (if ((!ANY_INTEGRAL_TYPE_P (type)
2146 || TYPE_OVERFLOW_WRAPS (type)
2147 || (INTEGRAL_TYPE_P (type)
2148 && tree_expr_nonzero_p (@0)
2149 && expr_not_equal_to (@0, wi::minus_one (TYPE_PRECISION (type)))))
2150 /* If @1 +- @2 is constant require a hard single-use on either
2151 original operand (but not on both). */
2152 && (single_use (@3) || single_use (@4)))
2153 (mult (plusminus @1 @2) @0)))
2154 /* We cannot generate constant 1 for fract. */
2155 (if (!ALL_FRACT_MODE_P (TYPE_MODE (type)))
2157 (plusminus @0 (mult:c@3 @0 @2))
2158 (if ((!ANY_INTEGRAL_TYPE_P (type)
2159 || TYPE_OVERFLOW_WRAPS (type)
2160 || (INTEGRAL_TYPE_P (type)
2161 && tree_expr_nonzero_p (@0)
2162 && expr_not_equal_to (@0, wi::minus_one (TYPE_PRECISION (type)))))
2164 (mult (plusminus { build_one_cst (type); } @2) @0)))
2166 (plusminus (mult:c@3 @0 @2) @0)
2167 (if ((!ANY_INTEGRAL_TYPE_P (type)
2168 || TYPE_OVERFLOW_WRAPS (type)
2169 || (INTEGRAL_TYPE_P (type)
2170 && tree_expr_nonzero_p (@0)
2171 && expr_not_equal_to (@0, wi::minus_one (TYPE_PRECISION (type)))))
2173 (mult (plusminus @2 { build_one_cst (type); }) @0))))))
2175 /* Simplifications of MIN_EXPR, MAX_EXPR, fmin() and fmax(). */
2177 (for minmax (min max FMIN_ALL FMAX_ALL)
2181 /* min(max(x,y),y) -> y. */
2183 (min:c (max:c @0 @1) @1)
2185 /* max(min(x,y),y) -> y. */
2187 (max:c (min:c @0 @1) @1)
2189 /* max(a,-a) -> abs(a). */
2191 (max:c @0 (negate @0))
2192 (if (TREE_CODE (type) != COMPLEX_TYPE
2193 && (! ANY_INTEGRAL_TYPE_P (type)
2194 || TYPE_OVERFLOW_UNDEFINED (type)))
2196 /* min(a,-a) -> -abs(a). */
2198 (min:c @0 (negate @0))
2199 (if (TREE_CODE (type) != COMPLEX_TYPE
2200 && (! ANY_INTEGRAL_TYPE_P (type)
2201 || TYPE_OVERFLOW_UNDEFINED (type)))
2206 (if (INTEGRAL_TYPE_P (type)
2207 && TYPE_MIN_VALUE (type)
2208 && operand_equal_p (@1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
2210 (if (INTEGRAL_TYPE_P (type)
2211 && TYPE_MAX_VALUE (type)
2212 && operand_equal_p (@1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
2217 (if (INTEGRAL_TYPE_P (type)
2218 && TYPE_MAX_VALUE (type)
2219 && operand_equal_p (@1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
2221 (if (INTEGRAL_TYPE_P (type)
2222 && TYPE_MIN_VALUE (type)
2223 && operand_equal_p (@1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
2226 /* max (a, a + CST) -> a + CST where CST is positive. */
2227 /* max (a, a + CST) -> a where CST is negative. */
2229 (max:c @0 (plus@2 @0 INTEGER_CST@1))
2230 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
2231 (if (tree_int_cst_sgn (@1) > 0)
2235 /* min (a, a + CST) -> a where CST is positive. */
2236 /* min (a, a + CST) -> a + CST where CST is negative. */
2238 (min:c @0 (plus@2 @0 INTEGER_CST@1))
2239 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
2240 (if (tree_int_cst_sgn (@1) > 0)
2244 /* (convert (minmax ((convert (x) c)))) -> minmax (x c) if x is promoted
2245 and the outer convert demotes the expression back to x's type. */
2246 (for minmax (min max)
2248 (convert (minmax@0 (convert @1) INTEGER_CST@2))
2249 (if (INTEGRAL_TYPE_P (type)
2250 && types_match (@1, type) && int_fits_type_p (@2, type)
2251 && TYPE_SIGN (TREE_TYPE (@0)) == TYPE_SIGN (type)
2252 && TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type))
2253 (minmax @1 (convert @2)))))
2255 (for minmax (FMIN_ALL FMAX_ALL)
2256 /* If either argument is NaN, return the other one. Avoid the
2257 transformation if we get (and honor) a signalling NaN. */
2259 (minmax:c @0 REAL_CST@1)
2260 (if (real_isnan (TREE_REAL_CST_PTR (@1))
2261 && (!HONOR_SNANS (@1) || !TREE_REAL_CST (@1).signalling))
2263 /* Convert fmin/fmax to MIN_EXPR/MAX_EXPR. C99 requires these
2264 functions to return the numeric arg if the other one is NaN.
2265 MIN and MAX don't honor that, so only transform if -ffinite-math-only
2266 is set. C99 doesn't require -0.0 to be handled, so we don't have to
2267 worry about it either. */
2268 (if (flag_finite_math_only)
2275 /* min (-A, -B) -> -max (A, B) */
2276 (for minmax (min max FMIN_ALL FMAX_ALL)
2277 maxmin (max min FMAX_ALL FMIN_ALL)
2279 (minmax (negate:s@2 @0) (negate:s@3 @1))
2280 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
2281 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2282 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
2283 (negate (maxmin @0 @1)))))
2284 /* MIN (~X, ~Y) -> ~MAX (X, Y)
2285 MAX (~X, ~Y) -> ~MIN (X, Y) */
2286 (for minmax (min max)
2289 (minmax (bit_not:s@2 @0) (bit_not:s@3 @1))
2290 (bit_not (maxmin @0 @1))))
2292 /* MIN (X, Y) == X -> X <= Y */
2293 (for minmax (min min max max)
2297 (cmp:c (minmax:c @0 @1) @0)
2298 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0)))
2300 /* MIN (X, 5) == 0 -> X == 0
2301 MIN (X, 5) == 7 -> false */
2304 (cmp (min @0 INTEGER_CST@1) INTEGER_CST@2)
2305 (if (wi::lt_p (wi::to_wide (@1), wi::to_wide (@2),
2306 TYPE_SIGN (TREE_TYPE (@0))))
2307 { constant_boolean_node (cmp == NE_EXPR, type); }
2308 (if (wi::gt_p (wi::to_wide (@1), wi::to_wide (@2),
2309 TYPE_SIGN (TREE_TYPE (@0))))
2313 (cmp (max @0 INTEGER_CST@1) INTEGER_CST@2)
2314 (if (wi::gt_p (wi::to_wide (@1), wi::to_wide (@2),
2315 TYPE_SIGN (TREE_TYPE (@0))))
2316 { constant_boolean_node (cmp == NE_EXPR, type); }
2317 (if (wi::lt_p (wi::to_wide (@1), wi::to_wide (@2),
2318 TYPE_SIGN (TREE_TYPE (@0))))
2320 /* MIN (X, C1) < C2 -> X < C2 || C1 < C2 */
2321 (for minmax (min min max max min min max max )
2322 cmp (lt le gt ge gt ge lt le )
2323 comb (bit_ior bit_ior bit_ior bit_ior bit_and bit_and bit_and bit_and)
2325 (cmp (minmax @0 INTEGER_CST@1) INTEGER_CST@2)
2326 (comb (cmp @0 @2) (cmp @1 @2))))
2328 /* Simplifications of shift and rotates. */
2330 (for rotate (lrotate rrotate)
2332 (rotate integer_all_onesp@0 @1)
2335 /* Optimize -1 >> x for arithmetic right shifts. */
2337 (rshift integer_all_onesp@0 @1)
2338 (if (!TYPE_UNSIGNED (type)
2339 && tree_expr_nonnegative_p (@1))
2342 /* Optimize (x >> c) << c into x & (-1<<c). */
2344 (lshift (rshift @0 INTEGER_CST@1) @1)
2345 (if (wi::ltu_p (wi::to_wide (@1), element_precision (type)))
2346 (bit_and @0 (lshift { build_minus_one_cst (type); } @1))))
2348 /* Optimize (x << c) >> c into x & ((unsigned)-1 >> c) for unsigned
2351 (rshift (lshift @0 INTEGER_CST@1) @1)
2352 (if (TYPE_UNSIGNED (type)
2353 && (wi::ltu_p (wi::to_wide (@1), element_precision (type))))
2354 (bit_and @0 (rshift { build_minus_one_cst (type); } @1))))
2356 (for shiftrotate (lrotate rrotate lshift rshift)
2358 (shiftrotate @0 integer_zerop)
2361 (shiftrotate integer_zerop@0 @1)
2363 /* Prefer vector1 << scalar to vector1 << vector2
2364 if vector2 is uniform. */
2365 (for vec (VECTOR_CST CONSTRUCTOR)
2367 (shiftrotate @0 vec@1)
2368 (with { tree tem = uniform_vector_p (@1); }
2370 (shiftrotate @0 { tem; }))))))
2372 /* Simplify X << Y where Y's low width bits are 0 to X, as only valid
2373 Y is 0. Similarly for X >> Y. */
2375 (for shift (lshift rshift)
2377 (shift @0 SSA_NAME@1)
2378 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1)))
2380 int width = ceil_log2 (element_precision (TREE_TYPE (@0)));
2381 int prec = TYPE_PRECISION (TREE_TYPE (@1));
2383 (if ((get_nonzero_bits (@1) & wi::mask (width, false, prec)) == 0)
2387 /* Rewrite an LROTATE_EXPR by a constant into an
2388 RROTATE_EXPR by a new constant. */
2390 (lrotate @0 INTEGER_CST@1)
2391 (rrotate @0 { const_binop (MINUS_EXPR, TREE_TYPE (@1),
2392 build_int_cst (TREE_TYPE (@1),
2393 element_precision (type)), @1); }))
2395 /* Turn (a OP c1) OP c2 into a OP (c1+c2). */
2396 (for op (lrotate rrotate rshift lshift)
2398 (op (op @0 INTEGER_CST@1) INTEGER_CST@2)
2399 (with { unsigned int prec = element_precision (type); }
2400 (if (wi::ge_p (wi::to_wide (@1), 0, TYPE_SIGN (TREE_TYPE (@1)))
2401 && wi::lt_p (wi::to_wide (@1), prec, TYPE_SIGN (TREE_TYPE (@1)))
2402 && wi::ge_p (wi::to_wide (@2), 0, TYPE_SIGN (TREE_TYPE (@2)))
2403 && wi::lt_p (wi::to_wide (@2), prec, TYPE_SIGN (TREE_TYPE (@2))))
2404 (with { unsigned int low = (tree_to_uhwi (@1)
2405 + tree_to_uhwi (@2)); }
2406 /* Deal with a OP (c1 + c2) being undefined but (a OP c1) OP c2
2407 being well defined. */
2409 (if (op == LROTATE_EXPR || op == RROTATE_EXPR)
2410 (op @0 { build_int_cst (TREE_TYPE (@1), low % prec); })
2411 (if (TYPE_UNSIGNED (type) || op == LSHIFT_EXPR)
2412 { build_zero_cst (type); }
2413 (op @0 { build_int_cst (TREE_TYPE (@1), prec - 1); })))
2414 (op @0 { build_int_cst (TREE_TYPE (@1), low); })))))))
2417 /* ((1 << A) & 1) != 0 -> A == 0
2418 ((1 << A) & 1) == 0 -> A != 0 */
2422 (cmp (bit_and (lshift integer_onep @0) integer_onep) integer_zerop)
2423 (icmp @0 { build_zero_cst (TREE_TYPE (@0)); })))
2425 /* (CST1 << A) == CST2 -> A == ctz (CST2) - ctz (CST1)
2426 (CST1 << A) != CST2 -> A != ctz (CST2) - ctz (CST1)
2430 (cmp (lshift INTEGER_CST@0 @1) INTEGER_CST@2)
2431 (with { int cand = wi::ctz (wi::to_wide (@2)) - wi::ctz (wi::to_wide (@0)); }
2433 || (!integer_zerop (@2)
2434 && wi::lshift (wi::to_wide (@0), cand) != wi::to_wide (@2)))
2435 { constant_boolean_node (cmp == NE_EXPR, type); }
2436 (if (!integer_zerop (@2)
2437 && wi::lshift (wi::to_wide (@0), cand) == wi::to_wide (@2))
2438 (cmp @1 { build_int_cst (TREE_TYPE (@1), cand); }))))))
2440 /* Fold (X << C1) & C2 into (X << C1) & (C2 | ((1 << C1) - 1))
2441 (X >> C1) & C2 into (X >> C1) & (C2 | ~((type) -1 >> C1))
2442 if the new mask might be further optimized. */
2443 (for shift (lshift rshift)
2445 (bit_and (convert?:s@4 (shift:s@5 (convert1?@3 @0) INTEGER_CST@1))
2447 (if (tree_nop_conversion_p (TREE_TYPE (@4), TREE_TYPE (@5))
2448 && TYPE_PRECISION (type) <= HOST_BITS_PER_WIDE_INT
2449 && tree_fits_uhwi_p (@1)
2450 && tree_to_uhwi (@1) > 0
2451 && tree_to_uhwi (@1) < TYPE_PRECISION (type))
2454 unsigned int shiftc = tree_to_uhwi (@1);
2455 unsigned HOST_WIDE_INT mask = TREE_INT_CST_LOW (@2);
2456 unsigned HOST_WIDE_INT newmask, zerobits = 0;
2457 tree shift_type = TREE_TYPE (@3);
2460 if (shift == LSHIFT_EXPR)
2461 zerobits = ((HOST_WIDE_INT_1U << shiftc) - 1);
2462 else if (shift == RSHIFT_EXPR
2463 && type_has_mode_precision_p (shift_type))
2465 prec = TYPE_PRECISION (TREE_TYPE (@3));
2467 /* See if more bits can be proven as zero because of
2470 && TYPE_UNSIGNED (TREE_TYPE (@0)))
2472 tree inner_type = TREE_TYPE (@0);
2473 if (type_has_mode_precision_p (inner_type)
2474 && TYPE_PRECISION (inner_type) < prec)
2476 prec = TYPE_PRECISION (inner_type);
2477 /* See if we can shorten the right shift. */
2479 shift_type = inner_type;
2480 /* Otherwise X >> C1 is all zeros, so we'll optimize
2481 it into (X, 0) later on by making sure zerobits
2485 zerobits = HOST_WIDE_INT_M1U;
2488 zerobits >>= HOST_BITS_PER_WIDE_INT - shiftc;
2489 zerobits <<= prec - shiftc;
2491 /* For arithmetic shift if sign bit could be set, zerobits
2492 can contain actually sign bits, so no transformation is
2493 possible, unless MASK masks them all away. In that
2494 case the shift needs to be converted into logical shift. */
2495 if (!TYPE_UNSIGNED (TREE_TYPE (@3))
2496 && prec == TYPE_PRECISION (TREE_TYPE (@3)))
2498 if ((mask & zerobits) == 0)
2499 shift_type = unsigned_type_for (TREE_TYPE (@3));
2505 /* ((X << 16) & 0xff00) is (X, 0). */
2506 (if ((mask & zerobits) == mask)
2507 { build_int_cst (type, 0); }
2508 (with { newmask = mask | zerobits; }
2509 (if (newmask != mask && (newmask & (newmask + 1)) == 0)
2512 /* Only do the transformation if NEWMASK is some integer
2514 for (prec = BITS_PER_UNIT;
2515 prec < HOST_BITS_PER_WIDE_INT; prec <<= 1)
2516 if (newmask == (HOST_WIDE_INT_1U << prec) - 1)
2519 (if (prec < HOST_BITS_PER_WIDE_INT
2520 || newmask == HOST_WIDE_INT_M1U)
2522 { tree newmaskt = build_int_cst_type (TREE_TYPE (@2), newmask); }
2523 (if (!tree_int_cst_equal (newmaskt, @2))
2524 (if (shift_type != TREE_TYPE (@3))
2525 (bit_and (convert (shift:shift_type (convert @3) @1)) { newmaskt; })
2526 (bit_and @4 { newmaskt; })))))))))))))
2528 /* Fold (X {&,^,|} C2) << C1 into (X << C1) {&,^,|} (C2 << C1)
2529 (X {&,^,|} C2) >> C1 into (X >> C1) & (C2 >> C1). */
2530 (for shift (lshift rshift)
2531 (for bit_op (bit_and bit_xor bit_ior)
2533 (shift (convert?:s (bit_op:s @0 INTEGER_CST@2)) INTEGER_CST@1)
2534 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2535 (with { tree mask = int_const_binop (shift, fold_convert (type, @2), @1); }
2536 (bit_op (shift (convert @0) @1) { mask; }))))))
2538 /* ~(~X >> Y) -> X >> Y (for arithmetic shift). */
2540 (bit_not (convert1?:s (rshift:s (convert2?@0 (bit_not @1)) @2)))
2541 (if (!TYPE_UNSIGNED (TREE_TYPE (@0))
2542 && (element_precision (TREE_TYPE (@0))
2543 <= element_precision (TREE_TYPE (@1))
2544 || !TYPE_UNSIGNED (TREE_TYPE (@1))))
2546 { tree shift_type = TREE_TYPE (@0); }
2547 (convert (rshift (convert:shift_type @1) @2)))))
2549 /* ~(~X >>r Y) -> X >>r Y
2550 ~(~X <<r Y) -> X <<r Y */
2551 (for rotate (lrotate rrotate)
2553 (bit_not (convert1?:s (rotate:s (convert2?@0 (bit_not @1)) @2)))
2554 (if ((element_precision (TREE_TYPE (@0))
2555 <= element_precision (TREE_TYPE (@1))
2556 || !TYPE_UNSIGNED (TREE_TYPE (@1)))
2557 && (element_precision (type) <= element_precision (TREE_TYPE (@0))
2558 || !TYPE_UNSIGNED (TREE_TYPE (@0))))
2560 { tree rotate_type = TREE_TYPE (@0); }
2561 (convert (rotate (convert:rotate_type @1) @2))))))
2563 /* Simplifications of conversions. */
2565 /* Basic strip-useless-type-conversions / strip_nops. */
2566 (for cvt (convert view_convert float fix_trunc)
2569 (if ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@0)))
2570 || (GENERIC && type == TREE_TYPE (@0)))
2573 /* Contract view-conversions. */
2575 (view_convert (view_convert @0))
2578 /* For integral conversions with the same precision or pointer
2579 conversions use a NOP_EXPR instead. */
2582 (if ((INTEGRAL_TYPE_P (type) || POINTER_TYPE_P (type))
2583 && (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
2584 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (@0)))
2587 /* Strip inner integral conversions that do not change precision or size, or
2588 zero-extend while keeping the same size (for bool-to-char). */
2590 (view_convert (convert@0 @1))
2591 (if ((INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
2592 && (INTEGRAL_TYPE_P (TREE_TYPE (@1)) || POINTER_TYPE_P (TREE_TYPE (@1)))
2593 && TYPE_SIZE (TREE_TYPE (@0)) == TYPE_SIZE (TREE_TYPE (@1))
2594 && (TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1))
2595 || (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (TREE_TYPE (@1))
2596 && TYPE_UNSIGNED (TREE_TYPE (@1)))))
2599 /* Simplify a view-converted empty constructor. */
2601 (view_convert CONSTRUCTOR@0)
2602 (if (TREE_CODE (@0) != SSA_NAME
2603 && CONSTRUCTOR_NELTS (@0) == 0)
2604 { build_zero_cst (type); }))
2606 /* Re-association barriers around constants and other re-association
2607 barriers can be removed. */
2609 (paren CONSTANT_CLASS_P@0)
2612 (paren (paren@1 @0))
2615 /* Handle cases of two conversions in a row. */
2616 (for ocvt (convert float fix_trunc)
2617 (for icvt (convert float)
2622 tree inside_type = TREE_TYPE (@0);
2623 tree inter_type = TREE_TYPE (@1);
2624 int inside_int = INTEGRAL_TYPE_P (inside_type);
2625 int inside_ptr = POINTER_TYPE_P (inside_type);
2626 int inside_float = FLOAT_TYPE_P (inside_type);
2627 int inside_vec = VECTOR_TYPE_P (inside_type);
2628 unsigned int inside_prec = TYPE_PRECISION (inside_type);
2629 int inside_unsignedp = TYPE_UNSIGNED (inside_type);
2630 int inter_int = INTEGRAL_TYPE_P (inter_type);
2631 int inter_ptr = POINTER_TYPE_P (inter_type);
2632 int inter_float = FLOAT_TYPE_P (inter_type);
2633 int inter_vec = VECTOR_TYPE_P (inter_type);
2634 unsigned int inter_prec = TYPE_PRECISION (inter_type);
2635 int inter_unsignedp = TYPE_UNSIGNED (inter_type);
2636 int final_int = INTEGRAL_TYPE_P (type);
2637 int final_ptr = POINTER_TYPE_P (type);
2638 int final_float = FLOAT_TYPE_P (type);
2639 int final_vec = VECTOR_TYPE_P (type);
2640 unsigned int final_prec = TYPE_PRECISION (type);
2641 int final_unsignedp = TYPE_UNSIGNED (type);
2644 /* In addition to the cases of two conversions in a row
2645 handled below, if we are converting something to its own
2646 type via an object of identical or wider precision, neither
2647 conversion is needed. */
2648 (if (((GIMPLE && useless_type_conversion_p (type, inside_type))
2650 && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (inside_type)))
2651 && (((inter_int || inter_ptr) && final_int)
2652 || (inter_float && final_float))
2653 && inter_prec >= final_prec)
2656 /* Likewise, if the intermediate and initial types are either both
2657 float or both integer, we don't need the middle conversion if the
2658 former is wider than the latter and doesn't change the signedness
2659 (for integers). Avoid this if the final type is a pointer since
2660 then we sometimes need the middle conversion. */
2661 (if (((inter_int && inside_int) || (inter_float && inside_float))
2662 && (final_int || final_float)
2663 && inter_prec >= inside_prec
2664 && (inter_float || inter_unsignedp == inside_unsignedp))
2667 /* If we have a sign-extension of a zero-extended value, we can
2668 replace that by a single zero-extension. Likewise if the
2669 final conversion does not change precision we can drop the
2670 intermediate conversion. */
2671 (if (inside_int && inter_int && final_int
2672 && ((inside_prec < inter_prec && inter_prec < final_prec
2673 && inside_unsignedp && !inter_unsignedp)
2674 || final_prec == inter_prec))
2677 /* Two conversions in a row are not needed unless:
2678 - some conversion is floating-point (overstrict for now), or
2679 - some conversion is a vector (overstrict for now), or
2680 - the intermediate type is narrower than both initial and
2682 - the intermediate type and innermost type differ in signedness,
2683 and the outermost type is wider than the intermediate, or
2684 - the initial type is a pointer type and the precisions of the
2685 intermediate and final types differ, or
2686 - the final type is a pointer type and the precisions of the
2687 initial and intermediate types differ. */
2688 (if (! inside_float && ! inter_float && ! final_float
2689 && ! inside_vec && ! inter_vec && ! final_vec
2690 && (inter_prec >= inside_prec || inter_prec >= final_prec)
2691 && ! (inside_int && inter_int
2692 && inter_unsignedp != inside_unsignedp
2693 && inter_prec < final_prec)
2694 && ((inter_unsignedp && inter_prec > inside_prec)
2695 == (final_unsignedp && final_prec > inter_prec))
2696 && ! (inside_ptr && inter_prec != final_prec)
2697 && ! (final_ptr && inside_prec != inter_prec))
2700 /* A truncation to an unsigned type (a zero-extension) should be
2701 canonicalized as bitwise and of a mask. */
2702 (if (GIMPLE /* PR70366: doing this in GENERIC breaks -Wconversion. */
2703 && final_int && inter_int && inside_int
2704 && final_prec == inside_prec
2705 && final_prec > inter_prec
2707 (convert (bit_and @0 { wide_int_to_tree
2709 wi::mask (inter_prec, false,
2710 TYPE_PRECISION (inside_type))); })))
2712 /* If we are converting an integer to a floating-point that can
2713 represent it exactly and back to an integer, we can skip the
2714 floating-point conversion. */
2715 (if (GIMPLE /* PR66211 */
2716 && inside_int && inter_float && final_int &&
2717 (unsigned) significand_size (TYPE_MODE (inter_type))
2718 >= inside_prec - !inside_unsignedp)
2721 /* If we have a narrowing conversion to an integral type that is fed by a
2722 BIT_AND_EXPR, we might be able to remove the BIT_AND_EXPR if it merely
2723 masks off bits outside the final type (and nothing else). */
2725 (convert (bit_and @0 INTEGER_CST@1))
2726 (if (INTEGRAL_TYPE_P (type)
2727 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
2728 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))
2729 && operand_equal_p (@1, build_low_bits_mask (TREE_TYPE (@1),
2730 TYPE_PRECISION (type)), 0))
2734 /* (X /[ex] A) * A -> X. */
2736 (mult (convert1? (exact_div @0 @@1)) (convert2? @1))
2739 /* Simplify (A / B) * B + (A % B) -> A. */
2740 (for div (trunc_div ceil_div floor_div round_div)
2741 mod (trunc_mod ceil_mod floor_mod round_mod)
2743 (plus:c (mult:c (div @0 @1) @1) (mod @0 @1))
2746 /* ((X /[ex] A) +- B) * A --> X +- A * B. */
2747 (for op (plus minus)
2749 (mult (convert1? (op (convert2? (exact_div @0 INTEGER_CST@@1)) INTEGER_CST@2)) @1)
2750 (if (tree_nop_conversion_p (type, TREE_TYPE (@2))
2751 && tree_nop_conversion_p (TREE_TYPE (@0), TREE_TYPE (@2)))
2754 wi::overflow_type overflow;
2755 wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@2),
2756 TYPE_SIGN (type), &overflow);
2758 (if (types_match (type, TREE_TYPE (@2))
2759 && types_match (TREE_TYPE (@0), TREE_TYPE (@2)) && !overflow)
2760 (op @0 { wide_int_to_tree (type, mul); })
2761 (with { tree utype = unsigned_type_for (type); }
2762 (convert (op (convert:utype @0)
2763 (mult (convert:utype @1) (convert:utype @2))))))))))
2765 /* Canonicalization of binary operations. */
2767 /* Convert X + -C into X - C. */
2769 (plus @0 REAL_CST@1)
2770 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
2771 (with { tree tem = const_unop (NEGATE_EXPR, type, @1); }
2772 (if (!TREE_OVERFLOW (tem) || !flag_trapping_math)
2773 (minus @0 { tem; })))))
2775 /* Convert x+x into x*2. */
2778 (if (SCALAR_FLOAT_TYPE_P (type))
2779 (mult @0 { build_real (type, dconst2); })
2780 (if (INTEGRAL_TYPE_P (type))
2781 (mult @0 { build_int_cst (type, 2); }))))
2785 (minus integer_zerop @1)
2788 (pointer_diff integer_zerop @1)
2789 (negate (convert @1)))
2791 /* (ARG0 - ARG1) is the same as (-ARG1 + ARG0). So check whether
2792 ARG0 is zero and X + ARG0 reduces to X, since that would mean
2793 (-ARG1 + ARG0) reduces to -ARG1. */
2795 (minus real_zerop@0 @1)
2796 (if (fold_real_zero_addition_p (type, @0, 0))
2799 /* Transform x * -1 into -x. */
2801 (mult @0 integer_minus_onep)
2804 /* Reassociate (X * CST) * Y to (X * Y) * CST. This does not introduce
2805 signed overflow for CST != 0 && CST != -1. */
2807 (mult:c (mult:s@3 @0 INTEGER_CST@1) @2)
2808 (if (TREE_CODE (@2) != INTEGER_CST
2810 && !integer_zerop (@1) && !integer_minus_onep (@1))
2811 (mult (mult @0 @2) @1)))
2813 /* True if we can easily extract the real and imaginary parts of a complex
2815 (match compositional_complex
2816 (convert? (complex @0 @1)))
2818 /* COMPLEX_EXPR and REALPART/IMAGPART_EXPR cancellations. */
2820 (complex (realpart @0) (imagpart @0))
2823 (realpart (complex @0 @1))
2826 (imagpart (complex @0 @1))
2829 /* Sometimes we only care about half of a complex expression. */
2831 (realpart (convert?:s (conj:s @0)))
2832 (convert (realpart @0)))
2834 (imagpart (convert?:s (conj:s @0)))
2835 (convert (negate (imagpart @0))))
2836 (for part (realpart imagpart)
2837 (for op (plus minus)
2839 (part (convert?:s@2 (op:s @0 @1)))
2840 (convert (op (part @0) (part @1))))))
2842 (realpart (convert?:s (CEXPI:s @0)))
2845 (imagpart (convert?:s (CEXPI:s @0)))
2848 /* conj(conj(x)) -> x */
2850 (conj (convert? (conj @0)))
2851 (if (tree_nop_conversion_p (TREE_TYPE (@0), type))
2854 /* conj({x,y}) -> {x,-y} */
2856 (conj (convert?:s (complex:s @0 @1)))
2857 (with { tree itype = TREE_TYPE (type); }
2858 (complex (convert:itype @0) (negate (convert:itype @1)))))
2860 /* BSWAP simplifications, transforms checked by gcc.dg/builtin-bswap-8.c. */
2861 (for bswap (BUILT_IN_BSWAP16 BUILT_IN_BSWAP32 BUILT_IN_BSWAP64)
2866 (bswap (bit_not (bswap @0)))
2868 (for bitop (bit_xor bit_ior bit_and)
2870 (bswap (bitop:c (bswap @0) @1))
2871 (bitop @0 (bswap @1)))))
2874 /* Combine COND_EXPRs and VEC_COND_EXPRs. */
2876 /* Simplify constant conditions.
2877 Only optimize constant conditions when the selected branch
2878 has the same type as the COND_EXPR. This avoids optimizing
2879 away "c ? x : throw", where the throw has a void type.
2880 Note that we cannot throw away the fold-const.c variant nor
2881 this one as we depend on doing this transform before possibly
2882 A ? B : B -> B triggers and the fold-const.c one can optimize
2883 0 ? A : B to B even if A has side-effects. Something
2884 genmatch cannot handle. */
2886 (cond INTEGER_CST@0 @1 @2)
2887 (if (integer_zerop (@0))
2888 (if (!VOID_TYPE_P (TREE_TYPE (@2)) || VOID_TYPE_P (type))
2890 (if (!VOID_TYPE_P (TREE_TYPE (@1)) || VOID_TYPE_P (type))
2893 (vec_cond VECTOR_CST@0 @1 @2)
2894 (if (integer_all_onesp (@0))
2896 (if (integer_zerop (@0))
2899 /* Simplification moved from fold_cond_expr_with_comparison. It may also
2901 /* This pattern implements two kinds simplification:
2904 (cond (cmp (convert1? x) c1) (convert2? x) c2) -> (minmax (x c)) if:
2905 1) Conversions are type widening from smaller type.
2906 2) Const c1 equals to c2 after canonicalizing comparison.
2907 3) Comparison has tree code LT, LE, GT or GE.
2908 This specific pattern is needed when (cmp (convert x) c) may not
2909 be simplified by comparison patterns because of multiple uses of
2910 x. It also makes sense here because simplifying across multiple
2911 referred var is always benefitial for complicated cases.
2914 (cond (eq (convert1? x) c1) (convert2? x) c2) -> (cond (eq x c1) c1 c2). */
2915 (for cmp (lt le gt ge eq)
2917 (cond (cmp (convert1? @1) INTEGER_CST@3) (convert2? @1) INTEGER_CST@2)
2920 tree from_type = TREE_TYPE (@1);
2921 tree c1_type = TREE_TYPE (@3), c2_type = TREE_TYPE (@2);
2922 enum tree_code code = ERROR_MARK;
2924 if (INTEGRAL_TYPE_P (from_type)
2925 && int_fits_type_p (@2, from_type)
2926 && (types_match (c1_type, from_type)
2927 || (TYPE_PRECISION (c1_type) > TYPE_PRECISION (from_type)
2928 && (TYPE_UNSIGNED (from_type)
2929 || TYPE_SIGN (c1_type) == TYPE_SIGN (from_type))))
2930 && (types_match (c2_type, from_type)
2931 || (TYPE_PRECISION (c2_type) > TYPE_PRECISION (from_type)
2932 && (TYPE_UNSIGNED (from_type)
2933 || TYPE_SIGN (c2_type) == TYPE_SIGN (from_type)))))
2937 if (wi::to_widest (@3) == (wi::to_widest (@2) - 1))
2939 /* X <= Y - 1 equals to X < Y. */
2942 /* X > Y - 1 equals to X >= Y. */
2946 if (wi::to_widest (@3) == (wi::to_widest (@2) + 1))
2948 /* X < Y + 1 equals to X <= Y. */
2951 /* X >= Y + 1 equals to X > Y. */
2955 if (code != ERROR_MARK
2956 || wi::to_widest (@2) == wi::to_widest (@3))
2958 if (cmp == LT_EXPR || cmp == LE_EXPR)
2960 if (cmp == GT_EXPR || cmp == GE_EXPR)
2964 /* Can do A == C1 ? A : C2 -> A == C1 ? C1 : C2? */
2965 else if (int_fits_type_p (@3, from_type))
2969 (if (code == MAX_EXPR)
2970 (convert (max @1 (convert @2)))
2971 (if (code == MIN_EXPR)
2972 (convert (min @1 (convert @2)))
2973 (if (code == EQ_EXPR)
2974 (convert (cond (eq @1 (convert @3))
2975 (convert:from_type @3) (convert:from_type @2)))))))))
2977 /* (cond (cmp (convert? x) c1) (op x c2) c3) -> (op (minmax x c1) c2) if:
2979 1) OP is PLUS or MINUS.
2980 2) CMP is LT, LE, GT or GE.
2981 3) C3 == (C1 op C2), and computation doesn't have undefined behavior.
2983 This pattern also handles special cases like:
2985 A) Operand x is a unsigned to signed type conversion and c1 is
2986 integer zero. In this case,
2987 (signed type)x < 0 <=> x > MAX_VAL(signed type)
2988 (signed type)x >= 0 <=> x <= MAX_VAL(signed type)
2989 B) Const c1 may not equal to (C3 op' C2). In this case we also
2990 check equality for (c1+1) and (c1-1) by adjusting comparison
2993 TODO: Though signed type is handled by this pattern, it cannot be
2994 simplified at the moment because C standard requires additional
2995 type promotion. In order to match&simplify it here, the IR needs
2996 to be cleaned up by other optimizers, i.e, VRP. */
2997 (for op (plus minus)
2998 (for cmp (lt le gt ge)
3000 (cond (cmp (convert? @X) INTEGER_CST@1) (op @X INTEGER_CST@2) INTEGER_CST@3)
3001 (with { tree from_type = TREE_TYPE (@X), to_type = TREE_TYPE (@1); }
3002 (if (types_match (from_type, to_type)
3003 /* Check if it is special case A). */
3004 || (TYPE_UNSIGNED (from_type)
3005 && !TYPE_UNSIGNED (to_type)
3006 && TYPE_PRECISION (from_type) == TYPE_PRECISION (to_type)
3007 && integer_zerop (@1)
3008 && (cmp == LT_EXPR || cmp == GE_EXPR)))
3011 wi::overflow_type overflow = wi::OVF_NONE;
3012 enum tree_code code, cmp_code = cmp;
3014 wide_int c1 = wi::to_wide (@1);
3015 wide_int c2 = wi::to_wide (@2);
3016 wide_int c3 = wi::to_wide (@3);
3017 signop sgn = TYPE_SIGN (from_type);
3019 /* Handle special case A), given x of unsigned type:
3020 ((signed type)x < 0) <=> (x > MAX_VAL(signed type))
3021 ((signed type)x >= 0) <=> (x <= MAX_VAL(signed type)) */
3022 if (!types_match (from_type, to_type))
3024 if (cmp_code == LT_EXPR)
3026 if (cmp_code == GE_EXPR)
3028 c1 = wi::max_value (to_type);
3030 /* To simplify this pattern, we require c3 = (c1 op c2). Here we
3031 compute (c3 op' c2) and check if it equals to c1 with op' being
3032 the inverted operator of op. Make sure overflow doesn't happen
3033 if it is undefined. */
3034 if (op == PLUS_EXPR)
3035 real_c1 = wi::sub (c3, c2, sgn, &overflow);
3037 real_c1 = wi::add (c3, c2, sgn, &overflow);
3040 if (!overflow || !TYPE_OVERFLOW_UNDEFINED (from_type))
3042 /* Check if c1 equals to real_c1. Boundary condition is handled
3043 by adjusting comparison operation if necessary. */
3044 if (!wi::cmp (wi::sub (real_c1, 1, sgn, &overflow), c1, sgn)
3047 /* X <= Y - 1 equals to X < Y. */
3048 if (cmp_code == LE_EXPR)
3050 /* X > Y - 1 equals to X >= Y. */
3051 if (cmp_code == GT_EXPR)
3054 if (!wi::cmp (wi::add (real_c1, 1, sgn, &overflow), c1, sgn)
3057 /* X < Y + 1 equals to X <= Y. */
3058 if (cmp_code == LT_EXPR)
3060 /* X >= Y + 1 equals to X > Y. */
3061 if (cmp_code == GE_EXPR)
3064 if (code != cmp_code || !wi::cmp (real_c1, c1, sgn))
3066 if (cmp_code == LT_EXPR || cmp_code == LE_EXPR)
3068 if (cmp_code == GT_EXPR || cmp_code == GE_EXPR)
3073 (if (code == MAX_EXPR)
3074 (op (max @X { wide_int_to_tree (from_type, real_c1); })
3075 { wide_int_to_tree (from_type, c2); })
3076 (if (code == MIN_EXPR)
3077 (op (min @X { wide_int_to_tree (from_type, real_c1); })
3078 { wide_int_to_tree (from_type, c2); })))))))))
3080 (for cnd (cond vec_cond)
3081 /* A ? B : (A ? X : C) -> A ? B : C. */
3083 (cnd @0 (cnd @0 @1 @2) @3)
3086 (cnd @0 @1 (cnd @0 @2 @3))
3088 /* A ? B : (!A ? C : X) -> A ? B : C. */
3089 /* ??? This matches embedded conditions open-coded because genmatch
3090 would generate matching code for conditions in separate stmts only.
3091 The following is still important to merge then and else arm cases
3092 from if-conversion. */
3094 (cnd @0 @1 (cnd @2 @3 @4))
3095 (if (inverse_conditions_p (@0, @2))
3098 (cnd @0 (cnd @1 @2 @3) @4)
3099 (if (inverse_conditions_p (@0, @1))
3102 /* A ? B : B -> B. */
3107 /* !A ? B : C -> A ? C : B. */
3109 (cnd (logical_inverted_value truth_valued_p@0) @1 @2)
3112 /* A + (B vcmp C ? 1 : 0) -> A - (B vcmp C ? -1 : 0), since vector comparisons
3113 return all -1 or all 0 results. */
3114 /* ??? We could instead convert all instances of the vec_cond to negate,
3115 but that isn't necessarily a win on its own. */
3117 (plus:c @3 (view_convert? (vec_cond:s @0 integer_each_onep@1 integer_zerop@2)))
3118 (if (VECTOR_TYPE_P (type)
3119 && known_eq (TYPE_VECTOR_SUBPARTS (type),
3120 TYPE_VECTOR_SUBPARTS (TREE_TYPE (@1)))
3121 && (TYPE_MODE (TREE_TYPE (type))
3122 == TYPE_MODE (TREE_TYPE (TREE_TYPE (@1)))))
3123 (minus @3 (view_convert (vec_cond @0 (negate @1) @2)))))
3125 /* ... likewise A - (B vcmp C ? 1 : 0) -> A + (B vcmp C ? -1 : 0). */
3127 (minus @3 (view_convert? (vec_cond:s @0 integer_each_onep@1 integer_zerop@2)))
3128 (if (VECTOR_TYPE_P (type)
3129 && known_eq (TYPE_VECTOR_SUBPARTS (type),
3130 TYPE_VECTOR_SUBPARTS (TREE_TYPE (@1)))
3131 && (TYPE_MODE (TREE_TYPE (type))
3132 == TYPE_MODE (TREE_TYPE (TREE_TYPE (@1)))))
3133 (plus @3 (view_convert (vec_cond @0 (negate @1) @2)))))
3136 /* Simplifications of comparisons. */
3138 /* See if we can reduce the magnitude of a constant involved in a
3139 comparison by changing the comparison code. This is a canonicalization
3140 formerly done by maybe_canonicalize_comparison_1. */
3144 (cmp @0 uniform_integer_cst_p@1)
3145 (with { tree cst = uniform_integer_cst_p (@1); }
3146 (if (tree_int_cst_sgn (cst) == -1)
3147 (acmp @0 { build_uniform_cst (TREE_TYPE (@1),
3148 wide_int_to_tree (TREE_TYPE (cst),
3154 (cmp @0 uniform_integer_cst_p@1)
3155 (with { tree cst = uniform_integer_cst_p (@1); }
3156 (if (tree_int_cst_sgn (cst) == 1)
3157 (acmp @0 { build_uniform_cst (TREE_TYPE (@1),
3158 wide_int_to_tree (TREE_TYPE (cst),
3159 wi::to_wide (cst) - 1)); })))))
3161 /* We can simplify a logical negation of a comparison to the
3162 inverted comparison. As we cannot compute an expression
3163 operator using invert_tree_comparison we have to simulate
3164 that with expression code iteration. */
3165 (for cmp (tcc_comparison)
3166 icmp (inverted_tcc_comparison)
3167 ncmp (inverted_tcc_comparison_with_nans)
3168 /* Ideally we'd like to combine the following two patterns
3169 and handle some more cases by using
3170 (logical_inverted_value (cmp @0 @1))
3171 here but for that genmatch would need to "inline" that.
3172 For now implement what forward_propagate_comparison did. */
3174 (bit_not (cmp @0 @1))
3175 (if (VECTOR_TYPE_P (type)
3176 || (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1))
3177 /* Comparison inversion may be impossible for trapping math,
3178 invert_tree_comparison will tell us. But we can't use
3179 a computed operator in the replacement tree thus we have
3180 to play the trick below. */
3181 (with { enum tree_code ic = invert_tree_comparison
3182 (cmp, HONOR_NANS (@0)); }
3188 (bit_xor (cmp @0 @1) integer_truep)
3189 (with { enum tree_code ic = invert_tree_comparison
3190 (cmp, HONOR_NANS (@0)); }
3196 /* Transform comparisons of the form X - Y CMP 0 to X CMP Y.
3197 ??? The transformation is valid for the other operators if overflow
3198 is undefined for the type, but performing it here badly interacts
3199 with the transformation in fold_cond_expr_with_comparison which
3200 attempts to synthetize ABS_EXPR. */
3202 (for sub (minus pointer_diff)
3204 (cmp (sub@2 @0 @1) integer_zerop)
3205 (if (single_use (@2))
3208 /* Transform comparisons of the form X * C1 CMP 0 to X CMP 0 in the
3209 signed arithmetic case. That form is created by the compiler
3210 often enough for folding it to be of value. One example is in
3211 computing loop trip counts after Operator Strength Reduction. */
3212 (for cmp (simple_comparison)
3213 scmp (swapped_simple_comparison)
3215 (cmp (mult@3 @0 INTEGER_CST@1) integer_zerop@2)
3216 /* Handle unfolded multiplication by zero. */
3217 (if (integer_zerop (@1))
3219 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3220 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
3222 /* If @1 is negative we swap the sense of the comparison. */
3223 (if (tree_int_cst_sgn (@1) < 0)
3227 /* Simplify comparison of something with itself. For IEEE
3228 floating-point, we can only do some of these simplifications. */
3232 (if (! FLOAT_TYPE_P (TREE_TYPE (@0))
3233 || ! HONOR_NANS (@0))
3234 { constant_boolean_node (true, type); }
3235 (if (cmp != EQ_EXPR)
3241 || ! FLOAT_TYPE_P (TREE_TYPE (@0))
3242 || ! HONOR_NANS (@0))
3243 { constant_boolean_node (false, type); })))
3244 (for cmp (unle unge uneq)
3247 { constant_boolean_node (true, type); }))
3248 (for cmp (unlt ungt)
3254 (if (!flag_trapping_math)
3255 { constant_boolean_node (false, type); }))
3257 /* Fold ~X op ~Y as Y op X. */
3258 (for cmp (simple_comparison)
3260 (cmp (bit_not@2 @0) (bit_not@3 @1))
3261 (if (single_use (@2) && single_use (@3))
3264 /* Fold ~X op C as X op' ~C, where op' is the swapped comparison. */
3265 (for cmp (simple_comparison)
3266 scmp (swapped_simple_comparison)
3268 (cmp (bit_not@2 @0) CONSTANT_CLASS_P@1)
3269 (if (single_use (@2)
3270 && (TREE_CODE (@1) == INTEGER_CST || TREE_CODE (@1) == VECTOR_CST))
3271 (scmp @0 (bit_not @1)))))
3273 (for cmp (simple_comparison)
3274 /* Fold (double)float1 CMP (double)float2 into float1 CMP float2. */
3276 (cmp (convert@2 @0) (convert? @1))
3277 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
3278 && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@2))
3279 == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0)))
3280 && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@2))
3281 == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1))))
3284 tree type1 = TREE_TYPE (@1);
3285 if (TREE_CODE (@1) == REAL_CST && !DECIMAL_FLOAT_TYPE_P (type1))
3287 REAL_VALUE_TYPE orig = TREE_REAL_CST (@1);
3288 if (TYPE_PRECISION (type1) > TYPE_PRECISION (float_type_node)
3289 && exact_real_truncate (TYPE_MODE (float_type_node), &orig))
3290 type1 = float_type_node;
3291 if (TYPE_PRECISION (type1) > TYPE_PRECISION (double_type_node)
3292 && exact_real_truncate (TYPE_MODE (double_type_node), &orig))
3293 type1 = double_type_node;
3296 = (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type1)
3297 ? TREE_TYPE (@0) : type1);
3299 (if (TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (newtype))
3300 (cmp (convert:newtype @0) (convert:newtype @1))))))
3304 /* IEEE doesn't distinguish +0 and -0 in comparisons. */
3306 /* a CMP (-0) -> a CMP 0 */
3307 (if (REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@1)))
3308 (cmp @0 { build_real (TREE_TYPE (@1), dconst0); }))
3309 /* x != NaN is always true, other ops are always false. */
3310 (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
3311 && ! HONOR_SNANS (@1))
3312 { constant_boolean_node (cmp == NE_EXPR, type); })
3313 /* Fold comparisons against infinity. */
3314 (if (REAL_VALUE_ISINF (TREE_REAL_CST (@1))
3315 && MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (@1))))
3318 REAL_VALUE_TYPE max;
3319 enum tree_code code = cmp;
3320 bool neg = REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1));
3322 code = swap_tree_comparison (code);
3325 /* x > +Inf is always false, if we ignore NaNs or exceptions. */
3326 (if (code == GT_EXPR
3327 && !(HONOR_NANS (@0) && flag_trapping_math))
3328 { constant_boolean_node (false, type); })
3329 (if (code == LE_EXPR)
3330 /* x <= +Inf is always true, if we don't care about NaNs. */
3331 (if (! HONOR_NANS (@0))
3332 { constant_boolean_node (true, type); }
3333 /* x <= +Inf is the same as x == x, i.e. !isnan(x), but this loses
3334 an "invalid" exception. */
3335 (if (!flag_trapping_math)
3337 /* x == +Inf and x >= +Inf are always equal to x > DBL_MAX, but
3338 for == this introduces an exception for x a NaN. */
3339 (if ((code == EQ_EXPR && !(HONOR_NANS (@0) && flag_trapping_math))
3341 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
3343 (lt @0 { build_real (TREE_TYPE (@0), max); })
3344 (gt @0 { build_real (TREE_TYPE (@0), max); }))))
3345 /* x < +Inf is always equal to x <= DBL_MAX. */
3346 (if (code == LT_EXPR)
3347 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
3349 (ge @0 { build_real (TREE_TYPE (@0), max); })
3350 (le @0 { build_real (TREE_TYPE (@0), max); }))))
3351 /* x != +Inf is always equal to !(x > DBL_MAX), but this introduces
3352 an exception for x a NaN so use an unordered comparison. */
3353 (if (code == NE_EXPR)
3354 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
3355 (if (! HONOR_NANS (@0))
3357 (ge @0 { build_real (TREE_TYPE (@0), max); })
3358 (le @0 { build_real (TREE_TYPE (@0), max); }))
3360 (unge @0 { build_real (TREE_TYPE (@0), max); })
3361 (unle @0 { build_real (TREE_TYPE (@0), max); }))))))))))
3363 /* If this is a comparison of a real constant with a PLUS_EXPR
3364 or a MINUS_EXPR of a real constant, we can convert it into a
3365 comparison with a revised real constant as long as no overflow
3366 occurs when unsafe_math_optimizations are enabled. */
3367 (if (flag_unsafe_math_optimizations)
3368 (for op (plus minus)
3370 (cmp (op @0 REAL_CST@1) REAL_CST@2)
3373 tree tem = const_binop (op == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR,
3374 TREE_TYPE (@1), @2, @1);
3376 (if (tem && !TREE_OVERFLOW (tem))
3377 (cmp @0 { tem; }))))))
3379 /* Likewise, we can simplify a comparison of a real constant with
3380 a MINUS_EXPR whose first operand is also a real constant, i.e.
3381 (c1 - x) < c2 becomes x > c1-c2. Reordering is allowed on
3382 floating-point types only if -fassociative-math is set. */
3383 (if (flag_associative_math)
3385 (cmp (minus REAL_CST@0 @1) REAL_CST@2)
3386 (with { tree tem = const_binop (MINUS_EXPR, TREE_TYPE (@1), @0, @2); }
3387 (if (tem && !TREE_OVERFLOW (tem))
3388 (cmp { tem; } @1)))))
3390 /* Fold comparisons against built-in math functions. */
3391 (if (flag_unsafe_math_optimizations
3392 && ! flag_errno_math)
3395 (cmp (sq @0) REAL_CST@1)
3397 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
3399 /* sqrt(x) < y is always false, if y is negative. */
3400 (if (cmp == EQ_EXPR || cmp == LT_EXPR || cmp == LE_EXPR)
3401 { constant_boolean_node (false, type); })
3402 /* sqrt(x) > y is always true, if y is negative and we
3403 don't care about NaNs, i.e. negative values of x. */
3404 (if (cmp == NE_EXPR || !HONOR_NANS (@0))
3405 { constant_boolean_node (true, type); })
3406 /* sqrt(x) > y is the same as x >= 0, if y is negative. */
3407 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })))
3408 (if (real_equal (TREE_REAL_CST_PTR (@1), &dconst0))
3410 /* sqrt(x) < 0 is always false. */
3411 (if (cmp == LT_EXPR)
3412 { constant_boolean_node (false, type); })
3413 /* sqrt(x) >= 0 is always true if we don't care about NaNs. */
3414 (if (cmp == GE_EXPR && !HONOR_NANS (@0))
3415 { constant_boolean_node (true, type); })
3416 /* sqrt(x) <= 0 -> x == 0. */
3417 (if (cmp == LE_EXPR)
3419 /* Otherwise sqrt(x) cmp 0 -> x cmp 0. Here cmp can be >=, >,
3420 == or !=. In the last case:
3422 (sqrt(x) != 0) == (NaN != 0) == true == (x != 0)
3424 if x is negative or NaN. Due to -funsafe-math-optimizations,
3425 the results for other x follow from natural arithmetic. */
3427 (if (cmp == GT_EXPR || cmp == GE_EXPR)
3431 real_arithmetic (&c2, MULT_EXPR,
3432 &TREE_REAL_CST (@1), &TREE_REAL_CST (@1));
3433 real_convert (&c2, TYPE_MODE (TREE_TYPE (@0)), &c2);
3435 (if (REAL_VALUE_ISINF (c2))
3436 /* sqrt(x) > y is x == +Inf, when y is very large. */
3437 (if (HONOR_INFINITIES (@0))
3438 (eq @0 { build_real (TREE_TYPE (@0), c2); })
3439 { constant_boolean_node (false, type); })
3440 /* sqrt(x) > c is the same as x > c*c. */
3441 (cmp @0 { build_real (TREE_TYPE (@0), c2); }))))
3442 (if (cmp == LT_EXPR || cmp == LE_EXPR)
3446 real_arithmetic (&c2, MULT_EXPR,
3447 &TREE_REAL_CST (@1), &TREE_REAL_CST (@1));
3448 real_convert (&c2, TYPE_MODE (TREE_TYPE (@0)), &c2);
3450 (if (REAL_VALUE_ISINF (c2))
3452 /* sqrt(x) < y is always true, when y is a very large
3453 value and we don't care about NaNs or Infinities. */
3454 (if (! HONOR_NANS (@0) && ! HONOR_INFINITIES (@0))
3455 { constant_boolean_node (true, type); })
3456 /* sqrt(x) < y is x != +Inf when y is very large and we
3457 don't care about NaNs. */
3458 (if (! HONOR_NANS (@0))
3459 (ne @0 { build_real (TREE_TYPE (@0), c2); }))
3460 /* sqrt(x) < y is x >= 0 when y is very large and we
3461 don't care about Infinities. */
3462 (if (! HONOR_INFINITIES (@0))
3463 (ge @0 { build_real (TREE_TYPE (@0), dconst0); }))
3464 /* sqrt(x) < y is x >= 0 && x != +Inf, when y is large. */
3467 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
3468 (ne @0 { build_real (TREE_TYPE (@0), c2); }))))
3469 /* sqrt(x) < c is the same as x < c*c, if we ignore NaNs. */
3470 (if (! HONOR_NANS (@0))
3471 (cmp @0 { build_real (TREE_TYPE (@0), c2); })
3472 /* sqrt(x) < c is the same as x >= 0 && x < c*c. */
3475 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
3476 (cmp @0 { build_real (TREE_TYPE (@0), c2); })))))))))
3477 /* Transform sqrt(x) cmp sqrt(y) -> x cmp y. */
3479 (cmp (sq @0) (sq @1))
3480 (if (! HONOR_NANS (@0))
3483 /* Optimize various special cases of (FTYPE) N CMP (FTYPE) M. */
3484 (for cmp (lt le eq ne ge gt unordered ordered unlt unle ungt unge uneq ltgt)
3485 icmp (lt le eq ne ge gt unordered ordered lt le gt ge eq ne)
3487 (cmp (float@0 @1) (float @2))
3488 (if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (@0))
3489 && ! DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0)))
3492 format_helper fmt (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (@0))));
3493 tree type1 = TREE_TYPE (@1);
3494 bool type1_signed_p = TYPE_SIGN (type1) == SIGNED;
3495 tree type2 = TREE_TYPE (@2);
3496 bool type2_signed_p = TYPE_SIGN (type2) == SIGNED;
3498 (if (fmt.can_represent_integral_type_p (type1)
3499 && fmt.can_represent_integral_type_p (type2))
3500 (if (cmp == ORDERED_EXPR || cmp == UNORDERED_EXPR)
3501 { constant_boolean_node (cmp == ORDERED_EXPR, type); }
3502 (if (TYPE_PRECISION (type1) > TYPE_PRECISION (type2)
3503 && type1_signed_p >= type2_signed_p)
3504 (icmp @1 (convert @2))
3505 (if (TYPE_PRECISION (type1) < TYPE_PRECISION (type2)
3506 && type1_signed_p <= type2_signed_p)
3507 (icmp (convert:type2 @1) @2)
3508 (if (TYPE_PRECISION (type1) == TYPE_PRECISION (type2)
3509 && type1_signed_p == type2_signed_p)
3510 (icmp @1 @2))))))))))
3512 /* Optimize various special cases of (FTYPE) N CMP CST. */
3513 (for cmp (lt le eq ne ge gt)
3514 icmp (le le eq ne ge ge)
3516 (cmp (float @0) REAL_CST@1)
3517 (if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (@1))
3518 && ! DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1)))
3521 tree itype = TREE_TYPE (@0);
3522 format_helper fmt (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (@1))));
3523 const REAL_VALUE_TYPE *cst = TREE_REAL_CST_PTR (@1);
3524 /* Be careful to preserve any potential exceptions due to
3525 NaNs. qNaNs are ok in == or != context.
3526 TODO: relax under -fno-trapping-math or
3527 -fno-signaling-nans. */
3529 = real_isnan (cst) && (cst->signalling
3530 || (cmp != EQ_EXPR && cmp != NE_EXPR));
3532 /* TODO: allow non-fitting itype and SNaNs when
3533 -fno-trapping-math. */
3534 (if (fmt.can_represent_integral_type_p (itype) && ! exception_p)
3537 signop isign = TYPE_SIGN (itype);
3538 REAL_VALUE_TYPE imin, imax;
3539 real_from_integer (&imin, fmt, wi::min_value (itype), isign);
3540 real_from_integer (&imax, fmt, wi::max_value (itype), isign);
3542 REAL_VALUE_TYPE icst;
3543 if (cmp == GT_EXPR || cmp == GE_EXPR)
3544 real_ceil (&icst, fmt, cst);
3545 else if (cmp == LT_EXPR || cmp == LE_EXPR)
3546 real_floor (&icst, fmt, cst);
3548 real_trunc (&icst, fmt, cst);
3550 bool cst_int_p = !real_isnan (cst) && real_identical (&icst, cst);
3552 bool overflow_p = false;
3554 = real_to_integer (&icst, &overflow_p, TYPE_PRECISION (itype));
3557 /* Optimize cases when CST is outside of ITYPE's range. */
3558 (if (real_compare (LT_EXPR, cst, &imin))
3559 { constant_boolean_node (cmp == GT_EXPR || cmp == GE_EXPR || cmp == NE_EXPR,
3561 (if (real_compare (GT_EXPR, cst, &imax))
3562 { constant_boolean_node (cmp == LT_EXPR || cmp == LE_EXPR || cmp == NE_EXPR,
3564 /* Remove cast if CST is an integer representable by ITYPE. */
3566 (cmp @0 { gcc_assert (!overflow_p);
3567 wide_int_to_tree (itype, icst_val); })
3569 /* When CST is fractional, optimize
3570 (FTYPE) N == CST -> 0
3571 (FTYPE) N != CST -> 1. */
3572 (if (cmp == EQ_EXPR || cmp == NE_EXPR)
3573 { constant_boolean_node (cmp == NE_EXPR, type); })
3574 /* Otherwise replace with sensible integer constant. */
3577 gcc_checking_assert (!overflow_p);
3579 (icmp @0 { wide_int_to_tree (itype, icst_val); })))))))))
3581 /* Fold A /[ex] B CMP C to A CMP B * C. */
3584 (cmp (exact_div @0 @1) INTEGER_CST@2)
3585 (if (!integer_zerop (@1))
3586 (if (wi::to_wide (@2) == 0)
3588 (if (TREE_CODE (@1) == INTEGER_CST)
3591 wi::overflow_type ovf;
3592 wide_int prod = wi::mul (wi::to_wide (@2), wi::to_wide (@1),
3593 TYPE_SIGN (TREE_TYPE (@1)), &ovf);
3596 { constant_boolean_node (cmp == NE_EXPR, type); }
3597 (cmp @0 { wide_int_to_tree (TREE_TYPE (@0), prod); }))))))))
3598 (for cmp (lt le gt ge)
3600 (cmp (exact_div @0 INTEGER_CST@1) INTEGER_CST@2)
3601 (if (wi::gt_p (wi::to_wide (@1), 0, TYPE_SIGN (TREE_TYPE (@1))))
3604 wi::overflow_type ovf;
3605 wide_int prod = wi::mul (wi::to_wide (@2), wi::to_wide (@1),
3606 TYPE_SIGN (TREE_TYPE (@1)), &ovf);
3609 { constant_boolean_node (wi::lt_p (wi::to_wide (@2), 0,
3610 TYPE_SIGN (TREE_TYPE (@2)))
3611 != (cmp == LT_EXPR || cmp == LE_EXPR), type); }
3612 (cmp @0 { wide_int_to_tree (TREE_TYPE (@0), prod); }))))))
3614 /* Unordered tests if either argument is a NaN. */
3616 (bit_ior (unordered @0 @0) (unordered @1 @1))
3617 (if (types_match (@0, @1))
3620 (bit_and (ordered @0 @0) (ordered @1 @1))
3621 (if (types_match (@0, @1))
3624 (bit_ior:c (unordered @0 @0) (unordered:c@2 @0 @1))
3627 (bit_and:c (ordered @0 @0) (ordered:c@2 @0 @1))
3630 /* Simple range test simplifications. */
3631 /* A < B || A >= B -> true. */
3632 (for test1 (lt le le le ne ge)
3633 test2 (ge gt ge ne eq ne)
3635 (bit_ior:c (test1 @0 @1) (test2 @0 @1))
3636 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
3637 || VECTOR_INTEGER_TYPE_P (TREE_TYPE (@0)))
3638 { constant_boolean_node (true, type); })))
3639 /* A < B && A >= B -> false. */
3640 (for test1 (lt lt lt le ne eq)
3641 test2 (ge gt eq gt eq gt)
3643 (bit_and:c (test1 @0 @1) (test2 @0 @1))
3644 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
3645 || VECTOR_INTEGER_TYPE_P (TREE_TYPE (@0)))
3646 { constant_boolean_node (false, type); })))
3648 /* A & (2**N - 1) <= 2**K - 1 -> A & (2**N - 2**K) == 0
3649 A & (2**N - 1) > 2**K - 1 -> A & (2**N - 2**K) != 0
3651 Note that comparisons
3652 A & (2**N - 1) < 2**K -> A & (2**N - 2**K) == 0
3653 A & (2**N - 1) >= 2**K -> A & (2**N - 2**K) != 0
3654 will be canonicalized to above so there's no need to
3661 (cmp (bit_and@0 @1 INTEGER_CST@2) INTEGER_CST@3)
3662 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)))
3665 tree ty = TREE_TYPE (@0);
3666 unsigned prec = TYPE_PRECISION (ty);
3667 wide_int mask = wi::to_wide (@2, prec);
3668 wide_int rhs = wi::to_wide (@3, prec);
3669 signop sgn = TYPE_SIGN (ty);
3671 (if ((mask & (mask + 1)) == 0 && wi::gt_p (rhs, 0, sgn)
3672 && (rhs & (rhs + 1)) == 0 && wi::ge_p (mask, rhs, sgn))
3673 (eqcmp (bit_and @1 { wide_int_to_tree (ty, mask - rhs); })
3674 { build_zero_cst (ty); }))))))
3676 /* -A CMP -B -> B CMP A. */
3677 (for cmp (tcc_comparison)
3678 scmp (swapped_tcc_comparison)
3680 (cmp (negate @0) (negate @1))
3681 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
3682 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3683 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
3686 (cmp (negate @0) CONSTANT_CLASS_P@1)
3687 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
3688 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3689 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
3690 (with { tree tem = const_unop (NEGATE_EXPR, TREE_TYPE (@0), @1); }
3691 (if (tem && !TREE_OVERFLOW (tem))
3692 (scmp @0 { tem; }))))))
3694 /* Convert ABS_EXPR<x> == 0 or ABS_EXPR<x> != 0 to x == 0 or x != 0. */
3697 (op (abs @0) zerop@1)
3700 /* From fold_sign_changed_comparison and fold_widened_comparison.
3701 FIXME: the lack of symmetry is disturbing. */
3702 (for cmp (simple_comparison)
3704 (cmp (convert@0 @00) (convert?@1 @10))
3705 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
3706 /* Disable this optimization if we're casting a function pointer
3707 type on targets that require function pointer canonicalization. */
3708 && !(targetm.have_canonicalize_funcptr_for_compare ()
3709 && ((POINTER_TYPE_P (TREE_TYPE (@00))
3710 && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@00))))
3711 || (POINTER_TYPE_P (TREE_TYPE (@10))
3712 && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@10))))))
3714 (if (TYPE_PRECISION (TREE_TYPE (@00)) == TYPE_PRECISION (TREE_TYPE (@0))
3715 && (TREE_CODE (@10) == INTEGER_CST
3717 && (TYPE_UNSIGNED (TREE_TYPE (@00)) == TYPE_UNSIGNED (TREE_TYPE (@0))
3720 && !POINTER_TYPE_P (TREE_TYPE (@00)))
3721 /* ??? The special-casing of INTEGER_CST conversion was in the original
3722 code and here to avoid a spurious overflow flag on the resulting
3723 constant which fold_convert produces. */
3724 (if (TREE_CODE (@1) == INTEGER_CST)
3725 (cmp @00 { force_fit_type (TREE_TYPE (@00), wi::to_widest (@1), 0,
3726 TREE_OVERFLOW (@1)); })
3727 (cmp @00 (convert @1)))
3729 (if (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (TREE_TYPE (@00)))
3730 /* If possible, express the comparison in the shorter mode. */
3731 (if ((cmp == EQ_EXPR || cmp == NE_EXPR
3732 || TYPE_UNSIGNED (TREE_TYPE (@0)) == TYPE_UNSIGNED (TREE_TYPE (@00))
3733 || (!TYPE_UNSIGNED (TREE_TYPE (@0))
3734 && TYPE_UNSIGNED (TREE_TYPE (@00))))
3735 && (types_match (TREE_TYPE (@10), TREE_TYPE (@00))
3736 || ((TYPE_PRECISION (TREE_TYPE (@00))
3737 >= TYPE_PRECISION (TREE_TYPE (@10)))
3738 && (TYPE_UNSIGNED (TREE_TYPE (@00))
3739 == TYPE_UNSIGNED (TREE_TYPE (@10))))
3740 || (TREE_CODE (@10) == INTEGER_CST
3741 && INTEGRAL_TYPE_P (TREE_TYPE (@00))
3742 && int_fits_type_p (@10, TREE_TYPE (@00)))))
3743 (cmp @00 (convert @10))
3744 (if (TREE_CODE (@10) == INTEGER_CST
3745 && INTEGRAL_TYPE_P (TREE_TYPE (@00))
3746 && !int_fits_type_p (@10, TREE_TYPE (@00)))
3749 tree min = lower_bound_in_type (TREE_TYPE (@10), TREE_TYPE (@00));
3750 tree max = upper_bound_in_type (TREE_TYPE (@10), TREE_TYPE (@00));
3751 bool above = integer_nonzerop (const_binop (LT_EXPR, type, max, @10));
3752 bool below = integer_nonzerop (const_binop (LT_EXPR, type, @10, min));
3754 (if (above || below)
3755 (if (cmp == EQ_EXPR || cmp == NE_EXPR)
3756 { constant_boolean_node (cmp == EQ_EXPR ? false : true, type); }
3757 (if (cmp == LT_EXPR || cmp == LE_EXPR)
3758 { constant_boolean_node (above ? true : false, type); }
3759 (if (cmp == GT_EXPR || cmp == GE_EXPR)
3760 { constant_boolean_node (above ? false : true, type); }))))))))))))
3763 /* A local variable can never be pointed to by
3764 the default SSA name of an incoming parameter.
3765 SSA names are canonicalized to 2nd place. */
3767 (cmp addr@0 SSA_NAME@1)
3768 (if (SSA_NAME_IS_DEFAULT_DEF (@1)
3769 && TREE_CODE (SSA_NAME_VAR (@1)) == PARM_DECL)
3770 (with { tree base = get_base_address (TREE_OPERAND (@0, 0)); }
3771 (if (TREE_CODE (base) == VAR_DECL
3772 && auto_var_in_fn_p (base, current_function_decl))
3773 (if (cmp == NE_EXPR)
3774 { constant_boolean_node (true, type); }
3775 { constant_boolean_node (false, type); }))))))
3777 /* Equality compare simplifications from fold_binary */
3780 /* If we have (A | C) == D where C & ~D != 0, convert this into 0.
3781 Similarly for NE_EXPR. */
3783 (cmp (convert?@3 (bit_ior @0 INTEGER_CST@1)) INTEGER_CST@2)
3784 (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0))
3785 && wi::bit_and_not (wi::to_wide (@1), wi::to_wide (@2)) != 0)
3786 { constant_boolean_node (cmp == NE_EXPR, type); }))
3788 /* (X ^ Y) == 0 becomes X == Y, and (X ^ Y) != 0 becomes X != Y. */
3790 (cmp (bit_xor @0 @1) integer_zerop)
3793 /* (X ^ Y) == Y becomes X == 0.
3794 Likewise (X ^ Y) == X becomes Y == 0. */
3796 (cmp:c (bit_xor:c @0 @1) @0)
3797 (cmp @1 { build_zero_cst (TREE_TYPE (@1)); }))
3799 /* (X ^ C1) op C2 can be rewritten as X op (C1 ^ C2). */
3801 (cmp (convert?@3 (bit_xor @0 INTEGER_CST@1)) INTEGER_CST@2)
3802 (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0)))
3803 (cmp @0 (bit_xor @1 (convert @2)))))
3806 (cmp (convert? addr@0) integer_zerop)
3807 (if (tree_single_nonzero_warnv_p (@0, NULL))
3808 { constant_boolean_node (cmp == NE_EXPR, type); })))
3810 /* If we have (A & C) == C where C is a power of 2, convert this into
3811 (A & C) != 0. Similarly for NE_EXPR. */
3815 (cmp (bit_and@2 @0 integer_pow2p@1) @1)
3816 (icmp @2 { build_zero_cst (TREE_TYPE (@0)); })))
3818 /* If we have (A & C) != 0 ? D : 0 where C and D are powers of 2,
3819 convert this into a shift followed by ANDing with D. */
3822 (ne (bit_and @0 integer_pow2p@1) integer_zerop)
3823 INTEGER_CST@2 integer_zerop)
3824 (if (integer_pow2p (@2))
3826 int shift = (wi::exact_log2 (wi::to_wide (@2))
3827 - wi::exact_log2 (wi::to_wide (@1)));
3831 (lshift (convert @0) { build_int_cst (integer_type_node, shift); }) @2)
3833 (convert (rshift @0 { build_int_cst (integer_type_node, -shift); }))
3836 /* If we have (A & C) != 0 where C is the sign bit of A, convert
3837 this into A < 0. Similarly for (A & C) == 0 into A >= 0. */
3841 (cmp (bit_and (convert?@2 @0) integer_pow2p@1) integer_zerop)
3842 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
3843 && type_has_mode_precision_p (TREE_TYPE (@0))
3844 && element_precision (@2) >= element_precision (@0)
3845 && wi::only_sign_bit_p (wi::to_wide (@1), element_precision (@0)))
3846 (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
3847 (ncmp (convert:stype @0) { build_zero_cst (stype); })))))
3849 /* If we have A < 0 ? C : 0 where C is a power of 2, convert
3850 this into a right shift or sign extension followed by ANDing with C. */
3853 (lt @0 integer_zerop)
3854 INTEGER_CST@1 integer_zerop)
3855 (if (integer_pow2p (@1)
3856 && !TYPE_UNSIGNED (TREE_TYPE (@0)))
3858 int shift = element_precision (@0) - wi::exact_log2 (wi::to_wide (@1)) - 1;
3862 (convert (rshift @0 { build_int_cst (integer_type_node, shift); }))
3864 /* Otherwise ctype must be wider than TREE_TYPE (@0) and pure
3865 sign extension followed by AND with C will achieve the effect. */
3866 (bit_and (convert @0) @1)))))
3868 /* When the addresses are not directly of decls compare base and offset.
3869 This implements some remaining parts of fold_comparison address
3870 comparisons but still no complete part of it. Still it is good
3871 enough to make fold_stmt not regress when not dispatching to fold_binary. */
3872 (for cmp (simple_comparison)
3874 (cmp (convert1?@2 addr@0) (convert2? addr@1))
3877 poly_int64 off0, off1;
3878 tree base0 = get_addr_base_and_unit_offset (TREE_OPERAND (@0, 0), &off0);
3879 tree base1 = get_addr_base_and_unit_offset (TREE_OPERAND (@1, 0), &off1);
3880 if (base0 && TREE_CODE (base0) == MEM_REF)
3882 off0 += mem_ref_offset (base0).force_shwi ();
3883 base0 = TREE_OPERAND (base0, 0);
3885 if (base1 && TREE_CODE (base1) == MEM_REF)
3887 off1 += mem_ref_offset (base1).force_shwi ();
3888 base1 = TREE_OPERAND (base1, 0);
3891 (if (base0 && base1)
3895 /* Punt in GENERIC on variables with value expressions;
3896 the value expressions might point to fields/elements
3897 of other vars etc. */
3899 && ((VAR_P (base0) && DECL_HAS_VALUE_EXPR_P (base0))
3900 || (VAR_P (base1) && DECL_HAS_VALUE_EXPR_P (base1))))
3902 else if (decl_in_symtab_p (base0)
3903 && decl_in_symtab_p (base1))
3904 equal = symtab_node::get_create (base0)
3905 ->equal_address_to (symtab_node::get_create (base1));
3906 else if ((DECL_P (base0)
3907 || TREE_CODE (base0) == SSA_NAME
3908 || TREE_CODE (base0) == STRING_CST)
3910 || TREE_CODE (base1) == SSA_NAME
3911 || TREE_CODE (base1) == STRING_CST))
3912 equal = (base0 == base1);
3915 HOST_WIDE_INT ioff0 = -1, ioff1 = -1;
3916 off0.is_constant (&ioff0);
3917 off1.is_constant (&ioff1);
3918 if ((DECL_P (base0) && TREE_CODE (base1) == STRING_CST)
3919 || (TREE_CODE (base0) == STRING_CST && DECL_P (base1))
3920 || (TREE_CODE (base0) == STRING_CST
3921 && TREE_CODE (base1) == STRING_CST
3922 && ioff0 >= 0 && ioff1 >= 0
3923 && ioff0 < TREE_STRING_LENGTH (base0)
3924 && ioff1 < TREE_STRING_LENGTH (base1)
3925 /* This is a too conservative test that the STRING_CSTs
3926 will not end up being string-merged. */
3927 && strncmp (TREE_STRING_POINTER (base0) + ioff0,
3928 TREE_STRING_POINTER (base1) + ioff1,
3929 MIN (TREE_STRING_LENGTH (base0) - ioff0,
3930 TREE_STRING_LENGTH (base1) - ioff1)) != 0))
3932 else if (!DECL_P (base0) || !DECL_P (base1))
3934 else if (cmp != EQ_EXPR && cmp != NE_EXPR)
3936 /* If this is a pointer comparison, ignore for now even
3937 valid equalities where one pointer is the offset zero
3938 of one object and the other to one past end of another one. */
3939 else if (!INTEGRAL_TYPE_P (TREE_TYPE (@2)))
3941 /* Assume that automatic variables can't be adjacent to global
3943 else if (is_global_var (base0) != is_global_var (base1))
3947 tree sz0 = DECL_SIZE_UNIT (base0);
3948 tree sz1 = DECL_SIZE_UNIT (base1);
3949 /* If sizes are unknown, e.g. VLA or not representable,
3951 if (!tree_fits_poly_int64_p (sz0)
3952 || !tree_fits_poly_int64_p (sz1))
3956 poly_int64 size0 = tree_to_poly_int64 (sz0);
3957 poly_int64 size1 = tree_to_poly_int64 (sz1);
3958 /* If one offset is pointing (or could be) to the beginning
3959 of one object and the other is pointing to one past the
3960 last byte of the other object, punt. */
3961 if (maybe_eq (off0, 0) && maybe_eq (off1, size1))
3963 else if (maybe_eq (off1, 0) && maybe_eq (off0, size0))
3965 /* If both offsets are the same, there are some cases
3966 we know that are ok. Either if we know they aren't
3967 zero, or if we know both sizes are no zero. */
3969 && known_eq (off0, off1)
3970 && (known_ne (off0, 0)
3971 || (known_ne (size0, 0) && known_ne (size1, 0))))
3978 && (cmp == EQ_EXPR || cmp == NE_EXPR
3979 /* If the offsets are equal we can ignore overflow. */
3980 || known_eq (off0, off1)
3981 || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
3982 /* Or if we compare using pointers to decls or strings. */
3983 || (POINTER_TYPE_P (TREE_TYPE (@2))
3984 && (DECL_P (base0) || TREE_CODE (base0) == STRING_CST))))
3986 (if (cmp == EQ_EXPR && (known_eq (off0, off1) || known_ne (off0, off1)))
3987 { constant_boolean_node (known_eq (off0, off1), type); })
3988 (if (cmp == NE_EXPR && (known_eq (off0, off1) || known_ne (off0, off1)))
3989 { constant_boolean_node (known_ne (off0, off1), type); })
3990 (if (cmp == LT_EXPR && (known_lt (off0, off1) || known_ge (off0, off1)))
3991 { constant_boolean_node (known_lt (off0, off1), type); })
3992 (if (cmp == LE_EXPR && (known_le (off0, off1) || known_gt (off0, off1)))
3993 { constant_boolean_node (known_le (off0, off1), type); })
3994 (if (cmp == GE_EXPR && (known_ge (off0, off1) || known_lt (off0, off1)))
3995 { constant_boolean_node (known_ge (off0, off1), type); })
3996 (if (cmp == GT_EXPR && (known_gt (off0, off1) || known_le (off0, off1)))
3997 { constant_boolean_node (known_gt (off0, off1), type); }))
4000 (if (cmp == EQ_EXPR)
4001 { constant_boolean_node (false, type); })
4002 (if (cmp == NE_EXPR)
4003 { constant_boolean_node (true, type); })))))))))
4005 /* Simplify pointer equality compares using PTA. */
4009 (if (POINTER_TYPE_P (TREE_TYPE (@0))
4010 && ptrs_compare_unequal (@0, @1))
4011 { constant_boolean_node (neeq != EQ_EXPR, type); })))
4013 /* PR70920: Transform (intptr_t)x eq/ne CST to x eq/ne (typeof x) CST.
4014 and (typeof ptr_cst) x eq/ne ptr_cst to x eq/ne (typeof x) CST.
4015 Disable the transform if either operand is pointer to function.
4016 This broke pr22051-2.c for arm where function pointer
4017 canonicalizaion is not wanted. */
4021 (cmp (convert @0) INTEGER_CST@1)
4022 (if (((POINTER_TYPE_P (TREE_TYPE (@0))
4023 && !FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@0)))
4024 && INTEGRAL_TYPE_P (TREE_TYPE (@1)))
4025 || (INTEGRAL_TYPE_P (TREE_TYPE (@0))
4026 && POINTER_TYPE_P (TREE_TYPE (@1))
4027 && !FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@1)))))
4028 && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
4029 (cmp @0 (convert @1)))))
4031 /* Non-equality compare simplifications from fold_binary */
4032 (for cmp (lt gt le ge)
4033 /* Comparisons with the highest or lowest possible integer of
4034 the specified precision will have known values. */
4036 (cmp (convert?@2 @0) uniform_integer_cst_p@1)
4037 (if ((INTEGRAL_TYPE_P (TREE_TYPE (@1))
4038 || POINTER_TYPE_P (TREE_TYPE (@1))
4039 || VECTOR_INTEGER_TYPE_P (TREE_TYPE (@1)))
4040 && tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@0)))
4043 tree cst = uniform_integer_cst_p (@1);
4044 tree arg1_type = TREE_TYPE (cst);
4045 unsigned int prec = TYPE_PRECISION (arg1_type);
4046 wide_int max = wi::max_value (arg1_type);
4047 wide_int signed_max = wi::max_value (prec, SIGNED);
4048 wide_int min = wi::min_value (arg1_type);
4051 (if (wi::to_wide (cst) == max)
4053 (if (cmp == GT_EXPR)
4054 { constant_boolean_node (false, type); })
4055 (if (cmp == GE_EXPR)
4057 (if (cmp == LE_EXPR)
4058 { constant_boolean_node (true, type); })
4059 (if (cmp == LT_EXPR)
4061 (if (wi::to_wide (cst) == min)
4063 (if (cmp == LT_EXPR)
4064 { constant_boolean_node (false, type); })
4065 (if (cmp == LE_EXPR)
4067 (if (cmp == GE_EXPR)
4068 { constant_boolean_node (true, type); })
4069 (if (cmp == GT_EXPR)
4071 (if (wi::to_wide (cst) == max - 1)
4073 (if (cmp == GT_EXPR)
4074 (eq @2 { build_uniform_cst (TREE_TYPE (@1),
4075 wide_int_to_tree (TREE_TYPE (cst),
4078 (if (cmp == LE_EXPR)
4079 (ne @2 { build_uniform_cst (TREE_TYPE (@1),
4080 wide_int_to_tree (TREE_TYPE (cst),
4083 (if (wi::to_wide (cst) == min + 1)
4085 (if (cmp == GE_EXPR)
4086 (ne @2 { build_uniform_cst (TREE_TYPE (@1),
4087 wide_int_to_tree (TREE_TYPE (cst),
4090 (if (cmp == LT_EXPR)
4091 (eq @2 { build_uniform_cst (TREE_TYPE (@1),
4092 wide_int_to_tree (TREE_TYPE (cst),
4095 (if (wi::to_wide (cst) == signed_max
4096 && TYPE_UNSIGNED (arg1_type)
4097 /* We will flip the signedness of the comparison operator
4098 associated with the mode of @1, so the sign bit is
4099 specified by this mode. Check that @1 is the signed
4100 max associated with this sign bit. */
4101 && prec == GET_MODE_PRECISION (SCALAR_INT_TYPE_MODE (arg1_type))
4102 /* signed_type does not work on pointer types. */
4103 && INTEGRAL_TYPE_P (arg1_type))
4104 /* The following case also applies to X < signed_max+1
4105 and X >= signed_max+1 because previous transformations. */
4106 (if (cmp == LE_EXPR || cmp == GT_EXPR)
4107 (with { tree st = signed_type_for (TREE_TYPE (@1)); }
4109 (if (cst == @1 && cmp == LE_EXPR)
4110 (ge (convert:st @0) { build_zero_cst (st); }))
4111 (if (cst == @1 && cmp == GT_EXPR)
4112 (lt (convert:st @0) { build_zero_cst (st); }))
4113 (if (cmp == LE_EXPR)
4114 (ge (view_convert:st @0) { build_zero_cst (st); }))
4115 (if (cmp == GT_EXPR)
4116 (lt (view_convert:st @0) { build_zero_cst (st); })))))))))))
4118 (for cmp (unordered ordered unlt unle ungt unge uneq ltgt)
4119 /* If the second operand is NaN, the result is constant. */
4122 (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
4123 && (cmp != LTGT_EXPR || ! flag_trapping_math))
4124 { constant_boolean_node (cmp == ORDERED_EXPR || cmp == LTGT_EXPR
4125 ? false : true, type); })))
4127 /* bool_var != 0 becomes bool_var. */
4129 (ne @0 integer_zerop)
4130 (if (TREE_CODE (TREE_TYPE (@0)) == BOOLEAN_TYPE
4131 && types_match (type, TREE_TYPE (@0)))
4133 /* bool_var == 1 becomes bool_var. */
4135 (eq @0 integer_onep)
4136 (if (TREE_CODE (TREE_TYPE (@0)) == BOOLEAN_TYPE
4137 && types_match (type, TREE_TYPE (@0)))
4140 bool_var == 0 becomes !bool_var or
4141 bool_var != 1 becomes !bool_var
4142 here because that only is good in assignment context as long
4143 as we require a tcc_comparison in GIMPLE_CONDs where we'd
4144 replace if (x == 0) with tem = ~x; if (tem != 0) which is
4145 clearly less optimal and which we'll transform again in forwprop. */
4147 /* When one argument is a constant, overflow detection can be simplified.
4148 Currently restricted to single use so as not to interfere too much with
4149 ADD_OVERFLOW detection in tree-ssa-math-opts.c.
4150 A + CST CMP A -> A CMP' CST' */
4151 (for cmp (lt le ge gt)
4154 (cmp:c (plus@2 @0 INTEGER_CST@1) @0)
4155 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
4156 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))
4157 && wi::to_wide (@1) != 0
4159 (with { unsigned int prec = TYPE_PRECISION (TREE_TYPE (@0)); }
4160 (out @0 { wide_int_to_tree (TREE_TYPE (@0),
4161 wi::max_value (prec, UNSIGNED)
4162 - wi::to_wide (@1)); })))))
4164 /* To detect overflow in unsigned A - B, A < B is simpler than A - B > A.
4165 However, the detection logic for SUB_OVERFLOW in tree-ssa-math-opts.c
4166 expects the long form, so we restrict the transformation for now. */
4169 (cmp:c (minus@2 @0 @1) @0)
4170 (if (single_use (@2)
4171 && ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
4172 && TYPE_UNSIGNED (TREE_TYPE (@0))
4173 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
4176 /* Testing for overflow is unnecessary if we already know the result. */
4181 (cmp:c (realpart (IFN_SUB_OVERFLOW@2 @0 @1)) @0)
4182 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
4183 && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
4184 (out (imagpart @2) { build_zero_cst (TREE_TYPE (@0)); }))))
4189 (cmp:c (realpart (IFN_ADD_OVERFLOW:c@2 @0 @1)) @0)
4190 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
4191 && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
4192 (out (imagpart @2) { build_zero_cst (TREE_TYPE (@0)); }))))
4194 /* For unsigned operands, -1 / B < A checks whether A * B would overflow.
4195 Simplify it to __builtin_mul_overflow (A, B, <unused>). */
4199 (cmp:c (trunc_div:s integer_all_onesp @1) @0)
4200 (if (TYPE_UNSIGNED (TREE_TYPE (@0)) && !VECTOR_TYPE_P (TREE_TYPE (@0)))
4201 (with { tree t = TREE_TYPE (@0), cpx = build_complex_type (t); }
4202 (out (imagpart (IFN_MUL_OVERFLOW:cpx @0 @1)) { build_zero_cst (t); })))))
4204 /* Simplification of math builtins. These rules must all be optimizations
4205 as well as IL simplifications. If there is a possibility that the new
4206 form could be a pessimization, the rule should go in the canonicalization
4207 section that follows this one.
4209 Rules can generally go in this section if they satisfy one of
4212 - the rule describes an identity
4214 - the rule replaces calls with something as simple as addition or
4217 - the rule contains unary calls only and simplifies the surrounding
4218 arithmetic. (The idea here is to exclude non-unary calls in which
4219 one operand is constant and in which the call is known to be cheap
4220 when the operand has that value.) */
4222 (if (flag_unsafe_math_optimizations)
4223 /* Simplify sqrt(x) * sqrt(x) -> x. */
4225 (mult (SQRT_ALL@1 @0) @1)
4226 (if (!HONOR_SNANS (type))
4229 (for op (plus minus)
4230 /* Simplify (A / C) +- (B / C) -> (A +- B) / C. */
4234 (rdiv (op @0 @2) @1)))
4236 (for cmp (lt le gt ge)
4237 neg_cmp (gt ge lt le)
4238 /* Simplify (x * C1) cmp C2 -> x cmp (C2 / C1), where C1 != 0. */
4240 (cmp (mult @0 REAL_CST@1) REAL_CST@2)
4242 { tree tem = const_binop (RDIV_EXPR, type, @2, @1); }
4244 && !(REAL_VALUE_ISINF (TREE_REAL_CST (tem))
4245 || (real_zerop (tem) && !real_zerop (@1))))
4247 (if (real_less (&dconst0, TREE_REAL_CST_PTR (@1)))
4249 (if (real_less (TREE_REAL_CST_PTR (@1), &dconst0))
4250 (neg_cmp @0 { tem; })))))))
4252 /* Simplify sqrt(x) * sqrt(y) -> sqrt(x*y). */
4253 (for root (SQRT CBRT)
4255 (mult (root:s @0) (root:s @1))
4256 (root (mult @0 @1))))
4258 /* Simplify expN(x) * expN(y) -> expN(x+y). */
4259 (for exps (EXP EXP2 EXP10 POW10)
4261 (mult (exps:s @0) (exps:s @1))
4262 (exps (plus @0 @1))))
4264 /* Simplify a/root(b/c) into a*root(c/b). */
4265 (for root (SQRT CBRT)
4267 (rdiv @0 (root:s (rdiv:s @1 @2)))
4268 (mult @0 (root (rdiv @2 @1)))))
4270 /* Simplify x/expN(y) into x*expN(-y). */
4271 (for exps (EXP EXP2 EXP10 POW10)
4273 (rdiv @0 (exps:s @1))
4274 (mult @0 (exps (negate @1)))))
4276 (for logs (LOG LOG2 LOG10 LOG10)
4277 exps (EXP EXP2 EXP10 POW10)
4278 /* logN(expN(x)) -> x. */
4282 /* expN(logN(x)) -> x. */
4287 /* Optimize logN(func()) for various exponential functions. We
4288 want to determine the value "x" and the power "exponent" in
4289 order to transform logN(x**exponent) into exponent*logN(x). */
4290 (for logs (LOG LOG LOG LOG2 LOG2 LOG2 LOG10 LOG10)
4291 exps (EXP2 EXP10 POW10 EXP EXP10 POW10 EXP EXP2)
4294 (if (SCALAR_FLOAT_TYPE_P (type))
4300 /* Prepare to do logN(exp(exponent)) -> exponent*logN(e). */
4301 x = build_real_truncate (type, dconst_e ());
4304 /* Prepare to do logN(exp2(exponent)) -> exponent*logN(2). */
4305 x = build_real (type, dconst2);
4309 /* Prepare to do logN(exp10(exponent)) -> exponent*logN(10). */
4311 REAL_VALUE_TYPE dconst10;
4312 real_from_integer (&dconst10, VOIDmode, 10, SIGNED);
4313 x = build_real (type, dconst10);
4320 (mult (logs { x; }) @0)))))
4328 (if (SCALAR_FLOAT_TYPE_P (type))
4334 /* Prepare to do logN(sqrt(x)) -> 0.5*logN(x). */
4335 x = build_real (type, dconsthalf);
4338 /* Prepare to do logN(cbrt(x)) -> (1/3)*logN(x). */
4339 x = build_real_truncate (type, dconst_third ());
4345 (mult { x; } (logs @0))))))
4347 /* logN(pow(x,exponent)) -> exponent*logN(x). */
4348 (for logs (LOG LOG2 LOG10)
4352 (mult @1 (logs @0))))
4354 /* pow(C,x) -> exp(log(C)*x) if C > 0,
4355 or if C is a positive power of 2,
4356 pow(C,x) -> exp2(log2(C)*x). */
4364 (pows REAL_CST@0 @1)
4365 (if (real_compare (GT_EXPR, TREE_REAL_CST_PTR (@0), &dconst0)
4366 && real_isfinite (TREE_REAL_CST_PTR (@0))
4367 /* As libmvec doesn't have a vectorized exp2, defer optimizing
4368 the use_exp2 case until after vectorization. It seems actually
4369 beneficial for all constants to postpone this until later,
4370 because exp(log(C)*x), while faster, will have worse precision
4371 and if x folds into a constant too, that is unnecessary
4373 && canonicalize_math_after_vectorization_p ())
4375 const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (@0);
4376 bool use_exp2 = false;
4377 if (targetm.libc_has_function (function_c99_misc)
4378 && value->cl == rvc_normal)
4380 REAL_VALUE_TYPE frac_rvt = *value;
4381 SET_REAL_EXP (&frac_rvt, 1);
4382 if (real_equal (&frac_rvt, &dconst1))
4387 (if (optimize_pow_to_exp (@0, @1))
4388 (exps (mult (logs @0) @1)))
4389 (exp2s (mult (log2s @0) @1)))))))
4392 /* pow(C,x)*expN(y) -> expN(logN(C)*x+y) if C > 0. */
4394 exps (EXP EXP2 EXP10 POW10)
4395 logs (LOG LOG2 LOG10 LOG10)
4397 (mult:c (pows:s REAL_CST@0 @1) (exps:s @2))
4398 (if (real_compare (GT_EXPR, TREE_REAL_CST_PTR (@0), &dconst0)
4399 && real_isfinite (TREE_REAL_CST_PTR (@0)))
4400 (exps (plus (mult (logs @0) @1) @2)))))
4405 exps (EXP EXP2 EXP10 POW10)
4406 /* sqrt(expN(x)) -> expN(x*0.5). */
4409 (exps (mult @0 { build_real (type, dconsthalf); })))
4410 /* cbrt(expN(x)) -> expN(x/3). */
4413 (exps (mult @0 { build_real_truncate (type, dconst_third ()); })))
4414 /* pow(expN(x), y) -> expN(x*y). */
4417 (exps (mult @0 @1))))
4419 /* tan(atan(x)) -> x. */
4426 /* Simplify sin(atan(x)) -> x / sqrt(x*x + 1). */
4430 copysigns (COPYSIGN)
4435 REAL_VALUE_TYPE r_cst;
4436 build_sinatan_real (&r_cst, type);
4437 tree t_cst = build_real (type, r_cst);
4438 tree t_one = build_one_cst (type);
4440 (if (SCALAR_FLOAT_TYPE_P (type))
4441 (cond (lt (abs @0) { t_cst; })
4442 (rdiv @0 (sqrts (plus (mult @0 @0) { t_one; })))
4443 (copysigns { t_one; } @0))))))
4445 /* Simplify cos(atan(x)) -> 1 / sqrt(x*x + 1). */
4449 copysigns (COPYSIGN)
4454 REAL_VALUE_TYPE r_cst;
4455 build_sinatan_real (&r_cst, type);
4456 tree t_cst = build_real (type, r_cst);
4457 tree t_one = build_one_cst (type);
4458 tree t_zero = build_zero_cst (type);
4460 (if (SCALAR_FLOAT_TYPE_P (type))
4461 (cond (lt (abs @0) { t_cst; })
4462 (rdiv { t_one; } (sqrts (plus (mult @0 @0) { t_one; })))
4463 (copysigns { t_zero; } @0))))))
4465 (if (!flag_errno_math)
4466 /* Simplify sinh(atanh(x)) -> x / sqrt((1 - x)*(1 + x)). */
4471 (sinhs (atanhs:s @0))
4472 (with { tree t_one = build_one_cst (type); }
4473 (rdiv @0 (sqrts (mult (minus { t_one; } @0) (plus { t_one; } @0)))))))
4475 /* Simplify cosh(atanh(x)) -> 1 / sqrt((1 - x)*(1 + x)) */
4480 (coshs (atanhs:s @0))
4481 (with { tree t_one = build_one_cst (type); }
4482 (rdiv { t_one; } (sqrts (mult (minus { t_one; } @0) (plus { t_one; } @0))))))))
4484 /* cabs(x+0i) or cabs(0+xi) -> abs(x). */
4486 (CABS (complex:C @0 real_zerop@1))
4489 /* trunc(trunc(x)) -> trunc(x), etc. */
4490 (for fns (TRUNC_ALL FLOOR_ALL CEIL_ALL ROUND_ALL NEARBYINT_ALL RINT_ALL)
4494 /* f(x) -> x if x is integer valued and f does nothing for such values. */
4495 (for fns (TRUNC_ALL FLOOR_ALL CEIL_ALL ROUND_ALL NEARBYINT_ALL RINT_ALL)
4497 (fns integer_valued_real_p@0)
4500 /* hypot(x,0) and hypot(0,x) -> abs(x). */
4502 (HYPOT:c @0 real_zerop@1)
4505 /* pow(1,x) -> 1. */
4507 (POW real_onep@0 @1)
4511 /* copysign(x,x) -> x. */
4512 (COPYSIGN_ALL @0 @0)
4516 /* copysign(x,y) -> fabs(x) if y is nonnegative. */
4517 (COPYSIGN_ALL @0 tree_expr_nonnegative_p@1)
4520 (for scale (LDEXP SCALBN SCALBLN)
4521 /* ldexp(0, x) -> 0. */
4523 (scale real_zerop@0 @1)
4525 /* ldexp(x, 0) -> x. */
4527 (scale @0 integer_zerop@1)
4529 /* ldexp(x, y) -> x if x is +-Inf or NaN. */
4531 (scale REAL_CST@0 @1)
4532 (if (!real_isfinite (TREE_REAL_CST_PTR (@0)))
4535 /* Canonicalization of sequences of math builtins. These rules represent
4536 IL simplifications but are not necessarily optimizations.
4538 The sincos pass is responsible for picking "optimal" implementations
4539 of math builtins, which may be more complicated and can sometimes go
4540 the other way, e.g. converting pow into a sequence of sqrts.
4541 We only want to do these canonicalizations before the pass has run. */
4543 (if (flag_unsafe_math_optimizations && canonicalize_math_p ())
4544 /* Simplify tan(x) * cos(x) -> sin(x). */
4546 (mult:c (TAN:s @0) (COS:s @0))
4549 /* Simplify x * pow(x,c) -> pow(x,c+1). */
4551 (mult:c @0 (POW:s @0 REAL_CST@1))
4552 (if (!TREE_OVERFLOW (@1))
4553 (POW @0 (plus @1 { build_one_cst (type); }))))
4555 /* Simplify sin(x) / cos(x) -> tan(x). */
4557 (rdiv (SIN:s @0) (COS:s @0))
4560 /* Simplify cos(x) / sin(x) -> 1 / tan(x). */
4562 (rdiv (COS:s @0) (SIN:s @0))
4563 (rdiv { build_one_cst (type); } (TAN @0)))
4565 /* Simplify sin(x) / tan(x) -> cos(x). */
4567 (rdiv (SIN:s @0) (TAN:s @0))
4568 (if (! HONOR_NANS (@0)
4569 && ! HONOR_INFINITIES (@0))
4572 /* Simplify tan(x) / sin(x) -> 1.0 / cos(x). */
4574 (rdiv (TAN:s @0) (SIN:s @0))
4575 (if (! HONOR_NANS (@0)
4576 && ! HONOR_INFINITIES (@0))
4577 (rdiv { build_one_cst (type); } (COS @0))))
4579 /* Simplify pow(x,y) * pow(x,z) -> pow(x,y+z). */
4581 (mult (POW:s @0 @1) (POW:s @0 @2))
4582 (POW @0 (plus @1 @2)))
4584 /* Simplify pow(x,y) * pow(z,y) -> pow(x*z,y). */
4586 (mult (POW:s @0 @1) (POW:s @2 @1))
4587 (POW (mult @0 @2) @1))
4589 /* Simplify powi(x,y) * powi(z,y) -> powi(x*z,y). */
4591 (mult (POWI:s @0 @1) (POWI:s @2 @1))
4592 (POWI (mult @0 @2) @1))
4594 /* Simplify pow(x,c) / x -> pow(x,c-1). */
4596 (rdiv (POW:s @0 REAL_CST@1) @0)
4597 (if (!TREE_OVERFLOW (@1))
4598 (POW @0 (minus @1 { build_one_cst (type); }))))
4600 /* Simplify x / pow (y,z) -> x * pow(y,-z). */
4602 (rdiv @0 (POW:s @1 @2))
4603 (mult @0 (POW @1 (negate @2))))
4608 /* sqrt(sqrt(x)) -> pow(x,1/4). */
4611 (pows @0 { build_real (type, dconst_quarter ()); }))
4612 /* sqrt(cbrt(x)) -> pow(x,1/6). */
4615 (pows @0 { build_real_truncate (type, dconst_sixth ()); }))
4616 /* cbrt(sqrt(x)) -> pow(x,1/6). */
4619 (pows @0 { build_real_truncate (type, dconst_sixth ()); }))
4620 /* cbrt(cbrt(x)) -> pow(x,1/9), iff x is nonnegative. */
4622 (cbrts (cbrts tree_expr_nonnegative_p@0))
4623 (pows @0 { build_real_truncate (type, dconst_ninth ()); }))
4624 /* sqrt(pow(x,y)) -> pow(|x|,y*0.5). */
4626 (sqrts (pows @0 @1))
4627 (pows (abs @0) (mult @1 { build_real (type, dconsthalf); })))
4628 /* cbrt(pow(x,y)) -> pow(x,y/3), iff x is nonnegative. */
4630 (cbrts (pows tree_expr_nonnegative_p@0 @1))
4631 (pows @0 (mult @1 { build_real_truncate (type, dconst_third ()); })))
4632 /* pow(sqrt(x),y) -> pow(x,y*0.5). */
4634 (pows (sqrts @0) @1)
4635 (pows @0 (mult @1 { build_real (type, dconsthalf); })))
4636 /* pow(cbrt(x),y) -> pow(x,y/3) iff x is nonnegative. */
4638 (pows (cbrts tree_expr_nonnegative_p@0) @1)
4639 (pows @0 (mult @1 { build_real_truncate (type, dconst_third ()); })))
4640 /* pow(pow(x,y),z) -> pow(x,y*z) iff x is nonnegative. */
4642 (pows (pows tree_expr_nonnegative_p@0 @1) @2)
4643 (pows @0 (mult @1 @2))))
4645 /* cabs(x+xi) -> fabs(x)*sqrt(2). */
4647 (CABS (complex @0 @0))
4648 (mult (abs @0) { build_real_truncate (type, dconst_sqrt2 ()); }))
4650 /* hypot(x,x) -> fabs(x)*sqrt(2). */
4653 (mult (abs @0) { build_real_truncate (type, dconst_sqrt2 ()); }))
4655 /* cexp(x+yi) -> exp(x)*cexpi(y). */
4660 (cexps compositional_complex@0)
4661 (if (targetm.libc_has_function (function_c99_math_complex))
4663 (mult (exps@1 (realpart @0)) (realpart (cexpis:type@2 (imagpart @0))))
4664 (mult @1 (imagpart @2)))))))
4666 (if (canonicalize_math_p ())
4667 /* floor(x) -> trunc(x) if x is nonnegative. */
4668 (for floors (FLOOR_ALL)
4671 (floors tree_expr_nonnegative_p@0)
4674 (match double_value_p
4676 (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == double_type_node)))
4677 (for froms (BUILT_IN_TRUNCL
4689 /* truncl(extend(x)) -> extend(trunc(x)), etc., if x is a double. */
4690 (if (optimize && canonicalize_math_p ())
4692 (froms (convert double_value_p@0))
4693 (convert (tos @0)))))
4695 (match float_value_p
4697 (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == float_type_node)))
4698 (for froms (BUILT_IN_TRUNCL BUILT_IN_TRUNC
4699 BUILT_IN_FLOORL BUILT_IN_FLOOR
4700 BUILT_IN_CEILL BUILT_IN_CEIL
4701 BUILT_IN_ROUNDL BUILT_IN_ROUND
4702 BUILT_IN_NEARBYINTL BUILT_IN_NEARBYINT
4703 BUILT_IN_RINTL BUILT_IN_RINT)
4704 tos (BUILT_IN_TRUNCF BUILT_IN_TRUNCF
4705 BUILT_IN_FLOORF BUILT_IN_FLOORF
4706 BUILT_IN_CEILF BUILT_IN_CEILF
4707 BUILT_IN_ROUNDF BUILT_IN_ROUNDF
4708 BUILT_IN_NEARBYINTF BUILT_IN_NEARBYINTF
4709 BUILT_IN_RINTF BUILT_IN_RINTF)
4710 /* truncl(extend(x)) and trunc(extend(x)) -> extend(truncf(x)), etc.,
4712 (if (optimize && canonicalize_math_p ()
4713 && targetm.libc_has_function (function_c99_misc))
4715 (froms (convert float_value_p@0))
4716 (convert (tos @0)))))
4718 (for froms (XFLOORL XCEILL XROUNDL XRINTL)
4719 tos (XFLOOR XCEIL XROUND XRINT)
4720 /* llfloorl(extend(x)) -> llfloor(x), etc., if x is a double. */
4721 (if (optimize && canonicalize_math_p ())
4723 (froms (convert double_value_p@0))
4726 (for froms (XFLOORL XCEILL XROUNDL XRINTL
4727 XFLOOR XCEIL XROUND XRINT)
4728 tos (XFLOORF XCEILF XROUNDF XRINTF)
4729 /* llfloorl(extend(x)) and llfloor(extend(x)) -> llfloorf(x), etc.,
4731 (if (optimize && canonicalize_math_p ())
4733 (froms (convert float_value_p@0))
4736 (if (canonicalize_math_p ())
4737 /* xfloor(x) -> fix_trunc(x) if x is nonnegative. */
4738 (for floors (IFLOOR LFLOOR LLFLOOR)
4740 (floors tree_expr_nonnegative_p@0)
4743 (if (canonicalize_math_p ())
4744 /* xfloor(x) -> fix_trunc(x), etc., if x is integer valued. */
4745 (for fns (IFLOOR LFLOOR LLFLOOR
4747 IROUND LROUND LLROUND)
4749 (fns integer_valued_real_p@0)
4751 (if (!flag_errno_math)
4752 /* xrint(x) -> fix_trunc(x), etc., if x is integer valued. */
4753 (for rints (IRINT LRINT LLRINT)
4755 (rints integer_valued_real_p@0)
4758 (if (canonicalize_math_p ())
4759 (for ifn (IFLOOR ICEIL IROUND IRINT)
4760 lfn (LFLOOR LCEIL LROUND LRINT)
4761 llfn (LLFLOOR LLCEIL LLROUND LLRINT)
4762 /* Canonicalize iround (x) to lround (x) on ILP32 targets where
4763 sizeof (int) == sizeof (long). */
4764 (if (TYPE_PRECISION (integer_type_node)
4765 == TYPE_PRECISION (long_integer_type_node))
4768 (lfn:long_integer_type_node @0)))
4769 /* Canonicalize llround (x) to lround (x) on LP64 targets where
4770 sizeof (long long) == sizeof (long). */
4771 (if (TYPE_PRECISION (long_long_integer_type_node)
4772 == TYPE_PRECISION (long_integer_type_node))
4775 (lfn:long_integer_type_node @0)))))
4777 /* cproj(x) -> x if we're ignoring infinities. */
4780 (if (!HONOR_INFINITIES (type))
4783 /* If the real part is inf and the imag part is known to be
4784 nonnegative, return (inf + 0i). */
4786 (CPROJ (complex REAL_CST@0 tree_expr_nonnegative_p@1))
4787 (if (real_isinf (TREE_REAL_CST_PTR (@0)))
4788 { build_complex_inf (type, false); }))
4790 /* If the imag part is inf, return (inf+I*copysign(0,imag)). */
4792 (CPROJ (complex @0 REAL_CST@1))
4793 (if (real_isinf (TREE_REAL_CST_PTR (@1)))
4794 { build_complex_inf (type, TREE_REAL_CST_PTR (@1)->sign); }))
4800 (pows @0 REAL_CST@1)
4802 const REAL_VALUE_TYPE *value = TREE_REAL_CST_PTR (@1);
4803 REAL_VALUE_TYPE tmp;
4806 /* pow(x,0) -> 1. */
4807 (if (real_equal (value, &dconst0))
4808 { build_real (type, dconst1); })
4809 /* pow(x,1) -> x. */
4810 (if (real_equal (value, &dconst1))
4812 /* pow(x,-1) -> 1/x. */
4813 (if (real_equal (value, &dconstm1))
4814 (rdiv { build_real (type, dconst1); } @0))
4815 /* pow(x,0.5) -> sqrt(x). */
4816 (if (flag_unsafe_math_optimizations
4817 && canonicalize_math_p ()
4818 && real_equal (value, &dconsthalf))
4820 /* pow(x,1/3) -> cbrt(x). */
4821 (if (flag_unsafe_math_optimizations
4822 && canonicalize_math_p ()
4823 && (tmp = real_value_truncate (TYPE_MODE (type), dconst_third ()),
4824 real_equal (value, &tmp)))
4827 /* powi(1,x) -> 1. */
4829 (POWI real_onep@0 @1)
4833 (POWI @0 INTEGER_CST@1)
4835 /* powi(x,0) -> 1. */
4836 (if (wi::to_wide (@1) == 0)
4837 { build_real (type, dconst1); })
4838 /* powi(x,1) -> x. */
4839 (if (wi::to_wide (@1) == 1)
4841 /* powi(x,-1) -> 1/x. */
4842 (if (wi::to_wide (@1) == -1)
4843 (rdiv { build_real (type, dconst1); } @0))))
4845 /* Narrowing of arithmetic and logical operations.
4847 These are conceptually similar to the transformations performed for
4848 the C/C++ front-ends by shorten_binary_op and shorten_compare. Long
4849 term we want to move all that code out of the front-ends into here. */
4851 /* If we have a narrowing conversion of an arithmetic operation where
4852 both operands are widening conversions from the same type as the outer
4853 narrowing conversion. Then convert the innermost operands to a suitable
4854 unsigned type (to avoid introducing undefined behavior), perform the
4855 operation and convert the result to the desired type. */
4856 (for op (plus minus)
4858 (convert (op:s (convert@2 @0) (convert?@3 @1)))
4859 (if (INTEGRAL_TYPE_P (type)
4860 /* We check for type compatibility between @0 and @1 below,
4861 so there's no need to check that @1/@3 are integral types. */
4862 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
4863 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
4864 /* The precision of the type of each operand must match the
4865 precision of the mode of each operand, similarly for the
4867 && type_has_mode_precision_p (TREE_TYPE (@0))
4868 && type_has_mode_precision_p (TREE_TYPE (@1))
4869 && type_has_mode_precision_p (type)
4870 /* The inner conversion must be a widening conversion. */
4871 && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
4872 && types_match (@0, type)
4873 && (types_match (@0, @1)
4874 /* Or the second operand is const integer or converted const
4875 integer from valueize. */
4876 || TREE_CODE (@1) == INTEGER_CST))
4877 (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
4878 (op @0 (convert @1))
4879 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
4880 (convert (op (convert:utype @0)
4881 (convert:utype @1))))))))
4883 /* This is another case of narrowing, specifically when there's an outer
4884 BIT_AND_EXPR which masks off bits outside the type of the innermost
4885 operands. Like the previous case we have to convert the operands
4886 to unsigned types to avoid introducing undefined behavior for the
4887 arithmetic operation. */
4888 (for op (minus plus)
4890 (bit_and (op:s (convert@2 @0) (convert@3 @1)) INTEGER_CST@4)
4891 (if (INTEGRAL_TYPE_P (type)
4892 /* We check for type compatibility between @0 and @1 below,
4893 so there's no need to check that @1/@3 are integral types. */
4894 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
4895 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
4896 /* The precision of the type of each operand must match the
4897 precision of the mode of each operand, similarly for the
4899 && type_has_mode_precision_p (TREE_TYPE (@0))
4900 && type_has_mode_precision_p (TREE_TYPE (@1))
4901 && type_has_mode_precision_p (type)
4902 /* The inner conversion must be a widening conversion. */
4903 && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
4904 && types_match (@0, @1)
4905 && (tree_int_cst_min_precision (@4, TYPE_SIGN (TREE_TYPE (@0)))
4906 <= TYPE_PRECISION (TREE_TYPE (@0)))
4907 && (wi::to_wide (@4)
4908 & wi::mask (TYPE_PRECISION (TREE_TYPE (@0)),
4909 true, TYPE_PRECISION (type))) == 0)
4910 (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
4911 (with { tree ntype = TREE_TYPE (@0); }
4912 (convert (bit_and (op @0 @1) (convert:ntype @4))))
4913 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
4914 (convert (bit_and (op (convert:utype @0) (convert:utype @1))
4915 (convert:utype @4))))))))
4917 /* Transform (@0 < @1 and @0 < @2) to use min,
4918 (@0 > @1 and @0 > @2) to use max */
4919 (for logic (bit_and bit_and bit_and bit_and bit_ior bit_ior bit_ior bit_ior)
4920 op (lt le gt ge lt le gt ge )
4921 ext (min min max max max max min min )
4923 (logic (op:cs @0 @1) (op:cs @0 @2))
4924 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
4925 && TREE_CODE (@0) != INTEGER_CST)
4926 (op @0 (ext @1 @2)))))
4929 /* signbit(x) -> 0 if x is nonnegative. */
4930 (SIGNBIT tree_expr_nonnegative_p@0)
4931 { integer_zero_node; })
4934 /* signbit(x) -> x<0 if x doesn't have signed zeros. */
4936 (if (!HONOR_SIGNED_ZEROS (@0))
4937 (convert (lt @0 { build_real (TREE_TYPE (@0), dconst0); }))))
4939 /* Transform comparisons of the form X +- C1 CMP C2 to X CMP C2 -+ C1. */
4941 (for op (plus minus)
4944 (cmp (op@3 @0 INTEGER_CST@1) INTEGER_CST@2)
4945 (if (!TREE_OVERFLOW (@1) && !TREE_OVERFLOW (@2)
4946 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0))
4947 && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@0))
4948 && !TYPE_SATURATING (TREE_TYPE (@0)))
4949 (with { tree res = int_const_binop (rop, @2, @1); }
4950 (if (TREE_OVERFLOW (res)
4951 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
4952 { constant_boolean_node (cmp == NE_EXPR, type); }
4953 (if (single_use (@3))
4954 (cmp @0 { TREE_OVERFLOW (res)
4955 ? drop_tree_overflow (res) : res; }))))))))
4956 (for cmp (lt le gt ge)
4957 (for op (plus minus)
4960 (cmp (op@3 @0 INTEGER_CST@1) INTEGER_CST@2)
4961 (if (!TREE_OVERFLOW (@1) && !TREE_OVERFLOW (@2)
4962 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
4963 (with { tree res = int_const_binop (rop, @2, @1); }
4964 (if (TREE_OVERFLOW (res))
4966 fold_overflow_warning (("assuming signed overflow does not occur "
4967 "when simplifying conditional to constant"),
4968 WARN_STRICT_OVERFLOW_CONDITIONAL);
4969 bool less = cmp == LE_EXPR || cmp == LT_EXPR;
4970 /* wi::ges_p (@2, 0) should be sufficient for a signed type. */
4971 bool ovf_high = wi::lt_p (wi::to_wide (@1), 0,
4972 TYPE_SIGN (TREE_TYPE (@1)))
4973 != (op == MINUS_EXPR);
4974 constant_boolean_node (less == ovf_high, type);
4976 (if (single_use (@3))
4979 fold_overflow_warning (("assuming signed overflow does not occur "
4980 "when changing X +- C1 cmp C2 to "
4982 WARN_STRICT_OVERFLOW_COMPARISON);
4984 (cmp @0 { res; })))))))))
4986 /* Canonicalizations of BIT_FIELD_REFs. */
4989 (BIT_FIELD_REF (BIT_FIELD_REF @0 @1 @2) @3 @4)
4990 (BIT_FIELD_REF @0 @3 { const_binop (PLUS_EXPR, bitsizetype, @2, @4); }))
4993 (BIT_FIELD_REF (view_convert @0) @1 @2)
4994 (BIT_FIELD_REF @0 @1 @2))
4997 (BIT_FIELD_REF @0 @1 integer_zerop)
4998 (if (tree_int_cst_equal (@1, TYPE_SIZE (TREE_TYPE (@0))))
5002 (BIT_FIELD_REF @0 @1 @2)
5004 (if (TREE_CODE (TREE_TYPE (@0)) == COMPLEX_TYPE
5005 && tree_int_cst_equal (@1, TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))))
5007 (if (integer_zerop (@2))
5008 (view_convert (realpart @0)))
5009 (if (tree_int_cst_equal (@2, TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))))
5010 (view_convert (imagpart @0)))))
5011 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
5012 && INTEGRAL_TYPE_P (type)
5013 /* On GIMPLE this should only apply to register arguments. */
5014 && (! GIMPLE || is_gimple_reg (@0))
5015 /* A bit-field-ref that referenced the full argument can be stripped. */
5016 && ((compare_tree_int (@1, TYPE_PRECISION (TREE_TYPE (@0))) == 0
5017 && integer_zerop (@2))
5018 /* Low-parts can be reduced to integral conversions.
5019 ??? The following doesn't work for PDP endian. */
5020 || (BYTES_BIG_ENDIAN == WORDS_BIG_ENDIAN
5021 /* Don't even think about BITS_BIG_ENDIAN. */
5022 && TYPE_PRECISION (TREE_TYPE (@0)) % BITS_PER_UNIT == 0
5023 && TYPE_PRECISION (type) % BITS_PER_UNIT == 0
5024 && compare_tree_int (@2, (BYTES_BIG_ENDIAN
5025 ? (TYPE_PRECISION (TREE_TYPE (@0))
5026 - TYPE_PRECISION (type))
5030 /* Simplify vector extracts. */
5033 (BIT_FIELD_REF CONSTRUCTOR@0 @1 @2)
5034 (if (VECTOR_TYPE_P (TREE_TYPE (@0))
5035 && (types_match (type, TREE_TYPE (TREE_TYPE (@0)))
5036 || (VECTOR_TYPE_P (type)
5037 && types_match (TREE_TYPE (type), TREE_TYPE (TREE_TYPE (@0))))))
5040 tree ctor = (TREE_CODE (@0) == SSA_NAME
5041 ? gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0)) : @0);
5042 tree eltype = TREE_TYPE (TREE_TYPE (ctor));
5043 unsigned HOST_WIDE_INT width = tree_to_uhwi (TYPE_SIZE (eltype));
5044 unsigned HOST_WIDE_INT n = tree_to_uhwi (@1);
5045 unsigned HOST_WIDE_INT idx = tree_to_uhwi (@2);
5048 && (idx % width) == 0
5050 && known_le ((idx + n) / width,
5051 TYPE_VECTOR_SUBPARTS (TREE_TYPE (ctor))))
5056 /* Constructor elements can be subvectors. */
5058 if (CONSTRUCTOR_NELTS (ctor) != 0)
5060 tree cons_elem = TREE_TYPE (CONSTRUCTOR_ELT (ctor, 0)->value);
5061 if (TREE_CODE (cons_elem) == VECTOR_TYPE)
5062 k = TYPE_VECTOR_SUBPARTS (cons_elem);
5064 unsigned HOST_WIDE_INT elt, count, const_k;
5067 /* We keep an exact subset of the constructor elements. */
5068 (if (multiple_p (idx, k, &elt) && multiple_p (n, k, &count))
5069 (if (CONSTRUCTOR_NELTS (ctor) == 0)
5070 { build_constructor (type, NULL); }
5072 (if (elt < CONSTRUCTOR_NELTS (ctor))
5073 (view_convert { CONSTRUCTOR_ELT (ctor, elt)->value; })
5074 { build_zero_cst (type); })
5076 vec<constructor_elt, va_gc> *vals;
5077 vec_alloc (vals, count);
5078 for (unsigned i = 0;
5079 i < count && elt + i < CONSTRUCTOR_NELTS (ctor); ++i)
5080 CONSTRUCTOR_APPEND_ELT (vals, NULL_TREE,
5081 CONSTRUCTOR_ELT (ctor, elt + i)->value);
5082 build_constructor (type, vals);
5084 /* The bitfield references a single constructor element. */
5085 (if (k.is_constant (&const_k)
5086 && idx + n <= (idx / const_k + 1) * const_k)
5088 (if (CONSTRUCTOR_NELTS (ctor) <= idx / const_k)
5089 { build_zero_cst (type); })
5091 (view_convert { CONSTRUCTOR_ELT (ctor, idx / const_k)->value; }))
5092 (BIT_FIELD_REF { CONSTRUCTOR_ELT (ctor, idx / const_k)->value; }
5093 @1 { bitsize_int ((idx % const_k) * width); })))))))))
5095 /* Simplify a bit extraction from a bit insertion for the cases with
5096 the inserted element fully covering the extraction or the insertion
5097 not touching the extraction. */
5099 (BIT_FIELD_REF (bit_insert @0 @1 @ipos) @rsize @rpos)
5102 unsigned HOST_WIDE_INT isize;
5103 if (INTEGRAL_TYPE_P (TREE_TYPE (@1)))
5104 isize = TYPE_PRECISION (TREE_TYPE (@1));
5106 isize = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (@1)));
5109 (if (wi::leu_p (wi::to_wide (@ipos), wi::to_wide (@rpos))
5110 && wi::leu_p (wi::to_wide (@rpos) + wi::to_wide (@rsize),
5111 wi::to_wide (@ipos) + isize))
5112 (BIT_FIELD_REF @1 @rsize { wide_int_to_tree (bitsizetype,
5114 - wi::to_wide (@ipos)); }))
5115 (if (wi::geu_p (wi::to_wide (@ipos),
5116 wi::to_wide (@rpos) + wi::to_wide (@rsize))
5117 || wi::geu_p (wi::to_wide (@rpos),
5118 wi::to_wide (@ipos) + isize))
5119 (BIT_FIELD_REF @0 @rsize @rpos)))))
5121 (if (canonicalize_math_after_vectorization_p ())
5124 (fmas:c (negate @0) @1 @2)
5125 (IFN_FNMA @0 @1 @2))
5127 (fmas @0 @1 (negate @2))
5130 (fmas:c (negate @0) @1 (negate @2))
5131 (IFN_FNMS @0 @1 @2))
5133 (negate (fmas@3 @0 @1 @2))
5134 (if (single_use (@3))
5135 (IFN_FNMS @0 @1 @2))))
5138 (IFN_FMS:c (negate @0) @1 @2)
5139 (IFN_FNMS @0 @1 @2))
5141 (IFN_FMS @0 @1 (negate @2))
5144 (IFN_FMS:c (negate @0) @1 (negate @2))
5145 (IFN_FNMA @0 @1 @2))
5147 (negate (IFN_FMS@3 @0 @1 @2))
5148 (if (single_use (@3))
5149 (IFN_FNMA @0 @1 @2)))
5152 (IFN_FNMA:c (negate @0) @1 @2)
5155 (IFN_FNMA @0 @1 (negate @2))
5156 (IFN_FNMS @0 @1 @2))
5158 (IFN_FNMA:c (negate @0) @1 (negate @2))
5161 (negate (IFN_FNMA@3 @0 @1 @2))
5162 (if (single_use (@3))
5163 (IFN_FMS @0 @1 @2)))
5166 (IFN_FNMS:c (negate @0) @1 @2)
5169 (IFN_FNMS @0 @1 (negate @2))
5170 (IFN_FNMA @0 @1 @2))
5172 (IFN_FNMS:c (negate @0) @1 (negate @2))
5175 (negate (IFN_FNMS@3 @0 @1 @2))
5176 (if (single_use (@3))
5177 (IFN_FMA @0 @1 @2))))
5179 /* POPCOUNT simplifications. */
5180 (for popcount (BUILT_IN_POPCOUNT BUILT_IN_POPCOUNTL BUILT_IN_POPCOUNTLL
5181 BUILT_IN_POPCOUNTIMAX)
5182 /* popcount(X&1) is nop_expr(X&1). */
5185 (if (tree_nonzero_bits (@0) == 1)
5187 /* popcount(X) + popcount(Y) is popcount(X|Y) when X&Y must be zero. */
5189 (plus (popcount:s @0) (popcount:s @1))
5190 (if (wi::bit_and (tree_nonzero_bits (@0), tree_nonzero_bits (@1)) == 0)
5191 (popcount (bit_ior @0 @1))))
5192 /* popcount(X) == 0 is X == 0, and related (in)equalities. */
5193 (for cmp (le eq ne gt)
5196 (cmp (popcount @0) integer_zerop)
5197 (rep @0 { build_zero_cst (TREE_TYPE (@0)); }))))
5206 r = c ? a1 op a2 : b;
5208 if the target can do it in one go. This makes the operation conditional
5209 on c, so could drop potentially-trapping arithmetic, but that's a valid
5210 simplification if the result of the operation isn't needed.
5212 Avoid speculatively generating a stand-alone vector comparison
5213 on targets that might not support them. Any target implementing
5214 conditional internal functions must support the same comparisons
5215 inside and outside a VEC_COND_EXPR. */
5218 (for uncond_op (UNCOND_BINARY)
5219 cond_op (COND_BINARY)
5221 (vec_cond @0 (view_convert? (uncond_op@4 @1 @2)) @3)
5222 (with { tree op_type = TREE_TYPE (@4); }
5223 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
5224 && element_precision (type) == element_precision (op_type))
5225 (view_convert (cond_op @0 @1 @2 (view_convert:op_type @3))))))
5227 (vec_cond @0 @1 (view_convert? (uncond_op@4 @2 @3)))
5228 (with { tree op_type = TREE_TYPE (@4); }
5229 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
5230 && element_precision (type) == element_precision (op_type))
5231 (view_convert (cond_op (bit_not @0) @2 @3 (view_convert:op_type @1)))))))
5233 /* Same for ternary operations. */
5234 (for uncond_op (UNCOND_TERNARY)
5235 cond_op (COND_TERNARY)
5237 (vec_cond @0 (view_convert? (uncond_op@5 @1 @2 @3)) @4)
5238 (with { tree op_type = TREE_TYPE (@5); }
5239 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
5240 && element_precision (type) == element_precision (op_type))
5241 (view_convert (cond_op @0 @1 @2 @3 (view_convert:op_type @4))))))
5243 (vec_cond @0 @1 (view_convert? (uncond_op@5 @2 @3 @4)))
5244 (with { tree op_type = TREE_TYPE (@5); }
5245 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
5246 && element_precision (type) == element_precision (op_type))
5247 (view_convert (cond_op (bit_not @0) @2 @3 @4
5248 (view_convert:op_type @1)))))))
5251 /* Detect cases in which a VEC_COND_EXPR effectively replaces the
5252 "else" value of an IFN_COND_*. */
5253 (for cond_op (COND_BINARY)
5255 (vec_cond @0 (view_convert? (cond_op @0 @1 @2 @3)) @4)
5256 (with { tree op_type = TREE_TYPE (@3); }
5257 (if (element_precision (type) == element_precision (op_type))
5258 (view_convert (cond_op @0 @1 @2 (view_convert:op_type @4))))))
5260 (vec_cond @0 @1 (view_convert? (cond_op @2 @3 @4 @5)))
5261 (with { tree op_type = TREE_TYPE (@5); }
5262 (if (inverse_conditions_p (@0, @2)
5263 && element_precision (type) == element_precision (op_type))
5264 (view_convert (cond_op @2 @3 @4 (view_convert:op_type @1)))))))
5266 /* Same for ternary operations. */
5267 (for cond_op (COND_TERNARY)
5269 (vec_cond @0 (view_convert? (cond_op @0 @1 @2 @3 @4)) @5)
5270 (with { tree op_type = TREE_TYPE (@4); }
5271 (if (element_precision (type) == element_precision (op_type))
5272 (view_convert (cond_op @0 @1 @2 @3 (view_convert:op_type @5))))))
5274 (vec_cond @0 @1 (view_convert? (cond_op @2 @3 @4 @5 @6)))
5275 (with { tree op_type = TREE_TYPE (@6); }
5276 (if (inverse_conditions_p (@0, @2)
5277 && element_precision (type) == element_precision (op_type))
5278 (view_convert (cond_op @2 @3 @4 @5 (view_convert:op_type @1)))))))
5280 /* For pointers @0 and @2 and nonnegative constant offset @1, look for
5283 A: (@0 + @1 < @2) | (@2 + @1 < @0)
5284 B: (@0 + @1 <= @2) | (@2 + @1 <= @0)
5286 If pointers are known not to wrap, B checks whether @1 bytes starting
5287 at @0 and @2 do not overlap, while A tests the same thing for @1 + 1
5288 bytes. A is more efficiently tested as:
5290 A: (sizetype) (@0 + @1 - @2) > @1 * 2
5292 The equivalent expression for B is given by replacing @1 with @1 - 1:
5294 B: (sizetype) (@0 + (@1 - 1) - @2) > (@1 - 1) * 2
5296 @0 and @2 can be swapped in both expressions without changing the result.
5298 The folds rely on sizetype's being unsigned (which is always true)
5299 and on its being the same width as the pointer (which we have to check).
5301 The fold replaces two pointer_plus expressions, two comparisons and
5302 an IOR with a pointer_plus, a pointer_diff, and a comparison, so in
5303 the best case it's a saving of two operations. The A fold retains one
5304 of the original pointer_pluses, so is a win even if both pointer_pluses
5305 are used elsewhere. The B fold is a wash if both pointer_pluses are
5306 used elsewhere, since all we end up doing is replacing a comparison with
5307 a pointer_plus. We do still apply the fold under those circumstances
5308 though, in case applying it to other conditions eventually makes one of the
5309 pointer_pluses dead. */
5310 (for ior (truth_orif truth_or bit_ior)
5313 (ior (cmp:cs (pointer_plus@3 @0 INTEGER_CST@1) @2)
5314 (cmp:cs (pointer_plus@4 @2 @1) @0))
5315 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
5316 && TYPE_OVERFLOW_WRAPS (sizetype)
5317 && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (sizetype))
5318 /* Calculate the rhs constant. */
5319 (with { offset_int off = wi::to_offset (@1) - (cmp == LE_EXPR ? 1 : 0);
5320 offset_int rhs = off * 2; }
5321 /* Always fails for negative values. */
5322 (if (wi::min_precision (rhs, UNSIGNED) <= TYPE_PRECISION (sizetype))
5323 /* Since the order of @0 and @2 doesn't matter, let tree_swap_operands_p
5324 pick a canonical order. This increases the chances of using the
5325 same pointer_plus in multiple checks. */
5326 (with { bool swap_p = tree_swap_operands_p (@0, @2);
5327 tree rhs_tree = wide_int_to_tree (sizetype, rhs); }
5328 (if (cmp == LT_EXPR)
5329 (gt (convert:sizetype
5330 (pointer_diff:ssizetype { swap_p ? @4 : @3; }
5331 { swap_p ? @0 : @2; }))
5333 (gt (convert:sizetype
5334 (pointer_diff:ssizetype
5335 (pointer_plus { swap_p ? @2 : @0; }
5336 { wide_int_to_tree (sizetype, off); })
5337 { swap_p ? @0 : @2; }))
5338 { rhs_tree; })))))))))
5340 /* Fold REDUC (@0 & @1) -> @0[I] & @1[I] if element I is the only nonzero
5342 (for reduc (IFN_REDUC_PLUS IFN_REDUC_IOR IFN_REDUC_XOR)
5343 (simplify (reduc (view_convert? (bit_and @0 VECTOR_CST@1)))
5344 (with { int i = single_nonzero_element (@1); }
5346 (with { tree elt = vector_cst_elt (@1, i);
5347 tree elt_type = TREE_TYPE (elt);
5348 unsigned int elt_bits = tree_to_uhwi (TYPE_SIZE (elt_type));
5349 tree size = bitsize_int (elt_bits);
5350 tree pos = bitsize_int (elt_bits * i); }
5353 (BIT_FIELD_REF:elt_type @0 { size; } { pos; })