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.
1037 Note the outer cast cannot be a boolean type as the only valid values
1038 are 0,-1/1 (depending on the signedness of the boolean) and the negative
1039 is there to get the correct value. */
1041 (convert (negate:s@1 (convert:s @0)))
1042 (if (INTEGRAL_TYPE_P (type)
1043 && tree_nop_conversion_p (type, TREE_TYPE (@1))
1044 && TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (@0))
1045 && TREE_CODE (type) != BOOLEAN_TYPE)
1046 (negate (convert @0))))
1048 (for op (negate abs)
1049 /* Simplify cos(-x) and cos(|x|) -> cos(x). Similarly for cosh. */
1050 (for coss (COS COSH)
1054 /* Simplify pow(-x, y) and pow(|x|,y) -> pow(x,y) if y is an even integer. */
1057 (pows (op @0) REAL_CST@1)
1058 (with { HOST_WIDE_INT n; }
1059 (if (real_isinteger (&TREE_REAL_CST (@1), &n) && (n & 1) == 0)
1061 /* Likewise for powi. */
1064 (pows (op @0) INTEGER_CST@1)
1065 (if ((wi::to_wide (@1) & 1) == 0)
1067 /* Strip negate and abs from both operands of hypot. */
1075 /* copysign(-x, y) and copysign(abs(x), y) -> copysign(x, y). */
1076 (for copysigns (COPYSIGN_ALL)
1078 (copysigns (op @0) @1)
1079 (copysigns @0 @1))))
1081 /* abs(x)*abs(x) -> x*x. Should be valid for all types. */
1083 (mult (abs@1 @0) @1)
1086 /* Convert absu(x)*absu(x) -> x*x. */
1088 (mult (absu@1 @0) @1)
1089 (mult (convert@2 @0) @2))
1091 /* cos(copysign(x, y)) -> cos(x). Similarly for cosh. */
1092 (for coss (COS COSH)
1093 (for copysigns (COPYSIGN)
1095 (coss (copysigns @0 @1))
1098 /* pow(copysign(x, y), z) -> pow(x, z) if z is an even integer. */
1100 (for copysigns (COPYSIGN)
1102 (pows (copysigns @0 @2) REAL_CST@1)
1103 (with { HOST_WIDE_INT n; }
1104 (if (real_isinteger (&TREE_REAL_CST (@1), &n) && (n & 1) == 0)
1106 /* Likewise for powi. */
1108 (for copysigns (COPYSIGN)
1110 (pows (copysigns @0 @2) INTEGER_CST@1)
1111 (if ((wi::to_wide (@1) & 1) == 0)
1115 (for copysigns (COPYSIGN)
1116 /* hypot(copysign(x, y), z) -> hypot(x, z). */
1118 (hypots (copysigns @0 @1) @2)
1120 /* hypot(x, copysign(y, z)) -> hypot(x, y). */
1122 (hypots @0 (copysigns @1 @2))
1125 /* copysign(x, CST) -> abs (x). */
1126 (for copysigns (COPYSIGN_ALL)
1128 (copysigns @0 REAL_CST@1)
1129 (if (!REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
1132 /* Transform fneg (fabs (X)) -> copysign (X, -1). */
1135 (IFN_COPYSIGN @0 { build_minus_one_cst (type); }))
1137 /* copysign(copysign(x, y), z) -> copysign(x, z). */
1138 (for copysigns (COPYSIGN_ALL)
1140 (copysigns (copysigns @0 @1) @2)
1143 /* copysign(x,y)*copysign(x,y) -> x*x. */
1144 (for copysigns (COPYSIGN_ALL)
1146 (mult (copysigns@2 @0 @1) @2)
1149 /* ccos(-x) -> ccos(x). Similarly for ccosh. */
1150 (for ccoss (CCOS CCOSH)
1155 /* cabs(-x) and cos(conj(x)) -> cabs(x). */
1156 (for ops (conj negate)
1162 /* Fold (a * (1 << b)) into (a << b) */
1164 (mult:c @0 (convert? (lshift integer_onep@1 @2)))
1165 (if (! FLOAT_TYPE_P (type)
1166 && tree_nop_conversion_p (type, TREE_TYPE (@1)))
1169 /* Shifts by precision or greater result in zero. */
1170 (for shift (lshift rshift)
1172 (shift @0 uniform_integer_cst_p@1)
1173 (if ((GIMPLE || !sanitize_flags_p (SANITIZE_SHIFT_EXPONENT))
1174 /* Leave arithmetic right shifts of possibly negative values alone. */
1175 && (TYPE_UNSIGNED (type)
1176 || shift == LSHIFT_EXPR
1177 || tree_expr_nonnegative_p (@0))
1178 /* Use a signed compare to leave negative shift counts alone. */
1179 && wi::ges_p (wi::to_wide (uniform_integer_cst_p (@1)),
1180 element_precision (type)))
1181 { build_zero_cst (type); })))
1183 /* Shifts by constants distribute over several binary operations,
1184 hence (X << C) + (Y << C) can be simplified to (X + Y) << C. */
1185 (for op (plus minus)
1187 (op (lshift:s @0 @1) (lshift:s @2 @1))
1188 (if (INTEGRAL_TYPE_P (type)
1189 && TYPE_OVERFLOW_WRAPS (type)
1190 && !TYPE_SATURATING (type))
1191 (lshift (op @0 @2) @1))))
1193 (for op (bit_and bit_ior bit_xor)
1195 (op (lshift:s @0 @1) (lshift:s @2 @1))
1196 (if (INTEGRAL_TYPE_P (type))
1197 (lshift (op @0 @2) @1)))
1199 (op (rshift:s @0 @1) (rshift:s @2 @1))
1200 (if (INTEGRAL_TYPE_P (type))
1201 (rshift (op @0 @2) @1))))
1203 /* Fold (1 << (C - x)) where C = precision(type) - 1
1204 into ((1 << C) >> x). */
1206 (lshift integer_onep@0 (minus@1 INTEGER_CST@2 @3))
1207 (if (INTEGRAL_TYPE_P (type)
1208 && wi::eq_p (wi::to_wide (@2), TYPE_PRECISION (type) - 1)
1210 (if (TYPE_UNSIGNED (type))
1211 (rshift (lshift @0 @2) @3)
1213 { tree utype = unsigned_type_for (type); }
1214 (convert (rshift (lshift (convert:utype @0) @2) @3))))))
1216 /* Fold ((type)(a<0)) << SIGNBITOFA into ((type)a) & signbit. */
1218 (lshift (convert (lt @0 integer_zerop@1)) INTEGER_CST@2)
1219 (if (TYPE_SIGN (TREE_TYPE (@0)) == SIGNED
1220 && wi::eq_p (wi::to_wide (@2), TYPE_PRECISION (TREE_TYPE (@0)) - 1))
1221 (with { wide_int wone = wi::one (TYPE_PRECISION (type)); }
1222 (bit_and (convert @0)
1223 { wide_int_to_tree (type,
1224 wi::lshift (wone, wi::to_wide (@2))); }))))
1226 /* Fold (-x >> C) into -(x > 0) where C = precision(type) - 1. */
1227 (for cst (INTEGER_CST VECTOR_CST)
1229 (rshift (negate:s @0) cst@1)
1230 (if (!TYPE_UNSIGNED (type)
1231 && TYPE_OVERFLOW_UNDEFINED (type))
1232 (with { tree stype = TREE_TYPE (@1);
1233 tree bt = truth_type_for (type);
1234 tree zeros = build_zero_cst (type);
1235 tree cst = NULL_TREE; }
1237 /* Handle scalar case. */
1238 (if (INTEGRAL_TYPE_P (type)
1239 /* If we apply the rule to the scalar type before vectorization
1240 we will enforce the result of the comparison being a bool
1241 which will require an extra AND on the result that will be
1242 indistinguishable from when the user did actually want 0
1243 or 1 as the result so it can't be removed. */
1244 && canonicalize_math_after_vectorization_p ()
1245 && wi::eq_p (wi::to_wide (@1), TYPE_PRECISION (type) - 1))
1246 (negate (convert (gt @0 { zeros; }))))
1247 /* Handle vector case. */
1248 (if (VECTOR_INTEGER_TYPE_P (type)
1249 /* First check whether the target has the same mode for vector
1250 comparison results as it's operands do. */
1251 && TYPE_MODE (bt) == TYPE_MODE (type)
1252 /* Then check to see if the target is able to expand the comparison
1253 with the given type later on, otherwise we may ICE. */
1254 && expand_vec_cmp_expr_p (type, bt, GT_EXPR)
1255 && (cst = uniform_integer_cst_p (@1)) != NULL
1256 && wi::eq_p (wi::to_wide (cst), element_precision (type) - 1))
1257 (view_convert (gt:bt @0 { zeros; }))))))))
1259 /* Fold (C1/X)*C2 into (C1*C2)/X. */
1261 (mult (rdiv@3 REAL_CST@0 @1) REAL_CST@2)
1262 (if (flag_associative_math
1265 { tree tem = const_binop (MULT_EXPR, type, @0, @2); }
1267 (rdiv { tem; } @1)))))
1269 /* Simplify ~X & X as zero. */
1271 (bit_and (convert? @0) (convert? @1))
1272 (with { bool wascmp; }
1273 (if (types_match (TREE_TYPE (@0), TREE_TYPE (@1))
1274 && bitwise_inverted_equal_p (@0, @1, wascmp))
1275 { wascmp ? constant_boolean_node (false, type) : build_zero_cst (type); })))
1277 /* PR71636: Transform x & ((1U << b) - 1) -> x & ~(~0U << b); */
1279 (bit_and:c @0 (plus:s (lshift:s integer_onep @1) integer_minus_onep))
1280 (if (TYPE_UNSIGNED (type))
1281 (bit_and @0 (bit_not (lshift { build_all_ones_cst (type); } @1)))))
1283 (for bitop (bit_and bit_ior)
1285 /* PR35691: Transform
1286 (x == 0 & y == 0) -> (x | typeof(x)(y)) == 0.
1287 (x != 0 | y != 0) -> (x | typeof(x)(y)) != 0. */
1289 (bitop (cmp @0 integer_zerop@2) (cmp @1 integer_zerop))
1290 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1291 && INTEGRAL_TYPE_P (TREE_TYPE (@1))
1292 && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
1293 (cmp (bit_ior @0 (convert @1)) @2)))
1295 (x == -1 & y == -1) -> (x & typeof(x)(y)) == -1.
1296 (x != -1 | y != -1) -> (x & typeof(x)(y)) != -1. */
1298 (bitop (cmp @0 integer_all_onesp@2) (cmp @1 integer_all_onesp))
1299 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1300 && INTEGRAL_TYPE_P (TREE_TYPE (@1))
1301 && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
1302 (cmp (bit_and @0 (convert @1)) @2))))
1304 /* Fold (A & ~B) - (A & B) into (A ^ B) - B. */
1306 (minus (bit_and:cs @0 (bit_not @1)) (bit_and:cs @0 @1))
1307 (minus (bit_xor @0 @1) @1))
1309 (minus (bit_and:s @0 INTEGER_CST@2) (bit_and:s @0 INTEGER_CST@1))
1310 (if (~wi::to_wide (@2) == wi::to_wide (@1))
1311 (minus (bit_xor @0 @1) @1)))
1313 /* Fold (A & B) - (A & ~B) into B - (A ^ B). */
1315 (minus (bit_and:cs @0 @1) (bit_and:cs @0 (bit_not @1)))
1316 (minus @1 (bit_xor @0 @1)))
1318 /* Simplify (X & ~Y) |^+ (~X & Y) -> X ^ Y. */
1319 (for op (bit_ior bit_xor plus)
1321 (op (bit_and:c @0 @2) (bit_and:c @3 @1))
1322 (with { bool wascmp0, wascmp1; }
1323 (if (bitwise_inverted_equal_p (@2, @1, wascmp0)
1324 && bitwise_inverted_equal_p (@0, @3, wascmp1)
1325 && ((!wascmp0 && !wascmp1)
1326 || element_precision (type) == 1))
1329 /* PR53979: Transform ((a ^ b) | a) -> (a | b) */
1331 (bit_ior:c (bit_xor:c @0 @1) @0)
1334 /* (a & ~b) | (a ^ b) --> a ^ b */
1336 (bit_ior:c (bit_and:c @0 (bit_not @1)) (bit_xor:c@2 @0 @1))
1339 /* (a & ~b) ^ ~a --> ~(a & b) */
1341 (bit_xor:c (bit_and:cs @0 (bit_not @1)) (bit_not @0))
1342 (bit_not (bit_and @0 @1)))
1344 /* (~a & b) ^ a --> (a | b) */
1346 (bit_xor:c (bit_and:cs (bit_not @0) @1) @0)
1349 /* (a | b) & ~(a ^ b) --> a & b */
1351 (bit_and:c (bit_ior @0 @1) (bit_not (bit_xor:c @0 @1)))
1354 /* (a | b) & (a == b) --> a & b (boolean version of the above). */
1356 (bit_and:c (bit_ior @0 @1) (nop_convert? (eq:c @0 @1)))
1357 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1358 && TYPE_PRECISION (TREE_TYPE (@0)) == 1)
1361 /* a | ~(a ^ b) --> a | ~b */
1363 (bit_ior:c @0 (bit_not:s (bit_xor:c @0 @1)))
1364 (bit_ior @0 (bit_not @1)))
1366 /* a | (a == b) --> a | (b^1) (boolean version of the above). */
1368 (bit_ior:c @0 (nop_convert? (eq:c @0 @1)))
1369 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1370 && TYPE_PRECISION (TREE_TYPE (@0)) == 1)
1371 (bit_ior @0 (bit_xor @1 { build_one_cst (type); }))))
1373 /* a | ((~a) ^ b) --> a | (~b) (alt version of the above 2) */
1375 (bit_ior:c @0 (bit_xor:cs @1 @2))
1376 (with { bool wascmp; }
1377 (if (bitwise_inverted_equal_p (@0, @1, wascmp)
1378 && (!wascmp || element_precision (type) == 1))
1379 (bit_ior @0 (bit_not @2)))))
1381 /* a & ~(a ^ b) --> a & b */
1383 (bit_and:c @0 (bit_not (bit_xor:c @0 @1)))
1386 /* a & (a == b) --> a & b (boolean version of the above). */
1388 (bit_and:c @0 (nop_convert? (eq:c @0 @1)))
1389 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1390 && TYPE_PRECISION (TREE_TYPE (@0)) == 1)
1393 /* a & ((~a) ^ b) --> a & b (alt version of the above 2) */
1395 (bit_and:c @0 (bit_xor:c @1 @2))
1396 (with { bool wascmp; }
1397 (if (bitwise_inverted_equal_p (@0, @1, wascmp)
1398 && (!wascmp || element_precision (type) == 1))
1401 /* (a | b) | (a &^ b) --> a | b */
1402 (for op (bit_and bit_xor)
1404 (bit_ior:c (bit_ior@2 @0 @1) (op:c @0 @1))
1407 /* (a & b) | ~(a ^ b) --> ~(a ^ b) */
1409 (bit_ior:c (bit_and:c @0 @1) (bit_not@2 (bit_xor @0 @1)))
1412 /* (a & b) | (a == b) --> a == b */
1414 (bit_ior:c (bit_and:c @0 @1) (nop_convert?@2 (eq @0 @1)))
1415 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1416 && TYPE_PRECISION (TREE_TYPE (@0)) == 1)
1419 /* ~(~a & b) --> a | ~b */
1421 (bit_not (bit_and:cs (bit_not @0) @1))
1422 (bit_ior @0 (bit_not @1)))
1424 /* ~(~a | b) --> a & ~b */
1426 (bit_not (bit_ior:cs (bit_not @0) @1))
1427 (bit_and @0 (bit_not @1)))
1429 /* (a ^ b) & ((b ^ c) ^ a) --> (a ^ b) & ~c */
1431 (bit_and:c (bit_xor:c@3 @0 @1) (bit_xor:cs (bit_xor:cs @1 @2) @0))
1432 (bit_and @3 (bit_not @2)))
1434 /* (a ^ b) | ((b ^ c) ^ a) --> (a ^ b) | c */
1436 (bit_ior:c (bit_xor:c@3 @0 @1) (bit_xor:c (bit_xor:c @1 @2) @0))
1439 /* (~X | C) ^ D -> (X | C) ^ (~D ^ C) if (~D ^ C) can be simplified. */
1441 (bit_xor:c (bit_ior:cs (bit_not:s @0) @1) @2)
1442 (bit_xor (bit_ior @0 @1) (bit_xor! (bit_not! @2) @1)))
1444 /* (~X & C) ^ D -> (X & C) ^ (D ^ C) if (D ^ C) can be simplified. */
1446 (bit_xor:c (bit_and:cs (bit_not:s @0) @1) @2)
1447 (bit_xor (bit_and @0 @1) (bit_xor! @2 @1)))
1449 /* Simplify (~X & Y) to X ^ Y if we know that (X & ~Y) is 0. */
1451 (bit_and (bit_not SSA_NAME@0) INTEGER_CST@1)
1452 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1453 && wi::bit_and_not (get_nonzero_bits (@0), wi::to_wide (@1)) == 0)
1456 /* For constants M and N, if M == (1LL << cst) - 1 && (N & M) == M,
1457 ((A & N) + B) & M -> (A + B) & M
1458 Similarly if (N & M) == 0,
1459 ((A | N) + B) & M -> (A + B) & M
1460 and for - instead of + (or unary - instead of +)
1461 and/or ^ instead of |.
1462 If B is constant and (B & M) == 0, fold into A & M. */
1463 (for op (plus minus)
1464 (for bitop (bit_and bit_ior bit_xor)
1466 (bit_and (op:s (bitop:s@0 @3 INTEGER_CST@4) @1) INTEGER_CST@2)
1469 tree utype = fold_bit_and_mask (TREE_TYPE (@0), @2, op, @0, bitop,
1470 @3, @4, @1, ERROR_MARK, NULL_TREE,
1473 (convert (bit_and (op (convert:utype { pmop[0]; })
1474 (convert:utype { pmop[1]; }))
1475 (convert:utype @2))))))
1477 (bit_and (op:s @0 (bitop:s@1 @3 INTEGER_CST@4)) INTEGER_CST@2)
1480 tree utype = fold_bit_and_mask (TREE_TYPE (@0), @2, op, @0, ERROR_MARK,
1481 NULL_TREE, NULL_TREE, @1, bitop, @3,
1484 (convert (bit_and (op (convert:utype { pmop[0]; })
1485 (convert:utype { pmop[1]; }))
1486 (convert:utype @2)))))))
1488 (bit_and (op:s @0 @1) INTEGER_CST@2)
1491 tree utype = fold_bit_and_mask (TREE_TYPE (@0), @2, op, @0, ERROR_MARK,
1492 NULL_TREE, NULL_TREE, @1, ERROR_MARK,
1493 NULL_TREE, NULL_TREE, pmop); }
1495 (convert (bit_and (op (convert:utype { pmop[0]; })
1496 (convert:utype { pmop[1]; }))
1497 (convert:utype @2)))))))
1498 (for bitop (bit_and bit_ior bit_xor)
1500 (bit_and (negate:s (bitop:s@0 @2 INTEGER_CST@3)) INTEGER_CST@1)
1503 tree utype = fold_bit_and_mask (TREE_TYPE (@0), @1, NEGATE_EXPR, @0,
1504 bitop, @2, @3, NULL_TREE, ERROR_MARK,
1505 NULL_TREE, NULL_TREE, pmop); }
1507 (convert (bit_and (negate (convert:utype { pmop[0]; }))
1508 (convert:utype @1)))))))
1510 /* X % Y is smaller than Y. */
1513 (cmp:c (trunc_mod @0 @1) @1)
1514 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
1515 { constant_boolean_node (cmp == LT_EXPR, type); })))
1519 (bit_ior @0 integer_all_onesp@1)
1524 (bit_ior @0 integer_zerop)
1529 (bit_and @0 integer_zerop@1)
1534 (for op (bit_ior bit_xor)
1536 (op (convert? @0) (convert? @1))
1537 (with { bool wascmp; }
1538 (if (types_match (TREE_TYPE (@0), TREE_TYPE (@1))
1539 && bitwise_inverted_equal_p (@0, @1, wascmp))
1542 ? constant_boolean_node (true, type)
1543 : build_all_ones_cst (TREE_TYPE (@0)); })))))
1548 { build_zero_cst (type); })
1550 /* Canonicalize X ^ ~0 to ~X. */
1552 (bit_xor @0 integer_all_onesp@1)
1557 (bit_and @0 integer_all_onesp)
1560 /* x & x -> x, x | x -> x */
1561 (for bitop (bit_and bit_ior)
1566 /* x & C -> x if we know that x & ~C == 0. */
1569 (bit_and SSA_NAME@0 INTEGER_CST@1)
1570 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1571 && wi::bit_and_not (get_nonzero_bits (@0), wi::to_wide (@1)) == 0)
1574 /* `a & (x | CST)` -> a if we know that (a & ~CST) == 0 */
1576 (bit_and:c SSA_NAME@0 (bit_ior @1 INTEGER_CST@2))
1577 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1578 && wi::bit_and_not (get_nonzero_bits (@0), wi::to_wide (@2)) == 0)
1581 /* x | C -> C if we know that x & ~C == 0. */
1583 (bit_ior SSA_NAME@0 INTEGER_CST@1)
1584 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1585 && wi::bit_and_not (get_nonzero_bits (@0), wi::to_wide (@1)) == 0)
1589 /* ~(~X - Y) -> X + Y and ~(~X + Y) -> X - Y. */
1591 (bit_not (minus (bit_not @0) @1))
1594 (bit_not (plus:c (bit_not @0) @1))
1596 /* (~X - ~Y) -> Y - X. */
1598 (minus (bit_not @0) (bit_not @1))
1599 (if (!TYPE_OVERFLOW_SANITIZED (type))
1600 (with { tree utype = unsigned_type_for (type); }
1601 (convert (minus (convert:utype @1) (convert:utype @0))))))
1603 /* ~(X - Y) -> ~X + Y. */
1605 (bit_not (minus:s @0 @1))
1606 (plus (bit_not @0) @1))
1608 (bit_not (plus:s @0 INTEGER_CST@1))
1609 (if ((INTEGRAL_TYPE_P (type)
1610 && TYPE_UNSIGNED (type))
1611 || (!TYPE_OVERFLOW_SANITIZED (type)
1612 && may_negate_without_overflow_p (@1)))
1613 (plus (bit_not @0) { const_unop (NEGATE_EXPR, type, @1); })))
1616 /* ~X + Y -> (Y - X) - 1. */
1618 (plus:c (bit_not @0) @1)
1619 (if (ANY_INTEGRAL_TYPE_P (type)
1620 && TYPE_OVERFLOW_WRAPS (type)
1621 /* -1 - X is folded to ~X, so we'd recurse endlessly. */
1622 && !integer_all_onesp (@1))
1623 (plus (minus @1 @0) { build_minus_one_cst (type); })
1624 (if (INTEGRAL_TYPE_P (type)
1625 && TREE_CODE (@1) == INTEGER_CST
1626 && wi::to_wide (@1) != wi::min_value (TYPE_PRECISION (type),
1628 (minus (plus @1 { build_minus_one_cst (type); }) @0))))
1631 /* ~(X >> Y) -> ~X >> Y if ~X can be simplified. */
1633 (bit_not (rshift:s @0 @1))
1634 (if (!TYPE_UNSIGNED (TREE_TYPE (@0)))
1635 (rshift (bit_not! @0) @1)
1636 /* For logical right shifts, this is possible only if @0 doesn't
1637 have MSB set and the logical right shift is changed into
1638 arithmetic shift. */
1639 (if (INTEGRAL_TYPE_P (type)
1640 && !wi::neg_p (tree_nonzero_bits (@0)))
1641 (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
1642 (convert (rshift (bit_not! (convert:stype @0)) @1))))))
1644 /* x + (x & 1) -> (x + 1) & ~1 */
1646 (plus:c @0 (bit_and:s @0 integer_onep@1))
1647 (bit_and (plus @0 @1) (bit_not @1)))
1649 /* x & ~(x & y) -> x & ~y */
1650 /* x | ~(x | y) -> x | ~y */
1651 (for bitop (bit_and bit_ior)
1653 (bitop:c @0 (bit_not (bitop:cs @0 @1)))
1654 (bitop @0 (bit_not @1))))
1656 /* (~x & y) | ~(x | y) -> ~x */
1658 (bit_ior:c (bit_and:c (bit_not@2 @0) @1) (bit_not (bit_ior:c @0 @1)))
1661 /* (x | y) ^ (x | ~y) -> ~x */
1663 (bit_xor:c (bit_ior:c @0 @1) (bit_ior:c @0 (bit_not @1)))
1666 /* (x & y) | ~(x | y) -> ~(x ^ y) */
1668 (bit_ior:c (bit_and:s @0 @1) (bit_not:s (bit_ior:s @0 @1)))
1669 (bit_not (bit_xor @0 @1)))
1671 /* (~x | y) ^ (x ^ y) -> x | ~y */
1673 (bit_xor:c (bit_ior:cs (bit_not @0) @1) (bit_xor:s @0 @1))
1674 (bit_ior @0 (bit_not @1)))
1676 /* (x ^ y) | ~(x | y) -> ~(x & y) */
1678 (bit_ior:c (bit_xor:s @0 @1) (bit_not:s (bit_ior:s @0 @1)))
1679 (bit_not (bit_and @0 @1)))
1681 /* (x & y) ^ (x | y) -> x ^ y */
1683 (bit_xor:c (bit_and @0 @1) (bit_ior @0 @1))
1686 /* (x ^ y) ^ (x | y) -> x & y */
1688 (bit_xor:c (bit_xor @0 @1) (bit_ior @0 @1))
1691 /* (x & y) + (x ^ y) -> x | y */
1692 /* (x & y) | (x ^ y) -> x | y */
1693 /* (x & y) ^ (x ^ y) -> x | y */
1694 (for op (plus bit_ior bit_xor)
1696 (op:c (bit_and @0 @1) (bit_xor @0 @1))
1699 /* (x & y) + (x | y) -> x + y */
1701 (plus:c (bit_and @0 @1) (bit_ior @0 @1))
1704 /* (x + y) - (x | y) -> x & y */
1706 (minus (plus @0 @1) (bit_ior @0 @1))
1707 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1708 && !TYPE_SATURATING (type))
1711 /* (x + y) - (x & y) -> x | y */
1713 (minus (plus @0 @1) (bit_and @0 @1))
1714 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1715 && !TYPE_SATURATING (type))
1718 /* (x | y) - y -> (x & ~y) */
1720 (minus (bit_ior:cs @0 @1) @1)
1721 (bit_and @0 (bit_not @1)))
1723 /* (x | y) - (x ^ y) -> x & y */
1725 (minus (bit_ior @0 @1) (bit_xor @0 @1))
1728 /* (x | y) - (x & y) -> x ^ y */
1730 (minus (bit_ior @0 @1) (bit_and @0 @1))
1733 /* (x | y) & ~(x & y) -> x ^ y */
1735 (bit_and:c (bit_ior @0 @1) (bit_not (bit_and @0 @1)))
1738 /* (x | y) & (~x ^ y) -> x & y */
1740 (bit_and:c (bit_ior:c @0 @1) (bit_xor:c @1 @2))
1741 (with { bool wascmp; }
1742 (if (bitwise_inverted_equal_p (@0, @2, wascmp)
1743 && (!wascmp || element_precision (type) == 1))
1746 /* (~x | y) & (x | ~y) -> ~(x ^ y) */
1748 (bit_and (bit_ior:cs (bit_not @0) @1) (bit_ior:cs @0 (bit_not @1)))
1749 (bit_not (bit_xor @0 @1)))
1751 /* (~x | y) ^ (x | ~y) -> x ^ y */
1753 (bit_xor (bit_ior:c (bit_not @0) @1) (bit_ior:c @0 (bit_not @1)))
1756 /* ((x & y) - (x | y)) - 1 -> ~(x ^ y) */
1758 (plus (nop_convert1? (minus@2 (nop_convert2? (bit_and:c @0 @1))
1759 (nop_convert2? (bit_ior @0 @1))))
1761 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1762 && !TYPE_SATURATING (type) && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2))
1763 && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@2))
1764 && !TYPE_SATURATING (TREE_TYPE (@2)))
1765 (bit_not (convert (bit_xor @0 @1)))))
1767 (minus (nop_convert1? (plus@2 (nop_convert2? (bit_and:c @0 @1))
1769 (nop_convert3? (bit_ior @0 @1)))
1770 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1771 && !TYPE_SATURATING (type) && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2))
1772 && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@2))
1773 && !TYPE_SATURATING (TREE_TYPE (@2)))
1774 (bit_not (convert (bit_xor @0 @1)))))
1776 (minus (nop_convert1? (bit_and @0 @1))
1777 (nop_convert2? (plus@2 (nop_convert3? (bit_ior:c @0 @1))
1779 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1780 && !TYPE_SATURATING (type) && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2))
1781 && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@2))
1782 && !TYPE_SATURATING (TREE_TYPE (@2)))
1783 (bit_not (convert (bit_xor @0 @1)))))
1785 /* ~x & ~y -> ~(x | y)
1786 ~x | ~y -> ~(x & y) */
1787 (for op (bit_and bit_ior)
1788 rop (bit_ior bit_and)
1790 (op (convert1? (bit_not @0)) (convert2? (bit_not @1)))
1791 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1792 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
1793 (bit_not (rop (convert @0) (convert @1))))))
1795 /* If we are XORing or adding two BIT_AND_EXPR's, both of which are and'ing
1796 with a constant, and the two constants have no bits in common,
1797 we should treat this as a BIT_IOR_EXPR since this may produce more
1799 (for op (bit_xor plus)
1801 (op (convert1? (bit_and@4 @0 INTEGER_CST@1))
1802 (convert2? (bit_and@5 @2 INTEGER_CST@3)))
1803 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
1804 && tree_nop_conversion_p (type, TREE_TYPE (@2))
1805 && (wi::to_wide (@1) & wi::to_wide (@3)) == 0)
1806 (bit_ior (convert @4) (convert @5)))))
1808 /* (X | Y) ^ X -> Y & ~ X*/
1810 (bit_xor:c (convert1? (bit_ior:c @@0 @1)) (convert2? @0))
1811 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1812 (convert (bit_and @1 (bit_not @0)))))
1814 /* (~X | Y) ^ X -> ~(X & Y). */
1816 (bit_xor:c (nop_convert1? (bit_ior:c (nop_convert2? (bit_not @0)) @1)) @2)
1817 (if (bitwise_equal_p (@0, @2))
1818 (convert (bit_not (bit_and @0 (convert @1))))))
1820 /* Convert ~X ^ ~Y to X ^ Y. */
1822 (bit_xor (convert1? (bit_not @0)) (convert2? (bit_not @1)))
1823 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1824 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
1825 (bit_xor (convert @0) (convert @1))))
1827 /* Convert ~X ^ C to X ^ ~C. */
1829 (bit_xor (convert? (bit_not @0)) INTEGER_CST@1)
1830 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1831 (bit_xor (convert @0) (bit_not @1))))
1833 /* Fold (X & Y) ^ Y and (X ^ Y) & Y as ~X & Y. */
1834 (for opo (bit_and bit_xor)
1835 opi (bit_xor bit_and)
1837 (opo:c (opi:cs @0 @1) @1)
1838 (bit_and (bit_not @0) @1)))
1840 /* Given a bit-wise operation CODE applied to ARG0 and ARG1, see if both
1841 operands are another bit-wise operation with a common input. If so,
1842 distribute the bit operations to save an operation and possibly two if
1843 constants are involved. For example, convert
1844 (A | B) & (A | C) into A | (B & C)
1845 Further simplification will occur if B and C are constants. */
1846 (for op (bit_and bit_ior bit_xor)
1847 rop (bit_ior bit_and bit_and)
1849 (op (convert? (rop:c @@0 @1)) (convert? (rop:c @0 @2)))
1850 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1851 && tree_nop_conversion_p (type, TREE_TYPE (@2)))
1852 (rop (convert @0) (op (convert @1) (convert @2))))))
1854 /* Some simple reassociation for bit operations, also handled in reassoc. */
1855 /* (X & Y) & Y -> X & Y
1856 (X | Y) | Y -> X | Y */
1857 (for op (bit_and bit_ior)
1859 (op:c (convert1?@2 (op:c @0 @@1)) (convert2? @1))
1861 /* (X ^ Y) ^ Y -> X */
1863 (bit_xor:c (convert1? (bit_xor:c @0 @@1)) (convert2? @1))
1866 /* (X & ~Y) & Y -> 0 */
1868 (bit_and:c (bit_and @0 @1) @2)
1869 (with { bool wascmp; }
1870 (if (bitwise_inverted_equal_p (@0, @2, wascmp)
1871 || bitwise_inverted_equal_p (@1, @2, wascmp))
1872 { wascmp ? constant_boolean_node (false, type) : build_zero_cst (type); })))
1873 /* (X | ~Y) | Y -> -1 */
1875 (bit_ior:c (bit_ior @0 @1) @2)
1876 (with { bool wascmp; }
1877 (if ((bitwise_inverted_equal_p (@0, @2, wascmp)
1878 || bitwise_inverted_equal_p (@1, @2, wascmp))
1879 && (!wascmp || element_precision (type) == 1))
1880 { build_all_ones_cst (TREE_TYPE (@0)); })))
1882 /* (X & Y) & (X & Z) -> (X & Y) & Z
1883 (X | Y) | (X | Z) -> (X | Y) | Z */
1884 (for op (bit_and bit_ior)
1886 (op (convert1?@3 (op:c@4 @0 @1)) (convert2?@5 (op:c@6 @0 @2)))
1887 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1888 && tree_nop_conversion_p (type, TREE_TYPE (@2)))
1889 (if (single_use (@5) && single_use (@6))
1890 (op @3 (convert @2))
1891 (if (single_use (@3) && single_use (@4))
1892 (op (convert @1) @5))))))
1893 /* (X ^ Y) ^ (X ^ Z) -> Y ^ Z */
1895 (bit_xor (convert1? (bit_xor:c @0 @1)) (convert2? (bit_xor:c @0 @2)))
1896 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1897 && tree_nop_conversion_p (type, TREE_TYPE (@2)))
1898 (bit_xor (convert @1) (convert @2))))
1900 /* Convert abs (abs (X)) into abs (X).
1901 also absu (absu (X)) into absu (X). */
1907 (absu (convert@2 (absu@1 @0)))
1908 (if (tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@1)))
1911 /* Convert abs[u] (-X) -> abs[u] (X). */
1920 /* Convert abs[u] (X) where X is nonnegative -> (X). */
1922 (abs tree_expr_nonnegative_p@0)
1926 (absu tree_expr_nonnegative_p@0)
1929 /* Simplify (-(X < 0) | 1) * X into abs (X) or absu(X). */
1931 (mult:c (nop_convert1?
1932 (bit_ior (nop_convert2? (negate (convert? (lt @0 integer_zerop))))
1935 (if (INTEGRAL_TYPE_P (type)
1936 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
1937 && !TYPE_UNSIGNED (TREE_TYPE (@0)))
1938 (if (TYPE_UNSIGNED (type))
1945 /* A few cases of fold-const.cc negate_expr_p predicate. */
1946 (match negate_expr_p
1948 (if ((INTEGRAL_TYPE_P (type)
1949 && TYPE_UNSIGNED (type))
1950 || (!TYPE_OVERFLOW_SANITIZED (type)
1951 && may_negate_without_overflow_p (t)))))
1952 (match negate_expr_p
1954 (match negate_expr_p
1956 (if (!TYPE_OVERFLOW_SANITIZED (type))))
1957 (match negate_expr_p
1959 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (t)))))
1960 /* VECTOR_CST handling of non-wrapping types would recurse in unsupported
1962 (match negate_expr_p
1964 (if (FLOAT_TYPE_P (TREE_TYPE (type)) || TYPE_OVERFLOW_WRAPS (type))))
1965 (match negate_expr_p
1967 (if ((ANY_INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_WRAPS (type))
1968 || (FLOAT_TYPE_P (type)
1969 && !HONOR_SIGN_DEPENDENT_ROUNDING (type)
1970 && !HONOR_SIGNED_ZEROS (type)))))
1972 /* (-A) * (-B) -> A * B */
1974 (mult:c (convert1? (negate @0)) (convert2? negate_expr_p@1))
1975 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
1976 && tree_nop_conversion_p (type, TREE_TYPE (@1)))
1977 (mult (convert @0) (convert (negate @1)))))
1979 /* -(A + B) -> (-B) - A. */
1981 (negate (plus:c @0 negate_expr_p@1))
1982 (if (!HONOR_SIGN_DEPENDENT_ROUNDING (type)
1983 && !HONOR_SIGNED_ZEROS (type))
1984 (minus (negate @1) @0)))
1986 /* -(A - B) -> B - A. */
1988 (negate (minus @0 @1))
1989 (if ((ANY_INTEGRAL_TYPE_P (type) && !TYPE_OVERFLOW_SANITIZED (type))
1990 || (FLOAT_TYPE_P (type)
1991 && !HONOR_SIGN_DEPENDENT_ROUNDING (type)
1992 && !HONOR_SIGNED_ZEROS (type)))
1995 (negate (pointer_diff @0 @1))
1996 (if (TYPE_OVERFLOW_UNDEFINED (type))
1997 (pointer_diff @1 @0)))
1999 /* A - B -> A + (-B) if B is easily negatable. */
2001 (minus @0 negate_expr_p@1)
2002 (if (!FIXED_POINT_TYPE_P (type))
2003 (plus @0 (negate @1))))
2005 /* 1 - a is a ^ 1 if a had a bool range. */
2006 /* This is only enabled for gimple as sometimes
2007 cfun is not set for the function which contains
2008 the SSA_NAME (e.g. while IPA passes are happening,
2009 fold might be called). */
2011 (minus integer_onep@0 SSA_NAME@1)
2012 (if (INTEGRAL_TYPE_P (type)
2013 && ssa_name_has_boolean_range (@1))
2016 /* Other simplifications of negation (c.f. fold_negate_expr_1). */
2018 (negate (mult:c@0 @1 negate_expr_p@2))
2019 (if (! TYPE_UNSIGNED (type)
2020 && ! HONOR_SIGN_DEPENDENT_ROUNDING (type)
2022 (mult @1 (negate @2))))
2025 (negate (rdiv@0 @1 negate_expr_p@2))
2026 (if (! HONOR_SIGN_DEPENDENT_ROUNDING (type)
2028 (rdiv @1 (negate @2))))
2031 (negate (rdiv@0 negate_expr_p@1 @2))
2032 (if (! HONOR_SIGN_DEPENDENT_ROUNDING (type)
2034 (rdiv (negate @1) @2)))
2036 /* Fold -((int)x >> (prec - 1)) into (unsigned)x >> (prec - 1). */
2038 (negate (convert? (rshift @0 INTEGER_CST@1)))
2039 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
2040 && wi::to_wide (@1) == element_precision (type) - 1)
2041 (with { tree stype = TREE_TYPE (@0);
2042 tree ntype = TYPE_UNSIGNED (stype) ? signed_type_for (stype)
2043 : unsigned_type_for (stype); }
2044 (if (VECTOR_TYPE_P (type))
2045 (view_convert (rshift (view_convert:ntype @0) @1))
2046 (convert (rshift (convert:ntype @0) @1))))))
2048 /* Try to fold (type) X op CST -> (type) (X op ((type-x) CST))
2050 For bitwise binary operations apply operand conversions to the
2051 binary operation result instead of to the operands. This allows
2052 to combine successive conversions and bitwise binary operations.
2053 We combine the above two cases by using a conditional convert. */
2054 (for bitop (bit_and bit_ior bit_xor)
2056 (bitop (convert@2 @0) (convert?@3 @1))
2057 (if (((TREE_CODE (@1) == INTEGER_CST
2058 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
2059 && (int_fits_type_p (@1, TREE_TYPE (@0))
2060 || tree_nop_conversion_p (TREE_TYPE (@0), type)))
2061 || types_match (@0, @1))
2062 && !POINTER_TYPE_P (TREE_TYPE (@0))
2063 && !VECTOR_TYPE_P (TREE_TYPE (@0))
2064 && TREE_CODE (TREE_TYPE (@0)) != OFFSET_TYPE
2065 /* ??? This transform conflicts with fold-const.cc doing
2066 Convert (T)(x & c) into (T)x & (T)c, if c is an integer
2067 constants (if x has signed type, the sign bit cannot be set
2068 in c). This folds extension into the BIT_AND_EXPR.
2069 Restrict it to GIMPLE to avoid endless recursions. */
2070 && (bitop != BIT_AND_EXPR || GIMPLE)
2071 && (/* That's a good idea if the conversion widens the operand, thus
2072 after hoisting the conversion the operation will be narrower.
2073 It is also a good if the conversion is a nop as moves the
2074 conversion to one side; allowing for combining of the conversions. */
2075 TYPE_PRECISION (TREE_TYPE (@0)) < TYPE_PRECISION (type)
2076 /* The conversion check for being a nop can only be done at the gimple
2077 level as fold_binary has some re-association code which can conflict
2078 with this if there is a "constant" which is not a full INTEGER_CST. */
2079 || (GIMPLE && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (type))
2080 /* It's also a good idea if the conversion is to a non-integer
2082 || GET_MODE_CLASS (TYPE_MODE (type)) != MODE_INT
2083 /* Or if the precision of TO is not the same as the precision
2085 || !type_has_mode_precision_p (type)
2086 /* In GIMPLE, getting rid of 2 conversions for one new results
2089 && TREE_CODE (@1) != INTEGER_CST
2090 && tree_nop_conversion_p (type, TREE_TYPE (@0))
2092 && single_use (@3))))
2093 (convert (bitop @0 (convert @1)))))
2094 /* In GIMPLE, getting rid of 2 conversions for one new results
2097 (convert (bitop:cs@2 (nop_convert:s @0) @1))
2099 && TREE_CODE (@1) != INTEGER_CST
2100 && tree_nop_conversion_p (type, TREE_TYPE (@2))
2101 && types_match (type, @0)
2102 && !POINTER_TYPE_P (TREE_TYPE (@0))
2103 && TREE_CODE (TREE_TYPE (@0)) != OFFSET_TYPE)
2104 (bitop @0 (convert @1)))))
2106 (for bitop (bit_and bit_ior)
2107 rbitop (bit_ior bit_and)
2108 /* (x | y) & x -> x */
2109 /* (x & y) | x -> x */
2111 (bitop:c (rbitop:c @0 @1) @0)
2113 /* (~x | y) & x -> x & y */
2114 /* (~x & y) | x -> x | y */
2116 (bitop:c (rbitop:c @2 @1) @0)
2117 (with { bool wascmp; }
2118 (if (bitwise_inverted_equal_p (@0, @2, wascmp)
2119 && (!wascmp || element_precision (type) == 1))
2121 /* (x | y) & (x & z) -> (x & z) */
2122 /* (x & y) | (x | z) -> (x | z) */
2124 (bitop:c (rbitop:c @0 @1) (bitop:c@3 @0 @2))
2126 /* (x | c) & ~(y | c) -> x & ~(y | c) */
2127 /* (x & c) | ~(y & c) -> x | ~(y & c) */
2129 (bitop:c (rbitop:c @0 @1) (bit_not@3 (rbitop:c @1 @2)))
2131 /* x & ~(y | x) -> 0 */
2132 /* x | ~(y & x) -> -1 */
2134 (bitop:c @0 (bit_not (rbitop:c @0 @1)))
2135 (if (bitop == BIT_AND_EXPR)
2136 { build_zero_cst (type); }
2137 { build_minus_one_cst (type); })))
2139 /* ((x | y) & z) | x -> (z & y) | x
2140 ((x ^ y) & z) | x -> (z & y) | x */
2141 (for op (bit_ior bit_xor)
2143 (bit_ior:c (nop_convert1?:s
2144 (bit_and:cs (nop_convert2?:s (op:cs @0 @1)) @2)) @3)
2145 (if (bitwise_equal_p (@0, @3))
2146 (convert (bit_ior (bit_and @1 (convert @2)) (convert @0))))))
2148 /* (x | CST1) & CST2 -> (x & CST2) | (CST1 & CST2) */
2150 (bit_and (bit_ior @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
2151 (bit_ior (bit_and @0 @2) (bit_and! @1 @2)))
2153 /* Combine successive equal operations with constants. */
2154 (for bitop (bit_and bit_ior bit_xor)
2156 (bitop (bitop @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
2157 (if (!CONSTANT_CLASS_P (@0))
2158 /* This is the canonical form regardless of whether (bitop @1 @2) can be
2159 folded to a constant. */
2160 (bitop @0 (bitop! @1 @2))
2161 /* In this case we have three constants and (bitop @0 @1) doesn't fold
2162 to a constant. This can happen if @0 or @1 is a POLY_INT_CST and if
2163 the values involved are such that the operation can't be decided at
2164 compile time. Try folding one of @0 or @1 with @2 to see whether
2165 that combination can be decided at compile time.
2167 Keep the existing form if both folds fail, to avoid endless
2169 (with { tree cst1 = const_binop (bitop, type, @0, @2); }
2171 (bitop @1 { cst1; })
2172 (with { tree cst2 = const_binop (bitop, type, @1, @2); }
2174 (bitop @0 { cst2; }))))))))
2176 /* Try simple folding for X op !X, and X op X with the help
2177 of the truth_valued_p and logical_inverted_value predicates. */
2178 (match truth_valued_p
2180 (if (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1)))
2181 (for op (tcc_comparison truth_and truth_andif truth_or truth_orif truth_xor)
2182 (match truth_valued_p
2184 (match truth_valued_p
2187 (match (logical_inverted_value @0)
2189 (match (logical_inverted_value @0)
2190 (bit_not truth_valued_p@0))
2191 (match (logical_inverted_value @0)
2192 (eq @0 integer_zerop))
2193 (match (logical_inverted_value @0)
2194 (ne truth_valued_p@0 integer_truep))
2195 (match (logical_inverted_value @0)
2196 (bit_xor truth_valued_p@0 integer_truep))
2200 (bit_and:c @0 (logical_inverted_value @0))
2201 { build_zero_cst (type); })
2202 /* X | !X and X ^ !X -> 1, , if X is truth-valued. */
2203 (for op (bit_ior bit_xor)
2205 (op:c truth_valued_p@0 (logical_inverted_value @0))
2206 { constant_boolean_node (true, type); }))
2207 /* X ==/!= !X is false/true. */
2210 (op:c truth_valued_p@0 (logical_inverted_value @0))
2211 { constant_boolean_node (op == NE_EXPR ? true : false, type); }))
2215 (bit_not (bit_not @0))
2218 /* zero_one_valued_p will match when a value is known to be either
2219 0 or 1 including constants 0 or 1.
2220 Signed 1-bits includes -1 so they cannot match here. */
2221 (match zero_one_valued_p
2223 (if (INTEGRAL_TYPE_P (type)
2224 && (TYPE_UNSIGNED (type)
2225 || TYPE_PRECISION (type) > 1)
2226 && wi::leu_p (tree_nonzero_bits (@0), 1))))
2227 (match zero_one_valued_p
2229 (if (INTEGRAL_TYPE_P (type)
2230 && (TYPE_UNSIGNED (type)
2231 || TYPE_PRECISION (type) > 1))))
2233 /* (a&1) is always [0,1] too. This is useful again when
2234 the range is not known. */
2235 /* Note this can't be recursive due to VN handling of equivalents,
2236 VN and would cause an infinite recursion. */
2237 (match zero_one_valued_p
2238 (bit_and:c@0 @1 integer_onep)
2239 (if (INTEGRAL_TYPE_P (type))))
2241 /* A conversion from an zero_one_valued_p is still a [0,1].
2242 This is useful when the range of a variable is not known */
2243 /* Note this matches can't be recursive because of the way VN handles
2244 nop conversions being equivalent and then recursive between them. */
2245 (match zero_one_valued_p
2247 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2248 && (TYPE_UNSIGNED (TREE_TYPE (@1))
2249 || TYPE_PRECISION (TREE_TYPE (@1)) > 1)
2250 && wi::leu_p (tree_nonzero_bits (@1), 1))))
2252 /* Transform { 0 or 1 } * { 0 or 1 } into { 0 or 1 } & { 0 or 1 }. */
2254 (mult zero_one_valued_p@0 zero_one_valued_p@1)
2255 (if (INTEGRAL_TYPE_P (type))
2258 (for cmp (tcc_comparison)
2259 icmp (inverted_tcc_comparison)
2260 /* Fold (((a < b) & c) | ((a >= b) & d)) into (a < b ? c : d) & 1. */
2263 (bit_and:c (convert? (cmp@0 @01 @02)) @3)
2264 (bit_and:c (convert? (icmp@4 @01 @02)) @5))
2265 (if (INTEGRAL_TYPE_P (type)
2266 && invert_tree_comparison (cmp, HONOR_NANS (@01)) == icmp
2267 /* The scalar version has to be canonicalized after vectorization
2268 because it makes unconditional loads conditional ones, which
2269 means we lose vectorization because the loads may trap. */
2270 && canonicalize_math_after_vectorization_p ())
2271 (bit_and (cond @0 @3 @5) { build_one_cst (type); })))
2273 /* Fold ((-(a < b) & c) | (-(a >= b) & d)) into a < b ? c : d. This is
2274 canonicalized further and we recognize the conditional form:
2275 (a < b ? c : 0) | (a >= b ? d : 0) into a < b ? c : d. */
2278 (cond (cmp@0 @01 @02) @3 zerop)
2279 (cond (icmp@4 @01 @02) @5 zerop))
2280 (if (INTEGRAL_TYPE_P (type)
2281 && invert_tree_comparison (cmp, HONOR_NANS (@01)) == icmp
2282 /* The scalar version has to be canonicalized after vectorization
2283 because it makes unconditional loads conditional ones, which
2284 means we lose vectorization because the loads may trap. */
2285 && canonicalize_math_after_vectorization_p ())
2288 /* Vector Fold (((a < b) & c) | ((a >= b) & d)) into a < b ? c : d.
2289 and ((~(a < b) & c) | (~(a >= b) & d)) into a < b ? c : d. */
2292 (bit_and:c (vec_cond:s (cmp@0 @6 @7) @4 @5) @2)
2293 (bit_and:c (vec_cond:s (icmp@1 @6 @7) @4 @5) @3))
2294 (if (integer_zerop (@5)
2295 && invert_tree_comparison (cmp, HONOR_NANS (@6)) == icmp)
2297 (if (integer_onep (@4))
2298 (bit_and (vec_cond @0 @2 @3) @4))
2299 (if (integer_minus_onep (@4))
2300 (vec_cond @0 @2 @3)))
2301 (if (integer_zerop (@4)
2302 && invert_tree_comparison (cmp, HONOR_NANS (@6)) == icmp)
2304 (if (integer_onep (@5))
2305 (bit_and (vec_cond @0 @3 @2) @5))
2306 (if (integer_minus_onep (@5))
2307 (vec_cond @0 @3 @2))))))
2309 /* Scalar Vectorized Fold ((-(a < b) & c) | (-(a >= b) & d))
2310 into a < b ? d : c. */
2313 (vec_cond:s (cmp@0 @4 @5) @2 integer_zerop)
2314 (vec_cond:s (icmp@1 @4 @5) @3 integer_zerop))
2315 (if (invert_tree_comparison (cmp, HONOR_NANS (@4)) == icmp)
2316 (vec_cond @0 @2 @3))))
2318 /* Transform X & -Y into X * Y when Y is { 0 or 1 }. */
2320 (bit_and:c (convert? (negate zero_one_valued_p@0)) @1)
2321 (if (INTEGRAL_TYPE_P (type)
2322 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
2323 && TREE_CODE (TREE_TYPE (@0)) != BOOLEAN_TYPE
2324 /* Sign extending of the neg or a truncation of the neg
2326 && (!TYPE_UNSIGNED (TREE_TYPE (@0))
2327 || TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))))
2328 (mult (convert @0) @1)))
2330 /* Narrow integer multiplication by a zero_one_valued_p operand.
2331 Multiplication by [0,1] is guaranteed not to overflow. */
2333 (convert (mult@0 zero_one_valued_p@1 INTEGER_CST@2))
2334 (if (INTEGRAL_TYPE_P (type)
2335 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
2336 && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (@0)))
2337 (mult (convert @1) (convert @2))))
2339 /* (X << C) != 0 can be simplified to X, when C is zero_one_valued_p.
2340 Check that the shift is well-defined (C is less than TYPE_PRECISION)
2341 as some targets (such as x86's SSE) may return zero for larger C. */
2343 (ne (lshift zero_one_valued_p@0 INTEGER_CST@1) integer_zerop@2)
2344 (if (tree_fits_shwi_p (@1)
2345 && tree_to_shwi (@1) > 0
2346 && tree_to_shwi (@1) < TYPE_PRECISION (TREE_TYPE (@0)))
2349 /* (X << C) == 0 can be simplified to X == 0, when C is zero_one_valued_p.
2350 Check that the shift is well-defined (C is less than TYPE_PRECISION)
2351 as some targets (such as x86's SSE) may return zero for larger C. */
2353 (eq (lshift zero_one_valued_p@0 INTEGER_CST@1) integer_zerop@2)
2354 (if (tree_fits_shwi_p (@1)
2355 && tree_to_shwi (@1) > 0
2356 && tree_to_shwi (@1) < TYPE_PRECISION (TREE_TYPE (@0)))
2359 /* Convert ~ (-A) to A - 1. */
2361 (bit_not (convert? (negate @0)))
2362 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
2363 || !TYPE_UNSIGNED (TREE_TYPE (@0)))
2364 (convert (minus @0 { build_each_one_cst (TREE_TYPE (@0)); }))))
2366 /* Convert - (~A) to A + 1. */
2368 (negate (nop_convert? (bit_not @0)))
2369 (plus (view_convert @0) { build_each_one_cst (type); }))
2371 /* (a & b) ^ (a == b) -> !(a | b) */
2372 /* (a & b) == (a ^ b) -> !(a | b) */
2373 (for first_op (bit_xor eq)
2374 second_op (eq bit_xor)
2376 (first_op:c (bit_and:c truth_valued_p@0 truth_valued_p@1) (second_op:c @0 @1))
2377 (bit_not (bit_ior @0 @1))))
2379 /* Convert ~ (A - 1) or ~ (A + -1) to -A. */
2381 (bit_not (convert? (minus @0 integer_each_onep)))
2382 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
2383 || !TYPE_UNSIGNED (TREE_TYPE (@0)))
2384 (convert (negate @0))))
2386 (bit_not (convert? (plus @0 integer_all_onesp)))
2387 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
2388 || !TYPE_UNSIGNED (TREE_TYPE (@0)))
2389 (convert (negate @0))))
2391 /* Part of convert ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify. */
2393 (bit_not (convert? (bit_xor @0 INTEGER_CST@1)))
2394 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2395 (convert (bit_xor @0 (bit_not @1)))))
2397 (bit_not (convert? (bit_xor:c (bit_not @0) @1)))
2398 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2399 (convert (bit_xor @0 @1))))
2401 /* Otherwise prefer ~(X ^ Y) to ~X ^ Y as more canonical. */
2403 (bit_xor:c (nop_convert?:s (bit_not:s @0)) @1)
2404 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2405 (bit_not (bit_xor (view_convert @0) @1))))
2407 /* ~(a ^ b) is a == b for truth valued a and b. */
2409 (bit_not (bit_xor:s truth_valued_p@0 truth_valued_p@1))
2410 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2411 && TYPE_PRECISION (TREE_TYPE (@0)) == 1)
2412 (convert (eq @0 @1))))
2414 /* (~a) == b is a ^ b for truth valued a and b. */
2416 (eq:c (bit_not:s truth_valued_p@0) truth_valued_p@1)
2417 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2418 && TYPE_PRECISION (TREE_TYPE (@0)) == 1)
2419 (convert (bit_xor @0 @1))))
2421 /* (x & ~m) | (y & m) -> ((x ^ y) & m) ^ x */
2423 (bit_ior:c (bit_and:cs @0 (bit_not @2)) (bit_and:cs @1 @2))
2424 (bit_xor (bit_and (bit_xor @0 @1) @2) @0))
2426 /* Fold A - (A & B) into ~B & A. */
2428 (minus (convert1? @0) (convert2?:s (bit_and:cs @@0 @1)))
2429 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
2430 && tree_nop_conversion_p (type, TREE_TYPE (@1)))
2431 (convert (bit_and (bit_not @1) @0))))
2433 /* (m1 CMP m2) * d -> (m1 CMP m2) ? d : 0 */
2434 (if (!canonicalize_math_p ())
2435 (for cmp (tcc_comparison)
2437 (mult:c (convert (cmp@0 @1 @2)) @3)
2438 (if (INTEGRAL_TYPE_P (type)
2439 && INTEGRAL_TYPE_P (TREE_TYPE (@0)))
2440 (cond @0 @3 { build_zero_cst (type); })))
2441 /* (-(m1 CMP m2)) & d -> (m1 CMP m2) ? d : 0 */
2443 (bit_and:c (negate (convert (cmp@0 @1 @2))) @3)
2444 (if (INTEGRAL_TYPE_P (type)
2445 && INTEGRAL_TYPE_P (TREE_TYPE (@0)))
2446 (cond @0 @3 { build_zero_cst (type); })))
2450 /* For integral types with undefined overflow and C != 0 fold
2451 x * C EQ/NE y * C into x EQ/NE y. */
2454 (cmp (mult:c @0 @1) (mult:c @2 @1))
2455 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2456 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2457 && tree_expr_nonzero_p (@1))
2460 /* For integral types with wrapping overflow and C odd fold
2461 x * C EQ/NE y * C into x EQ/NE y. */
2464 (cmp (mult @0 INTEGER_CST@1) (mult @2 @1))
2465 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2466 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))
2467 && (TREE_INT_CST_LOW (@1) & 1) != 0)
2470 /* For integral types with undefined overflow and C != 0 fold
2471 x * C RELOP y * C into:
2473 x RELOP y for nonnegative C
2474 y RELOP x for negative C */
2475 (for cmp (lt gt le ge)
2477 (cmp (mult:c @0 @1) (mult:c @2 @1))
2478 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2479 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
2480 (if (tree_expr_nonnegative_p (@1) && tree_expr_nonzero_p (@1))
2482 (if (TREE_CODE (@1) == INTEGER_CST
2483 && wi::neg_p (wi::to_wide (@1), TYPE_SIGN (TREE_TYPE (@1))))
2486 /* (X - 1U) <= INT_MAX-1U into (int) X > 0. */
2490 (cmp (plus @0 integer_minus_onep@1) INTEGER_CST@2)
2491 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2492 && TYPE_UNSIGNED (TREE_TYPE (@0))
2493 && TYPE_PRECISION (TREE_TYPE (@0)) > 1
2494 && (wi::to_wide (@2)
2495 == wi::max_value (TYPE_PRECISION (TREE_TYPE (@0)), SIGNED) - 1))
2496 (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
2497 (icmp (convert:stype @0) { build_int_cst (stype, 0); })))))
2499 /* X / 4 < Y / 4 iff X < Y when the division is known to be exact. */
2500 (for cmp (simple_comparison)
2502 (cmp (convert?@3 (exact_div @0 INTEGER_CST@2)) (convert? (exact_div @1 @2)))
2503 (if (element_precision (@3) >= element_precision (@0)
2504 && types_match (@0, @1))
2505 (if (wi::lt_p (wi::to_wide (@2), 0, TYPE_SIGN (TREE_TYPE (@2))))
2506 (if (!TYPE_UNSIGNED (TREE_TYPE (@3)))
2508 (if (tree_expr_nonzero_p (@0) && tree_expr_nonzero_p (@1))
2511 tree utype = unsigned_type_for (TREE_TYPE (@0));
2513 (cmp (convert:utype @1) (convert:utype @0)))))
2514 (if (wi::gt_p (wi::to_wide (@2), 1, TYPE_SIGN (TREE_TYPE (@2))))
2515 (if (TYPE_UNSIGNED (TREE_TYPE (@0)) || !TYPE_UNSIGNED (TREE_TYPE (@3)))
2519 tree utype = unsigned_type_for (TREE_TYPE (@0));
2521 (cmp (convert:utype @0) (convert:utype @1)))))))))
2523 /* X / C1 op C2 into a simple range test. */
2524 (for cmp (simple_comparison)
2526 (cmp (trunc_div:s @0 INTEGER_CST@1) INTEGER_CST@2)
2527 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2528 && integer_nonzerop (@1)
2529 && !TREE_OVERFLOW (@1)
2530 && !TREE_OVERFLOW (@2))
2531 (with { tree lo, hi; bool neg_overflow;
2532 enum tree_code code = fold_div_compare (cmp, @1, @2, &lo, &hi,
2535 (if (code == LT_EXPR || code == GE_EXPR)
2536 (if (TREE_OVERFLOW (lo))
2537 { build_int_cst (type, (code == LT_EXPR) ^ neg_overflow); }
2538 (if (code == LT_EXPR)
2541 (if (code == LE_EXPR || code == GT_EXPR)
2542 (if (TREE_OVERFLOW (hi))
2543 { build_int_cst (type, (code == LE_EXPR) ^ neg_overflow); }
2544 (if (code == LE_EXPR)
2548 { build_int_cst (type, code == NE_EXPR); })
2549 (if (code == EQ_EXPR && !hi)
2551 (if (code == EQ_EXPR && !lo)
2553 (if (code == NE_EXPR && !hi)
2555 (if (code == NE_EXPR && !lo)
2558 { build_range_check (UNKNOWN_LOCATION, type, @0, code == EQ_EXPR,
2562 tree etype = range_check_type (TREE_TYPE (@0));
2565 hi = fold_convert (etype, hi);
2566 lo = fold_convert (etype, lo);
2567 hi = const_binop (MINUS_EXPR, etype, hi, lo);
2570 (if (etype && hi && !TREE_OVERFLOW (hi))
2571 (if (code == EQ_EXPR)
2572 (le (minus (convert:etype @0) { lo; }) { hi; })
2573 (gt (minus (convert:etype @0) { lo; }) { hi; })))))))))
2575 /* X + Z < Y + Z is the same as X < Y when there is no overflow. */
2576 (for op (lt le ge gt)
2578 (op (plus:c @0 @2) (plus:c @1 @2))
2579 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2580 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
2583 /* As a special case, X + C < Y + C is the same as (signed) X < (signed) Y
2584 when C is an unsigned integer constant with only the MSB set, and X and
2585 Y have types of equal or lower integer conversion rank than C's. */
2586 (for op (lt le ge gt)
2588 (op (plus @1 INTEGER_CST@0) (plus @2 @0))
2589 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2590 && TYPE_UNSIGNED (TREE_TYPE (@0))
2591 && wi::only_sign_bit_p (wi::to_wide (@0)))
2592 (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
2593 (op (convert:stype @1) (convert:stype @2))))))
2595 /* For equality and subtraction, this is also true with wrapping overflow. */
2596 (for op (eq ne minus)
2598 (op (plus:c @0 @2) (plus:c @1 @2))
2599 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2600 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2601 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
2603 /* And similar for pointers. */
2606 (op (pointer_plus @0 @1) (pointer_plus @0 @2))
2609 (pointer_diff (pointer_plus @0 @1) (pointer_plus @0 @2))
2610 (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
2611 (convert (minus @1 @2))))
2613 /* X - Z < Y - Z is the same as X < Y when there is no overflow. */
2614 (for op (lt le ge gt)
2616 (op (minus @0 @2) (minus @1 @2))
2617 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2618 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
2620 /* For equality and subtraction, this is also true with wrapping overflow. */
2621 (for op (eq ne minus)
2623 (op (minus @0 @2) (minus @1 @2))
2624 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2625 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2626 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
2628 /* And for pointers... */
2629 (for op (simple_comparison)
2631 (op (pointer_diff@3 @0 @2) (pointer_diff @1 @2))
2632 (if (!TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
2635 (minus (pointer_diff@3 @0 @2) (pointer_diff @1 @2))
2636 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@3))
2637 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
2638 (pointer_diff @0 @1)))
2640 /* Z - X < Z - Y is the same as Y < X when there is no overflow. */
2641 (for op (lt le ge gt)
2643 (op (minus @2 @0) (minus @2 @1))
2644 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2645 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
2647 /* For equality and subtraction, this is also true with wrapping overflow. */
2648 (for op (eq ne minus)
2650 (op (minus @2 @0) (minus @2 @1))
2651 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2652 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2653 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
2655 /* And for pointers... */
2656 (for op (simple_comparison)
2658 (op (pointer_diff@3 @2 @0) (pointer_diff @2 @1))
2659 (if (!TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
2662 (minus (pointer_diff@3 @2 @0) (pointer_diff @2 @1))
2663 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@3))
2664 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
2665 (pointer_diff @1 @0)))
2667 /* X + Y < Y is the same as X < 0 when there is no overflow. */
2668 (for op (lt le gt ge)
2670 (op:c (plus:c@2 @0 @1) @1)
2671 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2672 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2673 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0))
2674 && (CONSTANT_CLASS_P (@0) || single_use (@2)))
2675 (op @0 { build_zero_cst (TREE_TYPE (@0)); }))))
2676 /* For equality, this is also true with wrapping overflow. */
2679 (op:c (nop_convert?@3 (plus:c@2 @0 (convert1? @1))) (convert2? @1))
2680 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2681 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2682 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
2683 && (CONSTANT_CLASS_P (@0) || (single_use (@2) && single_use (@3)))
2684 && tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@2))
2685 && tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@1)))
2686 (op @0 { build_zero_cst (TREE_TYPE (@0)); })))
2688 (op:c (nop_convert?@3 (pointer_plus@2 (convert1? @0) @1)) (convert2? @0))
2689 (if (tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@0))
2690 && tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0))
2691 && (CONSTANT_CLASS_P (@1) || (single_use (@2) && single_use (@3))))
2692 (op @1 { build_zero_cst (TREE_TYPE (@1)); }))))
2694 /* (&a + b) !=/== (&a[1] + c) -> (&a[0] - &a[1]) + b !=/== c */
2697 (neeq:c ADDR_EXPR@0 (pointer_plus @2 @3))
2698 (with { poly_int64 diff; tree inner_type = TREE_TYPE (@3);}
2699 (if (ptr_difference_const (@0, @2, &diff))
2700 (neeq { build_int_cst_type (inner_type, diff); } @3))))
2702 (neeq (pointer_plus ADDR_EXPR@0 @1) (pointer_plus ADDR_EXPR@2 @3))
2703 (with { poly_int64 diff; tree inner_type = TREE_TYPE (@1);}
2704 (if (ptr_difference_const (@0, @2, &diff))
2705 (neeq (plus { build_int_cst_type (inner_type, diff); } @1) @3)))))
2707 /* X - Y < X is the same as Y > 0 when there is no overflow.
2708 For equality, this is also true with wrapping overflow. */
2709 (for op (simple_comparison)
2711 (op:c @0 (minus@2 @0 @1))
2712 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2713 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2714 || ((op == EQ_EXPR || op == NE_EXPR)
2715 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
2716 && (CONSTANT_CLASS_P (@1) || single_use (@2)))
2717 (op @1 { build_zero_cst (TREE_TYPE (@1)); }))))
2720 (X / Y) == 0 -> X < Y if X, Y are unsigned.
2721 (X / Y) != 0 -> X >= Y, if X, Y are unsigned. */
2725 (cmp (trunc_div @0 @1) integer_zerop)
2726 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
2727 /* Complex ==/!= is allowed, but not </>=. */
2728 && TREE_CODE (TREE_TYPE (@0)) != COMPLEX_TYPE
2729 && (VECTOR_TYPE_P (type) || !VECTOR_TYPE_P (TREE_TYPE (@0))))
2732 /* X == C - X can never be true if C is odd. */
2735 (cmp:c (convert? @0) (convert1? (minus INTEGER_CST@1 (convert2? @0))))
2736 (if (TREE_INT_CST_LOW (@1) & 1)
2737 { constant_boolean_node (cmp == NE_EXPR, type); })))
2742 U needs to be non-negative.
2746 U and N needs to be non-negative
2750 U needs to be non-negative and N needs to be a negative constant.
2752 (for cmp (lt ge le gt )
2753 bitop (bit_ior bit_ior bit_and bit_and)
2755 (cmp:c (bitop:c tree_expr_nonnegative_p@0 @1) @0)
2756 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)))
2757 (if (bitop == BIT_AND_EXPR || tree_expr_nonnegative_p (@1))
2758 { constant_boolean_node (cmp == GE_EXPR || cmp == LE_EXPR, type); }
2759 /* The sign is opposite now so the comparison is swapped around. */
2760 (if (TREE_CODE (@1) == INTEGER_CST && wi::neg_p (wi::to_wide (@1)))
2761 { constant_boolean_node (cmp == LT_EXPR, type); })))))
2763 /* Arguments on which one can call get_nonzero_bits to get the bits
2765 (match with_possible_nonzero_bits
2767 (match with_possible_nonzero_bits
2769 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))))
2770 /* Slightly extended version, do not make it recursive to keep it cheap. */
2771 (match (with_possible_nonzero_bits2 @0)
2772 with_possible_nonzero_bits@0)
2773 (match (with_possible_nonzero_bits2 @0)
2774 (bit_and:c with_possible_nonzero_bits@0 @2))
2776 /* Same for bits that are known to be set, but we do not have
2777 an equivalent to get_nonzero_bits yet. */
2778 (match (with_certain_nonzero_bits2 @0)
2780 (match (with_certain_nonzero_bits2 @0)
2781 (bit_ior @1 INTEGER_CST@0))
2783 /* X == C (or X & Z == Y | C) is impossible if ~nonzero(X) & C != 0. */
2786 (cmp:c (with_possible_nonzero_bits2 @0) (with_certain_nonzero_bits2 @1))
2787 (if (wi::bit_and_not (wi::to_wide (@1), get_nonzero_bits (@0)) != 0)
2788 { constant_boolean_node (cmp == NE_EXPR, type); })))
2790 /* ((X inner_op C0) outer_op C1)
2791 With X being a tree where value_range has reasoned certain bits to always be
2792 zero throughout its computed value range,
2793 inner_op = {|,^}, outer_op = {|,^} and inner_op != outer_op
2794 where zero_mask has 1's for all bits that are sure to be 0 in
2796 if (inner_op == '^') C0 &= ~C1;
2797 if ((C0 & ~zero_mask) == 0) then emit (X outer_op (C0 outer_op C1)
2798 if ((C1 & ~zero_mask) == 0) then emit (X inner_op (C0 outer_op C1)
2800 (for inner_op (bit_ior bit_xor)
2801 outer_op (bit_xor bit_ior)
2804 (inner_op:s @2 INTEGER_CST@0) INTEGER_CST@1)
2808 wide_int zero_mask_not;
2812 if (TREE_CODE (@2) == SSA_NAME)
2813 zero_mask_not = get_nonzero_bits (@2);
2817 if (inner_op == BIT_XOR_EXPR)
2819 C0 = wi::bit_and_not (wi::to_wide (@0), wi::to_wide (@1));
2820 cst_emit = C0 | wi::to_wide (@1);
2824 C0 = wi::to_wide (@0);
2825 cst_emit = C0 ^ wi::to_wide (@1);
2828 (if (!fail && (C0 & zero_mask_not) == 0)
2829 (outer_op @2 { wide_int_to_tree (type, cst_emit); })
2830 (if (!fail && (wi::to_wide (@1) & zero_mask_not) == 0)
2831 (inner_op @2 { wide_int_to_tree (type, cst_emit); }))))))
2833 /* Associate (p +p off1) +p off2 as (p +p (off1 + off2)). */
2835 (pointer_plus (pointer_plus:s @0 @1) @3)
2836 (pointer_plus @0 (plus @1 @3)))
2839 (pointer_plus (convert:s (pointer_plus:s @0 @1)) @3)
2840 (convert:type (pointer_plus @0 (plus @1 @3))))
2847 tem4 = (unsigned long) tem3;
2852 (pointer_plus @0 (convert?@2 (minus@3 (convert @1) (convert @0))))
2853 /* Conditionally look through a sign-changing conversion. */
2854 (if (TYPE_PRECISION (TREE_TYPE (@2)) == TYPE_PRECISION (TREE_TYPE (@3))
2855 && ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@1)))
2856 || (GENERIC && type == TREE_TYPE (@1))))
2859 (pointer_plus @0 (convert?@2 (pointer_diff@3 @1 @@0)))
2860 (if (TYPE_PRECISION (TREE_TYPE (@2)) >= TYPE_PRECISION (TREE_TYPE (@3)))
2864 tem = (sizetype) ptr;
2868 and produce the simpler and easier to analyze with respect to alignment
2869 ... = ptr & ~algn; */
2871 (pointer_plus @0 (negate (bit_and (convert @0) INTEGER_CST@1)))
2872 (with { tree algn = wide_int_to_tree (TREE_TYPE (@0), ~wi::to_wide (@1)); }
2873 (bit_and @0 { algn; })))
2875 /* Try folding difference of addresses. */
2877 (minus (convert ADDR_EXPR@0) (convert (pointer_plus @1 @2)))
2878 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2879 (with { poly_int64 diff; }
2880 (if (ptr_difference_const (@0, @1, &diff))
2881 (minus { build_int_cst_type (type, diff); } (convert @2))))))
2883 (minus (convert (pointer_plus @0 @2)) (convert ADDR_EXPR@1))
2884 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2885 (with { poly_int64 diff; }
2886 (if (ptr_difference_const (@0, @1, &diff))
2887 (plus (convert @2) { build_int_cst_type (type, diff); })))))
2889 (minus (convert ADDR_EXPR@0) (convert @1))
2890 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2891 (with { poly_int64 diff; }
2892 (if (ptr_difference_const (@0, @1, &diff))
2893 { build_int_cst_type (type, diff); }))))
2895 (minus (convert @0) (convert ADDR_EXPR@1))
2896 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2897 (with { poly_int64 diff; }
2898 (if (ptr_difference_const (@0, @1, &diff))
2899 { build_int_cst_type (type, diff); }))))
2901 (pointer_diff (convert?@2 ADDR_EXPR@0) (convert1?@3 @1))
2902 (if (tree_nop_conversion_p (TREE_TYPE(@2), TREE_TYPE (@0))
2903 && tree_nop_conversion_p (TREE_TYPE(@3), TREE_TYPE (@1)))
2904 (with { poly_int64 diff; }
2905 (if (ptr_difference_const (@0, @1, &diff))
2906 { build_int_cst_type (type, diff); }))))
2908 (pointer_diff (convert?@2 @0) (convert1?@3 ADDR_EXPR@1))
2909 (if (tree_nop_conversion_p (TREE_TYPE(@2), TREE_TYPE (@0))
2910 && tree_nop_conversion_p (TREE_TYPE(@3), TREE_TYPE (@1)))
2911 (with { poly_int64 diff; }
2912 (if (ptr_difference_const (@0, @1, &diff))
2913 { build_int_cst_type (type, diff); }))))
2915 /* (&a+b) - (&a[1] + c) -> sizeof(a[0]) + (b - c) */
2917 (pointer_diff (pointer_plus ADDR_EXPR@0 @1) (pointer_plus ADDR_EXPR@2 @3))
2918 (with { poly_int64 diff; }
2919 (if (ptr_difference_const (@0, @2, &diff))
2920 (plus { build_int_cst_type (type, diff); } (convert (minus @1 @3))))))
2921 /* (p + b) - &p->d -> offsetof (*p, d) + b */
2923 (pointer_diff (pointer_plus @0 @1) ADDR_EXPR@2)
2924 (with { poly_int64 diff; }
2925 (if (ptr_difference_const (@0, @2, &diff))
2926 (plus { build_int_cst_type (type, diff); } (convert @1)))))
2928 (pointer_diff ADDR_EXPR@0 (pointer_plus @1 @2))
2929 (with { poly_int64 diff; }
2930 (if (ptr_difference_const (@0, @1, &diff))
2931 (minus { build_int_cst_type (type, diff); } (convert @2)))))
2933 /* Canonicalize (T *)(ptr - ptr-cst) to &MEM[ptr + -ptr-cst]. */
2935 (convert (pointer_diff @0 INTEGER_CST@1))
2936 (if (POINTER_TYPE_P (type))
2937 { build_fold_addr_expr_with_type
2938 (build2 (MEM_REF, char_type_node, @0,
2939 wide_int_to_tree (ptr_type_node, wi::neg (wi::to_wide (@1)))),
2942 /* If arg0 is derived from the address of an object or function, we may
2943 be able to fold this expression using the object or function's
2946 (bit_and (convert? @0) INTEGER_CST@1)
2947 (if (POINTER_TYPE_P (TREE_TYPE (@0))
2948 && tree_nop_conversion_p (type, TREE_TYPE (@0)))
2952 unsigned HOST_WIDE_INT bitpos;
2953 get_pointer_alignment_1 (@0, &align, &bitpos);
2955 (if (wi::ltu_p (wi::to_wide (@1), align / BITS_PER_UNIT))
2956 { wide_int_to_tree (type, (wi::to_wide (@1)
2957 & (bitpos / BITS_PER_UNIT))); }))))
2960 uniform_integer_cst_p
2962 tree int_cst = uniform_integer_cst_p (t);
2963 tree inner_type = TREE_TYPE (int_cst);
2965 (if ((INTEGRAL_TYPE_P (inner_type)
2966 || POINTER_TYPE_P (inner_type))
2967 && wi::eq_p (wi::to_wide (int_cst), wi::min_value (inner_type))))))
2970 uniform_integer_cst_p
2972 tree int_cst = uniform_integer_cst_p (t);
2973 tree itype = TREE_TYPE (int_cst);
2975 (if ((INTEGRAL_TYPE_P (itype)
2976 || POINTER_TYPE_P (itype))
2977 && wi::eq_p (wi::to_wide (int_cst), wi::max_value (itype))))))
2979 /* x > y && x != XXX_MIN --> x > y
2980 x > y && x == XXX_MIN --> false . */
2983 (bit_and:c (gt:c@2 @0 @1) (eqne @0 min_value))
2985 (if (eqne == EQ_EXPR)
2986 { constant_boolean_node (false, type); })
2987 (if (eqne == NE_EXPR)
2991 /* x < y && x != XXX_MAX --> x < y
2992 x < y && x == XXX_MAX --> false. */
2995 (bit_and:c (lt:c@2 @0 @1) (eqne @0 max_value))
2997 (if (eqne == EQ_EXPR)
2998 { constant_boolean_node (false, type); })
2999 (if (eqne == NE_EXPR)
3003 /* x <= y && x == XXX_MIN --> x == XXX_MIN. */
3005 (bit_and:c (le:c @0 @1) (eq@2 @0 min_value))
3008 /* x >= y && x == XXX_MAX --> x == XXX_MAX. */
3010 (bit_and:c (ge:c @0 @1) (eq@2 @0 max_value))
3013 /* x > y || x != XXX_MIN --> x != XXX_MIN. */
3015 (bit_ior:c (gt:c @0 @1) (ne@2 @0 min_value))
3018 /* x <= y || x != XXX_MIN --> true. */
3020 (bit_ior:c (le:c @0 @1) (ne @0 min_value))
3021 { constant_boolean_node (true, type); })
3023 /* x <= y || x == XXX_MIN --> x <= y. */
3025 (bit_ior:c (le:c@2 @0 @1) (eq @0 min_value))
3028 /* x < y || x != XXX_MAX --> x != XXX_MAX. */
3030 (bit_ior:c (lt:c @0 @1) (ne@2 @0 max_value))
3033 /* x >= y || x != XXX_MAX --> true
3034 x >= y || x == XXX_MAX --> x >= y. */
3037 (bit_ior:c (ge:c@2 @0 @1) (eqne @0 max_value))
3039 (if (eqne == EQ_EXPR)
3041 (if (eqne == NE_EXPR)
3042 { constant_boolean_node (true, type); }))))
3044 /* y == XXX_MIN || x < y --> x <= y - 1 */
3046 (bit_ior:c (eq:s @1 min_value) (lt:cs @0 @1))
3047 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
3048 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
3049 (le @0 (minus @1 { build_int_cst (TREE_TYPE (@1), 1); }))))
3051 /* y != XXX_MIN && x >= y --> x > y - 1 */
3053 (bit_and:c (ne:s @1 min_value) (ge:cs @0 @1))
3054 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
3055 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
3056 (gt @0 (minus @1 { build_int_cst (TREE_TYPE (@1), 1); }))))
3058 /* Convert (X == CST1) && ((other)X OP2 CST2) to a known value
3059 based on CST1 OP2 CST2. Similarly for (X != CST1). */
3060 /* Convert (X == Y) && (X OP2 Y) to a known value if X is an integral type.
3061 Similarly for (X != Y). */
3064 (for code2 (eq ne lt gt le ge)
3066 (bit_and:c (code1:c@3 @0 @1) (code2:c@4 (convert?@c0 @0) @2))
3067 (if ((TREE_CODE (@1) == INTEGER_CST
3068 && TREE_CODE (@2) == INTEGER_CST)
3069 || ((INTEGRAL_TYPE_P (TREE_TYPE (@1))
3070 || POINTER_TYPE_P (TREE_TYPE (@1)))
3071 && bitwise_equal_p (@1, @2)))
3074 bool one_before = false;
3075 bool one_after = false;
3077 bool allbits = true;
3078 if (TREE_CODE (@1) == INTEGER_CST
3079 && TREE_CODE (@2) == INTEGER_CST)
3081 allbits = TYPE_PRECISION (TREE_TYPE (@1)) <= TYPE_PRECISION (TREE_TYPE (@2));
3082 auto t1 = wi::to_wide (fold_convert (TREE_TYPE (@2), @1));
3083 auto t2 = wi::to_wide (@2);
3084 cmp = wi::cmp (t1, t2, TYPE_SIGN (TREE_TYPE (@2)));
3095 case EQ_EXPR: val = (cmp == 0); break;
3096 case NE_EXPR: val = (cmp != 0); break;
3097 case LT_EXPR: val = (cmp < 0); break;
3098 case GT_EXPR: val = (cmp > 0); break;
3099 case LE_EXPR: val = (cmp <= 0); break;
3100 case GE_EXPR: val = (cmp >= 0); break;
3101 default: gcc_unreachable ();
3105 (if (code1 == EQ_EXPR && val) @3)
3106 (if (code1 == EQ_EXPR && !val) { constant_boolean_node (false, type); })
3107 (if (code1 == NE_EXPR && !val && allbits) @4)
3108 (if (code1 == NE_EXPR
3112 (gt @c0 (convert @1)))
3113 (if (code1 == NE_EXPR
3117 (lt @c0 (convert @1)))
3118 /* (a != (b+1)) & (a > b) -> a > (b+1) */
3119 (if (code1 == NE_EXPR
3123 (gt @c0 (convert @1)))
3124 /* (a != (b-1)) & (a < b) -> a < (b-1) */
3125 (if (code1 == NE_EXPR
3129 (lt @c0 (convert @1)))
3137 /* Convert (X OP1 CST1) && (X OP2 CST2).
3138 Convert (X OP1 Y) && (X OP2 Y). */
3140 (for code1 (lt le gt ge)
3141 (for code2 (lt le gt ge)
3143 (bit_and (code1:c@3 @0 @1) (code2:c@4 @0 @2))
3144 (if ((TREE_CODE (@1) == INTEGER_CST
3145 && TREE_CODE (@2) == INTEGER_CST)
3146 || ((INTEGRAL_TYPE_P (TREE_TYPE (@1))
3147 || POINTER_TYPE_P (TREE_TYPE (@1)))
3148 && operand_equal_p (@1, @2)))
3152 if (TREE_CODE (@1) == INTEGER_CST
3153 && TREE_CODE (@2) == INTEGER_CST)
3154 cmp = tree_int_cst_compare (@1, @2);
3157 /* Choose the more restrictive of two < or <= comparisons. */
3158 (if ((code1 == LT_EXPR || code1 == LE_EXPR)
3159 && (code2 == LT_EXPR || code2 == LE_EXPR))
3160 (if ((cmp < 0) || (cmp == 0 && code1 == LT_EXPR))
3163 /* Likewise chose the more restrictive of two > or >= comparisons. */
3164 (if ((code1 == GT_EXPR || code1 == GE_EXPR)
3165 && (code2 == GT_EXPR || code2 == GE_EXPR))
3166 (if ((cmp > 0) || (cmp == 0 && code1 == GT_EXPR))
3169 /* Check for singleton ranges. */
3171 && ((code1 == LE_EXPR && code2 == GE_EXPR)
3172 || (code1 == GE_EXPR && code2 == LE_EXPR)))
3174 /* Check for disjoint ranges. */
3176 && (code1 == LT_EXPR || code1 == LE_EXPR)
3177 && (code2 == GT_EXPR || code2 == GE_EXPR))
3178 { constant_boolean_node (false, type); })
3180 && (code1 == GT_EXPR || code1 == GE_EXPR)
3181 && (code2 == LT_EXPR || code2 == LE_EXPR))
3182 { constant_boolean_node (false, type); })
3185 /* Convert (X == CST1) || (X OP2 CST2) to a known value
3186 based on CST1 OP2 CST2. Similarly for (X != CST1). */
3187 /* Convert (X == Y) || (X OP2 Y) to a known value if X is an integral type.
3188 Similarly for (X != Y). */
3191 (for code2 (eq ne lt gt le ge)
3193 (bit_ior:c (code1:c@3 @0 @1) (code2:c@4 (convert?@c0 @0) @2))
3194 (if ((TREE_CODE (@1) == INTEGER_CST
3195 && TREE_CODE (@2) == INTEGER_CST)
3196 || ((INTEGRAL_TYPE_P (TREE_TYPE (@1))
3197 || POINTER_TYPE_P (TREE_TYPE (@1)))
3198 && bitwise_equal_p (@1, @2)))
3201 bool one_before = false;
3202 bool one_after = false;
3204 bool allbits = true;
3205 if (TREE_CODE (@1) == INTEGER_CST
3206 && TREE_CODE (@2) == INTEGER_CST)
3208 allbits = TYPE_PRECISION (TREE_TYPE (@1)) <= TYPE_PRECISION (TREE_TYPE (@2));
3209 auto t1 = wi::to_wide (fold_convert (TREE_TYPE (@2), @1));
3210 auto t2 = wi::to_wide (@2);
3211 cmp = wi::cmp (t1, t2, TYPE_SIGN (TREE_TYPE (@2)));
3222 case EQ_EXPR: val = (cmp == 0); break;
3223 case NE_EXPR: val = (cmp != 0); break;
3224 case LT_EXPR: val = (cmp < 0); break;
3225 case GT_EXPR: val = (cmp > 0); break;
3226 case LE_EXPR: val = (cmp <= 0); break;
3227 case GE_EXPR: val = (cmp >= 0); break;
3228 default: gcc_unreachable ();
3232 (if (code1 == EQ_EXPR && val) @4)
3233 (if (code1 == NE_EXPR && val && allbits) { constant_boolean_node (true, type); })
3234 (if (code1 == NE_EXPR && !val && allbits) @3)
3235 (if (code1 == EQ_EXPR
3240 (if (code1 == EQ_EXPR
3245 /* (a == (b-1)) | (a >= b) -> a >= (b-1) */
3246 (if (code1 == EQ_EXPR
3250 (ge @c0 (convert @1)))
3251 /* (a == (b+1)) | (a <= b) -> a <= (b-1) */
3252 (if (code1 == EQ_EXPR
3256 (le @c0 (convert @1)))
3264 /* Convert (X OP1 CST1) || (X OP2 CST2).
3265 Convert (X OP1 Y) || (X OP2 Y). */
3267 (for code1 (lt le gt ge)
3268 (for code2 (lt le gt ge)
3270 (bit_ior (code1@3 @0 @1) (code2@4 @0 @2))
3271 (if ((TREE_CODE (@1) == INTEGER_CST
3272 && TREE_CODE (@2) == INTEGER_CST)
3273 || ((INTEGRAL_TYPE_P (TREE_TYPE (@1))
3274 || POINTER_TYPE_P (TREE_TYPE (@1)))
3275 && operand_equal_p (@1, @2)))
3279 if (TREE_CODE (@1) == INTEGER_CST
3280 && TREE_CODE (@2) == INTEGER_CST)
3281 cmp = tree_int_cst_compare (@1, @2);
3284 /* Choose the more restrictive of two < or <= comparisons. */
3285 (if ((code1 == LT_EXPR || code1 == LE_EXPR)
3286 && (code2 == LT_EXPR || code2 == LE_EXPR))
3287 (if ((cmp < 0) || (cmp == 0 && code1 == LT_EXPR))
3290 /* Likewise chose the more restrictive of two > or >= comparisons. */
3291 (if ((code1 == GT_EXPR || code1 == GE_EXPR)
3292 && (code2 == GT_EXPR || code2 == GE_EXPR))
3293 (if ((cmp > 0) || (cmp == 0 && code1 == GT_EXPR))
3296 /* Check for singleton ranges. */
3298 && ((code1 == LT_EXPR && code2 == GT_EXPR)
3299 || (code1 == GT_EXPR && code2 == LT_EXPR)))
3301 /* Check for disjoint ranges. */
3303 && (code1 == LT_EXPR || code1 == LE_EXPR)
3304 && (code2 == GT_EXPR || code2 == GE_EXPR))
3305 { constant_boolean_node (true, type); })
3307 && (code1 == GT_EXPR || code1 == GE_EXPR)
3308 && (code2 == LT_EXPR || code2 == LE_EXPR))
3309 { constant_boolean_node (true, type); })
3312 /* Optimize (a CMP b) ^ (a CMP b) */
3313 /* Optimize (a CMP b) != (a CMP b) */
3314 (for op (bit_xor ne)
3315 (for cmp1 (lt lt lt le le le)
3316 cmp2 (gt eq ne ge eq ne)
3317 rcmp (ne le gt ne lt ge)
3319 (op:c (cmp1:c @0 @1) (cmp2:c @0 @1))
3320 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
3323 /* Optimize (a CMP b) == (a CMP b) */
3324 (for cmp1 (lt lt lt le le le)
3325 cmp2 (gt eq ne ge eq ne)
3326 rcmp (eq gt le eq ge lt)
3328 (eq:c (cmp1:c @0 @1) (cmp2:c @0 @1))
3329 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
3332 /* We can't reassociate at all for saturating types. */
3333 (if (!TYPE_SATURATING (type))
3335 /* Contract negates. */
3336 /* A + (-B) -> A - B */
3338 (plus:c @0 (convert? (negate @1)))
3339 /* Apply STRIP_NOPS on the negate. */
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 (minus (convert:t1 @0) (convert:t1 @1))))))
3350 /* A - (-B) -> A + B */
3352 (minus @0 (convert? (negate @1)))
3353 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
3354 && !TYPE_OVERFLOW_SANITIZED (type))
3358 if (INTEGRAL_TYPE_P (type)
3359 && TYPE_OVERFLOW_WRAPS (type) != TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
3360 t1 = TYPE_OVERFLOW_WRAPS (type) ? type : TREE_TYPE (@1);
3362 (convert (plus (convert:t1 @0) (convert:t1 @1))))))
3364 Sign-extension is ok except for INT_MIN, which thankfully cannot
3365 happen without overflow. */
3367 (negate (convert (negate @1)))
3368 (if (INTEGRAL_TYPE_P (type)
3369 && (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@1))
3370 || (!TYPE_UNSIGNED (TREE_TYPE (@1))
3371 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))))
3372 && !TYPE_OVERFLOW_SANITIZED (type)
3373 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@1)))
3376 (negate (convert negate_expr_p@1))
3377 (if (SCALAR_FLOAT_TYPE_P (type)
3378 && ((DECIMAL_FLOAT_TYPE_P (type)
3379 == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1))
3380 && TYPE_PRECISION (type) >= TYPE_PRECISION (TREE_TYPE (@1)))
3381 || !HONOR_SIGN_DEPENDENT_ROUNDING (type)))
3382 (convert (negate @1))))
3384 (negate (nop_convert? (negate @1)))
3385 (if (!TYPE_OVERFLOW_SANITIZED (type)
3386 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@1)))
3389 /* We can't reassociate floating-point unless -fassociative-math
3390 or fixed-point plus or minus because of saturation to +-Inf. */
3391 (if ((!FLOAT_TYPE_P (type) || flag_associative_math)
3392 && !FIXED_POINT_TYPE_P (type))
3394 /* Match patterns that allow contracting a plus-minus pair
3395 irrespective of overflow issues. */
3396 /* (A +- B) - A -> +- B */
3397 /* (A +- B) -+ B -> A */
3398 /* A - (A +- B) -> -+ B */
3399 /* A +- (B -+ A) -> +- B */
3401 (minus (nop_convert1? (plus:c (nop_convert2? @0) @1)) @0)
3404 (minus (nop_convert1? (minus (nop_convert2? @0) @1)) @0)
3405 (if (!ANY_INTEGRAL_TYPE_P (type)
3406 || TYPE_OVERFLOW_WRAPS (type))
3407 (negate (view_convert @1))
3408 (view_convert (negate @1))))
3410 (plus:c (nop_convert1? (minus @0 (nop_convert2? @1))) @1)
3413 (minus @0 (nop_convert1? (plus:c (nop_convert2? @0) @1)))
3414 (if (!ANY_INTEGRAL_TYPE_P (type)
3415 || TYPE_OVERFLOW_WRAPS (type))
3416 (negate (view_convert @1))
3417 (view_convert (negate @1))))
3419 (minus @0 (nop_convert1? (minus (nop_convert2? @0) @1)))
3421 /* (A +- B) + (C - A) -> C +- B */
3422 /* (A + B) - (A - C) -> B + C */
3423 /* More cases are handled with comparisons. */
3425 (plus:c (plus:c @0 @1) (minus @2 @0))
3428 (plus:c (minus @0 @1) (minus @2 @0))
3431 (plus:c (pointer_diff @0 @1) (pointer_diff @2 @0))
3432 (if (TYPE_OVERFLOW_UNDEFINED (type)
3433 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0)))
3434 (pointer_diff @2 @1)))
3436 (minus (plus:c @0 @1) (minus @0 @2))
3439 /* (A +- CST1) +- CST2 -> A + CST3
3440 Use view_convert because it is safe for vectors and equivalent for
3442 (for outer_op (plus minus)
3443 (for inner_op (plus minus)
3444 neg_inner_op (minus plus)
3446 (outer_op (nop_convert? (inner_op @0 CONSTANT_CLASS_P@1))
3448 /* If one of the types wraps, use that one. */
3449 (if (!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_WRAPS (type))
3450 /* If all 3 captures are CONSTANT_CLASS_P, punt, as we might recurse
3451 forever if something doesn't simplify into a constant. */
3452 (if (!CONSTANT_CLASS_P (@0))
3453 (if (outer_op == PLUS_EXPR)
3454 (plus (view_convert @0) (inner_op! @2 (view_convert @1)))
3455 (minus (view_convert @0) (neg_inner_op! @2 (view_convert @1)))))
3456 (if (!ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3457 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
3458 (if (outer_op == PLUS_EXPR)
3459 (view_convert (plus @0 (inner_op! (view_convert @2) @1)))
3460 (view_convert (minus @0 (neg_inner_op! (view_convert @2) @1))))
3461 /* If the constant operation overflows we cannot do the transform
3462 directly as we would introduce undefined overflow, for example
3463 with (a - 1) + INT_MIN. */
3464 (if (types_match (type, @0) && !TYPE_OVERFLOW_SANITIZED (type))
3465 (with { tree cst = const_binop (outer_op == inner_op
3466 ? PLUS_EXPR : MINUS_EXPR,
3469 (if (INTEGRAL_TYPE_P (type) && !TREE_OVERFLOW (cst))
3470 (inner_op @0 { cst; } )
3471 /* X+INT_MAX+1 is X-INT_MIN. */
3472 (if (INTEGRAL_TYPE_P (type)
3473 && wi::to_wide (cst) == wi::min_value (type))
3474 (neg_inner_op @0 { wide_int_to_tree (type, wi::to_wide (cst)); })
3475 /* Last resort, use some unsigned type. */
3476 (with { tree utype = unsigned_type_for (type); }
3478 (view_convert (inner_op
3479 (view_convert:utype @0)
3481 { TREE_OVERFLOW (cst)
3482 ? drop_tree_overflow (cst) : cst; })))))))))))))))
3484 /* (CST1 - A) +- CST2 -> CST3 - A */
3485 (for outer_op (plus minus)
3487 (outer_op (nop_convert? (minus CONSTANT_CLASS_P@1 @0)) CONSTANT_CLASS_P@2)
3488 /* If one of the types wraps, use that one. */
3489 (if (!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_WRAPS (type))
3490 /* If all 3 captures are CONSTANT_CLASS_P, punt, as we might recurse
3491 forever if something doesn't simplify into a constant. */
3492 (if (!CONSTANT_CLASS_P (@0))
3493 (minus (outer_op! (view_convert @1) @2) (view_convert @0)))
3494 (if (!ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3495 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
3496 (view_convert (minus (outer_op! @1 (view_convert @2)) @0))
3497 (if (types_match (type, @0) && !TYPE_OVERFLOW_SANITIZED (type))
3498 (with { tree cst = const_binop (outer_op, type, @1, @2); }
3499 (if (cst && !TREE_OVERFLOW (cst))
3500 (minus { cst; } @0))))))))
3502 /* CST1 - (CST2 - A) -> CST3 + A
3503 Use view_convert because it is safe for vectors and equivalent for
3506 (minus CONSTANT_CLASS_P@1 (nop_convert? (minus CONSTANT_CLASS_P@2 @0)))
3507 /* If one of the types wraps, use that one. */
3508 (if (!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_WRAPS (type))
3509 /* If all 3 captures are CONSTANT_CLASS_P, punt, as we might recurse
3510 forever if something doesn't simplify into a constant. */
3511 (if (!CONSTANT_CLASS_P (@0))
3512 (plus (view_convert @0) (minus! @1 (view_convert @2))))
3513 (if (!ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3514 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
3515 (view_convert (plus @0 (minus! (view_convert @1) @2)))
3516 (if (types_match (type, @0) && !TYPE_OVERFLOW_SANITIZED (type))
3517 (with { tree cst = const_binop (MINUS_EXPR, type, @1, @2); }
3518 (if (cst && !TREE_OVERFLOW (cst))
3519 (plus { cst; } @0)))))))
3521 /* ((T)(A)) + CST -> (T)(A + CST) */
3524 (plus (convert:s SSA_NAME@0) INTEGER_CST@1)
3525 (if (TREE_CODE (TREE_TYPE (@0)) == INTEGER_TYPE
3526 && TREE_CODE (type) == INTEGER_TYPE
3527 && TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (@0))
3528 && int_fits_type_p (@1, TREE_TYPE (@0)))
3529 /* Perform binary operation inside the cast if the constant fits
3530 and (A + CST)'s range does not overflow. */
3533 wi::overflow_type min_ovf = wi::OVF_OVERFLOW,
3534 max_ovf = wi::OVF_OVERFLOW;
3535 tree inner_type = TREE_TYPE (@0);
3538 = wide_int::from (wi::to_wide (@1), TYPE_PRECISION (inner_type),
3539 TYPE_SIGN (inner_type));
3542 if (get_global_range_query ()->range_of_expr (vr, @0)
3543 && !vr.varying_p () && !vr.undefined_p ())
3545 wide_int wmin0 = vr.lower_bound ();
3546 wide_int wmax0 = vr.upper_bound ();
3547 wi::add (wmin0, w1, TYPE_SIGN (inner_type), &min_ovf);
3548 wi::add (wmax0, w1, TYPE_SIGN (inner_type), &max_ovf);
3551 (if (min_ovf == wi::OVF_NONE && max_ovf == wi::OVF_NONE)
3552 (convert (plus @0 { wide_int_to_tree (TREE_TYPE (@0), w1); } )))
3556 /* ((T)(A + CST1)) + CST2 -> (T)(A) + (T)CST1 + CST2 */
3558 (for op (plus minus)
3560 (plus (convert:s (op:s @0 INTEGER_CST@1)) INTEGER_CST@2)
3561 (if (TREE_CODE (TREE_TYPE (@0)) == INTEGER_TYPE
3562 && TREE_CODE (type) == INTEGER_TYPE
3563 && TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (@0))
3564 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
3565 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0))
3566 && TYPE_OVERFLOW_WRAPS (type))
3567 (plus (convert @0) (op @2 (convert @1))))))
3570 /* (T)(A) +- (T)(B) -> (T)(A +- B) only when (A +- B) could be simplified
3571 to a simple value. */
3572 (for op (plus minus)
3574 (op (convert @0) (convert @1))
3575 (if (INTEGRAL_TYPE_P (type)
3576 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
3577 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))
3578 && types_match (TREE_TYPE (@0), TREE_TYPE (@1))
3579 && !TYPE_OVERFLOW_TRAPS (type)
3580 && !TYPE_OVERFLOW_SANITIZED (type))
3581 (convert (op! @0 @1)))))
3585 (plus:c (convert? (bit_not @0)) (convert? @0))
3586 (if (!TYPE_OVERFLOW_TRAPS (type))
3587 (convert { build_all_ones_cst (TREE_TYPE (@0)); })))
3591 (plus (convert? (bit_not @0)) integer_each_onep)
3592 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
3593 (negate (convert @0))))
3597 (minus (convert? (negate @0)) integer_each_onep)
3598 (if (!TYPE_OVERFLOW_TRAPS (type)
3599 && TREE_CODE (type) != COMPLEX_TYPE
3600 && tree_nop_conversion_p (type, TREE_TYPE (@0)))
3601 (bit_not (convert @0))))
3605 (minus integer_all_onesp @0)
3606 (if (TREE_CODE (type) != COMPLEX_TYPE)
3609 /* (T)(P + A) - (T)P -> (T) A */
3611 (minus (convert (plus:c @@0 @1))
3613 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
3614 /* For integer types, if A has a smaller type
3615 than T the result depends on the possible
3617 E.g. T=size_t, A=(unsigned)429497295, P>0.
3618 However, if an overflow in P + A would cause
3619 undefined behavior, we can assume that there
3621 || (INTEGRAL_TYPE_P (TREE_TYPE (@1))
3622 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))))
3625 (minus (convert (pointer_plus @@0 @1))
3627 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
3628 /* For pointer types, if the conversion of A to the
3629 final type requires a sign- or zero-extension,
3630 then we have to punt - it is not defined which
3632 || (POINTER_TYPE_P (TREE_TYPE (@0))
3633 && TREE_CODE (@1) == INTEGER_CST
3634 && tree_int_cst_sign_bit (@1) == 0))
3637 (pointer_diff (pointer_plus @@0 @1) @0)
3638 /* The second argument of pointer_plus must be interpreted as signed, and
3639 thus sign-extended if necessary. */
3640 (with { tree stype = signed_type_for (TREE_TYPE (@1)); }
3641 /* Use view_convert instead of convert here, as POINTER_PLUS_EXPR
3642 second arg is unsigned even when we need to consider it as signed,
3643 we don't want to diagnose overflow here. */
3644 (convert (view_convert:stype @1))))
3646 /* (T)P - (T)(P + A) -> -(T) A */
3648 (minus (convert? @0)
3649 (convert (plus:c @@0 @1)))
3650 (if (INTEGRAL_TYPE_P (type)
3651 && TYPE_OVERFLOW_UNDEFINED (type)
3652 /* For integer literals, using an intermediate unsigned type to avoid
3653 an overflow at run time is counter-productive because it introduces
3654 spurious overflows at compile time, in the form of TREE_OVERFLOW on
3655 the result, which may be problematic in GENERIC for some front-ends:
3656 (T)P - (T)(P + 4) -> (T)(-(U)4) -> (T)(4294967292) -> -4(OVF)
3657 so we use the direct path for them. */
3658 && TREE_CODE (@1) != INTEGER_CST
3659 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
3660 (with { tree utype = unsigned_type_for (type); }
3661 (convert (negate (convert:utype @1))))
3662 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
3663 /* For integer types, if A has a smaller type
3664 than T the result depends on the possible
3666 E.g. T=size_t, A=(unsigned)429497295, P>0.
3667 However, if an overflow in P + A would cause
3668 undefined behavior, we can assume that there
3670 || (INTEGRAL_TYPE_P (TREE_TYPE (@1))
3671 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))))
3672 (negate (convert @1)))))
3675 (convert (pointer_plus @@0 @1)))
3676 (if (INTEGRAL_TYPE_P (type)
3677 && TYPE_OVERFLOW_UNDEFINED (type)
3678 /* See above the rationale for this condition. */
3679 && TREE_CODE (@1) != INTEGER_CST
3680 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
3681 (with { tree utype = unsigned_type_for (type); }
3682 (convert (negate (convert:utype @1))))
3683 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
3684 /* For pointer types, if the conversion of A to the
3685 final type requires a sign- or zero-extension,
3686 then we have to punt - it is not defined which
3688 || (POINTER_TYPE_P (TREE_TYPE (@0))
3689 && TREE_CODE (@1) == INTEGER_CST
3690 && tree_int_cst_sign_bit (@1) == 0))
3691 (negate (convert @1)))))
3693 (pointer_diff @0 (pointer_plus @@0 @1))
3694 /* The second argument of pointer_plus must be interpreted as signed, and
3695 thus sign-extended if necessary. */
3696 (with { tree stype = signed_type_for (TREE_TYPE (@1)); }
3697 /* Use view_convert instead of convert here, as POINTER_PLUS_EXPR
3698 second arg is unsigned even when we need to consider it as signed,
3699 we don't want to diagnose overflow here. */
3700 (negate (convert (view_convert:stype @1)))))
3702 /* (T)(P + A) - (T)(P + B) -> (T)A - (T)B */
3704 (minus (convert (plus:c @@0 @1))
3705 (convert (plus:c @0 @2)))
3706 (if (INTEGRAL_TYPE_P (type)
3707 && TYPE_OVERFLOW_UNDEFINED (type)
3708 && element_precision (type) <= element_precision (TREE_TYPE (@1))
3709 && element_precision (type) <= element_precision (TREE_TYPE (@2)))
3710 (with { tree utype = unsigned_type_for (type); }
3711 (convert (minus (convert:utype @1) (convert:utype @2))))
3712 (if (((element_precision (type) <= element_precision (TREE_TYPE (@1)))
3713 == (element_precision (type) <= element_precision (TREE_TYPE (@2))))
3714 && (element_precision (type) <= element_precision (TREE_TYPE (@1))
3715 /* For integer types, if A has a smaller type
3716 than T the result depends on the possible
3718 E.g. T=size_t, A=(unsigned)429497295, P>0.
3719 However, if an overflow in P + A would cause
3720 undefined behavior, we can assume that there
3722 || (INTEGRAL_TYPE_P (TREE_TYPE (@1))
3723 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
3724 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))
3725 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@2)))))
3726 (minus (convert @1) (convert @2)))))
3728 (minus (convert (pointer_plus @@0 @1))
3729 (convert (pointer_plus @0 @2)))
3730 (if (INTEGRAL_TYPE_P (type)
3731 && TYPE_OVERFLOW_UNDEFINED (type)
3732 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
3733 (with { tree utype = unsigned_type_for (type); }
3734 (convert (minus (convert:utype @1) (convert:utype @2))))
3735 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
3736 /* For pointer types, if the conversion of A to the
3737 final type requires a sign- or zero-extension,
3738 then we have to punt - it is not defined which
3740 || (POINTER_TYPE_P (TREE_TYPE (@0))
3741 && TREE_CODE (@1) == INTEGER_CST
3742 && tree_int_cst_sign_bit (@1) == 0
3743 && TREE_CODE (@2) == INTEGER_CST
3744 && tree_int_cst_sign_bit (@2) == 0))
3745 (minus (convert @1) (convert @2)))))
3747 (pointer_diff (pointer_plus @0 @2) (pointer_plus @1 @2))
3748 (pointer_diff @0 @1))
3750 (pointer_diff (pointer_plus @@0 @1) (pointer_plus @0 @2))
3751 /* The second argument of pointer_plus must be interpreted as signed, and
3752 thus sign-extended if necessary. */
3753 (with { tree stype = signed_type_for (TREE_TYPE (@1)); }
3754 /* Use view_convert instead of convert here, as POINTER_PLUS_EXPR
3755 second arg is unsigned even when we need to consider it as signed,
3756 we don't want to diagnose overflow here. */
3757 (minus (convert (view_convert:stype @1))
3758 (convert (view_convert:stype @2)))))))
3760 /* (A * C) +- (B * C) -> (A+-B) * C and (A * C) +- A -> A * (C+-1).
3761 Modeled after fold_plusminus_mult_expr. */
3762 (if (!TYPE_SATURATING (type)
3763 && (!FLOAT_TYPE_P (type) || flag_associative_math))
3764 (for plusminus (plus minus)
3766 (plusminus (mult:cs@3 @0 @1) (mult:cs@4 @0 @2))
3767 (if (!ANY_INTEGRAL_TYPE_P (type)
3768 || TYPE_OVERFLOW_WRAPS (type)
3769 || (INTEGRAL_TYPE_P (type)
3770 && tree_expr_nonzero_p (@0)
3771 && expr_not_equal_to (@0, wi::minus_one (TYPE_PRECISION (type)))))
3772 (if (single_use (@3) || single_use (@4))
3773 /* If @1 +- @2 is constant require a hard single-use on either
3774 original operand (but not on both). */
3775 (mult (plusminus @1 @2) @0)
3776 (mult! (plusminus @1 @2) @0)
3778 /* We cannot generate constant 1 for fract. */
3779 (if (!ALL_FRACT_MODE_P (TYPE_MODE (type)))
3781 (plusminus @0 (mult:c@3 @0 @2))
3782 (if ((!ANY_INTEGRAL_TYPE_P (type)
3783 || TYPE_OVERFLOW_WRAPS (type)
3784 /* For @0 + @0*@2 this transformation would introduce UB
3785 (where there was none before) for @0 in [-1,0] and @2 max.
3786 For @0 - @0*@2 this transformation would introduce UB
3787 for @0 0 and @2 in [min,min+1] or @0 -1 and @2 min+1. */
3788 || (INTEGRAL_TYPE_P (type)
3789 && ((tree_expr_nonzero_p (@0)
3790 && expr_not_equal_to (@0,
3791 wi::minus_one (TYPE_PRECISION (type))))
3792 || (plusminus == PLUS_EXPR
3793 ? expr_not_equal_to (@2,
3794 wi::max_value (TYPE_PRECISION (type), SIGNED))
3795 /* Let's ignore the @0 -1 and @2 min case. */
3796 : (expr_not_equal_to (@2,
3797 wi::min_value (TYPE_PRECISION (type), SIGNED))
3798 && expr_not_equal_to (@2,
3799 wi::min_value (TYPE_PRECISION (type), SIGNED)
3802 (mult (plusminus { build_one_cst (type); } @2) @0)))
3804 (plusminus (mult:c@3 @0 @2) @0)
3805 (if ((!ANY_INTEGRAL_TYPE_P (type)
3806 || TYPE_OVERFLOW_WRAPS (type)
3807 /* For @0*@2 + @0 this transformation would introduce UB
3808 (where there was none before) for @0 in [-1,0] and @2 max.
3809 For @0*@2 - @0 this transformation would introduce UB
3810 for @0 0 and @2 min. */
3811 || (INTEGRAL_TYPE_P (type)
3812 && ((tree_expr_nonzero_p (@0)
3813 && (plusminus == MINUS_EXPR
3814 || expr_not_equal_to (@0,
3815 wi::minus_one (TYPE_PRECISION (type)))))
3816 || expr_not_equal_to (@2,
3817 (plusminus == PLUS_EXPR
3818 ? wi::max_value (TYPE_PRECISION (type), SIGNED)
3819 : wi::min_value (TYPE_PRECISION (type), SIGNED))))))
3821 (mult (plusminus @2 { build_one_cst (type); }) @0))))))
3824 /* Canonicalize X + (X << C) into X * (1 + (1 << C)) and
3825 (X << C1) + (X << C2) into X * ((1 << C1) + (1 << C2)). */
3827 (plus:c @0 (lshift:s @0 INTEGER_CST@1))
3828 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3829 && tree_fits_uhwi_p (@1)
3830 && tree_to_uhwi (@1) < element_precision (type)
3831 && (INTEGRAL_TYPE_P (TREE_TYPE (@0))
3832 || optab_handler (smul_optab,
3833 TYPE_MODE (type)) != CODE_FOR_nothing))
3834 (with { tree t = type;
3835 if (!TYPE_OVERFLOW_WRAPS (t)) t = unsigned_type_for (t);
3836 wide_int w = wi::set_bit_in_zero (tree_to_uhwi (@1),
3837 element_precision (type));
3839 tree cst = wide_int_to_tree (VECTOR_TYPE_P (t) ? TREE_TYPE (t)
3841 cst = build_uniform_cst (t, cst); }
3842 (convert (mult (convert:t @0) { cst; })))))
3844 (plus (lshift:s @0 INTEGER_CST@1) (lshift:s @0 INTEGER_CST@2))
3845 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3846 && tree_fits_uhwi_p (@1)
3847 && tree_to_uhwi (@1) < element_precision (type)
3848 && tree_fits_uhwi_p (@2)
3849 && tree_to_uhwi (@2) < element_precision (type)
3850 && (INTEGRAL_TYPE_P (TREE_TYPE (@0))
3851 || optab_handler (smul_optab,
3852 TYPE_MODE (type)) != CODE_FOR_nothing))
3853 (with { tree t = type;
3854 if (!TYPE_OVERFLOW_WRAPS (t)) t = unsigned_type_for (t);
3855 unsigned int prec = element_precision (type);
3856 wide_int w = wi::set_bit_in_zero (tree_to_uhwi (@1), prec);
3857 w += wi::set_bit_in_zero (tree_to_uhwi (@2), prec);
3858 tree cst = wide_int_to_tree (VECTOR_TYPE_P (t) ? TREE_TYPE (t)
3860 cst = build_uniform_cst (t, cst); }
3861 (convert (mult (convert:t @0) { cst; })))))
3864 /* Canonicalize (X*C1)|(X*C2) and (X*C1)^(X*C2) to (C1+C2)*X when
3865 tree_nonzero_bits allows IOR and XOR to be treated like PLUS.
3866 Likewise, handle (X<<C3) and X as legitimate variants of X*C. */
3867 (for op (bit_ior bit_xor)
3869 (op (mult:s@0 @1 INTEGER_CST@2)
3870 (mult:s@3 @1 INTEGER_CST@4))
3871 (if (INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_WRAPS (type)
3872 && (tree_nonzero_bits (@0) & tree_nonzero_bits (@3)) == 0)
3874 { wide_int_to_tree (type, wi::to_wide (@2) + wi::to_wide (@4)); })))
3876 (op:c (mult:s@0 @1 INTEGER_CST@2)
3877 (lshift:s@3 @1 INTEGER_CST@4))
3878 (if (INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_WRAPS (type)
3879 && tree_int_cst_sgn (@4) > 0
3880 && (tree_nonzero_bits (@0) & tree_nonzero_bits (@3)) == 0)
3881 (with { wide_int wone = wi::one (TYPE_PRECISION (type));
3882 wide_int c = wi::add (wi::to_wide (@2),
3883 wi::lshift (wone, wi::to_wide (@4))); }
3884 (mult @1 { wide_int_to_tree (type, c); }))))
3886 (op:c (mult:s@0 @1 INTEGER_CST@2)
3888 (if (INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_WRAPS (type)
3889 && (tree_nonzero_bits (@0) & tree_nonzero_bits (@1)) == 0)
3891 { wide_int_to_tree (type,
3892 wi::add (wi::to_wide (@2), 1)); })))
3894 (op (lshift:s@0 @1 INTEGER_CST@2)
3895 (lshift:s@3 @1 INTEGER_CST@4))
3896 (if (INTEGRAL_TYPE_P (type)
3897 && tree_int_cst_sgn (@2) > 0
3898 && tree_int_cst_sgn (@4) > 0
3899 && (tree_nonzero_bits (@0) & tree_nonzero_bits (@3)) == 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)),
3905 wi::lshift (wone, wi::to_wide (@4))); }
3906 (convert (mult:t (convert:t @1) { wide_int_to_tree (t,c); })))))
3908 (op:c (lshift:s@0 @1 INTEGER_CST@2)
3910 (if (INTEGRAL_TYPE_P (type)
3911 && tree_int_cst_sgn (@2) > 0
3912 && (tree_nonzero_bits (@0) & tree_nonzero_bits (@1)) == 0)
3913 (with { tree t = type;
3914 if (!TYPE_OVERFLOW_WRAPS (t))
3915 t = unsigned_type_for (t);
3916 wide_int wone = wi::one (TYPE_PRECISION (t));
3917 wide_int c = wi::add (wi::lshift (wone, wi::to_wide (@2)), wone); }
3918 (convert (mult:t (convert:t @1) { wide_int_to_tree (t, c); }))))))
3920 /* Simplifications of MIN_EXPR, MAX_EXPR, fmin() and fmax(). */
3922 (for minmax (min max)
3926 /* max(max(x,y),x) -> max(x,y) */
3928 (minmax:c (minmax:c@2 @0 @1) @0)
3930 /* For fmin() and fmax(), skip folding when both are sNaN. */
3931 (for minmax (FMIN_ALL FMAX_ALL)
3934 (if (!tree_expr_maybe_signaling_nan_p (@0))
3936 /* min(max(x,y),y) -> y. */
3938 (min:c (max:c @0 @1) @1)
3940 /* max(min(x,y),y) -> y. */
3942 (max:c (min:c @0 @1) @1)
3944 /* max(a,-a) -> abs(a). */
3946 (max:c @0 (negate @0))
3947 (if (TREE_CODE (type) != COMPLEX_TYPE
3948 && (! ANY_INTEGRAL_TYPE_P (type)
3949 || TYPE_OVERFLOW_UNDEFINED (type)))
3951 /* min(a,-a) -> -abs(a). */
3953 (min:c @0 (negate @0))
3954 (if (TREE_CODE (type) != COMPLEX_TYPE
3955 && (! ANY_INTEGRAL_TYPE_P (type)
3956 || TYPE_OVERFLOW_UNDEFINED (type)))
3961 (if (INTEGRAL_TYPE_P (type)
3962 && TYPE_MIN_VALUE (type)
3963 && operand_equal_p (@1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
3965 (if (INTEGRAL_TYPE_P (type)
3966 && TYPE_MAX_VALUE (type)
3967 && operand_equal_p (@1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
3972 (if (INTEGRAL_TYPE_P (type)
3973 && TYPE_MAX_VALUE (type)
3974 && operand_equal_p (@1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
3976 (if (INTEGRAL_TYPE_P (type)
3977 && TYPE_MIN_VALUE (type)
3978 && operand_equal_p (@1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
3981 /* max (a, a + CST) -> a + CST where CST is positive. */
3982 /* max (a, a + CST) -> a where CST is negative. */
3984 (max:c @0 (plus@2 @0 INTEGER_CST@1))
3985 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
3986 (if (tree_int_cst_sgn (@1) > 0)
3990 /* min (a, a + CST) -> a where CST is positive. */
3991 /* min (a, a + CST) -> a + CST where CST is negative. */
3993 (min:c @0 (plus@2 @0 INTEGER_CST@1))
3994 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
3995 (if (tree_int_cst_sgn (@1) > 0)
3999 /* Simplify min (&var[off0], &var[off1]) etc. depending on whether
4000 the addresses are known to be less, equal or greater. */
4001 (for minmax (min max)
4004 (minmax (convert1?@2 addr@0) (convert2?@3 addr@1))
4007 poly_int64 off0, off1;
4009 int equal = address_compare (cmp, TREE_TYPE (@2), @0, @1, base0, base1,
4010 off0, off1, GENERIC);
4013 (if (minmax == MIN_EXPR)
4014 (if (known_le (off0, off1))
4016 (if (known_gt (off0, off1))
4018 (if (known_ge (off0, off1))
4020 (if (known_lt (off0, off1))
4023 /* (convert (minmax ((convert (x) c)))) -> minmax (x c) if x is promoted
4024 and the outer convert demotes the expression back to x's type. */
4025 (for minmax (min max)
4027 (convert (minmax@0 (convert @1) INTEGER_CST@2))
4028 (if (INTEGRAL_TYPE_P (type)
4029 && types_match (@1, type) && int_fits_type_p (@2, type)
4030 && TYPE_SIGN (TREE_TYPE (@0)) == TYPE_SIGN (type)
4031 && TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type))
4032 (minmax @1 (convert @2)))))
4034 (for minmax (FMIN_ALL FMAX_ALL)
4035 /* If either argument is NaN and other one is not sNaN, return the other
4036 one. Avoid the transformation if we get (and honor) a signalling NaN. */
4038 (minmax:c @0 REAL_CST@1)
4039 (if (real_isnan (TREE_REAL_CST_PTR (@1))
4040 && (!HONOR_SNANS (@1) || !TREE_REAL_CST (@1).signalling)
4041 && !tree_expr_maybe_signaling_nan_p (@0))
4043 /* Convert fmin/fmax to MIN_EXPR/MAX_EXPR. C99 requires these
4044 functions to return the numeric arg if the other one is NaN.
4045 MIN and MAX don't honor that, so only transform if -ffinite-math-only
4046 is set. C99 doesn't require -0.0 to be handled, so we don't have to
4047 worry about it either. */
4048 (if (flag_finite_math_only)
4055 /* min (-A, -B) -> -max (A, B) */
4056 (for minmax (min max FMIN_ALL FMAX_ALL)
4057 maxmin (max min FMAX_ALL FMIN_ALL)
4059 (minmax (negate:s@2 @0) (negate:s@3 @1))
4060 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
4061 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
4062 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
4063 (negate (maxmin @0 @1)))))
4064 /* MIN (~X, ~Y) -> ~MAX (X, Y)
4065 MAX (~X, ~Y) -> ~MIN (X, Y) */
4066 (for minmax (min max)
4069 (minmax (bit_not:s@2 @0) (bit_not:s@3 @1))
4070 (bit_not (maxmin @0 @1)))
4071 /* ~MAX(~X, Y) --> MIN(X, ~Y) */
4072 /* ~MIN(~X, Y) --> MAX(X, ~Y) */
4074 (bit_not (minmax:cs (bit_not @0) @1))
4075 (maxmin @0 (bit_not @1))))
4077 /* MIN (X, Y) == X -> X <= Y */
4078 /* MIN (X, Y) < X -> X > Y */
4079 /* MIN (X, Y) >= X -> X <= Y */
4080 (for minmax (min min min min max max max max)
4081 cmp (eq ne lt ge eq ne gt le )
4082 out (le gt gt le ge lt lt ge )
4084 (cmp:c (minmax:c @0 @1) @0)
4085 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0)))
4087 /* MIN (X, 5) == 0 -> X == 0
4088 MIN (X, 5) == 7 -> false */
4091 (cmp (min @0 INTEGER_CST@1) INTEGER_CST@2)
4092 (if (wi::lt_p (wi::to_wide (@1), wi::to_wide (@2),
4093 TYPE_SIGN (TREE_TYPE (@0))))
4094 { constant_boolean_node (cmp == NE_EXPR, type); }
4095 (if (wi::gt_p (wi::to_wide (@1), wi::to_wide (@2),
4096 TYPE_SIGN (TREE_TYPE (@0))))
4100 (cmp (max @0 INTEGER_CST@1) INTEGER_CST@2)
4101 (if (wi::gt_p (wi::to_wide (@1), wi::to_wide (@2),
4102 TYPE_SIGN (TREE_TYPE (@0))))
4103 { constant_boolean_node (cmp == NE_EXPR, type); }
4104 (if (wi::lt_p (wi::to_wide (@1), wi::to_wide (@2),
4105 TYPE_SIGN (TREE_TYPE (@0))))
4108 /* X <= MAX(X, Y) -> true
4109 X > MAX(X, Y) -> false
4110 X >= MIN(X, Y) -> true
4111 X < MIN(X, Y) -> false */
4112 (for minmax (min min max max )
4115 (cmp:c @0 (minmax:c @0 @1))
4116 { constant_boolean_node (cmp == GE_EXPR || cmp == LE_EXPR, type); } ))
4118 /* MIN (X, C1) < C2 -> X < C2 || C1 < C2 */
4119 (for minmax (min min max max min min max max )
4120 cmp (lt le gt ge gt ge lt le )
4121 comb (bit_ior bit_ior bit_ior bit_ior bit_and bit_and bit_and bit_and)
4123 (cmp (minmax @0 INTEGER_CST@1) INTEGER_CST@2)
4124 (comb (cmp @0 @2) (cmp @1 @2))))
4126 /* Undo fancy ways of writing max/min or other ?: expressions, like
4127 a - ((a - b) & -(a < b)) and a - (a - b) * (a < b) into (a < b) ? b : a.
4128 People normally use ?: and that is what we actually try to optimize. */
4129 /* Transform A + (B-A)*cmp into cmp ? B : A. */
4131 (plus:c @0 (mult:c (minus @1 @0) zero_one_valued_p@2))
4132 (if (INTEGRAL_TYPE_P (type)
4133 && (GIMPLE || !TREE_SIDE_EFFECTS (@1)))
4134 (cond (convert:boolean_type_node @2) @1 @0)))
4135 /* Transform A - (A-B)*cmp into cmp ? B : A. */
4137 (minus @0 (mult:c (minus @0 @1) zero_one_valued_p@2))
4138 (if (INTEGRAL_TYPE_P (type)
4139 && (GIMPLE || !TREE_SIDE_EFFECTS (@1)))
4140 (cond (convert:boolean_type_node @2) @1 @0)))
4141 /* Transform A ^ (A^B)*cmp into cmp ? B : A. */
4143 (bit_xor:c @0 (mult:c (bit_xor:c @0 @1) zero_one_valued_p@2))
4144 (if (INTEGRAL_TYPE_P (type)
4145 && (GIMPLE || !TREE_SIDE_EFFECTS (@1)))
4146 (cond (convert:boolean_type_node @2) @1 @0)))
4148 /* (x <= 0 ? -x : 0) -> max(-x, 0). */
4150 (cond (le @0 integer_zerop@1) (negate@2 @0) integer_zerop@1)
4153 /* (zero_one == 0) ? y : z <op> y -> ((typeof(y))zero_one * z) <op> y */
4154 (for op (bit_xor bit_ior plus)
4156 (cond (eq zero_one_valued_p@0
4160 (if (INTEGRAL_TYPE_P (type)
4161 && TYPE_PRECISION (type) > 1
4162 && (INTEGRAL_TYPE_P (TREE_TYPE (@0))))
4163 (op (mult (convert:type @0) @2) @1))))
4165 /* (zero_one != 0) ? z <op> y : y -> ((typeof(y))zero_one * z) <op> y */
4166 (for op (bit_xor bit_ior plus)
4168 (cond (ne zero_one_valued_p@0
4172 (if (INTEGRAL_TYPE_P (type)
4173 && TYPE_PRECISION (type) > 1
4174 && (INTEGRAL_TYPE_P (TREE_TYPE (@0))))
4175 (op (mult (convert:type @0) @2) @1))))
4177 /* ?: Value replacement. */
4178 /* a == 0 ? b : b + a -> b + a */
4179 (for op (plus bit_ior bit_xor)
4181 (cond (eq @0 integer_zerop) @1 (op:c@2 @1 @0))
4183 /* a == 0 ? b : b - a -> b - a */
4184 /* a == 0 ? b : b ptr+ a -> b ptr+ a */
4185 /* a == 0 ? b : b shift/rotate a -> b shift/rotate a */
4186 (for op (lrotate rrotate lshift rshift minus pointer_plus)
4188 (cond (eq @0 integer_zerop) @1 (op@2 @1 @0))
4191 /* a == 1 ? b : b / a -> b / a */
4192 (for op (trunc_div ceil_div floor_div round_div exact_div)
4194 (cond (eq @0 integer_onep) @1 (op@2 @1 @0))
4197 /* a == 1 ? b : a * b -> a * b */
4200 (cond (eq @0 integer_onep) @1 (op:c@2 @1 @0))
4203 /* a == -1 ? b : a & b -> a & b */
4206 (cond (eq @0 integer_all_onesp) @1 (op:c@2 @1 @0))
4209 /* Simplifications of shift and rotates. */
4211 (for rotate (lrotate rrotate)
4213 (rotate integer_all_onesp@0 @1)
4216 /* Optimize -1 >> x for arithmetic right shifts. */
4218 (rshift integer_all_onesp@0 @1)
4219 (if (!TYPE_UNSIGNED (type))
4222 /* Optimize (x >> c) << c into x & (-1<<c). */
4224 (lshift (nop_convert? (rshift @0 INTEGER_CST@1)) @1)
4225 (if (wi::ltu_p (wi::to_wide (@1), element_precision (type)))
4226 /* It doesn't matter if the right shift is arithmetic or logical. */
4227 (bit_and (view_convert @0) (lshift { build_minus_one_cst (type); } @1))))
4230 (lshift (convert (convert@2 (rshift @0 INTEGER_CST@1))) @1)
4231 (if (wi::ltu_p (wi::to_wide (@1), element_precision (type))
4232 /* Allow intermediate conversion to integral type with whatever sign, as
4233 long as the low TYPE_PRECISION (type)
4234 - TYPE_PRECISION (TREE_TYPE (@2)) bits are preserved. */
4235 && INTEGRAL_TYPE_P (type)
4236 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
4237 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
4238 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (@0))
4239 && (TYPE_PRECISION (TREE_TYPE (@2)) >= TYPE_PRECISION (type)
4240 || wi::geu_p (wi::to_wide (@1),
4241 TYPE_PRECISION (type)
4242 - TYPE_PRECISION (TREE_TYPE (@2)))))
4243 (bit_and (convert @0) (lshift { build_minus_one_cst (type); } @1))))
4245 /* For (x << c) >> c, optimize into x & ((unsigned)-1 >> c) for
4246 unsigned x OR truncate into the precision(type) - c lowest bits
4247 of signed x (if they have mode precision or a precision of 1). */
4249 (rshift (nop_convert? (lshift @0 INTEGER_CST@1)) @@1)
4250 (if (wi::ltu_p (wi::to_wide (@1), element_precision (type)))
4251 (if (TYPE_UNSIGNED (type))
4252 (bit_and (convert @0) (rshift { build_minus_one_cst (type); } @1))
4253 (if (INTEGRAL_TYPE_P (type))
4255 int width = element_precision (type) - tree_to_uhwi (@1);
4256 tree stype = NULL_TREE;
4257 if (width <= MAX_FIXED_MODE_SIZE)
4258 stype = build_nonstandard_integer_type (width, 0);
4260 (if (stype && (width == 1 || type_has_mode_precision_p (stype)))
4261 (convert (convert:stype @0))))))))
4263 /* Optimize x >> x into 0 */
4266 { build_zero_cst (type); })
4268 (for shiftrotate (lrotate rrotate lshift rshift)
4270 (shiftrotate @0 integer_zerop)
4273 (shiftrotate integer_zerop@0 @1)
4275 /* Prefer vector1 << scalar to vector1 << vector2
4276 if vector2 is uniform. */
4277 (for vec (VECTOR_CST CONSTRUCTOR)
4279 (shiftrotate @0 vec@1)
4280 (with { tree tem = uniform_vector_p (@1); }
4282 (shiftrotate @0 { tem; }))))))
4284 /* Simplify X << Y where Y's low width bits are 0 to X, as only valid
4285 Y is 0. Similarly for X >> Y. */
4287 (for shift (lshift rshift)
4289 (shift @0 SSA_NAME@1)
4290 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1)))
4292 int width = ceil_log2 (element_precision (TREE_TYPE (@0)));
4293 int prec = TYPE_PRECISION (TREE_TYPE (@1));
4295 (if ((get_nonzero_bits (@1) & wi::mask (width, false, prec)) == 0)
4299 /* Rewrite an LROTATE_EXPR by a constant into an
4300 RROTATE_EXPR by a new constant. */
4302 (lrotate @0 INTEGER_CST@1)
4303 (rrotate @0 { const_binop (MINUS_EXPR, TREE_TYPE (@1),
4304 build_int_cst (TREE_TYPE (@1),
4305 element_precision (type)), @1); }))
4307 /* Turn (a OP c1) OP c2 into a OP (c1+c2). */
4308 (for op (lrotate rrotate rshift lshift)
4310 (op (op @0 INTEGER_CST@1) INTEGER_CST@2)
4311 (with { unsigned int prec = element_precision (type); }
4312 (if (wi::ge_p (wi::to_wide (@1), 0, TYPE_SIGN (TREE_TYPE (@1)))
4313 && wi::lt_p (wi::to_wide (@1), prec, TYPE_SIGN (TREE_TYPE (@1)))
4314 && wi::ge_p (wi::to_wide (@2), 0, TYPE_SIGN (TREE_TYPE (@2)))
4315 && wi::lt_p (wi::to_wide (@2), prec, TYPE_SIGN (TREE_TYPE (@2))))
4316 (with { unsigned int low = (tree_to_uhwi (@1)
4317 + tree_to_uhwi (@2)); }
4318 /* Deal with a OP (c1 + c2) being undefined but (a OP c1) OP c2
4319 being well defined. */
4321 (if (op == LROTATE_EXPR || op == RROTATE_EXPR)
4322 (op @0 { build_int_cst (TREE_TYPE (@1), low % prec); })
4323 (if (TYPE_UNSIGNED (type) || op == LSHIFT_EXPR)
4324 { build_zero_cst (type); }
4325 (op @0 { build_int_cst (TREE_TYPE (@1), prec - 1); })))
4326 (op @0 { build_int_cst (TREE_TYPE (@1), low); })))))))
4329 /* Simplify (CST << x) & 1 to 0 if CST is even or to x == 0 if it is odd. */
4331 (bit_and (lshift INTEGER_CST@1 @0) integer_onep)
4332 (if ((wi::to_wide (@1) & 1) != 0)
4333 (convert (eq:boolean_type_node @0 { build_zero_cst (TREE_TYPE (@0)); }))
4334 { build_zero_cst (type); }))
4336 /* Simplify ((C << x) & D) != 0 where C and D are power of two constants,
4337 either to false if D is smaller (unsigned comparison) than C, or to
4338 x == log2 (D) - log2 (C). Similarly for right shifts.
4339 Note for `(1 >> x)`, the & 1 has been removed so matching that seperately. */
4343 (cmp (bit_and (lshift integer_pow2p@1 @0) integer_pow2p@2) integer_zerop)
4344 (with { int c1 = wi::clz (wi::to_wide (@1));
4345 int c2 = wi::clz (wi::to_wide (@2)); }
4347 { constant_boolean_node (cmp == NE_EXPR ? false : true, type); }
4348 (icmp @0 { build_int_cst (TREE_TYPE (@0), c1 - c2); }))))
4350 (cmp (bit_and (rshift integer_pow2p@1 @0) integer_pow2p@2) integer_zerop)
4351 (if (tree_int_cst_sgn (@1) > 0)
4352 (with { int c1 = wi::clz (wi::to_wide (@1));
4353 int c2 = wi::clz (wi::to_wide (@2)); }
4355 { constant_boolean_node (cmp == NE_EXPR ? false : true, type); }
4356 (icmp @0 { build_int_cst (TREE_TYPE (@0), c2 - c1); })))))
4357 /* `(1 >> X) != 0` -> `X == 0` */
4358 /* `(1 >> X) == 0` -> `X != 0` */
4360 (cmp (rshift integer_onep@1 @0) integer_zerop)
4361 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1)))
4362 (icmp @0 { build_zero_cst (TREE_TYPE (@0)); }))))
4364 /* (CST1 << A) == CST2 -> A == ctz (CST2) - ctz (CST1)
4365 (CST1 << A) != CST2 -> A != ctz (CST2) - ctz (CST1)
4369 (cmp (lshift INTEGER_CST@0 @1) INTEGER_CST@2)
4370 (with { int cand = wi::ctz (wi::to_wide (@2)) - wi::ctz (wi::to_wide (@0)); }
4372 || (!integer_zerop (@2)
4373 && wi::lshift (wi::to_wide (@0), cand) != wi::to_wide (@2)))
4374 { constant_boolean_node (cmp == NE_EXPR, type); }
4375 (if (!integer_zerop (@2)
4376 && wi::lshift (wi::to_wide (@0), cand) == wi::to_wide (@2))
4377 (cmp @1 { build_int_cst (TREE_TYPE (@1), cand); }))))))
4379 /* Fold ((X << C1) & C2) cmp C3 into (X & (C2 >> C1)) cmp (C3 >> C1)
4380 ((X >> C1) & C2) cmp C3 into (X & (C2 << C1)) cmp (C3 << C1). */
4383 (cmp (bit_and:s (lshift:s @0 INTEGER_CST@1) INTEGER_CST@2) INTEGER_CST@3)
4384 (if (tree_fits_shwi_p (@1)
4385 && tree_to_shwi (@1) > 0
4386 && tree_to_shwi (@1) < TYPE_PRECISION (TREE_TYPE (@0)))
4387 (if (tree_to_shwi (@1) > wi::ctz (wi::to_wide (@3)))
4388 { constant_boolean_node (cmp == NE_EXPR, type); }
4389 (with { wide_int c1 = wi::to_wide (@1);
4390 wide_int c2 = wi::lrshift (wi::to_wide (@2), c1);
4391 wide_int c3 = wi::lrshift (wi::to_wide (@3), c1); }
4392 (cmp (bit_and @0 { wide_int_to_tree (TREE_TYPE (@0), c2); })
4393 { wide_int_to_tree (TREE_TYPE (@0), c3); })))))
4395 (cmp (bit_and:s (rshift:s @0 INTEGER_CST@1) INTEGER_CST@2) INTEGER_CST@3)
4396 (if (tree_fits_shwi_p (@1)
4397 && tree_to_shwi (@1) > 0
4398 && tree_to_shwi (@1) < TYPE_PRECISION (TREE_TYPE (@0)))
4399 (with { tree t0 = TREE_TYPE (@0);
4400 unsigned int prec = TYPE_PRECISION (t0);
4401 wide_int c1 = wi::to_wide (@1);
4402 wide_int c2 = wi::to_wide (@2);
4403 wide_int c3 = wi::to_wide (@3);
4404 wide_int sb = wi::set_bit_in_zero (prec - 1, prec); }
4405 (if ((c2 & c3) != c3)
4406 { constant_boolean_node (cmp == NE_EXPR, type); }
4407 (if (TYPE_UNSIGNED (t0))
4408 (if ((c3 & wi::arshift (sb, c1 - 1)) != 0)
4409 { constant_boolean_node (cmp == NE_EXPR, type); }
4410 (cmp (bit_and @0 { wide_int_to_tree (t0, c2 << c1); })
4411 { wide_int_to_tree (t0, c3 << c1); }))
4412 (with { wide_int smask = wi::arshift (sb, c1); }
4414 (if ((c2 & smask) == 0)
4415 (cmp (bit_and @0 { wide_int_to_tree (t0, c2 << c1); })
4416 { wide_int_to_tree (t0, c3 << c1); }))
4417 (if ((c3 & smask) == 0)
4418 (cmp (bit_and @0 { wide_int_to_tree (t0, (c2 << c1) | sb); })
4419 { wide_int_to_tree (t0, c3 << c1); }))
4420 (if ((c2 & smask) != (c3 & smask))
4421 { constant_boolean_node (cmp == NE_EXPR, type); })
4422 (cmp (bit_and @0 { wide_int_to_tree (t0, (c2 << c1) | sb); })
4423 { wide_int_to_tree (t0, (c3 << c1) | sb); })))))))))
4425 /* Fold (X << C1) & C2 into (X << C1) & (C2 | ((1 << C1) - 1))
4426 (X >> C1) & C2 into (X >> C1) & (C2 | ~((type) -1 >> C1))
4427 if the new mask might be further optimized. */
4428 (for shift (lshift rshift)
4430 (bit_and (convert?:s@4 (shift:s@5 (convert1?@3 @0) INTEGER_CST@1))
4432 (if (tree_nop_conversion_p (TREE_TYPE (@4), TREE_TYPE (@5))
4433 && TYPE_PRECISION (type) <= HOST_BITS_PER_WIDE_INT
4434 && tree_fits_uhwi_p (@1)
4435 && tree_to_uhwi (@1) > 0
4436 && tree_to_uhwi (@1) < TYPE_PRECISION (type))
4439 unsigned int shiftc = tree_to_uhwi (@1);
4440 unsigned HOST_WIDE_INT mask = TREE_INT_CST_LOW (@2);
4441 unsigned HOST_WIDE_INT newmask, zerobits = 0;
4442 tree shift_type = TREE_TYPE (@3);
4445 if (shift == LSHIFT_EXPR)
4446 zerobits = ((HOST_WIDE_INT_1U << shiftc) - 1);
4447 else if (shift == RSHIFT_EXPR
4448 && type_has_mode_precision_p (shift_type))
4450 prec = TYPE_PRECISION (TREE_TYPE (@3));
4452 /* See if more bits can be proven as zero because of
4455 && TYPE_UNSIGNED (TREE_TYPE (@0)))
4457 tree inner_type = TREE_TYPE (@0);
4458 if (type_has_mode_precision_p (inner_type)
4459 && TYPE_PRECISION (inner_type) < prec)
4461 prec = TYPE_PRECISION (inner_type);
4462 /* See if we can shorten the right shift. */
4464 shift_type = inner_type;
4465 /* Otherwise X >> C1 is all zeros, so we'll optimize
4466 it into (X, 0) later on by making sure zerobits
4470 zerobits = HOST_WIDE_INT_M1U;
4473 zerobits >>= HOST_BITS_PER_WIDE_INT - shiftc;
4474 zerobits <<= prec - shiftc;
4476 /* For arithmetic shift if sign bit could be set, zerobits
4477 can contain actually sign bits, so no transformation is
4478 possible, unless MASK masks them all away. In that
4479 case the shift needs to be converted into logical shift. */
4480 if (!TYPE_UNSIGNED (TREE_TYPE (@3))
4481 && prec == TYPE_PRECISION (TREE_TYPE (@3)))
4483 if ((mask & zerobits) == 0)
4484 shift_type = unsigned_type_for (TREE_TYPE (@3));
4490 /* ((X << 16) & 0xff00) is (X, 0). */
4491 (if ((mask & zerobits) == mask)
4492 { build_int_cst (type, 0); }
4493 (with { newmask = mask | zerobits; }
4494 (if (newmask != mask && (newmask & (newmask + 1)) == 0)
4497 /* Only do the transformation if NEWMASK is some integer
4499 for (prec = BITS_PER_UNIT;
4500 prec < HOST_BITS_PER_WIDE_INT; prec <<= 1)
4501 if (newmask == (HOST_WIDE_INT_1U << prec) - 1)
4504 (if (prec < HOST_BITS_PER_WIDE_INT
4505 || newmask == HOST_WIDE_INT_M1U)
4507 { tree newmaskt = build_int_cst_type (TREE_TYPE (@2), newmask); }
4508 (if (!tree_int_cst_equal (newmaskt, @2))
4509 (if (shift_type != TREE_TYPE (@3))
4510 (bit_and (convert (shift:shift_type (convert @3) @1)) { newmaskt; })
4511 (bit_and @4 { newmaskt; })))))))))))))
4513 /* ((1 << n) & M) != 0 -> n == log2 (M) */
4519 (nop_convert? (lshift integer_onep @0)) integer_pow2p@1) integer_zerop)
4520 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)))
4521 (icmp @0 { wide_int_to_tree (TREE_TYPE (@0),
4522 wi::exact_log2 (wi::to_wide (@1))); }))))
4524 /* Fold (X {&,^,|} C2) << C1 into (X << C1) {&,^,|} (C2 << C1)
4525 (X {&,^,|} C2) >> C1 into (X >> C1) & (C2 >> C1). */
4526 (for shift (lshift rshift)
4527 (for bit_op (bit_and bit_xor bit_ior)
4529 (shift (convert?:s (bit_op:s @0 INTEGER_CST@2)) INTEGER_CST@1)
4530 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
4531 (with { tree mask = int_const_binop (shift, fold_convert (type, @2), @1); }
4533 (bit_op (shift (convert @0) @1) { mask; })))))))
4535 /* ~(~X >> Y) -> X >> Y (for arithmetic shift). */
4537 (bit_not (convert1?:s (rshift:s (convert2?@0 (bit_not @1)) @2)))
4538 (if (!TYPE_UNSIGNED (TREE_TYPE (@0))
4539 && (element_precision (TREE_TYPE (@0))
4540 <= element_precision (TREE_TYPE (@1))
4541 || !TYPE_UNSIGNED (TREE_TYPE (@1))))
4543 { tree shift_type = TREE_TYPE (@0); }
4544 (convert (rshift (convert:shift_type @1) @2)))))
4546 /* ~(~X >>r Y) -> X >>r Y
4547 ~(~X <<r Y) -> X <<r Y */
4548 (for rotate (lrotate rrotate)
4550 (bit_not (convert1?:s (rotate:s (convert2?@0 (bit_not @1)) @2)))
4551 (if ((element_precision (TREE_TYPE (@0))
4552 <= element_precision (TREE_TYPE (@1))
4553 || !TYPE_UNSIGNED (TREE_TYPE (@1)))
4554 && (element_precision (type) <= element_precision (TREE_TYPE (@0))
4555 || !TYPE_UNSIGNED (TREE_TYPE (@0))))
4557 { tree rotate_type = TREE_TYPE (@0); }
4558 (convert (rotate (convert:rotate_type @1) @2))))))
4561 (for rotate (lrotate rrotate)
4562 invrot (rrotate lrotate)
4563 /* (X >>r Y) cmp (Z >>r Y) may simplify to X cmp Y. */
4565 (cmp (rotate @1 @0) (rotate @2 @0))
4567 /* (X >>r C1) cmp C2 may simplify to X cmp C3. */
4569 (cmp (rotate @0 INTEGER_CST@1) INTEGER_CST@2)
4570 (cmp @0 { const_binop (invrot, TREE_TYPE (@0), @2, @1); }))
4571 /* (X >>r Y) cmp C where C is 0 or ~0, may simplify to X cmp C. */
4573 (cmp (rotate @0 @1) INTEGER_CST@2)
4574 (if (integer_zerop (@2) || integer_all_onesp (@2))
4577 /* Narrow a lshift by constant. */
4579 (convert (lshift:s@0 @1 INTEGER_CST@2))
4580 (if (INTEGRAL_TYPE_P (type)
4581 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
4582 && !integer_zerop (@2)
4583 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0)))
4584 (if (TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (@0))
4585 || wi::ltu_p (wi::to_wide (@2), TYPE_PRECISION (type)))
4586 (lshift (convert @1) @2)
4587 (if (wi::ltu_p (wi::to_wide (@2), TYPE_PRECISION (TREE_TYPE (@0))))
4588 { build_zero_cst (type); }))))
4590 /* Simplifications of conversions. */
4592 /* Basic strip-useless-type-conversions / strip_nops. */
4593 (for cvt (convert view_convert float fix_trunc)
4596 (if ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@0)))
4597 || (GENERIC && type == TREE_TYPE (@0)))
4600 /* Contract view-conversions. */
4602 (view_convert (view_convert @0))
4605 /* For integral conversions with the same precision or pointer
4606 conversions use a NOP_EXPR instead. */
4609 (if ((INTEGRAL_TYPE_P (type) || POINTER_TYPE_P (type))
4610 && (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
4611 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (@0)))
4614 /* Strip inner integral conversions that do not change precision or size, or
4615 zero-extend while keeping the same size (for bool-to-char). */
4617 (view_convert (convert@0 @1))
4618 (if ((INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
4619 && (INTEGRAL_TYPE_P (TREE_TYPE (@1)) || POINTER_TYPE_P (TREE_TYPE (@1)))
4620 && TYPE_SIZE (TREE_TYPE (@0)) == TYPE_SIZE (TREE_TYPE (@1))
4621 && (TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1))
4622 || (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (TREE_TYPE (@1))
4623 && TYPE_UNSIGNED (TREE_TYPE (@1)))))
4626 /* Simplify a view-converted empty or single-element constructor. */
4628 (view_convert CONSTRUCTOR@0)
4630 { tree ctor = (TREE_CODE (@0) == SSA_NAME
4631 ? gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0)) : @0); }
4633 (if (CONSTRUCTOR_NELTS (ctor) == 0)
4634 { build_zero_cst (type); })
4635 (if (CONSTRUCTOR_NELTS (ctor) == 1
4636 && VECTOR_TYPE_P (TREE_TYPE (ctor))
4637 && operand_equal_p (TYPE_SIZE (type),
4638 TYPE_SIZE (TREE_TYPE
4639 (CONSTRUCTOR_ELT (ctor, 0)->value))))
4640 (view_convert { CONSTRUCTOR_ELT (ctor, 0)->value; })))))
4642 /* Re-association barriers around constants and other re-association
4643 barriers can be removed. */
4645 (paren CONSTANT_CLASS_P@0)
4648 (paren (paren@1 @0))
4651 /* Handle cases of two conversions in a row. */
4652 (for ocvt (convert float fix_trunc)
4653 (for icvt (convert float)
4658 tree inside_type = TREE_TYPE (@0);
4659 tree inter_type = TREE_TYPE (@1);
4660 int inside_int = INTEGRAL_TYPE_P (inside_type);
4661 int inside_ptr = POINTER_TYPE_P (inside_type);
4662 int inside_float = FLOAT_TYPE_P (inside_type);
4663 int inside_vec = VECTOR_TYPE_P (inside_type);
4664 unsigned int inside_prec = element_precision (inside_type);
4665 int inside_unsignedp = TYPE_UNSIGNED (inside_type);
4666 int inter_int = INTEGRAL_TYPE_P (inter_type);
4667 int inter_ptr = POINTER_TYPE_P (inter_type);
4668 int inter_float = FLOAT_TYPE_P (inter_type);
4669 int inter_vec = VECTOR_TYPE_P (inter_type);
4670 unsigned int inter_prec = element_precision (inter_type);
4671 int inter_unsignedp = TYPE_UNSIGNED (inter_type);
4672 int final_int = INTEGRAL_TYPE_P (type);
4673 int final_ptr = POINTER_TYPE_P (type);
4674 int final_float = FLOAT_TYPE_P (type);
4675 int final_vec = VECTOR_TYPE_P (type);
4676 unsigned int final_prec = element_precision (type);
4677 int final_unsignedp = TYPE_UNSIGNED (type);
4680 /* In addition to the cases of two conversions in a row
4681 handled below, if we are converting something to its own
4682 type via an object of identical or wider precision, neither
4683 conversion is needed. */
4684 (if (((GIMPLE && useless_type_conversion_p (type, inside_type))
4686 && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (inside_type)))
4687 && (((inter_int || inter_ptr) && final_int)
4688 || (inter_float && final_float))
4689 && inter_prec >= final_prec)
4692 /* Likewise, if the intermediate and initial types are either both
4693 float or both integer, we don't need the middle conversion if the
4694 former is wider than the latter and doesn't change the signedness
4695 (for integers). Avoid this if the final type is a pointer since
4696 then we sometimes need the middle conversion. */
4697 (if (((inter_int && inside_int) || (inter_float && inside_float))
4698 && (final_int || final_float)
4699 && inter_prec >= inside_prec
4700 && (inter_float || inter_unsignedp == inside_unsignedp))
4703 /* If we have a sign-extension of a zero-extended value, we can
4704 replace that by a single zero-extension. Likewise if the
4705 final conversion does not change precision we can drop the
4706 intermediate conversion. */
4707 (if (inside_int && inter_int && final_int
4708 && ((inside_prec < inter_prec && inter_prec < final_prec
4709 && inside_unsignedp && !inter_unsignedp)
4710 || final_prec == inter_prec))
4713 /* Two conversions in a row are not needed unless:
4714 - some conversion is floating-point (overstrict for now), or
4715 - some conversion is a vector (overstrict for now), or
4716 - the intermediate type is narrower than both initial and
4718 - the intermediate type and innermost type differ in signedness,
4719 and the outermost type is wider than the intermediate, or
4720 - the initial type is a pointer type and the precisions of the
4721 intermediate and final types differ, or
4722 - the final type is a pointer type and the precisions of the
4723 initial and intermediate types differ. */
4724 (if (! inside_float && ! inter_float && ! final_float
4725 && ! inside_vec && ! inter_vec && ! final_vec
4726 && (inter_prec >= inside_prec || inter_prec >= final_prec)
4727 && ! (inside_int && inter_int
4728 && inter_unsignedp != inside_unsignedp
4729 && inter_prec < final_prec)
4730 && ((inter_unsignedp && inter_prec > inside_prec)
4731 == (final_unsignedp && final_prec > inter_prec))
4732 && ! (inside_ptr && inter_prec != final_prec)
4733 && ! (final_ptr && inside_prec != inter_prec))
4736 /* `(outer:M)(inter:N) a:O`
4737 can be converted to `(outer:M) a`
4738 if M <= O && N >= O. No matter what signedness of the casts,
4739 as the final is either a truncation from the original or just
4740 a sign change of the type. */
4741 (if (inside_int && inter_int && final_int
4742 && final_prec <= inside_prec
4743 && inter_prec >= inside_prec)
4746 /* A truncation to an unsigned type (a zero-extension) should be
4747 canonicalized as bitwise and of a mask. */
4748 (if (GIMPLE /* PR70366: doing this in GENERIC breaks -Wconversion. */
4749 && final_int && inter_int && inside_int
4750 && final_prec == inside_prec
4751 && final_prec > inter_prec
4753 (convert (bit_and @0 { wide_int_to_tree
4755 wi::mask (inter_prec, false,
4756 TYPE_PRECISION (inside_type))); })))
4758 /* If we are converting an integer to a floating-point that can
4759 represent it exactly and back to an integer, we can skip the
4760 floating-point conversion. */
4761 (if (GIMPLE /* PR66211 */
4762 && inside_int && inter_float && final_int &&
4763 (unsigned) significand_size (TYPE_MODE (inter_type))
4764 >= inside_prec - !inside_unsignedp)
4767 /* (float_type)(integer_type) x -> trunc (x) if the type of x matches
4768 float_type. Only do the transformation if we do not need to preserve
4769 trapping behaviour, so require !flag_trapping_math. */
4772 (float (fix_trunc @0))
4773 (if (!flag_trapping_math
4774 && types_match (type, TREE_TYPE (@0))
4775 && direct_internal_fn_supported_p (IFN_TRUNC, type,
4780 /* If we have a narrowing conversion to an integral type that is fed by a
4781 BIT_AND_EXPR, we might be able to remove the BIT_AND_EXPR if it merely
4782 masks off bits outside the final type (and nothing else). */
4784 (convert (bit_and @0 INTEGER_CST@1))
4785 (if (INTEGRAL_TYPE_P (type)
4786 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
4787 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))
4788 && operand_equal_p (@1, build_low_bits_mask (TREE_TYPE (@1),
4789 TYPE_PRECISION (type)), 0))
4793 /* (X /[ex] A) * A -> X. */
4795 (mult (convert1? (exact_div @0 @@1)) (convert2? @1))
4798 /* Simplify (A / B) * B + (A % B) -> A. */
4799 (for div (trunc_div ceil_div floor_div round_div)
4800 mod (trunc_mod ceil_mod floor_mod round_mod)
4802 (plus:c (mult:c (div @0 @1) @1) (mod @0 @1))
4805 /* x / y * y == x -> x % y == 0. */
4807 (eq:c (mult:c (trunc_div:s @0 @1) @1) @0)
4808 (if (TREE_CODE (TREE_TYPE (@0)) != COMPLEX_TYPE)
4809 (eq (trunc_mod @0 @1) { build_zero_cst (TREE_TYPE (@0)); })))
4811 /* ((X /[ex] A) +- B) * A --> X +- A * B. */
4812 (for op (plus minus)
4814 (mult (convert1? (op (convert2? (exact_div @0 INTEGER_CST@@1)) INTEGER_CST@2)) @1)
4815 (if (tree_nop_conversion_p (type, TREE_TYPE (@2))
4816 && tree_nop_conversion_p (TREE_TYPE (@0), TREE_TYPE (@2)))
4819 wi::overflow_type overflow;
4820 wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@2),
4821 TYPE_SIGN (type), &overflow);
4823 (if (types_match (type, TREE_TYPE (@2))
4824 && types_match (TREE_TYPE (@0), TREE_TYPE (@2)) && !overflow)
4825 (op @0 { wide_int_to_tree (type, mul); })
4826 (with { tree utype = unsigned_type_for (type); }
4827 (convert (op (convert:utype @0)
4828 (mult (convert:utype @1) (convert:utype @2))))))))))
4830 /* Canonicalization of binary operations. */
4832 /* Convert X + -C into X - C. */
4834 (plus @0 REAL_CST@1)
4835 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
4836 (with { tree tem = const_unop (NEGATE_EXPR, type, @1); }
4837 (if (!TREE_OVERFLOW (tem) || !flag_trapping_math)
4838 (minus @0 { tem; })))))
4840 /* Convert x+x into x*2. */
4843 (if (SCALAR_FLOAT_TYPE_P (type))
4844 (mult @0 { build_real (type, dconst2); })
4845 (if (INTEGRAL_TYPE_P (type))
4846 (mult @0 { build_int_cst (type, 2); }))))
4850 (minus integer_zerop @1)
4853 (pointer_diff integer_zerop @1)
4854 (negate (convert @1)))
4856 /* (ARG0 - ARG1) is the same as (-ARG1 + ARG0). So check whether
4857 ARG0 is zero and X + ARG0 reduces to X, since that would mean
4858 (-ARG1 + ARG0) reduces to -ARG1. */
4860 (minus real_zerop@0 @1)
4861 (if (fold_real_zero_addition_p (type, @1, @0, 0))
4864 /* Transform x * -1 into -x. */
4866 (mult @0 integer_minus_onep)
4869 /* Reassociate (X * CST) * Y to (X * Y) * CST. This does not introduce
4870 signed overflow for CST != 0 && CST != -1. */
4872 (mult:c (mult:s@3 @0 INTEGER_CST@1) @2)
4873 (if (TREE_CODE (@2) != INTEGER_CST
4875 && !integer_zerop (@1) && !integer_minus_onep (@1))
4876 (mult (mult @0 @2) @1)))
4878 /* True if we can easily extract the real and imaginary parts of a complex
4880 (match compositional_complex
4881 (convert? (complex @0 @1)))
4883 /* COMPLEX_EXPR and REALPART/IMAGPART_EXPR cancellations. */
4885 (complex (realpart @0) (imagpart @0))
4888 (realpart (complex @0 @1))
4891 (imagpart (complex @0 @1))
4894 /* Sometimes we only care about half of a complex expression. */
4896 (realpart (convert?:s (conj:s @0)))
4897 (convert (realpart @0)))
4899 (imagpart (convert?:s (conj:s @0)))
4900 (convert (negate (imagpart @0))))
4901 (for part (realpart imagpart)
4902 (for op (plus minus)
4904 (part (convert?:s@2 (op:s @0 @1)))
4905 (convert (op (part @0) (part @1))))))
4907 (realpart (convert?:s (CEXPI:s @0)))
4910 (imagpart (convert?:s (CEXPI:s @0)))
4913 /* conj(conj(x)) -> x */
4915 (conj (convert? (conj @0)))
4916 (if (tree_nop_conversion_p (TREE_TYPE (@0), type))
4919 /* conj({x,y}) -> {x,-y} */
4921 (conj (convert?:s (complex:s @0 @1)))
4922 (with { tree itype = TREE_TYPE (type); }
4923 (complex (convert:itype @0) (negate (convert:itype @1)))))
4925 /* BSWAP simplifications, transforms checked by gcc.dg/builtin-bswap-8.c. */
4931 (bswap (bit_not (bswap @0)))
4933 (for bitop (bit_xor bit_ior bit_and)
4935 (bswap (bitop:c (bswap @0) @1))
4936 (bitop @0 (bswap @1))))
4939 (cmp (bswap@2 @0) (bswap @1))
4940 (with { tree ctype = TREE_TYPE (@2); }
4941 (cmp (convert:ctype @0) (convert:ctype @1))))
4943 (cmp (bswap @0) INTEGER_CST@1)
4944 (with { tree ctype = TREE_TYPE (@1); }
4945 (cmp (convert:ctype @0) (bswap! @1)))))
4946 /* (bswap(x) >> C1) & C2 can sometimes be simplified to (x >> C3) & C2. */
4948 (bit_and (convert1? (rshift@0 (convert2? (bswap@4 @1)) INTEGER_CST@2))
4950 (if (BITS_PER_UNIT == 8
4951 && tree_fits_uhwi_p (@2)
4952 && tree_fits_uhwi_p (@3))
4955 unsigned HOST_WIDE_INT prec = TYPE_PRECISION (TREE_TYPE (@4));
4956 unsigned HOST_WIDE_INT bits = tree_to_uhwi (@2);
4957 unsigned HOST_WIDE_INT mask = tree_to_uhwi (@3);
4958 unsigned HOST_WIDE_INT lo = bits & 7;
4959 unsigned HOST_WIDE_INT hi = bits - lo;
4962 && mask < (256u>>lo)
4963 && bits < TYPE_PRECISION (TREE_TYPE(@0)))
4964 (with { unsigned HOST_WIDE_INT ns = (prec - (hi + 8)) + lo; }
4966 (bit_and (convert @1) @3)
4969 tree utype = unsigned_type_for (TREE_TYPE (@1));
4970 tree nst = build_int_cst (integer_type_node, ns);
4972 (bit_and (convert (rshift:utype (convert:utype @1) {nst;})) @3))))))))
4973 /* bswap(x) >> C1 can sometimes be simplified to (T)x >> C2. */
4975 (rshift (convert? (bswap@2 @0)) INTEGER_CST@1)
4976 (if (BITS_PER_UNIT == 8
4977 && CHAR_TYPE_SIZE == 8
4978 && tree_fits_uhwi_p (@1))
4981 unsigned HOST_WIDE_INT prec = TYPE_PRECISION (TREE_TYPE (@2));
4982 unsigned HOST_WIDE_INT bits = tree_to_uhwi (@1);
4983 /* If the bswap was extended before the original shift, this
4984 byte (shift) has the sign of the extension, not the sign of
4985 the original shift. */
4986 tree st = TYPE_PRECISION (type) > prec ? TREE_TYPE (@2) : type;
4988 /* Special case: logical right shift of sign-extended bswap.
4989 (unsigned)(short)bswap16(x)>>12 is (unsigned)((short)x<<8)>>12. */
4990 (if (TYPE_PRECISION (type) > prec
4991 && !TYPE_UNSIGNED (TREE_TYPE (@2))
4992 && TYPE_UNSIGNED (type)
4993 && bits < prec && bits + 8 >= prec)
4994 (with { tree nst = build_int_cst (integer_type_node, prec - 8); }
4995 (rshift (convert (lshift:st (convert:st @0) {nst;})) @1))
4996 (if (bits + 8 == prec)
4997 (if (TYPE_UNSIGNED (st))
4998 (convert (convert:unsigned_char_type_node @0))
4999 (convert (convert:signed_char_type_node @0)))
5000 (if (bits < prec && bits + 8 > prec)
5003 tree nst = build_int_cst (integer_type_node, bits & 7);
5004 tree bt = TYPE_UNSIGNED (st) ? unsigned_char_type_node
5005 : signed_char_type_node;
5007 (convert (rshift:bt (convert:bt @0) {nst;})))))))))
5008 /* bswap(x) & C1 can sometimes be simplified to (x >> C2) & C1. */
5010 (bit_and (convert? (bswap@2 @0)) INTEGER_CST@1)
5011 (if (BITS_PER_UNIT == 8
5012 && tree_fits_uhwi_p (@1)
5013 && tree_to_uhwi (@1) < 256)
5016 unsigned HOST_WIDE_INT prec = TYPE_PRECISION (TREE_TYPE (@2));
5017 tree utype = unsigned_type_for (TREE_TYPE (@0));
5018 tree nst = build_int_cst (integer_type_node, prec - 8);
5020 (bit_and (convert (rshift:utype (convert:utype @0) {nst;})) @1)))))
5023 /* Combine COND_EXPRs and VEC_COND_EXPRs. */
5025 /* Simplify constant conditions.
5026 Only optimize constant conditions when the selected branch
5027 has the same type as the COND_EXPR. This avoids optimizing
5028 away "c ? x : throw", where the throw has a void type.
5029 Note that we cannot throw away the fold-const.cc variant nor
5030 this one as we depend on doing this transform before possibly
5031 A ? B : B -> B triggers and the fold-const.cc one can optimize
5032 0 ? A : B to B even if A has side-effects. Something
5033 genmatch cannot handle. */
5035 (cond INTEGER_CST@0 @1 @2)
5036 (if (integer_zerop (@0))
5037 (if (!VOID_TYPE_P (TREE_TYPE (@2)) || VOID_TYPE_P (type))
5039 (if (!VOID_TYPE_P (TREE_TYPE (@1)) || VOID_TYPE_P (type))
5042 (vec_cond VECTOR_CST@0 @1 @2)
5043 (if (integer_all_onesp (@0))
5045 (if (integer_zerop (@0))
5048 /* Sink unary operations to branches, but only if we do fold both. */
5049 (for op (negate bit_not abs absu)
5051 (op (vec_cond:s @0 @1 @2))
5052 (vec_cond @0 (op! @1) (op! @2))))
5054 /* Sink unary conversions to branches, but only if we do fold both
5055 and the target's truth type is the same as we already have. */
5057 (convert (vec_cond:s @0 @1 @2))
5058 (if (VECTOR_TYPE_P (type)
5059 && types_match (TREE_TYPE (@0), truth_type_for (type)))
5060 (vec_cond @0 (convert! @1) (convert! @2))))
5062 /* Likewise for view_convert of nop_conversions. */
5064 (view_convert (vec_cond:s @0 @1 @2))
5065 (if (VECTOR_TYPE_P (type) && VECTOR_TYPE_P (TREE_TYPE (@1))
5066 && known_eq (TYPE_VECTOR_SUBPARTS (type),
5067 TYPE_VECTOR_SUBPARTS (TREE_TYPE (@1)))
5068 && tree_nop_conversion_p (TREE_TYPE (type), TREE_TYPE (TREE_TYPE (@1))))
5069 (vec_cond @0 (view_convert! @1) (view_convert! @2))))
5071 /* Sink binary operation to branches, but only if we can fold it. */
5072 (for op (tcc_comparison plus minus mult bit_and bit_ior bit_xor
5073 lshift rshift rdiv trunc_div ceil_div floor_div round_div
5074 trunc_mod ceil_mod floor_mod round_mod min max)
5075 /* (c ? a : b) op (c ? d : e) --> c ? (a op d) : (b op e) */
5077 (op (vec_cond:s @0 @1 @2) (vec_cond:s @0 @3 @4))
5078 (vec_cond @0 (op! @1 @3) (op! @2 @4)))
5080 /* (c ? a : b) op d --> c ? (a op d) : (b op d) */
5082 (op (vec_cond:s @0 @1 @2) @3)
5083 (vec_cond @0 (op! @1 @3) (op! @2 @3)))
5085 (op @3 (vec_cond:s @0 @1 @2))
5086 (vec_cond @0 (op! @3 @1) (op! @3 @2))))
5089 (match (nop_atomic_bit_test_and_p @0 @1 @4)
5090 (bit_and (convert?@4 (ATOMIC_FETCH_OR_XOR_N @2 INTEGER_CST@0 @3))
5093 int ibit = tree_log2 (@0);
5094 int ibit2 = tree_log2 (@1);
5098 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))))))
5100 (match (nop_atomic_bit_test_and_p @0 @1 @3)
5101 (bit_and (convert?@3 (SYNC_FETCH_OR_XOR_N @2 INTEGER_CST@0))
5104 int ibit = tree_log2 (@0);
5105 int ibit2 = tree_log2 (@1);
5109 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))))))
5111 (match (nop_atomic_bit_test_and_p @0 @0 @4)
5114 (ATOMIC_FETCH_OR_XOR_N @2 (nop_convert? (lshift@0 integer_onep@5 @6)) @3))
5116 (if (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0)))))
5118 (match (nop_atomic_bit_test_and_p @0 @0 @4)
5121 (SYNC_FETCH_OR_XOR_N @2 (nop_convert? (lshift@0 integer_onep@3 @5))))
5123 (if (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0)))))
5125 (match (nop_atomic_bit_test_and_p @0 @1 @3)
5126 (bit_and@4 (convert?@3 (ATOMIC_FETCH_AND_N @2 INTEGER_CST@0 @5))
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 @0 @1 @3)
5139 (convert?@3 (SYNC_FETCH_AND_AND_N @2 INTEGER_CST@0))
5142 int ibit = wi::exact_log2 (wi::zext (wi::bit_not (wi::to_wide (@0)),
5143 TYPE_PRECISION(type)));
5144 int ibit2 = tree_log2 (@1);
5148 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))))))
5150 (match (nop_atomic_bit_test_and_p @4 @0 @3)
5153 (ATOMIC_FETCH_AND_N @2 (nop_convert?@4 (bit_not (lshift@0 integer_onep@6 @7))) @5))
5155 (if (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@4)))))
5157 (match (nop_atomic_bit_test_and_p @4 @0 @3)
5160 (SYNC_FETCH_AND_AND_N @2 (nop_convert?@4 (bit_not (lshift@0 integer_onep@6 @7)))))
5162 (if (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@4)))))
5166 /* (v ? w : 0) ? a : b is just (v & w) ? a : b
5167 Currently disabled after pass lvec because ARM understands
5168 VEC_COND_EXPR<v==w,-1,0> but not a plain v==w fed to BIT_IOR_EXPR. */
5170 /* These can only be done in gimple as fold likes to convert:
5171 (CMP) & N into (CMP) ? N : 0
5172 and we try to match the same pattern again and again. */
5174 (vec_cond (vec_cond:s @0 @3 integer_zerop) @1 @2)
5175 (if (optimize_vectors_before_lowering_p () && types_match (@0, @3))
5176 (vec_cond (bit_and @0 @3) @1 @2)))
5178 (vec_cond (vec_cond:s @0 integer_all_onesp @3) @1 @2)
5179 (if (optimize_vectors_before_lowering_p () && types_match (@0, @3))
5180 (vec_cond (bit_ior @0 @3) @1 @2)))
5182 (vec_cond (vec_cond:s @0 integer_zerop @3) @1 @2)
5183 (if (optimize_vectors_before_lowering_p () && types_match (@0, @3))
5184 (vec_cond (bit_ior @0 (bit_not @3)) @2 @1)))
5186 (vec_cond (vec_cond:s @0 @3 integer_all_onesp) @1 @2)
5187 (if (optimize_vectors_before_lowering_p () && types_match (@0, @3))
5188 (vec_cond (bit_and @0 (bit_not @3)) @2 @1)))
5190 /* c1 ? c2 ? a : b : b --> (c1 & c2) ? a : b */
5192 (vec_cond @0 (vec_cond:s @1 @2 @3) @3)
5193 (if (optimize_vectors_before_lowering_p () && types_match (@0, @1))
5194 (vec_cond (bit_and @0 @1) @2 @3)))
5196 (vec_cond @0 @2 (vec_cond:s @1 @2 @3))
5197 (if (optimize_vectors_before_lowering_p () && types_match (@0, @1))
5198 (vec_cond (bit_ior @0 @1) @2 @3)))
5200 (vec_cond @0 (vec_cond:s @1 @2 @3) @2)
5201 (if (optimize_vectors_before_lowering_p () && types_match (@0, @1))
5202 (vec_cond (bit_ior (bit_not @0) @1) @2 @3)))
5204 (vec_cond @0 @3 (vec_cond:s @1 @2 @3))
5205 (if (optimize_vectors_before_lowering_p () && types_match (@0, @1))
5206 (vec_cond (bit_and (bit_not @0) @1) @2 @3)))
5209 /* Canonicalize mask ? { 0, ... } : { -1, ...} to ~mask if the mask
5210 types are compatible. */
5212 (vec_cond @0 VECTOR_CST@1 VECTOR_CST@2)
5213 (if (VECTOR_BOOLEAN_TYPE_P (type)
5214 && types_match (type, TREE_TYPE (@0)))
5215 (if (integer_zerop (@1) && integer_all_onesp (@2))
5217 (if (integer_all_onesp (@1) && integer_zerop (@2))
5220 /* A few simplifications of "a ? CST1 : CST2". */
5221 /* NOTE: Only do this on gimple as the if-chain-to-switch
5222 optimization depends on the gimple to have if statements in it. */
5225 (cond @0 INTEGER_CST@1 INTEGER_CST@2)
5227 (if (integer_zerop (@2))
5229 /* a ? 1 : 0 -> a if 0 and 1 are integral types. */
5230 (if (integer_onep (@1))
5231 (convert (convert:boolean_type_node @0)))
5232 /* a ? -1 : 0 -> -a. */
5233 (if (INTEGRAL_TYPE_P (type) && integer_all_onesp (@1))
5234 (if (TYPE_PRECISION (type) == 1)
5235 /* For signed 1-bit precision just cast bool to the type. */
5236 (convert (convert:boolean_type_node @0))
5237 (if (TREE_CODE (type) == BOOLEAN_TYPE)
5239 tree intt = build_nonstandard_integer_type (TYPE_PRECISION (type),
5240 TYPE_UNSIGNED (type));
5242 (convert (negate (convert:intt (convert:boolean_type_node @0)))))
5243 (negate (convert:type (convert:boolean_type_node @0))))))
5244 /* a ? powerof2cst : 0 -> a << (log2(powerof2cst)) */
5245 (if (INTEGRAL_TYPE_P (type) && integer_pow2p (@1))
5247 tree shift = build_int_cst (integer_type_node, tree_log2 (@1));
5249 (lshift (convert (convert:boolean_type_node @0)) { shift; })))))
5250 (if (integer_zerop (@1))
5252 /* a ? 0 : 1 -> !a. */
5253 (if (integer_onep (@2))
5254 (convert (bit_xor (convert:boolean_type_node @0) { boolean_true_node; })))
5255 /* a ? 0 : -1 -> -(!a). */
5256 (if (INTEGRAL_TYPE_P (type) && integer_all_onesp (@2))
5257 (if (TYPE_PRECISION (type) == 1)
5258 /* For signed 1-bit precision just cast bool to the type. */
5259 (convert (bit_xor (convert:boolean_type_node @0) { boolean_true_node; }))
5260 (if (TREE_CODE (type) == BOOLEAN_TYPE)
5262 tree intt = build_nonstandard_integer_type (TYPE_PRECISION (type),
5263 TYPE_UNSIGNED (type));
5265 (convert (negate (convert:intt (bit_xor (convert:boolean_type_node @0)
5266 { boolean_true_node; })))))
5267 (negate (convert:type (bit_xor (convert:boolean_type_node @0)
5268 { boolean_true_node; }))))))
5269 /* a ? 0 : powerof2cst -> (!a) << (log2(powerof2cst)) */
5270 (if (INTEGRAL_TYPE_P (type) && integer_pow2p (@2))
5272 tree shift = build_int_cst (integer_type_node, tree_log2 (@2));
5274 (lshift (convert (bit_xor (convert:boolean_type_node @0)
5275 { boolean_true_node; })) { shift; })))))))
5277 /* (a > 1) ? 0 : (cast)a is the same as (cast)(a == 1)
5278 for unsigned types. */
5280 (cond (gt @0 integer_onep@1) integer_zerop (convert? @2))
5281 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
5282 && bitwise_equal_p (@0, @2))
5283 (convert (eq @0 @1))
5287 /* (a <= 1) & (cast)a is the same as (cast)(a == 1)
5288 for unsigned types. */
5290 (bit_and:c (convert1? (le @0 integer_onep@1)) (convert2? @2))
5291 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
5292 && bitwise_equal_p (@0, @2))
5293 (convert (eq @0 @1))
5297 /* `(a == CST) & a` can be simplified to `0` or `(a == CST)` depending
5298 on the first bit of the CST. */
5300 (bit_and:c (convert@2 (eq @0 INTEGER_CST@1)) (convert? @0))
5301 (if ((wi::to_wide (@1) & 1) != 0)
5303 { build_zero_cst (type); }))
5306 # x_5 in range [cst1, cst2] where cst2 = cst1 + 1
5307 x_5 == cstN ? cst4 : cst3
5308 # op is == or != and N is 1 or 2
5309 to r_6 = x_5 + (min (cst3, cst4) - cst1) or
5310 r_6 = (min (cst3, cst4) + cst1) - x_5 depending on op, N and which
5311 of cst3 and cst4 is smaller.
5312 This was originally done by two_value_replacement in phiopt (PR 88676). */
5315 (cond (eqne SSA_NAME@0 INTEGER_CST@1) INTEGER_CST@2 INTEGER_CST@3)
5316 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
5317 && INTEGRAL_TYPE_P (type)
5318 && (wi::to_widest (@2) + 1 == wi::to_widest (@3)
5319 || wi::to_widest (@2) == wi::to_widest (@3) + 1))
5322 get_range_query (cfun)->range_of_expr (r, @0);
5323 if (r.undefined_p ())
5324 r.set_varying (TREE_TYPE (@0));
5326 wide_int min = r.lower_bound ();
5327 wide_int max = r.upper_bound ();
5330 && (wi::to_wide (@1) == min
5331 || wi::to_wide (@1) == max))
5333 tree arg0 = @2, arg1 = @3;
5335 if ((eqne == EQ_EXPR) ^ (wi::to_wide (@1) == min))
5336 std::swap (arg0, arg1);
5337 if (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type))
5338 type1 = TREE_TYPE (@0);
5341 auto prec = TYPE_PRECISION (type1);
5342 auto unsign = TYPE_UNSIGNED (type1);
5343 if (TREE_CODE (type1) == BOOLEAN_TYPE)
5344 type1 = build_nonstandard_integer_type (prec, unsign);
5345 min = wide_int::from (min, prec,
5346 TYPE_SIGN (TREE_TYPE (@0)));
5347 wide_int a = wide_int::from (wi::to_wide (arg0), prec,
5349 enum tree_code code;
5350 wi::overflow_type ovf;
5351 if (tree_int_cst_lt (arg0, arg1))
5357 /* lhs is known to be in range [min, min+1] and we want to add a
5358 to it. Check if that operation can overflow for those 2 values
5359 and if yes, force unsigned type. */
5360 wi::add (min + (wi::neg_p (a) ? 0 : 1), a, SIGNED, &ovf);
5362 type1 = unsigned_type_for (type1);
5371 /* lhs is known to be in range [min, min+1] and we want to subtract
5372 it from a. Check if that operation can overflow for those 2
5373 values and if yes, force unsigned type. */
5374 wi::sub (a, min + (wi::neg_p (min) ? 0 : 1), SIGNED, &ovf);
5376 type1 = unsigned_type_for (type1);
5379 tree arg = wide_int_to_tree (type1, a);
5381 (if (code == PLUS_EXPR)
5382 (convert (plus (convert:type1 @0) { arg; }))
5383 (convert (minus { arg; } (convert:type1 @0))))))))))
5387 (convert (cond@0 @1 INTEGER_CST@2 INTEGER_CST@3))
5388 (if (INTEGRAL_TYPE_P (type)
5389 && INTEGRAL_TYPE_P (TREE_TYPE (@0)))
5390 (cond @1 (convert @2) (convert @3))))
5392 /* Simplification moved from fold_cond_expr_with_comparison. It may also
5394 /* This pattern implements two kinds simplification:
5397 (cond (cmp (convert1? x) c1) (convert2? x) c2) -> (minmax (x c)) if:
5398 1) Conversions are type widening from smaller type.
5399 2) Const c1 equals to c2 after canonicalizing comparison.
5400 3) Comparison has tree code LT, LE, GT or GE.
5401 This specific pattern is needed when (cmp (convert x) c) may not
5402 be simplified by comparison patterns because of multiple uses of
5403 x. It also makes sense here because simplifying across multiple
5404 referred var is always benefitial for complicated cases.
5407 (cond (eq (convert1? x) c1) (convert2? x) c2) -> (cond (eq x c1) c1 c2). */
5408 (for cmp (lt le gt ge eq ne)
5410 (cond (cmp (convert1? @1) INTEGER_CST@3) (convert2? @1) INTEGER_CST@2)
5413 tree from_type = TREE_TYPE (@1);
5414 tree c1_type = TREE_TYPE (@3), c2_type = TREE_TYPE (@2);
5415 enum tree_code code = ERROR_MARK;
5417 if (INTEGRAL_TYPE_P (from_type)
5418 && int_fits_type_p (@2, from_type)
5419 && (types_match (c1_type, from_type)
5420 || (TYPE_PRECISION (c1_type) > TYPE_PRECISION (from_type)
5421 && (TYPE_UNSIGNED (from_type)
5422 || TYPE_SIGN (c1_type) == TYPE_SIGN (from_type))))
5423 && (types_match (c2_type, from_type)
5424 || (TYPE_PRECISION (c2_type) > TYPE_PRECISION (from_type)
5425 && (TYPE_UNSIGNED (from_type)
5426 || TYPE_SIGN (c2_type) == TYPE_SIGN (from_type)))))
5429 code = minmax_from_comparison (cmp, @1, @3, @1, @2);
5430 /* Can do A == C1 ? A : C2 -> A == C1 ? C1 : C2? */
5431 else if (int_fits_type_p (@3, from_type))
5435 (if (code == MAX_EXPR)
5436 (convert (max @1 (convert @2)))
5437 (if (code == MIN_EXPR)
5438 (convert (min @1 (convert @2)))
5439 (if (code == EQ_EXPR)
5440 (convert (cond (eq @1 (convert @3))
5441 (convert:from_type @3) (convert:from_type @2)))))))))
5443 /* (cond (cmp (convert? x) c1) (op x c2) c3) -> (op (minmax x c1) c2) if:
5445 1) OP is PLUS or MINUS.
5446 2) CMP is LT, LE, GT or GE.
5447 3) C3 == (C1 op C2), and computation doesn't have undefined behavior.
5449 This pattern also handles special cases like:
5451 A) Operand x is a unsigned to signed type conversion and c1 is
5452 integer zero. In this case,
5453 (signed type)x < 0 <=> x > MAX_VAL(signed type)
5454 (signed type)x >= 0 <=> x <= MAX_VAL(signed type)
5455 B) Const c1 may not equal to (C3 op' C2). In this case we also
5456 check equality for (c1+1) and (c1-1) by adjusting comparison
5459 TODO: Though signed type is handled by this pattern, it cannot be
5460 simplified at the moment because C standard requires additional
5461 type promotion. In order to match&simplify it here, the IR needs
5462 to be cleaned up by other optimizers, i.e, VRP. */
5463 (for op (plus minus)
5464 (for cmp (lt le gt ge)
5466 (cond (cmp (convert? @X) INTEGER_CST@1) (op @X INTEGER_CST@2) INTEGER_CST@3)
5467 (with { tree from_type = TREE_TYPE (@X), to_type = TREE_TYPE (@1); }
5468 (if (types_match (from_type, to_type)
5469 /* Check if it is special case A). */
5470 || (TYPE_UNSIGNED (from_type)
5471 && !TYPE_UNSIGNED (to_type)
5472 && TYPE_PRECISION (from_type) == TYPE_PRECISION (to_type)
5473 && integer_zerop (@1)
5474 && (cmp == LT_EXPR || cmp == GE_EXPR)))
5477 wi::overflow_type overflow = wi::OVF_NONE;
5478 enum tree_code code, cmp_code = cmp;
5480 wide_int c1 = wi::to_wide (@1);
5481 wide_int c2 = wi::to_wide (@2);
5482 wide_int c3 = wi::to_wide (@3);
5483 signop sgn = TYPE_SIGN (from_type);
5485 /* Handle special case A), given x of unsigned type:
5486 ((signed type)x < 0) <=> (x > MAX_VAL(signed type))
5487 ((signed type)x >= 0) <=> (x <= MAX_VAL(signed type)) */
5488 if (!types_match (from_type, to_type))
5490 if (cmp_code == LT_EXPR)
5492 if (cmp_code == GE_EXPR)
5494 c1 = wi::max_value (to_type);
5496 /* To simplify this pattern, we require c3 = (c1 op c2). Here we
5497 compute (c3 op' c2) and check if it equals to c1 with op' being
5498 the inverted operator of op. Make sure overflow doesn't happen
5499 if it is undefined. */
5500 if (op == PLUS_EXPR)
5501 real_c1 = wi::sub (c3, c2, sgn, &overflow);
5503 real_c1 = wi::add (c3, c2, sgn, &overflow);
5506 if (!overflow || !TYPE_OVERFLOW_UNDEFINED (from_type))
5508 /* Check if c1 equals to real_c1. Boundary condition is handled
5509 by adjusting comparison operation if necessary. */
5510 if (!wi::cmp (wi::sub (real_c1, 1, sgn, &overflow), c1, sgn)
5513 /* X <= Y - 1 equals to X < Y. */
5514 if (cmp_code == LE_EXPR)
5516 /* X > Y - 1 equals to X >= Y. */
5517 if (cmp_code == GT_EXPR)
5520 if (!wi::cmp (wi::add (real_c1, 1, sgn, &overflow), c1, sgn)
5523 /* X < Y + 1 equals to X <= Y. */
5524 if (cmp_code == LT_EXPR)
5526 /* X >= Y + 1 equals to X > Y. */
5527 if (cmp_code == GE_EXPR)
5530 if (code != cmp_code || !wi::cmp (real_c1, c1, sgn))
5532 if (cmp_code == LT_EXPR || cmp_code == LE_EXPR)
5534 if (cmp_code == GT_EXPR || cmp_code == GE_EXPR)
5539 (if (code == MAX_EXPR)
5540 (op (max @X { wide_int_to_tree (from_type, real_c1); })
5541 { wide_int_to_tree (from_type, c2); })
5542 (if (code == MIN_EXPR)
5543 (op (min @X { wide_int_to_tree (from_type, real_c1); })
5544 { wide_int_to_tree (from_type, c2); })))))))))
5547 /* A >= B ? A : B -> max (A, B) and friends. The code is still
5548 in fold_cond_expr_with_comparison for GENERIC folding with
5549 some extra constraints. */
5550 (for cmp (eq ne le lt unle unlt ge gt unge ungt uneq ltgt)
5552 (cond (cmp:c (nop_convert1?@c0 @0) (nop_convert2?@c1 @1))
5553 (convert3? @0) (convert4? @1))
5554 (if (!HONOR_SIGNED_ZEROS (type)
5555 && (/* Allow widening conversions of the compare operands as data. */
5556 (INTEGRAL_TYPE_P (type)
5557 && types_match (TREE_TYPE (@c0), TREE_TYPE (@0))
5558 && types_match (TREE_TYPE (@c1), TREE_TYPE (@1))
5559 && TYPE_PRECISION (TREE_TYPE (@0)) <= TYPE_PRECISION (type)
5560 && TYPE_PRECISION (TREE_TYPE (@1)) <= TYPE_PRECISION (type))
5561 /* Or sign conversions for the comparison. */
5562 || (types_match (type, TREE_TYPE (@0))
5563 && types_match (type, TREE_TYPE (@1)))))
5565 (if (cmp == EQ_EXPR)
5566 (if (VECTOR_TYPE_P (type))
5569 (if (cmp == NE_EXPR)
5570 (if (VECTOR_TYPE_P (type))
5573 (if (cmp == LE_EXPR || cmp == UNLE_EXPR || cmp == LT_EXPR || cmp == UNLT_EXPR)
5574 (if (!HONOR_NANS (type))
5575 (if (VECTOR_TYPE_P (type))
5576 (view_convert (min @c0 @c1))
5577 (convert (min @c0 @c1)))))
5578 (if (cmp == GE_EXPR || cmp == UNGE_EXPR || cmp == GT_EXPR || cmp == UNGT_EXPR)
5579 (if (!HONOR_NANS (type))
5580 (if (VECTOR_TYPE_P (type))
5581 (view_convert (max @c0 @c1))
5582 (convert (max @c0 @c1)))))
5583 (if (cmp == UNEQ_EXPR)
5584 (if (!HONOR_NANS (type))
5585 (if (VECTOR_TYPE_P (type))
5588 (if (cmp == LTGT_EXPR)
5589 (if (!HONOR_NANS (type))
5590 (if (VECTOR_TYPE_P (type))
5592 (convert @c0))))))))
5595 (for cnd (cond vec_cond)
5596 /* (a != b) ? (a - b) : 0 -> (a - b) */
5598 (cnd (ne:c @0 @1) (minus@2 @0 @1) integer_zerop)
5600 /* (a != b) ? (a ^ b) : 0 -> (a ^ b) */
5602 (cnd (ne:c @0 @1) (bit_xor:c@2 @0 @1) integer_zerop)
5604 /* (a != b) ? (a & b) : a -> (a & b) */
5605 /* (a != b) ? (a | b) : a -> (a | b) */
5606 /* (a != b) ? min(a,b) : a -> min(a,b) */
5607 /* (a != b) ? max(a,b) : a -> max(a,b) */
5608 (for op (bit_and bit_ior min max)
5610 (cnd (ne:c @0 @1) (op:c@2 @0 @1) @0)
5612 /* (a != b) ? (a * b) : (a * a) -> (a * b) */
5613 /* (a != b) ? (a + b) : (a + a) -> (a + b) */
5616 (cnd (ne:c @0 @1) (op@2 @0 @1) (op @0 @0))
5617 (if (ANY_INTEGRAL_TYPE_P (type))
5619 /* (a != b) ? (a + b) : (2 * a) -> (a + b) */
5621 (cnd (ne:c @0 @1) (plus@2 @0 @1) (mult @0 uniform_integer_cst_p@3))
5622 (if (wi::to_wide (uniform_integer_cst_p (@3)) == 2)
5626 /* These was part of minmax phiopt. */
5627 /* Optimize (a CMP b) ? minmax<a, c> : minmax<b, c>
5628 to minmax<min/max<a, b>, c> */
5629 (for minmax (min max)
5630 (for cmp (lt le gt ge ne)
5632 (cond (cmp:c @1 @3) (minmax:c @1 @4) (minmax:c @2 @4))
5635 tree_code code = minmax_from_comparison (cmp, @1, @2, @1, @3);
5637 (if (code == MIN_EXPR)
5638 (minmax (min @1 @2) @4)
5639 (if (code == MAX_EXPR)
5640 (minmax (max @1 @2) @4)))))))
5642 /* Optimize (a CMP CST1) ? max<a,CST2> : a */
5643 (for cmp (gt ge lt le)
5644 minmax (min min max max)
5646 (cond (cmp:c @0 @1) (minmax:c@2 @0 @3) @4)
5649 tree_code code = minmax_from_comparison (cmp, @0, @1, @0, @4);
5651 (if ((cmp == LT_EXPR || cmp == LE_EXPR)
5653 && integer_nonzerop (fold_build2 (LE_EXPR, boolean_type_node, @3, @4)))
5655 (if ((cmp == GT_EXPR || cmp == GE_EXPR)
5657 && integer_nonzerop (fold_build2 (GE_EXPR, boolean_type_node, @3, @4)))
5661 /* These patterns should be after min/max detection as simplifications
5662 of `(type)(zero_one ==/!= 0)` to `(type)(zero_one)`
5663 and `(type)(zero_one^1)` are not done yet. See PR 110637.
5664 Even without those, reaching min/max/and/ior faster is better. */
5666 (cond @0 zero_one_valued_p@1 zero_one_valued_p@2)
5668 /* bool0 ? bool1 : 0 -> bool0 & bool1 */
5669 (if (integer_zerop (@2))
5670 (bit_and (convert @0) @1))
5671 /* bool0 ? 0 : bool2 -> (bool0^1) & bool2 */
5672 (if (integer_zerop (@1))
5673 (bit_and (bit_xor (convert @0) { build_one_cst (type); } ) @2))
5674 /* bool0 ? 1 : bool2 -> bool0 | bool2 */
5675 (if (integer_onep (@1))
5676 (bit_ior (convert @0) @2))
5677 /* bool0 ? bool1 : 1 -> (bool0^1) | bool1 */
5678 (if (integer_onep (@2))
5679 (bit_ior (bit_xor (convert @0) @2) @1))
5684 /* X != C1 ? -X : C2 simplifies to -X when -C1 == C2. */
5686 (cond (ne @0 INTEGER_CST@1) (negate@3 @0) INTEGER_CST@2)
5687 (if (!TYPE_SATURATING (type)
5688 && (TYPE_OVERFLOW_WRAPS (type)
5689 || !wi::only_sign_bit_p (wi::to_wide (@1)))
5690 && wi::eq_p (wi::neg (wi::to_wide (@1)), wi::to_wide (@2)))
5693 /* X != C1 ? ~X : C2 simplifies to ~X when ~C1 == C2. */
5695 (cond (ne @0 INTEGER_CST@1) (bit_not@3 @0) INTEGER_CST@2)
5696 (if (wi::eq_p (wi::bit_not (wi::to_wide (@1)), wi::to_wide (@2)))
5699 /* X != C1 ? abs(X) : C2 simplifies to abs(x) when abs(C1) == C2. */
5702 (cond (ne @0 INTEGER_CST@1) (op@3 @0) INTEGER_CST@2)
5703 (if (wi::abs (wi::to_wide (@1)) == wi::to_wide (@2))
5704 (if (op != ABSU_EXPR && wi::only_sign_bit_p (wi::to_wide (@1)))
5705 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
5706 (convert (absu:utype @0)))
5709 /* (X + 1) > Y ? -X : 1 simplifies to X >= Y ? -X : 1 when
5710 X is unsigned, as when X + 1 overflows, X is -1, so -X == 1. */
5712 (cond (gt (plus @0 integer_onep) @1) (negate @0) integer_onep@2)
5713 (if (TYPE_UNSIGNED (type))
5714 (cond (ge @0 @1) (negate @0) @2)))
5716 (for cnd (cond vec_cond)
5717 /* A ? B : (A ? X : C) -> A ? B : C. */
5719 (cnd @0 (cnd @0 @1 @2) @3)
5722 (cnd @0 @1 (cnd @0 @2 @3))
5724 /* A ? B : (!A ? C : X) -> A ? B : C. */
5725 /* ??? This matches embedded conditions open-coded because genmatch
5726 would generate matching code for conditions in separate stmts only.
5727 The following is still important to merge then and else arm cases
5728 from if-conversion. */
5730 (cnd @0 @1 (cnd @2 @3 @4))
5731 (if (inverse_conditions_p (@0, @2))
5734 (cnd @0 (cnd @1 @2 @3) @4)
5735 (if (inverse_conditions_p (@0, @1))
5738 /* A ? B : B -> B. */
5743 /* !A ? B : C -> A ? C : B. */
5745 (cnd (logical_inverted_value truth_valued_p@0) @1 @2)
5748 /* abs/negative simplifications moved from fold_cond_expr_with_comparison.
5750 None of these transformations work for modes with signed
5751 zeros. If A is +/-0, the first two transformations will
5752 change the sign of the result (from +0 to -0, or vice
5753 versa). The last four will fix the sign of the result,
5754 even though the original expressions could be positive or
5755 negative, depending on the sign of A.
5757 Note that all these transformations are correct if A is
5758 NaN, since the two alternatives (A and -A) are also NaNs. */
5760 (for cnd (cond vec_cond)
5761 /* A == 0 ? A : -A same as -A */
5764 (cnd (cmp @0 zerop) @2 (negate@1 @2))
5765 (if (!HONOR_SIGNED_ZEROS (type)
5766 && bitwise_equal_p (@0, @2))
5769 (cnd (cmp @0 zerop) zerop (negate@1 @2))
5770 (if (!HONOR_SIGNED_ZEROS (type)
5771 && bitwise_equal_p (@0, @2))
5774 /* A != 0 ? A : -A same as A */
5777 (cnd (cmp @0 zerop) @1 (negate @1))
5778 (if (!HONOR_SIGNED_ZEROS (type)
5779 && bitwise_equal_p (@0, @1))
5782 (cnd (cmp @0 zerop) @1 integer_zerop)
5783 (if (!HONOR_SIGNED_ZEROS (type)
5784 && bitwise_equal_p (@0, @1))
5787 /* A >=/> 0 ? A : -A same as abs (A) */
5790 (cnd (cmp @0 zerop) @1 (negate @1))
5791 (if (!HONOR_SIGNED_ZEROS (TREE_TYPE(@0))
5792 && !TYPE_UNSIGNED (TREE_TYPE(@0))
5793 && bitwise_equal_p (@0, @1))
5794 (if (TYPE_UNSIGNED (type))
5797 /* A <=/< 0 ? A : -A same as -abs (A) */
5800 (cnd (cmp @0 zerop) @1 (negate @1))
5801 (if (!HONOR_SIGNED_ZEROS (TREE_TYPE(@0))
5802 && !TYPE_UNSIGNED (TREE_TYPE(@0))
5803 && bitwise_equal_p (@0, @1))
5804 (if ((ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
5805 && !TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
5806 || TYPE_UNSIGNED (type))
5808 tree utype = unsigned_type_for (TREE_TYPE(@0));
5810 (convert (negate (absu:utype @0))))
5811 (negate (abs @0)))))
5814 /* (A - B) == 0 ? (A - B) : (B - A) same as (B - A) */
5817 (cnd (cmp (minus@0 @1 @2) zerop) @0 (minus@3 @2 @1))
5818 (if (!HONOR_SIGNED_ZEROS (type))
5821 (cnd (cmp (minus@0 @1 @2) integer_zerop) integer_zerop (minus@3 @2 @1))
5824 /* (A - B) != 0 ? (A - B) : (B - A) same as (A - B) */
5827 (cnd (cmp (minus@0 @1 @2) zerop) @0 (minus @2 @1))
5828 (if (!HONOR_SIGNED_ZEROS (type))
5831 (cnd (cmp (minus@0 @1 @2) integer_zerop) @0 integer_zerop)
5834 /* (A - B) >=/> 0 ? (A - B) : (B - A) same as abs (A - B) */
5837 (cnd (cmp (minus@0 @1 @2) zerop) @0 (minus @2 @1))
5838 (if (!HONOR_SIGNED_ZEROS (type)
5839 && !TYPE_UNSIGNED (type))
5841 /* (A - B) <=/< 0 ? (A - B) : (B - A) same as -abs (A - B) */
5844 (cnd (cmp (minus@0 @1 @2) zerop) @0 (minus @2 @1))
5845 (if (!HONOR_SIGNED_ZEROS (type)
5846 && !TYPE_UNSIGNED (type))
5847 (if (ANY_INTEGRAL_TYPE_P (type)
5848 && !TYPE_OVERFLOW_WRAPS (type))
5850 tree utype = unsigned_type_for (type);
5852 (convert (negate (absu:utype @0))))
5853 (negate (abs @0)))))
5857 /* -(type)!A -> (type)A - 1. */
5859 (negate (convert?:s (logical_inverted_value:s @0)))
5860 (if (INTEGRAL_TYPE_P (type)
5861 && TREE_CODE (type) != BOOLEAN_TYPE
5862 && TYPE_PRECISION (type) > 1
5863 && TREE_CODE (@0) == SSA_NAME
5864 && ssa_name_has_boolean_range (@0))
5865 (plus (convert:type @0) { build_all_ones_cst (type); })))
5867 /* A + (B vcmp C ? 1 : 0) -> A - (B vcmp C ? -1 : 0), since vector comparisons
5868 return all -1 or all 0 results. */
5869 /* ??? We could instead convert all instances of the vec_cond to negate,
5870 but that isn't necessarily a win on its own. */
5872 (plus:c @3 (view_convert? (vec_cond:s @0 integer_each_onep@1 integer_zerop@2)))
5873 (if (VECTOR_TYPE_P (type)
5874 && known_eq (TYPE_VECTOR_SUBPARTS (type),
5875 TYPE_VECTOR_SUBPARTS (TREE_TYPE (@1)))
5876 && (TYPE_MODE (TREE_TYPE (type))
5877 == TYPE_MODE (TREE_TYPE (TREE_TYPE (@1)))))
5878 (minus @3 (view_convert (vec_cond @0 (negate @1) @2)))))
5880 /* ... likewise A - (B vcmp C ? 1 : 0) -> A + (B vcmp C ? -1 : 0). */
5882 (minus @3 (view_convert? (vec_cond:s @0 integer_each_onep@1 integer_zerop@2)))
5883 (if (VECTOR_TYPE_P (type)
5884 && known_eq (TYPE_VECTOR_SUBPARTS (type),
5885 TYPE_VECTOR_SUBPARTS (TREE_TYPE (@1)))
5886 && (TYPE_MODE (TREE_TYPE (type))
5887 == TYPE_MODE (TREE_TYPE (TREE_TYPE (@1)))))
5888 (plus @3 (view_convert (vec_cond @0 (negate @1) @2)))))
5891 /* Simplifications of comparisons. */
5893 /* See if we can reduce the magnitude of a constant involved in a
5894 comparison by changing the comparison code. This is a canonicalization
5895 formerly done by maybe_canonicalize_comparison_1. */
5899 (cmp @0 uniform_integer_cst_p@1)
5900 (with { tree cst = uniform_integer_cst_p (@1); }
5901 (if (tree_int_cst_sgn (cst) == -1)
5902 (acmp @0 { build_uniform_cst (TREE_TYPE (@1),
5903 wide_int_to_tree (TREE_TYPE (cst),
5909 (cmp @0 uniform_integer_cst_p@1)
5910 (with { tree cst = uniform_integer_cst_p (@1); }
5911 (if (tree_int_cst_sgn (cst) == 1)
5912 (acmp @0 { build_uniform_cst (TREE_TYPE (@1),
5913 wide_int_to_tree (TREE_TYPE (cst),
5914 wi::to_wide (cst) - 1)); })))))
5916 /* We can simplify a logical negation of a comparison to the
5917 inverted comparison. As we cannot compute an expression
5918 operator using invert_tree_comparison we have to simulate
5919 that with expression code iteration. */
5920 (for cmp (tcc_comparison)
5921 icmp (inverted_tcc_comparison)
5922 ncmp (inverted_tcc_comparison_with_nans)
5923 /* Ideally we'd like to combine the following two patterns
5924 and handle some more cases by using
5925 (logical_inverted_value (cmp @0 @1))
5926 here but for that genmatch would need to "inline" that.
5927 For now implement what forward_propagate_comparison did. */
5929 (bit_not (cmp @0 @1))
5930 (if (VECTOR_TYPE_P (type)
5931 || (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1))
5932 /* Comparison inversion may be impossible for trapping math,
5933 invert_tree_comparison will tell us. But we can't use
5934 a computed operator in the replacement tree thus we have
5935 to play the trick below. */
5936 (with { enum tree_code ic = invert_tree_comparison
5937 (cmp, HONOR_NANS (@0)); }
5943 (bit_xor (cmp @0 @1) integer_truep)
5944 (with { enum tree_code ic = invert_tree_comparison
5945 (cmp, HONOR_NANS (@0)); }
5950 /* The following bits are handled by fold_binary_op_with_conditional_arg. */
5952 (ne (cmp@2 @0 @1) integer_zerop)
5953 (if (types_match (type, TREE_TYPE (@2)))
5956 (eq (cmp@2 @0 @1) integer_truep)
5957 (if (types_match (type, TREE_TYPE (@2)))
5960 (ne (cmp@2 @0 @1) integer_truep)
5961 (if (types_match (type, TREE_TYPE (@2)))
5962 (with { enum tree_code ic = invert_tree_comparison
5963 (cmp, HONOR_NANS (@0)); }
5969 (eq (cmp@2 @0 @1) integer_zerop)
5970 (if (types_match (type, TREE_TYPE (@2)))
5971 (with { enum tree_code ic = invert_tree_comparison
5972 (cmp, HONOR_NANS (@0)); }
5978 /* Transform comparisons of the form X - Y CMP 0 to X CMP Y.
5979 ??? The transformation is valid for the other operators if overflow
5980 is undefined for the type, but performing it here badly interacts
5981 with the transformation in fold_cond_expr_with_comparison which
5982 attempts to synthetize ABS_EXPR. */
5984 (for sub (minus pointer_diff)
5986 (cmp (sub@2 @0 @1) integer_zerop)
5987 (if (single_use (@2))
5990 /* Simplify (x < 0) ^ (y < 0) to (x ^ y) < 0 and
5991 (x >= 0) ^ (y >= 0) to (x ^ y) < 0. */
5994 (bit_xor (cmp:s @0 integer_zerop) (cmp:s @1 integer_zerop))
5995 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
5996 && !TYPE_UNSIGNED (TREE_TYPE (@0))
5997 && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
5998 (lt (bit_xor @0 @1) { build_zero_cst (TREE_TYPE (@0)); }))))
5999 /* Simplify (x < 0) ^ (y >= 0) to (x ^ y) >= 0 and
6000 (x >= 0) ^ (y < 0) to (x ^ y) >= 0. */
6002 (bit_xor:c (lt:s @0 integer_zerop) (ge:s @1 integer_zerop))
6003 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6004 && !TYPE_UNSIGNED (TREE_TYPE (@0))
6005 && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
6006 (ge (bit_xor @0 @1) { build_zero_cst (TREE_TYPE (@0)); })))
6008 /* Transform comparisons of the form X * C1 CMP 0 to X CMP 0 in the
6009 signed arithmetic case. That form is created by the compiler
6010 often enough for folding it to be of value. One example is in
6011 computing loop trip counts after Operator Strength Reduction. */
6012 (for cmp (simple_comparison)
6013 scmp (swapped_simple_comparison)
6015 (cmp (mult@3 @0 INTEGER_CST@1) integer_zerop@2)
6016 /* Handle unfolded multiplication by zero. */
6017 (if (integer_zerop (@1))
6019 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
6020 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
6022 /* If @1 is negative we swap the sense of the comparison. */
6023 (if (tree_int_cst_sgn (@1) < 0)
6027 /* For integral types with undefined overflow fold
6028 x * C1 == C2 into x == C2 / C1 or false.
6029 If overflow wraps and C1 is odd, simplify to x == C2 / C1 in the ring
6033 (cmp (mult @0 INTEGER_CST@1) INTEGER_CST@2)
6034 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6035 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
6036 && wi::to_wide (@1) != 0)
6037 (with { widest_int quot; }
6038 (if (wi::multiple_of_p (wi::to_widest (@2), wi::to_widest (@1),
6039 TYPE_SIGN (TREE_TYPE (@0)), "))
6040 (cmp @0 { wide_int_to_tree (TREE_TYPE (@0), quot); })
6041 { constant_boolean_node (cmp == NE_EXPR, type); }))
6042 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6043 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))
6044 && (wi::bit_and (wi::to_wide (@1), 1) == 1))
6047 tree itype = TREE_TYPE (@0);
6048 int p = TYPE_PRECISION (itype);
6049 wide_int m = wi::one (p + 1) << p;
6050 wide_int a = wide_int::from (wi::to_wide (@1), p + 1, UNSIGNED);
6051 wide_int i = wide_int::from (wi::mod_inv (a, m),
6052 p, TYPE_SIGN (itype));
6053 wide_int_to_tree (itype, wi::mul (i, wi::to_wide (@2)));
6056 /* Simplify comparison of something with itself. For IEEE
6057 floating-point, we can only do some of these simplifications. */
6061 (if (! FLOAT_TYPE_P (TREE_TYPE (@0))
6062 || ! tree_expr_maybe_nan_p (@0))
6063 { constant_boolean_node (true, type); }
6065 /* With -ftrapping-math conversion to EQ loses an exception. */
6066 && (! FLOAT_TYPE_P (TREE_TYPE (@0))
6067 || ! flag_trapping_math))
6073 || ! FLOAT_TYPE_P (TREE_TYPE (@0))
6074 || ! tree_expr_maybe_nan_p (@0))
6075 { constant_boolean_node (false, type); })))
6076 (for cmp (unle unge uneq)
6079 { constant_boolean_node (true, type); }))
6080 (for cmp (unlt ungt)
6086 (if (!flag_trapping_math || !tree_expr_maybe_nan_p (@0))
6087 { constant_boolean_node (false, type); }))
6089 /* x == ~x -> false */
6090 /* x != ~x -> true */
6093 (cmp:c @0 (bit_not @0))
6094 { constant_boolean_node (cmp == NE_EXPR, type); }))
6096 /* Fold ~X op ~Y as Y op X. */
6097 (for cmp (simple_comparison)
6099 (cmp (nop_convert1?@4 (bit_not@2 @0)) (nop_convert2? (bit_not@3 @1)))
6100 (if (single_use (@2) && single_use (@3))
6101 (with { tree otype = TREE_TYPE (@4); }
6102 (cmp (convert:otype @1) (convert:otype @0))))))
6104 /* Fold ~X op C as X op' ~C, where op' is the swapped comparison. */
6105 (for cmp (simple_comparison)
6106 scmp (swapped_simple_comparison)
6108 (cmp (nop_convert? (bit_not@2 @0)) CONSTANT_CLASS_P@1)
6109 (if (single_use (@2)
6110 && (TREE_CODE (@1) == INTEGER_CST || TREE_CODE (@1) == VECTOR_CST))
6111 (with { tree otype = TREE_TYPE (@1); }
6112 (scmp (convert:otype @0) (bit_not @1))))))
6114 (for cmp (simple_comparison)
6117 /* IEEE doesn't distinguish +0 and -0 in comparisons. */
6119 /* a CMP (-0) -> a CMP 0 */
6120 (if (REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@1)))
6121 (cmp @0 { build_real (TREE_TYPE (@1), dconst0); }))
6122 /* (-0) CMP b -> 0 CMP b. */
6123 (if (TREE_CODE (@0) == REAL_CST
6124 && REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@0)))
6125 (cmp { build_real (TREE_TYPE (@0), dconst0); } @1))
6126 /* x != NaN is always true, other ops are always false. */
6127 (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
6128 && (cmp == EQ_EXPR || cmp == NE_EXPR || !flag_trapping_math)
6129 && !tree_expr_signaling_nan_p (@1)
6130 && !tree_expr_maybe_signaling_nan_p (@0))
6131 { constant_boolean_node (cmp == NE_EXPR, type); })
6132 /* NaN != y is always true, other ops are always false. */
6133 (if (TREE_CODE (@0) == REAL_CST
6134 && REAL_VALUE_ISNAN (TREE_REAL_CST (@0))
6135 && (cmp == EQ_EXPR || cmp == NE_EXPR || !flag_trapping_math)
6136 && !tree_expr_signaling_nan_p (@0)
6137 && !tree_expr_signaling_nan_p (@1))
6138 { constant_boolean_node (cmp == NE_EXPR, type); })
6139 /* Fold comparisons against infinity. */
6140 (if (REAL_VALUE_ISINF (TREE_REAL_CST (@1))
6141 && MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (@1))))
6144 REAL_VALUE_TYPE max;
6145 enum tree_code code = cmp;
6146 bool neg = REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1));
6148 code = swap_tree_comparison (code);
6151 /* x > +Inf is always false, if we ignore NaNs or exceptions. */
6152 (if (code == GT_EXPR
6153 && !(HONOR_NANS (@0) && flag_trapping_math))
6154 { constant_boolean_node (false, type); })
6155 (if (code == LE_EXPR)
6156 /* x <= +Inf is always true, if we don't care about NaNs. */
6157 (if (! HONOR_NANS (@0))
6158 { constant_boolean_node (true, type); }
6159 /* x <= +Inf is the same as x == x, i.e. !isnan(x), but this loses
6160 an "invalid" exception. */
6161 (if (!flag_trapping_math)
6163 /* x == +Inf and x >= +Inf are always equal to x > DBL_MAX, but
6164 for == this introduces an exception for x a NaN. */
6165 (if ((code == EQ_EXPR && !(HONOR_NANS (@0) && flag_trapping_math))
6167 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
6169 (lt @0 { build_real (TREE_TYPE (@0), max); })
6170 (gt @0 { build_real (TREE_TYPE (@0), max); }))))
6171 /* x < +Inf is always equal to x <= DBL_MAX. */
6172 (if (code == LT_EXPR)
6173 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
6175 (ge @0 { build_real (TREE_TYPE (@0), max); })
6176 (le @0 { build_real (TREE_TYPE (@0), max); }))))
6177 /* x != +Inf is always equal to !(x > DBL_MAX), but this introduces
6178 an exception for x a NaN so use an unordered comparison. */
6179 (if (code == NE_EXPR)
6180 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
6181 (if (! HONOR_NANS (@0))
6183 (ge @0 { build_real (TREE_TYPE (@0), max); })
6184 (le @0 { build_real (TREE_TYPE (@0), max); }))
6186 (unge @0 { build_real (TREE_TYPE (@0), max); })
6187 (unle @0 { build_real (TREE_TYPE (@0), max); }))))))))))
6189 /* If this is a comparison of a real constant with a PLUS_EXPR
6190 or a MINUS_EXPR of a real constant, we can convert it into a
6191 comparison with a revised real constant as long as no overflow
6192 occurs when unsafe_math_optimizations are enabled. */
6193 (if (flag_unsafe_math_optimizations)
6194 (for op (plus minus)
6196 (cmp (op @0 REAL_CST@1) REAL_CST@2)
6199 tree tem = const_binop (op == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR,
6200 TREE_TYPE (@1), @2, @1);
6202 (if (tem && !TREE_OVERFLOW (tem))
6203 (cmp @0 { tem; }))))))
6205 /* Likewise, we can simplify a comparison of a real constant with
6206 a MINUS_EXPR whose first operand is also a real constant, i.e.
6207 (c1 - x) < c2 becomes x > c1-c2. Reordering is allowed on
6208 floating-point types only if -fassociative-math is set. */
6209 (if (flag_associative_math)
6211 (cmp (minus REAL_CST@0 @1) REAL_CST@2)
6212 (with { tree tem = const_binop (MINUS_EXPR, TREE_TYPE (@1), @0, @2); }
6213 (if (tem && !TREE_OVERFLOW (tem))
6214 (cmp { tem; } @1)))))
6216 /* Fold comparisons against built-in math functions. */
6217 (if (flag_unsafe_math_optimizations && ! flag_errno_math)
6220 (cmp (sq @0) REAL_CST@1)
6222 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
6224 /* sqrt(x) < y is always false, if y is negative. */
6225 (if (cmp == EQ_EXPR || cmp == LT_EXPR || cmp == LE_EXPR)
6226 { constant_boolean_node (false, type); })
6227 /* sqrt(x) > y is always true, if y is negative and we
6228 don't care about NaNs, i.e. negative values of x. */
6229 (if (cmp == NE_EXPR || !HONOR_NANS (@0))
6230 { constant_boolean_node (true, type); })
6231 /* sqrt(x) > y is the same as x >= 0, if y is negative. */
6232 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })))
6233 (if (real_equal (TREE_REAL_CST_PTR (@1), &dconst0))
6235 /* sqrt(x) < 0 is always false. */
6236 (if (cmp == LT_EXPR)
6237 { constant_boolean_node (false, type); })
6238 /* sqrt(x) >= 0 is always true if we don't care about NaNs. */
6239 (if (cmp == GE_EXPR && !HONOR_NANS (@0))
6240 { constant_boolean_node (true, type); })
6241 /* sqrt(x) <= 0 -> x == 0. */
6242 (if (cmp == LE_EXPR)
6244 /* Otherwise sqrt(x) cmp 0 -> x cmp 0. Here cmp can be >=, >,
6245 == or !=. In the last case:
6247 (sqrt(x) != 0) == (NaN != 0) == true == (x != 0)
6249 if x is negative or NaN. Due to -funsafe-math-optimizations,
6250 the results for other x follow from natural arithmetic. */
6252 (if ((cmp == LT_EXPR
6256 && !REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
6257 /* Give up for -frounding-math. */
6258 && !HONOR_SIGN_DEPENDENT_ROUNDING (TREE_TYPE (@0)))
6262 enum tree_code ncmp = cmp;
6263 const real_format *fmt
6264 = REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (@0)));
6265 real_arithmetic (&c2, MULT_EXPR,
6266 &TREE_REAL_CST (@1), &TREE_REAL_CST (@1));
6267 real_convert (&c2, fmt, &c2);
6268 /* See PR91734: if c2 is inexact and sqrt(c2) < c (or sqrt(c2) >= c),
6269 then change LT_EXPR into LE_EXPR or GE_EXPR into GT_EXPR. */
6270 if (!REAL_VALUE_ISINF (c2))
6272 tree c3 = fold_const_call (CFN_SQRT, TREE_TYPE (@0),
6273 build_real (TREE_TYPE (@0), c2));
6274 if (c3 == NULL_TREE || TREE_CODE (c3) != REAL_CST)
6276 else if ((cmp == LT_EXPR || cmp == GE_EXPR)
6277 && real_less (&TREE_REAL_CST (c3), &TREE_REAL_CST (@1)))
6278 ncmp = cmp == LT_EXPR ? LE_EXPR : GT_EXPR;
6279 else if ((cmp == LE_EXPR || cmp == GT_EXPR)
6280 && real_less (&TREE_REAL_CST (@1), &TREE_REAL_CST (c3)))
6281 ncmp = cmp == LE_EXPR ? LT_EXPR : GE_EXPR;
6284 /* With rounding to even, sqrt of up to 3 different values
6285 gives the same normal result, so in some cases c2 needs
6287 REAL_VALUE_TYPE c2alt, tow;
6288 if (cmp == LT_EXPR || cmp == GE_EXPR)
6292 real_nextafter (&c2alt, fmt, &c2, &tow);
6293 real_convert (&c2alt, fmt, &c2alt);
6294 if (REAL_VALUE_ISINF (c2alt))
6298 c3 = fold_const_call (CFN_SQRT, TREE_TYPE (@0),
6299 build_real (TREE_TYPE (@0), c2alt));
6300 if (c3 == NULL_TREE || TREE_CODE (c3) != REAL_CST)
6302 else if (real_equal (&TREE_REAL_CST (c3),
6303 &TREE_REAL_CST (@1)))
6309 (if (cmp == GT_EXPR || cmp == GE_EXPR)
6310 (if (REAL_VALUE_ISINF (c2))
6311 /* sqrt(x) > y is x == +Inf, when y is very large. */
6312 (if (HONOR_INFINITIES (@0))
6313 (eq @0 { build_real (TREE_TYPE (@0), c2); })
6314 { constant_boolean_node (false, type); })
6315 /* sqrt(x) > c is the same as x > c*c. */
6316 (if (ncmp != ERROR_MARK)
6317 (if (ncmp == GE_EXPR)
6318 (ge @0 { build_real (TREE_TYPE (@0), c2); })
6319 (gt @0 { build_real (TREE_TYPE (@0), c2); }))))
6320 /* else if (cmp == LT_EXPR || cmp == LE_EXPR) */
6321 (if (REAL_VALUE_ISINF (c2))
6323 /* sqrt(x) < y is always true, when y is a very large
6324 value and we don't care about NaNs or Infinities. */
6325 (if (! HONOR_NANS (@0) && ! HONOR_INFINITIES (@0))
6326 { constant_boolean_node (true, type); })
6327 /* sqrt(x) < y is x != +Inf when y is very large and we
6328 don't care about NaNs. */
6329 (if (! HONOR_NANS (@0))
6330 (ne @0 { build_real (TREE_TYPE (@0), c2); }))
6331 /* sqrt(x) < y is x >= 0 when y is very large and we
6332 don't care about Infinities. */
6333 (if (! HONOR_INFINITIES (@0))
6334 (ge @0 { build_real (TREE_TYPE (@0), dconst0); }))
6335 /* sqrt(x) < y is x >= 0 && x != +Inf, when y is large. */
6338 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
6339 (ne @0 { build_real (TREE_TYPE (@0), c2); }))))
6340 /* sqrt(x) < c is the same as x < c*c, if we ignore NaNs. */
6341 (if (ncmp != ERROR_MARK && ! HONOR_NANS (@0))
6342 (if (ncmp == LT_EXPR)
6343 (lt @0 { build_real (TREE_TYPE (@0), c2); })
6344 (le @0 { build_real (TREE_TYPE (@0), c2); }))
6345 /* sqrt(x) < c is the same as x >= 0 && x < c*c. */
6346 (if (ncmp != ERROR_MARK && GENERIC)
6347 (if (ncmp == LT_EXPR)
6349 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
6350 (lt @0 { build_real (TREE_TYPE (@0), c2); }))
6352 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
6353 (le @0 { build_real (TREE_TYPE (@0), c2); })))))))))))
6354 /* Transform sqrt(x) cmp sqrt(y) -> x cmp y. */
6356 (cmp (sq @0) (sq @1))
6357 (if (! HONOR_NANS (@0))
6360 /* Optimize various special cases of (FTYPE) N CMP (FTYPE) M. */
6361 (for cmp (lt le eq ne ge gt unordered ordered unlt unle ungt unge uneq ltgt)
6362 icmp (lt le eq ne ge gt unordered ordered lt le gt ge eq ne)
6364 (cmp (float@0 @1) (float @2))
6365 (if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (@0))
6366 && ! DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0)))
6369 format_helper fmt (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (@0))));
6370 tree type1 = TREE_TYPE (@1);
6371 bool type1_signed_p = TYPE_SIGN (type1) == SIGNED;
6372 tree type2 = TREE_TYPE (@2);
6373 bool type2_signed_p = TYPE_SIGN (type2) == SIGNED;
6375 (if (fmt.can_represent_integral_type_p (type1)
6376 && fmt.can_represent_integral_type_p (type2))
6377 (if (cmp == ORDERED_EXPR || cmp == UNORDERED_EXPR)
6378 { constant_boolean_node (cmp == ORDERED_EXPR, type); }
6379 (if (TYPE_PRECISION (type1) > TYPE_PRECISION (type2)
6380 && type1_signed_p >= type2_signed_p)
6381 (icmp @1 (convert @2))
6382 (if (TYPE_PRECISION (type1) < TYPE_PRECISION (type2)
6383 && type1_signed_p <= type2_signed_p)
6384 (icmp (convert:type2 @1) @2)
6385 (if (TYPE_PRECISION (type1) == TYPE_PRECISION (type2)
6386 && type1_signed_p == type2_signed_p)
6387 (icmp @1 @2))))))))))
6389 /* Optimize various special cases of (FTYPE) N CMP CST. */
6390 (for cmp (lt le eq ne ge gt)
6391 icmp (le le eq ne ge ge)
6393 (cmp (float @0) REAL_CST@1)
6394 (if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (@1))
6395 && ! DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1)))
6398 tree itype = TREE_TYPE (@0);
6399 format_helper fmt (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (@1))));
6400 const REAL_VALUE_TYPE *cst = TREE_REAL_CST_PTR (@1);
6401 /* Be careful to preserve any potential exceptions due to
6402 NaNs. qNaNs are ok in == or != context.
6403 TODO: relax under -fno-trapping-math or
6404 -fno-signaling-nans. */
6406 = real_isnan (cst) && (cst->signalling
6407 || (cmp != EQ_EXPR && cmp != NE_EXPR));
6409 /* TODO: allow non-fitting itype and SNaNs when
6410 -fno-trapping-math. */
6411 (if (fmt.can_represent_integral_type_p (itype) && ! exception_p)
6414 signop isign = TYPE_SIGN (itype);
6415 REAL_VALUE_TYPE imin, imax;
6416 real_from_integer (&imin, fmt, wi::min_value (itype), isign);
6417 real_from_integer (&imax, fmt, wi::max_value (itype), isign);
6419 REAL_VALUE_TYPE icst;
6420 if (cmp == GT_EXPR || cmp == GE_EXPR)
6421 real_ceil (&icst, fmt, cst);
6422 else if (cmp == LT_EXPR || cmp == LE_EXPR)
6423 real_floor (&icst, fmt, cst);
6425 real_trunc (&icst, fmt, cst);
6427 bool cst_int_p = !real_isnan (cst) && real_identical (&icst, cst);
6429 bool overflow_p = false;
6431 = real_to_integer (&icst, &overflow_p, TYPE_PRECISION (itype));
6434 /* Optimize cases when CST is outside of ITYPE's range. */
6435 (if (real_compare (LT_EXPR, cst, &imin))
6436 { constant_boolean_node (cmp == GT_EXPR || cmp == GE_EXPR || cmp == NE_EXPR,
6438 (if (real_compare (GT_EXPR, cst, &imax))
6439 { constant_boolean_node (cmp == LT_EXPR || cmp == LE_EXPR || cmp == NE_EXPR,
6441 /* Remove cast if CST is an integer representable by ITYPE. */
6443 (cmp @0 { gcc_assert (!overflow_p);
6444 wide_int_to_tree (itype, icst_val); })
6446 /* When CST is fractional, optimize
6447 (FTYPE) N == CST -> 0
6448 (FTYPE) N != CST -> 1. */
6449 (if (cmp == EQ_EXPR || cmp == NE_EXPR)
6450 { constant_boolean_node (cmp == NE_EXPR, type); })
6451 /* Otherwise replace with sensible integer constant. */
6454 gcc_checking_assert (!overflow_p);
6456 (icmp @0 { wide_int_to_tree (itype, icst_val); })))))))))
6458 /* Fold A /[ex] B CMP C to A CMP B * C. */
6461 (cmp (exact_div @0 @1) INTEGER_CST@2)
6462 (if (!integer_zerop (@1))
6463 (if (wi::to_wide (@2) == 0)
6465 (if (TREE_CODE (@1) == INTEGER_CST)
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 (cmp == NE_EXPR, type); }
6474 (cmp @0 { wide_int_to_tree (TREE_TYPE (@0), prod); }))))))))
6475 (for cmp (lt le gt ge)
6477 (cmp (exact_div @0 INTEGER_CST@1) INTEGER_CST@2)
6478 (if (wi::gt_p (wi::to_wide (@1), 0, TYPE_SIGN (TREE_TYPE (@1))))
6481 wi::overflow_type ovf;
6482 wide_int prod = wi::mul (wi::to_wide (@2), wi::to_wide (@1),
6483 TYPE_SIGN (TREE_TYPE (@1)), &ovf);
6486 { constant_boolean_node (wi::lt_p (wi::to_wide (@2), 0,
6487 TYPE_SIGN (TREE_TYPE (@2)))
6488 != (cmp == LT_EXPR || cmp == LE_EXPR), type); }
6489 (cmp @0 { wide_int_to_tree (TREE_TYPE (@0), prod); }))))))
6491 /* Fold (size_t)(A /[ex] B) CMP C to (size_t)A CMP (size_t)B * C or A CMP' 0.
6493 For small C (less than max/B), this is (size_t)A CMP (size_t)B * C.
6494 For large C (more than min/B+2^size), this is also true, with the
6495 multiplication computed modulo 2^size.
6496 For intermediate C, this just tests the sign of A. */
6497 (for cmp (lt le gt ge)
6500 (cmp (convert (exact_div @0 INTEGER_CST@1)) INTEGER_CST@2)
6501 (if (tree_nop_conversion_p (TREE_TYPE (@0), TREE_TYPE (@2))
6502 && TYPE_UNSIGNED (TREE_TYPE (@2)) && !TYPE_UNSIGNED (TREE_TYPE (@0))
6503 && wi::gt_p (wi::to_wide (@1), 0, TYPE_SIGN (TREE_TYPE (@1))))
6506 tree utype = TREE_TYPE (@2);
6507 wide_int denom = wi::to_wide (@1);
6508 wide_int right = wi::to_wide (@2);
6509 wide_int smax = wi::sdiv_trunc (wi::max_value (TREE_TYPE (@0)), denom);
6510 wide_int smin = wi::sdiv_trunc (wi::min_value (TREE_TYPE (@0)), denom);
6511 bool small = wi::leu_p (right, smax);
6512 bool large = wi::geu_p (right, smin);
6514 (if (small || large)
6515 (cmp (convert:utype @0) (mult @2 (convert @1)))
6516 (cmp2 @0 { build_zero_cst (TREE_TYPE (@0)); }))))))
6518 /* Unordered tests if either argument is a NaN. */
6520 (bit_ior (unordered @0 @0) (unordered @1 @1))
6521 (if (types_match (@0, @1))
6524 (bit_and (ordered @0 @0) (ordered @1 @1))
6525 (if (types_match (@0, @1))
6528 (bit_ior:c (unordered @0 @0) (unordered:c@2 @0 @1))
6531 (bit_and:c (ordered @0 @0) (ordered:c@2 @0 @1))
6534 /* A & (2**N - 1) <= 2**K - 1 -> A & (2**N - 2**K) == 0
6535 A & (2**N - 1) > 2**K - 1 -> A & (2**N - 2**K) != 0
6537 Note that comparisons
6538 A & (2**N - 1) < 2**K -> A & (2**N - 2**K) == 0
6539 A & (2**N - 1) >= 2**K -> A & (2**N - 2**K) != 0
6540 will be canonicalized to above so there's no need to
6547 (cmp (bit_and@0 @1 INTEGER_CST@2) INTEGER_CST@3)
6548 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)))
6551 tree ty = TREE_TYPE (@0);
6552 unsigned prec = TYPE_PRECISION (ty);
6553 wide_int mask = wi::to_wide (@2, prec);
6554 wide_int rhs = wi::to_wide (@3, prec);
6555 signop sgn = TYPE_SIGN (ty);
6557 (if ((mask & (mask + 1)) == 0 && wi::gt_p (rhs, 0, sgn)
6558 && (rhs & (rhs + 1)) == 0 && wi::ge_p (mask, rhs, sgn))
6559 (eqcmp (bit_and @1 { wide_int_to_tree (ty, mask - rhs); })
6560 { build_zero_cst (ty); }))))))
6562 /* -A CMP -B -> B CMP A. */
6563 (for cmp (tcc_comparison)
6564 scmp (swapped_tcc_comparison)
6566 (cmp (negate @0) (negate @1))
6567 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
6568 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
6571 || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))))
6574 (cmp (negate @0) CONSTANT_CLASS_P@1)
6575 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
6576 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
6579 || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))))
6580 (with { tree tem = const_unop (NEGATE_EXPR, TREE_TYPE (@0), @1); }
6581 (if (tem && !TREE_OVERFLOW (tem))
6582 (scmp @0 { tem; }))))))
6584 /* Convert ABS[U]_EXPR<x> == 0 or ABS[U]_EXPR<x> != 0 to x == 0 or x != 0. */
6588 (eqne (op @0) zerop@1)
6589 (eqne @0 { build_zero_cst (TREE_TYPE (@0)); }))))
6591 /* From fold_sign_changed_comparison and fold_widened_comparison.
6592 FIXME: the lack of symmetry is disturbing. */
6593 (for cmp (simple_comparison)
6595 (cmp (convert@0 @00) (convert?@1 @10))
6596 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6597 /* Disable this optimization if we're casting a function pointer
6598 type on targets that require function pointer canonicalization. */
6599 && !(targetm.have_canonicalize_funcptr_for_compare ()
6600 && ((POINTER_TYPE_P (TREE_TYPE (@00))
6601 && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@00))))
6602 || (POINTER_TYPE_P (TREE_TYPE (@10))
6603 && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@10))))))
6605 (if (TYPE_PRECISION (TREE_TYPE (@00)) == TYPE_PRECISION (TREE_TYPE (@0))
6606 && (TREE_CODE (@10) == INTEGER_CST
6608 && (TYPE_UNSIGNED (TREE_TYPE (@00)) == TYPE_UNSIGNED (TREE_TYPE (@0))
6611 && !POINTER_TYPE_P (TREE_TYPE (@00))
6612 /* (int)bool:32 != (int)uint is not the same as
6613 bool:32 != (bool:32)uint since boolean types only have two valid
6614 values independent of their precision. */
6615 && (TREE_CODE (TREE_TYPE (@00)) != BOOLEAN_TYPE
6616 || TREE_CODE (TREE_TYPE (@10)) == BOOLEAN_TYPE))
6617 /* ??? The special-casing of INTEGER_CST conversion was in the original
6618 code and here to avoid a spurious overflow flag on the resulting
6619 constant which fold_convert produces. */
6620 (if (TREE_CODE (@1) == INTEGER_CST)
6621 (cmp @00 { force_fit_type (TREE_TYPE (@00),
6622 wide_int::from (wi::to_wide (@1),
6623 MAX (TYPE_PRECISION (TREE_TYPE (@1)),
6624 TYPE_PRECISION (TREE_TYPE (@00))),
6625 TYPE_SIGN (TREE_TYPE (@1))),
6626 0, TREE_OVERFLOW (@1)); })
6627 (cmp @00 (convert @1)))
6629 (if (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (TREE_TYPE (@00)))
6630 /* If possible, express the comparison in the shorter mode. */
6631 (if ((cmp == EQ_EXPR || cmp == NE_EXPR
6632 || TYPE_UNSIGNED (TREE_TYPE (@0)) == TYPE_UNSIGNED (TREE_TYPE (@00))
6633 || (!TYPE_UNSIGNED (TREE_TYPE (@0))
6634 && TYPE_UNSIGNED (TREE_TYPE (@00))))
6635 && (types_match (TREE_TYPE (@10), TREE_TYPE (@00))
6636 || ((TYPE_PRECISION (TREE_TYPE (@00))
6637 >= TYPE_PRECISION (TREE_TYPE (@10)))
6638 && (TYPE_UNSIGNED (TREE_TYPE (@00))
6639 == TYPE_UNSIGNED (TREE_TYPE (@10))))
6640 || (TREE_CODE (@10) == INTEGER_CST
6641 && INTEGRAL_TYPE_P (TREE_TYPE (@00))
6642 && int_fits_type_p (@10, TREE_TYPE (@00)))))
6643 (cmp @00 (convert @10))
6644 (if (TREE_CODE (@10) == INTEGER_CST
6645 && INTEGRAL_TYPE_P (TREE_TYPE (@00))
6646 && !int_fits_type_p (@10, TREE_TYPE (@00)))
6649 tree min = lower_bound_in_type (TREE_TYPE (@10), TREE_TYPE (@00));
6650 tree max = upper_bound_in_type (TREE_TYPE (@10), TREE_TYPE (@00));
6651 bool above = integer_nonzerop (const_binop (LT_EXPR, type, max, @10));
6652 bool below = integer_nonzerop (const_binop (LT_EXPR, type, @10, min));
6654 (if (above || below)
6655 (if (cmp == EQ_EXPR || cmp == NE_EXPR)
6656 { constant_boolean_node (cmp == EQ_EXPR ? false : true, type); }
6657 (if (cmp == LT_EXPR || cmp == LE_EXPR)
6658 { constant_boolean_node (above ? true : false, type); }
6659 (if (cmp == GT_EXPR || cmp == GE_EXPR)
6660 { constant_boolean_node (above ? false : true, type); })))))))))
6661 /* Fold (double)float1 CMP (double)float2 into float1 CMP float2. */
6662 (if (FLOAT_TYPE_P (TREE_TYPE (@00))
6663 && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0))
6664 == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@00)))
6665 && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0))
6666 == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@10))))
6669 tree type1 = TREE_TYPE (@10);
6670 if (TREE_CODE (@10) == REAL_CST && !DECIMAL_FLOAT_TYPE_P (type1))
6672 REAL_VALUE_TYPE orig = TREE_REAL_CST (@10);
6673 if (TYPE_PRECISION (type1) > TYPE_PRECISION (float_type_node)
6674 && exact_real_truncate (TYPE_MODE (float_type_node), &orig))
6675 type1 = float_type_node;
6676 if (TYPE_PRECISION (type1) > TYPE_PRECISION (double_type_node)
6677 && exact_real_truncate (TYPE_MODE (double_type_node), &orig))
6678 type1 = double_type_node;
6681 = (element_precision (TREE_TYPE (@00)) > element_precision (type1)
6682 ? TREE_TYPE (@00) : type1);
6684 (if (element_precision (TREE_TYPE (@0)) > element_precision (newtype))
6685 (cmp (convert:newtype @00) (convert:newtype @10))))))))
6690 /* SSA names are canonicalized to 2nd place. */
6691 (cmp addr@0 SSA_NAME@1)
6694 poly_int64 off; tree base;
6695 tree addr = (TREE_CODE (@0) == SSA_NAME
6696 ? gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0)) : @0);
6698 /* A local variable can never be pointed to by
6699 the default SSA name of an incoming parameter. */
6700 (if (SSA_NAME_IS_DEFAULT_DEF (@1)
6701 && TREE_CODE (SSA_NAME_VAR (@1)) == PARM_DECL
6702 && (base = get_base_address (TREE_OPERAND (addr, 0)))
6703 && TREE_CODE (base) == VAR_DECL
6704 && auto_var_in_fn_p (base, current_function_decl))
6705 (if (cmp == NE_EXPR)
6706 { constant_boolean_node (true, type); }
6707 { constant_boolean_node (false, type); })
6708 /* If the address is based on @1 decide using the offset. */
6709 (if ((base = get_addr_base_and_unit_offset (TREE_OPERAND (addr, 0), &off))
6710 && TREE_CODE (base) == MEM_REF
6711 && TREE_OPERAND (base, 0) == @1)
6712 (with { off += mem_ref_offset (base).force_shwi (); }
6713 (if (known_ne (off, 0))
6714 { constant_boolean_node (cmp == NE_EXPR, type); }
6715 (if (known_eq (off, 0))
6716 { constant_boolean_node (cmp == EQ_EXPR, type); }))))))))
6718 /* Equality compare simplifications from fold_binary */
6721 /* If we have (A | C) == D where C & ~D != 0, convert this into 0.
6722 Similarly for NE_EXPR. */
6724 (cmp (convert?@3 (bit_ior @0 INTEGER_CST@1)) INTEGER_CST@2)
6725 (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0))
6726 && wi::bit_and_not (wi::to_wide (@1), wi::to_wide (@2)) != 0)
6727 { constant_boolean_node (cmp == NE_EXPR, type); }))
6729 /* (X ^ Y) == 0 becomes X == Y, and (X ^ Y) != 0 becomes X != Y. */
6731 (cmp (bit_xor @0 @1) integer_zerop)
6734 /* (X ^ Y) == Y becomes X == 0.
6735 Likewise (X ^ Y) == X becomes Y == 0. */
6737 (cmp:c (bit_xor:c @0 @1) @0)
6738 (cmp @1 { build_zero_cst (TREE_TYPE (@1)); }))
6740 /* (X & Y) == X becomes (X & ~Y) == 0. */
6742 (cmp:c (bit_and:c @0 @1) @0)
6743 (cmp (bit_and @0 (bit_not! @1)) { build_zero_cst (TREE_TYPE (@0)); }))
6745 (cmp:c (convert@3 (bit_and (convert@2 @0) INTEGER_CST@1)) (convert @0))
6746 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6747 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
6748 && INTEGRAL_TYPE_P (TREE_TYPE (@3))
6749 && TYPE_PRECISION (TREE_TYPE (@2)) == TYPE_PRECISION (TREE_TYPE (@0))
6750 && TYPE_PRECISION (TREE_TYPE (@3)) > TYPE_PRECISION (TREE_TYPE (@2))
6751 && !wi::neg_p (wi::to_wide (@1)))
6752 (cmp (bit_and @0 (convert (bit_not @1)))
6753 { build_zero_cst (TREE_TYPE (@0)); })))
6755 /* (X | Y) == Y becomes (X & ~Y) == 0. */
6757 (cmp:c (bit_ior:c @0 @1) @1)
6758 (cmp (bit_and @0 (bit_not! @1)) { build_zero_cst (TREE_TYPE (@0)); }))
6760 /* (X ^ C1) op C2 can be rewritten as X op (C1 ^ C2). */
6762 (cmp (convert?@3 (bit_xor @0 INTEGER_CST@1)) INTEGER_CST@2)
6763 (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0)))
6764 (cmp @0 (bit_xor @1 (convert @2)))))
6767 (cmp (nop_convert? @0) integer_zerop)
6768 (if (tree_expr_nonzero_p (@0))
6769 { constant_boolean_node (cmp == NE_EXPR, type); }))
6771 /* (X & C) op (Y & C) into (X ^ Y) & C op 0. */
6773 (cmp (bit_and:cs @0 @2) (bit_and:cs @1 @2))
6774 (cmp (bit_and (bit_xor @0 @1) @2) { build_zero_cst (TREE_TYPE (@2)); })))
6776 /* (X < 0) != (Y < 0) into (X ^ Y) < 0.
6777 (X >= 0) != (Y >= 0) into (X ^ Y) < 0.
6778 (X < 0) == (Y < 0) into (X ^ Y) >= 0.
6779 (X >= 0) == (Y >= 0) into (X ^ Y) >= 0. */
6784 (cmp (sgncmp @0 integer_zerop@2) (sgncmp @1 integer_zerop))
6785 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
6786 && !TYPE_UNSIGNED (TREE_TYPE (@0))
6787 && types_match (@0, @1))
6788 (ncmp (bit_xor @0 @1) @2)))))
6789 /* (X < 0) == (Y >= 0) into (X ^ Y) < 0.
6790 (X < 0) != (Y >= 0) into (X ^ Y) >= 0. */
6794 (cmp:c (lt @0 integer_zerop@2) (ge @1 integer_zerop))
6795 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
6796 && !TYPE_UNSIGNED (TREE_TYPE (@0))
6797 && types_match (@0, @1))
6798 (ncmp (bit_xor @0 @1) @2))))
6800 /* If we have (A & C) == C where C is a power of 2, convert this into
6801 (A & C) != 0. Similarly for NE_EXPR. */
6805 (cmp (bit_and@2 @0 integer_pow2p@1) @1)
6806 (icmp @2 { build_zero_cst (TREE_TYPE (@0)); })))
6809 /* From fold_binary_op_with_conditional_arg handle the case of
6810 rewriting (a ? b : c) > d to a ? (b > d) : (c > d) when the
6811 compares simplify. */
6812 (for cmp (simple_comparison)
6814 (cmp:c (cond @0 @1 @2) @3)
6815 /* Do not move possibly trapping operations into the conditional as this
6816 pessimizes code and causes gimplification issues when applied late. */
6817 (if (!FLOAT_TYPE_P (TREE_TYPE (@3))
6818 || !operation_could_trap_p (cmp, true, false, @3))
6819 (cond @0 (cmp! @1 @3) (cmp! @2 @3)))))
6823 /* x < 0 ? ~y : y into (x >> (prec-1)) ^ y. */
6824 /* x >= 0 ? ~y : y into ~((x >> (prec-1)) ^ y). */
6826 (cond (cmp @0 integer_zerop) (bit_not @1) @1)
6827 (if (INTEGRAL_TYPE_P (type)
6828 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
6829 && !TYPE_UNSIGNED (TREE_TYPE (@0))
6830 && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (type))
6833 tree shifter = build_int_cst (integer_type_node, TYPE_PRECISION (type) - 1);
6835 (if (cmp == LT_EXPR)
6836 (bit_xor (convert (rshift @0 {shifter;})) @1)
6837 (bit_not (bit_xor (convert (rshift @0 {shifter;})) @1))))))
6838 /* x < 0 ? y : ~y into ~((x >> (prec-1)) ^ y). */
6839 /* x >= 0 ? y : ~y into (x >> (prec-1)) ^ y. */
6841 (cond (cmp @0 integer_zerop) @1 (bit_not @1))
6842 (if (INTEGRAL_TYPE_P (type)
6843 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
6844 && !TYPE_UNSIGNED (TREE_TYPE (@0))
6845 && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (type))
6848 tree shifter = build_int_cst (integer_type_node, TYPE_PRECISION (type) - 1);
6850 (if (cmp == GE_EXPR)
6851 (bit_xor (convert (rshift @0 {shifter;})) @1)
6852 (bit_not (bit_xor (convert (rshift @0 {shifter;})) @1)))))))
6854 /* If we have (A & C) != 0 ? D : 0 where C and D are powers of 2,
6855 convert this into a shift followed by ANDing with D. */
6858 (ne (bit_and @0 integer_pow2p@1) integer_zerop)
6859 INTEGER_CST@2 integer_zerop)
6860 (if (!POINTER_TYPE_P (type) && integer_pow2p (@2))
6862 int shift = (wi::exact_log2 (wi::to_wide (@2))
6863 - wi::exact_log2 (wi::to_wide (@1)));
6867 (lshift (convert @0) { build_int_cst (integer_type_node, shift); }) @2)
6869 (convert (rshift @0 { build_int_cst (integer_type_node, -shift); }))
6872 /* If we have (A & C) != 0 where C is the sign bit of A, convert
6873 this into A < 0. Similarly for (A & C) == 0 into A >= 0. */
6877 (cmp (bit_and (convert?@2 @0) integer_pow2p@1) integer_zerop)
6878 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6879 && type_has_mode_precision_p (TREE_TYPE (@0))
6880 && element_precision (@2) >= element_precision (@0)
6881 && wi::only_sign_bit_p (wi::to_wide (@1), element_precision (@0)))
6882 (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
6883 (ncmp (convert:stype @0) { build_zero_cst (stype); })))))
6885 /* If we have A < 0 ? C : 0 where C is a power of 2, convert
6886 this into a right shift or sign extension followed by ANDing with C. */
6889 (lt @0 integer_zerop)
6890 INTEGER_CST@1 integer_zerop)
6891 (if (integer_pow2p (@1)
6892 && !TYPE_UNSIGNED (TREE_TYPE (@0)))
6894 int shift = element_precision (@0) - wi::exact_log2 (wi::to_wide (@1)) - 1;
6898 (convert (rshift @0 { build_int_cst (integer_type_node, shift); }))
6900 /* Otherwise ctype must be wider than TREE_TYPE (@0) and pure
6901 sign extension followed by AND with C will achieve the effect. */
6902 (bit_and (convert @0) @1)))))
6904 /* When the addresses are not directly of decls compare base and offset.
6905 This implements some remaining parts of fold_comparison address
6906 comparisons but still no complete part of it. Still it is good
6907 enough to make fold_stmt not regress when not dispatching to fold_binary. */
6908 (for cmp (simple_comparison)
6910 (cmp (convert1?@2 addr@0) (convert2? addr@1))
6913 poly_int64 off0, off1;
6915 int equal = address_compare (cmp, TREE_TYPE (@2), @0, @1, base0, base1,
6916 off0, off1, GENERIC);
6920 (if (cmp == EQ_EXPR && (known_eq (off0, off1) || known_ne (off0, off1)))
6921 { constant_boolean_node (known_eq (off0, off1), type); })
6922 (if (cmp == NE_EXPR && (known_eq (off0, off1) || known_ne (off0, off1)))
6923 { constant_boolean_node (known_ne (off0, off1), type); })
6924 (if (cmp == LT_EXPR && (known_lt (off0, off1) || known_ge (off0, off1)))
6925 { constant_boolean_node (known_lt (off0, off1), type); })
6926 (if (cmp == LE_EXPR && (known_le (off0, off1) || known_gt (off0, off1)))
6927 { constant_boolean_node (known_le (off0, off1), type); })
6928 (if (cmp == GE_EXPR && (known_ge (off0, off1) || known_lt (off0, off1)))
6929 { constant_boolean_node (known_ge (off0, off1), type); })
6930 (if (cmp == GT_EXPR && (known_gt (off0, off1) || known_le (off0, off1)))
6931 { constant_boolean_node (known_gt (off0, off1), type); }))
6934 (if (cmp == EQ_EXPR)
6935 { constant_boolean_node (false, type); })
6936 (if (cmp == NE_EXPR)
6937 { constant_boolean_node (true, type); })))))))
6940 /* a?~t:t -> (-(a))^t */
6943 (with { bool wascmp; }
6944 (if (INTEGRAL_TYPE_P (type)
6945 && bitwise_inverted_equal_p (@1, @2, wascmp)
6946 && (!wascmp || TYPE_PRECISION (type) == 1))
6947 (if ((!TYPE_UNSIGNED (type) && TREE_CODE (type) == BOOLEAN_TYPE)
6948 || TYPE_PRECISION (type) == 1)
6949 (bit_xor (convert:type @0) @2)
6950 (bit_xor (negate (convert:type @0)) @2)))))
6953 /* Simplify pointer equality compares using PTA. */
6957 (if (POINTER_TYPE_P (TREE_TYPE (@0))
6958 && ptrs_compare_unequal (@0, @1))
6959 { constant_boolean_node (neeq != EQ_EXPR, type); })))
6961 /* PR70920: Transform (intptr_t)x eq/ne CST to x eq/ne (typeof x) CST.
6962 and (typeof ptr_cst) x eq/ne ptr_cst to x eq/ne (typeof x) CST.
6963 Disable the transform if either operand is pointer to function.
6964 This broke pr22051-2.c for arm where function pointer
6965 canonicalizaion is not wanted. */
6969 (cmp (convert @0) INTEGER_CST@1)
6970 (if (((POINTER_TYPE_P (TREE_TYPE (@0))
6971 && !FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@0)))
6972 && INTEGRAL_TYPE_P (TREE_TYPE (@1))
6973 /* Don't perform this optimization in GENERIC if @0 has reference
6974 type when sanitizing. See PR101210. */
6976 && TREE_CODE (TREE_TYPE (@0)) == REFERENCE_TYPE
6977 && (flag_sanitize & (SANITIZE_NULL | SANITIZE_ALIGNMENT))))
6978 || (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6979 && POINTER_TYPE_P (TREE_TYPE (@1))
6980 && !FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@1)))))
6981 && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
6982 (cmp @0 (convert @1)))))
6984 /* Non-equality compare simplifications from fold_binary */
6985 (for cmp (lt gt le ge)
6986 /* Comparisons with the highest or lowest possible integer of
6987 the specified precision will have known values. */
6989 (cmp (convert?@2 @0) uniform_integer_cst_p@1)
6990 (if ((INTEGRAL_TYPE_P (TREE_TYPE (@1))
6991 || POINTER_TYPE_P (TREE_TYPE (@1))
6992 || VECTOR_INTEGER_TYPE_P (TREE_TYPE (@1)))
6993 && tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@0)))
6996 tree cst = uniform_integer_cst_p (@1);
6997 tree arg1_type = TREE_TYPE (cst);
6998 unsigned int prec = TYPE_PRECISION (arg1_type);
6999 wide_int max = wi::max_value (arg1_type);
7000 wide_int signed_max = wi::max_value (prec, SIGNED);
7001 wide_int min = wi::min_value (arg1_type);
7004 (if (wi::to_wide (cst) == max)
7006 (if (cmp == GT_EXPR)
7007 { constant_boolean_node (false, type); })
7008 (if (cmp == GE_EXPR)
7010 (if (cmp == LE_EXPR)
7011 { constant_boolean_node (true, type); })
7012 (if (cmp == LT_EXPR)
7014 (if (wi::to_wide (cst) == min)
7016 (if (cmp == LT_EXPR)
7017 { constant_boolean_node (false, type); })
7018 (if (cmp == LE_EXPR)
7020 (if (cmp == GE_EXPR)
7021 { constant_boolean_node (true, type); })
7022 (if (cmp == GT_EXPR)
7024 (if (wi::to_wide (cst) == max - 1)
7026 (if (cmp == GT_EXPR)
7027 (eq @2 { build_uniform_cst (TREE_TYPE (@1),
7028 wide_int_to_tree (TREE_TYPE (cst),
7031 (if (cmp == LE_EXPR)
7032 (ne @2 { build_uniform_cst (TREE_TYPE (@1),
7033 wide_int_to_tree (TREE_TYPE (cst),
7036 (if (wi::to_wide (cst) == min + 1)
7038 (if (cmp == GE_EXPR)
7039 (ne @2 { build_uniform_cst (TREE_TYPE (@1),
7040 wide_int_to_tree (TREE_TYPE (cst),
7043 (if (cmp == LT_EXPR)
7044 (eq @2 { build_uniform_cst (TREE_TYPE (@1),
7045 wide_int_to_tree (TREE_TYPE (cst),
7048 (if (wi::to_wide (cst) == signed_max
7049 && TYPE_UNSIGNED (arg1_type)
7050 && TYPE_MODE (arg1_type) != BLKmode
7051 /* We will flip the signedness of the comparison operator
7052 associated with the mode of @1, so the sign bit is
7053 specified by this mode. Check that @1 is the signed
7054 max associated with this sign bit. */
7055 && prec == GET_MODE_PRECISION (SCALAR_INT_TYPE_MODE (arg1_type))
7056 /* signed_type does not work on pointer types. */
7057 && INTEGRAL_TYPE_P (arg1_type))
7058 /* The following case also applies to X < signed_max+1
7059 and X >= signed_max+1 because previous transformations. */
7060 (if (cmp == LE_EXPR || cmp == GT_EXPR)
7061 (with { tree st = signed_type_for (TREE_TYPE (@1)); }
7063 (if (cst == @1 && cmp == LE_EXPR)
7064 (ge (convert:st @0) { build_zero_cst (st); }))
7065 (if (cst == @1 && cmp == GT_EXPR)
7066 (lt (convert:st @0) { build_zero_cst (st); }))
7067 (if (cmp == LE_EXPR)
7068 (ge (view_convert:st @0) { build_zero_cst (st); }))
7069 (if (cmp == GT_EXPR)
7070 (lt (view_convert:st @0) { build_zero_cst (st); })))))))))))
7072 /* unsigned < (typeof unsigned)(unsigned != 0) is always false. */
7074 (lt:c @0 (convert (ne @0 integer_zerop)))
7075 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
7076 { constant_boolean_node (false, type); }))
7078 /* x != (typeof x)(x == CST) -> CST == 0 ? 1 : (CST == 1 ? (x!=0&&x!=1) : x != 0) */
7079 /* x != (typeof x)(x != CST) -> CST == 1 ? 1 : (CST == 0 ? (x!=0&&x!=1) : x != 1) */
7080 /* x == (typeof x)(x == CST) -> CST == 0 ? 0 : (CST == 1 ? (x==0||x==1) : x == 0) */
7081 /* x == (typeof x)(x != CST) -> CST == 1 ? 0 : (CST == 0 ? (x==0||x==1) : x == 1) */
7085 (outer:c @0 (convert (inner @0 INTEGER_CST@1)))
7087 bool cst1 = integer_onep (@1);
7088 bool cst0 = integer_zerop (@1);
7089 bool innereq = inner == EQ_EXPR;
7090 bool outereq = outer == EQ_EXPR;
7093 (if (innereq ? cst0 : cst1)
7094 { constant_boolean_node (!outereq, type); })
7095 (if (innereq ? cst1 : cst0)
7097 tree utype = unsigned_type_for (TREE_TYPE (@0));
7098 tree ucst1 = build_one_cst (utype);
7101 (gt (convert:utype @0) { ucst1; })
7102 (le (convert:utype @0) { ucst1; })
7107 tree value = build_int_cst (TREE_TYPE (@0), !innereq);
7120 (for cmp (unordered ordered unlt unle ungt unge uneq ltgt)
7121 /* If the second operand is NaN, the result is constant. */
7124 (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
7125 && (cmp != LTGT_EXPR || ! flag_trapping_math))
7126 { constant_boolean_node (cmp == ORDERED_EXPR || cmp == LTGT_EXPR
7127 ? false : true, type); })))
7129 /* Fold UNORDERED if either operand must be NaN, or neither can be. */
7133 (if (tree_expr_nan_p (@0) || tree_expr_nan_p (@1))
7134 { constant_boolean_node (true, type); })
7135 (if (!tree_expr_maybe_nan_p (@0) && !tree_expr_maybe_nan_p (@1))
7136 { constant_boolean_node (false, type); })))
7138 /* Fold ORDERED if either operand must be NaN, or neither can be. */
7142 (if (tree_expr_nan_p (@0) || tree_expr_nan_p (@1))
7143 { constant_boolean_node (false, type); })
7144 (if (!tree_expr_maybe_nan_p (@0) && !tree_expr_maybe_nan_p (@1))
7145 { constant_boolean_node (true, type); })))
7147 /* bool_var != 0 becomes bool_var. */
7149 (ne @0 integer_zerop)
7150 (if (TREE_CODE (TREE_TYPE (@0)) == BOOLEAN_TYPE
7151 && types_match (type, TREE_TYPE (@0)))
7153 /* bool_var == 1 becomes bool_var. */
7155 (eq @0 integer_onep)
7156 (if (TREE_CODE (TREE_TYPE (@0)) == BOOLEAN_TYPE
7157 && types_match (type, TREE_TYPE (@0)))
7160 bool_var == 0 becomes !bool_var or
7161 bool_var != 1 becomes !bool_var
7162 here because that only is good in assignment context as long
7163 as we require a tcc_comparison in GIMPLE_CONDs where we'd
7164 replace if (x == 0) with tem = ~x; if (tem != 0) which is
7165 clearly less optimal and which we'll transform again in forwprop. */
7167 /* Transform comparisons of the form (X & Y) CMP 0 to X CMP2 Z
7168 where ~Y + 1 == pow2 and Z = ~Y. */
7169 (for cst (VECTOR_CST INTEGER_CST)
7173 (cmp (bit_and:c@2 @0 cst@1) integer_zerop)
7174 (with { tree csts = bitmask_inv_cst_vector_p (@1); }
7175 (if (csts && (VECTOR_TYPE_P (TREE_TYPE (@1)) || single_use (@2)))
7176 (with { auto optab = VECTOR_TYPE_P (TREE_TYPE (@1))
7177 ? optab_vector : optab_default;
7178 tree utype = unsigned_type_for (TREE_TYPE (@1)); }
7179 (if (target_supports_op_p (utype, icmp, optab)
7180 || (optimize_vectors_before_lowering_p ()
7181 && (!target_supports_op_p (type, cmp, optab)
7182 || !target_supports_op_p (type, BIT_AND_EXPR, optab))))
7183 (if (TYPE_UNSIGNED (TREE_TYPE (@1)))
7185 (icmp (view_convert:utype @0) { csts; })))))))))
7187 /* When one argument is a constant, overflow detection can be simplified.
7188 Currently restricted to single use so as not to interfere too much with
7189 ADD_OVERFLOW detection in tree-ssa-math-opts.cc.
7190 CONVERT?(CONVERT?(A) + CST) CMP A -> A CMP' CST' */
7191 (for cmp (lt le ge gt)
7194 (cmp:c (convert?@3 (plus@2 (convert?@4 @0) INTEGER_CST@1)) @0)
7195 (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@2))
7196 && types_match (TREE_TYPE (@0), TREE_TYPE (@3))
7197 && tree_nop_conversion_p (TREE_TYPE (@4), TREE_TYPE (@0))
7198 && wi::to_wide (@1) != 0
7201 unsigned int prec = TYPE_PRECISION (TREE_TYPE (@0));
7202 signop sign = TYPE_SIGN (TREE_TYPE (@0));
7204 (out @0 { wide_int_to_tree (TREE_TYPE (@0),
7205 wi::max_value (prec, sign)
7206 - wi::to_wide (@1)); })))))
7208 /* To detect overflow in unsigned A - B, A < B is simpler than A - B > A.
7209 However, the detection logic for SUB_OVERFLOW in tree-ssa-math-opts.cc
7210 expects the long form, so we restrict the transformation for now. */
7213 (cmp:c (minus@2 @0 @1) @0)
7214 (if (single_use (@2)
7215 && ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
7216 && TYPE_UNSIGNED (TREE_TYPE (@0)))
7219 /* Optimize A - B + -1 >= A into B >= A for unsigned comparisons. */
7222 (cmp:c (plus (minus @0 @1) integer_minus_onep) @0)
7223 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
7224 && TYPE_UNSIGNED (TREE_TYPE (@0)))
7227 /* Testing for overflow is unnecessary if we already know the result. */
7232 (cmp:c (realpart (IFN_SUB_OVERFLOW@2 @0 @1)) @0)
7233 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
7234 && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
7235 (out (imagpart @2) { build_zero_cst (TREE_TYPE (@0)); }))))
7240 (cmp:c (realpart (IFN_ADD_OVERFLOW:c@2 @0 @1)) @0)
7241 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
7242 && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
7243 (out (imagpart @2) { build_zero_cst (TREE_TYPE (@0)); }))))
7245 /* For unsigned operands, -1 / B < A checks whether A * B would overflow.
7246 Simplify it to __builtin_mul_overflow (A, B, <unused>). */
7250 (cmp:c (trunc_div:s integer_all_onesp @1) @0)
7251 (if (TYPE_UNSIGNED (TREE_TYPE (@0)) && !VECTOR_TYPE_P (TREE_TYPE (@0)))
7252 (with { tree t = TREE_TYPE (@0), cpx = build_complex_type (t); }
7253 (out (imagpart (IFN_MUL_OVERFLOW:cpx @0 @1)) { build_zero_cst (t); })))))
7255 /* Similarly, for unsigned operands, (((type) A * B) >> prec) != 0 where type
7256 is at least twice as wide as type of A and B, simplify to
7257 __builtin_mul_overflow (A, B, <unused>). */
7260 (cmp (rshift (mult:s (convert@3 @0) (convert @1)) INTEGER_CST@2)
7262 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
7263 && INTEGRAL_TYPE_P (TREE_TYPE (@3))
7264 && TYPE_UNSIGNED (TREE_TYPE (@0))
7265 && (TYPE_PRECISION (TREE_TYPE (@3))
7266 >= 2 * TYPE_PRECISION (TREE_TYPE (@0)))
7267 && tree_fits_uhwi_p (@2)
7268 && tree_to_uhwi (@2) == TYPE_PRECISION (TREE_TYPE (@0))
7269 && types_match (@0, @1)
7270 && type_has_mode_precision_p (TREE_TYPE (@0))
7271 && (optab_handler (umulv4_optab, TYPE_MODE (TREE_TYPE (@0)))
7272 != CODE_FOR_nothing))
7273 (with { tree t = TREE_TYPE (@0), cpx = build_complex_type (t); }
7274 (cmp (imagpart (IFN_MUL_OVERFLOW:cpx @0 @1)) { build_zero_cst (t); })))))
7276 /* Demote operands of IFN_{ADD,SUB,MUL}_OVERFLOW. */
7277 (for ovf (IFN_ADD_OVERFLOW IFN_SUB_OVERFLOW IFN_MUL_OVERFLOW)
7279 (ovf (convert@2 @0) @1)
7280 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
7281 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
7282 && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
7283 && (!TYPE_UNSIGNED (TREE_TYPE (@2)) || TYPE_UNSIGNED (TREE_TYPE (@0))))
7286 (ovf @1 (convert@2 @0))
7287 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
7288 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
7289 && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
7290 && (!TYPE_UNSIGNED (TREE_TYPE (@2)) || TYPE_UNSIGNED (TREE_TYPE (@0))))
7293 /* Optimize __builtin_mul_overflow_p (x, cst, (utype) 0) if all 3 types
7294 are unsigned to x > (umax / cst). Similarly for signed type, but
7295 in that case it needs to be outside of a range. */
7297 (imagpart (IFN_MUL_OVERFLOW:cs@2 @0 integer_nonzerop@1))
7298 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
7299 && TYPE_MAX_VALUE (TREE_TYPE (@0))
7300 && types_match (TREE_TYPE (@0), TREE_TYPE (TREE_TYPE (@2)))
7301 && int_fits_type_p (@1, TREE_TYPE (@0)))
7302 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
7303 (convert (gt @0 (trunc_div! { TYPE_MAX_VALUE (TREE_TYPE (@0)); } @1)))
7304 (if (TYPE_MIN_VALUE (TREE_TYPE (@0)))
7305 (if (integer_minus_onep (@1))
7306 (convert (eq @0 { TYPE_MIN_VALUE (TREE_TYPE (@0)); }))
7309 tree div = fold_convert (TREE_TYPE (@0), @1);
7310 tree lo = int_const_binop (TRUNC_DIV_EXPR,
7311 TYPE_MIN_VALUE (TREE_TYPE (@0)), div);
7312 tree hi = int_const_binop (TRUNC_DIV_EXPR,
7313 TYPE_MAX_VALUE (TREE_TYPE (@0)), div);
7314 tree etype = range_check_type (TREE_TYPE (@0));
7317 if (wi::neg_p (wi::to_wide (div)))
7319 lo = fold_convert (etype, lo);
7320 hi = fold_convert (etype, hi);
7321 hi = int_const_binop (MINUS_EXPR, hi, lo);
7325 (convert (gt (minus (convert:etype @0) { lo; }) { hi; })))))))))
7327 /* Simplification of math builtins. These rules must all be optimizations
7328 as well as IL simplifications. If there is a possibility that the new
7329 form could be a pessimization, the rule should go in the canonicalization
7330 section that follows this one.
7332 Rules can generally go in this section if they satisfy one of
7335 - the rule describes an identity
7337 - the rule replaces calls with something as simple as addition or
7340 - the rule contains unary calls only and simplifies the surrounding
7341 arithmetic. (The idea here is to exclude non-unary calls in which
7342 one operand is constant and in which the call is known to be cheap
7343 when the operand has that value.) */
7345 (if (flag_unsafe_math_optimizations)
7346 /* Simplify sqrt(x) * sqrt(x) -> x. */
7348 (mult (SQRT_ALL@1 @0) @1)
7349 (if (!tree_expr_maybe_signaling_nan_p (@0))
7352 (for op (plus minus)
7353 /* Simplify (A / C) +- (B / C) -> (A +- B) / C. */
7357 (rdiv (op @0 @2) @1)))
7359 (for cmp (lt le gt ge)
7360 neg_cmp (gt ge lt le)
7361 /* Simplify (x * C1) cmp C2 -> x cmp (C2 / C1), where C1 != 0. */
7363 (cmp (mult @0 REAL_CST@1) REAL_CST@2)
7365 { tree tem = const_binop (RDIV_EXPR, type, @2, @1); }
7367 && !(REAL_VALUE_ISINF (TREE_REAL_CST (tem))
7368 || (real_zerop (tem) && !real_zerop (@1))))
7370 (if (real_less (&dconst0, TREE_REAL_CST_PTR (@1)))
7372 (if (real_less (TREE_REAL_CST_PTR (@1), &dconst0))
7373 (neg_cmp @0 { tem; })))))))
7375 /* Simplify sqrt(x) * sqrt(y) -> sqrt(x*y). */
7376 (for root (SQRT CBRT)
7378 (mult (root:s @0) (root:s @1))
7379 (root (mult @0 @1))))
7381 /* Simplify expN(x) * expN(y) -> expN(x+y). */
7382 (for exps (EXP EXP2 EXP10 POW10)
7384 (mult (exps:s @0) (exps:s @1))
7385 (exps (plus @0 @1))))
7387 /* Simplify a/root(b/c) into a*root(c/b). */
7388 (for root (SQRT CBRT)
7390 (rdiv @0 (root:s (rdiv:s @1 @2)))
7391 (mult @0 (root (rdiv @2 @1)))))
7393 /* Simplify x/expN(y) into x*expN(-y). */
7394 (for exps (EXP EXP2 EXP10 POW10)
7396 (rdiv @0 (exps:s @1))
7397 (mult @0 (exps (negate @1)))))
7399 (for logs (LOG LOG2 LOG10 LOG10)
7400 exps (EXP EXP2 EXP10 POW10)
7401 /* logN(expN(x)) -> x. */
7405 /* expN(logN(x)) -> x. */
7410 /* Optimize logN(func()) for various exponential functions. We
7411 want to determine the value "x" and the power "exponent" in
7412 order to transform logN(x**exponent) into exponent*logN(x). */
7413 (for logs (LOG LOG LOG LOG2 LOG2 LOG2 LOG10 LOG10)
7414 exps (EXP2 EXP10 POW10 EXP EXP10 POW10 EXP EXP2)
7417 (if (SCALAR_FLOAT_TYPE_P (type))
7423 /* Prepare to do logN(exp(exponent)) -> exponent*logN(e). */
7424 x = build_real_truncate (type, dconst_e ());
7427 /* Prepare to do logN(exp2(exponent)) -> exponent*logN(2). */
7428 x = build_real (type, dconst2);
7432 /* Prepare to do logN(exp10(exponent)) -> exponent*logN(10). */
7434 REAL_VALUE_TYPE dconst10;
7435 real_from_integer (&dconst10, VOIDmode, 10, SIGNED);
7436 x = build_real (type, dconst10);
7443 (mult (logs { x; }) @0)))))
7451 (if (SCALAR_FLOAT_TYPE_P (type))
7457 /* Prepare to do logN(sqrt(x)) -> 0.5*logN(x). */
7458 x = build_real (type, dconsthalf);
7461 /* Prepare to do logN(cbrt(x)) -> (1/3)*logN(x). */
7462 x = build_real_truncate (type, dconst_third ());
7468 (mult { x; } (logs @0))))))
7470 /* logN(pow(x,exponent)) -> exponent*logN(x). */
7471 (for logs (LOG LOG2 LOG10)
7475 (mult @1 (logs @0))))
7477 /* pow(C,x) -> exp(log(C)*x) if C > 0,
7478 or if C is a positive power of 2,
7479 pow(C,x) -> exp2(log2(C)*x). */
7487 (pows REAL_CST@0 @1)
7488 (if (real_compare (GT_EXPR, TREE_REAL_CST_PTR (@0), &dconst0)
7489 && real_isfinite (TREE_REAL_CST_PTR (@0))
7490 /* As libmvec doesn't have a vectorized exp2, defer optimizing
7491 the use_exp2 case until after vectorization. It seems actually
7492 beneficial for all constants to postpone this until later,
7493 because exp(log(C)*x), while faster, will have worse precision
7494 and if x folds into a constant too, that is unnecessary
7496 && canonicalize_math_after_vectorization_p ())
7498 const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (@0);
7499 bool use_exp2 = false;
7500 if (targetm.libc_has_function (function_c99_misc, TREE_TYPE (@0))
7501 && value->cl == rvc_normal)
7503 REAL_VALUE_TYPE frac_rvt = *value;
7504 SET_REAL_EXP (&frac_rvt, 1);
7505 if (real_equal (&frac_rvt, &dconst1))
7510 (if (optimize_pow_to_exp (@0, @1))
7511 (exps (mult (logs @0) @1)))
7512 (exp2s (mult (log2s @0) @1)))))))
7515 /* pow(C,x)*expN(y) -> expN(logN(C)*x+y) if C > 0. */
7517 exps (EXP EXP2 EXP10 POW10)
7518 logs (LOG LOG2 LOG10 LOG10)
7520 (mult:c (pows:s REAL_CST@0 @1) (exps:s @2))
7521 (if (real_compare (GT_EXPR, TREE_REAL_CST_PTR (@0), &dconst0)
7522 && real_isfinite (TREE_REAL_CST_PTR (@0)))
7523 (exps (plus (mult (logs @0) @1) @2)))))
7528 exps (EXP EXP2 EXP10 POW10)
7529 /* sqrt(expN(x)) -> expN(x*0.5). */
7532 (exps (mult @0 { build_real (type, dconsthalf); })))
7533 /* cbrt(expN(x)) -> expN(x/3). */
7536 (exps (mult @0 { build_real_truncate (type, dconst_third ()); })))
7537 /* pow(expN(x), y) -> expN(x*y). */
7540 (exps (mult @0 @1))))
7542 /* tan(atan(x)) -> x. */
7549 /* Simplify sin(atan(x)) -> x / sqrt(x*x + 1). */
7553 copysigns (COPYSIGN)
7558 REAL_VALUE_TYPE r_cst;
7559 build_sinatan_real (&r_cst, type);
7560 tree t_cst = build_real (type, r_cst);
7561 tree t_one = build_one_cst (type);
7563 (if (SCALAR_FLOAT_TYPE_P (type))
7564 (cond (lt (abs @0) { t_cst; })
7565 (rdiv @0 (sqrts (plus (mult @0 @0) { t_one; })))
7566 (copysigns { t_one; } @0))))))
7568 /* Simplify cos(atan(x)) -> 1 / sqrt(x*x + 1). */
7572 copysigns (COPYSIGN)
7577 REAL_VALUE_TYPE r_cst;
7578 build_sinatan_real (&r_cst, type);
7579 tree t_cst = build_real (type, r_cst);
7580 tree t_one = build_one_cst (type);
7581 tree t_zero = build_zero_cst (type);
7583 (if (SCALAR_FLOAT_TYPE_P (type))
7584 (cond (lt (abs @0) { t_cst; })
7585 (rdiv { t_one; } (sqrts (plus (mult @0 @0) { t_one; })))
7586 (copysigns { t_zero; } @0))))))
7588 (if (!flag_errno_math)
7589 /* Simplify sinh(atanh(x)) -> x / sqrt((1 - x)*(1 + x)). */
7594 (sinhs (atanhs:s @0))
7595 (with { tree t_one = build_one_cst (type); }
7596 (rdiv @0 (sqrts (mult (minus { t_one; } @0) (plus { t_one; } @0)))))))
7598 /* Simplify cosh(atanh(x)) -> 1 / sqrt((1 - x)*(1 + x)) */
7603 (coshs (atanhs:s @0))
7604 (with { tree t_one = build_one_cst (type); }
7605 (rdiv { t_one; } (sqrts (mult (minus { t_one; } @0) (plus { t_one; } @0))))))))
7607 /* cabs(x+0i) or cabs(0+xi) -> abs(x). */
7609 (CABS (complex:C @0 real_zerop@1))
7612 /* trunc(trunc(x)) -> trunc(x), etc. */
7613 (for fns (TRUNC_ALL FLOOR_ALL CEIL_ALL ROUND_ALL NEARBYINT_ALL RINT_ALL)
7617 /* f(x) -> x if x is integer valued and f does nothing for such values. */
7618 (for fns (TRUNC_ALL FLOOR_ALL CEIL_ALL ROUND_ALL NEARBYINT_ALL RINT_ALL)
7620 (fns integer_valued_real_p@0)
7623 /* hypot(x,0) and hypot(0,x) -> abs(x). */
7625 (HYPOT:c @0 real_zerop@1)
7628 /* pow(1,x) -> 1. */
7630 (POW real_onep@0 @1)
7634 /* copysign(x,x) -> x. */
7635 (COPYSIGN_ALL @0 @0)
7639 /* copysign(x,-x) -> -x. */
7640 (COPYSIGN_ALL @0 (negate@1 @0))
7644 /* copysign(x,y) -> fabs(x) if y is nonnegative. */
7645 (COPYSIGN_ALL @0 tree_expr_nonnegative_p@1)
7649 /* fabs (copysign(x, y)) -> fabs (x). */
7650 (abs (COPYSIGN_ALL @0 @1))
7653 (for scale (LDEXP SCALBN SCALBLN)
7654 /* ldexp(0, x) -> 0. */
7656 (scale real_zerop@0 @1)
7658 /* ldexp(x, 0) -> x. */
7660 (scale @0 integer_zerop@1)
7662 /* ldexp(x, y) -> x if x is +-Inf or NaN. */
7664 (scale REAL_CST@0 @1)
7665 (if (!real_isfinite (TREE_REAL_CST_PTR (@0)))
7668 /* Canonicalization of sequences of math builtins. These rules represent
7669 IL simplifications but are not necessarily optimizations.
7671 The sincos pass is responsible for picking "optimal" implementations
7672 of math builtins, which may be more complicated and can sometimes go
7673 the other way, e.g. converting pow into a sequence of sqrts.
7674 We only want to do these canonicalizations before the pass has run. */
7676 (if (flag_unsafe_math_optimizations && canonicalize_math_p ())
7677 /* Simplify tan(x) * cos(x) -> sin(x). */
7679 (mult:c (TAN:s @0) (COS:s @0))
7682 /* Simplify x * pow(x,c) -> pow(x,c+1). */
7684 (mult:c @0 (POW:s @0 REAL_CST@1))
7685 (if (!TREE_OVERFLOW (@1))
7686 (POW @0 (plus @1 { build_one_cst (type); }))))
7688 /* Simplify sin(x) / cos(x) -> tan(x). */
7690 (rdiv (SIN:s @0) (COS:s @0))
7693 /* Simplify sinh(x) / cosh(x) -> tanh(x). */
7695 (rdiv (SINH:s @0) (COSH:s @0))
7698 /* Simplify tanh (x) / sinh (x) -> 1.0 / cosh (x). */
7700 (rdiv (TANH:s @0) (SINH:s @0))
7701 (rdiv {build_one_cst (type);} (COSH @0)))
7703 /* Simplify cos(x) / sin(x) -> 1 / tan(x). */
7705 (rdiv (COS:s @0) (SIN:s @0))
7706 (rdiv { build_one_cst (type); } (TAN @0)))
7708 /* Simplify sin(x) / tan(x) -> cos(x). */
7710 (rdiv (SIN:s @0) (TAN:s @0))
7711 (if (! HONOR_NANS (@0)
7712 && ! HONOR_INFINITIES (@0))
7715 /* Simplify tan(x) / sin(x) -> 1.0 / cos(x). */
7717 (rdiv (TAN:s @0) (SIN:s @0))
7718 (if (! HONOR_NANS (@0)
7719 && ! HONOR_INFINITIES (@0))
7720 (rdiv { build_one_cst (type); } (COS @0))))
7722 /* Simplify pow(x,y) * pow(x,z) -> pow(x,y+z). */
7724 (mult (POW:s @0 @1) (POW:s @0 @2))
7725 (POW @0 (plus @1 @2)))
7727 /* Simplify pow(x,y) * pow(z,y) -> pow(x*z,y). */
7729 (mult (POW:s @0 @1) (POW:s @2 @1))
7730 (POW (mult @0 @2) @1))
7732 /* Simplify powi(x,y) * powi(z,y) -> powi(x*z,y). */
7734 (mult (POWI:s @0 @1) (POWI:s @2 @1))
7735 (POWI (mult @0 @2) @1))
7737 /* Simplify pow(x,c) / x -> pow(x,c-1). */
7739 (rdiv (POW:s @0 REAL_CST@1) @0)
7740 (if (!TREE_OVERFLOW (@1))
7741 (POW @0 (minus @1 { build_one_cst (type); }))))
7743 /* Simplify x / pow (y,z) -> x * pow(y,-z). */
7745 (rdiv @0 (POW:s @1 @2))
7746 (mult @0 (POW @1 (negate @2))))
7751 /* sqrt(sqrt(x)) -> pow(x,1/4). */
7754 (pows @0 { build_real (type, dconst_quarter ()); }))
7755 /* sqrt(cbrt(x)) -> pow(x,1/6). */
7758 (pows @0 { build_real_truncate (type, dconst_sixth ()); }))
7759 /* cbrt(sqrt(x)) -> pow(x,1/6). */
7762 (pows @0 { build_real_truncate (type, dconst_sixth ()); }))
7763 /* cbrt(cbrt(x)) -> pow(x,1/9), iff x is nonnegative. */
7765 (cbrts (cbrts tree_expr_nonnegative_p@0))
7766 (pows @0 { build_real_truncate (type, dconst_ninth ()); }))
7767 /* sqrt(pow(x,y)) -> pow(|x|,y*0.5). */
7769 (sqrts (pows @0 @1))
7770 (pows (abs @0) (mult @1 { build_real (type, dconsthalf); })))
7771 /* cbrt(pow(x,y)) -> pow(x,y/3), iff x is nonnegative. */
7773 (cbrts (pows tree_expr_nonnegative_p@0 @1))
7774 (pows @0 (mult @1 { build_real_truncate (type, dconst_third ()); })))
7775 /* pow(sqrt(x),y) -> pow(x,y*0.5). */
7777 (pows (sqrts @0) @1)
7778 (pows @0 (mult @1 { build_real (type, dconsthalf); })))
7779 /* pow(cbrt(x),y) -> pow(x,y/3) iff x is nonnegative. */
7781 (pows (cbrts tree_expr_nonnegative_p@0) @1)
7782 (pows @0 (mult @1 { build_real_truncate (type, dconst_third ()); })))
7783 /* pow(pow(x,y),z) -> pow(x,y*z) iff x is nonnegative. */
7785 (pows (pows tree_expr_nonnegative_p@0 @1) @2)
7786 (pows @0 (mult @1 @2))))
7788 /* cabs(x+xi) -> fabs(x)*sqrt(2). */
7790 (CABS (complex @0 @0))
7791 (mult (abs @0) { build_real_truncate (type, dconst_sqrt2 ()); }))
7793 /* hypot(x,x) -> fabs(x)*sqrt(2). */
7796 (mult (abs @0) { build_real_truncate (type, dconst_sqrt2 ()); }))
7798 /* cexp(x+yi) -> exp(x)*cexpi(y). */
7803 (cexps compositional_complex@0)
7804 (if (targetm.libc_has_function (function_c99_math_complex, TREE_TYPE (@0)))
7806 (mult (exps@1 (realpart @0)) (realpart (cexpis:type@2 (imagpart @0))))
7807 (mult @1 (imagpart @2)))))))
7809 (if (canonicalize_math_p ())
7810 /* floor(x) -> trunc(x) if x is nonnegative. */
7811 (for floors (FLOOR_ALL)
7814 (floors tree_expr_nonnegative_p@0)
7817 (match double_value_p
7819 (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == double_type_node)))
7820 (for froms (BUILT_IN_TRUNCL
7832 /* truncl(extend(x)) -> extend(trunc(x)), etc., if x is a double. */
7833 (if (optimize && canonicalize_math_p ())
7835 (froms (convert double_value_p@0))
7836 (convert (tos @0)))))
7838 (match float_value_p
7840 (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == float_type_node)))
7841 (for froms (BUILT_IN_TRUNCL BUILT_IN_TRUNC
7842 BUILT_IN_FLOORL BUILT_IN_FLOOR
7843 BUILT_IN_CEILL BUILT_IN_CEIL
7844 BUILT_IN_ROUNDL BUILT_IN_ROUND
7845 BUILT_IN_NEARBYINTL BUILT_IN_NEARBYINT
7846 BUILT_IN_RINTL BUILT_IN_RINT)
7847 tos (BUILT_IN_TRUNCF BUILT_IN_TRUNCF
7848 BUILT_IN_FLOORF BUILT_IN_FLOORF
7849 BUILT_IN_CEILF BUILT_IN_CEILF
7850 BUILT_IN_ROUNDF BUILT_IN_ROUNDF
7851 BUILT_IN_NEARBYINTF BUILT_IN_NEARBYINTF
7852 BUILT_IN_RINTF BUILT_IN_RINTF)
7853 /* truncl(extend(x)) and trunc(extend(x)) -> extend(truncf(x)), etc.,
7855 (if (optimize && canonicalize_math_p ()
7856 && targetm.libc_has_function (function_c99_misc, NULL_TREE))
7858 (froms (convert float_value_p@0))
7859 (convert (tos @0)))))
7862 (match float16_value_p
7864 (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == float16_type_node)))
7865 (for froms (BUILT_IN_TRUNCL BUILT_IN_TRUNC BUILT_IN_TRUNCF
7866 BUILT_IN_FLOORL BUILT_IN_FLOOR BUILT_IN_FLOORF
7867 BUILT_IN_CEILL BUILT_IN_CEIL BUILT_IN_CEILF
7868 BUILT_IN_ROUNDEVENL BUILT_IN_ROUNDEVEN BUILT_IN_ROUNDEVENF
7869 BUILT_IN_ROUNDL BUILT_IN_ROUND BUILT_IN_ROUNDF
7870 BUILT_IN_NEARBYINTL BUILT_IN_NEARBYINT BUILT_IN_NEARBYINTF
7871 BUILT_IN_RINTL BUILT_IN_RINT BUILT_IN_RINTF
7872 BUILT_IN_SQRTL BUILT_IN_SQRT BUILT_IN_SQRTF)
7873 tos (IFN_TRUNC IFN_TRUNC IFN_TRUNC
7874 IFN_FLOOR IFN_FLOOR IFN_FLOOR
7875 IFN_CEIL IFN_CEIL IFN_CEIL
7876 IFN_ROUNDEVEN IFN_ROUNDEVEN IFN_ROUNDEVEN
7877 IFN_ROUND IFN_ROUND IFN_ROUND
7878 IFN_NEARBYINT IFN_NEARBYINT IFN_NEARBYINT
7879 IFN_RINT IFN_RINT IFN_RINT
7880 IFN_SQRT IFN_SQRT IFN_SQRT)
7881 /* (_Float16) round ((doube) x) -> __built_in_roundf16 (x), etc.,
7882 if x is a _Float16. */
7884 (convert (froms (convert float16_value_p@0)))
7886 && types_match (type, TREE_TYPE (@0))
7887 && direct_internal_fn_supported_p (as_internal_fn (tos),
7888 type, OPTIMIZE_FOR_BOTH))
7891 /* Simplify (trunc)copysign ((extend)x, (extend)y) to copysignf (x, y),
7892 x,y is float value, similar for _Float16/double. */
7893 (for copysigns (COPYSIGN_ALL)
7895 (convert (copysigns (convert@2 @0) (convert @1)))
7897 && !HONOR_SNANS (@2)
7898 && types_match (type, TREE_TYPE (@0))
7899 && types_match (type, TREE_TYPE (@1))
7900 && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (@2))
7901 && direct_internal_fn_supported_p (IFN_COPYSIGN,
7902 type, OPTIMIZE_FOR_BOTH))
7903 (IFN_COPYSIGN @0 @1))))
7905 (for froms (BUILT_IN_FMAF BUILT_IN_FMA BUILT_IN_FMAL)
7906 tos (IFN_FMA IFN_FMA IFN_FMA)
7908 (convert (froms (convert@3 @0) (convert @1) (convert @2)))
7909 (if (flag_unsafe_math_optimizations
7911 && FLOAT_TYPE_P (type)
7912 && FLOAT_TYPE_P (TREE_TYPE (@3))
7913 && types_match (type, TREE_TYPE (@0))
7914 && types_match (type, TREE_TYPE (@1))
7915 && types_match (type, TREE_TYPE (@2))
7916 && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (@3))
7917 && direct_internal_fn_supported_p (as_internal_fn (tos),
7918 type, OPTIMIZE_FOR_BOTH))
7921 (for maxmin (max min)
7923 (convert (maxmin (convert@2 @0) (convert @1)))
7925 && FLOAT_TYPE_P (type)
7926 && FLOAT_TYPE_P (TREE_TYPE (@2))
7927 && types_match (type, TREE_TYPE (@0))
7928 && types_match (type, TREE_TYPE (@1))
7929 && element_precision (type) < element_precision (TREE_TYPE (@2)))
7933 (for froms (XFLOORL XCEILL XROUNDL XRINTL)
7934 tos (XFLOOR XCEIL XROUND XRINT)
7935 /* llfloorl(extend(x)) -> llfloor(x), etc., if x is a double. */
7936 (if (optimize && canonicalize_math_p ())
7938 (froms (convert double_value_p@0))
7941 (for froms (XFLOORL XCEILL XROUNDL XRINTL
7942 XFLOOR XCEIL XROUND XRINT)
7943 tos (XFLOORF XCEILF XROUNDF XRINTF)
7944 /* llfloorl(extend(x)) and llfloor(extend(x)) -> llfloorf(x), etc.,
7946 (if (optimize && canonicalize_math_p ())
7948 (froms (convert float_value_p@0))
7951 (if (canonicalize_math_p ())
7952 /* xfloor(x) -> fix_trunc(x) if x is nonnegative. */
7953 (for floors (IFLOOR LFLOOR LLFLOOR)
7955 (floors tree_expr_nonnegative_p@0)
7958 (if (canonicalize_math_p ())
7959 /* xfloor(x) -> fix_trunc(x), etc., if x is integer valued. */
7960 (for fns (IFLOOR LFLOOR LLFLOOR
7962 IROUND LROUND LLROUND)
7964 (fns integer_valued_real_p@0)
7966 (if (!flag_errno_math)
7967 /* xrint(x) -> fix_trunc(x), etc., if x is integer valued. */
7968 (for rints (IRINT LRINT LLRINT)
7970 (rints integer_valued_real_p@0)
7973 (if (canonicalize_math_p ())
7974 (for ifn (IFLOOR ICEIL IROUND IRINT)
7975 lfn (LFLOOR LCEIL LROUND LRINT)
7976 llfn (LLFLOOR LLCEIL LLROUND LLRINT)
7977 /* Canonicalize iround (x) to lround (x) on ILP32 targets where
7978 sizeof (int) == sizeof (long). */
7979 (if (TYPE_PRECISION (integer_type_node)
7980 == TYPE_PRECISION (long_integer_type_node))
7983 (lfn:long_integer_type_node @0)))
7984 /* Canonicalize llround (x) to lround (x) on LP64 targets where
7985 sizeof (long long) == sizeof (long). */
7986 (if (TYPE_PRECISION (long_long_integer_type_node)
7987 == TYPE_PRECISION (long_integer_type_node))
7990 (lfn:long_integer_type_node @0)))))
7992 /* cproj(x) -> x if we're ignoring infinities. */
7995 (if (!HONOR_INFINITIES (type))
7998 /* If the real part is inf and the imag part is known to be
7999 nonnegative, return (inf + 0i). */
8001 (CPROJ (complex REAL_CST@0 tree_expr_nonnegative_p@1))
8002 (if (real_isinf (TREE_REAL_CST_PTR (@0)))
8003 { build_complex_inf (type, false); }))
8005 /* If the imag part is inf, return (inf+I*copysign(0,imag)). */
8007 (CPROJ (complex @0 REAL_CST@1))
8008 (if (real_isinf (TREE_REAL_CST_PTR (@1)))
8009 { build_complex_inf (type, TREE_REAL_CST_PTR (@1)->sign); }))
8015 (pows @0 REAL_CST@1)
8017 const REAL_VALUE_TYPE *value = TREE_REAL_CST_PTR (@1);
8018 REAL_VALUE_TYPE tmp;
8021 /* pow(x,0) -> 1. */
8022 (if (real_equal (value, &dconst0))
8023 { build_real (type, dconst1); })
8024 /* pow(x,1) -> x. */
8025 (if (real_equal (value, &dconst1))
8027 /* pow(x,-1) -> 1/x. */
8028 (if (real_equal (value, &dconstm1))
8029 (rdiv { build_real (type, dconst1); } @0))
8030 /* pow(x,0.5) -> sqrt(x). */
8031 (if (flag_unsafe_math_optimizations
8032 && canonicalize_math_p ()
8033 && real_equal (value, &dconsthalf))
8035 /* pow(x,1/3) -> cbrt(x). */
8036 (if (flag_unsafe_math_optimizations
8037 && canonicalize_math_p ()
8038 && (tmp = real_value_truncate (TYPE_MODE (type), dconst_third ()),
8039 real_equal (value, &tmp)))
8042 /* powi(1,x) -> 1. */
8044 (POWI real_onep@0 @1)
8048 (POWI @0 INTEGER_CST@1)
8050 /* powi(x,0) -> 1. */
8051 (if (wi::to_wide (@1) == 0)
8052 { build_real (type, dconst1); })
8053 /* powi(x,1) -> x. */
8054 (if (wi::to_wide (@1) == 1)
8056 /* powi(x,-1) -> 1/x. */
8057 (if (wi::to_wide (@1) == -1)
8058 (rdiv { build_real (type, dconst1); } @0))))
8060 /* Narrowing of arithmetic and logical operations.
8062 These are conceptually similar to the transformations performed for
8063 the C/C++ front-ends by shorten_binary_op and shorten_compare. Long
8064 term we want to move all that code out of the front-ends into here. */
8066 /* Convert (outertype)((innertype0)a+(innertype1)b)
8067 into ((newtype)a+(newtype)b) where newtype
8068 is the widest mode from all of these. */
8069 (for op (plus minus mult rdiv)
8071 (convert (op:s@0 (convert1?@3 @1) (convert2?@4 @2)))
8072 /* If we have a narrowing conversion of an arithmetic operation where
8073 both operands are widening conversions from the same type as the outer
8074 narrowing conversion. Then convert the innermost operands to a
8075 suitable unsigned type (to avoid introducing undefined behavior),
8076 perform the operation and convert the result to the desired type. */
8077 (if (INTEGRAL_TYPE_P (type)
8080 /* We check for type compatibility between @0 and @1 below,
8081 so there's no need to check that @2/@4 are integral types. */
8082 && INTEGRAL_TYPE_P (TREE_TYPE (@1))
8083 && INTEGRAL_TYPE_P (TREE_TYPE (@3))
8084 /* The precision of the type of each operand must match the
8085 precision of the mode of each operand, similarly for the
8087 && type_has_mode_precision_p (TREE_TYPE (@1))
8088 && type_has_mode_precision_p (TREE_TYPE (@2))
8089 && type_has_mode_precision_p (type)
8090 /* The inner conversion must be a widening conversion. */
8091 && TYPE_PRECISION (TREE_TYPE (@3)) > TYPE_PRECISION (TREE_TYPE (@1))
8092 && types_match (@1, type)
8093 && (types_match (@1, @2)
8094 /* Or the second operand is const integer or converted const
8095 integer from valueize. */
8096 || poly_int_tree_p (@4)))
8097 (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
8098 (op @1 (convert @2))
8099 (with { tree utype = unsigned_type_for (TREE_TYPE (@1)); }
8100 (convert (op (convert:utype @1)
8101 (convert:utype @2)))))
8102 (if (FLOAT_TYPE_P (type)
8103 && DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0))
8104 == DECIMAL_FLOAT_TYPE_P (type))
8105 (with { tree arg0 = strip_float_extensions (@1);
8106 tree arg1 = strip_float_extensions (@2);
8107 tree itype = TREE_TYPE (@0);
8108 tree ty1 = TREE_TYPE (arg0);
8109 tree ty2 = TREE_TYPE (arg1);
8110 enum tree_code code = TREE_CODE (itype); }
8111 (if (FLOAT_TYPE_P (ty1)
8112 && FLOAT_TYPE_P (ty2))
8113 (with { tree newtype = type;
8114 if (TYPE_MODE (ty1) == SDmode
8115 || TYPE_MODE (ty2) == SDmode
8116 || TYPE_MODE (type) == SDmode)
8117 newtype = dfloat32_type_node;
8118 if (TYPE_MODE (ty1) == DDmode
8119 || TYPE_MODE (ty2) == DDmode
8120 || TYPE_MODE (type) == DDmode)
8121 newtype = dfloat64_type_node;
8122 if (TYPE_MODE (ty1) == TDmode
8123 || TYPE_MODE (ty2) == TDmode
8124 || TYPE_MODE (type) == TDmode)
8125 newtype = dfloat128_type_node; }
8126 (if ((newtype == dfloat32_type_node
8127 || newtype == dfloat64_type_node
8128 || newtype == dfloat128_type_node)
8130 && types_match (newtype, type))
8131 (op (convert:newtype @1) (convert:newtype @2))
8132 (with { if (element_precision (ty1) > element_precision (newtype))
8134 if (element_precision (ty2) > element_precision (newtype))
8136 /* Sometimes this transformation is safe (cannot
8137 change results through affecting double rounding
8138 cases) and sometimes it is not. If NEWTYPE is
8139 wider than TYPE, e.g. (float)((long double)double
8140 + (long double)double) converted to
8141 (float)(double + double), the transformation is
8142 unsafe regardless of the details of the types
8143 involved; double rounding can arise if the result
8144 of NEWTYPE arithmetic is a NEWTYPE value half way
8145 between two representable TYPE values but the
8146 exact value is sufficiently different (in the
8147 right direction) for this difference to be
8148 visible in ITYPE arithmetic. If NEWTYPE is the
8149 same as TYPE, however, the transformation may be
8150 safe depending on the types involved: it is safe
8151 if the ITYPE has strictly more than twice as many
8152 mantissa bits as TYPE, can represent infinities
8153 and NaNs if the TYPE can, and has sufficient
8154 exponent range for the product or ratio of two
8155 values representable in the TYPE to be within the
8156 range of normal values of ITYPE. */
8157 (if (element_precision (newtype) < element_precision (itype)
8158 && (!VECTOR_MODE_P (TYPE_MODE (newtype))
8159 || target_supports_op_p (newtype, op, optab_default))
8160 && (flag_unsafe_math_optimizations
8161 || (element_precision (newtype) == element_precision (type)
8162 && real_can_shorten_arithmetic (element_mode (itype),
8163 element_mode (type))
8164 && !excess_precision_type (newtype)))
8165 && !types_match (itype, newtype))
8166 (convert:type (op (convert:newtype @1)
8167 (convert:newtype @2)))
8172 /* This is another case of narrowing, specifically when there's an outer
8173 BIT_AND_EXPR which masks off bits outside the type of the innermost
8174 operands. Like the previous case we have to convert the operands
8175 to unsigned types to avoid introducing undefined behavior for the
8176 arithmetic operation. */
8177 (for op (minus plus)
8179 (bit_and (op:s (convert@2 @0) (convert@3 @1)) INTEGER_CST@4)
8180 (if (INTEGRAL_TYPE_P (type)
8181 /* We check for type compatibility between @0 and @1 below,
8182 so there's no need to check that @1/@3 are integral types. */
8183 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
8184 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
8185 /* The precision of the type of each operand must match the
8186 precision of the mode of each operand, similarly for the
8188 && type_has_mode_precision_p (TREE_TYPE (@0))
8189 && type_has_mode_precision_p (TREE_TYPE (@1))
8190 && type_has_mode_precision_p (type)
8191 /* The inner conversion must be a widening conversion. */
8192 && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
8193 && types_match (@0, @1)
8194 && (tree_int_cst_min_precision (@4, TYPE_SIGN (TREE_TYPE (@0)))
8195 <= TYPE_PRECISION (TREE_TYPE (@0)))
8196 && (wi::to_wide (@4)
8197 & wi::mask (TYPE_PRECISION (TREE_TYPE (@0)),
8198 true, TYPE_PRECISION (type))) == 0)
8199 (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
8200 (with { tree ntype = TREE_TYPE (@0); }
8201 (convert (bit_and (op @0 @1) (convert:ntype @4))))
8202 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
8203 (convert (bit_and (op (convert:utype @0) (convert:utype @1))
8204 (convert:utype @4))))))))
8206 /* Transform (@0 < @1 and @0 < @2) to use min,
8207 (@0 > @1 and @0 > @2) to use max */
8208 (for logic (bit_and bit_and bit_and bit_and bit_ior bit_ior bit_ior bit_ior)
8209 op (lt le gt ge lt le gt ge )
8210 ext (min min max max max max min min )
8212 (logic (op:cs @0 @1) (op:cs @0 @2))
8213 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
8214 && TREE_CODE (@0) != INTEGER_CST)
8215 (op @0 (ext @1 @2)))))
8217 /* Max<bool0, bool1> -> bool0 | bool1
8218 Min<bool0, bool1> -> bool0 & bool1 */
8220 logic (bit_ior bit_and)
8222 (op zero_one_valued_p@0 zero_one_valued_p@1)
8225 /* signbit(x) != 0 ? -x : x -> abs(x)
8226 signbit(x) == 0 ? -x : x -> -abs(x) */
8230 (cond (neeq (sign @0) integer_zerop) (negate @0) @0)
8231 (if (neeq == NE_EXPR)
8233 (negate (abs @0))))))
8236 /* signbit(x) -> 0 if x is nonnegative. */
8237 (SIGNBIT tree_expr_nonnegative_p@0)
8238 { integer_zero_node; })
8241 /* signbit(x) -> x<0 if x doesn't have signed zeros. */
8243 (if (!HONOR_SIGNED_ZEROS (@0))
8244 (convert (lt @0 { build_real (TREE_TYPE (@0), dconst0); }))))
8246 /* Transform comparisons of the form X +- C1 CMP C2 to X CMP C2 -+ C1. */
8248 (for op (plus minus)
8251 (cmp (op@3 @0 INTEGER_CST@1) INTEGER_CST@2)
8252 (if (!TREE_OVERFLOW (@1) && !TREE_OVERFLOW (@2)
8253 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0))
8254 && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@0))
8255 && !TYPE_SATURATING (TREE_TYPE (@0)))
8256 (with { tree res = int_const_binop (rop, @2, @1); }
8257 (if (TREE_OVERFLOW (res)
8258 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
8259 { constant_boolean_node (cmp == NE_EXPR, type); }
8260 (if (single_use (@3))
8261 (cmp @0 { TREE_OVERFLOW (res)
8262 ? drop_tree_overflow (res) : res; }))))))))
8263 (for cmp (lt le gt ge)
8264 (for op (plus minus)
8267 (cmp (op@3 @0 INTEGER_CST@1) INTEGER_CST@2)
8268 (if (!TREE_OVERFLOW (@1) && !TREE_OVERFLOW (@2)
8269 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
8270 (with { tree res = int_const_binop (rop, @2, @1); }
8271 (if (TREE_OVERFLOW (res))
8273 fold_overflow_warning (("assuming signed overflow does not occur "
8274 "when simplifying conditional to constant"),
8275 WARN_STRICT_OVERFLOW_CONDITIONAL);
8276 bool less = cmp == LE_EXPR || cmp == LT_EXPR;
8277 /* wi::ges_p (@2, 0) should be sufficient for a signed type. */
8278 bool ovf_high = wi::lt_p (wi::to_wide (@1), 0,
8279 TYPE_SIGN (TREE_TYPE (@1)))
8280 != (op == MINUS_EXPR);
8281 constant_boolean_node (less == ovf_high, type);
8283 (if (single_use (@3))
8286 fold_overflow_warning (("assuming signed overflow does not occur "
8287 "when changing X +- C1 cmp C2 to "
8289 WARN_STRICT_OVERFLOW_COMPARISON);
8291 (cmp @0 { res; })))))))))
8293 /* Canonicalizations of BIT_FIELD_REFs. */
8296 (BIT_FIELD_REF (BIT_FIELD_REF @0 @1 @2) @3 @4)
8297 (BIT_FIELD_REF @0 @3 { const_binop (PLUS_EXPR, bitsizetype, @2, @4); }))
8300 (BIT_FIELD_REF (view_convert @0) @1 @2)
8301 (if (! INTEGRAL_TYPE_P (TREE_TYPE (@0))
8302 || type_has_mode_precision_p (TREE_TYPE (@0)))
8303 (BIT_FIELD_REF @0 @1 @2)))
8306 (BIT_FIELD_REF @0 @1 integer_zerop)
8307 (if (tree_int_cst_equal (@1, TYPE_SIZE (TREE_TYPE (@0))))
8311 (BIT_FIELD_REF @0 @1 @2)
8313 (if (TREE_CODE (TREE_TYPE (@0)) == COMPLEX_TYPE
8314 && tree_int_cst_equal (@1, TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))))
8316 (if (integer_zerop (@2))
8317 (view_convert (realpart @0)))
8318 (if (tree_int_cst_equal (@2, TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))))
8319 (view_convert (imagpart @0)))))
8320 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
8321 && INTEGRAL_TYPE_P (type)
8322 /* On GIMPLE this should only apply to register arguments. */
8323 && (! GIMPLE || is_gimple_reg (@0))
8324 /* A bit-field-ref that referenced the full argument can be stripped. */
8325 && ((compare_tree_int (@1, TYPE_PRECISION (TREE_TYPE (@0))) == 0
8326 && integer_zerop (@2))
8327 /* Low-parts can be reduced to integral conversions.
8328 ??? The following doesn't work for PDP endian. */
8329 || (BYTES_BIG_ENDIAN == WORDS_BIG_ENDIAN
8330 /* But only do this after vectorization. */
8331 && canonicalize_math_after_vectorization_p ()
8332 /* Don't even think about BITS_BIG_ENDIAN. */
8333 && TYPE_PRECISION (TREE_TYPE (@0)) % BITS_PER_UNIT == 0
8334 && TYPE_PRECISION (type) % BITS_PER_UNIT == 0
8335 && compare_tree_int (@2, (BYTES_BIG_ENDIAN
8336 ? (TYPE_PRECISION (TREE_TYPE (@0))
8337 - TYPE_PRECISION (type))
8341 /* Simplify vector extracts. */
8344 (BIT_FIELD_REF CONSTRUCTOR@0 @1 @2)
8345 (if (VECTOR_TYPE_P (TREE_TYPE (@0))
8346 && tree_fits_uhwi_p (TYPE_SIZE (type))
8347 && ((tree_to_uhwi (TYPE_SIZE (type))
8348 == tree_to_uhwi (TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))))
8349 || (VECTOR_TYPE_P (type)
8350 && (tree_to_uhwi (TYPE_SIZE (TREE_TYPE (type)))
8351 == tree_to_uhwi (TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0))))))))
8354 tree ctor = (TREE_CODE (@0) == SSA_NAME
8355 ? gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0)) : @0);
8356 tree eltype = TREE_TYPE (TREE_TYPE (ctor));
8357 unsigned HOST_WIDE_INT width = tree_to_uhwi (TYPE_SIZE (eltype));
8358 unsigned HOST_WIDE_INT n = tree_to_uhwi (@1);
8359 unsigned HOST_WIDE_INT idx = tree_to_uhwi (@2);
8362 && (idx % width) == 0
8364 && known_le ((idx + n) / width,
8365 TYPE_VECTOR_SUBPARTS (TREE_TYPE (ctor))))
8370 /* Constructor elements can be subvectors. */
8372 if (CONSTRUCTOR_NELTS (ctor) != 0)
8374 tree cons_elem = TREE_TYPE (CONSTRUCTOR_ELT (ctor, 0)->value);
8375 if (TREE_CODE (cons_elem) == VECTOR_TYPE)
8376 k = TYPE_VECTOR_SUBPARTS (cons_elem);
8378 unsigned HOST_WIDE_INT elt, count, const_k;
8381 /* We keep an exact subset of the constructor elements. */
8382 (if (multiple_p (idx, k, &elt) && multiple_p (n, k, &count))
8383 (if (CONSTRUCTOR_NELTS (ctor) == 0)
8384 { build_zero_cst (type); }
8386 (if (elt < CONSTRUCTOR_NELTS (ctor))
8387 (view_convert { CONSTRUCTOR_ELT (ctor, elt)->value; })
8388 { build_zero_cst (type); })
8389 /* We don't want to emit new CTORs unless the old one goes away.
8390 ??? Eventually allow this if the CTOR ends up constant or
8392 (if (single_use (@0))
8395 vec<constructor_elt, va_gc> *vals;
8396 vec_alloc (vals, count);
8397 bool constant_p = true;
8399 for (unsigned i = 0;
8400 i < count && elt + i < CONSTRUCTOR_NELTS (ctor); ++i)
8402 tree e = CONSTRUCTOR_ELT (ctor, elt + i)->value;
8403 CONSTRUCTOR_APPEND_ELT (vals, NULL_TREE, e);
8404 if (!CONSTANT_CLASS_P (e))
8407 tree evtype = (types_match (TREE_TYPE (type),
8408 TREE_TYPE (TREE_TYPE (ctor)))
8410 : build_vector_type (TREE_TYPE (TREE_TYPE (ctor)),
8412 /* We used to build a CTOR in the non-constant case here
8413 but that's not a GIMPLE value. We'd have to expose this
8414 operation somehow so the code generation can properly
8415 split it out to a separate stmt. */
8416 res = (constant_p ? build_vector_from_ctor (evtype, vals)
8417 : (GIMPLE ? NULL_TREE : build_constructor (evtype, vals)));
8420 (view_convert { res; })))))))
8421 /* The bitfield references a single constructor element. */
8422 (if (k.is_constant (&const_k)
8423 && idx + n <= (idx / const_k + 1) * const_k)
8425 (if (CONSTRUCTOR_NELTS (ctor) <= idx / const_k)
8426 { build_zero_cst (type); })
8428 (view_convert { CONSTRUCTOR_ELT (ctor, idx / const_k)->value; }))
8429 (BIT_FIELD_REF { CONSTRUCTOR_ELT (ctor, idx / const_k)->value; }
8430 @1 { bitsize_int ((idx % const_k) * width); })))))))))
8432 /* Simplify a bit extraction from a bit insertion for the cases with
8433 the inserted element fully covering the extraction or the insertion
8434 not touching the extraction. */
8436 (BIT_FIELD_REF (bit_insert @0 @1 @ipos) @rsize @rpos)
8439 unsigned HOST_WIDE_INT isize;
8440 if (INTEGRAL_TYPE_P (TREE_TYPE (@1)))
8441 isize = TYPE_PRECISION (TREE_TYPE (@1));
8443 isize = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (@1)));
8446 (if ((!INTEGRAL_TYPE_P (TREE_TYPE (@1))
8447 || type_has_mode_precision_p (TREE_TYPE (@1)))
8448 && wi::leu_p (wi::to_wide (@ipos), wi::to_wide (@rpos))
8449 && wi::leu_p (wi::to_wide (@rpos) + wi::to_wide (@rsize),
8450 wi::to_wide (@ipos) + isize))
8451 (BIT_FIELD_REF @1 @rsize { wide_int_to_tree (bitsizetype,
8453 - wi::to_wide (@ipos)); }))
8454 (if (wi::eq_p (wi::to_wide (@ipos), wi::to_wide (@rpos))
8455 && compare_tree_int (@rsize, isize) == 0)
8457 (if (wi::geu_p (wi::to_wide (@ipos),
8458 wi::to_wide (@rpos) + wi::to_wide (@rsize))
8459 || wi::geu_p (wi::to_wide (@rpos),
8460 wi::to_wide (@ipos) + isize))
8461 (BIT_FIELD_REF @0 @rsize @rpos)))))
8463 /* Simplify vector inserts of other vector extracts to a permute. */
8465 (bit_insert @0 (BIT_FIELD_REF@2 @1 @rsize @rpos) @ipos)
8466 (if (VECTOR_TYPE_P (type)
8467 && types_match (@0, @1)
8468 && types_match (TREE_TYPE (TREE_TYPE (@0)), TREE_TYPE (@2))
8469 && TYPE_VECTOR_SUBPARTS (type).is_constant ())
8472 unsigned HOST_WIDE_INT elsz
8473 = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (TREE_TYPE (@1))));
8474 poly_uint64 relt = exact_div (tree_to_poly_uint64 (@rpos), elsz);
8475 poly_uint64 ielt = exact_div (tree_to_poly_uint64 (@ipos), elsz);
8476 unsigned nunits = TYPE_VECTOR_SUBPARTS (type).to_constant ();
8477 vec_perm_builder builder;
8478 builder.new_vector (nunits, nunits, 1);
8479 for (unsigned i = 0; i < nunits; ++i)
8480 builder.quick_push (known_eq (ielt, i) ? nunits + relt : i);
8481 vec_perm_indices sel (builder, 2, nunits);
8483 (if (!VECTOR_MODE_P (TYPE_MODE (type))
8484 || can_vec_perm_const_p (TYPE_MODE (type), TYPE_MODE (type), sel, false))
8485 (vec_perm @0 @1 { vec_perm_indices_to_tree
8486 (build_vector_type (ssizetype, nunits), sel); })))))
8488 (if (canonicalize_math_after_vectorization_p ())
8491 (fmas:c (negate @0) @1 @2)
8492 (IFN_FNMA @0 @1 @2))
8494 (fmas @0 @1 (negate @2))
8497 (fmas:c (negate @0) @1 (negate @2))
8498 (IFN_FNMS @0 @1 @2))
8500 (negate (fmas@3 @0 @1 @2))
8501 (if (single_use (@3))
8502 (IFN_FNMS @0 @1 @2))))
8505 (IFN_FMS:c (negate @0) @1 @2)
8506 (IFN_FNMS @0 @1 @2))
8508 (IFN_FMS @0 @1 (negate @2))
8511 (IFN_FMS:c (negate @0) @1 (negate @2))
8512 (IFN_FNMA @0 @1 @2))
8514 (negate (IFN_FMS@3 @0 @1 @2))
8515 (if (single_use (@3))
8516 (IFN_FNMA @0 @1 @2)))
8519 (IFN_FNMA:c (negate @0) @1 @2)
8522 (IFN_FNMA @0 @1 (negate @2))
8523 (IFN_FNMS @0 @1 @2))
8525 (IFN_FNMA:c (negate @0) @1 (negate @2))
8528 (negate (IFN_FNMA@3 @0 @1 @2))
8529 (if (single_use (@3))
8530 (IFN_FMS @0 @1 @2)))
8533 (IFN_FNMS:c (negate @0) @1 @2)
8536 (IFN_FNMS @0 @1 (negate @2))
8537 (IFN_FNMA @0 @1 @2))
8539 (IFN_FNMS:c (negate @0) @1 (negate @2))
8542 (negate (IFN_FNMS@3 @0 @1 @2))
8543 (if (single_use (@3))
8544 (IFN_FMA @0 @1 @2))))
8546 /* CLZ simplifications. */
8551 (op (clz:s@2 @0) INTEGER_CST@1)
8552 (if (integer_zerop (@1) && single_use (@2))
8553 /* clz(X) == 0 is (int)X < 0 and clz(X) != 0 is (int)X >= 0. */
8554 (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
8555 (cmp (convert:stype @0) { build_zero_cst (stype); }))
8556 /* clz(X) == (prec-1) is X == 1 and clz(X) != (prec-1) is X != 1. */
8557 (if (wi::to_wide (@1) == TYPE_PRECISION (TREE_TYPE (@0)) - 1)
8558 (op @0 { build_one_cst (TREE_TYPE (@0)); }))))))
8562 (op (IFN_CLZ:s@2 @0 @3) INTEGER_CST@1)
8563 (if (integer_zerop (@1) && single_use (@2))
8564 /* clz(X) == 0 is (int)X < 0 and clz(X) != 0 is (int)X >= 0. */
8565 (with { tree type0 = TREE_TYPE (@0);
8566 tree stype = signed_type_for (TREE_TYPE (@0));
8567 /* Punt if clz(0) == 0. */
8568 if (integer_zerop (@3))
8572 (cmp (convert:stype @0) { build_zero_cst (stype); })))
8573 /* clz(X) == (prec-1) is X == 1 and clz(X) != (prec-1) is X != 1. */
8574 (with { bool ok = true;
8575 tree type0 = TREE_TYPE (@0);
8576 /* Punt if clz(0) == prec - 1. */
8577 if (wi::to_widest (@3) == TYPE_PRECISION (type0) - 1)
8580 (if (ok && wi::to_wide (@1) == (TYPE_PRECISION (type0) - 1))
8581 (op @0 { build_one_cst (type0); }))))))
8583 /* CTZ simplifications. */
8585 (for op (ge gt le lt)
8588 /* __builtin_ctz (x) >= C -> (x & ((1 << C) - 1)) == 0. */
8589 (op (ctz:s @0) INTEGER_CST@1)
8590 (with { bool ok = true;
8591 HOST_WIDE_INT val = 0;
8592 if (!tree_fits_shwi_p (@1))
8596 val = tree_to_shwi (@1);
8597 /* Canonicalize to >= or <. */
8598 if (op == GT_EXPR || op == LE_EXPR)
8600 if (val == HOST_WIDE_INT_MAX)
8606 tree type0 = TREE_TYPE (@0);
8607 int prec = TYPE_PRECISION (type0);
8609 (if (ok && prec <= MAX_FIXED_MODE_SIZE)
8611 { constant_boolean_node (cmp == EQ_EXPR ? true : false, type); }
8613 { constant_boolean_node (cmp == EQ_EXPR ? false : true, type); }
8614 (cmp (bit_and @0 { wide_int_to_tree (type0,
8615 wi::mask (val, false, prec)); })
8616 { build_zero_cst (type0); })))))))
8619 /* __builtin_ctz (x) == C -> (x & ((1 << (C + 1)) - 1)) == (1 << C). */
8620 (op (ctz:s @0) INTEGER_CST@1)
8621 (with { tree type0 = TREE_TYPE (@0);
8622 int prec = TYPE_PRECISION (type0);
8624 (if (prec <= MAX_FIXED_MODE_SIZE)
8625 (if (tree_int_cst_sgn (@1) < 0 || wi::to_widest (@1) >= prec)
8626 { constant_boolean_node (op == EQ_EXPR ? false : true, type); }
8627 (op (bit_and @0 { wide_int_to_tree (type0,
8628 wi::mask (tree_to_uhwi (@1) + 1,
8630 { wide_int_to_tree (type0,
8631 wi::shifted_mask (tree_to_uhwi (@1), 1,
8632 false, prec)); })))))))
8633 (for op (ge gt le lt)
8636 /* __builtin_ctz (x) >= C -> (x & ((1 << C) - 1)) == 0. */
8637 (op (IFN_CTZ:s @0 @2) INTEGER_CST@1)
8638 (with { bool ok = true;
8639 HOST_WIDE_INT val = 0;
8640 if (!tree_fits_shwi_p (@1))
8644 val = tree_to_shwi (@1);
8645 /* Canonicalize to >= or <. */
8646 if (op == GT_EXPR || op == LE_EXPR)
8648 if (val == HOST_WIDE_INT_MAX)
8654 HOST_WIDE_INT zero_val = tree_to_shwi (@2);
8655 tree type0 = TREE_TYPE (@0);
8656 int prec = TYPE_PRECISION (type0);
8657 if (prec > MAX_FIXED_MODE_SIZE)
8661 (if (ok && zero_val >= val)
8662 { constant_boolean_node (cmp == EQ_EXPR ? true : false, type); })
8664 (if (ok && zero_val < val)
8665 { constant_boolean_node (cmp == EQ_EXPR ? false : true, type); })
8666 (if (ok && (zero_val < 0 || zero_val >= prec))
8667 (cmp (bit_and @0 { wide_int_to_tree (type0,
8668 wi::mask (val, false, prec)); })
8669 { build_zero_cst (type0); })))))))
8672 /* __builtin_ctz (x) == C -> (x & ((1 << (C + 1)) - 1)) == (1 << C). */
8673 (op (IFN_CTZ:s @0 @2) INTEGER_CST@1)
8674 (with { HOST_WIDE_INT zero_val = tree_to_shwi (@2);
8675 tree type0 = TREE_TYPE (@0);
8676 int prec = TYPE_PRECISION (type0);
8678 (if (prec <= MAX_FIXED_MODE_SIZE)
8679 (if (tree_int_cst_sgn (@1) < 0 || wi::to_widest (@1) >= prec)
8680 (if (zero_val != wi::to_widest (@1))
8681 { constant_boolean_node (op == EQ_EXPR ? false : true, type); })
8682 (if (zero_val < 0 || zero_val >= prec)
8683 (op (bit_and @0 { wide_int_to_tree (type0,
8684 wi::mask (tree_to_uhwi (@1) + 1,
8686 { wide_int_to_tree (type0,
8687 wi::shifted_mask (tree_to_uhwi (@1), 1,
8688 false, prec)); })))))))
8691 /* ctz(ext(X)) == ctz(X). Valid just for the UB at zero cases though. */
8693 (CTZ (convert@1 @0))
8694 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
8695 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
8696 && TYPE_PRECISION (TREE_TYPE (@1)) > TYPE_PRECISION (TREE_TYPE (@0)))
8697 (with { combined_fn cfn = CFN_LAST;
8698 tree type0 = TREE_TYPE (@0);
8699 if (TREE_CODE (type0) == BITINT_TYPE)
8701 if (TYPE_PRECISION (type0) > MAX_FIXED_MODE_SIZE)
8705 = build_nonstandard_integer_type (TYPE_PRECISION (type0),
8708 type0 = unsigned_type_for (type0);
8710 && direct_internal_fn_supported_p (IFN_CTZ, type0,
8714 && TYPE_PRECISION (TREE_TYPE (@1)) > BITS_PER_WORD
8715 && !direct_internal_fn_supported_p (IFN_CTZ,
8719 if (TYPE_PRECISION (type0)
8720 == TYPE_PRECISION (unsigned_type_node))
8721 cfn = CFN_BUILT_IN_CTZ;
8722 else if (TYPE_PRECISION (type0)
8723 == TYPE_PRECISION (long_long_unsigned_type_node))
8724 cfn = CFN_BUILT_IN_CTZLL;
8726 (if (cfn == CFN_CTZ)
8727 (IFN_CTZ (convert:type0 @0))
8728 (if (cfn == CFN_BUILT_IN_CTZ)
8729 (BUILT_IN_CTZ (convert:type0 @0))
8730 (if (cfn == CFN_BUILT_IN_CTZLL)
8731 (BUILT_IN_CTZLL (convert:type0 @0))))))))
8734 /* POPCOUNT simplifications. */
8735 /* popcount(X) + popcount(Y) is popcount(X|Y) when X&Y must be zero. */
8737 (plus (POPCOUNT:s @0) (POPCOUNT:s @1))
8738 (if (INTEGRAL_TYPE_P (type)
8739 && (wi::bit_and (widest_int::from (tree_nonzero_bits (@0), UNSIGNED),
8740 widest_int::from (tree_nonzero_bits (@1), UNSIGNED))
8742 (with { tree utype = TREE_TYPE (@0);
8743 if (TYPE_PRECISION (utype) < TYPE_PRECISION (TREE_TYPE (@1)))
8744 utype = TREE_TYPE (@1); }
8745 (POPCOUNT (bit_ior (convert:utype @0) (convert:utype @1))))))
8747 /* popcount(X) == 0 is X == 0, and related (in)equalities. */
8748 (for popcount (POPCOUNT)
8749 (for cmp (le eq ne gt)
8752 (cmp (popcount @0) integer_zerop)
8753 (rep @0 { build_zero_cst (TREE_TYPE (@0)); }))))
8755 /* popcount(bswap(x)) is popcount(x). */
8756 (for popcount (POPCOUNT)
8757 (for bswap (BUILT_IN_BSWAP16 BUILT_IN_BSWAP32
8758 BUILT_IN_BSWAP64 BUILT_IN_BSWAP128)
8760 (popcount (convert?@0 (bswap:s@1 @2)))
8761 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
8762 && INTEGRAL_TYPE_P (TREE_TYPE (@1)))
8763 (with { tree type0 = TREE_TYPE (@0);
8764 tree type1 = TREE_TYPE (@1);
8765 unsigned int prec0 = TYPE_PRECISION (type0);
8766 unsigned int prec1 = TYPE_PRECISION (type1); }
8767 (if (prec0 == prec1 || (prec0 > prec1 && TYPE_UNSIGNED (type1)))
8768 (popcount (convert:type0 (convert:type1 @2)))))))))
8770 /* popcount(rotate(X Y)) is popcount(X). */
8771 (for popcount (POPCOUNT)
8772 (for rot (lrotate rrotate)
8774 (popcount (convert?@0 (rot:s@1 @2 @3)))
8775 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
8776 && INTEGRAL_TYPE_P (TREE_TYPE (@1))
8777 && (GIMPLE || !TREE_SIDE_EFFECTS (@3)))
8778 (with { tree type0 = TREE_TYPE (@0);
8779 tree type1 = TREE_TYPE (@1);
8780 unsigned int prec0 = TYPE_PRECISION (type0);
8781 unsigned int prec1 = TYPE_PRECISION (type1); }
8782 (if (prec0 == prec1 || (prec0 > prec1 && TYPE_UNSIGNED (type1)))
8783 (popcount (convert:type0 @2))))))))
8785 /* Canonicalize POPCOUNT(x)&1 as PARITY(X). */
8787 (bit_and (POPCOUNT @0) integer_onep)
8790 /* popcount(X&Y) + popcount(X|Y) is popcount(x) + popcount(Y). */
8792 (plus:c (POPCOUNT:s (bit_and:s @0 @1)) (POPCOUNT:s (bit_ior:cs @0 @1)))
8793 (plus (POPCOUNT:type @0) (POPCOUNT:type @1)))
8795 /* popcount(X) + popcount(Y) - popcount(X&Y) is popcount(X|Y). */
8796 /* popcount(X) + popcount(Y) - popcount(X|Y) is popcount(X&Y). */
8797 (for popcount (POPCOUNT)
8798 (for log1 (bit_and bit_ior)
8799 log2 (bit_ior bit_and)
8801 (minus (plus:s (popcount:s @0) (popcount:s @1))
8802 (popcount:s (log1:cs @0 @1)))
8803 (popcount (log2 @0 @1)))
8805 (plus:c (minus:s (popcount:s @0) (popcount:s (log1:cs @0 @1)))
8807 (popcount (log2 @0 @1)))))
8810 /* popcount(zext(X)) == popcount(X). */
8812 (POPCOUNT (convert@1 @0))
8813 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
8814 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
8815 && TYPE_UNSIGNED (TREE_TYPE (@0))
8816 && TYPE_PRECISION (TREE_TYPE (@1)) > TYPE_PRECISION (TREE_TYPE (@0)))
8817 (with { combined_fn cfn = CFN_LAST;
8818 tree type0 = TREE_TYPE (@0);
8819 if (TREE_CODE (type0) == BITINT_TYPE)
8821 if (TYPE_PRECISION (type0) > MAX_FIXED_MODE_SIZE)
8825 = build_nonstandard_integer_type (TYPE_PRECISION (type0),
8829 && direct_internal_fn_supported_p (IFN_POPCOUNT, type0,
8833 && TYPE_PRECISION (TREE_TYPE (@1)) > BITS_PER_WORD
8834 && !direct_internal_fn_supported_p (IFN_POPCOUNT,
8838 if (TYPE_PRECISION (type0)
8839 == TYPE_PRECISION (unsigned_type_node))
8840 cfn = CFN_BUILT_IN_POPCOUNT;
8841 else if (TYPE_PRECISION (type0)
8842 == TYPE_PRECISION (long_long_unsigned_type_node))
8843 cfn = CFN_BUILT_IN_POPCOUNTLL;
8845 (if (cfn == CFN_POPCOUNT)
8846 (IFN_POPCOUNT (convert:type0 @0))
8847 (if (cfn == CFN_BUILT_IN_POPCOUNT)
8848 (BUILT_IN_POPCOUNT (convert:type0 @0))
8849 (if (cfn == CFN_BUILT_IN_POPCOUNTLL)
8850 (BUILT_IN_POPCOUNTLL (convert:type0 @0))))))))
8853 /* PARITY simplifications. */
8854 /* parity(~X) is parity(X). */
8856 (PARITY (bit_not @0))
8859 /* parity(bswap(x)) is parity(x). */
8860 (for parity (PARITY)
8861 (for bswap (BUILT_IN_BSWAP16 BUILT_IN_BSWAP32
8862 BUILT_IN_BSWAP64 BUILT_IN_BSWAP128)
8864 (parity (convert?@0 (bswap:s@1 @2)))
8865 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
8866 && INTEGRAL_TYPE_P (TREE_TYPE (@1))
8867 && TYPE_PRECISION (TREE_TYPE (@0))
8868 >= TYPE_PRECISION (TREE_TYPE (@1)))
8869 (with { tree type0 = TREE_TYPE (@0);
8870 tree type1 = TREE_TYPE (@1); }
8871 (parity (convert:type0 (convert:type1 @2))))))))
8873 /* parity(rotate(X Y)) is parity(X). */
8874 (for parity (PARITY)
8875 (for rot (lrotate rrotate)
8877 (parity (convert?@0 (rot:s@1 @2 @3)))
8878 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
8879 && INTEGRAL_TYPE_P (TREE_TYPE (@1))
8880 && (GIMPLE || !TREE_SIDE_EFFECTS (@3))
8881 && TYPE_PRECISION (TREE_TYPE (@0))
8882 >= TYPE_PRECISION (TREE_TYPE (@1)))
8883 (with { tree type0 = TREE_TYPE (@0); }
8884 (parity (convert:type0 @2)))))))
8886 /* parity(X)^parity(Y) is parity(X^Y). */
8888 (bit_xor (PARITY:s @0) (PARITY:s @1))
8889 (if (types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
8890 (PARITY (bit_xor @0 @1))
8891 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
8892 && INTEGRAL_TYPE_P (TREE_TYPE (@1)))
8893 (with { tree utype = TREE_TYPE (@0);
8894 if (TYPE_PRECISION (utype) < TYPE_PRECISION (TREE_TYPE (@1)))
8895 utype = TREE_TYPE (@1); }
8896 (PARITY (bit_xor (convert:utype @0) (convert:utype @1)))))))
8899 /* parity(zext(X)) == parity(X). */
8900 /* parity(sext(X)) == parity(X) if the difference in precision is even. */
8902 (PARITY (convert@1 @0))
8903 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
8904 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
8905 && TYPE_PRECISION (TREE_TYPE (@1)) > TYPE_PRECISION (TREE_TYPE (@0))
8906 && (TYPE_UNSIGNED (TREE_TYPE (@0))
8907 || ((TYPE_PRECISION (TREE_TYPE (@1))
8908 - TYPE_PRECISION (TREE_TYPE (@0))) & 1) == 0))
8909 (with { combined_fn cfn = CFN_LAST;
8910 tree type0 = TREE_TYPE (@0);
8911 if (TREE_CODE (type0) == BITINT_TYPE)
8913 if (TYPE_PRECISION (type0) > MAX_FIXED_MODE_SIZE)
8917 = build_nonstandard_integer_type (TYPE_PRECISION (type0),
8920 type0 = unsigned_type_for (type0);
8922 && direct_internal_fn_supported_p (IFN_PARITY, type0,
8926 && TYPE_PRECISION (TREE_TYPE (@1)) > BITS_PER_WORD
8927 && !direct_internal_fn_supported_p (IFN_PARITY,
8931 if (TYPE_PRECISION (type0)
8932 == TYPE_PRECISION (unsigned_type_node))
8933 cfn = CFN_BUILT_IN_PARITY;
8934 else if (TYPE_PRECISION (type0)
8935 == TYPE_PRECISION (long_long_unsigned_type_node))
8936 cfn = CFN_BUILT_IN_PARITYLL;
8938 (if (cfn == CFN_PARITY)
8939 (IFN_PARITY (convert:type0 @0))
8940 (if (cfn == CFN_BUILT_IN_PARITY)
8941 (BUILT_IN_PARITY (convert:type0 @0))
8942 (if (cfn == CFN_BUILT_IN_PARITYLL)
8943 (BUILT_IN_PARITYLL (convert:type0 @0))))))))
8946 /* a != 0 ? FUN(a) : 0 -> Fun(a) for some builtin functions. */
8947 (for func (POPCOUNT BSWAP FFS PARITY)
8949 (cond (ne @0 integer_zerop@1) (func@3 (convert? @0)) integer_zerop@2)
8952 /* a != 0 ? FUN(a) : CST -> Fun(a) for some CLRSB builtins
8953 where CST is precision-1. */
8956 (cond (ne @0 integer_zerop@1) (func@4 (convert?@3 @0)) INTEGER_CST@2)
8957 (if (wi::to_widest (@2) == TYPE_PRECISION (TREE_TYPE (@3)) - 1)
8961 /* a != 0 ? CLZ(a) : CST -> .CLZ(a) where CST is the result of the internal function for 0. */
8964 (cond (ne @0 integer_zerop@1) (func (convert?@3 @0)) INTEGER_CST@2)
8966 internal_fn ifn = IFN_LAST;
8967 if (TREE_CODE (TREE_TYPE (@3)) == BITINT_TYPE)
8969 if (tree_fits_shwi_p (@2))
8971 HOST_WIDE_INT valw = tree_to_shwi (@2);
8972 if ((int) valw == valw)
8979 else if (direct_internal_fn_supported_p (IFN_CLZ, TREE_TYPE (@3),
8981 && CLZ_DEFINED_VALUE_AT_ZERO
8982 (SCALAR_INT_TYPE_MODE (TREE_TYPE (@3)), val) == 2)
8985 (if (ifn == IFN_CLZ && wi::to_widest (@2) == val)
8988 (cond (ne @0 integer_zerop@1) (IFN_CLZ (convert?@3 @0) INTEGER_CST@2) @2)
8990 internal_fn ifn = IFN_LAST;
8991 if (TREE_CODE (TREE_TYPE (@3)) == BITINT_TYPE)
8993 else if (direct_internal_fn_supported_p (IFN_CLZ, TREE_TYPE (@3),
8997 (if (ifn == IFN_CLZ)
9000 /* a != 0 ? CTZ(a) : CST -> .CTZ(a) where CST is the result of the internal function for 0. */
9003 (cond (ne @0 integer_zerop@1) (func (convert?@3 @0)) INTEGER_CST@2)
9005 internal_fn ifn = IFN_LAST;
9006 if (TREE_CODE (TREE_TYPE (@3)) == BITINT_TYPE)
9008 if (tree_fits_shwi_p (@2))
9010 HOST_WIDE_INT valw = tree_to_shwi (@2);
9011 if ((int) valw == valw)
9018 else if (direct_internal_fn_supported_p (IFN_CTZ, TREE_TYPE (@3),
9020 && CTZ_DEFINED_VALUE_AT_ZERO
9021 (SCALAR_INT_TYPE_MODE (TREE_TYPE (@3)), val) == 2)
9024 (if (ifn == IFN_CTZ && wi::to_widest (@2) == val)
9027 (cond (ne @0 integer_zerop@1) (IFN_CTZ (convert?@3 @0) INTEGER_CST@2) @2)
9029 internal_fn ifn = IFN_LAST;
9030 if (TREE_CODE (TREE_TYPE (@3)) == BITINT_TYPE)
9032 else if (direct_internal_fn_supported_p (IFN_CTZ, TREE_TYPE (@3),
9036 (if (ifn == IFN_CTZ)
9040 /* Common POPCOUNT/PARITY simplifications. */
9041 /* popcount(X&C1) is (X>>C2)&1 when C1 == 1<<C2. Same for parity(X&C1). */
9042 (for pfun (POPCOUNT PARITY)
9045 (if (INTEGRAL_TYPE_P (type))
9046 (with { wide_int nz = tree_nonzero_bits (@0); }
9050 (if (wi::popcount (nz) == 1)
9051 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
9052 (convert (rshift:utype (convert:utype @0)
9053 { build_int_cst (integer_type_node,
9054 wi::ctz (nz)); })))))))))
9057 /* 64- and 32-bits branchless implementations of popcount are detected:
9059 int popcount64c (uint64_t x)
9061 x -= (x >> 1) & 0x5555555555555555ULL;
9062 x = (x & 0x3333333333333333ULL) + ((x >> 2) & 0x3333333333333333ULL);
9063 x = (x + (x >> 4)) & 0x0f0f0f0f0f0f0f0fULL;
9064 return (x * 0x0101010101010101ULL) >> 56;
9067 int popcount32c (uint32_t x)
9069 x -= (x >> 1) & 0x55555555;
9070 x = (x & 0x33333333) + ((x >> 2) & 0x33333333);
9071 x = (x + (x >> 4)) & 0x0f0f0f0f;
9072 return (x * 0x01010101) >> 24;
9079 (rshift @8 INTEGER_CST@5)
9081 (bit_and @6 INTEGER_CST@7)
9085 (bit_and (rshift @0 INTEGER_CST@4) INTEGER_CST@11))
9091 /* Check constants and optab. */
9092 (with { unsigned prec = TYPE_PRECISION (type);
9093 int shift = (64 - prec) & 63;
9094 unsigned HOST_WIDE_INT c1
9095 = HOST_WIDE_INT_UC (0x0101010101010101) >> shift;
9096 unsigned HOST_WIDE_INT c2
9097 = HOST_WIDE_INT_UC (0x0F0F0F0F0F0F0F0F) >> shift;
9098 unsigned HOST_WIDE_INT c3
9099 = HOST_WIDE_INT_UC (0x3333333333333333) >> shift;
9100 unsigned HOST_WIDE_INT c4
9101 = HOST_WIDE_INT_UC (0x5555555555555555) >> shift;
9106 && TYPE_UNSIGNED (type)
9107 && integer_onep (@4)
9108 && wi::to_widest (@10) == 2
9109 && wi::to_widest (@5) == 4
9110 && wi::to_widest (@1) == prec - 8
9111 && tree_to_uhwi (@2) == c1
9112 && tree_to_uhwi (@3) == c2
9113 && tree_to_uhwi (@9) == c3
9114 && tree_to_uhwi (@7) == c3
9115 && tree_to_uhwi (@11) == c4)
9116 (if (direct_internal_fn_supported_p (IFN_POPCOUNT, type,
9118 (convert (IFN_POPCOUNT:type @0))
9119 /* Try to do popcount in two halves. PREC must be at least
9120 five bits for this to work without extension before adding. */
9122 tree half_type = NULL_TREE;
9123 opt_machine_mode m = mode_for_size ((prec + 1) / 2, MODE_INT, 1);
9126 && m.require () != TYPE_MODE (type))
9128 half_prec = GET_MODE_PRECISION (as_a <scalar_int_mode> (m));
9129 half_type = build_nonstandard_integer_type (half_prec, 1);
9131 gcc_assert (half_prec > 2);
9133 (if (half_type != NULL_TREE
9134 && direct_internal_fn_supported_p (IFN_POPCOUNT, half_type,
9137 (IFN_POPCOUNT:half_type (convert @0))
9138 (IFN_POPCOUNT:half_type (convert (rshift @0
9139 { build_int_cst (integer_type_node, half_prec); } )))))))))))
9141 /* __builtin_ffs needs to deal on many targets with the possible zero
9142 argument. If we know the argument is always non-zero, __builtin_ctz + 1
9143 should lead to better code. */
9145 (FFS tree_expr_nonzero_p@0)
9146 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
9147 && direct_internal_fn_supported_p (IFN_CTZ, TREE_TYPE (@0),
9148 OPTIMIZE_FOR_SPEED))
9149 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
9150 (plus (CTZ:type (convert:utype @0)) { build_one_cst (type); }))))
9154 /* __builtin_ffs (X) == 0 -> X == 0.
9155 __builtin_ffs (X) == 6 -> (X & 63) == 32. */
9158 (cmp (ffs@2 @0) INTEGER_CST@1)
9159 (with { int prec = TYPE_PRECISION (TREE_TYPE (@0)); }
9161 (if (integer_zerop (@1))
9162 (cmp @0 { build_zero_cst (TREE_TYPE (@0)); }))
9163 (if (tree_int_cst_sgn (@1) < 0 || wi::to_widest (@1) > prec)
9164 { constant_boolean_node (cmp == NE_EXPR ? true : false, type); })
9165 (if (single_use (@2))
9166 (cmp (bit_and @0 { wide_int_to_tree (TREE_TYPE (@0),
9167 wi::mask (tree_to_uhwi (@1),
9169 { wide_int_to_tree (TREE_TYPE (@0),
9170 wi::shifted_mask (tree_to_uhwi (@1) - 1, 1,
9171 false, prec)); }))))))
9173 /* __builtin_ffs (X) > 6 -> X != 0 && (X & 63) == 0. */
9177 bit_op (bit_and bit_ior)
9179 (cmp (ffs@2 @0) INTEGER_CST@1)
9180 (with { int prec = TYPE_PRECISION (TREE_TYPE (@0)); }
9182 (if (integer_zerop (@1))
9183 (cmp2 @0 { build_zero_cst (TREE_TYPE (@0)); }))
9184 (if (tree_int_cst_sgn (@1) < 0)
9185 { constant_boolean_node (cmp == GT_EXPR ? true : false, type); })
9186 (if (wi::to_widest (@1) >= prec)
9187 { constant_boolean_node (cmp == GT_EXPR ? false : true, type); })
9188 (if (wi::to_widest (@1) == prec - 1)
9189 (cmp3 @0 { wide_int_to_tree (TREE_TYPE (@0),
9190 wi::shifted_mask (prec - 1, 1,
9192 (if (single_use (@2))
9193 (bit_op (cmp2 @0 { build_zero_cst (TREE_TYPE (@0)); })
9195 { wide_int_to_tree (TREE_TYPE (@0),
9196 wi::mask (tree_to_uhwi (@1),
9198 { build_zero_cst (TREE_TYPE (@0)); }))))))))
9201 /* ffs(ext(X)) == ffs(X). */
9203 (FFS (convert@1 @0))
9204 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
9205 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
9206 && TYPE_PRECISION (TREE_TYPE (@1)) > TYPE_PRECISION (TREE_TYPE (@0)))
9207 (with { combined_fn cfn = CFN_LAST;
9208 tree type0 = TREE_TYPE (@0);
9209 if (TREE_CODE (type0) == BITINT_TYPE)
9211 if (TYPE_PRECISION (type0) > MAX_FIXED_MODE_SIZE)
9215 = build_nonstandard_integer_type (TYPE_PRECISION (type0),
9218 type0 = signed_type_for (type0);
9220 && direct_internal_fn_supported_p (IFN_FFS, type0,
9224 && TYPE_PRECISION (TREE_TYPE (@1)) > BITS_PER_WORD
9225 && !direct_internal_fn_supported_p (IFN_FFS,
9229 if (TYPE_PRECISION (type0)
9230 == TYPE_PRECISION (integer_type_node))
9231 cfn = CFN_BUILT_IN_FFS;
9232 else if (TYPE_PRECISION (type0)
9233 == TYPE_PRECISION (long_long_integer_type_node))
9234 cfn = CFN_BUILT_IN_FFSLL;
9236 (if (cfn == CFN_FFS)
9237 (IFN_FFS (convert:type0 @0))
9238 (if (cfn == CFN_BUILT_IN_FFS)
9239 (BUILT_IN_FFS (convert:type0 @0))
9240 (if (cfn == CFN_BUILT_IN_FFSLL)
9241 (BUILT_IN_FFSLL (convert:type0 @0))))))))
9249 --> r = .COND_FN (cond, a, b)
9253 --> r = .COND_FN (~cond, b, a). */
9255 (for uncond_op (UNCOND_UNARY)
9256 cond_op (COND_UNARY)
9258 (vec_cond @0 (view_convert? (uncond_op@3 @1)) @2)
9259 (with { tree op_type = TREE_TYPE (@3); }
9260 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
9261 && is_truth_type_for (op_type, TREE_TYPE (@0)))
9262 (cond_op @0 (view_convert @1) @2))))
9264 (vec_cond @0 @1 (view_convert? (uncond_op@3 @2)))
9265 (with { tree op_type = TREE_TYPE (@3); }
9266 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
9267 && is_truth_type_for (op_type, TREE_TYPE (@0)))
9268 (cond_op (bit_not @0) (view_convert @2) @1)))))
9270 (for uncond_op (UNCOND_UNARY)
9271 cond_op (COND_LEN_UNARY)
9273 (IFN_VCOND_MASK_LEN @0 (view_convert? (uncond_op@3 @1)) @2 @4 @5)
9274 (with { tree op_type = TREE_TYPE (@3); }
9275 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
9276 && is_truth_type_for (op_type, TREE_TYPE (@0)))
9277 (cond_op @0 (view_convert @1) @2 @4 @5))))
9279 (IFN_VCOND_MASK_LEN @0 @1 (view_convert? (uncond_op@3 @2)) @4 @5)
9280 (with { tree op_type = TREE_TYPE (@3); }
9281 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
9282 && is_truth_type_for (op_type, TREE_TYPE (@0)))
9283 (cond_op (bit_not @0) (view_convert @2) @1 @4 @5)))))
9285 /* `(a ? -1 : 0) ^ b` can be converted into a conditional not. */
9287 (bit_xor:c (vec_cond @0 uniform_integer_cst_p@1 uniform_integer_cst_p@2) @3)
9288 (if (canonicalize_math_after_vectorization_p ()
9289 && vectorized_internal_fn_supported_p (IFN_COND_NOT, type)
9290 && is_truth_type_for (type, TREE_TYPE (@0)))
9291 (if (integer_all_onesp (@1) && integer_zerop (@2))
9292 (IFN_COND_NOT @0 @3 @3))
9293 (if (integer_all_onesp (@2) && integer_zerop (@1))
9294 (IFN_COND_NOT (bit_not @0) @3 @3))))
9303 r = c ? a1 op a2 : b;
9305 if the target can do it in one go. This makes the operation conditional
9306 on c, so could drop potentially-trapping arithmetic, but that's a valid
9307 simplification if the result of the operation isn't needed.
9309 Avoid speculatively generating a stand-alone vector comparison
9310 on targets that might not support them. Any target implementing
9311 conditional internal functions must support the same comparisons
9312 inside and outside a VEC_COND_EXPR. */
9314 (for uncond_op (UNCOND_BINARY)
9315 cond_op (COND_BINARY)
9317 (vec_cond @0 (view_convert? (uncond_op@4 @1 @2)) @3)
9318 (with { tree op_type = TREE_TYPE (@4); }
9319 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
9320 && is_truth_type_for (op_type, TREE_TYPE (@0))
9322 (view_convert (cond_op @0 @1 @2 (view_convert:op_type @3))))))
9324 (vec_cond @0 @1 (view_convert? (uncond_op@4 @2 @3)))
9325 (with { tree op_type = TREE_TYPE (@4); }
9326 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
9327 && is_truth_type_for (op_type, TREE_TYPE (@0))
9329 (view_convert (cond_op (bit_not @0) @2 @3 (view_convert:op_type @1)))))))
9331 (for uncond_op (UNCOND_BINARY)
9332 cond_op (COND_LEN_BINARY)
9334 (IFN_VCOND_MASK_LEN @0 (view_convert? (uncond_op@4 @1 @2)) @3 @5 @6)
9335 (with { tree op_type = TREE_TYPE (@4); }
9336 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
9337 && is_truth_type_for (op_type, TREE_TYPE (@0))
9339 (view_convert (cond_op @0 @1 @2 (view_convert:op_type @3) @5 @6)))))
9341 (IFN_VCOND_MASK_LEN @0 @1 (view_convert? (uncond_op@4 @2 @3)) @5 @6)
9342 (with { tree op_type = TREE_TYPE (@4); }
9343 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
9344 && is_truth_type_for (op_type, TREE_TYPE (@0))
9346 (view_convert (cond_op (bit_not @0) @2 @3 (view_convert:op_type @1) @5 @6))))))
9348 /* Same for ternary operations. */
9349 (for uncond_op (UNCOND_TERNARY)
9350 cond_op (COND_TERNARY)
9352 (vec_cond @0 (view_convert? (uncond_op@5 @1 @2 @3)) @4)
9353 (with { tree op_type = TREE_TYPE (@5); }
9354 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
9355 && is_truth_type_for (op_type, TREE_TYPE (@0))
9357 (view_convert (cond_op @0 @1 @2 @3 (view_convert:op_type @4))))))
9359 (vec_cond @0 @1 (view_convert? (uncond_op@5 @2 @3 @4)))
9360 (with { tree op_type = TREE_TYPE (@5); }
9361 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
9362 && is_truth_type_for (op_type, TREE_TYPE (@0))
9364 (view_convert (cond_op (bit_not @0) @2 @3 @4
9365 (view_convert:op_type @1)))))))
9367 (for uncond_op (UNCOND_TERNARY)
9368 cond_op (COND_LEN_TERNARY)
9370 (IFN_VCOND_MASK_LEN @0 (view_convert? (uncond_op@5 @1 @2 @3)) @4 @6 @7)
9371 (with { tree op_type = TREE_TYPE (@5); }
9372 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
9373 && is_truth_type_for (op_type, TREE_TYPE (@0))
9375 (view_convert (cond_op @0 @1 @2 @3 (view_convert:op_type @4) @6 @7)))))
9377 (IFN_VCOND_MASK_LEN @0 @1 (view_convert? (uncond_op@5 @2 @3 @4 @6 @7)))
9378 (with { tree op_type = TREE_TYPE (@5); }
9379 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
9380 && is_truth_type_for (op_type, TREE_TYPE (@0))
9382 (view_convert (cond_op (bit_not @0) @2 @3 @4 (view_convert:op_type @1) @6 @7))))))
9385 /* Detect cases in which a VEC_COND_EXPR effectively replaces the
9386 "else" value of an IFN_COND_*. */
9387 (for cond_op (COND_BINARY)
9389 (vec_cond @0 (view_convert? (cond_op @0 @1 @2 @3)) @4)
9390 (with { tree op_type = TREE_TYPE (@3); }
9391 (if (element_precision (type) == element_precision (op_type))
9392 (view_convert (cond_op @0 @1 @2 (view_convert:op_type @4))))))
9394 (vec_cond @0 @1 (view_convert? (cond_op @2 @3 @4 @5)))
9395 (with { tree op_type = TREE_TYPE (@5); }
9396 (if (inverse_conditions_p (@0, @2)
9397 && element_precision (type) == element_precision (op_type))
9398 (view_convert (cond_op @2 @3 @4 (view_convert:op_type @1)))))))
9400 /* Same for ternary operations. */
9401 (for cond_op (COND_TERNARY)
9403 (vec_cond @0 (view_convert? (cond_op @0 @1 @2 @3 @4)) @5)
9404 (with { tree op_type = TREE_TYPE (@4); }
9405 (if (element_precision (type) == element_precision (op_type))
9406 (view_convert (cond_op @0 @1 @2 @3 (view_convert:op_type @5))))))
9408 (vec_cond @0 @1 (view_convert? (cond_op @2 @3 @4 @5 @6)))
9409 (with { tree op_type = TREE_TYPE (@6); }
9410 (if (inverse_conditions_p (@0, @2)
9411 && element_precision (type) == element_precision (op_type))
9412 (view_convert (cond_op @2 @3 @4 @5 (view_convert:op_type @1)))))))
9414 /* Detect cases in which a VEC_COND_EXPR effectively replaces the
9415 "else" value of an IFN_COND_LEN_*. */
9416 (for cond_len_op (COND_LEN_BINARY)
9418 (vec_cond @0 (view_convert? (cond_len_op @0 @1 @2 @3 @4 @5)) @6)
9419 (with { tree op_type = TREE_TYPE (@3); }
9420 (if (element_precision (type) == element_precision (op_type))
9421 (view_convert (cond_len_op @0 @1 @2 (view_convert:op_type @6) @4 @5)))))
9423 (vec_cond @0 @1 (view_convert? (cond_len_op @2 @3 @4 @5 @6 @7)))
9424 (with { tree op_type = TREE_TYPE (@5); }
9425 (if (inverse_conditions_p (@0, @2)
9426 && element_precision (type) == element_precision (op_type))
9427 (view_convert (cond_len_op @2 @3 @4 (view_convert:op_type @1) @6 @7))))))
9429 /* Same for ternary operations. */
9430 (for cond_len_op (COND_LEN_TERNARY)
9432 (vec_cond @0 (view_convert? (cond_len_op @0 @1 @2 @3 @4 @5 @6)) @7)
9433 (with { tree op_type = TREE_TYPE (@4); }
9434 (if (element_precision (type) == element_precision (op_type))
9435 (view_convert (cond_len_op @0 @1 @2 @3 (view_convert:op_type @7) @5 @6)))))
9437 (vec_cond @0 @1 (view_convert? (cond_len_op @2 @3 @4 @5 @6 @7 @8)))
9438 (with { tree op_type = TREE_TYPE (@6); }
9439 (if (inverse_conditions_p (@0, @2)
9440 && element_precision (type) == element_precision (op_type))
9441 (view_convert (cond_len_op @2 @3 @4 @5 (view_convert:op_type @1) @7 @8))))))
9443 /* Detect simplication for a conditional reduction where
9446 c = mask2 ? d + a : d
9450 c = mask1 && mask2 ? d + b : d. */
9452 (IFN_COND_ADD @0 @1 (vec_cond @2 @3 zerop@4) @1)
9453 (if (ANY_INTEGRAL_TYPE_P (type)
9454 || (FLOAT_TYPE_P (type)
9455 && fold_real_zero_addition_p (type, NULL_TREE, @4, 0)))
9456 (IFN_COND_ADD (bit_and @0 @2) @1 @3 @1)))
9458 /* Detect simplication for a conditional length reduction where
9461 c = i < len + bias ? d + a : d
9465 c = mask && i < len + bias ? d + b : d. */
9467 (IFN_COND_LEN_ADD integer_truep @0 (vec_cond @1 @2 zerop@5) @0 @3 @4)
9468 (if (ANY_INTEGRAL_TYPE_P (type)
9469 || (FLOAT_TYPE_P (type)
9470 && fold_real_zero_addition_p (type, NULL_TREE, @5, 0)))
9471 (IFN_COND_LEN_ADD @1 @0 @2 @0 @3 @4)))
9473 /* Detect simplification for vector condition folding where
9475 c = mask1 ? (masked_op mask2 a b) : b
9479 c = masked_op (mask1 & mask2) a b
9481 where the operation can be partially applied to one operand. */
9483 (for cond_op (COND_BINARY)
9486 (cond_op:s @1 @2 @3 @4) @3)
9487 (cond_op (bit_and @1 @0) @2 @3 @4)))
9489 /* And same for ternary expressions. */
9491 (for cond_op (COND_TERNARY)
9494 (cond_op:s @1 @2 @3 @4 @5) @4)
9495 (cond_op (bit_and @1 @0) @2 @3 @4 @5)))
9497 /* For pointers @0 and @2 and nonnegative constant offset @1, look for
9500 A: (@0 + @1 < @2) | (@2 + @1 < @0)
9501 B: (@0 + @1 <= @2) | (@2 + @1 <= @0)
9503 If pointers are known not to wrap, B checks whether @1 bytes starting
9504 at @0 and @2 do not overlap, while A tests the same thing for @1 + 1
9505 bytes. A is more efficiently tested as:
9507 A: (sizetype) (@0 + @1 - @2) > @1 * 2
9509 The equivalent expression for B is given by replacing @1 with @1 - 1:
9511 B: (sizetype) (@0 + (@1 - 1) - @2) > (@1 - 1) * 2
9513 @0 and @2 can be swapped in both expressions without changing the result.
9515 The folds rely on sizetype's being unsigned (which is always true)
9516 and on its being the same width as the pointer (which we have to check).
9518 The fold replaces two pointer_plus expressions, two comparisons and
9519 an IOR with a pointer_plus, a pointer_diff, and a comparison, so in
9520 the best case it's a saving of two operations. The A fold retains one
9521 of the original pointer_pluses, so is a win even if both pointer_pluses
9522 are used elsewhere. The B fold is a wash if both pointer_pluses are
9523 used elsewhere, since all we end up doing is replacing a comparison with
9524 a pointer_plus. We do still apply the fold under those circumstances
9525 though, in case applying it to other conditions eventually makes one of the
9526 pointer_pluses dead. */
9527 (for ior (truth_orif truth_or bit_ior)
9530 (ior (cmp:cs (pointer_plus@3 @0 INTEGER_CST@1) @2)
9531 (cmp:cs (pointer_plus@4 @2 @1) @0))
9532 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
9533 && TYPE_OVERFLOW_WRAPS (sizetype)
9534 && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (sizetype))
9535 /* Calculate the rhs constant. */
9536 (with { offset_int off = wi::to_offset (@1) - (cmp == LE_EXPR ? 1 : 0);
9537 offset_int rhs = off * 2; }
9538 /* Always fails for negative values. */
9539 (if (wi::min_precision (rhs, UNSIGNED) <= TYPE_PRECISION (sizetype))
9540 /* Since the order of @0 and @2 doesn't matter, let tree_swap_operands_p
9541 pick a canonical order. This increases the chances of using the
9542 same pointer_plus in multiple checks. */
9543 (with { bool swap_p = tree_swap_operands_p (@0, @2);
9544 tree rhs_tree = wide_int_to_tree (sizetype, rhs); }
9545 (if (cmp == LT_EXPR)
9546 (gt (convert:sizetype
9547 (pointer_diff:ssizetype { swap_p ? @4 : @3; }
9548 { swap_p ? @0 : @2; }))
9550 (gt (convert:sizetype
9551 (pointer_diff:ssizetype
9552 (pointer_plus { swap_p ? @2 : @0; }
9553 { wide_int_to_tree (sizetype, off); })
9554 { swap_p ? @0 : @2; }))
9555 { rhs_tree; })))))))))
9557 /* Fold REDUC (@0 & @1) -> @0[I] & @1[I] if element I is the only nonzero
9559 (for reduc (IFN_REDUC_PLUS IFN_REDUC_IOR IFN_REDUC_XOR)
9560 (simplify (reduc (view_convert? (bit_and @0 VECTOR_CST@1)))
9561 (with { int i = single_nonzero_element (@1); }
9563 (with { tree elt = vector_cst_elt (@1, i);
9564 tree elt_type = TREE_TYPE (elt);
9565 unsigned int elt_bits = tree_to_uhwi (TYPE_SIZE (elt_type));
9566 tree size = bitsize_int (elt_bits);
9567 tree pos = bitsize_int (elt_bits * i); }
9570 (BIT_FIELD_REF:elt_type @0 { size; } { pos; })
9573 /* Fold reduction of a single nonzero element constructor. */
9574 (for reduc (IFN_REDUC_PLUS IFN_REDUC_IOR IFN_REDUC_XOR)
9575 (simplify (reduc (CONSTRUCTOR@0))
9576 (with { tree ctor = (TREE_CODE (@0) == SSA_NAME
9577 ? gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0)) : @0);
9578 tree elt = ctor_single_nonzero_element (ctor); }
9580 && !HONOR_SNANS (type)
9581 && !HONOR_SIGNED_ZEROS (type))
9584 /* Fold REDUC (@0 op VECTOR_CST) as REDUC (@0) op REDUC (VECTOR_CST). */
9585 (for reduc (IFN_REDUC_PLUS IFN_REDUC_MAX IFN_REDUC_MIN IFN_REDUC_FMAX
9586 IFN_REDUC_FMIN IFN_REDUC_AND IFN_REDUC_IOR IFN_REDUC_XOR)
9587 op (plus max min IFN_FMAX IFN_FMIN bit_and bit_ior bit_xor)
9588 (simplify (reduc (op @0 VECTOR_CST@1))
9589 (op (reduc:type @0) (reduc:type @1))))
9591 /* Simplify vector floating point operations of alternating sub/add pairs
9592 into using an fneg of a wider element type followed by a normal add.
9593 under IEEE 754 the fneg of the wider type will negate every even entry
9594 and when doing an add we get a sub of the even and add of every odd
9596 (for plusminus (plus minus)
9597 minusplus (minus plus)
9599 (vec_perm (plusminus @0 @1) (minusplus @2 @3) VECTOR_CST@4)
9600 (if (!VECTOR_INTEGER_TYPE_P (type)
9601 && !FLOAT_WORDS_BIG_ENDIAN
9602 /* plus is commutative, while minus is not, so :c can't be used.
9603 Do equality comparisons by hand and at the end pick the operands
9605 && (operand_equal_p (@0, @2, 0)
9606 ? operand_equal_p (@1, @3, 0)
9607 : operand_equal_p (@0, @3, 0) && operand_equal_p (@1, @2, 0)))
9610 /* Build a vector of integers from the tree mask. */
9611 vec_perm_builder builder;
9613 (if (tree_to_vec_perm_builder (&builder, @4))
9616 /* Create a vec_perm_indices for the integer vector. */
9617 poly_uint64 nelts = TYPE_VECTOR_SUBPARTS (type);
9618 vec_perm_indices sel (builder, 2, nelts);
9619 machine_mode vec_mode = TYPE_MODE (type);
9620 machine_mode wide_mode;
9621 scalar_mode wide_elt_mode;
9622 poly_uint64 wide_nunits;
9623 scalar_mode inner_mode = GET_MODE_INNER (vec_mode);
9625 (if (VECTOR_MODE_P (vec_mode)
9626 && sel.series_p (0, 2, 0, 2)
9627 && sel.series_p (1, 2, nelts + 1, 2)
9628 && GET_MODE_2XWIDER_MODE (inner_mode).exists (&wide_elt_mode)
9629 && multiple_p (GET_MODE_NUNITS (vec_mode), 2, &wide_nunits)
9630 && related_vector_mode (vec_mode, wide_elt_mode,
9631 wide_nunits).exists (&wide_mode))
9635 = lang_hooks.types.type_for_mode (GET_MODE_INNER (wide_mode),
9636 TYPE_UNSIGNED (type));
9637 tree ntype = build_vector_type_for_mode (stype, wide_mode);
9639 /* The format has to be a non-extended ieee format. */
9640 const struct real_format *fmt_old = FLOAT_MODE_FORMAT (vec_mode);
9641 const struct real_format *fmt_new = FLOAT_MODE_FORMAT (wide_mode);
9643 (if (TYPE_MODE (stype) != BLKmode
9644 && VECTOR_TYPE_P (ntype)
9649 /* If the target doesn't support v1xx vectors, try using
9650 scalar mode xx instead. */
9651 if (known_eq (GET_MODE_NUNITS (wide_mode), 1)
9652 && !target_supports_op_p (ntype, NEGATE_EXPR, optab_vector))
9655 (if (fmt_new->signbit_rw
9656 == fmt_old->signbit_rw + GET_MODE_UNIT_BITSIZE (vec_mode)
9657 && fmt_new->signbit_rw == fmt_new->signbit_ro
9658 && targetm.can_change_mode_class (TYPE_MODE (ntype),
9659 TYPE_MODE (type), ALL_REGS)
9660 && ((optimize_vectors_before_lowering_p ()
9661 && VECTOR_TYPE_P (ntype))
9662 || target_supports_op_p (ntype, NEGATE_EXPR, optab_vector)))
9663 (if (plusminus == PLUS_EXPR)
9664 (plus (view_convert:type (negate (view_convert:ntype @3))) @2)
9665 (minus @0 (view_convert:type
9666 (negate (view_convert:ntype @1))))))))))))))))
9669 (vec_perm @0 @1 VECTOR_CST@2)
9672 tree op0 = @0, op1 = @1, op2 = @2;
9673 machine_mode result_mode = TYPE_MODE (type);
9674 machine_mode op_mode = TYPE_MODE (TREE_TYPE (op0));
9676 /* Build a vector of integers from the tree mask. */
9677 vec_perm_builder builder;
9679 (if (tree_to_vec_perm_builder (&builder, op2))
9682 /* Create a vec_perm_indices for the integer vector. */
9683 poly_uint64 nelts = TYPE_VECTOR_SUBPARTS (type);
9684 bool single_arg = (op0 == op1);
9685 vec_perm_indices sel (builder, single_arg ? 1 : 2, nelts);
9687 (if (sel.series_p (0, 1, 0, 1))
9689 (if (sel.series_p (0, 1, nelts, 1))
9695 if (sel.all_from_input_p (0))
9697 else if (sel.all_from_input_p (1))
9700 sel.rotate_inputs (1);
9702 else if (known_ge (poly_uint64 (sel[0]), nelts))
9704 std::swap (op0, op1);
9705 sel.rotate_inputs (1);
9709 tree cop0 = op0, cop1 = op1;
9710 if (TREE_CODE (op0) == SSA_NAME
9711 && (def = dyn_cast <gassign *> (SSA_NAME_DEF_STMT (op0)))
9712 && gimple_assign_rhs_code (def) == CONSTRUCTOR)
9713 cop0 = gimple_assign_rhs1 (def);
9714 if (TREE_CODE (op1) == SSA_NAME
9715 && (def = dyn_cast <gassign *> (SSA_NAME_DEF_STMT (op1)))
9716 && gimple_assign_rhs_code (def) == CONSTRUCTOR)
9717 cop1 = gimple_assign_rhs1 (def);
9720 (if ((TREE_CODE (cop0) == VECTOR_CST
9721 || TREE_CODE (cop0) == CONSTRUCTOR)
9722 && (TREE_CODE (cop1) == VECTOR_CST
9723 || TREE_CODE (cop1) == CONSTRUCTOR)
9724 && (t = fold_vec_perm (type, cop0, cop1, sel)))
9728 bool changed = (op0 == op1 && !single_arg);
9729 tree ins = NULL_TREE;
9732 /* See if the permutation is performing a single element
9733 insert from a CONSTRUCTOR or constant and use a BIT_INSERT_EXPR
9734 in that case. But only if the vector mode is supported,
9735 otherwise this is invalid GIMPLE. */
9736 if (op_mode != BLKmode
9737 && (TREE_CODE (cop0) == VECTOR_CST
9738 || TREE_CODE (cop0) == CONSTRUCTOR
9739 || TREE_CODE (cop1) == VECTOR_CST
9740 || TREE_CODE (cop1) == CONSTRUCTOR))
9742 bool insert_first_p = sel.series_p (1, 1, nelts + 1, 1);
9745 /* After canonicalizing the first elt to come from the
9746 first vector we only can insert the first elt from
9747 the first vector. */
9749 if ((ins = fold_read_from_vector (cop0, sel[0])))
9752 /* The above can fail for two-element vectors which always
9753 appear to insert the first element, so try inserting
9754 into the second lane as well. For more than two
9755 elements that's wasted time. */
9756 if (!insert_first_p || (!ins && maybe_eq (nelts, 2u)))
9758 unsigned int encoded_nelts = sel.encoding ().encoded_nelts ();
9759 for (at = 0; at < encoded_nelts; ++at)
9760 if (maybe_ne (sel[at], at))
9762 if (at < encoded_nelts
9763 && (known_eq (at + 1, nelts)
9764 || sel.series_p (at + 1, 1, at + 1, 1)))
9766 if (known_lt (poly_uint64 (sel[at]), nelts))
9767 ins = fold_read_from_vector (cop0, sel[at]);
9769 ins = fold_read_from_vector (cop1, sel[at] - nelts);
9774 /* Generate a canonical form of the selector. */
9775 if (!ins && sel.encoding () != builder)
9777 /* Some targets are deficient and fail to expand a single
9778 argument permutation while still allowing an equivalent
9779 2-argument version. */
9781 if (sel.ninputs () == 2
9782 || can_vec_perm_const_p (result_mode, op_mode, sel, false))
9783 op2 = vec_perm_indices_to_tree (TREE_TYPE (op2), sel);
9786 vec_perm_indices sel2 (builder, 2, nelts);
9787 if (can_vec_perm_const_p (result_mode, op_mode, sel2, false))
9788 op2 = vec_perm_indices_to_tree (TREE_TYPE (op2), sel2);
9790 /* Not directly supported with either encoding,
9791 so use the preferred form. */
9792 op2 = vec_perm_indices_to_tree (TREE_TYPE (op2), sel);
9794 if (!operand_equal_p (op2, oldop2, 0))
9799 (bit_insert { op0; } { ins; }
9800 { bitsize_int (at * vector_element_bits (type)); })
9802 (vec_perm { op0; } { op1; } { op2; }))))))))))))
9804 /* VEC_PERM_EXPR (v, v, mask) -> v where v contains same element. */
9806 (match vec_same_elem_p
9809 (match vec_same_elem_p
9811 (if (TREE_CODE (@0) == SSA_NAME
9812 && uniform_vector_p (gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0))))))
9814 (match vec_same_elem_p
9816 (if (uniform_vector_p (@0))))
9820 (vec_perm vec_same_elem_p@0 @0 @1)
9821 (if (types_match (type, TREE_TYPE (@0)))
9825 tree elem = uniform_vector_p (@0);
9828 { build_vector_from_val (type, elem); }))))
9830 /* Push VEC_PERM earlier if that may help FMA perception (PR101895). */
9832 (plus:c (vec_perm:s (mult:c@0 @1 vec_same_elem_p@2) @0 @3) @4)
9833 (if (TREE_CODE (@0) == SSA_NAME && num_imm_uses (@0) == 2)
9834 (plus (mult (vec_perm @1 @1 @3) @2) @4)))
9836 (minus (vec_perm:s (mult:c@0 @1 vec_same_elem_p@2) @0 @3) @4)
9837 (if (TREE_CODE (@0) == SSA_NAME && num_imm_uses (@0) == 2)
9838 (minus (mult (vec_perm @1 @1 @3) @2) @4)))
9842 c = VEC_PERM_EXPR <a, b, VCST0>;
9843 d = VEC_PERM_EXPR <c, c, VCST1>;
9845 d = VEC_PERM_EXPR <a, b, NEW_VCST>; */
9848 (vec_perm (vec_perm@0 @1 @2 VECTOR_CST@3) @0 VECTOR_CST@4)
9849 (if (TYPE_VECTOR_SUBPARTS (type).is_constant ())
9852 machine_mode result_mode = TYPE_MODE (type);
9853 machine_mode op_mode = TYPE_MODE (TREE_TYPE (@1));
9854 int nelts = TYPE_VECTOR_SUBPARTS (type).to_constant ();
9855 vec_perm_builder builder0;
9856 vec_perm_builder builder1;
9857 vec_perm_builder builder2 (nelts, nelts, 1);
9859 (if (tree_to_vec_perm_builder (&builder0, @3)
9860 && tree_to_vec_perm_builder (&builder1, @4))
9863 vec_perm_indices sel0 (builder0, 2, nelts);
9864 vec_perm_indices sel1 (builder1, 1, nelts);
9866 for (int i = 0; i < nelts; i++)
9867 builder2.quick_push (sel0[sel1[i].to_constant ()]);
9869 vec_perm_indices sel2 (builder2, 2, nelts);
9871 tree op0 = NULL_TREE;
9872 /* If the new VEC_PERM_EXPR can't be handled but both
9873 original VEC_PERM_EXPRs can, punt.
9874 If one or both of the original VEC_PERM_EXPRs can't be
9875 handled and the new one can't be either, don't increase
9876 number of VEC_PERM_EXPRs that can't be handled. */
9877 if (can_vec_perm_const_p (result_mode, op_mode, sel2, false)
9879 ? (!can_vec_perm_const_p (result_mode, op_mode, sel0, false)
9880 || !can_vec_perm_const_p (result_mode, op_mode, sel1, false))
9881 : !can_vec_perm_const_p (result_mode, op_mode, sel1, false)))
9882 op0 = vec_perm_indices_to_tree (TREE_TYPE (@4), sel2);
9885 (vec_perm @1 @2 { op0; })))))))
9888 c = VEC_PERM_EXPR <a, b, VCST0>;
9889 d = VEC_PERM_EXPR <x, c, VCST1>;
9891 d = VEC_PERM_EXPR <x, {a,b}, NEW_VCST>;
9892 when all elements from a or b are replaced by the later
9896 (vec_perm @5 (vec_perm@0 @1 @2 VECTOR_CST@3) VECTOR_CST@4)
9897 (if (TYPE_VECTOR_SUBPARTS (type).is_constant ())
9900 machine_mode result_mode = TYPE_MODE (type);
9901 machine_mode op_mode = TYPE_MODE (TREE_TYPE (@1));
9902 int nelts = TYPE_VECTOR_SUBPARTS (type).to_constant ();
9903 vec_perm_builder builder0;
9904 vec_perm_builder builder1;
9905 vec_perm_builder builder2 (nelts, nelts, 2);
9907 (if (tree_to_vec_perm_builder (&builder0, @3)
9908 && tree_to_vec_perm_builder (&builder1, @4))
9911 vec_perm_indices sel0 (builder0, 2, nelts);
9912 vec_perm_indices sel1 (builder1, 2, nelts);
9913 bool use_1 = false, use_2 = false;
9915 for (int i = 0; i < nelts; i++)
9917 if (known_lt ((poly_uint64)sel1[i], sel1.nelts_per_input ()))
9918 builder2.quick_push (sel1[i]);
9921 poly_uint64 j = sel0[(sel1[i] - sel1.nelts_per_input ())
9923 if (known_lt (j, sel0.nelts_per_input ()))
9928 j -= sel0.nelts_per_input ();
9930 builder2.quick_push (j + sel1.nelts_per_input ());
9937 vec_perm_indices sel2 (builder2, 2, nelts);
9938 tree op0 = NULL_TREE;
9939 /* If the new VEC_PERM_EXPR can't be handled but both
9940 original VEC_PERM_EXPRs can, punt.
9941 If one or both of the original VEC_PERM_EXPRs can't be
9942 handled and the new one can't be either, don't increase
9943 number of VEC_PERM_EXPRs that can't be handled. */
9944 if (can_vec_perm_const_p (result_mode, op_mode, sel2, false)
9946 ? (!can_vec_perm_const_p (result_mode, op_mode, sel0, false)
9947 || !can_vec_perm_const_p (result_mode, op_mode, sel1, false))
9948 : !can_vec_perm_const_p (result_mode, op_mode, sel1, false)))
9949 op0 = vec_perm_indices_to_tree (TREE_TYPE (@4), sel2);
9954 (vec_perm @5 @1 { op0; }))
9956 (vec_perm @5 @2 { op0; })))))))))))
9958 /* And the case with swapped outer permute sources. */
9961 (vec_perm (vec_perm@0 @1 @2 VECTOR_CST@3) @5 VECTOR_CST@4)
9962 (if (TYPE_VECTOR_SUBPARTS (type).is_constant ())
9965 machine_mode result_mode = TYPE_MODE (type);
9966 machine_mode op_mode = TYPE_MODE (TREE_TYPE (@1));
9967 int nelts = TYPE_VECTOR_SUBPARTS (type).to_constant ();
9968 vec_perm_builder builder0;
9969 vec_perm_builder builder1;
9970 vec_perm_builder builder2 (nelts, nelts, 2);
9972 (if (tree_to_vec_perm_builder (&builder0, @3)
9973 && tree_to_vec_perm_builder (&builder1, @4))
9976 vec_perm_indices sel0 (builder0, 2, nelts);
9977 vec_perm_indices sel1 (builder1, 2, nelts);
9978 bool use_1 = false, use_2 = false;
9980 for (int i = 0; i < nelts; i++)
9982 if (known_ge ((poly_uint64)sel1[i], sel1.nelts_per_input ()))
9983 builder2.quick_push (sel1[i]);
9986 poly_uint64 j = sel0[sel1[i].to_constant ()];
9987 if (known_lt (j, sel0.nelts_per_input ()))
9992 j -= sel0.nelts_per_input ();
9994 builder2.quick_push (j);
10001 vec_perm_indices sel2 (builder2, 2, nelts);
10002 tree op0 = NULL_TREE;
10003 /* If the new VEC_PERM_EXPR can't be handled but both
10004 original VEC_PERM_EXPRs can, punt.
10005 If one or both of the original VEC_PERM_EXPRs can't be
10006 handled and the new one can't be either, don't increase
10007 number of VEC_PERM_EXPRs that can't be handled. */
10008 if (can_vec_perm_const_p (result_mode, op_mode, sel2, false)
10009 || (single_use (@0)
10010 ? (!can_vec_perm_const_p (result_mode, op_mode, sel0, false)
10011 || !can_vec_perm_const_p (result_mode, op_mode, sel1, false))
10012 : !can_vec_perm_const_p (result_mode, op_mode, sel1, false)))
10013 op0 = vec_perm_indices_to_tree (TREE_TYPE (@4), sel2);
10018 (vec_perm @1 @5 { op0; }))
10020 (vec_perm @2 @5 { op0; })))))))))))
10023 /* Match count trailing zeroes for simplify_count_trailing_zeroes in fwprop.
10024 The canonical form is array[((x & -x) * C) >> SHIFT] where C is a magic
10025 constant which when multiplied by a power of 2 contains a unique value
10026 in the top 5 or 6 bits. This is then indexed into a table which maps it
10027 to the number of trailing zeroes. */
10028 (match (ctz_table_index @1 @2 @3)
10029 (rshift (mult (bit_and:c (negate @1) @1) INTEGER_CST@2) INTEGER_CST@3))
10031 (match (cond_expr_convert_p @0 @2 @3 @6)
10032 (cond (simple_comparison@6 @0 @1) (convert@4 @2) (convert@5 @3))
10033 (if (INTEGRAL_TYPE_P (type)
10034 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
10035 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
10036 && INTEGRAL_TYPE_P (TREE_TYPE (@3))
10037 && TYPE_PRECISION (type) != TYPE_PRECISION (TREE_TYPE (@0))
10038 && TYPE_PRECISION (TREE_TYPE (@0))
10039 == TYPE_PRECISION (TREE_TYPE (@2))
10040 && TYPE_PRECISION (TREE_TYPE (@0))
10041 == TYPE_PRECISION (TREE_TYPE (@3))
10042 /* For vect_recog_cond_expr_convert_pattern, @2 and @3 can differ in
10043 signess when convert is truncation, but not ok for extension since
10044 it's sign_extend vs zero_extend. */
10045 && (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type)
10046 || (TYPE_UNSIGNED (TREE_TYPE (@2))
10047 == TYPE_UNSIGNED (TREE_TYPE (@3))))
10049 && single_use (@5))))
10051 (for bit_op (bit_and bit_ior bit_xor)
10052 (match (bitwise_induction_p @0 @2 @3)
10054 (nop_convert1? (bit_not2?@0 (convert3? (lshift integer_onep@1 @2))))
10057 (match (bitwise_induction_p @0 @2 @3)
10059 (nop_convert1? (bit_xor@0 (convert2? (lshift integer_onep@1 @2)) @3))))
10061 /* n - (((n > C1) ? n : C1) & -C2) -> n & C1 for unsigned case.
10062 n - (((n > C1) ? n : C1) & -C2) -> (n <= C1) ? n : (n & C1) for signed case. */
10064 (minus @0 (bit_and (max @0 INTEGER_CST@1) INTEGER_CST@2))
10065 (with { auto i = wi::neg (wi::to_wide (@2)); }
10066 /* Check if -C2 is a power of 2 and C1 = -C2 - 1. */
10067 (if (wi::popcount (i) == 1
10068 && (wi::to_wide (@1)) == (i - 1))
10069 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
10071 (cond (le @0 @1) @0 (bit_and @0 @1))))))
10073 /* -x & 1 -> x & 1. */
10075 (bit_and (negate @0) integer_onep@1)
10076 (if (!TYPE_OVERFLOW_SANITIZED (type))
10079 /* `-a` is just `a` if the type is 1bit wide or when converting
10080 to a 1bit type; similar to the above transformation of `(-x)&1`.
10081 This is used mostly with the transformation of
10082 `a ? ~b : b` into `(-a)^b`.
10083 It also can show up with bitfields. */
10085 (convert? (negate @0))
10086 (if (INTEGRAL_TYPE_P (type)
10087 && TYPE_PRECISION (type) == 1
10088 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0)))
10092 c1 = VEC_PERM_EXPR (a, a, mask)
10093 c2 = VEC_PERM_EXPR (b, b, mask)
10097 c3 = VEC_PERM_EXPR (c, c, mask)
10098 For all integer non-div operations. */
10099 (for op (plus minus mult bit_and bit_ior bit_xor
10102 (op (vec_perm @0 @0 @2) (vec_perm @1 @1 @2))
10103 (if (VECTOR_INTEGER_TYPE_P (type))
10104 (vec_perm (op@3 @0 @1) @3 @2))))
10106 /* Similar for float arithmetic when permutation constant covers
10107 all vector elements. */
10108 (for op (plus minus mult)
10110 (op (vec_perm @0 @0 VECTOR_CST@2) (vec_perm @1 @1 VECTOR_CST@2))
10111 (if (VECTOR_FLOAT_TYPE_P (type)
10112 && TYPE_VECTOR_SUBPARTS (type).is_constant ())
10115 tree perm_cst = @2;
10116 vec_perm_builder builder;
10117 bool full_perm_p = false;
10118 if (tree_to_vec_perm_builder (&builder, perm_cst))
10120 unsigned HOST_WIDE_INT nelts;
10122 nelts = TYPE_VECTOR_SUBPARTS (type).to_constant ();
10123 /* Create a vec_perm_indices for the VECTOR_CST. */
10124 vec_perm_indices sel (builder, 1, nelts);
10126 /* Check if perm indices covers all vector elements. */
10127 if (sel.encoding ().encoded_full_vector_p ())
10129 auto_sbitmap seen (nelts);
10130 bitmap_clear (seen);
10132 unsigned HOST_WIDE_INT count = 0, i;
10134 for (i = 0; i < nelts; i++)
10136 if (!bitmap_set_bit (seen, sel[i].to_constant ()))
10140 full_perm_p = count == nelts;
10145 (vec_perm (op@3 @0 @1) @3 @2))))))