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
41 expand_vec_cmp_expr_p)
44 (define_operator_list tcc_comparison
45 lt le eq ne ge gt unordered ordered unlt unle ungt unge uneq ltgt)
46 (define_operator_list inverted_tcc_comparison
47 ge gt ne eq lt le ordered unordered ge gt le lt ltgt uneq)
48 (define_operator_list inverted_tcc_comparison_with_nans
49 unge ungt ne eq unlt unle ordered unordered ge gt le lt ltgt uneq)
50 (define_operator_list swapped_tcc_comparison
51 gt ge eq ne le lt unordered ordered ungt unge unlt unle uneq ltgt)
52 (define_operator_list simple_comparison lt le eq ne ge gt)
53 (define_operator_list swapped_simple_comparison gt ge eq ne le lt)
55 #include "cfn-operators.pd"
57 /* Define operand lists for math rounding functions {,i,l,ll}FN,
58 where the versions prefixed with "i" return an int, those prefixed with
59 "l" return a long and those prefixed with "ll" return a long long.
61 Also define operand lists:
63 X<FN>F for all float functions, in the order i, l, ll
64 X<FN> for all double functions, in the same order
65 X<FN>L for all long double functions, in the same order. */
66 #define DEFINE_INT_AND_FLOAT_ROUND_FN(FN) \
67 (define_operator_list X##FN##F BUILT_IN_I##FN##F \
70 (define_operator_list X##FN BUILT_IN_I##FN \
73 (define_operator_list X##FN##L BUILT_IN_I##FN##L \
77 DEFINE_INT_AND_FLOAT_ROUND_FN (FLOOR)
78 DEFINE_INT_AND_FLOAT_ROUND_FN (CEIL)
79 DEFINE_INT_AND_FLOAT_ROUND_FN (ROUND)
80 DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
82 /* Unary operations and their associated IFN_COND_* function. */
83 (define_operator_list UNCOND_UNARY
85 (define_operator_list COND_UNARY
88 /* Binary operations and their associated IFN_COND_* function. */
89 (define_operator_list UNCOND_BINARY
91 mult trunc_div trunc_mod rdiv
94 bit_and bit_ior bit_xor
96 (define_operator_list COND_BINARY
97 IFN_COND_ADD IFN_COND_SUB
98 IFN_COND_MUL IFN_COND_DIV IFN_COND_MOD IFN_COND_RDIV
99 IFN_COND_MIN IFN_COND_MAX
100 IFN_COND_FMIN IFN_COND_FMAX
101 IFN_COND_AND IFN_COND_IOR IFN_COND_XOR
102 IFN_COND_SHL IFN_COND_SHR)
104 /* Same for ternary operations. */
105 (define_operator_list UNCOND_TERNARY
106 IFN_FMA IFN_FMS IFN_FNMA IFN_FNMS)
107 (define_operator_list COND_TERNARY
108 IFN_COND_FMA IFN_COND_FMS IFN_COND_FNMA IFN_COND_FNMS)
110 /* __atomic_fetch_or_*, __atomic_fetch_xor_*, __atomic_xor_fetch_* */
111 (define_operator_list ATOMIC_FETCH_OR_XOR_N
112 BUILT_IN_ATOMIC_FETCH_OR_1 BUILT_IN_ATOMIC_FETCH_OR_2
113 BUILT_IN_ATOMIC_FETCH_OR_4 BUILT_IN_ATOMIC_FETCH_OR_8
114 BUILT_IN_ATOMIC_FETCH_OR_16
115 BUILT_IN_ATOMIC_FETCH_XOR_1 BUILT_IN_ATOMIC_FETCH_XOR_2
116 BUILT_IN_ATOMIC_FETCH_XOR_4 BUILT_IN_ATOMIC_FETCH_XOR_8
117 BUILT_IN_ATOMIC_FETCH_XOR_16
118 BUILT_IN_ATOMIC_XOR_FETCH_1 BUILT_IN_ATOMIC_XOR_FETCH_2
119 BUILT_IN_ATOMIC_XOR_FETCH_4 BUILT_IN_ATOMIC_XOR_FETCH_8
120 BUILT_IN_ATOMIC_XOR_FETCH_16)
121 /* __sync_fetch_and_or_*, __sync_fetch_and_xor_*, __sync_xor_and_fetch_* */
122 (define_operator_list SYNC_FETCH_OR_XOR_N
123 BUILT_IN_SYNC_FETCH_AND_OR_1 BUILT_IN_SYNC_FETCH_AND_OR_2
124 BUILT_IN_SYNC_FETCH_AND_OR_4 BUILT_IN_SYNC_FETCH_AND_OR_8
125 BUILT_IN_SYNC_FETCH_AND_OR_16
126 BUILT_IN_SYNC_FETCH_AND_XOR_1 BUILT_IN_SYNC_FETCH_AND_XOR_2
127 BUILT_IN_SYNC_FETCH_AND_XOR_4 BUILT_IN_SYNC_FETCH_AND_XOR_8
128 BUILT_IN_SYNC_FETCH_AND_XOR_16
129 BUILT_IN_SYNC_XOR_AND_FETCH_1 BUILT_IN_SYNC_XOR_AND_FETCH_2
130 BUILT_IN_SYNC_XOR_AND_FETCH_4 BUILT_IN_SYNC_XOR_AND_FETCH_8
131 BUILT_IN_SYNC_XOR_AND_FETCH_16)
132 /* __atomic_fetch_and_*. */
133 (define_operator_list ATOMIC_FETCH_AND_N
134 BUILT_IN_ATOMIC_FETCH_AND_1 BUILT_IN_ATOMIC_FETCH_AND_2
135 BUILT_IN_ATOMIC_FETCH_AND_4 BUILT_IN_ATOMIC_FETCH_AND_8
136 BUILT_IN_ATOMIC_FETCH_AND_16)
137 /* __sync_fetch_and_and_*. */
138 (define_operator_list SYNC_FETCH_AND_AND_N
139 BUILT_IN_SYNC_FETCH_AND_AND_1 BUILT_IN_SYNC_FETCH_AND_AND_2
140 BUILT_IN_SYNC_FETCH_AND_AND_4 BUILT_IN_SYNC_FETCH_AND_AND_8
141 BUILT_IN_SYNC_FETCH_AND_AND_16)
143 /* With nop_convert? combine convert? and view_convert? in one pattern
144 plus conditionalize on tree_nop_conversion_p conversions. */
145 (match (nop_convert @0)
147 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))))
148 (match (nop_convert @0)
150 (if (VECTOR_TYPE_P (type) && VECTOR_TYPE_P (TREE_TYPE (@0))
151 && known_eq (TYPE_VECTOR_SUBPARTS (type),
152 TYPE_VECTOR_SUBPARTS (TREE_TYPE (@0)))
153 && tree_nop_conversion_p (TREE_TYPE (type), TREE_TYPE (TREE_TYPE (@0))))))
155 /* Transform likes of (char) ABS_EXPR <(int) x> into (char) ABSU_EXPR <x>
156 ABSU_EXPR returns unsigned absolute value of the operand and the operand
157 of the ABSU_EXPR will have the corresponding signed type. */
158 (simplify (abs (convert @0))
159 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
160 && !TYPE_UNSIGNED (TREE_TYPE (@0))
161 && element_precision (type) > element_precision (TREE_TYPE (@0)))
162 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
163 (convert (absu:utype @0)))))
166 /* Optimize (X + (X >> (prec - 1))) ^ (X >> (prec - 1)) into abs (X). */
168 (bit_xor:c (plus:c @0 (rshift@2 @0 INTEGER_CST@1)) @2)
169 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
170 && !TYPE_UNSIGNED (TREE_TYPE (@0))
171 && wi::to_widest (@1) == element_precision (TREE_TYPE (@0)) - 1)
175 /* Simplifications of operations with one constant operand and
176 simplifications to constants or single values. */
178 (for op (plus pointer_plus minus bit_ior bit_xor)
180 (op @0 integer_zerop)
183 /* 0 +p index -> (type)index */
185 (pointer_plus integer_zerop @1)
186 (non_lvalue (convert @1)))
188 /* ptr - 0 -> (type)ptr */
190 (pointer_diff @0 integer_zerop)
193 /* See if ARG1 is zero and X + ARG1 reduces to X.
194 Likewise if the operands are reversed. */
196 (plus:c @0 real_zerop@1)
197 (if (fold_real_zero_addition_p (type, @0, @1, 0))
200 /* See if ARG1 is zero and X - ARG1 reduces to X. */
202 (minus @0 real_zerop@1)
203 (if (fold_real_zero_addition_p (type, @0, @1, 1))
206 /* Even if the fold_real_zero_addition_p can't simplify X + 0.0
207 into X, we can optimize (X + 0.0) + 0.0 or (X + 0.0) - 0.0
208 or (X - 0.0) + 0.0 into X + 0.0 and (X - 0.0) - 0.0 into X - 0.0
209 if not -frounding-math. For sNaNs the first operation would raise
210 exceptions but turn the result into qNan, so the second operation
211 would not raise it. */
212 (for inner_op (plus minus)
213 (for outer_op (plus minus)
215 (outer_op (inner_op@3 @0 REAL_CST@1) REAL_CST@2)
218 && !HONOR_SIGN_DEPENDENT_ROUNDING (type))
219 (with { bool inner_plus = ((inner_op == PLUS_EXPR)
220 ^ REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@1)));
222 = ((outer_op == PLUS_EXPR)
223 ^ REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@2))); }
224 (if (outer_plus && !inner_plus)
229 This is unsafe for certain floats even in non-IEEE formats.
230 In IEEE, it is unsafe because it does wrong for NaNs.
231 Also note that operand_equal_p is always false if an operand
235 (if (!FLOAT_TYPE_P (type) || !tree_expr_maybe_nan_p (@0))
236 { build_zero_cst (type); }))
238 (pointer_diff @@0 @0)
239 { build_zero_cst (type); })
242 (mult @0 integer_zerop@1)
245 /* -x == x -> x == 0 */
248 (cmp:c @0 (negate @0))
249 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
250 && !TYPE_OVERFLOW_WRAPS (TREE_TYPE(@0)))
251 (cmp @0 { build_zero_cst (TREE_TYPE(@0)); }))))
253 /* Maybe fold x * 0 to 0. The expressions aren't the same
254 when x is NaN, since x * 0 is also NaN. Nor are they the
255 same in modes with signed zeros, since multiplying a
256 negative value by 0 gives -0, not +0. */
258 (mult @0 real_zerop@1)
259 (if (!tree_expr_maybe_nan_p (@0)
260 && !tree_expr_maybe_real_minus_zero_p (@0)
261 && !tree_expr_maybe_real_minus_zero_p (@1))
264 /* In IEEE floating point, x*1 is not equivalent to x for snans.
265 Likewise for complex arithmetic with signed zeros. */
268 (if (!tree_expr_maybe_signaling_nan_p (@0)
269 && (!HONOR_SIGNED_ZEROS (type)
270 || !COMPLEX_FLOAT_TYPE_P (type)))
273 /* Transform x * -1.0 into -x. */
275 (mult @0 real_minus_onep)
276 (if (!tree_expr_maybe_signaling_nan_p (@0)
277 && (!HONOR_SIGNED_ZEROS (type)
278 || !COMPLEX_FLOAT_TYPE_P (type)))
281 /* Transform { 0 or 1 } * { 0 or 1 } into { 0 or 1 } & { 0 or 1 } */
283 (mult SSA_NAME@1 SSA_NAME@2)
284 (if (INTEGRAL_TYPE_P (type)
285 && get_nonzero_bits (@1) == 1
286 && get_nonzero_bits (@2) == 1)
289 /* Transform x * { 0 or 1, 0 or 1, ... } into x & { 0 or -1, 0 or -1, ...},
290 unless the target has native support for the former but not the latter. */
292 (mult @0 VECTOR_CST@1)
293 (if (initializer_each_zero_or_onep (@1)
294 && !HONOR_SNANS (type)
295 && !HONOR_SIGNED_ZEROS (type))
296 (with { tree itype = FLOAT_TYPE_P (type) ? unsigned_type_for (type) : type; }
298 && (!VECTOR_MODE_P (TYPE_MODE (type))
299 || (VECTOR_MODE_P (TYPE_MODE (itype))
300 && optab_handler (and_optab,
301 TYPE_MODE (itype)) != CODE_FOR_nothing)))
302 (view_convert (bit_and:itype (view_convert @0)
303 (ne @1 { build_zero_cst (type); })))))))
305 (for cmp (gt ge lt le)
306 outp (convert convert negate negate)
307 outn (negate negate convert convert)
308 /* Transform X * (X > 0.0 ? 1.0 : -1.0) into abs(X). */
309 /* Transform X * (X >= 0.0 ? 1.0 : -1.0) into abs(X). */
310 /* Transform X * (X < 0.0 ? 1.0 : -1.0) into -abs(X). */
311 /* Transform X * (X <= 0.0 ? 1.0 : -1.0) into -abs(X). */
313 (mult:c @0 (cond (cmp @0 real_zerop) real_onep@1 real_minus_onep))
314 (if (!tree_expr_maybe_nan_p (@0) && !HONOR_SIGNED_ZEROS (type))
316 /* Transform X * (X > 0.0 ? -1.0 : 1.0) into -abs(X). */
317 /* Transform X * (X >= 0.0 ? -1.0 : 1.0) into -abs(X). */
318 /* Transform X * (X < 0.0 ? -1.0 : 1.0) into abs(X). */
319 /* Transform X * (X <= 0.0 ? -1.0 : 1.0) into abs(X). */
321 (mult:c @0 (cond (cmp @0 real_zerop) real_minus_onep real_onep@1))
322 (if (!tree_expr_maybe_nan_p (@0) && !HONOR_SIGNED_ZEROS (type))
325 /* Transform X * copysign (1.0, X) into abs(X). */
327 (mult:c @0 (COPYSIGN_ALL real_onep @0))
328 (if (!tree_expr_maybe_nan_p (@0) && !HONOR_SIGNED_ZEROS (type))
331 /* Transform X * copysign (1.0, -X) into -abs(X). */
333 (mult:c @0 (COPYSIGN_ALL real_onep (negate @0)))
334 (if (!tree_expr_maybe_nan_p (@0) && !HONOR_SIGNED_ZEROS (type))
337 /* Transform copysign (CST, X) into copysign (ABS(CST), X). */
339 (COPYSIGN_ALL REAL_CST@0 @1)
340 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@0)))
341 (COPYSIGN_ALL (negate @0) @1)))
343 /* X * 1, X / 1 -> X. */
344 (for op (mult trunc_div ceil_div floor_div round_div exact_div)
349 /* (A / (1 << B)) -> (A >> B).
350 Only for unsigned A. For signed A, this would not preserve rounding
352 For example: (-1 / ( 1 << B)) != -1 >> B.
353 Also also widening conversions, like:
354 (A / (unsigned long long) (1U << B)) -> (A >> B)
356 (A / (unsigned long long) (1 << B)) -> (A >> B).
357 If the left shift is signed, it can be done only if the upper bits
358 of A starting from shift's type sign bit are zero, as
359 (unsigned long long) (1 << 31) is -2147483648ULL, not 2147483648ULL,
360 so it is valid only if A >> 31 is zero. */
362 (trunc_div (convert?@0 @3) (convert2? (lshift integer_onep@1 @2)))
363 (if ((TYPE_UNSIGNED (type) || tree_expr_nonnegative_p (@0))
364 && (!VECTOR_TYPE_P (type)
365 || target_supports_op_p (type, RSHIFT_EXPR, optab_vector)
366 || target_supports_op_p (type, RSHIFT_EXPR, optab_scalar))
367 && (useless_type_conversion_p (type, TREE_TYPE (@1))
368 || (element_precision (type) >= element_precision (TREE_TYPE (@1))
369 && (TYPE_UNSIGNED (TREE_TYPE (@1))
370 || (element_precision (type)
371 == element_precision (TREE_TYPE (@1)))
372 || (INTEGRAL_TYPE_P (type)
373 && (tree_nonzero_bits (@0)
374 & wi::mask (element_precision (TREE_TYPE (@1)) - 1,
376 element_precision (type))) == 0)))))
377 (if (!VECTOR_TYPE_P (type)
378 && useless_type_conversion_p (TREE_TYPE (@3), TREE_TYPE (@1))
379 && element_precision (TREE_TYPE (@3)) < element_precision (type))
380 (convert (rshift @3 @2))
383 /* Preserve explicit divisions by 0: the C++ front-end wants to detect
384 undefined behavior in constexpr evaluation, and assuming that the division
385 traps enables better optimizations than these anyway. */
386 (for div (trunc_div ceil_div floor_div round_div exact_div)
387 /* 0 / X is always zero. */
389 (div integer_zerop@0 @1)
390 /* But not for 0 / 0 so that we can get the proper warnings and errors. */
391 (if (!integer_zerop (@1))
395 (div @0 integer_minus_onep@1)
396 (if (!TYPE_UNSIGNED (type))
398 /* X / bool_range_Y is X. */
401 (if (INTEGRAL_TYPE_P (type) && ssa_name_has_boolean_range (@1))
406 /* But not for 0 / 0 so that we can get the proper warnings and errors.
407 And not for _Fract types where we can't build 1. */
408 (if (!integer_zerop (@0) && !ALL_FRACT_MODE_P (TYPE_MODE (type)))
409 { build_one_cst (type); }))
410 /* X / abs (X) is X < 0 ? -1 : 1. */
413 (if (INTEGRAL_TYPE_P (type)
414 && TYPE_OVERFLOW_UNDEFINED (type))
415 (cond (lt @0 { build_zero_cst (type); })
416 { build_minus_one_cst (type); } { build_one_cst (type); })))
419 (div:C @0 (negate @0))
420 (if ((INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
421 && TYPE_OVERFLOW_UNDEFINED (type))
422 { build_minus_one_cst (type); })))
424 /* For unsigned integral types, FLOOR_DIV_EXPR is the same as
425 TRUNC_DIV_EXPR. Rewrite into the latter in this case. */
428 (if ((INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
429 && TYPE_UNSIGNED (type))
432 /* Combine two successive divisions. Note that combining ceil_div
433 and floor_div is trickier and combining round_div even more so. */
434 (for div (trunc_div exact_div)
436 (div (div@3 @0 INTEGER_CST@1) INTEGER_CST@2)
438 wi::overflow_type overflow;
439 wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@2),
440 TYPE_SIGN (type), &overflow);
442 (if (div == EXACT_DIV_EXPR
443 || optimize_successive_divisions_p (@2, @3))
445 (div @0 { wide_int_to_tree (type, mul); })
446 (if (TYPE_UNSIGNED (type)
447 || mul != wi::min_value (TYPE_PRECISION (type), SIGNED))
448 { build_zero_cst (type); }))))))
450 /* Combine successive multiplications. Similar to above, but handling
451 overflow is different. */
453 (mult (mult @0 INTEGER_CST@1) INTEGER_CST@2)
455 wi::overflow_type overflow;
456 wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@2),
457 TYPE_SIGN (type), &overflow);
459 /* Skip folding on overflow: the only special case is @1 * @2 == -INT_MIN,
460 otherwise undefined overflow implies that @0 must be zero. */
461 (if (!overflow || TYPE_OVERFLOW_WRAPS (type))
462 (mult @0 { wide_int_to_tree (type, mul); }))))
464 /* Optimize A / A to 1.0 if we don't care about
465 NaNs or Infinities. */
468 (if (FLOAT_TYPE_P (type)
469 && ! HONOR_NANS (type)
470 && ! HONOR_INFINITIES (type))
471 { build_one_cst (type); }))
473 /* Optimize -A / A to -1.0 if we don't care about
474 NaNs or Infinities. */
476 (rdiv:C @0 (negate @0))
477 (if (FLOAT_TYPE_P (type)
478 && ! HONOR_NANS (type)
479 && ! HONOR_INFINITIES (type))
480 { build_minus_one_cst (type); }))
482 /* PR71078: x / abs(x) -> copysign (1.0, x) */
484 (rdiv:C (convert? @0) (convert? (abs @0)))
485 (if (SCALAR_FLOAT_TYPE_P (type)
486 && ! HONOR_NANS (type)
487 && ! HONOR_INFINITIES (type))
489 (if (types_match (type, float_type_node))
490 (BUILT_IN_COPYSIGNF { build_one_cst (type); } (convert @0)))
491 (if (types_match (type, double_type_node))
492 (BUILT_IN_COPYSIGN { build_one_cst (type); } (convert @0)))
493 (if (types_match (type, long_double_type_node))
494 (BUILT_IN_COPYSIGNL { build_one_cst (type); } (convert @0))))))
496 /* In IEEE floating point, x/1 is not equivalent to x for snans. */
499 (if (!tree_expr_maybe_signaling_nan_p (@0))
502 /* In IEEE floating point, x/-1 is not equivalent to -x for snans. */
504 (rdiv @0 real_minus_onep)
505 (if (!tree_expr_maybe_signaling_nan_p (@0))
508 (if (flag_reciprocal_math)
509 /* Convert (A/B)/C to A/(B*C). */
511 (rdiv (rdiv:s @0 @1) @2)
512 (rdiv @0 (mult @1 @2)))
514 /* Canonicalize x / (C1 * y) to (x * C2) / y. */
516 (rdiv @0 (mult:s @1 REAL_CST@2))
518 { tree tem = const_binop (RDIV_EXPR, type, build_one_cst (type), @2); }
520 (rdiv (mult @0 { tem; } ) @1))))
522 /* Convert A/(B/C) to (A/B)*C */
524 (rdiv @0 (rdiv:s @1 @2))
525 (mult (rdiv @0 @1) @2)))
527 /* Simplify x / (- y) to -x / y. */
529 (rdiv @0 (negate @1))
530 (rdiv (negate @0) @1))
532 (if (flag_unsafe_math_optimizations)
533 /* Simplify (C / x op 0.0) to x op 0.0 for C != 0, C != Inf/Nan.
534 Since C / x may underflow to zero, do this only for unsafe math. */
535 (for op (lt le gt ge)
538 (op (rdiv REAL_CST@0 @1) real_zerop@2)
539 (if (!HONOR_SIGNED_ZEROS (@1) && !HONOR_INFINITIES (@1))
541 (if (real_less (&dconst0, TREE_REAL_CST_PTR (@0)))
543 /* For C < 0, use the inverted operator. */
544 (if (real_less (TREE_REAL_CST_PTR (@0), &dconst0))
547 /* Optimize (X & (-A)) / A where A is a power of 2, to X >> log2(A) */
548 (for div (trunc_div ceil_div floor_div round_div exact_div)
550 (div (convert? (bit_and @0 INTEGER_CST@1)) INTEGER_CST@2)
551 (if (integer_pow2p (@2)
552 && tree_int_cst_sgn (@2) > 0
553 && tree_nop_conversion_p (type, TREE_TYPE (@0))
554 && wi::to_wide (@2) + wi::to_wide (@1) == 0)
556 { build_int_cst (integer_type_node,
557 wi::exact_log2 (wi::to_wide (@2))); }))))
559 /* If ARG1 is a constant, we can convert this to a multiply by the
560 reciprocal. This does not have the same rounding properties,
561 so only do this if -freciprocal-math. We can actually
562 always safely do it if ARG1 is a power of two, but it's hard to
563 tell if it is or not in a portable manner. */
564 (for cst (REAL_CST COMPLEX_CST VECTOR_CST)
568 (if (flag_reciprocal_math
571 { tree tem = const_binop (RDIV_EXPR, type, build_one_cst (type), @1); }
573 (mult @0 { tem; } )))
574 (if (cst != COMPLEX_CST)
575 (with { tree inverse = exact_inverse (type, @1); }
577 (mult @0 { inverse; } ))))))))
579 (for mod (ceil_mod floor_mod round_mod trunc_mod)
580 /* 0 % X is always zero. */
582 (mod integer_zerop@0 @1)
583 /* But not for 0 % 0 so that we can get the proper warnings and errors. */
584 (if (!integer_zerop (@1))
586 /* X % 1 is always zero. */
588 (mod @0 integer_onep)
589 { build_zero_cst (type); })
590 /* X % -1 is zero. */
592 (mod @0 integer_minus_onep@1)
593 (if (!TYPE_UNSIGNED (type))
594 { build_zero_cst (type); }))
598 /* But not for 0 % 0 so that we can get the proper warnings and errors. */
599 (if (!integer_zerop (@0))
600 { build_zero_cst (type); }))
601 /* (X % Y) % Y is just X % Y. */
603 (mod (mod@2 @0 @1) @1)
605 /* From extract_muldiv_1: (X * C1) % C2 is zero if C1 is a multiple of C2. */
607 (mod (mult @0 INTEGER_CST@1) INTEGER_CST@2)
608 (if (ANY_INTEGRAL_TYPE_P (type)
609 && TYPE_OVERFLOW_UNDEFINED (type)
610 && wi::multiple_of_p (wi::to_wide (@1), wi::to_wide (@2),
612 { build_zero_cst (type); }))
613 /* For (X % C) == 0, if X is signed and C is power of 2, use unsigned
614 modulo and comparison, since it is simpler and equivalent. */
617 (cmp (mod @0 integer_pow2p@2) integer_zerop@1)
618 (if (!TYPE_UNSIGNED (TREE_TYPE (@0)))
619 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
620 (cmp (mod (convert:utype @0) (convert:utype @2)) (convert:utype @1)))))))
622 /* X % -C is the same as X % C. */
624 (trunc_mod @0 INTEGER_CST@1)
625 (if (TYPE_SIGN (type) == SIGNED
626 && !TREE_OVERFLOW (@1)
627 && wi::neg_p (wi::to_wide (@1))
628 && !TYPE_OVERFLOW_TRAPS (type)
629 /* Avoid this transformation if C is INT_MIN, i.e. C == -C. */
630 && !sign_bit_p (@1, @1))
631 (trunc_mod @0 (negate @1))))
633 /* X % -Y is the same as X % Y. */
635 (trunc_mod @0 (convert? (negate @1)))
636 (if (INTEGRAL_TYPE_P (type)
637 && !TYPE_UNSIGNED (type)
638 && !TYPE_OVERFLOW_TRAPS (type)
639 && tree_nop_conversion_p (type, TREE_TYPE (@1))
640 /* Avoid this transformation if X might be INT_MIN or
641 Y might be -1, because we would then change valid
642 INT_MIN % -(-1) into invalid INT_MIN % -1. */
643 && (expr_not_equal_to (@0, wi::to_wide (TYPE_MIN_VALUE (type)))
644 || expr_not_equal_to (@1, wi::minus_one (TYPE_PRECISION
646 (trunc_mod @0 (convert @1))))
648 /* X - (X / Y) * Y is the same as X % Y. */
650 (minus (convert1? @0) (convert2? (mult:c (trunc_div @@0 @@1) @1)))
651 (if (INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
652 (convert (trunc_mod @0 @1))))
654 /* x * (1 + y / x) - y -> x - y % x */
656 (minus (mult:cs @0 (plus:s (trunc_div:s @1 @0) integer_onep)) @1)
657 (if (INTEGRAL_TYPE_P (type))
658 (minus @0 (trunc_mod @1 @0))))
660 /* Optimize TRUNC_MOD_EXPR by a power of two into a BIT_AND_EXPR,
661 i.e. "X % C" into "X & (C - 1)", if X and C are positive.
662 Also optimize A % (C << N) where C is a power of 2,
663 to A & ((C << N) - 1).
664 Also optimize "A shift (B % C)", if C is a power of 2, to
665 "A shift (B & (C - 1))". SHIFT operation include "<<" and ">>"
666 and assume (B % C) is nonnegative as shifts negative values would
668 (match (power_of_two_cand @1)
670 (match (power_of_two_cand @1)
671 (lshift INTEGER_CST@1 @2))
672 (for mod (trunc_mod floor_mod)
673 (for shift (lshift rshift)
675 (shift @0 (mod @1 (power_of_two_cand@2 @3)))
676 (if (integer_pow2p (@3) && tree_int_cst_sgn (@3) > 0)
677 (shift @0 (bit_and @1 (minus @2 { build_int_cst (TREE_TYPE (@2),
680 (mod @0 (convert? (power_of_two_cand@1 @2)))
681 (if ((TYPE_UNSIGNED (type) || tree_expr_nonnegative_p (@0))
682 /* Allow any integral conversions of the divisor, except
683 conversion from narrower signed to wider unsigned type
684 where if @1 would be negative power of two, the divisor
685 would not be a power of two. */
686 && INTEGRAL_TYPE_P (type)
687 && INTEGRAL_TYPE_P (TREE_TYPE (@1))
688 && (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@1))
689 || TYPE_UNSIGNED (TREE_TYPE (@1))
690 || !TYPE_UNSIGNED (type))
691 && integer_pow2p (@2) && tree_int_cst_sgn (@2) > 0)
692 (with { tree utype = TREE_TYPE (@1);
693 if (!TYPE_OVERFLOW_WRAPS (utype))
694 utype = unsigned_type_for (utype); }
695 (bit_and @0 (convert (minus (convert:utype @1)
696 { build_one_cst (utype); })))))))
698 /* Simplify (unsigned t * 2)/2 -> unsigned t & 0x7FFFFFFF. */
700 (trunc_div (mult @0 integer_pow2p@1) @1)
701 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
702 (bit_and @0 { wide_int_to_tree
703 (type, wi::mask (TYPE_PRECISION (type)
704 - wi::exact_log2 (wi::to_wide (@1)),
705 false, TYPE_PRECISION (type))); })))
707 /* Simplify (unsigned t / 2) * 2 -> unsigned t & ~1. */
709 (mult (trunc_div @0 integer_pow2p@1) @1)
710 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
711 (bit_and @0 (negate @1))))
713 /* Simplify (t * 2) / 2) -> t. */
714 (for div (trunc_div ceil_div floor_div round_div exact_div)
716 (div (mult:c @0 @1) @1)
717 (if (ANY_INTEGRAL_TYPE_P (type))
718 (if (TYPE_OVERFLOW_UNDEFINED (type))
723 bool overflowed = true;
724 value_range vr0, vr1;
725 if (INTEGRAL_TYPE_P (type)
726 && get_global_range_query ()->range_of_expr (vr0, @0)
727 && get_global_range_query ()->range_of_expr (vr1, @1)
728 && vr0.kind () == VR_RANGE
729 && vr1.kind () == VR_RANGE)
731 wide_int wmin0 = vr0.lower_bound ();
732 wide_int wmax0 = vr0.upper_bound ();
733 wide_int wmin1 = vr1.lower_bound ();
734 wide_int wmax1 = vr1.upper_bound ();
735 /* If the multiplication can't overflow/wrap around, then
736 it can be optimized too. */
737 wi::overflow_type min_ovf, max_ovf;
738 wi::mul (wmin0, wmin1, TYPE_SIGN (type), &min_ovf);
739 wi::mul (wmax0, wmax1, TYPE_SIGN (type), &max_ovf);
740 if (min_ovf == wi::OVF_NONE && max_ovf == wi::OVF_NONE)
742 wi::mul (wmin0, wmax1, TYPE_SIGN (type), &min_ovf);
743 wi::mul (wmax0, wmin1, TYPE_SIGN (type), &max_ovf);
744 if (min_ovf == wi::OVF_NONE && max_ovf == wi::OVF_NONE)
755 /* Simplify cos(-x) and cos(|x|) -> cos(x). Similarly for cosh. */
760 /* Simplify pow(-x, y) and pow(|x|,y) -> pow(x,y) if y is an even integer. */
763 (pows (op @0) REAL_CST@1)
764 (with { HOST_WIDE_INT n; }
765 (if (real_isinteger (&TREE_REAL_CST (@1), &n) && (n & 1) == 0)
767 /* Likewise for powi. */
770 (pows (op @0) INTEGER_CST@1)
771 (if ((wi::to_wide (@1) & 1) == 0)
773 /* Strip negate and abs from both operands of hypot. */
781 /* copysign(-x, y) and copysign(abs(x), y) -> copysign(x, y). */
782 (for copysigns (COPYSIGN_ALL)
784 (copysigns (op @0) @1)
787 /* abs(x)*abs(x) -> x*x. Should be valid for all types. */
792 /* Convert absu(x)*absu(x) -> x*x. */
794 (mult (absu@1 @0) @1)
795 (mult (convert@2 @0) @2))
797 /* cos(copysign(x, y)) -> cos(x). Similarly for cosh. */
801 (coss (copysigns @0 @1))
804 /* pow(copysign(x, y), z) -> pow(x, z) if z is an even integer. */
808 (pows (copysigns @0 @2) REAL_CST@1)
809 (with { HOST_WIDE_INT n; }
810 (if (real_isinteger (&TREE_REAL_CST (@1), &n) && (n & 1) == 0)
812 /* Likewise for powi. */
816 (pows (copysigns @0 @2) INTEGER_CST@1)
817 (if ((wi::to_wide (@1) & 1) == 0)
822 /* hypot(copysign(x, y), z) -> hypot(x, z). */
824 (hypots (copysigns @0 @1) @2)
826 /* hypot(x, copysign(y, z)) -> hypot(x, y). */
828 (hypots @0 (copysigns @1 @2))
831 /* copysign(x, CST) -> [-]abs (x). */
832 (for copysigns (COPYSIGN_ALL)
834 (copysigns @0 REAL_CST@1)
835 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
839 /* copysign(copysign(x, y), z) -> copysign(x, z). */
840 (for copysigns (COPYSIGN_ALL)
842 (copysigns (copysigns @0 @1) @2)
845 /* copysign(x,y)*copysign(x,y) -> x*x. */
846 (for copysigns (COPYSIGN_ALL)
848 (mult (copysigns@2 @0 @1) @2)
851 /* ccos(-x) -> ccos(x). Similarly for ccosh. */
852 (for ccoss (CCOS CCOSH)
857 /* cabs(-x) and cos(conj(x)) -> cabs(x). */
858 (for ops (conj negate)
864 /* Fold (a * (1 << b)) into (a << b) */
866 (mult:c @0 (convert? (lshift integer_onep@1 @2)))
867 (if (! FLOAT_TYPE_P (type)
868 && tree_nop_conversion_p (type, TREE_TYPE (@1)))
871 /* Fold (1 << (C - x)) where C = precision(type) - 1
872 into ((1 << C) >> x). */
874 (lshift integer_onep@0 (minus@1 INTEGER_CST@2 @3))
875 (if (INTEGRAL_TYPE_P (type)
876 && wi::eq_p (wi::to_wide (@2), TYPE_PRECISION (type) - 1)
878 (if (TYPE_UNSIGNED (type))
879 (rshift (lshift @0 @2) @3)
881 { tree utype = unsigned_type_for (type); }
882 (convert (rshift (lshift (convert:utype @0) @2) @3))))))
884 /* Fold ((type)(a<0)) << SIGNBITOFA into ((type)a) & signbit. */
886 (lshift (convert (lt @0 integer_zerop@1)) INTEGER_CST@2)
887 (if (TYPE_SIGN (TREE_TYPE (@0)) == SIGNED
888 && wi::eq_p (wi::to_wide (@2), TYPE_PRECISION (TREE_TYPE (@0)) - 1))
889 (with { wide_int wone = wi::one (TYPE_PRECISION (type)); }
890 (bit_and (convert @0)
891 { wide_int_to_tree (type,
892 wi::lshift (wone, wi::to_wide (@2))); }))))
894 /* Fold (-x >> C) into -(x > 0) where C = precision(type) - 1. */
895 (for cst (INTEGER_CST VECTOR_CST)
897 (rshift (negate:s @0) cst@1)
898 (if (!TYPE_UNSIGNED (type)
899 && TYPE_OVERFLOW_UNDEFINED (type))
900 (with { tree stype = TREE_TYPE (@1);
901 tree bt = truth_type_for (type);
902 tree zeros = build_zero_cst (type);
903 tree cst = NULL_TREE; }
905 /* Handle scalar case. */
906 (if (INTEGRAL_TYPE_P (type)
907 /* If we apply the rule to the scalar type before vectorization
908 we will enforce the result of the comparison being a bool
909 which will require an extra AND on the result that will be
910 indistinguishable from when the user did actually want 0
911 or 1 as the result so it can't be removed. */
912 && canonicalize_math_after_vectorization_p ()
913 && wi::eq_p (wi::to_wide (@1), TYPE_PRECISION (type) - 1))
914 (negate (convert (gt @0 { zeros; }))))
915 /* Handle vector case. */
916 (if (VECTOR_INTEGER_TYPE_P (type)
917 /* First check whether the target has the same mode for vector
918 comparison results as it's operands do. */
919 && TYPE_MODE (bt) == TYPE_MODE (type)
920 /* Then check to see if the target is able to expand the comparison
921 with the given type later on, otherwise we may ICE. */
922 && expand_vec_cmp_expr_p (type, bt, GT_EXPR)
923 && (cst = uniform_integer_cst_p (@1)) != NULL
924 && wi::eq_p (wi::to_wide (cst), element_precision (type) - 1))
925 (view_convert (gt:bt @0 { zeros; }))))))))
927 /* Fold (C1/X)*C2 into (C1*C2)/X. */
929 (mult (rdiv@3 REAL_CST@0 @1) REAL_CST@2)
930 (if (flag_associative_math
933 { tree tem = const_binop (MULT_EXPR, type, @0, @2); }
935 (rdiv { tem; } @1)))))
937 /* Simplify ~X & X as zero. */
939 (bit_and:c (convert? @0) (convert? (bit_not @0)))
940 { build_zero_cst (type); })
942 /* PR71636: Transform x & ((1U << b) - 1) -> x & ~(~0U << b); */
944 (bit_and:c @0 (plus:s (lshift:s integer_onep @1) integer_minus_onep))
945 (if (TYPE_UNSIGNED (type))
946 (bit_and @0 (bit_not (lshift { build_all_ones_cst (type); } @1)))))
948 (for bitop (bit_and bit_ior)
950 /* PR35691: Transform
951 (x == 0 & y == 0) -> (x | typeof(x)(y)) == 0.
952 (x != 0 | y != 0) -> (x | typeof(x)(y)) != 0. */
954 (bitop (cmp @0 integer_zerop@2) (cmp @1 integer_zerop))
955 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
956 && INTEGRAL_TYPE_P (TREE_TYPE (@1))
957 && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
958 (cmp (bit_ior @0 (convert @1)) @2)))
960 (x == -1 & y == -1) -> (x & typeof(x)(y)) == -1.
961 (x != -1 | y != -1) -> (x & typeof(x)(y)) != -1. */
963 (bitop (cmp @0 integer_all_onesp@2) (cmp @1 integer_all_onesp))
964 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
965 && INTEGRAL_TYPE_P (TREE_TYPE (@1))
966 && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
967 (cmp (bit_and @0 (convert @1)) @2))))
969 /* Fold (A & ~B) - (A & B) into (A ^ B) - B. */
971 (minus (bit_and:cs @0 (bit_not @1)) (bit_and:cs @0 @1))
972 (minus (bit_xor @0 @1) @1))
974 (minus (bit_and:s @0 INTEGER_CST@2) (bit_and:s @0 INTEGER_CST@1))
975 (if (~wi::to_wide (@2) == wi::to_wide (@1))
976 (minus (bit_xor @0 @1) @1)))
978 /* Fold (A & B) - (A & ~B) into B - (A ^ B). */
980 (minus (bit_and:cs @0 @1) (bit_and:cs @0 (bit_not @1)))
981 (minus @1 (bit_xor @0 @1)))
983 /* Simplify (X & ~Y) |^+ (~X & Y) -> X ^ Y. */
984 (for op (bit_ior bit_xor plus)
986 (op (bit_and:c @0 (bit_not @1)) (bit_and:c (bit_not @0) @1))
989 (op:c (bit_and @0 INTEGER_CST@2) (bit_and (bit_not @0) INTEGER_CST@1))
990 (if (~wi::to_wide (@2) == wi::to_wide (@1))
993 /* PR53979: Transform ((a ^ b) | a) -> (a | b) */
995 (bit_ior:c (bit_xor:c @0 @1) @0)
998 /* (a & ~b) | (a ^ b) --> a ^ b */
1000 (bit_ior:c (bit_and:c @0 (bit_not @1)) (bit_xor:c@2 @0 @1))
1003 /* (a & ~b) ^ ~a --> ~(a & b) */
1005 (bit_xor:c (bit_and:cs @0 (bit_not @1)) (bit_not @0))
1006 (bit_not (bit_and @0 @1)))
1008 /* (~a & b) ^ a --> (a | b) */
1010 (bit_xor:c (bit_and:cs (bit_not @0) @1) @0)
1013 /* (a | b) & ~(a ^ b) --> a & b */
1015 (bit_and:c (bit_ior @0 @1) (bit_not (bit_xor:c @0 @1)))
1018 /* a | ~(a ^ b) --> a | ~b */
1020 (bit_ior:c @0 (bit_not:s (bit_xor:c @0 @1)))
1021 (bit_ior @0 (bit_not @1)))
1023 /* (a | b) | (a &^ b) --> a | b */
1024 (for op (bit_and bit_xor)
1026 (bit_ior:c (bit_ior@2 @0 @1) (op:c @0 @1))
1029 /* (a & b) | ~(a ^ b) --> ~(a ^ b) */
1031 (bit_ior:c (bit_and:c @0 @1) (bit_not@2 (bit_xor @0 @1)))
1034 /* ~(~a & b) --> a | ~b */
1036 (bit_not (bit_and:cs (bit_not @0) @1))
1037 (bit_ior @0 (bit_not @1)))
1039 /* ~(~a | b) --> a & ~b */
1041 (bit_not (bit_ior:cs (bit_not @0) @1))
1042 (bit_and @0 (bit_not @1)))
1044 /* (a ^ b) & ((b ^ c) ^ a) --> (a ^ b) & ~c */
1046 (bit_and:c (bit_xor:c@3 @0 @1) (bit_xor:cs (bit_xor:cs @1 @2) @0))
1047 (bit_and @3 (bit_not @2)))
1049 /* (a ^ b) | ((b ^ c) ^ a) --> (a ^ b) | c */
1051 (bit_ior:c (bit_xor:c@3 @0 @1) (bit_xor:c (bit_xor:c @1 @2) @0))
1055 /* (~X | C) ^ D -> (X | C) ^ (~D ^ C) if (~D ^ C) can be simplified. */
1057 (bit_xor:c (bit_ior:cs (bit_not:s @0) @1) @2)
1058 (bit_xor (bit_ior @0 @1) (bit_xor! (bit_not! @2) @1)))
1060 /* (~X & C) ^ D -> (X & C) ^ (D ^ C) if (D ^ C) can be simplified. */
1062 (bit_xor:c (bit_and:cs (bit_not:s @0) @1) @2)
1063 (bit_xor (bit_and @0 @1) (bit_xor! @2 @1)))
1065 /* Simplify (~X & Y) to X ^ Y if we know that (X & ~Y) is 0. */
1067 (bit_and (bit_not SSA_NAME@0) INTEGER_CST@1)
1068 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1069 && wi::bit_and_not (get_nonzero_bits (@0), wi::to_wide (@1)) == 0)
1073 /* For constants M and N, if M == (1LL << cst) - 1 && (N & M) == M,
1074 ((A & N) + B) & M -> (A + B) & M
1075 Similarly if (N & M) == 0,
1076 ((A | N) + B) & M -> (A + B) & M
1077 and for - instead of + (or unary - instead of +)
1078 and/or ^ instead of |.
1079 If B is constant and (B & M) == 0, fold into A & M. */
1080 (for op (plus minus)
1081 (for bitop (bit_and bit_ior bit_xor)
1083 (bit_and (op:s (bitop:s@0 @3 INTEGER_CST@4) @1) INTEGER_CST@2)
1086 tree utype = fold_bit_and_mask (TREE_TYPE (@0), @2, op, @0, bitop,
1087 @3, @4, @1, ERROR_MARK, NULL_TREE,
1090 (convert (bit_and (op (convert:utype { pmop[0]; })
1091 (convert:utype { pmop[1]; }))
1092 (convert:utype @2))))))
1094 (bit_and (op:s @0 (bitop:s@1 @3 INTEGER_CST@4)) INTEGER_CST@2)
1097 tree utype = fold_bit_and_mask (TREE_TYPE (@0), @2, op, @0, ERROR_MARK,
1098 NULL_TREE, NULL_TREE, @1, bitop, @3,
1101 (convert (bit_and (op (convert:utype { pmop[0]; })
1102 (convert:utype { pmop[1]; }))
1103 (convert:utype @2)))))))
1105 (bit_and (op:s @0 @1) INTEGER_CST@2)
1108 tree utype = fold_bit_and_mask (TREE_TYPE (@0), @2, op, @0, ERROR_MARK,
1109 NULL_TREE, NULL_TREE, @1, ERROR_MARK,
1110 NULL_TREE, NULL_TREE, pmop); }
1112 (convert (bit_and (op (convert:utype { pmop[0]; })
1113 (convert:utype { pmop[1]; }))
1114 (convert:utype @2)))))))
1115 (for bitop (bit_and bit_ior bit_xor)
1117 (bit_and (negate:s (bitop:s@0 @2 INTEGER_CST@3)) INTEGER_CST@1)
1120 tree utype = fold_bit_and_mask (TREE_TYPE (@0), @1, NEGATE_EXPR, @0,
1121 bitop, @2, @3, NULL_TREE, ERROR_MARK,
1122 NULL_TREE, NULL_TREE, pmop); }
1124 (convert (bit_and (negate (convert:utype { pmop[0]; }))
1125 (convert:utype @1)))))))
1127 /* X % Y is smaller than Y. */
1130 (cmp (trunc_mod @0 @1) @1)
1131 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
1132 { constant_boolean_node (cmp == LT_EXPR, type); })))
1135 (cmp @1 (trunc_mod @0 @1))
1136 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
1137 { constant_boolean_node (cmp == GT_EXPR, type); })))
1141 (bit_ior @0 integer_all_onesp@1)
1146 (bit_ior @0 integer_zerop)
1151 (bit_and @0 integer_zerop@1)
1157 (for op (bit_ior bit_xor plus)
1159 (op:c (convert? @0) (convert? (bit_not @0)))
1160 (convert { build_all_ones_cst (TREE_TYPE (@0)); })))
1165 { build_zero_cst (type); })
1167 /* Canonicalize X ^ ~0 to ~X. */
1169 (bit_xor @0 integer_all_onesp@1)
1174 (bit_and @0 integer_all_onesp)
1177 /* x & x -> x, x | x -> x */
1178 (for bitop (bit_and bit_ior)
1183 /* x & C -> x if we know that x & ~C == 0. */
1186 (bit_and SSA_NAME@0 INTEGER_CST@1)
1187 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1188 && wi::bit_and_not (get_nonzero_bits (@0), wi::to_wide (@1)) == 0)
1192 /* ~(~X - Y) -> X + Y and ~(~X + Y) -> X - Y. */
1194 (bit_not (minus (bit_not @0) @1))
1197 (bit_not (plus:c (bit_not @0) @1))
1200 /* ~(X - Y) -> ~X + Y. */
1202 (bit_not (minus:s @0 @1))
1203 (plus (bit_not @0) @1))
1205 (bit_not (plus:s @0 INTEGER_CST@1))
1206 (if ((INTEGRAL_TYPE_P (type)
1207 && TYPE_UNSIGNED (type))
1208 || (!TYPE_OVERFLOW_SANITIZED (type)
1209 && may_negate_without_overflow_p (@1)))
1210 (plus (bit_not @0) { const_unop (NEGATE_EXPR, type, @1); })))
1213 /* ~X + Y -> (Y - X) - 1. */
1215 (plus:c (bit_not @0) @1)
1216 (if (ANY_INTEGRAL_TYPE_P (type)
1217 && TYPE_OVERFLOW_WRAPS (type)
1218 /* -1 - X is folded to ~X, so we'd recurse endlessly. */
1219 && !integer_all_onesp (@1))
1220 (plus (minus @1 @0) { build_minus_one_cst (type); })
1221 (if (INTEGRAL_TYPE_P (type)
1222 && TREE_CODE (@1) == INTEGER_CST
1223 && wi::to_wide (@1) != wi::min_value (TYPE_PRECISION (type),
1225 (minus (plus @1 { build_minus_one_cst (type); }) @0))))
1227 /* ~(X >> Y) -> ~X >> Y if ~X can be simplified. */
1229 (bit_not (rshift:s @0 @1))
1230 (if (!TYPE_UNSIGNED (TREE_TYPE (@0)))
1231 (rshift (bit_not! @0) @1)
1232 /* For logical right shifts, this is possible only if @0 doesn't
1233 have MSB set and the logical right shift is changed into
1234 arithmetic shift. */
1235 (if (!wi::neg_p (tree_nonzero_bits (@0)))
1236 (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
1237 (convert (rshift (bit_not! (convert:stype @0)) @1))))))
1240 /* x + (x & 1) -> (x + 1) & ~1 */
1242 (plus:c @0 (bit_and:s @0 integer_onep@1))
1243 (bit_and (plus @0 @1) (bit_not @1)))
1245 /* x & ~(x & y) -> x & ~y */
1246 /* x | ~(x | y) -> x | ~y */
1247 (for bitop (bit_and bit_ior)
1249 (bitop:c @0 (bit_not (bitop:cs @0 @1)))
1250 (bitop @0 (bit_not @1))))
1252 /* (~x & y) | ~(x | y) -> ~x */
1254 (bit_ior:c (bit_and:c (bit_not@2 @0) @1) (bit_not (bit_ior:c @0 @1)))
1257 /* (x | y) ^ (x | ~y) -> ~x */
1259 (bit_xor:c (bit_ior:c @0 @1) (bit_ior:c @0 (bit_not @1)))
1262 /* (x & y) | ~(x | y) -> ~(x ^ y) */
1264 (bit_ior:c (bit_and:s @0 @1) (bit_not:s (bit_ior:s @0 @1)))
1265 (bit_not (bit_xor @0 @1)))
1267 /* (~x | y) ^ (x ^ y) -> x | ~y */
1269 (bit_xor:c (bit_ior:cs (bit_not @0) @1) (bit_xor:s @0 @1))
1270 (bit_ior @0 (bit_not @1)))
1272 /* (x ^ y) | ~(x | y) -> ~(x & y) */
1274 (bit_ior:c (bit_xor:s @0 @1) (bit_not:s (bit_ior:s @0 @1)))
1275 (bit_not (bit_and @0 @1)))
1277 /* (x | y) & ~x -> y & ~x */
1278 /* (x & y) | ~x -> y | ~x */
1279 (for bitop (bit_and bit_ior)
1280 rbitop (bit_ior bit_and)
1282 (bitop:c (rbitop:c @0 @1) (bit_not@2 @0))
1285 /* (x & y) ^ (x | y) -> x ^ y */
1287 (bit_xor:c (bit_and @0 @1) (bit_ior @0 @1))
1290 /* (x ^ y) ^ (x | y) -> x & y */
1292 (bit_xor:c (bit_xor @0 @1) (bit_ior @0 @1))
1295 /* (x & y) + (x ^ y) -> x | y */
1296 /* (x & y) | (x ^ y) -> x | y */
1297 /* (x & y) ^ (x ^ y) -> x | y */
1298 (for op (plus bit_ior bit_xor)
1300 (op:c (bit_and @0 @1) (bit_xor @0 @1))
1303 /* (x & y) + (x | y) -> x + y */
1305 (plus:c (bit_and @0 @1) (bit_ior @0 @1))
1308 /* (x + y) - (x | y) -> x & y */
1310 (minus (plus @0 @1) (bit_ior @0 @1))
1311 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1312 && !TYPE_SATURATING (type))
1315 /* (x + y) - (x & y) -> x | y */
1317 (minus (plus @0 @1) (bit_and @0 @1))
1318 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1319 && !TYPE_SATURATING (type))
1322 /* (x | y) - y -> (x & ~y) */
1324 (minus (bit_ior:cs @0 @1) @1)
1325 (bit_and @0 (bit_not @1)))
1327 /* (x | y) - (x ^ y) -> x & y */
1329 (minus (bit_ior @0 @1) (bit_xor @0 @1))
1332 /* (x | y) - (x & y) -> x ^ y */
1334 (minus (bit_ior @0 @1) (bit_and @0 @1))
1337 /* (x | y) & ~(x & y) -> x ^ y */
1339 (bit_and:c (bit_ior @0 @1) (bit_not (bit_and @0 @1)))
1342 /* (x | y) & (~x ^ y) -> x & y */
1344 (bit_and:c (bit_ior:c @0 @1) (bit_xor:c @1 (bit_not @0)))
1347 /* (~x | y) & (x | ~y) -> ~(x ^ y) */
1349 (bit_and (bit_ior:cs (bit_not @0) @1) (bit_ior:cs @0 (bit_not @1)))
1350 (bit_not (bit_xor @0 @1)))
1352 /* (~x | y) ^ (x | ~y) -> x ^ y */
1354 (bit_xor (bit_ior:c (bit_not @0) @1) (bit_ior:c @0 (bit_not @1)))
1357 /* ((x & y) - (x | y)) - 1 -> ~(x ^ y) */
1359 (plus (nop_convert1? (minus@2 (nop_convert2? (bit_and:c @0 @1))
1360 (nop_convert2? (bit_ior @0 @1))))
1362 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1363 && !TYPE_SATURATING (type) && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2))
1364 && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@2))
1365 && !TYPE_SATURATING (TREE_TYPE (@2)))
1366 (bit_not (convert (bit_xor @0 @1)))))
1368 (minus (nop_convert1? (plus@2 (nop_convert2? (bit_and:c @0 @1))
1370 (nop_convert3? (bit_ior @0 @1)))
1371 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1372 && !TYPE_SATURATING (type) && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2))
1373 && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@2))
1374 && !TYPE_SATURATING (TREE_TYPE (@2)))
1375 (bit_not (convert (bit_xor @0 @1)))))
1377 (minus (nop_convert1? (bit_and @0 @1))
1378 (nop_convert2? (plus@2 (nop_convert3? (bit_ior:c @0 @1))
1380 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1381 && !TYPE_SATURATING (type) && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2))
1382 && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@2))
1383 && !TYPE_SATURATING (TREE_TYPE (@2)))
1384 (bit_not (convert (bit_xor @0 @1)))))
1386 /* ~x & ~y -> ~(x | y)
1387 ~x | ~y -> ~(x & y) */
1388 (for op (bit_and bit_ior)
1389 rop (bit_ior bit_and)
1391 (op (convert1? (bit_not @0)) (convert2? (bit_not @1)))
1392 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1393 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
1394 (bit_not (rop (convert @0) (convert @1))))))
1396 /* If we are XORing or adding two BIT_AND_EXPR's, both of which are and'ing
1397 with a constant, and the two constants have no bits in common,
1398 we should treat this as a BIT_IOR_EXPR since this may produce more
1400 (for op (bit_xor plus)
1402 (op (convert1? (bit_and@4 @0 INTEGER_CST@1))
1403 (convert2? (bit_and@5 @2 INTEGER_CST@3)))
1404 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
1405 && tree_nop_conversion_p (type, TREE_TYPE (@2))
1406 && (wi::to_wide (@1) & wi::to_wide (@3)) == 0)
1407 (bit_ior (convert @4) (convert @5)))))
1409 /* (X | Y) ^ X -> Y & ~ X*/
1411 (bit_xor:c (convert1? (bit_ior:c @@0 @1)) (convert2? @0))
1412 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1413 (convert (bit_and @1 (bit_not @0)))))
1415 /* Convert ~X ^ ~Y to X ^ Y. */
1417 (bit_xor (convert1? (bit_not @0)) (convert2? (bit_not @1)))
1418 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1419 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
1420 (bit_xor (convert @0) (convert @1))))
1422 /* Convert ~X ^ C to X ^ ~C. */
1424 (bit_xor (convert? (bit_not @0)) INTEGER_CST@1)
1425 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1426 (bit_xor (convert @0) (bit_not @1))))
1428 /* Fold (X & Y) ^ Y and (X ^ Y) & Y as ~X & Y. */
1429 (for opo (bit_and bit_xor)
1430 opi (bit_xor bit_and)
1432 (opo:c (opi:cs @0 @1) @1)
1433 (bit_and (bit_not @0) @1)))
1435 /* Given a bit-wise operation CODE applied to ARG0 and ARG1, see if both
1436 operands are another bit-wise operation with a common input. If so,
1437 distribute the bit operations to save an operation and possibly two if
1438 constants are involved. For example, convert
1439 (A | B) & (A | C) into A | (B & C)
1440 Further simplification will occur if B and C are constants. */
1441 (for op (bit_and bit_ior bit_xor)
1442 rop (bit_ior bit_and bit_and)
1444 (op (convert? (rop:c @@0 @1)) (convert? (rop:c @0 @2)))
1445 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1446 && tree_nop_conversion_p (type, TREE_TYPE (@2)))
1447 (rop (convert @0) (op (convert @1) (convert @2))))))
1449 /* Some simple reassociation for bit operations, also handled in reassoc. */
1450 /* (X & Y) & Y -> X & Y
1451 (X | Y) | Y -> X | Y */
1452 (for op (bit_and bit_ior)
1454 (op:c (convert1?@2 (op:c @0 @@1)) (convert2? @1))
1456 /* (X ^ Y) ^ Y -> X */
1458 (bit_xor:c (convert1? (bit_xor:c @0 @@1)) (convert2? @1))
1460 /* (X & Y) & (X & Z) -> (X & Y) & Z
1461 (X | Y) | (X | Z) -> (X | Y) | Z */
1462 (for op (bit_and bit_ior)
1464 (op (convert1?@3 (op:c@4 @0 @1)) (convert2?@5 (op:c@6 @0 @2)))
1465 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1466 && tree_nop_conversion_p (type, TREE_TYPE (@2)))
1467 (if (single_use (@5) && single_use (@6))
1468 (op @3 (convert @2))
1469 (if (single_use (@3) && single_use (@4))
1470 (op (convert @1) @5))))))
1471 /* (X ^ Y) ^ (X ^ Z) -> Y ^ Z */
1473 (bit_xor (convert1? (bit_xor:c @0 @1)) (convert2? (bit_xor:c @0 @2)))
1474 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1475 && tree_nop_conversion_p (type, TREE_TYPE (@2)))
1476 (bit_xor (convert @1) (convert @2))))
1478 /* Convert abs (abs (X)) into abs (X).
1479 also absu (absu (X)) into absu (X). */
1485 (absu (convert@2 (absu@1 @0)))
1486 (if (tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@1)))
1489 /* Convert abs[u] (-X) -> abs[u] (X). */
1498 /* Convert abs[u] (X) where X is nonnegative -> (X). */
1500 (abs tree_expr_nonnegative_p@0)
1504 (absu tree_expr_nonnegative_p@0)
1507 /* Simplify (-(X < 0) | 1) * X into abs (X) or absu(X). */
1509 (mult:c (nop_convert1?
1510 (bit_ior (nop_convert2? (negate (convert? (lt @0 integer_zerop))))
1513 (if (INTEGRAL_TYPE_P (type)
1514 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
1515 && !TYPE_UNSIGNED (TREE_TYPE (@0)))
1516 (if (TYPE_UNSIGNED (type))
1523 /* A few cases of fold-const.c negate_expr_p predicate. */
1524 (match negate_expr_p
1526 (if ((INTEGRAL_TYPE_P (type)
1527 && TYPE_UNSIGNED (type))
1528 || (!TYPE_OVERFLOW_SANITIZED (type)
1529 && may_negate_without_overflow_p (t)))))
1530 (match negate_expr_p
1532 (match negate_expr_p
1534 (if (!TYPE_OVERFLOW_SANITIZED (type))))
1535 (match negate_expr_p
1537 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (t)))))
1538 /* VECTOR_CST handling of non-wrapping types would recurse in unsupported
1540 (match negate_expr_p
1542 (if (FLOAT_TYPE_P (TREE_TYPE (type)) || TYPE_OVERFLOW_WRAPS (type))))
1543 (match negate_expr_p
1545 (if ((ANY_INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_WRAPS (type))
1546 || (FLOAT_TYPE_P (type)
1547 && !HONOR_SIGN_DEPENDENT_ROUNDING (type)
1548 && !HONOR_SIGNED_ZEROS (type)))))
1550 /* (-A) * (-B) -> A * B */
1552 (mult:c (convert1? (negate @0)) (convert2? negate_expr_p@1))
1553 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
1554 && tree_nop_conversion_p (type, TREE_TYPE (@1)))
1555 (mult (convert @0) (convert (negate @1)))))
1557 /* -(A + B) -> (-B) - A. */
1559 (negate (plus:c @0 negate_expr_p@1))
1560 (if (!HONOR_SIGN_DEPENDENT_ROUNDING (type)
1561 && !HONOR_SIGNED_ZEROS (type))
1562 (minus (negate @1) @0)))
1564 /* -(A - B) -> B - A. */
1566 (negate (minus @0 @1))
1567 (if ((ANY_INTEGRAL_TYPE_P (type) && !TYPE_OVERFLOW_SANITIZED (type))
1568 || (FLOAT_TYPE_P (type)
1569 && !HONOR_SIGN_DEPENDENT_ROUNDING (type)
1570 && !HONOR_SIGNED_ZEROS (type)))
1573 (negate (pointer_diff @0 @1))
1574 (if (TYPE_OVERFLOW_UNDEFINED (type))
1575 (pointer_diff @1 @0)))
1577 /* A - B -> A + (-B) if B is easily negatable. */
1579 (minus @0 negate_expr_p@1)
1580 (if (!FIXED_POINT_TYPE_P (type))
1581 (plus @0 (negate @1))))
1583 /* Other simplifications of negation (c.f. fold_negate_expr_1). */
1585 (negate (mult:c@0 @1 negate_expr_p@2))
1586 (if (! TYPE_UNSIGNED (type)
1587 && ! HONOR_SIGN_DEPENDENT_ROUNDING (type)
1589 (mult @1 (negate @2))))
1592 (negate (rdiv@0 @1 negate_expr_p@2))
1593 (if (! HONOR_SIGN_DEPENDENT_ROUNDING (type)
1595 (rdiv @1 (negate @2))))
1598 (negate (rdiv@0 negate_expr_p@1 @2))
1599 (if (! HONOR_SIGN_DEPENDENT_ROUNDING (type)
1601 (rdiv (negate @1) @2)))
1603 /* Fold -((int)x >> (prec - 1)) into (unsigned)x >> (prec - 1). */
1605 (negate (convert? (rshift @0 INTEGER_CST@1)))
1606 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
1607 && wi::to_wide (@1) == element_precision (type) - 1)
1608 (with { tree stype = TREE_TYPE (@0);
1609 tree ntype = TYPE_UNSIGNED (stype) ? signed_type_for (stype)
1610 : unsigned_type_for (stype); }
1611 (convert (rshift:ntype (convert:ntype @0) @1)))))
1613 /* Try to fold (type) X op CST -> (type) (X op ((type-x) CST))
1615 For bitwise binary operations apply operand conversions to the
1616 binary operation result instead of to the operands. This allows
1617 to combine successive conversions and bitwise binary operations.
1618 We combine the above two cases by using a conditional convert. */
1619 (for bitop (bit_and bit_ior bit_xor)
1621 (bitop (convert@2 @0) (convert?@3 @1))
1622 (if (((TREE_CODE (@1) == INTEGER_CST
1623 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
1624 && (int_fits_type_p (@1, TREE_TYPE (@0))
1625 || tree_nop_conversion_p (TREE_TYPE (@0), type)))
1626 || types_match (@0, @1))
1627 /* ??? This transform conflicts with fold-const.c doing
1628 Convert (T)(x & c) into (T)x & (T)c, if c is an integer
1629 constants (if x has signed type, the sign bit cannot be set
1630 in c). This folds extension into the BIT_AND_EXPR.
1631 Restrict it to GIMPLE to avoid endless recursions. */
1632 && (bitop != BIT_AND_EXPR || GIMPLE)
1633 && (/* That's a good idea if the conversion widens the operand, thus
1634 after hoisting the conversion the operation will be narrower.
1635 It is also a good if the conversion is a nop as moves the
1636 conversion to one side; allowing for combining of the conversions. */
1637 TYPE_PRECISION (TREE_TYPE (@0)) < TYPE_PRECISION (type)
1638 /* The conversion check for being a nop can only be done at the gimple
1639 level as fold_binary has some re-association code which can conflict
1640 with this if there is a "constant" which is not a full INTEGER_CST. */
1641 || (GIMPLE && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (type))
1642 /* It's also a good idea if the conversion is to a non-integer
1644 || GET_MODE_CLASS (TYPE_MODE (type)) != MODE_INT
1645 /* Or if the precision of TO is not the same as the precision
1647 || !type_has_mode_precision_p (type)
1648 /* In GIMPLE, getting rid of 2 conversions for one new results
1651 && TREE_CODE (@1) != INTEGER_CST
1652 && tree_nop_conversion_p (type, TREE_TYPE (@0))
1654 && single_use (@3))))
1655 (convert (bitop @0 (convert @1)))))
1656 /* In GIMPLE, getting rid of 2 conversions for one new results
1659 (convert (bitop:cs@2 (nop_convert:s @0) @1))
1661 && TREE_CODE (@1) != INTEGER_CST
1662 && tree_nop_conversion_p (type, TREE_TYPE (@2))
1663 && types_match (type, @0))
1664 (bitop @0 (convert @1)))))
1666 (for bitop (bit_and bit_ior)
1667 rbitop (bit_ior bit_and)
1668 /* (x | y) & x -> x */
1669 /* (x & y) | x -> x */
1671 (bitop:c (rbitop:c @0 @1) @0)
1673 /* (~x | y) & x -> x & y */
1674 /* (~x & y) | x -> x | y */
1676 (bitop:c (rbitop:c (bit_not @0) @1) @0)
1679 /* ((x | y) & z) | x -> (z & y) | x */
1681 (bit_ior:c (bit_and:cs (bit_ior:cs @0 @1) @2) @0)
1682 (bit_ior (bit_and @2 @1) @0))
1684 /* (x | CST1) & CST2 -> (x & CST2) | (CST1 & CST2) */
1686 (bit_and (bit_ior @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
1687 (bit_ior (bit_and @0 @2) (bit_and @1 @2)))
1689 /* Combine successive equal operations with constants. */
1690 (for bitop (bit_and bit_ior bit_xor)
1692 (bitop (bitop @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
1693 (if (!CONSTANT_CLASS_P (@0))
1694 /* This is the canonical form regardless of whether (bitop @1 @2) can be
1695 folded to a constant. */
1696 (bitop @0 (bitop @1 @2))
1697 /* In this case we have three constants and (bitop @0 @1) doesn't fold
1698 to a constant. This can happen if @0 or @1 is a POLY_INT_CST and if
1699 the values involved are such that the operation can't be decided at
1700 compile time. Try folding one of @0 or @1 with @2 to see whether
1701 that combination can be decided at compile time.
1703 Keep the existing form if both folds fail, to avoid endless
1705 (with { tree cst1 = const_binop (bitop, type, @0, @2); }
1707 (bitop @1 { cst1; })
1708 (with { tree cst2 = const_binop (bitop, type, @1, @2); }
1710 (bitop @0 { cst2; }))))))))
1712 /* Try simple folding for X op !X, and X op X with the help
1713 of the truth_valued_p and logical_inverted_value predicates. */
1714 (match truth_valued_p
1716 (if (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1)))
1717 (for op (tcc_comparison truth_and truth_andif truth_or truth_orif truth_xor)
1718 (match truth_valued_p
1720 (match truth_valued_p
1723 (match (logical_inverted_value @0)
1725 (match (logical_inverted_value @0)
1726 (bit_not truth_valued_p@0))
1727 (match (logical_inverted_value @0)
1728 (eq @0 integer_zerop))
1729 (match (logical_inverted_value @0)
1730 (ne truth_valued_p@0 integer_truep))
1731 (match (logical_inverted_value @0)
1732 (bit_xor truth_valued_p@0 integer_truep))
1736 (bit_and:c @0 (logical_inverted_value @0))
1737 { build_zero_cst (type); })
1738 /* X | !X and X ^ !X -> 1, , if X is truth-valued. */
1739 (for op (bit_ior bit_xor)
1741 (op:c truth_valued_p@0 (logical_inverted_value @0))
1742 { constant_boolean_node (true, type); }))
1743 /* X ==/!= !X is false/true. */
1746 (op:c truth_valued_p@0 (logical_inverted_value @0))
1747 { constant_boolean_node (op == NE_EXPR ? true : false, type); }))
1751 (bit_not (bit_not @0))
1754 /* Convert ~ (-A) to A - 1. */
1756 (bit_not (convert? (negate @0)))
1757 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1758 || !TYPE_UNSIGNED (TREE_TYPE (@0)))
1759 (convert (minus @0 { build_each_one_cst (TREE_TYPE (@0)); }))))
1761 /* Convert - (~A) to A + 1. */
1763 (negate (nop_convert? (bit_not @0)))
1764 (plus (view_convert @0) { build_each_one_cst (type); }))
1766 /* Convert ~ (A - 1) or ~ (A + -1) to -A. */
1768 (bit_not (convert? (minus @0 integer_each_onep)))
1769 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1770 || !TYPE_UNSIGNED (TREE_TYPE (@0)))
1771 (convert (negate @0))))
1773 (bit_not (convert? (plus @0 integer_all_onesp)))
1774 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1775 || !TYPE_UNSIGNED (TREE_TYPE (@0)))
1776 (convert (negate @0))))
1778 /* Part of convert ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify. */
1780 (bit_not (convert? (bit_xor @0 INTEGER_CST@1)))
1781 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1782 (convert (bit_xor @0 (bit_not @1)))))
1784 (bit_not (convert? (bit_xor:c (bit_not @0) @1)))
1785 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1786 (convert (bit_xor @0 @1))))
1788 /* Otherwise prefer ~(X ^ Y) to ~X ^ Y as more canonical. */
1790 (bit_xor:c (nop_convert?:s (bit_not:s @0)) @1)
1791 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1792 (bit_not (bit_xor (view_convert @0) @1))))
1794 /* (x & ~m) | (y & m) -> ((x ^ y) & m) ^ x */
1796 (bit_ior:c (bit_and:cs @0 (bit_not @2)) (bit_and:cs @1 @2))
1797 (bit_xor (bit_and (bit_xor @0 @1) @2) @0))
1799 /* Fold A - (A & B) into ~B & A. */
1801 (minus (convert1? @0) (convert2?:s (bit_and:cs @@0 @1)))
1802 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
1803 && tree_nop_conversion_p (type, TREE_TYPE (@1)))
1804 (convert (bit_and (bit_not @1) @0))))
1806 /* (m1 CMP m2) * d -> (m1 CMP m2) ? d : 0 */
1807 (if (!canonicalize_math_p ())
1808 (for cmp (gt lt ge le)
1810 (mult (convert (cmp @0 @1)) @2)
1811 (cond (cmp @0 @1) @2 { build_zero_cst (type); }))))
1813 /* For integral types with undefined overflow and C != 0 fold
1814 x * C EQ/NE y * C into x EQ/NE y. */
1817 (cmp (mult:c @0 @1) (mult:c @2 @1))
1818 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
1819 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1820 && tree_expr_nonzero_p (@1))
1823 /* For integral types with wrapping overflow and C odd fold
1824 x * C EQ/NE y * C into x EQ/NE y. */
1827 (cmp (mult @0 INTEGER_CST@1) (mult @2 @1))
1828 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
1829 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))
1830 && (TREE_INT_CST_LOW (@1) & 1) != 0)
1833 /* For integral types with undefined overflow and C != 0 fold
1834 x * C RELOP y * C into:
1836 x RELOP y for nonnegative C
1837 y RELOP x for negative C */
1838 (for cmp (lt gt le ge)
1840 (cmp (mult:c @0 @1) (mult:c @2 @1))
1841 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
1842 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1843 (if (tree_expr_nonnegative_p (@1) && tree_expr_nonzero_p (@1))
1845 (if (TREE_CODE (@1) == INTEGER_CST
1846 && wi::neg_p (wi::to_wide (@1), TYPE_SIGN (TREE_TYPE (@1))))
1849 /* (X - 1U) <= INT_MAX-1U into (int) X > 0. */
1853 (cmp (plus @0 integer_minus_onep@1) INTEGER_CST@2)
1854 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1855 && TYPE_UNSIGNED (TREE_TYPE (@0))
1856 && TYPE_PRECISION (TREE_TYPE (@0)) > 1
1857 && (wi::to_wide (@2)
1858 == wi::max_value (TYPE_PRECISION (TREE_TYPE (@0)), SIGNED) - 1))
1859 (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
1860 (icmp (convert:stype @0) { build_int_cst (stype, 0); })))))
1862 /* X / 4 < Y / 4 iff X < Y when the division is known to be exact. */
1863 (for cmp (simple_comparison)
1865 (cmp (convert?@3 (exact_div @0 INTEGER_CST@2)) (convert? (exact_div @1 @2)))
1866 (if (element_precision (@3) >= element_precision (@0)
1867 && types_match (@0, @1))
1868 (if (wi::lt_p (wi::to_wide (@2), 0, TYPE_SIGN (TREE_TYPE (@2))))
1869 (if (!TYPE_UNSIGNED (TREE_TYPE (@3)))
1871 (if (tree_expr_nonzero_p (@0) && tree_expr_nonzero_p (@1))
1874 tree utype = unsigned_type_for (TREE_TYPE (@0));
1876 (cmp (convert:utype @1) (convert:utype @0)))))
1877 (if (wi::gt_p (wi::to_wide (@2), 1, TYPE_SIGN (TREE_TYPE (@2))))
1878 (if (TYPE_UNSIGNED (TREE_TYPE (@0)) || !TYPE_UNSIGNED (TREE_TYPE (@3)))
1882 tree utype = unsigned_type_for (TREE_TYPE (@0));
1884 (cmp (convert:utype @0) (convert:utype @1)))))))))
1886 /* X / C1 op C2 into a simple range test. */
1887 (for cmp (simple_comparison)
1889 (cmp (trunc_div:s @0 INTEGER_CST@1) INTEGER_CST@2)
1890 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1891 && integer_nonzerop (@1)
1892 && !TREE_OVERFLOW (@1)
1893 && !TREE_OVERFLOW (@2))
1894 (with { tree lo, hi; bool neg_overflow;
1895 enum tree_code code = fold_div_compare (cmp, @1, @2, &lo, &hi,
1898 (if (code == LT_EXPR || code == GE_EXPR)
1899 (if (TREE_OVERFLOW (lo))
1900 { build_int_cst (type, (code == LT_EXPR) ^ neg_overflow); }
1901 (if (code == LT_EXPR)
1904 (if (code == LE_EXPR || code == GT_EXPR)
1905 (if (TREE_OVERFLOW (hi))
1906 { build_int_cst (type, (code == LE_EXPR) ^ neg_overflow); }
1907 (if (code == LE_EXPR)
1911 { build_int_cst (type, code == NE_EXPR); })
1912 (if (code == EQ_EXPR && !hi)
1914 (if (code == EQ_EXPR && !lo)
1916 (if (code == NE_EXPR && !hi)
1918 (if (code == NE_EXPR && !lo)
1921 { build_range_check (UNKNOWN_LOCATION, type, @0, code == EQ_EXPR,
1925 tree etype = range_check_type (TREE_TYPE (@0));
1928 hi = fold_convert (etype, hi);
1929 lo = fold_convert (etype, lo);
1930 hi = const_binop (MINUS_EXPR, etype, hi, lo);
1933 (if (etype && hi && !TREE_OVERFLOW (hi))
1934 (if (code == EQ_EXPR)
1935 (le (minus (convert:etype @0) { lo; }) { hi; })
1936 (gt (minus (convert:etype @0) { lo; }) { hi; })))))))))
1938 /* X + Z < Y + Z is the same as X < Y when there is no overflow. */
1939 (for op (lt le ge gt)
1941 (op (plus:c @0 @2) (plus:c @1 @2))
1942 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1943 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1945 /* For equality and subtraction, this is also true with wrapping overflow. */
1946 (for op (eq ne minus)
1948 (op (plus:c @0 @2) (plus:c @1 @2))
1949 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1950 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1951 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
1954 /* X - Z < Y - Z is the same as X < Y when there is no overflow. */
1955 (for op (lt le ge gt)
1957 (op (minus @0 @2) (minus @1 @2))
1958 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1959 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1961 /* For equality and subtraction, this is also true with wrapping overflow. */
1962 (for op (eq ne minus)
1964 (op (minus @0 @2) (minus @1 @2))
1965 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1966 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1967 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
1969 /* And for pointers... */
1970 (for op (simple_comparison)
1972 (op (pointer_diff@3 @0 @2) (pointer_diff @1 @2))
1973 (if (!TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
1976 (minus (pointer_diff@3 @0 @2) (pointer_diff @1 @2))
1977 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@3))
1978 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
1979 (pointer_diff @0 @1)))
1981 /* Z - X < Z - Y is the same as Y < X when there is no overflow. */
1982 (for op (lt le ge gt)
1984 (op (minus @2 @0) (minus @2 @1))
1985 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1986 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1988 /* For equality and subtraction, this is also true with wrapping overflow. */
1989 (for op (eq ne minus)
1991 (op (minus @2 @0) (minus @2 @1))
1992 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1993 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1994 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
1996 /* And for pointers... */
1997 (for op (simple_comparison)
1999 (op (pointer_diff@3 @2 @0) (pointer_diff @2 @1))
2000 (if (!TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
2003 (minus (pointer_diff@3 @2 @0) (pointer_diff @2 @1))
2004 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@3))
2005 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
2006 (pointer_diff @1 @0)))
2008 /* X + Y < Y is the same as X < 0 when there is no overflow. */
2009 (for op (lt le gt ge)
2011 (op:c (plus:c@2 @0 @1) @1)
2012 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2013 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2014 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0))
2015 && (CONSTANT_CLASS_P (@0) || single_use (@2)))
2016 (op @0 { build_zero_cst (TREE_TYPE (@0)); }))))
2017 /* For equality, this is also true with wrapping overflow. */
2020 (op:c (nop_convert?@3 (plus:c@2 @0 (convert1? @1))) (convert2? @1))
2021 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2022 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2023 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
2024 && (CONSTANT_CLASS_P (@0) || (single_use (@2) && single_use (@3)))
2025 && tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@2))
2026 && tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@1)))
2027 (op @0 { build_zero_cst (TREE_TYPE (@0)); })))
2029 (op:c (nop_convert?@3 (pointer_plus@2 (convert1? @0) @1)) (convert2? @0))
2030 (if (tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@0))
2031 && tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0))
2032 && (CONSTANT_CLASS_P (@1) || (single_use (@2) && single_use (@3))))
2033 (op @1 { build_zero_cst (TREE_TYPE (@1)); }))))
2035 /* X - Y < X is the same as Y > 0 when there is no overflow.
2036 For equality, this is also true with wrapping overflow. */
2037 (for op (simple_comparison)
2039 (op:c @0 (minus@2 @0 @1))
2040 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2041 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2042 || ((op == EQ_EXPR || op == NE_EXPR)
2043 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
2044 && (CONSTANT_CLASS_P (@1) || single_use (@2)))
2045 (op @1 { build_zero_cst (TREE_TYPE (@1)); }))))
2048 (X / Y) == 0 -> X < Y if X, Y are unsigned.
2049 (X / Y) != 0 -> X >= Y, if X, Y are unsigned. */
2053 (cmp (trunc_div @0 @1) integer_zerop)
2054 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
2055 /* Complex ==/!= is allowed, but not </>=. */
2056 && TREE_CODE (TREE_TYPE (@0)) != COMPLEX_TYPE
2057 && (VECTOR_TYPE_P (type) || !VECTOR_TYPE_P (TREE_TYPE (@0))))
2060 /* X == C - X can never be true if C is odd. */
2063 (cmp:c (convert? @0) (convert1? (minus INTEGER_CST@1 (convert2? @0))))
2064 (if (TREE_INT_CST_LOW (@1) & 1)
2065 { constant_boolean_node (cmp == NE_EXPR, type); })))
2067 /* Arguments on which one can call get_nonzero_bits to get the bits
2069 (match with_possible_nonzero_bits
2071 (match with_possible_nonzero_bits
2073 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))))
2074 /* Slightly extended version, do not make it recursive to keep it cheap. */
2075 (match (with_possible_nonzero_bits2 @0)
2076 with_possible_nonzero_bits@0)
2077 (match (with_possible_nonzero_bits2 @0)
2078 (bit_and:c with_possible_nonzero_bits@0 @2))
2080 /* Same for bits that are known to be set, but we do not have
2081 an equivalent to get_nonzero_bits yet. */
2082 (match (with_certain_nonzero_bits2 @0)
2084 (match (with_certain_nonzero_bits2 @0)
2085 (bit_ior @1 INTEGER_CST@0))
2087 /* X == C (or X & Z == Y | C) is impossible if ~nonzero(X) & C != 0. */
2090 (cmp:c (with_possible_nonzero_bits2 @0) (with_certain_nonzero_bits2 @1))
2091 (if (wi::bit_and_not (wi::to_wide (@1), get_nonzero_bits (@0)) != 0)
2092 { constant_boolean_node (cmp == NE_EXPR, type); })))
2094 /* ((X inner_op C0) outer_op C1)
2095 With X being a tree where value_range has reasoned certain bits to always be
2096 zero throughout its computed value range,
2097 inner_op = {|,^}, outer_op = {|,^} and inner_op != outer_op
2098 where zero_mask has 1's for all bits that are sure to be 0 in
2100 if (inner_op == '^') C0 &= ~C1;
2101 if ((C0 & ~zero_mask) == 0) then emit (X outer_op (C0 outer_op C1)
2102 if ((C1 & ~zero_mask) == 0) then emit (X inner_op (C0 outer_op C1)
2104 (for inner_op (bit_ior bit_xor)
2105 outer_op (bit_xor bit_ior)
2108 (inner_op:s @2 INTEGER_CST@0) INTEGER_CST@1)
2112 wide_int zero_mask_not;
2116 if (TREE_CODE (@2) == SSA_NAME)
2117 zero_mask_not = get_nonzero_bits (@2);
2121 if (inner_op == BIT_XOR_EXPR)
2123 C0 = wi::bit_and_not (wi::to_wide (@0), wi::to_wide (@1));
2124 cst_emit = C0 | wi::to_wide (@1);
2128 C0 = wi::to_wide (@0);
2129 cst_emit = C0 ^ wi::to_wide (@1);
2132 (if (!fail && (C0 & zero_mask_not) == 0)
2133 (outer_op @2 { wide_int_to_tree (type, cst_emit); })
2134 (if (!fail && (wi::to_wide (@1) & zero_mask_not) == 0)
2135 (inner_op @2 { wide_int_to_tree (type, cst_emit); }))))))
2137 /* Associate (p +p off1) +p off2 as (p +p (off1 + off2)). */
2139 (pointer_plus (pointer_plus:s @0 @1) @3)
2140 (pointer_plus @0 (plus @1 @3)))
2146 tem4 = (unsigned long) tem3;
2151 (pointer_plus @0 (convert?@2 (minus@3 (convert @1) (convert @0))))
2152 /* Conditionally look through a sign-changing conversion. */
2153 (if (TYPE_PRECISION (TREE_TYPE (@2)) == TYPE_PRECISION (TREE_TYPE (@3))
2154 && ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@1)))
2155 || (GENERIC && type == TREE_TYPE (@1))))
2158 (pointer_plus @0 (convert?@2 (pointer_diff@3 @1 @@0)))
2159 (if (TYPE_PRECISION (TREE_TYPE (@2)) >= TYPE_PRECISION (TREE_TYPE (@3)))
2163 tem = (sizetype) ptr;
2167 and produce the simpler and easier to analyze with respect to alignment
2168 ... = ptr & ~algn; */
2170 (pointer_plus @0 (negate (bit_and (convert @0) INTEGER_CST@1)))
2171 (with { tree algn = wide_int_to_tree (TREE_TYPE (@0), ~wi::to_wide (@1)); }
2172 (bit_and @0 { algn; })))
2174 /* Try folding difference of addresses. */
2176 (minus (convert ADDR_EXPR@0) (convert @1))
2177 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2178 (with { poly_int64 diff; }
2179 (if (ptr_difference_const (@0, @1, &diff))
2180 { build_int_cst_type (type, diff); }))))
2182 (minus (convert @0) (convert ADDR_EXPR@1))
2183 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2184 (with { poly_int64 diff; }
2185 (if (ptr_difference_const (@0, @1, &diff))
2186 { build_int_cst_type (type, diff); }))))
2188 (pointer_diff (convert?@2 ADDR_EXPR@0) (convert1?@3 @1))
2189 (if (tree_nop_conversion_p (TREE_TYPE(@2), TREE_TYPE (@0))
2190 && tree_nop_conversion_p (TREE_TYPE(@3), TREE_TYPE (@1)))
2191 (with { poly_int64 diff; }
2192 (if (ptr_difference_const (@0, @1, &diff))
2193 { build_int_cst_type (type, diff); }))))
2195 (pointer_diff (convert?@2 @0) (convert1?@3 ADDR_EXPR@1))
2196 (if (tree_nop_conversion_p (TREE_TYPE(@2), TREE_TYPE (@0))
2197 && tree_nop_conversion_p (TREE_TYPE(@3), TREE_TYPE (@1)))
2198 (with { poly_int64 diff; }
2199 (if (ptr_difference_const (@0, @1, &diff))
2200 { build_int_cst_type (type, diff); }))))
2202 /* (&a+b) - (&a[1] + c) -> sizeof(a[0]) + (b - c) */
2204 (pointer_diff (pointer_plus ADDR_EXPR@0 @1) (pointer_plus ADDR_EXPR@2 @3))
2205 (with { poly_int64 diff; }
2206 (if (ptr_difference_const (@0, @2, &diff))
2207 (plus { build_int_cst_type (type, diff); } (convert (minus @1 @3))))))
2209 /* (&a+b) !=/== (&a[1] + c) -> sizeof(a[0]) + b !=/== c */
2212 (neeq (pointer_plus ADDR_EXPR@0 @1) (pointer_plus ADDR_EXPR@2 @3))
2213 (with { poly_int64 diff; tree inner_type = TREE_TYPE (@1);}
2214 (if (ptr_difference_const (@0, @2, &diff))
2215 (neeq (plus { build_int_cst_type (inner_type, diff); } @1) @3)))))
2217 /* Canonicalize (T *)(ptr - ptr-cst) to &MEM[ptr + -ptr-cst]. */
2219 (convert (pointer_diff @0 INTEGER_CST@1))
2220 (if (POINTER_TYPE_P (type))
2221 { build_fold_addr_expr_with_type
2222 (build2 (MEM_REF, char_type_node, @0,
2223 wide_int_to_tree (ptr_type_node, wi::neg (wi::to_wide (@1)))),
2226 /* If arg0 is derived from the address of an object or function, we may
2227 be able to fold this expression using the object or function's
2230 (bit_and (convert? @0) INTEGER_CST@1)
2231 (if (POINTER_TYPE_P (TREE_TYPE (@0))
2232 && tree_nop_conversion_p (type, TREE_TYPE (@0)))
2236 unsigned HOST_WIDE_INT bitpos;
2237 get_pointer_alignment_1 (@0, &align, &bitpos);
2239 (if (wi::ltu_p (wi::to_wide (@1), align / BITS_PER_UNIT))
2240 { wide_int_to_tree (type, (wi::to_wide (@1)
2241 & (bitpos / BITS_PER_UNIT))); }))))
2245 (if (INTEGRAL_TYPE_P (type)
2246 && wi::eq_p (wi::to_wide (t), wi::min_value (type)))))
2250 (if (INTEGRAL_TYPE_P (type)
2251 && wi::eq_p (wi::to_wide (t), wi::max_value (type)))))
2253 /* x > y && x != XXX_MIN --> x > y
2254 x > y && x == XXX_MIN --> false . */
2257 (bit_and:c (gt:c@2 @0 @1) (eqne @0 min_value))
2259 (if (eqne == EQ_EXPR)
2260 { constant_boolean_node (false, type); })
2261 (if (eqne == NE_EXPR)
2265 /* x < y && x != XXX_MAX --> x < y
2266 x < y && x == XXX_MAX --> false. */
2269 (bit_and:c (lt:c@2 @0 @1) (eqne @0 max_value))
2271 (if (eqne == EQ_EXPR)
2272 { constant_boolean_node (false, type); })
2273 (if (eqne == NE_EXPR)
2277 /* x <= y && x == XXX_MIN --> x == XXX_MIN. */
2279 (bit_and:c (le:c @0 @1) (eq@2 @0 min_value))
2282 /* x >= y && x == XXX_MAX --> x == XXX_MAX. */
2284 (bit_and:c (ge:c @0 @1) (eq@2 @0 max_value))
2287 /* x > y || x != XXX_MIN --> x != XXX_MIN. */
2289 (bit_ior:c (gt:c @0 @1) (ne@2 @0 min_value))
2292 /* x <= y || x != XXX_MIN --> true. */
2294 (bit_ior:c (le:c @0 @1) (ne @0 min_value))
2295 { constant_boolean_node (true, type); })
2297 /* x <= y || x == XXX_MIN --> x <= y. */
2299 (bit_ior:c (le:c@2 @0 @1) (eq @0 min_value))
2302 /* x < y || x != XXX_MAX --> x != XXX_MAX. */
2304 (bit_ior:c (lt:c @0 @1) (ne@2 @0 max_value))
2307 /* x >= y || x != XXX_MAX --> true
2308 x >= y || x == XXX_MAX --> x >= y. */
2311 (bit_ior:c (ge:c@2 @0 @1) (eqne @0 max_value))
2313 (if (eqne == EQ_EXPR)
2315 (if (eqne == NE_EXPR)
2316 { constant_boolean_node (true, type); }))))
2318 /* y == XXX_MIN || x < y --> x <= y - 1 */
2320 (bit_ior:c (eq:s @1 min_value) (lt:s @0 @1))
2321 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2322 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
2323 (le @0 (minus @1 { build_int_cst (TREE_TYPE (@1), 1); }))))
2325 /* y != XXX_MIN && x >= y --> x > y - 1 */
2327 (bit_and:c (ne:s @1 min_value) (ge:s @0 @1))
2328 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2329 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
2330 (gt @0 (minus @1 { build_int_cst (TREE_TYPE (@1), 1); }))))
2332 /* Convert (X == CST1) && (X OP2 CST2) to a known value
2333 based on CST1 OP2 CST2. Similarly for (X != CST1). */
2336 (for code2 (eq ne lt gt le ge)
2338 (bit_and:c (code1@3 @0 INTEGER_CST@1) (code2@4 @0 INTEGER_CST@2))
2341 int cmp = tree_int_cst_compare (@1, @2);
2345 case EQ_EXPR: val = (cmp == 0); break;
2346 case NE_EXPR: val = (cmp != 0); break;
2347 case LT_EXPR: val = (cmp < 0); break;
2348 case GT_EXPR: val = (cmp > 0); break;
2349 case LE_EXPR: val = (cmp <= 0); break;
2350 case GE_EXPR: val = (cmp >= 0); break;
2351 default: gcc_unreachable ();
2355 (if (code1 == EQ_EXPR && val) @3)
2356 (if (code1 == EQ_EXPR && !val) { constant_boolean_node (false, type); })
2357 (if (code1 == NE_EXPR && !val) @4))))))
2359 /* Convert (X OP1 CST1) && (X OP2 CST2). */
2361 (for code1 (lt le gt ge)
2362 (for code2 (lt le gt ge)
2364 (bit_and (code1:c@3 @0 INTEGER_CST@1) (code2:c@4 @0 INTEGER_CST@2))
2367 int cmp = tree_int_cst_compare (@1, @2);
2370 /* Choose the more restrictive of two < or <= comparisons. */
2371 (if ((code1 == LT_EXPR || code1 == LE_EXPR)
2372 && (code2 == LT_EXPR || code2 == LE_EXPR))
2373 (if ((cmp < 0) || (cmp == 0 && code1 == LT_EXPR))
2376 /* Likewise chose the more restrictive of two > or >= comparisons. */
2377 (if ((code1 == GT_EXPR || code1 == GE_EXPR)
2378 && (code2 == GT_EXPR || code2 == GE_EXPR))
2379 (if ((cmp > 0) || (cmp == 0 && code1 == GT_EXPR))
2382 /* Check for singleton ranges. */
2384 && ((code1 == LE_EXPR && code2 == GE_EXPR)
2385 || (code1 == GE_EXPR && code2 == LE_EXPR)))
2387 /* Check for disjoint ranges. */
2389 && (code1 == LT_EXPR || code1 == LE_EXPR)
2390 && (code2 == GT_EXPR || code2 == GE_EXPR))
2391 { constant_boolean_node (false, type); })
2393 && (code1 == GT_EXPR || code1 == GE_EXPR)
2394 && (code2 == LT_EXPR || code2 == LE_EXPR))
2395 { constant_boolean_node (false, type); })
2398 /* Convert (X == CST1) || (X OP2 CST2) to a known value
2399 based on CST1 OP2 CST2. Similarly for (X != CST1). */
2402 (for code2 (eq ne lt gt le ge)
2404 (bit_ior:c (code1@3 @0 INTEGER_CST@1) (code2@4 @0 INTEGER_CST@2))
2407 int cmp = tree_int_cst_compare (@1, @2);
2411 case EQ_EXPR: val = (cmp == 0); break;
2412 case NE_EXPR: val = (cmp != 0); break;
2413 case LT_EXPR: val = (cmp < 0); break;
2414 case GT_EXPR: val = (cmp > 0); break;
2415 case LE_EXPR: val = (cmp <= 0); break;
2416 case GE_EXPR: val = (cmp >= 0); break;
2417 default: gcc_unreachable ();
2421 (if (code1 == EQ_EXPR && val) @4)
2422 (if (code1 == NE_EXPR && val) { constant_boolean_node (true, type); })
2423 (if (code1 == NE_EXPR && !val) @3))))))
2425 /* Convert (X OP1 CST1) || (X OP2 CST2). */
2427 (for code1 (lt le gt ge)
2428 (for code2 (lt le gt ge)
2430 (bit_ior (code1@3 @0 INTEGER_CST@1) (code2@4 @0 INTEGER_CST@2))
2433 int cmp = tree_int_cst_compare (@1, @2);
2436 /* Choose the more restrictive of two < or <= comparisons. */
2437 (if ((code1 == LT_EXPR || code1 == LE_EXPR)
2438 && (code2 == LT_EXPR || code2 == LE_EXPR))
2439 (if ((cmp < 0) || (cmp == 0 && code1 == LT_EXPR))
2442 /* Likewise chose the more restrictive of two > or >= comparisons. */
2443 (if ((code1 == GT_EXPR || code1 == GE_EXPR)
2444 && (code2 == GT_EXPR || code2 == GE_EXPR))
2445 (if ((cmp > 0) || (cmp == 0 && code1 == GT_EXPR))
2448 /* Check for singleton ranges. */
2450 && ((code1 == LT_EXPR && code2 == GT_EXPR)
2451 || (code1 == GT_EXPR && code2 == LT_EXPR)))
2453 /* Check for disjoint ranges. */
2455 && (code1 == LT_EXPR || code1 == LE_EXPR)
2456 && (code2 == GT_EXPR || code2 == GE_EXPR))
2457 { constant_boolean_node (true, type); })
2459 && (code1 == GT_EXPR || code1 == GE_EXPR)
2460 && (code2 == LT_EXPR || code2 == LE_EXPR))
2461 { constant_boolean_node (true, type); })
2464 /* We can't reassociate at all for saturating types. */
2465 (if (!TYPE_SATURATING (type))
2467 /* Contract negates. */
2468 /* A + (-B) -> A - B */
2470 (plus:c @0 (convert? (negate @1)))
2471 /* Apply STRIP_NOPS on the negate. */
2472 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
2473 && !TYPE_OVERFLOW_SANITIZED (type))
2477 if (INTEGRAL_TYPE_P (type)
2478 && TYPE_OVERFLOW_WRAPS (type) != TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
2479 t1 = TYPE_OVERFLOW_WRAPS (type) ? type : TREE_TYPE (@1);
2481 (convert (minus (convert:t1 @0) (convert:t1 @1))))))
2482 /* A - (-B) -> A + B */
2484 (minus @0 (convert? (negate @1)))
2485 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
2486 && !TYPE_OVERFLOW_SANITIZED (type))
2490 if (INTEGRAL_TYPE_P (type)
2491 && TYPE_OVERFLOW_WRAPS (type) != TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
2492 t1 = TYPE_OVERFLOW_WRAPS (type) ? type : TREE_TYPE (@1);
2494 (convert (plus (convert:t1 @0) (convert:t1 @1))))))
2496 Sign-extension is ok except for INT_MIN, which thankfully cannot
2497 happen without overflow. */
2499 (negate (convert (negate @1)))
2500 (if (INTEGRAL_TYPE_P (type)
2501 && (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@1))
2502 || (!TYPE_UNSIGNED (TREE_TYPE (@1))
2503 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))))
2504 && !TYPE_OVERFLOW_SANITIZED (type)
2505 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@1)))
2508 (negate (convert negate_expr_p@1))
2509 (if (SCALAR_FLOAT_TYPE_P (type)
2510 && ((DECIMAL_FLOAT_TYPE_P (type)
2511 == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1))
2512 && TYPE_PRECISION (type) >= TYPE_PRECISION (TREE_TYPE (@1)))
2513 || !HONOR_SIGN_DEPENDENT_ROUNDING (type)))
2514 (convert (negate @1))))
2516 (negate (nop_convert? (negate @1)))
2517 (if (!TYPE_OVERFLOW_SANITIZED (type)
2518 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@1)))
2521 /* We can't reassociate floating-point unless -fassociative-math
2522 or fixed-point plus or minus because of saturation to +-Inf. */
2523 (if ((!FLOAT_TYPE_P (type) || flag_associative_math)
2524 && !FIXED_POINT_TYPE_P (type))
2526 /* Match patterns that allow contracting a plus-minus pair
2527 irrespective of overflow issues. */
2528 /* (A +- B) - A -> +- B */
2529 /* (A +- B) -+ B -> A */
2530 /* A - (A +- B) -> -+ B */
2531 /* A +- (B -+ A) -> +- B */
2533 (minus (nop_convert1? (plus:c (nop_convert2? @0) @1)) @0)
2536 (minus (nop_convert1? (minus (nop_convert2? @0) @1)) @0)
2537 (if (!ANY_INTEGRAL_TYPE_P (type)
2538 || TYPE_OVERFLOW_WRAPS (type))
2539 (negate (view_convert @1))
2540 (view_convert (negate @1))))
2542 (plus:c (nop_convert1? (minus @0 (nop_convert2? @1))) @1)
2545 (minus @0 (nop_convert1? (plus:c (nop_convert2? @0) @1)))
2546 (if (!ANY_INTEGRAL_TYPE_P (type)
2547 || TYPE_OVERFLOW_WRAPS (type))
2548 (negate (view_convert @1))
2549 (view_convert (negate @1))))
2551 (minus @0 (nop_convert1? (minus (nop_convert2? @0) @1)))
2553 /* (A +- B) + (C - A) -> C +- B */
2554 /* (A + B) - (A - C) -> B + C */
2555 /* More cases are handled with comparisons. */
2557 (plus:c (plus:c @0 @1) (minus @2 @0))
2560 (plus:c (minus @0 @1) (minus @2 @0))
2563 (plus:c (pointer_diff @0 @1) (pointer_diff @2 @0))
2564 (if (TYPE_OVERFLOW_UNDEFINED (type)
2565 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0)))
2566 (pointer_diff @2 @1)))
2568 (minus (plus:c @0 @1) (minus @0 @2))
2571 /* (A +- CST1) +- CST2 -> A + CST3
2572 Use view_convert because it is safe for vectors and equivalent for
2574 (for outer_op (plus minus)
2575 (for inner_op (plus minus)
2576 neg_inner_op (minus plus)
2578 (outer_op (nop_convert? (inner_op @0 CONSTANT_CLASS_P@1))
2580 /* If one of the types wraps, use that one. */
2581 (if (!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_WRAPS (type))
2582 /* If all 3 captures are CONSTANT_CLASS_P, punt, as we might recurse
2583 forever if something doesn't simplify into a constant. */
2584 (if (!CONSTANT_CLASS_P (@0))
2585 (if (outer_op == PLUS_EXPR)
2586 (plus (view_convert @0) (inner_op @2 (view_convert @1)))
2587 (minus (view_convert @0) (neg_inner_op @2 (view_convert @1)))))
2588 (if (!ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2589 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
2590 (if (outer_op == PLUS_EXPR)
2591 (view_convert (plus @0 (inner_op (view_convert @2) @1)))
2592 (view_convert (minus @0 (neg_inner_op (view_convert @2) @1))))
2593 /* If the constant operation overflows we cannot do the transform
2594 directly as we would introduce undefined overflow, for example
2595 with (a - 1) + INT_MIN. */
2596 (if (types_match (type, @0))
2597 (with { tree cst = const_binop (outer_op == inner_op
2598 ? PLUS_EXPR : MINUS_EXPR,
2600 (if (cst && !TREE_OVERFLOW (cst))
2601 (inner_op @0 { cst; } )
2602 /* X+INT_MAX+1 is X-INT_MIN. */
2603 (if (INTEGRAL_TYPE_P (type) && cst
2604 && wi::to_wide (cst) == wi::min_value (type))
2605 (neg_inner_op @0 { wide_int_to_tree (type, wi::to_wide (cst)); })
2606 /* Last resort, use some unsigned type. */
2607 (with { tree utype = unsigned_type_for (type); }
2609 (view_convert (inner_op
2610 (view_convert:utype @0)
2612 { drop_tree_overflow (cst); }))))))))))))))
2614 /* (CST1 - A) +- CST2 -> CST3 - A */
2615 (for outer_op (plus minus)
2617 (outer_op (nop_convert? (minus CONSTANT_CLASS_P@1 @0)) CONSTANT_CLASS_P@2)
2618 /* If one of the types wraps, use that one. */
2619 (if (!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_WRAPS (type))
2620 /* If all 3 captures are CONSTANT_CLASS_P, punt, as we might recurse
2621 forever if something doesn't simplify into a constant. */
2622 (if (!CONSTANT_CLASS_P (@0))
2623 (minus (outer_op (view_convert @1) @2) (view_convert @0)))
2624 (if (!ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2625 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
2626 (view_convert (minus (outer_op @1 (view_convert @2)) @0))
2627 (if (types_match (type, @0))
2628 (with { tree cst = const_binop (outer_op, type, @1, @2); }
2629 (if (cst && !TREE_OVERFLOW (cst))
2630 (minus { cst; } @0))))))))
2632 /* CST1 - (CST2 - A) -> CST3 + A
2633 Use view_convert because it is safe for vectors and equivalent for
2636 (minus CONSTANT_CLASS_P@1 (nop_convert? (minus CONSTANT_CLASS_P@2 @0)))
2637 /* If one of the types wraps, use that one. */
2638 (if (!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_WRAPS (type))
2639 /* If all 3 captures are CONSTANT_CLASS_P, punt, as we might recurse
2640 forever if something doesn't simplify into a constant. */
2641 (if (!CONSTANT_CLASS_P (@0))
2642 (plus (view_convert @0) (minus @1 (view_convert @2))))
2643 (if (!ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2644 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
2645 (view_convert (plus @0 (minus (view_convert @1) @2)))
2646 (if (types_match (type, @0))
2647 (with { tree cst = const_binop (MINUS_EXPR, type, @1, @2); }
2648 (if (cst && !TREE_OVERFLOW (cst))
2649 (plus { cst; } @0)))))))
2651 /* ((T)(A)) + CST -> (T)(A + CST) */
2654 (plus (convert:s SSA_NAME@0) INTEGER_CST@1)
2655 (if (TREE_CODE (TREE_TYPE (@0)) == INTEGER_TYPE
2656 && TREE_CODE (type) == INTEGER_TYPE
2657 && TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (@0))
2658 && int_fits_type_p (@1, TREE_TYPE (@0)))
2659 /* Perform binary operation inside the cast if the constant fits
2660 and (A + CST)'s range does not overflow. */
2663 wi::overflow_type min_ovf = wi::OVF_OVERFLOW,
2664 max_ovf = wi::OVF_OVERFLOW;
2665 tree inner_type = TREE_TYPE (@0);
2668 = wide_int::from (wi::to_wide (@1), TYPE_PRECISION (inner_type),
2669 TYPE_SIGN (inner_type));
2672 if (get_global_range_query ()->range_of_expr (vr, @0)
2673 && vr.kind () == VR_RANGE)
2675 wide_int wmin0 = vr.lower_bound ();
2676 wide_int wmax0 = vr.upper_bound ();
2677 wi::add (wmin0, w1, TYPE_SIGN (inner_type), &min_ovf);
2678 wi::add (wmax0, w1, TYPE_SIGN (inner_type), &max_ovf);
2681 (if (min_ovf == wi::OVF_NONE && max_ovf == wi::OVF_NONE)
2682 (convert (plus @0 { wide_int_to_tree (TREE_TYPE (@0), w1); } )))
2686 /* ((T)(A + CST1)) + CST2 -> (T)(A) + (T)CST1 + CST2 */
2688 (for op (plus minus)
2690 (plus (convert:s (op:s @0 INTEGER_CST@1)) INTEGER_CST@2)
2691 (if (TREE_CODE (TREE_TYPE (@0)) == INTEGER_TYPE
2692 && TREE_CODE (type) == INTEGER_TYPE
2693 && TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (@0))
2694 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2695 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0))
2696 && TYPE_OVERFLOW_WRAPS (type))
2697 (plus (convert @0) (op @2 (convert @1))))))
2700 /* (T)(A) +- (T)(B) -> (T)(A +- B) only when (A +- B) could be simplified
2701 to a simple value. */
2703 (for op (plus minus)
2705 (op (convert @0) (convert @1))
2706 (if (INTEGRAL_TYPE_P (type)
2707 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
2708 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))
2709 && types_match (TREE_TYPE (@0), TREE_TYPE (@1))
2710 && !TYPE_OVERFLOW_TRAPS (type)
2711 && !TYPE_OVERFLOW_SANITIZED (type))
2712 (convert (op! @0 @1)))))
2717 (plus:c (bit_not @0) @0)
2718 (if (!TYPE_OVERFLOW_TRAPS (type))
2719 { build_all_ones_cst (type); }))
2723 (plus (convert? (bit_not @0)) integer_each_onep)
2724 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2725 (negate (convert @0))))
2729 (minus (convert? (negate @0)) integer_each_onep)
2730 (if (!TYPE_OVERFLOW_TRAPS (type)
2731 && tree_nop_conversion_p (type, TREE_TYPE (@0)))
2732 (bit_not (convert @0))))
2736 (minus integer_all_onesp @0)
2739 /* (T)(P + A) - (T)P -> (T) A */
2741 (minus (convert (plus:c @@0 @1))
2743 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
2744 /* For integer types, if A has a smaller type
2745 than T the result depends on the possible
2747 E.g. T=size_t, A=(unsigned)429497295, P>0.
2748 However, if an overflow in P + A would cause
2749 undefined behavior, we can assume that there
2751 || (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2752 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))))
2755 (minus (convert (pointer_plus @@0 @1))
2757 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
2758 /* For pointer types, if the conversion of A to the
2759 final type requires a sign- or zero-extension,
2760 then we have to punt - it is not defined which
2762 || (POINTER_TYPE_P (TREE_TYPE (@0))
2763 && TREE_CODE (@1) == INTEGER_CST
2764 && tree_int_cst_sign_bit (@1) == 0))
2767 (pointer_diff (pointer_plus @@0 @1) @0)
2768 /* The second argument of pointer_plus must be interpreted as signed, and
2769 thus sign-extended if necessary. */
2770 (with { tree stype = signed_type_for (TREE_TYPE (@1)); }
2771 /* Use view_convert instead of convert here, as POINTER_PLUS_EXPR
2772 second arg is unsigned even when we need to consider it as signed,
2773 we don't want to diagnose overflow here. */
2774 (convert (view_convert:stype @1))))
2776 /* (T)P - (T)(P + A) -> -(T) A */
2778 (minus (convert? @0)
2779 (convert (plus:c @@0 @1)))
2780 (if (INTEGRAL_TYPE_P (type)
2781 && TYPE_OVERFLOW_UNDEFINED (type)
2782 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
2783 (with { tree utype = unsigned_type_for (type); }
2784 (convert (negate (convert:utype @1))))
2785 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
2786 /* For integer types, if A has a smaller type
2787 than T the result depends on the possible
2789 E.g. T=size_t, A=(unsigned)429497295, P>0.
2790 However, if an overflow in P + A would cause
2791 undefined behavior, we can assume that there
2793 || (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2794 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))))
2795 (negate (convert @1)))))
2798 (convert (pointer_plus @@0 @1)))
2799 (if (INTEGRAL_TYPE_P (type)
2800 && TYPE_OVERFLOW_UNDEFINED (type)
2801 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
2802 (with { tree utype = unsigned_type_for (type); }
2803 (convert (negate (convert:utype @1))))
2804 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
2805 /* For pointer types, if the conversion of A to the
2806 final type requires a sign- or zero-extension,
2807 then we have to punt - it is not defined which
2809 || (POINTER_TYPE_P (TREE_TYPE (@0))
2810 && TREE_CODE (@1) == INTEGER_CST
2811 && tree_int_cst_sign_bit (@1) == 0))
2812 (negate (convert @1)))))
2814 (pointer_diff @0 (pointer_plus @@0 @1))
2815 /* The second argument of pointer_plus must be interpreted as signed, and
2816 thus sign-extended if necessary. */
2817 (with { tree stype = signed_type_for (TREE_TYPE (@1)); }
2818 /* Use view_convert instead of convert here, as POINTER_PLUS_EXPR
2819 second arg is unsigned even when we need to consider it as signed,
2820 we don't want to diagnose overflow here. */
2821 (negate (convert (view_convert:stype @1)))))
2823 /* (T)(P + A) - (T)(P + B) -> (T)A - (T)B */
2825 (minus (convert (plus:c @@0 @1))
2826 (convert (plus:c @0 @2)))
2827 (if (INTEGRAL_TYPE_P (type)
2828 && TYPE_OVERFLOW_UNDEFINED (type)
2829 && element_precision (type) <= element_precision (TREE_TYPE (@1))
2830 && element_precision (type) <= element_precision (TREE_TYPE (@2)))
2831 (with { tree utype = unsigned_type_for (type); }
2832 (convert (minus (convert:utype @1) (convert:utype @2))))
2833 (if (((element_precision (type) <= element_precision (TREE_TYPE (@1)))
2834 == (element_precision (type) <= element_precision (TREE_TYPE (@2))))
2835 && (element_precision (type) <= element_precision (TREE_TYPE (@1))
2836 /* For integer types, if A has a smaller type
2837 than T the result depends on the possible
2839 E.g. T=size_t, A=(unsigned)429497295, P>0.
2840 However, if an overflow in P + A would cause
2841 undefined behavior, we can assume that there
2843 || (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2844 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
2845 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))
2846 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@2)))))
2847 (minus (convert @1) (convert @2)))))
2849 (minus (convert (pointer_plus @@0 @1))
2850 (convert (pointer_plus @0 @2)))
2851 (if (INTEGRAL_TYPE_P (type)
2852 && TYPE_OVERFLOW_UNDEFINED (type)
2853 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
2854 (with { tree utype = unsigned_type_for (type); }
2855 (convert (minus (convert:utype @1) (convert:utype @2))))
2856 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
2857 /* For pointer types, if the conversion of A to the
2858 final type requires a sign- or zero-extension,
2859 then we have to punt - it is not defined which
2861 || (POINTER_TYPE_P (TREE_TYPE (@0))
2862 && TREE_CODE (@1) == INTEGER_CST
2863 && tree_int_cst_sign_bit (@1) == 0
2864 && TREE_CODE (@2) == INTEGER_CST
2865 && tree_int_cst_sign_bit (@2) == 0))
2866 (minus (convert @1) (convert @2)))))
2868 (pointer_diff (pointer_plus @0 @2) (pointer_plus @1 @2))
2869 (pointer_diff @0 @1))
2871 (pointer_diff (pointer_plus @@0 @1) (pointer_plus @0 @2))
2872 /* The second argument of pointer_plus must be interpreted as signed, and
2873 thus sign-extended if necessary. */
2874 (with { tree stype = signed_type_for (TREE_TYPE (@1)); }
2875 /* Use view_convert instead of convert here, as POINTER_PLUS_EXPR
2876 second arg is unsigned even when we need to consider it as signed,
2877 we don't want to diagnose overflow here. */
2878 (minus (convert (view_convert:stype @1))
2879 (convert (view_convert:stype @2)))))))
2881 /* (A * C) +- (B * C) -> (A+-B) * C and (A * C) +- A -> A * (C+-1).
2882 Modeled after fold_plusminus_mult_expr. */
2883 (if (!TYPE_SATURATING (type)
2884 && (!FLOAT_TYPE_P (type) || flag_associative_math))
2885 (for plusminus (plus minus)
2887 (plusminus (mult:cs@3 @0 @1) (mult:cs@4 @0 @2))
2888 (if (!ANY_INTEGRAL_TYPE_P (type)
2889 || TYPE_OVERFLOW_WRAPS (type)
2890 || (INTEGRAL_TYPE_P (type)
2891 && tree_expr_nonzero_p (@0)
2892 && expr_not_equal_to (@0, wi::minus_one (TYPE_PRECISION (type)))))
2893 (if (single_use (@3) || single_use (@4))
2894 /* If @1 +- @2 is constant require a hard single-use on either
2895 original operand (but not on both). */
2896 (mult (plusminus @1 @2) @0)
2898 (mult! (plusminus @1 @2) @0)
2901 /* We cannot generate constant 1 for fract. */
2902 (if (!ALL_FRACT_MODE_P (TYPE_MODE (type)))
2904 (plusminus @0 (mult:c@3 @0 @2))
2905 (if ((!ANY_INTEGRAL_TYPE_P (type)
2906 || TYPE_OVERFLOW_WRAPS (type)
2907 /* For @0 + @0*@2 this transformation would introduce UB
2908 (where there was none before) for @0 in [-1,0] and @2 max.
2909 For @0 - @0*@2 this transformation would introduce UB
2910 for @0 0 and @2 in [min,min+1] or @0 -1 and @2 min+1. */
2911 || (INTEGRAL_TYPE_P (type)
2912 && ((tree_expr_nonzero_p (@0)
2913 && expr_not_equal_to (@0,
2914 wi::minus_one (TYPE_PRECISION (type))))
2915 || (plusminus == PLUS_EXPR
2916 ? expr_not_equal_to (@2,
2917 wi::max_value (TYPE_PRECISION (type), SIGNED))
2918 /* Let's ignore the @0 -1 and @2 min case. */
2919 : (expr_not_equal_to (@2,
2920 wi::min_value (TYPE_PRECISION (type), SIGNED))
2921 && expr_not_equal_to (@2,
2922 wi::min_value (TYPE_PRECISION (type), SIGNED)
2925 (mult (plusminus { build_one_cst (type); } @2) @0)))
2927 (plusminus (mult:c@3 @0 @2) @0)
2928 (if ((!ANY_INTEGRAL_TYPE_P (type)
2929 || TYPE_OVERFLOW_WRAPS (type)
2930 /* For @0*@2 + @0 this transformation would introduce UB
2931 (where there was none before) for @0 in [-1,0] and @2 max.
2932 For @0*@2 - @0 this transformation would introduce UB
2933 for @0 0 and @2 min. */
2934 || (INTEGRAL_TYPE_P (type)
2935 && ((tree_expr_nonzero_p (@0)
2936 && (plusminus == MINUS_EXPR
2937 || expr_not_equal_to (@0,
2938 wi::minus_one (TYPE_PRECISION (type)))))
2939 || expr_not_equal_to (@2,
2940 (plusminus == PLUS_EXPR
2941 ? wi::max_value (TYPE_PRECISION (type), SIGNED)
2942 : wi::min_value (TYPE_PRECISION (type), SIGNED))))))
2944 (mult (plusminus @2 { build_one_cst (type); }) @0))))))
2947 /* Canonicalize X + (X << C) into X * (1 + (1 << C)) and
2948 (X << C1) + (X << C2) into X * ((1 << C1) + (1 << C2)). */
2950 (plus:c @0 (lshift:s @0 INTEGER_CST@1))
2951 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2952 && tree_fits_uhwi_p (@1)
2953 && tree_to_uhwi (@1) < element_precision (type)
2954 && (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2955 || optab_handler (smul_optab,
2956 TYPE_MODE (type)) != CODE_FOR_nothing))
2957 (with { tree t = type;
2958 if (!TYPE_OVERFLOW_WRAPS (t)) t = unsigned_type_for (t);
2959 wide_int w = wi::set_bit_in_zero (tree_to_uhwi (@1),
2960 element_precision (type));
2962 tree cst = wide_int_to_tree (VECTOR_TYPE_P (t) ? TREE_TYPE (t)
2964 cst = build_uniform_cst (t, cst); }
2965 (convert (mult (convert:t @0) { cst; })))))
2967 (plus (lshift:s @0 INTEGER_CST@1) (lshift:s @0 INTEGER_CST@2))
2968 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2969 && tree_fits_uhwi_p (@1)
2970 && tree_to_uhwi (@1) < element_precision (type)
2971 && tree_fits_uhwi_p (@2)
2972 && tree_to_uhwi (@2) < element_precision (type)
2973 && (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2974 || optab_handler (smul_optab,
2975 TYPE_MODE (type)) != CODE_FOR_nothing))
2976 (with { tree t = type;
2977 if (!TYPE_OVERFLOW_WRAPS (t)) t = unsigned_type_for (t);
2978 unsigned int prec = element_precision (type);
2979 wide_int w = wi::set_bit_in_zero (tree_to_uhwi (@1), prec);
2980 w += wi::set_bit_in_zero (tree_to_uhwi (@2), prec);
2981 tree cst = wide_int_to_tree (VECTOR_TYPE_P (t) ? TREE_TYPE (t)
2983 cst = build_uniform_cst (t, cst); }
2984 (convert (mult (convert:t @0) { cst; })))))
2987 /* Canonicalize (X*C1)|(X*C2) and (X*C1)^(X*C2) to (C1+C2)*X when
2988 tree_nonzero_bits allows IOR and XOR to be treated like PLUS.
2989 Likewise, handle (X<<C3) and X as legitimate variants of X*C. */
2990 (for op (bit_ior bit_xor)
2992 (op (mult:s@0 @1 INTEGER_CST@2)
2993 (mult:s@3 @1 INTEGER_CST@4))
2994 (if (INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_WRAPS (type)
2995 && (tree_nonzero_bits (@0) & tree_nonzero_bits (@3)) == 0)
2997 { wide_int_to_tree (type, wi::to_wide (@2) + wi::to_wide (@4)); })))
2999 (op:c (mult:s@0 @1 INTEGER_CST@2)
3000 (lshift:s@3 @1 INTEGER_CST@4))
3001 (if (INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_WRAPS (type)
3002 && tree_int_cst_sgn (@4) > 0
3003 && (tree_nonzero_bits (@0) & tree_nonzero_bits (@3)) == 0)
3004 (with { wide_int wone = wi::one (TYPE_PRECISION (type));
3005 wide_int c = wi::add (wi::to_wide (@2),
3006 wi::lshift (wone, wi::to_wide (@4))); }
3007 (mult @1 { wide_int_to_tree (type, c); }))))
3009 (op:c (mult:s@0 @1 INTEGER_CST@2)
3011 (if (INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_WRAPS (type)
3012 && (tree_nonzero_bits (@0) & tree_nonzero_bits (@1)) == 0)
3014 { wide_int_to_tree (type,
3015 wi::add (wi::to_wide (@2), 1)); })))
3017 (op (lshift:s@0 @1 INTEGER_CST@2)
3018 (lshift:s@3 @1 INTEGER_CST@4))
3019 (if (INTEGRAL_TYPE_P (type)
3020 && tree_int_cst_sgn (@2) > 0
3021 && tree_int_cst_sgn (@4) > 0
3022 && (tree_nonzero_bits (@0) & tree_nonzero_bits (@3)) == 0)
3023 (with { tree t = type;
3024 if (!TYPE_OVERFLOW_WRAPS (t))
3025 t = unsigned_type_for (t);
3026 wide_int wone = wi::one (TYPE_PRECISION (t));
3027 wide_int c = wi::add (wi::lshift (wone, wi::to_wide (@2)),
3028 wi::lshift (wone, wi::to_wide (@4))); }
3029 (convert (mult:t (convert:t @1) { wide_int_to_tree (t,c); })))))
3031 (op:c (lshift:s@0 @1 INTEGER_CST@2)
3033 (if (INTEGRAL_TYPE_P (type)
3034 && tree_int_cst_sgn (@2) > 0
3035 && (tree_nonzero_bits (@0) & tree_nonzero_bits (@1)) == 0)
3036 (with { tree t = type;
3037 if (!TYPE_OVERFLOW_WRAPS (t))
3038 t = unsigned_type_for (t);
3039 wide_int wone = wi::one (TYPE_PRECISION (t));
3040 wide_int c = wi::add (wi::lshift (wone, wi::to_wide (@2)), wone); }
3041 (convert (mult:t (convert:t @1) { wide_int_to_tree (t, c); }))))))
3043 /* Simplifications of MIN_EXPR, MAX_EXPR, fmin() and fmax(). */
3045 (for minmax (min max FMIN_ALL FMAX_ALL)
3049 /* min(max(x,y),y) -> y. */
3051 (min:c (max:c @0 @1) @1)
3053 /* max(min(x,y),y) -> y. */
3055 (max:c (min:c @0 @1) @1)
3057 /* max(a,-a) -> abs(a). */
3059 (max:c @0 (negate @0))
3060 (if (TREE_CODE (type) != COMPLEX_TYPE
3061 && (! ANY_INTEGRAL_TYPE_P (type)
3062 || TYPE_OVERFLOW_UNDEFINED (type)))
3064 /* min(a,-a) -> -abs(a). */
3066 (min:c @0 (negate @0))
3067 (if (TREE_CODE (type) != COMPLEX_TYPE
3068 && (! ANY_INTEGRAL_TYPE_P (type)
3069 || TYPE_OVERFLOW_UNDEFINED (type)))
3074 (if (INTEGRAL_TYPE_P (type)
3075 && TYPE_MIN_VALUE (type)
3076 && operand_equal_p (@1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
3078 (if (INTEGRAL_TYPE_P (type)
3079 && TYPE_MAX_VALUE (type)
3080 && operand_equal_p (@1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
3085 (if (INTEGRAL_TYPE_P (type)
3086 && TYPE_MAX_VALUE (type)
3087 && operand_equal_p (@1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
3089 (if (INTEGRAL_TYPE_P (type)
3090 && TYPE_MIN_VALUE (type)
3091 && operand_equal_p (@1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
3094 /* max (a, a + CST) -> a + CST where CST is positive. */
3095 /* max (a, a + CST) -> a where CST is negative. */
3097 (max:c @0 (plus@2 @0 INTEGER_CST@1))
3098 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
3099 (if (tree_int_cst_sgn (@1) > 0)
3103 /* min (a, a + CST) -> a where CST is positive. */
3104 /* min (a, a + CST) -> a + CST where CST is negative. */
3106 (min:c @0 (plus@2 @0 INTEGER_CST@1))
3107 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
3108 (if (tree_int_cst_sgn (@1) > 0)
3112 /* Simplify min (&var[off0], &var[off1]) etc. depending on whether
3113 the addresses are known to be less, equal or greater. */
3114 (for minmax (min max)
3117 (minmax (convert1?@2 addr@0) (convert2?@3 addr@1))
3120 poly_int64 off0, off1;
3122 int equal = address_compare (cmp, TREE_TYPE (@2), @0, @1, base0, base1,
3123 off0, off1, GENERIC);
3126 (if (minmax == MIN_EXPR)
3127 (if (known_le (off0, off1))
3129 (if (known_gt (off0, off1))
3131 (if (known_ge (off0, off1))
3133 (if (known_lt (off0, off1))
3136 /* (convert (minmax ((convert (x) c)))) -> minmax (x c) if x is promoted
3137 and the outer convert demotes the expression back to x's type. */
3138 (for minmax (min max)
3140 (convert (minmax@0 (convert @1) INTEGER_CST@2))
3141 (if (INTEGRAL_TYPE_P (type)
3142 && types_match (@1, type) && int_fits_type_p (@2, type)
3143 && TYPE_SIGN (TREE_TYPE (@0)) == TYPE_SIGN (type)
3144 && TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type))
3145 (minmax @1 (convert @2)))))
3147 (for minmax (FMIN_ALL FMAX_ALL)
3148 /* If either argument is NaN, return the other one. Avoid the
3149 transformation if we get (and honor) a signalling NaN. */
3151 (minmax:c @0 REAL_CST@1)
3152 (if (real_isnan (TREE_REAL_CST_PTR (@1))
3153 && (!HONOR_SNANS (@1) || !TREE_REAL_CST (@1).signalling))
3155 /* Convert fmin/fmax to MIN_EXPR/MAX_EXPR. C99 requires these
3156 functions to return the numeric arg if the other one is NaN.
3157 MIN and MAX don't honor that, so only transform if -ffinite-math-only
3158 is set. C99 doesn't require -0.0 to be handled, so we don't have to
3159 worry about it either. */
3160 (if (flag_finite_math_only)
3167 /* min (-A, -B) -> -max (A, B) */
3168 (for minmax (min max FMIN_ALL FMAX_ALL)
3169 maxmin (max min FMAX_ALL FMIN_ALL)
3171 (minmax (negate:s@2 @0) (negate:s@3 @1))
3172 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
3173 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3174 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
3175 (negate (maxmin @0 @1)))))
3176 /* MIN (~X, ~Y) -> ~MAX (X, Y)
3177 MAX (~X, ~Y) -> ~MIN (X, Y) */
3178 (for minmax (min max)
3181 (minmax (bit_not:s@2 @0) (bit_not:s@3 @1))
3182 (bit_not (maxmin @0 @1))))
3184 /* MIN (X, Y) == X -> X <= Y */
3185 (for minmax (min min max max)
3189 (cmp:c (minmax:c @0 @1) @0)
3190 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0)))
3192 /* MIN (X, 5) == 0 -> X == 0
3193 MIN (X, 5) == 7 -> false */
3196 (cmp (min @0 INTEGER_CST@1) INTEGER_CST@2)
3197 (if (wi::lt_p (wi::to_wide (@1), wi::to_wide (@2),
3198 TYPE_SIGN (TREE_TYPE (@0))))
3199 { constant_boolean_node (cmp == NE_EXPR, type); }
3200 (if (wi::gt_p (wi::to_wide (@1), wi::to_wide (@2),
3201 TYPE_SIGN (TREE_TYPE (@0))))
3205 (cmp (max @0 INTEGER_CST@1) INTEGER_CST@2)
3206 (if (wi::gt_p (wi::to_wide (@1), wi::to_wide (@2),
3207 TYPE_SIGN (TREE_TYPE (@0))))
3208 { constant_boolean_node (cmp == NE_EXPR, type); }
3209 (if (wi::lt_p (wi::to_wide (@1), wi::to_wide (@2),
3210 TYPE_SIGN (TREE_TYPE (@0))))
3212 /* MIN (X, C1) < C2 -> X < C2 || C1 < C2 */
3213 (for minmax (min min max max min min max max )
3214 cmp (lt le gt ge gt ge lt le )
3215 comb (bit_ior bit_ior bit_ior bit_ior bit_and bit_and bit_and bit_and)
3217 (cmp (minmax @0 INTEGER_CST@1) INTEGER_CST@2)
3218 (comb (cmp @0 @2) (cmp @1 @2))))
3220 /* X <= MAX(X, Y) -> true
3221 X > MAX(X, Y) -> false
3222 X >= MIN(X, Y) -> true
3223 X < MIN(X, Y) -> false */
3224 (for minmax (min min max max )
3227 (cmp @0 (minmax:c @0 @1))
3228 { constant_boolean_node (cmp == GE_EXPR || cmp == LE_EXPR, type); } ))
3230 /* Undo fancy way of writing max/min or other ?: expressions,
3231 like a - ((a - b) & -(a < b)), in this case into (a < b) ? b : a.
3232 People normally use ?: and that is what we actually try to optimize. */
3233 (for cmp (simple_comparison)
3235 (minus @0 (bit_and:c (minus @0 @1)
3236 (convert? (negate@4 (convert? (cmp@5 @2 @3))))))
3237 (if (INTEGRAL_TYPE_P (type)
3238 && INTEGRAL_TYPE_P (TREE_TYPE (@4))
3239 && TREE_CODE (TREE_TYPE (@4)) != BOOLEAN_TYPE
3240 && INTEGRAL_TYPE_P (TREE_TYPE (@5))
3241 && (TYPE_PRECISION (TREE_TYPE (@4)) >= TYPE_PRECISION (type)
3242 || !TYPE_UNSIGNED (TREE_TYPE (@4)))
3243 && (GIMPLE || !TREE_SIDE_EFFECTS (@1)))
3244 (cond (cmp @2 @3) @1 @0)))
3246 (plus:c @0 (bit_and:c (minus @1 @0)
3247 (convert? (negate@4 (convert? (cmp@5 @2 @3))))))
3248 (if (INTEGRAL_TYPE_P (type)
3249 && INTEGRAL_TYPE_P (TREE_TYPE (@4))
3250 && TREE_CODE (TREE_TYPE (@4)) != BOOLEAN_TYPE
3251 && INTEGRAL_TYPE_P (TREE_TYPE (@5))
3252 && (TYPE_PRECISION (TREE_TYPE (@4)) >= TYPE_PRECISION (type)
3253 || !TYPE_UNSIGNED (TREE_TYPE (@4)))
3254 && (GIMPLE || !TREE_SIDE_EFFECTS (@1)))
3255 (cond (cmp @2 @3) @1 @0)))
3256 /* Similarly with ^ instead of - though in that case with :c. */
3258 (bit_xor:c @0 (bit_and:c (bit_xor:c @0 @1)
3259 (convert? (negate@4 (convert? (cmp@5 @2 @3))))))
3260 (if (INTEGRAL_TYPE_P (type)
3261 && INTEGRAL_TYPE_P (TREE_TYPE (@4))
3262 && TREE_CODE (TREE_TYPE (@4)) != BOOLEAN_TYPE
3263 && INTEGRAL_TYPE_P (TREE_TYPE (@5))
3264 && (TYPE_PRECISION (TREE_TYPE (@4)) >= TYPE_PRECISION (type)
3265 || !TYPE_UNSIGNED (TREE_TYPE (@4)))
3266 && (GIMPLE || !TREE_SIDE_EFFECTS (@1)))
3267 (cond (cmp @2 @3) @1 @0))))
3269 /* Simplifications of shift and rotates. */
3271 (for rotate (lrotate rrotate)
3273 (rotate integer_all_onesp@0 @1)
3276 /* Optimize -1 >> x for arithmetic right shifts. */
3278 (rshift integer_all_onesp@0 @1)
3279 (if (!TYPE_UNSIGNED (type))
3282 /* Optimize (x >> c) << c into x & (-1<<c). */
3284 (lshift (nop_convert? (rshift @0 INTEGER_CST@1)) @1)
3285 (if (wi::ltu_p (wi::to_wide (@1), element_precision (type)))
3286 /* It doesn't matter if the right shift is arithmetic or logical. */
3287 (bit_and (view_convert @0) (lshift { build_minus_one_cst (type); } @1))))
3290 (lshift (convert (convert@2 (rshift @0 INTEGER_CST@1))) @1)
3291 (if (wi::ltu_p (wi::to_wide (@1), element_precision (type))
3292 /* Allow intermediate conversion to integral type with whatever sign, as
3293 long as the low TYPE_PRECISION (type)
3294 - TYPE_PRECISION (TREE_TYPE (@2)) bits are preserved. */
3295 && INTEGRAL_TYPE_P (type)
3296 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
3297 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
3298 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (@0))
3299 && (TYPE_PRECISION (TREE_TYPE (@2)) >= TYPE_PRECISION (type)
3300 || wi::geu_p (wi::to_wide (@1),
3301 TYPE_PRECISION (type)
3302 - TYPE_PRECISION (TREE_TYPE (@2)))))
3303 (bit_and (convert @0) (lshift { build_minus_one_cst (type); } @1))))
3305 /* Optimize (x << c) >> c into x & ((unsigned)-1 >> c) for unsigned
3308 (rshift (lshift @0 INTEGER_CST@1) @1)
3309 (if (TYPE_UNSIGNED (type)
3310 && (wi::ltu_p (wi::to_wide (@1), element_precision (type))))
3311 (bit_and @0 (rshift { build_minus_one_cst (type); } @1))))
3313 /* Optimize x >> x into 0 */
3316 { build_zero_cst (type); })
3318 (for shiftrotate (lrotate rrotate lshift rshift)
3320 (shiftrotate @0 integer_zerop)
3323 (shiftrotate integer_zerop@0 @1)
3325 /* Prefer vector1 << scalar to vector1 << vector2
3326 if vector2 is uniform. */
3327 (for vec (VECTOR_CST CONSTRUCTOR)
3329 (shiftrotate @0 vec@1)
3330 (with { tree tem = uniform_vector_p (@1); }
3332 (shiftrotate @0 { tem; }))))))
3334 /* Simplify X << Y where Y's low width bits are 0 to X, as only valid
3335 Y is 0. Similarly for X >> Y. */
3337 (for shift (lshift rshift)
3339 (shift @0 SSA_NAME@1)
3340 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1)))
3342 int width = ceil_log2 (element_precision (TREE_TYPE (@0)));
3343 int prec = TYPE_PRECISION (TREE_TYPE (@1));
3345 (if ((get_nonzero_bits (@1) & wi::mask (width, false, prec)) == 0)
3349 /* Rewrite an LROTATE_EXPR by a constant into an
3350 RROTATE_EXPR by a new constant. */
3352 (lrotate @0 INTEGER_CST@1)
3353 (rrotate @0 { const_binop (MINUS_EXPR, TREE_TYPE (@1),
3354 build_int_cst (TREE_TYPE (@1),
3355 element_precision (type)), @1); }))
3357 /* Turn (a OP c1) OP c2 into a OP (c1+c2). */
3358 (for op (lrotate rrotate rshift lshift)
3360 (op (op @0 INTEGER_CST@1) INTEGER_CST@2)
3361 (with { unsigned int prec = element_precision (type); }
3362 (if (wi::ge_p (wi::to_wide (@1), 0, TYPE_SIGN (TREE_TYPE (@1)))
3363 && wi::lt_p (wi::to_wide (@1), prec, TYPE_SIGN (TREE_TYPE (@1)))
3364 && wi::ge_p (wi::to_wide (@2), 0, TYPE_SIGN (TREE_TYPE (@2)))
3365 && wi::lt_p (wi::to_wide (@2), prec, TYPE_SIGN (TREE_TYPE (@2))))
3366 (with { unsigned int low = (tree_to_uhwi (@1)
3367 + tree_to_uhwi (@2)); }
3368 /* Deal with a OP (c1 + c2) being undefined but (a OP c1) OP c2
3369 being well defined. */
3371 (if (op == LROTATE_EXPR || op == RROTATE_EXPR)
3372 (op @0 { build_int_cst (TREE_TYPE (@1), low % prec); })
3373 (if (TYPE_UNSIGNED (type) || op == LSHIFT_EXPR)
3374 { build_zero_cst (type); }
3375 (op @0 { build_int_cst (TREE_TYPE (@1), prec - 1); })))
3376 (op @0 { build_int_cst (TREE_TYPE (@1), low); })))))))
3379 /* Simplify (CST << x) & 1 to 0 if CST is even or to x == 0 if it is odd. */
3381 (bit_and (lshift INTEGER_CST@1 @0) integer_onep)
3382 (if ((wi::to_wide (@1) & 1) != 0)
3383 (convert (eq:boolean_type_node @0 { build_zero_cst (TREE_TYPE (@0)); }))
3384 { build_zero_cst (type); }))
3386 /* Simplify ((C << x) & D) != 0 where C and D are power of two constants,
3387 either to false if D is smaller (unsigned comparison) than C, or to
3388 x == log2 (D) - log2 (C). Similarly for right shifts. */
3392 (cmp (bit_and (lshift integer_pow2p@1 @0) integer_pow2p@2) integer_zerop)
3393 (with { int c1 = wi::clz (wi::to_wide (@1));
3394 int c2 = wi::clz (wi::to_wide (@2)); }
3396 { constant_boolean_node (cmp == NE_EXPR ? false : true, type); }
3397 (icmp @0 { build_int_cst (TREE_TYPE (@0), c1 - c2); }))))
3399 (cmp (bit_and (rshift integer_pow2p@1 @0) integer_pow2p@2) integer_zerop)
3400 (if (tree_int_cst_sgn (@1) > 0)
3401 (with { int c1 = wi::clz (wi::to_wide (@1));
3402 int c2 = wi::clz (wi::to_wide (@2)); }
3404 { constant_boolean_node (cmp == NE_EXPR ? false : true, type); }
3405 (icmp @0 { build_int_cst (TREE_TYPE (@0), c2 - c1); }))))))
3407 /* (CST1 << A) == CST2 -> A == ctz (CST2) - ctz (CST1)
3408 (CST1 << A) != CST2 -> A != ctz (CST2) - ctz (CST1)
3412 (cmp (lshift INTEGER_CST@0 @1) INTEGER_CST@2)
3413 (with { int cand = wi::ctz (wi::to_wide (@2)) - wi::ctz (wi::to_wide (@0)); }
3415 || (!integer_zerop (@2)
3416 && wi::lshift (wi::to_wide (@0), cand) != wi::to_wide (@2)))
3417 { constant_boolean_node (cmp == NE_EXPR, type); }
3418 (if (!integer_zerop (@2)
3419 && wi::lshift (wi::to_wide (@0), cand) == wi::to_wide (@2))
3420 (cmp @1 { build_int_cst (TREE_TYPE (@1), cand); }))))))
3422 /* Fold (X << C1) & C2 into (X << C1) & (C2 | ((1 << C1) - 1))
3423 (X >> C1) & C2 into (X >> C1) & (C2 | ~((type) -1 >> C1))
3424 if the new mask might be further optimized. */
3425 (for shift (lshift rshift)
3427 (bit_and (convert?:s@4 (shift:s@5 (convert1?@3 @0) INTEGER_CST@1))
3429 (if (tree_nop_conversion_p (TREE_TYPE (@4), TREE_TYPE (@5))
3430 && TYPE_PRECISION (type) <= HOST_BITS_PER_WIDE_INT
3431 && tree_fits_uhwi_p (@1)
3432 && tree_to_uhwi (@1) > 0
3433 && tree_to_uhwi (@1) < TYPE_PRECISION (type))
3436 unsigned int shiftc = tree_to_uhwi (@1);
3437 unsigned HOST_WIDE_INT mask = TREE_INT_CST_LOW (@2);
3438 unsigned HOST_WIDE_INT newmask, zerobits = 0;
3439 tree shift_type = TREE_TYPE (@3);
3442 if (shift == LSHIFT_EXPR)
3443 zerobits = ((HOST_WIDE_INT_1U << shiftc) - 1);
3444 else if (shift == RSHIFT_EXPR
3445 && type_has_mode_precision_p (shift_type))
3447 prec = TYPE_PRECISION (TREE_TYPE (@3));
3449 /* See if more bits can be proven as zero because of
3452 && TYPE_UNSIGNED (TREE_TYPE (@0)))
3454 tree inner_type = TREE_TYPE (@0);
3455 if (type_has_mode_precision_p (inner_type)
3456 && TYPE_PRECISION (inner_type) < prec)
3458 prec = TYPE_PRECISION (inner_type);
3459 /* See if we can shorten the right shift. */
3461 shift_type = inner_type;
3462 /* Otherwise X >> C1 is all zeros, so we'll optimize
3463 it into (X, 0) later on by making sure zerobits
3467 zerobits = HOST_WIDE_INT_M1U;
3470 zerobits >>= HOST_BITS_PER_WIDE_INT - shiftc;
3471 zerobits <<= prec - shiftc;
3473 /* For arithmetic shift if sign bit could be set, zerobits
3474 can contain actually sign bits, so no transformation is
3475 possible, unless MASK masks them all away. In that
3476 case the shift needs to be converted into logical shift. */
3477 if (!TYPE_UNSIGNED (TREE_TYPE (@3))
3478 && prec == TYPE_PRECISION (TREE_TYPE (@3)))
3480 if ((mask & zerobits) == 0)
3481 shift_type = unsigned_type_for (TREE_TYPE (@3));
3487 /* ((X << 16) & 0xff00) is (X, 0). */
3488 (if ((mask & zerobits) == mask)
3489 { build_int_cst (type, 0); }
3490 (with { newmask = mask | zerobits; }
3491 (if (newmask != mask && (newmask & (newmask + 1)) == 0)
3494 /* Only do the transformation if NEWMASK is some integer
3496 for (prec = BITS_PER_UNIT;
3497 prec < HOST_BITS_PER_WIDE_INT; prec <<= 1)
3498 if (newmask == (HOST_WIDE_INT_1U << prec) - 1)
3501 (if (prec < HOST_BITS_PER_WIDE_INT
3502 || newmask == HOST_WIDE_INT_M1U)
3504 { tree newmaskt = build_int_cst_type (TREE_TYPE (@2), newmask); }
3505 (if (!tree_int_cst_equal (newmaskt, @2))
3506 (if (shift_type != TREE_TYPE (@3))
3507 (bit_and (convert (shift:shift_type (convert @3) @1)) { newmaskt; })
3508 (bit_and @4 { newmaskt; })))))))))))))
3510 /* ((1 << n) & M) != 0 -> n == log2 (M) */
3516 (nop_convert? (lshift integer_onep @0)) integer_pow2p@1) integer_zerop)
3517 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)))
3518 (icmp @0 { wide_int_to_tree (TREE_TYPE (@0),
3519 wi::exact_log2 (wi::to_wide (@1))); }))))
3521 /* Fold (X {&,^,|} C2) << C1 into (X << C1) {&,^,|} (C2 << C1)
3522 (X {&,^,|} C2) >> C1 into (X >> C1) & (C2 >> C1). */
3523 (for shift (lshift rshift)
3524 (for bit_op (bit_and bit_xor bit_ior)
3526 (shift (convert?:s (bit_op:s @0 INTEGER_CST@2)) INTEGER_CST@1)
3527 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
3528 (with { tree mask = int_const_binop (shift, fold_convert (type, @2), @1); }
3530 (bit_op (shift (convert @0) @1) { mask; })))))))
3532 /* ~(~X >> Y) -> X >> Y (for arithmetic shift). */
3534 (bit_not (convert1?:s (rshift:s (convert2?@0 (bit_not @1)) @2)))
3535 (if (!TYPE_UNSIGNED (TREE_TYPE (@0))
3536 && (element_precision (TREE_TYPE (@0))
3537 <= element_precision (TREE_TYPE (@1))
3538 || !TYPE_UNSIGNED (TREE_TYPE (@1))))
3540 { tree shift_type = TREE_TYPE (@0); }
3541 (convert (rshift (convert:shift_type @1) @2)))))
3543 /* ~(~X >>r Y) -> X >>r Y
3544 ~(~X <<r Y) -> X <<r Y */
3545 (for rotate (lrotate rrotate)
3547 (bit_not (convert1?:s (rotate:s (convert2?@0 (bit_not @1)) @2)))
3548 (if ((element_precision (TREE_TYPE (@0))
3549 <= element_precision (TREE_TYPE (@1))
3550 || !TYPE_UNSIGNED (TREE_TYPE (@1)))
3551 && (element_precision (type) <= element_precision (TREE_TYPE (@0))
3552 || !TYPE_UNSIGNED (TREE_TYPE (@0))))
3554 { tree rotate_type = TREE_TYPE (@0); }
3555 (convert (rotate (convert:rotate_type @1) @2))))))
3558 (for rotate (lrotate rrotate)
3559 invrot (rrotate lrotate)
3560 /* (X >>r Y) cmp (Z >>r Y) may simplify to X cmp Y. */
3562 (cmp (rotate @1 @0) (rotate @2 @0))
3564 /* (X >>r C1) cmp C2 may simplify to X cmp C3. */
3566 (cmp (rotate @0 INTEGER_CST@1) INTEGER_CST@2)
3567 (cmp @0 { const_binop (invrot, TREE_TYPE (@0), @2, @1); }))
3568 /* (X >>r Y) cmp C where C is 0 or ~0, may simplify to X cmp C. */
3570 (cmp (rotate @0 @1) INTEGER_CST@2)
3571 (if (integer_zerop (@2) || integer_all_onesp (@2))
3574 /* Both signed and unsigned lshift produce the same result, so use
3575 the form that minimizes the number of conversions. Postpone this
3576 transformation until after shifts by zero have been folded. */
3578 (convert (lshift:s@0 (convert:s@1 @2) INTEGER_CST@3))
3579 (if (INTEGRAL_TYPE_P (type)
3580 && tree_nop_conversion_p (type, TREE_TYPE (@0))
3581 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
3582 && TYPE_PRECISION (TREE_TYPE (@2)) <= TYPE_PRECISION (type)
3583 && !integer_zerop (@3))
3584 (lshift (convert @2) @3)))
3586 /* Simplifications of conversions. */
3588 /* Basic strip-useless-type-conversions / strip_nops. */
3589 (for cvt (convert view_convert float fix_trunc)
3592 (if ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@0)))
3593 || (GENERIC && type == TREE_TYPE (@0)))
3596 /* Contract view-conversions. */
3598 (view_convert (view_convert @0))
3601 /* For integral conversions with the same precision or pointer
3602 conversions use a NOP_EXPR instead. */
3605 (if ((INTEGRAL_TYPE_P (type) || POINTER_TYPE_P (type))
3606 && (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
3607 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (@0)))
3610 /* Strip inner integral conversions that do not change precision or size, or
3611 zero-extend while keeping the same size (for bool-to-char). */
3613 (view_convert (convert@0 @1))
3614 (if ((INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
3615 && (INTEGRAL_TYPE_P (TREE_TYPE (@1)) || POINTER_TYPE_P (TREE_TYPE (@1)))
3616 && TYPE_SIZE (TREE_TYPE (@0)) == TYPE_SIZE (TREE_TYPE (@1))
3617 && (TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1))
3618 || (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (TREE_TYPE (@1))
3619 && TYPE_UNSIGNED (TREE_TYPE (@1)))))
3622 /* Simplify a view-converted empty constructor. */
3624 (view_convert CONSTRUCTOR@0)
3625 (if (TREE_CODE (@0) != SSA_NAME
3626 && CONSTRUCTOR_NELTS (@0) == 0)
3627 { build_zero_cst (type); }))
3629 /* Re-association barriers around constants and other re-association
3630 barriers can be removed. */
3632 (paren CONSTANT_CLASS_P@0)
3635 (paren (paren@1 @0))
3638 /* Handle cases of two conversions in a row. */
3639 (for ocvt (convert float fix_trunc)
3640 (for icvt (convert float)
3645 tree inside_type = TREE_TYPE (@0);
3646 tree inter_type = TREE_TYPE (@1);
3647 int inside_int = INTEGRAL_TYPE_P (inside_type);
3648 int inside_ptr = POINTER_TYPE_P (inside_type);
3649 int inside_float = FLOAT_TYPE_P (inside_type);
3650 int inside_vec = VECTOR_TYPE_P (inside_type);
3651 unsigned int inside_prec = TYPE_PRECISION (inside_type);
3652 int inside_unsignedp = TYPE_UNSIGNED (inside_type);
3653 int inter_int = INTEGRAL_TYPE_P (inter_type);
3654 int inter_ptr = POINTER_TYPE_P (inter_type);
3655 int inter_float = FLOAT_TYPE_P (inter_type);
3656 int inter_vec = VECTOR_TYPE_P (inter_type);
3657 unsigned int inter_prec = TYPE_PRECISION (inter_type);
3658 int inter_unsignedp = TYPE_UNSIGNED (inter_type);
3659 int final_int = INTEGRAL_TYPE_P (type);
3660 int final_ptr = POINTER_TYPE_P (type);
3661 int final_float = FLOAT_TYPE_P (type);
3662 int final_vec = VECTOR_TYPE_P (type);
3663 unsigned int final_prec = TYPE_PRECISION (type);
3664 int final_unsignedp = TYPE_UNSIGNED (type);
3667 /* In addition to the cases of two conversions in a row
3668 handled below, if we are converting something to its own
3669 type via an object of identical or wider precision, neither
3670 conversion is needed. */
3671 (if (((GIMPLE && useless_type_conversion_p (type, inside_type))
3673 && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (inside_type)))
3674 && (((inter_int || inter_ptr) && final_int)
3675 || (inter_float && final_float))
3676 && inter_prec >= final_prec)
3679 /* Likewise, if the intermediate and initial types are either both
3680 float or both integer, we don't need the middle conversion if the
3681 former is wider than the latter and doesn't change the signedness
3682 (for integers). Avoid this if the final type is a pointer since
3683 then we sometimes need the middle conversion. */
3684 (if (((inter_int && inside_int) || (inter_float && inside_float))
3685 && (final_int || final_float)
3686 && inter_prec >= inside_prec
3687 && (inter_float || inter_unsignedp == inside_unsignedp))
3690 /* If we have a sign-extension of a zero-extended value, we can
3691 replace that by a single zero-extension. Likewise if the
3692 final conversion does not change precision we can drop the
3693 intermediate conversion. */
3694 (if (inside_int && inter_int && final_int
3695 && ((inside_prec < inter_prec && inter_prec < final_prec
3696 && inside_unsignedp && !inter_unsignedp)
3697 || final_prec == inter_prec))
3700 /* Two conversions in a row are not needed unless:
3701 - some conversion is floating-point (overstrict for now), or
3702 - some conversion is a vector (overstrict for now), or
3703 - the intermediate type is narrower than both initial and
3705 - the intermediate type and innermost type differ in signedness,
3706 and the outermost type is wider than the intermediate, or
3707 - the initial type is a pointer type and the precisions of the
3708 intermediate and final types differ, or
3709 - the final type is a pointer type and the precisions of the
3710 initial and intermediate types differ. */
3711 (if (! inside_float && ! inter_float && ! final_float
3712 && ! inside_vec && ! inter_vec && ! final_vec
3713 && (inter_prec >= inside_prec || inter_prec >= final_prec)
3714 && ! (inside_int && inter_int
3715 && inter_unsignedp != inside_unsignedp
3716 && inter_prec < final_prec)
3717 && ((inter_unsignedp && inter_prec > inside_prec)
3718 == (final_unsignedp && final_prec > inter_prec))
3719 && ! (inside_ptr && inter_prec != final_prec)
3720 && ! (final_ptr && inside_prec != inter_prec))
3723 /* A truncation to an unsigned type (a zero-extension) should be
3724 canonicalized as bitwise and of a mask. */
3725 (if (GIMPLE /* PR70366: doing this in GENERIC breaks -Wconversion. */
3726 && final_int && inter_int && inside_int
3727 && final_prec == inside_prec
3728 && final_prec > inter_prec
3730 (convert (bit_and @0 { wide_int_to_tree
3732 wi::mask (inter_prec, false,
3733 TYPE_PRECISION (inside_type))); })))
3735 /* If we are converting an integer to a floating-point that can
3736 represent it exactly and back to an integer, we can skip the
3737 floating-point conversion. */
3738 (if (GIMPLE /* PR66211 */
3739 && inside_int && inter_float && final_int &&
3740 (unsigned) significand_size (TYPE_MODE (inter_type))
3741 >= inside_prec - !inside_unsignedp)
3744 /* (float_type)(integer_type) x -> trunc (x) if the type of x matches
3745 float_type. Only do the transformation if we do not need to preserve
3746 trapping behaviour, so require !flag_trapping_math. */
3749 (float (fix_trunc @0))
3750 (if (!flag_trapping_math
3751 && types_match (type, TREE_TYPE (@0))
3752 && direct_internal_fn_supported_p (IFN_TRUNC, type,
3757 /* If we have a narrowing conversion to an integral type that is fed by a
3758 BIT_AND_EXPR, we might be able to remove the BIT_AND_EXPR if it merely
3759 masks off bits outside the final type (and nothing else). */
3761 (convert (bit_and @0 INTEGER_CST@1))
3762 (if (INTEGRAL_TYPE_P (type)
3763 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
3764 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))
3765 && operand_equal_p (@1, build_low_bits_mask (TREE_TYPE (@1),
3766 TYPE_PRECISION (type)), 0))
3770 /* (X /[ex] A) * A -> X. */
3772 (mult (convert1? (exact_div @0 @@1)) (convert2? @1))
3775 /* Simplify (A / B) * B + (A % B) -> A. */
3776 (for div (trunc_div ceil_div floor_div round_div)
3777 mod (trunc_mod ceil_mod floor_mod round_mod)
3779 (plus:c (mult:c (div @0 @1) @1) (mod @0 @1))
3782 /* ((X /[ex] A) +- B) * A --> X +- A * B. */
3783 (for op (plus minus)
3785 (mult (convert1? (op (convert2? (exact_div @0 INTEGER_CST@@1)) INTEGER_CST@2)) @1)
3786 (if (tree_nop_conversion_p (type, TREE_TYPE (@2))
3787 && tree_nop_conversion_p (TREE_TYPE (@0), TREE_TYPE (@2)))
3790 wi::overflow_type overflow;
3791 wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@2),
3792 TYPE_SIGN (type), &overflow);
3794 (if (types_match (type, TREE_TYPE (@2))
3795 && types_match (TREE_TYPE (@0), TREE_TYPE (@2)) && !overflow)
3796 (op @0 { wide_int_to_tree (type, mul); })
3797 (with { tree utype = unsigned_type_for (type); }
3798 (convert (op (convert:utype @0)
3799 (mult (convert:utype @1) (convert:utype @2))))))))))
3801 /* Canonicalization of binary operations. */
3803 /* Convert X + -C into X - C. */
3805 (plus @0 REAL_CST@1)
3806 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
3807 (with { tree tem = const_unop (NEGATE_EXPR, type, @1); }
3808 (if (!TREE_OVERFLOW (tem) || !flag_trapping_math)
3809 (minus @0 { tem; })))))
3811 /* Convert x+x into x*2. */
3814 (if (SCALAR_FLOAT_TYPE_P (type))
3815 (mult @0 { build_real (type, dconst2); })
3816 (if (INTEGRAL_TYPE_P (type))
3817 (mult @0 { build_int_cst (type, 2); }))))
3821 (minus integer_zerop @1)
3824 (pointer_diff integer_zerop @1)
3825 (negate (convert @1)))
3827 /* (ARG0 - ARG1) is the same as (-ARG1 + ARG0). So check whether
3828 ARG0 is zero and X + ARG0 reduces to X, since that would mean
3829 (-ARG1 + ARG0) reduces to -ARG1. */
3831 (minus real_zerop@0 @1)
3832 (if (fold_real_zero_addition_p (type, @1, @0, 0))
3835 /* Transform x * -1 into -x. */
3837 (mult @0 integer_minus_onep)
3840 /* Reassociate (X * CST) * Y to (X * Y) * CST. This does not introduce
3841 signed overflow for CST != 0 && CST != -1. */
3843 (mult:c (mult:s@3 @0 INTEGER_CST@1) @2)
3844 (if (TREE_CODE (@2) != INTEGER_CST
3846 && !integer_zerop (@1) && !integer_minus_onep (@1))
3847 (mult (mult @0 @2) @1)))
3849 /* True if we can easily extract the real and imaginary parts of a complex
3851 (match compositional_complex
3852 (convert? (complex @0 @1)))
3854 /* COMPLEX_EXPR and REALPART/IMAGPART_EXPR cancellations. */
3856 (complex (realpart @0) (imagpart @0))
3859 (realpart (complex @0 @1))
3862 (imagpart (complex @0 @1))
3865 /* Sometimes we only care about half of a complex expression. */
3867 (realpart (convert?:s (conj:s @0)))
3868 (convert (realpart @0)))
3870 (imagpart (convert?:s (conj:s @0)))
3871 (convert (negate (imagpart @0))))
3872 (for part (realpart imagpart)
3873 (for op (plus minus)
3875 (part (convert?:s@2 (op:s @0 @1)))
3876 (convert (op (part @0) (part @1))))))
3878 (realpart (convert?:s (CEXPI:s @0)))
3881 (imagpart (convert?:s (CEXPI:s @0)))
3884 /* conj(conj(x)) -> x */
3886 (conj (convert? (conj @0)))
3887 (if (tree_nop_conversion_p (TREE_TYPE (@0), type))
3890 /* conj({x,y}) -> {x,-y} */
3892 (conj (convert?:s (complex:s @0 @1)))
3893 (with { tree itype = TREE_TYPE (type); }
3894 (complex (convert:itype @0) (negate (convert:itype @1)))))
3896 /* BSWAP simplifications, transforms checked by gcc.dg/builtin-bswap-8.c. */
3897 (for bswap (BUILT_IN_BSWAP16 BUILT_IN_BSWAP32
3898 BUILT_IN_BSWAP64 BUILT_IN_BSWAP128)
3903 (bswap (bit_not (bswap @0)))
3905 (for bitop (bit_xor bit_ior bit_and)
3907 (bswap (bitop:c (bswap @0) @1))
3908 (bitop @0 (bswap @1))))
3911 (cmp (bswap@2 @0) (bswap @1))
3912 (with { tree ctype = TREE_TYPE (@2); }
3913 (cmp (convert:ctype @0) (convert:ctype @1))))
3915 (cmp (bswap @0) INTEGER_CST@1)
3916 (with { tree ctype = TREE_TYPE (@1); }
3917 (cmp (convert:ctype @0) (bswap @1)))))
3918 /* (bswap(x) >> C1) & C2 can sometimes be simplified to (x >> C3) & C2. */
3920 (bit_and (convert1? (rshift@0 (convert2? (bswap@4 @1)) INTEGER_CST@2))
3922 (if (BITS_PER_UNIT == 8
3923 && tree_fits_uhwi_p (@2)
3924 && tree_fits_uhwi_p (@3))
3927 unsigned HOST_WIDE_INT prec = TYPE_PRECISION (TREE_TYPE (@4));
3928 unsigned HOST_WIDE_INT bits = tree_to_uhwi (@2);
3929 unsigned HOST_WIDE_INT mask = tree_to_uhwi (@3);
3930 unsigned HOST_WIDE_INT lo = bits & 7;
3931 unsigned HOST_WIDE_INT hi = bits - lo;
3934 && mask < (256u>>lo)
3935 && bits < TYPE_PRECISION (TREE_TYPE(@0)))
3936 (with { unsigned HOST_WIDE_INT ns = (prec - (hi + 8)) + lo; }
3938 (bit_and (convert @1) @3)
3941 tree utype = unsigned_type_for (TREE_TYPE (@1));
3942 tree nst = build_int_cst (integer_type_node, ns);
3944 (bit_and (convert (rshift:utype (convert:utype @1) {nst;})) @3))))))))
3945 /* bswap(x) >> C1 can sometimes be simplified to (T)x >> C2. */
3947 (rshift (convert? (bswap@2 @0)) INTEGER_CST@1)
3948 (if (BITS_PER_UNIT == 8
3949 && CHAR_TYPE_SIZE == 8
3950 && tree_fits_uhwi_p (@1))
3953 unsigned HOST_WIDE_INT prec = TYPE_PRECISION (TREE_TYPE (@2));
3954 unsigned HOST_WIDE_INT bits = tree_to_uhwi (@1);
3955 /* If the bswap was extended before the original shift, this
3956 byte (shift) has the sign of the extension, not the sign of
3957 the original shift. */
3958 tree st = TYPE_PRECISION (type) > prec ? TREE_TYPE (@2) : type;
3960 /* Special case: logical right shift of sign-extended bswap.
3961 (unsigned)(short)bswap16(x)>>12 is (unsigned)((short)x<<8)>>12. */
3962 (if (TYPE_PRECISION (type) > prec
3963 && !TYPE_UNSIGNED (TREE_TYPE (@2))
3964 && TYPE_UNSIGNED (type)
3965 && bits < prec && bits + 8 >= prec)
3966 (with { tree nst = build_int_cst (integer_type_node, prec - 8); }
3967 (rshift (convert (lshift:st (convert:st @0) {nst;})) @1))
3968 (if (bits + 8 == prec)
3969 (if (TYPE_UNSIGNED (st))
3970 (convert (convert:unsigned_char_type_node @0))
3971 (convert (convert:signed_char_type_node @0)))
3972 (if (bits < prec && bits + 8 > prec)
3975 tree nst = build_int_cst (integer_type_node, bits & 7);
3976 tree bt = TYPE_UNSIGNED (st) ? unsigned_char_type_node
3977 : signed_char_type_node;
3979 (convert (rshift:bt (convert:bt @0) {nst;})))))))))
3980 /* bswap(x) & C1 can sometimes be simplified to (x >> C2) & C1. */
3982 (bit_and (convert? (bswap@2 @0)) INTEGER_CST@1)
3983 (if (BITS_PER_UNIT == 8
3984 && tree_fits_uhwi_p (@1)
3985 && tree_to_uhwi (@1) < 256)
3988 unsigned HOST_WIDE_INT prec = TYPE_PRECISION (TREE_TYPE (@2));
3989 tree utype = unsigned_type_for (TREE_TYPE (@0));
3990 tree nst = build_int_cst (integer_type_node, prec - 8);
3992 (bit_and (convert (rshift:utype (convert:utype @0) {nst;})) @1)))))
3995 /* Combine COND_EXPRs and VEC_COND_EXPRs. */
3997 /* Simplify constant conditions.
3998 Only optimize constant conditions when the selected branch
3999 has the same type as the COND_EXPR. This avoids optimizing
4000 away "c ? x : throw", where the throw has a void type.
4001 Note that we cannot throw away the fold-const.c variant nor
4002 this one as we depend on doing this transform before possibly
4003 A ? B : B -> B triggers and the fold-const.c one can optimize
4004 0 ? A : B to B even if A has side-effects. Something
4005 genmatch cannot handle. */
4007 (cond INTEGER_CST@0 @1 @2)
4008 (if (integer_zerop (@0))
4009 (if (!VOID_TYPE_P (TREE_TYPE (@2)) || VOID_TYPE_P (type))
4011 (if (!VOID_TYPE_P (TREE_TYPE (@1)) || VOID_TYPE_P (type))
4014 (vec_cond VECTOR_CST@0 @1 @2)
4015 (if (integer_all_onesp (@0))
4017 (if (integer_zerop (@0))
4021 /* Sink unary operations to branches, but only if we do fold both. */
4022 (for op (negate bit_not abs absu)
4024 (op (vec_cond:s @0 @1 @2))
4025 (vec_cond @0 (op! @1) (op! @2))))
4027 /* Sink binary operation to branches, but only if we can fold it. */
4028 (for op (tcc_comparison plus minus mult bit_and bit_ior bit_xor
4029 lshift rshift rdiv trunc_div ceil_div floor_div round_div
4030 trunc_mod ceil_mod floor_mod round_mod min max)
4031 /* (c ? a : b) op (c ? d : e) --> c ? (a op d) : (b op e) */
4033 (op (vec_cond:s @0 @1 @2) (vec_cond:s @0 @3 @4))
4034 (vec_cond @0 (op! @1 @3) (op! @2 @4)))
4036 /* (c ? a : b) op d --> c ? (a op d) : (b op d) */
4038 (op (vec_cond:s @0 @1 @2) @3)
4039 (vec_cond @0 (op! @1 @3) (op! @2 @3)))
4041 (op @3 (vec_cond:s @0 @1 @2))
4042 (vec_cond @0 (op! @3 @1) (op! @3 @2))))
4046 (match (nop_atomic_bit_test_and_p @0 @1 @4)
4047 (bit_and (nop_convert?@4 (ATOMIC_FETCH_OR_XOR_N @2 INTEGER_CST@0 @3))
4050 int ibit = tree_log2 (@0);
4051 int ibit2 = tree_log2 (@1);
4056 (match (nop_atomic_bit_test_and_p @0 @1 @3)
4057 (bit_and (nop_convert?@3 (SYNC_FETCH_OR_XOR_N @2 INTEGER_CST@0))
4060 int ibit = tree_log2 (@0);
4061 int ibit2 = tree_log2 (@1);
4066 (match (nop_atomic_bit_test_and_p @0 @0 @4)
4069 (ATOMIC_FETCH_OR_XOR_N @2 (nop_convert? (lshift@0 integer_onep@5 @6)) @3))
4072 (match (nop_atomic_bit_test_and_p @0 @0 @4)
4075 (SYNC_FETCH_OR_XOR_N @2 (nop_convert? (lshift@0 integer_onep@3 @5))))
4078 (match (nop_atomic_bit_test_and_p @0 @1 @3)
4079 (bit_and@4 (nop_convert?@3 (ATOMIC_FETCH_AND_N @2 INTEGER_CST@0 @5))
4082 int ibit = wi::exact_log2 (wi::zext (wi::bit_not (wi::to_wide (@0)),
4083 TYPE_PRECISION(type)));
4084 int ibit2 = tree_log2 (@1);
4089 (match (nop_atomic_bit_test_and_p @0 @1 @3)
4091 (nop_convert?@3 (SYNC_FETCH_AND_AND_N @2 INTEGER_CST@0))
4094 int ibit = wi::exact_log2 (wi::zext (wi::bit_not (wi::to_wide (@0)),
4095 TYPE_PRECISION(type)));
4096 int ibit2 = tree_log2 (@1);
4101 (match (nop_atomic_bit_test_and_p @0 @0 @3)
4104 (ATOMIC_FETCH_AND_N @2 (nop_convert? (bit_not (lshift@0 integer_onep@6 @7))) @5))
4107 (match (nop_atomic_bit_test_and_p @0 @0 @3)
4110 (SYNC_FETCH_AND_AND_N @2 (nop_convert? (bit_not (lshift@0 integer_onep@6 @7)))))
4115 /* (v ? w : 0) ? a : b is just (v & w) ? a : b
4116 Currently disabled after pass lvec because ARM understands
4117 VEC_COND_EXPR<v==w,-1,0> but not a plain v==w fed to BIT_IOR_EXPR. */
4119 (vec_cond (vec_cond:s @0 @3 integer_zerop) @1 @2)
4120 (if (optimize_vectors_before_lowering_p () && types_match (@0, @3))
4121 (vec_cond (bit_and @0 @3) @1 @2)))
4123 (vec_cond (vec_cond:s @0 integer_all_onesp @3) @1 @2)
4124 (if (optimize_vectors_before_lowering_p () && types_match (@0, @3))
4125 (vec_cond (bit_ior @0 @3) @1 @2)))
4127 (vec_cond (vec_cond:s @0 integer_zerop @3) @1 @2)
4128 (if (optimize_vectors_before_lowering_p () && types_match (@0, @3))
4129 (vec_cond (bit_ior @0 (bit_not @3)) @2 @1)))
4131 (vec_cond (vec_cond:s @0 @3 integer_all_onesp) @1 @2)
4132 (if (optimize_vectors_before_lowering_p () && types_match (@0, @3))
4133 (vec_cond (bit_and @0 (bit_not @3)) @2 @1)))
4135 /* c1 ? c2 ? a : b : b --> (c1 & c2) ? a : b */
4137 (vec_cond @0 (vec_cond:s @1 @2 @3) @3)
4138 (if (optimize_vectors_before_lowering_p () && types_match (@0, @1))
4139 (vec_cond (bit_and @0 @1) @2 @3)))
4141 (vec_cond @0 @2 (vec_cond:s @1 @2 @3))
4142 (if (optimize_vectors_before_lowering_p () && types_match (@0, @1))
4143 (vec_cond (bit_ior @0 @1) @2 @3)))
4145 (vec_cond @0 (vec_cond:s @1 @2 @3) @2)
4146 (if (optimize_vectors_before_lowering_p () && types_match (@0, @1))
4147 (vec_cond (bit_ior (bit_not @0) @1) @2 @3)))
4149 (vec_cond @0 @3 (vec_cond:s @1 @2 @3))
4150 (if (optimize_vectors_before_lowering_p () && types_match (@0, @1))
4151 (vec_cond (bit_and (bit_not @0) @1) @2 @3)))
4153 /* Canonicalize mask ? { 0, ... } : { -1, ...} to ~mask if the mask
4154 types are compatible. */
4156 (vec_cond @0 VECTOR_CST@1 VECTOR_CST@2)
4157 (if (VECTOR_BOOLEAN_TYPE_P (type)
4158 && types_match (type, TREE_TYPE (@0)))
4159 (if (integer_zerop (@1) && integer_all_onesp (@2))
4161 (if (integer_all_onesp (@1) && integer_zerop (@2))
4164 /* A few simplifications of "a ? CST1 : CST2". */
4165 /* NOTE: Only do this on gimple as the if-chain-to-switch
4166 optimization depends on the gimple to have if statements in it. */
4169 (cond @0 INTEGER_CST@1 INTEGER_CST@2)
4171 (if (integer_zerop (@2))
4173 /* a ? 1 : 0 -> a if 0 and 1 are integral types. */
4174 (if (integer_onep (@1))
4175 (convert (convert:boolean_type_node @0)))
4176 /* a ? powerof2cst : 0 -> a << (log2(powerof2cst)) */
4177 (if (INTEGRAL_TYPE_P (type) && integer_pow2p (@1))
4179 tree shift = build_int_cst (integer_type_node, tree_log2 (@1));
4181 (lshift (convert (convert:boolean_type_node @0)) { shift; })))
4182 /* a ? -1 : 0 -> -a. No need to check the TYPE_PRECISION not being 1
4183 here as the powerof2cst case above will handle that case correctly. */
4184 (if (INTEGRAL_TYPE_P (type) && integer_all_onesp (@1))
4185 (negate (convert (convert:boolean_type_node @0))))))
4186 (if (integer_zerop (@1))
4188 tree booltrue = constant_boolean_node (true, boolean_type_node);
4191 /* a ? 0 : 1 -> !a. */
4192 (if (integer_onep (@2))
4193 (convert (bit_xor (convert:boolean_type_node @0) { booltrue; } )))
4194 /* a ? powerof2cst : 0 -> (!a) << (log2(powerof2cst)) */
4195 (if (INTEGRAL_TYPE_P (type) && integer_pow2p (@2))
4197 tree shift = build_int_cst (integer_type_node, tree_log2 (@2));
4199 (lshift (convert (bit_xor (convert:boolean_type_node @0) { booltrue; } ))
4201 /* a ? -1 : 0 -> -(!a). No need to check the TYPE_PRECISION not being 1
4202 here as the powerof2cst case above will handle that case correctly. */
4203 (if (INTEGRAL_TYPE_P (type) && integer_all_onesp (@2))
4204 (negate (convert (bit_xor (convert:boolean_type_node @0) { booltrue; } ))))
4212 /* Simplification moved from fold_cond_expr_with_comparison. It may also
4214 /* This pattern implements two kinds simplification:
4217 (cond (cmp (convert1? x) c1) (convert2? x) c2) -> (minmax (x c)) if:
4218 1) Conversions are type widening from smaller type.
4219 2) Const c1 equals to c2 after canonicalizing comparison.
4220 3) Comparison has tree code LT, LE, GT or GE.
4221 This specific pattern is needed when (cmp (convert x) c) may not
4222 be simplified by comparison patterns because of multiple uses of
4223 x. It also makes sense here because simplifying across multiple
4224 referred var is always benefitial for complicated cases.
4227 (cond (eq (convert1? x) c1) (convert2? x) c2) -> (cond (eq x c1) c1 c2). */
4228 (for cmp (lt le gt ge eq)
4230 (cond (cmp (convert1? @1) INTEGER_CST@3) (convert2? @1) INTEGER_CST@2)
4233 tree from_type = TREE_TYPE (@1);
4234 tree c1_type = TREE_TYPE (@3), c2_type = TREE_TYPE (@2);
4235 enum tree_code code = ERROR_MARK;
4237 if (INTEGRAL_TYPE_P (from_type)
4238 && int_fits_type_p (@2, from_type)
4239 && (types_match (c1_type, from_type)
4240 || (TYPE_PRECISION (c1_type) > TYPE_PRECISION (from_type)
4241 && (TYPE_UNSIGNED (from_type)
4242 || TYPE_SIGN (c1_type) == TYPE_SIGN (from_type))))
4243 && (types_match (c2_type, from_type)
4244 || (TYPE_PRECISION (c2_type) > TYPE_PRECISION (from_type)
4245 && (TYPE_UNSIGNED (from_type)
4246 || TYPE_SIGN (c2_type) == TYPE_SIGN (from_type)))))
4250 if (wi::to_widest (@3) == (wi::to_widest (@2) - 1))
4252 /* X <= Y - 1 equals to X < Y. */
4255 /* X > Y - 1 equals to X >= Y. */
4259 if (wi::to_widest (@3) == (wi::to_widest (@2) + 1))
4261 /* X < Y + 1 equals to X <= Y. */
4264 /* X >= Y + 1 equals to X > Y. */
4268 if (code != ERROR_MARK
4269 || wi::to_widest (@2) == wi::to_widest (@3))
4271 if (cmp == LT_EXPR || cmp == LE_EXPR)
4273 if (cmp == GT_EXPR || cmp == GE_EXPR)
4277 /* Can do A == C1 ? A : C2 -> A == C1 ? C1 : C2? */
4278 else if (int_fits_type_p (@3, from_type))
4282 (if (code == MAX_EXPR)
4283 (convert (max @1 (convert @2)))
4284 (if (code == MIN_EXPR)
4285 (convert (min @1 (convert @2)))
4286 (if (code == EQ_EXPR)
4287 (convert (cond (eq @1 (convert @3))
4288 (convert:from_type @3) (convert:from_type @2)))))))))
4290 /* (cond (cmp (convert? x) c1) (op x c2) c3) -> (op (minmax x c1) c2) if:
4292 1) OP is PLUS or MINUS.
4293 2) CMP is LT, LE, GT or GE.
4294 3) C3 == (C1 op C2), and computation doesn't have undefined behavior.
4296 This pattern also handles special cases like:
4298 A) Operand x is a unsigned to signed type conversion and c1 is
4299 integer zero. In this case,
4300 (signed type)x < 0 <=> x > MAX_VAL(signed type)
4301 (signed type)x >= 0 <=> x <= MAX_VAL(signed type)
4302 B) Const c1 may not equal to (C3 op' C2). In this case we also
4303 check equality for (c1+1) and (c1-1) by adjusting comparison
4306 TODO: Though signed type is handled by this pattern, it cannot be
4307 simplified at the moment because C standard requires additional
4308 type promotion. In order to match&simplify it here, the IR needs
4309 to be cleaned up by other optimizers, i.e, VRP. */
4310 (for op (plus minus)
4311 (for cmp (lt le gt ge)
4313 (cond (cmp (convert? @X) INTEGER_CST@1) (op @X INTEGER_CST@2) INTEGER_CST@3)
4314 (with { tree from_type = TREE_TYPE (@X), to_type = TREE_TYPE (@1); }
4315 (if (types_match (from_type, to_type)
4316 /* Check if it is special case A). */
4317 || (TYPE_UNSIGNED (from_type)
4318 && !TYPE_UNSIGNED (to_type)
4319 && TYPE_PRECISION (from_type) == TYPE_PRECISION (to_type)
4320 && integer_zerop (@1)
4321 && (cmp == LT_EXPR || cmp == GE_EXPR)))
4324 wi::overflow_type overflow = wi::OVF_NONE;
4325 enum tree_code code, cmp_code = cmp;
4327 wide_int c1 = wi::to_wide (@1);
4328 wide_int c2 = wi::to_wide (@2);
4329 wide_int c3 = wi::to_wide (@3);
4330 signop sgn = TYPE_SIGN (from_type);
4332 /* Handle special case A), given x of unsigned type:
4333 ((signed type)x < 0) <=> (x > MAX_VAL(signed type))
4334 ((signed type)x >= 0) <=> (x <= MAX_VAL(signed type)) */
4335 if (!types_match (from_type, to_type))
4337 if (cmp_code == LT_EXPR)
4339 if (cmp_code == GE_EXPR)
4341 c1 = wi::max_value (to_type);
4343 /* To simplify this pattern, we require c3 = (c1 op c2). Here we
4344 compute (c3 op' c2) and check if it equals to c1 with op' being
4345 the inverted operator of op. Make sure overflow doesn't happen
4346 if it is undefined. */
4347 if (op == PLUS_EXPR)
4348 real_c1 = wi::sub (c3, c2, sgn, &overflow);
4350 real_c1 = wi::add (c3, c2, sgn, &overflow);
4353 if (!overflow || !TYPE_OVERFLOW_UNDEFINED (from_type))
4355 /* Check if c1 equals to real_c1. Boundary condition is handled
4356 by adjusting comparison operation if necessary. */
4357 if (!wi::cmp (wi::sub (real_c1, 1, sgn, &overflow), c1, sgn)
4360 /* X <= Y - 1 equals to X < Y. */
4361 if (cmp_code == LE_EXPR)
4363 /* X > Y - 1 equals to X >= Y. */
4364 if (cmp_code == GT_EXPR)
4367 if (!wi::cmp (wi::add (real_c1, 1, sgn, &overflow), c1, sgn)
4370 /* X < Y + 1 equals to X <= Y. */
4371 if (cmp_code == LT_EXPR)
4373 /* X >= Y + 1 equals to X > Y. */
4374 if (cmp_code == GE_EXPR)
4377 if (code != cmp_code || !wi::cmp (real_c1, c1, sgn))
4379 if (cmp_code == LT_EXPR || cmp_code == LE_EXPR)
4381 if (cmp_code == GT_EXPR || cmp_code == GE_EXPR)
4386 (if (code == MAX_EXPR)
4387 (op (max @X { wide_int_to_tree (from_type, real_c1); })
4388 { wide_int_to_tree (from_type, c2); })
4389 (if (code == MIN_EXPR)
4390 (op (min @X { wide_int_to_tree (from_type, real_c1); })
4391 { wide_int_to_tree (from_type, c2); })))))))))
4393 (for cnd (cond vec_cond)
4394 /* A ? B : (A ? X : C) -> A ? B : C. */
4396 (cnd @0 (cnd @0 @1 @2) @3)
4399 (cnd @0 @1 (cnd @0 @2 @3))
4401 /* A ? B : (!A ? C : X) -> A ? B : C. */
4402 /* ??? This matches embedded conditions open-coded because genmatch
4403 would generate matching code for conditions in separate stmts only.
4404 The following is still important to merge then and else arm cases
4405 from if-conversion. */
4407 (cnd @0 @1 (cnd @2 @3 @4))
4408 (if (inverse_conditions_p (@0, @2))
4411 (cnd @0 (cnd @1 @2 @3) @4)
4412 (if (inverse_conditions_p (@0, @1))
4415 /* A ? B : B -> B. */
4420 /* !A ? B : C -> A ? C : B. */
4422 (cnd (logical_inverted_value truth_valued_p@0) @1 @2)
4425 /* abs/negative simplifications moved from fold_cond_expr_with_comparison,
4426 Need to handle (A - B) case as fold_cond_expr_with_comparison does.
4427 Need to handle UN* comparisons.
4429 None of these transformations work for modes with signed
4430 zeros. If A is +/-0, the first two transformations will
4431 change the sign of the result (from +0 to -0, or vice
4432 versa). The last four will fix the sign of the result,
4433 even though the original expressions could be positive or
4434 negative, depending on the sign of A.
4436 Note that all these transformations are correct if A is
4437 NaN, since the two alternatives (A and -A) are also NaNs. */
4439 (for cnd (cond vec_cond)
4440 /* A == 0 ? A : -A same as -A */
4443 (cnd (cmp @0 zerop) @0 (negate@1 @0))
4444 (if (!HONOR_SIGNED_ZEROS (type))
4447 (cnd (cmp @0 zerop) integer_zerop (negate@1 @0))
4448 (if (!HONOR_SIGNED_ZEROS (type))
4451 /* A != 0 ? A : -A same as A */
4454 (cnd (cmp @0 zerop) @0 (negate @0))
4455 (if (!HONOR_SIGNED_ZEROS (type))
4458 (cnd (cmp @0 zerop) @0 integer_zerop)
4459 (if (!HONOR_SIGNED_ZEROS (type))
4462 /* A >=/> 0 ? A : -A same as abs (A) */
4465 (cnd (cmp @0 zerop) @0 (negate @0))
4466 (if (!HONOR_SIGNED_ZEROS (type)
4467 && !TYPE_UNSIGNED (type))
4469 /* A <=/< 0 ? A : -A same as -abs (A) */
4472 (cnd (cmp @0 zerop) @0 (negate @0))
4473 (if (!HONOR_SIGNED_ZEROS (type)
4474 && !TYPE_UNSIGNED (type))
4475 (if (ANY_INTEGRAL_TYPE_P (type)
4476 && !TYPE_OVERFLOW_WRAPS (type))
4478 tree utype = unsigned_type_for (type);
4480 (convert (negate (absu:utype @0))))
4481 (negate (abs @0)))))
4485 /* -(type)!A -> (type)A - 1. */
4487 (negate (convert?:s (logical_inverted_value:s @0)))
4488 (if (INTEGRAL_TYPE_P (type)
4489 && TREE_CODE (type) != BOOLEAN_TYPE
4490 && TYPE_PRECISION (type) > 1
4491 && TREE_CODE (@0) == SSA_NAME
4492 && ssa_name_has_boolean_range (@0))
4493 (plus (convert:type @0) { build_all_ones_cst (type); })))
4495 /* A + (B vcmp C ? 1 : 0) -> A - (B vcmp C ? -1 : 0), since vector comparisons
4496 return all -1 or all 0 results. */
4497 /* ??? We could instead convert all instances of the vec_cond to negate,
4498 but that isn't necessarily a win on its own. */
4500 (plus:c @3 (view_convert? (vec_cond:s @0 integer_each_onep@1 integer_zerop@2)))
4501 (if (VECTOR_TYPE_P (type)
4502 && known_eq (TYPE_VECTOR_SUBPARTS (type),
4503 TYPE_VECTOR_SUBPARTS (TREE_TYPE (@1)))
4504 && (TYPE_MODE (TREE_TYPE (type))
4505 == TYPE_MODE (TREE_TYPE (TREE_TYPE (@1)))))
4506 (minus @3 (view_convert (vec_cond @0 (negate @1) @2)))))
4508 /* ... likewise A - (B vcmp C ? 1 : 0) -> A + (B vcmp C ? -1 : 0). */
4510 (minus @3 (view_convert? (vec_cond:s @0 integer_each_onep@1 integer_zerop@2)))
4511 (if (VECTOR_TYPE_P (type)
4512 && known_eq (TYPE_VECTOR_SUBPARTS (type),
4513 TYPE_VECTOR_SUBPARTS (TREE_TYPE (@1)))
4514 && (TYPE_MODE (TREE_TYPE (type))
4515 == TYPE_MODE (TREE_TYPE (TREE_TYPE (@1)))))
4516 (plus @3 (view_convert (vec_cond @0 (negate @1) @2)))))
4519 /* Simplifications of comparisons. */
4521 /* See if we can reduce the magnitude of a constant involved in a
4522 comparison by changing the comparison code. This is a canonicalization
4523 formerly done by maybe_canonicalize_comparison_1. */
4527 (cmp @0 uniform_integer_cst_p@1)
4528 (with { tree cst = uniform_integer_cst_p (@1); }
4529 (if (tree_int_cst_sgn (cst) == -1)
4530 (acmp @0 { build_uniform_cst (TREE_TYPE (@1),
4531 wide_int_to_tree (TREE_TYPE (cst),
4537 (cmp @0 uniform_integer_cst_p@1)
4538 (with { tree cst = uniform_integer_cst_p (@1); }
4539 (if (tree_int_cst_sgn (cst) == 1)
4540 (acmp @0 { build_uniform_cst (TREE_TYPE (@1),
4541 wide_int_to_tree (TREE_TYPE (cst),
4542 wi::to_wide (cst) - 1)); })))))
4544 /* We can simplify a logical negation of a comparison to the
4545 inverted comparison. As we cannot compute an expression
4546 operator using invert_tree_comparison we have to simulate
4547 that with expression code iteration. */
4548 (for cmp (tcc_comparison)
4549 icmp (inverted_tcc_comparison)
4550 ncmp (inverted_tcc_comparison_with_nans)
4551 /* Ideally we'd like to combine the following two patterns
4552 and handle some more cases by using
4553 (logical_inverted_value (cmp @0 @1))
4554 here but for that genmatch would need to "inline" that.
4555 For now implement what forward_propagate_comparison did. */
4557 (bit_not (cmp @0 @1))
4558 (if (VECTOR_TYPE_P (type)
4559 || (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1))
4560 /* Comparison inversion may be impossible for trapping math,
4561 invert_tree_comparison will tell us. But we can't use
4562 a computed operator in the replacement tree thus we have
4563 to play the trick below. */
4564 (with { enum tree_code ic = invert_tree_comparison
4565 (cmp, HONOR_NANS (@0)); }
4571 (bit_xor (cmp @0 @1) integer_truep)
4572 (with { enum tree_code ic = invert_tree_comparison
4573 (cmp, HONOR_NANS (@0)); }
4579 /* Transform comparisons of the form X - Y CMP 0 to X CMP Y.
4580 ??? The transformation is valid for the other operators if overflow
4581 is undefined for the type, but performing it here badly interacts
4582 with the transformation in fold_cond_expr_with_comparison which
4583 attempts to synthetize ABS_EXPR. */
4585 (for sub (minus pointer_diff)
4587 (cmp (sub@2 @0 @1) integer_zerop)
4588 (if (single_use (@2))
4591 /* Simplify (x < 0) ^ (y < 0) to (x ^ y) < 0 and
4592 (x >= 0) ^ (y >= 0) to (x ^ y) < 0. */
4595 (bit_xor (cmp:s @0 integer_zerop) (cmp:s @1 integer_zerop))
4596 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
4597 && !TYPE_UNSIGNED (TREE_TYPE (@0))
4598 && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
4599 (lt (bit_xor @0 @1) { build_zero_cst (TREE_TYPE (@0)); }))))
4600 /* Simplify (x < 0) ^ (y >= 0) to (x ^ y) >= 0 and
4601 (x >= 0) ^ (y < 0) to (x ^ y) >= 0. */
4603 (bit_xor:c (lt:s @0 integer_zerop) (ge:s @1 integer_zerop))
4604 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
4605 && !TYPE_UNSIGNED (TREE_TYPE (@0))
4606 && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
4607 (ge (bit_xor @0 @1) { build_zero_cst (TREE_TYPE (@0)); })))
4609 /* Transform comparisons of the form X * C1 CMP 0 to X CMP 0 in the
4610 signed arithmetic case. That form is created by the compiler
4611 often enough for folding it to be of value. One example is in
4612 computing loop trip counts after Operator Strength Reduction. */
4613 (for cmp (simple_comparison)
4614 scmp (swapped_simple_comparison)
4616 (cmp (mult@3 @0 INTEGER_CST@1) integer_zerop@2)
4617 /* Handle unfolded multiplication by zero. */
4618 (if (integer_zerop (@1))
4620 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
4621 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
4623 /* If @1 is negative we swap the sense of the comparison. */
4624 (if (tree_int_cst_sgn (@1) < 0)
4628 /* For integral types with undefined overflow fold
4629 x * C1 == C2 into x == C2 / C1 or false.
4630 If overflow wraps and C1 is odd, simplify to x == C2 / C1 in the ring
4634 (cmp (mult @0 INTEGER_CST@1) INTEGER_CST@2)
4635 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
4636 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
4637 && wi::to_wide (@1) != 0)
4638 (with { widest_int quot; }
4639 (if (wi::multiple_of_p (wi::to_widest (@2), wi::to_widest (@1),
4640 TYPE_SIGN (TREE_TYPE (@0)), "))
4641 (cmp @0 { wide_int_to_tree (TREE_TYPE (@0), quot); })
4642 { constant_boolean_node (cmp == NE_EXPR, type); }))
4643 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
4644 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))
4645 && (wi::bit_and (wi::to_wide (@1), 1) == 1))
4648 tree itype = TREE_TYPE (@0);
4649 int p = TYPE_PRECISION (itype);
4650 wide_int m = wi::one (p + 1) << p;
4651 wide_int a = wide_int::from (wi::to_wide (@1), p + 1, UNSIGNED);
4652 wide_int i = wide_int::from (wi::mod_inv (a, m),
4653 p, TYPE_SIGN (itype));
4654 wide_int_to_tree (itype, wi::mul (i, wi::to_wide (@2)));
4657 /* Simplify comparison of something with itself. For IEEE
4658 floating-point, we can only do some of these simplifications. */
4662 (if (! FLOAT_TYPE_P (TREE_TYPE (@0))
4663 || ! HONOR_NANS (@0))
4664 { constant_boolean_node (true, type); }
4665 (if (cmp != EQ_EXPR)
4671 || ! FLOAT_TYPE_P (TREE_TYPE (@0))
4672 || ! HONOR_NANS (@0))
4673 { constant_boolean_node (false, type); })))
4674 (for cmp (unle unge uneq)
4677 { constant_boolean_node (true, type); }))
4678 (for cmp (unlt ungt)
4684 (if (!flag_trapping_math)
4685 { constant_boolean_node (false, type); }))
4687 /* x == ~x -> false */
4688 /* x != ~x -> true */
4691 (cmp:c @0 (bit_not @0))
4692 { constant_boolean_node (cmp == NE_EXPR, type); }))
4694 /* Fold ~X op ~Y as Y op X. */
4695 (for cmp (simple_comparison)
4697 (cmp (bit_not@2 @0) (bit_not@3 @1))
4698 (if (single_use (@2) && single_use (@3))
4701 /* Fold ~X op C as X op' ~C, where op' is the swapped comparison. */
4702 (for cmp (simple_comparison)
4703 scmp (swapped_simple_comparison)
4705 (cmp (bit_not@2 @0) CONSTANT_CLASS_P@1)
4706 (if (single_use (@2)
4707 && (TREE_CODE (@1) == INTEGER_CST || TREE_CODE (@1) == VECTOR_CST))
4708 (scmp @0 (bit_not @1)))))
4710 (for cmp (simple_comparison)
4711 /* Fold (double)float1 CMP (double)float2 into float1 CMP float2. */
4713 (cmp (convert@2 @0) (convert? @1))
4714 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
4715 && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@2))
4716 == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0)))
4717 && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@2))
4718 == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1))))
4721 tree type1 = TREE_TYPE (@1);
4722 if (TREE_CODE (@1) == REAL_CST && !DECIMAL_FLOAT_TYPE_P (type1))
4724 REAL_VALUE_TYPE orig = TREE_REAL_CST (@1);
4725 if (TYPE_PRECISION (type1) > TYPE_PRECISION (float_type_node)
4726 && exact_real_truncate (TYPE_MODE (float_type_node), &orig))
4727 type1 = float_type_node;
4728 if (TYPE_PRECISION (type1) > TYPE_PRECISION (double_type_node)
4729 && exact_real_truncate (TYPE_MODE (double_type_node), &orig))
4730 type1 = double_type_node;
4733 = (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type1)
4734 ? TREE_TYPE (@0) : type1);
4736 (if (TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (newtype))
4737 (cmp (convert:newtype @0) (convert:newtype @1))))))
4741 /* IEEE doesn't distinguish +0 and -0 in comparisons. */
4743 /* a CMP (-0) -> a CMP 0 */
4744 (if (REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@1)))
4745 (cmp @0 { build_real (TREE_TYPE (@1), dconst0); }))
4746 /* (-0) CMP b -> 0 CMP b. */
4747 (if (TREE_CODE (@0) == REAL_CST
4748 && REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@0)))
4749 (cmp { build_real (TREE_TYPE (@0), dconst0); } @1))
4750 /* x != NaN is always true, other ops are always false. */
4751 (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
4752 && !tree_expr_signaling_nan_p (@1)
4753 && !tree_expr_maybe_signaling_nan_p (@0))
4754 { constant_boolean_node (cmp == NE_EXPR, type); })
4755 /* NaN != y is always true, other ops are always false. */
4756 (if (TREE_CODE (@0) == REAL_CST
4757 && REAL_VALUE_ISNAN (TREE_REAL_CST (@0))
4758 && !tree_expr_signaling_nan_p (@0)
4759 && !tree_expr_signaling_nan_p (@1))
4760 { constant_boolean_node (cmp == NE_EXPR, type); })
4761 /* Fold comparisons against infinity. */
4762 (if (REAL_VALUE_ISINF (TREE_REAL_CST (@1))
4763 && MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (@1))))
4766 REAL_VALUE_TYPE max;
4767 enum tree_code code = cmp;
4768 bool neg = REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1));
4770 code = swap_tree_comparison (code);
4773 /* x > +Inf is always false, if we ignore NaNs or exceptions. */
4774 (if (code == GT_EXPR
4775 && !(HONOR_NANS (@0) && flag_trapping_math))
4776 { constant_boolean_node (false, type); })
4777 (if (code == LE_EXPR)
4778 /* x <= +Inf is always true, if we don't care about NaNs. */
4779 (if (! HONOR_NANS (@0))
4780 { constant_boolean_node (true, type); }
4781 /* x <= +Inf is the same as x == x, i.e. !isnan(x), but this loses
4782 an "invalid" exception. */
4783 (if (!flag_trapping_math)
4785 /* x == +Inf and x >= +Inf are always equal to x > DBL_MAX, but
4786 for == this introduces an exception for x a NaN. */
4787 (if ((code == EQ_EXPR && !(HONOR_NANS (@0) && flag_trapping_math))
4789 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
4791 (lt @0 { build_real (TREE_TYPE (@0), max); })
4792 (gt @0 { build_real (TREE_TYPE (@0), max); }))))
4793 /* x < +Inf is always equal to x <= DBL_MAX. */
4794 (if (code == LT_EXPR)
4795 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
4797 (ge @0 { build_real (TREE_TYPE (@0), max); })
4798 (le @0 { build_real (TREE_TYPE (@0), max); }))))
4799 /* x != +Inf is always equal to !(x > DBL_MAX), but this introduces
4800 an exception for x a NaN so use an unordered comparison. */
4801 (if (code == NE_EXPR)
4802 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
4803 (if (! HONOR_NANS (@0))
4805 (ge @0 { build_real (TREE_TYPE (@0), max); })
4806 (le @0 { build_real (TREE_TYPE (@0), max); }))
4808 (unge @0 { build_real (TREE_TYPE (@0), max); })
4809 (unle @0 { build_real (TREE_TYPE (@0), max); }))))))))))
4811 /* If this is a comparison of a real constant with a PLUS_EXPR
4812 or a MINUS_EXPR of a real constant, we can convert it into a
4813 comparison with a revised real constant as long as no overflow
4814 occurs when unsafe_math_optimizations are enabled. */
4815 (if (flag_unsafe_math_optimizations)
4816 (for op (plus minus)
4818 (cmp (op @0 REAL_CST@1) REAL_CST@2)
4821 tree tem = const_binop (op == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR,
4822 TREE_TYPE (@1), @2, @1);
4824 (if (tem && !TREE_OVERFLOW (tem))
4825 (cmp @0 { tem; }))))))
4827 /* Likewise, we can simplify a comparison of a real constant with
4828 a MINUS_EXPR whose first operand is also a real constant, i.e.
4829 (c1 - x) < c2 becomes x > c1-c2. Reordering is allowed on
4830 floating-point types only if -fassociative-math is set. */
4831 (if (flag_associative_math)
4833 (cmp (minus REAL_CST@0 @1) REAL_CST@2)
4834 (with { tree tem = const_binop (MINUS_EXPR, TREE_TYPE (@1), @0, @2); }
4835 (if (tem && !TREE_OVERFLOW (tem))
4836 (cmp { tem; } @1)))))
4838 /* Fold comparisons against built-in math functions. */
4839 (if (flag_unsafe_math_optimizations && ! flag_errno_math)
4842 (cmp (sq @0) REAL_CST@1)
4844 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
4846 /* sqrt(x) < y is always false, if y is negative. */
4847 (if (cmp == EQ_EXPR || cmp == LT_EXPR || cmp == LE_EXPR)
4848 { constant_boolean_node (false, type); })
4849 /* sqrt(x) > y is always true, if y is negative and we
4850 don't care about NaNs, i.e. negative values of x. */
4851 (if (cmp == NE_EXPR || !HONOR_NANS (@0))
4852 { constant_boolean_node (true, type); })
4853 /* sqrt(x) > y is the same as x >= 0, if y is negative. */
4854 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })))
4855 (if (real_equal (TREE_REAL_CST_PTR (@1), &dconst0))
4857 /* sqrt(x) < 0 is always false. */
4858 (if (cmp == LT_EXPR)
4859 { constant_boolean_node (false, type); })
4860 /* sqrt(x) >= 0 is always true if we don't care about NaNs. */
4861 (if (cmp == GE_EXPR && !HONOR_NANS (@0))
4862 { constant_boolean_node (true, type); })
4863 /* sqrt(x) <= 0 -> x == 0. */
4864 (if (cmp == LE_EXPR)
4866 /* Otherwise sqrt(x) cmp 0 -> x cmp 0. Here cmp can be >=, >,
4867 == or !=. In the last case:
4869 (sqrt(x) != 0) == (NaN != 0) == true == (x != 0)
4871 if x is negative or NaN. Due to -funsafe-math-optimizations,
4872 the results for other x follow from natural arithmetic. */
4874 (if ((cmp == LT_EXPR
4878 && !REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
4879 /* Give up for -frounding-math. */
4880 && !HONOR_SIGN_DEPENDENT_ROUNDING (TREE_TYPE (@0)))
4884 enum tree_code ncmp = cmp;
4885 const real_format *fmt
4886 = REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (@0)));
4887 real_arithmetic (&c2, MULT_EXPR,
4888 &TREE_REAL_CST (@1), &TREE_REAL_CST (@1));
4889 real_convert (&c2, fmt, &c2);
4890 /* See PR91734: if c2 is inexact and sqrt(c2) < c (or sqrt(c2) >= c),
4891 then change LT_EXPR into LE_EXPR or GE_EXPR into GT_EXPR. */
4892 if (!REAL_VALUE_ISINF (c2))
4894 tree c3 = fold_const_call (CFN_SQRT, TREE_TYPE (@0),
4895 build_real (TREE_TYPE (@0), c2));
4896 if (c3 == NULL_TREE || TREE_CODE (c3) != REAL_CST)
4898 else if ((cmp == LT_EXPR || cmp == GE_EXPR)
4899 && real_less (&TREE_REAL_CST (c3), &TREE_REAL_CST (@1)))
4900 ncmp = cmp == LT_EXPR ? LE_EXPR : GT_EXPR;
4901 else if ((cmp == LE_EXPR || cmp == GT_EXPR)
4902 && real_less (&TREE_REAL_CST (@1), &TREE_REAL_CST (c3)))
4903 ncmp = cmp == LE_EXPR ? LT_EXPR : GE_EXPR;
4906 /* With rounding to even, sqrt of up to 3 different values
4907 gives the same normal result, so in some cases c2 needs
4909 REAL_VALUE_TYPE c2alt, tow;
4910 if (cmp == LT_EXPR || cmp == GE_EXPR)
4914 real_nextafter (&c2alt, fmt, &c2, &tow);
4915 real_convert (&c2alt, fmt, &c2alt);
4916 if (REAL_VALUE_ISINF (c2alt))
4920 c3 = fold_const_call (CFN_SQRT, TREE_TYPE (@0),
4921 build_real (TREE_TYPE (@0), c2alt));
4922 if (c3 == NULL_TREE || TREE_CODE (c3) != REAL_CST)
4924 else if (real_equal (&TREE_REAL_CST (c3),
4925 &TREE_REAL_CST (@1)))
4931 (if (cmp == GT_EXPR || cmp == GE_EXPR)
4932 (if (REAL_VALUE_ISINF (c2))
4933 /* sqrt(x) > y is x == +Inf, when y is very large. */
4934 (if (HONOR_INFINITIES (@0))
4935 (eq @0 { build_real (TREE_TYPE (@0), c2); })
4936 { constant_boolean_node (false, type); })
4937 /* sqrt(x) > c is the same as x > c*c. */
4938 (if (ncmp != ERROR_MARK)
4939 (if (ncmp == GE_EXPR)
4940 (ge @0 { build_real (TREE_TYPE (@0), c2); })
4941 (gt @0 { build_real (TREE_TYPE (@0), c2); }))))
4942 /* else if (cmp == LT_EXPR || cmp == LE_EXPR) */
4943 (if (REAL_VALUE_ISINF (c2))
4945 /* sqrt(x) < y is always true, when y is a very large
4946 value and we don't care about NaNs or Infinities. */
4947 (if (! HONOR_NANS (@0) && ! HONOR_INFINITIES (@0))
4948 { constant_boolean_node (true, type); })
4949 /* sqrt(x) < y is x != +Inf when y is very large and we
4950 don't care about NaNs. */
4951 (if (! HONOR_NANS (@0))
4952 (ne @0 { build_real (TREE_TYPE (@0), c2); }))
4953 /* sqrt(x) < y is x >= 0 when y is very large and we
4954 don't care about Infinities. */
4955 (if (! HONOR_INFINITIES (@0))
4956 (ge @0 { build_real (TREE_TYPE (@0), dconst0); }))
4957 /* sqrt(x) < y is x >= 0 && x != +Inf, when y is large. */
4960 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
4961 (ne @0 { build_real (TREE_TYPE (@0), c2); }))))
4962 /* sqrt(x) < c is the same as x < c*c, if we ignore NaNs. */
4963 (if (ncmp != ERROR_MARK && ! HONOR_NANS (@0))
4964 (if (ncmp == LT_EXPR)
4965 (lt @0 { build_real (TREE_TYPE (@0), c2); })
4966 (le @0 { build_real (TREE_TYPE (@0), c2); }))
4967 /* sqrt(x) < c is the same as x >= 0 && x < c*c. */
4968 (if (ncmp != ERROR_MARK && GENERIC)
4969 (if (ncmp == LT_EXPR)
4971 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
4972 (lt @0 { build_real (TREE_TYPE (@0), c2); }))
4974 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
4975 (le @0 { build_real (TREE_TYPE (@0), c2); })))))))))))
4976 /* Transform sqrt(x) cmp sqrt(y) -> x cmp y. */
4978 (cmp (sq @0) (sq @1))
4979 (if (! HONOR_NANS (@0))
4982 /* Optimize various special cases of (FTYPE) N CMP (FTYPE) M. */
4983 (for cmp (lt le eq ne ge gt unordered ordered unlt unle ungt unge uneq ltgt)
4984 icmp (lt le eq ne ge gt unordered ordered lt le gt ge eq ne)
4986 (cmp (float@0 @1) (float @2))
4987 (if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (@0))
4988 && ! DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0)))
4991 format_helper fmt (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (@0))));
4992 tree type1 = TREE_TYPE (@1);
4993 bool type1_signed_p = TYPE_SIGN (type1) == SIGNED;
4994 tree type2 = TREE_TYPE (@2);
4995 bool type2_signed_p = TYPE_SIGN (type2) == SIGNED;
4997 (if (fmt.can_represent_integral_type_p (type1)
4998 && fmt.can_represent_integral_type_p (type2))
4999 (if (cmp == ORDERED_EXPR || cmp == UNORDERED_EXPR)
5000 { constant_boolean_node (cmp == ORDERED_EXPR, type); }
5001 (if (TYPE_PRECISION (type1) > TYPE_PRECISION (type2)
5002 && type1_signed_p >= type2_signed_p)
5003 (icmp @1 (convert @2))
5004 (if (TYPE_PRECISION (type1) < TYPE_PRECISION (type2)
5005 && type1_signed_p <= type2_signed_p)
5006 (icmp (convert:type2 @1) @2)
5007 (if (TYPE_PRECISION (type1) == TYPE_PRECISION (type2)
5008 && type1_signed_p == type2_signed_p)
5009 (icmp @1 @2))))))))))
5011 /* Optimize various special cases of (FTYPE) N CMP CST. */
5012 (for cmp (lt le eq ne ge gt)
5013 icmp (le le eq ne ge ge)
5015 (cmp (float @0) REAL_CST@1)
5016 (if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (@1))
5017 && ! DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1)))
5020 tree itype = TREE_TYPE (@0);
5021 format_helper fmt (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (@1))));
5022 const REAL_VALUE_TYPE *cst = TREE_REAL_CST_PTR (@1);
5023 /* Be careful to preserve any potential exceptions due to
5024 NaNs. qNaNs are ok in == or != context.
5025 TODO: relax under -fno-trapping-math or
5026 -fno-signaling-nans. */
5028 = real_isnan (cst) && (cst->signalling
5029 || (cmp != EQ_EXPR && cmp != NE_EXPR));
5031 /* TODO: allow non-fitting itype and SNaNs when
5032 -fno-trapping-math. */
5033 (if (fmt.can_represent_integral_type_p (itype) && ! exception_p)
5036 signop isign = TYPE_SIGN (itype);
5037 REAL_VALUE_TYPE imin, imax;
5038 real_from_integer (&imin, fmt, wi::min_value (itype), isign);
5039 real_from_integer (&imax, fmt, wi::max_value (itype), isign);
5041 REAL_VALUE_TYPE icst;
5042 if (cmp == GT_EXPR || cmp == GE_EXPR)
5043 real_ceil (&icst, fmt, cst);
5044 else if (cmp == LT_EXPR || cmp == LE_EXPR)
5045 real_floor (&icst, fmt, cst);
5047 real_trunc (&icst, fmt, cst);
5049 bool cst_int_p = !real_isnan (cst) && real_identical (&icst, cst);
5051 bool overflow_p = false;
5053 = real_to_integer (&icst, &overflow_p, TYPE_PRECISION (itype));
5056 /* Optimize cases when CST is outside of ITYPE's range. */
5057 (if (real_compare (LT_EXPR, cst, &imin))
5058 { constant_boolean_node (cmp == GT_EXPR || cmp == GE_EXPR || cmp == NE_EXPR,
5060 (if (real_compare (GT_EXPR, cst, &imax))
5061 { constant_boolean_node (cmp == LT_EXPR || cmp == LE_EXPR || cmp == NE_EXPR,
5063 /* Remove cast if CST is an integer representable by ITYPE. */
5065 (cmp @0 { gcc_assert (!overflow_p);
5066 wide_int_to_tree (itype, icst_val); })
5068 /* When CST is fractional, optimize
5069 (FTYPE) N == CST -> 0
5070 (FTYPE) N != CST -> 1. */
5071 (if (cmp == EQ_EXPR || cmp == NE_EXPR)
5072 { constant_boolean_node (cmp == NE_EXPR, type); })
5073 /* Otherwise replace with sensible integer constant. */
5076 gcc_checking_assert (!overflow_p);
5078 (icmp @0 { wide_int_to_tree (itype, icst_val); })))))))))
5080 /* Fold A /[ex] B CMP C to A CMP B * C. */
5083 (cmp (exact_div @0 @1) INTEGER_CST@2)
5084 (if (!integer_zerop (@1))
5085 (if (wi::to_wide (@2) == 0)
5087 (if (TREE_CODE (@1) == INTEGER_CST)
5090 wi::overflow_type ovf;
5091 wide_int prod = wi::mul (wi::to_wide (@2), wi::to_wide (@1),
5092 TYPE_SIGN (TREE_TYPE (@1)), &ovf);
5095 { constant_boolean_node (cmp == NE_EXPR, type); }
5096 (cmp @0 { wide_int_to_tree (TREE_TYPE (@0), prod); }))))))))
5097 (for cmp (lt le gt ge)
5099 (cmp (exact_div @0 INTEGER_CST@1) INTEGER_CST@2)
5100 (if (wi::gt_p (wi::to_wide (@1), 0, TYPE_SIGN (TREE_TYPE (@1))))
5103 wi::overflow_type ovf;
5104 wide_int prod = wi::mul (wi::to_wide (@2), wi::to_wide (@1),
5105 TYPE_SIGN (TREE_TYPE (@1)), &ovf);
5108 { constant_boolean_node (wi::lt_p (wi::to_wide (@2), 0,
5109 TYPE_SIGN (TREE_TYPE (@2)))
5110 != (cmp == LT_EXPR || cmp == LE_EXPR), type); }
5111 (cmp @0 { wide_int_to_tree (TREE_TYPE (@0), prod); }))))))
5113 /* Fold (size_t)(A /[ex] B) CMP C to (size_t)A CMP (size_t)B * C or A CMP' 0.
5115 For small C (less than max/B), this is (size_t)A CMP (size_t)B * C.
5116 For large C (more than min/B+2^size), this is also true, with the
5117 multiplication computed modulo 2^size.
5118 For intermediate C, this just tests the sign of A. */
5119 (for cmp (lt le gt ge)
5122 (cmp (convert (exact_div @0 INTEGER_CST@1)) INTEGER_CST@2)
5123 (if (tree_nop_conversion_p (TREE_TYPE (@0), TREE_TYPE (@2))
5124 && TYPE_UNSIGNED (TREE_TYPE (@2)) && !TYPE_UNSIGNED (TREE_TYPE (@0))
5125 && wi::gt_p (wi::to_wide (@1), 0, TYPE_SIGN (TREE_TYPE (@1))))
5128 tree utype = TREE_TYPE (@2);
5129 wide_int denom = wi::to_wide (@1);
5130 wide_int right = wi::to_wide (@2);
5131 wide_int smax = wi::sdiv_trunc (wi::max_value (TREE_TYPE (@0)), denom);
5132 wide_int smin = wi::sdiv_trunc (wi::min_value (TREE_TYPE (@0)), denom);
5133 bool small = wi::leu_p (right, smax);
5134 bool large = wi::geu_p (right, smin);
5136 (if (small || large)
5137 (cmp (convert:utype @0) (mult @2 (convert @1)))
5138 (cmp2 @0 { build_zero_cst (TREE_TYPE (@0)); }))))))
5140 /* Unordered tests if either argument is a NaN. */
5142 (bit_ior (unordered @0 @0) (unordered @1 @1))
5143 (if (types_match (@0, @1))
5146 (bit_and (ordered @0 @0) (ordered @1 @1))
5147 (if (types_match (@0, @1))
5150 (bit_ior:c (unordered @0 @0) (unordered:c@2 @0 @1))
5153 (bit_and:c (ordered @0 @0) (ordered:c@2 @0 @1))
5156 /* Simple range test simplifications. */
5157 /* A < B || A >= B -> true. */
5158 (for test1 (lt le le le ne ge)
5159 test2 (ge gt ge ne eq ne)
5161 (bit_ior:c (test1 @0 @1) (test2 @0 @1))
5162 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
5163 || VECTOR_INTEGER_TYPE_P (TREE_TYPE (@0)))
5164 { constant_boolean_node (true, type); })))
5165 /* A < B && A >= B -> false. */
5166 (for test1 (lt lt lt le ne eq)
5167 test2 (ge gt eq gt eq gt)
5169 (bit_and:c (test1 @0 @1) (test2 @0 @1))
5170 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
5171 || VECTOR_INTEGER_TYPE_P (TREE_TYPE (@0)))
5172 { constant_boolean_node (false, type); })))
5174 /* A & (2**N - 1) <= 2**K - 1 -> A & (2**N - 2**K) == 0
5175 A & (2**N - 1) > 2**K - 1 -> A & (2**N - 2**K) != 0
5177 Note that comparisons
5178 A & (2**N - 1) < 2**K -> A & (2**N - 2**K) == 0
5179 A & (2**N - 1) >= 2**K -> A & (2**N - 2**K) != 0
5180 will be canonicalized to above so there's no need to
5187 (cmp (bit_and@0 @1 INTEGER_CST@2) INTEGER_CST@3)
5188 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)))
5191 tree ty = TREE_TYPE (@0);
5192 unsigned prec = TYPE_PRECISION (ty);
5193 wide_int mask = wi::to_wide (@2, prec);
5194 wide_int rhs = wi::to_wide (@3, prec);
5195 signop sgn = TYPE_SIGN (ty);
5197 (if ((mask & (mask + 1)) == 0 && wi::gt_p (rhs, 0, sgn)
5198 && (rhs & (rhs + 1)) == 0 && wi::ge_p (mask, rhs, sgn))
5199 (eqcmp (bit_and @1 { wide_int_to_tree (ty, mask - rhs); })
5200 { build_zero_cst (ty); }))))))
5202 /* -A CMP -B -> B CMP A. */
5203 (for cmp (tcc_comparison)
5204 scmp (swapped_tcc_comparison)
5206 (cmp (negate @0) (negate @1))
5207 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
5208 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
5209 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
5212 (cmp (negate @0) CONSTANT_CLASS_P@1)
5213 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
5214 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
5215 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
5216 (with { tree tem = const_unop (NEGATE_EXPR, TREE_TYPE (@0), @1); }
5217 (if (tem && !TREE_OVERFLOW (tem))
5218 (scmp @0 { tem; }))))))
5220 /* Convert ABS_EXPR<x> == 0 or ABS_EXPR<x> != 0 to x == 0 or x != 0. */
5223 (op (abs @0) zerop@1)
5226 /* From fold_sign_changed_comparison and fold_widened_comparison.
5227 FIXME: the lack of symmetry is disturbing. */
5228 (for cmp (simple_comparison)
5230 (cmp (convert@0 @00) (convert?@1 @10))
5231 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
5232 /* Disable this optimization if we're casting a function pointer
5233 type on targets that require function pointer canonicalization. */
5234 && !(targetm.have_canonicalize_funcptr_for_compare ()
5235 && ((POINTER_TYPE_P (TREE_TYPE (@00))
5236 && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@00))))
5237 || (POINTER_TYPE_P (TREE_TYPE (@10))
5238 && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@10))))))
5240 (if (TYPE_PRECISION (TREE_TYPE (@00)) == TYPE_PRECISION (TREE_TYPE (@0))
5241 && (TREE_CODE (@10) == INTEGER_CST
5243 && (TYPE_UNSIGNED (TREE_TYPE (@00)) == TYPE_UNSIGNED (TREE_TYPE (@0))
5246 && !POINTER_TYPE_P (TREE_TYPE (@00)))
5247 /* ??? The special-casing of INTEGER_CST conversion was in the original
5248 code and here to avoid a spurious overflow flag on the resulting
5249 constant which fold_convert produces. */
5250 (if (TREE_CODE (@1) == INTEGER_CST)
5251 (cmp @00 { force_fit_type (TREE_TYPE (@00), wi::to_widest (@1), 0,
5252 TREE_OVERFLOW (@1)); })
5253 (cmp @00 (convert @1)))
5255 (if (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (TREE_TYPE (@00)))
5256 /* If possible, express the comparison in the shorter mode. */
5257 (if ((cmp == EQ_EXPR || cmp == NE_EXPR
5258 || TYPE_UNSIGNED (TREE_TYPE (@0)) == TYPE_UNSIGNED (TREE_TYPE (@00))
5259 || (!TYPE_UNSIGNED (TREE_TYPE (@0))
5260 && TYPE_UNSIGNED (TREE_TYPE (@00))))
5261 && (types_match (TREE_TYPE (@10), TREE_TYPE (@00))
5262 || ((TYPE_PRECISION (TREE_TYPE (@00))
5263 >= TYPE_PRECISION (TREE_TYPE (@10)))
5264 && (TYPE_UNSIGNED (TREE_TYPE (@00))
5265 == TYPE_UNSIGNED (TREE_TYPE (@10))))
5266 || (TREE_CODE (@10) == INTEGER_CST
5267 && INTEGRAL_TYPE_P (TREE_TYPE (@00))
5268 && int_fits_type_p (@10, TREE_TYPE (@00)))))
5269 (cmp @00 (convert @10))
5270 (if (TREE_CODE (@10) == INTEGER_CST
5271 && INTEGRAL_TYPE_P (TREE_TYPE (@00))
5272 && !int_fits_type_p (@10, TREE_TYPE (@00)))
5275 tree min = lower_bound_in_type (TREE_TYPE (@10), TREE_TYPE (@00));
5276 tree max = upper_bound_in_type (TREE_TYPE (@10), TREE_TYPE (@00));
5277 bool above = integer_nonzerop (const_binop (LT_EXPR, type, max, @10));
5278 bool below = integer_nonzerop (const_binop (LT_EXPR, type, @10, min));
5280 (if (above || below)
5281 (if (cmp == EQ_EXPR || cmp == NE_EXPR)
5282 { constant_boolean_node (cmp == EQ_EXPR ? false : true, type); }
5283 (if (cmp == LT_EXPR || cmp == LE_EXPR)
5284 { constant_boolean_node (above ? true : false, type); }
5285 (if (cmp == GT_EXPR || cmp == GE_EXPR)
5286 { constant_boolean_node (above ? false : true, type); }))))))))))))
5290 /* SSA names are canonicalized to 2nd place. */
5291 (cmp addr@0 SSA_NAME@1)
5293 { poly_int64 off; tree base; }
5294 /* A local variable can never be pointed to by
5295 the default SSA name of an incoming parameter. */
5296 (if (SSA_NAME_IS_DEFAULT_DEF (@1)
5297 && TREE_CODE (SSA_NAME_VAR (@1)) == PARM_DECL
5298 && (base = get_base_address (TREE_OPERAND (@0, 0)))
5299 && TREE_CODE (base) == VAR_DECL
5300 && auto_var_in_fn_p (base, current_function_decl))
5301 (if (cmp == NE_EXPR)
5302 { constant_boolean_node (true, type); }
5303 { constant_boolean_node (false, type); })
5304 /* If the address is based on @1 decide using the offset. */
5305 (if ((base = get_addr_base_and_unit_offset (TREE_OPERAND (@0, 0), &off))
5306 && TREE_CODE (base) == MEM_REF
5307 && TREE_OPERAND (base, 0) == @1)
5308 (with { off += mem_ref_offset (base).force_shwi (); }
5309 (if (known_ne (off, 0))
5310 { constant_boolean_node (cmp == NE_EXPR, type); }
5311 (if (known_eq (off, 0))
5312 { constant_boolean_node (cmp == EQ_EXPR, type); }))))))))
5314 /* Equality compare simplifications from fold_binary */
5317 /* If we have (A | C) == D where C & ~D != 0, convert this into 0.
5318 Similarly for NE_EXPR. */
5320 (cmp (convert?@3 (bit_ior @0 INTEGER_CST@1)) INTEGER_CST@2)
5321 (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0))
5322 && wi::bit_and_not (wi::to_wide (@1), wi::to_wide (@2)) != 0)
5323 { constant_boolean_node (cmp == NE_EXPR, type); }))
5325 /* (X ^ Y) == 0 becomes X == Y, and (X ^ Y) != 0 becomes X != Y. */
5327 (cmp (bit_xor @0 @1) integer_zerop)
5330 /* (X ^ Y) == Y becomes X == 0.
5331 Likewise (X ^ Y) == X becomes Y == 0. */
5333 (cmp:c (bit_xor:c @0 @1) @0)
5334 (cmp @1 { build_zero_cst (TREE_TYPE (@1)); }))
5337 /* (X & Y) == X becomes (X & ~Y) == 0. */
5339 (cmp:c (bit_and:c @0 @1) @0)
5340 (cmp (bit_and @0 (bit_not! @1)) { build_zero_cst (TREE_TYPE (@0)); }))
5342 (cmp:c (convert@3 (bit_and (convert@2 @0) INTEGER_CST@1)) (convert @0))
5343 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
5344 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
5345 && INTEGRAL_TYPE_P (TREE_TYPE (@3))
5346 && TYPE_PRECISION (TREE_TYPE (@2)) == TYPE_PRECISION (TREE_TYPE (@0))
5347 && TYPE_PRECISION (TREE_TYPE (@3)) > TYPE_PRECISION (TREE_TYPE (@2))
5348 && !wi::neg_p (wi::to_wide (@1)))
5349 (cmp (bit_and @0 (convert (bit_not @1)))
5350 { build_zero_cst (TREE_TYPE (@0)); })))
5352 /* (X | Y) == Y becomes (X & ~Y) == 0. */
5354 (cmp:c (bit_ior:c @0 @1) @1)
5355 (cmp (bit_and @0 (bit_not! @1)) { build_zero_cst (TREE_TYPE (@0)); }))
5358 /* (X ^ C1) op C2 can be rewritten as X op (C1 ^ C2). */
5360 (cmp (convert?@3 (bit_xor @0 INTEGER_CST@1)) INTEGER_CST@2)
5361 (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0)))
5362 (cmp @0 (bit_xor @1 (convert @2)))))
5365 (cmp (convert? addr@0) integer_zerop)
5366 (if (tree_single_nonzero_warnv_p (@0, NULL))
5367 { constant_boolean_node (cmp == NE_EXPR, type); }))
5369 /* (X & C) op (Y & C) into (X ^ Y) & C op 0. */
5371 (cmp (bit_and:cs @0 @2) (bit_and:cs @1 @2))
5372 (cmp (bit_and (bit_xor @0 @1) @2) { build_zero_cst (TREE_TYPE (@2)); })))
5374 /* (X < 0) != (Y < 0) into (X ^ Y) < 0.
5375 (X >= 0) != (Y >= 0) into (X ^ Y) < 0.
5376 (X < 0) == (Y < 0) into (X ^ Y) >= 0.
5377 (X >= 0) == (Y >= 0) into (X ^ Y) >= 0. */
5382 (cmp (sgncmp @0 integer_zerop@2) (sgncmp @1 integer_zerop))
5383 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
5384 && !TYPE_UNSIGNED (TREE_TYPE (@0))
5385 && types_match (@0, @1))
5386 (ncmp (bit_xor @0 @1) @2)))))
5387 /* (X < 0) == (Y >= 0) into (X ^ Y) < 0.
5388 (X < 0) != (Y >= 0) into (X ^ Y) >= 0. */
5392 (cmp:c (lt @0 integer_zerop@2) (ge @1 integer_zerop))
5393 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
5394 && !TYPE_UNSIGNED (TREE_TYPE (@0))
5395 && types_match (@0, @1))
5396 (ncmp (bit_xor @0 @1) @2))))
5398 /* If we have (A & C) == C where C is a power of 2, convert this into
5399 (A & C) != 0. Similarly for NE_EXPR. */
5403 (cmp (bit_and@2 @0 integer_pow2p@1) @1)
5404 (icmp @2 { build_zero_cst (TREE_TYPE (@0)); })))
5407 /* x < 0 ? ~y : y into (x >> (prec-1)) ^ y. */
5408 /* x >= 0 ? ~y : y into ~((x >> (prec-1)) ^ y). */
5410 (cond (cmp @0 integer_zerop) (bit_not @1) @1)
5411 (if (INTEGRAL_TYPE_P (type)
5412 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
5413 && !TYPE_UNSIGNED (TREE_TYPE (@0))
5414 && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (type))
5417 tree shifter = build_int_cst (integer_type_node, TYPE_PRECISION (type) - 1);
5419 (if (cmp == LT_EXPR)
5420 (bit_xor (convert (rshift @0 {shifter;})) @1)
5421 (bit_not (bit_xor (convert (rshift @0 {shifter;})) @1))))))
5422 /* x < 0 ? y : ~y into ~((x >> (prec-1)) ^ y). */
5423 /* x >= 0 ? y : ~y into (x >> (prec-1)) ^ y. */
5425 (cond (cmp @0 integer_zerop) @1 (bit_not @1))
5426 (if (INTEGRAL_TYPE_P (type)
5427 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
5428 && !TYPE_UNSIGNED (TREE_TYPE (@0))
5429 && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (type))
5432 tree shifter = build_int_cst (integer_type_node, TYPE_PRECISION (type) - 1);
5434 (if (cmp == GE_EXPR)
5435 (bit_xor (convert (rshift @0 {shifter;})) @1)
5436 (bit_not (bit_xor (convert (rshift @0 {shifter;})) @1)))))))
5438 /* If we have (A & C) != 0 ? D : 0 where C and D are powers of 2,
5439 convert this into a shift followed by ANDing with D. */
5442 (ne (bit_and @0 integer_pow2p@1) integer_zerop)
5443 INTEGER_CST@2 integer_zerop)
5444 (if (!POINTER_TYPE_P (type) && integer_pow2p (@2))
5446 int shift = (wi::exact_log2 (wi::to_wide (@2))
5447 - wi::exact_log2 (wi::to_wide (@1)));
5451 (lshift (convert @0) { build_int_cst (integer_type_node, shift); }) @2)
5453 (convert (rshift @0 { build_int_cst (integer_type_node, -shift); }))
5456 /* If we have (A & C) != 0 where C is the sign bit of A, convert
5457 this into A < 0. Similarly for (A & C) == 0 into A >= 0. */
5461 (cmp (bit_and (convert?@2 @0) integer_pow2p@1) integer_zerop)
5462 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
5463 && type_has_mode_precision_p (TREE_TYPE (@0))
5464 && element_precision (@2) >= element_precision (@0)
5465 && wi::only_sign_bit_p (wi::to_wide (@1), element_precision (@0)))
5466 (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
5467 (ncmp (convert:stype @0) { build_zero_cst (stype); })))))
5469 /* If we have A < 0 ? C : 0 where C is a power of 2, convert
5470 this into a right shift or sign extension followed by ANDing with C. */
5473 (lt @0 integer_zerop)
5474 INTEGER_CST@1 integer_zerop)
5475 (if (integer_pow2p (@1)
5476 && !TYPE_UNSIGNED (TREE_TYPE (@0)))
5478 int shift = element_precision (@0) - wi::exact_log2 (wi::to_wide (@1)) - 1;
5482 (convert (rshift @0 { build_int_cst (integer_type_node, shift); }))
5484 /* Otherwise ctype must be wider than TREE_TYPE (@0) and pure
5485 sign extension followed by AND with C will achieve the effect. */
5486 (bit_and (convert @0) @1)))))
5488 /* When the addresses are not directly of decls compare base and offset.
5489 This implements some remaining parts of fold_comparison address
5490 comparisons but still no complete part of it. Still it is good
5491 enough to make fold_stmt not regress when not dispatching to fold_binary. */
5492 (for cmp (simple_comparison)
5494 (cmp (convert1?@2 addr@0) (convert2? addr@1))
5497 poly_int64 off0, off1;
5499 int equal = address_compare (cmp, TREE_TYPE (@2), @0, @1, base0, base1,
5500 off0, off1, GENERIC);
5504 (if (cmp == EQ_EXPR && (known_eq (off0, off1) || known_ne (off0, off1)))
5505 { constant_boolean_node (known_eq (off0, off1), type); })
5506 (if (cmp == NE_EXPR && (known_eq (off0, off1) || known_ne (off0, off1)))
5507 { constant_boolean_node (known_ne (off0, off1), type); })
5508 (if (cmp == LT_EXPR && (known_lt (off0, off1) || known_ge (off0, off1)))
5509 { constant_boolean_node (known_lt (off0, off1), type); })
5510 (if (cmp == LE_EXPR && (known_le (off0, off1) || known_gt (off0, off1)))
5511 { constant_boolean_node (known_le (off0, off1), type); })
5512 (if (cmp == GE_EXPR && (known_ge (off0, off1) || known_lt (off0, off1)))
5513 { constant_boolean_node (known_ge (off0, off1), type); })
5514 (if (cmp == GT_EXPR && (known_gt (off0, off1) || known_le (off0, off1)))
5515 { constant_boolean_node (known_gt (off0, off1), type); }))
5518 (if (cmp == EQ_EXPR)
5519 { constant_boolean_node (false, type); })
5520 (if (cmp == NE_EXPR)
5521 { constant_boolean_node (true, type); })))))))
5523 /* Simplify pointer equality compares using PTA. */
5527 (if (POINTER_TYPE_P (TREE_TYPE (@0))
5528 && ptrs_compare_unequal (@0, @1))
5529 { constant_boolean_node (neeq != EQ_EXPR, type); })))
5531 /* PR70920: Transform (intptr_t)x eq/ne CST to x eq/ne (typeof x) CST.
5532 and (typeof ptr_cst) x eq/ne ptr_cst to x eq/ne (typeof x) CST.
5533 Disable the transform if either operand is pointer to function.
5534 This broke pr22051-2.c for arm where function pointer
5535 canonicalizaion is not wanted. */
5539 (cmp (convert @0) INTEGER_CST@1)
5540 (if (((POINTER_TYPE_P (TREE_TYPE (@0))
5541 && !FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@0)))
5542 && INTEGRAL_TYPE_P (TREE_TYPE (@1))
5543 /* Don't perform this optimization in GENERIC if @0 has reference
5544 type when sanitizing. See PR101210. */
5546 && TREE_CODE (TREE_TYPE (@0)) == REFERENCE_TYPE
5547 && (flag_sanitize & (SANITIZE_NULL | SANITIZE_ALIGNMENT))))
5548 || (INTEGRAL_TYPE_P (TREE_TYPE (@0))
5549 && POINTER_TYPE_P (TREE_TYPE (@1))
5550 && !FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@1)))))
5551 && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
5552 (cmp @0 (convert @1)))))
5554 /* Non-equality compare simplifications from fold_binary */
5555 (for cmp (lt gt le ge)
5556 /* Comparisons with the highest or lowest possible integer of
5557 the specified precision will have known values. */
5559 (cmp (convert?@2 @0) uniform_integer_cst_p@1)
5560 (if ((INTEGRAL_TYPE_P (TREE_TYPE (@1))
5561 || POINTER_TYPE_P (TREE_TYPE (@1))
5562 || VECTOR_INTEGER_TYPE_P (TREE_TYPE (@1)))
5563 && tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@0)))
5566 tree cst = uniform_integer_cst_p (@1);
5567 tree arg1_type = TREE_TYPE (cst);
5568 unsigned int prec = TYPE_PRECISION (arg1_type);
5569 wide_int max = wi::max_value (arg1_type);
5570 wide_int signed_max = wi::max_value (prec, SIGNED);
5571 wide_int min = wi::min_value (arg1_type);
5574 (if (wi::to_wide (cst) == max)
5576 (if (cmp == GT_EXPR)
5577 { constant_boolean_node (false, type); })
5578 (if (cmp == GE_EXPR)
5580 (if (cmp == LE_EXPR)
5581 { constant_boolean_node (true, type); })
5582 (if (cmp == LT_EXPR)
5584 (if (wi::to_wide (cst) == min)
5586 (if (cmp == LT_EXPR)
5587 { constant_boolean_node (false, type); })
5588 (if (cmp == LE_EXPR)
5590 (if (cmp == GE_EXPR)
5591 { constant_boolean_node (true, type); })
5592 (if (cmp == GT_EXPR)
5594 (if (wi::to_wide (cst) == max - 1)
5596 (if (cmp == GT_EXPR)
5597 (eq @2 { build_uniform_cst (TREE_TYPE (@1),
5598 wide_int_to_tree (TREE_TYPE (cst),
5601 (if (cmp == LE_EXPR)
5602 (ne @2 { build_uniform_cst (TREE_TYPE (@1),
5603 wide_int_to_tree (TREE_TYPE (cst),
5606 (if (wi::to_wide (cst) == min + 1)
5608 (if (cmp == GE_EXPR)
5609 (ne @2 { build_uniform_cst (TREE_TYPE (@1),
5610 wide_int_to_tree (TREE_TYPE (cst),
5613 (if (cmp == LT_EXPR)
5614 (eq @2 { build_uniform_cst (TREE_TYPE (@1),
5615 wide_int_to_tree (TREE_TYPE (cst),
5618 (if (wi::to_wide (cst) == signed_max
5619 && TYPE_UNSIGNED (arg1_type)
5620 /* We will flip the signedness of the comparison operator
5621 associated with the mode of @1, so the sign bit is
5622 specified by this mode. Check that @1 is the signed
5623 max associated with this sign bit. */
5624 && prec == GET_MODE_PRECISION (SCALAR_INT_TYPE_MODE (arg1_type))
5625 /* signed_type does not work on pointer types. */
5626 && INTEGRAL_TYPE_P (arg1_type))
5627 /* The following case also applies to X < signed_max+1
5628 and X >= signed_max+1 because previous transformations. */
5629 (if (cmp == LE_EXPR || cmp == GT_EXPR)
5630 (with { tree st = signed_type_for (TREE_TYPE (@1)); }
5632 (if (cst == @1 && cmp == LE_EXPR)
5633 (ge (convert:st @0) { build_zero_cst (st); }))
5634 (if (cst == @1 && cmp == GT_EXPR)
5635 (lt (convert:st @0) { build_zero_cst (st); }))
5636 (if (cmp == LE_EXPR)
5637 (ge (view_convert:st @0) { build_zero_cst (st); }))
5638 (if (cmp == GT_EXPR)
5639 (lt (view_convert:st @0) { build_zero_cst (st); })))))))))))
5641 (for cmp (unordered ordered unlt unle ungt unge uneq ltgt)
5642 /* If the second operand is NaN, the result is constant. */
5645 (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
5646 && (cmp != LTGT_EXPR || ! flag_trapping_math))
5647 { constant_boolean_node (cmp == ORDERED_EXPR || cmp == LTGT_EXPR
5648 ? false : true, type); })))
5650 /* Fold UNORDERED if either operand must be NaN, or neither can be. */
5654 (if (tree_expr_nan_p (@0) || tree_expr_nan_p (@1))
5655 { constant_boolean_node (true, type); })
5656 (if (!tree_expr_maybe_nan_p (@0) && !tree_expr_maybe_nan_p (@1))
5657 { constant_boolean_node (false, type); })))
5659 /* Fold ORDERED if either operand must be NaN, or neither can be. */
5663 (if (tree_expr_nan_p (@0) || tree_expr_nan_p (@1))
5664 { constant_boolean_node (false, type); })
5665 (if (!tree_expr_maybe_nan_p (@0) && !tree_expr_maybe_nan_p (@1))
5666 { constant_boolean_node (true, type); })))
5668 /* bool_var != 0 becomes bool_var. */
5670 (ne @0 integer_zerop)
5671 (if (TREE_CODE (TREE_TYPE (@0)) == BOOLEAN_TYPE
5672 && types_match (type, TREE_TYPE (@0)))
5674 /* bool_var == 1 becomes bool_var. */
5676 (eq @0 integer_onep)
5677 (if (TREE_CODE (TREE_TYPE (@0)) == BOOLEAN_TYPE
5678 && types_match (type, TREE_TYPE (@0)))
5681 bool_var == 0 becomes !bool_var or
5682 bool_var != 1 becomes !bool_var
5683 here because that only is good in assignment context as long
5684 as we require a tcc_comparison in GIMPLE_CONDs where we'd
5685 replace if (x == 0) with tem = ~x; if (tem != 0) which is
5686 clearly less optimal and which we'll transform again in forwprop. */
5688 /* When one argument is a constant, overflow detection can be simplified.
5689 Currently restricted to single use so as not to interfere too much with
5690 ADD_OVERFLOW detection in tree-ssa-math-opts.c.
5691 CONVERT?(CONVERT?(A) + CST) CMP A -> A CMP' CST' */
5692 (for cmp (lt le ge gt)
5695 (cmp:c (convert?@3 (plus@2 (convert?@4 @0) INTEGER_CST@1)) @0)
5696 (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@2))
5697 && types_match (TREE_TYPE (@0), TREE_TYPE (@3))
5698 && tree_nop_conversion_p (TREE_TYPE (@4), TREE_TYPE (@0))
5699 && wi::to_wide (@1) != 0
5702 unsigned int prec = TYPE_PRECISION (TREE_TYPE (@0));
5703 signop sign = TYPE_SIGN (TREE_TYPE (@0));
5705 (out @0 { wide_int_to_tree (TREE_TYPE (@0),
5706 wi::max_value (prec, sign)
5707 - wi::to_wide (@1)); })))))
5709 /* To detect overflow in unsigned A - B, A < B is simpler than A - B > A.
5710 However, the detection logic for SUB_OVERFLOW in tree-ssa-math-opts.c
5711 expects the long form, so we restrict the transformation for now. */
5714 (cmp:c (minus@2 @0 @1) @0)
5715 (if (single_use (@2)
5716 && ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
5717 && TYPE_UNSIGNED (TREE_TYPE (@0)))
5720 /* Optimize A - B + -1 >= A into B >= A for unsigned comparisons. */
5723 (cmp:c (plus (minus @0 @1) integer_minus_onep) @0)
5724 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
5725 && TYPE_UNSIGNED (TREE_TYPE (@0)))
5728 /* Testing for overflow is unnecessary if we already know the result. */
5733 (cmp:c (realpart (IFN_SUB_OVERFLOW@2 @0 @1)) @0)
5734 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
5735 && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
5736 (out (imagpart @2) { build_zero_cst (TREE_TYPE (@0)); }))))
5741 (cmp:c (realpart (IFN_ADD_OVERFLOW:c@2 @0 @1)) @0)
5742 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
5743 && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
5744 (out (imagpart @2) { build_zero_cst (TREE_TYPE (@0)); }))))
5746 /* For unsigned operands, -1 / B < A checks whether A * B would overflow.
5747 Simplify it to __builtin_mul_overflow (A, B, <unused>). */
5751 (cmp:c (trunc_div:s integer_all_onesp @1) @0)
5752 (if (TYPE_UNSIGNED (TREE_TYPE (@0)) && !VECTOR_TYPE_P (TREE_TYPE (@0)))
5753 (with { tree t = TREE_TYPE (@0), cpx = build_complex_type (t); }
5754 (out (imagpart (IFN_MUL_OVERFLOW:cpx @0 @1)) { build_zero_cst (t); })))))
5756 /* Similarly, for unsigned operands, (((type) A * B) >> prec) != 0 where type
5757 is at least twice as wide as type of A and B, simplify to
5758 __builtin_mul_overflow (A, B, <unused>). */
5761 (cmp (rshift (mult:s (convert@3 @0) (convert @1)) INTEGER_CST@2)
5763 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
5764 && INTEGRAL_TYPE_P (TREE_TYPE (@3))
5765 && TYPE_UNSIGNED (TREE_TYPE (@0))
5766 && (TYPE_PRECISION (TREE_TYPE (@3))
5767 >= 2 * TYPE_PRECISION (TREE_TYPE (@0)))
5768 && tree_fits_uhwi_p (@2)
5769 && tree_to_uhwi (@2) == TYPE_PRECISION (TREE_TYPE (@0))
5770 && types_match (@0, @1)
5771 && type_has_mode_precision_p (TREE_TYPE (@0))
5772 && (optab_handler (umulv4_optab, TYPE_MODE (TREE_TYPE (@0)))
5773 != CODE_FOR_nothing))
5774 (with { tree t = TREE_TYPE (@0), cpx = build_complex_type (t); }
5775 (cmp (imagpart (IFN_MUL_OVERFLOW:cpx @0 @1)) { build_zero_cst (t); })))))
5777 /* Demote operands of IFN_{ADD,SUB,MUL}_OVERFLOW. */
5778 (for ovf (IFN_ADD_OVERFLOW IFN_SUB_OVERFLOW IFN_MUL_OVERFLOW)
5780 (ovf (convert@2 @0) @1)
5781 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
5782 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
5783 && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
5784 && (!TYPE_UNSIGNED (TREE_TYPE (@2)) || TYPE_UNSIGNED (TREE_TYPE (@0))))
5787 (ovf @1 (convert@2 @0))
5788 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
5789 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
5790 && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
5791 && (!TYPE_UNSIGNED (TREE_TYPE (@2)) || TYPE_UNSIGNED (TREE_TYPE (@0))))
5794 /* Simplification of math builtins. These rules must all be optimizations
5795 as well as IL simplifications. If there is a possibility that the new
5796 form could be a pessimization, the rule should go in the canonicalization
5797 section that follows this one.
5799 Rules can generally go in this section if they satisfy one of
5802 - the rule describes an identity
5804 - the rule replaces calls with something as simple as addition or
5807 - the rule contains unary calls only and simplifies the surrounding
5808 arithmetic. (The idea here is to exclude non-unary calls in which
5809 one operand is constant and in which the call is known to be cheap
5810 when the operand has that value.) */
5812 (if (flag_unsafe_math_optimizations)
5813 /* Simplify sqrt(x) * sqrt(x) -> x. */
5815 (mult (SQRT_ALL@1 @0) @1)
5816 (if (!tree_expr_maybe_signaling_nan_p (@0))
5819 (for op (plus minus)
5820 /* Simplify (A / C) +- (B / C) -> (A +- B) / C. */
5824 (rdiv (op @0 @2) @1)))
5826 (for cmp (lt le gt ge)
5827 neg_cmp (gt ge lt le)
5828 /* Simplify (x * C1) cmp C2 -> x cmp (C2 / C1), where C1 != 0. */
5830 (cmp (mult @0 REAL_CST@1) REAL_CST@2)
5832 { tree tem = const_binop (RDIV_EXPR, type, @2, @1); }
5834 && !(REAL_VALUE_ISINF (TREE_REAL_CST (tem))
5835 || (real_zerop (tem) && !real_zerop (@1))))
5837 (if (real_less (&dconst0, TREE_REAL_CST_PTR (@1)))
5839 (if (real_less (TREE_REAL_CST_PTR (@1), &dconst0))
5840 (neg_cmp @0 { tem; })))))))
5842 /* Simplify sqrt(x) * sqrt(y) -> sqrt(x*y). */
5843 (for root (SQRT CBRT)
5845 (mult (root:s @0) (root:s @1))
5846 (root (mult @0 @1))))
5848 /* Simplify expN(x) * expN(y) -> expN(x+y). */
5849 (for exps (EXP EXP2 EXP10 POW10)
5851 (mult (exps:s @0) (exps:s @1))
5852 (exps (plus @0 @1))))
5854 /* Simplify a/root(b/c) into a*root(c/b). */
5855 (for root (SQRT CBRT)
5857 (rdiv @0 (root:s (rdiv:s @1 @2)))
5858 (mult @0 (root (rdiv @2 @1)))))
5860 /* Simplify x/expN(y) into x*expN(-y). */
5861 (for exps (EXP EXP2 EXP10 POW10)
5863 (rdiv @0 (exps:s @1))
5864 (mult @0 (exps (negate @1)))))
5866 (for logs (LOG LOG2 LOG10 LOG10)
5867 exps (EXP EXP2 EXP10 POW10)
5868 /* logN(expN(x)) -> x. */
5872 /* expN(logN(x)) -> x. */
5877 /* Optimize logN(func()) for various exponential functions. We
5878 want to determine the value "x" and the power "exponent" in
5879 order to transform logN(x**exponent) into exponent*logN(x). */
5880 (for logs (LOG LOG LOG LOG2 LOG2 LOG2 LOG10 LOG10)
5881 exps (EXP2 EXP10 POW10 EXP EXP10 POW10 EXP EXP2)
5884 (if (SCALAR_FLOAT_TYPE_P (type))
5890 /* Prepare to do logN(exp(exponent)) -> exponent*logN(e). */
5891 x = build_real_truncate (type, dconst_e ());
5894 /* Prepare to do logN(exp2(exponent)) -> exponent*logN(2). */
5895 x = build_real (type, dconst2);
5899 /* Prepare to do logN(exp10(exponent)) -> exponent*logN(10). */
5901 REAL_VALUE_TYPE dconst10;
5902 real_from_integer (&dconst10, VOIDmode, 10, SIGNED);
5903 x = build_real (type, dconst10);
5910 (mult (logs { x; }) @0)))))
5918 (if (SCALAR_FLOAT_TYPE_P (type))
5924 /* Prepare to do logN(sqrt(x)) -> 0.5*logN(x). */
5925 x = build_real (type, dconsthalf);
5928 /* Prepare to do logN(cbrt(x)) -> (1/3)*logN(x). */
5929 x = build_real_truncate (type, dconst_third ());
5935 (mult { x; } (logs @0))))))
5937 /* logN(pow(x,exponent)) -> exponent*logN(x). */
5938 (for logs (LOG LOG2 LOG10)
5942 (mult @1 (logs @0))))
5944 /* pow(C,x) -> exp(log(C)*x) if C > 0,
5945 or if C is a positive power of 2,
5946 pow(C,x) -> exp2(log2(C)*x). */
5954 (pows REAL_CST@0 @1)
5955 (if (real_compare (GT_EXPR, TREE_REAL_CST_PTR (@0), &dconst0)
5956 && real_isfinite (TREE_REAL_CST_PTR (@0))
5957 /* As libmvec doesn't have a vectorized exp2, defer optimizing
5958 the use_exp2 case until after vectorization. It seems actually
5959 beneficial for all constants to postpone this until later,
5960 because exp(log(C)*x), while faster, will have worse precision
5961 and if x folds into a constant too, that is unnecessary
5963 && canonicalize_math_after_vectorization_p ())
5965 const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (@0);
5966 bool use_exp2 = false;
5967 if (targetm.libc_has_function (function_c99_misc, TREE_TYPE (@0))
5968 && value->cl == rvc_normal)
5970 REAL_VALUE_TYPE frac_rvt = *value;
5971 SET_REAL_EXP (&frac_rvt, 1);
5972 if (real_equal (&frac_rvt, &dconst1))
5977 (if (optimize_pow_to_exp (@0, @1))
5978 (exps (mult (logs @0) @1)))
5979 (exp2s (mult (log2s @0) @1)))))))
5982 /* pow(C,x)*expN(y) -> expN(logN(C)*x+y) if C > 0. */
5984 exps (EXP EXP2 EXP10 POW10)
5985 logs (LOG LOG2 LOG10 LOG10)
5987 (mult:c (pows:s REAL_CST@0 @1) (exps:s @2))
5988 (if (real_compare (GT_EXPR, TREE_REAL_CST_PTR (@0), &dconst0)
5989 && real_isfinite (TREE_REAL_CST_PTR (@0)))
5990 (exps (plus (mult (logs @0) @1) @2)))))
5995 exps (EXP EXP2 EXP10 POW10)
5996 /* sqrt(expN(x)) -> expN(x*0.5). */
5999 (exps (mult @0 { build_real (type, dconsthalf); })))
6000 /* cbrt(expN(x)) -> expN(x/3). */
6003 (exps (mult @0 { build_real_truncate (type, dconst_third ()); })))
6004 /* pow(expN(x), y) -> expN(x*y). */
6007 (exps (mult @0 @1))))
6009 /* tan(atan(x)) -> x. */
6016 /* Simplify sin(atan(x)) -> x / sqrt(x*x + 1). */
6020 copysigns (COPYSIGN)
6025 REAL_VALUE_TYPE r_cst;
6026 build_sinatan_real (&r_cst, type);
6027 tree t_cst = build_real (type, r_cst);
6028 tree t_one = build_one_cst (type);
6030 (if (SCALAR_FLOAT_TYPE_P (type))
6031 (cond (lt (abs @0) { t_cst; })
6032 (rdiv @0 (sqrts (plus (mult @0 @0) { t_one; })))
6033 (copysigns { t_one; } @0))))))
6035 /* Simplify cos(atan(x)) -> 1 / sqrt(x*x + 1). */
6039 copysigns (COPYSIGN)
6044 REAL_VALUE_TYPE r_cst;
6045 build_sinatan_real (&r_cst, type);
6046 tree t_cst = build_real (type, r_cst);
6047 tree t_one = build_one_cst (type);
6048 tree t_zero = build_zero_cst (type);
6050 (if (SCALAR_FLOAT_TYPE_P (type))
6051 (cond (lt (abs @0) { t_cst; })
6052 (rdiv { t_one; } (sqrts (plus (mult @0 @0) { t_one; })))
6053 (copysigns { t_zero; } @0))))))
6055 (if (!flag_errno_math)
6056 /* Simplify sinh(atanh(x)) -> x / sqrt((1 - x)*(1 + x)). */
6061 (sinhs (atanhs:s @0))
6062 (with { tree t_one = build_one_cst (type); }
6063 (rdiv @0 (sqrts (mult (minus { t_one; } @0) (plus { t_one; } @0)))))))
6065 /* Simplify cosh(atanh(x)) -> 1 / sqrt((1 - x)*(1 + x)) */
6070 (coshs (atanhs:s @0))
6071 (with { tree t_one = build_one_cst (type); }
6072 (rdiv { t_one; } (sqrts (mult (minus { t_one; } @0) (plus { t_one; } @0))))))))
6074 /* cabs(x+0i) or cabs(0+xi) -> abs(x). */
6076 (CABS (complex:C @0 real_zerop@1))
6079 /* trunc(trunc(x)) -> trunc(x), etc. */
6080 (for fns (TRUNC_ALL FLOOR_ALL CEIL_ALL ROUND_ALL NEARBYINT_ALL RINT_ALL)
6084 /* f(x) -> x if x is integer valued and f does nothing for such values. */
6085 (for fns (TRUNC_ALL FLOOR_ALL CEIL_ALL ROUND_ALL NEARBYINT_ALL RINT_ALL)
6087 (fns integer_valued_real_p@0)
6090 /* hypot(x,0) and hypot(0,x) -> abs(x). */
6092 (HYPOT:c @0 real_zerop@1)
6095 /* pow(1,x) -> 1. */
6097 (POW real_onep@0 @1)
6101 /* copysign(x,x) -> x. */
6102 (COPYSIGN_ALL @0 @0)
6106 /* copysign(x,-x) -> -x. */
6107 (COPYSIGN_ALL @0 (negate@1 @0))
6111 /* copysign(x,y) -> fabs(x) if y is nonnegative. */
6112 (COPYSIGN_ALL @0 tree_expr_nonnegative_p@1)
6115 (for scale (LDEXP SCALBN SCALBLN)
6116 /* ldexp(0, x) -> 0. */
6118 (scale real_zerop@0 @1)
6120 /* ldexp(x, 0) -> x. */
6122 (scale @0 integer_zerop@1)
6124 /* ldexp(x, y) -> x if x is +-Inf or NaN. */
6126 (scale REAL_CST@0 @1)
6127 (if (!real_isfinite (TREE_REAL_CST_PTR (@0)))
6130 /* Canonicalization of sequences of math builtins. These rules represent
6131 IL simplifications but are not necessarily optimizations.
6133 The sincos pass is responsible for picking "optimal" implementations
6134 of math builtins, which may be more complicated and can sometimes go
6135 the other way, e.g. converting pow into a sequence of sqrts.
6136 We only want to do these canonicalizations before the pass has run. */
6138 (if (flag_unsafe_math_optimizations && canonicalize_math_p ())
6139 /* Simplify tan(x) * cos(x) -> sin(x). */
6141 (mult:c (TAN:s @0) (COS:s @0))
6144 /* Simplify x * pow(x,c) -> pow(x,c+1). */
6146 (mult:c @0 (POW:s @0 REAL_CST@1))
6147 (if (!TREE_OVERFLOW (@1))
6148 (POW @0 (plus @1 { build_one_cst (type); }))))
6150 /* Simplify sin(x) / cos(x) -> tan(x). */
6152 (rdiv (SIN:s @0) (COS:s @0))
6155 /* Simplify sinh(x) / cosh(x) -> tanh(x). */
6157 (rdiv (SINH:s @0) (COSH:s @0))
6160 /* Simplify tanh (x) / sinh (x) -> 1.0 / cosh (x). */
6162 (rdiv (TANH:s @0) (SINH:s @0))
6163 (rdiv {build_one_cst (type);} (COSH @0)))
6165 /* Simplify cos(x) / sin(x) -> 1 / tan(x). */
6167 (rdiv (COS:s @0) (SIN:s @0))
6168 (rdiv { build_one_cst (type); } (TAN @0)))
6170 /* Simplify sin(x) / tan(x) -> cos(x). */
6172 (rdiv (SIN:s @0) (TAN:s @0))
6173 (if (! HONOR_NANS (@0)
6174 && ! HONOR_INFINITIES (@0))
6177 /* Simplify tan(x) / sin(x) -> 1.0 / cos(x). */
6179 (rdiv (TAN:s @0) (SIN:s @0))
6180 (if (! HONOR_NANS (@0)
6181 && ! HONOR_INFINITIES (@0))
6182 (rdiv { build_one_cst (type); } (COS @0))))
6184 /* Simplify pow(x,y) * pow(x,z) -> pow(x,y+z). */
6186 (mult (POW:s @0 @1) (POW:s @0 @2))
6187 (POW @0 (plus @1 @2)))
6189 /* Simplify pow(x,y) * pow(z,y) -> pow(x*z,y). */
6191 (mult (POW:s @0 @1) (POW:s @2 @1))
6192 (POW (mult @0 @2) @1))
6194 /* Simplify powi(x,y) * powi(z,y) -> powi(x*z,y). */
6196 (mult (POWI:s @0 @1) (POWI:s @2 @1))
6197 (POWI (mult @0 @2) @1))
6199 /* Simplify pow(x,c) / x -> pow(x,c-1). */
6201 (rdiv (POW:s @0 REAL_CST@1) @0)
6202 (if (!TREE_OVERFLOW (@1))
6203 (POW @0 (minus @1 { build_one_cst (type); }))))
6205 /* Simplify x / pow (y,z) -> x * pow(y,-z). */
6207 (rdiv @0 (POW:s @1 @2))
6208 (mult @0 (POW @1 (negate @2))))
6213 /* sqrt(sqrt(x)) -> pow(x,1/4). */
6216 (pows @0 { build_real (type, dconst_quarter ()); }))
6217 /* sqrt(cbrt(x)) -> pow(x,1/6). */
6220 (pows @0 { build_real_truncate (type, dconst_sixth ()); }))
6221 /* cbrt(sqrt(x)) -> pow(x,1/6). */
6224 (pows @0 { build_real_truncate (type, dconst_sixth ()); }))
6225 /* cbrt(cbrt(x)) -> pow(x,1/9), iff x is nonnegative. */
6227 (cbrts (cbrts tree_expr_nonnegative_p@0))
6228 (pows @0 { build_real_truncate (type, dconst_ninth ()); }))
6229 /* sqrt(pow(x,y)) -> pow(|x|,y*0.5). */
6231 (sqrts (pows @0 @1))
6232 (pows (abs @0) (mult @1 { build_real (type, dconsthalf); })))
6233 /* cbrt(pow(x,y)) -> pow(x,y/3), iff x is nonnegative. */
6235 (cbrts (pows tree_expr_nonnegative_p@0 @1))
6236 (pows @0 (mult @1 { build_real_truncate (type, dconst_third ()); })))
6237 /* pow(sqrt(x),y) -> pow(x,y*0.5). */
6239 (pows (sqrts @0) @1)
6240 (pows @0 (mult @1 { build_real (type, dconsthalf); })))
6241 /* pow(cbrt(x),y) -> pow(x,y/3) iff x is nonnegative. */
6243 (pows (cbrts tree_expr_nonnegative_p@0) @1)
6244 (pows @0 (mult @1 { build_real_truncate (type, dconst_third ()); })))
6245 /* pow(pow(x,y),z) -> pow(x,y*z) iff x is nonnegative. */
6247 (pows (pows tree_expr_nonnegative_p@0 @1) @2)
6248 (pows @0 (mult @1 @2))))
6250 /* cabs(x+xi) -> fabs(x)*sqrt(2). */
6252 (CABS (complex @0 @0))
6253 (mult (abs @0) { build_real_truncate (type, dconst_sqrt2 ()); }))
6255 /* hypot(x,x) -> fabs(x)*sqrt(2). */
6258 (mult (abs @0) { build_real_truncate (type, dconst_sqrt2 ()); }))
6260 /* cexp(x+yi) -> exp(x)*cexpi(y). */
6265 (cexps compositional_complex@0)
6266 (if (targetm.libc_has_function (function_c99_math_complex, TREE_TYPE (@0)))
6268 (mult (exps@1 (realpart @0)) (realpart (cexpis:type@2 (imagpart @0))))
6269 (mult @1 (imagpart @2)))))))
6271 (if (canonicalize_math_p ())
6272 /* floor(x) -> trunc(x) if x is nonnegative. */
6273 (for floors (FLOOR_ALL)
6276 (floors tree_expr_nonnegative_p@0)
6279 (match double_value_p
6281 (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == double_type_node)))
6282 (for froms (BUILT_IN_TRUNCL
6294 /* truncl(extend(x)) -> extend(trunc(x)), etc., if x is a double. */
6295 (if (optimize && canonicalize_math_p ())
6297 (froms (convert double_value_p@0))
6298 (convert (tos @0)))))
6300 (match float_value_p
6302 (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == float_type_node)))
6303 (for froms (BUILT_IN_TRUNCL BUILT_IN_TRUNC
6304 BUILT_IN_FLOORL BUILT_IN_FLOOR
6305 BUILT_IN_CEILL BUILT_IN_CEIL
6306 BUILT_IN_ROUNDL BUILT_IN_ROUND
6307 BUILT_IN_NEARBYINTL BUILT_IN_NEARBYINT
6308 BUILT_IN_RINTL BUILT_IN_RINT)
6309 tos (BUILT_IN_TRUNCF BUILT_IN_TRUNCF
6310 BUILT_IN_FLOORF BUILT_IN_FLOORF
6311 BUILT_IN_CEILF BUILT_IN_CEILF
6312 BUILT_IN_ROUNDF BUILT_IN_ROUNDF
6313 BUILT_IN_NEARBYINTF BUILT_IN_NEARBYINTF
6314 BUILT_IN_RINTF BUILT_IN_RINTF)
6315 /* truncl(extend(x)) and trunc(extend(x)) -> extend(truncf(x)), etc.,
6317 (if (optimize && canonicalize_math_p ()
6318 && targetm.libc_has_function (function_c99_misc, NULL_TREE))
6320 (froms (convert float_value_p@0))
6321 (convert (tos @0)))))
6324 (match float16_value_p
6326 (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == float16_type_node)))
6327 (for froms (BUILT_IN_TRUNCL BUILT_IN_TRUNC BUILT_IN_TRUNCF
6328 BUILT_IN_FLOORL BUILT_IN_FLOOR BUILT_IN_FLOORF
6329 BUILT_IN_CEILL BUILT_IN_CEIL BUILT_IN_CEILF
6330 BUILT_IN_ROUNDEVENL BUILT_IN_ROUNDEVEN BUILT_IN_ROUNDEVENF
6331 BUILT_IN_ROUNDL BUILT_IN_ROUND BUILT_IN_ROUNDF
6332 BUILT_IN_NEARBYINTL BUILT_IN_NEARBYINT BUILT_IN_NEARBYINTF
6333 BUILT_IN_RINTL BUILT_IN_RINT BUILT_IN_RINTF
6334 BUILT_IN_SQRTL BUILT_IN_SQRT BUILT_IN_SQRTF)
6335 tos (IFN_TRUNC IFN_TRUNC IFN_TRUNC
6336 IFN_FLOOR IFN_FLOOR IFN_FLOOR
6337 IFN_CEIL IFN_CEIL IFN_CEIL
6338 IFN_ROUNDEVEN IFN_ROUNDEVEN IFN_ROUNDEVEN
6339 IFN_ROUND IFN_ROUND IFN_ROUND
6340 IFN_NEARBYINT IFN_NEARBYINT IFN_NEARBYINT
6341 IFN_RINT IFN_RINT IFN_RINT
6342 IFN_SQRT IFN_SQRT IFN_SQRT)
6343 /* (_Float16) round ((doube) x) -> __built_in_roundf16 (x), etc.,
6344 if x is a _Float16. */
6346 (convert (froms (convert float16_value_p@0)))
6348 && types_match (type, TREE_TYPE (@0))
6349 && direct_internal_fn_supported_p (as_internal_fn (tos),
6350 type, OPTIMIZE_FOR_BOTH))
6353 /* Simplify (trunc)copysign ((extend)x, (extend)y) to copysignf (x, y),
6354 x,y is float value, similar for _Float16/double. */
6355 (for copysigns (COPYSIGN_ALL)
6357 (convert (copysigns (convert@2 @0) (convert @1)))
6359 && !HONOR_SNANS (@2)
6360 && types_match (type, TREE_TYPE (@0))
6361 && types_match (type, TREE_TYPE (@1))
6362 && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (@2))
6363 && direct_internal_fn_supported_p (IFN_COPYSIGN,
6364 type, OPTIMIZE_FOR_BOTH))
6365 (IFN_COPYSIGN @0 @1))))
6367 (for froms (BUILT_IN_FMAF BUILT_IN_FMA BUILT_IN_FMAL)
6368 tos (IFN_FMA IFN_FMA IFN_FMA)
6370 (convert (froms (convert@3 @0) (convert @1) (convert @2)))
6371 (if (flag_unsafe_math_optimizations
6373 && FLOAT_TYPE_P (type)
6374 && FLOAT_TYPE_P (TREE_TYPE (@3))
6375 && types_match (type, TREE_TYPE (@0))
6376 && types_match (type, TREE_TYPE (@1))
6377 && types_match (type, TREE_TYPE (@2))
6378 && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (@3))
6379 && direct_internal_fn_supported_p (as_internal_fn (tos),
6380 type, OPTIMIZE_FOR_BOTH))
6383 (for maxmin (max min)
6385 (convert (maxmin (convert@2 @0) (convert @1)))
6387 && FLOAT_TYPE_P (type)
6388 && FLOAT_TYPE_P (TREE_TYPE (@2))
6389 && types_match (type, TREE_TYPE (@0))
6390 && types_match (type, TREE_TYPE (@1))
6391 && element_precision (type) < element_precision (TREE_TYPE (@2)))
6395 (for froms (XFLOORL XCEILL XROUNDL XRINTL)
6396 tos (XFLOOR XCEIL XROUND XRINT)
6397 /* llfloorl(extend(x)) -> llfloor(x), etc., if x is a double. */
6398 (if (optimize && canonicalize_math_p ())
6400 (froms (convert double_value_p@0))
6403 (for froms (XFLOORL XCEILL XROUNDL XRINTL
6404 XFLOOR XCEIL XROUND XRINT)
6405 tos (XFLOORF XCEILF XROUNDF XRINTF)
6406 /* llfloorl(extend(x)) and llfloor(extend(x)) -> llfloorf(x), etc.,
6408 (if (optimize && canonicalize_math_p ())
6410 (froms (convert float_value_p@0))
6413 (if (canonicalize_math_p ())
6414 /* xfloor(x) -> fix_trunc(x) if x is nonnegative. */
6415 (for floors (IFLOOR LFLOOR LLFLOOR)
6417 (floors tree_expr_nonnegative_p@0)
6420 (if (canonicalize_math_p ())
6421 /* xfloor(x) -> fix_trunc(x), etc., if x is integer valued. */
6422 (for fns (IFLOOR LFLOOR LLFLOOR
6424 IROUND LROUND LLROUND)
6426 (fns integer_valued_real_p@0)
6428 (if (!flag_errno_math)
6429 /* xrint(x) -> fix_trunc(x), etc., if x is integer valued. */
6430 (for rints (IRINT LRINT LLRINT)
6432 (rints integer_valued_real_p@0)
6435 (if (canonicalize_math_p ())
6436 (for ifn (IFLOOR ICEIL IROUND IRINT)
6437 lfn (LFLOOR LCEIL LROUND LRINT)
6438 llfn (LLFLOOR LLCEIL LLROUND LLRINT)
6439 /* Canonicalize iround (x) to lround (x) on ILP32 targets where
6440 sizeof (int) == sizeof (long). */
6441 (if (TYPE_PRECISION (integer_type_node)
6442 == TYPE_PRECISION (long_integer_type_node))
6445 (lfn:long_integer_type_node @0)))
6446 /* Canonicalize llround (x) to lround (x) on LP64 targets where
6447 sizeof (long long) == sizeof (long). */
6448 (if (TYPE_PRECISION (long_long_integer_type_node)
6449 == TYPE_PRECISION (long_integer_type_node))
6452 (lfn:long_integer_type_node @0)))))
6454 /* cproj(x) -> x if we're ignoring infinities. */
6457 (if (!HONOR_INFINITIES (type))
6460 /* If the real part is inf and the imag part is known to be
6461 nonnegative, return (inf + 0i). */
6463 (CPROJ (complex REAL_CST@0 tree_expr_nonnegative_p@1))
6464 (if (real_isinf (TREE_REAL_CST_PTR (@0)))
6465 { build_complex_inf (type, false); }))
6467 /* If the imag part is inf, return (inf+I*copysign(0,imag)). */
6469 (CPROJ (complex @0 REAL_CST@1))
6470 (if (real_isinf (TREE_REAL_CST_PTR (@1)))
6471 { build_complex_inf (type, TREE_REAL_CST_PTR (@1)->sign); }))
6477 (pows @0 REAL_CST@1)
6479 const REAL_VALUE_TYPE *value = TREE_REAL_CST_PTR (@1);
6480 REAL_VALUE_TYPE tmp;
6483 /* pow(x,0) -> 1. */
6484 (if (real_equal (value, &dconst0))
6485 { build_real (type, dconst1); })
6486 /* pow(x,1) -> x. */
6487 (if (real_equal (value, &dconst1))
6489 /* pow(x,-1) -> 1/x. */
6490 (if (real_equal (value, &dconstm1))
6491 (rdiv { build_real (type, dconst1); } @0))
6492 /* pow(x,0.5) -> sqrt(x). */
6493 (if (flag_unsafe_math_optimizations
6494 && canonicalize_math_p ()
6495 && real_equal (value, &dconsthalf))
6497 /* pow(x,1/3) -> cbrt(x). */
6498 (if (flag_unsafe_math_optimizations
6499 && canonicalize_math_p ()
6500 && (tmp = real_value_truncate (TYPE_MODE (type), dconst_third ()),
6501 real_equal (value, &tmp)))
6504 /* powi(1,x) -> 1. */
6506 (POWI real_onep@0 @1)
6510 (POWI @0 INTEGER_CST@1)
6512 /* powi(x,0) -> 1. */
6513 (if (wi::to_wide (@1) == 0)
6514 { build_real (type, dconst1); })
6515 /* powi(x,1) -> x. */
6516 (if (wi::to_wide (@1) == 1)
6518 /* powi(x,-1) -> 1/x. */
6519 (if (wi::to_wide (@1) == -1)
6520 (rdiv { build_real (type, dconst1); } @0))))
6522 /* Narrowing of arithmetic and logical operations.
6524 These are conceptually similar to the transformations performed for
6525 the C/C++ front-ends by shorten_binary_op and shorten_compare. Long
6526 term we want to move all that code out of the front-ends into here. */
6528 /* Convert (outertype)((innertype0)a+(innertype1)b)
6529 into ((newtype)a+(newtype)b) where newtype
6530 is the widest mode from all of these. */
6531 (for op (plus minus mult rdiv)
6533 (convert (op:s@0 (convert1?@3 @1) (convert2?@4 @2)))
6534 /* If we have a narrowing conversion of an arithmetic operation where
6535 both operands are widening conversions from the same type as the outer
6536 narrowing conversion. Then convert the innermost operands to a
6537 suitable unsigned type (to avoid introducing undefined behavior),
6538 perform the operation and convert the result to the desired type. */
6539 (if (INTEGRAL_TYPE_P (type)
6542 /* We check for type compatibility between @0 and @1 below,
6543 so there's no need to check that @2/@4 are integral types. */
6544 && INTEGRAL_TYPE_P (TREE_TYPE (@1))
6545 && INTEGRAL_TYPE_P (TREE_TYPE (@3))
6546 /* The precision of the type of each operand must match the
6547 precision of the mode of each operand, similarly for the
6549 && type_has_mode_precision_p (TREE_TYPE (@1))
6550 && type_has_mode_precision_p (TREE_TYPE (@2))
6551 && type_has_mode_precision_p (type)
6552 /* The inner conversion must be a widening conversion. */
6553 && TYPE_PRECISION (TREE_TYPE (@3)) > TYPE_PRECISION (TREE_TYPE (@1))
6554 && types_match (@1, type)
6555 && (types_match (@1, @2)
6556 /* Or the second operand is const integer or converted const
6557 integer from valueize. */
6558 || poly_int_tree_p (@4)))
6559 (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
6560 (op @1 (convert @2))
6561 (with { tree utype = unsigned_type_for (TREE_TYPE (@1)); }
6562 (convert (op (convert:utype @1)
6563 (convert:utype @2)))))
6564 (if (FLOAT_TYPE_P (type)
6565 && DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0))
6566 == DECIMAL_FLOAT_TYPE_P (type))
6567 (with { tree arg0 = strip_float_extensions (@1);
6568 tree arg1 = strip_float_extensions (@2);
6569 tree itype = TREE_TYPE (@0);
6570 tree ty1 = TREE_TYPE (arg0);
6571 tree ty2 = TREE_TYPE (arg1);
6572 enum tree_code code = TREE_CODE (itype); }
6573 (if (FLOAT_TYPE_P (ty1)
6574 && FLOAT_TYPE_P (ty2))
6575 (with { tree newtype = type;
6576 if (TYPE_MODE (ty1) == SDmode
6577 || TYPE_MODE (ty2) == SDmode
6578 || TYPE_MODE (type) == SDmode)
6579 newtype = dfloat32_type_node;
6580 if (TYPE_MODE (ty1) == DDmode
6581 || TYPE_MODE (ty2) == DDmode
6582 || TYPE_MODE (type) == DDmode)
6583 newtype = dfloat64_type_node;
6584 if (TYPE_MODE (ty1) == TDmode
6585 || TYPE_MODE (ty2) == TDmode
6586 || TYPE_MODE (type) == TDmode)
6587 newtype = dfloat128_type_node; }
6588 (if ((newtype == dfloat32_type_node
6589 || newtype == dfloat64_type_node
6590 || newtype == dfloat128_type_node)
6592 && types_match (newtype, type))
6593 (op (convert:newtype @1) (convert:newtype @2))
6594 (with { if (TYPE_PRECISION (ty1) > TYPE_PRECISION (newtype))
6596 if (TYPE_PRECISION (ty2) > TYPE_PRECISION (newtype))
6598 /* Sometimes this transformation is safe (cannot
6599 change results through affecting double rounding
6600 cases) and sometimes it is not. If NEWTYPE is
6601 wider than TYPE, e.g. (float)((long double)double
6602 + (long double)double) converted to
6603 (float)(double + double), the transformation is
6604 unsafe regardless of the details of the types
6605 involved; double rounding can arise if the result
6606 of NEWTYPE arithmetic is a NEWTYPE value half way
6607 between two representable TYPE values but the
6608 exact value is sufficiently different (in the
6609 right direction) for this difference to be
6610 visible in ITYPE arithmetic. If NEWTYPE is the
6611 same as TYPE, however, the transformation may be
6612 safe depending on the types involved: it is safe
6613 if the ITYPE has strictly more than twice as many
6614 mantissa bits as TYPE, can represent infinities
6615 and NaNs if the TYPE can, and has sufficient
6616 exponent range for the product or ratio of two
6617 values representable in the TYPE to be within the
6618 range of normal values of ITYPE. */
6619 (if (TYPE_PRECISION (newtype) < TYPE_PRECISION (itype)
6620 && (flag_unsafe_math_optimizations
6621 || (TYPE_PRECISION (newtype) == TYPE_PRECISION (type)
6622 && real_can_shorten_arithmetic (TYPE_MODE (itype),
6624 && !excess_precision_type (newtype)))
6625 && !types_match (itype, newtype))
6626 (convert:type (op (convert:newtype @1)
6627 (convert:newtype @2)))
6632 /* This is another case of narrowing, specifically when there's an outer
6633 BIT_AND_EXPR which masks off bits outside the type of the innermost
6634 operands. Like the previous case we have to convert the operands
6635 to unsigned types to avoid introducing undefined behavior for the
6636 arithmetic operation. */
6637 (for op (minus plus)
6639 (bit_and (op:s (convert@2 @0) (convert@3 @1)) INTEGER_CST@4)
6640 (if (INTEGRAL_TYPE_P (type)
6641 /* We check for type compatibility between @0 and @1 below,
6642 so there's no need to check that @1/@3 are integral types. */
6643 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
6644 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
6645 /* The precision of the type of each operand must match the
6646 precision of the mode of each operand, similarly for the
6648 && type_has_mode_precision_p (TREE_TYPE (@0))
6649 && type_has_mode_precision_p (TREE_TYPE (@1))
6650 && type_has_mode_precision_p (type)
6651 /* The inner conversion must be a widening conversion. */
6652 && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
6653 && types_match (@0, @1)
6654 && (tree_int_cst_min_precision (@4, TYPE_SIGN (TREE_TYPE (@0)))
6655 <= TYPE_PRECISION (TREE_TYPE (@0)))
6656 && (wi::to_wide (@4)
6657 & wi::mask (TYPE_PRECISION (TREE_TYPE (@0)),
6658 true, TYPE_PRECISION (type))) == 0)
6659 (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
6660 (with { tree ntype = TREE_TYPE (@0); }
6661 (convert (bit_and (op @0 @1) (convert:ntype @4))))
6662 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
6663 (convert (bit_and (op (convert:utype @0) (convert:utype @1))
6664 (convert:utype @4))))))))
6666 /* Transform (@0 < @1 and @0 < @2) to use min,
6667 (@0 > @1 and @0 > @2) to use max */
6668 (for logic (bit_and bit_and bit_and bit_and bit_ior bit_ior bit_ior bit_ior)
6669 op (lt le gt ge lt le gt ge )
6670 ext (min min max max max max min min )
6672 (logic (op:cs @0 @1) (op:cs @0 @2))
6673 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6674 && TREE_CODE (@0) != INTEGER_CST)
6675 (op @0 (ext @1 @2)))))
6678 /* signbit(x) -> 0 if x is nonnegative. */
6679 (SIGNBIT tree_expr_nonnegative_p@0)
6680 { integer_zero_node; })
6683 /* signbit(x) -> x<0 if x doesn't have signed zeros. */
6685 (if (!HONOR_SIGNED_ZEROS (@0))
6686 (convert (lt @0 { build_real (TREE_TYPE (@0), dconst0); }))))
6688 /* Transform comparisons of the form X +- C1 CMP C2 to X CMP C2 -+ C1. */
6690 (for op (plus minus)
6693 (cmp (op@3 @0 INTEGER_CST@1) INTEGER_CST@2)
6694 (if (!TREE_OVERFLOW (@1) && !TREE_OVERFLOW (@2)
6695 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0))
6696 && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@0))
6697 && !TYPE_SATURATING (TREE_TYPE (@0)))
6698 (with { tree res = int_const_binop (rop, @2, @1); }
6699 (if (TREE_OVERFLOW (res)
6700 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
6701 { constant_boolean_node (cmp == NE_EXPR, type); }
6702 (if (single_use (@3))
6703 (cmp @0 { TREE_OVERFLOW (res)
6704 ? drop_tree_overflow (res) : res; }))))))))
6705 (for cmp (lt le gt ge)
6706 (for op (plus minus)
6709 (cmp (op@3 @0 INTEGER_CST@1) INTEGER_CST@2)
6710 (if (!TREE_OVERFLOW (@1) && !TREE_OVERFLOW (@2)
6711 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
6712 (with { tree res = int_const_binop (rop, @2, @1); }
6713 (if (TREE_OVERFLOW (res))
6715 fold_overflow_warning (("assuming signed overflow does not occur "
6716 "when simplifying conditional to constant"),
6717 WARN_STRICT_OVERFLOW_CONDITIONAL);
6718 bool less = cmp == LE_EXPR || cmp == LT_EXPR;
6719 /* wi::ges_p (@2, 0) should be sufficient for a signed type. */
6720 bool ovf_high = wi::lt_p (wi::to_wide (@1), 0,
6721 TYPE_SIGN (TREE_TYPE (@1)))
6722 != (op == MINUS_EXPR);
6723 constant_boolean_node (less == ovf_high, type);
6725 (if (single_use (@3))
6728 fold_overflow_warning (("assuming signed overflow does not occur "
6729 "when changing X +- C1 cmp C2 to "
6731 WARN_STRICT_OVERFLOW_COMPARISON);
6733 (cmp @0 { res; })))))))))
6735 /* Canonicalizations of BIT_FIELD_REFs. */
6738 (BIT_FIELD_REF (BIT_FIELD_REF @0 @1 @2) @3 @4)
6739 (BIT_FIELD_REF @0 @3 { const_binop (PLUS_EXPR, bitsizetype, @2, @4); }))
6742 (BIT_FIELD_REF (view_convert @0) @1 @2)
6743 (BIT_FIELD_REF @0 @1 @2))
6746 (BIT_FIELD_REF @0 @1 integer_zerop)
6747 (if (tree_int_cst_equal (@1, TYPE_SIZE (TREE_TYPE (@0))))
6751 (BIT_FIELD_REF @0 @1 @2)
6753 (if (TREE_CODE (TREE_TYPE (@0)) == COMPLEX_TYPE
6754 && tree_int_cst_equal (@1, TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))))
6756 (if (integer_zerop (@2))
6757 (view_convert (realpart @0)))
6758 (if (tree_int_cst_equal (@2, TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))))
6759 (view_convert (imagpart @0)))))
6760 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6761 && INTEGRAL_TYPE_P (type)
6762 /* On GIMPLE this should only apply to register arguments. */
6763 && (! GIMPLE || is_gimple_reg (@0))
6764 /* A bit-field-ref that referenced the full argument can be stripped. */
6765 && ((compare_tree_int (@1, TYPE_PRECISION (TREE_TYPE (@0))) == 0
6766 && integer_zerop (@2))
6767 /* Low-parts can be reduced to integral conversions.
6768 ??? The following doesn't work for PDP endian. */
6769 || (BYTES_BIG_ENDIAN == WORDS_BIG_ENDIAN
6770 /* But only do this after vectorization. */
6771 && canonicalize_math_after_vectorization_p ()
6772 /* Don't even think about BITS_BIG_ENDIAN. */
6773 && TYPE_PRECISION (TREE_TYPE (@0)) % BITS_PER_UNIT == 0
6774 && TYPE_PRECISION (type) % BITS_PER_UNIT == 0
6775 && compare_tree_int (@2, (BYTES_BIG_ENDIAN
6776 ? (TYPE_PRECISION (TREE_TYPE (@0))
6777 - TYPE_PRECISION (type))
6781 /* Simplify vector extracts. */
6784 (BIT_FIELD_REF CONSTRUCTOR@0 @1 @2)
6785 (if (VECTOR_TYPE_P (TREE_TYPE (@0))
6786 && tree_fits_uhwi_p (TYPE_SIZE (type))
6787 && ((tree_to_uhwi (TYPE_SIZE (type))
6788 == tree_to_uhwi (TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))))
6789 || (VECTOR_TYPE_P (type)
6790 && (tree_to_uhwi (TYPE_SIZE (TREE_TYPE (type)))
6791 == tree_to_uhwi (TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0))))))))
6794 tree ctor = (TREE_CODE (@0) == SSA_NAME
6795 ? gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0)) : @0);
6796 tree eltype = TREE_TYPE (TREE_TYPE (ctor));
6797 unsigned HOST_WIDE_INT width = tree_to_uhwi (TYPE_SIZE (eltype));
6798 unsigned HOST_WIDE_INT n = tree_to_uhwi (@1);
6799 unsigned HOST_WIDE_INT idx = tree_to_uhwi (@2);
6802 && (idx % width) == 0
6804 && known_le ((idx + n) / width,
6805 TYPE_VECTOR_SUBPARTS (TREE_TYPE (ctor))))
6810 /* Constructor elements can be subvectors. */
6812 if (CONSTRUCTOR_NELTS (ctor) != 0)
6814 tree cons_elem = TREE_TYPE (CONSTRUCTOR_ELT (ctor, 0)->value);
6815 if (TREE_CODE (cons_elem) == VECTOR_TYPE)
6816 k = TYPE_VECTOR_SUBPARTS (cons_elem);
6818 unsigned HOST_WIDE_INT elt, count, const_k;
6821 /* We keep an exact subset of the constructor elements. */
6822 (if (multiple_p (idx, k, &elt) && multiple_p (n, k, &count))
6823 (if (CONSTRUCTOR_NELTS (ctor) == 0)
6824 { build_zero_cst (type); }
6826 (if (elt < CONSTRUCTOR_NELTS (ctor))
6827 (view_convert { CONSTRUCTOR_ELT (ctor, elt)->value; })
6828 { build_zero_cst (type); })
6829 /* We don't want to emit new CTORs unless the old one goes away.
6830 ??? Eventually allow this if the CTOR ends up constant or
6832 (if (single_use (@0))
6835 vec<constructor_elt, va_gc> *vals;
6836 vec_alloc (vals, count);
6837 bool constant_p = true;
6839 for (unsigned i = 0;
6840 i < count && elt + i < CONSTRUCTOR_NELTS (ctor); ++i)
6842 tree e = CONSTRUCTOR_ELT (ctor, elt + i)->value;
6843 CONSTRUCTOR_APPEND_ELT (vals, NULL_TREE, e);
6844 if (!CONSTANT_CLASS_P (e))
6847 tree evtype = (types_match (TREE_TYPE (type),
6848 TREE_TYPE (TREE_TYPE (ctor)))
6850 : build_vector_type (TREE_TYPE (TREE_TYPE (ctor)),
6852 res = (constant_p ? build_vector_from_ctor (evtype, vals)
6853 : build_constructor (evtype, vals));
6855 (view_convert { res; }))))))
6856 /* The bitfield references a single constructor element. */
6857 (if (k.is_constant (&const_k)
6858 && idx + n <= (idx / const_k + 1) * const_k)
6860 (if (CONSTRUCTOR_NELTS (ctor) <= idx / const_k)
6861 { build_zero_cst (type); })
6863 (view_convert { CONSTRUCTOR_ELT (ctor, idx / const_k)->value; }))
6864 (BIT_FIELD_REF { CONSTRUCTOR_ELT (ctor, idx / const_k)->value; }
6865 @1 { bitsize_int ((idx % const_k) * width); })))))))))
6867 /* Simplify a bit extraction from a bit insertion for the cases with
6868 the inserted element fully covering the extraction or the insertion
6869 not touching the extraction. */
6871 (BIT_FIELD_REF (bit_insert @0 @1 @ipos) @rsize @rpos)
6874 unsigned HOST_WIDE_INT isize;
6875 if (INTEGRAL_TYPE_P (TREE_TYPE (@1)))
6876 isize = TYPE_PRECISION (TREE_TYPE (@1));
6878 isize = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (@1)));
6881 (if (wi::leu_p (wi::to_wide (@ipos), wi::to_wide (@rpos))
6882 && wi::leu_p (wi::to_wide (@rpos) + wi::to_wide (@rsize),
6883 wi::to_wide (@ipos) + isize))
6884 (BIT_FIELD_REF @1 @rsize { wide_int_to_tree (bitsizetype,
6886 - wi::to_wide (@ipos)); }))
6887 (if (wi::geu_p (wi::to_wide (@ipos),
6888 wi::to_wide (@rpos) + wi::to_wide (@rsize))
6889 || wi::geu_p (wi::to_wide (@rpos),
6890 wi::to_wide (@ipos) + isize))
6891 (BIT_FIELD_REF @0 @rsize @rpos)))))
6893 (if (canonicalize_math_after_vectorization_p ())
6896 (fmas:c (negate @0) @1 @2)
6897 (IFN_FNMA @0 @1 @2))
6899 (fmas @0 @1 (negate @2))
6902 (fmas:c (negate @0) @1 (negate @2))
6903 (IFN_FNMS @0 @1 @2))
6905 (negate (fmas@3 @0 @1 @2))
6906 (if (single_use (@3))
6907 (IFN_FNMS @0 @1 @2))))
6910 (IFN_FMS:c (negate @0) @1 @2)
6911 (IFN_FNMS @0 @1 @2))
6913 (IFN_FMS @0 @1 (negate @2))
6916 (IFN_FMS:c (negate @0) @1 (negate @2))
6917 (IFN_FNMA @0 @1 @2))
6919 (negate (IFN_FMS@3 @0 @1 @2))
6920 (if (single_use (@3))
6921 (IFN_FNMA @0 @1 @2)))
6924 (IFN_FNMA:c (negate @0) @1 @2)
6927 (IFN_FNMA @0 @1 (negate @2))
6928 (IFN_FNMS @0 @1 @2))
6930 (IFN_FNMA:c (negate @0) @1 (negate @2))
6933 (negate (IFN_FNMA@3 @0 @1 @2))
6934 (if (single_use (@3))
6935 (IFN_FMS @0 @1 @2)))
6938 (IFN_FNMS:c (negate @0) @1 @2)
6941 (IFN_FNMS @0 @1 (negate @2))
6942 (IFN_FNMA @0 @1 @2))
6944 (IFN_FNMS:c (negate @0) @1 (negate @2))
6947 (negate (IFN_FNMS@3 @0 @1 @2))
6948 (if (single_use (@3))
6949 (IFN_FMA @0 @1 @2))))
6951 /* CLZ simplifications. */
6956 (op (clz:s@2 @0) INTEGER_CST@1)
6957 (if (integer_zerop (@1) && single_use (@2))
6958 /* clz(X) == 0 is (int)X < 0 and clz(X) != 0 is (int)X >= 0. */
6959 (with { tree type0 = TREE_TYPE (@0);
6960 tree stype = signed_type_for (type0);
6961 HOST_WIDE_INT val = 0;
6962 /* Punt on hypothetical weird targets. */
6964 && CLZ_DEFINED_VALUE_AT_ZERO (SCALAR_TYPE_MODE (type0),
6970 (cmp (convert:stype @0) { build_zero_cst (stype); })))
6971 /* clz(X) == (prec-1) is X == 1 and clz(X) != (prec-1) is X != 1. */
6972 (with { bool ok = true;
6973 HOST_WIDE_INT val = 0;
6974 tree type0 = TREE_TYPE (@0);
6975 /* Punt on hypothetical weird targets. */
6977 && CLZ_DEFINED_VALUE_AT_ZERO (SCALAR_TYPE_MODE (type0),
6979 && val == TYPE_PRECISION (type0) - 1)
6982 (if (ok && wi::to_wide (@1) == (TYPE_PRECISION (type0) - 1))
6983 (op @0 { build_one_cst (type0); })))))))
6985 /* CTZ simplifications. */
6987 (for op (ge gt le lt)
6990 /* __builtin_ctz (x) >= C -> (x & ((1 << C) - 1)) == 0. */
6991 (op (ctz:s @0) INTEGER_CST@1)
6992 (with { bool ok = true;
6993 HOST_WIDE_INT val = 0;
6994 if (!tree_fits_shwi_p (@1))
6998 val = tree_to_shwi (@1);
6999 /* Canonicalize to >= or <. */
7000 if (op == GT_EXPR || op == LE_EXPR)
7002 if (val == HOST_WIDE_INT_MAX)
7008 bool zero_res = false;
7009 HOST_WIDE_INT zero_val = 0;
7010 tree type0 = TREE_TYPE (@0);
7011 int prec = TYPE_PRECISION (type0);
7013 && CTZ_DEFINED_VALUE_AT_ZERO (SCALAR_TYPE_MODE (type0),
7018 (if (ok && (!zero_res || zero_val >= val))
7019 { constant_boolean_node (cmp == EQ_EXPR ? true : false, type); })
7021 (if (ok && (!zero_res || zero_val < val))
7022 { constant_boolean_node (cmp == EQ_EXPR ? false : true, type); })
7023 (if (ok && (!zero_res || zero_val < 0 || zero_val >= prec))
7024 (cmp (bit_and @0 { wide_int_to_tree (type0,
7025 wi::mask (val, false, prec)); })
7026 { build_zero_cst (type0); })))))))
7029 /* __builtin_ctz (x) == C -> (x & ((1 << (C + 1)) - 1)) == (1 << C). */
7030 (op (ctz:s @0) INTEGER_CST@1)
7031 (with { bool zero_res = false;
7032 HOST_WIDE_INT zero_val = 0;
7033 tree type0 = TREE_TYPE (@0);
7034 int prec = TYPE_PRECISION (type0);
7036 && CTZ_DEFINED_VALUE_AT_ZERO (SCALAR_TYPE_MODE (type0),
7040 (if (tree_int_cst_sgn (@1) < 0 || wi::to_widest (@1) >= prec)
7041 (if (!zero_res || zero_val != wi::to_widest (@1))
7042 { constant_boolean_node (op == EQ_EXPR ? false : true, type); })
7043 (if (!zero_res || zero_val < 0 || zero_val >= prec)
7044 (op (bit_and @0 { wide_int_to_tree (type0,
7045 wi::mask (tree_to_uhwi (@1) + 1,
7047 { wide_int_to_tree (type0,
7048 wi::shifted_mask (tree_to_uhwi (@1), 1,
7049 false, prec)); })))))))
7051 /* POPCOUNT simplifications. */
7052 /* popcount(X) + popcount(Y) is popcount(X|Y) when X&Y must be zero. */
7054 (plus (POPCOUNT:s @0) (POPCOUNT:s @1))
7055 (if (wi::bit_and (tree_nonzero_bits (@0), tree_nonzero_bits (@1)) == 0)
7056 (POPCOUNT (bit_ior @0 @1))))
7058 /* popcount(X) == 0 is X == 0, and related (in)equalities. */
7059 (for popcount (POPCOUNT)
7060 (for cmp (le eq ne gt)
7063 (cmp (popcount @0) integer_zerop)
7064 (rep @0 { build_zero_cst (TREE_TYPE (@0)); }))))
7066 /* Canonicalize POPCOUNT(x)&1 as PARITY(X). */
7068 (bit_and (POPCOUNT @0) integer_onep)
7071 /* PARITY simplifications. */
7072 /* parity(~X) is parity(X). */
7074 (PARITY (bit_not @0))
7077 /* parity(X)^parity(Y) is parity(X^Y). */
7079 (bit_xor (PARITY:s @0) (PARITY:s @1))
7080 (PARITY (bit_xor @0 @1)))
7082 /* Common POPCOUNT/PARITY simplifications. */
7083 /* popcount(X&C1) is (X>>C2)&1 when C1 == 1<<C2. Same for parity(X&C1). */
7084 (for pfun (POPCOUNT PARITY)
7087 (with { wide_int nz = tree_nonzero_bits (@0); }
7091 (if (wi::popcount (nz) == 1)
7092 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
7093 (convert (rshift:utype (convert:utype @0)
7094 { build_int_cst (integer_type_node,
7095 wi::ctz (nz)); }))))))))
7098 /* 64- and 32-bits branchless implementations of popcount are detected:
7100 int popcount64c (uint64_t x)
7102 x -= (x >> 1) & 0x5555555555555555ULL;
7103 x = (x & 0x3333333333333333ULL) + ((x >> 2) & 0x3333333333333333ULL);
7104 x = (x + (x >> 4)) & 0x0f0f0f0f0f0f0f0fULL;
7105 return (x * 0x0101010101010101ULL) >> 56;
7108 int popcount32c (uint32_t x)
7110 x -= (x >> 1) & 0x55555555;
7111 x = (x & 0x33333333) + ((x >> 2) & 0x33333333);
7112 x = (x + (x >> 4)) & 0x0f0f0f0f;
7113 return (x * 0x01010101) >> 24;
7120 (rshift @8 INTEGER_CST@5)
7122 (bit_and @6 INTEGER_CST@7)
7126 (bit_and (rshift @0 INTEGER_CST@4) INTEGER_CST@11))
7132 /* Check constants and optab. */
7133 (with { unsigned prec = TYPE_PRECISION (type);
7134 int shift = (64 - prec) & 63;
7135 unsigned HOST_WIDE_INT c1
7136 = HOST_WIDE_INT_UC (0x0101010101010101) >> shift;
7137 unsigned HOST_WIDE_INT c2
7138 = HOST_WIDE_INT_UC (0x0F0F0F0F0F0F0F0F) >> shift;
7139 unsigned HOST_WIDE_INT c3
7140 = HOST_WIDE_INT_UC (0x3333333333333333) >> shift;
7141 unsigned HOST_WIDE_INT c4
7142 = HOST_WIDE_INT_UC (0x5555555555555555) >> shift;
7147 && TYPE_UNSIGNED (type)
7148 && integer_onep (@4)
7149 && wi::to_widest (@10) == 2
7150 && wi::to_widest (@5) == 4
7151 && wi::to_widest (@1) == prec - 8
7152 && tree_to_uhwi (@2) == c1
7153 && tree_to_uhwi (@3) == c2
7154 && tree_to_uhwi (@9) == c3
7155 && tree_to_uhwi (@7) == c3
7156 && tree_to_uhwi (@11) == c4)
7157 (if (direct_internal_fn_supported_p (IFN_POPCOUNT, type,
7159 (convert (IFN_POPCOUNT:type @0))
7160 /* Try to do popcount in two halves. PREC must be at least
7161 five bits for this to work without extension before adding. */
7163 tree half_type = NULL_TREE;
7164 opt_machine_mode m = mode_for_size ((prec + 1) / 2, MODE_INT, 1);
7167 && m.require () != TYPE_MODE (type))
7169 half_prec = GET_MODE_PRECISION (as_a <scalar_int_mode> (m));
7170 half_type = build_nonstandard_integer_type (half_prec, 1);
7172 gcc_assert (half_prec > 2);
7174 (if (half_type != NULL_TREE
7175 && direct_internal_fn_supported_p (IFN_POPCOUNT, half_type,
7178 (IFN_POPCOUNT:half_type (convert @0))
7179 (IFN_POPCOUNT:half_type (convert (rshift @0
7180 { build_int_cst (integer_type_node, half_prec); } )))))))))))
7182 /* __builtin_ffs needs to deal on many targets with the possible zero
7183 argument. If we know the argument is always non-zero, __builtin_ctz + 1
7184 should lead to better code. */
7186 (FFS tree_expr_nonzero_p@0)
7187 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
7188 && direct_internal_fn_supported_p (IFN_CTZ, TREE_TYPE (@0),
7189 OPTIMIZE_FOR_SPEED))
7190 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
7191 (plus (CTZ:type (convert:utype @0)) { build_one_cst (type); }))))
7194 (for ffs (BUILT_IN_FFS BUILT_IN_FFSL BUILT_IN_FFSLL
7196 /* __builtin_ffs (X) == 0 -> X == 0.
7197 __builtin_ffs (X) == 6 -> (X & 63) == 32. */
7200 (cmp (ffs@2 @0) INTEGER_CST@1)
7201 (with { int prec = TYPE_PRECISION (TREE_TYPE (@0)); }
7203 (if (integer_zerop (@1))
7204 (cmp @0 { build_zero_cst (TREE_TYPE (@0)); }))
7205 (if (tree_int_cst_sgn (@1) < 0 || wi::to_widest (@1) > prec)
7206 { constant_boolean_node (cmp == NE_EXPR ? true : false, type); })
7207 (if (single_use (@2))
7208 (cmp (bit_and @0 { wide_int_to_tree (TREE_TYPE (@0),
7209 wi::mask (tree_to_uhwi (@1),
7211 { wide_int_to_tree (TREE_TYPE (@0),
7212 wi::shifted_mask (tree_to_uhwi (@1) - 1, 1,
7213 false, prec)); }))))))
7215 /* __builtin_ffs (X) > 6 -> X != 0 && (X & 63) == 0. */
7219 bit_op (bit_and bit_ior)
7221 (cmp (ffs@2 @0) INTEGER_CST@1)
7222 (with { int prec = TYPE_PRECISION (TREE_TYPE (@0)); }
7224 (if (integer_zerop (@1))
7225 (cmp2 @0 { build_zero_cst (TREE_TYPE (@0)); }))
7226 (if (tree_int_cst_sgn (@1) < 0)
7227 { constant_boolean_node (cmp == GT_EXPR ? true : false, type); })
7228 (if (wi::to_widest (@1) >= prec)
7229 { constant_boolean_node (cmp == GT_EXPR ? false : true, type); })
7230 (if (wi::to_widest (@1) == prec - 1)
7231 (cmp3 @0 { wide_int_to_tree (TREE_TYPE (@0),
7232 wi::shifted_mask (prec - 1, 1,
7234 (if (single_use (@2))
7235 (bit_op (cmp2 @0 { build_zero_cst (TREE_TYPE (@0)); })
7237 { wide_int_to_tree (TREE_TYPE (@0),
7238 wi::mask (tree_to_uhwi (@1),
7240 { build_zero_cst (TREE_TYPE (@0)); }))))))))
7247 --> r = .COND_FN (cond, a, b)
7251 --> r = .COND_FN (~cond, b, a). */
7253 (for uncond_op (UNCOND_UNARY)
7254 cond_op (COND_UNARY)
7256 (vec_cond @0 (view_convert? (uncond_op@3 @1)) @2)
7257 (with { tree op_type = TREE_TYPE (@3); }
7258 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
7259 && is_truth_type_for (op_type, TREE_TYPE (@0)))
7260 (cond_op @0 @1 @2))))
7262 (vec_cond @0 @1 (view_convert? (uncond_op@3 @2)))
7263 (with { tree op_type = TREE_TYPE (@3); }
7264 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
7265 && is_truth_type_for (op_type, TREE_TYPE (@0)))
7266 (cond_op (bit_not @0) @2 @1)))))
7275 r = c ? a1 op a2 : b;
7277 if the target can do it in one go. This makes the operation conditional
7278 on c, so could drop potentially-trapping arithmetic, but that's a valid
7279 simplification if the result of the operation isn't needed.
7281 Avoid speculatively generating a stand-alone vector comparison
7282 on targets that might not support them. Any target implementing
7283 conditional internal functions must support the same comparisons
7284 inside and outside a VEC_COND_EXPR. */
7286 (for uncond_op (UNCOND_BINARY)
7287 cond_op (COND_BINARY)
7289 (vec_cond @0 (view_convert? (uncond_op@4 @1 @2)) @3)
7290 (with { tree op_type = TREE_TYPE (@4); }
7291 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
7292 && is_truth_type_for (op_type, TREE_TYPE (@0)))
7293 (view_convert (cond_op @0 @1 @2 (view_convert:op_type @3))))))
7295 (vec_cond @0 @1 (view_convert? (uncond_op@4 @2 @3)))
7296 (with { tree op_type = TREE_TYPE (@4); }
7297 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
7298 && is_truth_type_for (op_type, TREE_TYPE (@0)))
7299 (view_convert (cond_op (bit_not @0) @2 @3 (view_convert:op_type @1)))))))
7301 /* Same for ternary operations. */
7302 (for uncond_op (UNCOND_TERNARY)
7303 cond_op (COND_TERNARY)
7305 (vec_cond @0 (view_convert? (uncond_op@5 @1 @2 @3)) @4)
7306 (with { tree op_type = TREE_TYPE (@5); }
7307 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
7308 && is_truth_type_for (op_type, TREE_TYPE (@0)))
7309 (view_convert (cond_op @0 @1 @2 @3 (view_convert:op_type @4))))))
7311 (vec_cond @0 @1 (view_convert? (uncond_op@5 @2 @3 @4)))
7312 (with { tree op_type = TREE_TYPE (@5); }
7313 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
7314 && is_truth_type_for (op_type, TREE_TYPE (@0)))
7315 (view_convert (cond_op (bit_not @0) @2 @3 @4
7316 (view_convert:op_type @1)))))))
7319 /* Detect cases in which a VEC_COND_EXPR effectively replaces the
7320 "else" value of an IFN_COND_*. */
7321 (for cond_op (COND_BINARY)
7323 (vec_cond @0 (view_convert? (cond_op @0 @1 @2 @3)) @4)
7324 (with { tree op_type = TREE_TYPE (@3); }
7325 (if (element_precision (type) == element_precision (op_type))
7326 (view_convert (cond_op @0 @1 @2 (view_convert:op_type @4))))))
7328 (vec_cond @0 @1 (view_convert? (cond_op @2 @3 @4 @5)))
7329 (with { tree op_type = TREE_TYPE (@5); }
7330 (if (inverse_conditions_p (@0, @2)
7331 && element_precision (type) == element_precision (op_type))
7332 (view_convert (cond_op @2 @3 @4 (view_convert:op_type @1)))))))
7334 /* Same for ternary operations. */
7335 (for cond_op (COND_TERNARY)
7337 (vec_cond @0 (view_convert? (cond_op @0 @1 @2 @3 @4)) @5)
7338 (with { tree op_type = TREE_TYPE (@4); }
7339 (if (element_precision (type) == element_precision (op_type))
7340 (view_convert (cond_op @0 @1 @2 @3 (view_convert:op_type @5))))))
7342 (vec_cond @0 @1 (view_convert? (cond_op @2 @3 @4 @5 @6)))
7343 (with { tree op_type = TREE_TYPE (@6); }
7344 (if (inverse_conditions_p (@0, @2)
7345 && element_precision (type) == element_precision (op_type))
7346 (view_convert (cond_op @2 @3 @4 @5 (view_convert:op_type @1)))))))
7348 /* Detect simplication for a conditional reduction where
7351 c = mask2 ? d + a : d
7355 c = mask1 && mask2 ? d + b : d. */
7357 (IFN_COND_ADD @0 @1 (vec_cond @2 @3 integer_zerop) @1)
7358 (IFN_COND_ADD (bit_and @0 @2) @1 @3 @1))
7360 /* For pointers @0 and @2 and nonnegative constant offset @1, look for
7363 A: (@0 + @1 < @2) | (@2 + @1 < @0)
7364 B: (@0 + @1 <= @2) | (@2 + @1 <= @0)
7366 If pointers are known not to wrap, B checks whether @1 bytes starting
7367 at @0 and @2 do not overlap, while A tests the same thing for @1 + 1
7368 bytes. A is more efficiently tested as:
7370 A: (sizetype) (@0 + @1 - @2) > @1 * 2
7372 The equivalent expression for B is given by replacing @1 with @1 - 1:
7374 B: (sizetype) (@0 + (@1 - 1) - @2) > (@1 - 1) * 2
7376 @0 and @2 can be swapped in both expressions without changing the result.
7378 The folds rely on sizetype's being unsigned (which is always true)
7379 and on its being the same width as the pointer (which we have to check).
7381 The fold replaces two pointer_plus expressions, two comparisons and
7382 an IOR with a pointer_plus, a pointer_diff, and a comparison, so in
7383 the best case it's a saving of two operations. The A fold retains one
7384 of the original pointer_pluses, so is a win even if both pointer_pluses
7385 are used elsewhere. The B fold is a wash if both pointer_pluses are
7386 used elsewhere, since all we end up doing is replacing a comparison with
7387 a pointer_plus. We do still apply the fold under those circumstances
7388 though, in case applying it to other conditions eventually makes one of the
7389 pointer_pluses dead. */
7390 (for ior (truth_orif truth_or bit_ior)
7393 (ior (cmp:cs (pointer_plus@3 @0 INTEGER_CST@1) @2)
7394 (cmp:cs (pointer_plus@4 @2 @1) @0))
7395 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
7396 && TYPE_OVERFLOW_WRAPS (sizetype)
7397 && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (sizetype))
7398 /* Calculate the rhs constant. */
7399 (with { offset_int off = wi::to_offset (@1) - (cmp == LE_EXPR ? 1 : 0);
7400 offset_int rhs = off * 2; }
7401 /* Always fails for negative values. */
7402 (if (wi::min_precision (rhs, UNSIGNED) <= TYPE_PRECISION (sizetype))
7403 /* Since the order of @0 and @2 doesn't matter, let tree_swap_operands_p
7404 pick a canonical order. This increases the chances of using the
7405 same pointer_plus in multiple checks. */
7406 (with { bool swap_p = tree_swap_operands_p (@0, @2);
7407 tree rhs_tree = wide_int_to_tree (sizetype, rhs); }
7408 (if (cmp == LT_EXPR)
7409 (gt (convert:sizetype
7410 (pointer_diff:ssizetype { swap_p ? @4 : @3; }
7411 { swap_p ? @0 : @2; }))
7413 (gt (convert:sizetype
7414 (pointer_diff:ssizetype
7415 (pointer_plus { swap_p ? @2 : @0; }
7416 { wide_int_to_tree (sizetype, off); })
7417 { swap_p ? @0 : @2; }))
7418 { rhs_tree; })))))))))
7420 /* Fold REDUC (@0 & @1) -> @0[I] & @1[I] if element I is the only nonzero
7422 (for reduc (IFN_REDUC_PLUS IFN_REDUC_IOR IFN_REDUC_XOR)
7423 (simplify (reduc (view_convert? (bit_and @0 VECTOR_CST@1)))
7424 (with { int i = single_nonzero_element (@1); }
7426 (with { tree elt = vector_cst_elt (@1, i);
7427 tree elt_type = TREE_TYPE (elt);
7428 unsigned int elt_bits = tree_to_uhwi (TYPE_SIZE (elt_type));
7429 tree size = bitsize_int (elt_bits);
7430 tree pos = bitsize_int (elt_bits * i); }
7433 (BIT_FIELD_REF:elt_type @0 { size; } { pos; })
7437 (vec_perm @0 @1 VECTOR_CST@2)
7440 tree op0 = @0, op1 = @1, op2 = @2;
7442 /* Build a vector of integers from the tree mask. */
7443 vec_perm_builder builder;
7444 if (!tree_to_vec_perm_builder (&builder, op2))
7447 /* Create a vec_perm_indices for the integer vector. */
7448 poly_uint64 nelts = TYPE_VECTOR_SUBPARTS (type);
7449 bool single_arg = (op0 == op1);
7450 vec_perm_indices sel (builder, single_arg ? 1 : 2, nelts);
7452 (if (sel.series_p (0, 1, 0, 1))
7454 (if (sel.series_p (0, 1, nelts, 1))
7460 if (sel.all_from_input_p (0))
7462 else if (sel.all_from_input_p (1))
7465 sel.rotate_inputs (1);
7467 else if (known_ge (poly_uint64 (sel[0]), nelts))
7469 std::swap (op0, op1);
7470 sel.rotate_inputs (1);
7474 tree cop0 = op0, cop1 = op1;
7475 if (TREE_CODE (op0) == SSA_NAME
7476 && (def = dyn_cast <gassign *> (SSA_NAME_DEF_STMT (op0)))
7477 && gimple_assign_rhs_code (def) == CONSTRUCTOR)
7478 cop0 = gimple_assign_rhs1 (def);
7479 if (TREE_CODE (op1) == SSA_NAME
7480 && (def = dyn_cast <gassign *> (SSA_NAME_DEF_STMT (op1)))
7481 && gimple_assign_rhs_code (def) == CONSTRUCTOR)
7482 cop1 = gimple_assign_rhs1 (def);
7486 (if ((TREE_CODE (cop0) == VECTOR_CST
7487 || TREE_CODE (cop0) == CONSTRUCTOR)
7488 && (TREE_CODE (cop1) == VECTOR_CST
7489 || TREE_CODE (cop1) == CONSTRUCTOR)
7490 && (t = fold_vec_perm (type, cop0, cop1, sel)))
7494 bool changed = (op0 == op1 && !single_arg);
7495 tree ins = NULL_TREE;
7498 /* See if the permutation is performing a single element
7499 insert from a CONSTRUCTOR or constant and use a BIT_INSERT_EXPR
7500 in that case. But only if the vector mode is supported,
7501 otherwise this is invalid GIMPLE. */
7502 if (TYPE_MODE (type) != BLKmode
7503 && (TREE_CODE (cop0) == VECTOR_CST
7504 || TREE_CODE (cop0) == CONSTRUCTOR
7505 || TREE_CODE (cop1) == VECTOR_CST
7506 || TREE_CODE (cop1) == CONSTRUCTOR))
7508 bool insert_first_p = sel.series_p (1, 1, nelts + 1, 1);
7511 /* After canonicalizing the first elt to come from the
7512 first vector we only can insert the first elt from
7513 the first vector. */
7515 if ((ins = fold_read_from_vector (cop0, sel[0])))
7518 /* The above can fail for two-element vectors which always
7519 appear to insert the first element, so try inserting
7520 into the second lane as well. For more than two
7521 elements that's wasted time. */
7522 if (!insert_first_p || (!ins && maybe_eq (nelts, 2u)))
7524 unsigned int encoded_nelts = sel.encoding ().encoded_nelts ();
7525 for (at = 0; at < encoded_nelts; ++at)
7526 if (maybe_ne (sel[at], at))
7528 if (at < encoded_nelts
7529 && (known_eq (at + 1, nelts)
7530 || sel.series_p (at + 1, 1, at + 1, 1)))
7532 if (known_lt (poly_uint64 (sel[at]), nelts))
7533 ins = fold_read_from_vector (cop0, sel[at]);
7535 ins = fold_read_from_vector (cop1, sel[at] - nelts);
7540 /* Generate a canonical form of the selector. */
7541 if (!ins && sel.encoding () != builder)
7543 /* Some targets are deficient and fail to expand a single
7544 argument permutation while still allowing an equivalent
7545 2-argument version. */
7547 if (sel.ninputs () == 2
7548 || can_vec_perm_const_p (TYPE_MODE (type), sel, false))
7549 op2 = vec_perm_indices_to_tree (TREE_TYPE (op2), sel);
7552 vec_perm_indices sel2 (builder, 2, nelts);
7553 if (can_vec_perm_const_p (TYPE_MODE (type), sel2, false))
7554 op2 = vec_perm_indices_to_tree (TREE_TYPE (op2), sel2);
7556 /* Not directly supported with either encoding,
7557 so use the preferred form. */
7558 op2 = vec_perm_indices_to_tree (TREE_TYPE (op2), sel);
7560 if (!operand_equal_p (op2, oldop2, 0))
7565 (bit_insert { op0; } { ins; }
7566 { bitsize_int (at * vector_element_bits (type)); })
7568 (vec_perm { op0; } { op1; } { op2; }))))))))))
7570 /* VEC_PERM_EXPR (v, v, mask) -> v where v contains same element. */
7572 (match vec_same_elem_p
7574 (if (uniform_vector_p (@0))))
7576 (match vec_same_elem_p
7580 (vec_perm vec_same_elem_p@0 @0 @1)
7583 /* Match count trailing zeroes for simplify_count_trailing_zeroes in fwprop.
7584 The canonical form is array[((x & -x) * C) >> SHIFT] where C is a magic
7585 constant which when multiplied by a power of 2 contains a unique value
7586 in the top 5 or 6 bits. This is then indexed into a table which maps it
7587 to the number of trailing zeroes. */
7588 (match (ctz_table_index @1 @2 @3)
7589 (rshift (mult (bit_and:c (negate @1) @1) INTEGER_CST@2) INTEGER_CST@3))