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 /* Simplify (t * 2) / 2) -> t. */
934 (for div (trunc_div ceil_div floor_div round_div exact_div)
936 (div (mult:c @0 @1) @1)
937 (if (ANY_INTEGRAL_TYPE_P (type))
938 (if (TYPE_OVERFLOW_UNDEFINED (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))
951 (for div (trunc_div exact_div)
952 /* Simplify (X + M*N) / N -> X / N + M. */
954 (div (plus:c@4 @0 (mult:c@3 @1 @2)) @2)
955 (with {value_range vr0, vr1, vr2, vr3, vr4;}
956 (if (INTEGRAL_TYPE_P (type)
957 && get_range_query (cfun)->range_of_expr (vr1, @1)
958 && get_range_query (cfun)->range_of_expr (vr2, @2)
959 /* "N*M" doesn't overflow. */
960 && range_op_handler (MULT_EXPR).overflow_free_p (vr1, vr2)
961 && get_range_query (cfun)->range_of_expr (vr0, @0)
962 && get_range_query (cfun)->range_of_expr (vr3, @3)
963 /* "X+(N*M)" doesn't overflow. */
964 && range_op_handler (PLUS_EXPR).overflow_free_p (vr0, vr3)
965 && get_range_query (cfun)->range_of_expr (vr4, @4)
966 && !vr4.undefined_p ()
967 /* "X+N*M" is not with opposite sign as "X". */
968 && (TYPE_UNSIGNED (type)
969 || (vr0.nonnegative_p () && vr4.nonnegative_p ())
970 || (vr0.nonpositive_p () && vr4.nonpositive_p ())))
971 (plus (div @0 @2) @1))))
973 /* Simplify (X - M*N) / N -> X / N - M. */
975 (div (minus@4 @0 (mult:c@3 @1 @2)) @2)
976 (with {value_range vr0, vr1, vr2, vr3, vr4;}
977 (if (INTEGRAL_TYPE_P (type)
978 && get_range_query (cfun)->range_of_expr (vr1, @1)
979 && get_range_query (cfun)->range_of_expr (vr2, @2)
980 /* "N * M" doesn't overflow. */
981 && range_op_handler (MULT_EXPR).overflow_free_p (vr1, vr2)
982 && get_range_query (cfun)->range_of_expr (vr0, @0)
983 && get_range_query (cfun)->range_of_expr (vr3, @3)
984 /* "X - (N*M)" doesn't overflow. */
985 && range_op_handler (MINUS_EXPR).overflow_free_p (vr0, vr3)
986 && get_range_query (cfun)->range_of_expr (vr4, @4)
987 && !vr4.undefined_p ()
988 /* "X-N*M" is not with opposite sign as "X". */
989 && (TYPE_UNSIGNED (type)
990 || (vr0.nonnegative_p () && vr4.nonnegative_p ())
991 || (vr0.nonpositive_p () && vr4.nonpositive_p ())))
992 (minus (div @0 @2) @1)))))
995 (X + C) / N -> X / N + C / N where C is multiple of N.
996 (X + C) >> N -> X >> N + C>>N if low N bits of C is 0. */
997 (for op (trunc_div exact_div rshift)
999 (op (plus@3 @0 INTEGER_CST@1) INTEGER_CST@2)
1002 wide_int c = wi::to_wide (@1);
1003 wide_int n = wi::to_wide (@2);
1004 bool shift = op == RSHIFT_EXPR;
1005 #define plus_op1(v) (shift ? wi::rshift (v, n, TYPE_SIGN (type)) \
1006 : wi::div_trunc (v, n, TYPE_SIGN (type)))
1007 #define exact_mod(v) (shift ? wi::ctz (v) >= n.to_shwi () \
1008 : wi::multiple_of_p (v, n, TYPE_SIGN (type)))
1009 value_range vr0, vr1, vr3;
1011 (if (INTEGRAL_TYPE_P (type)
1012 && get_range_query (cfun)->range_of_expr (vr0, @0))
1014 && get_range_query (cfun)->range_of_expr (vr1, @1)
1015 /* "X+C" doesn't overflow. */
1016 && range_op_handler (PLUS_EXPR).overflow_free_p (vr0, vr1)
1017 && get_range_query (cfun)->range_of_expr (vr3, @3)
1018 && !vr3.undefined_p ()
1019 /* "X+C" and "X" are not of opposite sign. */
1020 && (TYPE_UNSIGNED (type)
1021 || (vr0.nonnegative_p () && vr3.nonnegative_p ())
1022 || (vr0.nonpositive_p () && vr3.nonpositive_p ())))
1023 (plus (op @0 @2) { wide_int_to_tree (type, plus_op1 (c)); })
1024 (if (!vr0.undefined_p () && TYPE_UNSIGNED (type) && c.sign_mask () < 0
1026 /* unsigned "X-(-C)" doesn't underflow. */
1027 && wi::geu_p (vr0.lower_bound (), -c))
1028 (plus (op @0 @2) { wide_int_to_tree (type, -plus_op1 (-c)); })))))))
1033 /* (nop_outer_cast)-(inner_cast)var -> -(outer_cast)(var)
1034 if var is smaller in precision.
1035 This is always safe for both doing the negative in signed or unsigned
1036 as the value for undefined will not show up. */
1038 (convert (negate:s@1 (convert:s @0)))
1039 (if (INTEGRAL_TYPE_P (type)
1040 && tree_nop_conversion_p (type, TREE_TYPE (@1))
1041 && TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (@0)))
1042 (negate (convert @0))))
1044 (for op (negate abs)
1045 /* Simplify cos(-x) and cos(|x|) -> cos(x). Similarly for cosh. */
1046 (for coss (COS COSH)
1050 /* Simplify pow(-x, y) and pow(|x|,y) -> pow(x,y) if y is an even integer. */
1053 (pows (op @0) REAL_CST@1)
1054 (with { HOST_WIDE_INT n; }
1055 (if (real_isinteger (&TREE_REAL_CST (@1), &n) && (n & 1) == 0)
1057 /* Likewise for powi. */
1060 (pows (op @0) INTEGER_CST@1)
1061 (if ((wi::to_wide (@1) & 1) == 0)
1063 /* Strip negate and abs from both operands of hypot. */
1071 /* copysign(-x, y) and copysign(abs(x), y) -> copysign(x, y). */
1072 (for copysigns (COPYSIGN_ALL)
1074 (copysigns (op @0) @1)
1075 (copysigns @0 @1))))
1077 /* abs(x)*abs(x) -> x*x. Should be valid for all types. */
1079 (mult (abs@1 @0) @1)
1082 /* Convert absu(x)*absu(x) -> x*x. */
1084 (mult (absu@1 @0) @1)
1085 (mult (convert@2 @0) @2))
1087 /* cos(copysign(x, y)) -> cos(x). Similarly for cosh. */
1088 (for coss (COS COSH)
1089 (for copysigns (COPYSIGN)
1091 (coss (copysigns @0 @1))
1094 /* pow(copysign(x, y), z) -> pow(x, z) if z is an even integer. */
1096 (for copysigns (COPYSIGN)
1098 (pows (copysigns @0 @2) REAL_CST@1)
1099 (with { HOST_WIDE_INT n; }
1100 (if (real_isinteger (&TREE_REAL_CST (@1), &n) && (n & 1) == 0)
1102 /* Likewise for powi. */
1104 (for copysigns (COPYSIGN)
1106 (pows (copysigns @0 @2) INTEGER_CST@1)
1107 (if ((wi::to_wide (@1) & 1) == 0)
1111 (for copysigns (COPYSIGN)
1112 /* hypot(copysign(x, y), z) -> hypot(x, z). */
1114 (hypots (copysigns @0 @1) @2)
1116 /* hypot(x, copysign(y, z)) -> hypot(x, y). */
1118 (hypots @0 (copysigns @1 @2))
1121 /* copysign(x, CST) -> abs (x). */
1122 (for copysigns (COPYSIGN_ALL)
1124 (copysigns @0 REAL_CST@1)
1125 (if (!REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
1128 /* Transform fneg (fabs (X)) -> copysign (X, -1). */
1131 (IFN_COPYSIGN @0 { build_minus_one_cst (type); }))
1133 /* copysign(copysign(x, y), z) -> copysign(x, z). */
1134 (for copysigns (COPYSIGN_ALL)
1136 (copysigns (copysigns @0 @1) @2)
1139 /* copysign(x,y)*copysign(x,y) -> x*x. */
1140 (for copysigns (COPYSIGN_ALL)
1142 (mult (copysigns@2 @0 @1) @2)
1145 /* ccos(-x) -> ccos(x). Similarly for ccosh. */
1146 (for ccoss (CCOS CCOSH)
1151 /* cabs(-x) and cos(conj(x)) -> cabs(x). */
1152 (for ops (conj negate)
1158 /* Fold (a * (1 << b)) into (a << b) */
1160 (mult:c @0 (convert? (lshift integer_onep@1 @2)))
1161 (if (! FLOAT_TYPE_P (type)
1162 && tree_nop_conversion_p (type, TREE_TYPE (@1)))
1165 /* Shifts by precision or greater result in zero. */
1166 (for shift (lshift rshift)
1168 (shift @0 uniform_integer_cst_p@1)
1169 (if ((GIMPLE || !sanitize_flags_p (SANITIZE_SHIFT_EXPONENT))
1170 /* Leave arithmetic right shifts of possibly negative values alone. */
1171 && (TYPE_UNSIGNED (type)
1172 || shift == LSHIFT_EXPR
1173 || tree_expr_nonnegative_p (@0))
1174 /* Use a signed compare to leave negative shift counts alone. */
1175 && wi::ges_p (wi::to_wide (uniform_integer_cst_p (@1)),
1176 element_precision (type)))
1177 { build_zero_cst (type); })))
1179 /* Shifts by constants distribute over several binary operations,
1180 hence (X << C) + (Y << C) can be simplified to (X + Y) << C. */
1181 (for op (plus minus)
1183 (op (lshift:s @0 @1) (lshift:s @2 @1))
1184 (if (INTEGRAL_TYPE_P (type)
1185 && TYPE_OVERFLOW_WRAPS (type)
1186 && !TYPE_SATURATING (type))
1187 (lshift (op @0 @2) @1))))
1189 (for op (bit_and bit_ior bit_xor)
1191 (op (lshift:s @0 @1) (lshift:s @2 @1))
1192 (if (INTEGRAL_TYPE_P (type))
1193 (lshift (op @0 @2) @1)))
1195 (op (rshift:s @0 @1) (rshift:s @2 @1))
1196 (if (INTEGRAL_TYPE_P (type))
1197 (rshift (op @0 @2) @1))))
1199 /* Fold (1 << (C - x)) where C = precision(type) - 1
1200 into ((1 << C) >> x). */
1202 (lshift integer_onep@0 (minus@1 INTEGER_CST@2 @3))
1203 (if (INTEGRAL_TYPE_P (type)
1204 && wi::eq_p (wi::to_wide (@2), TYPE_PRECISION (type) - 1)
1206 (if (TYPE_UNSIGNED (type))
1207 (rshift (lshift @0 @2) @3)
1209 { tree utype = unsigned_type_for (type); }
1210 (convert (rshift (lshift (convert:utype @0) @2) @3))))))
1212 /* Fold ((type)(a<0)) << SIGNBITOFA into ((type)a) & signbit. */
1214 (lshift (convert (lt @0 integer_zerop@1)) INTEGER_CST@2)
1215 (if (TYPE_SIGN (TREE_TYPE (@0)) == SIGNED
1216 && wi::eq_p (wi::to_wide (@2), TYPE_PRECISION (TREE_TYPE (@0)) - 1))
1217 (with { wide_int wone = wi::one (TYPE_PRECISION (type)); }
1218 (bit_and (convert @0)
1219 { wide_int_to_tree (type,
1220 wi::lshift (wone, wi::to_wide (@2))); }))))
1222 /* Fold (-x >> C) into -(x > 0) where C = precision(type) - 1. */
1223 (for cst (INTEGER_CST VECTOR_CST)
1225 (rshift (negate:s @0) cst@1)
1226 (if (!TYPE_UNSIGNED (type)
1227 && TYPE_OVERFLOW_UNDEFINED (type))
1228 (with { tree stype = TREE_TYPE (@1);
1229 tree bt = truth_type_for (type);
1230 tree zeros = build_zero_cst (type);
1231 tree cst = NULL_TREE; }
1233 /* Handle scalar case. */
1234 (if (INTEGRAL_TYPE_P (type)
1235 /* If we apply the rule to the scalar type before vectorization
1236 we will enforce the result of the comparison being a bool
1237 which will require an extra AND on the result that will be
1238 indistinguishable from when the user did actually want 0
1239 or 1 as the result so it can't be removed. */
1240 && canonicalize_math_after_vectorization_p ()
1241 && wi::eq_p (wi::to_wide (@1), TYPE_PRECISION (type) - 1))
1242 (negate (convert (gt @0 { zeros; }))))
1243 /* Handle vector case. */
1244 (if (VECTOR_INTEGER_TYPE_P (type)
1245 /* First check whether the target has the same mode for vector
1246 comparison results as it's operands do. */
1247 && TYPE_MODE (bt) == TYPE_MODE (type)
1248 /* Then check to see if the target is able to expand the comparison
1249 with the given type later on, otherwise we may ICE. */
1250 && expand_vec_cmp_expr_p (type, bt, GT_EXPR)
1251 && (cst = uniform_integer_cst_p (@1)) != NULL
1252 && wi::eq_p (wi::to_wide (cst), element_precision (type) - 1))
1253 (view_convert (gt:bt @0 { zeros; }))))))))
1255 /* Fold (C1/X)*C2 into (C1*C2)/X. */
1257 (mult (rdiv@3 REAL_CST@0 @1) REAL_CST@2)
1258 (if (flag_associative_math
1261 { tree tem = const_binop (MULT_EXPR, type, @0, @2); }
1263 (rdiv { tem; } @1)))))
1265 /* Simplify ~X & X as zero. */
1267 (bit_and (convert? @0) (convert? @1))
1268 (with { bool wascmp; }
1269 (if (types_match (TREE_TYPE (@0), TREE_TYPE (@1))
1270 && bitwise_inverted_equal_p (@0, @1, wascmp))
1271 { wascmp ? constant_boolean_node (false, type) : build_zero_cst (type); })))
1273 /* PR71636: Transform x & ((1U << b) - 1) -> x & ~(~0U << b); */
1275 (bit_and:c @0 (plus:s (lshift:s integer_onep @1) integer_minus_onep))
1276 (if (TYPE_UNSIGNED (type))
1277 (bit_and @0 (bit_not (lshift { build_all_ones_cst (type); } @1)))))
1279 (for bitop (bit_and bit_ior)
1281 /* PR35691: Transform
1282 (x == 0 & y == 0) -> (x | typeof(x)(y)) == 0.
1283 (x != 0 | y != 0) -> (x | typeof(x)(y)) != 0. */
1285 (bitop (cmp @0 integer_zerop@2) (cmp @1 integer_zerop))
1286 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1287 && INTEGRAL_TYPE_P (TREE_TYPE (@1))
1288 && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
1289 (cmp (bit_ior @0 (convert @1)) @2)))
1291 (x == -1 & y == -1) -> (x & typeof(x)(y)) == -1.
1292 (x != -1 | y != -1) -> (x & typeof(x)(y)) != -1. */
1294 (bitop (cmp @0 integer_all_onesp@2) (cmp @1 integer_all_onesp))
1295 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1296 && INTEGRAL_TYPE_P (TREE_TYPE (@1))
1297 && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
1298 (cmp (bit_and @0 (convert @1)) @2))))
1300 /* Fold (A & ~B) - (A & B) into (A ^ B) - B. */
1302 (minus (bit_and:cs @0 (bit_not @1)) (bit_and:cs @0 @1))
1303 (minus (bit_xor @0 @1) @1))
1305 (minus (bit_and:s @0 INTEGER_CST@2) (bit_and:s @0 INTEGER_CST@1))
1306 (if (~wi::to_wide (@2) == wi::to_wide (@1))
1307 (minus (bit_xor @0 @1) @1)))
1309 /* Fold (A & B) - (A & ~B) into B - (A ^ B). */
1311 (minus (bit_and:cs @0 @1) (bit_and:cs @0 (bit_not @1)))
1312 (minus @1 (bit_xor @0 @1)))
1314 /* Simplify (X & ~Y) |^+ (~X & Y) -> X ^ Y. */
1315 (for op (bit_ior bit_xor plus)
1317 (op (bit_and:c @0 @2) (bit_and:c @3 @1))
1318 (with { bool wascmp0, wascmp1; }
1319 (if (bitwise_inverted_equal_p (@2, @1, wascmp0)
1320 && bitwise_inverted_equal_p (@0, @3, wascmp1)
1321 && ((!wascmp0 && !wascmp1)
1322 || element_precision (type) == 1))
1325 /* PR53979: Transform ((a ^ b) | a) -> (a | b) */
1327 (bit_ior:c (bit_xor:c @0 @1) @0)
1330 /* (a & ~b) | (a ^ b) --> a ^ b */
1332 (bit_ior:c (bit_and:c @0 (bit_not @1)) (bit_xor:c@2 @0 @1))
1335 /* (a & ~b) ^ ~a --> ~(a & b) */
1337 (bit_xor:c (bit_and:cs @0 (bit_not @1)) (bit_not @0))
1338 (bit_not (bit_and @0 @1)))
1340 /* (~a & b) ^ a --> (a | b) */
1342 (bit_xor:c (bit_and:cs (bit_not @0) @1) @0)
1345 /* (a | b) & ~(a ^ b) --> a & b */
1347 (bit_and:c (bit_ior @0 @1) (bit_not (bit_xor:c @0 @1)))
1350 /* (a | b) & (a == b) --> a & b (boolean version of the above). */
1352 (bit_and:c (bit_ior @0 @1) (nop_convert? (eq:c @0 @1)))
1353 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1354 && TYPE_PRECISION (TREE_TYPE (@0)) == 1)
1357 /* a | ~(a ^ b) --> a | ~b */
1359 (bit_ior:c @0 (bit_not:s (bit_xor:c @0 @1)))
1360 (bit_ior @0 (bit_not @1)))
1362 /* a | (a == b) --> a | (b^1) (boolean version of the above). */
1364 (bit_ior:c @0 (nop_convert? (eq:c @0 @1)))
1365 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1366 && TYPE_PRECISION (TREE_TYPE (@0)) == 1)
1367 (bit_ior @0 (bit_xor @1 { build_one_cst (type); }))))
1369 /* a | ((~a) ^ b) --> a | (~b) (alt version of the above 2) */
1371 (bit_ior:c @0 (bit_xor:cs @1 @2))
1372 (with { bool wascmp; }
1373 (if (bitwise_inverted_equal_p (@0, @1, wascmp)
1374 && (!wascmp || element_precision (type) == 1))
1375 (bit_ior @0 (bit_not @2)))))
1377 /* a & ~(a ^ b) --> a & b */
1379 (bit_and:c @0 (bit_not (bit_xor:c @0 @1)))
1382 /* a & (a == b) --> a & b (boolean version of the above). */
1384 (bit_and:c @0 (nop_convert? (eq:c @0 @1)))
1385 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1386 && TYPE_PRECISION (TREE_TYPE (@0)) == 1)
1389 /* a & ((~a) ^ b) --> a & b (alt version of the above 2) */
1391 (bit_and:c @0 (bit_xor:c @1 @2))
1392 (with { bool wascmp; }
1393 (if (bitwise_inverted_equal_p (@0, @1, wascmp)
1394 && (!wascmp || element_precision (type) == 1))
1397 /* (a | b) | (a &^ b) --> a | b */
1398 (for op (bit_and bit_xor)
1400 (bit_ior:c (bit_ior@2 @0 @1) (op:c @0 @1))
1403 /* (a & b) | ~(a ^ b) --> ~(a ^ b) */
1405 (bit_ior:c (bit_and:c @0 @1) (bit_not@2 (bit_xor @0 @1)))
1408 /* (a & b) | (a == b) --> a == b */
1410 (bit_ior:c (bit_and:c @0 @1) (nop_convert?@2 (eq @0 @1)))
1411 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1412 && TYPE_PRECISION (TREE_TYPE (@0)) == 1)
1415 /* ~(~a & b) --> a | ~b */
1417 (bit_not (bit_and:cs (bit_not @0) @1))
1418 (bit_ior @0 (bit_not @1)))
1420 /* ~(~a | b) --> a & ~b */
1422 (bit_not (bit_ior:cs (bit_not @0) @1))
1423 (bit_and @0 (bit_not @1)))
1425 /* (a ^ b) & ((b ^ c) ^ a) --> (a ^ b) & ~c */
1427 (bit_and:c (bit_xor:c@3 @0 @1) (bit_xor:cs (bit_xor:cs @1 @2) @0))
1428 (bit_and @3 (bit_not @2)))
1430 /* (a ^ b) | ((b ^ c) ^ a) --> (a ^ b) | c */
1432 (bit_ior:c (bit_xor:c@3 @0 @1) (bit_xor:c (bit_xor:c @1 @2) @0))
1435 /* (~X | C) ^ D -> (X | C) ^ (~D ^ C) if (~D ^ C) can be simplified. */
1437 (bit_xor:c (bit_ior:cs (bit_not:s @0) @1) @2)
1438 (bit_xor (bit_ior @0 @1) (bit_xor! (bit_not! @2) @1)))
1440 /* (~X & C) ^ D -> (X & C) ^ (D ^ C) if (D ^ C) can be simplified. */
1442 (bit_xor:c (bit_and:cs (bit_not:s @0) @1) @2)
1443 (bit_xor (bit_and @0 @1) (bit_xor! @2 @1)))
1445 /* Simplify (~X & Y) to X ^ Y if we know that (X & ~Y) is 0. */
1447 (bit_and (bit_not SSA_NAME@0) INTEGER_CST@1)
1448 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1449 && wi::bit_and_not (get_nonzero_bits (@0), wi::to_wide (@1)) == 0)
1452 /* For constants M and N, if M == (1LL << cst) - 1 && (N & M) == M,
1453 ((A & N) + B) & M -> (A + B) & M
1454 Similarly if (N & M) == 0,
1455 ((A | N) + B) & M -> (A + B) & M
1456 and for - instead of + (or unary - instead of +)
1457 and/or ^ instead of |.
1458 If B is constant and (B & M) == 0, fold into A & M. */
1459 (for op (plus minus)
1460 (for bitop (bit_and bit_ior bit_xor)
1462 (bit_and (op:s (bitop:s@0 @3 INTEGER_CST@4) @1) INTEGER_CST@2)
1465 tree utype = fold_bit_and_mask (TREE_TYPE (@0), @2, op, @0, bitop,
1466 @3, @4, @1, ERROR_MARK, NULL_TREE,
1469 (convert (bit_and (op (convert:utype { pmop[0]; })
1470 (convert:utype { pmop[1]; }))
1471 (convert:utype @2))))))
1473 (bit_and (op:s @0 (bitop:s@1 @3 INTEGER_CST@4)) INTEGER_CST@2)
1476 tree utype = fold_bit_and_mask (TREE_TYPE (@0), @2, op, @0, ERROR_MARK,
1477 NULL_TREE, NULL_TREE, @1, bitop, @3,
1480 (convert (bit_and (op (convert:utype { pmop[0]; })
1481 (convert:utype { pmop[1]; }))
1482 (convert:utype @2)))))))
1484 (bit_and (op:s @0 @1) INTEGER_CST@2)
1487 tree utype = fold_bit_and_mask (TREE_TYPE (@0), @2, op, @0, ERROR_MARK,
1488 NULL_TREE, NULL_TREE, @1, ERROR_MARK,
1489 NULL_TREE, NULL_TREE, pmop); }
1491 (convert (bit_and (op (convert:utype { pmop[0]; })
1492 (convert:utype { pmop[1]; }))
1493 (convert:utype @2)))))))
1494 (for bitop (bit_and bit_ior bit_xor)
1496 (bit_and (negate:s (bitop:s@0 @2 INTEGER_CST@3)) INTEGER_CST@1)
1499 tree utype = fold_bit_and_mask (TREE_TYPE (@0), @1, NEGATE_EXPR, @0,
1500 bitop, @2, @3, NULL_TREE, ERROR_MARK,
1501 NULL_TREE, NULL_TREE, pmop); }
1503 (convert (bit_and (negate (convert:utype { pmop[0]; }))
1504 (convert:utype @1)))))))
1506 /* X % Y is smaller than Y. */
1509 (cmp:c (trunc_mod @0 @1) @1)
1510 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
1511 { constant_boolean_node (cmp == LT_EXPR, type); })))
1515 (bit_ior @0 integer_all_onesp@1)
1520 (bit_ior @0 integer_zerop)
1525 (bit_and @0 integer_zerop@1)
1530 (for op (bit_ior bit_xor)
1532 (op (convert? @0) (convert? @1))
1533 (with { bool wascmp; }
1534 (if (types_match (TREE_TYPE (@0), TREE_TYPE (@1))
1535 && bitwise_inverted_equal_p (@0, @1, wascmp))
1538 ? constant_boolean_node (true, type)
1539 : build_all_ones_cst (TREE_TYPE (@0)); })))))
1544 { build_zero_cst (type); })
1546 /* Canonicalize X ^ ~0 to ~X. */
1548 (bit_xor @0 integer_all_onesp@1)
1553 (bit_and @0 integer_all_onesp)
1556 /* x & x -> x, x | x -> x */
1557 (for bitop (bit_and bit_ior)
1562 /* x & C -> x if we know that x & ~C == 0. */
1565 (bit_and SSA_NAME@0 INTEGER_CST@1)
1566 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1567 && wi::bit_and_not (get_nonzero_bits (@0), wi::to_wide (@1)) == 0)
1570 /* `a & (x | CST)` -> a if we know that (a & ~CST) == 0 */
1572 (bit_and:c SSA_NAME@0 (bit_ior @1 INTEGER_CST@2))
1573 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1574 && wi::bit_and_not (get_nonzero_bits (@0), wi::to_wide (@2)) == 0)
1577 /* x | C -> C if we know that x & ~C == 0. */
1579 (bit_ior SSA_NAME@0 INTEGER_CST@1)
1580 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1581 && wi::bit_and_not (get_nonzero_bits (@0), wi::to_wide (@1)) == 0)
1585 /* ~(~X - Y) -> X + Y and ~(~X + Y) -> X - Y. */
1587 (bit_not (minus (bit_not @0) @1))
1590 (bit_not (plus:c (bit_not @0) @1))
1592 /* (~X - ~Y) -> Y - X. */
1594 (minus (bit_not @0) (bit_not @1))
1595 (if (!TYPE_OVERFLOW_SANITIZED (type))
1596 (with { tree utype = unsigned_type_for (type); }
1597 (convert (minus (convert:utype @1) (convert:utype @0))))))
1599 /* ~(X - Y) -> ~X + Y. */
1601 (bit_not (minus:s @0 @1))
1602 (plus (bit_not @0) @1))
1604 (bit_not (plus:s @0 INTEGER_CST@1))
1605 (if ((INTEGRAL_TYPE_P (type)
1606 && TYPE_UNSIGNED (type))
1607 || (!TYPE_OVERFLOW_SANITIZED (type)
1608 && may_negate_without_overflow_p (@1)))
1609 (plus (bit_not @0) { const_unop (NEGATE_EXPR, type, @1); })))
1612 /* ~X + Y -> (Y - X) - 1. */
1614 (plus:c (bit_not @0) @1)
1615 (if (ANY_INTEGRAL_TYPE_P (type)
1616 && TYPE_OVERFLOW_WRAPS (type)
1617 /* -1 - X is folded to ~X, so we'd recurse endlessly. */
1618 && !integer_all_onesp (@1))
1619 (plus (minus @1 @0) { build_minus_one_cst (type); })
1620 (if (INTEGRAL_TYPE_P (type)
1621 && TREE_CODE (@1) == INTEGER_CST
1622 && wi::to_wide (@1) != wi::min_value (TYPE_PRECISION (type),
1624 (minus (plus @1 { build_minus_one_cst (type); }) @0))))
1627 /* ~(X >> Y) -> ~X >> Y if ~X can be simplified. */
1629 (bit_not (rshift:s @0 @1))
1630 (if (!TYPE_UNSIGNED (TREE_TYPE (@0)))
1631 (rshift (bit_not! @0) @1)
1632 /* For logical right shifts, this is possible only if @0 doesn't
1633 have MSB set and the logical right shift is changed into
1634 arithmetic shift. */
1635 (if (INTEGRAL_TYPE_P (type)
1636 && !wi::neg_p (tree_nonzero_bits (@0)))
1637 (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
1638 (convert (rshift (bit_not! (convert:stype @0)) @1))))))
1640 /* x + (x & 1) -> (x + 1) & ~1 */
1642 (plus:c @0 (bit_and:s @0 integer_onep@1))
1643 (bit_and (plus @0 @1) (bit_not @1)))
1645 /* x & ~(x & y) -> x & ~y */
1646 /* x | ~(x | y) -> x | ~y */
1647 (for bitop (bit_and bit_ior)
1649 (bitop:c @0 (bit_not (bitop:cs @0 @1)))
1650 (bitop @0 (bit_not @1))))
1652 /* (~x & y) | ~(x | y) -> ~x */
1654 (bit_ior:c (bit_and:c (bit_not@2 @0) @1) (bit_not (bit_ior:c @0 @1)))
1657 /* (x | y) ^ (x | ~y) -> ~x */
1659 (bit_xor:c (bit_ior:c @0 @1) (bit_ior:c @0 (bit_not @1)))
1662 /* (x & y) | ~(x | y) -> ~(x ^ y) */
1664 (bit_ior:c (bit_and:s @0 @1) (bit_not:s (bit_ior:s @0 @1)))
1665 (bit_not (bit_xor @0 @1)))
1667 /* (~x | y) ^ (x ^ y) -> x | ~y */
1669 (bit_xor:c (bit_ior:cs (bit_not @0) @1) (bit_xor:s @0 @1))
1670 (bit_ior @0 (bit_not @1)))
1672 /* (x ^ y) | ~(x | y) -> ~(x & y) */
1674 (bit_ior:c (bit_xor:s @0 @1) (bit_not:s (bit_ior:s @0 @1)))
1675 (bit_not (bit_and @0 @1)))
1677 /* (x & y) ^ (x | y) -> x ^ y */
1679 (bit_xor:c (bit_and @0 @1) (bit_ior @0 @1))
1682 /* (x ^ y) ^ (x | y) -> x & y */
1684 (bit_xor:c (bit_xor @0 @1) (bit_ior @0 @1))
1687 /* (x & y) + (x ^ y) -> x | y */
1688 /* (x & y) | (x ^ y) -> x | y */
1689 /* (x & y) ^ (x ^ y) -> x | y */
1690 (for op (plus bit_ior bit_xor)
1692 (op:c (bit_and @0 @1) (bit_xor @0 @1))
1695 /* (x & y) + (x | y) -> x + y */
1697 (plus:c (bit_and @0 @1) (bit_ior @0 @1))
1700 /* (x + y) - (x | y) -> x & y */
1702 (minus (plus @0 @1) (bit_ior @0 @1))
1703 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1704 && !TYPE_SATURATING (type))
1707 /* (x + y) - (x & y) -> x | y */
1709 (minus (plus @0 @1) (bit_and @0 @1))
1710 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1711 && !TYPE_SATURATING (type))
1714 /* (x | y) - y -> (x & ~y) */
1716 (minus (bit_ior:cs @0 @1) @1)
1717 (bit_and @0 (bit_not @1)))
1719 /* (x | y) - (x ^ y) -> x & y */
1721 (minus (bit_ior @0 @1) (bit_xor @0 @1))
1724 /* (x | y) - (x & y) -> x ^ y */
1726 (minus (bit_ior @0 @1) (bit_and @0 @1))
1729 /* (x | y) & ~(x & y) -> x ^ y */
1731 (bit_and:c (bit_ior @0 @1) (bit_not (bit_and @0 @1)))
1734 /* (x | y) & (~x ^ y) -> x & y */
1736 (bit_and:c (bit_ior:c @0 @1) (bit_xor:c @1 @2))
1737 (with { bool wascmp; }
1738 (if (bitwise_inverted_equal_p (@0, @2, wascmp)
1739 && (!wascmp || element_precision (type) == 1))
1742 /* (~x | y) & (x | ~y) -> ~(x ^ y) */
1744 (bit_and (bit_ior:cs (bit_not @0) @1) (bit_ior:cs @0 (bit_not @1)))
1745 (bit_not (bit_xor @0 @1)))
1747 /* (~x | y) ^ (x | ~y) -> x ^ y */
1749 (bit_xor (bit_ior:c (bit_not @0) @1) (bit_ior:c @0 (bit_not @1)))
1752 /* ((x & y) - (x | y)) - 1 -> ~(x ^ y) */
1754 (plus (nop_convert1? (minus@2 (nop_convert2? (bit_and:c @0 @1))
1755 (nop_convert2? (bit_ior @0 @1))))
1757 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1758 && !TYPE_SATURATING (type) && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2))
1759 && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@2))
1760 && !TYPE_SATURATING (TREE_TYPE (@2)))
1761 (bit_not (convert (bit_xor @0 @1)))))
1763 (minus (nop_convert1? (plus@2 (nop_convert2? (bit_and:c @0 @1))
1765 (nop_convert3? (bit_ior @0 @1)))
1766 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1767 && !TYPE_SATURATING (type) && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2))
1768 && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@2))
1769 && !TYPE_SATURATING (TREE_TYPE (@2)))
1770 (bit_not (convert (bit_xor @0 @1)))))
1772 (minus (nop_convert1? (bit_and @0 @1))
1773 (nop_convert2? (plus@2 (nop_convert3? (bit_ior:c @0 @1))
1775 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1776 && !TYPE_SATURATING (type) && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2))
1777 && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@2))
1778 && !TYPE_SATURATING (TREE_TYPE (@2)))
1779 (bit_not (convert (bit_xor @0 @1)))))
1781 /* ~x & ~y -> ~(x | y)
1782 ~x | ~y -> ~(x & y) */
1783 (for op (bit_and bit_ior)
1784 rop (bit_ior bit_and)
1786 (op (convert1? (bit_not @0)) (convert2? (bit_not @1)))
1787 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1788 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
1789 (bit_not (rop (convert @0) (convert @1))))))
1791 /* If we are XORing or adding two BIT_AND_EXPR's, both of which are and'ing
1792 with a constant, and the two constants have no bits in common,
1793 we should treat this as a BIT_IOR_EXPR since this may produce more
1795 (for op (bit_xor plus)
1797 (op (convert1? (bit_and@4 @0 INTEGER_CST@1))
1798 (convert2? (bit_and@5 @2 INTEGER_CST@3)))
1799 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
1800 && tree_nop_conversion_p (type, TREE_TYPE (@2))
1801 && (wi::to_wide (@1) & wi::to_wide (@3)) == 0)
1802 (bit_ior (convert @4) (convert @5)))))
1804 /* (X | Y) ^ X -> Y & ~ X*/
1806 (bit_xor:c (convert1? (bit_ior:c @@0 @1)) (convert2? @0))
1807 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1808 (convert (bit_and @1 (bit_not @0)))))
1810 /* (~X | Y) ^ X -> ~(X & Y). */
1812 (bit_xor:c (nop_convert1? (bit_ior:c (nop_convert2? (bit_not @0)) @1)) @2)
1813 (if (bitwise_equal_p (@0, @2))
1814 (convert (bit_not (bit_and @0 (convert @1))))))
1816 /* Convert ~X ^ ~Y to X ^ Y. */
1818 (bit_xor (convert1? (bit_not @0)) (convert2? (bit_not @1)))
1819 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1820 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
1821 (bit_xor (convert @0) (convert @1))))
1823 /* Convert ~X ^ C to X ^ ~C. */
1825 (bit_xor (convert? (bit_not @0)) INTEGER_CST@1)
1826 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1827 (bit_xor (convert @0) (bit_not @1))))
1829 /* Fold (X & Y) ^ Y and (X ^ Y) & Y as ~X & Y. */
1830 (for opo (bit_and bit_xor)
1831 opi (bit_xor bit_and)
1833 (opo:c (opi:cs @0 @1) @1)
1834 (bit_and (bit_not @0) @1)))
1836 /* Given a bit-wise operation CODE applied to ARG0 and ARG1, see if both
1837 operands are another bit-wise operation with a common input. If so,
1838 distribute the bit operations to save an operation and possibly two if
1839 constants are involved. For example, convert
1840 (A | B) & (A | C) into A | (B & C)
1841 Further simplification will occur if B and C are constants. */
1842 (for op (bit_and bit_ior bit_xor)
1843 rop (bit_ior bit_and bit_and)
1845 (op (convert? (rop:c @@0 @1)) (convert? (rop:c @0 @2)))
1846 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1847 && tree_nop_conversion_p (type, TREE_TYPE (@2)))
1848 (rop (convert @0) (op (convert @1) (convert @2))))))
1850 /* Some simple reassociation for bit operations, also handled in reassoc. */
1851 /* (X & Y) & Y -> X & Y
1852 (X | Y) | Y -> X | Y */
1853 (for op (bit_and bit_ior)
1855 (op:c (convert1?@2 (op:c @0 @@1)) (convert2? @1))
1857 /* (X ^ Y) ^ Y -> X */
1859 (bit_xor:c (convert1? (bit_xor:c @0 @@1)) (convert2? @1))
1862 /* (X & ~Y) & Y -> 0 */
1864 (bit_and:c (bit_and @0 @1) @2)
1865 (with { bool wascmp; }
1866 (if (bitwise_inverted_equal_p (@0, @2, wascmp)
1867 || bitwise_inverted_equal_p (@1, @2, wascmp))
1868 { wascmp ? constant_boolean_node (false, type) : build_zero_cst (type); })))
1869 /* (X | ~Y) | Y -> -1 */
1871 (bit_ior:c (bit_ior @0 @1) @2)
1872 (with { bool wascmp; }
1873 (if ((bitwise_inverted_equal_p (@0, @2, wascmp)
1874 || bitwise_inverted_equal_p (@1, @2, wascmp))
1875 && (!wascmp || element_precision (type) == 1))
1876 { build_all_ones_cst (TREE_TYPE (@0)); })))
1878 /* (X & Y) & (X & Z) -> (X & Y) & Z
1879 (X | Y) | (X | Z) -> (X | Y) | Z */
1880 (for op (bit_and bit_ior)
1882 (op (convert1?@3 (op:c@4 @0 @1)) (convert2?@5 (op:c@6 @0 @2)))
1883 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1884 && tree_nop_conversion_p (type, TREE_TYPE (@2)))
1885 (if (single_use (@5) && single_use (@6))
1886 (op @3 (convert @2))
1887 (if (single_use (@3) && single_use (@4))
1888 (op (convert @1) @5))))))
1889 /* (X ^ Y) ^ (X ^ Z) -> Y ^ Z */
1891 (bit_xor (convert1? (bit_xor:c @0 @1)) (convert2? (bit_xor:c @0 @2)))
1892 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1893 && tree_nop_conversion_p (type, TREE_TYPE (@2)))
1894 (bit_xor (convert @1) (convert @2))))
1896 /* Convert abs (abs (X)) into abs (X).
1897 also absu (absu (X)) into absu (X). */
1903 (absu (convert@2 (absu@1 @0)))
1904 (if (tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@1)))
1907 /* Convert abs[u] (-X) -> abs[u] (X). */
1916 /* Convert abs[u] (X) where X is nonnegative -> (X). */
1918 (abs tree_expr_nonnegative_p@0)
1922 (absu tree_expr_nonnegative_p@0)
1925 /* Simplify (-(X < 0) | 1) * X into abs (X) or absu(X). */
1927 (mult:c (nop_convert1?
1928 (bit_ior (nop_convert2? (negate (convert? (lt @0 integer_zerop))))
1931 (if (INTEGRAL_TYPE_P (type)
1932 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
1933 && !TYPE_UNSIGNED (TREE_TYPE (@0)))
1934 (if (TYPE_UNSIGNED (type))
1941 /* A few cases of fold-const.cc negate_expr_p predicate. */
1942 (match negate_expr_p
1944 (if ((INTEGRAL_TYPE_P (type)
1945 && TYPE_UNSIGNED (type))
1946 || (!TYPE_OVERFLOW_SANITIZED (type)
1947 && may_negate_without_overflow_p (t)))))
1948 (match negate_expr_p
1950 (match negate_expr_p
1952 (if (!TYPE_OVERFLOW_SANITIZED (type))))
1953 (match negate_expr_p
1955 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (t)))))
1956 /* VECTOR_CST handling of non-wrapping types would recurse in unsupported
1958 (match negate_expr_p
1960 (if (FLOAT_TYPE_P (TREE_TYPE (type)) || TYPE_OVERFLOW_WRAPS (type))))
1961 (match negate_expr_p
1963 (if ((ANY_INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_WRAPS (type))
1964 || (FLOAT_TYPE_P (type)
1965 && !HONOR_SIGN_DEPENDENT_ROUNDING (type)
1966 && !HONOR_SIGNED_ZEROS (type)))))
1968 /* (-A) * (-B) -> A * B */
1970 (mult:c (convert1? (negate @0)) (convert2? negate_expr_p@1))
1971 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
1972 && tree_nop_conversion_p (type, TREE_TYPE (@1)))
1973 (mult (convert @0) (convert (negate @1)))))
1975 /* -(A + B) -> (-B) - A. */
1977 (negate (plus:c @0 negate_expr_p@1))
1978 (if (!HONOR_SIGN_DEPENDENT_ROUNDING (type)
1979 && !HONOR_SIGNED_ZEROS (type))
1980 (minus (negate @1) @0)))
1982 /* -(A - B) -> B - A. */
1984 (negate (minus @0 @1))
1985 (if ((ANY_INTEGRAL_TYPE_P (type) && !TYPE_OVERFLOW_SANITIZED (type))
1986 || (FLOAT_TYPE_P (type)
1987 && !HONOR_SIGN_DEPENDENT_ROUNDING (type)
1988 && !HONOR_SIGNED_ZEROS (type)))
1991 (negate (pointer_diff @0 @1))
1992 (if (TYPE_OVERFLOW_UNDEFINED (type))
1993 (pointer_diff @1 @0)))
1995 /* A - B -> A + (-B) if B is easily negatable. */
1997 (minus @0 negate_expr_p@1)
1998 (if (!FIXED_POINT_TYPE_P (type))
1999 (plus @0 (negate @1))))
2001 /* 1 - a is a ^ 1 if a had a bool range. */
2002 /* This is only enabled for gimple as sometimes
2003 cfun is not set for the function which contains
2004 the SSA_NAME (e.g. while IPA passes are happening,
2005 fold might be called). */
2007 (minus integer_onep@0 SSA_NAME@1)
2008 (if (INTEGRAL_TYPE_P (type)
2009 && ssa_name_has_boolean_range (@1))
2012 /* Other simplifications of negation (c.f. fold_negate_expr_1). */
2014 (negate (mult:c@0 @1 negate_expr_p@2))
2015 (if (! TYPE_UNSIGNED (type)
2016 && ! HONOR_SIGN_DEPENDENT_ROUNDING (type)
2018 (mult @1 (negate @2))))
2021 (negate (rdiv@0 @1 negate_expr_p@2))
2022 (if (! HONOR_SIGN_DEPENDENT_ROUNDING (type)
2024 (rdiv @1 (negate @2))))
2027 (negate (rdiv@0 negate_expr_p@1 @2))
2028 (if (! HONOR_SIGN_DEPENDENT_ROUNDING (type)
2030 (rdiv (negate @1) @2)))
2032 /* Fold -((int)x >> (prec - 1)) into (unsigned)x >> (prec - 1). */
2034 (negate (convert? (rshift @0 INTEGER_CST@1)))
2035 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
2036 && wi::to_wide (@1) == element_precision (type) - 1)
2037 (with { tree stype = TREE_TYPE (@0);
2038 tree ntype = TYPE_UNSIGNED (stype) ? signed_type_for (stype)
2039 : unsigned_type_for (stype); }
2040 (if (VECTOR_TYPE_P (type))
2041 (view_convert (rshift (view_convert:ntype @0) @1))
2042 (convert (rshift (convert:ntype @0) @1))))))
2044 /* Try to fold (type) X op CST -> (type) (X op ((type-x) CST))
2046 For bitwise binary operations apply operand conversions to the
2047 binary operation result instead of to the operands. This allows
2048 to combine successive conversions and bitwise binary operations.
2049 We combine the above two cases by using a conditional convert. */
2050 (for bitop (bit_and bit_ior bit_xor)
2052 (bitop (convert@2 @0) (convert?@3 @1))
2053 (if (((TREE_CODE (@1) == INTEGER_CST
2054 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
2055 && (int_fits_type_p (@1, TREE_TYPE (@0))
2056 || tree_nop_conversion_p (TREE_TYPE (@0), type)))
2057 || types_match (@0, @1))
2058 && !POINTER_TYPE_P (TREE_TYPE (@0))
2059 && !VECTOR_TYPE_P (TREE_TYPE (@0))
2060 && TREE_CODE (TREE_TYPE (@0)) != OFFSET_TYPE
2061 /* ??? This transform conflicts with fold-const.cc doing
2062 Convert (T)(x & c) into (T)x & (T)c, if c is an integer
2063 constants (if x has signed type, the sign bit cannot be set
2064 in c). This folds extension into the BIT_AND_EXPR.
2065 Restrict it to GIMPLE to avoid endless recursions. */
2066 && (bitop != BIT_AND_EXPR || GIMPLE)
2067 && (/* That's a good idea if the conversion widens the operand, thus
2068 after hoisting the conversion the operation will be narrower.
2069 It is also a good if the conversion is a nop as moves the
2070 conversion to one side; allowing for combining of the conversions. */
2071 TYPE_PRECISION (TREE_TYPE (@0)) < TYPE_PRECISION (type)
2072 /* The conversion check for being a nop can only be done at the gimple
2073 level as fold_binary has some re-association code which can conflict
2074 with this if there is a "constant" which is not a full INTEGER_CST. */
2075 || (GIMPLE && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (type))
2076 /* It's also a good idea if the conversion is to a non-integer
2078 || GET_MODE_CLASS (TYPE_MODE (type)) != MODE_INT
2079 /* Or if the precision of TO is not the same as the precision
2081 || !type_has_mode_precision_p (type)
2082 /* In GIMPLE, getting rid of 2 conversions for one new results
2085 && TREE_CODE (@1) != INTEGER_CST
2086 && tree_nop_conversion_p (type, TREE_TYPE (@0))
2088 && single_use (@3))))
2089 (convert (bitop @0 (convert @1)))))
2090 /* In GIMPLE, getting rid of 2 conversions for one new results
2093 (convert (bitop:cs@2 (nop_convert:s @0) @1))
2095 && TREE_CODE (@1) != INTEGER_CST
2096 && tree_nop_conversion_p (type, TREE_TYPE (@2))
2097 && types_match (type, @0)
2098 && !POINTER_TYPE_P (TREE_TYPE (@0))
2099 && TREE_CODE (TREE_TYPE (@0)) != OFFSET_TYPE)
2100 (bitop @0 (convert @1)))))
2102 (for bitop (bit_and bit_ior)
2103 rbitop (bit_ior bit_and)
2104 /* (x | y) & x -> x */
2105 /* (x & y) | x -> x */
2107 (bitop:c (rbitop:c @0 @1) @0)
2109 /* (~x | y) & x -> x & y */
2110 /* (~x & y) | x -> x | y */
2112 (bitop:c (rbitop:c @2 @1) @0)
2113 (with { bool wascmp; }
2114 (if (bitwise_inverted_equal_p (@0, @2, wascmp)
2115 && (!wascmp || element_precision (type) == 1))
2117 /* (x | y) & (x & z) -> (x & z) */
2118 /* (x & y) | (x | z) -> (x | z) */
2120 (bitop:c (rbitop:c @0 @1) (bitop:c@3 @0 @2))
2122 /* (x | c) & ~(y | c) -> x & ~(y | c) */
2123 /* (x & c) | ~(y & c) -> x | ~(y & c) */
2125 (bitop:c (rbitop:c @0 @1) (bit_not@3 (rbitop:c @1 @2)))
2127 /* x & ~(y | x) -> 0 */
2128 /* x | ~(y & x) -> -1 */
2130 (bitop:c @0 (bit_not (rbitop:c @0 @1)))
2131 (if (bitop == BIT_AND_EXPR)
2132 { build_zero_cst (type); }
2133 { build_minus_one_cst (type); })))
2135 /* ((x | y) & z) | x -> (z & y) | x
2136 ((x ^ y) & z) | x -> (z & y) | x */
2137 (for op (bit_ior bit_xor)
2139 (bit_ior:c (nop_convert1?:s
2140 (bit_and:cs (nop_convert2?:s (op:cs @0 @1)) @2)) @3)
2141 (if (bitwise_equal_p (@0, @3))
2142 (convert (bit_ior (bit_and @1 (convert @2)) (convert @0))))))
2144 /* (x | CST1) & CST2 -> (x & CST2) | (CST1 & CST2) */
2146 (bit_and (bit_ior @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
2147 (bit_ior (bit_and @0 @2) (bit_and! @1 @2)))
2149 /* Combine successive equal operations with constants. */
2150 (for bitop (bit_and bit_ior bit_xor)
2152 (bitop (bitop @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
2153 (if (!CONSTANT_CLASS_P (@0))
2154 /* This is the canonical form regardless of whether (bitop @1 @2) can be
2155 folded to a constant. */
2156 (bitop @0 (bitop! @1 @2))
2157 /* In this case we have three constants and (bitop @0 @1) doesn't fold
2158 to a constant. This can happen if @0 or @1 is a POLY_INT_CST and if
2159 the values involved are such that the operation can't be decided at
2160 compile time. Try folding one of @0 or @1 with @2 to see whether
2161 that combination can be decided at compile time.
2163 Keep the existing form if both folds fail, to avoid endless
2165 (with { tree cst1 = const_binop (bitop, type, @0, @2); }
2167 (bitop @1 { cst1; })
2168 (with { tree cst2 = const_binop (bitop, type, @1, @2); }
2170 (bitop @0 { cst2; }))))))))
2172 /* Try simple folding for X op !X, and X op X with the help
2173 of the truth_valued_p and logical_inverted_value predicates. */
2174 (match truth_valued_p
2176 (if (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1)))
2177 (for op (tcc_comparison truth_and truth_andif truth_or truth_orif truth_xor)
2178 (match truth_valued_p
2180 (match truth_valued_p
2183 (match (logical_inverted_value @0)
2185 (match (logical_inverted_value @0)
2186 (bit_not truth_valued_p@0))
2187 (match (logical_inverted_value @0)
2188 (eq @0 integer_zerop))
2189 (match (logical_inverted_value @0)
2190 (ne truth_valued_p@0 integer_truep))
2191 (match (logical_inverted_value @0)
2192 (bit_xor truth_valued_p@0 integer_truep))
2196 (bit_and:c @0 (logical_inverted_value @0))
2197 { build_zero_cst (type); })
2198 /* X | !X and X ^ !X -> 1, , if X is truth-valued. */
2199 (for op (bit_ior bit_xor)
2201 (op:c truth_valued_p@0 (logical_inverted_value @0))
2202 { constant_boolean_node (true, type); }))
2203 /* X ==/!= !X is false/true. */
2206 (op:c truth_valued_p@0 (logical_inverted_value @0))
2207 { constant_boolean_node (op == NE_EXPR ? true : false, type); }))
2211 (bit_not (bit_not @0))
2214 /* zero_one_valued_p will match when a value is known to be either
2215 0 or 1 including constants 0 or 1.
2216 Signed 1-bits includes -1 so they cannot match here. */
2217 (match zero_one_valued_p
2219 (if (INTEGRAL_TYPE_P (type)
2220 && (TYPE_UNSIGNED (type)
2221 || TYPE_PRECISION (type) > 1)
2222 && wi::leu_p (tree_nonzero_bits (@0), 1))))
2223 (match zero_one_valued_p
2225 (if (INTEGRAL_TYPE_P (type)
2226 && (TYPE_UNSIGNED (type)
2227 || TYPE_PRECISION (type) > 1))))
2229 /* (a&1) is always [0,1] too. This is useful again when
2230 the range is not known. */
2231 /* Note this can't be recursive due to VN handling of equivalents,
2232 VN and would cause an infinite recursion. */
2233 (match zero_one_valued_p
2234 (bit_and:c@0 @1 integer_onep)
2235 (if (INTEGRAL_TYPE_P (type))))
2237 /* A conversion from an zero_one_valued_p is still a [0,1].
2238 This is useful when the range of a variable is not known */
2239 /* Note this matches can't be recursive because of the way VN handles
2240 nop conversions being equivalent and then recursive between them. */
2241 (match zero_one_valued_p
2243 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2244 && (TYPE_UNSIGNED (TREE_TYPE (@1))
2245 || TYPE_PRECISION (TREE_TYPE (@1)) > 1)
2246 && wi::leu_p (tree_nonzero_bits (@1), 1))))
2248 /* Transform { 0 or 1 } * { 0 or 1 } into { 0 or 1 } & { 0 or 1 }. */
2250 (mult zero_one_valued_p@0 zero_one_valued_p@1)
2251 (if (INTEGRAL_TYPE_P (type))
2254 (for cmp (tcc_comparison)
2255 icmp (inverted_tcc_comparison)
2256 /* Fold (((a < b) & c) | ((a >= b) & d)) into (a < b ? c : d) & 1. */
2259 (bit_and:c (convert? (cmp@0 @01 @02)) @3)
2260 (bit_and:c (convert? (icmp@4 @01 @02)) @5))
2261 (if (INTEGRAL_TYPE_P (type)
2262 && invert_tree_comparison (cmp, HONOR_NANS (@01)) == icmp
2263 /* The scalar version has to be canonicalized after vectorization
2264 because it makes unconditional loads conditional ones, which
2265 means we lose vectorization because the loads may trap. */
2266 && canonicalize_math_after_vectorization_p ())
2267 (bit_and (cond @0 @3 @5) { build_one_cst (type); })))
2269 /* Fold ((-(a < b) & c) | (-(a >= b) & d)) into a < b ? c : d. This is
2270 canonicalized further and we recognize the conditional form:
2271 (a < b ? c : 0) | (a >= b ? d : 0) into a < b ? c : d. */
2274 (cond (cmp@0 @01 @02) @3 zerop)
2275 (cond (icmp@4 @01 @02) @5 zerop))
2276 (if (INTEGRAL_TYPE_P (type)
2277 && invert_tree_comparison (cmp, HONOR_NANS (@01)) == icmp
2278 /* The scalar version has to be canonicalized after vectorization
2279 because it makes unconditional loads conditional ones, which
2280 means we lose vectorization because the loads may trap. */
2281 && canonicalize_math_after_vectorization_p ())
2284 /* Vector Fold (((a < b) & c) | ((a >= b) & d)) into a < b ? c : d.
2285 and ((~(a < b) & c) | (~(a >= b) & d)) into a < b ? c : d. */
2288 (bit_and:c (vec_cond:s (cmp@0 @6 @7) @4 @5) @2)
2289 (bit_and:c (vec_cond:s (icmp@1 @6 @7) @4 @5) @3))
2290 (if (integer_zerop (@5)
2291 && invert_tree_comparison (cmp, HONOR_NANS (@6)) == icmp)
2293 (if (integer_onep (@4))
2294 (bit_and (vec_cond @0 @2 @3) @4))
2295 (if (integer_minus_onep (@4))
2296 (vec_cond @0 @2 @3)))
2297 (if (integer_zerop (@4)
2298 && invert_tree_comparison (cmp, HONOR_NANS (@6)) == icmp)
2300 (if (integer_onep (@5))
2301 (bit_and (vec_cond @0 @3 @2) @5))
2302 (if (integer_minus_onep (@5))
2303 (vec_cond @0 @3 @2))))))
2305 /* Scalar Vectorized Fold ((-(a < b) & c) | (-(a >= b) & d))
2306 into a < b ? d : c. */
2309 (vec_cond:s (cmp@0 @4 @5) @2 integer_zerop)
2310 (vec_cond:s (icmp@1 @4 @5) @3 integer_zerop))
2311 (if (invert_tree_comparison (cmp, HONOR_NANS (@4)) == icmp)
2312 (vec_cond @0 @2 @3))))
2314 /* Transform X & -Y into X * Y when Y is { 0 or 1 }. */
2316 (bit_and:c (convert? (negate zero_one_valued_p@0)) @1)
2317 (if (INTEGRAL_TYPE_P (type)
2318 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
2319 && TREE_CODE (TREE_TYPE (@0)) != BOOLEAN_TYPE
2320 /* Sign extending of the neg or a truncation of the neg
2322 && (!TYPE_UNSIGNED (TREE_TYPE (@0))
2323 || TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))))
2324 (mult (convert @0) @1)))
2326 /* Narrow integer multiplication by a zero_one_valued_p operand.
2327 Multiplication by [0,1] is guaranteed not to overflow. */
2329 (convert (mult@0 zero_one_valued_p@1 INTEGER_CST@2))
2330 (if (INTEGRAL_TYPE_P (type)
2331 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
2332 && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (@0)))
2333 (mult (convert @1) (convert @2))))
2335 /* (X << C) != 0 can be simplified to X, when C is zero_one_valued_p.
2336 Check that the shift is well-defined (C is less than TYPE_PRECISION)
2337 as some targets (such as x86's SSE) may return zero for larger C. */
2339 (ne (lshift zero_one_valued_p@0 INTEGER_CST@1) integer_zerop@2)
2340 (if (tree_fits_shwi_p (@1)
2341 && tree_to_shwi (@1) > 0
2342 && tree_to_shwi (@1) < TYPE_PRECISION (TREE_TYPE (@0)))
2345 /* (X << C) == 0 can be simplified to X == 0, when C is zero_one_valued_p.
2346 Check that the shift is well-defined (C is less than TYPE_PRECISION)
2347 as some targets (such as x86's SSE) may return zero for larger C. */
2349 (eq (lshift zero_one_valued_p@0 INTEGER_CST@1) integer_zerop@2)
2350 (if (tree_fits_shwi_p (@1)
2351 && tree_to_shwi (@1) > 0
2352 && tree_to_shwi (@1) < TYPE_PRECISION (TREE_TYPE (@0)))
2355 /* Convert ~ (-A) to A - 1. */
2357 (bit_not (convert? (negate @0)))
2358 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
2359 || !TYPE_UNSIGNED (TREE_TYPE (@0)))
2360 (convert (minus @0 { build_each_one_cst (TREE_TYPE (@0)); }))))
2362 /* Convert - (~A) to A + 1. */
2364 (negate (nop_convert? (bit_not @0)))
2365 (plus (view_convert @0) { build_each_one_cst (type); }))
2367 /* (a & b) ^ (a == b) -> !(a | b) */
2368 /* (a & b) == (a ^ b) -> !(a | b) */
2369 (for first_op (bit_xor eq)
2370 second_op (eq bit_xor)
2372 (first_op:c (bit_and:c truth_valued_p@0 truth_valued_p@1) (second_op:c @0 @1))
2373 (bit_not (bit_ior @0 @1))))
2375 /* Convert ~ (A - 1) or ~ (A + -1) to -A. */
2377 (bit_not (convert? (minus @0 integer_each_onep)))
2378 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
2379 || !TYPE_UNSIGNED (TREE_TYPE (@0)))
2380 (convert (negate @0))))
2382 (bit_not (convert? (plus @0 integer_all_onesp)))
2383 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
2384 || !TYPE_UNSIGNED (TREE_TYPE (@0)))
2385 (convert (negate @0))))
2387 /* Part of convert ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify. */
2389 (bit_not (convert? (bit_xor @0 INTEGER_CST@1)))
2390 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2391 (convert (bit_xor @0 (bit_not @1)))))
2393 (bit_not (convert? (bit_xor:c (bit_not @0) @1)))
2394 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2395 (convert (bit_xor @0 @1))))
2397 /* Otherwise prefer ~(X ^ Y) to ~X ^ Y as more canonical. */
2399 (bit_xor:c (nop_convert?:s (bit_not:s @0)) @1)
2400 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2401 (bit_not (bit_xor (view_convert @0) @1))))
2403 /* ~(a ^ b) is a == b for truth valued a and b. */
2405 (bit_not (bit_xor:s truth_valued_p@0 truth_valued_p@1))
2406 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2407 && TYPE_PRECISION (TREE_TYPE (@0)) == 1)
2408 (convert (eq @0 @1))))
2410 /* (~a) == b is a ^ b for truth valued a and b. */
2412 (eq:c (bit_not:s truth_valued_p@0) truth_valued_p@1)
2413 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2414 && TYPE_PRECISION (TREE_TYPE (@0)) == 1)
2415 (convert (bit_xor @0 @1))))
2417 /* (x & ~m) | (y & m) -> ((x ^ y) & m) ^ x */
2419 (bit_ior:c (bit_and:cs @0 (bit_not @2)) (bit_and:cs @1 @2))
2420 (bit_xor (bit_and (bit_xor @0 @1) @2) @0))
2422 /* Fold A - (A & B) into ~B & A. */
2424 (minus (convert1? @0) (convert2?:s (bit_and:cs @@0 @1)))
2425 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
2426 && tree_nop_conversion_p (type, TREE_TYPE (@1)))
2427 (convert (bit_and (bit_not @1) @0))))
2429 /* (m1 CMP m2) * d -> (m1 CMP m2) ? d : 0 */
2430 (if (!canonicalize_math_p ())
2431 (for cmp (tcc_comparison)
2433 (mult:c (convert (cmp@0 @1 @2)) @3)
2434 (if (INTEGRAL_TYPE_P (type)
2435 && INTEGRAL_TYPE_P (TREE_TYPE (@0)))
2436 (cond @0 @3 { build_zero_cst (type); })))
2437 /* (-(m1 CMP m2)) & d -> (m1 CMP m2) ? d : 0 */
2439 (bit_and:c (negate (convert (cmp@0 @1 @2))) @3)
2440 (if (INTEGRAL_TYPE_P (type)
2441 && INTEGRAL_TYPE_P (TREE_TYPE (@0)))
2442 (cond @0 @3 { build_zero_cst (type); })))
2446 /* For integral types with undefined overflow and C != 0 fold
2447 x * C EQ/NE y * C into x EQ/NE y. */
2450 (cmp (mult:c @0 @1) (mult:c @2 @1))
2451 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2452 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2453 && tree_expr_nonzero_p (@1))
2456 /* For integral types with wrapping overflow and C odd fold
2457 x * C EQ/NE y * C into x EQ/NE y. */
2460 (cmp (mult @0 INTEGER_CST@1) (mult @2 @1))
2461 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2462 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))
2463 && (TREE_INT_CST_LOW (@1) & 1) != 0)
2466 /* For integral types with undefined overflow and C != 0 fold
2467 x * C RELOP y * C into:
2469 x RELOP y for nonnegative C
2470 y RELOP x for negative C */
2471 (for cmp (lt gt le ge)
2473 (cmp (mult:c @0 @1) (mult:c @2 @1))
2474 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2475 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
2476 (if (tree_expr_nonnegative_p (@1) && tree_expr_nonzero_p (@1))
2478 (if (TREE_CODE (@1) == INTEGER_CST
2479 && wi::neg_p (wi::to_wide (@1), TYPE_SIGN (TREE_TYPE (@1))))
2482 /* (X - 1U) <= INT_MAX-1U into (int) X > 0. */
2486 (cmp (plus @0 integer_minus_onep@1) INTEGER_CST@2)
2487 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2488 && TYPE_UNSIGNED (TREE_TYPE (@0))
2489 && TYPE_PRECISION (TREE_TYPE (@0)) > 1
2490 && (wi::to_wide (@2)
2491 == wi::max_value (TYPE_PRECISION (TREE_TYPE (@0)), SIGNED) - 1))
2492 (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
2493 (icmp (convert:stype @0) { build_int_cst (stype, 0); })))))
2495 /* X / 4 < Y / 4 iff X < Y when the division is known to be exact. */
2496 (for cmp (simple_comparison)
2498 (cmp (convert?@3 (exact_div @0 INTEGER_CST@2)) (convert? (exact_div @1 @2)))
2499 (if (element_precision (@3) >= element_precision (@0)
2500 && types_match (@0, @1))
2501 (if (wi::lt_p (wi::to_wide (@2), 0, TYPE_SIGN (TREE_TYPE (@2))))
2502 (if (!TYPE_UNSIGNED (TREE_TYPE (@3)))
2504 (if (tree_expr_nonzero_p (@0) && tree_expr_nonzero_p (@1))
2507 tree utype = unsigned_type_for (TREE_TYPE (@0));
2509 (cmp (convert:utype @1) (convert:utype @0)))))
2510 (if (wi::gt_p (wi::to_wide (@2), 1, TYPE_SIGN (TREE_TYPE (@2))))
2511 (if (TYPE_UNSIGNED (TREE_TYPE (@0)) || !TYPE_UNSIGNED (TREE_TYPE (@3)))
2515 tree utype = unsigned_type_for (TREE_TYPE (@0));
2517 (cmp (convert:utype @0) (convert:utype @1)))))))))
2519 /* X / C1 op C2 into a simple range test. */
2520 (for cmp (simple_comparison)
2522 (cmp (trunc_div:s @0 INTEGER_CST@1) INTEGER_CST@2)
2523 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2524 && integer_nonzerop (@1)
2525 && !TREE_OVERFLOW (@1)
2526 && !TREE_OVERFLOW (@2))
2527 (with { tree lo, hi; bool neg_overflow;
2528 enum tree_code code = fold_div_compare (cmp, @1, @2, &lo, &hi,
2531 (if (code == LT_EXPR || code == GE_EXPR)
2532 (if (TREE_OVERFLOW (lo))
2533 { build_int_cst (type, (code == LT_EXPR) ^ neg_overflow); }
2534 (if (code == LT_EXPR)
2537 (if (code == LE_EXPR || code == GT_EXPR)
2538 (if (TREE_OVERFLOW (hi))
2539 { build_int_cst (type, (code == LE_EXPR) ^ neg_overflow); }
2540 (if (code == LE_EXPR)
2544 { build_int_cst (type, code == NE_EXPR); })
2545 (if (code == EQ_EXPR && !hi)
2547 (if (code == EQ_EXPR && !lo)
2549 (if (code == NE_EXPR && !hi)
2551 (if (code == NE_EXPR && !lo)
2554 { build_range_check (UNKNOWN_LOCATION, type, @0, code == EQ_EXPR,
2558 tree etype = range_check_type (TREE_TYPE (@0));
2561 hi = fold_convert (etype, hi);
2562 lo = fold_convert (etype, lo);
2563 hi = const_binop (MINUS_EXPR, etype, hi, lo);
2566 (if (etype && hi && !TREE_OVERFLOW (hi))
2567 (if (code == EQ_EXPR)
2568 (le (minus (convert:etype @0) { lo; }) { hi; })
2569 (gt (minus (convert:etype @0) { lo; }) { hi; })))))))))
2571 /* X + Z < Y + Z is the same as X < Y when there is no overflow. */
2572 (for op (lt le ge gt)
2574 (op (plus:c @0 @2) (plus:c @1 @2))
2575 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2576 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
2579 /* As a special case, X + C < Y + C is the same as (signed) X < (signed) Y
2580 when C is an unsigned integer constant with only the MSB set, and X and
2581 Y have types of equal or lower integer conversion rank than C's. */
2582 (for op (lt le ge gt)
2584 (op (plus @1 INTEGER_CST@0) (plus @2 @0))
2585 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2586 && TYPE_UNSIGNED (TREE_TYPE (@0))
2587 && wi::only_sign_bit_p (wi::to_wide (@0)))
2588 (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
2589 (op (convert:stype @1) (convert:stype @2))))))
2591 /* For equality and subtraction, this is also true with wrapping overflow. */
2592 (for op (eq ne minus)
2594 (op (plus:c @0 @2) (plus:c @1 @2))
2595 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2596 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2597 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
2600 /* X - Z < Y - Z is the same as X < Y when there is no overflow. */
2601 (for op (lt le ge gt)
2603 (op (minus @0 @2) (minus @1 @2))
2604 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2605 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
2607 /* For equality and subtraction, this is also true with wrapping overflow. */
2608 (for op (eq ne minus)
2610 (op (minus @0 @2) (minus @1 @2))
2611 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2612 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2613 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
2615 /* And for pointers... */
2616 (for op (simple_comparison)
2618 (op (pointer_diff@3 @0 @2) (pointer_diff @1 @2))
2619 (if (!TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
2622 (minus (pointer_diff@3 @0 @2) (pointer_diff @1 @2))
2623 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@3))
2624 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
2625 (pointer_diff @0 @1)))
2627 /* Z - X < Z - Y is the same as Y < X when there is no overflow. */
2628 (for op (lt le ge gt)
2630 (op (minus @2 @0) (minus @2 @1))
2631 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2632 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
2634 /* For equality and subtraction, this is also true with wrapping overflow. */
2635 (for op (eq ne minus)
2637 (op (minus @2 @0) (minus @2 @1))
2638 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2639 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2640 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
2642 /* And for pointers... */
2643 (for op (simple_comparison)
2645 (op (pointer_diff@3 @2 @0) (pointer_diff @2 @1))
2646 (if (!TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
2649 (minus (pointer_diff@3 @2 @0) (pointer_diff @2 @1))
2650 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@3))
2651 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
2652 (pointer_diff @1 @0)))
2654 /* X + Y < Y is the same as X < 0 when there is no overflow. */
2655 (for op (lt le gt ge)
2657 (op:c (plus:c@2 @0 @1) @1)
2658 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2659 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2660 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0))
2661 && (CONSTANT_CLASS_P (@0) || single_use (@2)))
2662 (op @0 { build_zero_cst (TREE_TYPE (@0)); }))))
2663 /* For equality, this is also true with wrapping overflow. */
2666 (op:c (nop_convert?@3 (plus:c@2 @0 (convert1? @1))) (convert2? @1))
2667 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2668 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2669 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
2670 && (CONSTANT_CLASS_P (@0) || (single_use (@2) && single_use (@3)))
2671 && tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@2))
2672 && tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@1)))
2673 (op @0 { build_zero_cst (TREE_TYPE (@0)); })))
2675 (op:c (nop_convert?@3 (pointer_plus@2 (convert1? @0) @1)) (convert2? @0))
2676 (if (tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@0))
2677 && tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0))
2678 && (CONSTANT_CLASS_P (@1) || (single_use (@2) && single_use (@3))))
2679 (op @1 { build_zero_cst (TREE_TYPE (@1)); }))))
2681 /* (&a + b) !=/== (&a[1] + c) -> (&a[0] - &a[1]) + b !=/== c */
2684 (neeq:c ADDR_EXPR@0 (pointer_plus @2 @3))
2685 (with { poly_int64 diff; tree inner_type = TREE_TYPE (@3);}
2686 (if (ptr_difference_const (@0, @2, &diff))
2687 (neeq { build_int_cst_type (inner_type, diff); } @3))))
2689 (neeq (pointer_plus ADDR_EXPR@0 @1) (pointer_plus ADDR_EXPR@2 @3))
2690 (with { poly_int64 diff; tree inner_type = TREE_TYPE (@1);}
2691 (if (ptr_difference_const (@0, @2, &diff))
2692 (neeq (plus { build_int_cst_type (inner_type, diff); } @1) @3)))))
2694 /* X - Y < X is the same as Y > 0 when there is no overflow.
2695 For equality, this is also true with wrapping overflow. */
2696 (for op (simple_comparison)
2698 (op:c @0 (minus@2 @0 @1))
2699 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2700 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2701 || ((op == EQ_EXPR || op == NE_EXPR)
2702 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
2703 && (CONSTANT_CLASS_P (@1) || single_use (@2)))
2704 (op @1 { build_zero_cst (TREE_TYPE (@1)); }))))
2707 (X / Y) == 0 -> X < Y if X, Y are unsigned.
2708 (X / Y) != 0 -> X >= Y, if X, Y are unsigned. */
2712 (cmp (trunc_div @0 @1) integer_zerop)
2713 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
2714 /* Complex ==/!= is allowed, but not </>=. */
2715 && TREE_CODE (TREE_TYPE (@0)) != COMPLEX_TYPE
2716 && (VECTOR_TYPE_P (type) || !VECTOR_TYPE_P (TREE_TYPE (@0))))
2719 /* X == C - X can never be true if C is odd. */
2722 (cmp:c (convert? @0) (convert1? (minus INTEGER_CST@1 (convert2? @0))))
2723 (if (TREE_INT_CST_LOW (@1) & 1)
2724 { constant_boolean_node (cmp == NE_EXPR, type); })))
2729 U needs to be non-negative.
2733 U and N needs to be non-negative
2737 U needs to be non-negative and N needs to be a negative constant.
2739 (for cmp (lt ge le gt )
2740 bitop (bit_ior bit_ior bit_and bit_and)
2742 (cmp:c (bitop:c tree_expr_nonnegative_p@0 @1) @0)
2743 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)))
2744 (if (bitop == BIT_AND_EXPR || tree_expr_nonnegative_p (@1))
2745 { constant_boolean_node (cmp == GE_EXPR || cmp == LE_EXPR, type); }
2746 /* The sign is opposite now so the comparison is swapped around. */
2747 (if (TREE_CODE (@1) == INTEGER_CST && wi::neg_p (wi::to_wide (@1)))
2748 { constant_boolean_node (cmp == LT_EXPR, type); })))))
2750 /* Arguments on which one can call get_nonzero_bits to get the bits
2752 (match with_possible_nonzero_bits
2754 (match with_possible_nonzero_bits
2756 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))))
2757 /* Slightly extended version, do not make it recursive to keep it cheap. */
2758 (match (with_possible_nonzero_bits2 @0)
2759 with_possible_nonzero_bits@0)
2760 (match (with_possible_nonzero_bits2 @0)
2761 (bit_and:c with_possible_nonzero_bits@0 @2))
2763 /* Same for bits that are known to be set, but we do not have
2764 an equivalent to get_nonzero_bits yet. */
2765 (match (with_certain_nonzero_bits2 @0)
2767 (match (with_certain_nonzero_bits2 @0)
2768 (bit_ior @1 INTEGER_CST@0))
2770 /* X == C (or X & Z == Y | C) is impossible if ~nonzero(X) & C != 0. */
2773 (cmp:c (with_possible_nonzero_bits2 @0) (with_certain_nonzero_bits2 @1))
2774 (if (wi::bit_and_not (wi::to_wide (@1), get_nonzero_bits (@0)) != 0)
2775 { constant_boolean_node (cmp == NE_EXPR, type); })))
2777 /* ((X inner_op C0) outer_op C1)
2778 With X being a tree where value_range has reasoned certain bits to always be
2779 zero throughout its computed value range,
2780 inner_op = {|,^}, outer_op = {|,^} and inner_op != outer_op
2781 where zero_mask has 1's for all bits that are sure to be 0 in
2783 if (inner_op == '^') C0 &= ~C1;
2784 if ((C0 & ~zero_mask) == 0) then emit (X outer_op (C0 outer_op C1)
2785 if ((C1 & ~zero_mask) == 0) then emit (X inner_op (C0 outer_op C1)
2787 (for inner_op (bit_ior bit_xor)
2788 outer_op (bit_xor bit_ior)
2791 (inner_op:s @2 INTEGER_CST@0) INTEGER_CST@1)
2795 wide_int zero_mask_not;
2799 if (TREE_CODE (@2) == SSA_NAME)
2800 zero_mask_not = get_nonzero_bits (@2);
2804 if (inner_op == BIT_XOR_EXPR)
2806 C0 = wi::bit_and_not (wi::to_wide (@0), wi::to_wide (@1));
2807 cst_emit = C0 | wi::to_wide (@1);
2811 C0 = wi::to_wide (@0);
2812 cst_emit = C0 ^ wi::to_wide (@1);
2815 (if (!fail && (C0 & zero_mask_not) == 0)
2816 (outer_op @2 { wide_int_to_tree (type, cst_emit); })
2817 (if (!fail && (wi::to_wide (@1) & zero_mask_not) == 0)
2818 (inner_op @2 { wide_int_to_tree (type, cst_emit); }))))))
2820 /* Associate (p +p off1) +p off2 as (p +p (off1 + off2)). */
2822 (pointer_plus (pointer_plus:s @0 @1) @3)
2823 (pointer_plus @0 (plus @1 @3)))
2826 (pointer_plus (convert:s (pointer_plus:s @0 @1)) @3)
2827 (convert:type (pointer_plus @0 (plus @1 @3))))
2834 tem4 = (unsigned long) tem3;
2839 (pointer_plus @0 (convert?@2 (minus@3 (convert @1) (convert @0))))
2840 /* Conditionally look through a sign-changing conversion. */
2841 (if (TYPE_PRECISION (TREE_TYPE (@2)) == TYPE_PRECISION (TREE_TYPE (@3))
2842 && ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@1)))
2843 || (GENERIC && type == TREE_TYPE (@1))))
2846 (pointer_plus @0 (convert?@2 (pointer_diff@3 @1 @@0)))
2847 (if (TYPE_PRECISION (TREE_TYPE (@2)) >= TYPE_PRECISION (TREE_TYPE (@3)))
2851 tem = (sizetype) ptr;
2855 and produce the simpler and easier to analyze with respect to alignment
2856 ... = ptr & ~algn; */
2858 (pointer_plus @0 (negate (bit_and (convert @0) INTEGER_CST@1)))
2859 (with { tree algn = wide_int_to_tree (TREE_TYPE (@0), ~wi::to_wide (@1)); }
2860 (bit_and @0 { algn; })))
2862 /* Try folding difference of addresses. */
2864 (minus (convert ADDR_EXPR@0) (convert (pointer_plus @1 @2)))
2865 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2866 (with { poly_int64 diff; }
2867 (if (ptr_difference_const (@0, @1, &diff))
2868 (minus { build_int_cst_type (type, diff); } (convert @2))))))
2870 (minus (convert (pointer_plus @0 @2)) (convert ADDR_EXPR@1))
2871 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2872 (with { poly_int64 diff; }
2873 (if (ptr_difference_const (@0, @1, &diff))
2874 (plus (convert @2) { build_int_cst_type (type, diff); })))))
2876 (minus (convert ADDR_EXPR@0) (convert @1))
2877 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2878 (with { poly_int64 diff; }
2879 (if (ptr_difference_const (@0, @1, &diff))
2880 { build_int_cst_type (type, diff); }))))
2882 (minus (convert @0) (convert ADDR_EXPR@1))
2883 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2884 (with { poly_int64 diff; }
2885 (if (ptr_difference_const (@0, @1, &diff))
2886 { build_int_cst_type (type, diff); }))))
2888 (pointer_diff (convert?@2 ADDR_EXPR@0) (convert1?@3 @1))
2889 (if (tree_nop_conversion_p (TREE_TYPE(@2), TREE_TYPE (@0))
2890 && tree_nop_conversion_p (TREE_TYPE(@3), TREE_TYPE (@1)))
2891 (with { poly_int64 diff; }
2892 (if (ptr_difference_const (@0, @1, &diff))
2893 { build_int_cst_type (type, diff); }))))
2895 (pointer_diff (convert?@2 @0) (convert1?@3 ADDR_EXPR@1))
2896 (if (tree_nop_conversion_p (TREE_TYPE(@2), TREE_TYPE (@0))
2897 && tree_nop_conversion_p (TREE_TYPE(@3), TREE_TYPE (@1)))
2898 (with { poly_int64 diff; }
2899 (if (ptr_difference_const (@0, @1, &diff))
2900 { build_int_cst_type (type, diff); }))))
2902 /* (&a+b) - (&a[1] + c) -> sizeof(a[0]) + (b - c) */
2904 (pointer_diff (pointer_plus ADDR_EXPR@0 @1) (pointer_plus ADDR_EXPR@2 @3))
2905 (with { poly_int64 diff; }
2906 (if (ptr_difference_const (@0, @2, &diff))
2907 (plus { build_int_cst_type (type, diff); } (convert (minus @1 @3))))))
2908 /* (p + b) - &p->d -> offsetof (*p, d) + b */
2910 (pointer_diff (pointer_plus @0 @1) ADDR_EXPR@2)
2911 (with { poly_int64 diff; }
2912 (if (ptr_difference_const (@0, @2, &diff))
2913 (plus { build_int_cst_type (type, diff); } (convert @1)))))
2915 (pointer_diff ADDR_EXPR@0 (pointer_plus @1 @2))
2916 (with { poly_int64 diff; }
2917 (if (ptr_difference_const (@0, @1, &diff))
2918 (minus { build_int_cst_type (type, diff); } (convert @2)))))
2920 /* Canonicalize (T *)(ptr - ptr-cst) to &MEM[ptr + -ptr-cst]. */
2922 (convert (pointer_diff @0 INTEGER_CST@1))
2923 (if (POINTER_TYPE_P (type))
2924 { build_fold_addr_expr_with_type
2925 (build2 (MEM_REF, char_type_node, @0,
2926 wide_int_to_tree (ptr_type_node, wi::neg (wi::to_wide (@1)))),
2929 /* If arg0 is derived from the address of an object or function, we may
2930 be able to fold this expression using the object or function's
2933 (bit_and (convert? @0) INTEGER_CST@1)
2934 (if (POINTER_TYPE_P (TREE_TYPE (@0))
2935 && tree_nop_conversion_p (type, TREE_TYPE (@0)))
2939 unsigned HOST_WIDE_INT bitpos;
2940 get_pointer_alignment_1 (@0, &align, &bitpos);
2942 (if (wi::ltu_p (wi::to_wide (@1), align / BITS_PER_UNIT))
2943 { wide_int_to_tree (type, (wi::to_wide (@1)
2944 & (bitpos / BITS_PER_UNIT))); }))))
2947 uniform_integer_cst_p
2949 tree int_cst = uniform_integer_cst_p (t);
2950 tree inner_type = TREE_TYPE (int_cst);
2952 (if ((INTEGRAL_TYPE_P (inner_type)
2953 || POINTER_TYPE_P (inner_type))
2954 && wi::eq_p (wi::to_wide (int_cst), wi::min_value (inner_type))))))
2957 uniform_integer_cst_p
2959 tree int_cst = uniform_integer_cst_p (t);
2960 tree itype = TREE_TYPE (int_cst);
2962 (if ((INTEGRAL_TYPE_P (itype)
2963 || POINTER_TYPE_P (itype))
2964 && wi::eq_p (wi::to_wide (int_cst), wi::max_value (itype))))))
2966 /* x > y && x != XXX_MIN --> x > y
2967 x > y && x == XXX_MIN --> false . */
2970 (bit_and:c (gt:c@2 @0 @1) (eqne @0 min_value))
2972 (if (eqne == EQ_EXPR)
2973 { constant_boolean_node (false, type); })
2974 (if (eqne == NE_EXPR)
2978 /* x < y && x != XXX_MAX --> x < y
2979 x < y && x == XXX_MAX --> false. */
2982 (bit_and:c (lt:c@2 @0 @1) (eqne @0 max_value))
2984 (if (eqne == EQ_EXPR)
2985 { constant_boolean_node (false, type); })
2986 (if (eqne == NE_EXPR)
2990 /* x <= y && x == XXX_MIN --> x == XXX_MIN. */
2992 (bit_and:c (le:c @0 @1) (eq@2 @0 min_value))
2995 /* x >= y && x == XXX_MAX --> x == XXX_MAX. */
2997 (bit_and:c (ge:c @0 @1) (eq@2 @0 max_value))
3000 /* x > y || x != XXX_MIN --> x != XXX_MIN. */
3002 (bit_ior:c (gt:c @0 @1) (ne@2 @0 min_value))
3005 /* x <= y || x != XXX_MIN --> true. */
3007 (bit_ior:c (le:c @0 @1) (ne @0 min_value))
3008 { constant_boolean_node (true, type); })
3010 /* x <= y || x == XXX_MIN --> x <= y. */
3012 (bit_ior:c (le:c@2 @0 @1) (eq @0 min_value))
3015 /* x < y || x != XXX_MAX --> x != XXX_MAX. */
3017 (bit_ior:c (lt:c @0 @1) (ne@2 @0 max_value))
3020 /* x >= y || x != XXX_MAX --> true
3021 x >= y || x == XXX_MAX --> x >= y. */
3024 (bit_ior:c (ge:c@2 @0 @1) (eqne @0 max_value))
3026 (if (eqne == EQ_EXPR)
3028 (if (eqne == NE_EXPR)
3029 { constant_boolean_node (true, type); }))))
3031 /* y == XXX_MIN || x < y --> x <= y - 1 */
3033 (bit_ior:c (eq:s @1 min_value) (lt:cs @0 @1))
3034 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
3035 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
3036 (le @0 (minus @1 { build_int_cst (TREE_TYPE (@1), 1); }))))
3038 /* y != XXX_MIN && x >= y --> x > y - 1 */
3040 (bit_and:c (ne:s @1 min_value) (ge:cs @0 @1))
3041 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
3042 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
3043 (gt @0 (minus @1 { build_int_cst (TREE_TYPE (@1), 1); }))))
3045 /* Convert (X == CST1) && ((other)X OP2 CST2) to a known value
3046 based on CST1 OP2 CST2. Similarly for (X != CST1). */
3047 /* Convert (X == Y) && (X OP2 Y) to a known value if X is an integral type.
3048 Similarly for (X != Y). */
3051 (for code2 (eq ne lt gt le ge)
3053 (bit_and:c (code1:c@3 @0 @1) (code2:c@4 (convert?@c0 @0) @2))
3054 (if ((TREE_CODE (@1) == INTEGER_CST
3055 && TREE_CODE (@2) == INTEGER_CST)
3056 || ((INTEGRAL_TYPE_P (TREE_TYPE (@1))
3057 || POINTER_TYPE_P (TREE_TYPE (@1)))
3058 && bitwise_equal_p (@1, @2)))
3061 bool one_before = false;
3062 bool one_after = false;
3064 bool allbits = true;
3065 if (TREE_CODE (@1) == INTEGER_CST
3066 && TREE_CODE (@2) == INTEGER_CST)
3068 allbits = TYPE_PRECISION (TREE_TYPE (@1)) <= TYPE_PRECISION (TREE_TYPE (@2));
3069 auto t1 = wi::to_wide (fold_convert (TREE_TYPE (@2), @1));
3070 auto t2 = wi::to_wide (@2);
3071 cmp = wi::cmp (t1, t2, TYPE_SIGN (TREE_TYPE (@2)));
3082 case EQ_EXPR: val = (cmp == 0); break;
3083 case NE_EXPR: val = (cmp != 0); break;
3084 case LT_EXPR: val = (cmp < 0); break;
3085 case GT_EXPR: val = (cmp > 0); break;
3086 case LE_EXPR: val = (cmp <= 0); break;
3087 case GE_EXPR: val = (cmp >= 0); break;
3088 default: gcc_unreachable ();
3092 (if (code1 == EQ_EXPR && val) @3)
3093 (if (code1 == EQ_EXPR && !val) { constant_boolean_node (false, type); })
3094 (if (code1 == NE_EXPR && !val && allbits) @4)
3095 (if (code1 == NE_EXPR
3099 (gt @c0 (convert @1)))
3100 (if (code1 == NE_EXPR
3104 (lt @c0 (convert @1)))
3105 /* (a != (b+1)) & (a > b) -> a > (b+1) */
3106 (if (code1 == NE_EXPR
3110 (gt @c0 (convert @1)))
3111 /* (a != (b-1)) & (a < b) -> a < (b-1) */
3112 (if (code1 == NE_EXPR
3116 (lt @c0 (convert @1)))
3124 /* Convert (X OP1 CST1) && (X OP2 CST2).
3125 Convert (X OP1 Y) && (X OP2 Y). */
3127 (for code1 (lt le gt ge)
3128 (for code2 (lt le gt ge)
3130 (bit_and (code1:c@3 @0 @1) (code2:c@4 @0 @2))
3131 (if ((TREE_CODE (@1) == INTEGER_CST
3132 && TREE_CODE (@2) == INTEGER_CST)
3133 || ((INTEGRAL_TYPE_P (TREE_TYPE (@1))
3134 || POINTER_TYPE_P (TREE_TYPE (@1)))
3135 && operand_equal_p (@1, @2)))
3139 if (TREE_CODE (@1) == INTEGER_CST
3140 && TREE_CODE (@2) == INTEGER_CST)
3141 cmp = tree_int_cst_compare (@1, @2);
3144 /* Choose the more restrictive of two < or <= comparisons. */
3145 (if ((code1 == LT_EXPR || code1 == LE_EXPR)
3146 && (code2 == LT_EXPR || code2 == LE_EXPR))
3147 (if ((cmp < 0) || (cmp == 0 && code1 == LT_EXPR))
3150 /* Likewise chose the more restrictive of two > or >= comparisons. */
3151 (if ((code1 == GT_EXPR || code1 == GE_EXPR)
3152 && (code2 == GT_EXPR || code2 == GE_EXPR))
3153 (if ((cmp > 0) || (cmp == 0 && code1 == GT_EXPR))
3156 /* Check for singleton ranges. */
3158 && ((code1 == LE_EXPR && code2 == GE_EXPR)
3159 || (code1 == GE_EXPR && code2 == LE_EXPR)))
3161 /* Check for disjoint ranges. */
3163 && (code1 == LT_EXPR || code1 == LE_EXPR)
3164 && (code2 == GT_EXPR || code2 == GE_EXPR))
3165 { constant_boolean_node (false, type); })
3167 && (code1 == GT_EXPR || code1 == GE_EXPR)
3168 && (code2 == LT_EXPR || code2 == LE_EXPR))
3169 { constant_boolean_node (false, type); })
3172 /* Convert (X == CST1) || (X OP2 CST2) to a known value
3173 based on CST1 OP2 CST2. Similarly for (X != CST1). */
3174 /* Convert (X == Y) || (X OP2 Y) to a known value if X is an integral type.
3175 Similarly for (X != Y). */
3178 (for code2 (eq ne lt gt le ge)
3180 (bit_ior:c (code1:c@3 @0 @1) (code2:c@4 (convert?@c0 @0) @2))
3181 (if ((TREE_CODE (@1) == INTEGER_CST
3182 && TREE_CODE (@2) == INTEGER_CST)
3183 || ((INTEGRAL_TYPE_P (TREE_TYPE (@1))
3184 || POINTER_TYPE_P (TREE_TYPE (@1)))
3185 && bitwise_equal_p (@1, @2)))
3188 bool one_before = false;
3189 bool one_after = false;
3191 bool allbits = true;
3192 if (TREE_CODE (@1) == INTEGER_CST
3193 && TREE_CODE (@2) == INTEGER_CST)
3195 allbits = TYPE_PRECISION (TREE_TYPE (@1)) <= TYPE_PRECISION (TREE_TYPE (@2));
3196 auto t1 = wi::to_wide (fold_convert (TREE_TYPE (@2), @1));
3197 auto t2 = wi::to_wide (@2);
3198 cmp = wi::cmp (t1, t2, TYPE_SIGN (TREE_TYPE (@2)));
3209 case EQ_EXPR: val = (cmp == 0); break;
3210 case NE_EXPR: val = (cmp != 0); break;
3211 case LT_EXPR: val = (cmp < 0); break;
3212 case GT_EXPR: val = (cmp > 0); break;
3213 case LE_EXPR: val = (cmp <= 0); break;
3214 case GE_EXPR: val = (cmp >= 0); break;
3215 default: gcc_unreachable ();
3219 (if (code1 == EQ_EXPR && val) @4)
3220 (if (code1 == NE_EXPR && val && allbits) { constant_boolean_node (true, type); })
3221 (if (code1 == NE_EXPR && !val && allbits) @3)
3222 (if (code1 == EQ_EXPR
3227 (if (code1 == EQ_EXPR
3232 /* (a == (b-1)) | (a >= b) -> a >= (b-1) */
3233 (if (code1 == EQ_EXPR
3237 (ge @c0 (convert @1)))
3238 /* (a == (b+1)) | (a <= b) -> a <= (b-1) */
3239 (if (code1 == EQ_EXPR
3243 (le @c0 (convert @1)))
3251 /* Convert (X OP1 CST1) || (X OP2 CST2).
3252 Convert (X OP1 Y) || (X OP2 Y). */
3254 (for code1 (lt le gt ge)
3255 (for code2 (lt le gt ge)
3257 (bit_ior (code1@3 @0 @1) (code2@4 @0 @2))
3258 (if ((TREE_CODE (@1) == INTEGER_CST
3259 && TREE_CODE (@2) == INTEGER_CST)
3260 || ((INTEGRAL_TYPE_P (TREE_TYPE (@1))
3261 || POINTER_TYPE_P (TREE_TYPE (@1)))
3262 && operand_equal_p (@1, @2)))
3266 if (TREE_CODE (@1) == INTEGER_CST
3267 && TREE_CODE (@2) == INTEGER_CST)
3268 cmp = tree_int_cst_compare (@1, @2);
3271 /* Choose the more restrictive of two < or <= comparisons. */
3272 (if ((code1 == LT_EXPR || code1 == LE_EXPR)
3273 && (code2 == LT_EXPR || code2 == LE_EXPR))
3274 (if ((cmp < 0) || (cmp == 0 && code1 == LT_EXPR))
3277 /* Likewise chose the more restrictive of two > or >= comparisons. */
3278 (if ((code1 == GT_EXPR || code1 == GE_EXPR)
3279 && (code2 == GT_EXPR || code2 == GE_EXPR))
3280 (if ((cmp > 0) || (cmp == 0 && code1 == GT_EXPR))
3283 /* Check for singleton ranges. */
3285 && ((code1 == LT_EXPR && code2 == GT_EXPR)
3286 || (code1 == GT_EXPR && code2 == LT_EXPR)))
3288 /* Check for disjoint ranges. */
3290 && (code1 == LT_EXPR || code1 == LE_EXPR)
3291 && (code2 == GT_EXPR || code2 == GE_EXPR))
3292 { constant_boolean_node (true, type); })
3294 && (code1 == GT_EXPR || code1 == GE_EXPR)
3295 && (code2 == LT_EXPR || code2 == LE_EXPR))
3296 { constant_boolean_node (true, type); })
3299 /* Optimize (a CMP b) ^ (a CMP b) */
3300 /* Optimize (a CMP b) != (a CMP b) */
3301 (for op (bit_xor ne)
3302 (for cmp1 (lt lt lt le le le)
3303 cmp2 (gt eq ne ge eq ne)
3304 rcmp (ne le gt ne lt ge)
3306 (op:c (cmp1:c @0 @1) (cmp2:c @0 @1))
3307 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
3310 /* Optimize (a CMP b) == (a CMP b) */
3311 (for cmp1 (lt lt lt le le le)
3312 cmp2 (gt eq ne ge eq ne)
3313 rcmp (eq gt le eq ge lt)
3315 (eq:c (cmp1:c @0 @1) (cmp2:c @0 @1))
3316 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
3319 /* We can't reassociate at all for saturating types. */
3320 (if (!TYPE_SATURATING (type))
3322 /* Contract negates. */
3323 /* A + (-B) -> A - B */
3325 (plus:c @0 (convert? (negate @1)))
3326 /* Apply STRIP_NOPS on the negate. */
3327 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
3328 && !TYPE_OVERFLOW_SANITIZED (type))
3332 if (INTEGRAL_TYPE_P (type)
3333 && TYPE_OVERFLOW_WRAPS (type) != TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
3334 t1 = TYPE_OVERFLOW_WRAPS (type) ? type : TREE_TYPE (@1);
3336 (convert (minus (convert:t1 @0) (convert:t1 @1))))))
3337 /* A - (-B) -> A + B */
3339 (minus @0 (convert? (negate @1)))
3340 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
3341 && !TYPE_OVERFLOW_SANITIZED (type))
3345 if (INTEGRAL_TYPE_P (type)
3346 && TYPE_OVERFLOW_WRAPS (type) != TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
3347 t1 = TYPE_OVERFLOW_WRAPS (type) ? type : TREE_TYPE (@1);
3349 (convert (plus (convert:t1 @0) (convert:t1 @1))))))
3351 Sign-extension is ok except for INT_MIN, which thankfully cannot
3352 happen without overflow. */
3354 (negate (convert (negate @1)))
3355 (if (INTEGRAL_TYPE_P (type)
3356 && (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@1))
3357 || (!TYPE_UNSIGNED (TREE_TYPE (@1))
3358 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))))
3359 && !TYPE_OVERFLOW_SANITIZED (type)
3360 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@1)))
3363 (negate (convert negate_expr_p@1))
3364 (if (SCALAR_FLOAT_TYPE_P (type)
3365 && ((DECIMAL_FLOAT_TYPE_P (type)
3366 == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1))
3367 && TYPE_PRECISION (type) >= TYPE_PRECISION (TREE_TYPE (@1)))
3368 || !HONOR_SIGN_DEPENDENT_ROUNDING (type)))
3369 (convert (negate @1))))
3371 (negate (nop_convert? (negate @1)))
3372 (if (!TYPE_OVERFLOW_SANITIZED (type)
3373 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@1)))
3376 /* We can't reassociate floating-point unless -fassociative-math
3377 or fixed-point plus or minus because of saturation to +-Inf. */
3378 (if ((!FLOAT_TYPE_P (type) || flag_associative_math)
3379 && !FIXED_POINT_TYPE_P (type))
3381 /* Match patterns that allow contracting a plus-minus pair
3382 irrespective of overflow issues. */
3383 /* (A +- B) - A -> +- B */
3384 /* (A +- B) -+ B -> A */
3385 /* A - (A +- B) -> -+ B */
3386 /* A +- (B -+ A) -> +- B */
3388 (minus (nop_convert1? (plus:c (nop_convert2? @0) @1)) @0)
3391 (minus (nop_convert1? (minus (nop_convert2? @0) @1)) @0)
3392 (if (!ANY_INTEGRAL_TYPE_P (type)
3393 || TYPE_OVERFLOW_WRAPS (type))
3394 (negate (view_convert @1))
3395 (view_convert (negate @1))))
3397 (plus:c (nop_convert1? (minus @0 (nop_convert2? @1))) @1)
3400 (minus @0 (nop_convert1? (plus:c (nop_convert2? @0) @1)))
3401 (if (!ANY_INTEGRAL_TYPE_P (type)
3402 || TYPE_OVERFLOW_WRAPS (type))
3403 (negate (view_convert @1))
3404 (view_convert (negate @1))))
3406 (minus @0 (nop_convert1? (minus (nop_convert2? @0) @1)))
3408 /* (A +- B) + (C - A) -> C +- B */
3409 /* (A + B) - (A - C) -> B + C */
3410 /* More cases are handled with comparisons. */
3412 (plus:c (plus:c @0 @1) (minus @2 @0))
3415 (plus:c (minus @0 @1) (minus @2 @0))
3418 (plus:c (pointer_diff @0 @1) (pointer_diff @2 @0))
3419 (if (TYPE_OVERFLOW_UNDEFINED (type)
3420 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0)))
3421 (pointer_diff @2 @1)))
3423 (minus (plus:c @0 @1) (minus @0 @2))
3426 /* (A +- CST1) +- CST2 -> A + CST3
3427 Use view_convert because it is safe for vectors and equivalent for
3429 (for outer_op (plus minus)
3430 (for inner_op (plus minus)
3431 neg_inner_op (minus plus)
3433 (outer_op (nop_convert? (inner_op @0 CONSTANT_CLASS_P@1))
3435 /* If one of the types wraps, use that one. */
3436 (if (!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_WRAPS (type))
3437 /* If all 3 captures are CONSTANT_CLASS_P, punt, as we might recurse
3438 forever if something doesn't simplify into a constant. */
3439 (if (!CONSTANT_CLASS_P (@0))
3440 (if (outer_op == PLUS_EXPR)
3441 (plus (view_convert @0) (inner_op! @2 (view_convert @1)))
3442 (minus (view_convert @0) (neg_inner_op! @2 (view_convert @1)))))
3443 (if (!ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3444 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
3445 (if (outer_op == PLUS_EXPR)
3446 (view_convert (plus @0 (inner_op! (view_convert @2) @1)))
3447 (view_convert (minus @0 (neg_inner_op! (view_convert @2) @1))))
3448 /* If the constant operation overflows we cannot do the transform
3449 directly as we would introduce undefined overflow, for example
3450 with (a - 1) + INT_MIN. */
3451 (if (types_match (type, @0) && !TYPE_OVERFLOW_SANITIZED (type))
3452 (with { tree cst = const_binop (outer_op == inner_op
3453 ? PLUS_EXPR : MINUS_EXPR,
3456 (if (INTEGRAL_TYPE_P (type) && !TREE_OVERFLOW (cst))
3457 (inner_op @0 { cst; } )
3458 /* X+INT_MAX+1 is X-INT_MIN. */
3459 (if (INTEGRAL_TYPE_P (type)
3460 && wi::to_wide (cst) == wi::min_value (type))
3461 (neg_inner_op @0 { wide_int_to_tree (type, wi::to_wide (cst)); })
3462 /* Last resort, use some unsigned type. */
3463 (with { tree utype = unsigned_type_for (type); }
3465 (view_convert (inner_op
3466 (view_convert:utype @0)
3468 { TREE_OVERFLOW (cst)
3469 ? drop_tree_overflow (cst) : cst; })))))))))))))))
3471 /* (CST1 - A) +- CST2 -> CST3 - A */
3472 (for outer_op (plus minus)
3474 (outer_op (nop_convert? (minus CONSTANT_CLASS_P@1 @0)) CONSTANT_CLASS_P@2)
3475 /* If one of the types wraps, use that one. */
3476 (if (!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_WRAPS (type))
3477 /* If all 3 captures are CONSTANT_CLASS_P, punt, as we might recurse
3478 forever if something doesn't simplify into a constant. */
3479 (if (!CONSTANT_CLASS_P (@0))
3480 (minus (outer_op! (view_convert @1) @2) (view_convert @0)))
3481 (if (!ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3482 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
3483 (view_convert (minus (outer_op! @1 (view_convert @2)) @0))
3484 (if (types_match (type, @0) && !TYPE_OVERFLOW_SANITIZED (type))
3485 (with { tree cst = const_binop (outer_op, type, @1, @2); }
3486 (if (cst && !TREE_OVERFLOW (cst))
3487 (minus { cst; } @0))))))))
3489 /* CST1 - (CST2 - A) -> CST3 + A
3490 Use view_convert because it is safe for vectors and equivalent for
3493 (minus CONSTANT_CLASS_P@1 (nop_convert? (minus CONSTANT_CLASS_P@2 @0)))
3494 /* If one of the types wraps, use that one. */
3495 (if (!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_WRAPS (type))
3496 /* If all 3 captures are CONSTANT_CLASS_P, punt, as we might recurse
3497 forever if something doesn't simplify into a constant. */
3498 (if (!CONSTANT_CLASS_P (@0))
3499 (plus (view_convert @0) (minus! @1 (view_convert @2))))
3500 (if (!ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3501 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
3502 (view_convert (plus @0 (minus! (view_convert @1) @2)))
3503 (if (types_match (type, @0) && !TYPE_OVERFLOW_SANITIZED (type))
3504 (with { tree cst = const_binop (MINUS_EXPR, type, @1, @2); }
3505 (if (cst && !TREE_OVERFLOW (cst))
3506 (plus { cst; } @0)))))))
3508 /* ((T)(A)) + CST -> (T)(A + CST) */
3511 (plus (convert:s SSA_NAME@0) INTEGER_CST@1)
3512 (if (TREE_CODE (TREE_TYPE (@0)) == INTEGER_TYPE
3513 && TREE_CODE (type) == INTEGER_TYPE
3514 && TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (@0))
3515 && int_fits_type_p (@1, TREE_TYPE (@0)))
3516 /* Perform binary operation inside the cast if the constant fits
3517 and (A + CST)'s range does not overflow. */
3520 wi::overflow_type min_ovf = wi::OVF_OVERFLOW,
3521 max_ovf = wi::OVF_OVERFLOW;
3522 tree inner_type = TREE_TYPE (@0);
3525 = wide_int::from (wi::to_wide (@1), TYPE_PRECISION (inner_type),
3526 TYPE_SIGN (inner_type));
3529 if (get_global_range_query ()->range_of_expr (vr, @0)
3530 && !vr.varying_p () && !vr.undefined_p ())
3532 wide_int wmin0 = vr.lower_bound ();
3533 wide_int wmax0 = vr.upper_bound ();
3534 wi::add (wmin0, w1, TYPE_SIGN (inner_type), &min_ovf);
3535 wi::add (wmax0, w1, TYPE_SIGN (inner_type), &max_ovf);
3538 (if (min_ovf == wi::OVF_NONE && max_ovf == wi::OVF_NONE)
3539 (convert (plus @0 { wide_int_to_tree (TREE_TYPE (@0), w1); } )))
3543 /* ((T)(A + CST1)) + CST2 -> (T)(A) + (T)CST1 + CST2 */
3545 (for op (plus minus)
3547 (plus (convert:s (op:s @0 INTEGER_CST@1)) INTEGER_CST@2)
3548 (if (TREE_CODE (TREE_TYPE (@0)) == INTEGER_TYPE
3549 && TREE_CODE (type) == INTEGER_TYPE
3550 && TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (@0))
3551 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
3552 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0))
3553 && TYPE_OVERFLOW_WRAPS (type))
3554 (plus (convert @0) (op @2 (convert @1))))))
3557 /* (T)(A) +- (T)(B) -> (T)(A +- B) only when (A +- B) could be simplified
3558 to a simple value. */
3559 (for op (plus minus)
3561 (op (convert @0) (convert @1))
3562 (if (INTEGRAL_TYPE_P (type)
3563 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
3564 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))
3565 && types_match (TREE_TYPE (@0), TREE_TYPE (@1))
3566 && !TYPE_OVERFLOW_TRAPS (type)
3567 && !TYPE_OVERFLOW_SANITIZED (type))
3568 (convert (op! @0 @1)))))
3572 (plus:c (convert? (bit_not @0)) (convert? @0))
3573 (if (!TYPE_OVERFLOW_TRAPS (type))
3574 (convert { build_all_ones_cst (TREE_TYPE (@0)); })))
3578 (plus (convert? (bit_not @0)) integer_each_onep)
3579 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
3580 (negate (convert @0))))
3584 (minus (convert? (negate @0)) integer_each_onep)
3585 (if (!TYPE_OVERFLOW_TRAPS (type)
3586 && TREE_CODE (type) != COMPLEX_TYPE
3587 && tree_nop_conversion_p (type, TREE_TYPE (@0)))
3588 (bit_not (convert @0))))
3592 (minus integer_all_onesp @0)
3593 (if (TREE_CODE (type) != COMPLEX_TYPE)
3596 /* (T)(P + A) - (T)P -> (T) A */
3598 (minus (convert (plus:c @@0 @1))
3600 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
3601 /* For integer types, if A has a smaller type
3602 than T the result depends on the possible
3604 E.g. T=size_t, A=(unsigned)429497295, P>0.
3605 However, if an overflow in P + A would cause
3606 undefined behavior, we can assume that there
3608 || (INTEGRAL_TYPE_P (TREE_TYPE (@1))
3609 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))))
3612 (minus (convert (pointer_plus @@0 @1))
3614 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
3615 /* For pointer types, if the conversion of A to the
3616 final type requires a sign- or zero-extension,
3617 then we have to punt - it is not defined which
3619 || (POINTER_TYPE_P (TREE_TYPE (@0))
3620 && TREE_CODE (@1) == INTEGER_CST
3621 && tree_int_cst_sign_bit (@1) == 0))
3624 (pointer_diff (pointer_plus @@0 @1) @0)
3625 /* The second argument of pointer_plus must be interpreted as signed, and
3626 thus sign-extended if necessary. */
3627 (with { tree stype = signed_type_for (TREE_TYPE (@1)); }
3628 /* Use view_convert instead of convert here, as POINTER_PLUS_EXPR
3629 second arg is unsigned even when we need to consider it as signed,
3630 we don't want to diagnose overflow here. */
3631 (convert (view_convert:stype @1))))
3633 /* (T)P - (T)(P + A) -> -(T) A */
3635 (minus (convert? @0)
3636 (convert (plus:c @@0 @1)))
3637 (if (INTEGRAL_TYPE_P (type)
3638 && TYPE_OVERFLOW_UNDEFINED (type)
3639 /* For integer literals, using an intermediate unsigned type to avoid
3640 an overflow at run time is counter-productive because it introduces
3641 spurious overflows at compile time, in the form of TREE_OVERFLOW on
3642 the result, which may be problematic in GENERIC for some front-ends:
3643 (T)P - (T)(P + 4) -> (T)(-(U)4) -> (T)(4294967292) -> -4(OVF)
3644 so we use the direct path for them. */
3645 && TREE_CODE (@1) != INTEGER_CST
3646 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
3647 (with { tree utype = unsigned_type_for (type); }
3648 (convert (negate (convert:utype @1))))
3649 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
3650 /* For integer types, if A has a smaller type
3651 than T the result depends on the possible
3653 E.g. T=size_t, A=(unsigned)429497295, P>0.
3654 However, if an overflow in P + A would cause
3655 undefined behavior, we can assume that there
3657 || (INTEGRAL_TYPE_P (TREE_TYPE (@1))
3658 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))))
3659 (negate (convert @1)))))
3662 (convert (pointer_plus @@0 @1)))
3663 (if (INTEGRAL_TYPE_P (type)
3664 && TYPE_OVERFLOW_UNDEFINED (type)
3665 /* See above the rationale for this condition. */
3666 && TREE_CODE (@1) != INTEGER_CST
3667 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
3668 (with { tree utype = unsigned_type_for (type); }
3669 (convert (negate (convert:utype @1))))
3670 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
3671 /* For pointer types, if the conversion of A to the
3672 final type requires a sign- or zero-extension,
3673 then we have to punt - it is not defined which
3675 || (POINTER_TYPE_P (TREE_TYPE (@0))
3676 && TREE_CODE (@1) == INTEGER_CST
3677 && tree_int_cst_sign_bit (@1) == 0))
3678 (negate (convert @1)))))
3680 (pointer_diff @0 (pointer_plus @@0 @1))
3681 /* The second argument of pointer_plus must be interpreted as signed, and
3682 thus sign-extended if necessary. */
3683 (with { tree stype = signed_type_for (TREE_TYPE (@1)); }
3684 /* Use view_convert instead of convert here, as POINTER_PLUS_EXPR
3685 second arg is unsigned even when we need to consider it as signed,
3686 we don't want to diagnose overflow here. */
3687 (negate (convert (view_convert:stype @1)))))
3689 /* (T)(P + A) - (T)(P + B) -> (T)A - (T)B */
3691 (minus (convert (plus:c @@0 @1))
3692 (convert (plus:c @0 @2)))
3693 (if (INTEGRAL_TYPE_P (type)
3694 && TYPE_OVERFLOW_UNDEFINED (type)
3695 && element_precision (type) <= element_precision (TREE_TYPE (@1))
3696 && element_precision (type) <= element_precision (TREE_TYPE (@2)))
3697 (with { tree utype = unsigned_type_for (type); }
3698 (convert (minus (convert:utype @1) (convert:utype @2))))
3699 (if (((element_precision (type) <= element_precision (TREE_TYPE (@1)))
3700 == (element_precision (type) <= element_precision (TREE_TYPE (@2))))
3701 && (element_precision (type) <= element_precision (TREE_TYPE (@1))
3702 /* For integer types, if A has a smaller type
3703 than T the result depends on the possible
3705 E.g. T=size_t, A=(unsigned)429497295, P>0.
3706 However, if an overflow in P + A would cause
3707 undefined behavior, we can assume that there
3709 || (INTEGRAL_TYPE_P (TREE_TYPE (@1))
3710 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
3711 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))
3712 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@2)))))
3713 (minus (convert @1) (convert @2)))))
3715 (minus (convert (pointer_plus @@0 @1))
3716 (convert (pointer_plus @0 @2)))
3717 (if (INTEGRAL_TYPE_P (type)
3718 && TYPE_OVERFLOW_UNDEFINED (type)
3719 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
3720 (with { tree utype = unsigned_type_for (type); }
3721 (convert (minus (convert:utype @1) (convert:utype @2))))
3722 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
3723 /* For pointer types, if the conversion of A to the
3724 final type requires a sign- or zero-extension,
3725 then we have to punt - it is not defined which
3727 || (POINTER_TYPE_P (TREE_TYPE (@0))
3728 && TREE_CODE (@1) == INTEGER_CST
3729 && tree_int_cst_sign_bit (@1) == 0
3730 && TREE_CODE (@2) == INTEGER_CST
3731 && tree_int_cst_sign_bit (@2) == 0))
3732 (minus (convert @1) (convert @2)))))
3734 (pointer_diff (pointer_plus @0 @2) (pointer_plus @1 @2))
3735 (pointer_diff @0 @1))
3737 (pointer_diff (pointer_plus @@0 @1) (pointer_plus @0 @2))
3738 /* The second argument of pointer_plus must be interpreted as signed, and
3739 thus sign-extended if necessary. */
3740 (with { tree stype = signed_type_for (TREE_TYPE (@1)); }
3741 /* Use view_convert instead of convert here, as POINTER_PLUS_EXPR
3742 second arg is unsigned even when we need to consider it as signed,
3743 we don't want to diagnose overflow here. */
3744 (minus (convert (view_convert:stype @1))
3745 (convert (view_convert:stype @2)))))))
3747 /* (A * C) +- (B * C) -> (A+-B) * C and (A * C) +- A -> A * (C+-1).
3748 Modeled after fold_plusminus_mult_expr. */
3749 (if (!TYPE_SATURATING (type)
3750 && (!FLOAT_TYPE_P (type) || flag_associative_math))
3751 (for plusminus (plus minus)
3753 (plusminus (mult:cs@3 @0 @1) (mult:cs@4 @0 @2))
3754 (if (!ANY_INTEGRAL_TYPE_P (type)
3755 || TYPE_OVERFLOW_WRAPS (type)
3756 || (INTEGRAL_TYPE_P (type)
3757 && tree_expr_nonzero_p (@0)
3758 && expr_not_equal_to (@0, wi::minus_one (TYPE_PRECISION (type)))))
3759 (if (single_use (@3) || single_use (@4))
3760 /* If @1 +- @2 is constant require a hard single-use on either
3761 original operand (but not on both). */
3762 (mult (plusminus @1 @2) @0)
3763 (mult! (plusminus @1 @2) @0)
3765 /* We cannot generate constant 1 for fract. */
3766 (if (!ALL_FRACT_MODE_P (TYPE_MODE (type)))
3768 (plusminus @0 (mult:c@3 @0 @2))
3769 (if ((!ANY_INTEGRAL_TYPE_P (type)
3770 || TYPE_OVERFLOW_WRAPS (type)
3771 /* For @0 + @0*@2 this transformation would introduce UB
3772 (where there was none before) for @0 in [-1,0] and @2 max.
3773 For @0 - @0*@2 this transformation would introduce UB
3774 for @0 0 and @2 in [min,min+1] or @0 -1 and @2 min+1. */
3775 || (INTEGRAL_TYPE_P (type)
3776 && ((tree_expr_nonzero_p (@0)
3777 && expr_not_equal_to (@0,
3778 wi::minus_one (TYPE_PRECISION (type))))
3779 || (plusminus == PLUS_EXPR
3780 ? expr_not_equal_to (@2,
3781 wi::max_value (TYPE_PRECISION (type), SIGNED))
3782 /* Let's ignore the @0 -1 and @2 min case. */
3783 : (expr_not_equal_to (@2,
3784 wi::min_value (TYPE_PRECISION (type), SIGNED))
3785 && expr_not_equal_to (@2,
3786 wi::min_value (TYPE_PRECISION (type), SIGNED)
3789 (mult (plusminus { build_one_cst (type); } @2) @0)))
3791 (plusminus (mult:c@3 @0 @2) @0)
3792 (if ((!ANY_INTEGRAL_TYPE_P (type)
3793 || TYPE_OVERFLOW_WRAPS (type)
3794 /* For @0*@2 + @0 this transformation would introduce UB
3795 (where there was none before) for @0 in [-1,0] and @2 max.
3796 For @0*@2 - @0 this transformation would introduce UB
3797 for @0 0 and @2 min. */
3798 || (INTEGRAL_TYPE_P (type)
3799 && ((tree_expr_nonzero_p (@0)
3800 && (plusminus == MINUS_EXPR
3801 || expr_not_equal_to (@0,
3802 wi::minus_one (TYPE_PRECISION (type)))))
3803 || expr_not_equal_to (@2,
3804 (plusminus == PLUS_EXPR
3805 ? wi::max_value (TYPE_PRECISION (type), SIGNED)
3806 : wi::min_value (TYPE_PRECISION (type), SIGNED))))))
3808 (mult (plusminus @2 { build_one_cst (type); }) @0))))))
3811 /* Canonicalize X + (X << C) into X * (1 + (1 << C)) and
3812 (X << C1) + (X << C2) into X * ((1 << C1) + (1 << C2)). */
3814 (plus:c @0 (lshift:s @0 INTEGER_CST@1))
3815 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3816 && tree_fits_uhwi_p (@1)
3817 && tree_to_uhwi (@1) < element_precision (type)
3818 && (INTEGRAL_TYPE_P (TREE_TYPE (@0))
3819 || optab_handler (smul_optab,
3820 TYPE_MODE (type)) != CODE_FOR_nothing))
3821 (with { tree t = type;
3822 if (!TYPE_OVERFLOW_WRAPS (t)) t = unsigned_type_for (t);
3823 wide_int w = wi::set_bit_in_zero (tree_to_uhwi (@1),
3824 element_precision (type));
3826 tree cst = wide_int_to_tree (VECTOR_TYPE_P (t) ? TREE_TYPE (t)
3828 cst = build_uniform_cst (t, cst); }
3829 (convert (mult (convert:t @0) { cst; })))))
3831 (plus (lshift:s @0 INTEGER_CST@1) (lshift:s @0 INTEGER_CST@2))
3832 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3833 && tree_fits_uhwi_p (@1)
3834 && tree_to_uhwi (@1) < element_precision (type)
3835 && tree_fits_uhwi_p (@2)
3836 && tree_to_uhwi (@2) < element_precision (type)
3837 && (INTEGRAL_TYPE_P (TREE_TYPE (@0))
3838 || optab_handler (smul_optab,
3839 TYPE_MODE (type)) != CODE_FOR_nothing))
3840 (with { tree t = type;
3841 if (!TYPE_OVERFLOW_WRAPS (t)) t = unsigned_type_for (t);
3842 unsigned int prec = element_precision (type);
3843 wide_int w = wi::set_bit_in_zero (tree_to_uhwi (@1), prec);
3844 w += wi::set_bit_in_zero (tree_to_uhwi (@2), prec);
3845 tree cst = wide_int_to_tree (VECTOR_TYPE_P (t) ? TREE_TYPE (t)
3847 cst = build_uniform_cst (t, cst); }
3848 (convert (mult (convert:t @0) { cst; })))))
3851 /* Canonicalize (X*C1)|(X*C2) and (X*C1)^(X*C2) to (C1+C2)*X when
3852 tree_nonzero_bits allows IOR and XOR to be treated like PLUS.
3853 Likewise, handle (X<<C3) and X as legitimate variants of X*C. */
3854 (for op (bit_ior bit_xor)
3856 (op (mult:s@0 @1 INTEGER_CST@2)
3857 (mult:s@3 @1 INTEGER_CST@4))
3858 (if (INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_WRAPS (type)
3859 && (tree_nonzero_bits (@0) & tree_nonzero_bits (@3)) == 0)
3861 { wide_int_to_tree (type, wi::to_wide (@2) + wi::to_wide (@4)); })))
3863 (op:c (mult:s@0 @1 INTEGER_CST@2)
3864 (lshift:s@3 @1 INTEGER_CST@4))
3865 (if (INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_WRAPS (type)
3866 && tree_int_cst_sgn (@4) > 0
3867 && (tree_nonzero_bits (@0) & tree_nonzero_bits (@3)) == 0)
3868 (with { wide_int wone = wi::one (TYPE_PRECISION (type));
3869 wide_int c = wi::add (wi::to_wide (@2),
3870 wi::lshift (wone, wi::to_wide (@4))); }
3871 (mult @1 { wide_int_to_tree (type, c); }))))
3873 (op:c (mult:s@0 @1 INTEGER_CST@2)
3875 (if (INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_WRAPS (type)
3876 && (tree_nonzero_bits (@0) & tree_nonzero_bits (@1)) == 0)
3878 { wide_int_to_tree (type,
3879 wi::add (wi::to_wide (@2), 1)); })))
3881 (op (lshift:s@0 @1 INTEGER_CST@2)
3882 (lshift:s@3 @1 INTEGER_CST@4))
3883 (if (INTEGRAL_TYPE_P (type)
3884 && tree_int_cst_sgn (@2) > 0
3885 && tree_int_cst_sgn (@4) > 0
3886 && (tree_nonzero_bits (@0) & tree_nonzero_bits (@3)) == 0)
3887 (with { tree t = type;
3888 if (!TYPE_OVERFLOW_WRAPS (t))
3889 t = unsigned_type_for (t);
3890 wide_int wone = wi::one (TYPE_PRECISION (t));
3891 wide_int c = wi::add (wi::lshift (wone, wi::to_wide (@2)),
3892 wi::lshift (wone, wi::to_wide (@4))); }
3893 (convert (mult:t (convert:t @1) { wide_int_to_tree (t,c); })))))
3895 (op:c (lshift:s@0 @1 INTEGER_CST@2)
3897 (if (INTEGRAL_TYPE_P (type)
3898 && tree_int_cst_sgn (@2) > 0
3899 && (tree_nonzero_bits (@0) & tree_nonzero_bits (@1)) == 0)
3900 (with { tree t = type;
3901 if (!TYPE_OVERFLOW_WRAPS (t))
3902 t = unsigned_type_for (t);
3903 wide_int wone = wi::one (TYPE_PRECISION (t));
3904 wide_int c = wi::add (wi::lshift (wone, wi::to_wide (@2)), wone); }
3905 (convert (mult:t (convert:t @1) { wide_int_to_tree (t, c); }))))))
3907 /* Simplifications of MIN_EXPR, MAX_EXPR, fmin() and fmax(). */
3909 (for minmax (min max)
3913 /* max(max(x,y),x) -> max(x,y) */
3915 (minmax:c (minmax:c@2 @0 @1) @0)
3917 /* For fmin() and fmax(), skip folding when both are sNaN. */
3918 (for minmax (FMIN_ALL FMAX_ALL)
3921 (if (!tree_expr_maybe_signaling_nan_p (@0))
3923 /* min(max(x,y),y) -> y. */
3925 (min:c (max:c @0 @1) @1)
3927 /* max(min(x,y),y) -> y. */
3929 (max:c (min:c @0 @1) @1)
3931 /* max(a,-a) -> abs(a). */
3933 (max:c @0 (negate @0))
3934 (if (TREE_CODE (type) != COMPLEX_TYPE
3935 && (! ANY_INTEGRAL_TYPE_P (type)
3936 || TYPE_OVERFLOW_UNDEFINED (type)))
3938 /* min(a,-a) -> -abs(a). */
3940 (min:c @0 (negate @0))
3941 (if (TREE_CODE (type) != COMPLEX_TYPE
3942 && (! ANY_INTEGRAL_TYPE_P (type)
3943 || TYPE_OVERFLOW_UNDEFINED (type)))
3948 (if (INTEGRAL_TYPE_P (type)
3949 && TYPE_MIN_VALUE (type)
3950 && operand_equal_p (@1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
3952 (if (INTEGRAL_TYPE_P (type)
3953 && TYPE_MAX_VALUE (type)
3954 && operand_equal_p (@1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
3959 (if (INTEGRAL_TYPE_P (type)
3960 && TYPE_MAX_VALUE (type)
3961 && operand_equal_p (@1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
3963 (if (INTEGRAL_TYPE_P (type)
3964 && TYPE_MIN_VALUE (type)
3965 && operand_equal_p (@1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
3968 /* max (a, a + CST) -> a + CST where CST is positive. */
3969 /* max (a, a + CST) -> a where CST is negative. */
3971 (max:c @0 (plus@2 @0 INTEGER_CST@1))
3972 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
3973 (if (tree_int_cst_sgn (@1) > 0)
3977 /* min (a, a + CST) -> a where CST is positive. */
3978 /* min (a, a + CST) -> a + CST where CST is negative. */
3980 (min:c @0 (plus@2 @0 INTEGER_CST@1))
3981 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
3982 (if (tree_int_cst_sgn (@1) > 0)
3986 /* Simplify min (&var[off0], &var[off1]) etc. depending on whether
3987 the addresses are known to be less, equal or greater. */
3988 (for minmax (min max)
3991 (minmax (convert1?@2 addr@0) (convert2?@3 addr@1))
3994 poly_int64 off0, off1;
3996 int equal = address_compare (cmp, TREE_TYPE (@2), @0, @1, base0, base1,
3997 off0, off1, GENERIC);
4000 (if (minmax == MIN_EXPR)
4001 (if (known_le (off0, off1))
4003 (if (known_gt (off0, off1))
4005 (if (known_ge (off0, off1))
4007 (if (known_lt (off0, off1))
4010 /* (convert (minmax ((convert (x) c)))) -> minmax (x c) if x is promoted
4011 and the outer convert demotes the expression back to x's type. */
4012 (for minmax (min max)
4014 (convert (minmax@0 (convert @1) INTEGER_CST@2))
4015 (if (INTEGRAL_TYPE_P (type)
4016 && types_match (@1, type) && int_fits_type_p (@2, type)
4017 && TYPE_SIGN (TREE_TYPE (@0)) == TYPE_SIGN (type)
4018 && TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type))
4019 (minmax @1 (convert @2)))))
4021 (for minmax (FMIN_ALL FMAX_ALL)
4022 /* If either argument is NaN and other one is not sNaN, return the other
4023 one. Avoid the transformation if we get (and honor) a signalling NaN. */
4025 (minmax:c @0 REAL_CST@1)
4026 (if (real_isnan (TREE_REAL_CST_PTR (@1))
4027 && (!HONOR_SNANS (@1) || !TREE_REAL_CST (@1).signalling)
4028 && !tree_expr_maybe_signaling_nan_p (@0))
4030 /* Convert fmin/fmax to MIN_EXPR/MAX_EXPR. C99 requires these
4031 functions to return the numeric arg if the other one is NaN.
4032 MIN and MAX don't honor that, so only transform if -ffinite-math-only
4033 is set. C99 doesn't require -0.0 to be handled, so we don't have to
4034 worry about it either. */
4035 (if (flag_finite_math_only)
4042 /* min (-A, -B) -> -max (A, B) */
4043 (for minmax (min max FMIN_ALL FMAX_ALL)
4044 maxmin (max min FMAX_ALL FMIN_ALL)
4046 (minmax (negate:s@2 @0) (negate:s@3 @1))
4047 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
4048 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
4049 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
4050 (negate (maxmin @0 @1)))))
4051 /* MIN (~X, ~Y) -> ~MAX (X, Y)
4052 MAX (~X, ~Y) -> ~MIN (X, Y) */
4053 (for minmax (min max)
4056 (minmax (bit_not:s@2 @0) (bit_not:s@3 @1))
4057 (bit_not (maxmin @0 @1)))
4058 /* ~MAX(~X, Y) --> MIN(X, ~Y) */
4059 /* ~MIN(~X, Y) --> MAX(X, ~Y) */
4061 (bit_not (minmax:cs (bit_not @0) @1))
4062 (maxmin @0 (bit_not @1))))
4064 /* MIN (X, Y) == X -> X <= Y */
4065 /* MIN (X, Y) < X -> X > Y */
4066 /* MIN (X, Y) >= X -> X <= Y */
4067 (for minmax (min min min min max max max max)
4068 cmp (eq ne lt ge eq ne gt le )
4069 out (le gt gt le ge lt lt ge )
4071 (cmp:c (minmax:c @0 @1) @0)
4072 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0)))
4074 /* MIN (X, 5) == 0 -> X == 0
4075 MIN (X, 5) == 7 -> false */
4078 (cmp (min @0 INTEGER_CST@1) INTEGER_CST@2)
4079 (if (wi::lt_p (wi::to_wide (@1), wi::to_wide (@2),
4080 TYPE_SIGN (TREE_TYPE (@0))))
4081 { constant_boolean_node (cmp == NE_EXPR, type); }
4082 (if (wi::gt_p (wi::to_wide (@1), wi::to_wide (@2),
4083 TYPE_SIGN (TREE_TYPE (@0))))
4087 (cmp (max @0 INTEGER_CST@1) INTEGER_CST@2)
4088 (if (wi::gt_p (wi::to_wide (@1), wi::to_wide (@2),
4089 TYPE_SIGN (TREE_TYPE (@0))))
4090 { constant_boolean_node (cmp == NE_EXPR, type); }
4091 (if (wi::lt_p (wi::to_wide (@1), wi::to_wide (@2),
4092 TYPE_SIGN (TREE_TYPE (@0))))
4095 /* X <= MAX(X, Y) -> true
4096 X > MAX(X, Y) -> false
4097 X >= MIN(X, Y) -> true
4098 X < MIN(X, Y) -> false */
4099 (for minmax (min min max max )
4102 (cmp:c @0 (minmax:c @0 @1))
4103 { constant_boolean_node (cmp == GE_EXPR || cmp == LE_EXPR, type); } ))
4105 /* MIN (X, C1) < C2 -> X < C2 || C1 < C2 */
4106 (for minmax (min min max max min min max max )
4107 cmp (lt le gt ge gt ge lt le )
4108 comb (bit_ior bit_ior bit_ior bit_ior bit_and bit_and bit_and bit_and)
4110 (cmp (minmax @0 INTEGER_CST@1) INTEGER_CST@2)
4111 (comb (cmp @0 @2) (cmp @1 @2))))
4113 /* Undo fancy ways of writing max/min or other ?: expressions, like
4114 a - ((a - b) & -(a < b)) and a - (a - b) * (a < b) into (a < b) ? b : a.
4115 People normally use ?: and that is what we actually try to optimize. */
4116 /* Transform A + (B-A)*cmp into cmp ? B : A. */
4118 (plus:c @0 (mult:c (minus @1 @0) zero_one_valued_p@2))
4119 (if (INTEGRAL_TYPE_P (type)
4120 && (GIMPLE || !TREE_SIDE_EFFECTS (@1)))
4121 (cond (convert:boolean_type_node @2) @1 @0)))
4122 /* Transform A - (A-B)*cmp into cmp ? B : A. */
4124 (minus @0 (mult:c (minus @0 @1) zero_one_valued_p@2))
4125 (if (INTEGRAL_TYPE_P (type)
4126 && (GIMPLE || !TREE_SIDE_EFFECTS (@1)))
4127 (cond (convert:boolean_type_node @2) @1 @0)))
4128 /* Transform A ^ (A^B)*cmp into cmp ? B : A. */
4130 (bit_xor:c @0 (mult:c (bit_xor:c @0 @1) zero_one_valued_p@2))
4131 (if (INTEGRAL_TYPE_P (type)
4132 && (GIMPLE || !TREE_SIDE_EFFECTS (@1)))
4133 (cond (convert:boolean_type_node @2) @1 @0)))
4135 /* (x <= 0 ? -x : 0) -> max(-x, 0). */
4137 (cond (le @0 integer_zerop@1) (negate@2 @0) integer_zerop@1)
4140 /* (zero_one == 0) ? y : z <op> y -> ((typeof(y))zero_one * z) <op> y */
4141 (for op (bit_xor bit_ior plus)
4143 (cond (eq zero_one_valued_p@0
4147 (if (INTEGRAL_TYPE_P (type)
4148 && TYPE_PRECISION (type) > 1
4149 && (INTEGRAL_TYPE_P (TREE_TYPE (@0))))
4150 (op (mult (convert:type @0) @2) @1))))
4152 /* (zero_one != 0) ? z <op> y : y -> ((typeof(y))zero_one * z) <op> y */
4153 (for op (bit_xor bit_ior plus)
4155 (cond (ne zero_one_valued_p@0
4159 (if (INTEGRAL_TYPE_P (type)
4160 && TYPE_PRECISION (type) > 1
4161 && (INTEGRAL_TYPE_P (TREE_TYPE (@0))))
4162 (op (mult (convert:type @0) @2) @1))))
4164 /* ?: Value replacement. */
4165 /* a == 0 ? b : b + a -> b + a */
4166 (for op (plus bit_ior bit_xor)
4168 (cond (eq @0 integer_zerop) @1 (op:c@2 @1 @0))
4170 /* a == 0 ? b : b - a -> b - a */
4171 /* a == 0 ? b : b ptr+ a -> b ptr+ a */
4172 /* a == 0 ? b : b shift/rotate a -> b shift/rotate a */
4173 (for op (lrotate rrotate lshift rshift minus pointer_plus)
4175 (cond (eq @0 integer_zerop) @1 (op@2 @1 @0))
4178 /* a == 1 ? b : b / a -> b / a */
4179 (for op (trunc_div ceil_div floor_div round_div exact_div)
4181 (cond (eq @0 integer_onep) @1 (op@2 @1 @0))
4184 /* a == 1 ? b : a * b -> a * b */
4187 (cond (eq @0 integer_onep) @1 (op:c@2 @1 @0))
4190 /* a == -1 ? b : a & b -> a & b */
4193 (cond (eq @0 integer_all_onesp) @1 (op:c@2 @1 @0))
4196 /* Simplifications of shift and rotates. */
4198 (for rotate (lrotate rrotate)
4200 (rotate integer_all_onesp@0 @1)
4203 /* Optimize -1 >> x for arithmetic right shifts. */
4205 (rshift integer_all_onesp@0 @1)
4206 (if (!TYPE_UNSIGNED (type))
4209 /* Optimize (x >> c) << c into x & (-1<<c). */
4211 (lshift (nop_convert? (rshift @0 INTEGER_CST@1)) @1)
4212 (if (wi::ltu_p (wi::to_wide (@1), element_precision (type)))
4213 /* It doesn't matter if the right shift is arithmetic or logical. */
4214 (bit_and (view_convert @0) (lshift { build_minus_one_cst (type); } @1))))
4217 (lshift (convert (convert@2 (rshift @0 INTEGER_CST@1))) @1)
4218 (if (wi::ltu_p (wi::to_wide (@1), element_precision (type))
4219 /* Allow intermediate conversion to integral type with whatever sign, as
4220 long as the low TYPE_PRECISION (type)
4221 - TYPE_PRECISION (TREE_TYPE (@2)) bits are preserved. */
4222 && INTEGRAL_TYPE_P (type)
4223 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
4224 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
4225 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (@0))
4226 && (TYPE_PRECISION (TREE_TYPE (@2)) >= TYPE_PRECISION (type)
4227 || wi::geu_p (wi::to_wide (@1),
4228 TYPE_PRECISION (type)
4229 - TYPE_PRECISION (TREE_TYPE (@2)))))
4230 (bit_and (convert @0) (lshift { build_minus_one_cst (type); } @1))))
4232 /* For (x << c) >> c, optimize into x & ((unsigned)-1 >> c) for
4233 unsigned x OR truncate into the precision(type) - c lowest bits
4234 of signed x (if they have mode precision or a precision of 1). */
4236 (rshift (nop_convert? (lshift @0 INTEGER_CST@1)) @@1)
4237 (if (wi::ltu_p (wi::to_wide (@1), element_precision (type)))
4238 (if (TYPE_UNSIGNED (type))
4239 (bit_and (convert @0) (rshift { build_minus_one_cst (type); } @1))
4240 (if (INTEGRAL_TYPE_P (type))
4242 int width = element_precision (type) - tree_to_uhwi (@1);
4243 tree stype = NULL_TREE;
4244 if (width <= MAX_FIXED_MODE_SIZE)
4245 stype = build_nonstandard_integer_type (width, 0);
4247 (if (stype && (width == 1 || type_has_mode_precision_p (stype)))
4248 (convert (convert:stype @0))))))))
4250 /* Optimize x >> x into 0 */
4253 { build_zero_cst (type); })
4255 (for shiftrotate (lrotate rrotate lshift rshift)
4257 (shiftrotate @0 integer_zerop)
4260 (shiftrotate integer_zerop@0 @1)
4262 /* Prefer vector1 << scalar to vector1 << vector2
4263 if vector2 is uniform. */
4264 (for vec (VECTOR_CST CONSTRUCTOR)
4266 (shiftrotate @0 vec@1)
4267 (with { tree tem = uniform_vector_p (@1); }
4269 (shiftrotate @0 { tem; }))))))
4271 /* Simplify X << Y where Y's low width bits are 0 to X, as only valid
4272 Y is 0. Similarly for X >> Y. */
4274 (for shift (lshift rshift)
4276 (shift @0 SSA_NAME@1)
4277 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1)))
4279 int width = ceil_log2 (element_precision (TREE_TYPE (@0)));
4280 int prec = TYPE_PRECISION (TREE_TYPE (@1));
4282 (if ((get_nonzero_bits (@1) & wi::mask (width, false, prec)) == 0)
4286 /* Rewrite an LROTATE_EXPR by a constant into an
4287 RROTATE_EXPR by a new constant. */
4289 (lrotate @0 INTEGER_CST@1)
4290 (rrotate @0 { const_binop (MINUS_EXPR, TREE_TYPE (@1),
4291 build_int_cst (TREE_TYPE (@1),
4292 element_precision (type)), @1); }))
4294 /* Turn (a OP c1) OP c2 into a OP (c1+c2). */
4295 (for op (lrotate rrotate rshift lshift)
4297 (op (op @0 INTEGER_CST@1) INTEGER_CST@2)
4298 (with { unsigned int prec = element_precision (type); }
4299 (if (wi::ge_p (wi::to_wide (@1), 0, TYPE_SIGN (TREE_TYPE (@1)))
4300 && wi::lt_p (wi::to_wide (@1), prec, TYPE_SIGN (TREE_TYPE (@1)))
4301 && wi::ge_p (wi::to_wide (@2), 0, TYPE_SIGN (TREE_TYPE (@2)))
4302 && wi::lt_p (wi::to_wide (@2), prec, TYPE_SIGN (TREE_TYPE (@2))))
4303 (with { unsigned int low = (tree_to_uhwi (@1)
4304 + tree_to_uhwi (@2)); }
4305 /* Deal with a OP (c1 + c2) being undefined but (a OP c1) OP c2
4306 being well defined. */
4308 (if (op == LROTATE_EXPR || op == RROTATE_EXPR)
4309 (op @0 { build_int_cst (TREE_TYPE (@1), low % prec); })
4310 (if (TYPE_UNSIGNED (type) || op == LSHIFT_EXPR)
4311 { build_zero_cst (type); }
4312 (op @0 { build_int_cst (TREE_TYPE (@1), prec - 1); })))
4313 (op @0 { build_int_cst (TREE_TYPE (@1), low); })))))))
4316 /* Simplify (CST << x) & 1 to 0 if CST is even or to x == 0 if it is odd. */
4318 (bit_and (lshift INTEGER_CST@1 @0) integer_onep)
4319 (if ((wi::to_wide (@1) & 1) != 0)
4320 (convert (eq:boolean_type_node @0 { build_zero_cst (TREE_TYPE (@0)); }))
4321 { build_zero_cst (type); }))
4323 /* Simplify ((C << x) & D) != 0 where C and D are power of two constants,
4324 either to false if D is smaller (unsigned comparison) than C, or to
4325 x == log2 (D) - log2 (C). Similarly for right shifts.
4326 Note for `(1 >> x)`, the & 1 has been removed so matching that seperately. */
4330 (cmp (bit_and (lshift integer_pow2p@1 @0) integer_pow2p@2) integer_zerop)
4331 (with { int c1 = wi::clz (wi::to_wide (@1));
4332 int c2 = wi::clz (wi::to_wide (@2)); }
4334 { constant_boolean_node (cmp == NE_EXPR ? false : true, type); }
4335 (icmp @0 { build_int_cst (TREE_TYPE (@0), c1 - c2); }))))
4337 (cmp (bit_and (rshift integer_pow2p@1 @0) integer_pow2p@2) integer_zerop)
4338 (if (tree_int_cst_sgn (@1) > 0)
4339 (with { int c1 = wi::clz (wi::to_wide (@1));
4340 int c2 = wi::clz (wi::to_wide (@2)); }
4342 { constant_boolean_node (cmp == NE_EXPR ? false : true, type); }
4343 (icmp @0 { build_int_cst (TREE_TYPE (@0), c2 - c1); })))))
4344 /* `(1 >> X) != 0` -> `X == 0` */
4345 /* `(1 >> X) == 0` -> `X != 0` */
4347 (cmp (rshift integer_onep@1 @0) integer_zerop)
4348 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1)))
4349 (icmp @0 { build_zero_cst (TREE_TYPE (@0)); }))))
4351 /* (CST1 << A) == CST2 -> A == ctz (CST2) - ctz (CST1)
4352 (CST1 << A) != CST2 -> A != ctz (CST2) - ctz (CST1)
4356 (cmp (lshift INTEGER_CST@0 @1) INTEGER_CST@2)
4357 (with { int cand = wi::ctz (wi::to_wide (@2)) - wi::ctz (wi::to_wide (@0)); }
4359 || (!integer_zerop (@2)
4360 && wi::lshift (wi::to_wide (@0), cand) != wi::to_wide (@2)))
4361 { constant_boolean_node (cmp == NE_EXPR, type); }
4362 (if (!integer_zerop (@2)
4363 && wi::lshift (wi::to_wide (@0), cand) == wi::to_wide (@2))
4364 (cmp @1 { build_int_cst (TREE_TYPE (@1), cand); }))))))
4366 /* Fold ((X << C1) & C2) cmp C3 into (X & (C2 >> C1)) cmp (C3 >> C1)
4367 ((X >> C1) & C2) cmp C3 into (X & (C2 << C1)) cmp (C3 << C1). */
4370 (cmp (bit_and:s (lshift:s @0 INTEGER_CST@1) INTEGER_CST@2) INTEGER_CST@3)
4371 (if (tree_fits_shwi_p (@1)
4372 && tree_to_shwi (@1) > 0
4373 && tree_to_shwi (@1) < TYPE_PRECISION (TREE_TYPE (@0)))
4374 (if (tree_to_shwi (@1) > wi::ctz (wi::to_wide (@3)))
4375 { constant_boolean_node (cmp == NE_EXPR, type); }
4376 (with { wide_int c1 = wi::to_wide (@1);
4377 wide_int c2 = wi::lrshift (wi::to_wide (@2), c1);
4378 wide_int c3 = wi::lrshift (wi::to_wide (@3), c1); }
4379 (cmp (bit_and @0 { wide_int_to_tree (TREE_TYPE (@0), c2); })
4380 { wide_int_to_tree (TREE_TYPE (@0), c3); })))))
4382 (cmp (bit_and:s (rshift:s @0 INTEGER_CST@1) INTEGER_CST@2) INTEGER_CST@3)
4383 (if (tree_fits_shwi_p (@1)
4384 && tree_to_shwi (@1) > 0
4385 && tree_to_shwi (@1) < TYPE_PRECISION (TREE_TYPE (@0)))
4386 (with { tree t0 = TREE_TYPE (@0);
4387 unsigned int prec = TYPE_PRECISION (t0);
4388 wide_int c1 = wi::to_wide (@1);
4389 wide_int c2 = wi::to_wide (@2);
4390 wide_int c3 = wi::to_wide (@3);
4391 wide_int sb = wi::set_bit_in_zero (prec - 1, prec); }
4392 (if ((c2 & c3) != c3)
4393 { constant_boolean_node (cmp == NE_EXPR, type); }
4394 (if (TYPE_UNSIGNED (t0))
4395 (if ((c3 & wi::arshift (sb, c1 - 1)) != 0)
4396 { constant_boolean_node (cmp == NE_EXPR, type); }
4397 (cmp (bit_and @0 { wide_int_to_tree (t0, c2 << c1); })
4398 { wide_int_to_tree (t0, c3 << c1); }))
4399 (with { wide_int smask = wi::arshift (sb, c1); }
4401 (if ((c2 & smask) == 0)
4402 (cmp (bit_and @0 { wide_int_to_tree (t0, c2 << c1); })
4403 { wide_int_to_tree (t0, c3 << c1); }))
4404 (if ((c3 & smask) == 0)
4405 (cmp (bit_and @0 { wide_int_to_tree (t0, (c2 << c1) | sb); })
4406 { wide_int_to_tree (t0, c3 << c1); }))
4407 (if ((c2 & smask) != (c3 & smask))
4408 { constant_boolean_node (cmp == NE_EXPR, type); })
4409 (cmp (bit_and @0 { wide_int_to_tree (t0, (c2 << c1) | sb); })
4410 { wide_int_to_tree (t0, (c3 << c1) | sb); })))))))))
4412 /* Fold (X << C1) & C2 into (X << C1) & (C2 | ((1 << C1) - 1))
4413 (X >> C1) & C2 into (X >> C1) & (C2 | ~((type) -1 >> C1))
4414 if the new mask might be further optimized. */
4415 (for shift (lshift rshift)
4417 (bit_and (convert?:s@4 (shift:s@5 (convert1?@3 @0) INTEGER_CST@1))
4419 (if (tree_nop_conversion_p (TREE_TYPE (@4), TREE_TYPE (@5))
4420 && TYPE_PRECISION (type) <= HOST_BITS_PER_WIDE_INT
4421 && tree_fits_uhwi_p (@1)
4422 && tree_to_uhwi (@1) > 0
4423 && tree_to_uhwi (@1) < TYPE_PRECISION (type))
4426 unsigned int shiftc = tree_to_uhwi (@1);
4427 unsigned HOST_WIDE_INT mask = TREE_INT_CST_LOW (@2);
4428 unsigned HOST_WIDE_INT newmask, zerobits = 0;
4429 tree shift_type = TREE_TYPE (@3);
4432 if (shift == LSHIFT_EXPR)
4433 zerobits = ((HOST_WIDE_INT_1U << shiftc) - 1);
4434 else if (shift == RSHIFT_EXPR
4435 && type_has_mode_precision_p (shift_type))
4437 prec = TYPE_PRECISION (TREE_TYPE (@3));
4439 /* See if more bits can be proven as zero because of
4442 && TYPE_UNSIGNED (TREE_TYPE (@0)))
4444 tree inner_type = TREE_TYPE (@0);
4445 if (type_has_mode_precision_p (inner_type)
4446 && TYPE_PRECISION (inner_type) < prec)
4448 prec = TYPE_PRECISION (inner_type);
4449 /* See if we can shorten the right shift. */
4451 shift_type = inner_type;
4452 /* Otherwise X >> C1 is all zeros, so we'll optimize
4453 it into (X, 0) later on by making sure zerobits
4457 zerobits = HOST_WIDE_INT_M1U;
4460 zerobits >>= HOST_BITS_PER_WIDE_INT - shiftc;
4461 zerobits <<= prec - shiftc;
4463 /* For arithmetic shift if sign bit could be set, zerobits
4464 can contain actually sign bits, so no transformation is
4465 possible, unless MASK masks them all away. In that
4466 case the shift needs to be converted into logical shift. */
4467 if (!TYPE_UNSIGNED (TREE_TYPE (@3))
4468 && prec == TYPE_PRECISION (TREE_TYPE (@3)))
4470 if ((mask & zerobits) == 0)
4471 shift_type = unsigned_type_for (TREE_TYPE (@3));
4477 /* ((X << 16) & 0xff00) is (X, 0). */
4478 (if ((mask & zerobits) == mask)
4479 { build_int_cst (type, 0); }
4480 (with { newmask = mask | zerobits; }
4481 (if (newmask != mask && (newmask & (newmask + 1)) == 0)
4484 /* Only do the transformation if NEWMASK is some integer
4486 for (prec = BITS_PER_UNIT;
4487 prec < HOST_BITS_PER_WIDE_INT; prec <<= 1)
4488 if (newmask == (HOST_WIDE_INT_1U << prec) - 1)
4491 (if (prec < HOST_BITS_PER_WIDE_INT
4492 || newmask == HOST_WIDE_INT_M1U)
4494 { tree newmaskt = build_int_cst_type (TREE_TYPE (@2), newmask); }
4495 (if (!tree_int_cst_equal (newmaskt, @2))
4496 (if (shift_type != TREE_TYPE (@3))
4497 (bit_and (convert (shift:shift_type (convert @3) @1)) { newmaskt; })
4498 (bit_and @4 { newmaskt; })))))))))))))
4500 /* ((1 << n) & M) != 0 -> n == log2 (M) */
4506 (nop_convert? (lshift integer_onep @0)) integer_pow2p@1) integer_zerop)
4507 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)))
4508 (icmp @0 { wide_int_to_tree (TREE_TYPE (@0),
4509 wi::exact_log2 (wi::to_wide (@1))); }))))
4511 /* Fold (X {&,^,|} C2) << C1 into (X << C1) {&,^,|} (C2 << C1)
4512 (X {&,^,|} C2) >> C1 into (X >> C1) & (C2 >> C1). */
4513 (for shift (lshift rshift)
4514 (for bit_op (bit_and bit_xor bit_ior)
4516 (shift (convert?:s (bit_op:s @0 INTEGER_CST@2)) INTEGER_CST@1)
4517 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
4518 (with { tree mask = int_const_binop (shift, fold_convert (type, @2), @1); }
4520 (bit_op (shift (convert @0) @1) { mask; })))))))
4522 /* ~(~X >> Y) -> X >> Y (for arithmetic shift). */
4524 (bit_not (convert1?:s (rshift:s (convert2?@0 (bit_not @1)) @2)))
4525 (if (!TYPE_UNSIGNED (TREE_TYPE (@0))
4526 && (element_precision (TREE_TYPE (@0))
4527 <= element_precision (TREE_TYPE (@1))
4528 || !TYPE_UNSIGNED (TREE_TYPE (@1))))
4530 { tree shift_type = TREE_TYPE (@0); }
4531 (convert (rshift (convert:shift_type @1) @2)))))
4533 /* ~(~X >>r Y) -> X >>r Y
4534 ~(~X <<r Y) -> X <<r Y */
4535 (for rotate (lrotate rrotate)
4537 (bit_not (convert1?:s (rotate:s (convert2?@0 (bit_not @1)) @2)))
4538 (if ((element_precision (TREE_TYPE (@0))
4539 <= element_precision (TREE_TYPE (@1))
4540 || !TYPE_UNSIGNED (TREE_TYPE (@1)))
4541 && (element_precision (type) <= element_precision (TREE_TYPE (@0))
4542 || !TYPE_UNSIGNED (TREE_TYPE (@0))))
4544 { tree rotate_type = TREE_TYPE (@0); }
4545 (convert (rotate (convert:rotate_type @1) @2))))))
4548 (for rotate (lrotate rrotate)
4549 invrot (rrotate lrotate)
4550 /* (X >>r Y) cmp (Z >>r Y) may simplify to X cmp Y. */
4552 (cmp (rotate @1 @0) (rotate @2 @0))
4554 /* (X >>r C1) cmp C2 may simplify to X cmp C3. */
4556 (cmp (rotate @0 INTEGER_CST@1) INTEGER_CST@2)
4557 (cmp @0 { const_binop (invrot, TREE_TYPE (@0), @2, @1); }))
4558 /* (X >>r Y) cmp C where C is 0 or ~0, may simplify to X cmp C. */
4560 (cmp (rotate @0 @1) INTEGER_CST@2)
4561 (if (integer_zerop (@2) || integer_all_onesp (@2))
4564 /* Narrow a lshift by constant. */
4566 (convert (lshift:s@0 @1 INTEGER_CST@2))
4567 (if (INTEGRAL_TYPE_P (type)
4568 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
4569 && !integer_zerop (@2)
4570 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0)))
4571 (if (TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (@0))
4572 || wi::ltu_p (wi::to_wide (@2), TYPE_PRECISION (type)))
4573 (lshift (convert @1) @2)
4574 (if (wi::ltu_p (wi::to_wide (@2), TYPE_PRECISION (TREE_TYPE (@0))))
4575 { build_zero_cst (type); }))))
4577 /* Simplifications of conversions. */
4579 /* Basic strip-useless-type-conversions / strip_nops. */
4580 (for cvt (convert view_convert float fix_trunc)
4583 (if ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@0)))
4584 || (GENERIC && type == TREE_TYPE (@0)))
4587 /* Contract view-conversions. */
4589 (view_convert (view_convert @0))
4592 /* For integral conversions with the same precision or pointer
4593 conversions use a NOP_EXPR instead. */
4596 (if ((INTEGRAL_TYPE_P (type) || POINTER_TYPE_P (type))
4597 && (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
4598 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (@0)))
4601 /* Strip inner integral conversions that do not change precision or size, or
4602 zero-extend while keeping the same size (for bool-to-char). */
4604 (view_convert (convert@0 @1))
4605 (if ((INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
4606 && (INTEGRAL_TYPE_P (TREE_TYPE (@1)) || POINTER_TYPE_P (TREE_TYPE (@1)))
4607 && TYPE_SIZE (TREE_TYPE (@0)) == TYPE_SIZE (TREE_TYPE (@1))
4608 && (TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1))
4609 || (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (TREE_TYPE (@1))
4610 && TYPE_UNSIGNED (TREE_TYPE (@1)))))
4613 /* Simplify a view-converted empty or single-element constructor. */
4615 (view_convert CONSTRUCTOR@0)
4617 { tree ctor = (TREE_CODE (@0) == SSA_NAME
4618 ? gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0)) : @0); }
4620 (if (CONSTRUCTOR_NELTS (ctor) == 0)
4621 { build_zero_cst (type); })
4622 (if (CONSTRUCTOR_NELTS (ctor) == 1
4623 && VECTOR_TYPE_P (TREE_TYPE (ctor))
4624 && operand_equal_p (TYPE_SIZE (type),
4625 TYPE_SIZE (TREE_TYPE
4626 (CONSTRUCTOR_ELT (ctor, 0)->value))))
4627 (view_convert { CONSTRUCTOR_ELT (ctor, 0)->value; })))))
4629 /* Re-association barriers around constants and other re-association
4630 barriers can be removed. */
4632 (paren CONSTANT_CLASS_P@0)
4635 (paren (paren@1 @0))
4638 /* Handle cases of two conversions in a row. */
4639 (for ocvt (convert float fix_trunc)
4640 (for icvt (convert float)
4645 tree inside_type = TREE_TYPE (@0);
4646 tree inter_type = TREE_TYPE (@1);
4647 int inside_int = INTEGRAL_TYPE_P (inside_type);
4648 int inside_ptr = POINTER_TYPE_P (inside_type);
4649 int inside_float = FLOAT_TYPE_P (inside_type);
4650 int inside_vec = VECTOR_TYPE_P (inside_type);
4651 unsigned int inside_prec = element_precision (inside_type);
4652 int inside_unsignedp = TYPE_UNSIGNED (inside_type);
4653 int inter_int = INTEGRAL_TYPE_P (inter_type);
4654 int inter_ptr = POINTER_TYPE_P (inter_type);
4655 int inter_float = FLOAT_TYPE_P (inter_type);
4656 int inter_vec = VECTOR_TYPE_P (inter_type);
4657 unsigned int inter_prec = element_precision (inter_type);
4658 int inter_unsignedp = TYPE_UNSIGNED (inter_type);
4659 int final_int = INTEGRAL_TYPE_P (type);
4660 int final_ptr = POINTER_TYPE_P (type);
4661 int final_float = FLOAT_TYPE_P (type);
4662 int final_vec = VECTOR_TYPE_P (type);
4663 unsigned int final_prec = element_precision (type);
4664 int final_unsignedp = TYPE_UNSIGNED (type);
4667 /* In addition to the cases of two conversions in a row
4668 handled below, if we are converting something to its own
4669 type via an object of identical or wider precision, neither
4670 conversion is needed. */
4671 (if (((GIMPLE && useless_type_conversion_p (type, inside_type))
4673 && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (inside_type)))
4674 && (((inter_int || inter_ptr) && final_int)
4675 || (inter_float && final_float))
4676 && inter_prec >= final_prec)
4679 /* Likewise, if the intermediate and initial types are either both
4680 float or both integer, we don't need the middle conversion if the
4681 former is wider than the latter and doesn't change the signedness
4682 (for integers). Avoid this if the final type is a pointer since
4683 then we sometimes need the middle conversion. */
4684 (if (((inter_int && inside_int) || (inter_float && inside_float))
4685 && (final_int || final_float)
4686 && inter_prec >= inside_prec
4687 && (inter_float || inter_unsignedp == inside_unsignedp))
4690 /* If we have a sign-extension of a zero-extended value, we can
4691 replace that by a single zero-extension. Likewise if the
4692 final conversion does not change precision we can drop the
4693 intermediate conversion. */
4694 (if (inside_int && inter_int && final_int
4695 && ((inside_prec < inter_prec && inter_prec < final_prec
4696 && inside_unsignedp && !inter_unsignedp)
4697 || final_prec == inter_prec))
4700 /* Two conversions in a row are not needed unless:
4701 - some conversion is floating-point (overstrict for now), or
4702 - some conversion is a vector (overstrict for now), or
4703 - the intermediate type is narrower than both initial and
4705 - the intermediate type and innermost type differ in signedness,
4706 and the outermost type is wider than the intermediate, or
4707 - the initial type is a pointer type and the precisions of the
4708 intermediate and final types differ, or
4709 - the final type is a pointer type and the precisions of the
4710 initial and intermediate types differ. */
4711 (if (! inside_float && ! inter_float && ! final_float
4712 && ! inside_vec && ! inter_vec && ! final_vec
4713 && (inter_prec >= inside_prec || inter_prec >= final_prec)
4714 && ! (inside_int && inter_int
4715 && inter_unsignedp != inside_unsignedp
4716 && inter_prec < final_prec)
4717 && ((inter_unsignedp && inter_prec > inside_prec)
4718 == (final_unsignedp && final_prec > inter_prec))
4719 && ! (inside_ptr && inter_prec != final_prec)
4720 && ! (final_ptr && inside_prec != inter_prec))
4723 /* `(outer:M)(inter:N) a:O`
4724 can be converted to `(outer:M) a`
4725 if M <= O && N >= O. No matter what signedness of the casts,
4726 as the final is either a truncation from the original or just
4727 a sign change of the type. */
4728 (if (inside_int && inter_int && final_int
4729 && final_prec <= inside_prec
4730 && inter_prec >= inside_prec)
4733 /* A truncation to an unsigned type (a zero-extension) should be
4734 canonicalized as bitwise and of a mask. */
4735 (if (GIMPLE /* PR70366: doing this in GENERIC breaks -Wconversion. */
4736 && final_int && inter_int && inside_int
4737 && final_prec == inside_prec
4738 && final_prec > inter_prec
4740 (convert (bit_and @0 { wide_int_to_tree
4742 wi::mask (inter_prec, false,
4743 TYPE_PRECISION (inside_type))); })))
4745 /* If we are converting an integer to a floating-point that can
4746 represent it exactly and back to an integer, we can skip the
4747 floating-point conversion. */
4748 (if (GIMPLE /* PR66211 */
4749 && inside_int && inter_float && final_int &&
4750 (unsigned) significand_size (TYPE_MODE (inter_type))
4751 >= inside_prec - !inside_unsignedp)
4754 /* (float_type)(integer_type) x -> trunc (x) if the type of x matches
4755 float_type. Only do the transformation if we do not need to preserve
4756 trapping behaviour, so require !flag_trapping_math. */
4759 (float (fix_trunc @0))
4760 (if (!flag_trapping_math
4761 && types_match (type, TREE_TYPE (@0))
4762 && direct_internal_fn_supported_p (IFN_TRUNC, type,
4767 /* If we have a narrowing conversion to an integral type that is fed by a
4768 BIT_AND_EXPR, we might be able to remove the BIT_AND_EXPR if it merely
4769 masks off bits outside the final type (and nothing else). */
4771 (convert (bit_and @0 INTEGER_CST@1))
4772 (if (INTEGRAL_TYPE_P (type)
4773 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
4774 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))
4775 && operand_equal_p (@1, build_low_bits_mask (TREE_TYPE (@1),
4776 TYPE_PRECISION (type)), 0))
4780 /* (X /[ex] A) * A -> X. */
4782 (mult (convert1? (exact_div @0 @@1)) (convert2? @1))
4785 /* Simplify (A / B) * B + (A % B) -> A. */
4786 (for div (trunc_div ceil_div floor_div round_div)
4787 mod (trunc_mod ceil_mod floor_mod round_mod)
4789 (plus:c (mult:c (div @0 @1) @1) (mod @0 @1))
4792 /* x / y * y == x -> x % y == 0. */
4794 (eq:c (mult:c (trunc_div:s @0 @1) @1) @0)
4795 (if (TREE_CODE (TREE_TYPE (@0)) != COMPLEX_TYPE)
4796 (eq (trunc_mod @0 @1) { build_zero_cst (TREE_TYPE (@0)); })))
4798 /* ((X /[ex] A) +- B) * A --> X +- A * B. */
4799 (for op (plus minus)
4801 (mult (convert1? (op (convert2? (exact_div @0 INTEGER_CST@@1)) INTEGER_CST@2)) @1)
4802 (if (tree_nop_conversion_p (type, TREE_TYPE (@2))
4803 && tree_nop_conversion_p (TREE_TYPE (@0), TREE_TYPE (@2)))
4806 wi::overflow_type overflow;
4807 wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@2),
4808 TYPE_SIGN (type), &overflow);
4810 (if (types_match (type, TREE_TYPE (@2))
4811 && types_match (TREE_TYPE (@0), TREE_TYPE (@2)) && !overflow)
4812 (op @0 { wide_int_to_tree (type, mul); })
4813 (with { tree utype = unsigned_type_for (type); }
4814 (convert (op (convert:utype @0)
4815 (mult (convert:utype @1) (convert:utype @2))))))))))
4817 /* Canonicalization of binary operations. */
4819 /* Convert X + -C into X - C. */
4821 (plus @0 REAL_CST@1)
4822 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
4823 (with { tree tem = const_unop (NEGATE_EXPR, type, @1); }
4824 (if (!TREE_OVERFLOW (tem) || !flag_trapping_math)
4825 (minus @0 { tem; })))))
4827 /* Convert x+x into x*2. */
4830 (if (SCALAR_FLOAT_TYPE_P (type))
4831 (mult @0 { build_real (type, dconst2); })
4832 (if (INTEGRAL_TYPE_P (type))
4833 (mult @0 { build_int_cst (type, 2); }))))
4837 (minus integer_zerop @1)
4840 (pointer_diff integer_zerop @1)
4841 (negate (convert @1)))
4843 /* (ARG0 - ARG1) is the same as (-ARG1 + ARG0). So check whether
4844 ARG0 is zero and X + ARG0 reduces to X, since that would mean
4845 (-ARG1 + ARG0) reduces to -ARG1. */
4847 (minus real_zerop@0 @1)
4848 (if (fold_real_zero_addition_p (type, @1, @0, 0))
4851 /* Transform x * -1 into -x. */
4853 (mult @0 integer_minus_onep)
4856 /* Reassociate (X * CST) * Y to (X * Y) * CST. This does not introduce
4857 signed overflow for CST != 0 && CST != -1. */
4859 (mult:c (mult:s@3 @0 INTEGER_CST@1) @2)
4860 (if (TREE_CODE (@2) != INTEGER_CST
4862 && !integer_zerop (@1) && !integer_minus_onep (@1))
4863 (mult (mult @0 @2) @1)))
4865 /* True if we can easily extract the real and imaginary parts of a complex
4867 (match compositional_complex
4868 (convert? (complex @0 @1)))
4870 /* COMPLEX_EXPR and REALPART/IMAGPART_EXPR cancellations. */
4872 (complex (realpart @0) (imagpart @0))
4875 (realpart (complex @0 @1))
4878 (imagpart (complex @0 @1))
4881 /* Sometimes we only care about half of a complex expression. */
4883 (realpart (convert?:s (conj:s @0)))
4884 (convert (realpart @0)))
4886 (imagpart (convert?:s (conj:s @0)))
4887 (convert (negate (imagpart @0))))
4888 (for part (realpart imagpart)
4889 (for op (plus minus)
4891 (part (convert?:s@2 (op:s @0 @1)))
4892 (convert (op (part @0) (part @1))))))
4894 (realpart (convert?:s (CEXPI:s @0)))
4897 (imagpart (convert?:s (CEXPI:s @0)))
4900 /* conj(conj(x)) -> x */
4902 (conj (convert? (conj @0)))
4903 (if (tree_nop_conversion_p (TREE_TYPE (@0), type))
4906 /* conj({x,y}) -> {x,-y} */
4908 (conj (convert?:s (complex:s @0 @1)))
4909 (with { tree itype = TREE_TYPE (type); }
4910 (complex (convert:itype @0) (negate (convert:itype @1)))))
4912 /* BSWAP simplifications, transforms checked by gcc.dg/builtin-bswap-8.c. */
4918 (bswap (bit_not (bswap @0)))
4920 (for bitop (bit_xor bit_ior bit_and)
4922 (bswap (bitop:c (bswap @0) @1))
4923 (bitop @0 (bswap @1))))
4926 (cmp (bswap@2 @0) (bswap @1))
4927 (with { tree ctype = TREE_TYPE (@2); }
4928 (cmp (convert:ctype @0) (convert:ctype @1))))
4930 (cmp (bswap @0) INTEGER_CST@1)
4931 (with { tree ctype = TREE_TYPE (@1); }
4932 (cmp (convert:ctype @0) (bswap! @1)))))
4933 /* (bswap(x) >> C1) & C2 can sometimes be simplified to (x >> C3) & C2. */
4935 (bit_and (convert1? (rshift@0 (convert2? (bswap@4 @1)) INTEGER_CST@2))
4937 (if (BITS_PER_UNIT == 8
4938 && tree_fits_uhwi_p (@2)
4939 && tree_fits_uhwi_p (@3))
4942 unsigned HOST_WIDE_INT prec = TYPE_PRECISION (TREE_TYPE (@4));
4943 unsigned HOST_WIDE_INT bits = tree_to_uhwi (@2);
4944 unsigned HOST_WIDE_INT mask = tree_to_uhwi (@3);
4945 unsigned HOST_WIDE_INT lo = bits & 7;
4946 unsigned HOST_WIDE_INT hi = bits - lo;
4949 && mask < (256u>>lo)
4950 && bits < TYPE_PRECISION (TREE_TYPE(@0)))
4951 (with { unsigned HOST_WIDE_INT ns = (prec - (hi + 8)) + lo; }
4953 (bit_and (convert @1) @3)
4956 tree utype = unsigned_type_for (TREE_TYPE (@1));
4957 tree nst = build_int_cst (integer_type_node, ns);
4959 (bit_and (convert (rshift:utype (convert:utype @1) {nst;})) @3))))))))
4960 /* bswap(x) >> C1 can sometimes be simplified to (T)x >> C2. */
4962 (rshift (convert? (bswap@2 @0)) INTEGER_CST@1)
4963 (if (BITS_PER_UNIT == 8
4964 && CHAR_TYPE_SIZE == 8
4965 && tree_fits_uhwi_p (@1))
4968 unsigned HOST_WIDE_INT prec = TYPE_PRECISION (TREE_TYPE (@2));
4969 unsigned HOST_WIDE_INT bits = tree_to_uhwi (@1);
4970 /* If the bswap was extended before the original shift, this
4971 byte (shift) has the sign of the extension, not the sign of
4972 the original shift. */
4973 tree st = TYPE_PRECISION (type) > prec ? TREE_TYPE (@2) : type;
4975 /* Special case: logical right shift of sign-extended bswap.
4976 (unsigned)(short)bswap16(x)>>12 is (unsigned)((short)x<<8)>>12. */
4977 (if (TYPE_PRECISION (type) > prec
4978 && !TYPE_UNSIGNED (TREE_TYPE (@2))
4979 && TYPE_UNSIGNED (type)
4980 && bits < prec && bits + 8 >= prec)
4981 (with { tree nst = build_int_cst (integer_type_node, prec - 8); }
4982 (rshift (convert (lshift:st (convert:st @0) {nst;})) @1))
4983 (if (bits + 8 == prec)
4984 (if (TYPE_UNSIGNED (st))
4985 (convert (convert:unsigned_char_type_node @0))
4986 (convert (convert:signed_char_type_node @0)))
4987 (if (bits < prec && bits + 8 > prec)
4990 tree nst = build_int_cst (integer_type_node, bits & 7);
4991 tree bt = TYPE_UNSIGNED (st) ? unsigned_char_type_node
4992 : signed_char_type_node;
4994 (convert (rshift:bt (convert:bt @0) {nst;})))))))))
4995 /* bswap(x) & C1 can sometimes be simplified to (x >> C2) & C1. */
4997 (bit_and (convert? (bswap@2 @0)) INTEGER_CST@1)
4998 (if (BITS_PER_UNIT == 8
4999 && tree_fits_uhwi_p (@1)
5000 && tree_to_uhwi (@1) < 256)
5003 unsigned HOST_WIDE_INT prec = TYPE_PRECISION (TREE_TYPE (@2));
5004 tree utype = unsigned_type_for (TREE_TYPE (@0));
5005 tree nst = build_int_cst (integer_type_node, prec - 8);
5007 (bit_and (convert (rshift:utype (convert:utype @0) {nst;})) @1)))))
5010 /* Combine COND_EXPRs and VEC_COND_EXPRs. */
5012 /* Simplify constant conditions.
5013 Only optimize constant conditions when the selected branch
5014 has the same type as the COND_EXPR. This avoids optimizing
5015 away "c ? x : throw", where the throw has a void type.
5016 Note that we cannot throw away the fold-const.cc variant nor
5017 this one as we depend on doing this transform before possibly
5018 A ? B : B -> B triggers and the fold-const.cc one can optimize
5019 0 ? A : B to B even if A has side-effects. Something
5020 genmatch cannot handle. */
5022 (cond INTEGER_CST@0 @1 @2)
5023 (if (integer_zerop (@0))
5024 (if (!VOID_TYPE_P (TREE_TYPE (@2)) || VOID_TYPE_P (type))
5026 (if (!VOID_TYPE_P (TREE_TYPE (@1)) || VOID_TYPE_P (type))
5029 (vec_cond VECTOR_CST@0 @1 @2)
5030 (if (integer_all_onesp (@0))
5032 (if (integer_zerop (@0))
5035 /* Sink unary operations to branches, but only if we do fold both. */
5036 (for op (negate bit_not abs absu)
5038 (op (vec_cond:s @0 @1 @2))
5039 (vec_cond @0 (op! @1) (op! @2))))
5041 /* Sink unary conversions to branches, but only if we do fold both
5042 and the target's truth type is the same as we already have. */
5044 (convert (vec_cond:s @0 @1 @2))
5045 (if (VECTOR_TYPE_P (type)
5046 && types_match (TREE_TYPE (@0), truth_type_for (type)))
5047 (vec_cond @0 (convert! @1) (convert! @2))))
5049 /* Likewise for view_convert of nop_conversions. */
5051 (view_convert (vec_cond:s @0 @1 @2))
5052 (if (VECTOR_TYPE_P (type) && VECTOR_TYPE_P (TREE_TYPE (@1))
5053 && known_eq (TYPE_VECTOR_SUBPARTS (type),
5054 TYPE_VECTOR_SUBPARTS (TREE_TYPE (@1)))
5055 && tree_nop_conversion_p (TREE_TYPE (type), TREE_TYPE (TREE_TYPE (@1))))
5056 (vec_cond @0 (view_convert! @1) (view_convert! @2))))
5058 /* Sink binary operation to branches, but only if we can fold it. */
5059 (for op (tcc_comparison plus minus mult bit_and bit_ior bit_xor
5060 lshift rshift rdiv trunc_div ceil_div floor_div round_div
5061 trunc_mod ceil_mod floor_mod round_mod min max)
5062 /* (c ? a : b) op (c ? d : e) --> c ? (a op d) : (b op e) */
5064 (op (vec_cond:s @0 @1 @2) (vec_cond:s @0 @3 @4))
5065 (vec_cond @0 (op! @1 @3) (op! @2 @4)))
5067 /* (c ? a : b) op d --> c ? (a op d) : (b op d) */
5069 (op (vec_cond:s @0 @1 @2) @3)
5070 (vec_cond @0 (op! @1 @3) (op! @2 @3)))
5072 (op @3 (vec_cond:s @0 @1 @2))
5073 (vec_cond @0 (op! @3 @1) (op! @3 @2))))
5076 (match (nop_atomic_bit_test_and_p @0 @1 @4)
5077 (bit_and (convert?@4 (ATOMIC_FETCH_OR_XOR_N @2 INTEGER_CST@0 @3))
5080 int ibit = tree_log2 (@0);
5081 int ibit2 = tree_log2 (@1);
5085 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))))))
5087 (match (nop_atomic_bit_test_and_p @0 @1 @3)
5088 (bit_and (convert?@3 (SYNC_FETCH_OR_XOR_N @2 INTEGER_CST@0))
5091 int ibit = tree_log2 (@0);
5092 int ibit2 = tree_log2 (@1);
5096 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))))))
5098 (match (nop_atomic_bit_test_and_p @0 @0 @4)
5101 (ATOMIC_FETCH_OR_XOR_N @2 (nop_convert? (lshift@0 integer_onep@5 @6)) @3))
5103 (if (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0)))))
5105 (match (nop_atomic_bit_test_and_p @0 @0 @4)
5108 (SYNC_FETCH_OR_XOR_N @2 (nop_convert? (lshift@0 integer_onep@3 @5))))
5110 (if (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0)))))
5112 (match (nop_atomic_bit_test_and_p @0 @1 @3)
5113 (bit_and@4 (convert?@3 (ATOMIC_FETCH_AND_N @2 INTEGER_CST@0 @5))
5116 int ibit = wi::exact_log2 (wi::zext (wi::bit_not (wi::to_wide (@0)),
5117 TYPE_PRECISION(type)));
5118 int ibit2 = tree_log2 (@1);
5122 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))))))
5124 (match (nop_atomic_bit_test_and_p @0 @1 @3)
5126 (convert?@3 (SYNC_FETCH_AND_AND_N @2 INTEGER_CST@0))
5129 int ibit = wi::exact_log2 (wi::zext (wi::bit_not (wi::to_wide (@0)),
5130 TYPE_PRECISION(type)));
5131 int ibit2 = tree_log2 (@1);
5135 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))))))
5137 (match (nop_atomic_bit_test_and_p @4 @0 @3)
5140 (ATOMIC_FETCH_AND_N @2 (nop_convert?@4 (bit_not (lshift@0 integer_onep@6 @7))) @5))
5142 (if (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@4)))))
5144 (match (nop_atomic_bit_test_and_p @4 @0 @3)
5147 (SYNC_FETCH_AND_AND_N @2 (nop_convert?@4 (bit_not (lshift@0 integer_onep@6 @7)))))
5149 (if (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@4)))))
5153 /* (v ? w : 0) ? a : b is just (v & w) ? a : b
5154 Currently disabled after pass lvec because ARM understands
5155 VEC_COND_EXPR<v==w,-1,0> but not a plain v==w fed to BIT_IOR_EXPR. */
5157 /* These can only be done in gimple as fold likes to convert:
5158 (CMP) & N into (CMP) ? N : 0
5159 and we try to match the same pattern again and again. */
5161 (vec_cond (vec_cond:s @0 @3 integer_zerop) @1 @2)
5162 (if (optimize_vectors_before_lowering_p () && types_match (@0, @3))
5163 (vec_cond (bit_and @0 @3) @1 @2)))
5165 (vec_cond (vec_cond:s @0 integer_all_onesp @3) @1 @2)
5166 (if (optimize_vectors_before_lowering_p () && types_match (@0, @3))
5167 (vec_cond (bit_ior @0 @3) @1 @2)))
5169 (vec_cond (vec_cond:s @0 integer_zerop @3) @1 @2)
5170 (if (optimize_vectors_before_lowering_p () && types_match (@0, @3))
5171 (vec_cond (bit_ior @0 (bit_not @3)) @2 @1)))
5173 (vec_cond (vec_cond:s @0 @3 integer_all_onesp) @1 @2)
5174 (if (optimize_vectors_before_lowering_p () && types_match (@0, @3))
5175 (vec_cond (bit_and @0 (bit_not @3)) @2 @1)))
5177 /* c1 ? c2 ? a : b : b --> (c1 & c2) ? a : b */
5179 (vec_cond @0 (vec_cond:s @1 @2 @3) @3)
5180 (if (optimize_vectors_before_lowering_p () && types_match (@0, @1))
5181 (vec_cond (bit_and @0 @1) @2 @3)))
5183 (vec_cond @0 @2 (vec_cond:s @1 @2 @3))
5184 (if (optimize_vectors_before_lowering_p () && types_match (@0, @1))
5185 (vec_cond (bit_ior @0 @1) @2 @3)))
5187 (vec_cond @0 (vec_cond:s @1 @2 @3) @2)
5188 (if (optimize_vectors_before_lowering_p () && types_match (@0, @1))
5189 (vec_cond (bit_ior (bit_not @0) @1) @2 @3)))
5191 (vec_cond @0 @3 (vec_cond:s @1 @2 @3))
5192 (if (optimize_vectors_before_lowering_p () && types_match (@0, @1))
5193 (vec_cond (bit_and (bit_not @0) @1) @2 @3)))
5196 /* Canonicalize mask ? { 0, ... } : { -1, ...} to ~mask if the mask
5197 types are compatible. */
5199 (vec_cond @0 VECTOR_CST@1 VECTOR_CST@2)
5200 (if (VECTOR_BOOLEAN_TYPE_P (type)
5201 && types_match (type, TREE_TYPE (@0)))
5202 (if (integer_zerop (@1) && integer_all_onesp (@2))
5204 (if (integer_all_onesp (@1) && integer_zerop (@2))
5207 /* A few simplifications of "a ? CST1 : CST2". */
5208 /* NOTE: Only do this on gimple as the if-chain-to-switch
5209 optimization depends on the gimple to have if statements in it. */
5212 (cond @0 INTEGER_CST@1 INTEGER_CST@2)
5214 (if (integer_zerop (@2))
5216 /* a ? 1 : 0 -> a if 0 and 1 are integral types. */
5217 (if (integer_onep (@1))
5218 (convert (convert:boolean_type_node @0)))
5219 /* a ? -1 : 0 -> -a. */
5220 (if (INTEGRAL_TYPE_P (type) && integer_all_onesp (@1))
5221 (if (TYPE_PRECISION (type) == 1)
5222 /* For signed 1-bit precision just cast bool to the type. */
5223 (convert (convert:boolean_type_node @0))
5224 (if (TREE_CODE (type) == BOOLEAN_TYPE)
5226 tree intt = build_nonstandard_integer_type (TYPE_PRECISION (type),
5227 TYPE_UNSIGNED (type));
5229 (convert (negate (convert:intt (convert:boolean_type_node @0)))))
5230 (negate (convert:type (convert:boolean_type_node @0))))))
5231 /* a ? powerof2cst : 0 -> a << (log2(powerof2cst)) */
5232 (if (INTEGRAL_TYPE_P (type) && integer_pow2p (@1))
5234 tree shift = build_int_cst (integer_type_node, tree_log2 (@1));
5236 (lshift (convert (convert:boolean_type_node @0)) { shift; })))))
5237 (if (integer_zerop (@1))
5239 /* a ? 0 : 1 -> !a. */
5240 (if (integer_onep (@2))
5241 (convert (bit_xor (convert:boolean_type_node @0) { boolean_true_node; })))
5242 /* a ? 0 : -1 -> -(!a). */
5243 (if (INTEGRAL_TYPE_P (type) && integer_all_onesp (@2))
5244 (if (TYPE_PRECISION (type) == 1)
5245 /* For signed 1-bit precision just cast bool to the type. */
5246 (convert (bit_xor (convert:boolean_type_node @0) { boolean_true_node; }))
5247 (if (TREE_CODE (type) == BOOLEAN_TYPE)
5249 tree intt = build_nonstandard_integer_type (TYPE_PRECISION (type),
5250 TYPE_UNSIGNED (type));
5252 (convert (negate (convert:intt (bit_xor (convert:boolean_type_node @0)
5253 { boolean_true_node; })))))
5254 (negate (convert:type (bit_xor (convert:boolean_type_node @0)
5255 { boolean_true_node; }))))))
5256 /* a ? 0 : powerof2cst -> (!a) << (log2(powerof2cst)) */
5257 (if (INTEGRAL_TYPE_P (type) && integer_pow2p (@2))
5259 tree shift = build_int_cst (integer_type_node, tree_log2 (@2));
5261 (lshift (convert (bit_xor (convert:boolean_type_node @0)
5262 { boolean_true_node; })) { shift; })))))))
5264 /* (a > 1) ? 0 : (cast)a is the same as (cast)(a == 1)
5265 for unsigned types. */
5267 (cond (gt @0 integer_onep@1) integer_zerop (convert? @2))
5268 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
5269 && bitwise_equal_p (@0, @2))
5270 (convert (eq @0 @1))
5274 /* (a <= 1) & (cast)a is the same as (cast)(a == 1)
5275 for unsigned types. */
5277 (bit_and:c (convert1? (le @0 integer_onep@1)) (convert2? @2))
5278 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
5279 && bitwise_equal_p (@0, @2))
5280 (convert (eq @0 @1))
5284 /* `(a == CST) & a` can be simplified to `0` or `(a == CST)` depending
5285 on the first bit of the CST. */
5287 (bit_and:c (convert@2 (eq @0 INTEGER_CST@1)) (convert? @0))
5288 (if ((wi::to_wide (@1) & 1) != 0)
5290 { build_zero_cst (type); }))
5293 # x_5 in range [cst1, cst2] where cst2 = cst1 + 1
5294 x_5 == cstN ? cst4 : cst3
5295 # op is == or != and N is 1 or 2
5296 to r_6 = x_5 + (min (cst3, cst4) - cst1) or
5297 r_6 = (min (cst3, cst4) + cst1) - x_5 depending on op, N and which
5298 of cst3 and cst4 is smaller.
5299 This was originally done by two_value_replacement in phiopt (PR 88676). */
5302 (cond (eqne SSA_NAME@0 INTEGER_CST@1) INTEGER_CST@2 INTEGER_CST@3)
5303 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
5304 && INTEGRAL_TYPE_P (type)
5305 && (wi::to_widest (@2) + 1 == wi::to_widest (@3)
5306 || wi::to_widest (@2) == wi::to_widest (@3) + 1))
5309 get_range_query (cfun)->range_of_expr (r, @0);
5310 if (r.undefined_p ())
5311 r.set_varying (TREE_TYPE (@0));
5313 wide_int min = r.lower_bound ();
5314 wide_int max = r.upper_bound ();
5317 && (wi::to_wide (@1) == min
5318 || wi::to_wide (@1) == max))
5320 tree arg0 = @2, arg1 = @3;
5322 if ((eqne == EQ_EXPR) ^ (wi::to_wide (@1) == min))
5323 std::swap (arg0, arg1);
5324 if (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type))
5325 type1 = TREE_TYPE (@0);
5328 auto prec = TYPE_PRECISION (type1);
5329 auto unsign = TYPE_UNSIGNED (type1);
5330 if (TREE_CODE (type1) == BOOLEAN_TYPE)
5331 type1 = build_nonstandard_integer_type (prec, unsign);
5332 min = wide_int::from (min, prec,
5333 TYPE_SIGN (TREE_TYPE (@0)));
5334 wide_int a = wide_int::from (wi::to_wide (arg0), prec,
5336 enum tree_code code;
5337 wi::overflow_type ovf;
5338 if (tree_int_cst_lt (arg0, arg1))
5344 /* lhs is known to be in range [min, min+1] and we want to add a
5345 to it. Check if that operation can overflow for those 2 values
5346 and if yes, force unsigned type. */
5347 wi::add (min + (wi::neg_p (a) ? 0 : 1), a, SIGNED, &ovf);
5349 type1 = unsigned_type_for (type1);
5358 /* lhs is known to be in range [min, min+1] and we want to subtract
5359 it from a. Check if that operation can overflow for those 2
5360 values and if yes, force unsigned type. */
5361 wi::sub (a, min + (wi::neg_p (min) ? 0 : 1), SIGNED, &ovf);
5363 type1 = unsigned_type_for (type1);
5366 tree arg = wide_int_to_tree (type1, a);
5368 (if (code == PLUS_EXPR)
5369 (convert (plus (convert:type1 @0) { arg; }))
5370 (convert (minus { arg; } (convert:type1 @0))))))))))
5374 (convert (cond@0 @1 INTEGER_CST@2 INTEGER_CST@3))
5375 (if (INTEGRAL_TYPE_P (type)
5376 && INTEGRAL_TYPE_P (TREE_TYPE (@0)))
5377 (cond @1 (convert @2) (convert @3))))
5379 /* Simplification moved from fold_cond_expr_with_comparison. It may also
5381 /* This pattern implements two kinds simplification:
5384 (cond (cmp (convert1? x) c1) (convert2? x) c2) -> (minmax (x c)) if:
5385 1) Conversions are type widening from smaller type.
5386 2) Const c1 equals to c2 after canonicalizing comparison.
5387 3) Comparison has tree code LT, LE, GT or GE.
5388 This specific pattern is needed when (cmp (convert x) c) may not
5389 be simplified by comparison patterns because of multiple uses of
5390 x. It also makes sense here because simplifying across multiple
5391 referred var is always benefitial for complicated cases.
5394 (cond (eq (convert1? x) c1) (convert2? x) c2) -> (cond (eq x c1) c1 c2). */
5395 (for cmp (lt le gt ge eq ne)
5397 (cond (cmp (convert1? @1) INTEGER_CST@3) (convert2? @1) INTEGER_CST@2)
5400 tree from_type = TREE_TYPE (@1);
5401 tree c1_type = TREE_TYPE (@3), c2_type = TREE_TYPE (@2);
5402 enum tree_code code = ERROR_MARK;
5404 if (INTEGRAL_TYPE_P (from_type)
5405 && int_fits_type_p (@2, from_type)
5406 && (types_match (c1_type, from_type)
5407 || (TYPE_PRECISION (c1_type) > TYPE_PRECISION (from_type)
5408 && (TYPE_UNSIGNED (from_type)
5409 || TYPE_SIGN (c1_type) == TYPE_SIGN (from_type))))
5410 && (types_match (c2_type, from_type)
5411 || (TYPE_PRECISION (c2_type) > TYPE_PRECISION (from_type)
5412 && (TYPE_UNSIGNED (from_type)
5413 || TYPE_SIGN (c2_type) == TYPE_SIGN (from_type)))))
5416 code = minmax_from_comparison (cmp, @1, @3, @1, @2);
5417 /* Can do A == C1 ? A : C2 -> A == C1 ? C1 : C2? */
5418 else if (int_fits_type_p (@3, from_type))
5422 (if (code == MAX_EXPR)
5423 (convert (max @1 (convert @2)))
5424 (if (code == MIN_EXPR)
5425 (convert (min @1 (convert @2)))
5426 (if (code == EQ_EXPR)
5427 (convert (cond (eq @1 (convert @3))
5428 (convert:from_type @3) (convert:from_type @2)))))))))
5430 /* (cond (cmp (convert? x) c1) (op x c2) c3) -> (op (minmax x c1) c2) if:
5432 1) OP is PLUS or MINUS.
5433 2) CMP is LT, LE, GT or GE.
5434 3) C3 == (C1 op C2), and computation doesn't have undefined behavior.
5436 This pattern also handles special cases like:
5438 A) Operand x is a unsigned to signed type conversion and c1 is
5439 integer zero. In this case,
5440 (signed type)x < 0 <=> x > MAX_VAL(signed type)
5441 (signed type)x >= 0 <=> x <= MAX_VAL(signed type)
5442 B) Const c1 may not equal to (C3 op' C2). In this case we also
5443 check equality for (c1+1) and (c1-1) by adjusting comparison
5446 TODO: Though signed type is handled by this pattern, it cannot be
5447 simplified at the moment because C standard requires additional
5448 type promotion. In order to match&simplify it here, the IR needs
5449 to be cleaned up by other optimizers, i.e, VRP. */
5450 (for op (plus minus)
5451 (for cmp (lt le gt ge)
5453 (cond (cmp (convert? @X) INTEGER_CST@1) (op @X INTEGER_CST@2) INTEGER_CST@3)
5454 (with { tree from_type = TREE_TYPE (@X), to_type = TREE_TYPE (@1); }
5455 (if (types_match (from_type, to_type)
5456 /* Check if it is special case A). */
5457 || (TYPE_UNSIGNED (from_type)
5458 && !TYPE_UNSIGNED (to_type)
5459 && TYPE_PRECISION (from_type) == TYPE_PRECISION (to_type)
5460 && integer_zerop (@1)
5461 && (cmp == LT_EXPR || cmp == GE_EXPR)))
5464 wi::overflow_type overflow = wi::OVF_NONE;
5465 enum tree_code code, cmp_code = cmp;
5467 wide_int c1 = wi::to_wide (@1);
5468 wide_int c2 = wi::to_wide (@2);
5469 wide_int c3 = wi::to_wide (@3);
5470 signop sgn = TYPE_SIGN (from_type);
5472 /* Handle special case A), given x of unsigned type:
5473 ((signed type)x < 0) <=> (x > MAX_VAL(signed type))
5474 ((signed type)x >= 0) <=> (x <= MAX_VAL(signed type)) */
5475 if (!types_match (from_type, to_type))
5477 if (cmp_code == LT_EXPR)
5479 if (cmp_code == GE_EXPR)
5481 c1 = wi::max_value (to_type);
5483 /* To simplify this pattern, we require c3 = (c1 op c2). Here we
5484 compute (c3 op' c2) and check if it equals to c1 with op' being
5485 the inverted operator of op. Make sure overflow doesn't happen
5486 if it is undefined. */
5487 if (op == PLUS_EXPR)
5488 real_c1 = wi::sub (c3, c2, sgn, &overflow);
5490 real_c1 = wi::add (c3, c2, sgn, &overflow);
5493 if (!overflow || !TYPE_OVERFLOW_UNDEFINED (from_type))
5495 /* Check if c1 equals to real_c1. Boundary condition is handled
5496 by adjusting comparison operation if necessary. */
5497 if (!wi::cmp (wi::sub (real_c1, 1, sgn, &overflow), c1, sgn)
5500 /* X <= Y - 1 equals to X < Y. */
5501 if (cmp_code == LE_EXPR)
5503 /* X > Y - 1 equals to X >= Y. */
5504 if (cmp_code == GT_EXPR)
5507 if (!wi::cmp (wi::add (real_c1, 1, sgn, &overflow), c1, sgn)
5510 /* X < Y + 1 equals to X <= Y. */
5511 if (cmp_code == LT_EXPR)
5513 /* X >= Y + 1 equals to X > Y. */
5514 if (cmp_code == GE_EXPR)
5517 if (code != cmp_code || !wi::cmp (real_c1, c1, sgn))
5519 if (cmp_code == LT_EXPR || cmp_code == LE_EXPR)
5521 if (cmp_code == GT_EXPR || cmp_code == GE_EXPR)
5526 (if (code == MAX_EXPR)
5527 (op (max @X { wide_int_to_tree (from_type, real_c1); })
5528 { wide_int_to_tree (from_type, c2); })
5529 (if (code == MIN_EXPR)
5530 (op (min @X { wide_int_to_tree (from_type, real_c1); })
5531 { wide_int_to_tree (from_type, c2); })))))))))
5534 /* A >= B ? A : B -> max (A, B) and friends. The code is still
5535 in fold_cond_expr_with_comparison for GENERIC folding with
5536 some extra constraints. */
5537 (for cmp (eq ne le lt unle unlt ge gt unge ungt uneq ltgt)
5539 (cond (cmp:c (nop_convert1?@c0 @0) (nop_convert2?@c1 @1))
5540 (convert3? @0) (convert4? @1))
5541 (if (!HONOR_SIGNED_ZEROS (type)
5542 && (/* Allow widening conversions of the compare operands as data. */
5543 (INTEGRAL_TYPE_P (type)
5544 && types_match (TREE_TYPE (@c0), TREE_TYPE (@0))
5545 && types_match (TREE_TYPE (@c1), TREE_TYPE (@1))
5546 && TYPE_PRECISION (TREE_TYPE (@0)) <= TYPE_PRECISION (type)
5547 && TYPE_PRECISION (TREE_TYPE (@1)) <= TYPE_PRECISION (type))
5548 /* Or sign conversions for the comparison. */
5549 || (types_match (type, TREE_TYPE (@0))
5550 && types_match (type, TREE_TYPE (@1)))))
5552 (if (cmp == EQ_EXPR)
5553 (if (VECTOR_TYPE_P (type))
5556 (if (cmp == NE_EXPR)
5557 (if (VECTOR_TYPE_P (type))
5560 (if (cmp == LE_EXPR || cmp == UNLE_EXPR || cmp == LT_EXPR || cmp == UNLT_EXPR)
5561 (if (!HONOR_NANS (type))
5562 (if (VECTOR_TYPE_P (type))
5563 (view_convert (min @c0 @c1))
5564 (convert (min @c0 @c1)))))
5565 (if (cmp == GE_EXPR || cmp == UNGE_EXPR || cmp == GT_EXPR || cmp == UNGT_EXPR)
5566 (if (!HONOR_NANS (type))
5567 (if (VECTOR_TYPE_P (type))
5568 (view_convert (max @c0 @c1))
5569 (convert (max @c0 @c1)))))
5570 (if (cmp == UNEQ_EXPR)
5571 (if (!HONOR_NANS (type))
5572 (if (VECTOR_TYPE_P (type))
5575 (if (cmp == LTGT_EXPR)
5576 (if (!HONOR_NANS (type))
5577 (if (VECTOR_TYPE_P (type))
5579 (convert @c0))))))))
5582 (for cnd (cond vec_cond)
5583 /* (a != b) ? (a - b) : 0 -> (a - b) */
5585 (cnd (ne:c @0 @1) (minus@2 @0 @1) integer_zerop)
5587 /* (a != b) ? (a ^ b) : 0 -> (a ^ b) */
5589 (cnd (ne:c @0 @1) (bit_xor:c@2 @0 @1) integer_zerop)
5591 /* (a != b) ? (a & b) : a -> (a & b) */
5592 /* (a != b) ? (a | b) : a -> (a | b) */
5593 /* (a != b) ? min(a,b) : a -> min(a,b) */
5594 /* (a != b) ? max(a,b) : a -> max(a,b) */
5595 (for op (bit_and bit_ior min max)
5597 (cnd (ne:c @0 @1) (op:c@2 @0 @1) @0)
5599 /* (a != b) ? (a * b) : (a * a) -> (a * b) */
5600 /* (a != b) ? (a + b) : (a + a) -> (a + b) */
5603 (cnd (ne:c @0 @1) (op@2 @0 @1) (op @0 @0))
5604 (if (ANY_INTEGRAL_TYPE_P (type))
5606 /* (a != b) ? (a + b) : (2 * a) -> (a + b) */
5608 (cnd (ne:c @0 @1) (plus@2 @0 @1) (mult @0 uniform_integer_cst_p@3))
5609 (if (wi::to_wide (uniform_integer_cst_p (@3)) == 2)
5613 /* These was part of minmax phiopt. */
5614 /* Optimize (a CMP b) ? minmax<a, c> : minmax<b, c>
5615 to minmax<min/max<a, b>, c> */
5616 (for minmax (min max)
5617 (for cmp (lt le gt ge ne)
5619 (cond (cmp:c @1 @3) (minmax:c @1 @4) (minmax:c @2 @4))
5622 tree_code code = minmax_from_comparison (cmp, @1, @2, @1, @3);
5624 (if (code == MIN_EXPR)
5625 (minmax (min @1 @2) @4)
5626 (if (code == MAX_EXPR)
5627 (minmax (max @1 @2) @4)))))))
5629 /* Optimize (a CMP CST1) ? max<a,CST2> : a */
5630 (for cmp (gt ge lt le)
5631 minmax (min min max max)
5633 (cond (cmp:c @0 @1) (minmax:c@2 @0 @3) @4)
5636 tree_code code = minmax_from_comparison (cmp, @0, @1, @0, @4);
5638 (if ((cmp == LT_EXPR || cmp == LE_EXPR)
5640 && integer_nonzerop (fold_build2 (LE_EXPR, boolean_type_node, @3, @4)))
5642 (if ((cmp == GT_EXPR || cmp == GE_EXPR)
5644 && integer_nonzerop (fold_build2 (GE_EXPR, boolean_type_node, @3, @4)))
5648 /* These patterns should be after min/max detection as simplifications
5649 of `(type)(zero_one ==/!= 0)` to `(type)(zero_one)`
5650 and `(type)(zero_one^1)` are not done yet. See PR 110637.
5651 Even without those, reaching min/max/and/ior faster is better. */
5653 (cond @0 zero_one_valued_p@1 zero_one_valued_p@2)
5655 /* bool0 ? bool1 : 0 -> bool0 & bool1 */
5656 (if (integer_zerop (@2))
5657 (bit_and (convert @0) @1))
5658 /* bool0 ? 0 : bool2 -> (bool0^1) & bool2 */
5659 (if (integer_zerop (@1))
5660 (bit_and (bit_xor (convert @0) { build_one_cst (type); } ) @2))
5661 /* bool0 ? 1 : bool2 -> bool0 | bool2 */
5662 (if (integer_onep (@1))
5663 (bit_ior (convert @0) @2))
5664 /* bool0 ? bool1 : 1 -> (bool0^1) | bool1 */
5665 (if (integer_onep (@2))
5666 (bit_ior (bit_xor (convert @0) @2) @1))
5671 /* X != C1 ? -X : C2 simplifies to -X when -C1 == C2. */
5673 (cond (ne @0 INTEGER_CST@1) (negate@3 @0) INTEGER_CST@2)
5674 (if (!TYPE_SATURATING (type)
5675 && (TYPE_OVERFLOW_WRAPS (type)
5676 || !wi::only_sign_bit_p (wi::to_wide (@1)))
5677 && wi::eq_p (wi::neg (wi::to_wide (@1)), wi::to_wide (@2)))
5680 /* X != C1 ? ~X : C2 simplifies to ~X when ~C1 == C2. */
5682 (cond (ne @0 INTEGER_CST@1) (bit_not@3 @0) INTEGER_CST@2)
5683 (if (wi::eq_p (wi::bit_not (wi::to_wide (@1)), wi::to_wide (@2)))
5686 /* X != C1 ? abs(X) : C2 simplifies to abs(x) when abs(C1) == C2. */
5689 (cond (ne @0 INTEGER_CST@1) (op@3 @0) INTEGER_CST@2)
5690 (if (wi::abs (wi::to_wide (@1)) == wi::to_wide (@2))
5691 (if (op != ABSU_EXPR && wi::only_sign_bit_p (wi::to_wide (@1)))
5692 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
5693 (convert (absu:utype @0)))
5696 /* (X + 1) > Y ? -X : 1 simplifies to X >= Y ? -X : 1 when
5697 X is unsigned, as when X + 1 overflows, X is -1, so -X == 1. */
5699 (cond (gt (plus @0 integer_onep) @1) (negate @0) integer_onep@2)
5700 (if (TYPE_UNSIGNED (type))
5701 (cond (ge @0 @1) (negate @0) @2)))
5703 (for cnd (cond vec_cond)
5704 /* A ? B : (A ? X : C) -> A ? B : C. */
5706 (cnd @0 (cnd @0 @1 @2) @3)
5709 (cnd @0 @1 (cnd @0 @2 @3))
5711 /* A ? B : (!A ? C : X) -> A ? B : C. */
5712 /* ??? This matches embedded conditions open-coded because genmatch
5713 would generate matching code for conditions in separate stmts only.
5714 The following is still important to merge then and else arm cases
5715 from if-conversion. */
5717 (cnd @0 @1 (cnd @2 @3 @4))
5718 (if (inverse_conditions_p (@0, @2))
5721 (cnd @0 (cnd @1 @2 @3) @4)
5722 (if (inverse_conditions_p (@0, @1))
5725 /* A ? B : B -> B. */
5730 /* !A ? B : C -> A ? C : B. */
5732 (cnd (logical_inverted_value truth_valued_p@0) @1 @2)
5735 /* abs/negative simplifications moved from fold_cond_expr_with_comparison.
5737 None of these transformations work for modes with signed
5738 zeros. If A is +/-0, the first two transformations will
5739 change the sign of the result (from +0 to -0, or vice
5740 versa). The last four will fix the sign of the result,
5741 even though the original expressions could be positive or
5742 negative, depending on the sign of A.
5744 Note that all these transformations are correct if A is
5745 NaN, since the two alternatives (A and -A) are also NaNs. */
5747 (for cnd (cond vec_cond)
5748 /* A == 0 ? A : -A same as -A */
5751 (cnd (cmp @0 zerop) @2 (negate@1 @2))
5752 (if (!HONOR_SIGNED_ZEROS (type)
5753 && bitwise_equal_p (@0, @2))
5756 (cnd (cmp @0 zerop) zerop (negate@1 @2))
5757 (if (!HONOR_SIGNED_ZEROS (type)
5758 && bitwise_equal_p (@0, @2))
5761 /* A != 0 ? A : -A same as A */
5764 (cnd (cmp @0 zerop) @1 (negate @1))
5765 (if (!HONOR_SIGNED_ZEROS (type)
5766 && bitwise_equal_p (@0, @1))
5769 (cnd (cmp @0 zerop) @1 integer_zerop)
5770 (if (!HONOR_SIGNED_ZEROS (type)
5771 && bitwise_equal_p (@0, @1))
5774 /* A >=/> 0 ? A : -A same as abs (A) */
5777 (cnd (cmp @0 zerop) @1 (negate @1))
5778 (if (!HONOR_SIGNED_ZEROS (TREE_TYPE(@0))
5779 && !TYPE_UNSIGNED (TREE_TYPE(@0))
5780 && bitwise_equal_p (@0, @1))
5781 (if (TYPE_UNSIGNED (type))
5784 /* A <=/< 0 ? A : -A same as -abs (A) */
5787 (cnd (cmp @0 zerop) @1 (negate @1))
5788 (if (!HONOR_SIGNED_ZEROS (TREE_TYPE(@0))
5789 && !TYPE_UNSIGNED (TREE_TYPE(@0))
5790 && bitwise_equal_p (@0, @1))
5791 (if ((ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
5792 && !TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
5793 || TYPE_UNSIGNED (type))
5795 tree utype = unsigned_type_for (TREE_TYPE(@0));
5797 (convert (negate (absu:utype @0))))
5798 (negate (abs @0)))))
5801 /* (A - B) == 0 ? (A - B) : (B - A) same as (B - A) */
5804 (cnd (cmp (minus@0 @1 @2) zerop) @0 (minus@3 @2 @1))
5805 (if (!HONOR_SIGNED_ZEROS (type))
5808 (cnd (cmp (minus@0 @1 @2) integer_zerop) integer_zerop (minus@3 @2 @1))
5811 /* (A - B) != 0 ? (A - B) : (B - A) same as (A - B) */
5814 (cnd (cmp (minus@0 @1 @2) zerop) @0 (minus @2 @1))
5815 (if (!HONOR_SIGNED_ZEROS (type))
5818 (cnd (cmp (minus@0 @1 @2) integer_zerop) @0 integer_zerop)
5821 /* (A - B) >=/> 0 ? (A - B) : (B - A) same as abs (A - B) */
5824 (cnd (cmp (minus@0 @1 @2) zerop) @0 (minus @2 @1))
5825 (if (!HONOR_SIGNED_ZEROS (type)
5826 && !TYPE_UNSIGNED (type))
5828 /* (A - B) <=/< 0 ? (A - B) : (B - A) same as -abs (A - B) */
5831 (cnd (cmp (minus@0 @1 @2) zerop) @0 (minus @2 @1))
5832 (if (!HONOR_SIGNED_ZEROS (type)
5833 && !TYPE_UNSIGNED (type))
5834 (if (ANY_INTEGRAL_TYPE_P (type)
5835 && !TYPE_OVERFLOW_WRAPS (type))
5837 tree utype = unsigned_type_for (type);
5839 (convert (negate (absu:utype @0))))
5840 (negate (abs @0)))))
5844 /* -(type)!A -> (type)A - 1. */
5846 (negate (convert?:s (logical_inverted_value:s @0)))
5847 (if (INTEGRAL_TYPE_P (type)
5848 && TREE_CODE (type) != BOOLEAN_TYPE
5849 && TYPE_PRECISION (type) > 1
5850 && TREE_CODE (@0) == SSA_NAME
5851 && ssa_name_has_boolean_range (@0))
5852 (plus (convert:type @0) { build_all_ones_cst (type); })))
5854 /* A + (B vcmp C ? 1 : 0) -> A - (B vcmp C ? -1 : 0), since vector comparisons
5855 return all -1 or all 0 results. */
5856 /* ??? We could instead convert all instances of the vec_cond to negate,
5857 but that isn't necessarily a win on its own. */
5859 (plus:c @3 (view_convert? (vec_cond:s @0 integer_each_onep@1 integer_zerop@2)))
5860 (if (VECTOR_TYPE_P (type)
5861 && known_eq (TYPE_VECTOR_SUBPARTS (type),
5862 TYPE_VECTOR_SUBPARTS (TREE_TYPE (@1)))
5863 && (TYPE_MODE (TREE_TYPE (type))
5864 == TYPE_MODE (TREE_TYPE (TREE_TYPE (@1)))))
5865 (minus @3 (view_convert (vec_cond @0 (negate @1) @2)))))
5867 /* ... likewise A - (B vcmp C ? 1 : 0) -> A + (B vcmp C ? -1 : 0). */
5869 (minus @3 (view_convert? (vec_cond:s @0 integer_each_onep@1 integer_zerop@2)))
5870 (if (VECTOR_TYPE_P (type)
5871 && known_eq (TYPE_VECTOR_SUBPARTS (type),
5872 TYPE_VECTOR_SUBPARTS (TREE_TYPE (@1)))
5873 && (TYPE_MODE (TREE_TYPE (type))
5874 == TYPE_MODE (TREE_TYPE (TREE_TYPE (@1)))))
5875 (plus @3 (view_convert (vec_cond @0 (negate @1) @2)))))
5878 /* Simplifications of comparisons. */
5880 /* See if we can reduce the magnitude of a constant involved in a
5881 comparison by changing the comparison code. This is a canonicalization
5882 formerly done by maybe_canonicalize_comparison_1. */
5886 (cmp @0 uniform_integer_cst_p@1)
5887 (with { tree cst = uniform_integer_cst_p (@1); }
5888 (if (tree_int_cst_sgn (cst) == -1)
5889 (acmp @0 { build_uniform_cst (TREE_TYPE (@1),
5890 wide_int_to_tree (TREE_TYPE (cst),
5896 (cmp @0 uniform_integer_cst_p@1)
5897 (with { tree cst = uniform_integer_cst_p (@1); }
5898 (if (tree_int_cst_sgn (cst) == 1)
5899 (acmp @0 { build_uniform_cst (TREE_TYPE (@1),
5900 wide_int_to_tree (TREE_TYPE (cst),
5901 wi::to_wide (cst) - 1)); })))))
5903 /* We can simplify a logical negation of a comparison to the
5904 inverted comparison. As we cannot compute an expression
5905 operator using invert_tree_comparison we have to simulate
5906 that with expression code iteration. */
5907 (for cmp (tcc_comparison)
5908 icmp (inverted_tcc_comparison)
5909 ncmp (inverted_tcc_comparison_with_nans)
5910 /* Ideally we'd like to combine the following two patterns
5911 and handle some more cases by using
5912 (logical_inverted_value (cmp @0 @1))
5913 here but for that genmatch would need to "inline" that.
5914 For now implement what forward_propagate_comparison did. */
5916 (bit_not (cmp @0 @1))
5917 (if (VECTOR_TYPE_P (type)
5918 || (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1))
5919 /* Comparison inversion may be impossible for trapping math,
5920 invert_tree_comparison will tell us. But we can't use
5921 a computed operator in the replacement tree thus we have
5922 to play the trick below. */
5923 (with { enum tree_code ic = invert_tree_comparison
5924 (cmp, HONOR_NANS (@0)); }
5930 (bit_xor (cmp @0 @1) integer_truep)
5931 (with { enum tree_code ic = invert_tree_comparison
5932 (cmp, HONOR_NANS (@0)); }
5937 /* The following bits are handled by fold_binary_op_with_conditional_arg. */
5939 (ne (cmp@2 @0 @1) integer_zerop)
5940 (if (types_match (type, TREE_TYPE (@2)))
5943 (eq (cmp@2 @0 @1) integer_truep)
5944 (if (types_match (type, TREE_TYPE (@2)))
5947 (ne (cmp@2 @0 @1) integer_truep)
5948 (if (types_match (type, TREE_TYPE (@2)))
5949 (with { enum tree_code ic = invert_tree_comparison
5950 (cmp, HONOR_NANS (@0)); }
5956 (eq (cmp@2 @0 @1) integer_zerop)
5957 (if (types_match (type, TREE_TYPE (@2)))
5958 (with { enum tree_code ic = invert_tree_comparison
5959 (cmp, HONOR_NANS (@0)); }
5965 /* Transform comparisons of the form X - Y CMP 0 to X CMP Y.
5966 ??? The transformation is valid for the other operators if overflow
5967 is undefined for the type, but performing it here badly interacts
5968 with the transformation in fold_cond_expr_with_comparison which
5969 attempts to synthetize ABS_EXPR. */
5971 (for sub (minus pointer_diff)
5973 (cmp (sub@2 @0 @1) integer_zerop)
5974 (if (single_use (@2))
5977 /* Simplify (x < 0) ^ (y < 0) to (x ^ y) < 0 and
5978 (x >= 0) ^ (y >= 0) to (x ^ y) < 0. */
5981 (bit_xor (cmp:s @0 integer_zerop) (cmp:s @1 integer_zerop))
5982 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
5983 && !TYPE_UNSIGNED (TREE_TYPE (@0))
5984 && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
5985 (lt (bit_xor @0 @1) { build_zero_cst (TREE_TYPE (@0)); }))))
5986 /* Simplify (x < 0) ^ (y >= 0) to (x ^ y) >= 0 and
5987 (x >= 0) ^ (y < 0) to (x ^ y) >= 0. */
5989 (bit_xor:c (lt:s @0 integer_zerop) (ge:s @1 integer_zerop))
5990 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
5991 && !TYPE_UNSIGNED (TREE_TYPE (@0))
5992 && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
5993 (ge (bit_xor @0 @1) { build_zero_cst (TREE_TYPE (@0)); })))
5995 /* Transform comparisons of the form X * C1 CMP 0 to X CMP 0 in the
5996 signed arithmetic case. That form is created by the compiler
5997 often enough for folding it to be of value. One example is in
5998 computing loop trip counts after Operator Strength Reduction. */
5999 (for cmp (simple_comparison)
6000 scmp (swapped_simple_comparison)
6002 (cmp (mult@3 @0 INTEGER_CST@1) integer_zerop@2)
6003 /* Handle unfolded multiplication by zero. */
6004 (if (integer_zerop (@1))
6006 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
6007 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
6009 /* If @1 is negative we swap the sense of the comparison. */
6010 (if (tree_int_cst_sgn (@1) < 0)
6014 /* For integral types with undefined overflow fold
6015 x * C1 == C2 into x == C2 / C1 or false.
6016 If overflow wraps and C1 is odd, simplify to x == C2 / C1 in the ring
6020 (cmp (mult @0 INTEGER_CST@1) INTEGER_CST@2)
6021 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6022 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
6023 && wi::to_wide (@1) != 0)
6024 (with { widest_int quot; }
6025 (if (wi::multiple_of_p (wi::to_widest (@2), wi::to_widest (@1),
6026 TYPE_SIGN (TREE_TYPE (@0)), "))
6027 (cmp @0 { wide_int_to_tree (TREE_TYPE (@0), quot); })
6028 { constant_boolean_node (cmp == NE_EXPR, type); }))
6029 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6030 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))
6031 && (wi::bit_and (wi::to_wide (@1), 1) == 1))
6034 tree itype = TREE_TYPE (@0);
6035 int p = TYPE_PRECISION (itype);
6036 wide_int m = wi::one (p + 1) << p;
6037 wide_int a = wide_int::from (wi::to_wide (@1), p + 1, UNSIGNED);
6038 wide_int i = wide_int::from (wi::mod_inv (a, m),
6039 p, TYPE_SIGN (itype));
6040 wide_int_to_tree (itype, wi::mul (i, wi::to_wide (@2)));
6043 /* Simplify comparison of something with itself. For IEEE
6044 floating-point, we can only do some of these simplifications. */
6048 (if (! FLOAT_TYPE_P (TREE_TYPE (@0))
6049 || ! tree_expr_maybe_nan_p (@0))
6050 { constant_boolean_node (true, type); }
6052 /* With -ftrapping-math conversion to EQ loses an exception. */
6053 && (! FLOAT_TYPE_P (TREE_TYPE (@0))
6054 || ! flag_trapping_math))
6060 || ! FLOAT_TYPE_P (TREE_TYPE (@0))
6061 || ! tree_expr_maybe_nan_p (@0))
6062 { constant_boolean_node (false, type); })))
6063 (for cmp (unle unge uneq)
6066 { constant_boolean_node (true, type); }))
6067 (for cmp (unlt ungt)
6073 (if (!flag_trapping_math || !tree_expr_maybe_nan_p (@0))
6074 { constant_boolean_node (false, type); }))
6076 /* x == ~x -> false */
6077 /* x != ~x -> true */
6080 (cmp:c @0 (bit_not @0))
6081 { constant_boolean_node (cmp == NE_EXPR, type); }))
6083 /* Fold ~X op ~Y as Y op X. */
6084 (for cmp (simple_comparison)
6086 (cmp (nop_convert1?@4 (bit_not@2 @0)) (nop_convert2? (bit_not@3 @1)))
6087 (if (single_use (@2) && single_use (@3))
6088 (with { tree otype = TREE_TYPE (@4); }
6089 (cmp (convert:otype @1) (convert:otype @0))))))
6091 /* Fold ~X op C as X op' ~C, where op' is the swapped comparison. */
6092 (for cmp (simple_comparison)
6093 scmp (swapped_simple_comparison)
6095 (cmp (nop_convert? (bit_not@2 @0)) CONSTANT_CLASS_P@1)
6096 (if (single_use (@2)
6097 && (TREE_CODE (@1) == INTEGER_CST || TREE_CODE (@1) == VECTOR_CST))
6098 (with { tree otype = TREE_TYPE (@1); }
6099 (scmp (convert:otype @0) (bit_not @1))))))
6101 (for cmp (simple_comparison)
6104 /* IEEE doesn't distinguish +0 and -0 in comparisons. */
6106 /* a CMP (-0) -> a CMP 0 */
6107 (if (REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@1)))
6108 (cmp @0 { build_real (TREE_TYPE (@1), dconst0); }))
6109 /* (-0) CMP b -> 0 CMP b. */
6110 (if (TREE_CODE (@0) == REAL_CST
6111 && REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@0)))
6112 (cmp { build_real (TREE_TYPE (@0), dconst0); } @1))
6113 /* x != NaN is always true, other ops are always false. */
6114 (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
6115 && (cmp == EQ_EXPR || cmp == NE_EXPR || !flag_trapping_math)
6116 && !tree_expr_signaling_nan_p (@1)
6117 && !tree_expr_maybe_signaling_nan_p (@0))
6118 { constant_boolean_node (cmp == NE_EXPR, type); })
6119 /* NaN != y is always true, other ops are always false. */
6120 (if (TREE_CODE (@0) == REAL_CST
6121 && REAL_VALUE_ISNAN (TREE_REAL_CST (@0))
6122 && (cmp == EQ_EXPR || cmp == NE_EXPR || !flag_trapping_math)
6123 && !tree_expr_signaling_nan_p (@0)
6124 && !tree_expr_signaling_nan_p (@1))
6125 { constant_boolean_node (cmp == NE_EXPR, type); })
6126 /* Fold comparisons against infinity. */
6127 (if (REAL_VALUE_ISINF (TREE_REAL_CST (@1))
6128 && MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (@1))))
6131 REAL_VALUE_TYPE max;
6132 enum tree_code code = cmp;
6133 bool neg = REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1));
6135 code = swap_tree_comparison (code);
6138 /* x > +Inf is always false, if we ignore NaNs or exceptions. */
6139 (if (code == GT_EXPR
6140 && !(HONOR_NANS (@0) && flag_trapping_math))
6141 { constant_boolean_node (false, type); })
6142 (if (code == LE_EXPR)
6143 /* x <= +Inf is always true, if we don't care about NaNs. */
6144 (if (! HONOR_NANS (@0))
6145 { constant_boolean_node (true, type); }
6146 /* x <= +Inf is the same as x == x, i.e. !isnan(x), but this loses
6147 an "invalid" exception. */
6148 (if (!flag_trapping_math)
6150 /* x == +Inf and x >= +Inf are always equal to x > DBL_MAX, but
6151 for == this introduces an exception for x a NaN. */
6152 (if ((code == EQ_EXPR && !(HONOR_NANS (@0) && flag_trapping_math))
6154 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
6156 (lt @0 { build_real (TREE_TYPE (@0), max); })
6157 (gt @0 { build_real (TREE_TYPE (@0), max); }))))
6158 /* x < +Inf is always equal to x <= DBL_MAX. */
6159 (if (code == LT_EXPR)
6160 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
6162 (ge @0 { build_real (TREE_TYPE (@0), max); })
6163 (le @0 { build_real (TREE_TYPE (@0), max); }))))
6164 /* x != +Inf is always equal to !(x > DBL_MAX), but this introduces
6165 an exception for x a NaN so use an unordered comparison. */
6166 (if (code == NE_EXPR)
6167 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
6168 (if (! HONOR_NANS (@0))
6170 (ge @0 { build_real (TREE_TYPE (@0), max); })
6171 (le @0 { build_real (TREE_TYPE (@0), max); }))
6173 (unge @0 { build_real (TREE_TYPE (@0), max); })
6174 (unle @0 { build_real (TREE_TYPE (@0), max); }))))))))))
6176 /* If this is a comparison of a real constant with a PLUS_EXPR
6177 or a MINUS_EXPR of a real constant, we can convert it into a
6178 comparison with a revised real constant as long as no overflow
6179 occurs when unsafe_math_optimizations are enabled. */
6180 (if (flag_unsafe_math_optimizations)
6181 (for op (plus minus)
6183 (cmp (op @0 REAL_CST@1) REAL_CST@2)
6186 tree tem = const_binop (op == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR,
6187 TREE_TYPE (@1), @2, @1);
6189 (if (tem && !TREE_OVERFLOW (tem))
6190 (cmp @0 { tem; }))))))
6192 /* Likewise, we can simplify a comparison of a real constant with
6193 a MINUS_EXPR whose first operand is also a real constant, i.e.
6194 (c1 - x) < c2 becomes x > c1-c2. Reordering is allowed on
6195 floating-point types only if -fassociative-math is set. */
6196 (if (flag_associative_math)
6198 (cmp (minus REAL_CST@0 @1) REAL_CST@2)
6199 (with { tree tem = const_binop (MINUS_EXPR, TREE_TYPE (@1), @0, @2); }
6200 (if (tem && !TREE_OVERFLOW (tem))
6201 (cmp { tem; } @1)))))
6203 /* Fold comparisons against built-in math functions. */
6204 (if (flag_unsafe_math_optimizations && ! flag_errno_math)
6207 (cmp (sq @0) REAL_CST@1)
6209 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
6211 /* sqrt(x) < y is always false, if y is negative. */
6212 (if (cmp == EQ_EXPR || cmp == LT_EXPR || cmp == LE_EXPR)
6213 { constant_boolean_node (false, type); })
6214 /* sqrt(x) > y is always true, if y is negative and we
6215 don't care about NaNs, i.e. negative values of x. */
6216 (if (cmp == NE_EXPR || !HONOR_NANS (@0))
6217 { constant_boolean_node (true, type); })
6218 /* sqrt(x) > y is the same as x >= 0, if y is negative. */
6219 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })))
6220 (if (real_equal (TREE_REAL_CST_PTR (@1), &dconst0))
6222 /* sqrt(x) < 0 is always false. */
6223 (if (cmp == LT_EXPR)
6224 { constant_boolean_node (false, type); })
6225 /* sqrt(x) >= 0 is always true if we don't care about NaNs. */
6226 (if (cmp == GE_EXPR && !HONOR_NANS (@0))
6227 { constant_boolean_node (true, type); })
6228 /* sqrt(x) <= 0 -> x == 0. */
6229 (if (cmp == LE_EXPR)
6231 /* Otherwise sqrt(x) cmp 0 -> x cmp 0. Here cmp can be >=, >,
6232 == or !=. In the last case:
6234 (sqrt(x) != 0) == (NaN != 0) == true == (x != 0)
6236 if x is negative or NaN. Due to -funsafe-math-optimizations,
6237 the results for other x follow from natural arithmetic. */
6239 (if ((cmp == LT_EXPR
6243 && !REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
6244 /* Give up for -frounding-math. */
6245 && !HONOR_SIGN_DEPENDENT_ROUNDING (TREE_TYPE (@0)))
6249 enum tree_code ncmp = cmp;
6250 const real_format *fmt
6251 = REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (@0)));
6252 real_arithmetic (&c2, MULT_EXPR,
6253 &TREE_REAL_CST (@1), &TREE_REAL_CST (@1));
6254 real_convert (&c2, fmt, &c2);
6255 /* See PR91734: if c2 is inexact and sqrt(c2) < c (or sqrt(c2) >= c),
6256 then change LT_EXPR into LE_EXPR or GE_EXPR into GT_EXPR. */
6257 if (!REAL_VALUE_ISINF (c2))
6259 tree c3 = fold_const_call (CFN_SQRT, TREE_TYPE (@0),
6260 build_real (TREE_TYPE (@0), c2));
6261 if (c3 == NULL_TREE || TREE_CODE (c3) != REAL_CST)
6263 else if ((cmp == LT_EXPR || cmp == GE_EXPR)
6264 && real_less (&TREE_REAL_CST (c3), &TREE_REAL_CST (@1)))
6265 ncmp = cmp == LT_EXPR ? LE_EXPR : GT_EXPR;
6266 else if ((cmp == LE_EXPR || cmp == GT_EXPR)
6267 && real_less (&TREE_REAL_CST (@1), &TREE_REAL_CST (c3)))
6268 ncmp = cmp == LE_EXPR ? LT_EXPR : GE_EXPR;
6271 /* With rounding to even, sqrt of up to 3 different values
6272 gives the same normal result, so in some cases c2 needs
6274 REAL_VALUE_TYPE c2alt, tow;
6275 if (cmp == LT_EXPR || cmp == GE_EXPR)
6279 real_nextafter (&c2alt, fmt, &c2, &tow);
6280 real_convert (&c2alt, fmt, &c2alt);
6281 if (REAL_VALUE_ISINF (c2alt))
6285 c3 = fold_const_call (CFN_SQRT, TREE_TYPE (@0),
6286 build_real (TREE_TYPE (@0), c2alt));
6287 if (c3 == NULL_TREE || TREE_CODE (c3) != REAL_CST)
6289 else if (real_equal (&TREE_REAL_CST (c3),
6290 &TREE_REAL_CST (@1)))
6296 (if (cmp == GT_EXPR || cmp == GE_EXPR)
6297 (if (REAL_VALUE_ISINF (c2))
6298 /* sqrt(x) > y is x == +Inf, when y is very large. */
6299 (if (HONOR_INFINITIES (@0))
6300 (eq @0 { build_real (TREE_TYPE (@0), c2); })
6301 { constant_boolean_node (false, type); })
6302 /* sqrt(x) > c is the same as x > c*c. */
6303 (if (ncmp != ERROR_MARK)
6304 (if (ncmp == GE_EXPR)
6305 (ge @0 { build_real (TREE_TYPE (@0), c2); })
6306 (gt @0 { build_real (TREE_TYPE (@0), c2); }))))
6307 /* else if (cmp == LT_EXPR || cmp == LE_EXPR) */
6308 (if (REAL_VALUE_ISINF (c2))
6310 /* sqrt(x) < y is always true, when y is a very large
6311 value and we don't care about NaNs or Infinities. */
6312 (if (! HONOR_NANS (@0) && ! HONOR_INFINITIES (@0))
6313 { constant_boolean_node (true, type); })
6314 /* sqrt(x) < y is x != +Inf when y is very large and we
6315 don't care about NaNs. */
6316 (if (! HONOR_NANS (@0))
6317 (ne @0 { build_real (TREE_TYPE (@0), c2); }))
6318 /* sqrt(x) < y is x >= 0 when y is very large and we
6319 don't care about Infinities. */
6320 (if (! HONOR_INFINITIES (@0))
6321 (ge @0 { build_real (TREE_TYPE (@0), dconst0); }))
6322 /* sqrt(x) < y is x >= 0 && x != +Inf, when y is large. */
6325 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
6326 (ne @0 { build_real (TREE_TYPE (@0), c2); }))))
6327 /* sqrt(x) < c is the same as x < c*c, if we ignore NaNs. */
6328 (if (ncmp != ERROR_MARK && ! HONOR_NANS (@0))
6329 (if (ncmp == LT_EXPR)
6330 (lt @0 { build_real (TREE_TYPE (@0), c2); })
6331 (le @0 { build_real (TREE_TYPE (@0), c2); }))
6332 /* sqrt(x) < c is the same as x >= 0 && x < c*c. */
6333 (if (ncmp != ERROR_MARK && GENERIC)
6334 (if (ncmp == LT_EXPR)
6336 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
6337 (lt @0 { build_real (TREE_TYPE (@0), c2); }))
6339 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
6340 (le @0 { build_real (TREE_TYPE (@0), c2); })))))))))))
6341 /* Transform sqrt(x) cmp sqrt(y) -> x cmp y. */
6343 (cmp (sq @0) (sq @1))
6344 (if (! HONOR_NANS (@0))
6347 /* Optimize various special cases of (FTYPE) N CMP (FTYPE) M. */
6348 (for cmp (lt le eq ne ge gt unordered ordered unlt unle ungt unge uneq ltgt)
6349 icmp (lt le eq ne ge gt unordered ordered lt le gt ge eq ne)
6351 (cmp (float@0 @1) (float @2))
6352 (if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (@0))
6353 && ! DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0)))
6356 format_helper fmt (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (@0))));
6357 tree type1 = TREE_TYPE (@1);
6358 bool type1_signed_p = TYPE_SIGN (type1) == SIGNED;
6359 tree type2 = TREE_TYPE (@2);
6360 bool type2_signed_p = TYPE_SIGN (type2) == SIGNED;
6362 (if (fmt.can_represent_integral_type_p (type1)
6363 && fmt.can_represent_integral_type_p (type2))
6364 (if (cmp == ORDERED_EXPR || cmp == UNORDERED_EXPR)
6365 { constant_boolean_node (cmp == ORDERED_EXPR, type); }
6366 (if (TYPE_PRECISION (type1) > TYPE_PRECISION (type2)
6367 && type1_signed_p >= type2_signed_p)
6368 (icmp @1 (convert @2))
6369 (if (TYPE_PRECISION (type1) < TYPE_PRECISION (type2)
6370 && type1_signed_p <= type2_signed_p)
6371 (icmp (convert:type2 @1) @2)
6372 (if (TYPE_PRECISION (type1) == TYPE_PRECISION (type2)
6373 && type1_signed_p == type2_signed_p)
6374 (icmp @1 @2))))))))))
6376 /* Optimize various special cases of (FTYPE) N CMP CST. */
6377 (for cmp (lt le eq ne ge gt)
6378 icmp (le le eq ne ge ge)
6380 (cmp (float @0) REAL_CST@1)
6381 (if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (@1))
6382 && ! DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1)))
6385 tree itype = TREE_TYPE (@0);
6386 format_helper fmt (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (@1))));
6387 const REAL_VALUE_TYPE *cst = TREE_REAL_CST_PTR (@1);
6388 /* Be careful to preserve any potential exceptions due to
6389 NaNs. qNaNs are ok in == or != context.
6390 TODO: relax under -fno-trapping-math or
6391 -fno-signaling-nans. */
6393 = real_isnan (cst) && (cst->signalling
6394 || (cmp != EQ_EXPR && cmp != NE_EXPR));
6396 /* TODO: allow non-fitting itype and SNaNs when
6397 -fno-trapping-math. */
6398 (if (fmt.can_represent_integral_type_p (itype) && ! exception_p)
6401 signop isign = TYPE_SIGN (itype);
6402 REAL_VALUE_TYPE imin, imax;
6403 real_from_integer (&imin, fmt, wi::min_value (itype), isign);
6404 real_from_integer (&imax, fmt, wi::max_value (itype), isign);
6406 REAL_VALUE_TYPE icst;
6407 if (cmp == GT_EXPR || cmp == GE_EXPR)
6408 real_ceil (&icst, fmt, cst);
6409 else if (cmp == LT_EXPR || cmp == LE_EXPR)
6410 real_floor (&icst, fmt, cst);
6412 real_trunc (&icst, fmt, cst);
6414 bool cst_int_p = !real_isnan (cst) && real_identical (&icst, cst);
6416 bool overflow_p = false;
6418 = real_to_integer (&icst, &overflow_p, TYPE_PRECISION (itype));
6421 /* Optimize cases when CST is outside of ITYPE's range. */
6422 (if (real_compare (LT_EXPR, cst, &imin))
6423 { constant_boolean_node (cmp == GT_EXPR || cmp == GE_EXPR || cmp == NE_EXPR,
6425 (if (real_compare (GT_EXPR, cst, &imax))
6426 { constant_boolean_node (cmp == LT_EXPR || cmp == LE_EXPR || cmp == NE_EXPR,
6428 /* Remove cast if CST is an integer representable by ITYPE. */
6430 (cmp @0 { gcc_assert (!overflow_p);
6431 wide_int_to_tree (itype, icst_val); })
6433 /* When CST is fractional, optimize
6434 (FTYPE) N == CST -> 0
6435 (FTYPE) N != CST -> 1. */
6436 (if (cmp == EQ_EXPR || cmp == NE_EXPR)
6437 { constant_boolean_node (cmp == NE_EXPR, type); })
6438 /* Otherwise replace with sensible integer constant. */
6441 gcc_checking_assert (!overflow_p);
6443 (icmp @0 { wide_int_to_tree (itype, icst_val); })))))))))
6445 /* Fold A /[ex] B CMP C to A CMP B * C. */
6448 (cmp (exact_div @0 @1) INTEGER_CST@2)
6449 (if (!integer_zerop (@1))
6450 (if (wi::to_wide (@2) == 0)
6452 (if (TREE_CODE (@1) == INTEGER_CST)
6455 wi::overflow_type ovf;
6456 wide_int prod = wi::mul (wi::to_wide (@2), wi::to_wide (@1),
6457 TYPE_SIGN (TREE_TYPE (@1)), &ovf);
6460 { constant_boolean_node (cmp == NE_EXPR, type); }
6461 (cmp @0 { wide_int_to_tree (TREE_TYPE (@0), prod); }))))))))
6462 (for cmp (lt le gt ge)
6464 (cmp (exact_div @0 INTEGER_CST@1) INTEGER_CST@2)
6465 (if (wi::gt_p (wi::to_wide (@1), 0, TYPE_SIGN (TREE_TYPE (@1))))
6468 wi::overflow_type ovf;
6469 wide_int prod = wi::mul (wi::to_wide (@2), wi::to_wide (@1),
6470 TYPE_SIGN (TREE_TYPE (@1)), &ovf);
6473 { constant_boolean_node (wi::lt_p (wi::to_wide (@2), 0,
6474 TYPE_SIGN (TREE_TYPE (@2)))
6475 != (cmp == LT_EXPR || cmp == LE_EXPR), type); }
6476 (cmp @0 { wide_int_to_tree (TREE_TYPE (@0), prod); }))))))
6478 /* Fold (size_t)(A /[ex] B) CMP C to (size_t)A CMP (size_t)B * C or A CMP' 0.
6480 For small C (less than max/B), this is (size_t)A CMP (size_t)B * C.
6481 For large C (more than min/B+2^size), this is also true, with the
6482 multiplication computed modulo 2^size.
6483 For intermediate C, this just tests the sign of A. */
6484 (for cmp (lt le gt ge)
6487 (cmp (convert (exact_div @0 INTEGER_CST@1)) INTEGER_CST@2)
6488 (if (tree_nop_conversion_p (TREE_TYPE (@0), TREE_TYPE (@2))
6489 && TYPE_UNSIGNED (TREE_TYPE (@2)) && !TYPE_UNSIGNED (TREE_TYPE (@0))
6490 && wi::gt_p (wi::to_wide (@1), 0, TYPE_SIGN (TREE_TYPE (@1))))
6493 tree utype = TREE_TYPE (@2);
6494 wide_int denom = wi::to_wide (@1);
6495 wide_int right = wi::to_wide (@2);
6496 wide_int smax = wi::sdiv_trunc (wi::max_value (TREE_TYPE (@0)), denom);
6497 wide_int smin = wi::sdiv_trunc (wi::min_value (TREE_TYPE (@0)), denom);
6498 bool small = wi::leu_p (right, smax);
6499 bool large = wi::geu_p (right, smin);
6501 (if (small || large)
6502 (cmp (convert:utype @0) (mult @2 (convert @1)))
6503 (cmp2 @0 { build_zero_cst (TREE_TYPE (@0)); }))))))
6505 /* Unordered tests if either argument is a NaN. */
6507 (bit_ior (unordered @0 @0) (unordered @1 @1))
6508 (if (types_match (@0, @1))
6511 (bit_and (ordered @0 @0) (ordered @1 @1))
6512 (if (types_match (@0, @1))
6515 (bit_ior:c (unordered @0 @0) (unordered:c@2 @0 @1))
6518 (bit_and:c (ordered @0 @0) (ordered:c@2 @0 @1))
6521 /* A & (2**N - 1) <= 2**K - 1 -> A & (2**N - 2**K) == 0
6522 A & (2**N - 1) > 2**K - 1 -> A & (2**N - 2**K) != 0
6524 Note that comparisons
6525 A & (2**N - 1) < 2**K -> A & (2**N - 2**K) == 0
6526 A & (2**N - 1) >= 2**K -> A & (2**N - 2**K) != 0
6527 will be canonicalized to above so there's no need to
6534 (cmp (bit_and@0 @1 INTEGER_CST@2) INTEGER_CST@3)
6535 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)))
6538 tree ty = TREE_TYPE (@0);
6539 unsigned prec = TYPE_PRECISION (ty);
6540 wide_int mask = wi::to_wide (@2, prec);
6541 wide_int rhs = wi::to_wide (@3, prec);
6542 signop sgn = TYPE_SIGN (ty);
6544 (if ((mask & (mask + 1)) == 0 && wi::gt_p (rhs, 0, sgn)
6545 && (rhs & (rhs + 1)) == 0 && wi::ge_p (mask, rhs, sgn))
6546 (eqcmp (bit_and @1 { wide_int_to_tree (ty, mask - rhs); })
6547 { build_zero_cst (ty); }))))))
6549 /* -A CMP -B -> B CMP A. */
6550 (for cmp (tcc_comparison)
6551 scmp (swapped_tcc_comparison)
6553 (cmp (negate @0) (negate @1))
6554 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
6555 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
6558 || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))))
6561 (cmp (negate @0) CONSTANT_CLASS_P@1)
6562 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
6563 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
6566 || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))))
6567 (with { tree tem = const_unop (NEGATE_EXPR, TREE_TYPE (@0), @1); }
6568 (if (tem && !TREE_OVERFLOW (tem))
6569 (scmp @0 { tem; }))))))
6571 /* Convert ABS[U]_EXPR<x> == 0 or ABS[U]_EXPR<x> != 0 to x == 0 or x != 0. */
6575 (eqne (op @0) zerop@1)
6576 (eqne @0 { build_zero_cst (TREE_TYPE (@0)); }))))
6578 /* From fold_sign_changed_comparison and fold_widened_comparison.
6579 FIXME: the lack of symmetry is disturbing. */
6580 (for cmp (simple_comparison)
6582 (cmp (convert@0 @00) (convert?@1 @10))
6583 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6584 /* Disable this optimization if we're casting a function pointer
6585 type on targets that require function pointer canonicalization. */
6586 && !(targetm.have_canonicalize_funcptr_for_compare ()
6587 && ((POINTER_TYPE_P (TREE_TYPE (@00))
6588 && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@00))))
6589 || (POINTER_TYPE_P (TREE_TYPE (@10))
6590 && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@10))))))
6592 (if (TYPE_PRECISION (TREE_TYPE (@00)) == TYPE_PRECISION (TREE_TYPE (@0))
6593 && (TREE_CODE (@10) == INTEGER_CST
6595 && (TYPE_UNSIGNED (TREE_TYPE (@00)) == TYPE_UNSIGNED (TREE_TYPE (@0))
6598 && !POINTER_TYPE_P (TREE_TYPE (@00))
6599 /* (int)bool:32 != (int)uint is not the same as
6600 bool:32 != (bool:32)uint since boolean types only have two valid
6601 values independent of their precision. */
6602 && (TREE_CODE (TREE_TYPE (@00)) != BOOLEAN_TYPE
6603 || TREE_CODE (TREE_TYPE (@10)) == BOOLEAN_TYPE))
6604 /* ??? The special-casing of INTEGER_CST conversion was in the original
6605 code and here to avoid a spurious overflow flag on the resulting
6606 constant which fold_convert produces. */
6607 (if (TREE_CODE (@1) == INTEGER_CST)
6608 (cmp @00 { force_fit_type (TREE_TYPE (@00),
6609 wide_int::from (wi::to_wide (@1),
6610 MAX (TYPE_PRECISION (TREE_TYPE (@1)),
6611 TYPE_PRECISION (TREE_TYPE (@00))),
6612 TYPE_SIGN (TREE_TYPE (@1))),
6613 0, TREE_OVERFLOW (@1)); })
6614 (cmp @00 (convert @1)))
6616 (if (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (TREE_TYPE (@00)))
6617 /* If possible, express the comparison in the shorter mode. */
6618 (if ((cmp == EQ_EXPR || cmp == NE_EXPR
6619 || TYPE_UNSIGNED (TREE_TYPE (@0)) == TYPE_UNSIGNED (TREE_TYPE (@00))
6620 || (!TYPE_UNSIGNED (TREE_TYPE (@0))
6621 && TYPE_UNSIGNED (TREE_TYPE (@00))))
6622 && (types_match (TREE_TYPE (@10), TREE_TYPE (@00))
6623 || ((TYPE_PRECISION (TREE_TYPE (@00))
6624 >= TYPE_PRECISION (TREE_TYPE (@10)))
6625 && (TYPE_UNSIGNED (TREE_TYPE (@00))
6626 == TYPE_UNSIGNED (TREE_TYPE (@10))))
6627 || (TREE_CODE (@10) == INTEGER_CST
6628 && INTEGRAL_TYPE_P (TREE_TYPE (@00))
6629 && int_fits_type_p (@10, TREE_TYPE (@00)))))
6630 (cmp @00 (convert @10))
6631 (if (TREE_CODE (@10) == INTEGER_CST
6632 && INTEGRAL_TYPE_P (TREE_TYPE (@00))
6633 && !int_fits_type_p (@10, TREE_TYPE (@00)))
6636 tree min = lower_bound_in_type (TREE_TYPE (@10), TREE_TYPE (@00));
6637 tree max = upper_bound_in_type (TREE_TYPE (@10), TREE_TYPE (@00));
6638 bool above = integer_nonzerop (const_binop (LT_EXPR, type, max, @10));
6639 bool below = integer_nonzerop (const_binop (LT_EXPR, type, @10, min));
6641 (if (above || below)
6642 (if (cmp == EQ_EXPR || cmp == NE_EXPR)
6643 { constant_boolean_node (cmp == EQ_EXPR ? false : true, type); }
6644 (if (cmp == LT_EXPR || cmp == LE_EXPR)
6645 { constant_boolean_node (above ? true : false, type); }
6646 (if (cmp == GT_EXPR || cmp == GE_EXPR)
6647 { constant_boolean_node (above ? false : true, type); })))))))))
6648 /* Fold (double)float1 CMP (double)float2 into float1 CMP float2. */
6649 (if (FLOAT_TYPE_P (TREE_TYPE (@00))
6650 && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0))
6651 == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@00)))
6652 && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0))
6653 == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@10))))
6656 tree type1 = TREE_TYPE (@10);
6657 if (TREE_CODE (@10) == REAL_CST && !DECIMAL_FLOAT_TYPE_P (type1))
6659 REAL_VALUE_TYPE orig = TREE_REAL_CST (@10);
6660 if (TYPE_PRECISION (type1) > TYPE_PRECISION (float_type_node)
6661 && exact_real_truncate (TYPE_MODE (float_type_node), &orig))
6662 type1 = float_type_node;
6663 if (TYPE_PRECISION (type1) > TYPE_PRECISION (double_type_node)
6664 && exact_real_truncate (TYPE_MODE (double_type_node), &orig))
6665 type1 = double_type_node;
6668 = (element_precision (TREE_TYPE (@00)) > element_precision (type1)
6669 ? TREE_TYPE (@00) : type1);
6671 (if (element_precision (TREE_TYPE (@0)) > element_precision (newtype))
6672 (cmp (convert:newtype @00) (convert:newtype @10))))))))
6677 /* SSA names are canonicalized to 2nd place. */
6678 (cmp addr@0 SSA_NAME@1)
6681 poly_int64 off; tree base;
6682 tree addr = (TREE_CODE (@0) == SSA_NAME
6683 ? gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0)) : @0);
6685 /* A local variable can never be pointed to by
6686 the default SSA name of an incoming parameter. */
6687 (if (SSA_NAME_IS_DEFAULT_DEF (@1)
6688 && TREE_CODE (SSA_NAME_VAR (@1)) == PARM_DECL
6689 && (base = get_base_address (TREE_OPERAND (addr, 0)))
6690 && TREE_CODE (base) == VAR_DECL
6691 && auto_var_in_fn_p (base, current_function_decl))
6692 (if (cmp == NE_EXPR)
6693 { constant_boolean_node (true, type); }
6694 { constant_boolean_node (false, type); })
6695 /* If the address is based on @1 decide using the offset. */
6696 (if ((base = get_addr_base_and_unit_offset (TREE_OPERAND (addr, 0), &off))
6697 && TREE_CODE (base) == MEM_REF
6698 && TREE_OPERAND (base, 0) == @1)
6699 (with { off += mem_ref_offset (base).force_shwi (); }
6700 (if (known_ne (off, 0))
6701 { constant_boolean_node (cmp == NE_EXPR, type); }
6702 (if (known_eq (off, 0))
6703 { constant_boolean_node (cmp == EQ_EXPR, type); }))))))))
6705 /* Equality compare simplifications from fold_binary */
6708 /* If we have (A | C) == D where C & ~D != 0, convert this into 0.
6709 Similarly for NE_EXPR. */
6711 (cmp (convert?@3 (bit_ior @0 INTEGER_CST@1)) INTEGER_CST@2)
6712 (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0))
6713 && wi::bit_and_not (wi::to_wide (@1), wi::to_wide (@2)) != 0)
6714 { constant_boolean_node (cmp == NE_EXPR, type); }))
6716 /* (X ^ Y) == 0 becomes X == Y, and (X ^ Y) != 0 becomes X != Y. */
6718 (cmp (bit_xor @0 @1) integer_zerop)
6721 /* (X ^ Y) == Y becomes X == 0.
6722 Likewise (X ^ Y) == X becomes Y == 0. */
6724 (cmp:c (bit_xor:c @0 @1) @0)
6725 (cmp @1 { build_zero_cst (TREE_TYPE (@1)); }))
6727 /* (X & Y) == X becomes (X & ~Y) == 0. */
6729 (cmp:c (bit_and:c @0 @1) @0)
6730 (cmp (bit_and @0 (bit_not! @1)) { build_zero_cst (TREE_TYPE (@0)); }))
6732 (cmp:c (convert@3 (bit_and (convert@2 @0) INTEGER_CST@1)) (convert @0))
6733 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6734 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
6735 && INTEGRAL_TYPE_P (TREE_TYPE (@3))
6736 && TYPE_PRECISION (TREE_TYPE (@2)) == TYPE_PRECISION (TREE_TYPE (@0))
6737 && TYPE_PRECISION (TREE_TYPE (@3)) > TYPE_PRECISION (TREE_TYPE (@2))
6738 && !wi::neg_p (wi::to_wide (@1)))
6739 (cmp (bit_and @0 (convert (bit_not @1)))
6740 { build_zero_cst (TREE_TYPE (@0)); })))
6742 /* (X | Y) == Y becomes (X & ~Y) == 0. */
6744 (cmp:c (bit_ior:c @0 @1) @1)
6745 (cmp (bit_and @0 (bit_not! @1)) { build_zero_cst (TREE_TYPE (@0)); }))
6747 /* (X ^ C1) op C2 can be rewritten as X op (C1 ^ C2). */
6749 (cmp (convert?@3 (bit_xor @0 INTEGER_CST@1)) INTEGER_CST@2)
6750 (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0)))
6751 (cmp @0 (bit_xor @1 (convert @2)))))
6754 (cmp (nop_convert? @0) integer_zerop)
6755 (if (tree_expr_nonzero_p (@0))
6756 { constant_boolean_node (cmp == NE_EXPR, type); }))
6758 /* (X & C) op (Y & C) into (X ^ Y) & C op 0. */
6760 (cmp (bit_and:cs @0 @2) (bit_and:cs @1 @2))
6761 (cmp (bit_and (bit_xor @0 @1) @2) { build_zero_cst (TREE_TYPE (@2)); })))
6763 /* (X < 0) != (Y < 0) into (X ^ Y) < 0.
6764 (X >= 0) != (Y >= 0) into (X ^ Y) < 0.
6765 (X < 0) == (Y < 0) into (X ^ Y) >= 0.
6766 (X >= 0) == (Y >= 0) into (X ^ Y) >= 0. */
6771 (cmp (sgncmp @0 integer_zerop@2) (sgncmp @1 integer_zerop))
6772 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
6773 && !TYPE_UNSIGNED (TREE_TYPE (@0))
6774 && types_match (@0, @1))
6775 (ncmp (bit_xor @0 @1) @2)))))
6776 /* (X < 0) == (Y >= 0) into (X ^ Y) < 0.
6777 (X < 0) != (Y >= 0) into (X ^ Y) >= 0. */
6781 (cmp:c (lt @0 integer_zerop@2) (ge @1 integer_zerop))
6782 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
6783 && !TYPE_UNSIGNED (TREE_TYPE (@0))
6784 && types_match (@0, @1))
6785 (ncmp (bit_xor @0 @1) @2))))
6787 /* If we have (A & C) == C where C is a power of 2, convert this into
6788 (A & C) != 0. Similarly for NE_EXPR. */
6792 (cmp (bit_and@2 @0 integer_pow2p@1) @1)
6793 (icmp @2 { build_zero_cst (TREE_TYPE (@0)); })))
6796 /* From fold_binary_op_with_conditional_arg handle the case of
6797 rewriting (a ? b : c) > d to a ? (b > d) : (c > d) when the
6798 compares simplify. */
6799 (for cmp (simple_comparison)
6801 (cmp:c (cond @0 @1 @2) @3)
6802 /* Do not move possibly trapping operations into the conditional as this
6803 pessimizes code and causes gimplification issues when applied late. */
6804 (if (!FLOAT_TYPE_P (TREE_TYPE (@3))
6805 || !operation_could_trap_p (cmp, true, false, @3))
6806 (cond @0 (cmp! @1 @3) (cmp! @2 @3)))))
6810 /* x < 0 ? ~y : y into (x >> (prec-1)) ^ y. */
6811 /* x >= 0 ? ~y : y into ~((x >> (prec-1)) ^ y). */
6813 (cond (cmp @0 integer_zerop) (bit_not @1) @1)
6814 (if (INTEGRAL_TYPE_P (type)
6815 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
6816 && !TYPE_UNSIGNED (TREE_TYPE (@0))
6817 && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (type))
6820 tree shifter = build_int_cst (integer_type_node, TYPE_PRECISION (type) - 1);
6822 (if (cmp == LT_EXPR)
6823 (bit_xor (convert (rshift @0 {shifter;})) @1)
6824 (bit_not (bit_xor (convert (rshift @0 {shifter;})) @1))))))
6825 /* x < 0 ? y : ~y into ~((x >> (prec-1)) ^ y). */
6826 /* x >= 0 ? y : ~y into (x >> (prec-1)) ^ y. */
6828 (cond (cmp @0 integer_zerop) @1 (bit_not @1))
6829 (if (INTEGRAL_TYPE_P (type)
6830 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
6831 && !TYPE_UNSIGNED (TREE_TYPE (@0))
6832 && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (type))
6835 tree shifter = build_int_cst (integer_type_node, TYPE_PRECISION (type) - 1);
6837 (if (cmp == GE_EXPR)
6838 (bit_xor (convert (rshift @0 {shifter;})) @1)
6839 (bit_not (bit_xor (convert (rshift @0 {shifter;})) @1)))))))
6841 /* If we have (A & C) != 0 ? D : 0 where C and D are powers of 2,
6842 convert this into a shift followed by ANDing with D. */
6845 (ne (bit_and @0 integer_pow2p@1) integer_zerop)
6846 INTEGER_CST@2 integer_zerop)
6847 (if (!POINTER_TYPE_P (type) && integer_pow2p (@2))
6849 int shift = (wi::exact_log2 (wi::to_wide (@2))
6850 - wi::exact_log2 (wi::to_wide (@1)));
6854 (lshift (convert @0) { build_int_cst (integer_type_node, shift); }) @2)
6856 (convert (rshift @0 { build_int_cst (integer_type_node, -shift); }))
6859 /* If we have (A & C) != 0 where C is the sign bit of A, convert
6860 this into A < 0. Similarly for (A & C) == 0 into A >= 0. */
6864 (cmp (bit_and (convert?@2 @0) integer_pow2p@1) integer_zerop)
6865 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6866 && type_has_mode_precision_p (TREE_TYPE (@0))
6867 && element_precision (@2) >= element_precision (@0)
6868 && wi::only_sign_bit_p (wi::to_wide (@1), element_precision (@0)))
6869 (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
6870 (ncmp (convert:stype @0) { build_zero_cst (stype); })))))
6872 /* If we have A < 0 ? C : 0 where C is a power of 2, convert
6873 this into a right shift or sign extension followed by ANDing with C. */
6876 (lt @0 integer_zerop)
6877 INTEGER_CST@1 integer_zerop)
6878 (if (integer_pow2p (@1)
6879 && !TYPE_UNSIGNED (TREE_TYPE (@0)))
6881 int shift = element_precision (@0) - wi::exact_log2 (wi::to_wide (@1)) - 1;
6885 (convert (rshift @0 { build_int_cst (integer_type_node, shift); }))
6887 /* Otherwise ctype must be wider than TREE_TYPE (@0) and pure
6888 sign extension followed by AND with C will achieve the effect. */
6889 (bit_and (convert @0) @1)))))
6891 /* When the addresses are not directly of decls compare base and offset.
6892 This implements some remaining parts of fold_comparison address
6893 comparisons but still no complete part of it. Still it is good
6894 enough to make fold_stmt not regress when not dispatching to fold_binary. */
6895 (for cmp (simple_comparison)
6897 (cmp (convert1?@2 addr@0) (convert2? addr@1))
6900 poly_int64 off0, off1;
6902 int equal = address_compare (cmp, TREE_TYPE (@2), @0, @1, base0, base1,
6903 off0, off1, GENERIC);
6907 (if (cmp == EQ_EXPR && (known_eq (off0, off1) || known_ne (off0, off1)))
6908 { constant_boolean_node (known_eq (off0, off1), type); })
6909 (if (cmp == NE_EXPR && (known_eq (off0, off1) || known_ne (off0, off1)))
6910 { constant_boolean_node (known_ne (off0, off1), type); })
6911 (if (cmp == LT_EXPR && (known_lt (off0, off1) || known_ge (off0, off1)))
6912 { constant_boolean_node (known_lt (off0, off1), type); })
6913 (if (cmp == LE_EXPR && (known_le (off0, off1) || known_gt (off0, off1)))
6914 { constant_boolean_node (known_le (off0, off1), type); })
6915 (if (cmp == GE_EXPR && (known_ge (off0, off1) || known_lt (off0, off1)))
6916 { constant_boolean_node (known_ge (off0, off1), type); })
6917 (if (cmp == GT_EXPR && (known_gt (off0, off1) || known_le (off0, off1)))
6918 { constant_boolean_node (known_gt (off0, off1), type); }))
6921 (if (cmp == EQ_EXPR)
6922 { constant_boolean_node (false, type); })
6923 (if (cmp == NE_EXPR)
6924 { constant_boolean_node (true, type); })))))))
6927 /* a?~t:t -> (-(a))^t */
6930 (with { bool wascmp; }
6931 (if (INTEGRAL_TYPE_P (type)
6932 && bitwise_inverted_equal_p (@1, @2, wascmp)
6933 && (!wascmp || TYPE_PRECISION (type) == 1))
6934 (if ((!TYPE_UNSIGNED (type) && TREE_CODE (type) == BOOLEAN_TYPE)
6935 || TYPE_PRECISION (type) == 1)
6936 (bit_xor (convert:type @0) @2)
6937 (bit_xor (negate (convert:type @0)) @2)))))
6940 /* Simplify pointer equality compares using PTA. */
6944 (if (POINTER_TYPE_P (TREE_TYPE (@0))
6945 && ptrs_compare_unequal (@0, @1))
6946 { constant_boolean_node (neeq != EQ_EXPR, type); })))
6948 /* PR70920: Transform (intptr_t)x eq/ne CST to x eq/ne (typeof x) CST.
6949 and (typeof ptr_cst) x eq/ne ptr_cst to x eq/ne (typeof x) CST.
6950 Disable the transform if either operand is pointer to function.
6951 This broke pr22051-2.c for arm where function pointer
6952 canonicalizaion is not wanted. */
6956 (cmp (convert @0) INTEGER_CST@1)
6957 (if (((POINTER_TYPE_P (TREE_TYPE (@0))
6958 && !FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@0)))
6959 && INTEGRAL_TYPE_P (TREE_TYPE (@1))
6960 /* Don't perform this optimization in GENERIC if @0 has reference
6961 type when sanitizing. See PR101210. */
6963 && TREE_CODE (TREE_TYPE (@0)) == REFERENCE_TYPE
6964 && (flag_sanitize & (SANITIZE_NULL | SANITIZE_ALIGNMENT))))
6965 || (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6966 && POINTER_TYPE_P (TREE_TYPE (@1))
6967 && !FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@1)))))
6968 && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
6969 (cmp @0 (convert @1)))))
6971 /* Non-equality compare simplifications from fold_binary */
6972 (for cmp (lt gt le ge)
6973 /* Comparisons with the highest or lowest possible integer of
6974 the specified precision will have known values. */
6976 (cmp (convert?@2 @0) uniform_integer_cst_p@1)
6977 (if ((INTEGRAL_TYPE_P (TREE_TYPE (@1))
6978 || POINTER_TYPE_P (TREE_TYPE (@1))
6979 || VECTOR_INTEGER_TYPE_P (TREE_TYPE (@1)))
6980 && tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@0)))
6983 tree cst = uniform_integer_cst_p (@1);
6984 tree arg1_type = TREE_TYPE (cst);
6985 unsigned int prec = TYPE_PRECISION (arg1_type);
6986 wide_int max = wi::max_value (arg1_type);
6987 wide_int signed_max = wi::max_value (prec, SIGNED);
6988 wide_int min = wi::min_value (arg1_type);
6991 (if (wi::to_wide (cst) == max)
6993 (if (cmp == GT_EXPR)
6994 { constant_boolean_node (false, type); })
6995 (if (cmp == GE_EXPR)
6997 (if (cmp == LE_EXPR)
6998 { constant_boolean_node (true, type); })
6999 (if (cmp == LT_EXPR)
7001 (if (wi::to_wide (cst) == min)
7003 (if (cmp == LT_EXPR)
7004 { constant_boolean_node (false, type); })
7005 (if (cmp == LE_EXPR)
7007 (if (cmp == GE_EXPR)
7008 { constant_boolean_node (true, type); })
7009 (if (cmp == GT_EXPR)
7011 (if (wi::to_wide (cst) == max - 1)
7013 (if (cmp == GT_EXPR)
7014 (eq @2 { build_uniform_cst (TREE_TYPE (@1),
7015 wide_int_to_tree (TREE_TYPE (cst),
7018 (if (cmp == LE_EXPR)
7019 (ne @2 { build_uniform_cst (TREE_TYPE (@1),
7020 wide_int_to_tree (TREE_TYPE (cst),
7023 (if (wi::to_wide (cst) == min + 1)
7025 (if (cmp == GE_EXPR)
7026 (ne @2 { build_uniform_cst (TREE_TYPE (@1),
7027 wide_int_to_tree (TREE_TYPE (cst),
7030 (if (cmp == LT_EXPR)
7031 (eq @2 { build_uniform_cst (TREE_TYPE (@1),
7032 wide_int_to_tree (TREE_TYPE (cst),
7035 (if (wi::to_wide (cst) == signed_max
7036 && TYPE_UNSIGNED (arg1_type)
7037 && TYPE_MODE (arg1_type) != BLKmode
7038 /* We will flip the signedness of the comparison operator
7039 associated with the mode of @1, so the sign bit is
7040 specified by this mode. Check that @1 is the signed
7041 max associated with this sign bit. */
7042 && prec == GET_MODE_PRECISION (SCALAR_INT_TYPE_MODE (arg1_type))
7043 /* signed_type does not work on pointer types. */
7044 && INTEGRAL_TYPE_P (arg1_type))
7045 /* The following case also applies to X < signed_max+1
7046 and X >= signed_max+1 because previous transformations. */
7047 (if (cmp == LE_EXPR || cmp == GT_EXPR)
7048 (with { tree st = signed_type_for (TREE_TYPE (@1)); }
7050 (if (cst == @1 && cmp == LE_EXPR)
7051 (ge (convert:st @0) { build_zero_cst (st); }))
7052 (if (cst == @1 && cmp == GT_EXPR)
7053 (lt (convert:st @0) { build_zero_cst (st); }))
7054 (if (cmp == LE_EXPR)
7055 (ge (view_convert:st @0) { build_zero_cst (st); }))
7056 (if (cmp == GT_EXPR)
7057 (lt (view_convert:st @0) { build_zero_cst (st); })))))))))))
7059 /* unsigned < (typeof unsigned)(unsigned != 0) is always false. */
7061 (lt:c @0 (convert (ne @0 integer_zerop)))
7062 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
7063 { constant_boolean_node (false, type); }))
7065 /* x != (typeof x)(x == CST) -> CST == 0 ? 1 : (CST == 1 ? (x!=0&&x!=1) : x != 0) */
7066 /* x != (typeof x)(x != CST) -> CST == 1 ? 1 : (CST == 0 ? (x!=0&&x!=1) : x != 1) */
7067 /* x == (typeof x)(x == CST) -> CST == 0 ? 0 : (CST == 1 ? (x==0||x==1) : x == 0) */
7068 /* x == (typeof x)(x != CST) -> CST == 1 ? 0 : (CST == 0 ? (x==0||x==1) : x == 1) */
7072 (outer:c @0 (convert (inner @0 INTEGER_CST@1)))
7074 bool cst1 = integer_onep (@1);
7075 bool cst0 = integer_zerop (@1);
7076 bool innereq = inner == EQ_EXPR;
7077 bool outereq = outer == EQ_EXPR;
7080 (if (innereq ? cst0 : cst1)
7081 { constant_boolean_node (!outereq, type); })
7082 (if (innereq ? cst1 : cst0)
7084 tree utype = unsigned_type_for (TREE_TYPE (@0));
7085 tree ucst1 = build_one_cst (utype);
7088 (gt (convert:utype @0) { ucst1; })
7089 (le (convert:utype @0) { ucst1; })
7094 tree value = build_int_cst (TREE_TYPE (@0), !innereq);
7107 (for cmp (unordered ordered unlt unle ungt unge uneq ltgt)
7108 /* If the second operand is NaN, the result is constant. */
7111 (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
7112 && (cmp != LTGT_EXPR || ! flag_trapping_math))
7113 { constant_boolean_node (cmp == ORDERED_EXPR || cmp == LTGT_EXPR
7114 ? false : true, type); })))
7116 /* Fold UNORDERED if either operand must be NaN, or neither can be. */
7120 (if (tree_expr_nan_p (@0) || tree_expr_nan_p (@1))
7121 { constant_boolean_node (true, type); })
7122 (if (!tree_expr_maybe_nan_p (@0) && !tree_expr_maybe_nan_p (@1))
7123 { constant_boolean_node (false, type); })))
7125 /* Fold ORDERED if either operand must be NaN, or neither can be. */
7129 (if (tree_expr_nan_p (@0) || tree_expr_nan_p (@1))
7130 { constant_boolean_node (false, type); })
7131 (if (!tree_expr_maybe_nan_p (@0) && !tree_expr_maybe_nan_p (@1))
7132 { constant_boolean_node (true, type); })))
7134 /* bool_var != 0 becomes bool_var. */
7136 (ne @0 integer_zerop)
7137 (if (TREE_CODE (TREE_TYPE (@0)) == BOOLEAN_TYPE
7138 && types_match (type, TREE_TYPE (@0)))
7140 /* bool_var == 1 becomes bool_var. */
7142 (eq @0 integer_onep)
7143 (if (TREE_CODE (TREE_TYPE (@0)) == BOOLEAN_TYPE
7144 && types_match (type, TREE_TYPE (@0)))
7147 bool_var == 0 becomes !bool_var or
7148 bool_var != 1 becomes !bool_var
7149 here because that only is good in assignment context as long
7150 as we require a tcc_comparison in GIMPLE_CONDs where we'd
7151 replace if (x == 0) with tem = ~x; if (tem != 0) which is
7152 clearly less optimal and which we'll transform again in forwprop. */
7154 /* Transform comparisons of the form (X & Y) CMP 0 to X CMP2 Z
7155 where ~Y + 1 == pow2 and Z = ~Y. */
7156 (for cst (VECTOR_CST INTEGER_CST)
7160 (cmp (bit_and:c@2 @0 cst@1) integer_zerop)
7161 (with { tree csts = bitmask_inv_cst_vector_p (@1); }
7162 (if (csts && (VECTOR_TYPE_P (TREE_TYPE (@1)) || single_use (@2)))
7163 (with { auto optab = VECTOR_TYPE_P (TREE_TYPE (@1))
7164 ? optab_vector : optab_default;
7165 tree utype = unsigned_type_for (TREE_TYPE (@1)); }
7166 (if (target_supports_op_p (utype, icmp, optab)
7167 || (optimize_vectors_before_lowering_p ()
7168 && (!target_supports_op_p (type, cmp, optab)
7169 || !target_supports_op_p (type, BIT_AND_EXPR, optab))))
7170 (if (TYPE_UNSIGNED (TREE_TYPE (@1)))
7172 (icmp (view_convert:utype @0) { csts; })))))))))
7174 /* When one argument is a constant, overflow detection can be simplified.
7175 Currently restricted to single use so as not to interfere too much with
7176 ADD_OVERFLOW detection in tree-ssa-math-opts.cc.
7177 CONVERT?(CONVERT?(A) + CST) CMP A -> A CMP' CST' */
7178 (for cmp (lt le ge gt)
7181 (cmp:c (convert?@3 (plus@2 (convert?@4 @0) INTEGER_CST@1)) @0)
7182 (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@2))
7183 && types_match (TREE_TYPE (@0), TREE_TYPE (@3))
7184 && tree_nop_conversion_p (TREE_TYPE (@4), TREE_TYPE (@0))
7185 && wi::to_wide (@1) != 0
7188 unsigned int prec = TYPE_PRECISION (TREE_TYPE (@0));
7189 signop sign = TYPE_SIGN (TREE_TYPE (@0));
7191 (out @0 { wide_int_to_tree (TREE_TYPE (@0),
7192 wi::max_value (prec, sign)
7193 - wi::to_wide (@1)); })))))
7195 /* To detect overflow in unsigned A - B, A < B is simpler than A - B > A.
7196 However, the detection logic for SUB_OVERFLOW in tree-ssa-math-opts.cc
7197 expects the long form, so we restrict the transformation for now. */
7200 (cmp:c (minus@2 @0 @1) @0)
7201 (if (single_use (@2)
7202 && ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
7203 && TYPE_UNSIGNED (TREE_TYPE (@0)))
7206 /* Optimize A - B + -1 >= A into B >= A for unsigned comparisons. */
7209 (cmp:c (plus (minus @0 @1) integer_minus_onep) @0)
7210 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
7211 && TYPE_UNSIGNED (TREE_TYPE (@0)))
7214 /* Testing for overflow is unnecessary if we already know the result. */
7219 (cmp:c (realpart (IFN_SUB_OVERFLOW@2 @0 @1)) @0)
7220 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
7221 && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
7222 (out (imagpart @2) { build_zero_cst (TREE_TYPE (@0)); }))))
7227 (cmp:c (realpart (IFN_ADD_OVERFLOW:c@2 @0 @1)) @0)
7228 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
7229 && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
7230 (out (imagpart @2) { build_zero_cst (TREE_TYPE (@0)); }))))
7232 /* For unsigned operands, -1 / B < A checks whether A * B would overflow.
7233 Simplify it to __builtin_mul_overflow (A, B, <unused>). */
7237 (cmp:c (trunc_div:s integer_all_onesp @1) @0)
7238 (if (TYPE_UNSIGNED (TREE_TYPE (@0)) && !VECTOR_TYPE_P (TREE_TYPE (@0)))
7239 (with { tree t = TREE_TYPE (@0), cpx = build_complex_type (t); }
7240 (out (imagpart (IFN_MUL_OVERFLOW:cpx @0 @1)) { build_zero_cst (t); })))))
7242 /* Similarly, for unsigned operands, (((type) A * B) >> prec) != 0 where type
7243 is at least twice as wide as type of A and B, simplify to
7244 __builtin_mul_overflow (A, B, <unused>). */
7247 (cmp (rshift (mult:s (convert@3 @0) (convert @1)) INTEGER_CST@2)
7249 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
7250 && INTEGRAL_TYPE_P (TREE_TYPE (@3))
7251 && TYPE_UNSIGNED (TREE_TYPE (@0))
7252 && (TYPE_PRECISION (TREE_TYPE (@3))
7253 >= 2 * TYPE_PRECISION (TREE_TYPE (@0)))
7254 && tree_fits_uhwi_p (@2)
7255 && tree_to_uhwi (@2) == TYPE_PRECISION (TREE_TYPE (@0))
7256 && types_match (@0, @1)
7257 && type_has_mode_precision_p (TREE_TYPE (@0))
7258 && (optab_handler (umulv4_optab, TYPE_MODE (TREE_TYPE (@0)))
7259 != CODE_FOR_nothing))
7260 (with { tree t = TREE_TYPE (@0), cpx = build_complex_type (t); }
7261 (cmp (imagpart (IFN_MUL_OVERFLOW:cpx @0 @1)) { build_zero_cst (t); })))))
7263 /* Demote operands of IFN_{ADD,SUB,MUL}_OVERFLOW. */
7264 (for ovf (IFN_ADD_OVERFLOW IFN_SUB_OVERFLOW IFN_MUL_OVERFLOW)
7266 (ovf (convert@2 @0) @1)
7267 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
7268 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
7269 && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
7270 && (!TYPE_UNSIGNED (TREE_TYPE (@2)) || TYPE_UNSIGNED (TREE_TYPE (@0))))
7273 (ovf @1 (convert@2 @0))
7274 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
7275 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
7276 && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
7277 && (!TYPE_UNSIGNED (TREE_TYPE (@2)) || TYPE_UNSIGNED (TREE_TYPE (@0))))
7280 /* Optimize __builtin_mul_overflow_p (x, cst, (utype) 0) if all 3 types
7281 are unsigned to x > (umax / cst). Similarly for signed type, but
7282 in that case it needs to be outside of a range. */
7284 (imagpart (IFN_MUL_OVERFLOW:cs@2 @0 integer_nonzerop@1))
7285 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
7286 && TYPE_MAX_VALUE (TREE_TYPE (@0))
7287 && types_match (TREE_TYPE (@0), TREE_TYPE (TREE_TYPE (@2)))
7288 && int_fits_type_p (@1, TREE_TYPE (@0)))
7289 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
7290 (convert (gt @0 (trunc_div! { TYPE_MAX_VALUE (TREE_TYPE (@0)); } @1)))
7291 (if (TYPE_MIN_VALUE (TREE_TYPE (@0)))
7292 (if (integer_minus_onep (@1))
7293 (convert (eq @0 { TYPE_MIN_VALUE (TREE_TYPE (@0)); }))
7296 tree div = fold_convert (TREE_TYPE (@0), @1);
7297 tree lo = int_const_binop (TRUNC_DIV_EXPR,
7298 TYPE_MIN_VALUE (TREE_TYPE (@0)), div);
7299 tree hi = int_const_binop (TRUNC_DIV_EXPR,
7300 TYPE_MAX_VALUE (TREE_TYPE (@0)), div);
7301 tree etype = range_check_type (TREE_TYPE (@0));
7304 if (wi::neg_p (wi::to_wide (div)))
7306 lo = fold_convert (etype, lo);
7307 hi = fold_convert (etype, hi);
7308 hi = int_const_binop (MINUS_EXPR, hi, lo);
7312 (convert (gt (minus (convert:etype @0) { lo; }) { hi; })))))))))
7314 /* Simplification of math builtins. These rules must all be optimizations
7315 as well as IL simplifications. If there is a possibility that the new
7316 form could be a pessimization, the rule should go in the canonicalization
7317 section that follows this one.
7319 Rules can generally go in this section if they satisfy one of
7322 - the rule describes an identity
7324 - the rule replaces calls with something as simple as addition or
7327 - the rule contains unary calls only and simplifies the surrounding
7328 arithmetic. (The idea here is to exclude non-unary calls in which
7329 one operand is constant and in which the call is known to be cheap
7330 when the operand has that value.) */
7332 (if (flag_unsafe_math_optimizations)
7333 /* Simplify sqrt(x) * sqrt(x) -> x. */
7335 (mult (SQRT_ALL@1 @0) @1)
7336 (if (!tree_expr_maybe_signaling_nan_p (@0))
7339 (for op (plus minus)
7340 /* Simplify (A / C) +- (B / C) -> (A +- B) / C. */
7344 (rdiv (op @0 @2) @1)))
7346 (for cmp (lt le gt ge)
7347 neg_cmp (gt ge lt le)
7348 /* Simplify (x * C1) cmp C2 -> x cmp (C2 / C1), where C1 != 0. */
7350 (cmp (mult @0 REAL_CST@1) REAL_CST@2)
7352 { tree tem = const_binop (RDIV_EXPR, type, @2, @1); }
7354 && !(REAL_VALUE_ISINF (TREE_REAL_CST (tem))
7355 || (real_zerop (tem) && !real_zerop (@1))))
7357 (if (real_less (&dconst0, TREE_REAL_CST_PTR (@1)))
7359 (if (real_less (TREE_REAL_CST_PTR (@1), &dconst0))
7360 (neg_cmp @0 { tem; })))))))
7362 /* Simplify sqrt(x) * sqrt(y) -> sqrt(x*y). */
7363 (for root (SQRT CBRT)
7365 (mult (root:s @0) (root:s @1))
7366 (root (mult @0 @1))))
7368 /* Simplify expN(x) * expN(y) -> expN(x+y). */
7369 (for exps (EXP EXP2 EXP10 POW10)
7371 (mult (exps:s @0) (exps:s @1))
7372 (exps (plus @0 @1))))
7374 /* Simplify a/root(b/c) into a*root(c/b). */
7375 (for root (SQRT CBRT)
7377 (rdiv @0 (root:s (rdiv:s @1 @2)))
7378 (mult @0 (root (rdiv @2 @1)))))
7380 /* Simplify x/expN(y) into x*expN(-y). */
7381 (for exps (EXP EXP2 EXP10 POW10)
7383 (rdiv @0 (exps:s @1))
7384 (mult @0 (exps (negate @1)))))
7386 (for logs (LOG LOG2 LOG10 LOG10)
7387 exps (EXP EXP2 EXP10 POW10)
7388 /* logN(expN(x)) -> x. */
7392 /* expN(logN(x)) -> x. */
7397 /* Optimize logN(func()) for various exponential functions. We
7398 want to determine the value "x" and the power "exponent" in
7399 order to transform logN(x**exponent) into exponent*logN(x). */
7400 (for logs (LOG LOG LOG LOG2 LOG2 LOG2 LOG10 LOG10)
7401 exps (EXP2 EXP10 POW10 EXP EXP10 POW10 EXP EXP2)
7404 (if (SCALAR_FLOAT_TYPE_P (type))
7410 /* Prepare to do logN(exp(exponent)) -> exponent*logN(e). */
7411 x = build_real_truncate (type, dconst_e ());
7414 /* Prepare to do logN(exp2(exponent)) -> exponent*logN(2). */
7415 x = build_real (type, dconst2);
7419 /* Prepare to do logN(exp10(exponent)) -> exponent*logN(10). */
7421 REAL_VALUE_TYPE dconst10;
7422 real_from_integer (&dconst10, VOIDmode, 10, SIGNED);
7423 x = build_real (type, dconst10);
7430 (mult (logs { x; }) @0)))))
7438 (if (SCALAR_FLOAT_TYPE_P (type))
7444 /* Prepare to do logN(sqrt(x)) -> 0.5*logN(x). */
7445 x = build_real (type, dconsthalf);
7448 /* Prepare to do logN(cbrt(x)) -> (1/3)*logN(x). */
7449 x = build_real_truncate (type, dconst_third ());
7455 (mult { x; } (logs @0))))))
7457 /* logN(pow(x,exponent)) -> exponent*logN(x). */
7458 (for logs (LOG LOG2 LOG10)
7462 (mult @1 (logs @0))))
7464 /* pow(C,x) -> exp(log(C)*x) if C > 0,
7465 or if C is a positive power of 2,
7466 pow(C,x) -> exp2(log2(C)*x). */
7474 (pows REAL_CST@0 @1)
7475 (if (real_compare (GT_EXPR, TREE_REAL_CST_PTR (@0), &dconst0)
7476 && real_isfinite (TREE_REAL_CST_PTR (@0))
7477 /* As libmvec doesn't have a vectorized exp2, defer optimizing
7478 the use_exp2 case until after vectorization. It seems actually
7479 beneficial for all constants to postpone this until later,
7480 because exp(log(C)*x), while faster, will have worse precision
7481 and if x folds into a constant too, that is unnecessary
7483 && canonicalize_math_after_vectorization_p ())
7485 const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (@0);
7486 bool use_exp2 = false;
7487 if (targetm.libc_has_function (function_c99_misc, TREE_TYPE (@0))
7488 && value->cl == rvc_normal)
7490 REAL_VALUE_TYPE frac_rvt = *value;
7491 SET_REAL_EXP (&frac_rvt, 1);
7492 if (real_equal (&frac_rvt, &dconst1))
7497 (if (optimize_pow_to_exp (@0, @1))
7498 (exps (mult (logs @0) @1)))
7499 (exp2s (mult (log2s @0) @1)))))))
7502 /* pow(C,x)*expN(y) -> expN(logN(C)*x+y) if C > 0. */
7504 exps (EXP EXP2 EXP10 POW10)
7505 logs (LOG LOG2 LOG10 LOG10)
7507 (mult:c (pows:s REAL_CST@0 @1) (exps:s @2))
7508 (if (real_compare (GT_EXPR, TREE_REAL_CST_PTR (@0), &dconst0)
7509 && real_isfinite (TREE_REAL_CST_PTR (@0)))
7510 (exps (plus (mult (logs @0) @1) @2)))))
7515 exps (EXP EXP2 EXP10 POW10)
7516 /* sqrt(expN(x)) -> expN(x*0.5). */
7519 (exps (mult @0 { build_real (type, dconsthalf); })))
7520 /* cbrt(expN(x)) -> expN(x/3). */
7523 (exps (mult @0 { build_real_truncate (type, dconst_third ()); })))
7524 /* pow(expN(x), y) -> expN(x*y). */
7527 (exps (mult @0 @1))))
7529 /* tan(atan(x)) -> x. */
7536 /* Simplify sin(atan(x)) -> x / sqrt(x*x + 1). */
7540 copysigns (COPYSIGN)
7545 REAL_VALUE_TYPE r_cst;
7546 build_sinatan_real (&r_cst, type);
7547 tree t_cst = build_real (type, r_cst);
7548 tree t_one = build_one_cst (type);
7550 (if (SCALAR_FLOAT_TYPE_P (type))
7551 (cond (lt (abs @0) { t_cst; })
7552 (rdiv @0 (sqrts (plus (mult @0 @0) { t_one; })))
7553 (copysigns { t_one; } @0))))))
7555 /* Simplify cos(atan(x)) -> 1 / sqrt(x*x + 1). */
7559 copysigns (COPYSIGN)
7564 REAL_VALUE_TYPE r_cst;
7565 build_sinatan_real (&r_cst, type);
7566 tree t_cst = build_real (type, r_cst);
7567 tree t_one = build_one_cst (type);
7568 tree t_zero = build_zero_cst (type);
7570 (if (SCALAR_FLOAT_TYPE_P (type))
7571 (cond (lt (abs @0) { t_cst; })
7572 (rdiv { t_one; } (sqrts (plus (mult @0 @0) { t_one; })))
7573 (copysigns { t_zero; } @0))))))
7575 (if (!flag_errno_math)
7576 /* Simplify sinh(atanh(x)) -> x / sqrt((1 - x)*(1 + x)). */
7581 (sinhs (atanhs:s @0))
7582 (with { tree t_one = build_one_cst (type); }
7583 (rdiv @0 (sqrts (mult (minus { t_one; } @0) (plus { t_one; } @0)))))))
7585 /* Simplify cosh(atanh(x)) -> 1 / sqrt((1 - x)*(1 + x)) */
7590 (coshs (atanhs:s @0))
7591 (with { tree t_one = build_one_cst (type); }
7592 (rdiv { t_one; } (sqrts (mult (minus { t_one; } @0) (plus { t_one; } @0))))))))
7594 /* cabs(x+0i) or cabs(0+xi) -> abs(x). */
7596 (CABS (complex:C @0 real_zerop@1))
7599 /* trunc(trunc(x)) -> trunc(x), etc. */
7600 (for fns (TRUNC_ALL FLOOR_ALL CEIL_ALL ROUND_ALL NEARBYINT_ALL RINT_ALL)
7604 /* f(x) -> x if x is integer valued and f does nothing for such values. */
7605 (for fns (TRUNC_ALL FLOOR_ALL CEIL_ALL ROUND_ALL NEARBYINT_ALL RINT_ALL)
7607 (fns integer_valued_real_p@0)
7610 /* hypot(x,0) and hypot(0,x) -> abs(x). */
7612 (HYPOT:c @0 real_zerop@1)
7615 /* pow(1,x) -> 1. */
7617 (POW real_onep@0 @1)
7621 /* copysign(x,x) -> x. */
7622 (COPYSIGN_ALL @0 @0)
7626 /* copysign(x,-x) -> -x. */
7627 (COPYSIGN_ALL @0 (negate@1 @0))
7631 /* copysign(x,y) -> fabs(x) if y is nonnegative. */
7632 (COPYSIGN_ALL @0 tree_expr_nonnegative_p@1)
7636 /* fabs (copysign(x, y)) -> fabs (x). */
7637 (abs (COPYSIGN_ALL @0 @1))
7640 (for scale (LDEXP SCALBN SCALBLN)
7641 /* ldexp(0, x) -> 0. */
7643 (scale real_zerop@0 @1)
7645 /* ldexp(x, 0) -> x. */
7647 (scale @0 integer_zerop@1)
7649 /* ldexp(x, y) -> x if x is +-Inf or NaN. */
7651 (scale REAL_CST@0 @1)
7652 (if (!real_isfinite (TREE_REAL_CST_PTR (@0)))
7655 /* Canonicalization of sequences of math builtins. These rules represent
7656 IL simplifications but are not necessarily optimizations.
7658 The sincos pass is responsible for picking "optimal" implementations
7659 of math builtins, which may be more complicated and can sometimes go
7660 the other way, e.g. converting pow into a sequence of sqrts.
7661 We only want to do these canonicalizations before the pass has run. */
7663 (if (flag_unsafe_math_optimizations && canonicalize_math_p ())
7664 /* Simplify tan(x) * cos(x) -> sin(x). */
7666 (mult:c (TAN:s @0) (COS:s @0))
7669 /* Simplify x * pow(x,c) -> pow(x,c+1). */
7671 (mult:c @0 (POW:s @0 REAL_CST@1))
7672 (if (!TREE_OVERFLOW (@1))
7673 (POW @0 (plus @1 { build_one_cst (type); }))))
7675 /* Simplify sin(x) / cos(x) -> tan(x). */
7677 (rdiv (SIN:s @0) (COS:s @0))
7680 /* Simplify sinh(x) / cosh(x) -> tanh(x). */
7682 (rdiv (SINH:s @0) (COSH:s @0))
7685 /* Simplify tanh (x) / sinh (x) -> 1.0 / cosh (x). */
7687 (rdiv (TANH:s @0) (SINH:s @0))
7688 (rdiv {build_one_cst (type);} (COSH @0)))
7690 /* Simplify cos(x) / sin(x) -> 1 / tan(x). */
7692 (rdiv (COS:s @0) (SIN:s @0))
7693 (rdiv { build_one_cst (type); } (TAN @0)))
7695 /* Simplify sin(x) / tan(x) -> cos(x). */
7697 (rdiv (SIN:s @0) (TAN:s @0))
7698 (if (! HONOR_NANS (@0)
7699 && ! HONOR_INFINITIES (@0))
7702 /* Simplify tan(x) / sin(x) -> 1.0 / cos(x). */
7704 (rdiv (TAN:s @0) (SIN:s @0))
7705 (if (! HONOR_NANS (@0)
7706 && ! HONOR_INFINITIES (@0))
7707 (rdiv { build_one_cst (type); } (COS @0))))
7709 /* Simplify pow(x,y) * pow(x,z) -> pow(x,y+z). */
7711 (mult (POW:s @0 @1) (POW:s @0 @2))
7712 (POW @0 (plus @1 @2)))
7714 /* Simplify pow(x,y) * pow(z,y) -> pow(x*z,y). */
7716 (mult (POW:s @0 @1) (POW:s @2 @1))
7717 (POW (mult @0 @2) @1))
7719 /* Simplify powi(x,y) * powi(z,y) -> powi(x*z,y). */
7721 (mult (POWI:s @0 @1) (POWI:s @2 @1))
7722 (POWI (mult @0 @2) @1))
7724 /* Simplify pow(x,c) / x -> pow(x,c-1). */
7726 (rdiv (POW:s @0 REAL_CST@1) @0)
7727 (if (!TREE_OVERFLOW (@1))
7728 (POW @0 (minus @1 { build_one_cst (type); }))))
7730 /* Simplify x / pow (y,z) -> x * pow(y,-z). */
7732 (rdiv @0 (POW:s @1 @2))
7733 (mult @0 (POW @1 (negate @2))))
7738 /* sqrt(sqrt(x)) -> pow(x,1/4). */
7741 (pows @0 { build_real (type, dconst_quarter ()); }))
7742 /* sqrt(cbrt(x)) -> pow(x,1/6). */
7745 (pows @0 { build_real_truncate (type, dconst_sixth ()); }))
7746 /* cbrt(sqrt(x)) -> pow(x,1/6). */
7749 (pows @0 { build_real_truncate (type, dconst_sixth ()); }))
7750 /* cbrt(cbrt(x)) -> pow(x,1/9), iff x is nonnegative. */
7752 (cbrts (cbrts tree_expr_nonnegative_p@0))
7753 (pows @0 { build_real_truncate (type, dconst_ninth ()); }))
7754 /* sqrt(pow(x,y)) -> pow(|x|,y*0.5). */
7756 (sqrts (pows @0 @1))
7757 (pows (abs @0) (mult @1 { build_real (type, dconsthalf); })))
7758 /* cbrt(pow(x,y)) -> pow(x,y/3), iff x is nonnegative. */
7760 (cbrts (pows tree_expr_nonnegative_p@0 @1))
7761 (pows @0 (mult @1 { build_real_truncate (type, dconst_third ()); })))
7762 /* pow(sqrt(x),y) -> pow(x,y*0.5). */
7764 (pows (sqrts @0) @1)
7765 (pows @0 (mult @1 { build_real (type, dconsthalf); })))
7766 /* pow(cbrt(x),y) -> pow(x,y/3) iff x is nonnegative. */
7768 (pows (cbrts tree_expr_nonnegative_p@0) @1)
7769 (pows @0 (mult @1 { build_real_truncate (type, dconst_third ()); })))
7770 /* pow(pow(x,y),z) -> pow(x,y*z) iff x is nonnegative. */
7772 (pows (pows tree_expr_nonnegative_p@0 @1) @2)
7773 (pows @0 (mult @1 @2))))
7775 /* cabs(x+xi) -> fabs(x)*sqrt(2). */
7777 (CABS (complex @0 @0))
7778 (mult (abs @0) { build_real_truncate (type, dconst_sqrt2 ()); }))
7780 /* hypot(x,x) -> fabs(x)*sqrt(2). */
7783 (mult (abs @0) { build_real_truncate (type, dconst_sqrt2 ()); }))
7785 /* cexp(x+yi) -> exp(x)*cexpi(y). */
7790 (cexps compositional_complex@0)
7791 (if (targetm.libc_has_function (function_c99_math_complex, TREE_TYPE (@0)))
7793 (mult (exps@1 (realpart @0)) (realpart (cexpis:type@2 (imagpart @0))))
7794 (mult @1 (imagpart @2)))))))
7796 (if (canonicalize_math_p ())
7797 /* floor(x) -> trunc(x) if x is nonnegative. */
7798 (for floors (FLOOR_ALL)
7801 (floors tree_expr_nonnegative_p@0)
7804 (match double_value_p
7806 (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == double_type_node)))
7807 (for froms (BUILT_IN_TRUNCL
7819 /* truncl(extend(x)) -> extend(trunc(x)), etc., if x is a double. */
7820 (if (optimize && canonicalize_math_p ())
7822 (froms (convert double_value_p@0))
7823 (convert (tos @0)))))
7825 (match float_value_p
7827 (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == float_type_node)))
7828 (for froms (BUILT_IN_TRUNCL BUILT_IN_TRUNC
7829 BUILT_IN_FLOORL BUILT_IN_FLOOR
7830 BUILT_IN_CEILL BUILT_IN_CEIL
7831 BUILT_IN_ROUNDL BUILT_IN_ROUND
7832 BUILT_IN_NEARBYINTL BUILT_IN_NEARBYINT
7833 BUILT_IN_RINTL BUILT_IN_RINT)
7834 tos (BUILT_IN_TRUNCF BUILT_IN_TRUNCF
7835 BUILT_IN_FLOORF BUILT_IN_FLOORF
7836 BUILT_IN_CEILF BUILT_IN_CEILF
7837 BUILT_IN_ROUNDF BUILT_IN_ROUNDF
7838 BUILT_IN_NEARBYINTF BUILT_IN_NEARBYINTF
7839 BUILT_IN_RINTF BUILT_IN_RINTF)
7840 /* truncl(extend(x)) and trunc(extend(x)) -> extend(truncf(x)), etc.,
7842 (if (optimize && canonicalize_math_p ()
7843 && targetm.libc_has_function (function_c99_misc, NULL_TREE))
7845 (froms (convert float_value_p@0))
7846 (convert (tos @0)))))
7849 (match float16_value_p
7851 (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == float16_type_node)))
7852 (for froms (BUILT_IN_TRUNCL BUILT_IN_TRUNC BUILT_IN_TRUNCF
7853 BUILT_IN_FLOORL BUILT_IN_FLOOR BUILT_IN_FLOORF
7854 BUILT_IN_CEILL BUILT_IN_CEIL BUILT_IN_CEILF
7855 BUILT_IN_ROUNDEVENL BUILT_IN_ROUNDEVEN BUILT_IN_ROUNDEVENF
7856 BUILT_IN_ROUNDL BUILT_IN_ROUND BUILT_IN_ROUNDF
7857 BUILT_IN_NEARBYINTL BUILT_IN_NEARBYINT BUILT_IN_NEARBYINTF
7858 BUILT_IN_RINTL BUILT_IN_RINT BUILT_IN_RINTF
7859 BUILT_IN_SQRTL BUILT_IN_SQRT BUILT_IN_SQRTF)
7860 tos (IFN_TRUNC IFN_TRUNC IFN_TRUNC
7861 IFN_FLOOR IFN_FLOOR IFN_FLOOR
7862 IFN_CEIL IFN_CEIL IFN_CEIL
7863 IFN_ROUNDEVEN IFN_ROUNDEVEN IFN_ROUNDEVEN
7864 IFN_ROUND IFN_ROUND IFN_ROUND
7865 IFN_NEARBYINT IFN_NEARBYINT IFN_NEARBYINT
7866 IFN_RINT IFN_RINT IFN_RINT
7867 IFN_SQRT IFN_SQRT IFN_SQRT)
7868 /* (_Float16) round ((doube) x) -> __built_in_roundf16 (x), etc.,
7869 if x is a _Float16. */
7871 (convert (froms (convert float16_value_p@0)))
7873 && types_match (type, TREE_TYPE (@0))
7874 && direct_internal_fn_supported_p (as_internal_fn (tos),
7875 type, OPTIMIZE_FOR_BOTH))
7878 /* Simplify (trunc)copysign ((extend)x, (extend)y) to copysignf (x, y),
7879 x,y is float value, similar for _Float16/double. */
7880 (for copysigns (COPYSIGN_ALL)
7882 (convert (copysigns (convert@2 @0) (convert @1)))
7884 && !HONOR_SNANS (@2)
7885 && types_match (type, TREE_TYPE (@0))
7886 && types_match (type, TREE_TYPE (@1))
7887 && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (@2))
7888 && direct_internal_fn_supported_p (IFN_COPYSIGN,
7889 type, OPTIMIZE_FOR_BOTH))
7890 (IFN_COPYSIGN @0 @1))))
7892 (for froms (BUILT_IN_FMAF BUILT_IN_FMA BUILT_IN_FMAL)
7893 tos (IFN_FMA IFN_FMA IFN_FMA)
7895 (convert (froms (convert@3 @0) (convert @1) (convert @2)))
7896 (if (flag_unsafe_math_optimizations
7898 && FLOAT_TYPE_P (type)
7899 && FLOAT_TYPE_P (TREE_TYPE (@3))
7900 && types_match (type, TREE_TYPE (@0))
7901 && types_match (type, TREE_TYPE (@1))
7902 && types_match (type, TREE_TYPE (@2))
7903 && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (@3))
7904 && direct_internal_fn_supported_p (as_internal_fn (tos),
7905 type, OPTIMIZE_FOR_BOTH))
7908 (for maxmin (max min)
7910 (convert (maxmin (convert@2 @0) (convert @1)))
7912 && FLOAT_TYPE_P (type)
7913 && FLOAT_TYPE_P (TREE_TYPE (@2))
7914 && types_match (type, TREE_TYPE (@0))
7915 && types_match (type, TREE_TYPE (@1))
7916 && element_precision (type) < element_precision (TREE_TYPE (@2)))
7920 (for froms (XFLOORL XCEILL XROUNDL XRINTL)
7921 tos (XFLOOR XCEIL XROUND XRINT)
7922 /* llfloorl(extend(x)) -> llfloor(x), etc., if x is a double. */
7923 (if (optimize && canonicalize_math_p ())
7925 (froms (convert double_value_p@0))
7928 (for froms (XFLOORL XCEILL XROUNDL XRINTL
7929 XFLOOR XCEIL XROUND XRINT)
7930 tos (XFLOORF XCEILF XROUNDF XRINTF)
7931 /* llfloorl(extend(x)) and llfloor(extend(x)) -> llfloorf(x), etc.,
7933 (if (optimize && canonicalize_math_p ())
7935 (froms (convert float_value_p@0))
7938 (if (canonicalize_math_p ())
7939 /* xfloor(x) -> fix_trunc(x) if x is nonnegative. */
7940 (for floors (IFLOOR LFLOOR LLFLOOR)
7942 (floors tree_expr_nonnegative_p@0)
7945 (if (canonicalize_math_p ())
7946 /* xfloor(x) -> fix_trunc(x), etc., if x is integer valued. */
7947 (for fns (IFLOOR LFLOOR LLFLOOR
7949 IROUND LROUND LLROUND)
7951 (fns integer_valued_real_p@0)
7953 (if (!flag_errno_math)
7954 /* xrint(x) -> fix_trunc(x), etc., if x is integer valued. */
7955 (for rints (IRINT LRINT LLRINT)
7957 (rints integer_valued_real_p@0)
7960 (if (canonicalize_math_p ())
7961 (for ifn (IFLOOR ICEIL IROUND IRINT)
7962 lfn (LFLOOR LCEIL LROUND LRINT)
7963 llfn (LLFLOOR LLCEIL LLROUND LLRINT)
7964 /* Canonicalize iround (x) to lround (x) on ILP32 targets where
7965 sizeof (int) == sizeof (long). */
7966 (if (TYPE_PRECISION (integer_type_node)
7967 == TYPE_PRECISION (long_integer_type_node))
7970 (lfn:long_integer_type_node @0)))
7971 /* Canonicalize llround (x) to lround (x) on LP64 targets where
7972 sizeof (long long) == sizeof (long). */
7973 (if (TYPE_PRECISION (long_long_integer_type_node)
7974 == TYPE_PRECISION (long_integer_type_node))
7977 (lfn:long_integer_type_node @0)))))
7979 /* cproj(x) -> x if we're ignoring infinities. */
7982 (if (!HONOR_INFINITIES (type))
7985 /* If the real part is inf and the imag part is known to be
7986 nonnegative, return (inf + 0i). */
7988 (CPROJ (complex REAL_CST@0 tree_expr_nonnegative_p@1))
7989 (if (real_isinf (TREE_REAL_CST_PTR (@0)))
7990 { build_complex_inf (type, false); }))
7992 /* If the imag part is inf, return (inf+I*copysign(0,imag)). */
7994 (CPROJ (complex @0 REAL_CST@1))
7995 (if (real_isinf (TREE_REAL_CST_PTR (@1)))
7996 { build_complex_inf (type, TREE_REAL_CST_PTR (@1)->sign); }))
8002 (pows @0 REAL_CST@1)
8004 const REAL_VALUE_TYPE *value = TREE_REAL_CST_PTR (@1);
8005 REAL_VALUE_TYPE tmp;
8008 /* pow(x,0) -> 1. */
8009 (if (real_equal (value, &dconst0))
8010 { build_real (type, dconst1); })
8011 /* pow(x,1) -> x. */
8012 (if (real_equal (value, &dconst1))
8014 /* pow(x,-1) -> 1/x. */
8015 (if (real_equal (value, &dconstm1))
8016 (rdiv { build_real (type, dconst1); } @0))
8017 /* pow(x,0.5) -> sqrt(x). */
8018 (if (flag_unsafe_math_optimizations
8019 && canonicalize_math_p ()
8020 && real_equal (value, &dconsthalf))
8022 /* pow(x,1/3) -> cbrt(x). */
8023 (if (flag_unsafe_math_optimizations
8024 && canonicalize_math_p ()
8025 && (tmp = real_value_truncate (TYPE_MODE (type), dconst_third ()),
8026 real_equal (value, &tmp)))
8029 /* powi(1,x) -> 1. */
8031 (POWI real_onep@0 @1)
8035 (POWI @0 INTEGER_CST@1)
8037 /* powi(x,0) -> 1. */
8038 (if (wi::to_wide (@1) == 0)
8039 { build_real (type, dconst1); })
8040 /* powi(x,1) -> x. */
8041 (if (wi::to_wide (@1) == 1)
8043 /* powi(x,-1) -> 1/x. */
8044 (if (wi::to_wide (@1) == -1)
8045 (rdiv { build_real (type, dconst1); } @0))))
8047 /* Narrowing of arithmetic and logical operations.
8049 These are conceptually similar to the transformations performed for
8050 the C/C++ front-ends by shorten_binary_op and shorten_compare. Long
8051 term we want to move all that code out of the front-ends into here. */
8053 /* Convert (outertype)((innertype0)a+(innertype1)b)
8054 into ((newtype)a+(newtype)b) where newtype
8055 is the widest mode from all of these. */
8056 (for op (plus minus mult rdiv)
8058 (convert (op:s@0 (convert1?@3 @1) (convert2?@4 @2)))
8059 /* If we have a narrowing conversion of an arithmetic operation where
8060 both operands are widening conversions from the same type as the outer
8061 narrowing conversion. Then convert the innermost operands to a
8062 suitable unsigned type (to avoid introducing undefined behavior),
8063 perform the operation and convert the result to the desired type. */
8064 (if (INTEGRAL_TYPE_P (type)
8067 /* We check for type compatibility between @0 and @1 below,
8068 so there's no need to check that @2/@4 are integral types. */
8069 && INTEGRAL_TYPE_P (TREE_TYPE (@1))
8070 && INTEGRAL_TYPE_P (TREE_TYPE (@3))
8071 /* The precision of the type of each operand must match the
8072 precision of the mode of each operand, similarly for the
8074 && type_has_mode_precision_p (TREE_TYPE (@1))
8075 && type_has_mode_precision_p (TREE_TYPE (@2))
8076 && type_has_mode_precision_p (type)
8077 /* The inner conversion must be a widening conversion. */
8078 && TYPE_PRECISION (TREE_TYPE (@3)) > TYPE_PRECISION (TREE_TYPE (@1))
8079 && types_match (@1, type)
8080 && (types_match (@1, @2)
8081 /* Or the second operand is const integer or converted const
8082 integer from valueize. */
8083 || poly_int_tree_p (@4)))
8084 (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
8085 (op @1 (convert @2))
8086 (with { tree utype = unsigned_type_for (TREE_TYPE (@1)); }
8087 (convert (op (convert:utype @1)
8088 (convert:utype @2)))))
8089 (if (FLOAT_TYPE_P (type)
8090 && DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0))
8091 == DECIMAL_FLOAT_TYPE_P (type))
8092 (with { tree arg0 = strip_float_extensions (@1);
8093 tree arg1 = strip_float_extensions (@2);
8094 tree itype = TREE_TYPE (@0);
8095 tree ty1 = TREE_TYPE (arg0);
8096 tree ty2 = TREE_TYPE (arg1);
8097 enum tree_code code = TREE_CODE (itype); }
8098 (if (FLOAT_TYPE_P (ty1)
8099 && FLOAT_TYPE_P (ty2))
8100 (with { tree newtype = type;
8101 if (TYPE_MODE (ty1) == SDmode
8102 || TYPE_MODE (ty2) == SDmode
8103 || TYPE_MODE (type) == SDmode)
8104 newtype = dfloat32_type_node;
8105 if (TYPE_MODE (ty1) == DDmode
8106 || TYPE_MODE (ty2) == DDmode
8107 || TYPE_MODE (type) == DDmode)
8108 newtype = dfloat64_type_node;
8109 if (TYPE_MODE (ty1) == TDmode
8110 || TYPE_MODE (ty2) == TDmode
8111 || TYPE_MODE (type) == TDmode)
8112 newtype = dfloat128_type_node; }
8113 (if ((newtype == dfloat32_type_node
8114 || newtype == dfloat64_type_node
8115 || newtype == dfloat128_type_node)
8117 && types_match (newtype, type))
8118 (op (convert:newtype @1) (convert:newtype @2))
8119 (with { if (element_precision (ty1) > element_precision (newtype))
8121 if (element_precision (ty2) > element_precision (newtype))
8123 /* Sometimes this transformation is safe (cannot
8124 change results through affecting double rounding
8125 cases) and sometimes it is not. If NEWTYPE is
8126 wider than TYPE, e.g. (float)((long double)double
8127 + (long double)double) converted to
8128 (float)(double + double), the transformation is
8129 unsafe regardless of the details of the types
8130 involved; double rounding can arise if the result
8131 of NEWTYPE arithmetic is a NEWTYPE value half way
8132 between two representable TYPE values but the
8133 exact value is sufficiently different (in the
8134 right direction) for this difference to be
8135 visible in ITYPE arithmetic. If NEWTYPE is the
8136 same as TYPE, however, the transformation may be
8137 safe depending on the types involved: it is safe
8138 if the ITYPE has strictly more than twice as many
8139 mantissa bits as TYPE, can represent infinities
8140 and NaNs if the TYPE can, and has sufficient
8141 exponent range for the product or ratio of two
8142 values representable in the TYPE to be within the
8143 range of normal values of ITYPE. */
8144 (if (element_precision (newtype) < element_precision (itype)
8145 && (!VECTOR_MODE_P (TYPE_MODE (newtype))
8146 || target_supports_op_p (newtype, op, optab_default))
8147 && (flag_unsafe_math_optimizations
8148 || (element_precision (newtype) == element_precision (type)
8149 && real_can_shorten_arithmetic (element_mode (itype),
8150 element_mode (type))
8151 && !excess_precision_type (newtype)))
8152 && !types_match (itype, newtype))
8153 (convert:type (op (convert:newtype @1)
8154 (convert:newtype @2)))
8159 /* This is another case of narrowing, specifically when there's an outer
8160 BIT_AND_EXPR which masks off bits outside the type of the innermost
8161 operands. Like the previous case we have to convert the operands
8162 to unsigned types to avoid introducing undefined behavior for the
8163 arithmetic operation. */
8164 (for op (minus plus)
8166 (bit_and (op:s (convert@2 @0) (convert@3 @1)) INTEGER_CST@4)
8167 (if (INTEGRAL_TYPE_P (type)
8168 /* We check for type compatibility between @0 and @1 below,
8169 so there's no need to check that @1/@3 are integral types. */
8170 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
8171 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
8172 /* The precision of the type of each operand must match the
8173 precision of the mode of each operand, similarly for the
8175 && type_has_mode_precision_p (TREE_TYPE (@0))
8176 && type_has_mode_precision_p (TREE_TYPE (@1))
8177 && type_has_mode_precision_p (type)
8178 /* The inner conversion must be a widening conversion. */
8179 && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
8180 && types_match (@0, @1)
8181 && (tree_int_cst_min_precision (@4, TYPE_SIGN (TREE_TYPE (@0)))
8182 <= TYPE_PRECISION (TREE_TYPE (@0)))
8183 && (wi::to_wide (@4)
8184 & wi::mask (TYPE_PRECISION (TREE_TYPE (@0)),
8185 true, TYPE_PRECISION (type))) == 0)
8186 (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
8187 (with { tree ntype = TREE_TYPE (@0); }
8188 (convert (bit_and (op @0 @1) (convert:ntype @4))))
8189 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
8190 (convert (bit_and (op (convert:utype @0) (convert:utype @1))
8191 (convert:utype @4))))))))
8193 /* Transform (@0 < @1 and @0 < @2) to use min,
8194 (@0 > @1 and @0 > @2) to use max */
8195 (for logic (bit_and bit_and bit_and bit_and bit_ior bit_ior bit_ior bit_ior)
8196 op (lt le gt ge lt le gt ge )
8197 ext (min min max max max max min min )
8199 (logic (op:cs @0 @1) (op:cs @0 @2))
8200 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
8201 && TREE_CODE (@0) != INTEGER_CST)
8202 (op @0 (ext @1 @2)))))
8204 /* Max<bool0, bool1> -> bool0 | bool1
8205 Min<bool0, bool1> -> bool0 & bool1 */
8207 logic (bit_ior bit_and)
8209 (op zero_one_valued_p@0 zero_one_valued_p@1)
8212 /* signbit(x) != 0 ? -x : x -> abs(x)
8213 signbit(x) == 0 ? -x : x -> -abs(x) */
8217 (cond (neeq (sign @0) integer_zerop) (negate @0) @0)
8218 (if (neeq == NE_EXPR)
8220 (negate (abs @0))))))
8223 /* signbit(x) -> 0 if x is nonnegative. */
8224 (SIGNBIT tree_expr_nonnegative_p@0)
8225 { integer_zero_node; })
8228 /* signbit(x) -> x<0 if x doesn't have signed zeros. */
8230 (if (!HONOR_SIGNED_ZEROS (@0))
8231 (convert (lt @0 { build_real (TREE_TYPE (@0), dconst0); }))))
8233 /* Transform comparisons of the form X +- C1 CMP C2 to X CMP C2 -+ C1. */
8235 (for op (plus minus)
8238 (cmp (op@3 @0 INTEGER_CST@1) INTEGER_CST@2)
8239 (if (!TREE_OVERFLOW (@1) && !TREE_OVERFLOW (@2)
8240 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0))
8241 && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@0))
8242 && !TYPE_SATURATING (TREE_TYPE (@0)))
8243 (with { tree res = int_const_binop (rop, @2, @1); }
8244 (if (TREE_OVERFLOW (res)
8245 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
8246 { constant_boolean_node (cmp == NE_EXPR, type); }
8247 (if (single_use (@3))
8248 (cmp @0 { TREE_OVERFLOW (res)
8249 ? drop_tree_overflow (res) : res; }))))))))
8250 (for cmp (lt le gt ge)
8251 (for op (plus minus)
8254 (cmp (op@3 @0 INTEGER_CST@1) INTEGER_CST@2)
8255 (if (!TREE_OVERFLOW (@1) && !TREE_OVERFLOW (@2)
8256 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
8257 (with { tree res = int_const_binop (rop, @2, @1); }
8258 (if (TREE_OVERFLOW (res))
8260 fold_overflow_warning (("assuming signed overflow does not occur "
8261 "when simplifying conditional to constant"),
8262 WARN_STRICT_OVERFLOW_CONDITIONAL);
8263 bool less = cmp == LE_EXPR || cmp == LT_EXPR;
8264 /* wi::ges_p (@2, 0) should be sufficient for a signed type. */
8265 bool ovf_high = wi::lt_p (wi::to_wide (@1), 0,
8266 TYPE_SIGN (TREE_TYPE (@1)))
8267 != (op == MINUS_EXPR);
8268 constant_boolean_node (less == ovf_high, type);
8270 (if (single_use (@3))
8273 fold_overflow_warning (("assuming signed overflow does not occur "
8274 "when changing X +- C1 cmp C2 to "
8276 WARN_STRICT_OVERFLOW_COMPARISON);
8278 (cmp @0 { res; })))))))))
8280 /* Canonicalizations of BIT_FIELD_REFs. */
8283 (BIT_FIELD_REF (BIT_FIELD_REF @0 @1 @2) @3 @4)
8284 (BIT_FIELD_REF @0 @3 { const_binop (PLUS_EXPR, bitsizetype, @2, @4); }))
8287 (BIT_FIELD_REF (view_convert @0) @1 @2)
8288 (BIT_FIELD_REF @0 @1 @2))
8291 (BIT_FIELD_REF @0 @1 integer_zerop)
8292 (if (tree_int_cst_equal (@1, TYPE_SIZE (TREE_TYPE (@0))))
8296 (BIT_FIELD_REF @0 @1 @2)
8298 (if (TREE_CODE (TREE_TYPE (@0)) == COMPLEX_TYPE
8299 && tree_int_cst_equal (@1, TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))))
8301 (if (integer_zerop (@2))
8302 (view_convert (realpart @0)))
8303 (if (tree_int_cst_equal (@2, TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))))
8304 (view_convert (imagpart @0)))))
8305 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
8306 && INTEGRAL_TYPE_P (type)
8307 /* On GIMPLE this should only apply to register arguments. */
8308 && (! GIMPLE || is_gimple_reg (@0))
8309 /* A bit-field-ref that referenced the full argument can be stripped. */
8310 && ((compare_tree_int (@1, TYPE_PRECISION (TREE_TYPE (@0))) == 0
8311 && integer_zerop (@2))
8312 /* Low-parts can be reduced to integral conversions.
8313 ??? The following doesn't work for PDP endian. */
8314 || (BYTES_BIG_ENDIAN == WORDS_BIG_ENDIAN
8315 /* But only do this after vectorization. */
8316 && canonicalize_math_after_vectorization_p ()
8317 /* Don't even think about BITS_BIG_ENDIAN. */
8318 && TYPE_PRECISION (TREE_TYPE (@0)) % BITS_PER_UNIT == 0
8319 && TYPE_PRECISION (type) % BITS_PER_UNIT == 0
8320 && compare_tree_int (@2, (BYTES_BIG_ENDIAN
8321 ? (TYPE_PRECISION (TREE_TYPE (@0))
8322 - TYPE_PRECISION (type))
8326 /* Simplify vector extracts. */
8329 (BIT_FIELD_REF CONSTRUCTOR@0 @1 @2)
8330 (if (VECTOR_TYPE_P (TREE_TYPE (@0))
8331 && tree_fits_uhwi_p (TYPE_SIZE (type))
8332 && ((tree_to_uhwi (TYPE_SIZE (type))
8333 == tree_to_uhwi (TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))))
8334 || (VECTOR_TYPE_P (type)
8335 && (tree_to_uhwi (TYPE_SIZE (TREE_TYPE (type)))
8336 == tree_to_uhwi (TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0))))))))
8339 tree ctor = (TREE_CODE (@0) == SSA_NAME
8340 ? gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0)) : @0);
8341 tree eltype = TREE_TYPE (TREE_TYPE (ctor));
8342 unsigned HOST_WIDE_INT width = tree_to_uhwi (TYPE_SIZE (eltype));
8343 unsigned HOST_WIDE_INT n = tree_to_uhwi (@1);
8344 unsigned HOST_WIDE_INT idx = tree_to_uhwi (@2);
8347 && (idx % width) == 0
8349 && known_le ((idx + n) / width,
8350 TYPE_VECTOR_SUBPARTS (TREE_TYPE (ctor))))
8355 /* Constructor elements can be subvectors. */
8357 if (CONSTRUCTOR_NELTS (ctor) != 0)
8359 tree cons_elem = TREE_TYPE (CONSTRUCTOR_ELT (ctor, 0)->value);
8360 if (TREE_CODE (cons_elem) == VECTOR_TYPE)
8361 k = TYPE_VECTOR_SUBPARTS (cons_elem);
8363 unsigned HOST_WIDE_INT elt, count, const_k;
8366 /* We keep an exact subset of the constructor elements. */
8367 (if (multiple_p (idx, k, &elt) && multiple_p (n, k, &count))
8368 (if (CONSTRUCTOR_NELTS (ctor) == 0)
8369 { build_zero_cst (type); }
8371 (if (elt < CONSTRUCTOR_NELTS (ctor))
8372 (view_convert { CONSTRUCTOR_ELT (ctor, elt)->value; })
8373 { build_zero_cst (type); })
8374 /* We don't want to emit new CTORs unless the old one goes away.
8375 ??? Eventually allow this if the CTOR ends up constant or
8377 (if (single_use (@0))
8380 vec<constructor_elt, va_gc> *vals;
8381 vec_alloc (vals, count);
8382 bool constant_p = true;
8384 for (unsigned i = 0;
8385 i < count && elt + i < CONSTRUCTOR_NELTS (ctor); ++i)
8387 tree e = CONSTRUCTOR_ELT (ctor, elt + i)->value;
8388 CONSTRUCTOR_APPEND_ELT (vals, NULL_TREE, e);
8389 if (!CONSTANT_CLASS_P (e))
8392 tree evtype = (types_match (TREE_TYPE (type),
8393 TREE_TYPE (TREE_TYPE (ctor)))
8395 : build_vector_type (TREE_TYPE (TREE_TYPE (ctor)),
8397 /* We used to build a CTOR in the non-constant case here
8398 but that's not a GIMPLE value. We'd have to expose this
8399 operation somehow so the code generation can properly
8400 split it out to a separate stmt. */
8401 res = (constant_p ? build_vector_from_ctor (evtype, vals)
8402 : (GIMPLE ? NULL_TREE : build_constructor (evtype, vals)));
8405 (view_convert { res; })))))))
8406 /* The bitfield references a single constructor element. */
8407 (if (k.is_constant (&const_k)
8408 && idx + n <= (idx / const_k + 1) * const_k)
8410 (if (CONSTRUCTOR_NELTS (ctor) <= idx / const_k)
8411 { build_zero_cst (type); })
8413 (view_convert { CONSTRUCTOR_ELT (ctor, idx / const_k)->value; }))
8414 (BIT_FIELD_REF { CONSTRUCTOR_ELT (ctor, idx / const_k)->value; }
8415 @1 { bitsize_int ((idx % const_k) * width); })))))))))
8417 /* Simplify a bit extraction from a bit insertion for the cases with
8418 the inserted element fully covering the extraction or the insertion
8419 not touching the extraction. */
8421 (BIT_FIELD_REF (bit_insert @0 @1 @ipos) @rsize @rpos)
8424 unsigned HOST_WIDE_INT isize;
8425 if (INTEGRAL_TYPE_P (TREE_TYPE (@1)))
8426 isize = TYPE_PRECISION (TREE_TYPE (@1));
8428 isize = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (@1)));
8431 (if ((!INTEGRAL_TYPE_P (TREE_TYPE (@1))
8432 || type_has_mode_precision_p (TREE_TYPE (@1)))
8433 && wi::leu_p (wi::to_wide (@ipos), wi::to_wide (@rpos))
8434 && wi::leu_p (wi::to_wide (@rpos) + wi::to_wide (@rsize),
8435 wi::to_wide (@ipos) + isize))
8436 (BIT_FIELD_REF @1 @rsize { wide_int_to_tree (bitsizetype,
8438 - wi::to_wide (@ipos)); }))
8439 (if (wi::eq_p (wi::to_wide (@ipos), wi::to_wide (@rpos))
8440 && compare_tree_int (@rsize, isize) == 0)
8442 (if (wi::geu_p (wi::to_wide (@ipos),
8443 wi::to_wide (@rpos) + wi::to_wide (@rsize))
8444 || wi::geu_p (wi::to_wide (@rpos),
8445 wi::to_wide (@ipos) + isize))
8446 (BIT_FIELD_REF @0 @rsize @rpos)))))
8448 /* Simplify vector inserts of other vector extracts to a permute. */
8450 (bit_insert @0 (BIT_FIELD_REF@2 @1 @rsize @rpos) @ipos)
8451 (if (VECTOR_TYPE_P (type)
8452 && types_match (@0, @1)
8453 && types_match (TREE_TYPE (TREE_TYPE (@0)), TREE_TYPE (@2))
8454 && TYPE_VECTOR_SUBPARTS (type).is_constant ())
8457 unsigned HOST_WIDE_INT elsz
8458 = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (TREE_TYPE (@1))));
8459 poly_uint64 relt = exact_div (tree_to_poly_uint64 (@rpos), elsz);
8460 poly_uint64 ielt = exact_div (tree_to_poly_uint64 (@ipos), elsz);
8461 unsigned nunits = TYPE_VECTOR_SUBPARTS (type).to_constant ();
8462 vec_perm_builder builder;
8463 builder.new_vector (nunits, nunits, 1);
8464 for (unsigned i = 0; i < nunits; ++i)
8465 builder.quick_push (known_eq (ielt, i) ? nunits + relt : i);
8466 vec_perm_indices sel (builder, 2, nunits);
8468 (if (!VECTOR_MODE_P (TYPE_MODE (type))
8469 || can_vec_perm_const_p (TYPE_MODE (type), TYPE_MODE (type), sel, false))
8470 (vec_perm @0 @1 { vec_perm_indices_to_tree
8471 (build_vector_type (ssizetype, nunits), sel); })))))
8473 (if (canonicalize_math_after_vectorization_p ())
8476 (fmas:c (negate @0) @1 @2)
8477 (IFN_FNMA @0 @1 @2))
8479 (fmas @0 @1 (negate @2))
8482 (fmas:c (negate @0) @1 (negate @2))
8483 (IFN_FNMS @0 @1 @2))
8485 (negate (fmas@3 @0 @1 @2))
8486 (if (single_use (@3))
8487 (IFN_FNMS @0 @1 @2))))
8490 (IFN_FMS:c (negate @0) @1 @2)
8491 (IFN_FNMS @0 @1 @2))
8493 (IFN_FMS @0 @1 (negate @2))
8496 (IFN_FMS:c (negate @0) @1 (negate @2))
8497 (IFN_FNMA @0 @1 @2))
8499 (negate (IFN_FMS@3 @0 @1 @2))
8500 (if (single_use (@3))
8501 (IFN_FNMA @0 @1 @2)))
8504 (IFN_FNMA:c (negate @0) @1 @2)
8507 (IFN_FNMA @0 @1 (negate @2))
8508 (IFN_FNMS @0 @1 @2))
8510 (IFN_FNMA:c (negate @0) @1 (negate @2))
8513 (negate (IFN_FNMA@3 @0 @1 @2))
8514 (if (single_use (@3))
8515 (IFN_FMS @0 @1 @2)))
8518 (IFN_FNMS:c (negate @0) @1 @2)
8521 (IFN_FNMS @0 @1 (negate @2))
8522 (IFN_FNMA @0 @1 @2))
8524 (IFN_FNMS:c (negate @0) @1 (negate @2))
8527 (negate (IFN_FNMS@3 @0 @1 @2))
8528 (if (single_use (@3))
8529 (IFN_FMA @0 @1 @2))))
8531 /* CLZ simplifications. */
8536 (op (clz:s@2 @0) INTEGER_CST@1)
8537 (if (integer_zerop (@1) && single_use (@2))
8538 /* clz(X) == 0 is (int)X < 0 and clz(X) != 0 is (int)X >= 0. */
8539 (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
8540 (cmp (convert:stype @0) { build_zero_cst (stype); }))
8541 /* clz(X) == (prec-1) is X == 1 and clz(X) != (prec-1) is X != 1. */
8542 (if (wi::to_wide (@1) == TYPE_PRECISION (TREE_TYPE (@0)) - 1)
8543 (op @0 { build_one_cst (TREE_TYPE (@0)); }))))))
8547 (op (IFN_CLZ:s@2 @0 @3) INTEGER_CST@1)
8548 (if (integer_zerop (@1) && single_use (@2))
8549 /* clz(X) == 0 is (int)X < 0 and clz(X) != 0 is (int)X >= 0. */
8550 (with { tree type0 = TREE_TYPE (@0);
8551 tree stype = signed_type_for (TREE_TYPE (@0));
8552 /* Punt if clz(0) == 0. */
8553 if (integer_zerop (@3))
8557 (cmp (convert:stype @0) { build_zero_cst (stype); })))
8558 /* clz(X) == (prec-1) is X == 1 and clz(X) != (prec-1) is X != 1. */
8559 (with { bool ok = true;
8560 tree type0 = TREE_TYPE (@0);
8561 /* Punt if clz(0) == prec - 1. */
8562 if (wi::to_widest (@3) == TYPE_PRECISION (type0) - 1)
8565 (if (ok && wi::to_wide (@1) == (TYPE_PRECISION (type0) - 1))
8566 (op @0 { build_one_cst (type0); }))))))
8568 /* CTZ simplifications. */
8570 (for op (ge gt le lt)
8573 /* __builtin_ctz (x) >= C -> (x & ((1 << C) - 1)) == 0. */
8574 (op (ctz:s @0) INTEGER_CST@1)
8575 (with { bool ok = true;
8576 HOST_WIDE_INT val = 0;
8577 if (!tree_fits_shwi_p (@1))
8581 val = tree_to_shwi (@1);
8582 /* Canonicalize to >= or <. */
8583 if (op == GT_EXPR || op == LE_EXPR)
8585 if (val == HOST_WIDE_INT_MAX)
8591 tree type0 = TREE_TYPE (@0);
8592 int prec = TYPE_PRECISION (type0);
8594 (if (ok && prec <= MAX_FIXED_MODE_SIZE)
8596 { constant_boolean_node (cmp == EQ_EXPR ? true : false, type); }
8598 { constant_boolean_node (cmp == EQ_EXPR ? false : true, type); }
8599 (cmp (bit_and @0 { wide_int_to_tree (type0,
8600 wi::mask (val, false, prec)); })
8601 { build_zero_cst (type0); })))))))
8604 /* __builtin_ctz (x) == C -> (x & ((1 << (C + 1)) - 1)) == (1 << C). */
8605 (op (ctz:s @0) INTEGER_CST@1)
8606 (with { tree type0 = TREE_TYPE (@0);
8607 int prec = TYPE_PRECISION (type0);
8609 (if (prec <= MAX_FIXED_MODE_SIZE)
8610 (if (tree_int_cst_sgn (@1) < 0 || wi::to_widest (@1) >= prec)
8611 { constant_boolean_node (op == EQ_EXPR ? false : true, type); }
8612 (op (bit_and @0 { wide_int_to_tree (type0,
8613 wi::mask (tree_to_uhwi (@1) + 1,
8615 { wide_int_to_tree (type0,
8616 wi::shifted_mask (tree_to_uhwi (@1), 1,
8617 false, prec)); })))))))
8618 (for op (ge gt le lt)
8621 /* __builtin_ctz (x) >= C -> (x & ((1 << C) - 1)) == 0. */
8622 (op (IFN_CTZ:s @0 @2) INTEGER_CST@1)
8623 (with { bool ok = true;
8624 HOST_WIDE_INT val = 0;
8625 if (!tree_fits_shwi_p (@1))
8629 val = tree_to_shwi (@1);
8630 /* Canonicalize to >= or <. */
8631 if (op == GT_EXPR || op == LE_EXPR)
8633 if (val == HOST_WIDE_INT_MAX)
8639 HOST_WIDE_INT zero_val = tree_to_shwi (@2);
8640 tree type0 = TREE_TYPE (@0);
8641 int prec = TYPE_PRECISION (type0);
8642 if (prec > MAX_FIXED_MODE_SIZE)
8646 (if (ok && zero_val >= val)
8647 { constant_boolean_node (cmp == EQ_EXPR ? true : false, type); })
8649 (if (ok && zero_val < val)
8650 { constant_boolean_node (cmp == EQ_EXPR ? false : true, type); })
8651 (if (ok && (zero_val < 0 || zero_val >= prec))
8652 (cmp (bit_and @0 { wide_int_to_tree (type0,
8653 wi::mask (val, false, prec)); })
8654 { build_zero_cst (type0); })))))))
8657 /* __builtin_ctz (x) == C -> (x & ((1 << (C + 1)) - 1)) == (1 << C). */
8658 (op (IFN_CTZ:s @0 @2) INTEGER_CST@1)
8659 (with { HOST_WIDE_INT zero_val = tree_to_shwi (@2);
8660 tree type0 = TREE_TYPE (@0);
8661 int prec = TYPE_PRECISION (type0);
8663 (if (prec <= MAX_FIXED_MODE_SIZE)
8664 (if (tree_int_cst_sgn (@1) < 0 || wi::to_widest (@1) >= prec)
8665 (if (zero_val != wi::to_widest (@1))
8666 { constant_boolean_node (op == EQ_EXPR ? false : true, type); })
8667 (if (zero_val < 0 || zero_val >= prec)
8668 (op (bit_and @0 { wide_int_to_tree (type0,
8669 wi::mask (tree_to_uhwi (@1) + 1,
8671 { wide_int_to_tree (type0,
8672 wi::shifted_mask (tree_to_uhwi (@1), 1,
8673 false, prec)); })))))))
8675 /* POPCOUNT simplifications. */
8676 /* popcount(X) + popcount(Y) is popcount(X|Y) when X&Y must be zero. */
8678 (plus (POPCOUNT:s @0) (POPCOUNT:s @1))
8679 (if (INTEGRAL_TYPE_P (type)
8680 && wi::bit_and (tree_nonzero_bits (@0), tree_nonzero_bits (@1)) == 0)
8681 (POPCOUNT (bit_ior @0 @1))))
8683 /* popcount(X) == 0 is X == 0, and related (in)equalities. */
8684 (for popcount (POPCOUNT)
8685 (for cmp (le eq ne gt)
8688 (cmp (popcount @0) integer_zerop)
8689 (rep @0 { build_zero_cst (TREE_TYPE (@0)); }))))
8691 /* popcount(bswap(x)) is popcount(x). */
8692 (for popcount (POPCOUNT)
8693 (for bswap (BUILT_IN_BSWAP16 BUILT_IN_BSWAP32
8694 BUILT_IN_BSWAP64 BUILT_IN_BSWAP128)
8696 (popcount (convert?@0 (bswap:s@1 @2)))
8697 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
8698 && INTEGRAL_TYPE_P (TREE_TYPE (@1)))
8699 (with { tree type0 = TREE_TYPE (@0);
8700 tree type1 = TREE_TYPE (@1);
8701 unsigned int prec0 = TYPE_PRECISION (type0);
8702 unsigned int prec1 = TYPE_PRECISION (type1); }
8703 (if (prec0 == prec1 || (prec0 > prec1 && TYPE_UNSIGNED (type1)))
8704 (popcount (convert:type0 (convert:type1 @2)))))))))
8706 /* popcount(rotate(X Y)) is popcount(X). */
8707 (for popcount (POPCOUNT)
8708 (for rot (lrotate rrotate)
8710 (popcount (convert?@0 (rot:s@1 @2 @3)))
8711 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
8712 && INTEGRAL_TYPE_P (TREE_TYPE (@1))
8713 && (GIMPLE || !TREE_SIDE_EFFECTS (@3)))
8714 (with { tree type0 = TREE_TYPE (@0);
8715 tree type1 = TREE_TYPE (@1);
8716 unsigned int prec0 = TYPE_PRECISION (type0);
8717 unsigned int prec1 = TYPE_PRECISION (type1); }
8718 (if (prec0 == prec1 || (prec0 > prec1 && TYPE_UNSIGNED (type1)))
8719 (popcount (convert:type0 @2))))))))
8721 /* Canonicalize POPCOUNT(x)&1 as PARITY(X). */
8723 (bit_and (POPCOUNT @0) integer_onep)
8726 /* popcount(X&Y) + popcount(X|Y) is popcount(x) + popcount(Y). */
8728 (plus:c (POPCOUNT:s (bit_and:s @0 @1)) (POPCOUNT:s (bit_ior:cs @0 @1)))
8729 (plus (POPCOUNT:type @0) (POPCOUNT:type @1)))
8731 /* popcount(X) + popcount(Y) - popcount(X&Y) is popcount(X|Y). */
8732 /* popcount(X) + popcount(Y) - popcount(X|Y) is popcount(X&Y). */
8733 (for popcount (POPCOUNT)
8734 (for log1 (bit_and bit_ior)
8735 log2 (bit_ior bit_and)
8737 (minus (plus:s (popcount:s @0) (popcount:s @1))
8738 (popcount:s (log1:cs @0 @1)))
8739 (popcount (log2 @0 @1)))
8741 (plus:c (minus:s (popcount:s @0) (popcount:s (log1:cs @0 @1)))
8743 (popcount (log2 @0 @1)))))
8745 /* PARITY simplifications. */
8746 /* parity(~X) is parity(X). */
8748 (PARITY (bit_not @0))
8751 /* parity(bswap(x)) is parity(x). */
8752 (for parity (PARITY)
8753 (for bswap (BUILT_IN_BSWAP16 BUILT_IN_BSWAP32
8754 BUILT_IN_BSWAP64 BUILT_IN_BSWAP128)
8756 (parity (convert?@0 (bswap:s@1 @2)))
8757 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
8758 && INTEGRAL_TYPE_P (TREE_TYPE (@1))
8759 && TYPE_PRECISION (TREE_TYPE (@0))
8760 >= TYPE_PRECISION (TREE_TYPE (@1)))
8761 (with { tree type0 = TREE_TYPE (@0);
8762 tree type1 = TREE_TYPE (@1); }
8763 (parity (convert:type0 (convert:type1 @2))))))))
8765 /* parity(rotate(X Y)) is parity(X). */
8766 (for parity (PARITY)
8767 (for rot (lrotate rrotate)
8769 (parity (convert?@0 (rot:s@1 @2 @3)))
8770 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
8771 && INTEGRAL_TYPE_P (TREE_TYPE (@1))
8772 && (GIMPLE || !TREE_SIDE_EFFECTS (@3))
8773 && TYPE_PRECISION (TREE_TYPE (@0))
8774 >= TYPE_PRECISION (TREE_TYPE (@1)))
8775 (with { tree type0 = TREE_TYPE (@0); }
8776 (parity (convert:type0 @2)))))))
8778 /* parity(X)^parity(Y) is parity(X^Y). */
8780 (bit_xor (PARITY:s @0) (PARITY:s @1))
8781 (PARITY (bit_xor @0 @1)))
8783 /* a != 0 ? FUN(a) : 0 -> Fun(a) for some builtin functions. */
8784 (for func (POPCOUNT BSWAP FFS PARITY)
8786 (cond (ne @0 integer_zerop@1) (func@3 (convert? @0)) integer_zerop@2)
8789 /* a != 0 ? FUN(a) : CST -> Fun(a) for some CLRSB builtins
8790 where CST is precision-1. */
8793 (cond (ne @0 integer_zerop@1) (func@4 (convert?@3 @0)) INTEGER_CST@2)
8794 (if (wi::to_widest (@2) == TYPE_PRECISION (TREE_TYPE (@3)) - 1)
8798 /* a != 0 ? CLZ(a) : CST -> .CLZ(a) where CST is the result of the internal function for 0. */
8801 (cond (ne @0 integer_zerop@1) (func (convert?@3 @0)) INTEGER_CST@2)
8803 internal_fn ifn = IFN_LAST;
8804 if (TREE_CODE (TREE_TYPE (@3)) == BITINT_TYPE)
8806 if (tree_fits_shwi_p (@2))
8808 HOST_WIDE_INT valw = tree_to_shwi (@2);
8809 if ((int) valw == valw)
8816 else if (direct_internal_fn_supported_p (IFN_CLZ, TREE_TYPE (@3),
8818 && CLZ_DEFINED_VALUE_AT_ZERO
8819 (SCALAR_INT_TYPE_MODE (TREE_TYPE (@3)), val) == 2)
8822 (if (ifn == IFN_CLZ && wi::to_widest (@2) == val)
8825 (cond (ne @0 integer_zerop@1) (IFN_CLZ (convert?@3 @0) INTEGER_CST@2) @2)
8827 internal_fn ifn = IFN_LAST;
8828 if (TREE_CODE (TREE_TYPE (@3)) == BITINT_TYPE)
8830 else if (direct_internal_fn_supported_p (IFN_CLZ, TREE_TYPE (@3),
8834 (if (ifn == IFN_CLZ)
8837 /* a != 0 ? CTZ(a) : CST -> .CTZ(a) where CST is the result of the internal function for 0. */
8840 (cond (ne @0 integer_zerop@1) (func (convert?@3 @0)) INTEGER_CST@2)
8842 internal_fn ifn = IFN_LAST;
8843 if (TREE_CODE (TREE_TYPE (@3)) == BITINT_TYPE)
8845 if (tree_fits_shwi_p (@2))
8847 HOST_WIDE_INT valw = tree_to_shwi (@2);
8848 if ((int) valw == valw)
8855 else if (direct_internal_fn_supported_p (IFN_CTZ, TREE_TYPE (@3),
8857 && CTZ_DEFINED_VALUE_AT_ZERO
8858 (SCALAR_INT_TYPE_MODE (TREE_TYPE (@3)), val) == 2)
8861 (if (ifn == IFN_CTZ && wi::to_widest (@2) == val)
8864 (cond (ne @0 integer_zerop@1) (IFN_CTZ (convert?@3 @0) INTEGER_CST@2) @2)
8866 internal_fn ifn = IFN_LAST;
8867 if (TREE_CODE (TREE_TYPE (@3)) == BITINT_TYPE)
8869 else if (direct_internal_fn_supported_p (IFN_CTZ, TREE_TYPE (@3),
8873 (if (ifn == IFN_CTZ)
8877 /* Common POPCOUNT/PARITY simplifications. */
8878 /* popcount(X&C1) is (X>>C2)&1 when C1 == 1<<C2. Same for parity(X&C1). */
8879 (for pfun (POPCOUNT PARITY)
8882 (if (INTEGRAL_TYPE_P (type))
8883 (with { wide_int nz = tree_nonzero_bits (@0); }
8887 (if (wi::popcount (nz) == 1)
8888 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
8889 (convert (rshift:utype (convert:utype @0)
8890 { build_int_cst (integer_type_node,
8891 wi::ctz (nz)); })))))))))
8894 /* 64- and 32-bits branchless implementations of popcount are detected:
8896 int popcount64c (uint64_t x)
8898 x -= (x >> 1) & 0x5555555555555555ULL;
8899 x = (x & 0x3333333333333333ULL) + ((x >> 2) & 0x3333333333333333ULL);
8900 x = (x + (x >> 4)) & 0x0f0f0f0f0f0f0f0fULL;
8901 return (x * 0x0101010101010101ULL) >> 56;
8904 int popcount32c (uint32_t x)
8906 x -= (x >> 1) & 0x55555555;
8907 x = (x & 0x33333333) + ((x >> 2) & 0x33333333);
8908 x = (x + (x >> 4)) & 0x0f0f0f0f;
8909 return (x * 0x01010101) >> 24;
8916 (rshift @8 INTEGER_CST@5)
8918 (bit_and @6 INTEGER_CST@7)
8922 (bit_and (rshift @0 INTEGER_CST@4) INTEGER_CST@11))
8928 /* Check constants and optab. */
8929 (with { unsigned prec = TYPE_PRECISION (type);
8930 int shift = (64 - prec) & 63;
8931 unsigned HOST_WIDE_INT c1
8932 = HOST_WIDE_INT_UC (0x0101010101010101) >> shift;
8933 unsigned HOST_WIDE_INT c2
8934 = HOST_WIDE_INT_UC (0x0F0F0F0F0F0F0F0F) >> shift;
8935 unsigned HOST_WIDE_INT c3
8936 = HOST_WIDE_INT_UC (0x3333333333333333) >> shift;
8937 unsigned HOST_WIDE_INT c4
8938 = HOST_WIDE_INT_UC (0x5555555555555555) >> shift;
8943 && TYPE_UNSIGNED (type)
8944 && integer_onep (@4)
8945 && wi::to_widest (@10) == 2
8946 && wi::to_widest (@5) == 4
8947 && wi::to_widest (@1) == prec - 8
8948 && tree_to_uhwi (@2) == c1
8949 && tree_to_uhwi (@3) == c2
8950 && tree_to_uhwi (@9) == c3
8951 && tree_to_uhwi (@7) == c3
8952 && tree_to_uhwi (@11) == c4)
8953 (if (direct_internal_fn_supported_p (IFN_POPCOUNT, type,
8955 (convert (IFN_POPCOUNT:type @0))
8956 /* Try to do popcount in two halves. PREC must be at least
8957 five bits for this to work without extension before adding. */
8959 tree half_type = NULL_TREE;
8960 opt_machine_mode m = mode_for_size ((prec + 1) / 2, MODE_INT, 1);
8963 && m.require () != TYPE_MODE (type))
8965 half_prec = GET_MODE_PRECISION (as_a <scalar_int_mode> (m));
8966 half_type = build_nonstandard_integer_type (half_prec, 1);
8968 gcc_assert (half_prec > 2);
8970 (if (half_type != NULL_TREE
8971 && direct_internal_fn_supported_p (IFN_POPCOUNT, half_type,
8974 (IFN_POPCOUNT:half_type (convert @0))
8975 (IFN_POPCOUNT:half_type (convert (rshift @0
8976 { build_int_cst (integer_type_node, half_prec); } )))))))))))
8978 /* __builtin_ffs needs to deal on many targets with the possible zero
8979 argument. If we know the argument is always non-zero, __builtin_ctz + 1
8980 should lead to better code. */
8982 (FFS tree_expr_nonzero_p@0)
8983 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
8984 && direct_internal_fn_supported_p (IFN_CTZ, TREE_TYPE (@0),
8985 OPTIMIZE_FOR_SPEED))
8986 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
8987 (plus (CTZ:type (convert:utype @0)) { build_one_cst (type); }))))
8990 (for ffs (BUILT_IN_FFS BUILT_IN_FFSL BUILT_IN_FFSLL
8992 /* __builtin_ffs (X) == 0 -> X == 0.
8993 __builtin_ffs (X) == 6 -> (X & 63) == 32. */
8996 (cmp (ffs@2 @0) INTEGER_CST@1)
8997 (with { int prec = TYPE_PRECISION (TREE_TYPE (@0)); }
8999 (if (integer_zerop (@1))
9000 (cmp @0 { build_zero_cst (TREE_TYPE (@0)); }))
9001 (if (tree_int_cst_sgn (@1) < 0 || wi::to_widest (@1) > prec)
9002 { constant_boolean_node (cmp == NE_EXPR ? true : false, type); })
9003 (if (single_use (@2))
9004 (cmp (bit_and @0 { wide_int_to_tree (TREE_TYPE (@0),
9005 wi::mask (tree_to_uhwi (@1),
9007 { wide_int_to_tree (TREE_TYPE (@0),
9008 wi::shifted_mask (tree_to_uhwi (@1) - 1, 1,
9009 false, prec)); }))))))
9011 /* __builtin_ffs (X) > 6 -> X != 0 && (X & 63) == 0. */
9015 bit_op (bit_and bit_ior)
9017 (cmp (ffs@2 @0) INTEGER_CST@1)
9018 (with { int prec = TYPE_PRECISION (TREE_TYPE (@0)); }
9020 (if (integer_zerop (@1))
9021 (cmp2 @0 { build_zero_cst (TREE_TYPE (@0)); }))
9022 (if (tree_int_cst_sgn (@1) < 0)
9023 { constant_boolean_node (cmp == GT_EXPR ? true : false, type); })
9024 (if (wi::to_widest (@1) >= prec)
9025 { constant_boolean_node (cmp == GT_EXPR ? false : true, type); })
9026 (if (wi::to_widest (@1) == prec - 1)
9027 (cmp3 @0 { wide_int_to_tree (TREE_TYPE (@0),
9028 wi::shifted_mask (prec - 1, 1,
9030 (if (single_use (@2))
9031 (bit_op (cmp2 @0 { build_zero_cst (TREE_TYPE (@0)); })
9033 { wide_int_to_tree (TREE_TYPE (@0),
9034 wi::mask (tree_to_uhwi (@1),
9036 { build_zero_cst (TREE_TYPE (@0)); }))))))))
9043 --> r = .COND_FN (cond, a, b)
9047 --> r = .COND_FN (~cond, b, a). */
9049 (for uncond_op (UNCOND_UNARY)
9050 cond_op (COND_UNARY)
9052 (vec_cond @0 (view_convert? (uncond_op@3 @1)) @2)
9053 (with { tree op_type = TREE_TYPE (@3); }
9054 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
9055 && is_truth_type_for (op_type, TREE_TYPE (@0)))
9056 (cond_op @0 (view_convert @1) @2))))
9058 (vec_cond @0 @1 (view_convert? (uncond_op@3 @2)))
9059 (with { tree op_type = TREE_TYPE (@3); }
9060 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
9061 && is_truth_type_for (op_type, TREE_TYPE (@0)))
9062 (cond_op (bit_not @0) (view_convert @2) @1)))))
9064 (for uncond_op (UNCOND_UNARY)
9065 cond_op (COND_LEN_UNARY)
9067 (IFN_VCOND_MASK_LEN @0 (view_convert? (uncond_op@3 @1)) @2 @4 @5)
9068 (with { tree op_type = TREE_TYPE (@3); }
9069 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
9070 && is_truth_type_for (op_type, TREE_TYPE (@0)))
9071 (cond_op @0 (view_convert @1) @2 @4 @5))))
9073 (IFN_VCOND_MASK_LEN @0 @1 (view_convert? (uncond_op@3 @2)) @4 @5)
9074 (with { tree op_type = TREE_TYPE (@3); }
9075 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
9076 && is_truth_type_for (op_type, TREE_TYPE (@0)))
9077 (cond_op (bit_not @0) (view_convert @2) @1 @4 @5)))))
9079 /* `(a ? -1 : 0) ^ b` can be converted into a conditional not. */
9081 (bit_xor:c (vec_cond @0 uniform_integer_cst_p@1 uniform_integer_cst_p@2) @3)
9082 (if (canonicalize_math_after_vectorization_p ()
9083 && vectorized_internal_fn_supported_p (IFN_COND_NOT, type)
9084 && is_truth_type_for (type, TREE_TYPE (@0)))
9085 (if (integer_all_onesp (@1) && integer_zerop (@2))
9086 (IFN_COND_NOT @0 @3 @3))
9087 (if (integer_all_onesp (@2) && integer_zerop (@1))
9088 (IFN_COND_NOT (bit_not @0) @3 @3))))
9097 r = c ? a1 op a2 : b;
9099 if the target can do it in one go. This makes the operation conditional
9100 on c, so could drop potentially-trapping arithmetic, but that's a valid
9101 simplification if the result of the operation isn't needed.
9103 Avoid speculatively generating a stand-alone vector comparison
9104 on targets that might not support them. Any target implementing
9105 conditional internal functions must support the same comparisons
9106 inside and outside a VEC_COND_EXPR. */
9108 (for uncond_op (UNCOND_BINARY)
9109 cond_op (COND_BINARY)
9111 (vec_cond @0 (view_convert? (uncond_op@4 @1 @2)) @3)
9112 (with { tree op_type = TREE_TYPE (@4); }
9113 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
9114 && is_truth_type_for (op_type, TREE_TYPE (@0))
9116 (view_convert (cond_op @0 @1 @2 (view_convert:op_type @3))))))
9118 (vec_cond @0 @1 (view_convert? (uncond_op@4 @2 @3)))
9119 (with { tree op_type = TREE_TYPE (@4); }
9120 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
9121 && is_truth_type_for (op_type, TREE_TYPE (@0))
9123 (view_convert (cond_op (bit_not @0) @2 @3 (view_convert:op_type @1)))))))
9125 (for uncond_op (UNCOND_BINARY)
9126 cond_op (COND_LEN_BINARY)
9128 (IFN_VCOND_MASK_LEN @0 (view_convert? (uncond_op@4 @1 @2)) @3 @5 @6)
9129 (with { tree op_type = TREE_TYPE (@4); }
9130 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
9131 && is_truth_type_for (op_type, TREE_TYPE (@0))
9133 (view_convert (cond_op @0 @1 @2 (view_convert:op_type @3) @5 @6)))))
9135 (IFN_VCOND_MASK_LEN @0 @1 (view_convert? (uncond_op@4 @2 @3)) @5 @6)
9136 (with { tree op_type = TREE_TYPE (@4); }
9137 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
9138 && is_truth_type_for (op_type, TREE_TYPE (@0))
9140 (view_convert (cond_op (bit_not @0) @2 @3 (view_convert:op_type @1) @5 @6))))))
9142 /* Same for ternary operations. */
9143 (for uncond_op (UNCOND_TERNARY)
9144 cond_op (COND_TERNARY)
9146 (vec_cond @0 (view_convert? (uncond_op@5 @1 @2 @3)) @4)
9147 (with { tree op_type = TREE_TYPE (@5); }
9148 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
9149 && is_truth_type_for (op_type, TREE_TYPE (@0))
9151 (view_convert (cond_op @0 @1 @2 @3 (view_convert:op_type @4))))))
9153 (vec_cond @0 @1 (view_convert? (uncond_op@5 @2 @3 @4)))
9154 (with { tree op_type = TREE_TYPE (@5); }
9155 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
9156 && is_truth_type_for (op_type, TREE_TYPE (@0))
9158 (view_convert (cond_op (bit_not @0) @2 @3 @4
9159 (view_convert:op_type @1)))))))
9161 (for uncond_op (UNCOND_TERNARY)
9162 cond_op (COND_LEN_TERNARY)
9164 (IFN_VCOND_MASK_LEN @0 (view_convert? (uncond_op@5 @1 @2 @3)) @4 @6 @7)
9165 (with { tree op_type = TREE_TYPE (@5); }
9166 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
9167 && is_truth_type_for (op_type, TREE_TYPE (@0))
9169 (view_convert (cond_op @0 @1 @2 @3 (view_convert:op_type @4) @6 @7)))))
9171 (IFN_VCOND_MASK_LEN @0 @1 (view_convert? (uncond_op@5 @2 @3 @4 @6 @7)))
9172 (with { tree op_type = TREE_TYPE (@5); }
9173 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
9174 && is_truth_type_for (op_type, TREE_TYPE (@0))
9176 (view_convert (cond_op (bit_not @0) @2 @3 @4 (view_convert:op_type @1) @6 @7))))))
9179 /* Detect cases in which a VEC_COND_EXPR effectively replaces the
9180 "else" value of an IFN_COND_*. */
9181 (for cond_op (COND_BINARY)
9183 (vec_cond @0 (view_convert? (cond_op @0 @1 @2 @3)) @4)
9184 (with { tree op_type = TREE_TYPE (@3); }
9185 (if (element_precision (type) == element_precision (op_type))
9186 (view_convert (cond_op @0 @1 @2 (view_convert:op_type @4))))))
9188 (vec_cond @0 @1 (view_convert? (cond_op @2 @3 @4 @5)))
9189 (with { tree op_type = TREE_TYPE (@5); }
9190 (if (inverse_conditions_p (@0, @2)
9191 && element_precision (type) == element_precision (op_type))
9192 (view_convert (cond_op @2 @3 @4 (view_convert:op_type @1)))))))
9194 /* Same for ternary operations. */
9195 (for cond_op (COND_TERNARY)
9197 (vec_cond @0 (view_convert? (cond_op @0 @1 @2 @3 @4)) @5)
9198 (with { tree op_type = TREE_TYPE (@4); }
9199 (if (element_precision (type) == element_precision (op_type))
9200 (view_convert (cond_op @0 @1 @2 @3 (view_convert:op_type @5))))))
9202 (vec_cond @0 @1 (view_convert? (cond_op @2 @3 @4 @5 @6)))
9203 (with { tree op_type = TREE_TYPE (@6); }
9204 (if (inverse_conditions_p (@0, @2)
9205 && element_precision (type) == element_precision (op_type))
9206 (view_convert (cond_op @2 @3 @4 @5 (view_convert:op_type @1)))))))
9208 /* Detect cases in which a VEC_COND_EXPR effectively replaces the
9209 "else" value of an IFN_COND_LEN_*. */
9210 (for cond_len_op (COND_LEN_BINARY)
9212 (vec_cond @0 (view_convert? (cond_len_op @0 @1 @2 @3 @4 @5)) @6)
9213 (with { tree op_type = TREE_TYPE (@3); }
9214 (if (element_precision (type) == element_precision (op_type))
9215 (view_convert (cond_len_op @0 @1 @2 (view_convert:op_type @6) @4 @5)))))
9217 (vec_cond @0 @1 (view_convert? (cond_len_op @2 @3 @4 @5 @6 @7)))
9218 (with { tree op_type = TREE_TYPE (@5); }
9219 (if (inverse_conditions_p (@0, @2)
9220 && element_precision (type) == element_precision (op_type))
9221 (view_convert (cond_len_op @2 @3 @4 (view_convert:op_type @1) @6 @7))))))
9223 /* Same for ternary operations. */
9224 (for cond_len_op (COND_LEN_TERNARY)
9226 (vec_cond @0 (view_convert? (cond_len_op @0 @1 @2 @3 @4 @5 @6)) @7)
9227 (with { tree op_type = TREE_TYPE (@4); }
9228 (if (element_precision (type) == element_precision (op_type))
9229 (view_convert (cond_len_op @0 @1 @2 @3 (view_convert:op_type @7) @5 @6)))))
9231 (vec_cond @0 @1 (view_convert? (cond_len_op @2 @3 @4 @5 @6 @7 @8)))
9232 (with { tree op_type = TREE_TYPE (@6); }
9233 (if (inverse_conditions_p (@0, @2)
9234 && element_precision (type) == element_precision (op_type))
9235 (view_convert (cond_len_op @2 @3 @4 @5 (view_convert:op_type @1) @7 @8))))))
9237 /* Detect simplication for a conditional reduction where
9240 c = mask2 ? d + a : d
9244 c = mask1 && mask2 ? d + b : d. */
9246 (IFN_COND_ADD @0 @1 (vec_cond @2 @3 zerop@4) @1)
9247 (if (ANY_INTEGRAL_TYPE_P (type)
9248 || (FLOAT_TYPE_P (type)
9249 && fold_real_zero_addition_p (type, NULL_TREE, @4, 0)))
9250 (IFN_COND_ADD (bit_and @0 @2) @1 @3 @1)))
9252 /* Detect simplication for a conditional length reduction where
9255 c = i < len + bias ? d + a : d
9259 c = mask && i < len + bias ? d + b : d. */
9261 (IFN_COND_LEN_ADD integer_truep @0 (vec_cond @1 @2 zerop@5) @0 @3 @4)
9262 (if (ANY_INTEGRAL_TYPE_P (type)
9263 || (FLOAT_TYPE_P (type)
9264 && fold_real_zero_addition_p (type, NULL_TREE, @5, 0)))
9265 (IFN_COND_LEN_ADD @1 @0 @2 @0 @3 @4)))
9267 /* Detect simplification for vector condition folding where
9269 c = mask1 ? (masked_op mask2 a b) : b
9273 c = masked_op (mask1 & mask2) a b
9275 where the operation can be partially applied to one operand. */
9277 (for cond_op (COND_BINARY)
9280 (cond_op:s @1 @2 @3 @4) @3)
9281 (cond_op (bit_and @1 @0) @2 @3 @4)))
9283 /* And same for ternary expressions. */
9285 (for cond_op (COND_TERNARY)
9288 (cond_op:s @1 @2 @3 @4 @5) @4)
9289 (cond_op (bit_and @1 @0) @2 @3 @4 @5)))
9291 /* For pointers @0 and @2 and nonnegative constant offset @1, look for
9294 A: (@0 + @1 < @2) | (@2 + @1 < @0)
9295 B: (@0 + @1 <= @2) | (@2 + @1 <= @0)
9297 If pointers are known not to wrap, B checks whether @1 bytes starting
9298 at @0 and @2 do not overlap, while A tests the same thing for @1 + 1
9299 bytes. A is more efficiently tested as:
9301 A: (sizetype) (@0 + @1 - @2) > @1 * 2
9303 The equivalent expression for B is given by replacing @1 with @1 - 1:
9305 B: (sizetype) (@0 + (@1 - 1) - @2) > (@1 - 1) * 2
9307 @0 and @2 can be swapped in both expressions without changing the result.
9309 The folds rely on sizetype's being unsigned (which is always true)
9310 and on its being the same width as the pointer (which we have to check).
9312 The fold replaces two pointer_plus expressions, two comparisons and
9313 an IOR with a pointer_plus, a pointer_diff, and a comparison, so in
9314 the best case it's a saving of two operations. The A fold retains one
9315 of the original pointer_pluses, so is a win even if both pointer_pluses
9316 are used elsewhere. The B fold is a wash if both pointer_pluses are
9317 used elsewhere, since all we end up doing is replacing a comparison with
9318 a pointer_plus. We do still apply the fold under those circumstances
9319 though, in case applying it to other conditions eventually makes one of the
9320 pointer_pluses dead. */
9321 (for ior (truth_orif truth_or bit_ior)
9324 (ior (cmp:cs (pointer_plus@3 @0 INTEGER_CST@1) @2)
9325 (cmp:cs (pointer_plus@4 @2 @1) @0))
9326 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
9327 && TYPE_OVERFLOW_WRAPS (sizetype)
9328 && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (sizetype))
9329 /* Calculate the rhs constant. */
9330 (with { offset_int off = wi::to_offset (@1) - (cmp == LE_EXPR ? 1 : 0);
9331 offset_int rhs = off * 2; }
9332 /* Always fails for negative values. */
9333 (if (wi::min_precision (rhs, UNSIGNED) <= TYPE_PRECISION (sizetype))
9334 /* Since the order of @0 and @2 doesn't matter, let tree_swap_operands_p
9335 pick a canonical order. This increases the chances of using the
9336 same pointer_plus in multiple checks. */
9337 (with { bool swap_p = tree_swap_operands_p (@0, @2);
9338 tree rhs_tree = wide_int_to_tree (sizetype, rhs); }
9339 (if (cmp == LT_EXPR)
9340 (gt (convert:sizetype
9341 (pointer_diff:ssizetype { swap_p ? @4 : @3; }
9342 { swap_p ? @0 : @2; }))
9344 (gt (convert:sizetype
9345 (pointer_diff:ssizetype
9346 (pointer_plus { swap_p ? @2 : @0; }
9347 { wide_int_to_tree (sizetype, off); })
9348 { swap_p ? @0 : @2; }))
9349 { rhs_tree; })))))))))
9351 /* Fold REDUC (@0 & @1) -> @0[I] & @1[I] if element I is the only nonzero
9353 (for reduc (IFN_REDUC_PLUS IFN_REDUC_IOR IFN_REDUC_XOR)
9354 (simplify (reduc (view_convert? (bit_and @0 VECTOR_CST@1)))
9355 (with { int i = single_nonzero_element (@1); }
9357 (with { tree elt = vector_cst_elt (@1, i);
9358 tree elt_type = TREE_TYPE (elt);
9359 unsigned int elt_bits = tree_to_uhwi (TYPE_SIZE (elt_type));
9360 tree size = bitsize_int (elt_bits);
9361 tree pos = bitsize_int (elt_bits * i); }
9364 (BIT_FIELD_REF:elt_type @0 { size; } { pos; })
9367 /* Fold reduction of a single nonzero element constructor. */
9368 (for reduc (IFN_REDUC_PLUS IFN_REDUC_IOR IFN_REDUC_XOR)
9369 (simplify (reduc (CONSTRUCTOR@0))
9370 (with { tree ctor = (TREE_CODE (@0) == SSA_NAME
9371 ? gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0)) : @0);
9372 tree elt = ctor_single_nonzero_element (ctor); }
9374 && !HONOR_SNANS (type)
9375 && !HONOR_SIGNED_ZEROS (type))
9378 /* Fold REDUC (@0 op VECTOR_CST) as REDUC (@0) op REDUC (VECTOR_CST). */
9379 (for reduc (IFN_REDUC_PLUS IFN_REDUC_MAX IFN_REDUC_MIN IFN_REDUC_FMAX
9380 IFN_REDUC_FMIN IFN_REDUC_AND IFN_REDUC_IOR IFN_REDUC_XOR)
9381 op (plus max min IFN_FMAX IFN_FMIN bit_and bit_ior bit_xor)
9382 (simplify (reduc (op @0 VECTOR_CST@1))
9383 (op (reduc:type @0) (reduc:type @1))))
9385 /* Simplify vector floating point operations of alternating sub/add pairs
9386 into using an fneg of a wider element type followed by a normal add.
9387 under IEEE 754 the fneg of the wider type will negate every even entry
9388 and when doing an add we get a sub of the even and add of every odd
9390 (for plusminus (plus minus)
9391 minusplus (minus plus)
9393 (vec_perm (plusminus @0 @1) (minusplus @2 @3) VECTOR_CST@4)
9394 (if (!VECTOR_INTEGER_TYPE_P (type)
9395 && !FLOAT_WORDS_BIG_ENDIAN
9396 /* plus is commutative, while minus is not, so :c can't be used.
9397 Do equality comparisons by hand and at the end pick the operands
9399 && (operand_equal_p (@0, @2, 0)
9400 ? operand_equal_p (@1, @3, 0)
9401 : operand_equal_p (@0, @3, 0) && operand_equal_p (@1, @2, 0)))
9404 /* Build a vector of integers from the tree mask. */
9405 vec_perm_builder builder;
9407 (if (tree_to_vec_perm_builder (&builder, @4))
9410 /* Create a vec_perm_indices for the integer vector. */
9411 poly_uint64 nelts = TYPE_VECTOR_SUBPARTS (type);
9412 vec_perm_indices sel (builder, 2, nelts);
9413 machine_mode vec_mode = TYPE_MODE (type);
9414 machine_mode wide_mode;
9415 scalar_mode wide_elt_mode;
9416 poly_uint64 wide_nunits;
9417 scalar_mode inner_mode = GET_MODE_INNER (vec_mode);
9419 (if (VECTOR_MODE_P (vec_mode)
9420 && sel.series_p (0, 2, 0, 2)
9421 && sel.series_p (1, 2, nelts + 1, 2)
9422 && GET_MODE_2XWIDER_MODE (inner_mode).exists (&wide_elt_mode)
9423 && multiple_p (GET_MODE_NUNITS (vec_mode), 2, &wide_nunits)
9424 && related_vector_mode (vec_mode, wide_elt_mode,
9425 wide_nunits).exists (&wide_mode))
9429 = lang_hooks.types.type_for_mode (GET_MODE_INNER (wide_mode),
9430 TYPE_UNSIGNED (type));
9431 tree ntype = build_vector_type_for_mode (stype, wide_mode);
9433 /* The format has to be a non-extended ieee format. */
9434 const struct real_format *fmt_old = FLOAT_MODE_FORMAT (vec_mode);
9435 const struct real_format *fmt_new = FLOAT_MODE_FORMAT (wide_mode);
9437 (if (TYPE_MODE (stype) != BLKmode
9438 && VECTOR_TYPE_P (ntype)
9443 /* If the target doesn't support v1xx vectors, try using
9444 scalar mode xx instead. */
9445 if (known_eq (GET_MODE_NUNITS (wide_mode), 1)
9446 && !target_supports_op_p (ntype, NEGATE_EXPR, optab_vector))
9449 (if (fmt_new->signbit_rw
9450 == fmt_old->signbit_rw + GET_MODE_UNIT_BITSIZE (vec_mode)
9451 && fmt_new->signbit_rw == fmt_new->signbit_ro
9452 && targetm.can_change_mode_class (TYPE_MODE (ntype),
9453 TYPE_MODE (type), ALL_REGS)
9454 && ((optimize_vectors_before_lowering_p ()
9455 && VECTOR_TYPE_P (ntype))
9456 || target_supports_op_p (ntype, NEGATE_EXPR, optab_vector)))
9457 (if (plusminus == PLUS_EXPR)
9458 (plus (view_convert:type (negate (view_convert:ntype @3))) @2)
9459 (minus @0 (view_convert:type
9460 (negate (view_convert:ntype @1))))))))))))))))
9463 (vec_perm @0 @1 VECTOR_CST@2)
9466 tree op0 = @0, op1 = @1, op2 = @2;
9467 machine_mode result_mode = TYPE_MODE (type);
9468 machine_mode op_mode = TYPE_MODE (TREE_TYPE (op0));
9470 /* Build a vector of integers from the tree mask. */
9471 vec_perm_builder builder;
9473 (if (tree_to_vec_perm_builder (&builder, op2))
9476 /* Create a vec_perm_indices for the integer vector. */
9477 poly_uint64 nelts = TYPE_VECTOR_SUBPARTS (type);
9478 bool single_arg = (op0 == op1);
9479 vec_perm_indices sel (builder, single_arg ? 1 : 2, nelts);
9481 (if (sel.series_p (0, 1, 0, 1))
9483 (if (sel.series_p (0, 1, nelts, 1))
9489 if (sel.all_from_input_p (0))
9491 else if (sel.all_from_input_p (1))
9494 sel.rotate_inputs (1);
9496 else if (known_ge (poly_uint64 (sel[0]), nelts))
9498 std::swap (op0, op1);
9499 sel.rotate_inputs (1);
9503 tree cop0 = op0, cop1 = op1;
9504 if (TREE_CODE (op0) == SSA_NAME
9505 && (def = dyn_cast <gassign *> (SSA_NAME_DEF_STMT (op0)))
9506 && gimple_assign_rhs_code (def) == CONSTRUCTOR)
9507 cop0 = gimple_assign_rhs1 (def);
9508 if (TREE_CODE (op1) == SSA_NAME
9509 && (def = dyn_cast <gassign *> (SSA_NAME_DEF_STMT (op1)))
9510 && gimple_assign_rhs_code (def) == CONSTRUCTOR)
9511 cop1 = gimple_assign_rhs1 (def);
9514 (if ((TREE_CODE (cop0) == VECTOR_CST
9515 || TREE_CODE (cop0) == CONSTRUCTOR)
9516 && (TREE_CODE (cop1) == VECTOR_CST
9517 || TREE_CODE (cop1) == CONSTRUCTOR)
9518 && (t = fold_vec_perm (type, cop0, cop1, sel)))
9522 bool changed = (op0 == op1 && !single_arg);
9523 tree ins = NULL_TREE;
9526 /* See if the permutation is performing a single element
9527 insert from a CONSTRUCTOR or constant and use a BIT_INSERT_EXPR
9528 in that case. But only if the vector mode is supported,
9529 otherwise this is invalid GIMPLE. */
9530 if (op_mode != BLKmode
9531 && (TREE_CODE (cop0) == VECTOR_CST
9532 || TREE_CODE (cop0) == CONSTRUCTOR
9533 || TREE_CODE (cop1) == VECTOR_CST
9534 || TREE_CODE (cop1) == CONSTRUCTOR))
9536 bool insert_first_p = sel.series_p (1, 1, nelts + 1, 1);
9539 /* After canonicalizing the first elt to come from the
9540 first vector we only can insert the first elt from
9541 the first vector. */
9543 if ((ins = fold_read_from_vector (cop0, sel[0])))
9546 /* The above can fail for two-element vectors which always
9547 appear to insert the first element, so try inserting
9548 into the second lane as well. For more than two
9549 elements that's wasted time. */
9550 if (!insert_first_p || (!ins && maybe_eq (nelts, 2u)))
9552 unsigned int encoded_nelts = sel.encoding ().encoded_nelts ();
9553 for (at = 0; at < encoded_nelts; ++at)
9554 if (maybe_ne (sel[at], at))
9556 if (at < encoded_nelts
9557 && (known_eq (at + 1, nelts)
9558 || sel.series_p (at + 1, 1, at + 1, 1)))
9560 if (known_lt (poly_uint64 (sel[at]), nelts))
9561 ins = fold_read_from_vector (cop0, sel[at]);
9563 ins = fold_read_from_vector (cop1, sel[at] - nelts);
9568 /* Generate a canonical form of the selector. */
9569 if (!ins && sel.encoding () != builder)
9571 /* Some targets are deficient and fail to expand a single
9572 argument permutation while still allowing an equivalent
9573 2-argument version. */
9575 if (sel.ninputs () == 2
9576 || can_vec_perm_const_p (result_mode, op_mode, sel, false))
9577 op2 = vec_perm_indices_to_tree (TREE_TYPE (op2), sel);
9580 vec_perm_indices sel2 (builder, 2, nelts);
9581 if (can_vec_perm_const_p (result_mode, op_mode, sel2, false))
9582 op2 = vec_perm_indices_to_tree (TREE_TYPE (op2), sel2);
9584 /* Not directly supported with either encoding,
9585 so use the preferred form. */
9586 op2 = vec_perm_indices_to_tree (TREE_TYPE (op2), sel);
9588 if (!operand_equal_p (op2, oldop2, 0))
9593 (bit_insert { op0; } { ins; }
9594 { bitsize_int (at * vector_element_bits (type)); })
9596 (vec_perm { op0; } { op1; } { op2; }))))))))))))
9598 /* VEC_PERM_EXPR (v, v, mask) -> v where v contains same element. */
9600 (match vec_same_elem_p
9603 (match vec_same_elem_p
9605 (if (TREE_CODE (@0) == SSA_NAME
9606 && uniform_vector_p (gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0))))))
9608 (match vec_same_elem_p
9610 (if (uniform_vector_p (@0))))
9614 (vec_perm vec_same_elem_p@0 @0 @1)
9615 (if (types_match (type, TREE_TYPE (@0)))
9619 tree elem = uniform_vector_p (@0);
9622 { build_vector_from_val (type, elem); }))))
9624 /* Push VEC_PERM earlier if that may help FMA perception (PR101895). */
9626 (plus:c (vec_perm:s (mult:c@0 @1 vec_same_elem_p@2) @0 @3) @4)
9627 (if (TREE_CODE (@0) == SSA_NAME && num_imm_uses (@0) == 2)
9628 (plus (mult (vec_perm @1 @1 @3) @2) @4)))
9630 (minus (vec_perm:s (mult:c@0 @1 vec_same_elem_p@2) @0 @3) @4)
9631 (if (TREE_CODE (@0) == SSA_NAME && num_imm_uses (@0) == 2)
9632 (minus (mult (vec_perm @1 @1 @3) @2) @4)))
9636 c = VEC_PERM_EXPR <a, b, VCST0>;
9637 d = VEC_PERM_EXPR <c, c, VCST1>;
9639 d = VEC_PERM_EXPR <a, b, NEW_VCST>; */
9642 (vec_perm (vec_perm@0 @1 @2 VECTOR_CST@3) @0 VECTOR_CST@4)
9643 (if (TYPE_VECTOR_SUBPARTS (type).is_constant ())
9646 machine_mode result_mode = TYPE_MODE (type);
9647 machine_mode op_mode = TYPE_MODE (TREE_TYPE (@1));
9648 int nelts = TYPE_VECTOR_SUBPARTS (type).to_constant ();
9649 vec_perm_builder builder0;
9650 vec_perm_builder builder1;
9651 vec_perm_builder builder2 (nelts, nelts, 1);
9653 (if (tree_to_vec_perm_builder (&builder0, @3)
9654 && tree_to_vec_perm_builder (&builder1, @4))
9657 vec_perm_indices sel0 (builder0, 2, nelts);
9658 vec_perm_indices sel1 (builder1, 1, nelts);
9660 for (int i = 0; i < nelts; i++)
9661 builder2.quick_push (sel0[sel1[i].to_constant ()]);
9663 vec_perm_indices sel2 (builder2, 2, nelts);
9665 tree op0 = NULL_TREE;
9666 /* If the new VEC_PERM_EXPR can't be handled but both
9667 original VEC_PERM_EXPRs can, punt.
9668 If one or both of the original VEC_PERM_EXPRs can't be
9669 handled and the new one can't be either, don't increase
9670 number of VEC_PERM_EXPRs that can't be handled. */
9671 if (can_vec_perm_const_p (result_mode, op_mode, sel2, false)
9673 ? (!can_vec_perm_const_p (result_mode, op_mode, sel0, false)
9674 || !can_vec_perm_const_p (result_mode, op_mode, sel1, false))
9675 : !can_vec_perm_const_p (result_mode, op_mode, sel1, false)))
9676 op0 = vec_perm_indices_to_tree (TREE_TYPE (@4), sel2);
9679 (vec_perm @1 @2 { op0; })))))))
9682 c = VEC_PERM_EXPR <a, b, VCST0>;
9683 d = VEC_PERM_EXPR <x, c, VCST1>;
9685 d = VEC_PERM_EXPR <x, {a,b}, NEW_VCST>;
9686 when all elements from a or b are replaced by the later
9690 (vec_perm @5 (vec_perm@0 @1 @2 VECTOR_CST@3) VECTOR_CST@4)
9691 (if (TYPE_VECTOR_SUBPARTS (type).is_constant ())
9694 machine_mode result_mode = TYPE_MODE (type);
9695 machine_mode op_mode = TYPE_MODE (TREE_TYPE (@1));
9696 int nelts = TYPE_VECTOR_SUBPARTS (type).to_constant ();
9697 vec_perm_builder builder0;
9698 vec_perm_builder builder1;
9699 vec_perm_builder builder2 (nelts, nelts, 2);
9701 (if (tree_to_vec_perm_builder (&builder0, @3)
9702 && tree_to_vec_perm_builder (&builder1, @4))
9705 vec_perm_indices sel0 (builder0, 2, nelts);
9706 vec_perm_indices sel1 (builder1, 2, nelts);
9707 bool use_1 = false, use_2 = false;
9709 for (int i = 0; i < nelts; i++)
9711 if (known_lt ((poly_uint64)sel1[i], sel1.nelts_per_input ()))
9712 builder2.quick_push (sel1[i]);
9715 poly_uint64 j = sel0[(sel1[i] - sel1.nelts_per_input ())
9717 if (known_lt (j, sel0.nelts_per_input ()))
9722 j -= sel0.nelts_per_input ();
9724 builder2.quick_push (j + sel1.nelts_per_input ());
9731 vec_perm_indices sel2 (builder2, 2, nelts);
9732 tree op0 = NULL_TREE;
9733 /* If the new VEC_PERM_EXPR can't be handled but both
9734 original VEC_PERM_EXPRs can, punt.
9735 If one or both of the original VEC_PERM_EXPRs can't be
9736 handled and the new one can't be either, don't increase
9737 number of VEC_PERM_EXPRs that can't be handled. */
9738 if (can_vec_perm_const_p (result_mode, op_mode, sel2, false)
9740 ? (!can_vec_perm_const_p (result_mode, op_mode, sel0, false)
9741 || !can_vec_perm_const_p (result_mode, op_mode, sel1, false))
9742 : !can_vec_perm_const_p (result_mode, op_mode, sel1, false)))
9743 op0 = vec_perm_indices_to_tree (TREE_TYPE (@4), sel2);
9748 (vec_perm @5 @1 { op0; }))
9750 (vec_perm @5 @2 { op0; })))))))))))
9752 /* And the case with swapped outer permute sources. */
9755 (vec_perm (vec_perm@0 @1 @2 VECTOR_CST@3) @5 VECTOR_CST@4)
9756 (if (TYPE_VECTOR_SUBPARTS (type).is_constant ())
9759 machine_mode result_mode = TYPE_MODE (type);
9760 machine_mode op_mode = TYPE_MODE (TREE_TYPE (@1));
9761 int nelts = TYPE_VECTOR_SUBPARTS (type).to_constant ();
9762 vec_perm_builder builder0;
9763 vec_perm_builder builder1;
9764 vec_perm_builder builder2 (nelts, nelts, 2);
9766 (if (tree_to_vec_perm_builder (&builder0, @3)
9767 && tree_to_vec_perm_builder (&builder1, @4))
9770 vec_perm_indices sel0 (builder0, 2, nelts);
9771 vec_perm_indices sel1 (builder1, 2, nelts);
9772 bool use_1 = false, use_2 = false;
9774 for (int i = 0; i < nelts; i++)
9776 if (known_ge ((poly_uint64)sel1[i], sel1.nelts_per_input ()))
9777 builder2.quick_push (sel1[i]);
9780 poly_uint64 j = sel0[sel1[i].to_constant ()];
9781 if (known_lt (j, sel0.nelts_per_input ()))
9786 j -= sel0.nelts_per_input ();
9788 builder2.quick_push (j);
9795 vec_perm_indices sel2 (builder2, 2, nelts);
9796 tree op0 = NULL_TREE;
9797 /* If the new VEC_PERM_EXPR can't be handled but both
9798 original VEC_PERM_EXPRs can, punt.
9799 If one or both of the original VEC_PERM_EXPRs can't be
9800 handled and the new one can't be either, don't increase
9801 number of VEC_PERM_EXPRs that can't be handled. */
9802 if (can_vec_perm_const_p (result_mode, op_mode, sel2, false)
9804 ? (!can_vec_perm_const_p (result_mode, op_mode, sel0, false)
9805 || !can_vec_perm_const_p (result_mode, op_mode, sel1, false))
9806 : !can_vec_perm_const_p (result_mode, op_mode, sel1, false)))
9807 op0 = vec_perm_indices_to_tree (TREE_TYPE (@4), sel2);
9812 (vec_perm @1 @5 { op0; }))
9814 (vec_perm @2 @5 { op0; })))))))))))
9817 /* Match count trailing zeroes for simplify_count_trailing_zeroes in fwprop.
9818 The canonical form is array[((x & -x) * C) >> SHIFT] where C is a magic
9819 constant which when multiplied by a power of 2 contains a unique value
9820 in the top 5 or 6 bits. This is then indexed into a table which maps it
9821 to the number of trailing zeroes. */
9822 (match (ctz_table_index @1 @2 @3)
9823 (rshift (mult (bit_and:c (negate @1) @1) INTEGER_CST@2) INTEGER_CST@3))
9825 (match (cond_expr_convert_p @0 @2 @3 @6)
9826 (cond (simple_comparison@6 @0 @1) (convert@4 @2) (convert@5 @3))
9827 (if (INTEGRAL_TYPE_P (type)
9828 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
9829 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
9830 && INTEGRAL_TYPE_P (TREE_TYPE (@3))
9831 && TYPE_PRECISION (type) != TYPE_PRECISION (TREE_TYPE (@0))
9832 && TYPE_PRECISION (TREE_TYPE (@0))
9833 == TYPE_PRECISION (TREE_TYPE (@2))
9834 && TYPE_PRECISION (TREE_TYPE (@0))
9835 == TYPE_PRECISION (TREE_TYPE (@3))
9836 /* For vect_recog_cond_expr_convert_pattern, @2 and @3 can differ in
9837 signess when convert is truncation, but not ok for extension since
9838 it's sign_extend vs zero_extend. */
9839 && (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type)
9840 || (TYPE_UNSIGNED (TREE_TYPE (@2))
9841 == TYPE_UNSIGNED (TREE_TYPE (@3))))
9843 && single_use (@5))))
9845 (for bit_op (bit_and bit_ior bit_xor)
9846 (match (bitwise_induction_p @0 @2 @3)
9848 (nop_convert1? (bit_not2?@0 (convert3? (lshift integer_onep@1 @2))))
9851 (match (bitwise_induction_p @0 @2 @3)
9853 (nop_convert1? (bit_xor@0 (convert2? (lshift integer_onep@1 @2)) @3))))
9855 /* n - (((n > C1) ? n : C1) & -C2) -> n & C1 for unsigned case.
9856 n - (((n > C1) ? n : C1) & -C2) -> (n <= C1) ? n : (n & C1) for signed case. */
9858 (minus @0 (bit_and (max @0 INTEGER_CST@1) INTEGER_CST@2))
9859 (with { auto i = wi::neg (wi::to_wide (@2)); }
9860 /* Check if -C2 is a power of 2 and C1 = -C2 - 1. */
9861 (if (wi::popcount (i) == 1
9862 && (wi::to_wide (@1)) == (i - 1))
9863 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
9865 (cond (le @0 @1) @0 (bit_and @0 @1))))))
9867 /* -x & 1 -> x & 1. */
9869 (bit_and (negate @0) integer_onep@1)
9870 (if (!TYPE_OVERFLOW_SANITIZED (type))
9873 /* `-a` is just `a` if the type is 1bit wide or when converting
9874 to a 1bit type; similar to the above transformation of `(-x)&1`.
9875 This is used mostly with the transformation of
9876 `a ? ~b : b` into `(-a)^b`.
9877 It also can show up with bitfields. */
9879 (convert? (negate @0))
9880 (if (INTEGRAL_TYPE_P (type)
9881 && TYPE_PRECISION (type) == 1
9882 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0)))
9886 c1 = VEC_PERM_EXPR (a, a, mask)
9887 c2 = VEC_PERM_EXPR (b, b, mask)
9891 c3 = VEC_PERM_EXPR (c, c, mask)
9892 For all integer non-div operations. */
9893 (for op (plus minus mult bit_and bit_ior bit_xor
9896 (op (vec_perm @0 @0 @2) (vec_perm @1 @1 @2))
9897 (if (VECTOR_INTEGER_TYPE_P (type))
9898 (vec_perm (op@3 @0 @1) @3 @2))))
9900 /* Similar for float arithmetic when permutation constant covers
9901 all vector elements. */
9902 (for op (plus minus mult)
9904 (op (vec_perm @0 @0 VECTOR_CST@2) (vec_perm @1 @1 VECTOR_CST@2))
9905 (if (VECTOR_FLOAT_TYPE_P (type)
9906 && TYPE_VECTOR_SUBPARTS (type).is_constant ())
9910 vec_perm_builder builder;
9911 bool full_perm_p = false;
9912 if (tree_to_vec_perm_builder (&builder, perm_cst))
9914 unsigned HOST_WIDE_INT nelts;
9916 nelts = TYPE_VECTOR_SUBPARTS (type).to_constant ();
9917 /* Create a vec_perm_indices for the VECTOR_CST. */
9918 vec_perm_indices sel (builder, 1, nelts);
9920 /* Check if perm indices covers all vector elements. */
9921 if (sel.encoding ().encoded_full_vector_p ())
9923 auto_sbitmap seen (nelts);
9924 bitmap_clear (seen);
9926 unsigned HOST_WIDE_INT count = 0, i;
9928 for (i = 0; i < nelts; i++)
9930 if (!bitmap_set_bit (seen, sel[i].to_constant ()))
9934 full_perm_p = count == nelts;
9939 (vec_perm (op@3 @0 @1) @3 @2))))))