1 /* Match-and-simplify patterns for shared GENERIC and GIMPLE folding.
2 This file is consumed by genmatch which produces gimple-match.cc
3 and generic-match.cc from it.
5 Copyright (C) 2014-2024 Free Software Foundation, Inc.
6 Contributed by Richard Biener <rguenther@suse.de>
7 and Prathamesh Kulkarni <bilbotheelffriend@gmail.com>
9 This file is part of GCC.
11 GCC is free software; you can redistribute it and/or modify it under
12 the terms of the GNU General Public License as published by the Free
13 Software Foundation; either version 3, or (at your option) any later
16 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
17 WARRANTY; without even the implied warranty of MERCHANTABILITY or
18 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21 You should have received a copy of the GNU General Public License
22 along with GCC; see the file COPYING3. If not see
23 <http://www.gnu.org/licenses/>. */
26 /* Generic tree predicates we inherit. */
28 integer_onep integer_zerop integer_all_onesp integer_minus_onep
29 integer_each_onep integer_truep integer_nonzerop
30 real_zerop real_onep real_minus_onep
32 initializer_each_zero_or_onep
34 tree_expr_nonnegative_p
42 bitmask_inv_cst_vector_p)
45 (define_operator_list tcc_comparison
46 lt le eq ne ge gt unordered ordered unlt unle ungt unge uneq ltgt)
47 (define_operator_list inverted_tcc_comparison
48 ge gt ne eq lt le ordered unordered ge gt le lt ltgt uneq)
49 (define_operator_list inverted_tcc_comparison_with_nans
50 unge ungt ne eq unlt unle ordered unordered ge gt le lt ltgt uneq)
51 (define_operator_list swapped_tcc_comparison
52 gt ge eq ne le lt unordered ordered ungt unge unlt unle uneq ltgt)
53 (define_operator_list simple_comparison lt le eq ne ge gt)
54 (define_operator_list swapped_simple_comparison gt ge eq ne le lt)
55 (define_operator_list BSWAP BUILT_IN_BSWAP16 BUILT_IN_BSWAP32
56 BUILT_IN_BSWAP64 BUILT_IN_BSWAP128)
58 #include "cfn-operators.pd"
60 /* Define operand lists for math rounding functions {,i,l,ll}FN,
61 where the versions prefixed with "i" return an int, those prefixed with
62 "l" return a long and those prefixed with "ll" return a long long.
64 Also define operand lists:
66 X<FN>F for all float functions, in the order i, l, ll
67 X<FN> for all double functions, in the same order
68 X<FN>L for all long double functions, in the same order. */
69 #define DEFINE_INT_AND_FLOAT_ROUND_FN(FN) \
70 (define_operator_list X##FN##F BUILT_IN_I##FN##F \
73 (define_operator_list X##FN BUILT_IN_I##FN \
76 (define_operator_list X##FN##L BUILT_IN_I##FN##L \
80 DEFINE_INT_AND_FLOAT_ROUND_FN (FLOOR)
81 DEFINE_INT_AND_FLOAT_ROUND_FN (CEIL)
82 DEFINE_INT_AND_FLOAT_ROUND_FN (ROUND)
83 DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
85 /* Unary operations and their associated IFN_COND_* function. */
86 (define_operator_list UNCOND_UNARY
88 (define_operator_list COND_UNARY
89 IFN_COND_NEG IFN_COND_NOT)
90 (define_operator_list COND_LEN_UNARY
91 IFN_COND_LEN_NEG IFN_COND_LEN_NOT)
93 /* Binary operations and their associated IFN_COND_* function. */
94 (define_operator_list UNCOND_BINARY
96 mult trunc_div trunc_mod rdiv
98 IFN_FMIN IFN_FMAX IFN_COPYSIGN
99 bit_and bit_ior bit_xor
101 (define_operator_list COND_BINARY
102 IFN_COND_ADD IFN_COND_SUB
103 IFN_COND_MUL IFN_COND_DIV IFN_COND_MOD IFN_COND_RDIV
104 IFN_COND_MIN IFN_COND_MAX
105 IFN_COND_FMIN IFN_COND_FMAX IFN_COND_COPYSIGN
106 IFN_COND_AND IFN_COND_IOR IFN_COND_XOR
107 IFN_COND_SHL IFN_COND_SHR)
108 (define_operator_list COND_LEN_BINARY
109 IFN_COND_LEN_ADD IFN_COND_LEN_SUB
110 IFN_COND_LEN_MUL IFN_COND_LEN_DIV
111 IFN_COND_LEN_MOD IFN_COND_LEN_RDIV
112 IFN_COND_LEN_MIN IFN_COND_LEN_MAX
113 IFN_COND_LEN_FMIN IFN_COND_LEN_FMAX IFN_COND_LEN_COPYSIGN
114 IFN_COND_LEN_AND IFN_COND_LEN_IOR IFN_COND_LEN_XOR
115 IFN_COND_LEN_SHL IFN_COND_LEN_SHR)
117 /* Same for ternary operations. */
118 (define_operator_list UNCOND_TERNARY
119 IFN_FMA IFN_FMS IFN_FNMA IFN_FNMS)
120 (define_operator_list COND_TERNARY
121 IFN_COND_FMA IFN_COND_FMS IFN_COND_FNMA IFN_COND_FNMS)
122 (define_operator_list COND_LEN_TERNARY
123 IFN_COND_LEN_FMA IFN_COND_LEN_FMS IFN_COND_LEN_FNMA IFN_COND_LEN_FNMS)
125 /* __atomic_fetch_or_*, __atomic_fetch_xor_*, __atomic_xor_fetch_* */
126 (define_operator_list ATOMIC_FETCH_OR_XOR_N
127 BUILT_IN_ATOMIC_FETCH_OR_1 BUILT_IN_ATOMIC_FETCH_OR_2
128 BUILT_IN_ATOMIC_FETCH_OR_4 BUILT_IN_ATOMIC_FETCH_OR_8
129 BUILT_IN_ATOMIC_FETCH_OR_16
130 BUILT_IN_ATOMIC_FETCH_XOR_1 BUILT_IN_ATOMIC_FETCH_XOR_2
131 BUILT_IN_ATOMIC_FETCH_XOR_4 BUILT_IN_ATOMIC_FETCH_XOR_8
132 BUILT_IN_ATOMIC_FETCH_XOR_16
133 BUILT_IN_ATOMIC_XOR_FETCH_1 BUILT_IN_ATOMIC_XOR_FETCH_2
134 BUILT_IN_ATOMIC_XOR_FETCH_4 BUILT_IN_ATOMIC_XOR_FETCH_8
135 BUILT_IN_ATOMIC_XOR_FETCH_16)
136 /* __sync_fetch_and_or_*, __sync_fetch_and_xor_*, __sync_xor_and_fetch_* */
137 (define_operator_list SYNC_FETCH_OR_XOR_N
138 BUILT_IN_SYNC_FETCH_AND_OR_1 BUILT_IN_SYNC_FETCH_AND_OR_2
139 BUILT_IN_SYNC_FETCH_AND_OR_4 BUILT_IN_SYNC_FETCH_AND_OR_8
140 BUILT_IN_SYNC_FETCH_AND_OR_16
141 BUILT_IN_SYNC_FETCH_AND_XOR_1 BUILT_IN_SYNC_FETCH_AND_XOR_2
142 BUILT_IN_SYNC_FETCH_AND_XOR_4 BUILT_IN_SYNC_FETCH_AND_XOR_8
143 BUILT_IN_SYNC_FETCH_AND_XOR_16
144 BUILT_IN_SYNC_XOR_AND_FETCH_1 BUILT_IN_SYNC_XOR_AND_FETCH_2
145 BUILT_IN_SYNC_XOR_AND_FETCH_4 BUILT_IN_SYNC_XOR_AND_FETCH_8
146 BUILT_IN_SYNC_XOR_AND_FETCH_16)
147 /* __atomic_fetch_and_*. */
148 (define_operator_list ATOMIC_FETCH_AND_N
149 BUILT_IN_ATOMIC_FETCH_AND_1 BUILT_IN_ATOMIC_FETCH_AND_2
150 BUILT_IN_ATOMIC_FETCH_AND_4 BUILT_IN_ATOMIC_FETCH_AND_8
151 BUILT_IN_ATOMIC_FETCH_AND_16)
152 /* __sync_fetch_and_and_*. */
153 (define_operator_list SYNC_FETCH_AND_AND_N
154 BUILT_IN_SYNC_FETCH_AND_AND_1 BUILT_IN_SYNC_FETCH_AND_AND_2
155 BUILT_IN_SYNC_FETCH_AND_AND_4 BUILT_IN_SYNC_FETCH_AND_AND_8
156 BUILT_IN_SYNC_FETCH_AND_AND_16)
158 /* With nop_convert? combine convert? and view_convert? in one pattern
159 plus conditionalize on tree_nop_conversion_p conversions. */
160 (match (nop_convert @0)
162 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))))
163 (match (nop_convert @0)
165 (if (VECTOR_TYPE_P (type) && VECTOR_TYPE_P (TREE_TYPE (@0))
166 && known_eq (TYPE_VECTOR_SUBPARTS (type),
167 TYPE_VECTOR_SUBPARTS (TREE_TYPE (@0)))
168 && tree_nop_conversion_p (TREE_TYPE (type), TREE_TYPE (TREE_TYPE (@0))))))
171 /* These are used by gimple_bitwise_inverted_equal_p to simplify
172 detection of BIT_NOT and comparisons. */
173 (match (bit_not_with_nop @0)
175 (match (bit_not_with_nop @0)
176 (convert (bit_not @0))
177 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))))
178 (for cmp (tcc_comparison)
179 (match (maybe_cmp @0)
181 (match (maybe_cmp @0)
182 (convert (cmp@0 @1 @2))
183 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))))
185 /* `a ^ b` is another form of `a != b` when the type
186 is a 1bit precission integer. */
187 (match (maybe_cmp @0)
189 (if (INTEGRAL_TYPE_P (type)
190 && TYPE_PRECISION (type) == 1)))
193 /* Transform likes of (char) ABS_EXPR <(int) x> into (char) ABSU_EXPR <x>
194 ABSU_EXPR returns unsigned absolute value of the operand and the operand
195 of the ABSU_EXPR will have the corresponding signed type. */
196 (simplify (abs (convert @0))
197 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
198 && !TYPE_UNSIGNED (TREE_TYPE (@0))
199 && element_precision (type) > element_precision (TREE_TYPE (@0)))
200 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
201 (convert (absu:utype @0)))))
204 /* Optimize (X + (X >> (prec - 1))) ^ (X >> (prec - 1)) into abs (X). */
206 (bit_xor:c (plus:c @0 (rshift@2 @0 INTEGER_CST@1)) @2)
207 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
208 && !TYPE_UNSIGNED (TREE_TYPE (@0))
209 && wi::to_widest (@1) == element_precision (TREE_TYPE (@0)) - 1)
213 /* Simplifications of operations with one constant operand and
214 simplifications to constants or single values. */
216 (for op (plus pointer_plus minus bit_ior bit_xor)
218 (op @0 integer_zerop)
221 /* 0 +p index -> (type)index */
223 (pointer_plus integer_zerop @1)
224 (non_lvalue (convert @1)))
226 /* ptr - 0 -> (type)ptr */
228 (pointer_diff @0 integer_zerop)
231 /* See if ARG1 is zero and X + ARG1 reduces to X.
232 Likewise if the operands are reversed. */
234 (plus:c @0 real_zerop@1)
235 (if (fold_real_zero_addition_p (type, @0, @1, 0))
238 /* See if ARG1 is zero and X - ARG1 reduces to X. */
240 (minus @0 real_zerop@1)
241 (if (fold_real_zero_addition_p (type, @0, @1, 1))
244 /* Even if the fold_real_zero_addition_p can't simplify X + 0.0
245 into X, we can optimize (X + 0.0) + 0.0 or (X + 0.0) - 0.0
246 or (X - 0.0) + 0.0 into X + 0.0 and (X - 0.0) - 0.0 into X - 0.0
247 if not -frounding-math. For sNaNs the first operation would raise
248 exceptions but turn the result into qNan, so the second operation
249 would not raise it. */
250 (for inner_op (plus minus)
251 (for outer_op (plus minus)
253 (outer_op (inner_op@3 @0 REAL_CST@1) REAL_CST@2)
256 && !HONOR_SIGN_DEPENDENT_ROUNDING (type))
257 (with { bool inner_plus = ((inner_op == PLUS_EXPR)
258 ^ REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@1)));
260 = ((outer_op == PLUS_EXPR)
261 ^ REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@2))); }
262 (if (outer_plus && !inner_plus)
267 This is unsafe for certain floats even in non-IEEE formats.
268 In IEEE, it is unsafe because it does wrong for NaNs.
269 PR middle-end/98420: x - x may be -0.0 with FE_DOWNWARD.
270 Also note that operand_equal_p is always false if an operand
274 (if (!FLOAT_TYPE_P (type)
275 || (!tree_expr_maybe_nan_p (@0)
276 && !tree_expr_maybe_infinite_p (@0)
277 && (!HONOR_SIGN_DEPENDENT_ROUNDING (type)
278 || !HONOR_SIGNED_ZEROS (type))))
279 { build_zero_cst (type); }))
281 (pointer_diff @@0 @0)
282 { build_zero_cst (type); })
285 (mult @0 integer_zerop@1)
288 /* -x == x -> x == 0 */
291 (cmp:c @0 (negate @0))
292 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
293 && !TYPE_OVERFLOW_WRAPS (TREE_TYPE(@0)))
294 (cmp @0 { build_zero_cst (TREE_TYPE(@0)); }))))
296 /* Maybe fold x * 0 to 0. The expressions aren't the same
297 when x is NaN, since x * 0 is also NaN. Nor are they the
298 same in modes with signed zeros, since multiplying a
299 negative value by 0 gives -0, not +0. Nor when x is +-Inf,
300 since x * 0 is NaN. */
302 (mult @0 real_zerop@1)
303 (if (!tree_expr_maybe_nan_p (@0)
304 && (!HONOR_NANS (type) || !tree_expr_maybe_infinite_p (@0))
305 && (!HONOR_SIGNED_ZEROS (type) || tree_expr_nonnegative_p (@0)))
308 /* In IEEE floating point, x*1 is not equivalent to x for snans.
309 Likewise for complex arithmetic with signed zeros. */
312 (if (!tree_expr_maybe_signaling_nan_p (@0)
313 && (!HONOR_SIGNED_ZEROS (type)
314 || !COMPLEX_FLOAT_TYPE_P (type)))
317 /* Transform x * -1.0 into -x. */
319 (mult @0 real_minus_onep)
320 (if (!tree_expr_maybe_signaling_nan_p (@0)
321 && (!HONOR_SIGNED_ZEROS (type)
322 || !COMPLEX_FLOAT_TYPE_P (type)))
325 /* Transform x * { 0 or 1, 0 or 1, ... } into x & { 0 or -1, 0 or -1, ...},
326 unless the target has native support for the former but not the latter. */
328 (mult @0 VECTOR_CST@1)
329 (if (initializer_each_zero_or_onep (@1)
330 && !HONOR_SNANS (type)
331 && !HONOR_SIGNED_ZEROS (type))
332 (with { tree itype = FLOAT_TYPE_P (type) ? unsigned_type_for (type) : type; }
334 && (!VECTOR_MODE_P (TYPE_MODE (type))
335 || (VECTOR_MODE_P (TYPE_MODE (itype))
336 && optab_handler (and_optab,
337 TYPE_MODE (itype)) != CODE_FOR_nothing)))
338 (view_convert (bit_and:itype (view_convert @0)
339 (ne @1 { build_zero_cst (type); })))))))
341 /* In SWAR (SIMD within a register) code a signed comparison of packed data
342 can be constructed with a particular combination of shift, bitwise and,
343 and multiplication by constants. If that code is vectorized we can
344 convert this pattern into a more efficient vector comparison. */
346 (mult (bit_and (rshift @0 uniform_integer_cst_p@1)
347 uniform_integer_cst_p@2)
348 uniform_integer_cst_p@3)
350 tree rshift_cst = uniform_integer_cst_p (@1);
351 tree bit_and_cst = uniform_integer_cst_p (@2);
352 tree mult_cst = uniform_integer_cst_p (@3);
354 /* Make sure we're working with vectors and uniform vector constants. */
355 (if (VECTOR_TYPE_P (type)
356 && tree_fits_uhwi_p (rshift_cst)
357 && tree_fits_uhwi_p (mult_cst)
358 && tree_fits_uhwi_p (bit_and_cst))
359 /* Compute what constants would be needed for this to represent a packed
360 comparison based on the shift amount denoted by RSHIFT_CST. */
362 HOST_WIDE_INT vec_elem_bits = vector_element_bits (type);
363 poly_int64 vec_nelts = TYPE_VECTOR_SUBPARTS (type);
364 poly_int64 vec_bits = vec_elem_bits * vec_nelts;
365 unsigned HOST_WIDE_INT cmp_bits_i, bit_and_i, mult_i;
366 unsigned HOST_WIDE_INT target_mult_i, target_bit_and_i;
367 cmp_bits_i = tree_to_uhwi (rshift_cst) + 1;
368 mult_i = tree_to_uhwi (mult_cst);
369 target_mult_i = (HOST_WIDE_INT_1U << cmp_bits_i) - 1;
370 bit_and_i = tree_to_uhwi (bit_and_cst);
371 target_bit_and_i = 0;
373 /* The bit pattern in BIT_AND_I should be a mask for the least
374 significant bit of each packed element that is CMP_BITS wide. */
375 for (unsigned i = 0; i < vec_elem_bits / cmp_bits_i; i++)
376 target_bit_and_i = (target_bit_and_i << cmp_bits_i) | 1U;
378 (if ((exact_log2 (cmp_bits_i)) >= 0
379 && cmp_bits_i < HOST_BITS_PER_WIDE_INT
380 && multiple_p (vec_bits, cmp_bits_i)
381 && vec_elem_bits <= HOST_BITS_PER_WIDE_INT
382 && target_mult_i == mult_i
383 && target_bit_and_i == bit_and_i)
384 /* Compute the vector shape for the comparison and check if the target is
385 able to expand the comparison with that type. */
387 /* We're doing a signed comparison. */
388 tree cmp_type = build_nonstandard_integer_type (cmp_bits_i, 0);
389 poly_int64 vector_type_nelts = exact_div (vec_bits, cmp_bits_i);
390 tree vec_cmp_type = build_vector_type (cmp_type, vector_type_nelts);
391 tree vec_truth_type = truth_type_for (vec_cmp_type);
392 tree zeros = build_zero_cst (vec_cmp_type);
393 tree ones = build_all_ones_cst (vec_cmp_type);
395 (if (expand_vec_cmp_expr_p (vec_cmp_type, vec_truth_type, LT_EXPR)
396 && expand_vec_cond_expr_p (vec_cmp_type, vec_truth_type, LT_EXPR))
397 (view_convert:type (vec_cond (lt:vec_truth_type
398 (view_convert:vec_cmp_type @0)
400 { ones; } { zeros; })))))))))
402 (for cmp (gt ge lt le)
403 outp (convert convert negate negate)
404 outn (negate negate convert convert)
405 /* Transform X * (X > 0.0 ? 1.0 : -1.0) into abs(X). */
406 /* Transform X * (X >= 0.0 ? 1.0 : -1.0) into abs(X). */
407 /* Transform X * (X < 0.0 ? 1.0 : -1.0) into -abs(X). */
408 /* Transform X * (X <= 0.0 ? 1.0 : -1.0) into -abs(X). */
410 (mult:c @0 (cond (cmp @0 real_zerop) real_onep@1 real_minus_onep))
411 (if (!tree_expr_maybe_nan_p (@0) && !HONOR_SIGNED_ZEROS (type))
413 /* Transform X * (X > 0.0 ? -1.0 : 1.0) into -abs(X). */
414 /* Transform X * (X >= 0.0 ? -1.0 : 1.0) into -abs(X). */
415 /* Transform X * (X < 0.0 ? -1.0 : 1.0) into abs(X). */
416 /* Transform X * (X <= 0.0 ? -1.0 : 1.0) into abs(X). */
418 (mult:c @0 (cond (cmp @0 real_zerop) real_minus_onep real_onep@1))
419 (if (!tree_expr_maybe_nan_p (@0) && !HONOR_SIGNED_ZEROS (type))
422 /* Transform X * copysign (1.0, X) into abs(X). */
424 (mult:c @0 (COPYSIGN_ALL real_onep @0))
425 (if (!tree_expr_maybe_nan_p (@0) && !HONOR_SIGNED_ZEROS (type))
428 /* Transform X * copysign (1.0, -X) into -abs(X). */
430 (mult:c @0 (COPYSIGN_ALL real_onep (negate @0)))
431 (if (!tree_expr_maybe_nan_p (@0) && !HONOR_SIGNED_ZEROS (type))
434 /* Transform copysign (CST, X) into copysign (ABS(CST), X). */
436 (COPYSIGN_ALL REAL_CST@0 @1)
437 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@0)))
438 (COPYSIGN_ALL (negate @0) @1)))
440 /* Transform c ? x * copysign (1, y) : z to c ? x ^ signs(y) : z.
441 tree-ssa-math-opts.cc does the corresponding optimization for
442 unconditional multiplications (via xorsign). */
444 (IFN_COND_MUL:c @0 @1 (IFN_COPYSIGN real_onep @2) @3)
445 (with { tree signs = sign_mask_for (type); }
447 (with { tree inttype = TREE_TYPE (signs); }
449 (IFN_COND_XOR:inttype @0
450 (view_convert:inttype @1)
451 (bit_and (view_convert:inttype @2) { signs; })
452 (view_convert:inttype @3)))))))
454 /* (x >= 0 ? x : 0) + (x <= 0 ? -x : 0) -> abs x. */
456 (plus:c (max @0 integer_zerop) (max (negate @0) integer_zerop))
459 /* X * 1, X / 1 -> X. */
460 (for op (mult trunc_div ceil_div floor_div round_div exact_div)
465 /* (A / (1 << B)) -> (A >> B).
466 Only for unsigned A. For signed A, this would not preserve rounding
468 For example: (-1 / ( 1 << B)) != -1 >> B.
469 Also handle widening conversions, like:
470 (A / (unsigned long long) (1U << B)) -> (A >> B)
472 (A / (unsigned long long) (1 << B)) -> (A >> B).
473 If the left shift is signed, it can be done only if the upper bits
474 of A starting from shift's type sign bit are zero, as
475 (unsigned long long) (1 << 31) is -2147483648ULL, not 2147483648ULL,
476 so it is valid only if A >> 31 is zero. */
478 (trunc_div (convert?@0 @3) (convert2? (lshift integer_onep@1 @2)))
479 (if ((TYPE_UNSIGNED (type) || tree_expr_nonnegative_p (@0))
480 && (!VECTOR_TYPE_P (type)
481 || target_supports_op_p (type, RSHIFT_EXPR, optab_vector)
482 || target_supports_op_p (type, RSHIFT_EXPR, optab_scalar))
483 && (useless_type_conversion_p (type, TREE_TYPE (@1))
484 || (element_precision (type) >= element_precision (TREE_TYPE (@1))
485 && (TYPE_UNSIGNED (TREE_TYPE (@1))
486 || (element_precision (type)
487 == element_precision (TREE_TYPE (@1)))
488 || (INTEGRAL_TYPE_P (type)
489 && (tree_nonzero_bits (@0)
490 & wi::mask (element_precision (TREE_TYPE (@1)) - 1,
492 element_precision (type))) == 0)))))
493 (if (!VECTOR_TYPE_P (type)
494 && useless_type_conversion_p (TREE_TYPE (@3), TREE_TYPE (@1))
495 && element_precision (TREE_TYPE (@3)) < element_precision (type))
496 (convert (rshift @3 @2))
499 /* Preserve explicit divisions by 0: the C++ front-end wants to detect
500 undefined behavior in constexpr evaluation, and assuming that the division
501 traps enables better optimizations than these anyway. */
502 (for div (trunc_div ceil_div floor_div round_div exact_div)
503 /* 0 / X is always zero. */
505 (div integer_zerop@0 @1)
506 /* But not for 0 / 0 so that we can get the proper warnings and errors. */
507 (if (!integer_zerop (@1))
511 (div @0 integer_minus_onep@1)
512 (if (!TYPE_UNSIGNED (type))
514 /* X / bool_range_Y is X. */
517 (if (INTEGRAL_TYPE_P (type)
518 && ssa_name_has_boolean_range (@1)
519 && !flag_non_call_exceptions)
524 /* But not for 0 / 0 so that we can get the proper warnings and errors.
525 And not for _Fract types where we can't build 1. */
526 (if (!ALL_FRACT_MODE_P (TYPE_MODE (type))
527 && !integer_zerop (@0)
528 && (!flag_non_call_exceptions || tree_expr_nonzero_p (@0)))
529 { build_one_cst (type); }))
530 /* X / abs (X) is X < 0 ? -1 : 1. */
533 (if (INTEGRAL_TYPE_P (type)
534 && TYPE_OVERFLOW_UNDEFINED (type)
535 && !integer_zerop (@0)
536 && (!flag_non_call_exceptions || tree_expr_nonzero_p (@0)))
537 (cond (lt @0 { build_zero_cst (type); })
538 { build_minus_one_cst (type); } { build_one_cst (type); })))
541 (div:C @0 (negate @0))
542 (if ((INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
543 && TYPE_OVERFLOW_UNDEFINED (type)
544 && !integer_zerop (@0)
545 && (!flag_non_call_exceptions || tree_expr_nonzero_p (@0)))
546 { build_minus_one_cst (type); })))
548 /* For unsigned integral types, FLOOR_DIV_EXPR is the same as
549 TRUNC_DIV_EXPR. Rewrite into the latter in this case. Similarly
550 for MOD instead of DIV. */
551 (for floor_divmod (floor_div floor_mod)
552 trunc_divmod (trunc_div trunc_mod)
555 (if ((INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
556 && TYPE_UNSIGNED (type))
557 (trunc_divmod @0 @1))))
559 /* 1 / X -> X == 1 for unsigned integer X.
560 1 / X -> X >= -1 && X <= 1 ? X : 0 for signed integer X.
561 But not for 1 / 0 so that we can get proper warnings and errors,
562 and not for 1-bit integers as they are edge cases better handled
565 (trunc_div integer_onep@0 @1)
566 (if (INTEGRAL_TYPE_P (type)
567 && TYPE_PRECISION (type) > 1
568 && !integer_zerop (@1)
569 && (!flag_non_call_exceptions || tree_expr_nonzero_p (@1)))
570 (if (TYPE_UNSIGNED (type))
571 (convert (eq:boolean_type_node @1 { build_one_cst (type); }))
572 (with { tree utype = unsigned_type_for (type); }
573 (cond (le (plus (convert:utype @1) { build_one_cst (utype); })
574 { build_int_cst (utype, 2); })
575 @1 { build_zero_cst (type); })))))
577 /* Combine two successive divisions. Note that combining ceil_div
578 and floor_div is trickier and combining round_div even more so. */
579 (for div (trunc_div exact_div)
581 (div (div@3 @0 INTEGER_CST@1) INTEGER_CST@2)
583 wi::overflow_type overflow;
584 wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@2),
585 TYPE_SIGN (type), &overflow);
587 (if (div == EXACT_DIV_EXPR
588 || optimize_successive_divisions_p (@2, @3))
590 (div @0 { wide_int_to_tree (type, mul); })
591 (if (TYPE_UNSIGNED (type)
592 || mul != wi::min_value (TYPE_PRECISION (type), SIGNED))
593 { build_zero_cst (type); }))))))
595 /* Combine successive multiplications. Similar to above, but handling
596 overflow is different. */
598 (mult (mult @0 INTEGER_CST@1) INTEGER_CST@2)
600 wi::overflow_type overflow;
601 wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@2),
602 TYPE_SIGN (type), &overflow);
604 /* Skip folding on overflow: the only special case is @1 * @2 == -INT_MIN,
605 otherwise undefined overflow implies that @0 must be zero. */
606 (if (!overflow || TYPE_OVERFLOW_WRAPS (type))
607 (mult @0 { wide_int_to_tree (type, mul); }))))
609 /* Similar to above, but there could be an extra add/sub between
610 successive multuiplications. */
612 (mult (plus:s (mult:s@4 @0 INTEGER_CST@1) INTEGER_CST@2) INTEGER_CST@3)
614 bool overflowed = true;
615 wi::overflow_type ovf1, ovf2;
616 wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@3),
617 TYPE_SIGN (type), &ovf1);
618 wide_int add = wi::mul (wi::to_wide (@2), wi::to_wide (@3),
619 TYPE_SIGN (type), &ovf2);
620 if (TYPE_OVERFLOW_UNDEFINED (type))
624 if (ovf1 == wi::OVF_NONE && ovf2 == wi::OVF_NONE
625 && get_global_range_query ()->range_of_expr (vr0, @4)
626 && !vr0.varying_p () && !vr0.undefined_p ())
628 wide_int wmin0 = vr0.lower_bound ();
629 wide_int wmax0 = vr0.upper_bound ();
630 wmin0 = wi::mul (wmin0, wi::to_wide (@3), TYPE_SIGN (type), &ovf1);
631 wmax0 = wi::mul (wmax0, wi::to_wide (@3), TYPE_SIGN (type), &ovf2);
632 if (ovf1 == wi::OVF_NONE && ovf2 == wi::OVF_NONE)
634 wi::add (wmin0, add, TYPE_SIGN (type), &ovf1);
635 wi::add (wmax0, add, TYPE_SIGN (type), &ovf2);
636 if (ovf1 == wi::OVF_NONE && ovf2 == wi::OVF_NONE)
645 /* Skip folding on overflow. */
647 (plus (mult @0 { wide_int_to_tree (type, mul); })
648 { wide_int_to_tree (type, add); }))))
650 /* Similar to above, but a multiplication between successive additions. */
652 (plus (mult:s (plus:s @0 INTEGER_CST@1) INTEGER_CST@2) INTEGER_CST@3)
654 bool overflowed = true;
655 wi::overflow_type ovf1;
656 wi::overflow_type ovf2;
657 wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@2),
658 TYPE_SIGN (type), &ovf1);
659 wide_int add = wi::add (mul, wi::to_wide (@3),
660 TYPE_SIGN (type), &ovf2);
661 if (TYPE_OVERFLOW_UNDEFINED (type))
665 if (ovf1 == wi::OVF_NONE && ovf2 == wi::OVF_NONE
666 && get_global_range_query ()->range_of_expr (vr0, @0)
667 && !vr0.varying_p () && !vr0.undefined_p ())
669 wide_int wmin0 = vr0.lower_bound ();
670 wide_int wmax0 = vr0.upper_bound ();
671 wmin0 = wi::mul (wmin0, wi::to_wide (@2), TYPE_SIGN (type), &ovf1);
672 wmax0 = wi::mul (wmax0, wi::to_wide (@2), TYPE_SIGN (type), &ovf2);
673 if (ovf1 == wi::OVF_NONE && ovf2 == wi::OVF_NONE)
675 wi::add (wmin0, mul, TYPE_SIGN (type), &ovf1);
676 wi::add (wmax0, mul, TYPE_SIGN (type), &ovf2);
677 if (ovf1 == wi::OVF_NONE && ovf2 == wi::OVF_NONE)
686 /* Skip folding on overflow. */
688 (plus (mult @0 @2) { wide_int_to_tree (type, add); }))))
690 /* Optimize A / A to 1.0 if we don't care about
691 NaNs or Infinities. */
694 (if (FLOAT_TYPE_P (type)
695 && ! HONOR_NANS (type)
696 && ! HONOR_INFINITIES (type))
697 { build_one_cst (type); }))
699 /* Optimize -A / A to -1.0 if we don't care about
700 NaNs or Infinities. */
702 (rdiv:C @0 (negate @0))
703 (if (FLOAT_TYPE_P (type)
704 && ! HONOR_NANS (type)
705 && ! HONOR_INFINITIES (type))
706 { build_minus_one_cst (type); }))
708 /* PR71078: x / abs(x) -> copysign (1.0, x) */
710 (rdiv:C (convert? @0) (convert? (abs @0)))
711 (if (SCALAR_FLOAT_TYPE_P (type)
712 && ! HONOR_NANS (type)
713 && ! HONOR_INFINITIES (type))
715 (if (types_match (type, float_type_node))
716 (BUILT_IN_COPYSIGNF { build_one_cst (type); } (convert @0)))
717 (if (types_match (type, double_type_node))
718 (BUILT_IN_COPYSIGN { build_one_cst (type); } (convert @0)))
719 (if (types_match (type, long_double_type_node))
720 (BUILT_IN_COPYSIGNL { build_one_cst (type); } (convert @0))))))
722 /* In IEEE floating point, x/1 is not equivalent to x for snans. */
725 (if (!tree_expr_maybe_signaling_nan_p (@0))
728 /* In IEEE floating point, x/-1 is not equivalent to -x for snans. */
730 (rdiv @0 real_minus_onep)
731 (if (!tree_expr_maybe_signaling_nan_p (@0))
734 (if (flag_reciprocal_math)
735 /* Convert (A/B)/C to A/(B*C). */
737 (rdiv (rdiv:s @0 @1) @2)
738 (rdiv @0 (mult @1 @2)))
740 /* Canonicalize x / (C1 * y) to (x * C2) / y. */
742 (rdiv @0 (mult:s @1 REAL_CST@2))
744 { tree tem = const_binop (RDIV_EXPR, type, build_one_cst (type), @2); }
746 (rdiv (mult @0 { tem; } ) @1))))
748 /* Convert A/(B/C) to (A/B)*C */
750 (rdiv @0 (rdiv:s @1 @2))
751 (mult (rdiv @0 @1) @2)))
753 /* Simplify x / (- y) to -x / y. */
755 (rdiv @0 (negate @1))
756 (rdiv (negate @0) @1))
758 (if (flag_unsafe_math_optimizations)
759 /* Simplify (C / x op 0.0) to x op 0.0 for C != 0, C != Inf/Nan.
760 Since C / x may underflow to zero, do this only for unsafe math. */
761 (for op (lt le gt ge)
764 (op (rdiv REAL_CST@0 @1) real_zerop@2)
765 (if (!HONOR_SIGNED_ZEROS (@1) && !HONOR_INFINITIES (@1))
767 (if (real_less (&dconst0, TREE_REAL_CST_PTR (@0)))
769 /* For C < 0, use the inverted operator. */
770 (if (real_less (TREE_REAL_CST_PTR (@0), &dconst0))
773 /* Optimize (X & (-A)) / A where A is a power of 2, to X >> log2(A) */
774 (for div (trunc_div ceil_div floor_div round_div exact_div)
776 (div (convert? (bit_and @0 INTEGER_CST@1)) INTEGER_CST@2)
777 (if (integer_pow2p (@2)
778 && tree_int_cst_sgn (@2) > 0
779 && tree_nop_conversion_p (type, TREE_TYPE (@0))
780 && wi::to_wide (@2) + wi::to_wide (@1) == 0)
782 { build_int_cst (integer_type_node,
783 wi::exact_log2 (wi::to_wide (@2))); }))))
785 /* If ARG1 is a constant, we can convert this to a multiply by the
786 reciprocal. This does not have the same rounding properties,
787 so only do this if -freciprocal-math. We can actually
788 always safely do it if ARG1 is a power of two, but it's hard to
789 tell if it is or not in a portable manner. */
790 (for cst (REAL_CST COMPLEX_CST VECTOR_CST)
794 (if (flag_reciprocal_math
797 { tree tem = const_binop (RDIV_EXPR, type, build_one_cst (type), @1); }
799 (mult @0 { tem; } )))
800 (if (cst != COMPLEX_CST)
801 (with { tree inverse = exact_inverse (type, @1); }
803 (mult @0 { inverse; } ))))))))
805 (for mod (ceil_mod floor_mod round_mod trunc_mod)
806 /* 0 % X is always zero. */
808 (mod integer_zerop@0 @1)
809 /* But not for 0 % 0 so that we can get the proper warnings and errors. */
810 (if (!integer_zerop (@1))
812 /* X % 1 is always zero. */
814 (mod @0 integer_onep)
815 { build_zero_cst (type); })
816 /* X % -1 is zero. */
818 (mod @0 integer_minus_onep@1)
819 (if (!TYPE_UNSIGNED (type))
820 { build_zero_cst (type); }))
824 /* But not for 0 % 0 so that we can get the proper warnings and errors. */
825 (if (!integer_zerop (@0))
826 { build_zero_cst (type); }))
827 /* (X % Y) % Y is just X % Y. */
829 (mod (mod@2 @0 @1) @1)
831 /* From extract_muldiv_1: (X * C1) % C2 is zero if C1 is a multiple of C2. */
833 (mod (mult @0 INTEGER_CST@1) INTEGER_CST@2)
834 (if (ANY_INTEGRAL_TYPE_P (type)
835 && TYPE_OVERFLOW_UNDEFINED (type)
836 && wi::multiple_of_p (wi::to_wide (@1), wi::to_wide (@2),
838 { build_zero_cst (type); }))
839 /* For (X % C) == 0, if X is signed and C is power of 2, use unsigned
840 modulo and comparison, since it is simpler and equivalent. */
843 (cmp (mod @0 integer_pow2p@2) integer_zerop@1)
844 (if (!TYPE_UNSIGNED (TREE_TYPE (@0)))
845 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
846 (cmp (mod (convert:utype @0) (convert:utype @2)) (convert:utype @1)))))))
848 /* X % -C is the same as X % C. */
850 (trunc_mod @0 INTEGER_CST@1)
851 (if (TYPE_SIGN (type) == SIGNED
852 && !TREE_OVERFLOW (@1)
853 && wi::neg_p (wi::to_wide (@1))
854 && !TYPE_OVERFLOW_TRAPS (type)
855 /* Avoid this transformation if C is INT_MIN, i.e. C == -C. */
856 && !sign_bit_p (@1, @1))
857 (trunc_mod @0 (negate @1))))
859 /* X % -Y is the same as X % Y. */
861 (trunc_mod @0 (convert? (negate @1)))
862 (if (INTEGRAL_TYPE_P (type)
863 && !TYPE_UNSIGNED (type)
864 && !TYPE_OVERFLOW_TRAPS (type)
865 && tree_nop_conversion_p (type, TREE_TYPE (@1))
866 /* Avoid this transformation if X might be INT_MIN or
867 Y might be -1, because we would then change valid
868 INT_MIN % -(-1) into invalid INT_MIN % -1. */
869 && (expr_not_equal_to (@0, wi::to_wide (TYPE_MIN_VALUE (type)))
870 || expr_not_equal_to (@1, wi::minus_one (TYPE_PRECISION
872 (trunc_mod @0 (convert @1))))
874 /* X - (X / Y) * Y is the same as X % Y. */
876 (minus (convert1? @0) (convert2? (mult:c (trunc_div @@0 @@1) @1)))
877 (if (INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
878 (convert (trunc_mod @0 @1))))
880 /* x * (1 + y / x) - y -> x - y % x */
882 (minus (mult:cs @0 (plus:s (trunc_div:s @1 @0) integer_onep)) @1)
883 (if (INTEGRAL_TYPE_P (type))
884 (minus @0 (trunc_mod @1 @0))))
886 /* Optimize TRUNC_MOD_EXPR by a power of two into a BIT_AND_EXPR,
887 i.e. "X % C" into "X & (C - 1)", if X and C are positive.
888 Also optimize A % (C << N) where C is a power of 2,
889 to A & ((C << N) - 1).
890 Also optimize "A shift (B % C)", if C is a power of 2, to
891 "A shift (B & (C - 1))". SHIFT operation include "<<" and ">>"
892 and assume (B % C) is nonnegative as shifts negative values would
894 (match (power_of_two_cand @1)
896 (match (power_of_two_cand @1)
897 (lshift INTEGER_CST@1 @2))
898 (for mod (trunc_mod floor_mod)
899 (for shift (lshift rshift)
901 (shift @0 (mod @1 (power_of_two_cand@2 @3)))
902 (if (integer_pow2p (@3) && tree_int_cst_sgn (@3) > 0)
903 (shift @0 (bit_and @1 (minus @2 { build_int_cst (TREE_TYPE (@2),
906 (mod @0 (convert? (power_of_two_cand@1 @2)))
907 (if ((TYPE_UNSIGNED (type) || tree_expr_nonnegative_p (@0))
908 /* Allow any integral conversions of the divisor, except
909 conversion from narrower signed to wider unsigned type
910 where if @1 would be negative power of two, the divisor
911 would not be a power of two. */
912 && INTEGRAL_TYPE_P (type)
913 && INTEGRAL_TYPE_P (TREE_TYPE (@1))
914 && (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@1))
915 || TYPE_UNSIGNED (TREE_TYPE (@1))
916 || !TYPE_UNSIGNED (type))
917 && integer_pow2p (@2) && tree_int_cst_sgn (@2) > 0)
918 (with { tree utype = TREE_TYPE (@1);
919 if (!TYPE_OVERFLOW_WRAPS (utype))
920 utype = unsigned_type_for (utype); }
921 (bit_and @0 (convert (minus (convert:utype @1)
922 { build_one_cst (utype); })))))))
924 /* Simplify (unsigned t * 2)/2 -> unsigned t & 0x7FFFFFFF. */
926 (trunc_div (mult @0 integer_pow2p@1) @1)
927 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) && TYPE_UNSIGNED (TREE_TYPE (@0)))
928 (bit_and @0 { wide_int_to_tree
929 (type, wi::mask (TYPE_PRECISION (type)
930 - wi::exact_log2 (wi::to_wide (@1)),
931 false, TYPE_PRECISION (type))); })))
933 /* Simplify (unsigned t / 2) * 2 -> unsigned t & ~1. */
935 (mult (trunc_div @0 integer_pow2p@1) @1)
936 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) && TYPE_UNSIGNED (TREE_TYPE (@0)))
937 (bit_and @0 (negate @1))))
939 (for div (trunc_div ceil_div floor_div round_div exact_div)
940 /* Simplify (t * u) / u -> t. */
942 (div (mult:c @0 @1) @1)
943 (if (ANY_INTEGRAL_TYPE_P (type))
944 (if (TYPE_OVERFLOW_UNDEFINED (type) && !TYPE_OVERFLOW_SANITIZED (type))
947 (with {value_range vr0, vr1;}
948 (if (INTEGRAL_TYPE_P (type)
949 && get_range_query (cfun)->range_of_expr (vr0, @0)
950 && get_range_query (cfun)->range_of_expr (vr1, @1)
951 && range_op_handler (MULT_EXPR).overflow_free_p (vr0, vr1))
956 /* Simplify (t * u) / v -> t * (u / v) if u is multiple of v. */
958 (div (mult @0 INTEGER_CST@1) INTEGER_CST@2)
959 (if (INTEGRAL_TYPE_P (type)
960 && wi::multiple_of_p (wi::to_widest (@1), wi::to_widest (@2), SIGNED))
961 (if (TYPE_OVERFLOW_UNDEFINED (type) && !TYPE_OVERFLOW_SANITIZED (type))
962 (mult @0 (div! @1 @2))
963 (with {value_range vr0, vr1;}
964 (if (get_range_query (cfun)->range_of_expr (vr0, @0)
965 && get_range_query (cfun)->range_of_expr (vr1, @1)
966 && range_op_handler (MULT_EXPR).overflow_free_p (vr0, vr1))
967 (mult @0 (div! @1 @2))))
970 /* Simplify (t * u) / (t * v) -> (u / v) if u is multiple of v. */
972 (div (mult @0 INTEGER_CST@1) (mult @0 INTEGER_CST@2))
973 (if (INTEGRAL_TYPE_P (type)
974 && wi::multiple_of_p (wi::to_widest (@1), wi::to_widest (@2), SIGNED))
975 (if (TYPE_OVERFLOW_UNDEFINED (type) && !TYPE_OVERFLOW_SANITIZED (type))
978 (with {value_range vr0, vr1, vr2;}
979 (if (get_range_query (cfun)->range_of_expr (vr0, @0)
980 && get_range_query (cfun)->range_of_expr (vr1, @1)
981 && get_range_query (cfun)->range_of_expr (vr2, @2)
982 && range_op_handler (MULT_EXPR).overflow_free_p (vr0, vr1)
983 && range_op_handler (MULT_EXPR).overflow_free_p (vr0, vr2))
989 (for div (trunc_div exact_div)
990 /* Simplify (X + M*N) / N -> X / N + M. */
992 (div (plus:c@4 @0 (mult:c@3 @1 @2)) @2)
993 (with {value_range vr0, vr1, vr2, vr3, vr4;}
994 (if (INTEGRAL_TYPE_P (type)
995 && get_range_query (cfun)->range_of_expr (vr1, @1)
996 && get_range_query (cfun)->range_of_expr (vr2, @2)
997 /* "N*M" doesn't overflow. */
998 && range_op_handler (MULT_EXPR).overflow_free_p (vr1, vr2)
999 && get_range_query (cfun)->range_of_expr (vr0, @0)
1000 && get_range_query (cfun)->range_of_expr (vr3, @3)
1001 /* "X+(N*M)" doesn't overflow. */
1002 && range_op_handler (PLUS_EXPR).overflow_free_p (vr0, vr3)
1003 && get_range_query (cfun)->range_of_expr (vr4, @4)
1004 && !vr4.undefined_p ()
1005 /* "X+N*M" is not with opposite sign as "X". */
1006 && (TYPE_UNSIGNED (type)
1007 || (vr0.nonnegative_p () && vr4.nonnegative_p ())
1008 || (vr0.nonpositive_p () && vr4.nonpositive_p ())))
1009 (plus (div @0 @2) @1))))
1011 /* Simplify (X - M*N) / N -> X / N - M. */
1013 (div (minus@4 @0 (mult:c@3 @1 @2)) @2)
1014 (with {value_range vr0, vr1, vr2, vr3, vr4;}
1015 (if (INTEGRAL_TYPE_P (type)
1016 && get_range_query (cfun)->range_of_expr (vr1, @1)
1017 && get_range_query (cfun)->range_of_expr (vr2, @2)
1018 /* "N * M" doesn't overflow. */
1019 && range_op_handler (MULT_EXPR).overflow_free_p (vr1, vr2)
1020 && get_range_query (cfun)->range_of_expr (vr0, @0)
1021 && get_range_query (cfun)->range_of_expr (vr3, @3)
1022 /* "X - (N*M)" doesn't overflow. */
1023 && range_op_handler (MINUS_EXPR).overflow_free_p (vr0, vr3)
1024 && get_range_query (cfun)->range_of_expr (vr4, @4)
1025 && !vr4.undefined_p ()
1026 /* "X-N*M" is not with opposite sign as "X". */
1027 && (TYPE_UNSIGNED (type)
1028 || (vr0.nonnegative_p () && vr4.nonnegative_p ())
1029 || (vr0.nonpositive_p () && vr4.nonpositive_p ())))
1030 (minus (div @0 @2) @1)))))
1033 (X + C) / N -> X / N + C / N where C is multiple of N.
1034 (X + C) >> N -> X >> N + C>>N if low N bits of C is 0. */
1035 (for op (trunc_div exact_div rshift)
1037 (op (plus@3 @0 INTEGER_CST@1) INTEGER_CST@2)
1040 wide_int c = wi::to_wide (@1);
1041 wide_int n = wi::to_wide (@2);
1042 bool shift = op == RSHIFT_EXPR;
1043 #define plus_op1(v) (shift ? wi::rshift (v, n, TYPE_SIGN (type)) \
1044 : wi::div_trunc (v, n, TYPE_SIGN (type)))
1045 #define exact_mod(v) (shift ? wi::ctz (v) >= n.to_shwi () \
1046 : wi::multiple_of_p (v, n, TYPE_SIGN (type)))
1047 value_range vr0, vr1, vr3;
1049 (if (INTEGRAL_TYPE_P (type)
1050 && get_range_query (cfun)->range_of_expr (vr0, @0))
1052 && get_range_query (cfun)->range_of_expr (vr1, @1)
1053 /* "X+C" doesn't overflow. */
1054 && range_op_handler (PLUS_EXPR).overflow_free_p (vr0, vr1)
1055 && get_range_query (cfun)->range_of_expr (vr3, @3)
1056 && !vr3.undefined_p ()
1057 /* "X+C" and "X" are not of opposite sign. */
1058 && (TYPE_UNSIGNED (type)
1059 || (vr0.nonnegative_p () && vr3.nonnegative_p ())
1060 || (vr0.nonpositive_p () && vr3.nonpositive_p ())))
1061 (plus (op @0 @2) { wide_int_to_tree (type, plus_op1 (c)); })
1062 (if (!vr0.undefined_p () && TYPE_UNSIGNED (type) && c.sign_mask () < 0
1064 /* unsigned "X-(-C)" doesn't underflow. */
1065 && wi::geu_p (vr0.lower_bound (), -c))
1066 (plus (op @0 @2) { wide_int_to_tree (type, -plus_op1 (-c)); })))))))
1071 /* (nop_outer_cast)-(inner_cast)var -> -(outer_cast)(var)
1072 if var is smaller in precision.
1073 This is always safe for both doing the negative in signed or unsigned
1074 as the value for undefined will not show up.
1075 Note the outer cast cannot be a boolean type as the only valid values
1076 are 0,-1/1 (depending on the signedness of the boolean) and the negative
1077 is there to get the correct value. */
1079 (convert (negate:s@1 (convert:s @0)))
1080 (if (INTEGRAL_TYPE_P (type)
1081 && tree_nop_conversion_p (type, TREE_TYPE (@1))
1082 && TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (@0))
1083 && TREE_CODE (type) != BOOLEAN_TYPE)
1084 (negate (convert @0))))
1086 (for op (negate abs)
1087 /* Simplify cos(-x) and cos(|x|) -> cos(x). Similarly for cosh. */
1088 (for coss (COS COSH)
1092 /* Simplify pow(-x, y) and pow(|x|,y) -> pow(x,y) if y is an even integer. */
1095 (pows (op @0) REAL_CST@1)
1096 (with { HOST_WIDE_INT n; }
1097 (if (real_isinteger (&TREE_REAL_CST (@1), &n) && (n & 1) == 0)
1099 /* Likewise for powi. */
1102 (pows (op @0) INTEGER_CST@1)
1103 (if ((wi::to_wide (@1) & 1) == 0)
1105 /* Strip negate and abs from both operands of hypot. */
1113 /* copysign(-x, y) and copysign(abs(x), y) -> copysign(x, y). */
1114 (for copysigns (COPYSIGN_ALL)
1116 (copysigns (op @0) @1)
1117 (copysigns @0 @1))))
1119 /* abs(x)*abs(x) -> x*x. Should be valid for all types. */
1121 (mult (abs@1 @0) @1)
1124 /* Convert absu(x)*absu(x) -> x*x. */
1126 (mult (absu@1 @0) @1)
1127 (mult (convert@2 @0) @2))
1129 /* cos(copysign(x, y)) -> cos(x). Similarly for cosh. */
1130 (for coss (COS COSH)
1131 (for copysigns (COPYSIGN)
1133 (coss (copysigns @0 @1))
1136 /* pow(copysign(x, y), z) -> pow(x, z) if z is an even integer. */
1138 (for copysigns (COPYSIGN)
1140 (pows (copysigns @0 @2) REAL_CST@1)
1141 (with { HOST_WIDE_INT n; }
1142 (if (real_isinteger (&TREE_REAL_CST (@1), &n) && (n & 1) == 0)
1144 /* Likewise for powi. */
1146 (for copysigns (COPYSIGN)
1148 (pows (copysigns @0 @2) INTEGER_CST@1)
1149 (if ((wi::to_wide (@1) & 1) == 0)
1153 (for copysigns (COPYSIGN)
1154 /* hypot(copysign(x, y), z) -> hypot(x, z). */
1156 (hypots (copysigns @0 @1) @2)
1158 /* hypot(x, copysign(y, z)) -> hypot(x, y). */
1160 (hypots @0 (copysigns @1 @2))
1163 /* copysign(x, CST) -> abs (x). If the target does not
1164 support the copysign optab then canonicalize
1165 copysign(x, -CST) -> fneg (abs (x)). */
1166 (for copysigns (COPYSIGN_ALL)
1168 (copysigns @0 REAL_CST@1)
1169 (if (!REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
1172 (if (!direct_internal_fn_supported_p (IFN_COPYSIGN, type,
1179 /* Transform fneg (fabs (X)) -> copysign (X, -1) as the canonical
1180 representation if the target supports the copysign optab. */
1183 (if (direct_internal_fn_supported_p (IFN_COPYSIGN, type,
1185 (IFN_COPYSIGN @0 { build_minus_one_cst (type); })))
1187 /* copysign(copysign(x, y), z) -> copysign(x, z). */
1188 (for copysigns (COPYSIGN_ALL)
1190 (copysigns (copysigns @0 @1) @2)
1193 /* copysign(x,y)*copysign(x,y) -> x*x. */
1194 (for copysigns (COPYSIGN_ALL)
1196 (mult (copysigns@2 @0 @1) @2)
1199 /* ccos(-x) -> ccos(x). Similarly for ccosh. */
1200 (for ccoss (CCOS CCOSH)
1205 /* cabs(-x) and cos(conj(x)) -> cabs(x). */
1206 (for ops (conj negate)
1212 /* Fold (a * (1 << b)) into (a << b) */
1214 (mult:c @0 (convert? (lshift integer_onep@1 @2)))
1215 (if (! FLOAT_TYPE_P (type)
1216 && tree_nop_conversion_p (type, TREE_TYPE (@1)))
1219 /* Shifts by precision or greater result in zero. */
1220 (for shift (lshift rshift)
1222 (shift @0 uniform_integer_cst_p@1)
1223 (if ((GIMPLE || !sanitize_flags_p (SANITIZE_SHIFT_EXPONENT))
1224 /* Leave arithmetic right shifts of possibly negative values alone. */
1225 && (TYPE_UNSIGNED (type)
1226 || shift == LSHIFT_EXPR
1227 || tree_expr_nonnegative_p (@0))
1228 /* Use a signed compare to leave negative shift counts alone. */
1229 && wi::ges_p (wi::to_wide (uniform_integer_cst_p (@1)),
1230 element_precision (type)))
1231 { build_zero_cst (type); })))
1233 /* Shifts by constants distribute over several binary operations,
1234 hence (X << C) + (Y << C) can be simplified to (X + Y) << C. */
1235 (for op (plus minus)
1237 (op (lshift:s @0 @1) (lshift:s @2 @1))
1238 (if (INTEGRAL_TYPE_P (type)
1239 && TYPE_OVERFLOW_WRAPS (type)
1240 && !TYPE_SATURATING (type))
1241 (lshift (op @0 @2) @1))))
1243 (for op (bit_and bit_ior bit_xor)
1245 (op (lshift:s @0 @1) (lshift:s @2 @1))
1246 (if (INTEGRAL_TYPE_P (type))
1247 (lshift (op @0 @2) @1)))
1249 (op (rshift:s @0 @1) (rshift:s @2 @1))
1250 (if (INTEGRAL_TYPE_P (type))
1251 (rshift (op @0 @2) @1))))
1253 /* Fold (1 << (C - x)) where C = precision(type) - 1
1254 into ((1 << C) >> x). */
1256 (lshift integer_onep@0 (minus@1 INTEGER_CST@2 @3))
1257 (if (INTEGRAL_TYPE_P (type)
1258 && wi::eq_p (wi::to_wide (@2), TYPE_PRECISION (type) - 1)
1260 (if (TYPE_UNSIGNED (type))
1261 (rshift (lshift @0 @2) @3)
1263 { tree utype = unsigned_type_for (type); }
1264 (convert (rshift (lshift (convert:utype @0) @2) @3))))))
1266 /* Fold ((type)(a<0)) << SIGNBITOFA into ((type)a) & signbit. */
1268 (lshift (convert (lt @0 integer_zerop@1)) INTEGER_CST@2)
1269 (if (TYPE_SIGN (TREE_TYPE (@0)) == SIGNED
1270 && wi::eq_p (wi::to_wide (@2), TYPE_PRECISION (TREE_TYPE (@0)) - 1))
1271 (with { wide_int wone = wi::one (TYPE_PRECISION (type)); }
1272 (bit_and (convert @0)
1273 { wide_int_to_tree (type,
1274 wi::lshift (wone, wi::to_wide (@2))); }))))
1276 /* Fold (-x >> C) into -(x > 0) where C = precision(type) - 1. */
1277 (for cst (INTEGER_CST VECTOR_CST)
1279 (rshift (negate:s @0) cst@1)
1280 (if (!TYPE_UNSIGNED (type)
1281 && TYPE_OVERFLOW_UNDEFINED (type))
1282 (with { tree stype = TREE_TYPE (@1);
1283 tree bt = truth_type_for (type);
1284 tree zeros = build_zero_cst (type);
1285 tree cst = NULL_TREE; }
1287 /* Handle scalar case. */
1288 (if (INTEGRAL_TYPE_P (type)
1289 /* If we apply the rule to the scalar type before vectorization
1290 we will enforce the result of the comparison being a bool
1291 which will require an extra AND on the result that will be
1292 indistinguishable from when the user did actually want 0
1293 or 1 as the result so it can't be removed. */
1294 && canonicalize_math_after_vectorization_p ()
1295 && wi::eq_p (wi::to_wide (@1), TYPE_PRECISION (type) - 1))
1296 (negate (convert (gt @0 { zeros; }))))
1297 /* Handle vector case. */
1298 (if (VECTOR_INTEGER_TYPE_P (type)
1299 /* First check whether the target has the same mode for vector
1300 comparison results as it's operands do. */
1301 && TYPE_MODE (bt) == TYPE_MODE (type)
1302 /* Then check to see if the target is able to expand the comparison
1303 with the given type later on, otherwise we may ICE. */
1304 && expand_vec_cmp_expr_p (type, bt, GT_EXPR)
1305 && (cst = uniform_integer_cst_p (@1)) != NULL
1306 && wi::eq_p (wi::to_wide (cst), element_precision (type) - 1))
1307 (view_convert (gt:bt @0 { zeros; }))))))))
1309 /* Fold (C1/X)*C2 into (C1*C2)/X. */
1311 (mult (rdiv@3 REAL_CST@0 @1) REAL_CST@2)
1312 (if (flag_associative_math
1315 { tree tem = const_binop (MULT_EXPR, type, @0, @2); }
1317 (rdiv { tem; } @1)))))
1319 /* Simplify ~X & X as zero. */
1321 (bit_and (convert? @0) (convert? @1))
1322 (with { bool wascmp; }
1323 (if (types_match (TREE_TYPE (@0), TREE_TYPE (@1))
1324 && bitwise_inverted_equal_p (@0, @1, wascmp))
1325 { wascmp ? constant_boolean_node (false, type) : build_zero_cst (type); })))
1327 /* PR71636: Transform x & ((1U << b) - 1) -> x & ~(~0U << b); */
1329 (bit_and:c @0 (plus:s (lshift:s integer_onep @1) integer_minus_onep))
1330 (if (TYPE_UNSIGNED (type))
1331 (bit_and @0 (bit_not (lshift { build_all_ones_cst (type); } @1)))))
1333 (for bitop (bit_and bit_ior)
1335 /* PR35691: Transform
1336 (x == 0 & y == 0) -> (x | typeof(x)(y)) == 0.
1337 (x != 0 | y != 0) -> (x | typeof(x)(y)) != 0. */
1339 (bitop (cmp @0 integer_zerop@2) (cmp @1 integer_zerop))
1340 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1341 && INTEGRAL_TYPE_P (TREE_TYPE (@1))
1342 && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
1343 (cmp (bit_ior @0 (convert @1)) @2)))
1345 (x == -1 & y == -1) -> (x & typeof(x)(y)) == -1.
1346 (x != -1 | y != -1) -> (x & typeof(x)(y)) != -1. */
1348 (bitop (cmp @0 integer_all_onesp@2) (cmp @1 integer_all_onesp))
1349 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1350 && INTEGRAL_TYPE_P (TREE_TYPE (@1))
1351 && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
1352 (cmp (bit_and @0 (convert @1)) @2))))
1354 /* Fold (A & ~B) - (A & B) into (A ^ B) - B. */
1356 (minus (bit_and:cs @0 (bit_not @1)) (bit_and:cs @0 @1))
1357 (minus (bit_xor @0 @1) @1))
1359 (minus (bit_and:s @0 INTEGER_CST@2) (bit_and:s @0 INTEGER_CST@1))
1360 (if (~wi::to_wide (@2) == wi::to_wide (@1))
1361 (minus (bit_xor @0 @1) @1)))
1363 /* Fold (A & B) - (A & ~B) into B - (A ^ B). */
1365 (minus (bit_and:cs @0 @1) (bit_and:cs @0 (bit_not @1)))
1366 (minus @1 (bit_xor @0 @1)))
1368 /* Simplify (X & ~Y) |^+ (~X & Y) -> X ^ Y. */
1369 (for op (bit_ior bit_xor plus)
1371 (op (bit_and:c @0 @2) (bit_and:c @3 @1))
1372 (with { bool wascmp0, wascmp1; }
1373 (if (bitwise_inverted_equal_p (@2, @1, wascmp0)
1374 && bitwise_inverted_equal_p (@0, @3, wascmp1)
1375 && ((!wascmp0 && !wascmp1)
1376 || element_precision (type) == 1))
1379 /* PR53979: Transform ((a ^ b) | a) -> (a | b) */
1381 (bit_ior:c (bit_xor:c @0 @1) @0)
1384 /* (a & ~b) | (a ^ b) --> a ^ b */
1386 (bit_ior:c (bit_and:c @0 (bit_not @1)) (bit_xor:c@2 @0 @1))
1389 /* (a & ~b) ^ ~a --> ~(a & b) */
1391 (bit_xor:c (bit_and:cs @0 (bit_not @1)) (bit_not @0))
1392 (bit_not (bit_and @0 @1)))
1394 /* (~a & b) ^ a --> (a | b) */
1396 (bit_xor:c (bit_and:cs (bit_not @0) @1) @0)
1399 /* (a | b) & ~(a ^ b) --> a & b */
1401 (bit_and:c (bit_ior @0 @1) (bit_not (bit_xor:c @0 @1)))
1404 /* (a | b) & (a == b) --> a & b (boolean version of the above). */
1406 (bit_and:c (bit_ior @0 @1) (nop_convert? (eq:c @0 @1)))
1407 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1408 && TYPE_PRECISION (TREE_TYPE (@0)) == 1)
1411 /* a | ~(a ^ b) --> a | ~b */
1413 (bit_ior:c @0 (bit_not:s (bit_xor:c @0 @1)))
1414 (bit_ior @0 (bit_not @1)))
1416 /* a | (a == b) --> a | (b^1) (boolean version of the above). */
1418 (bit_ior:c @0 (nop_convert? (eq:c @0 @1)))
1419 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1420 && TYPE_PRECISION (TREE_TYPE (@0)) == 1)
1421 (bit_ior @0 (bit_xor @1 { build_one_cst (type); }))))
1423 /* a | ((~a) ^ b) --> a | (~b) (alt version of the above 2) */
1425 (bit_ior:c @0 (bit_xor:cs @1 @2))
1426 (with { bool wascmp; }
1427 (if (bitwise_inverted_equal_p (@0, @1, wascmp)
1428 && (!wascmp || element_precision (type) == 1))
1429 (bit_ior @0 (bit_not @2)))))
1431 /* a & ~(a ^ b) --> a & b */
1433 (bit_and:c @0 (bit_not (bit_xor:c @0 @1)))
1436 /* a & (a == b) --> a & b (boolean version of the above). */
1438 (bit_and:c @0 (nop_convert? (eq:c @0 @1)))
1439 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1440 && TYPE_PRECISION (TREE_TYPE (@0)) == 1)
1443 /* a & ((~a) ^ b) --> a & b (alt version of the above 2) */
1445 (bit_and:c @0 (bit_xor:c @1 @2))
1446 (with { bool wascmp; }
1447 (if (bitwise_inverted_equal_p (@0, @1, wascmp)
1448 && (!wascmp || element_precision (type) == 1))
1451 /* (a | b) | (a &^ b) --> a | b */
1452 (for op (bit_and bit_xor)
1454 (bit_ior:c (bit_ior@2 @0 @1) (op:c @0 @1))
1457 /* (a & b) | ~(a ^ b) --> ~(a ^ b) */
1459 (bit_ior:c (bit_and:c @0 @1) (bit_not@2 (bit_xor @0 @1)))
1462 /* (a & b) | (a == b) --> a == b */
1464 (bit_ior:c (bit_and:c @0 @1) (nop_convert?@2 (eq @0 @1)))
1465 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1466 && TYPE_PRECISION (TREE_TYPE (@0)) == 1)
1469 /* ~(~a & b) --> a | ~b */
1471 (bit_not (bit_and:cs (bit_not @0) @1))
1472 (bit_ior @0 (bit_not @1)))
1474 /* ~(~a | b) --> a & ~b */
1476 (bit_not (bit_ior:cs (bit_not @0) @1))
1477 (bit_and @0 (bit_not @1)))
1479 /* (a ^ b) & ((b ^ c) ^ a) --> (a ^ b) & ~c */
1481 (bit_and:c (bit_xor:c@3 @0 @1) (bit_xor:cs (bit_xor:cs @1 @2) @0))
1482 (bit_and @3 (bit_not @2)))
1484 /* (a ^ b) | ((b ^ c) ^ a) --> (a ^ b) | c */
1486 (bit_ior:c (bit_xor:c@3 @0 @1) (bit_xor:c (bit_xor:c @1 @2) @0))
1489 /* (~X | C) ^ D -> (X | C) ^ (~D ^ C) if (~D ^ C) can be simplified. */
1491 (bit_xor:c (bit_ior:cs (bit_not:s @0) @1) @2)
1492 (bit_xor (bit_ior @0 @1) (bit_xor! (bit_not! @2) @1)))
1494 /* (~X & C) ^ D -> (X & C) ^ (D ^ C) if (D ^ C) can be simplified. */
1496 (bit_xor:c (bit_and:cs (bit_not:s @0) @1) @2)
1497 (bit_xor (bit_and @0 @1) (bit_xor! @2 @1)))
1499 /* Simplify (~X & Y) to X ^ Y if we know that (X & ~Y) is 0. */
1501 (bit_and (bit_not SSA_NAME@0) INTEGER_CST@1)
1502 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1503 && wi::bit_and_not (get_nonzero_bits (@0), wi::to_wide (@1)) == 0)
1506 /* For constants M and N, if M == (1LL << cst) - 1 && (N & M) == M,
1507 ((A & N) + B) & M -> (A + B) & M
1508 Similarly if (N & M) == 0,
1509 ((A | N) + B) & M -> (A + B) & M
1510 and for - instead of + (or unary - instead of +)
1511 and/or ^ instead of |.
1512 If B is constant and (B & M) == 0, fold into A & M. */
1513 (for op (plus minus)
1514 (for bitop (bit_and bit_ior bit_xor)
1516 (bit_and (op:s (bitop:s@0 @3 INTEGER_CST@4) @1) INTEGER_CST@2)
1519 tree utype = fold_bit_and_mask (TREE_TYPE (@0), @2, op, @0, bitop,
1520 @3, @4, @1, ERROR_MARK, NULL_TREE,
1523 (convert (bit_and (op (convert:utype { pmop[0]; })
1524 (convert:utype { pmop[1]; }))
1525 (convert:utype @2))))))
1527 (bit_and (op:s @0 (bitop:s@1 @3 INTEGER_CST@4)) INTEGER_CST@2)
1530 tree utype = fold_bit_and_mask (TREE_TYPE (@0), @2, op, @0, ERROR_MARK,
1531 NULL_TREE, NULL_TREE, @1, bitop, @3,
1534 (convert (bit_and (op (convert:utype { pmop[0]; })
1535 (convert:utype { pmop[1]; }))
1536 (convert:utype @2)))))))
1538 (bit_and (op:s @0 @1) INTEGER_CST@2)
1541 tree utype = fold_bit_and_mask (TREE_TYPE (@0), @2, op, @0, ERROR_MARK,
1542 NULL_TREE, NULL_TREE, @1, ERROR_MARK,
1543 NULL_TREE, NULL_TREE, pmop); }
1545 (convert (bit_and (op (convert:utype { pmop[0]; })
1546 (convert:utype { pmop[1]; }))
1547 (convert:utype @2)))))))
1548 (for bitop (bit_and bit_ior bit_xor)
1550 (bit_and (negate:s (bitop:s@0 @2 INTEGER_CST@3)) INTEGER_CST@1)
1553 tree utype = fold_bit_and_mask (TREE_TYPE (@0), @1, NEGATE_EXPR, @0,
1554 bitop, @2, @3, NULL_TREE, ERROR_MARK,
1555 NULL_TREE, NULL_TREE, pmop); }
1557 (convert (bit_and (negate (convert:utype { pmop[0]; }))
1558 (convert:utype @1)))))))
1560 /* X % Y is smaller than Y. */
1563 (cmp:c (trunc_mod @0 @1) @1)
1564 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
1565 { constant_boolean_node (cmp == LT_EXPR, type); })))
1569 (bit_ior @0 integer_all_onesp@1)
1574 (bit_ior @0 integer_zerop)
1579 (bit_and @0 integer_zerop@1)
1584 (for op (bit_ior bit_xor)
1586 (op (convert? @0) (convert? @1))
1587 (with { bool wascmp; }
1588 (if (types_match (TREE_TYPE (@0), TREE_TYPE (@1))
1589 && bitwise_inverted_equal_p (@0, @1, wascmp))
1592 ? constant_boolean_node (true, type)
1593 : build_all_ones_cst (TREE_TYPE (@0)); })))))
1598 { build_zero_cst (type); })
1600 /* Canonicalize X ^ ~0 to ~X. */
1602 (bit_xor @0 integer_all_onesp@1)
1607 (bit_and @0 integer_all_onesp)
1610 /* x & x -> x, x | x -> x */
1611 (for bitop (bit_and bit_ior)
1616 /* x & C -> x if we know that x & ~C == 0. */
1619 (bit_and SSA_NAME@0 INTEGER_CST@1)
1620 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1621 && wi::bit_and_not (get_nonzero_bits (@0), wi::to_wide (@1)) == 0)
1624 /* `a & (x | CST)` -> a if we know that (a & ~CST) == 0 */
1626 (bit_and:c SSA_NAME@0 (bit_ior @1 INTEGER_CST@2))
1627 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1628 && wi::bit_and_not (get_nonzero_bits (@0), wi::to_wide (@2)) == 0)
1631 /* x | C -> C if we know that x & ~C == 0. */
1633 (bit_ior SSA_NAME@0 INTEGER_CST@1)
1634 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1635 && wi::bit_and_not (get_nonzero_bits (@0), wi::to_wide (@1)) == 0)
1639 /* ~(~X - Y) -> X + Y and ~(~X + Y) -> X - Y. */
1641 (bit_not (minus (bit_not @0) @1))
1644 (bit_not (plus:c (bit_not @0) @1))
1646 /* (~X - ~Y) -> Y - X. */
1648 (minus (bit_not @0) (bit_not @1))
1649 (if (!TYPE_OVERFLOW_SANITIZED (type))
1650 (with { tree utype = unsigned_type_for (type); }
1651 (convert (minus (convert:utype @1) (convert:utype @0))))))
1653 /* ~(X - Y) -> ~X + Y. */
1655 (bit_not (minus:s @0 @1))
1656 (plus (bit_not @0) @1))
1658 (bit_not (plus:s @0 INTEGER_CST@1))
1659 (if ((INTEGRAL_TYPE_P (type)
1660 && TYPE_UNSIGNED (type))
1661 || (!TYPE_OVERFLOW_SANITIZED (type)
1662 && may_negate_without_overflow_p (@1)))
1663 (plus (bit_not @0) { const_unop (NEGATE_EXPR, type, @1); })))
1666 /* ~X + Y -> (Y - X) - 1. */
1668 (plus:c (bit_not @0) @1)
1669 (if (ANY_INTEGRAL_TYPE_P (type)
1670 && TYPE_OVERFLOW_WRAPS (type)
1671 /* -1 - X is folded to ~X, so we'd recurse endlessly. */
1672 && !integer_all_onesp (@1))
1673 (plus (minus @1 @0) { build_minus_one_cst (type); })
1674 (if (INTEGRAL_TYPE_P (type)
1675 && TREE_CODE (@1) == INTEGER_CST
1676 && wi::to_wide (@1) != wi::min_value (TYPE_PRECISION (type),
1678 (minus (plus @1 { build_minus_one_cst (type); }) @0))))
1681 /* ~(X >> Y) -> ~X >> Y if ~X can be simplified. */
1683 (bit_not (rshift:s @0 @1))
1684 (if (!TYPE_UNSIGNED (TREE_TYPE (@0)))
1685 (rshift (bit_not! @0) @1)
1686 /* For logical right shifts, this is possible only if @0 doesn't
1687 have MSB set and the logical right shift is changed into
1688 arithmetic shift. */
1689 (if (INTEGRAL_TYPE_P (type)
1690 && !wi::neg_p (tree_nonzero_bits (@0)))
1691 (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
1692 (convert (rshift (bit_not! (convert:stype @0)) @1))))))
1694 /* x + (x & 1) -> (x + 1) & ~1 */
1696 (plus:c @0 (bit_and:s @0 integer_onep@1))
1697 (bit_and (plus @0 @1) (bit_not @1)))
1699 /* x & ~(x & y) -> x & ~y */
1700 /* x | ~(x | y) -> x | ~y */
1701 (for bitop (bit_and bit_ior)
1703 (bitop:c @0 (bit_not (bitop:cs @0 @1)))
1704 (bitop @0 (bit_not @1))))
1706 /* (~x & y) | ~(x | y) -> ~x */
1708 (bit_ior:c (bit_and:c (bit_not@2 @0) @1) (bit_not (bit_ior:c @0 @1)))
1711 /* (x | y) ^ (x | ~y) -> ~x */
1713 (bit_xor:c (bit_ior:c @0 @1) (bit_ior:c @0 (bit_not @1)))
1716 /* (x & y) | ~(x | y) -> ~(x ^ y) */
1718 (bit_ior:c (bit_and:s @0 @1) (bit_not:s (bit_ior:s @0 @1)))
1719 (bit_not (bit_xor @0 @1)))
1721 /* (~x | y) ^ (x ^ y) -> x | ~y */
1723 (bit_xor:c (bit_ior:cs (bit_not @0) @1) (bit_xor:s @0 @1))
1724 (bit_ior @0 (bit_not @1)))
1726 /* (x ^ y) | ~(x | y) -> ~(x & y) */
1728 (bit_ior:c (bit_xor:s @0 @1) (bit_not:s (bit_ior:s @0 @1)))
1729 (bit_not (bit_and @0 @1)))
1731 /* (x & y) ^ (x | y) -> x ^ y */
1733 (bit_xor:c (bit_and @0 @1) (bit_ior @0 @1))
1736 /* (x ^ y) ^ (x | y) -> x & y */
1738 (bit_xor:c (bit_xor @0 @1) (bit_ior @0 @1))
1741 /* (x & y) + (x ^ y) -> x | y */
1742 /* (x & y) | (x ^ y) -> x | y */
1743 /* (x & y) ^ (x ^ y) -> x | y */
1744 (for op (plus bit_ior bit_xor)
1746 (op:c (bit_and @0 @1) (bit_xor @0 @1))
1749 /* (x & y) + (x | y) -> x + y */
1751 (plus:c (bit_and @0 @1) (bit_ior @0 @1))
1754 /* (x + y) - (x | y) -> x & y */
1756 (minus (plus @0 @1) (bit_ior @0 @1))
1757 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1758 && !TYPE_SATURATING (type))
1761 /* (x + y) - (x & y) -> x | y */
1763 (minus (plus @0 @1) (bit_and @0 @1))
1764 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1765 && !TYPE_SATURATING (type))
1768 /* (x | y) - y -> (x & ~y) */
1770 (minus (bit_ior:cs @0 @1) @1)
1771 (bit_and @0 (bit_not @1)))
1773 /* (x | y) - (x ^ y) -> x & y */
1775 (minus (bit_ior @0 @1) (bit_xor @0 @1))
1778 /* (x | y) - (x & y) -> x ^ y */
1780 (minus (bit_ior @0 @1) (bit_and @0 @1))
1783 /* (x | y) & ~(x & y) -> x ^ y */
1785 (bit_and:c (bit_ior @0 @1) (bit_not (bit_and @0 @1)))
1788 /* (x | y) & (~x ^ y) -> x & y */
1790 (bit_and:c (bit_ior:c @0 @1) (bit_xor:c @1 @2))
1791 (with { bool wascmp; }
1792 (if (bitwise_inverted_equal_p (@0, @2, wascmp)
1793 && (!wascmp || element_precision (type) == 1))
1796 /* (~x | y) & (x | ~y) -> ~(x ^ y) */
1798 (bit_and (bit_ior:cs (bit_not @0) @1) (bit_ior:cs @0 (bit_not @1)))
1799 (bit_not (bit_xor @0 @1)))
1801 /* (~x | y) ^ (x | ~y) -> x ^ y */
1803 (bit_xor (bit_ior:c (bit_not @0) @1) (bit_ior:c @0 (bit_not @1)))
1806 /* ((x & y) - (x | y)) - 1 -> ~(x ^ y) */
1808 (plus (nop_convert1? (minus@2 (nop_convert2? (bit_and:c @0 @1))
1809 (nop_convert2? (bit_ior @0 @1))))
1811 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1812 && !TYPE_SATURATING (type) && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2))
1813 && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@2))
1814 && !TYPE_SATURATING (TREE_TYPE (@2)))
1815 (bit_not (convert (bit_xor @0 @1)))))
1817 (minus (nop_convert1? (plus@2 (nop_convert2? (bit_and:c @0 @1))
1819 (nop_convert3? (bit_ior @0 @1)))
1820 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1821 && !TYPE_SATURATING (type) && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2))
1822 && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@2))
1823 && !TYPE_SATURATING (TREE_TYPE (@2)))
1824 (bit_not (convert (bit_xor @0 @1)))))
1826 (minus (nop_convert1? (bit_and @0 @1))
1827 (nop_convert2? (plus@2 (nop_convert3? (bit_ior:c @0 @1))
1829 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1830 && !TYPE_SATURATING (type) && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2))
1831 && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@2))
1832 && !TYPE_SATURATING (TREE_TYPE (@2)))
1833 (bit_not (convert (bit_xor @0 @1)))))
1835 /* ~x & ~y -> ~(x | y)
1836 ~x | ~y -> ~(x & y) */
1837 (for op (bit_and bit_ior)
1838 rop (bit_ior bit_and)
1840 (op (convert1? (bit_not @0)) (convert2? (bit_not @1)))
1841 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1842 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
1843 (bit_not (rop (convert @0) (convert @1))))))
1845 /* If we are XORing or adding two BIT_AND_EXPR's, both of which are and'ing
1846 with a constant, and the two constants have no bits in common,
1847 we should treat this as a BIT_IOR_EXPR since this may produce more
1849 (for op (bit_xor plus)
1851 (op (convert1? (bit_and@4 @0 INTEGER_CST@1))
1852 (convert2? (bit_and@5 @2 INTEGER_CST@3)))
1853 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
1854 && tree_nop_conversion_p (type, TREE_TYPE (@2))
1855 && (wi::to_wide (@1) & wi::to_wide (@3)) == 0)
1856 (bit_ior (convert @4) (convert @5)))))
1858 /* (X | Y) ^ X -> Y & ~ X*/
1860 (bit_xor:c (convert1? (bit_ior:c @@0 @1)) (convert2? @0))
1861 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1862 (convert (bit_and @1 (bit_not @0)))))
1864 /* (~X | Y) ^ X -> ~(X & Y). */
1866 (bit_xor:c (nop_convert1? (bit_ior:c (nop_convert2? (bit_not @0)) @1)) @2)
1867 (if (bitwise_equal_p (@0, @2))
1868 (convert (bit_not (bit_and @0 (convert @1))))))
1870 /* Convert ~X ^ ~Y to X ^ Y. */
1872 (bit_xor (convert1? (bit_not @0)) (convert2? (bit_not @1)))
1873 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1874 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
1875 (bit_xor (convert @0) (convert @1))))
1877 /* Convert ~X ^ C to X ^ ~C. */
1879 (bit_xor (convert? (bit_not @0)) INTEGER_CST@1)
1880 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1881 (bit_xor (convert @0) (bit_not @1))))
1883 /* Fold (X & Y) ^ Y and (X ^ Y) & Y as ~X & Y. */
1884 (for opo (bit_and bit_xor)
1885 opi (bit_xor bit_and)
1887 (opo:c (opi:cs @0 @1) @1)
1888 (bit_and (bit_not @0) @1)))
1890 /* Given a bit-wise operation CODE applied to ARG0 and ARG1, see if both
1891 operands are another bit-wise operation with a common input. If so,
1892 distribute the bit operations to save an operation and possibly two if
1893 constants are involved. For example, convert
1894 (A | B) & (A | C) into A | (B & C)
1895 Further simplification will occur if B and C are constants. */
1896 (for op (bit_and bit_ior bit_xor)
1897 rop (bit_ior bit_and bit_and)
1899 (op (convert? (rop:c @@0 @1)) (convert? (rop:c @0 @2)))
1900 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1901 && tree_nop_conversion_p (type, TREE_TYPE (@2)))
1902 (rop (convert @0) (op (convert @1) (convert @2))))))
1904 /* Some simple reassociation for bit operations, also handled in reassoc. */
1905 /* (X & Y) & Y -> X & Y
1906 (X | Y) | Y -> X | Y */
1907 (for op (bit_and bit_ior)
1909 (op:c (convert1?@2 (op:c @0 @@1)) (convert2? @1))
1911 /* (X ^ Y) ^ Y -> X */
1913 (bit_xor:c (convert1? (bit_xor:c @0 @@1)) (convert2? @1))
1916 /* (X & ~Y) & Y -> 0 */
1918 (bit_and:c (bit_and @0 @1) @2)
1919 (with { bool wascmp; }
1920 (if (bitwise_inverted_equal_p (@0, @2, wascmp)
1921 || bitwise_inverted_equal_p (@1, @2, wascmp))
1922 { wascmp ? constant_boolean_node (false, type) : build_zero_cst (type); })))
1923 /* (X | ~Y) | Y -> -1 */
1925 (bit_ior:c (bit_ior @0 @1) @2)
1926 (with { bool wascmp; }
1927 (if ((bitwise_inverted_equal_p (@0, @2, wascmp)
1928 || bitwise_inverted_equal_p (@1, @2, wascmp))
1929 && (!wascmp || element_precision (type) == 1))
1930 { build_all_ones_cst (TREE_TYPE (@0)); })))
1932 /* (X & Y) & (X & Z) -> (X & Y) & Z
1933 (X | Y) | (X | Z) -> (X | Y) | Z */
1934 (for op (bit_and bit_ior)
1936 (op (convert1?@3 (op:c@4 @0 @1)) (convert2?@5 (op:c@6 @0 @2)))
1937 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1938 && tree_nop_conversion_p (type, TREE_TYPE (@2)))
1939 (if (single_use (@5) && single_use (@6))
1940 (op @3 (convert @2))
1941 (if (single_use (@3) && single_use (@4))
1942 (op (convert @1) @5))))))
1943 /* (X ^ Y) ^ (X ^ Z) -> Y ^ Z */
1945 (bit_xor (convert1? (bit_xor:c @0 @1)) (convert2? (bit_xor:c @0 @2)))
1946 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1947 && tree_nop_conversion_p (type, TREE_TYPE (@2)))
1948 (bit_xor (convert @1) (convert @2))))
1950 /* Convert abs (abs (X)) into abs (X).
1951 also absu (absu (X)) into absu (X). */
1957 (absu (convert@2 (absu@1 @0)))
1958 (if (tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@1)))
1961 /* Convert abs[u] (-X) -> abs[u] (X). */
1970 /* Convert abs[u] (X) where X is nonnegative -> (X). */
1972 (abs tree_expr_nonnegative_p@0)
1976 (absu tree_expr_nonnegative_p@0)
1979 /* Simplify (-(X < 0) | 1) * X into abs (X) or absu(X). */
1981 (mult:c (nop_convert1?
1982 (bit_ior (nop_convert2? (negate (convert? (lt @0 integer_zerop))))
1985 (if (INTEGRAL_TYPE_P (type)
1986 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
1987 && !TYPE_UNSIGNED (TREE_TYPE (@0)))
1988 (if (TYPE_UNSIGNED (type))
1995 /* A few cases of fold-const.cc negate_expr_p predicate. */
1996 (match negate_expr_p
1998 (if ((INTEGRAL_TYPE_P (type)
1999 && TYPE_UNSIGNED (type))
2000 || (!TYPE_OVERFLOW_SANITIZED (type)
2001 && may_negate_without_overflow_p (t)))))
2002 (match negate_expr_p
2004 (match negate_expr_p
2006 (if (!TYPE_OVERFLOW_SANITIZED (type))))
2007 (match negate_expr_p
2009 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (t)))))
2010 /* VECTOR_CST handling of non-wrapping types would recurse in unsupported
2012 (match negate_expr_p
2014 (if (FLOAT_TYPE_P (TREE_TYPE (type)) || TYPE_OVERFLOW_WRAPS (type))))
2015 (match negate_expr_p
2017 (if ((ANY_INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_WRAPS (type))
2018 || (FLOAT_TYPE_P (type)
2019 && !HONOR_SIGN_DEPENDENT_ROUNDING (type)
2020 && !HONOR_SIGNED_ZEROS (type)))))
2022 /* (-A) * (-B) -> A * B */
2024 (mult:c (convert1? (negate @0)) (convert2? negate_expr_p@1))
2025 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
2026 && tree_nop_conversion_p (type, TREE_TYPE (@1)))
2027 (mult (convert @0) (convert (negate @1)))))
2029 /* -(A + B) -> (-B) - A. */
2031 (negate (plus:c @0 negate_expr_p@1))
2032 (if (!HONOR_SIGN_DEPENDENT_ROUNDING (type)
2033 && !HONOR_SIGNED_ZEROS (type))
2034 (minus (negate @1) @0)))
2036 /* -(A - B) -> B - A. */
2038 (negate (minus @0 @1))
2039 (if ((ANY_INTEGRAL_TYPE_P (type) && !TYPE_OVERFLOW_SANITIZED (type))
2040 || (FLOAT_TYPE_P (type)
2041 && !HONOR_SIGN_DEPENDENT_ROUNDING (type)
2042 && !HONOR_SIGNED_ZEROS (type)))
2045 (negate (pointer_diff @0 @1))
2046 (if (TYPE_OVERFLOW_UNDEFINED (type))
2047 (pointer_diff @1 @0)))
2049 /* A - B -> A + (-B) if B is easily negatable. */
2051 (minus @0 negate_expr_p@1)
2052 (if (!FIXED_POINT_TYPE_P (type))
2053 (plus @0 (negate @1))))
2055 /* 1 - a is a ^ 1 if a had a bool range. */
2056 /* This is only enabled for gimple as sometimes
2057 cfun is not set for the function which contains
2058 the SSA_NAME (e.g. while IPA passes are happening,
2059 fold might be called). */
2061 (minus integer_onep@0 SSA_NAME@1)
2062 (if (INTEGRAL_TYPE_P (type)
2063 && ssa_name_has_boolean_range (@1))
2066 /* Other simplifications of negation (c.f. fold_negate_expr_1). */
2068 (negate (mult:c@0 @1 negate_expr_p@2))
2069 (if (! TYPE_UNSIGNED (type)
2070 && ! HONOR_SIGN_DEPENDENT_ROUNDING (type)
2072 (mult @1 (negate @2))))
2075 (negate (rdiv@0 @1 negate_expr_p@2))
2076 (if (! HONOR_SIGN_DEPENDENT_ROUNDING (type)
2078 (rdiv @1 (negate @2))))
2081 (negate (rdiv@0 negate_expr_p@1 @2))
2082 (if (! HONOR_SIGN_DEPENDENT_ROUNDING (type)
2084 (rdiv (negate @1) @2)))
2086 /* Fold -((int)x >> (prec - 1)) into (unsigned)x >> (prec - 1). */
2088 (negate (convert? (rshift @0 INTEGER_CST@1)))
2089 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
2090 && wi::to_wide (@1) == element_precision (type) - 1)
2091 (with { tree stype = TREE_TYPE (@0);
2092 tree ntype = TYPE_UNSIGNED (stype) ? signed_type_for (stype)
2093 : unsigned_type_for (stype); }
2094 (if (VECTOR_TYPE_P (type))
2095 (view_convert (rshift (view_convert:ntype @0) @1))
2096 (convert (rshift (convert:ntype @0) @1))))))
2098 /* Try to fold (type) X op CST -> (type) (X op ((type-x) CST))
2100 For bitwise binary operations apply operand conversions to the
2101 binary operation result instead of to the operands. This allows
2102 to combine successive conversions and bitwise binary operations.
2103 We combine the above two cases by using a conditional convert. */
2104 (for bitop (bit_and bit_ior bit_xor)
2106 (bitop (convert@2 @0) (convert?@3 @1))
2107 (if (((TREE_CODE (@1) == INTEGER_CST
2108 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
2109 && (int_fits_type_p (@1, TREE_TYPE (@0))
2110 || tree_nop_conversion_p (TREE_TYPE (@0), type)))
2111 || types_match (@0, @1))
2112 && !POINTER_TYPE_P (TREE_TYPE (@0))
2113 && !VECTOR_TYPE_P (TREE_TYPE (@0))
2114 && TREE_CODE (TREE_TYPE (@0)) != OFFSET_TYPE
2115 /* ??? This transform conflicts with fold-const.cc doing
2116 Convert (T)(x & c) into (T)x & (T)c, if c is an integer
2117 constants (if x has signed type, the sign bit cannot be set
2118 in c). This folds extension into the BIT_AND_EXPR.
2119 Restrict it to GIMPLE to avoid endless recursions. */
2120 && (bitop != BIT_AND_EXPR || GIMPLE)
2121 && (/* That's a good idea if the conversion widens the operand, thus
2122 after hoisting the conversion the operation will be narrower.
2123 It is also a good if the conversion is a nop as moves the
2124 conversion to one side; allowing for combining of the conversions. */
2125 TYPE_PRECISION (TREE_TYPE (@0)) < TYPE_PRECISION (type)
2126 /* The conversion check for being a nop can only be done at the gimple
2127 level as fold_binary has some re-association code which can conflict
2128 with this if there is a "constant" which is not a full INTEGER_CST. */
2129 || (GIMPLE && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (type))
2130 /* It's also a good idea if the conversion is to a non-integer
2132 || GET_MODE_CLASS (TYPE_MODE (type)) != MODE_INT
2133 /* Or if the precision of TO is not the same as the precision
2135 || !type_has_mode_precision_p (type)
2136 /* In GIMPLE, getting rid of 2 conversions for one new results
2139 && TREE_CODE (@1) != INTEGER_CST
2140 && tree_nop_conversion_p (type, TREE_TYPE (@0))
2142 && single_use (@3))))
2143 (convert (bitop @0 (convert @1)))))
2144 /* In GIMPLE, getting rid of 2 conversions for one new results
2147 (convert (bitop:cs@2 (nop_convert:s @0) @1))
2149 && TREE_CODE (@1) != INTEGER_CST
2150 && tree_nop_conversion_p (type, TREE_TYPE (@2))
2151 && types_match (type, @0)
2152 && !POINTER_TYPE_P (TREE_TYPE (@0))
2153 && TREE_CODE (TREE_TYPE (@0)) != OFFSET_TYPE)
2154 (bitop @0 (convert @1)))))
2156 (for bitop (bit_and bit_ior)
2157 rbitop (bit_ior bit_and)
2158 /* (x | y) & x -> x */
2159 /* (x & y) | x -> x */
2161 (bitop:c (rbitop:c @0 @1) @0)
2163 /* (~x | y) & x -> x & y */
2164 /* (~x & y) | x -> x | y */
2166 (bitop:c (rbitop:c @2 @1) @0)
2167 (with { bool wascmp; }
2168 (if (bitwise_inverted_equal_p (@0, @2, wascmp)
2169 && (!wascmp || element_precision (type) == 1))
2171 /* (x | y) & (x & z) -> (x & z) */
2172 /* (x & y) | (x | z) -> (x | z) */
2174 (bitop:c (rbitop:c @0 @1) (bitop:c@3 @0 @2))
2176 /* (x | c) & ~(y | c) -> x & ~(y | c) */
2177 /* (x & c) | ~(y & c) -> x | ~(y & c) */
2179 (bitop:c (rbitop:c @0 @1) (bit_not@3 (rbitop:c @1 @2)))
2181 /* x & ~(y | x) -> 0 */
2182 /* x | ~(y & x) -> -1 */
2184 (bitop:c @0 (bit_not (rbitop:c @0 @1)))
2185 (if (bitop == BIT_AND_EXPR)
2186 { build_zero_cst (type); }
2187 { build_minus_one_cst (type); })))
2189 /* ((x | y) & z) | x -> (z & y) | x
2190 ((x ^ y) & z) | x -> (z & y) | x */
2191 (for op (bit_ior bit_xor)
2193 (bit_ior:c (nop_convert1?:s
2194 (bit_and:cs (nop_convert2?:s (op:cs @0 @1)) @2)) @3)
2195 (if (bitwise_equal_p (@0, @3))
2196 (convert (bit_ior (bit_and @1 (convert @2)) (convert @0))))))
2198 /* (x | CST1) & CST2 -> (x & CST2) | (CST1 & CST2) */
2200 (bit_and (bit_ior @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
2201 (bit_ior (bit_and @0 @2) (bit_and! @1 @2)))
2203 /* Combine successive equal operations with constants. */
2204 (for bitop (bit_and bit_ior bit_xor)
2206 (bitop (bitop @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
2207 (if (!CONSTANT_CLASS_P (@0))
2208 /* This is the canonical form regardless of whether (bitop @1 @2) can be
2209 folded to a constant. */
2210 (bitop @0 (bitop! @1 @2))
2211 /* In this case we have three constants and (bitop @0 @1) doesn't fold
2212 to a constant. This can happen if @0 or @1 is a POLY_INT_CST and if
2213 the values involved are such that the operation can't be decided at
2214 compile time. Try folding one of @0 or @1 with @2 to see whether
2215 that combination can be decided at compile time.
2217 Keep the existing form if both folds fail, to avoid endless
2219 (with { tree cst1 = const_binop (bitop, type, @0, @2); }
2221 (bitop @1 { cst1; })
2222 (with { tree cst2 = const_binop (bitop, type, @1, @2); }
2224 (bitop @0 { cst2; }))))))))
2226 /* Try simple folding for X op !X, and X op X with the help
2227 of the truth_valued_p and logical_inverted_value predicates. */
2228 (match truth_valued_p
2230 (if (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1)))
2231 (for op (tcc_comparison truth_and truth_andif truth_or truth_orif truth_xor)
2232 (match truth_valued_p
2234 (match truth_valued_p
2237 (match (logical_inverted_value @0)
2239 (match (logical_inverted_value @0)
2240 (bit_not truth_valued_p@0))
2241 (match (logical_inverted_value @0)
2242 (eq @0 integer_zerop))
2243 (match (logical_inverted_value @0)
2244 (ne truth_valued_p@0 integer_truep))
2245 (match (logical_inverted_value @0)
2246 (bit_xor truth_valued_p@0 integer_truep))
2250 (bit_and:c @0 (logical_inverted_value @0))
2251 { build_zero_cst (type); })
2252 /* X | !X and X ^ !X -> 1, , if X is truth-valued. */
2253 (for op (bit_ior bit_xor)
2255 (op:c truth_valued_p@0 (logical_inverted_value @0))
2256 { constant_boolean_node (true, type); }))
2257 /* X ==/!= !X is false/true. */
2260 (op:c truth_valued_p@0 (logical_inverted_value @0))
2261 { constant_boolean_node (op == NE_EXPR ? true : false, type); }))
2265 (bit_not (bit_not @0))
2268 /* zero_one_valued_p will match when a value is known to be either
2269 0 or 1 including constants 0 or 1.
2270 Signed 1-bits includes -1 so they cannot match here. */
2271 (match zero_one_valued_p
2273 (if (INTEGRAL_TYPE_P (type)
2274 && (TYPE_UNSIGNED (type)
2275 || TYPE_PRECISION (type) > 1)
2276 && wi::leu_p (tree_nonzero_bits (@0), 1))))
2277 (match zero_one_valued_p
2279 (if (INTEGRAL_TYPE_P (type)
2280 && (TYPE_UNSIGNED (type)
2281 || TYPE_PRECISION (type) > 1))))
2283 /* (a&1) is always [0,1] too. This is useful again when
2284 the range is not known. */
2285 /* Note this can't be recursive due to VN handling of equivalents,
2286 VN and would cause an infinite recursion. */
2287 (match zero_one_valued_p
2288 (bit_and:c@0 @1 integer_onep)
2289 (if (INTEGRAL_TYPE_P (type))))
2291 /* A conversion from an zero_one_valued_p is still a [0,1].
2292 This is useful when the range of a variable is not known */
2293 /* Note this matches can't be recursive because of the way VN handles
2294 nop conversions being equivalent and then recursive between them. */
2295 (match zero_one_valued_p
2297 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2298 && (TYPE_UNSIGNED (TREE_TYPE (@1))
2299 || TYPE_PRECISION (TREE_TYPE (@1)) > 1)
2300 && INTEGRAL_TYPE_P (type)
2301 && (TYPE_UNSIGNED (type)
2302 || TYPE_PRECISION (type) > 1)
2303 && wi::leu_p (tree_nonzero_bits (@1), 1))))
2305 /* Transform { 0 or 1 } * { 0 or 1 } into { 0 or 1 } & { 0 or 1 }. */
2307 (mult zero_one_valued_p@0 zero_one_valued_p@1)
2308 (if (INTEGRAL_TYPE_P (type))
2311 (for cmp (tcc_comparison)
2312 icmp (inverted_tcc_comparison)
2313 /* Fold (((a < b) & c) | ((a >= b) & d)) into (a < b ? c : d) & 1. */
2316 (bit_and:c (convert? (cmp@0 @01 @02)) @3)
2317 (bit_and:c (convert? (icmp@4 @01 @02)) @5))
2318 (if (INTEGRAL_TYPE_P (type)
2319 && invert_tree_comparison (cmp, HONOR_NANS (@01)) == icmp
2320 /* The scalar version has to be canonicalized after vectorization
2321 because it makes unconditional loads conditional ones, which
2322 means we lose vectorization because the loads may trap. */
2323 && canonicalize_math_after_vectorization_p ())
2324 (bit_and (cond @0 @3 @5) { build_one_cst (type); })))
2326 /* Fold ((-(a < b) & c) | (-(a >= b) & d)) into a < b ? c : d. This is
2327 canonicalized further and we recognize the conditional form:
2328 (a < b ? c : 0) | (a >= b ? d : 0) into a < b ? c : d. */
2331 (cond (cmp@0 @01 @02) @3 zerop)
2332 (cond (icmp@4 @01 @02) @5 zerop))
2333 (if (INTEGRAL_TYPE_P (type)
2334 && invert_tree_comparison (cmp, HONOR_NANS (@01)) == icmp
2335 /* The scalar version has to be canonicalized after vectorization
2336 because it makes unconditional loads conditional ones, which
2337 means we lose vectorization because the loads may trap. */
2338 && canonicalize_math_after_vectorization_p ())
2341 /* Vector Fold (((a < b) & c) | ((a >= b) & d)) into a < b ? c : d.
2342 and ((~(a < b) & c) | (~(a >= b) & d)) into a < b ? c : d. */
2345 (bit_and:c (vec_cond:s (cmp@0 @6 @7) @4 @5) @2)
2346 (bit_and:c (vec_cond:s (icmp@1 @6 @7) @4 @5) @3))
2347 (if (integer_zerop (@5)
2348 && invert_tree_comparison (cmp, HONOR_NANS (@6)) == icmp)
2350 (if (integer_onep (@4))
2351 (bit_and (vec_cond @0 @2 @3) @4))
2352 (if (integer_minus_onep (@4))
2353 (vec_cond @0 @2 @3)))
2354 (if (integer_zerop (@4)
2355 && invert_tree_comparison (cmp, HONOR_NANS (@6)) == icmp)
2357 (if (integer_onep (@5))
2358 (bit_and (vec_cond @0 @3 @2) @5))
2359 (if (integer_minus_onep (@5))
2360 (vec_cond @0 @3 @2))))))
2362 /* Scalar Vectorized Fold ((-(a < b) & c) | (-(a >= b) & d))
2363 into a < b ? d : c. */
2366 (vec_cond:s (cmp@0 @4 @5) @2 integer_zerop)
2367 (vec_cond:s (icmp@1 @4 @5) @3 integer_zerop))
2368 (if (invert_tree_comparison (cmp, HONOR_NANS (@4)) == icmp)
2369 (vec_cond @0 @2 @3))))
2371 /* Transform X & -Y into X * Y when Y is { 0 or 1 }. */
2373 (bit_and:c (convert? (negate zero_one_valued_p@0)) @1)
2374 (if (INTEGRAL_TYPE_P (type)
2375 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
2376 && TREE_CODE (TREE_TYPE (@0)) != BOOLEAN_TYPE
2377 /* Sign extending of the neg or a truncation of the neg
2379 && (!TYPE_UNSIGNED (TREE_TYPE (@0))
2380 || TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))))
2381 (mult (convert @0) @1)))
2383 /* Narrow integer multiplication by a zero_one_valued_p operand.
2384 Multiplication by [0,1] is guaranteed not to overflow. */
2386 (convert (mult@0 zero_one_valued_p@1 INTEGER_CST@2))
2387 (if (INTEGRAL_TYPE_P (type)
2388 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
2389 && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (@0)))
2390 (mult (convert @1) (convert @2))))
2392 /* (X << C) != 0 can be simplified to X, when C is zero_one_valued_p.
2393 Check that the shift is well-defined (C is less than TYPE_PRECISION)
2394 as some targets (such as x86's SSE) may return zero for larger C. */
2396 (ne (lshift zero_one_valued_p@0 INTEGER_CST@1) integer_zerop@2)
2397 (if (tree_fits_shwi_p (@1)
2398 && tree_to_shwi (@1) > 0
2399 && tree_to_shwi (@1) < TYPE_PRECISION (TREE_TYPE (@0)))
2402 /* (X << C) == 0 can be simplified to X == 0, when C is zero_one_valued_p.
2403 Check that the shift is well-defined (C is less than TYPE_PRECISION)
2404 as some targets (such as x86's SSE) may return zero for larger C. */
2406 (eq (lshift zero_one_valued_p@0 INTEGER_CST@1) integer_zerop@2)
2407 (if (tree_fits_shwi_p (@1)
2408 && tree_to_shwi (@1) > 0
2409 && tree_to_shwi (@1) < TYPE_PRECISION (TREE_TYPE (@0)))
2412 /* Convert ~ (-A) to A - 1. */
2414 (bit_not (convert? (negate @0)))
2415 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
2416 || !TYPE_UNSIGNED (TREE_TYPE (@0)))
2417 (convert (minus @0 { build_each_one_cst (TREE_TYPE (@0)); }))))
2419 /* Convert - (~A) to A + 1. */
2421 (negate (nop_convert? (bit_not @0)))
2422 (plus (view_convert @0) { build_each_one_cst (type); }))
2424 /* (a & b) ^ (a == b) -> !(a | b) */
2425 /* (a & b) == (a ^ b) -> !(a | b) */
2426 (for first_op (bit_xor eq)
2427 second_op (eq bit_xor)
2429 (first_op:c (bit_and:c truth_valued_p@0 truth_valued_p@1) (second_op:c @0 @1))
2430 (bit_not (bit_ior @0 @1))))
2432 /* Convert ~ (A - 1) or ~ (A + -1) to -A. */
2434 (bit_not (convert? (minus @0 integer_each_onep)))
2435 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
2436 || !TYPE_UNSIGNED (TREE_TYPE (@0)))
2437 (convert (negate @0))))
2439 (bit_not (convert? (plus @0 integer_all_onesp)))
2440 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
2441 || !TYPE_UNSIGNED (TREE_TYPE (@0)))
2442 (convert (negate @0))))
2444 /* Part of convert ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify. */
2446 (bit_not (convert? (bit_xor @0 INTEGER_CST@1)))
2447 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2448 (convert (bit_xor @0 (bit_not @1)))))
2450 (bit_not (convert? (bit_xor:c (bit_not @0) @1)))
2451 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2452 (convert (bit_xor @0 @1))))
2454 /* Otherwise prefer ~(X ^ Y) to ~X ^ Y as more canonical. */
2456 (bit_xor:c (nop_convert?:s (bit_not:s @0)) @1)
2457 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2458 (bit_not (bit_xor (view_convert @0) @1))))
2460 /* ~(a ^ b) is a == b for truth valued a and b. */
2462 (bit_not (bit_xor:s truth_valued_p@0 truth_valued_p@1))
2463 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2464 && TYPE_PRECISION (TREE_TYPE (@0)) == 1)
2465 (convert (eq @0 @1))))
2467 /* (~a) == b is a ^ b for truth valued a and b. */
2469 (eq:c (bit_not:s truth_valued_p@0) truth_valued_p@1)
2470 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2471 && TYPE_PRECISION (TREE_TYPE (@0)) == 1)
2472 (convert (bit_xor @0 @1))))
2474 /* (x & ~m) | (y & m) -> ((x ^ y) & m) ^ x */
2476 (bit_ior:c (bit_and:cs @0 (bit_not @2)) (bit_and:cs @1 @2))
2477 (bit_xor (bit_and (bit_xor @0 @1) @2) @0))
2479 /* Fold A - (A & B) into ~B & A. */
2481 (minus (convert1? @0) (convert2?:s (bit_and:cs @@0 @1)))
2482 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
2483 && tree_nop_conversion_p (type, TREE_TYPE (@1)))
2484 (convert (bit_and (bit_not @1) @0))))
2486 /* (m1 CMP m2) * d -> (m1 CMP m2) ? d : 0 */
2487 (if (!canonicalize_math_p ())
2488 (for cmp (tcc_comparison)
2490 (mult:c (convert (cmp@0 @1 @2)) @3)
2491 (if (INTEGRAL_TYPE_P (type)
2492 && INTEGRAL_TYPE_P (TREE_TYPE (@0)))
2493 (cond @0 @3 { build_zero_cst (type); })))
2494 /* (-(m1 CMP m2)) & d -> (m1 CMP m2) ? d : 0 */
2496 (bit_and:c (negate (convert (cmp@0 @1 @2))) @3)
2497 (if (INTEGRAL_TYPE_P (type)
2498 && INTEGRAL_TYPE_P (TREE_TYPE (@0)))
2499 (cond @0 @3 { build_zero_cst (type); })))
2503 /* For integral types with undefined overflow and C != 0 fold
2504 x * C EQ/NE y * C into x EQ/NE y. */
2507 (cmp (mult:c @0 @1) (mult:c @2 @1))
2508 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2509 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2510 && tree_expr_nonzero_p (@1))
2513 /* For integral types with wrapping overflow and C odd fold
2514 x * C EQ/NE y * C into x EQ/NE y. */
2517 (cmp (mult @0 INTEGER_CST@1) (mult @2 @1))
2518 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2519 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))
2520 && (TREE_INT_CST_LOW (@1) & 1) != 0)
2523 /* For integral types with undefined overflow and C != 0 fold
2524 x * C RELOP y * C into:
2526 x RELOP y for nonnegative C
2527 y RELOP x for negative C */
2528 (for cmp (lt gt le ge)
2530 (cmp (mult:c @0 @1) (mult:c @2 @1))
2531 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2532 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
2533 (if (tree_expr_nonnegative_p (@1) && tree_expr_nonzero_p (@1))
2535 (if (TREE_CODE (@1) == INTEGER_CST
2536 && wi::neg_p (wi::to_wide (@1), TYPE_SIGN (TREE_TYPE (@1))))
2539 /* (X - 1U) <= INT_MAX-1U into (int) X > 0. */
2543 (cmp (plus @0 integer_minus_onep@1) INTEGER_CST@2)
2544 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2545 && TYPE_UNSIGNED (TREE_TYPE (@0))
2546 && TYPE_PRECISION (TREE_TYPE (@0)) > 1
2547 && (wi::to_wide (@2)
2548 == wi::max_value (TYPE_PRECISION (TREE_TYPE (@0)), SIGNED) - 1))
2549 (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
2550 (icmp (convert:stype @0) { build_int_cst (stype, 0); })))))
2552 /* X / 4 < Y / 4 iff X < Y when the division is known to be exact. */
2553 (for cmp (simple_comparison)
2555 (cmp (convert?@3 (exact_div @0 INTEGER_CST@2)) (convert? (exact_div @1 @2)))
2556 (if (element_precision (@3) >= element_precision (@0)
2557 && types_match (@0, @1))
2558 (if (wi::lt_p (wi::to_wide (@2), 0, TYPE_SIGN (TREE_TYPE (@2))))
2559 (if (!TYPE_UNSIGNED (TREE_TYPE (@3)))
2561 (if (tree_expr_nonzero_p (@0) && tree_expr_nonzero_p (@1))
2564 tree utype = unsigned_type_for (TREE_TYPE (@0));
2566 (cmp (convert:utype @1) (convert:utype @0)))))
2567 (if (wi::gt_p (wi::to_wide (@2), 1, TYPE_SIGN (TREE_TYPE (@2))))
2568 (if (TYPE_UNSIGNED (TREE_TYPE (@0)) || !TYPE_UNSIGNED (TREE_TYPE (@3)))
2572 tree utype = unsigned_type_for (TREE_TYPE (@0));
2574 (cmp (convert:utype @0) (convert:utype @1)))))))))
2576 /* X / C1 op C2 into a simple range test. */
2577 (for cmp (simple_comparison)
2579 (cmp (trunc_div:s @0 INTEGER_CST@1) INTEGER_CST@2)
2580 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2581 && integer_nonzerop (@1)
2582 && !TREE_OVERFLOW (@1)
2583 && !TREE_OVERFLOW (@2))
2584 (with { tree lo, hi; bool neg_overflow;
2585 enum tree_code code = fold_div_compare (cmp, @1, @2, &lo, &hi,
2588 (if (code == LT_EXPR || code == GE_EXPR)
2589 (if (TREE_OVERFLOW (lo))
2590 { build_int_cst (type, (code == LT_EXPR) ^ neg_overflow); }
2591 (if (code == LT_EXPR)
2594 (if (code == LE_EXPR || code == GT_EXPR)
2595 (if (TREE_OVERFLOW (hi))
2596 { build_int_cst (type, (code == LE_EXPR) ^ neg_overflow); }
2597 (if (code == LE_EXPR)
2601 { build_int_cst (type, code == NE_EXPR); })
2602 (if (code == EQ_EXPR && !hi)
2604 (if (code == EQ_EXPR && !lo)
2606 (if (code == NE_EXPR && !hi)
2608 (if (code == NE_EXPR && !lo)
2611 { build_range_check (UNKNOWN_LOCATION, type, @0, code == EQ_EXPR,
2615 tree etype = range_check_type (TREE_TYPE (@0));
2618 hi = fold_convert (etype, hi);
2619 lo = fold_convert (etype, lo);
2620 hi = const_binop (MINUS_EXPR, etype, hi, lo);
2623 (if (etype && hi && !TREE_OVERFLOW (hi))
2624 (if (code == EQ_EXPR)
2625 (le (minus (convert:etype @0) { lo; }) { hi; })
2626 (gt (minus (convert:etype @0) { lo; }) { hi; })))))))))
2628 /* X + Z < Y + Z is the same as X < Y when there is no overflow. */
2629 (for op (lt le ge gt)
2631 (op (plus:c @0 @2) (plus:c @1 @2))
2632 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2633 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
2636 /* As a special case, X + C < Y + C is the same as (signed) X < (signed) Y
2637 when C is an unsigned integer constant with only the MSB set, and X and
2638 Y have types of equal or lower integer conversion rank than C's. */
2639 (for op (lt le ge gt)
2641 (op (plus @1 INTEGER_CST@0) (plus @2 @0))
2642 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2643 && TYPE_UNSIGNED (TREE_TYPE (@0))
2644 && wi::only_sign_bit_p (wi::to_wide (@0)))
2645 (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
2646 (op (convert:stype @1) (convert:stype @2))))))
2648 /* For equality and subtraction, this is also true with wrapping overflow. */
2649 (for op (eq ne minus)
2651 (op (plus:c @0 @2) (plus:c @1 @2))
2652 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2653 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2654 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
2656 /* And similar for pointers. */
2659 (op (pointer_plus @0 @1) (pointer_plus @0 @2))
2662 (pointer_diff (pointer_plus @0 @1) (pointer_plus @0 @2))
2663 (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
2664 (convert (minus @1 @2))))
2666 /* X - Z < Y - Z is the same as X < Y when there is no overflow. */
2667 (for op (lt le ge gt)
2669 (op (minus @0 @2) (minus @1 @2))
2670 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2671 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
2673 /* For equality and subtraction, this is also true with wrapping overflow. */
2674 (for op (eq ne minus)
2676 (op (minus @0 @2) (minus @1 @2))
2677 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2678 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2679 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
2681 /* And for pointers... */
2682 (for op (simple_comparison)
2684 (op (pointer_diff@3 @0 @2) (pointer_diff @1 @2))
2685 (if (!TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
2688 (minus (pointer_diff@3 @0 @2) (pointer_diff @1 @2))
2689 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@3))
2690 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
2691 (pointer_diff @0 @1)))
2693 /* Z - X < Z - Y is the same as Y < X when there is no overflow. */
2694 (for op (lt le ge gt)
2696 (op (minus @2 @0) (minus @2 @1))
2697 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2698 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
2700 /* For equality and subtraction, this is also true with wrapping overflow. */
2701 (for op (eq ne minus)
2703 (op (minus @2 @0) (minus @2 @1))
2704 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2705 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2706 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
2708 /* And for pointers... */
2709 (for op (simple_comparison)
2711 (op (pointer_diff@3 @2 @0) (pointer_diff @2 @1))
2712 (if (!TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
2715 (minus (pointer_diff@3 @2 @0) (pointer_diff @2 @1))
2716 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@3))
2717 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
2718 (pointer_diff @1 @0)))
2720 /* X + Y < Y is the same as X < 0 when there is no overflow. */
2721 (for op (lt le gt ge)
2723 (op:c (plus:c@2 @0 @1) @1)
2724 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2725 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2726 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0))
2727 && (CONSTANT_CLASS_P (@0) || single_use (@2)))
2728 (op @0 { build_zero_cst (TREE_TYPE (@0)); }))))
2729 /* For equality, this is also true with wrapping overflow. */
2732 (op:c (nop_convert?@3 (plus:c@2 @0 (convert1? @1))) (convert2? @1))
2733 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2734 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2735 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
2736 && (CONSTANT_CLASS_P (@0) || (single_use (@2) && single_use (@3)))
2737 && tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@2))
2738 && tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@1)))
2739 (op @0 { build_zero_cst (TREE_TYPE (@0)); })))
2741 (op:c (nop_convert?@3 (pointer_plus@2 (convert1? @0) @1)) (convert2? @0))
2742 (if (tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@0))
2743 && tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0))
2744 && (CONSTANT_CLASS_P (@1) || (single_use (@2) && single_use (@3))))
2745 (op @1 { build_zero_cst (TREE_TYPE (@1)); }))))
2747 /* (&a + b) !=/== (&a[1] + c) -> (&a[0] - &a[1]) + b !=/== c */
2750 (neeq:c ADDR_EXPR@0 (pointer_plus @2 @3))
2751 (with { poly_int64 diff; tree inner_type = TREE_TYPE (@3);}
2752 (if (ptr_difference_const (@0, @2, &diff))
2753 (neeq { build_int_cst_type (inner_type, diff); } @3))))
2755 (neeq (pointer_plus ADDR_EXPR@0 @1) (pointer_plus ADDR_EXPR@2 @3))
2756 (with { poly_int64 diff; tree inner_type = TREE_TYPE (@1);}
2757 (if (ptr_difference_const (@0, @2, &diff))
2758 (neeq (plus { build_int_cst_type (inner_type, diff); } @1) @3)))))
2760 /* X - Y < X is the same as Y > 0 when there is no overflow.
2761 For equality, this is also true with wrapping overflow. */
2762 (for op (simple_comparison)
2764 (op:c @0 (minus@2 @0 @1))
2765 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2766 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2767 || ((op == EQ_EXPR || op == NE_EXPR)
2768 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
2769 && (CONSTANT_CLASS_P (@1) || single_use (@2)))
2770 (op @1 { build_zero_cst (TREE_TYPE (@1)); }))))
2773 (X / Y) == 0 -> X < Y if X, Y are unsigned.
2774 (X / Y) != 0 -> X >= Y, if X, Y are unsigned. */
2778 (cmp (trunc_div @0 @1) integer_zerop)
2779 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
2780 /* Complex ==/!= is allowed, but not </>=. */
2781 && TREE_CODE (TREE_TYPE (@0)) != COMPLEX_TYPE
2782 && (VECTOR_TYPE_P (type) || !VECTOR_TYPE_P (TREE_TYPE (@0))))
2785 /* X == C - X can never be true if C is odd. */
2788 (cmp:c (convert? @0) (convert1? (minus INTEGER_CST@1 (convert2? @0))))
2789 (if (TREE_INT_CST_LOW (@1) & 1)
2790 { constant_boolean_node (cmp == NE_EXPR, type); })))
2795 U needs to be non-negative.
2799 U and N needs to be non-negative
2803 U needs to be non-negative and N needs to be a negative constant.
2805 (for cmp (lt ge le gt )
2806 bitop (bit_ior bit_ior bit_and bit_and)
2808 (cmp:c (bitop:c tree_expr_nonnegative_p@0 @1) @0)
2809 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)))
2810 (if (bitop == BIT_AND_EXPR || tree_expr_nonnegative_p (@1))
2811 { constant_boolean_node (cmp == GE_EXPR || cmp == LE_EXPR, type); }
2812 /* The sign is opposite now so the comparison is swapped around. */
2813 (if (TREE_CODE (@1) == INTEGER_CST && wi::neg_p (wi::to_wide (@1)))
2814 { constant_boolean_node (cmp == LT_EXPR, type); })))))
2816 /* Arguments on which one can call get_nonzero_bits to get the bits
2818 (match with_possible_nonzero_bits
2820 (match with_possible_nonzero_bits
2822 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))))
2823 /* Slightly extended version, do not make it recursive to keep it cheap. */
2824 (match (with_possible_nonzero_bits2 @0)
2825 with_possible_nonzero_bits@0)
2826 (match (with_possible_nonzero_bits2 @0)
2827 (bit_and:c with_possible_nonzero_bits@0 @2))
2829 /* Same for bits that are known to be set, but we do not have
2830 an equivalent to get_nonzero_bits yet. */
2831 (match (with_certain_nonzero_bits2 @0)
2833 (match (with_certain_nonzero_bits2 @0)
2834 (bit_ior @1 INTEGER_CST@0))
2836 /* X == C (or X & Z == Y | C) is impossible if ~nonzero(X) & C != 0. */
2839 (cmp:c (with_possible_nonzero_bits2 @0) (with_certain_nonzero_bits2 @1))
2840 (if (wi::bit_and_not (wi::to_wide (@1), get_nonzero_bits (@0)) != 0)
2841 { constant_boolean_node (cmp == NE_EXPR, type); })))
2843 /* ((X inner_op C0) outer_op C1)
2844 With X being a tree where value_range has reasoned certain bits to always be
2845 zero throughout its computed value range,
2846 inner_op = {|,^}, outer_op = {|,^} and inner_op != outer_op
2847 where zero_mask has 1's for all bits that are sure to be 0 in
2849 if (inner_op == '^') C0 &= ~C1;
2850 if ((C0 & ~zero_mask) == 0) then emit (X outer_op (C0 outer_op C1)
2851 if ((C1 & ~zero_mask) == 0) then emit (X inner_op (C0 outer_op C1)
2853 (for inner_op (bit_ior bit_xor)
2854 outer_op (bit_xor bit_ior)
2857 (inner_op:s @2 INTEGER_CST@0) INTEGER_CST@1)
2861 wide_int zero_mask_not;
2865 if (TREE_CODE (@2) == SSA_NAME)
2866 zero_mask_not = get_nonzero_bits (@2);
2870 if (inner_op == BIT_XOR_EXPR)
2872 C0 = wi::bit_and_not (wi::to_wide (@0), wi::to_wide (@1));
2873 cst_emit = C0 | wi::to_wide (@1);
2877 C0 = wi::to_wide (@0);
2878 cst_emit = C0 ^ wi::to_wide (@1);
2881 (if (!fail && (C0 & zero_mask_not) == 0)
2882 (outer_op @2 { wide_int_to_tree (type, cst_emit); })
2883 (if (!fail && (wi::to_wide (@1) & zero_mask_not) == 0)
2884 (inner_op @2 { wide_int_to_tree (type, cst_emit); }))))))
2886 /* Associate (p +p off1) +p off2 as (p +p (off1 + off2)). */
2888 (pointer_plus (pointer_plus:s @0 @1) @3)
2889 (pointer_plus @0 (plus @1 @3)))
2892 (pointer_plus (convert:s (pointer_plus:s @0 @1)) @3)
2893 (convert:type (pointer_plus @0 (plus @1 @3))))
2900 tem4 = (unsigned long) tem3;
2905 (pointer_plus @0 (convert?@2 (minus@3 (convert @1) (convert @0))))
2906 /* Conditionally look through a sign-changing conversion. */
2907 (if (TYPE_PRECISION (TREE_TYPE (@2)) == TYPE_PRECISION (TREE_TYPE (@3))
2908 && ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@1)))
2909 || (GENERIC && type == TREE_TYPE (@1))))
2912 (pointer_plus @0 (convert?@2 (pointer_diff@3 @1 @@0)))
2913 (if (TYPE_PRECISION (TREE_TYPE (@2)) >= TYPE_PRECISION (TREE_TYPE (@3)))
2917 tem = (sizetype) ptr;
2921 and produce the simpler and easier to analyze with respect to alignment
2922 ... = ptr & ~algn; */
2924 (pointer_plus @0 (negate (bit_and (convert @0) INTEGER_CST@1)))
2925 (with { tree algn = wide_int_to_tree (TREE_TYPE (@0), ~wi::to_wide (@1)); }
2926 (bit_and @0 { algn; })))
2928 /* Try folding difference of addresses. */
2930 (minus (convert ADDR_EXPR@0) (convert (pointer_plus @1 @2)))
2931 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2932 (with { poly_int64 diff; }
2933 (if (ptr_difference_const (@0, @1, &diff))
2934 (minus { build_int_cst_type (type, diff); } (convert @2))))))
2936 (minus (convert (pointer_plus @0 @2)) (convert ADDR_EXPR@1))
2937 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2938 (with { poly_int64 diff; }
2939 (if (ptr_difference_const (@0, @1, &diff))
2940 (plus (convert @2) { build_int_cst_type (type, diff); })))))
2942 (minus (convert ADDR_EXPR@0) (convert @1))
2943 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2944 (with { poly_int64 diff; }
2945 (if (ptr_difference_const (@0, @1, &diff))
2946 { build_int_cst_type (type, diff); }))))
2948 (minus (convert @0) (convert ADDR_EXPR@1))
2949 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2950 (with { poly_int64 diff; }
2951 (if (ptr_difference_const (@0, @1, &diff))
2952 { build_int_cst_type (type, diff); }))))
2954 (pointer_diff (convert?@2 ADDR_EXPR@0) (convert1?@3 @1))
2955 (if (tree_nop_conversion_p (TREE_TYPE(@2), TREE_TYPE (@0))
2956 && tree_nop_conversion_p (TREE_TYPE(@3), TREE_TYPE (@1)))
2957 (with { poly_int64 diff; }
2958 (if (ptr_difference_const (@0, @1, &diff))
2959 { build_int_cst_type (type, diff); }))))
2961 (pointer_diff (convert?@2 @0) (convert1?@3 ADDR_EXPR@1))
2962 (if (tree_nop_conversion_p (TREE_TYPE(@2), TREE_TYPE (@0))
2963 && tree_nop_conversion_p (TREE_TYPE(@3), TREE_TYPE (@1)))
2964 (with { poly_int64 diff; }
2965 (if (ptr_difference_const (@0, @1, &diff))
2966 { build_int_cst_type (type, diff); }))))
2968 /* (&a+b) - (&a[1] + c) -> sizeof(a[0]) + (b - c) */
2970 (pointer_diff (pointer_plus ADDR_EXPR@0 @1) (pointer_plus ADDR_EXPR@2 @3))
2971 (with { poly_int64 diff; }
2972 (if (ptr_difference_const (@0, @2, &diff))
2973 (plus { build_int_cst_type (type, diff); } (convert (minus @1 @3))))))
2974 /* (p + b) - &p->d -> offsetof (*p, d) + b */
2976 (pointer_diff (pointer_plus @0 @1) ADDR_EXPR@2)
2977 (with { poly_int64 diff; }
2978 (if (ptr_difference_const (@0, @2, &diff))
2979 (plus { build_int_cst_type (type, diff); } (convert @1)))))
2981 (pointer_diff ADDR_EXPR@0 (pointer_plus @1 @2))
2982 (with { poly_int64 diff; }
2983 (if (ptr_difference_const (@0, @1, &diff))
2984 (minus { build_int_cst_type (type, diff); } (convert @2)))))
2986 /* Canonicalize (T *)(ptr - ptr-cst) to &MEM[ptr + -ptr-cst]. */
2988 (convert (pointer_diff @0 INTEGER_CST@1))
2989 (if (POINTER_TYPE_P (type))
2990 { build_fold_addr_expr_with_type
2991 (build2 (MEM_REF, char_type_node, @0,
2992 wide_int_to_tree (ptr_type_node, wi::neg (wi::to_wide (@1)))),
2995 /* If arg0 is derived from the address of an object or function, we may
2996 be able to fold this expression using the object or function's
2999 (bit_and (convert? @0) INTEGER_CST@1)
3000 (if (POINTER_TYPE_P (TREE_TYPE (@0))
3001 && tree_nop_conversion_p (type, TREE_TYPE (@0)))
3005 unsigned HOST_WIDE_INT bitpos;
3006 get_pointer_alignment_1 (@0, &align, &bitpos);
3008 (if (wi::ltu_p (wi::to_wide (@1), align / BITS_PER_UNIT))
3009 { wide_int_to_tree (type, (wi::to_wide (@1)
3010 & (bitpos / BITS_PER_UNIT))); }))))
3013 uniform_integer_cst_p
3015 tree int_cst = uniform_integer_cst_p (t);
3016 tree inner_type = TREE_TYPE (int_cst);
3018 (if ((INTEGRAL_TYPE_P (inner_type)
3019 || POINTER_TYPE_P (inner_type))
3020 && wi::eq_p (wi::to_wide (int_cst), wi::min_value (inner_type))))))
3023 uniform_integer_cst_p
3025 tree int_cst = uniform_integer_cst_p (t);
3026 tree itype = TREE_TYPE (int_cst);
3028 (if ((INTEGRAL_TYPE_P (itype)
3029 || POINTER_TYPE_P (itype))
3030 && wi::eq_p (wi::to_wide (int_cst), wi::max_value (itype))))))
3032 /* x > y && x != XXX_MIN --> x > y
3033 x > y && x == XXX_MIN --> false . */
3036 (bit_and:c (gt:c@2 @0 @1) (eqne @0 min_value))
3038 (if (eqne == EQ_EXPR)
3039 { constant_boolean_node (false, type); })
3040 (if (eqne == NE_EXPR)
3044 /* x < y && x != XXX_MAX --> x < y
3045 x < y && x == XXX_MAX --> false. */
3048 (bit_and:c (lt:c@2 @0 @1) (eqne @0 max_value))
3050 (if (eqne == EQ_EXPR)
3051 { constant_boolean_node (false, type); })
3052 (if (eqne == NE_EXPR)
3056 /* x <= y && x == XXX_MIN --> x == XXX_MIN. */
3058 (bit_and:c (le:c @0 @1) (eq@2 @0 min_value))
3061 /* x >= y && x == XXX_MAX --> x == XXX_MAX. */
3063 (bit_and:c (ge:c @0 @1) (eq@2 @0 max_value))
3066 /* x > y || x != XXX_MIN --> x != XXX_MIN. */
3068 (bit_ior:c (gt:c @0 @1) (ne@2 @0 min_value))
3071 /* x <= y || x != XXX_MIN --> true. */
3073 (bit_ior:c (le:c @0 @1) (ne @0 min_value))
3074 { constant_boolean_node (true, type); })
3076 /* x <= y || x == XXX_MIN --> x <= y. */
3078 (bit_ior:c (le:c@2 @0 @1) (eq @0 min_value))
3081 /* x < y || x != XXX_MAX --> x != XXX_MAX. */
3083 (bit_ior:c (lt:c @0 @1) (ne@2 @0 max_value))
3086 /* x >= y || x != XXX_MAX --> true
3087 x >= y || x == XXX_MAX --> x >= y. */
3090 (bit_ior:c (ge:c@2 @0 @1) (eqne @0 max_value))
3092 (if (eqne == EQ_EXPR)
3094 (if (eqne == NE_EXPR)
3095 { constant_boolean_node (true, type); }))))
3097 /* y == XXX_MIN || x < y --> x <= y - 1 */
3099 (bit_ior:c (eq:s @1 min_value) (lt:cs @0 @1))
3100 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
3101 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
3102 (le @0 (minus @1 { build_int_cst (TREE_TYPE (@1), 1); }))))
3104 /* y != XXX_MIN && x >= y --> x > y - 1 */
3106 (bit_and:c (ne:s @1 min_value) (ge:cs @0 @1))
3107 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
3108 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
3109 (gt @0 (minus @1 { build_int_cst (TREE_TYPE (@1), 1); }))))
3111 /* Convert (X == CST1) && ((other)X OP2 CST2) to a known value
3112 based on CST1 OP2 CST2. Similarly for (X != CST1). */
3113 /* Convert (X == Y) && (X OP2 Y) to a known value if X is an integral type.
3114 Similarly for (X != Y). */
3117 (for code2 (eq ne lt gt le ge)
3119 (bit_and:c (code1:c@3 @0 @1) (code2:c@4 (convert?@c0 @0) @2))
3120 (if ((TREE_CODE (@1) == INTEGER_CST
3121 && TREE_CODE (@2) == INTEGER_CST)
3122 || ((INTEGRAL_TYPE_P (TREE_TYPE (@1))
3123 || POINTER_TYPE_P (TREE_TYPE (@1)))
3124 && bitwise_equal_p (@1, @2)))
3127 bool one_before = false;
3128 bool one_after = false;
3130 bool allbits = true;
3131 if (TREE_CODE (@1) == INTEGER_CST
3132 && TREE_CODE (@2) == INTEGER_CST)
3134 allbits = TYPE_PRECISION (TREE_TYPE (@1)) <= TYPE_PRECISION (TREE_TYPE (@2));
3135 auto t1 = wi::to_wide (fold_convert (TREE_TYPE (@2), @1));
3136 auto t2 = wi::to_wide (@2);
3137 cmp = wi::cmp (t1, t2, TYPE_SIGN (TREE_TYPE (@2)));
3148 case EQ_EXPR: val = (cmp == 0); break;
3149 case NE_EXPR: val = (cmp != 0); break;
3150 case LT_EXPR: val = (cmp < 0); break;
3151 case GT_EXPR: val = (cmp > 0); break;
3152 case LE_EXPR: val = (cmp <= 0); break;
3153 case GE_EXPR: val = (cmp >= 0); break;
3154 default: gcc_unreachable ();
3158 (if (code1 == EQ_EXPR && val) @3)
3159 (if (code1 == EQ_EXPR && !val) { constant_boolean_node (false, type); })
3160 (if (code1 == NE_EXPR && !val && allbits) @4)
3161 (if (code1 == NE_EXPR
3165 (gt @c0 (convert @1)))
3166 (if (code1 == NE_EXPR
3170 (lt @c0 (convert @1)))
3171 /* (a != (b+1)) & (a > b) -> a > (b+1) */
3172 (if (code1 == NE_EXPR
3176 (gt @c0 (convert @1)))
3177 /* (a != (b-1)) & (a < b) -> a < (b-1) */
3178 (if (code1 == NE_EXPR
3182 (lt @c0 (convert @1)))
3190 /* Convert (X OP1 CST1) && (X OP2 CST2).
3191 Convert (X OP1 Y) && (X OP2 Y). */
3193 (for code1 (lt le gt ge)
3194 (for code2 (lt le gt ge)
3196 (bit_and (code1:c@3 @0 @1) (code2:c@4 @0 @2))
3197 (if ((TREE_CODE (@1) == INTEGER_CST
3198 && TREE_CODE (@2) == INTEGER_CST)
3199 || ((INTEGRAL_TYPE_P (TREE_TYPE (@1))
3200 || POINTER_TYPE_P (TREE_TYPE (@1)))
3201 && operand_equal_p (@1, @2)))
3205 if (TREE_CODE (@1) == INTEGER_CST
3206 && TREE_CODE (@2) == INTEGER_CST)
3207 cmp = tree_int_cst_compare (@1, @2);
3210 /* Choose the more restrictive of two < or <= comparisons. */
3211 (if ((code1 == LT_EXPR || code1 == LE_EXPR)
3212 && (code2 == LT_EXPR || code2 == LE_EXPR))
3213 (if ((cmp < 0) || (cmp == 0 && code1 == LT_EXPR))
3216 /* Likewise chose the more restrictive of two > or >= comparisons. */
3217 (if ((code1 == GT_EXPR || code1 == GE_EXPR)
3218 && (code2 == GT_EXPR || code2 == GE_EXPR))
3219 (if ((cmp > 0) || (cmp == 0 && code1 == GT_EXPR))
3222 /* Check for singleton ranges. */
3224 && ((code1 == LE_EXPR && code2 == GE_EXPR)
3225 || (code1 == GE_EXPR && code2 == LE_EXPR)))
3227 /* Check for disjoint ranges. */
3229 && (code1 == LT_EXPR || code1 == LE_EXPR)
3230 && (code2 == GT_EXPR || code2 == GE_EXPR))
3231 { constant_boolean_node (false, type); })
3233 && (code1 == GT_EXPR || code1 == GE_EXPR)
3234 && (code2 == LT_EXPR || code2 == LE_EXPR))
3235 { constant_boolean_node (false, type); })
3238 /* Convert (X == CST1) || (X OP2 CST2) to a known value
3239 based on CST1 OP2 CST2. Similarly for (X != CST1). */
3240 /* Convert (X == Y) || (X OP2 Y) to a known value if X is an integral type.
3241 Similarly for (X != Y). */
3244 (for code2 (eq ne lt gt le ge)
3246 (bit_ior:c (code1:c@3 @0 @1) (code2:c@4 (convert?@c0 @0) @2))
3247 (if ((TREE_CODE (@1) == INTEGER_CST
3248 && TREE_CODE (@2) == INTEGER_CST)
3249 || ((INTEGRAL_TYPE_P (TREE_TYPE (@1))
3250 || POINTER_TYPE_P (TREE_TYPE (@1)))
3251 && bitwise_equal_p (@1, @2)))
3254 bool one_before = false;
3255 bool one_after = false;
3257 bool allbits = true;
3258 if (TREE_CODE (@1) == INTEGER_CST
3259 && TREE_CODE (@2) == INTEGER_CST)
3261 allbits = TYPE_PRECISION (TREE_TYPE (@1)) <= TYPE_PRECISION (TREE_TYPE (@2));
3262 auto t1 = wi::to_wide (fold_convert (TREE_TYPE (@2), @1));
3263 auto t2 = wi::to_wide (@2);
3264 cmp = wi::cmp (t1, t2, TYPE_SIGN (TREE_TYPE (@2)));
3275 case EQ_EXPR: val = (cmp == 0); break;
3276 case NE_EXPR: val = (cmp != 0); break;
3277 case LT_EXPR: val = (cmp < 0); break;
3278 case GT_EXPR: val = (cmp > 0); break;
3279 case LE_EXPR: val = (cmp <= 0); break;
3280 case GE_EXPR: val = (cmp >= 0); break;
3281 default: gcc_unreachable ();
3285 (if (code1 == EQ_EXPR && val) @4)
3286 (if (code1 == NE_EXPR && val && allbits) { constant_boolean_node (true, type); })
3287 (if (code1 == NE_EXPR && !val && allbits) @3)
3288 (if (code1 == EQ_EXPR
3293 (if (code1 == EQ_EXPR
3298 /* (a == (b-1)) | (a >= b) -> a >= (b-1) */
3299 (if (code1 == EQ_EXPR
3303 (ge @c0 (convert @1)))
3304 /* (a == (b+1)) | (a <= b) -> a <= (b-1) */
3305 (if (code1 == EQ_EXPR
3309 (le @c0 (convert @1)))
3317 /* Convert (X OP1 CST1) || (X OP2 CST2).
3318 Convert (X OP1 Y) || (X OP2 Y). */
3320 (for code1 (lt le gt ge)
3321 (for code2 (lt le gt ge)
3323 (bit_ior (code1@3 @0 @1) (code2@4 @0 @2))
3324 (if ((TREE_CODE (@1) == INTEGER_CST
3325 && TREE_CODE (@2) == INTEGER_CST)
3326 || ((INTEGRAL_TYPE_P (TREE_TYPE (@1))
3327 || POINTER_TYPE_P (TREE_TYPE (@1)))
3328 && operand_equal_p (@1, @2)))
3332 if (TREE_CODE (@1) == INTEGER_CST
3333 && TREE_CODE (@2) == INTEGER_CST)
3334 cmp = tree_int_cst_compare (@1, @2);
3337 /* Choose the more restrictive of two < or <= comparisons. */
3338 (if ((code1 == LT_EXPR || code1 == LE_EXPR)
3339 && (code2 == LT_EXPR || code2 == LE_EXPR))
3340 (if ((cmp < 0) || (cmp == 0 && code1 == LT_EXPR))
3343 /* Likewise chose the more restrictive of two > or >= comparisons. */
3344 (if ((code1 == GT_EXPR || code1 == GE_EXPR)
3345 && (code2 == GT_EXPR || code2 == GE_EXPR))
3346 (if ((cmp > 0) || (cmp == 0 && code1 == GT_EXPR))
3349 /* Check for singleton ranges. */
3351 && ((code1 == LT_EXPR && code2 == GT_EXPR)
3352 || (code1 == GT_EXPR && code2 == LT_EXPR)))
3354 /* Check for disjoint ranges. */
3356 && (code1 == LT_EXPR || code1 == LE_EXPR)
3357 && (code2 == GT_EXPR || code2 == GE_EXPR))
3358 { constant_boolean_node (true, type); })
3360 && (code1 == GT_EXPR || code1 == GE_EXPR)
3361 && (code2 == LT_EXPR || code2 == LE_EXPR))
3362 { constant_boolean_node (true, type); })
3365 /* Optimize (a CMP b) ^ (a CMP b) */
3366 /* Optimize (a CMP b) != (a CMP b) */
3367 (for op (bit_xor ne)
3368 (for cmp1 (lt lt lt le le le)
3369 cmp2 (gt eq ne ge eq ne)
3370 rcmp (ne le gt ne lt ge)
3372 (op:c (cmp1:c @0 @1) (cmp2:c @0 @1))
3373 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
3376 /* Optimize (a CMP b) == (a CMP b) */
3377 (for cmp1 (lt lt lt le le le)
3378 cmp2 (gt eq ne ge eq ne)
3379 rcmp (eq gt le eq ge lt)
3381 (eq:c (cmp1:c @0 @1) (cmp2:c @0 @1))
3382 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
3385 /* (type)([0,1]@a != 0) -> (type)a
3386 (type)([0,1]@a == 1) -> (type)a
3387 (type)([0,1]@a == 0) -> a ^ 1
3388 (type)([0,1]@a != 1) -> a ^ 1. */
3391 (convert (eqne zero_one_valued_p@0 INTEGER_CST@1))
3392 (if ((integer_zerop (@1) || integer_onep (@1)))
3393 (if ((eqne == EQ_EXPR) ^ integer_zerop (@1))
3395 /* Only do this if the types match as (type)(a == 0) is
3396 canonical form normally, while `a ^ 1` is canonical when
3397 there is no type change. */
3398 (if (types_match (type, TREE_TYPE (@0)))
3399 (bit_xor @0 { build_one_cst (type); } ))))))
3401 /* We can't reassociate at all for saturating types. */
3402 (if (!TYPE_SATURATING (type))
3404 /* Contract negates. */
3405 /* A + (-B) -> A - B */
3407 (plus:c @0 (convert? (negate @1)))
3408 /* Apply STRIP_NOPS on the negate. */
3409 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
3410 && !TYPE_OVERFLOW_SANITIZED (type))
3414 if (INTEGRAL_TYPE_P (type)
3415 && TYPE_OVERFLOW_WRAPS (type) != TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
3416 t1 = TYPE_OVERFLOW_WRAPS (type) ? type : TREE_TYPE (@1);
3418 (convert (minus (convert:t1 @0) (convert:t1 @1))))))
3419 /* A - (-B) -> A + B */
3421 (minus @0 (convert? (negate @1)))
3422 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
3423 && !TYPE_OVERFLOW_SANITIZED (type))
3427 if (INTEGRAL_TYPE_P (type)
3428 && TYPE_OVERFLOW_WRAPS (type) != TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
3429 t1 = TYPE_OVERFLOW_WRAPS (type) ? type : TREE_TYPE (@1);
3431 (convert (plus (convert:t1 @0) (convert:t1 @1))))))
3433 Sign-extension is ok except for INT_MIN, which thankfully cannot
3434 happen without overflow. */
3436 (negate (convert (negate @1)))
3437 (if (INTEGRAL_TYPE_P (type)
3438 && (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@1))
3439 || (!TYPE_UNSIGNED (TREE_TYPE (@1))
3440 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))))
3441 && !TYPE_OVERFLOW_SANITIZED (type)
3442 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@1)))
3445 (negate (convert negate_expr_p@1))
3446 (if (SCALAR_FLOAT_TYPE_P (type)
3447 && ((DECIMAL_FLOAT_TYPE_P (type)
3448 == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1))
3449 && TYPE_PRECISION (type) >= TYPE_PRECISION (TREE_TYPE (@1)))
3450 || !HONOR_SIGN_DEPENDENT_ROUNDING (type)))
3451 (convert (negate @1))))
3453 (negate (nop_convert? (negate @1)))
3454 (if (!TYPE_OVERFLOW_SANITIZED (type)
3455 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@1)))
3458 /* We can't reassociate floating-point unless -fassociative-math
3459 or fixed-point plus or minus because of saturation to +-Inf. */
3460 (if ((!FLOAT_TYPE_P (type) || flag_associative_math)
3461 && !FIXED_POINT_TYPE_P (type))
3463 /* Match patterns that allow contracting a plus-minus pair
3464 irrespective of overflow issues. */
3465 /* (A +- B) - A -> +- B */
3466 /* (A +- B) -+ B -> A */
3467 /* A - (A +- B) -> -+ B */
3468 /* A +- (B -+ A) -> +- B */
3470 (minus (nop_convert1? (plus:c (nop_convert2? @0) @1)) @0)
3473 (minus (nop_convert1? (minus (nop_convert2? @0) @1)) @0)
3474 (if (!ANY_INTEGRAL_TYPE_P (type)
3475 || TYPE_OVERFLOW_WRAPS (type))
3476 (negate (view_convert @1))
3477 (view_convert (negate @1))))
3479 (plus:c (nop_convert1? (minus @0 (nop_convert2? @1))) @1)
3482 (minus @0 (nop_convert1? (plus:c (nop_convert2? @0) @1)))
3483 (if (!ANY_INTEGRAL_TYPE_P (type)
3484 || TYPE_OVERFLOW_WRAPS (type))
3485 (negate (view_convert @1))
3486 (view_convert (negate @1))))
3488 (minus @0 (nop_convert1? (minus (nop_convert2? @0) @1)))
3490 /* (A +- B) + (C - A) -> C +- B */
3491 /* (A + B) - (A - C) -> B + C */
3492 /* More cases are handled with comparisons. */
3494 (plus:c (plus:c @0 @1) (minus @2 @0))
3497 (plus:c (minus @0 @1) (minus @2 @0))
3500 (plus:c (pointer_diff @0 @1) (pointer_diff @2 @0))
3501 (if (TYPE_OVERFLOW_UNDEFINED (type)
3502 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0)))
3503 (pointer_diff @2 @1)))
3505 (minus (plus:c @0 @1) (minus @0 @2))
3508 /* (A +- CST1) +- CST2 -> A + CST3
3509 Use view_convert because it is safe for vectors and equivalent for
3511 (for outer_op (plus minus)
3512 (for inner_op (plus minus)
3513 neg_inner_op (minus plus)
3515 (outer_op (nop_convert? (inner_op @0 CONSTANT_CLASS_P@1))
3517 /* If one of the types wraps, use that one. */
3518 (if (!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_WRAPS (type))
3519 /* If all 3 captures are CONSTANT_CLASS_P, punt, as we might recurse
3520 forever if something doesn't simplify into a constant. */
3521 (if (!CONSTANT_CLASS_P (@0))
3522 (if (outer_op == PLUS_EXPR)
3523 (plus (view_convert @0) (inner_op! @2 (view_convert @1)))
3524 (minus (view_convert @0) (neg_inner_op! @2 (view_convert @1)))))
3525 (if (!ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3526 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
3527 (if (outer_op == PLUS_EXPR)
3528 (view_convert (plus @0 (inner_op! (view_convert @2) @1)))
3529 (view_convert (minus @0 (neg_inner_op! (view_convert @2) @1))))
3530 /* If the constant operation overflows we cannot do the transform
3531 directly as we would introduce undefined overflow, for example
3532 with (a - 1) + INT_MIN. */
3533 (if (types_match (type, @0) && !TYPE_OVERFLOW_SANITIZED (type))
3534 (with { tree cst = const_binop (outer_op == inner_op
3535 ? PLUS_EXPR : MINUS_EXPR,
3538 (if (INTEGRAL_TYPE_P (type) && !TREE_OVERFLOW (cst))
3539 (inner_op @0 { cst; } )
3540 /* X+INT_MAX+1 is X-INT_MIN. */
3541 (if (INTEGRAL_TYPE_P (type)
3542 && wi::to_wide (cst) == wi::min_value (type))
3543 (neg_inner_op @0 { wide_int_to_tree (type, wi::to_wide (cst)); })
3544 /* Last resort, use some unsigned type. */
3545 (with { tree utype = unsigned_type_for (type); }
3547 (view_convert (inner_op
3548 (view_convert:utype @0)
3550 { TREE_OVERFLOW (cst)
3551 ? drop_tree_overflow (cst) : cst; })))))))))))))))
3553 /* (CST1 - A) +- CST2 -> CST3 - A */
3554 (for outer_op (plus minus)
3556 (outer_op (nop_convert? (minus CONSTANT_CLASS_P@1 @0)) CONSTANT_CLASS_P@2)
3557 /* If one of the types wraps, use that one. */
3558 (if (!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_WRAPS (type))
3559 /* If all 3 captures are CONSTANT_CLASS_P, punt, as we might recurse
3560 forever if something doesn't simplify into a constant. */
3561 (if (!CONSTANT_CLASS_P (@0))
3562 (minus (outer_op! (view_convert @1) @2) (view_convert @0)))
3563 (if (!ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3564 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
3565 (view_convert (minus (outer_op! @1 (view_convert @2)) @0))
3566 (if (types_match (type, @0) && !TYPE_OVERFLOW_SANITIZED (type))
3567 (with { tree cst = const_binop (outer_op, type, @1, @2); }
3568 (if (cst && !TREE_OVERFLOW (cst))
3569 (minus { cst; } @0))))))))
3571 /* CST1 - (CST2 - A) -> CST3 + A
3572 Use view_convert because it is safe for vectors and equivalent for
3575 (minus CONSTANT_CLASS_P@1 (nop_convert? (minus CONSTANT_CLASS_P@2 @0)))
3576 /* If one of the types wraps, use that one. */
3577 (if (!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_WRAPS (type))
3578 /* If all 3 captures are CONSTANT_CLASS_P, punt, as we might recurse
3579 forever if something doesn't simplify into a constant. */
3580 (if (!CONSTANT_CLASS_P (@0))
3581 (plus (view_convert @0) (minus! @1 (view_convert @2))))
3582 (if (!ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3583 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
3584 (view_convert (plus @0 (minus! (view_convert @1) @2)))
3585 (if (types_match (type, @0) && !TYPE_OVERFLOW_SANITIZED (type))
3586 (with { tree cst = const_binop (MINUS_EXPR, type, @1, @2); }
3587 (if (cst && !TREE_OVERFLOW (cst))
3588 (plus { cst; } @0)))))))
3590 /* ((T)(A)) + CST -> (T)(A + CST) */
3593 (plus (convert:s SSA_NAME@0) INTEGER_CST@1)
3594 (if (TREE_CODE (TREE_TYPE (@0)) == INTEGER_TYPE
3595 && TREE_CODE (type) == INTEGER_TYPE
3596 && TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (@0))
3597 && int_fits_type_p (@1, TREE_TYPE (@0)))
3598 /* Perform binary operation inside the cast if the constant fits
3599 and (A + CST)'s range does not overflow. */
3602 wi::overflow_type min_ovf = wi::OVF_OVERFLOW,
3603 max_ovf = wi::OVF_OVERFLOW;
3604 tree inner_type = TREE_TYPE (@0);
3607 = wide_int::from (wi::to_wide (@1), TYPE_PRECISION (inner_type),
3608 TYPE_SIGN (inner_type));
3611 if (get_global_range_query ()->range_of_expr (vr, @0)
3612 && !vr.varying_p () && !vr.undefined_p ())
3614 wide_int wmin0 = vr.lower_bound ();
3615 wide_int wmax0 = vr.upper_bound ();
3616 wi::add (wmin0, w1, TYPE_SIGN (inner_type), &min_ovf);
3617 wi::add (wmax0, w1, TYPE_SIGN (inner_type), &max_ovf);
3620 (if (min_ovf == wi::OVF_NONE && max_ovf == wi::OVF_NONE)
3621 (convert (plus @0 { wide_int_to_tree (TREE_TYPE (@0), w1); } )))
3625 /* ((T)(A + CST1)) + CST2 -> (T)(A) + (T)CST1 + CST2 */
3627 (for op (plus minus)
3629 (plus (convert:s (op:s @0 INTEGER_CST@1)) INTEGER_CST@2)
3630 (if (TREE_CODE (TREE_TYPE (@0)) == INTEGER_TYPE
3631 && TREE_CODE (type) == INTEGER_TYPE
3632 && TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (@0))
3633 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
3634 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0))
3635 && TYPE_OVERFLOW_WRAPS (type))
3636 (plus (convert @0) (op @2 (convert @1))))))
3639 /* (T)(A) +- (T)(B) -> (T)(A +- B) only when (A +- B) could be simplified
3640 to a simple value. */
3641 (for op (plus minus)
3643 (op (convert @0) (convert @1))
3644 (if (INTEGRAL_TYPE_P (type)
3645 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
3646 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))
3647 && types_match (TREE_TYPE (@0), TREE_TYPE (@1))
3648 && !TYPE_OVERFLOW_TRAPS (type)
3649 && !TYPE_OVERFLOW_SANITIZED (type))
3650 (convert (op! @0 @1)))))
3654 (plus:c (convert? (bit_not @0)) (convert? @0))
3655 (if (!TYPE_OVERFLOW_TRAPS (type))
3656 (convert { build_all_ones_cst (TREE_TYPE (@0)); })))
3660 (plus (convert? (bit_not @0)) integer_each_onep)
3661 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
3662 (negate (convert @0))))
3666 (minus (convert? (negate @0)) integer_each_onep)
3667 (if (!TYPE_OVERFLOW_TRAPS (type)
3668 && TREE_CODE (type) != COMPLEX_TYPE
3669 && tree_nop_conversion_p (type, TREE_TYPE (@0)))
3670 (bit_not (convert @0))))
3674 (minus integer_all_onesp @0)
3675 (if (TREE_CODE (type) != COMPLEX_TYPE)
3678 /* (T)(P + A) - (T)P -> (T) A */
3680 (minus (convert (plus:c @@0 @1))
3682 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
3683 /* For integer types, if A has a smaller type
3684 than T the result depends on the possible
3686 E.g. T=size_t, A=(unsigned)429497295, P>0.
3687 However, if an overflow in P + A would cause
3688 undefined behavior, we can assume that there
3690 || (INTEGRAL_TYPE_P (TREE_TYPE (@1))
3691 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))))
3694 (minus (convert (pointer_plus @@0 @1))
3696 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
3697 /* For pointer types, if the conversion of A to the
3698 final type requires a sign- or zero-extension,
3699 then we have to punt - it is not defined which
3701 || (POINTER_TYPE_P (TREE_TYPE (@0))
3702 && TREE_CODE (@1) == INTEGER_CST
3703 && tree_int_cst_sign_bit (@1) == 0))
3706 (pointer_diff (pointer_plus @@0 @1) @0)
3707 /* The second argument of pointer_plus must be interpreted as signed, and
3708 thus sign-extended if necessary. */
3709 (with { tree stype = signed_type_for (TREE_TYPE (@1)); }
3710 /* Use view_convert instead of convert here, as POINTER_PLUS_EXPR
3711 second arg is unsigned even when we need to consider it as signed,
3712 we don't want to diagnose overflow here. */
3713 (convert (view_convert:stype @1))))
3715 /* (T)P - (T)(P + A) -> -(T) A */
3717 (minus (convert? @0)
3718 (convert (plus:c @@0 @1)))
3719 (if (INTEGRAL_TYPE_P (type)
3720 && TYPE_OVERFLOW_UNDEFINED (type)
3721 /* For integer literals, using an intermediate unsigned type to avoid
3722 an overflow at run time is counter-productive because it introduces
3723 spurious overflows at compile time, in the form of TREE_OVERFLOW on
3724 the result, which may be problematic in GENERIC for some front-ends:
3725 (T)P - (T)(P + 4) -> (T)(-(U)4) -> (T)(4294967292) -> -4(OVF)
3726 so we use the direct path for them. */
3727 && TREE_CODE (@1) != INTEGER_CST
3728 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
3729 (with { tree utype = unsigned_type_for (type); }
3730 (convert (negate (convert:utype @1))))
3731 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
3732 /* For integer types, if A has a smaller type
3733 than T the result depends on the possible
3735 E.g. T=size_t, A=(unsigned)429497295, P>0.
3736 However, if an overflow in P + A would cause
3737 undefined behavior, we can assume that there
3739 || (INTEGRAL_TYPE_P (TREE_TYPE (@1))
3740 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))))
3741 (negate (convert @1)))))
3744 (convert (pointer_plus @@0 @1)))
3745 (if (INTEGRAL_TYPE_P (type)
3746 && TYPE_OVERFLOW_UNDEFINED (type)
3747 /* See above the rationale for this condition. */
3748 && TREE_CODE (@1) != INTEGER_CST
3749 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
3750 (with { tree utype = unsigned_type_for (type); }
3751 (convert (negate (convert:utype @1))))
3752 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
3753 /* For pointer types, if the conversion of A to the
3754 final type requires a sign- or zero-extension,
3755 then we have to punt - it is not defined which
3757 || (POINTER_TYPE_P (TREE_TYPE (@0))
3758 && TREE_CODE (@1) == INTEGER_CST
3759 && tree_int_cst_sign_bit (@1) == 0))
3760 (negate (convert @1)))))
3762 (pointer_diff @0 (pointer_plus @@0 @1))
3763 /* The second argument of pointer_plus must be interpreted as signed, and
3764 thus sign-extended if necessary. */
3765 (with { tree stype = signed_type_for (TREE_TYPE (@1)); }
3766 /* Use view_convert instead of convert here, as POINTER_PLUS_EXPR
3767 second arg is unsigned even when we need to consider it as signed,
3768 we don't want to diagnose overflow here. */
3769 (negate (convert (view_convert:stype @1)))))
3771 /* (T)(P + A) - (T)(P + B) -> (T)A - (T)B */
3773 (minus (convert (plus:c @@0 @1))
3774 (convert (plus:c @0 @2)))
3775 (if (INTEGRAL_TYPE_P (type)
3776 && TYPE_OVERFLOW_UNDEFINED (type)
3777 && element_precision (type) <= element_precision (TREE_TYPE (@1))
3778 && element_precision (type) <= element_precision (TREE_TYPE (@2)))
3779 (with { tree utype = unsigned_type_for (type); }
3780 (convert (minus (convert:utype @1) (convert:utype @2))))
3781 (if (((element_precision (type) <= element_precision (TREE_TYPE (@1)))
3782 == (element_precision (type) <= element_precision (TREE_TYPE (@2))))
3783 && (element_precision (type) <= element_precision (TREE_TYPE (@1))
3784 /* For integer types, if A has a smaller type
3785 than T the result depends on the possible
3787 E.g. T=size_t, A=(unsigned)429497295, P>0.
3788 However, if an overflow in P + A would cause
3789 undefined behavior, we can assume that there
3791 || (INTEGRAL_TYPE_P (TREE_TYPE (@1))
3792 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
3793 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))
3794 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@2)))))
3795 (minus (convert @1) (convert @2)))))
3797 (minus (convert (pointer_plus @@0 @1))
3798 (convert (pointer_plus @0 @2)))
3799 (if (INTEGRAL_TYPE_P (type)
3800 && TYPE_OVERFLOW_UNDEFINED (type)
3801 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
3802 (with { tree utype = unsigned_type_for (type); }
3803 (convert (minus (convert:utype @1) (convert:utype @2))))
3804 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
3805 /* For pointer types, if the conversion of A to the
3806 final type requires a sign- or zero-extension,
3807 then we have to punt - it is not defined which
3809 || (POINTER_TYPE_P (TREE_TYPE (@0))
3810 && TREE_CODE (@1) == INTEGER_CST
3811 && tree_int_cst_sign_bit (@1) == 0
3812 && TREE_CODE (@2) == INTEGER_CST
3813 && tree_int_cst_sign_bit (@2) == 0))
3814 (minus (convert @1) (convert @2)))))
3816 (pointer_diff (pointer_plus @0 @2) (pointer_plus @1 @2))
3817 (pointer_diff @0 @1))
3819 (pointer_diff (pointer_plus @@0 @1) (pointer_plus @0 @2))
3820 /* The second argument of pointer_plus must be interpreted as signed, and
3821 thus sign-extended if necessary. */
3822 (with { tree stype = signed_type_for (TREE_TYPE (@1)); }
3823 /* Use view_convert instead of convert here, as POINTER_PLUS_EXPR
3824 second arg is unsigned even when we need to consider it as signed,
3825 we don't want to diagnose overflow here. */
3826 (minus (convert (view_convert:stype @1))
3827 (convert (view_convert:stype @2)))))))
3829 /* (A * C) +- (B * C) -> (A+-B) * C and (A * C) +- A -> A * (C+-1).
3830 Modeled after fold_plusminus_mult_expr. */
3831 (if (!TYPE_SATURATING (type)
3832 && (!FLOAT_TYPE_P (type) || flag_associative_math))
3833 (for plusminus (plus minus)
3835 (plusminus (mult:cs@3 @0 @1) (mult:cs@4 @0 @2))
3836 (if (!ANY_INTEGRAL_TYPE_P (type)
3837 || TYPE_OVERFLOW_WRAPS (type)
3838 || (INTEGRAL_TYPE_P (type)
3839 && tree_expr_nonzero_p (@0)
3840 && expr_not_equal_to (@0, wi::minus_one (TYPE_PRECISION (type)))))
3841 (if (single_use (@3) || single_use (@4))
3842 /* If @1 +- @2 is constant require a hard single-use on either
3843 original operand (but not on both). */
3844 (mult (plusminus @1 @2) @0)
3845 (mult! (plusminus @1 @2) @0)
3847 /* We cannot generate constant 1 for fract. */
3848 (if (!ALL_FRACT_MODE_P (TYPE_MODE (type)))
3850 (plusminus @0 (mult:c@3 @0 @2))
3851 (if ((!ANY_INTEGRAL_TYPE_P (type)
3852 || TYPE_OVERFLOW_WRAPS (type)
3853 /* For @0 + @0*@2 this transformation would introduce UB
3854 (where there was none before) for @0 in [-1,0] and @2 max.
3855 For @0 - @0*@2 this transformation would introduce UB
3856 for @0 0 and @2 in [min,min+1] or @0 -1 and @2 min+1. */
3857 || (INTEGRAL_TYPE_P (type)
3858 && ((tree_expr_nonzero_p (@0)
3859 && expr_not_equal_to (@0,
3860 wi::minus_one (TYPE_PRECISION (type))))
3861 || (plusminus == PLUS_EXPR
3862 ? expr_not_equal_to (@2,
3863 wi::max_value (TYPE_PRECISION (type), SIGNED))
3864 /* Let's ignore the @0 -1 and @2 min case. */
3865 : (expr_not_equal_to (@2,
3866 wi::min_value (TYPE_PRECISION (type), SIGNED))
3867 && expr_not_equal_to (@2,
3868 wi::min_value (TYPE_PRECISION (type), SIGNED)
3871 (mult (plusminus { build_one_cst (type); } @2) @0)))
3873 (plusminus (mult:c@3 @0 @2) @0)
3874 (if ((!ANY_INTEGRAL_TYPE_P (type)
3875 || TYPE_OVERFLOW_WRAPS (type)
3876 /* For @0*@2 + @0 this transformation would introduce UB
3877 (where there was none before) for @0 in [-1,0] and @2 max.
3878 For @0*@2 - @0 this transformation would introduce UB
3879 for @0 0 and @2 min. */
3880 || (INTEGRAL_TYPE_P (type)
3881 && ((tree_expr_nonzero_p (@0)
3882 && (plusminus == MINUS_EXPR
3883 || expr_not_equal_to (@0,
3884 wi::minus_one (TYPE_PRECISION (type)))))
3885 || expr_not_equal_to (@2,
3886 (plusminus == PLUS_EXPR
3887 ? wi::max_value (TYPE_PRECISION (type), SIGNED)
3888 : wi::min_value (TYPE_PRECISION (type), SIGNED))))))
3890 (mult (plusminus @2 { build_one_cst (type); }) @0))))))
3893 /* Canonicalize X + (X << C) into X * (1 + (1 << C)) and
3894 (X << C1) + (X << C2) into X * ((1 << C1) + (1 << C2)). */
3896 (plus:c @0 (lshift:s @0 INTEGER_CST@1))
3897 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3898 && tree_fits_uhwi_p (@1)
3899 && tree_to_uhwi (@1) < element_precision (type)
3900 && (INTEGRAL_TYPE_P (TREE_TYPE (@0))
3901 || optab_handler (smul_optab,
3902 TYPE_MODE (type)) != CODE_FOR_nothing))
3903 (with { tree t = type;
3904 if (!TYPE_OVERFLOW_WRAPS (t)) t = unsigned_type_for (t);
3905 wide_int w = wi::set_bit_in_zero (tree_to_uhwi (@1),
3906 element_precision (type));
3908 tree cst = wide_int_to_tree (VECTOR_TYPE_P (t) ? TREE_TYPE (t)
3910 cst = build_uniform_cst (t, cst); }
3911 (convert (mult (convert:t @0) { cst; })))))
3913 (plus (lshift:s @0 INTEGER_CST@1) (lshift:s @0 INTEGER_CST@2))
3914 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3915 && tree_fits_uhwi_p (@1)
3916 && tree_to_uhwi (@1) < element_precision (type)
3917 && tree_fits_uhwi_p (@2)
3918 && tree_to_uhwi (@2) < element_precision (type)
3919 && (INTEGRAL_TYPE_P (TREE_TYPE (@0))
3920 || optab_handler (smul_optab,
3921 TYPE_MODE (type)) != CODE_FOR_nothing))
3922 (with { tree t = type;
3923 if (!TYPE_OVERFLOW_WRAPS (t)) t = unsigned_type_for (t);
3924 unsigned int prec = element_precision (type);
3925 wide_int w = wi::set_bit_in_zero (tree_to_uhwi (@1), prec);
3926 w += wi::set_bit_in_zero (tree_to_uhwi (@2), prec);
3927 tree cst = wide_int_to_tree (VECTOR_TYPE_P (t) ? TREE_TYPE (t)
3929 cst = build_uniform_cst (t, cst); }
3930 (convert (mult (convert:t @0) { cst; })))))
3933 /* Canonicalize (X*C1)|(X*C2) and (X*C1)^(X*C2) to (C1+C2)*X when
3934 tree_nonzero_bits allows IOR and XOR to be treated like PLUS.
3935 Likewise, handle (X<<C3) and X as legitimate variants of X*C. */
3936 (for op (bit_ior bit_xor)
3938 (op (mult:s@0 @1 INTEGER_CST@2)
3939 (mult:s@3 @1 INTEGER_CST@4))
3940 (if (INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_WRAPS (type)
3941 && (tree_nonzero_bits (@0) & tree_nonzero_bits (@3)) == 0)
3943 { wide_int_to_tree (type, wi::to_wide (@2) + wi::to_wide (@4)); })))
3945 (op:c (mult:s@0 @1 INTEGER_CST@2)
3946 (lshift:s@3 @1 INTEGER_CST@4))
3947 (if (INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_WRAPS (type)
3948 && tree_int_cst_sgn (@4) > 0
3949 && (tree_nonzero_bits (@0) & tree_nonzero_bits (@3)) == 0)
3950 (with { wide_int wone = wi::one (TYPE_PRECISION (type));
3951 wide_int c = wi::add (wi::to_wide (@2),
3952 wi::lshift (wone, wi::to_wide (@4))); }
3953 (mult @1 { wide_int_to_tree (type, c); }))))
3955 (op:c (mult:s@0 @1 INTEGER_CST@2)
3957 (if (INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_WRAPS (type)
3958 && (tree_nonzero_bits (@0) & tree_nonzero_bits (@1)) == 0)
3960 { wide_int_to_tree (type,
3961 wi::add (wi::to_wide (@2), 1)); })))
3963 (op (lshift:s@0 @1 INTEGER_CST@2)
3964 (lshift:s@3 @1 INTEGER_CST@4))
3965 (if (INTEGRAL_TYPE_P (type)
3966 && tree_int_cst_sgn (@2) > 0
3967 && tree_int_cst_sgn (@4) > 0
3968 && (tree_nonzero_bits (@0) & tree_nonzero_bits (@3)) == 0)
3969 (with { tree t = type;
3970 if (!TYPE_OVERFLOW_WRAPS (t))
3971 t = unsigned_type_for (t);
3972 wide_int wone = wi::one (TYPE_PRECISION (t));
3973 wide_int c = wi::add (wi::lshift (wone, wi::to_wide (@2)),
3974 wi::lshift (wone, wi::to_wide (@4))); }
3975 (convert (mult:t (convert:t @1) { wide_int_to_tree (t,c); })))))
3977 (op:c (lshift:s@0 @1 INTEGER_CST@2)
3979 (if (INTEGRAL_TYPE_P (type)
3980 && tree_int_cst_sgn (@2) > 0
3981 && (tree_nonzero_bits (@0) & tree_nonzero_bits (@1)) == 0)
3982 (with { tree t = type;
3983 if (!TYPE_OVERFLOW_WRAPS (t))
3984 t = unsigned_type_for (t);
3985 wide_int wone = wi::one (TYPE_PRECISION (t));
3986 wide_int c = wi::add (wi::lshift (wone, wi::to_wide (@2)), wone); }
3987 (convert (mult:t (convert:t @1) { wide_int_to_tree (t, c); }))))))
3989 /* Simplifications of MIN_EXPR, MAX_EXPR, fmin() and fmax(). */
3991 (for minmax (min max)
3995 /* max(max(x,y),x) -> max(x,y) */
3997 (minmax:c (minmax:c@2 @0 @1) @0)
3999 /* For fmin() and fmax(), skip folding when both are sNaN. */
4000 (for minmax (FMIN_ALL FMAX_ALL)
4003 (if (!tree_expr_maybe_signaling_nan_p (@0))
4005 /* min(max(x,y),y) -> y. */
4007 (min:c (max:c @0 @1) @1)
4009 /* max(min(x,y),y) -> y. */
4011 (max:c (min:c @0 @1) @1)
4013 /* max(a,-a) -> abs(a). */
4015 (max:c @0 (negate @0))
4016 (if (TREE_CODE (type) != COMPLEX_TYPE
4017 && (! ANY_INTEGRAL_TYPE_P (type)
4018 || TYPE_OVERFLOW_UNDEFINED (type)))
4020 /* min(a,-a) -> -abs(a). */
4022 (min:c @0 (negate @0))
4023 (if (TREE_CODE (type) != COMPLEX_TYPE
4024 && (! ANY_INTEGRAL_TYPE_P (type)
4025 || TYPE_OVERFLOW_UNDEFINED (type)))
4030 (if (INTEGRAL_TYPE_P (type)
4031 && TYPE_MIN_VALUE (type)
4032 && operand_equal_p (@1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
4034 (if (INTEGRAL_TYPE_P (type)
4035 && TYPE_MAX_VALUE (type)
4036 && operand_equal_p (@1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
4041 (if (INTEGRAL_TYPE_P (type)
4042 && TYPE_MAX_VALUE (type)
4043 && operand_equal_p (@1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
4045 (if (INTEGRAL_TYPE_P (type)
4046 && TYPE_MIN_VALUE (type)
4047 && operand_equal_p (@1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
4050 /* max (a, a + CST) -> a + CST where CST is positive. */
4051 /* max (a, a + CST) -> a where CST is negative. */
4053 (max:c @0 (plus@2 @0 INTEGER_CST@1))
4054 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
4055 (if (tree_int_cst_sgn (@1) > 0)
4059 /* min (a, a + CST) -> a where CST is positive. */
4060 /* min (a, a + CST) -> a + CST where CST is negative. */
4062 (min:c @0 (plus@2 @0 INTEGER_CST@1))
4063 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
4064 (if (tree_int_cst_sgn (@1) > 0)
4068 /* Simplify min (&var[off0], &var[off1]) etc. depending on whether
4069 the addresses are known to be less, equal or greater. */
4070 (for minmax (min max)
4073 (minmax (convert1?@2 addr@0) (convert2?@3 addr@1))
4076 poly_int64 off0, off1;
4078 int equal = address_compare (cmp, TREE_TYPE (@2), @0, @1, base0, base1,
4079 off0, off1, GENERIC);
4082 (if (minmax == MIN_EXPR)
4083 (if (known_le (off0, off1))
4085 (if (known_gt (off0, off1))
4087 (if (known_ge (off0, off1))
4089 (if (known_lt (off0, off1))
4092 /* (convert (minmax ((convert (x) c)))) -> minmax (x c) if x is promoted
4093 and the outer convert demotes the expression back to x's type. */
4094 (for minmax (min max)
4096 (convert (minmax@0 (convert @1) INTEGER_CST@2))
4097 (if (INTEGRAL_TYPE_P (type)
4098 && types_match (@1, type) && int_fits_type_p (@2, type)
4099 && TYPE_SIGN (TREE_TYPE (@0)) == TYPE_SIGN (type)
4100 && TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type))
4101 (minmax @1 (convert @2)))))
4103 (for minmax (FMIN_ALL FMAX_ALL)
4104 /* If either argument is NaN and other one is not sNaN, return the other
4105 one. Avoid the transformation if we get (and honor) a signalling NaN. */
4107 (minmax:c @0 REAL_CST@1)
4108 (if (real_isnan (TREE_REAL_CST_PTR (@1))
4109 && (!HONOR_SNANS (@1) || !TREE_REAL_CST (@1).signalling)
4110 && !tree_expr_maybe_signaling_nan_p (@0))
4112 /* Convert fmin/fmax to MIN_EXPR/MAX_EXPR. C99 requires these
4113 functions to return the numeric arg if the other one is NaN.
4114 MIN and MAX don't honor that, so only transform if -ffinite-math-only
4115 is set. C99 doesn't require -0.0 to be handled, so we don't have to
4116 worry about it either. */
4117 (if (flag_finite_math_only)
4124 /* min (-A, -B) -> -max (A, B) */
4125 (for minmax (min max FMIN_ALL FMAX_ALL)
4126 maxmin (max min FMAX_ALL FMIN_ALL)
4128 (minmax (negate:s@2 @0) (negate:s@3 @1))
4129 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
4130 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
4131 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
4132 (negate (maxmin @0 @1)))))
4133 /* MIN (~X, ~Y) -> ~MAX (X, Y)
4134 MAX (~X, ~Y) -> ~MIN (X, Y) */
4135 (for minmax (min max)
4138 (minmax (bit_not:s@2 @0) (bit_not:s@3 @1))
4139 (bit_not (maxmin @0 @1)))
4140 /* ~MAX(~X, Y) --> MIN(X, ~Y) */
4141 /* ~MIN(~X, Y) --> MAX(X, ~Y) */
4143 (bit_not (minmax:cs (bit_not @0) @1))
4144 (maxmin @0 (bit_not @1))))
4146 /* MIN (X, Y) == X -> X <= Y */
4147 /* MIN (X, Y) < X -> X > Y */
4148 /* MIN (X, Y) >= X -> X <= Y */
4149 (for minmax (min min min min max max max max)
4150 cmp (eq ne lt ge eq ne gt le )
4151 out (le gt gt le ge lt lt ge )
4153 (cmp:c (minmax:c @0 @1) @0)
4154 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0)))
4156 /* MIN (X, 5) == 0 -> X == 0
4157 MIN (X, 5) == 7 -> false */
4160 (cmp (min @0 INTEGER_CST@1) INTEGER_CST@2)
4161 (if (wi::lt_p (wi::to_wide (@1), wi::to_wide (@2),
4162 TYPE_SIGN (TREE_TYPE (@0))))
4163 { constant_boolean_node (cmp == NE_EXPR, type); }
4164 (if (wi::gt_p (wi::to_wide (@1), wi::to_wide (@2),
4165 TYPE_SIGN (TREE_TYPE (@0))))
4169 (cmp (max @0 INTEGER_CST@1) INTEGER_CST@2)
4170 (if (wi::gt_p (wi::to_wide (@1), wi::to_wide (@2),
4171 TYPE_SIGN (TREE_TYPE (@0))))
4172 { constant_boolean_node (cmp == NE_EXPR, type); }
4173 (if (wi::lt_p (wi::to_wide (@1), wi::to_wide (@2),
4174 TYPE_SIGN (TREE_TYPE (@0))))
4177 /* X <= MAX(X, Y) -> true
4178 X > MAX(X, Y) -> false
4179 X >= MIN(X, Y) -> true
4180 X < MIN(X, Y) -> false */
4181 (for minmax (min min max max )
4184 (cmp:c @0 (minmax:c @0 @1))
4185 { constant_boolean_node (cmp == GE_EXPR || cmp == LE_EXPR, type); } ))
4187 /* MIN (X, C1) < C2 -> X < C2 || C1 < C2 */
4188 (for minmax (min min max max min min max max )
4189 cmp (lt le gt ge gt ge lt le )
4190 comb (bit_ior bit_ior bit_ior bit_ior bit_and bit_and bit_and bit_and)
4192 (cmp (minmax @0 INTEGER_CST@1) INTEGER_CST@2)
4193 (comb (cmp @0 @2) (cmp @1 @2))))
4195 /* Undo fancy ways of writing max/min or other ?: expressions, like
4196 a - ((a - b) & -(a < b)) and a - (a - b) * (a < b) into (a < b) ? b : a.
4197 People normally use ?: and that is what we actually try to optimize. */
4198 /* Transform A + (B-A)*cmp into cmp ? B : A. */
4200 (plus:c @0 (mult:c (minus @1 @0) zero_one_valued_p@2))
4201 (if (INTEGRAL_TYPE_P (type)
4202 && (GIMPLE || !TREE_SIDE_EFFECTS (@1)))
4203 (cond (convert:boolean_type_node @2) @1 @0)))
4204 /* Transform A - (A-B)*cmp into cmp ? B : A. */
4206 (minus @0 (mult:c (minus @0 @1) zero_one_valued_p@2))
4207 (if (INTEGRAL_TYPE_P (type)
4208 && (GIMPLE || !TREE_SIDE_EFFECTS (@1)))
4209 (cond (convert:boolean_type_node @2) @1 @0)))
4210 /* Transform A ^ (A^B)*cmp into cmp ? B : A. */
4212 (bit_xor:c @0 (mult:c (bit_xor:c @0 @1) zero_one_valued_p@2))
4213 (if (INTEGRAL_TYPE_P (type)
4214 && (GIMPLE || !TREE_SIDE_EFFECTS (@1)))
4215 (cond (convert:boolean_type_node @2) @1 @0)))
4217 /* (x <= 0 ? -x : 0) -> max(-x, 0). */
4219 (cond (le @0 integer_zerop@1) (negate@2 @0) integer_zerop@1)
4222 /* (zero_one == 0) ? y : z <op> y -> ((typeof(y))zero_one * z) <op> y */
4223 (for op (bit_xor bit_ior plus)
4225 (cond (eq zero_one_valued_p@0
4229 (if (INTEGRAL_TYPE_P (type)
4230 && TYPE_PRECISION (type) > 1
4231 && (INTEGRAL_TYPE_P (TREE_TYPE (@0))))
4232 (op (mult (convert:type @0) @2) @1))))
4234 /* (zero_one != 0) ? z <op> y : y -> ((typeof(y))zero_one * z) <op> y */
4235 (for op (bit_xor bit_ior plus)
4237 (cond (ne zero_one_valued_p@0
4241 (if (INTEGRAL_TYPE_P (type)
4242 && TYPE_PRECISION (type) > 1
4243 && (INTEGRAL_TYPE_P (TREE_TYPE (@0))))
4244 (op (mult (convert:type @0) @2) @1))))
4246 /* ?: Value replacement. */
4247 /* a == 0 ? b : b + a -> b + a */
4248 (for op (plus bit_ior bit_xor)
4250 (cond (eq @0 integer_zerop) @1 (op:c@2 @1 @0))
4252 /* a == 0 ? b : b - a -> b - a */
4253 /* a == 0 ? b : b ptr+ a -> b ptr+ a */
4254 /* a == 0 ? b : b shift/rotate a -> b shift/rotate a */
4255 (for op (lrotate rrotate lshift rshift minus pointer_plus)
4257 (cond (eq @0 integer_zerop) @1 (op@2 @1 @0))
4260 /* a == 1 ? b : b / a -> b / a */
4261 (for op (trunc_div ceil_div floor_div round_div exact_div)
4263 (cond (eq @0 integer_onep) @1 (op@2 @1 @0))
4266 /* a == 1 ? b : a * b -> a * b */
4269 (cond (eq @0 integer_onep) @1 (op:c@2 @1 @0))
4272 /* a == -1 ? b : a & b -> a & b */
4275 (cond (eq @0 integer_all_onesp) @1 (op:c@2 @1 @0))
4278 /* Simplifications of shift and rotates. */
4280 (for rotate (lrotate rrotate)
4282 (rotate integer_all_onesp@0 @1)
4285 /* Optimize -1 >> x for arithmetic right shifts. */
4287 (rshift integer_all_onesp@0 @1)
4288 (if (!TYPE_UNSIGNED (type))
4291 /* Optimize (x >> c) << c into x & (-1<<c). */
4293 (lshift (nop_convert? (rshift @0 INTEGER_CST@1)) @1)
4294 (if (wi::ltu_p (wi::to_wide (@1), element_precision (type)))
4295 /* It doesn't matter if the right shift is arithmetic or logical. */
4296 (bit_and (view_convert @0) (lshift { build_minus_one_cst (type); } @1))))
4299 (lshift (convert (convert@2 (rshift @0 INTEGER_CST@1))) @1)
4300 (if (wi::ltu_p (wi::to_wide (@1), element_precision (type))
4301 /* Allow intermediate conversion to integral type with whatever sign, as
4302 long as the low TYPE_PRECISION (type)
4303 - TYPE_PRECISION (TREE_TYPE (@2)) bits are preserved. */
4304 && INTEGRAL_TYPE_P (type)
4305 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
4306 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
4307 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (@0))
4308 && (TYPE_PRECISION (TREE_TYPE (@2)) >= TYPE_PRECISION (type)
4309 || wi::geu_p (wi::to_wide (@1),
4310 TYPE_PRECISION (type)
4311 - TYPE_PRECISION (TREE_TYPE (@2)))))
4312 (bit_and (convert @0) (lshift { build_minus_one_cst (type); } @1))))
4314 /* For (x << c) >> c, optimize into x & ((unsigned)-1 >> c) for
4315 unsigned x OR truncate into the precision(type) - c lowest bits
4316 of signed x (if they have mode precision or a precision of 1). */
4318 (rshift (nop_convert? (lshift @0 INTEGER_CST@1)) @@1)
4319 (if (wi::ltu_p (wi::to_wide (@1), element_precision (type)))
4320 (if (TYPE_UNSIGNED (type))
4321 (bit_and (convert @0) (rshift { build_minus_one_cst (type); } @1))
4322 (if (INTEGRAL_TYPE_P (type))
4324 int width = element_precision (type) - tree_to_uhwi (@1);
4325 tree stype = NULL_TREE;
4326 if (width <= MAX_FIXED_MODE_SIZE)
4327 stype = build_nonstandard_integer_type (width, 0);
4329 (if (stype && (width == 1 || type_has_mode_precision_p (stype)))
4330 (convert (convert:stype @0))))))))
4332 /* Optimize x >> x into 0 */
4335 { build_zero_cst (type); })
4337 (for shiftrotate (lrotate rrotate lshift rshift)
4339 (shiftrotate @0 integer_zerop)
4342 (shiftrotate integer_zerop@0 @1)
4344 /* Prefer vector1 << scalar to vector1 << vector2
4345 if vector2 is uniform. */
4346 (for vec (VECTOR_CST CONSTRUCTOR)
4348 (shiftrotate @0 vec@1)
4349 (with { tree tem = uniform_vector_p (@1); }
4351 (shiftrotate @0 { tem; }))))))
4353 /* Simplify X << Y where Y's low width bits are 0 to X, as only valid
4354 Y is 0. Similarly for X >> Y. */
4356 (for shift (lshift rshift)
4358 (shift @0 SSA_NAME@1)
4359 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1)))
4361 int width = ceil_log2 (element_precision (TREE_TYPE (@0)));
4362 int prec = TYPE_PRECISION (TREE_TYPE (@1));
4364 (if ((get_nonzero_bits (@1) & wi::mask (width, false, prec)) == 0)
4368 /* Rewrite an LROTATE_EXPR by a constant into an
4369 RROTATE_EXPR by a new constant. */
4371 (lrotate @0 INTEGER_CST@1)
4372 (rrotate @0 { const_binop (MINUS_EXPR, TREE_TYPE (@1),
4373 build_int_cst (TREE_TYPE (@1),
4374 element_precision (type)), @1); }))
4376 /* Turn (a OP c1) OP c2 into a OP (c1+c2). */
4377 (for op (lrotate rrotate rshift lshift)
4379 (op (op @0 INTEGER_CST@1) INTEGER_CST@2)
4380 (with { unsigned int prec = element_precision (type); }
4381 (if (wi::ge_p (wi::to_wide (@1), 0, TYPE_SIGN (TREE_TYPE (@1)))
4382 && wi::lt_p (wi::to_wide (@1), prec, TYPE_SIGN (TREE_TYPE (@1)))
4383 && wi::ge_p (wi::to_wide (@2), 0, TYPE_SIGN (TREE_TYPE (@2)))
4384 && wi::lt_p (wi::to_wide (@2), prec, TYPE_SIGN (TREE_TYPE (@2))))
4385 (with { unsigned int low = (tree_to_uhwi (@1)
4386 + tree_to_uhwi (@2)); }
4387 /* Deal with a OP (c1 + c2) being undefined but (a OP c1) OP c2
4388 being well defined. */
4390 (if (op == LROTATE_EXPR || op == RROTATE_EXPR)
4391 (op @0 { build_int_cst (TREE_TYPE (@1), low % prec); })
4392 (if (TYPE_UNSIGNED (type) || op == LSHIFT_EXPR)
4393 { build_zero_cst (type); }
4394 (op @0 { build_int_cst (TREE_TYPE (@1), prec - 1); })))
4395 (op @0 { build_int_cst (TREE_TYPE (@1), low); })))))))
4398 /* Simplify (CST << x) & 1 to 0 if CST is even or to x == 0 if it is odd. */
4400 (bit_and (lshift INTEGER_CST@1 @0) integer_onep)
4401 (if ((wi::to_wide (@1) & 1) != 0)
4402 (convert (eq:boolean_type_node @0 { build_zero_cst (TREE_TYPE (@0)); }))
4403 { build_zero_cst (type); }))
4405 /* Simplify ((C << x) & D) != 0 where C and D are power of two constants,
4406 either to false if D is smaller (unsigned comparison) than C, or to
4407 x == log2 (D) - log2 (C). Similarly for right shifts.
4408 Note for `(1 >> x)`, the & 1 has been removed so matching that seperately. */
4412 (cmp (bit_and (lshift integer_pow2p@1 @0) integer_pow2p@2) integer_zerop)
4413 (with { int c1 = wi::clz (wi::to_wide (@1));
4414 int c2 = wi::clz (wi::to_wide (@2)); }
4416 { constant_boolean_node (cmp == NE_EXPR ? false : true, type); }
4417 (icmp @0 { build_int_cst (TREE_TYPE (@0), c1 - c2); }))))
4419 (cmp (bit_and (rshift integer_pow2p@1 @0) integer_pow2p@2) integer_zerop)
4420 (if (tree_int_cst_sgn (@1) > 0)
4421 (with { int c1 = wi::clz (wi::to_wide (@1));
4422 int c2 = wi::clz (wi::to_wide (@2)); }
4424 { constant_boolean_node (cmp == NE_EXPR ? false : true, type); }
4425 (icmp @0 { build_int_cst (TREE_TYPE (@0), c2 - c1); })))))
4426 /* `(1 >> X) != 0` -> `X == 0` */
4427 /* `(1 >> X) == 0` -> `X != 0` */
4429 (cmp (rshift integer_onep@1 @0) integer_zerop)
4430 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1)))
4431 (icmp @0 { build_zero_cst (TREE_TYPE (@0)); }))))
4433 /* (CST1 << A) == CST2 -> A == ctz (CST2) - ctz (CST1)
4434 (CST1 << A) != CST2 -> A != ctz (CST2) - ctz (CST1)
4438 (cmp (lshift INTEGER_CST@0 @1) INTEGER_CST@2)
4439 (with { int cand = wi::ctz (wi::to_wide (@2)) - wi::ctz (wi::to_wide (@0)); }
4441 || (!integer_zerop (@2)
4442 && wi::lshift (wi::to_wide (@0), cand) != wi::to_wide (@2)))
4443 { constant_boolean_node (cmp == NE_EXPR, type); }
4444 (if (!integer_zerop (@2)
4445 && wi::lshift (wi::to_wide (@0), cand) == wi::to_wide (@2))
4446 (cmp @1 { build_int_cst (TREE_TYPE (@1), cand); }))))))
4448 /* Fold ((X << C1) & C2) cmp C3 into (X & (C2 >> C1)) cmp (C3 >> C1)
4449 ((X >> C1) & C2) cmp C3 into (X & (C2 << C1)) cmp (C3 << C1). */
4452 (cmp (bit_and:s (lshift:s @0 INTEGER_CST@1) INTEGER_CST@2) INTEGER_CST@3)
4453 (if (tree_fits_shwi_p (@1)
4454 && tree_to_shwi (@1) > 0
4455 && tree_to_shwi (@1) < TYPE_PRECISION (TREE_TYPE (@0)))
4456 (if (tree_to_shwi (@1) > wi::ctz (wi::to_wide (@3)))
4457 { constant_boolean_node (cmp == NE_EXPR, type); }
4458 (with { wide_int c1 = wi::to_wide (@1);
4459 wide_int c2 = wi::lrshift (wi::to_wide (@2), c1);
4460 wide_int c3 = wi::lrshift (wi::to_wide (@3), c1); }
4461 (cmp (bit_and @0 { wide_int_to_tree (TREE_TYPE (@0), c2); })
4462 { wide_int_to_tree (TREE_TYPE (@0), c3); })))))
4464 (cmp (bit_and:s (rshift:s @0 INTEGER_CST@1) INTEGER_CST@2) INTEGER_CST@3)
4465 (if (tree_fits_shwi_p (@1)
4466 && tree_to_shwi (@1) > 0
4467 && tree_to_shwi (@1) < TYPE_PRECISION (TREE_TYPE (@0)))
4468 (with { tree t0 = TREE_TYPE (@0);
4469 unsigned int prec = TYPE_PRECISION (t0);
4470 wide_int c1 = wi::to_wide (@1);
4471 wide_int c2 = wi::to_wide (@2);
4472 wide_int c3 = wi::to_wide (@3);
4473 wide_int sb = wi::set_bit_in_zero (prec - 1, prec); }
4474 (if ((c2 & c3) != c3)
4475 { constant_boolean_node (cmp == NE_EXPR, type); }
4476 (if (TYPE_UNSIGNED (t0))
4477 (if ((c3 & wi::arshift (sb, c1 - 1)) != 0)
4478 { constant_boolean_node (cmp == NE_EXPR, type); }
4479 (cmp (bit_and @0 { wide_int_to_tree (t0, c2 << c1); })
4480 { wide_int_to_tree (t0, c3 << c1); }))
4481 (with { wide_int smask = wi::arshift (sb, c1); }
4483 (if ((c2 & smask) == 0)
4484 (cmp (bit_and @0 { wide_int_to_tree (t0, c2 << c1); })
4485 { wide_int_to_tree (t0, c3 << c1); }))
4486 (if ((c3 & smask) == 0)
4487 (cmp (bit_and @0 { wide_int_to_tree (t0, (c2 << c1) | sb); })
4488 { wide_int_to_tree (t0, c3 << c1); }))
4489 (if ((c2 & smask) != (c3 & smask))
4490 { constant_boolean_node (cmp == NE_EXPR, type); })
4491 (cmp (bit_and @0 { wide_int_to_tree (t0, (c2 << c1) | sb); })
4492 { wide_int_to_tree (t0, (c3 << c1) | sb); })))))))))
4494 /* Fold (X << C1) & C2 into (X << C1) & (C2 | ((1 << C1) - 1))
4495 (X >> C1) & C2 into (X >> C1) & (C2 | ~((type) -1 >> C1))
4496 if the new mask might be further optimized. */
4497 (for shift (lshift rshift)
4499 (bit_and (convert?:s@4 (shift:s@5 (convert1?@3 @0) INTEGER_CST@1))
4501 (if (tree_nop_conversion_p (TREE_TYPE (@4), TREE_TYPE (@5))
4502 && TYPE_PRECISION (type) <= HOST_BITS_PER_WIDE_INT
4503 && tree_fits_uhwi_p (@1)
4504 && tree_to_uhwi (@1) > 0
4505 && tree_to_uhwi (@1) < TYPE_PRECISION (type))
4508 unsigned int shiftc = tree_to_uhwi (@1);
4509 unsigned HOST_WIDE_INT mask = TREE_INT_CST_LOW (@2);
4510 unsigned HOST_WIDE_INT newmask, zerobits = 0;
4511 tree shift_type = TREE_TYPE (@3);
4514 if (shift == LSHIFT_EXPR)
4515 zerobits = ((HOST_WIDE_INT_1U << shiftc) - 1);
4516 else if (shift == RSHIFT_EXPR
4517 && type_has_mode_precision_p (shift_type))
4519 prec = TYPE_PRECISION (TREE_TYPE (@3));
4521 /* See if more bits can be proven as zero because of
4524 && TYPE_UNSIGNED (TREE_TYPE (@0)))
4526 tree inner_type = TREE_TYPE (@0);
4527 if (type_has_mode_precision_p (inner_type)
4528 && TYPE_PRECISION (inner_type) < prec)
4530 prec = TYPE_PRECISION (inner_type);
4531 /* See if we can shorten the right shift. */
4533 shift_type = inner_type;
4534 /* Otherwise X >> C1 is all zeros, so we'll optimize
4535 it into (X, 0) later on by making sure zerobits
4539 zerobits = HOST_WIDE_INT_M1U;
4542 zerobits >>= HOST_BITS_PER_WIDE_INT - shiftc;
4543 zerobits <<= prec - shiftc;
4545 /* For arithmetic shift if sign bit could be set, zerobits
4546 can contain actually sign bits, so no transformation is
4547 possible, unless MASK masks them all away. In that
4548 case the shift needs to be converted into logical shift. */
4549 if (!TYPE_UNSIGNED (TREE_TYPE (@3))
4550 && prec == TYPE_PRECISION (TREE_TYPE (@3)))
4552 if ((mask & zerobits) == 0)
4553 shift_type = unsigned_type_for (TREE_TYPE (@3));
4559 /* ((X << 16) & 0xff00) is (X, 0). */
4560 (if ((mask & zerobits) == mask)
4561 { build_int_cst (type, 0); }
4562 (with { newmask = mask | zerobits; }
4563 (if (newmask != mask && (newmask & (newmask + 1)) == 0)
4566 /* Only do the transformation if NEWMASK is some integer
4568 for (prec = BITS_PER_UNIT;
4569 prec < HOST_BITS_PER_WIDE_INT; prec <<= 1)
4570 if (newmask == (HOST_WIDE_INT_1U << prec) - 1)
4573 (if (prec < HOST_BITS_PER_WIDE_INT
4574 || newmask == HOST_WIDE_INT_M1U)
4576 { tree newmaskt = build_int_cst_type (TREE_TYPE (@2), newmask); }
4577 (if (!tree_int_cst_equal (newmaskt, @2))
4578 (if (shift_type != TREE_TYPE (@3))
4579 (bit_and (convert (shift:shift_type (convert @3) @1)) { newmaskt; })
4580 (bit_and @4 { newmaskt; })))))))))))))
4582 /* ((1 << n) & M) != 0 -> n == log2 (M) */
4588 (nop_convert? (lshift integer_onep @0)) integer_pow2p@1) integer_zerop)
4589 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)))
4590 (icmp @0 { wide_int_to_tree (TREE_TYPE (@0),
4591 wi::exact_log2 (wi::to_wide (@1))); }))))
4593 /* Fold (X {&,^,|} C2) << C1 into (X << C1) {&,^,|} (C2 << C1)
4594 (X {&,^,|} C2) >> C1 into (X >> C1) & (C2 >> C1). */
4595 (for shift (lshift rshift)
4596 (for bit_op (bit_and bit_xor bit_ior)
4598 (shift (convert?:s (bit_op:s @0 INTEGER_CST@2)) INTEGER_CST@1)
4599 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
4600 (with { tree mask = int_const_binop (shift, fold_convert (type, @2), @1); }
4602 (bit_op (shift (convert @0) @1) { mask; })))))))
4604 /* ~(~X >> Y) -> X >> Y (for arithmetic shift). */
4606 (bit_not (convert1?:s (rshift:s (convert2?@0 (bit_not @1)) @2)))
4607 (if (!TYPE_UNSIGNED (TREE_TYPE (@0))
4608 && (element_precision (TREE_TYPE (@0))
4609 <= element_precision (TREE_TYPE (@1))
4610 || !TYPE_UNSIGNED (TREE_TYPE (@1))))
4612 { tree shift_type = TREE_TYPE (@0); }
4613 (convert (rshift (convert:shift_type @1) @2)))))
4615 /* ~(~X >>r Y) -> X >>r Y
4616 ~(~X <<r Y) -> X <<r Y */
4617 (for rotate (lrotate rrotate)
4619 (bit_not (convert1?:s (rotate:s (convert2?@0 (bit_not @1)) @2)))
4620 (if ((element_precision (TREE_TYPE (@0))
4621 <= element_precision (TREE_TYPE (@1))
4622 || !TYPE_UNSIGNED (TREE_TYPE (@1)))
4623 && (element_precision (type) <= element_precision (TREE_TYPE (@0))
4624 || !TYPE_UNSIGNED (TREE_TYPE (@0))))
4626 { tree rotate_type = TREE_TYPE (@0); }
4627 (convert (rotate (convert:rotate_type @1) @2))))))
4630 (for rotate (lrotate rrotate)
4631 invrot (rrotate lrotate)
4632 /* (X >>r Y) cmp (Z >>r Y) may simplify to X cmp Y. */
4634 (cmp (rotate @1 @0) (rotate @2 @0))
4636 /* (X >>r C1) cmp C2 may simplify to X cmp C3. */
4638 (cmp (rotate @0 INTEGER_CST@1) INTEGER_CST@2)
4639 (cmp @0 { const_binop (invrot, TREE_TYPE (@0), @2, @1); }))
4640 /* (X >>r Y) cmp C where C is 0 or ~0, may simplify to X cmp C. */
4642 (cmp (rotate @0 @1) INTEGER_CST@2)
4643 (if (integer_zerop (@2) || integer_all_onesp (@2))
4646 /* Narrow a lshift by constant. */
4648 (convert (lshift:s@0 @1 INTEGER_CST@2))
4649 (if (INTEGRAL_TYPE_P (type)
4650 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
4651 && !integer_zerop (@2)
4652 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0)))
4653 (if (TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (@0))
4654 || wi::ltu_p (wi::to_wide (@2), TYPE_PRECISION (type)))
4655 (lshift (convert @1) @2)
4656 (if (wi::ltu_p (wi::to_wide (@2), TYPE_PRECISION (TREE_TYPE (@0))))
4657 { build_zero_cst (type); }))))
4659 /* Simplifications of conversions. */
4661 /* Basic strip-useless-type-conversions / strip_nops. */
4662 (for cvt (convert view_convert float fix_trunc)
4665 (if ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@0)))
4666 || (GENERIC && type == TREE_TYPE (@0)))
4669 /* Contract view-conversions. */
4671 (view_convert (view_convert @0))
4674 /* For integral conversions with the same precision or pointer
4675 conversions use a NOP_EXPR instead. */
4678 (if ((INTEGRAL_TYPE_P (type) || POINTER_TYPE_P (type))
4679 && (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
4680 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (@0)))
4683 /* Strip inner integral conversions that do not change precision or size, or
4684 zero-extend while keeping the same size (for bool-to-char). */
4686 (view_convert (convert@0 @1))
4687 (if ((INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
4688 && (INTEGRAL_TYPE_P (TREE_TYPE (@1)) || POINTER_TYPE_P (TREE_TYPE (@1)))
4689 && TYPE_SIZE (TREE_TYPE (@0)) == TYPE_SIZE (TREE_TYPE (@1))
4690 && (TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1))
4691 || (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (TREE_TYPE (@1))
4692 && TYPE_UNSIGNED (TREE_TYPE (@1)))))
4695 /* Simplify a view-converted empty or single-element constructor. */
4697 (view_convert CONSTRUCTOR@0)
4699 { tree ctor = (TREE_CODE (@0) == SSA_NAME
4700 ? gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0)) : @0); }
4702 (if (CONSTRUCTOR_NELTS (ctor) == 0)
4703 { build_zero_cst (type); })
4704 (if (CONSTRUCTOR_NELTS (ctor) == 1
4705 && VECTOR_TYPE_P (TREE_TYPE (ctor))
4706 && operand_equal_p (TYPE_SIZE (type),
4707 TYPE_SIZE (TREE_TYPE
4708 (CONSTRUCTOR_ELT (ctor, 0)->value))))
4709 (view_convert { CONSTRUCTOR_ELT (ctor, 0)->value; })))))
4711 /* Re-association barriers around constants and other re-association
4712 barriers can be removed. */
4714 (paren CONSTANT_CLASS_P@0)
4717 (paren (paren@1 @0))
4720 /* Handle cases of two conversions in a row. */
4721 (for ocvt (convert float fix_trunc)
4722 (for icvt (convert float)
4727 tree inside_type = TREE_TYPE (@0);
4728 tree inter_type = TREE_TYPE (@1);
4729 int inside_int = INTEGRAL_TYPE_P (inside_type);
4730 int inside_ptr = POINTER_TYPE_P (inside_type);
4731 int inside_float = FLOAT_TYPE_P (inside_type);
4732 int inside_vec = VECTOR_TYPE_P (inside_type);
4733 unsigned int inside_prec = element_precision (inside_type);
4734 int inside_unsignedp = TYPE_UNSIGNED (inside_type);
4735 int inter_int = INTEGRAL_TYPE_P (inter_type);
4736 int inter_ptr = POINTER_TYPE_P (inter_type);
4737 int inter_float = FLOAT_TYPE_P (inter_type);
4738 int inter_vec = VECTOR_TYPE_P (inter_type);
4739 unsigned int inter_prec = element_precision (inter_type);
4740 int inter_unsignedp = TYPE_UNSIGNED (inter_type);
4741 int final_int = INTEGRAL_TYPE_P (type);
4742 int final_ptr = POINTER_TYPE_P (type);
4743 int final_float = FLOAT_TYPE_P (type);
4744 int final_vec = VECTOR_TYPE_P (type);
4745 unsigned int final_prec = element_precision (type);
4746 int final_unsignedp = TYPE_UNSIGNED (type);
4749 /* In addition to the cases of two conversions in a row
4750 handled below, if we are converting something to its own
4751 type via an object of identical or wider precision, neither
4752 conversion is needed. */
4753 (if (((GIMPLE && useless_type_conversion_p (type, inside_type))
4755 && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (inside_type)))
4756 && (((inter_int || inter_ptr) && final_int)
4757 || (inter_float && final_float))
4758 && inter_prec >= final_prec)
4761 /* Likewise, if the intermediate and initial types are either both
4762 float or both integer, we don't need the middle conversion if the
4763 former is wider than the latter and doesn't change the signedness
4764 (for integers). Avoid this if the final type is a pointer since
4765 then we sometimes need the middle conversion. */
4766 (if (((inter_int && inside_int) || (inter_float && inside_float))
4767 && (final_int || final_float)
4768 && inter_prec >= inside_prec
4769 && (inter_float || inter_unsignedp == inside_unsignedp))
4772 /* If we have a sign-extension of a zero-extended value, we can
4773 replace that by a single zero-extension. Likewise if the
4774 final conversion does not change precision we can drop the
4775 intermediate conversion. Similarly truncation of a sign-extension
4776 can be replaced by a single sign-extension. */
4777 (if (inside_int && inter_int && final_int
4778 && ((inside_prec < inter_prec && inter_prec < final_prec
4779 && inside_unsignedp && !inter_unsignedp)
4780 || final_prec == inter_prec
4781 || (inside_prec < inter_prec && inter_prec > final_prec
4782 && !inside_unsignedp && inter_unsignedp)))
4785 /* Two conversions in a row are not needed unless:
4786 - some conversion is floating-point (overstrict for now), or
4787 - some conversion is a vector (overstrict for now), or
4788 - the intermediate type is narrower than both initial and
4790 - the intermediate type and innermost type differ in signedness,
4791 and the outermost type is wider than the intermediate, or
4792 - the initial type is a pointer type and the precisions of the
4793 intermediate and final types differ, or
4794 - the final type is a pointer type and the precisions of the
4795 initial and intermediate types differ. */
4796 (if (! inside_float && ! inter_float && ! final_float
4797 && ! inside_vec && ! inter_vec && ! final_vec
4798 && (inter_prec >= inside_prec || inter_prec >= final_prec)
4799 && ! (inside_int && inter_int
4800 && inter_unsignedp != inside_unsignedp
4801 && inter_prec < final_prec)
4802 && ((inter_unsignedp && inter_prec > inside_prec)
4803 == (final_unsignedp && final_prec > inter_prec))
4804 && ! (inside_ptr && inter_prec != final_prec)
4805 && ! (final_ptr && inside_prec != inter_prec))
4808 /* `(outer:M)(inter:N) a:O`
4809 can be converted to `(outer:M) a`
4810 if M <= O && N >= O. No matter what signedness of the casts,
4811 as the final is either a truncation from the original or just
4812 a sign change of the type. */
4813 (if (inside_int && inter_int && final_int
4814 && final_prec <= inside_prec
4815 && inter_prec >= inside_prec)
4818 /* A truncation to an unsigned type (a zero-extension) should be
4819 canonicalized as bitwise and of a mask. */
4820 (if (GIMPLE /* PR70366: doing this in GENERIC breaks -Wconversion. */
4821 && final_int && inter_int && inside_int
4822 && final_prec == inside_prec
4823 && final_prec > inter_prec
4825 (convert (bit_and @0 { wide_int_to_tree
4827 wi::mask (inter_prec, false,
4828 TYPE_PRECISION (inside_type))); })))
4830 /* If we are converting an integer to a floating-point that can
4831 represent it exactly and back to an integer, we can skip the
4832 floating-point conversion. */
4833 (if (GIMPLE /* PR66211 */
4834 && inside_int && inter_float && final_int &&
4835 (unsigned) significand_size (TYPE_MODE (inter_type))
4836 >= inside_prec - !inside_unsignedp)
4839 /* (float_type)(integer_type) x -> trunc (x) if the type of x matches
4840 float_type. Only do the transformation if we do not need to preserve
4841 trapping behaviour, so require !flag_trapping_math. */
4844 (float (fix_trunc @0))
4845 (if (!flag_trapping_math
4846 && types_match (type, TREE_TYPE (@0))
4847 && direct_internal_fn_supported_p (IFN_TRUNC, type,
4852 /* If we have a narrowing conversion to an integral type that is fed by a
4853 BIT_AND_EXPR, we might be able to remove the BIT_AND_EXPR if it merely
4854 masks off bits outside the final type (and nothing else). */
4856 (convert (bit_and @0 INTEGER_CST@1))
4857 (if (INTEGRAL_TYPE_P (type)
4858 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
4859 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))
4860 && operand_equal_p (@1, build_low_bits_mask (TREE_TYPE (@1),
4861 TYPE_PRECISION (type)), 0))
4865 /* (X /[ex] A) * A -> X. */
4867 (mult (convert1? (exact_div @0 @@1)) (convert2? @1))
4870 /* Simplify (A / B) * B + (A % B) -> A. */
4871 (for div (trunc_div ceil_div floor_div round_div)
4872 mod (trunc_mod ceil_mod floor_mod round_mod)
4874 (plus:c (mult:c (div @0 @1) @1) (mod @0 @1))
4877 /* x / y * y == x -> x % y == 0. */
4879 (eq:c (mult:c (trunc_div:s @0 @1) @1) @0)
4880 (if (TREE_CODE (TREE_TYPE (@0)) != COMPLEX_TYPE)
4881 (eq (trunc_mod @0 @1) { build_zero_cst (TREE_TYPE (@0)); })))
4883 /* ((X /[ex] A) +- B) * A --> X +- A * B. */
4884 (for op (plus minus)
4886 (mult (convert1? (op (convert2? (exact_div @0 INTEGER_CST@@1)) INTEGER_CST@2)) @1)
4887 (if (tree_nop_conversion_p (type, TREE_TYPE (@2))
4888 && tree_nop_conversion_p (TREE_TYPE (@0), TREE_TYPE (@2)))
4891 wi::overflow_type overflow;
4892 wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@2),
4893 TYPE_SIGN (type), &overflow);
4895 (if (types_match (type, TREE_TYPE (@2))
4896 && types_match (TREE_TYPE (@0), TREE_TYPE (@2)) && !overflow)
4897 (op @0 { wide_int_to_tree (type, mul); })
4898 (with { tree utype = unsigned_type_for (type); }
4899 (convert (op (convert:utype @0)
4900 (mult (convert:utype @1) (convert:utype @2))))))))))
4902 /* Canonicalization of binary operations. */
4904 /* Convert X + -C into X - C. */
4906 (plus @0 REAL_CST@1)
4907 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
4908 (with { tree tem = const_unop (NEGATE_EXPR, type, @1); }
4909 (if (!TREE_OVERFLOW (tem) || !flag_trapping_math)
4910 (minus @0 { tem; })))))
4912 /* Convert x+x into x*2. */
4915 (if (SCALAR_FLOAT_TYPE_P (type))
4916 (mult @0 { build_real (type, dconst2); })
4917 (if (INTEGRAL_TYPE_P (type))
4918 (mult @0 { build_int_cst (type, 2); }))))
4922 (minus integer_zerop @1)
4925 (pointer_diff integer_zerop @1)
4926 (negate (convert @1)))
4928 /* (ARG0 - ARG1) is the same as (-ARG1 + ARG0). So check whether
4929 ARG0 is zero and X + ARG0 reduces to X, since that would mean
4930 (-ARG1 + ARG0) reduces to -ARG1. */
4932 (minus real_zerop@0 @1)
4933 (if (fold_real_zero_addition_p (type, @1, @0, 0))
4936 /* Transform x * -1 into -x. */
4938 (mult @0 integer_minus_onep)
4941 /* Reassociate (X * CST) * Y to (X * Y) * CST. This does not introduce
4942 signed overflow for CST != 0 && CST != -1. */
4944 (mult:c (mult:s@3 @0 INTEGER_CST@1) @2)
4945 (if (TREE_CODE (@2) != INTEGER_CST
4947 && !integer_zerop (@1) && !integer_minus_onep (@1))
4948 (mult (mult @0 @2) @1)))
4950 /* True if we can easily extract the real and imaginary parts of a complex
4952 (match compositional_complex
4953 (convert? (complex @0 @1)))
4955 /* COMPLEX_EXPR and REALPART/IMAGPART_EXPR cancellations. */
4957 (complex (realpart @0) (imagpart @0))
4960 (realpart (complex @0 @1))
4963 (imagpart (complex @0 @1))
4966 /* Sometimes we only care about half of a complex expression. */
4968 (realpart (convert?:s (conj:s @0)))
4969 (convert (realpart @0)))
4971 (imagpart (convert?:s (conj:s @0)))
4972 (convert (negate (imagpart @0))))
4973 (for part (realpart imagpart)
4974 (for op (plus minus)
4976 (part (convert?:s@2 (op:s @0 @1)))
4977 (convert (op (part @0) (part @1))))))
4979 (realpart (convert?:s (CEXPI:s @0)))
4982 (imagpart (convert?:s (CEXPI:s @0)))
4985 /* conj(conj(x)) -> x */
4987 (conj (convert? (conj @0)))
4988 (if (tree_nop_conversion_p (TREE_TYPE (@0), type))
4991 /* conj({x,y}) -> {x,-y} */
4993 (conj (convert?:s (complex:s @0 @1)))
4994 (with { tree itype = TREE_TYPE (type); }
4995 (complex (convert:itype @0) (negate (convert:itype @1)))))
4997 /* BSWAP simplifications, transforms checked by gcc.dg/builtin-bswap-8.c. */
5003 (bswap (bit_not (bswap @0)))
5005 (for bitop (bit_xor bit_ior bit_and)
5007 (bswap (bitop:c (bswap @0) @1))
5008 (bitop @0 (bswap @1))))
5011 (cmp (bswap@2 @0) (bswap @1))
5012 (with { tree ctype = TREE_TYPE (@2); }
5013 (cmp (convert:ctype @0) (convert:ctype @1))))
5015 (cmp (bswap @0) INTEGER_CST@1)
5016 (with { tree ctype = TREE_TYPE (@1); }
5017 (cmp (convert:ctype @0) (bswap! @1)))))
5018 /* (bswap(x) >> C1) & C2 can sometimes be simplified to (x >> C3) & C2. */
5020 (bit_and (convert1? (rshift@0 (convert2? (bswap@4 @1)) INTEGER_CST@2))
5022 (if (BITS_PER_UNIT == 8
5023 && tree_fits_uhwi_p (@2)
5024 && tree_fits_uhwi_p (@3))
5027 unsigned HOST_WIDE_INT prec = TYPE_PRECISION (TREE_TYPE (@4));
5028 unsigned HOST_WIDE_INT bits = tree_to_uhwi (@2);
5029 unsigned HOST_WIDE_INT mask = tree_to_uhwi (@3);
5030 unsigned HOST_WIDE_INT lo = bits & 7;
5031 unsigned HOST_WIDE_INT hi = bits - lo;
5034 && mask < (256u>>lo)
5035 && bits < TYPE_PRECISION (TREE_TYPE(@0)))
5036 (with { unsigned HOST_WIDE_INT ns = (prec - (hi + 8)) + lo; }
5038 (bit_and (convert @1) @3)
5041 tree utype = unsigned_type_for (TREE_TYPE (@1));
5042 tree nst = build_int_cst (integer_type_node, ns);
5044 (bit_and (convert (rshift:utype (convert:utype @1) {nst;})) @3))))))))
5045 /* bswap(x) >> C1 can sometimes be simplified to (T)x >> C2. */
5047 (rshift (convert? (bswap@2 @0)) INTEGER_CST@1)
5048 (if (BITS_PER_UNIT == 8
5049 && CHAR_TYPE_SIZE == 8
5050 && tree_fits_uhwi_p (@1))
5053 unsigned HOST_WIDE_INT prec = TYPE_PRECISION (TREE_TYPE (@2));
5054 unsigned HOST_WIDE_INT bits = tree_to_uhwi (@1);
5055 /* If the bswap was extended before the original shift, this
5056 byte (shift) has the sign of the extension, not the sign of
5057 the original shift. */
5058 tree st = TYPE_PRECISION (type) > prec ? TREE_TYPE (@2) : type;
5060 /* Special case: logical right shift of sign-extended bswap.
5061 (unsigned)(short)bswap16(x)>>12 is (unsigned)((short)x<<8)>>12. */
5062 (if (TYPE_PRECISION (type) > prec
5063 && !TYPE_UNSIGNED (TREE_TYPE (@2))
5064 && TYPE_UNSIGNED (type)
5065 && bits < prec && bits + 8 >= prec)
5066 (with { tree nst = build_int_cst (integer_type_node, prec - 8); }
5067 (rshift (convert (lshift:st (convert:st @0) {nst;})) @1))
5068 (if (bits + 8 == prec)
5069 (if (TYPE_UNSIGNED (st))
5070 (convert (convert:unsigned_char_type_node @0))
5071 (convert (convert:signed_char_type_node @0)))
5072 (if (bits < prec && bits + 8 > prec)
5075 tree nst = build_int_cst (integer_type_node, bits & 7);
5076 tree bt = TYPE_UNSIGNED (st) ? unsigned_char_type_node
5077 : signed_char_type_node;
5079 (convert (rshift:bt (convert:bt @0) {nst;})))))))))
5080 /* bswap(x) & C1 can sometimes be simplified to (x >> C2) & C1. */
5082 (bit_and (convert? (bswap@2 @0)) INTEGER_CST@1)
5083 (if (BITS_PER_UNIT == 8
5084 && tree_fits_uhwi_p (@1)
5085 && tree_to_uhwi (@1) < 256)
5088 unsigned HOST_WIDE_INT prec = TYPE_PRECISION (TREE_TYPE (@2));
5089 tree utype = unsigned_type_for (TREE_TYPE (@0));
5090 tree nst = build_int_cst (integer_type_node, prec - 8);
5092 (bit_and (convert (rshift:utype (convert:utype @0) {nst;})) @1)))))
5095 /* Combine COND_EXPRs and VEC_COND_EXPRs. */
5097 /* Simplify constant conditions.
5098 Only optimize constant conditions when the selected branch
5099 has the same type as the COND_EXPR. This avoids optimizing
5100 away "c ? x : throw", where the throw has a void type.
5101 Note that we cannot throw away the fold-const.cc variant nor
5102 this one as we depend on doing this transform before possibly
5103 A ? B : B -> B triggers and the fold-const.cc one can optimize
5104 0 ? A : B to B even if A has side-effects. Something
5105 genmatch cannot handle. */
5107 (cond INTEGER_CST@0 @1 @2)
5108 (if (integer_zerop (@0))
5109 (if (!VOID_TYPE_P (TREE_TYPE (@2)) || VOID_TYPE_P (type))
5111 (if (!VOID_TYPE_P (TREE_TYPE (@1)) || VOID_TYPE_P (type))
5114 (vec_cond VECTOR_CST@0 @1 @2)
5115 (if (integer_all_onesp (@0))
5117 (if (integer_zerop (@0))
5120 /* Sink unary operations to branches, but only if we do fold both. */
5121 (for op (negate bit_not abs absu)
5123 (op (vec_cond:s @0 @1 @2))
5124 (vec_cond @0 (op! @1) (op! @2))))
5126 /* Sink unary conversions to branches, but only if we do fold both
5127 and the target's truth type is the same as we already have. */
5129 (convert (vec_cond:s @0 @1 @2))
5130 (if (VECTOR_TYPE_P (type)
5131 && types_match (TREE_TYPE (@0), truth_type_for (type)))
5132 (vec_cond @0 (convert! @1) (convert! @2))))
5134 /* Likewise for view_convert of nop_conversions. */
5136 (view_convert (vec_cond:s @0 @1 @2))
5137 (if (VECTOR_TYPE_P (type) && VECTOR_TYPE_P (TREE_TYPE (@1))
5138 && known_eq (TYPE_VECTOR_SUBPARTS (type),
5139 TYPE_VECTOR_SUBPARTS (TREE_TYPE (@1)))
5140 && tree_nop_conversion_p (TREE_TYPE (type), TREE_TYPE (TREE_TYPE (@1))))
5141 (vec_cond @0 (view_convert! @1) (view_convert! @2))))
5143 /* Sink binary operation to branches, but only if we can fold it. */
5144 (for op (tcc_comparison plus minus mult bit_and bit_ior bit_xor
5145 lshift rshift rdiv trunc_div ceil_div floor_div round_div
5146 trunc_mod ceil_mod floor_mod round_mod min max)
5147 /* (c ? a : b) op (c ? d : e) --> c ? (a op d) : (b op e) */
5149 (op (vec_cond:s @0 @1 @2) (vec_cond:s @0 @3 @4))
5150 (vec_cond @0 (op! @1 @3) (op! @2 @4)))
5152 /* (c ? a : b) op d --> c ? (a op d) : (b op d) */
5154 (op (vec_cond:s @0 @1 @2) @3)
5155 (vec_cond @0 (op! @1 @3) (op! @2 @3)))
5157 (op @3 (vec_cond:s @0 @1 @2))
5158 (vec_cond @0 (op! @3 @1) (op! @3 @2))))
5161 (match (nop_atomic_bit_test_and_p @0 @1 @4)
5162 (bit_and (convert?@4 (ATOMIC_FETCH_OR_XOR_N @2 INTEGER_CST@0 @3))
5165 int ibit = tree_log2 (@0);
5166 int ibit2 = tree_log2 (@1);
5170 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))))))
5172 (match (nop_atomic_bit_test_and_p @0 @1 @3)
5173 (bit_and (convert?@3 (SYNC_FETCH_OR_XOR_N @2 INTEGER_CST@0))
5176 int ibit = tree_log2 (@0);
5177 int ibit2 = tree_log2 (@1);
5181 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))))))
5183 (match (nop_atomic_bit_test_and_p @0 @0 @4)
5186 (ATOMIC_FETCH_OR_XOR_N @2 (nop_convert? (lshift@0 integer_onep@5 @6)) @3))
5188 (if (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0)))))
5190 (match (nop_atomic_bit_test_and_p @0 @0 @4)
5193 (SYNC_FETCH_OR_XOR_N @2 (nop_convert? (lshift@0 integer_onep@3 @5))))
5195 (if (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0)))))
5197 (match (nop_atomic_bit_test_and_p @0 @1 @3)
5198 (bit_and@4 (convert?@3 (ATOMIC_FETCH_AND_N @2 INTEGER_CST@0 @5))
5201 int ibit = wi::exact_log2 (wi::zext (wi::bit_not (wi::to_wide (@0)),
5202 TYPE_PRECISION(type)));
5203 int ibit2 = tree_log2 (@1);
5207 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))))))
5209 (match (nop_atomic_bit_test_and_p @0 @1 @3)
5211 (convert?@3 (SYNC_FETCH_AND_AND_N @2 INTEGER_CST@0))
5214 int ibit = wi::exact_log2 (wi::zext (wi::bit_not (wi::to_wide (@0)),
5215 TYPE_PRECISION(type)));
5216 int ibit2 = tree_log2 (@1);
5220 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))))))
5222 (match (nop_atomic_bit_test_and_p @4 @0 @3)
5225 (ATOMIC_FETCH_AND_N @2 (nop_convert?@4 (bit_not (lshift@0 integer_onep@6 @7))) @5))
5227 (if (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@4)))))
5229 (match (nop_atomic_bit_test_and_p @4 @0 @3)
5232 (SYNC_FETCH_AND_AND_N @2 (nop_convert?@4 (bit_not (lshift@0 integer_onep@6 @7)))))
5234 (if (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@4)))))
5238 /* (v ? w : 0) ? a : b is just (v & w) ? a : b
5239 Currently disabled after pass lvec because ARM understands
5240 VEC_COND_EXPR<v==w,-1,0> but not a plain v==w fed to BIT_IOR_EXPR. */
5242 /* These can only be done in gimple as fold likes to convert:
5243 (CMP) & N into (CMP) ? N : 0
5244 and we try to match the same pattern again and again. */
5246 (vec_cond (vec_cond:s @0 @3 integer_zerop) @1 @2)
5247 (if (optimize_vectors_before_lowering_p () && types_match (@0, @3))
5248 (vec_cond (bit_and @0 @3) @1 @2)))
5250 (vec_cond (vec_cond:s @0 integer_all_onesp @3) @1 @2)
5251 (if (optimize_vectors_before_lowering_p () && types_match (@0, @3))
5252 (vec_cond (bit_ior @0 @3) @1 @2)))
5254 (vec_cond (vec_cond:s @0 integer_zerop @3) @1 @2)
5255 (if (optimize_vectors_before_lowering_p () && types_match (@0, @3))
5256 (vec_cond (bit_ior @0 (bit_not @3)) @2 @1)))
5258 (vec_cond (vec_cond:s @0 @3 integer_all_onesp) @1 @2)
5259 (if (optimize_vectors_before_lowering_p () && types_match (@0, @3))
5260 (vec_cond (bit_and @0 (bit_not @3)) @2 @1)))
5262 /* ((VCE (a cmp b ? -1 : 0)) < 0) ? c : d is just
5263 (VCE ((a cmp b) ? (VCE c) : (VCE d))) when TYPE_PRECISION of the
5264 component type of the outer vec_cond is greater equal the inner one. */
5265 (for cmp (simple_comparison)
5268 (lt (view_convert@5 (vec_cond@6 (cmp@4 @0 @1)
5271 integer_zerop) @2 @3)
5272 (if (VECTOR_INTEGER_TYPE_P (TREE_TYPE (@0))
5273 && VECTOR_INTEGER_TYPE_P (TREE_TYPE (@5))
5274 && !TYPE_UNSIGNED (TREE_TYPE (@5))
5275 && VECTOR_TYPE_P (TREE_TYPE (@6))
5276 && VECTOR_TYPE_P (type)
5277 && tree_int_cst_le (TYPE_SIZE (TREE_TYPE (type)),
5278 TYPE_SIZE (TREE_TYPE (TREE_TYPE (@6))))
5279 && TYPE_SIZE (type) == TYPE_SIZE (TREE_TYPE (@6)))
5280 (with { tree vtype = TREE_TYPE (@6);}
5282 (vec_cond @4 (view_convert:vtype @2) (view_convert:vtype @3)))))))
5284 /* c1 ? c2 ? a : b : b --> (c1 & c2) ? a : b */
5286 (vec_cond @0 (vec_cond:s @1 @2 @3) @3)
5287 (if (optimize_vectors_before_lowering_p () && types_match (@0, @1))
5288 (vec_cond (bit_and @0 @1) @2 @3)))
5290 (vec_cond @0 @2 (vec_cond:s @1 @2 @3))
5291 (if (optimize_vectors_before_lowering_p () && types_match (@0, @1))
5292 (vec_cond (bit_ior @0 @1) @2 @3)))
5294 (vec_cond @0 (vec_cond:s @1 @2 @3) @2)
5295 (if (optimize_vectors_before_lowering_p () && types_match (@0, @1))
5296 (vec_cond (bit_ior (bit_not @0) @1) @2 @3)))
5298 (vec_cond @0 @3 (vec_cond:s @1 @2 @3))
5299 (if (optimize_vectors_before_lowering_p () && types_match (@0, @1))
5300 (vec_cond (bit_and (bit_not @0) @1) @2 @3)))
5303 /* Canonicalize mask ? { 0, ... } : { -1, ...} to ~mask if the mask
5304 types are compatible. */
5306 (vec_cond @0 VECTOR_CST@1 VECTOR_CST@2)
5307 (if (VECTOR_BOOLEAN_TYPE_P (type)
5308 && types_match (type, TREE_TYPE (@0)))
5309 (if (integer_zerop (@1) && integer_all_onesp (@2))
5311 (if (integer_all_onesp (@1) && integer_zerop (@2))
5314 /* A few simplifications of "a ? CST1 : CST2". */
5315 /* NOTE: Only do this on gimple as the if-chain-to-switch
5316 optimization depends on the gimple to have if statements in it. */
5319 (cond @0 INTEGER_CST@1 INTEGER_CST@2)
5321 (if (integer_zerop (@2))
5323 /* a ? 1 : 0 -> a if 0 and 1 are integral types. */
5324 (if (integer_onep (@1))
5325 (convert (convert:boolean_type_node @0)))
5326 /* a ? -1 : 0 -> -a. */
5327 (if (INTEGRAL_TYPE_P (type) && integer_all_onesp (@1))
5328 (if (TYPE_PRECISION (type) == 1)
5329 /* For signed 1-bit precision just cast bool to the type. */
5330 (convert (convert:boolean_type_node @0))
5331 (if (TREE_CODE (type) == BOOLEAN_TYPE)
5333 tree intt = build_nonstandard_integer_type (TYPE_PRECISION (type),
5334 TYPE_UNSIGNED (type));
5336 (convert (negate (convert:intt (convert:boolean_type_node @0)))))
5337 (negate (convert:type (convert:boolean_type_node @0))))))
5338 /* a ? powerof2cst : 0 -> a << (log2(powerof2cst)) */
5339 (if (INTEGRAL_TYPE_P (type) && integer_pow2p (@1))
5341 tree shift = build_int_cst (integer_type_node, tree_log2 (@1));
5343 (lshift (convert (convert:boolean_type_node @0)) { shift; })))))
5344 (if (integer_zerop (@1))
5346 /* a ? 0 : 1 -> !a. */
5347 (if (integer_onep (@2))
5348 (convert (bit_xor (convert:boolean_type_node @0) { boolean_true_node; })))
5349 /* a ? 0 : -1 -> -(!a). */
5350 (if (INTEGRAL_TYPE_P (type) && integer_all_onesp (@2))
5351 (if (TYPE_PRECISION (type) == 1)
5352 /* For signed 1-bit precision just cast bool to the type. */
5353 (convert (bit_xor (convert:boolean_type_node @0) { boolean_true_node; }))
5354 (if (TREE_CODE (type) == BOOLEAN_TYPE)
5356 tree intt = build_nonstandard_integer_type (TYPE_PRECISION (type),
5357 TYPE_UNSIGNED (type));
5359 (convert (negate (convert:intt (bit_xor (convert:boolean_type_node @0)
5360 { boolean_true_node; })))))
5361 (negate (convert:type (bit_xor (convert:boolean_type_node @0)
5362 { boolean_true_node; }))))))
5363 /* a ? 0 : powerof2cst -> (!a) << (log2(powerof2cst)) */
5364 (if (INTEGRAL_TYPE_P (type) && integer_pow2p (@2))
5366 tree shift = build_int_cst (integer_type_node, tree_log2 (@2));
5368 (lshift (convert (bit_xor (convert:boolean_type_node @0)
5369 { boolean_true_node; })) { shift; })))))))
5371 /* (a > 1) ? 0 : (cast)a is the same as (cast)(a == 1)
5372 for unsigned types. */
5374 (cond (gt @0 integer_onep@1) integer_zerop (convert? @2))
5375 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
5376 && bitwise_equal_p (@0, @2))
5377 (convert (eq @0 @1))
5381 /* (a <= 1) & (cast)a is the same as (cast)(a == 1)
5382 for unsigned types. */
5384 (bit_and:c (convert1? (le @0 integer_onep@1)) (convert2? @2))
5385 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
5386 && bitwise_equal_p (@0, @2))
5387 (convert (eq @0 @1))
5391 /* `(a == CST) & a` can be simplified to `0` or `(a == CST)` depending
5392 on the first bit of the CST. */
5394 (bit_and:c (convert@2 (eq @0 INTEGER_CST@1)) (convert? @0))
5395 (if ((wi::to_wide (@1) & 1) != 0)
5397 { build_zero_cst (type); }))
5400 # x_5 in range [cst1, cst2] where cst2 = cst1 + 1
5401 x_5 == cstN ? cst4 : cst3
5402 # op is == or != and N is 1 or 2
5403 to r_6 = x_5 + (min (cst3, cst4) - cst1) or
5404 r_6 = (min (cst3, cst4) + cst1) - x_5 depending on op, N and which
5405 of cst3 and cst4 is smaller.
5406 This was originally done by two_value_replacement in phiopt (PR 88676). */
5409 (cond (eqne SSA_NAME@0 INTEGER_CST@1) INTEGER_CST@2 INTEGER_CST@3)
5410 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
5411 && INTEGRAL_TYPE_P (type)
5412 && (wi::to_widest (@2) + 1 == wi::to_widest (@3)
5413 || wi::to_widest (@2) == wi::to_widest (@3) + 1))
5416 get_range_query (cfun)->range_of_expr (r, @0);
5417 if (r.undefined_p ())
5418 r.set_varying (TREE_TYPE (@0));
5420 wide_int min = r.lower_bound ();
5421 wide_int max = r.upper_bound ();
5424 && (wi::to_wide (@1) == min
5425 || wi::to_wide (@1) == max))
5427 tree arg0 = @2, arg1 = @3;
5429 if ((eqne == EQ_EXPR) ^ (wi::to_wide (@1) == min))
5430 std::swap (arg0, arg1);
5431 if (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type))
5432 type1 = TREE_TYPE (@0);
5435 auto prec = TYPE_PRECISION (type1);
5436 auto unsign = TYPE_UNSIGNED (type1);
5437 if (TREE_CODE (type1) == BOOLEAN_TYPE)
5438 type1 = build_nonstandard_integer_type (prec, unsign);
5439 min = wide_int::from (min, prec,
5440 TYPE_SIGN (TREE_TYPE (@0)));
5441 wide_int a = wide_int::from (wi::to_wide (arg0), prec,
5443 enum tree_code code;
5444 wi::overflow_type ovf;
5445 if (tree_int_cst_lt (arg0, arg1))
5451 /* lhs is known to be in range [min, min+1] and we want to add a
5452 to it. Check if that operation can overflow for those 2 values
5453 and if yes, force unsigned type. */
5454 wi::add (min + (wi::neg_p (a) ? 0 : 1), a, SIGNED, &ovf);
5456 type1 = unsigned_type_for (type1);
5465 /* lhs is known to be in range [min, min+1] and we want to subtract
5466 it from a. Check if that operation can overflow for those 2
5467 values and if yes, force unsigned type. */
5468 wi::sub (a, min + (wi::neg_p (min) ? 0 : 1), SIGNED, &ovf);
5470 type1 = unsigned_type_for (type1);
5473 tree arg = wide_int_to_tree (type1, a);
5475 (if (code == PLUS_EXPR)
5476 (convert (plus (convert:type1 @0) { arg; }))
5477 (convert (minus { arg; } (convert:type1 @0))))))))))
5481 (convert (cond@0 @1 INTEGER_CST@2 INTEGER_CST@3))
5482 (if (INTEGRAL_TYPE_P (type)
5483 && INTEGRAL_TYPE_P (TREE_TYPE (@0)))
5484 (cond @1 (convert @2) (convert @3))))
5486 /* Simplification moved from fold_cond_expr_with_comparison. It may also
5488 /* This pattern implements two kinds simplification:
5491 (cond (cmp (convert1? x) c1) (convert2? x) c2) -> (minmax (x c)) if:
5492 1) Conversions are type widening from smaller type.
5493 2) Const c1 equals to c2 after canonicalizing comparison.
5494 3) Comparison has tree code LT, LE, GT or GE.
5495 This specific pattern is needed when (cmp (convert x) c) may not
5496 be simplified by comparison patterns because of multiple uses of
5497 x. It also makes sense here because simplifying across multiple
5498 referred var is always benefitial for complicated cases.
5501 (cond (eq (convert1? x) c1) (convert2? x) c2) -> (cond (eq x c1) c1 c2). */
5502 (for cmp (lt le gt ge eq ne)
5504 (cond (cmp (convert1? @1) INTEGER_CST@3) (convert2? @1) INTEGER_CST@2)
5507 tree from_type = TREE_TYPE (@1);
5508 tree c1_type = TREE_TYPE (@3), c2_type = TREE_TYPE (@2);
5509 enum tree_code code = ERROR_MARK;
5511 if (INTEGRAL_TYPE_P (from_type)
5512 && int_fits_type_p (@2, from_type)
5513 && (types_match (c1_type, from_type)
5514 || (TYPE_PRECISION (c1_type) > TYPE_PRECISION (from_type)
5515 && (TYPE_UNSIGNED (from_type)
5516 || TYPE_SIGN (c1_type) == TYPE_SIGN (from_type))))
5517 && (types_match (c2_type, from_type)
5518 || (TYPE_PRECISION (c2_type) > TYPE_PRECISION (from_type)
5519 && (TYPE_UNSIGNED (from_type)
5520 || TYPE_SIGN (c2_type) == TYPE_SIGN (from_type)))))
5523 code = minmax_from_comparison (cmp, @1, @3, @1, @2);
5524 /* Can do A == C1 ? A : C2 -> A == C1 ? C1 : C2? */
5525 else if (int_fits_type_p (@3, from_type))
5529 (if (code == MAX_EXPR)
5530 (convert (max @1 (convert @2)))
5531 (if (code == MIN_EXPR)
5532 (convert (min @1 (convert @2)))
5533 (if (code == EQ_EXPR)
5534 (convert (cond (eq @1 (convert @3))
5535 (convert:from_type @3) (convert:from_type @2)))))))))
5537 /* (cond (cmp (convert? x) c1) (op x c2) c3) -> (op (minmax x c1) c2) if:
5539 1) OP is PLUS or MINUS.
5540 2) CMP is LT, LE, GT or GE.
5541 3) C3 == (C1 op C2), and computation doesn't have undefined behavior.
5543 This pattern also handles special cases like:
5545 A) Operand x is a unsigned to signed type conversion and c1 is
5546 integer zero. In this case,
5547 (signed type)x < 0 <=> x > MAX_VAL(signed type)
5548 (signed type)x >= 0 <=> x <= MAX_VAL(signed type)
5549 B) Const c1 may not equal to (C3 op' C2). In this case we also
5550 check equality for (c1+1) and (c1-1) by adjusting comparison
5553 TODO: Though signed type is handled by this pattern, it cannot be
5554 simplified at the moment because C standard requires additional
5555 type promotion. In order to match&simplify it here, the IR needs
5556 to be cleaned up by other optimizers, i.e, VRP. */
5557 (for op (plus minus)
5558 (for cmp (lt le gt ge)
5560 (cond (cmp (convert? @X) INTEGER_CST@1) (op @X INTEGER_CST@2) INTEGER_CST@3)
5561 (with { tree from_type = TREE_TYPE (@X), to_type = TREE_TYPE (@1); }
5562 (if (types_match (from_type, to_type)
5563 /* Check if it is special case A). */
5564 || (TYPE_UNSIGNED (from_type)
5565 && !TYPE_UNSIGNED (to_type)
5566 && TYPE_PRECISION (from_type) == TYPE_PRECISION (to_type)
5567 && integer_zerop (@1)
5568 && (cmp == LT_EXPR || cmp == GE_EXPR)))
5571 wi::overflow_type overflow = wi::OVF_NONE;
5572 enum tree_code code, cmp_code = cmp;
5574 wide_int c1 = wi::to_wide (@1);
5575 wide_int c2 = wi::to_wide (@2);
5576 wide_int c3 = wi::to_wide (@3);
5577 signop sgn = TYPE_SIGN (from_type);
5579 /* Handle special case A), given x of unsigned type:
5580 ((signed type)x < 0) <=> (x > MAX_VAL(signed type))
5581 ((signed type)x >= 0) <=> (x <= MAX_VAL(signed type)) */
5582 if (!types_match (from_type, to_type))
5584 if (cmp_code == LT_EXPR)
5586 if (cmp_code == GE_EXPR)
5588 c1 = wi::max_value (to_type);
5590 /* To simplify this pattern, we require c3 = (c1 op c2). Here we
5591 compute (c3 op' c2) and check if it equals to c1 with op' being
5592 the inverted operator of op. Make sure overflow doesn't happen
5593 if it is undefined. */
5594 if (op == PLUS_EXPR)
5595 real_c1 = wi::sub (c3, c2, sgn, &overflow);
5597 real_c1 = wi::add (c3, c2, sgn, &overflow);
5600 if (!overflow || !TYPE_OVERFLOW_UNDEFINED (from_type))
5602 /* Check if c1 equals to real_c1. Boundary condition is handled
5603 by adjusting comparison operation if necessary. */
5604 if (!wi::cmp (wi::sub (real_c1, 1, sgn, &overflow), c1, sgn)
5607 /* X <= Y - 1 equals to X < Y. */
5608 if (cmp_code == LE_EXPR)
5610 /* X > Y - 1 equals to X >= Y. */
5611 if (cmp_code == GT_EXPR)
5614 if (!wi::cmp (wi::add (real_c1, 1, sgn, &overflow), c1, sgn)
5617 /* X < Y + 1 equals to X <= Y. */
5618 if (cmp_code == LT_EXPR)
5620 /* X >= Y + 1 equals to X > Y. */
5621 if (cmp_code == GE_EXPR)
5624 if (code != cmp_code || !wi::cmp (real_c1, c1, sgn))
5626 if (cmp_code == LT_EXPR || cmp_code == LE_EXPR)
5628 if (cmp_code == GT_EXPR || cmp_code == GE_EXPR)
5633 (if (code == MAX_EXPR)
5634 (op (max @X { wide_int_to_tree (from_type, real_c1); })
5635 { wide_int_to_tree (from_type, c2); })
5636 (if (code == MIN_EXPR)
5637 (op (min @X { wide_int_to_tree (from_type, real_c1); })
5638 { wide_int_to_tree (from_type, c2); })))))))))
5641 /* A >= B ? A : B -> max (A, B) and friends. The code is still
5642 in fold_cond_expr_with_comparison for GENERIC folding with
5643 some extra constraints. */
5644 (for cmp (eq ne le lt unle unlt ge gt unge ungt uneq ltgt)
5646 (cond (cmp:c (nop_convert1?@c0 @0) (nop_convert2?@c1 @1))
5647 (convert3? @0) (convert4? @1))
5648 (if (!HONOR_SIGNED_ZEROS (type)
5649 && (/* Allow widening conversions of the compare operands as data. */
5650 (INTEGRAL_TYPE_P (type)
5651 && types_match (TREE_TYPE (@c0), TREE_TYPE (@0))
5652 && types_match (TREE_TYPE (@c1), TREE_TYPE (@1))
5653 && TYPE_PRECISION (TREE_TYPE (@0)) <= TYPE_PRECISION (type)
5654 && TYPE_PRECISION (TREE_TYPE (@1)) <= TYPE_PRECISION (type))
5655 /* Or sign conversions for the comparison. */
5656 || (types_match (type, TREE_TYPE (@0))
5657 && types_match (type, TREE_TYPE (@1)))))
5659 (if (cmp == EQ_EXPR)
5660 (if (VECTOR_TYPE_P (type))
5663 (if (cmp == NE_EXPR)
5664 (if (VECTOR_TYPE_P (type))
5667 (if (cmp == LE_EXPR || cmp == UNLE_EXPR || cmp == LT_EXPR || cmp == UNLT_EXPR)
5668 (if (!HONOR_NANS (type))
5669 (if (VECTOR_TYPE_P (type))
5670 (view_convert (min @c0 @c1))
5671 (convert (min @c0 @c1)))))
5672 (if (cmp == GE_EXPR || cmp == UNGE_EXPR || cmp == GT_EXPR || cmp == UNGT_EXPR)
5673 (if (!HONOR_NANS (type))
5674 (if (VECTOR_TYPE_P (type))
5675 (view_convert (max @c0 @c1))
5676 (convert (max @c0 @c1)))))
5677 (if (cmp == UNEQ_EXPR)
5678 (if (!HONOR_NANS (type))
5679 (if (VECTOR_TYPE_P (type))
5682 (if (cmp == LTGT_EXPR)
5683 (if (!HONOR_NANS (type))
5684 (if (VECTOR_TYPE_P (type))
5686 (convert @c0))))))))
5688 /* This is for VEC_COND_EXPR
5689 Optimize A < B ? A : B to MIN (A, B)
5690 A > B ? A : B to MAX (A, B). */
5691 (for cmp (lt le ungt unge gt ge unlt unle)
5692 minmax (min min min min max max max max)
5693 MINMAX (MIN_EXPR MIN_EXPR MIN_EXPR MIN_EXPR MAX_EXPR MAX_EXPR MAX_EXPR MAX_EXPR)
5695 (vec_cond (cmp @0 @1) @0 @1)
5696 (if (VECTOR_INTEGER_TYPE_P (type)
5697 && target_supports_op_p (type, MINMAX, optab_vector))
5700 (for cmp (lt le ungt unge gt ge unlt unle)
5701 minmax (max max max max min min min min)
5702 MINMAX (MAX_EXPR MAX_EXPR MAX_EXPR MAX_EXPR MIN_EXPR MIN_EXPR MIN_EXPR MIN_EXPR)
5704 (vec_cond (cmp @0 @1) @1 @0)
5705 (if (VECTOR_INTEGER_TYPE_P (type)
5706 && target_supports_op_p (type, MINMAX, optab_vector))
5710 (for cnd (cond vec_cond)
5711 /* (a != b) ? (a - b) : 0 -> (a - b) */
5713 (cnd (ne:c @0 @1) (minus@2 @0 @1) integer_zerop)
5715 /* (a != b) ? (a ^ b) : 0 -> (a ^ b) */
5717 (cnd (ne:c @0 @1) (bit_xor:c@2 @0 @1) integer_zerop)
5719 /* (a != b) ? (a & b) : a -> (a & b) */
5720 /* (a != b) ? (a | b) : a -> (a | b) */
5721 /* (a != b) ? min(a,b) : a -> min(a,b) */
5722 /* (a != b) ? max(a,b) : a -> max(a,b) */
5723 (for op (bit_and bit_ior min max)
5725 (cnd (ne:c @0 @1) (op:c@2 @0 @1) @0)
5727 /* (a != b) ? (a * b) : (a * a) -> (a * b) */
5728 /* (a != b) ? (a + b) : (a + a) -> (a + b) */
5731 (cnd (ne:c @0 @1) (op@2 @0 @1) (op @0 @0))
5732 (if (ANY_INTEGRAL_TYPE_P (type))
5734 /* (a != b) ? (a + b) : (2 * a) -> (a + b) */
5736 (cnd (ne:c @0 @1) (plus:c@2 @0 @1) (mult @0 uniform_integer_cst_p@3))
5737 (if (wi::to_wide (uniform_integer_cst_p (@3)) == 2)
5741 /* These was part of minmax phiopt. */
5742 /* Optimize (a CMP b) ? minmax<a, c> : minmax<b, c>
5743 to minmax<min/max<a, b>, c> */
5744 (for minmax (min max)
5745 (for cmp (lt le gt ge ne)
5747 (cond (cmp:c @1 @3) (minmax:c @1 @4) (minmax:c @2 @4))
5750 tree_code code = minmax_from_comparison (cmp, @1, @2, @1, @3);
5752 (if (code == MIN_EXPR)
5753 (minmax (min @1 @2) @4)
5754 (if (code == MAX_EXPR)
5755 (minmax (max @1 @2) @4)))))))
5757 /* Optimize (a CMP CST1) ? max<a,CST2> : a */
5758 (for cmp (gt ge lt le)
5759 minmax (min min max max)
5761 (cond (cmp:c @0 @1) (minmax:c@2 @0 @3) @4)
5764 tree_code code = minmax_from_comparison (cmp, @0, @1, @0, @4);
5766 (if ((cmp == LT_EXPR || cmp == LE_EXPR)
5768 && integer_nonzerop (fold_build2 (LE_EXPR, boolean_type_node, @3, @4)))
5770 (if ((cmp == GT_EXPR || cmp == GE_EXPR)
5772 && integer_nonzerop (fold_build2 (GE_EXPR, boolean_type_node, @3, @4)))
5776 /* These patterns should be after min/max detection as simplifications
5777 of `(type)(zero_one ==/!= 0)` to `(type)(zero_one)`
5778 and `(type)(zero_one^1)` are not done yet. See PR 110637.
5779 Even without those, reaching min/max/and/ior faster is better. */
5781 (cond @0 zero_one_valued_p@1 zero_one_valued_p@2)
5783 /* bool0 ? bool1 : 0 -> bool0 & bool1 */
5784 (if (integer_zerop (@2))
5785 (bit_and (convert @0) @1))
5786 /* bool0 ? 0 : bool2 -> (bool0^1) & bool2 */
5787 (if (integer_zerop (@1))
5788 (bit_and (bit_xor (convert @0) { build_one_cst (type); } ) @2))
5789 /* bool0 ? 1 : bool2 -> bool0 | bool2 */
5790 (if (integer_onep (@1))
5791 (bit_ior (convert @0) @2))
5792 /* bool0 ? bool1 : 1 -> (bool0^1) | bool1 */
5793 (if (integer_onep (@2))
5794 (bit_ior (bit_xor (convert @0) @2) @1))
5799 /* X != C1 ? -X : C2 simplifies to -X when -C1 == C2. */
5801 (cond (ne @0 INTEGER_CST@1) (negate@3 @0) INTEGER_CST@2)
5802 (if (!TYPE_SATURATING (type)
5803 && (TYPE_OVERFLOW_WRAPS (type)
5804 || !wi::only_sign_bit_p (wi::to_wide (@1)))
5805 && wi::eq_p (wi::neg (wi::to_wide (@1)), wi::to_wide (@2)))
5808 /* X != C1 ? ~X : C2 simplifies to ~X when ~C1 == C2. */
5810 (cond (ne @0 INTEGER_CST@1) (bit_not@3 @0) INTEGER_CST@2)
5811 (if (wi::eq_p (wi::bit_not (wi::to_wide (@1)), wi::to_wide (@2)))
5814 /* X != C1 ? abs(X) : C2 simplifies to abs(x) when abs(C1) == C2. */
5817 (cond (ne @0 INTEGER_CST@1) (op@3 @0) INTEGER_CST@2)
5818 (if (wi::abs (wi::to_wide (@1)) == wi::to_wide (@2))
5819 (if (op != ABSU_EXPR && wi::only_sign_bit_p (wi::to_wide (@1)))
5820 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
5821 (convert (absu:utype @0)))
5824 /* (X + 1) > Y ? -X : 1 simplifies to X >= Y ? -X : 1 when
5825 X is unsigned, as when X + 1 overflows, X is -1, so -X == 1. */
5827 (cond (gt (plus @0 integer_onep) @1) (negate @0) integer_onep@2)
5828 (if (TYPE_UNSIGNED (type))
5829 (cond (ge @0 @1) (negate @0) @2)))
5831 (for cnd (cond vec_cond)
5832 /* A ? B : (A ? X : C) -> A ? B : C. */
5834 (cnd @0 (cnd @0 @1 @2) @3)
5837 (cnd @0 @1 (cnd @0 @2 @3))
5839 /* A ? B : (!A ? C : X) -> A ? B : C. */
5840 /* ??? This matches embedded conditions open-coded because genmatch
5841 would generate matching code for conditions in separate stmts only.
5842 The following is still important to merge then and else arm cases
5843 from if-conversion. */
5845 (cnd @0 @1 (cnd @2 @3 @4))
5846 (if (inverse_conditions_p (@0, @2))
5849 (cnd @0 (cnd @1 @2 @3) @4)
5850 (if (inverse_conditions_p (@0, @1))
5853 /* A ? B : B -> B. */
5858 /* !A ? B : C -> A ? C : B. */
5860 (cnd (logical_inverted_value truth_valued_p@0) @1 @2)
5863 /* abs/negative simplifications moved from fold_cond_expr_with_comparison.
5865 None of these transformations work for modes with signed
5866 zeros. If A is +/-0, the first two transformations will
5867 change the sign of the result (from +0 to -0, or vice
5868 versa). The last four will fix the sign of the result,
5869 even though the original expressions could be positive or
5870 negative, depending on the sign of A.
5872 Note that all these transformations are correct if A is
5873 NaN, since the two alternatives (A and -A) are also NaNs. */
5875 (for cnd (cond vec_cond)
5876 /* A == 0 ? A : -A same as -A */
5879 (cnd (cmp @0 zerop) @2 (negate@1 @2))
5880 (if (!HONOR_SIGNED_ZEROS (type)
5881 && bitwise_equal_p (@0, @2))
5884 (cnd (cmp @0 zerop) zerop (negate@1 @2))
5885 (if (!HONOR_SIGNED_ZEROS (type)
5886 && bitwise_equal_p (@0, @2))
5889 /* A != 0 ? A : -A same as A */
5892 (cnd (cmp @0 zerop) @1 (negate @1))
5893 (if (!HONOR_SIGNED_ZEROS (type)
5894 && bitwise_equal_p (@0, @1))
5897 (cnd (cmp @0 zerop) @1 integer_zerop)
5898 (if (!HONOR_SIGNED_ZEROS (type)
5899 && bitwise_equal_p (@0, @1))
5902 /* A >=/> 0 ? A : -A same as abs (A) */
5905 (cnd (cmp @0 zerop) @1 (negate @1))
5906 (if (!HONOR_SIGNED_ZEROS (TREE_TYPE(@0))
5907 && !TYPE_UNSIGNED (TREE_TYPE(@0))
5908 && bitwise_equal_p (@0, @1))
5909 (if (TYPE_UNSIGNED (type))
5912 /* A <=/< 0 ? A : -A same as -abs (A) */
5915 (cnd (cmp @0 zerop) @1 (negate @1))
5916 (if (!HONOR_SIGNED_ZEROS (TREE_TYPE(@0))
5917 && !TYPE_UNSIGNED (TREE_TYPE(@0))
5918 && bitwise_equal_p (@0, @1))
5919 (if ((ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
5920 && !TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
5921 || TYPE_UNSIGNED (type))
5923 tree utype = unsigned_type_for (TREE_TYPE(@0));
5925 (convert (negate (absu:utype @0))))
5926 (negate (abs @0)))))
5929 /* (A - B) == 0 ? (A - B) : (B - A) same as (B - A) */
5932 (cnd (cmp (minus@0 @1 @2) zerop) @0 (minus@3 @2 @1))
5933 (if (!HONOR_SIGNED_ZEROS (type))
5936 (cnd (cmp (minus@0 @1 @2) integer_zerop) integer_zerop (minus@3 @2 @1))
5939 /* (A - B) != 0 ? (A - B) : (B - A) same as (A - B) */
5942 (cnd (cmp (minus@0 @1 @2) zerop) @0 (minus @2 @1))
5943 (if (!HONOR_SIGNED_ZEROS (type))
5946 (cnd (cmp (minus@0 @1 @2) integer_zerop) @0 integer_zerop)
5949 /* (A - B) >=/> 0 ? (A - B) : (B - A) same as abs (A - B) */
5952 (cnd (cmp (minus@0 @1 @2) zerop) @0 (minus @2 @1))
5953 (if (!HONOR_SIGNED_ZEROS (type)
5954 && !TYPE_UNSIGNED (type))
5956 /* (A - B) <=/< 0 ? (A - B) : (B - A) same as -abs (A - B) */
5959 (cnd (cmp (minus@0 @1 @2) zerop) @0 (minus @2 @1))
5960 (if (!HONOR_SIGNED_ZEROS (type)
5961 && !TYPE_UNSIGNED (type))
5962 (if (ANY_INTEGRAL_TYPE_P (type)
5963 && !TYPE_OVERFLOW_WRAPS (type))
5965 tree utype = unsigned_type_for (type);
5967 (convert (negate (absu:utype @0))))
5968 (negate (abs @0)))))
5972 /* -(type)!A -> (type)A - 1. */
5974 (negate (convert?:s (logical_inverted_value:s @0)))
5975 (if (INTEGRAL_TYPE_P (type)
5976 && TREE_CODE (type) != BOOLEAN_TYPE
5977 && TYPE_PRECISION (type) > 1
5978 && TREE_CODE (@0) == SSA_NAME
5979 && ssa_name_has_boolean_range (@0))
5980 (plus (convert:type @0) { build_all_ones_cst (type); })))
5982 /* A + (B vcmp C ? 1 : 0) -> A - (B vcmp C ? -1 : 0), since vector comparisons
5983 return all -1 or all 0 results. */
5984 /* ??? We could instead convert all instances of the vec_cond to negate,
5985 but that isn't necessarily a win on its own. */
5987 (plus:c @3 (view_convert? (vec_cond:s @0 integer_each_onep@1 integer_zerop@2)))
5988 (if (VECTOR_TYPE_P (type)
5989 && known_eq (TYPE_VECTOR_SUBPARTS (type),
5990 TYPE_VECTOR_SUBPARTS (TREE_TYPE (@1)))
5991 && (TYPE_MODE (TREE_TYPE (type))
5992 == TYPE_MODE (TREE_TYPE (TREE_TYPE (@1)))))
5993 (minus @3 (view_convert (vec_cond @0 (negate @1) @2)))))
5995 /* ... likewise A - (B vcmp C ? 1 : 0) -> A + (B vcmp C ? -1 : 0). */
5997 (minus @3 (view_convert? (vec_cond:s @0 integer_each_onep@1 integer_zerop@2)))
5998 (if (VECTOR_TYPE_P (type)
5999 && known_eq (TYPE_VECTOR_SUBPARTS (type),
6000 TYPE_VECTOR_SUBPARTS (TREE_TYPE (@1)))
6001 && (TYPE_MODE (TREE_TYPE (type))
6002 == TYPE_MODE (TREE_TYPE (TREE_TYPE (@1)))))
6003 (plus @3 (view_convert (vec_cond @0 (negate @1) @2)))))
6006 /* Simplifications of comparisons. */
6008 /* See if we can reduce the magnitude of a constant involved in a
6009 comparison by changing the comparison code. This is a canonicalization
6010 formerly done by maybe_canonicalize_comparison_1. */
6014 (cmp @0 uniform_integer_cst_p@1)
6015 (with { tree cst = uniform_integer_cst_p (@1); }
6016 (if (tree_int_cst_sgn (cst) == -1)
6017 (acmp @0 { build_uniform_cst (TREE_TYPE (@1),
6018 wide_int_to_tree (TREE_TYPE (cst),
6024 (cmp @0 uniform_integer_cst_p@1)
6025 (with { tree cst = uniform_integer_cst_p (@1); }
6026 (if (tree_int_cst_sgn (cst) == 1)
6027 (acmp @0 { build_uniform_cst (TREE_TYPE (@1),
6028 wide_int_to_tree (TREE_TYPE (cst),
6029 wi::to_wide (cst) - 1)); })))))
6031 /* We can simplify a logical negation of a comparison to the
6032 inverted comparison. As we cannot compute an expression
6033 operator using invert_tree_comparison we have to simulate
6034 that with expression code iteration. */
6035 (for cmp (tcc_comparison)
6036 icmp (inverted_tcc_comparison)
6037 ncmp (inverted_tcc_comparison_with_nans)
6038 /* Ideally we'd like to combine the following two patterns
6039 and handle some more cases by using
6040 (logical_inverted_value (cmp @0 @1))
6041 here but for that genmatch would need to "inline" that.
6042 For now implement what forward_propagate_comparison did. */
6044 (bit_not (cmp @0 @1))
6045 (if (VECTOR_TYPE_P (type)
6046 || (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1))
6047 /* Comparison inversion may be impossible for trapping math,
6048 invert_tree_comparison will tell us. But we can't use
6049 a computed operator in the replacement tree thus we have
6050 to play the trick below. */
6051 (with { enum tree_code ic = invert_tree_comparison
6052 (cmp, HONOR_NANS (@0)); }
6058 (bit_xor (cmp @0 @1) integer_truep)
6059 (with { enum tree_code ic = invert_tree_comparison
6060 (cmp, HONOR_NANS (@0)); }
6065 /* The following bits are handled by fold_binary_op_with_conditional_arg. */
6067 (ne (cmp@2 @0 @1) integer_zerop)
6068 (if (types_match (type, TREE_TYPE (@2)))
6071 (eq (cmp@2 @0 @1) integer_truep)
6072 (if (types_match (type, TREE_TYPE (@2)))
6075 (ne (cmp@2 @0 @1) integer_truep)
6076 (if (types_match (type, TREE_TYPE (@2)))
6077 (with { enum tree_code ic = invert_tree_comparison
6078 (cmp, HONOR_NANS (@0)); }
6084 (eq (cmp@2 @0 @1) integer_zerop)
6085 (if (types_match (type, TREE_TYPE (@2)))
6086 (with { enum tree_code ic = invert_tree_comparison
6087 (cmp, HONOR_NANS (@0)); }
6093 /* Transform comparisons of the form X - Y CMP 0 to X CMP Y.
6094 ??? The transformation is valid for the other operators if overflow
6095 is undefined for the type, but performing it here badly interacts
6096 with the transformation in fold_cond_expr_with_comparison which
6097 attempts to synthetize ABS_EXPR. */
6099 (for sub (minus pointer_diff)
6101 (cmp (sub@2 @0 @1) integer_zerop)
6102 (if (single_use (@2))
6105 /* Simplify (x < 0) ^ (y < 0) to (x ^ y) < 0 and
6106 (x >= 0) ^ (y >= 0) to (x ^ y) < 0. */
6109 (bit_xor (cmp:s @0 integer_zerop) (cmp:s @1 integer_zerop))
6110 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6111 && !TYPE_UNSIGNED (TREE_TYPE (@0))
6112 && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
6113 (lt (bit_xor @0 @1) { build_zero_cst (TREE_TYPE (@0)); }))))
6114 /* Simplify (x < 0) ^ (y >= 0) to (x ^ y) >= 0 and
6115 (x >= 0) ^ (y < 0) to (x ^ y) >= 0. */
6117 (bit_xor:c (lt:s @0 integer_zerop) (ge:s @1 integer_zerop))
6118 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6119 && !TYPE_UNSIGNED (TREE_TYPE (@0))
6120 && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
6121 (ge (bit_xor @0 @1) { build_zero_cst (TREE_TYPE (@0)); })))
6123 /* Transform comparisons of the form X * C1 CMP 0 to X CMP 0 in the
6124 signed arithmetic case. That form is created by the compiler
6125 often enough for folding it to be of value. One example is in
6126 computing loop trip counts after Operator Strength Reduction. */
6127 (for cmp (simple_comparison)
6128 scmp (swapped_simple_comparison)
6130 (cmp (mult@3 @0 INTEGER_CST@1) integer_zerop@2)
6131 /* Handle unfolded multiplication by zero. */
6132 (if (integer_zerop (@1))
6134 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
6135 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
6137 /* If @1 is negative we swap the sense of the comparison. */
6138 (if (tree_int_cst_sgn (@1) < 0)
6142 /* For integral types with undefined overflow fold
6143 x * C1 == C2 into x == C2 / C1 or false.
6144 If overflow wraps and C1 is odd, simplify to x == C2 / C1 in the ring
6148 (cmp (mult @0 INTEGER_CST@1) INTEGER_CST@2)
6149 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6150 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
6151 && wi::to_wide (@1) != 0)
6152 (with { widest_int quot; }
6153 (if (wi::multiple_of_p (wi::to_widest (@2), wi::to_widest (@1),
6154 TYPE_SIGN (TREE_TYPE (@0)), "))
6155 (cmp @0 { wide_int_to_tree (TREE_TYPE (@0), quot); })
6156 { constant_boolean_node (cmp == NE_EXPR, type); }))
6157 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6158 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))
6159 && (wi::bit_and (wi::to_wide (@1), 1) == 1))
6162 tree itype = TREE_TYPE (@0);
6163 int p = TYPE_PRECISION (itype);
6164 wide_int m = wi::one (p + 1) << p;
6165 wide_int a = wide_int::from (wi::to_wide (@1), p + 1, UNSIGNED);
6166 wide_int i = wide_int::from (wi::mod_inv (a, m),
6167 p, TYPE_SIGN (itype));
6168 wide_int_to_tree (itype, wi::mul (i, wi::to_wide (@2)));
6171 /* Simplify comparison of something with itself. For IEEE
6172 floating-point, we can only do some of these simplifications. */
6176 (if (! FLOAT_TYPE_P (TREE_TYPE (@0))
6177 || ! tree_expr_maybe_nan_p (@0))
6178 { constant_boolean_node (true, type); }
6180 /* With -ftrapping-math conversion to EQ loses an exception. */
6181 && (! FLOAT_TYPE_P (TREE_TYPE (@0))
6182 || ! flag_trapping_math))
6188 || ! FLOAT_TYPE_P (TREE_TYPE (@0))
6189 || ! tree_expr_maybe_nan_p (@0))
6190 { constant_boolean_node (false, type); })))
6191 (for cmp (unle unge uneq)
6194 { constant_boolean_node (true, type); }))
6195 (for cmp (unlt ungt)
6201 (if (!flag_trapping_math || !tree_expr_maybe_nan_p (@0))
6202 { constant_boolean_node (false, type); }))
6204 /* x == ~x -> false */
6205 /* x != ~x -> true */
6208 (cmp:c @0 (bit_not @0))
6209 { constant_boolean_node (cmp == NE_EXPR, type); }))
6211 /* Fold ~X op ~Y as Y op X. */
6212 (for cmp (simple_comparison)
6214 (cmp (nop_convert1?@4 (bit_not@2 @0)) (nop_convert2? (bit_not@3 @1)))
6215 (if (single_use (@2) && single_use (@3))
6216 (with { tree otype = TREE_TYPE (@4); }
6217 (cmp (convert:otype @1) (convert:otype @0))))))
6219 /* Fold ~X op C as X op' ~C, where op' is the swapped comparison. */
6220 (for cmp (simple_comparison)
6221 scmp (swapped_simple_comparison)
6223 (cmp (nop_convert? (bit_not@2 @0)) CONSTANT_CLASS_P@1)
6224 (if (single_use (@2)
6225 && (TREE_CODE (@1) == INTEGER_CST || TREE_CODE (@1) == VECTOR_CST))
6226 (with { tree otype = TREE_TYPE (@1); }
6227 (scmp (convert:otype @0) (bit_not @1))))))
6229 (for cmp (simple_comparison)
6232 /* IEEE doesn't distinguish +0 and -0 in comparisons. */
6234 /* a CMP (-0) -> a CMP 0 */
6235 (if (REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@1)))
6236 (cmp @0 { build_real (TREE_TYPE (@1), dconst0); }))
6237 /* (-0) CMP b -> 0 CMP b. */
6238 (if (TREE_CODE (@0) == REAL_CST
6239 && REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@0)))
6240 (cmp { build_real (TREE_TYPE (@0), dconst0); } @1))
6241 /* x != NaN is always true, other ops are always false. */
6242 (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
6243 && (cmp == EQ_EXPR || cmp == NE_EXPR || !flag_trapping_math)
6244 && !tree_expr_signaling_nan_p (@1)
6245 && !tree_expr_maybe_signaling_nan_p (@0))
6246 { constant_boolean_node (cmp == NE_EXPR, type); })
6247 /* NaN != y is always true, other ops are always false. */
6248 (if (TREE_CODE (@0) == REAL_CST
6249 && REAL_VALUE_ISNAN (TREE_REAL_CST (@0))
6250 && (cmp == EQ_EXPR || cmp == NE_EXPR || !flag_trapping_math)
6251 && !tree_expr_signaling_nan_p (@0)
6252 && !tree_expr_signaling_nan_p (@1))
6253 { constant_boolean_node (cmp == NE_EXPR, type); })
6254 /* Fold comparisons against infinity. */
6255 (if (REAL_VALUE_ISINF (TREE_REAL_CST (@1))
6256 && MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (@1))))
6259 REAL_VALUE_TYPE max;
6260 enum tree_code code = cmp;
6261 bool neg = REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1));
6263 code = swap_tree_comparison (code);
6266 /* x > +Inf is always false, if we ignore NaNs or exceptions. */
6267 (if (code == GT_EXPR
6268 && !(HONOR_NANS (@0) && flag_trapping_math))
6269 { constant_boolean_node (false, type); })
6270 (if (code == LE_EXPR)
6271 /* x <= +Inf is always true, if we don't care about NaNs. */
6272 (if (! HONOR_NANS (@0))
6273 { constant_boolean_node (true, type); }
6274 /* x <= +Inf is the same as x == x, i.e. !isnan(x), but this loses
6275 an "invalid" exception. */
6276 (if (!flag_trapping_math)
6278 /* x == +Inf and x >= +Inf are always equal to x > DBL_MAX, but
6279 for == this introduces an exception for x a NaN. */
6280 (if ((code == EQ_EXPR && !(HONOR_NANS (@0) && flag_trapping_math))
6282 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
6284 (lt @0 { build_real (TREE_TYPE (@0), max); })
6285 (gt @0 { build_real (TREE_TYPE (@0), max); }))))
6286 /* x < +Inf is always equal to x <= DBL_MAX. */
6287 (if (code == LT_EXPR)
6288 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
6290 (ge @0 { build_real (TREE_TYPE (@0), max); })
6291 (le @0 { build_real (TREE_TYPE (@0), max); }))))
6292 /* x != +Inf is always equal to !(x > DBL_MAX), but this introduces
6293 an exception for x a NaN so use an unordered comparison. */
6294 (if (code == NE_EXPR)
6295 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
6296 (if (! HONOR_NANS (@0))
6298 (ge @0 { build_real (TREE_TYPE (@0), max); })
6299 (le @0 { build_real (TREE_TYPE (@0), max); }))
6301 (unge @0 { build_real (TREE_TYPE (@0), max); })
6302 (unle @0 { build_real (TREE_TYPE (@0), max); }))))))))))
6304 /* If this is a comparison of a real constant with a PLUS_EXPR
6305 or a MINUS_EXPR of a real constant, we can convert it into a
6306 comparison with a revised real constant as long as no overflow
6307 occurs when unsafe_math_optimizations are enabled. */
6308 (if (flag_unsafe_math_optimizations)
6309 (for op (plus minus)
6311 (cmp (op @0 REAL_CST@1) REAL_CST@2)
6314 tree tem = const_binop (op == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR,
6315 TREE_TYPE (@1), @2, @1);
6317 (if (tem && !TREE_OVERFLOW (tem))
6318 (cmp @0 { tem; }))))))
6320 /* Likewise, we can simplify a comparison of a real constant with
6321 a MINUS_EXPR whose first operand is also a real constant, i.e.
6322 (c1 - x) < c2 becomes x > c1-c2. Reordering is allowed on
6323 floating-point types only if -fassociative-math is set. */
6324 (if (flag_associative_math)
6326 (cmp (minus REAL_CST@0 @1) REAL_CST@2)
6327 (with { tree tem = const_binop (MINUS_EXPR, TREE_TYPE (@1), @0, @2); }
6328 (if (tem && !TREE_OVERFLOW (tem))
6329 (cmp { tem; } @1)))))
6331 /* Fold comparisons against built-in math functions. */
6332 (if (flag_unsafe_math_optimizations && ! flag_errno_math)
6335 (cmp (sq @0) REAL_CST@1)
6337 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
6339 /* sqrt(x) < y is always false, if y is negative. */
6340 (if (cmp == EQ_EXPR || cmp == LT_EXPR || cmp == LE_EXPR)
6341 { constant_boolean_node (false, type); })
6342 /* sqrt(x) > y is always true, if y is negative and we
6343 don't care about NaNs, i.e. negative values of x. */
6344 (if (cmp == NE_EXPR || !HONOR_NANS (@0))
6345 { constant_boolean_node (true, type); })
6346 /* sqrt(x) > y is the same as x >= 0, if y is negative. */
6347 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })))
6348 (if (real_equal (TREE_REAL_CST_PTR (@1), &dconst0))
6350 /* sqrt(x) < 0 is always false. */
6351 (if (cmp == LT_EXPR)
6352 { constant_boolean_node (false, type); })
6353 /* sqrt(x) >= 0 is always true if we don't care about NaNs. */
6354 (if (cmp == GE_EXPR && !HONOR_NANS (@0))
6355 { constant_boolean_node (true, type); })
6356 /* sqrt(x) <= 0 -> x == 0. */
6357 (if (cmp == LE_EXPR)
6359 /* Otherwise sqrt(x) cmp 0 -> x cmp 0. Here cmp can be >=, >,
6360 == or !=. In the last case:
6362 (sqrt(x) != 0) == (NaN != 0) == true == (x != 0)
6364 if x is negative or NaN. Due to -funsafe-math-optimizations,
6365 the results for other x follow from natural arithmetic. */
6367 (if ((cmp == LT_EXPR
6371 && !REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
6372 /* Give up for -frounding-math. */
6373 && !HONOR_SIGN_DEPENDENT_ROUNDING (TREE_TYPE (@0)))
6377 enum tree_code ncmp = cmp;
6378 const real_format *fmt
6379 = REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (@0)));
6380 real_arithmetic (&c2, MULT_EXPR,
6381 &TREE_REAL_CST (@1), &TREE_REAL_CST (@1));
6382 real_convert (&c2, fmt, &c2);
6383 /* See PR91734: if c2 is inexact and sqrt(c2) < c (or sqrt(c2) >= c),
6384 then change LT_EXPR into LE_EXPR or GE_EXPR into GT_EXPR. */
6385 if (!REAL_VALUE_ISINF (c2))
6387 tree c3 = fold_const_call (CFN_SQRT, TREE_TYPE (@0),
6388 build_real (TREE_TYPE (@0), c2));
6389 if (c3 == NULL_TREE || TREE_CODE (c3) != REAL_CST)
6391 else if ((cmp == LT_EXPR || cmp == GE_EXPR)
6392 && real_less (&TREE_REAL_CST (c3), &TREE_REAL_CST (@1)))
6393 ncmp = cmp == LT_EXPR ? LE_EXPR : GT_EXPR;
6394 else if ((cmp == LE_EXPR || cmp == GT_EXPR)
6395 && real_less (&TREE_REAL_CST (@1), &TREE_REAL_CST (c3)))
6396 ncmp = cmp == LE_EXPR ? LT_EXPR : GE_EXPR;
6399 /* With rounding to even, sqrt of up to 3 different values
6400 gives the same normal result, so in some cases c2 needs
6402 REAL_VALUE_TYPE c2alt, tow;
6403 if (cmp == LT_EXPR || cmp == GE_EXPR)
6407 real_nextafter (&c2alt, fmt, &c2, &tow);
6408 real_convert (&c2alt, fmt, &c2alt);
6409 if (REAL_VALUE_ISINF (c2alt))
6413 c3 = fold_const_call (CFN_SQRT, TREE_TYPE (@0),
6414 build_real (TREE_TYPE (@0), c2alt));
6415 if (c3 == NULL_TREE || TREE_CODE (c3) != REAL_CST)
6417 else if (real_equal (&TREE_REAL_CST (c3),
6418 &TREE_REAL_CST (@1)))
6424 (if (cmp == GT_EXPR || cmp == GE_EXPR)
6425 (if (REAL_VALUE_ISINF (c2))
6426 /* sqrt(x) > y is x == +Inf, when y is very large. */
6427 (if (HONOR_INFINITIES (@0))
6428 (eq @0 { build_real (TREE_TYPE (@0), c2); })
6429 { constant_boolean_node (false, type); })
6430 /* sqrt(x) > c is the same as x > c*c. */
6431 (if (ncmp != ERROR_MARK)
6432 (if (ncmp == GE_EXPR)
6433 (ge @0 { build_real (TREE_TYPE (@0), c2); })
6434 (gt @0 { build_real (TREE_TYPE (@0), c2); }))))
6435 /* else if (cmp == LT_EXPR || cmp == LE_EXPR) */
6436 (if (REAL_VALUE_ISINF (c2))
6438 /* sqrt(x) < y is always true, when y is a very large
6439 value and we don't care about NaNs or Infinities. */
6440 (if (! HONOR_NANS (@0) && ! HONOR_INFINITIES (@0))
6441 { constant_boolean_node (true, type); })
6442 /* sqrt(x) < y is x != +Inf when y is very large and we
6443 don't care about NaNs. */
6444 (if (! HONOR_NANS (@0))
6445 (ne @0 { build_real (TREE_TYPE (@0), c2); }))
6446 /* sqrt(x) < y is x >= 0 when y is very large and we
6447 don't care about Infinities. */
6448 (if (! HONOR_INFINITIES (@0))
6449 (ge @0 { build_real (TREE_TYPE (@0), dconst0); }))
6450 /* sqrt(x) < y is x >= 0 && x != +Inf, when y is large. */
6453 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
6454 (ne @0 { build_real (TREE_TYPE (@0), c2); }))))
6455 /* sqrt(x) < c is the same as x < c*c, if we ignore NaNs. */
6456 (if (ncmp != ERROR_MARK && ! HONOR_NANS (@0))
6457 (if (ncmp == LT_EXPR)
6458 (lt @0 { build_real (TREE_TYPE (@0), c2); })
6459 (le @0 { build_real (TREE_TYPE (@0), c2); }))
6460 /* sqrt(x) < c is the same as x >= 0 && x < c*c. */
6461 (if (ncmp != ERROR_MARK && GENERIC)
6462 (if (ncmp == LT_EXPR)
6464 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
6465 (lt @0 { build_real (TREE_TYPE (@0), c2); }))
6467 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
6468 (le @0 { build_real (TREE_TYPE (@0), c2); })))))))))))
6469 /* Transform sqrt(x) cmp sqrt(y) -> x cmp y. */
6471 (cmp (sq @0) (sq @1))
6472 (if (! HONOR_NANS (@0))
6475 /* Optimize various special cases of (FTYPE) N CMP (FTYPE) M. */
6476 (for cmp (lt le eq ne ge gt unordered ordered unlt unle ungt unge uneq ltgt)
6477 icmp (lt le eq ne ge gt unordered ordered lt le gt ge eq ne)
6479 (cmp (float@0 @1) (float @2))
6480 (if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (@0))
6481 && ! DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0)))
6484 format_helper fmt (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (@0))));
6485 tree type1 = TREE_TYPE (@1);
6486 bool type1_signed_p = TYPE_SIGN (type1) == SIGNED;
6487 tree type2 = TREE_TYPE (@2);
6488 bool type2_signed_p = TYPE_SIGN (type2) == SIGNED;
6490 (if (fmt.can_represent_integral_type_p (type1)
6491 && fmt.can_represent_integral_type_p (type2))
6492 (if (cmp == ORDERED_EXPR || cmp == UNORDERED_EXPR)
6493 { constant_boolean_node (cmp == ORDERED_EXPR, type); }
6494 (if (TYPE_PRECISION (type1) > TYPE_PRECISION (type2)
6495 && type1_signed_p >= type2_signed_p)
6496 (icmp @1 (convert @2))
6497 (if (TYPE_PRECISION (type1) < TYPE_PRECISION (type2)
6498 && type1_signed_p <= type2_signed_p)
6499 (icmp (convert:type2 @1) @2)
6500 (if (TYPE_PRECISION (type1) == TYPE_PRECISION (type2)
6501 && type1_signed_p == type2_signed_p)
6502 (icmp @1 @2))))))))))
6504 /* Optimize various special cases of (FTYPE) N CMP CST. */
6505 (for cmp (lt le eq ne ge gt)
6506 icmp (le le eq ne ge ge)
6508 (cmp (float @0) REAL_CST@1)
6509 (if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (@1))
6510 && ! DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1)))
6513 tree itype = TREE_TYPE (@0);
6514 format_helper fmt (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (@1))));
6515 const REAL_VALUE_TYPE *cst = TREE_REAL_CST_PTR (@1);
6516 /* Be careful to preserve any potential exceptions due to
6517 NaNs. qNaNs are ok in == or != context.
6518 TODO: relax under -fno-trapping-math or
6519 -fno-signaling-nans. */
6521 = real_isnan (cst) && (cst->signalling
6522 || (cmp != EQ_EXPR && cmp != NE_EXPR));
6524 /* TODO: allow non-fitting itype and SNaNs when
6525 -fno-trapping-math. */
6526 (if (fmt.can_represent_integral_type_p (itype) && ! exception_p)
6529 signop isign = TYPE_SIGN (itype);
6530 REAL_VALUE_TYPE imin, imax;
6531 real_from_integer (&imin, fmt, wi::min_value (itype), isign);
6532 real_from_integer (&imax, fmt, wi::max_value (itype), isign);
6534 REAL_VALUE_TYPE icst;
6535 if (cmp == GT_EXPR || cmp == GE_EXPR)
6536 real_ceil (&icst, fmt, cst);
6537 else if (cmp == LT_EXPR || cmp == LE_EXPR)
6538 real_floor (&icst, fmt, cst);
6540 real_trunc (&icst, fmt, cst);
6542 bool cst_int_p = !real_isnan (cst) && real_identical (&icst, cst);
6544 bool overflow_p = false;
6546 = real_to_integer (&icst, &overflow_p, TYPE_PRECISION (itype));
6549 /* Optimize cases when CST is outside of ITYPE's range. */
6550 (if (real_compare (LT_EXPR, cst, &imin))
6551 { constant_boolean_node (cmp == GT_EXPR || cmp == GE_EXPR || cmp == NE_EXPR,
6553 (if (real_compare (GT_EXPR, cst, &imax))
6554 { constant_boolean_node (cmp == LT_EXPR || cmp == LE_EXPR || cmp == NE_EXPR,
6556 /* Remove cast if CST is an integer representable by ITYPE. */
6558 (cmp @0 { gcc_assert (!overflow_p);
6559 wide_int_to_tree (itype, icst_val); })
6561 /* When CST is fractional, optimize
6562 (FTYPE) N == CST -> 0
6563 (FTYPE) N != CST -> 1. */
6564 (if (cmp == EQ_EXPR || cmp == NE_EXPR)
6565 { constant_boolean_node (cmp == NE_EXPR, type); })
6566 /* Otherwise replace with sensible integer constant. */
6569 gcc_checking_assert (!overflow_p);
6571 (icmp @0 { wide_int_to_tree (itype, icst_val); })))))))))
6573 /* Fold A /[ex] B CMP C to A CMP B * C. */
6576 (cmp (exact_div @0 @1) INTEGER_CST@2)
6577 (if (!integer_zerop (@1))
6578 (if (wi::to_wide (@2) == 0)
6580 (if (TREE_CODE (@1) == INTEGER_CST)
6583 wi::overflow_type ovf;
6584 wide_int prod = wi::mul (wi::to_wide (@2), wi::to_wide (@1),
6585 TYPE_SIGN (TREE_TYPE (@1)), &ovf);
6588 { constant_boolean_node (cmp == NE_EXPR, type); }
6589 (cmp @0 { wide_int_to_tree (TREE_TYPE (@0), prod); }))))))))
6590 (for cmp (lt le gt ge)
6592 (cmp (exact_div @0 INTEGER_CST@1) INTEGER_CST@2)
6593 (if (wi::gt_p (wi::to_wide (@1), 0, TYPE_SIGN (TREE_TYPE (@1))))
6596 wi::overflow_type ovf;
6597 wide_int prod = wi::mul (wi::to_wide (@2), wi::to_wide (@1),
6598 TYPE_SIGN (TREE_TYPE (@1)), &ovf);
6601 { constant_boolean_node (wi::lt_p (wi::to_wide (@2), 0,
6602 TYPE_SIGN (TREE_TYPE (@2)))
6603 != (cmp == LT_EXPR || cmp == LE_EXPR), type); }
6604 (cmp @0 { wide_int_to_tree (TREE_TYPE (@0), prod); }))))))
6606 /* Fold (size_t)(A /[ex] B) CMP C to (size_t)A CMP (size_t)B * C or A CMP' 0.
6608 For small C (less than max/B), this is (size_t)A CMP (size_t)B * C.
6609 For large C (more than min/B+2^size), this is also true, with the
6610 multiplication computed modulo 2^size.
6611 For intermediate C, this just tests the sign of A. */
6612 (for cmp (lt le gt ge)
6615 (cmp (convert (exact_div @0 INTEGER_CST@1)) INTEGER_CST@2)
6616 (if (tree_nop_conversion_p (TREE_TYPE (@0), TREE_TYPE (@2))
6617 && TYPE_UNSIGNED (TREE_TYPE (@2)) && !TYPE_UNSIGNED (TREE_TYPE (@0))
6618 && wi::gt_p (wi::to_wide (@1), 0, TYPE_SIGN (TREE_TYPE (@1))))
6621 tree utype = TREE_TYPE (@2);
6622 wide_int denom = wi::to_wide (@1);
6623 wide_int right = wi::to_wide (@2);
6624 wide_int smax = wi::sdiv_trunc (wi::max_value (TREE_TYPE (@0)), denom);
6625 wide_int smin = wi::sdiv_trunc (wi::min_value (TREE_TYPE (@0)), denom);
6626 bool small = wi::leu_p (right, smax);
6627 bool large = wi::geu_p (right, smin);
6629 (if (small || large)
6630 (cmp (convert:utype @0) (mult @2 (convert @1)))
6631 (cmp2 @0 { build_zero_cst (TREE_TYPE (@0)); }))))))
6633 /* Unordered tests if either argument is a NaN. */
6635 (bit_ior (unordered @0 @0) (unordered @1 @1))
6636 (if (types_match (@0, @1))
6639 (bit_and (ordered @0 @0) (ordered @1 @1))
6640 (if (types_match (@0, @1))
6643 (bit_ior:c (unordered @0 @0) (unordered:c@2 @0 @1))
6646 (bit_and:c (ordered @0 @0) (ordered:c@2 @0 @1))
6649 /* A & (2**N - 1) <= 2**K - 1 -> A & (2**N - 2**K) == 0
6650 A & (2**N - 1) > 2**K - 1 -> A & (2**N - 2**K) != 0
6652 Note that comparisons
6653 A & (2**N - 1) < 2**K -> A & (2**N - 2**K) == 0
6654 A & (2**N - 1) >= 2**K -> A & (2**N - 2**K) != 0
6655 will be canonicalized to above so there's no need to
6662 (cmp (bit_and@0 @1 INTEGER_CST@2) INTEGER_CST@3)
6663 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)))
6666 tree ty = TREE_TYPE (@0);
6667 unsigned prec = TYPE_PRECISION (ty);
6668 wide_int mask = wi::to_wide (@2, prec);
6669 wide_int rhs = wi::to_wide (@3, prec);
6670 signop sgn = TYPE_SIGN (ty);
6672 (if ((mask & (mask + 1)) == 0 && wi::gt_p (rhs, 0, sgn)
6673 && (rhs & (rhs + 1)) == 0 && wi::ge_p (mask, rhs, sgn))
6674 (eqcmp (bit_and @1 { wide_int_to_tree (ty, mask - rhs); })
6675 { build_zero_cst (ty); }))))))
6677 /* -A CMP -B -> B CMP A. */
6678 (for cmp (tcc_comparison)
6679 scmp (swapped_tcc_comparison)
6681 (cmp (negate @0) (negate @1))
6682 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
6683 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
6686 || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))))
6689 (cmp (negate @0) CONSTANT_CLASS_P@1)
6690 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
6691 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
6694 || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))))
6695 (with { tree tem = const_unop (NEGATE_EXPR, TREE_TYPE (@0), @1); }
6696 (if (tem && !TREE_OVERFLOW (tem))
6697 (scmp @0 { tem; }))))))
6699 /* Convert ABS[U]_EXPR<x> == 0 or ABS[U]_EXPR<x> != 0 to x == 0 or x != 0. */
6703 (eqne (op @0) zerop@1)
6704 (eqne @0 { build_zero_cst (TREE_TYPE (@0)); }))))
6706 /* From fold_sign_changed_comparison and fold_widened_comparison.
6707 FIXME: the lack of symmetry is disturbing. */
6708 (for cmp (simple_comparison)
6710 (cmp (convert@0 @00) (convert?@1 @10))
6711 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6712 /* Disable this optimization if we're casting a function pointer
6713 type on targets that require function pointer canonicalization. */
6714 && !(targetm.have_canonicalize_funcptr_for_compare ()
6715 && ((POINTER_TYPE_P (TREE_TYPE (@00))
6716 && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@00))))
6717 || (POINTER_TYPE_P (TREE_TYPE (@10))
6718 && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@10))))))
6720 (if (TYPE_PRECISION (TREE_TYPE (@00)) == TYPE_PRECISION (TREE_TYPE (@0))
6721 && (TREE_CODE (@10) == INTEGER_CST
6723 && (TYPE_UNSIGNED (TREE_TYPE (@00)) == TYPE_UNSIGNED (TREE_TYPE (@0))
6726 && !POINTER_TYPE_P (TREE_TYPE (@00))
6727 /* (int)bool:32 != (int)uint is not the same as
6728 bool:32 != (bool:32)uint since boolean types only have two valid
6729 values independent of their precision. */
6730 && (TREE_CODE (TREE_TYPE (@00)) != BOOLEAN_TYPE
6731 || TREE_CODE (TREE_TYPE (@10)) == BOOLEAN_TYPE))
6732 /* ??? The special-casing of INTEGER_CST conversion was in the original
6733 code and here to avoid a spurious overflow flag on the resulting
6734 constant which fold_convert produces. */
6735 (if (TREE_CODE (@1) == INTEGER_CST)
6736 (cmp @00 { force_fit_type (TREE_TYPE (@00),
6737 wide_int::from (wi::to_wide (@1),
6738 MAX (TYPE_PRECISION (TREE_TYPE (@1)),
6739 TYPE_PRECISION (TREE_TYPE (@00))),
6740 TYPE_SIGN (TREE_TYPE (@1))),
6741 0, TREE_OVERFLOW (@1)); })
6742 (cmp @00 (convert @1)))
6744 (if (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (TREE_TYPE (@00)))
6745 /* If possible, express the comparison in the shorter mode. */
6746 (if ((cmp == EQ_EXPR || cmp == NE_EXPR
6747 || TYPE_UNSIGNED (TREE_TYPE (@0)) == TYPE_UNSIGNED (TREE_TYPE (@00))
6748 || (!TYPE_UNSIGNED (TREE_TYPE (@0))
6749 && TYPE_UNSIGNED (TREE_TYPE (@00))))
6750 && (types_match (TREE_TYPE (@10), TREE_TYPE (@00))
6751 || ((TYPE_PRECISION (TREE_TYPE (@00))
6752 >= TYPE_PRECISION (TREE_TYPE (@10)))
6753 && (TYPE_UNSIGNED (TREE_TYPE (@00))
6754 == TYPE_UNSIGNED (TREE_TYPE (@10))))
6755 || (TREE_CODE (@10) == INTEGER_CST
6756 && INTEGRAL_TYPE_P (TREE_TYPE (@00))
6757 && int_fits_type_p (@10, TREE_TYPE (@00)))))
6758 (cmp @00 (convert @10))
6759 (if (TREE_CODE (@10) == INTEGER_CST
6760 && INTEGRAL_TYPE_P (TREE_TYPE (@00))
6761 && !int_fits_type_p (@10, TREE_TYPE (@00)))
6764 tree min = lower_bound_in_type (TREE_TYPE (@10), TREE_TYPE (@00));
6765 tree max = upper_bound_in_type (TREE_TYPE (@10), TREE_TYPE (@00));
6766 bool above = integer_nonzerop (const_binop (LT_EXPR, type, max, @10));
6767 bool below = integer_nonzerop (const_binop (LT_EXPR, type, @10, min));
6769 (if (above || below)
6770 (if (cmp == EQ_EXPR || cmp == NE_EXPR)
6771 { constant_boolean_node (cmp == EQ_EXPR ? false : true, type); }
6772 (if (cmp == LT_EXPR || cmp == LE_EXPR)
6773 { constant_boolean_node (above ? true : false, type); }
6774 (if (cmp == GT_EXPR || cmp == GE_EXPR)
6775 { constant_boolean_node (above ? false : true, type); })))))))))
6776 /* Fold (double)float1 CMP (double)float2 into float1 CMP float2. */
6777 (if (FLOAT_TYPE_P (TREE_TYPE (@00))
6778 && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0))
6779 == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@00)))
6780 && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0))
6781 == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@10))))
6784 tree type1 = TREE_TYPE (@10);
6785 if (TREE_CODE (@10) == REAL_CST && !DECIMAL_FLOAT_TYPE_P (type1))
6787 REAL_VALUE_TYPE orig = TREE_REAL_CST (@10);
6788 if (TYPE_PRECISION (type1) > TYPE_PRECISION (float_type_node)
6789 && exact_real_truncate (TYPE_MODE (float_type_node), &orig))
6790 type1 = float_type_node;
6791 if (TYPE_PRECISION (type1) > TYPE_PRECISION (double_type_node)
6792 && exact_real_truncate (TYPE_MODE (double_type_node), &orig))
6793 type1 = double_type_node;
6796 = (element_precision (TREE_TYPE (@00)) > element_precision (type1)
6797 ? TREE_TYPE (@00) : type1);
6799 (if (element_precision (TREE_TYPE (@0)) > element_precision (newtype)
6800 && is_truth_type_for (newtype, type))
6801 (cmp (convert:newtype @00) (convert:newtype @10))))))))
6806 /* SSA names are canonicalized to 2nd place. */
6807 (cmp addr@0 SSA_NAME@1)
6810 poly_int64 off; tree base;
6811 tree addr = (TREE_CODE (@0) == SSA_NAME
6812 ? gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0)) : @0);
6814 /* A local variable can never be pointed to by
6815 the default SSA name of an incoming parameter. */
6816 (if (SSA_NAME_IS_DEFAULT_DEF (@1)
6817 && TREE_CODE (SSA_NAME_VAR (@1)) == PARM_DECL
6818 && (base = get_base_address (TREE_OPERAND (addr, 0)))
6819 && TREE_CODE (base) == VAR_DECL
6820 && auto_var_in_fn_p (base, current_function_decl))
6821 (if (cmp == NE_EXPR)
6822 { constant_boolean_node (true, type); }
6823 { constant_boolean_node (false, type); })
6824 /* If the address is based on @1 decide using the offset. */
6825 (if ((base = get_addr_base_and_unit_offset (TREE_OPERAND (addr, 0), &off))
6826 && TREE_CODE (base) == MEM_REF
6827 && TREE_OPERAND (base, 0) == @1)
6828 (with { off += mem_ref_offset (base).force_shwi (); }
6829 (if (known_ne (off, 0))
6830 { constant_boolean_node (cmp == NE_EXPR, type); }
6831 (if (known_eq (off, 0))
6832 { constant_boolean_node (cmp == EQ_EXPR, type); }))))))))
6834 /* Equality compare simplifications from fold_binary */
6837 /* If we have (A | C) == D where C & ~D != 0, convert this into 0.
6838 Similarly for NE_EXPR. */
6840 (cmp (convert?@3 (bit_ior @0 INTEGER_CST@1)) INTEGER_CST@2)
6841 (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0))
6842 && wi::bit_and_not (wi::to_wide (@1), wi::to_wide (@2)) != 0)
6843 { constant_boolean_node (cmp == NE_EXPR, type); }))
6845 /* (X ^ Y) == 0 becomes X == Y, and (X ^ Y) != 0 becomes X != Y. */
6847 (cmp (bit_xor @0 @1) integer_zerop)
6850 /* (X ^ Y) == Y becomes X == 0.
6851 Likewise (X ^ Y) == X becomes Y == 0. */
6853 (cmp:c (bit_xor:c @0 @1) @0)
6854 (cmp @1 { build_zero_cst (TREE_TYPE (@1)); }))
6856 /* (X & Y) == X becomes (X & ~Y) == 0. */
6858 (cmp:c (bit_and:c @0 @1) @0)
6859 (cmp (bit_and @0 (bit_not! @1)) { build_zero_cst (TREE_TYPE (@0)); }))
6861 (cmp:c (convert@3 (bit_and (convert@2 @0) INTEGER_CST@1)) (convert @0))
6862 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6863 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
6864 && INTEGRAL_TYPE_P (TREE_TYPE (@3))
6865 && TYPE_PRECISION (TREE_TYPE (@2)) == TYPE_PRECISION (TREE_TYPE (@0))
6866 && TYPE_PRECISION (TREE_TYPE (@3)) > TYPE_PRECISION (TREE_TYPE (@2))
6867 && !wi::neg_p (wi::to_wide (@1)))
6868 (cmp (bit_and @0 (convert (bit_not @1)))
6869 { build_zero_cst (TREE_TYPE (@0)); })))
6871 /* (X | Y) == Y becomes (X & ~Y) == 0. */
6873 (cmp:c (bit_ior:c @0 @1) @1)
6874 (cmp (bit_and @0 (bit_not! @1)) { build_zero_cst (TREE_TYPE (@0)); }))
6876 /* (X ^ C1) op C2 can be rewritten as X op (C1 ^ C2). */
6878 (cmp (convert?@3 (bit_xor @0 INTEGER_CST@1)) INTEGER_CST@2)
6879 (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0)))
6880 (cmp @0 (bit_xor @1 (convert @2)))))
6883 (cmp (nop_convert? @0) integer_zerop)
6884 (if (tree_expr_nonzero_p (@0))
6885 { constant_boolean_node (cmp == NE_EXPR, type); }))
6887 /* (X & C) op (Y & C) into (X ^ Y) & C op 0. */
6889 (cmp (bit_and:cs @0 @2) (bit_and:cs @1 @2))
6890 (cmp (bit_and (bit_xor @0 @1) @2) { build_zero_cst (TREE_TYPE (@2)); })))
6892 /* (X < 0) != (Y < 0) into (X ^ Y) < 0.
6893 (X >= 0) != (Y >= 0) into (X ^ Y) < 0.
6894 (X < 0) == (Y < 0) into (X ^ Y) >= 0.
6895 (X >= 0) == (Y >= 0) into (X ^ Y) >= 0. */
6900 (cmp (sgncmp @0 integer_zerop@2) (sgncmp @1 integer_zerop))
6901 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
6902 && !TYPE_UNSIGNED (TREE_TYPE (@0))
6903 && types_match (@0, @1))
6904 (ncmp (bit_xor @0 @1) @2)))))
6905 /* (X < 0) == (Y >= 0) into (X ^ Y) < 0.
6906 (X < 0) != (Y >= 0) into (X ^ Y) >= 0. */
6910 (cmp:c (lt @0 integer_zerop@2) (ge @1 integer_zerop))
6911 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
6912 && !TYPE_UNSIGNED (TREE_TYPE (@0))
6913 && types_match (@0, @1))
6914 (ncmp (bit_xor @0 @1) @2))))
6916 /* If we have (A & C) == C where C is a power of 2, convert this into
6917 (A & C) != 0. Similarly for NE_EXPR. */
6921 (cmp (bit_and@2 @0 integer_pow2p@1) @1)
6922 (icmp @2 { build_zero_cst (TREE_TYPE (@0)); })))
6925 /* From fold_binary_op_with_conditional_arg handle the case of
6926 rewriting (a ? b : c) > d to a ? (b > d) : (c > d) when the
6927 compares simplify. */
6928 (for cmp (simple_comparison)
6930 (cmp:c (cond @0 @1 @2) @3)
6931 /* Do not move possibly trapping operations into the conditional as this
6932 pessimizes code and causes gimplification issues when applied late. */
6933 (if (!FLOAT_TYPE_P (TREE_TYPE (@3))
6934 || !operation_could_trap_p (cmp, true, false, @3))
6935 (cond @0 (cmp! @1 @3) (cmp! @2 @3)))))
6939 /* x < 0 ? ~y : y into (x >> (prec-1)) ^ y. */
6940 /* x >= 0 ? ~y : y into ~((x >> (prec-1)) ^ y). */
6942 (cond (cmp @0 integer_zerop) (bit_not @1) @1)
6943 (if (INTEGRAL_TYPE_P (type)
6944 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
6945 && !TYPE_UNSIGNED (TREE_TYPE (@0))
6946 && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (type))
6949 tree shifter = build_int_cst (integer_type_node, TYPE_PRECISION (type) - 1);
6951 (if (cmp == LT_EXPR)
6952 (bit_xor (convert (rshift @0 {shifter;})) @1)
6953 (bit_not (bit_xor (convert (rshift @0 {shifter;})) @1))))))
6954 /* x < 0 ? y : ~y into ~((x >> (prec-1)) ^ y). */
6955 /* x >= 0 ? y : ~y into (x >> (prec-1)) ^ y. */
6957 (cond (cmp @0 integer_zerop) @1 (bit_not @1))
6958 (if (INTEGRAL_TYPE_P (type)
6959 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
6960 && !TYPE_UNSIGNED (TREE_TYPE (@0))
6961 && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (type))
6964 tree shifter = build_int_cst (integer_type_node, TYPE_PRECISION (type) - 1);
6966 (if (cmp == GE_EXPR)
6967 (bit_xor (convert (rshift @0 {shifter;})) @1)
6968 (bit_not (bit_xor (convert (rshift @0 {shifter;})) @1)))))))
6970 /* If we have (A & C) != 0 ? D : 0 where C and D are powers of 2,
6971 convert this into a shift followed by ANDing with D. */
6974 (ne (bit_and @0 integer_pow2p@1) integer_zerop)
6975 INTEGER_CST@2 integer_zerop)
6976 (if (!POINTER_TYPE_P (type) && integer_pow2p (@2))
6978 int shift = (wi::exact_log2 (wi::to_wide (@2))
6979 - wi::exact_log2 (wi::to_wide (@1)));
6983 (lshift (convert @0) { build_int_cst (integer_type_node, shift); }) @2)
6985 (convert (rshift @0 { build_int_cst (integer_type_node, -shift); }))
6988 /* If we have (A & C) != 0 where C is the sign bit of A, convert
6989 this into A < 0. Similarly for (A & C) == 0 into A >= 0. */
6993 (cmp (bit_and (convert?@2 @0) integer_pow2p@1) integer_zerop)
6994 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6995 && type_has_mode_precision_p (TREE_TYPE (@0))
6996 && element_precision (@2) >= element_precision (@0)
6997 && wi::only_sign_bit_p (wi::to_wide (@1), element_precision (@0)))
6998 (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
6999 (ncmp (convert:stype @0) { build_zero_cst (stype); })))))
7001 /* If we have A < 0 ? C : 0 where C is a power of 2, convert
7002 this into a right shift or sign extension followed by ANDing with C. */
7005 (lt @0 integer_zerop)
7006 INTEGER_CST@1 integer_zerop)
7007 (if (integer_pow2p (@1)
7008 && !TYPE_UNSIGNED (TREE_TYPE (@0)))
7010 int shift = element_precision (@0) - wi::exact_log2 (wi::to_wide (@1)) - 1;
7014 (convert (rshift @0 { build_int_cst (integer_type_node, shift); }))
7016 /* Otherwise ctype must be wider than TREE_TYPE (@0) and pure
7017 sign extension followed by AND with C will achieve the effect. */
7018 (bit_and (convert @0) @1)))))
7020 /* When the addresses are not directly of decls compare base and offset.
7021 This implements some remaining parts of fold_comparison address
7022 comparisons but still no complete part of it. Still it is good
7023 enough to make fold_stmt not regress when not dispatching to fold_binary. */
7024 (for cmp (simple_comparison)
7026 (cmp (convert1?@2 addr@0) (convert2? addr@1))
7029 poly_int64 off0, off1;
7031 int equal = address_compare (cmp, TREE_TYPE (@2), @0, @1, base0, base1,
7032 off0, off1, GENERIC);
7036 (if (cmp == EQ_EXPR && (known_eq (off0, off1) || known_ne (off0, off1)))
7037 { constant_boolean_node (known_eq (off0, off1), type); })
7038 (if (cmp == NE_EXPR && (known_eq (off0, off1) || known_ne (off0, off1)))
7039 { constant_boolean_node (known_ne (off0, off1), type); })
7040 (if (cmp == LT_EXPR && (known_lt (off0, off1) || known_ge (off0, off1)))
7041 { constant_boolean_node (known_lt (off0, off1), type); })
7042 (if (cmp == LE_EXPR && (known_le (off0, off1) || known_gt (off0, off1)))
7043 { constant_boolean_node (known_le (off0, off1), type); })
7044 (if (cmp == GE_EXPR && (known_ge (off0, off1) || known_lt (off0, off1)))
7045 { constant_boolean_node (known_ge (off0, off1), type); })
7046 (if (cmp == GT_EXPR && (known_gt (off0, off1) || known_le (off0, off1)))
7047 { constant_boolean_node (known_gt (off0, off1), type); }))
7050 (if (cmp == EQ_EXPR)
7051 { constant_boolean_node (false, type); })
7052 (if (cmp == NE_EXPR)
7053 { constant_boolean_node (true, type); })))))))
7056 /* a?~t:t -> (-(a))^t */
7059 (with { bool wascmp; }
7060 (if (INTEGRAL_TYPE_P (type)
7061 && bitwise_inverted_equal_p (@1, @2, wascmp)
7062 && (!wascmp || TYPE_PRECISION (type) == 1))
7063 (if ((!TYPE_UNSIGNED (type) && TREE_CODE (type) == BOOLEAN_TYPE)
7064 || TYPE_PRECISION (type) == 1)
7065 (bit_xor (convert:type @0) @2)
7066 (bit_xor (negate (convert:type @0)) @2)))))
7069 /* Simplify pointer equality compares using PTA. */
7073 (if (POINTER_TYPE_P (TREE_TYPE (@0))
7074 && ptrs_compare_unequal (@0, @1))
7075 { constant_boolean_node (neeq != EQ_EXPR, type); })))
7077 /* PR70920: Transform (intptr_t)x eq/ne CST to x eq/ne (typeof x) CST.
7078 and (typeof ptr_cst) x eq/ne ptr_cst to x eq/ne (typeof x) CST.
7079 Disable the transform if either operand is pointer to function.
7080 This broke pr22051-2.c for arm where function pointer
7081 canonicalizaion is not wanted. */
7085 (cmp (convert @0) INTEGER_CST@1)
7086 (if (((POINTER_TYPE_P (TREE_TYPE (@0))
7087 && !FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@0)))
7088 && INTEGRAL_TYPE_P (TREE_TYPE (@1))
7089 /* Don't perform this optimization in GENERIC if @0 has reference
7090 type when sanitizing. See PR101210. */
7092 && TREE_CODE (TREE_TYPE (@0)) == REFERENCE_TYPE
7093 && (flag_sanitize & (SANITIZE_NULL | SANITIZE_ALIGNMENT))))
7094 || (INTEGRAL_TYPE_P (TREE_TYPE (@0))
7095 && POINTER_TYPE_P (TREE_TYPE (@1))
7096 && !FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@1)))))
7097 && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
7098 (cmp @0 (convert @1)))))
7100 /* Non-equality compare simplifications from fold_binary */
7101 (for cmp (lt gt le ge)
7102 /* Comparisons with the highest or lowest possible integer of
7103 the specified precision will have known values. */
7105 (cmp (convert?@2 @0) uniform_integer_cst_p@1)
7106 (if ((INTEGRAL_TYPE_P (TREE_TYPE (@1))
7107 || POINTER_TYPE_P (TREE_TYPE (@1))
7108 || VECTOR_INTEGER_TYPE_P (TREE_TYPE (@1)))
7109 && tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@0)))
7112 tree cst = uniform_integer_cst_p (@1);
7113 tree arg1_type = TREE_TYPE (cst);
7114 unsigned int prec = TYPE_PRECISION (arg1_type);
7115 wide_int max = wi::max_value (arg1_type);
7116 wide_int signed_max = wi::max_value (prec, SIGNED);
7117 wide_int min = wi::min_value (arg1_type);
7120 (if (wi::to_wide (cst) == max)
7122 (if (cmp == GT_EXPR)
7123 { constant_boolean_node (false, type); })
7124 (if (cmp == GE_EXPR)
7126 (if (cmp == LE_EXPR)
7127 { constant_boolean_node (true, type); })
7128 (if (cmp == LT_EXPR)
7130 (if (wi::to_wide (cst) == min)
7132 (if (cmp == LT_EXPR)
7133 { constant_boolean_node (false, type); })
7134 (if (cmp == LE_EXPR)
7136 (if (cmp == GE_EXPR)
7137 { constant_boolean_node (true, type); })
7138 (if (cmp == GT_EXPR)
7140 (if (wi::to_wide (cst) == max - 1)
7142 (if (cmp == GT_EXPR)
7143 (eq @2 { build_uniform_cst (TREE_TYPE (@1),
7144 wide_int_to_tree (TREE_TYPE (cst),
7147 (if (cmp == LE_EXPR)
7148 (ne @2 { build_uniform_cst (TREE_TYPE (@1),
7149 wide_int_to_tree (TREE_TYPE (cst),
7152 (if (wi::to_wide (cst) == min + 1)
7154 (if (cmp == GE_EXPR)
7155 (ne @2 { build_uniform_cst (TREE_TYPE (@1),
7156 wide_int_to_tree (TREE_TYPE (cst),
7159 (if (cmp == LT_EXPR)
7160 (eq @2 { build_uniform_cst (TREE_TYPE (@1),
7161 wide_int_to_tree (TREE_TYPE (cst),
7164 (if (wi::to_wide (cst) == signed_max
7165 && TYPE_UNSIGNED (arg1_type)
7166 && TYPE_MODE (arg1_type) != BLKmode
7167 /* We will flip the signedness of the comparison operator
7168 associated with the mode of @1, so the sign bit is
7169 specified by this mode. Check that @1 is the signed
7170 max associated with this sign bit. */
7171 && prec == GET_MODE_PRECISION (SCALAR_INT_TYPE_MODE (arg1_type))
7172 /* signed_type does not work on pointer types. */
7173 && INTEGRAL_TYPE_P (arg1_type))
7174 /* The following case also applies to X < signed_max+1
7175 and X >= signed_max+1 because previous transformations. */
7176 (if (cmp == LE_EXPR || cmp == GT_EXPR)
7177 (with { tree st = signed_type_for (TREE_TYPE (@1)); }
7179 (if (cst == @1 && cmp == LE_EXPR)
7180 (ge (convert:st @0) { build_zero_cst (st); }))
7181 (if (cst == @1 && cmp == GT_EXPR)
7182 (lt (convert:st @0) { build_zero_cst (st); }))
7183 (if (cmp == LE_EXPR)
7184 (ge (view_convert:st @0) { build_zero_cst (st); }))
7185 (if (cmp == GT_EXPR)
7186 (lt (view_convert:st @0) { build_zero_cst (st); })))))))))))
7188 /* unsigned < (typeof unsigned)(unsigned != 0) is always false. */
7190 (lt:c @0 (convert (ne @0 integer_zerop)))
7191 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
7192 { constant_boolean_node (false, type); }))
7194 /* x != (typeof x)(x == CST) -> CST == 0 ? 1 : (CST == 1 ? (x!=0&&x!=1) : x != 0) */
7195 /* x != (typeof x)(x != CST) -> CST == 1 ? 1 : (CST == 0 ? (x!=0&&x!=1) : x != 1) */
7196 /* x == (typeof x)(x == CST) -> CST == 0 ? 0 : (CST == 1 ? (x==0||x==1) : x == 0) */
7197 /* x == (typeof x)(x != CST) -> CST == 1 ? 0 : (CST == 0 ? (x==0||x==1) : x == 1) */
7201 (outer:c @0 (convert (inner @0 INTEGER_CST@1)))
7203 bool cst1 = integer_onep (@1);
7204 bool cst0 = integer_zerop (@1);
7205 bool innereq = inner == EQ_EXPR;
7206 bool outereq = outer == EQ_EXPR;
7209 (if (innereq ? cst0 : cst1)
7210 { constant_boolean_node (!outereq, type); })
7211 (if (innereq ? cst1 : cst0)
7213 tree utype = unsigned_type_for (TREE_TYPE (@0));
7214 tree ucst1 = build_one_cst (utype);
7217 (gt (convert:utype @0) { ucst1; })
7218 (le (convert:utype @0) { ucst1; })
7223 tree value = build_int_cst (TREE_TYPE (@0), !innereq);
7236 (for cmp (unordered ordered unlt unle ungt unge uneq ltgt)
7237 /* If the second operand is NaN, the result is constant. */
7240 (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
7241 && (cmp != LTGT_EXPR || ! flag_trapping_math))
7242 { constant_boolean_node (cmp == ORDERED_EXPR || cmp == LTGT_EXPR
7243 ? false : true, type); })))
7245 /* Fold UNORDERED if either operand must be NaN, or neither can be. */
7249 (if (tree_expr_nan_p (@0) || tree_expr_nan_p (@1))
7250 { constant_boolean_node (true, type); })
7251 (if (!tree_expr_maybe_nan_p (@0) && !tree_expr_maybe_nan_p (@1))
7252 { constant_boolean_node (false, type); })))
7254 /* Fold ORDERED if either operand must be NaN, or neither can be. */
7258 (if (tree_expr_nan_p (@0) || tree_expr_nan_p (@1))
7259 { constant_boolean_node (false, type); })
7260 (if (!tree_expr_maybe_nan_p (@0) && !tree_expr_maybe_nan_p (@1))
7261 { constant_boolean_node (true, type); })))
7263 /* bool_var != 0 becomes bool_var. */
7265 (ne @0 integer_zerop)
7266 (if (TREE_CODE (TREE_TYPE (@0)) == BOOLEAN_TYPE
7267 && types_match (type, TREE_TYPE (@0)))
7269 /* bool_var == 1 becomes bool_var. */
7271 (eq @0 integer_onep)
7272 (if (TREE_CODE (TREE_TYPE (@0)) == BOOLEAN_TYPE
7273 && types_match (type, TREE_TYPE (@0)))
7276 bool_var == 0 becomes !bool_var or
7277 bool_var != 1 becomes !bool_var
7278 here because that only is good in assignment context as long
7279 as we require a tcc_comparison in GIMPLE_CONDs where we'd
7280 replace if (x == 0) with tem = ~x; if (tem != 0) which is
7281 clearly less optimal and which we'll transform again in forwprop. */
7283 /* Transform comparisons of the form (X & Y) CMP 0 to X CMP2 Z
7284 where ~Y + 1 == pow2 and Z = ~Y. */
7285 (for cst (VECTOR_CST INTEGER_CST)
7289 (cmp (bit_and:c@2 @0 cst@1) integer_zerop)
7290 (with { tree csts = bitmask_inv_cst_vector_p (@1); }
7291 (if (csts && (VECTOR_TYPE_P (TREE_TYPE (@1)) || single_use (@2)))
7292 (with { auto optab = VECTOR_TYPE_P (TREE_TYPE (@1))
7293 ? optab_vector : optab_default;
7294 tree utype = unsigned_type_for (TREE_TYPE (@1)); }
7295 (if (target_supports_op_p (utype, icmp, optab)
7296 || (optimize_vectors_before_lowering_p ()
7297 && (!target_supports_op_p (type, cmp, optab)
7298 || !target_supports_op_p (type, BIT_AND_EXPR, optab))))
7299 (if (TYPE_UNSIGNED (TREE_TYPE (@1)))
7301 (icmp (view_convert:utype @0) { csts; })))))))))
7303 /* When one argument is a constant, overflow detection can be simplified.
7304 Currently restricted to single use so as not to interfere too much with
7305 ADD_OVERFLOW detection in tree-ssa-math-opts.cc.
7306 CONVERT?(CONVERT?(A) + CST) CMP A -> A CMP' CST' */
7307 (for cmp (lt le ge gt)
7310 (cmp:c (convert?@3 (plus@2 (convert?@4 @0) INTEGER_CST@1)) @0)
7311 (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@2))
7312 && types_match (TREE_TYPE (@0), TREE_TYPE (@3))
7313 && tree_nop_conversion_p (TREE_TYPE (@4), TREE_TYPE (@0))
7314 && wi::to_wide (@1) != 0
7317 unsigned int prec = TYPE_PRECISION (TREE_TYPE (@0));
7318 signop sign = TYPE_SIGN (TREE_TYPE (@0));
7320 (out @0 { wide_int_to_tree (TREE_TYPE (@0),
7321 wi::max_value (prec, sign)
7322 - wi::to_wide (@1)); })))))
7324 /* To detect overflow in unsigned A - B, A < B is simpler than A - B > A.
7325 However, the detection logic for SUB_OVERFLOW in tree-ssa-math-opts.cc
7326 expects the long form, so we restrict the transformation for now. */
7329 (cmp:c (minus@2 @0 @1) @0)
7330 (if (single_use (@2)
7331 && ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
7332 && TYPE_UNSIGNED (TREE_TYPE (@0)))
7335 /* Optimize A - B + -1 >= A into B >= A for unsigned comparisons. */
7338 (cmp:c (plus (minus @0 @1) integer_minus_onep) @0)
7339 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
7340 && TYPE_UNSIGNED (TREE_TYPE (@0)))
7343 /* Testing for overflow is unnecessary if we already know the result. */
7348 (cmp:c (realpart (IFN_SUB_OVERFLOW@2 @0 @1)) @0)
7349 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
7350 && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
7351 (out (imagpart @2) { build_zero_cst (TREE_TYPE (@0)); }))))
7356 (cmp:c (realpart (IFN_ADD_OVERFLOW:c@2 @0 @1)) @0)
7357 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
7358 && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
7359 (out (imagpart @2) { build_zero_cst (TREE_TYPE (@0)); }))))
7361 /* For unsigned operands, -1 / B < A checks whether A * B would overflow.
7362 Simplify it to __builtin_mul_overflow (A, B, <unused>). */
7366 (cmp:c (trunc_div:s integer_all_onesp @1) @0)
7367 (if (TYPE_UNSIGNED (TREE_TYPE (@0)) && !VECTOR_TYPE_P (TREE_TYPE (@0)))
7368 (with { tree t = TREE_TYPE (@0), cpx = build_complex_type (t); }
7369 (out (imagpart (IFN_MUL_OVERFLOW:cpx @0 @1)) { build_zero_cst (t); })))))
7371 /* Similarly, for unsigned operands, (((type) A * B) >> prec) != 0 where type
7372 is at least twice as wide as type of A and B, simplify to
7373 __builtin_mul_overflow (A, B, <unused>). */
7376 (cmp (rshift (mult:s (convert@3 @0) (convert @1)) INTEGER_CST@2)
7378 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
7379 && INTEGRAL_TYPE_P (TREE_TYPE (@3))
7380 && TYPE_UNSIGNED (TREE_TYPE (@0))
7381 && (TYPE_PRECISION (TREE_TYPE (@3))
7382 >= 2 * TYPE_PRECISION (TREE_TYPE (@0)))
7383 && tree_fits_uhwi_p (@2)
7384 && tree_to_uhwi (@2) == TYPE_PRECISION (TREE_TYPE (@0))
7385 && types_match (@0, @1)
7386 && type_has_mode_precision_p (TREE_TYPE (@0))
7387 && (optab_handler (umulv4_optab, TYPE_MODE (TREE_TYPE (@0)))
7388 != CODE_FOR_nothing))
7389 (with { tree t = TREE_TYPE (@0), cpx = build_complex_type (t); }
7390 (cmp (imagpart (IFN_MUL_OVERFLOW:cpx @0 @1)) { build_zero_cst (t); })))))
7392 /* Demote operands of IFN_{ADD,SUB,MUL}_OVERFLOW. */
7393 (for ovf (IFN_ADD_OVERFLOW IFN_SUB_OVERFLOW IFN_MUL_OVERFLOW)
7395 (ovf (convert@2 @0) @1)
7396 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
7397 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
7398 && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
7399 && (!TYPE_UNSIGNED (TREE_TYPE (@2)) || TYPE_UNSIGNED (TREE_TYPE (@0))))
7402 (ovf @1 (convert@2 @0))
7403 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
7404 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
7405 && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
7406 && (!TYPE_UNSIGNED (TREE_TYPE (@2)) || TYPE_UNSIGNED (TREE_TYPE (@0))))
7409 /* Optimize __builtin_mul_overflow_p (x, cst, (utype) 0) if all 3 types
7410 are unsigned to x > (umax / cst). Similarly for signed type, but
7411 in that case it needs to be outside of a range. */
7413 (imagpart (IFN_MUL_OVERFLOW:cs@2 @0 integer_nonzerop@1))
7414 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
7415 && TYPE_MAX_VALUE (TREE_TYPE (@0))
7416 && types_match (TREE_TYPE (@0), TREE_TYPE (TREE_TYPE (@2)))
7417 && int_fits_type_p (@1, TREE_TYPE (@0)))
7418 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
7419 (convert (gt @0 (trunc_div! { TYPE_MAX_VALUE (TREE_TYPE (@0)); } @1)))
7420 (if (TYPE_MIN_VALUE (TREE_TYPE (@0)))
7421 (if (integer_minus_onep (@1))
7422 (convert (eq @0 { TYPE_MIN_VALUE (TREE_TYPE (@0)); }))
7425 tree div = fold_convert (TREE_TYPE (@0), @1);
7426 tree lo = int_const_binop (TRUNC_DIV_EXPR,
7427 TYPE_MIN_VALUE (TREE_TYPE (@0)), div);
7428 tree hi = int_const_binop (TRUNC_DIV_EXPR,
7429 TYPE_MAX_VALUE (TREE_TYPE (@0)), div);
7430 tree etype = range_check_type (TREE_TYPE (@0));
7433 if (wi::neg_p (wi::to_wide (div)))
7435 lo = fold_convert (etype, lo);
7436 hi = fold_convert (etype, hi);
7437 hi = int_const_binop (MINUS_EXPR, hi, lo);
7441 (convert (gt (minus (convert:etype @0) { lo; }) { hi; })))))))))
7443 /* Simplification of math builtins. These rules must all be optimizations
7444 as well as IL simplifications. If there is a possibility that the new
7445 form could be a pessimization, the rule should go in the canonicalization
7446 section that follows this one.
7448 Rules can generally go in this section if they satisfy one of
7451 - the rule describes an identity
7453 - the rule replaces calls with something as simple as addition or
7456 - the rule contains unary calls only and simplifies the surrounding
7457 arithmetic. (The idea here is to exclude non-unary calls in which
7458 one operand is constant and in which the call is known to be cheap
7459 when the operand has that value.) */
7461 (if (flag_unsafe_math_optimizations)
7462 /* Simplify sqrt(x) * sqrt(x) -> x. */
7464 (mult (SQRT_ALL@1 @0) @1)
7465 (if (!tree_expr_maybe_signaling_nan_p (@0))
7468 (for op (plus minus)
7469 /* Simplify (A / C) +- (B / C) -> (A +- B) / C. */
7473 (rdiv (op @0 @2) @1)))
7475 (for cmp (lt le gt ge)
7476 neg_cmp (gt ge lt le)
7477 /* Simplify (x * C1) cmp C2 -> x cmp (C2 / C1), where C1 != 0. */
7479 (cmp (mult @0 REAL_CST@1) REAL_CST@2)
7481 { tree tem = const_binop (RDIV_EXPR, type, @2, @1); }
7483 && !(REAL_VALUE_ISINF (TREE_REAL_CST (tem))
7484 || (real_zerop (tem) && !real_zerop (@1))))
7486 (if (real_less (&dconst0, TREE_REAL_CST_PTR (@1)))
7488 (if (real_less (TREE_REAL_CST_PTR (@1), &dconst0))
7489 (neg_cmp @0 { tem; })))))))
7491 /* Simplify sqrt(x) * sqrt(y) -> sqrt(x*y). */
7492 (for root (SQRT CBRT)
7494 (mult (root:s @0) (root:s @1))
7495 (root (mult @0 @1))))
7497 /* Simplify expN(x) * expN(y) -> expN(x+y). */
7498 (for exps (EXP EXP2 EXP10 POW10)
7500 (mult (exps:s @0) (exps:s @1))
7501 (exps (plus @0 @1))))
7503 /* Simplify a/root(b/c) into a*root(c/b). */
7504 (for root (SQRT CBRT)
7506 (rdiv @0 (root:s (rdiv:s @1 @2)))
7507 (mult @0 (root (rdiv @2 @1)))))
7509 /* Simplify x/expN(y) into x*expN(-y). */
7510 (for exps (EXP EXP2 EXP10 POW10)
7512 (rdiv @0 (exps:s @1))
7513 (mult @0 (exps (negate @1)))))
7515 (for logs (LOG LOG2 LOG10 LOG10)
7516 exps (EXP EXP2 EXP10 POW10)
7517 /* logN(expN(x)) -> x. */
7521 /* expN(logN(x)) -> x. */
7526 /* Optimize logN(func()) for various exponential functions. We
7527 want to determine the value "x" and the power "exponent" in
7528 order to transform logN(x**exponent) into exponent*logN(x). */
7529 (for logs (LOG LOG LOG LOG2 LOG2 LOG2 LOG10 LOG10)
7530 exps (EXP2 EXP10 POW10 EXP EXP10 POW10 EXP EXP2)
7533 (if (SCALAR_FLOAT_TYPE_P (type))
7539 /* Prepare to do logN(exp(exponent)) -> exponent*logN(e). */
7540 x = build_real_truncate (type, dconst_e ());
7543 /* Prepare to do logN(exp2(exponent)) -> exponent*logN(2). */
7544 x = build_real (type, dconst2);
7548 /* Prepare to do logN(exp10(exponent)) -> exponent*logN(10). */
7550 REAL_VALUE_TYPE dconst10;
7551 real_from_integer (&dconst10, VOIDmode, 10, SIGNED);
7552 x = build_real (type, dconst10);
7559 (mult (logs { x; }) @0)))))
7567 (if (SCALAR_FLOAT_TYPE_P (type))
7573 /* Prepare to do logN(sqrt(x)) -> 0.5*logN(x). */
7574 x = build_real (type, dconsthalf);
7577 /* Prepare to do logN(cbrt(x)) -> (1/3)*logN(x). */
7578 x = build_real_truncate (type, dconst_third ());
7584 (mult { x; } (logs @0))))))
7586 /* logN(pow(x,exponent)) -> exponent*logN(x). */
7587 (for logs (LOG LOG2 LOG10)
7591 (mult @1 (logs @0))))
7593 /* pow(C,x) -> exp(log(C)*x) if C > 0,
7594 or if C is a positive power of 2,
7595 pow(C,x) -> exp2(log2(C)*x). */
7603 (pows REAL_CST@0 @1)
7604 (if (real_compare (GT_EXPR, TREE_REAL_CST_PTR (@0), &dconst0)
7605 && real_isfinite (TREE_REAL_CST_PTR (@0))
7606 /* As libmvec doesn't have a vectorized exp2, defer optimizing
7607 the use_exp2 case until after vectorization. It seems actually
7608 beneficial for all constants to postpone this until later,
7609 because exp(log(C)*x), while faster, will have worse precision
7610 and if x folds into a constant too, that is unnecessary
7612 && canonicalize_math_after_vectorization_p ())
7614 const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (@0);
7615 bool use_exp2 = false;
7616 if (targetm.libc_has_function (function_c99_misc, TREE_TYPE (@0))
7617 && value->cl == rvc_normal)
7619 REAL_VALUE_TYPE frac_rvt = *value;
7620 SET_REAL_EXP (&frac_rvt, 1);
7621 if (real_equal (&frac_rvt, &dconst1))
7626 (if (optimize_pow_to_exp (@0, @1))
7627 (exps (mult (logs @0) @1)))
7628 (exp2s (mult (log2s @0) @1)))))))
7631 /* pow(C,x)*expN(y) -> expN(logN(C)*x+y) if C > 0. */
7633 exps (EXP EXP2 EXP10 POW10)
7634 logs (LOG LOG2 LOG10 LOG10)
7636 (mult:c (pows:s REAL_CST@0 @1) (exps:s @2))
7637 (if (real_compare (GT_EXPR, TREE_REAL_CST_PTR (@0), &dconst0)
7638 && real_isfinite (TREE_REAL_CST_PTR (@0)))
7639 (exps (plus (mult (logs @0) @1) @2)))))
7644 exps (EXP EXP2 EXP10 POW10)
7645 /* sqrt(expN(x)) -> expN(x*0.5). */
7648 (exps (mult @0 { build_real (type, dconsthalf); })))
7649 /* cbrt(expN(x)) -> expN(x/3). */
7652 (exps (mult @0 { build_real_truncate (type, dconst_third ()); })))
7653 /* pow(expN(x), y) -> expN(x*y). */
7656 (exps (mult @0 @1))))
7658 /* tan(atan(x)) -> x. */
7665 /* Simplify sin(atan(x)) -> x / sqrt(x*x + 1). */
7669 copysigns (COPYSIGN)
7674 REAL_VALUE_TYPE r_cst;
7675 build_sinatan_real (&r_cst, type);
7676 tree t_cst = build_real (type, r_cst);
7677 tree t_one = build_one_cst (type);
7679 (if (SCALAR_FLOAT_TYPE_P (type))
7680 (cond (lt (abs @0) { t_cst; })
7681 (rdiv @0 (sqrts (plus (mult @0 @0) { t_one; })))
7682 (copysigns { t_one; } @0))))))
7684 /* Simplify cos(atan(x)) -> 1 / sqrt(x*x + 1). */
7688 copysigns (COPYSIGN)
7693 REAL_VALUE_TYPE r_cst;
7694 build_sinatan_real (&r_cst, type);
7695 tree t_cst = build_real (type, r_cst);
7696 tree t_one = build_one_cst (type);
7697 tree t_zero = build_zero_cst (type);
7699 (if (SCALAR_FLOAT_TYPE_P (type))
7700 (cond (lt (abs @0) { t_cst; })
7701 (rdiv { t_one; } (sqrts (plus (mult @0 @0) { t_one; })))
7702 (copysigns { t_zero; } @0))))))
7704 (if (!flag_errno_math)
7705 /* Simplify sinh(atanh(x)) -> x / sqrt((1 - x)*(1 + x)). */
7710 (sinhs (atanhs:s @0))
7711 (with { tree t_one = build_one_cst (type); }
7712 (rdiv @0 (sqrts (mult (minus { t_one; } @0) (plus { t_one; } @0)))))))
7714 /* Simplify cosh(atanh(x)) -> 1 / sqrt((1 - x)*(1 + x)) */
7719 (coshs (atanhs:s @0))
7720 (with { tree t_one = build_one_cst (type); }
7721 (rdiv { t_one; } (sqrts (mult (minus { t_one; } @0) (plus { t_one; } @0))))))))
7723 /* cabs(x+0i) or cabs(0+xi) -> abs(x). */
7725 (CABS (complex:C @0 real_zerop@1))
7728 /* trunc(trunc(x)) -> trunc(x), etc. */
7729 (for fns (TRUNC_ALL FLOOR_ALL CEIL_ALL ROUND_ALL NEARBYINT_ALL RINT_ALL)
7733 /* f(x) -> x if x is integer valued and f does nothing for such values. */
7734 (for fns (TRUNC_ALL FLOOR_ALL CEIL_ALL ROUND_ALL NEARBYINT_ALL RINT_ALL)
7736 (fns integer_valued_real_p@0)
7739 /* hypot(x,0) and hypot(0,x) -> abs(x). */
7741 (HYPOT:c @0 real_zerop@1)
7744 /* pow(1,x) -> 1. */
7746 (POW real_onep@0 @1)
7750 /* copysign(x,x) -> x. */
7751 (COPYSIGN_ALL @0 @0)
7755 /* copysign(x,-x) -> -x. */
7756 (COPYSIGN_ALL @0 (negate@1 @0))
7760 /* copysign(x,y) -> fabs(x) if y is nonnegative. */
7761 (COPYSIGN_ALL @0 tree_expr_nonnegative_p@1)
7765 /* fabs (copysign(x, y)) -> fabs (x). */
7766 (abs (COPYSIGN_ALL @0 @1))
7769 (for scale (LDEXP SCALBN SCALBLN)
7770 /* ldexp(0, x) -> 0. */
7772 (scale real_zerop@0 @1)
7774 /* ldexp(x, 0) -> x. */
7776 (scale @0 integer_zerop@1)
7778 /* ldexp(x, y) -> x if x is +-Inf or NaN. */
7780 (scale REAL_CST@0 @1)
7781 (if (!real_isfinite (TREE_REAL_CST_PTR (@0)))
7784 /* Canonicalization of sequences of math builtins. These rules represent
7785 IL simplifications but are not necessarily optimizations.
7787 The sincos pass is responsible for picking "optimal" implementations
7788 of math builtins, which may be more complicated and can sometimes go
7789 the other way, e.g. converting pow into a sequence of sqrts.
7790 We only want to do these canonicalizations before the pass has run. */
7792 (if (flag_unsafe_math_optimizations && canonicalize_math_p ())
7793 /* Simplify tan(x) * cos(x) -> sin(x). */
7795 (mult:c (TAN:s @0) (COS:s @0))
7798 /* Simplify x * pow(x,c) -> pow(x,c+1). */
7800 (mult:c @0 (POW:s @0 REAL_CST@1))
7801 (if (!TREE_OVERFLOW (@1))
7802 (POW @0 (plus @1 { build_one_cst (type); }))))
7804 /* Simplify sin(x) / cos(x) -> tan(x). */
7806 (rdiv (SIN:s @0) (COS:s @0))
7809 /* Simplify sinh(x) / cosh(x) -> tanh(x). */
7811 (rdiv (SINH:s @0) (COSH:s @0))
7814 /* Simplify tanh (x) / sinh (x) -> 1.0 / cosh (x). */
7816 (rdiv (TANH:s @0) (SINH:s @0))
7817 (rdiv {build_one_cst (type);} (COSH @0)))
7819 /* Simplify cos(x) / sin(x) -> 1 / tan(x). */
7821 (rdiv (COS:s @0) (SIN:s @0))
7822 (rdiv { build_one_cst (type); } (TAN @0)))
7824 /* Simplify sin(x) / tan(x) -> cos(x). */
7826 (rdiv (SIN:s @0) (TAN:s @0))
7827 (if (! HONOR_NANS (@0)
7828 && ! HONOR_INFINITIES (@0))
7831 /* Simplify tan(x) / sin(x) -> 1.0 / cos(x). */
7833 (rdiv (TAN:s @0) (SIN:s @0))
7834 (if (! HONOR_NANS (@0)
7835 && ! HONOR_INFINITIES (@0))
7836 (rdiv { build_one_cst (type); } (COS @0))))
7838 /* Simplify pow(x,y) * pow(x,z) -> pow(x,y+z). */
7840 (mult (POW:s @0 @1) (POW:s @0 @2))
7841 (POW @0 (plus @1 @2)))
7843 /* Simplify pow(x,y) * pow(z,y) -> pow(x*z,y). */
7845 (mult (POW:s @0 @1) (POW:s @2 @1))
7846 (POW (mult @0 @2) @1))
7848 /* Simplify powi(x,y) * powi(z,y) -> powi(x*z,y). */
7850 (mult (POWI:s @0 @1) (POWI:s @2 @1))
7851 (POWI (mult @0 @2) @1))
7853 /* Simplify pow(x,c) / x -> pow(x,c-1). */
7855 (rdiv (POW:s @0 REAL_CST@1) @0)
7856 (if (!TREE_OVERFLOW (@1))
7857 (POW @0 (minus @1 { build_one_cst (type); }))))
7859 /* Simplify x / pow (y,z) -> x * pow(y,-z). */
7861 (rdiv @0 (POW:s @1 @2))
7862 (mult @0 (POW @1 (negate @2))))
7867 /* sqrt(sqrt(x)) -> pow(x,1/4). */
7870 (pows @0 { build_real (type, dconst_quarter ()); }))
7871 /* sqrt(cbrt(x)) -> pow(x,1/6). */
7874 (pows @0 { build_real_truncate (type, dconst_sixth ()); }))
7875 /* cbrt(sqrt(x)) -> pow(x,1/6). */
7878 (pows @0 { build_real_truncate (type, dconst_sixth ()); }))
7879 /* cbrt(cbrt(x)) -> pow(x,1/9), iff x is nonnegative. */
7881 (cbrts (cbrts tree_expr_nonnegative_p@0))
7882 (pows @0 { build_real_truncate (type, dconst_ninth ()); }))
7883 /* sqrt(pow(x,y)) -> pow(|x|,y*0.5). */
7885 (sqrts (pows @0 @1))
7886 (pows (abs @0) (mult @1 { build_real (type, dconsthalf); })))
7887 /* cbrt(pow(x,y)) -> pow(x,y/3), iff x is nonnegative. */
7889 (cbrts (pows tree_expr_nonnegative_p@0 @1))
7890 (pows @0 (mult @1 { build_real_truncate (type, dconst_third ()); })))
7891 /* pow(sqrt(x),y) -> pow(x,y*0.5). */
7893 (pows (sqrts @0) @1)
7894 (pows @0 (mult @1 { build_real (type, dconsthalf); })))
7895 /* pow(cbrt(x),y) -> pow(x,y/3) iff x is nonnegative. */
7897 (pows (cbrts tree_expr_nonnegative_p@0) @1)
7898 (pows @0 (mult @1 { build_real_truncate (type, dconst_third ()); })))
7899 /* pow(pow(x,y),z) -> pow(x,y*z) iff x is nonnegative. */
7901 (pows (pows tree_expr_nonnegative_p@0 @1) @2)
7902 (pows @0 (mult @1 @2))))
7904 /* cabs(x+xi) -> fabs(x)*sqrt(2). */
7906 (CABS (complex @0 @0))
7907 (mult (abs @0) { build_real_truncate (type, dconst_sqrt2 ()); }))
7909 /* hypot(x,x) -> fabs(x)*sqrt(2). */
7912 (mult (abs @0) { build_real_truncate (type, dconst_sqrt2 ()); }))
7914 /* cexp(x+yi) -> exp(x)*cexpi(y). */
7919 (cexps compositional_complex@0)
7920 (if (targetm.libc_has_function (function_c99_math_complex, TREE_TYPE (@0)))
7922 (mult (exps@1 (realpart @0)) (realpart (cexpis:type@2 (imagpart @0))))
7923 (mult @1 (imagpart @2)))))))
7925 (if (canonicalize_math_p ())
7926 /* floor(x) -> trunc(x) if x is nonnegative. */
7927 (for floors (FLOOR_ALL)
7930 (floors tree_expr_nonnegative_p@0)
7933 (match double_value_p
7935 (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == double_type_node)))
7936 (for froms (BUILT_IN_TRUNCL
7948 /* truncl(extend(x)) -> extend(trunc(x)), etc., if x is a double. */
7949 (if (optimize && canonicalize_math_p ())
7951 (froms (convert double_value_p@0))
7952 (convert (tos @0)))))
7954 (match float_value_p
7956 (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == float_type_node)))
7957 (for froms (BUILT_IN_TRUNCL BUILT_IN_TRUNC
7958 BUILT_IN_FLOORL BUILT_IN_FLOOR
7959 BUILT_IN_CEILL BUILT_IN_CEIL
7960 BUILT_IN_ROUNDL BUILT_IN_ROUND
7961 BUILT_IN_NEARBYINTL BUILT_IN_NEARBYINT
7962 BUILT_IN_RINTL BUILT_IN_RINT)
7963 tos (BUILT_IN_TRUNCF BUILT_IN_TRUNCF
7964 BUILT_IN_FLOORF BUILT_IN_FLOORF
7965 BUILT_IN_CEILF BUILT_IN_CEILF
7966 BUILT_IN_ROUNDF BUILT_IN_ROUNDF
7967 BUILT_IN_NEARBYINTF BUILT_IN_NEARBYINTF
7968 BUILT_IN_RINTF BUILT_IN_RINTF)
7969 /* truncl(extend(x)) and trunc(extend(x)) -> extend(truncf(x)), etc.,
7971 (if (optimize && canonicalize_math_p ()
7972 && targetm.libc_has_function (function_c99_misc, NULL_TREE))
7974 (froms (convert float_value_p@0))
7975 (convert (tos @0)))))
7978 (match float16_value_p
7980 (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == float16_type_node)))
7981 (for froms (BUILT_IN_TRUNCL BUILT_IN_TRUNC BUILT_IN_TRUNCF
7982 BUILT_IN_FLOORL BUILT_IN_FLOOR BUILT_IN_FLOORF
7983 BUILT_IN_CEILL BUILT_IN_CEIL BUILT_IN_CEILF
7984 BUILT_IN_ROUNDEVENL BUILT_IN_ROUNDEVEN BUILT_IN_ROUNDEVENF
7985 BUILT_IN_ROUNDL BUILT_IN_ROUND BUILT_IN_ROUNDF
7986 BUILT_IN_NEARBYINTL BUILT_IN_NEARBYINT BUILT_IN_NEARBYINTF
7987 BUILT_IN_RINTL BUILT_IN_RINT BUILT_IN_RINTF
7988 BUILT_IN_SQRTL BUILT_IN_SQRT BUILT_IN_SQRTF)
7989 tos (IFN_TRUNC IFN_TRUNC IFN_TRUNC
7990 IFN_FLOOR IFN_FLOOR IFN_FLOOR
7991 IFN_CEIL IFN_CEIL IFN_CEIL
7992 IFN_ROUNDEVEN IFN_ROUNDEVEN IFN_ROUNDEVEN
7993 IFN_ROUND IFN_ROUND IFN_ROUND
7994 IFN_NEARBYINT IFN_NEARBYINT IFN_NEARBYINT
7995 IFN_RINT IFN_RINT IFN_RINT
7996 IFN_SQRT IFN_SQRT IFN_SQRT)
7997 /* (_Float16) round ((doube) x) -> __built_in_roundf16 (x), etc.,
7998 if x is a _Float16. */
8000 (convert (froms (convert float16_value_p@0)))
8002 && types_match (type, TREE_TYPE (@0))
8003 && direct_internal_fn_supported_p (as_internal_fn (tos),
8004 type, OPTIMIZE_FOR_BOTH))
8007 /* Simplify (trunc)copysign ((extend)x, (extend)y) to copysignf (x, y),
8008 x,y is float value, similar for _Float16/double. */
8009 (for copysigns (COPYSIGN_ALL)
8011 (convert (copysigns (convert@2 @0) (convert @1)))
8013 && !HONOR_SNANS (@2)
8014 && types_match (type, TREE_TYPE (@0))
8015 && types_match (type, TREE_TYPE (@1))
8016 && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (@2))
8017 && direct_internal_fn_supported_p (IFN_COPYSIGN,
8018 type, OPTIMIZE_FOR_BOTH))
8019 (IFN_COPYSIGN @0 @1))))
8021 (for froms (BUILT_IN_FMAF BUILT_IN_FMA BUILT_IN_FMAL)
8022 tos (IFN_FMA IFN_FMA IFN_FMA)
8024 (convert (froms (convert@3 @0) (convert @1) (convert @2)))
8025 (if (flag_unsafe_math_optimizations
8027 && FLOAT_TYPE_P (type)
8028 && FLOAT_TYPE_P (TREE_TYPE (@3))
8029 && types_match (type, TREE_TYPE (@0))
8030 && types_match (type, TREE_TYPE (@1))
8031 && types_match (type, TREE_TYPE (@2))
8032 && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (@3))
8033 && direct_internal_fn_supported_p (as_internal_fn (tos),
8034 type, OPTIMIZE_FOR_BOTH))
8037 (for maxmin (max min)
8039 (convert (maxmin (convert@2 @0) (convert @1)))
8041 && FLOAT_TYPE_P (type)
8042 && FLOAT_TYPE_P (TREE_TYPE (@2))
8043 && types_match (type, TREE_TYPE (@0))
8044 && types_match (type, TREE_TYPE (@1))
8045 && element_precision (type) < element_precision (TREE_TYPE (@2)))
8049 (for froms (XFLOORL XCEILL XROUNDL XRINTL)
8050 tos (XFLOOR XCEIL XROUND XRINT)
8051 /* llfloorl(extend(x)) -> llfloor(x), etc., if x is a double. */
8052 (if (optimize && canonicalize_math_p ())
8054 (froms (convert double_value_p@0))
8057 (for froms (XFLOORL XCEILL XROUNDL XRINTL
8058 XFLOOR XCEIL XROUND XRINT)
8059 tos (XFLOORF XCEILF XROUNDF XRINTF)
8060 /* llfloorl(extend(x)) and llfloor(extend(x)) -> llfloorf(x), etc.,
8062 (if (optimize && canonicalize_math_p ())
8064 (froms (convert float_value_p@0))
8067 (if (canonicalize_math_p ())
8068 /* xfloor(x) -> fix_trunc(x) if x is nonnegative. */
8069 (for floors (IFLOOR LFLOOR LLFLOOR)
8071 (floors tree_expr_nonnegative_p@0)
8074 (if (canonicalize_math_p ())
8075 /* xfloor(x) -> fix_trunc(x), etc., if x is integer valued. */
8076 (for fns (IFLOOR LFLOOR LLFLOOR
8078 IROUND LROUND LLROUND)
8080 (fns integer_valued_real_p@0)
8082 (if (!flag_errno_math)
8083 /* xrint(x) -> fix_trunc(x), etc., if x is integer valued. */
8084 (for rints (IRINT LRINT LLRINT)
8086 (rints integer_valued_real_p@0)
8089 (if (canonicalize_math_p ())
8090 (for ifn (IFLOOR ICEIL IROUND IRINT)
8091 lfn (LFLOOR LCEIL LROUND LRINT)
8092 llfn (LLFLOOR LLCEIL LLROUND LLRINT)
8093 /* Canonicalize iround (x) to lround (x) on ILP32 targets where
8094 sizeof (int) == sizeof (long). */
8095 (if (TYPE_PRECISION (integer_type_node)
8096 == TYPE_PRECISION (long_integer_type_node))
8099 (lfn:long_integer_type_node @0)))
8100 /* Canonicalize llround (x) to lround (x) on LP64 targets where
8101 sizeof (long long) == sizeof (long). */
8102 (if (TYPE_PRECISION (long_long_integer_type_node)
8103 == TYPE_PRECISION (long_integer_type_node))
8106 (lfn:long_integer_type_node @0)))))
8108 /* cproj(x) -> x if we're ignoring infinities. */
8111 (if (!HONOR_INFINITIES (type))
8114 /* If the real part is inf and the imag part is known to be
8115 nonnegative, return (inf + 0i). */
8117 (CPROJ (complex REAL_CST@0 tree_expr_nonnegative_p@1))
8118 (if (real_isinf (TREE_REAL_CST_PTR (@0)))
8119 { build_complex_inf (type, false); }))
8121 /* If the imag part is inf, return (inf+I*copysign(0,imag)). */
8123 (CPROJ (complex @0 REAL_CST@1))
8124 (if (real_isinf (TREE_REAL_CST_PTR (@1)))
8125 { build_complex_inf (type, TREE_REAL_CST_PTR (@1)->sign); }))
8131 (pows @0 REAL_CST@1)
8133 const REAL_VALUE_TYPE *value = TREE_REAL_CST_PTR (@1);
8134 REAL_VALUE_TYPE tmp;
8137 /* pow(x,0) -> 1. */
8138 (if (real_equal (value, &dconst0))
8139 { build_real (type, dconst1); })
8140 /* pow(x,1) -> x. */
8141 (if (real_equal (value, &dconst1))
8143 /* pow(x,-1) -> 1/x. */
8144 (if (real_equal (value, &dconstm1))
8145 (rdiv { build_real (type, dconst1); } @0))
8146 /* pow(x,0.5) -> sqrt(x). */
8147 (if (flag_unsafe_math_optimizations
8148 && canonicalize_math_p ()
8149 && real_equal (value, &dconsthalf))
8151 /* pow(x,1/3) -> cbrt(x). */
8152 (if (flag_unsafe_math_optimizations
8153 && canonicalize_math_p ()
8154 && (tmp = real_value_truncate (TYPE_MODE (type), dconst_third ()),
8155 real_equal (value, &tmp)))
8158 /* powi(1,x) -> 1. */
8160 (POWI real_onep@0 @1)
8164 (POWI @0 INTEGER_CST@1)
8166 /* powi(x,0) -> 1. */
8167 (if (wi::to_wide (@1) == 0)
8168 { build_real (type, dconst1); })
8169 /* powi(x,1) -> x. */
8170 (if (wi::to_wide (@1) == 1)
8172 /* powi(x,-1) -> 1/x. */
8173 (if (wi::to_wide (@1) == -1)
8174 (rdiv { build_real (type, dconst1); } @0))))
8176 /* Narrowing of arithmetic and logical operations.
8178 These are conceptually similar to the transformations performed for
8179 the C/C++ front-ends by shorten_binary_op and shorten_compare. Long
8180 term we want to move all that code out of the front-ends into here. */
8182 /* Convert (outertype)((innertype0)a+(innertype1)b)
8183 into ((newtype)a+(newtype)b) where newtype
8184 is the widest mode from all of these. */
8185 (for op (plus minus mult rdiv)
8187 (convert (op:s@0 (convert1?@3 @1) (convert2?@4 @2)))
8188 /* If we have a narrowing conversion of an arithmetic operation where
8189 both operands are widening conversions from the same type as the outer
8190 narrowing conversion. Then convert the innermost operands to a
8191 suitable unsigned type (to avoid introducing undefined behavior),
8192 perform the operation and convert the result to the desired type. */
8193 (if (INTEGRAL_TYPE_P (type)
8196 /* We check for type compatibility between @0 and @1 below,
8197 so there's no need to check that @2/@4 are integral types. */
8198 && INTEGRAL_TYPE_P (TREE_TYPE (@1))
8199 && INTEGRAL_TYPE_P (TREE_TYPE (@3))
8200 /* The precision of the type of each operand must match the
8201 precision of the mode of each operand, similarly for the
8203 && type_has_mode_precision_p (TREE_TYPE (@1))
8204 && type_has_mode_precision_p (TREE_TYPE (@2))
8205 && type_has_mode_precision_p (type)
8206 /* The inner conversion must be a widening conversion. */
8207 && TYPE_PRECISION (TREE_TYPE (@3)) > TYPE_PRECISION (TREE_TYPE (@1))
8208 && types_match (@1, type)
8209 && (types_match (@1, @2)
8210 /* Or the second operand is const integer or converted const
8211 integer from valueize. */
8212 || poly_int_tree_p (@4)))
8213 (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
8214 (op @1 (convert @2))
8215 (with { tree utype = unsigned_type_for (TREE_TYPE (@1)); }
8216 (convert (op (convert:utype @1)
8217 (convert:utype @2)))))
8218 (if (FLOAT_TYPE_P (type)
8219 && DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0))
8220 == DECIMAL_FLOAT_TYPE_P (type))
8221 (with { tree arg0 = strip_float_extensions (@1);
8222 tree arg1 = strip_float_extensions (@2);
8223 tree itype = TREE_TYPE (@0);
8224 tree ty1 = TREE_TYPE (arg0);
8225 tree ty2 = TREE_TYPE (arg1);
8226 enum tree_code code = TREE_CODE (itype); }
8227 (if (FLOAT_TYPE_P (ty1)
8228 && FLOAT_TYPE_P (ty2))
8229 (with { tree newtype = type;
8230 if (TYPE_MODE (ty1) == SDmode
8231 || TYPE_MODE (ty2) == SDmode
8232 || TYPE_MODE (type) == SDmode)
8233 newtype = dfloat32_type_node;
8234 if (TYPE_MODE (ty1) == DDmode
8235 || TYPE_MODE (ty2) == DDmode
8236 || TYPE_MODE (type) == DDmode)
8237 newtype = dfloat64_type_node;
8238 if (TYPE_MODE (ty1) == TDmode
8239 || TYPE_MODE (ty2) == TDmode
8240 || TYPE_MODE (type) == TDmode)
8241 newtype = dfloat128_type_node; }
8242 (if ((newtype == dfloat32_type_node
8243 || newtype == dfloat64_type_node
8244 || newtype == dfloat128_type_node)
8246 && types_match (newtype, type))
8247 (op (convert:newtype @1) (convert:newtype @2))
8248 (with { if (element_precision (ty1) > element_precision (newtype))
8250 if (element_precision (ty2) > element_precision (newtype))
8252 /* Sometimes this transformation is safe (cannot
8253 change results through affecting double rounding
8254 cases) and sometimes it is not. If NEWTYPE is
8255 wider than TYPE, e.g. (float)((long double)double
8256 + (long double)double) converted to
8257 (float)(double + double), the transformation is
8258 unsafe regardless of the details of the types
8259 involved; double rounding can arise if the result
8260 of NEWTYPE arithmetic is a NEWTYPE value half way
8261 between two representable TYPE values but the
8262 exact value is sufficiently different (in the
8263 right direction) for this difference to be
8264 visible in ITYPE arithmetic. If NEWTYPE is the
8265 same as TYPE, however, the transformation may be
8266 safe depending on the types involved: it is safe
8267 if the ITYPE has strictly more than twice as many
8268 mantissa bits as TYPE, can represent infinities
8269 and NaNs if the TYPE can, and has sufficient
8270 exponent range for the product or ratio of two
8271 values representable in the TYPE to be within the
8272 range of normal values of ITYPE. */
8273 (if (element_precision (newtype) < element_precision (itype)
8274 && (!VECTOR_MODE_P (TYPE_MODE (newtype))
8275 || target_supports_op_p (newtype, op, optab_default))
8276 && (flag_unsafe_math_optimizations
8277 || (element_precision (newtype) == element_precision (type)
8278 && real_can_shorten_arithmetic (element_mode (itype),
8279 element_mode (type))
8280 && !excess_precision_type (newtype)))
8281 && !types_match (itype, newtype))
8282 (convert:type (op (convert:newtype @1)
8283 (convert:newtype @2)))
8288 /* This is another case of narrowing, specifically when there's an outer
8289 BIT_AND_EXPR which masks off bits outside the type of the innermost
8290 operands. Like the previous case we have to convert the operands
8291 to unsigned types to avoid introducing undefined behavior for the
8292 arithmetic operation. */
8293 (for op (minus plus)
8295 (bit_and (op:s (convert@2 @0) (convert@3 @1)) INTEGER_CST@4)
8296 (if (INTEGRAL_TYPE_P (type)
8297 /* We check for type compatibility between @0 and @1 below,
8298 so there's no need to check that @1/@3 are integral types. */
8299 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
8300 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
8301 /* The precision of the type of each operand must match the
8302 precision of the mode of each operand, similarly for the
8304 && type_has_mode_precision_p (TREE_TYPE (@0))
8305 && type_has_mode_precision_p (TREE_TYPE (@1))
8306 && type_has_mode_precision_p (type)
8307 /* The inner conversion must be a widening conversion. */
8308 && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
8309 && types_match (@0, @1)
8310 && (tree_int_cst_min_precision (@4, TYPE_SIGN (TREE_TYPE (@0)))
8311 <= TYPE_PRECISION (TREE_TYPE (@0)))
8312 && (wi::to_wide (@4)
8313 & wi::mask (TYPE_PRECISION (TREE_TYPE (@0)),
8314 true, TYPE_PRECISION (type))) == 0)
8315 (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
8316 (with { tree ntype = TREE_TYPE (@0); }
8317 (convert (bit_and (op @0 @1) (convert:ntype @4))))
8318 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
8319 (convert (bit_and (op (convert:utype @0) (convert:utype @1))
8320 (convert:utype @4))))))))
8322 /* Transform (@0 < @1 and @0 < @2) to use min,
8323 (@0 > @1 and @0 > @2) to use max */
8324 (for logic (bit_and bit_and bit_and bit_and bit_ior bit_ior bit_ior bit_ior)
8325 op (lt le gt ge lt le gt ge )
8326 ext (min min max max max max min min )
8328 (logic (op:cs @0 @1) (op:cs @0 @2))
8329 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
8330 && TREE_CODE (@0) != INTEGER_CST)
8331 (op @0 (ext @1 @2)))))
8333 /* Max<bool0, bool1> -> bool0 | bool1
8334 Min<bool0, bool1> -> bool0 & bool1 */
8336 logic (bit_ior bit_and)
8338 (op zero_one_valued_p@0 zero_one_valued_p@1)
8341 /* signbit(x) != 0 ? -x : x -> abs(x)
8342 signbit(x) == 0 ? -x : x -> -abs(x) */
8346 (cond (neeq (sign @0) integer_zerop) (negate @0) @0)
8347 (if (neeq == NE_EXPR)
8349 (negate (abs @0))))))
8352 /* signbit(x) -> 0 if x is nonnegative. */
8353 (SIGNBIT tree_expr_nonnegative_p@0)
8354 { integer_zero_node; })
8357 /* signbit(x) -> x<0 if x doesn't have signed zeros. */
8359 (if (!HONOR_SIGNED_ZEROS (@0))
8360 (convert (lt @0 { build_real (TREE_TYPE (@0), dconst0); }))))
8362 /* Transform comparisons of the form X +- C1 CMP C2 to X CMP C2 -+ C1. */
8364 (for op (plus minus)
8367 (cmp (op@3 @0 INTEGER_CST@1) INTEGER_CST@2)
8368 (if (!TREE_OVERFLOW (@1) && !TREE_OVERFLOW (@2)
8369 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0))
8370 && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@0))
8371 && !TYPE_SATURATING (TREE_TYPE (@0)))
8372 (with { tree res = int_const_binop (rop, @2, @1); }
8373 (if (TREE_OVERFLOW (res)
8374 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
8375 { constant_boolean_node (cmp == NE_EXPR, type); }
8376 (if (single_use (@3))
8377 (cmp @0 { TREE_OVERFLOW (res)
8378 ? drop_tree_overflow (res) : res; }))))))))
8379 (for cmp (lt le gt ge)
8380 (for op (plus minus)
8383 (cmp (op@3 @0 INTEGER_CST@1) INTEGER_CST@2)
8384 (if (!TREE_OVERFLOW (@1) && !TREE_OVERFLOW (@2)
8385 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
8386 (with { tree res = int_const_binop (rop, @2, @1); }
8387 (if (TREE_OVERFLOW (res))
8389 fold_overflow_warning (("assuming signed overflow does not occur "
8390 "when simplifying conditional to constant"),
8391 WARN_STRICT_OVERFLOW_CONDITIONAL);
8392 bool less = cmp == LE_EXPR || cmp == LT_EXPR;
8393 /* wi::ges_p (@2, 0) should be sufficient for a signed type. */
8394 bool ovf_high = wi::lt_p (wi::to_wide (@1), 0,
8395 TYPE_SIGN (TREE_TYPE (@1)))
8396 != (op == MINUS_EXPR);
8397 constant_boolean_node (less == ovf_high, type);
8399 (if (single_use (@3))
8402 fold_overflow_warning (("assuming signed overflow does not occur "
8403 "when changing X +- C1 cmp C2 to "
8405 WARN_STRICT_OVERFLOW_COMPARISON);
8407 (cmp @0 { res; })))))))))
8409 /* Canonicalizations of BIT_FIELD_REFs. */
8412 (BIT_FIELD_REF (BIT_FIELD_REF @0 @1 @2) @3 @4)
8413 (BIT_FIELD_REF @0 @3 { const_binop (PLUS_EXPR, bitsizetype, @2, @4); }))
8416 (BIT_FIELD_REF (view_convert @0) @1 @2)
8417 (if (! INTEGRAL_TYPE_P (TREE_TYPE (@0))
8418 || type_has_mode_precision_p (TREE_TYPE (@0)))
8419 (BIT_FIELD_REF @0 @1 @2)))
8422 (BIT_FIELD_REF @0 @1 integer_zerop)
8423 (if (tree_int_cst_equal (@1, TYPE_SIZE (TREE_TYPE (@0))))
8427 (BIT_FIELD_REF @0 @1 @2)
8429 (if (TREE_CODE (TREE_TYPE (@0)) == COMPLEX_TYPE
8430 && tree_int_cst_equal (@1, TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))))
8432 (if (integer_zerop (@2))
8433 (view_convert (realpart @0)))
8434 (if (tree_int_cst_equal (@2, TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))))
8435 (view_convert (imagpart @0)))))
8436 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
8437 && INTEGRAL_TYPE_P (type)
8438 /* On GIMPLE this should only apply to register arguments. */
8439 && (! GIMPLE || is_gimple_reg (@0))
8440 /* A bit-field-ref that referenced the full argument can be stripped. */
8441 && ((compare_tree_int (@1, TYPE_PRECISION (TREE_TYPE (@0))) == 0
8442 && integer_zerop (@2))
8443 /* Low-parts can be reduced to integral conversions.
8444 ??? The following doesn't work for PDP endian. */
8445 || (BYTES_BIG_ENDIAN == WORDS_BIG_ENDIAN
8446 /* But only do this after vectorization. */
8447 && canonicalize_math_after_vectorization_p ()
8448 /* Don't even think about BITS_BIG_ENDIAN. */
8449 && TYPE_PRECISION (TREE_TYPE (@0)) % BITS_PER_UNIT == 0
8450 && TYPE_PRECISION (type) % BITS_PER_UNIT == 0
8451 && compare_tree_int (@2, (BYTES_BIG_ENDIAN
8452 ? (TYPE_PRECISION (TREE_TYPE (@0))
8453 - TYPE_PRECISION (type))
8457 /* Simplify vector extracts. */
8460 (BIT_FIELD_REF CONSTRUCTOR@0 @1 @2)
8461 (if (VECTOR_TYPE_P (TREE_TYPE (@0))
8462 && tree_fits_uhwi_p (TYPE_SIZE (type))
8463 && ((tree_to_uhwi (TYPE_SIZE (type))
8464 == tree_to_uhwi (TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))))
8465 || (VECTOR_TYPE_P (type)
8466 && (tree_to_uhwi (TYPE_SIZE (TREE_TYPE (type)))
8467 == tree_to_uhwi (TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0))))))))
8470 tree ctor = (TREE_CODE (@0) == SSA_NAME
8471 ? gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0)) : @0);
8472 tree eltype = TREE_TYPE (TREE_TYPE (ctor));
8473 unsigned HOST_WIDE_INT width = tree_to_uhwi (TYPE_SIZE (eltype));
8474 unsigned HOST_WIDE_INT n = tree_to_uhwi (@1);
8475 unsigned HOST_WIDE_INT idx = tree_to_uhwi (@2);
8478 && (idx % width) == 0
8480 && known_le ((idx + n) / width,
8481 TYPE_VECTOR_SUBPARTS (TREE_TYPE (ctor))))
8486 /* Constructor elements can be subvectors. */
8488 if (CONSTRUCTOR_NELTS (ctor) != 0)
8490 tree cons_elem = TREE_TYPE (CONSTRUCTOR_ELT (ctor, 0)->value);
8491 if (TREE_CODE (cons_elem) == VECTOR_TYPE)
8492 k = TYPE_VECTOR_SUBPARTS (cons_elem);
8494 unsigned HOST_WIDE_INT elt, count, const_k;
8497 /* We keep an exact subset of the constructor elements. */
8498 (if (multiple_p (idx, k, &elt) && multiple_p (n, k, &count))
8499 (if (CONSTRUCTOR_NELTS (ctor) == 0)
8500 { build_zero_cst (type); }
8502 (if (elt < CONSTRUCTOR_NELTS (ctor))
8503 (view_convert { CONSTRUCTOR_ELT (ctor, elt)->value; })
8504 { build_zero_cst (type); })
8505 /* We don't want to emit new CTORs unless the old one goes away.
8506 ??? Eventually allow this if the CTOR ends up constant or
8508 (if (single_use (@0))
8511 vec<constructor_elt, va_gc> *vals;
8512 vec_alloc (vals, count);
8513 bool constant_p = true;
8515 for (unsigned i = 0;
8516 i < count && elt + i < CONSTRUCTOR_NELTS (ctor); ++i)
8518 tree e = CONSTRUCTOR_ELT (ctor, elt + i)->value;
8519 CONSTRUCTOR_APPEND_ELT (vals, NULL_TREE, e);
8520 if (!CONSTANT_CLASS_P (e))
8523 tree evtype = (types_match (TREE_TYPE (type),
8524 TREE_TYPE (TREE_TYPE (ctor)))
8526 : build_vector_type (TREE_TYPE (TREE_TYPE (ctor)),
8528 /* We used to build a CTOR in the non-constant case here
8529 but that's not a GIMPLE value. We'd have to expose this
8530 operation somehow so the code generation can properly
8531 split it out to a separate stmt. */
8532 res = (constant_p ? build_vector_from_ctor (evtype, vals)
8533 : (GIMPLE ? NULL_TREE : build_constructor (evtype, vals)));
8536 (view_convert { res; })))))))
8537 /* The bitfield references a single constructor element. */
8538 (if (k.is_constant (&const_k)
8539 && idx + n <= (idx / const_k + 1) * const_k)
8541 (if (CONSTRUCTOR_NELTS (ctor) <= idx / const_k)
8542 { build_zero_cst (type); })
8544 (view_convert { CONSTRUCTOR_ELT (ctor, idx / const_k)->value; }))
8545 (BIT_FIELD_REF { CONSTRUCTOR_ELT (ctor, idx / const_k)->value; }
8546 @1 { bitsize_int ((idx % const_k) * width); })))))))))
8548 /* Simplify a bit extraction from a bit insertion for the cases with
8549 the inserted element fully covering the extraction or the insertion
8550 not touching the extraction. */
8552 (BIT_FIELD_REF (bit_insert @0 @1 @ipos) @rsize @rpos)
8555 unsigned HOST_WIDE_INT isize;
8556 if (INTEGRAL_TYPE_P (TREE_TYPE (@1)))
8557 isize = TYPE_PRECISION (TREE_TYPE (@1));
8559 isize = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (@1)));
8562 (if ((!INTEGRAL_TYPE_P (TREE_TYPE (@1))
8563 || type_has_mode_precision_p (TREE_TYPE (@1)))
8564 && wi::leu_p (wi::to_wide (@ipos), wi::to_wide (@rpos))
8565 && wi::leu_p (wi::to_wide (@rpos) + wi::to_wide (@rsize),
8566 wi::to_wide (@ipos) + isize))
8567 (BIT_FIELD_REF @1 @rsize { wide_int_to_tree (bitsizetype,
8569 - wi::to_wide (@ipos)); }))
8570 (if (wi::eq_p (wi::to_wide (@ipos), wi::to_wide (@rpos))
8571 && compare_tree_int (@rsize, isize) == 0)
8573 (if (wi::geu_p (wi::to_wide (@ipos),
8574 wi::to_wide (@rpos) + wi::to_wide (@rsize))
8575 || wi::geu_p (wi::to_wide (@rpos),
8576 wi::to_wide (@ipos) + isize))
8577 (BIT_FIELD_REF @0 @rsize @rpos)))))
8579 /* Simplify vector inserts of other vector extracts to a permute. */
8581 (bit_insert @0 (BIT_FIELD_REF@2 @1 @rsize @rpos) @ipos)
8582 (if (VECTOR_TYPE_P (type)
8583 && (VECTOR_MODE_P (TYPE_MODE (type))
8584 || optimize_vectors_before_lowering_p ())
8585 && types_match (@0, @1)
8586 && types_match (TREE_TYPE (TREE_TYPE (@0)), TREE_TYPE (@2))
8587 && TYPE_VECTOR_SUBPARTS (type).is_constant ())
8590 unsigned HOST_WIDE_INT elsz
8591 = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (TREE_TYPE (@1))));
8592 poly_uint64 relt = exact_div (tree_to_poly_uint64 (@rpos), elsz);
8593 poly_uint64 ielt = exact_div (tree_to_poly_uint64 (@ipos), elsz);
8594 unsigned nunits = TYPE_VECTOR_SUBPARTS (type).to_constant ();
8595 vec_perm_builder builder;
8596 builder.new_vector (nunits, nunits, 1);
8597 for (unsigned i = 0; i < nunits; ++i)
8598 builder.quick_push (known_eq (ielt, i) ? nunits + relt : i);
8599 vec_perm_indices sel (builder, 2, nunits);
8601 (if (!VECTOR_MODE_P (TYPE_MODE (type))
8602 || can_vec_perm_const_p (TYPE_MODE (type), TYPE_MODE (type), sel, false))
8603 (vec_perm @0 @1 { vec_perm_indices_to_tree
8604 (build_vector_type (ssizetype, nunits), sel); })))))
8606 (if (canonicalize_math_after_vectorization_p ())
8609 (fmas:c (negate @0) @1 @2)
8610 (IFN_FNMA @0 @1 @2))
8612 (fmas @0 @1 (negate @2))
8615 (fmas:c (negate @0) @1 (negate @2))
8616 (IFN_FNMS @0 @1 @2))
8618 (negate (fmas@3 @0 @1 @2))
8619 (if (single_use (@3))
8620 (IFN_FNMS @0 @1 @2))))
8623 (IFN_FMS:c (negate @0) @1 @2)
8624 (IFN_FNMS @0 @1 @2))
8626 (IFN_FMS @0 @1 (negate @2))
8629 (IFN_FMS:c (negate @0) @1 (negate @2))
8630 (IFN_FNMA @0 @1 @2))
8632 (negate (IFN_FMS@3 @0 @1 @2))
8633 (if (single_use (@3))
8634 (IFN_FNMA @0 @1 @2)))
8637 (IFN_FNMA:c (negate @0) @1 @2)
8640 (IFN_FNMA @0 @1 (negate @2))
8641 (IFN_FNMS @0 @1 @2))
8643 (IFN_FNMA:c (negate @0) @1 (negate @2))
8646 (negate (IFN_FNMA@3 @0 @1 @2))
8647 (if (single_use (@3))
8648 (IFN_FMS @0 @1 @2)))
8651 (IFN_FNMS:c (negate @0) @1 @2)
8654 (IFN_FNMS @0 @1 (negate @2))
8655 (IFN_FNMA @0 @1 @2))
8657 (IFN_FNMS:c (negate @0) @1 (negate @2))
8660 (negate (IFN_FNMS@3 @0 @1 @2))
8661 (if (single_use (@3))
8662 (IFN_FMA @0 @1 @2))))
8664 /* CLZ simplifications. */
8669 (op (clz:s@2 @0) INTEGER_CST@1)
8670 (if (integer_zerop (@1) && single_use (@2))
8671 /* clz(X) == 0 is (int)X < 0 and clz(X) != 0 is (int)X >= 0. */
8672 (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
8673 (cmp (convert:stype @0) { build_zero_cst (stype); }))
8674 /* clz(X) == (prec-1) is X == 1 and clz(X) != (prec-1) is X != 1. */
8675 (if (wi::to_wide (@1) == TYPE_PRECISION (TREE_TYPE (@0)) - 1)
8676 (op @0 { build_one_cst (TREE_TYPE (@0)); }))))))
8680 (op (IFN_CLZ:s@2 @0 @3) INTEGER_CST@1)
8681 (if (integer_zerop (@1) && single_use (@2))
8682 /* clz(X) == 0 is (int)X < 0 and clz(X) != 0 is (int)X >= 0. */
8683 (with { tree type0 = TREE_TYPE (@0);
8684 tree stype = signed_type_for (TREE_TYPE (@0));
8685 /* Punt if clz(0) == 0. */
8686 if (integer_zerop (@3))
8690 (cmp (convert:stype @0) { build_zero_cst (stype); })))
8691 /* clz(X) == (prec-1) is X == 1 and clz(X) != (prec-1) is X != 1. */
8692 (with { bool ok = true;
8693 tree type0 = TREE_TYPE (@0);
8694 /* Punt if clz(0) == prec - 1. */
8695 if (wi::to_widest (@3) == TYPE_PRECISION (type0) - 1)
8698 (if (ok && wi::to_wide (@1) == (TYPE_PRECISION (type0) - 1))
8699 (op @0 { build_one_cst (type0); }))))))
8701 /* CTZ simplifications. */
8703 (for op (ge gt le lt)
8706 /* __builtin_ctz (x) >= C -> (x & ((1 << C) - 1)) == 0. */
8707 (op (ctz:s @0) INTEGER_CST@1)
8708 (with { bool ok = true;
8709 HOST_WIDE_INT val = 0;
8710 if (!tree_fits_shwi_p (@1))
8714 val = tree_to_shwi (@1);
8715 /* Canonicalize to >= or <. */
8716 if (op == GT_EXPR || op == LE_EXPR)
8718 if (val == HOST_WIDE_INT_MAX)
8724 tree type0 = TREE_TYPE (@0);
8725 int prec = TYPE_PRECISION (type0);
8727 (if (ok && prec <= MAX_FIXED_MODE_SIZE)
8729 { constant_boolean_node (cmp == EQ_EXPR ? true : false, type); }
8731 { constant_boolean_node (cmp == EQ_EXPR ? false : true, type); }
8732 (cmp (bit_and @0 { wide_int_to_tree (type0,
8733 wi::mask (val, false, prec)); })
8734 { build_zero_cst (type0); })))))))
8737 /* __builtin_ctz (x) == C -> (x & ((1 << (C + 1)) - 1)) == (1 << C). */
8738 (op (ctz:s @0) INTEGER_CST@1)
8739 (with { tree type0 = TREE_TYPE (@0);
8740 int prec = TYPE_PRECISION (type0);
8742 (if (prec <= MAX_FIXED_MODE_SIZE)
8743 (if (tree_int_cst_sgn (@1) < 0 || wi::to_widest (@1) >= prec)
8744 { constant_boolean_node (op == EQ_EXPR ? false : true, type); }
8745 (op (bit_and @0 { wide_int_to_tree (type0,
8746 wi::mask (tree_to_uhwi (@1) + 1,
8748 { wide_int_to_tree (type0,
8749 wi::shifted_mask (tree_to_uhwi (@1), 1,
8750 false, prec)); })))))))
8751 (for op (ge gt le lt)
8754 /* __builtin_ctz (x) >= C -> (x & ((1 << C) - 1)) == 0. */
8755 (op (IFN_CTZ:s @0 @2) INTEGER_CST@1)
8756 (with { bool ok = true;
8757 HOST_WIDE_INT val = 0;
8758 if (!tree_fits_shwi_p (@1))
8762 val = tree_to_shwi (@1);
8763 /* Canonicalize to >= or <. */
8764 if (op == GT_EXPR || op == LE_EXPR)
8766 if (val == HOST_WIDE_INT_MAX)
8772 HOST_WIDE_INT zero_val = tree_to_shwi (@2);
8773 tree type0 = TREE_TYPE (@0);
8774 int prec = TYPE_PRECISION (type0);
8775 if (prec > MAX_FIXED_MODE_SIZE)
8779 (if (ok && zero_val >= val)
8780 { constant_boolean_node (cmp == EQ_EXPR ? true : false, type); })
8782 (if (ok && zero_val < val)
8783 { constant_boolean_node (cmp == EQ_EXPR ? false : true, type); })
8784 (if (ok && (zero_val < 0 || zero_val >= prec))
8785 (cmp (bit_and @0 { wide_int_to_tree (type0,
8786 wi::mask (val, false, prec)); })
8787 { build_zero_cst (type0); })))))))
8790 /* __builtin_ctz (x) == C -> (x & ((1 << (C + 1)) - 1)) == (1 << C). */
8791 (op (IFN_CTZ:s @0 @2) INTEGER_CST@1)
8792 (with { HOST_WIDE_INT zero_val = tree_to_shwi (@2);
8793 tree type0 = TREE_TYPE (@0);
8794 int prec = TYPE_PRECISION (type0);
8796 (if (prec <= MAX_FIXED_MODE_SIZE)
8797 (if (tree_int_cst_sgn (@1) < 0 || wi::to_widest (@1) >= prec)
8798 (if (zero_val != wi::to_widest (@1))
8799 { constant_boolean_node (op == EQ_EXPR ? false : true, type); })
8800 (if (zero_val < 0 || zero_val >= prec)
8801 (op (bit_and @0 { wide_int_to_tree (type0,
8802 wi::mask (tree_to_uhwi (@1) + 1,
8804 { wide_int_to_tree (type0,
8805 wi::shifted_mask (tree_to_uhwi (@1), 1,
8806 false, prec)); })))))))
8809 /* ctz(ext(X)) == ctz(X). Valid just for the UB at zero cases though. */
8811 (CTZ (convert@1 @0))
8812 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
8813 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
8814 && TYPE_PRECISION (TREE_TYPE (@1)) > TYPE_PRECISION (TREE_TYPE (@0)))
8815 (with { combined_fn cfn = CFN_LAST;
8816 tree type0 = TREE_TYPE (@0);
8817 if (TREE_CODE (type0) == BITINT_TYPE)
8819 if (TYPE_PRECISION (type0) > MAX_FIXED_MODE_SIZE)
8823 = build_nonstandard_integer_type (TYPE_PRECISION (type0),
8826 type0 = unsigned_type_for (type0);
8828 && direct_internal_fn_supported_p (IFN_CTZ, type0,
8832 && TYPE_PRECISION (TREE_TYPE (@1)) > BITS_PER_WORD
8833 && !direct_internal_fn_supported_p (IFN_CTZ,
8837 if (TYPE_PRECISION (type0)
8838 == TYPE_PRECISION (unsigned_type_node))
8839 cfn = CFN_BUILT_IN_CTZ;
8840 else if (TYPE_PRECISION (type0)
8841 == TYPE_PRECISION (long_long_unsigned_type_node))
8842 cfn = CFN_BUILT_IN_CTZLL;
8844 (if (cfn == CFN_CTZ)
8845 (IFN_CTZ (convert:type0 @0))
8846 (if (cfn == CFN_BUILT_IN_CTZ)
8847 (BUILT_IN_CTZ (convert:type0 @0))
8848 (if (cfn == CFN_BUILT_IN_CTZLL)
8849 (BUILT_IN_CTZLL (convert:type0 @0))))))))
8852 /* POPCOUNT simplifications. */
8853 /* popcount(X) + popcount(Y) is popcount(X|Y) when X&Y must be zero. */
8855 (plus (POPCOUNT:s @0) (POPCOUNT:s @1))
8856 (if (INTEGRAL_TYPE_P (type)
8857 && (wi::bit_and (widest_int::from (tree_nonzero_bits (@0), UNSIGNED),
8858 widest_int::from (tree_nonzero_bits (@1), UNSIGNED))
8860 (with { tree utype = TREE_TYPE (@0);
8861 if (TYPE_PRECISION (utype) < TYPE_PRECISION (TREE_TYPE (@1)))
8862 utype = TREE_TYPE (@1); }
8863 (POPCOUNT (bit_ior (convert:utype @0) (convert:utype @1))))))
8865 /* popcount(X) == 0 is X == 0, and related (in)equalities. */
8866 (for popcount (POPCOUNT)
8867 (for cmp (le eq ne gt)
8870 (cmp (popcount @0) integer_zerop)
8871 (rep @0 { build_zero_cst (TREE_TYPE (@0)); }))))
8873 /* popcount(bswap(x)) is popcount(x). */
8874 (for popcount (POPCOUNT)
8875 (for bswap (BUILT_IN_BSWAP16 BUILT_IN_BSWAP32
8876 BUILT_IN_BSWAP64 BUILT_IN_BSWAP128)
8878 (popcount (convert?@0 (bswap:s@1 @2)))
8879 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
8880 && INTEGRAL_TYPE_P (TREE_TYPE (@1)))
8881 (with { tree type0 = TREE_TYPE (@0);
8882 tree type1 = TREE_TYPE (@1);
8883 unsigned int prec0 = TYPE_PRECISION (type0);
8884 unsigned int prec1 = TYPE_PRECISION (type1); }
8885 (if (prec0 == prec1 || (prec0 > prec1 && TYPE_UNSIGNED (type1)))
8886 (popcount (convert:type0 (convert:type1 @2)))))))))
8888 /* popcount(rotate(X Y)) is popcount(X). */
8889 (for popcount (POPCOUNT)
8890 (for rot (lrotate rrotate)
8892 (popcount (convert?@0 (rot:s@1 @2 @3)))
8893 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
8894 && INTEGRAL_TYPE_P (TREE_TYPE (@1))
8895 && (GIMPLE || !TREE_SIDE_EFFECTS (@3)))
8896 (with { tree type0 = TREE_TYPE (@0);
8897 tree type1 = TREE_TYPE (@1);
8898 unsigned int prec0 = TYPE_PRECISION (type0);
8899 unsigned int prec1 = TYPE_PRECISION (type1); }
8900 (if (prec0 == prec1 || (prec0 > prec1 && TYPE_UNSIGNED (type1)))
8901 (popcount (convert:type0 @2))))))))
8903 /* Canonicalize POPCOUNT(x)&1 as PARITY(X). */
8905 (bit_and (POPCOUNT @0) integer_onep)
8908 /* popcount(X&Y) + popcount(X|Y) is popcount(x) + popcount(Y). */
8910 (plus:c (POPCOUNT:s (bit_and:s @0 @1)) (POPCOUNT:s (bit_ior:cs @0 @1)))
8911 (plus (POPCOUNT:type @0) (POPCOUNT:type @1)))
8913 /* popcount(X) + popcount(Y) - popcount(X&Y) is popcount(X|Y). */
8914 /* popcount(X) + popcount(Y) - popcount(X|Y) is popcount(X&Y). */
8915 (for popcount (POPCOUNT)
8916 (for log1 (bit_and bit_ior)
8917 log2 (bit_ior bit_and)
8919 (minus (plus:s (popcount:s @0) (popcount:s @1))
8920 (popcount:s (log1:cs @0 @1)))
8921 (popcount (log2 @0 @1)))
8923 (plus:c (minus:s (popcount:s @0) (popcount:s (log1:cs @0 @1)))
8925 (popcount (log2 @0 @1)))))
8928 /* popcount(zext(X)) == popcount(X). */
8930 (POPCOUNT (convert@1 @0))
8931 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
8932 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
8933 && TYPE_UNSIGNED (TREE_TYPE (@0))
8934 && TYPE_PRECISION (TREE_TYPE (@1)) > TYPE_PRECISION (TREE_TYPE (@0)))
8935 (with { combined_fn cfn = CFN_LAST;
8936 tree type0 = TREE_TYPE (@0);
8937 if (TREE_CODE (type0) == BITINT_TYPE)
8939 if (TYPE_PRECISION (type0) > MAX_FIXED_MODE_SIZE)
8943 = build_nonstandard_integer_type (TYPE_PRECISION (type0),
8947 && direct_internal_fn_supported_p (IFN_POPCOUNT, type0,
8951 && TYPE_PRECISION (TREE_TYPE (@1)) > BITS_PER_WORD
8952 && !direct_internal_fn_supported_p (IFN_POPCOUNT,
8956 if (TYPE_PRECISION (type0)
8957 == TYPE_PRECISION (unsigned_type_node))
8958 cfn = CFN_BUILT_IN_POPCOUNT;
8959 else if (TYPE_PRECISION (type0)
8960 == TYPE_PRECISION (long_long_unsigned_type_node))
8961 cfn = CFN_BUILT_IN_POPCOUNTLL;
8963 (if (cfn == CFN_POPCOUNT)
8964 (IFN_POPCOUNT (convert:type0 @0))
8965 (if (cfn == CFN_BUILT_IN_POPCOUNT)
8966 (BUILT_IN_POPCOUNT (convert:type0 @0))
8967 (if (cfn == CFN_BUILT_IN_POPCOUNTLL)
8968 (BUILT_IN_POPCOUNTLL (convert:type0 @0))))))))
8971 /* PARITY simplifications. */
8972 /* parity(~X) is parity(X). */
8974 (PARITY (bit_not @0))
8977 /* parity(bswap(x)) is parity(x). */
8978 (for parity (PARITY)
8979 (for bswap (BUILT_IN_BSWAP16 BUILT_IN_BSWAP32
8980 BUILT_IN_BSWAP64 BUILT_IN_BSWAP128)
8982 (parity (convert?@0 (bswap:s@1 @2)))
8983 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
8984 && INTEGRAL_TYPE_P (TREE_TYPE (@1))
8985 && TYPE_PRECISION (TREE_TYPE (@0))
8986 >= TYPE_PRECISION (TREE_TYPE (@1)))
8987 (with { tree type0 = TREE_TYPE (@0);
8988 tree type1 = TREE_TYPE (@1); }
8989 (parity (convert:type0 (convert:type1 @2))))))))
8991 /* parity(rotate(X Y)) is parity(X). */
8992 (for parity (PARITY)
8993 (for rot (lrotate rrotate)
8995 (parity (convert?@0 (rot:s@1 @2 @3)))
8996 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
8997 && INTEGRAL_TYPE_P (TREE_TYPE (@1))
8998 && (GIMPLE || !TREE_SIDE_EFFECTS (@3))
8999 && TYPE_PRECISION (TREE_TYPE (@0))
9000 >= TYPE_PRECISION (TREE_TYPE (@1)))
9001 (with { tree type0 = TREE_TYPE (@0); }
9002 (parity (convert:type0 @2)))))))
9004 /* parity(X)^parity(Y) is parity(X^Y). */
9006 (bit_xor (PARITY:s @0) (PARITY:s @1))
9007 (if (types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
9008 (PARITY (bit_xor @0 @1))
9009 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
9010 && INTEGRAL_TYPE_P (TREE_TYPE (@1)))
9011 (with { tree utype = TREE_TYPE (@0);
9012 if (TYPE_PRECISION (utype) < TYPE_PRECISION (TREE_TYPE (@1)))
9013 utype = TREE_TYPE (@1); }
9014 (PARITY (bit_xor (convert:utype @0) (convert:utype @1)))))))
9017 /* parity(zext(X)) == parity(X). */
9018 /* parity(sext(X)) == parity(X) if the difference in precision is even. */
9020 (PARITY (convert@1 @0))
9021 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
9022 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
9023 && TYPE_PRECISION (TREE_TYPE (@1)) > TYPE_PRECISION (TREE_TYPE (@0))
9024 && (TYPE_UNSIGNED (TREE_TYPE (@0))
9025 || ((TYPE_PRECISION (TREE_TYPE (@1))
9026 - TYPE_PRECISION (TREE_TYPE (@0))) & 1) == 0))
9027 (with { combined_fn cfn = CFN_LAST;
9028 tree type0 = TREE_TYPE (@0);
9029 if (TREE_CODE (type0) == BITINT_TYPE)
9031 if (TYPE_PRECISION (type0) > MAX_FIXED_MODE_SIZE)
9035 = build_nonstandard_integer_type (TYPE_PRECISION (type0),
9038 type0 = unsigned_type_for (type0);
9040 && direct_internal_fn_supported_p (IFN_PARITY, type0,
9044 && TYPE_PRECISION (TREE_TYPE (@1)) > BITS_PER_WORD
9045 && !direct_internal_fn_supported_p (IFN_PARITY,
9049 if (TYPE_PRECISION (type0)
9050 == TYPE_PRECISION (unsigned_type_node))
9051 cfn = CFN_BUILT_IN_PARITY;
9052 else if (TYPE_PRECISION (type0)
9053 == TYPE_PRECISION (long_long_unsigned_type_node))
9054 cfn = CFN_BUILT_IN_PARITYLL;
9056 (if (cfn == CFN_PARITY)
9057 (IFN_PARITY (convert:type0 @0))
9058 (if (cfn == CFN_BUILT_IN_PARITY)
9059 (BUILT_IN_PARITY (convert:type0 @0))
9060 (if (cfn == CFN_BUILT_IN_PARITYLL)
9061 (BUILT_IN_PARITYLL (convert:type0 @0))))))))
9064 /* a != 0 ? FUN(a) : 0 -> Fun(a) for some builtin functions. */
9065 (for func (POPCOUNT BSWAP FFS PARITY)
9067 (cond (ne @0 integer_zerop@1) (func@3 (convert? @0)) integer_zerop@2)
9070 /* a != 0 ? FUN(a) : CST -> Fun(a) for some CLRSB builtins
9071 where CST is precision-1. */
9074 (cond (ne @0 integer_zerop@1) (func@4 (convert?@3 @0)) INTEGER_CST@2)
9075 (if (wi::to_widest (@2) == TYPE_PRECISION (TREE_TYPE (@3)) - 1)
9079 /* a != 0 ? CLZ(a) : CST -> .CLZ(a) where CST is the result of the internal function for 0. */
9082 (cond (ne @0 integer_zerop@1) (func (convert?@3 @0)) INTEGER_CST@2)
9084 internal_fn ifn = IFN_LAST;
9085 if (TREE_CODE (TREE_TYPE (@3)) == BITINT_TYPE)
9087 if (tree_fits_shwi_p (@2))
9089 HOST_WIDE_INT valw = tree_to_shwi (@2);
9090 if ((int) valw == valw)
9097 else if (direct_internal_fn_supported_p (IFN_CLZ, TREE_TYPE (@3),
9099 && CLZ_DEFINED_VALUE_AT_ZERO
9100 (SCALAR_INT_TYPE_MODE (TREE_TYPE (@3)), val) == 2)
9103 (if (ifn == IFN_CLZ && wi::to_widest (@2) == val)
9106 (cond (ne @0 integer_zerop@1) (IFN_CLZ (convert?@3 @0) INTEGER_CST@2) @2)
9108 internal_fn ifn = IFN_LAST;
9109 if (TREE_CODE (TREE_TYPE (@3)) == BITINT_TYPE)
9111 else if (direct_internal_fn_supported_p (IFN_CLZ, TREE_TYPE (@3),
9115 (if (ifn == IFN_CLZ)
9118 /* a != 0 ? CTZ(a) : CST -> .CTZ(a) where CST is the result of the internal function for 0. */
9121 (cond (ne @0 integer_zerop@1) (func (convert?@3 @0)) INTEGER_CST@2)
9123 internal_fn ifn = IFN_LAST;
9124 if (TREE_CODE (TREE_TYPE (@3)) == BITINT_TYPE)
9126 if (tree_fits_shwi_p (@2))
9128 HOST_WIDE_INT valw = tree_to_shwi (@2);
9129 if ((int) valw == valw)
9136 else if (direct_internal_fn_supported_p (IFN_CTZ, TREE_TYPE (@3),
9138 && CTZ_DEFINED_VALUE_AT_ZERO
9139 (SCALAR_INT_TYPE_MODE (TREE_TYPE (@3)), val) == 2)
9142 (if (ifn == IFN_CTZ && wi::to_widest (@2) == val)
9145 (cond (ne @0 integer_zerop@1) (IFN_CTZ (convert?@3 @0) INTEGER_CST@2) @2)
9147 internal_fn ifn = IFN_LAST;
9148 if (TREE_CODE (TREE_TYPE (@3)) == BITINT_TYPE)
9150 else if (direct_internal_fn_supported_p (IFN_CTZ, TREE_TYPE (@3),
9154 (if (ifn == IFN_CTZ)
9158 /* Common POPCOUNT/PARITY simplifications. */
9159 /* popcount(X&C1) is (X>>C2)&1 when C1 == 1<<C2. Same for parity(X&C1). */
9160 (for pfun (POPCOUNT PARITY)
9163 (if (INTEGRAL_TYPE_P (type))
9164 (with { wide_int nz = tree_nonzero_bits (@0); }
9168 (if (wi::popcount (nz) == 1)
9169 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
9170 (convert (rshift:utype (convert:utype @0)
9171 { build_int_cst (integer_type_node,
9172 wi::ctz (nz)); })))))))))
9175 /* 64- and 32-bits branchless implementations of popcount are detected:
9177 int popcount64c (uint64_t x)
9179 x -= (x >> 1) & 0x5555555555555555ULL;
9180 x = (x & 0x3333333333333333ULL) + ((x >> 2) & 0x3333333333333333ULL);
9181 x = (x + (x >> 4)) & 0x0f0f0f0f0f0f0f0fULL;
9182 return (x * 0x0101010101010101ULL) >> 56;
9185 int popcount32c (uint32_t x)
9187 x -= (x >> 1) & 0x55555555;
9188 x = (x & 0x33333333) + ((x >> 2) & 0x33333333);
9189 x = (x + (x >> 4)) & 0x0f0f0f0f;
9190 return (x * 0x01010101) >> 24;
9197 (rshift @8 INTEGER_CST@5)
9199 (bit_and @6 INTEGER_CST@7)
9203 (bit_and (rshift @0 INTEGER_CST@4) INTEGER_CST@11))
9209 /* Check constants and optab. */
9210 (with { unsigned prec = TYPE_PRECISION (type);
9211 int shift = (64 - prec) & 63;
9212 unsigned HOST_WIDE_INT c1
9213 = HOST_WIDE_INT_UC (0x0101010101010101) >> shift;
9214 unsigned HOST_WIDE_INT c2
9215 = HOST_WIDE_INT_UC (0x0F0F0F0F0F0F0F0F) >> shift;
9216 unsigned HOST_WIDE_INT c3
9217 = HOST_WIDE_INT_UC (0x3333333333333333) >> shift;
9218 unsigned HOST_WIDE_INT c4
9219 = HOST_WIDE_INT_UC (0x5555555555555555) >> shift;
9224 && TYPE_UNSIGNED (type)
9225 && integer_onep (@4)
9226 && wi::to_widest (@10) == 2
9227 && wi::to_widest (@5) == 4
9228 && wi::to_widest (@1) == prec - 8
9229 && tree_to_uhwi (@2) == c1
9230 && tree_to_uhwi (@3) == c2
9231 && tree_to_uhwi (@9) == c3
9232 && tree_to_uhwi (@7) == c3
9233 && tree_to_uhwi (@11) == c4)
9234 (if (direct_internal_fn_supported_p (IFN_POPCOUNT, type,
9236 (convert (IFN_POPCOUNT:type @0))
9237 /* Try to do popcount in two halves. PREC must be at least
9238 five bits for this to work without extension before adding. */
9240 tree half_type = NULL_TREE;
9241 opt_machine_mode m = mode_for_size ((prec + 1) / 2, MODE_INT, 1);
9244 && m.require () != TYPE_MODE (type))
9246 half_prec = GET_MODE_PRECISION (as_a <scalar_int_mode> (m));
9247 half_type = build_nonstandard_integer_type (half_prec, 1);
9249 gcc_assert (half_prec > 2);
9251 (if (half_type != NULL_TREE
9252 && direct_internal_fn_supported_p (IFN_POPCOUNT, half_type,
9255 (IFN_POPCOUNT:half_type (convert @0))
9256 (IFN_POPCOUNT:half_type (convert (rshift @0
9257 { build_int_cst (integer_type_node, half_prec); } )))))))))))
9259 /* __builtin_ffs needs to deal on many targets with the possible zero
9260 argument. If we know the argument is always non-zero, __builtin_ctz + 1
9261 should lead to better code. */
9263 (FFS tree_expr_nonzero_p@0)
9264 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
9265 && direct_internal_fn_supported_p (IFN_CTZ, TREE_TYPE (@0),
9266 OPTIMIZE_FOR_SPEED))
9267 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
9268 (plus (CTZ:type (convert:utype @0)) { build_one_cst (type); }))))
9272 /* __builtin_ffs (X) == 0 -> X == 0.
9273 __builtin_ffs (X) == 6 -> (X & 63) == 32. */
9276 (cmp (ffs@2 @0) INTEGER_CST@1)
9277 (with { int prec = TYPE_PRECISION (TREE_TYPE (@0)); }
9279 (if (integer_zerop (@1))
9280 (cmp @0 { build_zero_cst (TREE_TYPE (@0)); }))
9281 (if (tree_int_cst_sgn (@1) < 0 || wi::to_widest (@1) > prec)
9282 { constant_boolean_node (cmp == NE_EXPR ? true : false, type); })
9283 (if (single_use (@2))
9284 (cmp (bit_and @0 { wide_int_to_tree (TREE_TYPE (@0),
9285 wi::mask (tree_to_uhwi (@1),
9287 { wide_int_to_tree (TREE_TYPE (@0),
9288 wi::shifted_mask (tree_to_uhwi (@1) - 1, 1,
9289 false, prec)); }))))))
9291 /* __builtin_ffs (X) > 6 -> X != 0 && (X & 63) == 0. */
9295 bit_op (bit_and bit_ior)
9297 (cmp (ffs@2 @0) INTEGER_CST@1)
9298 (with { int prec = TYPE_PRECISION (TREE_TYPE (@0)); }
9300 (if (integer_zerop (@1))
9301 (cmp2 @0 { build_zero_cst (TREE_TYPE (@0)); }))
9302 (if (tree_int_cst_sgn (@1) < 0)
9303 { constant_boolean_node (cmp == GT_EXPR ? true : false, type); })
9304 (if (wi::to_widest (@1) >= prec)
9305 { constant_boolean_node (cmp == GT_EXPR ? false : true, type); })
9306 (if (wi::to_widest (@1) == prec - 1)
9307 (cmp3 @0 { wide_int_to_tree (TREE_TYPE (@0),
9308 wi::shifted_mask (prec - 1, 1,
9310 (if (single_use (@2))
9311 (bit_op (cmp2 @0 { build_zero_cst (TREE_TYPE (@0)); })
9313 { wide_int_to_tree (TREE_TYPE (@0),
9314 wi::mask (tree_to_uhwi (@1),
9316 { build_zero_cst (TREE_TYPE (@0)); }))))))))
9319 /* ffs(ext(X)) == ffs(X). */
9321 (FFS (convert@1 @0))
9322 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
9323 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
9324 && TYPE_PRECISION (TREE_TYPE (@1)) > TYPE_PRECISION (TREE_TYPE (@0)))
9325 (with { combined_fn cfn = CFN_LAST;
9326 tree type0 = TREE_TYPE (@0);
9327 if (TREE_CODE (type0) == BITINT_TYPE)
9329 if (TYPE_PRECISION (type0) > MAX_FIXED_MODE_SIZE)
9333 = build_nonstandard_integer_type (TYPE_PRECISION (type0),
9336 type0 = signed_type_for (type0);
9338 && direct_internal_fn_supported_p (IFN_FFS, type0,
9342 && TYPE_PRECISION (TREE_TYPE (@1)) > BITS_PER_WORD
9343 && !direct_internal_fn_supported_p (IFN_FFS,
9347 if (TYPE_PRECISION (type0)
9348 == TYPE_PRECISION (integer_type_node))
9349 cfn = CFN_BUILT_IN_FFS;
9350 else if (TYPE_PRECISION (type0)
9351 == TYPE_PRECISION (long_long_integer_type_node))
9352 cfn = CFN_BUILT_IN_FFSLL;
9354 (if (cfn == CFN_FFS)
9355 (IFN_FFS (convert:type0 @0))
9356 (if (cfn == CFN_BUILT_IN_FFS)
9357 (BUILT_IN_FFS (convert:type0 @0))
9358 (if (cfn == CFN_BUILT_IN_FFSLL)
9359 (BUILT_IN_FFSLL (convert:type0 @0))))))))
9367 --> r = .COND_FN (cond, a, b)
9371 --> r = .COND_FN (~cond, b, a). */
9373 (for uncond_op (UNCOND_UNARY)
9374 cond_op (COND_UNARY)
9376 (vec_cond @0 (view_convert? (uncond_op@3 @1)) @2)
9377 (with { tree op_type = TREE_TYPE (@3); }
9378 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
9379 && is_truth_type_for (op_type, TREE_TYPE (@0)))
9380 (cond_op @0 (view_convert @1) @2))))
9382 (vec_cond @0 @1 (view_convert? (uncond_op@3 @2)))
9383 (with { tree op_type = TREE_TYPE (@3); }
9384 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
9385 && is_truth_type_for (op_type, TREE_TYPE (@0)))
9386 (cond_op (bit_not @0) (view_convert @2) @1)))))
9388 (for uncond_op (UNCOND_UNARY)
9389 cond_op (COND_LEN_UNARY)
9391 (IFN_VCOND_MASK_LEN @0 (view_convert? (uncond_op@3 @1)) @2 @4 @5)
9392 (with { tree op_type = TREE_TYPE (@3); }
9393 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
9394 && is_truth_type_for (op_type, TREE_TYPE (@0)))
9395 (cond_op @0 (view_convert @1) @2 @4 @5))))
9397 (IFN_VCOND_MASK_LEN @0 @1 (view_convert? (uncond_op@3 @2)) @4 @5)
9398 (with { tree op_type = TREE_TYPE (@3); }
9399 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
9400 && is_truth_type_for (op_type, TREE_TYPE (@0)))
9401 (cond_op (bit_not @0) (view_convert @2) @1 @4 @5)))))
9403 /* `(a ? -1 : 0) ^ b` can be converted into a conditional not. */
9405 (bit_xor:c (vec_cond @0 uniform_integer_cst_p@1 uniform_integer_cst_p@2) @3)
9406 (if (canonicalize_math_after_vectorization_p ()
9407 && vectorized_internal_fn_supported_p (IFN_COND_NOT, type)
9408 && is_truth_type_for (type, TREE_TYPE (@0)))
9409 (if (integer_all_onesp (@1) && integer_zerop (@2))
9410 (IFN_COND_NOT @0 @3 @3))
9411 (if (integer_all_onesp (@2) && integer_zerop (@1))
9412 (IFN_COND_NOT (bit_not @0) @3 @3))))
9421 r = c ? a1 op a2 : b;
9423 if the target can do it in one go. This makes the operation conditional
9424 on c, so could drop potentially-trapping arithmetic, but that's a valid
9425 simplification if the result of the operation isn't needed.
9427 Avoid speculatively generating a stand-alone vector comparison
9428 on targets that might not support them. Any target implementing
9429 conditional internal functions must support the same comparisons
9430 inside and outside a VEC_COND_EXPR. */
9432 (for uncond_op (UNCOND_BINARY)
9433 cond_op (COND_BINARY)
9435 (vec_cond @0 (view_convert? (uncond_op@4 @1 @2)) @3)
9436 (with { tree op_type = TREE_TYPE (@4); }
9437 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
9438 && is_truth_type_for (op_type, TREE_TYPE (@0))
9440 (view_convert (cond_op @0 @1 @2 (view_convert:op_type @3))))))
9442 (vec_cond @0 @1 (view_convert? (uncond_op@4 @2 @3)))
9443 (with { tree op_type = TREE_TYPE (@4); }
9444 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
9445 && is_truth_type_for (op_type, TREE_TYPE (@0))
9447 (view_convert (cond_op (bit_not @0) @2 @3 (view_convert:op_type @1)))))))
9449 (for uncond_op (UNCOND_BINARY)
9450 cond_op (COND_LEN_BINARY)
9452 (IFN_VCOND_MASK_LEN @0 (view_convert? (uncond_op@4 @1 @2)) @3 @5 @6)
9453 (with { tree op_type = TREE_TYPE (@4); }
9454 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
9455 && is_truth_type_for (op_type, TREE_TYPE (@0))
9457 (view_convert (cond_op @0 @1 @2 (view_convert:op_type @3) @5 @6)))))
9459 (IFN_VCOND_MASK_LEN @0 @1 (view_convert? (uncond_op@4 @2 @3)) @5 @6)
9460 (with { tree op_type = TREE_TYPE (@4); }
9461 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
9462 && is_truth_type_for (op_type, TREE_TYPE (@0))
9464 (view_convert (cond_op (bit_not @0) @2 @3 (view_convert:op_type @1) @5 @6))))))
9466 /* Same for ternary operations. */
9467 (for uncond_op (UNCOND_TERNARY)
9468 cond_op (COND_TERNARY)
9470 (vec_cond @0 (view_convert? (uncond_op@5 @1 @2 @3)) @4)
9471 (with { tree op_type = TREE_TYPE (@5); }
9472 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
9473 && is_truth_type_for (op_type, TREE_TYPE (@0))
9475 (view_convert (cond_op @0 @1 @2 @3 (view_convert:op_type @4))))))
9477 (vec_cond @0 @1 (view_convert? (uncond_op@5 @2 @3 @4)))
9478 (with { tree op_type = TREE_TYPE (@5); }
9479 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
9480 && is_truth_type_for (op_type, TREE_TYPE (@0))
9482 (view_convert (cond_op (bit_not @0) @2 @3 @4
9483 (view_convert:op_type @1)))))))
9485 (for uncond_op (UNCOND_TERNARY)
9486 cond_op (COND_LEN_TERNARY)
9488 (IFN_VCOND_MASK_LEN @0 (view_convert? (uncond_op@5 @1 @2 @3)) @4 @6 @7)
9489 (with { tree op_type = TREE_TYPE (@5); }
9490 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
9491 && is_truth_type_for (op_type, TREE_TYPE (@0))
9493 (view_convert (cond_op @0 @1 @2 @3 (view_convert:op_type @4) @6 @7)))))
9495 (IFN_VCOND_MASK_LEN @0 @1 (view_convert? (uncond_op@5 @2 @3 @4 @6 @7)))
9496 (with { tree op_type = TREE_TYPE (@5); }
9497 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
9498 && is_truth_type_for (op_type, TREE_TYPE (@0))
9500 (view_convert (cond_op (bit_not @0) @2 @3 @4 (view_convert:op_type @1) @6 @7))))))
9503 /* Detect cases in which a VEC_COND_EXPR effectively replaces the
9504 "else" value of an IFN_COND_*. */
9505 (for cond_op (COND_BINARY)
9507 (vec_cond @0 (view_convert? (cond_op @0 @1 @2 @3)) @4)
9508 (with { tree op_type = TREE_TYPE (@3); }
9509 (if (element_precision (type) == element_precision (op_type))
9510 (view_convert (cond_op @0 @1 @2 (view_convert:op_type @4))))))
9512 (vec_cond @0 @1 (view_convert? (cond_op @2 @3 @4 @5)))
9513 (with { tree op_type = TREE_TYPE (@5); }
9514 (if (inverse_conditions_p (@0, @2)
9515 && element_precision (type) == element_precision (op_type))
9516 (view_convert (cond_op @2 @3 @4 (view_convert:op_type @1)))))))
9518 /* Same for ternary operations. */
9519 (for cond_op (COND_TERNARY)
9521 (vec_cond @0 (view_convert? (cond_op @0 @1 @2 @3 @4)) @5)
9522 (with { tree op_type = TREE_TYPE (@4); }
9523 (if (element_precision (type) == element_precision (op_type))
9524 (view_convert (cond_op @0 @1 @2 @3 (view_convert:op_type @5))))))
9526 (vec_cond @0 @1 (view_convert? (cond_op @2 @3 @4 @5 @6)))
9527 (with { tree op_type = TREE_TYPE (@6); }
9528 (if (inverse_conditions_p (@0, @2)
9529 && element_precision (type) == element_precision (op_type))
9530 (view_convert (cond_op @2 @3 @4 @5 (view_convert:op_type @1)))))))
9532 /* Detect cases in which a VEC_COND_EXPR effectively replaces the
9533 "else" value of an IFN_COND_LEN_*. */
9534 (for cond_len_op (COND_LEN_BINARY)
9536 (vec_cond @0 (view_convert? (cond_len_op @0 @1 @2 @3 @4 @5)) @6)
9537 (with { tree op_type = TREE_TYPE (@3); }
9538 (if (element_precision (type) == element_precision (op_type))
9539 (view_convert (cond_len_op @0 @1 @2 (view_convert:op_type @6) @4 @5)))))
9541 (vec_cond @0 @1 (view_convert? (cond_len_op @2 @3 @4 @5 @6 @7)))
9542 (with { tree op_type = TREE_TYPE (@5); }
9543 (if (inverse_conditions_p (@0, @2)
9544 && element_precision (type) == element_precision (op_type))
9545 (view_convert (cond_len_op @2 @3 @4 (view_convert:op_type @1) @6 @7))))))
9547 /* Same for ternary operations. */
9548 (for cond_len_op (COND_LEN_TERNARY)
9550 (vec_cond @0 (view_convert? (cond_len_op @0 @1 @2 @3 @4 @5 @6)) @7)
9551 (with { tree op_type = TREE_TYPE (@4); }
9552 (if (element_precision (type) == element_precision (op_type))
9553 (view_convert (cond_len_op @0 @1 @2 @3 (view_convert:op_type @7) @5 @6)))))
9555 (vec_cond @0 @1 (view_convert? (cond_len_op @2 @3 @4 @5 @6 @7 @8)))
9556 (with { tree op_type = TREE_TYPE (@6); }
9557 (if (inverse_conditions_p (@0, @2)
9558 && element_precision (type) == element_precision (op_type))
9559 (view_convert (cond_len_op @2 @3 @4 @5 (view_convert:op_type @1) @7 @8))))))
9561 /* Detect simplication for a conditional reduction where
9564 c = mask2 ? d + a : d
9568 c = mask1 && mask2 ? d + b : d. */
9570 (IFN_COND_ADD @0 @1 (vec_cond @2 @3 zerop@4) @1)
9571 (if (ANY_INTEGRAL_TYPE_P (type)
9572 || (FLOAT_TYPE_P (type)
9573 && fold_real_zero_addition_p (type, NULL_TREE, @4, 0)))
9574 (IFN_COND_ADD (bit_and @0 @2) @1 @3 @1)))
9576 /* Detect simplication for a conditional length reduction where
9579 c = i < len + bias ? d + a : d
9583 c = mask && i < len + bias ? d + b : d. */
9585 (IFN_COND_LEN_ADD integer_truep @0 (vec_cond @1 @2 zerop@5) @0 @3 @4)
9586 (if (ANY_INTEGRAL_TYPE_P (type)
9587 || (FLOAT_TYPE_P (type)
9588 && fold_real_zero_addition_p (type, NULL_TREE, @5, 0)))
9589 (IFN_COND_LEN_ADD @1 @0 @2 @0 @3 @4)))
9591 /* Detect simplification for vector condition folding where
9593 c = mask1 ? (masked_op mask2 a b) : b
9597 c = masked_op (mask1 & mask2) a b
9599 where the operation can be partially applied to one operand. */
9601 (for cond_op (COND_BINARY)
9604 (cond_op:s @1 @2 @3 @4) @3)
9605 (cond_op (bit_and @1 @0) @2 @3 @4)))
9607 /* And same for ternary expressions. */
9609 (for cond_op (COND_TERNARY)
9612 (cond_op:s @1 @2 @3 @4 @5) @4)
9613 (cond_op (bit_and @1 @0) @2 @3 @4 @5)))
9615 /* For pointers @0 and @2 and nonnegative constant offset @1, look for
9618 A: (@0 + @1 < @2) | (@2 + @1 < @0)
9619 B: (@0 + @1 <= @2) | (@2 + @1 <= @0)
9621 If pointers are known not to wrap, B checks whether @1 bytes starting
9622 at @0 and @2 do not overlap, while A tests the same thing for @1 + 1
9623 bytes. A is more efficiently tested as:
9625 A: (sizetype) (@0 + @1 - @2) > @1 * 2
9627 The equivalent expression for B is given by replacing @1 with @1 - 1:
9629 B: (sizetype) (@0 + (@1 - 1) - @2) > (@1 - 1) * 2
9631 @0 and @2 can be swapped in both expressions without changing the result.
9633 The folds rely on sizetype's being unsigned (which is always true)
9634 and on its being the same width as the pointer (which we have to check).
9636 The fold replaces two pointer_plus expressions, two comparisons and
9637 an IOR with a pointer_plus, a pointer_diff, and a comparison, so in
9638 the best case it's a saving of two operations. The A fold retains one
9639 of the original pointer_pluses, so is a win even if both pointer_pluses
9640 are used elsewhere. The B fold is a wash if both pointer_pluses are
9641 used elsewhere, since all we end up doing is replacing a comparison with
9642 a pointer_plus. We do still apply the fold under those circumstances
9643 though, in case applying it to other conditions eventually makes one of the
9644 pointer_pluses dead. */
9645 (for ior (truth_orif truth_or bit_ior)
9648 (ior (cmp:cs (pointer_plus@3 @0 INTEGER_CST@1) @2)
9649 (cmp:cs (pointer_plus@4 @2 @1) @0))
9650 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
9651 && TYPE_OVERFLOW_WRAPS (sizetype)
9652 && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (sizetype))
9653 /* Calculate the rhs constant. */
9654 (with { offset_int off = wi::to_offset (@1) - (cmp == LE_EXPR ? 1 : 0);
9655 offset_int rhs = off * 2; }
9656 /* Always fails for negative values. */
9657 (if (wi::min_precision (rhs, UNSIGNED) <= TYPE_PRECISION (sizetype))
9658 /* Since the order of @0 and @2 doesn't matter, let tree_swap_operands_p
9659 pick a canonical order. This increases the chances of using the
9660 same pointer_plus in multiple checks. */
9661 (with { bool swap_p = tree_swap_operands_p (@0, @2);
9662 tree rhs_tree = wide_int_to_tree (sizetype, rhs); }
9663 (if (cmp == LT_EXPR)
9664 (gt (convert:sizetype
9665 (pointer_diff:ssizetype { swap_p ? @4 : @3; }
9666 { swap_p ? @0 : @2; }))
9668 (gt (convert:sizetype
9669 (pointer_diff:ssizetype
9670 (pointer_plus { swap_p ? @2 : @0; }
9671 { wide_int_to_tree (sizetype, off); })
9672 { swap_p ? @0 : @2; }))
9673 { rhs_tree; })))))))))
9675 /* Fold REDUC (@0 & @1) -> @0[I] & @1[I] if element I is the only nonzero
9677 (for reduc (IFN_REDUC_PLUS IFN_REDUC_IOR IFN_REDUC_XOR)
9678 (simplify (reduc (view_convert? (bit_and @0 VECTOR_CST@1)))
9679 (with { int i = single_nonzero_element (@1); }
9681 (with { tree elt = vector_cst_elt (@1, i);
9682 tree elt_type = TREE_TYPE (elt);
9683 unsigned int elt_bits = tree_to_uhwi (TYPE_SIZE (elt_type));
9684 tree size = bitsize_int (elt_bits);
9685 tree pos = bitsize_int (elt_bits * i); }
9688 (BIT_FIELD_REF:elt_type @0 { size; } { pos; })
9691 /* Fold reduction of a single nonzero element constructor. */
9692 (for reduc (IFN_REDUC_PLUS IFN_REDUC_IOR IFN_REDUC_XOR)
9693 (simplify (reduc (CONSTRUCTOR@0))
9694 (with { tree ctor = (TREE_CODE (@0) == SSA_NAME
9695 ? gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0)) : @0);
9696 tree elt = ctor_single_nonzero_element (ctor); }
9698 && !HONOR_SNANS (type)
9699 && !HONOR_SIGNED_ZEROS (type))
9702 /* Fold REDUC (@0 op VECTOR_CST) as REDUC (@0) op REDUC (VECTOR_CST). */
9703 (for reduc (IFN_REDUC_PLUS IFN_REDUC_MAX IFN_REDUC_MIN IFN_REDUC_FMAX
9704 IFN_REDUC_FMIN IFN_REDUC_AND IFN_REDUC_IOR IFN_REDUC_XOR)
9705 op (plus max min IFN_FMAX IFN_FMIN bit_and bit_ior bit_xor)
9706 (simplify (reduc (op @0 VECTOR_CST@1))
9707 (op (reduc:type @0) (reduc:type @1))))
9709 /* Simplify vector floating point operations of alternating sub/add pairs
9710 into using an fneg of a wider element type followed by a normal add.
9711 under IEEE 754 the fneg of the wider type will negate every even entry
9712 and when doing an add we get a sub of the even and add of every odd
9714 (for plusminus (plus minus)
9715 minusplus (minus plus)
9717 (vec_perm (plusminus @0 @1) (minusplus @2 @3) VECTOR_CST@4)
9718 (if (!VECTOR_INTEGER_TYPE_P (type)
9719 && !FLOAT_WORDS_BIG_ENDIAN
9720 /* plus is commutative, while minus is not, so :c can't be used.
9721 Do equality comparisons by hand and at the end pick the operands
9723 && (operand_equal_p (@0, @2, 0)
9724 ? operand_equal_p (@1, @3, 0)
9725 : operand_equal_p (@0, @3, 0) && operand_equal_p (@1, @2, 0)))
9728 /* Build a vector of integers from the tree mask. */
9729 vec_perm_builder builder;
9731 (if (tree_to_vec_perm_builder (&builder, @4))
9734 /* Create a vec_perm_indices for the integer vector. */
9735 poly_uint64 nelts = TYPE_VECTOR_SUBPARTS (type);
9736 vec_perm_indices sel (builder, 2, nelts);
9737 machine_mode vec_mode = TYPE_MODE (type);
9738 machine_mode wide_mode;
9739 scalar_mode wide_elt_mode;
9740 poly_uint64 wide_nunits;
9741 scalar_mode inner_mode = GET_MODE_INNER (vec_mode);
9743 (if (VECTOR_MODE_P (vec_mode)
9744 && sel.series_p (0, 2, 0, 2)
9745 && sel.series_p (1, 2, nelts + 1, 2)
9746 && GET_MODE_2XWIDER_MODE (inner_mode).exists (&wide_elt_mode)
9747 && multiple_p (GET_MODE_NUNITS (vec_mode), 2, &wide_nunits)
9748 && related_vector_mode (vec_mode, wide_elt_mode,
9749 wide_nunits).exists (&wide_mode))
9753 = lang_hooks.types.type_for_mode (GET_MODE_INNER (wide_mode),
9754 TYPE_UNSIGNED (type));
9755 tree ntype = build_vector_type_for_mode (stype, wide_mode);
9757 /* The format has to be a non-extended ieee format. */
9758 const struct real_format *fmt_old = FLOAT_MODE_FORMAT (vec_mode);
9759 const struct real_format *fmt_new = FLOAT_MODE_FORMAT (wide_mode);
9761 (if (TYPE_MODE (stype) != BLKmode
9762 && VECTOR_TYPE_P (ntype)
9767 /* If the target doesn't support v1xx vectors, try using
9768 scalar mode xx instead. */
9769 if (known_eq (GET_MODE_NUNITS (wide_mode), 1)
9770 && !target_supports_op_p (ntype, NEGATE_EXPR, optab_vector))
9773 (if (fmt_new->signbit_rw
9774 == fmt_old->signbit_rw + GET_MODE_UNIT_BITSIZE (vec_mode)
9775 && fmt_new->signbit_rw == fmt_new->signbit_ro
9776 && targetm.can_change_mode_class (TYPE_MODE (ntype),
9777 TYPE_MODE (type), ALL_REGS)
9778 && ((optimize_vectors_before_lowering_p ()
9779 && VECTOR_TYPE_P (ntype))
9780 || target_supports_op_p (ntype, NEGATE_EXPR, optab_vector)))
9781 (if (plusminus == PLUS_EXPR)
9782 (plus (view_convert:type (negate (view_convert:ntype @3))) @2)
9783 (minus @0 (view_convert:type
9784 (negate (view_convert:ntype @1))))))))))))))))
9787 (vec_perm @0 @1 VECTOR_CST@2)
9790 tree op0 = @0, op1 = @1, op2 = @2;
9791 machine_mode result_mode = TYPE_MODE (type);
9792 machine_mode op_mode = TYPE_MODE (TREE_TYPE (op0));
9794 /* Build a vector of integers from the tree mask. */
9795 vec_perm_builder builder;
9797 (if (tree_to_vec_perm_builder (&builder, op2))
9800 /* Create a vec_perm_indices for the integer vector. */
9801 poly_uint64 nelts = TYPE_VECTOR_SUBPARTS (type);
9802 bool single_arg = (op0 == op1);
9803 vec_perm_indices sel (builder, single_arg ? 1 : 2, nelts);
9805 (if (sel.series_p (0, 1, 0, 1))
9807 (if (sel.series_p (0, 1, nelts, 1))
9813 if (sel.all_from_input_p (0))
9815 else if (sel.all_from_input_p (1))
9818 sel.rotate_inputs (1);
9820 else if (known_ge (poly_uint64 (sel[0]), nelts))
9822 std::swap (op0, op1);
9823 sel.rotate_inputs (1);
9827 tree cop0 = op0, cop1 = op1;
9828 if (TREE_CODE (op0) == SSA_NAME
9829 && (def = dyn_cast <gassign *> (SSA_NAME_DEF_STMT (op0)))
9830 && gimple_assign_rhs_code (def) == CONSTRUCTOR)
9831 cop0 = gimple_assign_rhs1 (def);
9832 if (TREE_CODE (op1) == SSA_NAME
9833 && (def = dyn_cast <gassign *> (SSA_NAME_DEF_STMT (op1)))
9834 && gimple_assign_rhs_code (def) == CONSTRUCTOR)
9835 cop1 = gimple_assign_rhs1 (def);
9838 (if ((TREE_CODE (cop0) == VECTOR_CST
9839 || TREE_CODE (cop0) == CONSTRUCTOR)
9840 && (TREE_CODE (cop1) == VECTOR_CST
9841 || TREE_CODE (cop1) == CONSTRUCTOR)
9842 && (t = fold_vec_perm (type, cop0, cop1, sel)))
9846 bool changed = (op0 == op1 && !single_arg);
9847 tree ins = NULL_TREE;
9850 /* See if the permutation is performing a single element
9851 insert from a CONSTRUCTOR or constant and use a BIT_INSERT_EXPR
9852 in that case. But only if the vector mode is supported,
9853 otherwise this is invalid GIMPLE. */
9854 if (op_mode != BLKmode
9855 && (TREE_CODE (cop0) == VECTOR_CST
9856 || TREE_CODE (cop0) == CONSTRUCTOR
9857 || TREE_CODE (cop1) == VECTOR_CST
9858 || TREE_CODE (cop1) == CONSTRUCTOR))
9860 bool insert_first_p = sel.series_p (1, 1, nelts + 1, 1);
9863 /* After canonicalizing the first elt to come from the
9864 first vector we only can insert the first elt from
9865 the first vector. */
9867 if ((ins = fold_read_from_vector (cop0, sel[0])))
9870 /* The above can fail for two-element vectors which always
9871 appear to insert the first element, so try inserting
9872 into the second lane as well. For more than two
9873 elements that's wasted time. */
9874 if (!insert_first_p || (!ins && maybe_eq (nelts, 2u)))
9876 unsigned int encoded_nelts = sel.encoding ().encoded_nelts ();
9877 for (at = 0; at < encoded_nelts; ++at)
9878 if (maybe_ne (sel[at], at))
9880 if (at < encoded_nelts
9881 && (known_eq (at + 1, nelts)
9882 || sel.series_p (at + 1, 1, at + 1, 1)))
9884 if (known_lt (poly_uint64 (sel[at]), nelts))
9885 ins = fold_read_from_vector (cop0, sel[at]);
9887 ins = fold_read_from_vector (cop1, sel[at] - nelts);
9892 /* Generate a canonical form of the selector. */
9893 if (!ins && sel.encoding () != builder)
9895 /* Some targets are deficient and fail to expand a single
9896 argument permutation while still allowing an equivalent
9897 2-argument version. */
9899 if (sel.ninputs () == 2
9900 || can_vec_perm_const_p (result_mode, op_mode, sel, false))
9901 op2 = vec_perm_indices_to_tree (TREE_TYPE (op2), sel);
9904 vec_perm_indices sel2 (builder, 2, nelts);
9905 if (can_vec_perm_const_p (result_mode, op_mode, sel2, false))
9906 op2 = vec_perm_indices_to_tree (TREE_TYPE (op2), sel2);
9908 /* Not directly supported with either encoding,
9909 so use the preferred form. */
9910 op2 = vec_perm_indices_to_tree (TREE_TYPE (op2), sel);
9912 if (!operand_equal_p (op2, oldop2, 0))
9917 (bit_insert { op0; } { ins; }
9918 { bitsize_int (at * vector_element_bits (type)); })
9920 (vec_perm { op0; } { op1; } { op2; }))))))))))))
9922 /* VEC_PERM_EXPR (v, v, mask) -> v where v contains same element. */
9924 (match vec_same_elem_p
9927 (match vec_same_elem_p
9929 (if (TREE_CODE (@0) == SSA_NAME
9930 && uniform_vector_p (gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0))))))
9932 (match vec_same_elem_p
9934 (if (uniform_vector_p (@0))))
9938 (vec_perm vec_same_elem_p@0 @0 @1)
9939 (if (types_match (type, TREE_TYPE (@0)))
9943 tree elem = uniform_vector_p (@0);
9946 { build_vector_from_val (type, elem); }))))
9948 /* Push VEC_PERM earlier if that may help FMA perception (PR101895). */
9950 (plus:c (vec_perm:s (mult:c@0 @1 vec_same_elem_p@2) @0 @3) @4)
9951 (if (TREE_CODE (@0) == SSA_NAME && num_imm_uses (@0) == 2)
9952 (plus (mult (vec_perm @1 @1 @3) @2) @4)))
9954 (minus (vec_perm:s (mult:c@0 @1 vec_same_elem_p@2) @0 @3) @4)
9955 (if (TREE_CODE (@0) == SSA_NAME && num_imm_uses (@0) == 2)
9956 (minus (mult (vec_perm @1 @1 @3) @2) @4)))
9960 c = VEC_PERM_EXPR <a, b, VCST0>;
9961 d = VEC_PERM_EXPR <c, c, VCST1>;
9963 d = VEC_PERM_EXPR <a, b, NEW_VCST>; */
9966 (vec_perm (vec_perm@0 @1 @2 VECTOR_CST@3) @0 VECTOR_CST@4)
9967 (if (TYPE_VECTOR_SUBPARTS (type).is_constant ())
9970 machine_mode result_mode = TYPE_MODE (type);
9971 machine_mode op_mode = TYPE_MODE (TREE_TYPE (@1));
9972 int nelts = TYPE_VECTOR_SUBPARTS (type).to_constant ();
9973 vec_perm_builder builder0;
9974 vec_perm_builder builder1;
9975 vec_perm_builder builder2 (nelts, nelts, 1);
9977 (if (tree_to_vec_perm_builder (&builder0, @3)
9978 && tree_to_vec_perm_builder (&builder1, @4))
9981 vec_perm_indices sel0 (builder0, 2, nelts);
9982 vec_perm_indices sel1 (builder1, 1, nelts);
9984 for (int i = 0; i < nelts; i++)
9985 builder2.quick_push (sel0[sel1[i].to_constant ()]);
9987 vec_perm_indices sel2 (builder2, 2, nelts);
9989 tree op0 = NULL_TREE;
9990 /* If the new VEC_PERM_EXPR can't be handled but both
9991 original VEC_PERM_EXPRs can, punt.
9992 If one or both of the original VEC_PERM_EXPRs can't be
9993 handled and the new one can't be either, don't increase
9994 number of VEC_PERM_EXPRs that can't be handled. */
9995 if (can_vec_perm_const_p (result_mode, op_mode, sel2, false)
9997 ? (!can_vec_perm_const_p (result_mode, op_mode, sel0, false)
9998 || !can_vec_perm_const_p (result_mode, op_mode, sel1, false))
9999 : !can_vec_perm_const_p (result_mode, op_mode, sel1, false)))
10000 op0 = vec_perm_indices_to_tree (TREE_TYPE (@4), sel2);
10003 (vec_perm @1 @2 { op0; })))))))
10006 c = VEC_PERM_EXPR <a, b, VCST0>;
10007 d = VEC_PERM_EXPR <x, c, VCST1>;
10009 d = VEC_PERM_EXPR <x, {a,b}, NEW_VCST>;
10010 when all elements from a or b are replaced by the later
10014 (vec_perm @5 (vec_perm@0 @1 @2 VECTOR_CST@3) VECTOR_CST@4)
10015 (if (TYPE_VECTOR_SUBPARTS (type).is_constant ())
10018 machine_mode result_mode = TYPE_MODE (type);
10019 machine_mode op_mode = TYPE_MODE (TREE_TYPE (@1));
10020 int nelts = TYPE_VECTOR_SUBPARTS (type).to_constant ();
10021 vec_perm_builder builder0;
10022 vec_perm_builder builder1;
10023 vec_perm_builder builder2 (nelts, nelts, 2);
10025 (if (tree_to_vec_perm_builder (&builder0, @3)
10026 && tree_to_vec_perm_builder (&builder1, @4))
10029 vec_perm_indices sel0 (builder0, 2, nelts);
10030 vec_perm_indices sel1 (builder1, 2, nelts);
10031 bool use_1 = false, use_2 = false;
10033 for (int i = 0; i < nelts; i++)
10035 if (known_lt ((poly_uint64)sel1[i], sel1.nelts_per_input ()))
10036 builder2.quick_push (sel1[i]);
10039 poly_uint64 j = sel0[(sel1[i] - sel1.nelts_per_input ())
10041 if (known_lt (j, sel0.nelts_per_input ()))
10046 j -= sel0.nelts_per_input ();
10048 builder2.quick_push (j + sel1.nelts_per_input ());
10052 (if (use_1 ^ use_2)
10055 vec_perm_indices sel2 (builder2, 2, nelts);
10056 tree op0 = NULL_TREE;
10057 /* If the new VEC_PERM_EXPR can't be handled but both
10058 original VEC_PERM_EXPRs can, punt.
10059 If one or both of the original VEC_PERM_EXPRs can't be
10060 handled and the new one can't be either, don't increase
10061 number of VEC_PERM_EXPRs that can't be handled. */
10062 if (can_vec_perm_const_p (result_mode, op_mode, sel2, false)
10063 || (single_use (@0)
10064 ? (!can_vec_perm_const_p (result_mode, op_mode, sel0, false)
10065 || !can_vec_perm_const_p (result_mode, op_mode, sel1, false))
10066 : !can_vec_perm_const_p (result_mode, op_mode, sel1, false)))
10067 op0 = vec_perm_indices_to_tree (TREE_TYPE (@4), sel2);
10072 (vec_perm @5 @1 { op0; }))
10074 (vec_perm @5 @2 { op0; })))))))))))
10076 /* And the case with swapped outer permute sources. */
10079 (vec_perm (vec_perm@0 @1 @2 VECTOR_CST@3) @5 VECTOR_CST@4)
10080 (if (TYPE_VECTOR_SUBPARTS (type).is_constant ())
10083 machine_mode result_mode = TYPE_MODE (type);
10084 machine_mode op_mode = TYPE_MODE (TREE_TYPE (@1));
10085 int nelts = TYPE_VECTOR_SUBPARTS (type).to_constant ();
10086 vec_perm_builder builder0;
10087 vec_perm_builder builder1;
10088 vec_perm_builder builder2 (nelts, nelts, 2);
10090 (if (tree_to_vec_perm_builder (&builder0, @3)
10091 && tree_to_vec_perm_builder (&builder1, @4))
10094 vec_perm_indices sel0 (builder0, 2, nelts);
10095 vec_perm_indices sel1 (builder1, 2, nelts);
10096 bool use_1 = false, use_2 = false;
10098 for (int i = 0; i < nelts; i++)
10100 if (known_ge ((poly_uint64)sel1[i], sel1.nelts_per_input ()))
10101 builder2.quick_push (sel1[i]);
10104 poly_uint64 j = sel0[sel1[i].to_constant ()];
10105 if (known_lt (j, sel0.nelts_per_input ()))
10110 j -= sel0.nelts_per_input ();
10112 builder2.quick_push (j);
10116 (if (use_1 ^ use_2)
10119 vec_perm_indices sel2 (builder2, 2, nelts);
10120 tree op0 = NULL_TREE;
10121 /* If the new VEC_PERM_EXPR can't be handled but both
10122 original VEC_PERM_EXPRs can, punt.
10123 If one or both of the original VEC_PERM_EXPRs can't be
10124 handled and the new one can't be either, don't increase
10125 number of VEC_PERM_EXPRs that can't be handled. */
10126 if (can_vec_perm_const_p (result_mode, op_mode, sel2, false)
10127 || (single_use (@0)
10128 ? (!can_vec_perm_const_p (result_mode, op_mode, sel0, false)
10129 || !can_vec_perm_const_p (result_mode, op_mode, sel1, false))
10130 : !can_vec_perm_const_p (result_mode, op_mode, sel1, false)))
10131 op0 = vec_perm_indices_to_tree (TREE_TYPE (@4), sel2);
10136 (vec_perm @1 @5 { op0; }))
10138 (vec_perm @2 @5 { op0; })))))))))))
10141 /* Match count trailing zeroes for simplify_count_trailing_zeroes in fwprop.
10142 The canonical form is array[((x & -x) * C) >> SHIFT] where C is a magic
10143 constant which when multiplied by a power of 2 contains a unique value
10144 in the top 5 or 6 bits. This is then indexed into a table which maps it
10145 to the number of trailing zeroes. */
10146 (match (ctz_table_index @1 @2 @3)
10147 (rshift (mult (bit_and:c (negate @1) @1) INTEGER_CST@2) INTEGER_CST@3))
10149 (match (cond_expr_convert_p @0 @2 @3 @6)
10150 (cond (simple_comparison@6 @0 @1) (convert@4 @2) (convert@5 @3))
10151 (if (INTEGRAL_TYPE_P (type)
10152 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
10153 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
10154 && INTEGRAL_TYPE_P (TREE_TYPE (@3))
10155 && TYPE_PRECISION (type) != TYPE_PRECISION (TREE_TYPE (@0))
10156 && TYPE_PRECISION (TREE_TYPE (@0))
10157 == TYPE_PRECISION (TREE_TYPE (@2))
10158 && TYPE_PRECISION (TREE_TYPE (@0))
10159 == TYPE_PRECISION (TREE_TYPE (@3))
10160 /* For vect_recog_cond_expr_convert_pattern, @2 and @3 can differ in
10161 signess when convert is truncation, but not ok for extension since
10162 it's sign_extend vs zero_extend. */
10163 && (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type)
10164 || (TYPE_UNSIGNED (TREE_TYPE (@2))
10165 == TYPE_UNSIGNED (TREE_TYPE (@3))))
10167 && single_use (@5))))
10169 (for bit_op (bit_and bit_ior bit_xor)
10170 (match (bitwise_induction_p @0 @2 @3)
10172 (nop_convert1? (bit_not2?@0 (convert3? (lshift integer_onep@1 @2))))
10175 (match (bitwise_induction_p @0 @2 @3)
10177 (nop_convert1? (bit_xor@0 (convert2? (lshift integer_onep@1 @2)) @3))))
10179 /* n - (((n > C1) ? n : C1) & -C2) -> n & C1 for unsigned case.
10180 n - (((n > C1) ? n : C1) & -C2) -> (n <= C1) ? n : (n & C1) for signed case. */
10182 (minus @0 (bit_and (max @0 INTEGER_CST@1) INTEGER_CST@2))
10183 (with { auto i = wi::neg (wi::to_wide (@2)); }
10184 /* Check if -C2 is a power of 2 and C1 = -C2 - 1. */
10185 (if (wi::popcount (i) == 1
10186 && (wi::to_wide (@1)) == (i - 1))
10187 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
10189 (cond (le @0 @1) @0 (bit_and @0 @1))))))
10191 /* -x & 1 -> x & 1. */
10193 (bit_and (negate @0) integer_onep@1)
10194 (if (!TYPE_OVERFLOW_SANITIZED (type))
10197 /* `-a` is just `a` if the type is 1bit wide or when converting
10198 to a 1bit type; similar to the above transformation of `(-x)&1`.
10199 This is used mostly with the transformation of
10200 `a ? ~b : b` into `(-a)^b`.
10201 It also can show up with bitfields. */
10203 (convert? (negate @0))
10204 (if (INTEGRAL_TYPE_P (type)
10205 && TYPE_PRECISION (type) == 1
10206 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0)))
10210 c1 = VEC_PERM_EXPR (a, a, mask)
10211 c2 = VEC_PERM_EXPR (b, b, mask)
10215 c3 = VEC_PERM_EXPR (c, c, mask)
10216 For all integer non-div operations. */
10217 (for op (plus minus mult bit_and bit_ior bit_xor
10220 (op (vec_perm @0 @0 @2) (vec_perm @1 @1 @2))
10221 (if (VECTOR_INTEGER_TYPE_P (type))
10222 (vec_perm (op@3 @0 @1) @3 @2))))
10224 /* Similar for float arithmetic when permutation constant covers
10225 all vector elements. */
10226 (for op (plus minus mult)
10228 (op (vec_perm @0 @0 VECTOR_CST@2) (vec_perm @1 @1 VECTOR_CST@2))
10229 (if (VECTOR_FLOAT_TYPE_P (type)
10230 && TYPE_VECTOR_SUBPARTS (type).is_constant ())
10233 tree perm_cst = @2;
10234 vec_perm_builder builder;
10235 bool full_perm_p = false;
10236 if (tree_to_vec_perm_builder (&builder, perm_cst))
10238 unsigned HOST_WIDE_INT nelts;
10240 nelts = TYPE_VECTOR_SUBPARTS (type).to_constant ();
10241 /* Create a vec_perm_indices for the VECTOR_CST. */
10242 vec_perm_indices sel (builder, 1, nelts);
10244 /* Check if perm indices covers all vector elements. */
10245 if (sel.encoding ().encoded_full_vector_p ())
10247 auto_sbitmap seen (nelts);
10248 bitmap_clear (seen);
10250 unsigned HOST_WIDE_INT count = 0, i;
10252 for (i = 0; i < nelts; i++)
10254 if (!bitmap_set_bit (seen, sel[i].to_constant ()))
10258 full_perm_p = count == nelts;
10263 (vec_perm (op@3 @0 @1) @3 @2))))))