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-2021 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
43 (define_operator_list tcc_comparison
44 lt le eq ne ge gt unordered ordered unlt unle ungt unge uneq ltgt)
45 (define_operator_list inverted_tcc_comparison
46 ge gt ne eq lt le ordered unordered ge gt le lt ltgt uneq)
47 (define_operator_list inverted_tcc_comparison_with_nans
48 unge ungt ne eq unlt unle ordered unordered ge gt le lt ltgt uneq)
49 (define_operator_list swapped_tcc_comparison
50 gt ge eq ne le lt unordered ordered ungt unge unlt unle uneq ltgt)
51 (define_operator_list simple_comparison lt le eq ne ge gt)
52 (define_operator_list swapped_simple_comparison gt ge eq ne le lt)
54 #include "cfn-operators.pd"
56 /* Define operand lists for math rounding functions {,i,l,ll}FN,
57 where the versions prefixed with "i" return an int, those prefixed with
58 "l" return a long and those prefixed with "ll" return a long long.
60 Also define operand lists:
62 X<FN>F for all float functions, in the order i, l, ll
63 X<FN> for all double functions, in the same order
64 X<FN>L for all long double functions, in the same order. */
65 #define DEFINE_INT_AND_FLOAT_ROUND_FN(FN) \
66 (define_operator_list X##FN##F BUILT_IN_I##FN##F \
69 (define_operator_list X##FN BUILT_IN_I##FN \
72 (define_operator_list X##FN##L BUILT_IN_I##FN##L \
76 DEFINE_INT_AND_FLOAT_ROUND_FN (FLOOR)
77 DEFINE_INT_AND_FLOAT_ROUND_FN (CEIL)
78 DEFINE_INT_AND_FLOAT_ROUND_FN (ROUND)
79 DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
81 /* Binary operations and their associated IFN_COND_* function. */
82 (define_operator_list UNCOND_BINARY
84 mult trunc_div trunc_mod rdiv
86 bit_and bit_ior bit_xor
88 (define_operator_list COND_BINARY
89 IFN_COND_ADD IFN_COND_SUB
90 IFN_COND_MUL IFN_COND_DIV IFN_COND_MOD IFN_COND_RDIV
91 IFN_COND_MIN IFN_COND_MAX
92 IFN_COND_AND IFN_COND_IOR IFN_COND_XOR
93 IFN_COND_SHL IFN_COND_SHR)
95 /* Same for ternary operations. */
96 (define_operator_list UNCOND_TERNARY
97 IFN_FMA IFN_FMS IFN_FNMA IFN_FNMS)
98 (define_operator_list COND_TERNARY
99 IFN_COND_FMA IFN_COND_FMS IFN_COND_FNMA IFN_COND_FNMS)
101 /* With nop_convert? combine convert? and view_convert? in one pattern
102 plus conditionalize on tree_nop_conversion_p conversions. */
103 (match (nop_convert @0)
105 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))))
106 (match (nop_convert @0)
108 (if (VECTOR_TYPE_P (type) && VECTOR_TYPE_P (TREE_TYPE (@0))
109 && known_eq (TYPE_VECTOR_SUBPARTS (type),
110 TYPE_VECTOR_SUBPARTS (TREE_TYPE (@0)))
111 && tree_nop_conversion_p (TREE_TYPE (type), TREE_TYPE (TREE_TYPE (@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 /* Optimize (X + (X >> (prec - 1))) ^ (X >> (prec - 1)) into abs (X). */
126 (bit_xor:c (plus:c @0 (rshift@2 @0 INTEGER_CST@1)) @2)
127 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
128 && !TYPE_UNSIGNED (TREE_TYPE (@0))
129 && wi::to_widest (@1) == element_precision (TREE_TYPE (@0)) - 1)
133 /* Simplifications of operations with one constant operand and
134 simplifications to constants or single values. */
136 (for op (plus pointer_plus minus bit_ior bit_xor)
138 (op @0 integer_zerop)
141 /* 0 +p index -> (type)index */
143 (pointer_plus integer_zerop @1)
144 (non_lvalue (convert @1)))
146 /* ptr - 0 -> (type)ptr */
148 (pointer_diff @0 integer_zerop)
151 /* See if ARG1 is zero and X + ARG1 reduces to X.
152 Likewise if the operands are reversed. */
154 (plus:c @0 real_zerop@1)
155 (if (fold_real_zero_addition_p (type, @1, 0))
158 /* See if ARG1 is zero and X - ARG1 reduces to X. */
160 (minus @0 real_zerop@1)
161 (if (fold_real_zero_addition_p (type, @1, 1))
164 /* Even if the fold_real_zero_addition_p can't simplify X + 0.0
165 into X, we can optimize (X + 0.0) + 0.0 or (X + 0.0) - 0.0
166 or (X - 0.0) + 0.0 into X + 0.0 and (X - 0.0) - 0.0 into X - 0.0
167 if not -frounding-math. For sNaNs the first operation would raise
168 exceptions but turn the result into qNan, so the second operation
169 would not raise it. */
170 (for inner_op (plus minus)
171 (for outer_op (plus minus)
173 (outer_op (inner_op@3 @0 REAL_CST@1) REAL_CST@2)
176 && !HONOR_SIGN_DEPENDENT_ROUNDING (type))
177 (with { bool inner_plus = ((inner_op == PLUS_EXPR)
178 ^ REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@1)));
180 = ((outer_op == PLUS_EXPR)
181 ^ REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@2))); }
182 (if (outer_plus && !inner_plus)
187 This is unsafe for certain floats even in non-IEEE formats.
188 In IEEE, it is unsafe because it does wrong for NaNs.
189 Also note that operand_equal_p is always false if an operand
193 (if (!FLOAT_TYPE_P (type) || !HONOR_NANS (type))
194 { build_zero_cst (type); }))
196 (pointer_diff @@0 @0)
197 { build_zero_cst (type); })
200 (mult @0 integer_zerop@1)
203 /* Maybe fold x * 0 to 0. The expressions aren't the same
204 when x is NaN, since x * 0 is also NaN. Nor are they the
205 same in modes with signed zeros, since multiplying a
206 negative value by 0 gives -0, not +0. */
208 (mult @0 real_zerop@1)
209 (if (!HONOR_NANS (type) && !HONOR_SIGNED_ZEROS (type))
212 /* In IEEE floating point, x*1 is not equivalent to x for snans.
213 Likewise for complex arithmetic with signed zeros. */
216 (if (!HONOR_SNANS (type)
217 && (!HONOR_SIGNED_ZEROS (type)
218 || !COMPLEX_FLOAT_TYPE_P (type)))
221 /* Transform x * -1.0 into -x. */
223 (mult @0 real_minus_onep)
224 (if (!HONOR_SNANS (type)
225 && (!HONOR_SIGNED_ZEROS (type)
226 || !COMPLEX_FLOAT_TYPE_P (type)))
229 /* Transform { 0 or 1 } * { 0 or 1 } into { 0 or 1 } & { 0 or 1 } */
231 (mult SSA_NAME@1 SSA_NAME@2)
232 (if (INTEGRAL_TYPE_P (type)
233 && get_nonzero_bits (@1) == 1
234 && get_nonzero_bits (@2) == 1)
237 /* Transform x * { 0 or 1, 0 or 1, ... } into x & { 0 or -1, 0 or -1, ...},
238 unless the target has native support for the former but not the latter. */
240 (mult @0 VECTOR_CST@1)
241 (if (initializer_each_zero_or_onep (@1)
242 && !HONOR_SNANS (type)
243 && !HONOR_SIGNED_ZEROS (type))
244 (with { tree itype = FLOAT_TYPE_P (type) ? unsigned_type_for (type) : type; }
246 && (!VECTOR_MODE_P (TYPE_MODE (type))
247 || (VECTOR_MODE_P (TYPE_MODE (itype))
248 && optab_handler (and_optab,
249 TYPE_MODE (itype)) != CODE_FOR_nothing)))
250 (view_convert (bit_and:itype (view_convert @0)
251 (ne @1 { build_zero_cst (type); })))))))
253 (for cmp (gt ge lt le)
254 outp (convert convert negate negate)
255 outn (negate negate convert convert)
256 /* Transform (X > 0.0 ? 1.0 : -1.0) into copysign(1, X). */
257 /* Transform (X >= 0.0 ? 1.0 : -1.0) into copysign(1, X). */
258 /* Transform (X < 0.0 ? 1.0 : -1.0) into copysign(1,-X). */
259 /* Transform (X <= 0.0 ? 1.0 : -1.0) into copysign(1,-X). */
261 (cond (cmp @0 real_zerop) real_onep@1 real_minus_onep)
262 (if (!HONOR_NANS (type) && !HONOR_SIGNED_ZEROS (type)
263 && types_match (type, TREE_TYPE (@0)))
265 (if (types_match (type, float_type_node))
266 (BUILT_IN_COPYSIGNF @1 (outp @0)))
267 (if (types_match (type, double_type_node))
268 (BUILT_IN_COPYSIGN @1 (outp @0)))
269 (if (types_match (type, long_double_type_node))
270 (BUILT_IN_COPYSIGNL @1 (outp @0))))))
271 /* Transform (X > 0.0 ? -1.0 : 1.0) into copysign(1,-X). */
272 /* Transform (X >= 0.0 ? -1.0 : 1.0) into copysign(1,-X). */
273 /* Transform (X < 0.0 ? -1.0 : 1.0) into copysign(1,X). */
274 /* Transform (X <= 0.0 ? -1.0 : 1.0) into copysign(1,X). */
276 (cond (cmp @0 real_zerop) real_minus_onep real_onep@1)
277 (if (!HONOR_NANS (type) && !HONOR_SIGNED_ZEROS (type)
278 && types_match (type, TREE_TYPE (@0)))
280 (if (types_match (type, float_type_node))
281 (BUILT_IN_COPYSIGNF @1 (outn @0)))
282 (if (types_match (type, double_type_node))
283 (BUILT_IN_COPYSIGN @1 (outn @0)))
284 (if (types_match (type, long_double_type_node))
285 (BUILT_IN_COPYSIGNL @1 (outn @0)))))))
287 /* Transform X * copysign (1.0, X) into abs(X). */
289 (mult:c @0 (COPYSIGN_ALL real_onep @0))
290 (if (!HONOR_NANS (type) && !HONOR_SIGNED_ZEROS (type))
293 /* Transform X * copysign (1.0, -X) into -abs(X). */
295 (mult:c @0 (COPYSIGN_ALL real_onep (negate @0)))
296 (if (!HONOR_NANS (type) && !HONOR_SIGNED_ZEROS (type))
299 /* Transform copysign (CST, X) into copysign (ABS(CST), X). */
301 (COPYSIGN_ALL REAL_CST@0 @1)
302 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@0)))
303 (COPYSIGN_ALL (negate @0) @1)))
305 /* X * 1, X / 1 -> X. */
306 (for op (mult trunc_div ceil_div floor_div round_div exact_div)
311 /* (A / (1 << B)) -> (A >> B).
312 Only for unsigned A. For signed A, this would not preserve rounding
314 For example: (-1 / ( 1 << B)) != -1 >> B.
315 Also also widening conversions, like:
316 (A / (unsigned long long) (1U << B)) -> (A >> B)
318 (A / (unsigned long long) (1 << B)) -> (A >> B).
319 If the left shift is signed, it can be done only if the upper bits
320 of A starting from shift's type sign bit are zero, as
321 (unsigned long long) (1 << 31) is -2147483648ULL, not 2147483648ULL,
322 so it is valid only if A >> 31 is zero. */
324 (trunc_div (convert?@0 @3) (convert2? (lshift integer_onep@1 @2)))
325 (if ((TYPE_UNSIGNED (type) || tree_expr_nonnegative_p (@0))
326 && (!VECTOR_TYPE_P (type)
327 || target_supports_op_p (type, RSHIFT_EXPR, optab_vector)
328 || target_supports_op_p (type, RSHIFT_EXPR, optab_scalar))
329 && (useless_type_conversion_p (type, TREE_TYPE (@1))
330 || (element_precision (type) >= element_precision (TREE_TYPE (@1))
331 && (TYPE_UNSIGNED (TREE_TYPE (@1))
332 || (element_precision (type)
333 == element_precision (TREE_TYPE (@1)))
334 || (INTEGRAL_TYPE_P (type)
335 && (tree_nonzero_bits (@0)
336 & wi::mask (element_precision (TREE_TYPE (@1)) - 1,
338 element_precision (type))) == 0)))))
339 (if (!VECTOR_TYPE_P (type)
340 && useless_type_conversion_p (TREE_TYPE (@3), TREE_TYPE (@1))
341 && element_precision (TREE_TYPE (@3)) < element_precision (type))
342 (convert (rshift @3 @2))
345 /* Preserve explicit divisions by 0: the C++ front-end wants to detect
346 undefined behavior in constexpr evaluation, and assuming that the division
347 traps enables better optimizations than these anyway. */
348 (for div (trunc_div ceil_div floor_div round_div exact_div)
349 /* 0 / X is always zero. */
351 (div integer_zerop@0 @1)
352 /* But not for 0 / 0 so that we can get the proper warnings and errors. */
353 (if (!integer_zerop (@1))
357 (div @0 integer_minus_onep@1)
358 (if (!TYPE_UNSIGNED (type))
360 /* X / bool_range_Y is X. */
363 (if (INTEGRAL_TYPE_P (type) && ssa_name_has_boolean_range (@1))
368 /* But not for 0 / 0 so that we can get the proper warnings and errors.
369 And not for _Fract types where we can't build 1. */
370 (if (!integer_zerop (@0) && !ALL_FRACT_MODE_P (TYPE_MODE (type)))
371 { build_one_cst (type); }))
372 /* X / abs (X) is X < 0 ? -1 : 1. */
375 (if (INTEGRAL_TYPE_P (type)
376 && TYPE_OVERFLOW_UNDEFINED (type))
377 (cond (lt @0 { build_zero_cst (type); })
378 { build_minus_one_cst (type); } { build_one_cst (type); })))
381 (div:C @0 (negate @0))
382 (if ((INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
383 && TYPE_OVERFLOW_UNDEFINED (type))
384 { build_minus_one_cst (type); })))
386 /* For unsigned integral types, FLOOR_DIV_EXPR is the same as
387 TRUNC_DIV_EXPR. Rewrite into the latter in this case. */
390 (if ((INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
391 && TYPE_UNSIGNED (type))
394 /* Combine two successive divisions. Note that combining ceil_div
395 and floor_div is trickier and combining round_div even more so. */
396 (for div (trunc_div exact_div)
398 (div (div@3 @0 INTEGER_CST@1) INTEGER_CST@2)
400 wi::overflow_type overflow;
401 wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@2),
402 TYPE_SIGN (type), &overflow);
404 (if (div == EXACT_DIV_EXPR
405 || optimize_successive_divisions_p (@2, @3))
407 (div @0 { wide_int_to_tree (type, mul); })
408 (if (TYPE_UNSIGNED (type)
409 || mul != wi::min_value (TYPE_PRECISION (type), SIGNED))
410 { build_zero_cst (type); }))))))
412 /* Combine successive multiplications. Similar to above, but handling
413 overflow is different. */
415 (mult (mult @0 INTEGER_CST@1) INTEGER_CST@2)
417 wi::overflow_type overflow;
418 wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@2),
419 TYPE_SIGN (type), &overflow);
421 /* Skip folding on overflow: the only special case is @1 * @2 == -INT_MIN,
422 otherwise undefined overflow implies that @0 must be zero. */
423 (if (!overflow || TYPE_OVERFLOW_WRAPS (type))
424 (mult @0 { wide_int_to_tree (type, mul); }))))
426 /* Optimize A / A to 1.0 if we don't care about
427 NaNs or Infinities. */
430 (if (FLOAT_TYPE_P (type)
431 && ! HONOR_NANS (type)
432 && ! HONOR_INFINITIES (type))
433 { build_one_cst (type); }))
435 /* Optimize -A / A to -1.0 if we don't care about
436 NaNs or Infinities. */
438 (rdiv:C @0 (negate @0))
439 (if (FLOAT_TYPE_P (type)
440 && ! HONOR_NANS (type)
441 && ! HONOR_INFINITIES (type))
442 { build_minus_one_cst (type); }))
444 /* PR71078: x / abs(x) -> copysign (1.0, x) */
446 (rdiv:C (convert? @0) (convert? (abs @0)))
447 (if (SCALAR_FLOAT_TYPE_P (type)
448 && ! HONOR_NANS (type)
449 && ! HONOR_INFINITIES (type))
451 (if (types_match (type, float_type_node))
452 (BUILT_IN_COPYSIGNF { build_one_cst (type); } (convert @0)))
453 (if (types_match (type, double_type_node))
454 (BUILT_IN_COPYSIGN { build_one_cst (type); } (convert @0)))
455 (if (types_match (type, long_double_type_node))
456 (BUILT_IN_COPYSIGNL { build_one_cst (type); } (convert @0))))))
458 /* In IEEE floating point, x/1 is not equivalent to x for snans. */
461 (if (!HONOR_SNANS (type))
464 /* In IEEE floating point, x/-1 is not equivalent to -x for snans. */
466 (rdiv @0 real_minus_onep)
467 (if (!HONOR_SNANS (type))
470 (if (flag_reciprocal_math)
471 /* Convert (A/B)/C to A/(B*C). */
473 (rdiv (rdiv:s @0 @1) @2)
474 (rdiv @0 (mult @1 @2)))
476 /* Canonicalize x / (C1 * y) to (x * C2) / y. */
478 (rdiv @0 (mult:s @1 REAL_CST@2))
480 { tree tem = const_binop (RDIV_EXPR, type, build_one_cst (type), @2); }
482 (rdiv (mult @0 { tem; } ) @1))))
484 /* Convert A/(B/C) to (A/B)*C */
486 (rdiv @0 (rdiv:s @1 @2))
487 (mult (rdiv @0 @1) @2)))
489 /* Simplify x / (- y) to -x / y. */
491 (rdiv @0 (negate @1))
492 (rdiv (negate @0) @1))
494 (if (flag_unsafe_math_optimizations)
495 /* Simplify (C / x op 0.0) to x op 0.0 for C != 0, C != Inf/Nan.
496 Since C / x may underflow to zero, do this only for unsafe math. */
497 (for op (lt le gt ge)
500 (op (rdiv REAL_CST@0 @1) real_zerop@2)
501 (if (!HONOR_SIGNED_ZEROS (@1) && !HONOR_INFINITIES (@1))
503 (if (real_less (&dconst0, TREE_REAL_CST_PTR (@0)))
505 /* For C < 0, use the inverted operator. */
506 (if (real_less (TREE_REAL_CST_PTR (@0), &dconst0))
509 /* Optimize (X & (-A)) / A where A is a power of 2, to X >> log2(A) */
510 (for div (trunc_div ceil_div floor_div round_div exact_div)
512 (div (convert? (bit_and @0 INTEGER_CST@1)) INTEGER_CST@2)
513 (if (integer_pow2p (@2)
514 && tree_int_cst_sgn (@2) > 0
515 && tree_nop_conversion_p (type, TREE_TYPE (@0))
516 && wi::to_wide (@2) + wi::to_wide (@1) == 0)
518 { build_int_cst (integer_type_node,
519 wi::exact_log2 (wi::to_wide (@2))); }))))
521 /* If ARG1 is a constant, we can convert this to a multiply by the
522 reciprocal. This does not have the same rounding properties,
523 so only do this if -freciprocal-math. We can actually
524 always safely do it if ARG1 is a power of two, but it's hard to
525 tell if it is or not in a portable manner. */
526 (for cst (REAL_CST COMPLEX_CST VECTOR_CST)
530 (if (flag_reciprocal_math
533 { tree tem = const_binop (RDIV_EXPR, type, build_one_cst (type), @1); }
535 (mult @0 { tem; } )))
536 (if (cst != COMPLEX_CST)
537 (with { tree inverse = exact_inverse (type, @1); }
539 (mult @0 { inverse; } ))))))))
541 (for mod (ceil_mod floor_mod round_mod trunc_mod)
542 /* 0 % X is always zero. */
544 (mod integer_zerop@0 @1)
545 /* But not for 0 % 0 so that we can get the proper warnings and errors. */
546 (if (!integer_zerop (@1))
548 /* X % 1 is always zero. */
550 (mod @0 integer_onep)
551 { build_zero_cst (type); })
552 /* X % -1 is zero. */
554 (mod @0 integer_minus_onep@1)
555 (if (!TYPE_UNSIGNED (type))
556 { build_zero_cst (type); }))
560 /* But not for 0 % 0 so that we can get the proper warnings and errors. */
561 (if (!integer_zerop (@0))
562 { build_zero_cst (type); }))
563 /* (X % Y) % Y is just X % Y. */
565 (mod (mod@2 @0 @1) @1)
567 /* From extract_muldiv_1: (X * C1) % C2 is zero if C1 is a multiple of C2. */
569 (mod (mult @0 INTEGER_CST@1) INTEGER_CST@2)
570 (if (ANY_INTEGRAL_TYPE_P (type)
571 && TYPE_OVERFLOW_UNDEFINED (type)
572 && wi::multiple_of_p (wi::to_wide (@1), wi::to_wide (@2),
574 { build_zero_cst (type); }))
575 /* For (X % C) == 0, if X is signed and C is power of 2, use unsigned
576 modulo and comparison, since it is simpler and equivalent. */
579 (cmp (mod @0 integer_pow2p@2) integer_zerop@1)
580 (if (!TYPE_UNSIGNED (TREE_TYPE (@0)))
581 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
582 (cmp (mod (convert:utype @0) (convert:utype @2)) (convert:utype @1)))))))
584 /* X % -C is the same as X % C. */
586 (trunc_mod @0 INTEGER_CST@1)
587 (if (TYPE_SIGN (type) == SIGNED
588 && !TREE_OVERFLOW (@1)
589 && wi::neg_p (wi::to_wide (@1))
590 && !TYPE_OVERFLOW_TRAPS (type)
591 /* Avoid this transformation if C is INT_MIN, i.e. C == -C. */
592 && !sign_bit_p (@1, @1))
593 (trunc_mod @0 (negate @1))))
595 /* X % -Y is the same as X % Y. */
597 (trunc_mod @0 (convert? (negate @1)))
598 (if (INTEGRAL_TYPE_P (type)
599 && !TYPE_UNSIGNED (type)
600 && !TYPE_OVERFLOW_TRAPS (type)
601 && tree_nop_conversion_p (type, TREE_TYPE (@1))
602 /* Avoid this transformation if X might be INT_MIN or
603 Y might be -1, because we would then change valid
604 INT_MIN % -(-1) into invalid INT_MIN % -1. */
605 && (expr_not_equal_to (@0, wi::to_wide (TYPE_MIN_VALUE (type)))
606 || expr_not_equal_to (@1, wi::minus_one (TYPE_PRECISION
608 (trunc_mod @0 (convert @1))))
610 /* X - (X / Y) * Y is the same as X % Y. */
612 (minus (convert1? @0) (convert2? (mult:c (trunc_div @@0 @@1) @1)))
613 (if (INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
614 (convert (trunc_mod @0 @1))))
616 /* Optimize TRUNC_MOD_EXPR by a power of two into a BIT_AND_EXPR,
617 i.e. "X % C" into "X & (C - 1)", if X and C are positive.
618 Also optimize A % (C << N) where C is a power of 2,
619 to A & ((C << N) - 1).
620 Also optimize "A shift (B % C)", if C is a power of 2, to
621 "A shift (B & (C - 1))". SHIFT operation include "<<" and ">>"
622 and assume (B % C) is nonnegative as shifts negative values would
624 (match (power_of_two_cand @1)
626 (match (power_of_two_cand @1)
627 (lshift INTEGER_CST@1 @2))
628 (for mod (trunc_mod floor_mod)
629 (for shift (lshift rshift)
631 (shift @0 (mod @1 (power_of_two_cand@2 @3)))
632 (if (integer_pow2p (@3) && tree_int_cst_sgn (@3) > 0)
633 (shift @0 (bit_and @1 (minus @2 { build_int_cst (TREE_TYPE (@2),
636 (mod @0 (convert?@3 (power_of_two_cand@1 @2)))
637 (if ((TYPE_UNSIGNED (type)
638 || tree_expr_nonnegative_p (@0))
639 && tree_nop_conversion_p (type, TREE_TYPE (@3))
640 && integer_pow2p (@2) && tree_int_cst_sgn (@2) > 0)
641 (bit_and @0 (convert (minus @1 { build_int_cst (TREE_TYPE (@1), 1); }))))))
643 /* Simplify (unsigned t * 2)/2 -> unsigned t & 0x7FFFFFFF. */
645 (trunc_div (mult @0 integer_pow2p@1) @1)
646 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
647 (bit_and @0 { wide_int_to_tree
648 (type, wi::mask (TYPE_PRECISION (type)
649 - wi::exact_log2 (wi::to_wide (@1)),
650 false, TYPE_PRECISION (type))); })))
652 /* Simplify (unsigned t / 2) * 2 -> unsigned t & ~1. */
654 (mult (trunc_div @0 integer_pow2p@1) @1)
655 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
656 (bit_and @0 (negate @1))))
658 /* Simplify (t * 2) / 2) -> t. */
659 (for div (trunc_div ceil_div floor_div round_div exact_div)
661 (div (mult:c @0 @1) @1)
662 (if (ANY_INTEGRAL_TYPE_P (type))
663 (if (TYPE_OVERFLOW_UNDEFINED (type))
668 bool overflowed = true;
669 wide_int wmin0, wmax0, wmin1, wmax1;
670 if (INTEGRAL_TYPE_P (type)
671 && get_range_info (@0, &wmin0, &wmax0) == VR_RANGE
672 && get_range_info (@1, &wmin1, &wmax1) == VR_RANGE)
674 /* If the multiplication can't overflow/wrap around, then
675 it can be optimized too. */
676 wi::overflow_type min_ovf, max_ovf;
677 wi::mul (wmin0, wmin1, TYPE_SIGN (type), &min_ovf);
678 wi::mul (wmax0, wmax1, TYPE_SIGN (type), &max_ovf);
679 if (min_ovf == wi::OVF_NONE && max_ovf == wi::OVF_NONE)
681 wi::mul (wmin0, wmax1, TYPE_SIGN (type), &min_ovf);
682 wi::mul (wmax0, wmin1, TYPE_SIGN (type), &max_ovf);
683 if (min_ovf == wi::OVF_NONE && max_ovf == wi::OVF_NONE)
694 /* Simplify cos(-x) and cos(|x|) -> cos(x). Similarly for cosh. */
699 /* Simplify pow(-x, y) and pow(|x|,y) -> pow(x,y) if y is an even integer. */
702 (pows (op @0) REAL_CST@1)
703 (with { HOST_WIDE_INT n; }
704 (if (real_isinteger (&TREE_REAL_CST (@1), &n) && (n & 1) == 0)
706 /* Likewise for powi. */
709 (pows (op @0) INTEGER_CST@1)
710 (if ((wi::to_wide (@1) & 1) == 0)
712 /* Strip negate and abs from both operands of hypot. */
720 /* copysign(-x, y) and copysign(abs(x), y) -> copysign(x, y). */
721 (for copysigns (COPYSIGN_ALL)
723 (copysigns (op @0) @1)
726 /* abs(x)*abs(x) -> x*x. Should be valid for all types. */
731 /* Convert absu(x)*absu(x) -> x*x. */
733 (mult (absu@1 @0) @1)
734 (mult (convert@2 @0) @2))
736 /* cos(copysign(x, y)) -> cos(x). Similarly for cosh. */
740 (coss (copysigns @0 @1))
743 /* pow(copysign(x, y), z) -> pow(x, z) if z is an even integer. */
747 (pows (copysigns @0 @2) REAL_CST@1)
748 (with { HOST_WIDE_INT n; }
749 (if (real_isinteger (&TREE_REAL_CST (@1), &n) && (n & 1) == 0)
751 /* Likewise for powi. */
755 (pows (copysigns @0 @2) INTEGER_CST@1)
756 (if ((wi::to_wide (@1) & 1) == 0)
761 /* hypot(copysign(x, y), z) -> hypot(x, z). */
763 (hypots (copysigns @0 @1) @2)
765 /* hypot(x, copysign(y, z)) -> hypot(x, y). */
767 (hypots @0 (copysigns @1 @2))
770 /* copysign(x, CST) -> [-]abs (x). */
771 (for copysigns (COPYSIGN_ALL)
773 (copysigns @0 REAL_CST@1)
774 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
778 /* copysign(copysign(x, y), z) -> copysign(x, z). */
779 (for copysigns (COPYSIGN_ALL)
781 (copysigns (copysigns @0 @1) @2)
784 /* copysign(x,y)*copysign(x,y) -> x*x. */
785 (for copysigns (COPYSIGN_ALL)
787 (mult (copysigns@2 @0 @1) @2)
790 /* ccos(-x) -> ccos(x). Similarly for ccosh. */
791 (for ccoss (CCOS CCOSH)
796 /* cabs(-x) and cos(conj(x)) -> cabs(x). */
797 (for ops (conj negate)
803 /* Fold (a * (1 << b)) into (a << b) */
805 (mult:c @0 (convert? (lshift integer_onep@1 @2)))
806 (if (! FLOAT_TYPE_P (type)
807 && tree_nop_conversion_p (type, TREE_TYPE (@1)))
810 /* Fold (1 << (C - x)) where C = precision(type) - 1
811 into ((1 << C) >> x). */
813 (lshift integer_onep@0 (minus@1 INTEGER_CST@2 @3))
814 (if (INTEGRAL_TYPE_P (type)
815 && wi::eq_p (wi::to_wide (@2), TYPE_PRECISION (type) - 1)
817 (if (TYPE_UNSIGNED (type))
818 (rshift (lshift @0 @2) @3)
820 { tree utype = unsigned_type_for (type); }
821 (convert (rshift (lshift (convert:utype @0) @2) @3))))))
823 /* Fold (C1/X)*C2 into (C1*C2)/X. */
825 (mult (rdiv@3 REAL_CST@0 @1) REAL_CST@2)
826 (if (flag_associative_math
829 { tree tem = const_binop (MULT_EXPR, type, @0, @2); }
831 (rdiv { tem; } @1)))))
833 /* Simplify ~X & X as zero. */
835 (bit_and:c (convert? @0) (convert? (bit_not @0)))
836 { build_zero_cst (type); })
838 /* PR71636: Transform x & ((1U << b) - 1) -> x & ~(~0U << b); */
840 (bit_and:c @0 (plus:s (lshift:s integer_onep @1) integer_minus_onep))
841 (if (TYPE_UNSIGNED (type))
842 (bit_and @0 (bit_not (lshift { build_all_ones_cst (type); } @1)))))
844 (for bitop (bit_and bit_ior)
846 /* PR35691: Transform
847 (x == 0 & y == 0) -> (x | typeof(x)(y)) == 0.
848 (x != 0 | y != 0) -> (x | typeof(x)(y)) != 0. */
850 (bitop (cmp @0 integer_zerop@2) (cmp @1 integer_zerop))
851 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
852 && INTEGRAL_TYPE_P (TREE_TYPE (@1))
853 && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
854 (cmp (bit_ior @0 (convert @1)) @2)))
856 (x == -1 & y == -1) -> (x & typeof(x)(y)) == -1.
857 (x != -1 | y != -1) -> (x & typeof(x)(y)) != -1. */
859 (bitop (cmp @0 integer_all_onesp@2) (cmp @1 integer_all_onesp))
860 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
861 && INTEGRAL_TYPE_P (TREE_TYPE (@1))
862 && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
863 (cmp (bit_and @0 (convert @1)) @2))))
865 /* Fold (A & ~B) - (A & B) into (A ^ B) - B. */
867 (minus (bit_and:cs @0 (bit_not @1)) (bit_and:cs @0 @1))
868 (minus (bit_xor @0 @1) @1))
870 (minus (bit_and:s @0 INTEGER_CST@2) (bit_and:s @0 INTEGER_CST@1))
871 (if (~wi::to_wide (@2) == wi::to_wide (@1))
872 (minus (bit_xor @0 @1) @1)))
874 /* Fold (A & B) - (A & ~B) into B - (A ^ B). */
876 (minus (bit_and:cs @0 @1) (bit_and:cs @0 (bit_not @1)))
877 (minus @1 (bit_xor @0 @1)))
879 /* Simplify (X & ~Y) |^+ (~X & Y) -> X ^ Y. */
880 (for op (bit_ior bit_xor plus)
882 (op (bit_and:c @0 (bit_not @1)) (bit_and:c (bit_not @0) @1))
885 (op:c (bit_and @0 INTEGER_CST@2) (bit_and (bit_not @0) INTEGER_CST@1))
886 (if (~wi::to_wide (@2) == wi::to_wide (@1))
889 /* PR53979: Transform ((a ^ b) | a) -> (a | b) */
891 (bit_ior:c (bit_xor:c @0 @1) @0)
894 /* (a & ~b) | (a ^ b) --> a ^ b */
896 (bit_ior:c (bit_and:c @0 (bit_not @1)) (bit_xor:c@2 @0 @1))
899 /* (a & ~b) ^ ~a --> ~(a & b) */
901 (bit_xor:c (bit_and:cs @0 (bit_not @1)) (bit_not @0))
902 (bit_not (bit_and @0 @1)))
904 /* (~a & b) ^ a --> (a | b) */
906 (bit_xor:c (bit_and:cs (bit_not @0) @1) @0)
909 /* (a | b) & ~(a ^ b) --> a & b */
911 (bit_and:c (bit_ior @0 @1) (bit_not (bit_xor:c @0 @1)))
914 /* a | ~(a ^ b) --> a | ~b */
916 (bit_ior:c @0 (bit_not:s (bit_xor:c @0 @1)))
917 (bit_ior @0 (bit_not @1)))
919 /* (a | b) | (a &^ b) --> a | b */
920 (for op (bit_and bit_xor)
922 (bit_ior:c (bit_ior@2 @0 @1) (op:c @0 @1))
925 /* (a & b) | ~(a ^ b) --> ~(a ^ b) */
927 (bit_ior:c (bit_and:c @0 @1) (bit_not@2 (bit_xor @0 @1)))
930 /* ~(~a & b) --> a | ~b */
932 (bit_not (bit_and:cs (bit_not @0) @1))
933 (bit_ior @0 (bit_not @1)))
935 /* ~(~a | b) --> a & ~b */
937 (bit_not (bit_ior:cs (bit_not @0) @1))
938 (bit_and @0 (bit_not @1)))
940 /* (a ^ b) & ((b ^ c) ^ a) --> (a ^ b) & ~c */
942 (bit_and:c (bit_xor:c@3 @0 @1) (bit_xor:cs (bit_xor:cs @1 @2) @0))
943 (bit_and @3 (bit_not @2)))
945 /* (a ^ b) | ((b ^ c) ^ a) --> (a ^ b) | c */
947 (bit_ior:c (bit_xor:c@3 @0 @1) (bit_xor:c (bit_xor:c @1 @2) @0))
951 /* (~X | C) ^ D -> (X | C) ^ (~D ^ C) if (~D ^ C) can be simplified. */
953 (bit_xor:c (bit_ior:cs (bit_not:s @0) @1) @2)
954 (bit_xor (bit_ior @0 @1) (bit_xor! (bit_not! @2) @1)))
956 /* (~X & C) ^ D -> (X & C) ^ (D ^ C) if (D ^ C) can be simplified. */
958 (bit_xor:c (bit_and:cs (bit_not:s @0) @1) @2)
959 (bit_xor (bit_and @0 @1) (bit_xor! @2 @1)))
961 /* Simplify (~X & Y) to X ^ Y if we know that (X & ~Y) is 0. */
963 (bit_and (bit_not SSA_NAME@0) INTEGER_CST@1)
964 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
965 && wi::bit_and_not (get_nonzero_bits (@0), wi::to_wide (@1)) == 0)
969 /* For constants M and N, if M == (1LL << cst) - 1 && (N & M) == M,
970 ((A & N) + B) & M -> (A + B) & M
971 Similarly if (N & M) == 0,
972 ((A | N) + B) & M -> (A + B) & M
973 and for - instead of + (or unary - instead of +)
974 and/or ^ instead of |.
975 If B is constant and (B & M) == 0, fold into A & M. */
977 (for bitop (bit_and bit_ior bit_xor)
979 (bit_and (op:s (bitop:s@0 @3 INTEGER_CST@4) @1) INTEGER_CST@2)
982 tree utype = fold_bit_and_mask (TREE_TYPE (@0), @2, op, @0, bitop,
983 @3, @4, @1, ERROR_MARK, NULL_TREE,
986 (convert (bit_and (op (convert:utype { pmop[0]; })
987 (convert:utype { pmop[1]; }))
988 (convert:utype @2))))))
990 (bit_and (op:s @0 (bitop:s@1 @3 INTEGER_CST@4)) INTEGER_CST@2)
993 tree utype = fold_bit_and_mask (TREE_TYPE (@0), @2, op, @0, ERROR_MARK,
994 NULL_TREE, NULL_TREE, @1, bitop, @3,
997 (convert (bit_and (op (convert:utype { pmop[0]; })
998 (convert:utype { pmop[1]; }))
999 (convert:utype @2)))))))
1001 (bit_and (op:s @0 @1) INTEGER_CST@2)
1004 tree utype = fold_bit_and_mask (TREE_TYPE (@0), @2, op, @0, ERROR_MARK,
1005 NULL_TREE, NULL_TREE, @1, ERROR_MARK,
1006 NULL_TREE, NULL_TREE, pmop); }
1008 (convert (bit_and (op (convert:utype { pmop[0]; })
1009 (convert:utype { pmop[1]; }))
1010 (convert:utype @2)))))))
1011 (for bitop (bit_and bit_ior bit_xor)
1013 (bit_and (negate:s (bitop:s@0 @2 INTEGER_CST@3)) INTEGER_CST@1)
1016 tree utype = fold_bit_and_mask (TREE_TYPE (@0), @1, NEGATE_EXPR, @0,
1017 bitop, @2, @3, NULL_TREE, ERROR_MARK,
1018 NULL_TREE, NULL_TREE, pmop); }
1020 (convert (bit_and (negate (convert:utype { pmop[0]; }))
1021 (convert:utype @1)))))))
1023 /* X % Y is smaller than Y. */
1026 (cmp (trunc_mod @0 @1) @1)
1027 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
1028 { constant_boolean_node (cmp == LT_EXPR, type); })))
1031 (cmp @1 (trunc_mod @0 @1))
1032 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
1033 { constant_boolean_node (cmp == GT_EXPR, type); })))
1037 (bit_ior @0 integer_all_onesp@1)
1042 (bit_ior @0 integer_zerop)
1047 (bit_and @0 integer_zerop@1)
1053 (for op (bit_ior bit_xor plus)
1055 (op:c (convert? @0) (convert? (bit_not @0)))
1056 (convert { build_all_ones_cst (TREE_TYPE (@0)); })))
1061 { build_zero_cst (type); })
1063 /* Canonicalize X ^ ~0 to ~X. */
1065 (bit_xor @0 integer_all_onesp@1)
1070 (bit_and @0 integer_all_onesp)
1073 /* x & x -> x, x | x -> x */
1074 (for bitop (bit_and bit_ior)
1079 /* x & C -> x if we know that x & ~C == 0. */
1082 (bit_and SSA_NAME@0 INTEGER_CST@1)
1083 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1084 && wi::bit_and_not (get_nonzero_bits (@0), wi::to_wide (@1)) == 0)
1088 /* ~(~X - Y) -> X + Y and ~(~X + Y) -> X - Y. */
1090 (bit_not (minus (bit_not @0) @1))
1093 (bit_not (plus:c (bit_not @0) @1))
1096 /* ~(X - Y) -> ~X + Y. */
1098 (bit_not (minus:s @0 @1))
1099 (plus (bit_not @0) @1))
1101 (bit_not (plus:s @0 INTEGER_CST@1))
1102 (if ((INTEGRAL_TYPE_P (type)
1103 && TYPE_UNSIGNED (type))
1104 || (!TYPE_OVERFLOW_SANITIZED (type)
1105 && may_negate_without_overflow_p (@1)))
1106 (plus (bit_not @0) { const_unop (NEGATE_EXPR, type, @1); })))
1109 /* ~X + Y -> (Y - X) - 1. */
1111 (plus:c (bit_not @0) @1)
1112 (if (ANY_INTEGRAL_TYPE_P (type)
1113 && TYPE_OVERFLOW_WRAPS (type)
1114 /* -1 - X is folded to ~X, so we'd recurse endlessly. */
1115 && !integer_all_onesp (@1))
1116 (plus (minus @1 @0) { build_minus_one_cst (type); })
1117 (if (INTEGRAL_TYPE_P (type)
1118 && TREE_CODE (@1) == INTEGER_CST
1119 && wi::to_wide (@1) != wi::min_value (TYPE_PRECISION (type),
1121 (minus (plus @1 { build_minus_one_cst (type); }) @0))))
1123 /* ~(X >> Y) -> ~X >> Y if ~X can be simplified. */
1125 (bit_not (rshift:s @0 @1))
1126 (if (!TYPE_UNSIGNED (TREE_TYPE (@0)))
1127 (rshift (bit_not! @0) @1)
1128 /* For logical right shifts, this is possible only if @0 doesn't
1129 have MSB set and the logical right shift is changed into
1130 arithmetic shift. */
1131 (if (!wi::neg_p (tree_nonzero_bits (@0)))
1132 (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
1133 (convert (rshift (bit_not! (convert:stype @0)) @1))))))
1136 /* x + (x & 1) -> (x + 1) & ~1 */
1138 (plus:c @0 (bit_and:s @0 integer_onep@1))
1139 (bit_and (plus @0 @1) (bit_not @1)))
1141 /* x & ~(x & y) -> x & ~y */
1142 /* x | ~(x | y) -> x | ~y */
1143 (for bitop (bit_and bit_ior)
1145 (bitop:c @0 (bit_not (bitop:cs @0 @1)))
1146 (bitop @0 (bit_not @1))))
1148 /* (~x & y) | ~(x | y) -> ~x */
1150 (bit_ior:c (bit_and:c (bit_not@2 @0) @1) (bit_not (bit_ior:c @0 @1)))
1153 /* (x | y) ^ (x | ~y) -> ~x */
1155 (bit_xor:c (bit_ior:c @0 @1) (bit_ior:c @0 (bit_not @1)))
1158 /* (x & y) | ~(x | y) -> ~(x ^ y) */
1160 (bit_ior:c (bit_and:s @0 @1) (bit_not:s (bit_ior:s @0 @1)))
1161 (bit_not (bit_xor @0 @1)))
1163 /* (~x | y) ^ (x ^ y) -> x | ~y */
1165 (bit_xor:c (bit_ior:cs (bit_not @0) @1) (bit_xor:s @0 @1))
1166 (bit_ior @0 (bit_not @1)))
1168 /* (x ^ y) | ~(x | y) -> ~(x & y) */
1170 (bit_ior:c (bit_xor:s @0 @1) (bit_not:s (bit_ior:s @0 @1)))
1171 (bit_not (bit_and @0 @1)))
1173 /* (x | y) & ~x -> y & ~x */
1174 /* (x & y) | ~x -> y | ~x */
1175 (for bitop (bit_and bit_ior)
1176 rbitop (bit_ior bit_and)
1178 (bitop:c (rbitop:c @0 @1) (bit_not@2 @0))
1181 /* (x & y) ^ (x | y) -> x ^ y */
1183 (bit_xor:c (bit_and @0 @1) (bit_ior @0 @1))
1186 /* (x ^ y) ^ (x | y) -> x & y */
1188 (bit_xor:c (bit_xor @0 @1) (bit_ior @0 @1))
1191 /* (x & y) + (x ^ y) -> x | y */
1192 /* (x & y) | (x ^ y) -> x | y */
1193 /* (x & y) ^ (x ^ y) -> x | y */
1194 (for op (plus bit_ior bit_xor)
1196 (op:c (bit_and @0 @1) (bit_xor @0 @1))
1199 /* (x & y) + (x | y) -> x + y */
1201 (plus:c (bit_and @0 @1) (bit_ior @0 @1))
1204 /* (x + y) - (x | y) -> x & y */
1206 (minus (plus @0 @1) (bit_ior @0 @1))
1207 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1208 && !TYPE_SATURATING (type))
1211 /* (x + y) - (x & y) -> x | y */
1213 (minus (plus @0 @1) (bit_and @0 @1))
1214 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1215 && !TYPE_SATURATING (type))
1218 /* (x | y) - y -> (x & ~y) */
1220 (minus (bit_ior:cs @0 @1) @1)
1221 (bit_and @0 (bit_not @1)))
1223 /* (x | y) - (x ^ y) -> x & y */
1225 (minus (bit_ior @0 @1) (bit_xor @0 @1))
1228 /* (x | y) - (x & y) -> x ^ y */
1230 (minus (bit_ior @0 @1) (bit_and @0 @1))
1233 /* (x | y) & ~(x & y) -> x ^ y */
1235 (bit_and:c (bit_ior @0 @1) (bit_not (bit_and @0 @1)))
1238 /* (x | y) & (~x ^ y) -> x & y */
1240 (bit_and:c (bit_ior:c @0 @1) (bit_xor:c @1 (bit_not @0)))
1243 /* (~x | y) & (x | ~y) -> ~(x ^ y) */
1245 (bit_and (bit_ior:cs (bit_not @0) @1) (bit_ior:cs @0 (bit_not @1)))
1246 (bit_not (bit_xor @0 @1)))
1248 /* (~x | y) ^ (x | ~y) -> x ^ y */
1250 (bit_xor (bit_ior:c (bit_not @0) @1) (bit_ior:c @0 (bit_not @1)))
1253 /* ((x & y) - (x | y)) - 1 -> ~(x ^ y) */
1255 (plus (nop_convert1? (minus@2 (nop_convert2? (bit_and:c @0 @1))
1256 (nop_convert2? (bit_ior @0 @1))))
1258 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1259 && !TYPE_SATURATING (type) && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2))
1260 && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@2))
1261 && !TYPE_SATURATING (TREE_TYPE (@2)))
1262 (bit_not (convert (bit_xor @0 @1)))))
1264 (minus (nop_convert1? (plus@2 (nop_convert2? (bit_and:c @0 @1))
1266 (nop_convert3? (bit_ior @0 @1)))
1267 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1268 && !TYPE_SATURATING (type) && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2))
1269 && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@2))
1270 && !TYPE_SATURATING (TREE_TYPE (@2)))
1271 (bit_not (convert (bit_xor @0 @1)))))
1273 (minus (nop_convert1? (bit_and @0 @1))
1274 (nop_convert2? (plus@2 (nop_convert3? (bit_ior:c @0 @1))
1276 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1277 && !TYPE_SATURATING (type) && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2))
1278 && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@2))
1279 && !TYPE_SATURATING (TREE_TYPE (@2)))
1280 (bit_not (convert (bit_xor @0 @1)))))
1282 /* ~x & ~y -> ~(x | y)
1283 ~x | ~y -> ~(x & y) */
1284 (for op (bit_and bit_ior)
1285 rop (bit_ior bit_and)
1287 (op (convert1? (bit_not @0)) (convert2? (bit_not @1)))
1288 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1289 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
1290 (bit_not (rop (convert @0) (convert @1))))))
1292 /* If we are XORing or adding two BIT_AND_EXPR's, both of which are and'ing
1293 with a constant, and the two constants have no bits in common,
1294 we should treat this as a BIT_IOR_EXPR since this may produce more
1296 (for op (bit_xor plus)
1298 (op (convert1? (bit_and@4 @0 INTEGER_CST@1))
1299 (convert2? (bit_and@5 @2 INTEGER_CST@3)))
1300 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
1301 && tree_nop_conversion_p (type, TREE_TYPE (@2))
1302 && (wi::to_wide (@1) & wi::to_wide (@3)) == 0)
1303 (bit_ior (convert @4) (convert @5)))))
1305 /* (X | Y) ^ X -> Y & ~ X*/
1307 (bit_xor:c (convert1? (bit_ior:c @@0 @1)) (convert2? @0))
1308 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1309 (convert (bit_and @1 (bit_not @0)))))
1311 /* Convert ~X ^ ~Y to X ^ Y. */
1313 (bit_xor (convert1? (bit_not @0)) (convert2? (bit_not @1)))
1314 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1315 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
1316 (bit_xor (convert @0) (convert @1))))
1318 /* Convert ~X ^ C to X ^ ~C. */
1320 (bit_xor (convert? (bit_not @0)) INTEGER_CST@1)
1321 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1322 (bit_xor (convert @0) (bit_not @1))))
1324 /* Fold (X & Y) ^ Y and (X ^ Y) & Y as ~X & Y. */
1325 (for opo (bit_and bit_xor)
1326 opi (bit_xor bit_and)
1328 (opo:c (opi:cs @0 @1) @1)
1329 (bit_and (bit_not @0) @1)))
1331 /* Given a bit-wise operation CODE applied to ARG0 and ARG1, see if both
1332 operands are another bit-wise operation with a common input. If so,
1333 distribute the bit operations to save an operation and possibly two if
1334 constants are involved. For example, convert
1335 (A | B) & (A | C) into A | (B & C)
1336 Further simplification will occur if B and C are constants. */
1337 (for op (bit_and bit_ior bit_xor)
1338 rop (bit_ior bit_and bit_and)
1340 (op (convert? (rop:c @@0 @1)) (convert? (rop:c @0 @2)))
1341 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1342 && tree_nop_conversion_p (type, TREE_TYPE (@2)))
1343 (rop (convert @0) (op (convert @1) (convert @2))))))
1345 /* Some simple reassociation for bit operations, also handled in reassoc. */
1346 /* (X & Y) & Y -> X & Y
1347 (X | Y) | Y -> X | Y */
1348 (for op (bit_and bit_ior)
1350 (op:c (convert1?@2 (op:c @0 @@1)) (convert2? @1))
1352 /* (X ^ Y) ^ Y -> X */
1354 (bit_xor:c (convert1? (bit_xor:c @0 @@1)) (convert2? @1))
1356 /* (X & Y) & (X & Z) -> (X & Y) & Z
1357 (X | Y) | (X | Z) -> (X | Y) | Z */
1358 (for op (bit_and bit_ior)
1360 (op (convert1?@3 (op:c@4 @0 @1)) (convert2?@5 (op:c@6 @0 @2)))
1361 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1362 && tree_nop_conversion_p (type, TREE_TYPE (@2)))
1363 (if (single_use (@5) && single_use (@6))
1364 (op @3 (convert @2))
1365 (if (single_use (@3) && single_use (@4))
1366 (op (convert @1) @5))))))
1367 /* (X ^ Y) ^ (X ^ Z) -> Y ^ Z */
1369 (bit_xor (convert1? (bit_xor:c @0 @1)) (convert2? (bit_xor:c @0 @2)))
1370 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1371 && tree_nop_conversion_p (type, TREE_TYPE (@2)))
1372 (bit_xor (convert @1) (convert @2))))
1374 /* Convert abs (abs (X)) into abs (X).
1375 also absu (absu (X)) into absu (X). */
1381 (absu (convert@2 (absu@1 @0)))
1382 (if (tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@1)))
1385 /* Convert abs[u] (-X) -> abs[u] (X). */
1394 /* Convert abs[u] (X) where X is nonnegative -> (X). */
1396 (abs tree_expr_nonnegative_p@0)
1400 (absu tree_expr_nonnegative_p@0)
1403 /* Simplify (-(X < 0) | 1) * X into abs (X). */
1405 (mult:c (bit_ior (negate (convert? (lt @0 integer_zerop))) integer_onep) @0)
1406 (if (INTEGRAL_TYPE_P (type) && !TYPE_UNSIGNED (type))
1409 /* Similarly (-(X < 0) | 1U) * X into absu (X). */
1411 (mult:c (bit_ior (nop_convert (negate (convert? (lt @0 integer_zerop))))
1412 integer_onep) (nop_convert @0))
1413 (if (INTEGRAL_TYPE_P (type)
1414 && TYPE_UNSIGNED (type)
1415 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
1416 && !TYPE_UNSIGNED (TREE_TYPE (@0)))
1419 /* A few cases of fold-const.c negate_expr_p predicate. */
1420 (match negate_expr_p
1422 (if ((INTEGRAL_TYPE_P (type)
1423 && TYPE_UNSIGNED (type))
1424 || (!TYPE_OVERFLOW_SANITIZED (type)
1425 && may_negate_without_overflow_p (t)))))
1426 (match negate_expr_p
1428 (match negate_expr_p
1430 (if (!TYPE_OVERFLOW_SANITIZED (type))))
1431 (match negate_expr_p
1433 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (t)))))
1434 /* VECTOR_CST handling of non-wrapping types would recurse in unsupported
1436 (match negate_expr_p
1438 (if (FLOAT_TYPE_P (TREE_TYPE (type)) || TYPE_OVERFLOW_WRAPS (type))))
1439 (match negate_expr_p
1441 (if ((ANY_INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_WRAPS (type))
1442 || (FLOAT_TYPE_P (type)
1443 && !HONOR_SIGN_DEPENDENT_ROUNDING (type)
1444 && !HONOR_SIGNED_ZEROS (type)))))
1446 /* (-A) * (-B) -> A * B */
1448 (mult:c (convert1? (negate @0)) (convert2? negate_expr_p@1))
1449 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
1450 && tree_nop_conversion_p (type, TREE_TYPE (@1)))
1451 (mult (convert @0) (convert (negate @1)))))
1453 /* -(A + B) -> (-B) - A. */
1455 (negate (plus:c @0 negate_expr_p@1))
1456 (if (!HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type))
1457 && !HONOR_SIGNED_ZEROS (element_mode (type)))
1458 (minus (negate @1) @0)))
1460 /* -(A - B) -> B - A. */
1462 (negate (minus @0 @1))
1463 (if ((ANY_INTEGRAL_TYPE_P (type) && !TYPE_OVERFLOW_SANITIZED (type))
1464 || (FLOAT_TYPE_P (type)
1465 && !HONOR_SIGN_DEPENDENT_ROUNDING (type)
1466 && !HONOR_SIGNED_ZEROS (type)))
1469 (negate (pointer_diff @0 @1))
1470 (if (TYPE_OVERFLOW_UNDEFINED (type))
1471 (pointer_diff @1 @0)))
1473 /* A - B -> A + (-B) if B is easily negatable. */
1475 (minus @0 negate_expr_p@1)
1476 (if (!FIXED_POINT_TYPE_P (type))
1477 (plus @0 (negate @1))))
1479 /* Try to fold (type) X op CST -> (type) (X op ((type-x) CST))
1481 For bitwise binary operations apply operand conversions to the
1482 binary operation result instead of to the operands. This allows
1483 to combine successive conversions and bitwise binary operations.
1484 We combine the above two cases by using a conditional convert. */
1485 (for bitop (bit_and bit_ior bit_xor)
1487 (bitop (convert@2 @0) (convert?@3 @1))
1488 (if (((TREE_CODE (@1) == INTEGER_CST
1489 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
1490 && int_fits_type_p (@1, TREE_TYPE (@0)))
1491 || types_match (@0, @1))
1492 /* ??? This transform conflicts with fold-const.c doing
1493 Convert (T)(x & c) into (T)x & (T)c, if c is an integer
1494 constants (if x has signed type, the sign bit cannot be set
1495 in c). This folds extension into the BIT_AND_EXPR.
1496 Restrict it to GIMPLE to avoid endless recursions. */
1497 && (bitop != BIT_AND_EXPR || GIMPLE)
1498 && (/* That's a good idea if the conversion widens the operand, thus
1499 after hoisting the conversion the operation will be narrower. */
1500 TYPE_PRECISION (TREE_TYPE (@0)) < TYPE_PRECISION (type)
1501 /* It's also a good idea if the conversion is to a non-integer
1503 || GET_MODE_CLASS (TYPE_MODE (type)) != MODE_INT
1504 /* Or if the precision of TO is not the same as the precision
1506 || !type_has_mode_precision_p (type)
1507 /* In GIMPLE, getting rid of 2 conversions for one new results
1510 && TREE_CODE (@1) != INTEGER_CST
1511 && tree_nop_conversion_p (type, TREE_TYPE (@0))
1513 && single_use (@3))))
1514 (convert (bitop @0 (convert @1)))))
1515 /* In GIMPLE, getting rid of 2 conversions for one new results
1518 (convert (bitop:cs@2 (nop_convert:s @0) @1))
1520 && TREE_CODE (@1) != INTEGER_CST
1521 && tree_nop_conversion_p (type, TREE_TYPE (@2))
1522 && types_match (type, @0))
1523 (bitop @0 (convert @1)))))
1525 (for bitop (bit_and bit_ior)
1526 rbitop (bit_ior bit_and)
1527 /* (x | y) & x -> x */
1528 /* (x & y) | x -> x */
1530 (bitop:c (rbitop:c @0 @1) @0)
1532 /* (~x | y) & x -> x & y */
1533 /* (~x & y) | x -> x | y */
1535 (bitop:c (rbitop:c (bit_not @0) @1) @0)
1538 /* ((x | y) & z) | x -> (z & y) | x */
1540 (bit_ior:c (bit_and:cs (bit_ior:cs @0 @1) @2) @0)
1541 (bit_ior (bit_and @2 @1) @0))
1543 /* (x | CST1) & CST2 -> (x & CST2) | (CST1 & CST2) */
1545 (bit_and (bit_ior @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
1546 (bit_ior (bit_and @0 @2) (bit_and @1 @2)))
1548 /* Combine successive equal operations with constants. */
1549 (for bitop (bit_and bit_ior bit_xor)
1551 (bitop (bitop @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
1552 (if (!CONSTANT_CLASS_P (@0))
1553 /* This is the canonical form regardless of whether (bitop @1 @2) can be
1554 folded to a constant. */
1555 (bitop @0 (bitop @1 @2))
1556 /* In this case we have three constants and (bitop @0 @1) doesn't fold
1557 to a constant. This can happen if @0 or @1 is a POLY_INT_CST and if
1558 the values involved are such that the operation can't be decided at
1559 compile time. Try folding one of @0 or @1 with @2 to see whether
1560 that combination can be decided at compile time.
1562 Keep the existing form if both folds fail, to avoid endless
1564 (with { tree cst1 = const_binop (bitop, type, @0, @2); }
1566 (bitop @1 { cst1; })
1567 (with { tree cst2 = const_binop (bitop, type, @1, @2); }
1569 (bitop @0 { cst2; }))))))))
1571 /* Try simple folding for X op !X, and X op X with the help
1572 of the truth_valued_p and logical_inverted_value predicates. */
1573 (match truth_valued_p
1575 (if (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1)))
1576 (for op (tcc_comparison truth_and truth_andif truth_or truth_orif truth_xor)
1577 (match truth_valued_p
1579 (match truth_valued_p
1582 (match (logical_inverted_value @0)
1584 (match (logical_inverted_value @0)
1585 (bit_not truth_valued_p@0))
1586 (match (logical_inverted_value @0)
1587 (eq @0 integer_zerop))
1588 (match (logical_inverted_value @0)
1589 (ne truth_valued_p@0 integer_truep))
1590 (match (logical_inverted_value @0)
1591 (bit_xor truth_valued_p@0 integer_truep))
1595 (bit_and:c @0 (logical_inverted_value @0))
1596 { build_zero_cst (type); })
1597 /* X | !X and X ^ !X -> 1, , if X is truth-valued. */
1598 (for op (bit_ior bit_xor)
1600 (op:c truth_valued_p@0 (logical_inverted_value @0))
1601 { constant_boolean_node (true, type); }))
1602 /* X ==/!= !X is false/true. */
1605 (op:c truth_valued_p@0 (logical_inverted_value @0))
1606 { constant_boolean_node (op == NE_EXPR ? true : false, type); }))
1610 (bit_not (bit_not @0))
1613 /* Convert ~ (-A) to A - 1. */
1615 (bit_not (convert? (negate @0)))
1616 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1617 || !TYPE_UNSIGNED (TREE_TYPE (@0)))
1618 (convert (minus @0 { build_each_one_cst (TREE_TYPE (@0)); }))))
1620 /* Convert - (~A) to A + 1. */
1622 (negate (nop_convert? (bit_not @0)))
1623 (plus (view_convert @0) { build_each_one_cst (type); }))
1625 /* Convert ~ (A - 1) or ~ (A + -1) to -A. */
1627 (bit_not (convert? (minus @0 integer_each_onep)))
1628 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1629 || !TYPE_UNSIGNED (TREE_TYPE (@0)))
1630 (convert (negate @0))))
1632 (bit_not (convert? (plus @0 integer_all_onesp)))
1633 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1634 || !TYPE_UNSIGNED (TREE_TYPE (@0)))
1635 (convert (negate @0))))
1637 /* Part of convert ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify. */
1639 (bit_not (convert? (bit_xor @0 INTEGER_CST@1)))
1640 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1641 (convert (bit_xor @0 (bit_not @1)))))
1643 (bit_not (convert? (bit_xor:c (bit_not @0) @1)))
1644 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1645 (convert (bit_xor @0 @1))))
1647 /* Otherwise prefer ~(X ^ Y) to ~X ^ Y as more canonical. */
1649 (bit_xor:c (nop_convert?:s (bit_not:s @0)) @1)
1650 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1651 (bit_not (bit_xor (view_convert @0) @1))))
1653 /* (x & ~m) | (y & m) -> ((x ^ y) & m) ^ x */
1655 (bit_ior:c (bit_and:cs @0 (bit_not @2)) (bit_and:cs @1 @2))
1656 (bit_xor (bit_and (bit_xor @0 @1) @2) @0))
1658 /* Fold A - (A & B) into ~B & A. */
1660 (minus (convert1? @0) (convert2?:s (bit_and:cs @@0 @1)))
1661 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
1662 && tree_nop_conversion_p (type, TREE_TYPE (@1)))
1663 (convert (bit_and (bit_not @1) @0))))
1665 /* (m1 CMP m2) * d -> (m1 CMP m2) ? d : 0 */
1666 (for cmp (gt lt ge le)
1668 (mult (convert (cmp @0 @1)) @2)
1669 (if (GIMPLE || !TREE_SIDE_EFFECTS (@2))
1670 (cond (cmp @0 @1) @2 { build_zero_cst (type); }))))
1672 /* For integral types with undefined overflow and C != 0 fold
1673 x * C EQ/NE y * C into x EQ/NE y. */
1676 (cmp (mult:c @0 @1) (mult:c @2 @1))
1677 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
1678 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1679 && tree_expr_nonzero_p (@1))
1682 /* For integral types with wrapping overflow and C odd fold
1683 x * C EQ/NE y * C into x EQ/NE y. */
1686 (cmp (mult @0 INTEGER_CST@1) (mult @2 @1))
1687 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
1688 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))
1689 && (TREE_INT_CST_LOW (@1) & 1) != 0)
1692 /* For integral types with undefined overflow and C != 0 fold
1693 x * C RELOP y * C into:
1695 x RELOP y for nonnegative C
1696 y RELOP x for negative C */
1697 (for cmp (lt gt le ge)
1699 (cmp (mult:c @0 @1) (mult:c @2 @1))
1700 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
1701 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1702 (if (tree_expr_nonnegative_p (@1) && tree_expr_nonzero_p (@1))
1704 (if (TREE_CODE (@1) == INTEGER_CST
1705 && wi::neg_p (wi::to_wide (@1), TYPE_SIGN (TREE_TYPE (@1))))
1708 /* (X - 1U) <= INT_MAX-1U into (int) X > 0. */
1712 (cmp (plus @0 integer_minus_onep@1) INTEGER_CST@2)
1713 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1714 && TYPE_UNSIGNED (TREE_TYPE (@0))
1715 && TYPE_PRECISION (TREE_TYPE (@0)) > 1
1716 && (wi::to_wide (@2)
1717 == wi::max_value (TYPE_PRECISION (TREE_TYPE (@0)), SIGNED) - 1))
1718 (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
1719 (icmp (convert:stype @0) { build_int_cst (stype, 0); })))))
1721 /* X / 4 < Y / 4 iff X < Y when the division is known to be exact. */
1722 (for cmp (simple_comparison)
1724 (cmp (convert?@3 (exact_div @0 INTEGER_CST@2)) (convert? (exact_div @1 @2)))
1725 (if (element_precision (@3) >= element_precision (@0)
1726 && types_match (@0, @1))
1727 (if (wi::lt_p (wi::to_wide (@2), 0, TYPE_SIGN (TREE_TYPE (@2))))
1728 (if (!TYPE_UNSIGNED (TREE_TYPE (@3)))
1730 (if (tree_expr_nonzero_p (@0) && tree_expr_nonzero_p (@1))
1733 tree utype = unsigned_type_for (TREE_TYPE (@0));
1735 (cmp (convert:utype @1) (convert:utype @0)))))
1736 (if (wi::gt_p (wi::to_wide (@2), 1, TYPE_SIGN (TREE_TYPE (@2))))
1737 (if (TYPE_UNSIGNED (TREE_TYPE (@0)) || !TYPE_UNSIGNED (TREE_TYPE (@3)))
1741 tree utype = unsigned_type_for (TREE_TYPE (@0));
1743 (cmp (convert:utype @0) (convert:utype @1)))))))))
1745 /* X / C1 op C2 into a simple range test. */
1746 (for cmp (simple_comparison)
1748 (cmp (trunc_div:s @0 INTEGER_CST@1) INTEGER_CST@2)
1749 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1750 && integer_nonzerop (@1)
1751 && !TREE_OVERFLOW (@1)
1752 && !TREE_OVERFLOW (@2))
1753 (with { tree lo, hi; bool neg_overflow;
1754 enum tree_code code = fold_div_compare (cmp, @1, @2, &lo, &hi,
1757 (if (code == LT_EXPR || code == GE_EXPR)
1758 (if (TREE_OVERFLOW (lo))
1759 { build_int_cst (type, (code == LT_EXPR) ^ neg_overflow); }
1760 (if (code == LT_EXPR)
1763 (if (code == LE_EXPR || code == GT_EXPR)
1764 (if (TREE_OVERFLOW (hi))
1765 { build_int_cst (type, (code == LE_EXPR) ^ neg_overflow); }
1766 (if (code == LE_EXPR)
1770 { build_int_cst (type, code == NE_EXPR); })
1771 (if (code == EQ_EXPR && !hi)
1773 (if (code == EQ_EXPR && !lo)
1775 (if (code == NE_EXPR && !hi)
1777 (if (code == NE_EXPR && !lo)
1780 { build_range_check (UNKNOWN_LOCATION, type, @0, code == EQ_EXPR,
1784 tree etype = range_check_type (TREE_TYPE (@0));
1787 hi = fold_convert (etype, hi);
1788 lo = fold_convert (etype, lo);
1789 hi = const_binop (MINUS_EXPR, etype, hi, lo);
1792 (if (etype && hi && !TREE_OVERFLOW (hi))
1793 (if (code == EQ_EXPR)
1794 (le (minus (convert:etype @0) { lo; }) { hi; })
1795 (gt (minus (convert:etype @0) { lo; }) { hi; })))))))))
1797 /* X + Z < Y + Z is the same as X < Y when there is no overflow. */
1798 (for op (lt le ge gt)
1800 (op (plus:c @0 @2) (plus:c @1 @2))
1801 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1802 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1804 /* For equality and subtraction, this is also true with wrapping overflow. */
1805 (for op (eq ne minus)
1807 (op (plus:c @0 @2) (plus:c @1 @2))
1808 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1809 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1810 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
1813 /* X - Z < Y - Z is the same as X < Y when there is no overflow. */
1814 (for op (lt le ge gt)
1816 (op (minus @0 @2) (minus @1 @2))
1817 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1818 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1820 /* For equality and subtraction, this is also true with wrapping overflow. */
1821 (for op (eq ne minus)
1823 (op (minus @0 @2) (minus @1 @2))
1824 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1825 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1826 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
1828 /* And for pointers... */
1829 (for op (simple_comparison)
1831 (op (pointer_diff@3 @0 @2) (pointer_diff @1 @2))
1832 (if (!TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
1835 (minus (pointer_diff@3 @0 @2) (pointer_diff @1 @2))
1836 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@3))
1837 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
1838 (pointer_diff @0 @1)))
1840 /* Z - X < Z - Y is the same as Y < X when there is no overflow. */
1841 (for op (lt le ge gt)
1843 (op (minus @2 @0) (minus @2 @1))
1844 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1845 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1847 /* For equality and subtraction, this is also true with wrapping overflow. */
1848 (for op (eq ne minus)
1850 (op (minus @2 @0) (minus @2 @1))
1851 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1852 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1853 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
1855 /* And for pointers... */
1856 (for op (simple_comparison)
1858 (op (pointer_diff@3 @2 @0) (pointer_diff @2 @1))
1859 (if (!TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
1862 (minus (pointer_diff@3 @2 @0) (pointer_diff @2 @1))
1863 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@3))
1864 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
1865 (pointer_diff @1 @0)))
1867 /* X + Y < Y is the same as X < 0 when there is no overflow. */
1868 (for op (lt le gt ge)
1870 (op:c (plus:c@2 @0 @1) @1)
1871 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1872 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1873 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0))
1874 && (CONSTANT_CLASS_P (@0) || single_use (@2)))
1875 (op @0 { build_zero_cst (TREE_TYPE (@0)); }))))
1876 /* For equality, this is also true with wrapping overflow. */
1879 (op:c (nop_convert?@3 (plus:c@2 @0 (convert1? @1))) (convert2? @1))
1880 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1881 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1882 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
1883 && (CONSTANT_CLASS_P (@0) || (single_use (@2) && single_use (@3)))
1884 && tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@2))
1885 && tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@1)))
1886 (op @0 { build_zero_cst (TREE_TYPE (@0)); })))
1888 (op:c (nop_convert?@3 (pointer_plus@2 (convert1? @0) @1)) (convert2? @0))
1889 (if (tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@0))
1890 && tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0))
1891 && (CONSTANT_CLASS_P (@1) || (single_use (@2) && single_use (@3))))
1892 (op @1 { build_zero_cst (TREE_TYPE (@1)); }))))
1894 /* X - Y < X is the same as Y > 0 when there is no overflow.
1895 For equality, this is also true with wrapping overflow. */
1896 (for op (simple_comparison)
1898 (op:c @0 (minus@2 @0 @1))
1899 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1900 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1901 || ((op == EQ_EXPR || op == NE_EXPR)
1902 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
1903 && (CONSTANT_CLASS_P (@1) || single_use (@2)))
1904 (op @1 { build_zero_cst (TREE_TYPE (@1)); }))))
1907 (X / Y) == 0 -> X < Y if X, Y are unsigned.
1908 (X / Y) != 0 -> X >= Y, if X, Y are unsigned. */
1912 (cmp (trunc_div @0 @1) integer_zerop)
1913 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
1914 /* Complex ==/!= is allowed, but not </>=. */
1915 && TREE_CODE (TREE_TYPE (@0)) != COMPLEX_TYPE
1916 && (VECTOR_TYPE_P (type) || !VECTOR_TYPE_P (TREE_TYPE (@0))))
1919 /* X == C - X can never be true if C is odd. */
1922 (cmp:c (convert? @0) (convert1? (minus INTEGER_CST@1 (convert2? @0))))
1923 (if (TREE_INT_CST_LOW (@1) & 1)
1924 { constant_boolean_node (cmp == NE_EXPR, type); })))
1926 /* Arguments on which one can call get_nonzero_bits to get the bits
1928 (match with_possible_nonzero_bits
1930 (match with_possible_nonzero_bits
1932 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))))
1933 /* Slightly extended version, do not make it recursive to keep it cheap. */
1934 (match (with_possible_nonzero_bits2 @0)
1935 with_possible_nonzero_bits@0)
1936 (match (with_possible_nonzero_bits2 @0)
1937 (bit_and:c with_possible_nonzero_bits@0 @2))
1939 /* Same for bits that are known to be set, but we do not have
1940 an equivalent to get_nonzero_bits yet. */
1941 (match (with_certain_nonzero_bits2 @0)
1943 (match (with_certain_nonzero_bits2 @0)
1944 (bit_ior @1 INTEGER_CST@0))
1946 /* X == C (or X & Z == Y | C) is impossible if ~nonzero(X) & C != 0. */
1949 (cmp:c (with_possible_nonzero_bits2 @0) (with_certain_nonzero_bits2 @1))
1950 (if (wi::bit_and_not (wi::to_wide (@1), get_nonzero_bits (@0)) != 0)
1951 { constant_boolean_node (cmp == NE_EXPR, type); })))
1953 /* ((X inner_op C0) outer_op C1)
1954 With X being a tree where value_range has reasoned certain bits to always be
1955 zero throughout its computed value range,
1956 inner_op = {|,^}, outer_op = {|,^} and inner_op != outer_op
1957 where zero_mask has 1's for all bits that are sure to be 0 in
1959 if (inner_op == '^') C0 &= ~C1;
1960 if ((C0 & ~zero_mask) == 0) then emit (X outer_op (C0 outer_op C1)
1961 if ((C1 & ~zero_mask) == 0) then emit (X inner_op (C0 outer_op C1)
1963 (for inner_op (bit_ior bit_xor)
1964 outer_op (bit_xor bit_ior)
1967 (inner_op:s @2 INTEGER_CST@0) INTEGER_CST@1)
1971 wide_int zero_mask_not;
1975 if (TREE_CODE (@2) == SSA_NAME)
1976 zero_mask_not = get_nonzero_bits (@2);
1980 if (inner_op == BIT_XOR_EXPR)
1982 C0 = wi::bit_and_not (wi::to_wide (@0), wi::to_wide (@1));
1983 cst_emit = C0 | wi::to_wide (@1);
1987 C0 = wi::to_wide (@0);
1988 cst_emit = C0 ^ wi::to_wide (@1);
1991 (if (!fail && (C0 & zero_mask_not) == 0)
1992 (outer_op @2 { wide_int_to_tree (type, cst_emit); })
1993 (if (!fail && (wi::to_wide (@1) & zero_mask_not) == 0)
1994 (inner_op @2 { wide_int_to_tree (type, cst_emit); }))))))
1996 /* Associate (p +p off1) +p off2 as (p +p (off1 + off2)). */
1998 (pointer_plus (pointer_plus:s @0 @1) @3)
1999 (pointer_plus @0 (plus @1 @3)))
2005 tem4 = (unsigned long) tem3;
2010 (pointer_plus @0 (convert?@2 (minus@3 (convert @1) (convert @0))))
2011 /* Conditionally look through a sign-changing conversion. */
2012 (if (TYPE_PRECISION (TREE_TYPE (@2)) == TYPE_PRECISION (TREE_TYPE (@3))
2013 && ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@1)))
2014 || (GENERIC && type == TREE_TYPE (@1))))
2017 (pointer_plus @0 (convert?@2 (pointer_diff@3 @1 @@0)))
2018 (if (TYPE_PRECISION (TREE_TYPE (@2)) >= TYPE_PRECISION (TREE_TYPE (@3)))
2022 tem = (sizetype) ptr;
2026 and produce the simpler and easier to analyze with respect to alignment
2027 ... = ptr & ~algn; */
2029 (pointer_plus @0 (negate (bit_and (convert @0) INTEGER_CST@1)))
2030 (with { tree algn = wide_int_to_tree (TREE_TYPE (@0), ~wi::to_wide (@1)); }
2031 (bit_and @0 { algn; })))
2033 /* Try folding difference of addresses. */
2035 (minus (convert ADDR_EXPR@0) (convert @1))
2036 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2037 (with { poly_int64 diff; }
2038 (if (ptr_difference_const (@0, @1, &diff))
2039 { build_int_cst_type (type, diff); }))))
2041 (minus (convert @0) (convert ADDR_EXPR@1))
2042 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2043 (with { poly_int64 diff; }
2044 (if (ptr_difference_const (@0, @1, &diff))
2045 { build_int_cst_type (type, diff); }))))
2047 (pointer_diff (convert?@2 ADDR_EXPR@0) (convert1?@3 @1))
2048 (if (tree_nop_conversion_p (TREE_TYPE(@2), TREE_TYPE (@0))
2049 && tree_nop_conversion_p (TREE_TYPE(@3), TREE_TYPE (@1)))
2050 (with { poly_int64 diff; }
2051 (if (ptr_difference_const (@0, @1, &diff))
2052 { build_int_cst_type (type, diff); }))))
2054 (pointer_diff (convert?@2 @0) (convert1?@3 ADDR_EXPR@1))
2055 (if (tree_nop_conversion_p (TREE_TYPE(@2), TREE_TYPE (@0))
2056 && tree_nop_conversion_p (TREE_TYPE(@3), TREE_TYPE (@1)))
2057 (with { poly_int64 diff; }
2058 (if (ptr_difference_const (@0, @1, &diff))
2059 { build_int_cst_type (type, diff); }))))
2061 /* Canonicalize (T *)(ptr - ptr-cst) to &MEM[ptr + -ptr-cst]. */
2063 (convert (pointer_diff @0 INTEGER_CST@1))
2064 (if (POINTER_TYPE_P (type))
2065 { build_fold_addr_expr_with_type
2066 (build2 (MEM_REF, char_type_node, @0,
2067 wide_int_to_tree (ptr_type_node, wi::neg (wi::to_wide (@1)))),
2070 /* If arg0 is derived from the address of an object or function, we may
2071 be able to fold this expression using the object or function's
2074 (bit_and (convert? @0) INTEGER_CST@1)
2075 (if (POINTER_TYPE_P (TREE_TYPE (@0))
2076 && tree_nop_conversion_p (type, TREE_TYPE (@0)))
2080 unsigned HOST_WIDE_INT bitpos;
2081 get_pointer_alignment_1 (@0, &align, &bitpos);
2083 (if (wi::ltu_p (wi::to_wide (@1), align / BITS_PER_UNIT))
2084 { wide_int_to_tree (type, (wi::to_wide (@1)
2085 & (bitpos / BITS_PER_UNIT))); }))))
2089 (if (INTEGRAL_TYPE_P (type)
2090 && wi::eq_p (wi::to_wide (t), wi::min_value (type)))))
2094 (if (INTEGRAL_TYPE_P (type)
2095 && wi::eq_p (wi::to_wide (t), wi::max_value (type)))))
2097 /* x > y && x != XXX_MIN --> x > y
2098 x > y && x == XXX_MIN --> false . */
2101 (bit_and:c (gt:c@2 @0 @1) (eqne @0 min_value))
2103 (if (eqne == EQ_EXPR)
2104 { constant_boolean_node (false, type); })
2105 (if (eqne == NE_EXPR)
2109 /* x < y && x != XXX_MAX --> x < y
2110 x < y && x == XXX_MAX --> false. */
2113 (bit_and:c (lt:c@2 @0 @1) (eqne @0 max_value))
2115 (if (eqne == EQ_EXPR)
2116 { constant_boolean_node (false, type); })
2117 (if (eqne == NE_EXPR)
2121 /* x <= y && x == XXX_MIN --> x == XXX_MIN. */
2123 (bit_and:c (le:c @0 @1) (eq@2 @0 min_value))
2126 /* x >= y && x == XXX_MAX --> x == XXX_MAX. */
2128 (bit_and:c (ge:c @0 @1) (eq@2 @0 max_value))
2131 /* x > y || x != XXX_MIN --> x != XXX_MIN. */
2133 (bit_ior:c (gt:c @0 @1) (ne@2 @0 min_value))
2136 /* x <= y || x != XXX_MIN --> true. */
2138 (bit_ior:c (le:c @0 @1) (ne @0 min_value))
2139 { constant_boolean_node (true, type); })
2141 /* x <= y || x == XXX_MIN --> x <= y. */
2143 (bit_ior:c (le:c@2 @0 @1) (eq @0 min_value))
2146 /* x < y || x != XXX_MAX --> x != XXX_MAX. */
2148 (bit_ior:c (lt:c @0 @1) (ne@2 @0 max_value))
2151 /* x >= y || x != XXX_MAX --> true
2152 x >= y || x == XXX_MAX --> x >= y. */
2155 (bit_ior:c (ge:c@2 @0 @1) (eqne @0 max_value))
2157 (if (eqne == EQ_EXPR)
2159 (if (eqne == NE_EXPR)
2160 { constant_boolean_node (true, type); }))))
2162 /* Convert (X == CST1) && (X OP2 CST2) to a known value
2163 based on CST1 OP2 CST2. Similarly for (X != CST1). */
2166 (for code2 (eq ne lt gt le ge)
2168 (bit_and:c (code1@3 @0 INTEGER_CST@1) (code2@4 @0 INTEGER_CST@2))
2171 int cmp = tree_int_cst_compare (@1, @2);
2175 case EQ_EXPR: val = (cmp == 0); break;
2176 case NE_EXPR: val = (cmp != 0); break;
2177 case LT_EXPR: val = (cmp < 0); break;
2178 case GT_EXPR: val = (cmp > 0); break;
2179 case LE_EXPR: val = (cmp <= 0); break;
2180 case GE_EXPR: val = (cmp >= 0); break;
2181 default: gcc_unreachable ();
2185 (if (code1 == EQ_EXPR && val) @3)
2186 (if (code1 == EQ_EXPR && !val) { constant_boolean_node (false, type); })
2187 (if (code1 == NE_EXPR && !val) @4))))))
2189 /* Convert (X OP1 CST1) && (X OP2 CST2). */
2191 (for code1 (lt le gt ge)
2192 (for code2 (lt le gt ge)
2194 (bit_and (code1:c@3 @0 INTEGER_CST@1) (code2:c@4 @0 INTEGER_CST@2))
2197 int cmp = tree_int_cst_compare (@1, @2);
2200 /* Choose the more restrictive of two < or <= comparisons. */
2201 (if ((code1 == LT_EXPR || code1 == LE_EXPR)
2202 && (code2 == LT_EXPR || code2 == LE_EXPR))
2203 (if ((cmp < 0) || (cmp == 0 && code1 == LT_EXPR))
2206 /* Likewise chose the more restrictive of two > or >= comparisons. */
2207 (if ((code1 == GT_EXPR || code1 == GE_EXPR)
2208 && (code2 == GT_EXPR || code2 == GE_EXPR))
2209 (if ((cmp > 0) || (cmp == 0 && code1 == GT_EXPR))
2212 /* Check for singleton ranges. */
2214 && ((code1 == LE_EXPR && code2 == GE_EXPR)
2215 || (code1 == GE_EXPR && code2 == LE_EXPR)))
2217 /* Check for disjoint ranges. */
2219 && (code1 == LT_EXPR || code1 == LE_EXPR)
2220 && (code2 == GT_EXPR || code2 == GE_EXPR))
2221 { constant_boolean_node (false, type); })
2223 && (code1 == GT_EXPR || code1 == GE_EXPR)
2224 && (code2 == LT_EXPR || code2 == LE_EXPR))
2225 { constant_boolean_node (false, type); })
2228 /* Convert (X == CST1) || (X OP2 CST2) to a known value
2229 based on CST1 OP2 CST2. Similarly for (X != CST1). */
2232 (for code2 (eq ne lt gt le ge)
2234 (bit_ior:c (code1@3 @0 INTEGER_CST@1) (code2@4 @0 INTEGER_CST@2))
2237 int cmp = tree_int_cst_compare (@1, @2);
2241 case EQ_EXPR: val = (cmp == 0); break;
2242 case NE_EXPR: val = (cmp != 0); break;
2243 case LT_EXPR: val = (cmp < 0); break;
2244 case GT_EXPR: val = (cmp > 0); break;
2245 case LE_EXPR: val = (cmp <= 0); break;
2246 case GE_EXPR: val = (cmp >= 0); break;
2247 default: gcc_unreachable ();
2251 (if (code1 == EQ_EXPR && val) @4)
2252 (if (code1 == NE_EXPR && val) { constant_boolean_node (true, type); })
2253 (if (code1 == NE_EXPR && !val) @3))))))
2255 /* Convert (X OP1 CST1) || (X OP2 CST2). */
2257 (for code1 (lt le gt ge)
2258 (for code2 (lt le gt ge)
2260 (bit_ior (code1@3 @0 INTEGER_CST@1) (code2@4 @0 INTEGER_CST@2))
2263 int cmp = tree_int_cst_compare (@1, @2);
2266 /* Choose the more restrictive of two < or <= comparisons. */
2267 (if ((code1 == LT_EXPR || code1 == LE_EXPR)
2268 && (code2 == LT_EXPR || code2 == LE_EXPR))
2269 (if ((cmp < 0) || (cmp == 0 && code1 == LT_EXPR))
2272 /* Likewise chose the more restrictive of two > or >= comparisons. */
2273 (if ((code1 == GT_EXPR || code1 == GE_EXPR)
2274 && (code2 == GT_EXPR || code2 == GE_EXPR))
2275 (if ((cmp > 0) || (cmp == 0 && code1 == GT_EXPR))
2278 /* Check for singleton ranges. */
2280 && ((code1 == LT_EXPR && code2 == GT_EXPR)
2281 || (code1 == GT_EXPR && code2 == LT_EXPR)))
2283 /* Check for disjoint ranges. */
2285 && (code1 == LT_EXPR || code1 == LE_EXPR)
2286 && (code2 == GT_EXPR || code2 == GE_EXPR))
2287 { constant_boolean_node (true, type); })
2289 && (code1 == GT_EXPR || code1 == GE_EXPR)
2290 && (code2 == LT_EXPR || code2 == LE_EXPR))
2291 { constant_boolean_node (true, type); })
2294 /* We can't reassociate at all for saturating types. */
2295 (if (!TYPE_SATURATING (type))
2297 /* Contract negates. */
2298 /* A + (-B) -> A - B */
2300 (plus:c @0 (convert? (negate @1)))
2301 /* Apply STRIP_NOPS on the negate. */
2302 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
2303 && !TYPE_OVERFLOW_SANITIZED (type))
2307 if (INTEGRAL_TYPE_P (type)
2308 && TYPE_OVERFLOW_WRAPS (type) != TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
2309 t1 = TYPE_OVERFLOW_WRAPS (type) ? type : TREE_TYPE (@1);
2311 (convert (minus (convert:t1 @0) (convert:t1 @1))))))
2312 /* A - (-B) -> A + B */
2314 (minus @0 (convert? (negate @1)))
2315 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
2316 && !TYPE_OVERFLOW_SANITIZED (type))
2320 if (INTEGRAL_TYPE_P (type)
2321 && TYPE_OVERFLOW_WRAPS (type) != TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
2322 t1 = TYPE_OVERFLOW_WRAPS (type) ? type : TREE_TYPE (@1);
2324 (convert (plus (convert:t1 @0) (convert:t1 @1))))))
2326 Sign-extension is ok except for INT_MIN, which thankfully cannot
2327 happen without overflow. */
2329 (negate (convert (negate @1)))
2330 (if (INTEGRAL_TYPE_P (type)
2331 && (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@1))
2332 || (!TYPE_UNSIGNED (TREE_TYPE (@1))
2333 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))))
2334 && !TYPE_OVERFLOW_SANITIZED (type)
2335 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@1)))
2338 (negate (convert negate_expr_p@1))
2339 (if (SCALAR_FLOAT_TYPE_P (type)
2340 && ((DECIMAL_FLOAT_TYPE_P (type)
2341 == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1))
2342 && TYPE_PRECISION (type) >= TYPE_PRECISION (TREE_TYPE (@1)))
2343 || !HONOR_SIGN_DEPENDENT_ROUNDING (type)))
2344 (convert (negate @1))))
2346 (negate (nop_convert? (negate @1)))
2347 (if (!TYPE_OVERFLOW_SANITIZED (type)
2348 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@1)))
2351 /* We can't reassociate floating-point unless -fassociative-math
2352 or fixed-point plus or minus because of saturation to +-Inf. */
2353 (if ((!FLOAT_TYPE_P (type) || flag_associative_math)
2354 && !FIXED_POINT_TYPE_P (type))
2356 /* Match patterns that allow contracting a plus-minus pair
2357 irrespective of overflow issues. */
2358 /* (A +- B) - A -> +- B */
2359 /* (A +- B) -+ B -> A */
2360 /* A - (A +- B) -> -+ B */
2361 /* A +- (B -+ A) -> +- B */
2363 (minus (nop_convert1? (plus:c (nop_convert2? @0) @1)) @0)
2366 (minus (nop_convert1? (minus (nop_convert2? @0) @1)) @0)
2367 (if (!ANY_INTEGRAL_TYPE_P (type)
2368 || TYPE_OVERFLOW_WRAPS (type))
2369 (negate (view_convert @1))
2370 (view_convert (negate @1))))
2372 (plus:c (nop_convert1? (minus @0 (nop_convert2? @1))) @1)
2375 (minus @0 (nop_convert1? (plus:c (nop_convert2? @0) @1)))
2376 (if (!ANY_INTEGRAL_TYPE_P (type)
2377 || TYPE_OVERFLOW_WRAPS (type))
2378 (negate (view_convert @1))
2379 (view_convert (negate @1))))
2381 (minus @0 (nop_convert1? (minus (nop_convert2? @0) @1)))
2383 /* (A +- B) + (C - A) -> C +- B */
2384 /* (A + B) - (A - C) -> B + C */
2385 /* More cases are handled with comparisons. */
2387 (plus:c (plus:c @0 @1) (minus @2 @0))
2390 (plus:c (minus @0 @1) (minus @2 @0))
2393 (plus:c (pointer_diff @0 @1) (pointer_diff @2 @0))
2394 (if (TYPE_OVERFLOW_UNDEFINED (type)
2395 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0)))
2396 (pointer_diff @2 @1)))
2398 (minus (plus:c @0 @1) (minus @0 @2))
2401 /* (A +- CST1) +- CST2 -> A + CST3
2402 Use view_convert because it is safe for vectors and equivalent for
2404 (for outer_op (plus minus)
2405 (for inner_op (plus minus)
2406 neg_inner_op (minus plus)
2408 (outer_op (nop_convert? (inner_op @0 CONSTANT_CLASS_P@1))
2410 /* If one of the types wraps, use that one. */
2411 (if (!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_WRAPS (type))
2412 /* If all 3 captures are CONSTANT_CLASS_P, punt, as we might recurse
2413 forever if something doesn't simplify into a constant. */
2414 (if (!CONSTANT_CLASS_P (@0))
2415 (if (outer_op == PLUS_EXPR)
2416 (plus (view_convert @0) (inner_op @2 (view_convert @1)))
2417 (minus (view_convert @0) (neg_inner_op @2 (view_convert @1)))))
2418 (if (!ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2419 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
2420 (if (outer_op == PLUS_EXPR)
2421 (view_convert (plus @0 (inner_op (view_convert @2) @1)))
2422 (view_convert (minus @0 (neg_inner_op (view_convert @2) @1))))
2423 /* If the constant operation overflows we cannot do the transform
2424 directly as we would introduce undefined overflow, for example
2425 with (a - 1) + INT_MIN. */
2426 (if (types_match (type, @0))
2427 (with { tree cst = const_binop (outer_op == inner_op
2428 ? PLUS_EXPR : MINUS_EXPR,
2430 (if (cst && !TREE_OVERFLOW (cst))
2431 (inner_op @0 { cst; } )
2432 /* X+INT_MAX+1 is X-INT_MIN. */
2433 (if (INTEGRAL_TYPE_P (type) && cst
2434 && wi::to_wide (cst) == wi::min_value (type))
2435 (neg_inner_op @0 { wide_int_to_tree (type, wi::to_wide (cst)); })
2436 /* Last resort, use some unsigned type. */
2437 (with { tree utype = unsigned_type_for (type); }
2439 (view_convert (inner_op
2440 (view_convert:utype @0)
2442 { drop_tree_overflow (cst); }))))))))))))))
2444 /* (CST1 - A) +- CST2 -> CST3 - A */
2445 (for outer_op (plus minus)
2447 (outer_op (nop_convert? (minus CONSTANT_CLASS_P@1 @0)) CONSTANT_CLASS_P@2)
2448 /* If one of the types wraps, use that one. */
2449 (if (!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_WRAPS (type))
2450 /* If all 3 captures are CONSTANT_CLASS_P, punt, as we might recurse
2451 forever if something doesn't simplify into a constant. */
2452 (if (!CONSTANT_CLASS_P (@0))
2453 (minus (outer_op (view_convert @1) @2) (view_convert @0)))
2454 (if (!ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2455 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
2456 (view_convert (minus (outer_op @1 (view_convert @2)) @0))
2457 (if (types_match (type, @0))
2458 (with { tree cst = const_binop (outer_op, type, @1, @2); }
2459 (if (cst && !TREE_OVERFLOW (cst))
2460 (minus { cst; } @0))))))))
2462 /* CST1 - (CST2 - A) -> CST3 + A
2463 Use view_convert because it is safe for vectors and equivalent for
2466 (minus CONSTANT_CLASS_P@1 (nop_convert? (minus CONSTANT_CLASS_P@2 @0)))
2467 /* If one of the types wraps, use that one. */
2468 (if (!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_WRAPS (type))
2469 /* If all 3 captures are CONSTANT_CLASS_P, punt, as we might recurse
2470 forever if something doesn't simplify into a constant. */
2471 (if (!CONSTANT_CLASS_P (@0))
2472 (plus (view_convert @0) (minus @1 (view_convert @2))))
2473 (if (!ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2474 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
2475 (view_convert (plus @0 (minus (view_convert @1) @2)))
2476 (if (types_match (type, @0))
2477 (with { tree cst = const_binop (MINUS_EXPR, type, @1, @2); }
2478 (if (cst && !TREE_OVERFLOW (cst))
2479 (plus { cst; } @0)))))))
2481 /* ((T)(A)) + CST -> (T)(A + CST) */
2484 (plus (convert SSA_NAME@0) INTEGER_CST@1)
2485 (if (TREE_CODE (TREE_TYPE (@0)) == INTEGER_TYPE
2486 && TREE_CODE (type) == INTEGER_TYPE
2487 && TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (@0))
2488 && int_fits_type_p (@1, TREE_TYPE (@0)))
2489 /* Perform binary operation inside the cast if the constant fits
2490 and (A + CST)'s range does not overflow. */
2493 wi::overflow_type min_ovf = wi::OVF_OVERFLOW,
2494 max_ovf = wi::OVF_OVERFLOW;
2495 tree inner_type = TREE_TYPE (@0);
2498 = wide_int::from (wi::to_wide (@1), TYPE_PRECISION (inner_type),
2499 TYPE_SIGN (inner_type));
2501 wide_int wmin0, wmax0;
2502 if (get_range_info (@0, &wmin0, &wmax0) == VR_RANGE)
2504 wi::add (wmin0, w1, TYPE_SIGN (inner_type), &min_ovf);
2505 wi::add (wmax0, w1, TYPE_SIGN (inner_type), &max_ovf);
2508 (if (min_ovf == wi::OVF_NONE && max_ovf == wi::OVF_NONE)
2509 (convert (plus @0 { wide_int_to_tree (TREE_TYPE (@0), w1); } )))
2513 /* ((T)(A + CST1)) + CST2 -> (T)(A) + (T)CST1 + CST2 */
2515 (for op (plus minus)
2517 (plus (convert:s (op:s @0 INTEGER_CST@1)) INTEGER_CST@2)
2518 (if (TREE_CODE (TREE_TYPE (@0)) == INTEGER_TYPE
2519 && TREE_CODE (type) == INTEGER_TYPE
2520 && TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (@0))
2521 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2522 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0))
2523 && TYPE_OVERFLOW_WRAPS (type))
2524 (plus (convert @0) (op @2 (convert @1))))))
2527 /* (T)(A) +- (T)(B) -> (T)(A +- B) only when (A +- B) could be simplified
2528 to a simple value. */
2530 (for op (plus minus)
2532 (op (convert @0) (convert @1))
2533 (if (INTEGRAL_TYPE_P (type)
2534 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
2535 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))
2536 && types_match (TREE_TYPE (@0), TREE_TYPE (@1))
2537 && !TYPE_OVERFLOW_TRAPS (type)
2538 && !TYPE_OVERFLOW_SANITIZED (type))
2539 (convert (op! @0 @1)))))
2544 (plus:c (bit_not @0) @0)
2545 (if (!TYPE_OVERFLOW_TRAPS (type))
2546 { build_all_ones_cst (type); }))
2550 (plus (convert? (bit_not @0)) integer_each_onep)
2551 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2552 (negate (convert @0))))
2556 (minus (convert? (negate @0)) integer_each_onep)
2557 (if (!TYPE_OVERFLOW_TRAPS (type)
2558 && tree_nop_conversion_p (type, TREE_TYPE (@0)))
2559 (bit_not (convert @0))))
2563 (minus integer_all_onesp @0)
2566 /* (T)(P + A) - (T)P -> (T) A */
2568 (minus (convert (plus:c @@0 @1))
2570 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
2571 /* For integer types, if A has a smaller type
2572 than T the result depends on the possible
2574 E.g. T=size_t, A=(unsigned)429497295, P>0.
2575 However, if an overflow in P + A would cause
2576 undefined behavior, we can assume that there
2578 || (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2579 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))))
2582 (minus (convert (pointer_plus @@0 @1))
2584 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
2585 /* For pointer types, if the conversion of A to the
2586 final type requires a sign- or zero-extension,
2587 then we have to punt - it is not defined which
2589 || (POINTER_TYPE_P (TREE_TYPE (@0))
2590 && TREE_CODE (@1) == INTEGER_CST
2591 && tree_int_cst_sign_bit (@1) == 0))
2594 (pointer_diff (pointer_plus @@0 @1) @0)
2595 /* The second argument of pointer_plus must be interpreted as signed, and
2596 thus sign-extended if necessary. */
2597 (with { tree stype = signed_type_for (TREE_TYPE (@1)); }
2598 /* Use view_convert instead of convert here, as POINTER_PLUS_EXPR
2599 second arg is unsigned even when we need to consider it as signed,
2600 we don't want to diagnose overflow here. */
2601 (convert (view_convert:stype @1))))
2603 /* (T)P - (T)(P + A) -> -(T) A */
2605 (minus (convert? @0)
2606 (convert (plus:c @@0 @1)))
2607 (if (INTEGRAL_TYPE_P (type)
2608 && TYPE_OVERFLOW_UNDEFINED (type)
2609 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
2610 (with { tree utype = unsigned_type_for (type); }
2611 (convert (negate (convert:utype @1))))
2612 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
2613 /* For integer types, if A has a smaller type
2614 than T the result depends on the possible
2616 E.g. T=size_t, A=(unsigned)429497295, P>0.
2617 However, if an overflow in P + A would cause
2618 undefined behavior, we can assume that there
2620 || (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2621 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))))
2622 (negate (convert @1)))))
2625 (convert (pointer_plus @@0 @1)))
2626 (if (INTEGRAL_TYPE_P (type)
2627 && TYPE_OVERFLOW_UNDEFINED (type)
2628 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
2629 (with { tree utype = unsigned_type_for (type); }
2630 (convert (negate (convert:utype @1))))
2631 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
2632 /* For pointer types, if the conversion of A to the
2633 final type requires a sign- or zero-extension,
2634 then we have to punt - it is not defined which
2636 || (POINTER_TYPE_P (TREE_TYPE (@0))
2637 && TREE_CODE (@1) == INTEGER_CST
2638 && tree_int_cst_sign_bit (@1) == 0))
2639 (negate (convert @1)))))
2641 (pointer_diff @0 (pointer_plus @@0 @1))
2642 /* The second argument of pointer_plus must be interpreted as signed, and
2643 thus sign-extended if necessary. */
2644 (with { tree stype = signed_type_for (TREE_TYPE (@1)); }
2645 /* Use view_convert instead of convert here, as POINTER_PLUS_EXPR
2646 second arg is unsigned even when we need to consider it as signed,
2647 we don't want to diagnose overflow here. */
2648 (negate (convert (view_convert:stype @1)))))
2650 /* (T)(P + A) - (T)(P + B) -> (T)A - (T)B */
2652 (minus (convert (plus:c @@0 @1))
2653 (convert (plus:c @0 @2)))
2654 (if (INTEGRAL_TYPE_P (type)
2655 && TYPE_OVERFLOW_UNDEFINED (type)
2656 && element_precision (type) <= element_precision (TREE_TYPE (@1))
2657 && element_precision (type) <= element_precision (TREE_TYPE (@2)))
2658 (with { tree utype = unsigned_type_for (type); }
2659 (convert (minus (convert:utype @1) (convert:utype @2))))
2660 (if (((element_precision (type) <= element_precision (TREE_TYPE (@1)))
2661 == (element_precision (type) <= element_precision (TREE_TYPE (@2))))
2662 && (element_precision (type) <= element_precision (TREE_TYPE (@1))
2663 /* For integer types, if A has a smaller type
2664 than T the result depends on the possible
2666 E.g. T=size_t, A=(unsigned)429497295, P>0.
2667 However, if an overflow in P + A would cause
2668 undefined behavior, we can assume that there
2670 || (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2671 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
2672 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))
2673 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@2)))))
2674 (minus (convert @1) (convert @2)))))
2676 (minus (convert (pointer_plus @@0 @1))
2677 (convert (pointer_plus @0 @2)))
2678 (if (INTEGRAL_TYPE_P (type)
2679 && TYPE_OVERFLOW_UNDEFINED (type)
2680 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
2681 (with { tree utype = unsigned_type_for (type); }
2682 (convert (minus (convert:utype @1) (convert:utype @2))))
2683 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
2684 /* For pointer types, if the conversion of A to the
2685 final type requires a sign- or zero-extension,
2686 then we have to punt - it is not defined which
2688 || (POINTER_TYPE_P (TREE_TYPE (@0))
2689 && TREE_CODE (@1) == INTEGER_CST
2690 && tree_int_cst_sign_bit (@1) == 0
2691 && TREE_CODE (@2) == INTEGER_CST
2692 && tree_int_cst_sign_bit (@2) == 0))
2693 (minus (convert @1) (convert @2)))))
2695 (pointer_diff (pointer_plus @0 @2) (pointer_plus @1 @2))
2696 (pointer_diff @0 @1))
2698 (pointer_diff (pointer_plus @@0 @1) (pointer_plus @0 @2))
2699 /* The second argument of pointer_plus must be interpreted as signed, and
2700 thus sign-extended if necessary. */
2701 (with { tree stype = signed_type_for (TREE_TYPE (@1)); }
2702 /* Use view_convert instead of convert here, as POINTER_PLUS_EXPR
2703 second arg is unsigned even when we need to consider it as signed,
2704 we don't want to diagnose overflow here. */
2705 (minus (convert (view_convert:stype @1))
2706 (convert (view_convert:stype @2)))))))
2708 /* (A * C) +- (B * C) -> (A+-B) * C and (A * C) +- A -> A * (C+-1).
2709 Modeled after fold_plusminus_mult_expr. */
2710 (if (!TYPE_SATURATING (type)
2711 && (!FLOAT_TYPE_P (type) || flag_associative_math))
2712 (for plusminus (plus minus)
2714 (plusminus (mult:cs@3 @0 @1) (mult:cs@4 @0 @2))
2715 (if (!ANY_INTEGRAL_TYPE_P (type)
2716 || TYPE_OVERFLOW_WRAPS (type)
2717 || (INTEGRAL_TYPE_P (type)
2718 && tree_expr_nonzero_p (@0)
2719 && expr_not_equal_to (@0, wi::minus_one (TYPE_PRECISION (type)))))
2720 (if (single_use (@3) || single_use (@4))
2721 /* If @1 +- @2 is constant require a hard single-use on either
2722 original operand (but not on both). */
2723 (mult (plusminus @1 @2) @0)
2725 (mult! (plusminus @1 @2) @0)
2728 /* We cannot generate constant 1 for fract. */
2729 (if (!ALL_FRACT_MODE_P (TYPE_MODE (type)))
2731 (plusminus @0 (mult:c@3 @0 @2))
2732 (if ((!ANY_INTEGRAL_TYPE_P (type)
2733 || TYPE_OVERFLOW_WRAPS (type)
2734 /* For @0 + @0*@2 this transformation would introduce UB
2735 (where there was none before) for @0 in [-1,0] and @2 max.
2736 For @0 - @0*@2 this transformation would introduce UB
2737 for @0 0 and @2 in [min,min+1] or @0 -1 and @2 min+1. */
2738 || (INTEGRAL_TYPE_P (type)
2739 && ((tree_expr_nonzero_p (@0)
2740 && expr_not_equal_to (@0,
2741 wi::minus_one (TYPE_PRECISION (type))))
2742 || (plusminus == PLUS_EXPR
2743 ? expr_not_equal_to (@2,
2744 wi::max_value (TYPE_PRECISION (type), SIGNED))
2745 /* Let's ignore the @0 -1 and @2 min case. */
2746 : (expr_not_equal_to (@2,
2747 wi::min_value (TYPE_PRECISION (type), SIGNED))
2748 && expr_not_equal_to (@2,
2749 wi::min_value (TYPE_PRECISION (type), SIGNED)
2752 (mult (plusminus { build_one_cst (type); } @2) @0)))
2754 (plusminus (mult:c@3 @0 @2) @0)
2755 (if ((!ANY_INTEGRAL_TYPE_P (type)
2756 || TYPE_OVERFLOW_WRAPS (type)
2757 /* For @0*@2 + @0 this transformation would introduce UB
2758 (where there was none before) for @0 in [-1,0] and @2 max.
2759 For @0*@2 - @0 this transformation would introduce UB
2760 for @0 0 and @2 min. */
2761 || (INTEGRAL_TYPE_P (type)
2762 && ((tree_expr_nonzero_p (@0)
2763 && (plusminus == MINUS_EXPR
2764 || expr_not_equal_to (@0,
2765 wi::minus_one (TYPE_PRECISION (type)))))
2766 || expr_not_equal_to (@2,
2767 (plusminus == PLUS_EXPR
2768 ? wi::max_value (TYPE_PRECISION (type), SIGNED)
2769 : wi::min_value (TYPE_PRECISION (type), SIGNED))))))
2771 (mult (plusminus @2 { build_one_cst (type); }) @0))))))
2774 /* Canonicalize X + (X << C) into X * (1 + (1 << C)) and
2775 (X << C1) + (X << C2) into X * ((1 << C1) + (1 << C2)). */
2777 (plus:c @0 (lshift:s @0 INTEGER_CST@1))
2778 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2779 && tree_fits_uhwi_p (@1)
2780 && tree_to_uhwi (@1) < element_precision (type))
2781 (with { tree t = type;
2782 if (!TYPE_OVERFLOW_WRAPS (t)) t = unsigned_type_for (t);
2783 wide_int w = wi::set_bit_in_zero (tree_to_uhwi (@1),
2784 element_precision (type));
2786 tree cst = wide_int_to_tree (VECTOR_TYPE_P (t) ? TREE_TYPE (t)
2788 cst = build_uniform_cst (t, cst); }
2789 (convert (mult (convert:t @0) { cst; })))))
2791 (plus (lshift:s @0 INTEGER_CST@1) (lshift:s @0 INTEGER_CST@2))
2792 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2793 && tree_fits_uhwi_p (@1)
2794 && tree_to_uhwi (@1) < element_precision (type)
2795 && tree_fits_uhwi_p (@2)
2796 && tree_to_uhwi (@2) < element_precision (type))
2797 (with { tree t = type;
2798 if (!TYPE_OVERFLOW_WRAPS (t)) t = unsigned_type_for (t);
2799 unsigned int prec = element_precision (type);
2800 wide_int w = wi::set_bit_in_zero (tree_to_uhwi (@1), prec);
2801 w += wi::set_bit_in_zero (tree_to_uhwi (@2), prec);
2802 tree cst = wide_int_to_tree (VECTOR_TYPE_P (t) ? TREE_TYPE (t)
2804 cst = build_uniform_cst (t, cst); }
2805 (convert (mult (convert:t @0) { cst; })))))
2808 /* Simplifications of MIN_EXPR, MAX_EXPR, fmin() and fmax(). */
2810 (for minmax (min max FMIN_ALL FMAX_ALL)
2814 /* min(max(x,y),y) -> y. */
2816 (min:c (max:c @0 @1) @1)
2818 /* max(min(x,y),y) -> y. */
2820 (max:c (min:c @0 @1) @1)
2822 /* max(a,-a) -> abs(a). */
2824 (max:c @0 (negate @0))
2825 (if (TREE_CODE (type) != COMPLEX_TYPE
2826 && (! ANY_INTEGRAL_TYPE_P (type)
2827 || TYPE_OVERFLOW_UNDEFINED (type)))
2829 /* min(a,-a) -> -abs(a). */
2831 (min:c @0 (negate @0))
2832 (if (TREE_CODE (type) != COMPLEX_TYPE
2833 && (! ANY_INTEGRAL_TYPE_P (type)
2834 || TYPE_OVERFLOW_UNDEFINED (type)))
2839 (if (INTEGRAL_TYPE_P (type)
2840 && TYPE_MIN_VALUE (type)
2841 && operand_equal_p (@1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
2843 (if (INTEGRAL_TYPE_P (type)
2844 && TYPE_MAX_VALUE (type)
2845 && operand_equal_p (@1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
2850 (if (INTEGRAL_TYPE_P (type)
2851 && TYPE_MAX_VALUE (type)
2852 && operand_equal_p (@1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
2854 (if (INTEGRAL_TYPE_P (type)
2855 && TYPE_MIN_VALUE (type)
2856 && operand_equal_p (@1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
2859 /* max (a, a + CST) -> a + CST where CST is positive. */
2860 /* max (a, a + CST) -> a where CST is negative. */
2862 (max:c @0 (plus@2 @0 INTEGER_CST@1))
2863 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
2864 (if (tree_int_cst_sgn (@1) > 0)
2868 /* min (a, a + CST) -> a where CST is positive. */
2869 /* min (a, a + CST) -> a + CST where CST is negative. */
2871 (min:c @0 (plus@2 @0 INTEGER_CST@1))
2872 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
2873 (if (tree_int_cst_sgn (@1) > 0)
2877 /* (convert (minmax ((convert (x) c)))) -> minmax (x c) if x is promoted
2878 and the outer convert demotes the expression back to x's type. */
2879 (for minmax (min max)
2881 (convert (minmax@0 (convert @1) INTEGER_CST@2))
2882 (if (INTEGRAL_TYPE_P (type)
2883 && types_match (@1, type) && int_fits_type_p (@2, type)
2884 && TYPE_SIGN (TREE_TYPE (@0)) == TYPE_SIGN (type)
2885 && TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type))
2886 (minmax @1 (convert @2)))))
2888 (for minmax (FMIN_ALL FMAX_ALL)
2889 /* If either argument is NaN, return the other one. Avoid the
2890 transformation if we get (and honor) a signalling NaN. */
2892 (minmax:c @0 REAL_CST@1)
2893 (if (real_isnan (TREE_REAL_CST_PTR (@1))
2894 && (!HONOR_SNANS (@1) || !TREE_REAL_CST (@1).signalling))
2896 /* Convert fmin/fmax to MIN_EXPR/MAX_EXPR. C99 requires these
2897 functions to return the numeric arg if the other one is NaN.
2898 MIN and MAX don't honor that, so only transform if -ffinite-math-only
2899 is set. C99 doesn't require -0.0 to be handled, so we don't have to
2900 worry about it either. */
2901 (if (flag_finite_math_only)
2908 /* min (-A, -B) -> -max (A, B) */
2909 (for minmax (min max FMIN_ALL FMAX_ALL)
2910 maxmin (max min FMAX_ALL FMIN_ALL)
2912 (minmax (negate:s@2 @0) (negate:s@3 @1))
2913 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
2914 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2915 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
2916 (negate (maxmin @0 @1)))))
2917 /* MIN (~X, ~Y) -> ~MAX (X, Y)
2918 MAX (~X, ~Y) -> ~MIN (X, Y) */
2919 (for minmax (min max)
2922 (minmax (bit_not:s@2 @0) (bit_not:s@3 @1))
2923 (bit_not (maxmin @0 @1))))
2925 /* MIN (X, Y) == X -> X <= Y */
2926 (for minmax (min min max max)
2930 (cmp:c (minmax:c @0 @1) @0)
2931 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0)))
2933 /* MIN (X, 5) == 0 -> X == 0
2934 MIN (X, 5) == 7 -> false */
2937 (cmp (min @0 INTEGER_CST@1) INTEGER_CST@2)
2938 (if (wi::lt_p (wi::to_wide (@1), wi::to_wide (@2),
2939 TYPE_SIGN (TREE_TYPE (@0))))
2940 { constant_boolean_node (cmp == NE_EXPR, type); }
2941 (if (wi::gt_p (wi::to_wide (@1), wi::to_wide (@2),
2942 TYPE_SIGN (TREE_TYPE (@0))))
2946 (cmp (max @0 INTEGER_CST@1) INTEGER_CST@2)
2947 (if (wi::gt_p (wi::to_wide (@1), wi::to_wide (@2),
2948 TYPE_SIGN (TREE_TYPE (@0))))
2949 { constant_boolean_node (cmp == NE_EXPR, type); }
2950 (if (wi::lt_p (wi::to_wide (@1), wi::to_wide (@2),
2951 TYPE_SIGN (TREE_TYPE (@0))))
2953 /* MIN (X, C1) < C2 -> X < C2 || C1 < C2 */
2954 (for minmax (min min max max min min max max )
2955 cmp (lt le gt ge gt ge lt le )
2956 comb (bit_ior bit_ior bit_ior bit_ior bit_and bit_and bit_and bit_and)
2958 (cmp (minmax @0 INTEGER_CST@1) INTEGER_CST@2)
2959 (comb (cmp @0 @2) (cmp @1 @2))))
2961 /* X <= MAX(X, Y) -> true
2962 X > MAX(X, Y) -> false
2963 X >= MIN(X, Y) -> true
2964 X < MIN(X, Y) -> false */
2965 (for minmax (min min max max )
2968 (cmp @0 (minmax:c @0 @1))
2969 { constant_boolean_node (cmp == GE_EXPR || cmp == LE_EXPR, type); } ))
2971 /* Undo fancy way of writing max/min or other ?: expressions,
2972 like a - ((a - b) & -(a < b)), in this case into (a < b) ? b : a.
2973 People normally use ?: and that is what we actually try to optimize. */
2974 (for cmp (simple_comparison)
2976 (minus @0 (bit_and:c (minus @0 @1)
2977 (convert? (negate@4 (convert? (cmp@5 @2 @3))))))
2978 (if (INTEGRAL_TYPE_P (type)
2979 && INTEGRAL_TYPE_P (TREE_TYPE (@4))
2980 && TREE_CODE (TREE_TYPE (@4)) != BOOLEAN_TYPE
2981 && INTEGRAL_TYPE_P (TREE_TYPE (@5))
2982 && (TYPE_PRECISION (TREE_TYPE (@4)) >= TYPE_PRECISION (type)
2983 || !TYPE_UNSIGNED (TREE_TYPE (@4)))
2984 && (GIMPLE || !TREE_SIDE_EFFECTS (@1)))
2985 (cond (cmp @2 @3) @1 @0)))
2987 (plus:c @0 (bit_and:c (minus @1 @0)
2988 (convert? (negate@4 (convert? (cmp@5 @2 @3))))))
2989 (if (INTEGRAL_TYPE_P (type)
2990 && INTEGRAL_TYPE_P (TREE_TYPE (@4))
2991 && TREE_CODE (TREE_TYPE (@4)) != BOOLEAN_TYPE
2992 && INTEGRAL_TYPE_P (TREE_TYPE (@5))
2993 && (TYPE_PRECISION (TREE_TYPE (@4)) >= TYPE_PRECISION (type)
2994 || !TYPE_UNSIGNED (TREE_TYPE (@4)))
2995 && (GIMPLE || !TREE_SIDE_EFFECTS (@1)))
2996 (cond (cmp @2 @3) @1 @0)))
2997 /* Similarly with ^ instead of - though in that case with :c. */
2999 (bit_xor:c @0 (bit_and:c (bit_xor:c @0 @1)
3000 (convert? (negate@4 (convert? (cmp@5 @2 @3))))))
3001 (if (INTEGRAL_TYPE_P (type)
3002 && INTEGRAL_TYPE_P (TREE_TYPE (@4))
3003 && TREE_CODE (TREE_TYPE (@4)) != BOOLEAN_TYPE
3004 && INTEGRAL_TYPE_P (TREE_TYPE (@5))
3005 && (TYPE_PRECISION (TREE_TYPE (@4)) >= TYPE_PRECISION (type)
3006 || !TYPE_UNSIGNED (TREE_TYPE (@4)))
3007 && (GIMPLE || !TREE_SIDE_EFFECTS (@1)))
3008 (cond (cmp @2 @3) @1 @0))))
3010 /* Simplifications of shift and rotates. */
3012 (for rotate (lrotate rrotate)
3014 (rotate integer_all_onesp@0 @1)
3017 /* Optimize -1 >> x for arithmetic right shifts. */
3019 (rshift integer_all_onesp@0 @1)
3020 (if (!TYPE_UNSIGNED (type))
3023 /* Optimize (x >> c) << c into x & (-1<<c). */
3025 (lshift (nop_convert? (rshift @0 INTEGER_CST@1)) @1)
3026 (if (wi::ltu_p (wi::to_wide (@1), element_precision (type)))
3027 /* It doesn't matter if the right shift is arithmetic or logical. */
3028 (bit_and (view_convert @0) (lshift { build_minus_one_cst (type); } @1))))
3031 (lshift (convert (convert@2 (rshift @0 INTEGER_CST@1))) @1)
3032 (if (wi::ltu_p (wi::to_wide (@1), element_precision (type))
3033 /* Allow intermediate conversion to integral type with whatever sign, as
3034 long as the low TYPE_PRECISION (type)
3035 - TYPE_PRECISION (TREE_TYPE (@2)) bits are preserved. */
3036 && INTEGRAL_TYPE_P (type)
3037 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
3038 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
3039 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (@0))
3040 && (TYPE_PRECISION (TREE_TYPE (@2)) >= TYPE_PRECISION (type)
3041 || wi::geu_p (wi::to_wide (@1),
3042 TYPE_PRECISION (type)
3043 - TYPE_PRECISION (TREE_TYPE (@2)))))
3044 (bit_and (convert @0) (lshift { build_minus_one_cst (type); } @1))))
3046 /* Optimize (x << c) >> c into x & ((unsigned)-1 >> c) for unsigned
3049 (rshift (lshift @0 INTEGER_CST@1) @1)
3050 (if (TYPE_UNSIGNED (type)
3051 && (wi::ltu_p (wi::to_wide (@1), element_precision (type))))
3052 (bit_and @0 (rshift { build_minus_one_cst (type); } @1))))
3054 /* Optimize x >> x into 0 */
3057 { build_zero_cst (type); })
3059 (for shiftrotate (lrotate rrotate lshift rshift)
3061 (shiftrotate @0 integer_zerop)
3064 (shiftrotate integer_zerop@0 @1)
3066 /* Prefer vector1 << scalar to vector1 << vector2
3067 if vector2 is uniform. */
3068 (for vec (VECTOR_CST CONSTRUCTOR)
3070 (shiftrotate @0 vec@1)
3071 (with { tree tem = uniform_vector_p (@1); }
3073 (shiftrotate @0 { tem; }))))))
3075 /* Simplify X << Y where Y's low width bits are 0 to X, as only valid
3076 Y is 0. Similarly for X >> Y. */
3078 (for shift (lshift rshift)
3080 (shift @0 SSA_NAME@1)
3081 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1)))
3083 int width = ceil_log2 (element_precision (TREE_TYPE (@0)));
3084 int prec = TYPE_PRECISION (TREE_TYPE (@1));
3086 (if ((get_nonzero_bits (@1) & wi::mask (width, false, prec)) == 0)
3090 /* Rewrite an LROTATE_EXPR by a constant into an
3091 RROTATE_EXPR by a new constant. */
3093 (lrotate @0 INTEGER_CST@1)
3094 (rrotate @0 { const_binop (MINUS_EXPR, TREE_TYPE (@1),
3095 build_int_cst (TREE_TYPE (@1),
3096 element_precision (type)), @1); }))
3098 /* Turn (a OP c1) OP c2 into a OP (c1+c2). */
3099 (for op (lrotate rrotate rshift lshift)
3101 (op (op @0 INTEGER_CST@1) INTEGER_CST@2)
3102 (with { unsigned int prec = element_precision (type); }
3103 (if (wi::ge_p (wi::to_wide (@1), 0, TYPE_SIGN (TREE_TYPE (@1)))
3104 && wi::lt_p (wi::to_wide (@1), prec, TYPE_SIGN (TREE_TYPE (@1)))
3105 && wi::ge_p (wi::to_wide (@2), 0, TYPE_SIGN (TREE_TYPE (@2)))
3106 && wi::lt_p (wi::to_wide (@2), prec, TYPE_SIGN (TREE_TYPE (@2))))
3107 (with { unsigned int low = (tree_to_uhwi (@1)
3108 + tree_to_uhwi (@2)); }
3109 /* Deal with a OP (c1 + c2) being undefined but (a OP c1) OP c2
3110 being well defined. */
3112 (if (op == LROTATE_EXPR || op == RROTATE_EXPR)
3113 (op @0 { build_int_cst (TREE_TYPE (@1), low % prec); })
3114 (if (TYPE_UNSIGNED (type) || op == LSHIFT_EXPR)
3115 { build_zero_cst (type); }
3116 (op @0 { build_int_cst (TREE_TYPE (@1), prec - 1); })))
3117 (op @0 { build_int_cst (TREE_TYPE (@1), low); })))))))
3120 /* ((1 << A) & 1) != 0 -> A == 0
3121 ((1 << A) & 1) == 0 -> A != 0 */
3125 (cmp (bit_and (lshift integer_onep @0) integer_onep) integer_zerop)
3126 (icmp @0 { build_zero_cst (TREE_TYPE (@0)); })))
3128 /* (CST1 << A) == CST2 -> A == ctz (CST2) - ctz (CST1)
3129 (CST1 << A) != CST2 -> A != ctz (CST2) - ctz (CST1)
3133 (cmp (lshift INTEGER_CST@0 @1) INTEGER_CST@2)
3134 (with { int cand = wi::ctz (wi::to_wide (@2)) - wi::ctz (wi::to_wide (@0)); }
3136 || (!integer_zerop (@2)
3137 && wi::lshift (wi::to_wide (@0), cand) != wi::to_wide (@2)))
3138 { constant_boolean_node (cmp == NE_EXPR, type); }
3139 (if (!integer_zerop (@2)
3140 && wi::lshift (wi::to_wide (@0), cand) == wi::to_wide (@2))
3141 (cmp @1 { build_int_cst (TREE_TYPE (@1), cand); }))))))
3143 /* Fold (X << C1) & C2 into (X << C1) & (C2 | ((1 << C1) - 1))
3144 (X >> C1) & C2 into (X >> C1) & (C2 | ~((type) -1 >> C1))
3145 if the new mask might be further optimized. */
3146 (for shift (lshift rshift)
3148 (bit_and (convert?:s@4 (shift:s@5 (convert1?@3 @0) INTEGER_CST@1))
3150 (if (tree_nop_conversion_p (TREE_TYPE (@4), TREE_TYPE (@5))
3151 && TYPE_PRECISION (type) <= HOST_BITS_PER_WIDE_INT
3152 && tree_fits_uhwi_p (@1)
3153 && tree_to_uhwi (@1) > 0
3154 && tree_to_uhwi (@1) < TYPE_PRECISION (type))
3157 unsigned int shiftc = tree_to_uhwi (@1);
3158 unsigned HOST_WIDE_INT mask = TREE_INT_CST_LOW (@2);
3159 unsigned HOST_WIDE_INT newmask, zerobits = 0;
3160 tree shift_type = TREE_TYPE (@3);
3163 if (shift == LSHIFT_EXPR)
3164 zerobits = ((HOST_WIDE_INT_1U << shiftc) - 1);
3165 else if (shift == RSHIFT_EXPR
3166 && type_has_mode_precision_p (shift_type))
3168 prec = TYPE_PRECISION (TREE_TYPE (@3));
3170 /* See if more bits can be proven as zero because of
3173 && TYPE_UNSIGNED (TREE_TYPE (@0)))
3175 tree inner_type = TREE_TYPE (@0);
3176 if (type_has_mode_precision_p (inner_type)
3177 && TYPE_PRECISION (inner_type) < prec)
3179 prec = TYPE_PRECISION (inner_type);
3180 /* See if we can shorten the right shift. */
3182 shift_type = inner_type;
3183 /* Otherwise X >> C1 is all zeros, so we'll optimize
3184 it into (X, 0) later on by making sure zerobits
3188 zerobits = HOST_WIDE_INT_M1U;
3191 zerobits >>= HOST_BITS_PER_WIDE_INT - shiftc;
3192 zerobits <<= prec - shiftc;
3194 /* For arithmetic shift if sign bit could be set, zerobits
3195 can contain actually sign bits, so no transformation is
3196 possible, unless MASK masks them all away. In that
3197 case the shift needs to be converted into logical shift. */
3198 if (!TYPE_UNSIGNED (TREE_TYPE (@3))
3199 && prec == TYPE_PRECISION (TREE_TYPE (@3)))
3201 if ((mask & zerobits) == 0)
3202 shift_type = unsigned_type_for (TREE_TYPE (@3));
3208 /* ((X << 16) & 0xff00) is (X, 0). */
3209 (if ((mask & zerobits) == mask)
3210 { build_int_cst (type, 0); }
3211 (with { newmask = mask | zerobits; }
3212 (if (newmask != mask && (newmask & (newmask + 1)) == 0)
3215 /* Only do the transformation if NEWMASK is some integer
3217 for (prec = BITS_PER_UNIT;
3218 prec < HOST_BITS_PER_WIDE_INT; prec <<= 1)
3219 if (newmask == (HOST_WIDE_INT_1U << prec) - 1)
3222 (if (prec < HOST_BITS_PER_WIDE_INT
3223 || newmask == HOST_WIDE_INT_M1U)
3225 { tree newmaskt = build_int_cst_type (TREE_TYPE (@2), newmask); }
3226 (if (!tree_int_cst_equal (newmaskt, @2))
3227 (if (shift_type != TREE_TYPE (@3))
3228 (bit_and (convert (shift:shift_type (convert @3) @1)) { newmaskt; })
3229 (bit_and @4 { newmaskt; })))))))))))))
3231 /* Fold (X {&,^,|} C2) << C1 into (X << C1) {&,^,|} (C2 << C1)
3232 (X {&,^,|} C2) >> C1 into (X >> C1) & (C2 >> C1). */
3233 (for shift (lshift rshift)
3234 (for bit_op (bit_and bit_xor bit_ior)
3236 (shift (convert?:s (bit_op:s @0 INTEGER_CST@2)) INTEGER_CST@1)
3237 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
3238 (with { tree mask = int_const_binop (shift, fold_convert (type, @2), @1); }
3240 (bit_op (shift (convert @0) @1) { mask; })))))))
3242 /* ~(~X >> Y) -> X >> Y (for arithmetic shift). */
3244 (bit_not (convert1?:s (rshift:s (convert2?@0 (bit_not @1)) @2)))
3245 (if (!TYPE_UNSIGNED (TREE_TYPE (@0))
3246 && (element_precision (TREE_TYPE (@0))
3247 <= element_precision (TREE_TYPE (@1))
3248 || !TYPE_UNSIGNED (TREE_TYPE (@1))))
3250 { tree shift_type = TREE_TYPE (@0); }
3251 (convert (rshift (convert:shift_type @1) @2)))))
3253 /* ~(~X >>r Y) -> X >>r Y
3254 ~(~X <<r Y) -> X <<r Y */
3255 (for rotate (lrotate rrotate)
3257 (bit_not (convert1?:s (rotate:s (convert2?@0 (bit_not @1)) @2)))
3258 (if ((element_precision (TREE_TYPE (@0))
3259 <= element_precision (TREE_TYPE (@1))
3260 || !TYPE_UNSIGNED (TREE_TYPE (@1)))
3261 && (element_precision (type) <= element_precision (TREE_TYPE (@0))
3262 || !TYPE_UNSIGNED (TREE_TYPE (@0))))
3264 { tree rotate_type = TREE_TYPE (@0); }
3265 (convert (rotate (convert:rotate_type @1) @2))))))
3267 /* Simplifications of conversions. */
3269 /* Basic strip-useless-type-conversions / strip_nops. */
3270 (for cvt (convert view_convert float fix_trunc)
3273 (if ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@0)))
3274 || (GENERIC && type == TREE_TYPE (@0)))
3277 /* Contract view-conversions. */
3279 (view_convert (view_convert @0))
3282 /* For integral conversions with the same precision or pointer
3283 conversions use a NOP_EXPR instead. */
3286 (if ((INTEGRAL_TYPE_P (type) || POINTER_TYPE_P (type))
3287 && (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
3288 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (@0)))
3291 /* Strip inner integral conversions that do not change precision or size, or
3292 zero-extend while keeping the same size (for bool-to-char). */
3294 (view_convert (convert@0 @1))
3295 (if ((INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
3296 && (INTEGRAL_TYPE_P (TREE_TYPE (@1)) || POINTER_TYPE_P (TREE_TYPE (@1)))
3297 && TYPE_SIZE (TREE_TYPE (@0)) == TYPE_SIZE (TREE_TYPE (@1))
3298 && (TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1))
3299 || (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (TREE_TYPE (@1))
3300 && TYPE_UNSIGNED (TREE_TYPE (@1)))))
3303 /* Simplify a view-converted empty constructor. */
3305 (view_convert CONSTRUCTOR@0)
3306 (if (TREE_CODE (@0) != SSA_NAME
3307 && CONSTRUCTOR_NELTS (@0) == 0)
3308 { build_zero_cst (type); }))
3310 /* Re-association barriers around constants and other re-association
3311 barriers can be removed. */
3313 (paren CONSTANT_CLASS_P@0)
3316 (paren (paren@1 @0))
3319 /* Handle cases of two conversions in a row. */
3320 (for ocvt (convert float fix_trunc)
3321 (for icvt (convert float)
3326 tree inside_type = TREE_TYPE (@0);
3327 tree inter_type = TREE_TYPE (@1);
3328 int inside_int = INTEGRAL_TYPE_P (inside_type);
3329 int inside_ptr = POINTER_TYPE_P (inside_type);
3330 int inside_float = FLOAT_TYPE_P (inside_type);
3331 int inside_vec = VECTOR_TYPE_P (inside_type);
3332 unsigned int inside_prec = TYPE_PRECISION (inside_type);
3333 int inside_unsignedp = TYPE_UNSIGNED (inside_type);
3334 int inter_int = INTEGRAL_TYPE_P (inter_type);
3335 int inter_ptr = POINTER_TYPE_P (inter_type);
3336 int inter_float = FLOAT_TYPE_P (inter_type);
3337 int inter_vec = VECTOR_TYPE_P (inter_type);
3338 unsigned int inter_prec = TYPE_PRECISION (inter_type);
3339 int inter_unsignedp = TYPE_UNSIGNED (inter_type);
3340 int final_int = INTEGRAL_TYPE_P (type);
3341 int final_ptr = POINTER_TYPE_P (type);
3342 int final_float = FLOAT_TYPE_P (type);
3343 int final_vec = VECTOR_TYPE_P (type);
3344 unsigned int final_prec = TYPE_PRECISION (type);
3345 int final_unsignedp = TYPE_UNSIGNED (type);
3348 /* In addition to the cases of two conversions in a row
3349 handled below, if we are converting something to its own
3350 type via an object of identical or wider precision, neither
3351 conversion is needed. */
3352 (if (((GIMPLE && useless_type_conversion_p (type, inside_type))
3354 && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (inside_type)))
3355 && (((inter_int || inter_ptr) && final_int)
3356 || (inter_float && final_float))
3357 && inter_prec >= final_prec)
3360 /* Likewise, if the intermediate and initial types are either both
3361 float or both integer, we don't need the middle conversion if the
3362 former is wider than the latter and doesn't change the signedness
3363 (for integers). Avoid this if the final type is a pointer since
3364 then we sometimes need the middle conversion. */
3365 (if (((inter_int && inside_int) || (inter_float && inside_float))
3366 && (final_int || final_float)
3367 && inter_prec >= inside_prec
3368 && (inter_float || inter_unsignedp == inside_unsignedp))
3371 /* If we have a sign-extension of a zero-extended value, we can
3372 replace that by a single zero-extension. Likewise if the
3373 final conversion does not change precision we can drop the
3374 intermediate conversion. */
3375 (if (inside_int && inter_int && final_int
3376 && ((inside_prec < inter_prec && inter_prec < final_prec
3377 && inside_unsignedp && !inter_unsignedp)
3378 || final_prec == inter_prec))
3381 /* Two conversions in a row are not needed unless:
3382 - some conversion is floating-point (overstrict for now), or
3383 - some conversion is a vector (overstrict for now), or
3384 - the intermediate type is narrower than both initial and
3386 - the intermediate type and innermost type differ in signedness,
3387 and the outermost type is wider than the intermediate, or
3388 - the initial type is a pointer type and the precisions of the
3389 intermediate and final types differ, or
3390 - the final type is a pointer type and the precisions of the
3391 initial and intermediate types differ. */
3392 (if (! inside_float && ! inter_float && ! final_float
3393 && ! inside_vec && ! inter_vec && ! final_vec
3394 && (inter_prec >= inside_prec || inter_prec >= final_prec)
3395 && ! (inside_int && inter_int
3396 && inter_unsignedp != inside_unsignedp
3397 && inter_prec < final_prec)
3398 && ((inter_unsignedp && inter_prec > inside_prec)
3399 == (final_unsignedp && final_prec > inter_prec))
3400 && ! (inside_ptr && inter_prec != final_prec)
3401 && ! (final_ptr && inside_prec != inter_prec))
3404 /* A truncation to an unsigned type (a zero-extension) should be
3405 canonicalized as bitwise and of a mask. */
3406 (if (GIMPLE /* PR70366: doing this in GENERIC breaks -Wconversion. */
3407 && final_int && inter_int && inside_int
3408 && final_prec == inside_prec
3409 && final_prec > inter_prec
3411 (convert (bit_and @0 { wide_int_to_tree
3413 wi::mask (inter_prec, false,
3414 TYPE_PRECISION (inside_type))); })))
3416 /* If we are converting an integer to a floating-point that can
3417 represent it exactly and back to an integer, we can skip the
3418 floating-point conversion. */
3419 (if (GIMPLE /* PR66211 */
3420 && inside_int && inter_float && final_int &&
3421 (unsigned) significand_size (TYPE_MODE (inter_type))
3422 >= inside_prec - !inside_unsignedp)
3425 /* If we have a narrowing conversion to an integral type that is fed by a
3426 BIT_AND_EXPR, we might be able to remove the BIT_AND_EXPR if it merely
3427 masks off bits outside the final type (and nothing else). */
3429 (convert (bit_and @0 INTEGER_CST@1))
3430 (if (INTEGRAL_TYPE_P (type)
3431 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
3432 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))
3433 && operand_equal_p (@1, build_low_bits_mask (TREE_TYPE (@1),
3434 TYPE_PRECISION (type)), 0))
3438 /* (X /[ex] A) * A -> X. */
3440 (mult (convert1? (exact_div @0 @@1)) (convert2? @1))
3443 /* Simplify (A / B) * B + (A % B) -> A. */
3444 (for div (trunc_div ceil_div floor_div round_div)
3445 mod (trunc_mod ceil_mod floor_mod round_mod)
3447 (plus:c (mult:c (div @0 @1) @1) (mod @0 @1))
3450 /* ((X /[ex] A) +- B) * A --> X +- A * B. */
3451 (for op (plus minus)
3453 (mult (convert1? (op (convert2? (exact_div @0 INTEGER_CST@@1)) INTEGER_CST@2)) @1)
3454 (if (tree_nop_conversion_p (type, TREE_TYPE (@2))
3455 && tree_nop_conversion_p (TREE_TYPE (@0), TREE_TYPE (@2)))
3458 wi::overflow_type overflow;
3459 wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@2),
3460 TYPE_SIGN (type), &overflow);
3462 (if (types_match (type, TREE_TYPE (@2))
3463 && types_match (TREE_TYPE (@0), TREE_TYPE (@2)) && !overflow)
3464 (op @0 { wide_int_to_tree (type, mul); })
3465 (with { tree utype = unsigned_type_for (type); }
3466 (convert (op (convert:utype @0)
3467 (mult (convert:utype @1) (convert:utype @2))))))))))
3469 /* Canonicalization of binary operations. */
3471 /* Convert X + -C into X - C. */
3473 (plus @0 REAL_CST@1)
3474 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
3475 (with { tree tem = const_unop (NEGATE_EXPR, type, @1); }
3476 (if (!TREE_OVERFLOW (tem) || !flag_trapping_math)
3477 (minus @0 { tem; })))))
3479 /* Convert x+x into x*2. */
3482 (if (SCALAR_FLOAT_TYPE_P (type))
3483 (mult @0 { build_real (type, dconst2); })
3484 (if (INTEGRAL_TYPE_P (type))
3485 (mult @0 { build_int_cst (type, 2); }))))
3489 (minus integer_zerop @1)
3492 (pointer_diff integer_zerop @1)
3493 (negate (convert @1)))
3495 /* (ARG0 - ARG1) is the same as (-ARG1 + ARG0). So check whether
3496 ARG0 is zero and X + ARG0 reduces to X, since that would mean
3497 (-ARG1 + ARG0) reduces to -ARG1. */
3499 (minus real_zerop@0 @1)
3500 (if (fold_real_zero_addition_p (type, @0, 0))
3503 /* Transform x * -1 into -x. */
3505 (mult @0 integer_minus_onep)
3508 /* Reassociate (X * CST) * Y to (X * Y) * CST. This does not introduce
3509 signed overflow for CST != 0 && CST != -1. */
3511 (mult:c (mult:s@3 @0 INTEGER_CST@1) @2)
3512 (if (TREE_CODE (@2) != INTEGER_CST
3514 && !integer_zerop (@1) && !integer_minus_onep (@1))
3515 (mult (mult @0 @2) @1)))
3517 /* True if we can easily extract the real and imaginary parts of a complex
3519 (match compositional_complex
3520 (convert? (complex @0 @1)))
3522 /* COMPLEX_EXPR and REALPART/IMAGPART_EXPR cancellations. */
3524 (complex (realpart @0) (imagpart @0))
3527 (realpart (complex @0 @1))
3530 (imagpart (complex @0 @1))
3533 /* Sometimes we only care about half of a complex expression. */
3535 (realpart (convert?:s (conj:s @0)))
3536 (convert (realpart @0)))
3538 (imagpart (convert?:s (conj:s @0)))
3539 (convert (negate (imagpart @0))))
3540 (for part (realpart imagpart)
3541 (for op (plus minus)
3543 (part (convert?:s@2 (op:s @0 @1)))
3544 (convert (op (part @0) (part @1))))))
3546 (realpart (convert?:s (CEXPI:s @0)))
3549 (imagpart (convert?:s (CEXPI:s @0)))
3552 /* conj(conj(x)) -> x */
3554 (conj (convert? (conj @0)))
3555 (if (tree_nop_conversion_p (TREE_TYPE (@0), type))
3558 /* conj({x,y}) -> {x,-y} */
3560 (conj (convert?:s (complex:s @0 @1)))
3561 (with { tree itype = TREE_TYPE (type); }
3562 (complex (convert:itype @0) (negate (convert:itype @1)))))
3564 /* BSWAP simplifications, transforms checked by gcc.dg/builtin-bswap-8.c. */
3565 (for bswap (BUILT_IN_BSWAP16 BUILT_IN_BSWAP32 BUILT_IN_BSWAP64)
3570 (bswap (bit_not (bswap @0)))
3572 (for bitop (bit_xor bit_ior bit_and)
3574 (bswap (bitop:c (bswap @0) @1))
3575 (bitop @0 (bswap @1)))))
3578 /* Combine COND_EXPRs and VEC_COND_EXPRs. */
3580 /* Simplify constant conditions.
3581 Only optimize constant conditions when the selected branch
3582 has the same type as the COND_EXPR. This avoids optimizing
3583 away "c ? x : throw", where the throw has a void type.
3584 Note that we cannot throw away the fold-const.c variant nor
3585 this one as we depend on doing this transform before possibly
3586 A ? B : B -> B triggers and the fold-const.c one can optimize
3587 0 ? A : B to B even if A has side-effects. Something
3588 genmatch cannot handle. */
3590 (cond INTEGER_CST@0 @1 @2)
3591 (if (integer_zerop (@0))
3592 (if (!VOID_TYPE_P (TREE_TYPE (@2)) || VOID_TYPE_P (type))
3594 (if (!VOID_TYPE_P (TREE_TYPE (@1)) || VOID_TYPE_P (type))
3597 (vec_cond VECTOR_CST@0 @1 @2)
3598 (if (integer_all_onesp (@0))
3600 (if (integer_zerop (@0))
3604 /* Sink unary operations to branches, but only if we do fold both. */
3605 (for op (negate bit_not abs absu)
3607 (op (vec_cond:s @0 @1 @2))
3608 (vec_cond @0 (op! @1) (op! @2))))
3610 /* Sink binary operation to branches, but only if we can fold it. */
3611 (for op (tcc_comparison plus minus mult bit_and bit_ior bit_xor
3612 rdiv trunc_div ceil_div floor_div round_div
3613 trunc_mod ceil_mod floor_mod round_mod min max)
3614 /* (c ? a : b) op (c ? d : e) --> c ? (a op d) : (b op e) */
3616 (op (vec_cond:s @0 @1 @2) (vec_cond:s @0 @3 @4))
3617 (vec_cond @0 (op! @1 @3) (op! @2 @4)))
3619 /* (c ? a : b) op d --> c ? (a op d) : (b op d) */
3621 (op (vec_cond:s @0 @1 @2) @3)
3622 (vec_cond @0 (op! @1 @3) (op! @2 @3)))
3624 (op @3 (vec_cond:s @0 @1 @2))
3625 (vec_cond @0 (op! @3 @1) (op! @3 @2))))
3628 /* (v ? w : 0) ? a : b is just (v & w) ? a : b
3629 Currently disabled after pass lvec because ARM understands
3630 VEC_COND_EXPR<v==w,-1,0> but not a plain v==w fed to BIT_IOR_EXPR. */
3632 (vec_cond (vec_cond:s @0 @3 integer_zerop) @1 @2)
3633 (if (optimize_vectors_before_lowering_p () && types_match (@0, @3))
3634 (vec_cond (bit_and @0 @3) @1 @2)))
3636 (vec_cond (vec_cond:s @0 integer_all_onesp @3) @1 @2)
3637 (if (optimize_vectors_before_lowering_p () && types_match (@0, @3))
3638 (vec_cond (bit_ior @0 @3) @1 @2)))
3640 (vec_cond (vec_cond:s @0 integer_zerop @3) @1 @2)
3641 (if (optimize_vectors_before_lowering_p () && types_match (@0, @3))
3642 (vec_cond (bit_ior @0 (bit_not @3)) @2 @1)))
3644 (vec_cond (vec_cond:s @0 @3 integer_all_onesp) @1 @2)
3645 (if (optimize_vectors_before_lowering_p () && types_match (@0, @3))
3646 (vec_cond (bit_and @0 (bit_not @3)) @2 @1)))
3648 /* c1 ? c2 ? a : b : b --> (c1 & c2) ? a : b */
3650 (vec_cond @0 (vec_cond:s @1 @2 @3) @3)
3651 (if (optimize_vectors_before_lowering_p () && types_match (@0, @1))
3652 (vec_cond (bit_and @0 @1) @2 @3)))
3654 (vec_cond @0 @2 (vec_cond:s @1 @2 @3))
3655 (if (optimize_vectors_before_lowering_p () && types_match (@0, @1))
3656 (vec_cond (bit_ior @0 @1) @2 @3)))
3658 (vec_cond @0 (vec_cond:s @1 @2 @3) @2)
3659 (if (optimize_vectors_before_lowering_p () && types_match (@0, @1))
3660 (vec_cond (bit_ior (bit_not @0) @1) @2 @3)))
3662 (vec_cond @0 @3 (vec_cond:s @1 @2 @3))
3663 (if (optimize_vectors_before_lowering_p () && types_match (@0, @1))
3664 (vec_cond (bit_and (bit_not @0) @1) @2 @3)))
3666 /* Canonicalize mask ? { 0, ... } : { -1, ...} to ~mask if the mask
3667 types are compatible. */
3669 (vec_cond @0 VECTOR_CST@1 VECTOR_CST@2)
3670 (if (VECTOR_BOOLEAN_TYPE_P (type)
3671 && types_match (type, TREE_TYPE (@0)))
3672 (if (integer_zerop (@1) && integer_all_onesp (@2))
3674 (if (integer_all_onesp (@1) && integer_zerop (@2))
3677 /* Simplification moved from fold_cond_expr_with_comparison. It may also
3679 /* This pattern implements two kinds simplification:
3682 (cond (cmp (convert1? x) c1) (convert2? x) c2) -> (minmax (x c)) if:
3683 1) Conversions are type widening from smaller type.
3684 2) Const c1 equals to c2 after canonicalizing comparison.
3685 3) Comparison has tree code LT, LE, GT or GE.
3686 This specific pattern is needed when (cmp (convert x) c) may not
3687 be simplified by comparison patterns because of multiple uses of
3688 x. It also makes sense here because simplifying across multiple
3689 referred var is always benefitial for complicated cases.
3692 (cond (eq (convert1? x) c1) (convert2? x) c2) -> (cond (eq x c1) c1 c2). */
3693 (for cmp (lt le gt ge eq)
3695 (cond (cmp (convert1? @1) INTEGER_CST@3) (convert2? @1) INTEGER_CST@2)
3698 tree from_type = TREE_TYPE (@1);
3699 tree c1_type = TREE_TYPE (@3), c2_type = TREE_TYPE (@2);
3700 enum tree_code code = ERROR_MARK;
3702 if (INTEGRAL_TYPE_P (from_type)
3703 && int_fits_type_p (@2, from_type)
3704 && (types_match (c1_type, from_type)
3705 || (TYPE_PRECISION (c1_type) > TYPE_PRECISION (from_type)
3706 && (TYPE_UNSIGNED (from_type)
3707 || TYPE_SIGN (c1_type) == TYPE_SIGN (from_type))))
3708 && (types_match (c2_type, from_type)
3709 || (TYPE_PRECISION (c2_type) > TYPE_PRECISION (from_type)
3710 && (TYPE_UNSIGNED (from_type)
3711 || TYPE_SIGN (c2_type) == TYPE_SIGN (from_type)))))
3715 if (wi::to_widest (@3) == (wi::to_widest (@2) - 1))
3717 /* X <= Y - 1 equals to X < Y. */
3720 /* X > Y - 1 equals to X >= Y. */
3724 if (wi::to_widest (@3) == (wi::to_widest (@2) + 1))
3726 /* X < Y + 1 equals to X <= Y. */
3729 /* X >= Y + 1 equals to X > Y. */
3733 if (code != ERROR_MARK
3734 || wi::to_widest (@2) == wi::to_widest (@3))
3736 if (cmp == LT_EXPR || cmp == LE_EXPR)
3738 if (cmp == GT_EXPR || cmp == GE_EXPR)
3742 /* Can do A == C1 ? A : C2 -> A == C1 ? C1 : C2? */
3743 else if (int_fits_type_p (@3, from_type))
3747 (if (code == MAX_EXPR)
3748 (convert (max @1 (convert @2)))
3749 (if (code == MIN_EXPR)
3750 (convert (min @1 (convert @2)))
3751 (if (code == EQ_EXPR)
3752 (convert (cond (eq @1 (convert @3))
3753 (convert:from_type @3) (convert:from_type @2)))))))))
3755 /* (cond (cmp (convert? x) c1) (op x c2) c3) -> (op (minmax x c1) c2) if:
3757 1) OP is PLUS or MINUS.
3758 2) CMP is LT, LE, GT or GE.
3759 3) C3 == (C1 op C2), and computation doesn't have undefined behavior.
3761 This pattern also handles special cases like:
3763 A) Operand x is a unsigned to signed type conversion and c1 is
3764 integer zero. In this case,
3765 (signed type)x < 0 <=> x > MAX_VAL(signed type)
3766 (signed type)x >= 0 <=> x <= MAX_VAL(signed type)
3767 B) Const c1 may not equal to (C3 op' C2). In this case we also
3768 check equality for (c1+1) and (c1-1) by adjusting comparison
3771 TODO: Though signed type is handled by this pattern, it cannot be
3772 simplified at the moment because C standard requires additional
3773 type promotion. In order to match&simplify it here, the IR needs
3774 to be cleaned up by other optimizers, i.e, VRP. */
3775 (for op (plus minus)
3776 (for cmp (lt le gt ge)
3778 (cond (cmp (convert? @X) INTEGER_CST@1) (op @X INTEGER_CST@2) INTEGER_CST@3)
3779 (with { tree from_type = TREE_TYPE (@X), to_type = TREE_TYPE (@1); }
3780 (if (types_match (from_type, to_type)
3781 /* Check if it is special case A). */
3782 || (TYPE_UNSIGNED (from_type)
3783 && !TYPE_UNSIGNED (to_type)
3784 && TYPE_PRECISION (from_type) == TYPE_PRECISION (to_type)
3785 && integer_zerop (@1)
3786 && (cmp == LT_EXPR || cmp == GE_EXPR)))
3789 wi::overflow_type overflow = wi::OVF_NONE;
3790 enum tree_code code, cmp_code = cmp;
3792 wide_int c1 = wi::to_wide (@1);
3793 wide_int c2 = wi::to_wide (@2);
3794 wide_int c3 = wi::to_wide (@3);
3795 signop sgn = TYPE_SIGN (from_type);
3797 /* Handle special case A), given x of unsigned type:
3798 ((signed type)x < 0) <=> (x > MAX_VAL(signed type))
3799 ((signed type)x >= 0) <=> (x <= MAX_VAL(signed type)) */
3800 if (!types_match (from_type, to_type))
3802 if (cmp_code == LT_EXPR)
3804 if (cmp_code == GE_EXPR)
3806 c1 = wi::max_value (to_type);
3808 /* To simplify this pattern, we require c3 = (c1 op c2). Here we
3809 compute (c3 op' c2) and check if it equals to c1 with op' being
3810 the inverted operator of op. Make sure overflow doesn't happen
3811 if it is undefined. */
3812 if (op == PLUS_EXPR)
3813 real_c1 = wi::sub (c3, c2, sgn, &overflow);
3815 real_c1 = wi::add (c3, c2, sgn, &overflow);
3818 if (!overflow || !TYPE_OVERFLOW_UNDEFINED (from_type))
3820 /* Check if c1 equals to real_c1. Boundary condition is handled
3821 by adjusting comparison operation if necessary. */
3822 if (!wi::cmp (wi::sub (real_c1, 1, sgn, &overflow), c1, sgn)
3825 /* X <= Y - 1 equals to X < Y. */
3826 if (cmp_code == LE_EXPR)
3828 /* X > Y - 1 equals to X >= Y. */
3829 if (cmp_code == GT_EXPR)
3832 if (!wi::cmp (wi::add (real_c1, 1, sgn, &overflow), c1, sgn)
3835 /* X < Y + 1 equals to X <= Y. */
3836 if (cmp_code == LT_EXPR)
3838 /* X >= Y + 1 equals to X > Y. */
3839 if (cmp_code == GE_EXPR)
3842 if (code != cmp_code || !wi::cmp (real_c1, c1, sgn))
3844 if (cmp_code == LT_EXPR || cmp_code == LE_EXPR)
3846 if (cmp_code == GT_EXPR || cmp_code == GE_EXPR)
3851 (if (code == MAX_EXPR)
3852 (op (max @X { wide_int_to_tree (from_type, real_c1); })
3853 { wide_int_to_tree (from_type, c2); })
3854 (if (code == MIN_EXPR)
3855 (op (min @X { wide_int_to_tree (from_type, real_c1); })
3856 { wide_int_to_tree (from_type, c2); })))))))))
3858 (for cnd (cond vec_cond)
3859 /* A ? B : (A ? X : C) -> A ? B : C. */
3861 (cnd @0 (cnd @0 @1 @2) @3)
3864 (cnd @0 @1 (cnd @0 @2 @3))
3866 /* A ? B : (!A ? C : X) -> A ? B : C. */
3867 /* ??? This matches embedded conditions open-coded because genmatch
3868 would generate matching code for conditions in separate stmts only.
3869 The following is still important to merge then and else arm cases
3870 from if-conversion. */
3872 (cnd @0 @1 (cnd @2 @3 @4))
3873 (if (inverse_conditions_p (@0, @2))
3876 (cnd @0 (cnd @1 @2 @3) @4)
3877 (if (inverse_conditions_p (@0, @1))
3880 /* A ? B : B -> B. */
3885 /* !A ? B : C -> A ? C : B. */
3887 (cnd (logical_inverted_value truth_valued_p@0) @1 @2)
3890 /* -(type)!A -> (type)A - 1. */
3892 (negate (convert?:s (logical_inverted_value:s @0)))
3893 (if (INTEGRAL_TYPE_P (type)
3894 && TREE_CODE (type) != BOOLEAN_TYPE
3895 && TYPE_PRECISION (type) > 1
3896 && TREE_CODE (@0) == SSA_NAME
3897 && ssa_name_has_boolean_range (@0))
3898 (plus (convert:type @0) { build_all_ones_cst (type); })))
3900 /* A + (B vcmp C ? 1 : 0) -> A - (B vcmp C ? -1 : 0), since vector comparisons
3901 return all -1 or all 0 results. */
3902 /* ??? We could instead convert all instances of the vec_cond to negate,
3903 but that isn't necessarily a win on its own. */
3905 (plus:c @3 (view_convert? (vec_cond:s @0 integer_each_onep@1 integer_zerop@2)))
3906 (if (VECTOR_TYPE_P (type)
3907 && known_eq (TYPE_VECTOR_SUBPARTS (type),
3908 TYPE_VECTOR_SUBPARTS (TREE_TYPE (@1)))
3909 && (TYPE_MODE (TREE_TYPE (type))
3910 == TYPE_MODE (TREE_TYPE (TREE_TYPE (@1)))))
3911 (minus @3 (view_convert (vec_cond @0 (negate @1) @2)))))
3913 /* ... likewise A - (B vcmp C ? 1 : 0) -> A + (B vcmp C ? -1 : 0). */
3915 (minus @3 (view_convert? (vec_cond:s @0 integer_each_onep@1 integer_zerop@2)))
3916 (if (VECTOR_TYPE_P (type)
3917 && known_eq (TYPE_VECTOR_SUBPARTS (type),
3918 TYPE_VECTOR_SUBPARTS (TREE_TYPE (@1)))
3919 && (TYPE_MODE (TREE_TYPE (type))
3920 == TYPE_MODE (TREE_TYPE (TREE_TYPE (@1)))))
3921 (plus @3 (view_convert (vec_cond @0 (negate @1) @2)))))
3924 /* Simplifications of comparisons. */
3926 /* See if we can reduce the magnitude of a constant involved in a
3927 comparison by changing the comparison code. This is a canonicalization
3928 formerly done by maybe_canonicalize_comparison_1. */
3932 (cmp @0 uniform_integer_cst_p@1)
3933 (with { tree cst = uniform_integer_cst_p (@1); }
3934 (if (tree_int_cst_sgn (cst) == -1)
3935 (acmp @0 { build_uniform_cst (TREE_TYPE (@1),
3936 wide_int_to_tree (TREE_TYPE (cst),
3942 (cmp @0 uniform_integer_cst_p@1)
3943 (with { tree cst = uniform_integer_cst_p (@1); }
3944 (if (tree_int_cst_sgn (cst) == 1)
3945 (acmp @0 { build_uniform_cst (TREE_TYPE (@1),
3946 wide_int_to_tree (TREE_TYPE (cst),
3947 wi::to_wide (cst) - 1)); })))))
3949 /* We can simplify a logical negation of a comparison to the
3950 inverted comparison. As we cannot compute an expression
3951 operator using invert_tree_comparison we have to simulate
3952 that with expression code iteration. */
3953 (for cmp (tcc_comparison)
3954 icmp (inverted_tcc_comparison)
3955 ncmp (inverted_tcc_comparison_with_nans)
3956 /* Ideally we'd like to combine the following two patterns
3957 and handle some more cases by using
3958 (logical_inverted_value (cmp @0 @1))
3959 here but for that genmatch would need to "inline" that.
3960 For now implement what forward_propagate_comparison did. */
3962 (bit_not (cmp @0 @1))
3963 (if (VECTOR_TYPE_P (type)
3964 || (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1))
3965 /* Comparison inversion may be impossible for trapping math,
3966 invert_tree_comparison will tell us. But we can't use
3967 a computed operator in the replacement tree thus we have
3968 to play the trick below. */
3969 (with { enum tree_code ic = invert_tree_comparison
3970 (cmp, HONOR_NANS (@0)); }
3976 (bit_xor (cmp @0 @1) integer_truep)
3977 (with { enum tree_code ic = invert_tree_comparison
3978 (cmp, HONOR_NANS (@0)); }
3984 /* Transform comparisons of the form X - Y CMP 0 to X CMP Y.
3985 ??? The transformation is valid for the other operators if overflow
3986 is undefined for the type, but performing it here badly interacts
3987 with the transformation in fold_cond_expr_with_comparison which
3988 attempts to synthetize ABS_EXPR. */
3990 (for sub (minus pointer_diff)
3992 (cmp (sub@2 @0 @1) integer_zerop)
3993 (if (single_use (@2))
3996 /* Transform comparisons of the form X * C1 CMP 0 to X CMP 0 in the
3997 signed arithmetic case. That form is created by the compiler
3998 often enough for folding it to be of value. One example is in
3999 computing loop trip counts after Operator Strength Reduction. */
4000 (for cmp (simple_comparison)
4001 scmp (swapped_simple_comparison)
4003 (cmp (mult@3 @0 INTEGER_CST@1) integer_zerop@2)
4004 /* Handle unfolded multiplication by zero. */
4005 (if (integer_zerop (@1))
4007 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
4008 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
4010 /* If @1 is negative we swap the sense of the comparison. */
4011 (if (tree_int_cst_sgn (@1) < 0)
4015 /* For integral types with undefined overflow fold
4016 x * C1 == C2 into x == C2 / C1 or false.
4017 If overflow wraps and C1 is odd, simplify to x == C2 / C1 in the ring
4021 (cmp (mult @0 INTEGER_CST@1) INTEGER_CST@2)
4022 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
4023 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
4024 && wi::to_wide (@1) != 0)
4025 (with { widest_int quot; }
4026 (if (wi::multiple_of_p (wi::to_widest (@2), wi::to_widest (@1),
4027 TYPE_SIGN (TREE_TYPE (@0)), "))
4028 (cmp @0 { wide_int_to_tree (TREE_TYPE (@0), quot); })
4029 { constant_boolean_node (cmp == NE_EXPR, type); }))
4030 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
4031 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))
4032 && (wi::bit_and (wi::to_wide (@1), 1) == 1))
4035 tree itype = TREE_TYPE (@0);
4036 int p = TYPE_PRECISION (itype);
4037 wide_int m = wi::one (p + 1) << p;
4038 wide_int a = wide_int::from (wi::to_wide (@1), p + 1, UNSIGNED);
4039 wide_int i = wide_int::from (wi::mod_inv (a, m),
4040 p, TYPE_SIGN (itype));
4041 wide_int_to_tree (itype, wi::mul (i, wi::to_wide (@2)));
4044 /* Simplify comparison of something with itself. For IEEE
4045 floating-point, we can only do some of these simplifications. */
4049 (if (! FLOAT_TYPE_P (TREE_TYPE (@0))
4050 || ! HONOR_NANS (@0))
4051 { constant_boolean_node (true, type); }
4052 (if (cmp != EQ_EXPR)
4058 || ! FLOAT_TYPE_P (TREE_TYPE (@0))
4059 || ! HONOR_NANS (@0))
4060 { constant_boolean_node (false, type); })))
4061 (for cmp (unle unge uneq)
4064 { constant_boolean_node (true, type); }))
4065 (for cmp (unlt ungt)
4071 (if (!flag_trapping_math)
4072 { constant_boolean_node (false, type); }))
4074 /* x == ~x -> false */
4075 /* x != ~x -> true */
4078 (cmp:c @0 (bit_not @0))
4079 { constant_boolean_node (cmp == NE_EXPR, type); }))
4081 /* Fold ~X op ~Y as Y op X. */
4082 (for cmp (simple_comparison)
4084 (cmp (bit_not@2 @0) (bit_not@3 @1))
4085 (if (single_use (@2) && single_use (@3))
4088 /* Fold ~X op C as X op' ~C, where op' is the swapped comparison. */
4089 (for cmp (simple_comparison)
4090 scmp (swapped_simple_comparison)
4092 (cmp (bit_not@2 @0) CONSTANT_CLASS_P@1)
4093 (if (single_use (@2)
4094 && (TREE_CODE (@1) == INTEGER_CST || TREE_CODE (@1) == VECTOR_CST))
4095 (scmp @0 (bit_not @1)))))
4097 (for cmp (simple_comparison)
4098 /* Fold (double)float1 CMP (double)float2 into float1 CMP float2. */
4100 (cmp (convert@2 @0) (convert? @1))
4101 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
4102 && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@2))
4103 == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0)))
4104 && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@2))
4105 == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1))))
4108 tree type1 = TREE_TYPE (@1);
4109 if (TREE_CODE (@1) == REAL_CST && !DECIMAL_FLOAT_TYPE_P (type1))
4111 REAL_VALUE_TYPE orig = TREE_REAL_CST (@1);
4112 if (TYPE_PRECISION (type1) > TYPE_PRECISION (float_type_node)
4113 && exact_real_truncate (TYPE_MODE (float_type_node), &orig))
4114 type1 = float_type_node;
4115 if (TYPE_PRECISION (type1) > TYPE_PRECISION (double_type_node)
4116 && exact_real_truncate (TYPE_MODE (double_type_node), &orig))
4117 type1 = double_type_node;
4120 = (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type1)
4121 ? TREE_TYPE (@0) : type1);
4123 (if (TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (newtype))
4124 (cmp (convert:newtype @0) (convert:newtype @1))))))
4128 /* IEEE doesn't distinguish +0 and -0 in comparisons. */
4130 /* a CMP (-0) -> a CMP 0 */
4131 (if (REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@1)))
4132 (cmp @0 { build_real (TREE_TYPE (@1), dconst0); }))
4133 /* x != NaN is always true, other ops are always false. */
4134 (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
4135 && ! HONOR_SNANS (@1))
4136 { constant_boolean_node (cmp == NE_EXPR, type); })
4137 /* Fold comparisons against infinity. */
4138 (if (REAL_VALUE_ISINF (TREE_REAL_CST (@1))
4139 && MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (@1))))
4142 REAL_VALUE_TYPE max;
4143 enum tree_code code = cmp;
4144 bool neg = REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1));
4146 code = swap_tree_comparison (code);
4149 /* x > +Inf is always false, if we ignore NaNs or exceptions. */
4150 (if (code == GT_EXPR
4151 && !(HONOR_NANS (@0) && flag_trapping_math))
4152 { constant_boolean_node (false, type); })
4153 (if (code == LE_EXPR)
4154 /* x <= +Inf is always true, if we don't care about NaNs. */
4155 (if (! HONOR_NANS (@0))
4156 { constant_boolean_node (true, type); }
4157 /* x <= +Inf is the same as x == x, i.e. !isnan(x), but this loses
4158 an "invalid" exception. */
4159 (if (!flag_trapping_math)
4161 /* x == +Inf and x >= +Inf are always equal to x > DBL_MAX, but
4162 for == this introduces an exception for x a NaN. */
4163 (if ((code == EQ_EXPR && !(HONOR_NANS (@0) && flag_trapping_math))
4165 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
4167 (lt @0 { build_real (TREE_TYPE (@0), max); })
4168 (gt @0 { build_real (TREE_TYPE (@0), max); }))))
4169 /* x < +Inf is always equal to x <= DBL_MAX. */
4170 (if (code == LT_EXPR)
4171 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
4173 (ge @0 { build_real (TREE_TYPE (@0), max); })
4174 (le @0 { build_real (TREE_TYPE (@0), max); }))))
4175 /* x != +Inf is always equal to !(x > DBL_MAX), but this introduces
4176 an exception for x a NaN so use an unordered comparison. */
4177 (if (code == NE_EXPR)
4178 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
4179 (if (! HONOR_NANS (@0))
4181 (ge @0 { build_real (TREE_TYPE (@0), max); })
4182 (le @0 { build_real (TREE_TYPE (@0), max); }))
4184 (unge @0 { build_real (TREE_TYPE (@0), max); })
4185 (unle @0 { build_real (TREE_TYPE (@0), max); }))))))))))
4187 /* If this is a comparison of a real constant with a PLUS_EXPR
4188 or a MINUS_EXPR of a real constant, we can convert it into a
4189 comparison with a revised real constant as long as no overflow
4190 occurs when unsafe_math_optimizations are enabled. */
4191 (if (flag_unsafe_math_optimizations)
4192 (for op (plus minus)
4194 (cmp (op @0 REAL_CST@1) REAL_CST@2)
4197 tree tem = const_binop (op == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR,
4198 TREE_TYPE (@1), @2, @1);
4200 (if (tem && !TREE_OVERFLOW (tem))
4201 (cmp @0 { tem; }))))))
4203 /* Likewise, we can simplify a comparison of a real constant with
4204 a MINUS_EXPR whose first operand is also a real constant, i.e.
4205 (c1 - x) < c2 becomes x > c1-c2. Reordering is allowed on
4206 floating-point types only if -fassociative-math is set. */
4207 (if (flag_associative_math)
4209 (cmp (minus REAL_CST@0 @1) REAL_CST@2)
4210 (with { tree tem = const_binop (MINUS_EXPR, TREE_TYPE (@1), @0, @2); }
4211 (if (tem && !TREE_OVERFLOW (tem))
4212 (cmp { tem; } @1)))))
4214 /* Fold comparisons against built-in math functions. */
4215 (if (flag_unsafe_math_optimizations && ! flag_errno_math)
4218 (cmp (sq @0) REAL_CST@1)
4220 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
4222 /* sqrt(x) < y is always false, if y is negative. */
4223 (if (cmp == EQ_EXPR || cmp == LT_EXPR || cmp == LE_EXPR)
4224 { constant_boolean_node (false, type); })
4225 /* sqrt(x) > y is always true, if y is negative and we
4226 don't care about NaNs, i.e. negative values of x. */
4227 (if (cmp == NE_EXPR || !HONOR_NANS (@0))
4228 { constant_boolean_node (true, type); })
4229 /* sqrt(x) > y is the same as x >= 0, if y is negative. */
4230 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })))
4231 (if (real_equal (TREE_REAL_CST_PTR (@1), &dconst0))
4233 /* sqrt(x) < 0 is always false. */
4234 (if (cmp == LT_EXPR)
4235 { constant_boolean_node (false, type); })
4236 /* sqrt(x) >= 0 is always true if we don't care about NaNs. */
4237 (if (cmp == GE_EXPR && !HONOR_NANS (@0))
4238 { constant_boolean_node (true, type); })
4239 /* sqrt(x) <= 0 -> x == 0. */
4240 (if (cmp == LE_EXPR)
4242 /* Otherwise sqrt(x) cmp 0 -> x cmp 0. Here cmp can be >=, >,
4243 == or !=. In the last case:
4245 (sqrt(x) != 0) == (NaN != 0) == true == (x != 0)
4247 if x is negative or NaN. Due to -funsafe-math-optimizations,
4248 the results for other x follow from natural arithmetic. */
4250 (if ((cmp == LT_EXPR
4254 && !REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
4255 /* Give up for -frounding-math. */
4256 && !HONOR_SIGN_DEPENDENT_ROUNDING (TREE_TYPE (@0)))
4260 enum tree_code ncmp = cmp;
4261 const real_format *fmt
4262 = REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (@0)));
4263 real_arithmetic (&c2, MULT_EXPR,
4264 &TREE_REAL_CST (@1), &TREE_REAL_CST (@1));
4265 real_convert (&c2, fmt, &c2);
4266 /* See PR91734: if c2 is inexact and sqrt(c2) < c (or sqrt(c2) >= c),
4267 then change LT_EXPR into LE_EXPR or GE_EXPR into GT_EXPR. */
4268 if (!REAL_VALUE_ISINF (c2))
4270 tree c3 = fold_const_call (CFN_SQRT, TREE_TYPE (@0),
4271 build_real (TREE_TYPE (@0), c2));
4272 if (c3 == NULL_TREE || TREE_CODE (c3) != REAL_CST)
4274 else if ((cmp == LT_EXPR || cmp == GE_EXPR)
4275 && real_less (&TREE_REAL_CST (c3), &TREE_REAL_CST (@1)))
4276 ncmp = cmp == LT_EXPR ? LE_EXPR : GT_EXPR;
4277 else if ((cmp == LE_EXPR || cmp == GT_EXPR)
4278 && real_less (&TREE_REAL_CST (@1), &TREE_REAL_CST (c3)))
4279 ncmp = cmp == LE_EXPR ? LT_EXPR : GE_EXPR;
4282 /* With rounding to even, sqrt of up to 3 different values
4283 gives the same normal result, so in some cases c2 needs
4285 REAL_VALUE_TYPE c2alt, tow;
4286 if (cmp == LT_EXPR || cmp == GE_EXPR)
4290 real_nextafter (&c2alt, fmt, &c2, &tow);
4291 real_convert (&c2alt, fmt, &c2alt);
4292 if (REAL_VALUE_ISINF (c2alt))
4296 c3 = fold_const_call (CFN_SQRT, TREE_TYPE (@0),
4297 build_real (TREE_TYPE (@0), c2alt));
4298 if (c3 == NULL_TREE || TREE_CODE (c3) != REAL_CST)
4300 else if (real_equal (&TREE_REAL_CST (c3),
4301 &TREE_REAL_CST (@1)))
4307 (if (cmp == GT_EXPR || cmp == GE_EXPR)
4308 (if (REAL_VALUE_ISINF (c2))
4309 /* sqrt(x) > y is x == +Inf, when y is very large. */
4310 (if (HONOR_INFINITIES (@0))
4311 (eq @0 { build_real (TREE_TYPE (@0), c2); })
4312 { constant_boolean_node (false, type); })
4313 /* sqrt(x) > c is the same as x > c*c. */
4314 (if (ncmp != ERROR_MARK)
4315 (if (ncmp == GE_EXPR)
4316 (ge @0 { build_real (TREE_TYPE (@0), c2); })
4317 (gt @0 { build_real (TREE_TYPE (@0), c2); }))))
4318 /* else if (cmp == LT_EXPR || cmp == LE_EXPR) */
4319 (if (REAL_VALUE_ISINF (c2))
4321 /* sqrt(x) < y is always true, when y is a very large
4322 value and we don't care about NaNs or Infinities. */
4323 (if (! HONOR_NANS (@0) && ! HONOR_INFINITIES (@0))
4324 { constant_boolean_node (true, type); })
4325 /* sqrt(x) < y is x != +Inf when y is very large and we
4326 don't care about NaNs. */
4327 (if (! HONOR_NANS (@0))
4328 (ne @0 { build_real (TREE_TYPE (@0), c2); }))
4329 /* sqrt(x) < y is x >= 0 when y is very large and we
4330 don't care about Infinities. */
4331 (if (! HONOR_INFINITIES (@0))
4332 (ge @0 { build_real (TREE_TYPE (@0), dconst0); }))
4333 /* sqrt(x) < y is x >= 0 && x != +Inf, when y is large. */
4336 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
4337 (ne @0 { build_real (TREE_TYPE (@0), c2); }))))
4338 /* sqrt(x) < c is the same as x < c*c, if we ignore NaNs. */
4339 (if (ncmp != ERROR_MARK && ! HONOR_NANS (@0))
4340 (if (ncmp == LT_EXPR)
4341 (lt @0 { build_real (TREE_TYPE (@0), c2); })
4342 (le @0 { build_real (TREE_TYPE (@0), c2); }))
4343 /* sqrt(x) < c is the same as x >= 0 && x < c*c. */
4344 (if (ncmp != ERROR_MARK && GENERIC)
4345 (if (ncmp == LT_EXPR)
4347 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
4348 (lt @0 { build_real (TREE_TYPE (@0), c2); }))
4350 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
4351 (le @0 { build_real (TREE_TYPE (@0), c2); })))))))))))
4352 /* Transform sqrt(x) cmp sqrt(y) -> x cmp y. */
4354 (cmp (sq @0) (sq @1))
4355 (if (! HONOR_NANS (@0))
4358 /* Optimize various special cases of (FTYPE) N CMP (FTYPE) M. */
4359 (for cmp (lt le eq ne ge gt unordered ordered unlt unle ungt unge uneq ltgt)
4360 icmp (lt le eq ne ge gt unordered ordered lt le gt ge eq ne)
4362 (cmp (float@0 @1) (float @2))
4363 (if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (@0))
4364 && ! DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0)))
4367 format_helper fmt (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (@0))));
4368 tree type1 = TREE_TYPE (@1);
4369 bool type1_signed_p = TYPE_SIGN (type1) == SIGNED;
4370 tree type2 = TREE_TYPE (@2);
4371 bool type2_signed_p = TYPE_SIGN (type2) == SIGNED;
4373 (if (fmt.can_represent_integral_type_p (type1)
4374 && fmt.can_represent_integral_type_p (type2))
4375 (if (cmp == ORDERED_EXPR || cmp == UNORDERED_EXPR)
4376 { constant_boolean_node (cmp == ORDERED_EXPR, type); }
4377 (if (TYPE_PRECISION (type1) > TYPE_PRECISION (type2)
4378 && type1_signed_p >= type2_signed_p)
4379 (icmp @1 (convert @2))
4380 (if (TYPE_PRECISION (type1) < TYPE_PRECISION (type2)
4381 && type1_signed_p <= type2_signed_p)
4382 (icmp (convert:type2 @1) @2)
4383 (if (TYPE_PRECISION (type1) == TYPE_PRECISION (type2)
4384 && type1_signed_p == type2_signed_p)
4385 (icmp @1 @2))))))))))
4387 /* Optimize various special cases of (FTYPE) N CMP CST. */
4388 (for cmp (lt le eq ne ge gt)
4389 icmp (le le eq ne ge ge)
4391 (cmp (float @0) REAL_CST@1)
4392 (if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (@1))
4393 && ! DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1)))
4396 tree itype = TREE_TYPE (@0);
4397 format_helper fmt (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (@1))));
4398 const REAL_VALUE_TYPE *cst = TREE_REAL_CST_PTR (@1);
4399 /* Be careful to preserve any potential exceptions due to
4400 NaNs. qNaNs are ok in == or != context.
4401 TODO: relax under -fno-trapping-math or
4402 -fno-signaling-nans. */
4404 = real_isnan (cst) && (cst->signalling
4405 || (cmp != EQ_EXPR && cmp != NE_EXPR));
4407 /* TODO: allow non-fitting itype and SNaNs when
4408 -fno-trapping-math. */
4409 (if (fmt.can_represent_integral_type_p (itype) && ! exception_p)
4412 signop isign = TYPE_SIGN (itype);
4413 REAL_VALUE_TYPE imin, imax;
4414 real_from_integer (&imin, fmt, wi::min_value (itype), isign);
4415 real_from_integer (&imax, fmt, wi::max_value (itype), isign);
4417 REAL_VALUE_TYPE icst;
4418 if (cmp == GT_EXPR || cmp == GE_EXPR)
4419 real_ceil (&icst, fmt, cst);
4420 else if (cmp == LT_EXPR || cmp == LE_EXPR)
4421 real_floor (&icst, fmt, cst);
4423 real_trunc (&icst, fmt, cst);
4425 bool cst_int_p = !real_isnan (cst) && real_identical (&icst, cst);
4427 bool overflow_p = false;
4429 = real_to_integer (&icst, &overflow_p, TYPE_PRECISION (itype));
4432 /* Optimize cases when CST is outside of ITYPE's range. */
4433 (if (real_compare (LT_EXPR, cst, &imin))
4434 { constant_boolean_node (cmp == GT_EXPR || cmp == GE_EXPR || cmp == NE_EXPR,
4436 (if (real_compare (GT_EXPR, cst, &imax))
4437 { constant_boolean_node (cmp == LT_EXPR || cmp == LE_EXPR || cmp == NE_EXPR,
4439 /* Remove cast if CST is an integer representable by ITYPE. */
4441 (cmp @0 { gcc_assert (!overflow_p);
4442 wide_int_to_tree (itype, icst_val); })
4444 /* When CST is fractional, optimize
4445 (FTYPE) N == CST -> 0
4446 (FTYPE) N != CST -> 1. */
4447 (if (cmp == EQ_EXPR || cmp == NE_EXPR)
4448 { constant_boolean_node (cmp == NE_EXPR, type); })
4449 /* Otherwise replace with sensible integer constant. */
4452 gcc_checking_assert (!overflow_p);
4454 (icmp @0 { wide_int_to_tree (itype, icst_val); })))))))))
4456 /* Fold A /[ex] B CMP C to A CMP B * C. */
4459 (cmp (exact_div @0 @1) INTEGER_CST@2)
4460 (if (!integer_zerop (@1))
4461 (if (wi::to_wide (@2) == 0)
4463 (if (TREE_CODE (@1) == INTEGER_CST)
4466 wi::overflow_type ovf;
4467 wide_int prod = wi::mul (wi::to_wide (@2), wi::to_wide (@1),
4468 TYPE_SIGN (TREE_TYPE (@1)), &ovf);
4471 { constant_boolean_node (cmp == NE_EXPR, type); }
4472 (cmp @0 { wide_int_to_tree (TREE_TYPE (@0), prod); }))))))))
4473 (for cmp (lt le gt ge)
4475 (cmp (exact_div @0 INTEGER_CST@1) INTEGER_CST@2)
4476 (if (wi::gt_p (wi::to_wide (@1), 0, TYPE_SIGN (TREE_TYPE (@1))))
4479 wi::overflow_type ovf;
4480 wide_int prod = wi::mul (wi::to_wide (@2), wi::to_wide (@1),
4481 TYPE_SIGN (TREE_TYPE (@1)), &ovf);
4484 { constant_boolean_node (wi::lt_p (wi::to_wide (@2), 0,
4485 TYPE_SIGN (TREE_TYPE (@2)))
4486 != (cmp == LT_EXPR || cmp == LE_EXPR), type); }
4487 (cmp @0 { wide_int_to_tree (TREE_TYPE (@0), prod); }))))))
4489 /* Fold (size_t)(A /[ex] B) CMP C to (size_t)A CMP (size_t)B * C or A CMP' 0.
4491 For small C (less than max/B), this is (size_t)A CMP (size_t)B * C.
4492 For large C (more than min/B+2^size), this is also true, with the
4493 multiplication computed modulo 2^size.
4494 For intermediate C, this just tests the sign of A. */
4495 (for cmp (lt le gt ge)
4498 (cmp (convert (exact_div @0 INTEGER_CST@1)) INTEGER_CST@2)
4499 (if (tree_nop_conversion_p (TREE_TYPE (@0), TREE_TYPE (@2))
4500 && TYPE_UNSIGNED (TREE_TYPE (@2)) && !TYPE_UNSIGNED (TREE_TYPE (@0))
4501 && wi::gt_p (wi::to_wide (@1), 0, TYPE_SIGN (TREE_TYPE (@1))))
4504 tree utype = TREE_TYPE (@2);
4505 wide_int denom = wi::to_wide (@1);
4506 wide_int right = wi::to_wide (@2);
4507 wide_int smax = wi::sdiv_trunc (wi::max_value (TREE_TYPE (@0)), denom);
4508 wide_int smin = wi::sdiv_trunc (wi::min_value (TREE_TYPE (@0)), denom);
4509 bool small = wi::leu_p (right, smax);
4510 bool large = wi::geu_p (right, smin);
4512 (if (small || large)
4513 (cmp (convert:utype @0) (mult @2 (convert @1)))
4514 (cmp2 @0 { build_zero_cst (TREE_TYPE (@0)); }))))))
4516 /* Unordered tests if either argument is a NaN. */
4518 (bit_ior (unordered @0 @0) (unordered @1 @1))
4519 (if (types_match (@0, @1))
4522 (bit_and (ordered @0 @0) (ordered @1 @1))
4523 (if (types_match (@0, @1))
4526 (bit_ior:c (unordered @0 @0) (unordered:c@2 @0 @1))
4529 (bit_and:c (ordered @0 @0) (ordered:c@2 @0 @1))
4532 /* Simple range test simplifications. */
4533 /* A < B || A >= B -> true. */
4534 (for test1 (lt le le le ne ge)
4535 test2 (ge gt ge ne eq ne)
4537 (bit_ior:c (test1 @0 @1) (test2 @0 @1))
4538 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
4539 || VECTOR_INTEGER_TYPE_P (TREE_TYPE (@0)))
4540 { constant_boolean_node (true, type); })))
4541 /* A < B && A >= B -> false. */
4542 (for test1 (lt lt lt le ne eq)
4543 test2 (ge gt eq gt eq gt)
4545 (bit_and:c (test1 @0 @1) (test2 @0 @1))
4546 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
4547 || VECTOR_INTEGER_TYPE_P (TREE_TYPE (@0)))
4548 { constant_boolean_node (false, type); })))
4550 /* A & (2**N - 1) <= 2**K - 1 -> A & (2**N - 2**K) == 0
4551 A & (2**N - 1) > 2**K - 1 -> A & (2**N - 2**K) != 0
4553 Note that comparisons
4554 A & (2**N - 1) < 2**K -> A & (2**N - 2**K) == 0
4555 A & (2**N - 1) >= 2**K -> A & (2**N - 2**K) != 0
4556 will be canonicalized to above so there's no need to
4563 (cmp (bit_and@0 @1 INTEGER_CST@2) INTEGER_CST@3)
4564 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)))
4567 tree ty = TREE_TYPE (@0);
4568 unsigned prec = TYPE_PRECISION (ty);
4569 wide_int mask = wi::to_wide (@2, prec);
4570 wide_int rhs = wi::to_wide (@3, prec);
4571 signop sgn = TYPE_SIGN (ty);
4573 (if ((mask & (mask + 1)) == 0 && wi::gt_p (rhs, 0, sgn)
4574 && (rhs & (rhs + 1)) == 0 && wi::ge_p (mask, rhs, sgn))
4575 (eqcmp (bit_and @1 { wide_int_to_tree (ty, mask - rhs); })
4576 { build_zero_cst (ty); }))))))
4578 /* -A CMP -B -> B CMP A. */
4579 (for cmp (tcc_comparison)
4580 scmp (swapped_tcc_comparison)
4582 (cmp (negate @0) (negate @1))
4583 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
4584 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
4585 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
4588 (cmp (negate @0) CONSTANT_CLASS_P@1)
4589 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
4590 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
4591 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
4592 (with { tree tem = const_unop (NEGATE_EXPR, TREE_TYPE (@0), @1); }
4593 (if (tem && !TREE_OVERFLOW (tem))
4594 (scmp @0 { tem; }))))))
4596 /* Convert ABS_EXPR<x> == 0 or ABS_EXPR<x> != 0 to x == 0 or x != 0. */
4599 (op (abs @0) zerop@1)
4602 /* From fold_sign_changed_comparison and fold_widened_comparison.
4603 FIXME: the lack of symmetry is disturbing. */
4604 (for cmp (simple_comparison)
4606 (cmp (convert@0 @00) (convert?@1 @10))
4607 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
4608 /* Disable this optimization if we're casting a function pointer
4609 type on targets that require function pointer canonicalization. */
4610 && !(targetm.have_canonicalize_funcptr_for_compare ()
4611 && ((POINTER_TYPE_P (TREE_TYPE (@00))
4612 && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@00))))
4613 || (POINTER_TYPE_P (TREE_TYPE (@10))
4614 && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@10))))))
4616 (if (TYPE_PRECISION (TREE_TYPE (@00)) == TYPE_PRECISION (TREE_TYPE (@0))
4617 && (TREE_CODE (@10) == INTEGER_CST
4619 && (TYPE_UNSIGNED (TREE_TYPE (@00)) == TYPE_UNSIGNED (TREE_TYPE (@0))
4622 && !POINTER_TYPE_P (TREE_TYPE (@00)))
4623 /* ??? The special-casing of INTEGER_CST conversion was in the original
4624 code and here to avoid a spurious overflow flag on the resulting
4625 constant which fold_convert produces. */
4626 (if (TREE_CODE (@1) == INTEGER_CST)
4627 (cmp @00 { force_fit_type (TREE_TYPE (@00), wi::to_widest (@1), 0,
4628 TREE_OVERFLOW (@1)); })
4629 (cmp @00 (convert @1)))
4631 (if (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (TREE_TYPE (@00)))
4632 /* If possible, express the comparison in the shorter mode. */
4633 (if ((cmp == EQ_EXPR || cmp == NE_EXPR
4634 || TYPE_UNSIGNED (TREE_TYPE (@0)) == TYPE_UNSIGNED (TREE_TYPE (@00))
4635 || (!TYPE_UNSIGNED (TREE_TYPE (@0))
4636 && TYPE_UNSIGNED (TREE_TYPE (@00))))
4637 && (types_match (TREE_TYPE (@10), TREE_TYPE (@00))
4638 || ((TYPE_PRECISION (TREE_TYPE (@00))
4639 >= TYPE_PRECISION (TREE_TYPE (@10)))
4640 && (TYPE_UNSIGNED (TREE_TYPE (@00))
4641 == TYPE_UNSIGNED (TREE_TYPE (@10))))
4642 || (TREE_CODE (@10) == INTEGER_CST
4643 && INTEGRAL_TYPE_P (TREE_TYPE (@00))
4644 && int_fits_type_p (@10, TREE_TYPE (@00)))))
4645 (cmp @00 (convert @10))
4646 (if (TREE_CODE (@10) == INTEGER_CST
4647 && INTEGRAL_TYPE_P (TREE_TYPE (@00))
4648 && !int_fits_type_p (@10, TREE_TYPE (@00)))
4651 tree min = lower_bound_in_type (TREE_TYPE (@10), TREE_TYPE (@00));
4652 tree max = upper_bound_in_type (TREE_TYPE (@10), TREE_TYPE (@00));
4653 bool above = integer_nonzerop (const_binop (LT_EXPR, type, max, @10));
4654 bool below = integer_nonzerop (const_binop (LT_EXPR, type, @10, min));
4656 (if (above || below)
4657 (if (cmp == EQ_EXPR || cmp == NE_EXPR)
4658 { constant_boolean_node (cmp == EQ_EXPR ? false : true, type); }
4659 (if (cmp == LT_EXPR || cmp == LE_EXPR)
4660 { constant_boolean_node (above ? true : false, type); }
4661 (if (cmp == GT_EXPR || cmp == GE_EXPR)
4662 { constant_boolean_node (above ? false : true, type); }))))))))))))
4666 /* SSA names are canonicalized to 2nd place. */
4667 (cmp addr@0 SSA_NAME@1)
4669 { poly_int64 off; tree base; }
4670 /* A local variable can never be pointed to by
4671 the default SSA name of an incoming parameter. */
4672 (if (SSA_NAME_IS_DEFAULT_DEF (@1)
4673 && TREE_CODE (SSA_NAME_VAR (@1)) == PARM_DECL
4674 && (base = get_base_address (TREE_OPERAND (@0, 0)))
4675 && TREE_CODE (base) == VAR_DECL
4676 && auto_var_in_fn_p (base, current_function_decl))
4677 (if (cmp == NE_EXPR)
4678 { constant_boolean_node (true, type); }
4679 { constant_boolean_node (false, type); })
4680 /* If the address is based on @1 decide using the offset. */
4681 (if ((base = get_addr_base_and_unit_offset (TREE_OPERAND (@0, 0), &off))
4682 && TREE_CODE (base) == MEM_REF
4683 && TREE_OPERAND (base, 0) == @1)
4684 (with { off += mem_ref_offset (base).force_shwi (); }
4685 (if (known_ne (off, 0))
4686 { constant_boolean_node (cmp == NE_EXPR, type); }
4687 (if (known_eq (off, 0))
4688 { constant_boolean_node (cmp == EQ_EXPR, type); }))))))))
4690 /* Equality compare simplifications from fold_binary */
4693 /* If we have (A | C) == D where C & ~D != 0, convert this into 0.
4694 Similarly for NE_EXPR. */
4696 (cmp (convert?@3 (bit_ior @0 INTEGER_CST@1)) INTEGER_CST@2)
4697 (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0))
4698 && wi::bit_and_not (wi::to_wide (@1), wi::to_wide (@2)) != 0)
4699 { constant_boolean_node (cmp == NE_EXPR, type); }))
4701 /* (X ^ Y) == 0 becomes X == Y, and (X ^ Y) != 0 becomes X != Y. */
4703 (cmp (bit_xor @0 @1) integer_zerop)
4706 /* (X ^ Y) == Y becomes X == 0.
4707 Likewise (X ^ Y) == X becomes Y == 0. */
4709 (cmp:c (bit_xor:c @0 @1) @0)
4710 (cmp @1 { build_zero_cst (TREE_TYPE (@1)); }))
4712 /* (X ^ C1) op C2 can be rewritten as X op (C1 ^ C2). */
4714 (cmp (convert?@3 (bit_xor @0 INTEGER_CST@1)) INTEGER_CST@2)
4715 (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0)))
4716 (cmp @0 (bit_xor @1 (convert @2)))))
4719 (cmp (convert? addr@0) integer_zerop)
4720 (if (tree_single_nonzero_warnv_p (@0, NULL))
4721 { constant_boolean_node (cmp == NE_EXPR, type); }))
4723 /* (X & C) op (Y & C) into (X ^ Y) & C op 0. */
4725 (cmp (bit_and:cs @0 @2) (bit_and:cs @1 @2))
4726 (cmp (bit_and (bit_xor @0 @1) @2) { build_zero_cst (TREE_TYPE (@2)); })))
4728 /* (X < 0) != (Y < 0) into (X ^ Y) < 0.
4729 (X >= 0) != (Y >= 0) into (X ^ Y) < 0.
4730 (X < 0) == (Y < 0) into (X ^ Y) >= 0.
4731 (X >= 0) == (Y >= 0) into (X ^ Y) >= 0. */
4736 (cmp (sgncmp @0 integer_zerop@2) (sgncmp @1 integer_zerop))
4737 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
4738 && !TYPE_UNSIGNED (TREE_TYPE (@0))
4739 && types_match (@0, @1))
4740 (ncmp (bit_xor @0 @1) @2)))))
4741 /* (X < 0) == (Y >= 0) into (X ^ Y) < 0.
4742 (X < 0) != (Y >= 0) into (X ^ Y) >= 0. */
4746 (cmp:c (lt @0 integer_zerop@2) (ge @1 integer_zerop))
4747 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
4748 && !TYPE_UNSIGNED (TREE_TYPE (@0))
4749 && types_match (@0, @1))
4750 (ncmp (bit_xor @0 @1) @2))))
4752 /* If we have (A & C) == C where C is a power of 2, convert this into
4753 (A & C) != 0. Similarly for NE_EXPR. */
4757 (cmp (bit_and@2 @0 integer_pow2p@1) @1)
4758 (icmp @2 { build_zero_cst (TREE_TYPE (@0)); })))
4760 /* If we have (A & C) != 0 ? D : 0 where C and D are powers of 2,
4761 convert this into a shift followed by ANDing with D. */
4764 (ne (bit_and @0 integer_pow2p@1) integer_zerop)
4765 INTEGER_CST@2 integer_zerop)
4766 (if (integer_pow2p (@2))
4768 int shift = (wi::exact_log2 (wi::to_wide (@2))
4769 - wi::exact_log2 (wi::to_wide (@1)));
4773 (lshift (convert @0) { build_int_cst (integer_type_node, shift); }) @2)
4775 (convert (rshift @0 { build_int_cst (integer_type_node, -shift); }))
4778 /* If we have (A & C) != 0 where C is the sign bit of A, convert
4779 this into A < 0. Similarly for (A & C) == 0 into A >= 0. */
4783 (cmp (bit_and (convert?@2 @0) integer_pow2p@1) integer_zerop)
4784 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
4785 && type_has_mode_precision_p (TREE_TYPE (@0))
4786 && element_precision (@2) >= element_precision (@0)
4787 && wi::only_sign_bit_p (wi::to_wide (@1), element_precision (@0)))
4788 (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
4789 (ncmp (convert:stype @0) { build_zero_cst (stype); })))))
4791 /* If we have A < 0 ? C : 0 where C is a power of 2, convert
4792 this into a right shift or sign extension followed by ANDing with C. */
4795 (lt @0 integer_zerop)
4796 INTEGER_CST@1 integer_zerop)
4797 (if (integer_pow2p (@1)
4798 && !TYPE_UNSIGNED (TREE_TYPE (@0)))
4800 int shift = element_precision (@0) - wi::exact_log2 (wi::to_wide (@1)) - 1;
4804 (convert (rshift @0 { build_int_cst (integer_type_node, shift); }))
4806 /* Otherwise ctype must be wider than TREE_TYPE (@0) and pure
4807 sign extension followed by AND with C will achieve the effect. */
4808 (bit_and (convert @0) @1)))))
4810 /* When the addresses are not directly of decls compare base and offset.
4811 This implements some remaining parts of fold_comparison address
4812 comparisons but still no complete part of it. Still it is good
4813 enough to make fold_stmt not regress when not dispatching to fold_binary. */
4814 (for cmp (simple_comparison)
4816 (cmp (convert1?@2 addr@0) (convert2? addr@1))
4819 poly_int64 off0, off1;
4820 tree base0 = get_addr_base_and_unit_offset (TREE_OPERAND (@0, 0), &off0);
4821 tree base1 = get_addr_base_and_unit_offset (TREE_OPERAND (@1, 0), &off1);
4822 if (base0 && TREE_CODE (base0) == MEM_REF)
4824 off0 += mem_ref_offset (base0).force_shwi ();
4825 base0 = TREE_OPERAND (base0, 0);
4827 if (base1 && TREE_CODE (base1) == MEM_REF)
4829 off1 += mem_ref_offset (base1).force_shwi ();
4830 base1 = TREE_OPERAND (base1, 0);
4833 (if (base0 && base1)
4837 /* Punt in GENERIC on variables with value expressions;
4838 the value expressions might point to fields/elements
4839 of other vars etc. */
4841 && ((VAR_P (base0) && DECL_HAS_VALUE_EXPR_P (base0))
4842 || (VAR_P (base1) && DECL_HAS_VALUE_EXPR_P (base1))))
4844 else if (decl_in_symtab_p (base0)
4845 && decl_in_symtab_p (base1))
4846 equal = symtab_node::get_create (base0)
4847 ->equal_address_to (symtab_node::get_create (base1));
4848 else if ((DECL_P (base0)
4849 || TREE_CODE (base0) == SSA_NAME
4850 || TREE_CODE (base0) == STRING_CST)
4852 || TREE_CODE (base1) == SSA_NAME
4853 || TREE_CODE (base1) == STRING_CST))
4854 equal = (base0 == base1);
4857 HOST_WIDE_INT ioff0 = -1, ioff1 = -1;
4858 off0.is_constant (&ioff0);
4859 off1.is_constant (&ioff1);
4860 if ((DECL_P (base0) && TREE_CODE (base1) == STRING_CST)
4861 || (TREE_CODE (base0) == STRING_CST && DECL_P (base1))
4862 || (TREE_CODE (base0) == STRING_CST
4863 && TREE_CODE (base1) == STRING_CST
4864 && ioff0 >= 0 && ioff1 >= 0
4865 && ioff0 < TREE_STRING_LENGTH (base0)
4866 && ioff1 < TREE_STRING_LENGTH (base1)
4867 /* This is a too conservative test that the STRING_CSTs
4868 will not end up being string-merged. */
4869 && strncmp (TREE_STRING_POINTER (base0) + ioff0,
4870 TREE_STRING_POINTER (base1) + ioff1,
4871 MIN (TREE_STRING_LENGTH (base0) - ioff0,
4872 TREE_STRING_LENGTH (base1) - ioff1)) != 0))
4874 else if (!DECL_P (base0) || !DECL_P (base1))
4876 else if (cmp != EQ_EXPR && cmp != NE_EXPR)
4878 /* If this is a pointer comparison, ignore for now even
4879 valid equalities where one pointer is the offset zero
4880 of one object and the other to one past end of another one. */
4881 else if (!INTEGRAL_TYPE_P (TREE_TYPE (@2)))
4883 /* Assume that automatic variables can't be adjacent to global
4885 else if (is_global_var (base0) != is_global_var (base1))
4889 tree sz0 = DECL_SIZE_UNIT (base0);
4890 tree sz1 = DECL_SIZE_UNIT (base1);
4891 /* If sizes are unknown, e.g. VLA or not representable,
4893 if (!tree_fits_poly_int64_p (sz0)
4894 || !tree_fits_poly_int64_p (sz1))
4898 poly_int64 size0 = tree_to_poly_int64 (sz0);
4899 poly_int64 size1 = tree_to_poly_int64 (sz1);
4900 /* If one offset is pointing (or could be) to the beginning
4901 of one object and the other is pointing to one past the
4902 last byte of the other object, punt. */
4903 if (maybe_eq (off0, 0) && maybe_eq (off1, size1))
4905 else if (maybe_eq (off1, 0) && maybe_eq (off0, size0))
4907 /* If both offsets are the same, there are some cases
4908 we know that are ok. Either if we know they aren't
4909 zero, or if we know both sizes are no zero. */
4911 && known_eq (off0, off1)
4912 && (known_ne (off0, 0)
4913 || (known_ne (size0, 0) && known_ne (size1, 0))))
4920 && (cmp == EQ_EXPR || cmp == NE_EXPR
4921 /* If the offsets are equal we can ignore overflow. */
4922 || known_eq (off0, off1)
4923 || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
4924 /* Or if we compare using pointers to decls or strings. */
4925 || (POINTER_TYPE_P (TREE_TYPE (@2))
4926 && (DECL_P (base0) || TREE_CODE (base0) == STRING_CST))))
4928 (if (cmp == EQ_EXPR && (known_eq (off0, off1) || known_ne (off0, off1)))
4929 { constant_boolean_node (known_eq (off0, off1), type); })
4930 (if (cmp == NE_EXPR && (known_eq (off0, off1) || known_ne (off0, off1)))
4931 { constant_boolean_node (known_ne (off0, off1), type); })
4932 (if (cmp == LT_EXPR && (known_lt (off0, off1) || known_ge (off0, off1)))
4933 { constant_boolean_node (known_lt (off0, off1), type); })
4934 (if (cmp == LE_EXPR && (known_le (off0, off1) || known_gt (off0, off1)))
4935 { constant_boolean_node (known_le (off0, off1), type); })
4936 (if (cmp == GE_EXPR && (known_ge (off0, off1) || known_lt (off0, off1)))
4937 { constant_boolean_node (known_ge (off0, off1), type); })
4938 (if (cmp == GT_EXPR && (known_gt (off0, off1) || known_le (off0, off1)))
4939 { constant_boolean_node (known_gt (off0, off1), type); }))
4942 (if (cmp == EQ_EXPR)
4943 { constant_boolean_node (false, type); })
4944 (if (cmp == NE_EXPR)
4945 { constant_boolean_node (true, type); })))))))))
4947 /* Simplify pointer equality compares using PTA. */
4951 (if (POINTER_TYPE_P (TREE_TYPE (@0))
4952 && ptrs_compare_unequal (@0, @1))
4953 { constant_boolean_node (neeq != EQ_EXPR, type); })))
4955 /* PR70920: Transform (intptr_t)x eq/ne CST to x eq/ne (typeof x) CST.
4956 and (typeof ptr_cst) x eq/ne ptr_cst to x eq/ne (typeof x) CST.
4957 Disable the transform if either operand is pointer to function.
4958 This broke pr22051-2.c for arm where function pointer
4959 canonicalizaion is not wanted. */
4963 (cmp (convert @0) INTEGER_CST@1)
4964 (if (((POINTER_TYPE_P (TREE_TYPE (@0))
4965 && !FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@0)))
4966 && INTEGRAL_TYPE_P (TREE_TYPE (@1)))
4967 || (INTEGRAL_TYPE_P (TREE_TYPE (@0))
4968 && POINTER_TYPE_P (TREE_TYPE (@1))
4969 && !FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@1)))))
4970 && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
4971 (cmp @0 (convert @1)))))
4973 /* Non-equality compare simplifications from fold_binary */
4974 (for cmp (lt gt le ge)
4975 /* Comparisons with the highest or lowest possible integer of
4976 the specified precision will have known values. */
4978 (cmp (convert?@2 @0) uniform_integer_cst_p@1)
4979 (if ((INTEGRAL_TYPE_P (TREE_TYPE (@1))
4980 || POINTER_TYPE_P (TREE_TYPE (@1))
4981 || VECTOR_INTEGER_TYPE_P (TREE_TYPE (@1)))
4982 && tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@0)))
4985 tree cst = uniform_integer_cst_p (@1);
4986 tree arg1_type = TREE_TYPE (cst);
4987 unsigned int prec = TYPE_PRECISION (arg1_type);
4988 wide_int max = wi::max_value (arg1_type);
4989 wide_int signed_max = wi::max_value (prec, SIGNED);
4990 wide_int min = wi::min_value (arg1_type);
4993 (if (wi::to_wide (cst) == max)
4995 (if (cmp == GT_EXPR)
4996 { constant_boolean_node (false, type); })
4997 (if (cmp == GE_EXPR)
4999 (if (cmp == LE_EXPR)
5000 { constant_boolean_node (true, type); })
5001 (if (cmp == LT_EXPR)
5003 (if (wi::to_wide (cst) == min)
5005 (if (cmp == LT_EXPR)
5006 { constant_boolean_node (false, type); })
5007 (if (cmp == LE_EXPR)
5009 (if (cmp == GE_EXPR)
5010 { constant_boolean_node (true, type); })
5011 (if (cmp == GT_EXPR)
5013 (if (wi::to_wide (cst) == max - 1)
5015 (if (cmp == GT_EXPR)
5016 (eq @2 { build_uniform_cst (TREE_TYPE (@1),
5017 wide_int_to_tree (TREE_TYPE (cst),
5020 (if (cmp == LE_EXPR)
5021 (ne @2 { build_uniform_cst (TREE_TYPE (@1),
5022 wide_int_to_tree (TREE_TYPE (cst),
5025 (if (wi::to_wide (cst) == min + 1)
5027 (if (cmp == GE_EXPR)
5028 (ne @2 { build_uniform_cst (TREE_TYPE (@1),
5029 wide_int_to_tree (TREE_TYPE (cst),
5032 (if (cmp == LT_EXPR)
5033 (eq @2 { build_uniform_cst (TREE_TYPE (@1),
5034 wide_int_to_tree (TREE_TYPE (cst),
5037 (if (wi::to_wide (cst) == signed_max
5038 && TYPE_UNSIGNED (arg1_type)
5039 /* We will flip the signedness of the comparison operator
5040 associated with the mode of @1, so the sign bit is
5041 specified by this mode. Check that @1 is the signed
5042 max associated with this sign bit. */
5043 && prec == GET_MODE_PRECISION (SCALAR_INT_TYPE_MODE (arg1_type))
5044 /* signed_type does not work on pointer types. */
5045 && INTEGRAL_TYPE_P (arg1_type))
5046 /* The following case also applies to X < signed_max+1
5047 and X >= signed_max+1 because previous transformations. */
5048 (if (cmp == LE_EXPR || cmp == GT_EXPR)
5049 (with { tree st = signed_type_for (TREE_TYPE (@1)); }
5051 (if (cst == @1 && cmp == LE_EXPR)
5052 (ge (convert:st @0) { build_zero_cst (st); }))
5053 (if (cst == @1 && cmp == GT_EXPR)
5054 (lt (convert:st @0) { build_zero_cst (st); }))
5055 (if (cmp == LE_EXPR)
5056 (ge (view_convert:st @0) { build_zero_cst (st); }))
5057 (if (cmp == GT_EXPR)
5058 (lt (view_convert:st @0) { build_zero_cst (st); })))))))))))
5060 (for cmp (unordered ordered unlt unle ungt unge uneq ltgt)
5061 /* If the second operand is NaN, the result is constant. */
5064 (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
5065 && (cmp != LTGT_EXPR || ! flag_trapping_math))
5066 { constant_boolean_node (cmp == ORDERED_EXPR || cmp == LTGT_EXPR
5067 ? false : true, type); })))
5069 /* Fold UNORDERED if either operand must be NaN, or neither can be. */
5073 (if (tree_expr_nan_p (@0) || tree_expr_nan_p (@1))
5074 { constant_boolean_node (true, type); })
5075 (if (!tree_expr_maybe_nan_p (@0) && !tree_expr_maybe_nan_p (@1))
5076 { constant_boolean_node (false, type); })))
5078 /* Fold ORDERED if either operand must be NaN, or neither can be. */
5082 (if (tree_expr_nan_p (@0) || tree_expr_nan_p (@1))
5083 { constant_boolean_node (false, type); })
5084 (if (!tree_expr_maybe_nan_p (@0) && !tree_expr_maybe_nan_p (@1))
5085 { constant_boolean_node (true, type); })))
5087 /* bool_var != 0 becomes bool_var. */
5089 (ne @0 integer_zerop)
5090 (if (TREE_CODE (TREE_TYPE (@0)) == BOOLEAN_TYPE
5091 && types_match (type, TREE_TYPE (@0)))
5093 /* bool_var == 1 becomes bool_var. */
5095 (eq @0 integer_onep)
5096 (if (TREE_CODE (TREE_TYPE (@0)) == BOOLEAN_TYPE
5097 && types_match (type, TREE_TYPE (@0)))
5100 bool_var == 0 becomes !bool_var or
5101 bool_var != 1 becomes !bool_var
5102 here because that only is good in assignment context as long
5103 as we require a tcc_comparison in GIMPLE_CONDs where we'd
5104 replace if (x == 0) with tem = ~x; if (tem != 0) which is
5105 clearly less optimal and which we'll transform again in forwprop. */
5107 /* When one argument is a constant, overflow detection can be simplified.
5108 Currently restricted to single use so as not to interfere too much with
5109 ADD_OVERFLOW detection in tree-ssa-math-opts.c.
5110 CONVERT?(CONVERT?(A) + CST) CMP A -> A CMP' CST' */
5111 (for cmp (lt le ge gt)
5114 (cmp:c (convert?@3 (plus@2 (convert?@4 @0) INTEGER_CST@1)) @0)
5115 (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@2))
5116 && types_match (TREE_TYPE (@0), TREE_TYPE (@3))
5117 && tree_nop_conversion_p (TREE_TYPE (@4), TREE_TYPE (@0))
5118 && wi::to_wide (@1) != 0
5121 unsigned int prec = TYPE_PRECISION (TREE_TYPE (@0));
5122 signop sign = TYPE_SIGN (TREE_TYPE (@0));
5124 (out @0 { wide_int_to_tree (TREE_TYPE (@0),
5125 wi::max_value (prec, sign)
5126 - wi::to_wide (@1)); })))))
5128 /* To detect overflow in unsigned A - B, A < B is simpler than A - B > A.
5129 However, the detection logic for SUB_OVERFLOW in tree-ssa-math-opts.c
5130 expects the long form, so we restrict the transformation for now. */
5133 (cmp:c (minus@2 @0 @1) @0)
5134 (if (single_use (@2)
5135 && ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
5136 && TYPE_UNSIGNED (TREE_TYPE (@0)))
5139 /* Optimize A - B + -1 >= A into B >= A for unsigned comparisons. */
5142 (cmp:c (plus (minus @0 @1) integer_minus_onep) @0)
5143 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
5144 && TYPE_UNSIGNED (TREE_TYPE (@0)))
5147 /* Testing for overflow is unnecessary if we already know the result. */
5152 (cmp:c (realpart (IFN_SUB_OVERFLOW@2 @0 @1)) @0)
5153 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
5154 && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
5155 (out (imagpart @2) { build_zero_cst (TREE_TYPE (@0)); }))))
5160 (cmp:c (realpart (IFN_ADD_OVERFLOW:c@2 @0 @1)) @0)
5161 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
5162 && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
5163 (out (imagpart @2) { build_zero_cst (TREE_TYPE (@0)); }))))
5165 /* For unsigned operands, -1 / B < A checks whether A * B would overflow.
5166 Simplify it to __builtin_mul_overflow (A, B, <unused>). */
5170 (cmp:c (trunc_div:s integer_all_onesp @1) @0)
5171 (if (TYPE_UNSIGNED (TREE_TYPE (@0)) && !VECTOR_TYPE_P (TREE_TYPE (@0)))
5172 (with { tree t = TREE_TYPE (@0), cpx = build_complex_type (t); }
5173 (out (imagpart (IFN_MUL_OVERFLOW:cpx @0 @1)) { build_zero_cst (t); })))))
5175 /* Similarly, for unsigned operands, (((type) A * B) >> prec) != 0 where type
5176 is at least twice as wide as type of A and B, simplify to
5177 __builtin_mul_overflow (A, B, <unused>). */
5180 (cmp (rshift (mult:s (convert@3 @0) (convert @1)) INTEGER_CST@2)
5182 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
5183 && INTEGRAL_TYPE_P (TREE_TYPE (@3))
5184 && TYPE_UNSIGNED (TREE_TYPE (@0))
5185 && (TYPE_PRECISION (TREE_TYPE (@3))
5186 >= 2 * TYPE_PRECISION (TREE_TYPE (@0)))
5187 && tree_fits_uhwi_p (@2)
5188 && tree_to_uhwi (@2) == TYPE_PRECISION (TREE_TYPE (@0))
5189 && types_match (@0, @1)
5190 && type_has_mode_precision_p (TREE_TYPE (@0))
5191 && (optab_handler (umulv4_optab, TYPE_MODE (TREE_TYPE (@0)))
5192 != CODE_FOR_nothing))
5193 (with { tree t = TREE_TYPE (@0), cpx = build_complex_type (t); }
5194 (cmp (imagpart (IFN_MUL_OVERFLOW:cpx @0 @1)) { build_zero_cst (t); })))))
5196 /* Simplification of math builtins. These rules must all be optimizations
5197 as well as IL simplifications. If there is a possibility that the new
5198 form could be a pessimization, the rule should go in the canonicalization
5199 section that follows this one.
5201 Rules can generally go in this section if they satisfy one of
5204 - the rule describes an identity
5206 - the rule replaces calls with something as simple as addition or
5209 - the rule contains unary calls only and simplifies the surrounding
5210 arithmetic. (The idea here is to exclude non-unary calls in which
5211 one operand is constant and in which the call is known to be cheap
5212 when the operand has that value.) */
5214 (if (flag_unsafe_math_optimizations)
5215 /* Simplify sqrt(x) * sqrt(x) -> x. */
5217 (mult (SQRT_ALL@1 @0) @1)
5218 (if (!tree_expr_maybe_signaling_nan_p (@0))
5221 (for op (plus minus)
5222 /* Simplify (A / C) +- (B / C) -> (A +- B) / C. */
5226 (rdiv (op @0 @2) @1)))
5228 (for cmp (lt le gt ge)
5229 neg_cmp (gt ge lt le)
5230 /* Simplify (x * C1) cmp C2 -> x cmp (C2 / C1), where C1 != 0. */
5232 (cmp (mult @0 REAL_CST@1) REAL_CST@2)
5234 { tree tem = const_binop (RDIV_EXPR, type, @2, @1); }
5236 && !(REAL_VALUE_ISINF (TREE_REAL_CST (tem))
5237 || (real_zerop (tem) && !real_zerop (@1))))
5239 (if (real_less (&dconst0, TREE_REAL_CST_PTR (@1)))
5241 (if (real_less (TREE_REAL_CST_PTR (@1), &dconst0))
5242 (neg_cmp @0 { tem; })))))))
5244 /* Simplify sqrt(x) * sqrt(y) -> sqrt(x*y). */
5245 (for root (SQRT CBRT)
5247 (mult (root:s @0) (root:s @1))
5248 (root (mult @0 @1))))
5250 /* Simplify expN(x) * expN(y) -> expN(x+y). */
5251 (for exps (EXP EXP2 EXP10 POW10)
5253 (mult (exps:s @0) (exps:s @1))
5254 (exps (plus @0 @1))))
5256 /* Simplify a/root(b/c) into a*root(c/b). */
5257 (for root (SQRT CBRT)
5259 (rdiv @0 (root:s (rdiv:s @1 @2)))
5260 (mult @0 (root (rdiv @2 @1)))))
5262 /* Simplify x/expN(y) into x*expN(-y). */
5263 (for exps (EXP EXP2 EXP10 POW10)
5265 (rdiv @0 (exps:s @1))
5266 (mult @0 (exps (negate @1)))))
5268 (for logs (LOG LOG2 LOG10 LOG10)
5269 exps (EXP EXP2 EXP10 POW10)
5270 /* logN(expN(x)) -> x. */
5274 /* expN(logN(x)) -> x. */
5279 /* Optimize logN(func()) for various exponential functions. We
5280 want to determine the value "x" and the power "exponent" in
5281 order to transform logN(x**exponent) into exponent*logN(x). */
5282 (for logs (LOG LOG LOG LOG2 LOG2 LOG2 LOG10 LOG10)
5283 exps (EXP2 EXP10 POW10 EXP EXP10 POW10 EXP EXP2)
5286 (if (SCALAR_FLOAT_TYPE_P (type))
5292 /* Prepare to do logN(exp(exponent)) -> exponent*logN(e). */
5293 x = build_real_truncate (type, dconst_e ());
5296 /* Prepare to do logN(exp2(exponent)) -> exponent*logN(2). */
5297 x = build_real (type, dconst2);
5301 /* Prepare to do logN(exp10(exponent)) -> exponent*logN(10). */
5303 REAL_VALUE_TYPE dconst10;
5304 real_from_integer (&dconst10, VOIDmode, 10, SIGNED);
5305 x = build_real (type, dconst10);
5312 (mult (logs { x; }) @0)))))
5320 (if (SCALAR_FLOAT_TYPE_P (type))
5326 /* Prepare to do logN(sqrt(x)) -> 0.5*logN(x). */
5327 x = build_real (type, dconsthalf);
5330 /* Prepare to do logN(cbrt(x)) -> (1/3)*logN(x). */
5331 x = build_real_truncate (type, dconst_third ());
5337 (mult { x; } (logs @0))))))
5339 /* logN(pow(x,exponent)) -> exponent*logN(x). */
5340 (for logs (LOG LOG2 LOG10)
5344 (mult @1 (logs @0))))
5346 /* pow(C,x) -> exp(log(C)*x) if C > 0,
5347 or if C is a positive power of 2,
5348 pow(C,x) -> exp2(log2(C)*x). */
5356 (pows REAL_CST@0 @1)
5357 (if (real_compare (GT_EXPR, TREE_REAL_CST_PTR (@0), &dconst0)
5358 && real_isfinite (TREE_REAL_CST_PTR (@0))
5359 /* As libmvec doesn't have a vectorized exp2, defer optimizing
5360 the use_exp2 case until after vectorization. It seems actually
5361 beneficial for all constants to postpone this until later,
5362 because exp(log(C)*x), while faster, will have worse precision
5363 and if x folds into a constant too, that is unnecessary
5365 && canonicalize_math_after_vectorization_p ())
5367 const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (@0);
5368 bool use_exp2 = false;
5369 if (targetm.libc_has_function (function_c99_misc, TREE_TYPE (@0))
5370 && value->cl == rvc_normal)
5372 REAL_VALUE_TYPE frac_rvt = *value;
5373 SET_REAL_EXP (&frac_rvt, 1);
5374 if (real_equal (&frac_rvt, &dconst1))
5379 (if (optimize_pow_to_exp (@0, @1))
5380 (exps (mult (logs @0) @1)))
5381 (exp2s (mult (log2s @0) @1)))))))
5384 /* pow(C,x)*expN(y) -> expN(logN(C)*x+y) if C > 0. */
5386 exps (EXP EXP2 EXP10 POW10)
5387 logs (LOG LOG2 LOG10 LOG10)
5389 (mult:c (pows:s REAL_CST@0 @1) (exps:s @2))
5390 (if (real_compare (GT_EXPR, TREE_REAL_CST_PTR (@0), &dconst0)
5391 && real_isfinite (TREE_REAL_CST_PTR (@0)))
5392 (exps (plus (mult (logs @0) @1) @2)))))
5397 exps (EXP EXP2 EXP10 POW10)
5398 /* sqrt(expN(x)) -> expN(x*0.5). */
5401 (exps (mult @0 { build_real (type, dconsthalf); })))
5402 /* cbrt(expN(x)) -> expN(x/3). */
5405 (exps (mult @0 { build_real_truncate (type, dconst_third ()); })))
5406 /* pow(expN(x), y) -> expN(x*y). */
5409 (exps (mult @0 @1))))
5411 /* tan(atan(x)) -> x. */
5418 /* Simplify sin(atan(x)) -> x / sqrt(x*x + 1). */
5422 copysigns (COPYSIGN)
5427 REAL_VALUE_TYPE r_cst;
5428 build_sinatan_real (&r_cst, type);
5429 tree t_cst = build_real (type, r_cst);
5430 tree t_one = build_one_cst (type);
5432 (if (SCALAR_FLOAT_TYPE_P (type))
5433 (cond (lt (abs @0) { t_cst; })
5434 (rdiv @0 (sqrts (plus (mult @0 @0) { t_one; })))
5435 (copysigns { t_one; } @0))))))
5437 /* Simplify cos(atan(x)) -> 1 / sqrt(x*x + 1). */
5441 copysigns (COPYSIGN)
5446 REAL_VALUE_TYPE r_cst;
5447 build_sinatan_real (&r_cst, type);
5448 tree t_cst = build_real (type, r_cst);
5449 tree t_one = build_one_cst (type);
5450 tree t_zero = build_zero_cst (type);
5452 (if (SCALAR_FLOAT_TYPE_P (type))
5453 (cond (lt (abs @0) { t_cst; })
5454 (rdiv { t_one; } (sqrts (plus (mult @0 @0) { t_one; })))
5455 (copysigns { t_zero; } @0))))))
5457 (if (!flag_errno_math)
5458 /* Simplify sinh(atanh(x)) -> x / sqrt((1 - x)*(1 + x)). */
5463 (sinhs (atanhs:s @0))
5464 (with { tree t_one = build_one_cst (type); }
5465 (rdiv @0 (sqrts (mult (minus { t_one; } @0) (plus { t_one; } @0)))))))
5467 /* Simplify cosh(atanh(x)) -> 1 / sqrt((1 - x)*(1 + x)) */
5472 (coshs (atanhs:s @0))
5473 (with { tree t_one = build_one_cst (type); }
5474 (rdiv { t_one; } (sqrts (mult (minus { t_one; } @0) (plus { t_one; } @0))))))))
5476 /* cabs(x+0i) or cabs(0+xi) -> abs(x). */
5478 (CABS (complex:C @0 real_zerop@1))
5481 /* trunc(trunc(x)) -> trunc(x), etc. */
5482 (for fns (TRUNC_ALL FLOOR_ALL CEIL_ALL ROUND_ALL NEARBYINT_ALL RINT_ALL)
5486 /* f(x) -> x if x is integer valued and f does nothing for such values. */
5487 (for fns (TRUNC_ALL FLOOR_ALL CEIL_ALL ROUND_ALL NEARBYINT_ALL RINT_ALL)
5489 (fns integer_valued_real_p@0)
5492 /* hypot(x,0) and hypot(0,x) -> abs(x). */
5494 (HYPOT:c @0 real_zerop@1)
5497 /* pow(1,x) -> 1. */
5499 (POW real_onep@0 @1)
5503 /* copysign(x,x) -> x. */
5504 (COPYSIGN_ALL @0 @0)
5508 /* copysign(x,-x) -> -x. */
5509 (COPYSIGN_ALL @0 (negate@1 @0))
5513 /* copysign(x,y) -> fabs(x) if y is nonnegative. */
5514 (COPYSIGN_ALL @0 tree_expr_nonnegative_p@1)
5517 (for scale (LDEXP SCALBN SCALBLN)
5518 /* ldexp(0, x) -> 0. */
5520 (scale real_zerop@0 @1)
5522 /* ldexp(x, 0) -> x. */
5524 (scale @0 integer_zerop@1)
5526 /* ldexp(x, y) -> x if x is +-Inf or NaN. */
5528 (scale REAL_CST@0 @1)
5529 (if (!real_isfinite (TREE_REAL_CST_PTR (@0)))
5532 /* Canonicalization of sequences of math builtins. These rules represent
5533 IL simplifications but are not necessarily optimizations.
5535 The sincos pass is responsible for picking "optimal" implementations
5536 of math builtins, which may be more complicated and can sometimes go
5537 the other way, e.g. converting pow into a sequence of sqrts.
5538 We only want to do these canonicalizations before the pass has run. */
5540 (if (flag_unsafe_math_optimizations && canonicalize_math_p ())
5541 /* Simplify tan(x) * cos(x) -> sin(x). */
5543 (mult:c (TAN:s @0) (COS:s @0))
5546 /* Simplify x * pow(x,c) -> pow(x,c+1). */
5548 (mult:c @0 (POW:s @0 REAL_CST@1))
5549 (if (!TREE_OVERFLOW (@1))
5550 (POW @0 (plus @1 { build_one_cst (type); }))))
5552 /* Simplify sin(x) / cos(x) -> tan(x). */
5554 (rdiv (SIN:s @0) (COS:s @0))
5557 /* Simplify sinh(x) / cosh(x) -> tanh(x). */
5559 (rdiv (SINH:s @0) (COSH:s @0))
5562 /* Simplify tanh (x) / sinh (x) -> 1.0 / cosh (x). */
5564 (rdiv (TANH:s @0) (SINH:s @0))
5565 (rdiv {build_one_cst (type);} (COSH @0)))
5567 /* Simplify cos(x) / sin(x) -> 1 / tan(x). */
5569 (rdiv (COS:s @0) (SIN:s @0))
5570 (rdiv { build_one_cst (type); } (TAN @0)))
5572 /* Simplify sin(x) / tan(x) -> cos(x). */
5574 (rdiv (SIN:s @0) (TAN:s @0))
5575 (if (! HONOR_NANS (@0)
5576 && ! HONOR_INFINITIES (@0))
5579 /* Simplify tan(x) / sin(x) -> 1.0 / cos(x). */
5581 (rdiv (TAN:s @0) (SIN:s @0))
5582 (if (! HONOR_NANS (@0)
5583 && ! HONOR_INFINITIES (@0))
5584 (rdiv { build_one_cst (type); } (COS @0))))
5586 /* Simplify pow(x,y) * pow(x,z) -> pow(x,y+z). */
5588 (mult (POW:s @0 @1) (POW:s @0 @2))
5589 (POW @0 (plus @1 @2)))
5591 /* Simplify pow(x,y) * pow(z,y) -> pow(x*z,y). */
5593 (mult (POW:s @0 @1) (POW:s @2 @1))
5594 (POW (mult @0 @2) @1))
5596 /* Simplify powi(x,y) * powi(z,y) -> powi(x*z,y). */
5598 (mult (POWI:s @0 @1) (POWI:s @2 @1))
5599 (POWI (mult @0 @2) @1))
5601 /* Simplify pow(x,c) / x -> pow(x,c-1). */
5603 (rdiv (POW:s @0 REAL_CST@1) @0)
5604 (if (!TREE_OVERFLOW (@1))
5605 (POW @0 (minus @1 { build_one_cst (type); }))))
5607 /* Simplify x / pow (y,z) -> x * pow(y,-z). */
5609 (rdiv @0 (POW:s @1 @2))
5610 (mult @0 (POW @1 (negate @2))))
5615 /* sqrt(sqrt(x)) -> pow(x,1/4). */
5618 (pows @0 { build_real (type, dconst_quarter ()); }))
5619 /* sqrt(cbrt(x)) -> pow(x,1/6). */
5622 (pows @0 { build_real_truncate (type, dconst_sixth ()); }))
5623 /* cbrt(sqrt(x)) -> pow(x,1/6). */
5626 (pows @0 { build_real_truncate (type, dconst_sixth ()); }))
5627 /* cbrt(cbrt(x)) -> pow(x,1/9), iff x is nonnegative. */
5629 (cbrts (cbrts tree_expr_nonnegative_p@0))
5630 (pows @0 { build_real_truncate (type, dconst_ninth ()); }))
5631 /* sqrt(pow(x,y)) -> pow(|x|,y*0.5). */
5633 (sqrts (pows @0 @1))
5634 (pows (abs @0) (mult @1 { build_real (type, dconsthalf); })))
5635 /* cbrt(pow(x,y)) -> pow(x,y/3), iff x is nonnegative. */
5637 (cbrts (pows tree_expr_nonnegative_p@0 @1))
5638 (pows @0 (mult @1 { build_real_truncate (type, dconst_third ()); })))
5639 /* pow(sqrt(x),y) -> pow(x,y*0.5). */
5641 (pows (sqrts @0) @1)
5642 (pows @0 (mult @1 { build_real (type, dconsthalf); })))
5643 /* pow(cbrt(x),y) -> pow(x,y/3) iff x is nonnegative. */
5645 (pows (cbrts tree_expr_nonnegative_p@0) @1)
5646 (pows @0 (mult @1 { build_real_truncate (type, dconst_third ()); })))
5647 /* pow(pow(x,y),z) -> pow(x,y*z) iff x is nonnegative. */
5649 (pows (pows tree_expr_nonnegative_p@0 @1) @2)
5650 (pows @0 (mult @1 @2))))
5652 /* cabs(x+xi) -> fabs(x)*sqrt(2). */
5654 (CABS (complex @0 @0))
5655 (mult (abs @0) { build_real_truncate (type, dconst_sqrt2 ()); }))
5657 /* hypot(x,x) -> fabs(x)*sqrt(2). */
5660 (mult (abs @0) { build_real_truncate (type, dconst_sqrt2 ()); }))
5662 /* cexp(x+yi) -> exp(x)*cexpi(y). */
5667 (cexps compositional_complex@0)
5668 (if (targetm.libc_has_function (function_c99_math_complex, TREE_TYPE (@0)))
5670 (mult (exps@1 (realpart @0)) (realpart (cexpis:type@2 (imagpart @0))))
5671 (mult @1 (imagpart @2)))))))
5673 (if (canonicalize_math_p ())
5674 /* floor(x) -> trunc(x) if x is nonnegative. */
5675 (for floors (FLOOR_ALL)
5678 (floors tree_expr_nonnegative_p@0)
5681 (match double_value_p
5683 (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == double_type_node)))
5684 (for froms (BUILT_IN_TRUNCL
5696 /* truncl(extend(x)) -> extend(trunc(x)), etc., if x is a double. */
5697 (if (optimize && canonicalize_math_p ())
5699 (froms (convert double_value_p@0))
5700 (convert (tos @0)))))
5702 (match float_value_p
5704 (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == float_type_node)))
5705 (for froms (BUILT_IN_TRUNCL BUILT_IN_TRUNC
5706 BUILT_IN_FLOORL BUILT_IN_FLOOR
5707 BUILT_IN_CEILL BUILT_IN_CEIL
5708 BUILT_IN_ROUNDL BUILT_IN_ROUND
5709 BUILT_IN_NEARBYINTL BUILT_IN_NEARBYINT
5710 BUILT_IN_RINTL BUILT_IN_RINT)
5711 tos (BUILT_IN_TRUNCF BUILT_IN_TRUNCF
5712 BUILT_IN_FLOORF BUILT_IN_FLOORF
5713 BUILT_IN_CEILF BUILT_IN_CEILF
5714 BUILT_IN_ROUNDF BUILT_IN_ROUNDF
5715 BUILT_IN_NEARBYINTF BUILT_IN_NEARBYINTF
5716 BUILT_IN_RINTF BUILT_IN_RINTF)
5717 /* truncl(extend(x)) and trunc(extend(x)) -> extend(truncf(x)), etc.,
5719 (if (optimize && canonicalize_math_p ()
5720 && targetm.libc_has_function (function_c99_misc, NULL_TREE))
5722 (froms (convert float_value_p@0))
5723 (convert (tos @0)))))
5725 (for froms (XFLOORL XCEILL XROUNDL XRINTL)
5726 tos (XFLOOR XCEIL XROUND XRINT)
5727 /* llfloorl(extend(x)) -> llfloor(x), etc., if x is a double. */
5728 (if (optimize && canonicalize_math_p ())
5730 (froms (convert double_value_p@0))
5733 (for froms (XFLOORL XCEILL XROUNDL XRINTL
5734 XFLOOR XCEIL XROUND XRINT)
5735 tos (XFLOORF XCEILF XROUNDF XRINTF)
5736 /* llfloorl(extend(x)) and llfloor(extend(x)) -> llfloorf(x), etc.,
5738 (if (optimize && canonicalize_math_p ())
5740 (froms (convert float_value_p@0))
5743 (if (canonicalize_math_p ())
5744 /* xfloor(x) -> fix_trunc(x) if x is nonnegative. */
5745 (for floors (IFLOOR LFLOOR LLFLOOR)
5747 (floors tree_expr_nonnegative_p@0)
5750 (if (canonicalize_math_p ())
5751 /* xfloor(x) -> fix_trunc(x), etc., if x is integer valued. */
5752 (for fns (IFLOOR LFLOOR LLFLOOR
5754 IROUND LROUND LLROUND)
5756 (fns integer_valued_real_p@0)
5758 (if (!flag_errno_math)
5759 /* xrint(x) -> fix_trunc(x), etc., if x is integer valued. */
5760 (for rints (IRINT LRINT LLRINT)
5762 (rints integer_valued_real_p@0)
5765 (if (canonicalize_math_p ())
5766 (for ifn (IFLOOR ICEIL IROUND IRINT)
5767 lfn (LFLOOR LCEIL LROUND LRINT)
5768 llfn (LLFLOOR LLCEIL LLROUND LLRINT)
5769 /* Canonicalize iround (x) to lround (x) on ILP32 targets where
5770 sizeof (int) == sizeof (long). */
5771 (if (TYPE_PRECISION (integer_type_node)
5772 == TYPE_PRECISION (long_integer_type_node))
5775 (lfn:long_integer_type_node @0)))
5776 /* Canonicalize llround (x) to lround (x) on LP64 targets where
5777 sizeof (long long) == sizeof (long). */
5778 (if (TYPE_PRECISION (long_long_integer_type_node)
5779 == TYPE_PRECISION (long_integer_type_node))
5782 (lfn:long_integer_type_node @0)))))
5784 /* cproj(x) -> x if we're ignoring infinities. */
5787 (if (!HONOR_INFINITIES (type))
5790 /* If the real part is inf and the imag part is known to be
5791 nonnegative, return (inf + 0i). */
5793 (CPROJ (complex REAL_CST@0 tree_expr_nonnegative_p@1))
5794 (if (real_isinf (TREE_REAL_CST_PTR (@0)))
5795 { build_complex_inf (type, false); }))
5797 /* If the imag part is inf, return (inf+I*copysign(0,imag)). */
5799 (CPROJ (complex @0 REAL_CST@1))
5800 (if (real_isinf (TREE_REAL_CST_PTR (@1)))
5801 { build_complex_inf (type, TREE_REAL_CST_PTR (@1)->sign); }))
5807 (pows @0 REAL_CST@1)
5809 const REAL_VALUE_TYPE *value = TREE_REAL_CST_PTR (@1);
5810 REAL_VALUE_TYPE tmp;
5813 /* pow(x,0) -> 1. */
5814 (if (real_equal (value, &dconst0))
5815 { build_real (type, dconst1); })
5816 /* pow(x,1) -> x. */
5817 (if (real_equal (value, &dconst1))
5819 /* pow(x,-1) -> 1/x. */
5820 (if (real_equal (value, &dconstm1))
5821 (rdiv { build_real (type, dconst1); } @0))
5822 /* pow(x,0.5) -> sqrt(x). */
5823 (if (flag_unsafe_math_optimizations
5824 && canonicalize_math_p ()
5825 && real_equal (value, &dconsthalf))
5827 /* pow(x,1/3) -> cbrt(x). */
5828 (if (flag_unsafe_math_optimizations
5829 && canonicalize_math_p ()
5830 && (tmp = real_value_truncate (TYPE_MODE (type), dconst_third ()),
5831 real_equal (value, &tmp)))
5834 /* powi(1,x) -> 1. */
5836 (POWI real_onep@0 @1)
5840 (POWI @0 INTEGER_CST@1)
5842 /* powi(x,0) -> 1. */
5843 (if (wi::to_wide (@1) == 0)
5844 { build_real (type, dconst1); })
5845 /* powi(x,1) -> x. */
5846 (if (wi::to_wide (@1) == 1)
5848 /* powi(x,-1) -> 1/x. */
5849 (if (wi::to_wide (@1) == -1)
5850 (rdiv { build_real (type, dconst1); } @0))))
5852 /* Narrowing of arithmetic and logical operations.
5854 These are conceptually similar to the transformations performed for
5855 the C/C++ front-ends by shorten_binary_op and shorten_compare. Long
5856 term we want to move all that code out of the front-ends into here. */
5858 /* Convert (outertype)((innertype0)a+(innertype1)b)
5859 into ((newtype)a+(newtype)b) where newtype
5860 is the widest mode from all of these. */
5861 (for op (plus minus mult rdiv)
5863 (convert (op:s@0 (convert1?@3 @1) (convert2?@4 @2)))
5864 /* If we have a narrowing conversion of an arithmetic operation where
5865 both operands are widening conversions from the same type as the outer
5866 narrowing conversion. Then convert the innermost operands to a
5867 suitable unsigned type (to avoid introducing undefined behavior),
5868 perform the operation and convert the result to the desired type. */
5869 (if (INTEGRAL_TYPE_P (type)
5872 /* We check for type compatibility between @0 and @1 below,
5873 so there's no need to check that @2/@4 are integral types. */
5874 && INTEGRAL_TYPE_P (TREE_TYPE (@1))
5875 && INTEGRAL_TYPE_P (TREE_TYPE (@3))
5876 /* The precision of the type of each operand must match the
5877 precision of the mode of each operand, similarly for the
5879 && type_has_mode_precision_p (TREE_TYPE (@1))
5880 && type_has_mode_precision_p (TREE_TYPE (@2))
5881 && type_has_mode_precision_p (type)
5882 /* The inner conversion must be a widening conversion. */
5883 && TYPE_PRECISION (TREE_TYPE (@3)) > TYPE_PRECISION (TREE_TYPE (@1))
5884 && types_match (@1, type)
5885 && (types_match (@1, @2)
5886 /* Or the second operand is const integer or converted const
5887 integer from valueize. */
5888 || TREE_CODE (@2) == INTEGER_CST))
5889 (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
5890 (op @1 (convert @2))
5891 (with { tree utype = unsigned_type_for (TREE_TYPE (@1)); }
5892 (convert (op (convert:utype @1)
5893 (convert:utype @2)))))
5894 (if (FLOAT_TYPE_P (type)
5895 && DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0))
5896 == DECIMAL_FLOAT_TYPE_P (type))
5897 (with { tree arg0 = strip_float_extensions (@1);
5898 tree arg1 = strip_float_extensions (@2);
5899 tree itype = TREE_TYPE (@0);
5900 tree ty1 = TREE_TYPE (arg0);
5901 tree ty2 = TREE_TYPE (arg1);
5902 enum tree_code code = TREE_CODE (itype); }
5903 (if (FLOAT_TYPE_P (ty1)
5904 && FLOAT_TYPE_P (ty2))
5905 (with { tree newtype = type;
5906 if (TYPE_MODE (ty1) == SDmode
5907 || TYPE_MODE (ty2) == SDmode
5908 || TYPE_MODE (type) == SDmode)
5909 newtype = dfloat32_type_node;
5910 if (TYPE_MODE (ty1) == DDmode
5911 || TYPE_MODE (ty2) == DDmode
5912 || TYPE_MODE (type) == DDmode)
5913 newtype = dfloat64_type_node;
5914 if (TYPE_MODE (ty1) == TDmode
5915 || TYPE_MODE (ty2) == TDmode
5916 || TYPE_MODE (type) == TDmode)
5917 newtype = dfloat128_type_node; }
5918 (if ((newtype == dfloat32_type_node
5919 || newtype == dfloat64_type_node
5920 || newtype == dfloat128_type_node)
5922 && types_match (newtype, type))
5923 (op (convert:newtype @1) (convert:newtype @2))
5924 (with { if (TYPE_PRECISION (ty1) > TYPE_PRECISION (newtype))
5926 if (TYPE_PRECISION (ty2) > TYPE_PRECISION (newtype))
5928 /* Sometimes this transformation is safe (cannot
5929 change results through affecting double rounding
5930 cases) and sometimes it is not. If NEWTYPE is
5931 wider than TYPE, e.g. (float)((long double)double
5932 + (long double)double) converted to
5933 (float)(double + double), the transformation is
5934 unsafe regardless of the details of the types
5935 involved; double rounding can arise if the result
5936 of NEWTYPE arithmetic is a NEWTYPE value half way
5937 between two representable TYPE values but the
5938 exact value is sufficiently different (in the
5939 right direction) for this difference to be
5940 visible in ITYPE arithmetic. If NEWTYPE is the
5941 same as TYPE, however, the transformation may be
5942 safe depending on the types involved: it is safe
5943 if the ITYPE has strictly more than twice as many
5944 mantissa bits as TYPE, can represent infinities
5945 and NaNs if the TYPE can, and has sufficient
5946 exponent range for the product or ratio of two
5947 values representable in the TYPE to be within the
5948 range of normal values of ITYPE. */
5949 (if (TYPE_PRECISION (newtype) < TYPE_PRECISION (itype)
5950 && (flag_unsafe_math_optimizations
5951 || (TYPE_PRECISION (newtype) == TYPE_PRECISION (type)
5952 && real_can_shorten_arithmetic (TYPE_MODE (itype),
5954 && !excess_precision_type (newtype)))
5955 && !types_match (itype, newtype))
5956 (convert:type (op (convert:newtype @1)
5957 (convert:newtype @2)))
5962 /* This is another case of narrowing, specifically when there's an outer
5963 BIT_AND_EXPR which masks off bits outside the type of the innermost
5964 operands. Like the previous case we have to convert the operands
5965 to unsigned types to avoid introducing undefined behavior for the
5966 arithmetic operation. */
5967 (for op (minus plus)
5969 (bit_and (op:s (convert@2 @0) (convert@3 @1)) INTEGER_CST@4)
5970 (if (INTEGRAL_TYPE_P (type)
5971 /* We check for type compatibility between @0 and @1 below,
5972 so there's no need to check that @1/@3 are integral types. */
5973 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
5974 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
5975 /* The precision of the type of each operand must match the
5976 precision of the mode of each operand, similarly for the
5978 && type_has_mode_precision_p (TREE_TYPE (@0))
5979 && type_has_mode_precision_p (TREE_TYPE (@1))
5980 && type_has_mode_precision_p (type)
5981 /* The inner conversion must be a widening conversion. */
5982 && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
5983 && types_match (@0, @1)
5984 && (tree_int_cst_min_precision (@4, TYPE_SIGN (TREE_TYPE (@0)))
5985 <= TYPE_PRECISION (TREE_TYPE (@0)))
5986 && (wi::to_wide (@4)
5987 & wi::mask (TYPE_PRECISION (TREE_TYPE (@0)),
5988 true, TYPE_PRECISION (type))) == 0)
5989 (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
5990 (with { tree ntype = TREE_TYPE (@0); }
5991 (convert (bit_and (op @0 @1) (convert:ntype @4))))
5992 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
5993 (convert (bit_and (op (convert:utype @0) (convert:utype @1))
5994 (convert:utype @4))))))))
5996 /* Transform (@0 < @1 and @0 < @2) to use min,
5997 (@0 > @1 and @0 > @2) to use max */
5998 (for logic (bit_and bit_and bit_and bit_and bit_ior bit_ior bit_ior bit_ior)
5999 op (lt le gt ge lt le gt ge )
6000 ext (min min max max max max min min )
6002 (logic (op:cs @0 @1) (op:cs @0 @2))
6003 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6004 && TREE_CODE (@0) != INTEGER_CST)
6005 (op @0 (ext @1 @2)))))
6008 /* signbit(x) -> 0 if x is nonnegative. */
6009 (SIGNBIT tree_expr_nonnegative_p@0)
6010 { integer_zero_node; })
6013 /* signbit(x) -> x<0 if x doesn't have signed zeros. */
6015 (if (!HONOR_SIGNED_ZEROS (@0))
6016 (convert (lt @0 { build_real (TREE_TYPE (@0), dconst0); }))))
6018 /* Transform comparisons of the form X +- C1 CMP C2 to X CMP C2 -+ C1. */
6020 (for op (plus minus)
6023 (cmp (op@3 @0 INTEGER_CST@1) INTEGER_CST@2)
6024 (if (!TREE_OVERFLOW (@1) && !TREE_OVERFLOW (@2)
6025 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0))
6026 && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@0))
6027 && !TYPE_SATURATING (TREE_TYPE (@0)))
6028 (with { tree res = int_const_binop (rop, @2, @1); }
6029 (if (TREE_OVERFLOW (res)
6030 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
6031 { constant_boolean_node (cmp == NE_EXPR, type); }
6032 (if (single_use (@3))
6033 (cmp @0 { TREE_OVERFLOW (res)
6034 ? drop_tree_overflow (res) : res; }))))))))
6035 (for cmp (lt le gt ge)
6036 (for op (plus minus)
6039 (cmp (op@3 @0 INTEGER_CST@1) INTEGER_CST@2)
6040 (if (!TREE_OVERFLOW (@1) && !TREE_OVERFLOW (@2)
6041 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
6042 (with { tree res = int_const_binop (rop, @2, @1); }
6043 (if (TREE_OVERFLOW (res))
6045 fold_overflow_warning (("assuming signed overflow does not occur "
6046 "when simplifying conditional to constant"),
6047 WARN_STRICT_OVERFLOW_CONDITIONAL);
6048 bool less = cmp == LE_EXPR || cmp == LT_EXPR;
6049 /* wi::ges_p (@2, 0) should be sufficient for a signed type. */
6050 bool ovf_high = wi::lt_p (wi::to_wide (@1), 0,
6051 TYPE_SIGN (TREE_TYPE (@1)))
6052 != (op == MINUS_EXPR);
6053 constant_boolean_node (less == ovf_high, type);
6055 (if (single_use (@3))
6058 fold_overflow_warning (("assuming signed overflow does not occur "
6059 "when changing X +- C1 cmp C2 to "
6061 WARN_STRICT_OVERFLOW_COMPARISON);
6063 (cmp @0 { res; })))))))))
6065 /* Canonicalizations of BIT_FIELD_REFs. */
6068 (BIT_FIELD_REF (BIT_FIELD_REF @0 @1 @2) @3 @4)
6069 (BIT_FIELD_REF @0 @3 { const_binop (PLUS_EXPR, bitsizetype, @2, @4); }))
6072 (BIT_FIELD_REF (view_convert @0) @1 @2)
6073 (BIT_FIELD_REF @0 @1 @2))
6076 (BIT_FIELD_REF @0 @1 integer_zerop)
6077 (if (tree_int_cst_equal (@1, TYPE_SIZE (TREE_TYPE (@0))))
6081 (BIT_FIELD_REF @0 @1 @2)
6083 (if (TREE_CODE (TREE_TYPE (@0)) == COMPLEX_TYPE
6084 && tree_int_cst_equal (@1, TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))))
6086 (if (integer_zerop (@2))
6087 (view_convert (realpart @0)))
6088 (if (tree_int_cst_equal (@2, TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))))
6089 (view_convert (imagpart @0)))))
6090 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6091 && INTEGRAL_TYPE_P (type)
6092 /* On GIMPLE this should only apply to register arguments. */
6093 && (! GIMPLE || is_gimple_reg (@0))
6094 /* A bit-field-ref that referenced the full argument can be stripped. */
6095 && ((compare_tree_int (@1, TYPE_PRECISION (TREE_TYPE (@0))) == 0
6096 && integer_zerop (@2))
6097 /* Low-parts can be reduced to integral conversions.
6098 ??? The following doesn't work for PDP endian. */
6099 || (BYTES_BIG_ENDIAN == WORDS_BIG_ENDIAN
6100 /* But only do this after vectorization. */
6101 && canonicalize_math_after_vectorization_p ()
6102 /* Don't even think about BITS_BIG_ENDIAN. */
6103 && TYPE_PRECISION (TREE_TYPE (@0)) % BITS_PER_UNIT == 0
6104 && TYPE_PRECISION (type) % BITS_PER_UNIT == 0
6105 && compare_tree_int (@2, (BYTES_BIG_ENDIAN
6106 ? (TYPE_PRECISION (TREE_TYPE (@0))
6107 - TYPE_PRECISION (type))
6111 /* Simplify vector extracts. */
6114 (BIT_FIELD_REF CONSTRUCTOR@0 @1 @2)
6115 (if (VECTOR_TYPE_P (TREE_TYPE (@0))
6116 && (types_match (type, TREE_TYPE (TREE_TYPE (@0)))
6117 || (VECTOR_TYPE_P (type)
6118 && types_match (TREE_TYPE (type), TREE_TYPE (TREE_TYPE (@0))))))
6121 tree ctor = (TREE_CODE (@0) == SSA_NAME
6122 ? gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0)) : @0);
6123 tree eltype = TREE_TYPE (TREE_TYPE (ctor));
6124 unsigned HOST_WIDE_INT width = tree_to_uhwi (TYPE_SIZE (eltype));
6125 unsigned HOST_WIDE_INT n = tree_to_uhwi (@1);
6126 unsigned HOST_WIDE_INT idx = tree_to_uhwi (@2);
6129 && (idx % width) == 0
6131 && known_le ((idx + n) / width,
6132 TYPE_VECTOR_SUBPARTS (TREE_TYPE (ctor))))
6137 /* Constructor elements can be subvectors. */
6139 if (CONSTRUCTOR_NELTS (ctor) != 0)
6141 tree cons_elem = TREE_TYPE (CONSTRUCTOR_ELT (ctor, 0)->value);
6142 if (TREE_CODE (cons_elem) == VECTOR_TYPE)
6143 k = TYPE_VECTOR_SUBPARTS (cons_elem);
6145 unsigned HOST_WIDE_INT elt, count, const_k;
6148 /* We keep an exact subset of the constructor elements. */
6149 (if (multiple_p (idx, k, &elt) && multiple_p (n, k, &count))
6150 (if (CONSTRUCTOR_NELTS (ctor) == 0)
6151 { build_constructor (type, NULL); }
6153 (if (elt < CONSTRUCTOR_NELTS (ctor))
6154 (view_convert { CONSTRUCTOR_ELT (ctor, elt)->value; })
6155 { build_zero_cst (type); })
6156 /* We don't want to emit new CTORs unless the old one goes away.
6157 ??? Eventually allow this if the CTOR ends up constant or
6159 (if (single_use (@0))
6161 vec<constructor_elt, va_gc> *vals;
6162 vec_alloc (vals, count);
6163 for (unsigned i = 0;
6164 i < count && elt + i < CONSTRUCTOR_NELTS (ctor); ++i)
6165 CONSTRUCTOR_APPEND_ELT (vals, NULL_TREE,
6166 CONSTRUCTOR_ELT (ctor, elt + i)->value);
6167 build_constructor (type, vals);
6169 /* The bitfield references a single constructor element. */
6170 (if (k.is_constant (&const_k)
6171 && idx + n <= (idx / const_k + 1) * const_k)
6173 (if (CONSTRUCTOR_NELTS (ctor) <= idx / const_k)
6174 { build_zero_cst (type); })
6176 (view_convert { CONSTRUCTOR_ELT (ctor, idx / const_k)->value; }))
6177 (BIT_FIELD_REF { CONSTRUCTOR_ELT (ctor, idx / const_k)->value; }
6178 @1 { bitsize_int ((idx % const_k) * width); })))))))))
6180 /* Simplify a bit extraction from a bit insertion for the cases with
6181 the inserted element fully covering the extraction or the insertion
6182 not touching the extraction. */
6184 (BIT_FIELD_REF (bit_insert @0 @1 @ipos) @rsize @rpos)
6187 unsigned HOST_WIDE_INT isize;
6188 if (INTEGRAL_TYPE_P (TREE_TYPE (@1)))
6189 isize = TYPE_PRECISION (TREE_TYPE (@1));
6191 isize = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (@1)));
6194 (if (wi::leu_p (wi::to_wide (@ipos), wi::to_wide (@rpos))
6195 && wi::leu_p (wi::to_wide (@rpos) + wi::to_wide (@rsize),
6196 wi::to_wide (@ipos) + isize))
6197 (BIT_FIELD_REF @1 @rsize { wide_int_to_tree (bitsizetype,
6199 - wi::to_wide (@ipos)); }))
6200 (if (wi::geu_p (wi::to_wide (@ipos),
6201 wi::to_wide (@rpos) + wi::to_wide (@rsize))
6202 || wi::geu_p (wi::to_wide (@rpos),
6203 wi::to_wide (@ipos) + isize))
6204 (BIT_FIELD_REF @0 @rsize @rpos)))))
6206 (if (canonicalize_math_after_vectorization_p ())
6209 (fmas:c (negate @0) @1 @2)
6210 (IFN_FNMA @0 @1 @2))
6212 (fmas @0 @1 (negate @2))
6215 (fmas:c (negate @0) @1 (negate @2))
6216 (IFN_FNMS @0 @1 @2))
6218 (negate (fmas@3 @0 @1 @2))
6219 (if (single_use (@3))
6220 (IFN_FNMS @0 @1 @2))))
6223 (IFN_FMS:c (negate @0) @1 @2)
6224 (IFN_FNMS @0 @1 @2))
6226 (IFN_FMS @0 @1 (negate @2))
6229 (IFN_FMS:c (negate @0) @1 (negate @2))
6230 (IFN_FNMA @0 @1 @2))
6232 (negate (IFN_FMS@3 @0 @1 @2))
6233 (if (single_use (@3))
6234 (IFN_FNMA @0 @1 @2)))
6237 (IFN_FNMA:c (negate @0) @1 @2)
6240 (IFN_FNMA @0 @1 (negate @2))
6241 (IFN_FNMS @0 @1 @2))
6243 (IFN_FNMA:c (negate @0) @1 (negate @2))
6246 (negate (IFN_FNMA@3 @0 @1 @2))
6247 (if (single_use (@3))
6248 (IFN_FMS @0 @1 @2)))
6251 (IFN_FNMS:c (negate @0) @1 @2)
6254 (IFN_FNMS @0 @1 (negate @2))
6255 (IFN_FNMA @0 @1 @2))
6257 (IFN_FNMS:c (negate @0) @1 (negate @2))
6260 (negate (IFN_FNMS@3 @0 @1 @2))
6261 (if (single_use (@3))
6262 (IFN_FMA @0 @1 @2))))
6264 /* CLZ simplifications. */
6269 (op (clz:s @0) INTEGER_CST@1)
6270 (if (integer_zerop (@1))
6271 /* clz(X) == 0 is (int)X < 0 and clz(X) != 0 is (int)X >= 0. */
6272 (with { tree stype = signed_type_for (TREE_TYPE (@0));
6273 HOST_WIDE_INT val = 0;
6274 #ifdef CLZ_DEFINED_VALUE_AT_ZERO
6275 /* Punt on hypothetical weird targets. */
6276 if (CLZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (TREE_TYPE (@0)),
6283 (cmp (convert:stype @0) { build_zero_cst (stype); })))
6284 /* clz(X) == (prec-1) is X == 1 and clz(X) != (prec-1) is X != 1. */
6285 (with { bool ok = true;
6286 #ifdef CLZ_DEFINED_VALUE_AT_ZERO
6287 /* Punt on hypothetical weird targets. */
6288 if (CLZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (TREE_TYPE (@0)),
6290 && val == TYPE_PRECISION (TREE_TYPE (@0)) - 1)
6294 (if (ok && wi::to_wide (@1) == (TYPE_PRECISION (TREE_TYPE (@0)) - 1))
6295 (op @0 { build_one_cst (TREE_TYPE (@0)); })))))))
6297 /* POPCOUNT simplifications. */
6298 /* popcount(X) + popcount(Y) is popcount(X|Y) when X&Y must be zero. */
6300 (plus (POPCOUNT:s @0) (POPCOUNT:s @1))
6301 (if (wi::bit_and (tree_nonzero_bits (@0), tree_nonzero_bits (@1)) == 0)
6302 (POPCOUNT (bit_ior @0 @1))))
6304 /* popcount(X) == 0 is X == 0, and related (in)equalities. */
6305 (for popcount (POPCOUNT)
6306 (for cmp (le eq ne gt)
6309 (cmp (popcount @0) integer_zerop)
6310 (rep @0 { build_zero_cst (TREE_TYPE (@0)); }))))
6312 /* Canonicalize POPCOUNT(x)&1 as PARITY(X). */
6314 (bit_and (POPCOUNT @0) integer_onep)
6317 /* PARITY simplifications. */
6318 /* parity(~X) is parity(X). */
6320 (PARITY (bit_not @0))
6323 /* parity(X)^parity(Y) is parity(X^Y). */
6325 (bit_xor (PARITY:s @0) (PARITY:s @1))
6326 (PARITY (bit_xor @0 @1)))
6328 /* Common POPCOUNT/PARITY simplifications. */
6329 /* popcount(X&C1) is (X>>C2)&1 when C1 == 1<<C2. Same for parity(X&C1). */
6330 (for pfun (POPCOUNT PARITY)
6333 (with { wide_int nz = tree_nonzero_bits (@0); }
6337 (if (wi::popcount (nz) == 1)
6338 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
6339 (convert (rshift:utype (convert:utype @0)
6340 { build_int_cst (integer_type_node,
6341 wi::ctz (nz)); }))))))))
6344 /* 64- and 32-bits branchless implementations of popcount are detected:
6346 int popcount64c (uint64_t x)
6348 x -= (x >> 1) & 0x5555555555555555ULL;
6349 x = (x & 0x3333333333333333ULL) + ((x >> 2) & 0x3333333333333333ULL);
6350 x = (x + (x >> 4)) & 0x0f0f0f0f0f0f0f0fULL;
6351 return (x * 0x0101010101010101ULL) >> 56;
6354 int popcount32c (uint32_t x)
6356 x -= (x >> 1) & 0x55555555;
6357 x = (x & 0x33333333) + ((x >> 2) & 0x33333333);
6358 x = (x + (x >> 4)) & 0x0f0f0f0f;
6359 return (x * 0x01010101) >> 24;
6366 (rshift @8 INTEGER_CST@5)
6368 (bit_and @6 INTEGER_CST@7)
6372 (bit_and (rshift @0 INTEGER_CST@4) INTEGER_CST@11))
6378 /* Check constants and optab. */
6379 (with { unsigned prec = TYPE_PRECISION (type);
6380 int shift = (64 - prec) & 63;
6381 unsigned HOST_WIDE_INT c1
6382 = HOST_WIDE_INT_UC (0x0101010101010101) >> shift;
6383 unsigned HOST_WIDE_INT c2
6384 = HOST_WIDE_INT_UC (0x0F0F0F0F0F0F0F0F) >> shift;
6385 unsigned HOST_WIDE_INT c3
6386 = HOST_WIDE_INT_UC (0x3333333333333333) >> shift;
6387 unsigned HOST_WIDE_INT c4
6388 = HOST_WIDE_INT_UC (0x5555555555555555) >> shift;
6393 && TYPE_UNSIGNED (type)
6394 && integer_onep (@4)
6395 && wi::to_widest (@10) == 2
6396 && wi::to_widest (@5) == 4
6397 && wi::to_widest (@1) == prec - 8
6398 && tree_to_uhwi (@2) == c1
6399 && tree_to_uhwi (@3) == c2
6400 && tree_to_uhwi (@9) == c3
6401 && tree_to_uhwi (@7) == c3
6402 && tree_to_uhwi (@11) == c4
6403 && direct_internal_fn_supported_p (IFN_POPCOUNT, type,
6405 (convert (IFN_POPCOUNT:type @0)))))
6407 /* __builtin_ffs needs to deal on many targets with the possible zero
6408 argument. If we know the argument is always non-zero, __builtin_ctz + 1
6409 should lead to better code. */
6411 (FFS tree_expr_nonzero_p@0)
6412 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6413 && direct_internal_fn_supported_p (IFN_CTZ, TREE_TYPE (@0),
6414 OPTIMIZE_FOR_SPEED))
6415 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
6416 (plus (CTZ:type (convert:utype @0)) { build_one_cst (type); }))))
6419 (for ffs (BUILT_IN_FFS BUILT_IN_FFSL BUILT_IN_FFSLL
6421 /* __builtin_ffs (X) == 0 -> X == 0.
6422 __builtin_ffs (X) == 6 -> (X & 63) == 32. */
6425 (cmp (ffs@2 @0) INTEGER_CST@1)
6426 (with { int prec = TYPE_PRECISION (TREE_TYPE (@0)); }
6428 (if (integer_zerop (@1))
6429 (cmp @0 { build_zero_cst (TREE_TYPE (@0)); }))
6430 (if (tree_int_cst_sgn (@1) < 0 || wi::to_widest (@1) > prec)
6431 { constant_boolean_node (cmp == NE_EXPR ? true : false, type); })
6432 (if (single_use (@2))
6433 (cmp (bit_and @0 { wide_int_to_tree (TREE_TYPE (@0),
6434 wi::mask (tree_to_uhwi (@1),
6436 { wide_int_to_tree (TREE_TYPE (@0),
6437 wi::shifted_mask (tree_to_uhwi (@1) - 1, 1,
6438 false, prec)); }))))))
6440 /* __builtin_ffs (X) > 6 -> X != 0 && (X & 63) == 0. */
6444 bit_op (bit_and bit_ior)
6446 (cmp (ffs@2 @0) INTEGER_CST@1)
6447 (with { int prec = TYPE_PRECISION (TREE_TYPE (@0)); }
6449 (if (integer_zerop (@1))
6450 (cmp2 @0 { build_zero_cst (TREE_TYPE (@0)); }))
6451 (if (tree_int_cst_sgn (@1) < 0)
6452 { constant_boolean_node (cmp == GT_EXPR ? true : false, type); })
6453 (if (wi::to_widest (@1) >= prec)
6454 { constant_boolean_node (cmp == GT_EXPR ? false : true, type); })
6455 (if (wi::to_widest (@1) == prec - 1)
6456 (cmp3 @0 { wide_int_to_tree (TREE_TYPE (@0),
6457 wi::shifted_mask (prec - 1, 1,
6459 (if (single_use (@2))
6460 (bit_op (cmp2 @0 { build_zero_cst (TREE_TYPE (@0)); })
6462 { wide_int_to_tree (TREE_TYPE (@0),
6463 wi::mask (tree_to_uhwi (@1),
6465 { build_zero_cst (TREE_TYPE (@0)); }))))))))
6474 r = c ? a1 op a2 : b;
6476 if the target can do it in one go. This makes the operation conditional
6477 on c, so could drop potentially-trapping arithmetic, but that's a valid
6478 simplification if the result of the operation isn't needed.
6480 Avoid speculatively generating a stand-alone vector comparison
6481 on targets that might not support them. Any target implementing
6482 conditional internal functions must support the same comparisons
6483 inside and outside a VEC_COND_EXPR. */
6486 (for uncond_op (UNCOND_BINARY)
6487 cond_op (COND_BINARY)
6489 (vec_cond @0 (view_convert? (uncond_op@4 @1 @2)) @3)
6490 (with { tree op_type = TREE_TYPE (@4); }
6491 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
6492 && element_precision (type) == element_precision (op_type))
6493 (view_convert (cond_op @0 @1 @2 (view_convert:op_type @3))))))
6495 (vec_cond @0 @1 (view_convert? (uncond_op@4 @2 @3)))
6496 (with { tree op_type = TREE_TYPE (@4); }
6497 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
6498 && element_precision (type) == element_precision (op_type))
6499 (view_convert (cond_op (bit_not @0) @2 @3 (view_convert:op_type @1)))))))
6501 /* Same for ternary operations. */
6502 (for uncond_op (UNCOND_TERNARY)
6503 cond_op (COND_TERNARY)
6505 (vec_cond @0 (view_convert? (uncond_op@5 @1 @2 @3)) @4)
6506 (with { tree op_type = TREE_TYPE (@5); }
6507 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
6508 && element_precision (type) == element_precision (op_type))
6509 (view_convert (cond_op @0 @1 @2 @3 (view_convert:op_type @4))))))
6511 (vec_cond @0 @1 (view_convert? (uncond_op@5 @2 @3 @4)))
6512 (with { tree op_type = TREE_TYPE (@5); }
6513 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
6514 && element_precision (type) == element_precision (op_type))
6515 (view_convert (cond_op (bit_not @0) @2 @3 @4
6516 (view_convert:op_type @1)))))))
6519 /* Detect cases in which a VEC_COND_EXPR effectively replaces the
6520 "else" value of an IFN_COND_*. */
6521 (for cond_op (COND_BINARY)
6523 (vec_cond @0 (view_convert? (cond_op @0 @1 @2 @3)) @4)
6524 (with { tree op_type = TREE_TYPE (@3); }
6525 (if (element_precision (type) == element_precision (op_type))
6526 (view_convert (cond_op @0 @1 @2 (view_convert:op_type @4))))))
6528 (vec_cond @0 @1 (view_convert? (cond_op @2 @3 @4 @5)))
6529 (with { tree op_type = TREE_TYPE (@5); }
6530 (if (inverse_conditions_p (@0, @2)
6531 && element_precision (type) == element_precision (op_type))
6532 (view_convert (cond_op @2 @3 @4 (view_convert:op_type @1)))))))
6534 /* Same for ternary operations. */
6535 (for cond_op (COND_TERNARY)
6537 (vec_cond @0 (view_convert? (cond_op @0 @1 @2 @3 @4)) @5)
6538 (with { tree op_type = TREE_TYPE (@4); }
6539 (if (element_precision (type) == element_precision (op_type))
6540 (view_convert (cond_op @0 @1 @2 @3 (view_convert:op_type @5))))))
6542 (vec_cond @0 @1 (view_convert? (cond_op @2 @3 @4 @5 @6)))
6543 (with { tree op_type = TREE_TYPE (@6); }
6544 (if (inverse_conditions_p (@0, @2)
6545 && element_precision (type) == element_precision (op_type))
6546 (view_convert (cond_op @2 @3 @4 @5 (view_convert:op_type @1)))))))
6548 /* For pointers @0 and @2 and nonnegative constant offset @1, look for
6551 A: (@0 + @1 < @2) | (@2 + @1 < @0)
6552 B: (@0 + @1 <= @2) | (@2 + @1 <= @0)
6554 If pointers are known not to wrap, B checks whether @1 bytes starting
6555 at @0 and @2 do not overlap, while A tests the same thing for @1 + 1
6556 bytes. A is more efficiently tested as:
6558 A: (sizetype) (@0 + @1 - @2) > @1 * 2
6560 The equivalent expression for B is given by replacing @1 with @1 - 1:
6562 B: (sizetype) (@0 + (@1 - 1) - @2) > (@1 - 1) * 2
6564 @0 and @2 can be swapped in both expressions without changing the result.
6566 The folds rely on sizetype's being unsigned (which is always true)
6567 and on its being the same width as the pointer (which we have to check).
6569 The fold replaces two pointer_plus expressions, two comparisons and
6570 an IOR with a pointer_plus, a pointer_diff, and a comparison, so in
6571 the best case it's a saving of two operations. The A fold retains one
6572 of the original pointer_pluses, so is a win even if both pointer_pluses
6573 are used elsewhere. The B fold is a wash if both pointer_pluses are
6574 used elsewhere, since all we end up doing is replacing a comparison with
6575 a pointer_plus. We do still apply the fold under those circumstances
6576 though, in case applying it to other conditions eventually makes one of the
6577 pointer_pluses dead. */
6578 (for ior (truth_orif truth_or bit_ior)
6581 (ior (cmp:cs (pointer_plus@3 @0 INTEGER_CST@1) @2)
6582 (cmp:cs (pointer_plus@4 @2 @1) @0))
6583 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
6584 && TYPE_OVERFLOW_WRAPS (sizetype)
6585 && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (sizetype))
6586 /* Calculate the rhs constant. */
6587 (with { offset_int off = wi::to_offset (@1) - (cmp == LE_EXPR ? 1 : 0);
6588 offset_int rhs = off * 2; }
6589 /* Always fails for negative values. */
6590 (if (wi::min_precision (rhs, UNSIGNED) <= TYPE_PRECISION (sizetype))
6591 /* Since the order of @0 and @2 doesn't matter, let tree_swap_operands_p
6592 pick a canonical order. This increases the chances of using the
6593 same pointer_plus in multiple checks. */
6594 (with { bool swap_p = tree_swap_operands_p (@0, @2);
6595 tree rhs_tree = wide_int_to_tree (sizetype, rhs); }
6596 (if (cmp == LT_EXPR)
6597 (gt (convert:sizetype
6598 (pointer_diff:ssizetype { swap_p ? @4 : @3; }
6599 { swap_p ? @0 : @2; }))
6601 (gt (convert:sizetype
6602 (pointer_diff:ssizetype
6603 (pointer_plus { swap_p ? @2 : @0; }
6604 { wide_int_to_tree (sizetype, off); })
6605 { swap_p ? @0 : @2; }))
6606 { rhs_tree; })))))))))
6608 /* Fold REDUC (@0 & @1) -> @0[I] & @1[I] if element I is the only nonzero
6610 (for reduc (IFN_REDUC_PLUS IFN_REDUC_IOR IFN_REDUC_XOR)
6611 (simplify (reduc (view_convert? (bit_and @0 VECTOR_CST@1)))
6612 (with { int i = single_nonzero_element (@1); }
6614 (with { tree elt = vector_cst_elt (@1, i);
6615 tree elt_type = TREE_TYPE (elt);
6616 unsigned int elt_bits = tree_to_uhwi (TYPE_SIZE (elt_type));
6617 tree size = bitsize_int (elt_bits);
6618 tree pos = bitsize_int (elt_bits * i); }
6621 (BIT_FIELD_REF:elt_type @0 { size; } { pos; })
6625 (vec_perm @0 @1 VECTOR_CST@2)
6628 tree op0 = @0, op1 = @1, op2 = @2;
6630 /* Build a vector of integers from the tree mask. */
6631 vec_perm_builder builder;
6632 if (!tree_to_vec_perm_builder (&builder, op2))
6635 /* Create a vec_perm_indices for the integer vector. */
6636 poly_uint64 nelts = TYPE_VECTOR_SUBPARTS (type);
6637 bool single_arg = (op0 == op1);
6638 vec_perm_indices sel (builder, single_arg ? 1 : 2, nelts);
6640 (if (sel.series_p (0, 1, 0, 1))
6642 (if (sel.series_p (0, 1, nelts, 1))
6648 if (sel.all_from_input_p (0))
6650 else if (sel.all_from_input_p (1))
6653 sel.rotate_inputs (1);
6655 else if (known_ge (poly_uint64 (sel[0]), nelts))
6657 std::swap (op0, op1);
6658 sel.rotate_inputs (1);
6662 tree cop0 = op0, cop1 = op1;
6663 if (TREE_CODE (op0) == SSA_NAME
6664 && (def = dyn_cast <gassign *> (SSA_NAME_DEF_STMT (op0)))
6665 && gimple_assign_rhs_code (def) == CONSTRUCTOR)
6666 cop0 = gimple_assign_rhs1 (def);
6667 if (TREE_CODE (op1) == SSA_NAME
6668 && (def = dyn_cast <gassign *> (SSA_NAME_DEF_STMT (op1)))
6669 && gimple_assign_rhs_code (def) == CONSTRUCTOR)
6670 cop1 = gimple_assign_rhs1 (def);
6674 (if ((TREE_CODE (cop0) == VECTOR_CST
6675 || TREE_CODE (cop0) == CONSTRUCTOR)
6676 && (TREE_CODE (cop1) == VECTOR_CST
6677 || TREE_CODE (cop1) == CONSTRUCTOR)
6678 && (t = fold_vec_perm (type, cop0, cop1, sel)))
6682 bool changed = (op0 == op1 && !single_arg);
6683 tree ins = NULL_TREE;
6686 /* See if the permutation is performing a single element
6687 insert from a CONSTRUCTOR or constant and use a BIT_INSERT_EXPR
6688 in that case. But only if the vector mode is supported,
6689 otherwise this is invalid GIMPLE. */
6690 if (TYPE_MODE (type) != BLKmode
6691 && (TREE_CODE (cop0) == VECTOR_CST
6692 || TREE_CODE (cop0) == CONSTRUCTOR
6693 || TREE_CODE (cop1) == VECTOR_CST
6694 || TREE_CODE (cop1) == CONSTRUCTOR))
6696 bool insert_first_p = sel.series_p (1, 1, nelts + 1, 1);
6699 /* After canonicalizing the first elt to come from the
6700 first vector we only can insert the first elt from
6701 the first vector. */
6703 if ((ins = fold_read_from_vector (cop0, sel[0])))
6706 /* The above can fail for two-element vectors which always
6707 appear to insert the first element, so try inserting
6708 into the second lane as well. For more than two
6709 elements that's wasted time. */
6710 if (!insert_first_p || (!ins && maybe_eq (nelts, 2u)))
6712 unsigned int encoded_nelts = sel.encoding ().encoded_nelts ();
6713 for (at = 0; at < encoded_nelts; ++at)
6714 if (maybe_ne (sel[at], at))
6716 if (at < encoded_nelts
6717 && (known_eq (at + 1, nelts)
6718 || sel.series_p (at + 1, 1, at + 1, 1)))
6720 if (known_lt (poly_uint64 (sel[at]), nelts))
6721 ins = fold_read_from_vector (cop0, sel[at]);
6723 ins = fold_read_from_vector (cop1, sel[at] - nelts);
6728 /* Generate a canonical form of the selector. */
6729 if (!ins && sel.encoding () != builder)
6731 /* Some targets are deficient and fail to expand a single
6732 argument permutation while still allowing an equivalent
6733 2-argument version. */
6735 if (sel.ninputs () == 2
6736 || can_vec_perm_const_p (TYPE_MODE (type), sel, false))
6737 op2 = vec_perm_indices_to_tree (TREE_TYPE (op2), sel);
6740 vec_perm_indices sel2 (builder, 2, nelts);
6741 if (can_vec_perm_const_p (TYPE_MODE (type), sel2, false))
6742 op2 = vec_perm_indices_to_tree (TREE_TYPE (op2), sel2);
6744 /* Not directly supported with either encoding,
6745 so use the preferred form. */
6746 op2 = vec_perm_indices_to_tree (TREE_TYPE (op2), sel);
6748 if (!operand_equal_p (op2, oldop2, 0))
6753 (bit_insert { op0; } { ins; }
6754 { bitsize_int (at * vector_element_bits (type)); })
6756 (vec_perm { op0; } { op1; } { op2; }))))))))))
6758 /* VEC_PERM_EXPR (v, v, mask) -> v where v contains same element. */
6760 (match vec_same_elem_p
6762 (if (uniform_vector_p (@0))))
6764 (match vec_same_elem_p
6768 (vec_perm vec_same_elem_p@0 @0 @1)
6771 /* Match count trailing zeroes for simplify_count_trailing_zeroes in fwprop.
6772 The canonical form is array[((x & -x) * C) >> SHIFT] where C is a magic
6773 constant which when multiplied by a power of 2 contains a unique value
6774 in the top 5 or 6 bits. This is then indexed into a table which maps it
6775 to the number of trailing zeroes. */
6776 (match (ctz_table_index @1 @2 @3)
6777 (rshift (mult (bit_and:c (negate @1) @1) INTEGER_CST@2) INTEGER_CST@3))