1 /* Match-and-simplify patterns for shared GENERIC and GIMPLE folding.
2 This file is consumed by genmatch which produces gimple-match.cc
3 and generic-match.cc from it.
5 Copyright (C) 2014-2024 Free Software Foundation, Inc.
6 Contributed by Richard Biener <rguenther@suse.de>
7 and Prathamesh Kulkarni <bilbotheelffriend@gmail.com>
9 This file is part of GCC.
11 GCC is free software; you can redistribute it and/or modify it under
12 the terms of the GNU General Public License as published by the Free
13 Software Foundation; either version 3, or (at your option) any later
16 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
17 WARRANTY; without even the implied warranty of MERCHANTABILITY or
18 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21 You should have received a copy of the GNU General Public License
22 along with GCC; see the file COPYING3. If not see
23 <http://www.gnu.org/licenses/>. */
26 /* Generic tree predicates we inherit. */
28 integer_onep integer_zerop integer_all_onesp integer_minus_onep
29 integer_each_onep integer_truep integer_nonzerop
30 real_zerop real_onep real_minus_onep
32 initializer_each_zero_or_onep
34 tree_expr_nonnegative_p
42 bitmask_inv_cst_vector_p)
45 (define_operator_list tcc_comparison
46 lt le eq ne ge gt unordered ordered unlt unle ungt unge uneq ltgt)
47 (define_operator_list inverted_tcc_comparison
48 ge gt ne eq lt le ordered unordered ge gt le lt ltgt uneq)
49 (define_operator_list inverted_tcc_comparison_with_nans
50 unge ungt ne eq unlt unle ordered unordered ge gt le lt ltgt uneq)
51 (define_operator_list swapped_tcc_comparison
52 gt ge eq ne le lt unordered ordered ungt unge unlt unle uneq ltgt)
53 (define_operator_list simple_comparison lt le eq ne ge gt)
54 (define_operator_list swapped_simple_comparison gt ge eq ne le lt)
55 (define_operator_list BSWAP BUILT_IN_BSWAP16 BUILT_IN_BSWAP32
56 BUILT_IN_BSWAP64 BUILT_IN_BSWAP128)
58 #include "cfn-operators.pd"
60 /* Define operand lists for math rounding functions {,i,l,ll}FN,
61 where the versions prefixed with "i" return an int, those prefixed with
62 "l" return a long and those prefixed with "ll" return a long long.
64 Also define operand lists:
66 X<FN>F for all float functions, in the order i, l, ll
67 X<FN> for all double functions, in the same order
68 X<FN>L for all long double functions, in the same order. */
69 #define DEFINE_INT_AND_FLOAT_ROUND_FN(FN) \
70 (define_operator_list X##FN##F BUILT_IN_I##FN##F \
73 (define_operator_list X##FN BUILT_IN_I##FN \
76 (define_operator_list X##FN##L BUILT_IN_I##FN##L \
80 DEFINE_INT_AND_FLOAT_ROUND_FN (FLOOR)
81 DEFINE_INT_AND_FLOAT_ROUND_FN (CEIL)
82 DEFINE_INT_AND_FLOAT_ROUND_FN (ROUND)
83 DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
85 /* Unary operations and their associated IFN_COND_* function. */
86 (define_operator_list UNCOND_UNARY
88 (define_operator_list COND_UNARY
89 IFN_COND_NEG IFN_COND_NOT)
90 (define_operator_list COND_LEN_UNARY
91 IFN_COND_LEN_NEG IFN_COND_LEN_NOT)
93 /* Binary operations and their associated IFN_COND_* function. */
94 (define_operator_list UNCOND_BINARY
96 mult trunc_div trunc_mod rdiv
98 IFN_FMIN IFN_FMAX IFN_COPYSIGN
99 bit_and bit_ior bit_xor
101 (define_operator_list COND_BINARY
102 IFN_COND_ADD IFN_COND_SUB
103 IFN_COND_MUL IFN_COND_DIV IFN_COND_MOD IFN_COND_RDIV
104 IFN_COND_MIN IFN_COND_MAX
105 IFN_COND_FMIN IFN_COND_FMAX IFN_COND_COPYSIGN
106 IFN_COND_AND IFN_COND_IOR IFN_COND_XOR
107 IFN_COND_SHL IFN_COND_SHR)
108 (define_operator_list COND_LEN_BINARY
109 IFN_COND_LEN_ADD IFN_COND_LEN_SUB
110 IFN_COND_LEN_MUL IFN_COND_LEN_DIV
111 IFN_COND_LEN_MOD IFN_COND_LEN_RDIV
112 IFN_COND_LEN_MIN IFN_COND_LEN_MAX
113 IFN_COND_LEN_FMIN IFN_COND_LEN_FMAX IFN_COND_LEN_COPYSIGN
114 IFN_COND_LEN_AND IFN_COND_LEN_IOR IFN_COND_LEN_XOR
115 IFN_COND_LEN_SHL IFN_COND_LEN_SHR)
117 /* Same for ternary operations. */
118 (define_operator_list UNCOND_TERNARY
119 IFN_FMA IFN_FMS IFN_FNMA IFN_FNMS)
120 (define_operator_list COND_TERNARY
121 IFN_COND_FMA IFN_COND_FMS IFN_COND_FNMA IFN_COND_FNMS)
122 (define_operator_list COND_LEN_TERNARY
123 IFN_COND_LEN_FMA IFN_COND_LEN_FMS IFN_COND_LEN_FNMA IFN_COND_LEN_FNMS)
125 /* __atomic_fetch_or_*, __atomic_fetch_xor_*, __atomic_xor_fetch_* */
126 (define_operator_list ATOMIC_FETCH_OR_XOR_N
127 BUILT_IN_ATOMIC_FETCH_OR_1 BUILT_IN_ATOMIC_FETCH_OR_2
128 BUILT_IN_ATOMIC_FETCH_OR_4 BUILT_IN_ATOMIC_FETCH_OR_8
129 BUILT_IN_ATOMIC_FETCH_OR_16
130 BUILT_IN_ATOMIC_FETCH_XOR_1 BUILT_IN_ATOMIC_FETCH_XOR_2
131 BUILT_IN_ATOMIC_FETCH_XOR_4 BUILT_IN_ATOMIC_FETCH_XOR_8
132 BUILT_IN_ATOMIC_FETCH_XOR_16
133 BUILT_IN_ATOMIC_XOR_FETCH_1 BUILT_IN_ATOMIC_XOR_FETCH_2
134 BUILT_IN_ATOMIC_XOR_FETCH_4 BUILT_IN_ATOMIC_XOR_FETCH_8
135 BUILT_IN_ATOMIC_XOR_FETCH_16)
136 /* __sync_fetch_and_or_*, __sync_fetch_and_xor_*, __sync_xor_and_fetch_* */
137 (define_operator_list SYNC_FETCH_OR_XOR_N
138 BUILT_IN_SYNC_FETCH_AND_OR_1 BUILT_IN_SYNC_FETCH_AND_OR_2
139 BUILT_IN_SYNC_FETCH_AND_OR_4 BUILT_IN_SYNC_FETCH_AND_OR_8
140 BUILT_IN_SYNC_FETCH_AND_OR_16
141 BUILT_IN_SYNC_FETCH_AND_XOR_1 BUILT_IN_SYNC_FETCH_AND_XOR_2
142 BUILT_IN_SYNC_FETCH_AND_XOR_4 BUILT_IN_SYNC_FETCH_AND_XOR_8
143 BUILT_IN_SYNC_FETCH_AND_XOR_16
144 BUILT_IN_SYNC_XOR_AND_FETCH_1 BUILT_IN_SYNC_XOR_AND_FETCH_2
145 BUILT_IN_SYNC_XOR_AND_FETCH_4 BUILT_IN_SYNC_XOR_AND_FETCH_8
146 BUILT_IN_SYNC_XOR_AND_FETCH_16)
147 /* __atomic_fetch_and_*. */
148 (define_operator_list ATOMIC_FETCH_AND_N
149 BUILT_IN_ATOMIC_FETCH_AND_1 BUILT_IN_ATOMIC_FETCH_AND_2
150 BUILT_IN_ATOMIC_FETCH_AND_4 BUILT_IN_ATOMIC_FETCH_AND_8
151 BUILT_IN_ATOMIC_FETCH_AND_16)
152 /* __sync_fetch_and_and_*. */
153 (define_operator_list SYNC_FETCH_AND_AND_N
154 BUILT_IN_SYNC_FETCH_AND_AND_1 BUILT_IN_SYNC_FETCH_AND_AND_2
155 BUILT_IN_SYNC_FETCH_AND_AND_4 BUILT_IN_SYNC_FETCH_AND_AND_8
156 BUILT_IN_SYNC_FETCH_AND_AND_16)
158 /* With nop_convert? combine convert? and view_convert? in one pattern
159 plus conditionalize on tree_nop_conversion_p conversions. */
160 (match (nop_convert @0)
162 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))))
163 (match (nop_convert @0)
165 (if (VECTOR_TYPE_P (type) && VECTOR_TYPE_P (TREE_TYPE (@0))
166 && known_eq (TYPE_VECTOR_SUBPARTS (type),
167 TYPE_VECTOR_SUBPARTS (TREE_TYPE (@0)))
168 && tree_nop_conversion_p (TREE_TYPE (type), TREE_TYPE (TREE_TYPE (@0))))))
171 /* These are used by gimple_bitwise_inverted_equal_p to simplify
172 detection of BIT_NOT and comparisons. */
173 (match (bit_not_with_nop @0)
175 (match (bit_not_with_nop @0)
176 (convert (bit_not @0))
177 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))))
178 (for cmp (tcc_comparison)
179 (match (maybe_cmp @0)
181 (match (maybe_cmp @0)
182 (convert (cmp@0 @1 @2))
183 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))))
185 /* `a ^ b` is another form of `a != b` when the type
186 is a 1bit precission integer. */
187 (match (maybe_cmp @0)
189 (if (INTEGRAL_TYPE_P (type)
190 && TYPE_PRECISION (type) == 1)))
193 /* Transform likes of (char) ABS_EXPR <(int) x> into (char) ABSU_EXPR <x>
194 ABSU_EXPR returns unsigned absolute value of the operand and the operand
195 of the ABSU_EXPR will have the corresponding signed type. */
196 (simplify (abs (convert @0))
197 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
198 && !TYPE_UNSIGNED (TREE_TYPE (@0))
199 && element_precision (type) > element_precision (TREE_TYPE (@0)))
200 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
201 (convert (absu:utype @0)))))
204 /* Optimize (X + (X >> (prec - 1))) ^ (X >> (prec - 1)) into abs (X). */
206 (bit_xor:c (plus:c @0 (rshift@2 @0 INTEGER_CST@1)) @2)
207 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
208 && !TYPE_UNSIGNED (TREE_TYPE (@0))
209 && wi::to_widest (@1) == element_precision (TREE_TYPE (@0)) - 1)
213 /* Simplifications of operations with one constant operand and
214 simplifications to constants or single values. */
216 (for op (plus pointer_plus minus bit_ior bit_xor)
218 (op @0 integer_zerop)
221 /* 0 +p index -> (type)index */
223 (pointer_plus integer_zerop @1)
224 (non_lvalue (convert @1)))
226 /* ptr - 0 -> (type)ptr */
228 (pointer_diff @0 integer_zerop)
231 /* See if ARG1 is zero and X + ARG1 reduces to X.
232 Likewise if the operands are reversed. */
234 (plus:c @0 real_zerop@1)
235 (if (fold_real_zero_addition_p (type, @0, @1, 0))
238 /* See if ARG1 is zero and X - ARG1 reduces to X. */
240 (minus @0 real_zerop@1)
241 (if (fold_real_zero_addition_p (type, @0, @1, 1))
244 /* Even if the fold_real_zero_addition_p can't simplify X + 0.0
245 into X, we can optimize (X + 0.0) + 0.0 or (X + 0.0) - 0.0
246 or (X - 0.0) + 0.0 into X + 0.0 and (X - 0.0) - 0.0 into X - 0.0
247 if not -frounding-math. For sNaNs the first operation would raise
248 exceptions but turn the result into qNan, so the second operation
249 would not raise it. */
250 (for inner_op (plus minus)
251 (for outer_op (plus minus)
253 (outer_op (inner_op@3 @0 REAL_CST@1) REAL_CST@2)
256 && !HONOR_SIGN_DEPENDENT_ROUNDING (type))
257 (with { bool inner_plus = ((inner_op == PLUS_EXPR)
258 ^ REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@1)));
260 = ((outer_op == PLUS_EXPR)
261 ^ REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@2))); }
262 (if (outer_plus && !inner_plus)
267 This is unsafe for certain floats even in non-IEEE formats.
268 In IEEE, it is unsafe because it does wrong for NaNs.
269 PR middle-end/98420: x - x may be -0.0 with FE_DOWNWARD.
270 Also note that operand_equal_p is always false if an operand
274 (if (!FLOAT_TYPE_P (type)
275 || (!tree_expr_maybe_nan_p (@0)
276 && !tree_expr_maybe_infinite_p (@0)
277 && (!HONOR_SIGN_DEPENDENT_ROUNDING (type)
278 || !HONOR_SIGNED_ZEROS (type))))
279 { build_zero_cst (type); }))
281 (pointer_diff @@0 @0)
282 { build_zero_cst (type); })
285 (mult @0 integer_zerop@1)
288 /* -x == x -> x == 0 */
291 (cmp:c @0 (negate @0))
292 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
293 && !TYPE_OVERFLOW_WRAPS (TREE_TYPE(@0)))
294 (cmp @0 { build_zero_cst (TREE_TYPE(@0)); }))))
296 /* Maybe fold x * 0 to 0. The expressions aren't the same
297 when x is NaN, since x * 0 is also NaN. Nor are they the
298 same in modes with signed zeros, since multiplying a
299 negative value by 0 gives -0, not +0. Nor when x is +-Inf,
300 since x * 0 is NaN. */
302 (mult @0 real_zerop@1)
303 (if (!tree_expr_maybe_nan_p (@0)
304 && (!HONOR_NANS (type) || !tree_expr_maybe_infinite_p (@0))
305 && (!HONOR_SIGNED_ZEROS (type) || tree_expr_nonnegative_p (@0)))
308 /* In IEEE floating point, x*1 is not equivalent to x for snans.
309 Likewise for complex arithmetic with signed zeros. */
312 (if (!tree_expr_maybe_signaling_nan_p (@0)
313 && (!HONOR_SIGNED_ZEROS (type)
314 || !COMPLEX_FLOAT_TYPE_P (type)))
317 /* Transform x * -1.0 into -x. */
319 (mult @0 real_minus_onep)
320 (if (!tree_expr_maybe_signaling_nan_p (@0)
321 && (!HONOR_SIGNED_ZEROS (type)
322 || !COMPLEX_FLOAT_TYPE_P (type)))
325 /* Transform x * { 0 or 1, 0 or 1, ... } into x & { 0 or -1, 0 or -1, ...},
326 unless the target has native support for the former but not the latter. */
328 (mult @0 VECTOR_CST@1)
329 (if (initializer_each_zero_or_onep (@1)
330 && !HONOR_SNANS (type)
331 && !HONOR_SIGNED_ZEROS (type))
332 (with { tree itype = FLOAT_TYPE_P (type) ? unsigned_type_for (type) : type; }
334 && (!VECTOR_MODE_P (TYPE_MODE (type))
335 || (VECTOR_MODE_P (TYPE_MODE (itype))
336 && optab_handler (and_optab,
337 TYPE_MODE (itype)) != CODE_FOR_nothing)))
338 (view_convert (bit_and:itype (view_convert @0)
339 (ne @1 { build_zero_cst (type); })))))))
341 /* In SWAR (SIMD within a register) code a signed comparison of packed data
342 can be constructed with a particular combination of shift, bitwise and,
343 and multiplication by constants. If that code is vectorized we can
344 convert this pattern into a more efficient vector comparison. */
346 (mult (bit_and (rshift @0 uniform_integer_cst_p@1)
347 uniform_integer_cst_p@2)
348 uniform_integer_cst_p@3)
350 tree rshift_cst = uniform_integer_cst_p (@1);
351 tree bit_and_cst = uniform_integer_cst_p (@2);
352 tree mult_cst = uniform_integer_cst_p (@3);
354 /* Make sure we're working with vectors and uniform vector constants. */
355 (if (VECTOR_TYPE_P (type)
356 && tree_fits_uhwi_p (rshift_cst)
357 && tree_fits_uhwi_p (mult_cst)
358 && tree_fits_uhwi_p (bit_and_cst))
359 /* Compute what constants would be needed for this to represent a packed
360 comparison based on the shift amount denoted by RSHIFT_CST. */
362 HOST_WIDE_INT vec_elem_bits = vector_element_bits (type);
363 poly_int64 vec_nelts = TYPE_VECTOR_SUBPARTS (type);
364 poly_int64 vec_bits = vec_elem_bits * vec_nelts;
365 unsigned HOST_WIDE_INT cmp_bits_i, bit_and_i, mult_i;
366 unsigned HOST_WIDE_INT target_mult_i, target_bit_and_i;
367 cmp_bits_i = tree_to_uhwi (rshift_cst) + 1;
368 mult_i = tree_to_uhwi (mult_cst);
369 target_mult_i = (HOST_WIDE_INT_1U << cmp_bits_i) - 1;
370 bit_and_i = tree_to_uhwi (bit_and_cst);
371 target_bit_and_i = 0;
373 /* The bit pattern in BIT_AND_I should be a mask for the least
374 significant bit of each packed element that is CMP_BITS wide. */
375 for (unsigned i = 0; i < vec_elem_bits / cmp_bits_i; i++)
376 target_bit_and_i = (target_bit_and_i << cmp_bits_i) | 1U;
378 (if ((exact_log2 (cmp_bits_i)) >= 0
379 && cmp_bits_i < HOST_BITS_PER_WIDE_INT
380 && multiple_p (vec_bits, cmp_bits_i)
381 && vec_elem_bits <= HOST_BITS_PER_WIDE_INT
382 && target_mult_i == mult_i
383 && target_bit_and_i == bit_and_i)
384 /* Compute the vector shape for the comparison and check if the target is
385 able to expand the comparison with that type. */
387 /* We're doing a signed comparison. */
388 tree cmp_type = build_nonstandard_integer_type (cmp_bits_i, 0);
389 poly_int64 vector_type_nelts = exact_div (vec_bits, cmp_bits_i);
390 tree vec_cmp_type = build_vector_type (cmp_type, vector_type_nelts);
391 tree vec_truth_type = truth_type_for (vec_cmp_type);
392 tree zeros = build_zero_cst (vec_cmp_type);
393 tree ones = build_all_ones_cst (vec_cmp_type);
395 (if (expand_vec_cmp_expr_p (vec_cmp_type, vec_truth_type, LT_EXPR)
396 && expand_vec_cond_expr_p (vec_cmp_type, vec_truth_type, LT_EXPR))
397 (view_convert:type (vec_cond (lt:vec_truth_type
398 (view_convert:vec_cmp_type @0)
400 { ones; } { zeros; })))))))))
402 (for cmp (gt ge lt le)
403 outp (convert convert negate negate)
404 outn (negate negate convert convert)
405 /* Transform X * (X > 0.0 ? 1.0 : -1.0) into abs(X). */
406 /* Transform X * (X >= 0.0 ? 1.0 : -1.0) into abs(X). */
407 /* Transform X * (X < 0.0 ? 1.0 : -1.0) into -abs(X). */
408 /* Transform X * (X <= 0.0 ? 1.0 : -1.0) into -abs(X). */
410 (mult:c @0 (cond (cmp @0 real_zerop) real_onep@1 real_minus_onep))
411 (if (!tree_expr_maybe_nan_p (@0) && !HONOR_SIGNED_ZEROS (type))
413 /* Transform X * (X > 0.0 ? -1.0 : 1.0) into -abs(X). */
414 /* Transform X * (X >= 0.0 ? -1.0 : 1.0) into -abs(X). */
415 /* Transform X * (X < 0.0 ? -1.0 : 1.0) into abs(X). */
416 /* Transform X * (X <= 0.0 ? -1.0 : 1.0) into abs(X). */
418 (mult:c @0 (cond (cmp @0 real_zerop) real_minus_onep real_onep@1))
419 (if (!tree_expr_maybe_nan_p (@0) && !HONOR_SIGNED_ZEROS (type))
422 /* Transform X * copysign (1.0, X) into abs(X). */
424 (mult:c @0 (COPYSIGN_ALL real_onep @0))
425 (if (!tree_expr_maybe_nan_p (@0) && !HONOR_SIGNED_ZEROS (type))
428 /* Transform X * copysign (1.0, -X) into -abs(X). */
430 (mult:c @0 (COPYSIGN_ALL real_onep (negate @0)))
431 (if (!tree_expr_maybe_nan_p (@0) && !HONOR_SIGNED_ZEROS (type))
434 /* Transform copysign (CST, X) into copysign (ABS(CST), X). */
436 (COPYSIGN_ALL REAL_CST@0 @1)
437 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@0)))
438 (COPYSIGN_ALL (negate @0) @1)))
440 /* Transform c ? x * copysign (1, y) : z to c ? x ^ signs(y) : z.
441 tree-ssa-math-opts.cc does the corresponding optimization for
442 unconditional multiplications (via xorsign). */
444 (IFN_COND_MUL:c @0 @1 (IFN_COPYSIGN real_onep @2) @3)
445 (with { tree signs = sign_mask_for (type); }
447 (with { tree inttype = TREE_TYPE (signs); }
449 (IFN_COND_XOR:inttype @0
450 (view_convert:inttype @1)
451 (bit_and (view_convert:inttype @2) { signs; })
452 (view_convert:inttype @3)))))))
454 /* (x >= 0 ? x : 0) + (x <= 0 ? -x : 0) -> abs x. */
456 (plus:c (max @0 integer_zerop) (max (negate @0) integer_zerop))
459 /* X * 1, X / 1 -> X. */
460 (for op (mult trunc_div ceil_div floor_div round_div exact_div)
465 /* (A / (1 << B)) -> (A >> B).
466 Only for unsigned A. For signed A, this would not preserve rounding
468 For example: (-1 / ( 1 << B)) != -1 >> B.
469 Also handle widening conversions, like:
470 (A / (unsigned long long) (1U << B)) -> (A >> B)
472 (A / (unsigned long long) (1 << B)) -> (A >> B).
473 If the left shift is signed, it can be done only if the upper bits
474 of A starting from shift's type sign bit are zero, as
475 (unsigned long long) (1 << 31) is -2147483648ULL, not 2147483648ULL,
476 so it is valid only if A >> 31 is zero. */
478 (trunc_div (convert?@0 @3) (convert2? (lshift integer_onep@1 @2)))
479 (if ((TYPE_UNSIGNED (type) || tree_expr_nonnegative_p (@0))
480 && (!VECTOR_TYPE_P (type)
481 || target_supports_op_p (type, RSHIFT_EXPR, optab_vector)
482 || target_supports_op_p (type, RSHIFT_EXPR, optab_scalar))
483 && (useless_type_conversion_p (type, TREE_TYPE (@1))
484 || (element_precision (type) >= element_precision (TREE_TYPE (@1))
485 && (TYPE_UNSIGNED (TREE_TYPE (@1))
486 || (element_precision (type)
487 == element_precision (TREE_TYPE (@1)))
488 || (INTEGRAL_TYPE_P (type)
489 && (tree_nonzero_bits (@0)
490 & wi::mask (element_precision (TREE_TYPE (@1)) - 1,
492 element_precision (type))) == 0)))))
493 (if (!VECTOR_TYPE_P (type)
494 && useless_type_conversion_p (TREE_TYPE (@3), TREE_TYPE (@1))
495 && element_precision (TREE_TYPE (@3)) < element_precision (type))
496 (convert (rshift @3 @2))
499 /* Preserve explicit divisions by 0: the C++ front-end wants to detect
500 undefined behavior in constexpr evaluation, and assuming that the division
501 traps enables better optimizations than these anyway. */
502 (for div (trunc_div ceil_div floor_div round_div exact_div)
503 /* 0 / X is always zero. */
505 (div integer_zerop@0 @1)
506 /* But not for 0 / 0 so that we can get the proper warnings and errors. */
507 (if (!integer_zerop (@1))
511 (div @0 integer_minus_onep@1)
512 (if (!TYPE_UNSIGNED (type))
514 /* X / bool_range_Y is X. */
517 (if (INTEGRAL_TYPE_P (type)
518 && ssa_name_has_boolean_range (@1)
519 && !flag_non_call_exceptions)
524 /* But not for 0 / 0 so that we can get the proper warnings and errors.
525 And not for _Fract types where we can't build 1. */
526 (if (!ALL_FRACT_MODE_P (TYPE_MODE (type))
527 && !integer_zerop (@0)
528 && (!flag_non_call_exceptions || tree_expr_nonzero_p (@0)))
529 { build_one_cst (type); }))
530 /* X / abs (X) is X < 0 ? -1 : 1. */
533 (if (INTEGRAL_TYPE_P (type)
534 && TYPE_OVERFLOW_UNDEFINED (type)
535 && !integer_zerop (@0)
536 && (!flag_non_call_exceptions || tree_expr_nonzero_p (@0)))
537 (cond (lt @0 { build_zero_cst (type); })
538 { build_minus_one_cst (type); } { build_one_cst (type); })))
541 (div:C @0 (negate @0))
542 (if ((INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
543 && TYPE_OVERFLOW_UNDEFINED (type)
544 && !integer_zerop (@0)
545 && (!flag_non_call_exceptions || tree_expr_nonzero_p (@0)))
546 { build_minus_one_cst (type); })))
548 /* For unsigned integral types, FLOOR_DIV_EXPR is the same as
549 TRUNC_DIV_EXPR. Rewrite into the latter in this case. Similarly
550 for MOD instead of DIV. */
551 (for floor_divmod (floor_div floor_mod)
552 trunc_divmod (trunc_div trunc_mod)
555 (if ((INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
556 && TYPE_UNSIGNED (type))
557 (trunc_divmod @0 @1))))
559 /* 1 / X -> X == 1 for unsigned integer X.
560 1 / X -> X >= -1 && X <= 1 ? X : 0 for signed integer X.
561 But not for 1 / 0 so that we can get proper warnings and errors,
562 and not for 1-bit integers as they are edge cases better handled
565 (trunc_div integer_onep@0 @1)
566 (if (INTEGRAL_TYPE_P (type)
567 && TYPE_PRECISION (type) > 1
568 && !integer_zerop (@1)
569 && (!flag_non_call_exceptions || tree_expr_nonzero_p (@1)))
570 (if (TYPE_UNSIGNED (type))
571 (convert (eq:boolean_type_node @1 { build_one_cst (type); }))
572 (with { tree utype = unsigned_type_for (type); }
573 (cond (le (plus (convert:utype @1) { build_one_cst (utype); })
574 { build_int_cst (utype, 2); })
575 @1 { build_zero_cst (type); })))))
577 /* Combine two successive divisions. Note that combining ceil_div
578 and floor_div is trickier and combining round_div even more so. */
579 (for div (trunc_div exact_div)
581 (div (div@3 @0 INTEGER_CST@1) INTEGER_CST@2)
583 wi::overflow_type overflow;
584 wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@2),
585 TYPE_SIGN (type), &overflow);
587 (if (div == EXACT_DIV_EXPR
588 || optimize_successive_divisions_p (@2, @3))
590 (div @0 { wide_int_to_tree (type, mul); })
591 (if (TYPE_UNSIGNED (type)
592 || mul != wi::min_value (TYPE_PRECISION (type), SIGNED))
593 { build_zero_cst (type); }))))))
595 /* Combine successive multiplications. Similar to above, but handling
596 overflow is different. */
598 (mult (mult @0 INTEGER_CST@1) INTEGER_CST@2)
600 wi::overflow_type overflow;
601 wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@2),
602 TYPE_SIGN (type), &overflow);
604 /* Skip folding on overflow: the only special case is @1 * @2 == -INT_MIN,
605 otherwise undefined overflow implies that @0 must be zero. */
606 (if (!overflow || TYPE_OVERFLOW_WRAPS (type))
607 (mult @0 { wide_int_to_tree (type, mul); }))))
609 /* Similar to above, but there could be an extra add/sub between
610 successive multuiplications. */
612 (mult (plus:s (mult:s@4 @0 INTEGER_CST@1) INTEGER_CST@2) INTEGER_CST@3)
614 bool overflowed = true;
615 wi::overflow_type ovf1, ovf2;
616 wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@3),
617 TYPE_SIGN (type), &ovf1);
618 wide_int add = wi::mul (wi::to_wide (@2), wi::to_wide (@3),
619 TYPE_SIGN (type), &ovf2);
620 if (TYPE_OVERFLOW_UNDEFINED (type))
624 if (ovf1 == wi::OVF_NONE && ovf2 == wi::OVF_NONE
625 && get_global_range_query ()->range_of_expr (vr0, @4)
626 && !vr0.varying_p () && !vr0.undefined_p ())
628 wide_int wmin0 = vr0.lower_bound ();
629 wide_int wmax0 = vr0.upper_bound ();
630 wmin0 = wi::mul (wmin0, wi::to_wide (@3), TYPE_SIGN (type), &ovf1);
631 wmax0 = wi::mul (wmax0, wi::to_wide (@3), TYPE_SIGN (type), &ovf2);
632 if (ovf1 == wi::OVF_NONE && ovf2 == wi::OVF_NONE)
634 wi::add (wmin0, add, TYPE_SIGN (type), &ovf1);
635 wi::add (wmax0, add, TYPE_SIGN (type), &ovf2);
636 if (ovf1 == wi::OVF_NONE && ovf2 == wi::OVF_NONE)
645 /* Skip folding on overflow. */
647 (plus (mult @0 { wide_int_to_tree (type, mul); })
648 { wide_int_to_tree (type, add); }))))
650 /* Similar to above, but a multiplication between successive additions. */
652 (plus (mult:s (plus:s @0 INTEGER_CST@1) INTEGER_CST@2) INTEGER_CST@3)
654 bool overflowed = true;
655 wi::overflow_type ovf1;
656 wi::overflow_type ovf2;
657 wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@2),
658 TYPE_SIGN (type), &ovf1);
659 wide_int add = wi::add (mul, wi::to_wide (@3),
660 TYPE_SIGN (type), &ovf2);
661 if (TYPE_OVERFLOW_UNDEFINED (type))
665 if (ovf1 == wi::OVF_NONE && ovf2 == wi::OVF_NONE
666 && get_global_range_query ()->range_of_expr (vr0, @0)
667 && !vr0.varying_p () && !vr0.undefined_p ())
669 wide_int wmin0 = vr0.lower_bound ();
670 wide_int wmax0 = vr0.upper_bound ();
671 wmin0 = wi::mul (wmin0, wi::to_wide (@2), TYPE_SIGN (type), &ovf1);
672 wmax0 = wi::mul (wmax0, wi::to_wide (@2), TYPE_SIGN (type), &ovf2);
673 if (ovf1 == wi::OVF_NONE && ovf2 == wi::OVF_NONE)
675 wi::add (wmin0, mul, TYPE_SIGN (type), &ovf1);
676 wi::add (wmax0, mul, TYPE_SIGN (type), &ovf2);
677 if (ovf1 == wi::OVF_NONE && ovf2 == wi::OVF_NONE)
686 /* Skip folding on overflow. */
688 (plus (mult @0 @2) { wide_int_to_tree (type, add); }))))
690 /* Optimize A / A to 1.0 if we don't care about
691 NaNs or Infinities. */
694 (if (FLOAT_TYPE_P (type)
695 && ! HONOR_NANS (type)
696 && ! HONOR_INFINITIES (type))
697 { build_one_cst (type); }))
699 /* Optimize -A / A to -1.0 if we don't care about
700 NaNs or Infinities. */
702 (rdiv:C @0 (negate @0))
703 (if (FLOAT_TYPE_P (type)
704 && ! HONOR_NANS (type)
705 && ! HONOR_INFINITIES (type))
706 { build_minus_one_cst (type); }))
708 /* PR71078: x / abs(x) -> copysign (1.0, x) */
710 (rdiv:C (convert? @0) (convert? (abs @0)))
711 (if (SCALAR_FLOAT_TYPE_P (type)
712 && ! HONOR_NANS (type)
713 && ! HONOR_INFINITIES (type))
715 (if (types_match (type, float_type_node))
716 (BUILT_IN_COPYSIGNF { build_one_cst (type); } (convert @0)))
717 (if (types_match (type, double_type_node))
718 (BUILT_IN_COPYSIGN { build_one_cst (type); } (convert @0)))
719 (if (types_match (type, long_double_type_node))
720 (BUILT_IN_COPYSIGNL { build_one_cst (type); } (convert @0))))))
722 /* In IEEE floating point, x/1 is not equivalent to x for snans. */
725 (if (!tree_expr_maybe_signaling_nan_p (@0))
728 /* In IEEE floating point, x/-1 is not equivalent to -x for snans. */
730 (rdiv @0 real_minus_onep)
731 (if (!tree_expr_maybe_signaling_nan_p (@0))
734 (if (flag_reciprocal_math)
735 /* Convert (A/B)/C to A/(B*C). */
737 (rdiv (rdiv:s @0 @1) @2)
738 (rdiv @0 (mult @1 @2)))
740 /* Canonicalize x / (C1 * y) to (x * C2) / y. */
742 (rdiv @0 (mult:s @1 REAL_CST@2))
744 { tree tem = const_binop (RDIV_EXPR, type, build_one_cst (type), @2); }
746 (rdiv (mult @0 { tem; } ) @1))))
748 /* Convert A/(B/C) to (A/B)*C */
750 (rdiv @0 (rdiv:s @1 @2))
751 (mult (rdiv @0 @1) @2)))
753 /* Simplify x / (- y) to -x / y. */
755 (rdiv @0 (negate @1))
756 (rdiv (negate @0) @1))
758 (if (flag_unsafe_math_optimizations)
759 /* Simplify (C / x op 0.0) to x op 0.0 for C != 0, C != Inf/Nan.
760 Since C / x may underflow to zero, do this only for unsafe math. */
761 (for op (lt le gt ge)
764 (op (rdiv REAL_CST@0 @1) real_zerop@2)
765 (if (!HONOR_SIGNED_ZEROS (@1) && !HONOR_INFINITIES (@1))
767 (if (real_less (&dconst0, TREE_REAL_CST_PTR (@0)))
769 /* For C < 0, use the inverted operator. */
770 (if (real_less (TREE_REAL_CST_PTR (@0), &dconst0))
773 /* Optimize (X & (-A)) / A where A is a power of 2, to X >> log2(A) */
774 (for div (trunc_div ceil_div floor_div round_div exact_div)
776 (div (convert? (bit_and @0 INTEGER_CST@1)) INTEGER_CST@2)
777 (if (integer_pow2p (@2)
778 && tree_int_cst_sgn (@2) > 0
779 && tree_nop_conversion_p (type, TREE_TYPE (@0))
780 && wi::to_wide (@2) + wi::to_wide (@1) == 0)
782 { build_int_cst (integer_type_node,
783 wi::exact_log2 (wi::to_wide (@2))); }))))
785 /* If ARG1 is a constant, we can convert this to a multiply by the
786 reciprocal. This does not have the same rounding properties,
787 so only do this if -freciprocal-math. We can actually
788 always safely do it if ARG1 is a power of two, but it's hard to
789 tell if it is or not in a portable manner. */
790 (for cst (REAL_CST COMPLEX_CST VECTOR_CST)
794 (if (flag_reciprocal_math
797 { tree tem = const_binop (RDIV_EXPR, type, build_one_cst (type), @1); }
799 (mult @0 { tem; } )))
800 (if (cst != COMPLEX_CST)
801 (with { tree inverse = exact_inverse (type, @1); }
803 (mult @0 { inverse; } ))))))))
805 (for mod (ceil_mod floor_mod round_mod trunc_mod)
806 /* 0 % X is always zero. */
808 (mod integer_zerop@0 @1)
809 /* But not for 0 % 0 so that we can get the proper warnings and errors. */
810 (if (!integer_zerop (@1))
812 /* X % 1 is always zero. */
814 (mod @0 integer_onep)
815 { build_zero_cst (type); })
816 /* X % -1 is zero. */
818 (mod @0 integer_minus_onep@1)
819 (if (!TYPE_UNSIGNED (type))
820 { build_zero_cst (type); }))
824 /* But not for 0 % 0 so that we can get the proper warnings and errors. */
825 (if (!integer_zerop (@0))
826 { build_zero_cst (type); }))
827 /* (X % Y) % Y is just X % Y. */
829 (mod (mod@2 @0 @1) @1)
831 /* From extract_muldiv_1: (X * C1) % C2 is zero if C1 is a multiple of C2. */
833 (mod (mult @0 INTEGER_CST@1) INTEGER_CST@2)
834 (if (ANY_INTEGRAL_TYPE_P (type)
835 && TYPE_OVERFLOW_UNDEFINED (type)
836 && wi::multiple_of_p (wi::to_wide (@1), wi::to_wide (@2),
838 { build_zero_cst (type); }))
839 /* For (X % C) == 0, if X is signed and C is power of 2, use unsigned
840 modulo and comparison, since it is simpler and equivalent. */
843 (cmp (mod @0 integer_pow2p@2) integer_zerop@1)
844 (if (!TYPE_UNSIGNED (TREE_TYPE (@0)))
845 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
846 (cmp (mod (convert:utype @0) (convert:utype @2)) (convert:utype @1)))))))
848 /* X % -C is the same as X % C. */
850 (trunc_mod @0 INTEGER_CST@1)
851 (if (TYPE_SIGN (type) == SIGNED
852 && !TREE_OVERFLOW (@1)
853 && wi::neg_p (wi::to_wide (@1))
854 && !TYPE_OVERFLOW_TRAPS (type)
855 /* Avoid this transformation if C is INT_MIN, i.e. C == -C. */
856 && !sign_bit_p (@1, @1))
857 (trunc_mod @0 (negate @1))))
859 /* X % -Y is the same as X % Y. */
861 (trunc_mod @0 (convert? (negate @1)))
862 (if (INTEGRAL_TYPE_P (type)
863 && !TYPE_UNSIGNED (type)
864 && !TYPE_OVERFLOW_TRAPS (type)
865 && tree_nop_conversion_p (type, TREE_TYPE (@1))
866 /* Avoid this transformation if X might be INT_MIN or
867 Y might be -1, because we would then change valid
868 INT_MIN % -(-1) into invalid INT_MIN % -1. */
869 && (expr_not_equal_to (@0, wi::to_wide (TYPE_MIN_VALUE (type)))
870 || expr_not_equal_to (@1, wi::minus_one (TYPE_PRECISION
872 (trunc_mod @0 (convert @1))))
874 /* X - (X / Y) * Y is the same as X % Y. */
876 (minus (convert1? @0) (convert2? (mult:c (trunc_div @@0 @@1) @1)))
877 (if (INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
878 (convert (trunc_mod @0 @1))))
880 /* x * (1 + y / x) - y -> x - y % x */
882 (minus (mult:cs @0 (plus:s (trunc_div:s @1 @0) integer_onep)) @1)
883 (if (INTEGRAL_TYPE_P (type))
884 (minus @0 (trunc_mod @1 @0))))
886 /* Optimize TRUNC_MOD_EXPR by a power of two into a BIT_AND_EXPR,
887 i.e. "X % C" into "X & (C - 1)", if X and C are positive.
888 Also optimize A % (C << N) where C is a power of 2,
889 to A & ((C << N) - 1).
890 Also optimize "A shift (B % C)", if C is a power of 2, to
891 "A shift (B & (C - 1))". SHIFT operation include "<<" and ">>"
892 and assume (B % C) is nonnegative as shifts negative values would
894 (match (power_of_two_cand @1)
896 (match (power_of_two_cand @1)
897 (lshift INTEGER_CST@1 @2))
898 (for mod (trunc_mod floor_mod)
899 (for shift (lshift rshift)
901 (shift @0 (mod @1 (power_of_two_cand@2 @3)))
902 (if (integer_pow2p (@3) && tree_int_cst_sgn (@3) > 0)
903 (shift @0 (bit_and @1 (minus @2 { build_int_cst (TREE_TYPE (@2),
906 (mod @0 (convert? (power_of_two_cand@1 @2)))
907 (if ((TYPE_UNSIGNED (type) || tree_expr_nonnegative_p (@0))
908 /* Allow any integral conversions of the divisor, except
909 conversion from narrower signed to wider unsigned type
910 where if @1 would be negative power of two, the divisor
911 would not be a power of two. */
912 && INTEGRAL_TYPE_P (type)
913 && INTEGRAL_TYPE_P (TREE_TYPE (@1))
914 && (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@1))
915 || TYPE_UNSIGNED (TREE_TYPE (@1))
916 || !TYPE_UNSIGNED (type))
917 && integer_pow2p (@2) && tree_int_cst_sgn (@2) > 0)
918 (with { tree utype = TREE_TYPE (@1);
919 if (!TYPE_OVERFLOW_WRAPS (utype))
920 utype = unsigned_type_for (utype); }
921 (bit_and @0 (convert (minus (convert:utype @1)
922 { build_one_cst (utype); })))))))
924 /* Simplify (unsigned t * 2)/2 -> unsigned t & 0x7FFFFFFF. */
926 (trunc_div (mult @0 integer_pow2p@1) @1)
927 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) && TYPE_UNSIGNED (TREE_TYPE (@0)))
928 (bit_and @0 { wide_int_to_tree
929 (type, wi::mask (TYPE_PRECISION (type)
930 - wi::exact_log2 (wi::to_wide (@1)),
931 false, TYPE_PRECISION (type))); })))
933 /* Simplify (unsigned t / 2) * 2 -> unsigned t & ~1. */
935 (mult (trunc_div @0 integer_pow2p@1) @1)
936 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) && TYPE_UNSIGNED (TREE_TYPE (@0)))
937 (bit_and @0 (negate @1))))
939 (for div (trunc_div ceil_div floor_div round_div exact_div)
940 /* Simplify (t * u) / u -> t. */
942 (div (mult:c @0 @1) @1)
943 (if (ANY_INTEGRAL_TYPE_P (type))
944 (if (TYPE_OVERFLOW_UNDEFINED (type) && !TYPE_OVERFLOW_SANITIZED (type))
947 (with {value_range vr0, vr1;}
948 (if (INTEGRAL_TYPE_P (type)
949 && get_range_query (cfun)->range_of_expr (vr0, @0)
950 && get_range_query (cfun)->range_of_expr (vr1, @1)
951 && range_op_handler (MULT_EXPR).overflow_free_p (vr0, vr1))
956 /* Simplify (t * u) / v -> t * (u / v) if u is multiple of v. */
958 (div (mult @0 INTEGER_CST@1) INTEGER_CST@2)
959 (if (INTEGRAL_TYPE_P (type)
960 && wi::multiple_of_p (wi::to_widest (@1), wi::to_widest (@2), SIGNED))
961 (if (TYPE_OVERFLOW_UNDEFINED (type) && !TYPE_OVERFLOW_SANITIZED (type))
962 (mult @0 (div! @1 @2))
963 (with {value_range vr0, vr1;}
964 (if (get_range_query (cfun)->range_of_expr (vr0, @0)
965 && get_range_query (cfun)->range_of_expr (vr1, @1)
966 && range_op_handler (MULT_EXPR).overflow_free_p (vr0, vr1))
967 (mult @0 (div! @1 @2))))
970 /* Simplify (t * u) / (t * v) -> (u / v) if u is multiple of v. */
972 (div (mult @0 INTEGER_CST@1) (mult @0 INTEGER_CST@2))
973 (if (INTEGRAL_TYPE_P (type)
974 && wi::multiple_of_p (wi::to_widest (@1), wi::to_widest (@2), SIGNED))
975 (if (TYPE_OVERFLOW_UNDEFINED (type) && !TYPE_OVERFLOW_SANITIZED (type))
978 (with {value_range vr0, vr1, vr2;}
979 (if (get_range_query (cfun)->range_of_expr (vr0, @0)
980 && get_range_query (cfun)->range_of_expr (vr1, @1)
981 && get_range_query (cfun)->range_of_expr (vr2, @2)
982 && range_op_handler (MULT_EXPR).overflow_free_p (vr0, vr1)
983 && range_op_handler (MULT_EXPR).overflow_free_p (vr0, vr2))
989 (for div (trunc_div exact_div)
990 /* Simplify (X + M*N) / N -> X / N + M. */
992 (div (plus:c@4 @0 (mult:c@3 @1 @2)) @2)
993 (with {value_range vr0, vr1, vr2, vr3, vr4;}
994 (if (INTEGRAL_TYPE_P (type)
995 && get_range_query (cfun)->range_of_expr (vr1, @1)
996 && get_range_query (cfun)->range_of_expr (vr2, @2)
997 /* "N*M" doesn't overflow. */
998 && range_op_handler (MULT_EXPR).overflow_free_p (vr1, vr2)
999 && get_range_query (cfun)->range_of_expr (vr0, @0)
1000 && get_range_query (cfun)->range_of_expr (vr3, @3)
1001 /* "X+(N*M)" doesn't overflow. */
1002 && range_op_handler (PLUS_EXPR).overflow_free_p (vr0, vr3)
1003 && get_range_query (cfun)->range_of_expr (vr4, @4)
1004 && !vr4.undefined_p ()
1005 /* "X+N*M" is not with opposite sign as "X". */
1006 && (TYPE_UNSIGNED (type)
1007 || (vr0.nonnegative_p () && vr4.nonnegative_p ())
1008 || (vr0.nonpositive_p () && vr4.nonpositive_p ())))
1009 (plus (div @0 @2) @1))))
1011 /* Simplify (X - M*N) / N -> X / N - M. */
1013 (div (minus@4 @0 (mult:c@3 @1 @2)) @2)
1014 (with {value_range vr0, vr1, vr2, vr3, vr4;}
1015 (if (INTEGRAL_TYPE_P (type)
1016 && get_range_query (cfun)->range_of_expr (vr1, @1)
1017 && get_range_query (cfun)->range_of_expr (vr2, @2)
1018 /* "N * M" doesn't overflow. */
1019 && range_op_handler (MULT_EXPR).overflow_free_p (vr1, vr2)
1020 && get_range_query (cfun)->range_of_expr (vr0, @0)
1021 && get_range_query (cfun)->range_of_expr (vr3, @3)
1022 /* "X - (N*M)" doesn't overflow. */
1023 && range_op_handler (MINUS_EXPR).overflow_free_p (vr0, vr3)
1024 && get_range_query (cfun)->range_of_expr (vr4, @4)
1025 && !vr4.undefined_p ()
1026 /* "X-N*M" is not with opposite sign as "X". */
1027 && (TYPE_UNSIGNED (type)
1028 || (vr0.nonnegative_p () && vr4.nonnegative_p ())
1029 || (vr0.nonpositive_p () && vr4.nonpositive_p ())))
1030 (minus (div @0 @2) @1)))))
1033 (X + C) / N -> X / N + C / N where C is multiple of N.
1034 (X + C) >> N -> X >> N + C>>N if low N bits of C is 0. */
1035 (for op (trunc_div exact_div rshift)
1037 (op (plus@3 @0 INTEGER_CST@1) INTEGER_CST@2)
1040 wide_int c = wi::to_wide (@1);
1041 wide_int n = wi::to_wide (@2);
1042 bool shift = op == RSHIFT_EXPR;
1043 #define plus_op1(v) (shift ? wi::rshift (v, n, TYPE_SIGN (type)) \
1044 : wi::div_trunc (v, n, TYPE_SIGN (type)))
1045 #define exact_mod(v) (shift ? wi::ctz (v) >= n.to_shwi () \
1046 : wi::multiple_of_p (v, n, TYPE_SIGN (type)))
1047 value_range vr0, vr1, vr3;
1049 (if (INTEGRAL_TYPE_P (type)
1050 && get_range_query (cfun)->range_of_expr (vr0, @0))
1052 && get_range_query (cfun)->range_of_expr (vr1, @1)
1053 /* "X+C" doesn't overflow. */
1054 && range_op_handler (PLUS_EXPR).overflow_free_p (vr0, vr1)
1055 && get_range_query (cfun)->range_of_expr (vr3, @3)
1056 && !vr3.undefined_p ()
1057 /* "X+C" and "X" are not of opposite sign. */
1058 && (TYPE_UNSIGNED (type)
1059 || (vr0.nonnegative_p () && vr3.nonnegative_p ())
1060 || (vr0.nonpositive_p () && vr3.nonpositive_p ())))
1061 (plus (op @0 @2) { wide_int_to_tree (type, plus_op1 (c)); })
1062 (if (!vr0.undefined_p () && TYPE_UNSIGNED (type) && c.sign_mask () < 0
1064 /* unsigned "X-(-C)" doesn't underflow. */
1065 && wi::geu_p (vr0.lower_bound (), -c))
1066 (plus (op @0 @2) { wide_int_to_tree (type, -plus_op1 (-c)); })))))))
1071 /* (nop_outer_cast)-(inner_cast)var -> -(outer_cast)(var)
1072 if var is smaller in precision.
1073 This is always safe for both doing the negative in signed or unsigned
1074 as the value for undefined will not show up.
1075 Note the outer cast cannot be a boolean type as the only valid values
1076 are 0,-1/1 (depending on the signedness of the boolean) and the negative
1077 is there to get the correct value. */
1079 (convert (negate:s@1 (convert:s @0)))
1080 (if (INTEGRAL_TYPE_P (type)
1081 && tree_nop_conversion_p (type, TREE_TYPE (@1))
1082 && TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (@0))
1083 && TREE_CODE (type) != BOOLEAN_TYPE)
1084 (negate (convert @0))))
1086 (for op (negate abs)
1087 /* Simplify cos(-x) and cos(|x|) -> cos(x). Similarly for cosh. */
1088 (for coss (COS COSH)
1092 /* Simplify pow(-x, y) and pow(|x|,y) -> pow(x,y) if y is an even integer. */
1095 (pows (op @0) REAL_CST@1)
1096 (with { HOST_WIDE_INT n; }
1097 (if (real_isinteger (&TREE_REAL_CST (@1), &n) && (n & 1) == 0)
1099 /* Likewise for powi. */
1102 (pows (op @0) INTEGER_CST@1)
1103 (if ((wi::to_wide (@1) & 1) == 0)
1105 /* Strip negate and abs from both operands of hypot. */
1113 /* copysign(-x, y) and copysign(abs(x), y) -> copysign(x, y). */
1114 (for copysigns (COPYSIGN_ALL)
1116 (copysigns (op @0) @1)
1117 (copysigns @0 @1))))
1119 /* abs(x)*abs(x) -> x*x. Should be valid for all types. */
1121 (mult (abs@1 @0) @1)
1124 /* Convert absu(x)*absu(x) -> x*x. */
1126 (mult (absu@1 @0) @1)
1127 (mult (convert@2 @0) @2))
1129 /* cos(copysign(x, y)) -> cos(x). Similarly for cosh. */
1130 (for coss (COS COSH)
1131 (for copysigns (COPYSIGN)
1133 (coss (copysigns @0 @1))
1136 /* pow(copysign(x, y), z) -> pow(x, z) if z is an even integer. */
1138 (for copysigns (COPYSIGN)
1140 (pows (copysigns @0 @2) REAL_CST@1)
1141 (with { HOST_WIDE_INT n; }
1142 (if (real_isinteger (&TREE_REAL_CST (@1), &n) && (n & 1) == 0)
1144 /* Likewise for powi. */
1146 (for copysigns (COPYSIGN)
1148 (pows (copysigns @0 @2) INTEGER_CST@1)
1149 (if ((wi::to_wide (@1) & 1) == 0)
1153 (for copysigns (COPYSIGN)
1154 /* hypot(copysign(x, y), z) -> hypot(x, z). */
1156 (hypots (copysigns @0 @1) @2)
1158 /* hypot(x, copysign(y, z)) -> hypot(x, y). */
1160 (hypots @0 (copysigns @1 @2))
1163 /* copysign(x, CST) -> abs (x). */
1164 (for copysigns (COPYSIGN_ALL)
1166 (copysigns @0 REAL_CST@1)
1167 (if (!REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
1170 /* Transform fneg (fabs (X)) -> copysign (X, -1). */
1173 (IFN_COPYSIGN @0 { build_minus_one_cst (type); }))
1175 /* copysign(copysign(x, y), z) -> copysign(x, z). */
1176 (for copysigns (COPYSIGN_ALL)
1178 (copysigns (copysigns @0 @1) @2)
1181 /* copysign(x,y)*copysign(x,y) -> x*x. */
1182 (for copysigns (COPYSIGN_ALL)
1184 (mult (copysigns@2 @0 @1) @2)
1187 /* ccos(-x) -> ccos(x). Similarly for ccosh. */
1188 (for ccoss (CCOS CCOSH)
1193 /* cabs(-x) and cos(conj(x)) -> cabs(x). */
1194 (for ops (conj negate)
1200 /* Fold (a * (1 << b)) into (a << b) */
1202 (mult:c @0 (convert? (lshift integer_onep@1 @2)))
1203 (if (! FLOAT_TYPE_P (type)
1204 && tree_nop_conversion_p (type, TREE_TYPE (@1)))
1207 /* Shifts by precision or greater result in zero. */
1208 (for shift (lshift rshift)
1210 (shift @0 uniform_integer_cst_p@1)
1211 (if ((GIMPLE || !sanitize_flags_p (SANITIZE_SHIFT_EXPONENT))
1212 /* Leave arithmetic right shifts of possibly negative values alone. */
1213 && (TYPE_UNSIGNED (type)
1214 || shift == LSHIFT_EXPR
1215 || tree_expr_nonnegative_p (@0))
1216 /* Use a signed compare to leave negative shift counts alone. */
1217 && wi::ges_p (wi::to_wide (uniform_integer_cst_p (@1)),
1218 element_precision (type)))
1219 { build_zero_cst (type); })))
1221 /* Shifts by constants distribute over several binary operations,
1222 hence (X << C) + (Y << C) can be simplified to (X + Y) << C. */
1223 (for op (plus minus)
1225 (op (lshift:s @0 @1) (lshift:s @2 @1))
1226 (if (INTEGRAL_TYPE_P (type)
1227 && TYPE_OVERFLOW_WRAPS (type)
1228 && !TYPE_SATURATING (type))
1229 (lshift (op @0 @2) @1))))
1231 (for op (bit_and bit_ior bit_xor)
1233 (op (lshift:s @0 @1) (lshift:s @2 @1))
1234 (if (INTEGRAL_TYPE_P (type))
1235 (lshift (op @0 @2) @1)))
1237 (op (rshift:s @0 @1) (rshift:s @2 @1))
1238 (if (INTEGRAL_TYPE_P (type))
1239 (rshift (op @0 @2) @1))))
1241 /* Fold (1 << (C - x)) where C = precision(type) - 1
1242 into ((1 << C) >> x). */
1244 (lshift integer_onep@0 (minus@1 INTEGER_CST@2 @3))
1245 (if (INTEGRAL_TYPE_P (type)
1246 && wi::eq_p (wi::to_wide (@2), TYPE_PRECISION (type) - 1)
1248 (if (TYPE_UNSIGNED (type))
1249 (rshift (lshift @0 @2) @3)
1251 { tree utype = unsigned_type_for (type); }
1252 (convert (rshift (lshift (convert:utype @0) @2) @3))))))
1254 /* Fold ((type)(a<0)) << SIGNBITOFA into ((type)a) & signbit. */
1256 (lshift (convert (lt @0 integer_zerop@1)) INTEGER_CST@2)
1257 (if (TYPE_SIGN (TREE_TYPE (@0)) == SIGNED
1258 && wi::eq_p (wi::to_wide (@2), TYPE_PRECISION (TREE_TYPE (@0)) - 1))
1259 (with { wide_int wone = wi::one (TYPE_PRECISION (type)); }
1260 (bit_and (convert @0)
1261 { wide_int_to_tree (type,
1262 wi::lshift (wone, wi::to_wide (@2))); }))))
1264 /* Fold (-x >> C) into -(x > 0) where C = precision(type) - 1. */
1265 (for cst (INTEGER_CST VECTOR_CST)
1267 (rshift (negate:s @0) cst@1)
1268 (if (!TYPE_UNSIGNED (type)
1269 && TYPE_OVERFLOW_UNDEFINED (type))
1270 (with { tree stype = TREE_TYPE (@1);
1271 tree bt = truth_type_for (type);
1272 tree zeros = build_zero_cst (type);
1273 tree cst = NULL_TREE; }
1275 /* Handle scalar case. */
1276 (if (INTEGRAL_TYPE_P (type)
1277 /* If we apply the rule to the scalar type before vectorization
1278 we will enforce the result of the comparison being a bool
1279 which will require an extra AND on the result that will be
1280 indistinguishable from when the user did actually want 0
1281 or 1 as the result so it can't be removed. */
1282 && canonicalize_math_after_vectorization_p ()
1283 && wi::eq_p (wi::to_wide (@1), TYPE_PRECISION (type) - 1))
1284 (negate (convert (gt @0 { zeros; }))))
1285 /* Handle vector case. */
1286 (if (VECTOR_INTEGER_TYPE_P (type)
1287 /* First check whether the target has the same mode for vector
1288 comparison results as it's operands do. */
1289 && TYPE_MODE (bt) == TYPE_MODE (type)
1290 /* Then check to see if the target is able to expand the comparison
1291 with the given type later on, otherwise we may ICE. */
1292 && expand_vec_cmp_expr_p (type, bt, GT_EXPR)
1293 && (cst = uniform_integer_cst_p (@1)) != NULL
1294 && wi::eq_p (wi::to_wide (cst), element_precision (type) - 1))
1295 (view_convert (gt:bt @0 { zeros; }))))))))
1297 /* Fold (C1/X)*C2 into (C1*C2)/X. */
1299 (mult (rdiv@3 REAL_CST@0 @1) REAL_CST@2)
1300 (if (flag_associative_math
1303 { tree tem = const_binop (MULT_EXPR, type, @0, @2); }
1305 (rdiv { tem; } @1)))))
1307 /* Simplify ~X & X as zero. */
1309 (bit_and (convert? @0) (convert? @1))
1310 (with { bool wascmp; }
1311 (if (types_match (TREE_TYPE (@0), TREE_TYPE (@1))
1312 && bitwise_inverted_equal_p (@0, @1, wascmp))
1313 { wascmp ? constant_boolean_node (false, type) : build_zero_cst (type); })))
1315 /* PR71636: Transform x & ((1U << b) - 1) -> x & ~(~0U << b); */
1317 (bit_and:c @0 (plus:s (lshift:s integer_onep @1) integer_minus_onep))
1318 (if (TYPE_UNSIGNED (type))
1319 (bit_and @0 (bit_not (lshift { build_all_ones_cst (type); } @1)))))
1321 (for bitop (bit_and bit_ior)
1323 /* PR35691: Transform
1324 (x == 0 & y == 0) -> (x | typeof(x)(y)) == 0.
1325 (x != 0 | y != 0) -> (x | typeof(x)(y)) != 0. */
1327 (bitop (cmp @0 integer_zerop@2) (cmp @1 integer_zerop))
1328 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1329 && INTEGRAL_TYPE_P (TREE_TYPE (@1))
1330 && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
1331 (cmp (bit_ior @0 (convert @1)) @2)))
1333 (x == -1 & y == -1) -> (x & typeof(x)(y)) == -1.
1334 (x != -1 | y != -1) -> (x & typeof(x)(y)) != -1. */
1336 (bitop (cmp @0 integer_all_onesp@2) (cmp @1 integer_all_onesp))
1337 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1338 && INTEGRAL_TYPE_P (TREE_TYPE (@1))
1339 && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
1340 (cmp (bit_and @0 (convert @1)) @2))))
1342 /* Fold (A & ~B) - (A & B) into (A ^ B) - B. */
1344 (minus (bit_and:cs @0 (bit_not @1)) (bit_and:cs @0 @1))
1345 (minus (bit_xor @0 @1) @1))
1347 (minus (bit_and:s @0 INTEGER_CST@2) (bit_and:s @0 INTEGER_CST@1))
1348 (if (~wi::to_wide (@2) == wi::to_wide (@1))
1349 (minus (bit_xor @0 @1) @1)))
1351 /* Fold (A & B) - (A & ~B) into B - (A ^ B). */
1353 (minus (bit_and:cs @0 @1) (bit_and:cs @0 (bit_not @1)))
1354 (minus @1 (bit_xor @0 @1)))
1356 /* Simplify (X & ~Y) |^+ (~X & Y) -> X ^ Y. */
1357 (for op (bit_ior bit_xor plus)
1359 (op (bit_and:c @0 @2) (bit_and:c @3 @1))
1360 (with { bool wascmp0, wascmp1; }
1361 (if (bitwise_inverted_equal_p (@2, @1, wascmp0)
1362 && bitwise_inverted_equal_p (@0, @3, wascmp1)
1363 && ((!wascmp0 && !wascmp1)
1364 || element_precision (type) == 1))
1367 /* PR53979: Transform ((a ^ b) | a) -> (a | b) */
1369 (bit_ior:c (bit_xor:c @0 @1) @0)
1372 /* (a & ~b) | (a ^ b) --> a ^ b */
1374 (bit_ior:c (bit_and:c @0 (bit_not @1)) (bit_xor:c@2 @0 @1))
1377 /* (a & ~b) ^ ~a --> ~(a & b) */
1379 (bit_xor:c (bit_and:cs @0 (bit_not @1)) (bit_not @0))
1380 (bit_not (bit_and @0 @1)))
1382 /* (~a & b) ^ a --> (a | b) */
1384 (bit_xor:c (bit_and:cs (bit_not @0) @1) @0)
1387 /* (a | b) & ~(a ^ b) --> a & b */
1389 (bit_and:c (bit_ior @0 @1) (bit_not (bit_xor:c @0 @1)))
1392 /* (a | b) & (a == b) --> a & b (boolean version of the above). */
1394 (bit_and:c (bit_ior @0 @1) (nop_convert? (eq:c @0 @1)))
1395 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1396 && TYPE_PRECISION (TREE_TYPE (@0)) == 1)
1399 /* a | ~(a ^ b) --> a | ~b */
1401 (bit_ior:c @0 (bit_not:s (bit_xor:c @0 @1)))
1402 (bit_ior @0 (bit_not @1)))
1404 /* a | (a == b) --> a | (b^1) (boolean version of the above). */
1406 (bit_ior:c @0 (nop_convert? (eq:c @0 @1)))
1407 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1408 && TYPE_PRECISION (TREE_TYPE (@0)) == 1)
1409 (bit_ior @0 (bit_xor @1 { build_one_cst (type); }))))
1411 /* a | ((~a) ^ b) --> a | (~b) (alt version of the above 2) */
1413 (bit_ior:c @0 (bit_xor:cs @1 @2))
1414 (with { bool wascmp; }
1415 (if (bitwise_inverted_equal_p (@0, @1, wascmp)
1416 && (!wascmp || element_precision (type) == 1))
1417 (bit_ior @0 (bit_not @2)))))
1419 /* a & ~(a ^ b) --> a & b */
1421 (bit_and:c @0 (bit_not (bit_xor:c @0 @1)))
1424 /* a & (a == b) --> a & b (boolean version of the above). */
1426 (bit_and:c @0 (nop_convert? (eq:c @0 @1)))
1427 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1428 && TYPE_PRECISION (TREE_TYPE (@0)) == 1)
1431 /* a & ((~a) ^ b) --> a & b (alt version of the above 2) */
1433 (bit_and:c @0 (bit_xor:c @1 @2))
1434 (with { bool wascmp; }
1435 (if (bitwise_inverted_equal_p (@0, @1, wascmp)
1436 && (!wascmp || element_precision (type) == 1))
1439 /* (a | b) | (a &^ b) --> a | b */
1440 (for op (bit_and bit_xor)
1442 (bit_ior:c (bit_ior@2 @0 @1) (op:c @0 @1))
1445 /* (a & b) | ~(a ^ b) --> ~(a ^ b) */
1447 (bit_ior:c (bit_and:c @0 @1) (bit_not@2 (bit_xor @0 @1)))
1450 /* (a & b) | (a == b) --> a == b */
1452 (bit_ior:c (bit_and:c @0 @1) (nop_convert?@2 (eq @0 @1)))
1453 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1454 && TYPE_PRECISION (TREE_TYPE (@0)) == 1)
1457 /* ~(~a & b) --> a | ~b */
1459 (bit_not (bit_and:cs (bit_not @0) @1))
1460 (bit_ior @0 (bit_not @1)))
1462 /* ~(~a | b) --> a & ~b */
1464 (bit_not (bit_ior:cs (bit_not @0) @1))
1465 (bit_and @0 (bit_not @1)))
1467 /* (a ^ b) & ((b ^ c) ^ a) --> (a ^ b) & ~c */
1469 (bit_and:c (bit_xor:c@3 @0 @1) (bit_xor:cs (bit_xor:cs @1 @2) @0))
1470 (bit_and @3 (bit_not @2)))
1472 /* (a ^ b) | ((b ^ c) ^ a) --> (a ^ b) | c */
1474 (bit_ior:c (bit_xor:c@3 @0 @1) (bit_xor:c (bit_xor:c @1 @2) @0))
1477 /* (~X | C) ^ D -> (X | C) ^ (~D ^ C) if (~D ^ C) can be simplified. */
1479 (bit_xor:c (bit_ior:cs (bit_not:s @0) @1) @2)
1480 (bit_xor (bit_ior @0 @1) (bit_xor! (bit_not! @2) @1)))
1482 /* (~X & C) ^ D -> (X & C) ^ (D ^ C) if (D ^ C) can be simplified. */
1484 (bit_xor:c (bit_and:cs (bit_not:s @0) @1) @2)
1485 (bit_xor (bit_and @0 @1) (bit_xor! @2 @1)))
1487 /* Simplify (~X & Y) to X ^ Y if we know that (X & ~Y) is 0. */
1489 (bit_and (bit_not SSA_NAME@0) INTEGER_CST@1)
1490 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1491 && wi::bit_and_not (get_nonzero_bits (@0), wi::to_wide (@1)) == 0)
1494 /* For constants M and N, if M == (1LL << cst) - 1 && (N & M) == M,
1495 ((A & N) + B) & M -> (A + B) & M
1496 Similarly if (N & M) == 0,
1497 ((A | N) + B) & M -> (A + B) & M
1498 and for - instead of + (or unary - instead of +)
1499 and/or ^ instead of |.
1500 If B is constant and (B & M) == 0, fold into A & M. */
1501 (for op (plus minus)
1502 (for bitop (bit_and bit_ior bit_xor)
1504 (bit_and (op:s (bitop:s@0 @3 INTEGER_CST@4) @1) INTEGER_CST@2)
1507 tree utype = fold_bit_and_mask (TREE_TYPE (@0), @2, op, @0, bitop,
1508 @3, @4, @1, ERROR_MARK, NULL_TREE,
1511 (convert (bit_and (op (convert:utype { pmop[0]; })
1512 (convert:utype { pmop[1]; }))
1513 (convert:utype @2))))))
1515 (bit_and (op:s @0 (bitop:s@1 @3 INTEGER_CST@4)) INTEGER_CST@2)
1518 tree utype = fold_bit_and_mask (TREE_TYPE (@0), @2, op, @0, ERROR_MARK,
1519 NULL_TREE, NULL_TREE, @1, bitop, @3,
1522 (convert (bit_and (op (convert:utype { pmop[0]; })
1523 (convert:utype { pmop[1]; }))
1524 (convert:utype @2)))))))
1526 (bit_and (op:s @0 @1) INTEGER_CST@2)
1529 tree utype = fold_bit_and_mask (TREE_TYPE (@0), @2, op, @0, ERROR_MARK,
1530 NULL_TREE, NULL_TREE, @1, ERROR_MARK,
1531 NULL_TREE, NULL_TREE, pmop); }
1533 (convert (bit_and (op (convert:utype { pmop[0]; })
1534 (convert:utype { pmop[1]; }))
1535 (convert:utype @2)))))))
1536 (for bitop (bit_and bit_ior bit_xor)
1538 (bit_and (negate:s (bitop:s@0 @2 INTEGER_CST@3)) INTEGER_CST@1)
1541 tree utype = fold_bit_and_mask (TREE_TYPE (@0), @1, NEGATE_EXPR, @0,
1542 bitop, @2, @3, NULL_TREE, ERROR_MARK,
1543 NULL_TREE, NULL_TREE, pmop); }
1545 (convert (bit_and (negate (convert:utype { pmop[0]; }))
1546 (convert:utype @1)))))))
1548 /* X % Y is smaller than Y. */
1551 (cmp:c (trunc_mod @0 @1) @1)
1552 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
1553 { constant_boolean_node (cmp == LT_EXPR, type); })))
1557 (bit_ior @0 integer_all_onesp@1)
1562 (bit_ior @0 integer_zerop)
1567 (bit_and @0 integer_zerop@1)
1572 (for op (bit_ior bit_xor)
1574 (op (convert? @0) (convert? @1))
1575 (with { bool wascmp; }
1576 (if (types_match (TREE_TYPE (@0), TREE_TYPE (@1))
1577 && bitwise_inverted_equal_p (@0, @1, wascmp))
1580 ? constant_boolean_node (true, type)
1581 : build_all_ones_cst (TREE_TYPE (@0)); })))))
1586 { build_zero_cst (type); })
1588 /* Canonicalize X ^ ~0 to ~X. */
1590 (bit_xor @0 integer_all_onesp@1)
1595 (bit_and @0 integer_all_onesp)
1598 /* x & x -> x, x | x -> x */
1599 (for bitop (bit_and bit_ior)
1604 /* x & C -> x if we know that x & ~C == 0. */
1607 (bit_and SSA_NAME@0 INTEGER_CST@1)
1608 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1609 && wi::bit_and_not (get_nonzero_bits (@0), wi::to_wide (@1)) == 0)
1612 /* `a & (x | CST)` -> a if we know that (a & ~CST) == 0 */
1614 (bit_and:c SSA_NAME@0 (bit_ior @1 INTEGER_CST@2))
1615 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1616 && wi::bit_and_not (get_nonzero_bits (@0), wi::to_wide (@2)) == 0)
1619 /* x | C -> C if we know that x & ~C == 0. */
1621 (bit_ior SSA_NAME@0 INTEGER_CST@1)
1622 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1623 && wi::bit_and_not (get_nonzero_bits (@0), wi::to_wide (@1)) == 0)
1627 /* ~(~X - Y) -> X + Y and ~(~X + Y) -> X - Y. */
1629 (bit_not (minus (bit_not @0) @1))
1632 (bit_not (plus:c (bit_not @0) @1))
1634 /* (~X - ~Y) -> Y - X. */
1636 (minus (bit_not @0) (bit_not @1))
1637 (if (!TYPE_OVERFLOW_SANITIZED (type))
1638 (with { tree utype = unsigned_type_for (type); }
1639 (convert (minus (convert:utype @1) (convert:utype @0))))))
1641 /* ~(X - Y) -> ~X + Y. */
1643 (bit_not (minus:s @0 @1))
1644 (plus (bit_not @0) @1))
1646 (bit_not (plus:s @0 INTEGER_CST@1))
1647 (if ((INTEGRAL_TYPE_P (type)
1648 && TYPE_UNSIGNED (type))
1649 || (!TYPE_OVERFLOW_SANITIZED (type)
1650 && may_negate_without_overflow_p (@1)))
1651 (plus (bit_not @0) { const_unop (NEGATE_EXPR, type, @1); })))
1654 /* ~X + Y -> (Y - X) - 1. */
1656 (plus:c (bit_not @0) @1)
1657 (if (ANY_INTEGRAL_TYPE_P (type)
1658 && TYPE_OVERFLOW_WRAPS (type)
1659 /* -1 - X is folded to ~X, so we'd recurse endlessly. */
1660 && !integer_all_onesp (@1))
1661 (plus (minus @1 @0) { build_minus_one_cst (type); })
1662 (if (INTEGRAL_TYPE_P (type)
1663 && TREE_CODE (@1) == INTEGER_CST
1664 && wi::to_wide (@1) != wi::min_value (TYPE_PRECISION (type),
1666 (minus (plus @1 { build_minus_one_cst (type); }) @0))))
1669 /* ~(X >> Y) -> ~X >> Y if ~X can be simplified. */
1671 (bit_not (rshift:s @0 @1))
1672 (if (!TYPE_UNSIGNED (TREE_TYPE (@0)))
1673 (rshift (bit_not! @0) @1)
1674 /* For logical right shifts, this is possible only if @0 doesn't
1675 have MSB set and the logical right shift is changed into
1676 arithmetic shift. */
1677 (if (INTEGRAL_TYPE_P (type)
1678 && !wi::neg_p (tree_nonzero_bits (@0)))
1679 (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
1680 (convert (rshift (bit_not! (convert:stype @0)) @1))))))
1682 /* x + (x & 1) -> (x + 1) & ~1 */
1684 (plus:c @0 (bit_and:s @0 integer_onep@1))
1685 (bit_and (plus @0 @1) (bit_not @1)))
1687 /* x & ~(x & y) -> x & ~y */
1688 /* x | ~(x | y) -> x | ~y */
1689 (for bitop (bit_and bit_ior)
1691 (bitop:c @0 (bit_not (bitop:cs @0 @1)))
1692 (bitop @0 (bit_not @1))))
1694 /* (~x & y) | ~(x | y) -> ~x */
1696 (bit_ior:c (bit_and:c (bit_not@2 @0) @1) (bit_not (bit_ior:c @0 @1)))
1699 /* (x | y) ^ (x | ~y) -> ~x */
1701 (bit_xor:c (bit_ior:c @0 @1) (bit_ior:c @0 (bit_not @1)))
1704 /* (x & y) | ~(x | y) -> ~(x ^ y) */
1706 (bit_ior:c (bit_and:s @0 @1) (bit_not:s (bit_ior:s @0 @1)))
1707 (bit_not (bit_xor @0 @1)))
1709 /* (~x | y) ^ (x ^ y) -> x | ~y */
1711 (bit_xor:c (bit_ior:cs (bit_not @0) @1) (bit_xor:s @0 @1))
1712 (bit_ior @0 (bit_not @1)))
1714 /* (x ^ y) | ~(x | y) -> ~(x & y) */
1716 (bit_ior:c (bit_xor:s @0 @1) (bit_not:s (bit_ior:s @0 @1)))
1717 (bit_not (bit_and @0 @1)))
1719 /* (x & y) ^ (x | y) -> x ^ y */
1721 (bit_xor:c (bit_and @0 @1) (bit_ior @0 @1))
1724 /* (x ^ y) ^ (x | y) -> x & y */
1726 (bit_xor:c (bit_xor @0 @1) (bit_ior @0 @1))
1729 /* (x & y) + (x ^ y) -> x | y */
1730 /* (x & y) | (x ^ y) -> x | y */
1731 /* (x & y) ^ (x ^ y) -> x | y */
1732 (for op (plus bit_ior bit_xor)
1734 (op:c (bit_and @0 @1) (bit_xor @0 @1))
1737 /* (x & y) + (x | y) -> x + y */
1739 (plus:c (bit_and @0 @1) (bit_ior @0 @1))
1742 /* (x + y) - (x | y) -> x & y */
1744 (minus (plus @0 @1) (bit_ior @0 @1))
1745 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1746 && !TYPE_SATURATING (type))
1749 /* (x + y) - (x & y) -> x | y */
1751 (minus (plus @0 @1) (bit_and @0 @1))
1752 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1753 && !TYPE_SATURATING (type))
1756 /* (x | y) - y -> (x & ~y) */
1758 (minus (bit_ior:cs @0 @1) @1)
1759 (bit_and @0 (bit_not @1)))
1761 /* (x | y) - (x ^ y) -> x & y */
1763 (minus (bit_ior @0 @1) (bit_xor @0 @1))
1766 /* (x | y) - (x & y) -> x ^ y */
1768 (minus (bit_ior @0 @1) (bit_and @0 @1))
1771 /* (x | y) & ~(x & y) -> x ^ y */
1773 (bit_and:c (bit_ior @0 @1) (bit_not (bit_and @0 @1)))
1776 /* (x | y) & (~x ^ y) -> x & y */
1778 (bit_and:c (bit_ior:c @0 @1) (bit_xor:c @1 @2))
1779 (with { bool wascmp; }
1780 (if (bitwise_inverted_equal_p (@0, @2, wascmp)
1781 && (!wascmp || element_precision (type) == 1))
1784 /* (~x | y) & (x | ~y) -> ~(x ^ y) */
1786 (bit_and (bit_ior:cs (bit_not @0) @1) (bit_ior:cs @0 (bit_not @1)))
1787 (bit_not (bit_xor @0 @1)))
1789 /* (~x | y) ^ (x | ~y) -> x ^ y */
1791 (bit_xor (bit_ior:c (bit_not @0) @1) (bit_ior:c @0 (bit_not @1)))
1794 /* ((x & y) - (x | y)) - 1 -> ~(x ^ y) */
1796 (plus (nop_convert1? (minus@2 (nop_convert2? (bit_and:c @0 @1))
1797 (nop_convert2? (bit_ior @0 @1))))
1799 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1800 && !TYPE_SATURATING (type) && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2))
1801 && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@2))
1802 && !TYPE_SATURATING (TREE_TYPE (@2)))
1803 (bit_not (convert (bit_xor @0 @1)))))
1805 (minus (nop_convert1? (plus@2 (nop_convert2? (bit_and:c @0 @1))
1807 (nop_convert3? (bit_ior @0 @1)))
1808 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1809 && !TYPE_SATURATING (type) && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2))
1810 && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@2))
1811 && !TYPE_SATURATING (TREE_TYPE (@2)))
1812 (bit_not (convert (bit_xor @0 @1)))))
1814 (minus (nop_convert1? (bit_and @0 @1))
1815 (nop_convert2? (plus@2 (nop_convert3? (bit_ior:c @0 @1))
1817 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1818 && !TYPE_SATURATING (type) && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2))
1819 && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@2))
1820 && !TYPE_SATURATING (TREE_TYPE (@2)))
1821 (bit_not (convert (bit_xor @0 @1)))))
1823 /* ~x & ~y -> ~(x | y)
1824 ~x | ~y -> ~(x & y) */
1825 (for op (bit_and bit_ior)
1826 rop (bit_ior bit_and)
1828 (op (convert1? (bit_not @0)) (convert2? (bit_not @1)))
1829 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1830 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
1831 (bit_not (rop (convert @0) (convert @1))))))
1833 /* If we are XORing or adding two BIT_AND_EXPR's, both of which are and'ing
1834 with a constant, and the two constants have no bits in common,
1835 we should treat this as a BIT_IOR_EXPR since this may produce more
1837 (for op (bit_xor plus)
1839 (op (convert1? (bit_and@4 @0 INTEGER_CST@1))
1840 (convert2? (bit_and@5 @2 INTEGER_CST@3)))
1841 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
1842 && tree_nop_conversion_p (type, TREE_TYPE (@2))
1843 && (wi::to_wide (@1) & wi::to_wide (@3)) == 0)
1844 (bit_ior (convert @4) (convert @5)))))
1846 /* (X | Y) ^ X -> Y & ~ X*/
1848 (bit_xor:c (convert1? (bit_ior:c @@0 @1)) (convert2? @0))
1849 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1850 (convert (bit_and @1 (bit_not @0)))))
1852 /* (~X | Y) ^ X -> ~(X & Y). */
1854 (bit_xor:c (nop_convert1? (bit_ior:c (nop_convert2? (bit_not @0)) @1)) @2)
1855 (if (bitwise_equal_p (@0, @2))
1856 (convert (bit_not (bit_and @0 (convert @1))))))
1858 /* Convert ~X ^ ~Y to X ^ Y. */
1860 (bit_xor (convert1? (bit_not @0)) (convert2? (bit_not @1)))
1861 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1862 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
1863 (bit_xor (convert @0) (convert @1))))
1865 /* Convert ~X ^ C to X ^ ~C. */
1867 (bit_xor (convert? (bit_not @0)) INTEGER_CST@1)
1868 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1869 (bit_xor (convert @0) (bit_not @1))))
1871 /* Fold (X & Y) ^ Y and (X ^ Y) & Y as ~X & Y. */
1872 (for opo (bit_and bit_xor)
1873 opi (bit_xor bit_and)
1875 (opo:c (opi:cs @0 @1) @1)
1876 (bit_and (bit_not @0) @1)))
1878 /* Given a bit-wise operation CODE applied to ARG0 and ARG1, see if both
1879 operands are another bit-wise operation with a common input. If so,
1880 distribute the bit operations to save an operation and possibly two if
1881 constants are involved. For example, convert
1882 (A | B) & (A | C) into A | (B & C)
1883 Further simplification will occur if B and C are constants. */
1884 (for op (bit_and bit_ior bit_xor)
1885 rop (bit_ior bit_and bit_and)
1887 (op (convert? (rop:c @@0 @1)) (convert? (rop:c @0 @2)))
1888 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1889 && tree_nop_conversion_p (type, TREE_TYPE (@2)))
1890 (rop (convert @0) (op (convert @1) (convert @2))))))
1892 /* Some simple reassociation for bit operations, also handled in reassoc. */
1893 /* (X & Y) & Y -> X & Y
1894 (X | Y) | Y -> X | Y */
1895 (for op (bit_and bit_ior)
1897 (op:c (convert1?@2 (op:c @0 @@1)) (convert2? @1))
1899 /* (X ^ Y) ^ Y -> X */
1901 (bit_xor:c (convert1? (bit_xor:c @0 @@1)) (convert2? @1))
1904 /* (X & ~Y) & Y -> 0 */
1906 (bit_and:c (bit_and @0 @1) @2)
1907 (with { bool wascmp; }
1908 (if (bitwise_inverted_equal_p (@0, @2, wascmp)
1909 || bitwise_inverted_equal_p (@1, @2, wascmp))
1910 { wascmp ? constant_boolean_node (false, type) : build_zero_cst (type); })))
1911 /* (X | ~Y) | Y -> -1 */
1913 (bit_ior:c (bit_ior @0 @1) @2)
1914 (with { bool wascmp; }
1915 (if ((bitwise_inverted_equal_p (@0, @2, wascmp)
1916 || bitwise_inverted_equal_p (@1, @2, wascmp))
1917 && (!wascmp || element_precision (type) == 1))
1918 { build_all_ones_cst (TREE_TYPE (@0)); })))
1920 /* (X & Y) & (X & Z) -> (X & Y) & Z
1921 (X | Y) | (X | Z) -> (X | Y) | Z */
1922 (for op (bit_and bit_ior)
1924 (op (convert1?@3 (op:c@4 @0 @1)) (convert2?@5 (op:c@6 @0 @2)))
1925 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1926 && tree_nop_conversion_p (type, TREE_TYPE (@2)))
1927 (if (single_use (@5) && single_use (@6))
1928 (op @3 (convert @2))
1929 (if (single_use (@3) && single_use (@4))
1930 (op (convert @1) @5))))))
1931 /* (X ^ Y) ^ (X ^ Z) -> Y ^ Z */
1933 (bit_xor (convert1? (bit_xor:c @0 @1)) (convert2? (bit_xor:c @0 @2)))
1934 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1935 && tree_nop_conversion_p (type, TREE_TYPE (@2)))
1936 (bit_xor (convert @1) (convert @2))))
1938 /* Convert abs (abs (X)) into abs (X).
1939 also absu (absu (X)) into absu (X). */
1945 (absu (convert@2 (absu@1 @0)))
1946 (if (tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@1)))
1949 /* Convert abs[u] (-X) -> abs[u] (X). */
1958 /* Convert abs[u] (X) where X is nonnegative -> (X). */
1960 (abs tree_expr_nonnegative_p@0)
1964 (absu tree_expr_nonnegative_p@0)
1967 /* Simplify (-(X < 0) | 1) * X into abs (X) or absu(X). */
1969 (mult:c (nop_convert1?
1970 (bit_ior (nop_convert2? (negate (convert? (lt @0 integer_zerop))))
1973 (if (INTEGRAL_TYPE_P (type)
1974 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
1975 && !TYPE_UNSIGNED (TREE_TYPE (@0)))
1976 (if (TYPE_UNSIGNED (type))
1983 /* A few cases of fold-const.cc negate_expr_p predicate. */
1984 (match negate_expr_p
1986 (if ((INTEGRAL_TYPE_P (type)
1987 && TYPE_UNSIGNED (type))
1988 || (!TYPE_OVERFLOW_SANITIZED (type)
1989 && may_negate_without_overflow_p (t)))))
1990 (match negate_expr_p
1992 (match negate_expr_p
1994 (if (!TYPE_OVERFLOW_SANITIZED (type))))
1995 (match negate_expr_p
1997 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (t)))))
1998 /* VECTOR_CST handling of non-wrapping types would recurse in unsupported
2000 (match negate_expr_p
2002 (if (FLOAT_TYPE_P (TREE_TYPE (type)) || TYPE_OVERFLOW_WRAPS (type))))
2003 (match negate_expr_p
2005 (if ((ANY_INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_WRAPS (type))
2006 || (FLOAT_TYPE_P (type)
2007 && !HONOR_SIGN_DEPENDENT_ROUNDING (type)
2008 && !HONOR_SIGNED_ZEROS (type)))))
2010 /* (-A) * (-B) -> A * B */
2012 (mult:c (convert1? (negate @0)) (convert2? negate_expr_p@1))
2013 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
2014 && tree_nop_conversion_p (type, TREE_TYPE (@1)))
2015 (mult (convert @0) (convert (negate @1)))))
2017 /* -(A + B) -> (-B) - A. */
2019 (negate (plus:c @0 negate_expr_p@1))
2020 (if (!HONOR_SIGN_DEPENDENT_ROUNDING (type)
2021 && !HONOR_SIGNED_ZEROS (type))
2022 (minus (negate @1) @0)))
2024 /* -(A - B) -> B - A. */
2026 (negate (minus @0 @1))
2027 (if ((ANY_INTEGRAL_TYPE_P (type) && !TYPE_OVERFLOW_SANITIZED (type))
2028 || (FLOAT_TYPE_P (type)
2029 && !HONOR_SIGN_DEPENDENT_ROUNDING (type)
2030 && !HONOR_SIGNED_ZEROS (type)))
2033 (negate (pointer_diff @0 @1))
2034 (if (TYPE_OVERFLOW_UNDEFINED (type))
2035 (pointer_diff @1 @0)))
2037 /* A - B -> A + (-B) if B is easily negatable. */
2039 (minus @0 negate_expr_p@1)
2040 (if (!FIXED_POINT_TYPE_P (type))
2041 (plus @0 (negate @1))))
2043 /* 1 - a is a ^ 1 if a had a bool range. */
2044 /* This is only enabled for gimple as sometimes
2045 cfun is not set for the function which contains
2046 the SSA_NAME (e.g. while IPA passes are happening,
2047 fold might be called). */
2049 (minus integer_onep@0 SSA_NAME@1)
2050 (if (INTEGRAL_TYPE_P (type)
2051 && ssa_name_has_boolean_range (@1))
2054 /* Other simplifications of negation (c.f. fold_negate_expr_1). */
2056 (negate (mult:c@0 @1 negate_expr_p@2))
2057 (if (! TYPE_UNSIGNED (type)
2058 && ! HONOR_SIGN_DEPENDENT_ROUNDING (type)
2060 (mult @1 (negate @2))))
2063 (negate (rdiv@0 @1 negate_expr_p@2))
2064 (if (! HONOR_SIGN_DEPENDENT_ROUNDING (type)
2066 (rdiv @1 (negate @2))))
2069 (negate (rdiv@0 negate_expr_p@1 @2))
2070 (if (! HONOR_SIGN_DEPENDENT_ROUNDING (type)
2072 (rdiv (negate @1) @2)))
2074 /* Fold -((int)x >> (prec - 1)) into (unsigned)x >> (prec - 1). */
2076 (negate (convert? (rshift @0 INTEGER_CST@1)))
2077 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
2078 && wi::to_wide (@1) == element_precision (type) - 1)
2079 (with { tree stype = TREE_TYPE (@0);
2080 tree ntype = TYPE_UNSIGNED (stype) ? signed_type_for (stype)
2081 : unsigned_type_for (stype); }
2082 (if (VECTOR_TYPE_P (type))
2083 (view_convert (rshift (view_convert:ntype @0) @1))
2084 (convert (rshift (convert:ntype @0) @1))))))
2086 /* Try to fold (type) X op CST -> (type) (X op ((type-x) CST))
2088 For bitwise binary operations apply operand conversions to the
2089 binary operation result instead of to the operands. This allows
2090 to combine successive conversions and bitwise binary operations.
2091 We combine the above two cases by using a conditional convert. */
2092 (for bitop (bit_and bit_ior bit_xor)
2094 (bitop (convert@2 @0) (convert?@3 @1))
2095 (if (((TREE_CODE (@1) == INTEGER_CST
2096 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
2097 && (int_fits_type_p (@1, TREE_TYPE (@0))
2098 || tree_nop_conversion_p (TREE_TYPE (@0), type)))
2099 || types_match (@0, @1))
2100 && !POINTER_TYPE_P (TREE_TYPE (@0))
2101 && !VECTOR_TYPE_P (TREE_TYPE (@0))
2102 && TREE_CODE (TREE_TYPE (@0)) != OFFSET_TYPE
2103 /* ??? This transform conflicts with fold-const.cc doing
2104 Convert (T)(x & c) into (T)x & (T)c, if c is an integer
2105 constants (if x has signed type, the sign bit cannot be set
2106 in c). This folds extension into the BIT_AND_EXPR.
2107 Restrict it to GIMPLE to avoid endless recursions. */
2108 && (bitop != BIT_AND_EXPR || GIMPLE)
2109 && (/* That's a good idea if the conversion widens the operand, thus
2110 after hoisting the conversion the operation will be narrower.
2111 It is also a good if the conversion is a nop as moves the
2112 conversion to one side; allowing for combining of the conversions. */
2113 TYPE_PRECISION (TREE_TYPE (@0)) < TYPE_PRECISION (type)
2114 /* The conversion check for being a nop can only be done at the gimple
2115 level as fold_binary has some re-association code which can conflict
2116 with this if there is a "constant" which is not a full INTEGER_CST. */
2117 || (GIMPLE && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (type))
2118 /* It's also a good idea if the conversion is to a non-integer
2120 || GET_MODE_CLASS (TYPE_MODE (type)) != MODE_INT
2121 /* Or if the precision of TO is not the same as the precision
2123 || !type_has_mode_precision_p (type)
2124 /* In GIMPLE, getting rid of 2 conversions for one new results
2127 && TREE_CODE (@1) != INTEGER_CST
2128 && tree_nop_conversion_p (type, TREE_TYPE (@0))
2130 && single_use (@3))))
2131 (convert (bitop @0 (convert @1)))))
2132 /* In GIMPLE, getting rid of 2 conversions for one new results
2135 (convert (bitop:cs@2 (nop_convert:s @0) @1))
2137 && TREE_CODE (@1) != INTEGER_CST
2138 && tree_nop_conversion_p (type, TREE_TYPE (@2))
2139 && types_match (type, @0)
2140 && !POINTER_TYPE_P (TREE_TYPE (@0))
2141 && TREE_CODE (TREE_TYPE (@0)) != OFFSET_TYPE)
2142 (bitop @0 (convert @1)))))
2144 (for bitop (bit_and bit_ior)
2145 rbitop (bit_ior bit_and)
2146 /* (x | y) & x -> x */
2147 /* (x & y) | x -> x */
2149 (bitop:c (rbitop:c @0 @1) @0)
2151 /* (~x | y) & x -> x & y */
2152 /* (~x & y) | x -> x | y */
2154 (bitop:c (rbitop:c @2 @1) @0)
2155 (with { bool wascmp; }
2156 (if (bitwise_inverted_equal_p (@0, @2, wascmp)
2157 && (!wascmp || element_precision (type) == 1))
2159 /* (x | y) & (x & z) -> (x & z) */
2160 /* (x & y) | (x | z) -> (x | z) */
2162 (bitop:c (rbitop:c @0 @1) (bitop:c@3 @0 @2))
2164 /* (x | c) & ~(y | c) -> x & ~(y | c) */
2165 /* (x & c) | ~(y & c) -> x | ~(y & c) */
2167 (bitop:c (rbitop:c @0 @1) (bit_not@3 (rbitop:c @1 @2)))
2169 /* x & ~(y | x) -> 0 */
2170 /* x | ~(y & x) -> -1 */
2172 (bitop:c @0 (bit_not (rbitop:c @0 @1)))
2173 (if (bitop == BIT_AND_EXPR)
2174 { build_zero_cst (type); }
2175 { build_minus_one_cst (type); })))
2177 /* ((x | y) & z) | x -> (z & y) | x
2178 ((x ^ y) & z) | x -> (z & y) | x */
2179 (for op (bit_ior bit_xor)
2181 (bit_ior:c (nop_convert1?:s
2182 (bit_and:cs (nop_convert2?:s (op:cs @0 @1)) @2)) @3)
2183 (if (bitwise_equal_p (@0, @3))
2184 (convert (bit_ior (bit_and @1 (convert @2)) (convert @0))))))
2186 /* (x | CST1) & CST2 -> (x & CST2) | (CST1 & CST2) */
2188 (bit_and (bit_ior @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
2189 (bit_ior (bit_and @0 @2) (bit_and! @1 @2)))
2191 /* Combine successive equal operations with constants. */
2192 (for bitop (bit_and bit_ior bit_xor)
2194 (bitop (bitop @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
2195 (if (!CONSTANT_CLASS_P (@0))
2196 /* This is the canonical form regardless of whether (bitop @1 @2) can be
2197 folded to a constant. */
2198 (bitop @0 (bitop! @1 @2))
2199 /* In this case we have three constants and (bitop @0 @1) doesn't fold
2200 to a constant. This can happen if @0 or @1 is a POLY_INT_CST and if
2201 the values involved are such that the operation can't be decided at
2202 compile time. Try folding one of @0 or @1 with @2 to see whether
2203 that combination can be decided at compile time.
2205 Keep the existing form if both folds fail, to avoid endless
2207 (with { tree cst1 = const_binop (bitop, type, @0, @2); }
2209 (bitop @1 { cst1; })
2210 (with { tree cst2 = const_binop (bitop, type, @1, @2); }
2212 (bitop @0 { cst2; }))))))))
2214 /* Try simple folding for X op !X, and X op X with the help
2215 of the truth_valued_p and logical_inverted_value predicates. */
2216 (match truth_valued_p
2218 (if (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1)))
2219 (for op (tcc_comparison truth_and truth_andif truth_or truth_orif truth_xor)
2220 (match truth_valued_p
2222 (match truth_valued_p
2225 (match (logical_inverted_value @0)
2227 (match (logical_inverted_value @0)
2228 (bit_not truth_valued_p@0))
2229 (match (logical_inverted_value @0)
2230 (eq @0 integer_zerop))
2231 (match (logical_inverted_value @0)
2232 (ne truth_valued_p@0 integer_truep))
2233 (match (logical_inverted_value @0)
2234 (bit_xor truth_valued_p@0 integer_truep))
2238 (bit_and:c @0 (logical_inverted_value @0))
2239 { build_zero_cst (type); })
2240 /* X | !X and X ^ !X -> 1, , if X is truth-valued. */
2241 (for op (bit_ior bit_xor)
2243 (op:c truth_valued_p@0 (logical_inverted_value @0))
2244 { constant_boolean_node (true, type); }))
2245 /* X ==/!= !X is false/true. */
2248 (op:c truth_valued_p@0 (logical_inverted_value @0))
2249 { constant_boolean_node (op == NE_EXPR ? true : false, type); }))
2253 (bit_not (bit_not @0))
2256 /* zero_one_valued_p will match when a value is known to be either
2257 0 or 1 including constants 0 or 1.
2258 Signed 1-bits includes -1 so they cannot match here. */
2259 (match zero_one_valued_p
2261 (if (INTEGRAL_TYPE_P (type)
2262 && (TYPE_UNSIGNED (type)
2263 || TYPE_PRECISION (type) > 1)
2264 && wi::leu_p (tree_nonzero_bits (@0), 1))))
2265 (match zero_one_valued_p
2267 (if (INTEGRAL_TYPE_P (type)
2268 && (TYPE_UNSIGNED (type)
2269 || TYPE_PRECISION (type) > 1))))
2271 /* (a&1) is always [0,1] too. This is useful again when
2272 the range is not known. */
2273 /* Note this can't be recursive due to VN handling of equivalents,
2274 VN and would cause an infinite recursion. */
2275 (match zero_one_valued_p
2276 (bit_and:c@0 @1 integer_onep)
2277 (if (INTEGRAL_TYPE_P (type))))
2279 /* A conversion from an zero_one_valued_p is still a [0,1].
2280 This is useful when the range of a variable is not known */
2281 /* Note this matches can't be recursive because of the way VN handles
2282 nop conversions being equivalent and then recursive between them. */
2283 (match zero_one_valued_p
2285 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2286 && (TYPE_UNSIGNED (TREE_TYPE (@1))
2287 || TYPE_PRECISION (TREE_TYPE (@1)) > 1)
2288 && INTEGRAL_TYPE_P (type)
2289 && (TYPE_UNSIGNED (type)
2290 || TYPE_PRECISION (type) > 1)
2291 && wi::leu_p (tree_nonzero_bits (@1), 1))))
2293 /* Transform { 0 or 1 } * { 0 or 1 } into { 0 or 1 } & { 0 or 1 }. */
2295 (mult zero_one_valued_p@0 zero_one_valued_p@1)
2296 (if (INTEGRAL_TYPE_P (type))
2299 (for cmp (tcc_comparison)
2300 icmp (inverted_tcc_comparison)
2301 /* Fold (((a < b) & c) | ((a >= b) & d)) into (a < b ? c : d) & 1. */
2304 (bit_and:c (convert? (cmp@0 @01 @02)) @3)
2305 (bit_and:c (convert? (icmp@4 @01 @02)) @5))
2306 (if (INTEGRAL_TYPE_P (type)
2307 && invert_tree_comparison (cmp, HONOR_NANS (@01)) == icmp
2308 /* The scalar version has to be canonicalized after vectorization
2309 because it makes unconditional loads conditional ones, which
2310 means we lose vectorization because the loads may trap. */
2311 && canonicalize_math_after_vectorization_p ())
2312 (bit_and (cond @0 @3 @5) { build_one_cst (type); })))
2314 /* Fold ((-(a < b) & c) | (-(a >= b) & d)) into a < b ? c : d. This is
2315 canonicalized further and we recognize the conditional form:
2316 (a < b ? c : 0) | (a >= b ? d : 0) into a < b ? c : d. */
2319 (cond (cmp@0 @01 @02) @3 zerop)
2320 (cond (icmp@4 @01 @02) @5 zerop))
2321 (if (INTEGRAL_TYPE_P (type)
2322 && invert_tree_comparison (cmp, HONOR_NANS (@01)) == icmp
2323 /* The scalar version has to be canonicalized after vectorization
2324 because it makes unconditional loads conditional ones, which
2325 means we lose vectorization because the loads may trap. */
2326 && canonicalize_math_after_vectorization_p ())
2329 /* Vector Fold (((a < b) & c) | ((a >= b) & d)) into a < b ? c : d.
2330 and ((~(a < b) & c) | (~(a >= b) & d)) into a < b ? c : d. */
2333 (bit_and:c (vec_cond:s (cmp@0 @6 @7) @4 @5) @2)
2334 (bit_and:c (vec_cond:s (icmp@1 @6 @7) @4 @5) @3))
2335 (if (integer_zerop (@5)
2336 && invert_tree_comparison (cmp, HONOR_NANS (@6)) == icmp)
2338 (if (integer_onep (@4))
2339 (bit_and (vec_cond @0 @2 @3) @4))
2340 (if (integer_minus_onep (@4))
2341 (vec_cond @0 @2 @3)))
2342 (if (integer_zerop (@4)
2343 && invert_tree_comparison (cmp, HONOR_NANS (@6)) == icmp)
2345 (if (integer_onep (@5))
2346 (bit_and (vec_cond @0 @3 @2) @5))
2347 (if (integer_minus_onep (@5))
2348 (vec_cond @0 @3 @2))))))
2350 /* Scalar Vectorized Fold ((-(a < b) & c) | (-(a >= b) & d))
2351 into a < b ? d : c. */
2354 (vec_cond:s (cmp@0 @4 @5) @2 integer_zerop)
2355 (vec_cond:s (icmp@1 @4 @5) @3 integer_zerop))
2356 (if (invert_tree_comparison (cmp, HONOR_NANS (@4)) == icmp)
2357 (vec_cond @0 @2 @3))))
2359 /* Transform X & -Y into X * Y when Y is { 0 or 1 }. */
2361 (bit_and:c (convert? (negate zero_one_valued_p@0)) @1)
2362 (if (INTEGRAL_TYPE_P (type)
2363 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
2364 && TREE_CODE (TREE_TYPE (@0)) != BOOLEAN_TYPE
2365 /* Sign extending of the neg or a truncation of the neg
2367 && (!TYPE_UNSIGNED (TREE_TYPE (@0))
2368 || TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))))
2369 (mult (convert @0) @1)))
2371 /* Narrow integer multiplication by a zero_one_valued_p operand.
2372 Multiplication by [0,1] is guaranteed not to overflow. */
2374 (convert (mult@0 zero_one_valued_p@1 INTEGER_CST@2))
2375 (if (INTEGRAL_TYPE_P (type)
2376 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
2377 && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (@0)))
2378 (mult (convert @1) (convert @2))))
2380 /* (X << C) != 0 can be simplified to X, when C is zero_one_valued_p.
2381 Check that the shift is well-defined (C is less than TYPE_PRECISION)
2382 as some targets (such as x86's SSE) may return zero for larger C. */
2384 (ne (lshift zero_one_valued_p@0 INTEGER_CST@1) integer_zerop@2)
2385 (if (tree_fits_shwi_p (@1)
2386 && tree_to_shwi (@1) > 0
2387 && tree_to_shwi (@1) < TYPE_PRECISION (TREE_TYPE (@0)))
2390 /* (X << C) == 0 can be simplified to X == 0, when C is zero_one_valued_p.
2391 Check that the shift is well-defined (C is less than TYPE_PRECISION)
2392 as some targets (such as x86's SSE) may return zero for larger C. */
2394 (eq (lshift zero_one_valued_p@0 INTEGER_CST@1) integer_zerop@2)
2395 (if (tree_fits_shwi_p (@1)
2396 && tree_to_shwi (@1) > 0
2397 && tree_to_shwi (@1) < TYPE_PRECISION (TREE_TYPE (@0)))
2400 /* Convert ~ (-A) to A - 1. */
2402 (bit_not (convert? (negate @0)))
2403 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
2404 || !TYPE_UNSIGNED (TREE_TYPE (@0)))
2405 (convert (minus @0 { build_each_one_cst (TREE_TYPE (@0)); }))))
2407 /* Convert - (~A) to A + 1. */
2409 (negate (nop_convert? (bit_not @0)))
2410 (plus (view_convert @0) { build_each_one_cst (type); }))
2412 /* (a & b) ^ (a == b) -> !(a | b) */
2413 /* (a & b) == (a ^ b) -> !(a | b) */
2414 (for first_op (bit_xor eq)
2415 second_op (eq bit_xor)
2417 (first_op:c (bit_and:c truth_valued_p@0 truth_valued_p@1) (second_op:c @0 @1))
2418 (bit_not (bit_ior @0 @1))))
2420 /* Convert ~ (A - 1) or ~ (A + -1) to -A. */
2422 (bit_not (convert? (minus @0 integer_each_onep)))
2423 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
2424 || !TYPE_UNSIGNED (TREE_TYPE (@0)))
2425 (convert (negate @0))))
2427 (bit_not (convert? (plus @0 integer_all_onesp)))
2428 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
2429 || !TYPE_UNSIGNED (TREE_TYPE (@0)))
2430 (convert (negate @0))))
2432 /* Part of convert ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify. */
2434 (bit_not (convert? (bit_xor @0 INTEGER_CST@1)))
2435 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2436 (convert (bit_xor @0 (bit_not @1)))))
2438 (bit_not (convert? (bit_xor:c (bit_not @0) @1)))
2439 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2440 (convert (bit_xor @0 @1))))
2442 /* Otherwise prefer ~(X ^ Y) to ~X ^ Y as more canonical. */
2444 (bit_xor:c (nop_convert?:s (bit_not:s @0)) @1)
2445 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2446 (bit_not (bit_xor (view_convert @0) @1))))
2448 /* ~(a ^ b) is a == b for truth valued a and b. */
2450 (bit_not (bit_xor:s truth_valued_p@0 truth_valued_p@1))
2451 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2452 && TYPE_PRECISION (TREE_TYPE (@0)) == 1)
2453 (convert (eq @0 @1))))
2455 /* (~a) == b is a ^ b for truth valued a and b. */
2457 (eq:c (bit_not:s truth_valued_p@0) truth_valued_p@1)
2458 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2459 && TYPE_PRECISION (TREE_TYPE (@0)) == 1)
2460 (convert (bit_xor @0 @1))))
2462 /* (x & ~m) | (y & m) -> ((x ^ y) & m) ^ x */
2464 (bit_ior:c (bit_and:cs @0 (bit_not @2)) (bit_and:cs @1 @2))
2465 (bit_xor (bit_and (bit_xor @0 @1) @2) @0))
2467 /* Fold A - (A & B) into ~B & A. */
2469 (minus (convert1? @0) (convert2?:s (bit_and:cs @@0 @1)))
2470 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
2471 && tree_nop_conversion_p (type, TREE_TYPE (@1)))
2472 (convert (bit_and (bit_not @1) @0))))
2474 /* (m1 CMP m2) * d -> (m1 CMP m2) ? d : 0 */
2475 (if (!canonicalize_math_p ())
2476 (for cmp (tcc_comparison)
2478 (mult:c (convert (cmp@0 @1 @2)) @3)
2479 (if (INTEGRAL_TYPE_P (type)
2480 && INTEGRAL_TYPE_P (TREE_TYPE (@0)))
2481 (cond @0 @3 { build_zero_cst (type); })))
2482 /* (-(m1 CMP m2)) & d -> (m1 CMP m2) ? d : 0 */
2484 (bit_and:c (negate (convert (cmp@0 @1 @2))) @3)
2485 (if (INTEGRAL_TYPE_P (type)
2486 && INTEGRAL_TYPE_P (TREE_TYPE (@0)))
2487 (cond @0 @3 { build_zero_cst (type); })))
2491 /* For integral types with undefined overflow and C != 0 fold
2492 x * C EQ/NE y * C into x EQ/NE y. */
2495 (cmp (mult:c @0 @1) (mult:c @2 @1))
2496 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2497 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2498 && tree_expr_nonzero_p (@1))
2501 /* For integral types with wrapping overflow and C odd fold
2502 x * C EQ/NE y * C into x EQ/NE y. */
2505 (cmp (mult @0 INTEGER_CST@1) (mult @2 @1))
2506 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2507 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))
2508 && (TREE_INT_CST_LOW (@1) & 1) != 0)
2511 /* For integral types with undefined overflow and C != 0 fold
2512 x * C RELOP y * C into:
2514 x RELOP y for nonnegative C
2515 y RELOP x for negative C */
2516 (for cmp (lt gt le ge)
2518 (cmp (mult:c @0 @1) (mult:c @2 @1))
2519 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2520 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
2521 (if (tree_expr_nonnegative_p (@1) && tree_expr_nonzero_p (@1))
2523 (if (TREE_CODE (@1) == INTEGER_CST
2524 && wi::neg_p (wi::to_wide (@1), TYPE_SIGN (TREE_TYPE (@1))))
2527 /* (X - 1U) <= INT_MAX-1U into (int) X > 0. */
2531 (cmp (plus @0 integer_minus_onep@1) INTEGER_CST@2)
2532 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2533 && TYPE_UNSIGNED (TREE_TYPE (@0))
2534 && TYPE_PRECISION (TREE_TYPE (@0)) > 1
2535 && (wi::to_wide (@2)
2536 == wi::max_value (TYPE_PRECISION (TREE_TYPE (@0)), SIGNED) - 1))
2537 (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
2538 (icmp (convert:stype @0) { build_int_cst (stype, 0); })))))
2540 /* X / 4 < Y / 4 iff X < Y when the division is known to be exact. */
2541 (for cmp (simple_comparison)
2543 (cmp (convert?@3 (exact_div @0 INTEGER_CST@2)) (convert? (exact_div @1 @2)))
2544 (if (element_precision (@3) >= element_precision (@0)
2545 && types_match (@0, @1))
2546 (if (wi::lt_p (wi::to_wide (@2), 0, TYPE_SIGN (TREE_TYPE (@2))))
2547 (if (!TYPE_UNSIGNED (TREE_TYPE (@3)))
2549 (if (tree_expr_nonzero_p (@0) && tree_expr_nonzero_p (@1))
2552 tree utype = unsigned_type_for (TREE_TYPE (@0));
2554 (cmp (convert:utype @1) (convert:utype @0)))))
2555 (if (wi::gt_p (wi::to_wide (@2), 1, TYPE_SIGN (TREE_TYPE (@2))))
2556 (if (TYPE_UNSIGNED (TREE_TYPE (@0)) || !TYPE_UNSIGNED (TREE_TYPE (@3)))
2560 tree utype = unsigned_type_for (TREE_TYPE (@0));
2562 (cmp (convert:utype @0) (convert:utype @1)))))))))
2564 /* X / C1 op C2 into a simple range test. */
2565 (for cmp (simple_comparison)
2567 (cmp (trunc_div:s @0 INTEGER_CST@1) INTEGER_CST@2)
2568 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2569 && integer_nonzerop (@1)
2570 && !TREE_OVERFLOW (@1)
2571 && !TREE_OVERFLOW (@2))
2572 (with { tree lo, hi; bool neg_overflow;
2573 enum tree_code code = fold_div_compare (cmp, @1, @2, &lo, &hi,
2576 (if (code == LT_EXPR || code == GE_EXPR)
2577 (if (TREE_OVERFLOW (lo))
2578 { build_int_cst (type, (code == LT_EXPR) ^ neg_overflow); }
2579 (if (code == LT_EXPR)
2582 (if (code == LE_EXPR || code == GT_EXPR)
2583 (if (TREE_OVERFLOW (hi))
2584 { build_int_cst (type, (code == LE_EXPR) ^ neg_overflow); }
2585 (if (code == LE_EXPR)
2589 { build_int_cst (type, code == NE_EXPR); })
2590 (if (code == EQ_EXPR && !hi)
2592 (if (code == EQ_EXPR && !lo)
2594 (if (code == NE_EXPR && !hi)
2596 (if (code == NE_EXPR && !lo)
2599 { build_range_check (UNKNOWN_LOCATION, type, @0, code == EQ_EXPR,
2603 tree etype = range_check_type (TREE_TYPE (@0));
2606 hi = fold_convert (etype, hi);
2607 lo = fold_convert (etype, lo);
2608 hi = const_binop (MINUS_EXPR, etype, hi, lo);
2611 (if (etype && hi && !TREE_OVERFLOW (hi))
2612 (if (code == EQ_EXPR)
2613 (le (minus (convert:etype @0) { lo; }) { hi; })
2614 (gt (minus (convert:etype @0) { lo; }) { hi; })))))))))
2616 /* X + Z < Y + Z is the same as X < Y when there is no overflow. */
2617 (for op (lt le ge gt)
2619 (op (plus:c @0 @2) (plus:c @1 @2))
2620 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2621 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
2624 /* As a special case, X + C < Y + C is the same as (signed) X < (signed) Y
2625 when C is an unsigned integer constant with only the MSB set, and X and
2626 Y have types of equal or lower integer conversion rank than C's. */
2627 (for op (lt le ge gt)
2629 (op (plus @1 INTEGER_CST@0) (plus @2 @0))
2630 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2631 && TYPE_UNSIGNED (TREE_TYPE (@0))
2632 && wi::only_sign_bit_p (wi::to_wide (@0)))
2633 (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
2634 (op (convert:stype @1) (convert:stype @2))))))
2636 /* For equality and subtraction, this is also true with wrapping overflow. */
2637 (for op (eq ne minus)
2639 (op (plus:c @0 @2) (plus:c @1 @2))
2640 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2641 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2642 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
2644 /* And similar for pointers. */
2647 (op (pointer_plus @0 @1) (pointer_plus @0 @2))
2650 (pointer_diff (pointer_plus @0 @1) (pointer_plus @0 @2))
2651 (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
2652 (convert (minus @1 @2))))
2654 /* X - Z < Y - Z is the same as X < Y when there is no overflow. */
2655 (for op (lt le ge gt)
2657 (op (minus @0 @2) (minus @1 @2))
2658 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2659 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
2661 /* For equality and subtraction, this is also true with wrapping overflow. */
2662 (for op (eq ne minus)
2664 (op (minus @0 @2) (minus @1 @2))
2665 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2666 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2667 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
2669 /* And for pointers... */
2670 (for op (simple_comparison)
2672 (op (pointer_diff@3 @0 @2) (pointer_diff @1 @2))
2673 (if (!TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
2676 (minus (pointer_diff@3 @0 @2) (pointer_diff @1 @2))
2677 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@3))
2678 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
2679 (pointer_diff @0 @1)))
2681 /* Z - X < Z - Y is the same as Y < X when there is no overflow. */
2682 (for op (lt le ge gt)
2684 (op (minus @2 @0) (minus @2 @1))
2685 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2686 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
2688 /* For equality and subtraction, this is also true with wrapping overflow. */
2689 (for op (eq ne minus)
2691 (op (minus @2 @0) (minus @2 @1))
2692 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2693 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2694 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
2696 /* And for pointers... */
2697 (for op (simple_comparison)
2699 (op (pointer_diff@3 @2 @0) (pointer_diff @2 @1))
2700 (if (!TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
2703 (minus (pointer_diff@3 @2 @0) (pointer_diff @2 @1))
2704 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@3))
2705 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
2706 (pointer_diff @1 @0)))
2708 /* X + Y < Y is the same as X < 0 when there is no overflow. */
2709 (for op (lt le gt ge)
2711 (op:c (plus:c@2 @0 @1) @1)
2712 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2713 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2714 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0))
2715 && (CONSTANT_CLASS_P (@0) || single_use (@2)))
2716 (op @0 { build_zero_cst (TREE_TYPE (@0)); }))))
2717 /* For equality, this is also true with wrapping overflow. */
2720 (op:c (nop_convert?@3 (plus:c@2 @0 (convert1? @1))) (convert2? @1))
2721 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2722 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2723 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
2724 && (CONSTANT_CLASS_P (@0) || (single_use (@2) && single_use (@3)))
2725 && tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@2))
2726 && tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@1)))
2727 (op @0 { build_zero_cst (TREE_TYPE (@0)); })))
2729 (op:c (nop_convert?@3 (pointer_plus@2 (convert1? @0) @1)) (convert2? @0))
2730 (if (tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@0))
2731 && tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0))
2732 && (CONSTANT_CLASS_P (@1) || (single_use (@2) && single_use (@3))))
2733 (op @1 { build_zero_cst (TREE_TYPE (@1)); }))))
2735 /* (&a + b) !=/== (&a[1] + c) -> (&a[0] - &a[1]) + b !=/== c */
2738 (neeq:c ADDR_EXPR@0 (pointer_plus @2 @3))
2739 (with { poly_int64 diff; tree inner_type = TREE_TYPE (@3);}
2740 (if (ptr_difference_const (@0, @2, &diff))
2741 (neeq { build_int_cst_type (inner_type, diff); } @3))))
2743 (neeq (pointer_plus ADDR_EXPR@0 @1) (pointer_plus ADDR_EXPR@2 @3))
2744 (with { poly_int64 diff; tree inner_type = TREE_TYPE (@1);}
2745 (if (ptr_difference_const (@0, @2, &diff))
2746 (neeq (plus { build_int_cst_type (inner_type, diff); } @1) @3)))))
2748 /* X - Y < X is the same as Y > 0 when there is no overflow.
2749 For equality, this is also true with wrapping overflow. */
2750 (for op (simple_comparison)
2752 (op:c @0 (minus@2 @0 @1))
2753 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2754 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2755 || ((op == EQ_EXPR || op == NE_EXPR)
2756 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
2757 && (CONSTANT_CLASS_P (@1) || single_use (@2)))
2758 (op @1 { build_zero_cst (TREE_TYPE (@1)); }))))
2761 (X / Y) == 0 -> X < Y if X, Y are unsigned.
2762 (X / Y) != 0 -> X >= Y, if X, Y are unsigned. */
2766 (cmp (trunc_div @0 @1) integer_zerop)
2767 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
2768 /* Complex ==/!= is allowed, but not </>=. */
2769 && TREE_CODE (TREE_TYPE (@0)) != COMPLEX_TYPE
2770 && (VECTOR_TYPE_P (type) || !VECTOR_TYPE_P (TREE_TYPE (@0))))
2773 /* X == C - X can never be true if C is odd. */
2776 (cmp:c (convert? @0) (convert1? (minus INTEGER_CST@1 (convert2? @0))))
2777 (if (TREE_INT_CST_LOW (@1) & 1)
2778 { constant_boolean_node (cmp == NE_EXPR, type); })))
2783 U needs to be non-negative.
2787 U and N needs to be non-negative
2791 U needs to be non-negative and N needs to be a negative constant.
2793 (for cmp (lt ge le gt )
2794 bitop (bit_ior bit_ior bit_and bit_and)
2796 (cmp:c (bitop:c tree_expr_nonnegative_p@0 @1) @0)
2797 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)))
2798 (if (bitop == BIT_AND_EXPR || tree_expr_nonnegative_p (@1))
2799 { constant_boolean_node (cmp == GE_EXPR || cmp == LE_EXPR, type); }
2800 /* The sign is opposite now so the comparison is swapped around. */
2801 (if (TREE_CODE (@1) == INTEGER_CST && wi::neg_p (wi::to_wide (@1)))
2802 { constant_boolean_node (cmp == LT_EXPR, type); })))))
2804 /* Arguments on which one can call get_nonzero_bits to get the bits
2806 (match with_possible_nonzero_bits
2808 (match with_possible_nonzero_bits
2810 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))))
2811 /* Slightly extended version, do not make it recursive to keep it cheap. */
2812 (match (with_possible_nonzero_bits2 @0)
2813 with_possible_nonzero_bits@0)
2814 (match (with_possible_nonzero_bits2 @0)
2815 (bit_and:c with_possible_nonzero_bits@0 @2))
2817 /* Same for bits that are known to be set, but we do not have
2818 an equivalent to get_nonzero_bits yet. */
2819 (match (with_certain_nonzero_bits2 @0)
2821 (match (with_certain_nonzero_bits2 @0)
2822 (bit_ior @1 INTEGER_CST@0))
2824 /* X == C (or X & Z == Y | C) is impossible if ~nonzero(X) & C != 0. */
2827 (cmp:c (with_possible_nonzero_bits2 @0) (with_certain_nonzero_bits2 @1))
2828 (if (wi::bit_and_not (wi::to_wide (@1), get_nonzero_bits (@0)) != 0)
2829 { constant_boolean_node (cmp == NE_EXPR, type); })))
2831 /* ((X inner_op C0) outer_op C1)
2832 With X being a tree where value_range has reasoned certain bits to always be
2833 zero throughout its computed value range,
2834 inner_op = {|,^}, outer_op = {|,^} and inner_op != outer_op
2835 where zero_mask has 1's for all bits that are sure to be 0 in
2837 if (inner_op == '^') C0 &= ~C1;
2838 if ((C0 & ~zero_mask) == 0) then emit (X outer_op (C0 outer_op C1)
2839 if ((C1 & ~zero_mask) == 0) then emit (X inner_op (C0 outer_op C1)
2841 (for inner_op (bit_ior bit_xor)
2842 outer_op (bit_xor bit_ior)
2845 (inner_op:s @2 INTEGER_CST@0) INTEGER_CST@1)
2849 wide_int zero_mask_not;
2853 if (TREE_CODE (@2) == SSA_NAME)
2854 zero_mask_not = get_nonzero_bits (@2);
2858 if (inner_op == BIT_XOR_EXPR)
2860 C0 = wi::bit_and_not (wi::to_wide (@0), wi::to_wide (@1));
2861 cst_emit = C0 | wi::to_wide (@1);
2865 C0 = wi::to_wide (@0);
2866 cst_emit = C0 ^ wi::to_wide (@1);
2869 (if (!fail && (C0 & zero_mask_not) == 0)
2870 (outer_op @2 { wide_int_to_tree (type, cst_emit); })
2871 (if (!fail && (wi::to_wide (@1) & zero_mask_not) == 0)
2872 (inner_op @2 { wide_int_to_tree (type, cst_emit); }))))))
2874 /* Associate (p +p off1) +p off2 as (p +p (off1 + off2)). */
2876 (pointer_plus (pointer_plus:s @0 @1) @3)
2877 (pointer_plus @0 (plus @1 @3)))
2880 (pointer_plus (convert:s (pointer_plus:s @0 @1)) @3)
2881 (convert:type (pointer_plus @0 (plus @1 @3))))
2888 tem4 = (unsigned long) tem3;
2893 (pointer_plus @0 (convert?@2 (minus@3 (convert @1) (convert @0))))
2894 /* Conditionally look through a sign-changing conversion. */
2895 (if (TYPE_PRECISION (TREE_TYPE (@2)) == TYPE_PRECISION (TREE_TYPE (@3))
2896 && ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@1)))
2897 || (GENERIC && type == TREE_TYPE (@1))))
2900 (pointer_plus @0 (convert?@2 (pointer_diff@3 @1 @@0)))
2901 (if (TYPE_PRECISION (TREE_TYPE (@2)) >= TYPE_PRECISION (TREE_TYPE (@3)))
2905 tem = (sizetype) ptr;
2909 and produce the simpler and easier to analyze with respect to alignment
2910 ... = ptr & ~algn; */
2912 (pointer_plus @0 (negate (bit_and (convert @0) INTEGER_CST@1)))
2913 (with { tree algn = wide_int_to_tree (TREE_TYPE (@0), ~wi::to_wide (@1)); }
2914 (bit_and @0 { algn; })))
2916 /* Try folding difference of addresses. */
2918 (minus (convert ADDR_EXPR@0) (convert (pointer_plus @1 @2)))
2919 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2920 (with { poly_int64 diff; }
2921 (if (ptr_difference_const (@0, @1, &diff))
2922 (minus { build_int_cst_type (type, diff); } (convert @2))))))
2924 (minus (convert (pointer_plus @0 @2)) (convert ADDR_EXPR@1))
2925 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2926 (with { poly_int64 diff; }
2927 (if (ptr_difference_const (@0, @1, &diff))
2928 (plus (convert @2) { build_int_cst_type (type, diff); })))))
2930 (minus (convert ADDR_EXPR@0) (convert @1))
2931 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2932 (with { poly_int64 diff; }
2933 (if (ptr_difference_const (@0, @1, &diff))
2934 { build_int_cst_type (type, diff); }))))
2936 (minus (convert @0) (convert ADDR_EXPR@1))
2937 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2938 (with { poly_int64 diff; }
2939 (if (ptr_difference_const (@0, @1, &diff))
2940 { build_int_cst_type (type, diff); }))))
2942 (pointer_diff (convert?@2 ADDR_EXPR@0) (convert1?@3 @1))
2943 (if (tree_nop_conversion_p (TREE_TYPE(@2), TREE_TYPE (@0))
2944 && tree_nop_conversion_p (TREE_TYPE(@3), TREE_TYPE (@1)))
2945 (with { poly_int64 diff; }
2946 (if (ptr_difference_const (@0, @1, &diff))
2947 { build_int_cst_type (type, diff); }))))
2949 (pointer_diff (convert?@2 @0) (convert1?@3 ADDR_EXPR@1))
2950 (if (tree_nop_conversion_p (TREE_TYPE(@2), TREE_TYPE (@0))
2951 && tree_nop_conversion_p (TREE_TYPE(@3), TREE_TYPE (@1)))
2952 (with { poly_int64 diff; }
2953 (if (ptr_difference_const (@0, @1, &diff))
2954 { build_int_cst_type (type, diff); }))))
2956 /* (&a+b) - (&a[1] + c) -> sizeof(a[0]) + (b - c) */
2958 (pointer_diff (pointer_plus ADDR_EXPR@0 @1) (pointer_plus ADDR_EXPR@2 @3))
2959 (with { poly_int64 diff; }
2960 (if (ptr_difference_const (@0, @2, &diff))
2961 (plus { build_int_cst_type (type, diff); } (convert (minus @1 @3))))))
2962 /* (p + b) - &p->d -> offsetof (*p, d) + b */
2964 (pointer_diff (pointer_plus @0 @1) ADDR_EXPR@2)
2965 (with { poly_int64 diff; }
2966 (if (ptr_difference_const (@0, @2, &diff))
2967 (plus { build_int_cst_type (type, diff); } (convert @1)))))
2969 (pointer_diff ADDR_EXPR@0 (pointer_plus @1 @2))
2970 (with { poly_int64 diff; }
2971 (if (ptr_difference_const (@0, @1, &diff))
2972 (minus { build_int_cst_type (type, diff); } (convert @2)))))
2974 /* Canonicalize (T *)(ptr - ptr-cst) to &MEM[ptr + -ptr-cst]. */
2976 (convert (pointer_diff @0 INTEGER_CST@1))
2977 (if (POINTER_TYPE_P (type))
2978 { build_fold_addr_expr_with_type
2979 (build2 (MEM_REF, char_type_node, @0,
2980 wide_int_to_tree (ptr_type_node, wi::neg (wi::to_wide (@1)))),
2983 /* If arg0 is derived from the address of an object or function, we may
2984 be able to fold this expression using the object or function's
2987 (bit_and (convert? @0) INTEGER_CST@1)
2988 (if (POINTER_TYPE_P (TREE_TYPE (@0))
2989 && tree_nop_conversion_p (type, TREE_TYPE (@0)))
2993 unsigned HOST_WIDE_INT bitpos;
2994 get_pointer_alignment_1 (@0, &align, &bitpos);
2996 (if (wi::ltu_p (wi::to_wide (@1), align / BITS_PER_UNIT))
2997 { wide_int_to_tree (type, (wi::to_wide (@1)
2998 & (bitpos / BITS_PER_UNIT))); }))))
3001 uniform_integer_cst_p
3003 tree int_cst = uniform_integer_cst_p (t);
3004 tree inner_type = TREE_TYPE (int_cst);
3006 (if ((INTEGRAL_TYPE_P (inner_type)
3007 || POINTER_TYPE_P (inner_type))
3008 && wi::eq_p (wi::to_wide (int_cst), wi::min_value (inner_type))))))
3011 uniform_integer_cst_p
3013 tree int_cst = uniform_integer_cst_p (t);
3014 tree itype = TREE_TYPE (int_cst);
3016 (if ((INTEGRAL_TYPE_P (itype)
3017 || POINTER_TYPE_P (itype))
3018 && wi::eq_p (wi::to_wide (int_cst), wi::max_value (itype))))))
3020 /* x > y && x != XXX_MIN --> x > y
3021 x > y && x == XXX_MIN --> false . */
3024 (bit_and:c (gt:c@2 @0 @1) (eqne @0 min_value))
3026 (if (eqne == EQ_EXPR)
3027 { constant_boolean_node (false, type); })
3028 (if (eqne == NE_EXPR)
3032 /* x < y && x != XXX_MAX --> x < y
3033 x < y && x == XXX_MAX --> false. */
3036 (bit_and:c (lt:c@2 @0 @1) (eqne @0 max_value))
3038 (if (eqne == EQ_EXPR)
3039 { constant_boolean_node (false, type); })
3040 (if (eqne == NE_EXPR)
3044 /* x <= y && x == XXX_MIN --> x == XXX_MIN. */
3046 (bit_and:c (le:c @0 @1) (eq@2 @0 min_value))
3049 /* x >= y && x == XXX_MAX --> x == XXX_MAX. */
3051 (bit_and:c (ge:c @0 @1) (eq@2 @0 max_value))
3054 /* x > y || x != XXX_MIN --> x != XXX_MIN. */
3056 (bit_ior:c (gt:c @0 @1) (ne@2 @0 min_value))
3059 /* x <= y || x != XXX_MIN --> true. */
3061 (bit_ior:c (le:c @0 @1) (ne @0 min_value))
3062 { constant_boolean_node (true, type); })
3064 /* x <= y || x == XXX_MIN --> x <= y. */
3066 (bit_ior:c (le:c@2 @0 @1) (eq @0 min_value))
3069 /* x < y || x != XXX_MAX --> x != XXX_MAX. */
3071 (bit_ior:c (lt:c @0 @1) (ne@2 @0 max_value))
3074 /* x >= y || x != XXX_MAX --> true
3075 x >= y || x == XXX_MAX --> x >= y. */
3078 (bit_ior:c (ge:c@2 @0 @1) (eqne @0 max_value))
3080 (if (eqne == EQ_EXPR)
3082 (if (eqne == NE_EXPR)
3083 { constant_boolean_node (true, type); }))))
3085 /* y == XXX_MIN || x < y --> x <= y - 1 */
3087 (bit_ior:c (eq:s @1 min_value) (lt:cs @0 @1))
3088 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
3089 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
3090 (le @0 (minus @1 { build_int_cst (TREE_TYPE (@1), 1); }))))
3092 /* y != XXX_MIN && x >= y --> x > y - 1 */
3094 (bit_and:c (ne:s @1 min_value) (ge:cs @0 @1))
3095 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
3096 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
3097 (gt @0 (minus @1 { build_int_cst (TREE_TYPE (@1), 1); }))))
3099 /* Convert (X == CST1) && ((other)X OP2 CST2) to a known value
3100 based on CST1 OP2 CST2. Similarly for (X != CST1). */
3101 /* Convert (X == Y) && (X OP2 Y) to a known value if X is an integral type.
3102 Similarly for (X != Y). */
3105 (for code2 (eq ne lt gt le ge)
3107 (bit_and:c (code1:c@3 @0 @1) (code2:c@4 (convert?@c0 @0) @2))
3108 (if ((TREE_CODE (@1) == INTEGER_CST
3109 && TREE_CODE (@2) == INTEGER_CST)
3110 || ((INTEGRAL_TYPE_P (TREE_TYPE (@1))
3111 || POINTER_TYPE_P (TREE_TYPE (@1)))
3112 && bitwise_equal_p (@1, @2)))
3115 bool one_before = false;
3116 bool one_after = false;
3118 bool allbits = true;
3119 if (TREE_CODE (@1) == INTEGER_CST
3120 && TREE_CODE (@2) == INTEGER_CST)
3122 allbits = TYPE_PRECISION (TREE_TYPE (@1)) <= TYPE_PRECISION (TREE_TYPE (@2));
3123 auto t1 = wi::to_wide (fold_convert (TREE_TYPE (@2), @1));
3124 auto t2 = wi::to_wide (@2);
3125 cmp = wi::cmp (t1, t2, TYPE_SIGN (TREE_TYPE (@2)));
3136 case EQ_EXPR: val = (cmp == 0); break;
3137 case NE_EXPR: val = (cmp != 0); break;
3138 case LT_EXPR: val = (cmp < 0); break;
3139 case GT_EXPR: val = (cmp > 0); break;
3140 case LE_EXPR: val = (cmp <= 0); break;
3141 case GE_EXPR: val = (cmp >= 0); break;
3142 default: gcc_unreachable ();
3146 (if (code1 == EQ_EXPR && val) @3)
3147 (if (code1 == EQ_EXPR && !val) { constant_boolean_node (false, type); })
3148 (if (code1 == NE_EXPR && !val && allbits) @4)
3149 (if (code1 == NE_EXPR
3153 (gt @c0 (convert @1)))
3154 (if (code1 == NE_EXPR
3158 (lt @c0 (convert @1)))
3159 /* (a != (b+1)) & (a > b) -> a > (b+1) */
3160 (if (code1 == NE_EXPR
3164 (gt @c0 (convert @1)))
3165 /* (a != (b-1)) & (a < b) -> a < (b-1) */
3166 (if (code1 == NE_EXPR
3170 (lt @c0 (convert @1)))
3178 /* Convert (X OP1 CST1) && (X OP2 CST2).
3179 Convert (X OP1 Y) && (X OP2 Y). */
3181 (for code1 (lt le gt ge)
3182 (for code2 (lt le gt ge)
3184 (bit_and (code1:c@3 @0 @1) (code2:c@4 @0 @2))
3185 (if ((TREE_CODE (@1) == INTEGER_CST
3186 && TREE_CODE (@2) == INTEGER_CST)
3187 || ((INTEGRAL_TYPE_P (TREE_TYPE (@1))
3188 || POINTER_TYPE_P (TREE_TYPE (@1)))
3189 && operand_equal_p (@1, @2)))
3193 if (TREE_CODE (@1) == INTEGER_CST
3194 && TREE_CODE (@2) == INTEGER_CST)
3195 cmp = tree_int_cst_compare (@1, @2);
3198 /* Choose the more restrictive of two < or <= comparisons. */
3199 (if ((code1 == LT_EXPR || code1 == LE_EXPR)
3200 && (code2 == LT_EXPR || code2 == LE_EXPR))
3201 (if ((cmp < 0) || (cmp == 0 && code1 == LT_EXPR))
3204 /* Likewise chose the more restrictive of two > or >= comparisons. */
3205 (if ((code1 == GT_EXPR || code1 == GE_EXPR)
3206 && (code2 == GT_EXPR || code2 == GE_EXPR))
3207 (if ((cmp > 0) || (cmp == 0 && code1 == GT_EXPR))
3210 /* Check for singleton ranges. */
3212 && ((code1 == LE_EXPR && code2 == GE_EXPR)
3213 || (code1 == GE_EXPR && code2 == LE_EXPR)))
3215 /* Check for disjoint ranges. */
3217 && (code1 == LT_EXPR || code1 == LE_EXPR)
3218 && (code2 == GT_EXPR || code2 == GE_EXPR))
3219 { constant_boolean_node (false, type); })
3221 && (code1 == GT_EXPR || code1 == GE_EXPR)
3222 && (code2 == LT_EXPR || code2 == LE_EXPR))
3223 { constant_boolean_node (false, type); })
3226 /* Convert (X == CST1) || (X OP2 CST2) to a known value
3227 based on CST1 OP2 CST2. Similarly for (X != CST1). */
3228 /* Convert (X == Y) || (X OP2 Y) to a known value if X is an integral type.
3229 Similarly for (X != Y). */
3232 (for code2 (eq ne lt gt le ge)
3234 (bit_ior:c (code1:c@3 @0 @1) (code2:c@4 (convert?@c0 @0) @2))
3235 (if ((TREE_CODE (@1) == INTEGER_CST
3236 && TREE_CODE (@2) == INTEGER_CST)
3237 || ((INTEGRAL_TYPE_P (TREE_TYPE (@1))
3238 || POINTER_TYPE_P (TREE_TYPE (@1)))
3239 && bitwise_equal_p (@1, @2)))
3242 bool one_before = false;
3243 bool one_after = false;
3245 bool allbits = true;
3246 if (TREE_CODE (@1) == INTEGER_CST
3247 && TREE_CODE (@2) == INTEGER_CST)
3249 allbits = TYPE_PRECISION (TREE_TYPE (@1)) <= TYPE_PRECISION (TREE_TYPE (@2));
3250 auto t1 = wi::to_wide (fold_convert (TREE_TYPE (@2), @1));
3251 auto t2 = wi::to_wide (@2);
3252 cmp = wi::cmp (t1, t2, TYPE_SIGN (TREE_TYPE (@2)));
3263 case EQ_EXPR: val = (cmp == 0); break;
3264 case NE_EXPR: val = (cmp != 0); break;
3265 case LT_EXPR: val = (cmp < 0); break;
3266 case GT_EXPR: val = (cmp > 0); break;
3267 case LE_EXPR: val = (cmp <= 0); break;
3268 case GE_EXPR: val = (cmp >= 0); break;
3269 default: gcc_unreachable ();
3273 (if (code1 == EQ_EXPR && val) @4)
3274 (if (code1 == NE_EXPR && val && allbits) { constant_boolean_node (true, type); })
3275 (if (code1 == NE_EXPR && !val && allbits) @3)
3276 (if (code1 == EQ_EXPR
3281 (if (code1 == EQ_EXPR
3286 /* (a == (b-1)) | (a >= b) -> a >= (b-1) */
3287 (if (code1 == EQ_EXPR
3291 (ge @c0 (convert @1)))
3292 /* (a == (b+1)) | (a <= b) -> a <= (b-1) */
3293 (if (code1 == EQ_EXPR
3297 (le @c0 (convert @1)))
3305 /* Convert (X OP1 CST1) || (X OP2 CST2).
3306 Convert (X OP1 Y) || (X OP2 Y). */
3308 (for code1 (lt le gt ge)
3309 (for code2 (lt le gt ge)
3311 (bit_ior (code1@3 @0 @1) (code2@4 @0 @2))
3312 (if ((TREE_CODE (@1) == INTEGER_CST
3313 && TREE_CODE (@2) == INTEGER_CST)
3314 || ((INTEGRAL_TYPE_P (TREE_TYPE (@1))
3315 || POINTER_TYPE_P (TREE_TYPE (@1)))
3316 && operand_equal_p (@1, @2)))
3320 if (TREE_CODE (@1) == INTEGER_CST
3321 && TREE_CODE (@2) == INTEGER_CST)
3322 cmp = tree_int_cst_compare (@1, @2);
3325 /* Choose the more restrictive of two < or <= comparisons. */
3326 (if ((code1 == LT_EXPR || code1 == LE_EXPR)
3327 && (code2 == LT_EXPR || code2 == LE_EXPR))
3328 (if ((cmp < 0) || (cmp == 0 && code1 == LT_EXPR))
3331 /* Likewise chose the more restrictive of two > or >= comparisons. */
3332 (if ((code1 == GT_EXPR || code1 == GE_EXPR)
3333 && (code2 == GT_EXPR || code2 == GE_EXPR))
3334 (if ((cmp > 0) || (cmp == 0 && code1 == GT_EXPR))
3337 /* Check for singleton ranges. */
3339 && ((code1 == LT_EXPR && code2 == GT_EXPR)
3340 || (code1 == GT_EXPR && code2 == LT_EXPR)))
3342 /* Check for disjoint ranges. */
3344 && (code1 == LT_EXPR || code1 == LE_EXPR)
3345 && (code2 == GT_EXPR || code2 == GE_EXPR))
3346 { constant_boolean_node (true, type); })
3348 && (code1 == GT_EXPR || code1 == GE_EXPR)
3349 && (code2 == LT_EXPR || code2 == LE_EXPR))
3350 { constant_boolean_node (true, type); })
3353 /* Optimize (a CMP b) ^ (a CMP b) */
3354 /* Optimize (a CMP b) != (a CMP b) */
3355 (for op (bit_xor ne)
3356 (for cmp1 (lt lt lt le le le)
3357 cmp2 (gt eq ne ge eq ne)
3358 rcmp (ne le gt ne lt ge)
3360 (op:c (cmp1:c @0 @1) (cmp2:c @0 @1))
3361 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
3364 /* Optimize (a CMP b) == (a CMP b) */
3365 (for cmp1 (lt lt lt le le le)
3366 cmp2 (gt eq ne ge eq ne)
3367 rcmp (eq gt le eq ge lt)
3369 (eq:c (cmp1:c @0 @1) (cmp2:c @0 @1))
3370 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
3373 /* (type)([0,1]@a != 0) -> (type)a
3374 (type)([0,1]@a == 1) -> (type)a
3375 (type)([0,1]@a == 0) -> a ^ 1
3376 (type)([0,1]@a != 1) -> a ^ 1. */
3379 (convert (eqne zero_one_valued_p@0 INTEGER_CST@1))
3380 (if ((integer_zerop (@1) || integer_onep (@1)))
3381 (if ((eqne == EQ_EXPR) ^ integer_zerop (@1))
3383 /* Only do this if the types match as (type)(a == 0) is
3384 canonical form normally, while `a ^ 1` is canonical when
3385 there is no type change. */
3386 (if (types_match (type, TREE_TYPE (@0)))
3387 (bit_xor @0 { build_one_cst (type); } ))))))
3389 /* We can't reassociate at all for saturating types. */
3390 (if (!TYPE_SATURATING (type))
3392 /* Contract negates. */
3393 /* A + (-B) -> A - B */
3395 (plus:c @0 (convert? (negate @1)))
3396 /* Apply STRIP_NOPS on the negate. */
3397 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
3398 && !TYPE_OVERFLOW_SANITIZED (type))
3402 if (INTEGRAL_TYPE_P (type)
3403 && TYPE_OVERFLOW_WRAPS (type) != TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
3404 t1 = TYPE_OVERFLOW_WRAPS (type) ? type : TREE_TYPE (@1);
3406 (convert (minus (convert:t1 @0) (convert:t1 @1))))))
3407 /* A - (-B) -> A + B */
3409 (minus @0 (convert? (negate @1)))
3410 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
3411 && !TYPE_OVERFLOW_SANITIZED (type))
3415 if (INTEGRAL_TYPE_P (type)
3416 && TYPE_OVERFLOW_WRAPS (type) != TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
3417 t1 = TYPE_OVERFLOW_WRAPS (type) ? type : TREE_TYPE (@1);
3419 (convert (plus (convert:t1 @0) (convert:t1 @1))))))
3421 Sign-extension is ok except for INT_MIN, which thankfully cannot
3422 happen without overflow. */
3424 (negate (convert (negate @1)))
3425 (if (INTEGRAL_TYPE_P (type)
3426 && (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@1))
3427 || (!TYPE_UNSIGNED (TREE_TYPE (@1))
3428 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))))
3429 && !TYPE_OVERFLOW_SANITIZED (type)
3430 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@1)))
3433 (negate (convert negate_expr_p@1))
3434 (if (SCALAR_FLOAT_TYPE_P (type)
3435 && ((DECIMAL_FLOAT_TYPE_P (type)
3436 == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1))
3437 && TYPE_PRECISION (type) >= TYPE_PRECISION (TREE_TYPE (@1)))
3438 || !HONOR_SIGN_DEPENDENT_ROUNDING (type)))
3439 (convert (negate @1))))
3441 (negate (nop_convert? (negate @1)))
3442 (if (!TYPE_OVERFLOW_SANITIZED (type)
3443 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@1)))
3446 /* We can't reassociate floating-point unless -fassociative-math
3447 or fixed-point plus or minus because of saturation to +-Inf. */
3448 (if ((!FLOAT_TYPE_P (type) || flag_associative_math)
3449 && !FIXED_POINT_TYPE_P (type))
3451 /* Match patterns that allow contracting a plus-minus pair
3452 irrespective of overflow issues. */
3453 /* (A +- B) - A -> +- B */
3454 /* (A +- B) -+ B -> A */
3455 /* A - (A +- B) -> -+ B */
3456 /* A +- (B -+ A) -> +- B */
3458 (minus (nop_convert1? (plus:c (nop_convert2? @0) @1)) @0)
3461 (minus (nop_convert1? (minus (nop_convert2? @0) @1)) @0)
3462 (if (!ANY_INTEGRAL_TYPE_P (type)
3463 || TYPE_OVERFLOW_WRAPS (type))
3464 (negate (view_convert @1))
3465 (view_convert (negate @1))))
3467 (plus:c (nop_convert1? (minus @0 (nop_convert2? @1))) @1)
3470 (minus @0 (nop_convert1? (plus:c (nop_convert2? @0) @1)))
3471 (if (!ANY_INTEGRAL_TYPE_P (type)
3472 || TYPE_OVERFLOW_WRAPS (type))
3473 (negate (view_convert @1))
3474 (view_convert (negate @1))))
3476 (minus @0 (nop_convert1? (minus (nop_convert2? @0) @1)))
3478 /* (A +- B) + (C - A) -> C +- B */
3479 /* (A + B) - (A - C) -> B + C */
3480 /* More cases are handled with comparisons. */
3482 (plus:c (plus:c @0 @1) (minus @2 @0))
3485 (plus:c (minus @0 @1) (minus @2 @0))
3488 (plus:c (pointer_diff @0 @1) (pointer_diff @2 @0))
3489 (if (TYPE_OVERFLOW_UNDEFINED (type)
3490 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0)))
3491 (pointer_diff @2 @1)))
3493 (minus (plus:c @0 @1) (minus @0 @2))
3496 /* (A +- CST1) +- CST2 -> A + CST3
3497 Use view_convert because it is safe for vectors and equivalent for
3499 (for outer_op (plus minus)
3500 (for inner_op (plus minus)
3501 neg_inner_op (minus plus)
3503 (outer_op (nop_convert? (inner_op @0 CONSTANT_CLASS_P@1))
3505 /* If one of the types wraps, use that one. */
3506 (if (!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_WRAPS (type))
3507 /* If all 3 captures are CONSTANT_CLASS_P, punt, as we might recurse
3508 forever if something doesn't simplify into a constant. */
3509 (if (!CONSTANT_CLASS_P (@0))
3510 (if (outer_op == PLUS_EXPR)
3511 (plus (view_convert @0) (inner_op! @2 (view_convert @1)))
3512 (minus (view_convert @0) (neg_inner_op! @2 (view_convert @1)))))
3513 (if (!ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3514 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
3515 (if (outer_op == PLUS_EXPR)
3516 (view_convert (plus @0 (inner_op! (view_convert @2) @1)))
3517 (view_convert (minus @0 (neg_inner_op! (view_convert @2) @1))))
3518 /* If the constant operation overflows we cannot do the transform
3519 directly as we would introduce undefined overflow, for example
3520 with (a - 1) + INT_MIN. */
3521 (if (types_match (type, @0) && !TYPE_OVERFLOW_SANITIZED (type))
3522 (with { tree cst = const_binop (outer_op == inner_op
3523 ? PLUS_EXPR : MINUS_EXPR,
3526 (if (INTEGRAL_TYPE_P (type) && !TREE_OVERFLOW (cst))
3527 (inner_op @0 { cst; } )
3528 /* X+INT_MAX+1 is X-INT_MIN. */
3529 (if (INTEGRAL_TYPE_P (type)
3530 && wi::to_wide (cst) == wi::min_value (type))
3531 (neg_inner_op @0 { wide_int_to_tree (type, wi::to_wide (cst)); })
3532 /* Last resort, use some unsigned type. */
3533 (with { tree utype = unsigned_type_for (type); }
3535 (view_convert (inner_op
3536 (view_convert:utype @0)
3538 { TREE_OVERFLOW (cst)
3539 ? drop_tree_overflow (cst) : cst; })))))))))))))))
3541 /* (CST1 - A) +- CST2 -> CST3 - A */
3542 (for outer_op (plus minus)
3544 (outer_op (nop_convert? (minus CONSTANT_CLASS_P@1 @0)) CONSTANT_CLASS_P@2)
3545 /* If one of the types wraps, use that one. */
3546 (if (!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_WRAPS (type))
3547 /* If all 3 captures are CONSTANT_CLASS_P, punt, as we might recurse
3548 forever if something doesn't simplify into a constant. */
3549 (if (!CONSTANT_CLASS_P (@0))
3550 (minus (outer_op! (view_convert @1) @2) (view_convert @0)))
3551 (if (!ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3552 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
3553 (view_convert (minus (outer_op! @1 (view_convert @2)) @0))
3554 (if (types_match (type, @0) && !TYPE_OVERFLOW_SANITIZED (type))
3555 (with { tree cst = const_binop (outer_op, type, @1, @2); }
3556 (if (cst && !TREE_OVERFLOW (cst))
3557 (minus { cst; } @0))))))))
3559 /* CST1 - (CST2 - A) -> CST3 + A
3560 Use view_convert because it is safe for vectors and equivalent for
3563 (minus CONSTANT_CLASS_P@1 (nop_convert? (minus CONSTANT_CLASS_P@2 @0)))
3564 /* If one of the types wraps, use that one. */
3565 (if (!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_WRAPS (type))
3566 /* If all 3 captures are CONSTANT_CLASS_P, punt, as we might recurse
3567 forever if something doesn't simplify into a constant. */
3568 (if (!CONSTANT_CLASS_P (@0))
3569 (plus (view_convert @0) (minus! @1 (view_convert @2))))
3570 (if (!ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3571 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
3572 (view_convert (plus @0 (minus! (view_convert @1) @2)))
3573 (if (types_match (type, @0) && !TYPE_OVERFLOW_SANITIZED (type))
3574 (with { tree cst = const_binop (MINUS_EXPR, type, @1, @2); }
3575 (if (cst && !TREE_OVERFLOW (cst))
3576 (plus { cst; } @0)))))))
3578 /* ((T)(A)) + CST -> (T)(A + CST) */
3581 (plus (convert:s SSA_NAME@0) INTEGER_CST@1)
3582 (if (TREE_CODE (TREE_TYPE (@0)) == INTEGER_TYPE
3583 && TREE_CODE (type) == INTEGER_TYPE
3584 && TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (@0))
3585 && int_fits_type_p (@1, TREE_TYPE (@0)))
3586 /* Perform binary operation inside the cast if the constant fits
3587 and (A + CST)'s range does not overflow. */
3590 wi::overflow_type min_ovf = wi::OVF_OVERFLOW,
3591 max_ovf = wi::OVF_OVERFLOW;
3592 tree inner_type = TREE_TYPE (@0);
3595 = wide_int::from (wi::to_wide (@1), TYPE_PRECISION (inner_type),
3596 TYPE_SIGN (inner_type));
3599 if (get_global_range_query ()->range_of_expr (vr, @0)
3600 && !vr.varying_p () && !vr.undefined_p ())
3602 wide_int wmin0 = vr.lower_bound ();
3603 wide_int wmax0 = vr.upper_bound ();
3604 wi::add (wmin0, w1, TYPE_SIGN (inner_type), &min_ovf);
3605 wi::add (wmax0, w1, TYPE_SIGN (inner_type), &max_ovf);
3608 (if (min_ovf == wi::OVF_NONE && max_ovf == wi::OVF_NONE)
3609 (convert (plus @0 { wide_int_to_tree (TREE_TYPE (@0), w1); } )))
3613 /* ((T)(A + CST1)) + CST2 -> (T)(A) + (T)CST1 + CST2 */
3615 (for op (plus minus)
3617 (plus (convert:s (op:s @0 INTEGER_CST@1)) INTEGER_CST@2)
3618 (if (TREE_CODE (TREE_TYPE (@0)) == INTEGER_TYPE
3619 && TREE_CODE (type) == INTEGER_TYPE
3620 && TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (@0))
3621 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
3622 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0))
3623 && TYPE_OVERFLOW_WRAPS (type))
3624 (plus (convert @0) (op @2 (convert @1))))))
3627 /* (T)(A) +- (T)(B) -> (T)(A +- B) only when (A +- B) could be simplified
3628 to a simple value. */
3629 (for op (plus minus)
3631 (op (convert @0) (convert @1))
3632 (if (INTEGRAL_TYPE_P (type)
3633 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
3634 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))
3635 && types_match (TREE_TYPE (@0), TREE_TYPE (@1))
3636 && !TYPE_OVERFLOW_TRAPS (type)
3637 && !TYPE_OVERFLOW_SANITIZED (type))
3638 (convert (op! @0 @1)))))
3642 (plus:c (convert? (bit_not @0)) (convert? @0))
3643 (if (!TYPE_OVERFLOW_TRAPS (type))
3644 (convert { build_all_ones_cst (TREE_TYPE (@0)); })))
3648 (plus (convert? (bit_not @0)) integer_each_onep)
3649 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
3650 (negate (convert @0))))
3654 (minus (convert? (negate @0)) integer_each_onep)
3655 (if (!TYPE_OVERFLOW_TRAPS (type)
3656 && TREE_CODE (type) != COMPLEX_TYPE
3657 && tree_nop_conversion_p (type, TREE_TYPE (@0)))
3658 (bit_not (convert @0))))
3662 (minus integer_all_onesp @0)
3663 (if (TREE_CODE (type) != COMPLEX_TYPE)
3666 /* (T)(P + A) - (T)P -> (T) A */
3668 (minus (convert (plus:c @@0 @1))
3670 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
3671 /* For integer types, if A has a smaller type
3672 than T the result depends on the possible
3674 E.g. T=size_t, A=(unsigned)429497295, P>0.
3675 However, if an overflow in P + A would cause
3676 undefined behavior, we can assume that there
3678 || (INTEGRAL_TYPE_P (TREE_TYPE (@1))
3679 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))))
3682 (minus (convert (pointer_plus @@0 @1))
3684 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
3685 /* For pointer types, if the conversion of A to the
3686 final type requires a sign- or zero-extension,
3687 then we have to punt - it is not defined which
3689 || (POINTER_TYPE_P (TREE_TYPE (@0))
3690 && TREE_CODE (@1) == INTEGER_CST
3691 && tree_int_cst_sign_bit (@1) == 0))
3694 (pointer_diff (pointer_plus @@0 @1) @0)
3695 /* The second argument of pointer_plus must be interpreted as signed, and
3696 thus sign-extended if necessary. */
3697 (with { tree stype = signed_type_for (TREE_TYPE (@1)); }
3698 /* Use view_convert instead of convert here, as POINTER_PLUS_EXPR
3699 second arg is unsigned even when we need to consider it as signed,
3700 we don't want to diagnose overflow here. */
3701 (convert (view_convert:stype @1))))
3703 /* (T)P - (T)(P + A) -> -(T) A */
3705 (minus (convert? @0)
3706 (convert (plus:c @@0 @1)))
3707 (if (INTEGRAL_TYPE_P (type)
3708 && TYPE_OVERFLOW_UNDEFINED (type)
3709 /* For integer literals, using an intermediate unsigned type to avoid
3710 an overflow at run time is counter-productive because it introduces
3711 spurious overflows at compile time, in the form of TREE_OVERFLOW on
3712 the result, which may be problematic in GENERIC for some front-ends:
3713 (T)P - (T)(P + 4) -> (T)(-(U)4) -> (T)(4294967292) -> -4(OVF)
3714 so we use the direct path for them. */
3715 && TREE_CODE (@1) != INTEGER_CST
3716 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
3717 (with { tree utype = unsigned_type_for (type); }
3718 (convert (negate (convert:utype @1))))
3719 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
3720 /* For integer types, if A has a smaller type
3721 than T the result depends on the possible
3723 E.g. T=size_t, A=(unsigned)429497295, P>0.
3724 However, if an overflow in P + A would cause
3725 undefined behavior, we can assume that there
3727 || (INTEGRAL_TYPE_P (TREE_TYPE (@1))
3728 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))))
3729 (negate (convert @1)))))
3732 (convert (pointer_plus @@0 @1)))
3733 (if (INTEGRAL_TYPE_P (type)
3734 && TYPE_OVERFLOW_UNDEFINED (type)
3735 /* See above the rationale for this condition. */
3736 && TREE_CODE (@1) != INTEGER_CST
3737 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
3738 (with { tree utype = unsigned_type_for (type); }
3739 (convert (negate (convert:utype @1))))
3740 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
3741 /* For pointer types, if the conversion of A to the
3742 final type requires a sign- or zero-extension,
3743 then we have to punt - it is not defined which
3745 || (POINTER_TYPE_P (TREE_TYPE (@0))
3746 && TREE_CODE (@1) == INTEGER_CST
3747 && tree_int_cst_sign_bit (@1) == 0))
3748 (negate (convert @1)))))
3750 (pointer_diff @0 (pointer_plus @@0 @1))
3751 /* The second argument of pointer_plus must be interpreted as signed, and
3752 thus sign-extended if necessary. */
3753 (with { tree stype = signed_type_for (TREE_TYPE (@1)); }
3754 /* Use view_convert instead of convert here, as POINTER_PLUS_EXPR
3755 second arg is unsigned even when we need to consider it as signed,
3756 we don't want to diagnose overflow here. */
3757 (negate (convert (view_convert:stype @1)))))
3759 /* (T)(P + A) - (T)(P + B) -> (T)A - (T)B */
3761 (minus (convert (plus:c @@0 @1))
3762 (convert (plus:c @0 @2)))
3763 (if (INTEGRAL_TYPE_P (type)
3764 && TYPE_OVERFLOW_UNDEFINED (type)
3765 && element_precision (type) <= element_precision (TREE_TYPE (@1))
3766 && element_precision (type) <= element_precision (TREE_TYPE (@2)))
3767 (with { tree utype = unsigned_type_for (type); }
3768 (convert (minus (convert:utype @1) (convert:utype @2))))
3769 (if (((element_precision (type) <= element_precision (TREE_TYPE (@1)))
3770 == (element_precision (type) <= element_precision (TREE_TYPE (@2))))
3771 && (element_precision (type) <= element_precision (TREE_TYPE (@1))
3772 /* For integer types, if A has a smaller type
3773 than T the result depends on the possible
3775 E.g. T=size_t, A=(unsigned)429497295, P>0.
3776 However, if an overflow in P + A would cause
3777 undefined behavior, we can assume that there
3779 || (INTEGRAL_TYPE_P (TREE_TYPE (@1))
3780 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
3781 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))
3782 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@2)))))
3783 (minus (convert @1) (convert @2)))))
3785 (minus (convert (pointer_plus @@0 @1))
3786 (convert (pointer_plus @0 @2)))
3787 (if (INTEGRAL_TYPE_P (type)
3788 && TYPE_OVERFLOW_UNDEFINED (type)
3789 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
3790 (with { tree utype = unsigned_type_for (type); }
3791 (convert (minus (convert:utype @1) (convert:utype @2))))
3792 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
3793 /* For pointer types, if the conversion of A to the
3794 final type requires a sign- or zero-extension,
3795 then we have to punt - it is not defined which
3797 || (POINTER_TYPE_P (TREE_TYPE (@0))
3798 && TREE_CODE (@1) == INTEGER_CST
3799 && tree_int_cst_sign_bit (@1) == 0
3800 && TREE_CODE (@2) == INTEGER_CST
3801 && tree_int_cst_sign_bit (@2) == 0))
3802 (minus (convert @1) (convert @2)))))
3804 (pointer_diff (pointer_plus @0 @2) (pointer_plus @1 @2))
3805 (pointer_diff @0 @1))
3807 (pointer_diff (pointer_plus @@0 @1) (pointer_plus @0 @2))
3808 /* The second argument of pointer_plus must be interpreted as signed, and
3809 thus sign-extended if necessary. */
3810 (with { tree stype = signed_type_for (TREE_TYPE (@1)); }
3811 /* Use view_convert instead of convert here, as POINTER_PLUS_EXPR
3812 second arg is unsigned even when we need to consider it as signed,
3813 we don't want to diagnose overflow here. */
3814 (minus (convert (view_convert:stype @1))
3815 (convert (view_convert:stype @2)))))))
3817 /* (A * C) +- (B * C) -> (A+-B) * C and (A * C) +- A -> A * (C+-1).
3818 Modeled after fold_plusminus_mult_expr. */
3819 (if (!TYPE_SATURATING (type)
3820 && (!FLOAT_TYPE_P (type) || flag_associative_math))
3821 (for plusminus (plus minus)
3823 (plusminus (mult:cs@3 @0 @1) (mult:cs@4 @0 @2))
3824 (if (!ANY_INTEGRAL_TYPE_P (type)
3825 || TYPE_OVERFLOW_WRAPS (type)
3826 || (INTEGRAL_TYPE_P (type)
3827 && tree_expr_nonzero_p (@0)
3828 && expr_not_equal_to (@0, wi::minus_one (TYPE_PRECISION (type)))))
3829 (if (single_use (@3) || single_use (@4))
3830 /* If @1 +- @2 is constant require a hard single-use on either
3831 original operand (but not on both). */
3832 (mult (plusminus @1 @2) @0)
3833 (mult! (plusminus @1 @2) @0)
3835 /* We cannot generate constant 1 for fract. */
3836 (if (!ALL_FRACT_MODE_P (TYPE_MODE (type)))
3838 (plusminus @0 (mult:c@3 @0 @2))
3839 (if ((!ANY_INTEGRAL_TYPE_P (type)
3840 || TYPE_OVERFLOW_WRAPS (type)
3841 /* For @0 + @0*@2 this transformation would introduce UB
3842 (where there was none before) for @0 in [-1,0] and @2 max.
3843 For @0 - @0*@2 this transformation would introduce UB
3844 for @0 0 and @2 in [min,min+1] or @0 -1 and @2 min+1. */
3845 || (INTEGRAL_TYPE_P (type)
3846 && ((tree_expr_nonzero_p (@0)
3847 && expr_not_equal_to (@0,
3848 wi::minus_one (TYPE_PRECISION (type))))
3849 || (plusminus == PLUS_EXPR
3850 ? expr_not_equal_to (@2,
3851 wi::max_value (TYPE_PRECISION (type), SIGNED))
3852 /* Let's ignore the @0 -1 and @2 min case. */
3853 : (expr_not_equal_to (@2,
3854 wi::min_value (TYPE_PRECISION (type), SIGNED))
3855 && expr_not_equal_to (@2,
3856 wi::min_value (TYPE_PRECISION (type), SIGNED)
3859 (mult (plusminus { build_one_cst (type); } @2) @0)))
3861 (plusminus (mult:c@3 @0 @2) @0)
3862 (if ((!ANY_INTEGRAL_TYPE_P (type)
3863 || TYPE_OVERFLOW_WRAPS (type)
3864 /* For @0*@2 + @0 this transformation would introduce UB
3865 (where there was none before) for @0 in [-1,0] and @2 max.
3866 For @0*@2 - @0 this transformation would introduce UB
3867 for @0 0 and @2 min. */
3868 || (INTEGRAL_TYPE_P (type)
3869 && ((tree_expr_nonzero_p (@0)
3870 && (plusminus == MINUS_EXPR
3871 || expr_not_equal_to (@0,
3872 wi::minus_one (TYPE_PRECISION (type)))))
3873 || expr_not_equal_to (@2,
3874 (plusminus == PLUS_EXPR
3875 ? wi::max_value (TYPE_PRECISION (type), SIGNED)
3876 : wi::min_value (TYPE_PRECISION (type), SIGNED))))))
3878 (mult (plusminus @2 { build_one_cst (type); }) @0))))))
3881 /* Canonicalize X + (X << C) into X * (1 + (1 << C)) and
3882 (X << C1) + (X << C2) into X * ((1 << C1) + (1 << C2)). */
3884 (plus:c @0 (lshift:s @0 INTEGER_CST@1))
3885 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3886 && tree_fits_uhwi_p (@1)
3887 && tree_to_uhwi (@1) < element_precision (type)
3888 && (INTEGRAL_TYPE_P (TREE_TYPE (@0))
3889 || optab_handler (smul_optab,
3890 TYPE_MODE (type)) != CODE_FOR_nothing))
3891 (with { tree t = type;
3892 if (!TYPE_OVERFLOW_WRAPS (t)) t = unsigned_type_for (t);
3893 wide_int w = wi::set_bit_in_zero (tree_to_uhwi (@1),
3894 element_precision (type));
3896 tree cst = wide_int_to_tree (VECTOR_TYPE_P (t) ? TREE_TYPE (t)
3898 cst = build_uniform_cst (t, cst); }
3899 (convert (mult (convert:t @0) { cst; })))))
3901 (plus (lshift:s @0 INTEGER_CST@1) (lshift:s @0 INTEGER_CST@2))
3902 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3903 && tree_fits_uhwi_p (@1)
3904 && tree_to_uhwi (@1) < element_precision (type)
3905 && tree_fits_uhwi_p (@2)
3906 && tree_to_uhwi (@2) < element_precision (type)
3907 && (INTEGRAL_TYPE_P (TREE_TYPE (@0))
3908 || optab_handler (smul_optab,
3909 TYPE_MODE (type)) != CODE_FOR_nothing))
3910 (with { tree t = type;
3911 if (!TYPE_OVERFLOW_WRAPS (t)) t = unsigned_type_for (t);
3912 unsigned int prec = element_precision (type);
3913 wide_int w = wi::set_bit_in_zero (tree_to_uhwi (@1), prec);
3914 w += wi::set_bit_in_zero (tree_to_uhwi (@2), prec);
3915 tree cst = wide_int_to_tree (VECTOR_TYPE_P (t) ? TREE_TYPE (t)
3917 cst = build_uniform_cst (t, cst); }
3918 (convert (mult (convert:t @0) { cst; })))))
3921 /* Canonicalize (X*C1)|(X*C2) and (X*C1)^(X*C2) to (C1+C2)*X when
3922 tree_nonzero_bits allows IOR and XOR to be treated like PLUS.
3923 Likewise, handle (X<<C3) and X as legitimate variants of X*C. */
3924 (for op (bit_ior bit_xor)
3926 (op (mult:s@0 @1 INTEGER_CST@2)
3927 (mult:s@3 @1 INTEGER_CST@4))
3928 (if (INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_WRAPS (type)
3929 && (tree_nonzero_bits (@0) & tree_nonzero_bits (@3)) == 0)
3931 { wide_int_to_tree (type, wi::to_wide (@2) + wi::to_wide (@4)); })))
3933 (op:c (mult:s@0 @1 INTEGER_CST@2)
3934 (lshift:s@3 @1 INTEGER_CST@4))
3935 (if (INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_WRAPS (type)
3936 && tree_int_cst_sgn (@4) > 0
3937 && (tree_nonzero_bits (@0) & tree_nonzero_bits (@3)) == 0)
3938 (with { wide_int wone = wi::one (TYPE_PRECISION (type));
3939 wide_int c = wi::add (wi::to_wide (@2),
3940 wi::lshift (wone, wi::to_wide (@4))); }
3941 (mult @1 { wide_int_to_tree (type, c); }))))
3943 (op:c (mult:s@0 @1 INTEGER_CST@2)
3945 (if (INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_WRAPS (type)
3946 && (tree_nonzero_bits (@0) & tree_nonzero_bits (@1)) == 0)
3948 { wide_int_to_tree (type,
3949 wi::add (wi::to_wide (@2), 1)); })))
3951 (op (lshift:s@0 @1 INTEGER_CST@2)
3952 (lshift:s@3 @1 INTEGER_CST@4))
3953 (if (INTEGRAL_TYPE_P (type)
3954 && tree_int_cst_sgn (@2) > 0
3955 && tree_int_cst_sgn (@4) > 0
3956 && (tree_nonzero_bits (@0) & tree_nonzero_bits (@3)) == 0)
3957 (with { tree t = type;
3958 if (!TYPE_OVERFLOW_WRAPS (t))
3959 t = unsigned_type_for (t);
3960 wide_int wone = wi::one (TYPE_PRECISION (t));
3961 wide_int c = wi::add (wi::lshift (wone, wi::to_wide (@2)),
3962 wi::lshift (wone, wi::to_wide (@4))); }
3963 (convert (mult:t (convert:t @1) { wide_int_to_tree (t,c); })))))
3965 (op:c (lshift:s@0 @1 INTEGER_CST@2)
3967 (if (INTEGRAL_TYPE_P (type)
3968 && tree_int_cst_sgn (@2) > 0
3969 && (tree_nonzero_bits (@0) & tree_nonzero_bits (@1)) == 0)
3970 (with { tree t = type;
3971 if (!TYPE_OVERFLOW_WRAPS (t))
3972 t = unsigned_type_for (t);
3973 wide_int wone = wi::one (TYPE_PRECISION (t));
3974 wide_int c = wi::add (wi::lshift (wone, wi::to_wide (@2)), wone); }
3975 (convert (mult:t (convert:t @1) { wide_int_to_tree (t, c); }))))))
3977 /* Simplifications of MIN_EXPR, MAX_EXPR, fmin() and fmax(). */
3979 (for minmax (min max)
3983 /* max(max(x,y),x) -> max(x,y) */
3985 (minmax:c (minmax:c@2 @0 @1) @0)
3987 /* For fmin() and fmax(), skip folding when both are sNaN. */
3988 (for minmax (FMIN_ALL FMAX_ALL)
3991 (if (!tree_expr_maybe_signaling_nan_p (@0))
3993 /* min(max(x,y),y) -> y. */
3995 (min:c (max:c @0 @1) @1)
3997 /* max(min(x,y),y) -> y. */
3999 (max:c (min:c @0 @1) @1)
4001 /* max(a,-a) -> abs(a). */
4003 (max:c @0 (negate @0))
4004 (if (TREE_CODE (type) != COMPLEX_TYPE
4005 && (! ANY_INTEGRAL_TYPE_P (type)
4006 || TYPE_OVERFLOW_UNDEFINED (type)))
4008 /* min(a,-a) -> -abs(a). */
4010 (min:c @0 (negate @0))
4011 (if (TREE_CODE (type) != COMPLEX_TYPE
4012 && (! ANY_INTEGRAL_TYPE_P (type)
4013 || TYPE_OVERFLOW_UNDEFINED (type)))
4018 (if (INTEGRAL_TYPE_P (type)
4019 && TYPE_MIN_VALUE (type)
4020 && operand_equal_p (@1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
4022 (if (INTEGRAL_TYPE_P (type)
4023 && TYPE_MAX_VALUE (type)
4024 && operand_equal_p (@1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
4029 (if (INTEGRAL_TYPE_P (type)
4030 && TYPE_MAX_VALUE (type)
4031 && operand_equal_p (@1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
4033 (if (INTEGRAL_TYPE_P (type)
4034 && TYPE_MIN_VALUE (type)
4035 && operand_equal_p (@1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
4038 /* max (a, a + CST) -> a + CST where CST is positive. */
4039 /* max (a, a + CST) -> a where CST is negative. */
4041 (max:c @0 (plus@2 @0 INTEGER_CST@1))
4042 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
4043 (if (tree_int_cst_sgn (@1) > 0)
4047 /* min (a, a + CST) -> a where CST is positive. */
4048 /* min (a, a + CST) -> a + CST where CST is negative. */
4050 (min:c @0 (plus@2 @0 INTEGER_CST@1))
4051 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
4052 (if (tree_int_cst_sgn (@1) > 0)
4056 /* Simplify min (&var[off0], &var[off1]) etc. depending on whether
4057 the addresses are known to be less, equal or greater. */
4058 (for minmax (min max)
4061 (minmax (convert1?@2 addr@0) (convert2?@3 addr@1))
4064 poly_int64 off0, off1;
4066 int equal = address_compare (cmp, TREE_TYPE (@2), @0, @1, base0, base1,
4067 off0, off1, GENERIC);
4070 (if (minmax == MIN_EXPR)
4071 (if (known_le (off0, off1))
4073 (if (known_gt (off0, off1))
4075 (if (known_ge (off0, off1))
4077 (if (known_lt (off0, off1))
4080 /* (convert (minmax ((convert (x) c)))) -> minmax (x c) if x is promoted
4081 and the outer convert demotes the expression back to x's type. */
4082 (for minmax (min max)
4084 (convert (minmax@0 (convert @1) INTEGER_CST@2))
4085 (if (INTEGRAL_TYPE_P (type)
4086 && types_match (@1, type) && int_fits_type_p (@2, type)
4087 && TYPE_SIGN (TREE_TYPE (@0)) == TYPE_SIGN (type)
4088 && TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type))
4089 (minmax @1 (convert @2)))))
4091 (for minmax (FMIN_ALL FMAX_ALL)
4092 /* If either argument is NaN and other one is not sNaN, return the other
4093 one. Avoid the transformation if we get (and honor) a signalling NaN. */
4095 (minmax:c @0 REAL_CST@1)
4096 (if (real_isnan (TREE_REAL_CST_PTR (@1))
4097 && (!HONOR_SNANS (@1) || !TREE_REAL_CST (@1).signalling)
4098 && !tree_expr_maybe_signaling_nan_p (@0))
4100 /* Convert fmin/fmax to MIN_EXPR/MAX_EXPR. C99 requires these
4101 functions to return the numeric arg if the other one is NaN.
4102 MIN and MAX don't honor that, so only transform if -ffinite-math-only
4103 is set. C99 doesn't require -0.0 to be handled, so we don't have to
4104 worry about it either. */
4105 (if (flag_finite_math_only)
4112 /* min (-A, -B) -> -max (A, B) */
4113 (for minmax (min max FMIN_ALL FMAX_ALL)
4114 maxmin (max min FMAX_ALL FMIN_ALL)
4116 (minmax (negate:s@2 @0) (negate:s@3 @1))
4117 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
4118 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
4119 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
4120 (negate (maxmin @0 @1)))))
4121 /* MIN (~X, ~Y) -> ~MAX (X, Y)
4122 MAX (~X, ~Y) -> ~MIN (X, Y) */
4123 (for minmax (min max)
4126 (minmax (bit_not:s@2 @0) (bit_not:s@3 @1))
4127 (bit_not (maxmin @0 @1)))
4128 /* ~MAX(~X, Y) --> MIN(X, ~Y) */
4129 /* ~MIN(~X, Y) --> MAX(X, ~Y) */
4131 (bit_not (minmax:cs (bit_not @0) @1))
4132 (maxmin @0 (bit_not @1))))
4134 /* MIN (X, Y) == X -> X <= Y */
4135 /* MIN (X, Y) < X -> X > Y */
4136 /* MIN (X, Y) >= X -> X <= Y */
4137 (for minmax (min min min min max max max max)
4138 cmp (eq ne lt ge eq ne gt le )
4139 out (le gt gt le ge lt lt ge )
4141 (cmp:c (minmax:c @0 @1) @0)
4142 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0)))
4144 /* MIN (X, 5) == 0 -> X == 0
4145 MIN (X, 5) == 7 -> false */
4148 (cmp (min @0 INTEGER_CST@1) INTEGER_CST@2)
4149 (if (wi::lt_p (wi::to_wide (@1), wi::to_wide (@2),
4150 TYPE_SIGN (TREE_TYPE (@0))))
4151 { constant_boolean_node (cmp == NE_EXPR, type); }
4152 (if (wi::gt_p (wi::to_wide (@1), wi::to_wide (@2),
4153 TYPE_SIGN (TREE_TYPE (@0))))
4157 (cmp (max @0 INTEGER_CST@1) INTEGER_CST@2)
4158 (if (wi::gt_p (wi::to_wide (@1), wi::to_wide (@2),
4159 TYPE_SIGN (TREE_TYPE (@0))))
4160 { constant_boolean_node (cmp == NE_EXPR, type); }
4161 (if (wi::lt_p (wi::to_wide (@1), wi::to_wide (@2),
4162 TYPE_SIGN (TREE_TYPE (@0))))
4165 /* X <= MAX(X, Y) -> true
4166 X > MAX(X, Y) -> false
4167 X >= MIN(X, Y) -> true
4168 X < MIN(X, Y) -> false */
4169 (for minmax (min min max max )
4172 (cmp:c @0 (minmax:c @0 @1))
4173 { constant_boolean_node (cmp == GE_EXPR || cmp == LE_EXPR, type); } ))
4175 /* MIN (X, C1) < C2 -> X < C2 || C1 < C2 */
4176 (for minmax (min min max max min min max max )
4177 cmp (lt le gt ge gt ge lt le )
4178 comb (bit_ior bit_ior bit_ior bit_ior bit_and bit_and bit_and bit_and)
4180 (cmp (minmax @0 INTEGER_CST@1) INTEGER_CST@2)
4181 (comb (cmp @0 @2) (cmp @1 @2))))
4183 /* Undo fancy ways of writing max/min or other ?: expressions, like
4184 a - ((a - b) & -(a < b)) and a - (a - b) * (a < b) into (a < b) ? b : a.
4185 People normally use ?: and that is what we actually try to optimize. */
4186 /* Transform A + (B-A)*cmp into cmp ? B : A. */
4188 (plus:c @0 (mult:c (minus @1 @0) zero_one_valued_p@2))
4189 (if (INTEGRAL_TYPE_P (type)
4190 && (GIMPLE || !TREE_SIDE_EFFECTS (@1)))
4191 (cond (convert:boolean_type_node @2) @1 @0)))
4192 /* Transform A - (A-B)*cmp into cmp ? B : A. */
4194 (minus @0 (mult:c (minus @0 @1) zero_one_valued_p@2))
4195 (if (INTEGRAL_TYPE_P (type)
4196 && (GIMPLE || !TREE_SIDE_EFFECTS (@1)))
4197 (cond (convert:boolean_type_node @2) @1 @0)))
4198 /* Transform A ^ (A^B)*cmp into cmp ? B : A. */
4200 (bit_xor:c @0 (mult:c (bit_xor:c @0 @1) zero_one_valued_p@2))
4201 (if (INTEGRAL_TYPE_P (type)
4202 && (GIMPLE || !TREE_SIDE_EFFECTS (@1)))
4203 (cond (convert:boolean_type_node @2) @1 @0)))
4205 /* (x <= 0 ? -x : 0) -> max(-x, 0). */
4207 (cond (le @0 integer_zerop@1) (negate@2 @0) integer_zerop@1)
4210 /* (zero_one == 0) ? y : z <op> y -> ((typeof(y))zero_one * z) <op> y */
4211 (for op (bit_xor bit_ior plus)
4213 (cond (eq zero_one_valued_p@0
4217 (if (INTEGRAL_TYPE_P (type)
4218 && TYPE_PRECISION (type) > 1
4219 && (INTEGRAL_TYPE_P (TREE_TYPE (@0))))
4220 (op (mult (convert:type @0) @2) @1))))
4222 /* (zero_one != 0) ? z <op> y : y -> ((typeof(y))zero_one * z) <op> y */
4223 (for op (bit_xor bit_ior plus)
4225 (cond (ne zero_one_valued_p@0
4229 (if (INTEGRAL_TYPE_P (type)
4230 && TYPE_PRECISION (type) > 1
4231 && (INTEGRAL_TYPE_P (TREE_TYPE (@0))))
4232 (op (mult (convert:type @0) @2) @1))))
4234 /* ?: Value replacement. */
4235 /* a == 0 ? b : b + a -> b + a */
4236 (for op (plus bit_ior bit_xor)
4238 (cond (eq @0 integer_zerop) @1 (op:c@2 @1 @0))
4240 /* a == 0 ? b : b - a -> b - a */
4241 /* a == 0 ? b : b ptr+ a -> b ptr+ a */
4242 /* a == 0 ? b : b shift/rotate a -> b shift/rotate a */
4243 (for op (lrotate rrotate lshift rshift minus pointer_plus)
4245 (cond (eq @0 integer_zerop) @1 (op@2 @1 @0))
4248 /* a == 1 ? b : b / a -> b / a */
4249 (for op (trunc_div ceil_div floor_div round_div exact_div)
4251 (cond (eq @0 integer_onep) @1 (op@2 @1 @0))
4254 /* a == 1 ? b : a * b -> a * b */
4257 (cond (eq @0 integer_onep) @1 (op:c@2 @1 @0))
4260 /* a == -1 ? b : a & b -> a & b */
4263 (cond (eq @0 integer_all_onesp) @1 (op:c@2 @1 @0))
4266 /* Simplifications of shift and rotates. */
4268 (for rotate (lrotate rrotate)
4270 (rotate integer_all_onesp@0 @1)
4273 /* Optimize -1 >> x for arithmetic right shifts. */
4275 (rshift integer_all_onesp@0 @1)
4276 (if (!TYPE_UNSIGNED (type))
4279 /* Optimize (x >> c) << c into x & (-1<<c). */
4281 (lshift (nop_convert? (rshift @0 INTEGER_CST@1)) @1)
4282 (if (wi::ltu_p (wi::to_wide (@1), element_precision (type)))
4283 /* It doesn't matter if the right shift is arithmetic or logical. */
4284 (bit_and (view_convert @0) (lshift { build_minus_one_cst (type); } @1))))
4287 (lshift (convert (convert@2 (rshift @0 INTEGER_CST@1))) @1)
4288 (if (wi::ltu_p (wi::to_wide (@1), element_precision (type))
4289 /* Allow intermediate conversion to integral type with whatever sign, as
4290 long as the low TYPE_PRECISION (type)
4291 - TYPE_PRECISION (TREE_TYPE (@2)) bits are preserved. */
4292 && INTEGRAL_TYPE_P (type)
4293 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
4294 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
4295 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (@0))
4296 && (TYPE_PRECISION (TREE_TYPE (@2)) >= TYPE_PRECISION (type)
4297 || wi::geu_p (wi::to_wide (@1),
4298 TYPE_PRECISION (type)
4299 - TYPE_PRECISION (TREE_TYPE (@2)))))
4300 (bit_and (convert @0) (lshift { build_minus_one_cst (type); } @1))))
4302 /* For (x << c) >> c, optimize into x & ((unsigned)-1 >> c) for
4303 unsigned x OR truncate into the precision(type) - c lowest bits
4304 of signed x (if they have mode precision or a precision of 1). */
4306 (rshift (nop_convert? (lshift @0 INTEGER_CST@1)) @@1)
4307 (if (wi::ltu_p (wi::to_wide (@1), element_precision (type)))
4308 (if (TYPE_UNSIGNED (type))
4309 (bit_and (convert @0) (rshift { build_minus_one_cst (type); } @1))
4310 (if (INTEGRAL_TYPE_P (type))
4312 int width = element_precision (type) - tree_to_uhwi (@1);
4313 tree stype = NULL_TREE;
4314 if (width <= MAX_FIXED_MODE_SIZE)
4315 stype = build_nonstandard_integer_type (width, 0);
4317 (if (stype && (width == 1 || type_has_mode_precision_p (stype)))
4318 (convert (convert:stype @0))))))))
4320 /* Optimize x >> x into 0 */
4323 { build_zero_cst (type); })
4325 (for shiftrotate (lrotate rrotate lshift rshift)
4327 (shiftrotate @0 integer_zerop)
4330 (shiftrotate integer_zerop@0 @1)
4332 /* Prefer vector1 << scalar to vector1 << vector2
4333 if vector2 is uniform. */
4334 (for vec (VECTOR_CST CONSTRUCTOR)
4336 (shiftrotate @0 vec@1)
4337 (with { tree tem = uniform_vector_p (@1); }
4339 (shiftrotate @0 { tem; }))))))
4341 /* Simplify X << Y where Y's low width bits are 0 to X, as only valid
4342 Y is 0. Similarly for X >> Y. */
4344 (for shift (lshift rshift)
4346 (shift @0 SSA_NAME@1)
4347 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1)))
4349 int width = ceil_log2 (element_precision (TREE_TYPE (@0)));
4350 int prec = TYPE_PRECISION (TREE_TYPE (@1));
4352 (if ((get_nonzero_bits (@1) & wi::mask (width, false, prec)) == 0)
4356 /* Rewrite an LROTATE_EXPR by a constant into an
4357 RROTATE_EXPR by a new constant. */
4359 (lrotate @0 INTEGER_CST@1)
4360 (rrotate @0 { const_binop (MINUS_EXPR, TREE_TYPE (@1),
4361 build_int_cst (TREE_TYPE (@1),
4362 element_precision (type)), @1); }))
4364 /* Turn (a OP c1) OP c2 into a OP (c1+c2). */
4365 (for op (lrotate rrotate rshift lshift)
4367 (op (op @0 INTEGER_CST@1) INTEGER_CST@2)
4368 (with { unsigned int prec = element_precision (type); }
4369 (if (wi::ge_p (wi::to_wide (@1), 0, TYPE_SIGN (TREE_TYPE (@1)))
4370 && wi::lt_p (wi::to_wide (@1), prec, TYPE_SIGN (TREE_TYPE (@1)))
4371 && wi::ge_p (wi::to_wide (@2), 0, TYPE_SIGN (TREE_TYPE (@2)))
4372 && wi::lt_p (wi::to_wide (@2), prec, TYPE_SIGN (TREE_TYPE (@2))))
4373 (with { unsigned int low = (tree_to_uhwi (@1)
4374 + tree_to_uhwi (@2)); }
4375 /* Deal with a OP (c1 + c2) being undefined but (a OP c1) OP c2
4376 being well defined. */
4378 (if (op == LROTATE_EXPR || op == RROTATE_EXPR)
4379 (op @0 { build_int_cst (TREE_TYPE (@1), low % prec); })
4380 (if (TYPE_UNSIGNED (type) || op == LSHIFT_EXPR)
4381 { build_zero_cst (type); }
4382 (op @0 { build_int_cst (TREE_TYPE (@1), prec - 1); })))
4383 (op @0 { build_int_cst (TREE_TYPE (@1), low); })))))))
4386 /* Simplify (CST << x) & 1 to 0 if CST is even or to x == 0 if it is odd. */
4388 (bit_and (lshift INTEGER_CST@1 @0) integer_onep)
4389 (if ((wi::to_wide (@1) & 1) != 0)
4390 (convert (eq:boolean_type_node @0 { build_zero_cst (TREE_TYPE (@0)); }))
4391 { build_zero_cst (type); }))
4393 /* Simplify ((C << x) & D) != 0 where C and D are power of two constants,
4394 either to false if D is smaller (unsigned comparison) than C, or to
4395 x == log2 (D) - log2 (C). Similarly for right shifts.
4396 Note for `(1 >> x)`, the & 1 has been removed so matching that seperately. */
4400 (cmp (bit_and (lshift integer_pow2p@1 @0) integer_pow2p@2) integer_zerop)
4401 (with { int c1 = wi::clz (wi::to_wide (@1));
4402 int c2 = wi::clz (wi::to_wide (@2)); }
4404 { constant_boolean_node (cmp == NE_EXPR ? false : true, type); }
4405 (icmp @0 { build_int_cst (TREE_TYPE (@0), c1 - c2); }))))
4407 (cmp (bit_and (rshift integer_pow2p@1 @0) integer_pow2p@2) integer_zerop)
4408 (if (tree_int_cst_sgn (@1) > 0)
4409 (with { int c1 = wi::clz (wi::to_wide (@1));
4410 int c2 = wi::clz (wi::to_wide (@2)); }
4412 { constant_boolean_node (cmp == NE_EXPR ? false : true, type); }
4413 (icmp @0 { build_int_cst (TREE_TYPE (@0), c2 - c1); })))))
4414 /* `(1 >> X) != 0` -> `X == 0` */
4415 /* `(1 >> X) == 0` -> `X != 0` */
4417 (cmp (rshift integer_onep@1 @0) integer_zerop)
4418 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1)))
4419 (icmp @0 { build_zero_cst (TREE_TYPE (@0)); }))))
4421 /* (CST1 << A) == CST2 -> A == ctz (CST2) - ctz (CST1)
4422 (CST1 << A) != CST2 -> A != ctz (CST2) - ctz (CST1)
4426 (cmp (lshift INTEGER_CST@0 @1) INTEGER_CST@2)
4427 (with { int cand = wi::ctz (wi::to_wide (@2)) - wi::ctz (wi::to_wide (@0)); }
4429 || (!integer_zerop (@2)
4430 && wi::lshift (wi::to_wide (@0), cand) != wi::to_wide (@2)))
4431 { constant_boolean_node (cmp == NE_EXPR, type); }
4432 (if (!integer_zerop (@2)
4433 && wi::lshift (wi::to_wide (@0), cand) == wi::to_wide (@2))
4434 (cmp @1 { build_int_cst (TREE_TYPE (@1), cand); }))))))
4436 /* Fold ((X << C1) & C2) cmp C3 into (X & (C2 >> C1)) cmp (C3 >> C1)
4437 ((X >> C1) & C2) cmp C3 into (X & (C2 << C1)) cmp (C3 << C1). */
4440 (cmp (bit_and:s (lshift:s @0 INTEGER_CST@1) INTEGER_CST@2) INTEGER_CST@3)
4441 (if (tree_fits_shwi_p (@1)
4442 && tree_to_shwi (@1) > 0
4443 && tree_to_shwi (@1) < TYPE_PRECISION (TREE_TYPE (@0)))
4444 (if (tree_to_shwi (@1) > wi::ctz (wi::to_wide (@3)))
4445 { constant_boolean_node (cmp == NE_EXPR, type); }
4446 (with { wide_int c1 = wi::to_wide (@1);
4447 wide_int c2 = wi::lrshift (wi::to_wide (@2), c1);
4448 wide_int c3 = wi::lrshift (wi::to_wide (@3), c1); }
4449 (cmp (bit_and @0 { wide_int_to_tree (TREE_TYPE (@0), c2); })
4450 { wide_int_to_tree (TREE_TYPE (@0), c3); })))))
4452 (cmp (bit_and:s (rshift:s @0 INTEGER_CST@1) INTEGER_CST@2) INTEGER_CST@3)
4453 (if (tree_fits_shwi_p (@1)
4454 && tree_to_shwi (@1) > 0
4455 && tree_to_shwi (@1) < TYPE_PRECISION (TREE_TYPE (@0)))
4456 (with { tree t0 = TREE_TYPE (@0);
4457 unsigned int prec = TYPE_PRECISION (t0);
4458 wide_int c1 = wi::to_wide (@1);
4459 wide_int c2 = wi::to_wide (@2);
4460 wide_int c3 = wi::to_wide (@3);
4461 wide_int sb = wi::set_bit_in_zero (prec - 1, prec); }
4462 (if ((c2 & c3) != c3)
4463 { constant_boolean_node (cmp == NE_EXPR, type); }
4464 (if (TYPE_UNSIGNED (t0))
4465 (if ((c3 & wi::arshift (sb, c1 - 1)) != 0)
4466 { constant_boolean_node (cmp == NE_EXPR, type); }
4467 (cmp (bit_and @0 { wide_int_to_tree (t0, c2 << c1); })
4468 { wide_int_to_tree (t0, c3 << c1); }))
4469 (with { wide_int smask = wi::arshift (sb, c1); }
4471 (if ((c2 & smask) == 0)
4472 (cmp (bit_and @0 { wide_int_to_tree (t0, c2 << c1); })
4473 { wide_int_to_tree (t0, c3 << c1); }))
4474 (if ((c3 & smask) == 0)
4475 (cmp (bit_and @0 { wide_int_to_tree (t0, (c2 << c1) | sb); })
4476 { wide_int_to_tree (t0, c3 << c1); }))
4477 (if ((c2 & smask) != (c3 & smask))
4478 { constant_boolean_node (cmp == NE_EXPR, type); })
4479 (cmp (bit_and @0 { wide_int_to_tree (t0, (c2 << c1) | sb); })
4480 { wide_int_to_tree (t0, (c3 << c1) | sb); })))))))))
4482 /* Fold (X << C1) & C2 into (X << C1) & (C2 | ((1 << C1) - 1))
4483 (X >> C1) & C2 into (X >> C1) & (C2 | ~((type) -1 >> C1))
4484 if the new mask might be further optimized. */
4485 (for shift (lshift rshift)
4487 (bit_and (convert?:s@4 (shift:s@5 (convert1?@3 @0) INTEGER_CST@1))
4489 (if (tree_nop_conversion_p (TREE_TYPE (@4), TREE_TYPE (@5))
4490 && TYPE_PRECISION (type) <= HOST_BITS_PER_WIDE_INT
4491 && tree_fits_uhwi_p (@1)
4492 && tree_to_uhwi (@1) > 0
4493 && tree_to_uhwi (@1) < TYPE_PRECISION (type))
4496 unsigned int shiftc = tree_to_uhwi (@1);
4497 unsigned HOST_WIDE_INT mask = TREE_INT_CST_LOW (@2);
4498 unsigned HOST_WIDE_INT newmask, zerobits = 0;
4499 tree shift_type = TREE_TYPE (@3);
4502 if (shift == LSHIFT_EXPR)
4503 zerobits = ((HOST_WIDE_INT_1U << shiftc) - 1);
4504 else if (shift == RSHIFT_EXPR
4505 && type_has_mode_precision_p (shift_type))
4507 prec = TYPE_PRECISION (TREE_TYPE (@3));
4509 /* See if more bits can be proven as zero because of
4512 && TYPE_UNSIGNED (TREE_TYPE (@0)))
4514 tree inner_type = TREE_TYPE (@0);
4515 if (type_has_mode_precision_p (inner_type)
4516 && TYPE_PRECISION (inner_type) < prec)
4518 prec = TYPE_PRECISION (inner_type);
4519 /* See if we can shorten the right shift. */
4521 shift_type = inner_type;
4522 /* Otherwise X >> C1 is all zeros, so we'll optimize
4523 it into (X, 0) later on by making sure zerobits
4527 zerobits = HOST_WIDE_INT_M1U;
4530 zerobits >>= HOST_BITS_PER_WIDE_INT - shiftc;
4531 zerobits <<= prec - shiftc;
4533 /* For arithmetic shift if sign bit could be set, zerobits
4534 can contain actually sign bits, so no transformation is
4535 possible, unless MASK masks them all away. In that
4536 case the shift needs to be converted into logical shift. */
4537 if (!TYPE_UNSIGNED (TREE_TYPE (@3))
4538 && prec == TYPE_PRECISION (TREE_TYPE (@3)))
4540 if ((mask & zerobits) == 0)
4541 shift_type = unsigned_type_for (TREE_TYPE (@3));
4547 /* ((X << 16) & 0xff00) is (X, 0). */
4548 (if ((mask & zerobits) == mask)
4549 { build_int_cst (type, 0); }
4550 (with { newmask = mask | zerobits; }
4551 (if (newmask != mask && (newmask & (newmask + 1)) == 0)
4554 /* Only do the transformation if NEWMASK is some integer
4556 for (prec = BITS_PER_UNIT;
4557 prec < HOST_BITS_PER_WIDE_INT; prec <<= 1)
4558 if (newmask == (HOST_WIDE_INT_1U << prec) - 1)
4561 (if (prec < HOST_BITS_PER_WIDE_INT
4562 || newmask == HOST_WIDE_INT_M1U)
4564 { tree newmaskt = build_int_cst_type (TREE_TYPE (@2), newmask); }
4565 (if (!tree_int_cst_equal (newmaskt, @2))
4566 (if (shift_type != TREE_TYPE (@3))
4567 (bit_and (convert (shift:shift_type (convert @3) @1)) { newmaskt; })
4568 (bit_and @4 { newmaskt; })))))))))))))
4570 /* ((1 << n) & M) != 0 -> n == log2 (M) */
4576 (nop_convert? (lshift integer_onep @0)) integer_pow2p@1) integer_zerop)
4577 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)))
4578 (icmp @0 { wide_int_to_tree (TREE_TYPE (@0),
4579 wi::exact_log2 (wi::to_wide (@1))); }))))
4581 /* Fold (X {&,^,|} C2) << C1 into (X << C1) {&,^,|} (C2 << C1)
4582 (X {&,^,|} C2) >> C1 into (X >> C1) & (C2 >> C1). */
4583 (for shift (lshift rshift)
4584 (for bit_op (bit_and bit_xor bit_ior)
4586 (shift (convert?:s (bit_op:s @0 INTEGER_CST@2)) INTEGER_CST@1)
4587 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
4588 (with { tree mask = int_const_binop (shift, fold_convert (type, @2), @1); }
4590 (bit_op (shift (convert @0) @1) { mask; })))))))
4592 /* ~(~X >> Y) -> X >> Y (for arithmetic shift). */
4594 (bit_not (convert1?:s (rshift:s (convert2?@0 (bit_not @1)) @2)))
4595 (if (!TYPE_UNSIGNED (TREE_TYPE (@0))
4596 && (element_precision (TREE_TYPE (@0))
4597 <= element_precision (TREE_TYPE (@1))
4598 || !TYPE_UNSIGNED (TREE_TYPE (@1))))
4600 { tree shift_type = TREE_TYPE (@0); }
4601 (convert (rshift (convert:shift_type @1) @2)))))
4603 /* ~(~X >>r Y) -> X >>r Y
4604 ~(~X <<r Y) -> X <<r Y */
4605 (for rotate (lrotate rrotate)
4607 (bit_not (convert1?:s (rotate:s (convert2?@0 (bit_not @1)) @2)))
4608 (if ((element_precision (TREE_TYPE (@0))
4609 <= element_precision (TREE_TYPE (@1))
4610 || !TYPE_UNSIGNED (TREE_TYPE (@1)))
4611 && (element_precision (type) <= element_precision (TREE_TYPE (@0))
4612 || !TYPE_UNSIGNED (TREE_TYPE (@0))))
4614 { tree rotate_type = TREE_TYPE (@0); }
4615 (convert (rotate (convert:rotate_type @1) @2))))))
4618 (for rotate (lrotate rrotate)
4619 invrot (rrotate lrotate)
4620 /* (X >>r Y) cmp (Z >>r Y) may simplify to X cmp Y. */
4622 (cmp (rotate @1 @0) (rotate @2 @0))
4624 /* (X >>r C1) cmp C2 may simplify to X cmp C3. */
4626 (cmp (rotate @0 INTEGER_CST@1) INTEGER_CST@2)
4627 (cmp @0 { const_binop (invrot, TREE_TYPE (@0), @2, @1); }))
4628 /* (X >>r Y) cmp C where C is 0 or ~0, may simplify to X cmp C. */
4630 (cmp (rotate @0 @1) INTEGER_CST@2)
4631 (if (integer_zerop (@2) || integer_all_onesp (@2))
4634 /* Narrow a lshift by constant. */
4636 (convert (lshift:s@0 @1 INTEGER_CST@2))
4637 (if (INTEGRAL_TYPE_P (type)
4638 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
4639 && !integer_zerop (@2)
4640 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0)))
4641 (if (TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (@0))
4642 || wi::ltu_p (wi::to_wide (@2), TYPE_PRECISION (type)))
4643 (lshift (convert @1) @2)
4644 (if (wi::ltu_p (wi::to_wide (@2), TYPE_PRECISION (TREE_TYPE (@0))))
4645 { build_zero_cst (type); }))))
4647 /* Simplifications of conversions. */
4649 /* Basic strip-useless-type-conversions / strip_nops. */
4650 (for cvt (convert view_convert float fix_trunc)
4653 (if ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@0)))
4654 || (GENERIC && type == TREE_TYPE (@0)))
4657 /* Contract view-conversions. */
4659 (view_convert (view_convert @0))
4662 /* For integral conversions with the same precision or pointer
4663 conversions use a NOP_EXPR instead. */
4666 (if ((INTEGRAL_TYPE_P (type) || POINTER_TYPE_P (type))
4667 && (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
4668 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (@0)))
4671 /* Strip inner integral conversions that do not change precision or size, or
4672 zero-extend while keeping the same size (for bool-to-char). */
4674 (view_convert (convert@0 @1))
4675 (if ((INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
4676 && (INTEGRAL_TYPE_P (TREE_TYPE (@1)) || POINTER_TYPE_P (TREE_TYPE (@1)))
4677 && TYPE_SIZE (TREE_TYPE (@0)) == TYPE_SIZE (TREE_TYPE (@1))
4678 && (TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1))
4679 || (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (TREE_TYPE (@1))
4680 && TYPE_UNSIGNED (TREE_TYPE (@1)))))
4683 /* Simplify a view-converted empty or single-element constructor. */
4685 (view_convert CONSTRUCTOR@0)
4687 { tree ctor = (TREE_CODE (@0) == SSA_NAME
4688 ? gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0)) : @0); }
4690 (if (CONSTRUCTOR_NELTS (ctor) == 0)
4691 { build_zero_cst (type); })
4692 (if (CONSTRUCTOR_NELTS (ctor) == 1
4693 && VECTOR_TYPE_P (TREE_TYPE (ctor))
4694 && operand_equal_p (TYPE_SIZE (type),
4695 TYPE_SIZE (TREE_TYPE
4696 (CONSTRUCTOR_ELT (ctor, 0)->value))))
4697 (view_convert { CONSTRUCTOR_ELT (ctor, 0)->value; })))))
4699 /* Re-association barriers around constants and other re-association
4700 barriers can be removed. */
4702 (paren CONSTANT_CLASS_P@0)
4705 (paren (paren@1 @0))
4708 /* Handle cases of two conversions in a row. */
4709 (for ocvt (convert float fix_trunc)
4710 (for icvt (convert float)
4715 tree inside_type = TREE_TYPE (@0);
4716 tree inter_type = TREE_TYPE (@1);
4717 int inside_int = INTEGRAL_TYPE_P (inside_type);
4718 int inside_ptr = POINTER_TYPE_P (inside_type);
4719 int inside_float = FLOAT_TYPE_P (inside_type);
4720 int inside_vec = VECTOR_TYPE_P (inside_type);
4721 unsigned int inside_prec = element_precision (inside_type);
4722 int inside_unsignedp = TYPE_UNSIGNED (inside_type);
4723 int inter_int = INTEGRAL_TYPE_P (inter_type);
4724 int inter_ptr = POINTER_TYPE_P (inter_type);
4725 int inter_float = FLOAT_TYPE_P (inter_type);
4726 int inter_vec = VECTOR_TYPE_P (inter_type);
4727 unsigned int inter_prec = element_precision (inter_type);
4728 int inter_unsignedp = TYPE_UNSIGNED (inter_type);
4729 int final_int = INTEGRAL_TYPE_P (type);
4730 int final_ptr = POINTER_TYPE_P (type);
4731 int final_float = FLOAT_TYPE_P (type);
4732 int final_vec = VECTOR_TYPE_P (type);
4733 unsigned int final_prec = element_precision (type);
4734 int final_unsignedp = TYPE_UNSIGNED (type);
4737 /* In addition to the cases of two conversions in a row
4738 handled below, if we are converting something to its own
4739 type via an object of identical or wider precision, neither
4740 conversion is needed. */
4741 (if (((GIMPLE && useless_type_conversion_p (type, inside_type))
4743 && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (inside_type)))
4744 && (((inter_int || inter_ptr) && final_int)
4745 || (inter_float && final_float))
4746 && inter_prec >= final_prec)
4749 /* Likewise, if the intermediate and initial types are either both
4750 float or both integer, we don't need the middle conversion if the
4751 former is wider than the latter and doesn't change the signedness
4752 (for integers). Avoid this if the final type is a pointer since
4753 then we sometimes need the middle conversion. */
4754 (if (((inter_int && inside_int) || (inter_float && inside_float))
4755 && (final_int || final_float)
4756 && inter_prec >= inside_prec
4757 && (inter_float || inter_unsignedp == inside_unsignedp))
4760 /* If we have a sign-extension of a zero-extended value, we can
4761 replace that by a single zero-extension. Likewise if the
4762 final conversion does not change precision we can drop the
4763 intermediate conversion. Similarly truncation of a sign-extension
4764 can be replaced by a single sign-extension. */
4765 (if (inside_int && inter_int && final_int
4766 && ((inside_prec < inter_prec && inter_prec < final_prec
4767 && inside_unsignedp && !inter_unsignedp)
4768 || final_prec == inter_prec
4769 || (inside_prec < inter_prec && inter_prec > final_prec
4770 && !inside_unsignedp && inter_unsignedp)))
4773 /* Two conversions in a row are not needed unless:
4774 - some conversion is floating-point (overstrict for now), or
4775 - some conversion is a vector (overstrict for now), or
4776 - the intermediate type is narrower than both initial and
4778 - the intermediate type and innermost type differ in signedness,
4779 and the outermost type is wider than the intermediate, or
4780 - the initial type is a pointer type and the precisions of the
4781 intermediate and final types differ, or
4782 - the final type is a pointer type and the precisions of the
4783 initial and intermediate types differ. */
4784 (if (! inside_float && ! inter_float && ! final_float
4785 && ! inside_vec && ! inter_vec && ! final_vec
4786 && (inter_prec >= inside_prec || inter_prec >= final_prec)
4787 && ! (inside_int && inter_int
4788 && inter_unsignedp != inside_unsignedp
4789 && inter_prec < final_prec)
4790 && ((inter_unsignedp && inter_prec > inside_prec)
4791 == (final_unsignedp && final_prec > inter_prec))
4792 && ! (inside_ptr && inter_prec != final_prec)
4793 && ! (final_ptr && inside_prec != inter_prec))
4796 /* `(outer:M)(inter:N) a:O`
4797 can be converted to `(outer:M) a`
4798 if M <= O && N >= O. No matter what signedness of the casts,
4799 as the final is either a truncation from the original or just
4800 a sign change of the type. */
4801 (if (inside_int && inter_int && final_int
4802 && final_prec <= inside_prec
4803 && inter_prec >= inside_prec)
4806 /* A truncation to an unsigned type (a zero-extension) should be
4807 canonicalized as bitwise and of a mask. */
4808 (if (GIMPLE /* PR70366: doing this in GENERIC breaks -Wconversion. */
4809 && final_int && inter_int && inside_int
4810 && final_prec == inside_prec
4811 && final_prec > inter_prec
4813 (convert (bit_and @0 { wide_int_to_tree
4815 wi::mask (inter_prec, false,
4816 TYPE_PRECISION (inside_type))); })))
4818 /* If we are converting an integer to a floating-point that can
4819 represent it exactly and back to an integer, we can skip the
4820 floating-point conversion. */
4821 (if (GIMPLE /* PR66211 */
4822 && inside_int && inter_float && final_int &&
4823 (unsigned) significand_size (TYPE_MODE (inter_type))
4824 >= inside_prec - !inside_unsignedp)
4827 /* (float_type)(integer_type) x -> trunc (x) if the type of x matches
4828 float_type. Only do the transformation if we do not need to preserve
4829 trapping behaviour, so require !flag_trapping_math. */
4832 (float (fix_trunc @0))
4833 (if (!flag_trapping_math
4834 && types_match (type, TREE_TYPE (@0))
4835 && direct_internal_fn_supported_p (IFN_TRUNC, type,
4840 /* If we have a narrowing conversion to an integral type that is fed by a
4841 BIT_AND_EXPR, we might be able to remove the BIT_AND_EXPR if it merely
4842 masks off bits outside the final type (and nothing else). */
4844 (convert (bit_and @0 INTEGER_CST@1))
4845 (if (INTEGRAL_TYPE_P (type)
4846 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
4847 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))
4848 && operand_equal_p (@1, build_low_bits_mask (TREE_TYPE (@1),
4849 TYPE_PRECISION (type)), 0))
4853 /* (X /[ex] A) * A -> X. */
4855 (mult (convert1? (exact_div @0 @@1)) (convert2? @1))
4858 /* Simplify (A / B) * B + (A % B) -> A. */
4859 (for div (trunc_div ceil_div floor_div round_div)
4860 mod (trunc_mod ceil_mod floor_mod round_mod)
4862 (plus:c (mult:c (div @0 @1) @1) (mod @0 @1))
4865 /* x / y * y == x -> x % y == 0. */
4867 (eq:c (mult:c (trunc_div:s @0 @1) @1) @0)
4868 (if (TREE_CODE (TREE_TYPE (@0)) != COMPLEX_TYPE)
4869 (eq (trunc_mod @0 @1) { build_zero_cst (TREE_TYPE (@0)); })))
4871 /* ((X /[ex] A) +- B) * A --> X +- A * B. */
4872 (for op (plus minus)
4874 (mult (convert1? (op (convert2? (exact_div @0 INTEGER_CST@@1)) INTEGER_CST@2)) @1)
4875 (if (tree_nop_conversion_p (type, TREE_TYPE (@2))
4876 && tree_nop_conversion_p (TREE_TYPE (@0), TREE_TYPE (@2)))
4879 wi::overflow_type overflow;
4880 wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@2),
4881 TYPE_SIGN (type), &overflow);
4883 (if (types_match (type, TREE_TYPE (@2))
4884 && types_match (TREE_TYPE (@0), TREE_TYPE (@2)) && !overflow)
4885 (op @0 { wide_int_to_tree (type, mul); })
4886 (with { tree utype = unsigned_type_for (type); }
4887 (convert (op (convert:utype @0)
4888 (mult (convert:utype @1) (convert:utype @2))))))))))
4890 /* Canonicalization of binary operations. */
4892 /* Convert X + -C into X - C. */
4894 (plus @0 REAL_CST@1)
4895 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
4896 (with { tree tem = const_unop (NEGATE_EXPR, type, @1); }
4897 (if (!TREE_OVERFLOW (tem) || !flag_trapping_math)
4898 (minus @0 { tem; })))))
4900 /* Convert x+x into x*2. */
4903 (if (SCALAR_FLOAT_TYPE_P (type))
4904 (mult @0 { build_real (type, dconst2); })
4905 (if (INTEGRAL_TYPE_P (type))
4906 (mult @0 { build_int_cst (type, 2); }))))
4910 (minus integer_zerop @1)
4913 (pointer_diff integer_zerop @1)
4914 (negate (convert @1)))
4916 /* (ARG0 - ARG1) is the same as (-ARG1 + ARG0). So check whether
4917 ARG0 is zero and X + ARG0 reduces to X, since that would mean
4918 (-ARG1 + ARG0) reduces to -ARG1. */
4920 (minus real_zerop@0 @1)
4921 (if (fold_real_zero_addition_p (type, @1, @0, 0))
4924 /* Transform x * -1 into -x. */
4926 (mult @0 integer_minus_onep)
4929 /* Reassociate (X * CST) * Y to (X * Y) * CST. This does not introduce
4930 signed overflow for CST != 0 && CST != -1. */
4932 (mult:c (mult:s@3 @0 INTEGER_CST@1) @2)
4933 (if (TREE_CODE (@2) != INTEGER_CST
4935 && !integer_zerop (@1) && !integer_minus_onep (@1))
4936 (mult (mult @0 @2) @1)))
4938 /* True if we can easily extract the real and imaginary parts of a complex
4940 (match compositional_complex
4941 (convert? (complex @0 @1)))
4943 /* COMPLEX_EXPR and REALPART/IMAGPART_EXPR cancellations. */
4945 (complex (realpart @0) (imagpart @0))
4948 (realpart (complex @0 @1))
4951 (imagpart (complex @0 @1))
4954 /* Sometimes we only care about half of a complex expression. */
4956 (realpart (convert?:s (conj:s @0)))
4957 (convert (realpart @0)))
4959 (imagpart (convert?:s (conj:s @0)))
4960 (convert (negate (imagpart @0))))
4961 (for part (realpart imagpart)
4962 (for op (plus minus)
4964 (part (convert?:s@2 (op:s @0 @1)))
4965 (convert (op (part @0) (part @1))))))
4967 (realpart (convert?:s (CEXPI:s @0)))
4970 (imagpart (convert?:s (CEXPI:s @0)))
4973 /* conj(conj(x)) -> x */
4975 (conj (convert? (conj @0)))
4976 (if (tree_nop_conversion_p (TREE_TYPE (@0), type))
4979 /* conj({x,y}) -> {x,-y} */
4981 (conj (convert?:s (complex:s @0 @1)))
4982 (with { tree itype = TREE_TYPE (type); }
4983 (complex (convert:itype @0) (negate (convert:itype @1)))))
4985 /* BSWAP simplifications, transforms checked by gcc.dg/builtin-bswap-8.c. */
4991 (bswap (bit_not (bswap @0)))
4993 (for bitop (bit_xor bit_ior bit_and)
4995 (bswap (bitop:c (bswap @0) @1))
4996 (bitop @0 (bswap @1))))
4999 (cmp (bswap@2 @0) (bswap @1))
5000 (with { tree ctype = TREE_TYPE (@2); }
5001 (cmp (convert:ctype @0) (convert:ctype @1))))
5003 (cmp (bswap @0) INTEGER_CST@1)
5004 (with { tree ctype = TREE_TYPE (@1); }
5005 (cmp (convert:ctype @0) (bswap! @1)))))
5006 /* (bswap(x) >> C1) & C2 can sometimes be simplified to (x >> C3) & C2. */
5008 (bit_and (convert1? (rshift@0 (convert2? (bswap@4 @1)) INTEGER_CST@2))
5010 (if (BITS_PER_UNIT == 8
5011 && tree_fits_uhwi_p (@2)
5012 && tree_fits_uhwi_p (@3))
5015 unsigned HOST_WIDE_INT prec = TYPE_PRECISION (TREE_TYPE (@4));
5016 unsigned HOST_WIDE_INT bits = tree_to_uhwi (@2);
5017 unsigned HOST_WIDE_INT mask = tree_to_uhwi (@3);
5018 unsigned HOST_WIDE_INT lo = bits & 7;
5019 unsigned HOST_WIDE_INT hi = bits - lo;
5022 && mask < (256u>>lo)
5023 && bits < TYPE_PRECISION (TREE_TYPE(@0)))
5024 (with { unsigned HOST_WIDE_INT ns = (prec - (hi + 8)) + lo; }
5026 (bit_and (convert @1) @3)
5029 tree utype = unsigned_type_for (TREE_TYPE (@1));
5030 tree nst = build_int_cst (integer_type_node, ns);
5032 (bit_and (convert (rshift:utype (convert:utype @1) {nst;})) @3))))))))
5033 /* bswap(x) >> C1 can sometimes be simplified to (T)x >> C2. */
5035 (rshift (convert? (bswap@2 @0)) INTEGER_CST@1)
5036 (if (BITS_PER_UNIT == 8
5037 && CHAR_TYPE_SIZE == 8
5038 && tree_fits_uhwi_p (@1))
5041 unsigned HOST_WIDE_INT prec = TYPE_PRECISION (TREE_TYPE (@2));
5042 unsigned HOST_WIDE_INT bits = tree_to_uhwi (@1);
5043 /* If the bswap was extended before the original shift, this
5044 byte (shift) has the sign of the extension, not the sign of
5045 the original shift. */
5046 tree st = TYPE_PRECISION (type) > prec ? TREE_TYPE (@2) : type;
5048 /* Special case: logical right shift of sign-extended bswap.
5049 (unsigned)(short)bswap16(x)>>12 is (unsigned)((short)x<<8)>>12. */
5050 (if (TYPE_PRECISION (type) > prec
5051 && !TYPE_UNSIGNED (TREE_TYPE (@2))
5052 && TYPE_UNSIGNED (type)
5053 && bits < prec && bits + 8 >= prec)
5054 (with { tree nst = build_int_cst (integer_type_node, prec - 8); }
5055 (rshift (convert (lshift:st (convert:st @0) {nst;})) @1))
5056 (if (bits + 8 == prec)
5057 (if (TYPE_UNSIGNED (st))
5058 (convert (convert:unsigned_char_type_node @0))
5059 (convert (convert:signed_char_type_node @0)))
5060 (if (bits < prec && bits + 8 > prec)
5063 tree nst = build_int_cst (integer_type_node, bits & 7);
5064 tree bt = TYPE_UNSIGNED (st) ? unsigned_char_type_node
5065 : signed_char_type_node;
5067 (convert (rshift:bt (convert:bt @0) {nst;})))))))))
5068 /* bswap(x) & C1 can sometimes be simplified to (x >> C2) & C1. */
5070 (bit_and (convert? (bswap@2 @0)) INTEGER_CST@1)
5071 (if (BITS_PER_UNIT == 8
5072 && tree_fits_uhwi_p (@1)
5073 && tree_to_uhwi (@1) < 256)
5076 unsigned HOST_WIDE_INT prec = TYPE_PRECISION (TREE_TYPE (@2));
5077 tree utype = unsigned_type_for (TREE_TYPE (@0));
5078 tree nst = build_int_cst (integer_type_node, prec - 8);
5080 (bit_and (convert (rshift:utype (convert:utype @0) {nst;})) @1)))))
5083 /* Combine COND_EXPRs and VEC_COND_EXPRs. */
5085 /* Simplify constant conditions.
5086 Only optimize constant conditions when the selected branch
5087 has the same type as the COND_EXPR. This avoids optimizing
5088 away "c ? x : throw", where the throw has a void type.
5089 Note that we cannot throw away the fold-const.cc variant nor
5090 this one as we depend on doing this transform before possibly
5091 A ? B : B -> B triggers and the fold-const.cc one can optimize
5092 0 ? A : B to B even if A has side-effects. Something
5093 genmatch cannot handle. */
5095 (cond INTEGER_CST@0 @1 @2)
5096 (if (integer_zerop (@0))
5097 (if (!VOID_TYPE_P (TREE_TYPE (@2)) || VOID_TYPE_P (type))
5099 (if (!VOID_TYPE_P (TREE_TYPE (@1)) || VOID_TYPE_P (type))
5102 (vec_cond VECTOR_CST@0 @1 @2)
5103 (if (integer_all_onesp (@0))
5105 (if (integer_zerop (@0))
5108 /* Sink unary operations to branches, but only if we do fold both. */
5109 (for op (negate bit_not abs absu)
5111 (op (vec_cond:s @0 @1 @2))
5112 (vec_cond @0 (op! @1) (op! @2))))
5114 /* Sink unary conversions to branches, but only if we do fold both
5115 and the target's truth type is the same as we already have. */
5117 (convert (vec_cond:s @0 @1 @2))
5118 (if (VECTOR_TYPE_P (type)
5119 && types_match (TREE_TYPE (@0), truth_type_for (type)))
5120 (vec_cond @0 (convert! @1) (convert! @2))))
5122 /* Likewise for view_convert of nop_conversions. */
5124 (view_convert (vec_cond:s @0 @1 @2))
5125 (if (VECTOR_TYPE_P (type) && VECTOR_TYPE_P (TREE_TYPE (@1))
5126 && known_eq (TYPE_VECTOR_SUBPARTS (type),
5127 TYPE_VECTOR_SUBPARTS (TREE_TYPE (@1)))
5128 && tree_nop_conversion_p (TREE_TYPE (type), TREE_TYPE (TREE_TYPE (@1))))
5129 (vec_cond @0 (view_convert! @1) (view_convert! @2))))
5131 /* Sink binary operation to branches, but only if we can fold it. */
5132 (for op (tcc_comparison plus minus mult bit_and bit_ior bit_xor
5133 lshift rshift rdiv trunc_div ceil_div floor_div round_div
5134 trunc_mod ceil_mod floor_mod round_mod min max)
5135 /* (c ? a : b) op (c ? d : e) --> c ? (a op d) : (b op e) */
5137 (op (vec_cond:s @0 @1 @2) (vec_cond:s @0 @3 @4))
5138 (vec_cond @0 (op! @1 @3) (op! @2 @4)))
5140 /* (c ? a : b) op d --> c ? (a op d) : (b op d) */
5142 (op (vec_cond:s @0 @1 @2) @3)
5143 (vec_cond @0 (op! @1 @3) (op! @2 @3)))
5145 (op @3 (vec_cond:s @0 @1 @2))
5146 (vec_cond @0 (op! @3 @1) (op! @3 @2))))
5149 (match (nop_atomic_bit_test_and_p @0 @1 @4)
5150 (bit_and (convert?@4 (ATOMIC_FETCH_OR_XOR_N @2 INTEGER_CST@0 @3))
5153 int ibit = tree_log2 (@0);
5154 int ibit2 = tree_log2 (@1);
5158 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))))))
5160 (match (nop_atomic_bit_test_and_p @0 @1 @3)
5161 (bit_and (convert?@3 (SYNC_FETCH_OR_XOR_N @2 INTEGER_CST@0))
5164 int ibit = tree_log2 (@0);
5165 int ibit2 = tree_log2 (@1);
5169 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))))))
5171 (match (nop_atomic_bit_test_and_p @0 @0 @4)
5174 (ATOMIC_FETCH_OR_XOR_N @2 (nop_convert? (lshift@0 integer_onep@5 @6)) @3))
5176 (if (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0)))))
5178 (match (nop_atomic_bit_test_and_p @0 @0 @4)
5181 (SYNC_FETCH_OR_XOR_N @2 (nop_convert? (lshift@0 integer_onep@3 @5))))
5183 (if (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0)))))
5185 (match (nop_atomic_bit_test_and_p @0 @1 @3)
5186 (bit_and@4 (convert?@3 (ATOMIC_FETCH_AND_N @2 INTEGER_CST@0 @5))
5189 int ibit = wi::exact_log2 (wi::zext (wi::bit_not (wi::to_wide (@0)),
5190 TYPE_PRECISION(type)));
5191 int ibit2 = tree_log2 (@1);
5195 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))))))
5197 (match (nop_atomic_bit_test_and_p @0 @1 @3)
5199 (convert?@3 (SYNC_FETCH_AND_AND_N @2 INTEGER_CST@0))
5202 int ibit = wi::exact_log2 (wi::zext (wi::bit_not (wi::to_wide (@0)),
5203 TYPE_PRECISION(type)));
5204 int ibit2 = tree_log2 (@1);
5208 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))))))
5210 (match (nop_atomic_bit_test_and_p @4 @0 @3)
5213 (ATOMIC_FETCH_AND_N @2 (nop_convert?@4 (bit_not (lshift@0 integer_onep@6 @7))) @5))
5215 (if (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@4)))))
5217 (match (nop_atomic_bit_test_and_p @4 @0 @3)
5220 (SYNC_FETCH_AND_AND_N @2 (nop_convert?@4 (bit_not (lshift@0 integer_onep@6 @7)))))
5222 (if (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@4)))))
5226 /* (v ? w : 0) ? a : b is just (v & w) ? a : b
5227 Currently disabled after pass lvec because ARM understands
5228 VEC_COND_EXPR<v==w,-1,0> but not a plain v==w fed to BIT_IOR_EXPR. */
5230 /* These can only be done in gimple as fold likes to convert:
5231 (CMP) & N into (CMP) ? N : 0
5232 and we try to match the same pattern again and again. */
5234 (vec_cond (vec_cond:s @0 @3 integer_zerop) @1 @2)
5235 (if (optimize_vectors_before_lowering_p () && types_match (@0, @3))
5236 (vec_cond (bit_and @0 @3) @1 @2)))
5238 (vec_cond (vec_cond:s @0 integer_all_onesp @3) @1 @2)
5239 (if (optimize_vectors_before_lowering_p () && types_match (@0, @3))
5240 (vec_cond (bit_ior @0 @3) @1 @2)))
5242 (vec_cond (vec_cond:s @0 integer_zerop @3) @1 @2)
5243 (if (optimize_vectors_before_lowering_p () && types_match (@0, @3))
5244 (vec_cond (bit_ior @0 (bit_not @3)) @2 @1)))
5246 (vec_cond (vec_cond:s @0 @3 integer_all_onesp) @1 @2)
5247 (if (optimize_vectors_before_lowering_p () && types_match (@0, @3))
5248 (vec_cond (bit_and @0 (bit_not @3)) @2 @1)))
5250 /* ((VCE (a cmp b ? -1 : 0)) < 0) ? c : d is just
5251 (VCE ((a cmp b) ? (VCE c) : (VCE d))) when TYPE_PRECISION of the
5252 component type of the outer vec_cond is greater equal the inner one. */
5253 (for cmp (simple_comparison)
5256 (lt (view_convert@5 (vec_cond@6 (cmp@4 @0 @1)
5259 integer_zerop) @2 @3)
5260 (if (VECTOR_INTEGER_TYPE_P (TREE_TYPE (@0))
5261 && VECTOR_INTEGER_TYPE_P (TREE_TYPE (@5))
5262 && !TYPE_UNSIGNED (TREE_TYPE (@5))
5263 && VECTOR_TYPE_P (TREE_TYPE (@6))
5264 && VECTOR_TYPE_P (type)
5265 && tree_int_cst_le (TYPE_SIZE (TREE_TYPE (type)),
5266 TYPE_SIZE (TREE_TYPE (TREE_TYPE (@6))))
5267 && TYPE_SIZE (type) == TYPE_SIZE (TREE_TYPE (@6)))
5268 (with { tree vtype = TREE_TYPE (@6);}
5270 (vec_cond @4 (view_convert:vtype @2) (view_convert:vtype @3)))))))
5272 /* c1 ? c2 ? a : b : b --> (c1 & c2) ? a : b */
5274 (vec_cond @0 (vec_cond:s @1 @2 @3) @3)
5275 (if (optimize_vectors_before_lowering_p () && types_match (@0, @1))
5276 (vec_cond (bit_and @0 @1) @2 @3)))
5278 (vec_cond @0 @2 (vec_cond:s @1 @2 @3))
5279 (if (optimize_vectors_before_lowering_p () && types_match (@0, @1))
5280 (vec_cond (bit_ior @0 @1) @2 @3)))
5282 (vec_cond @0 (vec_cond:s @1 @2 @3) @2)
5283 (if (optimize_vectors_before_lowering_p () && types_match (@0, @1))
5284 (vec_cond (bit_ior (bit_not @0) @1) @2 @3)))
5286 (vec_cond @0 @3 (vec_cond:s @1 @2 @3))
5287 (if (optimize_vectors_before_lowering_p () && types_match (@0, @1))
5288 (vec_cond (bit_and (bit_not @0) @1) @2 @3)))
5291 /* Canonicalize mask ? { 0, ... } : { -1, ...} to ~mask if the mask
5292 types are compatible. */
5294 (vec_cond @0 VECTOR_CST@1 VECTOR_CST@2)
5295 (if (VECTOR_BOOLEAN_TYPE_P (type)
5296 && types_match (type, TREE_TYPE (@0)))
5297 (if (integer_zerop (@1) && integer_all_onesp (@2))
5299 (if (integer_all_onesp (@1) && integer_zerop (@2))
5302 /* A few simplifications of "a ? CST1 : CST2". */
5303 /* NOTE: Only do this on gimple as the if-chain-to-switch
5304 optimization depends on the gimple to have if statements in it. */
5307 (cond @0 INTEGER_CST@1 INTEGER_CST@2)
5309 (if (integer_zerop (@2))
5311 /* a ? 1 : 0 -> a if 0 and 1 are integral types. */
5312 (if (integer_onep (@1))
5313 (convert (convert:boolean_type_node @0)))
5314 /* a ? -1 : 0 -> -a. */
5315 (if (INTEGRAL_TYPE_P (type) && integer_all_onesp (@1))
5316 (if (TYPE_PRECISION (type) == 1)
5317 /* For signed 1-bit precision just cast bool to the type. */
5318 (convert (convert:boolean_type_node @0))
5319 (if (TREE_CODE (type) == BOOLEAN_TYPE)
5321 tree intt = build_nonstandard_integer_type (TYPE_PRECISION (type),
5322 TYPE_UNSIGNED (type));
5324 (convert (negate (convert:intt (convert:boolean_type_node @0)))))
5325 (negate (convert:type (convert:boolean_type_node @0))))))
5326 /* a ? powerof2cst : 0 -> a << (log2(powerof2cst)) */
5327 (if (INTEGRAL_TYPE_P (type) && integer_pow2p (@1))
5329 tree shift = build_int_cst (integer_type_node, tree_log2 (@1));
5331 (lshift (convert (convert:boolean_type_node @0)) { shift; })))))
5332 (if (integer_zerop (@1))
5334 /* a ? 0 : 1 -> !a. */
5335 (if (integer_onep (@2))
5336 (convert (bit_xor (convert:boolean_type_node @0) { boolean_true_node; })))
5337 /* a ? 0 : -1 -> -(!a). */
5338 (if (INTEGRAL_TYPE_P (type) && integer_all_onesp (@2))
5339 (if (TYPE_PRECISION (type) == 1)
5340 /* For signed 1-bit precision just cast bool to the type. */
5341 (convert (bit_xor (convert:boolean_type_node @0) { boolean_true_node; }))
5342 (if (TREE_CODE (type) == BOOLEAN_TYPE)
5344 tree intt = build_nonstandard_integer_type (TYPE_PRECISION (type),
5345 TYPE_UNSIGNED (type));
5347 (convert (negate (convert:intt (bit_xor (convert:boolean_type_node @0)
5348 { boolean_true_node; })))))
5349 (negate (convert:type (bit_xor (convert:boolean_type_node @0)
5350 { boolean_true_node; }))))))
5351 /* a ? 0 : powerof2cst -> (!a) << (log2(powerof2cst)) */
5352 (if (INTEGRAL_TYPE_P (type) && integer_pow2p (@2))
5354 tree shift = build_int_cst (integer_type_node, tree_log2 (@2));
5356 (lshift (convert (bit_xor (convert:boolean_type_node @0)
5357 { boolean_true_node; })) { shift; })))))))
5359 /* (a > 1) ? 0 : (cast)a is the same as (cast)(a == 1)
5360 for unsigned types. */
5362 (cond (gt @0 integer_onep@1) integer_zerop (convert? @2))
5363 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
5364 && bitwise_equal_p (@0, @2))
5365 (convert (eq @0 @1))
5369 /* (a <= 1) & (cast)a is the same as (cast)(a == 1)
5370 for unsigned types. */
5372 (bit_and:c (convert1? (le @0 integer_onep@1)) (convert2? @2))
5373 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
5374 && bitwise_equal_p (@0, @2))
5375 (convert (eq @0 @1))
5379 /* `(a == CST) & a` can be simplified to `0` or `(a == CST)` depending
5380 on the first bit of the CST. */
5382 (bit_and:c (convert@2 (eq @0 INTEGER_CST@1)) (convert? @0))
5383 (if ((wi::to_wide (@1) & 1) != 0)
5385 { build_zero_cst (type); }))
5388 # x_5 in range [cst1, cst2] where cst2 = cst1 + 1
5389 x_5 == cstN ? cst4 : cst3
5390 # op is == or != and N is 1 or 2
5391 to r_6 = x_5 + (min (cst3, cst4) - cst1) or
5392 r_6 = (min (cst3, cst4) + cst1) - x_5 depending on op, N and which
5393 of cst3 and cst4 is smaller.
5394 This was originally done by two_value_replacement in phiopt (PR 88676). */
5397 (cond (eqne SSA_NAME@0 INTEGER_CST@1) INTEGER_CST@2 INTEGER_CST@3)
5398 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
5399 && INTEGRAL_TYPE_P (type)
5400 && (wi::to_widest (@2) + 1 == wi::to_widest (@3)
5401 || wi::to_widest (@2) == wi::to_widest (@3) + 1))
5404 get_range_query (cfun)->range_of_expr (r, @0);
5405 if (r.undefined_p ())
5406 r.set_varying (TREE_TYPE (@0));
5408 wide_int min = r.lower_bound ();
5409 wide_int max = r.upper_bound ();
5412 && (wi::to_wide (@1) == min
5413 || wi::to_wide (@1) == max))
5415 tree arg0 = @2, arg1 = @3;
5417 if ((eqne == EQ_EXPR) ^ (wi::to_wide (@1) == min))
5418 std::swap (arg0, arg1);
5419 if (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type))
5420 type1 = TREE_TYPE (@0);
5423 auto prec = TYPE_PRECISION (type1);
5424 auto unsign = TYPE_UNSIGNED (type1);
5425 if (TREE_CODE (type1) == BOOLEAN_TYPE)
5426 type1 = build_nonstandard_integer_type (prec, unsign);
5427 min = wide_int::from (min, prec,
5428 TYPE_SIGN (TREE_TYPE (@0)));
5429 wide_int a = wide_int::from (wi::to_wide (arg0), prec,
5431 enum tree_code code;
5432 wi::overflow_type ovf;
5433 if (tree_int_cst_lt (arg0, arg1))
5439 /* lhs is known to be in range [min, min+1] and we want to add a
5440 to it. Check if that operation can overflow for those 2 values
5441 and if yes, force unsigned type. */
5442 wi::add (min + (wi::neg_p (a) ? 0 : 1), a, SIGNED, &ovf);
5444 type1 = unsigned_type_for (type1);
5453 /* lhs is known to be in range [min, min+1] and we want to subtract
5454 it from a. Check if that operation can overflow for those 2
5455 values and if yes, force unsigned type. */
5456 wi::sub (a, min + (wi::neg_p (min) ? 0 : 1), SIGNED, &ovf);
5458 type1 = unsigned_type_for (type1);
5461 tree arg = wide_int_to_tree (type1, a);
5463 (if (code == PLUS_EXPR)
5464 (convert (plus (convert:type1 @0) { arg; }))
5465 (convert (minus { arg; } (convert:type1 @0))))))))))
5469 (convert (cond@0 @1 INTEGER_CST@2 INTEGER_CST@3))
5470 (if (INTEGRAL_TYPE_P (type)
5471 && INTEGRAL_TYPE_P (TREE_TYPE (@0)))
5472 (cond @1 (convert @2) (convert @3))))
5474 /* Simplification moved from fold_cond_expr_with_comparison. It may also
5476 /* This pattern implements two kinds simplification:
5479 (cond (cmp (convert1? x) c1) (convert2? x) c2) -> (minmax (x c)) if:
5480 1) Conversions are type widening from smaller type.
5481 2) Const c1 equals to c2 after canonicalizing comparison.
5482 3) Comparison has tree code LT, LE, GT or GE.
5483 This specific pattern is needed when (cmp (convert x) c) may not
5484 be simplified by comparison patterns because of multiple uses of
5485 x. It also makes sense here because simplifying across multiple
5486 referred var is always benefitial for complicated cases.
5489 (cond (eq (convert1? x) c1) (convert2? x) c2) -> (cond (eq x c1) c1 c2). */
5490 (for cmp (lt le gt ge eq ne)
5492 (cond (cmp (convert1? @1) INTEGER_CST@3) (convert2? @1) INTEGER_CST@2)
5495 tree from_type = TREE_TYPE (@1);
5496 tree c1_type = TREE_TYPE (@3), c2_type = TREE_TYPE (@2);
5497 enum tree_code code = ERROR_MARK;
5499 if (INTEGRAL_TYPE_P (from_type)
5500 && int_fits_type_p (@2, from_type)
5501 && (types_match (c1_type, from_type)
5502 || (TYPE_PRECISION (c1_type) > TYPE_PRECISION (from_type)
5503 && (TYPE_UNSIGNED (from_type)
5504 || TYPE_SIGN (c1_type) == TYPE_SIGN (from_type))))
5505 && (types_match (c2_type, from_type)
5506 || (TYPE_PRECISION (c2_type) > TYPE_PRECISION (from_type)
5507 && (TYPE_UNSIGNED (from_type)
5508 || TYPE_SIGN (c2_type) == TYPE_SIGN (from_type)))))
5511 code = minmax_from_comparison (cmp, @1, @3, @1, @2);
5512 /* Can do A == C1 ? A : C2 -> A == C1 ? C1 : C2? */
5513 else if (int_fits_type_p (@3, from_type))
5517 (if (code == MAX_EXPR)
5518 (convert (max @1 (convert @2)))
5519 (if (code == MIN_EXPR)
5520 (convert (min @1 (convert @2)))
5521 (if (code == EQ_EXPR)
5522 (convert (cond (eq @1 (convert @3))
5523 (convert:from_type @3) (convert:from_type @2)))))))))
5525 /* (cond (cmp (convert? x) c1) (op x c2) c3) -> (op (minmax x c1) c2) if:
5527 1) OP is PLUS or MINUS.
5528 2) CMP is LT, LE, GT or GE.
5529 3) C3 == (C1 op C2), and computation doesn't have undefined behavior.
5531 This pattern also handles special cases like:
5533 A) Operand x is a unsigned to signed type conversion and c1 is
5534 integer zero. In this case,
5535 (signed type)x < 0 <=> x > MAX_VAL(signed type)
5536 (signed type)x >= 0 <=> x <= MAX_VAL(signed type)
5537 B) Const c1 may not equal to (C3 op' C2). In this case we also
5538 check equality for (c1+1) and (c1-1) by adjusting comparison
5541 TODO: Though signed type is handled by this pattern, it cannot be
5542 simplified at the moment because C standard requires additional
5543 type promotion. In order to match&simplify it here, the IR needs
5544 to be cleaned up by other optimizers, i.e, VRP. */
5545 (for op (plus minus)
5546 (for cmp (lt le gt ge)
5548 (cond (cmp (convert? @X) INTEGER_CST@1) (op @X INTEGER_CST@2) INTEGER_CST@3)
5549 (with { tree from_type = TREE_TYPE (@X), to_type = TREE_TYPE (@1); }
5550 (if (types_match (from_type, to_type)
5551 /* Check if it is special case A). */
5552 || (TYPE_UNSIGNED (from_type)
5553 && !TYPE_UNSIGNED (to_type)
5554 && TYPE_PRECISION (from_type) == TYPE_PRECISION (to_type)
5555 && integer_zerop (@1)
5556 && (cmp == LT_EXPR || cmp == GE_EXPR)))
5559 wi::overflow_type overflow = wi::OVF_NONE;
5560 enum tree_code code, cmp_code = cmp;
5562 wide_int c1 = wi::to_wide (@1);
5563 wide_int c2 = wi::to_wide (@2);
5564 wide_int c3 = wi::to_wide (@3);
5565 signop sgn = TYPE_SIGN (from_type);
5567 /* Handle special case A), given x of unsigned type:
5568 ((signed type)x < 0) <=> (x > MAX_VAL(signed type))
5569 ((signed type)x >= 0) <=> (x <= MAX_VAL(signed type)) */
5570 if (!types_match (from_type, to_type))
5572 if (cmp_code == LT_EXPR)
5574 if (cmp_code == GE_EXPR)
5576 c1 = wi::max_value (to_type);
5578 /* To simplify this pattern, we require c3 = (c1 op c2). Here we
5579 compute (c3 op' c2) and check if it equals to c1 with op' being
5580 the inverted operator of op. Make sure overflow doesn't happen
5581 if it is undefined. */
5582 if (op == PLUS_EXPR)
5583 real_c1 = wi::sub (c3, c2, sgn, &overflow);
5585 real_c1 = wi::add (c3, c2, sgn, &overflow);
5588 if (!overflow || !TYPE_OVERFLOW_UNDEFINED (from_type))
5590 /* Check if c1 equals to real_c1. Boundary condition is handled
5591 by adjusting comparison operation if necessary. */
5592 if (!wi::cmp (wi::sub (real_c1, 1, sgn, &overflow), c1, sgn)
5595 /* X <= Y - 1 equals to X < Y. */
5596 if (cmp_code == LE_EXPR)
5598 /* X > Y - 1 equals to X >= Y. */
5599 if (cmp_code == GT_EXPR)
5602 if (!wi::cmp (wi::add (real_c1, 1, sgn, &overflow), c1, sgn)
5605 /* X < Y + 1 equals to X <= Y. */
5606 if (cmp_code == LT_EXPR)
5608 /* X >= Y + 1 equals to X > Y. */
5609 if (cmp_code == GE_EXPR)
5612 if (code != cmp_code || !wi::cmp (real_c1, c1, sgn))
5614 if (cmp_code == LT_EXPR || cmp_code == LE_EXPR)
5616 if (cmp_code == GT_EXPR || cmp_code == GE_EXPR)
5621 (if (code == MAX_EXPR)
5622 (op (max @X { wide_int_to_tree (from_type, real_c1); })
5623 { wide_int_to_tree (from_type, c2); })
5624 (if (code == MIN_EXPR)
5625 (op (min @X { wide_int_to_tree (from_type, real_c1); })
5626 { wide_int_to_tree (from_type, c2); })))))))))
5629 /* A >= B ? A : B -> max (A, B) and friends. The code is still
5630 in fold_cond_expr_with_comparison for GENERIC folding with
5631 some extra constraints. */
5632 (for cmp (eq ne le lt unle unlt ge gt unge ungt uneq ltgt)
5634 (cond (cmp:c (nop_convert1?@c0 @0) (nop_convert2?@c1 @1))
5635 (convert3? @0) (convert4? @1))
5636 (if (!HONOR_SIGNED_ZEROS (type)
5637 && (/* Allow widening conversions of the compare operands as data. */
5638 (INTEGRAL_TYPE_P (type)
5639 && types_match (TREE_TYPE (@c0), TREE_TYPE (@0))
5640 && types_match (TREE_TYPE (@c1), TREE_TYPE (@1))
5641 && TYPE_PRECISION (TREE_TYPE (@0)) <= TYPE_PRECISION (type)
5642 && TYPE_PRECISION (TREE_TYPE (@1)) <= TYPE_PRECISION (type))
5643 /* Or sign conversions for the comparison. */
5644 || (types_match (type, TREE_TYPE (@0))
5645 && types_match (type, TREE_TYPE (@1)))))
5647 (if (cmp == EQ_EXPR)
5648 (if (VECTOR_TYPE_P (type))
5651 (if (cmp == NE_EXPR)
5652 (if (VECTOR_TYPE_P (type))
5655 (if (cmp == LE_EXPR || cmp == UNLE_EXPR || cmp == LT_EXPR || cmp == UNLT_EXPR)
5656 (if (!HONOR_NANS (type))
5657 (if (VECTOR_TYPE_P (type))
5658 (view_convert (min @c0 @c1))
5659 (convert (min @c0 @c1)))))
5660 (if (cmp == GE_EXPR || cmp == UNGE_EXPR || cmp == GT_EXPR || cmp == UNGT_EXPR)
5661 (if (!HONOR_NANS (type))
5662 (if (VECTOR_TYPE_P (type))
5663 (view_convert (max @c0 @c1))
5664 (convert (max @c0 @c1)))))
5665 (if (cmp == UNEQ_EXPR)
5666 (if (!HONOR_NANS (type))
5667 (if (VECTOR_TYPE_P (type))
5670 (if (cmp == LTGT_EXPR)
5671 (if (!HONOR_NANS (type))
5672 (if (VECTOR_TYPE_P (type))
5674 (convert @c0))))))))
5677 (for cnd (cond vec_cond)
5678 /* (a != b) ? (a - b) : 0 -> (a - b) */
5680 (cnd (ne:c @0 @1) (minus@2 @0 @1) integer_zerop)
5682 /* (a != b) ? (a ^ b) : 0 -> (a ^ b) */
5684 (cnd (ne:c @0 @1) (bit_xor:c@2 @0 @1) integer_zerop)
5686 /* (a != b) ? (a & b) : a -> (a & b) */
5687 /* (a != b) ? (a | b) : a -> (a | b) */
5688 /* (a != b) ? min(a,b) : a -> min(a,b) */
5689 /* (a != b) ? max(a,b) : a -> max(a,b) */
5690 (for op (bit_and bit_ior min max)
5692 (cnd (ne:c @0 @1) (op:c@2 @0 @1) @0)
5694 /* (a != b) ? (a * b) : (a * a) -> (a * b) */
5695 /* (a != b) ? (a + b) : (a + a) -> (a + b) */
5698 (cnd (ne:c @0 @1) (op@2 @0 @1) (op @0 @0))
5699 (if (ANY_INTEGRAL_TYPE_P (type))
5701 /* (a != b) ? (a + b) : (2 * a) -> (a + b) */
5703 (cnd (ne:c @0 @1) (plus:c@2 @0 @1) (mult @0 uniform_integer_cst_p@3))
5704 (if (wi::to_wide (uniform_integer_cst_p (@3)) == 2)
5708 /* These was part of minmax phiopt. */
5709 /* Optimize (a CMP b) ? minmax<a, c> : minmax<b, c>
5710 to minmax<min/max<a, b>, c> */
5711 (for minmax (min max)
5712 (for cmp (lt le gt ge ne)
5714 (cond (cmp:c @1 @3) (minmax:c @1 @4) (minmax:c @2 @4))
5717 tree_code code = minmax_from_comparison (cmp, @1, @2, @1, @3);
5719 (if (code == MIN_EXPR)
5720 (minmax (min @1 @2) @4)
5721 (if (code == MAX_EXPR)
5722 (minmax (max @1 @2) @4)))))))
5724 /* Optimize (a CMP CST1) ? max<a,CST2> : a */
5725 (for cmp (gt ge lt le)
5726 minmax (min min max max)
5728 (cond (cmp:c @0 @1) (minmax:c@2 @0 @3) @4)
5731 tree_code code = minmax_from_comparison (cmp, @0, @1, @0, @4);
5733 (if ((cmp == LT_EXPR || cmp == LE_EXPR)
5735 && integer_nonzerop (fold_build2 (LE_EXPR, boolean_type_node, @3, @4)))
5737 (if ((cmp == GT_EXPR || cmp == GE_EXPR)
5739 && integer_nonzerop (fold_build2 (GE_EXPR, boolean_type_node, @3, @4)))
5743 /* These patterns should be after min/max detection as simplifications
5744 of `(type)(zero_one ==/!= 0)` to `(type)(zero_one)`
5745 and `(type)(zero_one^1)` are not done yet. See PR 110637.
5746 Even without those, reaching min/max/and/ior faster is better. */
5748 (cond @0 zero_one_valued_p@1 zero_one_valued_p@2)
5750 /* bool0 ? bool1 : 0 -> bool0 & bool1 */
5751 (if (integer_zerop (@2))
5752 (bit_and (convert @0) @1))
5753 /* bool0 ? 0 : bool2 -> (bool0^1) & bool2 */
5754 (if (integer_zerop (@1))
5755 (bit_and (bit_xor (convert @0) { build_one_cst (type); } ) @2))
5756 /* bool0 ? 1 : bool2 -> bool0 | bool2 */
5757 (if (integer_onep (@1))
5758 (bit_ior (convert @0) @2))
5759 /* bool0 ? bool1 : 1 -> (bool0^1) | bool1 */
5760 (if (integer_onep (@2))
5761 (bit_ior (bit_xor (convert @0) @2) @1))
5766 /* X != C1 ? -X : C2 simplifies to -X when -C1 == C2. */
5768 (cond (ne @0 INTEGER_CST@1) (negate@3 @0) INTEGER_CST@2)
5769 (if (!TYPE_SATURATING (type)
5770 && (TYPE_OVERFLOW_WRAPS (type)
5771 || !wi::only_sign_bit_p (wi::to_wide (@1)))
5772 && wi::eq_p (wi::neg (wi::to_wide (@1)), wi::to_wide (@2)))
5775 /* X != C1 ? ~X : C2 simplifies to ~X when ~C1 == C2. */
5777 (cond (ne @0 INTEGER_CST@1) (bit_not@3 @0) INTEGER_CST@2)
5778 (if (wi::eq_p (wi::bit_not (wi::to_wide (@1)), wi::to_wide (@2)))
5781 /* X != C1 ? abs(X) : C2 simplifies to abs(x) when abs(C1) == C2. */
5784 (cond (ne @0 INTEGER_CST@1) (op@3 @0) INTEGER_CST@2)
5785 (if (wi::abs (wi::to_wide (@1)) == wi::to_wide (@2))
5786 (if (op != ABSU_EXPR && wi::only_sign_bit_p (wi::to_wide (@1)))
5787 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
5788 (convert (absu:utype @0)))
5791 /* (X + 1) > Y ? -X : 1 simplifies to X >= Y ? -X : 1 when
5792 X is unsigned, as when X + 1 overflows, X is -1, so -X == 1. */
5794 (cond (gt (plus @0 integer_onep) @1) (negate @0) integer_onep@2)
5795 (if (TYPE_UNSIGNED (type))
5796 (cond (ge @0 @1) (negate @0) @2)))
5798 (for cnd (cond vec_cond)
5799 /* A ? B : (A ? X : C) -> A ? B : C. */
5801 (cnd @0 (cnd @0 @1 @2) @3)
5804 (cnd @0 @1 (cnd @0 @2 @3))
5806 /* A ? B : (!A ? C : X) -> A ? B : C. */
5807 /* ??? This matches embedded conditions open-coded because genmatch
5808 would generate matching code for conditions in separate stmts only.
5809 The following is still important to merge then and else arm cases
5810 from if-conversion. */
5812 (cnd @0 @1 (cnd @2 @3 @4))
5813 (if (inverse_conditions_p (@0, @2))
5816 (cnd @0 (cnd @1 @2 @3) @4)
5817 (if (inverse_conditions_p (@0, @1))
5820 /* A ? B : B -> B. */
5825 /* !A ? B : C -> A ? C : B. */
5827 (cnd (logical_inverted_value truth_valued_p@0) @1 @2)
5830 /* abs/negative simplifications moved from fold_cond_expr_with_comparison.
5832 None of these transformations work for modes with signed
5833 zeros. If A is +/-0, the first two transformations will
5834 change the sign of the result (from +0 to -0, or vice
5835 versa). The last four will fix the sign of the result,
5836 even though the original expressions could be positive or
5837 negative, depending on the sign of A.
5839 Note that all these transformations are correct if A is
5840 NaN, since the two alternatives (A and -A) are also NaNs. */
5842 (for cnd (cond vec_cond)
5843 /* A == 0 ? A : -A same as -A */
5846 (cnd (cmp @0 zerop) @2 (negate@1 @2))
5847 (if (!HONOR_SIGNED_ZEROS (type)
5848 && bitwise_equal_p (@0, @2))
5851 (cnd (cmp @0 zerop) zerop (negate@1 @2))
5852 (if (!HONOR_SIGNED_ZEROS (type)
5853 && bitwise_equal_p (@0, @2))
5856 /* A != 0 ? A : -A same as A */
5859 (cnd (cmp @0 zerop) @1 (negate @1))
5860 (if (!HONOR_SIGNED_ZEROS (type)
5861 && bitwise_equal_p (@0, @1))
5864 (cnd (cmp @0 zerop) @1 integer_zerop)
5865 (if (!HONOR_SIGNED_ZEROS (type)
5866 && bitwise_equal_p (@0, @1))
5869 /* A >=/> 0 ? A : -A same as abs (A) */
5872 (cnd (cmp @0 zerop) @1 (negate @1))
5873 (if (!HONOR_SIGNED_ZEROS (TREE_TYPE(@0))
5874 && !TYPE_UNSIGNED (TREE_TYPE(@0))
5875 && bitwise_equal_p (@0, @1))
5876 (if (TYPE_UNSIGNED (type))
5879 /* A <=/< 0 ? A : -A same as -abs (A) */
5882 (cnd (cmp @0 zerop) @1 (negate @1))
5883 (if (!HONOR_SIGNED_ZEROS (TREE_TYPE(@0))
5884 && !TYPE_UNSIGNED (TREE_TYPE(@0))
5885 && bitwise_equal_p (@0, @1))
5886 (if ((ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
5887 && !TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
5888 || TYPE_UNSIGNED (type))
5890 tree utype = unsigned_type_for (TREE_TYPE(@0));
5892 (convert (negate (absu:utype @0))))
5893 (negate (abs @0)))))
5896 /* (A - B) == 0 ? (A - B) : (B - A) same as (B - A) */
5899 (cnd (cmp (minus@0 @1 @2) zerop) @0 (minus@3 @2 @1))
5900 (if (!HONOR_SIGNED_ZEROS (type))
5903 (cnd (cmp (minus@0 @1 @2) integer_zerop) integer_zerop (minus@3 @2 @1))
5906 /* (A - B) != 0 ? (A - B) : (B - A) same as (A - B) */
5909 (cnd (cmp (minus@0 @1 @2) zerop) @0 (minus @2 @1))
5910 (if (!HONOR_SIGNED_ZEROS (type))
5913 (cnd (cmp (minus@0 @1 @2) integer_zerop) @0 integer_zerop)
5916 /* (A - B) >=/> 0 ? (A - B) : (B - A) same as abs (A - B) */
5919 (cnd (cmp (minus@0 @1 @2) zerop) @0 (minus @2 @1))
5920 (if (!HONOR_SIGNED_ZEROS (type)
5921 && !TYPE_UNSIGNED (type))
5923 /* (A - B) <=/< 0 ? (A - B) : (B - A) same as -abs (A - B) */
5926 (cnd (cmp (minus@0 @1 @2) zerop) @0 (minus @2 @1))
5927 (if (!HONOR_SIGNED_ZEROS (type)
5928 && !TYPE_UNSIGNED (type))
5929 (if (ANY_INTEGRAL_TYPE_P (type)
5930 && !TYPE_OVERFLOW_WRAPS (type))
5932 tree utype = unsigned_type_for (type);
5934 (convert (negate (absu:utype @0))))
5935 (negate (abs @0)))))
5939 /* -(type)!A -> (type)A - 1. */
5941 (negate (convert?:s (logical_inverted_value:s @0)))
5942 (if (INTEGRAL_TYPE_P (type)
5943 && TREE_CODE (type) != BOOLEAN_TYPE
5944 && TYPE_PRECISION (type) > 1
5945 && TREE_CODE (@0) == SSA_NAME
5946 && ssa_name_has_boolean_range (@0))
5947 (plus (convert:type @0) { build_all_ones_cst (type); })))
5949 /* A + (B vcmp C ? 1 : 0) -> A - (B vcmp C ? -1 : 0), since vector comparisons
5950 return all -1 or all 0 results. */
5951 /* ??? We could instead convert all instances of the vec_cond to negate,
5952 but that isn't necessarily a win on its own. */
5954 (plus:c @3 (view_convert? (vec_cond:s @0 integer_each_onep@1 integer_zerop@2)))
5955 (if (VECTOR_TYPE_P (type)
5956 && known_eq (TYPE_VECTOR_SUBPARTS (type),
5957 TYPE_VECTOR_SUBPARTS (TREE_TYPE (@1)))
5958 && (TYPE_MODE (TREE_TYPE (type))
5959 == TYPE_MODE (TREE_TYPE (TREE_TYPE (@1)))))
5960 (minus @3 (view_convert (vec_cond @0 (negate @1) @2)))))
5962 /* ... likewise A - (B vcmp C ? 1 : 0) -> A + (B vcmp C ? -1 : 0). */
5964 (minus @3 (view_convert? (vec_cond:s @0 integer_each_onep@1 integer_zerop@2)))
5965 (if (VECTOR_TYPE_P (type)
5966 && known_eq (TYPE_VECTOR_SUBPARTS (type),
5967 TYPE_VECTOR_SUBPARTS (TREE_TYPE (@1)))
5968 && (TYPE_MODE (TREE_TYPE (type))
5969 == TYPE_MODE (TREE_TYPE (TREE_TYPE (@1)))))
5970 (plus @3 (view_convert (vec_cond @0 (negate @1) @2)))))
5973 /* Simplifications of comparisons. */
5975 /* See if we can reduce the magnitude of a constant involved in a
5976 comparison by changing the comparison code. This is a canonicalization
5977 formerly done by maybe_canonicalize_comparison_1. */
5981 (cmp @0 uniform_integer_cst_p@1)
5982 (with { tree cst = uniform_integer_cst_p (@1); }
5983 (if (tree_int_cst_sgn (cst) == -1)
5984 (acmp @0 { build_uniform_cst (TREE_TYPE (@1),
5985 wide_int_to_tree (TREE_TYPE (cst),
5991 (cmp @0 uniform_integer_cst_p@1)
5992 (with { tree cst = uniform_integer_cst_p (@1); }
5993 (if (tree_int_cst_sgn (cst) == 1)
5994 (acmp @0 { build_uniform_cst (TREE_TYPE (@1),
5995 wide_int_to_tree (TREE_TYPE (cst),
5996 wi::to_wide (cst) - 1)); })))))
5998 /* We can simplify a logical negation of a comparison to the
5999 inverted comparison. As we cannot compute an expression
6000 operator using invert_tree_comparison we have to simulate
6001 that with expression code iteration. */
6002 (for cmp (tcc_comparison)
6003 icmp (inverted_tcc_comparison)
6004 ncmp (inverted_tcc_comparison_with_nans)
6005 /* Ideally we'd like to combine the following two patterns
6006 and handle some more cases by using
6007 (logical_inverted_value (cmp @0 @1))
6008 here but for that genmatch would need to "inline" that.
6009 For now implement what forward_propagate_comparison did. */
6011 (bit_not (cmp @0 @1))
6012 (if (VECTOR_TYPE_P (type)
6013 || (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1))
6014 /* Comparison inversion may be impossible for trapping math,
6015 invert_tree_comparison will tell us. But we can't use
6016 a computed operator in the replacement tree thus we have
6017 to play the trick below. */
6018 (with { enum tree_code ic = invert_tree_comparison
6019 (cmp, HONOR_NANS (@0)); }
6025 (bit_xor (cmp @0 @1) integer_truep)
6026 (with { enum tree_code ic = invert_tree_comparison
6027 (cmp, HONOR_NANS (@0)); }
6032 /* The following bits are handled by fold_binary_op_with_conditional_arg. */
6034 (ne (cmp@2 @0 @1) integer_zerop)
6035 (if (types_match (type, TREE_TYPE (@2)))
6038 (eq (cmp@2 @0 @1) integer_truep)
6039 (if (types_match (type, TREE_TYPE (@2)))
6042 (ne (cmp@2 @0 @1) integer_truep)
6043 (if (types_match (type, TREE_TYPE (@2)))
6044 (with { enum tree_code ic = invert_tree_comparison
6045 (cmp, HONOR_NANS (@0)); }
6051 (eq (cmp@2 @0 @1) integer_zerop)
6052 (if (types_match (type, TREE_TYPE (@2)))
6053 (with { enum tree_code ic = invert_tree_comparison
6054 (cmp, HONOR_NANS (@0)); }
6060 /* Transform comparisons of the form X - Y CMP 0 to X CMP Y.
6061 ??? The transformation is valid for the other operators if overflow
6062 is undefined for the type, but performing it here badly interacts
6063 with the transformation in fold_cond_expr_with_comparison which
6064 attempts to synthetize ABS_EXPR. */
6066 (for sub (minus pointer_diff)
6068 (cmp (sub@2 @0 @1) integer_zerop)
6069 (if (single_use (@2))
6072 /* Simplify (x < 0) ^ (y < 0) to (x ^ y) < 0 and
6073 (x >= 0) ^ (y >= 0) to (x ^ y) < 0. */
6076 (bit_xor (cmp:s @0 integer_zerop) (cmp:s @1 integer_zerop))
6077 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6078 && !TYPE_UNSIGNED (TREE_TYPE (@0))
6079 && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
6080 (lt (bit_xor @0 @1) { build_zero_cst (TREE_TYPE (@0)); }))))
6081 /* Simplify (x < 0) ^ (y >= 0) to (x ^ y) >= 0 and
6082 (x >= 0) ^ (y < 0) to (x ^ y) >= 0. */
6084 (bit_xor:c (lt:s @0 integer_zerop) (ge:s @1 integer_zerop))
6085 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6086 && !TYPE_UNSIGNED (TREE_TYPE (@0))
6087 && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
6088 (ge (bit_xor @0 @1) { build_zero_cst (TREE_TYPE (@0)); })))
6090 /* Transform comparisons of the form X * C1 CMP 0 to X CMP 0 in the
6091 signed arithmetic case. That form is created by the compiler
6092 often enough for folding it to be of value. One example is in
6093 computing loop trip counts after Operator Strength Reduction. */
6094 (for cmp (simple_comparison)
6095 scmp (swapped_simple_comparison)
6097 (cmp (mult@3 @0 INTEGER_CST@1) integer_zerop@2)
6098 /* Handle unfolded multiplication by zero. */
6099 (if (integer_zerop (@1))
6101 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
6102 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
6104 /* If @1 is negative we swap the sense of the comparison. */
6105 (if (tree_int_cst_sgn (@1) < 0)
6109 /* For integral types with undefined overflow fold
6110 x * C1 == C2 into x == C2 / C1 or false.
6111 If overflow wraps and C1 is odd, simplify to x == C2 / C1 in the ring
6115 (cmp (mult @0 INTEGER_CST@1) INTEGER_CST@2)
6116 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6117 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
6118 && wi::to_wide (@1) != 0)
6119 (with { widest_int quot; }
6120 (if (wi::multiple_of_p (wi::to_widest (@2), wi::to_widest (@1),
6121 TYPE_SIGN (TREE_TYPE (@0)), "))
6122 (cmp @0 { wide_int_to_tree (TREE_TYPE (@0), quot); })
6123 { constant_boolean_node (cmp == NE_EXPR, type); }))
6124 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6125 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))
6126 && (wi::bit_and (wi::to_wide (@1), 1) == 1))
6129 tree itype = TREE_TYPE (@0);
6130 int p = TYPE_PRECISION (itype);
6131 wide_int m = wi::one (p + 1) << p;
6132 wide_int a = wide_int::from (wi::to_wide (@1), p + 1, UNSIGNED);
6133 wide_int i = wide_int::from (wi::mod_inv (a, m),
6134 p, TYPE_SIGN (itype));
6135 wide_int_to_tree (itype, wi::mul (i, wi::to_wide (@2)));
6138 /* Simplify comparison of something with itself. For IEEE
6139 floating-point, we can only do some of these simplifications. */
6143 (if (! FLOAT_TYPE_P (TREE_TYPE (@0))
6144 || ! tree_expr_maybe_nan_p (@0))
6145 { constant_boolean_node (true, type); }
6147 /* With -ftrapping-math conversion to EQ loses an exception. */
6148 && (! FLOAT_TYPE_P (TREE_TYPE (@0))
6149 || ! flag_trapping_math))
6155 || ! FLOAT_TYPE_P (TREE_TYPE (@0))
6156 || ! tree_expr_maybe_nan_p (@0))
6157 { constant_boolean_node (false, type); })))
6158 (for cmp (unle unge uneq)
6161 { constant_boolean_node (true, type); }))
6162 (for cmp (unlt ungt)
6168 (if (!flag_trapping_math || !tree_expr_maybe_nan_p (@0))
6169 { constant_boolean_node (false, type); }))
6171 /* x == ~x -> false */
6172 /* x != ~x -> true */
6175 (cmp:c @0 (bit_not @0))
6176 { constant_boolean_node (cmp == NE_EXPR, type); }))
6178 /* Fold ~X op ~Y as Y op X. */
6179 (for cmp (simple_comparison)
6181 (cmp (nop_convert1?@4 (bit_not@2 @0)) (nop_convert2? (bit_not@3 @1)))
6182 (if (single_use (@2) && single_use (@3))
6183 (with { tree otype = TREE_TYPE (@4); }
6184 (cmp (convert:otype @1) (convert:otype @0))))))
6186 /* Fold ~X op C as X op' ~C, where op' is the swapped comparison. */
6187 (for cmp (simple_comparison)
6188 scmp (swapped_simple_comparison)
6190 (cmp (nop_convert? (bit_not@2 @0)) CONSTANT_CLASS_P@1)
6191 (if (single_use (@2)
6192 && (TREE_CODE (@1) == INTEGER_CST || TREE_CODE (@1) == VECTOR_CST))
6193 (with { tree otype = TREE_TYPE (@1); }
6194 (scmp (convert:otype @0) (bit_not @1))))))
6196 (for cmp (simple_comparison)
6199 /* IEEE doesn't distinguish +0 and -0 in comparisons. */
6201 /* a CMP (-0) -> a CMP 0 */
6202 (if (REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@1)))
6203 (cmp @0 { build_real (TREE_TYPE (@1), dconst0); }))
6204 /* (-0) CMP b -> 0 CMP b. */
6205 (if (TREE_CODE (@0) == REAL_CST
6206 && REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@0)))
6207 (cmp { build_real (TREE_TYPE (@0), dconst0); } @1))
6208 /* x != NaN is always true, other ops are always false. */
6209 (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
6210 && (cmp == EQ_EXPR || cmp == NE_EXPR || !flag_trapping_math)
6211 && !tree_expr_signaling_nan_p (@1)
6212 && !tree_expr_maybe_signaling_nan_p (@0))
6213 { constant_boolean_node (cmp == NE_EXPR, type); })
6214 /* NaN != y is always true, other ops are always false. */
6215 (if (TREE_CODE (@0) == REAL_CST
6216 && REAL_VALUE_ISNAN (TREE_REAL_CST (@0))
6217 && (cmp == EQ_EXPR || cmp == NE_EXPR || !flag_trapping_math)
6218 && !tree_expr_signaling_nan_p (@0)
6219 && !tree_expr_signaling_nan_p (@1))
6220 { constant_boolean_node (cmp == NE_EXPR, type); })
6221 /* Fold comparisons against infinity. */
6222 (if (REAL_VALUE_ISINF (TREE_REAL_CST (@1))
6223 && MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (@1))))
6226 REAL_VALUE_TYPE max;
6227 enum tree_code code = cmp;
6228 bool neg = REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1));
6230 code = swap_tree_comparison (code);
6233 /* x > +Inf is always false, if we ignore NaNs or exceptions. */
6234 (if (code == GT_EXPR
6235 && !(HONOR_NANS (@0) && flag_trapping_math))
6236 { constant_boolean_node (false, type); })
6237 (if (code == LE_EXPR)
6238 /* x <= +Inf is always true, if we don't care about NaNs. */
6239 (if (! HONOR_NANS (@0))
6240 { constant_boolean_node (true, type); }
6241 /* x <= +Inf is the same as x == x, i.e. !isnan(x), but this loses
6242 an "invalid" exception. */
6243 (if (!flag_trapping_math)
6245 /* x == +Inf and x >= +Inf are always equal to x > DBL_MAX, but
6246 for == this introduces an exception for x a NaN. */
6247 (if ((code == EQ_EXPR && !(HONOR_NANS (@0) && flag_trapping_math))
6249 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
6251 (lt @0 { build_real (TREE_TYPE (@0), max); })
6252 (gt @0 { build_real (TREE_TYPE (@0), max); }))))
6253 /* x < +Inf is always equal to x <= DBL_MAX. */
6254 (if (code == LT_EXPR)
6255 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
6257 (ge @0 { build_real (TREE_TYPE (@0), max); })
6258 (le @0 { build_real (TREE_TYPE (@0), max); }))))
6259 /* x != +Inf is always equal to !(x > DBL_MAX), but this introduces
6260 an exception for x a NaN so use an unordered comparison. */
6261 (if (code == NE_EXPR)
6262 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
6263 (if (! HONOR_NANS (@0))
6265 (ge @0 { build_real (TREE_TYPE (@0), max); })
6266 (le @0 { build_real (TREE_TYPE (@0), max); }))
6268 (unge @0 { build_real (TREE_TYPE (@0), max); })
6269 (unle @0 { build_real (TREE_TYPE (@0), max); }))))))))))
6271 /* If this is a comparison of a real constant with a PLUS_EXPR
6272 or a MINUS_EXPR of a real constant, we can convert it into a
6273 comparison with a revised real constant as long as no overflow
6274 occurs when unsafe_math_optimizations are enabled. */
6275 (if (flag_unsafe_math_optimizations)
6276 (for op (plus minus)
6278 (cmp (op @0 REAL_CST@1) REAL_CST@2)
6281 tree tem = const_binop (op == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR,
6282 TREE_TYPE (@1), @2, @1);
6284 (if (tem && !TREE_OVERFLOW (tem))
6285 (cmp @0 { tem; }))))))
6287 /* Likewise, we can simplify a comparison of a real constant with
6288 a MINUS_EXPR whose first operand is also a real constant, i.e.
6289 (c1 - x) < c2 becomes x > c1-c2. Reordering is allowed on
6290 floating-point types only if -fassociative-math is set. */
6291 (if (flag_associative_math)
6293 (cmp (minus REAL_CST@0 @1) REAL_CST@2)
6294 (with { tree tem = const_binop (MINUS_EXPR, TREE_TYPE (@1), @0, @2); }
6295 (if (tem && !TREE_OVERFLOW (tem))
6296 (cmp { tem; } @1)))))
6298 /* Fold comparisons against built-in math functions. */
6299 (if (flag_unsafe_math_optimizations && ! flag_errno_math)
6302 (cmp (sq @0) REAL_CST@1)
6304 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
6306 /* sqrt(x) < y is always false, if y is negative. */
6307 (if (cmp == EQ_EXPR || cmp == LT_EXPR || cmp == LE_EXPR)
6308 { constant_boolean_node (false, type); })
6309 /* sqrt(x) > y is always true, if y is negative and we
6310 don't care about NaNs, i.e. negative values of x. */
6311 (if (cmp == NE_EXPR || !HONOR_NANS (@0))
6312 { constant_boolean_node (true, type); })
6313 /* sqrt(x) > y is the same as x >= 0, if y is negative. */
6314 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })))
6315 (if (real_equal (TREE_REAL_CST_PTR (@1), &dconst0))
6317 /* sqrt(x) < 0 is always false. */
6318 (if (cmp == LT_EXPR)
6319 { constant_boolean_node (false, type); })
6320 /* sqrt(x) >= 0 is always true if we don't care about NaNs. */
6321 (if (cmp == GE_EXPR && !HONOR_NANS (@0))
6322 { constant_boolean_node (true, type); })
6323 /* sqrt(x) <= 0 -> x == 0. */
6324 (if (cmp == LE_EXPR)
6326 /* Otherwise sqrt(x) cmp 0 -> x cmp 0. Here cmp can be >=, >,
6327 == or !=. In the last case:
6329 (sqrt(x) != 0) == (NaN != 0) == true == (x != 0)
6331 if x is negative or NaN. Due to -funsafe-math-optimizations,
6332 the results for other x follow from natural arithmetic. */
6334 (if ((cmp == LT_EXPR
6338 && !REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
6339 /* Give up for -frounding-math. */
6340 && !HONOR_SIGN_DEPENDENT_ROUNDING (TREE_TYPE (@0)))
6344 enum tree_code ncmp = cmp;
6345 const real_format *fmt
6346 = REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (@0)));
6347 real_arithmetic (&c2, MULT_EXPR,
6348 &TREE_REAL_CST (@1), &TREE_REAL_CST (@1));
6349 real_convert (&c2, fmt, &c2);
6350 /* See PR91734: if c2 is inexact and sqrt(c2) < c (or sqrt(c2) >= c),
6351 then change LT_EXPR into LE_EXPR or GE_EXPR into GT_EXPR. */
6352 if (!REAL_VALUE_ISINF (c2))
6354 tree c3 = fold_const_call (CFN_SQRT, TREE_TYPE (@0),
6355 build_real (TREE_TYPE (@0), c2));
6356 if (c3 == NULL_TREE || TREE_CODE (c3) != REAL_CST)
6358 else if ((cmp == LT_EXPR || cmp == GE_EXPR)
6359 && real_less (&TREE_REAL_CST (c3), &TREE_REAL_CST (@1)))
6360 ncmp = cmp == LT_EXPR ? LE_EXPR : GT_EXPR;
6361 else if ((cmp == LE_EXPR || cmp == GT_EXPR)
6362 && real_less (&TREE_REAL_CST (@1), &TREE_REAL_CST (c3)))
6363 ncmp = cmp == LE_EXPR ? LT_EXPR : GE_EXPR;
6366 /* With rounding to even, sqrt of up to 3 different values
6367 gives the same normal result, so in some cases c2 needs
6369 REAL_VALUE_TYPE c2alt, tow;
6370 if (cmp == LT_EXPR || cmp == GE_EXPR)
6374 real_nextafter (&c2alt, fmt, &c2, &tow);
6375 real_convert (&c2alt, fmt, &c2alt);
6376 if (REAL_VALUE_ISINF (c2alt))
6380 c3 = fold_const_call (CFN_SQRT, TREE_TYPE (@0),
6381 build_real (TREE_TYPE (@0), c2alt));
6382 if (c3 == NULL_TREE || TREE_CODE (c3) != REAL_CST)
6384 else if (real_equal (&TREE_REAL_CST (c3),
6385 &TREE_REAL_CST (@1)))
6391 (if (cmp == GT_EXPR || cmp == GE_EXPR)
6392 (if (REAL_VALUE_ISINF (c2))
6393 /* sqrt(x) > y is x == +Inf, when y is very large. */
6394 (if (HONOR_INFINITIES (@0))
6395 (eq @0 { build_real (TREE_TYPE (@0), c2); })
6396 { constant_boolean_node (false, type); })
6397 /* sqrt(x) > c is the same as x > c*c. */
6398 (if (ncmp != ERROR_MARK)
6399 (if (ncmp == GE_EXPR)
6400 (ge @0 { build_real (TREE_TYPE (@0), c2); })
6401 (gt @0 { build_real (TREE_TYPE (@0), c2); }))))
6402 /* else if (cmp == LT_EXPR || cmp == LE_EXPR) */
6403 (if (REAL_VALUE_ISINF (c2))
6405 /* sqrt(x) < y is always true, when y is a very large
6406 value and we don't care about NaNs or Infinities. */
6407 (if (! HONOR_NANS (@0) && ! HONOR_INFINITIES (@0))
6408 { constant_boolean_node (true, type); })
6409 /* sqrt(x) < y is x != +Inf when y is very large and we
6410 don't care about NaNs. */
6411 (if (! HONOR_NANS (@0))
6412 (ne @0 { build_real (TREE_TYPE (@0), c2); }))
6413 /* sqrt(x) < y is x >= 0 when y is very large and we
6414 don't care about Infinities. */
6415 (if (! HONOR_INFINITIES (@0))
6416 (ge @0 { build_real (TREE_TYPE (@0), dconst0); }))
6417 /* sqrt(x) < y is x >= 0 && x != +Inf, when y is large. */
6420 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
6421 (ne @0 { build_real (TREE_TYPE (@0), c2); }))))
6422 /* sqrt(x) < c is the same as x < c*c, if we ignore NaNs. */
6423 (if (ncmp != ERROR_MARK && ! HONOR_NANS (@0))
6424 (if (ncmp == LT_EXPR)
6425 (lt @0 { build_real (TREE_TYPE (@0), c2); })
6426 (le @0 { build_real (TREE_TYPE (@0), c2); }))
6427 /* sqrt(x) < c is the same as x >= 0 && x < c*c. */
6428 (if (ncmp != ERROR_MARK && GENERIC)
6429 (if (ncmp == LT_EXPR)
6431 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
6432 (lt @0 { build_real (TREE_TYPE (@0), c2); }))
6434 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
6435 (le @0 { build_real (TREE_TYPE (@0), c2); })))))))))))
6436 /* Transform sqrt(x) cmp sqrt(y) -> x cmp y. */
6438 (cmp (sq @0) (sq @1))
6439 (if (! HONOR_NANS (@0))
6442 /* Optimize various special cases of (FTYPE) N CMP (FTYPE) M. */
6443 (for cmp (lt le eq ne ge gt unordered ordered unlt unle ungt unge uneq ltgt)
6444 icmp (lt le eq ne ge gt unordered ordered lt le gt ge eq ne)
6446 (cmp (float@0 @1) (float @2))
6447 (if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (@0))
6448 && ! DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0)))
6451 format_helper fmt (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (@0))));
6452 tree type1 = TREE_TYPE (@1);
6453 bool type1_signed_p = TYPE_SIGN (type1) == SIGNED;
6454 tree type2 = TREE_TYPE (@2);
6455 bool type2_signed_p = TYPE_SIGN (type2) == SIGNED;
6457 (if (fmt.can_represent_integral_type_p (type1)
6458 && fmt.can_represent_integral_type_p (type2))
6459 (if (cmp == ORDERED_EXPR || cmp == UNORDERED_EXPR)
6460 { constant_boolean_node (cmp == ORDERED_EXPR, type); }
6461 (if (TYPE_PRECISION (type1) > TYPE_PRECISION (type2)
6462 && type1_signed_p >= type2_signed_p)
6463 (icmp @1 (convert @2))
6464 (if (TYPE_PRECISION (type1) < TYPE_PRECISION (type2)
6465 && type1_signed_p <= type2_signed_p)
6466 (icmp (convert:type2 @1) @2)
6467 (if (TYPE_PRECISION (type1) == TYPE_PRECISION (type2)
6468 && type1_signed_p == type2_signed_p)
6469 (icmp @1 @2))))))))))
6471 /* Optimize various special cases of (FTYPE) N CMP CST. */
6472 (for cmp (lt le eq ne ge gt)
6473 icmp (le le eq ne ge ge)
6475 (cmp (float @0) REAL_CST@1)
6476 (if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (@1))
6477 && ! DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1)))
6480 tree itype = TREE_TYPE (@0);
6481 format_helper fmt (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (@1))));
6482 const REAL_VALUE_TYPE *cst = TREE_REAL_CST_PTR (@1);
6483 /* Be careful to preserve any potential exceptions due to
6484 NaNs. qNaNs are ok in == or != context.
6485 TODO: relax under -fno-trapping-math or
6486 -fno-signaling-nans. */
6488 = real_isnan (cst) && (cst->signalling
6489 || (cmp != EQ_EXPR && cmp != NE_EXPR));
6491 /* TODO: allow non-fitting itype and SNaNs when
6492 -fno-trapping-math. */
6493 (if (fmt.can_represent_integral_type_p (itype) && ! exception_p)
6496 signop isign = TYPE_SIGN (itype);
6497 REAL_VALUE_TYPE imin, imax;
6498 real_from_integer (&imin, fmt, wi::min_value (itype), isign);
6499 real_from_integer (&imax, fmt, wi::max_value (itype), isign);
6501 REAL_VALUE_TYPE icst;
6502 if (cmp == GT_EXPR || cmp == GE_EXPR)
6503 real_ceil (&icst, fmt, cst);
6504 else if (cmp == LT_EXPR || cmp == LE_EXPR)
6505 real_floor (&icst, fmt, cst);
6507 real_trunc (&icst, fmt, cst);
6509 bool cst_int_p = !real_isnan (cst) && real_identical (&icst, cst);
6511 bool overflow_p = false;
6513 = real_to_integer (&icst, &overflow_p, TYPE_PRECISION (itype));
6516 /* Optimize cases when CST is outside of ITYPE's range. */
6517 (if (real_compare (LT_EXPR, cst, &imin))
6518 { constant_boolean_node (cmp == GT_EXPR || cmp == GE_EXPR || cmp == NE_EXPR,
6520 (if (real_compare (GT_EXPR, cst, &imax))
6521 { constant_boolean_node (cmp == LT_EXPR || cmp == LE_EXPR || cmp == NE_EXPR,
6523 /* Remove cast if CST is an integer representable by ITYPE. */
6525 (cmp @0 { gcc_assert (!overflow_p);
6526 wide_int_to_tree (itype, icst_val); })
6528 /* When CST is fractional, optimize
6529 (FTYPE) N == CST -> 0
6530 (FTYPE) N != CST -> 1. */
6531 (if (cmp == EQ_EXPR || cmp == NE_EXPR)
6532 { constant_boolean_node (cmp == NE_EXPR, type); })
6533 /* Otherwise replace with sensible integer constant. */
6536 gcc_checking_assert (!overflow_p);
6538 (icmp @0 { wide_int_to_tree (itype, icst_val); })))))))))
6540 /* Fold A /[ex] B CMP C to A CMP B * C. */
6543 (cmp (exact_div @0 @1) INTEGER_CST@2)
6544 (if (!integer_zerop (@1))
6545 (if (wi::to_wide (@2) == 0)
6547 (if (TREE_CODE (@1) == INTEGER_CST)
6550 wi::overflow_type ovf;
6551 wide_int prod = wi::mul (wi::to_wide (@2), wi::to_wide (@1),
6552 TYPE_SIGN (TREE_TYPE (@1)), &ovf);
6555 { constant_boolean_node (cmp == NE_EXPR, type); }
6556 (cmp @0 { wide_int_to_tree (TREE_TYPE (@0), prod); }))))))))
6557 (for cmp (lt le gt ge)
6559 (cmp (exact_div @0 INTEGER_CST@1) INTEGER_CST@2)
6560 (if (wi::gt_p (wi::to_wide (@1), 0, TYPE_SIGN (TREE_TYPE (@1))))
6563 wi::overflow_type ovf;
6564 wide_int prod = wi::mul (wi::to_wide (@2), wi::to_wide (@1),
6565 TYPE_SIGN (TREE_TYPE (@1)), &ovf);
6568 { constant_boolean_node (wi::lt_p (wi::to_wide (@2), 0,
6569 TYPE_SIGN (TREE_TYPE (@2)))
6570 != (cmp == LT_EXPR || cmp == LE_EXPR), type); }
6571 (cmp @0 { wide_int_to_tree (TREE_TYPE (@0), prod); }))))))
6573 /* Fold (size_t)(A /[ex] B) CMP C to (size_t)A CMP (size_t)B * C or A CMP' 0.
6575 For small C (less than max/B), this is (size_t)A CMP (size_t)B * C.
6576 For large C (more than min/B+2^size), this is also true, with the
6577 multiplication computed modulo 2^size.
6578 For intermediate C, this just tests the sign of A. */
6579 (for cmp (lt le gt ge)
6582 (cmp (convert (exact_div @0 INTEGER_CST@1)) INTEGER_CST@2)
6583 (if (tree_nop_conversion_p (TREE_TYPE (@0), TREE_TYPE (@2))
6584 && TYPE_UNSIGNED (TREE_TYPE (@2)) && !TYPE_UNSIGNED (TREE_TYPE (@0))
6585 && wi::gt_p (wi::to_wide (@1), 0, TYPE_SIGN (TREE_TYPE (@1))))
6588 tree utype = TREE_TYPE (@2);
6589 wide_int denom = wi::to_wide (@1);
6590 wide_int right = wi::to_wide (@2);
6591 wide_int smax = wi::sdiv_trunc (wi::max_value (TREE_TYPE (@0)), denom);
6592 wide_int smin = wi::sdiv_trunc (wi::min_value (TREE_TYPE (@0)), denom);
6593 bool small = wi::leu_p (right, smax);
6594 bool large = wi::geu_p (right, smin);
6596 (if (small || large)
6597 (cmp (convert:utype @0) (mult @2 (convert @1)))
6598 (cmp2 @0 { build_zero_cst (TREE_TYPE (@0)); }))))))
6600 /* Unordered tests if either argument is a NaN. */
6602 (bit_ior (unordered @0 @0) (unordered @1 @1))
6603 (if (types_match (@0, @1))
6606 (bit_and (ordered @0 @0) (ordered @1 @1))
6607 (if (types_match (@0, @1))
6610 (bit_ior:c (unordered @0 @0) (unordered:c@2 @0 @1))
6613 (bit_and:c (ordered @0 @0) (ordered:c@2 @0 @1))
6616 /* A & (2**N - 1) <= 2**K - 1 -> A & (2**N - 2**K) == 0
6617 A & (2**N - 1) > 2**K - 1 -> A & (2**N - 2**K) != 0
6619 Note that comparisons
6620 A & (2**N - 1) < 2**K -> A & (2**N - 2**K) == 0
6621 A & (2**N - 1) >= 2**K -> A & (2**N - 2**K) != 0
6622 will be canonicalized to above so there's no need to
6629 (cmp (bit_and@0 @1 INTEGER_CST@2) INTEGER_CST@3)
6630 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)))
6633 tree ty = TREE_TYPE (@0);
6634 unsigned prec = TYPE_PRECISION (ty);
6635 wide_int mask = wi::to_wide (@2, prec);
6636 wide_int rhs = wi::to_wide (@3, prec);
6637 signop sgn = TYPE_SIGN (ty);
6639 (if ((mask & (mask + 1)) == 0 && wi::gt_p (rhs, 0, sgn)
6640 && (rhs & (rhs + 1)) == 0 && wi::ge_p (mask, rhs, sgn))
6641 (eqcmp (bit_and @1 { wide_int_to_tree (ty, mask - rhs); })
6642 { build_zero_cst (ty); }))))))
6644 /* -A CMP -B -> B CMP A. */
6645 (for cmp (tcc_comparison)
6646 scmp (swapped_tcc_comparison)
6648 (cmp (negate @0) (negate @1))
6649 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
6650 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
6653 || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))))
6656 (cmp (negate @0) CONSTANT_CLASS_P@1)
6657 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
6658 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
6661 || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))))
6662 (with { tree tem = const_unop (NEGATE_EXPR, TREE_TYPE (@0), @1); }
6663 (if (tem && !TREE_OVERFLOW (tem))
6664 (scmp @0 { tem; }))))))
6666 /* Convert ABS[U]_EXPR<x> == 0 or ABS[U]_EXPR<x> != 0 to x == 0 or x != 0. */
6670 (eqne (op @0) zerop@1)
6671 (eqne @0 { build_zero_cst (TREE_TYPE (@0)); }))))
6673 /* From fold_sign_changed_comparison and fold_widened_comparison.
6674 FIXME: the lack of symmetry is disturbing. */
6675 (for cmp (simple_comparison)
6677 (cmp (convert@0 @00) (convert?@1 @10))
6678 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6679 /* Disable this optimization if we're casting a function pointer
6680 type on targets that require function pointer canonicalization. */
6681 && !(targetm.have_canonicalize_funcptr_for_compare ()
6682 && ((POINTER_TYPE_P (TREE_TYPE (@00))
6683 && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@00))))
6684 || (POINTER_TYPE_P (TREE_TYPE (@10))
6685 && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@10))))))
6687 (if (TYPE_PRECISION (TREE_TYPE (@00)) == TYPE_PRECISION (TREE_TYPE (@0))
6688 && (TREE_CODE (@10) == INTEGER_CST
6690 && (TYPE_UNSIGNED (TREE_TYPE (@00)) == TYPE_UNSIGNED (TREE_TYPE (@0))
6693 && !POINTER_TYPE_P (TREE_TYPE (@00))
6694 /* (int)bool:32 != (int)uint is not the same as
6695 bool:32 != (bool:32)uint since boolean types only have two valid
6696 values independent of their precision. */
6697 && (TREE_CODE (TREE_TYPE (@00)) != BOOLEAN_TYPE
6698 || TREE_CODE (TREE_TYPE (@10)) == BOOLEAN_TYPE))
6699 /* ??? The special-casing of INTEGER_CST conversion was in the original
6700 code and here to avoid a spurious overflow flag on the resulting
6701 constant which fold_convert produces. */
6702 (if (TREE_CODE (@1) == INTEGER_CST)
6703 (cmp @00 { force_fit_type (TREE_TYPE (@00),
6704 wide_int::from (wi::to_wide (@1),
6705 MAX (TYPE_PRECISION (TREE_TYPE (@1)),
6706 TYPE_PRECISION (TREE_TYPE (@00))),
6707 TYPE_SIGN (TREE_TYPE (@1))),
6708 0, TREE_OVERFLOW (@1)); })
6709 (cmp @00 (convert @1)))
6711 (if (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (TREE_TYPE (@00)))
6712 /* If possible, express the comparison in the shorter mode. */
6713 (if ((cmp == EQ_EXPR || cmp == NE_EXPR
6714 || TYPE_UNSIGNED (TREE_TYPE (@0)) == TYPE_UNSIGNED (TREE_TYPE (@00))
6715 || (!TYPE_UNSIGNED (TREE_TYPE (@0))
6716 && TYPE_UNSIGNED (TREE_TYPE (@00))))
6717 && (types_match (TREE_TYPE (@10), TREE_TYPE (@00))
6718 || ((TYPE_PRECISION (TREE_TYPE (@00))
6719 >= TYPE_PRECISION (TREE_TYPE (@10)))
6720 && (TYPE_UNSIGNED (TREE_TYPE (@00))
6721 == TYPE_UNSIGNED (TREE_TYPE (@10))))
6722 || (TREE_CODE (@10) == INTEGER_CST
6723 && INTEGRAL_TYPE_P (TREE_TYPE (@00))
6724 && int_fits_type_p (@10, TREE_TYPE (@00)))))
6725 (cmp @00 (convert @10))
6726 (if (TREE_CODE (@10) == INTEGER_CST
6727 && INTEGRAL_TYPE_P (TREE_TYPE (@00))
6728 && !int_fits_type_p (@10, TREE_TYPE (@00)))
6731 tree min = lower_bound_in_type (TREE_TYPE (@10), TREE_TYPE (@00));
6732 tree max = upper_bound_in_type (TREE_TYPE (@10), TREE_TYPE (@00));
6733 bool above = integer_nonzerop (const_binop (LT_EXPR, type, max, @10));
6734 bool below = integer_nonzerop (const_binop (LT_EXPR, type, @10, min));
6736 (if (above || below)
6737 (if (cmp == EQ_EXPR || cmp == NE_EXPR)
6738 { constant_boolean_node (cmp == EQ_EXPR ? false : true, type); }
6739 (if (cmp == LT_EXPR || cmp == LE_EXPR)
6740 { constant_boolean_node (above ? true : false, type); }
6741 (if (cmp == GT_EXPR || cmp == GE_EXPR)
6742 { constant_boolean_node (above ? false : true, type); })))))))))
6743 /* Fold (double)float1 CMP (double)float2 into float1 CMP float2. */
6744 (if (FLOAT_TYPE_P (TREE_TYPE (@00))
6745 && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0))
6746 == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@00)))
6747 && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0))
6748 == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@10))))
6751 tree type1 = TREE_TYPE (@10);
6752 if (TREE_CODE (@10) == REAL_CST && !DECIMAL_FLOAT_TYPE_P (type1))
6754 REAL_VALUE_TYPE orig = TREE_REAL_CST (@10);
6755 if (TYPE_PRECISION (type1) > TYPE_PRECISION (float_type_node)
6756 && exact_real_truncate (TYPE_MODE (float_type_node), &orig))
6757 type1 = float_type_node;
6758 if (TYPE_PRECISION (type1) > TYPE_PRECISION (double_type_node)
6759 && exact_real_truncate (TYPE_MODE (double_type_node), &orig))
6760 type1 = double_type_node;
6763 = (element_precision (TREE_TYPE (@00)) > element_precision (type1)
6764 ? TREE_TYPE (@00) : type1);
6766 (if (element_precision (TREE_TYPE (@0)) > element_precision (newtype))
6767 (cmp (convert:newtype @00) (convert:newtype @10))))))))
6772 /* SSA names are canonicalized to 2nd place. */
6773 (cmp addr@0 SSA_NAME@1)
6776 poly_int64 off; tree base;
6777 tree addr = (TREE_CODE (@0) == SSA_NAME
6778 ? gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0)) : @0);
6780 /* A local variable can never be pointed to by
6781 the default SSA name of an incoming parameter. */
6782 (if (SSA_NAME_IS_DEFAULT_DEF (@1)
6783 && TREE_CODE (SSA_NAME_VAR (@1)) == PARM_DECL
6784 && (base = get_base_address (TREE_OPERAND (addr, 0)))
6785 && TREE_CODE (base) == VAR_DECL
6786 && auto_var_in_fn_p (base, current_function_decl))
6787 (if (cmp == NE_EXPR)
6788 { constant_boolean_node (true, type); }
6789 { constant_boolean_node (false, type); })
6790 /* If the address is based on @1 decide using the offset. */
6791 (if ((base = get_addr_base_and_unit_offset (TREE_OPERAND (addr, 0), &off))
6792 && TREE_CODE (base) == MEM_REF
6793 && TREE_OPERAND (base, 0) == @1)
6794 (with { off += mem_ref_offset (base).force_shwi (); }
6795 (if (known_ne (off, 0))
6796 { constant_boolean_node (cmp == NE_EXPR, type); }
6797 (if (known_eq (off, 0))
6798 { constant_boolean_node (cmp == EQ_EXPR, type); }))))))))
6800 /* Equality compare simplifications from fold_binary */
6803 /* If we have (A | C) == D where C & ~D != 0, convert this into 0.
6804 Similarly for NE_EXPR. */
6806 (cmp (convert?@3 (bit_ior @0 INTEGER_CST@1)) INTEGER_CST@2)
6807 (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0))
6808 && wi::bit_and_not (wi::to_wide (@1), wi::to_wide (@2)) != 0)
6809 { constant_boolean_node (cmp == NE_EXPR, type); }))
6811 /* (X ^ Y) == 0 becomes X == Y, and (X ^ Y) != 0 becomes X != Y. */
6813 (cmp (bit_xor @0 @1) integer_zerop)
6816 /* (X ^ Y) == Y becomes X == 0.
6817 Likewise (X ^ Y) == X becomes Y == 0. */
6819 (cmp:c (bit_xor:c @0 @1) @0)
6820 (cmp @1 { build_zero_cst (TREE_TYPE (@1)); }))
6822 /* (X & Y) == X becomes (X & ~Y) == 0. */
6824 (cmp:c (bit_and:c @0 @1) @0)
6825 (cmp (bit_and @0 (bit_not! @1)) { build_zero_cst (TREE_TYPE (@0)); }))
6827 (cmp:c (convert@3 (bit_and (convert@2 @0) INTEGER_CST@1)) (convert @0))
6828 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6829 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
6830 && INTEGRAL_TYPE_P (TREE_TYPE (@3))
6831 && TYPE_PRECISION (TREE_TYPE (@2)) == TYPE_PRECISION (TREE_TYPE (@0))
6832 && TYPE_PRECISION (TREE_TYPE (@3)) > TYPE_PRECISION (TREE_TYPE (@2))
6833 && !wi::neg_p (wi::to_wide (@1)))
6834 (cmp (bit_and @0 (convert (bit_not @1)))
6835 { build_zero_cst (TREE_TYPE (@0)); })))
6837 /* (X | Y) == Y becomes (X & ~Y) == 0. */
6839 (cmp:c (bit_ior:c @0 @1) @1)
6840 (cmp (bit_and @0 (bit_not! @1)) { build_zero_cst (TREE_TYPE (@0)); }))
6842 /* (X ^ C1) op C2 can be rewritten as X op (C1 ^ C2). */
6844 (cmp (convert?@3 (bit_xor @0 INTEGER_CST@1)) INTEGER_CST@2)
6845 (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0)))
6846 (cmp @0 (bit_xor @1 (convert @2)))))
6849 (cmp (nop_convert? @0) integer_zerop)
6850 (if (tree_expr_nonzero_p (@0))
6851 { constant_boolean_node (cmp == NE_EXPR, type); }))
6853 /* (X & C) op (Y & C) into (X ^ Y) & C op 0. */
6855 (cmp (bit_and:cs @0 @2) (bit_and:cs @1 @2))
6856 (cmp (bit_and (bit_xor @0 @1) @2) { build_zero_cst (TREE_TYPE (@2)); })))
6858 /* (X < 0) != (Y < 0) into (X ^ Y) < 0.
6859 (X >= 0) != (Y >= 0) into (X ^ Y) < 0.
6860 (X < 0) == (Y < 0) into (X ^ Y) >= 0.
6861 (X >= 0) == (Y >= 0) into (X ^ Y) >= 0. */
6866 (cmp (sgncmp @0 integer_zerop@2) (sgncmp @1 integer_zerop))
6867 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
6868 && !TYPE_UNSIGNED (TREE_TYPE (@0))
6869 && types_match (@0, @1))
6870 (ncmp (bit_xor @0 @1) @2)))))
6871 /* (X < 0) == (Y >= 0) into (X ^ Y) < 0.
6872 (X < 0) != (Y >= 0) into (X ^ Y) >= 0. */
6876 (cmp:c (lt @0 integer_zerop@2) (ge @1 integer_zerop))
6877 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
6878 && !TYPE_UNSIGNED (TREE_TYPE (@0))
6879 && types_match (@0, @1))
6880 (ncmp (bit_xor @0 @1) @2))))
6882 /* If we have (A & C) == C where C is a power of 2, convert this into
6883 (A & C) != 0. Similarly for NE_EXPR. */
6887 (cmp (bit_and@2 @0 integer_pow2p@1) @1)
6888 (icmp @2 { build_zero_cst (TREE_TYPE (@0)); })))
6891 /* From fold_binary_op_with_conditional_arg handle the case of
6892 rewriting (a ? b : c) > d to a ? (b > d) : (c > d) when the
6893 compares simplify. */
6894 (for cmp (simple_comparison)
6896 (cmp:c (cond @0 @1 @2) @3)
6897 /* Do not move possibly trapping operations into the conditional as this
6898 pessimizes code and causes gimplification issues when applied late. */
6899 (if (!FLOAT_TYPE_P (TREE_TYPE (@3))
6900 || !operation_could_trap_p (cmp, true, false, @3))
6901 (cond @0 (cmp! @1 @3) (cmp! @2 @3)))))
6905 /* x < 0 ? ~y : y into (x >> (prec-1)) ^ y. */
6906 /* x >= 0 ? ~y : y into ~((x >> (prec-1)) ^ y). */
6908 (cond (cmp @0 integer_zerop) (bit_not @1) @1)
6909 (if (INTEGRAL_TYPE_P (type)
6910 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
6911 && !TYPE_UNSIGNED (TREE_TYPE (@0))
6912 && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (type))
6915 tree shifter = build_int_cst (integer_type_node, TYPE_PRECISION (type) - 1);
6917 (if (cmp == LT_EXPR)
6918 (bit_xor (convert (rshift @0 {shifter;})) @1)
6919 (bit_not (bit_xor (convert (rshift @0 {shifter;})) @1))))))
6920 /* x < 0 ? y : ~y into ~((x >> (prec-1)) ^ y). */
6921 /* x >= 0 ? y : ~y into (x >> (prec-1)) ^ y. */
6923 (cond (cmp @0 integer_zerop) @1 (bit_not @1))
6924 (if (INTEGRAL_TYPE_P (type)
6925 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
6926 && !TYPE_UNSIGNED (TREE_TYPE (@0))
6927 && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (type))
6930 tree shifter = build_int_cst (integer_type_node, TYPE_PRECISION (type) - 1);
6932 (if (cmp == GE_EXPR)
6933 (bit_xor (convert (rshift @0 {shifter;})) @1)
6934 (bit_not (bit_xor (convert (rshift @0 {shifter;})) @1)))))))
6936 /* If we have (A & C) != 0 ? D : 0 where C and D are powers of 2,
6937 convert this into a shift followed by ANDing with D. */
6940 (ne (bit_and @0 integer_pow2p@1) integer_zerop)
6941 INTEGER_CST@2 integer_zerop)
6942 (if (!POINTER_TYPE_P (type) && integer_pow2p (@2))
6944 int shift = (wi::exact_log2 (wi::to_wide (@2))
6945 - wi::exact_log2 (wi::to_wide (@1)));
6949 (lshift (convert @0) { build_int_cst (integer_type_node, shift); }) @2)
6951 (convert (rshift @0 { build_int_cst (integer_type_node, -shift); }))
6954 /* If we have (A & C) != 0 where C is the sign bit of A, convert
6955 this into A < 0. Similarly for (A & C) == 0 into A >= 0. */
6959 (cmp (bit_and (convert?@2 @0) integer_pow2p@1) integer_zerop)
6960 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6961 && type_has_mode_precision_p (TREE_TYPE (@0))
6962 && element_precision (@2) >= element_precision (@0)
6963 && wi::only_sign_bit_p (wi::to_wide (@1), element_precision (@0)))
6964 (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
6965 (ncmp (convert:stype @0) { build_zero_cst (stype); })))))
6967 /* If we have A < 0 ? C : 0 where C is a power of 2, convert
6968 this into a right shift or sign extension followed by ANDing with C. */
6971 (lt @0 integer_zerop)
6972 INTEGER_CST@1 integer_zerop)
6973 (if (integer_pow2p (@1)
6974 && !TYPE_UNSIGNED (TREE_TYPE (@0)))
6976 int shift = element_precision (@0) - wi::exact_log2 (wi::to_wide (@1)) - 1;
6980 (convert (rshift @0 { build_int_cst (integer_type_node, shift); }))
6982 /* Otherwise ctype must be wider than TREE_TYPE (@0) and pure
6983 sign extension followed by AND with C will achieve the effect. */
6984 (bit_and (convert @0) @1)))))
6986 /* When the addresses are not directly of decls compare base and offset.
6987 This implements some remaining parts of fold_comparison address
6988 comparisons but still no complete part of it. Still it is good
6989 enough to make fold_stmt not regress when not dispatching to fold_binary. */
6990 (for cmp (simple_comparison)
6992 (cmp (convert1?@2 addr@0) (convert2? addr@1))
6995 poly_int64 off0, off1;
6997 int equal = address_compare (cmp, TREE_TYPE (@2), @0, @1, base0, base1,
6998 off0, off1, GENERIC);
7002 (if (cmp == EQ_EXPR && (known_eq (off0, off1) || known_ne (off0, off1)))
7003 { constant_boolean_node (known_eq (off0, off1), type); })
7004 (if (cmp == NE_EXPR && (known_eq (off0, off1) || known_ne (off0, off1)))
7005 { constant_boolean_node (known_ne (off0, off1), type); })
7006 (if (cmp == LT_EXPR && (known_lt (off0, off1) || known_ge (off0, off1)))
7007 { constant_boolean_node (known_lt (off0, off1), type); })
7008 (if (cmp == LE_EXPR && (known_le (off0, off1) || known_gt (off0, off1)))
7009 { constant_boolean_node (known_le (off0, off1), type); })
7010 (if (cmp == GE_EXPR && (known_ge (off0, off1) || known_lt (off0, off1)))
7011 { constant_boolean_node (known_ge (off0, off1), type); })
7012 (if (cmp == GT_EXPR && (known_gt (off0, off1) || known_le (off0, off1)))
7013 { constant_boolean_node (known_gt (off0, off1), type); }))
7016 (if (cmp == EQ_EXPR)
7017 { constant_boolean_node (false, type); })
7018 (if (cmp == NE_EXPR)
7019 { constant_boolean_node (true, type); })))))))
7022 /* a?~t:t -> (-(a))^t */
7025 (with { bool wascmp; }
7026 (if (INTEGRAL_TYPE_P (type)
7027 && bitwise_inverted_equal_p (@1, @2, wascmp)
7028 && (!wascmp || TYPE_PRECISION (type) == 1))
7029 (if ((!TYPE_UNSIGNED (type) && TREE_CODE (type) == BOOLEAN_TYPE)
7030 || TYPE_PRECISION (type) == 1)
7031 (bit_xor (convert:type @0) @2)
7032 (bit_xor (negate (convert:type @0)) @2)))))
7035 /* Simplify pointer equality compares using PTA. */
7039 (if (POINTER_TYPE_P (TREE_TYPE (@0))
7040 && ptrs_compare_unequal (@0, @1))
7041 { constant_boolean_node (neeq != EQ_EXPR, type); })))
7043 /* PR70920: Transform (intptr_t)x eq/ne CST to x eq/ne (typeof x) CST.
7044 and (typeof ptr_cst) x eq/ne ptr_cst to x eq/ne (typeof x) CST.
7045 Disable the transform if either operand is pointer to function.
7046 This broke pr22051-2.c for arm where function pointer
7047 canonicalizaion is not wanted. */
7051 (cmp (convert @0) INTEGER_CST@1)
7052 (if (((POINTER_TYPE_P (TREE_TYPE (@0))
7053 && !FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@0)))
7054 && INTEGRAL_TYPE_P (TREE_TYPE (@1))
7055 /* Don't perform this optimization in GENERIC if @0 has reference
7056 type when sanitizing. See PR101210. */
7058 && TREE_CODE (TREE_TYPE (@0)) == REFERENCE_TYPE
7059 && (flag_sanitize & (SANITIZE_NULL | SANITIZE_ALIGNMENT))))
7060 || (INTEGRAL_TYPE_P (TREE_TYPE (@0))
7061 && POINTER_TYPE_P (TREE_TYPE (@1))
7062 && !FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@1)))))
7063 && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
7064 (cmp @0 (convert @1)))))
7066 /* Non-equality compare simplifications from fold_binary */
7067 (for cmp (lt gt le ge)
7068 /* Comparisons with the highest or lowest possible integer of
7069 the specified precision will have known values. */
7071 (cmp (convert?@2 @0) uniform_integer_cst_p@1)
7072 (if ((INTEGRAL_TYPE_P (TREE_TYPE (@1))
7073 || POINTER_TYPE_P (TREE_TYPE (@1))
7074 || VECTOR_INTEGER_TYPE_P (TREE_TYPE (@1)))
7075 && tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@0)))
7078 tree cst = uniform_integer_cst_p (@1);
7079 tree arg1_type = TREE_TYPE (cst);
7080 unsigned int prec = TYPE_PRECISION (arg1_type);
7081 wide_int max = wi::max_value (arg1_type);
7082 wide_int signed_max = wi::max_value (prec, SIGNED);
7083 wide_int min = wi::min_value (arg1_type);
7086 (if (wi::to_wide (cst) == max)
7088 (if (cmp == GT_EXPR)
7089 { constant_boolean_node (false, type); })
7090 (if (cmp == GE_EXPR)
7092 (if (cmp == LE_EXPR)
7093 { constant_boolean_node (true, type); })
7094 (if (cmp == LT_EXPR)
7096 (if (wi::to_wide (cst) == min)
7098 (if (cmp == LT_EXPR)
7099 { constant_boolean_node (false, type); })
7100 (if (cmp == LE_EXPR)
7102 (if (cmp == GE_EXPR)
7103 { constant_boolean_node (true, type); })
7104 (if (cmp == GT_EXPR)
7106 (if (wi::to_wide (cst) == max - 1)
7108 (if (cmp == GT_EXPR)
7109 (eq @2 { build_uniform_cst (TREE_TYPE (@1),
7110 wide_int_to_tree (TREE_TYPE (cst),
7113 (if (cmp == LE_EXPR)
7114 (ne @2 { build_uniform_cst (TREE_TYPE (@1),
7115 wide_int_to_tree (TREE_TYPE (cst),
7118 (if (wi::to_wide (cst) == min + 1)
7120 (if (cmp == GE_EXPR)
7121 (ne @2 { build_uniform_cst (TREE_TYPE (@1),
7122 wide_int_to_tree (TREE_TYPE (cst),
7125 (if (cmp == LT_EXPR)
7126 (eq @2 { build_uniform_cst (TREE_TYPE (@1),
7127 wide_int_to_tree (TREE_TYPE (cst),
7130 (if (wi::to_wide (cst) == signed_max
7131 && TYPE_UNSIGNED (arg1_type)
7132 && TYPE_MODE (arg1_type) != BLKmode
7133 /* We will flip the signedness of the comparison operator
7134 associated with the mode of @1, so the sign bit is
7135 specified by this mode. Check that @1 is the signed
7136 max associated with this sign bit. */
7137 && prec == GET_MODE_PRECISION (SCALAR_INT_TYPE_MODE (arg1_type))
7138 /* signed_type does not work on pointer types. */
7139 && INTEGRAL_TYPE_P (arg1_type))
7140 /* The following case also applies to X < signed_max+1
7141 and X >= signed_max+1 because previous transformations. */
7142 (if (cmp == LE_EXPR || cmp == GT_EXPR)
7143 (with { tree st = signed_type_for (TREE_TYPE (@1)); }
7145 (if (cst == @1 && cmp == LE_EXPR)
7146 (ge (convert:st @0) { build_zero_cst (st); }))
7147 (if (cst == @1 && cmp == GT_EXPR)
7148 (lt (convert:st @0) { build_zero_cst (st); }))
7149 (if (cmp == LE_EXPR)
7150 (ge (view_convert:st @0) { build_zero_cst (st); }))
7151 (if (cmp == GT_EXPR)
7152 (lt (view_convert:st @0) { build_zero_cst (st); })))))))))))
7154 /* unsigned < (typeof unsigned)(unsigned != 0) is always false. */
7156 (lt:c @0 (convert (ne @0 integer_zerop)))
7157 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
7158 { constant_boolean_node (false, type); }))
7160 /* x != (typeof x)(x == CST) -> CST == 0 ? 1 : (CST == 1 ? (x!=0&&x!=1) : x != 0) */
7161 /* x != (typeof x)(x != CST) -> CST == 1 ? 1 : (CST == 0 ? (x!=0&&x!=1) : x != 1) */
7162 /* x == (typeof x)(x == CST) -> CST == 0 ? 0 : (CST == 1 ? (x==0||x==1) : x == 0) */
7163 /* x == (typeof x)(x != CST) -> CST == 1 ? 0 : (CST == 0 ? (x==0||x==1) : x == 1) */
7167 (outer:c @0 (convert (inner @0 INTEGER_CST@1)))
7169 bool cst1 = integer_onep (@1);
7170 bool cst0 = integer_zerop (@1);
7171 bool innereq = inner == EQ_EXPR;
7172 bool outereq = outer == EQ_EXPR;
7175 (if (innereq ? cst0 : cst1)
7176 { constant_boolean_node (!outereq, type); })
7177 (if (innereq ? cst1 : cst0)
7179 tree utype = unsigned_type_for (TREE_TYPE (@0));
7180 tree ucst1 = build_one_cst (utype);
7183 (gt (convert:utype @0) { ucst1; })
7184 (le (convert:utype @0) { ucst1; })
7189 tree value = build_int_cst (TREE_TYPE (@0), !innereq);
7202 (for cmp (unordered ordered unlt unle ungt unge uneq ltgt)
7203 /* If the second operand is NaN, the result is constant. */
7206 (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
7207 && (cmp != LTGT_EXPR || ! flag_trapping_math))
7208 { constant_boolean_node (cmp == ORDERED_EXPR || cmp == LTGT_EXPR
7209 ? false : true, type); })))
7211 /* Fold UNORDERED if either operand must be NaN, or neither can be. */
7215 (if (tree_expr_nan_p (@0) || tree_expr_nan_p (@1))
7216 { constant_boolean_node (true, type); })
7217 (if (!tree_expr_maybe_nan_p (@0) && !tree_expr_maybe_nan_p (@1))
7218 { constant_boolean_node (false, type); })))
7220 /* Fold ORDERED if either operand must be NaN, or neither can be. */
7224 (if (tree_expr_nan_p (@0) || tree_expr_nan_p (@1))
7225 { constant_boolean_node (false, type); })
7226 (if (!tree_expr_maybe_nan_p (@0) && !tree_expr_maybe_nan_p (@1))
7227 { constant_boolean_node (true, type); })))
7229 /* bool_var != 0 becomes bool_var. */
7231 (ne @0 integer_zerop)
7232 (if (TREE_CODE (TREE_TYPE (@0)) == BOOLEAN_TYPE
7233 && types_match (type, TREE_TYPE (@0)))
7235 /* bool_var == 1 becomes bool_var. */
7237 (eq @0 integer_onep)
7238 (if (TREE_CODE (TREE_TYPE (@0)) == BOOLEAN_TYPE
7239 && types_match (type, TREE_TYPE (@0)))
7242 bool_var == 0 becomes !bool_var or
7243 bool_var != 1 becomes !bool_var
7244 here because that only is good in assignment context as long
7245 as we require a tcc_comparison in GIMPLE_CONDs where we'd
7246 replace if (x == 0) with tem = ~x; if (tem != 0) which is
7247 clearly less optimal and which we'll transform again in forwprop. */
7249 /* Transform comparisons of the form (X & Y) CMP 0 to X CMP2 Z
7250 where ~Y + 1 == pow2 and Z = ~Y. */
7251 (for cst (VECTOR_CST INTEGER_CST)
7255 (cmp (bit_and:c@2 @0 cst@1) integer_zerop)
7256 (with { tree csts = bitmask_inv_cst_vector_p (@1); }
7257 (if (csts && (VECTOR_TYPE_P (TREE_TYPE (@1)) || single_use (@2)))
7258 (with { auto optab = VECTOR_TYPE_P (TREE_TYPE (@1))
7259 ? optab_vector : optab_default;
7260 tree utype = unsigned_type_for (TREE_TYPE (@1)); }
7261 (if (target_supports_op_p (utype, icmp, optab)
7262 || (optimize_vectors_before_lowering_p ()
7263 && (!target_supports_op_p (type, cmp, optab)
7264 || !target_supports_op_p (type, BIT_AND_EXPR, optab))))
7265 (if (TYPE_UNSIGNED (TREE_TYPE (@1)))
7267 (icmp (view_convert:utype @0) { csts; })))))))))
7269 /* When one argument is a constant, overflow detection can be simplified.
7270 Currently restricted to single use so as not to interfere too much with
7271 ADD_OVERFLOW detection in tree-ssa-math-opts.cc.
7272 CONVERT?(CONVERT?(A) + CST) CMP A -> A CMP' CST' */
7273 (for cmp (lt le ge gt)
7276 (cmp:c (convert?@3 (plus@2 (convert?@4 @0) INTEGER_CST@1)) @0)
7277 (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@2))
7278 && types_match (TREE_TYPE (@0), TREE_TYPE (@3))
7279 && tree_nop_conversion_p (TREE_TYPE (@4), TREE_TYPE (@0))
7280 && wi::to_wide (@1) != 0
7283 unsigned int prec = TYPE_PRECISION (TREE_TYPE (@0));
7284 signop sign = TYPE_SIGN (TREE_TYPE (@0));
7286 (out @0 { wide_int_to_tree (TREE_TYPE (@0),
7287 wi::max_value (prec, sign)
7288 - wi::to_wide (@1)); })))))
7290 /* To detect overflow in unsigned A - B, A < B is simpler than A - B > A.
7291 However, the detection logic for SUB_OVERFLOW in tree-ssa-math-opts.cc
7292 expects the long form, so we restrict the transformation for now. */
7295 (cmp:c (minus@2 @0 @1) @0)
7296 (if (single_use (@2)
7297 && ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
7298 && TYPE_UNSIGNED (TREE_TYPE (@0)))
7301 /* Optimize A - B + -1 >= A into B >= A for unsigned comparisons. */
7304 (cmp:c (plus (minus @0 @1) integer_minus_onep) @0)
7305 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
7306 && TYPE_UNSIGNED (TREE_TYPE (@0)))
7309 /* Testing for overflow is unnecessary if we already know the result. */
7314 (cmp:c (realpart (IFN_SUB_OVERFLOW@2 @0 @1)) @0)
7315 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
7316 && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
7317 (out (imagpart @2) { build_zero_cst (TREE_TYPE (@0)); }))))
7322 (cmp:c (realpart (IFN_ADD_OVERFLOW:c@2 @0 @1)) @0)
7323 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
7324 && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
7325 (out (imagpart @2) { build_zero_cst (TREE_TYPE (@0)); }))))
7327 /* For unsigned operands, -1 / B < A checks whether A * B would overflow.
7328 Simplify it to __builtin_mul_overflow (A, B, <unused>). */
7332 (cmp:c (trunc_div:s integer_all_onesp @1) @0)
7333 (if (TYPE_UNSIGNED (TREE_TYPE (@0)) && !VECTOR_TYPE_P (TREE_TYPE (@0)))
7334 (with { tree t = TREE_TYPE (@0), cpx = build_complex_type (t); }
7335 (out (imagpart (IFN_MUL_OVERFLOW:cpx @0 @1)) { build_zero_cst (t); })))))
7337 /* Similarly, for unsigned operands, (((type) A * B) >> prec) != 0 where type
7338 is at least twice as wide as type of A and B, simplify to
7339 __builtin_mul_overflow (A, B, <unused>). */
7342 (cmp (rshift (mult:s (convert@3 @0) (convert @1)) INTEGER_CST@2)
7344 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
7345 && INTEGRAL_TYPE_P (TREE_TYPE (@3))
7346 && TYPE_UNSIGNED (TREE_TYPE (@0))
7347 && (TYPE_PRECISION (TREE_TYPE (@3))
7348 >= 2 * TYPE_PRECISION (TREE_TYPE (@0)))
7349 && tree_fits_uhwi_p (@2)
7350 && tree_to_uhwi (@2) == TYPE_PRECISION (TREE_TYPE (@0))
7351 && types_match (@0, @1)
7352 && type_has_mode_precision_p (TREE_TYPE (@0))
7353 && (optab_handler (umulv4_optab, TYPE_MODE (TREE_TYPE (@0)))
7354 != CODE_FOR_nothing))
7355 (with { tree t = TREE_TYPE (@0), cpx = build_complex_type (t); }
7356 (cmp (imagpart (IFN_MUL_OVERFLOW:cpx @0 @1)) { build_zero_cst (t); })))))
7358 /* Demote operands of IFN_{ADD,SUB,MUL}_OVERFLOW. */
7359 (for ovf (IFN_ADD_OVERFLOW IFN_SUB_OVERFLOW IFN_MUL_OVERFLOW)
7361 (ovf (convert@2 @0) @1)
7362 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
7363 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
7364 && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
7365 && (!TYPE_UNSIGNED (TREE_TYPE (@2)) || TYPE_UNSIGNED (TREE_TYPE (@0))))
7368 (ovf @1 (convert@2 @0))
7369 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
7370 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
7371 && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
7372 && (!TYPE_UNSIGNED (TREE_TYPE (@2)) || TYPE_UNSIGNED (TREE_TYPE (@0))))
7375 /* Optimize __builtin_mul_overflow_p (x, cst, (utype) 0) if all 3 types
7376 are unsigned to x > (umax / cst). Similarly for signed type, but
7377 in that case it needs to be outside of a range. */
7379 (imagpart (IFN_MUL_OVERFLOW:cs@2 @0 integer_nonzerop@1))
7380 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
7381 && TYPE_MAX_VALUE (TREE_TYPE (@0))
7382 && types_match (TREE_TYPE (@0), TREE_TYPE (TREE_TYPE (@2)))
7383 && int_fits_type_p (@1, TREE_TYPE (@0)))
7384 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
7385 (convert (gt @0 (trunc_div! { TYPE_MAX_VALUE (TREE_TYPE (@0)); } @1)))
7386 (if (TYPE_MIN_VALUE (TREE_TYPE (@0)))
7387 (if (integer_minus_onep (@1))
7388 (convert (eq @0 { TYPE_MIN_VALUE (TREE_TYPE (@0)); }))
7391 tree div = fold_convert (TREE_TYPE (@0), @1);
7392 tree lo = int_const_binop (TRUNC_DIV_EXPR,
7393 TYPE_MIN_VALUE (TREE_TYPE (@0)), div);
7394 tree hi = int_const_binop (TRUNC_DIV_EXPR,
7395 TYPE_MAX_VALUE (TREE_TYPE (@0)), div);
7396 tree etype = range_check_type (TREE_TYPE (@0));
7399 if (wi::neg_p (wi::to_wide (div)))
7401 lo = fold_convert (etype, lo);
7402 hi = fold_convert (etype, hi);
7403 hi = int_const_binop (MINUS_EXPR, hi, lo);
7407 (convert (gt (minus (convert:etype @0) { lo; }) { hi; })))))))))
7409 /* Simplification of math builtins. These rules must all be optimizations
7410 as well as IL simplifications. If there is a possibility that the new
7411 form could be a pessimization, the rule should go in the canonicalization
7412 section that follows this one.
7414 Rules can generally go in this section if they satisfy one of
7417 - the rule describes an identity
7419 - the rule replaces calls with something as simple as addition or
7422 - the rule contains unary calls only and simplifies the surrounding
7423 arithmetic. (The idea here is to exclude non-unary calls in which
7424 one operand is constant and in which the call is known to be cheap
7425 when the operand has that value.) */
7427 (if (flag_unsafe_math_optimizations)
7428 /* Simplify sqrt(x) * sqrt(x) -> x. */
7430 (mult (SQRT_ALL@1 @0) @1)
7431 (if (!tree_expr_maybe_signaling_nan_p (@0))
7434 (for op (plus minus)
7435 /* Simplify (A / C) +- (B / C) -> (A +- B) / C. */
7439 (rdiv (op @0 @2) @1)))
7441 (for cmp (lt le gt ge)
7442 neg_cmp (gt ge lt le)
7443 /* Simplify (x * C1) cmp C2 -> x cmp (C2 / C1), where C1 != 0. */
7445 (cmp (mult @0 REAL_CST@1) REAL_CST@2)
7447 { tree tem = const_binop (RDIV_EXPR, type, @2, @1); }
7449 && !(REAL_VALUE_ISINF (TREE_REAL_CST (tem))
7450 || (real_zerop (tem) && !real_zerop (@1))))
7452 (if (real_less (&dconst0, TREE_REAL_CST_PTR (@1)))
7454 (if (real_less (TREE_REAL_CST_PTR (@1), &dconst0))
7455 (neg_cmp @0 { tem; })))))))
7457 /* Simplify sqrt(x) * sqrt(y) -> sqrt(x*y). */
7458 (for root (SQRT CBRT)
7460 (mult (root:s @0) (root:s @1))
7461 (root (mult @0 @1))))
7463 /* Simplify expN(x) * expN(y) -> expN(x+y). */
7464 (for exps (EXP EXP2 EXP10 POW10)
7466 (mult (exps:s @0) (exps:s @1))
7467 (exps (plus @0 @1))))
7469 /* Simplify a/root(b/c) into a*root(c/b). */
7470 (for root (SQRT CBRT)
7472 (rdiv @0 (root:s (rdiv:s @1 @2)))
7473 (mult @0 (root (rdiv @2 @1)))))
7475 /* Simplify x/expN(y) into x*expN(-y). */
7476 (for exps (EXP EXP2 EXP10 POW10)
7478 (rdiv @0 (exps:s @1))
7479 (mult @0 (exps (negate @1)))))
7481 (for logs (LOG LOG2 LOG10 LOG10)
7482 exps (EXP EXP2 EXP10 POW10)
7483 /* logN(expN(x)) -> x. */
7487 /* expN(logN(x)) -> x. */
7492 /* Optimize logN(func()) for various exponential functions. We
7493 want to determine the value "x" and the power "exponent" in
7494 order to transform logN(x**exponent) into exponent*logN(x). */
7495 (for logs (LOG LOG LOG LOG2 LOG2 LOG2 LOG10 LOG10)
7496 exps (EXP2 EXP10 POW10 EXP EXP10 POW10 EXP EXP2)
7499 (if (SCALAR_FLOAT_TYPE_P (type))
7505 /* Prepare to do logN(exp(exponent)) -> exponent*logN(e). */
7506 x = build_real_truncate (type, dconst_e ());
7509 /* Prepare to do logN(exp2(exponent)) -> exponent*logN(2). */
7510 x = build_real (type, dconst2);
7514 /* Prepare to do logN(exp10(exponent)) -> exponent*logN(10). */
7516 REAL_VALUE_TYPE dconst10;
7517 real_from_integer (&dconst10, VOIDmode, 10, SIGNED);
7518 x = build_real (type, dconst10);
7525 (mult (logs { x; }) @0)))))
7533 (if (SCALAR_FLOAT_TYPE_P (type))
7539 /* Prepare to do logN(sqrt(x)) -> 0.5*logN(x). */
7540 x = build_real (type, dconsthalf);
7543 /* Prepare to do logN(cbrt(x)) -> (1/3)*logN(x). */
7544 x = build_real_truncate (type, dconst_third ());
7550 (mult { x; } (logs @0))))))
7552 /* logN(pow(x,exponent)) -> exponent*logN(x). */
7553 (for logs (LOG LOG2 LOG10)
7557 (mult @1 (logs @0))))
7559 /* pow(C,x) -> exp(log(C)*x) if C > 0,
7560 or if C is a positive power of 2,
7561 pow(C,x) -> exp2(log2(C)*x). */
7569 (pows REAL_CST@0 @1)
7570 (if (real_compare (GT_EXPR, TREE_REAL_CST_PTR (@0), &dconst0)
7571 && real_isfinite (TREE_REAL_CST_PTR (@0))
7572 /* As libmvec doesn't have a vectorized exp2, defer optimizing
7573 the use_exp2 case until after vectorization. It seems actually
7574 beneficial for all constants to postpone this until later,
7575 because exp(log(C)*x), while faster, will have worse precision
7576 and if x folds into a constant too, that is unnecessary
7578 && canonicalize_math_after_vectorization_p ())
7580 const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (@0);
7581 bool use_exp2 = false;
7582 if (targetm.libc_has_function (function_c99_misc, TREE_TYPE (@0))
7583 && value->cl == rvc_normal)
7585 REAL_VALUE_TYPE frac_rvt = *value;
7586 SET_REAL_EXP (&frac_rvt, 1);
7587 if (real_equal (&frac_rvt, &dconst1))
7592 (if (optimize_pow_to_exp (@0, @1))
7593 (exps (mult (logs @0) @1)))
7594 (exp2s (mult (log2s @0) @1)))))))
7597 /* pow(C,x)*expN(y) -> expN(logN(C)*x+y) if C > 0. */
7599 exps (EXP EXP2 EXP10 POW10)
7600 logs (LOG LOG2 LOG10 LOG10)
7602 (mult:c (pows:s REAL_CST@0 @1) (exps:s @2))
7603 (if (real_compare (GT_EXPR, TREE_REAL_CST_PTR (@0), &dconst0)
7604 && real_isfinite (TREE_REAL_CST_PTR (@0)))
7605 (exps (plus (mult (logs @0) @1) @2)))))
7610 exps (EXP EXP2 EXP10 POW10)
7611 /* sqrt(expN(x)) -> expN(x*0.5). */
7614 (exps (mult @0 { build_real (type, dconsthalf); })))
7615 /* cbrt(expN(x)) -> expN(x/3). */
7618 (exps (mult @0 { build_real_truncate (type, dconst_third ()); })))
7619 /* pow(expN(x), y) -> expN(x*y). */
7622 (exps (mult @0 @1))))
7624 /* tan(atan(x)) -> x. */
7631 /* Simplify sin(atan(x)) -> x / sqrt(x*x + 1). */
7635 copysigns (COPYSIGN)
7640 REAL_VALUE_TYPE r_cst;
7641 build_sinatan_real (&r_cst, type);
7642 tree t_cst = build_real (type, r_cst);
7643 tree t_one = build_one_cst (type);
7645 (if (SCALAR_FLOAT_TYPE_P (type))
7646 (cond (lt (abs @0) { t_cst; })
7647 (rdiv @0 (sqrts (plus (mult @0 @0) { t_one; })))
7648 (copysigns { t_one; } @0))))))
7650 /* Simplify cos(atan(x)) -> 1 / sqrt(x*x + 1). */
7654 copysigns (COPYSIGN)
7659 REAL_VALUE_TYPE r_cst;
7660 build_sinatan_real (&r_cst, type);
7661 tree t_cst = build_real (type, r_cst);
7662 tree t_one = build_one_cst (type);
7663 tree t_zero = build_zero_cst (type);
7665 (if (SCALAR_FLOAT_TYPE_P (type))
7666 (cond (lt (abs @0) { t_cst; })
7667 (rdiv { t_one; } (sqrts (plus (mult @0 @0) { t_one; })))
7668 (copysigns { t_zero; } @0))))))
7670 (if (!flag_errno_math)
7671 /* Simplify sinh(atanh(x)) -> x / sqrt((1 - x)*(1 + x)). */
7676 (sinhs (atanhs:s @0))
7677 (with { tree t_one = build_one_cst (type); }
7678 (rdiv @0 (sqrts (mult (minus { t_one; } @0) (plus { t_one; } @0)))))))
7680 /* Simplify cosh(atanh(x)) -> 1 / sqrt((1 - x)*(1 + x)) */
7685 (coshs (atanhs:s @0))
7686 (with { tree t_one = build_one_cst (type); }
7687 (rdiv { t_one; } (sqrts (mult (minus { t_one; } @0) (plus { t_one; } @0))))))))
7689 /* cabs(x+0i) or cabs(0+xi) -> abs(x). */
7691 (CABS (complex:C @0 real_zerop@1))
7694 /* trunc(trunc(x)) -> trunc(x), etc. */
7695 (for fns (TRUNC_ALL FLOOR_ALL CEIL_ALL ROUND_ALL NEARBYINT_ALL RINT_ALL)
7699 /* f(x) -> x if x is integer valued and f does nothing for such values. */
7700 (for fns (TRUNC_ALL FLOOR_ALL CEIL_ALL ROUND_ALL NEARBYINT_ALL RINT_ALL)
7702 (fns integer_valued_real_p@0)
7705 /* hypot(x,0) and hypot(0,x) -> abs(x). */
7707 (HYPOT:c @0 real_zerop@1)
7710 /* pow(1,x) -> 1. */
7712 (POW real_onep@0 @1)
7716 /* copysign(x,x) -> x. */
7717 (COPYSIGN_ALL @0 @0)
7721 /* copysign(x,-x) -> -x. */
7722 (COPYSIGN_ALL @0 (negate@1 @0))
7726 /* copysign(x,y) -> fabs(x) if y is nonnegative. */
7727 (COPYSIGN_ALL @0 tree_expr_nonnegative_p@1)
7731 /* fabs (copysign(x, y)) -> fabs (x). */
7732 (abs (COPYSIGN_ALL @0 @1))
7735 (for scale (LDEXP SCALBN SCALBLN)
7736 /* ldexp(0, x) -> 0. */
7738 (scale real_zerop@0 @1)
7740 /* ldexp(x, 0) -> x. */
7742 (scale @0 integer_zerop@1)
7744 /* ldexp(x, y) -> x if x is +-Inf or NaN. */
7746 (scale REAL_CST@0 @1)
7747 (if (!real_isfinite (TREE_REAL_CST_PTR (@0)))
7750 /* Canonicalization of sequences of math builtins. These rules represent
7751 IL simplifications but are not necessarily optimizations.
7753 The sincos pass is responsible for picking "optimal" implementations
7754 of math builtins, which may be more complicated and can sometimes go
7755 the other way, e.g. converting pow into a sequence of sqrts.
7756 We only want to do these canonicalizations before the pass has run. */
7758 (if (flag_unsafe_math_optimizations && canonicalize_math_p ())
7759 /* Simplify tan(x) * cos(x) -> sin(x). */
7761 (mult:c (TAN:s @0) (COS:s @0))
7764 /* Simplify x * pow(x,c) -> pow(x,c+1). */
7766 (mult:c @0 (POW:s @0 REAL_CST@1))
7767 (if (!TREE_OVERFLOW (@1))
7768 (POW @0 (plus @1 { build_one_cst (type); }))))
7770 /* Simplify sin(x) / cos(x) -> tan(x). */
7772 (rdiv (SIN:s @0) (COS:s @0))
7775 /* Simplify sinh(x) / cosh(x) -> tanh(x). */
7777 (rdiv (SINH:s @0) (COSH:s @0))
7780 /* Simplify tanh (x) / sinh (x) -> 1.0 / cosh (x). */
7782 (rdiv (TANH:s @0) (SINH:s @0))
7783 (rdiv {build_one_cst (type);} (COSH @0)))
7785 /* Simplify cos(x) / sin(x) -> 1 / tan(x). */
7787 (rdiv (COS:s @0) (SIN:s @0))
7788 (rdiv { build_one_cst (type); } (TAN @0)))
7790 /* Simplify sin(x) / tan(x) -> cos(x). */
7792 (rdiv (SIN:s @0) (TAN:s @0))
7793 (if (! HONOR_NANS (@0)
7794 && ! HONOR_INFINITIES (@0))
7797 /* Simplify tan(x) / sin(x) -> 1.0 / cos(x). */
7799 (rdiv (TAN:s @0) (SIN:s @0))
7800 (if (! HONOR_NANS (@0)
7801 && ! HONOR_INFINITIES (@0))
7802 (rdiv { build_one_cst (type); } (COS @0))))
7804 /* Simplify pow(x,y) * pow(x,z) -> pow(x,y+z). */
7806 (mult (POW:s @0 @1) (POW:s @0 @2))
7807 (POW @0 (plus @1 @2)))
7809 /* Simplify pow(x,y) * pow(z,y) -> pow(x*z,y). */
7811 (mult (POW:s @0 @1) (POW:s @2 @1))
7812 (POW (mult @0 @2) @1))
7814 /* Simplify powi(x,y) * powi(z,y) -> powi(x*z,y). */
7816 (mult (POWI:s @0 @1) (POWI:s @2 @1))
7817 (POWI (mult @0 @2) @1))
7819 /* Simplify pow(x,c) / x -> pow(x,c-1). */
7821 (rdiv (POW:s @0 REAL_CST@1) @0)
7822 (if (!TREE_OVERFLOW (@1))
7823 (POW @0 (minus @1 { build_one_cst (type); }))))
7825 /* Simplify x / pow (y,z) -> x * pow(y,-z). */
7827 (rdiv @0 (POW:s @1 @2))
7828 (mult @0 (POW @1 (negate @2))))
7833 /* sqrt(sqrt(x)) -> pow(x,1/4). */
7836 (pows @0 { build_real (type, dconst_quarter ()); }))
7837 /* sqrt(cbrt(x)) -> pow(x,1/6). */
7840 (pows @0 { build_real_truncate (type, dconst_sixth ()); }))
7841 /* cbrt(sqrt(x)) -> pow(x,1/6). */
7844 (pows @0 { build_real_truncate (type, dconst_sixth ()); }))
7845 /* cbrt(cbrt(x)) -> pow(x,1/9), iff x is nonnegative. */
7847 (cbrts (cbrts tree_expr_nonnegative_p@0))
7848 (pows @0 { build_real_truncate (type, dconst_ninth ()); }))
7849 /* sqrt(pow(x,y)) -> pow(|x|,y*0.5). */
7851 (sqrts (pows @0 @1))
7852 (pows (abs @0) (mult @1 { build_real (type, dconsthalf); })))
7853 /* cbrt(pow(x,y)) -> pow(x,y/3), iff x is nonnegative. */
7855 (cbrts (pows tree_expr_nonnegative_p@0 @1))
7856 (pows @0 (mult @1 { build_real_truncate (type, dconst_third ()); })))
7857 /* pow(sqrt(x),y) -> pow(x,y*0.5). */
7859 (pows (sqrts @0) @1)
7860 (pows @0 (mult @1 { build_real (type, dconsthalf); })))
7861 /* pow(cbrt(x),y) -> pow(x,y/3) iff x is nonnegative. */
7863 (pows (cbrts tree_expr_nonnegative_p@0) @1)
7864 (pows @0 (mult @1 { build_real_truncate (type, dconst_third ()); })))
7865 /* pow(pow(x,y),z) -> pow(x,y*z) iff x is nonnegative. */
7867 (pows (pows tree_expr_nonnegative_p@0 @1) @2)
7868 (pows @0 (mult @1 @2))))
7870 /* cabs(x+xi) -> fabs(x)*sqrt(2). */
7872 (CABS (complex @0 @0))
7873 (mult (abs @0) { build_real_truncate (type, dconst_sqrt2 ()); }))
7875 /* hypot(x,x) -> fabs(x)*sqrt(2). */
7878 (mult (abs @0) { build_real_truncate (type, dconst_sqrt2 ()); }))
7880 /* cexp(x+yi) -> exp(x)*cexpi(y). */
7885 (cexps compositional_complex@0)
7886 (if (targetm.libc_has_function (function_c99_math_complex, TREE_TYPE (@0)))
7888 (mult (exps@1 (realpart @0)) (realpart (cexpis:type@2 (imagpart @0))))
7889 (mult @1 (imagpart @2)))))))
7891 (if (canonicalize_math_p ())
7892 /* floor(x) -> trunc(x) if x is nonnegative. */
7893 (for floors (FLOOR_ALL)
7896 (floors tree_expr_nonnegative_p@0)
7899 (match double_value_p
7901 (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == double_type_node)))
7902 (for froms (BUILT_IN_TRUNCL
7914 /* truncl(extend(x)) -> extend(trunc(x)), etc., if x is a double. */
7915 (if (optimize && canonicalize_math_p ())
7917 (froms (convert double_value_p@0))
7918 (convert (tos @0)))))
7920 (match float_value_p
7922 (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == float_type_node)))
7923 (for froms (BUILT_IN_TRUNCL BUILT_IN_TRUNC
7924 BUILT_IN_FLOORL BUILT_IN_FLOOR
7925 BUILT_IN_CEILL BUILT_IN_CEIL
7926 BUILT_IN_ROUNDL BUILT_IN_ROUND
7927 BUILT_IN_NEARBYINTL BUILT_IN_NEARBYINT
7928 BUILT_IN_RINTL BUILT_IN_RINT)
7929 tos (BUILT_IN_TRUNCF BUILT_IN_TRUNCF
7930 BUILT_IN_FLOORF BUILT_IN_FLOORF
7931 BUILT_IN_CEILF BUILT_IN_CEILF
7932 BUILT_IN_ROUNDF BUILT_IN_ROUNDF
7933 BUILT_IN_NEARBYINTF BUILT_IN_NEARBYINTF
7934 BUILT_IN_RINTF BUILT_IN_RINTF)
7935 /* truncl(extend(x)) and trunc(extend(x)) -> extend(truncf(x)), etc.,
7937 (if (optimize && canonicalize_math_p ()
7938 && targetm.libc_has_function (function_c99_misc, NULL_TREE))
7940 (froms (convert float_value_p@0))
7941 (convert (tos @0)))))
7944 (match float16_value_p
7946 (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == float16_type_node)))
7947 (for froms (BUILT_IN_TRUNCL BUILT_IN_TRUNC BUILT_IN_TRUNCF
7948 BUILT_IN_FLOORL BUILT_IN_FLOOR BUILT_IN_FLOORF
7949 BUILT_IN_CEILL BUILT_IN_CEIL BUILT_IN_CEILF
7950 BUILT_IN_ROUNDEVENL BUILT_IN_ROUNDEVEN BUILT_IN_ROUNDEVENF
7951 BUILT_IN_ROUNDL BUILT_IN_ROUND BUILT_IN_ROUNDF
7952 BUILT_IN_NEARBYINTL BUILT_IN_NEARBYINT BUILT_IN_NEARBYINTF
7953 BUILT_IN_RINTL BUILT_IN_RINT BUILT_IN_RINTF
7954 BUILT_IN_SQRTL BUILT_IN_SQRT BUILT_IN_SQRTF)
7955 tos (IFN_TRUNC IFN_TRUNC IFN_TRUNC
7956 IFN_FLOOR IFN_FLOOR IFN_FLOOR
7957 IFN_CEIL IFN_CEIL IFN_CEIL
7958 IFN_ROUNDEVEN IFN_ROUNDEVEN IFN_ROUNDEVEN
7959 IFN_ROUND IFN_ROUND IFN_ROUND
7960 IFN_NEARBYINT IFN_NEARBYINT IFN_NEARBYINT
7961 IFN_RINT IFN_RINT IFN_RINT
7962 IFN_SQRT IFN_SQRT IFN_SQRT)
7963 /* (_Float16) round ((doube) x) -> __built_in_roundf16 (x), etc.,
7964 if x is a _Float16. */
7966 (convert (froms (convert float16_value_p@0)))
7968 && types_match (type, TREE_TYPE (@0))
7969 && direct_internal_fn_supported_p (as_internal_fn (tos),
7970 type, OPTIMIZE_FOR_BOTH))
7973 /* Simplify (trunc)copysign ((extend)x, (extend)y) to copysignf (x, y),
7974 x,y is float value, similar for _Float16/double. */
7975 (for copysigns (COPYSIGN_ALL)
7977 (convert (copysigns (convert@2 @0) (convert @1)))
7979 && !HONOR_SNANS (@2)
7980 && types_match (type, TREE_TYPE (@0))
7981 && types_match (type, TREE_TYPE (@1))
7982 && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (@2))
7983 && direct_internal_fn_supported_p (IFN_COPYSIGN,
7984 type, OPTIMIZE_FOR_BOTH))
7985 (IFN_COPYSIGN @0 @1))))
7987 (for froms (BUILT_IN_FMAF BUILT_IN_FMA BUILT_IN_FMAL)
7988 tos (IFN_FMA IFN_FMA IFN_FMA)
7990 (convert (froms (convert@3 @0) (convert @1) (convert @2)))
7991 (if (flag_unsafe_math_optimizations
7993 && FLOAT_TYPE_P (type)
7994 && FLOAT_TYPE_P (TREE_TYPE (@3))
7995 && types_match (type, TREE_TYPE (@0))
7996 && types_match (type, TREE_TYPE (@1))
7997 && types_match (type, TREE_TYPE (@2))
7998 && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (@3))
7999 && direct_internal_fn_supported_p (as_internal_fn (tos),
8000 type, OPTIMIZE_FOR_BOTH))
8003 (for maxmin (max min)
8005 (convert (maxmin (convert@2 @0) (convert @1)))
8007 && FLOAT_TYPE_P (type)
8008 && FLOAT_TYPE_P (TREE_TYPE (@2))
8009 && types_match (type, TREE_TYPE (@0))
8010 && types_match (type, TREE_TYPE (@1))
8011 && element_precision (type) < element_precision (TREE_TYPE (@2)))
8015 (for froms (XFLOORL XCEILL XROUNDL XRINTL)
8016 tos (XFLOOR XCEIL XROUND XRINT)
8017 /* llfloorl(extend(x)) -> llfloor(x), etc., if x is a double. */
8018 (if (optimize && canonicalize_math_p ())
8020 (froms (convert double_value_p@0))
8023 (for froms (XFLOORL XCEILL XROUNDL XRINTL
8024 XFLOOR XCEIL XROUND XRINT)
8025 tos (XFLOORF XCEILF XROUNDF XRINTF)
8026 /* llfloorl(extend(x)) and llfloor(extend(x)) -> llfloorf(x), etc.,
8028 (if (optimize && canonicalize_math_p ())
8030 (froms (convert float_value_p@0))
8033 (if (canonicalize_math_p ())
8034 /* xfloor(x) -> fix_trunc(x) if x is nonnegative. */
8035 (for floors (IFLOOR LFLOOR LLFLOOR)
8037 (floors tree_expr_nonnegative_p@0)
8040 (if (canonicalize_math_p ())
8041 /* xfloor(x) -> fix_trunc(x), etc., if x is integer valued. */
8042 (for fns (IFLOOR LFLOOR LLFLOOR
8044 IROUND LROUND LLROUND)
8046 (fns integer_valued_real_p@0)
8048 (if (!flag_errno_math)
8049 /* xrint(x) -> fix_trunc(x), etc., if x is integer valued. */
8050 (for rints (IRINT LRINT LLRINT)
8052 (rints integer_valued_real_p@0)
8055 (if (canonicalize_math_p ())
8056 (for ifn (IFLOOR ICEIL IROUND IRINT)
8057 lfn (LFLOOR LCEIL LROUND LRINT)
8058 llfn (LLFLOOR LLCEIL LLROUND LLRINT)
8059 /* Canonicalize iround (x) to lround (x) on ILP32 targets where
8060 sizeof (int) == sizeof (long). */
8061 (if (TYPE_PRECISION (integer_type_node)
8062 == TYPE_PRECISION (long_integer_type_node))
8065 (lfn:long_integer_type_node @0)))
8066 /* Canonicalize llround (x) to lround (x) on LP64 targets where
8067 sizeof (long long) == sizeof (long). */
8068 (if (TYPE_PRECISION (long_long_integer_type_node)
8069 == TYPE_PRECISION (long_integer_type_node))
8072 (lfn:long_integer_type_node @0)))))
8074 /* cproj(x) -> x if we're ignoring infinities. */
8077 (if (!HONOR_INFINITIES (type))
8080 /* If the real part is inf and the imag part is known to be
8081 nonnegative, return (inf + 0i). */
8083 (CPROJ (complex REAL_CST@0 tree_expr_nonnegative_p@1))
8084 (if (real_isinf (TREE_REAL_CST_PTR (@0)))
8085 { build_complex_inf (type, false); }))
8087 /* If the imag part is inf, return (inf+I*copysign(0,imag)). */
8089 (CPROJ (complex @0 REAL_CST@1))
8090 (if (real_isinf (TREE_REAL_CST_PTR (@1)))
8091 { build_complex_inf (type, TREE_REAL_CST_PTR (@1)->sign); }))
8097 (pows @0 REAL_CST@1)
8099 const REAL_VALUE_TYPE *value = TREE_REAL_CST_PTR (@1);
8100 REAL_VALUE_TYPE tmp;
8103 /* pow(x,0) -> 1. */
8104 (if (real_equal (value, &dconst0))
8105 { build_real (type, dconst1); })
8106 /* pow(x,1) -> x. */
8107 (if (real_equal (value, &dconst1))
8109 /* pow(x,-1) -> 1/x. */
8110 (if (real_equal (value, &dconstm1))
8111 (rdiv { build_real (type, dconst1); } @0))
8112 /* pow(x,0.5) -> sqrt(x). */
8113 (if (flag_unsafe_math_optimizations
8114 && canonicalize_math_p ()
8115 && real_equal (value, &dconsthalf))
8117 /* pow(x,1/3) -> cbrt(x). */
8118 (if (flag_unsafe_math_optimizations
8119 && canonicalize_math_p ()
8120 && (tmp = real_value_truncate (TYPE_MODE (type), dconst_third ()),
8121 real_equal (value, &tmp)))
8124 /* powi(1,x) -> 1. */
8126 (POWI real_onep@0 @1)
8130 (POWI @0 INTEGER_CST@1)
8132 /* powi(x,0) -> 1. */
8133 (if (wi::to_wide (@1) == 0)
8134 { build_real (type, dconst1); })
8135 /* powi(x,1) -> x. */
8136 (if (wi::to_wide (@1) == 1)
8138 /* powi(x,-1) -> 1/x. */
8139 (if (wi::to_wide (@1) == -1)
8140 (rdiv { build_real (type, dconst1); } @0))))
8142 /* Narrowing of arithmetic and logical operations.
8144 These are conceptually similar to the transformations performed for
8145 the C/C++ front-ends by shorten_binary_op and shorten_compare. Long
8146 term we want to move all that code out of the front-ends into here. */
8148 /* Convert (outertype)((innertype0)a+(innertype1)b)
8149 into ((newtype)a+(newtype)b) where newtype
8150 is the widest mode from all of these. */
8151 (for op (plus minus mult rdiv)
8153 (convert (op:s@0 (convert1?@3 @1) (convert2?@4 @2)))
8154 /* If we have a narrowing conversion of an arithmetic operation where
8155 both operands are widening conversions from the same type as the outer
8156 narrowing conversion. Then convert the innermost operands to a
8157 suitable unsigned type (to avoid introducing undefined behavior),
8158 perform the operation and convert the result to the desired type. */
8159 (if (INTEGRAL_TYPE_P (type)
8162 /* We check for type compatibility between @0 and @1 below,
8163 so there's no need to check that @2/@4 are integral types. */
8164 && INTEGRAL_TYPE_P (TREE_TYPE (@1))
8165 && INTEGRAL_TYPE_P (TREE_TYPE (@3))
8166 /* The precision of the type of each operand must match the
8167 precision of the mode of each operand, similarly for the
8169 && type_has_mode_precision_p (TREE_TYPE (@1))
8170 && type_has_mode_precision_p (TREE_TYPE (@2))
8171 && type_has_mode_precision_p (type)
8172 /* The inner conversion must be a widening conversion. */
8173 && TYPE_PRECISION (TREE_TYPE (@3)) > TYPE_PRECISION (TREE_TYPE (@1))
8174 && types_match (@1, type)
8175 && (types_match (@1, @2)
8176 /* Or the second operand is const integer or converted const
8177 integer from valueize. */
8178 || poly_int_tree_p (@4)))
8179 (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
8180 (op @1 (convert @2))
8181 (with { tree utype = unsigned_type_for (TREE_TYPE (@1)); }
8182 (convert (op (convert:utype @1)
8183 (convert:utype @2)))))
8184 (if (FLOAT_TYPE_P (type)
8185 && DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0))
8186 == DECIMAL_FLOAT_TYPE_P (type))
8187 (with { tree arg0 = strip_float_extensions (@1);
8188 tree arg1 = strip_float_extensions (@2);
8189 tree itype = TREE_TYPE (@0);
8190 tree ty1 = TREE_TYPE (arg0);
8191 tree ty2 = TREE_TYPE (arg1);
8192 enum tree_code code = TREE_CODE (itype); }
8193 (if (FLOAT_TYPE_P (ty1)
8194 && FLOAT_TYPE_P (ty2))
8195 (with { tree newtype = type;
8196 if (TYPE_MODE (ty1) == SDmode
8197 || TYPE_MODE (ty2) == SDmode
8198 || TYPE_MODE (type) == SDmode)
8199 newtype = dfloat32_type_node;
8200 if (TYPE_MODE (ty1) == DDmode
8201 || TYPE_MODE (ty2) == DDmode
8202 || TYPE_MODE (type) == DDmode)
8203 newtype = dfloat64_type_node;
8204 if (TYPE_MODE (ty1) == TDmode
8205 || TYPE_MODE (ty2) == TDmode
8206 || TYPE_MODE (type) == TDmode)
8207 newtype = dfloat128_type_node; }
8208 (if ((newtype == dfloat32_type_node
8209 || newtype == dfloat64_type_node
8210 || newtype == dfloat128_type_node)
8212 && types_match (newtype, type))
8213 (op (convert:newtype @1) (convert:newtype @2))
8214 (with { if (element_precision (ty1) > element_precision (newtype))
8216 if (element_precision (ty2) > element_precision (newtype))
8218 /* Sometimes this transformation is safe (cannot
8219 change results through affecting double rounding
8220 cases) and sometimes it is not. If NEWTYPE is
8221 wider than TYPE, e.g. (float)((long double)double
8222 + (long double)double) converted to
8223 (float)(double + double), the transformation is
8224 unsafe regardless of the details of the types
8225 involved; double rounding can arise if the result
8226 of NEWTYPE arithmetic is a NEWTYPE value half way
8227 between two representable TYPE values but the
8228 exact value is sufficiently different (in the
8229 right direction) for this difference to be
8230 visible in ITYPE arithmetic. If NEWTYPE is the
8231 same as TYPE, however, the transformation may be
8232 safe depending on the types involved: it is safe
8233 if the ITYPE has strictly more than twice as many
8234 mantissa bits as TYPE, can represent infinities
8235 and NaNs if the TYPE can, and has sufficient
8236 exponent range for the product or ratio of two
8237 values representable in the TYPE to be within the
8238 range of normal values of ITYPE. */
8239 (if (element_precision (newtype) < element_precision (itype)
8240 && (!VECTOR_MODE_P (TYPE_MODE (newtype))
8241 || target_supports_op_p (newtype, op, optab_default))
8242 && (flag_unsafe_math_optimizations
8243 || (element_precision (newtype) == element_precision (type)
8244 && real_can_shorten_arithmetic (element_mode (itype),
8245 element_mode (type))
8246 && !excess_precision_type (newtype)))
8247 && !types_match (itype, newtype))
8248 (convert:type (op (convert:newtype @1)
8249 (convert:newtype @2)))
8254 /* This is another case of narrowing, specifically when there's an outer
8255 BIT_AND_EXPR which masks off bits outside the type of the innermost
8256 operands. Like the previous case we have to convert the operands
8257 to unsigned types to avoid introducing undefined behavior for the
8258 arithmetic operation. */
8259 (for op (minus plus)
8261 (bit_and (op:s (convert@2 @0) (convert@3 @1)) INTEGER_CST@4)
8262 (if (INTEGRAL_TYPE_P (type)
8263 /* We check for type compatibility between @0 and @1 below,
8264 so there's no need to check that @1/@3 are integral types. */
8265 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
8266 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
8267 /* The precision of the type of each operand must match the
8268 precision of the mode of each operand, similarly for the
8270 && type_has_mode_precision_p (TREE_TYPE (@0))
8271 && type_has_mode_precision_p (TREE_TYPE (@1))
8272 && type_has_mode_precision_p (type)
8273 /* The inner conversion must be a widening conversion. */
8274 && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
8275 && types_match (@0, @1)
8276 && (tree_int_cst_min_precision (@4, TYPE_SIGN (TREE_TYPE (@0)))
8277 <= TYPE_PRECISION (TREE_TYPE (@0)))
8278 && (wi::to_wide (@4)
8279 & wi::mask (TYPE_PRECISION (TREE_TYPE (@0)),
8280 true, TYPE_PRECISION (type))) == 0)
8281 (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
8282 (with { tree ntype = TREE_TYPE (@0); }
8283 (convert (bit_and (op @0 @1) (convert:ntype @4))))
8284 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
8285 (convert (bit_and (op (convert:utype @0) (convert:utype @1))
8286 (convert:utype @4))))))))
8288 /* Transform (@0 < @1 and @0 < @2) to use min,
8289 (@0 > @1 and @0 > @2) to use max */
8290 (for logic (bit_and bit_and bit_and bit_and bit_ior bit_ior bit_ior bit_ior)
8291 op (lt le gt ge lt le gt ge )
8292 ext (min min max max max max min min )
8294 (logic (op:cs @0 @1) (op:cs @0 @2))
8295 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
8296 && TREE_CODE (@0) != INTEGER_CST)
8297 (op @0 (ext @1 @2)))))
8299 /* Max<bool0, bool1> -> bool0 | bool1
8300 Min<bool0, bool1> -> bool0 & bool1 */
8302 logic (bit_ior bit_and)
8304 (op zero_one_valued_p@0 zero_one_valued_p@1)
8307 /* signbit(x) != 0 ? -x : x -> abs(x)
8308 signbit(x) == 0 ? -x : x -> -abs(x) */
8312 (cond (neeq (sign @0) integer_zerop) (negate @0) @0)
8313 (if (neeq == NE_EXPR)
8315 (negate (abs @0))))))
8318 /* signbit(x) -> 0 if x is nonnegative. */
8319 (SIGNBIT tree_expr_nonnegative_p@0)
8320 { integer_zero_node; })
8323 /* signbit(x) -> x<0 if x doesn't have signed zeros. */
8325 (if (!HONOR_SIGNED_ZEROS (@0))
8326 (convert (lt @0 { build_real (TREE_TYPE (@0), dconst0); }))))
8328 /* Transform comparisons of the form X +- C1 CMP C2 to X CMP C2 -+ C1. */
8330 (for op (plus minus)
8333 (cmp (op@3 @0 INTEGER_CST@1) INTEGER_CST@2)
8334 (if (!TREE_OVERFLOW (@1) && !TREE_OVERFLOW (@2)
8335 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0))
8336 && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@0))
8337 && !TYPE_SATURATING (TREE_TYPE (@0)))
8338 (with { tree res = int_const_binop (rop, @2, @1); }
8339 (if (TREE_OVERFLOW (res)
8340 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
8341 { constant_boolean_node (cmp == NE_EXPR, type); }
8342 (if (single_use (@3))
8343 (cmp @0 { TREE_OVERFLOW (res)
8344 ? drop_tree_overflow (res) : res; }))))))))
8345 (for cmp (lt le gt ge)
8346 (for op (plus minus)
8349 (cmp (op@3 @0 INTEGER_CST@1) INTEGER_CST@2)
8350 (if (!TREE_OVERFLOW (@1) && !TREE_OVERFLOW (@2)
8351 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
8352 (with { tree res = int_const_binop (rop, @2, @1); }
8353 (if (TREE_OVERFLOW (res))
8355 fold_overflow_warning (("assuming signed overflow does not occur "
8356 "when simplifying conditional to constant"),
8357 WARN_STRICT_OVERFLOW_CONDITIONAL);
8358 bool less = cmp == LE_EXPR || cmp == LT_EXPR;
8359 /* wi::ges_p (@2, 0) should be sufficient for a signed type. */
8360 bool ovf_high = wi::lt_p (wi::to_wide (@1), 0,
8361 TYPE_SIGN (TREE_TYPE (@1)))
8362 != (op == MINUS_EXPR);
8363 constant_boolean_node (less == ovf_high, type);
8365 (if (single_use (@3))
8368 fold_overflow_warning (("assuming signed overflow does not occur "
8369 "when changing X +- C1 cmp C2 to "
8371 WARN_STRICT_OVERFLOW_COMPARISON);
8373 (cmp @0 { res; })))))))))
8375 /* Canonicalizations of BIT_FIELD_REFs. */
8378 (BIT_FIELD_REF (BIT_FIELD_REF @0 @1 @2) @3 @4)
8379 (BIT_FIELD_REF @0 @3 { const_binop (PLUS_EXPR, bitsizetype, @2, @4); }))
8382 (BIT_FIELD_REF (view_convert @0) @1 @2)
8383 (if (! INTEGRAL_TYPE_P (TREE_TYPE (@0))
8384 || type_has_mode_precision_p (TREE_TYPE (@0)))
8385 (BIT_FIELD_REF @0 @1 @2)))
8388 (BIT_FIELD_REF @0 @1 integer_zerop)
8389 (if (tree_int_cst_equal (@1, TYPE_SIZE (TREE_TYPE (@0))))
8393 (BIT_FIELD_REF @0 @1 @2)
8395 (if (TREE_CODE (TREE_TYPE (@0)) == COMPLEX_TYPE
8396 && tree_int_cst_equal (@1, TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))))
8398 (if (integer_zerop (@2))
8399 (view_convert (realpart @0)))
8400 (if (tree_int_cst_equal (@2, TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))))
8401 (view_convert (imagpart @0)))))
8402 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
8403 && INTEGRAL_TYPE_P (type)
8404 /* On GIMPLE this should only apply to register arguments. */
8405 && (! GIMPLE || is_gimple_reg (@0))
8406 /* A bit-field-ref that referenced the full argument can be stripped. */
8407 && ((compare_tree_int (@1, TYPE_PRECISION (TREE_TYPE (@0))) == 0
8408 && integer_zerop (@2))
8409 /* Low-parts can be reduced to integral conversions.
8410 ??? The following doesn't work for PDP endian. */
8411 || (BYTES_BIG_ENDIAN == WORDS_BIG_ENDIAN
8412 /* But only do this after vectorization. */
8413 && canonicalize_math_after_vectorization_p ()
8414 /* Don't even think about BITS_BIG_ENDIAN. */
8415 && TYPE_PRECISION (TREE_TYPE (@0)) % BITS_PER_UNIT == 0
8416 && TYPE_PRECISION (type) % BITS_PER_UNIT == 0
8417 && compare_tree_int (@2, (BYTES_BIG_ENDIAN
8418 ? (TYPE_PRECISION (TREE_TYPE (@0))
8419 - TYPE_PRECISION (type))
8423 /* Simplify vector extracts. */
8426 (BIT_FIELD_REF CONSTRUCTOR@0 @1 @2)
8427 (if (VECTOR_TYPE_P (TREE_TYPE (@0))
8428 && tree_fits_uhwi_p (TYPE_SIZE (type))
8429 && ((tree_to_uhwi (TYPE_SIZE (type))
8430 == tree_to_uhwi (TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))))
8431 || (VECTOR_TYPE_P (type)
8432 && (tree_to_uhwi (TYPE_SIZE (TREE_TYPE (type)))
8433 == tree_to_uhwi (TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0))))))))
8436 tree ctor = (TREE_CODE (@0) == SSA_NAME
8437 ? gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0)) : @0);
8438 tree eltype = TREE_TYPE (TREE_TYPE (ctor));
8439 unsigned HOST_WIDE_INT width = tree_to_uhwi (TYPE_SIZE (eltype));
8440 unsigned HOST_WIDE_INT n = tree_to_uhwi (@1);
8441 unsigned HOST_WIDE_INT idx = tree_to_uhwi (@2);
8444 && (idx % width) == 0
8446 && known_le ((idx + n) / width,
8447 TYPE_VECTOR_SUBPARTS (TREE_TYPE (ctor))))
8452 /* Constructor elements can be subvectors. */
8454 if (CONSTRUCTOR_NELTS (ctor) != 0)
8456 tree cons_elem = TREE_TYPE (CONSTRUCTOR_ELT (ctor, 0)->value);
8457 if (TREE_CODE (cons_elem) == VECTOR_TYPE)
8458 k = TYPE_VECTOR_SUBPARTS (cons_elem);
8460 unsigned HOST_WIDE_INT elt, count, const_k;
8463 /* We keep an exact subset of the constructor elements. */
8464 (if (multiple_p (idx, k, &elt) && multiple_p (n, k, &count))
8465 (if (CONSTRUCTOR_NELTS (ctor) == 0)
8466 { build_zero_cst (type); }
8468 (if (elt < CONSTRUCTOR_NELTS (ctor))
8469 (view_convert { CONSTRUCTOR_ELT (ctor, elt)->value; })
8470 { build_zero_cst (type); })
8471 /* We don't want to emit new CTORs unless the old one goes away.
8472 ??? Eventually allow this if the CTOR ends up constant or
8474 (if (single_use (@0))
8477 vec<constructor_elt, va_gc> *vals;
8478 vec_alloc (vals, count);
8479 bool constant_p = true;
8481 for (unsigned i = 0;
8482 i < count && elt + i < CONSTRUCTOR_NELTS (ctor); ++i)
8484 tree e = CONSTRUCTOR_ELT (ctor, elt + i)->value;
8485 CONSTRUCTOR_APPEND_ELT (vals, NULL_TREE, e);
8486 if (!CONSTANT_CLASS_P (e))
8489 tree evtype = (types_match (TREE_TYPE (type),
8490 TREE_TYPE (TREE_TYPE (ctor)))
8492 : build_vector_type (TREE_TYPE (TREE_TYPE (ctor)),
8494 /* We used to build a CTOR in the non-constant case here
8495 but that's not a GIMPLE value. We'd have to expose this
8496 operation somehow so the code generation can properly
8497 split it out to a separate stmt. */
8498 res = (constant_p ? build_vector_from_ctor (evtype, vals)
8499 : (GIMPLE ? NULL_TREE : build_constructor (evtype, vals)));
8502 (view_convert { res; })))))))
8503 /* The bitfield references a single constructor element. */
8504 (if (k.is_constant (&const_k)
8505 && idx + n <= (idx / const_k + 1) * const_k)
8507 (if (CONSTRUCTOR_NELTS (ctor) <= idx / const_k)
8508 { build_zero_cst (type); })
8510 (view_convert { CONSTRUCTOR_ELT (ctor, idx / const_k)->value; }))
8511 (BIT_FIELD_REF { CONSTRUCTOR_ELT (ctor, idx / const_k)->value; }
8512 @1 { bitsize_int ((idx % const_k) * width); })))))))))
8514 /* Simplify a bit extraction from a bit insertion for the cases with
8515 the inserted element fully covering the extraction or the insertion
8516 not touching the extraction. */
8518 (BIT_FIELD_REF (bit_insert @0 @1 @ipos) @rsize @rpos)
8521 unsigned HOST_WIDE_INT isize;
8522 if (INTEGRAL_TYPE_P (TREE_TYPE (@1)))
8523 isize = TYPE_PRECISION (TREE_TYPE (@1));
8525 isize = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (@1)));
8528 (if ((!INTEGRAL_TYPE_P (TREE_TYPE (@1))
8529 || type_has_mode_precision_p (TREE_TYPE (@1)))
8530 && wi::leu_p (wi::to_wide (@ipos), wi::to_wide (@rpos))
8531 && wi::leu_p (wi::to_wide (@rpos) + wi::to_wide (@rsize),
8532 wi::to_wide (@ipos) + isize))
8533 (BIT_FIELD_REF @1 @rsize { wide_int_to_tree (bitsizetype,
8535 - wi::to_wide (@ipos)); }))
8536 (if (wi::eq_p (wi::to_wide (@ipos), wi::to_wide (@rpos))
8537 && compare_tree_int (@rsize, isize) == 0)
8539 (if (wi::geu_p (wi::to_wide (@ipos),
8540 wi::to_wide (@rpos) + wi::to_wide (@rsize))
8541 || wi::geu_p (wi::to_wide (@rpos),
8542 wi::to_wide (@ipos) + isize))
8543 (BIT_FIELD_REF @0 @rsize @rpos)))))
8545 /* Simplify vector inserts of other vector extracts to a permute. */
8547 (bit_insert @0 (BIT_FIELD_REF@2 @1 @rsize @rpos) @ipos)
8548 (if (VECTOR_TYPE_P (type)
8549 && (VECTOR_MODE_P (TYPE_MODE (type))
8550 || optimize_vectors_before_lowering_p ())
8551 && types_match (@0, @1)
8552 && types_match (TREE_TYPE (TREE_TYPE (@0)), TREE_TYPE (@2))
8553 && TYPE_VECTOR_SUBPARTS (type).is_constant ())
8556 unsigned HOST_WIDE_INT elsz
8557 = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (TREE_TYPE (@1))));
8558 poly_uint64 relt = exact_div (tree_to_poly_uint64 (@rpos), elsz);
8559 poly_uint64 ielt = exact_div (tree_to_poly_uint64 (@ipos), elsz);
8560 unsigned nunits = TYPE_VECTOR_SUBPARTS (type).to_constant ();
8561 vec_perm_builder builder;
8562 builder.new_vector (nunits, nunits, 1);
8563 for (unsigned i = 0; i < nunits; ++i)
8564 builder.quick_push (known_eq (ielt, i) ? nunits + relt : i);
8565 vec_perm_indices sel (builder, 2, nunits);
8567 (if (!VECTOR_MODE_P (TYPE_MODE (type))
8568 || can_vec_perm_const_p (TYPE_MODE (type), TYPE_MODE (type), sel, false))
8569 (vec_perm @0 @1 { vec_perm_indices_to_tree
8570 (build_vector_type (ssizetype, nunits), sel); })))))
8572 (if (canonicalize_math_after_vectorization_p ())
8575 (fmas:c (negate @0) @1 @2)
8576 (IFN_FNMA @0 @1 @2))
8578 (fmas @0 @1 (negate @2))
8581 (fmas:c (negate @0) @1 (negate @2))
8582 (IFN_FNMS @0 @1 @2))
8584 (negate (fmas@3 @0 @1 @2))
8585 (if (single_use (@3))
8586 (IFN_FNMS @0 @1 @2))))
8589 (IFN_FMS:c (negate @0) @1 @2)
8590 (IFN_FNMS @0 @1 @2))
8592 (IFN_FMS @0 @1 (negate @2))
8595 (IFN_FMS:c (negate @0) @1 (negate @2))
8596 (IFN_FNMA @0 @1 @2))
8598 (negate (IFN_FMS@3 @0 @1 @2))
8599 (if (single_use (@3))
8600 (IFN_FNMA @0 @1 @2)))
8603 (IFN_FNMA:c (negate @0) @1 @2)
8606 (IFN_FNMA @0 @1 (negate @2))
8607 (IFN_FNMS @0 @1 @2))
8609 (IFN_FNMA:c (negate @0) @1 (negate @2))
8612 (negate (IFN_FNMA@3 @0 @1 @2))
8613 (if (single_use (@3))
8614 (IFN_FMS @0 @1 @2)))
8617 (IFN_FNMS:c (negate @0) @1 @2)
8620 (IFN_FNMS @0 @1 (negate @2))
8621 (IFN_FNMA @0 @1 @2))
8623 (IFN_FNMS:c (negate @0) @1 (negate @2))
8626 (negate (IFN_FNMS@3 @0 @1 @2))
8627 (if (single_use (@3))
8628 (IFN_FMA @0 @1 @2))))
8630 /* CLZ simplifications. */
8635 (op (clz:s@2 @0) INTEGER_CST@1)
8636 (if (integer_zerop (@1) && single_use (@2))
8637 /* clz(X) == 0 is (int)X < 0 and clz(X) != 0 is (int)X >= 0. */
8638 (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
8639 (cmp (convert:stype @0) { build_zero_cst (stype); }))
8640 /* clz(X) == (prec-1) is X == 1 and clz(X) != (prec-1) is X != 1. */
8641 (if (wi::to_wide (@1) == TYPE_PRECISION (TREE_TYPE (@0)) - 1)
8642 (op @0 { build_one_cst (TREE_TYPE (@0)); }))))))
8646 (op (IFN_CLZ:s@2 @0 @3) INTEGER_CST@1)
8647 (if (integer_zerop (@1) && single_use (@2))
8648 /* clz(X) == 0 is (int)X < 0 and clz(X) != 0 is (int)X >= 0. */
8649 (with { tree type0 = TREE_TYPE (@0);
8650 tree stype = signed_type_for (TREE_TYPE (@0));
8651 /* Punt if clz(0) == 0. */
8652 if (integer_zerop (@3))
8656 (cmp (convert:stype @0) { build_zero_cst (stype); })))
8657 /* clz(X) == (prec-1) is X == 1 and clz(X) != (prec-1) is X != 1. */
8658 (with { bool ok = true;
8659 tree type0 = TREE_TYPE (@0);
8660 /* Punt if clz(0) == prec - 1. */
8661 if (wi::to_widest (@3) == TYPE_PRECISION (type0) - 1)
8664 (if (ok && wi::to_wide (@1) == (TYPE_PRECISION (type0) - 1))
8665 (op @0 { build_one_cst (type0); }))))))
8667 /* CTZ simplifications. */
8669 (for op (ge gt le lt)
8672 /* __builtin_ctz (x) >= C -> (x & ((1 << C) - 1)) == 0. */
8673 (op (ctz:s @0) INTEGER_CST@1)
8674 (with { bool ok = true;
8675 HOST_WIDE_INT val = 0;
8676 if (!tree_fits_shwi_p (@1))
8680 val = tree_to_shwi (@1);
8681 /* Canonicalize to >= or <. */
8682 if (op == GT_EXPR || op == LE_EXPR)
8684 if (val == HOST_WIDE_INT_MAX)
8690 tree type0 = TREE_TYPE (@0);
8691 int prec = TYPE_PRECISION (type0);
8693 (if (ok && prec <= MAX_FIXED_MODE_SIZE)
8695 { constant_boolean_node (cmp == EQ_EXPR ? true : false, type); }
8697 { constant_boolean_node (cmp == EQ_EXPR ? false : true, type); }
8698 (cmp (bit_and @0 { wide_int_to_tree (type0,
8699 wi::mask (val, false, prec)); })
8700 { build_zero_cst (type0); })))))))
8703 /* __builtin_ctz (x) == C -> (x & ((1 << (C + 1)) - 1)) == (1 << C). */
8704 (op (ctz:s @0) INTEGER_CST@1)
8705 (with { tree type0 = TREE_TYPE (@0);
8706 int prec = TYPE_PRECISION (type0);
8708 (if (prec <= MAX_FIXED_MODE_SIZE)
8709 (if (tree_int_cst_sgn (@1) < 0 || wi::to_widest (@1) >= prec)
8710 { constant_boolean_node (op == EQ_EXPR ? false : true, type); }
8711 (op (bit_and @0 { wide_int_to_tree (type0,
8712 wi::mask (tree_to_uhwi (@1) + 1,
8714 { wide_int_to_tree (type0,
8715 wi::shifted_mask (tree_to_uhwi (@1), 1,
8716 false, prec)); })))))))
8717 (for op (ge gt le lt)
8720 /* __builtin_ctz (x) >= C -> (x & ((1 << C) - 1)) == 0. */
8721 (op (IFN_CTZ:s @0 @2) INTEGER_CST@1)
8722 (with { bool ok = true;
8723 HOST_WIDE_INT val = 0;
8724 if (!tree_fits_shwi_p (@1))
8728 val = tree_to_shwi (@1);
8729 /* Canonicalize to >= or <. */
8730 if (op == GT_EXPR || op == LE_EXPR)
8732 if (val == HOST_WIDE_INT_MAX)
8738 HOST_WIDE_INT zero_val = tree_to_shwi (@2);
8739 tree type0 = TREE_TYPE (@0);
8740 int prec = TYPE_PRECISION (type0);
8741 if (prec > MAX_FIXED_MODE_SIZE)
8745 (if (ok && zero_val >= val)
8746 { constant_boolean_node (cmp == EQ_EXPR ? true : false, type); })
8748 (if (ok && zero_val < val)
8749 { constant_boolean_node (cmp == EQ_EXPR ? false : true, type); })
8750 (if (ok && (zero_val < 0 || zero_val >= prec))
8751 (cmp (bit_and @0 { wide_int_to_tree (type0,
8752 wi::mask (val, false, prec)); })
8753 { build_zero_cst (type0); })))))))
8756 /* __builtin_ctz (x) == C -> (x & ((1 << (C + 1)) - 1)) == (1 << C). */
8757 (op (IFN_CTZ:s @0 @2) INTEGER_CST@1)
8758 (with { HOST_WIDE_INT zero_val = tree_to_shwi (@2);
8759 tree type0 = TREE_TYPE (@0);
8760 int prec = TYPE_PRECISION (type0);
8762 (if (prec <= MAX_FIXED_MODE_SIZE)
8763 (if (tree_int_cst_sgn (@1) < 0 || wi::to_widest (@1) >= prec)
8764 (if (zero_val != wi::to_widest (@1))
8765 { constant_boolean_node (op == EQ_EXPR ? false : true, type); })
8766 (if (zero_val < 0 || zero_val >= prec)
8767 (op (bit_and @0 { wide_int_to_tree (type0,
8768 wi::mask (tree_to_uhwi (@1) + 1,
8770 { wide_int_to_tree (type0,
8771 wi::shifted_mask (tree_to_uhwi (@1), 1,
8772 false, prec)); })))))))
8775 /* ctz(ext(X)) == ctz(X). Valid just for the UB at zero cases though. */
8777 (CTZ (convert@1 @0))
8778 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
8779 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
8780 && TYPE_PRECISION (TREE_TYPE (@1)) > TYPE_PRECISION (TREE_TYPE (@0)))
8781 (with { combined_fn cfn = CFN_LAST;
8782 tree type0 = TREE_TYPE (@0);
8783 if (TREE_CODE (type0) == BITINT_TYPE)
8785 if (TYPE_PRECISION (type0) > MAX_FIXED_MODE_SIZE)
8789 = build_nonstandard_integer_type (TYPE_PRECISION (type0),
8792 type0 = unsigned_type_for (type0);
8794 && direct_internal_fn_supported_p (IFN_CTZ, type0,
8798 && TYPE_PRECISION (TREE_TYPE (@1)) > BITS_PER_WORD
8799 && !direct_internal_fn_supported_p (IFN_CTZ,
8803 if (TYPE_PRECISION (type0)
8804 == TYPE_PRECISION (unsigned_type_node))
8805 cfn = CFN_BUILT_IN_CTZ;
8806 else if (TYPE_PRECISION (type0)
8807 == TYPE_PRECISION (long_long_unsigned_type_node))
8808 cfn = CFN_BUILT_IN_CTZLL;
8810 (if (cfn == CFN_CTZ)
8811 (IFN_CTZ (convert:type0 @0))
8812 (if (cfn == CFN_BUILT_IN_CTZ)
8813 (BUILT_IN_CTZ (convert:type0 @0))
8814 (if (cfn == CFN_BUILT_IN_CTZLL)
8815 (BUILT_IN_CTZLL (convert:type0 @0))))))))
8818 /* POPCOUNT simplifications. */
8819 /* popcount(X) + popcount(Y) is popcount(X|Y) when X&Y must be zero. */
8821 (plus (POPCOUNT:s @0) (POPCOUNT:s @1))
8822 (if (INTEGRAL_TYPE_P (type)
8823 && (wi::bit_and (widest_int::from (tree_nonzero_bits (@0), UNSIGNED),
8824 widest_int::from (tree_nonzero_bits (@1), UNSIGNED))
8826 (with { tree utype = TREE_TYPE (@0);
8827 if (TYPE_PRECISION (utype) < TYPE_PRECISION (TREE_TYPE (@1)))
8828 utype = TREE_TYPE (@1); }
8829 (POPCOUNT (bit_ior (convert:utype @0) (convert:utype @1))))))
8831 /* popcount(X) == 0 is X == 0, and related (in)equalities. */
8832 (for popcount (POPCOUNT)
8833 (for cmp (le eq ne gt)
8836 (cmp (popcount @0) integer_zerop)
8837 (rep @0 { build_zero_cst (TREE_TYPE (@0)); }))))
8839 /* popcount(bswap(x)) is popcount(x). */
8840 (for popcount (POPCOUNT)
8841 (for bswap (BUILT_IN_BSWAP16 BUILT_IN_BSWAP32
8842 BUILT_IN_BSWAP64 BUILT_IN_BSWAP128)
8844 (popcount (convert?@0 (bswap:s@1 @2)))
8845 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
8846 && INTEGRAL_TYPE_P (TREE_TYPE (@1)))
8847 (with { tree type0 = TREE_TYPE (@0);
8848 tree type1 = TREE_TYPE (@1);
8849 unsigned int prec0 = TYPE_PRECISION (type0);
8850 unsigned int prec1 = TYPE_PRECISION (type1); }
8851 (if (prec0 == prec1 || (prec0 > prec1 && TYPE_UNSIGNED (type1)))
8852 (popcount (convert:type0 (convert:type1 @2)))))))))
8854 /* popcount(rotate(X Y)) is popcount(X). */
8855 (for popcount (POPCOUNT)
8856 (for rot (lrotate rrotate)
8858 (popcount (convert?@0 (rot:s@1 @2 @3)))
8859 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
8860 && INTEGRAL_TYPE_P (TREE_TYPE (@1))
8861 && (GIMPLE || !TREE_SIDE_EFFECTS (@3)))
8862 (with { tree type0 = TREE_TYPE (@0);
8863 tree type1 = TREE_TYPE (@1);
8864 unsigned int prec0 = TYPE_PRECISION (type0);
8865 unsigned int prec1 = TYPE_PRECISION (type1); }
8866 (if (prec0 == prec1 || (prec0 > prec1 && TYPE_UNSIGNED (type1)))
8867 (popcount (convert:type0 @2))))))))
8869 /* Canonicalize POPCOUNT(x)&1 as PARITY(X). */
8871 (bit_and (POPCOUNT @0) integer_onep)
8874 /* popcount(X&Y) + popcount(X|Y) is popcount(x) + popcount(Y). */
8876 (plus:c (POPCOUNT:s (bit_and:s @0 @1)) (POPCOUNT:s (bit_ior:cs @0 @1)))
8877 (plus (POPCOUNT:type @0) (POPCOUNT:type @1)))
8879 /* popcount(X) + popcount(Y) - popcount(X&Y) is popcount(X|Y). */
8880 /* popcount(X) + popcount(Y) - popcount(X|Y) is popcount(X&Y). */
8881 (for popcount (POPCOUNT)
8882 (for log1 (bit_and bit_ior)
8883 log2 (bit_ior bit_and)
8885 (minus (plus:s (popcount:s @0) (popcount:s @1))
8886 (popcount:s (log1:cs @0 @1)))
8887 (popcount (log2 @0 @1)))
8889 (plus:c (minus:s (popcount:s @0) (popcount:s (log1:cs @0 @1)))
8891 (popcount (log2 @0 @1)))))
8894 /* popcount(zext(X)) == popcount(X). */
8896 (POPCOUNT (convert@1 @0))
8897 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
8898 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
8899 && TYPE_UNSIGNED (TREE_TYPE (@0))
8900 && TYPE_PRECISION (TREE_TYPE (@1)) > TYPE_PRECISION (TREE_TYPE (@0)))
8901 (with { combined_fn cfn = CFN_LAST;
8902 tree type0 = TREE_TYPE (@0);
8903 if (TREE_CODE (type0) == BITINT_TYPE)
8905 if (TYPE_PRECISION (type0) > MAX_FIXED_MODE_SIZE)
8909 = build_nonstandard_integer_type (TYPE_PRECISION (type0),
8913 && direct_internal_fn_supported_p (IFN_POPCOUNT, type0,
8917 && TYPE_PRECISION (TREE_TYPE (@1)) > BITS_PER_WORD
8918 && !direct_internal_fn_supported_p (IFN_POPCOUNT,
8922 if (TYPE_PRECISION (type0)
8923 == TYPE_PRECISION (unsigned_type_node))
8924 cfn = CFN_BUILT_IN_POPCOUNT;
8925 else if (TYPE_PRECISION (type0)
8926 == TYPE_PRECISION (long_long_unsigned_type_node))
8927 cfn = CFN_BUILT_IN_POPCOUNTLL;
8929 (if (cfn == CFN_POPCOUNT)
8930 (IFN_POPCOUNT (convert:type0 @0))
8931 (if (cfn == CFN_BUILT_IN_POPCOUNT)
8932 (BUILT_IN_POPCOUNT (convert:type0 @0))
8933 (if (cfn == CFN_BUILT_IN_POPCOUNTLL)
8934 (BUILT_IN_POPCOUNTLL (convert:type0 @0))))))))
8937 /* PARITY simplifications. */
8938 /* parity(~X) is parity(X). */
8940 (PARITY (bit_not @0))
8943 /* parity(bswap(x)) is parity(x). */
8944 (for parity (PARITY)
8945 (for bswap (BUILT_IN_BSWAP16 BUILT_IN_BSWAP32
8946 BUILT_IN_BSWAP64 BUILT_IN_BSWAP128)
8948 (parity (convert?@0 (bswap:s@1 @2)))
8949 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
8950 && INTEGRAL_TYPE_P (TREE_TYPE (@1))
8951 && TYPE_PRECISION (TREE_TYPE (@0))
8952 >= TYPE_PRECISION (TREE_TYPE (@1)))
8953 (with { tree type0 = TREE_TYPE (@0);
8954 tree type1 = TREE_TYPE (@1); }
8955 (parity (convert:type0 (convert:type1 @2))))))))
8957 /* parity(rotate(X Y)) is parity(X). */
8958 (for parity (PARITY)
8959 (for rot (lrotate rrotate)
8961 (parity (convert?@0 (rot:s@1 @2 @3)))
8962 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
8963 && INTEGRAL_TYPE_P (TREE_TYPE (@1))
8964 && (GIMPLE || !TREE_SIDE_EFFECTS (@3))
8965 && TYPE_PRECISION (TREE_TYPE (@0))
8966 >= TYPE_PRECISION (TREE_TYPE (@1)))
8967 (with { tree type0 = TREE_TYPE (@0); }
8968 (parity (convert:type0 @2)))))))
8970 /* parity(X)^parity(Y) is parity(X^Y). */
8972 (bit_xor (PARITY:s @0) (PARITY:s @1))
8973 (if (types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
8974 (PARITY (bit_xor @0 @1))
8975 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
8976 && INTEGRAL_TYPE_P (TREE_TYPE (@1)))
8977 (with { tree utype = TREE_TYPE (@0);
8978 if (TYPE_PRECISION (utype) < TYPE_PRECISION (TREE_TYPE (@1)))
8979 utype = TREE_TYPE (@1); }
8980 (PARITY (bit_xor (convert:utype @0) (convert:utype @1)))))))
8983 /* parity(zext(X)) == parity(X). */
8984 /* parity(sext(X)) == parity(X) if the difference in precision is even. */
8986 (PARITY (convert@1 @0))
8987 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
8988 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
8989 && TYPE_PRECISION (TREE_TYPE (@1)) > TYPE_PRECISION (TREE_TYPE (@0))
8990 && (TYPE_UNSIGNED (TREE_TYPE (@0))
8991 || ((TYPE_PRECISION (TREE_TYPE (@1))
8992 - TYPE_PRECISION (TREE_TYPE (@0))) & 1) == 0))
8993 (with { combined_fn cfn = CFN_LAST;
8994 tree type0 = TREE_TYPE (@0);
8995 if (TREE_CODE (type0) == BITINT_TYPE)
8997 if (TYPE_PRECISION (type0) > MAX_FIXED_MODE_SIZE)
9001 = build_nonstandard_integer_type (TYPE_PRECISION (type0),
9004 type0 = unsigned_type_for (type0);
9006 && direct_internal_fn_supported_p (IFN_PARITY, type0,
9010 && TYPE_PRECISION (TREE_TYPE (@1)) > BITS_PER_WORD
9011 && !direct_internal_fn_supported_p (IFN_PARITY,
9015 if (TYPE_PRECISION (type0)
9016 == TYPE_PRECISION (unsigned_type_node))
9017 cfn = CFN_BUILT_IN_PARITY;
9018 else if (TYPE_PRECISION (type0)
9019 == TYPE_PRECISION (long_long_unsigned_type_node))
9020 cfn = CFN_BUILT_IN_PARITYLL;
9022 (if (cfn == CFN_PARITY)
9023 (IFN_PARITY (convert:type0 @0))
9024 (if (cfn == CFN_BUILT_IN_PARITY)
9025 (BUILT_IN_PARITY (convert:type0 @0))
9026 (if (cfn == CFN_BUILT_IN_PARITYLL)
9027 (BUILT_IN_PARITYLL (convert:type0 @0))))))))
9030 /* a != 0 ? FUN(a) : 0 -> Fun(a) for some builtin functions. */
9031 (for func (POPCOUNT BSWAP FFS PARITY)
9033 (cond (ne @0 integer_zerop@1) (func@3 (convert? @0)) integer_zerop@2)
9036 /* a != 0 ? FUN(a) : CST -> Fun(a) for some CLRSB builtins
9037 where CST is precision-1. */
9040 (cond (ne @0 integer_zerop@1) (func@4 (convert?@3 @0)) INTEGER_CST@2)
9041 (if (wi::to_widest (@2) == TYPE_PRECISION (TREE_TYPE (@3)) - 1)
9045 /* a != 0 ? CLZ(a) : CST -> .CLZ(a) where CST is the result of the internal function for 0. */
9048 (cond (ne @0 integer_zerop@1) (func (convert?@3 @0)) INTEGER_CST@2)
9050 internal_fn ifn = IFN_LAST;
9051 if (TREE_CODE (TREE_TYPE (@3)) == BITINT_TYPE)
9053 if (tree_fits_shwi_p (@2))
9055 HOST_WIDE_INT valw = tree_to_shwi (@2);
9056 if ((int) valw == valw)
9063 else if (direct_internal_fn_supported_p (IFN_CLZ, TREE_TYPE (@3),
9065 && CLZ_DEFINED_VALUE_AT_ZERO
9066 (SCALAR_INT_TYPE_MODE (TREE_TYPE (@3)), val) == 2)
9069 (if (ifn == IFN_CLZ && wi::to_widest (@2) == val)
9072 (cond (ne @0 integer_zerop@1) (IFN_CLZ (convert?@3 @0) INTEGER_CST@2) @2)
9074 internal_fn ifn = IFN_LAST;
9075 if (TREE_CODE (TREE_TYPE (@3)) == BITINT_TYPE)
9077 else if (direct_internal_fn_supported_p (IFN_CLZ, TREE_TYPE (@3),
9081 (if (ifn == IFN_CLZ)
9084 /* a != 0 ? CTZ(a) : CST -> .CTZ(a) where CST is the result of the internal function for 0. */
9087 (cond (ne @0 integer_zerop@1) (func (convert?@3 @0)) INTEGER_CST@2)
9089 internal_fn ifn = IFN_LAST;
9090 if (TREE_CODE (TREE_TYPE (@3)) == BITINT_TYPE)
9092 if (tree_fits_shwi_p (@2))
9094 HOST_WIDE_INT valw = tree_to_shwi (@2);
9095 if ((int) valw == valw)
9102 else if (direct_internal_fn_supported_p (IFN_CTZ, TREE_TYPE (@3),
9104 && CTZ_DEFINED_VALUE_AT_ZERO
9105 (SCALAR_INT_TYPE_MODE (TREE_TYPE (@3)), val) == 2)
9108 (if (ifn == IFN_CTZ && wi::to_widest (@2) == val)
9111 (cond (ne @0 integer_zerop@1) (IFN_CTZ (convert?@3 @0) INTEGER_CST@2) @2)
9113 internal_fn ifn = IFN_LAST;
9114 if (TREE_CODE (TREE_TYPE (@3)) == BITINT_TYPE)
9116 else if (direct_internal_fn_supported_p (IFN_CTZ, TREE_TYPE (@3),
9120 (if (ifn == IFN_CTZ)
9124 /* Common POPCOUNT/PARITY simplifications. */
9125 /* popcount(X&C1) is (X>>C2)&1 when C1 == 1<<C2. Same for parity(X&C1). */
9126 (for pfun (POPCOUNT PARITY)
9129 (if (INTEGRAL_TYPE_P (type))
9130 (with { wide_int nz = tree_nonzero_bits (@0); }
9134 (if (wi::popcount (nz) == 1)
9135 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
9136 (convert (rshift:utype (convert:utype @0)
9137 { build_int_cst (integer_type_node,
9138 wi::ctz (nz)); })))))))))
9141 /* 64- and 32-bits branchless implementations of popcount are detected:
9143 int popcount64c (uint64_t x)
9145 x -= (x >> 1) & 0x5555555555555555ULL;
9146 x = (x & 0x3333333333333333ULL) + ((x >> 2) & 0x3333333333333333ULL);
9147 x = (x + (x >> 4)) & 0x0f0f0f0f0f0f0f0fULL;
9148 return (x * 0x0101010101010101ULL) >> 56;
9151 int popcount32c (uint32_t x)
9153 x -= (x >> 1) & 0x55555555;
9154 x = (x & 0x33333333) + ((x >> 2) & 0x33333333);
9155 x = (x + (x >> 4)) & 0x0f0f0f0f;
9156 return (x * 0x01010101) >> 24;
9163 (rshift @8 INTEGER_CST@5)
9165 (bit_and @6 INTEGER_CST@7)
9169 (bit_and (rshift @0 INTEGER_CST@4) INTEGER_CST@11))
9175 /* Check constants and optab. */
9176 (with { unsigned prec = TYPE_PRECISION (type);
9177 int shift = (64 - prec) & 63;
9178 unsigned HOST_WIDE_INT c1
9179 = HOST_WIDE_INT_UC (0x0101010101010101) >> shift;
9180 unsigned HOST_WIDE_INT c2
9181 = HOST_WIDE_INT_UC (0x0F0F0F0F0F0F0F0F) >> shift;
9182 unsigned HOST_WIDE_INT c3
9183 = HOST_WIDE_INT_UC (0x3333333333333333) >> shift;
9184 unsigned HOST_WIDE_INT c4
9185 = HOST_WIDE_INT_UC (0x5555555555555555) >> shift;
9190 && TYPE_UNSIGNED (type)
9191 && integer_onep (@4)
9192 && wi::to_widest (@10) == 2
9193 && wi::to_widest (@5) == 4
9194 && wi::to_widest (@1) == prec - 8
9195 && tree_to_uhwi (@2) == c1
9196 && tree_to_uhwi (@3) == c2
9197 && tree_to_uhwi (@9) == c3
9198 && tree_to_uhwi (@7) == c3
9199 && tree_to_uhwi (@11) == c4)
9200 (if (direct_internal_fn_supported_p (IFN_POPCOUNT, type,
9202 (convert (IFN_POPCOUNT:type @0))
9203 /* Try to do popcount in two halves. PREC must be at least
9204 five bits for this to work without extension before adding. */
9206 tree half_type = NULL_TREE;
9207 opt_machine_mode m = mode_for_size ((prec + 1) / 2, MODE_INT, 1);
9210 && m.require () != TYPE_MODE (type))
9212 half_prec = GET_MODE_PRECISION (as_a <scalar_int_mode> (m));
9213 half_type = build_nonstandard_integer_type (half_prec, 1);
9215 gcc_assert (half_prec > 2);
9217 (if (half_type != NULL_TREE
9218 && direct_internal_fn_supported_p (IFN_POPCOUNT, half_type,
9221 (IFN_POPCOUNT:half_type (convert @0))
9222 (IFN_POPCOUNT:half_type (convert (rshift @0
9223 { build_int_cst (integer_type_node, half_prec); } )))))))))))
9225 /* __builtin_ffs needs to deal on many targets with the possible zero
9226 argument. If we know the argument is always non-zero, __builtin_ctz + 1
9227 should lead to better code. */
9229 (FFS tree_expr_nonzero_p@0)
9230 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
9231 && direct_internal_fn_supported_p (IFN_CTZ, TREE_TYPE (@0),
9232 OPTIMIZE_FOR_SPEED))
9233 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
9234 (plus (CTZ:type (convert:utype @0)) { build_one_cst (type); }))))
9238 /* __builtin_ffs (X) == 0 -> X == 0.
9239 __builtin_ffs (X) == 6 -> (X & 63) == 32. */
9242 (cmp (ffs@2 @0) INTEGER_CST@1)
9243 (with { int prec = TYPE_PRECISION (TREE_TYPE (@0)); }
9245 (if (integer_zerop (@1))
9246 (cmp @0 { build_zero_cst (TREE_TYPE (@0)); }))
9247 (if (tree_int_cst_sgn (@1) < 0 || wi::to_widest (@1) > prec)
9248 { constant_boolean_node (cmp == NE_EXPR ? true : false, type); })
9249 (if (single_use (@2))
9250 (cmp (bit_and @0 { wide_int_to_tree (TREE_TYPE (@0),
9251 wi::mask (tree_to_uhwi (@1),
9253 { wide_int_to_tree (TREE_TYPE (@0),
9254 wi::shifted_mask (tree_to_uhwi (@1) - 1, 1,
9255 false, prec)); }))))))
9257 /* __builtin_ffs (X) > 6 -> X != 0 && (X & 63) == 0. */
9261 bit_op (bit_and bit_ior)
9263 (cmp (ffs@2 @0) INTEGER_CST@1)
9264 (with { int prec = TYPE_PRECISION (TREE_TYPE (@0)); }
9266 (if (integer_zerop (@1))
9267 (cmp2 @0 { build_zero_cst (TREE_TYPE (@0)); }))
9268 (if (tree_int_cst_sgn (@1) < 0)
9269 { constant_boolean_node (cmp == GT_EXPR ? true : false, type); })
9270 (if (wi::to_widest (@1) >= prec)
9271 { constant_boolean_node (cmp == GT_EXPR ? false : true, type); })
9272 (if (wi::to_widest (@1) == prec - 1)
9273 (cmp3 @0 { wide_int_to_tree (TREE_TYPE (@0),
9274 wi::shifted_mask (prec - 1, 1,
9276 (if (single_use (@2))
9277 (bit_op (cmp2 @0 { build_zero_cst (TREE_TYPE (@0)); })
9279 { wide_int_to_tree (TREE_TYPE (@0),
9280 wi::mask (tree_to_uhwi (@1),
9282 { build_zero_cst (TREE_TYPE (@0)); }))))))))
9285 /* ffs(ext(X)) == ffs(X). */
9287 (FFS (convert@1 @0))
9288 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
9289 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
9290 && TYPE_PRECISION (TREE_TYPE (@1)) > TYPE_PRECISION (TREE_TYPE (@0)))
9291 (with { combined_fn cfn = CFN_LAST;
9292 tree type0 = TREE_TYPE (@0);
9293 if (TREE_CODE (type0) == BITINT_TYPE)
9295 if (TYPE_PRECISION (type0) > MAX_FIXED_MODE_SIZE)
9299 = build_nonstandard_integer_type (TYPE_PRECISION (type0),
9302 type0 = signed_type_for (type0);
9304 && direct_internal_fn_supported_p (IFN_FFS, type0,
9308 && TYPE_PRECISION (TREE_TYPE (@1)) > BITS_PER_WORD
9309 && !direct_internal_fn_supported_p (IFN_FFS,
9313 if (TYPE_PRECISION (type0)
9314 == TYPE_PRECISION (integer_type_node))
9315 cfn = CFN_BUILT_IN_FFS;
9316 else if (TYPE_PRECISION (type0)
9317 == TYPE_PRECISION (long_long_integer_type_node))
9318 cfn = CFN_BUILT_IN_FFSLL;
9320 (if (cfn == CFN_FFS)
9321 (IFN_FFS (convert:type0 @0))
9322 (if (cfn == CFN_BUILT_IN_FFS)
9323 (BUILT_IN_FFS (convert:type0 @0))
9324 (if (cfn == CFN_BUILT_IN_FFSLL)
9325 (BUILT_IN_FFSLL (convert:type0 @0))))))))
9333 --> r = .COND_FN (cond, a, b)
9337 --> r = .COND_FN (~cond, b, a). */
9339 (for uncond_op (UNCOND_UNARY)
9340 cond_op (COND_UNARY)
9342 (vec_cond @0 (view_convert? (uncond_op@3 @1)) @2)
9343 (with { tree op_type = TREE_TYPE (@3); }
9344 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
9345 && is_truth_type_for (op_type, TREE_TYPE (@0)))
9346 (cond_op @0 (view_convert @1) @2))))
9348 (vec_cond @0 @1 (view_convert? (uncond_op@3 @2)))
9349 (with { tree op_type = TREE_TYPE (@3); }
9350 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
9351 && is_truth_type_for (op_type, TREE_TYPE (@0)))
9352 (cond_op (bit_not @0) (view_convert @2) @1)))))
9354 (for uncond_op (UNCOND_UNARY)
9355 cond_op (COND_LEN_UNARY)
9357 (IFN_VCOND_MASK_LEN @0 (view_convert? (uncond_op@3 @1)) @2 @4 @5)
9358 (with { tree op_type = TREE_TYPE (@3); }
9359 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
9360 && is_truth_type_for (op_type, TREE_TYPE (@0)))
9361 (cond_op @0 (view_convert @1) @2 @4 @5))))
9363 (IFN_VCOND_MASK_LEN @0 @1 (view_convert? (uncond_op@3 @2)) @4 @5)
9364 (with { tree op_type = TREE_TYPE (@3); }
9365 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
9366 && is_truth_type_for (op_type, TREE_TYPE (@0)))
9367 (cond_op (bit_not @0) (view_convert @2) @1 @4 @5)))))
9369 /* `(a ? -1 : 0) ^ b` can be converted into a conditional not. */
9371 (bit_xor:c (vec_cond @0 uniform_integer_cst_p@1 uniform_integer_cst_p@2) @3)
9372 (if (canonicalize_math_after_vectorization_p ()
9373 && vectorized_internal_fn_supported_p (IFN_COND_NOT, type)
9374 && is_truth_type_for (type, TREE_TYPE (@0)))
9375 (if (integer_all_onesp (@1) && integer_zerop (@2))
9376 (IFN_COND_NOT @0 @3 @3))
9377 (if (integer_all_onesp (@2) && integer_zerop (@1))
9378 (IFN_COND_NOT (bit_not @0) @3 @3))))
9387 r = c ? a1 op a2 : b;
9389 if the target can do it in one go. This makes the operation conditional
9390 on c, so could drop potentially-trapping arithmetic, but that's a valid
9391 simplification if the result of the operation isn't needed.
9393 Avoid speculatively generating a stand-alone vector comparison
9394 on targets that might not support them. Any target implementing
9395 conditional internal functions must support the same comparisons
9396 inside and outside a VEC_COND_EXPR. */
9398 (for uncond_op (UNCOND_BINARY)
9399 cond_op (COND_BINARY)
9401 (vec_cond @0 (view_convert? (uncond_op@4 @1 @2)) @3)
9402 (with { tree op_type = TREE_TYPE (@4); }
9403 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
9404 && is_truth_type_for (op_type, TREE_TYPE (@0))
9406 (view_convert (cond_op @0 @1 @2 (view_convert:op_type @3))))))
9408 (vec_cond @0 @1 (view_convert? (uncond_op@4 @2 @3)))
9409 (with { tree op_type = TREE_TYPE (@4); }
9410 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
9411 && is_truth_type_for (op_type, TREE_TYPE (@0))
9413 (view_convert (cond_op (bit_not @0) @2 @3 (view_convert:op_type @1)))))))
9415 (for uncond_op (UNCOND_BINARY)
9416 cond_op (COND_LEN_BINARY)
9418 (IFN_VCOND_MASK_LEN @0 (view_convert? (uncond_op@4 @1 @2)) @3 @5 @6)
9419 (with { tree op_type = TREE_TYPE (@4); }
9420 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
9421 && is_truth_type_for (op_type, TREE_TYPE (@0))
9423 (view_convert (cond_op @0 @1 @2 (view_convert:op_type @3) @5 @6)))))
9425 (IFN_VCOND_MASK_LEN @0 @1 (view_convert? (uncond_op@4 @2 @3)) @5 @6)
9426 (with { tree op_type = TREE_TYPE (@4); }
9427 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
9428 && is_truth_type_for (op_type, TREE_TYPE (@0))
9430 (view_convert (cond_op (bit_not @0) @2 @3 (view_convert:op_type @1) @5 @6))))))
9432 /* Same for ternary operations. */
9433 (for uncond_op (UNCOND_TERNARY)
9434 cond_op (COND_TERNARY)
9436 (vec_cond @0 (view_convert? (uncond_op@5 @1 @2 @3)) @4)
9437 (with { tree op_type = TREE_TYPE (@5); }
9438 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
9439 && is_truth_type_for (op_type, TREE_TYPE (@0))
9441 (view_convert (cond_op @0 @1 @2 @3 (view_convert:op_type @4))))))
9443 (vec_cond @0 @1 (view_convert? (uncond_op@5 @2 @3 @4)))
9444 (with { tree op_type = TREE_TYPE (@5); }
9445 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
9446 && is_truth_type_for (op_type, TREE_TYPE (@0))
9448 (view_convert (cond_op (bit_not @0) @2 @3 @4
9449 (view_convert:op_type @1)))))))
9451 (for uncond_op (UNCOND_TERNARY)
9452 cond_op (COND_LEN_TERNARY)
9454 (IFN_VCOND_MASK_LEN @0 (view_convert? (uncond_op@5 @1 @2 @3)) @4 @6 @7)
9455 (with { tree op_type = TREE_TYPE (@5); }
9456 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
9457 && is_truth_type_for (op_type, TREE_TYPE (@0))
9459 (view_convert (cond_op @0 @1 @2 @3 (view_convert:op_type @4) @6 @7)))))
9461 (IFN_VCOND_MASK_LEN @0 @1 (view_convert? (uncond_op@5 @2 @3 @4 @6 @7)))
9462 (with { tree op_type = TREE_TYPE (@5); }
9463 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
9464 && is_truth_type_for (op_type, TREE_TYPE (@0))
9466 (view_convert (cond_op (bit_not @0) @2 @3 @4 (view_convert:op_type @1) @6 @7))))))
9469 /* Detect cases in which a VEC_COND_EXPR effectively replaces the
9470 "else" value of an IFN_COND_*. */
9471 (for cond_op (COND_BINARY)
9473 (vec_cond @0 (view_convert? (cond_op @0 @1 @2 @3)) @4)
9474 (with { tree op_type = TREE_TYPE (@3); }
9475 (if (element_precision (type) == element_precision (op_type))
9476 (view_convert (cond_op @0 @1 @2 (view_convert:op_type @4))))))
9478 (vec_cond @0 @1 (view_convert? (cond_op @2 @3 @4 @5)))
9479 (with { tree op_type = TREE_TYPE (@5); }
9480 (if (inverse_conditions_p (@0, @2)
9481 && element_precision (type) == element_precision (op_type))
9482 (view_convert (cond_op @2 @3 @4 (view_convert:op_type @1)))))))
9484 /* Same for ternary operations. */
9485 (for cond_op (COND_TERNARY)
9487 (vec_cond @0 (view_convert? (cond_op @0 @1 @2 @3 @4)) @5)
9488 (with { tree op_type = TREE_TYPE (@4); }
9489 (if (element_precision (type) == element_precision (op_type))
9490 (view_convert (cond_op @0 @1 @2 @3 (view_convert:op_type @5))))))
9492 (vec_cond @0 @1 (view_convert? (cond_op @2 @3 @4 @5 @6)))
9493 (with { tree op_type = TREE_TYPE (@6); }
9494 (if (inverse_conditions_p (@0, @2)
9495 && element_precision (type) == element_precision (op_type))
9496 (view_convert (cond_op @2 @3 @4 @5 (view_convert:op_type @1)))))))
9498 /* Detect cases in which a VEC_COND_EXPR effectively replaces the
9499 "else" value of an IFN_COND_LEN_*. */
9500 (for cond_len_op (COND_LEN_BINARY)
9502 (vec_cond @0 (view_convert? (cond_len_op @0 @1 @2 @3 @4 @5)) @6)
9503 (with { tree op_type = TREE_TYPE (@3); }
9504 (if (element_precision (type) == element_precision (op_type))
9505 (view_convert (cond_len_op @0 @1 @2 (view_convert:op_type @6) @4 @5)))))
9507 (vec_cond @0 @1 (view_convert? (cond_len_op @2 @3 @4 @5 @6 @7)))
9508 (with { tree op_type = TREE_TYPE (@5); }
9509 (if (inverse_conditions_p (@0, @2)
9510 && element_precision (type) == element_precision (op_type))
9511 (view_convert (cond_len_op @2 @3 @4 (view_convert:op_type @1) @6 @7))))))
9513 /* Same for ternary operations. */
9514 (for cond_len_op (COND_LEN_TERNARY)
9516 (vec_cond @0 (view_convert? (cond_len_op @0 @1 @2 @3 @4 @5 @6)) @7)
9517 (with { tree op_type = TREE_TYPE (@4); }
9518 (if (element_precision (type) == element_precision (op_type))
9519 (view_convert (cond_len_op @0 @1 @2 @3 (view_convert:op_type @7) @5 @6)))))
9521 (vec_cond @0 @1 (view_convert? (cond_len_op @2 @3 @4 @5 @6 @7 @8)))
9522 (with { tree op_type = TREE_TYPE (@6); }
9523 (if (inverse_conditions_p (@0, @2)
9524 && element_precision (type) == element_precision (op_type))
9525 (view_convert (cond_len_op @2 @3 @4 @5 (view_convert:op_type @1) @7 @8))))))
9527 /* Detect simplication for a conditional reduction where
9530 c = mask2 ? d + a : d
9534 c = mask1 && mask2 ? d + b : d. */
9536 (IFN_COND_ADD @0 @1 (vec_cond @2 @3 zerop@4) @1)
9537 (if (ANY_INTEGRAL_TYPE_P (type)
9538 || (FLOAT_TYPE_P (type)
9539 && fold_real_zero_addition_p (type, NULL_TREE, @4, 0)))
9540 (IFN_COND_ADD (bit_and @0 @2) @1 @3 @1)))
9542 /* Detect simplication for a conditional length reduction where
9545 c = i < len + bias ? d + a : d
9549 c = mask && i < len + bias ? d + b : d. */
9551 (IFN_COND_LEN_ADD integer_truep @0 (vec_cond @1 @2 zerop@5) @0 @3 @4)
9552 (if (ANY_INTEGRAL_TYPE_P (type)
9553 || (FLOAT_TYPE_P (type)
9554 && fold_real_zero_addition_p (type, NULL_TREE, @5, 0)))
9555 (IFN_COND_LEN_ADD @1 @0 @2 @0 @3 @4)))
9557 /* Detect simplification for vector condition folding where
9559 c = mask1 ? (masked_op mask2 a b) : b
9563 c = masked_op (mask1 & mask2) a b
9565 where the operation can be partially applied to one operand. */
9567 (for cond_op (COND_BINARY)
9570 (cond_op:s @1 @2 @3 @4) @3)
9571 (cond_op (bit_and @1 @0) @2 @3 @4)))
9573 /* And same for ternary expressions. */
9575 (for cond_op (COND_TERNARY)
9578 (cond_op:s @1 @2 @3 @4 @5) @4)
9579 (cond_op (bit_and @1 @0) @2 @3 @4 @5)))
9581 /* For pointers @0 and @2 and nonnegative constant offset @1, look for
9584 A: (@0 + @1 < @2) | (@2 + @1 < @0)
9585 B: (@0 + @1 <= @2) | (@2 + @1 <= @0)
9587 If pointers are known not to wrap, B checks whether @1 bytes starting
9588 at @0 and @2 do not overlap, while A tests the same thing for @1 + 1
9589 bytes. A is more efficiently tested as:
9591 A: (sizetype) (@0 + @1 - @2) > @1 * 2
9593 The equivalent expression for B is given by replacing @1 with @1 - 1:
9595 B: (sizetype) (@0 + (@1 - 1) - @2) > (@1 - 1) * 2
9597 @0 and @2 can be swapped in both expressions without changing the result.
9599 The folds rely on sizetype's being unsigned (which is always true)
9600 and on its being the same width as the pointer (which we have to check).
9602 The fold replaces two pointer_plus expressions, two comparisons and
9603 an IOR with a pointer_plus, a pointer_diff, and a comparison, so in
9604 the best case it's a saving of two operations. The A fold retains one
9605 of the original pointer_pluses, so is a win even if both pointer_pluses
9606 are used elsewhere. The B fold is a wash if both pointer_pluses are
9607 used elsewhere, since all we end up doing is replacing a comparison with
9608 a pointer_plus. We do still apply the fold under those circumstances
9609 though, in case applying it to other conditions eventually makes one of the
9610 pointer_pluses dead. */
9611 (for ior (truth_orif truth_or bit_ior)
9614 (ior (cmp:cs (pointer_plus@3 @0 INTEGER_CST@1) @2)
9615 (cmp:cs (pointer_plus@4 @2 @1) @0))
9616 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
9617 && TYPE_OVERFLOW_WRAPS (sizetype)
9618 && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (sizetype))
9619 /* Calculate the rhs constant. */
9620 (with { offset_int off = wi::to_offset (@1) - (cmp == LE_EXPR ? 1 : 0);
9621 offset_int rhs = off * 2; }
9622 /* Always fails for negative values. */
9623 (if (wi::min_precision (rhs, UNSIGNED) <= TYPE_PRECISION (sizetype))
9624 /* Since the order of @0 and @2 doesn't matter, let tree_swap_operands_p
9625 pick a canonical order. This increases the chances of using the
9626 same pointer_plus in multiple checks. */
9627 (with { bool swap_p = tree_swap_operands_p (@0, @2);
9628 tree rhs_tree = wide_int_to_tree (sizetype, rhs); }
9629 (if (cmp == LT_EXPR)
9630 (gt (convert:sizetype
9631 (pointer_diff:ssizetype { swap_p ? @4 : @3; }
9632 { swap_p ? @0 : @2; }))
9634 (gt (convert:sizetype
9635 (pointer_diff:ssizetype
9636 (pointer_plus { swap_p ? @2 : @0; }
9637 { wide_int_to_tree (sizetype, off); })
9638 { swap_p ? @0 : @2; }))
9639 { rhs_tree; })))))))))
9641 /* Fold REDUC (@0 & @1) -> @0[I] & @1[I] if element I is the only nonzero
9643 (for reduc (IFN_REDUC_PLUS IFN_REDUC_IOR IFN_REDUC_XOR)
9644 (simplify (reduc (view_convert? (bit_and @0 VECTOR_CST@1)))
9645 (with { int i = single_nonzero_element (@1); }
9647 (with { tree elt = vector_cst_elt (@1, i);
9648 tree elt_type = TREE_TYPE (elt);
9649 unsigned int elt_bits = tree_to_uhwi (TYPE_SIZE (elt_type));
9650 tree size = bitsize_int (elt_bits);
9651 tree pos = bitsize_int (elt_bits * i); }
9654 (BIT_FIELD_REF:elt_type @0 { size; } { pos; })
9657 /* Fold reduction of a single nonzero element constructor. */
9658 (for reduc (IFN_REDUC_PLUS IFN_REDUC_IOR IFN_REDUC_XOR)
9659 (simplify (reduc (CONSTRUCTOR@0))
9660 (with { tree ctor = (TREE_CODE (@0) == SSA_NAME
9661 ? gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0)) : @0);
9662 tree elt = ctor_single_nonzero_element (ctor); }
9664 && !HONOR_SNANS (type)
9665 && !HONOR_SIGNED_ZEROS (type))
9668 /* Fold REDUC (@0 op VECTOR_CST) as REDUC (@0) op REDUC (VECTOR_CST). */
9669 (for reduc (IFN_REDUC_PLUS IFN_REDUC_MAX IFN_REDUC_MIN IFN_REDUC_FMAX
9670 IFN_REDUC_FMIN IFN_REDUC_AND IFN_REDUC_IOR IFN_REDUC_XOR)
9671 op (plus max min IFN_FMAX IFN_FMIN bit_and bit_ior bit_xor)
9672 (simplify (reduc (op @0 VECTOR_CST@1))
9673 (op (reduc:type @0) (reduc:type @1))))
9675 /* Simplify vector floating point operations of alternating sub/add pairs
9676 into using an fneg of a wider element type followed by a normal add.
9677 under IEEE 754 the fneg of the wider type will negate every even entry
9678 and when doing an add we get a sub of the even and add of every odd
9680 (for plusminus (plus minus)
9681 minusplus (minus plus)
9683 (vec_perm (plusminus @0 @1) (minusplus @2 @3) VECTOR_CST@4)
9684 (if (!VECTOR_INTEGER_TYPE_P (type)
9685 && !FLOAT_WORDS_BIG_ENDIAN
9686 /* plus is commutative, while minus is not, so :c can't be used.
9687 Do equality comparisons by hand and at the end pick the operands
9689 && (operand_equal_p (@0, @2, 0)
9690 ? operand_equal_p (@1, @3, 0)
9691 : operand_equal_p (@0, @3, 0) && operand_equal_p (@1, @2, 0)))
9694 /* Build a vector of integers from the tree mask. */
9695 vec_perm_builder builder;
9697 (if (tree_to_vec_perm_builder (&builder, @4))
9700 /* Create a vec_perm_indices for the integer vector. */
9701 poly_uint64 nelts = TYPE_VECTOR_SUBPARTS (type);
9702 vec_perm_indices sel (builder, 2, nelts);
9703 machine_mode vec_mode = TYPE_MODE (type);
9704 machine_mode wide_mode;
9705 scalar_mode wide_elt_mode;
9706 poly_uint64 wide_nunits;
9707 scalar_mode inner_mode = GET_MODE_INNER (vec_mode);
9709 (if (VECTOR_MODE_P (vec_mode)
9710 && sel.series_p (0, 2, 0, 2)
9711 && sel.series_p (1, 2, nelts + 1, 2)
9712 && GET_MODE_2XWIDER_MODE (inner_mode).exists (&wide_elt_mode)
9713 && multiple_p (GET_MODE_NUNITS (vec_mode), 2, &wide_nunits)
9714 && related_vector_mode (vec_mode, wide_elt_mode,
9715 wide_nunits).exists (&wide_mode))
9719 = lang_hooks.types.type_for_mode (GET_MODE_INNER (wide_mode),
9720 TYPE_UNSIGNED (type));
9721 tree ntype = build_vector_type_for_mode (stype, wide_mode);
9723 /* The format has to be a non-extended ieee format. */
9724 const struct real_format *fmt_old = FLOAT_MODE_FORMAT (vec_mode);
9725 const struct real_format *fmt_new = FLOAT_MODE_FORMAT (wide_mode);
9727 (if (TYPE_MODE (stype) != BLKmode
9728 && VECTOR_TYPE_P (ntype)
9733 /* If the target doesn't support v1xx vectors, try using
9734 scalar mode xx instead. */
9735 if (known_eq (GET_MODE_NUNITS (wide_mode), 1)
9736 && !target_supports_op_p (ntype, NEGATE_EXPR, optab_vector))
9739 (if (fmt_new->signbit_rw
9740 == fmt_old->signbit_rw + GET_MODE_UNIT_BITSIZE (vec_mode)
9741 && fmt_new->signbit_rw == fmt_new->signbit_ro
9742 && targetm.can_change_mode_class (TYPE_MODE (ntype),
9743 TYPE_MODE (type), ALL_REGS)
9744 && ((optimize_vectors_before_lowering_p ()
9745 && VECTOR_TYPE_P (ntype))
9746 || target_supports_op_p (ntype, NEGATE_EXPR, optab_vector)))
9747 (if (plusminus == PLUS_EXPR)
9748 (plus (view_convert:type (negate (view_convert:ntype @3))) @2)
9749 (minus @0 (view_convert:type
9750 (negate (view_convert:ntype @1))))))))))))))))
9753 (vec_perm @0 @1 VECTOR_CST@2)
9756 tree op0 = @0, op1 = @1, op2 = @2;
9757 machine_mode result_mode = TYPE_MODE (type);
9758 machine_mode op_mode = TYPE_MODE (TREE_TYPE (op0));
9760 /* Build a vector of integers from the tree mask. */
9761 vec_perm_builder builder;
9763 (if (tree_to_vec_perm_builder (&builder, op2))
9766 /* Create a vec_perm_indices for the integer vector. */
9767 poly_uint64 nelts = TYPE_VECTOR_SUBPARTS (type);
9768 bool single_arg = (op0 == op1);
9769 vec_perm_indices sel (builder, single_arg ? 1 : 2, nelts);
9771 (if (sel.series_p (0, 1, 0, 1))
9773 (if (sel.series_p (0, 1, nelts, 1))
9779 if (sel.all_from_input_p (0))
9781 else if (sel.all_from_input_p (1))
9784 sel.rotate_inputs (1);
9786 else if (known_ge (poly_uint64 (sel[0]), nelts))
9788 std::swap (op0, op1);
9789 sel.rotate_inputs (1);
9793 tree cop0 = op0, cop1 = op1;
9794 if (TREE_CODE (op0) == SSA_NAME
9795 && (def = dyn_cast <gassign *> (SSA_NAME_DEF_STMT (op0)))
9796 && gimple_assign_rhs_code (def) == CONSTRUCTOR)
9797 cop0 = gimple_assign_rhs1 (def);
9798 if (TREE_CODE (op1) == SSA_NAME
9799 && (def = dyn_cast <gassign *> (SSA_NAME_DEF_STMT (op1)))
9800 && gimple_assign_rhs_code (def) == CONSTRUCTOR)
9801 cop1 = gimple_assign_rhs1 (def);
9804 (if ((TREE_CODE (cop0) == VECTOR_CST
9805 || TREE_CODE (cop0) == CONSTRUCTOR)
9806 && (TREE_CODE (cop1) == VECTOR_CST
9807 || TREE_CODE (cop1) == CONSTRUCTOR)
9808 && (t = fold_vec_perm (type, cop0, cop1, sel)))
9812 bool changed = (op0 == op1 && !single_arg);
9813 tree ins = NULL_TREE;
9816 /* See if the permutation is performing a single element
9817 insert from a CONSTRUCTOR or constant and use a BIT_INSERT_EXPR
9818 in that case. But only if the vector mode is supported,
9819 otherwise this is invalid GIMPLE. */
9820 if (op_mode != BLKmode
9821 && (TREE_CODE (cop0) == VECTOR_CST
9822 || TREE_CODE (cop0) == CONSTRUCTOR
9823 || TREE_CODE (cop1) == VECTOR_CST
9824 || TREE_CODE (cop1) == CONSTRUCTOR))
9826 bool insert_first_p = sel.series_p (1, 1, nelts + 1, 1);
9829 /* After canonicalizing the first elt to come from the
9830 first vector we only can insert the first elt from
9831 the first vector. */
9833 if ((ins = fold_read_from_vector (cop0, sel[0])))
9836 /* The above can fail for two-element vectors which always
9837 appear to insert the first element, so try inserting
9838 into the second lane as well. For more than two
9839 elements that's wasted time. */
9840 if (!insert_first_p || (!ins && maybe_eq (nelts, 2u)))
9842 unsigned int encoded_nelts = sel.encoding ().encoded_nelts ();
9843 for (at = 0; at < encoded_nelts; ++at)
9844 if (maybe_ne (sel[at], at))
9846 if (at < encoded_nelts
9847 && (known_eq (at + 1, nelts)
9848 || sel.series_p (at + 1, 1, at + 1, 1)))
9850 if (known_lt (poly_uint64 (sel[at]), nelts))
9851 ins = fold_read_from_vector (cop0, sel[at]);
9853 ins = fold_read_from_vector (cop1, sel[at] - nelts);
9858 /* Generate a canonical form of the selector. */
9859 if (!ins && sel.encoding () != builder)
9861 /* Some targets are deficient and fail to expand a single
9862 argument permutation while still allowing an equivalent
9863 2-argument version. */
9865 if (sel.ninputs () == 2
9866 || can_vec_perm_const_p (result_mode, op_mode, sel, false))
9867 op2 = vec_perm_indices_to_tree (TREE_TYPE (op2), sel);
9870 vec_perm_indices sel2 (builder, 2, nelts);
9871 if (can_vec_perm_const_p (result_mode, op_mode, sel2, false))
9872 op2 = vec_perm_indices_to_tree (TREE_TYPE (op2), sel2);
9874 /* Not directly supported with either encoding,
9875 so use the preferred form. */
9876 op2 = vec_perm_indices_to_tree (TREE_TYPE (op2), sel);
9878 if (!operand_equal_p (op2, oldop2, 0))
9883 (bit_insert { op0; } { ins; }
9884 { bitsize_int (at * vector_element_bits (type)); })
9886 (vec_perm { op0; } { op1; } { op2; }))))))))))))
9888 /* VEC_PERM_EXPR (v, v, mask) -> v where v contains same element. */
9890 (match vec_same_elem_p
9893 (match vec_same_elem_p
9895 (if (TREE_CODE (@0) == SSA_NAME
9896 && uniform_vector_p (gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0))))))
9898 (match vec_same_elem_p
9900 (if (uniform_vector_p (@0))))
9904 (vec_perm vec_same_elem_p@0 @0 @1)
9905 (if (types_match (type, TREE_TYPE (@0)))
9909 tree elem = uniform_vector_p (@0);
9912 { build_vector_from_val (type, elem); }))))
9914 /* Push VEC_PERM earlier if that may help FMA perception (PR101895). */
9916 (plus:c (vec_perm:s (mult:c@0 @1 vec_same_elem_p@2) @0 @3) @4)
9917 (if (TREE_CODE (@0) == SSA_NAME && num_imm_uses (@0) == 2)
9918 (plus (mult (vec_perm @1 @1 @3) @2) @4)))
9920 (minus (vec_perm:s (mult:c@0 @1 vec_same_elem_p@2) @0 @3) @4)
9921 (if (TREE_CODE (@0) == SSA_NAME && num_imm_uses (@0) == 2)
9922 (minus (mult (vec_perm @1 @1 @3) @2) @4)))
9926 c = VEC_PERM_EXPR <a, b, VCST0>;
9927 d = VEC_PERM_EXPR <c, c, VCST1>;
9929 d = VEC_PERM_EXPR <a, b, NEW_VCST>; */
9932 (vec_perm (vec_perm@0 @1 @2 VECTOR_CST@3) @0 VECTOR_CST@4)
9933 (if (TYPE_VECTOR_SUBPARTS (type).is_constant ())
9936 machine_mode result_mode = TYPE_MODE (type);
9937 machine_mode op_mode = TYPE_MODE (TREE_TYPE (@1));
9938 int nelts = TYPE_VECTOR_SUBPARTS (type).to_constant ();
9939 vec_perm_builder builder0;
9940 vec_perm_builder builder1;
9941 vec_perm_builder builder2 (nelts, nelts, 1);
9943 (if (tree_to_vec_perm_builder (&builder0, @3)
9944 && tree_to_vec_perm_builder (&builder1, @4))
9947 vec_perm_indices sel0 (builder0, 2, nelts);
9948 vec_perm_indices sel1 (builder1, 1, nelts);
9950 for (int i = 0; i < nelts; i++)
9951 builder2.quick_push (sel0[sel1[i].to_constant ()]);
9953 vec_perm_indices sel2 (builder2, 2, nelts);
9955 tree op0 = NULL_TREE;
9956 /* If the new VEC_PERM_EXPR can't be handled but both
9957 original VEC_PERM_EXPRs can, punt.
9958 If one or both of the original VEC_PERM_EXPRs can't be
9959 handled and the new one can't be either, don't increase
9960 number of VEC_PERM_EXPRs that can't be handled. */
9961 if (can_vec_perm_const_p (result_mode, op_mode, sel2, false)
9963 ? (!can_vec_perm_const_p (result_mode, op_mode, sel0, false)
9964 || !can_vec_perm_const_p (result_mode, op_mode, sel1, false))
9965 : !can_vec_perm_const_p (result_mode, op_mode, sel1, false)))
9966 op0 = vec_perm_indices_to_tree (TREE_TYPE (@4), sel2);
9969 (vec_perm @1 @2 { op0; })))))))
9972 c = VEC_PERM_EXPR <a, b, VCST0>;
9973 d = VEC_PERM_EXPR <x, c, VCST1>;
9975 d = VEC_PERM_EXPR <x, {a,b}, NEW_VCST>;
9976 when all elements from a or b are replaced by the later
9980 (vec_perm @5 (vec_perm@0 @1 @2 VECTOR_CST@3) VECTOR_CST@4)
9981 (if (TYPE_VECTOR_SUBPARTS (type).is_constant ())
9984 machine_mode result_mode = TYPE_MODE (type);
9985 machine_mode op_mode = TYPE_MODE (TREE_TYPE (@1));
9986 int nelts = TYPE_VECTOR_SUBPARTS (type).to_constant ();
9987 vec_perm_builder builder0;
9988 vec_perm_builder builder1;
9989 vec_perm_builder builder2 (nelts, nelts, 2);
9991 (if (tree_to_vec_perm_builder (&builder0, @3)
9992 && tree_to_vec_perm_builder (&builder1, @4))
9995 vec_perm_indices sel0 (builder0, 2, nelts);
9996 vec_perm_indices sel1 (builder1, 2, nelts);
9997 bool use_1 = false, use_2 = false;
9999 for (int i = 0; i < nelts; i++)
10001 if (known_lt ((poly_uint64)sel1[i], sel1.nelts_per_input ()))
10002 builder2.quick_push (sel1[i]);
10005 poly_uint64 j = sel0[(sel1[i] - sel1.nelts_per_input ())
10007 if (known_lt (j, sel0.nelts_per_input ()))
10012 j -= sel0.nelts_per_input ();
10014 builder2.quick_push (j + sel1.nelts_per_input ());
10018 (if (use_1 ^ use_2)
10021 vec_perm_indices sel2 (builder2, 2, nelts);
10022 tree op0 = NULL_TREE;
10023 /* If the new VEC_PERM_EXPR can't be handled but both
10024 original VEC_PERM_EXPRs can, punt.
10025 If one or both of the original VEC_PERM_EXPRs can't be
10026 handled and the new one can't be either, don't increase
10027 number of VEC_PERM_EXPRs that can't be handled. */
10028 if (can_vec_perm_const_p (result_mode, op_mode, sel2, false)
10029 || (single_use (@0)
10030 ? (!can_vec_perm_const_p (result_mode, op_mode, sel0, false)
10031 || !can_vec_perm_const_p (result_mode, op_mode, sel1, false))
10032 : !can_vec_perm_const_p (result_mode, op_mode, sel1, false)))
10033 op0 = vec_perm_indices_to_tree (TREE_TYPE (@4), sel2);
10038 (vec_perm @5 @1 { op0; }))
10040 (vec_perm @5 @2 { op0; })))))))))))
10042 /* And the case with swapped outer permute sources. */
10045 (vec_perm (vec_perm@0 @1 @2 VECTOR_CST@3) @5 VECTOR_CST@4)
10046 (if (TYPE_VECTOR_SUBPARTS (type).is_constant ())
10049 machine_mode result_mode = TYPE_MODE (type);
10050 machine_mode op_mode = TYPE_MODE (TREE_TYPE (@1));
10051 int nelts = TYPE_VECTOR_SUBPARTS (type).to_constant ();
10052 vec_perm_builder builder0;
10053 vec_perm_builder builder1;
10054 vec_perm_builder builder2 (nelts, nelts, 2);
10056 (if (tree_to_vec_perm_builder (&builder0, @3)
10057 && tree_to_vec_perm_builder (&builder1, @4))
10060 vec_perm_indices sel0 (builder0, 2, nelts);
10061 vec_perm_indices sel1 (builder1, 2, nelts);
10062 bool use_1 = false, use_2 = false;
10064 for (int i = 0; i < nelts; i++)
10066 if (known_ge ((poly_uint64)sel1[i], sel1.nelts_per_input ()))
10067 builder2.quick_push (sel1[i]);
10070 poly_uint64 j = sel0[sel1[i].to_constant ()];
10071 if (known_lt (j, sel0.nelts_per_input ()))
10076 j -= sel0.nelts_per_input ();
10078 builder2.quick_push (j);
10082 (if (use_1 ^ use_2)
10085 vec_perm_indices sel2 (builder2, 2, nelts);
10086 tree op0 = NULL_TREE;
10087 /* If the new VEC_PERM_EXPR can't be handled but both
10088 original VEC_PERM_EXPRs can, punt.
10089 If one or both of the original VEC_PERM_EXPRs can't be
10090 handled and the new one can't be either, don't increase
10091 number of VEC_PERM_EXPRs that can't be handled. */
10092 if (can_vec_perm_const_p (result_mode, op_mode, sel2, false)
10093 || (single_use (@0)
10094 ? (!can_vec_perm_const_p (result_mode, op_mode, sel0, false)
10095 || !can_vec_perm_const_p (result_mode, op_mode, sel1, false))
10096 : !can_vec_perm_const_p (result_mode, op_mode, sel1, false)))
10097 op0 = vec_perm_indices_to_tree (TREE_TYPE (@4), sel2);
10102 (vec_perm @1 @5 { op0; }))
10104 (vec_perm @2 @5 { op0; })))))))))))
10107 /* Match count trailing zeroes for simplify_count_trailing_zeroes in fwprop.
10108 The canonical form is array[((x & -x) * C) >> SHIFT] where C is a magic
10109 constant which when multiplied by a power of 2 contains a unique value
10110 in the top 5 or 6 bits. This is then indexed into a table which maps it
10111 to the number of trailing zeroes. */
10112 (match (ctz_table_index @1 @2 @3)
10113 (rshift (mult (bit_and:c (negate @1) @1) INTEGER_CST@2) INTEGER_CST@3))
10115 (match (cond_expr_convert_p @0 @2 @3 @6)
10116 (cond (simple_comparison@6 @0 @1) (convert@4 @2) (convert@5 @3))
10117 (if (INTEGRAL_TYPE_P (type)
10118 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
10119 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
10120 && INTEGRAL_TYPE_P (TREE_TYPE (@3))
10121 && TYPE_PRECISION (type) != TYPE_PRECISION (TREE_TYPE (@0))
10122 && TYPE_PRECISION (TREE_TYPE (@0))
10123 == TYPE_PRECISION (TREE_TYPE (@2))
10124 && TYPE_PRECISION (TREE_TYPE (@0))
10125 == TYPE_PRECISION (TREE_TYPE (@3))
10126 /* For vect_recog_cond_expr_convert_pattern, @2 and @3 can differ in
10127 signess when convert is truncation, but not ok for extension since
10128 it's sign_extend vs zero_extend. */
10129 && (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type)
10130 || (TYPE_UNSIGNED (TREE_TYPE (@2))
10131 == TYPE_UNSIGNED (TREE_TYPE (@3))))
10133 && single_use (@5))))
10135 (for bit_op (bit_and bit_ior bit_xor)
10136 (match (bitwise_induction_p @0 @2 @3)
10138 (nop_convert1? (bit_not2?@0 (convert3? (lshift integer_onep@1 @2))))
10141 (match (bitwise_induction_p @0 @2 @3)
10143 (nop_convert1? (bit_xor@0 (convert2? (lshift integer_onep@1 @2)) @3))))
10145 /* n - (((n > C1) ? n : C1) & -C2) -> n & C1 for unsigned case.
10146 n - (((n > C1) ? n : C1) & -C2) -> (n <= C1) ? n : (n & C1) for signed case. */
10148 (minus @0 (bit_and (max @0 INTEGER_CST@1) INTEGER_CST@2))
10149 (with { auto i = wi::neg (wi::to_wide (@2)); }
10150 /* Check if -C2 is a power of 2 and C1 = -C2 - 1. */
10151 (if (wi::popcount (i) == 1
10152 && (wi::to_wide (@1)) == (i - 1))
10153 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
10155 (cond (le @0 @1) @0 (bit_and @0 @1))))))
10157 /* -x & 1 -> x & 1. */
10159 (bit_and (negate @0) integer_onep@1)
10160 (if (!TYPE_OVERFLOW_SANITIZED (type))
10163 /* `-a` is just `a` if the type is 1bit wide or when converting
10164 to a 1bit type; similar to the above transformation of `(-x)&1`.
10165 This is used mostly with the transformation of
10166 `a ? ~b : b` into `(-a)^b`.
10167 It also can show up with bitfields. */
10169 (convert? (negate @0))
10170 (if (INTEGRAL_TYPE_P (type)
10171 && TYPE_PRECISION (type) == 1
10172 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0)))
10176 c1 = VEC_PERM_EXPR (a, a, mask)
10177 c2 = VEC_PERM_EXPR (b, b, mask)
10181 c3 = VEC_PERM_EXPR (c, c, mask)
10182 For all integer non-div operations. */
10183 (for op (plus minus mult bit_and bit_ior bit_xor
10186 (op (vec_perm @0 @0 @2) (vec_perm @1 @1 @2))
10187 (if (VECTOR_INTEGER_TYPE_P (type))
10188 (vec_perm (op@3 @0 @1) @3 @2))))
10190 /* Similar for float arithmetic when permutation constant covers
10191 all vector elements. */
10192 (for op (plus minus mult)
10194 (op (vec_perm @0 @0 VECTOR_CST@2) (vec_perm @1 @1 VECTOR_CST@2))
10195 (if (VECTOR_FLOAT_TYPE_P (type)
10196 && TYPE_VECTOR_SUBPARTS (type).is_constant ())
10199 tree perm_cst = @2;
10200 vec_perm_builder builder;
10201 bool full_perm_p = false;
10202 if (tree_to_vec_perm_builder (&builder, perm_cst))
10204 unsigned HOST_WIDE_INT nelts;
10206 nelts = TYPE_VECTOR_SUBPARTS (type).to_constant ();
10207 /* Create a vec_perm_indices for the VECTOR_CST. */
10208 vec_perm_indices sel (builder, 1, nelts);
10210 /* Check if perm indices covers all vector elements. */
10211 if (sel.encoding ().encoded_full_vector_p ())
10213 auto_sbitmap seen (nelts);
10214 bitmap_clear (seen);
10216 unsigned HOST_WIDE_INT count = 0, i;
10218 for (i = 0; i < nelts; i++)
10220 if (!bitmap_set_bit (seen, sel[i].to_constant ()))
10224 full_perm_p = count == nelts;
10229 (vec_perm (op@3 @0 @1) @3 @2))))))