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-2023 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)))))
187 /* Transform likes of (char) ABS_EXPR <(int) x> into (char) ABSU_EXPR <x>
188 ABSU_EXPR returns unsigned absolute value of the operand and the operand
189 of the ABSU_EXPR will have the corresponding signed type. */
190 (simplify (abs (convert @0))
191 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
192 && !TYPE_UNSIGNED (TREE_TYPE (@0))
193 && element_precision (type) > element_precision (TREE_TYPE (@0)))
194 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
195 (convert (absu:utype @0)))))
198 /* Optimize (X + (X >> (prec - 1))) ^ (X >> (prec - 1)) into abs (X). */
200 (bit_xor:c (plus:c @0 (rshift@2 @0 INTEGER_CST@1)) @2)
201 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
202 && !TYPE_UNSIGNED (TREE_TYPE (@0))
203 && wi::to_widest (@1) == element_precision (TREE_TYPE (@0)) - 1)
207 /* Simplifications of operations with one constant operand and
208 simplifications to constants or single values. */
210 (for op (plus pointer_plus minus bit_ior bit_xor)
212 (op @0 integer_zerop)
215 /* 0 +p index -> (type)index */
217 (pointer_plus integer_zerop @1)
218 (non_lvalue (convert @1)))
220 /* ptr - 0 -> (type)ptr */
222 (pointer_diff @0 integer_zerop)
225 /* See if ARG1 is zero and X + ARG1 reduces to X.
226 Likewise if the operands are reversed. */
228 (plus:c @0 real_zerop@1)
229 (if (fold_real_zero_addition_p (type, @0, @1, 0))
232 /* See if ARG1 is zero and X - ARG1 reduces to X. */
234 (minus @0 real_zerop@1)
235 (if (fold_real_zero_addition_p (type, @0, @1, 1))
238 /* Even if the fold_real_zero_addition_p can't simplify X + 0.0
239 into X, we can optimize (X + 0.0) + 0.0 or (X + 0.0) - 0.0
240 or (X - 0.0) + 0.0 into X + 0.0 and (X - 0.0) - 0.0 into X - 0.0
241 if not -frounding-math. For sNaNs the first operation would raise
242 exceptions but turn the result into qNan, so the second operation
243 would not raise it. */
244 (for inner_op (plus minus)
245 (for outer_op (plus minus)
247 (outer_op (inner_op@3 @0 REAL_CST@1) REAL_CST@2)
250 && !HONOR_SIGN_DEPENDENT_ROUNDING (type))
251 (with { bool inner_plus = ((inner_op == PLUS_EXPR)
252 ^ REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@1)));
254 = ((outer_op == PLUS_EXPR)
255 ^ REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@2))); }
256 (if (outer_plus && !inner_plus)
261 This is unsafe for certain floats even in non-IEEE formats.
262 In IEEE, it is unsafe because it does wrong for NaNs.
263 PR middle-end/98420: x - x may be -0.0 with FE_DOWNWARD.
264 Also note that operand_equal_p is always false if an operand
268 (if (!FLOAT_TYPE_P (type)
269 || (!tree_expr_maybe_nan_p (@0)
270 && !tree_expr_maybe_infinite_p (@0)
271 && (!HONOR_SIGN_DEPENDENT_ROUNDING (type)
272 || !HONOR_SIGNED_ZEROS (type))))
273 { build_zero_cst (type); }))
275 (pointer_diff @@0 @0)
276 { build_zero_cst (type); })
279 (mult @0 integer_zerop@1)
282 /* -x == x -> x == 0 */
285 (cmp:c @0 (negate @0))
286 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
287 && !TYPE_OVERFLOW_WRAPS (TREE_TYPE(@0)))
288 (cmp @0 { build_zero_cst (TREE_TYPE(@0)); }))))
290 /* Maybe fold x * 0 to 0. The expressions aren't the same
291 when x is NaN, since x * 0 is also NaN. Nor are they the
292 same in modes with signed zeros, since multiplying a
293 negative value by 0 gives -0, not +0. Nor when x is +-Inf,
294 since x * 0 is NaN. */
296 (mult @0 real_zerop@1)
297 (if (!tree_expr_maybe_nan_p (@0)
298 && (!HONOR_NANS (type) || !tree_expr_maybe_infinite_p (@0))
299 && (!HONOR_SIGNED_ZEROS (type) || tree_expr_nonnegative_p (@0)))
302 /* In IEEE floating point, x*1 is not equivalent to x for snans.
303 Likewise for complex arithmetic with signed zeros. */
306 (if (!tree_expr_maybe_signaling_nan_p (@0)
307 && (!HONOR_SIGNED_ZEROS (type)
308 || !COMPLEX_FLOAT_TYPE_P (type)))
311 /* Transform x * -1.0 into -x. */
313 (mult @0 real_minus_onep)
314 (if (!tree_expr_maybe_signaling_nan_p (@0)
315 && (!HONOR_SIGNED_ZEROS (type)
316 || !COMPLEX_FLOAT_TYPE_P (type)))
319 /* Transform x * { 0 or 1, 0 or 1, ... } into x & { 0 or -1, 0 or -1, ...},
320 unless the target has native support for the former but not the latter. */
322 (mult @0 VECTOR_CST@1)
323 (if (initializer_each_zero_or_onep (@1)
324 && !HONOR_SNANS (type)
325 && !HONOR_SIGNED_ZEROS (type))
326 (with { tree itype = FLOAT_TYPE_P (type) ? unsigned_type_for (type) : type; }
328 && (!VECTOR_MODE_P (TYPE_MODE (type))
329 || (VECTOR_MODE_P (TYPE_MODE (itype))
330 && optab_handler (and_optab,
331 TYPE_MODE (itype)) != CODE_FOR_nothing)))
332 (view_convert (bit_and:itype (view_convert @0)
333 (ne @1 { build_zero_cst (type); })))))))
335 /* In SWAR (SIMD within a register) code a signed comparison of packed data
336 can be constructed with a particular combination of shift, bitwise and,
337 and multiplication by constants. If that code is vectorized we can
338 convert this pattern into a more efficient vector comparison. */
340 (mult (bit_and (rshift @0 uniform_integer_cst_p@1)
341 uniform_integer_cst_p@2)
342 uniform_integer_cst_p@3)
344 tree rshift_cst = uniform_integer_cst_p (@1);
345 tree bit_and_cst = uniform_integer_cst_p (@2);
346 tree mult_cst = uniform_integer_cst_p (@3);
348 /* Make sure we're working with vectors and uniform vector constants. */
349 (if (VECTOR_TYPE_P (type)
350 && tree_fits_uhwi_p (rshift_cst)
351 && tree_fits_uhwi_p (mult_cst)
352 && tree_fits_uhwi_p (bit_and_cst))
353 /* Compute what constants would be needed for this to represent a packed
354 comparison based on the shift amount denoted by RSHIFT_CST. */
356 HOST_WIDE_INT vec_elem_bits = vector_element_bits (type);
357 poly_int64 vec_nelts = TYPE_VECTOR_SUBPARTS (type);
358 poly_int64 vec_bits = vec_elem_bits * vec_nelts;
359 unsigned HOST_WIDE_INT cmp_bits_i, bit_and_i, mult_i;
360 unsigned HOST_WIDE_INT target_mult_i, target_bit_and_i;
361 cmp_bits_i = tree_to_uhwi (rshift_cst) + 1;
362 mult_i = tree_to_uhwi (mult_cst);
363 target_mult_i = (HOST_WIDE_INT_1U << cmp_bits_i) - 1;
364 bit_and_i = tree_to_uhwi (bit_and_cst);
365 target_bit_and_i = 0;
367 /* The bit pattern in BIT_AND_I should be a mask for the least
368 significant bit of each packed element that is CMP_BITS wide. */
369 for (unsigned i = 0; i < vec_elem_bits / cmp_bits_i; i++)
370 target_bit_and_i = (target_bit_and_i << cmp_bits_i) | 1U;
372 (if ((exact_log2 (cmp_bits_i)) >= 0
373 && cmp_bits_i < HOST_BITS_PER_WIDE_INT
374 && multiple_p (vec_bits, cmp_bits_i)
375 && vec_elem_bits <= HOST_BITS_PER_WIDE_INT
376 && target_mult_i == mult_i
377 && target_bit_and_i == bit_and_i)
378 /* Compute the vector shape for the comparison and check if the target is
379 able to expand the comparison with that type. */
381 /* We're doing a signed comparison. */
382 tree cmp_type = build_nonstandard_integer_type (cmp_bits_i, 0);
383 poly_int64 vector_type_nelts = exact_div (vec_bits, cmp_bits_i);
384 tree vec_cmp_type = build_vector_type (cmp_type, vector_type_nelts);
385 tree vec_truth_type = truth_type_for (vec_cmp_type);
386 tree zeros = build_zero_cst (vec_cmp_type);
387 tree ones = build_all_ones_cst (vec_cmp_type);
389 (if (expand_vec_cmp_expr_p (vec_cmp_type, vec_truth_type, LT_EXPR)
390 && expand_vec_cond_expr_p (vec_cmp_type, vec_truth_type, LT_EXPR))
391 (view_convert:type (vec_cond (lt:vec_truth_type
392 (view_convert:vec_cmp_type @0)
394 { ones; } { zeros; })))))))))
396 (for cmp (gt ge lt le)
397 outp (convert convert negate negate)
398 outn (negate negate convert convert)
399 /* Transform X * (X > 0.0 ? 1.0 : -1.0) into abs(X). */
400 /* Transform X * (X >= 0.0 ? 1.0 : -1.0) into abs(X). */
401 /* Transform X * (X < 0.0 ? 1.0 : -1.0) into -abs(X). */
402 /* Transform X * (X <= 0.0 ? 1.0 : -1.0) into -abs(X). */
404 (mult:c @0 (cond (cmp @0 real_zerop) real_onep@1 real_minus_onep))
405 (if (!tree_expr_maybe_nan_p (@0) && !HONOR_SIGNED_ZEROS (type))
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). */
409 /* Transform X * (X < 0.0 ? -1.0 : 1.0) into abs(X). */
410 /* Transform X * (X <= 0.0 ? -1.0 : 1.0) into abs(X). */
412 (mult:c @0 (cond (cmp @0 real_zerop) real_minus_onep real_onep@1))
413 (if (!tree_expr_maybe_nan_p (@0) && !HONOR_SIGNED_ZEROS (type))
416 /* Transform X * copysign (1.0, X) into abs(X). */
418 (mult:c @0 (COPYSIGN_ALL real_onep @0))
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 (negate @0)))
425 (if (!tree_expr_maybe_nan_p (@0) && !HONOR_SIGNED_ZEROS (type))
428 /* Transform copysign (CST, X) into copysign (ABS(CST), X). */
430 (COPYSIGN_ALL REAL_CST@0 @1)
431 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@0)))
432 (COPYSIGN_ALL (negate @0) @1)))
434 /* Transform c ? x * copysign (1, y) : z to c ? x ^ signs(y) : z.
435 tree-ssa-math-opts.cc does the corresponding optimization for
436 unconditional multiplications (via xorsign). */
438 (IFN_COND_MUL:c @0 @1 (IFN_COPYSIGN real_onep @2) @3)
439 (with { tree signs = sign_mask_for (type); }
441 (with { tree inttype = TREE_TYPE (signs); }
443 (IFN_COND_XOR:inttype @0
444 (view_convert:inttype @1)
445 (bit_and (view_convert:inttype @2) { signs; })
446 (view_convert:inttype @3)))))))
448 /* (x >= 0 ? x : 0) + (x <= 0 ? -x : 0) -> abs x. */
450 (plus:c (max @0 integer_zerop) (max (negate @0) integer_zerop))
453 /* X * 1, X / 1 -> X. */
454 (for op (mult trunc_div ceil_div floor_div round_div exact_div)
459 /* (A / (1 << B)) -> (A >> B).
460 Only for unsigned A. For signed A, this would not preserve rounding
462 For example: (-1 / ( 1 << B)) != -1 >> B.
463 Also handle widening conversions, like:
464 (A / (unsigned long long) (1U << B)) -> (A >> B)
466 (A / (unsigned long long) (1 << B)) -> (A >> B).
467 If the left shift is signed, it can be done only if the upper bits
468 of A starting from shift's type sign bit are zero, as
469 (unsigned long long) (1 << 31) is -2147483648ULL, not 2147483648ULL,
470 so it is valid only if A >> 31 is zero. */
472 (trunc_div (convert?@0 @3) (convert2? (lshift integer_onep@1 @2)))
473 (if ((TYPE_UNSIGNED (type) || tree_expr_nonnegative_p (@0))
474 && (!VECTOR_TYPE_P (type)
475 || target_supports_op_p (type, RSHIFT_EXPR, optab_vector)
476 || target_supports_op_p (type, RSHIFT_EXPR, optab_scalar))
477 && (useless_type_conversion_p (type, TREE_TYPE (@1))
478 || (element_precision (type) >= element_precision (TREE_TYPE (@1))
479 && (TYPE_UNSIGNED (TREE_TYPE (@1))
480 || (element_precision (type)
481 == element_precision (TREE_TYPE (@1)))
482 || (INTEGRAL_TYPE_P (type)
483 && (tree_nonzero_bits (@0)
484 & wi::mask (element_precision (TREE_TYPE (@1)) - 1,
486 element_precision (type))) == 0)))))
487 (if (!VECTOR_TYPE_P (type)
488 && useless_type_conversion_p (TREE_TYPE (@3), TREE_TYPE (@1))
489 && element_precision (TREE_TYPE (@3)) < element_precision (type))
490 (convert (rshift @3 @2))
493 /* Preserve explicit divisions by 0: the C++ front-end wants to detect
494 undefined behavior in constexpr evaluation, and assuming that the division
495 traps enables better optimizations than these anyway. */
496 (for div (trunc_div ceil_div floor_div round_div exact_div)
497 /* 0 / X is always zero. */
499 (div integer_zerop@0 @1)
500 /* But not for 0 / 0 so that we can get the proper warnings and errors. */
501 (if (!integer_zerop (@1))
505 (div @0 integer_minus_onep@1)
506 (if (!TYPE_UNSIGNED (type))
508 /* X / bool_range_Y is X. */
511 (if (INTEGRAL_TYPE_P (type)
512 && ssa_name_has_boolean_range (@1)
513 && !flag_non_call_exceptions)
518 /* But not for 0 / 0 so that we can get the proper warnings and errors.
519 And not for _Fract types where we can't build 1. */
520 (if (!ALL_FRACT_MODE_P (TYPE_MODE (type))
521 && !integer_zerop (@0)
522 && (!flag_non_call_exceptions || tree_expr_nonzero_p (@0)))
523 { build_one_cst (type); }))
524 /* X / abs (X) is X < 0 ? -1 : 1. */
527 (if (INTEGRAL_TYPE_P (type)
528 && TYPE_OVERFLOW_UNDEFINED (type)
529 && !integer_zerop (@0)
530 && (!flag_non_call_exceptions || tree_expr_nonzero_p (@0)))
531 (cond (lt @0 { build_zero_cst (type); })
532 { build_minus_one_cst (type); } { build_one_cst (type); })))
535 (div:C @0 (negate @0))
536 (if ((INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
537 && TYPE_OVERFLOW_UNDEFINED (type)
538 && !integer_zerop (@0)
539 && (!flag_non_call_exceptions || tree_expr_nonzero_p (@0)))
540 { build_minus_one_cst (type); })))
542 /* For unsigned integral types, FLOOR_DIV_EXPR is the same as
543 TRUNC_DIV_EXPR. Rewrite into the latter in this case. Similarly
544 for MOD instead of DIV. */
545 (for floor_divmod (floor_div floor_mod)
546 trunc_divmod (trunc_div trunc_mod)
549 (if ((INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
550 && TYPE_UNSIGNED (type))
551 (trunc_divmod @0 @1))))
553 /* 1 / X -> X == 1 for unsigned integer X.
554 1 / X -> X >= -1 && X <= 1 ? X : 0 for signed integer X.
555 But not for 1 / 0 so that we can get proper warnings and errors,
556 and not for 1-bit integers as they are edge cases better handled
559 (trunc_div integer_onep@0 @1)
560 (if (INTEGRAL_TYPE_P (type)
561 && TYPE_PRECISION (type) > 1
562 && !integer_zerop (@1)
563 && (!flag_non_call_exceptions || tree_expr_nonzero_p (@1)))
564 (if (TYPE_UNSIGNED (type))
565 (convert (eq:boolean_type_node @1 { build_one_cst (type); }))
566 (with { tree utype = unsigned_type_for (type); }
567 (cond (le (plus (convert:utype @1) { build_one_cst (utype); })
568 { build_int_cst (utype, 2); })
569 @1 { build_zero_cst (type); })))))
571 /* Combine two successive divisions. Note that combining ceil_div
572 and floor_div is trickier and combining round_div even more so. */
573 (for div (trunc_div exact_div)
575 (div (div@3 @0 INTEGER_CST@1) INTEGER_CST@2)
577 wi::overflow_type overflow;
578 wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@2),
579 TYPE_SIGN (type), &overflow);
581 (if (div == EXACT_DIV_EXPR
582 || optimize_successive_divisions_p (@2, @3))
584 (div @0 { wide_int_to_tree (type, mul); })
585 (if (TYPE_UNSIGNED (type)
586 || mul != wi::min_value (TYPE_PRECISION (type), SIGNED))
587 { build_zero_cst (type); }))))))
589 /* Combine successive multiplications. Similar to above, but handling
590 overflow is different. */
592 (mult (mult @0 INTEGER_CST@1) INTEGER_CST@2)
594 wi::overflow_type overflow;
595 wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@2),
596 TYPE_SIGN (type), &overflow);
598 /* Skip folding on overflow: the only special case is @1 * @2 == -INT_MIN,
599 otherwise undefined overflow implies that @0 must be zero. */
600 (if (!overflow || TYPE_OVERFLOW_WRAPS (type))
601 (mult @0 { wide_int_to_tree (type, mul); }))))
603 /* Similar to above, but there could be an extra add/sub between
604 successive multuiplications. */
606 (mult (plus:s (mult:s@4 @0 INTEGER_CST@1) INTEGER_CST@2) INTEGER_CST@3)
608 bool overflowed = true;
609 wi::overflow_type ovf1, ovf2;
610 wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@3),
611 TYPE_SIGN (type), &ovf1);
612 wide_int add = wi::mul (wi::to_wide (@2), wi::to_wide (@3),
613 TYPE_SIGN (type), &ovf2);
614 if (TYPE_OVERFLOW_UNDEFINED (type))
618 if (ovf1 == wi::OVF_NONE && ovf2 == wi::OVF_NONE
619 && get_global_range_query ()->range_of_expr (vr0, @4)
620 && !vr0.varying_p () && !vr0.undefined_p ())
622 wide_int wmin0 = vr0.lower_bound ();
623 wide_int wmax0 = vr0.upper_bound ();
624 wmin0 = wi::mul (wmin0, wi::to_wide (@3), TYPE_SIGN (type), &ovf1);
625 wmax0 = wi::mul (wmax0, wi::to_wide (@3), TYPE_SIGN (type), &ovf2);
626 if (ovf1 == wi::OVF_NONE && ovf2 == wi::OVF_NONE)
628 wi::add (wmin0, add, TYPE_SIGN (type), &ovf1);
629 wi::add (wmax0, add, TYPE_SIGN (type), &ovf2);
630 if (ovf1 == wi::OVF_NONE && ovf2 == wi::OVF_NONE)
639 /* Skip folding on overflow. */
641 (plus (mult @0 { wide_int_to_tree (type, mul); })
642 { wide_int_to_tree (type, add); }))))
644 /* Similar to above, but a multiplication between successive additions. */
646 (plus (mult:s (plus:s @0 INTEGER_CST@1) INTEGER_CST@2) INTEGER_CST@3)
648 bool overflowed = true;
649 wi::overflow_type ovf1;
650 wi::overflow_type ovf2;
651 wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@2),
652 TYPE_SIGN (type), &ovf1);
653 wide_int add = wi::add (mul, wi::to_wide (@3),
654 TYPE_SIGN (type), &ovf2);
655 if (TYPE_OVERFLOW_UNDEFINED (type))
659 if (ovf1 == wi::OVF_NONE && ovf2 == wi::OVF_NONE
660 && get_global_range_query ()->range_of_expr (vr0, @0)
661 && !vr0.varying_p () && !vr0.undefined_p ())
663 wide_int wmin0 = vr0.lower_bound ();
664 wide_int wmax0 = vr0.upper_bound ();
665 wmin0 = wi::mul (wmin0, wi::to_wide (@2), TYPE_SIGN (type), &ovf1);
666 wmax0 = wi::mul (wmax0, wi::to_wide (@2), TYPE_SIGN (type), &ovf2);
667 if (ovf1 == wi::OVF_NONE && ovf2 == wi::OVF_NONE)
669 wi::add (wmin0, mul, TYPE_SIGN (type), &ovf1);
670 wi::add (wmax0, mul, TYPE_SIGN (type), &ovf2);
671 if (ovf1 == wi::OVF_NONE && ovf2 == wi::OVF_NONE)
680 /* Skip folding on overflow. */
682 (plus (mult @0 @2) { wide_int_to_tree (type, add); }))))
684 /* Optimize A / A to 1.0 if we don't care about
685 NaNs or Infinities. */
688 (if (FLOAT_TYPE_P (type)
689 && ! HONOR_NANS (type)
690 && ! HONOR_INFINITIES (type))
691 { build_one_cst (type); }))
693 /* Optimize -A / A to -1.0 if we don't care about
694 NaNs or Infinities. */
696 (rdiv:C @0 (negate @0))
697 (if (FLOAT_TYPE_P (type)
698 && ! HONOR_NANS (type)
699 && ! HONOR_INFINITIES (type))
700 { build_minus_one_cst (type); }))
702 /* PR71078: x / abs(x) -> copysign (1.0, x) */
704 (rdiv:C (convert? @0) (convert? (abs @0)))
705 (if (SCALAR_FLOAT_TYPE_P (type)
706 && ! HONOR_NANS (type)
707 && ! HONOR_INFINITIES (type))
709 (if (types_match (type, float_type_node))
710 (BUILT_IN_COPYSIGNF { build_one_cst (type); } (convert @0)))
711 (if (types_match (type, double_type_node))
712 (BUILT_IN_COPYSIGN { build_one_cst (type); } (convert @0)))
713 (if (types_match (type, long_double_type_node))
714 (BUILT_IN_COPYSIGNL { build_one_cst (type); } (convert @0))))))
716 /* In IEEE floating point, x/1 is not equivalent to x for snans. */
719 (if (!tree_expr_maybe_signaling_nan_p (@0))
722 /* In IEEE floating point, x/-1 is not equivalent to -x for snans. */
724 (rdiv @0 real_minus_onep)
725 (if (!tree_expr_maybe_signaling_nan_p (@0))
728 (if (flag_reciprocal_math)
729 /* Convert (A/B)/C to A/(B*C). */
731 (rdiv (rdiv:s @0 @1) @2)
732 (rdiv @0 (mult @1 @2)))
734 /* Canonicalize x / (C1 * y) to (x * C2) / y. */
736 (rdiv @0 (mult:s @1 REAL_CST@2))
738 { tree tem = const_binop (RDIV_EXPR, type, build_one_cst (type), @2); }
740 (rdiv (mult @0 { tem; } ) @1))))
742 /* Convert A/(B/C) to (A/B)*C */
744 (rdiv @0 (rdiv:s @1 @2))
745 (mult (rdiv @0 @1) @2)))
747 /* Simplify x / (- y) to -x / y. */
749 (rdiv @0 (negate @1))
750 (rdiv (negate @0) @1))
752 (if (flag_unsafe_math_optimizations)
753 /* Simplify (C / x op 0.0) to x op 0.0 for C != 0, C != Inf/Nan.
754 Since C / x may underflow to zero, do this only for unsafe math. */
755 (for op (lt le gt ge)
758 (op (rdiv REAL_CST@0 @1) real_zerop@2)
759 (if (!HONOR_SIGNED_ZEROS (@1) && !HONOR_INFINITIES (@1))
761 (if (real_less (&dconst0, TREE_REAL_CST_PTR (@0)))
763 /* For C < 0, use the inverted operator. */
764 (if (real_less (TREE_REAL_CST_PTR (@0), &dconst0))
767 /* Optimize (X & (-A)) / A where A is a power of 2, to X >> log2(A) */
768 (for div (trunc_div ceil_div floor_div round_div exact_div)
770 (div (convert? (bit_and @0 INTEGER_CST@1)) INTEGER_CST@2)
771 (if (integer_pow2p (@2)
772 && tree_int_cst_sgn (@2) > 0
773 && tree_nop_conversion_p (type, TREE_TYPE (@0))
774 && wi::to_wide (@2) + wi::to_wide (@1) == 0)
776 { build_int_cst (integer_type_node,
777 wi::exact_log2 (wi::to_wide (@2))); }))))
779 /* If ARG1 is a constant, we can convert this to a multiply by the
780 reciprocal. This does not have the same rounding properties,
781 so only do this if -freciprocal-math. We can actually
782 always safely do it if ARG1 is a power of two, but it's hard to
783 tell if it is or not in a portable manner. */
784 (for cst (REAL_CST COMPLEX_CST VECTOR_CST)
788 (if (flag_reciprocal_math
791 { tree tem = const_binop (RDIV_EXPR, type, build_one_cst (type), @1); }
793 (mult @0 { tem; } )))
794 (if (cst != COMPLEX_CST)
795 (with { tree inverse = exact_inverse (type, @1); }
797 (mult @0 { inverse; } ))))))))
799 (for mod (ceil_mod floor_mod round_mod trunc_mod)
800 /* 0 % X is always zero. */
802 (mod integer_zerop@0 @1)
803 /* But not for 0 % 0 so that we can get the proper warnings and errors. */
804 (if (!integer_zerop (@1))
806 /* X % 1 is always zero. */
808 (mod @0 integer_onep)
809 { build_zero_cst (type); })
810 /* X % -1 is zero. */
812 (mod @0 integer_minus_onep@1)
813 (if (!TYPE_UNSIGNED (type))
814 { build_zero_cst (type); }))
818 /* But not for 0 % 0 so that we can get the proper warnings and errors. */
819 (if (!integer_zerop (@0))
820 { build_zero_cst (type); }))
821 /* (X % Y) % Y is just X % Y. */
823 (mod (mod@2 @0 @1) @1)
825 /* From extract_muldiv_1: (X * C1) % C2 is zero if C1 is a multiple of C2. */
827 (mod (mult @0 INTEGER_CST@1) INTEGER_CST@2)
828 (if (ANY_INTEGRAL_TYPE_P (type)
829 && TYPE_OVERFLOW_UNDEFINED (type)
830 && wi::multiple_of_p (wi::to_wide (@1), wi::to_wide (@2),
832 { build_zero_cst (type); }))
833 /* For (X % C) == 0, if X is signed and C is power of 2, use unsigned
834 modulo and comparison, since it is simpler and equivalent. */
837 (cmp (mod @0 integer_pow2p@2) integer_zerop@1)
838 (if (!TYPE_UNSIGNED (TREE_TYPE (@0)))
839 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
840 (cmp (mod (convert:utype @0) (convert:utype @2)) (convert:utype @1)))))))
842 /* X % -C is the same as X % C. */
844 (trunc_mod @0 INTEGER_CST@1)
845 (if (TYPE_SIGN (type) == SIGNED
846 && !TREE_OVERFLOW (@1)
847 && wi::neg_p (wi::to_wide (@1))
848 && !TYPE_OVERFLOW_TRAPS (type)
849 /* Avoid this transformation if C is INT_MIN, i.e. C == -C. */
850 && !sign_bit_p (@1, @1))
851 (trunc_mod @0 (negate @1))))
853 /* X % -Y is the same as X % Y. */
855 (trunc_mod @0 (convert? (negate @1)))
856 (if (INTEGRAL_TYPE_P (type)
857 && !TYPE_UNSIGNED (type)
858 && !TYPE_OVERFLOW_TRAPS (type)
859 && tree_nop_conversion_p (type, TREE_TYPE (@1))
860 /* Avoid this transformation if X might be INT_MIN or
861 Y might be -1, because we would then change valid
862 INT_MIN % -(-1) into invalid INT_MIN % -1. */
863 && (expr_not_equal_to (@0, wi::to_wide (TYPE_MIN_VALUE (type)))
864 || expr_not_equal_to (@1, wi::minus_one (TYPE_PRECISION
866 (trunc_mod @0 (convert @1))))
868 /* X - (X / Y) * Y is the same as X % Y. */
870 (minus (convert1? @0) (convert2? (mult:c (trunc_div @@0 @@1) @1)))
871 (if (INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
872 (convert (trunc_mod @0 @1))))
874 /* x * (1 + y / x) - y -> x - y % x */
876 (minus (mult:cs @0 (plus:s (trunc_div:s @1 @0) integer_onep)) @1)
877 (if (INTEGRAL_TYPE_P (type))
878 (minus @0 (trunc_mod @1 @0))))
880 /* Optimize TRUNC_MOD_EXPR by a power of two into a BIT_AND_EXPR,
881 i.e. "X % C" into "X & (C - 1)", if X and C are positive.
882 Also optimize A % (C << N) where C is a power of 2,
883 to A & ((C << N) - 1).
884 Also optimize "A shift (B % C)", if C is a power of 2, to
885 "A shift (B & (C - 1))". SHIFT operation include "<<" and ">>"
886 and assume (B % C) is nonnegative as shifts negative values would
888 (match (power_of_two_cand @1)
890 (match (power_of_two_cand @1)
891 (lshift INTEGER_CST@1 @2))
892 (for mod (trunc_mod floor_mod)
893 (for shift (lshift rshift)
895 (shift @0 (mod @1 (power_of_two_cand@2 @3)))
896 (if (integer_pow2p (@3) && tree_int_cst_sgn (@3) > 0)
897 (shift @0 (bit_and @1 (minus @2 { build_int_cst (TREE_TYPE (@2),
900 (mod @0 (convert? (power_of_two_cand@1 @2)))
901 (if ((TYPE_UNSIGNED (type) || tree_expr_nonnegative_p (@0))
902 /* Allow any integral conversions of the divisor, except
903 conversion from narrower signed to wider unsigned type
904 where if @1 would be negative power of two, the divisor
905 would not be a power of two. */
906 && INTEGRAL_TYPE_P (type)
907 && INTEGRAL_TYPE_P (TREE_TYPE (@1))
908 && (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@1))
909 || TYPE_UNSIGNED (TREE_TYPE (@1))
910 || !TYPE_UNSIGNED (type))
911 && integer_pow2p (@2) && tree_int_cst_sgn (@2) > 0)
912 (with { tree utype = TREE_TYPE (@1);
913 if (!TYPE_OVERFLOW_WRAPS (utype))
914 utype = unsigned_type_for (utype); }
915 (bit_and @0 (convert (minus (convert:utype @1)
916 { build_one_cst (utype); })))))))
918 /* Simplify (unsigned t * 2)/2 -> unsigned t & 0x7FFFFFFF. */
920 (trunc_div (mult @0 integer_pow2p@1) @1)
921 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) && TYPE_UNSIGNED (TREE_TYPE (@0)))
922 (bit_and @0 { wide_int_to_tree
923 (type, wi::mask (TYPE_PRECISION (type)
924 - wi::exact_log2 (wi::to_wide (@1)),
925 false, TYPE_PRECISION (type))); })))
927 /* Simplify (unsigned t / 2) * 2 -> unsigned t & ~1. */
929 (mult (trunc_div @0 integer_pow2p@1) @1)
930 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) && TYPE_UNSIGNED (TREE_TYPE (@0)))
931 (bit_and @0 (negate @1))))
933 (for div (trunc_div ceil_div floor_div round_div exact_div)
934 /* Simplify (t * u) / u -> t. */
936 (div (mult:c @0 @1) @1)
937 (if (ANY_INTEGRAL_TYPE_P (type))
938 (if (TYPE_OVERFLOW_UNDEFINED (type) && !TYPE_OVERFLOW_SANITIZED (type))
941 (with {value_range vr0, vr1;}
942 (if (INTEGRAL_TYPE_P (type)
943 && get_range_query (cfun)->range_of_expr (vr0, @0)
944 && get_range_query (cfun)->range_of_expr (vr1, @1)
945 && range_op_handler (MULT_EXPR).overflow_free_p (vr0, vr1))
950 /* Simplify (t * u) / v -> t * (u / v) if u is multiple of v. */
952 (div (mult @0 INTEGER_CST@1) INTEGER_CST@2)
953 (if (INTEGRAL_TYPE_P (type)
954 && wi::multiple_of_p (wi::to_widest (@1), wi::to_widest (@2), SIGNED))
955 (if (TYPE_OVERFLOW_UNDEFINED (type) && !TYPE_OVERFLOW_SANITIZED (type))
956 (mult @0 (div! @1 @2))
957 (with {value_range vr0, vr1;}
958 (if (get_range_query (cfun)->range_of_expr (vr0, @0)
959 && get_range_query (cfun)->range_of_expr (vr1, @1)
960 && range_op_handler (MULT_EXPR).overflow_free_p (vr0, vr1))
961 (mult @0 (div! @1 @2))))
964 /* Simplify (t * u) / (t * v) -> (u / v) if u is multiple of v. */
966 (div (mult @0 INTEGER_CST@1) (mult @0 INTEGER_CST@2))
967 (if (INTEGRAL_TYPE_P (type)
968 && wi::multiple_of_p (wi::to_widest (@1), wi::to_widest (@2), SIGNED))
969 (if (TYPE_OVERFLOW_UNDEFINED (type) && !TYPE_OVERFLOW_SANITIZED (type))
972 (with {value_range vr0, vr1, vr2;}
973 (if (get_range_query (cfun)->range_of_expr (vr0, @0)
974 && get_range_query (cfun)->range_of_expr (vr1, @1)
975 && get_range_query (cfun)->range_of_expr (vr2, @2)
976 && range_op_handler (MULT_EXPR).overflow_free_p (vr0, vr1)
977 && range_op_handler (MULT_EXPR).overflow_free_p (vr0, vr2))
983 (for div (trunc_div exact_div)
984 /* Simplify (X + M*N) / N -> X / N + M. */
986 (div (plus:c@4 @0 (mult:c@3 @1 @2)) @2)
987 (with {value_range vr0, vr1, vr2, vr3, vr4;}
988 (if (INTEGRAL_TYPE_P (type)
989 && get_range_query (cfun)->range_of_expr (vr1, @1)
990 && get_range_query (cfun)->range_of_expr (vr2, @2)
991 /* "N*M" doesn't overflow. */
992 && range_op_handler (MULT_EXPR).overflow_free_p (vr1, vr2)
993 && get_range_query (cfun)->range_of_expr (vr0, @0)
994 && get_range_query (cfun)->range_of_expr (vr3, @3)
995 /* "X+(N*M)" doesn't overflow. */
996 && range_op_handler (PLUS_EXPR).overflow_free_p (vr0, vr3)
997 && get_range_query (cfun)->range_of_expr (vr4, @4)
998 && !vr4.undefined_p ()
999 /* "X+N*M" is not with opposite sign as "X". */
1000 && (TYPE_UNSIGNED (type)
1001 || (vr0.nonnegative_p () && vr4.nonnegative_p ())
1002 || (vr0.nonpositive_p () && vr4.nonpositive_p ())))
1003 (plus (div @0 @2) @1))))
1005 /* Simplify (X - M*N) / N -> X / N - M. */
1007 (div (minus@4 @0 (mult:c@3 @1 @2)) @2)
1008 (with {value_range vr0, vr1, vr2, vr3, vr4;}
1009 (if (INTEGRAL_TYPE_P (type)
1010 && get_range_query (cfun)->range_of_expr (vr1, @1)
1011 && get_range_query (cfun)->range_of_expr (vr2, @2)
1012 /* "N * M" doesn't overflow. */
1013 && range_op_handler (MULT_EXPR).overflow_free_p (vr1, vr2)
1014 && get_range_query (cfun)->range_of_expr (vr0, @0)
1015 && get_range_query (cfun)->range_of_expr (vr3, @3)
1016 /* "X - (N*M)" doesn't overflow. */
1017 && range_op_handler (MINUS_EXPR).overflow_free_p (vr0, vr3)
1018 && get_range_query (cfun)->range_of_expr (vr4, @4)
1019 && !vr4.undefined_p ()
1020 /* "X-N*M" is not with opposite sign as "X". */
1021 && (TYPE_UNSIGNED (type)
1022 || (vr0.nonnegative_p () && vr4.nonnegative_p ())
1023 || (vr0.nonpositive_p () && vr4.nonpositive_p ())))
1024 (minus (div @0 @2) @1)))))
1027 (X + C) / N -> X / N + C / N where C is multiple of N.
1028 (X + C) >> N -> X >> N + C>>N if low N bits of C is 0. */
1029 (for op (trunc_div exact_div rshift)
1031 (op (plus@3 @0 INTEGER_CST@1) INTEGER_CST@2)
1034 wide_int c = wi::to_wide (@1);
1035 wide_int n = wi::to_wide (@2);
1036 bool shift = op == RSHIFT_EXPR;
1037 #define plus_op1(v) (shift ? wi::rshift (v, n, TYPE_SIGN (type)) \
1038 : wi::div_trunc (v, n, TYPE_SIGN (type)))
1039 #define exact_mod(v) (shift ? wi::ctz (v) >= n.to_shwi () \
1040 : wi::multiple_of_p (v, n, TYPE_SIGN (type)))
1041 value_range vr0, vr1, vr3;
1043 (if (INTEGRAL_TYPE_P (type)
1044 && get_range_query (cfun)->range_of_expr (vr0, @0))
1046 && get_range_query (cfun)->range_of_expr (vr1, @1)
1047 /* "X+C" doesn't overflow. */
1048 && range_op_handler (PLUS_EXPR).overflow_free_p (vr0, vr1)
1049 && get_range_query (cfun)->range_of_expr (vr3, @3)
1050 && !vr3.undefined_p ()
1051 /* "X+C" and "X" are not of opposite sign. */
1052 && (TYPE_UNSIGNED (type)
1053 || (vr0.nonnegative_p () && vr3.nonnegative_p ())
1054 || (vr0.nonpositive_p () && vr3.nonpositive_p ())))
1055 (plus (op @0 @2) { wide_int_to_tree (type, plus_op1 (c)); })
1056 (if (!vr0.undefined_p () && TYPE_UNSIGNED (type) && c.sign_mask () < 0
1058 /* unsigned "X-(-C)" doesn't underflow. */
1059 && wi::geu_p (vr0.lower_bound (), -c))
1060 (plus (op @0 @2) { wide_int_to_tree (type, -plus_op1 (-c)); })))))))
1065 /* (nop_outer_cast)-(inner_cast)var -> -(outer_cast)(var)
1066 if var is smaller in precision.
1067 This is always safe for both doing the negative in signed or unsigned
1068 as the value for undefined will not show up.
1069 Note the outer cast cannot be a boolean type as the only valid values
1070 are 0,-1/1 (depending on the signedness of the boolean) and the negative
1071 is there to get the correct value. */
1073 (convert (negate:s@1 (convert:s @0)))
1074 (if (INTEGRAL_TYPE_P (type)
1075 && tree_nop_conversion_p (type, TREE_TYPE (@1))
1076 && TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (@0))
1077 && TREE_CODE (type) != BOOLEAN_TYPE)
1078 (negate (convert @0))))
1080 (for op (negate abs)
1081 /* Simplify cos(-x) and cos(|x|) -> cos(x). Similarly for cosh. */
1082 (for coss (COS COSH)
1086 /* Simplify pow(-x, y) and pow(|x|,y) -> pow(x,y) if y is an even integer. */
1089 (pows (op @0) REAL_CST@1)
1090 (with { HOST_WIDE_INT n; }
1091 (if (real_isinteger (&TREE_REAL_CST (@1), &n) && (n & 1) == 0)
1093 /* Likewise for powi. */
1096 (pows (op @0) INTEGER_CST@1)
1097 (if ((wi::to_wide (@1) & 1) == 0)
1099 /* Strip negate and abs from both operands of hypot. */
1107 /* copysign(-x, y) and copysign(abs(x), y) -> copysign(x, y). */
1108 (for copysigns (COPYSIGN_ALL)
1110 (copysigns (op @0) @1)
1111 (copysigns @0 @1))))
1113 /* abs(x)*abs(x) -> x*x. Should be valid for all types. */
1115 (mult (abs@1 @0) @1)
1118 /* Convert absu(x)*absu(x) -> x*x. */
1120 (mult (absu@1 @0) @1)
1121 (mult (convert@2 @0) @2))
1123 /* cos(copysign(x, y)) -> cos(x). Similarly for cosh. */
1124 (for coss (COS COSH)
1125 (for copysigns (COPYSIGN)
1127 (coss (copysigns @0 @1))
1130 /* pow(copysign(x, y), z) -> pow(x, z) if z is an even integer. */
1132 (for copysigns (COPYSIGN)
1134 (pows (copysigns @0 @2) REAL_CST@1)
1135 (with { HOST_WIDE_INT n; }
1136 (if (real_isinteger (&TREE_REAL_CST (@1), &n) && (n & 1) == 0)
1138 /* Likewise for powi. */
1140 (for copysigns (COPYSIGN)
1142 (pows (copysigns @0 @2) INTEGER_CST@1)
1143 (if ((wi::to_wide (@1) & 1) == 0)
1147 (for copysigns (COPYSIGN)
1148 /* hypot(copysign(x, y), z) -> hypot(x, z). */
1150 (hypots (copysigns @0 @1) @2)
1152 /* hypot(x, copysign(y, z)) -> hypot(x, y). */
1154 (hypots @0 (copysigns @1 @2))
1157 /* copysign(x, CST) -> abs (x). */
1158 (for copysigns (COPYSIGN_ALL)
1160 (copysigns @0 REAL_CST@1)
1161 (if (!REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
1164 /* Transform fneg (fabs (X)) -> copysign (X, -1). */
1167 (IFN_COPYSIGN @0 { build_minus_one_cst (type); }))
1169 /* copysign(copysign(x, y), z) -> copysign(x, z). */
1170 (for copysigns (COPYSIGN_ALL)
1172 (copysigns (copysigns @0 @1) @2)
1175 /* copysign(x,y)*copysign(x,y) -> x*x. */
1176 (for copysigns (COPYSIGN_ALL)
1178 (mult (copysigns@2 @0 @1) @2)
1181 /* ccos(-x) -> ccos(x). Similarly for ccosh. */
1182 (for ccoss (CCOS CCOSH)
1187 /* cabs(-x) and cos(conj(x)) -> cabs(x). */
1188 (for ops (conj negate)
1194 /* Fold (a * (1 << b)) into (a << b) */
1196 (mult:c @0 (convert? (lshift integer_onep@1 @2)))
1197 (if (! FLOAT_TYPE_P (type)
1198 && tree_nop_conversion_p (type, TREE_TYPE (@1)))
1201 /* Shifts by precision or greater result in zero. */
1202 (for shift (lshift rshift)
1204 (shift @0 uniform_integer_cst_p@1)
1205 (if ((GIMPLE || !sanitize_flags_p (SANITIZE_SHIFT_EXPONENT))
1206 /* Leave arithmetic right shifts of possibly negative values alone. */
1207 && (TYPE_UNSIGNED (type)
1208 || shift == LSHIFT_EXPR
1209 || tree_expr_nonnegative_p (@0))
1210 /* Use a signed compare to leave negative shift counts alone. */
1211 && wi::ges_p (wi::to_wide (uniform_integer_cst_p (@1)),
1212 element_precision (type)))
1213 { build_zero_cst (type); })))
1215 /* Shifts by constants distribute over several binary operations,
1216 hence (X << C) + (Y << C) can be simplified to (X + Y) << C. */
1217 (for op (plus minus)
1219 (op (lshift:s @0 @1) (lshift:s @2 @1))
1220 (if (INTEGRAL_TYPE_P (type)
1221 && TYPE_OVERFLOW_WRAPS (type)
1222 && !TYPE_SATURATING (type))
1223 (lshift (op @0 @2) @1))))
1225 (for op (bit_and bit_ior bit_xor)
1227 (op (lshift:s @0 @1) (lshift:s @2 @1))
1228 (if (INTEGRAL_TYPE_P (type))
1229 (lshift (op @0 @2) @1)))
1231 (op (rshift:s @0 @1) (rshift:s @2 @1))
1232 (if (INTEGRAL_TYPE_P (type))
1233 (rshift (op @0 @2) @1))))
1235 /* Fold (1 << (C - x)) where C = precision(type) - 1
1236 into ((1 << C) >> x). */
1238 (lshift integer_onep@0 (minus@1 INTEGER_CST@2 @3))
1239 (if (INTEGRAL_TYPE_P (type)
1240 && wi::eq_p (wi::to_wide (@2), TYPE_PRECISION (type) - 1)
1242 (if (TYPE_UNSIGNED (type))
1243 (rshift (lshift @0 @2) @3)
1245 { tree utype = unsigned_type_for (type); }
1246 (convert (rshift (lshift (convert:utype @0) @2) @3))))))
1248 /* Fold ((type)(a<0)) << SIGNBITOFA into ((type)a) & signbit. */
1250 (lshift (convert (lt @0 integer_zerop@1)) INTEGER_CST@2)
1251 (if (TYPE_SIGN (TREE_TYPE (@0)) == SIGNED
1252 && wi::eq_p (wi::to_wide (@2), TYPE_PRECISION (TREE_TYPE (@0)) - 1))
1253 (with { wide_int wone = wi::one (TYPE_PRECISION (type)); }
1254 (bit_and (convert @0)
1255 { wide_int_to_tree (type,
1256 wi::lshift (wone, wi::to_wide (@2))); }))))
1258 /* Fold (-x >> C) into -(x > 0) where C = precision(type) - 1. */
1259 (for cst (INTEGER_CST VECTOR_CST)
1261 (rshift (negate:s @0) cst@1)
1262 (if (!TYPE_UNSIGNED (type)
1263 && TYPE_OVERFLOW_UNDEFINED (type))
1264 (with { tree stype = TREE_TYPE (@1);
1265 tree bt = truth_type_for (type);
1266 tree zeros = build_zero_cst (type);
1267 tree cst = NULL_TREE; }
1269 /* Handle scalar case. */
1270 (if (INTEGRAL_TYPE_P (type)
1271 /* If we apply the rule to the scalar type before vectorization
1272 we will enforce the result of the comparison being a bool
1273 which will require an extra AND on the result that will be
1274 indistinguishable from when the user did actually want 0
1275 or 1 as the result so it can't be removed. */
1276 && canonicalize_math_after_vectorization_p ()
1277 && wi::eq_p (wi::to_wide (@1), TYPE_PRECISION (type) - 1))
1278 (negate (convert (gt @0 { zeros; }))))
1279 /* Handle vector case. */
1280 (if (VECTOR_INTEGER_TYPE_P (type)
1281 /* First check whether the target has the same mode for vector
1282 comparison results as it's operands do. */
1283 && TYPE_MODE (bt) == TYPE_MODE (type)
1284 /* Then check to see if the target is able to expand the comparison
1285 with the given type later on, otherwise we may ICE. */
1286 && expand_vec_cmp_expr_p (type, bt, GT_EXPR)
1287 && (cst = uniform_integer_cst_p (@1)) != NULL
1288 && wi::eq_p (wi::to_wide (cst), element_precision (type) - 1))
1289 (view_convert (gt:bt @0 { zeros; }))))))))
1291 /* Fold (C1/X)*C2 into (C1*C2)/X. */
1293 (mult (rdiv@3 REAL_CST@0 @1) REAL_CST@2)
1294 (if (flag_associative_math
1297 { tree tem = const_binop (MULT_EXPR, type, @0, @2); }
1299 (rdiv { tem; } @1)))))
1301 /* Simplify ~X & X as zero. */
1303 (bit_and (convert? @0) (convert? @1))
1304 (with { bool wascmp; }
1305 (if (types_match (TREE_TYPE (@0), TREE_TYPE (@1))
1306 && bitwise_inverted_equal_p (@0, @1, wascmp))
1307 { wascmp ? constant_boolean_node (false, type) : build_zero_cst (type); })))
1309 /* PR71636: Transform x & ((1U << b) - 1) -> x & ~(~0U << b); */
1311 (bit_and:c @0 (plus:s (lshift:s integer_onep @1) integer_minus_onep))
1312 (if (TYPE_UNSIGNED (type))
1313 (bit_and @0 (bit_not (lshift { build_all_ones_cst (type); } @1)))))
1315 (for bitop (bit_and bit_ior)
1317 /* PR35691: Transform
1318 (x == 0 & y == 0) -> (x | typeof(x)(y)) == 0.
1319 (x != 0 | y != 0) -> (x | typeof(x)(y)) != 0. */
1321 (bitop (cmp @0 integer_zerop@2) (cmp @1 integer_zerop))
1322 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1323 && INTEGRAL_TYPE_P (TREE_TYPE (@1))
1324 && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
1325 (cmp (bit_ior @0 (convert @1)) @2)))
1327 (x == -1 & y == -1) -> (x & typeof(x)(y)) == -1.
1328 (x != -1 | y != -1) -> (x & typeof(x)(y)) != -1. */
1330 (bitop (cmp @0 integer_all_onesp@2) (cmp @1 integer_all_onesp))
1331 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1332 && INTEGRAL_TYPE_P (TREE_TYPE (@1))
1333 && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
1334 (cmp (bit_and @0 (convert @1)) @2))))
1336 /* Fold (A & ~B) - (A & B) into (A ^ B) - B. */
1338 (minus (bit_and:cs @0 (bit_not @1)) (bit_and:cs @0 @1))
1339 (minus (bit_xor @0 @1) @1))
1341 (minus (bit_and:s @0 INTEGER_CST@2) (bit_and:s @0 INTEGER_CST@1))
1342 (if (~wi::to_wide (@2) == wi::to_wide (@1))
1343 (minus (bit_xor @0 @1) @1)))
1345 /* Fold (A & B) - (A & ~B) into B - (A ^ B). */
1347 (minus (bit_and:cs @0 @1) (bit_and:cs @0 (bit_not @1)))
1348 (minus @1 (bit_xor @0 @1)))
1350 /* Simplify (X & ~Y) |^+ (~X & Y) -> X ^ Y. */
1351 (for op (bit_ior bit_xor plus)
1353 (op (bit_and:c @0 @2) (bit_and:c @3 @1))
1354 (with { bool wascmp0, wascmp1; }
1355 (if (bitwise_inverted_equal_p (@2, @1, wascmp0)
1356 && bitwise_inverted_equal_p (@0, @3, wascmp1)
1357 && ((!wascmp0 && !wascmp1)
1358 || element_precision (type) == 1))
1361 /* PR53979: Transform ((a ^ b) | a) -> (a | b) */
1363 (bit_ior:c (bit_xor:c @0 @1) @0)
1366 /* (a & ~b) | (a ^ b) --> a ^ b */
1368 (bit_ior:c (bit_and:c @0 (bit_not @1)) (bit_xor:c@2 @0 @1))
1371 /* (a & ~b) ^ ~a --> ~(a & b) */
1373 (bit_xor:c (bit_and:cs @0 (bit_not @1)) (bit_not @0))
1374 (bit_not (bit_and @0 @1)))
1376 /* (~a & b) ^ a --> (a | b) */
1378 (bit_xor:c (bit_and:cs (bit_not @0) @1) @0)
1381 /* (a | b) & ~(a ^ b) --> a & b */
1383 (bit_and:c (bit_ior @0 @1) (bit_not (bit_xor:c @0 @1)))
1386 /* (a | b) & (a == b) --> a & b (boolean version of the above). */
1388 (bit_and:c (bit_ior @0 @1) (nop_convert? (eq:c @0 @1)))
1389 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1390 && TYPE_PRECISION (TREE_TYPE (@0)) == 1)
1393 /* a | ~(a ^ b) --> a | ~b */
1395 (bit_ior:c @0 (bit_not:s (bit_xor:c @0 @1)))
1396 (bit_ior @0 (bit_not @1)))
1398 /* a | (a == b) --> a | (b^1) (boolean version of the above). */
1400 (bit_ior:c @0 (nop_convert? (eq:c @0 @1)))
1401 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1402 && TYPE_PRECISION (TREE_TYPE (@0)) == 1)
1403 (bit_ior @0 (bit_xor @1 { build_one_cst (type); }))))
1405 /* a | ((~a) ^ b) --> a | (~b) (alt version of the above 2) */
1407 (bit_ior:c @0 (bit_xor:cs @1 @2))
1408 (with { bool wascmp; }
1409 (if (bitwise_inverted_equal_p (@0, @1, wascmp)
1410 && (!wascmp || element_precision (type) == 1))
1411 (bit_ior @0 (bit_not @2)))))
1413 /* a & ~(a ^ b) --> a & b */
1415 (bit_and:c @0 (bit_not (bit_xor:c @0 @1)))
1418 /* a & (a == b) --> a & b (boolean version of the above). */
1420 (bit_and:c @0 (nop_convert? (eq:c @0 @1)))
1421 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1422 && TYPE_PRECISION (TREE_TYPE (@0)) == 1)
1425 /* a & ((~a) ^ b) --> a & b (alt version of the above 2) */
1427 (bit_and:c @0 (bit_xor:c @1 @2))
1428 (with { bool wascmp; }
1429 (if (bitwise_inverted_equal_p (@0, @1, wascmp)
1430 && (!wascmp || element_precision (type) == 1))
1433 /* (a | b) | (a &^ b) --> a | b */
1434 (for op (bit_and bit_xor)
1436 (bit_ior:c (bit_ior@2 @0 @1) (op:c @0 @1))
1439 /* (a & b) | ~(a ^ b) --> ~(a ^ b) */
1441 (bit_ior:c (bit_and:c @0 @1) (bit_not@2 (bit_xor @0 @1)))
1444 /* (a & b) | (a == b) --> a == b */
1446 (bit_ior:c (bit_and:c @0 @1) (nop_convert?@2 (eq @0 @1)))
1447 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1448 && TYPE_PRECISION (TREE_TYPE (@0)) == 1)
1451 /* ~(~a & b) --> a | ~b */
1453 (bit_not (bit_and:cs (bit_not @0) @1))
1454 (bit_ior @0 (bit_not @1)))
1456 /* ~(~a | b) --> a & ~b */
1458 (bit_not (bit_ior:cs (bit_not @0) @1))
1459 (bit_and @0 (bit_not @1)))
1461 /* (a ^ b) & ((b ^ c) ^ a) --> (a ^ b) & ~c */
1463 (bit_and:c (bit_xor:c@3 @0 @1) (bit_xor:cs (bit_xor:cs @1 @2) @0))
1464 (bit_and @3 (bit_not @2)))
1466 /* (a ^ b) | ((b ^ c) ^ a) --> (a ^ b) | c */
1468 (bit_ior:c (bit_xor:c@3 @0 @1) (bit_xor:c (bit_xor:c @1 @2) @0))
1471 /* (~X | C) ^ D -> (X | C) ^ (~D ^ C) if (~D ^ C) can be simplified. */
1473 (bit_xor:c (bit_ior:cs (bit_not:s @0) @1) @2)
1474 (bit_xor (bit_ior @0 @1) (bit_xor! (bit_not! @2) @1)))
1476 /* (~X & C) ^ D -> (X & C) ^ (D ^ C) if (D ^ C) can be simplified. */
1478 (bit_xor:c (bit_and:cs (bit_not:s @0) @1) @2)
1479 (bit_xor (bit_and @0 @1) (bit_xor! @2 @1)))
1481 /* Simplify (~X & Y) to X ^ Y if we know that (X & ~Y) is 0. */
1483 (bit_and (bit_not SSA_NAME@0) INTEGER_CST@1)
1484 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1485 && wi::bit_and_not (get_nonzero_bits (@0), wi::to_wide (@1)) == 0)
1488 /* For constants M and N, if M == (1LL << cst) - 1 && (N & M) == M,
1489 ((A & N) + B) & M -> (A + B) & M
1490 Similarly if (N & M) == 0,
1491 ((A | N) + B) & M -> (A + B) & M
1492 and for - instead of + (or unary - instead of +)
1493 and/or ^ instead of |.
1494 If B is constant and (B & M) == 0, fold into A & M. */
1495 (for op (plus minus)
1496 (for bitop (bit_and bit_ior bit_xor)
1498 (bit_and (op:s (bitop:s@0 @3 INTEGER_CST@4) @1) INTEGER_CST@2)
1501 tree utype = fold_bit_and_mask (TREE_TYPE (@0), @2, op, @0, bitop,
1502 @3, @4, @1, ERROR_MARK, NULL_TREE,
1505 (convert (bit_and (op (convert:utype { pmop[0]; })
1506 (convert:utype { pmop[1]; }))
1507 (convert:utype @2))))))
1509 (bit_and (op:s @0 (bitop:s@1 @3 INTEGER_CST@4)) INTEGER_CST@2)
1512 tree utype = fold_bit_and_mask (TREE_TYPE (@0), @2, op, @0, ERROR_MARK,
1513 NULL_TREE, NULL_TREE, @1, bitop, @3,
1516 (convert (bit_and (op (convert:utype { pmop[0]; })
1517 (convert:utype { pmop[1]; }))
1518 (convert:utype @2)))))))
1520 (bit_and (op:s @0 @1) INTEGER_CST@2)
1523 tree utype = fold_bit_and_mask (TREE_TYPE (@0), @2, op, @0, ERROR_MARK,
1524 NULL_TREE, NULL_TREE, @1, ERROR_MARK,
1525 NULL_TREE, NULL_TREE, pmop); }
1527 (convert (bit_and (op (convert:utype { pmop[0]; })
1528 (convert:utype { pmop[1]; }))
1529 (convert:utype @2)))))))
1530 (for bitop (bit_and bit_ior bit_xor)
1532 (bit_and (negate:s (bitop:s@0 @2 INTEGER_CST@3)) INTEGER_CST@1)
1535 tree utype = fold_bit_and_mask (TREE_TYPE (@0), @1, NEGATE_EXPR, @0,
1536 bitop, @2, @3, NULL_TREE, ERROR_MARK,
1537 NULL_TREE, NULL_TREE, pmop); }
1539 (convert (bit_and (negate (convert:utype { pmop[0]; }))
1540 (convert:utype @1)))))))
1542 /* X % Y is smaller than Y. */
1545 (cmp:c (trunc_mod @0 @1) @1)
1546 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
1547 { constant_boolean_node (cmp == LT_EXPR, type); })))
1551 (bit_ior @0 integer_all_onesp@1)
1556 (bit_ior @0 integer_zerop)
1561 (bit_and @0 integer_zerop@1)
1566 (for op (bit_ior bit_xor)
1568 (op (convert? @0) (convert? @1))
1569 (with { bool wascmp; }
1570 (if (types_match (TREE_TYPE (@0), TREE_TYPE (@1))
1571 && bitwise_inverted_equal_p (@0, @1, wascmp))
1574 ? constant_boolean_node (true, type)
1575 : build_all_ones_cst (TREE_TYPE (@0)); })))))
1580 { build_zero_cst (type); })
1582 /* Canonicalize X ^ ~0 to ~X. */
1584 (bit_xor @0 integer_all_onesp@1)
1589 (bit_and @0 integer_all_onesp)
1592 /* x & x -> x, x | x -> x */
1593 (for bitop (bit_and bit_ior)
1598 /* x & C -> x if we know that x & ~C == 0. */
1601 (bit_and SSA_NAME@0 INTEGER_CST@1)
1602 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1603 && wi::bit_and_not (get_nonzero_bits (@0), wi::to_wide (@1)) == 0)
1606 /* `a & (x | CST)` -> a if we know that (a & ~CST) == 0 */
1608 (bit_and:c SSA_NAME@0 (bit_ior @1 INTEGER_CST@2))
1609 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1610 && wi::bit_and_not (get_nonzero_bits (@0), wi::to_wide (@2)) == 0)
1613 /* x | C -> C if we know that x & ~C == 0. */
1615 (bit_ior SSA_NAME@0 INTEGER_CST@1)
1616 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1617 && wi::bit_and_not (get_nonzero_bits (@0), wi::to_wide (@1)) == 0)
1621 /* ~(~X - Y) -> X + Y and ~(~X + Y) -> X - Y. */
1623 (bit_not (minus (bit_not @0) @1))
1626 (bit_not (plus:c (bit_not @0) @1))
1628 /* (~X - ~Y) -> Y - X. */
1630 (minus (bit_not @0) (bit_not @1))
1631 (if (!TYPE_OVERFLOW_SANITIZED (type))
1632 (with { tree utype = unsigned_type_for (type); }
1633 (convert (minus (convert:utype @1) (convert:utype @0))))))
1635 /* ~(X - Y) -> ~X + Y. */
1637 (bit_not (minus:s @0 @1))
1638 (plus (bit_not @0) @1))
1640 (bit_not (plus:s @0 INTEGER_CST@1))
1641 (if ((INTEGRAL_TYPE_P (type)
1642 && TYPE_UNSIGNED (type))
1643 || (!TYPE_OVERFLOW_SANITIZED (type)
1644 && may_negate_without_overflow_p (@1)))
1645 (plus (bit_not @0) { const_unop (NEGATE_EXPR, type, @1); })))
1648 /* ~X + Y -> (Y - X) - 1. */
1650 (plus:c (bit_not @0) @1)
1651 (if (ANY_INTEGRAL_TYPE_P (type)
1652 && TYPE_OVERFLOW_WRAPS (type)
1653 /* -1 - X is folded to ~X, so we'd recurse endlessly. */
1654 && !integer_all_onesp (@1))
1655 (plus (minus @1 @0) { build_minus_one_cst (type); })
1656 (if (INTEGRAL_TYPE_P (type)
1657 && TREE_CODE (@1) == INTEGER_CST
1658 && wi::to_wide (@1) != wi::min_value (TYPE_PRECISION (type),
1660 (minus (plus @1 { build_minus_one_cst (type); }) @0))))
1663 /* ~(X >> Y) -> ~X >> Y if ~X can be simplified. */
1665 (bit_not (rshift:s @0 @1))
1666 (if (!TYPE_UNSIGNED (TREE_TYPE (@0)))
1667 (rshift (bit_not! @0) @1)
1668 /* For logical right shifts, this is possible only if @0 doesn't
1669 have MSB set and the logical right shift is changed into
1670 arithmetic shift. */
1671 (if (INTEGRAL_TYPE_P (type)
1672 && !wi::neg_p (tree_nonzero_bits (@0)))
1673 (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
1674 (convert (rshift (bit_not! (convert:stype @0)) @1))))))
1676 /* x + (x & 1) -> (x + 1) & ~1 */
1678 (plus:c @0 (bit_and:s @0 integer_onep@1))
1679 (bit_and (plus @0 @1) (bit_not @1)))
1681 /* x & ~(x & y) -> x & ~y */
1682 /* x | ~(x | y) -> x | ~y */
1683 (for bitop (bit_and bit_ior)
1685 (bitop:c @0 (bit_not (bitop:cs @0 @1)))
1686 (bitop @0 (bit_not @1))))
1688 /* (~x & y) | ~(x | y) -> ~x */
1690 (bit_ior:c (bit_and:c (bit_not@2 @0) @1) (bit_not (bit_ior:c @0 @1)))
1693 /* (x | y) ^ (x | ~y) -> ~x */
1695 (bit_xor:c (bit_ior:c @0 @1) (bit_ior:c @0 (bit_not @1)))
1698 /* (x & y) | ~(x | y) -> ~(x ^ y) */
1700 (bit_ior:c (bit_and:s @0 @1) (bit_not:s (bit_ior:s @0 @1)))
1701 (bit_not (bit_xor @0 @1)))
1703 /* (~x | y) ^ (x ^ y) -> x | ~y */
1705 (bit_xor:c (bit_ior:cs (bit_not @0) @1) (bit_xor:s @0 @1))
1706 (bit_ior @0 (bit_not @1)))
1708 /* (x ^ y) | ~(x | y) -> ~(x & y) */
1710 (bit_ior:c (bit_xor:s @0 @1) (bit_not:s (bit_ior:s @0 @1)))
1711 (bit_not (bit_and @0 @1)))
1713 /* (x & y) ^ (x | y) -> x ^ y */
1715 (bit_xor:c (bit_and @0 @1) (bit_ior @0 @1))
1718 /* (x ^ y) ^ (x | y) -> x & y */
1720 (bit_xor:c (bit_xor @0 @1) (bit_ior @0 @1))
1723 /* (x & y) + (x ^ y) -> x | y */
1724 /* (x & y) | (x ^ y) -> x | y */
1725 /* (x & y) ^ (x ^ y) -> x | y */
1726 (for op (plus bit_ior bit_xor)
1728 (op:c (bit_and @0 @1) (bit_xor @0 @1))
1731 /* (x & y) + (x | y) -> x + y */
1733 (plus:c (bit_and @0 @1) (bit_ior @0 @1))
1736 /* (x + y) - (x | y) -> x & y */
1738 (minus (plus @0 @1) (bit_ior @0 @1))
1739 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1740 && !TYPE_SATURATING (type))
1743 /* (x + y) - (x & y) -> x | y */
1745 (minus (plus @0 @1) (bit_and @0 @1))
1746 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1747 && !TYPE_SATURATING (type))
1750 /* (x | y) - y -> (x & ~y) */
1752 (minus (bit_ior:cs @0 @1) @1)
1753 (bit_and @0 (bit_not @1)))
1755 /* (x | y) - (x ^ y) -> x & y */
1757 (minus (bit_ior @0 @1) (bit_xor @0 @1))
1760 /* (x | y) - (x & y) -> x ^ y */
1762 (minus (bit_ior @0 @1) (bit_and @0 @1))
1765 /* (x | y) & ~(x & y) -> x ^ y */
1767 (bit_and:c (bit_ior @0 @1) (bit_not (bit_and @0 @1)))
1770 /* (x | y) & (~x ^ y) -> x & y */
1772 (bit_and:c (bit_ior:c @0 @1) (bit_xor:c @1 @2))
1773 (with { bool wascmp; }
1774 (if (bitwise_inverted_equal_p (@0, @2, wascmp)
1775 && (!wascmp || element_precision (type) == 1))
1778 /* (~x | y) & (x | ~y) -> ~(x ^ y) */
1780 (bit_and (bit_ior:cs (bit_not @0) @1) (bit_ior:cs @0 (bit_not @1)))
1781 (bit_not (bit_xor @0 @1)))
1783 /* (~x | y) ^ (x | ~y) -> x ^ y */
1785 (bit_xor (bit_ior:c (bit_not @0) @1) (bit_ior:c @0 (bit_not @1)))
1788 /* ((x & y) - (x | y)) - 1 -> ~(x ^ y) */
1790 (plus (nop_convert1? (minus@2 (nop_convert2? (bit_and:c @0 @1))
1791 (nop_convert2? (bit_ior @0 @1))))
1793 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1794 && !TYPE_SATURATING (type) && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2))
1795 && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@2))
1796 && !TYPE_SATURATING (TREE_TYPE (@2)))
1797 (bit_not (convert (bit_xor @0 @1)))))
1799 (minus (nop_convert1? (plus@2 (nop_convert2? (bit_and:c @0 @1))
1801 (nop_convert3? (bit_ior @0 @1)))
1802 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1803 && !TYPE_SATURATING (type) && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2))
1804 && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@2))
1805 && !TYPE_SATURATING (TREE_TYPE (@2)))
1806 (bit_not (convert (bit_xor @0 @1)))))
1808 (minus (nop_convert1? (bit_and @0 @1))
1809 (nop_convert2? (plus@2 (nop_convert3? (bit_ior:c @0 @1))
1811 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1812 && !TYPE_SATURATING (type) && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2))
1813 && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@2))
1814 && !TYPE_SATURATING (TREE_TYPE (@2)))
1815 (bit_not (convert (bit_xor @0 @1)))))
1817 /* ~x & ~y -> ~(x | y)
1818 ~x | ~y -> ~(x & y) */
1819 (for op (bit_and bit_ior)
1820 rop (bit_ior bit_and)
1822 (op (convert1? (bit_not @0)) (convert2? (bit_not @1)))
1823 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1824 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
1825 (bit_not (rop (convert @0) (convert @1))))))
1827 /* If we are XORing or adding two BIT_AND_EXPR's, both of which are and'ing
1828 with a constant, and the two constants have no bits in common,
1829 we should treat this as a BIT_IOR_EXPR since this may produce more
1831 (for op (bit_xor plus)
1833 (op (convert1? (bit_and@4 @0 INTEGER_CST@1))
1834 (convert2? (bit_and@5 @2 INTEGER_CST@3)))
1835 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
1836 && tree_nop_conversion_p (type, TREE_TYPE (@2))
1837 && (wi::to_wide (@1) & wi::to_wide (@3)) == 0)
1838 (bit_ior (convert @4) (convert @5)))))
1840 /* (X | Y) ^ X -> Y & ~ X*/
1842 (bit_xor:c (convert1? (bit_ior:c @@0 @1)) (convert2? @0))
1843 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1844 (convert (bit_and @1 (bit_not @0)))))
1846 /* (~X | Y) ^ X -> ~(X & Y). */
1848 (bit_xor:c (nop_convert1? (bit_ior:c (nop_convert2? (bit_not @0)) @1)) @2)
1849 (if (bitwise_equal_p (@0, @2))
1850 (convert (bit_not (bit_and @0 (convert @1))))))
1852 /* Convert ~X ^ ~Y to X ^ Y. */
1854 (bit_xor (convert1? (bit_not @0)) (convert2? (bit_not @1)))
1855 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1856 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
1857 (bit_xor (convert @0) (convert @1))))
1859 /* Convert ~X ^ C to X ^ ~C. */
1861 (bit_xor (convert? (bit_not @0)) INTEGER_CST@1)
1862 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1863 (bit_xor (convert @0) (bit_not @1))))
1865 /* Fold (X & Y) ^ Y and (X ^ Y) & Y as ~X & Y. */
1866 (for opo (bit_and bit_xor)
1867 opi (bit_xor bit_and)
1869 (opo:c (opi:cs @0 @1) @1)
1870 (bit_and (bit_not @0) @1)))
1872 /* Given a bit-wise operation CODE applied to ARG0 and ARG1, see if both
1873 operands are another bit-wise operation with a common input. If so,
1874 distribute the bit operations to save an operation and possibly two if
1875 constants are involved. For example, convert
1876 (A | B) & (A | C) into A | (B & C)
1877 Further simplification will occur if B and C are constants. */
1878 (for op (bit_and bit_ior bit_xor)
1879 rop (bit_ior bit_and bit_and)
1881 (op (convert? (rop:c @@0 @1)) (convert? (rop:c @0 @2)))
1882 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1883 && tree_nop_conversion_p (type, TREE_TYPE (@2)))
1884 (rop (convert @0) (op (convert @1) (convert @2))))))
1886 /* Some simple reassociation for bit operations, also handled in reassoc. */
1887 /* (X & Y) & Y -> X & Y
1888 (X | Y) | Y -> X | Y */
1889 (for op (bit_and bit_ior)
1891 (op:c (convert1?@2 (op:c @0 @@1)) (convert2? @1))
1893 /* (X ^ Y) ^ Y -> X */
1895 (bit_xor:c (convert1? (bit_xor:c @0 @@1)) (convert2? @1))
1898 /* (X & ~Y) & Y -> 0 */
1900 (bit_and:c (bit_and @0 @1) @2)
1901 (with { bool wascmp; }
1902 (if (bitwise_inverted_equal_p (@0, @2, wascmp)
1903 || bitwise_inverted_equal_p (@1, @2, wascmp))
1904 { wascmp ? constant_boolean_node (false, type) : build_zero_cst (type); })))
1905 /* (X | ~Y) | Y -> -1 */
1907 (bit_ior:c (bit_ior @0 @1) @2)
1908 (with { bool wascmp; }
1909 (if ((bitwise_inverted_equal_p (@0, @2, wascmp)
1910 || bitwise_inverted_equal_p (@1, @2, wascmp))
1911 && (!wascmp || element_precision (type) == 1))
1912 { build_all_ones_cst (TREE_TYPE (@0)); })))
1914 /* (X & Y) & (X & Z) -> (X & Y) & Z
1915 (X | Y) | (X | Z) -> (X | Y) | Z */
1916 (for op (bit_and bit_ior)
1918 (op (convert1?@3 (op:c@4 @0 @1)) (convert2?@5 (op:c@6 @0 @2)))
1919 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1920 && tree_nop_conversion_p (type, TREE_TYPE (@2)))
1921 (if (single_use (@5) && single_use (@6))
1922 (op @3 (convert @2))
1923 (if (single_use (@3) && single_use (@4))
1924 (op (convert @1) @5))))))
1925 /* (X ^ Y) ^ (X ^ Z) -> Y ^ Z */
1927 (bit_xor (convert1? (bit_xor:c @0 @1)) (convert2? (bit_xor:c @0 @2)))
1928 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1929 && tree_nop_conversion_p (type, TREE_TYPE (@2)))
1930 (bit_xor (convert @1) (convert @2))))
1932 /* Convert abs (abs (X)) into abs (X).
1933 also absu (absu (X)) into absu (X). */
1939 (absu (convert@2 (absu@1 @0)))
1940 (if (tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@1)))
1943 /* Convert abs[u] (-X) -> abs[u] (X). */
1952 /* Convert abs[u] (X) where X is nonnegative -> (X). */
1954 (abs tree_expr_nonnegative_p@0)
1958 (absu tree_expr_nonnegative_p@0)
1961 /* Simplify (-(X < 0) | 1) * X into abs (X) or absu(X). */
1963 (mult:c (nop_convert1?
1964 (bit_ior (nop_convert2? (negate (convert? (lt @0 integer_zerop))))
1967 (if (INTEGRAL_TYPE_P (type)
1968 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
1969 && !TYPE_UNSIGNED (TREE_TYPE (@0)))
1970 (if (TYPE_UNSIGNED (type))
1977 /* A few cases of fold-const.cc negate_expr_p predicate. */
1978 (match negate_expr_p
1980 (if ((INTEGRAL_TYPE_P (type)
1981 && TYPE_UNSIGNED (type))
1982 || (!TYPE_OVERFLOW_SANITIZED (type)
1983 && may_negate_without_overflow_p (t)))))
1984 (match negate_expr_p
1986 (match negate_expr_p
1988 (if (!TYPE_OVERFLOW_SANITIZED (type))))
1989 (match negate_expr_p
1991 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (t)))))
1992 /* VECTOR_CST handling of non-wrapping types would recurse in unsupported
1994 (match negate_expr_p
1996 (if (FLOAT_TYPE_P (TREE_TYPE (type)) || TYPE_OVERFLOW_WRAPS (type))))
1997 (match negate_expr_p
1999 (if ((ANY_INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_WRAPS (type))
2000 || (FLOAT_TYPE_P (type)
2001 && !HONOR_SIGN_DEPENDENT_ROUNDING (type)
2002 && !HONOR_SIGNED_ZEROS (type)))))
2004 /* (-A) * (-B) -> A * B */
2006 (mult:c (convert1? (negate @0)) (convert2? negate_expr_p@1))
2007 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
2008 && tree_nop_conversion_p (type, TREE_TYPE (@1)))
2009 (mult (convert @0) (convert (negate @1)))))
2011 /* -(A + B) -> (-B) - A. */
2013 (negate (plus:c @0 negate_expr_p@1))
2014 (if (!HONOR_SIGN_DEPENDENT_ROUNDING (type)
2015 && !HONOR_SIGNED_ZEROS (type))
2016 (minus (negate @1) @0)))
2018 /* -(A - B) -> B - A. */
2020 (negate (minus @0 @1))
2021 (if ((ANY_INTEGRAL_TYPE_P (type) && !TYPE_OVERFLOW_SANITIZED (type))
2022 || (FLOAT_TYPE_P (type)
2023 && !HONOR_SIGN_DEPENDENT_ROUNDING (type)
2024 && !HONOR_SIGNED_ZEROS (type)))
2027 (negate (pointer_diff @0 @1))
2028 (if (TYPE_OVERFLOW_UNDEFINED (type))
2029 (pointer_diff @1 @0)))
2031 /* A - B -> A + (-B) if B is easily negatable. */
2033 (minus @0 negate_expr_p@1)
2034 (if (!FIXED_POINT_TYPE_P (type))
2035 (plus @0 (negate @1))))
2037 /* 1 - a is a ^ 1 if a had a bool range. */
2038 /* This is only enabled for gimple as sometimes
2039 cfun is not set for the function which contains
2040 the SSA_NAME (e.g. while IPA passes are happening,
2041 fold might be called). */
2043 (minus integer_onep@0 SSA_NAME@1)
2044 (if (INTEGRAL_TYPE_P (type)
2045 && ssa_name_has_boolean_range (@1))
2048 /* Other simplifications of negation (c.f. fold_negate_expr_1). */
2050 (negate (mult:c@0 @1 negate_expr_p@2))
2051 (if (! TYPE_UNSIGNED (type)
2052 && ! HONOR_SIGN_DEPENDENT_ROUNDING (type)
2054 (mult @1 (negate @2))))
2057 (negate (rdiv@0 @1 negate_expr_p@2))
2058 (if (! HONOR_SIGN_DEPENDENT_ROUNDING (type)
2060 (rdiv @1 (negate @2))))
2063 (negate (rdiv@0 negate_expr_p@1 @2))
2064 (if (! HONOR_SIGN_DEPENDENT_ROUNDING (type)
2066 (rdiv (negate @1) @2)))
2068 /* Fold -((int)x >> (prec - 1)) into (unsigned)x >> (prec - 1). */
2070 (negate (convert? (rshift @0 INTEGER_CST@1)))
2071 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
2072 && wi::to_wide (@1) == element_precision (type) - 1)
2073 (with { tree stype = TREE_TYPE (@0);
2074 tree ntype = TYPE_UNSIGNED (stype) ? signed_type_for (stype)
2075 : unsigned_type_for (stype); }
2076 (if (VECTOR_TYPE_P (type))
2077 (view_convert (rshift (view_convert:ntype @0) @1))
2078 (convert (rshift (convert:ntype @0) @1))))))
2080 /* Try to fold (type) X op CST -> (type) (X op ((type-x) CST))
2082 For bitwise binary operations apply operand conversions to the
2083 binary operation result instead of to the operands. This allows
2084 to combine successive conversions and bitwise binary operations.
2085 We combine the above two cases by using a conditional convert. */
2086 (for bitop (bit_and bit_ior bit_xor)
2088 (bitop (convert@2 @0) (convert?@3 @1))
2089 (if (((TREE_CODE (@1) == INTEGER_CST
2090 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
2091 && (int_fits_type_p (@1, TREE_TYPE (@0))
2092 || tree_nop_conversion_p (TREE_TYPE (@0), type)))
2093 || types_match (@0, @1))
2094 && !POINTER_TYPE_P (TREE_TYPE (@0))
2095 && !VECTOR_TYPE_P (TREE_TYPE (@0))
2096 && TREE_CODE (TREE_TYPE (@0)) != OFFSET_TYPE
2097 /* ??? This transform conflicts with fold-const.cc doing
2098 Convert (T)(x & c) into (T)x & (T)c, if c is an integer
2099 constants (if x has signed type, the sign bit cannot be set
2100 in c). This folds extension into the BIT_AND_EXPR.
2101 Restrict it to GIMPLE to avoid endless recursions. */
2102 && (bitop != BIT_AND_EXPR || GIMPLE)
2103 && (/* That's a good idea if the conversion widens the operand, thus
2104 after hoisting the conversion the operation will be narrower.
2105 It is also a good if the conversion is a nop as moves the
2106 conversion to one side; allowing for combining of the conversions. */
2107 TYPE_PRECISION (TREE_TYPE (@0)) < TYPE_PRECISION (type)
2108 /* The conversion check for being a nop can only be done at the gimple
2109 level as fold_binary has some re-association code which can conflict
2110 with this if there is a "constant" which is not a full INTEGER_CST. */
2111 || (GIMPLE && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (type))
2112 /* It's also a good idea if the conversion is to a non-integer
2114 || GET_MODE_CLASS (TYPE_MODE (type)) != MODE_INT
2115 /* Or if the precision of TO is not the same as the precision
2117 || !type_has_mode_precision_p (type)
2118 /* In GIMPLE, getting rid of 2 conversions for one new results
2121 && TREE_CODE (@1) != INTEGER_CST
2122 && tree_nop_conversion_p (type, TREE_TYPE (@0))
2124 && single_use (@3))))
2125 (convert (bitop @0 (convert @1)))))
2126 /* In GIMPLE, getting rid of 2 conversions for one new results
2129 (convert (bitop:cs@2 (nop_convert:s @0) @1))
2131 && TREE_CODE (@1) != INTEGER_CST
2132 && tree_nop_conversion_p (type, TREE_TYPE (@2))
2133 && types_match (type, @0)
2134 && !POINTER_TYPE_P (TREE_TYPE (@0))
2135 && TREE_CODE (TREE_TYPE (@0)) != OFFSET_TYPE)
2136 (bitop @0 (convert @1)))))
2138 (for bitop (bit_and bit_ior)
2139 rbitop (bit_ior bit_and)
2140 /* (x | y) & x -> x */
2141 /* (x & y) | x -> x */
2143 (bitop:c (rbitop:c @0 @1) @0)
2145 /* (~x | y) & x -> x & y */
2146 /* (~x & y) | x -> x | y */
2148 (bitop:c (rbitop:c @2 @1) @0)
2149 (with { bool wascmp; }
2150 (if (bitwise_inverted_equal_p (@0, @2, wascmp)
2151 && (!wascmp || element_precision (type) == 1))
2153 /* (x | y) & (x & z) -> (x & z) */
2154 /* (x & y) | (x | z) -> (x | z) */
2156 (bitop:c (rbitop:c @0 @1) (bitop:c@3 @0 @2))
2158 /* (x | c) & ~(y | c) -> x & ~(y | c) */
2159 /* (x & c) | ~(y & c) -> x | ~(y & c) */
2161 (bitop:c (rbitop:c @0 @1) (bit_not@3 (rbitop:c @1 @2)))
2163 /* x & ~(y | x) -> 0 */
2164 /* x | ~(y & x) -> -1 */
2166 (bitop:c @0 (bit_not (rbitop:c @0 @1)))
2167 (if (bitop == BIT_AND_EXPR)
2168 { build_zero_cst (type); }
2169 { build_minus_one_cst (type); })))
2171 /* ((x | y) & z) | x -> (z & y) | x
2172 ((x ^ y) & z) | x -> (z & y) | x */
2173 (for op (bit_ior bit_xor)
2175 (bit_ior:c (nop_convert1?:s
2176 (bit_and:cs (nop_convert2?:s (op:cs @0 @1)) @2)) @3)
2177 (if (bitwise_equal_p (@0, @3))
2178 (convert (bit_ior (bit_and @1 (convert @2)) (convert @0))))))
2180 /* (x | CST1) & CST2 -> (x & CST2) | (CST1 & CST2) */
2182 (bit_and (bit_ior @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
2183 (bit_ior (bit_and @0 @2) (bit_and! @1 @2)))
2185 /* Combine successive equal operations with constants. */
2186 (for bitop (bit_and bit_ior bit_xor)
2188 (bitop (bitop @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
2189 (if (!CONSTANT_CLASS_P (@0))
2190 /* This is the canonical form regardless of whether (bitop @1 @2) can be
2191 folded to a constant. */
2192 (bitop @0 (bitop! @1 @2))
2193 /* In this case we have three constants and (bitop @0 @1) doesn't fold
2194 to a constant. This can happen if @0 or @1 is a POLY_INT_CST and if
2195 the values involved are such that the operation can't be decided at
2196 compile time. Try folding one of @0 or @1 with @2 to see whether
2197 that combination can be decided at compile time.
2199 Keep the existing form if both folds fail, to avoid endless
2201 (with { tree cst1 = const_binop (bitop, type, @0, @2); }
2203 (bitop @1 { cst1; })
2204 (with { tree cst2 = const_binop (bitop, type, @1, @2); }
2206 (bitop @0 { cst2; }))))))))
2208 /* Try simple folding for X op !X, and X op X with the help
2209 of the truth_valued_p and logical_inverted_value predicates. */
2210 (match truth_valued_p
2212 (if (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1)))
2213 (for op (tcc_comparison truth_and truth_andif truth_or truth_orif truth_xor)
2214 (match truth_valued_p
2216 (match truth_valued_p
2219 (match (logical_inverted_value @0)
2221 (match (logical_inverted_value @0)
2222 (bit_not truth_valued_p@0))
2223 (match (logical_inverted_value @0)
2224 (eq @0 integer_zerop))
2225 (match (logical_inverted_value @0)
2226 (ne truth_valued_p@0 integer_truep))
2227 (match (logical_inverted_value @0)
2228 (bit_xor truth_valued_p@0 integer_truep))
2232 (bit_and:c @0 (logical_inverted_value @0))
2233 { build_zero_cst (type); })
2234 /* X | !X and X ^ !X -> 1, , if X is truth-valued. */
2235 (for op (bit_ior bit_xor)
2237 (op:c truth_valued_p@0 (logical_inverted_value @0))
2238 { constant_boolean_node (true, type); }))
2239 /* X ==/!= !X is false/true. */
2242 (op:c truth_valued_p@0 (logical_inverted_value @0))
2243 { constant_boolean_node (op == NE_EXPR ? true : false, type); }))
2247 (bit_not (bit_not @0))
2250 /* zero_one_valued_p will match when a value is known to be either
2251 0 or 1 including constants 0 or 1.
2252 Signed 1-bits includes -1 so they cannot match here. */
2253 (match zero_one_valued_p
2255 (if (INTEGRAL_TYPE_P (type)
2256 && (TYPE_UNSIGNED (type)
2257 || TYPE_PRECISION (type) > 1)
2258 && wi::leu_p (tree_nonzero_bits (@0), 1))))
2259 (match zero_one_valued_p
2261 (if (INTEGRAL_TYPE_P (type)
2262 && (TYPE_UNSIGNED (type)
2263 || TYPE_PRECISION (type) > 1))))
2265 /* (a&1) is always [0,1] too. This is useful again when
2266 the range is not known. */
2267 /* Note this can't be recursive due to VN handling of equivalents,
2268 VN and would cause an infinite recursion. */
2269 (match zero_one_valued_p
2270 (bit_and:c@0 @1 integer_onep)
2271 (if (INTEGRAL_TYPE_P (type))))
2273 /* A conversion from an zero_one_valued_p is still a [0,1].
2274 This is useful when the range of a variable is not known */
2275 /* Note this matches can't be recursive because of the way VN handles
2276 nop conversions being equivalent and then recursive between them. */
2277 (match zero_one_valued_p
2279 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2280 && (TYPE_UNSIGNED (TREE_TYPE (@1))
2281 || TYPE_PRECISION (TREE_TYPE (@1)) > 1)
2282 && INTEGRAL_TYPE_P (type)
2283 && (TYPE_UNSIGNED (type)
2284 || TYPE_PRECISION (type) > 1)
2285 && wi::leu_p (tree_nonzero_bits (@1), 1))))
2287 /* Transform { 0 or 1 } * { 0 or 1 } into { 0 or 1 } & { 0 or 1 }. */
2289 (mult zero_one_valued_p@0 zero_one_valued_p@1)
2290 (if (INTEGRAL_TYPE_P (type))
2293 (for cmp (tcc_comparison)
2294 icmp (inverted_tcc_comparison)
2295 /* Fold (((a < b) & c) | ((a >= b) & d)) into (a < b ? c : d) & 1. */
2298 (bit_and:c (convert? (cmp@0 @01 @02)) @3)
2299 (bit_and:c (convert? (icmp@4 @01 @02)) @5))
2300 (if (INTEGRAL_TYPE_P (type)
2301 && invert_tree_comparison (cmp, HONOR_NANS (@01)) == icmp
2302 /* The scalar version has to be canonicalized after vectorization
2303 because it makes unconditional loads conditional ones, which
2304 means we lose vectorization because the loads may trap. */
2305 && canonicalize_math_after_vectorization_p ())
2306 (bit_and (cond @0 @3 @5) { build_one_cst (type); })))
2308 /* Fold ((-(a < b) & c) | (-(a >= b) & d)) into a < b ? c : d. This is
2309 canonicalized further and we recognize the conditional form:
2310 (a < b ? c : 0) | (a >= b ? d : 0) into a < b ? c : d. */
2313 (cond (cmp@0 @01 @02) @3 zerop)
2314 (cond (icmp@4 @01 @02) @5 zerop))
2315 (if (INTEGRAL_TYPE_P (type)
2316 && invert_tree_comparison (cmp, HONOR_NANS (@01)) == icmp
2317 /* The scalar version has to be canonicalized after vectorization
2318 because it makes unconditional loads conditional ones, which
2319 means we lose vectorization because the loads may trap. */
2320 && canonicalize_math_after_vectorization_p ())
2323 /* Vector Fold (((a < b) & c) | ((a >= b) & d)) into a < b ? c : d.
2324 and ((~(a < b) & c) | (~(a >= b) & d)) into a < b ? c : d. */
2327 (bit_and:c (vec_cond:s (cmp@0 @6 @7) @4 @5) @2)
2328 (bit_and:c (vec_cond:s (icmp@1 @6 @7) @4 @5) @3))
2329 (if (integer_zerop (@5)
2330 && invert_tree_comparison (cmp, HONOR_NANS (@6)) == icmp)
2332 (if (integer_onep (@4))
2333 (bit_and (vec_cond @0 @2 @3) @4))
2334 (if (integer_minus_onep (@4))
2335 (vec_cond @0 @2 @3)))
2336 (if (integer_zerop (@4)
2337 && invert_tree_comparison (cmp, HONOR_NANS (@6)) == icmp)
2339 (if (integer_onep (@5))
2340 (bit_and (vec_cond @0 @3 @2) @5))
2341 (if (integer_minus_onep (@5))
2342 (vec_cond @0 @3 @2))))))
2344 /* Scalar Vectorized Fold ((-(a < b) & c) | (-(a >= b) & d))
2345 into a < b ? d : c. */
2348 (vec_cond:s (cmp@0 @4 @5) @2 integer_zerop)
2349 (vec_cond:s (icmp@1 @4 @5) @3 integer_zerop))
2350 (if (invert_tree_comparison (cmp, HONOR_NANS (@4)) == icmp)
2351 (vec_cond @0 @2 @3))))
2353 /* Transform X & -Y into X * Y when Y is { 0 or 1 }. */
2355 (bit_and:c (convert? (negate zero_one_valued_p@0)) @1)
2356 (if (INTEGRAL_TYPE_P (type)
2357 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
2358 && TREE_CODE (TREE_TYPE (@0)) != BOOLEAN_TYPE
2359 /* Sign extending of the neg or a truncation of the neg
2361 && (!TYPE_UNSIGNED (TREE_TYPE (@0))
2362 || TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))))
2363 (mult (convert @0) @1)))
2365 /* Narrow integer multiplication by a zero_one_valued_p operand.
2366 Multiplication by [0,1] is guaranteed not to overflow. */
2368 (convert (mult@0 zero_one_valued_p@1 INTEGER_CST@2))
2369 (if (INTEGRAL_TYPE_P (type)
2370 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
2371 && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (@0)))
2372 (mult (convert @1) (convert @2))))
2374 /* (X << C) != 0 can be simplified to X, when C is zero_one_valued_p.
2375 Check that the shift is well-defined (C is less than TYPE_PRECISION)
2376 as some targets (such as x86's SSE) may return zero for larger C. */
2378 (ne (lshift zero_one_valued_p@0 INTEGER_CST@1) integer_zerop@2)
2379 (if (tree_fits_shwi_p (@1)
2380 && tree_to_shwi (@1) > 0
2381 && tree_to_shwi (@1) < TYPE_PRECISION (TREE_TYPE (@0)))
2384 /* (X << C) == 0 can be simplified to X == 0, when C is zero_one_valued_p.
2385 Check that the shift is well-defined (C is less than TYPE_PRECISION)
2386 as some targets (such as x86's SSE) may return zero for larger C. */
2388 (eq (lshift zero_one_valued_p@0 INTEGER_CST@1) integer_zerop@2)
2389 (if (tree_fits_shwi_p (@1)
2390 && tree_to_shwi (@1) > 0
2391 && tree_to_shwi (@1) < TYPE_PRECISION (TREE_TYPE (@0)))
2394 /* Convert ~ (-A) to A - 1. */
2396 (bit_not (convert? (negate @0)))
2397 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
2398 || !TYPE_UNSIGNED (TREE_TYPE (@0)))
2399 (convert (minus @0 { build_each_one_cst (TREE_TYPE (@0)); }))))
2401 /* Convert - (~A) to A + 1. */
2403 (negate (nop_convert? (bit_not @0)))
2404 (plus (view_convert @0) { build_each_one_cst (type); }))
2406 /* (a & b) ^ (a == b) -> !(a | b) */
2407 /* (a & b) == (a ^ b) -> !(a | b) */
2408 (for first_op (bit_xor eq)
2409 second_op (eq bit_xor)
2411 (first_op:c (bit_and:c truth_valued_p@0 truth_valued_p@1) (second_op:c @0 @1))
2412 (bit_not (bit_ior @0 @1))))
2414 /* Convert ~ (A - 1) or ~ (A + -1) to -A. */
2416 (bit_not (convert? (minus @0 integer_each_onep)))
2417 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
2418 || !TYPE_UNSIGNED (TREE_TYPE (@0)))
2419 (convert (negate @0))))
2421 (bit_not (convert? (plus @0 integer_all_onesp)))
2422 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
2423 || !TYPE_UNSIGNED (TREE_TYPE (@0)))
2424 (convert (negate @0))))
2426 /* Part of convert ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify. */
2428 (bit_not (convert? (bit_xor @0 INTEGER_CST@1)))
2429 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2430 (convert (bit_xor @0 (bit_not @1)))))
2432 (bit_not (convert? (bit_xor:c (bit_not @0) @1)))
2433 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2434 (convert (bit_xor @0 @1))))
2436 /* Otherwise prefer ~(X ^ Y) to ~X ^ Y as more canonical. */
2438 (bit_xor:c (nop_convert?:s (bit_not:s @0)) @1)
2439 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2440 (bit_not (bit_xor (view_convert @0) @1))))
2442 /* ~(a ^ b) is a == b for truth valued a and b. */
2444 (bit_not (bit_xor:s truth_valued_p@0 truth_valued_p@1))
2445 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2446 && TYPE_PRECISION (TREE_TYPE (@0)) == 1)
2447 (convert (eq @0 @1))))
2449 /* (~a) == b is a ^ b for truth valued a and b. */
2451 (eq:c (bit_not:s truth_valued_p@0) truth_valued_p@1)
2452 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2453 && TYPE_PRECISION (TREE_TYPE (@0)) == 1)
2454 (convert (bit_xor @0 @1))))
2456 /* (x & ~m) | (y & m) -> ((x ^ y) & m) ^ x */
2458 (bit_ior:c (bit_and:cs @0 (bit_not @2)) (bit_and:cs @1 @2))
2459 (bit_xor (bit_and (bit_xor @0 @1) @2) @0))
2461 /* Fold A - (A & B) into ~B & A. */
2463 (minus (convert1? @0) (convert2?:s (bit_and:cs @@0 @1)))
2464 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
2465 && tree_nop_conversion_p (type, TREE_TYPE (@1)))
2466 (convert (bit_and (bit_not @1) @0))))
2468 /* (m1 CMP m2) * d -> (m1 CMP m2) ? d : 0 */
2469 (if (!canonicalize_math_p ())
2470 (for cmp (tcc_comparison)
2472 (mult:c (convert (cmp@0 @1 @2)) @3)
2473 (if (INTEGRAL_TYPE_P (type)
2474 && INTEGRAL_TYPE_P (TREE_TYPE (@0)))
2475 (cond @0 @3 { build_zero_cst (type); })))
2476 /* (-(m1 CMP m2)) & d -> (m1 CMP m2) ? d : 0 */
2478 (bit_and:c (negate (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); })))
2485 /* For integral types with undefined overflow and C != 0 fold
2486 x * C EQ/NE y * C into x EQ/NE y. */
2489 (cmp (mult:c @0 @1) (mult:c @2 @1))
2490 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2491 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2492 && tree_expr_nonzero_p (@1))
2495 /* For integral types with wrapping overflow and C odd fold
2496 x * C EQ/NE y * C into x EQ/NE y. */
2499 (cmp (mult @0 INTEGER_CST@1) (mult @2 @1))
2500 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2501 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))
2502 && (TREE_INT_CST_LOW (@1) & 1) != 0)
2505 /* For integral types with undefined overflow and C != 0 fold
2506 x * C RELOP y * C into:
2508 x RELOP y for nonnegative C
2509 y RELOP x for negative C */
2510 (for cmp (lt gt le ge)
2512 (cmp (mult:c @0 @1) (mult:c @2 @1))
2513 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2514 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
2515 (if (tree_expr_nonnegative_p (@1) && tree_expr_nonzero_p (@1))
2517 (if (TREE_CODE (@1) == INTEGER_CST
2518 && wi::neg_p (wi::to_wide (@1), TYPE_SIGN (TREE_TYPE (@1))))
2521 /* (X - 1U) <= INT_MAX-1U into (int) X > 0. */
2525 (cmp (plus @0 integer_minus_onep@1) INTEGER_CST@2)
2526 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2527 && TYPE_UNSIGNED (TREE_TYPE (@0))
2528 && TYPE_PRECISION (TREE_TYPE (@0)) > 1
2529 && (wi::to_wide (@2)
2530 == wi::max_value (TYPE_PRECISION (TREE_TYPE (@0)), SIGNED) - 1))
2531 (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
2532 (icmp (convert:stype @0) { build_int_cst (stype, 0); })))))
2534 /* X / 4 < Y / 4 iff X < Y when the division is known to be exact. */
2535 (for cmp (simple_comparison)
2537 (cmp (convert?@3 (exact_div @0 INTEGER_CST@2)) (convert? (exact_div @1 @2)))
2538 (if (element_precision (@3) >= element_precision (@0)
2539 && types_match (@0, @1))
2540 (if (wi::lt_p (wi::to_wide (@2), 0, TYPE_SIGN (TREE_TYPE (@2))))
2541 (if (!TYPE_UNSIGNED (TREE_TYPE (@3)))
2543 (if (tree_expr_nonzero_p (@0) && tree_expr_nonzero_p (@1))
2546 tree utype = unsigned_type_for (TREE_TYPE (@0));
2548 (cmp (convert:utype @1) (convert:utype @0)))))
2549 (if (wi::gt_p (wi::to_wide (@2), 1, TYPE_SIGN (TREE_TYPE (@2))))
2550 (if (TYPE_UNSIGNED (TREE_TYPE (@0)) || !TYPE_UNSIGNED (TREE_TYPE (@3)))
2554 tree utype = unsigned_type_for (TREE_TYPE (@0));
2556 (cmp (convert:utype @0) (convert:utype @1)))))))))
2558 /* X / C1 op C2 into a simple range test. */
2559 (for cmp (simple_comparison)
2561 (cmp (trunc_div:s @0 INTEGER_CST@1) INTEGER_CST@2)
2562 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2563 && integer_nonzerop (@1)
2564 && !TREE_OVERFLOW (@1)
2565 && !TREE_OVERFLOW (@2))
2566 (with { tree lo, hi; bool neg_overflow;
2567 enum tree_code code = fold_div_compare (cmp, @1, @2, &lo, &hi,
2570 (if (code == LT_EXPR || code == GE_EXPR)
2571 (if (TREE_OVERFLOW (lo))
2572 { build_int_cst (type, (code == LT_EXPR) ^ neg_overflow); }
2573 (if (code == LT_EXPR)
2576 (if (code == LE_EXPR || code == GT_EXPR)
2577 (if (TREE_OVERFLOW (hi))
2578 { build_int_cst (type, (code == LE_EXPR) ^ neg_overflow); }
2579 (if (code == LE_EXPR)
2583 { build_int_cst (type, code == NE_EXPR); })
2584 (if (code == EQ_EXPR && !hi)
2586 (if (code == EQ_EXPR && !lo)
2588 (if (code == NE_EXPR && !hi)
2590 (if (code == NE_EXPR && !lo)
2593 { build_range_check (UNKNOWN_LOCATION, type, @0, code == EQ_EXPR,
2597 tree etype = range_check_type (TREE_TYPE (@0));
2600 hi = fold_convert (etype, hi);
2601 lo = fold_convert (etype, lo);
2602 hi = const_binop (MINUS_EXPR, etype, hi, lo);
2605 (if (etype && hi && !TREE_OVERFLOW (hi))
2606 (if (code == EQ_EXPR)
2607 (le (minus (convert:etype @0) { lo; }) { hi; })
2608 (gt (minus (convert:etype @0) { lo; }) { hi; })))))))))
2610 /* X + Z < Y + Z is the same as X < Y when there is no overflow. */
2611 (for op (lt le ge gt)
2613 (op (plus:c @0 @2) (plus:c @1 @2))
2614 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2615 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
2618 /* As a special case, X + C < Y + C is the same as (signed) X < (signed) Y
2619 when C is an unsigned integer constant with only the MSB set, and X and
2620 Y have types of equal or lower integer conversion rank than C's. */
2621 (for op (lt le ge gt)
2623 (op (plus @1 INTEGER_CST@0) (plus @2 @0))
2624 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2625 && TYPE_UNSIGNED (TREE_TYPE (@0))
2626 && wi::only_sign_bit_p (wi::to_wide (@0)))
2627 (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
2628 (op (convert:stype @1) (convert:stype @2))))))
2630 /* For equality and subtraction, this is also true with wrapping overflow. */
2631 (for op (eq ne minus)
2633 (op (plus:c @0 @2) (plus:c @1 @2))
2634 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2635 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2636 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
2638 /* And similar for pointers. */
2641 (op (pointer_plus @0 @1) (pointer_plus @0 @2))
2644 (pointer_diff (pointer_plus @0 @1) (pointer_plus @0 @2))
2645 (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
2646 (convert (minus @1 @2))))
2648 /* X - Z < Y - Z is the same as X < Y when there is no overflow. */
2649 (for op (lt le ge gt)
2651 (op (minus @0 @2) (minus @1 @2))
2652 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2653 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
2655 /* For equality and subtraction, this is also true with wrapping overflow. */
2656 (for op (eq ne minus)
2658 (op (minus @0 @2) (minus @1 @2))
2659 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2660 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2661 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
2663 /* And for pointers... */
2664 (for op (simple_comparison)
2666 (op (pointer_diff@3 @0 @2) (pointer_diff @1 @2))
2667 (if (!TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
2670 (minus (pointer_diff@3 @0 @2) (pointer_diff @1 @2))
2671 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@3))
2672 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
2673 (pointer_diff @0 @1)))
2675 /* Z - X < Z - Y is the same as Y < X when there is no overflow. */
2676 (for op (lt le ge gt)
2678 (op (minus @2 @0) (minus @2 @1))
2679 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2680 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
2682 /* For equality and subtraction, this is also true with wrapping overflow. */
2683 (for op (eq ne minus)
2685 (op (minus @2 @0) (minus @2 @1))
2686 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2687 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2688 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
2690 /* And for pointers... */
2691 (for op (simple_comparison)
2693 (op (pointer_diff@3 @2 @0) (pointer_diff @2 @1))
2694 (if (!TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
2697 (minus (pointer_diff@3 @2 @0) (pointer_diff @2 @1))
2698 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@3))
2699 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
2700 (pointer_diff @1 @0)))
2702 /* X + Y < Y is the same as X < 0 when there is no overflow. */
2703 (for op (lt le gt ge)
2705 (op:c (plus:c@2 @0 @1) @1)
2706 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2707 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2708 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0))
2709 && (CONSTANT_CLASS_P (@0) || single_use (@2)))
2710 (op @0 { build_zero_cst (TREE_TYPE (@0)); }))))
2711 /* For equality, this is also true with wrapping overflow. */
2714 (op:c (nop_convert?@3 (plus:c@2 @0 (convert1? @1))) (convert2? @1))
2715 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2716 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2717 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
2718 && (CONSTANT_CLASS_P (@0) || (single_use (@2) && single_use (@3)))
2719 && tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@2))
2720 && tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@1)))
2721 (op @0 { build_zero_cst (TREE_TYPE (@0)); })))
2723 (op:c (nop_convert?@3 (pointer_plus@2 (convert1? @0) @1)) (convert2? @0))
2724 (if (tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@0))
2725 && tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0))
2726 && (CONSTANT_CLASS_P (@1) || (single_use (@2) && single_use (@3))))
2727 (op @1 { build_zero_cst (TREE_TYPE (@1)); }))))
2729 /* (&a + b) !=/== (&a[1] + c) -> (&a[0] - &a[1]) + b !=/== c */
2732 (neeq:c ADDR_EXPR@0 (pointer_plus @2 @3))
2733 (with { poly_int64 diff; tree inner_type = TREE_TYPE (@3);}
2734 (if (ptr_difference_const (@0, @2, &diff))
2735 (neeq { build_int_cst_type (inner_type, diff); } @3))))
2737 (neeq (pointer_plus ADDR_EXPR@0 @1) (pointer_plus ADDR_EXPR@2 @3))
2738 (with { poly_int64 diff; tree inner_type = TREE_TYPE (@1);}
2739 (if (ptr_difference_const (@0, @2, &diff))
2740 (neeq (plus { build_int_cst_type (inner_type, diff); } @1) @3)))))
2742 /* X - Y < X is the same as Y > 0 when there is no overflow.
2743 For equality, this is also true with wrapping overflow. */
2744 (for op (simple_comparison)
2746 (op:c @0 (minus@2 @0 @1))
2747 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2748 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2749 || ((op == EQ_EXPR || op == NE_EXPR)
2750 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
2751 && (CONSTANT_CLASS_P (@1) || single_use (@2)))
2752 (op @1 { build_zero_cst (TREE_TYPE (@1)); }))))
2755 (X / Y) == 0 -> X < Y if X, Y are unsigned.
2756 (X / Y) != 0 -> X >= Y, if X, Y are unsigned. */
2760 (cmp (trunc_div @0 @1) integer_zerop)
2761 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
2762 /* Complex ==/!= is allowed, but not </>=. */
2763 && TREE_CODE (TREE_TYPE (@0)) != COMPLEX_TYPE
2764 && (VECTOR_TYPE_P (type) || !VECTOR_TYPE_P (TREE_TYPE (@0))))
2767 /* X == C - X can never be true if C is odd. */
2770 (cmp:c (convert? @0) (convert1? (minus INTEGER_CST@1 (convert2? @0))))
2771 (if (TREE_INT_CST_LOW (@1) & 1)
2772 { constant_boolean_node (cmp == NE_EXPR, type); })))
2777 U needs to be non-negative.
2781 U and N needs to be non-negative
2785 U needs to be non-negative and N needs to be a negative constant.
2787 (for cmp (lt ge le gt )
2788 bitop (bit_ior bit_ior bit_and bit_and)
2790 (cmp:c (bitop:c tree_expr_nonnegative_p@0 @1) @0)
2791 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)))
2792 (if (bitop == BIT_AND_EXPR || tree_expr_nonnegative_p (@1))
2793 { constant_boolean_node (cmp == GE_EXPR || cmp == LE_EXPR, type); }
2794 /* The sign is opposite now so the comparison is swapped around. */
2795 (if (TREE_CODE (@1) == INTEGER_CST && wi::neg_p (wi::to_wide (@1)))
2796 { constant_boolean_node (cmp == LT_EXPR, type); })))))
2798 /* Arguments on which one can call get_nonzero_bits to get the bits
2800 (match with_possible_nonzero_bits
2802 (match with_possible_nonzero_bits
2804 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))))
2805 /* Slightly extended version, do not make it recursive to keep it cheap. */
2806 (match (with_possible_nonzero_bits2 @0)
2807 with_possible_nonzero_bits@0)
2808 (match (with_possible_nonzero_bits2 @0)
2809 (bit_and:c with_possible_nonzero_bits@0 @2))
2811 /* Same for bits that are known to be set, but we do not have
2812 an equivalent to get_nonzero_bits yet. */
2813 (match (with_certain_nonzero_bits2 @0)
2815 (match (with_certain_nonzero_bits2 @0)
2816 (bit_ior @1 INTEGER_CST@0))
2818 /* X == C (or X & Z == Y | C) is impossible if ~nonzero(X) & C != 0. */
2821 (cmp:c (with_possible_nonzero_bits2 @0) (with_certain_nonzero_bits2 @1))
2822 (if (wi::bit_and_not (wi::to_wide (@1), get_nonzero_bits (@0)) != 0)
2823 { constant_boolean_node (cmp == NE_EXPR, type); })))
2825 /* ((X inner_op C0) outer_op C1)
2826 With X being a tree where value_range has reasoned certain bits to always be
2827 zero throughout its computed value range,
2828 inner_op = {|,^}, outer_op = {|,^} and inner_op != outer_op
2829 where zero_mask has 1's for all bits that are sure to be 0 in
2831 if (inner_op == '^') C0 &= ~C1;
2832 if ((C0 & ~zero_mask) == 0) then emit (X outer_op (C0 outer_op C1)
2833 if ((C1 & ~zero_mask) == 0) then emit (X inner_op (C0 outer_op C1)
2835 (for inner_op (bit_ior bit_xor)
2836 outer_op (bit_xor bit_ior)
2839 (inner_op:s @2 INTEGER_CST@0) INTEGER_CST@1)
2843 wide_int zero_mask_not;
2847 if (TREE_CODE (@2) == SSA_NAME)
2848 zero_mask_not = get_nonzero_bits (@2);
2852 if (inner_op == BIT_XOR_EXPR)
2854 C0 = wi::bit_and_not (wi::to_wide (@0), wi::to_wide (@1));
2855 cst_emit = C0 | wi::to_wide (@1);
2859 C0 = wi::to_wide (@0);
2860 cst_emit = C0 ^ wi::to_wide (@1);
2863 (if (!fail && (C0 & zero_mask_not) == 0)
2864 (outer_op @2 { wide_int_to_tree (type, cst_emit); })
2865 (if (!fail && (wi::to_wide (@1) & zero_mask_not) == 0)
2866 (inner_op @2 { wide_int_to_tree (type, cst_emit); }))))))
2868 /* Associate (p +p off1) +p off2 as (p +p (off1 + off2)). */
2870 (pointer_plus (pointer_plus:s @0 @1) @3)
2871 (pointer_plus @0 (plus @1 @3)))
2874 (pointer_plus (convert:s (pointer_plus:s @0 @1)) @3)
2875 (convert:type (pointer_plus @0 (plus @1 @3))))
2882 tem4 = (unsigned long) tem3;
2887 (pointer_plus @0 (convert?@2 (minus@3 (convert @1) (convert @0))))
2888 /* Conditionally look through a sign-changing conversion. */
2889 (if (TYPE_PRECISION (TREE_TYPE (@2)) == TYPE_PRECISION (TREE_TYPE (@3))
2890 && ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@1)))
2891 || (GENERIC && type == TREE_TYPE (@1))))
2894 (pointer_plus @0 (convert?@2 (pointer_diff@3 @1 @@0)))
2895 (if (TYPE_PRECISION (TREE_TYPE (@2)) >= TYPE_PRECISION (TREE_TYPE (@3)))
2899 tem = (sizetype) ptr;
2903 and produce the simpler and easier to analyze with respect to alignment
2904 ... = ptr & ~algn; */
2906 (pointer_plus @0 (negate (bit_and (convert @0) INTEGER_CST@1)))
2907 (with { tree algn = wide_int_to_tree (TREE_TYPE (@0), ~wi::to_wide (@1)); }
2908 (bit_and @0 { algn; })))
2910 /* Try folding difference of addresses. */
2912 (minus (convert ADDR_EXPR@0) (convert (pointer_plus @1 @2)))
2913 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2914 (with { poly_int64 diff; }
2915 (if (ptr_difference_const (@0, @1, &diff))
2916 (minus { build_int_cst_type (type, diff); } (convert @2))))))
2918 (minus (convert (pointer_plus @0 @2)) (convert ADDR_EXPR@1))
2919 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2920 (with { poly_int64 diff; }
2921 (if (ptr_difference_const (@0, @1, &diff))
2922 (plus (convert @2) { build_int_cst_type (type, diff); })))))
2924 (minus (convert ADDR_EXPR@0) (convert @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 { build_int_cst_type (type, diff); }))))
2930 (minus (convert @0) (convert ADDR_EXPR@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 (pointer_diff (convert?@2 ADDR_EXPR@0) (convert1?@3 @1))
2937 (if (tree_nop_conversion_p (TREE_TYPE(@2), TREE_TYPE (@0))
2938 && tree_nop_conversion_p (TREE_TYPE(@3), TREE_TYPE (@1)))
2939 (with { poly_int64 diff; }
2940 (if (ptr_difference_const (@0, @1, &diff))
2941 { build_int_cst_type (type, diff); }))))
2943 (pointer_diff (convert?@2 @0) (convert1?@3 ADDR_EXPR@1))
2944 (if (tree_nop_conversion_p (TREE_TYPE(@2), TREE_TYPE (@0))
2945 && tree_nop_conversion_p (TREE_TYPE(@3), TREE_TYPE (@1)))
2946 (with { poly_int64 diff; }
2947 (if (ptr_difference_const (@0, @1, &diff))
2948 { build_int_cst_type (type, diff); }))))
2950 /* (&a+b) - (&a[1] + c) -> sizeof(a[0]) + (b - c) */
2952 (pointer_diff (pointer_plus ADDR_EXPR@0 @1) (pointer_plus ADDR_EXPR@2 @3))
2953 (with { poly_int64 diff; }
2954 (if (ptr_difference_const (@0, @2, &diff))
2955 (plus { build_int_cst_type (type, diff); } (convert (minus @1 @3))))))
2956 /* (p + b) - &p->d -> offsetof (*p, d) + b */
2958 (pointer_diff (pointer_plus @0 @1) ADDR_EXPR@2)
2959 (with { poly_int64 diff; }
2960 (if (ptr_difference_const (@0, @2, &diff))
2961 (plus { build_int_cst_type (type, diff); } (convert @1)))))
2963 (pointer_diff ADDR_EXPR@0 (pointer_plus @1 @2))
2964 (with { poly_int64 diff; }
2965 (if (ptr_difference_const (@0, @1, &diff))
2966 (minus { build_int_cst_type (type, diff); } (convert @2)))))
2968 /* Canonicalize (T *)(ptr - ptr-cst) to &MEM[ptr + -ptr-cst]. */
2970 (convert (pointer_diff @0 INTEGER_CST@1))
2971 (if (POINTER_TYPE_P (type))
2972 { build_fold_addr_expr_with_type
2973 (build2 (MEM_REF, char_type_node, @0,
2974 wide_int_to_tree (ptr_type_node, wi::neg (wi::to_wide (@1)))),
2977 /* If arg0 is derived from the address of an object or function, we may
2978 be able to fold this expression using the object or function's
2981 (bit_and (convert? @0) INTEGER_CST@1)
2982 (if (POINTER_TYPE_P (TREE_TYPE (@0))
2983 && tree_nop_conversion_p (type, TREE_TYPE (@0)))
2987 unsigned HOST_WIDE_INT bitpos;
2988 get_pointer_alignment_1 (@0, &align, &bitpos);
2990 (if (wi::ltu_p (wi::to_wide (@1), align / BITS_PER_UNIT))
2991 { wide_int_to_tree (type, (wi::to_wide (@1)
2992 & (bitpos / BITS_PER_UNIT))); }))))
2995 uniform_integer_cst_p
2997 tree int_cst = uniform_integer_cst_p (t);
2998 tree inner_type = TREE_TYPE (int_cst);
3000 (if ((INTEGRAL_TYPE_P (inner_type)
3001 || POINTER_TYPE_P (inner_type))
3002 && wi::eq_p (wi::to_wide (int_cst), wi::min_value (inner_type))))))
3005 uniform_integer_cst_p
3007 tree int_cst = uniform_integer_cst_p (t);
3008 tree itype = TREE_TYPE (int_cst);
3010 (if ((INTEGRAL_TYPE_P (itype)
3011 || POINTER_TYPE_P (itype))
3012 && wi::eq_p (wi::to_wide (int_cst), wi::max_value (itype))))))
3014 /* x > y && x != XXX_MIN --> x > y
3015 x > y && x == XXX_MIN --> false . */
3018 (bit_and:c (gt:c@2 @0 @1) (eqne @0 min_value))
3020 (if (eqne == EQ_EXPR)
3021 { constant_boolean_node (false, type); })
3022 (if (eqne == NE_EXPR)
3026 /* x < y && x != XXX_MAX --> x < y
3027 x < y && x == XXX_MAX --> false. */
3030 (bit_and:c (lt:c@2 @0 @1) (eqne @0 max_value))
3032 (if (eqne == EQ_EXPR)
3033 { constant_boolean_node (false, type); })
3034 (if (eqne == NE_EXPR)
3038 /* x <= y && x == XXX_MIN --> x == XXX_MIN. */
3040 (bit_and:c (le:c @0 @1) (eq@2 @0 min_value))
3043 /* x >= y && x == XXX_MAX --> x == XXX_MAX. */
3045 (bit_and:c (ge:c @0 @1) (eq@2 @0 max_value))
3048 /* x > y || x != XXX_MIN --> x != XXX_MIN. */
3050 (bit_ior:c (gt:c @0 @1) (ne@2 @0 min_value))
3053 /* x <= y || x != XXX_MIN --> true. */
3055 (bit_ior:c (le:c @0 @1) (ne @0 min_value))
3056 { constant_boolean_node (true, type); })
3058 /* x <= y || x == XXX_MIN --> x <= y. */
3060 (bit_ior:c (le:c@2 @0 @1) (eq @0 min_value))
3063 /* x < y || x != XXX_MAX --> x != XXX_MAX. */
3065 (bit_ior:c (lt:c @0 @1) (ne@2 @0 max_value))
3068 /* x >= y || x != XXX_MAX --> true
3069 x >= y || x == XXX_MAX --> x >= y. */
3072 (bit_ior:c (ge:c@2 @0 @1) (eqne @0 max_value))
3074 (if (eqne == EQ_EXPR)
3076 (if (eqne == NE_EXPR)
3077 { constant_boolean_node (true, type); }))))
3079 /* y == XXX_MIN || x < y --> x <= y - 1 */
3081 (bit_ior:c (eq:s @1 min_value) (lt:cs @0 @1))
3082 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
3083 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
3084 (le @0 (minus @1 { build_int_cst (TREE_TYPE (@1), 1); }))))
3086 /* y != XXX_MIN && x >= y --> x > y - 1 */
3088 (bit_and:c (ne:s @1 min_value) (ge:cs @0 @1))
3089 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
3090 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
3091 (gt @0 (minus @1 { build_int_cst (TREE_TYPE (@1), 1); }))))
3093 /* Convert (X == CST1) && ((other)X OP2 CST2) to a known value
3094 based on CST1 OP2 CST2. Similarly for (X != CST1). */
3095 /* Convert (X == Y) && (X OP2 Y) to a known value if X is an integral type.
3096 Similarly for (X != Y). */
3099 (for code2 (eq ne lt gt le ge)
3101 (bit_and:c (code1:c@3 @0 @1) (code2:c@4 (convert?@c0 @0) @2))
3102 (if ((TREE_CODE (@1) == INTEGER_CST
3103 && TREE_CODE (@2) == INTEGER_CST)
3104 || ((INTEGRAL_TYPE_P (TREE_TYPE (@1))
3105 || POINTER_TYPE_P (TREE_TYPE (@1)))
3106 && bitwise_equal_p (@1, @2)))
3109 bool one_before = false;
3110 bool one_after = false;
3112 bool allbits = true;
3113 if (TREE_CODE (@1) == INTEGER_CST
3114 && TREE_CODE (@2) == INTEGER_CST)
3116 allbits = TYPE_PRECISION (TREE_TYPE (@1)) <= TYPE_PRECISION (TREE_TYPE (@2));
3117 auto t1 = wi::to_wide (fold_convert (TREE_TYPE (@2), @1));
3118 auto t2 = wi::to_wide (@2);
3119 cmp = wi::cmp (t1, t2, TYPE_SIGN (TREE_TYPE (@2)));
3130 case EQ_EXPR: val = (cmp == 0); break;
3131 case NE_EXPR: val = (cmp != 0); break;
3132 case LT_EXPR: val = (cmp < 0); break;
3133 case GT_EXPR: val = (cmp > 0); break;
3134 case LE_EXPR: val = (cmp <= 0); break;
3135 case GE_EXPR: val = (cmp >= 0); break;
3136 default: gcc_unreachable ();
3140 (if (code1 == EQ_EXPR && val) @3)
3141 (if (code1 == EQ_EXPR && !val) { constant_boolean_node (false, type); })
3142 (if (code1 == NE_EXPR && !val && allbits) @4)
3143 (if (code1 == NE_EXPR
3147 (gt @c0 (convert @1)))
3148 (if (code1 == NE_EXPR
3152 (lt @c0 (convert @1)))
3153 /* (a != (b+1)) & (a > b) -> a > (b+1) */
3154 (if (code1 == NE_EXPR
3158 (gt @c0 (convert @1)))
3159 /* (a != (b-1)) & (a < b) -> a < (b-1) */
3160 (if (code1 == NE_EXPR
3164 (lt @c0 (convert @1)))
3172 /* Convert (X OP1 CST1) && (X OP2 CST2).
3173 Convert (X OP1 Y) && (X OP2 Y). */
3175 (for code1 (lt le gt ge)
3176 (for code2 (lt le gt ge)
3178 (bit_and (code1:c@3 @0 @1) (code2:c@4 @0 @2))
3179 (if ((TREE_CODE (@1) == INTEGER_CST
3180 && TREE_CODE (@2) == INTEGER_CST)
3181 || ((INTEGRAL_TYPE_P (TREE_TYPE (@1))
3182 || POINTER_TYPE_P (TREE_TYPE (@1)))
3183 && operand_equal_p (@1, @2)))
3187 if (TREE_CODE (@1) == INTEGER_CST
3188 && TREE_CODE (@2) == INTEGER_CST)
3189 cmp = tree_int_cst_compare (@1, @2);
3192 /* Choose the more restrictive of two < or <= comparisons. */
3193 (if ((code1 == LT_EXPR || code1 == LE_EXPR)
3194 && (code2 == LT_EXPR || code2 == LE_EXPR))
3195 (if ((cmp < 0) || (cmp == 0 && code1 == LT_EXPR))
3198 /* Likewise chose the more restrictive of two > or >= comparisons. */
3199 (if ((code1 == GT_EXPR || code1 == GE_EXPR)
3200 && (code2 == GT_EXPR || code2 == GE_EXPR))
3201 (if ((cmp > 0) || (cmp == 0 && code1 == GT_EXPR))
3204 /* Check for singleton ranges. */
3206 && ((code1 == LE_EXPR && code2 == GE_EXPR)
3207 || (code1 == GE_EXPR && code2 == LE_EXPR)))
3209 /* Check for disjoint ranges. */
3211 && (code1 == LT_EXPR || code1 == LE_EXPR)
3212 && (code2 == GT_EXPR || code2 == GE_EXPR))
3213 { constant_boolean_node (false, type); })
3215 && (code1 == GT_EXPR || code1 == GE_EXPR)
3216 && (code2 == LT_EXPR || code2 == LE_EXPR))
3217 { constant_boolean_node (false, type); })
3220 /* Convert (X == CST1) || (X OP2 CST2) to a known value
3221 based on CST1 OP2 CST2. Similarly for (X != CST1). */
3222 /* Convert (X == Y) || (X OP2 Y) to a known value if X is an integral type.
3223 Similarly for (X != Y). */
3226 (for code2 (eq ne lt gt le ge)
3228 (bit_ior:c (code1:c@3 @0 @1) (code2:c@4 (convert?@c0 @0) @2))
3229 (if ((TREE_CODE (@1) == INTEGER_CST
3230 && TREE_CODE (@2) == INTEGER_CST)
3231 || ((INTEGRAL_TYPE_P (TREE_TYPE (@1))
3232 || POINTER_TYPE_P (TREE_TYPE (@1)))
3233 && bitwise_equal_p (@1, @2)))
3236 bool one_before = false;
3237 bool one_after = false;
3239 bool allbits = true;
3240 if (TREE_CODE (@1) == INTEGER_CST
3241 && TREE_CODE (@2) == INTEGER_CST)
3243 allbits = TYPE_PRECISION (TREE_TYPE (@1)) <= TYPE_PRECISION (TREE_TYPE (@2));
3244 auto t1 = wi::to_wide (fold_convert (TREE_TYPE (@2), @1));
3245 auto t2 = wi::to_wide (@2);
3246 cmp = wi::cmp (t1, t2, TYPE_SIGN (TREE_TYPE (@2)));
3257 case EQ_EXPR: val = (cmp == 0); break;
3258 case NE_EXPR: val = (cmp != 0); break;
3259 case LT_EXPR: val = (cmp < 0); break;
3260 case GT_EXPR: val = (cmp > 0); break;
3261 case LE_EXPR: val = (cmp <= 0); break;
3262 case GE_EXPR: val = (cmp >= 0); break;
3263 default: gcc_unreachable ();
3267 (if (code1 == EQ_EXPR && val) @4)
3268 (if (code1 == NE_EXPR && val && allbits) { constant_boolean_node (true, type); })
3269 (if (code1 == NE_EXPR && !val && allbits) @3)
3270 (if (code1 == EQ_EXPR
3275 (if (code1 == EQ_EXPR
3280 /* (a == (b-1)) | (a >= b) -> a >= (b-1) */
3281 (if (code1 == EQ_EXPR
3285 (ge @c0 (convert @1)))
3286 /* (a == (b+1)) | (a <= b) -> a <= (b-1) */
3287 (if (code1 == EQ_EXPR
3291 (le @c0 (convert @1)))
3299 /* Convert (X OP1 CST1) || (X OP2 CST2).
3300 Convert (X OP1 Y) || (X OP2 Y). */
3302 (for code1 (lt le gt ge)
3303 (for code2 (lt le gt ge)
3305 (bit_ior (code1@3 @0 @1) (code2@4 @0 @2))
3306 (if ((TREE_CODE (@1) == INTEGER_CST
3307 && TREE_CODE (@2) == INTEGER_CST)
3308 || ((INTEGRAL_TYPE_P (TREE_TYPE (@1))
3309 || POINTER_TYPE_P (TREE_TYPE (@1)))
3310 && operand_equal_p (@1, @2)))
3314 if (TREE_CODE (@1) == INTEGER_CST
3315 && TREE_CODE (@2) == INTEGER_CST)
3316 cmp = tree_int_cst_compare (@1, @2);
3319 /* Choose the more restrictive of two < or <= comparisons. */
3320 (if ((code1 == LT_EXPR || code1 == LE_EXPR)
3321 && (code2 == LT_EXPR || code2 == LE_EXPR))
3322 (if ((cmp < 0) || (cmp == 0 && code1 == LT_EXPR))
3325 /* Likewise chose the more restrictive of two > or >= comparisons. */
3326 (if ((code1 == GT_EXPR || code1 == GE_EXPR)
3327 && (code2 == GT_EXPR || code2 == GE_EXPR))
3328 (if ((cmp > 0) || (cmp == 0 && code1 == GT_EXPR))
3331 /* Check for singleton ranges. */
3333 && ((code1 == LT_EXPR && code2 == GT_EXPR)
3334 || (code1 == GT_EXPR && code2 == LT_EXPR)))
3336 /* Check for disjoint ranges. */
3338 && (code1 == LT_EXPR || code1 == LE_EXPR)
3339 && (code2 == GT_EXPR || code2 == GE_EXPR))
3340 { constant_boolean_node (true, type); })
3342 && (code1 == GT_EXPR || code1 == GE_EXPR)
3343 && (code2 == LT_EXPR || code2 == LE_EXPR))
3344 { constant_boolean_node (true, type); })
3347 /* Optimize (a CMP b) ^ (a CMP b) */
3348 /* Optimize (a CMP b) != (a CMP b) */
3349 (for op (bit_xor ne)
3350 (for cmp1 (lt lt lt le le le)
3351 cmp2 (gt eq ne ge eq ne)
3352 rcmp (ne le gt ne lt ge)
3354 (op:c (cmp1:c @0 @1) (cmp2:c @0 @1))
3355 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
3358 /* Optimize (a CMP b) == (a CMP b) */
3359 (for cmp1 (lt lt lt le le le)
3360 cmp2 (gt eq ne ge eq ne)
3361 rcmp (eq gt le eq ge lt)
3363 (eq:c (cmp1:c @0 @1) (cmp2:c @0 @1))
3364 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
3367 /* (type)([0,1]@a != 0) -> (type)a
3368 (type)([0,1]@a == 1) -> (type)a
3369 (type)([0,1]@a == 0) -> a ^ 1
3370 (type)([0,1]@a != 1) -> a ^ 1. */
3373 (convert (eqne zero_one_valued_p@0 INTEGER_CST@1))
3374 (if ((integer_zerop (@1) || integer_onep (@1)))
3375 (if ((eqne == EQ_EXPR) ^ integer_zerop (@1))
3377 /* Only do this if the types match as (type)(a == 0) is
3378 canonical form normally, while `a ^ 1` is canonical when
3379 there is no type change. */
3380 (if (types_match (type, TREE_TYPE (@0)))
3381 (bit_xor @0 { build_one_cst (type); } ))))))
3383 /* We can't reassociate at all for saturating types. */
3384 (if (!TYPE_SATURATING (type))
3386 /* Contract negates. */
3387 /* A + (-B) -> A - B */
3389 (plus:c @0 (convert? (negate @1)))
3390 /* Apply STRIP_NOPS on the negate. */
3391 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
3392 && !TYPE_OVERFLOW_SANITIZED (type))
3396 if (INTEGRAL_TYPE_P (type)
3397 && TYPE_OVERFLOW_WRAPS (type) != TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
3398 t1 = TYPE_OVERFLOW_WRAPS (type) ? type : TREE_TYPE (@1);
3400 (convert (minus (convert:t1 @0) (convert:t1 @1))))))
3401 /* A - (-B) -> A + B */
3403 (minus @0 (convert? (negate @1)))
3404 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
3405 && !TYPE_OVERFLOW_SANITIZED (type))
3409 if (INTEGRAL_TYPE_P (type)
3410 && TYPE_OVERFLOW_WRAPS (type) != TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
3411 t1 = TYPE_OVERFLOW_WRAPS (type) ? type : TREE_TYPE (@1);
3413 (convert (plus (convert:t1 @0) (convert:t1 @1))))))
3415 Sign-extension is ok except for INT_MIN, which thankfully cannot
3416 happen without overflow. */
3418 (negate (convert (negate @1)))
3419 (if (INTEGRAL_TYPE_P (type)
3420 && (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@1))
3421 || (!TYPE_UNSIGNED (TREE_TYPE (@1))
3422 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))))
3423 && !TYPE_OVERFLOW_SANITIZED (type)
3424 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@1)))
3427 (negate (convert negate_expr_p@1))
3428 (if (SCALAR_FLOAT_TYPE_P (type)
3429 && ((DECIMAL_FLOAT_TYPE_P (type)
3430 == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1))
3431 && TYPE_PRECISION (type) >= TYPE_PRECISION (TREE_TYPE (@1)))
3432 || !HONOR_SIGN_DEPENDENT_ROUNDING (type)))
3433 (convert (negate @1))))
3435 (negate (nop_convert? (negate @1)))
3436 (if (!TYPE_OVERFLOW_SANITIZED (type)
3437 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@1)))
3440 /* We can't reassociate floating-point unless -fassociative-math
3441 or fixed-point plus or minus because of saturation to +-Inf. */
3442 (if ((!FLOAT_TYPE_P (type) || flag_associative_math)
3443 && !FIXED_POINT_TYPE_P (type))
3445 /* Match patterns that allow contracting a plus-minus pair
3446 irrespective of overflow issues. */
3447 /* (A +- B) - A -> +- B */
3448 /* (A +- B) -+ B -> A */
3449 /* A - (A +- B) -> -+ B */
3450 /* A +- (B -+ A) -> +- B */
3452 (minus (nop_convert1? (plus:c (nop_convert2? @0) @1)) @0)
3455 (minus (nop_convert1? (minus (nop_convert2? @0) @1)) @0)
3456 (if (!ANY_INTEGRAL_TYPE_P (type)
3457 || TYPE_OVERFLOW_WRAPS (type))
3458 (negate (view_convert @1))
3459 (view_convert (negate @1))))
3461 (plus:c (nop_convert1? (minus @0 (nop_convert2? @1))) @1)
3464 (minus @0 (nop_convert1? (plus:c (nop_convert2? @0) @1)))
3465 (if (!ANY_INTEGRAL_TYPE_P (type)
3466 || TYPE_OVERFLOW_WRAPS (type))
3467 (negate (view_convert @1))
3468 (view_convert (negate @1))))
3470 (minus @0 (nop_convert1? (minus (nop_convert2? @0) @1)))
3472 /* (A +- B) + (C - A) -> C +- B */
3473 /* (A + B) - (A - C) -> B + C */
3474 /* More cases are handled with comparisons. */
3476 (plus:c (plus:c @0 @1) (minus @2 @0))
3479 (plus:c (minus @0 @1) (minus @2 @0))
3482 (plus:c (pointer_diff @0 @1) (pointer_diff @2 @0))
3483 (if (TYPE_OVERFLOW_UNDEFINED (type)
3484 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0)))
3485 (pointer_diff @2 @1)))
3487 (minus (plus:c @0 @1) (minus @0 @2))
3490 /* (A +- CST1) +- CST2 -> A + CST3
3491 Use view_convert because it is safe for vectors and equivalent for
3493 (for outer_op (plus minus)
3494 (for inner_op (plus minus)
3495 neg_inner_op (minus plus)
3497 (outer_op (nop_convert? (inner_op @0 CONSTANT_CLASS_P@1))
3499 /* If one of the types wraps, use that one. */
3500 (if (!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_WRAPS (type))
3501 /* If all 3 captures are CONSTANT_CLASS_P, punt, as we might recurse
3502 forever if something doesn't simplify into a constant. */
3503 (if (!CONSTANT_CLASS_P (@0))
3504 (if (outer_op == PLUS_EXPR)
3505 (plus (view_convert @0) (inner_op! @2 (view_convert @1)))
3506 (minus (view_convert @0) (neg_inner_op! @2 (view_convert @1)))))
3507 (if (!ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3508 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
3509 (if (outer_op == PLUS_EXPR)
3510 (view_convert (plus @0 (inner_op! (view_convert @2) @1)))
3511 (view_convert (minus @0 (neg_inner_op! (view_convert @2) @1))))
3512 /* If the constant operation overflows we cannot do the transform
3513 directly as we would introduce undefined overflow, for example
3514 with (a - 1) + INT_MIN. */
3515 (if (types_match (type, @0) && !TYPE_OVERFLOW_SANITIZED (type))
3516 (with { tree cst = const_binop (outer_op == inner_op
3517 ? PLUS_EXPR : MINUS_EXPR,
3520 (if (INTEGRAL_TYPE_P (type) && !TREE_OVERFLOW (cst))
3521 (inner_op @0 { cst; } )
3522 /* X+INT_MAX+1 is X-INT_MIN. */
3523 (if (INTEGRAL_TYPE_P (type)
3524 && wi::to_wide (cst) == wi::min_value (type))
3525 (neg_inner_op @0 { wide_int_to_tree (type, wi::to_wide (cst)); })
3526 /* Last resort, use some unsigned type. */
3527 (with { tree utype = unsigned_type_for (type); }
3529 (view_convert (inner_op
3530 (view_convert:utype @0)
3532 { TREE_OVERFLOW (cst)
3533 ? drop_tree_overflow (cst) : cst; })))))))))))))))
3535 /* (CST1 - A) +- CST2 -> CST3 - A */
3536 (for outer_op (plus minus)
3538 (outer_op (nop_convert? (minus CONSTANT_CLASS_P@1 @0)) CONSTANT_CLASS_P@2)
3539 /* If one of the types wraps, use that one. */
3540 (if (!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_WRAPS (type))
3541 /* If all 3 captures are CONSTANT_CLASS_P, punt, as we might recurse
3542 forever if something doesn't simplify into a constant. */
3543 (if (!CONSTANT_CLASS_P (@0))
3544 (minus (outer_op! (view_convert @1) @2) (view_convert @0)))
3545 (if (!ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3546 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
3547 (view_convert (minus (outer_op! @1 (view_convert @2)) @0))
3548 (if (types_match (type, @0) && !TYPE_OVERFLOW_SANITIZED (type))
3549 (with { tree cst = const_binop (outer_op, type, @1, @2); }
3550 (if (cst && !TREE_OVERFLOW (cst))
3551 (minus { cst; } @0))))))))
3553 /* CST1 - (CST2 - A) -> CST3 + A
3554 Use view_convert because it is safe for vectors and equivalent for
3557 (minus CONSTANT_CLASS_P@1 (nop_convert? (minus CONSTANT_CLASS_P@2 @0)))
3558 /* If one of the types wraps, use that one. */
3559 (if (!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_WRAPS (type))
3560 /* If all 3 captures are CONSTANT_CLASS_P, punt, as we might recurse
3561 forever if something doesn't simplify into a constant. */
3562 (if (!CONSTANT_CLASS_P (@0))
3563 (plus (view_convert @0) (minus! @1 (view_convert @2))))
3564 (if (!ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3565 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
3566 (view_convert (plus @0 (minus! (view_convert @1) @2)))
3567 (if (types_match (type, @0) && !TYPE_OVERFLOW_SANITIZED (type))
3568 (with { tree cst = const_binop (MINUS_EXPR, type, @1, @2); }
3569 (if (cst && !TREE_OVERFLOW (cst))
3570 (plus { cst; } @0)))))))
3572 /* ((T)(A)) + CST -> (T)(A + CST) */
3575 (plus (convert:s SSA_NAME@0) INTEGER_CST@1)
3576 (if (TREE_CODE (TREE_TYPE (@0)) == INTEGER_TYPE
3577 && TREE_CODE (type) == INTEGER_TYPE
3578 && TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (@0))
3579 && int_fits_type_p (@1, TREE_TYPE (@0)))
3580 /* Perform binary operation inside the cast if the constant fits
3581 and (A + CST)'s range does not overflow. */
3584 wi::overflow_type min_ovf = wi::OVF_OVERFLOW,
3585 max_ovf = wi::OVF_OVERFLOW;
3586 tree inner_type = TREE_TYPE (@0);
3589 = wide_int::from (wi::to_wide (@1), TYPE_PRECISION (inner_type),
3590 TYPE_SIGN (inner_type));
3593 if (get_global_range_query ()->range_of_expr (vr, @0)
3594 && !vr.varying_p () && !vr.undefined_p ())
3596 wide_int wmin0 = vr.lower_bound ();
3597 wide_int wmax0 = vr.upper_bound ();
3598 wi::add (wmin0, w1, TYPE_SIGN (inner_type), &min_ovf);
3599 wi::add (wmax0, w1, TYPE_SIGN (inner_type), &max_ovf);
3602 (if (min_ovf == wi::OVF_NONE && max_ovf == wi::OVF_NONE)
3603 (convert (plus @0 { wide_int_to_tree (TREE_TYPE (@0), w1); } )))
3607 /* ((T)(A + CST1)) + CST2 -> (T)(A) + (T)CST1 + CST2 */
3609 (for op (plus minus)
3611 (plus (convert:s (op:s @0 INTEGER_CST@1)) INTEGER_CST@2)
3612 (if (TREE_CODE (TREE_TYPE (@0)) == INTEGER_TYPE
3613 && TREE_CODE (type) == INTEGER_TYPE
3614 && TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (@0))
3615 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
3616 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0))
3617 && TYPE_OVERFLOW_WRAPS (type))
3618 (plus (convert @0) (op @2 (convert @1))))))
3621 /* (T)(A) +- (T)(B) -> (T)(A +- B) only when (A +- B) could be simplified
3622 to a simple value. */
3623 (for op (plus minus)
3625 (op (convert @0) (convert @1))
3626 (if (INTEGRAL_TYPE_P (type)
3627 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
3628 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))
3629 && types_match (TREE_TYPE (@0), TREE_TYPE (@1))
3630 && !TYPE_OVERFLOW_TRAPS (type)
3631 && !TYPE_OVERFLOW_SANITIZED (type))
3632 (convert (op! @0 @1)))))
3636 (plus:c (convert? (bit_not @0)) (convert? @0))
3637 (if (!TYPE_OVERFLOW_TRAPS (type))
3638 (convert { build_all_ones_cst (TREE_TYPE (@0)); })))
3642 (plus (convert? (bit_not @0)) integer_each_onep)
3643 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
3644 (negate (convert @0))))
3648 (minus (convert? (negate @0)) integer_each_onep)
3649 (if (!TYPE_OVERFLOW_TRAPS (type)
3650 && TREE_CODE (type) != COMPLEX_TYPE
3651 && tree_nop_conversion_p (type, TREE_TYPE (@0)))
3652 (bit_not (convert @0))))
3656 (minus integer_all_onesp @0)
3657 (if (TREE_CODE (type) != COMPLEX_TYPE)
3660 /* (T)(P + A) - (T)P -> (T) A */
3662 (minus (convert (plus:c @@0 @1))
3664 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
3665 /* For integer types, if A has a smaller type
3666 than T the result depends on the possible
3668 E.g. T=size_t, A=(unsigned)429497295, P>0.
3669 However, if an overflow in P + A would cause
3670 undefined behavior, we can assume that there
3672 || (INTEGRAL_TYPE_P (TREE_TYPE (@1))
3673 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))))
3676 (minus (convert (pointer_plus @@0 @1))
3678 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
3679 /* For pointer types, if the conversion of A to the
3680 final type requires a sign- or zero-extension,
3681 then we have to punt - it is not defined which
3683 || (POINTER_TYPE_P (TREE_TYPE (@0))
3684 && TREE_CODE (@1) == INTEGER_CST
3685 && tree_int_cst_sign_bit (@1) == 0))
3688 (pointer_diff (pointer_plus @@0 @1) @0)
3689 /* The second argument of pointer_plus must be interpreted as signed, and
3690 thus sign-extended if necessary. */
3691 (with { tree stype = signed_type_for (TREE_TYPE (@1)); }
3692 /* Use view_convert instead of convert here, as POINTER_PLUS_EXPR
3693 second arg is unsigned even when we need to consider it as signed,
3694 we don't want to diagnose overflow here. */
3695 (convert (view_convert:stype @1))))
3697 /* (T)P - (T)(P + A) -> -(T) A */
3699 (minus (convert? @0)
3700 (convert (plus:c @@0 @1)))
3701 (if (INTEGRAL_TYPE_P (type)
3702 && TYPE_OVERFLOW_UNDEFINED (type)
3703 /* For integer literals, using an intermediate unsigned type to avoid
3704 an overflow at run time is counter-productive because it introduces
3705 spurious overflows at compile time, in the form of TREE_OVERFLOW on
3706 the result, which may be problematic in GENERIC for some front-ends:
3707 (T)P - (T)(P + 4) -> (T)(-(U)4) -> (T)(4294967292) -> -4(OVF)
3708 so we use the direct path for them. */
3709 && TREE_CODE (@1) != INTEGER_CST
3710 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
3711 (with { tree utype = unsigned_type_for (type); }
3712 (convert (negate (convert:utype @1))))
3713 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
3714 /* For integer types, if A has a smaller type
3715 than T the result depends on the possible
3717 E.g. T=size_t, A=(unsigned)429497295, P>0.
3718 However, if an overflow in P + A would cause
3719 undefined behavior, we can assume that there
3721 || (INTEGRAL_TYPE_P (TREE_TYPE (@1))
3722 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))))
3723 (negate (convert @1)))))
3726 (convert (pointer_plus @@0 @1)))
3727 (if (INTEGRAL_TYPE_P (type)
3728 && TYPE_OVERFLOW_UNDEFINED (type)
3729 /* See above the rationale for this condition. */
3730 && TREE_CODE (@1) != INTEGER_CST
3731 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
3732 (with { tree utype = unsigned_type_for (type); }
3733 (convert (negate (convert:utype @1))))
3734 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
3735 /* For pointer types, if the conversion of A to the
3736 final type requires a sign- or zero-extension,
3737 then we have to punt - it is not defined which
3739 || (POINTER_TYPE_P (TREE_TYPE (@0))
3740 && TREE_CODE (@1) == INTEGER_CST
3741 && tree_int_cst_sign_bit (@1) == 0))
3742 (negate (convert @1)))))
3744 (pointer_diff @0 (pointer_plus @@0 @1))
3745 /* The second argument of pointer_plus must be interpreted as signed, and
3746 thus sign-extended if necessary. */
3747 (with { tree stype = signed_type_for (TREE_TYPE (@1)); }
3748 /* Use view_convert instead of convert here, as POINTER_PLUS_EXPR
3749 second arg is unsigned even when we need to consider it as signed,
3750 we don't want to diagnose overflow here. */
3751 (negate (convert (view_convert:stype @1)))))
3753 /* (T)(P + A) - (T)(P + B) -> (T)A - (T)B */
3755 (minus (convert (plus:c @@0 @1))
3756 (convert (plus:c @0 @2)))
3757 (if (INTEGRAL_TYPE_P (type)
3758 && TYPE_OVERFLOW_UNDEFINED (type)
3759 && element_precision (type) <= element_precision (TREE_TYPE (@1))
3760 && element_precision (type) <= element_precision (TREE_TYPE (@2)))
3761 (with { tree utype = unsigned_type_for (type); }
3762 (convert (minus (convert:utype @1) (convert:utype @2))))
3763 (if (((element_precision (type) <= element_precision (TREE_TYPE (@1)))
3764 == (element_precision (type) <= element_precision (TREE_TYPE (@2))))
3765 && (element_precision (type) <= element_precision (TREE_TYPE (@1))
3766 /* For integer types, if A has a smaller type
3767 than T the result depends on the possible
3769 E.g. T=size_t, A=(unsigned)429497295, P>0.
3770 However, if an overflow in P + A would cause
3771 undefined behavior, we can assume that there
3773 || (INTEGRAL_TYPE_P (TREE_TYPE (@1))
3774 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
3775 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))
3776 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@2)))))
3777 (minus (convert @1) (convert @2)))))
3779 (minus (convert (pointer_plus @@0 @1))
3780 (convert (pointer_plus @0 @2)))
3781 (if (INTEGRAL_TYPE_P (type)
3782 && TYPE_OVERFLOW_UNDEFINED (type)
3783 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
3784 (with { tree utype = unsigned_type_for (type); }
3785 (convert (minus (convert:utype @1) (convert:utype @2))))
3786 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
3787 /* For pointer types, if the conversion of A to the
3788 final type requires a sign- or zero-extension,
3789 then we have to punt - it is not defined which
3791 || (POINTER_TYPE_P (TREE_TYPE (@0))
3792 && TREE_CODE (@1) == INTEGER_CST
3793 && tree_int_cst_sign_bit (@1) == 0
3794 && TREE_CODE (@2) == INTEGER_CST
3795 && tree_int_cst_sign_bit (@2) == 0))
3796 (minus (convert @1) (convert @2)))))
3798 (pointer_diff (pointer_plus @0 @2) (pointer_plus @1 @2))
3799 (pointer_diff @0 @1))
3801 (pointer_diff (pointer_plus @@0 @1) (pointer_plus @0 @2))
3802 /* The second argument of pointer_plus must be interpreted as signed, and
3803 thus sign-extended if necessary. */
3804 (with { tree stype = signed_type_for (TREE_TYPE (@1)); }
3805 /* Use view_convert instead of convert here, as POINTER_PLUS_EXPR
3806 second arg is unsigned even when we need to consider it as signed,
3807 we don't want to diagnose overflow here. */
3808 (minus (convert (view_convert:stype @1))
3809 (convert (view_convert:stype @2)))))))
3811 /* (A * C) +- (B * C) -> (A+-B) * C and (A * C) +- A -> A * (C+-1).
3812 Modeled after fold_plusminus_mult_expr. */
3813 (if (!TYPE_SATURATING (type)
3814 && (!FLOAT_TYPE_P (type) || flag_associative_math))
3815 (for plusminus (plus minus)
3817 (plusminus (mult:cs@3 @0 @1) (mult:cs@4 @0 @2))
3818 (if (!ANY_INTEGRAL_TYPE_P (type)
3819 || TYPE_OVERFLOW_WRAPS (type)
3820 || (INTEGRAL_TYPE_P (type)
3821 && tree_expr_nonzero_p (@0)
3822 && expr_not_equal_to (@0, wi::minus_one (TYPE_PRECISION (type)))))
3823 (if (single_use (@3) || single_use (@4))
3824 /* If @1 +- @2 is constant require a hard single-use on either
3825 original operand (but not on both). */
3826 (mult (plusminus @1 @2) @0)
3827 (mult! (plusminus @1 @2) @0)
3829 /* We cannot generate constant 1 for fract. */
3830 (if (!ALL_FRACT_MODE_P (TYPE_MODE (type)))
3832 (plusminus @0 (mult:c@3 @0 @2))
3833 (if ((!ANY_INTEGRAL_TYPE_P (type)
3834 || TYPE_OVERFLOW_WRAPS (type)
3835 /* For @0 + @0*@2 this transformation would introduce UB
3836 (where there was none before) for @0 in [-1,0] and @2 max.
3837 For @0 - @0*@2 this transformation would introduce UB
3838 for @0 0 and @2 in [min,min+1] or @0 -1 and @2 min+1. */
3839 || (INTEGRAL_TYPE_P (type)
3840 && ((tree_expr_nonzero_p (@0)
3841 && expr_not_equal_to (@0,
3842 wi::minus_one (TYPE_PRECISION (type))))
3843 || (plusminus == PLUS_EXPR
3844 ? expr_not_equal_to (@2,
3845 wi::max_value (TYPE_PRECISION (type), SIGNED))
3846 /* Let's ignore the @0 -1 and @2 min case. */
3847 : (expr_not_equal_to (@2,
3848 wi::min_value (TYPE_PRECISION (type), SIGNED))
3849 && expr_not_equal_to (@2,
3850 wi::min_value (TYPE_PRECISION (type), SIGNED)
3853 (mult (plusminus { build_one_cst (type); } @2) @0)))
3855 (plusminus (mult:c@3 @0 @2) @0)
3856 (if ((!ANY_INTEGRAL_TYPE_P (type)
3857 || TYPE_OVERFLOW_WRAPS (type)
3858 /* For @0*@2 + @0 this transformation would introduce UB
3859 (where there was none before) for @0 in [-1,0] and @2 max.
3860 For @0*@2 - @0 this transformation would introduce UB
3861 for @0 0 and @2 min. */
3862 || (INTEGRAL_TYPE_P (type)
3863 && ((tree_expr_nonzero_p (@0)
3864 && (plusminus == MINUS_EXPR
3865 || expr_not_equal_to (@0,
3866 wi::minus_one (TYPE_PRECISION (type)))))
3867 || expr_not_equal_to (@2,
3868 (plusminus == PLUS_EXPR
3869 ? wi::max_value (TYPE_PRECISION (type), SIGNED)
3870 : wi::min_value (TYPE_PRECISION (type), SIGNED))))))
3872 (mult (plusminus @2 { build_one_cst (type); }) @0))))))
3875 /* Canonicalize X + (X << C) into X * (1 + (1 << C)) and
3876 (X << C1) + (X << C2) into X * ((1 << C1) + (1 << C2)). */
3878 (plus:c @0 (lshift:s @0 INTEGER_CST@1))
3879 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3880 && tree_fits_uhwi_p (@1)
3881 && tree_to_uhwi (@1) < element_precision (type)
3882 && (INTEGRAL_TYPE_P (TREE_TYPE (@0))
3883 || optab_handler (smul_optab,
3884 TYPE_MODE (type)) != CODE_FOR_nothing))
3885 (with { tree t = type;
3886 if (!TYPE_OVERFLOW_WRAPS (t)) t = unsigned_type_for (t);
3887 wide_int w = wi::set_bit_in_zero (tree_to_uhwi (@1),
3888 element_precision (type));
3890 tree cst = wide_int_to_tree (VECTOR_TYPE_P (t) ? TREE_TYPE (t)
3892 cst = build_uniform_cst (t, cst); }
3893 (convert (mult (convert:t @0) { cst; })))))
3895 (plus (lshift:s @0 INTEGER_CST@1) (lshift:s @0 INTEGER_CST@2))
3896 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3897 && tree_fits_uhwi_p (@1)
3898 && tree_to_uhwi (@1) < element_precision (type)
3899 && tree_fits_uhwi_p (@2)
3900 && tree_to_uhwi (@2) < element_precision (type)
3901 && (INTEGRAL_TYPE_P (TREE_TYPE (@0))
3902 || optab_handler (smul_optab,
3903 TYPE_MODE (type)) != CODE_FOR_nothing))
3904 (with { tree t = type;
3905 if (!TYPE_OVERFLOW_WRAPS (t)) t = unsigned_type_for (t);
3906 unsigned int prec = element_precision (type);
3907 wide_int w = wi::set_bit_in_zero (tree_to_uhwi (@1), prec);
3908 w += wi::set_bit_in_zero (tree_to_uhwi (@2), prec);
3909 tree cst = wide_int_to_tree (VECTOR_TYPE_P (t) ? TREE_TYPE (t)
3911 cst = build_uniform_cst (t, cst); }
3912 (convert (mult (convert:t @0) { cst; })))))
3915 /* Canonicalize (X*C1)|(X*C2) and (X*C1)^(X*C2) to (C1+C2)*X when
3916 tree_nonzero_bits allows IOR and XOR to be treated like PLUS.
3917 Likewise, handle (X<<C3) and X as legitimate variants of X*C. */
3918 (for op (bit_ior bit_xor)
3920 (op (mult:s@0 @1 INTEGER_CST@2)
3921 (mult:s@3 @1 INTEGER_CST@4))
3922 (if (INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_WRAPS (type)
3923 && (tree_nonzero_bits (@0) & tree_nonzero_bits (@3)) == 0)
3925 { wide_int_to_tree (type, wi::to_wide (@2) + wi::to_wide (@4)); })))
3927 (op:c (mult:s@0 @1 INTEGER_CST@2)
3928 (lshift:s@3 @1 INTEGER_CST@4))
3929 (if (INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_WRAPS (type)
3930 && tree_int_cst_sgn (@4) > 0
3931 && (tree_nonzero_bits (@0) & tree_nonzero_bits (@3)) == 0)
3932 (with { wide_int wone = wi::one (TYPE_PRECISION (type));
3933 wide_int c = wi::add (wi::to_wide (@2),
3934 wi::lshift (wone, wi::to_wide (@4))); }
3935 (mult @1 { wide_int_to_tree (type, c); }))))
3937 (op:c (mult:s@0 @1 INTEGER_CST@2)
3939 (if (INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_WRAPS (type)
3940 && (tree_nonzero_bits (@0) & tree_nonzero_bits (@1)) == 0)
3942 { wide_int_to_tree (type,
3943 wi::add (wi::to_wide (@2), 1)); })))
3945 (op (lshift:s@0 @1 INTEGER_CST@2)
3946 (lshift:s@3 @1 INTEGER_CST@4))
3947 (if (INTEGRAL_TYPE_P (type)
3948 && tree_int_cst_sgn (@2) > 0
3949 && tree_int_cst_sgn (@4) > 0
3950 && (tree_nonzero_bits (@0) & tree_nonzero_bits (@3)) == 0)
3951 (with { tree t = type;
3952 if (!TYPE_OVERFLOW_WRAPS (t))
3953 t = unsigned_type_for (t);
3954 wide_int wone = wi::one (TYPE_PRECISION (t));
3955 wide_int c = wi::add (wi::lshift (wone, wi::to_wide (@2)),
3956 wi::lshift (wone, wi::to_wide (@4))); }
3957 (convert (mult:t (convert:t @1) { wide_int_to_tree (t,c); })))))
3959 (op:c (lshift:s@0 @1 INTEGER_CST@2)
3961 (if (INTEGRAL_TYPE_P (type)
3962 && tree_int_cst_sgn (@2) > 0
3963 && (tree_nonzero_bits (@0) & tree_nonzero_bits (@1)) == 0)
3964 (with { tree t = type;
3965 if (!TYPE_OVERFLOW_WRAPS (t))
3966 t = unsigned_type_for (t);
3967 wide_int wone = wi::one (TYPE_PRECISION (t));
3968 wide_int c = wi::add (wi::lshift (wone, wi::to_wide (@2)), wone); }
3969 (convert (mult:t (convert:t @1) { wide_int_to_tree (t, c); }))))))
3971 /* Simplifications of MIN_EXPR, MAX_EXPR, fmin() and fmax(). */
3973 (for minmax (min max)
3977 /* max(max(x,y),x) -> max(x,y) */
3979 (minmax:c (minmax:c@2 @0 @1) @0)
3981 /* For fmin() and fmax(), skip folding when both are sNaN. */
3982 (for minmax (FMIN_ALL FMAX_ALL)
3985 (if (!tree_expr_maybe_signaling_nan_p (@0))
3987 /* min(max(x,y),y) -> y. */
3989 (min:c (max:c @0 @1) @1)
3991 /* max(min(x,y),y) -> y. */
3993 (max:c (min:c @0 @1) @1)
3995 /* max(a,-a) -> abs(a). */
3997 (max:c @0 (negate @0))
3998 (if (TREE_CODE (type) != COMPLEX_TYPE
3999 && (! ANY_INTEGRAL_TYPE_P (type)
4000 || TYPE_OVERFLOW_UNDEFINED (type)))
4002 /* min(a,-a) -> -abs(a). */
4004 (min:c @0 (negate @0))
4005 (if (TREE_CODE (type) != COMPLEX_TYPE
4006 && (! ANY_INTEGRAL_TYPE_P (type)
4007 || TYPE_OVERFLOW_UNDEFINED (type)))
4012 (if (INTEGRAL_TYPE_P (type)
4013 && TYPE_MIN_VALUE (type)
4014 && operand_equal_p (@1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
4016 (if (INTEGRAL_TYPE_P (type)
4017 && TYPE_MAX_VALUE (type)
4018 && operand_equal_p (@1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
4023 (if (INTEGRAL_TYPE_P (type)
4024 && TYPE_MAX_VALUE (type)
4025 && operand_equal_p (@1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
4027 (if (INTEGRAL_TYPE_P (type)
4028 && TYPE_MIN_VALUE (type)
4029 && operand_equal_p (@1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
4032 /* max (a, a + CST) -> a + CST where CST is positive. */
4033 /* max (a, a + CST) -> a where CST is negative. */
4035 (max:c @0 (plus@2 @0 INTEGER_CST@1))
4036 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
4037 (if (tree_int_cst_sgn (@1) > 0)
4041 /* min (a, a + CST) -> a where CST is positive. */
4042 /* min (a, a + CST) -> a + CST where CST is negative. */
4044 (min:c @0 (plus@2 @0 INTEGER_CST@1))
4045 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
4046 (if (tree_int_cst_sgn (@1) > 0)
4050 /* Simplify min (&var[off0], &var[off1]) etc. depending on whether
4051 the addresses are known to be less, equal or greater. */
4052 (for minmax (min max)
4055 (minmax (convert1?@2 addr@0) (convert2?@3 addr@1))
4058 poly_int64 off0, off1;
4060 int equal = address_compare (cmp, TREE_TYPE (@2), @0, @1, base0, base1,
4061 off0, off1, GENERIC);
4064 (if (minmax == MIN_EXPR)
4065 (if (known_le (off0, off1))
4067 (if (known_gt (off0, off1))
4069 (if (known_ge (off0, off1))
4071 (if (known_lt (off0, off1))
4074 /* (convert (minmax ((convert (x) c)))) -> minmax (x c) if x is promoted
4075 and the outer convert demotes the expression back to x's type. */
4076 (for minmax (min max)
4078 (convert (minmax@0 (convert @1) INTEGER_CST@2))
4079 (if (INTEGRAL_TYPE_P (type)
4080 && types_match (@1, type) && int_fits_type_p (@2, type)
4081 && TYPE_SIGN (TREE_TYPE (@0)) == TYPE_SIGN (type)
4082 && TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type))
4083 (minmax @1 (convert @2)))))
4085 (for minmax (FMIN_ALL FMAX_ALL)
4086 /* If either argument is NaN and other one is not sNaN, return the other
4087 one. Avoid the transformation if we get (and honor) a signalling NaN. */
4089 (minmax:c @0 REAL_CST@1)
4090 (if (real_isnan (TREE_REAL_CST_PTR (@1))
4091 && (!HONOR_SNANS (@1) || !TREE_REAL_CST (@1).signalling)
4092 && !tree_expr_maybe_signaling_nan_p (@0))
4094 /* Convert fmin/fmax to MIN_EXPR/MAX_EXPR. C99 requires these
4095 functions to return the numeric arg if the other one is NaN.
4096 MIN and MAX don't honor that, so only transform if -ffinite-math-only
4097 is set. C99 doesn't require -0.0 to be handled, so we don't have to
4098 worry about it either. */
4099 (if (flag_finite_math_only)
4106 /* min (-A, -B) -> -max (A, B) */
4107 (for minmax (min max FMIN_ALL FMAX_ALL)
4108 maxmin (max min FMAX_ALL FMIN_ALL)
4110 (minmax (negate:s@2 @0) (negate:s@3 @1))
4111 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
4112 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
4113 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
4114 (negate (maxmin @0 @1)))))
4115 /* MIN (~X, ~Y) -> ~MAX (X, Y)
4116 MAX (~X, ~Y) -> ~MIN (X, Y) */
4117 (for minmax (min max)
4120 (minmax (bit_not:s@2 @0) (bit_not:s@3 @1))
4121 (bit_not (maxmin @0 @1)))
4122 /* ~MAX(~X, Y) --> MIN(X, ~Y) */
4123 /* ~MIN(~X, Y) --> MAX(X, ~Y) */
4125 (bit_not (minmax:cs (bit_not @0) @1))
4126 (maxmin @0 (bit_not @1))))
4128 /* MIN (X, Y) == X -> X <= Y */
4129 /* MIN (X, Y) < X -> X > Y */
4130 /* MIN (X, Y) >= X -> X <= Y */
4131 (for minmax (min min min min max max max max)
4132 cmp (eq ne lt ge eq ne gt le )
4133 out (le gt gt le ge lt lt ge )
4135 (cmp:c (minmax:c @0 @1) @0)
4136 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0)))
4138 /* MIN (X, 5) == 0 -> X == 0
4139 MIN (X, 5) == 7 -> false */
4142 (cmp (min @0 INTEGER_CST@1) INTEGER_CST@2)
4143 (if (wi::lt_p (wi::to_wide (@1), wi::to_wide (@2),
4144 TYPE_SIGN (TREE_TYPE (@0))))
4145 { constant_boolean_node (cmp == NE_EXPR, type); }
4146 (if (wi::gt_p (wi::to_wide (@1), wi::to_wide (@2),
4147 TYPE_SIGN (TREE_TYPE (@0))))
4151 (cmp (max @0 INTEGER_CST@1) INTEGER_CST@2)
4152 (if (wi::gt_p (wi::to_wide (@1), wi::to_wide (@2),
4153 TYPE_SIGN (TREE_TYPE (@0))))
4154 { constant_boolean_node (cmp == NE_EXPR, type); }
4155 (if (wi::lt_p (wi::to_wide (@1), wi::to_wide (@2),
4156 TYPE_SIGN (TREE_TYPE (@0))))
4159 /* X <= MAX(X, Y) -> true
4160 X > MAX(X, Y) -> false
4161 X >= MIN(X, Y) -> true
4162 X < MIN(X, Y) -> false */
4163 (for minmax (min min max max )
4166 (cmp:c @0 (minmax:c @0 @1))
4167 { constant_boolean_node (cmp == GE_EXPR || cmp == LE_EXPR, type); } ))
4169 /* MIN (X, C1) < C2 -> X < C2 || C1 < C2 */
4170 (for minmax (min min max max min min max max )
4171 cmp (lt le gt ge gt ge lt le )
4172 comb (bit_ior bit_ior bit_ior bit_ior bit_and bit_and bit_and bit_and)
4174 (cmp (minmax @0 INTEGER_CST@1) INTEGER_CST@2)
4175 (comb (cmp @0 @2) (cmp @1 @2))))
4177 /* Undo fancy ways of writing max/min or other ?: expressions, like
4178 a - ((a - b) & -(a < b)) and a - (a - b) * (a < b) into (a < b) ? b : a.
4179 People normally use ?: and that is what we actually try to optimize. */
4180 /* Transform A + (B-A)*cmp into cmp ? B : A. */
4182 (plus:c @0 (mult:c (minus @1 @0) zero_one_valued_p@2))
4183 (if (INTEGRAL_TYPE_P (type)
4184 && (GIMPLE || !TREE_SIDE_EFFECTS (@1)))
4185 (cond (convert:boolean_type_node @2) @1 @0)))
4186 /* Transform A - (A-B)*cmp into cmp ? B : A. */
4188 (minus @0 (mult:c (minus @0 @1) 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 (bit_xor:c @0 (mult:c (bit_xor:c @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)))
4199 /* (x <= 0 ? -x : 0) -> max(-x, 0). */
4201 (cond (le @0 integer_zerop@1) (negate@2 @0) integer_zerop@1)
4204 /* (zero_one == 0) ? y : z <op> y -> ((typeof(y))zero_one * z) <op> y */
4205 (for op (bit_xor bit_ior plus)
4207 (cond (eq zero_one_valued_p@0
4211 (if (INTEGRAL_TYPE_P (type)
4212 && TYPE_PRECISION (type) > 1
4213 && (INTEGRAL_TYPE_P (TREE_TYPE (@0))))
4214 (op (mult (convert:type @0) @2) @1))))
4216 /* (zero_one != 0) ? z <op> y : y -> ((typeof(y))zero_one * z) <op> y */
4217 (for op (bit_xor bit_ior plus)
4219 (cond (ne zero_one_valued_p@0
4223 (if (INTEGRAL_TYPE_P (type)
4224 && TYPE_PRECISION (type) > 1
4225 && (INTEGRAL_TYPE_P (TREE_TYPE (@0))))
4226 (op (mult (convert:type @0) @2) @1))))
4228 /* ?: Value replacement. */
4229 /* a == 0 ? b : b + a -> b + a */
4230 (for op (plus bit_ior bit_xor)
4232 (cond (eq @0 integer_zerop) @1 (op:c@2 @1 @0))
4234 /* a == 0 ? b : b - a -> b - a */
4235 /* a == 0 ? b : b ptr+ a -> b ptr+ a */
4236 /* a == 0 ? b : b shift/rotate a -> b shift/rotate a */
4237 (for op (lrotate rrotate lshift rshift minus pointer_plus)
4239 (cond (eq @0 integer_zerop) @1 (op@2 @1 @0))
4242 /* a == 1 ? b : b / a -> b / a */
4243 (for op (trunc_div ceil_div floor_div round_div exact_div)
4245 (cond (eq @0 integer_onep) @1 (op@2 @1 @0))
4248 /* a == 1 ? b : a * b -> a * b */
4251 (cond (eq @0 integer_onep) @1 (op:c@2 @1 @0))
4254 /* a == -1 ? b : a & b -> a & b */
4257 (cond (eq @0 integer_all_onesp) @1 (op:c@2 @1 @0))
4260 /* Simplifications of shift and rotates. */
4262 (for rotate (lrotate rrotate)
4264 (rotate integer_all_onesp@0 @1)
4267 /* Optimize -1 >> x for arithmetic right shifts. */
4269 (rshift integer_all_onesp@0 @1)
4270 (if (!TYPE_UNSIGNED (type))
4273 /* Optimize (x >> c) << c into x & (-1<<c). */
4275 (lshift (nop_convert? (rshift @0 INTEGER_CST@1)) @1)
4276 (if (wi::ltu_p (wi::to_wide (@1), element_precision (type)))
4277 /* It doesn't matter if the right shift is arithmetic or logical. */
4278 (bit_and (view_convert @0) (lshift { build_minus_one_cst (type); } @1))))
4281 (lshift (convert (convert@2 (rshift @0 INTEGER_CST@1))) @1)
4282 (if (wi::ltu_p (wi::to_wide (@1), element_precision (type))
4283 /* Allow intermediate conversion to integral type with whatever sign, as
4284 long as the low TYPE_PRECISION (type)
4285 - TYPE_PRECISION (TREE_TYPE (@2)) bits are preserved. */
4286 && INTEGRAL_TYPE_P (type)
4287 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
4288 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
4289 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (@0))
4290 && (TYPE_PRECISION (TREE_TYPE (@2)) >= TYPE_PRECISION (type)
4291 || wi::geu_p (wi::to_wide (@1),
4292 TYPE_PRECISION (type)
4293 - TYPE_PRECISION (TREE_TYPE (@2)))))
4294 (bit_and (convert @0) (lshift { build_minus_one_cst (type); } @1))))
4296 /* For (x << c) >> c, optimize into x & ((unsigned)-1 >> c) for
4297 unsigned x OR truncate into the precision(type) - c lowest bits
4298 of signed x (if they have mode precision or a precision of 1). */
4300 (rshift (nop_convert? (lshift @0 INTEGER_CST@1)) @@1)
4301 (if (wi::ltu_p (wi::to_wide (@1), element_precision (type)))
4302 (if (TYPE_UNSIGNED (type))
4303 (bit_and (convert @0) (rshift { build_minus_one_cst (type); } @1))
4304 (if (INTEGRAL_TYPE_P (type))
4306 int width = element_precision (type) - tree_to_uhwi (@1);
4307 tree stype = NULL_TREE;
4308 if (width <= MAX_FIXED_MODE_SIZE)
4309 stype = build_nonstandard_integer_type (width, 0);
4311 (if (stype && (width == 1 || type_has_mode_precision_p (stype)))
4312 (convert (convert:stype @0))))))))
4314 /* Optimize x >> x into 0 */
4317 { build_zero_cst (type); })
4319 (for shiftrotate (lrotate rrotate lshift rshift)
4321 (shiftrotate @0 integer_zerop)
4324 (shiftrotate integer_zerop@0 @1)
4326 /* Prefer vector1 << scalar to vector1 << vector2
4327 if vector2 is uniform. */
4328 (for vec (VECTOR_CST CONSTRUCTOR)
4330 (shiftrotate @0 vec@1)
4331 (with { tree tem = uniform_vector_p (@1); }
4333 (shiftrotate @0 { tem; }))))))
4335 /* Simplify X << Y where Y's low width bits are 0 to X, as only valid
4336 Y is 0. Similarly for X >> Y. */
4338 (for shift (lshift rshift)
4340 (shift @0 SSA_NAME@1)
4341 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1)))
4343 int width = ceil_log2 (element_precision (TREE_TYPE (@0)));
4344 int prec = TYPE_PRECISION (TREE_TYPE (@1));
4346 (if ((get_nonzero_bits (@1) & wi::mask (width, false, prec)) == 0)
4350 /* Rewrite an LROTATE_EXPR by a constant into an
4351 RROTATE_EXPR by a new constant. */
4353 (lrotate @0 INTEGER_CST@1)
4354 (rrotate @0 { const_binop (MINUS_EXPR, TREE_TYPE (@1),
4355 build_int_cst (TREE_TYPE (@1),
4356 element_precision (type)), @1); }))
4358 /* Turn (a OP c1) OP c2 into a OP (c1+c2). */
4359 (for op (lrotate rrotate rshift lshift)
4361 (op (op @0 INTEGER_CST@1) INTEGER_CST@2)
4362 (with { unsigned int prec = element_precision (type); }
4363 (if (wi::ge_p (wi::to_wide (@1), 0, TYPE_SIGN (TREE_TYPE (@1)))
4364 && wi::lt_p (wi::to_wide (@1), prec, TYPE_SIGN (TREE_TYPE (@1)))
4365 && wi::ge_p (wi::to_wide (@2), 0, TYPE_SIGN (TREE_TYPE (@2)))
4366 && wi::lt_p (wi::to_wide (@2), prec, TYPE_SIGN (TREE_TYPE (@2))))
4367 (with { unsigned int low = (tree_to_uhwi (@1)
4368 + tree_to_uhwi (@2)); }
4369 /* Deal with a OP (c1 + c2) being undefined but (a OP c1) OP c2
4370 being well defined. */
4372 (if (op == LROTATE_EXPR || op == RROTATE_EXPR)
4373 (op @0 { build_int_cst (TREE_TYPE (@1), low % prec); })
4374 (if (TYPE_UNSIGNED (type) || op == LSHIFT_EXPR)
4375 { build_zero_cst (type); }
4376 (op @0 { build_int_cst (TREE_TYPE (@1), prec - 1); })))
4377 (op @0 { build_int_cst (TREE_TYPE (@1), low); })))))))
4380 /* Simplify (CST << x) & 1 to 0 if CST is even or to x == 0 if it is odd. */
4382 (bit_and (lshift INTEGER_CST@1 @0) integer_onep)
4383 (if ((wi::to_wide (@1) & 1) != 0)
4384 (convert (eq:boolean_type_node @0 { build_zero_cst (TREE_TYPE (@0)); }))
4385 { build_zero_cst (type); }))
4387 /* Simplify ((C << x) & D) != 0 where C and D are power of two constants,
4388 either to false if D is smaller (unsigned comparison) than C, or to
4389 x == log2 (D) - log2 (C). Similarly for right shifts.
4390 Note for `(1 >> x)`, the & 1 has been removed so matching that seperately. */
4394 (cmp (bit_and (lshift integer_pow2p@1 @0) integer_pow2p@2) integer_zerop)
4395 (with { int c1 = wi::clz (wi::to_wide (@1));
4396 int c2 = wi::clz (wi::to_wide (@2)); }
4398 { constant_boolean_node (cmp == NE_EXPR ? false : true, type); }
4399 (icmp @0 { build_int_cst (TREE_TYPE (@0), c1 - c2); }))))
4401 (cmp (bit_and (rshift integer_pow2p@1 @0) integer_pow2p@2) integer_zerop)
4402 (if (tree_int_cst_sgn (@1) > 0)
4403 (with { int c1 = wi::clz (wi::to_wide (@1));
4404 int c2 = wi::clz (wi::to_wide (@2)); }
4406 { constant_boolean_node (cmp == NE_EXPR ? false : true, type); }
4407 (icmp @0 { build_int_cst (TREE_TYPE (@0), c2 - c1); })))))
4408 /* `(1 >> X) != 0` -> `X == 0` */
4409 /* `(1 >> X) == 0` -> `X != 0` */
4411 (cmp (rshift integer_onep@1 @0) integer_zerop)
4412 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1)))
4413 (icmp @0 { build_zero_cst (TREE_TYPE (@0)); }))))
4415 /* (CST1 << A) == CST2 -> A == ctz (CST2) - ctz (CST1)
4416 (CST1 << A) != CST2 -> A != ctz (CST2) - ctz (CST1)
4420 (cmp (lshift INTEGER_CST@0 @1) INTEGER_CST@2)
4421 (with { int cand = wi::ctz (wi::to_wide (@2)) - wi::ctz (wi::to_wide (@0)); }
4423 || (!integer_zerop (@2)
4424 && wi::lshift (wi::to_wide (@0), cand) != wi::to_wide (@2)))
4425 { constant_boolean_node (cmp == NE_EXPR, type); }
4426 (if (!integer_zerop (@2)
4427 && wi::lshift (wi::to_wide (@0), cand) == wi::to_wide (@2))
4428 (cmp @1 { build_int_cst (TREE_TYPE (@1), cand); }))))))
4430 /* Fold ((X << C1) & C2) cmp C3 into (X & (C2 >> C1)) cmp (C3 >> C1)
4431 ((X >> C1) & C2) cmp C3 into (X & (C2 << C1)) cmp (C3 << C1). */
4434 (cmp (bit_and:s (lshift:s @0 INTEGER_CST@1) INTEGER_CST@2) INTEGER_CST@3)
4435 (if (tree_fits_shwi_p (@1)
4436 && tree_to_shwi (@1) > 0
4437 && tree_to_shwi (@1) < TYPE_PRECISION (TREE_TYPE (@0)))
4438 (if (tree_to_shwi (@1) > wi::ctz (wi::to_wide (@3)))
4439 { constant_boolean_node (cmp == NE_EXPR, type); }
4440 (with { wide_int c1 = wi::to_wide (@1);
4441 wide_int c2 = wi::lrshift (wi::to_wide (@2), c1);
4442 wide_int c3 = wi::lrshift (wi::to_wide (@3), c1); }
4443 (cmp (bit_and @0 { wide_int_to_tree (TREE_TYPE (@0), c2); })
4444 { wide_int_to_tree (TREE_TYPE (@0), c3); })))))
4446 (cmp (bit_and:s (rshift:s @0 INTEGER_CST@1) INTEGER_CST@2) INTEGER_CST@3)
4447 (if (tree_fits_shwi_p (@1)
4448 && tree_to_shwi (@1) > 0
4449 && tree_to_shwi (@1) < TYPE_PRECISION (TREE_TYPE (@0)))
4450 (with { tree t0 = TREE_TYPE (@0);
4451 unsigned int prec = TYPE_PRECISION (t0);
4452 wide_int c1 = wi::to_wide (@1);
4453 wide_int c2 = wi::to_wide (@2);
4454 wide_int c3 = wi::to_wide (@3);
4455 wide_int sb = wi::set_bit_in_zero (prec - 1, prec); }
4456 (if ((c2 & c3) != c3)
4457 { constant_boolean_node (cmp == NE_EXPR, type); }
4458 (if (TYPE_UNSIGNED (t0))
4459 (if ((c3 & wi::arshift (sb, c1 - 1)) != 0)
4460 { constant_boolean_node (cmp == NE_EXPR, type); }
4461 (cmp (bit_and @0 { wide_int_to_tree (t0, c2 << c1); })
4462 { wide_int_to_tree (t0, c3 << c1); }))
4463 (with { wide_int smask = wi::arshift (sb, c1); }
4465 (if ((c2 & smask) == 0)
4466 (cmp (bit_and @0 { wide_int_to_tree (t0, c2 << c1); })
4467 { wide_int_to_tree (t0, c3 << c1); }))
4468 (if ((c3 & smask) == 0)
4469 (cmp (bit_and @0 { wide_int_to_tree (t0, (c2 << c1) | sb); })
4470 { wide_int_to_tree (t0, c3 << c1); }))
4471 (if ((c2 & smask) != (c3 & smask))
4472 { constant_boolean_node (cmp == NE_EXPR, type); })
4473 (cmp (bit_and @0 { wide_int_to_tree (t0, (c2 << c1) | sb); })
4474 { wide_int_to_tree (t0, (c3 << c1) | sb); })))))))))
4476 /* Fold (X << C1) & C2 into (X << C1) & (C2 | ((1 << C1) - 1))
4477 (X >> C1) & C2 into (X >> C1) & (C2 | ~((type) -1 >> C1))
4478 if the new mask might be further optimized. */
4479 (for shift (lshift rshift)
4481 (bit_and (convert?:s@4 (shift:s@5 (convert1?@3 @0) INTEGER_CST@1))
4483 (if (tree_nop_conversion_p (TREE_TYPE (@4), TREE_TYPE (@5))
4484 && TYPE_PRECISION (type) <= HOST_BITS_PER_WIDE_INT
4485 && tree_fits_uhwi_p (@1)
4486 && tree_to_uhwi (@1) > 0
4487 && tree_to_uhwi (@1) < TYPE_PRECISION (type))
4490 unsigned int shiftc = tree_to_uhwi (@1);
4491 unsigned HOST_WIDE_INT mask = TREE_INT_CST_LOW (@2);
4492 unsigned HOST_WIDE_INT newmask, zerobits = 0;
4493 tree shift_type = TREE_TYPE (@3);
4496 if (shift == LSHIFT_EXPR)
4497 zerobits = ((HOST_WIDE_INT_1U << shiftc) - 1);
4498 else if (shift == RSHIFT_EXPR
4499 && type_has_mode_precision_p (shift_type))
4501 prec = TYPE_PRECISION (TREE_TYPE (@3));
4503 /* See if more bits can be proven as zero because of
4506 && TYPE_UNSIGNED (TREE_TYPE (@0)))
4508 tree inner_type = TREE_TYPE (@0);
4509 if (type_has_mode_precision_p (inner_type)
4510 && TYPE_PRECISION (inner_type) < prec)
4512 prec = TYPE_PRECISION (inner_type);
4513 /* See if we can shorten the right shift. */
4515 shift_type = inner_type;
4516 /* Otherwise X >> C1 is all zeros, so we'll optimize
4517 it into (X, 0) later on by making sure zerobits
4521 zerobits = HOST_WIDE_INT_M1U;
4524 zerobits >>= HOST_BITS_PER_WIDE_INT - shiftc;
4525 zerobits <<= prec - shiftc;
4527 /* For arithmetic shift if sign bit could be set, zerobits
4528 can contain actually sign bits, so no transformation is
4529 possible, unless MASK masks them all away. In that
4530 case the shift needs to be converted into logical shift. */
4531 if (!TYPE_UNSIGNED (TREE_TYPE (@3))
4532 && prec == TYPE_PRECISION (TREE_TYPE (@3)))
4534 if ((mask & zerobits) == 0)
4535 shift_type = unsigned_type_for (TREE_TYPE (@3));
4541 /* ((X << 16) & 0xff00) is (X, 0). */
4542 (if ((mask & zerobits) == mask)
4543 { build_int_cst (type, 0); }
4544 (with { newmask = mask | zerobits; }
4545 (if (newmask != mask && (newmask & (newmask + 1)) == 0)
4548 /* Only do the transformation if NEWMASK is some integer
4550 for (prec = BITS_PER_UNIT;
4551 prec < HOST_BITS_PER_WIDE_INT; prec <<= 1)
4552 if (newmask == (HOST_WIDE_INT_1U << prec) - 1)
4555 (if (prec < HOST_BITS_PER_WIDE_INT
4556 || newmask == HOST_WIDE_INT_M1U)
4558 { tree newmaskt = build_int_cst_type (TREE_TYPE (@2), newmask); }
4559 (if (!tree_int_cst_equal (newmaskt, @2))
4560 (if (shift_type != TREE_TYPE (@3))
4561 (bit_and (convert (shift:shift_type (convert @3) @1)) { newmaskt; })
4562 (bit_and @4 { newmaskt; })))))))))))))
4564 /* ((1 << n) & M) != 0 -> n == log2 (M) */
4570 (nop_convert? (lshift integer_onep @0)) integer_pow2p@1) integer_zerop)
4571 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)))
4572 (icmp @0 { wide_int_to_tree (TREE_TYPE (@0),
4573 wi::exact_log2 (wi::to_wide (@1))); }))))
4575 /* Fold (X {&,^,|} C2) << C1 into (X << C1) {&,^,|} (C2 << C1)
4576 (X {&,^,|} C2) >> C1 into (X >> C1) & (C2 >> C1). */
4577 (for shift (lshift rshift)
4578 (for bit_op (bit_and bit_xor bit_ior)
4580 (shift (convert?:s (bit_op:s @0 INTEGER_CST@2)) INTEGER_CST@1)
4581 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
4582 (with { tree mask = int_const_binop (shift, fold_convert (type, @2), @1); }
4584 (bit_op (shift (convert @0) @1) { mask; })))))))
4586 /* ~(~X >> Y) -> X >> Y (for arithmetic shift). */
4588 (bit_not (convert1?:s (rshift:s (convert2?@0 (bit_not @1)) @2)))
4589 (if (!TYPE_UNSIGNED (TREE_TYPE (@0))
4590 && (element_precision (TREE_TYPE (@0))
4591 <= element_precision (TREE_TYPE (@1))
4592 || !TYPE_UNSIGNED (TREE_TYPE (@1))))
4594 { tree shift_type = TREE_TYPE (@0); }
4595 (convert (rshift (convert:shift_type @1) @2)))))
4597 /* ~(~X >>r Y) -> X >>r Y
4598 ~(~X <<r Y) -> X <<r Y */
4599 (for rotate (lrotate rrotate)
4601 (bit_not (convert1?:s (rotate:s (convert2?@0 (bit_not @1)) @2)))
4602 (if ((element_precision (TREE_TYPE (@0))
4603 <= element_precision (TREE_TYPE (@1))
4604 || !TYPE_UNSIGNED (TREE_TYPE (@1)))
4605 && (element_precision (type) <= element_precision (TREE_TYPE (@0))
4606 || !TYPE_UNSIGNED (TREE_TYPE (@0))))
4608 { tree rotate_type = TREE_TYPE (@0); }
4609 (convert (rotate (convert:rotate_type @1) @2))))))
4612 (for rotate (lrotate rrotate)
4613 invrot (rrotate lrotate)
4614 /* (X >>r Y) cmp (Z >>r Y) may simplify to X cmp Y. */
4616 (cmp (rotate @1 @0) (rotate @2 @0))
4618 /* (X >>r C1) cmp C2 may simplify to X cmp C3. */
4620 (cmp (rotate @0 INTEGER_CST@1) INTEGER_CST@2)
4621 (cmp @0 { const_binop (invrot, TREE_TYPE (@0), @2, @1); }))
4622 /* (X >>r Y) cmp C where C is 0 or ~0, may simplify to X cmp C. */
4624 (cmp (rotate @0 @1) INTEGER_CST@2)
4625 (if (integer_zerop (@2) || integer_all_onesp (@2))
4628 /* Narrow a lshift by constant. */
4630 (convert (lshift:s@0 @1 INTEGER_CST@2))
4631 (if (INTEGRAL_TYPE_P (type)
4632 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
4633 && !integer_zerop (@2)
4634 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0)))
4635 (if (TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (@0))
4636 || wi::ltu_p (wi::to_wide (@2), TYPE_PRECISION (type)))
4637 (lshift (convert @1) @2)
4638 (if (wi::ltu_p (wi::to_wide (@2), TYPE_PRECISION (TREE_TYPE (@0))))
4639 { build_zero_cst (type); }))))
4641 /* Simplifications of conversions. */
4643 /* Basic strip-useless-type-conversions / strip_nops. */
4644 (for cvt (convert view_convert float fix_trunc)
4647 (if ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@0)))
4648 || (GENERIC && type == TREE_TYPE (@0)))
4651 /* Contract view-conversions. */
4653 (view_convert (view_convert @0))
4656 /* For integral conversions with the same precision or pointer
4657 conversions use a NOP_EXPR instead. */
4660 (if ((INTEGRAL_TYPE_P (type) || POINTER_TYPE_P (type))
4661 && (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
4662 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (@0)))
4665 /* Strip inner integral conversions that do not change precision or size, or
4666 zero-extend while keeping the same size (for bool-to-char). */
4668 (view_convert (convert@0 @1))
4669 (if ((INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
4670 && (INTEGRAL_TYPE_P (TREE_TYPE (@1)) || POINTER_TYPE_P (TREE_TYPE (@1)))
4671 && TYPE_SIZE (TREE_TYPE (@0)) == TYPE_SIZE (TREE_TYPE (@1))
4672 && (TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1))
4673 || (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (TREE_TYPE (@1))
4674 && TYPE_UNSIGNED (TREE_TYPE (@1)))))
4677 /* Simplify a view-converted empty or single-element constructor. */
4679 (view_convert CONSTRUCTOR@0)
4681 { tree ctor = (TREE_CODE (@0) == SSA_NAME
4682 ? gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0)) : @0); }
4684 (if (CONSTRUCTOR_NELTS (ctor) == 0)
4685 { build_zero_cst (type); })
4686 (if (CONSTRUCTOR_NELTS (ctor) == 1
4687 && VECTOR_TYPE_P (TREE_TYPE (ctor))
4688 && operand_equal_p (TYPE_SIZE (type),
4689 TYPE_SIZE (TREE_TYPE
4690 (CONSTRUCTOR_ELT (ctor, 0)->value))))
4691 (view_convert { CONSTRUCTOR_ELT (ctor, 0)->value; })))))
4693 /* Re-association barriers around constants and other re-association
4694 barriers can be removed. */
4696 (paren CONSTANT_CLASS_P@0)
4699 (paren (paren@1 @0))
4702 /* Handle cases of two conversions in a row. */
4703 (for ocvt (convert float fix_trunc)
4704 (for icvt (convert float)
4709 tree inside_type = TREE_TYPE (@0);
4710 tree inter_type = TREE_TYPE (@1);
4711 int inside_int = INTEGRAL_TYPE_P (inside_type);
4712 int inside_ptr = POINTER_TYPE_P (inside_type);
4713 int inside_float = FLOAT_TYPE_P (inside_type);
4714 int inside_vec = VECTOR_TYPE_P (inside_type);
4715 unsigned int inside_prec = element_precision (inside_type);
4716 int inside_unsignedp = TYPE_UNSIGNED (inside_type);
4717 int inter_int = INTEGRAL_TYPE_P (inter_type);
4718 int inter_ptr = POINTER_TYPE_P (inter_type);
4719 int inter_float = FLOAT_TYPE_P (inter_type);
4720 int inter_vec = VECTOR_TYPE_P (inter_type);
4721 unsigned int inter_prec = element_precision (inter_type);
4722 int inter_unsignedp = TYPE_UNSIGNED (inter_type);
4723 int final_int = INTEGRAL_TYPE_P (type);
4724 int final_ptr = POINTER_TYPE_P (type);
4725 int final_float = FLOAT_TYPE_P (type);
4726 int final_vec = VECTOR_TYPE_P (type);
4727 unsigned int final_prec = element_precision (type);
4728 int final_unsignedp = TYPE_UNSIGNED (type);
4731 /* In addition to the cases of two conversions in a row
4732 handled below, if we are converting something to its own
4733 type via an object of identical or wider precision, neither
4734 conversion is needed. */
4735 (if (((GIMPLE && useless_type_conversion_p (type, inside_type))
4737 && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (inside_type)))
4738 && (((inter_int || inter_ptr) && final_int)
4739 || (inter_float && final_float))
4740 && inter_prec >= final_prec)
4743 /* Likewise, if the intermediate and initial types are either both
4744 float or both integer, we don't need the middle conversion if the
4745 former is wider than the latter and doesn't change the signedness
4746 (for integers). Avoid this if the final type is a pointer since
4747 then we sometimes need the middle conversion. */
4748 (if (((inter_int && inside_int) || (inter_float && inside_float))
4749 && (final_int || final_float)
4750 && inter_prec >= inside_prec
4751 && (inter_float || inter_unsignedp == inside_unsignedp))
4754 /* If we have a sign-extension of a zero-extended value, we can
4755 replace that by a single zero-extension. Likewise if the
4756 final conversion does not change precision we can drop the
4757 intermediate conversion. Similarly truncation of a sign-extension
4758 can be replaced by a single sign-extension. */
4759 (if (inside_int && inter_int && final_int
4760 && ((inside_prec < inter_prec && inter_prec < final_prec
4761 && inside_unsignedp && !inter_unsignedp)
4762 || final_prec == inter_prec
4763 || (inside_prec < inter_prec && inter_prec > final_prec
4764 && !inside_unsignedp && inter_unsignedp)))
4767 /* Two conversions in a row are not needed unless:
4768 - some conversion is floating-point (overstrict for now), or
4769 - some conversion is a vector (overstrict for now), or
4770 - the intermediate type is narrower than both initial and
4772 - the intermediate type and innermost type differ in signedness,
4773 and the outermost type is wider than the intermediate, or
4774 - the initial type is a pointer type and the precisions of the
4775 intermediate and final types differ, or
4776 - the final type is a pointer type and the precisions of the
4777 initial and intermediate types differ. */
4778 (if (! inside_float && ! inter_float && ! final_float
4779 && ! inside_vec && ! inter_vec && ! final_vec
4780 && (inter_prec >= inside_prec || inter_prec >= final_prec)
4781 && ! (inside_int && inter_int
4782 && inter_unsignedp != inside_unsignedp
4783 && inter_prec < final_prec)
4784 && ((inter_unsignedp && inter_prec > inside_prec)
4785 == (final_unsignedp && final_prec > inter_prec))
4786 && ! (inside_ptr && inter_prec != final_prec)
4787 && ! (final_ptr && inside_prec != inter_prec))
4790 /* `(outer:M)(inter:N) a:O`
4791 can be converted to `(outer:M) a`
4792 if M <= O && N >= O. No matter what signedness of the casts,
4793 as the final is either a truncation from the original or just
4794 a sign change of the type. */
4795 (if (inside_int && inter_int && final_int
4796 && final_prec <= inside_prec
4797 && inter_prec >= inside_prec)
4800 /* A truncation to an unsigned type (a zero-extension) should be
4801 canonicalized as bitwise and of a mask. */
4802 (if (GIMPLE /* PR70366: doing this in GENERIC breaks -Wconversion. */
4803 && final_int && inter_int && inside_int
4804 && final_prec == inside_prec
4805 && final_prec > inter_prec
4807 (convert (bit_and @0 { wide_int_to_tree
4809 wi::mask (inter_prec, false,
4810 TYPE_PRECISION (inside_type))); })))
4812 /* If we are converting an integer to a floating-point that can
4813 represent it exactly and back to an integer, we can skip the
4814 floating-point conversion. */
4815 (if (GIMPLE /* PR66211 */
4816 && inside_int && inter_float && final_int &&
4817 (unsigned) significand_size (TYPE_MODE (inter_type))
4818 >= inside_prec - !inside_unsignedp)
4821 /* (float_type)(integer_type) x -> trunc (x) if the type of x matches
4822 float_type. Only do the transformation if we do not need to preserve
4823 trapping behaviour, so require !flag_trapping_math. */
4826 (float (fix_trunc @0))
4827 (if (!flag_trapping_math
4828 && types_match (type, TREE_TYPE (@0))
4829 && direct_internal_fn_supported_p (IFN_TRUNC, type,
4834 /* If we have a narrowing conversion to an integral type that is fed by a
4835 BIT_AND_EXPR, we might be able to remove the BIT_AND_EXPR if it merely
4836 masks off bits outside the final type (and nothing else). */
4838 (convert (bit_and @0 INTEGER_CST@1))
4839 (if (INTEGRAL_TYPE_P (type)
4840 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
4841 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))
4842 && operand_equal_p (@1, build_low_bits_mask (TREE_TYPE (@1),
4843 TYPE_PRECISION (type)), 0))
4847 /* (X /[ex] A) * A -> X. */
4849 (mult (convert1? (exact_div @0 @@1)) (convert2? @1))
4852 /* Simplify (A / B) * B + (A % B) -> A. */
4853 (for div (trunc_div ceil_div floor_div round_div)
4854 mod (trunc_mod ceil_mod floor_mod round_mod)
4856 (plus:c (mult:c (div @0 @1) @1) (mod @0 @1))
4859 /* x / y * y == x -> x % y == 0. */
4861 (eq:c (mult:c (trunc_div:s @0 @1) @1) @0)
4862 (if (TREE_CODE (TREE_TYPE (@0)) != COMPLEX_TYPE)
4863 (eq (trunc_mod @0 @1) { build_zero_cst (TREE_TYPE (@0)); })))
4865 /* ((X /[ex] A) +- B) * A --> X +- A * B. */
4866 (for op (plus minus)
4868 (mult (convert1? (op (convert2? (exact_div @0 INTEGER_CST@@1)) INTEGER_CST@2)) @1)
4869 (if (tree_nop_conversion_p (type, TREE_TYPE (@2))
4870 && tree_nop_conversion_p (TREE_TYPE (@0), TREE_TYPE (@2)))
4873 wi::overflow_type overflow;
4874 wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@2),
4875 TYPE_SIGN (type), &overflow);
4877 (if (types_match (type, TREE_TYPE (@2))
4878 && types_match (TREE_TYPE (@0), TREE_TYPE (@2)) && !overflow)
4879 (op @0 { wide_int_to_tree (type, mul); })
4880 (with { tree utype = unsigned_type_for (type); }
4881 (convert (op (convert:utype @0)
4882 (mult (convert:utype @1) (convert:utype @2))))))))))
4884 /* Canonicalization of binary operations. */
4886 /* Convert X + -C into X - C. */
4888 (plus @0 REAL_CST@1)
4889 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
4890 (with { tree tem = const_unop (NEGATE_EXPR, type, @1); }
4891 (if (!TREE_OVERFLOW (tem) || !flag_trapping_math)
4892 (minus @0 { tem; })))))
4894 /* Convert x+x into x*2. */
4897 (if (SCALAR_FLOAT_TYPE_P (type))
4898 (mult @0 { build_real (type, dconst2); })
4899 (if (INTEGRAL_TYPE_P (type))
4900 (mult @0 { build_int_cst (type, 2); }))))
4904 (minus integer_zerop @1)
4907 (pointer_diff integer_zerop @1)
4908 (negate (convert @1)))
4910 /* (ARG0 - ARG1) is the same as (-ARG1 + ARG0). So check whether
4911 ARG0 is zero and X + ARG0 reduces to X, since that would mean
4912 (-ARG1 + ARG0) reduces to -ARG1. */
4914 (minus real_zerop@0 @1)
4915 (if (fold_real_zero_addition_p (type, @1, @0, 0))
4918 /* Transform x * -1 into -x. */
4920 (mult @0 integer_minus_onep)
4923 /* Reassociate (X * CST) * Y to (X * Y) * CST. This does not introduce
4924 signed overflow for CST != 0 && CST != -1. */
4926 (mult:c (mult:s@3 @0 INTEGER_CST@1) @2)
4927 (if (TREE_CODE (@2) != INTEGER_CST
4929 && !integer_zerop (@1) && !integer_minus_onep (@1))
4930 (mult (mult @0 @2) @1)))
4932 /* True if we can easily extract the real and imaginary parts of a complex
4934 (match compositional_complex
4935 (convert? (complex @0 @1)))
4937 /* COMPLEX_EXPR and REALPART/IMAGPART_EXPR cancellations. */
4939 (complex (realpart @0) (imagpart @0))
4942 (realpart (complex @0 @1))
4945 (imagpart (complex @0 @1))
4948 /* Sometimes we only care about half of a complex expression. */
4950 (realpart (convert?:s (conj:s @0)))
4951 (convert (realpart @0)))
4953 (imagpart (convert?:s (conj:s @0)))
4954 (convert (negate (imagpart @0))))
4955 (for part (realpart imagpart)
4956 (for op (plus minus)
4958 (part (convert?:s@2 (op:s @0 @1)))
4959 (convert (op (part @0) (part @1))))))
4961 (realpart (convert?:s (CEXPI:s @0)))
4964 (imagpart (convert?:s (CEXPI:s @0)))
4967 /* conj(conj(x)) -> x */
4969 (conj (convert? (conj @0)))
4970 (if (tree_nop_conversion_p (TREE_TYPE (@0), type))
4973 /* conj({x,y}) -> {x,-y} */
4975 (conj (convert?:s (complex:s @0 @1)))
4976 (with { tree itype = TREE_TYPE (type); }
4977 (complex (convert:itype @0) (negate (convert:itype @1)))))
4979 /* BSWAP simplifications, transforms checked by gcc.dg/builtin-bswap-8.c. */
4985 (bswap (bit_not (bswap @0)))
4987 (for bitop (bit_xor bit_ior bit_and)
4989 (bswap (bitop:c (bswap @0) @1))
4990 (bitop @0 (bswap @1))))
4993 (cmp (bswap@2 @0) (bswap @1))
4994 (with { tree ctype = TREE_TYPE (@2); }
4995 (cmp (convert:ctype @0) (convert:ctype @1))))
4997 (cmp (bswap @0) INTEGER_CST@1)
4998 (with { tree ctype = TREE_TYPE (@1); }
4999 (cmp (convert:ctype @0) (bswap! @1)))))
5000 /* (bswap(x) >> C1) & C2 can sometimes be simplified to (x >> C3) & C2. */
5002 (bit_and (convert1? (rshift@0 (convert2? (bswap@4 @1)) INTEGER_CST@2))
5004 (if (BITS_PER_UNIT == 8
5005 && tree_fits_uhwi_p (@2)
5006 && tree_fits_uhwi_p (@3))
5009 unsigned HOST_WIDE_INT prec = TYPE_PRECISION (TREE_TYPE (@4));
5010 unsigned HOST_WIDE_INT bits = tree_to_uhwi (@2);
5011 unsigned HOST_WIDE_INT mask = tree_to_uhwi (@3);
5012 unsigned HOST_WIDE_INT lo = bits & 7;
5013 unsigned HOST_WIDE_INT hi = bits - lo;
5016 && mask < (256u>>lo)
5017 && bits < TYPE_PRECISION (TREE_TYPE(@0)))
5018 (with { unsigned HOST_WIDE_INT ns = (prec - (hi + 8)) + lo; }
5020 (bit_and (convert @1) @3)
5023 tree utype = unsigned_type_for (TREE_TYPE (@1));
5024 tree nst = build_int_cst (integer_type_node, ns);
5026 (bit_and (convert (rshift:utype (convert:utype @1) {nst;})) @3))))))))
5027 /* bswap(x) >> C1 can sometimes be simplified to (T)x >> C2. */
5029 (rshift (convert? (bswap@2 @0)) INTEGER_CST@1)
5030 (if (BITS_PER_UNIT == 8
5031 && CHAR_TYPE_SIZE == 8
5032 && tree_fits_uhwi_p (@1))
5035 unsigned HOST_WIDE_INT prec = TYPE_PRECISION (TREE_TYPE (@2));
5036 unsigned HOST_WIDE_INT bits = tree_to_uhwi (@1);
5037 /* If the bswap was extended before the original shift, this
5038 byte (shift) has the sign of the extension, not the sign of
5039 the original shift. */
5040 tree st = TYPE_PRECISION (type) > prec ? TREE_TYPE (@2) : type;
5042 /* Special case: logical right shift of sign-extended bswap.
5043 (unsigned)(short)bswap16(x)>>12 is (unsigned)((short)x<<8)>>12. */
5044 (if (TYPE_PRECISION (type) > prec
5045 && !TYPE_UNSIGNED (TREE_TYPE (@2))
5046 && TYPE_UNSIGNED (type)
5047 && bits < prec && bits + 8 >= prec)
5048 (with { tree nst = build_int_cst (integer_type_node, prec - 8); }
5049 (rshift (convert (lshift:st (convert:st @0) {nst;})) @1))
5050 (if (bits + 8 == prec)
5051 (if (TYPE_UNSIGNED (st))
5052 (convert (convert:unsigned_char_type_node @0))
5053 (convert (convert:signed_char_type_node @0)))
5054 (if (bits < prec && bits + 8 > prec)
5057 tree nst = build_int_cst (integer_type_node, bits & 7);
5058 tree bt = TYPE_UNSIGNED (st) ? unsigned_char_type_node
5059 : signed_char_type_node;
5061 (convert (rshift:bt (convert:bt @0) {nst;})))))))))
5062 /* bswap(x) & C1 can sometimes be simplified to (x >> C2) & C1. */
5064 (bit_and (convert? (bswap@2 @0)) INTEGER_CST@1)
5065 (if (BITS_PER_UNIT == 8
5066 && tree_fits_uhwi_p (@1)
5067 && tree_to_uhwi (@1) < 256)
5070 unsigned HOST_WIDE_INT prec = TYPE_PRECISION (TREE_TYPE (@2));
5071 tree utype = unsigned_type_for (TREE_TYPE (@0));
5072 tree nst = build_int_cst (integer_type_node, prec - 8);
5074 (bit_and (convert (rshift:utype (convert:utype @0) {nst;})) @1)))))
5077 /* Combine COND_EXPRs and VEC_COND_EXPRs. */
5079 /* Simplify constant conditions.
5080 Only optimize constant conditions when the selected branch
5081 has the same type as the COND_EXPR. This avoids optimizing
5082 away "c ? x : throw", where the throw has a void type.
5083 Note that we cannot throw away the fold-const.cc variant nor
5084 this one as we depend on doing this transform before possibly
5085 A ? B : B -> B triggers and the fold-const.cc one can optimize
5086 0 ? A : B to B even if A has side-effects. Something
5087 genmatch cannot handle. */
5089 (cond INTEGER_CST@0 @1 @2)
5090 (if (integer_zerop (@0))
5091 (if (!VOID_TYPE_P (TREE_TYPE (@2)) || VOID_TYPE_P (type))
5093 (if (!VOID_TYPE_P (TREE_TYPE (@1)) || VOID_TYPE_P (type))
5096 (vec_cond VECTOR_CST@0 @1 @2)
5097 (if (integer_all_onesp (@0))
5099 (if (integer_zerop (@0))
5102 /* Sink unary operations to branches, but only if we do fold both. */
5103 (for op (negate bit_not abs absu)
5105 (op (vec_cond:s @0 @1 @2))
5106 (vec_cond @0 (op! @1) (op! @2))))
5108 /* Sink unary conversions to branches, but only if we do fold both
5109 and the target's truth type is the same as we already have. */
5111 (convert (vec_cond:s @0 @1 @2))
5112 (if (VECTOR_TYPE_P (type)
5113 && types_match (TREE_TYPE (@0), truth_type_for (type)))
5114 (vec_cond @0 (convert! @1) (convert! @2))))
5116 /* Likewise for view_convert of nop_conversions. */
5118 (view_convert (vec_cond:s @0 @1 @2))
5119 (if (VECTOR_TYPE_P (type) && VECTOR_TYPE_P (TREE_TYPE (@1))
5120 && known_eq (TYPE_VECTOR_SUBPARTS (type),
5121 TYPE_VECTOR_SUBPARTS (TREE_TYPE (@1)))
5122 && tree_nop_conversion_p (TREE_TYPE (type), TREE_TYPE (TREE_TYPE (@1))))
5123 (vec_cond @0 (view_convert! @1) (view_convert! @2))))
5125 /* Sink binary operation to branches, but only if we can fold it. */
5126 (for op (tcc_comparison plus minus mult bit_and bit_ior bit_xor
5127 lshift rshift rdiv trunc_div ceil_div floor_div round_div
5128 trunc_mod ceil_mod floor_mod round_mod min max)
5129 /* (c ? a : b) op (c ? d : e) --> c ? (a op d) : (b op e) */
5131 (op (vec_cond:s @0 @1 @2) (vec_cond:s @0 @3 @4))
5132 (vec_cond @0 (op! @1 @3) (op! @2 @4)))
5134 /* (c ? a : b) op d --> c ? (a op d) : (b op d) */
5136 (op (vec_cond:s @0 @1 @2) @3)
5137 (vec_cond @0 (op! @1 @3) (op! @2 @3)))
5139 (op @3 (vec_cond:s @0 @1 @2))
5140 (vec_cond @0 (op! @3 @1) (op! @3 @2))))
5143 (match (nop_atomic_bit_test_and_p @0 @1 @4)
5144 (bit_and (convert?@4 (ATOMIC_FETCH_OR_XOR_N @2 INTEGER_CST@0 @3))
5147 int ibit = tree_log2 (@0);
5148 int ibit2 = tree_log2 (@1);
5152 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))))))
5154 (match (nop_atomic_bit_test_and_p @0 @1 @3)
5155 (bit_and (convert?@3 (SYNC_FETCH_OR_XOR_N @2 INTEGER_CST@0))
5158 int ibit = tree_log2 (@0);
5159 int ibit2 = tree_log2 (@1);
5163 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))))))
5165 (match (nop_atomic_bit_test_and_p @0 @0 @4)
5168 (ATOMIC_FETCH_OR_XOR_N @2 (nop_convert? (lshift@0 integer_onep@5 @6)) @3))
5170 (if (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0)))))
5172 (match (nop_atomic_bit_test_and_p @0 @0 @4)
5175 (SYNC_FETCH_OR_XOR_N @2 (nop_convert? (lshift@0 integer_onep@3 @5))))
5177 (if (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0)))))
5179 (match (nop_atomic_bit_test_and_p @0 @1 @3)
5180 (bit_and@4 (convert?@3 (ATOMIC_FETCH_AND_N @2 INTEGER_CST@0 @5))
5183 int ibit = wi::exact_log2 (wi::zext (wi::bit_not (wi::to_wide (@0)),
5184 TYPE_PRECISION(type)));
5185 int ibit2 = tree_log2 (@1);
5189 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))))))
5191 (match (nop_atomic_bit_test_and_p @0 @1 @3)
5193 (convert?@3 (SYNC_FETCH_AND_AND_N @2 INTEGER_CST@0))
5196 int ibit = wi::exact_log2 (wi::zext (wi::bit_not (wi::to_wide (@0)),
5197 TYPE_PRECISION(type)));
5198 int ibit2 = tree_log2 (@1);
5202 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))))))
5204 (match (nop_atomic_bit_test_and_p @4 @0 @3)
5207 (ATOMIC_FETCH_AND_N @2 (nop_convert?@4 (bit_not (lshift@0 integer_onep@6 @7))) @5))
5209 (if (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@4)))))
5211 (match (nop_atomic_bit_test_and_p @4 @0 @3)
5214 (SYNC_FETCH_AND_AND_N @2 (nop_convert?@4 (bit_not (lshift@0 integer_onep@6 @7)))))
5216 (if (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@4)))))
5220 /* (v ? w : 0) ? a : b is just (v & w) ? a : b
5221 Currently disabled after pass lvec because ARM understands
5222 VEC_COND_EXPR<v==w,-1,0> but not a plain v==w fed to BIT_IOR_EXPR. */
5224 /* These can only be done in gimple as fold likes to convert:
5225 (CMP) & N into (CMP) ? N : 0
5226 and we try to match the same pattern again and again. */
5228 (vec_cond (vec_cond:s @0 @3 integer_zerop) @1 @2)
5229 (if (optimize_vectors_before_lowering_p () && types_match (@0, @3))
5230 (vec_cond (bit_and @0 @3) @1 @2)))
5232 (vec_cond (vec_cond:s @0 integer_all_onesp @3) @1 @2)
5233 (if (optimize_vectors_before_lowering_p () && types_match (@0, @3))
5234 (vec_cond (bit_ior @0 @3) @1 @2)))
5236 (vec_cond (vec_cond:s @0 integer_zerop @3) @1 @2)
5237 (if (optimize_vectors_before_lowering_p () && types_match (@0, @3))
5238 (vec_cond (bit_ior @0 (bit_not @3)) @2 @1)))
5240 (vec_cond (vec_cond:s @0 @3 integer_all_onesp) @1 @2)
5241 (if (optimize_vectors_before_lowering_p () && types_match (@0, @3))
5242 (vec_cond (bit_and @0 (bit_not @3)) @2 @1)))
5244 /* ((VCE (a cmp b ? -1 : 0)) < 0) ? c : d is just
5245 (VCE ((a cmp b) ? (VCE c) : (VCE d))) when TYPE_PRECISION of the
5246 component type of the outer vec_cond is greater equal the inner one. */
5247 (for cmp (simple_comparison)
5250 (lt (view_convert@5 (vec_cond@6 (cmp@4 @0 @1)
5253 integer_zerop) @2 @3)
5254 (if (VECTOR_INTEGER_TYPE_P (TREE_TYPE (@0))
5255 && VECTOR_INTEGER_TYPE_P (TREE_TYPE (@5))
5256 && !TYPE_UNSIGNED (TREE_TYPE (@5))
5257 && VECTOR_TYPE_P (TREE_TYPE (@6))
5258 && VECTOR_TYPE_P (type)
5259 && tree_int_cst_le (TYPE_SIZE (TREE_TYPE (type)),
5260 TYPE_SIZE (TREE_TYPE (TREE_TYPE (@6))))
5261 && TYPE_SIZE (type) == TYPE_SIZE (TREE_TYPE (@6)))
5262 (with { tree vtype = TREE_TYPE (@6);}
5264 (vec_cond @4 (view_convert:vtype @2) (view_convert:vtype @3)))))))
5266 /* c1 ? c2 ? a : b : b --> (c1 & c2) ? a : b */
5268 (vec_cond @0 (vec_cond:s @1 @2 @3) @3)
5269 (if (optimize_vectors_before_lowering_p () && types_match (@0, @1))
5270 (vec_cond (bit_and @0 @1) @2 @3)))
5272 (vec_cond @0 @2 (vec_cond:s @1 @2 @3))
5273 (if (optimize_vectors_before_lowering_p () && types_match (@0, @1))
5274 (vec_cond (bit_ior @0 @1) @2 @3)))
5276 (vec_cond @0 (vec_cond:s @1 @2 @3) @2)
5277 (if (optimize_vectors_before_lowering_p () && types_match (@0, @1))
5278 (vec_cond (bit_ior (bit_not @0) @1) @2 @3)))
5280 (vec_cond @0 @3 (vec_cond:s @1 @2 @3))
5281 (if (optimize_vectors_before_lowering_p () && types_match (@0, @1))
5282 (vec_cond (bit_and (bit_not @0) @1) @2 @3)))
5285 /* Canonicalize mask ? { 0, ... } : { -1, ...} to ~mask if the mask
5286 types are compatible. */
5288 (vec_cond @0 VECTOR_CST@1 VECTOR_CST@2)
5289 (if (VECTOR_BOOLEAN_TYPE_P (type)
5290 && types_match (type, TREE_TYPE (@0)))
5291 (if (integer_zerop (@1) && integer_all_onesp (@2))
5293 (if (integer_all_onesp (@1) && integer_zerop (@2))
5296 /* A few simplifications of "a ? CST1 : CST2". */
5297 /* NOTE: Only do this on gimple as the if-chain-to-switch
5298 optimization depends on the gimple to have if statements in it. */
5301 (cond @0 INTEGER_CST@1 INTEGER_CST@2)
5303 (if (integer_zerop (@2))
5305 /* a ? 1 : 0 -> a if 0 and 1 are integral types. */
5306 (if (integer_onep (@1))
5307 (convert (convert:boolean_type_node @0)))
5308 /* a ? -1 : 0 -> -a. */
5309 (if (INTEGRAL_TYPE_P (type) && integer_all_onesp (@1))
5310 (if (TYPE_PRECISION (type) == 1)
5311 /* For signed 1-bit precision just cast bool to the type. */
5312 (convert (convert:boolean_type_node @0))
5313 (if (TREE_CODE (type) == BOOLEAN_TYPE)
5315 tree intt = build_nonstandard_integer_type (TYPE_PRECISION (type),
5316 TYPE_UNSIGNED (type));
5318 (convert (negate (convert:intt (convert:boolean_type_node @0)))))
5319 (negate (convert:type (convert:boolean_type_node @0))))))
5320 /* a ? powerof2cst : 0 -> a << (log2(powerof2cst)) */
5321 (if (INTEGRAL_TYPE_P (type) && integer_pow2p (@1))
5323 tree shift = build_int_cst (integer_type_node, tree_log2 (@1));
5325 (lshift (convert (convert:boolean_type_node @0)) { shift; })))))
5326 (if (integer_zerop (@1))
5328 /* a ? 0 : 1 -> !a. */
5329 (if (integer_onep (@2))
5330 (convert (bit_xor (convert:boolean_type_node @0) { boolean_true_node; })))
5331 /* a ? 0 : -1 -> -(!a). */
5332 (if (INTEGRAL_TYPE_P (type) && integer_all_onesp (@2))
5333 (if (TYPE_PRECISION (type) == 1)
5334 /* For signed 1-bit precision just cast bool to the type. */
5335 (convert (bit_xor (convert:boolean_type_node @0) { boolean_true_node; }))
5336 (if (TREE_CODE (type) == BOOLEAN_TYPE)
5338 tree intt = build_nonstandard_integer_type (TYPE_PRECISION (type),
5339 TYPE_UNSIGNED (type));
5341 (convert (negate (convert:intt (bit_xor (convert:boolean_type_node @0)
5342 { boolean_true_node; })))))
5343 (negate (convert:type (bit_xor (convert:boolean_type_node @0)
5344 { boolean_true_node; }))))))
5345 /* a ? 0 : powerof2cst -> (!a) << (log2(powerof2cst)) */
5346 (if (INTEGRAL_TYPE_P (type) && integer_pow2p (@2))
5348 tree shift = build_int_cst (integer_type_node, tree_log2 (@2));
5350 (lshift (convert (bit_xor (convert:boolean_type_node @0)
5351 { boolean_true_node; })) { shift; })))))))
5353 /* (a > 1) ? 0 : (cast)a is the same as (cast)(a == 1)
5354 for unsigned types. */
5356 (cond (gt @0 integer_onep@1) integer_zerop (convert? @2))
5357 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
5358 && bitwise_equal_p (@0, @2))
5359 (convert (eq @0 @1))
5363 /* (a <= 1) & (cast)a is the same as (cast)(a == 1)
5364 for unsigned types. */
5366 (bit_and:c (convert1? (le @0 integer_onep@1)) (convert2? @2))
5367 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
5368 && bitwise_equal_p (@0, @2))
5369 (convert (eq @0 @1))
5373 /* `(a == CST) & a` can be simplified to `0` or `(a == CST)` depending
5374 on the first bit of the CST. */
5376 (bit_and:c (convert@2 (eq @0 INTEGER_CST@1)) (convert? @0))
5377 (if ((wi::to_wide (@1) & 1) != 0)
5379 { build_zero_cst (type); }))
5382 # x_5 in range [cst1, cst2] where cst2 = cst1 + 1
5383 x_5 == cstN ? cst4 : cst3
5384 # op is == or != and N is 1 or 2
5385 to r_6 = x_5 + (min (cst3, cst4) - cst1) or
5386 r_6 = (min (cst3, cst4) + cst1) - x_5 depending on op, N and which
5387 of cst3 and cst4 is smaller.
5388 This was originally done by two_value_replacement in phiopt (PR 88676). */
5391 (cond (eqne SSA_NAME@0 INTEGER_CST@1) INTEGER_CST@2 INTEGER_CST@3)
5392 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
5393 && INTEGRAL_TYPE_P (type)
5394 && (wi::to_widest (@2) + 1 == wi::to_widest (@3)
5395 || wi::to_widest (@2) == wi::to_widest (@3) + 1))
5398 get_range_query (cfun)->range_of_expr (r, @0);
5399 if (r.undefined_p ())
5400 r.set_varying (TREE_TYPE (@0));
5402 wide_int min = r.lower_bound ();
5403 wide_int max = r.upper_bound ();
5406 && (wi::to_wide (@1) == min
5407 || wi::to_wide (@1) == max))
5409 tree arg0 = @2, arg1 = @3;
5411 if ((eqne == EQ_EXPR) ^ (wi::to_wide (@1) == min))
5412 std::swap (arg0, arg1);
5413 if (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type))
5414 type1 = TREE_TYPE (@0);
5417 auto prec = TYPE_PRECISION (type1);
5418 auto unsign = TYPE_UNSIGNED (type1);
5419 if (TREE_CODE (type1) == BOOLEAN_TYPE)
5420 type1 = build_nonstandard_integer_type (prec, unsign);
5421 min = wide_int::from (min, prec,
5422 TYPE_SIGN (TREE_TYPE (@0)));
5423 wide_int a = wide_int::from (wi::to_wide (arg0), prec,
5425 enum tree_code code;
5426 wi::overflow_type ovf;
5427 if (tree_int_cst_lt (arg0, arg1))
5433 /* lhs is known to be in range [min, min+1] and we want to add a
5434 to it. Check if that operation can overflow for those 2 values
5435 and if yes, force unsigned type. */
5436 wi::add (min + (wi::neg_p (a) ? 0 : 1), a, SIGNED, &ovf);
5438 type1 = unsigned_type_for (type1);
5447 /* lhs is known to be in range [min, min+1] and we want to subtract
5448 it from a. Check if that operation can overflow for those 2
5449 values and if yes, force unsigned type. */
5450 wi::sub (a, min + (wi::neg_p (min) ? 0 : 1), SIGNED, &ovf);
5452 type1 = unsigned_type_for (type1);
5455 tree arg = wide_int_to_tree (type1, a);
5457 (if (code == PLUS_EXPR)
5458 (convert (plus (convert:type1 @0) { arg; }))
5459 (convert (minus { arg; } (convert:type1 @0))))))))))
5463 (convert (cond@0 @1 INTEGER_CST@2 INTEGER_CST@3))
5464 (if (INTEGRAL_TYPE_P (type)
5465 && INTEGRAL_TYPE_P (TREE_TYPE (@0)))
5466 (cond @1 (convert @2) (convert @3))))
5468 /* Simplification moved from fold_cond_expr_with_comparison. It may also
5470 /* This pattern implements two kinds simplification:
5473 (cond (cmp (convert1? x) c1) (convert2? x) c2) -> (minmax (x c)) if:
5474 1) Conversions are type widening from smaller type.
5475 2) Const c1 equals to c2 after canonicalizing comparison.
5476 3) Comparison has tree code LT, LE, GT or GE.
5477 This specific pattern is needed when (cmp (convert x) c) may not
5478 be simplified by comparison patterns because of multiple uses of
5479 x. It also makes sense here because simplifying across multiple
5480 referred var is always benefitial for complicated cases.
5483 (cond (eq (convert1? x) c1) (convert2? x) c2) -> (cond (eq x c1) c1 c2). */
5484 (for cmp (lt le gt ge eq ne)
5486 (cond (cmp (convert1? @1) INTEGER_CST@3) (convert2? @1) INTEGER_CST@2)
5489 tree from_type = TREE_TYPE (@1);
5490 tree c1_type = TREE_TYPE (@3), c2_type = TREE_TYPE (@2);
5491 enum tree_code code = ERROR_MARK;
5493 if (INTEGRAL_TYPE_P (from_type)
5494 && int_fits_type_p (@2, from_type)
5495 && (types_match (c1_type, from_type)
5496 || (TYPE_PRECISION (c1_type) > TYPE_PRECISION (from_type)
5497 && (TYPE_UNSIGNED (from_type)
5498 || TYPE_SIGN (c1_type) == TYPE_SIGN (from_type))))
5499 && (types_match (c2_type, from_type)
5500 || (TYPE_PRECISION (c2_type) > TYPE_PRECISION (from_type)
5501 && (TYPE_UNSIGNED (from_type)
5502 || TYPE_SIGN (c2_type) == TYPE_SIGN (from_type)))))
5505 code = minmax_from_comparison (cmp, @1, @3, @1, @2);
5506 /* Can do A == C1 ? A : C2 -> A == C1 ? C1 : C2? */
5507 else if (int_fits_type_p (@3, from_type))
5511 (if (code == MAX_EXPR)
5512 (convert (max @1 (convert @2)))
5513 (if (code == MIN_EXPR)
5514 (convert (min @1 (convert @2)))
5515 (if (code == EQ_EXPR)
5516 (convert (cond (eq @1 (convert @3))
5517 (convert:from_type @3) (convert:from_type @2)))))))))
5519 /* (cond (cmp (convert? x) c1) (op x c2) c3) -> (op (minmax x c1) c2) if:
5521 1) OP is PLUS or MINUS.
5522 2) CMP is LT, LE, GT or GE.
5523 3) C3 == (C1 op C2), and computation doesn't have undefined behavior.
5525 This pattern also handles special cases like:
5527 A) Operand x is a unsigned to signed type conversion and c1 is
5528 integer zero. In this case,
5529 (signed type)x < 0 <=> x > MAX_VAL(signed type)
5530 (signed type)x >= 0 <=> x <= MAX_VAL(signed type)
5531 B) Const c1 may not equal to (C3 op' C2). In this case we also
5532 check equality for (c1+1) and (c1-1) by adjusting comparison
5535 TODO: Though signed type is handled by this pattern, it cannot be
5536 simplified at the moment because C standard requires additional
5537 type promotion. In order to match&simplify it here, the IR needs
5538 to be cleaned up by other optimizers, i.e, VRP. */
5539 (for op (plus minus)
5540 (for cmp (lt le gt ge)
5542 (cond (cmp (convert? @X) INTEGER_CST@1) (op @X INTEGER_CST@2) INTEGER_CST@3)
5543 (with { tree from_type = TREE_TYPE (@X), to_type = TREE_TYPE (@1); }
5544 (if (types_match (from_type, to_type)
5545 /* Check if it is special case A). */
5546 || (TYPE_UNSIGNED (from_type)
5547 && !TYPE_UNSIGNED (to_type)
5548 && TYPE_PRECISION (from_type) == TYPE_PRECISION (to_type)
5549 && integer_zerop (@1)
5550 && (cmp == LT_EXPR || cmp == GE_EXPR)))
5553 wi::overflow_type overflow = wi::OVF_NONE;
5554 enum tree_code code, cmp_code = cmp;
5556 wide_int c1 = wi::to_wide (@1);
5557 wide_int c2 = wi::to_wide (@2);
5558 wide_int c3 = wi::to_wide (@3);
5559 signop sgn = TYPE_SIGN (from_type);
5561 /* Handle special case A), given x of unsigned type:
5562 ((signed type)x < 0) <=> (x > MAX_VAL(signed type))
5563 ((signed type)x >= 0) <=> (x <= MAX_VAL(signed type)) */
5564 if (!types_match (from_type, to_type))
5566 if (cmp_code == LT_EXPR)
5568 if (cmp_code == GE_EXPR)
5570 c1 = wi::max_value (to_type);
5572 /* To simplify this pattern, we require c3 = (c1 op c2). Here we
5573 compute (c3 op' c2) and check if it equals to c1 with op' being
5574 the inverted operator of op. Make sure overflow doesn't happen
5575 if it is undefined. */
5576 if (op == PLUS_EXPR)
5577 real_c1 = wi::sub (c3, c2, sgn, &overflow);
5579 real_c1 = wi::add (c3, c2, sgn, &overflow);
5582 if (!overflow || !TYPE_OVERFLOW_UNDEFINED (from_type))
5584 /* Check if c1 equals to real_c1. Boundary condition is handled
5585 by adjusting comparison operation if necessary. */
5586 if (!wi::cmp (wi::sub (real_c1, 1, sgn, &overflow), c1, sgn)
5589 /* X <= Y - 1 equals to X < Y. */
5590 if (cmp_code == LE_EXPR)
5592 /* X > Y - 1 equals to X >= Y. */
5593 if (cmp_code == GT_EXPR)
5596 if (!wi::cmp (wi::add (real_c1, 1, sgn, &overflow), c1, sgn)
5599 /* X < Y + 1 equals to X <= Y. */
5600 if (cmp_code == LT_EXPR)
5602 /* X >= Y + 1 equals to X > Y. */
5603 if (cmp_code == GE_EXPR)
5606 if (code != cmp_code || !wi::cmp (real_c1, c1, sgn))
5608 if (cmp_code == LT_EXPR || cmp_code == LE_EXPR)
5610 if (cmp_code == GT_EXPR || cmp_code == GE_EXPR)
5615 (if (code == MAX_EXPR)
5616 (op (max @X { wide_int_to_tree (from_type, real_c1); })
5617 { wide_int_to_tree (from_type, c2); })
5618 (if (code == MIN_EXPR)
5619 (op (min @X { wide_int_to_tree (from_type, real_c1); })
5620 { wide_int_to_tree (from_type, c2); })))))))))
5623 /* A >= B ? A : B -> max (A, B) and friends. The code is still
5624 in fold_cond_expr_with_comparison for GENERIC folding with
5625 some extra constraints. */
5626 (for cmp (eq ne le lt unle unlt ge gt unge ungt uneq ltgt)
5628 (cond (cmp:c (nop_convert1?@c0 @0) (nop_convert2?@c1 @1))
5629 (convert3? @0) (convert4? @1))
5630 (if (!HONOR_SIGNED_ZEROS (type)
5631 && (/* Allow widening conversions of the compare operands as data. */
5632 (INTEGRAL_TYPE_P (type)
5633 && types_match (TREE_TYPE (@c0), TREE_TYPE (@0))
5634 && types_match (TREE_TYPE (@c1), TREE_TYPE (@1))
5635 && TYPE_PRECISION (TREE_TYPE (@0)) <= TYPE_PRECISION (type)
5636 && TYPE_PRECISION (TREE_TYPE (@1)) <= TYPE_PRECISION (type))
5637 /* Or sign conversions for the comparison. */
5638 || (types_match (type, TREE_TYPE (@0))
5639 && types_match (type, TREE_TYPE (@1)))))
5641 (if (cmp == EQ_EXPR)
5642 (if (VECTOR_TYPE_P (type))
5645 (if (cmp == NE_EXPR)
5646 (if (VECTOR_TYPE_P (type))
5649 (if (cmp == LE_EXPR || cmp == UNLE_EXPR || cmp == LT_EXPR || cmp == UNLT_EXPR)
5650 (if (!HONOR_NANS (type))
5651 (if (VECTOR_TYPE_P (type))
5652 (view_convert (min @c0 @c1))
5653 (convert (min @c0 @c1)))))
5654 (if (cmp == GE_EXPR || cmp == UNGE_EXPR || cmp == GT_EXPR || cmp == UNGT_EXPR)
5655 (if (!HONOR_NANS (type))
5656 (if (VECTOR_TYPE_P (type))
5657 (view_convert (max @c0 @c1))
5658 (convert (max @c0 @c1)))))
5659 (if (cmp == UNEQ_EXPR)
5660 (if (!HONOR_NANS (type))
5661 (if (VECTOR_TYPE_P (type))
5664 (if (cmp == LTGT_EXPR)
5665 (if (!HONOR_NANS (type))
5666 (if (VECTOR_TYPE_P (type))
5668 (convert @c0))))))))
5671 (for cnd (cond vec_cond)
5672 /* (a != b) ? (a - b) : 0 -> (a - b) */
5674 (cnd (ne:c @0 @1) (minus@2 @0 @1) integer_zerop)
5676 /* (a != b) ? (a ^ b) : 0 -> (a ^ b) */
5678 (cnd (ne:c @0 @1) (bit_xor:c@2 @0 @1) integer_zerop)
5680 /* (a != b) ? (a & b) : a -> (a & b) */
5681 /* (a != b) ? (a | b) : a -> (a | b) */
5682 /* (a != b) ? min(a,b) : a -> min(a,b) */
5683 /* (a != b) ? max(a,b) : a -> max(a,b) */
5684 (for op (bit_and bit_ior min max)
5686 (cnd (ne:c @0 @1) (op:c@2 @0 @1) @0)
5688 /* (a != b) ? (a * b) : (a * a) -> (a * b) */
5689 /* (a != b) ? (a + b) : (a + a) -> (a + b) */
5692 (cnd (ne:c @0 @1) (op@2 @0 @1) (op @0 @0))
5693 (if (ANY_INTEGRAL_TYPE_P (type))
5695 /* (a != b) ? (a + b) : (2 * a) -> (a + b) */
5697 (cnd (ne:c @0 @1) (plus@2 @0 @1) (mult @0 uniform_integer_cst_p@3))
5698 (if (wi::to_wide (uniform_integer_cst_p (@3)) == 2)
5702 /* These was part of minmax phiopt. */
5703 /* Optimize (a CMP b) ? minmax<a, c> : minmax<b, c>
5704 to minmax<min/max<a, b>, c> */
5705 (for minmax (min max)
5706 (for cmp (lt le gt ge ne)
5708 (cond (cmp:c @1 @3) (minmax:c @1 @4) (minmax:c @2 @4))
5711 tree_code code = minmax_from_comparison (cmp, @1, @2, @1, @3);
5713 (if (code == MIN_EXPR)
5714 (minmax (min @1 @2) @4)
5715 (if (code == MAX_EXPR)
5716 (minmax (max @1 @2) @4)))))))
5718 /* Optimize (a CMP CST1) ? max<a,CST2> : a */
5719 (for cmp (gt ge lt le)
5720 minmax (min min max max)
5722 (cond (cmp:c @0 @1) (minmax:c@2 @0 @3) @4)
5725 tree_code code = minmax_from_comparison (cmp, @0, @1, @0, @4);
5727 (if ((cmp == LT_EXPR || cmp == LE_EXPR)
5729 && integer_nonzerop (fold_build2 (LE_EXPR, boolean_type_node, @3, @4)))
5731 (if ((cmp == GT_EXPR || cmp == GE_EXPR)
5733 && integer_nonzerop (fold_build2 (GE_EXPR, boolean_type_node, @3, @4)))
5737 /* These patterns should be after min/max detection as simplifications
5738 of `(type)(zero_one ==/!= 0)` to `(type)(zero_one)`
5739 and `(type)(zero_one^1)` are not done yet. See PR 110637.
5740 Even without those, reaching min/max/and/ior faster is better. */
5742 (cond @0 zero_one_valued_p@1 zero_one_valued_p@2)
5744 /* bool0 ? bool1 : 0 -> bool0 & bool1 */
5745 (if (integer_zerop (@2))
5746 (bit_and (convert @0) @1))
5747 /* bool0 ? 0 : bool2 -> (bool0^1) & bool2 */
5748 (if (integer_zerop (@1))
5749 (bit_and (bit_xor (convert @0) { build_one_cst (type); } ) @2))
5750 /* bool0 ? 1 : bool2 -> bool0 | bool2 */
5751 (if (integer_onep (@1))
5752 (bit_ior (convert @0) @2))
5753 /* bool0 ? bool1 : 1 -> (bool0^1) | bool1 */
5754 (if (integer_onep (@2))
5755 (bit_ior (bit_xor (convert @0) @2) @1))
5760 /* X != C1 ? -X : C2 simplifies to -X when -C1 == C2. */
5762 (cond (ne @0 INTEGER_CST@1) (negate@3 @0) INTEGER_CST@2)
5763 (if (!TYPE_SATURATING (type)
5764 && (TYPE_OVERFLOW_WRAPS (type)
5765 || !wi::only_sign_bit_p (wi::to_wide (@1)))
5766 && wi::eq_p (wi::neg (wi::to_wide (@1)), wi::to_wide (@2)))
5769 /* X != C1 ? ~X : C2 simplifies to ~X when ~C1 == C2. */
5771 (cond (ne @0 INTEGER_CST@1) (bit_not@3 @0) INTEGER_CST@2)
5772 (if (wi::eq_p (wi::bit_not (wi::to_wide (@1)), wi::to_wide (@2)))
5775 /* X != C1 ? abs(X) : C2 simplifies to abs(x) when abs(C1) == C2. */
5778 (cond (ne @0 INTEGER_CST@1) (op@3 @0) INTEGER_CST@2)
5779 (if (wi::abs (wi::to_wide (@1)) == wi::to_wide (@2))
5780 (if (op != ABSU_EXPR && wi::only_sign_bit_p (wi::to_wide (@1)))
5781 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
5782 (convert (absu:utype @0)))
5785 /* (X + 1) > Y ? -X : 1 simplifies to X >= Y ? -X : 1 when
5786 X is unsigned, as when X + 1 overflows, X is -1, so -X == 1. */
5788 (cond (gt (plus @0 integer_onep) @1) (negate @0) integer_onep@2)
5789 (if (TYPE_UNSIGNED (type))
5790 (cond (ge @0 @1) (negate @0) @2)))
5792 (for cnd (cond vec_cond)
5793 /* A ? B : (A ? X : C) -> A ? B : C. */
5795 (cnd @0 (cnd @0 @1 @2) @3)
5798 (cnd @0 @1 (cnd @0 @2 @3))
5800 /* A ? B : (!A ? C : X) -> A ? B : C. */
5801 /* ??? This matches embedded conditions open-coded because genmatch
5802 would generate matching code for conditions in separate stmts only.
5803 The following is still important to merge then and else arm cases
5804 from if-conversion. */
5806 (cnd @0 @1 (cnd @2 @3 @4))
5807 (if (inverse_conditions_p (@0, @2))
5810 (cnd @0 (cnd @1 @2 @3) @4)
5811 (if (inverse_conditions_p (@0, @1))
5814 /* A ? B : B -> B. */
5819 /* !A ? B : C -> A ? C : B. */
5821 (cnd (logical_inverted_value truth_valued_p@0) @1 @2)
5824 /* abs/negative simplifications moved from fold_cond_expr_with_comparison.
5826 None of these transformations work for modes with signed
5827 zeros. If A is +/-0, the first two transformations will
5828 change the sign of the result (from +0 to -0, or vice
5829 versa). The last four will fix the sign of the result,
5830 even though the original expressions could be positive or
5831 negative, depending on the sign of A.
5833 Note that all these transformations are correct if A is
5834 NaN, since the two alternatives (A and -A) are also NaNs. */
5836 (for cnd (cond vec_cond)
5837 /* A == 0 ? A : -A same as -A */
5840 (cnd (cmp @0 zerop) @2 (negate@1 @2))
5841 (if (!HONOR_SIGNED_ZEROS (type)
5842 && bitwise_equal_p (@0, @2))
5845 (cnd (cmp @0 zerop) zerop (negate@1 @2))
5846 (if (!HONOR_SIGNED_ZEROS (type)
5847 && bitwise_equal_p (@0, @2))
5850 /* A != 0 ? A : -A same as A */
5853 (cnd (cmp @0 zerop) @1 (negate @1))
5854 (if (!HONOR_SIGNED_ZEROS (type)
5855 && bitwise_equal_p (@0, @1))
5858 (cnd (cmp @0 zerop) @1 integer_zerop)
5859 (if (!HONOR_SIGNED_ZEROS (type)
5860 && bitwise_equal_p (@0, @1))
5863 /* A >=/> 0 ? A : -A same as abs (A) */
5866 (cnd (cmp @0 zerop) @1 (negate @1))
5867 (if (!HONOR_SIGNED_ZEROS (TREE_TYPE(@0))
5868 && !TYPE_UNSIGNED (TREE_TYPE(@0))
5869 && bitwise_equal_p (@0, @1))
5870 (if (TYPE_UNSIGNED (type))
5873 /* A <=/< 0 ? A : -A same as -abs (A) */
5876 (cnd (cmp @0 zerop) @1 (negate @1))
5877 (if (!HONOR_SIGNED_ZEROS (TREE_TYPE(@0))
5878 && !TYPE_UNSIGNED (TREE_TYPE(@0))
5879 && bitwise_equal_p (@0, @1))
5880 (if ((ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
5881 && !TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
5882 || TYPE_UNSIGNED (type))
5884 tree utype = unsigned_type_for (TREE_TYPE(@0));
5886 (convert (negate (absu:utype @0))))
5887 (negate (abs @0)))))
5890 /* (A - B) == 0 ? (A - B) : (B - A) same as (B - A) */
5893 (cnd (cmp (minus@0 @1 @2) zerop) @0 (minus@3 @2 @1))
5894 (if (!HONOR_SIGNED_ZEROS (type))
5897 (cnd (cmp (minus@0 @1 @2) integer_zerop) integer_zerop (minus@3 @2 @1))
5900 /* (A - B) != 0 ? (A - B) : (B - A) same as (A - B) */
5903 (cnd (cmp (minus@0 @1 @2) zerop) @0 (minus @2 @1))
5904 (if (!HONOR_SIGNED_ZEROS (type))
5907 (cnd (cmp (minus@0 @1 @2) integer_zerop) @0 integer_zerop)
5910 /* (A - B) >=/> 0 ? (A - B) : (B - A) same as abs (A - B) */
5913 (cnd (cmp (minus@0 @1 @2) zerop) @0 (minus @2 @1))
5914 (if (!HONOR_SIGNED_ZEROS (type)
5915 && !TYPE_UNSIGNED (type))
5917 /* (A - B) <=/< 0 ? (A - B) : (B - A) same as -abs (A - B) */
5920 (cnd (cmp (minus@0 @1 @2) zerop) @0 (minus @2 @1))
5921 (if (!HONOR_SIGNED_ZEROS (type)
5922 && !TYPE_UNSIGNED (type))
5923 (if (ANY_INTEGRAL_TYPE_P (type)
5924 && !TYPE_OVERFLOW_WRAPS (type))
5926 tree utype = unsigned_type_for (type);
5928 (convert (negate (absu:utype @0))))
5929 (negate (abs @0)))))
5933 /* -(type)!A -> (type)A - 1. */
5935 (negate (convert?:s (logical_inverted_value:s @0)))
5936 (if (INTEGRAL_TYPE_P (type)
5937 && TREE_CODE (type) != BOOLEAN_TYPE
5938 && TYPE_PRECISION (type) > 1
5939 && TREE_CODE (@0) == SSA_NAME
5940 && ssa_name_has_boolean_range (@0))
5941 (plus (convert:type @0) { build_all_ones_cst (type); })))
5943 /* A + (B vcmp C ? 1 : 0) -> A - (B vcmp C ? -1 : 0), since vector comparisons
5944 return all -1 or all 0 results. */
5945 /* ??? We could instead convert all instances of the vec_cond to negate,
5946 but that isn't necessarily a win on its own. */
5948 (plus:c @3 (view_convert? (vec_cond:s @0 integer_each_onep@1 integer_zerop@2)))
5949 (if (VECTOR_TYPE_P (type)
5950 && known_eq (TYPE_VECTOR_SUBPARTS (type),
5951 TYPE_VECTOR_SUBPARTS (TREE_TYPE (@1)))
5952 && (TYPE_MODE (TREE_TYPE (type))
5953 == TYPE_MODE (TREE_TYPE (TREE_TYPE (@1)))))
5954 (minus @3 (view_convert (vec_cond @0 (negate @1) @2)))))
5956 /* ... likewise A - (B vcmp C ? 1 : 0) -> A + (B vcmp C ? -1 : 0). */
5958 (minus @3 (view_convert? (vec_cond:s @0 integer_each_onep@1 integer_zerop@2)))
5959 (if (VECTOR_TYPE_P (type)
5960 && known_eq (TYPE_VECTOR_SUBPARTS (type),
5961 TYPE_VECTOR_SUBPARTS (TREE_TYPE (@1)))
5962 && (TYPE_MODE (TREE_TYPE (type))
5963 == TYPE_MODE (TREE_TYPE (TREE_TYPE (@1)))))
5964 (plus @3 (view_convert (vec_cond @0 (negate @1) @2)))))
5967 /* Simplifications of comparisons. */
5969 /* See if we can reduce the magnitude of a constant involved in a
5970 comparison by changing the comparison code. This is a canonicalization
5971 formerly done by maybe_canonicalize_comparison_1. */
5975 (cmp @0 uniform_integer_cst_p@1)
5976 (with { tree cst = uniform_integer_cst_p (@1); }
5977 (if (tree_int_cst_sgn (cst) == -1)
5978 (acmp @0 { build_uniform_cst (TREE_TYPE (@1),
5979 wide_int_to_tree (TREE_TYPE (cst),
5985 (cmp @0 uniform_integer_cst_p@1)
5986 (with { tree cst = uniform_integer_cst_p (@1); }
5987 (if (tree_int_cst_sgn (cst) == 1)
5988 (acmp @0 { build_uniform_cst (TREE_TYPE (@1),
5989 wide_int_to_tree (TREE_TYPE (cst),
5990 wi::to_wide (cst) - 1)); })))))
5992 /* We can simplify a logical negation of a comparison to the
5993 inverted comparison. As we cannot compute an expression
5994 operator using invert_tree_comparison we have to simulate
5995 that with expression code iteration. */
5996 (for cmp (tcc_comparison)
5997 icmp (inverted_tcc_comparison)
5998 ncmp (inverted_tcc_comparison_with_nans)
5999 /* Ideally we'd like to combine the following two patterns
6000 and handle some more cases by using
6001 (logical_inverted_value (cmp @0 @1))
6002 here but for that genmatch would need to "inline" that.
6003 For now implement what forward_propagate_comparison did. */
6005 (bit_not (cmp @0 @1))
6006 (if (VECTOR_TYPE_P (type)
6007 || (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1))
6008 /* Comparison inversion may be impossible for trapping math,
6009 invert_tree_comparison will tell us. But we can't use
6010 a computed operator in the replacement tree thus we have
6011 to play the trick below. */
6012 (with { enum tree_code ic = invert_tree_comparison
6013 (cmp, HONOR_NANS (@0)); }
6019 (bit_xor (cmp @0 @1) integer_truep)
6020 (with { enum tree_code ic = invert_tree_comparison
6021 (cmp, HONOR_NANS (@0)); }
6026 /* The following bits are handled by fold_binary_op_with_conditional_arg. */
6028 (ne (cmp@2 @0 @1) integer_zerop)
6029 (if (types_match (type, TREE_TYPE (@2)))
6032 (eq (cmp@2 @0 @1) integer_truep)
6033 (if (types_match (type, TREE_TYPE (@2)))
6036 (ne (cmp@2 @0 @1) integer_truep)
6037 (if (types_match (type, TREE_TYPE (@2)))
6038 (with { enum tree_code ic = invert_tree_comparison
6039 (cmp, HONOR_NANS (@0)); }
6045 (eq (cmp@2 @0 @1) integer_zerop)
6046 (if (types_match (type, TREE_TYPE (@2)))
6047 (with { enum tree_code ic = invert_tree_comparison
6048 (cmp, HONOR_NANS (@0)); }
6054 /* Transform comparisons of the form X - Y CMP 0 to X CMP Y.
6055 ??? The transformation is valid for the other operators if overflow
6056 is undefined for the type, but performing it here badly interacts
6057 with the transformation in fold_cond_expr_with_comparison which
6058 attempts to synthetize ABS_EXPR. */
6060 (for sub (minus pointer_diff)
6062 (cmp (sub@2 @0 @1) integer_zerop)
6063 (if (single_use (@2))
6066 /* Simplify (x < 0) ^ (y < 0) to (x ^ y) < 0 and
6067 (x >= 0) ^ (y >= 0) to (x ^ y) < 0. */
6070 (bit_xor (cmp:s @0 integer_zerop) (cmp:s @1 integer_zerop))
6071 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6072 && !TYPE_UNSIGNED (TREE_TYPE (@0))
6073 && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
6074 (lt (bit_xor @0 @1) { build_zero_cst (TREE_TYPE (@0)); }))))
6075 /* Simplify (x < 0) ^ (y >= 0) to (x ^ y) >= 0 and
6076 (x >= 0) ^ (y < 0) to (x ^ y) >= 0. */
6078 (bit_xor:c (lt:s @0 integer_zerop) (ge:s @1 integer_zerop))
6079 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6080 && !TYPE_UNSIGNED (TREE_TYPE (@0))
6081 && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
6082 (ge (bit_xor @0 @1) { build_zero_cst (TREE_TYPE (@0)); })))
6084 /* Transform comparisons of the form X * C1 CMP 0 to X CMP 0 in the
6085 signed arithmetic case. That form is created by the compiler
6086 often enough for folding it to be of value. One example is in
6087 computing loop trip counts after Operator Strength Reduction. */
6088 (for cmp (simple_comparison)
6089 scmp (swapped_simple_comparison)
6091 (cmp (mult@3 @0 INTEGER_CST@1) integer_zerop@2)
6092 /* Handle unfolded multiplication by zero. */
6093 (if (integer_zerop (@1))
6095 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
6096 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
6098 /* If @1 is negative we swap the sense of the comparison. */
6099 (if (tree_int_cst_sgn (@1) < 0)
6103 /* For integral types with undefined overflow fold
6104 x * C1 == C2 into x == C2 / C1 or false.
6105 If overflow wraps and C1 is odd, simplify to x == C2 / C1 in the ring
6109 (cmp (mult @0 INTEGER_CST@1) INTEGER_CST@2)
6110 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6111 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
6112 && wi::to_wide (@1) != 0)
6113 (with { widest_int quot; }
6114 (if (wi::multiple_of_p (wi::to_widest (@2), wi::to_widest (@1),
6115 TYPE_SIGN (TREE_TYPE (@0)), "))
6116 (cmp @0 { wide_int_to_tree (TREE_TYPE (@0), quot); })
6117 { constant_boolean_node (cmp == NE_EXPR, type); }))
6118 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6119 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))
6120 && (wi::bit_and (wi::to_wide (@1), 1) == 1))
6123 tree itype = TREE_TYPE (@0);
6124 int p = TYPE_PRECISION (itype);
6125 wide_int m = wi::one (p + 1) << p;
6126 wide_int a = wide_int::from (wi::to_wide (@1), p + 1, UNSIGNED);
6127 wide_int i = wide_int::from (wi::mod_inv (a, m),
6128 p, TYPE_SIGN (itype));
6129 wide_int_to_tree (itype, wi::mul (i, wi::to_wide (@2)));
6132 /* Simplify comparison of something with itself. For IEEE
6133 floating-point, we can only do some of these simplifications. */
6137 (if (! FLOAT_TYPE_P (TREE_TYPE (@0))
6138 || ! tree_expr_maybe_nan_p (@0))
6139 { constant_boolean_node (true, type); }
6141 /* With -ftrapping-math conversion to EQ loses an exception. */
6142 && (! FLOAT_TYPE_P (TREE_TYPE (@0))
6143 || ! flag_trapping_math))
6149 || ! FLOAT_TYPE_P (TREE_TYPE (@0))
6150 || ! tree_expr_maybe_nan_p (@0))
6151 { constant_boolean_node (false, type); })))
6152 (for cmp (unle unge uneq)
6155 { constant_boolean_node (true, type); }))
6156 (for cmp (unlt ungt)
6162 (if (!flag_trapping_math || !tree_expr_maybe_nan_p (@0))
6163 { constant_boolean_node (false, type); }))
6165 /* x == ~x -> false */
6166 /* x != ~x -> true */
6169 (cmp:c @0 (bit_not @0))
6170 { constant_boolean_node (cmp == NE_EXPR, type); }))
6172 /* Fold ~X op ~Y as Y op X. */
6173 (for cmp (simple_comparison)
6175 (cmp (nop_convert1?@4 (bit_not@2 @0)) (nop_convert2? (bit_not@3 @1)))
6176 (if (single_use (@2) && single_use (@3))
6177 (with { tree otype = TREE_TYPE (@4); }
6178 (cmp (convert:otype @1) (convert:otype @0))))))
6180 /* Fold ~X op C as X op' ~C, where op' is the swapped comparison. */
6181 (for cmp (simple_comparison)
6182 scmp (swapped_simple_comparison)
6184 (cmp (nop_convert? (bit_not@2 @0)) CONSTANT_CLASS_P@1)
6185 (if (single_use (@2)
6186 && (TREE_CODE (@1) == INTEGER_CST || TREE_CODE (@1) == VECTOR_CST))
6187 (with { tree otype = TREE_TYPE (@1); }
6188 (scmp (convert:otype @0) (bit_not @1))))))
6190 (for cmp (simple_comparison)
6193 /* IEEE doesn't distinguish +0 and -0 in comparisons. */
6195 /* a CMP (-0) -> a CMP 0 */
6196 (if (REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@1)))
6197 (cmp @0 { build_real (TREE_TYPE (@1), dconst0); }))
6198 /* (-0) CMP b -> 0 CMP b. */
6199 (if (TREE_CODE (@0) == REAL_CST
6200 && REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@0)))
6201 (cmp { build_real (TREE_TYPE (@0), dconst0); } @1))
6202 /* x != NaN is always true, other ops are always false. */
6203 (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
6204 && (cmp == EQ_EXPR || cmp == NE_EXPR || !flag_trapping_math)
6205 && !tree_expr_signaling_nan_p (@1)
6206 && !tree_expr_maybe_signaling_nan_p (@0))
6207 { constant_boolean_node (cmp == NE_EXPR, type); })
6208 /* NaN != y is always true, other ops are always false. */
6209 (if (TREE_CODE (@0) == REAL_CST
6210 && REAL_VALUE_ISNAN (TREE_REAL_CST (@0))
6211 && (cmp == EQ_EXPR || cmp == NE_EXPR || !flag_trapping_math)
6212 && !tree_expr_signaling_nan_p (@0)
6213 && !tree_expr_signaling_nan_p (@1))
6214 { constant_boolean_node (cmp == NE_EXPR, type); })
6215 /* Fold comparisons against infinity. */
6216 (if (REAL_VALUE_ISINF (TREE_REAL_CST (@1))
6217 && MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (@1))))
6220 REAL_VALUE_TYPE max;
6221 enum tree_code code = cmp;
6222 bool neg = REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1));
6224 code = swap_tree_comparison (code);
6227 /* x > +Inf is always false, if we ignore NaNs or exceptions. */
6228 (if (code == GT_EXPR
6229 && !(HONOR_NANS (@0) && flag_trapping_math))
6230 { constant_boolean_node (false, type); })
6231 (if (code == LE_EXPR)
6232 /* x <= +Inf is always true, if we don't care about NaNs. */
6233 (if (! HONOR_NANS (@0))
6234 { constant_boolean_node (true, type); }
6235 /* x <= +Inf is the same as x == x, i.e. !isnan(x), but this loses
6236 an "invalid" exception. */
6237 (if (!flag_trapping_math)
6239 /* x == +Inf and x >= +Inf are always equal to x > DBL_MAX, but
6240 for == this introduces an exception for x a NaN. */
6241 (if ((code == EQ_EXPR && !(HONOR_NANS (@0) && flag_trapping_math))
6243 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
6245 (lt @0 { build_real (TREE_TYPE (@0), max); })
6246 (gt @0 { build_real (TREE_TYPE (@0), max); }))))
6247 /* x < +Inf is always equal to x <= DBL_MAX. */
6248 (if (code == LT_EXPR)
6249 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
6251 (ge @0 { build_real (TREE_TYPE (@0), max); })
6252 (le @0 { build_real (TREE_TYPE (@0), max); }))))
6253 /* x != +Inf is always equal to !(x > DBL_MAX), but this introduces
6254 an exception for x a NaN so use an unordered comparison. */
6255 (if (code == NE_EXPR)
6256 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
6257 (if (! HONOR_NANS (@0))
6259 (ge @0 { build_real (TREE_TYPE (@0), max); })
6260 (le @0 { build_real (TREE_TYPE (@0), max); }))
6262 (unge @0 { build_real (TREE_TYPE (@0), max); })
6263 (unle @0 { build_real (TREE_TYPE (@0), max); }))))))))))
6265 /* If this is a comparison of a real constant with a PLUS_EXPR
6266 or a MINUS_EXPR of a real constant, we can convert it into a
6267 comparison with a revised real constant as long as no overflow
6268 occurs when unsafe_math_optimizations are enabled. */
6269 (if (flag_unsafe_math_optimizations)
6270 (for op (plus minus)
6272 (cmp (op @0 REAL_CST@1) REAL_CST@2)
6275 tree tem = const_binop (op == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR,
6276 TREE_TYPE (@1), @2, @1);
6278 (if (tem && !TREE_OVERFLOW (tem))
6279 (cmp @0 { tem; }))))))
6281 /* Likewise, we can simplify a comparison of a real constant with
6282 a MINUS_EXPR whose first operand is also a real constant, i.e.
6283 (c1 - x) < c2 becomes x > c1-c2. Reordering is allowed on
6284 floating-point types only if -fassociative-math is set. */
6285 (if (flag_associative_math)
6287 (cmp (minus REAL_CST@0 @1) REAL_CST@2)
6288 (with { tree tem = const_binop (MINUS_EXPR, TREE_TYPE (@1), @0, @2); }
6289 (if (tem && !TREE_OVERFLOW (tem))
6290 (cmp { tem; } @1)))))
6292 /* Fold comparisons against built-in math functions. */
6293 (if (flag_unsafe_math_optimizations && ! flag_errno_math)
6296 (cmp (sq @0) REAL_CST@1)
6298 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
6300 /* sqrt(x) < y is always false, if y is negative. */
6301 (if (cmp == EQ_EXPR || cmp == LT_EXPR || cmp == LE_EXPR)
6302 { constant_boolean_node (false, type); })
6303 /* sqrt(x) > y is always true, if y is negative and we
6304 don't care about NaNs, i.e. negative values of x. */
6305 (if (cmp == NE_EXPR || !HONOR_NANS (@0))
6306 { constant_boolean_node (true, type); })
6307 /* sqrt(x) > y is the same as x >= 0, if y is negative. */
6308 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })))
6309 (if (real_equal (TREE_REAL_CST_PTR (@1), &dconst0))
6311 /* sqrt(x) < 0 is always false. */
6312 (if (cmp == LT_EXPR)
6313 { constant_boolean_node (false, type); })
6314 /* sqrt(x) >= 0 is always true if we don't care about NaNs. */
6315 (if (cmp == GE_EXPR && !HONOR_NANS (@0))
6316 { constant_boolean_node (true, type); })
6317 /* sqrt(x) <= 0 -> x == 0. */
6318 (if (cmp == LE_EXPR)
6320 /* Otherwise sqrt(x) cmp 0 -> x cmp 0. Here cmp can be >=, >,
6321 == or !=. In the last case:
6323 (sqrt(x) != 0) == (NaN != 0) == true == (x != 0)
6325 if x is negative or NaN. Due to -funsafe-math-optimizations,
6326 the results for other x follow from natural arithmetic. */
6328 (if ((cmp == LT_EXPR
6332 && !REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
6333 /* Give up for -frounding-math. */
6334 && !HONOR_SIGN_DEPENDENT_ROUNDING (TREE_TYPE (@0)))
6338 enum tree_code ncmp = cmp;
6339 const real_format *fmt
6340 = REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (@0)));
6341 real_arithmetic (&c2, MULT_EXPR,
6342 &TREE_REAL_CST (@1), &TREE_REAL_CST (@1));
6343 real_convert (&c2, fmt, &c2);
6344 /* See PR91734: if c2 is inexact and sqrt(c2) < c (or sqrt(c2) >= c),
6345 then change LT_EXPR into LE_EXPR or GE_EXPR into GT_EXPR. */
6346 if (!REAL_VALUE_ISINF (c2))
6348 tree c3 = fold_const_call (CFN_SQRT, TREE_TYPE (@0),
6349 build_real (TREE_TYPE (@0), c2));
6350 if (c3 == NULL_TREE || TREE_CODE (c3) != REAL_CST)
6352 else if ((cmp == LT_EXPR || cmp == GE_EXPR)
6353 && real_less (&TREE_REAL_CST (c3), &TREE_REAL_CST (@1)))
6354 ncmp = cmp == LT_EXPR ? LE_EXPR : GT_EXPR;
6355 else if ((cmp == LE_EXPR || cmp == GT_EXPR)
6356 && real_less (&TREE_REAL_CST (@1), &TREE_REAL_CST (c3)))
6357 ncmp = cmp == LE_EXPR ? LT_EXPR : GE_EXPR;
6360 /* With rounding to even, sqrt of up to 3 different values
6361 gives the same normal result, so in some cases c2 needs
6363 REAL_VALUE_TYPE c2alt, tow;
6364 if (cmp == LT_EXPR || cmp == GE_EXPR)
6368 real_nextafter (&c2alt, fmt, &c2, &tow);
6369 real_convert (&c2alt, fmt, &c2alt);
6370 if (REAL_VALUE_ISINF (c2alt))
6374 c3 = fold_const_call (CFN_SQRT, TREE_TYPE (@0),
6375 build_real (TREE_TYPE (@0), c2alt));
6376 if (c3 == NULL_TREE || TREE_CODE (c3) != REAL_CST)
6378 else if (real_equal (&TREE_REAL_CST (c3),
6379 &TREE_REAL_CST (@1)))
6385 (if (cmp == GT_EXPR || cmp == GE_EXPR)
6386 (if (REAL_VALUE_ISINF (c2))
6387 /* sqrt(x) > y is x == +Inf, when y is very large. */
6388 (if (HONOR_INFINITIES (@0))
6389 (eq @0 { build_real (TREE_TYPE (@0), c2); })
6390 { constant_boolean_node (false, type); })
6391 /* sqrt(x) > c is the same as x > c*c. */
6392 (if (ncmp != ERROR_MARK)
6393 (if (ncmp == GE_EXPR)
6394 (ge @0 { build_real (TREE_TYPE (@0), c2); })
6395 (gt @0 { build_real (TREE_TYPE (@0), c2); }))))
6396 /* else if (cmp == LT_EXPR || cmp == LE_EXPR) */
6397 (if (REAL_VALUE_ISINF (c2))
6399 /* sqrt(x) < y is always true, when y is a very large
6400 value and we don't care about NaNs or Infinities. */
6401 (if (! HONOR_NANS (@0) && ! HONOR_INFINITIES (@0))
6402 { constant_boolean_node (true, type); })
6403 /* sqrt(x) < y is x != +Inf when y is very large and we
6404 don't care about NaNs. */
6405 (if (! HONOR_NANS (@0))
6406 (ne @0 { build_real (TREE_TYPE (@0), c2); }))
6407 /* sqrt(x) < y is x >= 0 when y is very large and we
6408 don't care about Infinities. */
6409 (if (! HONOR_INFINITIES (@0))
6410 (ge @0 { build_real (TREE_TYPE (@0), dconst0); }))
6411 /* sqrt(x) < y is x >= 0 && x != +Inf, when y is large. */
6414 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
6415 (ne @0 { build_real (TREE_TYPE (@0), c2); }))))
6416 /* sqrt(x) < c is the same as x < c*c, if we ignore NaNs. */
6417 (if (ncmp != ERROR_MARK && ! HONOR_NANS (@0))
6418 (if (ncmp == LT_EXPR)
6419 (lt @0 { build_real (TREE_TYPE (@0), c2); })
6420 (le @0 { build_real (TREE_TYPE (@0), c2); }))
6421 /* sqrt(x) < c is the same as x >= 0 && x < c*c. */
6422 (if (ncmp != ERROR_MARK && GENERIC)
6423 (if (ncmp == LT_EXPR)
6425 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
6426 (lt @0 { build_real (TREE_TYPE (@0), c2); }))
6428 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
6429 (le @0 { build_real (TREE_TYPE (@0), c2); })))))))))))
6430 /* Transform sqrt(x) cmp sqrt(y) -> x cmp y. */
6432 (cmp (sq @0) (sq @1))
6433 (if (! HONOR_NANS (@0))
6436 /* Optimize various special cases of (FTYPE) N CMP (FTYPE) M. */
6437 (for cmp (lt le eq ne ge gt unordered ordered unlt unle ungt unge uneq ltgt)
6438 icmp (lt le eq ne ge gt unordered ordered lt le gt ge eq ne)
6440 (cmp (float@0 @1) (float @2))
6441 (if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (@0))
6442 && ! DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0)))
6445 format_helper fmt (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (@0))));
6446 tree type1 = TREE_TYPE (@1);
6447 bool type1_signed_p = TYPE_SIGN (type1) == SIGNED;
6448 tree type2 = TREE_TYPE (@2);
6449 bool type2_signed_p = TYPE_SIGN (type2) == SIGNED;
6451 (if (fmt.can_represent_integral_type_p (type1)
6452 && fmt.can_represent_integral_type_p (type2))
6453 (if (cmp == ORDERED_EXPR || cmp == UNORDERED_EXPR)
6454 { constant_boolean_node (cmp == ORDERED_EXPR, type); }
6455 (if (TYPE_PRECISION (type1) > TYPE_PRECISION (type2)
6456 && type1_signed_p >= type2_signed_p)
6457 (icmp @1 (convert @2))
6458 (if (TYPE_PRECISION (type1) < TYPE_PRECISION (type2)
6459 && type1_signed_p <= type2_signed_p)
6460 (icmp (convert:type2 @1) @2)
6461 (if (TYPE_PRECISION (type1) == TYPE_PRECISION (type2)
6462 && type1_signed_p == type2_signed_p)
6463 (icmp @1 @2))))))))))
6465 /* Optimize various special cases of (FTYPE) N CMP CST. */
6466 (for cmp (lt le eq ne ge gt)
6467 icmp (le le eq ne ge ge)
6469 (cmp (float @0) REAL_CST@1)
6470 (if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (@1))
6471 && ! DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1)))
6474 tree itype = TREE_TYPE (@0);
6475 format_helper fmt (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (@1))));
6476 const REAL_VALUE_TYPE *cst = TREE_REAL_CST_PTR (@1);
6477 /* Be careful to preserve any potential exceptions due to
6478 NaNs. qNaNs are ok in == or != context.
6479 TODO: relax under -fno-trapping-math or
6480 -fno-signaling-nans. */
6482 = real_isnan (cst) && (cst->signalling
6483 || (cmp != EQ_EXPR && cmp != NE_EXPR));
6485 /* TODO: allow non-fitting itype and SNaNs when
6486 -fno-trapping-math. */
6487 (if (fmt.can_represent_integral_type_p (itype) && ! exception_p)
6490 signop isign = TYPE_SIGN (itype);
6491 REAL_VALUE_TYPE imin, imax;
6492 real_from_integer (&imin, fmt, wi::min_value (itype), isign);
6493 real_from_integer (&imax, fmt, wi::max_value (itype), isign);
6495 REAL_VALUE_TYPE icst;
6496 if (cmp == GT_EXPR || cmp == GE_EXPR)
6497 real_ceil (&icst, fmt, cst);
6498 else if (cmp == LT_EXPR || cmp == LE_EXPR)
6499 real_floor (&icst, fmt, cst);
6501 real_trunc (&icst, fmt, cst);
6503 bool cst_int_p = !real_isnan (cst) && real_identical (&icst, cst);
6505 bool overflow_p = false;
6507 = real_to_integer (&icst, &overflow_p, TYPE_PRECISION (itype));
6510 /* Optimize cases when CST is outside of ITYPE's range. */
6511 (if (real_compare (LT_EXPR, cst, &imin))
6512 { constant_boolean_node (cmp == GT_EXPR || cmp == GE_EXPR || cmp == NE_EXPR,
6514 (if (real_compare (GT_EXPR, cst, &imax))
6515 { constant_boolean_node (cmp == LT_EXPR || cmp == LE_EXPR || cmp == NE_EXPR,
6517 /* Remove cast if CST is an integer representable by ITYPE. */
6519 (cmp @0 { gcc_assert (!overflow_p);
6520 wide_int_to_tree (itype, icst_val); })
6522 /* When CST is fractional, optimize
6523 (FTYPE) N == CST -> 0
6524 (FTYPE) N != CST -> 1. */
6525 (if (cmp == EQ_EXPR || cmp == NE_EXPR)
6526 { constant_boolean_node (cmp == NE_EXPR, type); })
6527 /* Otherwise replace with sensible integer constant. */
6530 gcc_checking_assert (!overflow_p);
6532 (icmp @0 { wide_int_to_tree (itype, icst_val); })))))))))
6534 /* Fold A /[ex] B CMP C to A CMP B * C. */
6537 (cmp (exact_div @0 @1) INTEGER_CST@2)
6538 (if (!integer_zerop (@1))
6539 (if (wi::to_wide (@2) == 0)
6541 (if (TREE_CODE (@1) == INTEGER_CST)
6544 wi::overflow_type ovf;
6545 wide_int prod = wi::mul (wi::to_wide (@2), wi::to_wide (@1),
6546 TYPE_SIGN (TREE_TYPE (@1)), &ovf);
6549 { constant_boolean_node (cmp == NE_EXPR, type); }
6550 (cmp @0 { wide_int_to_tree (TREE_TYPE (@0), prod); }))))))))
6551 (for cmp (lt le gt ge)
6553 (cmp (exact_div @0 INTEGER_CST@1) INTEGER_CST@2)
6554 (if (wi::gt_p (wi::to_wide (@1), 0, TYPE_SIGN (TREE_TYPE (@1))))
6557 wi::overflow_type ovf;
6558 wide_int prod = wi::mul (wi::to_wide (@2), wi::to_wide (@1),
6559 TYPE_SIGN (TREE_TYPE (@1)), &ovf);
6562 { constant_boolean_node (wi::lt_p (wi::to_wide (@2), 0,
6563 TYPE_SIGN (TREE_TYPE (@2)))
6564 != (cmp == LT_EXPR || cmp == LE_EXPR), type); }
6565 (cmp @0 { wide_int_to_tree (TREE_TYPE (@0), prod); }))))))
6567 /* Fold (size_t)(A /[ex] B) CMP C to (size_t)A CMP (size_t)B * C or A CMP' 0.
6569 For small C (less than max/B), this is (size_t)A CMP (size_t)B * C.
6570 For large C (more than min/B+2^size), this is also true, with the
6571 multiplication computed modulo 2^size.
6572 For intermediate C, this just tests the sign of A. */
6573 (for cmp (lt le gt ge)
6576 (cmp (convert (exact_div @0 INTEGER_CST@1)) INTEGER_CST@2)
6577 (if (tree_nop_conversion_p (TREE_TYPE (@0), TREE_TYPE (@2))
6578 && TYPE_UNSIGNED (TREE_TYPE (@2)) && !TYPE_UNSIGNED (TREE_TYPE (@0))
6579 && wi::gt_p (wi::to_wide (@1), 0, TYPE_SIGN (TREE_TYPE (@1))))
6582 tree utype = TREE_TYPE (@2);
6583 wide_int denom = wi::to_wide (@1);
6584 wide_int right = wi::to_wide (@2);
6585 wide_int smax = wi::sdiv_trunc (wi::max_value (TREE_TYPE (@0)), denom);
6586 wide_int smin = wi::sdiv_trunc (wi::min_value (TREE_TYPE (@0)), denom);
6587 bool small = wi::leu_p (right, smax);
6588 bool large = wi::geu_p (right, smin);
6590 (if (small || large)
6591 (cmp (convert:utype @0) (mult @2 (convert @1)))
6592 (cmp2 @0 { build_zero_cst (TREE_TYPE (@0)); }))))))
6594 /* Unordered tests if either argument is a NaN. */
6596 (bit_ior (unordered @0 @0) (unordered @1 @1))
6597 (if (types_match (@0, @1))
6600 (bit_and (ordered @0 @0) (ordered @1 @1))
6601 (if (types_match (@0, @1))
6604 (bit_ior:c (unordered @0 @0) (unordered:c@2 @0 @1))
6607 (bit_and:c (ordered @0 @0) (ordered:c@2 @0 @1))
6610 /* A & (2**N - 1) <= 2**K - 1 -> A & (2**N - 2**K) == 0
6611 A & (2**N - 1) > 2**K - 1 -> A & (2**N - 2**K) != 0
6613 Note that comparisons
6614 A & (2**N - 1) < 2**K -> A & (2**N - 2**K) == 0
6615 A & (2**N - 1) >= 2**K -> A & (2**N - 2**K) != 0
6616 will be canonicalized to above so there's no need to
6623 (cmp (bit_and@0 @1 INTEGER_CST@2) INTEGER_CST@3)
6624 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)))
6627 tree ty = TREE_TYPE (@0);
6628 unsigned prec = TYPE_PRECISION (ty);
6629 wide_int mask = wi::to_wide (@2, prec);
6630 wide_int rhs = wi::to_wide (@3, prec);
6631 signop sgn = TYPE_SIGN (ty);
6633 (if ((mask & (mask + 1)) == 0 && wi::gt_p (rhs, 0, sgn)
6634 && (rhs & (rhs + 1)) == 0 && wi::ge_p (mask, rhs, sgn))
6635 (eqcmp (bit_and @1 { wide_int_to_tree (ty, mask - rhs); })
6636 { build_zero_cst (ty); }))))))
6638 /* -A CMP -B -> B CMP A. */
6639 (for cmp (tcc_comparison)
6640 scmp (swapped_tcc_comparison)
6642 (cmp (negate @0) (negate @1))
6643 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
6644 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
6647 || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))))
6650 (cmp (negate @0) CONSTANT_CLASS_P@1)
6651 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
6652 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
6655 || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))))
6656 (with { tree tem = const_unop (NEGATE_EXPR, TREE_TYPE (@0), @1); }
6657 (if (tem && !TREE_OVERFLOW (tem))
6658 (scmp @0 { tem; }))))))
6660 /* Convert ABS[U]_EXPR<x> == 0 or ABS[U]_EXPR<x> != 0 to x == 0 or x != 0. */
6664 (eqne (op @0) zerop@1)
6665 (eqne @0 { build_zero_cst (TREE_TYPE (@0)); }))))
6667 /* From fold_sign_changed_comparison and fold_widened_comparison.
6668 FIXME: the lack of symmetry is disturbing. */
6669 (for cmp (simple_comparison)
6671 (cmp (convert@0 @00) (convert?@1 @10))
6672 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6673 /* Disable this optimization if we're casting a function pointer
6674 type on targets that require function pointer canonicalization. */
6675 && !(targetm.have_canonicalize_funcptr_for_compare ()
6676 && ((POINTER_TYPE_P (TREE_TYPE (@00))
6677 && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@00))))
6678 || (POINTER_TYPE_P (TREE_TYPE (@10))
6679 && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@10))))))
6681 (if (TYPE_PRECISION (TREE_TYPE (@00)) == TYPE_PRECISION (TREE_TYPE (@0))
6682 && (TREE_CODE (@10) == INTEGER_CST
6684 && (TYPE_UNSIGNED (TREE_TYPE (@00)) == TYPE_UNSIGNED (TREE_TYPE (@0))
6687 && !POINTER_TYPE_P (TREE_TYPE (@00))
6688 /* (int)bool:32 != (int)uint is not the same as
6689 bool:32 != (bool:32)uint since boolean types only have two valid
6690 values independent of their precision. */
6691 && (TREE_CODE (TREE_TYPE (@00)) != BOOLEAN_TYPE
6692 || TREE_CODE (TREE_TYPE (@10)) == BOOLEAN_TYPE))
6693 /* ??? The special-casing of INTEGER_CST conversion was in the original
6694 code and here to avoid a spurious overflow flag on the resulting
6695 constant which fold_convert produces. */
6696 (if (TREE_CODE (@1) == INTEGER_CST)
6697 (cmp @00 { force_fit_type (TREE_TYPE (@00),
6698 wide_int::from (wi::to_wide (@1),
6699 MAX (TYPE_PRECISION (TREE_TYPE (@1)),
6700 TYPE_PRECISION (TREE_TYPE (@00))),
6701 TYPE_SIGN (TREE_TYPE (@1))),
6702 0, TREE_OVERFLOW (@1)); })
6703 (cmp @00 (convert @1)))
6705 (if (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (TREE_TYPE (@00)))
6706 /* If possible, express the comparison in the shorter mode. */
6707 (if ((cmp == EQ_EXPR || cmp == NE_EXPR
6708 || TYPE_UNSIGNED (TREE_TYPE (@0)) == TYPE_UNSIGNED (TREE_TYPE (@00))
6709 || (!TYPE_UNSIGNED (TREE_TYPE (@0))
6710 && TYPE_UNSIGNED (TREE_TYPE (@00))))
6711 && (types_match (TREE_TYPE (@10), TREE_TYPE (@00))
6712 || ((TYPE_PRECISION (TREE_TYPE (@00))
6713 >= TYPE_PRECISION (TREE_TYPE (@10)))
6714 && (TYPE_UNSIGNED (TREE_TYPE (@00))
6715 == TYPE_UNSIGNED (TREE_TYPE (@10))))
6716 || (TREE_CODE (@10) == INTEGER_CST
6717 && INTEGRAL_TYPE_P (TREE_TYPE (@00))
6718 && int_fits_type_p (@10, TREE_TYPE (@00)))))
6719 (cmp @00 (convert @10))
6720 (if (TREE_CODE (@10) == INTEGER_CST
6721 && INTEGRAL_TYPE_P (TREE_TYPE (@00))
6722 && !int_fits_type_p (@10, TREE_TYPE (@00)))
6725 tree min = lower_bound_in_type (TREE_TYPE (@10), TREE_TYPE (@00));
6726 tree max = upper_bound_in_type (TREE_TYPE (@10), TREE_TYPE (@00));
6727 bool above = integer_nonzerop (const_binop (LT_EXPR, type, max, @10));
6728 bool below = integer_nonzerop (const_binop (LT_EXPR, type, @10, min));
6730 (if (above || below)
6731 (if (cmp == EQ_EXPR || cmp == NE_EXPR)
6732 { constant_boolean_node (cmp == EQ_EXPR ? false : true, type); }
6733 (if (cmp == LT_EXPR || cmp == LE_EXPR)
6734 { constant_boolean_node (above ? true : false, type); }
6735 (if (cmp == GT_EXPR || cmp == GE_EXPR)
6736 { constant_boolean_node (above ? false : true, type); })))))))))
6737 /* Fold (double)float1 CMP (double)float2 into float1 CMP float2. */
6738 (if (FLOAT_TYPE_P (TREE_TYPE (@00))
6739 && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0))
6740 == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@00)))
6741 && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0))
6742 == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@10))))
6745 tree type1 = TREE_TYPE (@10);
6746 if (TREE_CODE (@10) == REAL_CST && !DECIMAL_FLOAT_TYPE_P (type1))
6748 REAL_VALUE_TYPE orig = TREE_REAL_CST (@10);
6749 if (TYPE_PRECISION (type1) > TYPE_PRECISION (float_type_node)
6750 && exact_real_truncate (TYPE_MODE (float_type_node), &orig))
6751 type1 = float_type_node;
6752 if (TYPE_PRECISION (type1) > TYPE_PRECISION (double_type_node)
6753 && exact_real_truncate (TYPE_MODE (double_type_node), &orig))
6754 type1 = double_type_node;
6757 = (element_precision (TREE_TYPE (@00)) > element_precision (type1)
6758 ? TREE_TYPE (@00) : type1);
6760 (if (element_precision (TREE_TYPE (@0)) > element_precision (newtype))
6761 (cmp (convert:newtype @00) (convert:newtype @10))))))))
6766 /* SSA names are canonicalized to 2nd place. */
6767 (cmp addr@0 SSA_NAME@1)
6770 poly_int64 off; tree base;
6771 tree addr = (TREE_CODE (@0) == SSA_NAME
6772 ? gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0)) : @0);
6774 /* A local variable can never be pointed to by
6775 the default SSA name of an incoming parameter. */
6776 (if (SSA_NAME_IS_DEFAULT_DEF (@1)
6777 && TREE_CODE (SSA_NAME_VAR (@1)) == PARM_DECL
6778 && (base = get_base_address (TREE_OPERAND (addr, 0)))
6779 && TREE_CODE (base) == VAR_DECL
6780 && auto_var_in_fn_p (base, current_function_decl))
6781 (if (cmp == NE_EXPR)
6782 { constant_boolean_node (true, type); }
6783 { constant_boolean_node (false, type); })
6784 /* If the address is based on @1 decide using the offset. */
6785 (if ((base = get_addr_base_and_unit_offset (TREE_OPERAND (addr, 0), &off))
6786 && TREE_CODE (base) == MEM_REF
6787 && TREE_OPERAND (base, 0) == @1)
6788 (with { off += mem_ref_offset (base).force_shwi (); }
6789 (if (known_ne (off, 0))
6790 { constant_boolean_node (cmp == NE_EXPR, type); }
6791 (if (known_eq (off, 0))
6792 { constant_boolean_node (cmp == EQ_EXPR, type); }))))))))
6794 /* Equality compare simplifications from fold_binary */
6797 /* If we have (A | C) == D where C & ~D != 0, convert this into 0.
6798 Similarly for NE_EXPR. */
6800 (cmp (convert?@3 (bit_ior @0 INTEGER_CST@1)) INTEGER_CST@2)
6801 (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0))
6802 && wi::bit_and_not (wi::to_wide (@1), wi::to_wide (@2)) != 0)
6803 { constant_boolean_node (cmp == NE_EXPR, type); }))
6805 /* (X ^ Y) == 0 becomes X == Y, and (X ^ Y) != 0 becomes X != Y. */
6807 (cmp (bit_xor @0 @1) integer_zerop)
6810 /* (X ^ Y) == Y becomes X == 0.
6811 Likewise (X ^ Y) == X becomes Y == 0. */
6813 (cmp:c (bit_xor:c @0 @1) @0)
6814 (cmp @1 { build_zero_cst (TREE_TYPE (@1)); }))
6816 /* (X & Y) == X becomes (X & ~Y) == 0. */
6818 (cmp:c (bit_and:c @0 @1) @0)
6819 (cmp (bit_and @0 (bit_not! @1)) { build_zero_cst (TREE_TYPE (@0)); }))
6821 (cmp:c (convert@3 (bit_and (convert@2 @0) INTEGER_CST@1)) (convert @0))
6822 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6823 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
6824 && INTEGRAL_TYPE_P (TREE_TYPE (@3))
6825 && TYPE_PRECISION (TREE_TYPE (@2)) == TYPE_PRECISION (TREE_TYPE (@0))
6826 && TYPE_PRECISION (TREE_TYPE (@3)) > TYPE_PRECISION (TREE_TYPE (@2))
6827 && !wi::neg_p (wi::to_wide (@1)))
6828 (cmp (bit_and @0 (convert (bit_not @1)))
6829 { build_zero_cst (TREE_TYPE (@0)); })))
6831 /* (X | Y) == Y becomes (X & ~Y) == 0. */
6833 (cmp:c (bit_ior:c @0 @1) @1)
6834 (cmp (bit_and @0 (bit_not! @1)) { build_zero_cst (TREE_TYPE (@0)); }))
6836 /* (X ^ C1) op C2 can be rewritten as X op (C1 ^ C2). */
6838 (cmp (convert?@3 (bit_xor @0 INTEGER_CST@1)) INTEGER_CST@2)
6839 (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0)))
6840 (cmp @0 (bit_xor @1 (convert @2)))))
6843 (cmp (nop_convert? @0) integer_zerop)
6844 (if (tree_expr_nonzero_p (@0))
6845 { constant_boolean_node (cmp == NE_EXPR, type); }))
6847 /* (X & C) op (Y & C) into (X ^ Y) & C op 0. */
6849 (cmp (bit_and:cs @0 @2) (bit_and:cs @1 @2))
6850 (cmp (bit_and (bit_xor @0 @1) @2) { build_zero_cst (TREE_TYPE (@2)); })))
6852 /* (X < 0) != (Y < 0) into (X ^ Y) < 0.
6853 (X >= 0) != (Y >= 0) into (X ^ Y) < 0.
6854 (X < 0) == (Y < 0) into (X ^ Y) >= 0.
6855 (X >= 0) == (Y >= 0) into (X ^ Y) >= 0. */
6860 (cmp (sgncmp @0 integer_zerop@2) (sgncmp @1 integer_zerop))
6861 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
6862 && !TYPE_UNSIGNED (TREE_TYPE (@0))
6863 && types_match (@0, @1))
6864 (ncmp (bit_xor @0 @1) @2)))))
6865 /* (X < 0) == (Y >= 0) into (X ^ Y) < 0.
6866 (X < 0) != (Y >= 0) into (X ^ Y) >= 0. */
6870 (cmp:c (lt @0 integer_zerop@2) (ge @1 integer_zerop))
6871 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
6872 && !TYPE_UNSIGNED (TREE_TYPE (@0))
6873 && types_match (@0, @1))
6874 (ncmp (bit_xor @0 @1) @2))))
6876 /* If we have (A & C) == C where C is a power of 2, convert this into
6877 (A & C) != 0. Similarly for NE_EXPR. */
6881 (cmp (bit_and@2 @0 integer_pow2p@1) @1)
6882 (icmp @2 { build_zero_cst (TREE_TYPE (@0)); })))
6885 /* From fold_binary_op_with_conditional_arg handle the case of
6886 rewriting (a ? b : c) > d to a ? (b > d) : (c > d) when the
6887 compares simplify. */
6888 (for cmp (simple_comparison)
6890 (cmp:c (cond @0 @1 @2) @3)
6891 /* Do not move possibly trapping operations into the conditional as this
6892 pessimizes code and causes gimplification issues when applied late. */
6893 (if (!FLOAT_TYPE_P (TREE_TYPE (@3))
6894 || !operation_could_trap_p (cmp, true, false, @3))
6895 (cond @0 (cmp! @1 @3) (cmp! @2 @3)))))
6899 /* x < 0 ? ~y : y into (x >> (prec-1)) ^ y. */
6900 /* x >= 0 ? ~y : y into ~((x >> (prec-1)) ^ y). */
6902 (cond (cmp @0 integer_zerop) (bit_not @1) @1)
6903 (if (INTEGRAL_TYPE_P (type)
6904 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
6905 && !TYPE_UNSIGNED (TREE_TYPE (@0))
6906 && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (type))
6909 tree shifter = build_int_cst (integer_type_node, TYPE_PRECISION (type) - 1);
6911 (if (cmp == LT_EXPR)
6912 (bit_xor (convert (rshift @0 {shifter;})) @1)
6913 (bit_not (bit_xor (convert (rshift @0 {shifter;})) @1))))))
6914 /* x < 0 ? y : ~y into ~((x >> (prec-1)) ^ y). */
6915 /* x >= 0 ? y : ~y into (x >> (prec-1)) ^ y. */
6917 (cond (cmp @0 integer_zerop) @1 (bit_not @1))
6918 (if (INTEGRAL_TYPE_P (type)
6919 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
6920 && !TYPE_UNSIGNED (TREE_TYPE (@0))
6921 && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (type))
6924 tree shifter = build_int_cst (integer_type_node, TYPE_PRECISION (type) - 1);
6926 (if (cmp == GE_EXPR)
6927 (bit_xor (convert (rshift @0 {shifter;})) @1)
6928 (bit_not (bit_xor (convert (rshift @0 {shifter;})) @1)))))))
6930 /* If we have (A & C) != 0 ? D : 0 where C and D are powers of 2,
6931 convert this into a shift followed by ANDing with D. */
6934 (ne (bit_and @0 integer_pow2p@1) integer_zerop)
6935 INTEGER_CST@2 integer_zerop)
6936 (if (!POINTER_TYPE_P (type) && integer_pow2p (@2))
6938 int shift = (wi::exact_log2 (wi::to_wide (@2))
6939 - wi::exact_log2 (wi::to_wide (@1)));
6943 (lshift (convert @0) { build_int_cst (integer_type_node, shift); }) @2)
6945 (convert (rshift @0 { build_int_cst (integer_type_node, -shift); }))
6948 /* If we have (A & C) != 0 where C is the sign bit of A, convert
6949 this into A < 0. Similarly for (A & C) == 0 into A >= 0. */
6953 (cmp (bit_and (convert?@2 @0) integer_pow2p@1) integer_zerop)
6954 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6955 && type_has_mode_precision_p (TREE_TYPE (@0))
6956 && element_precision (@2) >= element_precision (@0)
6957 && wi::only_sign_bit_p (wi::to_wide (@1), element_precision (@0)))
6958 (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
6959 (ncmp (convert:stype @0) { build_zero_cst (stype); })))))
6961 /* If we have A < 0 ? C : 0 where C is a power of 2, convert
6962 this into a right shift or sign extension followed by ANDing with C. */
6965 (lt @0 integer_zerop)
6966 INTEGER_CST@1 integer_zerop)
6967 (if (integer_pow2p (@1)
6968 && !TYPE_UNSIGNED (TREE_TYPE (@0)))
6970 int shift = element_precision (@0) - wi::exact_log2 (wi::to_wide (@1)) - 1;
6974 (convert (rshift @0 { build_int_cst (integer_type_node, shift); }))
6976 /* Otherwise ctype must be wider than TREE_TYPE (@0) and pure
6977 sign extension followed by AND with C will achieve the effect. */
6978 (bit_and (convert @0) @1)))))
6980 /* When the addresses are not directly of decls compare base and offset.
6981 This implements some remaining parts of fold_comparison address
6982 comparisons but still no complete part of it. Still it is good
6983 enough to make fold_stmt not regress when not dispatching to fold_binary. */
6984 (for cmp (simple_comparison)
6986 (cmp (convert1?@2 addr@0) (convert2? addr@1))
6989 poly_int64 off0, off1;
6991 int equal = address_compare (cmp, TREE_TYPE (@2), @0, @1, base0, base1,
6992 off0, off1, GENERIC);
6996 (if (cmp == EQ_EXPR && (known_eq (off0, off1) || known_ne (off0, off1)))
6997 { constant_boolean_node (known_eq (off0, off1), type); })
6998 (if (cmp == NE_EXPR && (known_eq (off0, off1) || known_ne (off0, off1)))
6999 { constant_boolean_node (known_ne (off0, off1), type); })
7000 (if (cmp == LT_EXPR && (known_lt (off0, off1) || known_ge (off0, off1)))
7001 { constant_boolean_node (known_lt (off0, off1), type); })
7002 (if (cmp == LE_EXPR && (known_le (off0, off1) || known_gt (off0, off1)))
7003 { constant_boolean_node (known_le (off0, off1), type); })
7004 (if (cmp == GE_EXPR && (known_ge (off0, off1) || known_lt (off0, off1)))
7005 { constant_boolean_node (known_ge (off0, off1), type); })
7006 (if (cmp == GT_EXPR && (known_gt (off0, off1) || known_le (off0, off1)))
7007 { constant_boolean_node (known_gt (off0, off1), type); }))
7010 (if (cmp == EQ_EXPR)
7011 { constant_boolean_node (false, type); })
7012 (if (cmp == NE_EXPR)
7013 { constant_boolean_node (true, type); })))))))
7016 /* a?~t:t -> (-(a))^t */
7019 (with { bool wascmp; }
7020 (if (INTEGRAL_TYPE_P (type)
7021 && bitwise_inverted_equal_p (@1, @2, wascmp)
7022 && (!wascmp || TYPE_PRECISION (type) == 1))
7023 (if ((!TYPE_UNSIGNED (type) && TREE_CODE (type) == BOOLEAN_TYPE)
7024 || TYPE_PRECISION (type) == 1)
7025 (bit_xor (convert:type @0) @2)
7026 (bit_xor (negate (convert:type @0)) @2)))))
7029 /* Simplify pointer equality compares using PTA. */
7033 (if (POINTER_TYPE_P (TREE_TYPE (@0))
7034 && ptrs_compare_unequal (@0, @1))
7035 { constant_boolean_node (neeq != EQ_EXPR, type); })))
7037 /* PR70920: Transform (intptr_t)x eq/ne CST to x eq/ne (typeof x) CST.
7038 and (typeof ptr_cst) x eq/ne ptr_cst to x eq/ne (typeof x) CST.
7039 Disable the transform if either operand is pointer to function.
7040 This broke pr22051-2.c for arm where function pointer
7041 canonicalizaion is not wanted. */
7045 (cmp (convert @0) INTEGER_CST@1)
7046 (if (((POINTER_TYPE_P (TREE_TYPE (@0))
7047 && !FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@0)))
7048 && INTEGRAL_TYPE_P (TREE_TYPE (@1))
7049 /* Don't perform this optimization in GENERIC if @0 has reference
7050 type when sanitizing. See PR101210. */
7052 && TREE_CODE (TREE_TYPE (@0)) == REFERENCE_TYPE
7053 && (flag_sanitize & (SANITIZE_NULL | SANITIZE_ALIGNMENT))))
7054 || (INTEGRAL_TYPE_P (TREE_TYPE (@0))
7055 && POINTER_TYPE_P (TREE_TYPE (@1))
7056 && !FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@1)))))
7057 && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
7058 (cmp @0 (convert @1)))))
7060 /* Non-equality compare simplifications from fold_binary */
7061 (for cmp (lt gt le ge)
7062 /* Comparisons with the highest or lowest possible integer of
7063 the specified precision will have known values. */
7065 (cmp (convert?@2 @0) uniform_integer_cst_p@1)
7066 (if ((INTEGRAL_TYPE_P (TREE_TYPE (@1))
7067 || POINTER_TYPE_P (TREE_TYPE (@1))
7068 || VECTOR_INTEGER_TYPE_P (TREE_TYPE (@1)))
7069 && tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@0)))
7072 tree cst = uniform_integer_cst_p (@1);
7073 tree arg1_type = TREE_TYPE (cst);
7074 unsigned int prec = TYPE_PRECISION (arg1_type);
7075 wide_int max = wi::max_value (arg1_type);
7076 wide_int signed_max = wi::max_value (prec, SIGNED);
7077 wide_int min = wi::min_value (arg1_type);
7080 (if (wi::to_wide (cst) == max)
7082 (if (cmp == GT_EXPR)
7083 { constant_boolean_node (false, type); })
7084 (if (cmp == GE_EXPR)
7086 (if (cmp == LE_EXPR)
7087 { constant_boolean_node (true, type); })
7088 (if (cmp == LT_EXPR)
7090 (if (wi::to_wide (cst) == min)
7092 (if (cmp == LT_EXPR)
7093 { constant_boolean_node (false, type); })
7094 (if (cmp == LE_EXPR)
7096 (if (cmp == GE_EXPR)
7097 { constant_boolean_node (true, type); })
7098 (if (cmp == GT_EXPR)
7100 (if (wi::to_wide (cst) == max - 1)
7102 (if (cmp == GT_EXPR)
7103 (eq @2 { build_uniform_cst (TREE_TYPE (@1),
7104 wide_int_to_tree (TREE_TYPE (cst),
7107 (if (cmp == LE_EXPR)
7108 (ne @2 { build_uniform_cst (TREE_TYPE (@1),
7109 wide_int_to_tree (TREE_TYPE (cst),
7112 (if (wi::to_wide (cst) == min + 1)
7114 (if (cmp == GE_EXPR)
7115 (ne @2 { build_uniform_cst (TREE_TYPE (@1),
7116 wide_int_to_tree (TREE_TYPE (cst),
7119 (if (cmp == LT_EXPR)
7120 (eq @2 { build_uniform_cst (TREE_TYPE (@1),
7121 wide_int_to_tree (TREE_TYPE (cst),
7124 (if (wi::to_wide (cst) == signed_max
7125 && TYPE_UNSIGNED (arg1_type)
7126 && TYPE_MODE (arg1_type) != BLKmode
7127 /* We will flip the signedness of the comparison operator
7128 associated with the mode of @1, so the sign bit is
7129 specified by this mode. Check that @1 is the signed
7130 max associated with this sign bit. */
7131 && prec == GET_MODE_PRECISION (SCALAR_INT_TYPE_MODE (arg1_type))
7132 /* signed_type does not work on pointer types. */
7133 && INTEGRAL_TYPE_P (arg1_type))
7134 /* The following case also applies to X < signed_max+1
7135 and X >= signed_max+1 because previous transformations. */
7136 (if (cmp == LE_EXPR || cmp == GT_EXPR)
7137 (with { tree st = signed_type_for (TREE_TYPE (@1)); }
7139 (if (cst == @1 && cmp == LE_EXPR)
7140 (ge (convert:st @0) { build_zero_cst (st); }))
7141 (if (cst == @1 && cmp == GT_EXPR)
7142 (lt (convert:st @0) { build_zero_cst (st); }))
7143 (if (cmp == LE_EXPR)
7144 (ge (view_convert:st @0) { build_zero_cst (st); }))
7145 (if (cmp == GT_EXPR)
7146 (lt (view_convert:st @0) { build_zero_cst (st); })))))))))))
7148 /* unsigned < (typeof unsigned)(unsigned != 0) is always false. */
7150 (lt:c @0 (convert (ne @0 integer_zerop)))
7151 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
7152 { constant_boolean_node (false, type); }))
7154 /* x != (typeof x)(x == CST) -> CST == 0 ? 1 : (CST == 1 ? (x!=0&&x!=1) : x != 0) */
7155 /* x != (typeof x)(x != CST) -> CST == 1 ? 1 : (CST == 0 ? (x!=0&&x!=1) : x != 1) */
7156 /* x == (typeof x)(x == CST) -> CST == 0 ? 0 : (CST == 1 ? (x==0||x==1) : x == 0) */
7157 /* x == (typeof x)(x != CST) -> CST == 1 ? 0 : (CST == 0 ? (x==0||x==1) : x == 1) */
7161 (outer:c @0 (convert (inner @0 INTEGER_CST@1)))
7163 bool cst1 = integer_onep (@1);
7164 bool cst0 = integer_zerop (@1);
7165 bool innereq = inner == EQ_EXPR;
7166 bool outereq = outer == EQ_EXPR;
7169 (if (innereq ? cst0 : cst1)
7170 { constant_boolean_node (!outereq, type); })
7171 (if (innereq ? cst1 : cst0)
7173 tree utype = unsigned_type_for (TREE_TYPE (@0));
7174 tree ucst1 = build_one_cst (utype);
7177 (gt (convert:utype @0) { ucst1; })
7178 (le (convert:utype @0) { ucst1; })
7183 tree value = build_int_cst (TREE_TYPE (@0), !innereq);
7196 (for cmp (unordered ordered unlt unle ungt unge uneq ltgt)
7197 /* If the second operand is NaN, the result is constant. */
7200 (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
7201 && (cmp != LTGT_EXPR || ! flag_trapping_math))
7202 { constant_boolean_node (cmp == ORDERED_EXPR || cmp == LTGT_EXPR
7203 ? false : true, type); })))
7205 /* Fold UNORDERED if either operand must be NaN, or neither can be. */
7209 (if (tree_expr_nan_p (@0) || tree_expr_nan_p (@1))
7210 { constant_boolean_node (true, type); })
7211 (if (!tree_expr_maybe_nan_p (@0) && !tree_expr_maybe_nan_p (@1))
7212 { constant_boolean_node (false, type); })))
7214 /* Fold ORDERED if either operand must be NaN, or neither can be. */
7218 (if (tree_expr_nan_p (@0) || tree_expr_nan_p (@1))
7219 { constant_boolean_node (false, type); })
7220 (if (!tree_expr_maybe_nan_p (@0) && !tree_expr_maybe_nan_p (@1))
7221 { constant_boolean_node (true, type); })))
7223 /* bool_var != 0 becomes bool_var. */
7225 (ne @0 integer_zerop)
7226 (if (TREE_CODE (TREE_TYPE (@0)) == BOOLEAN_TYPE
7227 && types_match (type, TREE_TYPE (@0)))
7229 /* bool_var == 1 becomes bool_var. */
7231 (eq @0 integer_onep)
7232 (if (TREE_CODE (TREE_TYPE (@0)) == BOOLEAN_TYPE
7233 && types_match (type, TREE_TYPE (@0)))
7236 bool_var == 0 becomes !bool_var or
7237 bool_var != 1 becomes !bool_var
7238 here because that only is good in assignment context as long
7239 as we require a tcc_comparison in GIMPLE_CONDs where we'd
7240 replace if (x == 0) with tem = ~x; if (tem != 0) which is
7241 clearly less optimal and which we'll transform again in forwprop. */
7243 /* Transform comparisons of the form (X & Y) CMP 0 to X CMP2 Z
7244 where ~Y + 1 == pow2 and Z = ~Y. */
7245 (for cst (VECTOR_CST INTEGER_CST)
7249 (cmp (bit_and:c@2 @0 cst@1) integer_zerop)
7250 (with { tree csts = bitmask_inv_cst_vector_p (@1); }
7251 (if (csts && (VECTOR_TYPE_P (TREE_TYPE (@1)) || single_use (@2)))
7252 (with { auto optab = VECTOR_TYPE_P (TREE_TYPE (@1))
7253 ? optab_vector : optab_default;
7254 tree utype = unsigned_type_for (TREE_TYPE (@1)); }
7255 (if (target_supports_op_p (utype, icmp, optab)
7256 || (optimize_vectors_before_lowering_p ()
7257 && (!target_supports_op_p (type, cmp, optab)
7258 || !target_supports_op_p (type, BIT_AND_EXPR, optab))))
7259 (if (TYPE_UNSIGNED (TREE_TYPE (@1)))
7261 (icmp (view_convert:utype @0) { csts; })))))))))
7263 /* When one argument is a constant, overflow detection can be simplified.
7264 Currently restricted to single use so as not to interfere too much with
7265 ADD_OVERFLOW detection in tree-ssa-math-opts.cc.
7266 CONVERT?(CONVERT?(A) + CST) CMP A -> A CMP' CST' */
7267 (for cmp (lt le ge gt)
7270 (cmp:c (convert?@3 (plus@2 (convert?@4 @0) INTEGER_CST@1)) @0)
7271 (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@2))
7272 && types_match (TREE_TYPE (@0), TREE_TYPE (@3))
7273 && tree_nop_conversion_p (TREE_TYPE (@4), TREE_TYPE (@0))
7274 && wi::to_wide (@1) != 0
7277 unsigned int prec = TYPE_PRECISION (TREE_TYPE (@0));
7278 signop sign = TYPE_SIGN (TREE_TYPE (@0));
7280 (out @0 { wide_int_to_tree (TREE_TYPE (@0),
7281 wi::max_value (prec, sign)
7282 - wi::to_wide (@1)); })))))
7284 /* To detect overflow in unsigned A - B, A < B is simpler than A - B > A.
7285 However, the detection logic for SUB_OVERFLOW in tree-ssa-math-opts.cc
7286 expects the long form, so we restrict the transformation for now. */
7289 (cmp:c (minus@2 @0 @1) @0)
7290 (if (single_use (@2)
7291 && ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
7292 && TYPE_UNSIGNED (TREE_TYPE (@0)))
7295 /* Optimize A - B + -1 >= A into B >= A for unsigned comparisons. */
7298 (cmp:c (plus (minus @0 @1) integer_minus_onep) @0)
7299 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
7300 && TYPE_UNSIGNED (TREE_TYPE (@0)))
7303 /* Testing for overflow is unnecessary if we already know the result. */
7308 (cmp:c (realpart (IFN_SUB_OVERFLOW@2 @0 @1)) @0)
7309 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
7310 && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
7311 (out (imagpart @2) { build_zero_cst (TREE_TYPE (@0)); }))))
7316 (cmp:c (realpart (IFN_ADD_OVERFLOW:c@2 @0 @1)) @0)
7317 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
7318 && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
7319 (out (imagpart @2) { build_zero_cst (TREE_TYPE (@0)); }))))
7321 /* For unsigned operands, -1 / B < A checks whether A * B would overflow.
7322 Simplify it to __builtin_mul_overflow (A, B, <unused>). */
7326 (cmp:c (trunc_div:s integer_all_onesp @1) @0)
7327 (if (TYPE_UNSIGNED (TREE_TYPE (@0)) && !VECTOR_TYPE_P (TREE_TYPE (@0)))
7328 (with { tree t = TREE_TYPE (@0), cpx = build_complex_type (t); }
7329 (out (imagpart (IFN_MUL_OVERFLOW:cpx @0 @1)) { build_zero_cst (t); })))))
7331 /* Similarly, for unsigned operands, (((type) A * B) >> prec) != 0 where type
7332 is at least twice as wide as type of A and B, simplify to
7333 __builtin_mul_overflow (A, B, <unused>). */
7336 (cmp (rshift (mult:s (convert@3 @0) (convert @1)) INTEGER_CST@2)
7338 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
7339 && INTEGRAL_TYPE_P (TREE_TYPE (@3))
7340 && TYPE_UNSIGNED (TREE_TYPE (@0))
7341 && (TYPE_PRECISION (TREE_TYPE (@3))
7342 >= 2 * TYPE_PRECISION (TREE_TYPE (@0)))
7343 && tree_fits_uhwi_p (@2)
7344 && tree_to_uhwi (@2) == TYPE_PRECISION (TREE_TYPE (@0))
7345 && types_match (@0, @1)
7346 && type_has_mode_precision_p (TREE_TYPE (@0))
7347 && (optab_handler (umulv4_optab, TYPE_MODE (TREE_TYPE (@0)))
7348 != CODE_FOR_nothing))
7349 (with { tree t = TREE_TYPE (@0), cpx = build_complex_type (t); }
7350 (cmp (imagpart (IFN_MUL_OVERFLOW:cpx @0 @1)) { build_zero_cst (t); })))))
7352 /* Demote operands of IFN_{ADD,SUB,MUL}_OVERFLOW. */
7353 (for ovf (IFN_ADD_OVERFLOW IFN_SUB_OVERFLOW IFN_MUL_OVERFLOW)
7355 (ovf (convert@2 @0) @1)
7356 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
7357 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
7358 && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
7359 && (!TYPE_UNSIGNED (TREE_TYPE (@2)) || TYPE_UNSIGNED (TREE_TYPE (@0))))
7362 (ovf @1 (convert@2 @0))
7363 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
7364 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
7365 && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
7366 && (!TYPE_UNSIGNED (TREE_TYPE (@2)) || TYPE_UNSIGNED (TREE_TYPE (@0))))
7369 /* Optimize __builtin_mul_overflow_p (x, cst, (utype) 0) if all 3 types
7370 are unsigned to x > (umax / cst). Similarly for signed type, but
7371 in that case it needs to be outside of a range. */
7373 (imagpart (IFN_MUL_OVERFLOW:cs@2 @0 integer_nonzerop@1))
7374 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
7375 && TYPE_MAX_VALUE (TREE_TYPE (@0))
7376 && types_match (TREE_TYPE (@0), TREE_TYPE (TREE_TYPE (@2)))
7377 && int_fits_type_p (@1, TREE_TYPE (@0)))
7378 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
7379 (convert (gt @0 (trunc_div! { TYPE_MAX_VALUE (TREE_TYPE (@0)); } @1)))
7380 (if (TYPE_MIN_VALUE (TREE_TYPE (@0)))
7381 (if (integer_minus_onep (@1))
7382 (convert (eq @0 { TYPE_MIN_VALUE (TREE_TYPE (@0)); }))
7385 tree div = fold_convert (TREE_TYPE (@0), @1);
7386 tree lo = int_const_binop (TRUNC_DIV_EXPR,
7387 TYPE_MIN_VALUE (TREE_TYPE (@0)), div);
7388 tree hi = int_const_binop (TRUNC_DIV_EXPR,
7389 TYPE_MAX_VALUE (TREE_TYPE (@0)), div);
7390 tree etype = range_check_type (TREE_TYPE (@0));
7393 if (wi::neg_p (wi::to_wide (div)))
7395 lo = fold_convert (etype, lo);
7396 hi = fold_convert (etype, hi);
7397 hi = int_const_binop (MINUS_EXPR, hi, lo);
7401 (convert (gt (minus (convert:etype @0) { lo; }) { hi; })))))))))
7403 /* Simplification of math builtins. These rules must all be optimizations
7404 as well as IL simplifications. If there is a possibility that the new
7405 form could be a pessimization, the rule should go in the canonicalization
7406 section that follows this one.
7408 Rules can generally go in this section if they satisfy one of
7411 - the rule describes an identity
7413 - the rule replaces calls with something as simple as addition or
7416 - the rule contains unary calls only and simplifies the surrounding
7417 arithmetic. (The idea here is to exclude non-unary calls in which
7418 one operand is constant and in which the call is known to be cheap
7419 when the operand has that value.) */
7421 (if (flag_unsafe_math_optimizations)
7422 /* Simplify sqrt(x) * sqrt(x) -> x. */
7424 (mult (SQRT_ALL@1 @0) @1)
7425 (if (!tree_expr_maybe_signaling_nan_p (@0))
7428 (for op (plus minus)
7429 /* Simplify (A / C) +- (B / C) -> (A +- B) / C. */
7433 (rdiv (op @0 @2) @1)))
7435 (for cmp (lt le gt ge)
7436 neg_cmp (gt ge lt le)
7437 /* Simplify (x * C1) cmp C2 -> x cmp (C2 / C1), where C1 != 0. */
7439 (cmp (mult @0 REAL_CST@1) REAL_CST@2)
7441 { tree tem = const_binop (RDIV_EXPR, type, @2, @1); }
7443 && !(REAL_VALUE_ISINF (TREE_REAL_CST (tem))
7444 || (real_zerop (tem) && !real_zerop (@1))))
7446 (if (real_less (&dconst0, TREE_REAL_CST_PTR (@1)))
7448 (if (real_less (TREE_REAL_CST_PTR (@1), &dconst0))
7449 (neg_cmp @0 { tem; })))))))
7451 /* Simplify sqrt(x) * sqrt(y) -> sqrt(x*y). */
7452 (for root (SQRT CBRT)
7454 (mult (root:s @0) (root:s @1))
7455 (root (mult @0 @1))))
7457 /* Simplify expN(x) * expN(y) -> expN(x+y). */
7458 (for exps (EXP EXP2 EXP10 POW10)
7460 (mult (exps:s @0) (exps:s @1))
7461 (exps (plus @0 @1))))
7463 /* Simplify a/root(b/c) into a*root(c/b). */
7464 (for root (SQRT CBRT)
7466 (rdiv @0 (root:s (rdiv:s @1 @2)))
7467 (mult @0 (root (rdiv @2 @1)))))
7469 /* Simplify x/expN(y) into x*expN(-y). */
7470 (for exps (EXP EXP2 EXP10 POW10)
7472 (rdiv @0 (exps:s @1))
7473 (mult @0 (exps (negate @1)))))
7475 (for logs (LOG LOG2 LOG10 LOG10)
7476 exps (EXP EXP2 EXP10 POW10)
7477 /* logN(expN(x)) -> x. */
7481 /* expN(logN(x)) -> x. */
7486 /* Optimize logN(func()) for various exponential functions. We
7487 want to determine the value "x" and the power "exponent" in
7488 order to transform logN(x**exponent) into exponent*logN(x). */
7489 (for logs (LOG LOG LOG LOG2 LOG2 LOG2 LOG10 LOG10)
7490 exps (EXP2 EXP10 POW10 EXP EXP10 POW10 EXP EXP2)
7493 (if (SCALAR_FLOAT_TYPE_P (type))
7499 /* Prepare to do logN(exp(exponent)) -> exponent*logN(e). */
7500 x = build_real_truncate (type, dconst_e ());
7503 /* Prepare to do logN(exp2(exponent)) -> exponent*logN(2). */
7504 x = build_real (type, dconst2);
7508 /* Prepare to do logN(exp10(exponent)) -> exponent*logN(10). */
7510 REAL_VALUE_TYPE dconst10;
7511 real_from_integer (&dconst10, VOIDmode, 10, SIGNED);
7512 x = build_real (type, dconst10);
7519 (mult (logs { x; }) @0)))))
7527 (if (SCALAR_FLOAT_TYPE_P (type))
7533 /* Prepare to do logN(sqrt(x)) -> 0.5*logN(x). */
7534 x = build_real (type, dconsthalf);
7537 /* Prepare to do logN(cbrt(x)) -> (1/3)*logN(x). */
7538 x = build_real_truncate (type, dconst_third ());
7544 (mult { x; } (logs @0))))))
7546 /* logN(pow(x,exponent)) -> exponent*logN(x). */
7547 (for logs (LOG LOG2 LOG10)
7551 (mult @1 (logs @0))))
7553 /* pow(C,x) -> exp(log(C)*x) if C > 0,
7554 or if C is a positive power of 2,
7555 pow(C,x) -> exp2(log2(C)*x). */
7563 (pows REAL_CST@0 @1)
7564 (if (real_compare (GT_EXPR, TREE_REAL_CST_PTR (@0), &dconst0)
7565 && real_isfinite (TREE_REAL_CST_PTR (@0))
7566 /* As libmvec doesn't have a vectorized exp2, defer optimizing
7567 the use_exp2 case until after vectorization. It seems actually
7568 beneficial for all constants to postpone this until later,
7569 because exp(log(C)*x), while faster, will have worse precision
7570 and if x folds into a constant too, that is unnecessary
7572 && canonicalize_math_after_vectorization_p ())
7574 const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (@0);
7575 bool use_exp2 = false;
7576 if (targetm.libc_has_function (function_c99_misc, TREE_TYPE (@0))
7577 && value->cl == rvc_normal)
7579 REAL_VALUE_TYPE frac_rvt = *value;
7580 SET_REAL_EXP (&frac_rvt, 1);
7581 if (real_equal (&frac_rvt, &dconst1))
7586 (if (optimize_pow_to_exp (@0, @1))
7587 (exps (mult (logs @0) @1)))
7588 (exp2s (mult (log2s @0) @1)))))))
7591 /* pow(C,x)*expN(y) -> expN(logN(C)*x+y) if C > 0. */
7593 exps (EXP EXP2 EXP10 POW10)
7594 logs (LOG LOG2 LOG10 LOG10)
7596 (mult:c (pows:s REAL_CST@0 @1) (exps:s @2))
7597 (if (real_compare (GT_EXPR, TREE_REAL_CST_PTR (@0), &dconst0)
7598 && real_isfinite (TREE_REAL_CST_PTR (@0)))
7599 (exps (plus (mult (logs @0) @1) @2)))))
7604 exps (EXP EXP2 EXP10 POW10)
7605 /* sqrt(expN(x)) -> expN(x*0.5). */
7608 (exps (mult @0 { build_real (type, dconsthalf); })))
7609 /* cbrt(expN(x)) -> expN(x/3). */
7612 (exps (mult @0 { build_real_truncate (type, dconst_third ()); })))
7613 /* pow(expN(x), y) -> expN(x*y). */
7616 (exps (mult @0 @1))))
7618 /* tan(atan(x)) -> x. */
7625 /* Simplify sin(atan(x)) -> x / sqrt(x*x + 1). */
7629 copysigns (COPYSIGN)
7634 REAL_VALUE_TYPE r_cst;
7635 build_sinatan_real (&r_cst, type);
7636 tree t_cst = build_real (type, r_cst);
7637 tree t_one = build_one_cst (type);
7639 (if (SCALAR_FLOAT_TYPE_P (type))
7640 (cond (lt (abs @0) { t_cst; })
7641 (rdiv @0 (sqrts (plus (mult @0 @0) { t_one; })))
7642 (copysigns { t_one; } @0))))))
7644 /* Simplify cos(atan(x)) -> 1 / sqrt(x*x + 1). */
7648 copysigns (COPYSIGN)
7653 REAL_VALUE_TYPE r_cst;
7654 build_sinatan_real (&r_cst, type);
7655 tree t_cst = build_real (type, r_cst);
7656 tree t_one = build_one_cst (type);
7657 tree t_zero = build_zero_cst (type);
7659 (if (SCALAR_FLOAT_TYPE_P (type))
7660 (cond (lt (abs @0) { t_cst; })
7661 (rdiv { t_one; } (sqrts (plus (mult @0 @0) { t_one; })))
7662 (copysigns { t_zero; } @0))))))
7664 (if (!flag_errno_math)
7665 /* Simplify sinh(atanh(x)) -> x / sqrt((1 - x)*(1 + x)). */
7670 (sinhs (atanhs:s @0))
7671 (with { tree t_one = build_one_cst (type); }
7672 (rdiv @0 (sqrts (mult (minus { t_one; } @0) (plus { t_one; } @0)))))))
7674 /* Simplify cosh(atanh(x)) -> 1 / sqrt((1 - x)*(1 + x)) */
7679 (coshs (atanhs:s @0))
7680 (with { tree t_one = build_one_cst (type); }
7681 (rdiv { t_one; } (sqrts (mult (minus { t_one; } @0) (plus { t_one; } @0))))))))
7683 /* cabs(x+0i) or cabs(0+xi) -> abs(x). */
7685 (CABS (complex:C @0 real_zerop@1))
7688 /* trunc(trunc(x)) -> trunc(x), etc. */
7689 (for fns (TRUNC_ALL FLOOR_ALL CEIL_ALL ROUND_ALL NEARBYINT_ALL RINT_ALL)
7693 /* f(x) -> x if x is integer valued and f does nothing for such values. */
7694 (for fns (TRUNC_ALL FLOOR_ALL CEIL_ALL ROUND_ALL NEARBYINT_ALL RINT_ALL)
7696 (fns integer_valued_real_p@0)
7699 /* hypot(x,0) and hypot(0,x) -> abs(x). */
7701 (HYPOT:c @0 real_zerop@1)
7704 /* pow(1,x) -> 1. */
7706 (POW real_onep@0 @1)
7710 /* copysign(x,x) -> x. */
7711 (COPYSIGN_ALL @0 @0)
7715 /* copysign(x,-x) -> -x. */
7716 (COPYSIGN_ALL @0 (negate@1 @0))
7720 /* copysign(x,y) -> fabs(x) if y is nonnegative. */
7721 (COPYSIGN_ALL @0 tree_expr_nonnegative_p@1)
7725 /* fabs (copysign(x, y)) -> fabs (x). */
7726 (abs (COPYSIGN_ALL @0 @1))
7729 (for scale (LDEXP SCALBN SCALBLN)
7730 /* ldexp(0, x) -> 0. */
7732 (scale real_zerop@0 @1)
7734 /* ldexp(x, 0) -> x. */
7736 (scale @0 integer_zerop@1)
7738 /* ldexp(x, y) -> x if x is +-Inf or NaN. */
7740 (scale REAL_CST@0 @1)
7741 (if (!real_isfinite (TREE_REAL_CST_PTR (@0)))
7744 /* Canonicalization of sequences of math builtins. These rules represent
7745 IL simplifications but are not necessarily optimizations.
7747 The sincos pass is responsible for picking "optimal" implementations
7748 of math builtins, which may be more complicated and can sometimes go
7749 the other way, e.g. converting pow into a sequence of sqrts.
7750 We only want to do these canonicalizations before the pass has run. */
7752 (if (flag_unsafe_math_optimizations && canonicalize_math_p ())
7753 /* Simplify tan(x) * cos(x) -> sin(x). */
7755 (mult:c (TAN:s @0) (COS:s @0))
7758 /* Simplify x * pow(x,c) -> pow(x,c+1). */
7760 (mult:c @0 (POW:s @0 REAL_CST@1))
7761 (if (!TREE_OVERFLOW (@1))
7762 (POW @0 (plus @1 { build_one_cst (type); }))))
7764 /* Simplify sin(x) / cos(x) -> tan(x). */
7766 (rdiv (SIN:s @0) (COS:s @0))
7769 /* Simplify sinh(x) / cosh(x) -> tanh(x). */
7771 (rdiv (SINH:s @0) (COSH:s @0))
7774 /* Simplify tanh (x) / sinh (x) -> 1.0 / cosh (x). */
7776 (rdiv (TANH:s @0) (SINH:s @0))
7777 (rdiv {build_one_cst (type);} (COSH @0)))
7779 /* Simplify cos(x) / sin(x) -> 1 / tan(x). */
7781 (rdiv (COS:s @0) (SIN:s @0))
7782 (rdiv { build_one_cst (type); } (TAN @0)))
7784 /* Simplify sin(x) / tan(x) -> cos(x). */
7786 (rdiv (SIN:s @0) (TAN:s @0))
7787 (if (! HONOR_NANS (@0)
7788 && ! HONOR_INFINITIES (@0))
7791 /* Simplify tan(x) / sin(x) -> 1.0 / cos(x). */
7793 (rdiv (TAN:s @0) (SIN:s @0))
7794 (if (! HONOR_NANS (@0)
7795 && ! HONOR_INFINITIES (@0))
7796 (rdiv { build_one_cst (type); } (COS @0))))
7798 /* Simplify pow(x,y) * pow(x,z) -> pow(x,y+z). */
7800 (mult (POW:s @0 @1) (POW:s @0 @2))
7801 (POW @0 (plus @1 @2)))
7803 /* Simplify pow(x,y) * pow(z,y) -> pow(x*z,y). */
7805 (mult (POW:s @0 @1) (POW:s @2 @1))
7806 (POW (mult @0 @2) @1))
7808 /* Simplify powi(x,y) * powi(z,y) -> powi(x*z,y). */
7810 (mult (POWI:s @0 @1) (POWI:s @2 @1))
7811 (POWI (mult @0 @2) @1))
7813 /* Simplify pow(x,c) / x -> pow(x,c-1). */
7815 (rdiv (POW:s @0 REAL_CST@1) @0)
7816 (if (!TREE_OVERFLOW (@1))
7817 (POW @0 (minus @1 { build_one_cst (type); }))))
7819 /* Simplify x / pow (y,z) -> x * pow(y,-z). */
7821 (rdiv @0 (POW:s @1 @2))
7822 (mult @0 (POW @1 (negate @2))))
7827 /* sqrt(sqrt(x)) -> pow(x,1/4). */
7830 (pows @0 { build_real (type, dconst_quarter ()); }))
7831 /* sqrt(cbrt(x)) -> pow(x,1/6). */
7834 (pows @0 { build_real_truncate (type, dconst_sixth ()); }))
7835 /* cbrt(sqrt(x)) -> pow(x,1/6). */
7838 (pows @0 { build_real_truncate (type, dconst_sixth ()); }))
7839 /* cbrt(cbrt(x)) -> pow(x,1/9), iff x is nonnegative. */
7841 (cbrts (cbrts tree_expr_nonnegative_p@0))
7842 (pows @0 { build_real_truncate (type, dconst_ninth ()); }))
7843 /* sqrt(pow(x,y)) -> pow(|x|,y*0.5). */
7845 (sqrts (pows @0 @1))
7846 (pows (abs @0) (mult @1 { build_real (type, dconsthalf); })))
7847 /* cbrt(pow(x,y)) -> pow(x,y/3), iff x is nonnegative. */
7849 (cbrts (pows tree_expr_nonnegative_p@0 @1))
7850 (pows @0 (mult @1 { build_real_truncate (type, dconst_third ()); })))
7851 /* pow(sqrt(x),y) -> pow(x,y*0.5). */
7853 (pows (sqrts @0) @1)
7854 (pows @0 (mult @1 { build_real (type, dconsthalf); })))
7855 /* pow(cbrt(x),y) -> pow(x,y/3) iff x is nonnegative. */
7857 (pows (cbrts tree_expr_nonnegative_p@0) @1)
7858 (pows @0 (mult @1 { build_real_truncate (type, dconst_third ()); })))
7859 /* pow(pow(x,y),z) -> pow(x,y*z) iff x is nonnegative. */
7861 (pows (pows tree_expr_nonnegative_p@0 @1) @2)
7862 (pows @0 (mult @1 @2))))
7864 /* cabs(x+xi) -> fabs(x)*sqrt(2). */
7866 (CABS (complex @0 @0))
7867 (mult (abs @0) { build_real_truncate (type, dconst_sqrt2 ()); }))
7869 /* hypot(x,x) -> fabs(x)*sqrt(2). */
7872 (mult (abs @0) { build_real_truncate (type, dconst_sqrt2 ()); }))
7874 /* cexp(x+yi) -> exp(x)*cexpi(y). */
7879 (cexps compositional_complex@0)
7880 (if (targetm.libc_has_function (function_c99_math_complex, TREE_TYPE (@0)))
7882 (mult (exps@1 (realpart @0)) (realpart (cexpis:type@2 (imagpart @0))))
7883 (mult @1 (imagpart @2)))))))
7885 (if (canonicalize_math_p ())
7886 /* floor(x) -> trunc(x) if x is nonnegative. */
7887 (for floors (FLOOR_ALL)
7890 (floors tree_expr_nonnegative_p@0)
7893 (match double_value_p
7895 (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == double_type_node)))
7896 (for froms (BUILT_IN_TRUNCL
7908 /* truncl(extend(x)) -> extend(trunc(x)), etc., if x is a double. */
7909 (if (optimize && canonicalize_math_p ())
7911 (froms (convert double_value_p@0))
7912 (convert (tos @0)))))
7914 (match float_value_p
7916 (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == float_type_node)))
7917 (for froms (BUILT_IN_TRUNCL BUILT_IN_TRUNC
7918 BUILT_IN_FLOORL BUILT_IN_FLOOR
7919 BUILT_IN_CEILL BUILT_IN_CEIL
7920 BUILT_IN_ROUNDL BUILT_IN_ROUND
7921 BUILT_IN_NEARBYINTL BUILT_IN_NEARBYINT
7922 BUILT_IN_RINTL BUILT_IN_RINT)
7923 tos (BUILT_IN_TRUNCF BUILT_IN_TRUNCF
7924 BUILT_IN_FLOORF BUILT_IN_FLOORF
7925 BUILT_IN_CEILF BUILT_IN_CEILF
7926 BUILT_IN_ROUNDF BUILT_IN_ROUNDF
7927 BUILT_IN_NEARBYINTF BUILT_IN_NEARBYINTF
7928 BUILT_IN_RINTF BUILT_IN_RINTF)
7929 /* truncl(extend(x)) and trunc(extend(x)) -> extend(truncf(x)), etc.,
7931 (if (optimize && canonicalize_math_p ()
7932 && targetm.libc_has_function (function_c99_misc, NULL_TREE))
7934 (froms (convert float_value_p@0))
7935 (convert (tos @0)))))
7938 (match float16_value_p
7940 (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == float16_type_node)))
7941 (for froms (BUILT_IN_TRUNCL BUILT_IN_TRUNC BUILT_IN_TRUNCF
7942 BUILT_IN_FLOORL BUILT_IN_FLOOR BUILT_IN_FLOORF
7943 BUILT_IN_CEILL BUILT_IN_CEIL BUILT_IN_CEILF
7944 BUILT_IN_ROUNDEVENL BUILT_IN_ROUNDEVEN BUILT_IN_ROUNDEVENF
7945 BUILT_IN_ROUNDL BUILT_IN_ROUND BUILT_IN_ROUNDF
7946 BUILT_IN_NEARBYINTL BUILT_IN_NEARBYINT BUILT_IN_NEARBYINTF
7947 BUILT_IN_RINTL BUILT_IN_RINT BUILT_IN_RINTF
7948 BUILT_IN_SQRTL BUILT_IN_SQRT BUILT_IN_SQRTF)
7949 tos (IFN_TRUNC IFN_TRUNC IFN_TRUNC
7950 IFN_FLOOR IFN_FLOOR IFN_FLOOR
7951 IFN_CEIL IFN_CEIL IFN_CEIL
7952 IFN_ROUNDEVEN IFN_ROUNDEVEN IFN_ROUNDEVEN
7953 IFN_ROUND IFN_ROUND IFN_ROUND
7954 IFN_NEARBYINT IFN_NEARBYINT IFN_NEARBYINT
7955 IFN_RINT IFN_RINT IFN_RINT
7956 IFN_SQRT IFN_SQRT IFN_SQRT)
7957 /* (_Float16) round ((doube) x) -> __built_in_roundf16 (x), etc.,
7958 if x is a _Float16. */
7960 (convert (froms (convert float16_value_p@0)))
7962 && types_match (type, TREE_TYPE (@0))
7963 && direct_internal_fn_supported_p (as_internal_fn (tos),
7964 type, OPTIMIZE_FOR_BOTH))
7967 /* Simplify (trunc)copysign ((extend)x, (extend)y) to copysignf (x, y),
7968 x,y is float value, similar for _Float16/double. */
7969 (for copysigns (COPYSIGN_ALL)
7971 (convert (copysigns (convert@2 @0) (convert @1)))
7973 && !HONOR_SNANS (@2)
7974 && types_match (type, TREE_TYPE (@0))
7975 && types_match (type, TREE_TYPE (@1))
7976 && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (@2))
7977 && direct_internal_fn_supported_p (IFN_COPYSIGN,
7978 type, OPTIMIZE_FOR_BOTH))
7979 (IFN_COPYSIGN @0 @1))))
7981 (for froms (BUILT_IN_FMAF BUILT_IN_FMA BUILT_IN_FMAL)
7982 tos (IFN_FMA IFN_FMA IFN_FMA)
7984 (convert (froms (convert@3 @0) (convert @1) (convert @2)))
7985 (if (flag_unsafe_math_optimizations
7987 && FLOAT_TYPE_P (type)
7988 && FLOAT_TYPE_P (TREE_TYPE (@3))
7989 && types_match (type, TREE_TYPE (@0))
7990 && types_match (type, TREE_TYPE (@1))
7991 && types_match (type, TREE_TYPE (@2))
7992 && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (@3))
7993 && direct_internal_fn_supported_p (as_internal_fn (tos),
7994 type, OPTIMIZE_FOR_BOTH))
7997 (for maxmin (max min)
7999 (convert (maxmin (convert@2 @0) (convert @1)))
8001 && FLOAT_TYPE_P (type)
8002 && FLOAT_TYPE_P (TREE_TYPE (@2))
8003 && types_match (type, TREE_TYPE (@0))
8004 && types_match (type, TREE_TYPE (@1))
8005 && element_precision (type) < element_precision (TREE_TYPE (@2)))
8009 (for froms (XFLOORL XCEILL XROUNDL XRINTL)
8010 tos (XFLOOR XCEIL XROUND XRINT)
8011 /* llfloorl(extend(x)) -> llfloor(x), etc., if x is a double. */
8012 (if (optimize && canonicalize_math_p ())
8014 (froms (convert double_value_p@0))
8017 (for froms (XFLOORL XCEILL XROUNDL XRINTL
8018 XFLOOR XCEIL XROUND XRINT)
8019 tos (XFLOORF XCEILF XROUNDF XRINTF)
8020 /* llfloorl(extend(x)) and llfloor(extend(x)) -> llfloorf(x), etc.,
8022 (if (optimize && canonicalize_math_p ())
8024 (froms (convert float_value_p@0))
8027 (if (canonicalize_math_p ())
8028 /* xfloor(x) -> fix_trunc(x) if x is nonnegative. */
8029 (for floors (IFLOOR LFLOOR LLFLOOR)
8031 (floors tree_expr_nonnegative_p@0)
8034 (if (canonicalize_math_p ())
8035 /* xfloor(x) -> fix_trunc(x), etc., if x is integer valued. */
8036 (for fns (IFLOOR LFLOOR LLFLOOR
8038 IROUND LROUND LLROUND)
8040 (fns integer_valued_real_p@0)
8042 (if (!flag_errno_math)
8043 /* xrint(x) -> fix_trunc(x), etc., if x is integer valued. */
8044 (for rints (IRINT LRINT LLRINT)
8046 (rints integer_valued_real_p@0)
8049 (if (canonicalize_math_p ())
8050 (for ifn (IFLOOR ICEIL IROUND IRINT)
8051 lfn (LFLOOR LCEIL LROUND LRINT)
8052 llfn (LLFLOOR LLCEIL LLROUND LLRINT)
8053 /* Canonicalize iround (x) to lround (x) on ILP32 targets where
8054 sizeof (int) == sizeof (long). */
8055 (if (TYPE_PRECISION (integer_type_node)
8056 == TYPE_PRECISION (long_integer_type_node))
8059 (lfn:long_integer_type_node @0)))
8060 /* Canonicalize llround (x) to lround (x) on LP64 targets where
8061 sizeof (long long) == sizeof (long). */
8062 (if (TYPE_PRECISION (long_long_integer_type_node)
8063 == TYPE_PRECISION (long_integer_type_node))
8066 (lfn:long_integer_type_node @0)))))
8068 /* cproj(x) -> x if we're ignoring infinities. */
8071 (if (!HONOR_INFINITIES (type))
8074 /* If the real part is inf and the imag part is known to be
8075 nonnegative, return (inf + 0i). */
8077 (CPROJ (complex REAL_CST@0 tree_expr_nonnegative_p@1))
8078 (if (real_isinf (TREE_REAL_CST_PTR (@0)))
8079 { build_complex_inf (type, false); }))
8081 /* If the imag part is inf, return (inf+I*copysign(0,imag)). */
8083 (CPROJ (complex @0 REAL_CST@1))
8084 (if (real_isinf (TREE_REAL_CST_PTR (@1)))
8085 { build_complex_inf (type, TREE_REAL_CST_PTR (@1)->sign); }))
8091 (pows @0 REAL_CST@1)
8093 const REAL_VALUE_TYPE *value = TREE_REAL_CST_PTR (@1);
8094 REAL_VALUE_TYPE tmp;
8097 /* pow(x,0) -> 1. */
8098 (if (real_equal (value, &dconst0))
8099 { build_real (type, dconst1); })
8100 /* pow(x,1) -> x. */
8101 (if (real_equal (value, &dconst1))
8103 /* pow(x,-1) -> 1/x. */
8104 (if (real_equal (value, &dconstm1))
8105 (rdiv { build_real (type, dconst1); } @0))
8106 /* pow(x,0.5) -> sqrt(x). */
8107 (if (flag_unsafe_math_optimizations
8108 && canonicalize_math_p ()
8109 && real_equal (value, &dconsthalf))
8111 /* pow(x,1/3) -> cbrt(x). */
8112 (if (flag_unsafe_math_optimizations
8113 && canonicalize_math_p ()
8114 && (tmp = real_value_truncate (TYPE_MODE (type), dconst_third ()),
8115 real_equal (value, &tmp)))
8118 /* powi(1,x) -> 1. */
8120 (POWI real_onep@0 @1)
8124 (POWI @0 INTEGER_CST@1)
8126 /* powi(x,0) -> 1. */
8127 (if (wi::to_wide (@1) == 0)
8128 { build_real (type, dconst1); })
8129 /* powi(x,1) -> x. */
8130 (if (wi::to_wide (@1) == 1)
8132 /* powi(x,-1) -> 1/x. */
8133 (if (wi::to_wide (@1) == -1)
8134 (rdiv { build_real (type, dconst1); } @0))))
8136 /* Narrowing of arithmetic and logical operations.
8138 These are conceptually similar to the transformations performed for
8139 the C/C++ front-ends by shorten_binary_op and shorten_compare. Long
8140 term we want to move all that code out of the front-ends into here. */
8142 /* Convert (outertype)((innertype0)a+(innertype1)b)
8143 into ((newtype)a+(newtype)b) where newtype
8144 is the widest mode from all of these. */
8145 (for op (plus minus mult rdiv)
8147 (convert (op:s@0 (convert1?@3 @1) (convert2?@4 @2)))
8148 /* If we have a narrowing conversion of an arithmetic operation where
8149 both operands are widening conversions from the same type as the outer
8150 narrowing conversion. Then convert the innermost operands to a
8151 suitable unsigned type (to avoid introducing undefined behavior),
8152 perform the operation and convert the result to the desired type. */
8153 (if (INTEGRAL_TYPE_P (type)
8156 /* We check for type compatibility between @0 and @1 below,
8157 so there's no need to check that @2/@4 are integral types. */
8158 && INTEGRAL_TYPE_P (TREE_TYPE (@1))
8159 && INTEGRAL_TYPE_P (TREE_TYPE (@3))
8160 /* The precision of the type of each operand must match the
8161 precision of the mode of each operand, similarly for the
8163 && type_has_mode_precision_p (TREE_TYPE (@1))
8164 && type_has_mode_precision_p (TREE_TYPE (@2))
8165 && type_has_mode_precision_p (type)
8166 /* The inner conversion must be a widening conversion. */
8167 && TYPE_PRECISION (TREE_TYPE (@3)) > TYPE_PRECISION (TREE_TYPE (@1))
8168 && types_match (@1, type)
8169 && (types_match (@1, @2)
8170 /* Or the second operand is const integer or converted const
8171 integer from valueize. */
8172 || poly_int_tree_p (@4)))
8173 (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
8174 (op @1 (convert @2))
8175 (with { tree utype = unsigned_type_for (TREE_TYPE (@1)); }
8176 (convert (op (convert:utype @1)
8177 (convert:utype @2)))))
8178 (if (FLOAT_TYPE_P (type)
8179 && DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0))
8180 == DECIMAL_FLOAT_TYPE_P (type))
8181 (with { tree arg0 = strip_float_extensions (@1);
8182 tree arg1 = strip_float_extensions (@2);
8183 tree itype = TREE_TYPE (@0);
8184 tree ty1 = TREE_TYPE (arg0);
8185 tree ty2 = TREE_TYPE (arg1);
8186 enum tree_code code = TREE_CODE (itype); }
8187 (if (FLOAT_TYPE_P (ty1)
8188 && FLOAT_TYPE_P (ty2))
8189 (with { tree newtype = type;
8190 if (TYPE_MODE (ty1) == SDmode
8191 || TYPE_MODE (ty2) == SDmode
8192 || TYPE_MODE (type) == SDmode)
8193 newtype = dfloat32_type_node;
8194 if (TYPE_MODE (ty1) == DDmode
8195 || TYPE_MODE (ty2) == DDmode
8196 || TYPE_MODE (type) == DDmode)
8197 newtype = dfloat64_type_node;
8198 if (TYPE_MODE (ty1) == TDmode
8199 || TYPE_MODE (ty2) == TDmode
8200 || TYPE_MODE (type) == TDmode)
8201 newtype = dfloat128_type_node; }
8202 (if ((newtype == dfloat32_type_node
8203 || newtype == dfloat64_type_node
8204 || newtype == dfloat128_type_node)
8206 && types_match (newtype, type))
8207 (op (convert:newtype @1) (convert:newtype @2))
8208 (with { if (element_precision (ty1) > element_precision (newtype))
8210 if (element_precision (ty2) > element_precision (newtype))
8212 /* Sometimes this transformation is safe (cannot
8213 change results through affecting double rounding
8214 cases) and sometimes it is not. If NEWTYPE is
8215 wider than TYPE, e.g. (float)((long double)double
8216 + (long double)double) converted to
8217 (float)(double + double), the transformation is
8218 unsafe regardless of the details of the types
8219 involved; double rounding can arise if the result
8220 of NEWTYPE arithmetic is a NEWTYPE value half way
8221 between two representable TYPE values but the
8222 exact value is sufficiently different (in the
8223 right direction) for this difference to be
8224 visible in ITYPE arithmetic. If NEWTYPE is the
8225 same as TYPE, however, the transformation may be
8226 safe depending on the types involved: it is safe
8227 if the ITYPE has strictly more than twice as many
8228 mantissa bits as TYPE, can represent infinities
8229 and NaNs if the TYPE can, and has sufficient
8230 exponent range for the product or ratio of two
8231 values representable in the TYPE to be within the
8232 range of normal values of ITYPE. */
8233 (if (element_precision (newtype) < element_precision (itype)
8234 && (!VECTOR_MODE_P (TYPE_MODE (newtype))
8235 || target_supports_op_p (newtype, op, optab_default))
8236 && (flag_unsafe_math_optimizations
8237 || (element_precision (newtype) == element_precision (type)
8238 && real_can_shorten_arithmetic (element_mode (itype),
8239 element_mode (type))
8240 && !excess_precision_type (newtype)))
8241 && !types_match (itype, newtype))
8242 (convert:type (op (convert:newtype @1)
8243 (convert:newtype @2)))
8248 /* This is another case of narrowing, specifically when there's an outer
8249 BIT_AND_EXPR which masks off bits outside the type of the innermost
8250 operands. Like the previous case we have to convert the operands
8251 to unsigned types to avoid introducing undefined behavior for the
8252 arithmetic operation. */
8253 (for op (minus plus)
8255 (bit_and (op:s (convert@2 @0) (convert@3 @1)) INTEGER_CST@4)
8256 (if (INTEGRAL_TYPE_P (type)
8257 /* We check for type compatibility between @0 and @1 below,
8258 so there's no need to check that @1/@3 are integral types. */
8259 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
8260 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
8261 /* The precision of the type of each operand must match the
8262 precision of the mode of each operand, similarly for the
8264 && type_has_mode_precision_p (TREE_TYPE (@0))
8265 && type_has_mode_precision_p (TREE_TYPE (@1))
8266 && type_has_mode_precision_p (type)
8267 /* The inner conversion must be a widening conversion. */
8268 && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
8269 && types_match (@0, @1)
8270 && (tree_int_cst_min_precision (@4, TYPE_SIGN (TREE_TYPE (@0)))
8271 <= TYPE_PRECISION (TREE_TYPE (@0)))
8272 && (wi::to_wide (@4)
8273 & wi::mask (TYPE_PRECISION (TREE_TYPE (@0)),
8274 true, TYPE_PRECISION (type))) == 0)
8275 (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
8276 (with { tree ntype = TREE_TYPE (@0); }
8277 (convert (bit_and (op @0 @1) (convert:ntype @4))))
8278 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
8279 (convert (bit_and (op (convert:utype @0) (convert:utype @1))
8280 (convert:utype @4))))))))
8282 /* Transform (@0 < @1 and @0 < @2) to use min,
8283 (@0 > @1 and @0 > @2) to use max */
8284 (for logic (bit_and bit_and bit_and bit_and bit_ior bit_ior bit_ior bit_ior)
8285 op (lt le gt ge lt le gt ge )
8286 ext (min min max max max max min min )
8288 (logic (op:cs @0 @1) (op:cs @0 @2))
8289 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
8290 && TREE_CODE (@0) != INTEGER_CST)
8291 (op @0 (ext @1 @2)))))
8293 /* Max<bool0, bool1> -> bool0 | bool1
8294 Min<bool0, bool1> -> bool0 & bool1 */
8296 logic (bit_ior bit_and)
8298 (op zero_one_valued_p@0 zero_one_valued_p@1)
8301 /* signbit(x) != 0 ? -x : x -> abs(x)
8302 signbit(x) == 0 ? -x : x -> -abs(x) */
8306 (cond (neeq (sign @0) integer_zerop) (negate @0) @0)
8307 (if (neeq == NE_EXPR)
8309 (negate (abs @0))))))
8312 /* signbit(x) -> 0 if x is nonnegative. */
8313 (SIGNBIT tree_expr_nonnegative_p@0)
8314 { integer_zero_node; })
8317 /* signbit(x) -> x<0 if x doesn't have signed zeros. */
8319 (if (!HONOR_SIGNED_ZEROS (@0))
8320 (convert (lt @0 { build_real (TREE_TYPE (@0), dconst0); }))))
8322 /* Transform comparisons of the form X +- C1 CMP C2 to X CMP C2 -+ C1. */
8324 (for op (plus minus)
8327 (cmp (op@3 @0 INTEGER_CST@1) INTEGER_CST@2)
8328 (if (!TREE_OVERFLOW (@1) && !TREE_OVERFLOW (@2)
8329 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0))
8330 && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@0))
8331 && !TYPE_SATURATING (TREE_TYPE (@0)))
8332 (with { tree res = int_const_binop (rop, @2, @1); }
8333 (if (TREE_OVERFLOW (res)
8334 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
8335 { constant_boolean_node (cmp == NE_EXPR, type); }
8336 (if (single_use (@3))
8337 (cmp @0 { TREE_OVERFLOW (res)
8338 ? drop_tree_overflow (res) : res; }))))))))
8339 (for cmp (lt le gt ge)
8340 (for op (plus minus)
8343 (cmp (op@3 @0 INTEGER_CST@1) INTEGER_CST@2)
8344 (if (!TREE_OVERFLOW (@1) && !TREE_OVERFLOW (@2)
8345 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
8346 (with { tree res = int_const_binop (rop, @2, @1); }
8347 (if (TREE_OVERFLOW (res))
8349 fold_overflow_warning (("assuming signed overflow does not occur "
8350 "when simplifying conditional to constant"),
8351 WARN_STRICT_OVERFLOW_CONDITIONAL);
8352 bool less = cmp == LE_EXPR || cmp == LT_EXPR;
8353 /* wi::ges_p (@2, 0) should be sufficient for a signed type. */
8354 bool ovf_high = wi::lt_p (wi::to_wide (@1), 0,
8355 TYPE_SIGN (TREE_TYPE (@1)))
8356 != (op == MINUS_EXPR);
8357 constant_boolean_node (less == ovf_high, type);
8359 (if (single_use (@3))
8362 fold_overflow_warning (("assuming signed overflow does not occur "
8363 "when changing X +- C1 cmp C2 to "
8365 WARN_STRICT_OVERFLOW_COMPARISON);
8367 (cmp @0 { res; })))))))))
8369 /* Canonicalizations of BIT_FIELD_REFs. */
8372 (BIT_FIELD_REF (BIT_FIELD_REF @0 @1 @2) @3 @4)
8373 (BIT_FIELD_REF @0 @3 { const_binop (PLUS_EXPR, bitsizetype, @2, @4); }))
8376 (BIT_FIELD_REF (view_convert @0) @1 @2)
8377 (if (! INTEGRAL_TYPE_P (TREE_TYPE (@0))
8378 || type_has_mode_precision_p (TREE_TYPE (@0)))
8379 (BIT_FIELD_REF @0 @1 @2)))
8382 (BIT_FIELD_REF @0 @1 integer_zerop)
8383 (if (tree_int_cst_equal (@1, TYPE_SIZE (TREE_TYPE (@0))))
8387 (BIT_FIELD_REF @0 @1 @2)
8389 (if (TREE_CODE (TREE_TYPE (@0)) == COMPLEX_TYPE
8390 && tree_int_cst_equal (@1, TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))))
8392 (if (integer_zerop (@2))
8393 (view_convert (realpart @0)))
8394 (if (tree_int_cst_equal (@2, TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))))
8395 (view_convert (imagpart @0)))))
8396 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
8397 && INTEGRAL_TYPE_P (type)
8398 /* On GIMPLE this should only apply to register arguments. */
8399 && (! GIMPLE || is_gimple_reg (@0))
8400 /* A bit-field-ref that referenced the full argument can be stripped. */
8401 && ((compare_tree_int (@1, TYPE_PRECISION (TREE_TYPE (@0))) == 0
8402 && integer_zerop (@2))
8403 /* Low-parts can be reduced to integral conversions.
8404 ??? The following doesn't work for PDP endian. */
8405 || (BYTES_BIG_ENDIAN == WORDS_BIG_ENDIAN
8406 /* But only do this after vectorization. */
8407 && canonicalize_math_after_vectorization_p ()
8408 /* Don't even think about BITS_BIG_ENDIAN. */
8409 && TYPE_PRECISION (TREE_TYPE (@0)) % BITS_PER_UNIT == 0
8410 && TYPE_PRECISION (type) % BITS_PER_UNIT == 0
8411 && compare_tree_int (@2, (BYTES_BIG_ENDIAN
8412 ? (TYPE_PRECISION (TREE_TYPE (@0))
8413 - TYPE_PRECISION (type))
8417 /* Simplify vector extracts. */
8420 (BIT_FIELD_REF CONSTRUCTOR@0 @1 @2)
8421 (if (VECTOR_TYPE_P (TREE_TYPE (@0))
8422 && tree_fits_uhwi_p (TYPE_SIZE (type))
8423 && ((tree_to_uhwi (TYPE_SIZE (type))
8424 == tree_to_uhwi (TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))))
8425 || (VECTOR_TYPE_P (type)
8426 && (tree_to_uhwi (TYPE_SIZE (TREE_TYPE (type)))
8427 == tree_to_uhwi (TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0))))))))
8430 tree ctor = (TREE_CODE (@0) == SSA_NAME
8431 ? gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0)) : @0);
8432 tree eltype = TREE_TYPE (TREE_TYPE (ctor));
8433 unsigned HOST_WIDE_INT width = tree_to_uhwi (TYPE_SIZE (eltype));
8434 unsigned HOST_WIDE_INT n = tree_to_uhwi (@1);
8435 unsigned HOST_WIDE_INT idx = tree_to_uhwi (@2);
8438 && (idx % width) == 0
8440 && known_le ((idx + n) / width,
8441 TYPE_VECTOR_SUBPARTS (TREE_TYPE (ctor))))
8446 /* Constructor elements can be subvectors. */
8448 if (CONSTRUCTOR_NELTS (ctor) != 0)
8450 tree cons_elem = TREE_TYPE (CONSTRUCTOR_ELT (ctor, 0)->value);
8451 if (TREE_CODE (cons_elem) == VECTOR_TYPE)
8452 k = TYPE_VECTOR_SUBPARTS (cons_elem);
8454 unsigned HOST_WIDE_INT elt, count, const_k;
8457 /* We keep an exact subset of the constructor elements. */
8458 (if (multiple_p (idx, k, &elt) && multiple_p (n, k, &count))
8459 (if (CONSTRUCTOR_NELTS (ctor) == 0)
8460 { build_zero_cst (type); }
8462 (if (elt < CONSTRUCTOR_NELTS (ctor))
8463 (view_convert { CONSTRUCTOR_ELT (ctor, elt)->value; })
8464 { build_zero_cst (type); })
8465 /* We don't want to emit new CTORs unless the old one goes away.
8466 ??? Eventually allow this if the CTOR ends up constant or
8468 (if (single_use (@0))
8471 vec<constructor_elt, va_gc> *vals;
8472 vec_alloc (vals, count);
8473 bool constant_p = true;
8475 for (unsigned i = 0;
8476 i < count && elt + i < CONSTRUCTOR_NELTS (ctor); ++i)
8478 tree e = CONSTRUCTOR_ELT (ctor, elt + i)->value;
8479 CONSTRUCTOR_APPEND_ELT (vals, NULL_TREE, e);
8480 if (!CONSTANT_CLASS_P (e))
8483 tree evtype = (types_match (TREE_TYPE (type),
8484 TREE_TYPE (TREE_TYPE (ctor)))
8486 : build_vector_type (TREE_TYPE (TREE_TYPE (ctor)),
8488 /* We used to build a CTOR in the non-constant case here
8489 but that's not a GIMPLE value. We'd have to expose this
8490 operation somehow so the code generation can properly
8491 split it out to a separate stmt. */
8492 res = (constant_p ? build_vector_from_ctor (evtype, vals)
8493 : (GIMPLE ? NULL_TREE : build_constructor (evtype, vals)));
8496 (view_convert { res; })))))))
8497 /* The bitfield references a single constructor element. */
8498 (if (k.is_constant (&const_k)
8499 && idx + n <= (idx / const_k + 1) * const_k)
8501 (if (CONSTRUCTOR_NELTS (ctor) <= idx / const_k)
8502 { build_zero_cst (type); })
8504 (view_convert { CONSTRUCTOR_ELT (ctor, idx / const_k)->value; }))
8505 (BIT_FIELD_REF { CONSTRUCTOR_ELT (ctor, idx / const_k)->value; }
8506 @1 { bitsize_int ((idx % const_k) * width); })))))))))
8508 /* Simplify a bit extraction from a bit insertion for the cases with
8509 the inserted element fully covering the extraction or the insertion
8510 not touching the extraction. */
8512 (BIT_FIELD_REF (bit_insert @0 @1 @ipos) @rsize @rpos)
8515 unsigned HOST_WIDE_INT isize;
8516 if (INTEGRAL_TYPE_P (TREE_TYPE (@1)))
8517 isize = TYPE_PRECISION (TREE_TYPE (@1));
8519 isize = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (@1)));
8522 (if ((!INTEGRAL_TYPE_P (TREE_TYPE (@1))
8523 || type_has_mode_precision_p (TREE_TYPE (@1)))
8524 && wi::leu_p (wi::to_wide (@ipos), wi::to_wide (@rpos))
8525 && wi::leu_p (wi::to_wide (@rpos) + wi::to_wide (@rsize),
8526 wi::to_wide (@ipos) + isize))
8527 (BIT_FIELD_REF @1 @rsize { wide_int_to_tree (bitsizetype,
8529 - wi::to_wide (@ipos)); }))
8530 (if (wi::eq_p (wi::to_wide (@ipos), wi::to_wide (@rpos))
8531 && compare_tree_int (@rsize, isize) == 0)
8533 (if (wi::geu_p (wi::to_wide (@ipos),
8534 wi::to_wide (@rpos) + wi::to_wide (@rsize))
8535 || wi::geu_p (wi::to_wide (@rpos),
8536 wi::to_wide (@ipos) + isize))
8537 (BIT_FIELD_REF @0 @rsize @rpos)))))
8539 /* Simplify vector inserts of other vector extracts to a permute. */
8541 (bit_insert @0 (BIT_FIELD_REF@2 @1 @rsize @rpos) @ipos)
8542 (if (VECTOR_TYPE_P (type)
8543 && (VECTOR_MODE_P (TYPE_MODE (type))
8544 || optimize_vectors_before_lowering_p ())
8545 && types_match (@0, @1)
8546 && types_match (TREE_TYPE (TREE_TYPE (@0)), TREE_TYPE (@2))
8547 && TYPE_VECTOR_SUBPARTS (type).is_constant ())
8550 unsigned HOST_WIDE_INT elsz
8551 = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (TREE_TYPE (@1))));
8552 poly_uint64 relt = exact_div (tree_to_poly_uint64 (@rpos), elsz);
8553 poly_uint64 ielt = exact_div (tree_to_poly_uint64 (@ipos), elsz);
8554 unsigned nunits = TYPE_VECTOR_SUBPARTS (type).to_constant ();
8555 vec_perm_builder builder;
8556 builder.new_vector (nunits, nunits, 1);
8557 for (unsigned i = 0; i < nunits; ++i)
8558 builder.quick_push (known_eq (ielt, i) ? nunits + relt : i);
8559 vec_perm_indices sel (builder, 2, nunits);
8561 (if (!VECTOR_MODE_P (TYPE_MODE (type))
8562 || can_vec_perm_const_p (TYPE_MODE (type), TYPE_MODE (type), sel, false))
8563 (vec_perm @0 @1 { vec_perm_indices_to_tree
8564 (build_vector_type (ssizetype, nunits), sel); })))))
8566 (if (canonicalize_math_after_vectorization_p ())
8569 (fmas:c (negate @0) @1 @2)
8570 (IFN_FNMA @0 @1 @2))
8572 (fmas @0 @1 (negate @2))
8575 (fmas:c (negate @0) @1 (negate @2))
8576 (IFN_FNMS @0 @1 @2))
8578 (negate (fmas@3 @0 @1 @2))
8579 (if (single_use (@3))
8580 (IFN_FNMS @0 @1 @2))))
8583 (IFN_FMS:c (negate @0) @1 @2)
8584 (IFN_FNMS @0 @1 @2))
8586 (IFN_FMS @0 @1 (negate @2))
8589 (IFN_FMS:c (negate @0) @1 (negate @2))
8590 (IFN_FNMA @0 @1 @2))
8592 (negate (IFN_FMS@3 @0 @1 @2))
8593 (if (single_use (@3))
8594 (IFN_FNMA @0 @1 @2)))
8597 (IFN_FNMA:c (negate @0) @1 @2)
8600 (IFN_FNMA @0 @1 (negate @2))
8601 (IFN_FNMS @0 @1 @2))
8603 (IFN_FNMA:c (negate @0) @1 (negate @2))
8606 (negate (IFN_FNMA@3 @0 @1 @2))
8607 (if (single_use (@3))
8608 (IFN_FMS @0 @1 @2)))
8611 (IFN_FNMS:c (negate @0) @1 @2)
8614 (IFN_FNMS @0 @1 (negate @2))
8615 (IFN_FNMA @0 @1 @2))
8617 (IFN_FNMS:c (negate @0) @1 (negate @2))
8620 (negate (IFN_FNMS@3 @0 @1 @2))
8621 (if (single_use (@3))
8622 (IFN_FMA @0 @1 @2))))
8624 /* CLZ simplifications. */
8629 (op (clz:s@2 @0) INTEGER_CST@1)
8630 (if (integer_zerop (@1) && single_use (@2))
8631 /* clz(X) == 0 is (int)X < 0 and clz(X) != 0 is (int)X >= 0. */
8632 (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
8633 (cmp (convert:stype @0) { build_zero_cst (stype); }))
8634 /* clz(X) == (prec-1) is X == 1 and clz(X) != (prec-1) is X != 1. */
8635 (if (wi::to_wide (@1) == TYPE_PRECISION (TREE_TYPE (@0)) - 1)
8636 (op @0 { build_one_cst (TREE_TYPE (@0)); }))))))
8640 (op (IFN_CLZ:s@2 @0 @3) INTEGER_CST@1)
8641 (if (integer_zerop (@1) && single_use (@2))
8642 /* clz(X) == 0 is (int)X < 0 and clz(X) != 0 is (int)X >= 0. */
8643 (with { tree type0 = TREE_TYPE (@0);
8644 tree stype = signed_type_for (TREE_TYPE (@0));
8645 /* Punt if clz(0) == 0. */
8646 if (integer_zerop (@3))
8650 (cmp (convert:stype @0) { build_zero_cst (stype); })))
8651 /* clz(X) == (prec-1) is X == 1 and clz(X) != (prec-1) is X != 1. */
8652 (with { bool ok = true;
8653 tree type0 = TREE_TYPE (@0);
8654 /* Punt if clz(0) == prec - 1. */
8655 if (wi::to_widest (@3) == TYPE_PRECISION (type0) - 1)
8658 (if (ok && wi::to_wide (@1) == (TYPE_PRECISION (type0) - 1))
8659 (op @0 { build_one_cst (type0); }))))))
8661 /* CTZ simplifications. */
8663 (for op (ge gt le lt)
8666 /* __builtin_ctz (x) >= C -> (x & ((1 << C) - 1)) == 0. */
8667 (op (ctz:s @0) INTEGER_CST@1)
8668 (with { bool ok = true;
8669 HOST_WIDE_INT val = 0;
8670 if (!tree_fits_shwi_p (@1))
8674 val = tree_to_shwi (@1);
8675 /* Canonicalize to >= or <. */
8676 if (op == GT_EXPR || op == LE_EXPR)
8678 if (val == HOST_WIDE_INT_MAX)
8684 tree type0 = TREE_TYPE (@0);
8685 int prec = TYPE_PRECISION (type0);
8687 (if (ok && prec <= MAX_FIXED_MODE_SIZE)
8689 { constant_boolean_node (cmp == EQ_EXPR ? true : false, type); }
8691 { constant_boolean_node (cmp == EQ_EXPR ? false : true, type); }
8692 (cmp (bit_and @0 { wide_int_to_tree (type0,
8693 wi::mask (val, false, prec)); })
8694 { build_zero_cst (type0); })))))))
8697 /* __builtin_ctz (x) == C -> (x & ((1 << (C + 1)) - 1)) == (1 << C). */
8698 (op (ctz:s @0) INTEGER_CST@1)
8699 (with { tree type0 = TREE_TYPE (@0);
8700 int prec = TYPE_PRECISION (type0);
8702 (if (prec <= MAX_FIXED_MODE_SIZE)
8703 (if (tree_int_cst_sgn (@1) < 0 || wi::to_widest (@1) >= prec)
8704 { constant_boolean_node (op == EQ_EXPR ? false : true, type); }
8705 (op (bit_and @0 { wide_int_to_tree (type0,
8706 wi::mask (tree_to_uhwi (@1) + 1,
8708 { wide_int_to_tree (type0,
8709 wi::shifted_mask (tree_to_uhwi (@1), 1,
8710 false, prec)); })))))))
8711 (for op (ge gt le lt)
8714 /* __builtin_ctz (x) >= C -> (x & ((1 << C) - 1)) == 0. */
8715 (op (IFN_CTZ:s @0 @2) INTEGER_CST@1)
8716 (with { bool ok = true;
8717 HOST_WIDE_INT val = 0;
8718 if (!tree_fits_shwi_p (@1))
8722 val = tree_to_shwi (@1);
8723 /* Canonicalize to >= or <. */
8724 if (op == GT_EXPR || op == LE_EXPR)
8726 if (val == HOST_WIDE_INT_MAX)
8732 HOST_WIDE_INT zero_val = tree_to_shwi (@2);
8733 tree type0 = TREE_TYPE (@0);
8734 int prec = TYPE_PRECISION (type0);
8735 if (prec > MAX_FIXED_MODE_SIZE)
8739 (if (ok && zero_val >= val)
8740 { constant_boolean_node (cmp == EQ_EXPR ? true : false, type); })
8742 (if (ok && zero_val < val)
8743 { constant_boolean_node (cmp == EQ_EXPR ? false : true, type); })
8744 (if (ok && (zero_val < 0 || zero_val >= prec))
8745 (cmp (bit_and @0 { wide_int_to_tree (type0,
8746 wi::mask (val, false, prec)); })
8747 { build_zero_cst (type0); })))))))
8750 /* __builtin_ctz (x) == C -> (x & ((1 << (C + 1)) - 1)) == (1 << C). */
8751 (op (IFN_CTZ:s @0 @2) INTEGER_CST@1)
8752 (with { HOST_WIDE_INT zero_val = tree_to_shwi (@2);
8753 tree type0 = TREE_TYPE (@0);
8754 int prec = TYPE_PRECISION (type0);
8756 (if (prec <= MAX_FIXED_MODE_SIZE)
8757 (if (tree_int_cst_sgn (@1) < 0 || wi::to_widest (@1) >= prec)
8758 (if (zero_val != wi::to_widest (@1))
8759 { constant_boolean_node (op == EQ_EXPR ? false : true, type); })
8760 (if (zero_val < 0 || zero_val >= prec)
8761 (op (bit_and @0 { wide_int_to_tree (type0,
8762 wi::mask (tree_to_uhwi (@1) + 1,
8764 { wide_int_to_tree (type0,
8765 wi::shifted_mask (tree_to_uhwi (@1), 1,
8766 false, prec)); })))))))
8769 /* ctz(ext(X)) == ctz(X). Valid just for the UB at zero cases though. */
8771 (CTZ (convert@1 @0))
8772 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
8773 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
8774 && TYPE_PRECISION (TREE_TYPE (@1)) > TYPE_PRECISION (TREE_TYPE (@0)))
8775 (with { combined_fn cfn = CFN_LAST;
8776 tree type0 = TREE_TYPE (@0);
8777 if (TREE_CODE (type0) == BITINT_TYPE)
8779 if (TYPE_PRECISION (type0) > MAX_FIXED_MODE_SIZE)
8783 = build_nonstandard_integer_type (TYPE_PRECISION (type0),
8786 type0 = unsigned_type_for (type0);
8788 && direct_internal_fn_supported_p (IFN_CTZ, type0,
8792 && TYPE_PRECISION (TREE_TYPE (@1)) > BITS_PER_WORD
8793 && !direct_internal_fn_supported_p (IFN_CTZ,
8797 if (TYPE_PRECISION (type0)
8798 == TYPE_PRECISION (unsigned_type_node))
8799 cfn = CFN_BUILT_IN_CTZ;
8800 else if (TYPE_PRECISION (type0)
8801 == TYPE_PRECISION (long_long_unsigned_type_node))
8802 cfn = CFN_BUILT_IN_CTZLL;
8804 (if (cfn == CFN_CTZ)
8805 (IFN_CTZ (convert:type0 @0))
8806 (if (cfn == CFN_BUILT_IN_CTZ)
8807 (BUILT_IN_CTZ (convert:type0 @0))
8808 (if (cfn == CFN_BUILT_IN_CTZLL)
8809 (BUILT_IN_CTZLL (convert:type0 @0))))))))
8812 /* POPCOUNT simplifications. */
8813 /* popcount(X) + popcount(Y) is popcount(X|Y) when X&Y must be zero. */
8815 (plus (POPCOUNT:s @0) (POPCOUNT:s @1))
8816 (if (INTEGRAL_TYPE_P (type)
8817 && (wi::bit_and (widest_int::from (tree_nonzero_bits (@0), UNSIGNED),
8818 widest_int::from (tree_nonzero_bits (@1), UNSIGNED))
8820 (with { tree utype = TREE_TYPE (@0);
8821 if (TYPE_PRECISION (utype) < TYPE_PRECISION (TREE_TYPE (@1)))
8822 utype = TREE_TYPE (@1); }
8823 (POPCOUNT (bit_ior (convert:utype @0) (convert:utype @1))))))
8825 /* popcount(X) == 0 is X == 0, and related (in)equalities. */
8826 (for popcount (POPCOUNT)
8827 (for cmp (le eq ne gt)
8830 (cmp (popcount @0) integer_zerop)
8831 (rep @0 { build_zero_cst (TREE_TYPE (@0)); }))))
8833 /* popcount(bswap(x)) is popcount(x). */
8834 (for popcount (POPCOUNT)
8835 (for bswap (BUILT_IN_BSWAP16 BUILT_IN_BSWAP32
8836 BUILT_IN_BSWAP64 BUILT_IN_BSWAP128)
8838 (popcount (convert?@0 (bswap:s@1 @2)))
8839 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
8840 && INTEGRAL_TYPE_P (TREE_TYPE (@1)))
8841 (with { tree type0 = TREE_TYPE (@0);
8842 tree type1 = TREE_TYPE (@1);
8843 unsigned int prec0 = TYPE_PRECISION (type0);
8844 unsigned int prec1 = TYPE_PRECISION (type1); }
8845 (if (prec0 == prec1 || (prec0 > prec1 && TYPE_UNSIGNED (type1)))
8846 (popcount (convert:type0 (convert:type1 @2)))))))))
8848 /* popcount(rotate(X Y)) is popcount(X). */
8849 (for popcount (POPCOUNT)
8850 (for rot (lrotate rrotate)
8852 (popcount (convert?@0 (rot:s@1 @2 @3)))
8853 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
8854 && INTEGRAL_TYPE_P (TREE_TYPE (@1))
8855 && (GIMPLE || !TREE_SIDE_EFFECTS (@3)))
8856 (with { tree type0 = TREE_TYPE (@0);
8857 tree type1 = TREE_TYPE (@1);
8858 unsigned int prec0 = TYPE_PRECISION (type0);
8859 unsigned int prec1 = TYPE_PRECISION (type1); }
8860 (if (prec0 == prec1 || (prec0 > prec1 && TYPE_UNSIGNED (type1)))
8861 (popcount (convert:type0 @2))))))))
8863 /* Canonicalize POPCOUNT(x)&1 as PARITY(X). */
8865 (bit_and (POPCOUNT @0) integer_onep)
8868 /* popcount(X&Y) + popcount(X|Y) is popcount(x) + popcount(Y). */
8870 (plus:c (POPCOUNT:s (bit_and:s @0 @1)) (POPCOUNT:s (bit_ior:cs @0 @1)))
8871 (plus (POPCOUNT:type @0) (POPCOUNT:type @1)))
8873 /* popcount(X) + popcount(Y) - popcount(X&Y) is popcount(X|Y). */
8874 /* popcount(X) + popcount(Y) - popcount(X|Y) is popcount(X&Y). */
8875 (for popcount (POPCOUNT)
8876 (for log1 (bit_and bit_ior)
8877 log2 (bit_ior bit_and)
8879 (minus (plus:s (popcount:s @0) (popcount:s @1))
8880 (popcount:s (log1:cs @0 @1)))
8881 (popcount (log2 @0 @1)))
8883 (plus:c (minus:s (popcount:s @0) (popcount:s (log1:cs @0 @1)))
8885 (popcount (log2 @0 @1)))))
8888 /* popcount(zext(X)) == popcount(X). */
8890 (POPCOUNT (convert@1 @0))
8891 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
8892 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
8893 && TYPE_UNSIGNED (TREE_TYPE (@0))
8894 && TYPE_PRECISION (TREE_TYPE (@1)) > TYPE_PRECISION (TREE_TYPE (@0)))
8895 (with { combined_fn cfn = CFN_LAST;
8896 tree type0 = TREE_TYPE (@0);
8897 if (TREE_CODE (type0) == BITINT_TYPE)
8899 if (TYPE_PRECISION (type0) > MAX_FIXED_MODE_SIZE)
8903 = build_nonstandard_integer_type (TYPE_PRECISION (type0),
8907 && direct_internal_fn_supported_p (IFN_POPCOUNT, type0,
8911 && TYPE_PRECISION (TREE_TYPE (@1)) > BITS_PER_WORD
8912 && !direct_internal_fn_supported_p (IFN_POPCOUNT,
8916 if (TYPE_PRECISION (type0)
8917 == TYPE_PRECISION (unsigned_type_node))
8918 cfn = CFN_BUILT_IN_POPCOUNT;
8919 else if (TYPE_PRECISION (type0)
8920 == TYPE_PRECISION (long_long_unsigned_type_node))
8921 cfn = CFN_BUILT_IN_POPCOUNTLL;
8923 (if (cfn == CFN_POPCOUNT)
8924 (IFN_POPCOUNT (convert:type0 @0))
8925 (if (cfn == CFN_BUILT_IN_POPCOUNT)
8926 (BUILT_IN_POPCOUNT (convert:type0 @0))
8927 (if (cfn == CFN_BUILT_IN_POPCOUNTLL)
8928 (BUILT_IN_POPCOUNTLL (convert:type0 @0))))))))
8931 /* PARITY simplifications. */
8932 /* parity(~X) is parity(X). */
8934 (PARITY (bit_not @0))
8937 /* parity(bswap(x)) is parity(x). */
8938 (for parity (PARITY)
8939 (for bswap (BUILT_IN_BSWAP16 BUILT_IN_BSWAP32
8940 BUILT_IN_BSWAP64 BUILT_IN_BSWAP128)
8942 (parity (convert?@0 (bswap:s@1 @2)))
8943 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
8944 && INTEGRAL_TYPE_P (TREE_TYPE (@1))
8945 && TYPE_PRECISION (TREE_TYPE (@0))
8946 >= TYPE_PRECISION (TREE_TYPE (@1)))
8947 (with { tree type0 = TREE_TYPE (@0);
8948 tree type1 = TREE_TYPE (@1); }
8949 (parity (convert:type0 (convert:type1 @2))))))))
8951 /* parity(rotate(X Y)) is parity(X). */
8952 (for parity (PARITY)
8953 (for rot (lrotate rrotate)
8955 (parity (convert?@0 (rot:s@1 @2 @3)))
8956 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
8957 && INTEGRAL_TYPE_P (TREE_TYPE (@1))
8958 && (GIMPLE || !TREE_SIDE_EFFECTS (@3))
8959 && TYPE_PRECISION (TREE_TYPE (@0))
8960 >= TYPE_PRECISION (TREE_TYPE (@1)))
8961 (with { tree type0 = TREE_TYPE (@0); }
8962 (parity (convert:type0 @2)))))))
8964 /* parity(X)^parity(Y) is parity(X^Y). */
8966 (bit_xor (PARITY:s @0) (PARITY:s @1))
8967 (if (types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
8968 (PARITY (bit_xor @0 @1))
8969 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
8970 && INTEGRAL_TYPE_P (TREE_TYPE (@1)))
8971 (with { tree utype = TREE_TYPE (@0);
8972 if (TYPE_PRECISION (utype) < TYPE_PRECISION (TREE_TYPE (@1)))
8973 utype = TREE_TYPE (@1); }
8974 (PARITY (bit_xor (convert:utype @0) (convert:utype @1)))))))
8977 /* parity(zext(X)) == parity(X). */
8978 /* parity(sext(X)) == parity(X) if the difference in precision is even. */
8980 (PARITY (convert@1 @0))
8981 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
8982 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
8983 && TYPE_PRECISION (TREE_TYPE (@1)) > TYPE_PRECISION (TREE_TYPE (@0))
8984 && (TYPE_UNSIGNED (TREE_TYPE (@0))
8985 || ((TYPE_PRECISION (TREE_TYPE (@1))
8986 - TYPE_PRECISION (TREE_TYPE (@0))) & 1) == 0))
8987 (with { combined_fn cfn = CFN_LAST;
8988 tree type0 = TREE_TYPE (@0);
8989 if (TREE_CODE (type0) == BITINT_TYPE)
8991 if (TYPE_PRECISION (type0) > MAX_FIXED_MODE_SIZE)
8995 = build_nonstandard_integer_type (TYPE_PRECISION (type0),
8998 type0 = unsigned_type_for (type0);
9000 && direct_internal_fn_supported_p (IFN_PARITY, type0,
9004 && TYPE_PRECISION (TREE_TYPE (@1)) > BITS_PER_WORD
9005 && !direct_internal_fn_supported_p (IFN_PARITY,
9009 if (TYPE_PRECISION (type0)
9010 == TYPE_PRECISION (unsigned_type_node))
9011 cfn = CFN_BUILT_IN_PARITY;
9012 else if (TYPE_PRECISION (type0)
9013 == TYPE_PRECISION (long_long_unsigned_type_node))
9014 cfn = CFN_BUILT_IN_PARITYLL;
9016 (if (cfn == CFN_PARITY)
9017 (IFN_PARITY (convert:type0 @0))
9018 (if (cfn == CFN_BUILT_IN_PARITY)
9019 (BUILT_IN_PARITY (convert:type0 @0))
9020 (if (cfn == CFN_BUILT_IN_PARITYLL)
9021 (BUILT_IN_PARITYLL (convert:type0 @0))))))))
9024 /* a != 0 ? FUN(a) : 0 -> Fun(a) for some builtin functions. */
9025 (for func (POPCOUNT BSWAP FFS PARITY)
9027 (cond (ne @0 integer_zerop@1) (func@3 (convert? @0)) integer_zerop@2)
9030 /* a != 0 ? FUN(a) : CST -> Fun(a) for some CLRSB builtins
9031 where CST is precision-1. */
9034 (cond (ne @0 integer_zerop@1) (func@4 (convert?@3 @0)) INTEGER_CST@2)
9035 (if (wi::to_widest (@2) == TYPE_PRECISION (TREE_TYPE (@3)) - 1)
9039 /* a != 0 ? CLZ(a) : CST -> .CLZ(a) where CST is the result of the internal function for 0. */
9042 (cond (ne @0 integer_zerop@1) (func (convert?@3 @0)) INTEGER_CST@2)
9044 internal_fn ifn = IFN_LAST;
9045 if (TREE_CODE (TREE_TYPE (@3)) == BITINT_TYPE)
9047 if (tree_fits_shwi_p (@2))
9049 HOST_WIDE_INT valw = tree_to_shwi (@2);
9050 if ((int) valw == valw)
9057 else if (direct_internal_fn_supported_p (IFN_CLZ, TREE_TYPE (@3),
9059 && CLZ_DEFINED_VALUE_AT_ZERO
9060 (SCALAR_INT_TYPE_MODE (TREE_TYPE (@3)), val) == 2)
9063 (if (ifn == IFN_CLZ && wi::to_widest (@2) == val)
9066 (cond (ne @0 integer_zerop@1) (IFN_CLZ (convert?@3 @0) INTEGER_CST@2) @2)
9068 internal_fn ifn = IFN_LAST;
9069 if (TREE_CODE (TREE_TYPE (@3)) == BITINT_TYPE)
9071 else if (direct_internal_fn_supported_p (IFN_CLZ, TREE_TYPE (@3),
9075 (if (ifn == IFN_CLZ)
9078 /* a != 0 ? CTZ(a) : CST -> .CTZ(a) where CST is the result of the internal function for 0. */
9081 (cond (ne @0 integer_zerop@1) (func (convert?@3 @0)) INTEGER_CST@2)
9083 internal_fn ifn = IFN_LAST;
9084 if (TREE_CODE (TREE_TYPE (@3)) == BITINT_TYPE)
9086 if (tree_fits_shwi_p (@2))
9088 HOST_WIDE_INT valw = tree_to_shwi (@2);
9089 if ((int) valw == valw)
9096 else if (direct_internal_fn_supported_p (IFN_CTZ, TREE_TYPE (@3),
9098 && CTZ_DEFINED_VALUE_AT_ZERO
9099 (SCALAR_INT_TYPE_MODE (TREE_TYPE (@3)), val) == 2)
9102 (if (ifn == IFN_CTZ && wi::to_widest (@2) == val)
9105 (cond (ne @0 integer_zerop@1) (IFN_CTZ (convert?@3 @0) INTEGER_CST@2) @2)
9107 internal_fn ifn = IFN_LAST;
9108 if (TREE_CODE (TREE_TYPE (@3)) == BITINT_TYPE)
9110 else if (direct_internal_fn_supported_p (IFN_CTZ, TREE_TYPE (@3),
9114 (if (ifn == IFN_CTZ)
9118 /* Common POPCOUNT/PARITY simplifications. */
9119 /* popcount(X&C1) is (X>>C2)&1 when C1 == 1<<C2. Same for parity(X&C1). */
9120 (for pfun (POPCOUNT PARITY)
9123 (if (INTEGRAL_TYPE_P (type))
9124 (with { wide_int nz = tree_nonzero_bits (@0); }
9128 (if (wi::popcount (nz) == 1)
9129 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
9130 (convert (rshift:utype (convert:utype @0)
9131 { build_int_cst (integer_type_node,
9132 wi::ctz (nz)); })))))))))
9135 /* 64- and 32-bits branchless implementations of popcount are detected:
9137 int popcount64c (uint64_t x)
9139 x -= (x >> 1) & 0x5555555555555555ULL;
9140 x = (x & 0x3333333333333333ULL) + ((x >> 2) & 0x3333333333333333ULL);
9141 x = (x + (x >> 4)) & 0x0f0f0f0f0f0f0f0fULL;
9142 return (x * 0x0101010101010101ULL) >> 56;
9145 int popcount32c (uint32_t x)
9147 x -= (x >> 1) & 0x55555555;
9148 x = (x & 0x33333333) + ((x >> 2) & 0x33333333);
9149 x = (x + (x >> 4)) & 0x0f0f0f0f;
9150 return (x * 0x01010101) >> 24;
9157 (rshift @8 INTEGER_CST@5)
9159 (bit_and @6 INTEGER_CST@7)
9163 (bit_and (rshift @0 INTEGER_CST@4) INTEGER_CST@11))
9169 /* Check constants and optab. */
9170 (with { unsigned prec = TYPE_PRECISION (type);
9171 int shift = (64 - prec) & 63;
9172 unsigned HOST_WIDE_INT c1
9173 = HOST_WIDE_INT_UC (0x0101010101010101) >> shift;
9174 unsigned HOST_WIDE_INT c2
9175 = HOST_WIDE_INT_UC (0x0F0F0F0F0F0F0F0F) >> shift;
9176 unsigned HOST_WIDE_INT c3
9177 = HOST_WIDE_INT_UC (0x3333333333333333) >> shift;
9178 unsigned HOST_WIDE_INT c4
9179 = HOST_WIDE_INT_UC (0x5555555555555555) >> shift;
9184 && TYPE_UNSIGNED (type)
9185 && integer_onep (@4)
9186 && wi::to_widest (@10) == 2
9187 && wi::to_widest (@5) == 4
9188 && wi::to_widest (@1) == prec - 8
9189 && tree_to_uhwi (@2) == c1
9190 && tree_to_uhwi (@3) == c2
9191 && tree_to_uhwi (@9) == c3
9192 && tree_to_uhwi (@7) == c3
9193 && tree_to_uhwi (@11) == c4)
9194 (if (direct_internal_fn_supported_p (IFN_POPCOUNT, type,
9196 (convert (IFN_POPCOUNT:type @0))
9197 /* Try to do popcount in two halves. PREC must be at least
9198 five bits for this to work without extension before adding. */
9200 tree half_type = NULL_TREE;
9201 opt_machine_mode m = mode_for_size ((prec + 1) / 2, MODE_INT, 1);
9204 && m.require () != TYPE_MODE (type))
9206 half_prec = GET_MODE_PRECISION (as_a <scalar_int_mode> (m));
9207 half_type = build_nonstandard_integer_type (half_prec, 1);
9209 gcc_assert (half_prec > 2);
9211 (if (half_type != NULL_TREE
9212 && direct_internal_fn_supported_p (IFN_POPCOUNT, half_type,
9215 (IFN_POPCOUNT:half_type (convert @0))
9216 (IFN_POPCOUNT:half_type (convert (rshift @0
9217 { build_int_cst (integer_type_node, half_prec); } )))))))))))
9219 /* __builtin_ffs needs to deal on many targets with the possible zero
9220 argument. If we know the argument is always non-zero, __builtin_ctz + 1
9221 should lead to better code. */
9223 (FFS tree_expr_nonzero_p@0)
9224 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
9225 && direct_internal_fn_supported_p (IFN_CTZ, TREE_TYPE (@0),
9226 OPTIMIZE_FOR_SPEED))
9227 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
9228 (plus (CTZ:type (convert:utype @0)) { build_one_cst (type); }))))
9232 /* __builtin_ffs (X) == 0 -> X == 0.
9233 __builtin_ffs (X) == 6 -> (X & 63) == 32. */
9236 (cmp (ffs@2 @0) INTEGER_CST@1)
9237 (with { int prec = TYPE_PRECISION (TREE_TYPE (@0)); }
9239 (if (integer_zerop (@1))
9240 (cmp @0 { build_zero_cst (TREE_TYPE (@0)); }))
9241 (if (tree_int_cst_sgn (@1) < 0 || wi::to_widest (@1) > prec)
9242 { constant_boolean_node (cmp == NE_EXPR ? true : false, type); })
9243 (if (single_use (@2))
9244 (cmp (bit_and @0 { wide_int_to_tree (TREE_TYPE (@0),
9245 wi::mask (tree_to_uhwi (@1),
9247 { wide_int_to_tree (TREE_TYPE (@0),
9248 wi::shifted_mask (tree_to_uhwi (@1) - 1, 1,
9249 false, prec)); }))))))
9251 /* __builtin_ffs (X) > 6 -> X != 0 && (X & 63) == 0. */
9255 bit_op (bit_and bit_ior)
9257 (cmp (ffs@2 @0) INTEGER_CST@1)
9258 (with { int prec = TYPE_PRECISION (TREE_TYPE (@0)); }
9260 (if (integer_zerop (@1))
9261 (cmp2 @0 { build_zero_cst (TREE_TYPE (@0)); }))
9262 (if (tree_int_cst_sgn (@1) < 0)
9263 { constant_boolean_node (cmp == GT_EXPR ? true : false, type); })
9264 (if (wi::to_widest (@1) >= prec)
9265 { constant_boolean_node (cmp == GT_EXPR ? false : true, type); })
9266 (if (wi::to_widest (@1) == prec - 1)
9267 (cmp3 @0 { wide_int_to_tree (TREE_TYPE (@0),
9268 wi::shifted_mask (prec - 1, 1,
9270 (if (single_use (@2))
9271 (bit_op (cmp2 @0 { build_zero_cst (TREE_TYPE (@0)); })
9273 { wide_int_to_tree (TREE_TYPE (@0),
9274 wi::mask (tree_to_uhwi (@1),
9276 { build_zero_cst (TREE_TYPE (@0)); }))))))))
9279 /* ffs(ext(X)) == ffs(X). */
9281 (FFS (convert@1 @0))
9282 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
9283 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
9284 && TYPE_PRECISION (TREE_TYPE (@1)) > TYPE_PRECISION (TREE_TYPE (@0)))
9285 (with { combined_fn cfn = CFN_LAST;
9286 tree type0 = TREE_TYPE (@0);
9287 if (TREE_CODE (type0) == BITINT_TYPE)
9289 if (TYPE_PRECISION (type0) > MAX_FIXED_MODE_SIZE)
9293 = build_nonstandard_integer_type (TYPE_PRECISION (type0),
9296 type0 = signed_type_for (type0);
9298 && direct_internal_fn_supported_p (IFN_FFS, type0,
9302 && TYPE_PRECISION (TREE_TYPE (@1)) > BITS_PER_WORD
9303 && !direct_internal_fn_supported_p (IFN_FFS,
9307 if (TYPE_PRECISION (type0)
9308 == TYPE_PRECISION (integer_type_node))
9309 cfn = CFN_BUILT_IN_FFS;
9310 else if (TYPE_PRECISION (type0)
9311 == TYPE_PRECISION (long_long_integer_type_node))
9312 cfn = CFN_BUILT_IN_FFSLL;
9314 (if (cfn == CFN_FFS)
9315 (IFN_FFS (convert:type0 @0))
9316 (if (cfn == CFN_BUILT_IN_FFS)
9317 (BUILT_IN_FFS (convert:type0 @0))
9318 (if (cfn == CFN_BUILT_IN_FFSLL)
9319 (BUILT_IN_FFSLL (convert:type0 @0))))))))
9327 --> r = .COND_FN (cond, a, b)
9331 --> r = .COND_FN (~cond, b, a). */
9333 (for uncond_op (UNCOND_UNARY)
9334 cond_op (COND_UNARY)
9336 (vec_cond @0 (view_convert? (uncond_op@3 @1)) @2)
9337 (with { tree op_type = TREE_TYPE (@3); }
9338 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
9339 && is_truth_type_for (op_type, TREE_TYPE (@0)))
9340 (cond_op @0 (view_convert @1) @2))))
9342 (vec_cond @0 @1 (view_convert? (uncond_op@3 @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 (bit_not @0) (view_convert @2) @1)))))
9348 (for uncond_op (UNCOND_UNARY)
9349 cond_op (COND_LEN_UNARY)
9351 (IFN_VCOND_MASK_LEN @0 (view_convert? (uncond_op@3 @1)) @2 @4 @5)
9352 (with { tree op_type = TREE_TYPE (@3); }
9353 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
9354 && is_truth_type_for (op_type, TREE_TYPE (@0)))
9355 (cond_op @0 (view_convert @1) @2 @4 @5))))
9357 (IFN_VCOND_MASK_LEN @0 @1 (view_convert? (uncond_op@3 @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 (bit_not @0) (view_convert @2) @1 @4 @5)))))
9363 /* `(a ? -1 : 0) ^ b` can be converted into a conditional not. */
9365 (bit_xor:c (vec_cond @0 uniform_integer_cst_p@1 uniform_integer_cst_p@2) @3)
9366 (if (canonicalize_math_after_vectorization_p ()
9367 && vectorized_internal_fn_supported_p (IFN_COND_NOT, type)
9368 && is_truth_type_for (type, TREE_TYPE (@0)))
9369 (if (integer_all_onesp (@1) && integer_zerop (@2))
9370 (IFN_COND_NOT @0 @3 @3))
9371 (if (integer_all_onesp (@2) && integer_zerop (@1))
9372 (IFN_COND_NOT (bit_not @0) @3 @3))))
9381 r = c ? a1 op a2 : b;
9383 if the target can do it in one go. This makes the operation conditional
9384 on c, so could drop potentially-trapping arithmetic, but that's a valid
9385 simplification if the result of the operation isn't needed.
9387 Avoid speculatively generating a stand-alone vector comparison
9388 on targets that might not support them. Any target implementing
9389 conditional internal functions must support the same comparisons
9390 inside and outside a VEC_COND_EXPR. */
9392 (for uncond_op (UNCOND_BINARY)
9393 cond_op (COND_BINARY)
9395 (vec_cond @0 (view_convert? (uncond_op@4 @1 @2)) @3)
9396 (with { tree op_type = TREE_TYPE (@4); }
9397 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
9398 && is_truth_type_for (op_type, TREE_TYPE (@0))
9400 (view_convert (cond_op @0 @1 @2 (view_convert:op_type @3))))))
9402 (vec_cond @0 @1 (view_convert? (uncond_op@4 @2 @3)))
9403 (with { tree op_type = TREE_TYPE (@4); }
9404 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
9405 && is_truth_type_for (op_type, TREE_TYPE (@0))
9407 (view_convert (cond_op (bit_not @0) @2 @3 (view_convert:op_type @1)))))))
9409 (for uncond_op (UNCOND_BINARY)
9410 cond_op (COND_LEN_BINARY)
9412 (IFN_VCOND_MASK_LEN @0 (view_convert? (uncond_op@4 @1 @2)) @3 @5 @6)
9413 (with { tree op_type = TREE_TYPE (@4); }
9414 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
9415 && is_truth_type_for (op_type, TREE_TYPE (@0))
9417 (view_convert (cond_op @0 @1 @2 (view_convert:op_type @3) @5 @6)))))
9419 (IFN_VCOND_MASK_LEN @0 @1 (view_convert? (uncond_op@4 @2 @3)) @5 @6)
9420 (with { tree op_type = TREE_TYPE (@4); }
9421 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
9422 && is_truth_type_for (op_type, TREE_TYPE (@0))
9424 (view_convert (cond_op (bit_not @0) @2 @3 (view_convert:op_type @1) @5 @6))))))
9426 /* Same for ternary operations. */
9427 (for uncond_op (UNCOND_TERNARY)
9428 cond_op (COND_TERNARY)
9430 (vec_cond @0 (view_convert? (uncond_op@5 @1 @2 @3)) @4)
9431 (with { tree op_type = TREE_TYPE (@5); }
9432 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
9433 && is_truth_type_for (op_type, TREE_TYPE (@0))
9435 (view_convert (cond_op @0 @1 @2 @3 (view_convert:op_type @4))))))
9437 (vec_cond @0 @1 (view_convert? (uncond_op@5 @2 @3 @4)))
9438 (with { tree op_type = TREE_TYPE (@5); }
9439 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
9440 && is_truth_type_for (op_type, TREE_TYPE (@0))
9442 (view_convert (cond_op (bit_not @0) @2 @3 @4
9443 (view_convert:op_type @1)))))))
9445 (for uncond_op (UNCOND_TERNARY)
9446 cond_op (COND_LEN_TERNARY)
9448 (IFN_VCOND_MASK_LEN @0 (view_convert? (uncond_op@5 @1 @2 @3)) @4 @6 @7)
9449 (with { tree op_type = TREE_TYPE (@5); }
9450 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
9451 && is_truth_type_for (op_type, TREE_TYPE (@0))
9453 (view_convert (cond_op @0 @1 @2 @3 (view_convert:op_type @4) @6 @7)))))
9455 (IFN_VCOND_MASK_LEN @0 @1 (view_convert? (uncond_op@5 @2 @3 @4 @6 @7)))
9456 (with { tree op_type = TREE_TYPE (@5); }
9457 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
9458 && is_truth_type_for (op_type, TREE_TYPE (@0))
9460 (view_convert (cond_op (bit_not @0) @2 @3 @4 (view_convert:op_type @1) @6 @7))))))
9463 /* Detect cases in which a VEC_COND_EXPR effectively replaces the
9464 "else" value of an IFN_COND_*. */
9465 (for cond_op (COND_BINARY)
9467 (vec_cond @0 (view_convert? (cond_op @0 @1 @2 @3)) @4)
9468 (with { tree op_type = TREE_TYPE (@3); }
9469 (if (element_precision (type) == element_precision (op_type))
9470 (view_convert (cond_op @0 @1 @2 (view_convert:op_type @4))))))
9472 (vec_cond @0 @1 (view_convert? (cond_op @2 @3 @4 @5)))
9473 (with { tree op_type = TREE_TYPE (@5); }
9474 (if (inverse_conditions_p (@0, @2)
9475 && element_precision (type) == element_precision (op_type))
9476 (view_convert (cond_op @2 @3 @4 (view_convert:op_type @1)))))))
9478 /* Same for ternary operations. */
9479 (for cond_op (COND_TERNARY)
9481 (vec_cond @0 (view_convert? (cond_op @0 @1 @2 @3 @4)) @5)
9482 (with { tree op_type = TREE_TYPE (@4); }
9483 (if (element_precision (type) == element_precision (op_type))
9484 (view_convert (cond_op @0 @1 @2 @3 (view_convert:op_type @5))))))
9486 (vec_cond @0 @1 (view_convert? (cond_op @2 @3 @4 @5 @6)))
9487 (with { tree op_type = TREE_TYPE (@6); }
9488 (if (inverse_conditions_p (@0, @2)
9489 && element_precision (type) == element_precision (op_type))
9490 (view_convert (cond_op @2 @3 @4 @5 (view_convert:op_type @1)))))))
9492 /* Detect cases in which a VEC_COND_EXPR effectively replaces the
9493 "else" value of an IFN_COND_LEN_*. */
9494 (for cond_len_op (COND_LEN_BINARY)
9496 (vec_cond @0 (view_convert? (cond_len_op @0 @1 @2 @3 @4 @5)) @6)
9497 (with { tree op_type = TREE_TYPE (@3); }
9498 (if (element_precision (type) == element_precision (op_type))
9499 (view_convert (cond_len_op @0 @1 @2 (view_convert:op_type @6) @4 @5)))))
9501 (vec_cond @0 @1 (view_convert? (cond_len_op @2 @3 @4 @5 @6 @7)))
9502 (with { tree op_type = TREE_TYPE (@5); }
9503 (if (inverse_conditions_p (@0, @2)
9504 && element_precision (type) == element_precision (op_type))
9505 (view_convert (cond_len_op @2 @3 @4 (view_convert:op_type @1) @6 @7))))))
9507 /* Same for ternary operations. */
9508 (for cond_len_op (COND_LEN_TERNARY)
9510 (vec_cond @0 (view_convert? (cond_len_op @0 @1 @2 @3 @4 @5 @6)) @7)
9511 (with { tree op_type = TREE_TYPE (@4); }
9512 (if (element_precision (type) == element_precision (op_type))
9513 (view_convert (cond_len_op @0 @1 @2 @3 (view_convert:op_type @7) @5 @6)))))
9515 (vec_cond @0 @1 (view_convert? (cond_len_op @2 @3 @4 @5 @6 @7 @8)))
9516 (with { tree op_type = TREE_TYPE (@6); }
9517 (if (inverse_conditions_p (@0, @2)
9518 && element_precision (type) == element_precision (op_type))
9519 (view_convert (cond_len_op @2 @3 @4 @5 (view_convert:op_type @1) @7 @8))))))
9521 /* Detect simplication for a conditional reduction where
9524 c = mask2 ? d + a : d
9528 c = mask1 && mask2 ? d + b : d. */
9530 (IFN_COND_ADD @0 @1 (vec_cond @2 @3 zerop@4) @1)
9531 (if (ANY_INTEGRAL_TYPE_P (type)
9532 || (FLOAT_TYPE_P (type)
9533 && fold_real_zero_addition_p (type, NULL_TREE, @4, 0)))
9534 (IFN_COND_ADD (bit_and @0 @2) @1 @3 @1)))
9536 /* Detect simplication for a conditional length reduction where
9539 c = i < len + bias ? d + a : d
9543 c = mask && i < len + bias ? d + b : d. */
9545 (IFN_COND_LEN_ADD integer_truep @0 (vec_cond @1 @2 zerop@5) @0 @3 @4)
9546 (if (ANY_INTEGRAL_TYPE_P (type)
9547 || (FLOAT_TYPE_P (type)
9548 && fold_real_zero_addition_p (type, NULL_TREE, @5, 0)))
9549 (IFN_COND_LEN_ADD @1 @0 @2 @0 @3 @4)))
9551 /* Detect simplification for vector condition folding where
9553 c = mask1 ? (masked_op mask2 a b) : b
9557 c = masked_op (mask1 & mask2) a b
9559 where the operation can be partially applied to one operand. */
9561 (for cond_op (COND_BINARY)
9564 (cond_op:s @1 @2 @3 @4) @3)
9565 (cond_op (bit_and @1 @0) @2 @3 @4)))
9567 /* And same for ternary expressions. */
9569 (for cond_op (COND_TERNARY)
9572 (cond_op:s @1 @2 @3 @4 @5) @4)
9573 (cond_op (bit_and @1 @0) @2 @3 @4 @5)))
9575 /* For pointers @0 and @2 and nonnegative constant offset @1, look for
9578 A: (@0 + @1 < @2) | (@2 + @1 < @0)
9579 B: (@0 + @1 <= @2) | (@2 + @1 <= @0)
9581 If pointers are known not to wrap, B checks whether @1 bytes starting
9582 at @0 and @2 do not overlap, while A tests the same thing for @1 + 1
9583 bytes. A is more efficiently tested as:
9585 A: (sizetype) (@0 + @1 - @2) > @1 * 2
9587 The equivalent expression for B is given by replacing @1 with @1 - 1:
9589 B: (sizetype) (@0 + (@1 - 1) - @2) > (@1 - 1) * 2
9591 @0 and @2 can be swapped in both expressions without changing the result.
9593 The folds rely on sizetype's being unsigned (which is always true)
9594 and on its being the same width as the pointer (which we have to check).
9596 The fold replaces two pointer_plus expressions, two comparisons and
9597 an IOR with a pointer_plus, a pointer_diff, and a comparison, so in
9598 the best case it's a saving of two operations. The A fold retains one
9599 of the original pointer_pluses, so is a win even if both pointer_pluses
9600 are used elsewhere. The B fold is a wash if both pointer_pluses are
9601 used elsewhere, since all we end up doing is replacing a comparison with
9602 a pointer_plus. We do still apply the fold under those circumstances
9603 though, in case applying it to other conditions eventually makes one of the
9604 pointer_pluses dead. */
9605 (for ior (truth_orif truth_or bit_ior)
9608 (ior (cmp:cs (pointer_plus@3 @0 INTEGER_CST@1) @2)
9609 (cmp:cs (pointer_plus@4 @2 @1) @0))
9610 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
9611 && TYPE_OVERFLOW_WRAPS (sizetype)
9612 && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (sizetype))
9613 /* Calculate the rhs constant. */
9614 (with { offset_int off = wi::to_offset (@1) - (cmp == LE_EXPR ? 1 : 0);
9615 offset_int rhs = off * 2; }
9616 /* Always fails for negative values. */
9617 (if (wi::min_precision (rhs, UNSIGNED) <= TYPE_PRECISION (sizetype))
9618 /* Since the order of @0 and @2 doesn't matter, let tree_swap_operands_p
9619 pick a canonical order. This increases the chances of using the
9620 same pointer_plus in multiple checks. */
9621 (with { bool swap_p = tree_swap_operands_p (@0, @2);
9622 tree rhs_tree = wide_int_to_tree (sizetype, rhs); }
9623 (if (cmp == LT_EXPR)
9624 (gt (convert:sizetype
9625 (pointer_diff:ssizetype { swap_p ? @4 : @3; }
9626 { swap_p ? @0 : @2; }))
9628 (gt (convert:sizetype
9629 (pointer_diff:ssizetype
9630 (pointer_plus { swap_p ? @2 : @0; }
9631 { wide_int_to_tree (sizetype, off); })
9632 { swap_p ? @0 : @2; }))
9633 { rhs_tree; })))))))))
9635 /* Fold REDUC (@0 & @1) -> @0[I] & @1[I] if element I is the only nonzero
9637 (for reduc (IFN_REDUC_PLUS IFN_REDUC_IOR IFN_REDUC_XOR)
9638 (simplify (reduc (view_convert? (bit_and @0 VECTOR_CST@1)))
9639 (with { int i = single_nonzero_element (@1); }
9641 (with { tree elt = vector_cst_elt (@1, i);
9642 tree elt_type = TREE_TYPE (elt);
9643 unsigned int elt_bits = tree_to_uhwi (TYPE_SIZE (elt_type));
9644 tree size = bitsize_int (elt_bits);
9645 tree pos = bitsize_int (elt_bits * i); }
9648 (BIT_FIELD_REF:elt_type @0 { size; } { pos; })
9651 /* Fold reduction of a single nonzero element constructor. */
9652 (for reduc (IFN_REDUC_PLUS IFN_REDUC_IOR IFN_REDUC_XOR)
9653 (simplify (reduc (CONSTRUCTOR@0))
9654 (with { tree ctor = (TREE_CODE (@0) == SSA_NAME
9655 ? gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0)) : @0);
9656 tree elt = ctor_single_nonzero_element (ctor); }
9658 && !HONOR_SNANS (type)
9659 && !HONOR_SIGNED_ZEROS (type))
9662 /* Fold REDUC (@0 op VECTOR_CST) as REDUC (@0) op REDUC (VECTOR_CST). */
9663 (for reduc (IFN_REDUC_PLUS IFN_REDUC_MAX IFN_REDUC_MIN IFN_REDUC_FMAX
9664 IFN_REDUC_FMIN IFN_REDUC_AND IFN_REDUC_IOR IFN_REDUC_XOR)
9665 op (plus max min IFN_FMAX IFN_FMIN bit_and bit_ior bit_xor)
9666 (simplify (reduc (op @0 VECTOR_CST@1))
9667 (op (reduc:type @0) (reduc:type @1))))
9669 /* Simplify vector floating point operations of alternating sub/add pairs
9670 into using an fneg of a wider element type followed by a normal add.
9671 under IEEE 754 the fneg of the wider type will negate every even entry
9672 and when doing an add we get a sub of the even and add of every odd
9674 (for plusminus (plus minus)
9675 minusplus (minus plus)
9677 (vec_perm (plusminus @0 @1) (minusplus @2 @3) VECTOR_CST@4)
9678 (if (!VECTOR_INTEGER_TYPE_P (type)
9679 && !FLOAT_WORDS_BIG_ENDIAN
9680 /* plus is commutative, while minus is not, so :c can't be used.
9681 Do equality comparisons by hand and at the end pick the operands
9683 && (operand_equal_p (@0, @2, 0)
9684 ? operand_equal_p (@1, @3, 0)
9685 : operand_equal_p (@0, @3, 0) && operand_equal_p (@1, @2, 0)))
9688 /* Build a vector of integers from the tree mask. */
9689 vec_perm_builder builder;
9691 (if (tree_to_vec_perm_builder (&builder, @4))
9694 /* Create a vec_perm_indices for the integer vector. */
9695 poly_uint64 nelts = TYPE_VECTOR_SUBPARTS (type);
9696 vec_perm_indices sel (builder, 2, nelts);
9697 machine_mode vec_mode = TYPE_MODE (type);
9698 machine_mode wide_mode;
9699 scalar_mode wide_elt_mode;
9700 poly_uint64 wide_nunits;
9701 scalar_mode inner_mode = GET_MODE_INNER (vec_mode);
9703 (if (VECTOR_MODE_P (vec_mode)
9704 && sel.series_p (0, 2, 0, 2)
9705 && sel.series_p (1, 2, nelts + 1, 2)
9706 && GET_MODE_2XWIDER_MODE (inner_mode).exists (&wide_elt_mode)
9707 && multiple_p (GET_MODE_NUNITS (vec_mode), 2, &wide_nunits)
9708 && related_vector_mode (vec_mode, wide_elt_mode,
9709 wide_nunits).exists (&wide_mode))
9713 = lang_hooks.types.type_for_mode (GET_MODE_INNER (wide_mode),
9714 TYPE_UNSIGNED (type));
9715 tree ntype = build_vector_type_for_mode (stype, wide_mode);
9717 /* The format has to be a non-extended ieee format. */
9718 const struct real_format *fmt_old = FLOAT_MODE_FORMAT (vec_mode);
9719 const struct real_format *fmt_new = FLOAT_MODE_FORMAT (wide_mode);
9721 (if (TYPE_MODE (stype) != BLKmode
9722 && VECTOR_TYPE_P (ntype)
9727 /* If the target doesn't support v1xx vectors, try using
9728 scalar mode xx instead. */
9729 if (known_eq (GET_MODE_NUNITS (wide_mode), 1)
9730 && !target_supports_op_p (ntype, NEGATE_EXPR, optab_vector))
9733 (if (fmt_new->signbit_rw
9734 == fmt_old->signbit_rw + GET_MODE_UNIT_BITSIZE (vec_mode)
9735 && fmt_new->signbit_rw == fmt_new->signbit_ro
9736 && targetm.can_change_mode_class (TYPE_MODE (ntype),
9737 TYPE_MODE (type), ALL_REGS)
9738 && ((optimize_vectors_before_lowering_p ()
9739 && VECTOR_TYPE_P (ntype))
9740 || target_supports_op_p (ntype, NEGATE_EXPR, optab_vector)))
9741 (if (plusminus == PLUS_EXPR)
9742 (plus (view_convert:type (negate (view_convert:ntype @3))) @2)
9743 (minus @0 (view_convert:type
9744 (negate (view_convert:ntype @1))))))))))))))))
9747 (vec_perm @0 @1 VECTOR_CST@2)
9750 tree op0 = @0, op1 = @1, op2 = @2;
9751 machine_mode result_mode = TYPE_MODE (type);
9752 machine_mode op_mode = TYPE_MODE (TREE_TYPE (op0));
9754 /* Build a vector of integers from the tree mask. */
9755 vec_perm_builder builder;
9757 (if (tree_to_vec_perm_builder (&builder, op2))
9760 /* Create a vec_perm_indices for the integer vector. */
9761 poly_uint64 nelts = TYPE_VECTOR_SUBPARTS (type);
9762 bool single_arg = (op0 == op1);
9763 vec_perm_indices sel (builder, single_arg ? 1 : 2, nelts);
9765 (if (sel.series_p (0, 1, 0, 1))
9767 (if (sel.series_p (0, 1, nelts, 1))
9773 if (sel.all_from_input_p (0))
9775 else if (sel.all_from_input_p (1))
9778 sel.rotate_inputs (1);
9780 else if (known_ge (poly_uint64 (sel[0]), nelts))
9782 std::swap (op0, op1);
9783 sel.rotate_inputs (1);
9787 tree cop0 = op0, cop1 = op1;
9788 if (TREE_CODE (op0) == SSA_NAME
9789 && (def = dyn_cast <gassign *> (SSA_NAME_DEF_STMT (op0)))
9790 && gimple_assign_rhs_code (def) == CONSTRUCTOR)
9791 cop0 = gimple_assign_rhs1 (def);
9792 if (TREE_CODE (op1) == SSA_NAME
9793 && (def = dyn_cast <gassign *> (SSA_NAME_DEF_STMT (op1)))
9794 && gimple_assign_rhs_code (def) == CONSTRUCTOR)
9795 cop1 = gimple_assign_rhs1 (def);
9798 (if ((TREE_CODE (cop0) == VECTOR_CST
9799 || TREE_CODE (cop0) == CONSTRUCTOR)
9800 && (TREE_CODE (cop1) == VECTOR_CST
9801 || TREE_CODE (cop1) == CONSTRUCTOR)
9802 && (t = fold_vec_perm (type, cop0, cop1, sel)))
9806 bool changed = (op0 == op1 && !single_arg);
9807 tree ins = NULL_TREE;
9810 /* See if the permutation is performing a single element
9811 insert from a CONSTRUCTOR or constant and use a BIT_INSERT_EXPR
9812 in that case. But only if the vector mode is supported,
9813 otherwise this is invalid GIMPLE. */
9814 if (op_mode != BLKmode
9815 && (TREE_CODE (cop0) == VECTOR_CST
9816 || TREE_CODE (cop0) == CONSTRUCTOR
9817 || TREE_CODE (cop1) == VECTOR_CST
9818 || TREE_CODE (cop1) == CONSTRUCTOR))
9820 bool insert_first_p = sel.series_p (1, 1, nelts + 1, 1);
9823 /* After canonicalizing the first elt to come from the
9824 first vector we only can insert the first elt from
9825 the first vector. */
9827 if ((ins = fold_read_from_vector (cop0, sel[0])))
9830 /* The above can fail for two-element vectors which always
9831 appear to insert the first element, so try inserting
9832 into the second lane as well. For more than two
9833 elements that's wasted time. */
9834 if (!insert_first_p || (!ins && maybe_eq (nelts, 2u)))
9836 unsigned int encoded_nelts = sel.encoding ().encoded_nelts ();
9837 for (at = 0; at < encoded_nelts; ++at)
9838 if (maybe_ne (sel[at], at))
9840 if (at < encoded_nelts
9841 && (known_eq (at + 1, nelts)
9842 || sel.series_p (at + 1, 1, at + 1, 1)))
9844 if (known_lt (poly_uint64 (sel[at]), nelts))
9845 ins = fold_read_from_vector (cop0, sel[at]);
9847 ins = fold_read_from_vector (cop1, sel[at] - nelts);
9852 /* Generate a canonical form of the selector. */
9853 if (!ins && sel.encoding () != builder)
9855 /* Some targets are deficient and fail to expand a single
9856 argument permutation while still allowing an equivalent
9857 2-argument version. */
9859 if (sel.ninputs () == 2
9860 || can_vec_perm_const_p (result_mode, op_mode, sel, false))
9861 op2 = vec_perm_indices_to_tree (TREE_TYPE (op2), sel);
9864 vec_perm_indices sel2 (builder, 2, nelts);
9865 if (can_vec_perm_const_p (result_mode, op_mode, sel2, false))
9866 op2 = vec_perm_indices_to_tree (TREE_TYPE (op2), sel2);
9868 /* Not directly supported with either encoding,
9869 so use the preferred form. */
9870 op2 = vec_perm_indices_to_tree (TREE_TYPE (op2), sel);
9872 if (!operand_equal_p (op2, oldop2, 0))
9877 (bit_insert { op0; } { ins; }
9878 { bitsize_int (at * vector_element_bits (type)); })
9880 (vec_perm { op0; } { op1; } { op2; }))))))))))))
9882 /* VEC_PERM_EXPR (v, v, mask) -> v where v contains same element. */
9884 (match vec_same_elem_p
9887 (match vec_same_elem_p
9889 (if (TREE_CODE (@0) == SSA_NAME
9890 && uniform_vector_p (gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0))))))
9892 (match vec_same_elem_p
9894 (if (uniform_vector_p (@0))))
9898 (vec_perm vec_same_elem_p@0 @0 @1)
9899 (if (types_match (type, TREE_TYPE (@0)))
9903 tree elem = uniform_vector_p (@0);
9906 { build_vector_from_val (type, elem); }))))
9908 /* Push VEC_PERM earlier if that may help FMA perception (PR101895). */
9910 (plus:c (vec_perm:s (mult:c@0 @1 vec_same_elem_p@2) @0 @3) @4)
9911 (if (TREE_CODE (@0) == SSA_NAME && num_imm_uses (@0) == 2)
9912 (plus (mult (vec_perm @1 @1 @3) @2) @4)))
9914 (minus (vec_perm:s (mult:c@0 @1 vec_same_elem_p@2) @0 @3) @4)
9915 (if (TREE_CODE (@0) == SSA_NAME && num_imm_uses (@0) == 2)
9916 (minus (mult (vec_perm @1 @1 @3) @2) @4)))
9920 c = VEC_PERM_EXPR <a, b, VCST0>;
9921 d = VEC_PERM_EXPR <c, c, VCST1>;
9923 d = VEC_PERM_EXPR <a, b, NEW_VCST>; */
9926 (vec_perm (vec_perm@0 @1 @2 VECTOR_CST@3) @0 VECTOR_CST@4)
9927 (if (TYPE_VECTOR_SUBPARTS (type).is_constant ())
9930 machine_mode result_mode = TYPE_MODE (type);
9931 machine_mode op_mode = TYPE_MODE (TREE_TYPE (@1));
9932 int nelts = TYPE_VECTOR_SUBPARTS (type).to_constant ();
9933 vec_perm_builder builder0;
9934 vec_perm_builder builder1;
9935 vec_perm_builder builder2 (nelts, nelts, 1);
9937 (if (tree_to_vec_perm_builder (&builder0, @3)
9938 && tree_to_vec_perm_builder (&builder1, @4))
9941 vec_perm_indices sel0 (builder0, 2, nelts);
9942 vec_perm_indices sel1 (builder1, 1, nelts);
9944 for (int i = 0; i < nelts; i++)
9945 builder2.quick_push (sel0[sel1[i].to_constant ()]);
9947 vec_perm_indices sel2 (builder2, 2, nelts);
9949 tree op0 = NULL_TREE;
9950 /* If the new VEC_PERM_EXPR can't be handled but both
9951 original VEC_PERM_EXPRs can, punt.
9952 If one or both of the original VEC_PERM_EXPRs can't be
9953 handled and the new one can't be either, don't increase
9954 number of VEC_PERM_EXPRs that can't be handled. */
9955 if (can_vec_perm_const_p (result_mode, op_mode, sel2, false)
9957 ? (!can_vec_perm_const_p (result_mode, op_mode, sel0, false)
9958 || !can_vec_perm_const_p (result_mode, op_mode, sel1, false))
9959 : !can_vec_perm_const_p (result_mode, op_mode, sel1, false)))
9960 op0 = vec_perm_indices_to_tree (TREE_TYPE (@4), sel2);
9963 (vec_perm @1 @2 { op0; })))))))
9966 c = VEC_PERM_EXPR <a, b, VCST0>;
9967 d = VEC_PERM_EXPR <x, c, VCST1>;
9969 d = VEC_PERM_EXPR <x, {a,b}, NEW_VCST>;
9970 when all elements from a or b are replaced by the later
9974 (vec_perm @5 (vec_perm@0 @1 @2 VECTOR_CST@3) VECTOR_CST@4)
9975 (if (TYPE_VECTOR_SUBPARTS (type).is_constant ())
9978 machine_mode result_mode = TYPE_MODE (type);
9979 machine_mode op_mode = TYPE_MODE (TREE_TYPE (@1));
9980 int nelts = TYPE_VECTOR_SUBPARTS (type).to_constant ();
9981 vec_perm_builder builder0;
9982 vec_perm_builder builder1;
9983 vec_perm_builder builder2 (nelts, nelts, 2);
9985 (if (tree_to_vec_perm_builder (&builder0, @3)
9986 && tree_to_vec_perm_builder (&builder1, @4))
9989 vec_perm_indices sel0 (builder0, 2, nelts);
9990 vec_perm_indices sel1 (builder1, 2, nelts);
9991 bool use_1 = false, use_2 = false;
9993 for (int i = 0; i < nelts; i++)
9995 if (known_lt ((poly_uint64)sel1[i], sel1.nelts_per_input ()))
9996 builder2.quick_push (sel1[i]);
9999 poly_uint64 j = sel0[(sel1[i] - sel1.nelts_per_input ())
10001 if (known_lt (j, sel0.nelts_per_input ()))
10006 j -= sel0.nelts_per_input ();
10008 builder2.quick_push (j + sel1.nelts_per_input ());
10012 (if (use_1 ^ use_2)
10015 vec_perm_indices sel2 (builder2, 2, nelts);
10016 tree op0 = NULL_TREE;
10017 /* If the new VEC_PERM_EXPR can't be handled but both
10018 original VEC_PERM_EXPRs can, punt.
10019 If one or both of the original VEC_PERM_EXPRs can't be
10020 handled and the new one can't be either, don't increase
10021 number of VEC_PERM_EXPRs that can't be handled. */
10022 if (can_vec_perm_const_p (result_mode, op_mode, sel2, false)
10023 || (single_use (@0)
10024 ? (!can_vec_perm_const_p (result_mode, op_mode, sel0, false)
10025 || !can_vec_perm_const_p (result_mode, op_mode, sel1, false))
10026 : !can_vec_perm_const_p (result_mode, op_mode, sel1, false)))
10027 op0 = vec_perm_indices_to_tree (TREE_TYPE (@4), sel2);
10032 (vec_perm @5 @1 { op0; }))
10034 (vec_perm @5 @2 { op0; })))))))))))
10036 /* And the case with swapped outer permute sources. */
10039 (vec_perm (vec_perm@0 @1 @2 VECTOR_CST@3) @5 VECTOR_CST@4)
10040 (if (TYPE_VECTOR_SUBPARTS (type).is_constant ())
10043 machine_mode result_mode = TYPE_MODE (type);
10044 machine_mode op_mode = TYPE_MODE (TREE_TYPE (@1));
10045 int nelts = TYPE_VECTOR_SUBPARTS (type).to_constant ();
10046 vec_perm_builder builder0;
10047 vec_perm_builder builder1;
10048 vec_perm_builder builder2 (nelts, nelts, 2);
10050 (if (tree_to_vec_perm_builder (&builder0, @3)
10051 && tree_to_vec_perm_builder (&builder1, @4))
10054 vec_perm_indices sel0 (builder0, 2, nelts);
10055 vec_perm_indices sel1 (builder1, 2, nelts);
10056 bool use_1 = false, use_2 = false;
10058 for (int i = 0; i < nelts; i++)
10060 if (known_ge ((poly_uint64)sel1[i], sel1.nelts_per_input ()))
10061 builder2.quick_push (sel1[i]);
10064 poly_uint64 j = sel0[sel1[i].to_constant ()];
10065 if (known_lt (j, sel0.nelts_per_input ()))
10070 j -= sel0.nelts_per_input ();
10072 builder2.quick_push (j);
10076 (if (use_1 ^ use_2)
10079 vec_perm_indices sel2 (builder2, 2, nelts);
10080 tree op0 = NULL_TREE;
10081 /* If the new VEC_PERM_EXPR can't be handled but both
10082 original VEC_PERM_EXPRs can, punt.
10083 If one or both of the original VEC_PERM_EXPRs can't be
10084 handled and the new one can't be either, don't increase
10085 number of VEC_PERM_EXPRs that can't be handled. */
10086 if (can_vec_perm_const_p (result_mode, op_mode, sel2, false)
10087 || (single_use (@0)
10088 ? (!can_vec_perm_const_p (result_mode, op_mode, sel0, false)
10089 || !can_vec_perm_const_p (result_mode, op_mode, sel1, false))
10090 : !can_vec_perm_const_p (result_mode, op_mode, sel1, false)))
10091 op0 = vec_perm_indices_to_tree (TREE_TYPE (@4), sel2);
10096 (vec_perm @1 @5 { op0; }))
10098 (vec_perm @2 @5 { op0; })))))))))))
10101 /* Match count trailing zeroes for simplify_count_trailing_zeroes in fwprop.
10102 The canonical form is array[((x & -x) * C) >> SHIFT] where C is a magic
10103 constant which when multiplied by a power of 2 contains a unique value
10104 in the top 5 or 6 bits. This is then indexed into a table which maps it
10105 to the number of trailing zeroes. */
10106 (match (ctz_table_index @1 @2 @3)
10107 (rshift (mult (bit_and:c (negate @1) @1) INTEGER_CST@2) INTEGER_CST@3))
10109 (match (cond_expr_convert_p @0 @2 @3 @6)
10110 (cond (simple_comparison@6 @0 @1) (convert@4 @2) (convert@5 @3))
10111 (if (INTEGRAL_TYPE_P (type)
10112 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
10113 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
10114 && INTEGRAL_TYPE_P (TREE_TYPE (@3))
10115 && TYPE_PRECISION (type) != TYPE_PRECISION (TREE_TYPE (@0))
10116 && TYPE_PRECISION (TREE_TYPE (@0))
10117 == TYPE_PRECISION (TREE_TYPE (@2))
10118 && TYPE_PRECISION (TREE_TYPE (@0))
10119 == TYPE_PRECISION (TREE_TYPE (@3))
10120 /* For vect_recog_cond_expr_convert_pattern, @2 and @3 can differ in
10121 signess when convert is truncation, but not ok for extension since
10122 it's sign_extend vs zero_extend. */
10123 && (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type)
10124 || (TYPE_UNSIGNED (TREE_TYPE (@2))
10125 == TYPE_UNSIGNED (TREE_TYPE (@3))))
10127 && single_use (@5))))
10129 (for bit_op (bit_and bit_ior bit_xor)
10130 (match (bitwise_induction_p @0 @2 @3)
10132 (nop_convert1? (bit_not2?@0 (convert3? (lshift integer_onep@1 @2))))
10135 (match (bitwise_induction_p @0 @2 @3)
10137 (nop_convert1? (bit_xor@0 (convert2? (lshift integer_onep@1 @2)) @3))))
10139 /* n - (((n > C1) ? n : C1) & -C2) -> n & C1 for unsigned case.
10140 n - (((n > C1) ? n : C1) & -C2) -> (n <= C1) ? n : (n & C1) for signed case. */
10142 (minus @0 (bit_and (max @0 INTEGER_CST@1) INTEGER_CST@2))
10143 (with { auto i = wi::neg (wi::to_wide (@2)); }
10144 /* Check if -C2 is a power of 2 and C1 = -C2 - 1. */
10145 (if (wi::popcount (i) == 1
10146 && (wi::to_wide (@1)) == (i - 1))
10147 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
10149 (cond (le @0 @1) @0 (bit_and @0 @1))))))
10151 /* -x & 1 -> x & 1. */
10153 (bit_and (negate @0) integer_onep@1)
10154 (if (!TYPE_OVERFLOW_SANITIZED (type))
10157 /* `-a` is just `a` if the type is 1bit wide or when converting
10158 to a 1bit type; similar to the above transformation of `(-x)&1`.
10159 This is used mostly with the transformation of
10160 `a ? ~b : b` into `(-a)^b`.
10161 It also can show up with bitfields. */
10163 (convert? (negate @0))
10164 (if (INTEGRAL_TYPE_P (type)
10165 && TYPE_PRECISION (type) == 1
10166 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0)))
10170 c1 = VEC_PERM_EXPR (a, a, mask)
10171 c2 = VEC_PERM_EXPR (b, b, mask)
10175 c3 = VEC_PERM_EXPR (c, c, mask)
10176 For all integer non-div operations. */
10177 (for op (plus minus mult bit_and bit_ior bit_xor
10180 (op (vec_perm @0 @0 @2) (vec_perm @1 @1 @2))
10181 (if (VECTOR_INTEGER_TYPE_P (type))
10182 (vec_perm (op@3 @0 @1) @3 @2))))
10184 /* Similar for float arithmetic when permutation constant covers
10185 all vector elements. */
10186 (for op (plus minus mult)
10188 (op (vec_perm @0 @0 VECTOR_CST@2) (vec_perm @1 @1 VECTOR_CST@2))
10189 (if (VECTOR_FLOAT_TYPE_P (type)
10190 && TYPE_VECTOR_SUBPARTS (type).is_constant ())
10193 tree perm_cst = @2;
10194 vec_perm_builder builder;
10195 bool full_perm_p = false;
10196 if (tree_to_vec_perm_builder (&builder, perm_cst))
10198 unsigned HOST_WIDE_INT nelts;
10200 nelts = TYPE_VECTOR_SUBPARTS (type).to_constant ();
10201 /* Create a vec_perm_indices for the VECTOR_CST. */
10202 vec_perm_indices sel (builder, 1, nelts);
10204 /* Check if perm indices covers all vector elements. */
10205 if (sel.encoding ().encoded_full_vector_p ())
10207 auto_sbitmap seen (nelts);
10208 bitmap_clear (seen);
10210 unsigned HOST_WIDE_INT count = 0, i;
10212 for (i = 0; i < nelts; i++)
10214 if (!bitmap_set_bit (seen, sel[i].to_constant ()))
10218 full_perm_p = count == nelts;
10223 (vec_perm (op@3 @0 @1) @3 @2))))))