1 /* Match-and-simplify patterns for shared GENERIC and GIMPLE folding.
2 This file is consumed by genmatch which produces gimple-match.cc
3 and generic-match.cc from it.
5 Copyright (C) 2014-2023 Free Software Foundation, Inc.
6 Contributed by Richard Biener <rguenther@suse.de>
7 and Prathamesh Kulkarni <bilbotheelffriend@gmail.com>
9 This file is part of GCC.
11 GCC is free software; you can redistribute it and/or modify it under
12 the terms of the GNU General Public License as published by the Free
13 Software Foundation; either version 3, or (at your option) any later
16 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
17 WARRANTY; without even the implied warranty of MERCHANTABILITY or
18 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21 You should have received a copy of the GNU General Public License
22 along with GCC; see the file COPYING3. If not see
23 <http://www.gnu.org/licenses/>. */
26 /* Generic tree predicates we inherit. */
28 integer_onep integer_zerop integer_all_onesp integer_minus_onep
29 integer_each_onep integer_truep integer_nonzerop
30 real_zerop real_onep real_minus_onep
32 initializer_each_zero_or_onep
34 tree_expr_nonnegative_p
42 bitmask_inv_cst_vector_p)
45 (define_operator_list tcc_comparison
46 lt le eq ne ge gt unordered ordered unlt unle ungt unge uneq ltgt)
47 (define_operator_list inverted_tcc_comparison
48 ge gt ne eq lt le ordered unordered ge gt le lt ltgt uneq)
49 (define_operator_list inverted_tcc_comparison_with_nans
50 unge ungt ne eq unlt unle ordered unordered ge gt le lt ltgt uneq)
51 (define_operator_list swapped_tcc_comparison
52 gt ge eq ne le lt unordered ordered ungt unge unlt unle uneq ltgt)
53 (define_operator_list simple_comparison lt le eq ne ge gt)
54 (define_operator_list swapped_simple_comparison gt ge eq ne le lt)
55 (define_operator_list BSWAP BUILT_IN_BSWAP16 BUILT_IN_BSWAP32
56 BUILT_IN_BSWAP64 BUILT_IN_BSWAP128)
58 #include "cfn-operators.pd"
60 /* Define operand lists for math rounding functions {,i,l,ll}FN,
61 where the versions prefixed with "i" return an int, those prefixed with
62 "l" return a long and those prefixed with "ll" return a long long.
64 Also define operand lists:
66 X<FN>F for all float functions, in the order i, l, ll
67 X<FN> for all double functions, in the same order
68 X<FN>L for all long double functions, in the same order. */
69 #define DEFINE_INT_AND_FLOAT_ROUND_FN(FN) \
70 (define_operator_list X##FN##F BUILT_IN_I##FN##F \
73 (define_operator_list X##FN BUILT_IN_I##FN \
76 (define_operator_list X##FN##L BUILT_IN_I##FN##L \
80 DEFINE_INT_AND_FLOAT_ROUND_FN (FLOOR)
81 DEFINE_INT_AND_FLOAT_ROUND_FN (CEIL)
82 DEFINE_INT_AND_FLOAT_ROUND_FN (ROUND)
83 DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
85 /* Unary operations and their associated IFN_COND_* function. */
86 (define_operator_list UNCOND_UNARY
88 (define_operator_list COND_UNARY
89 IFN_COND_NEG IFN_COND_NOT)
90 (define_operator_list COND_LEN_UNARY
91 IFN_COND_LEN_NEG IFN_COND_LEN_NOT)
93 /* Binary operations and their associated IFN_COND_* function. */
94 (define_operator_list UNCOND_BINARY
96 mult trunc_div trunc_mod rdiv
98 IFN_FMIN IFN_FMAX IFN_COPYSIGN
99 bit_and bit_ior bit_xor
101 (define_operator_list COND_BINARY
102 IFN_COND_ADD IFN_COND_SUB
103 IFN_COND_MUL IFN_COND_DIV IFN_COND_MOD IFN_COND_RDIV
104 IFN_COND_MIN IFN_COND_MAX
105 IFN_COND_FMIN IFN_COND_FMAX IFN_COND_COPYSIGN
106 IFN_COND_AND IFN_COND_IOR IFN_COND_XOR
107 IFN_COND_SHL IFN_COND_SHR)
108 (define_operator_list COND_LEN_BINARY
109 IFN_COND_LEN_ADD IFN_COND_LEN_SUB
110 IFN_COND_LEN_MUL IFN_COND_LEN_DIV
111 IFN_COND_LEN_MOD IFN_COND_LEN_RDIV
112 IFN_COND_LEN_MIN IFN_COND_LEN_MAX
113 IFN_COND_LEN_FMIN IFN_COND_LEN_FMAX IFN_COND_LEN_COPYSIGN
114 IFN_COND_LEN_AND IFN_COND_LEN_IOR IFN_COND_LEN_XOR
115 IFN_COND_LEN_SHL IFN_COND_LEN_SHR)
117 /* Same for ternary operations. */
118 (define_operator_list UNCOND_TERNARY
119 IFN_FMA IFN_FMS IFN_FNMA IFN_FNMS)
120 (define_operator_list COND_TERNARY
121 IFN_COND_FMA IFN_COND_FMS IFN_COND_FNMA IFN_COND_FNMS)
122 (define_operator_list COND_LEN_TERNARY
123 IFN_COND_LEN_FMA IFN_COND_LEN_FMS IFN_COND_LEN_FNMA IFN_COND_LEN_FNMS)
125 /* __atomic_fetch_or_*, __atomic_fetch_xor_*, __atomic_xor_fetch_* */
126 (define_operator_list ATOMIC_FETCH_OR_XOR_N
127 BUILT_IN_ATOMIC_FETCH_OR_1 BUILT_IN_ATOMIC_FETCH_OR_2
128 BUILT_IN_ATOMIC_FETCH_OR_4 BUILT_IN_ATOMIC_FETCH_OR_8
129 BUILT_IN_ATOMIC_FETCH_OR_16
130 BUILT_IN_ATOMIC_FETCH_XOR_1 BUILT_IN_ATOMIC_FETCH_XOR_2
131 BUILT_IN_ATOMIC_FETCH_XOR_4 BUILT_IN_ATOMIC_FETCH_XOR_8
132 BUILT_IN_ATOMIC_FETCH_XOR_16
133 BUILT_IN_ATOMIC_XOR_FETCH_1 BUILT_IN_ATOMIC_XOR_FETCH_2
134 BUILT_IN_ATOMIC_XOR_FETCH_4 BUILT_IN_ATOMIC_XOR_FETCH_8
135 BUILT_IN_ATOMIC_XOR_FETCH_16)
136 /* __sync_fetch_and_or_*, __sync_fetch_and_xor_*, __sync_xor_and_fetch_* */
137 (define_operator_list SYNC_FETCH_OR_XOR_N
138 BUILT_IN_SYNC_FETCH_AND_OR_1 BUILT_IN_SYNC_FETCH_AND_OR_2
139 BUILT_IN_SYNC_FETCH_AND_OR_4 BUILT_IN_SYNC_FETCH_AND_OR_8
140 BUILT_IN_SYNC_FETCH_AND_OR_16
141 BUILT_IN_SYNC_FETCH_AND_XOR_1 BUILT_IN_SYNC_FETCH_AND_XOR_2
142 BUILT_IN_SYNC_FETCH_AND_XOR_4 BUILT_IN_SYNC_FETCH_AND_XOR_8
143 BUILT_IN_SYNC_FETCH_AND_XOR_16
144 BUILT_IN_SYNC_XOR_AND_FETCH_1 BUILT_IN_SYNC_XOR_AND_FETCH_2
145 BUILT_IN_SYNC_XOR_AND_FETCH_4 BUILT_IN_SYNC_XOR_AND_FETCH_8
146 BUILT_IN_SYNC_XOR_AND_FETCH_16)
147 /* __atomic_fetch_and_*. */
148 (define_operator_list ATOMIC_FETCH_AND_N
149 BUILT_IN_ATOMIC_FETCH_AND_1 BUILT_IN_ATOMIC_FETCH_AND_2
150 BUILT_IN_ATOMIC_FETCH_AND_4 BUILT_IN_ATOMIC_FETCH_AND_8
151 BUILT_IN_ATOMIC_FETCH_AND_16)
152 /* __sync_fetch_and_and_*. */
153 (define_operator_list SYNC_FETCH_AND_AND_N
154 BUILT_IN_SYNC_FETCH_AND_AND_1 BUILT_IN_SYNC_FETCH_AND_AND_2
155 BUILT_IN_SYNC_FETCH_AND_AND_4 BUILT_IN_SYNC_FETCH_AND_AND_8
156 BUILT_IN_SYNC_FETCH_AND_AND_16)
158 /* With nop_convert? combine convert? and view_convert? in one pattern
159 plus conditionalize on tree_nop_conversion_p conversions. */
160 (match (nop_convert @0)
162 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))))
163 (match (nop_convert @0)
165 (if (VECTOR_TYPE_P (type) && VECTOR_TYPE_P (TREE_TYPE (@0))
166 && known_eq (TYPE_VECTOR_SUBPARTS (type),
167 TYPE_VECTOR_SUBPARTS (TREE_TYPE (@0)))
168 && tree_nop_conversion_p (TREE_TYPE (type), TREE_TYPE (TREE_TYPE (@0))))))
171 /* These are used by gimple_bitwise_inverted_equal_p to simplify
172 detection of BIT_NOT and comparisons. */
173 (match (bit_not_with_nop @0)
175 (match (bit_not_with_nop @0)
176 (convert (bit_not @0))
177 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))))
178 (for cmp (tcc_comparison)
179 (match (maybe_cmp @0)
181 (match (maybe_cmp @0)
182 (convert (cmp@0 @1 @2))
183 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))))
187 /* Transform likes of (char) ABS_EXPR <(int) x> into (char) ABSU_EXPR <x>
188 ABSU_EXPR returns unsigned absolute value of the operand and the operand
189 of the ABSU_EXPR will have the corresponding signed type. */
190 (simplify (abs (convert @0))
191 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
192 && !TYPE_UNSIGNED (TREE_TYPE (@0))
193 && element_precision (type) > element_precision (TREE_TYPE (@0)))
194 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
195 (convert (absu:utype @0)))))
198 /* Optimize (X + (X >> (prec - 1))) ^ (X >> (prec - 1)) into abs (X). */
200 (bit_xor:c (plus:c @0 (rshift@2 @0 INTEGER_CST@1)) @2)
201 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
202 && !TYPE_UNSIGNED (TREE_TYPE (@0))
203 && wi::to_widest (@1) == element_precision (TREE_TYPE (@0)) - 1)
207 /* Simplifications of operations with one constant operand and
208 simplifications to constants or single values. */
210 (for op (plus pointer_plus minus bit_ior bit_xor)
212 (op @0 integer_zerop)
215 /* 0 +p index -> (type)index */
217 (pointer_plus integer_zerop @1)
218 (non_lvalue (convert @1)))
220 /* ptr - 0 -> (type)ptr */
222 (pointer_diff @0 integer_zerop)
225 /* See if ARG1 is zero and X + ARG1 reduces to X.
226 Likewise if the operands are reversed. */
228 (plus:c @0 real_zerop@1)
229 (if (fold_real_zero_addition_p (type, @0, @1, 0))
232 /* See if ARG1 is zero and X - ARG1 reduces to X. */
234 (minus @0 real_zerop@1)
235 (if (fold_real_zero_addition_p (type, @0, @1, 1))
238 /* Even if the fold_real_zero_addition_p can't simplify X + 0.0
239 into X, we can optimize (X + 0.0) + 0.0 or (X + 0.0) - 0.0
240 or (X - 0.0) + 0.0 into X + 0.0 and (X - 0.0) - 0.0 into X - 0.0
241 if not -frounding-math. For sNaNs the first operation would raise
242 exceptions but turn the result into qNan, so the second operation
243 would not raise it. */
244 (for inner_op (plus minus)
245 (for outer_op (plus minus)
247 (outer_op (inner_op@3 @0 REAL_CST@1) REAL_CST@2)
250 && !HONOR_SIGN_DEPENDENT_ROUNDING (type))
251 (with { bool inner_plus = ((inner_op == PLUS_EXPR)
252 ^ REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@1)));
254 = ((outer_op == PLUS_EXPR)
255 ^ REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@2))); }
256 (if (outer_plus && !inner_plus)
261 This is unsafe for certain floats even in non-IEEE formats.
262 In IEEE, it is unsafe because it does wrong for NaNs.
263 PR middle-end/98420: x - x may be -0.0 with FE_DOWNWARD.
264 Also note that operand_equal_p is always false if an operand
268 (if (!FLOAT_TYPE_P (type)
269 || (!tree_expr_maybe_nan_p (@0)
270 && !tree_expr_maybe_infinite_p (@0)
271 && (!HONOR_SIGN_DEPENDENT_ROUNDING (type)
272 || !HONOR_SIGNED_ZEROS (type))))
273 { build_zero_cst (type); }))
275 (pointer_diff @@0 @0)
276 { build_zero_cst (type); })
279 (mult @0 integer_zerop@1)
282 /* -x == x -> x == 0 */
285 (cmp:c @0 (negate @0))
286 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
287 && !TYPE_OVERFLOW_WRAPS (TREE_TYPE(@0)))
288 (cmp @0 { build_zero_cst (TREE_TYPE(@0)); }))))
290 /* Maybe fold x * 0 to 0. The expressions aren't the same
291 when x is NaN, since x * 0 is also NaN. Nor are they the
292 same in modes with signed zeros, since multiplying a
293 negative value by 0 gives -0, not +0. Nor when x is +-Inf,
294 since x * 0 is NaN. */
296 (mult @0 real_zerop@1)
297 (if (!tree_expr_maybe_nan_p (@0)
298 && (!HONOR_NANS (type) || !tree_expr_maybe_infinite_p (@0))
299 && (!HONOR_SIGNED_ZEROS (type) || tree_expr_nonnegative_p (@0)))
302 /* In IEEE floating point, x*1 is not equivalent to x for snans.
303 Likewise for complex arithmetic with signed zeros. */
306 (if (!tree_expr_maybe_signaling_nan_p (@0)
307 && (!HONOR_SIGNED_ZEROS (type)
308 || !COMPLEX_FLOAT_TYPE_P (type)))
311 /* Transform x * -1.0 into -x. */
313 (mult @0 real_minus_onep)
314 (if (!tree_expr_maybe_signaling_nan_p (@0)
315 && (!HONOR_SIGNED_ZEROS (type)
316 || !COMPLEX_FLOAT_TYPE_P (type)))
319 /* Transform x * { 0 or 1, 0 or 1, ... } into x & { 0 or -1, 0 or -1, ...},
320 unless the target has native support for the former but not the latter. */
322 (mult @0 VECTOR_CST@1)
323 (if (initializer_each_zero_or_onep (@1)
324 && !HONOR_SNANS (type)
325 && !HONOR_SIGNED_ZEROS (type))
326 (with { tree itype = FLOAT_TYPE_P (type) ? unsigned_type_for (type) : type; }
328 && (!VECTOR_MODE_P (TYPE_MODE (type))
329 || (VECTOR_MODE_P (TYPE_MODE (itype))
330 && optab_handler (and_optab,
331 TYPE_MODE (itype)) != CODE_FOR_nothing)))
332 (view_convert (bit_and:itype (view_convert @0)
333 (ne @1 { build_zero_cst (type); })))))))
335 /* In SWAR (SIMD within a register) code a signed comparison of packed data
336 can be constructed with a particular combination of shift, bitwise and,
337 and multiplication by constants. If that code is vectorized we can
338 convert this pattern into a more efficient vector comparison. */
340 (mult (bit_and (rshift @0 uniform_integer_cst_p@1)
341 uniform_integer_cst_p@2)
342 uniform_integer_cst_p@3)
344 tree rshift_cst = uniform_integer_cst_p (@1);
345 tree bit_and_cst = uniform_integer_cst_p (@2);
346 tree mult_cst = uniform_integer_cst_p (@3);
348 /* Make sure we're working with vectors and uniform vector constants. */
349 (if (VECTOR_TYPE_P (type)
350 && tree_fits_uhwi_p (rshift_cst)
351 && tree_fits_uhwi_p (mult_cst)
352 && tree_fits_uhwi_p (bit_and_cst))
353 /* Compute what constants would be needed for this to represent a packed
354 comparison based on the shift amount denoted by RSHIFT_CST. */
356 HOST_WIDE_INT vec_elem_bits = vector_element_bits (type);
357 poly_int64 vec_nelts = TYPE_VECTOR_SUBPARTS (type);
358 poly_int64 vec_bits = vec_elem_bits * vec_nelts;
359 unsigned HOST_WIDE_INT cmp_bits_i, bit_and_i, mult_i;
360 unsigned HOST_WIDE_INT target_mult_i, target_bit_and_i;
361 cmp_bits_i = tree_to_uhwi (rshift_cst) + 1;
362 mult_i = tree_to_uhwi (mult_cst);
363 target_mult_i = (HOST_WIDE_INT_1U << cmp_bits_i) - 1;
364 bit_and_i = tree_to_uhwi (bit_and_cst);
365 target_bit_and_i = 0;
367 /* The bit pattern in BIT_AND_I should be a mask for the least
368 significant bit of each packed element that is CMP_BITS wide. */
369 for (unsigned i = 0; i < vec_elem_bits / cmp_bits_i; i++)
370 target_bit_and_i = (target_bit_and_i << cmp_bits_i) | 1U;
372 (if ((exact_log2 (cmp_bits_i)) >= 0
373 && cmp_bits_i < HOST_BITS_PER_WIDE_INT
374 && multiple_p (vec_bits, cmp_bits_i)
375 && vec_elem_bits <= HOST_BITS_PER_WIDE_INT
376 && target_mult_i == mult_i
377 && target_bit_and_i == bit_and_i)
378 /* Compute the vector shape for the comparison and check if the target is
379 able to expand the comparison with that type. */
381 /* We're doing a signed comparison. */
382 tree cmp_type = build_nonstandard_integer_type (cmp_bits_i, 0);
383 poly_int64 vector_type_nelts = exact_div (vec_bits, cmp_bits_i);
384 tree vec_cmp_type = build_vector_type (cmp_type, vector_type_nelts);
385 tree vec_truth_type = truth_type_for (vec_cmp_type);
386 tree zeros = build_zero_cst (vec_cmp_type);
387 tree ones = build_all_ones_cst (vec_cmp_type);
389 (if (expand_vec_cmp_expr_p (vec_cmp_type, vec_truth_type, LT_EXPR)
390 && expand_vec_cond_expr_p (vec_cmp_type, vec_truth_type, LT_EXPR))
391 (view_convert:type (vec_cond (lt:vec_truth_type
392 (view_convert:vec_cmp_type @0)
394 { ones; } { zeros; })))))))))
396 (for cmp (gt ge lt le)
397 outp (convert convert negate negate)
398 outn (negate negate convert convert)
399 /* Transform X * (X > 0.0 ? 1.0 : -1.0) into abs(X). */
400 /* Transform X * (X >= 0.0 ? 1.0 : -1.0) into abs(X). */
401 /* Transform X * (X < 0.0 ? 1.0 : -1.0) into -abs(X). */
402 /* Transform X * (X <= 0.0 ? 1.0 : -1.0) into -abs(X). */
404 (mult:c @0 (cond (cmp @0 real_zerop) real_onep@1 real_minus_onep))
405 (if (!tree_expr_maybe_nan_p (@0) && !HONOR_SIGNED_ZEROS (type))
407 /* Transform X * (X > 0.0 ? -1.0 : 1.0) into -abs(X). */
408 /* Transform X * (X >= 0.0 ? -1.0 : 1.0) into -abs(X). */
409 /* Transform X * (X < 0.0 ? -1.0 : 1.0) into abs(X). */
410 /* Transform X * (X <= 0.0 ? -1.0 : 1.0) into abs(X). */
412 (mult:c @0 (cond (cmp @0 real_zerop) real_minus_onep real_onep@1))
413 (if (!tree_expr_maybe_nan_p (@0) && !HONOR_SIGNED_ZEROS (type))
416 /* Transform X * copysign (1.0, X) into abs(X). */
418 (mult:c @0 (COPYSIGN_ALL real_onep @0))
419 (if (!tree_expr_maybe_nan_p (@0) && !HONOR_SIGNED_ZEROS (type))
422 /* Transform X * copysign (1.0, -X) into -abs(X). */
424 (mult:c @0 (COPYSIGN_ALL real_onep (negate @0)))
425 (if (!tree_expr_maybe_nan_p (@0) && !HONOR_SIGNED_ZEROS (type))
428 /* Transform copysign (CST, X) into copysign (ABS(CST), X). */
430 (COPYSIGN_ALL REAL_CST@0 @1)
431 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@0)))
432 (COPYSIGN_ALL (negate @0) @1)))
434 /* Transform c ? x * copysign (1, y) : z to c ? x ^ signs(y) : z.
435 tree-ssa-math-opts.cc does the corresponding optimization for
436 unconditional multiplications (via xorsign). */
438 (IFN_COND_MUL:c @0 @1 (IFN_COPYSIGN real_onep @2) @3)
439 (with { tree signs = sign_mask_for (type); }
441 (with { tree inttype = TREE_TYPE (signs); }
443 (IFN_COND_XOR:inttype @0
444 (view_convert:inttype @1)
445 (bit_and (view_convert:inttype @2) { signs; })
446 (view_convert:inttype @3)))))))
448 /* (x >= 0 ? x : 0) + (x <= 0 ? -x : 0) -> abs x. */
450 (plus:c (max @0 integer_zerop) (max (negate @0) integer_zerop))
453 /* X * 1, X / 1 -> X. */
454 (for op (mult trunc_div ceil_div floor_div round_div exact_div)
459 /* (A / (1 << B)) -> (A >> B).
460 Only for unsigned A. For signed A, this would not preserve rounding
462 For example: (-1 / ( 1 << B)) != -1 >> B.
463 Also handle widening conversions, like:
464 (A / (unsigned long long) (1U << B)) -> (A >> B)
466 (A / (unsigned long long) (1 << B)) -> (A >> B).
467 If the left shift is signed, it can be done only if the upper bits
468 of A starting from shift's type sign bit are zero, as
469 (unsigned long long) (1 << 31) is -2147483648ULL, not 2147483648ULL,
470 so it is valid only if A >> 31 is zero. */
472 (trunc_div (convert?@0 @3) (convert2? (lshift integer_onep@1 @2)))
473 (if ((TYPE_UNSIGNED (type) || tree_expr_nonnegative_p (@0))
474 && (!VECTOR_TYPE_P (type)
475 || target_supports_op_p (type, RSHIFT_EXPR, optab_vector)
476 || target_supports_op_p (type, RSHIFT_EXPR, optab_scalar))
477 && (useless_type_conversion_p (type, TREE_TYPE (@1))
478 || (element_precision (type) >= element_precision (TREE_TYPE (@1))
479 && (TYPE_UNSIGNED (TREE_TYPE (@1))
480 || (element_precision (type)
481 == element_precision (TREE_TYPE (@1)))
482 || (INTEGRAL_TYPE_P (type)
483 && (tree_nonzero_bits (@0)
484 & wi::mask (element_precision (TREE_TYPE (@1)) - 1,
486 element_precision (type))) == 0)))))
487 (if (!VECTOR_TYPE_P (type)
488 && useless_type_conversion_p (TREE_TYPE (@3), TREE_TYPE (@1))
489 && element_precision (TREE_TYPE (@3)) < element_precision (type))
490 (convert (rshift @3 @2))
493 /* Preserve explicit divisions by 0: the C++ front-end wants to detect
494 undefined behavior in constexpr evaluation, and assuming that the division
495 traps enables better optimizations than these anyway. */
496 (for div (trunc_div ceil_div floor_div round_div exact_div)
497 /* 0 / X is always zero. */
499 (div integer_zerop@0 @1)
500 /* But not for 0 / 0 so that we can get the proper warnings and errors. */
501 (if (!integer_zerop (@1))
505 (div @0 integer_minus_onep@1)
506 (if (!TYPE_UNSIGNED (type))
508 /* X / bool_range_Y is X. */
511 (if (INTEGRAL_TYPE_P (type)
512 && ssa_name_has_boolean_range (@1)
513 && !flag_non_call_exceptions)
518 /* But not for 0 / 0 so that we can get the proper warnings and errors.
519 And not for _Fract types where we can't build 1. */
520 (if (!ALL_FRACT_MODE_P (TYPE_MODE (type))
521 && !integer_zerop (@0)
522 && (!flag_non_call_exceptions || tree_expr_nonzero_p (@0)))
523 { build_one_cst (type); }))
524 /* X / abs (X) is X < 0 ? -1 : 1. */
527 (if (INTEGRAL_TYPE_P (type)
528 && TYPE_OVERFLOW_UNDEFINED (type)
529 && !integer_zerop (@0)
530 && (!flag_non_call_exceptions || tree_expr_nonzero_p (@0)))
531 (cond (lt @0 { build_zero_cst (type); })
532 { build_minus_one_cst (type); } { build_one_cst (type); })))
535 (div:C @0 (negate @0))
536 (if ((INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
537 && TYPE_OVERFLOW_UNDEFINED (type)
538 && !integer_zerop (@0)
539 && (!flag_non_call_exceptions || tree_expr_nonzero_p (@0)))
540 { build_minus_one_cst (type); })))
542 /* For unsigned integral types, FLOOR_DIV_EXPR is the same as
543 TRUNC_DIV_EXPR. Rewrite into the latter in this case. Similarly
544 for MOD instead of DIV. */
545 (for floor_divmod (floor_div floor_mod)
546 trunc_divmod (trunc_div trunc_mod)
549 (if ((INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
550 && TYPE_UNSIGNED (type))
551 (trunc_divmod @0 @1))))
553 /* 1 / X -> X == 1 for unsigned integer X.
554 1 / X -> X >= -1 && X <= 1 ? X : 0 for signed integer X.
555 But not for 1 / 0 so that we can get proper warnings and errors,
556 and not for 1-bit integers as they are edge cases better handled
559 (trunc_div integer_onep@0 @1)
560 (if (INTEGRAL_TYPE_P (type)
561 && TYPE_PRECISION (type) > 1
562 && !integer_zerop (@1)
563 && (!flag_non_call_exceptions || tree_expr_nonzero_p (@1)))
564 (if (TYPE_UNSIGNED (type))
565 (convert (eq:boolean_type_node @1 { build_one_cst (type); }))
566 (with { tree utype = unsigned_type_for (type); }
567 (cond (le (plus (convert:utype @1) { build_one_cst (utype); })
568 { build_int_cst (utype, 2); })
569 @1 { build_zero_cst (type); })))))
571 /* Combine two successive divisions. Note that combining ceil_div
572 and floor_div is trickier and combining round_div even more so. */
573 (for div (trunc_div exact_div)
575 (div (div@3 @0 INTEGER_CST@1) INTEGER_CST@2)
577 wi::overflow_type overflow;
578 wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@2),
579 TYPE_SIGN (type), &overflow);
581 (if (div == EXACT_DIV_EXPR
582 || optimize_successive_divisions_p (@2, @3))
584 (div @0 { wide_int_to_tree (type, mul); })
585 (if (TYPE_UNSIGNED (type)
586 || mul != wi::min_value (TYPE_PRECISION (type), SIGNED))
587 { build_zero_cst (type); }))))))
589 /* Combine successive multiplications. Similar to above, but handling
590 overflow is different. */
592 (mult (mult @0 INTEGER_CST@1) INTEGER_CST@2)
594 wi::overflow_type overflow;
595 wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@2),
596 TYPE_SIGN (type), &overflow);
598 /* Skip folding on overflow: the only special case is @1 * @2 == -INT_MIN,
599 otherwise undefined overflow implies that @0 must be zero. */
600 (if (!overflow || TYPE_OVERFLOW_WRAPS (type))
601 (mult @0 { wide_int_to_tree (type, mul); }))))
603 /* Similar to above, but there could be an extra add/sub between
604 successive multuiplications. */
606 (mult (plus:s (mult:s@4 @0 INTEGER_CST@1) INTEGER_CST@2) INTEGER_CST@3)
608 bool overflowed = true;
609 wi::overflow_type ovf1, ovf2;
610 wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@3),
611 TYPE_SIGN (type), &ovf1);
612 wide_int add = wi::mul (wi::to_wide (@2), wi::to_wide (@3),
613 TYPE_SIGN (type), &ovf2);
614 if (TYPE_OVERFLOW_UNDEFINED (type))
618 if (ovf1 == wi::OVF_NONE && ovf2 == wi::OVF_NONE
619 && get_global_range_query ()->range_of_expr (vr0, @4)
620 && !vr0.varying_p () && !vr0.undefined_p ())
622 wide_int wmin0 = vr0.lower_bound ();
623 wide_int wmax0 = vr0.upper_bound ();
624 wmin0 = wi::mul (wmin0, wi::to_wide (@3), TYPE_SIGN (type), &ovf1);
625 wmax0 = wi::mul (wmax0, wi::to_wide (@3), TYPE_SIGN (type), &ovf2);
626 if (ovf1 == wi::OVF_NONE && ovf2 == wi::OVF_NONE)
628 wi::add (wmin0, add, TYPE_SIGN (type), &ovf1);
629 wi::add (wmax0, add, TYPE_SIGN (type), &ovf2);
630 if (ovf1 == wi::OVF_NONE && ovf2 == wi::OVF_NONE)
639 /* Skip folding on overflow. */
641 (plus (mult @0 { wide_int_to_tree (type, mul); })
642 { wide_int_to_tree (type, add); }))))
644 /* Similar to above, but a multiplication between successive additions. */
646 (plus (mult:s (plus:s @0 INTEGER_CST@1) INTEGER_CST@2) INTEGER_CST@3)
648 bool overflowed = true;
649 wi::overflow_type ovf1;
650 wi::overflow_type ovf2;
651 wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@2),
652 TYPE_SIGN (type), &ovf1);
653 wide_int add = wi::add (mul, wi::to_wide (@3),
654 TYPE_SIGN (type), &ovf2);
655 if (TYPE_OVERFLOW_UNDEFINED (type))
659 if (ovf1 == wi::OVF_NONE && ovf2 == wi::OVF_NONE
660 && get_global_range_query ()->range_of_expr (vr0, @0)
661 && !vr0.varying_p () && !vr0.undefined_p ())
663 wide_int wmin0 = vr0.lower_bound ();
664 wide_int wmax0 = vr0.upper_bound ();
665 wmin0 = wi::mul (wmin0, wi::to_wide (@2), TYPE_SIGN (type), &ovf1);
666 wmax0 = wi::mul (wmax0, wi::to_wide (@2), TYPE_SIGN (type), &ovf2);
667 if (ovf1 == wi::OVF_NONE && ovf2 == wi::OVF_NONE)
669 wi::add (wmin0, mul, TYPE_SIGN (type), &ovf1);
670 wi::add (wmax0, mul, TYPE_SIGN (type), &ovf2);
671 if (ovf1 == wi::OVF_NONE && ovf2 == wi::OVF_NONE)
680 /* Skip folding on overflow. */
682 (plus (mult @0 @2) { wide_int_to_tree (type, add); }))))
684 /* Optimize A / A to 1.0 if we don't care about
685 NaNs or Infinities. */
688 (if (FLOAT_TYPE_P (type)
689 && ! HONOR_NANS (type)
690 && ! HONOR_INFINITIES (type))
691 { build_one_cst (type); }))
693 /* Optimize -A / A to -1.0 if we don't care about
694 NaNs or Infinities. */
696 (rdiv:C @0 (negate @0))
697 (if (FLOAT_TYPE_P (type)
698 && ! HONOR_NANS (type)
699 && ! HONOR_INFINITIES (type))
700 { build_minus_one_cst (type); }))
702 /* PR71078: x / abs(x) -> copysign (1.0, x) */
704 (rdiv:C (convert? @0) (convert? (abs @0)))
705 (if (SCALAR_FLOAT_TYPE_P (type)
706 && ! HONOR_NANS (type)
707 && ! HONOR_INFINITIES (type))
709 (if (types_match (type, float_type_node))
710 (BUILT_IN_COPYSIGNF { build_one_cst (type); } (convert @0)))
711 (if (types_match (type, double_type_node))
712 (BUILT_IN_COPYSIGN { build_one_cst (type); } (convert @0)))
713 (if (types_match (type, long_double_type_node))
714 (BUILT_IN_COPYSIGNL { build_one_cst (type); } (convert @0))))))
716 /* In IEEE floating point, x/1 is not equivalent to x for snans. */
719 (if (!tree_expr_maybe_signaling_nan_p (@0))
722 /* In IEEE floating point, x/-1 is not equivalent to -x for snans. */
724 (rdiv @0 real_minus_onep)
725 (if (!tree_expr_maybe_signaling_nan_p (@0))
728 (if (flag_reciprocal_math)
729 /* Convert (A/B)/C to A/(B*C). */
731 (rdiv (rdiv:s @0 @1) @2)
732 (rdiv @0 (mult @1 @2)))
734 /* Canonicalize x / (C1 * y) to (x * C2) / y. */
736 (rdiv @0 (mult:s @1 REAL_CST@2))
738 { tree tem = const_binop (RDIV_EXPR, type, build_one_cst (type), @2); }
740 (rdiv (mult @0 { tem; } ) @1))))
742 /* Convert A/(B/C) to (A/B)*C */
744 (rdiv @0 (rdiv:s @1 @2))
745 (mult (rdiv @0 @1) @2)))
747 /* Simplify x / (- y) to -x / y. */
749 (rdiv @0 (negate @1))
750 (rdiv (negate @0) @1))
752 (if (flag_unsafe_math_optimizations)
753 /* Simplify (C / x op 0.0) to x op 0.0 for C != 0, C != Inf/Nan.
754 Since C / x may underflow to zero, do this only for unsafe math. */
755 (for op (lt le gt ge)
758 (op (rdiv REAL_CST@0 @1) real_zerop@2)
759 (if (!HONOR_SIGNED_ZEROS (@1) && !HONOR_INFINITIES (@1))
761 (if (real_less (&dconst0, TREE_REAL_CST_PTR (@0)))
763 /* For C < 0, use the inverted operator. */
764 (if (real_less (TREE_REAL_CST_PTR (@0), &dconst0))
767 /* Optimize (X & (-A)) / A where A is a power of 2, to X >> log2(A) */
768 (for div (trunc_div ceil_div floor_div round_div exact_div)
770 (div (convert? (bit_and @0 INTEGER_CST@1)) INTEGER_CST@2)
771 (if (integer_pow2p (@2)
772 && tree_int_cst_sgn (@2) > 0
773 && tree_nop_conversion_p (type, TREE_TYPE (@0))
774 && wi::to_wide (@2) + wi::to_wide (@1) == 0)
776 { build_int_cst (integer_type_node,
777 wi::exact_log2 (wi::to_wide (@2))); }))))
779 /* If ARG1 is a constant, we can convert this to a multiply by the
780 reciprocal. This does not have the same rounding properties,
781 so only do this if -freciprocal-math. We can actually
782 always safely do it if ARG1 is a power of two, but it's hard to
783 tell if it is or not in a portable manner. */
784 (for cst (REAL_CST COMPLEX_CST VECTOR_CST)
788 (if (flag_reciprocal_math
791 { tree tem = const_binop (RDIV_EXPR, type, build_one_cst (type), @1); }
793 (mult @0 { tem; } )))
794 (if (cst != COMPLEX_CST)
795 (with { tree inverse = exact_inverse (type, @1); }
797 (mult @0 { inverse; } ))))))))
799 (for mod (ceil_mod floor_mod round_mod trunc_mod)
800 /* 0 % X is always zero. */
802 (mod integer_zerop@0 @1)
803 /* But not for 0 % 0 so that we can get the proper warnings and errors. */
804 (if (!integer_zerop (@1))
806 /* X % 1 is always zero. */
808 (mod @0 integer_onep)
809 { build_zero_cst (type); })
810 /* X % -1 is zero. */
812 (mod @0 integer_minus_onep@1)
813 (if (!TYPE_UNSIGNED (type))
814 { build_zero_cst (type); }))
818 /* But not for 0 % 0 so that we can get the proper warnings and errors. */
819 (if (!integer_zerop (@0))
820 { build_zero_cst (type); }))
821 /* (X % Y) % Y is just X % Y. */
823 (mod (mod@2 @0 @1) @1)
825 /* From extract_muldiv_1: (X * C1) % C2 is zero if C1 is a multiple of C2. */
827 (mod (mult @0 INTEGER_CST@1) INTEGER_CST@2)
828 (if (ANY_INTEGRAL_TYPE_P (type)
829 && TYPE_OVERFLOW_UNDEFINED (type)
830 && wi::multiple_of_p (wi::to_wide (@1), wi::to_wide (@2),
832 { build_zero_cst (type); }))
833 /* For (X % C) == 0, if X is signed and C is power of 2, use unsigned
834 modulo and comparison, since it is simpler and equivalent. */
837 (cmp (mod @0 integer_pow2p@2) integer_zerop@1)
838 (if (!TYPE_UNSIGNED (TREE_TYPE (@0)))
839 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
840 (cmp (mod (convert:utype @0) (convert:utype @2)) (convert:utype @1)))))))
842 /* X % -C is the same as X % C. */
844 (trunc_mod @0 INTEGER_CST@1)
845 (if (TYPE_SIGN (type) == SIGNED
846 && !TREE_OVERFLOW (@1)
847 && wi::neg_p (wi::to_wide (@1))
848 && !TYPE_OVERFLOW_TRAPS (type)
849 /* Avoid this transformation if C is INT_MIN, i.e. C == -C. */
850 && !sign_bit_p (@1, @1))
851 (trunc_mod @0 (negate @1))))
853 /* X % -Y is the same as X % Y. */
855 (trunc_mod @0 (convert? (negate @1)))
856 (if (INTEGRAL_TYPE_P (type)
857 && !TYPE_UNSIGNED (type)
858 && !TYPE_OVERFLOW_TRAPS (type)
859 && tree_nop_conversion_p (type, TREE_TYPE (@1))
860 /* Avoid this transformation if X might be INT_MIN or
861 Y might be -1, because we would then change valid
862 INT_MIN % -(-1) into invalid INT_MIN % -1. */
863 && (expr_not_equal_to (@0, wi::to_wide (TYPE_MIN_VALUE (type)))
864 || expr_not_equal_to (@1, wi::minus_one (TYPE_PRECISION
866 (trunc_mod @0 (convert @1))))
868 /* X - (X / Y) * Y is the same as X % Y. */
870 (minus (convert1? @0) (convert2? (mult:c (trunc_div @@0 @@1) @1)))
871 (if (INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
872 (convert (trunc_mod @0 @1))))
874 /* x * (1 + y / x) - y -> x - y % x */
876 (minus (mult:cs @0 (plus:s (trunc_div:s @1 @0) integer_onep)) @1)
877 (if (INTEGRAL_TYPE_P (type))
878 (minus @0 (trunc_mod @1 @0))))
880 /* Optimize TRUNC_MOD_EXPR by a power of two into a BIT_AND_EXPR,
881 i.e. "X % C" into "X & (C - 1)", if X and C are positive.
882 Also optimize A % (C << N) where C is a power of 2,
883 to A & ((C << N) - 1).
884 Also optimize "A shift (B % C)", if C is a power of 2, to
885 "A shift (B & (C - 1))". SHIFT operation include "<<" and ">>"
886 and assume (B % C) is nonnegative as shifts negative values would
888 (match (power_of_two_cand @1)
890 (match (power_of_two_cand @1)
891 (lshift INTEGER_CST@1 @2))
892 (for mod (trunc_mod floor_mod)
893 (for shift (lshift rshift)
895 (shift @0 (mod @1 (power_of_two_cand@2 @3)))
896 (if (integer_pow2p (@3) && tree_int_cst_sgn (@3) > 0)
897 (shift @0 (bit_and @1 (minus @2 { build_int_cst (TREE_TYPE (@2),
900 (mod @0 (convert? (power_of_two_cand@1 @2)))
901 (if ((TYPE_UNSIGNED (type) || tree_expr_nonnegative_p (@0))
902 /* Allow any integral conversions of the divisor, except
903 conversion from narrower signed to wider unsigned type
904 where if @1 would be negative power of two, the divisor
905 would not be a power of two. */
906 && INTEGRAL_TYPE_P (type)
907 && INTEGRAL_TYPE_P (TREE_TYPE (@1))
908 && (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@1))
909 || TYPE_UNSIGNED (TREE_TYPE (@1))
910 || !TYPE_UNSIGNED (type))
911 && integer_pow2p (@2) && tree_int_cst_sgn (@2) > 0)
912 (with { tree utype = TREE_TYPE (@1);
913 if (!TYPE_OVERFLOW_WRAPS (utype))
914 utype = unsigned_type_for (utype); }
915 (bit_and @0 (convert (minus (convert:utype @1)
916 { build_one_cst (utype); })))))))
918 /* Simplify (unsigned t * 2)/2 -> unsigned t & 0x7FFFFFFF. */
920 (trunc_div (mult @0 integer_pow2p@1) @1)
921 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) && TYPE_UNSIGNED (TREE_TYPE (@0)))
922 (bit_and @0 { wide_int_to_tree
923 (type, wi::mask (TYPE_PRECISION (type)
924 - wi::exact_log2 (wi::to_wide (@1)),
925 false, TYPE_PRECISION (type))); })))
927 /* Simplify (unsigned t / 2) * 2 -> unsigned t & ~1. */
929 (mult (trunc_div @0 integer_pow2p@1) @1)
930 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) && TYPE_UNSIGNED (TREE_TYPE (@0)))
931 (bit_and @0 (negate @1))))
933 /* Simplify (t * 2) / 2) -> t. */
934 (for div (trunc_div ceil_div floor_div round_div exact_div)
936 (div (mult:c @0 @1) @1)
937 (if (ANY_INTEGRAL_TYPE_P (type))
938 (if (TYPE_OVERFLOW_UNDEFINED (type))
941 (with {value_range vr0, vr1;}
942 (if (INTEGRAL_TYPE_P (type)
943 && get_range_query (cfun)->range_of_expr (vr0, @0)
944 && get_range_query (cfun)->range_of_expr (vr1, @1)
945 && range_op_handler (MULT_EXPR).overflow_free_p (vr0, vr1))
951 (for div (trunc_div exact_div)
952 /* Simplify (X + M*N) / N -> X / N + M. */
954 (div (plus:c@4 @0 (mult:c@3 @1 @2)) @2)
955 (with {value_range vr0, vr1, vr2, vr3, vr4;}
956 (if (INTEGRAL_TYPE_P (type)
957 && get_range_query (cfun)->range_of_expr (vr1, @1)
958 && get_range_query (cfun)->range_of_expr (vr2, @2)
959 /* "N*M" doesn't overflow. */
960 && range_op_handler (MULT_EXPR).overflow_free_p (vr1, vr2)
961 && get_range_query (cfun)->range_of_expr (vr0, @0)
962 && get_range_query (cfun)->range_of_expr (vr3, @3)
963 /* "X+(N*M)" doesn't overflow. */
964 && range_op_handler (PLUS_EXPR).overflow_free_p (vr0, vr3)
965 && get_range_query (cfun)->range_of_expr (vr4, @4)
966 && !vr4.undefined_p ()
967 /* "X+N*M" is not with opposite sign as "X". */
968 && (TYPE_UNSIGNED (type)
969 || (vr0.nonnegative_p () && vr4.nonnegative_p ())
970 || (vr0.nonpositive_p () && vr4.nonpositive_p ())))
971 (plus (div @0 @2) @1))))
973 /* Simplify (X - M*N) / N -> X / N - M. */
975 (div (minus@4 @0 (mult:c@3 @1 @2)) @2)
976 (with {value_range vr0, vr1, vr2, vr3, vr4;}
977 (if (INTEGRAL_TYPE_P (type)
978 && get_range_query (cfun)->range_of_expr (vr1, @1)
979 && get_range_query (cfun)->range_of_expr (vr2, @2)
980 /* "N * M" doesn't overflow. */
981 && range_op_handler (MULT_EXPR).overflow_free_p (vr1, vr2)
982 && get_range_query (cfun)->range_of_expr (vr0, @0)
983 && get_range_query (cfun)->range_of_expr (vr3, @3)
984 /* "X - (N*M)" doesn't overflow. */
985 && range_op_handler (MINUS_EXPR).overflow_free_p (vr0, vr3)
986 && get_range_query (cfun)->range_of_expr (vr4, @4)
987 && !vr4.undefined_p ()
988 /* "X-N*M" is not with opposite sign as "X". */
989 && (TYPE_UNSIGNED (type)
990 || (vr0.nonnegative_p () && vr4.nonnegative_p ())
991 || (vr0.nonpositive_p () && vr4.nonpositive_p ())))
992 (minus (div @0 @2) @1)))))
995 (X + C) / N -> X / N + C / N where C is multiple of N.
996 (X + C) >> N -> X >> N + C>>N if low N bits of C is 0. */
997 (for op (trunc_div exact_div rshift)
999 (op (plus@3 @0 INTEGER_CST@1) INTEGER_CST@2)
1002 wide_int c = wi::to_wide (@1);
1003 wide_int n = wi::to_wide (@2);
1004 bool shift = op == RSHIFT_EXPR;
1005 #define plus_op1(v) (shift ? wi::rshift (v, n, TYPE_SIGN (type)) \
1006 : wi::div_trunc (v, n, TYPE_SIGN (type)))
1007 #define exact_mod(v) (shift ? wi::ctz (v) >= n.to_shwi () \
1008 : wi::multiple_of_p (v, n, TYPE_SIGN (type)))
1009 value_range vr0, vr1, vr3;
1011 (if (INTEGRAL_TYPE_P (type)
1012 && get_range_query (cfun)->range_of_expr (vr0, @0))
1014 && get_range_query (cfun)->range_of_expr (vr1, @1)
1015 /* "X+C" doesn't overflow. */
1016 && range_op_handler (PLUS_EXPR).overflow_free_p (vr0, vr1)
1017 && get_range_query (cfun)->range_of_expr (vr3, @3)
1018 && !vr3.undefined_p ()
1019 /* "X+C" and "X" are not of opposite sign. */
1020 && (TYPE_UNSIGNED (type)
1021 || (vr0.nonnegative_p () && vr3.nonnegative_p ())
1022 || (vr0.nonpositive_p () && vr3.nonpositive_p ())))
1023 (plus (op @0 @2) { wide_int_to_tree (type, plus_op1 (c)); })
1024 (if (!vr0.undefined_p () && TYPE_UNSIGNED (type) && c.sign_mask () < 0
1026 /* unsigned "X-(-C)" doesn't underflow. */
1027 && wi::geu_p (vr0.lower_bound (), -c))
1028 (plus (op @0 @2) { wide_int_to_tree (type, -plus_op1 (-c)); })))))))
1033 /* (nop_outer_cast)-(inner_cast)var -> -(outer_cast)(var)
1034 if var is smaller in precision.
1035 This is always safe for both doing the negative in signed or unsigned
1036 as the value for undefined will not show up. */
1038 (convert (negate:s@1 (convert:s @0)))
1039 (if (INTEGRAL_TYPE_P (type)
1040 && tree_nop_conversion_p (type, TREE_TYPE (@1))
1041 && TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (@0)))
1042 (negate (convert @0))))
1044 (for op (negate abs)
1045 /* Simplify cos(-x) and cos(|x|) -> cos(x). Similarly for cosh. */
1046 (for coss (COS COSH)
1050 /* Simplify pow(-x, y) and pow(|x|,y) -> pow(x,y) if y is an even integer. */
1053 (pows (op @0) REAL_CST@1)
1054 (with { HOST_WIDE_INT n; }
1055 (if (real_isinteger (&TREE_REAL_CST (@1), &n) && (n & 1) == 0)
1057 /* Likewise for powi. */
1060 (pows (op @0) INTEGER_CST@1)
1061 (if ((wi::to_wide (@1) & 1) == 0)
1063 /* Strip negate and abs from both operands of hypot. */
1071 /* copysign(-x, y) and copysign(abs(x), y) -> copysign(x, y). */
1072 (for copysigns (COPYSIGN_ALL)
1074 (copysigns (op @0) @1)
1075 (copysigns @0 @1))))
1077 /* abs(x)*abs(x) -> x*x. Should be valid for all types. */
1079 (mult (abs@1 @0) @1)
1082 /* Convert absu(x)*absu(x) -> x*x. */
1084 (mult (absu@1 @0) @1)
1085 (mult (convert@2 @0) @2))
1087 /* cos(copysign(x, y)) -> cos(x). Similarly for cosh. */
1088 (for coss (COS COSH)
1089 (for copysigns (COPYSIGN)
1091 (coss (copysigns @0 @1))
1094 /* pow(copysign(x, y), z) -> pow(x, z) if z is an even integer. */
1096 (for copysigns (COPYSIGN)
1098 (pows (copysigns @0 @2) REAL_CST@1)
1099 (with { HOST_WIDE_INT n; }
1100 (if (real_isinteger (&TREE_REAL_CST (@1), &n) && (n & 1) == 0)
1102 /* Likewise for powi. */
1104 (for copysigns (COPYSIGN)
1106 (pows (copysigns @0 @2) INTEGER_CST@1)
1107 (if ((wi::to_wide (@1) & 1) == 0)
1111 (for copysigns (COPYSIGN)
1112 /* hypot(copysign(x, y), z) -> hypot(x, z). */
1114 (hypots (copysigns @0 @1) @2)
1116 /* hypot(x, copysign(y, z)) -> hypot(x, y). */
1118 (hypots @0 (copysigns @1 @2))
1121 /* copysign(x, CST) -> abs (x). */
1122 (for copysigns (COPYSIGN_ALL)
1124 (copysigns @0 REAL_CST@1)
1125 (if (!REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
1128 /* Transform fneg (fabs (X)) -> copysign (X, -1). */
1131 (IFN_COPYSIGN @0 { build_minus_one_cst (type); }))
1133 /* copysign(copysign(x, y), z) -> copysign(x, z). */
1134 (for copysigns (COPYSIGN_ALL)
1136 (copysigns (copysigns @0 @1) @2)
1139 /* copysign(x,y)*copysign(x,y) -> x*x. */
1140 (for copysigns (COPYSIGN_ALL)
1142 (mult (copysigns@2 @0 @1) @2)
1145 /* ccos(-x) -> ccos(x). Similarly for ccosh. */
1146 (for ccoss (CCOS CCOSH)
1151 /* cabs(-x) and cos(conj(x)) -> cabs(x). */
1152 (for ops (conj negate)
1158 /* Fold (a * (1 << b)) into (a << b) */
1160 (mult:c @0 (convert? (lshift integer_onep@1 @2)))
1161 (if (! FLOAT_TYPE_P (type)
1162 && tree_nop_conversion_p (type, TREE_TYPE (@1)))
1165 /* Shifts by precision or greater result in zero. */
1166 (for shift (lshift rshift)
1168 (shift @0 uniform_integer_cst_p@1)
1169 (if ((GIMPLE || !sanitize_flags_p (SANITIZE_SHIFT_EXPONENT))
1170 /* Leave arithmetic right shifts of possibly negative values alone. */
1171 && (TYPE_UNSIGNED (type)
1172 || shift == LSHIFT_EXPR
1173 || tree_expr_nonnegative_p (@0))
1174 /* Use a signed compare to leave negative shift counts alone. */
1175 && wi::ges_p (wi::to_wide (uniform_integer_cst_p (@1)),
1176 element_precision (type)))
1177 { build_zero_cst (type); })))
1179 /* Shifts by constants distribute over several binary operations,
1180 hence (X << C) + (Y << C) can be simplified to (X + Y) << C. */
1181 (for op (plus minus)
1183 (op (lshift:s @0 @1) (lshift:s @2 @1))
1184 (if (INTEGRAL_TYPE_P (type)
1185 && TYPE_OVERFLOW_WRAPS (type)
1186 && !TYPE_SATURATING (type))
1187 (lshift (op @0 @2) @1))))
1189 (for op (bit_and bit_ior bit_xor)
1191 (op (lshift:s @0 @1) (lshift:s @2 @1))
1192 (if (INTEGRAL_TYPE_P (type))
1193 (lshift (op @0 @2) @1)))
1195 (op (rshift:s @0 @1) (rshift:s @2 @1))
1196 (if (INTEGRAL_TYPE_P (type))
1197 (rshift (op @0 @2) @1))))
1199 /* Fold (1 << (C - x)) where C = precision(type) - 1
1200 into ((1 << C) >> x). */
1202 (lshift integer_onep@0 (minus@1 INTEGER_CST@2 @3))
1203 (if (INTEGRAL_TYPE_P (type)
1204 && wi::eq_p (wi::to_wide (@2), TYPE_PRECISION (type) - 1)
1206 (if (TYPE_UNSIGNED (type))
1207 (rshift (lshift @0 @2) @3)
1209 { tree utype = unsigned_type_for (type); }
1210 (convert (rshift (lshift (convert:utype @0) @2) @3))))))
1212 /* Fold ((type)(a<0)) << SIGNBITOFA into ((type)a) & signbit. */
1214 (lshift (convert (lt @0 integer_zerop@1)) INTEGER_CST@2)
1215 (if (TYPE_SIGN (TREE_TYPE (@0)) == SIGNED
1216 && wi::eq_p (wi::to_wide (@2), TYPE_PRECISION (TREE_TYPE (@0)) - 1))
1217 (with { wide_int wone = wi::one (TYPE_PRECISION (type)); }
1218 (bit_and (convert @0)
1219 { wide_int_to_tree (type,
1220 wi::lshift (wone, wi::to_wide (@2))); }))))
1222 /* Fold (-x >> C) into -(x > 0) where C = precision(type) - 1. */
1223 (for cst (INTEGER_CST VECTOR_CST)
1225 (rshift (negate:s @0) cst@1)
1226 (if (!TYPE_UNSIGNED (type)
1227 && TYPE_OVERFLOW_UNDEFINED (type))
1228 (with { tree stype = TREE_TYPE (@1);
1229 tree bt = truth_type_for (type);
1230 tree zeros = build_zero_cst (type);
1231 tree cst = NULL_TREE; }
1233 /* Handle scalar case. */
1234 (if (INTEGRAL_TYPE_P (type)
1235 /* If we apply the rule to the scalar type before vectorization
1236 we will enforce the result of the comparison being a bool
1237 which will require an extra AND on the result that will be
1238 indistinguishable from when the user did actually want 0
1239 or 1 as the result so it can't be removed. */
1240 && canonicalize_math_after_vectorization_p ()
1241 && wi::eq_p (wi::to_wide (@1), TYPE_PRECISION (type) - 1))
1242 (negate (convert (gt @0 { zeros; }))))
1243 /* Handle vector case. */
1244 (if (VECTOR_INTEGER_TYPE_P (type)
1245 /* First check whether the target has the same mode for vector
1246 comparison results as it's operands do. */
1247 && TYPE_MODE (bt) == TYPE_MODE (type)
1248 /* Then check to see if the target is able to expand the comparison
1249 with the given type later on, otherwise we may ICE. */
1250 && expand_vec_cmp_expr_p (type, bt, GT_EXPR)
1251 && (cst = uniform_integer_cst_p (@1)) != NULL
1252 && wi::eq_p (wi::to_wide (cst), element_precision (type) - 1))
1253 (view_convert (gt:bt @0 { zeros; }))))))))
1255 /* Fold (C1/X)*C2 into (C1*C2)/X. */
1257 (mult (rdiv@3 REAL_CST@0 @1) REAL_CST@2)
1258 (if (flag_associative_math
1261 { tree tem = const_binop (MULT_EXPR, type, @0, @2); }
1263 (rdiv { tem; } @1)))))
1265 /* Simplify ~X & X as zero. */
1267 (bit_and (convert? @0) (convert? @1))
1268 (with { bool wascmp; }
1269 (if (types_match (TREE_TYPE (@0), TREE_TYPE (@1))
1270 && bitwise_inverted_equal_p (@0, @1, wascmp))
1271 { wascmp ? constant_boolean_node (false, type) : build_zero_cst (type); })))
1273 /* PR71636: Transform x & ((1U << b) - 1) -> x & ~(~0U << b); */
1275 (bit_and:c @0 (plus:s (lshift:s integer_onep @1) integer_minus_onep))
1276 (if (TYPE_UNSIGNED (type))
1277 (bit_and @0 (bit_not (lshift { build_all_ones_cst (type); } @1)))))
1279 (for bitop (bit_and bit_ior)
1281 /* PR35691: Transform
1282 (x == 0 & y == 0) -> (x | typeof(x)(y)) == 0.
1283 (x != 0 | y != 0) -> (x | typeof(x)(y)) != 0. */
1285 (bitop (cmp @0 integer_zerop@2) (cmp @1 integer_zerop))
1286 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1287 && INTEGRAL_TYPE_P (TREE_TYPE (@1))
1288 && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
1289 (cmp (bit_ior @0 (convert @1)) @2)))
1291 (x == -1 & y == -1) -> (x & typeof(x)(y)) == -1.
1292 (x != -1 | y != -1) -> (x & typeof(x)(y)) != -1. */
1294 (bitop (cmp @0 integer_all_onesp@2) (cmp @1 integer_all_onesp))
1295 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1296 && INTEGRAL_TYPE_P (TREE_TYPE (@1))
1297 && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
1298 (cmp (bit_and @0 (convert @1)) @2))))
1300 /* Fold (A & ~B) - (A & B) into (A ^ B) - B. */
1302 (minus (bit_and:cs @0 (bit_not @1)) (bit_and:cs @0 @1))
1303 (minus (bit_xor @0 @1) @1))
1305 (minus (bit_and:s @0 INTEGER_CST@2) (bit_and:s @0 INTEGER_CST@1))
1306 (if (~wi::to_wide (@2) == wi::to_wide (@1))
1307 (minus (bit_xor @0 @1) @1)))
1309 /* Fold (A & B) - (A & ~B) into B - (A ^ B). */
1311 (minus (bit_and:cs @0 @1) (bit_and:cs @0 (bit_not @1)))
1312 (minus @1 (bit_xor @0 @1)))
1314 /* Simplify (X & ~Y) |^+ (~X & Y) -> X ^ Y. */
1315 (for op (bit_ior bit_xor plus)
1317 (op (bit_and:c @0 @2) (bit_and:c @3 @1))
1318 (with { bool wascmp0, wascmp1; }
1319 (if (bitwise_inverted_equal_p (@2, @1, wascmp0)
1320 && bitwise_inverted_equal_p (@0, @3, wascmp1)
1321 && ((!wascmp0 && !wascmp1)
1322 || element_precision (type) == 1))
1325 /* PR53979: Transform ((a ^ b) | a) -> (a | b) */
1327 (bit_ior:c (bit_xor:c @0 @1) @0)
1330 /* (a & ~b) | (a ^ b) --> a ^ b */
1332 (bit_ior:c (bit_and:c @0 (bit_not @1)) (bit_xor:c@2 @0 @1))
1335 /* (a & ~b) ^ ~a --> ~(a & b) */
1337 (bit_xor:c (bit_and:cs @0 (bit_not @1)) (bit_not @0))
1338 (bit_not (bit_and @0 @1)))
1340 /* (~a & b) ^ a --> (a | b) */
1342 (bit_xor:c (bit_and:cs (bit_not @0) @1) @0)
1345 /* (a | b) & ~(a ^ b) --> a & b */
1347 (bit_and:c (bit_ior @0 @1) (bit_not (bit_xor:c @0 @1)))
1350 /* (a | b) & (a == b) --> a & b (boolean version of the above). */
1352 (bit_and:c (bit_ior @0 @1) (nop_convert? (eq:c @0 @1)))
1353 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1354 && TYPE_PRECISION (TREE_TYPE (@0)) == 1)
1357 /* a | ~(a ^ b) --> a | ~b */
1359 (bit_ior:c @0 (bit_not:s (bit_xor:c @0 @1)))
1360 (bit_ior @0 (bit_not @1)))
1362 /* a | (a == b) --> a | (b^1) (boolean version of the above). */
1364 (bit_ior:c @0 (nop_convert? (eq:c @0 @1)))
1365 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1366 && TYPE_PRECISION (TREE_TYPE (@0)) == 1)
1367 (bit_ior @0 (bit_xor @1 { build_one_cst (type); }))))
1369 /* a | ((~a) ^ b) --> a | (~b) (alt version of the above 2) */
1371 (bit_ior:c @0 (bit_xor:cs @1 @2))
1372 (with { bool wascmp; }
1373 (if (bitwise_inverted_equal_p (@0, @1, wascmp)
1374 && (!wascmp || element_precision (type) == 1))
1375 (bit_ior @0 (bit_not @2)))))
1377 /* a & ~(a ^ b) --> a & b */
1379 (bit_and:c @0 (bit_not (bit_xor:c @0 @1)))
1382 /* a & (a == b) --> a & b (boolean version of the above). */
1384 (bit_and:c @0 (nop_convert? (eq:c @0 @1)))
1385 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1386 && TYPE_PRECISION (TREE_TYPE (@0)) == 1)
1389 /* a & ((~a) ^ b) --> a & b (alt version of the above 2) */
1391 (bit_and:c @0 (bit_xor:c @1 @2))
1392 (with { bool wascmp; }
1393 (if (bitwise_inverted_equal_p (@0, @1, wascmp)
1394 && (!wascmp || element_precision (type) == 1))
1397 /* (a | b) | (a &^ b) --> a | b */
1398 (for op (bit_and bit_xor)
1400 (bit_ior:c (bit_ior@2 @0 @1) (op:c @0 @1))
1403 /* (a & b) | ~(a ^ b) --> ~(a ^ b) */
1405 (bit_ior:c (bit_and:c @0 @1) (bit_not@2 (bit_xor @0 @1)))
1408 /* (a & b) | (a == b) --> a == b */
1410 (bit_ior:c (bit_and:c @0 @1) (nop_convert?@2 (eq @0 @1)))
1411 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1412 && TYPE_PRECISION (TREE_TYPE (@0)) == 1)
1415 /* ~(~a & b) --> a | ~b */
1417 (bit_not (bit_and:cs (bit_not @0) @1))
1418 (bit_ior @0 (bit_not @1)))
1420 /* ~(~a | b) --> a & ~b */
1422 (bit_not (bit_ior:cs (bit_not @0) @1))
1423 (bit_and @0 (bit_not @1)))
1425 /* (a ^ b) & ((b ^ c) ^ a) --> (a ^ b) & ~c */
1427 (bit_and:c (bit_xor:c@3 @0 @1) (bit_xor:cs (bit_xor:cs @1 @2) @0))
1428 (bit_and @3 (bit_not @2)))
1430 /* (a ^ b) | ((b ^ c) ^ a) --> (a ^ b) | c */
1432 (bit_ior:c (bit_xor:c@3 @0 @1) (bit_xor:c (bit_xor:c @1 @2) @0))
1435 /* (~X | C) ^ D -> (X | C) ^ (~D ^ C) if (~D ^ C) can be simplified. */
1437 (bit_xor:c (bit_ior:cs (bit_not:s @0) @1) @2)
1438 (bit_xor (bit_ior @0 @1) (bit_xor! (bit_not! @2) @1)))
1440 /* (~X & C) ^ D -> (X & C) ^ (D ^ C) if (D ^ C) can be simplified. */
1442 (bit_xor:c (bit_and:cs (bit_not:s @0) @1) @2)
1443 (bit_xor (bit_and @0 @1) (bit_xor! @2 @1)))
1445 /* Simplify (~X & Y) to X ^ Y if we know that (X & ~Y) is 0. */
1447 (bit_and (bit_not SSA_NAME@0) INTEGER_CST@1)
1448 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1449 && wi::bit_and_not (get_nonzero_bits (@0), wi::to_wide (@1)) == 0)
1452 /* For constants M and N, if M == (1LL << cst) - 1 && (N & M) == M,
1453 ((A & N) + B) & M -> (A + B) & M
1454 Similarly if (N & M) == 0,
1455 ((A | N) + B) & M -> (A + B) & M
1456 and for - instead of + (or unary - instead of +)
1457 and/or ^ instead of |.
1458 If B is constant and (B & M) == 0, fold into A & M. */
1459 (for op (plus minus)
1460 (for bitop (bit_and bit_ior bit_xor)
1462 (bit_and (op:s (bitop:s@0 @3 INTEGER_CST@4) @1) INTEGER_CST@2)
1465 tree utype = fold_bit_and_mask (TREE_TYPE (@0), @2, op, @0, bitop,
1466 @3, @4, @1, ERROR_MARK, NULL_TREE,
1469 (convert (bit_and (op (convert:utype { pmop[0]; })
1470 (convert:utype { pmop[1]; }))
1471 (convert:utype @2))))))
1473 (bit_and (op:s @0 (bitop:s@1 @3 INTEGER_CST@4)) INTEGER_CST@2)
1476 tree utype = fold_bit_and_mask (TREE_TYPE (@0), @2, op, @0, ERROR_MARK,
1477 NULL_TREE, NULL_TREE, @1, bitop, @3,
1480 (convert (bit_and (op (convert:utype { pmop[0]; })
1481 (convert:utype { pmop[1]; }))
1482 (convert:utype @2)))))))
1484 (bit_and (op:s @0 @1) INTEGER_CST@2)
1487 tree utype = fold_bit_and_mask (TREE_TYPE (@0), @2, op, @0, ERROR_MARK,
1488 NULL_TREE, NULL_TREE, @1, ERROR_MARK,
1489 NULL_TREE, NULL_TREE, pmop); }
1491 (convert (bit_and (op (convert:utype { pmop[0]; })
1492 (convert:utype { pmop[1]; }))
1493 (convert:utype @2)))))))
1494 (for bitop (bit_and bit_ior bit_xor)
1496 (bit_and (negate:s (bitop:s@0 @2 INTEGER_CST@3)) INTEGER_CST@1)
1499 tree utype = fold_bit_and_mask (TREE_TYPE (@0), @1, NEGATE_EXPR, @0,
1500 bitop, @2, @3, NULL_TREE, ERROR_MARK,
1501 NULL_TREE, NULL_TREE, pmop); }
1503 (convert (bit_and (negate (convert:utype { pmop[0]; }))
1504 (convert:utype @1)))))))
1506 /* X % Y is smaller than Y. */
1509 (cmp:c (trunc_mod @0 @1) @1)
1510 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
1511 { constant_boolean_node (cmp == LT_EXPR, type); })))
1515 (bit_ior @0 integer_all_onesp@1)
1520 (bit_ior @0 integer_zerop)
1525 (bit_and @0 integer_zerop@1)
1530 (for op (bit_ior bit_xor)
1532 (op (convert? @0) (convert? @1))
1533 (with { bool wascmp; }
1534 (if (types_match (TREE_TYPE (@0), TREE_TYPE (@1))
1535 && bitwise_inverted_equal_p (@0, @1, wascmp))
1538 ? constant_boolean_node (true, type)
1539 : build_all_ones_cst (TREE_TYPE (@0)); })))))
1544 { build_zero_cst (type); })
1546 /* Canonicalize X ^ ~0 to ~X. */
1548 (bit_xor @0 integer_all_onesp@1)
1553 (bit_and @0 integer_all_onesp)
1556 /* x & x -> x, x | x -> x */
1557 (for bitop (bit_and bit_ior)
1562 /* x & C -> x if we know that x & ~C == 0. */
1565 (bit_and SSA_NAME@0 INTEGER_CST@1)
1566 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1567 && wi::bit_and_not (get_nonzero_bits (@0), wi::to_wide (@1)) == 0)
1570 /* `a & (x | CST)` -> a if we know that (a & ~CST) == 0 */
1572 (bit_and:c SSA_NAME@0 (bit_ior @1 INTEGER_CST@2))
1573 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1574 && wi::bit_and_not (get_nonzero_bits (@0), wi::to_wide (@2)) == 0)
1577 /* x | C -> C if we know that x & ~C == 0. */
1579 (bit_ior SSA_NAME@0 INTEGER_CST@1)
1580 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1581 && wi::bit_and_not (get_nonzero_bits (@0), wi::to_wide (@1)) == 0)
1585 /* ~(~X - Y) -> X + Y and ~(~X + Y) -> X - Y. */
1587 (bit_not (minus (bit_not @0) @1))
1590 (bit_not (plus:c (bit_not @0) @1))
1592 /* (~X - ~Y) -> Y - X. */
1594 (minus (bit_not @0) (bit_not @1))
1595 (if (!TYPE_OVERFLOW_SANITIZED (type))
1596 (with { tree utype = unsigned_type_for (type); }
1597 (convert (minus (convert:utype @1) (convert:utype @0))))))
1599 /* ~(X - Y) -> ~X + Y. */
1601 (bit_not (minus:s @0 @1))
1602 (plus (bit_not @0) @1))
1604 (bit_not (plus:s @0 INTEGER_CST@1))
1605 (if ((INTEGRAL_TYPE_P (type)
1606 && TYPE_UNSIGNED (type))
1607 || (!TYPE_OVERFLOW_SANITIZED (type)
1608 && may_negate_without_overflow_p (@1)))
1609 (plus (bit_not @0) { const_unop (NEGATE_EXPR, type, @1); })))
1612 /* ~X + Y -> (Y - X) - 1. */
1614 (plus:c (bit_not @0) @1)
1615 (if (ANY_INTEGRAL_TYPE_P (type)
1616 && TYPE_OVERFLOW_WRAPS (type)
1617 /* -1 - X is folded to ~X, so we'd recurse endlessly. */
1618 && !integer_all_onesp (@1))
1619 (plus (minus @1 @0) { build_minus_one_cst (type); })
1620 (if (INTEGRAL_TYPE_P (type)
1621 && TREE_CODE (@1) == INTEGER_CST
1622 && wi::to_wide (@1) != wi::min_value (TYPE_PRECISION (type),
1624 (minus (plus @1 { build_minus_one_cst (type); }) @0))))
1627 /* ~(X >> Y) -> ~X >> Y if ~X can be simplified. */
1629 (bit_not (rshift:s @0 @1))
1630 (if (!TYPE_UNSIGNED (TREE_TYPE (@0)))
1631 (rshift (bit_not! @0) @1)
1632 /* For logical right shifts, this is possible only if @0 doesn't
1633 have MSB set and the logical right shift is changed into
1634 arithmetic shift. */
1635 (if (INTEGRAL_TYPE_P (type)
1636 && !wi::neg_p (tree_nonzero_bits (@0)))
1637 (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
1638 (convert (rshift (bit_not! (convert:stype @0)) @1))))))
1640 /* x + (x & 1) -> (x + 1) & ~1 */
1642 (plus:c @0 (bit_and:s @0 integer_onep@1))
1643 (bit_and (plus @0 @1) (bit_not @1)))
1645 /* x & ~(x & y) -> x & ~y */
1646 /* x | ~(x | y) -> x | ~y */
1647 (for bitop (bit_and bit_ior)
1649 (bitop:c @0 (bit_not (bitop:cs @0 @1)))
1650 (bitop @0 (bit_not @1))))
1652 /* (~x & y) | ~(x | y) -> ~x */
1654 (bit_ior:c (bit_and:c (bit_not@2 @0) @1) (bit_not (bit_ior:c @0 @1)))
1657 /* (x | y) ^ (x | ~y) -> ~x */
1659 (bit_xor:c (bit_ior:c @0 @1) (bit_ior:c @0 (bit_not @1)))
1662 /* (x & y) | ~(x | y) -> ~(x ^ y) */
1664 (bit_ior:c (bit_and:s @0 @1) (bit_not:s (bit_ior:s @0 @1)))
1665 (bit_not (bit_xor @0 @1)))
1667 /* (~x | y) ^ (x ^ y) -> x | ~y */
1669 (bit_xor:c (bit_ior:cs (bit_not @0) @1) (bit_xor:s @0 @1))
1670 (bit_ior @0 (bit_not @1)))
1672 /* (x ^ y) | ~(x | y) -> ~(x & y) */
1674 (bit_ior:c (bit_xor:s @0 @1) (bit_not:s (bit_ior:s @0 @1)))
1675 (bit_not (bit_and @0 @1)))
1677 /* (x & y) ^ (x | y) -> x ^ y */
1679 (bit_xor:c (bit_and @0 @1) (bit_ior @0 @1))
1682 /* (x ^ y) ^ (x | y) -> x & y */
1684 (bit_xor:c (bit_xor @0 @1) (bit_ior @0 @1))
1687 /* (x & y) + (x ^ y) -> x | y */
1688 /* (x & y) | (x ^ y) -> x | y */
1689 /* (x & y) ^ (x ^ y) -> x | y */
1690 (for op (plus bit_ior bit_xor)
1692 (op:c (bit_and @0 @1) (bit_xor @0 @1))
1695 /* (x & y) + (x | y) -> x + y */
1697 (plus:c (bit_and @0 @1) (bit_ior @0 @1))
1700 /* (x + y) - (x | y) -> x & y */
1702 (minus (plus @0 @1) (bit_ior @0 @1))
1703 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1704 && !TYPE_SATURATING (type))
1707 /* (x + y) - (x & y) -> x | y */
1709 (minus (plus @0 @1) (bit_and @0 @1))
1710 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1711 && !TYPE_SATURATING (type))
1714 /* (x | y) - y -> (x & ~y) */
1716 (minus (bit_ior:cs @0 @1) @1)
1717 (bit_and @0 (bit_not @1)))
1719 /* (x | y) - (x ^ y) -> x & y */
1721 (minus (bit_ior @0 @1) (bit_xor @0 @1))
1724 /* (x | y) - (x & y) -> x ^ y */
1726 (minus (bit_ior @0 @1) (bit_and @0 @1))
1729 /* (x | y) & ~(x & y) -> x ^ y */
1731 (bit_and:c (bit_ior @0 @1) (bit_not (bit_and @0 @1)))
1734 /* (x | y) & (~x ^ y) -> x & y */
1736 (bit_and:c (bit_ior:c @0 @1) (bit_xor:c @1 @2))
1737 (with { bool wascmp; }
1738 (if (bitwise_inverted_equal_p (@0, @2, wascmp)
1739 && (!wascmp || element_precision (type) == 1))
1742 /* (~x | y) & (x | ~y) -> ~(x ^ y) */
1744 (bit_and (bit_ior:cs (bit_not @0) @1) (bit_ior:cs @0 (bit_not @1)))
1745 (bit_not (bit_xor @0 @1)))
1747 /* (~x | y) ^ (x | ~y) -> x ^ y */
1749 (bit_xor (bit_ior:c (bit_not @0) @1) (bit_ior:c @0 (bit_not @1)))
1752 /* ((x & y) - (x | y)) - 1 -> ~(x ^ y) */
1754 (plus (nop_convert1? (minus@2 (nop_convert2? (bit_and:c @0 @1))
1755 (nop_convert2? (bit_ior @0 @1))))
1757 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1758 && !TYPE_SATURATING (type) && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2))
1759 && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@2))
1760 && !TYPE_SATURATING (TREE_TYPE (@2)))
1761 (bit_not (convert (bit_xor @0 @1)))))
1763 (minus (nop_convert1? (plus@2 (nop_convert2? (bit_and:c @0 @1))
1765 (nop_convert3? (bit_ior @0 @1)))
1766 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1767 && !TYPE_SATURATING (type) && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2))
1768 && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@2))
1769 && !TYPE_SATURATING (TREE_TYPE (@2)))
1770 (bit_not (convert (bit_xor @0 @1)))))
1772 (minus (nop_convert1? (bit_and @0 @1))
1773 (nop_convert2? (plus@2 (nop_convert3? (bit_ior:c @0 @1))
1775 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1776 && !TYPE_SATURATING (type) && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2))
1777 && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@2))
1778 && !TYPE_SATURATING (TREE_TYPE (@2)))
1779 (bit_not (convert (bit_xor @0 @1)))))
1781 /* ~x & ~y -> ~(x | y)
1782 ~x | ~y -> ~(x & y) */
1783 (for op (bit_and bit_ior)
1784 rop (bit_ior bit_and)
1786 (op (convert1? (bit_not @0)) (convert2? (bit_not @1)))
1787 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1788 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
1789 (bit_not (rop (convert @0) (convert @1))))))
1791 /* If we are XORing or adding two BIT_AND_EXPR's, both of which are and'ing
1792 with a constant, and the two constants have no bits in common,
1793 we should treat this as a BIT_IOR_EXPR since this may produce more
1795 (for op (bit_xor plus)
1797 (op (convert1? (bit_and@4 @0 INTEGER_CST@1))
1798 (convert2? (bit_and@5 @2 INTEGER_CST@3)))
1799 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
1800 && tree_nop_conversion_p (type, TREE_TYPE (@2))
1801 && (wi::to_wide (@1) & wi::to_wide (@3)) == 0)
1802 (bit_ior (convert @4) (convert @5)))))
1804 /* (X | Y) ^ X -> Y & ~ X*/
1806 (bit_xor:c (convert1? (bit_ior:c @@0 @1)) (convert2? @0))
1807 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1808 (convert (bit_and @1 (bit_not @0)))))
1810 /* (~X | Y) ^ X -> ~(X & Y). */
1812 (bit_xor:c (nop_convert1? (bit_ior:c (nop_convert2? (bit_not @0)) @1)) @2)
1813 (if (bitwise_equal_p (@0, @2))
1814 (convert (bit_not (bit_and @0 (convert @1))))))
1816 /* Convert ~X ^ ~Y to X ^ Y. */
1818 (bit_xor (convert1? (bit_not @0)) (convert2? (bit_not @1)))
1819 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1820 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
1821 (bit_xor (convert @0) (convert @1))))
1823 /* Convert ~X ^ C to X ^ ~C. */
1825 (bit_xor (convert? (bit_not @0)) INTEGER_CST@1)
1826 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1827 (bit_xor (convert @0) (bit_not @1))))
1829 /* Fold (X & Y) ^ Y and (X ^ Y) & Y as ~X & Y. */
1830 (for opo (bit_and bit_xor)
1831 opi (bit_xor bit_and)
1833 (opo:c (opi:cs @0 @1) @1)
1834 (bit_and (bit_not @0) @1)))
1836 /* Given a bit-wise operation CODE applied to ARG0 and ARG1, see if both
1837 operands are another bit-wise operation with a common input. If so,
1838 distribute the bit operations to save an operation and possibly two if
1839 constants are involved. For example, convert
1840 (A | B) & (A | C) into A | (B & C)
1841 Further simplification will occur if B and C are constants. */
1842 (for op (bit_and bit_ior bit_xor)
1843 rop (bit_ior bit_and bit_and)
1845 (op (convert? (rop:c @@0 @1)) (convert? (rop:c @0 @2)))
1846 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1847 && tree_nop_conversion_p (type, TREE_TYPE (@2)))
1848 (rop (convert @0) (op (convert @1) (convert @2))))))
1850 /* Some simple reassociation for bit operations, also handled in reassoc. */
1851 /* (X & Y) & Y -> X & Y
1852 (X | Y) | Y -> X | Y */
1853 (for op (bit_and bit_ior)
1855 (op:c (convert1?@2 (op:c @0 @@1)) (convert2? @1))
1857 /* (X ^ Y) ^ Y -> X */
1859 (bit_xor:c (convert1? (bit_xor:c @0 @@1)) (convert2? @1))
1862 /* (X & ~Y) & Y -> 0 */
1864 (bit_and:c (bit_and @0 @1) @2)
1865 (with { bool wascmp; }
1866 (if (bitwise_inverted_equal_p (@0, @2, wascmp)
1867 || bitwise_inverted_equal_p (@1, @2, wascmp))
1868 { wascmp ? constant_boolean_node (false, type) : build_zero_cst (type); })))
1869 /* (X | ~Y) | Y -> -1 */
1871 (bit_ior:c (bit_ior @0 @1) @2)
1872 (with { bool wascmp; }
1873 (if ((bitwise_inverted_equal_p (@0, @2, wascmp)
1874 || bitwise_inverted_equal_p (@1, @2, wascmp))
1875 && (!wascmp || element_precision (type) == 1))
1876 { build_all_ones_cst (TREE_TYPE (@0)); })))
1878 /* (X & Y) & (X & Z) -> (X & Y) & Z
1879 (X | Y) | (X | Z) -> (X | Y) | Z */
1880 (for op (bit_and bit_ior)
1882 (op (convert1?@3 (op:c@4 @0 @1)) (convert2?@5 (op:c@6 @0 @2)))
1883 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1884 && tree_nop_conversion_p (type, TREE_TYPE (@2)))
1885 (if (single_use (@5) && single_use (@6))
1886 (op @3 (convert @2))
1887 (if (single_use (@3) && single_use (@4))
1888 (op (convert @1) @5))))))
1889 /* (X ^ Y) ^ (X ^ Z) -> Y ^ Z */
1891 (bit_xor (convert1? (bit_xor:c @0 @1)) (convert2? (bit_xor:c @0 @2)))
1892 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1893 && tree_nop_conversion_p (type, TREE_TYPE (@2)))
1894 (bit_xor (convert @1) (convert @2))))
1896 /* Convert abs (abs (X)) into abs (X).
1897 also absu (absu (X)) into absu (X). */
1903 (absu (convert@2 (absu@1 @0)))
1904 (if (tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@1)))
1907 /* Convert abs[u] (-X) -> abs[u] (X). */
1916 /* Convert abs[u] (X) where X is nonnegative -> (X). */
1918 (abs tree_expr_nonnegative_p@0)
1922 (absu tree_expr_nonnegative_p@0)
1925 /* Simplify (-(X < 0) | 1) * X into abs (X) or absu(X). */
1927 (mult:c (nop_convert1?
1928 (bit_ior (nop_convert2? (negate (convert? (lt @0 integer_zerop))))
1931 (if (INTEGRAL_TYPE_P (type)
1932 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
1933 && !TYPE_UNSIGNED (TREE_TYPE (@0)))
1934 (if (TYPE_UNSIGNED (type))
1941 /* A few cases of fold-const.cc negate_expr_p predicate. */
1942 (match negate_expr_p
1944 (if ((INTEGRAL_TYPE_P (type)
1945 && TYPE_UNSIGNED (type))
1946 || (!TYPE_OVERFLOW_SANITIZED (type)
1947 && may_negate_without_overflow_p (t)))))
1948 (match negate_expr_p
1950 (match negate_expr_p
1952 (if (!TYPE_OVERFLOW_SANITIZED (type))))
1953 (match negate_expr_p
1955 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (t)))))
1956 /* VECTOR_CST handling of non-wrapping types would recurse in unsupported
1958 (match negate_expr_p
1960 (if (FLOAT_TYPE_P (TREE_TYPE (type)) || TYPE_OVERFLOW_WRAPS (type))))
1961 (match negate_expr_p
1963 (if ((ANY_INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_WRAPS (type))
1964 || (FLOAT_TYPE_P (type)
1965 && !HONOR_SIGN_DEPENDENT_ROUNDING (type)
1966 && !HONOR_SIGNED_ZEROS (type)))))
1968 /* (-A) * (-B) -> A * B */
1970 (mult:c (convert1? (negate @0)) (convert2? negate_expr_p@1))
1971 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
1972 && tree_nop_conversion_p (type, TREE_TYPE (@1)))
1973 (mult (convert @0) (convert (negate @1)))))
1975 /* -(A + B) -> (-B) - A. */
1977 (negate (plus:c @0 negate_expr_p@1))
1978 (if (!HONOR_SIGN_DEPENDENT_ROUNDING (type)
1979 && !HONOR_SIGNED_ZEROS (type))
1980 (minus (negate @1) @0)))
1982 /* -(A - B) -> B - A. */
1984 (negate (minus @0 @1))
1985 (if ((ANY_INTEGRAL_TYPE_P (type) && !TYPE_OVERFLOW_SANITIZED (type))
1986 || (FLOAT_TYPE_P (type)
1987 && !HONOR_SIGN_DEPENDENT_ROUNDING (type)
1988 && !HONOR_SIGNED_ZEROS (type)))
1991 (negate (pointer_diff @0 @1))
1992 (if (TYPE_OVERFLOW_UNDEFINED (type))
1993 (pointer_diff @1 @0)))
1995 /* A - B -> A + (-B) if B is easily negatable. */
1997 (minus @0 negate_expr_p@1)
1998 (if (!FIXED_POINT_TYPE_P (type))
1999 (plus @0 (negate @1))))
2001 /* 1 - a is a ^ 1 if a had a bool range. */
2002 /* This is only enabled for gimple as sometimes
2003 cfun is not set for the function which contains
2004 the SSA_NAME (e.g. while IPA passes are happening,
2005 fold might be called). */
2007 (minus integer_onep@0 SSA_NAME@1)
2008 (if (INTEGRAL_TYPE_P (type)
2009 && ssa_name_has_boolean_range (@1))
2012 /* Other simplifications of negation (c.f. fold_negate_expr_1). */
2014 (negate (mult:c@0 @1 negate_expr_p@2))
2015 (if (! TYPE_UNSIGNED (type)
2016 && ! HONOR_SIGN_DEPENDENT_ROUNDING (type)
2018 (mult @1 (negate @2))))
2021 (negate (rdiv@0 @1 negate_expr_p@2))
2022 (if (! HONOR_SIGN_DEPENDENT_ROUNDING (type)
2024 (rdiv @1 (negate @2))))
2027 (negate (rdiv@0 negate_expr_p@1 @2))
2028 (if (! HONOR_SIGN_DEPENDENT_ROUNDING (type)
2030 (rdiv (negate @1) @2)))
2032 /* Fold -((int)x >> (prec - 1)) into (unsigned)x >> (prec - 1). */
2034 (negate (convert? (rshift @0 INTEGER_CST@1)))
2035 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
2036 && wi::to_wide (@1) == element_precision (type) - 1)
2037 (with { tree stype = TREE_TYPE (@0);
2038 tree ntype = TYPE_UNSIGNED (stype) ? signed_type_for (stype)
2039 : unsigned_type_for (stype); }
2040 (if (VECTOR_TYPE_P (type))
2041 (view_convert (rshift (view_convert:ntype @0) @1))
2042 (convert (rshift (convert:ntype @0) @1))))))
2044 /* Try to fold (type) X op CST -> (type) (X op ((type-x) CST))
2046 For bitwise binary operations apply operand conversions to the
2047 binary operation result instead of to the operands. This allows
2048 to combine successive conversions and bitwise binary operations.
2049 We combine the above two cases by using a conditional convert. */
2050 (for bitop (bit_and bit_ior bit_xor)
2052 (bitop (convert@2 @0) (convert?@3 @1))
2053 (if (((TREE_CODE (@1) == INTEGER_CST
2054 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
2055 && (int_fits_type_p (@1, TREE_TYPE (@0))
2056 || tree_nop_conversion_p (TREE_TYPE (@0), type)))
2057 || types_match (@0, @1))
2058 && !POINTER_TYPE_P (TREE_TYPE (@0))
2059 && !VECTOR_TYPE_P (TREE_TYPE (@0))
2060 && TREE_CODE (TREE_TYPE (@0)) != OFFSET_TYPE
2061 /* ??? This transform conflicts with fold-const.cc doing
2062 Convert (T)(x & c) into (T)x & (T)c, if c is an integer
2063 constants (if x has signed type, the sign bit cannot be set
2064 in c). This folds extension into the BIT_AND_EXPR.
2065 Restrict it to GIMPLE to avoid endless recursions. */
2066 && (bitop != BIT_AND_EXPR || GIMPLE)
2067 && (/* That's a good idea if the conversion widens the operand, thus
2068 after hoisting the conversion the operation will be narrower.
2069 It is also a good if the conversion is a nop as moves the
2070 conversion to one side; allowing for combining of the conversions. */
2071 TYPE_PRECISION (TREE_TYPE (@0)) < TYPE_PRECISION (type)
2072 /* The conversion check for being a nop can only be done at the gimple
2073 level as fold_binary has some re-association code which can conflict
2074 with this if there is a "constant" which is not a full INTEGER_CST. */
2075 || (GIMPLE && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (type))
2076 /* It's also a good idea if the conversion is to a non-integer
2078 || GET_MODE_CLASS (TYPE_MODE (type)) != MODE_INT
2079 /* Or if the precision of TO is not the same as the precision
2081 || !type_has_mode_precision_p (type)
2082 /* In GIMPLE, getting rid of 2 conversions for one new results
2085 && TREE_CODE (@1) != INTEGER_CST
2086 && tree_nop_conversion_p (type, TREE_TYPE (@0))
2088 && single_use (@3))))
2089 (convert (bitop @0 (convert @1)))))
2090 /* In GIMPLE, getting rid of 2 conversions for one new results
2093 (convert (bitop:cs@2 (nop_convert:s @0) @1))
2095 && TREE_CODE (@1) != INTEGER_CST
2096 && tree_nop_conversion_p (type, TREE_TYPE (@2))
2097 && types_match (type, @0)
2098 && !POINTER_TYPE_P (TREE_TYPE (@0))
2099 && TREE_CODE (TREE_TYPE (@0)) != OFFSET_TYPE)
2100 (bitop @0 (convert @1)))))
2102 (for bitop (bit_and bit_ior)
2103 rbitop (bit_ior bit_and)
2104 /* (x | y) & x -> x */
2105 /* (x & y) | x -> x */
2107 (bitop:c (rbitop:c @0 @1) @0)
2109 /* (~x | y) & x -> x & y */
2110 /* (~x & y) | x -> x | y */
2112 (bitop:c (rbitop:c @2 @1) @0)
2113 (with { bool wascmp; }
2114 (if (bitwise_inverted_equal_p (@0, @2, wascmp)
2115 && (!wascmp || element_precision (type) == 1))
2117 /* (x | y) & (x & z) -> (x & z) */
2118 /* (x & y) | (x | z) -> (x | z) */
2120 (bitop:c (rbitop:c @0 @1) (bitop:c@3 @0 @2))
2122 /* (x | c) & ~(y | c) -> x & ~(y | c) */
2123 /* (x & c) | ~(y & c) -> x | ~(y & c) */
2125 (bitop:c (rbitop:c @0 @1) (bit_not@3 (rbitop:c @1 @2)))
2127 /* x & ~(y | x) -> 0 */
2128 /* x | ~(y & x) -> -1 */
2130 (bitop:c @0 (bit_not (rbitop:c @0 @1)))
2131 (if (bitop == BIT_AND_EXPR)
2132 { build_zero_cst (type); }
2133 { build_minus_one_cst (type); })))
2135 /* ((x | y) & z) | x -> (z & y) | x
2136 ((x ^ y) & z) | x -> (z & y) | x */
2137 (for op (bit_ior bit_xor)
2139 (bit_ior:c (nop_convert1?:s
2140 (bit_and:cs (nop_convert2?:s (op:cs @0 @1)) @2)) @3)
2141 (if (bitwise_equal_p (@0, @3))
2142 (convert (bit_ior (bit_and @1 (convert @2)) (convert @0))))))
2144 /* (x | CST1) & CST2 -> (x & CST2) | (CST1 & CST2) */
2146 (bit_and (bit_ior @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
2147 (bit_ior (bit_and @0 @2) (bit_and! @1 @2)))
2149 /* Combine successive equal operations with constants. */
2150 (for bitop (bit_and bit_ior bit_xor)
2152 (bitop (bitop @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
2153 (if (!CONSTANT_CLASS_P (@0))
2154 /* This is the canonical form regardless of whether (bitop @1 @2) can be
2155 folded to a constant. */
2156 (bitop @0 (bitop! @1 @2))
2157 /* In this case we have three constants and (bitop @0 @1) doesn't fold
2158 to a constant. This can happen if @0 or @1 is a POLY_INT_CST and if
2159 the values involved are such that the operation can't be decided at
2160 compile time. Try folding one of @0 or @1 with @2 to see whether
2161 that combination can be decided at compile time.
2163 Keep the existing form if both folds fail, to avoid endless
2165 (with { tree cst1 = const_binop (bitop, type, @0, @2); }
2167 (bitop @1 { cst1; })
2168 (with { tree cst2 = const_binop (bitop, type, @1, @2); }
2170 (bitop @0 { cst2; }))))))))
2172 /* Try simple folding for X op !X, and X op X with the help
2173 of the truth_valued_p and logical_inverted_value predicates. */
2174 (match truth_valued_p
2176 (if (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1)))
2177 (for op (tcc_comparison truth_and truth_andif truth_or truth_orif truth_xor)
2178 (match truth_valued_p
2180 (match truth_valued_p
2183 (match (logical_inverted_value @0)
2185 (match (logical_inverted_value @0)
2186 (bit_not truth_valued_p@0))
2187 (match (logical_inverted_value @0)
2188 (eq @0 integer_zerop))
2189 (match (logical_inverted_value @0)
2190 (ne truth_valued_p@0 integer_truep))
2191 (match (logical_inverted_value @0)
2192 (bit_xor truth_valued_p@0 integer_truep))
2196 (bit_and:c @0 (logical_inverted_value @0))
2197 { build_zero_cst (type); })
2198 /* X | !X and X ^ !X -> 1, , if X is truth-valued. */
2199 (for op (bit_ior bit_xor)
2201 (op:c truth_valued_p@0 (logical_inverted_value @0))
2202 { constant_boolean_node (true, type); }))
2203 /* X ==/!= !X is false/true. */
2206 (op:c truth_valued_p@0 (logical_inverted_value @0))
2207 { constant_boolean_node (op == NE_EXPR ? true : false, type); }))
2211 (bit_not (bit_not @0))
2214 /* zero_one_valued_p will match when a value is known to be either
2215 0 or 1 including constants 0 or 1.
2216 Signed 1-bits includes -1 so they cannot match here. */
2217 (match zero_one_valued_p
2219 (if (INTEGRAL_TYPE_P (type)
2220 && (TYPE_UNSIGNED (type)
2221 || TYPE_PRECISION (type) > 1)
2222 && wi::leu_p (tree_nonzero_bits (@0), 1))))
2223 (match zero_one_valued_p
2225 (if (INTEGRAL_TYPE_P (type)
2226 && (TYPE_UNSIGNED (type)
2227 || TYPE_PRECISION (type) > 1))))
2229 /* (a&1) is always [0,1] too. This is useful again when
2230 the range is not known. */
2231 /* Note this can't be recursive due to VN handling of equivalents,
2232 VN and would cause an infinite recursion. */
2233 (match zero_one_valued_p
2234 (bit_and:c@0 @1 integer_onep)
2235 (if (INTEGRAL_TYPE_P (type))))
2237 /* A conversion from an zero_one_valued_p is still a [0,1].
2238 This is useful when the range of a variable is not known */
2239 /* Note this matches can't be recursive because of the way VN handles
2240 nop conversions being equivalent and then recursive between them. */
2241 (match zero_one_valued_p
2243 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2244 && (TYPE_UNSIGNED (TREE_TYPE (@1))
2245 || TYPE_PRECISION (TREE_TYPE (@1)) > 1)
2246 && wi::leu_p (tree_nonzero_bits (@1), 1))))
2248 /* Transform { 0 or 1 } * { 0 or 1 } into { 0 or 1 } & { 0 or 1 }. */
2250 (mult zero_one_valued_p@0 zero_one_valued_p@1)
2251 (if (INTEGRAL_TYPE_P (type))
2254 (for cmp (tcc_comparison)
2255 icmp (inverted_tcc_comparison)
2256 /* Fold (((a < b) & c) | ((a >= b) & d)) into (a < b ? c : d) & 1. */
2259 (bit_and:c (convert? (cmp@0 @01 @02)) @3)
2260 (bit_and:c (convert? (icmp@4 @01 @02)) @5))
2261 (if (INTEGRAL_TYPE_P (type)
2262 && invert_tree_comparison (cmp, HONOR_NANS (@01)) == icmp
2263 /* The scalar version has to be canonicalized after vectorization
2264 because it makes unconditional loads conditional ones, which
2265 means we lose vectorization because the loads may trap. */
2266 && canonicalize_math_after_vectorization_p ())
2267 (bit_and (cond @0 @3 @5) { build_one_cst (type); })))
2269 /* Fold ((-(a < b) & c) | (-(a >= b) & d)) into a < b ? c : d. This is
2270 canonicalized further and we recognize the conditional form:
2271 (a < b ? c : 0) | (a >= b ? d : 0) into a < b ? c : d. */
2274 (cond (cmp@0 @01 @02) @3 zerop)
2275 (cond (icmp@4 @01 @02) @5 zerop))
2276 (if (INTEGRAL_TYPE_P (type)
2277 && invert_tree_comparison (cmp, HONOR_NANS (@01)) == icmp
2278 /* The scalar version has to be canonicalized after vectorization
2279 because it makes unconditional loads conditional ones, which
2280 means we lose vectorization because the loads may trap. */
2281 && canonicalize_math_after_vectorization_p ())
2284 /* Vector Fold (((a < b) & c) | ((a >= b) & d)) into a < b ? c : d.
2285 and ((~(a < b) & c) | (~(a >= b) & d)) into a < b ? c : d. */
2288 (bit_and:c (vec_cond:s (cmp@0 @6 @7) @4 @5) @2)
2289 (bit_and:c (vec_cond:s (icmp@1 @6 @7) @4 @5) @3))
2290 (if (integer_zerop (@5)
2291 && invert_tree_comparison (cmp, HONOR_NANS (@6)) == icmp)
2293 (if (integer_onep (@4))
2294 (bit_and (vec_cond @0 @2 @3) @4))
2295 (if (integer_minus_onep (@4))
2296 (vec_cond @0 @2 @3)))
2297 (if (integer_zerop (@4)
2298 && invert_tree_comparison (cmp, HONOR_NANS (@6)) == icmp)
2300 (if (integer_onep (@5))
2301 (bit_and (vec_cond @0 @3 @2) @5))
2302 (if (integer_minus_onep (@5))
2303 (vec_cond @0 @3 @2))))))
2305 /* Scalar Vectorized Fold ((-(a < b) & c) | (-(a >= b) & d))
2306 into a < b ? d : c. */
2309 (vec_cond:s (cmp@0 @4 @5) @2 integer_zerop)
2310 (vec_cond:s (icmp@1 @4 @5) @3 integer_zerop))
2311 (if (invert_tree_comparison (cmp, HONOR_NANS (@4)) == icmp)
2312 (vec_cond @0 @2 @3))))
2314 /* Transform X & -Y into X * Y when Y is { 0 or 1 }. */
2316 (bit_and:c (convert? (negate zero_one_valued_p@0)) @1)
2317 (if (INTEGRAL_TYPE_P (type)
2318 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
2319 && TREE_CODE (TREE_TYPE (@0)) != BOOLEAN_TYPE
2320 /* Sign extending of the neg or a truncation of the neg
2322 && (!TYPE_UNSIGNED (TREE_TYPE (@0))
2323 || TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))))
2324 (mult (convert @0) @1)))
2326 /* Narrow integer multiplication by a zero_one_valued_p operand.
2327 Multiplication by [0,1] is guaranteed not to overflow. */
2329 (convert (mult@0 zero_one_valued_p@1 INTEGER_CST@2))
2330 (if (INTEGRAL_TYPE_P (type)
2331 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
2332 && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (@0)))
2333 (mult (convert @1) (convert @2))))
2335 /* (X << C) != 0 can be simplified to X, when C is zero_one_valued_p.
2336 Check that the shift is well-defined (C is less than TYPE_PRECISION)
2337 as some targets (such as x86's SSE) may return zero for larger C. */
2339 (ne (lshift zero_one_valued_p@0 INTEGER_CST@1) integer_zerop@2)
2340 (if (tree_fits_shwi_p (@1)
2341 && tree_to_shwi (@1) > 0
2342 && tree_to_shwi (@1) < TYPE_PRECISION (TREE_TYPE (@0)))
2345 /* (X << C) == 0 can be simplified to X == 0, when C is zero_one_valued_p.
2346 Check that the shift is well-defined (C is less than TYPE_PRECISION)
2347 as some targets (such as x86's SSE) may return zero for larger C. */
2349 (eq (lshift zero_one_valued_p@0 INTEGER_CST@1) integer_zerop@2)
2350 (if (tree_fits_shwi_p (@1)
2351 && tree_to_shwi (@1) > 0
2352 && tree_to_shwi (@1) < TYPE_PRECISION (TREE_TYPE (@0)))
2355 /* Convert ~ (-A) to A - 1. */
2357 (bit_not (convert? (negate @0)))
2358 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
2359 || !TYPE_UNSIGNED (TREE_TYPE (@0)))
2360 (convert (minus @0 { build_each_one_cst (TREE_TYPE (@0)); }))))
2362 /* Convert - (~A) to A + 1. */
2364 (negate (nop_convert? (bit_not @0)))
2365 (plus (view_convert @0) { build_each_one_cst (type); }))
2367 /* (a & b) ^ (a == b) -> !(a | b) */
2368 /* (a & b) == (a ^ b) -> !(a | b) */
2369 (for first_op (bit_xor eq)
2370 second_op (eq bit_xor)
2372 (first_op:c (bit_and:c truth_valued_p@0 truth_valued_p@1) (second_op:c @0 @1))
2373 (bit_not (bit_ior @0 @1))))
2375 /* Convert ~ (A - 1) or ~ (A + -1) to -A. */
2377 (bit_not (convert? (minus @0 integer_each_onep)))
2378 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
2379 || !TYPE_UNSIGNED (TREE_TYPE (@0)))
2380 (convert (negate @0))))
2382 (bit_not (convert? (plus @0 integer_all_onesp)))
2383 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
2384 || !TYPE_UNSIGNED (TREE_TYPE (@0)))
2385 (convert (negate @0))))
2387 /* Part of convert ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify. */
2389 (bit_not (convert? (bit_xor @0 INTEGER_CST@1)))
2390 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2391 (convert (bit_xor @0 (bit_not @1)))))
2393 (bit_not (convert? (bit_xor:c (bit_not @0) @1)))
2394 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2395 (convert (bit_xor @0 @1))))
2397 /* Otherwise prefer ~(X ^ Y) to ~X ^ Y as more canonical. */
2399 (bit_xor:c (nop_convert?:s (bit_not:s @0)) @1)
2400 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2401 (bit_not (bit_xor (view_convert @0) @1))))
2403 /* ~(a ^ b) is a == b for truth valued a and b. */
2405 (bit_not (bit_xor:s truth_valued_p@0 truth_valued_p@1))
2406 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2407 && TYPE_PRECISION (TREE_TYPE (@0)) == 1)
2408 (convert (eq @0 @1))))
2410 /* (~a) == b is a ^ b for truth valued a and b. */
2412 (eq:c (bit_not:s truth_valued_p@0) truth_valued_p@1)
2413 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2414 && TYPE_PRECISION (TREE_TYPE (@0)) == 1)
2415 (convert (bit_xor @0 @1))))
2417 /* (x & ~m) | (y & m) -> ((x ^ y) & m) ^ x */
2419 (bit_ior:c (bit_and:cs @0 (bit_not @2)) (bit_and:cs @1 @2))
2420 (bit_xor (bit_and (bit_xor @0 @1) @2) @0))
2422 /* Fold A - (A & B) into ~B & A. */
2424 (minus (convert1? @0) (convert2?:s (bit_and:cs @@0 @1)))
2425 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
2426 && tree_nop_conversion_p (type, TREE_TYPE (@1)))
2427 (convert (bit_and (bit_not @1) @0))))
2429 /* (m1 CMP m2) * d -> (m1 CMP m2) ? d : 0 */
2430 (if (!canonicalize_math_p ())
2431 (for cmp (tcc_comparison)
2433 (mult:c (convert (cmp@0 @1 @2)) @3)
2434 (if (INTEGRAL_TYPE_P (type)
2435 && INTEGRAL_TYPE_P (TREE_TYPE (@0)))
2436 (cond @0 @3 { build_zero_cst (type); })))
2437 /* (-(m1 CMP m2)) & d -> (m1 CMP m2) ? d : 0 */
2439 (bit_and:c (negate (convert (cmp@0 @1 @2))) @3)
2440 (if (INTEGRAL_TYPE_P (type)
2441 && INTEGRAL_TYPE_P (TREE_TYPE (@0)))
2442 (cond @0 @3 { build_zero_cst (type); })))
2446 /* For integral types with undefined overflow and C != 0 fold
2447 x * C EQ/NE y * C into x EQ/NE y. */
2450 (cmp (mult:c @0 @1) (mult:c @2 @1))
2451 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2452 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2453 && tree_expr_nonzero_p (@1))
2456 /* For integral types with wrapping overflow and C odd fold
2457 x * C EQ/NE y * C into x EQ/NE y. */
2460 (cmp (mult @0 INTEGER_CST@1) (mult @2 @1))
2461 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2462 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))
2463 && (TREE_INT_CST_LOW (@1) & 1) != 0)
2466 /* For integral types with undefined overflow and C != 0 fold
2467 x * C RELOP y * C into:
2469 x RELOP y for nonnegative C
2470 y RELOP x for negative C */
2471 (for cmp (lt gt le ge)
2473 (cmp (mult:c @0 @1) (mult:c @2 @1))
2474 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2475 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
2476 (if (tree_expr_nonnegative_p (@1) && tree_expr_nonzero_p (@1))
2478 (if (TREE_CODE (@1) == INTEGER_CST
2479 && wi::neg_p (wi::to_wide (@1), TYPE_SIGN (TREE_TYPE (@1))))
2482 /* (X - 1U) <= INT_MAX-1U into (int) X > 0. */
2486 (cmp (plus @0 integer_minus_onep@1) INTEGER_CST@2)
2487 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2488 && TYPE_UNSIGNED (TREE_TYPE (@0))
2489 && TYPE_PRECISION (TREE_TYPE (@0)) > 1
2490 && (wi::to_wide (@2)
2491 == wi::max_value (TYPE_PRECISION (TREE_TYPE (@0)), SIGNED) - 1))
2492 (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
2493 (icmp (convert:stype @0) { build_int_cst (stype, 0); })))))
2495 /* X / 4 < Y / 4 iff X < Y when the division is known to be exact. */
2496 (for cmp (simple_comparison)
2498 (cmp (convert?@3 (exact_div @0 INTEGER_CST@2)) (convert? (exact_div @1 @2)))
2499 (if (element_precision (@3) >= element_precision (@0)
2500 && types_match (@0, @1))
2501 (if (wi::lt_p (wi::to_wide (@2), 0, TYPE_SIGN (TREE_TYPE (@2))))
2502 (if (!TYPE_UNSIGNED (TREE_TYPE (@3)))
2504 (if (tree_expr_nonzero_p (@0) && tree_expr_nonzero_p (@1))
2507 tree utype = unsigned_type_for (TREE_TYPE (@0));
2509 (cmp (convert:utype @1) (convert:utype @0)))))
2510 (if (wi::gt_p (wi::to_wide (@2), 1, TYPE_SIGN (TREE_TYPE (@2))))
2511 (if (TYPE_UNSIGNED (TREE_TYPE (@0)) || !TYPE_UNSIGNED (TREE_TYPE (@3)))
2515 tree utype = unsigned_type_for (TREE_TYPE (@0));
2517 (cmp (convert:utype @0) (convert:utype @1)))))))))
2519 /* X / C1 op C2 into a simple range test. */
2520 (for cmp (simple_comparison)
2522 (cmp (trunc_div:s @0 INTEGER_CST@1) INTEGER_CST@2)
2523 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2524 && integer_nonzerop (@1)
2525 && !TREE_OVERFLOW (@1)
2526 && !TREE_OVERFLOW (@2))
2527 (with { tree lo, hi; bool neg_overflow;
2528 enum tree_code code = fold_div_compare (cmp, @1, @2, &lo, &hi,
2531 (if (code == LT_EXPR || code == GE_EXPR)
2532 (if (TREE_OVERFLOW (lo))
2533 { build_int_cst (type, (code == LT_EXPR) ^ neg_overflow); }
2534 (if (code == LT_EXPR)
2537 (if (code == LE_EXPR || code == GT_EXPR)
2538 (if (TREE_OVERFLOW (hi))
2539 { build_int_cst (type, (code == LE_EXPR) ^ neg_overflow); }
2540 (if (code == LE_EXPR)
2544 { build_int_cst (type, code == NE_EXPR); })
2545 (if (code == EQ_EXPR && !hi)
2547 (if (code == EQ_EXPR && !lo)
2549 (if (code == NE_EXPR && !hi)
2551 (if (code == NE_EXPR && !lo)
2554 { build_range_check (UNKNOWN_LOCATION, type, @0, code == EQ_EXPR,
2558 tree etype = range_check_type (TREE_TYPE (@0));
2561 hi = fold_convert (etype, hi);
2562 lo = fold_convert (etype, lo);
2563 hi = const_binop (MINUS_EXPR, etype, hi, lo);
2566 (if (etype && hi && !TREE_OVERFLOW (hi))
2567 (if (code == EQ_EXPR)
2568 (le (minus (convert:etype @0) { lo; }) { hi; })
2569 (gt (minus (convert:etype @0) { lo; }) { hi; })))))))))
2571 /* X + Z < Y + Z is the same as X < Y when there is no overflow. */
2572 (for op (lt le ge gt)
2574 (op (plus:c @0 @2) (plus:c @1 @2))
2575 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2576 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
2579 /* As a special case, X + C < Y + C is the same as (signed) X < (signed) Y
2580 when C is an unsigned integer constant with only the MSB set, and X and
2581 Y have types of equal or lower integer conversion rank than C's. */
2582 (for op (lt le ge gt)
2584 (op (plus @1 INTEGER_CST@0) (plus @2 @0))
2585 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2586 && TYPE_UNSIGNED (TREE_TYPE (@0))
2587 && wi::only_sign_bit_p (wi::to_wide (@0)))
2588 (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
2589 (op (convert:stype @1) (convert:stype @2))))))
2591 /* For equality and subtraction, this is also true with wrapping overflow. */
2592 (for op (eq ne minus)
2594 (op (plus:c @0 @2) (plus:c @1 @2))
2595 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2596 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2597 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
2599 /* And similar for pointers. */
2602 (op (pointer_plus @0 @1) (pointer_plus @0 @2))
2605 (pointer_diff (pointer_plus @0 @1) (pointer_plus @0 @2))
2606 (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
2607 (convert (minus @1 @2))))
2609 /* X - Z < Y - Z is the same as X < Y when there is no overflow. */
2610 (for op (lt le ge gt)
2612 (op (minus @0 @2) (minus @1 @2))
2613 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2614 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
2616 /* For equality and subtraction, this is also true with wrapping overflow. */
2617 (for op (eq ne minus)
2619 (op (minus @0 @2) (minus @1 @2))
2620 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2621 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2622 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
2624 /* And for pointers... */
2625 (for op (simple_comparison)
2627 (op (pointer_diff@3 @0 @2) (pointer_diff @1 @2))
2628 (if (!TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
2631 (minus (pointer_diff@3 @0 @2) (pointer_diff @1 @2))
2632 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@3))
2633 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
2634 (pointer_diff @0 @1)))
2636 /* Z - X < Z - Y is the same as Y < X when there is no overflow. */
2637 (for op (lt le ge gt)
2639 (op (minus @2 @0) (minus @2 @1))
2640 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2641 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
2643 /* For equality and subtraction, this is also true with wrapping overflow. */
2644 (for op (eq ne minus)
2646 (op (minus @2 @0) (minus @2 @1))
2647 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2648 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2649 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
2651 /* And for pointers... */
2652 (for op (simple_comparison)
2654 (op (pointer_diff@3 @2 @0) (pointer_diff @2 @1))
2655 (if (!TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
2658 (minus (pointer_diff@3 @2 @0) (pointer_diff @2 @1))
2659 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@3))
2660 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
2661 (pointer_diff @1 @0)))
2663 /* X + Y < Y is the same as X < 0 when there is no overflow. */
2664 (for op (lt le gt ge)
2666 (op:c (plus:c@2 @0 @1) @1)
2667 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2668 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2669 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0))
2670 && (CONSTANT_CLASS_P (@0) || single_use (@2)))
2671 (op @0 { build_zero_cst (TREE_TYPE (@0)); }))))
2672 /* For equality, this is also true with wrapping overflow. */
2675 (op:c (nop_convert?@3 (plus:c@2 @0 (convert1? @1))) (convert2? @1))
2676 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2677 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2678 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
2679 && (CONSTANT_CLASS_P (@0) || (single_use (@2) && single_use (@3)))
2680 && tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@2))
2681 && tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@1)))
2682 (op @0 { build_zero_cst (TREE_TYPE (@0)); })))
2684 (op:c (nop_convert?@3 (pointer_plus@2 (convert1? @0) @1)) (convert2? @0))
2685 (if (tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@0))
2686 && tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0))
2687 && (CONSTANT_CLASS_P (@1) || (single_use (@2) && single_use (@3))))
2688 (op @1 { build_zero_cst (TREE_TYPE (@1)); }))))
2690 /* (&a + b) !=/== (&a[1] + c) -> (&a[0] - &a[1]) + b !=/== c */
2693 (neeq:c ADDR_EXPR@0 (pointer_plus @2 @3))
2694 (with { poly_int64 diff; tree inner_type = TREE_TYPE (@3);}
2695 (if (ptr_difference_const (@0, @2, &diff))
2696 (neeq { build_int_cst_type (inner_type, diff); } @3))))
2698 (neeq (pointer_plus ADDR_EXPR@0 @1) (pointer_plus ADDR_EXPR@2 @3))
2699 (with { poly_int64 diff; tree inner_type = TREE_TYPE (@1);}
2700 (if (ptr_difference_const (@0, @2, &diff))
2701 (neeq (plus { build_int_cst_type (inner_type, diff); } @1) @3)))))
2703 /* X - Y < X is the same as Y > 0 when there is no overflow.
2704 For equality, this is also true with wrapping overflow. */
2705 (for op (simple_comparison)
2707 (op:c @0 (minus@2 @0 @1))
2708 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2709 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2710 || ((op == EQ_EXPR || op == NE_EXPR)
2711 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
2712 && (CONSTANT_CLASS_P (@1) || single_use (@2)))
2713 (op @1 { build_zero_cst (TREE_TYPE (@1)); }))))
2716 (X / Y) == 0 -> X < Y if X, Y are unsigned.
2717 (X / Y) != 0 -> X >= Y, if X, Y are unsigned. */
2721 (cmp (trunc_div @0 @1) integer_zerop)
2722 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
2723 /* Complex ==/!= is allowed, but not </>=. */
2724 && TREE_CODE (TREE_TYPE (@0)) != COMPLEX_TYPE
2725 && (VECTOR_TYPE_P (type) || !VECTOR_TYPE_P (TREE_TYPE (@0))))
2728 /* X == C - X can never be true if C is odd. */
2731 (cmp:c (convert? @0) (convert1? (minus INTEGER_CST@1 (convert2? @0))))
2732 (if (TREE_INT_CST_LOW (@1) & 1)
2733 { constant_boolean_node (cmp == NE_EXPR, type); })))
2738 U needs to be non-negative.
2742 U and N needs to be non-negative
2746 U needs to be non-negative and N needs to be a negative constant.
2748 (for cmp (lt ge le gt )
2749 bitop (bit_ior bit_ior bit_and bit_and)
2751 (cmp:c (bitop:c tree_expr_nonnegative_p@0 @1) @0)
2752 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)))
2753 (if (bitop == BIT_AND_EXPR || tree_expr_nonnegative_p (@1))
2754 { constant_boolean_node (cmp == GE_EXPR || cmp == LE_EXPR, type); }
2755 /* The sign is opposite now so the comparison is swapped around. */
2756 (if (TREE_CODE (@1) == INTEGER_CST && wi::neg_p (wi::to_wide (@1)))
2757 { constant_boolean_node (cmp == LT_EXPR, type); })))))
2759 /* Arguments on which one can call get_nonzero_bits to get the bits
2761 (match with_possible_nonzero_bits
2763 (match with_possible_nonzero_bits
2765 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))))
2766 /* Slightly extended version, do not make it recursive to keep it cheap. */
2767 (match (with_possible_nonzero_bits2 @0)
2768 with_possible_nonzero_bits@0)
2769 (match (with_possible_nonzero_bits2 @0)
2770 (bit_and:c with_possible_nonzero_bits@0 @2))
2772 /* Same for bits that are known to be set, but we do not have
2773 an equivalent to get_nonzero_bits yet. */
2774 (match (with_certain_nonzero_bits2 @0)
2776 (match (with_certain_nonzero_bits2 @0)
2777 (bit_ior @1 INTEGER_CST@0))
2779 /* X == C (or X & Z == Y | C) is impossible if ~nonzero(X) & C != 0. */
2782 (cmp:c (with_possible_nonzero_bits2 @0) (with_certain_nonzero_bits2 @1))
2783 (if (wi::bit_and_not (wi::to_wide (@1), get_nonzero_bits (@0)) != 0)
2784 { constant_boolean_node (cmp == NE_EXPR, type); })))
2786 /* ((X inner_op C0) outer_op C1)
2787 With X being a tree where value_range has reasoned certain bits to always be
2788 zero throughout its computed value range,
2789 inner_op = {|,^}, outer_op = {|,^} and inner_op != outer_op
2790 where zero_mask has 1's for all bits that are sure to be 0 in
2792 if (inner_op == '^') C0 &= ~C1;
2793 if ((C0 & ~zero_mask) == 0) then emit (X outer_op (C0 outer_op C1)
2794 if ((C1 & ~zero_mask) == 0) then emit (X inner_op (C0 outer_op C1)
2796 (for inner_op (bit_ior bit_xor)
2797 outer_op (bit_xor bit_ior)
2800 (inner_op:s @2 INTEGER_CST@0) INTEGER_CST@1)
2804 wide_int zero_mask_not;
2808 if (TREE_CODE (@2) == SSA_NAME)
2809 zero_mask_not = get_nonzero_bits (@2);
2813 if (inner_op == BIT_XOR_EXPR)
2815 C0 = wi::bit_and_not (wi::to_wide (@0), wi::to_wide (@1));
2816 cst_emit = C0 | wi::to_wide (@1);
2820 C0 = wi::to_wide (@0);
2821 cst_emit = C0 ^ wi::to_wide (@1);
2824 (if (!fail && (C0 & zero_mask_not) == 0)
2825 (outer_op @2 { wide_int_to_tree (type, cst_emit); })
2826 (if (!fail && (wi::to_wide (@1) & zero_mask_not) == 0)
2827 (inner_op @2 { wide_int_to_tree (type, cst_emit); }))))))
2829 /* Associate (p +p off1) +p off2 as (p +p (off1 + off2)). */
2831 (pointer_plus (pointer_plus:s @0 @1) @3)
2832 (pointer_plus @0 (plus @1 @3)))
2835 (pointer_plus (convert:s (pointer_plus:s @0 @1)) @3)
2836 (convert:type (pointer_plus @0 (plus @1 @3))))
2843 tem4 = (unsigned long) tem3;
2848 (pointer_plus @0 (convert?@2 (minus@3 (convert @1) (convert @0))))
2849 /* Conditionally look through a sign-changing conversion. */
2850 (if (TYPE_PRECISION (TREE_TYPE (@2)) == TYPE_PRECISION (TREE_TYPE (@3))
2851 && ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@1)))
2852 || (GENERIC && type == TREE_TYPE (@1))))
2855 (pointer_plus @0 (convert?@2 (pointer_diff@3 @1 @@0)))
2856 (if (TYPE_PRECISION (TREE_TYPE (@2)) >= TYPE_PRECISION (TREE_TYPE (@3)))
2860 tem = (sizetype) ptr;
2864 and produce the simpler and easier to analyze with respect to alignment
2865 ... = ptr & ~algn; */
2867 (pointer_plus @0 (negate (bit_and (convert @0) INTEGER_CST@1)))
2868 (with { tree algn = wide_int_to_tree (TREE_TYPE (@0), ~wi::to_wide (@1)); }
2869 (bit_and @0 { algn; })))
2871 /* Try folding difference of addresses. */
2873 (minus (convert ADDR_EXPR@0) (convert (pointer_plus @1 @2)))
2874 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2875 (with { poly_int64 diff; }
2876 (if (ptr_difference_const (@0, @1, &diff))
2877 (minus { build_int_cst_type (type, diff); } (convert @2))))))
2879 (minus (convert (pointer_plus @0 @2)) (convert ADDR_EXPR@1))
2880 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2881 (with { poly_int64 diff; }
2882 (if (ptr_difference_const (@0, @1, &diff))
2883 (plus (convert @2) { build_int_cst_type (type, diff); })))))
2885 (minus (convert ADDR_EXPR@0) (convert @1))
2886 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2887 (with { poly_int64 diff; }
2888 (if (ptr_difference_const (@0, @1, &diff))
2889 { build_int_cst_type (type, diff); }))))
2891 (minus (convert @0) (convert ADDR_EXPR@1))
2892 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2893 (with { poly_int64 diff; }
2894 (if (ptr_difference_const (@0, @1, &diff))
2895 { build_int_cst_type (type, diff); }))))
2897 (pointer_diff (convert?@2 ADDR_EXPR@0) (convert1?@3 @1))
2898 (if (tree_nop_conversion_p (TREE_TYPE(@2), TREE_TYPE (@0))
2899 && tree_nop_conversion_p (TREE_TYPE(@3), TREE_TYPE (@1)))
2900 (with { poly_int64 diff; }
2901 (if (ptr_difference_const (@0, @1, &diff))
2902 { build_int_cst_type (type, diff); }))))
2904 (pointer_diff (convert?@2 @0) (convert1?@3 ADDR_EXPR@1))
2905 (if (tree_nop_conversion_p (TREE_TYPE(@2), TREE_TYPE (@0))
2906 && tree_nop_conversion_p (TREE_TYPE(@3), TREE_TYPE (@1)))
2907 (with { poly_int64 diff; }
2908 (if (ptr_difference_const (@0, @1, &diff))
2909 { build_int_cst_type (type, diff); }))))
2911 /* (&a+b) - (&a[1] + c) -> sizeof(a[0]) + (b - c) */
2913 (pointer_diff (pointer_plus ADDR_EXPR@0 @1) (pointer_plus ADDR_EXPR@2 @3))
2914 (with { poly_int64 diff; }
2915 (if (ptr_difference_const (@0, @2, &diff))
2916 (plus { build_int_cst_type (type, diff); } (convert (minus @1 @3))))))
2917 /* (p + b) - &p->d -> offsetof (*p, d) + b */
2919 (pointer_diff (pointer_plus @0 @1) ADDR_EXPR@2)
2920 (with { poly_int64 diff; }
2921 (if (ptr_difference_const (@0, @2, &diff))
2922 (plus { build_int_cst_type (type, diff); } (convert @1)))))
2924 (pointer_diff ADDR_EXPR@0 (pointer_plus @1 @2))
2925 (with { poly_int64 diff; }
2926 (if (ptr_difference_const (@0, @1, &diff))
2927 (minus { build_int_cst_type (type, diff); } (convert @2)))))
2929 /* Canonicalize (T *)(ptr - ptr-cst) to &MEM[ptr + -ptr-cst]. */
2931 (convert (pointer_diff @0 INTEGER_CST@1))
2932 (if (POINTER_TYPE_P (type))
2933 { build_fold_addr_expr_with_type
2934 (build2 (MEM_REF, char_type_node, @0,
2935 wide_int_to_tree (ptr_type_node, wi::neg (wi::to_wide (@1)))),
2938 /* If arg0 is derived from the address of an object or function, we may
2939 be able to fold this expression using the object or function's
2942 (bit_and (convert? @0) INTEGER_CST@1)
2943 (if (POINTER_TYPE_P (TREE_TYPE (@0))
2944 && tree_nop_conversion_p (type, TREE_TYPE (@0)))
2948 unsigned HOST_WIDE_INT bitpos;
2949 get_pointer_alignment_1 (@0, &align, &bitpos);
2951 (if (wi::ltu_p (wi::to_wide (@1), align / BITS_PER_UNIT))
2952 { wide_int_to_tree (type, (wi::to_wide (@1)
2953 & (bitpos / BITS_PER_UNIT))); }))))
2956 uniform_integer_cst_p
2958 tree int_cst = uniform_integer_cst_p (t);
2959 tree inner_type = TREE_TYPE (int_cst);
2961 (if ((INTEGRAL_TYPE_P (inner_type)
2962 || POINTER_TYPE_P (inner_type))
2963 && wi::eq_p (wi::to_wide (int_cst), wi::min_value (inner_type))))))
2966 uniform_integer_cst_p
2968 tree int_cst = uniform_integer_cst_p (t);
2969 tree itype = TREE_TYPE (int_cst);
2971 (if ((INTEGRAL_TYPE_P (itype)
2972 || POINTER_TYPE_P (itype))
2973 && wi::eq_p (wi::to_wide (int_cst), wi::max_value (itype))))))
2975 /* x > y && x != XXX_MIN --> x > y
2976 x > y && x == XXX_MIN --> false . */
2979 (bit_and:c (gt:c@2 @0 @1) (eqne @0 min_value))
2981 (if (eqne == EQ_EXPR)
2982 { constant_boolean_node (false, type); })
2983 (if (eqne == NE_EXPR)
2987 /* x < y && x != XXX_MAX --> x < y
2988 x < y && x == XXX_MAX --> false. */
2991 (bit_and:c (lt:c@2 @0 @1) (eqne @0 max_value))
2993 (if (eqne == EQ_EXPR)
2994 { constant_boolean_node (false, type); })
2995 (if (eqne == NE_EXPR)
2999 /* x <= y && x == XXX_MIN --> x == XXX_MIN. */
3001 (bit_and:c (le:c @0 @1) (eq@2 @0 min_value))
3004 /* x >= y && x == XXX_MAX --> x == XXX_MAX. */
3006 (bit_and:c (ge:c @0 @1) (eq@2 @0 max_value))
3009 /* x > y || x != XXX_MIN --> x != XXX_MIN. */
3011 (bit_ior:c (gt:c @0 @1) (ne@2 @0 min_value))
3014 /* x <= y || x != XXX_MIN --> true. */
3016 (bit_ior:c (le:c @0 @1) (ne @0 min_value))
3017 { constant_boolean_node (true, type); })
3019 /* x <= y || x == XXX_MIN --> x <= y. */
3021 (bit_ior:c (le:c@2 @0 @1) (eq @0 min_value))
3024 /* x < y || x != XXX_MAX --> x != XXX_MAX. */
3026 (bit_ior:c (lt:c @0 @1) (ne@2 @0 max_value))
3029 /* x >= y || x != XXX_MAX --> true
3030 x >= y || x == XXX_MAX --> x >= y. */
3033 (bit_ior:c (ge:c@2 @0 @1) (eqne @0 max_value))
3035 (if (eqne == EQ_EXPR)
3037 (if (eqne == NE_EXPR)
3038 { constant_boolean_node (true, type); }))))
3040 /* y == XXX_MIN || x < y --> x <= y - 1 */
3042 (bit_ior:c (eq:s @1 min_value) (lt:cs @0 @1))
3043 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
3044 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
3045 (le @0 (minus @1 { build_int_cst (TREE_TYPE (@1), 1); }))))
3047 /* y != XXX_MIN && x >= y --> x > y - 1 */
3049 (bit_and:c (ne:s @1 min_value) (ge:cs @0 @1))
3050 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
3051 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
3052 (gt @0 (minus @1 { build_int_cst (TREE_TYPE (@1), 1); }))))
3054 /* Convert (X == CST1) && ((other)X OP2 CST2) to a known value
3055 based on CST1 OP2 CST2. Similarly for (X != CST1). */
3056 /* Convert (X == Y) && (X OP2 Y) to a known value if X is an integral type.
3057 Similarly for (X != Y). */
3060 (for code2 (eq ne lt gt le ge)
3062 (bit_and:c (code1:c@3 @0 @1) (code2:c@4 (convert?@c0 @0) @2))
3063 (if ((TREE_CODE (@1) == INTEGER_CST
3064 && TREE_CODE (@2) == INTEGER_CST)
3065 || ((INTEGRAL_TYPE_P (TREE_TYPE (@1))
3066 || POINTER_TYPE_P (TREE_TYPE (@1)))
3067 && bitwise_equal_p (@1, @2)))
3070 bool one_before = false;
3071 bool one_after = false;
3073 bool allbits = true;
3074 if (TREE_CODE (@1) == INTEGER_CST
3075 && TREE_CODE (@2) == INTEGER_CST)
3077 allbits = TYPE_PRECISION (TREE_TYPE (@1)) <= TYPE_PRECISION (TREE_TYPE (@2));
3078 auto t1 = wi::to_wide (fold_convert (TREE_TYPE (@2), @1));
3079 auto t2 = wi::to_wide (@2);
3080 cmp = wi::cmp (t1, t2, TYPE_SIGN (TREE_TYPE (@2)));
3091 case EQ_EXPR: val = (cmp == 0); break;
3092 case NE_EXPR: val = (cmp != 0); break;
3093 case LT_EXPR: val = (cmp < 0); break;
3094 case GT_EXPR: val = (cmp > 0); break;
3095 case LE_EXPR: val = (cmp <= 0); break;
3096 case GE_EXPR: val = (cmp >= 0); break;
3097 default: gcc_unreachable ();
3101 (if (code1 == EQ_EXPR && val) @3)
3102 (if (code1 == EQ_EXPR && !val) { constant_boolean_node (false, type); })
3103 (if (code1 == NE_EXPR && !val && allbits) @4)
3104 (if (code1 == NE_EXPR
3108 (gt @c0 (convert @1)))
3109 (if (code1 == NE_EXPR
3113 (lt @c0 (convert @1)))
3114 /* (a != (b+1)) & (a > b) -> a > (b+1) */
3115 (if (code1 == NE_EXPR
3119 (gt @c0 (convert @1)))
3120 /* (a != (b-1)) & (a < b) -> a < (b-1) */
3121 (if (code1 == NE_EXPR
3125 (lt @c0 (convert @1)))
3133 /* Convert (X OP1 CST1) && (X OP2 CST2).
3134 Convert (X OP1 Y) && (X OP2 Y). */
3136 (for code1 (lt le gt ge)
3137 (for code2 (lt le gt ge)
3139 (bit_and (code1:c@3 @0 @1) (code2:c@4 @0 @2))
3140 (if ((TREE_CODE (@1) == INTEGER_CST
3141 && TREE_CODE (@2) == INTEGER_CST)
3142 || ((INTEGRAL_TYPE_P (TREE_TYPE (@1))
3143 || POINTER_TYPE_P (TREE_TYPE (@1)))
3144 && operand_equal_p (@1, @2)))
3148 if (TREE_CODE (@1) == INTEGER_CST
3149 && TREE_CODE (@2) == INTEGER_CST)
3150 cmp = tree_int_cst_compare (@1, @2);
3153 /* Choose the more restrictive of two < or <= comparisons. */
3154 (if ((code1 == LT_EXPR || code1 == LE_EXPR)
3155 && (code2 == LT_EXPR || code2 == LE_EXPR))
3156 (if ((cmp < 0) || (cmp == 0 && code1 == LT_EXPR))
3159 /* Likewise chose the more restrictive of two > or >= comparisons. */
3160 (if ((code1 == GT_EXPR || code1 == GE_EXPR)
3161 && (code2 == GT_EXPR || code2 == GE_EXPR))
3162 (if ((cmp > 0) || (cmp == 0 && code1 == GT_EXPR))
3165 /* Check for singleton ranges. */
3167 && ((code1 == LE_EXPR && code2 == GE_EXPR)
3168 || (code1 == GE_EXPR && code2 == LE_EXPR)))
3170 /* Check for disjoint ranges. */
3172 && (code1 == LT_EXPR || code1 == LE_EXPR)
3173 && (code2 == GT_EXPR || code2 == GE_EXPR))
3174 { constant_boolean_node (false, type); })
3176 && (code1 == GT_EXPR || code1 == GE_EXPR)
3177 && (code2 == LT_EXPR || code2 == LE_EXPR))
3178 { constant_boolean_node (false, type); })
3181 /* Convert (X == CST1) || (X OP2 CST2) to a known value
3182 based on CST1 OP2 CST2. Similarly for (X != CST1). */
3183 /* Convert (X == Y) || (X OP2 Y) to a known value if X is an integral type.
3184 Similarly for (X != Y). */
3187 (for code2 (eq ne lt gt le ge)
3189 (bit_ior:c (code1:c@3 @0 @1) (code2:c@4 (convert?@c0 @0) @2))
3190 (if ((TREE_CODE (@1) == INTEGER_CST
3191 && TREE_CODE (@2) == INTEGER_CST)
3192 || ((INTEGRAL_TYPE_P (TREE_TYPE (@1))
3193 || POINTER_TYPE_P (TREE_TYPE (@1)))
3194 && bitwise_equal_p (@1, @2)))
3197 bool one_before = false;
3198 bool one_after = false;
3200 bool allbits = true;
3201 if (TREE_CODE (@1) == INTEGER_CST
3202 && TREE_CODE (@2) == INTEGER_CST)
3204 allbits = TYPE_PRECISION (TREE_TYPE (@1)) <= TYPE_PRECISION (TREE_TYPE (@2));
3205 auto t1 = wi::to_wide (fold_convert (TREE_TYPE (@2), @1));
3206 auto t2 = wi::to_wide (@2);
3207 cmp = wi::cmp (t1, t2, TYPE_SIGN (TREE_TYPE (@2)));
3218 case EQ_EXPR: val = (cmp == 0); break;
3219 case NE_EXPR: val = (cmp != 0); break;
3220 case LT_EXPR: val = (cmp < 0); break;
3221 case GT_EXPR: val = (cmp > 0); break;
3222 case LE_EXPR: val = (cmp <= 0); break;
3223 case GE_EXPR: val = (cmp >= 0); break;
3224 default: gcc_unreachable ();
3228 (if (code1 == EQ_EXPR && val) @4)
3229 (if (code1 == NE_EXPR && val && allbits) { constant_boolean_node (true, type); })
3230 (if (code1 == NE_EXPR && !val && allbits) @3)
3231 (if (code1 == EQ_EXPR
3236 (if (code1 == EQ_EXPR
3241 /* (a == (b-1)) | (a >= b) -> a >= (b-1) */
3242 (if (code1 == EQ_EXPR
3246 (ge @c0 (convert @1)))
3247 /* (a == (b+1)) | (a <= b) -> a <= (b-1) */
3248 (if (code1 == EQ_EXPR
3252 (le @c0 (convert @1)))
3260 /* Convert (X OP1 CST1) || (X OP2 CST2).
3261 Convert (X OP1 Y) || (X OP2 Y). */
3263 (for code1 (lt le gt ge)
3264 (for code2 (lt le gt ge)
3266 (bit_ior (code1@3 @0 @1) (code2@4 @0 @2))
3267 (if ((TREE_CODE (@1) == INTEGER_CST
3268 && TREE_CODE (@2) == INTEGER_CST)
3269 || ((INTEGRAL_TYPE_P (TREE_TYPE (@1))
3270 || POINTER_TYPE_P (TREE_TYPE (@1)))
3271 && operand_equal_p (@1, @2)))
3275 if (TREE_CODE (@1) == INTEGER_CST
3276 && TREE_CODE (@2) == INTEGER_CST)
3277 cmp = tree_int_cst_compare (@1, @2);
3280 /* Choose the more restrictive of two < or <= comparisons. */
3281 (if ((code1 == LT_EXPR || code1 == LE_EXPR)
3282 && (code2 == LT_EXPR || code2 == LE_EXPR))
3283 (if ((cmp < 0) || (cmp == 0 && code1 == LT_EXPR))
3286 /* Likewise chose the more restrictive of two > or >= comparisons. */
3287 (if ((code1 == GT_EXPR || code1 == GE_EXPR)
3288 && (code2 == GT_EXPR || code2 == GE_EXPR))
3289 (if ((cmp > 0) || (cmp == 0 && code1 == GT_EXPR))
3292 /* Check for singleton ranges. */
3294 && ((code1 == LT_EXPR && code2 == GT_EXPR)
3295 || (code1 == GT_EXPR && code2 == LT_EXPR)))
3297 /* Check for disjoint ranges. */
3299 && (code1 == LT_EXPR || code1 == LE_EXPR)
3300 && (code2 == GT_EXPR || code2 == GE_EXPR))
3301 { constant_boolean_node (true, type); })
3303 && (code1 == GT_EXPR || code1 == GE_EXPR)
3304 && (code2 == LT_EXPR || code2 == LE_EXPR))
3305 { constant_boolean_node (true, type); })
3308 /* Optimize (a CMP b) ^ (a CMP b) */
3309 /* Optimize (a CMP b) != (a CMP b) */
3310 (for op (bit_xor ne)
3311 (for cmp1 (lt lt lt le le le)
3312 cmp2 (gt eq ne ge eq ne)
3313 rcmp (ne le gt ne lt ge)
3315 (op:c (cmp1:c @0 @1) (cmp2:c @0 @1))
3316 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
3319 /* Optimize (a CMP b) == (a CMP b) */
3320 (for cmp1 (lt lt lt le le le)
3321 cmp2 (gt eq ne ge eq ne)
3322 rcmp (eq gt le eq ge lt)
3324 (eq:c (cmp1:c @0 @1) (cmp2:c @0 @1))
3325 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
3328 /* We can't reassociate at all for saturating types. */
3329 (if (!TYPE_SATURATING (type))
3331 /* Contract negates. */
3332 /* A + (-B) -> A - B */
3334 (plus:c @0 (convert? (negate @1)))
3335 /* Apply STRIP_NOPS on the negate. */
3336 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
3337 && !TYPE_OVERFLOW_SANITIZED (type))
3341 if (INTEGRAL_TYPE_P (type)
3342 && TYPE_OVERFLOW_WRAPS (type) != TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
3343 t1 = TYPE_OVERFLOW_WRAPS (type) ? type : TREE_TYPE (@1);
3345 (convert (minus (convert:t1 @0) (convert:t1 @1))))))
3346 /* A - (-B) -> A + B */
3348 (minus @0 (convert? (negate @1)))
3349 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
3350 && !TYPE_OVERFLOW_SANITIZED (type))
3354 if (INTEGRAL_TYPE_P (type)
3355 && TYPE_OVERFLOW_WRAPS (type) != TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
3356 t1 = TYPE_OVERFLOW_WRAPS (type) ? type : TREE_TYPE (@1);
3358 (convert (plus (convert:t1 @0) (convert:t1 @1))))))
3360 Sign-extension is ok except for INT_MIN, which thankfully cannot
3361 happen without overflow. */
3363 (negate (convert (negate @1)))
3364 (if (INTEGRAL_TYPE_P (type)
3365 && (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@1))
3366 || (!TYPE_UNSIGNED (TREE_TYPE (@1))
3367 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))))
3368 && !TYPE_OVERFLOW_SANITIZED (type)
3369 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@1)))
3372 (negate (convert negate_expr_p@1))
3373 (if (SCALAR_FLOAT_TYPE_P (type)
3374 && ((DECIMAL_FLOAT_TYPE_P (type)
3375 == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1))
3376 && TYPE_PRECISION (type) >= TYPE_PRECISION (TREE_TYPE (@1)))
3377 || !HONOR_SIGN_DEPENDENT_ROUNDING (type)))
3378 (convert (negate @1))))
3380 (negate (nop_convert? (negate @1)))
3381 (if (!TYPE_OVERFLOW_SANITIZED (type)
3382 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@1)))
3385 /* We can't reassociate floating-point unless -fassociative-math
3386 or fixed-point plus or minus because of saturation to +-Inf. */
3387 (if ((!FLOAT_TYPE_P (type) || flag_associative_math)
3388 && !FIXED_POINT_TYPE_P (type))
3390 /* Match patterns that allow contracting a plus-minus pair
3391 irrespective of overflow issues. */
3392 /* (A +- B) - A -> +- B */
3393 /* (A +- B) -+ B -> A */
3394 /* A - (A +- B) -> -+ B */
3395 /* A +- (B -+ A) -> +- B */
3397 (minus (nop_convert1? (plus:c (nop_convert2? @0) @1)) @0)
3400 (minus (nop_convert1? (minus (nop_convert2? @0) @1)) @0)
3401 (if (!ANY_INTEGRAL_TYPE_P (type)
3402 || TYPE_OVERFLOW_WRAPS (type))
3403 (negate (view_convert @1))
3404 (view_convert (negate @1))))
3406 (plus:c (nop_convert1? (minus @0 (nop_convert2? @1))) @1)
3409 (minus @0 (nop_convert1? (plus:c (nop_convert2? @0) @1)))
3410 (if (!ANY_INTEGRAL_TYPE_P (type)
3411 || TYPE_OVERFLOW_WRAPS (type))
3412 (negate (view_convert @1))
3413 (view_convert (negate @1))))
3415 (minus @0 (nop_convert1? (minus (nop_convert2? @0) @1)))
3417 /* (A +- B) + (C - A) -> C +- B */
3418 /* (A + B) - (A - C) -> B + C */
3419 /* More cases are handled with comparisons. */
3421 (plus:c (plus:c @0 @1) (minus @2 @0))
3424 (plus:c (minus @0 @1) (minus @2 @0))
3427 (plus:c (pointer_diff @0 @1) (pointer_diff @2 @0))
3428 (if (TYPE_OVERFLOW_UNDEFINED (type)
3429 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0)))
3430 (pointer_diff @2 @1)))
3432 (minus (plus:c @0 @1) (minus @0 @2))
3435 /* (A +- CST1) +- CST2 -> A + CST3
3436 Use view_convert because it is safe for vectors and equivalent for
3438 (for outer_op (plus minus)
3439 (for inner_op (plus minus)
3440 neg_inner_op (minus plus)
3442 (outer_op (nop_convert? (inner_op @0 CONSTANT_CLASS_P@1))
3444 /* If one of the types wraps, use that one. */
3445 (if (!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_WRAPS (type))
3446 /* If all 3 captures are CONSTANT_CLASS_P, punt, as we might recurse
3447 forever if something doesn't simplify into a constant. */
3448 (if (!CONSTANT_CLASS_P (@0))
3449 (if (outer_op == PLUS_EXPR)
3450 (plus (view_convert @0) (inner_op! @2 (view_convert @1)))
3451 (minus (view_convert @0) (neg_inner_op! @2 (view_convert @1)))))
3452 (if (!ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3453 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
3454 (if (outer_op == PLUS_EXPR)
3455 (view_convert (plus @0 (inner_op! (view_convert @2) @1)))
3456 (view_convert (minus @0 (neg_inner_op! (view_convert @2) @1))))
3457 /* If the constant operation overflows we cannot do the transform
3458 directly as we would introduce undefined overflow, for example
3459 with (a - 1) + INT_MIN. */
3460 (if (types_match (type, @0) && !TYPE_OVERFLOW_SANITIZED (type))
3461 (with { tree cst = const_binop (outer_op == inner_op
3462 ? PLUS_EXPR : MINUS_EXPR,
3465 (if (INTEGRAL_TYPE_P (type) && !TREE_OVERFLOW (cst))
3466 (inner_op @0 { cst; } )
3467 /* X+INT_MAX+1 is X-INT_MIN. */
3468 (if (INTEGRAL_TYPE_P (type)
3469 && wi::to_wide (cst) == wi::min_value (type))
3470 (neg_inner_op @0 { wide_int_to_tree (type, wi::to_wide (cst)); })
3471 /* Last resort, use some unsigned type. */
3472 (with { tree utype = unsigned_type_for (type); }
3474 (view_convert (inner_op
3475 (view_convert:utype @0)
3477 { TREE_OVERFLOW (cst)
3478 ? drop_tree_overflow (cst) : cst; })))))))))))))))
3480 /* (CST1 - A) +- CST2 -> CST3 - A */
3481 (for outer_op (plus minus)
3483 (outer_op (nop_convert? (minus CONSTANT_CLASS_P@1 @0)) CONSTANT_CLASS_P@2)
3484 /* If one of the types wraps, use that one. */
3485 (if (!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_WRAPS (type))
3486 /* If all 3 captures are CONSTANT_CLASS_P, punt, as we might recurse
3487 forever if something doesn't simplify into a constant. */
3488 (if (!CONSTANT_CLASS_P (@0))
3489 (minus (outer_op! (view_convert @1) @2) (view_convert @0)))
3490 (if (!ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3491 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
3492 (view_convert (minus (outer_op! @1 (view_convert @2)) @0))
3493 (if (types_match (type, @0) && !TYPE_OVERFLOW_SANITIZED (type))
3494 (with { tree cst = const_binop (outer_op, type, @1, @2); }
3495 (if (cst && !TREE_OVERFLOW (cst))
3496 (minus { cst; } @0))))))))
3498 /* CST1 - (CST2 - A) -> CST3 + A
3499 Use view_convert because it is safe for vectors and equivalent for
3502 (minus CONSTANT_CLASS_P@1 (nop_convert? (minus CONSTANT_CLASS_P@2 @0)))
3503 /* If one of the types wraps, use that one. */
3504 (if (!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_WRAPS (type))
3505 /* If all 3 captures are CONSTANT_CLASS_P, punt, as we might recurse
3506 forever if something doesn't simplify into a constant. */
3507 (if (!CONSTANT_CLASS_P (@0))
3508 (plus (view_convert @0) (minus! @1 (view_convert @2))))
3509 (if (!ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3510 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
3511 (view_convert (plus @0 (minus! (view_convert @1) @2)))
3512 (if (types_match (type, @0) && !TYPE_OVERFLOW_SANITIZED (type))
3513 (with { tree cst = const_binop (MINUS_EXPR, type, @1, @2); }
3514 (if (cst && !TREE_OVERFLOW (cst))
3515 (plus { cst; } @0)))))))
3517 /* ((T)(A)) + CST -> (T)(A + CST) */
3520 (plus (convert:s SSA_NAME@0) INTEGER_CST@1)
3521 (if (TREE_CODE (TREE_TYPE (@0)) == INTEGER_TYPE
3522 && TREE_CODE (type) == INTEGER_TYPE
3523 && TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (@0))
3524 && int_fits_type_p (@1, TREE_TYPE (@0)))
3525 /* Perform binary operation inside the cast if the constant fits
3526 and (A + CST)'s range does not overflow. */
3529 wi::overflow_type min_ovf = wi::OVF_OVERFLOW,
3530 max_ovf = wi::OVF_OVERFLOW;
3531 tree inner_type = TREE_TYPE (@0);
3534 = wide_int::from (wi::to_wide (@1), TYPE_PRECISION (inner_type),
3535 TYPE_SIGN (inner_type));
3538 if (get_global_range_query ()->range_of_expr (vr, @0)
3539 && !vr.varying_p () && !vr.undefined_p ())
3541 wide_int wmin0 = vr.lower_bound ();
3542 wide_int wmax0 = vr.upper_bound ();
3543 wi::add (wmin0, w1, TYPE_SIGN (inner_type), &min_ovf);
3544 wi::add (wmax0, w1, TYPE_SIGN (inner_type), &max_ovf);
3547 (if (min_ovf == wi::OVF_NONE && max_ovf == wi::OVF_NONE)
3548 (convert (plus @0 { wide_int_to_tree (TREE_TYPE (@0), w1); } )))
3552 /* ((T)(A + CST1)) + CST2 -> (T)(A) + (T)CST1 + CST2 */
3554 (for op (plus minus)
3556 (plus (convert:s (op:s @0 INTEGER_CST@1)) INTEGER_CST@2)
3557 (if (TREE_CODE (TREE_TYPE (@0)) == INTEGER_TYPE
3558 && TREE_CODE (type) == INTEGER_TYPE
3559 && TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (@0))
3560 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
3561 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0))
3562 && TYPE_OVERFLOW_WRAPS (type))
3563 (plus (convert @0) (op @2 (convert @1))))))
3566 /* (T)(A) +- (T)(B) -> (T)(A +- B) only when (A +- B) could be simplified
3567 to a simple value. */
3568 (for op (plus minus)
3570 (op (convert @0) (convert @1))
3571 (if (INTEGRAL_TYPE_P (type)
3572 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
3573 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))
3574 && types_match (TREE_TYPE (@0), TREE_TYPE (@1))
3575 && !TYPE_OVERFLOW_TRAPS (type)
3576 && !TYPE_OVERFLOW_SANITIZED (type))
3577 (convert (op! @0 @1)))))
3581 (plus:c (convert? (bit_not @0)) (convert? @0))
3582 (if (!TYPE_OVERFLOW_TRAPS (type))
3583 (convert { build_all_ones_cst (TREE_TYPE (@0)); })))
3587 (plus (convert? (bit_not @0)) integer_each_onep)
3588 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
3589 (negate (convert @0))))
3593 (minus (convert? (negate @0)) integer_each_onep)
3594 (if (!TYPE_OVERFLOW_TRAPS (type)
3595 && TREE_CODE (type) != COMPLEX_TYPE
3596 && tree_nop_conversion_p (type, TREE_TYPE (@0)))
3597 (bit_not (convert @0))))
3601 (minus integer_all_onesp @0)
3602 (if (TREE_CODE (type) != COMPLEX_TYPE)
3605 /* (T)(P + A) - (T)P -> (T) A */
3607 (minus (convert (plus:c @@0 @1))
3609 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
3610 /* For integer types, if A has a smaller type
3611 than T the result depends on the possible
3613 E.g. T=size_t, A=(unsigned)429497295, P>0.
3614 However, if an overflow in P + A would cause
3615 undefined behavior, we can assume that there
3617 || (INTEGRAL_TYPE_P (TREE_TYPE (@1))
3618 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))))
3621 (minus (convert (pointer_plus @@0 @1))
3623 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
3624 /* For pointer types, if the conversion of A to the
3625 final type requires a sign- or zero-extension,
3626 then we have to punt - it is not defined which
3628 || (POINTER_TYPE_P (TREE_TYPE (@0))
3629 && TREE_CODE (@1) == INTEGER_CST
3630 && tree_int_cst_sign_bit (@1) == 0))
3633 (pointer_diff (pointer_plus @@0 @1) @0)
3634 /* The second argument of pointer_plus must be interpreted as signed, and
3635 thus sign-extended if necessary. */
3636 (with { tree stype = signed_type_for (TREE_TYPE (@1)); }
3637 /* Use view_convert instead of convert here, as POINTER_PLUS_EXPR
3638 second arg is unsigned even when we need to consider it as signed,
3639 we don't want to diagnose overflow here. */
3640 (convert (view_convert:stype @1))))
3642 /* (T)P - (T)(P + A) -> -(T) A */
3644 (minus (convert? @0)
3645 (convert (plus:c @@0 @1)))
3646 (if (INTEGRAL_TYPE_P (type)
3647 && TYPE_OVERFLOW_UNDEFINED (type)
3648 /* For integer literals, using an intermediate unsigned type to avoid
3649 an overflow at run time is counter-productive because it introduces
3650 spurious overflows at compile time, in the form of TREE_OVERFLOW on
3651 the result, which may be problematic in GENERIC for some front-ends:
3652 (T)P - (T)(P + 4) -> (T)(-(U)4) -> (T)(4294967292) -> -4(OVF)
3653 so we use the direct path for them. */
3654 && TREE_CODE (@1) != INTEGER_CST
3655 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
3656 (with { tree utype = unsigned_type_for (type); }
3657 (convert (negate (convert:utype @1))))
3658 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
3659 /* For integer types, if A has a smaller type
3660 than T the result depends on the possible
3662 E.g. T=size_t, A=(unsigned)429497295, P>0.
3663 However, if an overflow in P + A would cause
3664 undefined behavior, we can assume that there
3666 || (INTEGRAL_TYPE_P (TREE_TYPE (@1))
3667 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))))
3668 (negate (convert @1)))))
3671 (convert (pointer_plus @@0 @1)))
3672 (if (INTEGRAL_TYPE_P (type)
3673 && TYPE_OVERFLOW_UNDEFINED (type)
3674 /* See above the rationale for this condition. */
3675 && TREE_CODE (@1) != INTEGER_CST
3676 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
3677 (with { tree utype = unsigned_type_for (type); }
3678 (convert (negate (convert:utype @1))))
3679 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
3680 /* For pointer types, if the conversion of A to the
3681 final type requires a sign- or zero-extension,
3682 then we have to punt - it is not defined which
3684 || (POINTER_TYPE_P (TREE_TYPE (@0))
3685 && TREE_CODE (@1) == INTEGER_CST
3686 && tree_int_cst_sign_bit (@1) == 0))
3687 (negate (convert @1)))))
3689 (pointer_diff @0 (pointer_plus @@0 @1))
3690 /* The second argument of pointer_plus must be interpreted as signed, and
3691 thus sign-extended if necessary. */
3692 (with { tree stype = signed_type_for (TREE_TYPE (@1)); }
3693 /* Use view_convert instead of convert here, as POINTER_PLUS_EXPR
3694 second arg is unsigned even when we need to consider it as signed,
3695 we don't want to diagnose overflow here. */
3696 (negate (convert (view_convert:stype @1)))))
3698 /* (T)(P + A) - (T)(P + B) -> (T)A - (T)B */
3700 (minus (convert (plus:c @@0 @1))
3701 (convert (plus:c @0 @2)))
3702 (if (INTEGRAL_TYPE_P (type)
3703 && TYPE_OVERFLOW_UNDEFINED (type)
3704 && element_precision (type) <= element_precision (TREE_TYPE (@1))
3705 && element_precision (type) <= element_precision (TREE_TYPE (@2)))
3706 (with { tree utype = unsigned_type_for (type); }
3707 (convert (minus (convert:utype @1) (convert:utype @2))))
3708 (if (((element_precision (type) <= element_precision (TREE_TYPE (@1)))
3709 == (element_precision (type) <= element_precision (TREE_TYPE (@2))))
3710 && (element_precision (type) <= element_precision (TREE_TYPE (@1))
3711 /* For integer types, if A has a smaller type
3712 than T the result depends on the possible
3714 E.g. T=size_t, A=(unsigned)429497295, P>0.
3715 However, if an overflow in P + A would cause
3716 undefined behavior, we can assume that there
3718 || (INTEGRAL_TYPE_P (TREE_TYPE (@1))
3719 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
3720 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))
3721 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@2)))))
3722 (minus (convert @1) (convert @2)))))
3724 (minus (convert (pointer_plus @@0 @1))
3725 (convert (pointer_plus @0 @2)))
3726 (if (INTEGRAL_TYPE_P (type)
3727 && TYPE_OVERFLOW_UNDEFINED (type)
3728 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
3729 (with { tree utype = unsigned_type_for (type); }
3730 (convert (minus (convert:utype @1) (convert:utype @2))))
3731 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
3732 /* For pointer types, if the conversion of A to the
3733 final type requires a sign- or zero-extension,
3734 then we have to punt - it is not defined which
3736 || (POINTER_TYPE_P (TREE_TYPE (@0))
3737 && TREE_CODE (@1) == INTEGER_CST
3738 && tree_int_cst_sign_bit (@1) == 0
3739 && TREE_CODE (@2) == INTEGER_CST
3740 && tree_int_cst_sign_bit (@2) == 0))
3741 (minus (convert @1) (convert @2)))))
3743 (pointer_diff (pointer_plus @0 @2) (pointer_plus @1 @2))
3744 (pointer_diff @0 @1))
3746 (pointer_diff (pointer_plus @@0 @1) (pointer_plus @0 @2))
3747 /* The second argument of pointer_plus must be interpreted as signed, and
3748 thus sign-extended if necessary. */
3749 (with { tree stype = signed_type_for (TREE_TYPE (@1)); }
3750 /* Use view_convert instead of convert here, as POINTER_PLUS_EXPR
3751 second arg is unsigned even when we need to consider it as signed,
3752 we don't want to diagnose overflow here. */
3753 (minus (convert (view_convert:stype @1))
3754 (convert (view_convert:stype @2)))))))
3756 /* (A * C) +- (B * C) -> (A+-B) * C and (A * C) +- A -> A * (C+-1).
3757 Modeled after fold_plusminus_mult_expr. */
3758 (if (!TYPE_SATURATING (type)
3759 && (!FLOAT_TYPE_P (type) || flag_associative_math))
3760 (for plusminus (plus minus)
3762 (plusminus (mult:cs@3 @0 @1) (mult:cs@4 @0 @2))
3763 (if (!ANY_INTEGRAL_TYPE_P (type)
3764 || TYPE_OVERFLOW_WRAPS (type)
3765 || (INTEGRAL_TYPE_P (type)
3766 && tree_expr_nonzero_p (@0)
3767 && expr_not_equal_to (@0, wi::minus_one (TYPE_PRECISION (type)))))
3768 (if (single_use (@3) || single_use (@4))
3769 /* If @1 +- @2 is constant require a hard single-use on either
3770 original operand (but not on both). */
3771 (mult (plusminus @1 @2) @0)
3772 (mult! (plusminus @1 @2) @0)
3774 /* We cannot generate constant 1 for fract. */
3775 (if (!ALL_FRACT_MODE_P (TYPE_MODE (type)))
3777 (plusminus @0 (mult:c@3 @0 @2))
3778 (if ((!ANY_INTEGRAL_TYPE_P (type)
3779 || TYPE_OVERFLOW_WRAPS (type)
3780 /* For @0 + @0*@2 this transformation would introduce UB
3781 (where there was none before) for @0 in [-1,0] and @2 max.
3782 For @0 - @0*@2 this transformation would introduce UB
3783 for @0 0 and @2 in [min,min+1] or @0 -1 and @2 min+1. */
3784 || (INTEGRAL_TYPE_P (type)
3785 && ((tree_expr_nonzero_p (@0)
3786 && expr_not_equal_to (@0,
3787 wi::minus_one (TYPE_PRECISION (type))))
3788 || (plusminus == PLUS_EXPR
3789 ? expr_not_equal_to (@2,
3790 wi::max_value (TYPE_PRECISION (type), SIGNED))
3791 /* Let's ignore the @0 -1 and @2 min case. */
3792 : (expr_not_equal_to (@2,
3793 wi::min_value (TYPE_PRECISION (type), SIGNED))
3794 && expr_not_equal_to (@2,
3795 wi::min_value (TYPE_PRECISION (type), SIGNED)
3798 (mult (plusminus { build_one_cst (type); } @2) @0)))
3800 (plusminus (mult:c@3 @0 @2) @0)
3801 (if ((!ANY_INTEGRAL_TYPE_P (type)
3802 || TYPE_OVERFLOW_WRAPS (type)
3803 /* For @0*@2 + @0 this transformation would introduce UB
3804 (where there was none before) for @0 in [-1,0] and @2 max.
3805 For @0*@2 - @0 this transformation would introduce UB
3806 for @0 0 and @2 min. */
3807 || (INTEGRAL_TYPE_P (type)
3808 && ((tree_expr_nonzero_p (@0)
3809 && (plusminus == MINUS_EXPR
3810 || expr_not_equal_to (@0,
3811 wi::minus_one (TYPE_PRECISION (type)))))
3812 || expr_not_equal_to (@2,
3813 (plusminus == PLUS_EXPR
3814 ? wi::max_value (TYPE_PRECISION (type), SIGNED)
3815 : wi::min_value (TYPE_PRECISION (type), SIGNED))))))
3817 (mult (plusminus @2 { build_one_cst (type); }) @0))))))
3820 /* Canonicalize X + (X << C) into X * (1 + (1 << C)) and
3821 (X << C1) + (X << C2) into X * ((1 << C1) + (1 << C2)). */
3823 (plus:c @0 (lshift:s @0 INTEGER_CST@1))
3824 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3825 && tree_fits_uhwi_p (@1)
3826 && tree_to_uhwi (@1) < element_precision (type)
3827 && (INTEGRAL_TYPE_P (TREE_TYPE (@0))
3828 || optab_handler (smul_optab,
3829 TYPE_MODE (type)) != CODE_FOR_nothing))
3830 (with { tree t = type;
3831 if (!TYPE_OVERFLOW_WRAPS (t)) t = unsigned_type_for (t);
3832 wide_int w = wi::set_bit_in_zero (tree_to_uhwi (@1),
3833 element_precision (type));
3835 tree cst = wide_int_to_tree (VECTOR_TYPE_P (t) ? TREE_TYPE (t)
3837 cst = build_uniform_cst (t, cst); }
3838 (convert (mult (convert:t @0) { cst; })))))
3840 (plus (lshift:s @0 INTEGER_CST@1) (lshift:s @0 INTEGER_CST@2))
3841 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3842 && tree_fits_uhwi_p (@1)
3843 && tree_to_uhwi (@1) < element_precision (type)
3844 && tree_fits_uhwi_p (@2)
3845 && tree_to_uhwi (@2) < element_precision (type)
3846 && (INTEGRAL_TYPE_P (TREE_TYPE (@0))
3847 || optab_handler (smul_optab,
3848 TYPE_MODE (type)) != CODE_FOR_nothing))
3849 (with { tree t = type;
3850 if (!TYPE_OVERFLOW_WRAPS (t)) t = unsigned_type_for (t);
3851 unsigned int prec = element_precision (type);
3852 wide_int w = wi::set_bit_in_zero (tree_to_uhwi (@1), prec);
3853 w += wi::set_bit_in_zero (tree_to_uhwi (@2), prec);
3854 tree cst = wide_int_to_tree (VECTOR_TYPE_P (t) ? TREE_TYPE (t)
3856 cst = build_uniform_cst (t, cst); }
3857 (convert (mult (convert:t @0) { cst; })))))
3860 /* Canonicalize (X*C1)|(X*C2) and (X*C1)^(X*C2) to (C1+C2)*X when
3861 tree_nonzero_bits allows IOR and XOR to be treated like PLUS.
3862 Likewise, handle (X<<C3) and X as legitimate variants of X*C. */
3863 (for op (bit_ior bit_xor)
3865 (op (mult:s@0 @1 INTEGER_CST@2)
3866 (mult:s@3 @1 INTEGER_CST@4))
3867 (if (INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_WRAPS (type)
3868 && (tree_nonzero_bits (@0) & tree_nonzero_bits (@3)) == 0)
3870 { wide_int_to_tree (type, wi::to_wide (@2) + wi::to_wide (@4)); })))
3872 (op:c (mult:s@0 @1 INTEGER_CST@2)
3873 (lshift:s@3 @1 INTEGER_CST@4))
3874 (if (INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_WRAPS (type)
3875 && tree_int_cst_sgn (@4) > 0
3876 && (tree_nonzero_bits (@0) & tree_nonzero_bits (@3)) == 0)
3877 (with { wide_int wone = wi::one (TYPE_PRECISION (type));
3878 wide_int c = wi::add (wi::to_wide (@2),
3879 wi::lshift (wone, wi::to_wide (@4))); }
3880 (mult @1 { wide_int_to_tree (type, c); }))))
3882 (op:c (mult:s@0 @1 INTEGER_CST@2)
3884 (if (INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_WRAPS (type)
3885 && (tree_nonzero_bits (@0) & tree_nonzero_bits (@1)) == 0)
3887 { wide_int_to_tree (type,
3888 wi::add (wi::to_wide (@2), 1)); })))
3890 (op (lshift:s@0 @1 INTEGER_CST@2)
3891 (lshift:s@3 @1 INTEGER_CST@4))
3892 (if (INTEGRAL_TYPE_P (type)
3893 && tree_int_cst_sgn (@2) > 0
3894 && tree_int_cst_sgn (@4) > 0
3895 && (tree_nonzero_bits (@0) & tree_nonzero_bits (@3)) == 0)
3896 (with { tree t = type;
3897 if (!TYPE_OVERFLOW_WRAPS (t))
3898 t = unsigned_type_for (t);
3899 wide_int wone = wi::one (TYPE_PRECISION (t));
3900 wide_int c = wi::add (wi::lshift (wone, wi::to_wide (@2)),
3901 wi::lshift (wone, wi::to_wide (@4))); }
3902 (convert (mult:t (convert:t @1) { wide_int_to_tree (t,c); })))))
3904 (op:c (lshift:s@0 @1 INTEGER_CST@2)
3906 (if (INTEGRAL_TYPE_P (type)
3907 && tree_int_cst_sgn (@2) > 0
3908 && (tree_nonzero_bits (@0) & tree_nonzero_bits (@1)) == 0)
3909 (with { tree t = type;
3910 if (!TYPE_OVERFLOW_WRAPS (t))
3911 t = unsigned_type_for (t);
3912 wide_int wone = wi::one (TYPE_PRECISION (t));
3913 wide_int c = wi::add (wi::lshift (wone, wi::to_wide (@2)), wone); }
3914 (convert (mult:t (convert:t @1) { wide_int_to_tree (t, c); }))))))
3916 /* Simplifications of MIN_EXPR, MAX_EXPR, fmin() and fmax(). */
3918 (for minmax (min max)
3922 /* max(max(x,y),x) -> max(x,y) */
3924 (minmax:c (minmax:c@2 @0 @1) @0)
3926 /* For fmin() and fmax(), skip folding when both are sNaN. */
3927 (for minmax (FMIN_ALL FMAX_ALL)
3930 (if (!tree_expr_maybe_signaling_nan_p (@0))
3932 /* min(max(x,y),y) -> y. */
3934 (min:c (max:c @0 @1) @1)
3936 /* max(min(x,y),y) -> y. */
3938 (max:c (min:c @0 @1) @1)
3940 /* max(a,-a) -> abs(a). */
3942 (max:c @0 (negate @0))
3943 (if (TREE_CODE (type) != COMPLEX_TYPE
3944 && (! ANY_INTEGRAL_TYPE_P (type)
3945 || TYPE_OVERFLOW_UNDEFINED (type)))
3947 /* min(a,-a) -> -abs(a). */
3949 (min:c @0 (negate @0))
3950 (if (TREE_CODE (type) != COMPLEX_TYPE
3951 && (! ANY_INTEGRAL_TYPE_P (type)
3952 || TYPE_OVERFLOW_UNDEFINED (type)))
3957 (if (INTEGRAL_TYPE_P (type)
3958 && TYPE_MIN_VALUE (type)
3959 && operand_equal_p (@1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
3961 (if (INTEGRAL_TYPE_P (type)
3962 && TYPE_MAX_VALUE (type)
3963 && operand_equal_p (@1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
3968 (if (INTEGRAL_TYPE_P (type)
3969 && TYPE_MAX_VALUE (type)
3970 && operand_equal_p (@1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
3972 (if (INTEGRAL_TYPE_P (type)
3973 && TYPE_MIN_VALUE (type)
3974 && operand_equal_p (@1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
3977 /* max (a, a + CST) -> a + CST where CST is positive. */
3978 /* max (a, a + CST) -> a where CST is negative. */
3980 (max:c @0 (plus@2 @0 INTEGER_CST@1))
3981 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
3982 (if (tree_int_cst_sgn (@1) > 0)
3986 /* min (a, a + CST) -> a where CST is positive. */
3987 /* min (a, a + CST) -> a + CST where CST is negative. */
3989 (min:c @0 (plus@2 @0 INTEGER_CST@1))
3990 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
3991 (if (tree_int_cst_sgn (@1) > 0)
3995 /* Simplify min (&var[off0], &var[off1]) etc. depending on whether
3996 the addresses are known to be less, equal or greater. */
3997 (for minmax (min max)
4000 (minmax (convert1?@2 addr@0) (convert2?@3 addr@1))
4003 poly_int64 off0, off1;
4005 int equal = address_compare (cmp, TREE_TYPE (@2), @0, @1, base0, base1,
4006 off0, off1, GENERIC);
4009 (if (minmax == MIN_EXPR)
4010 (if (known_le (off0, off1))
4012 (if (known_gt (off0, off1))
4014 (if (known_ge (off0, off1))
4016 (if (known_lt (off0, off1))
4019 /* (convert (minmax ((convert (x) c)))) -> minmax (x c) if x is promoted
4020 and the outer convert demotes the expression back to x's type. */
4021 (for minmax (min max)
4023 (convert (minmax@0 (convert @1) INTEGER_CST@2))
4024 (if (INTEGRAL_TYPE_P (type)
4025 && types_match (@1, type) && int_fits_type_p (@2, type)
4026 && TYPE_SIGN (TREE_TYPE (@0)) == TYPE_SIGN (type)
4027 && TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type))
4028 (minmax @1 (convert @2)))))
4030 (for minmax (FMIN_ALL FMAX_ALL)
4031 /* If either argument is NaN and other one is not sNaN, return the other
4032 one. Avoid the transformation if we get (and honor) a signalling NaN. */
4034 (minmax:c @0 REAL_CST@1)
4035 (if (real_isnan (TREE_REAL_CST_PTR (@1))
4036 && (!HONOR_SNANS (@1) || !TREE_REAL_CST (@1).signalling)
4037 && !tree_expr_maybe_signaling_nan_p (@0))
4039 /* Convert fmin/fmax to MIN_EXPR/MAX_EXPR. C99 requires these
4040 functions to return the numeric arg if the other one is NaN.
4041 MIN and MAX don't honor that, so only transform if -ffinite-math-only
4042 is set. C99 doesn't require -0.0 to be handled, so we don't have to
4043 worry about it either. */
4044 (if (flag_finite_math_only)
4051 /* min (-A, -B) -> -max (A, B) */
4052 (for minmax (min max FMIN_ALL FMAX_ALL)
4053 maxmin (max min FMAX_ALL FMIN_ALL)
4055 (minmax (negate:s@2 @0) (negate:s@3 @1))
4056 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
4057 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
4058 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
4059 (negate (maxmin @0 @1)))))
4060 /* MIN (~X, ~Y) -> ~MAX (X, Y)
4061 MAX (~X, ~Y) -> ~MIN (X, Y) */
4062 (for minmax (min max)
4065 (minmax (bit_not:s@2 @0) (bit_not:s@3 @1))
4066 (bit_not (maxmin @0 @1)))
4067 /* ~MAX(~X, Y) --> MIN(X, ~Y) */
4068 /* ~MIN(~X, Y) --> MAX(X, ~Y) */
4070 (bit_not (minmax:cs (bit_not @0) @1))
4071 (maxmin @0 (bit_not @1))))
4073 /* MIN (X, Y) == X -> X <= Y */
4074 /* MIN (X, Y) < X -> X > Y */
4075 /* MIN (X, Y) >= X -> X <= Y */
4076 (for minmax (min min min min max max max max)
4077 cmp (eq ne lt ge eq ne gt le )
4078 out (le gt gt le ge lt lt ge )
4080 (cmp:c (minmax:c @0 @1) @0)
4081 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0)))
4083 /* MIN (X, 5) == 0 -> X == 0
4084 MIN (X, 5) == 7 -> false */
4087 (cmp (min @0 INTEGER_CST@1) INTEGER_CST@2)
4088 (if (wi::lt_p (wi::to_wide (@1), wi::to_wide (@2),
4089 TYPE_SIGN (TREE_TYPE (@0))))
4090 { constant_boolean_node (cmp == NE_EXPR, type); }
4091 (if (wi::gt_p (wi::to_wide (@1), wi::to_wide (@2),
4092 TYPE_SIGN (TREE_TYPE (@0))))
4096 (cmp (max @0 INTEGER_CST@1) INTEGER_CST@2)
4097 (if (wi::gt_p (wi::to_wide (@1), wi::to_wide (@2),
4098 TYPE_SIGN (TREE_TYPE (@0))))
4099 { constant_boolean_node (cmp == NE_EXPR, type); }
4100 (if (wi::lt_p (wi::to_wide (@1), wi::to_wide (@2),
4101 TYPE_SIGN (TREE_TYPE (@0))))
4104 /* X <= MAX(X, Y) -> true
4105 X > MAX(X, Y) -> false
4106 X >= MIN(X, Y) -> true
4107 X < MIN(X, Y) -> false */
4108 (for minmax (min min max max )
4111 (cmp:c @0 (minmax:c @0 @1))
4112 { constant_boolean_node (cmp == GE_EXPR || cmp == LE_EXPR, type); } ))
4114 /* MIN (X, C1) < C2 -> X < C2 || C1 < C2 */
4115 (for minmax (min min max max min min max max )
4116 cmp (lt le gt ge gt ge lt le )
4117 comb (bit_ior bit_ior bit_ior bit_ior bit_and bit_and bit_and bit_and)
4119 (cmp (minmax @0 INTEGER_CST@1) INTEGER_CST@2)
4120 (comb (cmp @0 @2) (cmp @1 @2))))
4122 /* Undo fancy ways of writing max/min or other ?: expressions, like
4123 a - ((a - b) & -(a < b)) and a - (a - b) * (a < b) into (a < b) ? b : a.
4124 People normally use ?: and that is what we actually try to optimize. */
4125 /* Transform A + (B-A)*cmp into cmp ? B : A. */
4127 (plus:c @0 (mult:c (minus @1 @0) zero_one_valued_p@2))
4128 (if (INTEGRAL_TYPE_P (type)
4129 && (GIMPLE || !TREE_SIDE_EFFECTS (@1)))
4130 (cond (convert:boolean_type_node @2) @1 @0)))
4131 /* Transform A - (A-B)*cmp into cmp ? B : A. */
4133 (minus @0 (mult:c (minus @0 @1) zero_one_valued_p@2))
4134 (if (INTEGRAL_TYPE_P (type)
4135 && (GIMPLE || !TREE_SIDE_EFFECTS (@1)))
4136 (cond (convert:boolean_type_node @2) @1 @0)))
4137 /* Transform A ^ (A^B)*cmp into cmp ? B : A. */
4139 (bit_xor:c @0 (mult:c (bit_xor:c @0 @1) zero_one_valued_p@2))
4140 (if (INTEGRAL_TYPE_P (type)
4141 && (GIMPLE || !TREE_SIDE_EFFECTS (@1)))
4142 (cond (convert:boolean_type_node @2) @1 @0)))
4144 /* (x <= 0 ? -x : 0) -> max(-x, 0). */
4146 (cond (le @0 integer_zerop@1) (negate@2 @0) integer_zerop@1)
4149 /* (zero_one == 0) ? y : z <op> y -> ((typeof(y))zero_one * z) <op> y */
4150 (for op (bit_xor bit_ior plus)
4152 (cond (eq zero_one_valued_p@0
4156 (if (INTEGRAL_TYPE_P (type)
4157 && TYPE_PRECISION (type) > 1
4158 && (INTEGRAL_TYPE_P (TREE_TYPE (@0))))
4159 (op (mult (convert:type @0) @2) @1))))
4161 /* (zero_one != 0) ? z <op> y : y -> ((typeof(y))zero_one * z) <op> y */
4162 (for op (bit_xor bit_ior plus)
4164 (cond (ne zero_one_valued_p@0
4168 (if (INTEGRAL_TYPE_P (type)
4169 && TYPE_PRECISION (type) > 1
4170 && (INTEGRAL_TYPE_P (TREE_TYPE (@0))))
4171 (op (mult (convert:type @0) @2) @1))))
4173 /* ?: Value replacement. */
4174 /* a == 0 ? b : b + a -> b + a */
4175 (for op (plus bit_ior bit_xor)
4177 (cond (eq @0 integer_zerop) @1 (op:c@2 @1 @0))
4179 /* a == 0 ? b : b - a -> b - a */
4180 /* a == 0 ? b : b ptr+ a -> b ptr+ a */
4181 /* a == 0 ? b : b shift/rotate a -> b shift/rotate a */
4182 (for op (lrotate rrotate lshift rshift minus pointer_plus)
4184 (cond (eq @0 integer_zerop) @1 (op@2 @1 @0))
4187 /* a == 1 ? b : b / a -> b / a */
4188 (for op (trunc_div ceil_div floor_div round_div exact_div)
4190 (cond (eq @0 integer_onep) @1 (op@2 @1 @0))
4193 /* a == 1 ? b : a * b -> a * b */
4196 (cond (eq @0 integer_onep) @1 (op:c@2 @1 @0))
4199 /* a == -1 ? b : a & b -> a & b */
4202 (cond (eq @0 integer_all_onesp) @1 (op:c@2 @1 @0))
4205 /* Simplifications of shift and rotates. */
4207 (for rotate (lrotate rrotate)
4209 (rotate integer_all_onesp@0 @1)
4212 /* Optimize -1 >> x for arithmetic right shifts. */
4214 (rshift integer_all_onesp@0 @1)
4215 (if (!TYPE_UNSIGNED (type))
4218 /* Optimize (x >> c) << c into x & (-1<<c). */
4220 (lshift (nop_convert? (rshift @0 INTEGER_CST@1)) @1)
4221 (if (wi::ltu_p (wi::to_wide (@1), element_precision (type)))
4222 /* It doesn't matter if the right shift is arithmetic or logical. */
4223 (bit_and (view_convert @0) (lshift { build_minus_one_cst (type); } @1))))
4226 (lshift (convert (convert@2 (rshift @0 INTEGER_CST@1))) @1)
4227 (if (wi::ltu_p (wi::to_wide (@1), element_precision (type))
4228 /* Allow intermediate conversion to integral type with whatever sign, as
4229 long as the low TYPE_PRECISION (type)
4230 - TYPE_PRECISION (TREE_TYPE (@2)) bits are preserved. */
4231 && INTEGRAL_TYPE_P (type)
4232 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
4233 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
4234 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (@0))
4235 && (TYPE_PRECISION (TREE_TYPE (@2)) >= TYPE_PRECISION (type)
4236 || wi::geu_p (wi::to_wide (@1),
4237 TYPE_PRECISION (type)
4238 - TYPE_PRECISION (TREE_TYPE (@2)))))
4239 (bit_and (convert @0) (lshift { build_minus_one_cst (type); } @1))))
4241 /* For (x << c) >> c, optimize into x & ((unsigned)-1 >> c) for
4242 unsigned x OR truncate into the precision(type) - c lowest bits
4243 of signed x (if they have mode precision or a precision of 1). */
4245 (rshift (nop_convert? (lshift @0 INTEGER_CST@1)) @@1)
4246 (if (wi::ltu_p (wi::to_wide (@1), element_precision (type)))
4247 (if (TYPE_UNSIGNED (type))
4248 (bit_and (convert @0) (rshift { build_minus_one_cst (type); } @1))
4249 (if (INTEGRAL_TYPE_P (type))
4251 int width = element_precision (type) - tree_to_uhwi (@1);
4252 tree stype = NULL_TREE;
4253 if (width <= MAX_FIXED_MODE_SIZE)
4254 stype = build_nonstandard_integer_type (width, 0);
4256 (if (stype && (width == 1 || type_has_mode_precision_p (stype)))
4257 (convert (convert:stype @0))))))))
4259 /* Optimize x >> x into 0 */
4262 { build_zero_cst (type); })
4264 (for shiftrotate (lrotate rrotate lshift rshift)
4266 (shiftrotate @0 integer_zerop)
4269 (shiftrotate integer_zerop@0 @1)
4271 /* Prefer vector1 << scalar to vector1 << vector2
4272 if vector2 is uniform. */
4273 (for vec (VECTOR_CST CONSTRUCTOR)
4275 (shiftrotate @0 vec@1)
4276 (with { tree tem = uniform_vector_p (@1); }
4278 (shiftrotate @0 { tem; }))))))
4280 /* Simplify X << Y where Y's low width bits are 0 to X, as only valid
4281 Y is 0. Similarly for X >> Y. */
4283 (for shift (lshift rshift)
4285 (shift @0 SSA_NAME@1)
4286 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1)))
4288 int width = ceil_log2 (element_precision (TREE_TYPE (@0)));
4289 int prec = TYPE_PRECISION (TREE_TYPE (@1));
4291 (if ((get_nonzero_bits (@1) & wi::mask (width, false, prec)) == 0)
4295 /* Rewrite an LROTATE_EXPR by a constant into an
4296 RROTATE_EXPR by a new constant. */
4298 (lrotate @0 INTEGER_CST@1)
4299 (rrotate @0 { const_binop (MINUS_EXPR, TREE_TYPE (@1),
4300 build_int_cst (TREE_TYPE (@1),
4301 element_precision (type)), @1); }))
4303 /* Turn (a OP c1) OP c2 into a OP (c1+c2). */
4304 (for op (lrotate rrotate rshift lshift)
4306 (op (op @0 INTEGER_CST@1) INTEGER_CST@2)
4307 (with { unsigned int prec = element_precision (type); }
4308 (if (wi::ge_p (wi::to_wide (@1), 0, TYPE_SIGN (TREE_TYPE (@1)))
4309 && wi::lt_p (wi::to_wide (@1), prec, TYPE_SIGN (TREE_TYPE (@1)))
4310 && wi::ge_p (wi::to_wide (@2), 0, TYPE_SIGN (TREE_TYPE (@2)))
4311 && wi::lt_p (wi::to_wide (@2), prec, TYPE_SIGN (TREE_TYPE (@2))))
4312 (with { unsigned int low = (tree_to_uhwi (@1)
4313 + tree_to_uhwi (@2)); }
4314 /* Deal with a OP (c1 + c2) being undefined but (a OP c1) OP c2
4315 being well defined. */
4317 (if (op == LROTATE_EXPR || op == RROTATE_EXPR)
4318 (op @0 { build_int_cst (TREE_TYPE (@1), low % prec); })
4319 (if (TYPE_UNSIGNED (type) || op == LSHIFT_EXPR)
4320 { build_zero_cst (type); }
4321 (op @0 { build_int_cst (TREE_TYPE (@1), prec - 1); })))
4322 (op @0 { build_int_cst (TREE_TYPE (@1), low); })))))))
4325 /* Simplify (CST << x) & 1 to 0 if CST is even or to x == 0 if it is odd. */
4327 (bit_and (lshift INTEGER_CST@1 @0) integer_onep)
4328 (if ((wi::to_wide (@1) & 1) != 0)
4329 (convert (eq:boolean_type_node @0 { build_zero_cst (TREE_TYPE (@0)); }))
4330 { build_zero_cst (type); }))
4332 /* Simplify ((C << x) & D) != 0 where C and D are power of two constants,
4333 either to false if D is smaller (unsigned comparison) than C, or to
4334 x == log2 (D) - log2 (C). Similarly for right shifts.
4335 Note for `(1 >> x)`, the & 1 has been removed so matching that seperately. */
4339 (cmp (bit_and (lshift integer_pow2p@1 @0) integer_pow2p@2) integer_zerop)
4340 (with { int c1 = wi::clz (wi::to_wide (@1));
4341 int c2 = wi::clz (wi::to_wide (@2)); }
4343 { constant_boolean_node (cmp == NE_EXPR ? false : true, type); }
4344 (icmp @0 { build_int_cst (TREE_TYPE (@0), c1 - c2); }))))
4346 (cmp (bit_and (rshift integer_pow2p@1 @0) integer_pow2p@2) integer_zerop)
4347 (if (tree_int_cst_sgn (@1) > 0)
4348 (with { int c1 = wi::clz (wi::to_wide (@1));
4349 int c2 = wi::clz (wi::to_wide (@2)); }
4351 { constant_boolean_node (cmp == NE_EXPR ? false : true, type); }
4352 (icmp @0 { build_int_cst (TREE_TYPE (@0), c2 - c1); })))))
4353 /* `(1 >> X) != 0` -> `X == 0` */
4354 /* `(1 >> X) == 0` -> `X != 0` */
4356 (cmp (rshift integer_onep@1 @0) integer_zerop)
4357 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1)))
4358 (icmp @0 { build_zero_cst (TREE_TYPE (@0)); }))))
4360 /* (CST1 << A) == CST2 -> A == ctz (CST2) - ctz (CST1)
4361 (CST1 << A) != CST2 -> A != ctz (CST2) - ctz (CST1)
4365 (cmp (lshift INTEGER_CST@0 @1) INTEGER_CST@2)
4366 (with { int cand = wi::ctz (wi::to_wide (@2)) - wi::ctz (wi::to_wide (@0)); }
4368 || (!integer_zerop (@2)
4369 && wi::lshift (wi::to_wide (@0), cand) != wi::to_wide (@2)))
4370 { constant_boolean_node (cmp == NE_EXPR, type); }
4371 (if (!integer_zerop (@2)
4372 && wi::lshift (wi::to_wide (@0), cand) == wi::to_wide (@2))
4373 (cmp @1 { build_int_cst (TREE_TYPE (@1), cand); }))))))
4375 /* Fold ((X << C1) & C2) cmp C3 into (X & (C2 >> C1)) cmp (C3 >> C1)
4376 ((X >> C1) & C2) cmp C3 into (X & (C2 << C1)) cmp (C3 << C1). */
4379 (cmp (bit_and:s (lshift:s @0 INTEGER_CST@1) INTEGER_CST@2) INTEGER_CST@3)
4380 (if (tree_fits_shwi_p (@1)
4381 && tree_to_shwi (@1) > 0
4382 && tree_to_shwi (@1) < TYPE_PRECISION (TREE_TYPE (@0)))
4383 (if (tree_to_shwi (@1) > wi::ctz (wi::to_wide (@3)))
4384 { constant_boolean_node (cmp == NE_EXPR, type); }
4385 (with { wide_int c1 = wi::to_wide (@1);
4386 wide_int c2 = wi::lrshift (wi::to_wide (@2), c1);
4387 wide_int c3 = wi::lrshift (wi::to_wide (@3), c1); }
4388 (cmp (bit_and @0 { wide_int_to_tree (TREE_TYPE (@0), c2); })
4389 { wide_int_to_tree (TREE_TYPE (@0), c3); })))))
4391 (cmp (bit_and:s (rshift:s @0 INTEGER_CST@1) INTEGER_CST@2) INTEGER_CST@3)
4392 (if (tree_fits_shwi_p (@1)
4393 && tree_to_shwi (@1) > 0
4394 && tree_to_shwi (@1) < TYPE_PRECISION (TREE_TYPE (@0)))
4395 (with { tree t0 = TREE_TYPE (@0);
4396 unsigned int prec = TYPE_PRECISION (t0);
4397 wide_int c1 = wi::to_wide (@1);
4398 wide_int c2 = wi::to_wide (@2);
4399 wide_int c3 = wi::to_wide (@3);
4400 wide_int sb = wi::set_bit_in_zero (prec - 1, prec); }
4401 (if ((c2 & c3) != c3)
4402 { constant_boolean_node (cmp == NE_EXPR, type); }
4403 (if (TYPE_UNSIGNED (t0))
4404 (if ((c3 & wi::arshift (sb, c1 - 1)) != 0)
4405 { constant_boolean_node (cmp == NE_EXPR, type); }
4406 (cmp (bit_and @0 { wide_int_to_tree (t0, c2 << c1); })
4407 { wide_int_to_tree (t0, c3 << c1); }))
4408 (with { wide_int smask = wi::arshift (sb, c1); }
4410 (if ((c2 & smask) == 0)
4411 (cmp (bit_and @0 { wide_int_to_tree (t0, c2 << c1); })
4412 { wide_int_to_tree (t0, c3 << c1); }))
4413 (if ((c3 & smask) == 0)
4414 (cmp (bit_and @0 { wide_int_to_tree (t0, (c2 << c1) | sb); })
4415 { wide_int_to_tree (t0, c3 << c1); }))
4416 (if ((c2 & smask) != (c3 & smask))
4417 { constant_boolean_node (cmp == NE_EXPR, type); })
4418 (cmp (bit_and @0 { wide_int_to_tree (t0, (c2 << c1) | sb); })
4419 { wide_int_to_tree (t0, (c3 << c1) | sb); })))))))))
4421 /* Fold (X << C1) & C2 into (X << C1) & (C2 | ((1 << C1) - 1))
4422 (X >> C1) & C2 into (X >> C1) & (C2 | ~((type) -1 >> C1))
4423 if the new mask might be further optimized. */
4424 (for shift (lshift rshift)
4426 (bit_and (convert?:s@4 (shift:s@5 (convert1?@3 @0) INTEGER_CST@1))
4428 (if (tree_nop_conversion_p (TREE_TYPE (@4), TREE_TYPE (@5))
4429 && TYPE_PRECISION (type) <= HOST_BITS_PER_WIDE_INT
4430 && tree_fits_uhwi_p (@1)
4431 && tree_to_uhwi (@1) > 0
4432 && tree_to_uhwi (@1) < TYPE_PRECISION (type))
4435 unsigned int shiftc = tree_to_uhwi (@1);
4436 unsigned HOST_WIDE_INT mask = TREE_INT_CST_LOW (@2);
4437 unsigned HOST_WIDE_INT newmask, zerobits = 0;
4438 tree shift_type = TREE_TYPE (@3);
4441 if (shift == LSHIFT_EXPR)
4442 zerobits = ((HOST_WIDE_INT_1U << shiftc) - 1);
4443 else if (shift == RSHIFT_EXPR
4444 && type_has_mode_precision_p (shift_type))
4446 prec = TYPE_PRECISION (TREE_TYPE (@3));
4448 /* See if more bits can be proven as zero because of
4451 && TYPE_UNSIGNED (TREE_TYPE (@0)))
4453 tree inner_type = TREE_TYPE (@0);
4454 if (type_has_mode_precision_p (inner_type)
4455 && TYPE_PRECISION (inner_type) < prec)
4457 prec = TYPE_PRECISION (inner_type);
4458 /* See if we can shorten the right shift. */
4460 shift_type = inner_type;
4461 /* Otherwise X >> C1 is all zeros, so we'll optimize
4462 it into (X, 0) later on by making sure zerobits
4466 zerobits = HOST_WIDE_INT_M1U;
4469 zerobits >>= HOST_BITS_PER_WIDE_INT - shiftc;
4470 zerobits <<= prec - shiftc;
4472 /* For arithmetic shift if sign bit could be set, zerobits
4473 can contain actually sign bits, so no transformation is
4474 possible, unless MASK masks them all away. In that
4475 case the shift needs to be converted into logical shift. */
4476 if (!TYPE_UNSIGNED (TREE_TYPE (@3))
4477 && prec == TYPE_PRECISION (TREE_TYPE (@3)))
4479 if ((mask & zerobits) == 0)
4480 shift_type = unsigned_type_for (TREE_TYPE (@3));
4486 /* ((X << 16) & 0xff00) is (X, 0). */
4487 (if ((mask & zerobits) == mask)
4488 { build_int_cst (type, 0); }
4489 (with { newmask = mask | zerobits; }
4490 (if (newmask != mask && (newmask & (newmask + 1)) == 0)
4493 /* Only do the transformation if NEWMASK is some integer
4495 for (prec = BITS_PER_UNIT;
4496 prec < HOST_BITS_PER_WIDE_INT; prec <<= 1)
4497 if (newmask == (HOST_WIDE_INT_1U << prec) - 1)
4500 (if (prec < HOST_BITS_PER_WIDE_INT
4501 || newmask == HOST_WIDE_INT_M1U)
4503 { tree newmaskt = build_int_cst_type (TREE_TYPE (@2), newmask); }
4504 (if (!tree_int_cst_equal (newmaskt, @2))
4505 (if (shift_type != TREE_TYPE (@3))
4506 (bit_and (convert (shift:shift_type (convert @3) @1)) { newmaskt; })
4507 (bit_and @4 { newmaskt; })))))))))))))
4509 /* ((1 << n) & M) != 0 -> n == log2 (M) */
4515 (nop_convert? (lshift integer_onep @0)) integer_pow2p@1) integer_zerop)
4516 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)))
4517 (icmp @0 { wide_int_to_tree (TREE_TYPE (@0),
4518 wi::exact_log2 (wi::to_wide (@1))); }))))
4520 /* Fold (X {&,^,|} C2) << C1 into (X << C1) {&,^,|} (C2 << C1)
4521 (X {&,^,|} C2) >> C1 into (X >> C1) & (C2 >> C1). */
4522 (for shift (lshift rshift)
4523 (for bit_op (bit_and bit_xor bit_ior)
4525 (shift (convert?:s (bit_op:s @0 INTEGER_CST@2)) INTEGER_CST@1)
4526 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
4527 (with { tree mask = int_const_binop (shift, fold_convert (type, @2), @1); }
4529 (bit_op (shift (convert @0) @1) { mask; })))))))
4531 /* ~(~X >> Y) -> X >> Y (for arithmetic shift). */
4533 (bit_not (convert1?:s (rshift:s (convert2?@0 (bit_not @1)) @2)))
4534 (if (!TYPE_UNSIGNED (TREE_TYPE (@0))
4535 && (element_precision (TREE_TYPE (@0))
4536 <= element_precision (TREE_TYPE (@1))
4537 || !TYPE_UNSIGNED (TREE_TYPE (@1))))
4539 { tree shift_type = TREE_TYPE (@0); }
4540 (convert (rshift (convert:shift_type @1) @2)))))
4542 /* ~(~X >>r Y) -> X >>r Y
4543 ~(~X <<r Y) -> X <<r Y */
4544 (for rotate (lrotate rrotate)
4546 (bit_not (convert1?:s (rotate:s (convert2?@0 (bit_not @1)) @2)))
4547 (if ((element_precision (TREE_TYPE (@0))
4548 <= element_precision (TREE_TYPE (@1))
4549 || !TYPE_UNSIGNED (TREE_TYPE (@1)))
4550 && (element_precision (type) <= element_precision (TREE_TYPE (@0))
4551 || !TYPE_UNSIGNED (TREE_TYPE (@0))))
4553 { tree rotate_type = TREE_TYPE (@0); }
4554 (convert (rotate (convert:rotate_type @1) @2))))))
4557 (for rotate (lrotate rrotate)
4558 invrot (rrotate lrotate)
4559 /* (X >>r Y) cmp (Z >>r Y) may simplify to X cmp Y. */
4561 (cmp (rotate @1 @0) (rotate @2 @0))
4563 /* (X >>r C1) cmp C2 may simplify to X cmp C3. */
4565 (cmp (rotate @0 INTEGER_CST@1) INTEGER_CST@2)
4566 (cmp @0 { const_binop (invrot, TREE_TYPE (@0), @2, @1); }))
4567 /* (X >>r Y) cmp C where C is 0 or ~0, may simplify to X cmp C. */
4569 (cmp (rotate @0 @1) INTEGER_CST@2)
4570 (if (integer_zerop (@2) || integer_all_onesp (@2))
4573 /* Narrow a lshift by constant. */
4575 (convert (lshift:s@0 @1 INTEGER_CST@2))
4576 (if (INTEGRAL_TYPE_P (type)
4577 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
4578 && !integer_zerop (@2)
4579 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0)))
4580 (if (TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (@0))
4581 || wi::ltu_p (wi::to_wide (@2), TYPE_PRECISION (type)))
4582 (lshift (convert @1) @2)
4583 (if (wi::ltu_p (wi::to_wide (@2), TYPE_PRECISION (TREE_TYPE (@0))))
4584 { build_zero_cst (type); }))))
4586 /* Simplifications of conversions. */
4588 /* Basic strip-useless-type-conversions / strip_nops. */
4589 (for cvt (convert view_convert float fix_trunc)
4592 (if ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@0)))
4593 || (GENERIC && type == TREE_TYPE (@0)))
4596 /* Contract view-conversions. */
4598 (view_convert (view_convert @0))
4601 /* For integral conversions with the same precision or pointer
4602 conversions use a NOP_EXPR instead. */
4605 (if ((INTEGRAL_TYPE_P (type) || POINTER_TYPE_P (type))
4606 && (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
4607 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (@0)))
4610 /* Strip inner integral conversions that do not change precision or size, or
4611 zero-extend while keeping the same size (for bool-to-char). */
4613 (view_convert (convert@0 @1))
4614 (if ((INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
4615 && (INTEGRAL_TYPE_P (TREE_TYPE (@1)) || POINTER_TYPE_P (TREE_TYPE (@1)))
4616 && TYPE_SIZE (TREE_TYPE (@0)) == TYPE_SIZE (TREE_TYPE (@1))
4617 && (TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1))
4618 || (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (TREE_TYPE (@1))
4619 && TYPE_UNSIGNED (TREE_TYPE (@1)))))
4622 /* Simplify a view-converted empty or single-element constructor. */
4624 (view_convert CONSTRUCTOR@0)
4626 { tree ctor = (TREE_CODE (@0) == SSA_NAME
4627 ? gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0)) : @0); }
4629 (if (CONSTRUCTOR_NELTS (ctor) == 0)
4630 { build_zero_cst (type); })
4631 (if (CONSTRUCTOR_NELTS (ctor) == 1
4632 && VECTOR_TYPE_P (TREE_TYPE (ctor))
4633 && operand_equal_p (TYPE_SIZE (type),
4634 TYPE_SIZE (TREE_TYPE
4635 (CONSTRUCTOR_ELT (ctor, 0)->value))))
4636 (view_convert { CONSTRUCTOR_ELT (ctor, 0)->value; })))))
4638 /* Re-association barriers around constants and other re-association
4639 barriers can be removed. */
4641 (paren CONSTANT_CLASS_P@0)
4644 (paren (paren@1 @0))
4647 /* Handle cases of two conversions in a row. */
4648 (for ocvt (convert float fix_trunc)
4649 (for icvt (convert float)
4654 tree inside_type = TREE_TYPE (@0);
4655 tree inter_type = TREE_TYPE (@1);
4656 int inside_int = INTEGRAL_TYPE_P (inside_type);
4657 int inside_ptr = POINTER_TYPE_P (inside_type);
4658 int inside_float = FLOAT_TYPE_P (inside_type);
4659 int inside_vec = VECTOR_TYPE_P (inside_type);
4660 unsigned int inside_prec = element_precision (inside_type);
4661 int inside_unsignedp = TYPE_UNSIGNED (inside_type);
4662 int inter_int = INTEGRAL_TYPE_P (inter_type);
4663 int inter_ptr = POINTER_TYPE_P (inter_type);
4664 int inter_float = FLOAT_TYPE_P (inter_type);
4665 int inter_vec = VECTOR_TYPE_P (inter_type);
4666 unsigned int inter_prec = element_precision (inter_type);
4667 int inter_unsignedp = TYPE_UNSIGNED (inter_type);
4668 int final_int = INTEGRAL_TYPE_P (type);
4669 int final_ptr = POINTER_TYPE_P (type);
4670 int final_float = FLOAT_TYPE_P (type);
4671 int final_vec = VECTOR_TYPE_P (type);
4672 unsigned int final_prec = element_precision (type);
4673 int final_unsignedp = TYPE_UNSIGNED (type);
4676 /* In addition to the cases of two conversions in a row
4677 handled below, if we are converting something to its own
4678 type via an object of identical or wider precision, neither
4679 conversion is needed. */
4680 (if (((GIMPLE && useless_type_conversion_p (type, inside_type))
4682 && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (inside_type)))
4683 && (((inter_int || inter_ptr) && final_int)
4684 || (inter_float && final_float))
4685 && inter_prec >= final_prec)
4688 /* Likewise, if the intermediate and initial types are either both
4689 float or both integer, we don't need the middle conversion if the
4690 former is wider than the latter and doesn't change the signedness
4691 (for integers). Avoid this if the final type is a pointer since
4692 then we sometimes need the middle conversion. */
4693 (if (((inter_int && inside_int) || (inter_float && inside_float))
4694 && (final_int || final_float)
4695 && inter_prec >= inside_prec
4696 && (inter_float || inter_unsignedp == inside_unsignedp))
4699 /* If we have a sign-extension of a zero-extended value, we can
4700 replace that by a single zero-extension. Likewise if the
4701 final conversion does not change precision we can drop the
4702 intermediate conversion. */
4703 (if (inside_int && inter_int && final_int
4704 && ((inside_prec < inter_prec && inter_prec < final_prec
4705 && inside_unsignedp && !inter_unsignedp)
4706 || final_prec == inter_prec))
4709 /* Two conversions in a row are not needed unless:
4710 - some conversion is floating-point (overstrict for now), or
4711 - some conversion is a vector (overstrict for now), or
4712 - the intermediate type is narrower than both initial and
4714 - the intermediate type and innermost type differ in signedness,
4715 and the outermost type is wider than the intermediate, or
4716 - the initial type is a pointer type and the precisions of the
4717 intermediate and final types differ, or
4718 - the final type is a pointer type and the precisions of the
4719 initial and intermediate types differ. */
4720 (if (! inside_float && ! inter_float && ! final_float
4721 && ! inside_vec && ! inter_vec && ! final_vec
4722 && (inter_prec >= inside_prec || inter_prec >= final_prec)
4723 && ! (inside_int && inter_int
4724 && inter_unsignedp != inside_unsignedp
4725 && inter_prec < final_prec)
4726 && ((inter_unsignedp && inter_prec > inside_prec)
4727 == (final_unsignedp && final_prec > inter_prec))
4728 && ! (inside_ptr && inter_prec != final_prec)
4729 && ! (final_ptr && inside_prec != inter_prec))
4732 /* `(outer:M)(inter:N) a:O`
4733 can be converted to `(outer:M) a`
4734 if M <= O && N >= O. No matter what signedness of the casts,
4735 as the final is either a truncation from the original or just
4736 a sign change of the type. */
4737 (if (inside_int && inter_int && final_int
4738 && final_prec <= inside_prec
4739 && inter_prec >= inside_prec)
4742 /* A truncation to an unsigned type (a zero-extension) should be
4743 canonicalized as bitwise and of a mask. */
4744 (if (GIMPLE /* PR70366: doing this in GENERIC breaks -Wconversion. */
4745 && final_int && inter_int && inside_int
4746 && final_prec == inside_prec
4747 && final_prec > inter_prec
4749 (convert (bit_and @0 { wide_int_to_tree
4751 wi::mask (inter_prec, false,
4752 TYPE_PRECISION (inside_type))); })))
4754 /* If we are converting an integer to a floating-point that can
4755 represent it exactly and back to an integer, we can skip the
4756 floating-point conversion. */
4757 (if (GIMPLE /* PR66211 */
4758 && inside_int && inter_float && final_int &&
4759 (unsigned) significand_size (TYPE_MODE (inter_type))
4760 >= inside_prec - !inside_unsignedp)
4763 /* (float_type)(integer_type) x -> trunc (x) if the type of x matches
4764 float_type. Only do the transformation if we do not need to preserve
4765 trapping behaviour, so require !flag_trapping_math. */
4768 (float (fix_trunc @0))
4769 (if (!flag_trapping_math
4770 && types_match (type, TREE_TYPE (@0))
4771 && direct_internal_fn_supported_p (IFN_TRUNC, type,
4776 /* If we have a narrowing conversion to an integral type that is fed by a
4777 BIT_AND_EXPR, we might be able to remove the BIT_AND_EXPR if it merely
4778 masks off bits outside the final type (and nothing else). */
4780 (convert (bit_and @0 INTEGER_CST@1))
4781 (if (INTEGRAL_TYPE_P (type)
4782 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
4783 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))
4784 && operand_equal_p (@1, build_low_bits_mask (TREE_TYPE (@1),
4785 TYPE_PRECISION (type)), 0))
4789 /* (X /[ex] A) * A -> X. */
4791 (mult (convert1? (exact_div @0 @@1)) (convert2? @1))
4794 /* Simplify (A / B) * B + (A % B) -> A. */
4795 (for div (trunc_div ceil_div floor_div round_div)
4796 mod (trunc_mod ceil_mod floor_mod round_mod)
4798 (plus:c (mult:c (div @0 @1) @1) (mod @0 @1))
4801 /* x / y * y == x -> x % y == 0. */
4803 (eq:c (mult:c (trunc_div:s @0 @1) @1) @0)
4804 (if (TREE_CODE (TREE_TYPE (@0)) != COMPLEX_TYPE)
4805 (eq (trunc_mod @0 @1) { build_zero_cst (TREE_TYPE (@0)); })))
4807 /* ((X /[ex] A) +- B) * A --> X +- A * B. */
4808 (for op (plus minus)
4810 (mult (convert1? (op (convert2? (exact_div @0 INTEGER_CST@@1)) INTEGER_CST@2)) @1)
4811 (if (tree_nop_conversion_p (type, TREE_TYPE (@2))
4812 && tree_nop_conversion_p (TREE_TYPE (@0), TREE_TYPE (@2)))
4815 wi::overflow_type overflow;
4816 wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@2),
4817 TYPE_SIGN (type), &overflow);
4819 (if (types_match (type, TREE_TYPE (@2))
4820 && types_match (TREE_TYPE (@0), TREE_TYPE (@2)) && !overflow)
4821 (op @0 { wide_int_to_tree (type, mul); })
4822 (with { tree utype = unsigned_type_for (type); }
4823 (convert (op (convert:utype @0)
4824 (mult (convert:utype @1) (convert:utype @2))))))))))
4826 /* Canonicalization of binary operations. */
4828 /* Convert X + -C into X - C. */
4830 (plus @0 REAL_CST@1)
4831 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
4832 (with { tree tem = const_unop (NEGATE_EXPR, type, @1); }
4833 (if (!TREE_OVERFLOW (tem) || !flag_trapping_math)
4834 (minus @0 { tem; })))))
4836 /* Convert x+x into x*2. */
4839 (if (SCALAR_FLOAT_TYPE_P (type))
4840 (mult @0 { build_real (type, dconst2); })
4841 (if (INTEGRAL_TYPE_P (type))
4842 (mult @0 { build_int_cst (type, 2); }))))
4846 (minus integer_zerop @1)
4849 (pointer_diff integer_zerop @1)
4850 (negate (convert @1)))
4852 /* (ARG0 - ARG1) is the same as (-ARG1 + ARG0). So check whether
4853 ARG0 is zero and X + ARG0 reduces to X, since that would mean
4854 (-ARG1 + ARG0) reduces to -ARG1. */
4856 (minus real_zerop@0 @1)
4857 (if (fold_real_zero_addition_p (type, @1, @0, 0))
4860 /* Transform x * -1 into -x. */
4862 (mult @0 integer_minus_onep)
4865 /* Reassociate (X * CST) * Y to (X * Y) * CST. This does not introduce
4866 signed overflow for CST != 0 && CST != -1. */
4868 (mult:c (mult:s@3 @0 INTEGER_CST@1) @2)
4869 (if (TREE_CODE (@2) != INTEGER_CST
4871 && !integer_zerop (@1) && !integer_minus_onep (@1))
4872 (mult (mult @0 @2) @1)))
4874 /* True if we can easily extract the real and imaginary parts of a complex
4876 (match compositional_complex
4877 (convert? (complex @0 @1)))
4879 /* COMPLEX_EXPR and REALPART/IMAGPART_EXPR cancellations. */
4881 (complex (realpart @0) (imagpart @0))
4884 (realpart (complex @0 @1))
4887 (imagpart (complex @0 @1))
4890 /* Sometimes we only care about half of a complex expression. */
4892 (realpart (convert?:s (conj:s @0)))
4893 (convert (realpart @0)))
4895 (imagpart (convert?:s (conj:s @0)))
4896 (convert (negate (imagpart @0))))
4897 (for part (realpart imagpart)
4898 (for op (plus minus)
4900 (part (convert?:s@2 (op:s @0 @1)))
4901 (convert (op (part @0) (part @1))))))
4903 (realpart (convert?:s (CEXPI:s @0)))
4906 (imagpart (convert?:s (CEXPI:s @0)))
4909 /* conj(conj(x)) -> x */
4911 (conj (convert? (conj @0)))
4912 (if (tree_nop_conversion_p (TREE_TYPE (@0), type))
4915 /* conj({x,y}) -> {x,-y} */
4917 (conj (convert?:s (complex:s @0 @1)))
4918 (with { tree itype = TREE_TYPE (type); }
4919 (complex (convert:itype @0) (negate (convert:itype @1)))))
4921 /* BSWAP simplifications, transforms checked by gcc.dg/builtin-bswap-8.c. */
4927 (bswap (bit_not (bswap @0)))
4929 (for bitop (bit_xor bit_ior bit_and)
4931 (bswap (bitop:c (bswap @0) @1))
4932 (bitop @0 (bswap @1))))
4935 (cmp (bswap@2 @0) (bswap @1))
4936 (with { tree ctype = TREE_TYPE (@2); }
4937 (cmp (convert:ctype @0) (convert:ctype @1))))
4939 (cmp (bswap @0) INTEGER_CST@1)
4940 (with { tree ctype = TREE_TYPE (@1); }
4941 (cmp (convert:ctype @0) (bswap! @1)))))
4942 /* (bswap(x) >> C1) & C2 can sometimes be simplified to (x >> C3) & C2. */
4944 (bit_and (convert1? (rshift@0 (convert2? (bswap@4 @1)) INTEGER_CST@2))
4946 (if (BITS_PER_UNIT == 8
4947 && tree_fits_uhwi_p (@2)
4948 && tree_fits_uhwi_p (@3))
4951 unsigned HOST_WIDE_INT prec = TYPE_PRECISION (TREE_TYPE (@4));
4952 unsigned HOST_WIDE_INT bits = tree_to_uhwi (@2);
4953 unsigned HOST_WIDE_INT mask = tree_to_uhwi (@3);
4954 unsigned HOST_WIDE_INT lo = bits & 7;
4955 unsigned HOST_WIDE_INT hi = bits - lo;
4958 && mask < (256u>>lo)
4959 && bits < TYPE_PRECISION (TREE_TYPE(@0)))
4960 (with { unsigned HOST_WIDE_INT ns = (prec - (hi + 8)) + lo; }
4962 (bit_and (convert @1) @3)
4965 tree utype = unsigned_type_for (TREE_TYPE (@1));
4966 tree nst = build_int_cst (integer_type_node, ns);
4968 (bit_and (convert (rshift:utype (convert:utype @1) {nst;})) @3))))))))
4969 /* bswap(x) >> C1 can sometimes be simplified to (T)x >> C2. */
4971 (rshift (convert? (bswap@2 @0)) INTEGER_CST@1)
4972 (if (BITS_PER_UNIT == 8
4973 && CHAR_TYPE_SIZE == 8
4974 && tree_fits_uhwi_p (@1))
4977 unsigned HOST_WIDE_INT prec = TYPE_PRECISION (TREE_TYPE (@2));
4978 unsigned HOST_WIDE_INT bits = tree_to_uhwi (@1);
4979 /* If the bswap was extended before the original shift, this
4980 byte (shift) has the sign of the extension, not the sign of
4981 the original shift. */
4982 tree st = TYPE_PRECISION (type) > prec ? TREE_TYPE (@2) : type;
4984 /* Special case: logical right shift of sign-extended bswap.
4985 (unsigned)(short)bswap16(x)>>12 is (unsigned)((short)x<<8)>>12. */
4986 (if (TYPE_PRECISION (type) > prec
4987 && !TYPE_UNSIGNED (TREE_TYPE (@2))
4988 && TYPE_UNSIGNED (type)
4989 && bits < prec && bits + 8 >= prec)
4990 (with { tree nst = build_int_cst (integer_type_node, prec - 8); }
4991 (rshift (convert (lshift:st (convert:st @0) {nst;})) @1))
4992 (if (bits + 8 == prec)
4993 (if (TYPE_UNSIGNED (st))
4994 (convert (convert:unsigned_char_type_node @0))
4995 (convert (convert:signed_char_type_node @0)))
4996 (if (bits < prec && bits + 8 > prec)
4999 tree nst = build_int_cst (integer_type_node, bits & 7);
5000 tree bt = TYPE_UNSIGNED (st) ? unsigned_char_type_node
5001 : signed_char_type_node;
5003 (convert (rshift:bt (convert:bt @0) {nst;})))))))))
5004 /* bswap(x) & C1 can sometimes be simplified to (x >> C2) & C1. */
5006 (bit_and (convert? (bswap@2 @0)) INTEGER_CST@1)
5007 (if (BITS_PER_UNIT == 8
5008 && tree_fits_uhwi_p (@1)
5009 && tree_to_uhwi (@1) < 256)
5012 unsigned HOST_WIDE_INT prec = TYPE_PRECISION (TREE_TYPE (@2));
5013 tree utype = unsigned_type_for (TREE_TYPE (@0));
5014 tree nst = build_int_cst (integer_type_node, prec - 8);
5016 (bit_and (convert (rshift:utype (convert:utype @0) {nst;})) @1)))))
5019 /* Combine COND_EXPRs and VEC_COND_EXPRs. */
5021 /* Simplify constant conditions.
5022 Only optimize constant conditions when the selected branch
5023 has the same type as the COND_EXPR. This avoids optimizing
5024 away "c ? x : throw", where the throw has a void type.
5025 Note that we cannot throw away the fold-const.cc variant nor
5026 this one as we depend on doing this transform before possibly
5027 A ? B : B -> B triggers and the fold-const.cc one can optimize
5028 0 ? A : B to B even if A has side-effects. Something
5029 genmatch cannot handle. */
5031 (cond INTEGER_CST@0 @1 @2)
5032 (if (integer_zerop (@0))
5033 (if (!VOID_TYPE_P (TREE_TYPE (@2)) || VOID_TYPE_P (type))
5035 (if (!VOID_TYPE_P (TREE_TYPE (@1)) || VOID_TYPE_P (type))
5038 (vec_cond VECTOR_CST@0 @1 @2)
5039 (if (integer_all_onesp (@0))
5041 (if (integer_zerop (@0))
5044 /* Sink unary operations to branches, but only if we do fold both. */
5045 (for op (negate bit_not abs absu)
5047 (op (vec_cond:s @0 @1 @2))
5048 (vec_cond @0 (op! @1) (op! @2))))
5050 /* Sink unary conversions to branches, but only if we do fold both
5051 and the target's truth type is the same as we already have. */
5053 (convert (vec_cond:s @0 @1 @2))
5054 (if (VECTOR_TYPE_P (type)
5055 && types_match (TREE_TYPE (@0), truth_type_for (type)))
5056 (vec_cond @0 (convert! @1) (convert! @2))))
5058 /* Likewise for view_convert of nop_conversions. */
5060 (view_convert (vec_cond:s @0 @1 @2))
5061 (if (VECTOR_TYPE_P (type) && VECTOR_TYPE_P (TREE_TYPE (@1))
5062 && known_eq (TYPE_VECTOR_SUBPARTS (type),
5063 TYPE_VECTOR_SUBPARTS (TREE_TYPE (@1)))
5064 && tree_nop_conversion_p (TREE_TYPE (type), TREE_TYPE (TREE_TYPE (@1))))
5065 (vec_cond @0 (view_convert! @1) (view_convert! @2))))
5067 /* Sink binary operation to branches, but only if we can fold it. */
5068 (for op (tcc_comparison plus minus mult bit_and bit_ior bit_xor
5069 lshift rshift rdiv trunc_div ceil_div floor_div round_div
5070 trunc_mod ceil_mod floor_mod round_mod min max)
5071 /* (c ? a : b) op (c ? d : e) --> c ? (a op d) : (b op e) */
5073 (op (vec_cond:s @0 @1 @2) (vec_cond:s @0 @3 @4))
5074 (vec_cond @0 (op! @1 @3) (op! @2 @4)))
5076 /* (c ? a : b) op d --> c ? (a op d) : (b op d) */
5078 (op (vec_cond:s @0 @1 @2) @3)
5079 (vec_cond @0 (op! @1 @3) (op! @2 @3)))
5081 (op @3 (vec_cond:s @0 @1 @2))
5082 (vec_cond @0 (op! @3 @1) (op! @3 @2))))
5085 (match (nop_atomic_bit_test_and_p @0 @1 @4)
5086 (bit_and (convert?@4 (ATOMIC_FETCH_OR_XOR_N @2 INTEGER_CST@0 @3))
5089 int ibit = tree_log2 (@0);
5090 int ibit2 = tree_log2 (@1);
5094 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))))))
5096 (match (nop_atomic_bit_test_and_p @0 @1 @3)
5097 (bit_and (convert?@3 (SYNC_FETCH_OR_XOR_N @2 INTEGER_CST@0))
5100 int ibit = tree_log2 (@0);
5101 int ibit2 = tree_log2 (@1);
5105 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))))))
5107 (match (nop_atomic_bit_test_and_p @0 @0 @4)
5110 (ATOMIC_FETCH_OR_XOR_N @2 (nop_convert? (lshift@0 integer_onep@5 @6)) @3))
5112 (if (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0)))))
5114 (match (nop_atomic_bit_test_and_p @0 @0 @4)
5117 (SYNC_FETCH_OR_XOR_N @2 (nop_convert? (lshift@0 integer_onep@3 @5))))
5119 (if (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0)))))
5121 (match (nop_atomic_bit_test_and_p @0 @1 @3)
5122 (bit_and@4 (convert?@3 (ATOMIC_FETCH_AND_N @2 INTEGER_CST@0 @5))
5125 int ibit = wi::exact_log2 (wi::zext (wi::bit_not (wi::to_wide (@0)),
5126 TYPE_PRECISION(type)));
5127 int ibit2 = tree_log2 (@1);
5131 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))))))
5133 (match (nop_atomic_bit_test_and_p @0 @1 @3)
5135 (convert?@3 (SYNC_FETCH_AND_AND_N @2 INTEGER_CST@0))
5138 int ibit = wi::exact_log2 (wi::zext (wi::bit_not (wi::to_wide (@0)),
5139 TYPE_PRECISION(type)));
5140 int ibit2 = tree_log2 (@1);
5144 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))))))
5146 (match (nop_atomic_bit_test_and_p @4 @0 @3)
5149 (ATOMIC_FETCH_AND_N @2 (nop_convert?@4 (bit_not (lshift@0 integer_onep@6 @7))) @5))
5151 (if (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@4)))))
5153 (match (nop_atomic_bit_test_and_p @4 @0 @3)
5156 (SYNC_FETCH_AND_AND_N @2 (nop_convert?@4 (bit_not (lshift@0 integer_onep@6 @7)))))
5158 (if (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@4)))))
5162 /* (v ? w : 0) ? a : b is just (v & w) ? a : b
5163 Currently disabled after pass lvec because ARM understands
5164 VEC_COND_EXPR<v==w,-1,0> but not a plain v==w fed to BIT_IOR_EXPR. */
5166 /* These can only be done in gimple as fold likes to convert:
5167 (CMP) & N into (CMP) ? N : 0
5168 and we try to match the same pattern again and again. */
5170 (vec_cond (vec_cond:s @0 @3 integer_zerop) @1 @2)
5171 (if (optimize_vectors_before_lowering_p () && types_match (@0, @3))
5172 (vec_cond (bit_and @0 @3) @1 @2)))
5174 (vec_cond (vec_cond:s @0 integer_all_onesp @3) @1 @2)
5175 (if (optimize_vectors_before_lowering_p () && types_match (@0, @3))
5176 (vec_cond (bit_ior @0 @3) @1 @2)))
5178 (vec_cond (vec_cond:s @0 integer_zerop @3) @1 @2)
5179 (if (optimize_vectors_before_lowering_p () && types_match (@0, @3))
5180 (vec_cond (bit_ior @0 (bit_not @3)) @2 @1)))
5182 (vec_cond (vec_cond:s @0 @3 integer_all_onesp) @1 @2)
5183 (if (optimize_vectors_before_lowering_p () && types_match (@0, @3))
5184 (vec_cond (bit_and @0 (bit_not @3)) @2 @1)))
5186 /* c1 ? c2 ? a : b : b --> (c1 & c2) ? a : b */
5188 (vec_cond @0 (vec_cond:s @1 @2 @3) @3)
5189 (if (optimize_vectors_before_lowering_p () && types_match (@0, @1))
5190 (vec_cond (bit_and @0 @1) @2 @3)))
5192 (vec_cond @0 @2 (vec_cond:s @1 @2 @3))
5193 (if (optimize_vectors_before_lowering_p () && types_match (@0, @1))
5194 (vec_cond (bit_ior @0 @1) @2 @3)))
5196 (vec_cond @0 (vec_cond:s @1 @2 @3) @2)
5197 (if (optimize_vectors_before_lowering_p () && types_match (@0, @1))
5198 (vec_cond (bit_ior (bit_not @0) @1) @2 @3)))
5200 (vec_cond @0 @3 (vec_cond:s @1 @2 @3))
5201 (if (optimize_vectors_before_lowering_p () && types_match (@0, @1))
5202 (vec_cond (bit_and (bit_not @0) @1) @2 @3)))
5205 /* Canonicalize mask ? { 0, ... } : { -1, ...} to ~mask if the mask
5206 types are compatible. */
5208 (vec_cond @0 VECTOR_CST@1 VECTOR_CST@2)
5209 (if (VECTOR_BOOLEAN_TYPE_P (type)
5210 && types_match (type, TREE_TYPE (@0)))
5211 (if (integer_zerop (@1) && integer_all_onesp (@2))
5213 (if (integer_all_onesp (@1) && integer_zerop (@2))
5216 /* A few simplifications of "a ? CST1 : CST2". */
5217 /* NOTE: Only do this on gimple as the if-chain-to-switch
5218 optimization depends on the gimple to have if statements in it. */
5221 (cond @0 INTEGER_CST@1 INTEGER_CST@2)
5223 (if (integer_zerop (@2))
5225 /* a ? 1 : 0 -> a if 0 and 1 are integral types. */
5226 (if (integer_onep (@1))
5227 (convert (convert:boolean_type_node @0)))
5228 /* a ? -1 : 0 -> -a. */
5229 (if (INTEGRAL_TYPE_P (type) && integer_all_onesp (@1))
5230 (if (TYPE_PRECISION (type) == 1)
5231 /* For signed 1-bit precision just cast bool to the type. */
5232 (convert (convert:boolean_type_node @0))
5233 (if (TREE_CODE (type) == BOOLEAN_TYPE)
5235 tree intt = build_nonstandard_integer_type (TYPE_PRECISION (type),
5236 TYPE_UNSIGNED (type));
5238 (convert (negate (convert:intt (convert:boolean_type_node @0)))))
5239 (negate (convert:type (convert:boolean_type_node @0))))))
5240 /* a ? powerof2cst : 0 -> a << (log2(powerof2cst)) */
5241 (if (INTEGRAL_TYPE_P (type) && integer_pow2p (@1))
5243 tree shift = build_int_cst (integer_type_node, tree_log2 (@1));
5245 (lshift (convert (convert:boolean_type_node @0)) { shift; })))))
5246 (if (integer_zerop (@1))
5248 /* a ? 0 : 1 -> !a. */
5249 (if (integer_onep (@2))
5250 (convert (bit_xor (convert:boolean_type_node @0) { boolean_true_node; })))
5251 /* a ? 0 : -1 -> -(!a). */
5252 (if (INTEGRAL_TYPE_P (type) && integer_all_onesp (@2))
5253 (if (TYPE_PRECISION (type) == 1)
5254 /* For signed 1-bit precision just cast bool to the type. */
5255 (convert (bit_xor (convert:boolean_type_node @0) { boolean_true_node; }))
5256 (if (TREE_CODE (type) == BOOLEAN_TYPE)
5258 tree intt = build_nonstandard_integer_type (TYPE_PRECISION (type),
5259 TYPE_UNSIGNED (type));
5261 (convert (negate (convert:intt (bit_xor (convert:boolean_type_node @0)
5262 { boolean_true_node; })))))
5263 (negate (convert:type (bit_xor (convert:boolean_type_node @0)
5264 { boolean_true_node; }))))))
5265 /* a ? 0 : powerof2cst -> (!a) << (log2(powerof2cst)) */
5266 (if (INTEGRAL_TYPE_P (type) && integer_pow2p (@2))
5268 tree shift = build_int_cst (integer_type_node, tree_log2 (@2));
5270 (lshift (convert (bit_xor (convert:boolean_type_node @0)
5271 { boolean_true_node; })) { shift; })))))))
5273 /* (a > 1) ? 0 : (cast)a is the same as (cast)(a == 1)
5274 for unsigned types. */
5276 (cond (gt @0 integer_onep@1) integer_zerop (convert? @2))
5277 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
5278 && bitwise_equal_p (@0, @2))
5279 (convert (eq @0 @1))
5283 /* (a <= 1) & (cast)a is the same as (cast)(a == 1)
5284 for unsigned types. */
5286 (bit_and:c (convert1? (le @0 integer_onep@1)) (convert2? @2))
5287 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
5288 && bitwise_equal_p (@0, @2))
5289 (convert (eq @0 @1))
5293 /* `(a == CST) & a` can be simplified to `0` or `(a == CST)` depending
5294 on the first bit of the CST. */
5296 (bit_and:c (convert@2 (eq @0 INTEGER_CST@1)) (convert? @0))
5297 (if ((wi::to_wide (@1) & 1) != 0)
5299 { build_zero_cst (type); }))
5302 # x_5 in range [cst1, cst2] where cst2 = cst1 + 1
5303 x_5 == cstN ? cst4 : cst3
5304 # op is == or != and N is 1 or 2
5305 to r_6 = x_5 + (min (cst3, cst4) - cst1) or
5306 r_6 = (min (cst3, cst4) + cst1) - x_5 depending on op, N and which
5307 of cst3 and cst4 is smaller.
5308 This was originally done by two_value_replacement in phiopt (PR 88676). */
5311 (cond (eqne SSA_NAME@0 INTEGER_CST@1) INTEGER_CST@2 INTEGER_CST@3)
5312 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
5313 && INTEGRAL_TYPE_P (type)
5314 && (wi::to_widest (@2) + 1 == wi::to_widest (@3)
5315 || wi::to_widest (@2) == wi::to_widest (@3) + 1))
5318 get_range_query (cfun)->range_of_expr (r, @0);
5319 if (r.undefined_p ())
5320 r.set_varying (TREE_TYPE (@0));
5322 wide_int min = r.lower_bound ();
5323 wide_int max = r.upper_bound ();
5326 && (wi::to_wide (@1) == min
5327 || wi::to_wide (@1) == max))
5329 tree arg0 = @2, arg1 = @3;
5331 if ((eqne == EQ_EXPR) ^ (wi::to_wide (@1) == min))
5332 std::swap (arg0, arg1);
5333 if (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type))
5334 type1 = TREE_TYPE (@0);
5337 auto prec = TYPE_PRECISION (type1);
5338 auto unsign = TYPE_UNSIGNED (type1);
5339 if (TREE_CODE (type1) == BOOLEAN_TYPE)
5340 type1 = build_nonstandard_integer_type (prec, unsign);
5341 min = wide_int::from (min, prec,
5342 TYPE_SIGN (TREE_TYPE (@0)));
5343 wide_int a = wide_int::from (wi::to_wide (arg0), prec,
5345 enum tree_code code;
5346 wi::overflow_type ovf;
5347 if (tree_int_cst_lt (arg0, arg1))
5353 /* lhs is known to be in range [min, min+1] and we want to add a
5354 to it. Check if that operation can overflow for those 2 values
5355 and if yes, force unsigned type. */
5356 wi::add (min + (wi::neg_p (a) ? 0 : 1), a, SIGNED, &ovf);
5358 type1 = unsigned_type_for (type1);
5367 /* lhs is known to be in range [min, min+1] and we want to subtract
5368 it from a. Check if that operation can overflow for those 2
5369 values and if yes, force unsigned type. */
5370 wi::sub (a, min + (wi::neg_p (min) ? 0 : 1), SIGNED, &ovf);
5372 type1 = unsigned_type_for (type1);
5375 tree arg = wide_int_to_tree (type1, a);
5377 (if (code == PLUS_EXPR)
5378 (convert (plus (convert:type1 @0) { arg; }))
5379 (convert (minus { arg; } (convert:type1 @0))))))))))
5383 (convert (cond@0 @1 INTEGER_CST@2 INTEGER_CST@3))
5384 (if (INTEGRAL_TYPE_P (type)
5385 && INTEGRAL_TYPE_P (TREE_TYPE (@0)))
5386 (cond @1 (convert @2) (convert @3))))
5388 /* Simplification moved from fold_cond_expr_with_comparison. It may also
5390 /* This pattern implements two kinds simplification:
5393 (cond (cmp (convert1? x) c1) (convert2? x) c2) -> (minmax (x c)) if:
5394 1) Conversions are type widening from smaller type.
5395 2) Const c1 equals to c2 after canonicalizing comparison.
5396 3) Comparison has tree code LT, LE, GT or GE.
5397 This specific pattern is needed when (cmp (convert x) c) may not
5398 be simplified by comparison patterns because of multiple uses of
5399 x. It also makes sense here because simplifying across multiple
5400 referred var is always benefitial for complicated cases.
5403 (cond (eq (convert1? x) c1) (convert2? x) c2) -> (cond (eq x c1) c1 c2). */
5404 (for cmp (lt le gt ge eq ne)
5406 (cond (cmp (convert1? @1) INTEGER_CST@3) (convert2? @1) INTEGER_CST@2)
5409 tree from_type = TREE_TYPE (@1);
5410 tree c1_type = TREE_TYPE (@3), c2_type = TREE_TYPE (@2);
5411 enum tree_code code = ERROR_MARK;
5413 if (INTEGRAL_TYPE_P (from_type)
5414 && int_fits_type_p (@2, from_type)
5415 && (types_match (c1_type, from_type)
5416 || (TYPE_PRECISION (c1_type) > TYPE_PRECISION (from_type)
5417 && (TYPE_UNSIGNED (from_type)
5418 || TYPE_SIGN (c1_type) == TYPE_SIGN (from_type))))
5419 && (types_match (c2_type, from_type)
5420 || (TYPE_PRECISION (c2_type) > TYPE_PRECISION (from_type)
5421 && (TYPE_UNSIGNED (from_type)
5422 || TYPE_SIGN (c2_type) == TYPE_SIGN (from_type)))))
5425 code = minmax_from_comparison (cmp, @1, @3, @1, @2);
5426 /* Can do A == C1 ? A : C2 -> A == C1 ? C1 : C2? */
5427 else if (int_fits_type_p (@3, from_type))
5431 (if (code == MAX_EXPR)
5432 (convert (max @1 (convert @2)))
5433 (if (code == MIN_EXPR)
5434 (convert (min @1 (convert @2)))
5435 (if (code == EQ_EXPR)
5436 (convert (cond (eq @1 (convert @3))
5437 (convert:from_type @3) (convert:from_type @2)))))))))
5439 /* (cond (cmp (convert? x) c1) (op x c2) c3) -> (op (minmax x c1) c2) if:
5441 1) OP is PLUS or MINUS.
5442 2) CMP is LT, LE, GT or GE.
5443 3) C3 == (C1 op C2), and computation doesn't have undefined behavior.
5445 This pattern also handles special cases like:
5447 A) Operand x is a unsigned to signed type conversion and c1 is
5448 integer zero. In this case,
5449 (signed type)x < 0 <=> x > MAX_VAL(signed type)
5450 (signed type)x >= 0 <=> x <= MAX_VAL(signed type)
5451 B) Const c1 may not equal to (C3 op' C2). In this case we also
5452 check equality for (c1+1) and (c1-1) by adjusting comparison
5455 TODO: Though signed type is handled by this pattern, it cannot be
5456 simplified at the moment because C standard requires additional
5457 type promotion. In order to match&simplify it here, the IR needs
5458 to be cleaned up by other optimizers, i.e, VRP. */
5459 (for op (plus minus)
5460 (for cmp (lt le gt ge)
5462 (cond (cmp (convert? @X) INTEGER_CST@1) (op @X INTEGER_CST@2) INTEGER_CST@3)
5463 (with { tree from_type = TREE_TYPE (@X), to_type = TREE_TYPE (@1); }
5464 (if (types_match (from_type, to_type)
5465 /* Check if it is special case A). */
5466 || (TYPE_UNSIGNED (from_type)
5467 && !TYPE_UNSIGNED (to_type)
5468 && TYPE_PRECISION (from_type) == TYPE_PRECISION (to_type)
5469 && integer_zerop (@1)
5470 && (cmp == LT_EXPR || cmp == GE_EXPR)))
5473 wi::overflow_type overflow = wi::OVF_NONE;
5474 enum tree_code code, cmp_code = cmp;
5476 wide_int c1 = wi::to_wide (@1);
5477 wide_int c2 = wi::to_wide (@2);
5478 wide_int c3 = wi::to_wide (@3);
5479 signop sgn = TYPE_SIGN (from_type);
5481 /* Handle special case A), given x of unsigned type:
5482 ((signed type)x < 0) <=> (x > MAX_VAL(signed type))
5483 ((signed type)x >= 0) <=> (x <= MAX_VAL(signed type)) */
5484 if (!types_match (from_type, to_type))
5486 if (cmp_code == LT_EXPR)
5488 if (cmp_code == GE_EXPR)
5490 c1 = wi::max_value (to_type);
5492 /* To simplify this pattern, we require c3 = (c1 op c2). Here we
5493 compute (c3 op' c2) and check if it equals to c1 with op' being
5494 the inverted operator of op. Make sure overflow doesn't happen
5495 if it is undefined. */
5496 if (op == PLUS_EXPR)
5497 real_c1 = wi::sub (c3, c2, sgn, &overflow);
5499 real_c1 = wi::add (c3, c2, sgn, &overflow);
5502 if (!overflow || !TYPE_OVERFLOW_UNDEFINED (from_type))
5504 /* Check if c1 equals to real_c1. Boundary condition is handled
5505 by adjusting comparison operation if necessary. */
5506 if (!wi::cmp (wi::sub (real_c1, 1, sgn, &overflow), c1, sgn)
5509 /* X <= Y - 1 equals to X < Y. */
5510 if (cmp_code == LE_EXPR)
5512 /* X > Y - 1 equals to X >= Y. */
5513 if (cmp_code == GT_EXPR)
5516 if (!wi::cmp (wi::add (real_c1, 1, sgn, &overflow), c1, sgn)
5519 /* X < Y + 1 equals to X <= Y. */
5520 if (cmp_code == LT_EXPR)
5522 /* X >= Y + 1 equals to X > Y. */
5523 if (cmp_code == GE_EXPR)
5526 if (code != cmp_code || !wi::cmp (real_c1, c1, sgn))
5528 if (cmp_code == LT_EXPR || cmp_code == LE_EXPR)
5530 if (cmp_code == GT_EXPR || cmp_code == GE_EXPR)
5535 (if (code == MAX_EXPR)
5536 (op (max @X { wide_int_to_tree (from_type, real_c1); })
5537 { wide_int_to_tree (from_type, c2); })
5538 (if (code == MIN_EXPR)
5539 (op (min @X { wide_int_to_tree (from_type, real_c1); })
5540 { wide_int_to_tree (from_type, c2); })))))))))
5543 /* A >= B ? A : B -> max (A, B) and friends. The code is still
5544 in fold_cond_expr_with_comparison for GENERIC folding with
5545 some extra constraints. */
5546 (for cmp (eq ne le lt unle unlt ge gt unge ungt uneq ltgt)
5548 (cond (cmp:c (nop_convert1?@c0 @0) (nop_convert2?@c1 @1))
5549 (convert3? @0) (convert4? @1))
5550 (if (!HONOR_SIGNED_ZEROS (type)
5551 && (/* Allow widening conversions of the compare operands as data. */
5552 (INTEGRAL_TYPE_P (type)
5553 && types_match (TREE_TYPE (@c0), TREE_TYPE (@0))
5554 && types_match (TREE_TYPE (@c1), TREE_TYPE (@1))
5555 && TYPE_PRECISION (TREE_TYPE (@0)) <= TYPE_PRECISION (type)
5556 && TYPE_PRECISION (TREE_TYPE (@1)) <= TYPE_PRECISION (type))
5557 /* Or sign conversions for the comparison. */
5558 || (types_match (type, TREE_TYPE (@0))
5559 && types_match (type, TREE_TYPE (@1)))))
5561 (if (cmp == EQ_EXPR)
5562 (if (VECTOR_TYPE_P (type))
5565 (if (cmp == NE_EXPR)
5566 (if (VECTOR_TYPE_P (type))
5569 (if (cmp == LE_EXPR || cmp == UNLE_EXPR || cmp == LT_EXPR || cmp == UNLT_EXPR)
5570 (if (!HONOR_NANS (type))
5571 (if (VECTOR_TYPE_P (type))
5572 (view_convert (min @c0 @c1))
5573 (convert (min @c0 @c1)))))
5574 (if (cmp == GE_EXPR || cmp == UNGE_EXPR || cmp == GT_EXPR || cmp == UNGT_EXPR)
5575 (if (!HONOR_NANS (type))
5576 (if (VECTOR_TYPE_P (type))
5577 (view_convert (max @c0 @c1))
5578 (convert (max @c0 @c1)))))
5579 (if (cmp == UNEQ_EXPR)
5580 (if (!HONOR_NANS (type))
5581 (if (VECTOR_TYPE_P (type))
5584 (if (cmp == LTGT_EXPR)
5585 (if (!HONOR_NANS (type))
5586 (if (VECTOR_TYPE_P (type))
5588 (convert @c0))))))))
5591 (for cnd (cond vec_cond)
5592 /* (a != b) ? (a - b) : 0 -> (a - b) */
5594 (cnd (ne:c @0 @1) (minus@2 @0 @1) integer_zerop)
5596 /* (a != b) ? (a ^ b) : 0 -> (a ^ b) */
5598 (cnd (ne:c @0 @1) (bit_xor:c@2 @0 @1) integer_zerop)
5600 /* (a != b) ? (a & b) : a -> (a & b) */
5601 /* (a != b) ? (a | b) : a -> (a | b) */
5602 /* (a != b) ? min(a,b) : a -> min(a,b) */
5603 /* (a != b) ? max(a,b) : a -> max(a,b) */
5604 (for op (bit_and bit_ior min max)
5606 (cnd (ne:c @0 @1) (op:c@2 @0 @1) @0)
5608 /* (a != b) ? (a * b) : (a * a) -> (a * b) */
5609 /* (a != b) ? (a + b) : (a + a) -> (a + b) */
5612 (cnd (ne:c @0 @1) (op@2 @0 @1) (op @0 @0))
5613 (if (ANY_INTEGRAL_TYPE_P (type))
5615 /* (a != b) ? (a + b) : (2 * a) -> (a + b) */
5617 (cnd (ne:c @0 @1) (plus@2 @0 @1) (mult @0 uniform_integer_cst_p@3))
5618 (if (wi::to_wide (uniform_integer_cst_p (@3)) == 2)
5622 /* These was part of minmax phiopt. */
5623 /* Optimize (a CMP b) ? minmax<a, c> : minmax<b, c>
5624 to minmax<min/max<a, b>, c> */
5625 (for minmax (min max)
5626 (for cmp (lt le gt ge ne)
5628 (cond (cmp:c @1 @3) (minmax:c @1 @4) (minmax:c @2 @4))
5631 tree_code code = minmax_from_comparison (cmp, @1, @2, @1, @3);
5633 (if (code == MIN_EXPR)
5634 (minmax (min @1 @2) @4)
5635 (if (code == MAX_EXPR)
5636 (minmax (max @1 @2) @4)))))))
5638 /* Optimize (a CMP CST1) ? max<a,CST2> : a */
5639 (for cmp (gt ge lt le)
5640 minmax (min min max max)
5642 (cond (cmp:c @0 @1) (minmax:c@2 @0 @3) @4)
5645 tree_code code = minmax_from_comparison (cmp, @0, @1, @0, @4);
5647 (if ((cmp == LT_EXPR || cmp == LE_EXPR)
5649 && integer_nonzerop (fold_build2 (LE_EXPR, boolean_type_node, @3, @4)))
5651 (if ((cmp == GT_EXPR || cmp == GE_EXPR)
5653 && integer_nonzerop (fold_build2 (GE_EXPR, boolean_type_node, @3, @4)))
5657 /* These patterns should be after min/max detection as simplifications
5658 of `(type)(zero_one ==/!= 0)` to `(type)(zero_one)`
5659 and `(type)(zero_one^1)` are not done yet. See PR 110637.
5660 Even without those, reaching min/max/and/ior faster is better. */
5662 (cond @0 zero_one_valued_p@1 zero_one_valued_p@2)
5664 /* bool0 ? bool1 : 0 -> bool0 & bool1 */
5665 (if (integer_zerop (@2))
5666 (bit_and (convert @0) @1))
5667 /* bool0 ? 0 : bool2 -> (bool0^1) & bool2 */
5668 (if (integer_zerop (@1))
5669 (bit_and (bit_xor (convert @0) { build_one_cst (type); } ) @2))
5670 /* bool0 ? 1 : bool2 -> bool0 | bool2 */
5671 (if (integer_onep (@1))
5672 (bit_ior (convert @0) @2))
5673 /* bool0 ? bool1 : 1 -> (bool0^1) | bool1 */
5674 (if (integer_onep (@2))
5675 (bit_ior (bit_xor (convert @0) @2) @1))
5680 /* X != C1 ? -X : C2 simplifies to -X when -C1 == C2. */
5682 (cond (ne @0 INTEGER_CST@1) (negate@3 @0) INTEGER_CST@2)
5683 (if (!TYPE_SATURATING (type)
5684 && (TYPE_OVERFLOW_WRAPS (type)
5685 || !wi::only_sign_bit_p (wi::to_wide (@1)))
5686 && wi::eq_p (wi::neg (wi::to_wide (@1)), wi::to_wide (@2)))
5689 /* X != C1 ? ~X : C2 simplifies to ~X when ~C1 == C2. */
5691 (cond (ne @0 INTEGER_CST@1) (bit_not@3 @0) INTEGER_CST@2)
5692 (if (wi::eq_p (wi::bit_not (wi::to_wide (@1)), wi::to_wide (@2)))
5695 /* X != C1 ? abs(X) : C2 simplifies to abs(x) when abs(C1) == C2. */
5698 (cond (ne @0 INTEGER_CST@1) (op@3 @0) INTEGER_CST@2)
5699 (if (wi::abs (wi::to_wide (@1)) == wi::to_wide (@2))
5700 (if (op != ABSU_EXPR && wi::only_sign_bit_p (wi::to_wide (@1)))
5701 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
5702 (convert (absu:utype @0)))
5705 /* (X + 1) > Y ? -X : 1 simplifies to X >= Y ? -X : 1 when
5706 X is unsigned, as when X + 1 overflows, X is -1, so -X == 1. */
5708 (cond (gt (plus @0 integer_onep) @1) (negate @0) integer_onep@2)
5709 (if (TYPE_UNSIGNED (type))
5710 (cond (ge @0 @1) (negate @0) @2)))
5712 (for cnd (cond vec_cond)
5713 /* A ? B : (A ? X : C) -> A ? B : C. */
5715 (cnd @0 (cnd @0 @1 @2) @3)
5718 (cnd @0 @1 (cnd @0 @2 @3))
5720 /* A ? B : (!A ? C : X) -> A ? B : C. */
5721 /* ??? This matches embedded conditions open-coded because genmatch
5722 would generate matching code for conditions in separate stmts only.
5723 The following is still important to merge then and else arm cases
5724 from if-conversion. */
5726 (cnd @0 @1 (cnd @2 @3 @4))
5727 (if (inverse_conditions_p (@0, @2))
5730 (cnd @0 (cnd @1 @2 @3) @4)
5731 (if (inverse_conditions_p (@0, @1))
5734 /* A ? B : B -> B. */
5739 /* !A ? B : C -> A ? C : B. */
5741 (cnd (logical_inverted_value truth_valued_p@0) @1 @2)
5744 /* abs/negative simplifications moved from fold_cond_expr_with_comparison.
5746 None of these transformations work for modes with signed
5747 zeros. If A is +/-0, the first two transformations will
5748 change the sign of the result (from +0 to -0, or vice
5749 versa). The last four will fix the sign of the result,
5750 even though the original expressions could be positive or
5751 negative, depending on the sign of A.
5753 Note that all these transformations are correct if A is
5754 NaN, since the two alternatives (A and -A) are also NaNs. */
5756 (for cnd (cond vec_cond)
5757 /* A == 0 ? A : -A same as -A */
5760 (cnd (cmp @0 zerop) @2 (negate@1 @2))
5761 (if (!HONOR_SIGNED_ZEROS (type)
5762 && bitwise_equal_p (@0, @2))
5765 (cnd (cmp @0 zerop) zerop (negate@1 @2))
5766 (if (!HONOR_SIGNED_ZEROS (type)
5767 && bitwise_equal_p (@0, @2))
5770 /* A != 0 ? A : -A same as A */
5773 (cnd (cmp @0 zerop) @1 (negate @1))
5774 (if (!HONOR_SIGNED_ZEROS (type)
5775 && bitwise_equal_p (@0, @1))
5778 (cnd (cmp @0 zerop) @1 integer_zerop)
5779 (if (!HONOR_SIGNED_ZEROS (type)
5780 && bitwise_equal_p (@0, @1))
5783 /* A >=/> 0 ? A : -A same as abs (A) */
5786 (cnd (cmp @0 zerop) @1 (negate @1))
5787 (if (!HONOR_SIGNED_ZEROS (TREE_TYPE(@0))
5788 && !TYPE_UNSIGNED (TREE_TYPE(@0))
5789 && bitwise_equal_p (@0, @1))
5790 (if (TYPE_UNSIGNED (type))
5793 /* A <=/< 0 ? A : -A same as -abs (A) */
5796 (cnd (cmp @0 zerop) @1 (negate @1))
5797 (if (!HONOR_SIGNED_ZEROS (TREE_TYPE(@0))
5798 && !TYPE_UNSIGNED (TREE_TYPE(@0))
5799 && bitwise_equal_p (@0, @1))
5800 (if ((ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
5801 && !TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
5802 || TYPE_UNSIGNED (type))
5804 tree utype = unsigned_type_for (TREE_TYPE(@0));
5806 (convert (negate (absu:utype @0))))
5807 (negate (abs @0)))))
5810 /* (A - B) == 0 ? (A - B) : (B - A) same as (B - A) */
5813 (cnd (cmp (minus@0 @1 @2) zerop) @0 (minus@3 @2 @1))
5814 (if (!HONOR_SIGNED_ZEROS (type))
5817 (cnd (cmp (minus@0 @1 @2) integer_zerop) integer_zerop (minus@3 @2 @1))
5820 /* (A - B) != 0 ? (A - B) : (B - A) same as (A - B) */
5823 (cnd (cmp (minus@0 @1 @2) zerop) @0 (minus @2 @1))
5824 (if (!HONOR_SIGNED_ZEROS (type))
5827 (cnd (cmp (minus@0 @1 @2) integer_zerop) @0 integer_zerop)
5830 /* (A - B) >=/> 0 ? (A - B) : (B - A) same as abs (A - B) */
5833 (cnd (cmp (minus@0 @1 @2) zerop) @0 (minus @2 @1))
5834 (if (!HONOR_SIGNED_ZEROS (type)
5835 && !TYPE_UNSIGNED (type))
5837 /* (A - B) <=/< 0 ? (A - B) : (B - A) same as -abs (A - B) */
5840 (cnd (cmp (minus@0 @1 @2) zerop) @0 (minus @2 @1))
5841 (if (!HONOR_SIGNED_ZEROS (type)
5842 && !TYPE_UNSIGNED (type))
5843 (if (ANY_INTEGRAL_TYPE_P (type)
5844 && !TYPE_OVERFLOW_WRAPS (type))
5846 tree utype = unsigned_type_for (type);
5848 (convert (negate (absu:utype @0))))
5849 (negate (abs @0)))))
5853 /* -(type)!A -> (type)A - 1. */
5855 (negate (convert?:s (logical_inverted_value:s @0)))
5856 (if (INTEGRAL_TYPE_P (type)
5857 && TREE_CODE (type) != BOOLEAN_TYPE
5858 && TYPE_PRECISION (type) > 1
5859 && TREE_CODE (@0) == SSA_NAME
5860 && ssa_name_has_boolean_range (@0))
5861 (plus (convert:type @0) { build_all_ones_cst (type); })))
5863 /* A + (B vcmp C ? 1 : 0) -> A - (B vcmp C ? -1 : 0), since vector comparisons
5864 return all -1 or all 0 results. */
5865 /* ??? We could instead convert all instances of the vec_cond to negate,
5866 but that isn't necessarily a win on its own. */
5868 (plus:c @3 (view_convert? (vec_cond:s @0 integer_each_onep@1 integer_zerop@2)))
5869 (if (VECTOR_TYPE_P (type)
5870 && known_eq (TYPE_VECTOR_SUBPARTS (type),
5871 TYPE_VECTOR_SUBPARTS (TREE_TYPE (@1)))
5872 && (TYPE_MODE (TREE_TYPE (type))
5873 == TYPE_MODE (TREE_TYPE (TREE_TYPE (@1)))))
5874 (minus @3 (view_convert (vec_cond @0 (negate @1) @2)))))
5876 /* ... likewise A - (B vcmp C ? 1 : 0) -> A + (B vcmp C ? -1 : 0). */
5878 (minus @3 (view_convert? (vec_cond:s @0 integer_each_onep@1 integer_zerop@2)))
5879 (if (VECTOR_TYPE_P (type)
5880 && known_eq (TYPE_VECTOR_SUBPARTS (type),
5881 TYPE_VECTOR_SUBPARTS (TREE_TYPE (@1)))
5882 && (TYPE_MODE (TREE_TYPE (type))
5883 == TYPE_MODE (TREE_TYPE (TREE_TYPE (@1)))))
5884 (plus @3 (view_convert (vec_cond @0 (negate @1) @2)))))
5887 /* Simplifications of comparisons. */
5889 /* See if we can reduce the magnitude of a constant involved in a
5890 comparison by changing the comparison code. This is a canonicalization
5891 formerly done by maybe_canonicalize_comparison_1. */
5895 (cmp @0 uniform_integer_cst_p@1)
5896 (with { tree cst = uniform_integer_cst_p (@1); }
5897 (if (tree_int_cst_sgn (cst) == -1)
5898 (acmp @0 { build_uniform_cst (TREE_TYPE (@1),
5899 wide_int_to_tree (TREE_TYPE (cst),
5905 (cmp @0 uniform_integer_cst_p@1)
5906 (with { tree cst = uniform_integer_cst_p (@1); }
5907 (if (tree_int_cst_sgn (cst) == 1)
5908 (acmp @0 { build_uniform_cst (TREE_TYPE (@1),
5909 wide_int_to_tree (TREE_TYPE (cst),
5910 wi::to_wide (cst) - 1)); })))))
5912 /* We can simplify a logical negation of a comparison to the
5913 inverted comparison. As we cannot compute an expression
5914 operator using invert_tree_comparison we have to simulate
5915 that with expression code iteration. */
5916 (for cmp (tcc_comparison)
5917 icmp (inverted_tcc_comparison)
5918 ncmp (inverted_tcc_comparison_with_nans)
5919 /* Ideally we'd like to combine the following two patterns
5920 and handle some more cases by using
5921 (logical_inverted_value (cmp @0 @1))
5922 here but for that genmatch would need to "inline" that.
5923 For now implement what forward_propagate_comparison did. */
5925 (bit_not (cmp @0 @1))
5926 (if (VECTOR_TYPE_P (type)
5927 || (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1))
5928 /* Comparison inversion may be impossible for trapping math,
5929 invert_tree_comparison will tell us. But we can't use
5930 a computed operator in the replacement tree thus we have
5931 to play the trick below. */
5932 (with { enum tree_code ic = invert_tree_comparison
5933 (cmp, HONOR_NANS (@0)); }
5939 (bit_xor (cmp @0 @1) integer_truep)
5940 (with { enum tree_code ic = invert_tree_comparison
5941 (cmp, HONOR_NANS (@0)); }
5946 /* The following bits are handled by fold_binary_op_with_conditional_arg. */
5948 (ne (cmp@2 @0 @1) integer_zerop)
5949 (if (types_match (type, TREE_TYPE (@2)))
5952 (eq (cmp@2 @0 @1) integer_truep)
5953 (if (types_match (type, TREE_TYPE (@2)))
5956 (ne (cmp@2 @0 @1) integer_truep)
5957 (if (types_match (type, TREE_TYPE (@2)))
5958 (with { enum tree_code ic = invert_tree_comparison
5959 (cmp, HONOR_NANS (@0)); }
5965 (eq (cmp@2 @0 @1) integer_zerop)
5966 (if (types_match (type, TREE_TYPE (@2)))
5967 (with { enum tree_code ic = invert_tree_comparison
5968 (cmp, HONOR_NANS (@0)); }
5974 /* Transform comparisons of the form X - Y CMP 0 to X CMP Y.
5975 ??? The transformation is valid for the other operators if overflow
5976 is undefined for the type, but performing it here badly interacts
5977 with the transformation in fold_cond_expr_with_comparison which
5978 attempts to synthetize ABS_EXPR. */
5980 (for sub (minus pointer_diff)
5982 (cmp (sub@2 @0 @1) integer_zerop)
5983 (if (single_use (@2))
5986 /* Simplify (x < 0) ^ (y < 0) to (x ^ y) < 0 and
5987 (x >= 0) ^ (y >= 0) to (x ^ y) < 0. */
5990 (bit_xor (cmp:s @0 integer_zerop) (cmp:s @1 integer_zerop))
5991 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
5992 && !TYPE_UNSIGNED (TREE_TYPE (@0))
5993 && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
5994 (lt (bit_xor @0 @1) { build_zero_cst (TREE_TYPE (@0)); }))))
5995 /* Simplify (x < 0) ^ (y >= 0) to (x ^ y) >= 0 and
5996 (x >= 0) ^ (y < 0) to (x ^ y) >= 0. */
5998 (bit_xor:c (lt:s @0 integer_zerop) (ge:s @1 integer_zerop))
5999 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6000 && !TYPE_UNSIGNED (TREE_TYPE (@0))
6001 && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
6002 (ge (bit_xor @0 @1) { build_zero_cst (TREE_TYPE (@0)); })))
6004 /* Transform comparisons of the form X * C1 CMP 0 to X CMP 0 in the
6005 signed arithmetic case. That form is created by the compiler
6006 often enough for folding it to be of value. One example is in
6007 computing loop trip counts after Operator Strength Reduction. */
6008 (for cmp (simple_comparison)
6009 scmp (swapped_simple_comparison)
6011 (cmp (mult@3 @0 INTEGER_CST@1) integer_zerop@2)
6012 /* Handle unfolded multiplication by zero. */
6013 (if (integer_zerop (@1))
6015 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
6016 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
6018 /* If @1 is negative we swap the sense of the comparison. */
6019 (if (tree_int_cst_sgn (@1) < 0)
6023 /* For integral types with undefined overflow fold
6024 x * C1 == C2 into x == C2 / C1 or false.
6025 If overflow wraps and C1 is odd, simplify to x == C2 / C1 in the ring
6029 (cmp (mult @0 INTEGER_CST@1) INTEGER_CST@2)
6030 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6031 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
6032 && wi::to_wide (@1) != 0)
6033 (with { widest_int quot; }
6034 (if (wi::multiple_of_p (wi::to_widest (@2), wi::to_widest (@1),
6035 TYPE_SIGN (TREE_TYPE (@0)), "))
6036 (cmp @0 { wide_int_to_tree (TREE_TYPE (@0), quot); })
6037 { constant_boolean_node (cmp == NE_EXPR, type); }))
6038 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6039 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))
6040 && (wi::bit_and (wi::to_wide (@1), 1) == 1))
6043 tree itype = TREE_TYPE (@0);
6044 int p = TYPE_PRECISION (itype);
6045 wide_int m = wi::one (p + 1) << p;
6046 wide_int a = wide_int::from (wi::to_wide (@1), p + 1, UNSIGNED);
6047 wide_int i = wide_int::from (wi::mod_inv (a, m),
6048 p, TYPE_SIGN (itype));
6049 wide_int_to_tree (itype, wi::mul (i, wi::to_wide (@2)));
6052 /* Simplify comparison of something with itself. For IEEE
6053 floating-point, we can only do some of these simplifications. */
6057 (if (! FLOAT_TYPE_P (TREE_TYPE (@0))
6058 || ! tree_expr_maybe_nan_p (@0))
6059 { constant_boolean_node (true, type); }
6061 /* With -ftrapping-math conversion to EQ loses an exception. */
6062 && (! FLOAT_TYPE_P (TREE_TYPE (@0))
6063 || ! flag_trapping_math))
6069 || ! FLOAT_TYPE_P (TREE_TYPE (@0))
6070 || ! tree_expr_maybe_nan_p (@0))
6071 { constant_boolean_node (false, type); })))
6072 (for cmp (unle unge uneq)
6075 { constant_boolean_node (true, type); }))
6076 (for cmp (unlt ungt)
6082 (if (!flag_trapping_math || !tree_expr_maybe_nan_p (@0))
6083 { constant_boolean_node (false, type); }))
6085 /* x == ~x -> false */
6086 /* x != ~x -> true */
6089 (cmp:c @0 (bit_not @0))
6090 { constant_boolean_node (cmp == NE_EXPR, type); }))
6092 /* Fold ~X op ~Y as Y op X. */
6093 (for cmp (simple_comparison)
6095 (cmp (nop_convert1?@4 (bit_not@2 @0)) (nop_convert2? (bit_not@3 @1)))
6096 (if (single_use (@2) && single_use (@3))
6097 (with { tree otype = TREE_TYPE (@4); }
6098 (cmp (convert:otype @1) (convert:otype @0))))))
6100 /* Fold ~X op C as X op' ~C, where op' is the swapped comparison. */
6101 (for cmp (simple_comparison)
6102 scmp (swapped_simple_comparison)
6104 (cmp (nop_convert? (bit_not@2 @0)) CONSTANT_CLASS_P@1)
6105 (if (single_use (@2)
6106 && (TREE_CODE (@1) == INTEGER_CST || TREE_CODE (@1) == VECTOR_CST))
6107 (with { tree otype = TREE_TYPE (@1); }
6108 (scmp (convert:otype @0) (bit_not @1))))))
6110 (for cmp (simple_comparison)
6113 /* IEEE doesn't distinguish +0 and -0 in comparisons. */
6115 /* a CMP (-0) -> a CMP 0 */
6116 (if (REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@1)))
6117 (cmp @0 { build_real (TREE_TYPE (@1), dconst0); }))
6118 /* (-0) CMP b -> 0 CMP b. */
6119 (if (TREE_CODE (@0) == REAL_CST
6120 && REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@0)))
6121 (cmp { build_real (TREE_TYPE (@0), dconst0); } @1))
6122 /* x != NaN is always true, other ops are always false. */
6123 (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
6124 && (cmp == EQ_EXPR || cmp == NE_EXPR || !flag_trapping_math)
6125 && !tree_expr_signaling_nan_p (@1)
6126 && !tree_expr_maybe_signaling_nan_p (@0))
6127 { constant_boolean_node (cmp == NE_EXPR, type); })
6128 /* NaN != y is always true, other ops are always false. */
6129 (if (TREE_CODE (@0) == REAL_CST
6130 && REAL_VALUE_ISNAN (TREE_REAL_CST (@0))
6131 && (cmp == EQ_EXPR || cmp == NE_EXPR || !flag_trapping_math)
6132 && !tree_expr_signaling_nan_p (@0)
6133 && !tree_expr_signaling_nan_p (@1))
6134 { constant_boolean_node (cmp == NE_EXPR, type); })
6135 /* Fold comparisons against infinity. */
6136 (if (REAL_VALUE_ISINF (TREE_REAL_CST (@1))
6137 && MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (@1))))
6140 REAL_VALUE_TYPE max;
6141 enum tree_code code = cmp;
6142 bool neg = REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1));
6144 code = swap_tree_comparison (code);
6147 /* x > +Inf is always false, if we ignore NaNs or exceptions. */
6148 (if (code == GT_EXPR
6149 && !(HONOR_NANS (@0) && flag_trapping_math))
6150 { constant_boolean_node (false, type); })
6151 (if (code == LE_EXPR)
6152 /* x <= +Inf is always true, if we don't care about NaNs. */
6153 (if (! HONOR_NANS (@0))
6154 { constant_boolean_node (true, type); }
6155 /* x <= +Inf is the same as x == x, i.e. !isnan(x), but this loses
6156 an "invalid" exception. */
6157 (if (!flag_trapping_math)
6159 /* x == +Inf and x >= +Inf are always equal to x > DBL_MAX, but
6160 for == this introduces an exception for x a NaN. */
6161 (if ((code == EQ_EXPR && !(HONOR_NANS (@0) && flag_trapping_math))
6163 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
6165 (lt @0 { build_real (TREE_TYPE (@0), max); })
6166 (gt @0 { build_real (TREE_TYPE (@0), max); }))))
6167 /* x < +Inf is always equal to x <= DBL_MAX. */
6168 (if (code == LT_EXPR)
6169 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
6171 (ge @0 { build_real (TREE_TYPE (@0), max); })
6172 (le @0 { build_real (TREE_TYPE (@0), max); }))))
6173 /* x != +Inf is always equal to !(x > DBL_MAX), but this introduces
6174 an exception for x a NaN so use an unordered comparison. */
6175 (if (code == NE_EXPR)
6176 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
6177 (if (! HONOR_NANS (@0))
6179 (ge @0 { build_real (TREE_TYPE (@0), max); })
6180 (le @0 { build_real (TREE_TYPE (@0), max); }))
6182 (unge @0 { build_real (TREE_TYPE (@0), max); })
6183 (unle @0 { build_real (TREE_TYPE (@0), max); }))))))))))
6185 /* If this is a comparison of a real constant with a PLUS_EXPR
6186 or a MINUS_EXPR of a real constant, we can convert it into a
6187 comparison with a revised real constant as long as no overflow
6188 occurs when unsafe_math_optimizations are enabled. */
6189 (if (flag_unsafe_math_optimizations)
6190 (for op (plus minus)
6192 (cmp (op @0 REAL_CST@1) REAL_CST@2)
6195 tree tem = const_binop (op == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR,
6196 TREE_TYPE (@1), @2, @1);
6198 (if (tem && !TREE_OVERFLOW (tem))
6199 (cmp @0 { tem; }))))))
6201 /* Likewise, we can simplify a comparison of a real constant with
6202 a MINUS_EXPR whose first operand is also a real constant, i.e.
6203 (c1 - x) < c2 becomes x > c1-c2. Reordering is allowed on
6204 floating-point types only if -fassociative-math is set. */
6205 (if (flag_associative_math)
6207 (cmp (minus REAL_CST@0 @1) REAL_CST@2)
6208 (with { tree tem = const_binop (MINUS_EXPR, TREE_TYPE (@1), @0, @2); }
6209 (if (tem && !TREE_OVERFLOW (tem))
6210 (cmp { tem; } @1)))))
6212 /* Fold comparisons against built-in math functions. */
6213 (if (flag_unsafe_math_optimizations && ! flag_errno_math)
6216 (cmp (sq @0) REAL_CST@1)
6218 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
6220 /* sqrt(x) < y is always false, if y is negative. */
6221 (if (cmp == EQ_EXPR || cmp == LT_EXPR || cmp == LE_EXPR)
6222 { constant_boolean_node (false, type); })
6223 /* sqrt(x) > y is always true, if y is negative and we
6224 don't care about NaNs, i.e. negative values of x. */
6225 (if (cmp == NE_EXPR || !HONOR_NANS (@0))
6226 { constant_boolean_node (true, type); })
6227 /* sqrt(x) > y is the same as x >= 0, if y is negative. */
6228 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })))
6229 (if (real_equal (TREE_REAL_CST_PTR (@1), &dconst0))
6231 /* sqrt(x) < 0 is always false. */
6232 (if (cmp == LT_EXPR)
6233 { constant_boolean_node (false, type); })
6234 /* sqrt(x) >= 0 is always true if we don't care about NaNs. */
6235 (if (cmp == GE_EXPR && !HONOR_NANS (@0))
6236 { constant_boolean_node (true, type); })
6237 /* sqrt(x) <= 0 -> x == 0. */
6238 (if (cmp == LE_EXPR)
6240 /* Otherwise sqrt(x) cmp 0 -> x cmp 0. Here cmp can be >=, >,
6241 == or !=. In the last case:
6243 (sqrt(x) != 0) == (NaN != 0) == true == (x != 0)
6245 if x is negative or NaN. Due to -funsafe-math-optimizations,
6246 the results for other x follow from natural arithmetic. */
6248 (if ((cmp == LT_EXPR
6252 && !REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
6253 /* Give up for -frounding-math. */
6254 && !HONOR_SIGN_DEPENDENT_ROUNDING (TREE_TYPE (@0)))
6258 enum tree_code ncmp = cmp;
6259 const real_format *fmt
6260 = REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (@0)));
6261 real_arithmetic (&c2, MULT_EXPR,
6262 &TREE_REAL_CST (@1), &TREE_REAL_CST (@1));
6263 real_convert (&c2, fmt, &c2);
6264 /* See PR91734: if c2 is inexact and sqrt(c2) < c (or sqrt(c2) >= c),
6265 then change LT_EXPR into LE_EXPR or GE_EXPR into GT_EXPR. */
6266 if (!REAL_VALUE_ISINF (c2))
6268 tree c3 = fold_const_call (CFN_SQRT, TREE_TYPE (@0),
6269 build_real (TREE_TYPE (@0), c2));
6270 if (c3 == NULL_TREE || TREE_CODE (c3) != REAL_CST)
6272 else if ((cmp == LT_EXPR || cmp == GE_EXPR)
6273 && real_less (&TREE_REAL_CST (c3), &TREE_REAL_CST (@1)))
6274 ncmp = cmp == LT_EXPR ? LE_EXPR : GT_EXPR;
6275 else if ((cmp == LE_EXPR || cmp == GT_EXPR)
6276 && real_less (&TREE_REAL_CST (@1), &TREE_REAL_CST (c3)))
6277 ncmp = cmp == LE_EXPR ? LT_EXPR : GE_EXPR;
6280 /* With rounding to even, sqrt of up to 3 different values
6281 gives the same normal result, so in some cases c2 needs
6283 REAL_VALUE_TYPE c2alt, tow;
6284 if (cmp == LT_EXPR || cmp == GE_EXPR)
6288 real_nextafter (&c2alt, fmt, &c2, &tow);
6289 real_convert (&c2alt, fmt, &c2alt);
6290 if (REAL_VALUE_ISINF (c2alt))
6294 c3 = fold_const_call (CFN_SQRT, TREE_TYPE (@0),
6295 build_real (TREE_TYPE (@0), c2alt));
6296 if (c3 == NULL_TREE || TREE_CODE (c3) != REAL_CST)
6298 else if (real_equal (&TREE_REAL_CST (c3),
6299 &TREE_REAL_CST (@1)))
6305 (if (cmp == GT_EXPR || cmp == GE_EXPR)
6306 (if (REAL_VALUE_ISINF (c2))
6307 /* sqrt(x) > y is x == +Inf, when y is very large. */
6308 (if (HONOR_INFINITIES (@0))
6309 (eq @0 { build_real (TREE_TYPE (@0), c2); })
6310 { constant_boolean_node (false, type); })
6311 /* sqrt(x) > c is the same as x > c*c. */
6312 (if (ncmp != ERROR_MARK)
6313 (if (ncmp == GE_EXPR)
6314 (ge @0 { build_real (TREE_TYPE (@0), c2); })
6315 (gt @0 { build_real (TREE_TYPE (@0), c2); }))))
6316 /* else if (cmp == LT_EXPR || cmp == LE_EXPR) */
6317 (if (REAL_VALUE_ISINF (c2))
6319 /* sqrt(x) < y is always true, when y is a very large
6320 value and we don't care about NaNs or Infinities. */
6321 (if (! HONOR_NANS (@0) && ! HONOR_INFINITIES (@0))
6322 { constant_boolean_node (true, type); })
6323 /* sqrt(x) < y is x != +Inf when y is very large and we
6324 don't care about NaNs. */
6325 (if (! HONOR_NANS (@0))
6326 (ne @0 { build_real (TREE_TYPE (@0), c2); }))
6327 /* sqrt(x) < y is x >= 0 when y is very large and we
6328 don't care about Infinities. */
6329 (if (! HONOR_INFINITIES (@0))
6330 (ge @0 { build_real (TREE_TYPE (@0), dconst0); }))
6331 /* sqrt(x) < y is x >= 0 && x != +Inf, when y is large. */
6334 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
6335 (ne @0 { build_real (TREE_TYPE (@0), c2); }))))
6336 /* sqrt(x) < c is the same as x < c*c, if we ignore NaNs. */
6337 (if (ncmp != ERROR_MARK && ! HONOR_NANS (@0))
6338 (if (ncmp == LT_EXPR)
6339 (lt @0 { build_real (TREE_TYPE (@0), c2); })
6340 (le @0 { build_real (TREE_TYPE (@0), c2); }))
6341 /* sqrt(x) < c is the same as x >= 0 && x < c*c. */
6342 (if (ncmp != ERROR_MARK && GENERIC)
6343 (if (ncmp == LT_EXPR)
6345 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
6346 (lt @0 { build_real (TREE_TYPE (@0), c2); }))
6348 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
6349 (le @0 { build_real (TREE_TYPE (@0), c2); })))))))))))
6350 /* Transform sqrt(x) cmp sqrt(y) -> x cmp y. */
6352 (cmp (sq @0) (sq @1))
6353 (if (! HONOR_NANS (@0))
6356 /* Optimize various special cases of (FTYPE) N CMP (FTYPE) M. */
6357 (for cmp (lt le eq ne ge gt unordered ordered unlt unle ungt unge uneq ltgt)
6358 icmp (lt le eq ne ge gt unordered ordered lt le gt ge eq ne)
6360 (cmp (float@0 @1) (float @2))
6361 (if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (@0))
6362 && ! DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0)))
6365 format_helper fmt (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (@0))));
6366 tree type1 = TREE_TYPE (@1);
6367 bool type1_signed_p = TYPE_SIGN (type1) == SIGNED;
6368 tree type2 = TREE_TYPE (@2);
6369 bool type2_signed_p = TYPE_SIGN (type2) == SIGNED;
6371 (if (fmt.can_represent_integral_type_p (type1)
6372 && fmt.can_represent_integral_type_p (type2))
6373 (if (cmp == ORDERED_EXPR || cmp == UNORDERED_EXPR)
6374 { constant_boolean_node (cmp == ORDERED_EXPR, type); }
6375 (if (TYPE_PRECISION (type1) > TYPE_PRECISION (type2)
6376 && type1_signed_p >= type2_signed_p)
6377 (icmp @1 (convert @2))
6378 (if (TYPE_PRECISION (type1) < TYPE_PRECISION (type2)
6379 && type1_signed_p <= type2_signed_p)
6380 (icmp (convert:type2 @1) @2)
6381 (if (TYPE_PRECISION (type1) == TYPE_PRECISION (type2)
6382 && type1_signed_p == type2_signed_p)
6383 (icmp @1 @2))))))))))
6385 /* Optimize various special cases of (FTYPE) N CMP CST. */
6386 (for cmp (lt le eq ne ge gt)
6387 icmp (le le eq ne ge ge)
6389 (cmp (float @0) REAL_CST@1)
6390 (if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (@1))
6391 && ! DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1)))
6394 tree itype = TREE_TYPE (@0);
6395 format_helper fmt (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (@1))));
6396 const REAL_VALUE_TYPE *cst = TREE_REAL_CST_PTR (@1);
6397 /* Be careful to preserve any potential exceptions due to
6398 NaNs. qNaNs are ok in == or != context.
6399 TODO: relax under -fno-trapping-math or
6400 -fno-signaling-nans. */
6402 = real_isnan (cst) && (cst->signalling
6403 || (cmp != EQ_EXPR && cmp != NE_EXPR));
6405 /* TODO: allow non-fitting itype and SNaNs when
6406 -fno-trapping-math. */
6407 (if (fmt.can_represent_integral_type_p (itype) && ! exception_p)
6410 signop isign = TYPE_SIGN (itype);
6411 REAL_VALUE_TYPE imin, imax;
6412 real_from_integer (&imin, fmt, wi::min_value (itype), isign);
6413 real_from_integer (&imax, fmt, wi::max_value (itype), isign);
6415 REAL_VALUE_TYPE icst;
6416 if (cmp == GT_EXPR || cmp == GE_EXPR)
6417 real_ceil (&icst, fmt, cst);
6418 else if (cmp == LT_EXPR || cmp == LE_EXPR)
6419 real_floor (&icst, fmt, cst);
6421 real_trunc (&icst, fmt, cst);
6423 bool cst_int_p = !real_isnan (cst) && real_identical (&icst, cst);
6425 bool overflow_p = false;
6427 = real_to_integer (&icst, &overflow_p, TYPE_PRECISION (itype));
6430 /* Optimize cases when CST is outside of ITYPE's range. */
6431 (if (real_compare (LT_EXPR, cst, &imin))
6432 { constant_boolean_node (cmp == GT_EXPR || cmp == GE_EXPR || cmp == NE_EXPR,
6434 (if (real_compare (GT_EXPR, cst, &imax))
6435 { constant_boolean_node (cmp == LT_EXPR || cmp == LE_EXPR || cmp == NE_EXPR,
6437 /* Remove cast if CST is an integer representable by ITYPE. */
6439 (cmp @0 { gcc_assert (!overflow_p);
6440 wide_int_to_tree (itype, icst_val); })
6442 /* When CST is fractional, optimize
6443 (FTYPE) N == CST -> 0
6444 (FTYPE) N != CST -> 1. */
6445 (if (cmp == EQ_EXPR || cmp == NE_EXPR)
6446 { constant_boolean_node (cmp == NE_EXPR, type); })
6447 /* Otherwise replace with sensible integer constant. */
6450 gcc_checking_assert (!overflow_p);
6452 (icmp @0 { wide_int_to_tree (itype, icst_val); })))))))))
6454 /* Fold A /[ex] B CMP C to A CMP B * C. */
6457 (cmp (exact_div @0 @1) INTEGER_CST@2)
6458 (if (!integer_zerop (@1))
6459 (if (wi::to_wide (@2) == 0)
6461 (if (TREE_CODE (@1) == INTEGER_CST)
6464 wi::overflow_type ovf;
6465 wide_int prod = wi::mul (wi::to_wide (@2), wi::to_wide (@1),
6466 TYPE_SIGN (TREE_TYPE (@1)), &ovf);
6469 { constant_boolean_node (cmp == NE_EXPR, type); }
6470 (cmp @0 { wide_int_to_tree (TREE_TYPE (@0), prod); }))))))))
6471 (for cmp (lt le gt ge)
6473 (cmp (exact_div @0 INTEGER_CST@1) INTEGER_CST@2)
6474 (if (wi::gt_p (wi::to_wide (@1), 0, TYPE_SIGN (TREE_TYPE (@1))))
6477 wi::overflow_type ovf;
6478 wide_int prod = wi::mul (wi::to_wide (@2), wi::to_wide (@1),
6479 TYPE_SIGN (TREE_TYPE (@1)), &ovf);
6482 { constant_boolean_node (wi::lt_p (wi::to_wide (@2), 0,
6483 TYPE_SIGN (TREE_TYPE (@2)))
6484 != (cmp == LT_EXPR || cmp == LE_EXPR), type); }
6485 (cmp @0 { wide_int_to_tree (TREE_TYPE (@0), prod); }))))))
6487 /* Fold (size_t)(A /[ex] B) CMP C to (size_t)A CMP (size_t)B * C or A CMP' 0.
6489 For small C (less than max/B), this is (size_t)A CMP (size_t)B * C.
6490 For large C (more than min/B+2^size), this is also true, with the
6491 multiplication computed modulo 2^size.
6492 For intermediate C, this just tests the sign of A. */
6493 (for cmp (lt le gt ge)
6496 (cmp (convert (exact_div @0 INTEGER_CST@1)) INTEGER_CST@2)
6497 (if (tree_nop_conversion_p (TREE_TYPE (@0), TREE_TYPE (@2))
6498 && TYPE_UNSIGNED (TREE_TYPE (@2)) && !TYPE_UNSIGNED (TREE_TYPE (@0))
6499 && wi::gt_p (wi::to_wide (@1), 0, TYPE_SIGN (TREE_TYPE (@1))))
6502 tree utype = TREE_TYPE (@2);
6503 wide_int denom = wi::to_wide (@1);
6504 wide_int right = wi::to_wide (@2);
6505 wide_int smax = wi::sdiv_trunc (wi::max_value (TREE_TYPE (@0)), denom);
6506 wide_int smin = wi::sdiv_trunc (wi::min_value (TREE_TYPE (@0)), denom);
6507 bool small = wi::leu_p (right, smax);
6508 bool large = wi::geu_p (right, smin);
6510 (if (small || large)
6511 (cmp (convert:utype @0) (mult @2 (convert @1)))
6512 (cmp2 @0 { build_zero_cst (TREE_TYPE (@0)); }))))))
6514 /* Unordered tests if either argument is a NaN. */
6516 (bit_ior (unordered @0 @0) (unordered @1 @1))
6517 (if (types_match (@0, @1))
6520 (bit_and (ordered @0 @0) (ordered @1 @1))
6521 (if (types_match (@0, @1))
6524 (bit_ior:c (unordered @0 @0) (unordered:c@2 @0 @1))
6527 (bit_and:c (ordered @0 @0) (ordered:c@2 @0 @1))
6530 /* A & (2**N - 1) <= 2**K - 1 -> A & (2**N - 2**K) == 0
6531 A & (2**N - 1) > 2**K - 1 -> A & (2**N - 2**K) != 0
6533 Note that comparisons
6534 A & (2**N - 1) < 2**K -> A & (2**N - 2**K) == 0
6535 A & (2**N - 1) >= 2**K -> A & (2**N - 2**K) != 0
6536 will be canonicalized to above so there's no need to
6543 (cmp (bit_and@0 @1 INTEGER_CST@2) INTEGER_CST@3)
6544 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)))
6547 tree ty = TREE_TYPE (@0);
6548 unsigned prec = TYPE_PRECISION (ty);
6549 wide_int mask = wi::to_wide (@2, prec);
6550 wide_int rhs = wi::to_wide (@3, prec);
6551 signop sgn = TYPE_SIGN (ty);
6553 (if ((mask & (mask + 1)) == 0 && wi::gt_p (rhs, 0, sgn)
6554 && (rhs & (rhs + 1)) == 0 && wi::ge_p (mask, rhs, sgn))
6555 (eqcmp (bit_and @1 { wide_int_to_tree (ty, mask - rhs); })
6556 { build_zero_cst (ty); }))))))
6558 /* -A CMP -B -> B CMP A. */
6559 (for cmp (tcc_comparison)
6560 scmp (swapped_tcc_comparison)
6562 (cmp (negate @0) (negate @1))
6563 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
6564 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
6567 || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))))
6570 (cmp (negate @0) CONSTANT_CLASS_P@1)
6571 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
6572 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
6575 || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))))
6576 (with { tree tem = const_unop (NEGATE_EXPR, TREE_TYPE (@0), @1); }
6577 (if (tem && !TREE_OVERFLOW (tem))
6578 (scmp @0 { tem; }))))))
6580 /* Convert ABS[U]_EXPR<x> == 0 or ABS[U]_EXPR<x> != 0 to x == 0 or x != 0. */
6584 (eqne (op @0) zerop@1)
6585 (eqne @0 { build_zero_cst (TREE_TYPE (@0)); }))))
6587 /* From fold_sign_changed_comparison and fold_widened_comparison.
6588 FIXME: the lack of symmetry is disturbing. */
6589 (for cmp (simple_comparison)
6591 (cmp (convert@0 @00) (convert?@1 @10))
6592 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6593 /* Disable this optimization if we're casting a function pointer
6594 type on targets that require function pointer canonicalization. */
6595 && !(targetm.have_canonicalize_funcptr_for_compare ()
6596 && ((POINTER_TYPE_P (TREE_TYPE (@00))
6597 && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@00))))
6598 || (POINTER_TYPE_P (TREE_TYPE (@10))
6599 && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@10))))))
6601 (if (TYPE_PRECISION (TREE_TYPE (@00)) == TYPE_PRECISION (TREE_TYPE (@0))
6602 && (TREE_CODE (@10) == INTEGER_CST
6604 && (TYPE_UNSIGNED (TREE_TYPE (@00)) == TYPE_UNSIGNED (TREE_TYPE (@0))
6607 && !POINTER_TYPE_P (TREE_TYPE (@00))
6608 /* (int)bool:32 != (int)uint is not the same as
6609 bool:32 != (bool:32)uint since boolean types only have two valid
6610 values independent of their precision. */
6611 && (TREE_CODE (TREE_TYPE (@00)) != BOOLEAN_TYPE
6612 || TREE_CODE (TREE_TYPE (@10)) == BOOLEAN_TYPE))
6613 /* ??? The special-casing of INTEGER_CST conversion was in the original
6614 code and here to avoid a spurious overflow flag on the resulting
6615 constant which fold_convert produces. */
6616 (if (TREE_CODE (@1) == INTEGER_CST)
6617 (cmp @00 { force_fit_type (TREE_TYPE (@00),
6618 wide_int::from (wi::to_wide (@1),
6619 MAX (TYPE_PRECISION (TREE_TYPE (@1)),
6620 TYPE_PRECISION (TREE_TYPE (@00))),
6621 TYPE_SIGN (TREE_TYPE (@1))),
6622 0, TREE_OVERFLOW (@1)); })
6623 (cmp @00 (convert @1)))
6625 (if (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (TREE_TYPE (@00)))
6626 /* If possible, express the comparison in the shorter mode. */
6627 (if ((cmp == EQ_EXPR || cmp == NE_EXPR
6628 || TYPE_UNSIGNED (TREE_TYPE (@0)) == TYPE_UNSIGNED (TREE_TYPE (@00))
6629 || (!TYPE_UNSIGNED (TREE_TYPE (@0))
6630 && TYPE_UNSIGNED (TREE_TYPE (@00))))
6631 && (types_match (TREE_TYPE (@10), TREE_TYPE (@00))
6632 || ((TYPE_PRECISION (TREE_TYPE (@00))
6633 >= TYPE_PRECISION (TREE_TYPE (@10)))
6634 && (TYPE_UNSIGNED (TREE_TYPE (@00))
6635 == TYPE_UNSIGNED (TREE_TYPE (@10))))
6636 || (TREE_CODE (@10) == INTEGER_CST
6637 && INTEGRAL_TYPE_P (TREE_TYPE (@00))
6638 && int_fits_type_p (@10, TREE_TYPE (@00)))))
6639 (cmp @00 (convert @10))
6640 (if (TREE_CODE (@10) == INTEGER_CST
6641 && INTEGRAL_TYPE_P (TREE_TYPE (@00))
6642 && !int_fits_type_p (@10, TREE_TYPE (@00)))
6645 tree min = lower_bound_in_type (TREE_TYPE (@10), TREE_TYPE (@00));
6646 tree max = upper_bound_in_type (TREE_TYPE (@10), TREE_TYPE (@00));
6647 bool above = integer_nonzerop (const_binop (LT_EXPR, type, max, @10));
6648 bool below = integer_nonzerop (const_binop (LT_EXPR, type, @10, min));
6650 (if (above || below)
6651 (if (cmp == EQ_EXPR || cmp == NE_EXPR)
6652 { constant_boolean_node (cmp == EQ_EXPR ? false : true, type); }
6653 (if (cmp == LT_EXPR || cmp == LE_EXPR)
6654 { constant_boolean_node (above ? true : false, type); }
6655 (if (cmp == GT_EXPR || cmp == GE_EXPR)
6656 { constant_boolean_node (above ? false : true, type); })))))))))
6657 /* Fold (double)float1 CMP (double)float2 into float1 CMP float2. */
6658 (if (FLOAT_TYPE_P (TREE_TYPE (@00))
6659 && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0))
6660 == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@00)))
6661 && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0))
6662 == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@10))))
6665 tree type1 = TREE_TYPE (@10);
6666 if (TREE_CODE (@10) == REAL_CST && !DECIMAL_FLOAT_TYPE_P (type1))
6668 REAL_VALUE_TYPE orig = TREE_REAL_CST (@10);
6669 if (TYPE_PRECISION (type1) > TYPE_PRECISION (float_type_node)
6670 && exact_real_truncate (TYPE_MODE (float_type_node), &orig))
6671 type1 = float_type_node;
6672 if (TYPE_PRECISION (type1) > TYPE_PRECISION (double_type_node)
6673 && exact_real_truncate (TYPE_MODE (double_type_node), &orig))
6674 type1 = double_type_node;
6677 = (element_precision (TREE_TYPE (@00)) > element_precision (type1)
6678 ? TREE_TYPE (@00) : type1);
6680 (if (element_precision (TREE_TYPE (@0)) > element_precision (newtype))
6681 (cmp (convert:newtype @00) (convert:newtype @10))))))))
6686 /* SSA names are canonicalized to 2nd place. */
6687 (cmp addr@0 SSA_NAME@1)
6690 poly_int64 off; tree base;
6691 tree addr = (TREE_CODE (@0) == SSA_NAME
6692 ? gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0)) : @0);
6694 /* A local variable can never be pointed to by
6695 the default SSA name of an incoming parameter. */
6696 (if (SSA_NAME_IS_DEFAULT_DEF (@1)
6697 && TREE_CODE (SSA_NAME_VAR (@1)) == PARM_DECL
6698 && (base = get_base_address (TREE_OPERAND (addr, 0)))
6699 && TREE_CODE (base) == VAR_DECL
6700 && auto_var_in_fn_p (base, current_function_decl))
6701 (if (cmp == NE_EXPR)
6702 { constant_boolean_node (true, type); }
6703 { constant_boolean_node (false, type); })
6704 /* If the address is based on @1 decide using the offset. */
6705 (if ((base = get_addr_base_and_unit_offset (TREE_OPERAND (addr, 0), &off))
6706 && TREE_CODE (base) == MEM_REF
6707 && TREE_OPERAND (base, 0) == @1)
6708 (with { off += mem_ref_offset (base).force_shwi (); }
6709 (if (known_ne (off, 0))
6710 { constant_boolean_node (cmp == NE_EXPR, type); }
6711 (if (known_eq (off, 0))
6712 { constant_boolean_node (cmp == EQ_EXPR, type); }))))))))
6714 /* Equality compare simplifications from fold_binary */
6717 /* If we have (A | C) == D where C & ~D != 0, convert this into 0.
6718 Similarly for NE_EXPR. */
6720 (cmp (convert?@3 (bit_ior @0 INTEGER_CST@1)) INTEGER_CST@2)
6721 (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0))
6722 && wi::bit_and_not (wi::to_wide (@1), wi::to_wide (@2)) != 0)
6723 { constant_boolean_node (cmp == NE_EXPR, type); }))
6725 /* (X ^ Y) == 0 becomes X == Y, and (X ^ Y) != 0 becomes X != Y. */
6727 (cmp (bit_xor @0 @1) integer_zerop)
6730 /* (X ^ Y) == Y becomes X == 0.
6731 Likewise (X ^ Y) == X becomes Y == 0. */
6733 (cmp:c (bit_xor:c @0 @1) @0)
6734 (cmp @1 { build_zero_cst (TREE_TYPE (@1)); }))
6736 /* (X & Y) == X becomes (X & ~Y) == 0. */
6738 (cmp:c (bit_and:c @0 @1) @0)
6739 (cmp (bit_and @0 (bit_not! @1)) { build_zero_cst (TREE_TYPE (@0)); }))
6741 (cmp:c (convert@3 (bit_and (convert@2 @0) INTEGER_CST@1)) (convert @0))
6742 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6743 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
6744 && INTEGRAL_TYPE_P (TREE_TYPE (@3))
6745 && TYPE_PRECISION (TREE_TYPE (@2)) == TYPE_PRECISION (TREE_TYPE (@0))
6746 && TYPE_PRECISION (TREE_TYPE (@3)) > TYPE_PRECISION (TREE_TYPE (@2))
6747 && !wi::neg_p (wi::to_wide (@1)))
6748 (cmp (bit_and @0 (convert (bit_not @1)))
6749 { build_zero_cst (TREE_TYPE (@0)); })))
6751 /* (X | Y) == Y becomes (X & ~Y) == 0. */
6753 (cmp:c (bit_ior:c @0 @1) @1)
6754 (cmp (bit_and @0 (bit_not! @1)) { build_zero_cst (TREE_TYPE (@0)); }))
6756 /* (X ^ C1) op C2 can be rewritten as X op (C1 ^ C2). */
6758 (cmp (convert?@3 (bit_xor @0 INTEGER_CST@1)) INTEGER_CST@2)
6759 (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0)))
6760 (cmp @0 (bit_xor @1 (convert @2)))))
6763 (cmp (nop_convert? @0) integer_zerop)
6764 (if (tree_expr_nonzero_p (@0))
6765 { constant_boolean_node (cmp == NE_EXPR, type); }))
6767 /* (X & C) op (Y & C) into (X ^ Y) & C op 0. */
6769 (cmp (bit_and:cs @0 @2) (bit_and:cs @1 @2))
6770 (cmp (bit_and (bit_xor @0 @1) @2) { build_zero_cst (TREE_TYPE (@2)); })))
6772 /* (X < 0) != (Y < 0) into (X ^ Y) < 0.
6773 (X >= 0) != (Y >= 0) into (X ^ Y) < 0.
6774 (X < 0) == (Y < 0) into (X ^ Y) >= 0.
6775 (X >= 0) == (Y >= 0) into (X ^ Y) >= 0. */
6780 (cmp (sgncmp @0 integer_zerop@2) (sgncmp @1 integer_zerop))
6781 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
6782 && !TYPE_UNSIGNED (TREE_TYPE (@0))
6783 && types_match (@0, @1))
6784 (ncmp (bit_xor @0 @1) @2)))))
6785 /* (X < 0) == (Y >= 0) into (X ^ Y) < 0.
6786 (X < 0) != (Y >= 0) into (X ^ Y) >= 0. */
6790 (cmp:c (lt @0 integer_zerop@2) (ge @1 integer_zerop))
6791 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
6792 && !TYPE_UNSIGNED (TREE_TYPE (@0))
6793 && types_match (@0, @1))
6794 (ncmp (bit_xor @0 @1) @2))))
6796 /* If we have (A & C) == C where C is a power of 2, convert this into
6797 (A & C) != 0. Similarly for NE_EXPR. */
6801 (cmp (bit_and@2 @0 integer_pow2p@1) @1)
6802 (icmp @2 { build_zero_cst (TREE_TYPE (@0)); })))
6805 /* From fold_binary_op_with_conditional_arg handle the case of
6806 rewriting (a ? b : c) > d to a ? (b > d) : (c > d) when the
6807 compares simplify. */
6808 (for cmp (simple_comparison)
6810 (cmp:c (cond @0 @1 @2) @3)
6811 /* Do not move possibly trapping operations into the conditional as this
6812 pessimizes code and causes gimplification issues when applied late. */
6813 (if (!FLOAT_TYPE_P (TREE_TYPE (@3))
6814 || !operation_could_trap_p (cmp, true, false, @3))
6815 (cond @0 (cmp! @1 @3) (cmp! @2 @3)))))
6819 /* x < 0 ? ~y : y into (x >> (prec-1)) ^ y. */
6820 /* x >= 0 ? ~y : y into ~((x >> (prec-1)) ^ y). */
6822 (cond (cmp @0 integer_zerop) (bit_not @1) @1)
6823 (if (INTEGRAL_TYPE_P (type)
6824 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
6825 && !TYPE_UNSIGNED (TREE_TYPE (@0))
6826 && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (type))
6829 tree shifter = build_int_cst (integer_type_node, TYPE_PRECISION (type) - 1);
6831 (if (cmp == LT_EXPR)
6832 (bit_xor (convert (rshift @0 {shifter;})) @1)
6833 (bit_not (bit_xor (convert (rshift @0 {shifter;})) @1))))))
6834 /* x < 0 ? y : ~y into ~((x >> (prec-1)) ^ y). */
6835 /* x >= 0 ? y : ~y into (x >> (prec-1)) ^ y. */
6837 (cond (cmp @0 integer_zerop) @1 (bit_not @1))
6838 (if (INTEGRAL_TYPE_P (type)
6839 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
6840 && !TYPE_UNSIGNED (TREE_TYPE (@0))
6841 && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (type))
6844 tree shifter = build_int_cst (integer_type_node, TYPE_PRECISION (type) - 1);
6846 (if (cmp == GE_EXPR)
6847 (bit_xor (convert (rshift @0 {shifter;})) @1)
6848 (bit_not (bit_xor (convert (rshift @0 {shifter;})) @1)))))))
6850 /* If we have (A & C) != 0 ? D : 0 where C and D are powers of 2,
6851 convert this into a shift followed by ANDing with D. */
6854 (ne (bit_and @0 integer_pow2p@1) integer_zerop)
6855 INTEGER_CST@2 integer_zerop)
6856 (if (!POINTER_TYPE_P (type) && integer_pow2p (@2))
6858 int shift = (wi::exact_log2 (wi::to_wide (@2))
6859 - wi::exact_log2 (wi::to_wide (@1)));
6863 (lshift (convert @0) { build_int_cst (integer_type_node, shift); }) @2)
6865 (convert (rshift @0 { build_int_cst (integer_type_node, -shift); }))
6868 /* If we have (A & C) != 0 where C is the sign bit of A, convert
6869 this into A < 0. Similarly for (A & C) == 0 into A >= 0. */
6873 (cmp (bit_and (convert?@2 @0) integer_pow2p@1) integer_zerop)
6874 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6875 && type_has_mode_precision_p (TREE_TYPE (@0))
6876 && element_precision (@2) >= element_precision (@0)
6877 && wi::only_sign_bit_p (wi::to_wide (@1), element_precision (@0)))
6878 (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
6879 (ncmp (convert:stype @0) { build_zero_cst (stype); })))))
6881 /* If we have A < 0 ? C : 0 where C is a power of 2, convert
6882 this into a right shift or sign extension followed by ANDing with C. */
6885 (lt @0 integer_zerop)
6886 INTEGER_CST@1 integer_zerop)
6887 (if (integer_pow2p (@1)
6888 && !TYPE_UNSIGNED (TREE_TYPE (@0)))
6890 int shift = element_precision (@0) - wi::exact_log2 (wi::to_wide (@1)) - 1;
6894 (convert (rshift @0 { build_int_cst (integer_type_node, shift); }))
6896 /* Otherwise ctype must be wider than TREE_TYPE (@0) and pure
6897 sign extension followed by AND with C will achieve the effect. */
6898 (bit_and (convert @0) @1)))))
6900 /* When the addresses are not directly of decls compare base and offset.
6901 This implements some remaining parts of fold_comparison address
6902 comparisons but still no complete part of it. Still it is good
6903 enough to make fold_stmt not regress when not dispatching to fold_binary. */
6904 (for cmp (simple_comparison)
6906 (cmp (convert1?@2 addr@0) (convert2? addr@1))
6909 poly_int64 off0, off1;
6911 int equal = address_compare (cmp, TREE_TYPE (@2), @0, @1, base0, base1,
6912 off0, off1, GENERIC);
6916 (if (cmp == EQ_EXPR && (known_eq (off0, off1) || known_ne (off0, off1)))
6917 { constant_boolean_node (known_eq (off0, off1), type); })
6918 (if (cmp == NE_EXPR && (known_eq (off0, off1) || known_ne (off0, off1)))
6919 { constant_boolean_node (known_ne (off0, off1), type); })
6920 (if (cmp == LT_EXPR && (known_lt (off0, off1) || known_ge (off0, off1)))
6921 { constant_boolean_node (known_lt (off0, off1), type); })
6922 (if (cmp == LE_EXPR && (known_le (off0, off1) || known_gt (off0, off1)))
6923 { constant_boolean_node (known_le (off0, off1), type); })
6924 (if (cmp == GE_EXPR && (known_ge (off0, off1) || known_lt (off0, off1)))
6925 { constant_boolean_node (known_ge (off0, off1), type); })
6926 (if (cmp == GT_EXPR && (known_gt (off0, off1) || known_le (off0, off1)))
6927 { constant_boolean_node (known_gt (off0, off1), type); }))
6930 (if (cmp == EQ_EXPR)
6931 { constant_boolean_node (false, type); })
6932 (if (cmp == NE_EXPR)
6933 { constant_boolean_node (true, type); })))))))
6936 /* a?~t:t -> (-(a))^t */
6939 (with { bool wascmp; }
6940 (if (INTEGRAL_TYPE_P (type)
6941 && bitwise_inverted_equal_p (@1, @2, wascmp)
6942 && (!wascmp || TYPE_PRECISION (type) == 1))
6943 (if ((!TYPE_UNSIGNED (type) && TREE_CODE (type) == BOOLEAN_TYPE)
6944 || TYPE_PRECISION (type) == 1)
6945 (bit_xor (convert:type @0) @2)
6946 (bit_xor (negate (convert:type @0)) @2)))))
6949 /* Simplify pointer equality compares using PTA. */
6953 (if (POINTER_TYPE_P (TREE_TYPE (@0))
6954 && ptrs_compare_unequal (@0, @1))
6955 { constant_boolean_node (neeq != EQ_EXPR, type); })))
6957 /* PR70920: Transform (intptr_t)x eq/ne CST to x eq/ne (typeof x) CST.
6958 and (typeof ptr_cst) x eq/ne ptr_cst to x eq/ne (typeof x) CST.
6959 Disable the transform if either operand is pointer to function.
6960 This broke pr22051-2.c for arm where function pointer
6961 canonicalizaion is not wanted. */
6965 (cmp (convert @0) INTEGER_CST@1)
6966 (if (((POINTER_TYPE_P (TREE_TYPE (@0))
6967 && !FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@0)))
6968 && INTEGRAL_TYPE_P (TREE_TYPE (@1))
6969 /* Don't perform this optimization in GENERIC if @0 has reference
6970 type when sanitizing. See PR101210. */
6972 && TREE_CODE (TREE_TYPE (@0)) == REFERENCE_TYPE
6973 && (flag_sanitize & (SANITIZE_NULL | SANITIZE_ALIGNMENT))))
6974 || (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6975 && POINTER_TYPE_P (TREE_TYPE (@1))
6976 && !FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@1)))))
6977 && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
6978 (cmp @0 (convert @1)))))
6980 /* Non-equality compare simplifications from fold_binary */
6981 (for cmp (lt gt le ge)
6982 /* Comparisons with the highest or lowest possible integer of
6983 the specified precision will have known values. */
6985 (cmp (convert?@2 @0) uniform_integer_cst_p@1)
6986 (if ((INTEGRAL_TYPE_P (TREE_TYPE (@1))
6987 || POINTER_TYPE_P (TREE_TYPE (@1))
6988 || VECTOR_INTEGER_TYPE_P (TREE_TYPE (@1)))
6989 && tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@0)))
6992 tree cst = uniform_integer_cst_p (@1);
6993 tree arg1_type = TREE_TYPE (cst);
6994 unsigned int prec = TYPE_PRECISION (arg1_type);
6995 wide_int max = wi::max_value (arg1_type);
6996 wide_int signed_max = wi::max_value (prec, SIGNED);
6997 wide_int min = wi::min_value (arg1_type);
7000 (if (wi::to_wide (cst) == max)
7002 (if (cmp == GT_EXPR)
7003 { constant_boolean_node (false, type); })
7004 (if (cmp == GE_EXPR)
7006 (if (cmp == LE_EXPR)
7007 { constant_boolean_node (true, type); })
7008 (if (cmp == LT_EXPR)
7010 (if (wi::to_wide (cst) == min)
7012 (if (cmp == LT_EXPR)
7013 { constant_boolean_node (false, type); })
7014 (if (cmp == LE_EXPR)
7016 (if (cmp == GE_EXPR)
7017 { constant_boolean_node (true, type); })
7018 (if (cmp == GT_EXPR)
7020 (if (wi::to_wide (cst) == max - 1)
7022 (if (cmp == GT_EXPR)
7023 (eq @2 { build_uniform_cst (TREE_TYPE (@1),
7024 wide_int_to_tree (TREE_TYPE (cst),
7027 (if (cmp == LE_EXPR)
7028 (ne @2 { build_uniform_cst (TREE_TYPE (@1),
7029 wide_int_to_tree (TREE_TYPE (cst),
7032 (if (wi::to_wide (cst) == min + 1)
7034 (if (cmp == GE_EXPR)
7035 (ne @2 { build_uniform_cst (TREE_TYPE (@1),
7036 wide_int_to_tree (TREE_TYPE (cst),
7039 (if (cmp == LT_EXPR)
7040 (eq @2 { build_uniform_cst (TREE_TYPE (@1),
7041 wide_int_to_tree (TREE_TYPE (cst),
7044 (if (wi::to_wide (cst) == signed_max
7045 && TYPE_UNSIGNED (arg1_type)
7046 && TYPE_MODE (arg1_type) != BLKmode
7047 /* We will flip the signedness of the comparison operator
7048 associated with the mode of @1, so the sign bit is
7049 specified by this mode. Check that @1 is the signed
7050 max associated with this sign bit. */
7051 && prec == GET_MODE_PRECISION (SCALAR_INT_TYPE_MODE (arg1_type))
7052 /* signed_type does not work on pointer types. */
7053 && INTEGRAL_TYPE_P (arg1_type))
7054 /* The following case also applies to X < signed_max+1
7055 and X >= signed_max+1 because previous transformations. */
7056 (if (cmp == LE_EXPR || cmp == GT_EXPR)
7057 (with { tree st = signed_type_for (TREE_TYPE (@1)); }
7059 (if (cst == @1 && cmp == LE_EXPR)
7060 (ge (convert:st @0) { build_zero_cst (st); }))
7061 (if (cst == @1 && cmp == GT_EXPR)
7062 (lt (convert:st @0) { build_zero_cst (st); }))
7063 (if (cmp == LE_EXPR)
7064 (ge (view_convert:st @0) { build_zero_cst (st); }))
7065 (if (cmp == GT_EXPR)
7066 (lt (view_convert:st @0) { build_zero_cst (st); })))))))))))
7068 /* unsigned < (typeof unsigned)(unsigned != 0) is always false. */
7070 (lt:c @0 (convert (ne @0 integer_zerop)))
7071 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
7072 { constant_boolean_node (false, type); }))
7074 /* x != (typeof x)(x == CST) -> CST == 0 ? 1 : (CST == 1 ? (x!=0&&x!=1) : x != 0) */
7075 /* x != (typeof x)(x != CST) -> CST == 1 ? 1 : (CST == 0 ? (x!=0&&x!=1) : x != 1) */
7076 /* x == (typeof x)(x == CST) -> CST == 0 ? 0 : (CST == 1 ? (x==0||x==1) : x == 0) */
7077 /* x == (typeof x)(x != CST) -> CST == 1 ? 0 : (CST == 0 ? (x==0||x==1) : x == 1) */
7081 (outer:c @0 (convert (inner @0 INTEGER_CST@1)))
7083 bool cst1 = integer_onep (@1);
7084 bool cst0 = integer_zerop (@1);
7085 bool innereq = inner == EQ_EXPR;
7086 bool outereq = outer == EQ_EXPR;
7089 (if (innereq ? cst0 : cst1)
7090 { constant_boolean_node (!outereq, type); })
7091 (if (innereq ? cst1 : cst0)
7093 tree utype = unsigned_type_for (TREE_TYPE (@0));
7094 tree ucst1 = build_one_cst (utype);
7097 (gt (convert:utype @0) { ucst1; })
7098 (le (convert:utype @0) { ucst1; })
7103 tree value = build_int_cst (TREE_TYPE (@0), !innereq);
7116 (for cmp (unordered ordered unlt unle ungt unge uneq ltgt)
7117 /* If the second operand is NaN, the result is constant. */
7120 (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
7121 && (cmp != LTGT_EXPR || ! flag_trapping_math))
7122 { constant_boolean_node (cmp == ORDERED_EXPR || cmp == LTGT_EXPR
7123 ? false : true, type); })))
7125 /* Fold UNORDERED if either operand must be NaN, or neither can be. */
7129 (if (tree_expr_nan_p (@0) || tree_expr_nan_p (@1))
7130 { constant_boolean_node (true, type); })
7131 (if (!tree_expr_maybe_nan_p (@0) && !tree_expr_maybe_nan_p (@1))
7132 { constant_boolean_node (false, type); })))
7134 /* Fold ORDERED if either operand must be NaN, or neither can be. */
7138 (if (tree_expr_nan_p (@0) || tree_expr_nan_p (@1))
7139 { constant_boolean_node (false, type); })
7140 (if (!tree_expr_maybe_nan_p (@0) && !tree_expr_maybe_nan_p (@1))
7141 { constant_boolean_node (true, type); })))
7143 /* bool_var != 0 becomes bool_var. */
7145 (ne @0 integer_zerop)
7146 (if (TREE_CODE (TREE_TYPE (@0)) == BOOLEAN_TYPE
7147 && types_match (type, TREE_TYPE (@0)))
7149 /* bool_var == 1 becomes bool_var. */
7151 (eq @0 integer_onep)
7152 (if (TREE_CODE (TREE_TYPE (@0)) == BOOLEAN_TYPE
7153 && types_match (type, TREE_TYPE (@0)))
7156 bool_var == 0 becomes !bool_var or
7157 bool_var != 1 becomes !bool_var
7158 here because that only is good in assignment context as long
7159 as we require a tcc_comparison in GIMPLE_CONDs where we'd
7160 replace if (x == 0) with tem = ~x; if (tem != 0) which is
7161 clearly less optimal and which we'll transform again in forwprop. */
7163 /* Transform comparisons of the form (X & Y) CMP 0 to X CMP2 Z
7164 where ~Y + 1 == pow2 and Z = ~Y. */
7165 (for cst (VECTOR_CST INTEGER_CST)
7169 (cmp (bit_and:c@2 @0 cst@1) integer_zerop)
7170 (with { tree csts = bitmask_inv_cst_vector_p (@1); }
7171 (if (csts && (VECTOR_TYPE_P (TREE_TYPE (@1)) || single_use (@2)))
7172 (with { auto optab = VECTOR_TYPE_P (TREE_TYPE (@1))
7173 ? optab_vector : optab_default;
7174 tree utype = unsigned_type_for (TREE_TYPE (@1)); }
7175 (if (target_supports_op_p (utype, icmp, optab)
7176 || (optimize_vectors_before_lowering_p ()
7177 && (!target_supports_op_p (type, cmp, optab)
7178 || !target_supports_op_p (type, BIT_AND_EXPR, optab))))
7179 (if (TYPE_UNSIGNED (TREE_TYPE (@1)))
7181 (icmp (view_convert:utype @0) { csts; })))))))))
7183 /* When one argument is a constant, overflow detection can be simplified.
7184 Currently restricted to single use so as not to interfere too much with
7185 ADD_OVERFLOW detection in tree-ssa-math-opts.cc.
7186 CONVERT?(CONVERT?(A) + CST) CMP A -> A CMP' CST' */
7187 (for cmp (lt le ge gt)
7190 (cmp:c (convert?@3 (plus@2 (convert?@4 @0) INTEGER_CST@1)) @0)
7191 (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@2))
7192 && types_match (TREE_TYPE (@0), TREE_TYPE (@3))
7193 && tree_nop_conversion_p (TREE_TYPE (@4), TREE_TYPE (@0))
7194 && wi::to_wide (@1) != 0
7197 unsigned int prec = TYPE_PRECISION (TREE_TYPE (@0));
7198 signop sign = TYPE_SIGN (TREE_TYPE (@0));
7200 (out @0 { wide_int_to_tree (TREE_TYPE (@0),
7201 wi::max_value (prec, sign)
7202 - wi::to_wide (@1)); })))))
7204 /* To detect overflow in unsigned A - B, A < B is simpler than A - B > A.
7205 However, the detection logic for SUB_OVERFLOW in tree-ssa-math-opts.cc
7206 expects the long form, so we restrict the transformation for now. */
7209 (cmp:c (minus@2 @0 @1) @0)
7210 (if (single_use (@2)
7211 && ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
7212 && TYPE_UNSIGNED (TREE_TYPE (@0)))
7215 /* Optimize A - B + -1 >= A into B >= A for unsigned comparisons. */
7218 (cmp:c (plus (minus @0 @1) integer_minus_onep) @0)
7219 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
7220 && TYPE_UNSIGNED (TREE_TYPE (@0)))
7223 /* Testing for overflow is unnecessary if we already know the result. */
7228 (cmp:c (realpart (IFN_SUB_OVERFLOW@2 @0 @1)) @0)
7229 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
7230 && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
7231 (out (imagpart @2) { build_zero_cst (TREE_TYPE (@0)); }))))
7236 (cmp:c (realpart (IFN_ADD_OVERFLOW:c@2 @0 @1)) @0)
7237 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
7238 && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
7239 (out (imagpart @2) { build_zero_cst (TREE_TYPE (@0)); }))))
7241 /* For unsigned operands, -1 / B < A checks whether A * B would overflow.
7242 Simplify it to __builtin_mul_overflow (A, B, <unused>). */
7246 (cmp:c (trunc_div:s integer_all_onesp @1) @0)
7247 (if (TYPE_UNSIGNED (TREE_TYPE (@0)) && !VECTOR_TYPE_P (TREE_TYPE (@0)))
7248 (with { tree t = TREE_TYPE (@0), cpx = build_complex_type (t); }
7249 (out (imagpart (IFN_MUL_OVERFLOW:cpx @0 @1)) { build_zero_cst (t); })))))
7251 /* Similarly, for unsigned operands, (((type) A * B) >> prec) != 0 where type
7252 is at least twice as wide as type of A and B, simplify to
7253 __builtin_mul_overflow (A, B, <unused>). */
7256 (cmp (rshift (mult:s (convert@3 @0) (convert @1)) INTEGER_CST@2)
7258 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
7259 && INTEGRAL_TYPE_P (TREE_TYPE (@3))
7260 && TYPE_UNSIGNED (TREE_TYPE (@0))
7261 && (TYPE_PRECISION (TREE_TYPE (@3))
7262 >= 2 * TYPE_PRECISION (TREE_TYPE (@0)))
7263 && tree_fits_uhwi_p (@2)
7264 && tree_to_uhwi (@2) == TYPE_PRECISION (TREE_TYPE (@0))
7265 && types_match (@0, @1)
7266 && type_has_mode_precision_p (TREE_TYPE (@0))
7267 && (optab_handler (umulv4_optab, TYPE_MODE (TREE_TYPE (@0)))
7268 != CODE_FOR_nothing))
7269 (with { tree t = TREE_TYPE (@0), cpx = build_complex_type (t); }
7270 (cmp (imagpart (IFN_MUL_OVERFLOW:cpx @0 @1)) { build_zero_cst (t); })))))
7272 /* Demote operands of IFN_{ADD,SUB,MUL}_OVERFLOW. */
7273 (for ovf (IFN_ADD_OVERFLOW IFN_SUB_OVERFLOW IFN_MUL_OVERFLOW)
7275 (ovf (convert@2 @0) @1)
7276 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
7277 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
7278 && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
7279 && (!TYPE_UNSIGNED (TREE_TYPE (@2)) || TYPE_UNSIGNED (TREE_TYPE (@0))))
7282 (ovf @1 (convert@2 @0))
7283 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
7284 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
7285 && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
7286 && (!TYPE_UNSIGNED (TREE_TYPE (@2)) || TYPE_UNSIGNED (TREE_TYPE (@0))))
7289 /* Optimize __builtin_mul_overflow_p (x, cst, (utype) 0) if all 3 types
7290 are unsigned to x > (umax / cst). Similarly for signed type, but
7291 in that case it needs to be outside of a range. */
7293 (imagpart (IFN_MUL_OVERFLOW:cs@2 @0 integer_nonzerop@1))
7294 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
7295 && TYPE_MAX_VALUE (TREE_TYPE (@0))
7296 && types_match (TREE_TYPE (@0), TREE_TYPE (TREE_TYPE (@2)))
7297 && int_fits_type_p (@1, TREE_TYPE (@0)))
7298 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
7299 (convert (gt @0 (trunc_div! { TYPE_MAX_VALUE (TREE_TYPE (@0)); } @1)))
7300 (if (TYPE_MIN_VALUE (TREE_TYPE (@0)))
7301 (if (integer_minus_onep (@1))
7302 (convert (eq @0 { TYPE_MIN_VALUE (TREE_TYPE (@0)); }))
7305 tree div = fold_convert (TREE_TYPE (@0), @1);
7306 tree lo = int_const_binop (TRUNC_DIV_EXPR,
7307 TYPE_MIN_VALUE (TREE_TYPE (@0)), div);
7308 tree hi = int_const_binop (TRUNC_DIV_EXPR,
7309 TYPE_MAX_VALUE (TREE_TYPE (@0)), div);
7310 tree etype = range_check_type (TREE_TYPE (@0));
7313 if (wi::neg_p (wi::to_wide (div)))
7315 lo = fold_convert (etype, lo);
7316 hi = fold_convert (etype, hi);
7317 hi = int_const_binop (MINUS_EXPR, hi, lo);
7321 (convert (gt (minus (convert:etype @0) { lo; }) { hi; })))))))))
7323 /* Simplification of math builtins. These rules must all be optimizations
7324 as well as IL simplifications. If there is a possibility that the new
7325 form could be a pessimization, the rule should go in the canonicalization
7326 section that follows this one.
7328 Rules can generally go in this section if they satisfy one of
7331 - the rule describes an identity
7333 - the rule replaces calls with something as simple as addition or
7336 - the rule contains unary calls only and simplifies the surrounding
7337 arithmetic. (The idea here is to exclude non-unary calls in which
7338 one operand is constant and in which the call is known to be cheap
7339 when the operand has that value.) */
7341 (if (flag_unsafe_math_optimizations)
7342 /* Simplify sqrt(x) * sqrt(x) -> x. */
7344 (mult (SQRT_ALL@1 @0) @1)
7345 (if (!tree_expr_maybe_signaling_nan_p (@0))
7348 (for op (plus minus)
7349 /* Simplify (A / C) +- (B / C) -> (A +- B) / C. */
7353 (rdiv (op @0 @2) @1)))
7355 (for cmp (lt le gt ge)
7356 neg_cmp (gt ge lt le)
7357 /* Simplify (x * C1) cmp C2 -> x cmp (C2 / C1), where C1 != 0. */
7359 (cmp (mult @0 REAL_CST@1) REAL_CST@2)
7361 { tree tem = const_binop (RDIV_EXPR, type, @2, @1); }
7363 && !(REAL_VALUE_ISINF (TREE_REAL_CST (tem))
7364 || (real_zerop (tem) && !real_zerop (@1))))
7366 (if (real_less (&dconst0, TREE_REAL_CST_PTR (@1)))
7368 (if (real_less (TREE_REAL_CST_PTR (@1), &dconst0))
7369 (neg_cmp @0 { tem; })))))))
7371 /* Simplify sqrt(x) * sqrt(y) -> sqrt(x*y). */
7372 (for root (SQRT CBRT)
7374 (mult (root:s @0) (root:s @1))
7375 (root (mult @0 @1))))
7377 /* Simplify expN(x) * expN(y) -> expN(x+y). */
7378 (for exps (EXP EXP2 EXP10 POW10)
7380 (mult (exps:s @0) (exps:s @1))
7381 (exps (plus @0 @1))))
7383 /* Simplify a/root(b/c) into a*root(c/b). */
7384 (for root (SQRT CBRT)
7386 (rdiv @0 (root:s (rdiv:s @1 @2)))
7387 (mult @0 (root (rdiv @2 @1)))))
7389 /* Simplify x/expN(y) into x*expN(-y). */
7390 (for exps (EXP EXP2 EXP10 POW10)
7392 (rdiv @0 (exps:s @1))
7393 (mult @0 (exps (negate @1)))))
7395 (for logs (LOG LOG2 LOG10 LOG10)
7396 exps (EXP EXP2 EXP10 POW10)
7397 /* logN(expN(x)) -> x. */
7401 /* expN(logN(x)) -> x. */
7406 /* Optimize logN(func()) for various exponential functions. We
7407 want to determine the value "x" and the power "exponent" in
7408 order to transform logN(x**exponent) into exponent*logN(x). */
7409 (for logs (LOG LOG LOG LOG2 LOG2 LOG2 LOG10 LOG10)
7410 exps (EXP2 EXP10 POW10 EXP EXP10 POW10 EXP EXP2)
7413 (if (SCALAR_FLOAT_TYPE_P (type))
7419 /* Prepare to do logN(exp(exponent)) -> exponent*logN(e). */
7420 x = build_real_truncate (type, dconst_e ());
7423 /* Prepare to do logN(exp2(exponent)) -> exponent*logN(2). */
7424 x = build_real (type, dconst2);
7428 /* Prepare to do logN(exp10(exponent)) -> exponent*logN(10). */
7430 REAL_VALUE_TYPE dconst10;
7431 real_from_integer (&dconst10, VOIDmode, 10, SIGNED);
7432 x = build_real (type, dconst10);
7439 (mult (logs { x; }) @0)))))
7447 (if (SCALAR_FLOAT_TYPE_P (type))
7453 /* Prepare to do logN(sqrt(x)) -> 0.5*logN(x). */
7454 x = build_real (type, dconsthalf);
7457 /* Prepare to do logN(cbrt(x)) -> (1/3)*logN(x). */
7458 x = build_real_truncate (type, dconst_third ());
7464 (mult { x; } (logs @0))))))
7466 /* logN(pow(x,exponent)) -> exponent*logN(x). */
7467 (for logs (LOG LOG2 LOG10)
7471 (mult @1 (logs @0))))
7473 /* pow(C,x) -> exp(log(C)*x) if C > 0,
7474 or if C is a positive power of 2,
7475 pow(C,x) -> exp2(log2(C)*x). */
7483 (pows REAL_CST@0 @1)
7484 (if (real_compare (GT_EXPR, TREE_REAL_CST_PTR (@0), &dconst0)
7485 && real_isfinite (TREE_REAL_CST_PTR (@0))
7486 /* As libmvec doesn't have a vectorized exp2, defer optimizing
7487 the use_exp2 case until after vectorization. It seems actually
7488 beneficial for all constants to postpone this until later,
7489 because exp(log(C)*x), while faster, will have worse precision
7490 and if x folds into a constant too, that is unnecessary
7492 && canonicalize_math_after_vectorization_p ())
7494 const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (@0);
7495 bool use_exp2 = false;
7496 if (targetm.libc_has_function (function_c99_misc, TREE_TYPE (@0))
7497 && value->cl == rvc_normal)
7499 REAL_VALUE_TYPE frac_rvt = *value;
7500 SET_REAL_EXP (&frac_rvt, 1);
7501 if (real_equal (&frac_rvt, &dconst1))
7506 (if (optimize_pow_to_exp (@0, @1))
7507 (exps (mult (logs @0) @1)))
7508 (exp2s (mult (log2s @0) @1)))))))
7511 /* pow(C,x)*expN(y) -> expN(logN(C)*x+y) if C > 0. */
7513 exps (EXP EXP2 EXP10 POW10)
7514 logs (LOG LOG2 LOG10 LOG10)
7516 (mult:c (pows:s REAL_CST@0 @1) (exps:s @2))
7517 (if (real_compare (GT_EXPR, TREE_REAL_CST_PTR (@0), &dconst0)
7518 && real_isfinite (TREE_REAL_CST_PTR (@0)))
7519 (exps (plus (mult (logs @0) @1) @2)))))
7524 exps (EXP EXP2 EXP10 POW10)
7525 /* sqrt(expN(x)) -> expN(x*0.5). */
7528 (exps (mult @0 { build_real (type, dconsthalf); })))
7529 /* cbrt(expN(x)) -> expN(x/3). */
7532 (exps (mult @0 { build_real_truncate (type, dconst_third ()); })))
7533 /* pow(expN(x), y) -> expN(x*y). */
7536 (exps (mult @0 @1))))
7538 /* tan(atan(x)) -> x. */
7545 /* Simplify sin(atan(x)) -> x / sqrt(x*x + 1). */
7549 copysigns (COPYSIGN)
7554 REAL_VALUE_TYPE r_cst;
7555 build_sinatan_real (&r_cst, type);
7556 tree t_cst = build_real (type, r_cst);
7557 tree t_one = build_one_cst (type);
7559 (if (SCALAR_FLOAT_TYPE_P (type))
7560 (cond (lt (abs @0) { t_cst; })
7561 (rdiv @0 (sqrts (plus (mult @0 @0) { t_one; })))
7562 (copysigns { t_one; } @0))))))
7564 /* Simplify cos(atan(x)) -> 1 / sqrt(x*x + 1). */
7568 copysigns (COPYSIGN)
7573 REAL_VALUE_TYPE r_cst;
7574 build_sinatan_real (&r_cst, type);
7575 tree t_cst = build_real (type, r_cst);
7576 tree t_one = build_one_cst (type);
7577 tree t_zero = build_zero_cst (type);
7579 (if (SCALAR_FLOAT_TYPE_P (type))
7580 (cond (lt (abs @0) { t_cst; })
7581 (rdiv { t_one; } (sqrts (plus (mult @0 @0) { t_one; })))
7582 (copysigns { t_zero; } @0))))))
7584 (if (!flag_errno_math)
7585 /* Simplify sinh(atanh(x)) -> x / sqrt((1 - x)*(1 + x)). */
7590 (sinhs (atanhs:s @0))
7591 (with { tree t_one = build_one_cst (type); }
7592 (rdiv @0 (sqrts (mult (minus { t_one; } @0) (plus { t_one; } @0)))))))
7594 /* Simplify cosh(atanh(x)) -> 1 / sqrt((1 - x)*(1 + x)) */
7599 (coshs (atanhs:s @0))
7600 (with { tree t_one = build_one_cst (type); }
7601 (rdiv { t_one; } (sqrts (mult (minus { t_one; } @0) (plus { t_one; } @0))))))))
7603 /* cabs(x+0i) or cabs(0+xi) -> abs(x). */
7605 (CABS (complex:C @0 real_zerop@1))
7608 /* trunc(trunc(x)) -> trunc(x), etc. */
7609 (for fns (TRUNC_ALL FLOOR_ALL CEIL_ALL ROUND_ALL NEARBYINT_ALL RINT_ALL)
7613 /* f(x) -> x if x is integer valued and f does nothing for such values. */
7614 (for fns (TRUNC_ALL FLOOR_ALL CEIL_ALL ROUND_ALL NEARBYINT_ALL RINT_ALL)
7616 (fns integer_valued_real_p@0)
7619 /* hypot(x,0) and hypot(0,x) -> abs(x). */
7621 (HYPOT:c @0 real_zerop@1)
7624 /* pow(1,x) -> 1. */
7626 (POW real_onep@0 @1)
7630 /* copysign(x,x) -> x. */
7631 (COPYSIGN_ALL @0 @0)
7635 /* copysign(x,-x) -> -x. */
7636 (COPYSIGN_ALL @0 (negate@1 @0))
7640 /* copysign(x,y) -> fabs(x) if y is nonnegative. */
7641 (COPYSIGN_ALL @0 tree_expr_nonnegative_p@1)
7645 /* fabs (copysign(x, y)) -> fabs (x). */
7646 (abs (COPYSIGN_ALL @0 @1))
7649 (for scale (LDEXP SCALBN SCALBLN)
7650 /* ldexp(0, x) -> 0. */
7652 (scale real_zerop@0 @1)
7654 /* ldexp(x, 0) -> x. */
7656 (scale @0 integer_zerop@1)
7658 /* ldexp(x, y) -> x if x is +-Inf or NaN. */
7660 (scale REAL_CST@0 @1)
7661 (if (!real_isfinite (TREE_REAL_CST_PTR (@0)))
7664 /* Canonicalization of sequences of math builtins. These rules represent
7665 IL simplifications but are not necessarily optimizations.
7667 The sincos pass is responsible for picking "optimal" implementations
7668 of math builtins, which may be more complicated and can sometimes go
7669 the other way, e.g. converting pow into a sequence of sqrts.
7670 We only want to do these canonicalizations before the pass has run. */
7672 (if (flag_unsafe_math_optimizations && canonicalize_math_p ())
7673 /* Simplify tan(x) * cos(x) -> sin(x). */
7675 (mult:c (TAN:s @0) (COS:s @0))
7678 /* Simplify x * pow(x,c) -> pow(x,c+1). */
7680 (mult:c @0 (POW:s @0 REAL_CST@1))
7681 (if (!TREE_OVERFLOW (@1))
7682 (POW @0 (plus @1 { build_one_cst (type); }))))
7684 /* Simplify sin(x) / cos(x) -> tan(x). */
7686 (rdiv (SIN:s @0) (COS:s @0))
7689 /* Simplify sinh(x) / cosh(x) -> tanh(x). */
7691 (rdiv (SINH:s @0) (COSH:s @0))
7694 /* Simplify tanh (x) / sinh (x) -> 1.0 / cosh (x). */
7696 (rdiv (TANH:s @0) (SINH:s @0))
7697 (rdiv {build_one_cst (type);} (COSH @0)))
7699 /* Simplify cos(x) / sin(x) -> 1 / tan(x). */
7701 (rdiv (COS:s @0) (SIN:s @0))
7702 (rdiv { build_one_cst (type); } (TAN @0)))
7704 /* Simplify sin(x) / tan(x) -> cos(x). */
7706 (rdiv (SIN:s @0) (TAN:s @0))
7707 (if (! HONOR_NANS (@0)
7708 && ! HONOR_INFINITIES (@0))
7711 /* Simplify tan(x) / sin(x) -> 1.0 / cos(x). */
7713 (rdiv (TAN:s @0) (SIN:s @0))
7714 (if (! HONOR_NANS (@0)
7715 && ! HONOR_INFINITIES (@0))
7716 (rdiv { build_one_cst (type); } (COS @0))))
7718 /* Simplify pow(x,y) * pow(x,z) -> pow(x,y+z). */
7720 (mult (POW:s @0 @1) (POW:s @0 @2))
7721 (POW @0 (plus @1 @2)))
7723 /* Simplify pow(x,y) * pow(z,y) -> pow(x*z,y). */
7725 (mult (POW:s @0 @1) (POW:s @2 @1))
7726 (POW (mult @0 @2) @1))
7728 /* Simplify powi(x,y) * powi(z,y) -> powi(x*z,y). */
7730 (mult (POWI:s @0 @1) (POWI:s @2 @1))
7731 (POWI (mult @0 @2) @1))
7733 /* Simplify pow(x,c) / x -> pow(x,c-1). */
7735 (rdiv (POW:s @0 REAL_CST@1) @0)
7736 (if (!TREE_OVERFLOW (@1))
7737 (POW @0 (minus @1 { build_one_cst (type); }))))
7739 /* Simplify x / pow (y,z) -> x * pow(y,-z). */
7741 (rdiv @0 (POW:s @1 @2))
7742 (mult @0 (POW @1 (negate @2))))
7747 /* sqrt(sqrt(x)) -> pow(x,1/4). */
7750 (pows @0 { build_real (type, dconst_quarter ()); }))
7751 /* sqrt(cbrt(x)) -> pow(x,1/6). */
7754 (pows @0 { build_real_truncate (type, dconst_sixth ()); }))
7755 /* cbrt(sqrt(x)) -> pow(x,1/6). */
7758 (pows @0 { build_real_truncate (type, dconst_sixth ()); }))
7759 /* cbrt(cbrt(x)) -> pow(x,1/9), iff x is nonnegative. */
7761 (cbrts (cbrts tree_expr_nonnegative_p@0))
7762 (pows @0 { build_real_truncate (type, dconst_ninth ()); }))
7763 /* sqrt(pow(x,y)) -> pow(|x|,y*0.5). */
7765 (sqrts (pows @0 @1))
7766 (pows (abs @0) (mult @1 { build_real (type, dconsthalf); })))
7767 /* cbrt(pow(x,y)) -> pow(x,y/3), iff x is nonnegative. */
7769 (cbrts (pows tree_expr_nonnegative_p@0 @1))
7770 (pows @0 (mult @1 { build_real_truncate (type, dconst_third ()); })))
7771 /* pow(sqrt(x),y) -> pow(x,y*0.5). */
7773 (pows (sqrts @0) @1)
7774 (pows @0 (mult @1 { build_real (type, dconsthalf); })))
7775 /* pow(cbrt(x),y) -> pow(x,y/3) iff x is nonnegative. */
7777 (pows (cbrts tree_expr_nonnegative_p@0) @1)
7778 (pows @0 (mult @1 { build_real_truncate (type, dconst_third ()); })))
7779 /* pow(pow(x,y),z) -> pow(x,y*z) iff x is nonnegative. */
7781 (pows (pows tree_expr_nonnegative_p@0 @1) @2)
7782 (pows @0 (mult @1 @2))))
7784 /* cabs(x+xi) -> fabs(x)*sqrt(2). */
7786 (CABS (complex @0 @0))
7787 (mult (abs @0) { build_real_truncate (type, dconst_sqrt2 ()); }))
7789 /* hypot(x,x) -> fabs(x)*sqrt(2). */
7792 (mult (abs @0) { build_real_truncate (type, dconst_sqrt2 ()); }))
7794 /* cexp(x+yi) -> exp(x)*cexpi(y). */
7799 (cexps compositional_complex@0)
7800 (if (targetm.libc_has_function (function_c99_math_complex, TREE_TYPE (@0)))
7802 (mult (exps@1 (realpart @0)) (realpart (cexpis:type@2 (imagpart @0))))
7803 (mult @1 (imagpart @2)))))))
7805 (if (canonicalize_math_p ())
7806 /* floor(x) -> trunc(x) if x is nonnegative. */
7807 (for floors (FLOOR_ALL)
7810 (floors tree_expr_nonnegative_p@0)
7813 (match double_value_p
7815 (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == double_type_node)))
7816 (for froms (BUILT_IN_TRUNCL
7828 /* truncl(extend(x)) -> extend(trunc(x)), etc., if x is a double. */
7829 (if (optimize && canonicalize_math_p ())
7831 (froms (convert double_value_p@0))
7832 (convert (tos @0)))))
7834 (match float_value_p
7836 (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == float_type_node)))
7837 (for froms (BUILT_IN_TRUNCL BUILT_IN_TRUNC
7838 BUILT_IN_FLOORL BUILT_IN_FLOOR
7839 BUILT_IN_CEILL BUILT_IN_CEIL
7840 BUILT_IN_ROUNDL BUILT_IN_ROUND
7841 BUILT_IN_NEARBYINTL BUILT_IN_NEARBYINT
7842 BUILT_IN_RINTL BUILT_IN_RINT)
7843 tos (BUILT_IN_TRUNCF BUILT_IN_TRUNCF
7844 BUILT_IN_FLOORF BUILT_IN_FLOORF
7845 BUILT_IN_CEILF BUILT_IN_CEILF
7846 BUILT_IN_ROUNDF BUILT_IN_ROUNDF
7847 BUILT_IN_NEARBYINTF BUILT_IN_NEARBYINTF
7848 BUILT_IN_RINTF BUILT_IN_RINTF)
7849 /* truncl(extend(x)) and trunc(extend(x)) -> extend(truncf(x)), etc.,
7851 (if (optimize && canonicalize_math_p ()
7852 && targetm.libc_has_function (function_c99_misc, NULL_TREE))
7854 (froms (convert float_value_p@0))
7855 (convert (tos @0)))))
7858 (match float16_value_p
7860 (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == float16_type_node)))
7861 (for froms (BUILT_IN_TRUNCL BUILT_IN_TRUNC BUILT_IN_TRUNCF
7862 BUILT_IN_FLOORL BUILT_IN_FLOOR BUILT_IN_FLOORF
7863 BUILT_IN_CEILL BUILT_IN_CEIL BUILT_IN_CEILF
7864 BUILT_IN_ROUNDEVENL BUILT_IN_ROUNDEVEN BUILT_IN_ROUNDEVENF
7865 BUILT_IN_ROUNDL BUILT_IN_ROUND BUILT_IN_ROUNDF
7866 BUILT_IN_NEARBYINTL BUILT_IN_NEARBYINT BUILT_IN_NEARBYINTF
7867 BUILT_IN_RINTL BUILT_IN_RINT BUILT_IN_RINTF
7868 BUILT_IN_SQRTL BUILT_IN_SQRT BUILT_IN_SQRTF)
7869 tos (IFN_TRUNC IFN_TRUNC IFN_TRUNC
7870 IFN_FLOOR IFN_FLOOR IFN_FLOOR
7871 IFN_CEIL IFN_CEIL IFN_CEIL
7872 IFN_ROUNDEVEN IFN_ROUNDEVEN IFN_ROUNDEVEN
7873 IFN_ROUND IFN_ROUND IFN_ROUND
7874 IFN_NEARBYINT IFN_NEARBYINT IFN_NEARBYINT
7875 IFN_RINT IFN_RINT IFN_RINT
7876 IFN_SQRT IFN_SQRT IFN_SQRT)
7877 /* (_Float16) round ((doube) x) -> __built_in_roundf16 (x), etc.,
7878 if x is a _Float16. */
7880 (convert (froms (convert float16_value_p@0)))
7882 && types_match (type, TREE_TYPE (@0))
7883 && direct_internal_fn_supported_p (as_internal_fn (tos),
7884 type, OPTIMIZE_FOR_BOTH))
7887 /* Simplify (trunc)copysign ((extend)x, (extend)y) to copysignf (x, y),
7888 x,y is float value, similar for _Float16/double. */
7889 (for copysigns (COPYSIGN_ALL)
7891 (convert (copysigns (convert@2 @0) (convert @1)))
7893 && !HONOR_SNANS (@2)
7894 && types_match (type, TREE_TYPE (@0))
7895 && types_match (type, TREE_TYPE (@1))
7896 && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (@2))
7897 && direct_internal_fn_supported_p (IFN_COPYSIGN,
7898 type, OPTIMIZE_FOR_BOTH))
7899 (IFN_COPYSIGN @0 @1))))
7901 (for froms (BUILT_IN_FMAF BUILT_IN_FMA BUILT_IN_FMAL)
7902 tos (IFN_FMA IFN_FMA IFN_FMA)
7904 (convert (froms (convert@3 @0) (convert @1) (convert @2)))
7905 (if (flag_unsafe_math_optimizations
7907 && FLOAT_TYPE_P (type)
7908 && FLOAT_TYPE_P (TREE_TYPE (@3))
7909 && types_match (type, TREE_TYPE (@0))
7910 && types_match (type, TREE_TYPE (@1))
7911 && types_match (type, TREE_TYPE (@2))
7912 && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (@3))
7913 && direct_internal_fn_supported_p (as_internal_fn (tos),
7914 type, OPTIMIZE_FOR_BOTH))
7917 (for maxmin (max min)
7919 (convert (maxmin (convert@2 @0) (convert @1)))
7921 && FLOAT_TYPE_P (type)
7922 && FLOAT_TYPE_P (TREE_TYPE (@2))
7923 && types_match (type, TREE_TYPE (@0))
7924 && types_match (type, TREE_TYPE (@1))
7925 && element_precision (type) < element_precision (TREE_TYPE (@2)))
7929 (for froms (XFLOORL XCEILL XROUNDL XRINTL)
7930 tos (XFLOOR XCEIL XROUND XRINT)
7931 /* llfloorl(extend(x)) -> llfloor(x), etc., if x is a double. */
7932 (if (optimize && canonicalize_math_p ())
7934 (froms (convert double_value_p@0))
7937 (for froms (XFLOORL XCEILL XROUNDL XRINTL
7938 XFLOOR XCEIL XROUND XRINT)
7939 tos (XFLOORF XCEILF XROUNDF XRINTF)
7940 /* llfloorl(extend(x)) and llfloor(extend(x)) -> llfloorf(x), etc.,
7942 (if (optimize && canonicalize_math_p ())
7944 (froms (convert float_value_p@0))
7947 (if (canonicalize_math_p ())
7948 /* xfloor(x) -> fix_trunc(x) if x is nonnegative. */
7949 (for floors (IFLOOR LFLOOR LLFLOOR)
7951 (floors tree_expr_nonnegative_p@0)
7954 (if (canonicalize_math_p ())
7955 /* xfloor(x) -> fix_trunc(x), etc., if x is integer valued. */
7956 (for fns (IFLOOR LFLOOR LLFLOOR
7958 IROUND LROUND LLROUND)
7960 (fns integer_valued_real_p@0)
7962 (if (!flag_errno_math)
7963 /* xrint(x) -> fix_trunc(x), etc., if x is integer valued. */
7964 (for rints (IRINT LRINT LLRINT)
7966 (rints integer_valued_real_p@0)
7969 (if (canonicalize_math_p ())
7970 (for ifn (IFLOOR ICEIL IROUND IRINT)
7971 lfn (LFLOOR LCEIL LROUND LRINT)
7972 llfn (LLFLOOR LLCEIL LLROUND LLRINT)
7973 /* Canonicalize iround (x) to lround (x) on ILP32 targets where
7974 sizeof (int) == sizeof (long). */
7975 (if (TYPE_PRECISION (integer_type_node)
7976 == TYPE_PRECISION (long_integer_type_node))
7979 (lfn:long_integer_type_node @0)))
7980 /* Canonicalize llround (x) to lround (x) on LP64 targets where
7981 sizeof (long long) == sizeof (long). */
7982 (if (TYPE_PRECISION (long_long_integer_type_node)
7983 == TYPE_PRECISION (long_integer_type_node))
7986 (lfn:long_integer_type_node @0)))))
7988 /* cproj(x) -> x if we're ignoring infinities. */
7991 (if (!HONOR_INFINITIES (type))
7994 /* If the real part is inf and the imag part is known to be
7995 nonnegative, return (inf + 0i). */
7997 (CPROJ (complex REAL_CST@0 tree_expr_nonnegative_p@1))
7998 (if (real_isinf (TREE_REAL_CST_PTR (@0)))
7999 { build_complex_inf (type, false); }))
8001 /* If the imag part is inf, return (inf+I*copysign(0,imag)). */
8003 (CPROJ (complex @0 REAL_CST@1))
8004 (if (real_isinf (TREE_REAL_CST_PTR (@1)))
8005 { build_complex_inf (type, TREE_REAL_CST_PTR (@1)->sign); }))
8011 (pows @0 REAL_CST@1)
8013 const REAL_VALUE_TYPE *value = TREE_REAL_CST_PTR (@1);
8014 REAL_VALUE_TYPE tmp;
8017 /* pow(x,0) -> 1. */
8018 (if (real_equal (value, &dconst0))
8019 { build_real (type, dconst1); })
8020 /* pow(x,1) -> x. */
8021 (if (real_equal (value, &dconst1))
8023 /* pow(x,-1) -> 1/x. */
8024 (if (real_equal (value, &dconstm1))
8025 (rdiv { build_real (type, dconst1); } @0))
8026 /* pow(x,0.5) -> sqrt(x). */
8027 (if (flag_unsafe_math_optimizations
8028 && canonicalize_math_p ()
8029 && real_equal (value, &dconsthalf))
8031 /* pow(x,1/3) -> cbrt(x). */
8032 (if (flag_unsafe_math_optimizations
8033 && canonicalize_math_p ()
8034 && (tmp = real_value_truncate (TYPE_MODE (type), dconst_third ()),
8035 real_equal (value, &tmp)))
8038 /* powi(1,x) -> 1. */
8040 (POWI real_onep@0 @1)
8044 (POWI @0 INTEGER_CST@1)
8046 /* powi(x,0) -> 1. */
8047 (if (wi::to_wide (@1) == 0)
8048 { build_real (type, dconst1); })
8049 /* powi(x,1) -> x. */
8050 (if (wi::to_wide (@1) == 1)
8052 /* powi(x,-1) -> 1/x. */
8053 (if (wi::to_wide (@1) == -1)
8054 (rdiv { build_real (type, dconst1); } @0))))
8056 /* Narrowing of arithmetic and logical operations.
8058 These are conceptually similar to the transformations performed for
8059 the C/C++ front-ends by shorten_binary_op and shorten_compare. Long
8060 term we want to move all that code out of the front-ends into here. */
8062 /* Convert (outertype)((innertype0)a+(innertype1)b)
8063 into ((newtype)a+(newtype)b) where newtype
8064 is the widest mode from all of these. */
8065 (for op (plus minus mult rdiv)
8067 (convert (op:s@0 (convert1?@3 @1) (convert2?@4 @2)))
8068 /* If we have a narrowing conversion of an arithmetic operation where
8069 both operands are widening conversions from the same type as the outer
8070 narrowing conversion. Then convert the innermost operands to a
8071 suitable unsigned type (to avoid introducing undefined behavior),
8072 perform the operation and convert the result to the desired type. */
8073 (if (INTEGRAL_TYPE_P (type)
8076 /* We check for type compatibility between @0 and @1 below,
8077 so there's no need to check that @2/@4 are integral types. */
8078 && INTEGRAL_TYPE_P (TREE_TYPE (@1))
8079 && INTEGRAL_TYPE_P (TREE_TYPE (@3))
8080 /* The precision of the type of each operand must match the
8081 precision of the mode of each operand, similarly for the
8083 && type_has_mode_precision_p (TREE_TYPE (@1))
8084 && type_has_mode_precision_p (TREE_TYPE (@2))
8085 && type_has_mode_precision_p (type)
8086 /* The inner conversion must be a widening conversion. */
8087 && TYPE_PRECISION (TREE_TYPE (@3)) > TYPE_PRECISION (TREE_TYPE (@1))
8088 && types_match (@1, type)
8089 && (types_match (@1, @2)
8090 /* Or the second operand is const integer or converted const
8091 integer from valueize. */
8092 || poly_int_tree_p (@4)))
8093 (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
8094 (op @1 (convert @2))
8095 (with { tree utype = unsigned_type_for (TREE_TYPE (@1)); }
8096 (convert (op (convert:utype @1)
8097 (convert:utype @2)))))
8098 (if (FLOAT_TYPE_P (type)
8099 && DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0))
8100 == DECIMAL_FLOAT_TYPE_P (type))
8101 (with { tree arg0 = strip_float_extensions (@1);
8102 tree arg1 = strip_float_extensions (@2);
8103 tree itype = TREE_TYPE (@0);
8104 tree ty1 = TREE_TYPE (arg0);
8105 tree ty2 = TREE_TYPE (arg1);
8106 enum tree_code code = TREE_CODE (itype); }
8107 (if (FLOAT_TYPE_P (ty1)
8108 && FLOAT_TYPE_P (ty2))
8109 (with { tree newtype = type;
8110 if (TYPE_MODE (ty1) == SDmode
8111 || TYPE_MODE (ty2) == SDmode
8112 || TYPE_MODE (type) == SDmode)
8113 newtype = dfloat32_type_node;
8114 if (TYPE_MODE (ty1) == DDmode
8115 || TYPE_MODE (ty2) == DDmode
8116 || TYPE_MODE (type) == DDmode)
8117 newtype = dfloat64_type_node;
8118 if (TYPE_MODE (ty1) == TDmode
8119 || TYPE_MODE (ty2) == TDmode
8120 || TYPE_MODE (type) == TDmode)
8121 newtype = dfloat128_type_node; }
8122 (if ((newtype == dfloat32_type_node
8123 || newtype == dfloat64_type_node
8124 || newtype == dfloat128_type_node)
8126 && types_match (newtype, type))
8127 (op (convert:newtype @1) (convert:newtype @2))
8128 (with { if (element_precision (ty1) > element_precision (newtype))
8130 if (element_precision (ty2) > element_precision (newtype))
8132 /* Sometimes this transformation is safe (cannot
8133 change results through affecting double rounding
8134 cases) and sometimes it is not. If NEWTYPE is
8135 wider than TYPE, e.g. (float)((long double)double
8136 + (long double)double) converted to
8137 (float)(double + double), the transformation is
8138 unsafe regardless of the details of the types
8139 involved; double rounding can arise if the result
8140 of NEWTYPE arithmetic is a NEWTYPE value half way
8141 between two representable TYPE values but the
8142 exact value is sufficiently different (in the
8143 right direction) for this difference to be
8144 visible in ITYPE arithmetic. If NEWTYPE is the
8145 same as TYPE, however, the transformation may be
8146 safe depending on the types involved: it is safe
8147 if the ITYPE has strictly more than twice as many
8148 mantissa bits as TYPE, can represent infinities
8149 and NaNs if the TYPE can, and has sufficient
8150 exponent range for the product or ratio of two
8151 values representable in the TYPE to be within the
8152 range of normal values of ITYPE. */
8153 (if (element_precision (newtype) < element_precision (itype)
8154 && (!VECTOR_MODE_P (TYPE_MODE (newtype))
8155 || target_supports_op_p (newtype, op, optab_default))
8156 && (flag_unsafe_math_optimizations
8157 || (element_precision (newtype) == element_precision (type)
8158 && real_can_shorten_arithmetic (element_mode (itype),
8159 element_mode (type))
8160 && !excess_precision_type (newtype)))
8161 && !types_match (itype, newtype))
8162 (convert:type (op (convert:newtype @1)
8163 (convert:newtype @2)))
8168 /* This is another case of narrowing, specifically when there's an outer
8169 BIT_AND_EXPR which masks off bits outside the type of the innermost
8170 operands. Like the previous case we have to convert the operands
8171 to unsigned types to avoid introducing undefined behavior for the
8172 arithmetic operation. */
8173 (for op (minus plus)
8175 (bit_and (op:s (convert@2 @0) (convert@3 @1)) INTEGER_CST@4)
8176 (if (INTEGRAL_TYPE_P (type)
8177 /* We check for type compatibility between @0 and @1 below,
8178 so there's no need to check that @1/@3 are integral types. */
8179 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
8180 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
8181 /* The precision of the type of each operand must match the
8182 precision of the mode of each operand, similarly for the
8184 && type_has_mode_precision_p (TREE_TYPE (@0))
8185 && type_has_mode_precision_p (TREE_TYPE (@1))
8186 && type_has_mode_precision_p (type)
8187 /* The inner conversion must be a widening conversion. */
8188 && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
8189 && types_match (@0, @1)
8190 && (tree_int_cst_min_precision (@4, TYPE_SIGN (TREE_TYPE (@0)))
8191 <= TYPE_PRECISION (TREE_TYPE (@0)))
8192 && (wi::to_wide (@4)
8193 & wi::mask (TYPE_PRECISION (TREE_TYPE (@0)),
8194 true, TYPE_PRECISION (type))) == 0)
8195 (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
8196 (with { tree ntype = TREE_TYPE (@0); }
8197 (convert (bit_and (op @0 @1) (convert:ntype @4))))
8198 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
8199 (convert (bit_and (op (convert:utype @0) (convert:utype @1))
8200 (convert:utype @4))))))))
8202 /* Transform (@0 < @1 and @0 < @2) to use min,
8203 (@0 > @1 and @0 > @2) to use max */
8204 (for logic (bit_and bit_and bit_and bit_and bit_ior bit_ior bit_ior bit_ior)
8205 op (lt le gt ge lt le gt ge )
8206 ext (min min max max max max min min )
8208 (logic (op:cs @0 @1) (op:cs @0 @2))
8209 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
8210 && TREE_CODE (@0) != INTEGER_CST)
8211 (op @0 (ext @1 @2)))))
8213 /* Max<bool0, bool1> -> bool0 | bool1
8214 Min<bool0, bool1> -> bool0 & bool1 */
8216 logic (bit_ior bit_and)
8218 (op zero_one_valued_p@0 zero_one_valued_p@1)
8221 /* signbit(x) != 0 ? -x : x -> abs(x)
8222 signbit(x) == 0 ? -x : x -> -abs(x) */
8226 (cond (neeq (sign @0) integer_zerop) (negate @0) @0)
8227 (if (neeq == NE_EXPR)
8229 (negate (abs @0))))))
8232 /* signbit(x) -> 0 if x is nonnegative. */
8233 (SIGNBIT tree_expr_nonnegative_p@0)
8234 { integer_zero_node; })
8237 /* signbit(x) -> x<0 if x doesn't have signed zeros. */
8239 (if (!HONOR_SIGNED_ZEROS (@0))
8240 (convert (lt @0 { build_real (TREE_TYPE (@0), dconst0); }))))
8242 /* Transform comparisons of the form X +- C1 CMP C2 to X CMP C2 -+ C1. */
8244 (for op (plus minus)
8247 (cmp (op@3 @0 INTEGER_CST@1) INTEGER_CST@2)
8248 (if (!TREE_OVERFLOW (@1) && !TREE_OVERFLOW (@2)
8249 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0))
8250 && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@0))
8251 && !TYPE_SATURATING (TREE_TYPE (@0)))
8252 (with { tree res = int_const_binop (rop, @2, @1); }
8253 (if (TREE_OVERFLOW (res)
8254 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
8255 { constant_boolean_node (cmp == NE_EXPR, type); }
8256 (if (single_use (@3))
8257 (cmp @0 { TREE_OVERFLOW (res)
8258 ? drop_tree_overflow (res) : res; }))))))))
8259 (for cmp (lt le gt ge)
8260 (for op (plus minus)
8263 (cmp (op@3 @0 INTEGER_CST@1) INTEGER_CST@2)
8264 (if (!TREE_OVERFLOW (@1) && !TREE_OVERFLOW (@2)
8265 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
8266 (with { tree res = int_const_binop (rop, @2, @1); }
8267 (if (TREE_OVERFLOW (res))
8269 fold_overflow_warning (("assuming signed overflow does not occur "
8270 "when simplifying conditional to constant"),
8271 WARN_STRICT_OVERFLOW_CONDITIONAL);
8272 bool less = cmp == LE_EXPR || cmp == LT_EXPR;
8273 /* wi::ges_p (@2, 0) should be sufficient for a signed type. */
8274 bool ovf_high = wi::lt_p (wi::to_wide (@1), 0,
8275 TYPE_SIGN (TREE_TYPE (@1)))
8276 != (op == MINUS_EXPR);
8277 constant_boolean_node (less == ovf_high, type);
8279 (if (single_use (@3))
8282 fold_overflow_warning (("assuming signed overflow does not occur "
8283 "when changing X +- C1 cmp C2 to "
8285 WARN_STRICT_OVERFLOW_COMPARISON);
8287 (cmp @0 { res; })))))))))
8289 /* Canonicalizations of BIT_FIELD_REFs. */
8292 (BIT_FIELD_REF (BIT_FIELD_REF @0 @1 @2) @3 @4)
8293 (BIT_FIELD_REF @0 @3 { const_binop (PLUS_EXPR, bitsizetype, @2, @4); }))
8296 (BIT_FIELD_REF (view_convert @0) @1 @2)
8297 (if (! INTEGRAL_TYPE_P (TREE_TYPE (@0))
8298 || type_has_mode_precision_p (TREE_TYPE (@0)))
8299 (BIT_FIELD_REF @0 @1 @2)))
8302 (BIT_FIELD_REF @0 @1 integer_zerop)
8303 (if (tree_int_cst_equal (@1, TYPE_SIZE (TREE_TYPE (@0))))
8307 (BIT_FIELD_REF @0 @1 @2)
8309 (if (TREE_CODE (TREE_TYPE (@0)) == COMPLEX_TYPE
8310 && tree_int_cst_equal (@1, TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))))
8312 (if (integer_zerop (@2))
8313 (view_convert (realpart @0)))
8314 (if (tree_int_cst_equal (@2, TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))))
8315 (view_convert (imagpart @0)))))
8316 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
8317 && INTEGRAL_TYPE_P (type)
8318 /* On GIMPLE this should only apply to register arguments. */
8319 && (! GIMPLE || is_gimple_reg (@0))
8320 /* A bit-field-ref that referenced the full argument can be stripped. */
8321 && ((compare_tree_int (@1, TYPE_PRECISION (TREE_TYPE (@0))) == 0
8322 && integer_zerop (@2))
8323 /* Low-parts can be reduced to integral conversions.
8324 ??? The following doesn't work for PDP endian. */
8325 || (BYTES_BIG_ENDIAN == WORDS_BIG_ENDIAN
8326 /* But only do this after vectorization. */
8327 && canonicalize_math_after_vectorization_p ()
8328 /* Don't even think about BITS_BIG_ENDIAN. */
8329 && TYPE_PRECISION (TREE_TYPE (@0)) % BITS_PER_UNIT == 0
8330 && TYPE_PRECISION (type) % BITS_PER_UNIT == 0
8331 && compare_tree_int (@2, (BYTES_BIG_ENDIAN
8332 ? (TYPE_PRECISION (TREE_TYPE (@0))
8333 - TYPE_PRECISION (type))
8337 /* Simplify vector extracts. */
8340 (BIT_FIELD_REF CONSTRUCTOR@0 @1 @2)
8341 (if (VECTOR_TYPE_P (TREE_TYPE (@0))
8342 && tree_fits_uhwi_p (TYPE_SIZE (type))
8343 && ((tree_to_uhwi (TYPE_SIZE (type))
8344 == tree_to_uhwi (TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))))
8345 || (VECTOR_TYPE_P (type)
8346 && (tree_to_uhwi (TYPE_SIZE (TREE_TYPE (type)))
8347 == tree_to_uhwi (TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0))))))))
8350 tree ctor = (TREE_CODE (@0) == SSA_NAME
8351 ? gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0)) : @0);
8352 tree eltype = TREE_TYPE (TREE_TYPE (ctor));
8353 unsigned HOST_WIDE_INT width = tree_to_uhwi (TYPE_SIZE (eltype));
8354 unsigned HOST_WIDE_INT n = tree_to_uhwi (@1);
8355 unsigned HOST_WIDE_INT idx = tree_to_uhwi (@2);
8358 && (idx % width) == 0
8360 && known_le ((idx + n) / width,
8361 TYPE_VECTOR_SUBPARTS (TREE_TYPE (ctor))))
8366 /* Constructor elements can be subvectors. */
8368 if (CONSTRUCTOR_NELTS (ctor) != 0)
8370 tree cons_elem = TREE_TYPE (CONSTRUCTOR_ELT (ctor, 0)->value);
8371 if (TREE_CODE (cons_elem) == VECTOR_TYPE)
8372 k = TYPE_VECTOR_SUBPARTS (cons_elem);
8374 unsigned HOST_WIDE_INT elt, count, const_k;
8377 /* We keep an exact subset of the constructor elements. */
8378 (if (multiple_p (idx, k, &elt) && multiple_p (n, k, &count))
8379 (if (CONSTRUCTOR_NELTS (ctor) == 0)
8380 { build_zero_cst (type); }
8382 (if (elt < CONSTRUCTOR_NELTS (ctor))
8383 (view_convert { CONSTRUCTOR_ELT (ctor, elt)->value; })
8384 { build_zero_cst (type); })
8385 /* We don't want to emit new CTORs unless the old one goes away.
8386 ??? Eventually allow this if the CTOR ends up constant or
8388 (if (single_use (@0))
8391 vec<constructor_elt, va_gc> *vals;
8392 vec_alloc (vals, count);
8393 bool constant_p = true;
8395 for (unsigned i = 0;
8396 i < count && elt + i < CONSTRUCTOR_NELTS (ctor); ++i)
8398 tree e = CONSTRUCTOR_ELT (ctor, elt + i)->value;
8399 CONSTRUCTOR_APPEND_ELT (vals, NULL_TREE, e);
8400 if (!CONSTANT_CLASS_P (e))
8403 tree evtype = (types_match (TREE_TYPE (type),
8404 TREE_TYPE (TREE_TYPE (ctor)))
8406 : build_vector_type (TREE_TYPE (TREE_TYPE (ctor)),
8408 /* We used to build a CTOR in the non-constant case here
8409 but that's not a GIMPLE value. We'd have to expose this
8410 operation somehow so the code generation can properly
8411 split it out to a separate stmt. */
8412 res = (constant_p ? build_vector_from_ctor (evtype, vals)
8413 : (GIMPLE ? NULL_TREE : build_constructor (evtype, vals)));
8416 (view_convert { res; })))))))
8417 /* The bitfield references a single constructor element. */
8418 (if (k.is_constant (&const_k)
8419 && idx + n <= (idx / const_k + 1) * const_k)
8421 (if (CONSTRUCTOR_NELTS (ctor) <= idx / const_k)
8422 { build_zero_cst (type); })
8424 (view_convert { CONSTRUCTOR_ELT (ctor, idx / const_k)->value; }))
8425 (BIT_FIELD_REF { CONSTRUCTOR_ELT (ctor, idx / const_k)->value; }
8426 @1 { bitsize_int ((idx % const_k) * width); })))))))))
8428 /* Simplify a bit extraction from a bit insertion for the cases with
8429 the inserted element fully covering the extraction or the insertion
8430 not touching the extraction. */
8432 (BIT_FIELD_REF (bit_insert @0 @1 @ipos) @rsize @rpos)
8435 unsigned HOST_WIDE_INT isize;
8436 if (INTEGRAL_TYPE_P (TREE_TYPE (@1)))
8437 isize = TYPE_PRECISION (TREE_TYPE (@1));
8439 isize = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (@1)));
8442 (if ((!INTEGRAL_TYPE_P (TREE_TYPE (@1))
8443 || type_has_mode_precision_p (TREE_TYPE (@1)))
8444 && wi::leu_p (wi::to_wide (@ipos), wi::to_wide (@rpos))
8445 && wi::leu_p (wi::to_wide (@rpos) + wi::to_wide (@rsize),
8446 wi::to_wide (@ipos) + isize))
8447 (BIT_FIELD_REF @1 @rsize { wide_int_to_tree (bitsizetype,
8449 - wi::to_wide (@ipos)); }))
8450 (if (wi::eq_p (wi::to_wide (@ipos), wi::to_wide (@rpos))
8451 && compare_tree_int (@rsize, isize) == 0)
8453 (if (wi::geu_p (wi::to_wide (@ipos),
8454 wi::to_wide (@rpos) + wi::to_wide (@rsize))
8455 || wi::geu_p (wi::to_wide (@rpos),
8456 wi::to_wide (@ipos) + isize))
8457 (BIT_FIELD_REF @0 @rsize @rpos)))))
8459 /* Simplify vector inserts of other vector extracts to a permute. */
8461 (bit_insert @0 (BIT_FIELD_REF@2 @1 @rsize @rpos) @ipos)
8462 (if (VECTOR_TYPE_P (type)
8463 && types_match (@0, @1)
8464 && types_match (TREE_TYPE (TREE_TYPE (@0)), TREE_TYPE (@2))
8465 && TYPE_VECTOR_SUBPARTS (type).is_constant ())
8468 unsigned HOST_WIDE_INT elsz
8469 = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (TREE_TYPE (@1))));
8470 poly_uint64 relt = exact_div (tree_to_poly_uint64 (@rpos), elsz);
8471 poly_uint64 ielt = exact_div (tree_to_poly_uint64 (@ipos), elsz);
8472 unsigned nunits = TYPE_VECTOR_SUBPARTS (type).to_constant ();
8473 vec_perm_builder builder;
8474 builder.new_vector (nunits, nunits, 1);
8475 for (unsigned i = 0; i < nunits; ++i)
8476 builder.quick_push (known_eq (ielt, i) ? nunits + relt : i);
8477 vec_perm_indices sel (builder, 2, nunits);
8479 (if (!VECTOR_MODE_P (TYPE_MODE (type))
8480 || can_vec_perm_const_p (TYPE_MODE (type), TYPE_MODE (type), sel, false))
8481 (vec_perm @0 @1 { vec_perm_indices_to_tree
8482 (build_vector_type (ssizetype, nunits), sel); })))))
8484 (if (canonicalize_math_after_vectorization_p ())
8487 (fmas:c (negate @0) @1 @2)
8488 (IFN_FNMA @0 @1 @2))
8490 (fmas @0 @1 (negate @2))
8493 (fmas:c (negate @0) @1 (negate @2))
8494 (IFN_FNMS @0 @1 @2))
8496 (negate (fmas@3 @0 @1 @2))
8497 (if (single_use (@3))
8498 (IFN_FNMS @0 @1 @2))))
8501 (IFN_FMS:c (negate @0) @1 @2)
8502 (IFN_FNMS @0 @1 @2))
8504 (IFN_FMS @0 @1 (negate @2))
8507 (IFN_FMS:c (negate @0) @1 (negate @2))
8508 (IFN_FNMA @0 @1 @2))
8510 (negate (IFN_FMS@3 @0 @1 @2))
8511 (if (single_use (@3))
8512 (IFN_FNMA @0 @1 @2)))
8515 (IFN_FNMA:c (negate @0) @1 @2)
8518 (IFN_FNMA @0 @1 (negate @2))
8519 (IFN_FNMS @0 @1 @2))
8521 (IFN_FNMA:c (negate @0) @1 (negate @2))
8524 (negate (IFN_FNMA@3 @0 @1 @2))
8525 (if (single_use (@3))
8526 (IFN_FMS @0 @1 @2)))
8529 (IFN_FNMS:c (negate @0) @1 @2)
8532 (IFN_FNMS @0 @1 (negate @2))
8533 (IFN_FNMA @0 @1 @2))
8535 (IFN_FNMS:c (negate @0) @1 (negate @2))
8538 (negate (IFN_FNMS@3 @0 @1 @2))
8539 (if (single_use (@3))
8540 (IFN_FMA @0 @1 @2))))
8542 /* CLZ simplifications. */
8547 (op (clz:s@2 @0) INTEGER_CST@1)
8548 (if (integer_zerop (@1) && single_use (@2))
8549 /* clz(X) == 0 is (int)X < 0 and clz(X) != 0 is (int)X >= 0. */
8550 (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
8551 (cmp (convert:stype @0) { build_zero_cst (stype); }))
8552 /* clz(X) == (prec-1) is X == 1 and clz(X) != (prec-1) is X != 1. */
8553 (if (wi::to_wide (@1) == TYPE_PRECISION (TREE_TYPE (@0)) - 1)
8554 (op @0 { build_one_cst (TREE_TYPE (@0)); }))))))
8558 (op (IFN_CLZ:s@2 @0 @3) INTEGER_CST@1)
8559 (if (integer_zerop (@1) && single_use (@2))
8560 /* clz(X) == 0 is (int)X < 0 and clz(X) != 0 is (int)X >= 0. */
8561 (with { tree type0 = TREE_TYPE (@0);
8562 tree stype = signed_type_for (TREE_TYPE (@0));
8563 /* Punt if clz(0) == 0. */
8564 if (integer_zerop (@3))
8568 (cmp (convert:stype @0) { build_zero_cst (stype); })))
8569 /* clz(X) == (prec-1) is X == 1 and clz(X) != (prec-1) is X != 1. */
8570 (with { bool ok = true;
8571 tree type0 = TREE_TYPE (@0);
8572 /* Punt if clz(0) == prec - 1. */
8573 if (wi::to_widest (@3) == TYPE_PRECISION (type0) - 1)
8576 (if (ok && wi::to_wide (@1) == (TYPE_PRECISION (type0) - 1))
8577 (op @0 { build_one_cst (type0); }))))))
8579 /* CTZ simplifications. */
8581 (for op (ge gt le lt)
8584 /* __builtin_ctz (x) >= C -> (x & ((1 << C) - 1)) == 0. */
8585 (op (ctz:s @0) INTEGER_CST@1)
8586 (with { bool ok = true;
8587 HOST_WIDE_INT val = 0;
8588 if (!tree_fits_shwi_p (@1))
8592 val = tree_to_shwi (@1);
8593 /* Canonicalize to >= or <. */
8594 if (op == GT_EXPR || op == LE_EXPR)
8596 if (val == HOST_WIDE_INT_MAX)
8602 tree type0 = TREE_TYPE (@0);
8603 int prec = TYPE_PRECISION (type0);
8605 (if (ok && prec <= MAX_FIXED_MODE_SIZE)
8607 { constant_boolean_node (cmp == EQ_EXPR ? true : false, type); }
8609 { constant_boolean_node (cmp == EQ_EXPR ? false : true, type); }
8610 (cmp (bit_and @0 { wide_int_to_tree (type0,
8611 wi::mask (val, false, prec)); })
8612 { build_zero_cst (type0); })))))))
8615 /* __builtin_ctz (x) == C -> (x & ((1 << (C + 1)) - 1)) == (1 << C). */
8616 (op (ctz:s @0) INTEGER_CST@1)
8617 (with { tree type0 = TREE_TYPE (@0);
8618 int prec = TYPE_PRECISION (type0);
8620 (if (prec <= MAX_FIXED_MODE_SIZE)
8621 (if (tree_int_cst_sgn (@1) < 0 || wi::to_widest (@1) >= prec)
8622 { constant_boolean_node (op == EQ_EXPR ? false : true, type); }
8623 (op (bit_and @0 { wide_int_to_tree (type0,
8624 wi::mask (tree_to_uhwi (@1) + 1,
8626 { wide_int_to_tree (type0,
8627 wi::shifted_mask (tree_to_uhwi (@1), 1,
8628 false, prec)); })))))))
8629 (for op (ge gt le lt)
8632 /* __builtin_ctz (x) >= C -> (x & ((1 << C) - 1)) == 0. */
8633 (op (IFN_CTZ:s @0 @2) INTEGER_CST@1)
8634 (with { bool ok = true;
8635 HOST_WIDE_INT val = 0;
8636 if (!tree_fits_shwi_p (@1))
8640 val = tree_to_shwi (@1);
8641 /* Canonicalize to >= or <. */
8642 if (op == GT_EXPR || op == LE_EXPR)
8644 if (val == HOST_WIDE_INT_MAX)
8650 HOST_WIDE_INT zero_val = tree_to_shwi (@2);
8651 tree type0 = TREE_TYPE (@0);
8652 int prec = TYPE_PRECISION (type0);
8653 if (prec > MAX_FIXED_MODE_SIZE)
8657 (if (ok && zero_val >= val)
8658 { constant_boolean_node (cmp == EQ_EXPR ? true : false, type); })
8660 (if (ok && zero_val < val)
8661 { constant_boolean_node (cmp == EQ_EXPR ? false : true, type); })
8662 (if (ok && (zero_val < 0 || zero_val >= prec))
8663 (cmp (bit_and @0 { wide_int_to_tree (type0,
8664 wi::mask (val, false, prec)); })
8665 { build_zero_cst (type0); })))))))
8668 /* __builtin_ctz (x) == C -> (x & ((1 << (C + 1)) - 1)) == (1 << C). */
8669 (op (IFN_CTZ:s @0 @2) INTEGER_CST@1)
8670 (with { HOST_WIDE_INT zero_val = tree_to_shwi (@2);
8671 tree type0 = TREE_TYPE (@0);
8672 int prec = TYPE_PRECISION (type0);
8674 (if (prec <= MAX_FIXED_MODE_SIZE)
8675 (if (tree_int_cst_sgn (@1) < 0 || wi::to_widest (@1) >= prec)
8676 (if (zero_val != wi::to_widest (@1))
8677 { constant_boolean_node (op == EQ_EXPR ? false : true, type); })
8678 (if (zero_val < 0 || zero_val >= prec)
8679 (op (bit_and @0 { wide_int_to_tree (type0,
8680 wi::mask (tree_to_uhwi (@1) + 1,
8682 { wide_int_to_tree (type0,
8683 wi::shifted_mask (tree_to_uhwi (@1), 1,
8684 false, prec)); })))))))
8687 /* ctz(ext(X)) == ctz(X). Valid just for the UB at zero cases though. */
8689 (CTZ (convert@1 @0))
8690 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
8691 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
8692 && TYPE_PRECISION (TREE_TYPE (@1)) > TYPE_PRECISION (TREE_TYPE (@0)))
8693 (with { combined_fn cfn = CFN_LAST;
8694 tree type0 = TREE_TYPE (@0);
8695 if (TREE_CODE (type0) == BITINT_TYPE)
8697 if (TYPE_PRECISION (type0) > MAX_FIXED_MODE_SIZE)
8701 = build_nonstandard_integer_type (TYPE_PRECISION (type0),
8704 type0 = unsigned_type_for (type0);
8706 && direct_internal_fn_supported_p (IFN_CTZ, type0,
8710 && TYPE_PRECISION (TREE_TYPE (@1)) > BITS_PER_WORD
8711 && !direct_internal_fn_supported_p (IFN_CTZ,
8715 if (TYPE_PRECISION (type0)
8716 == TYPE_PRECISION (unsigned_type_node))
8717 cfn = CFN_BUILT_IN_CTZ;
8718 else if (TYPE_PRECISION (type0)
8719 == TYPE_PRECISION (long_long_unsigned_type_node))
8720 cfn = CFN_BUILT_IN_CTZLL;
8722 (if (cfn == CFN_CTZ)
8723 (IFN_CTZ (convert:type0 @0))
8724 (if (cfn == CFN_BUILT_IN_CTZ)
8725 (BUILT_IN_CTZ (convert:type0 @0))
8726 (if (cfn == CFN_BUILT_IN_CTZLL)
8727 (BUILT_IN_CTZLL (convert:type0 @0))))))))
8730 /* POPCOUNT simplifications. */
8731 /* popcount(X) + popcount(Y) is popcount(X|Y) when X&Y must be zero. */
8733 (plus (POPCOUNT:s @0) (POPCOUNT:s @1))
8734 (if (INTEGRAL_TYPE_P (type)
8735 && (wi::bit_and (widest_int::from (tree_nonzero_bits (@0), UNSIGNED),
8736 widest_int::from (tree_nonzero_bits (@1), UNSIGNED))
8738 (with { tree utype = TREE_TYPE (@0);
8739 if (TYPE_PRECISION (utype) < TYPE_PRECISION (TREE_TYPE (@1)))
8740 utype = TREE_TYPE (@1); }
8741 (POPCOUNT (bit_ior (convert:utype @0) (convert:utype @1))))))
8743 /* popcount(X) == 0 is X == 0, and related (in)equalities. */
8744 (for popcount (POPCOUNT)
8745 (for cmp (le eq ne gt)
8748 (cmp (popcount @0) integer_zerop)
8749 (rep @0 { build_zero_cst (TREE_TYPE (@0)); }))))
8751 /* popcount(bswap(x)) is popcount(x). */
8752 (for popcount (POPCOUNT)
8753 (for bswap (BUILT_IN_BSWAP16 BUILT_IN_BSWAP32
8754 BUILT_IN_BSWAP64 BUILT_IN_BSWAP128)
8756 (popcount (convert?@0 (bswap:s@1 @2)))
8757 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
8758 && INTEGRAL_TYPE_P (TREE_TYPE (@1)))
8759 (with { tree type0 = TREE_TYPE (@0);
8760 tree type1 = TREE_TYPE (@1);
8761 unsigned int prec0 = TYPE_PRECISION (type0);
8762 unsigned int prec1 = TYPE_PRECISION (type1); }
8763 (if (prec0 == prec1 || (prec0 > prec1 && TYPE_UNSIGNED (type1)))
8764 (popcount (convert:type0 (convert:type1 @2)))))))))
8766 /* popcount(rotate(X Y)) is popcount(X). */
8767 (for popcount (POPCOUNT)
8768 (for rot (lrotate rrotate)
8770 (popcount (convert?@0 (rot:s@1 @2 @3)))
8771 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
8772 && INTEGRAL_TYPE_P (TREE_TYPE (@1))
8773 && (GIMPLE || !TREE_SIDE_EFFECTS (@3)))
8774 (with { tree type0 = TREE_TYPE (@0);
8775 tree type1 = TREE_TYPE (@1);
8776 unsigned int prec0 = TYPE_PRECISION (type0);
8777 unsigned int prec1 = TYPE_PRECISION (type1); }
8778 (if (prec0 == prec1 || (prec0 > prec1 && TYPE_UNSIGNED (type1)))
8779 (popcount (convert:type0 @2))))))))
8781 /* Canonicalize POPCOUNT(x)&1 as PARITY(X). */
8783 (bit_and (POPCOUNT @0) integer_onep)
8786 /* popcount(X&Y) + popcount(X|Y) is popcount(x) + popcount(Y). */
8788 (plus:c (POPCOUNT:s (bit_and:s @0 @1)) (POPCOUNT:s (bit_ior:cs @0 @1)))
8789 (plus (POPCOUNT:type @0) (POPCOUNT:type @1)))
8791 /* popcount(X) + popcount(Y) - popcount(X&Y) is popcount(X|Y). */
8792 /* popcount(X) + popcount(Y) - popcount(X|Y) is popcount(X&Y). */
8793 (for popcount (POPCOUNT)
8794 (for log1 (bit_and bit_ior)
8795 log2 (bit_ior bit_and)
8797 (minus (plus:s (popcount:s @0) (popcount:s @1))
8798 (popcount:s (log1:cs @0 @1)))
8799 (popcount (log2 @0 @1)))
8801 (plus:c (minus:s (popcount:s @0) (popcount:s (log1:cs @0 @1)))
8803 (popcount (log2 @0 @1)))))
8806 /* popcount(zext(X)) == popcount(X). */
8808 (POPCOUNT (convert@1 @0))
8809 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
8810 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
8811 && TYPE_UNSIGNED (TREE_TYPE (@0))
8812 && TYPE_PRECISION (TREE_TYPE (@1)) > TYPE_PRECISION (TREE_TYPE (@0)))
8813 (with { combined_fn cfn = CFN_LAST;
8814 tree type0 = TREE_TYPE (@0);
8815 if (TREE_CODE (type0) == BITINT_TYPE)
8817 if (TYPE_PRECISION (type0) > MAX_FIXED_MODE_SIZE)
8821 = build_nonstandard_integer_type (TYPE_PRECISION (type0),
8825 && direct_internal_fn_supported_p (IFN_POPCOUNT, type0,
8829 && TYPE_PRECISION (TREE_TYPE (@1)) > BITS_PER_WORD
8830 && !direct_internal_fn_supported_p (IFN_POPCOUNT,
8834 if (TYPE_PRECISION (type0)
8835 == TYPE_PRECISION (unsigned_type_node))
8836 cfn = CFN_BUILT_IN_POPCOUNT;
8837 else if (TYPE_PRECISION (type0)
8838 == TYPE_PRECISION (long_long_unsigned_type_node))
8839 cfn = CFN_BUILT_IN_POPCOUNTLL;
8841 (if (cfn == CFN_POPCOUNT)
8842 (IFN_POPCOUNT (convert:type0 @0))
8843 (if (cfn == CFN_BUILT_IN_POPCOUNT)
8844 (BUILT_IN_POPCOUNT (convert:type0 @0))
8845 (if (cfn == CFN_BUILT_IN_POPCOUNTLL)
8846 (BUILT_IN_POPCOUNTLL (convert:type0 @0))))))))
8849 /* PARITY simplifications. */
8850 /* parity(~X) is parity(X). */
8852 (PARITY (bit_not @0))
8855 /* parity(bswap(x)) is parity(x). */
8856 (for parity (PARITY)
8857 (for bswap (BUILT_IN_BSWAP16 BUILT_IN_BSWAP32
8858 BUILT_IN_BSWAP64 BUILT_IN_BSWAP128)
8860 (parity (convert?@0 (bswap:s@1 @2)))
8861 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
8862 && INTEGRAL_TYPE_P (TREE_TYPE (@1))
8863 && TYPE_PRECISION (TREE_TYPE (@0))
8864 >= TYPE_PRECISION (TREE_TYPE (@1)))
8865 (with { tree type0 = TREE_TYPE (@0);
8866 tree type1 = TREE_TYPE (@1); }
8867 (parity (convert:type0 (convert:type1 @2))))))))
8869 /* parity(rotate(X Y)) is parity(X). */
8870 (for parity (PARITY)
8871 (for rot (lrotate rrotate)
8873 (parity (convert?@0 (rot:s@1 @2 @3)))
8874 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
8875 && INTEGRAL_TYPE_P (TREE_TYPE (@1))
8876 && (GIMPLE || !TREE_SIDE_EFFECTS (@3))
8877 && TYPE_PRECISION (TREE_TYPE (@0))
8878 >= TYPE_PRECISION (TREE_TYPE (@1)))
8879 (with { tree type0 = TREE_TYPE (@0); }
8880 (parity (convert:type0 @2)))))))
8882 /* parity(X)^parity(Y) is parity(X^Y). */
8884 (bit_xor (PARITY:s @0) (PARITY:s @1))
8885 (PARITY (bit_xor @0 @1)))
8888 /* parity(zext(X)) == parity(X). */
8889 /* parity(sext(X)) == parity(X) if the difference in precision is even. */
8891 (PARITY (convert@1 @0))
8892 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
8893 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
8894 && TYPE_PRECISION (TREE_TYPE (@1)) > TYPE_PRECISION (TREE_TYPE (@0))
8895 && (TYPE_UNSIGNED (TREE_TYPE (@0))
8896 || ((TYPE_PRECISION (TREE_TYPE (@1))
8897 - TYPE_PRECISION (TREE_TYPE (@0))) & 1) == 0))
8898 (with { combined_fn cfn = CFN_LAST;
8899 tree type0 = TREE_TYPE (@0);
8900 if (TREE_CODE (type0) == BITINT_TYPE)
8902 if (TYPE_PRECISION (type0) > MAX_FIXED_MODE_SIZE)
8906 = build_nonstandard_integer_type (TYPE_PRECISION (type0),
8909 type0 = unsigned_type_for (type0);
8911 && direct_internal_fn_supported_p (IFN_PARITY, type0,
8915 && TYPE_PRECISION (TREE_TYPE (@1)) > BITS_PER_WORD
8916 && !direct_internal_fn_supported_p (IFN_PARITY,
8920 if (TYPE_PRECISION (type0)
8921 == TYPE_PRECISION (unsigned_type_node))
8922 cfn = CFN_BUILT_IN_PARITY;
8923 else if (TYPE_PRECISION (type0)
8924 == TYPE_PRECISION (long_long_unsigned_type_node))
8925 cfn = CFN_BUILT_IN_PARITYLL;
8927 (if (cfn == CFN_PARITY)
8928 (IFN_PARITY (convert:type0 @0))
8929 (if (cfn == CFN_BUILT_IN_PARITY)
8930 (BUILT_IN_PARITY (convert:type0 @0))
8931 (if (cfn == CFN_BUILT_IN_PARITYLL)
8932 (BUILT_IN_PARITYLL (convert:type0 @0))))))))
8935 /* a != 0 ? FUN(a) : 0 -> Fun(a) for some builtin functions. */
8936 (for func (POPCOUNT BSWAP FFS PARITY)
8938 (cond (ne @0 integer_zerop@1) (func@3 (convert? @0)) integer_zerop@2)
8941 /* a != 0 ? FUN(a) : CST -> Fun(a) for some CLRSB builtins
8942 where CST is precision-1. */
8945 (cond (ne @0 integer_zerop@1) (func@4 (convert?@3 @0)) INTEGER_CST@2)
8946 (if (wi::to_widest (@2) == TYPE_PRECISION (TREE_TYPE (@3)) - 1)
8950 /* a != 0 ? CLZ(a) : CST -> .CLZ(a) where CST is the result of the internal function for 0. */
8953 (cond (ne @0 integer_zerop@1) (func (convert?@3 @0)) INTEGER_CST@2)
8955 internal_fn ifn = IFN_LAST;
8956 if (TREE_CODE (TREE_TYPE (@3)) == BITINT_TYPE)
8958 if (tree_fits_shwi_p (@2))
8960 HOST_WIDE_INT valw = tree_to_shwi (@2);
8961 if ((int) valw == valw)
8968 else if (direct_internal_fn_supported_p (IFN_CLZ, TREE_TYPE (@3),
8970 && CLZ_DEFINED_VALUE_AT_ZERO
8971 (SCALAR_INT_TYPE_MODE (TREE_TYPE (@3)), val) == 2)
8974 (if (ifn == IFN_CLZ && wi::to_widest (@2) == val)
8977 (cond (ne @0 integer_zerop@1) (IFN_CLZ (convert?@3 @0) INTEGER_CST@2) @2)
8979 internal_fn ifn = IFN_LAST;
8980 if (TREE_CODE (TREE_TYPE (@3)) == BITINT_TYPE)
8982 else if (direct_internal_fn_supported_p (IFN_CLZ, TREE_TYPE (@3),
8986 (if (ifn == IFN_CLZ)
8989 /* a != 0 ? CTZ(a) : CST -> .CTZ(a) where CST is the result of the internal function for 0. */
8992 (cond (ne @0 integer_zerop@1) (func (convert?@3 @0)) INTEGER_CST@2)
8994 internal_fn ifn = IFN_LAST;
8995 if (TREE_CODE (TREE_TYPE (@3)) == BITINT_TYPE)
8997 if (tree_fits_shwi_p (@2))
8999 HOST_WIDE_INT valw = tree_to_shwi (@2);
9000 if ((int) valw == valw)
9007 else if (direct_internal_fn_supported_p (IFN_CTZ, TREE_TYPE (@3),
9009 && CTZ_DEFINED_VALUE_AT_ZERO
9010 (SCALAR_INT_TYPE_MODE (TREE_TYPE (@3)), val) == 2)
9013 (if (ifn == IFN_CTZ && wi::to_widest (@2) == val)
9016 (cond (ne @0 integer_zerop@1) (IFN_CTZ (convert?@3 @0) INTEGER_CST@2) @2)
9018 internal_fn ifn = IFN_LAST;
9019 if (TREE_CODE (TREE_TYPE (@3)) == BITINT_TYPE)
9021 else if (direct_internal_fn_supported_p (IFN_CTZ, TREE_TYPE (@3),
9025 (if (ifn == IFN_CTZ)
9029 /* Common POPCOUNT/PARITY simplifications. */
9030 /* popcount(X&C1) is (X>>C2)&1 when C1 == 1<<C2. Same for parity(X&C1). */
9031 (for pfun (POPCOUNT PARITY)
9034 (if (INTEGRAL_TYPE_P (type))
9035 (with { wide_int nz = tree_nonzero_bits (@0); }
9039 (if (wi::popcount (nz) == 1)
9040 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
9041 (convert (rshift:utype (convert:utype @0)
9042 { build_int_cst (integer_type_node,
9043 wi::ctz (nz)); })))))))))
9046 /* 64- and 32-bits branchless implementations of popcount are detected:
9048 int popcount64c (uint64_t x)
9050 x -= (x >> 1) & 0x5555555555555555ULL;
9051 x = (x & 0x3333333333333333ULL) + ((x >> 2) & 0x3333333333333333ULL);
9052 x = (x + (x >> 4)) & 0x0f0f0f0f0f0f0f0fULL;
9053 return (x * 0x0101010101010101ULL) >> 56;
9056 int popcount32c (uint32_t x)
9058 x -= (x >> 1) & 0x55555555;
9059 x = (x & 0x33333333) + ((x >> 2) & 0x33333333);
9060 x = (x + (x >> 4)) & 0x0f0f0f0f;
9061 return (x * 0x01010101) >> 24;
9068 (rshift @8 INTEGER_CST@5)
9070 (bit_and @6 INTEGER_CST@7)
9074 (bit_and (rshift @0 INTEGER_CST@4) INTEGER_CST@11))
9080 /* Check constants and optab. */
9081 (with { unsigned prec = TYPE_PRECISION (type);
9082 int shift = (64 - prec) & 63;
9083 unsigned HOST_WIDE_INT c1
9084 = HOST_WIDE_INT_UC (0x0101010101010101) >> shift;
9085 unsigned HOST_WIDE_INT c2
9086 = HOST_WIDE_INT_UC (0x0F0F0F0F0F0F0F0F) >> shift;
9087 unsigned HOST_WIDE_INT c3
9088 = HOST_WIDE_INT_UC (0x3333333333333333) >> shift;
9089 unsigned HOST_WIDE_INT c4
9090 = HOST_WIDE_INT_UC (0x5555555555555555) >> shift;
9095 && TYPE_UNSIGNED (type)
9096 && integer_onep (@4)
9097 && wi::to_widest (@10) == 2
9098 && wi::to_widest (@5) == 4
9099 && wi::to_widest (@1) == prec - 8
9100 && tree_to_uhwi (@2) == c1
9101 && tree_to_uhwi (@3) == c2
9102 && tree_to_uhwi (@9) == c3
9103 && tree_to_uhwi (@7) == c3
9104 && tree_to_uhwi (@11) == c4)
9105 (if (direct_internal_fn_supported_p (IFN_POPCOUNT, type,
9107 (convert (IFN_POPCOUNT:type @0))
9108 /* Try to do popcount in two halves. PREC must be at least
9109 five bits for this to work without extension before adding. */
9111 tree half_type = NULL_TREE;
9112 opt_machine_mode m = mode_for_size ((prec + 1) / 2, MODE_INT, 1);
9115 && m.require () != TYPE_MODE (type))
9117 half_prec = GET_MODE_PRECISION (as_a <scalar_int_mode> (m));
9118 half_type = build_nonstandard_integer_type (half_prec, 1);
9120 gcc_assert (half_prec > 2);
9122 (if (half_type != NULL_TREE
9123 && direct_internal_fn_supported_p (IFN_POPCOUNT, half_type,
9126 (IFN_POPCOUNT:half_type (convert @0))
9127 (IFN_POPCOUNT:half_type (convert (rshift @0
9128 { build_int_cst (integer_type_node, half_prec); } )))))))))))
9130 /* __builtin_ffs needs to deal on many targets with the possible zero
9131 argument. If we know the argument is always non-zero, __builtin_ctz + 1
9132 should lead to better code. */
9134 (FFS tree_expr_nonzero_p@0)
9135 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
9136 && direct_internal_fn_supported_p (IFN_CTZ, TREE_TYPE (@0),
9137 OPTIMIZE_FOR_SPEED))
9138 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
9139 (plus (CTZ:type (convert:utype @0)) { build_one_cst (type); }))))
9143 /* __builtin_ffs (X) == 0 -> X == 0.
9144 __builtin_ffs (X) == 6 -> (X & 63) == 32. */
9147 (cmp (ffs@2 @0) INTEGER_CST@1)
9148 (with { int prec = TYPE_PRECISION (TREE_TYPE (@0)); }
9150 (if (integer_zerop (@1))
9151 (cmp @0 { build_zero_cst (TREE_TYPE (@0)); }))
9152 (if (tree_int_cst_sgn (@1) < 0 || wi::to_widest (@1) > prec)
9153 { constant_boolean_node (cmp == NE_EXPR ? true : false, type); })
9154 (if (single_use (@2))
9155 (cmp (bit_and @0 { wide_int_to_tree (TREE_TYPE (@0),
9156 wi::mask (tree_to_uhwi (@1),
9158 { wide_int_to_tree (TREE_TYPE (@0),
9159 wi::shifted_mask (tree_to_uhwi (@1) - 1, 1,
9160 false, prec)); }))))))
9162 /* __builtin_ffs (X) > 6 -> X != 0 && (X & 63) == 0. */
9166 bit_op (bit_and bit_ior)
9168 (cmp (ffs@2 @0) INTEGER_CST@1)
9169 (with { int prec = TYPE_PRECISION (TREE_TYPE (@0)); }
9171 (if (integer_zerop (@1))
9172 (cmp2 @0 { build_zero_cst (TREE_TYPE (@0)); }))
9173 (if (tree_int_cst_sgn (@1) < 0)
9174 { constant_boolean_node (cmp == GT_EXPR ? true : false, type); })
9175 (if (wi::to_widest (@1) >= prec)
9176 { constant_boolean_node (cmp == GT_EXPR ? false : true, type); })
9177 (if (wi::to_widest (@1) == prec - 1)
9178 (cmp3 @0 { wide_int_to_tree (TREE_TYPE (@0),
9179 wi::shifted_mask (prec - 1, 1,
9181 (if (single_use (@2))
9182 (bit_op (cmp2 @0 { build_zero_cst (TREE_TYPE (@0)); })
9184 { wide_int_to_tree (TREE_TYPE (@0),
9185 wi::mask (tree_to_uhwi (@1),
9187 { build_zero_cst (TREE_TYPE (@0)); }))))))))
9190 /* ffs(ext(X)) == ffs(X). */
9192 (FFS (convert@1 @0))
9193 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
9194 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
9195 && TYPE_PRECISION (TREE_TYPE (@1)) > TYPE_PRECISION (TREE_TYPE (@0)))
9196 (with { combined_fn cfn = CFN_LAST;
9197 tree type0 = TREE_TYPE (@0);
9198 if (TREE_CODE (type0) == BITINT_TYPE)
9200 if (TYPE_PRECISION (type0) > MAX_FIXED_MODE_SIZE)
9204 = build_nonstandard_integer_type (TYPE_PRECISION (type0),
9207 type0 = signed_type_for (type0);
9209 && direct_internal_fn_supported_p (IFN_FFS, type0,
9213 && TYPE_PRECISION (TREE_TYPE (@1)) > BITS_PER_WORD
9214 && !direct_internal_fn_supported_p (IFN_FFS,
9218 if (TYPE_PRECISION (type0)
9219 == TYPE_PRECISION (integer_type_node))
9220 cfn = CFN_BUILT_IN_FFS;
9221 else if (TYPE_PRECISION (type0)
9222 == TYPE_PRECISION (long_long_integer_type_node))
9223 cfn = CFN_BUILT_IN_FFSLL;
9225 (if (cfn == CFN_FFS)
9226 (IFN_FFS (convert:type0 @0))
9227 (if (cfn == CFN_BUILT_IN_FFS)
9228 (BUILT_IN_FFS (convert:type0 @0))
9229 (if (cfn == CFN_BUILT_IN_FFSLL)
9230 (BUILT_IN_FFSLL (convert:type0 @0))))))))
9238 --> r = .COND_FN (cond, a, b)
9242 --> r = .COND_FN (~cond, b, a). */
9244 (for uncond_op (UNCOND_UNARY)
9245 cond_op (COND_UNARY)
9247 (vec_cond @0 (view_convert? (uncond_op@3 @1)) @2)
9248 (with { tree op_type = TREE_TYPE (@3); }
9249 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
9250 && is_truth_type_for (op_type, TREE_TYPE (@0)))
9251 (cond_op @0 (view_convert @1) @2))))
9253 (vec_cond @0 @1 (view_convert? (uncond_op@3 @2)))
9254 (with { tree op_type = TREE_TYPE (@3); }
9255 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
9256 && is_truth_type_for (op_type, TREE_TYPE (@0)))
9257 (cond_op (bit_not @0) (view_convert @2) @1)))))
9259 (for uncond_op (UNCOND_UNARY)
9260 cond_op (COND_LEN_UNARY)
9262 (IFN_VCOND_MASK_LEN @0 (view_convert? (uncond_op@3 @1)) @2 @4 @5)
9263 (with { tree op_type = TREE_TYPE (@3); }
9264 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
9265 && is_truth_type_for (op_type, TREE_TYPE (@0)))
9266 (cond_op @0 (view_convert @1) @2 @4 @5))))
9268 (IFN_VCOND_MASK_LEN @0 @1 (view_convert? (uncond_op@3 @2)) @4 @5)
9269 (with { tree op_type = TREE_TYPE (@3); }
9270 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
9271 && is_truth_type_for (op_type, TREE_TYPE (@0)))
9272 (cond_op (bit_not @0) (view_convert @2) @1 @4 @5)))))
9274 /* `(a ? -1 : 0) ^ b` can be converted into a conditional not. */
9276 (bit_xor:c (vec_cond @0 uniform_integer_cst_p@1 uniform_integer_cst_p@2) @3)
9277 (if (canonicalize_math_after_vectorization_p ()
9278 && vectorized_internal_fn_supported_p (IFN_COND_NOT, type)
9279 && is_truth_type_for (type, TREE_TYPE (@0)))
9280 (if (integer_all_onesp (@1) && integer_zerop (@2))
9281 (IFN_COND_NOT @0 @3 @3))
9282 (if (integer_all_onesp (@2) && integer_zerop (@1))
9283 (IFN_COND_NOT (bit_not @0) @3 @3))))
9292 r = c ? a1 op a2 : b;
9294 if the target can do it in one go. This makes the operation conditional
9295 on c, so could drop potentially-trapping arithmetic, but that's a valid
9296 simplification if the result of the operation isn't needed.
9298 Avoid speculatively generating a stand-alone vector comparison
9299 on targets that might not support them. Any target implementing
9300 conditional internal functions must support the same comparisons
9301 inside and outside a VEC_COND_EXPR. */
9303 (for uncond_op (UNCOND_BINARY)
9304 cond_op (COND_BINARY)
9306 (vec_cond @0 (view_convert? (uncond_op@4 @1 @2)) @3)
9307 (with { tree op_type = TREE_TYPE (@4); }
9308 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
9309 && is_truth_type_for (op_type, TREE_TYPE (@0))
9311 (view_convert (cond_op @0 @1 @2 (view_convert:op_type @3))))))
9313 (vec_cond @0 @1 (view_convert? (uncond_op@4 @2 @3)))
9314 (with { tree op_type = TREE_TYPE (@4); }
9315 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
9316 && is_truth_type_for (op_type, TREE_TYPE (@0))
9318 (view_convert (cond_op (bit_not @0) @2 @3 (view_convert:op_type @1)))))))
9320 (for uncond_op (UNCOND_BINARY)
9321 cond_op (COND_LEN_BINARY)
9323 (IFN_VCOND_MASK_LEN @0 (view_convert? (uncond_op@4 @1 @2)) @3 @5 @6)
9324 (with { tree op_type = TREE_TYPE (@4); }
9325 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
9326 && is_truth_type_for (op_type, TREE_TYPE (@0))
9328 (view_convert (cond_op @0 @1 @2 (view_convert:op_type @3) @5 @6)))))
9330 (IFN_VCOND_MASK_LEN @0 @1 (view_convert? (uncond_op@4 @2 @3)) @5 @6)
9331 (with { tree op_type = TREE_TYPE (@4); }
9332 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
9333 && is_truth_type_for (op_type, TREE_TYPE (@0))
9335 (view_convert (cond_op (bit_not @0) @2 @3 (view_convert:op_type @1) @5 @6))))))
9337 /* Same for ternary operations. */
9338 (for uncond_op (UNCOND_TERNARY)
9339 cond_op (COND_TERNARY)
9341 (vec_cond @0 (view_convert? (uncond_op@5 @1 @2 @3)) @4)
9342 (with { tree op_type = TREE_TYPE (@5); }
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 @0 @1 @2 @3 (view_convert:op_type @4))))))
9348 (vec_cond @0 @1 (view_convert? (uncond_op@5 @2 @3 @4)))
9349 (with { tree op_type = TREE_TYPE (@5); }
9350 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
9351 && is_truth_type_for (op_type, TREE_TYPE (@0))
9353 (view_convert (cond_op (bit_not @0) @2 @3 @4
9354 (view_convert:op_type @1)))))))
9356 (for uncond_op (UNCOND_TERNARY)
9357 cond_op (COND_LEN_TERNARY)
9359 (IFN_VCOND_MASK_LEN @0 (view_convert? (uncond_op@5 @1 @2 @3)) @4 @6 @7)
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 @0 @1 @2 @3 (view_convert:op_type @4) @6 @7)))))
9366 (IFN_VCOND_MASK_LEN @0 @1 (view_convert? (uncond_op@5 @2 @3 @4 @6 @7)))
9367 (with { tree op_type = TREE_TYPE (@5); }
9368 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
9369 && is_truth_type_for (op_type, TREE_TYPE (@0))
9371 (view_convert (cond_op (bit_not @0) @2 @3 @4 (view_convert:op_type @1) @6 @7))))))
9374 /* Detect cases in which a VEC_COND_EXPR effectively replaces the
9375 "else" value of an IFN_COND_*. */
9376 (for cond_op (COND_BINARY)
9378 (vec_cond @0 (view_convert? (cond_op @0 @1 @2 @3)) @4)
9379 (with { tree op_type = TREE_TYPE (@3); }
9380 (if (element_precision (type) == element_precision (op_type))
9381 (view_convert (cond_op @0 @1 @2 (view_convert:op_type @4))))))
9383 (vec_cond @0 @1 (view_convert? (cond_op @2 @3 @4 @5)))
9384 (with { tree op_type = TREE_TYPE (@5); }
9385 (if (inverse_conditions_p (@0, @2)
9386 && element_precision (type) == element_precision (op_type))
9387 (view_convert (cond_op @2 @3 @4 (view_convert:op_type @1)))))))
9389 /* Same for ternary operations. */
9390 (for cond_op (COND_TERNARY)
9392 (vec_cond @0 (view_convert? (cond_op @0 @1 @2 @3 @4)) @5)
9393 (with { tree op_type = TREE_TYPE (@4); }
9394 (if (element_precision (type) == element_precision (op_type))
9395 (view_convert (cond_op @0 @1 @2 @3 (view_convert:op_type @5))))))
9397 (vec_cond @0 @1 (view_convert? (cond_op @2 @3 @4 @5 @6)))
9398 (with { tree op_type = TREE_TYPE (@6); }
9399 (if (inverse_conditions_p (@0, @2)
9400 && element_precision (type) == element_precision (op_type))
9401 (view_convert (cond_op @2 @3 @4 @5 (view_convert:op_type @1)))))))
9403 /* Detect cases in which a VEC_COND_EXPR effectively replaces the
9404 "else" value of an IFN_COND_LEN_*. */
9405 (for cond_len_op (COND_LEN_BINARY)
9407 (vec_cond @0 (view_convert? (cond_len_op @0 @1 @2 @3 @4 @5)) @6)
9408 (with { tree op_type = TREE_TYPE (@3); }
9409 (if (element_precision (type) == element_precision (op_type))
9410 (view_convert (cond_len_op @0 @1 @2 (view_convert:op_type @6) @4 @5)))))
9412 (vec_cond @0 @1 (view_convert? (cond_len_op @2 @3 @4 @5 @6 @7)))
9413 (with { tree op_type = TREE_TYPE (@5); }
9414 (if (inverse_conditions_p (@0, @2)
9415 && element_precision (type) == element_precision (op_type))
9416 (view_convert (cond_len_op @2 @3 @4 (view_convert:op_type @1) @6 @7))))))
9418 /* Same for ternary operations. */
9419 (for cond_len_op (COND_LEN_TERNARY)
9421 (vec_cond @0 (view_convert? (cond_len_op @0 @1 @2 @3 @4 @5 @6)) @7)
9422 (with { tree op_type = TREE_TYPE (@4); }
9423 (if (element_precision (type) == element_precision (op_type))
9424 (view_convert (cond_len_op @0 @1 @2 @3 (view_convert:op_type @7) @5 @6)))))
9426 (vec_cond @0 @1 (view_convert? (cond_len_op @2 @3 @4 @5 @6 @7 @8)))
9427 (with { tree op_type = TREE_TYPE (@6); }
9428 (if (inverse_conditions_p (@0, @2)
9429 && element_precision (type) == element_precision (op_type))
9430 (view_convert (cond_len_op @2 @3 @4 @5 (view_convert:op_type @1) @7 @8))))))
9432 /* Detect simplication for a conditional reduction where
9435 c = mask2 ? d + a : d
9439 c = mask1 && mask2 ? d + b : d. */
9441 (IFN_COND_ADD @0 @1 (vec_cond @2 @3 zerop@4) @1)
9442 (if (ANY_INTEGRAL_TYPE_P (type)
9443 || (FLOAT_TYPE_P (type)
9444 && fold_real_zero_addition_p (type, NULL_TREE, @4, 0)))
9445 (IFN_COND_ADD (bit_and @0 @2) @1 @3 @1)))
9447 /* Detect simplication for a conditional length reduction where
9450 c = i < len + bias ? d + a : d
9454 c = mask && i < len + bias ? d + b : d. */
9456 (IFN_COND_LEN_ADD integer_truep @0 (vec_cond @1 @2 zerop@5) @0 @3 @4)
9457 (if (ANY_INTEGRAL_TYPE_P (type)
9458 || (FLOAT_TYPE_P (type)
9459 && fold_real_zero_addition_p (type, NULL_TREE, @5, 0)))
9460 (IFN_COND_LEN_ADD @1 @0 @2 @0 @3 @4)))
9462 /* Detect simplification for vector condition folding where
9464 c = mask1 ? (masked_op mask2 a b) : b
9468 c = masked_op (mask1 & mask2) a b
9470 where the operation can be partially applied to one operand. */
9472 (for cond_op (COND_BINARY)
9475 (cond_op:s @1 @2 @3 @4) @3)
9476 (cond_op (bit_and @1 @0) @2 @3 @4)))
9478 /* And same for ternary expressions. */
9480 (for cond_op (COND_TERNARY)
9483 (cond_op:s @1 @2 @3 @4 @5) @4)
9484 (cond_op (bit_and @1 @0) @2 @3 @4 @5)))
9486 /* For pointers @0 and @2 and nonnegative constant offset @1, look for
9489 A: (@0 + @1 < @2) | (@2 + @1 < @0)
9490 B: (@0 + @1 <= @2) | (@2 + @1 <= @0)
9492 If pointers are known not to wrap, B checks whether @1 bytes starting
9493 at @0 and @2 do not overlap, while A tests the same thing for @1 + 1
9494 bytes. A is more efficiently tested as:
9496 A: (sizetype) (@0 + @1 - @2) > @1 * 2
9498 The equivalent expression for B is given by replacing @1 with @1 - 1:
9500 B: (sizetype) (@0 + (@1 - 1) - @2) > (@1 - 1) * 2
9502 @0 and @2 can be swapped in both expressions without changing the result.
9504 The folds rely on sizetype's being unsigned (which is always true)
9505 and on its being the same width as the pointer (which we have to check).
9507 The fold replaces two pointer_plus expressions, two comparisons and
9508 an IOR with a pointer_plus, a pointer_diff, and a comparison, so in
9509 the best case it's a saving of two operations. The A fold retains one
9510 of the original pointer_pluses, so is a win even if both pointer_pluses
9511 are used elsewhere. The B fold is a wash if both pointer_pluses are
9512 used elsewhere, since all we end up doing is replacing a comparison with
9513 a pointer_plus. We do still apply the fold under those circumstances
9514 though, in case applying it to other conditions eventually makes one of the
9515 pointer_pluses dead. */
9516 (for ior (truth_orif truth_or bit_ior)
9519 (ior (cmp:cs (pointer_plus@3 @0 INTEGER_CST@1) @2)
9520 (cmp:cs (pointer_plus@4 @2 @1) @0))
9521 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
9522 && TYPE_OVERFLOW_WRAPS (sizetype)
9523 && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (sizetype))
9524 /* Calculate the rhs constant. */
9525 (with { offset_int off = wi::to_offset (@1) - (cmp == LE_EXPR ? 1 : 0);
9526 offset_int rhs = off * 2; }
9527 /* Always fails for negative values. */
9528 (if (wi::min_precision (rhs, UNSIGNED) <= TYPE_PRECISION (sizetype))
9529 /* Since the order of @0 and @2 doesn't matter, let tree_swap_operands_p
9530 pick a canonical order. This increases the chances of using the
9531 same pointer_plus in multiple checks. */
9532 (with { bool swap_p = tree_swap_operands_p (@0, @2);
9533 tree rhs_tree = wide_int_to_tree (sizetype, rhs); }
9534 (if (cmp == LT_EXPR)
9535 (gt (convert:sizetype
9536 (pointer_diff:ssizetype { swap_p ? @4 : @3; }
9537 { swap_p ? @0 : @2; }))
9539 (gt (convert:sizetype
9540 (pointer_diff:ssizetype
9541 (pointer_plus { swap_p ? @2 : @0; }
9542 { wide_int_to_tree (sizetype, off); })
9543 { swap_p ? @0 : @2; }))
9544 { rhs_tree; })))))))))
9546 /* Fold REDUC (@0 & @1) -> @0[I] & @1[I] if element I is the only nonzero
9548 (for reduc (IFN_REDUC_PLUS IFN_REDUC_IOR IFN_REDUC_XOR)
9549 (simplify (reduc (view_convert? (bit_and @0 VECTOR_CST@1)))
9550 (with { int i = single_nonzero_element (@1); }
9552 (with { tree elt = vector_cst_elt (@1, i);
9553 tree elt_type = TREE_TYPE (elt);
9554 unsigned int elt_bits = tree_to_uhwi (TYPE_SIZE (elt_type));
9555 tree size = bitsize_int (elt_bits);
9556 tree pos = bitsize_int (elt_bits * i); }
9559 (BIT_FIELD_REF:elt_type @0 { size; } { pos; })
9562 /* Fold reduction of a single nonzero element constructor. */
9563 (for reduc (IFN_REDUC_PLUS IFN_REDUC_IOR IFN_REDUC_XOR)
9564 (simplify (reduc (CONSTRUCTOR@0))
9565 (with { tree ctor = (TREE_CODE (@0) == SSA_NAME
9566 ? gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0)) : @0);
9567 tree elt = ctor_single_nonzero_element (ctor); }
9569 && !HONOR_SNANS (type)
9570 && !HONOR_SIGNED_ZEROS (type))
9573 /* Fold REDUC (@0 op VECTOR_CST) as REDUC (@0) op REDUC (VECTOR_CST). */
9574 (for reduc (IFN_REDUC_PLUS IFN_REDUC_MAX IFN_REDUC_MIN IFN_REDUC_FMAX
9575 IFN_REDUC_FMIN IFN_REDUC_AND IFN_REDUC_IOR IFN_REDUC_XOR)
9576 op (plus max min IFN_FMAX IFN_FMIN bit_and bit_ior bit_xor)
9577 (simplify (reduc (op @0 VECTOR_CST@1))
9578 (op (reduc:type @0) (reduc:type @1))))
9580 /* Simplify vector floating point operations of alternating sub/add pairs
9581 into using an fneg of a wider element type followed by a normal add.
9582 under IEEE 754 the fneg of the wider type will negate every even entry
9583 and when doing an add we get a sub of the even and add of every odd
9585 (for plusminus (plus minus)
9586 minusplus (minus plus)
9588 (vec_perm (plusminus @0 @1) (minusplus @2 @3) VECTOR_CST@4)
9589 (if (!VECTOR_INTEGER_TYPE_P (type)
9590 && !FLOAT_WORDS_BIG_ENDIAN
9591 /* plus is commutative, while minus is not, so :c can't be used.
9592 Do equality comparisons by hand and at the end pick the operands
9594 && (operand_equal_p (@0, @2, 0)
9595 ? operand_equal_p (@1, @3, 0)
9596 : operand_equal_p (@0, @3, 0) && operand_equal_p (@1, @2, 0)))
9599 /* Build a vector of integers from the tree mask. */
9600 vec_perm_builder builder;
9602 (if (tree_to_vec_perm_builder (&builder, @4))
9605 /* Create a vec_perm_indices for the integer vector. */
9606 poly_uint64 nelts = TYPE_VECTOR_SUBPARTS (type);
9607 vec_perm_indices sel (builder, 2, nelts);
9608 machine_mode vec_mode = TYPE_MODE (type);
9609 machine_mode wide_mode;
9610 scalar_mode wide_elt_mode;
9611 poly_uint64 wide_nunits;
9612 scalar_mode inner_mode = GET_MODE_INNER (vec_mode);
9614 (if (VECTOR_MODE_P (vec_mode)
9615 && sel.series_p (0, 2, 0, 2)
9616 && sel.series_p (1, 2, nelts + 1, 2)
9617 && GET_MODE_2XWIDER_MODE (inner_mode).exists (&wide_elt_mode)
9618 && multiple_p (GET_MODE_NUNITS (vec_mode), 2, &wide_nunits)
9619 && related_vector_mode (vec_mode, wide_elt_mode,
9620 wide_nunits).exists (&wide_mode))
9624 = lang_hooks.types.type_for_mode (GET_MODE_INNER (wide_mode),
9625 TYPE_UNSIGNED (type));
9626 tree ntype = build_vector_type_for_mode (stype, wide_mode);
9628 /* The format has to be a non-extended ieee format. */
9629 const struct real_format *fmt_old = FLOAT_MODE_FORMAT (vec_mode);
9630 const struct real_format *fmt_new = FLOAT_MODE_FORMAT (wide_mode);
9632 (if (TYPE_MODE (stype) != BLKmode
9633 && VECTOR_TYPE_P (ntype)
9638 /* If the target doesn't support v1xx vectors, try using
9639 scalar mode xx instead. */
9640 if (known_eq (GET_MODE_NUNITS (wide_mode), 1)
9641 && !target_supports_op_p (ntype, NEGATE_EXPR, optab_vector))
9644 (if (fmt_new->signbit_rw
9645 == fmt_old->signbit_rw + GET_MODE_UNIT_BITSIZE (vec_mode)
9646 && fmt_new->signbit_rw == fmt_new->signbit_ro
9647 && targetm.can_change_mode_class (TYPE_MODE (ntype),
9648 TYPE_MODE (type), ALL_REGS)
9649 && ((optimize_vectors_before_lowering_p ()
9650 && VECTOR_TYPE_P (ntype))
9651 || target_supports_op_p (ntype, NEGATE_EXPR, optab_vector)))
9652 (if (plusminus == PLUS_EXPR)
9653 (plus (view_convert:type (negate (view_convert:ntype @3))) @2)
9654 (minus @0 (view_convert:type
9655 (negate (view_convert:ntype @1))))))))))))))))
9658 (vec_perm @0 @1 VECTOR_CST@2)
9661 tree op0 = @0, op1 = @1, op2 = @2;
9662 machine_mode result_mode = TYPE_MODE (type);
9663 machine_mode op_mode = TYPE_MODE (TREE_TYPE (op0));
9665 /* Build a vector of integers from the tree mask. */
9666 vec_perm_builder builder;
9668 (if (tree_to_vec_perm_builder (&builder, op2))
9671 /* Create a vec_perm_indices for the integer vector. */
9672 poly_uint64 nelts = TYPE_VECTOR_SUBPARTS (type);
9673 bool single_arg = (op0 == op1);
9674 vec_perm_indices sel (builder, single_arg ? 1 : 2, nelts);
9676 (if (sel.series_p (0, 1, 0, 1))
9678 (if (sel.series_p (0, 1, nelts, 1))
9684 if (sel.all_from_input_p (0))
9686 else if (sel.all_from_input_p (1))
9689 sel.rotate_inputs (1);
9691 else if (known_ge (poly_uint64 (sel[0]), nelts))
9693 std::swap (op0, op1);
9694 sel.rotate_inputs (1);
9698 tree cop0 = op0, cop1 = op1;
9699 if (TREE_CODE (op0) == SSA_NAME
9700 && (def = dyn_cast <gassign *> (SSA_NAME_DEF_STMT (op0)))
9701 && gimple_assign_rhs_code (def) == CONSTRUCTOR)
9702 cop0 = gimple_assign_rhs1 (def);
9703 if (TREE_CODE (op1) == SSA_NAME
9704 && (def = dyn_cast <gassign *> (SSA_NAME_DEF_STMT (op1)))
9705 && gimple_assign_rhs_code (def) == CONSTRUCTOR)
9706 cop1 = gimple_assign_rhs1 (def);
9709 (if ((TREE_CODE (cop0) == VECTOR_CST
9710 || TREE_CODE (cop0) == CONSTRUCTOR)
9711 && (TREE_CODE (cop1) == VECTOR_CST
9712 || TREE_CODE (cop1) == CONSTRUCTOR)
9713 && (t = fold_vec_perm (type, cop0, cop1, sel)))
9717 bool changed = (op0 == op1 && !single_arg);
9718 tree ins = NULL_TREE;
9721 /* See if the permutation is performing a single element
9722 insert from a CONSTRUCTOR or constant and use a BIT_INSERT_EXPR
9723 in that case. But only if the vector mode is supported,
9724 otherwise this is invalid GIMPLE. */
9725 if (op_mode != BLKmode
9726 && (TREE_CODE (cop0) == VECTOR_CST
9727 || TREE_CODE (cop0) == CONSTRUCTOR
9728 || TREE_CODE (cop1) == VECTOR_CST
9729 || TREE_CODE (cop1) == CONSTRUCTOR))
9731 bool insert_first_p = sel.series_p (1, 1, nelts + 1, 1);
9734 /* After canonicalizing the first elt to come from the
9735 first vector we only can insert the first elt from
9736 the first vector. */
9738 if ((ins = fold_read_from_vector (cop0, sel[0])))
9741 /* The above can fail for two-element vectors which always
9742 appear to insert the first element, so try inserting
9743 into the second lane as well. For more than two
9744 elements that's wasted time. */
9745 if (!insert_first_p || (!ins && maybe_eq (nelts, 2u)))
9747 unsigned int encoded_nelts = sel.encoding ().encoded_nelts ();
9748 for (at = 0; at < encoded_nelts; ++at)
9749 if (maybe_ne (sel[at], at))
9751 if (at < encoded_nelts
9752 && (known_eq (at + 1, nelts)
9753 || sel.series_p (at + 1, 1, at + 1, 1)))
9755 if (known_lt (poly_uint64 (sel[at]), nelts))
9756 ins = fold_read_from_vector (cop0, sel[at]);
9758 ins = fold_read_from_vector (cop1, sel[at] - nelts);
9763 /* Generate a canonical form of the selector. */
9764 if (!ins && sel.encoding () != builder)
9766 /* Some targets are deficient and fail to expand a single
9767 argument permutation while still allowing an equivalent
9768 2-argument version. */
9770 if (sel.ninputs () == 2
9771 || can_vec_perm_const_p (result_mode, op_mode, sel, false))
9772 op2 = vec_perm_indices_to_tree (TREE_TYPE (op2), sel);
9775 vec_perm_indices sel2 (builder, 2, nelts);
9776 if (can_vec_perm_const_p (result_mode, op_mode, sel2, false))
9777 op2 = vec_perm_indices_to_tree (TREE_TYPE (op2), sel2);
9779 /* Not directly supported with either encoding,
9780 so use the preferred form. */
9781 op2 = vec_perm_indices_to_tree (TREE_TYPE (op2), sel);
9783 if (!operand_equal_p (op2, oldop2, 0))
9788 (bit_insert { op0; } { ins; }
9789 { bitsize_int (at * vector_element_bits (type)); })
9791 (vec_perm { op0; } { op1; } { op2; }))))))))))))
9793 /* VEC_PERM_EXPR (v, v, mask) -> v where v contains same element. */
9795 (match vec_same_elem_p
9798 (match vec_same_elem_p
9800 (if (TREE_CODE (@0) == SSA_NAME
9801 && uniform_vector_p (gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0))))))
9803 (match vec_same_elem_p
9805 (if (uniform_vector_p (@0))))
9809 (vec_perm vec_same_elem_p@0 @0 @1)
9810 (if (types_match (type, TREE_TYPE (@0)))
9814 tree elem = uniform_vector_p (@0);
9817 { build_vector_from_val (type, elem); }))))
9819 /* Push VEC_PERM earlier if that may help FMA perception (PR101895). */
9821 (plus:c (vec_perm:s (mult:c@0 @1 vec_same_elem_p@2) @0 @3) @4)
9822 (if (TREE_CODE (@0) == SSA_NAME && num_imm_uses (@0) == 2)
9823 (plus (mult (vec_perm @1 @1 @3) @2) @4)))
9825 (minus (vec_perm:s (mult:c@0 @1 vec_same_elem_p@2) @0 @3) @4)
9826 (if (TREE_CODE (@0) == SSA_NAME && num_imm_uses (@0) == 2)
9827 (minus (mult (vec_perm @1 @1 @3) @2) @4)))
9831 c = VEC_PERM_EXPR <a, b, VCST0>;
9832 d = VEC_PERM_EXPR <c, c, VCST1>;
9834 d = VEC_PERM_EXPR <a, b, NEW_VCST>; */
9837 (vec_perm (vec_perm@0 @1 @2 VECTOR_CST@3) @0 VECTOR_CST@4)
9838 (if (TYPE_VECTOR_SUBPARTS (type).is_constant ())
9841 machine_mode result_mode = TYPE_MODE (type);
9842 machine_mode op_mode = TYPE_MODE (TREE_TYPE (@1));
9843 int nelts = TYPE_VECTOR_SUBPARTS (type).to_constant ();
9844 vec_perm_builder builder0;
9845 vec_perm_builder builder1;
9846 vec_perm_builder builder2 (nelts, nelts, 1);
9848 (if (tree_to_vec_perm_builder (&builder0, @3)
9849 && tree_to_vec_perm_builder (&builder1, @4))
9852 vec_perm_indices sel0 (builder0, 2, nelts);
9853 vec_perm_indices sel1 (builder1, 1, nelts);
9855 for (int i = 0; i < nelts; i++)
9856 builder2.quick_push (sel0[sel1[i].to_constant ()]);
9858 vec_perm_indices sel2 (builder2, 2, nelts);
9860 tree op0 = NULL_TREE;
9861 /* If the new VEC_PERM_EXPR can't be handled but both
9862 original VEC_PERM_EXPRs can, punt.
9863 If one or both of the original VEC_PERM_EXPRs can't be
9864 handled and the new one can't be either, don't increase
9865 number of VEC_PERM_EXPRs that can't be handled. */
9866 if (can_vec_perm_const_p (result_mode, op_mode, sel2, false)
9868 ? (!can_vec_perm_const_p (result_mode, op_mode, sel0, false)
9869 || !can_vec_perm_const_p (result_mode, op_mode, sel1, false))
9870 : !can_vec_perm_const_p (result_mode, op_mode, sel1, false)))
9871 op0 = vec_perm_indices_to_tree (TREE_TYPE (@4), sel2);
9874 (vec_perm @1 @2 { op0; })))))))
9877 c = VEC_PERM_EXPR <a, b, VCST0>;
9878 d = VEC_PERM_EXPR <x, c, VCST1>;
9880 d = VEC_PERM_EXPR <x, {a,b}, NEW_VCST>;
9881 when all elements from a or b are replaced by the later
9885 (vec_perm @5 (vec_perm@0 @1 @2 VECTOR_CST@3) VECTOR_CST@4)
9886 (if (TYPE_VECTOR_SUBPARTS (type).is_constant ())
9889 machine_mode result_mode = TYPE_MODE (type);
9890 machine_mode op_mode = TYPE_MODE (TREE_TYPE (@1));
9891 int nelts = TYPE_VECTOR_SUBPARTS (type).to_constant ();
9892 vec_perm_builder builder0;
9893 vec_perm_builder builder1;
9894 vec_perm_builder builder2 (nelts, nelts, 2);
9896 (if (tree_to_vec_perm_builder (&builder0, @3)
9897 && tree_to_vec_perm_builder (&builder1, @4))
9900 vec_perm_indices sel0 (builder0, 2, nelts);
9901 vec_perm_indices sel1 (builder1, 2, nelts);
9902 bool use_1 = false, use_2 = false;
9904 for (int i = 0; i < nelts; i++)
9906 if (known_lt ((poly_uint64)sel1[i], sel1.nelts_per_input ()))
9907 builder2.quick_push (sel1[i]);
9910 poly_uint64 j = sel0[(sel1[i] - sel1.nelts_per_input ())
9912 if (known_lt (j, sel0.nelts_per_input ()))
9917 j -= sel0.nelts_per_input ();
9919 builder2.quick_push (j + sel1.nelts_per_input ());
9926 vec_perm_indices sel2 (builder2, 2, nelts);
9927 tree op0 = NULL_TREE;
9928 /* If the new VEC_PERM_EXPR can't be handled but both
9929 original VEC_PERM_EXPRs can, punt.
9930 If one or both of the original VEC_PERM_EXPRs can't be
9931 handled and the new one can't be either, don't increase
9932 number of VEC_PERM_EXPRs that can't be handled. */
9933 if (can_vec_perm_const_p (result_mode, op_mode, sel2, false)
9935 ? (!can_vec_perm_const_p (result_mode, op_mode, sel0, false)
9936 || !can_vec_perm_const_p (result_mode, op_mode, sel1, false))
9937 : !can_vec_perm_const_p (result_mode, op_mode, sel1, false)))
9938 op0 = vec_perm_indices_to_tree (TREE_TYPE (@4), sel2);
9943 (vec_perm @5 @1 { op0; }))
9945 (vec_perm @5 @2 { op0; })))))))))))
9947 /* And the case with swapped outer permute sources. */
9950 (vec_perm (vec_perm@0 @1 @2 VECTOR_CST@3) @5 VECTOR_CST@4)
9951 (if (TYPE_VECTOR_SUBPARTS (type).is_constant ())
9954 machine_mode result_mode = TYPE_MODE (type);
9955 machine_mode op_mode = TYPE_MODE (TREE_TYPE (@1));
9956 int nelts = TYPE_VECTOR_SUBPARTS (type).to_constant ();
9957 vec_perm_builder builder0;
9958 vec_perm_builder builder1;
9959 vec_perm_builder builder2 (nelts, nelts, 2);
9961 (if (tree_to_vec_perm_builder (&builder0, @3)
9962 && tree_to_vec_perm_builder (&builder1, @4))
9965 vec_perm_indices sel0 (builder0, 2, nelts);
9966 vec_perm_indices sel1 (builder1, 2, nelts);
9967 bool use_1 = false, use_2 = false;
9969 for (int i = 0; i < nelts; i++)
9971 if (known_ge ((poly_uint64)sel1[i], sel1.nelts_per_input ()))
9972 builder2.quick_push (sel1[i]);
9975 poly_uint64 j = sel0[sel1[i].to_constant ()];
9976 if (known_lt (j, sel0.nelts_per_input ()))
9981 j -= sel0.nelts_per_input ();
9983 builder2.quick_push (j);
9990 vec_perm_indices sel2 (builder2, 2, nelts);
9991 tree op0 = NULL_TREE;
9992 /* If the new VEC_PERM_EXPR can't be handled but both
9993 original VEC_PERM_EXPRs can, punt.
9994 If one or both of the original VEC_PERM_EXPRs can't be
9995 handled and the new one can't be either, don't increase
9996 number of VEC_PERM_EXPRs that can't be handled. */
9997 if (can_vec_perm_const_p (result_mode, op_mode, sel2, false)
9999 ? (!can_vec_perm_const_p (result_mode, op_mode, sel0, false)
10000 || !can_vec_perm_const_p (result_mode, op_mode, sel1, false))
10001 : !can_vec_perm_const_p (result_mode, op_mode, sel1, false)))
10002 op0 = vec_perm_indices_to_tree (TREE_TYPE (@4), sel2);
10007 (vec_perm @1 @5 { op0; }))
10009 (vec_perm @2 @5 { op0; })))))))))))
10012 /* Match count trailing zeroes for simplify_count_trailing_zeroes in fwprop.
10013 The canonical form is array[((x & -x) * C) >> SHIFT] where C is a magic
10014 constant which when multiplied by a power of 2 contains a unique value
10015 in the top 5 or 6 bits. This is then indexed into a table which maps it
10016 to the number of trailing zeroes. */
10017 (match (ctz_table_index @1 @2 @3)
10018 (rshift (mult (bit_and:c (negate @1) @1) INTEGER_CST@2) INTEGER_CST@3))
10020 (match (cond_expr_convert_p @0 @2 @3 @6)
10021 (cond (simple_comparison@6 @0 @1) (convert@4 @2) (convert@5 @3))
10022 (if (INTEGRAL_TYPE_P (type)
10023 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
10024 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
10025 && INTEGRAL_TYPE_P (TREE_TYPE (@3))
10026 && TYPE_PRECISION (type) != TYPE_PRECISION (TREE_TYPE (@0))
10027 && TYPE_PRECISION (TREE_TYPE (@0))
10028 == TYPE_PRECISION (TREE_TYPE (@2))
10029 && TYPE_PRECISION (TREE_TYPE (@0))
10030 == TYPE_PRECISION (TREE_TYPE (@3))
10031 /* For vect_recog_cond_expr_convert_pattern, @2 and @3 can differ in
10032 signess when convert is truncation, but not ok for extension since
10033 it's sign_extend vs zero_extend. */
10034 && (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type)
10035 || (TYPE_UNSIGNED (TREE_TYPE (@2))
10036 == TYPE_UNSIGNED (TREE_TYPE (@3))))
10038 && single_use (@5))))
10040 (for bit_op (bit_and bit_ior bit_xor)
10041 (match (bitwise_induction_p @0 @2 @3)
10043 (nop_convert1? (bit_not2?@0 (convert3? (lshift integer_onep@1 @2))))
10046 (match (bitwise_induction_p @0 @2 @3)
10048 (nop_convert1? (bit_xor@0 (convert2? (lshift integer_onep@1 @2)) @3))))
10050 /* n - (((n > C1) ? n : C1) & -C2) -> n & C1 for unsigned case.
10051 n - (((n > C1) ? n : C1) & -C2) -> (n <= C1) ? n : (n & C1) for signed case. */
10053 (minus @0 (bit_and (max @0 INTEGER_CST@1) INTEGER_CST@2))
10054 (with { auto i = wi::neg (wi::to_wide (@2)); }
10055 /* Check if -C2 is a power of 2 and C1 = -C2 - 1. */
10056 (if (wi::popcount (i) == 1
10057 && (wi::to_wide (@1)) == (i - 1))
10058 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
10060 (cond (le @0 @1) @0 (bit_and @0 @1))))))
10062 /* -x & 1 -> x & 1. */
10064 (bit_and (negate @0) integer_onep@1)
10065 (if (!TYPE_OVERFLOW_SANITIZED (type))
10068 /* `-a` is just `a` if the type is 1bit wide or when converting
10069 to a 1bit type; similar to the above transformation of `(-x)&1`.
10070 This is used mostly with the transformation of
10071 `a ? ~b : b` into `(-a)^b`.
10072 It also can show up with bitfields. */
10074 (convert? (negate @0))
10075 (if (INTEGRAL_TYPE_P (type)
10076 && TYPE_PRECISION (type) == 1
10077 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0)))
10081 c1 = VEC_PERM_EXPR (a, a, mask)
10082 c2 = VEC_PERM_EXPR (b, b, mask)
10086 c3 = VEC_PERM_EXPR (c, c, mask)
10087 For all integer non-div operations. */
10088 (for op (plus minus mult bit_and bit_ior bit_xor
10091 (op (vec_perm @0 @0 @2) (vec_perm @1 @1 @2))
10092 (if (VECTOR_INTEGER_TYPE_P (type))
10093 (vec_perm (op@3 @0 @1) @3 @2))))
10095 /* Similar for float arithmetic when permutation constant covers
10096 all vector elements. */
10097 (for op (plus minus mult)
10099 (op (vec_perm @0 @0 VECTOR_CST@2) (vec_perm @1 @1 VECTOR_CST@2))
10100 (if (VECTOR_FLOAT_TYPE_P (type)
10101 && TYPE_VECTOR_SUBPARTS (type).is_constant ())
10104 tree perm_cst = @2;
10105 vec_perm_builder builder;
10106 bool full_perm_p = false;
10107 if (tree_to_vec_perm_builder (&builder, perm_cst))
10109 unsigned HOST_WIDE_INT nelts;
10111 nelts = TYPE_VECTOR_SUBPARTS (type).to_constant ();
10112 /* Create a vec_perm_indices for the VECTOR_CST. */
10113 vec_perm_indices sel (builder, 1, nelts);
10115 /* Check if perm indices covers all vector elements. */
10116 if (sel.encoding ().encoded_full_vector_p ())
10118 auto_sbitmap seen (nelts);
10119 bitmap_clear (seen);
10121 unsigned HOST_WIDE_INT count = 0, i;
10123 for (i = 0; i < nelts; i++)
10125 if (!bitmap_set_bit (seen, sel[i].to_constant ()))
10129 full_perm_p = count == nelts;
10134 (vec_perm (op@3 @0 @1) @3 @2))))))