1 /* Match-and-simplify patterns for shared GENERIC and GIMPLE folding.
2 This file is consumed by genmatch which produces gimple-match.cc
3 and generic-match.cc from it.
5 Copyright (C) 2014-2023 Free Software Foundation, Inc.
6 Contributed by Richard Biener <rguenther@suse.de>
7 and Prathamesh Kulkarni <bilbotheelffriend@gmail.com>
9 This file is part of GCC.
11 GCC is free software; you can redistribute it and/or modify it under
12 the terms of the GNU General Public License as published by the Free
13 Software Foundation; either version 3, or (at your option) any later
16 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
17 WARRANTY; without even the implied warranty of MERCHANTABILITY or
18 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21 You should have received a copy of the GNU General Public License
22 along with GCC; see the file COPYING3. If not see
23 <http://www.gnu.org/licenses/>. */
26 /* Generic tree predicates we inherit. */
28 integer_onep integer_zerop integer_all_onesp integer_minus_onep
29 integer_each_onep integer_truep integer_nonzerop
30 real_zerop real_onep real_minus_onep
32 initializer_each_zero_or_onep
34 tree_expr_nonnegative_p
42 bitmask_inv_cst_vector_p)
45 (define_operator_list tcc_comparison
46 lt le eq ne ge gt unordered ordered unlt unle ungt unge uneq ltgt)
47 (define_operator_list inverted_tcc_comparison
48 ge gt ne eq lt le ordered unordered ge gt le lt ltgt uneq)
49 (define_operator_list inverted_tcc_comparison_with_nans
50 unge ungt ne eq unlt unle ordered unordered ge gt le lt ltgt uneq)
51 (define_operator_list swapped_tcc_comparison
52 gt ge eq ne le lt unordered ordered ungt unge unlt unle uneq ltgt)
53 (define_operator_list simple_comparison lt le eq ne ge gt)
54 (define_operator_list swapped_simple_comparison gt ge eq ne le lt)
55 (define_operator_list BSWAP BUILT_IN_BSWAP16 BUILT_IN_BSWAP32
56 BUILT_IN_BSWAP64 BUILT_IN_BSWAP128)
58 #include "cfn-operators.pd"
60 /* Define operand lists for math rounding functions {,i,l,ll}FN,
61 where the versions prefixed with "i" return an int, those prefixed with
62 "l" return a long and those prefixed with "ll" return a long long.
64 Also define operand lists:
66 X<FN>F for all float functions, in the order i, l, ll
67 X<FN> for all double functions, in the same order
68 X<FN>L for all long double functions, in the same order. */
69 #define DEFINE_INT_AND_FLOAT_ROUND_FN(FN) \
70 (define_operator_list X##FN##F BUILT_IN_I##FN##F \
73 (define_operator_list X##FN BUILT_IN_I##FN \
76 (define_operator_list X##FN##L BUILT_IN_I##FN##L \
80 DEFINE_INT_AND_FLOAT_ROUND_FN (FLOOR)
81 DEFINE_INT_AND_FLOAT_ROUND_FN (CEIL)
82 DEFINE_INT_AND_FLOAT_ROUND_FN (ROUND)
83 DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
85 /* Unary operations and their associated IFN_COND_* function. */
86 (define_operator_list UNCOND_UNARY
88 (define_operator_list COND_UNARY
89 IFN_COND_NEG IFN_COND_NOT)
90 (define_operator_list COND_LEN_UNARY
91 IFN_COND_LEN_NEG IFN_COND_LEN_NOT)
93 /* Binary operations and their associated IFN_COND_* function. */
94 (define_operator_list UNCOND_BINARY
96 mult trunc_div trunc_mod rdiv
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
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
114 IFN_COND_LEN_AND IFN_COND_LEN_IOR IFN_COND_LEN_XOR
115 IFN_COND_LEN_SHL IFN_COND_LEN_SHR)
117 /* Same for ternary operations. */
118 (define_operator_list UNCOND_TERNARY
119 IFN_FMA IFN_FMS IFN_FNMA IFN_FNMS)
120 (define_operator_list COND_TERNARY
121 IFN_COND_FMA IFN_COND_FMS IFN_COND_FNMA IFN_COND_FNMS)
122 (define_operator_list COND_LEN_TERNARY
123 IFN_COND_LEN_FMA IFN_COND_LEN_FMS IFN_COND_LEN_FNMA IFN_COND_LEN_FNMS)
125 /* __atomic_fetch_or_*, __atomic_fetch_xor_*, __atomic_xor_fetch_* */
126 (define_operator_list ATOMIC_FETCH_OR_XOR_N
127 BUILT_IN_ATOMIC_FETCH_OR_1 BUILT_IN_ATOMIC_FETCH_OR_2
128 BUILT_IN_ATOMIC_FETCH_OR_4 BUILT_IN_ATOMIC_FETCH_OR_8
129 BUILT_IN_ATOMIC_FETCH_OR_16
130 BUILT_IN_ATOMIC_FETCH_XOR_1 BUILT_IN_ATOMIC_FETCH_XOR_2
131 BUILT_IN_ATOMIC_FETCH_XOR_4 BUILT_IN_ATOMIC_FETCH_XOR_8
132 BUILT_IN_ATOMIC_FETCH_XOR_16
133 BUILT_IN_ATOMIC_XOR_FETCH_1 BUILT_IN_ATOMIC_XOR_FETCH_2
134 BUILT_IN_ATOMIC_XOR_FETCH_4 BUILT_IN_ATOMIC_XOR_FETCH_8
135 BUILT_IN_ATOMIC_XOR_FETCH_16)
136 /* __sync_fetch_and_or_*, __sync_fetch_and_xor_*, __sync_xor_and_fetch_* */
137 (define_operator_list SYNC_FETCH_OR_XOR_N
138 BUILT_IN_SYNC_FETCH_AND_OR_1 BUILT_IN_SYNC_FETCH_AND_OR_2
139 BUILT_IN_SYNC_FETCH_AND_OR_4 BUILT_IN_SYNC_FETCH_AND_OR_8
140 BUILT_IN_SYNC_FETCH_AND_OR_16
141 BUILT_IN_SYNC_FETCH_AND_XOR_1 BUILT_IN_SYNC_FETCH_AND_XOR_2
142 BUILT_IN_SYNC_FETCH_AND_XOR_4 BUILT_IN_SYNC_FETCH_AND_XOR_8
143 BUILT_IN_SYNC_FETCH_AND_XOR_16
144 BUILT_IN_SYNC_XOR_AND_FETCH_1 BUILT_IN_SYNC_XOR_AND_FETCH_2
145 BUILT_IN_SYNC_XOR_AND_FETCH_4 BUILT_IN_SYNC_XOR_AND_FETCH_8
146 BUILT_IN_SYNC_XOR_AND_FETCH_16)
147 /* __atomic_fetch_and_*. */
148 (define_operator_list ATOMIC_FETCH_AND_N
149 BUILT_IN_ATOMIC_FETCH_AND_1 BUILT_IN_ATOMIC_FETCH_AND_2
150 BUILT_IN_ATOMIC_FETCH_AND_4 BUILT_IN_ATOMIC_FETCH_AND_8
151 BUILT_IN_ATOMIC_FETCH_AND_16)
152 /* __sync_fetch_and_and_*. */
153 (define_operator_list SYNC_FETCH_AND_AND_N
154 BUILT_IN_SYNC_FETCH_AND_AND_1 BUILT_IN_SYNC_FETCH_AND_AND_2
155 BUILT_IN_SYNC_FETCH_AND_AND_4 BUILT_IN_SYNC_FETCH_AND_AND_8
156 BUILT_IN_SYNC_FETCH_AND_AND_16)
158 /* With nop_convert? combine convert? and view_convert? in one pattern
159 plus conditionalize on tree_nop_conversion_p conversions. */
160 (match (nop_convert @0)
162 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))))
163 (match (nop_convert @0)
165 (if (VECTOR_TYPE_P (type) && VECTOR_TYPE_P (TREE_TYPE (@0))
166 && known_eq (TYPE_VECTOR_SUBPARTS (type),
167 TYPE_VECTOR_SUBPARTS (TREE_TYPE (@0)))
168 && tree_nop_conversion_p (TREE_TYPE (type), TREE_TYPE (TREE_TYPE (@0))))))
171 /* These are used by gimple_bitwise_inverted_equal_p to simplify
172 detection of BIT_NOT and comparisons. */
173 (match (bit_not_with_nop @0)
175 (match (bit_not_with_nop @0)
176 (convert (bit_not @0))
177 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))))
178 (for cmp (tcc_comparison)
179 (match (maybe_cmp @0)
181 (match (maybe_cmp @0)
182 (convert (cmp@0 @1 @2))
183 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))))
187 /* Transform likes of (char) ABS_EXPR <(int) x> into (char) ABSU_EXPR <x>
188 ABSU_EXPR returns unsigned absolute value of the operand and the operand
189 of the ABSU_EXPR will have the corresponding signed type. */
190 (simplify (abs (convert @0))
191 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
192 && !TYPE_UNSIGNED (TREE_TYPE (@0))
193 && element_precision (type) > element_precision (TREE_TYPE (@0)))
194 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
195 (convert (absu:utype @0)))))
198 /* Optimize (X + (X >> (prec - 1))) ^ (X >> (prec - 1)) into abs (X). */
200 (bit_xor:c (plus:c @0 (rshift@2 @0 INTEGER_CST@1)) @2)
201 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
202 && !TYPE_UNSIGNED (TREE_TYPE (@0))
203 && wi::to_widest (@1) == element_precision (TREE_TYPE (@0)) - 1)
207 /* Simplifications of operations with one constant operand and
208 simplifications to constants or single values. */
210 (for op (plus pointer_plus minus bit_ior bit_xor)
212 (op @0 integer_zerop)
215 /* 0 +p index -> (type)index */
217 (pointer_plus integer_zerop @1)
218 (non_lvalue (convert @1)))
220 /* ptr - 0 -> (type)ptr */
222 (pointer_diff @0 integer_zerop)
225 /* See if ARG1 is zero and X + ARG1 reduces to X.
226 Likewise if the operands are reversed. */
228 (plus:c @0 real_zerop@1)
229 (if (fold_real_zero_addition_p (type, @0, @1, 0))
232 /* See if ARG1 is zero and X - ARG1 reduces to X. */
234 (minus @0 real_zerop@1)
235 (if (fold_real_zero_addition_p (type, @0, @1, 1))
238 /* Even if the fold_real_zero_addition_p can't simplify X + 0.0
239 into X, we can optimize (X + 0.0) + 0.0 or (X + 0.0) - 0.0
240 or (X - 0.0) + 0.0 into X + 0.0 and (X - 0.0) - 0.0 into X - 0.0
241 if not -frounding-math. For sNaNs the first operation would raise
242 exceptions but turn the result into qNan, so the second operation
243 would not raise it. */
244 (for inner_op (plus minus)
245 (for outer_op (plus minus)
247 (outer_op (inner_op@3 @0 REAL_CST@1) REAL_CST@2)
250 && !HONOR_SIGN_DEPENDENT_ROUNDING (type))
251 (with { bool inner_plus = ((inner_op == PLUS_EXPR)
252 ^ REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@1)));
254 = ((outer_op == PLUS_EXPR)
255 ^ REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@2))); }
256 (if (outer_plus && !inner_plus)
261 This is unsafe for certain floats even in non-IEEE formats.
262 In IEEE, it is unsafe because it does wrong for NaNs.
263 PR middle-end/98420: x - x may be -0.0 with FE_DOWNWARD.
264 Also note that operand_equal_p is always false if an operand
268 (if (!FLOAT_TYPE_P (type)
269 || (!tree_expr_maybe_nan_p (@0)
270 && !tree_expr_maybe_infinite_p (@0)
271 && (!HONOR_SIGN_DEPENDENT_ROUNDING (type)
272 || !HONOR_SIGNED_ZEROS (type))))
273 { build_zero_cst (type); }))
275 (pointer_diff @@0 @0)
276 { build_zero_cst (type); })
279 (mult @0 integer_zerop@1)
282 /* -x == x -> x == 0 */
285 (cmp:c @0 (negate @0))
286 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
287 && !TYPE_OVERFLOW_WRAPS (TREE_TYPE(@0)))
288 (cmp @0 { build_zero_cst (TREE_TYPE(@0)); }))))
290 /* Maybe fold x * 0 to 0. The expressions aren't the same
291 when x is NaN, since x * 0 is also NaN. Nor are they the
292 same in modes with signed zeros, since multiplying a
293 negative value by 0 gives -0, not +0. Nor when x is +-Inf,
294 since x * 0 is NaN. */
296 (mult @0 real_zerop@1)
297 (if (!tree_expr_maybe_nan_p (@0)
298 && (!HONOR_NANS (type) || !tree_expr_maybe_infinite_p (@0))
299 && (!HONOR_SIGNED_ZEROS (type) || tree_expr_nonnegative_p (@0)))
302 /* In IEEE floating point, x*1 is not equivalent to x for snans.
303 Likewise for complex arithmetic with signed zeros. */
306 (if (!tree_expr_maybe_signaling_nan_p (@0)
307 && (!HONOR_SIGNED_ZEROS (type)
308 || !COMPLEX_FLOAT_TYPE_P (type)))
311 /* Transform x * -1.0 into -x. */
313 (mult @0 real_minus_onep)
314 (if (!tree_expr_maybe_signaling_nan_p (@0)
315 && (!HONOR_SIGNED_ZEROS (type)
316 || !COMPLEX_FLOAT_TYPE_P (type)))
319 /* Transform x * { 0 or 1, 0 or 1, ... } into x & { 0 or -1, 0 or -1, ...},
320 unless the target has native support for the former but not the latter. */
322 (mult @0 VECTOR_CST@1)
323 (if (initializer_each_zero_or_onep (@1)
324 && !HONOR_SNANS (type)
325 && !HONOR_SIGNED_ZEROS (type))
326 (with { tree itype = FLOAT_TYPE_P (type) ? unsigned_type_for (type) : type; }
328 && (!VECTOR_MODE_P (TYPE_MODE (type))
329 || (VECTOR_MODE_P (TYPE_MODE (itype))
330 && optab_handler (and_optab,
331 TYPE_MODE (itype)) != CODE_FOR_nothing)))
332 (view_convert (bit_and:itype (view_convert @0)
333 (ne @1 { build_zero_cst (type); })))))))
335 /* In SWAR (SIMD within a register) code a signed comparison of packed data
336 can be constructed with a particular combination of shift, bitwise and,
337 and multiplication by constants. If that code is vectorized we can
338 convert this pattern into a more efficient vector comparison. */
340 (mult (bit_and (rshift @0 uniform_integer_cst_p@1)
341 uniform_integer_cst_p@2)
342 uniform_integer_cst_p@3)
344 tree rshift_cst = uniform_integer_cst_p (@1);
345 tree bit_and_cst = uniform_integer_cst_p (@2);
346 tree mult_cst = uniform_integer_cst_p (@3);
348 /* Make sure we're working with vectors and uniform vector constants. */
349 (if (VECTOR_TYPE_P (type)
350 && tree_fits_uhwi_p (rshift_cst)
351 && tree_fits_uhwi_p (mult_cst)
352 && tree_fits_uhwi_p (bit_and_cst))
353 /* Compute what constants would be needed for this to represent a packed
354 comparison based on the shift amount denoted by RSHIFT_CST. */
356 HOST_WIDE_INT vec_elem_bits = vector_element_bits (type);
357 poly_int64 vec_nelts = TYPE_VECTOR_SUBPARTS (type);
358 poly_int64 vec_bits = vec_elem_bits * vec_nelts;
359 unsigned HOST_WIDE_INT cmp_bits_i, bit_and_i, mult_i;
360 unsigned HOST_WIDE_INT target_mult_i, target_bit_and_i;
361 cmp_bits_i = tree_to_uhwi (rshift_cst) + 1;
362 mult_i = tree_to_uhwi (mult_cst);
363 target_mult_i = (HOST_WIDE_INT_1U << cmp_bits_i) - 1;
364 bit_and_i = tree_to_uhwi (bit_and_cst);
365 target_bit_and_i = 0;
367 /* The bit pattern in BIT_AND_I should be a mask for the least
368 significant bit of each packed element that is CMP_BITS wide. */
369 for (unsigned i = 0; i < vec_elem_bits / cmp_bits_i; i++)
370 target_bit_and_i = (target_bit_and_i << cmp_bits_i) | 1U;
372 (if ((exact_log2 (cmp_bits_i)) >= 0
373 && cmp_bits_i < HOST_BITS_PER_WIDE_INT
374 && multiple_p (vec_bits, cmp_bits_i)
375 && vec_elem_bits <= HOST_BITS_PER_WIDE_INT
376 && target_mult_i == mult_i
377 && target_bit_and_i == bit_and_i)
378 /* Compute the vector shape for the comparison and check if the target is
379 able to expand the comparison with that type. */
381 /* We're doing a signed comparison. */
382 tree cmp_type = build_nonstandard_integer_type (cmp_bits_i, 0);
383 poly_int64 vector_type_nelts = exact_div (vec_bits, cmp_bits_i);
384 tree vec_cmp_type = build_vector_type (cmp_type, vector_type_nelts);
385 tree vec_truth_type = truth_type_for (vec_cmp_type);
386 tree zeros = build_zero_cst (vec_cmp_type);
387 tree ones = build_all_ones_cst (vec_cmp_type);
389 (if (expand_vec_cmp_expr_p (vec_cmp_type, vec_truth_type, LT_EXPR)
390 && expand_vec_cond_expr_p (vec_cmp_type, vec_truth_type, LT_EXPR))
391 (view_convert:type (vec_cond (lt:vec_truth_type
392 (view_convert:vec_cmp_type @0)
394 { ones; } { zeros; })))))))))
396 (for cmp (gt ge lt le)
397 outp (convert convert negate negate)
398 outn (negate negate convert convert)
399 /* Transform X * (X > 0.0 ? 1.0 : -1.0) into abs(X). */
400 /* Transform X * (X >= 0.0 ? 1.0 : -1.0) into abs(X). */
401 /* Transform X * (X < 0.0 ? 1.0 : -1.0) into -abs(X). */
402 /* Transform X * (X <= 0.0 ? 1.0 : -1.0) into -abs(X). */
404 (mult:c @0 (cond (cmp @0 real_zerop) real_onep@1 real_minus_onep))
405 (if (!tree_expr_maybe_nan_p (@0) && !HONOR_SIGNED_ZEROS (type))
407 /* Transform X * (X > 0.0 ? -1.0 : 1.0) into -abs(X). */
408 /* Transform X * (X >= 0.0 ? -1.0 : 1.0) into -abs(X). */
409 /* Transform X * (X < 0.0 ? -1.0 : 1.0) into abs(X). */
410 /* Transform X * (X <= 0.0 ? -1.0 : 1.0) into abs(X). */
412 (mult:c @0 (cond (cmp @0 real_zerop) real_minus_onep real_onep@1))
413 (if (!tree_expr_maybe_nan_p (@0) && !HONOR_SIGNED_ZEROS (type))
416 /* Transform X * copysign (1.0, X) into abs(X). */
418 (mult:c @0 (COPYSIGN_ALL real_onep @0))
419 (if (!tree_expr_maybe_nan_p (@0) && !HONOR_SIGNED_ZEROS (type))
422 /* Transform X * copysign (1.0, -X) into -abs(X). */
424 (mult:c @0 (COPYSIGN_ALL real_onep (negate @0)))
425 (if (!tree_expr_maybe_nan_p (@0) && !HONOR_SIGNED_ZEROS (type))
428 /* Transform copysign (CST, X) into copysign (ABS(CST), X). */
430 (COPYSIGN_ALL REAL_CST@0 @1)
431 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@0)))
432 (COPYSIGN_ALL (negate @0) @1)))
434 /* Transform c ? x * copysign (1, y) : z to c ? x ^ signs(y) : z.
435 tree-ssa-math-opts.cc does the corresponding optimization for
436 unconditional multiplications (via xorsign). */
438 (IFN_COND_MUL:c @0 @1 (IFN_COPYSIGN real_onep @2) @3)
439 (with { tree signs = sign_mask_for (type); }
441 (with { tree inttype = TREE_TYPE (signs); }
443 (IFN_COND_XOR:inttype @0
444 (view_convert:inttype @1)
445 (bit_and (view_convert:inttype @2) { signs; })
446 (view_convert:inttype @3)))))))
448 /* (x >= 0 ? x : 0) + (x <= 0 ? -x : 0) -> abs x. */
450 (plus:c (max @0 integer_zerop) (max (negate @0) integer_zerop))
453 /* X * 1, X / 1 -> X. */
454 (for op (mult trunc_div ceil_div floor_div round_div exact_div)
459 /* (A / (1 << B)) -> (A >> B).
460 Only for unsigned A. For signed A, this would not preserve rounding
462 For example: (-1 / ( 1 << B)) != -1 >> B.
463 Also handle widening conversions, like:
464 (A / (unsigned long long) (1U << B)) -> (A >> B)
466 (A / (unsigned long long) (1 << B)) -> (A >> B).
467 If the left shift is signed, it can be done only if the upper bits
468 of A starting from shift's type sign bit are zero, as
469 (unsigned long long) (1 << 31) is -2147483648ULL, not 2147483648ULL,
470 so it is valid only if A >> 31 is zero. */
472 (trunc_div (convert?@0 @3) (convert2? (lshift integer_onep@1 @2)))
473 (if ((TYPE_UNSIGNED (type) || tree_expr_nonnegative_p (@0))
474 && (!VECTOR_TYPE_P (type)
475 || target_supports_op_p (type, RSHIFT_EXPR, optab_vector)
476 || target_supports_op_p (type, RSHIFT_EXPR, optab_scalar))
477 && (useless_type_conversion_p (type, TREE_TYPE (@1))
478 || (element_precision (type) >= element_precision (TREE_TYPE (@1))
479 && (TYPE_UNSIGNED (TREE_TYPE (@1))
480 || (element_precision (type)
481 == element_precision (TREE_TYPE (@1)))
482 || (INTEGRAL_TYPE_P (type)
483 && (tree_nonzero_bits (@0)
484 & wi::mask (element_precision (TREE_TYPE (@1)) - 1,
486 element_precision (type))) == 0)))))
487 (if (!VECTOR_TYPE_P (type)
488 && useless_type_conversion_p (TREE_TYPE (@3), TREE_TYPE (@1))
489 && element_precision (TREE_TYPE (@3)) < element_precision (type))
490 (convert (rshift @3 @2))
493 /* Preserve explicit divisions by 0: the C++ front-end wants to detect
494 undefined behavior in constexpr evaluation, and assuming that the division
495 traps enables better optimizations than these anyway. */
496 (for div (trunc_div ceil_div floor_div round_div exact_div)
497 /* 0 / X is always zero. */
499 (div integer_zerop@0 @1)
500 /* But not for 0 / 0 so that we can get the proper warnings and errors. */
501 (if (!integer_zerop (@1))
505 (div @0 integer_minus_onep@1)
506 (if (!TYPE_UNSIGNED (type))
508 /* X / bool_range_Y is X. */
511 (if (INTEGRAL_TYPE_P (type)
512 && ssa_name_has_boolean_range (@1)
513 && !flag_non_call_exceptions)
518 /* But not for 0 / 0 so that we can get the proper warnings and errors.
519 And not for _Fract types where we can't build 1. */
520 (if (!ALL_FRACT_MODE_P (TYPE_MODE (type))
521 && !integer_zerop (@0)
522 && (!flag_non_call_exceptions || tree_expr_nonzero_p (@0)))
523 { build_one_cst (type); }))
524 /* X / abs (X) is X < 0 ? -1 : 1. */
527 (if (INTEGRAL_TYPE_P (type)
528 && TYPE_OVERFLOW_UNDEFINED (type)
529 && !integer_zerop (@0)
530 && (!flag_non_call_exceptions || tree_expr_nonzero_p (@0)))
531 (cond (lt @0 { build_zero_cst (type); })
532 { build_minus_one_cst (type); } { build_one_cst (type); })))
535 (div:C @0 (negate @0))
536 (if ((INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
537 && TYPE_OVERFLOW_UNDEFINED (type)
538 && !integer_zerop (@0)
539 && (!flag_non_call_exceptions || tree_expr_nonzero_p (@0)))
540 { build_minus_one_cst (type); })))
542 /* For unsigned integral types, FLOOR_DIV_EXPR is the same as
543 TRUNC_DIV_EXPR. Rewrite into the latter in this case. Similarly
544 for MOD instead of DIV. */
545 (for floor_divmod (floor_div floor_mod)
546 trunc_divmod (trunc_div trunc_mod)
549 (if ((INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
550 && TYPE_UNSIGNED (type))
551 (trunc_divmod @0 @1))))
553 /* 1 / X -> X == 1 for unsigned integer X.
554 1 / X -> X >= -1 && X <= 1 ? X : 0 for signed integer X.
555 But not for 1 / 0 so that we can get proper warnings and errors,
556 and not for 1-bit integers as they are edge cases better handled
559 (trunc_div integer_onep@0 @1)
560 (if (INTEGRAL_TYPE_P (type)
561 && TYPE_PRECISION (type) > 1
562 && !integer_zerop (@1)
563 && (!flag_non_call_exceptions || tree_expr_nonzero_p (@1)))
564 (if (TYPE_UNSIGNED (type))
565 (convert (eq:boolean_type_node @1 { build_one_cst (type); }))
566 (with { tree utype = unsigned_type_for (type); }
567 (cond (le (plus (convert:utype @1) { build_one_cst (utype); })
568 { build_int_cst (utype, 2); })
569 @1 { build_zero_cst (type); })))))
571 /* Combine two successive divisions. Note that combining ceil_div
572 and floor_div is trickier and combining round_div even more so. */
573 (for div (trunc_div exact_div)
575 (div (div@3 @0 INTEGER_CST@1) INTEGER_CST@2)
577 wi::overflow_type overflow;
578 wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@2),
579 TYPE_SIGN (type), &overflow);
581 (if (div == EXACT_DIV_EXPR
582 || optimize_successive_divisions_p (@2, @3))
584 (div @0 { wide_int_to_tree (type, mul); })
585 (if (TYPE_UNSIGNED (type)
586 || mul != wi::min_value (TYPE_PRECISION (type), SIGNED))
587 { build_zero_cst (type); }))))))
589 /* Combine successive multiplications. Similar to above, but handling
590 overflow is different. */
592 (mult (mult @0 INTEGER_CST@1) INTEGER_CST@2)
594 wi::overflow_type overflow;
595 wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@2),
596 TYPE_SIGN (type), &overflow);
598 /* Skip folding on overflow: the only special case is @1 * @2 == -INT_MIN,
599 otherwise undefined overflow implies that @0 must be zero. */
600 (if (!overflow || TYPE_OVERFLOW_WRAPS (type))
601 (mult @0 { wide_int_to_tree (type, mul); }))))
603 /* Similar to above, but there could be an extra add/sub between
604 successive multuiplications. */
606 (mult (plus:s (mult:s@4 @0 INTEGER_CST@1) INTEGER_CST@2) INTEGER_CST@3)
608 bool overflowed = true;
609 wi::overflow_type ovf1, ovf2;
610 wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@3),
611 TYPE_SIGN (type), &ovf1);
612 wide_int add = wi::mul (wi::to_wide (@2), wi::to_wide (@3),
613 TYPE_SIGN (type), &ovf2);
614 if (TYPE_OVERFLOW_UNDEFINED (type))
618 if (ovf1 == wi::OVF_NONE && ovf2 == wi::OVF_NONE
619 && get_global_range_query ()->range_of_expr (vr0, @4)
620 && !vr0.varying_p () && !vr0.undefined_p ())
622 wide_int wmin0 = vr0.lower_bound ();
623 wide_int wmax0 = vr0.upper_bound ();
624 wmin0 = wi::mul (wmin0, wi::to_wide (@3), TYPE_SIGN (type), &ovf1);
625 wmax0 = wi::mul (wmax0, wi::to_wide (@3), TYPE_SIGN (type), &ovf2);
626 if (ovf1 == wi::OVF_NONE && ovf2 == wi::OVF_NONE)
628 wi::add (wmin0, add, TYPE_SIGN (type), &ovf1);
629 wi::add (wmax0, add, TYPE_SIGN (type), &ovf2);
630 if (ovf1 == wi::OVF_NONE && ovf2 == wi::OVF_NONE)
639 /* Skip folding on overflow. */
641 (plus (mult @0 { wide_int_to_tree (type, mul); })
642 { wide_int_to_tree (type, add); }))))
644 /* Similar to above, but a multiplication between successive additions. */
646 (plus (mult:s (plus:s @0 INTEGER_CST@1) INTEGER_CST@2) INTEGER_CST@3)
648 bool overflowed = true;
649 wi::overflow_type ovf1;
650 wi::overflow_type ovf2;
651 wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@2),
652 TYPE_SIGN (type), &ovf1);
653 wide_int add = wi::add (mul, wi::to_wide (@3),
654 TYPE_SIGN (type), &ovf2);
655 if (TYPE_OVERFLOW_UNDEFINED (type))
659 if (ovf1 == wi::OVF_NONE && ovf2 == wi::OVF_NONE
660 && get_global_range_query ()->range_of_expr (vr0, @0)
661 && !vr0.varying_p () && !vr0.undefined_p ())
663 wide_int wmin0 = vr0.lower_bound ();
664 wide_int wmax0 = vr0.upper_bound ();
665 wmin0 = wi::mul (wmin0, wi::to_wide (@2), TYPE_SIGN (type), &ovf1);
666 wmax0 = wi::mul (wmax0, wi::to_wide (@2), TYPE_SIGN (type), &ovf2);
667 if (ovf1 == wi::OVF_NONE && ovf2 == wi::OVF_NONE)
669 wi::add (wmin0, mul, TYPE_SIGN (type), &ovf1);
670 wi::add (wmax0, mul, TYPE_SIGN (type), &ovf2);
671 if (ovf1 == wi::OVF_NONE && ovf2 == wi::OVF_NONE)
680 /* Skip folding on overflow. */
682 (plus (mult @0 @2) { wide_int_to_tree (type, add); }))))
684 /* Optimize A / A to 1.0 if we don't care about
685 NaNs or Infinities. */
688 (if (FLOAT_TYPE_P (type)
689 && ! HONOR_NANS (type)
690 && ! HONOR_INFINITIES (type))
691 { build_one_cst (type); }))
693 /* Optimize -A / A to -1.0 if we don't care about
694 NaNs or Infinities. */
696 (rdiv:C @0 (negate @0))
697 (if (FLOAT_TYPE_P (type)
698 && ! HONOR_NANS (type)
699 && ! HONOR_INFINITIES (type))
700 { build_minus_one_cst (type); }))
702 /* PR71078: x / abs(x) -> copysign (1.0, x) */
704 (rdiv:C (convert? @0) (convert? (abs @0)))
705 (if (SCALAR_FLOAT_TYPE_P (type)
706 && ! HONOR_NANS (type)
707 && ! HONOR_INFINITIES (type))
709 (if (types_match (type, float_type_node))
710 (BUILT_IN_COPYSIGNF { build_one_cst (type); } (convert @0)))
711 (if (types_match (type, double_type_node))
712 (BUILT_IN_COPYSIGN { build_one_cst (type); } (convert @0)))
713 (if (types_match (type, long_double_type_node))
714 (BUILT_IN_COPYSIGNL { build_one_cst (type); } (convert @0))))))
716 /* In IEEE floating point, x/1 is not equivalent to x for snans. */
719 (if (!tree_expr_maybe_signaling_nan_p (@0))
722 /* In IEEE floating point, x/-1 is not equivalent to -x for snans. */
724 (rdiv @0 real_minus_onep)
725 (if (!tree_expr_maybe_signaling_nan_p (@0))
728 (if (flag_reciprocal_math)
729 /* Convert (A/B)/C to A/(B*C). */
731 (rdiv (rdiv:s @0 @1) @2)
732 (rdiv @0 (mult @1 @2)))
734 /* Canonicalize x / (C1 * y) to (x * C2) / y. */
736 (rdiv @0 (mult:s @1 REAL_CST@2))
738 { tree tem = const_binop (RDIV_EXPR, type, build_one_cst (type), @2); }
740 (rdiv (mult @0 { tem; } ) @1))))
742 /* Convert A/(B/C) to (A/B)*C */
744 (rdiv @0 (rdiv:s @1 @2))
745 (mult (rdiv @0 @1) @2)))
747 /* Simplify x / (- y) to -x / y. */
749 (rdiv @0 (negate @1))
750 (rdiv (negate @0) @1))
752 (if (flag_unsafe_math_optimizations)
753 /* Simplify (C / x op 0.0) to x op 0.0 for C != 0, C != Inf/Nan.
754 Since C / x may underflow to zero, do this only for unsafe math. */
755 (for op (lt le gt ge)
758 (op (rdiv REAL_CST@0 @1) real_zerop@2)
759 (if (!HONOR_SIGNED_ZEROS (@1) && !HONOR_INFINITIES (@1))
761 (if (real_less (&dconst0, TREE_REAL_CST_PTR (@0)))
763 /* For C < 0, use the inverted operator. */
764 (if (real_less (TREE_REAL_CST_PTR (@0), &dconst0))
767 /* Optimize (X & (-A)) / A where A is a power of 2, to X >> log2(A) */
768 (for div (trunc_div ceil_div floor_div round_div exact_div)
770 (div (convert? (bit_and @0 INTEGER_CST@1)) INTEGER_CST@2)
771 (if (integer_pow2p (@2)
772 && tree_int_cst_sgn (@2) > 0
773 && tree_nop_conversion_p (type, TREE_TYPE (@0))
774 && wi::to_wide (@2) + wi::to_wide (@1) == 0)
776 { build_int_cst (integer_type_node,
777 wi::exact_log2 (wi::to_wide (@2))); }))))
779 /* If ARG1 is a constant, we can convert this to a multiply by the
780 reciprocal. This does not have the same rounding properties,
781 so only do this if -freciprocal-math. We can actually
782 always safely do it if ARG1 is a power of two, but it's hard to
783 tell if it is or not in a portable manner. */
784 (for cst (REAL_CST COMPLEX_CST VECTOR_CST)
788 (if (flag_reciprocal_math
791 { tree tem = const_binop (RDIV_EXPR, type, build_one_cst (type), @1); }
793 (mult @0 { tem; } )))
794 (if (cst != COMPLEX_CST)
795 (with { tree inverse = exact_inverse (type, @1); }
797 (mult @0 { inverse; } ))))))))
799 (for mod (ceil_mod floor_mod round_mod trunc_mod)
800 /* 0 % X is always zero. */
802 (mod integer_zerop@0 @1)
803 /* But not for 0 % 0 so that we can get the proper warnings and errors. */
804 (if (!integer_zerop (@1))
806 /* X % 1 is always zero. */
808 (mod @0 integer_onep)
809 { build_zero_cst (type); })
810 /* X % -1 is zero. */
812 (mod @0 integer_minus_onep@1)
813 (if (!TYPE_UNSIGNED (type))
814 { build_zero_cst (type); }))
818 /* But not for 0 % 0 so that we can get the proper warnings and errors. */
819 (if (!integer_zerop (@0))
820 { build_zero_cst (type); }))
821 /* (X % Y) % Y is just X % Y. */
823 (mod (mod@2 @0 @1) @1)
825 /* From extract_muldiv_1: (X * C1) % C2 is zero if C1 is a multiple of C2. */
827 (mod (mult @0 INTEGER_CST@1) INTEGER_CST@2)
828 (if (ANY_INTEGRAL_TYPE_P (type)
829 && TYPE_OVERFLOW_UNDEFINED (type)
830 && wi::multiple_of_p (wi::to_wide (@1), wi::to_wide (@2),
832 { build_zero_cst (type); }))
833 /* For (X % C) == 0, if X is signed and C is power of 2, use unsigned
834 modulo and comparison, since it is simpler and equivalent. */
837 (cmp (mod @0 integer_pow2p@2) integer_zerop@1)
838 (if (!TYPE_UNSIGNED (TREE_TYPE (@0)))
839 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
840 (cmp (mod (convert:utype @0) (convert:utype @2)) (convert:utype @1)))))))
842 /* X % -C is the same as X % C. */
844 (trunc_mod @0 INTEGER_CST@1)
845 (if (TYPE_SIGN (type) == SIGNED
846 && !TREE_OVERFLOW (@1)
847 && wi::neg_p (wi::to_wide (@1))
848 && !TYPE_OVERFLOW_TRAPS (type)
849 /* Avoid this transformation if C is INT_MIN, i.e. C == -C. */
850 && !sign_bit_p (@1, @1))
851 (trunc_mod @0 (negate @1))))
853 /* X % -Y is the same as X % Y. */
855 (trunc_mod @0 (convert? (negate @1)))
856 (if (INTEGRAL_TYPE_P (type)
857 && !TYPE_UNSIGNED (type)
858 && !TYPE_OVERFLOW_TRAPS (type)
859 && tree_nop_conversion_p (type, TREE_TYPE (@1))
860 /* Avoid this transformation if X might be INT_MIN or
861 Y might be -1, because we would then change valid
862 INT_MIN % -(-1) into invalid INT_MIN % -1. */
863 && (expr_not_equal_to (@0, wi::to_wide (TYPE_MIN_VALUE (type)))
864 || expr_not_equal_to (@1, wi::minus_one (TYPE_PRECISION
866 (trunc_mod @0 (convert @1))))
868 /* X - (X / Y) * Y is the same as X % Y. */
870 (minus (convert1? @0) (convert2? (mult:c (trunc_div @@0 @@1) @1)))
871 (if (INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
872 (convert (trunc_mod @0 @1))))
874 /* x * (1 + y / x) - y -> x - y % x */
876 (minus (mult:cs @0 (plus:s (trunc_div:s @1 @0) integer_onep)) @1)
877 (if (INTEGRAL_TYPE_P (type))
878 (minus @0 (trunc_mod @1 @0))))
880 /* Optimize TRUNC_MOD_EXPR by a power of two into a BIT_AND_EXPR,
881 i.e. "X % C" into "X & (C - 1)", if X and C are positive.
882 Also optimize A % (C << N) where C is a power of 2,
883 to A & ((C << N) - 1).
884 Also optimize "A shift (B % C)", if C is a power of 2, to
885 "A shift (B & (C - 1))". SHIFT operation include "<<" and ">>"
886 and assume (B % C) is nonnegative as shifts negative values would
888 (match (power_of_two_cand @1)
890 (match (power_of_two_cand @1)
891 (lshift INTEGER_CST@1 @2))
892 (for mod (trunc_mod floor_mod)
893 (for shift (lshift rshift)
895 (shift @0 (mod @1 (power_of_two_cand@2 @3)))
896 (if (integer_pow2p (@3) && tree_int_cst_sgn (@3) > 0)
897 (shift @0 (bit_and @1 (minus @2 { build_int_cst (TREE_TYPE (@2),
900 (mod @0 (convert? (power_of_two_cand@1 @2)))
901 (if ((TYPE_UNSIGNED (type) || tree_expr_nonnegative_p (@0))
902 /* Allow any integral conversions of the divisor, except
903 conversion from narrower signed to wider unsigned type
904 where if @1 would be negative power of two, the divisor
905 would not be a power of two. */
906 && INTEGRAL_TYPE_P (type)
907 && INTEGRAL_TYPE_P (TREE_TYPE (@1))
908 && (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@1))
909 || TYPE_UNSIGNED (TREE_TYPE (@1))
910 || !TYPE_UNSIGNED (type))
911 && integer_pow2p (@2) && tree_int_cst_sgn (@2) > 0)
912 (with { tree utype = TREE_TYPE (@1);
913 if (!TYPE_OVERFLOW_WRAPS (utype))
914 utype = unsigned_type_for (utype); }
915 (bit_and @0 (convert (minus (convert:utype @1)
916 { build_one_cst (utype); })))))))
918 /* Simplify (unsigned t * 2)/2 -> unsigned t & 0x7FFFFFFF. */
920 (trunc_div (mult @0 integer_pow2p@1) @1)
921 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) && TYPE_UNSIGNED (TREE_TYPE (@0)))
922 (bit_and @0 { wide_int_to_tree
923 (type, wi::mask (TYPE_PRECISION (type)
924 - wi::exact_log2 (wi::to_wide (@1)),
925 false, TYPE_PRECISION (type))); })))
927 /* Simplify (unsigned t / 2) * 2 -> unsigned t & ~1. */
929 (mult (trunc_div @0 integer_pow2p@1) @1)
930 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) && TYPE_UNSIGNED (TREE_TYPE (@0)))
931 (bit_and @0 (negate @1))))
933 /* Simplify (t * 2) / 2) -> t. */
934 (for div (trunc_div ceil_div floor_div round_div exact_div)
936 (div (mult:c @0 @1) @1)
937 (if (ANY_INTEGRAL_TYPE_P (type))
938 (if (TYPE_OVERFLOW_UNDEFINED (type))
941 (with {value_range vr0, vr1;}
942 (if (INTEGRAL_TYPE_P (type)
943 && get_range_query (cfun)->range_of_expr (vr0, @0)
944 && get_range_query (cfun)->range_of_expr (vr1, @1)
945 && range_op_handler (MULT_EXPR).overflow_free_p (vr0, vr1))
951 (for div (trunc_div exact_div)
952 /* Simplify (X + M*N) / N -> X / N + M. */
954 (div (plus:c@4 @0 (mult:c@3 @1 @2)) @2)
955 (with {value_range vr0, vr1, vr2, vr3, vr4;}
956 (if (INTEGRAL_TYPE_P (type)
957 && get_range_query (cfun)->range_of_expr (vr1, @1)
958 && get_range_query (cfun)->range_of_expr (vr2, @2)
959 /* "N*M" doesn't overflow. */
960 && range_op_handler (MULT_EXPR).overflow_free_p (vr1, vr2)
961 && get_range_query (cfun)->range_of_expr (vr0, @0)
962 && get_range_query (cfun)->range_of_expr (vr3, @3)
963 /* "X+(N*M)" doesn't overflow. */
964 && range_op_handler (PLUS_EXPR).overflow_free_p (vr0, vr3)
965 && get_range_query (cfun)->range_of_expr (vr4, @4)
966 && !vr4.undefined_p ()
967 /* "X+N*M" is not with opposite sign as "X". */
968 && (TYPE_UNSIGNED (type)
969 || (vr0.nonnegative_p () && vr4.nonnegative_p ())
970 || (vr0.nonpositive_p () && vr4.nonpositive_p ())))
971 (plus (div @0 @2) @1))))
973 /* Simplify (X - M*N) / N -> X / N - M. */
975 (div (minus@4 @0 (mult:c@3 @1 @2)) @2)
976 (with {value_range vr0, vr1, vr2, vr3, vr4;}
977 (if (INTEGRAL_TYPE_P (type)
978 && get_range_query (cfun)->range_of_expr (vr1, @1)
979 && get_range_query (cfun)->range_of_expr (vr2, @2)
980 /* "N * M" doesn't overflow. */
981 && range_op_handler (MULT_EXPR).overflow_free_p (vr1, vr2)
982 && get_range_query (cfun)->range_of_expr (vr0, @0)
983 && get_range_query (cfun)->range_of_expr (vr3, @3)
984 /* "X - (N*M)" doesn't overflow. */
985 && range_op_handler (MINUS_EXPR).overflow_free_p (vr0, vr3)
986 && get_range_query (cfun)->range_of_expr (vr4, @4)
987 && !vr4.undefined_p ()
988 /* "X-N*M" is not with opposite sign as "X". */
989 && (TYPE_UNSIGNED (type)
990 || (vr0.nonnegative_p () && vr4.nonnegative_p ())
991 || (vr0.nonpositive_p () && vr4.nonpositive_p ())))
992 (minus (div @0 @2) @1)))))
995 (X + C) / N -> X / N + C / N where C is multiple of N.
996 (X + C) >> N -> X >> N + C>>N if low N bits of C is 0. */
997 (for op (trunc_div exact_div rshift)
999 (op (plus@3 @0 INTEGER_CST@1) INTEGER_CST@2)
1002 wide_int c = wi::to_wide (@1);
1003 wide_int n = wi::to_wide (@2);
1004 bool shift = op == RSHIFT_EXPR;
1005 #define plus_op1(v) (shift ? wi::rshift (v, n, TYPE_SIGN (type)) \
1006 : wi::div_trunc (v, n, TYPE_SIGN (type)))
1007 #define exact_mod(v) (shift ? wi::ctz (v) >= n.to_shwi () \
1008 : wi::multiple_of_p (v, n, TYPE_SIGN (type)))
1009 value_range vr0, vr1, vr3;
1011 (if (INTEGRAL_TYPE_P (type)
1012 && get_range_query (cfun)->range_of_expr (vr0, @0))
1014 && get_range_query (cfun)->range_of_expr (vr1, @1)
1015 /* "X+C" doesn't overflow. */
1016 && range_op_handler (PLUS_EXPR).overflow_free_p (vr0, vr1)
1017 && get_range_query (cfun)->range_of_expr (vr3, @3)
1018 && !vr3.undefined_p ()
1019 /* "X+C" and "X" are not of opposite sign. */
1020 && (TYPE_UNSIGNED (type)
1021 || (vr0.nonnegative_p () && vr3.nonnegative_p ())
1022 || (vr0.nonpositive_p () && vr3.nonpositive_p ())))
1023 (plus (op @0 @2) { wide_int_to_tree (type, plus_op1 (c)); })
1024 (if (!vr0.undefined_p () && TYPE_UNSIGNED (type) && c.sign_mask () < 0
1026 /* unsigned "X-(-C)" doesn't underflow. */
1027 && wi::geu_p (vr0.lower_bound (), -c))
1028 (plus (op @0 @2) { wide_int_to_tree (type, -plus_op1 (-c)); })))))))
1033 /* (nop_outer_cast)-(inner_cast)var -> -(outer_cast)(var)
1034 if var is smaller in precision.
1035 This is always safe for both doing the negative in signed or unsigned
1036 as the value for undefined will not show up. */
1038 (convert (negate:s@1 (convert:s @0)))
1039 (if (INTEGRAL_TYPE_P (type)
1040 && tree_nop_conversion_p (type, TREE_TYPE (@1))
1041 && TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (@0)))
1042 (negate (convert @0))))
1044 (for op (negate abs)
1045 /* Simplify cos(-x) and cos(|x|) -> cos(x). Similarly for cosh. */
1046 (for coss (COS COSH)
1050 /* Simplify pow(-x, y) and pow(|x|,y) -> pow(x,y) if y is an even integer. */
1053 (pows (op @0) REAL_CST@1)
1054 (with { HOST_WIDE_INT n; }
1055 (if (real_isinteger (&TREE_REAL_CST (@1), &n) && (n & 1) == 0)
1057 /* Likewise for powi. */
1060 (pows (op @0) INTEGER_CST@1)
1061 (if ((wi::to_wide (@1) & 1) == 0)
1063 /* Strip negate and abs from both operands of hypot. */
1071 /* copysign(-x, y) and copysign(abs(x), y) -> copysign(x, y). */
1072 (for copysigns (COPYSIGN_ALL)
1074 (copysigns (op @0) @1)
1075 (copysigns @0 @1))))
1077 /* abs(x)*abs(x) -> x*x. Should be valid for all types. */
1079 (mult (abs@1 @0) @1)
1082 /* Convert absu(x)*absu(x) -> x*x. */
1084 (mult (absu@1 @0) @1)
1085 (mult (convert@2 @0) @2))
1087 /* cos(copysign(x, y)) -> cos(x). Similarly for cosh. */
1088 (for coss (COS COSH)
1089 copysigns (COPYSIGN)
1091 (coss (copysigns @0 @1))
1094 /* pow(copysign(x, y), z) -> pow(x, z) if z is an even integer. */
1096 copysigns (COPYSIGN)
1098 (pows (copysigns @0 @2) REAL_CST@1)
1099 (with { HOST_WIDE_INT n; }
1100 (if (real_isinteger (&TREE_REAL_CST (@1), &n) && (n & 1) == 0)
1102 /* Likewise for powi. */
1104 copysigns (COPYSIGN)
1106 (pows (copysigns @0 @2) INTEGER_CST@1)
1107 (if ((wi::to_wide (@1) & 1) == 0)
1111 copysigns (COPYSIGN)
1112 /* hypot(copysign(x, y), z) -> hypot(x, z). */
1114 (hypots (copysigns @0 @1) @2)
1116 /* hypot(x, copysign(y, z)) -> hypot(x, y). */
1118 (hypots @0 (copysigns @1 @2))
1121 /* copysign(x, CST) -> [-]abs (x). */
1122 (for copysigns (COPYSIGN_ALL)
1124 (copysigns @0 REAL_CST@1)
1125 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
1129 /* copysign(copysign(x, y), z) -> copysign(x, z). */
1130 (for copysigns (COPYSIGN_ALL)
1132 (copysigns (copysigns @0 @1) @2)
1135 /* copysign(x,y)*copysign(x,y) -> x*x. */
1136 (for copysigns (COPYSIGN_ALL)
1138 (mult (copysigns@2 @0 @1) @2)
1141 /* ccos(-x) -> ccos(x). Similarly for ccosh. */
1142 (for ccoss (CCOS CCOSH)
1147 /* cabs(-x) and cos(conj(x)) -> cabs(x). */
1148 (for ops (conj negate)
1154 /* Fold (a * (1 << b)) into (a << b) */
1156 (mult:c @0 (convert? (lshift integer_onep@1 @2)))
1157 (if (! FLOAT_TYPE_P (type)
1158 && tree_nop_conversion_p (type, TREE_TYPE (@1)))
1161 /* Shifts by precision or greater result in zero. */
1162 (for shift (lshift rshift)
1164 (shift @0 uniform_integer_cst_p@1)
1165 (if ((GIMPLE || !sanitize_flags_p (SANITIZE_SHIFT_EXPONENT))
1166 /* Leave arithmetic right shifts of possibly negative values alone. */
1167 && (TYPE_UNSIGNED (type)
1168 || shift == LSHIFT_EXPR
1169 || tree_expr_nonnegative_p (@0))
1170 /* Use a signed compare to leave negative shift counts alone. */
1171 && wi::ges_p (wi::to_wide (uniform_integer_cst_p (@1)),
1172 element_precision (type)))
1173 { build_zero_cst (type); })))
1175 /* Shifts by constants distribute over several binary operations,
1176 hence (X << C) + (Y << C) can be simplified to (X + Y) << C. */
1177 (for op (plus minus)
1179 (op (lshift:s @0 @1) (lshift:s @2 @1))
1180 (if (INTEGRAL_TYPE_P (type)
1181 && TYPE_OVERFLOW_WRAPS (type)
1182 && !TYPE_SATURATING (type))
1183 (lshift (op @0 @2) @1))))
1185 (for op (bit_and bit_ior bit_xor)
1187 (op (lshift:s @0 @1) (lshift:s @2 @1))
1188 (if (INTEGRAL_TYPE_P (type))
1189 (lshift (op @0 @2) @1)))
1191 (op (rshift:s @0 @1) (rshift:s @2 @1))
1192 (if (INTEGRAL_TYPE_P (type))
1193 (rshift (op @0 @2) @1))))
1195 /* Fold (1 << (C - x)) where C = precision(type) - 1
1196 into ((1 << C) >> x). */
1198 (lshift integer_onep@0 (minus@1 INTEGER_CST@2 @3))
1199 (if (INTEGRAL_TYPE_P (type)
1200 && wi::eq_p (wi::to_wide (@2), TYPE_PRECISION (type) - 1)
1202 (if (TYPE_UNSIGNED (type))
1203 (rshift (lshift @0 @2) @3)
1205 { tree utype = unsigned_type_for (type); }
1206 (convert (rshift (lshift (convert:utype @0) @2) @3))))))
1208 /* Fold ((type)(a<0)) << SIGNBITOFA into ((type)a) & signbit. */
1210 (lshift (convert (lt @0 integer_zerop@1)) INTEGER_CST@2)
1211 (if (TYPE_SIGN (TREE_TYPE (@0)) == SIGNED
1212 && wi::eq_p (wi::to_wide (@2), TYPE_PRECISION (TREE_TYPE (@0)) - 1))
1213 (with { wide_int wone = wi::one (TYPE_PRECISION (type)); }
1214 (bit_and (convert @0)
1215 { wide_int_to_tree (type,
1216 wi::lshift (wone, wi::to_wide (@2))); }))))
1218 /* Fold (-x >> C) into -(x > 0) where C = precision(type) - 1. */
1219 (for cst (INTEGER_CST VECTOR_CST)
1221 (rshift (negate:s @0) cst@1)
1222 (if (!TYPE_UNSIGNED (type)
1223 && TYPE_OVERFLOW_UNDEFINED (type))
1224 (with { tree stype = TREE_TYPE (@1);
1225 tree bt = truth_type_for (type);
1226 tree zeros = build_zero_cst (type);
1227 tree cst = NULL_TREE; }
1229 /* Handle scalar case. */
1230 (if (INTEGRAL_TYPE_P (type)
1231 /* If we apply the rule to the scalar type before vectorization
1232 we will enforce the result of the comparison being a bool
1233 which will require an extra AND on the result that will be
1234 indistinguishable from when the user did actually want 0
1235 or 1 as the result so it can't be removed. */
1236 && canonicalize_math_after_vectorization_p ()
1237 && wi::eq_p (wi::to_wide (@1), TYPE_PRECISION (type) - 1))
1238 (negate (convert (gt @0 { zeros; }))))
1239 /* Handle vector case. */
1240 (if (VECTOR_INTEGER_TYPE_P (type)
1241 /* First check whether the target has the same mode for vector
1242 comparison results as it's operands do. */
1243 && TYPE_MODE (bt) == TYPE_MODE (type)
1244 /* Then check to see if the target is able to expand the comparison
1245 with the given type later on, otherwise we may ICE. */
1246 && expand_vec_cmp_expr_p (type, bt, GT_EXPR)
1247 && (cst = uniform_integer_cst_p (@1)) != NULL
1248 && wi::eq_p (wi::to_wide (cst), element_precision (type) - 1))
1249 (view_convert (gt:bt @0 { zeros; }))))))))
1251 /* Fold (C1/X)*C2 into (C1*C2)/X. */
1253 (mult (rdiv@3 REAL_CST@0 @1) REAL_CST@2)
1254 (if (flag_associative_math
1257 { tree tem = const_binop (MULT_EXPR, type, @0, @2); }
1259 (rdiv { tem; } @1)))))
1261 /* Simplify ~X & X as zero. */
1263 (bit_and (convert? @0) (convert? @1))
1264 (with { bool wascmp; }
1265 (if (types_match (TREE_TYPE (@0), TREE_TYPE (@1))
1266 && bitwise_inverted_equal_p (@0, @1, wascmp))
1267 { wascmp ? constant_boolean_node (false, type) : build_zero_cst (type); })))
1269 /* PR71636: Transform x & ((1U << b) - 1) -> x & ~(~0U << b); */
1271 (bit_and:c @0 (plus:s (lshift:s integer_onep @1) integer_minus_onep))
1272 (if (TYPE_UNSIGNED (type))
1273 (bit_and @0 (bit_not (lshift { build_all_ones_cst (type); } @1)))))
1275 (for bitop (bit_and bit_ior)
1277 /* PR35691: Transform
1278 (x == 0 & y == 0) -> (x | typeof(x)(y)) == 0.
1279 (x != 0 | y != 0) -> (x | typeof(x)(y)) != 0. */
1281 (bitop (cmp @0 integer_zerop@2) (cmp @1 integer_zerop))
1282 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1283 && INTEGRAL_TYPE_P (TREE_TYPE (@1))
1284 && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
1285 (cmp (bit_ior @0 (convert @1)) @2)))
1287 (x == -1 & y == -1) -> (x & typeof(x)(y)) == -1.
1288 (x != -1 | y != -1) -> (x & typeof(x)(y)) != -1. */
1290 (bitop (cmp @0 integer_all_onesp@2) (cmp @1 integer_all_onesp))
1291 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1292 && INTEGRAL_TYPE_P (TREE_TYPE (@1))
1293 && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
1294 (cmp (bit_and @0 (convert @1)) @2))))
1296 /* Fold (A & ~B) - (A & B) into (A ^ B) - B. */
1298 (minus (bit_and:cs @0 (bit_not @1)) (bit_and:cs @0 @1))
1299 (minus (bit_xor @0 @1) @1))
1301 (minus (bit_and:s @0 INTEGER_CST@2) (bit_and:s @0 INTEGER_CST@1))
1302 (if (~wi::to_wide (@2) == wi::to_wide (@1))
1303 (minus (bit_xor @0 @1) @1)))
1305 /* Fold (A & B) - (A & ~B) into B - (A ^ B). */
1307 (minus (bit_and:cs @0 @1) (bit_and:cs @0 (bit_not @1)))
1308 (minus @1 (bit_xor @0 @1)))
1310 /* Simplify (X & ~Y) |^+ (~X & Y) -> X ^ Y. */
1311 (for op (bit_ior bit_xor plus)
1313 (op (bit_and:c @0 @2) (bit_and:c @3 @1))
1314 (with { bool wascmp0, wascmp1; }
1315 (if (bitwise_inverted_equal_p (@2, @1, wascmp0)
1316 && bitwise_inverted_equal_p (@0, @3, wascmp1)
1317 && ((!wascmp0 && !wascmp1)
1318 || element_precision (type) == 1))
1321 /* PR53979: Transform ((a ^ b) | a) -> (a | b) */
1323 (bit_ior:c (bit_xor:c @0 @1) @0)
1326 /* (a & ~b) | (a ^ b) --> a ^ b */
1328 (bit_ior:c (bit_and:c @0 (bit_not @1)) (bit_xor:c@2 @0 @1))
1331 /* (a & ~b) ^ ~a --> ~(a & b) */
1333 (bit_xor:c (bit_and:cs @0 (bit_not @1)) (bit_not @0))
1334 (bit_not (bit_and @0 @1)))
1336 /* (~a & b) ^ a --> (a | b) */
1338 (bit_xor:c (bit_and:cs (bit_not @0) @1) @0)
1341 /* (a | b) & ~(a ^ b) --> a & b */
1343 (bit_and:c (bit_ior @0 @1) (bit_not (bit_xor:c @0 @1)))
1346 /* (a | b) & (a == b) --> a & b (boolean version of the above). */
1348 (bit_and:c (bit_ior @0 @1) (nop_convert? (eq:c @0 @1)))
1349 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1350 && TYPE_PRECISION (TREE_TYPE (@0)) == 1)
1353 /* a | ~(a ^ b) --> a | ~b */
1355 (bit_ior:c @0 (bit_not:s (bit_xor:c @0 @1)))
1356 (bit_ior @0 (bit_not @1)))
1358 /* a | (a == b) --> a | (b^1) (boolean version of the above). */
1360 (bit_ior:c @0 (nop_convert? (eq:c @0 @1)))
1361 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1362 && TYPE_PRECISION (TREE_TYPE (@0)) == 1)
1363 (bit_ior @0 (bit_xor @1 { build_one_cst (type); }))))
1365 /* a | ((~a) ^ b) --> a | (~b) (alt version of the above 2) */
1367 (bit_ior:c @0 (bit_xor:cs @1 @2))
1368 (with { bool wascmp; }
1369 (if (bitwise_inverted_equal_p (@0, @1, wascmp)
1370 && (!wascmp || element_precision (type) == 1))
1371 (bit_ior @0 (bit_not @2)))))
1373 /* a & ~(a ^ b) --> a & b */
1375 (bit_and:c @0 (bit_not (bit_xor:c @0 @1)))
1378 /* a & (a == b) --> a & b (boolean version of the above). */
1380 (bit_and:c @0 (nop_convert? (eq:c @0 @1)))
1381 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1382 && TYPE_PRECISION (TREE_TYPE (@0)) == 1)
1385 /* a & ((~a) ^ b) --> a & b (alt version of the above 2) */
1387 (bit_and:c @0 (bit_xor:c @1 @2))
1388 (with { bool wascmp; }
1389 (if (bitwise_inverted_equal_p (@0, @1, wascmp)
1390 && (!wascmp || element_precision (type) == 1))
1393 /* (a | b) | (a &^ b) --> a | b */
1394 (for op (bit_and bit_xor)
1396 (bit_ior:c (bit_ior@2 @0 @1) (op:c @0 @1))
1399 /* (a & b) | ~(a ^ b) --> ~(a ^ b) */
1401 (bit_ior:c (bit_and:c @0 @1) (bit_not@2 (bit_xor @0 @1)))
1404 /* (a & b) | (a == b) --> a == b */
1406 (bit_ior:c (bit_and:c @0 @1) (nop_convert?@2 (eq @0 @1)))
1407 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1408 && TYPE_PRECISION (TREE_TYPE (@0)) == 1)
1411 /* ~(~a & b) --> a | ~b */
1413 (bit_not (bit_and:cs (bit_not @0) @1))
1414 (bit_ior @0 (bit_not @1)))
1416 /* ~(~a | b) --> a & ~b */
1418 (bit_not (bit_ior:cs (bit_not @0) @1))
1419 (bit_and @0 (bit_not @1)))
1421 /* (a ^ b) & ((b ^ c) ^ a) --> (a ^ b) & ~c */
1423 (bit_and:c (bit_xor:c@3 @0 @1) (bit_xor:cs (bit_xor:cs @1 @2) @0))
1424 (bit_and @3 (bit_not @2)))
1426 /* (a ^ b) | ((b ^ c) ^ a) --> (a ^ b) | c */
1428 (bit_ior:c (bit_xor:c@3 @0 @1) (bit_xor:c (bit_xor:c @1 @2) @0))
1431 /* (~X | C) ^ D -> (X | C) ^ (~D ^ C) if (~D ^ C) can be simplified. */
1433 (bit_xor:c (bit_ior:cs (bit_not:s @0) @1) @2)
1434 (bit_xor (bit_ior @0 @1) (bit_xor! (bit_not! @2) @1)))
1436 /* (~X & C) ^ D -> (X & C) ^ (D ^ C) if (D ^ C) can be simplified. */
1438 (bit_xor:c (bit_and:cs (bit_not:s @0) @1) @2)
1439 (bit_xor (bit_and @0 @1) (bit_xor! @2 @1)))
1441 /* Simplify (~X & Y) to X ^ Y if we know that (X & ~Y) is 0. */
1443 (bit_and (bit_not SSA_NAME@0) INTEGER_CST@1)
1444 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1445 && wi::bit_and_not (get_nonzero_bits (@0), wi::to_wide (@1)) == 0)
1448 /* For constants M and N, if M == (1LL << cst) - 1 && (N & M) == M,
1449 ((A & N) + B) & M -> (A + B) & M
1450 Similarly if (N & M) == 0,
1451 ((A | N) + B) & M -> (A + B) & M
1452 and for - instead of + (or unary - instead of +)
1453 and/or ^ instead of |.
1454 If B is constant and (B & M) == 0, fold into A & M. */
1455 (for op (plus minus)
1456 (for bitop (bit_and bit_ior bit_xor)
1458 (bit_and (op:s (bitop:s@0 @3 INTEGER_CST@4) @1) INTEGER_CST@2)
1461 tree utype = fold_bit_and_mask (TREE_TYPE (@0), @2, op, @0, bitop,
1462 @3, @4, @1, ERROR_MARK, NULL_TREE,
1465 (convert (bit_and (op (convert:utype { pmop[0]; })
1466 (convert:utype { pmop[1]; }))
1467 (convert:utype @2))))))
1469 (bit_and (op:s @0 (bitop:s@1 @3 INTEGER_CST@4)) INTEGER_CST@2)
1472 tree utype = fold_bit_and_mask (TREE_TYPE (@0), @2, op, @0, ERROR_MARK,
1473 NULL_TREE, NULL_TREE, @1, bitop, @3,
1476 (convert (bit_and (op (convert:utype { pmop[0]; })
1477 (convert:utype { pmop[1]; }))
1478 (convert:utype @2)))))))
1480 (bit_and (op:s @0 @1) INTEGER_CST@2)
1483 tree utype = fold_bit_and_mask (TREE_TYPE (@0), @2, op, @0, ERROR_MARK,
1484 NULL_TREE, NULL_TREE, @1, ERROR_MARK,
1485 NULL_TREE, NULL_TREE, pmop); }
1487 (convert (bit_and (op (convert:utype { pmop[0]; })
1488 (convert:utype { pmop[1]; }))
1489 (convert:utype @2)))))))
1490 (for bitop (bit_and bit_ior bit_xor)
1492 (bit_and (negate:s (bitop:s@0 @2 INTEGER_CST@3)) INTEGER_CST@1)
1495 tree utype = fold_bit_and_mask (TREE_TYPE (@0), @1, NEGATE_EXPR, @0,
1496 bitop, @2, @3, NULL_TREE, ERROR_MARK,
1497 NULL_TREE, NULL_TREE, pmop); }
1499 (convert (bit_and (negate (convert:utype { pmop[0]; }))
1500 (convert:utype @1)))))))
1502 /* X % Y is smaller than Y. */
1505 (cmp:c (trunc_mod @0 @1) @1)
1506 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
1507 { constant_boolean_node (cmp == LT_EXPR, type); })))
1511 (bit_ior @0 integer_all_onesp@1)
1516 (bit_ior @0 integer_zerop)
1521 (bit_and @0 integer_zerop@1)
1526 (for op (bit_ior bit_xor)
1528 (op (convert? @0) (convert? @1))
1529 (with { bool wascmp; }
1530 (if (types_match (TREE_TYPE (@0), TREE_TYPE (@1))
1531 && bitwise_inverted_equal_p (@0, @1, wascmp))
1534 ? constant_boolean_node (true, type)
1535 : build_all_ones_cst (TREE_TYPE (@0)); })))))
1540 { build_zero_cst (type); })
1542 /* Canonicalize X ^ ~0 to ~X. */
1544 (bit_xor @0 integer_all_onesp@1)
1549 (bit_and @0 integer_all_onesp)
1552 /* x & x -> x, x | x -> x */
1553 (for bitop (bit_and bit_ior)
1558 /* x & C -> x if we know that x & ~C == 0. */
1561 (bit_and SSA_NAME@0 INTEGER_CST@1)
1562 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1563 && wi::bit_and_not (get_nonzero_bits (@0), wi::to_wide (@1)) == 0)
1566 /* `a & (x | CST)` -> a if we know that (a & ~CST) == 0 */
1568 (bit_and:c SSA_NAME@0 (bit_ior @1 INTEGER_CST@2))
1569 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1570 && wi::bit_and_not (get_nonzero_bits (@0), wi::to_wide (@2)) == 0)
1573 /* x | C -> C if we know that x & ~C == 0. */
1575 (bit_ior SSA_NAME@0 INTEGER_CST@1)
1576 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1577 && wi::bit_and_not (get_nonzero_bits (@0), wi::to_wide (@1)) == 0)
1581 /* ~(~X - Y) -> X + Y and ~(~X + Y) -> X - Y. */
1583 (bit_not (minus (bit_not @0) @1))
1586 (bit_not (plus:c (bit_not @0) @1))
1588 /* (~X - ~Y) -> Y - X. */
1590 (minus (bit_not @0) (bit_not @1))
1591 (if (!TYPE_OVERFLOW_SANITIZED (type))
1592 (with { tree utype = unsigned_type_for (type); }
1593 (convert (minus (convert:utype @1) (convert:utype @0))))))
1595 /* ~(X - Y) -> ~X + Y. */
1597 (bit_not (minus:s @0 @1))
1598 (plus (bit_not @0) @1))
1600 (bit_not (plus:s @0 INTEGER_CST@1))
1601 (if ((INTEGRAL_TYPE_P (type)
1602 && TYPE_UNSIGNED (type))
1603 || (!TYPE_OVERFLOW_SANITIZED (type)
1604 && may_negate_without_overflow_p (@1)))
1605 (plus (bit_not @0) { const_unop (NEGATE_EXPR, type, @1); })))
1608 /* ~X + Y -> (Y - X) - 1. */
1610 (plus:c (bit_not @0) @1)
1611 (if (ANY_INTEGRAL_TYPE_P (type)
1612 && TYPE_OVERFLOW_WRAPS (type)
1613 /* -1 - X is folded to ~X, so we'd recurse endlessly. */
1614 && !integer_all_onesp (@1))
1615 (plus (minus @1 @0) { build_minus_one_cst (type); })
1616 (if (INTEGRAL_TYPE_P (type)
1617 && TREE_CODE (@1) == INTEGER_CST
1618 && wi::to_wide (@1) != wi::min_value (TYPE_PRECISION (type),
1620 (minus (plus @1 { build_minus_one_cst (type); }) @0))))
1623 /* ~(X >> Y) -> ~X >> Y if ~X can be simplified. */
1625 (bit_not (rshift:s @0 @1))
1626 (if (!TYPE_UNSIGNED (TREE_TYPE (@0)))
1627 (rshift (bit_not! @0) @1)
1628 /* For logical right shifts, this is possible only if @0 doesn't
1629 have MSB set and the logical right shift is changed into
1630 arithmetic shift. */
1631 (if (INTEGRAL_TYPE_P (type)
1632 && !wi::neg_p (tree_nonzero_bits (@0)))
1633 (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
1634 (convert (rshift (bit_not! (convert:stype @0)) @1))))))
1636 /* x + (x & 1) -> (x + 1) & ~1 */
1638 (plus:c @0 (bit_and:s @0 integer_onep@1))
1639 (bit_and (plus @0 @1) (bit_not @1)))
1641 /* x & ~(x & y) -> x & ~y */
1642 /* x | ~(x | y) -> x | ~y */
1643 (for bitop (bit_and bit_ior)
1645 (bitop:c @0 (bit_not (bitop:cs @0 @1)))
1646 (bitop @0 (bit_not @1))))
1648 /* (~x & y) | ~(x | y) -> ~x */
1650 (bit_ior:c (bit_and:c (bit_not@2 @0) @1) (bit_not (bit_ior:c @0 @1)))
1653 /* (x | y) ^ (x | ~y) -> ~x */
1655 (bit_xor:c (bit_ior:c @0 @1) (bit_ior:c @0 (bit_not @1)))
1658 /* (x & y) | ~(x | y) -> ~(x ^ y) */
1660 (bit_ior:c (bit_and:s @0 @1) (bit_not:s (bit_ior:s @0 @1)))
1661 (bit_not (bit_xor @0 @1)))
1663 /* (~x | y) ^ (x ^ y) -> x | ~y */
1665 (bit_xor:c (bit_ior:cs (bit_not @0) @1) (bit_xor:s @0 @1))
1666 (bit_ior @0 (bit_not @1)))
1668 /* (x ^ y) | ~(x | y) -> ~(x & y) */
1670 (bit_ior:c (bit_xor:s @0 @1) (bit_not:s (bit_ior:s @0 @1)))
1671 (bit_not (bit_and @0 @1)))
1673 /* (x & y) ^ (x | y) -> x ^ y */
1675 (bit_xor:c (bit_and @0 @1) (bit_ior @0 @1))
1678 /* (x ^ y) ^ (x | y) -> x & y */
1680 (bit_xor:c (bit_xor @0 @1) (bit_ior @0 @1))
1683 /* (x & y) + (x ^ y) -> x | y */
1684 /* (x & y) | (x ^ y) -> x | y */
1685 /* (x & y) ^ (x ^ y) -> x | y */
1686 (for op (plus bit_ior bit_xor)
1688 (op:c (bit_and @0 @1) (bit_xor @0 @1))
1691 /* (x & y) + (x | y) -> x + y */
1693 (plus:c (bit_and @0 @1) (bit_ior @0 @1))
1696 /* (x + y) - (x | y) -> x & y */
1698 (minus (plus @0 @1) (bit_ior @0 @1))
1699 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1700 && !TYPE_SATURATING (type))
1703 /* (x + y) - (x & y) -> x | y */
1705 (minus (plus @0 @1) (bit_and @0 @1))
1706 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1707 && !TYPE_SATURATING (type))
1710 /* (x | y) - y -> (x & ~y) */
1712 (minus (bit_ior:cs @0 @1) @1)
1713 (bit_and @0 (bit_not @1)))
1715 /* (x | y) - (x ^ y) -> x & y */
1717 (minus (bit_ior @0 @1) (bit_xor @0 @1))
1720 /* (x | y) - (x & y) -> x ^ y */
1722 (minus (bit_ior @0 @1) (bit_and @0 @1))
1725 /* (x | y) & ~(x & y) -> x ^ y */
1727 (bit_and:c (bit_ior @0 @1) (bit_not (bit_and @0 @1)))
1730 /* (x | y) & (~x ^ y) -> x & y */
1732 (bit_and:c (bit_ior:c @0 @1) (bit_xor:c @1 @2))
1733 (with { bool wascmp; }
1734 (if (bitwise_inverted_equal_p (@0, @2, wascmp)
1735 && (!wascmp || element_precision (type) == 1))
1738 /* (~x | y) & (x | ~y) -> ~(x ^ y) */
1740 (bit_and (bit_ior:cs (bit_not @0) @1) (bit_ior:cs @0 (bit_not @1)))
1741 (bit_not (bit_xor @0 @1)))
1743 /* (~x | y) ^ (x | ~y) -> x ^ y */
1745 (bit_xor (bit_ior:c (bit_not @0) @1) (bit_ior:c @0 (bit_not @1)))
1748 /* ((x & y) - (x | y)) - 1 -> ~(x ^ y) */
1750 (plus (nop_convert1? (minus@2 (nop_convert2? (bit_and:c @0 @1))
1751 (nop_convert2? (bit_ior @0 @1))))
1753 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1754 && !TYPE_SATURATING (type) && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2))
1755 && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@2))
1756 && !TYPE_SATURATING (TREE_TYPE (@2)))
1757 (bit_not (convert (bit_xor @0 @1)))))
1759 (minus (nop_convert1? (plus@2 (nop_convert2? (bit_and:c @0 @1))
1761 (nop_convert3? (bit_ior @0 @1)))
1762 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1763 && !TYPE_SATURATING (type) && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2))
1764 && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@2))
1765 && !TYPE_SATURATING (TREE_TYPE (@2)))
1766 (bit_not (convert (bit_xor @0 @1)))))
1768 (minus (nop_convert1? (bit_and @0 @1))
1769 (nop_convert2? (plus@2 (nop_convert3? (bit_ior:c @0 @1))
1771 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1772 && !TYPE_SATURATING (type) && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2))
1773 && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@2))
1774 && !TYPE_SATURATING (TREE_TYPE (@2)))
1775 (bit_not (convert (bit_xor @0 @1)))))
1777 /* ~x & ~y -> ~(x | y)
1778 ~x | ~y -> ~(x & y) */
1779 (for op (bit_and bit_ior)
1780 rop (bit_ior bit_and)
1782 (op (convert1? (bit_not @0)) (convert2? (bit_not @1)))
1783 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1784 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
1785 (bit_not (rop (convert @0) (convert @1))))))
1787 /* If we are XORing or adding two BIT_AND_EXPR's, both of which are and'ing
1788 with a constant, and the two constants have no bits in common,
1789 we should treat this as a BIT_IOR_EXPR since this may produce more
1791 (for op (bit_xor plus)
1793 (op (convert1? (bit_and@4 @0 INTEGER_CST@1))
1794 (convert2? (bit_and@5 @2 INTEGER_CST@3)))
1795 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
1796 && tree_nop_conversion_p (type, TREE_TYPE (@2))
1797 && (wi::to_wide (@1) & wi::to_wide (@3)) == 0)
1798 (bit_ior (convert @4) (convert @5)))))
1800 /* (X | Y) ^ X -> Y & ~ X*/
1802 (bit_xor:c (convert1? (bit_ior:c @@0 @1)) (convert2? @0))
1803 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1804 (convert (bit_and @1 (bit_not @0)))))
1806 /* (~X | Y) ^ X -> ~(X & Y). */
1808 (bit_xor:c (nop_convert1? (bit_ior:c (nop_convert2? (bit_not @0)) @1)) @2)
1809 (if (bitwise_equal_p (@0, @2))
1810 (convert (bit_not (bit_and @0 (convert @1))))))
1812 /* Convert ~X ^ ~Y to X ^ Y. */
1814 (bit_xor (convert1? (bit_not @0)) (convert2? (bit_not @1)))
1815 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1816 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
1817 (bit_xor (convert @0) (convert @1))))
1819 /* Convert ~X ^ C to X ^ ~C. */
1821 (bit_xor (convert? (bit_not @0)) INTEGER_CST@1)
1822 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1823 (bit_xor (convert @0) (bit_not @1))))
1825 /* Fold (X & Y) ^ Y and (X ^ Y) & Y as ~X & Y. */
1826 (for opo (bit_and bit_xor)
1827 opi (bit_xor bit_and)
1829 (opo:c (opi:cs @0 @1) @1)
1830 (bit_and (bit_not @0) @1)))
1832 /* Given a bit-wise operation CODE applied to ARG0 and ARG1, see if both
1833 operands are another bit-wise operation with a common input. If so,
1834 distribute the bit operations to save an operation and possibly two if
1835 constants are involved. For example, convert
1836 (A | B) & (A | C) into A | (B & C)
1837 Further simplification will occur if B and C are constants. */
1838 (for op (bit_and bit_ior bit_xor)
1839 rop (bit_ior bit_and bit_and)
1841 (op (convert? (rop:c @@0 @1)) (convert? (rop:c @0 @2)))
1842 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1843 && tree_nop_conversion_p (type, TREE_TYPE (@2)))
1844 (rop (convert @0) (op (convert @1) (convert @2))))))
1846 /* Some simple reassociation for bit operations, also handled in reassoc. */
1847 /* (X & Y) & Y -> X & Y
1848 (X | Y) | Y -> X | Y */
1849 (for op (bit_and bit_ior)
1851 (op:c (convert1?@2 (op:c @0 @@1)) (convert2? @1))
1853 /* (X ^ Y) ^ Y -> X */
1855 (bit_xor:c (convert1? (bit_xor:c @0 @@1)) (convert2? @1))
1858 /* (X & ~Y) & Y -> 0 */
1860 (bit_and:c (bit_and @0 @1) @2)
1861 (with { bool wascmp; }
1862 (if (bitwise_inverted_equal_p (@0, @2, wascmp)
1863 || bitwise_inverted_equal_p (@1, @2, wascmp))
1864 { wascmp ? constant_boolean_node (false, type) : build_zero_cst (type); })))
1865 /* (X | ~Y) | Y -> -1 */
1867 (bit_ior:c (bit_ior @0 @1) @2)
1868 (with { bool wascmp; }
1869 (if ((bitwise_inverted_equal_p (@0, @2, wascmp)
1870 || bitwise_inverted_equal_p (@1, @2, wascmp))
1871 && (!wascmp || element_precision (type) == 1))
1872 { build_all_ones_cst (TREE_TYPE (@0)); })))
1874 /* (X & Y) & (X & Z) -> (X & Y) & Z
1875 (X | Y) | (X | Z) -> (X | Y) | Z */
1876 (for op (bit_and bit_ior)
1878 (op (convert1?@3 (op:c@4 @0 @1)) (convert2?@5 (op:c@6 @0 @2)))
1879 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1880 && tree_nop_conversion_p (type, TREE_TYPE (@2)))
1881 (if (single_use (@5) && single_use (@6))
1882 (op @3 (convert @2))
1883 (if (single_use (@3) && single_use (@4))
1884 (op (convert @1) @5))))))
1885 /* (X ^ Y) ^ (X ^ Z) -> Y ^ Z */
1887 (bit_xor (convert1? (bit_xor:c @0 @1)) (convert2? (bit_xor:c @0 @2)))
1888 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1889 && tree_nop_conversion_p (type, TREE_TYPE (@2)))
1890 (bit_xor (convert @1) (convert @2))))
1892 /* Convert abs (abs (X)) into abs (X).
1893 also absu (absu (X)) into absu (X). */
1899 (absu (convert@2 (absu@1 @0)))
1900 (if (tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@1)))
1903 /* Convert abs[u] (-X) -> abs[u] (X). */
1912 /* Convert abs[u] (X) where X is nonnegative -> (X). */
1914 (abs tree_expr_nonnegative_p@0)
1918 (absu tree_expr_nonnegative_p@0)
1921 /* Simplify (-(X < 0) | 1) * X into abs (X) or absu(X). */
1923 (mult:c (nop_convert1?
1924 (bit_ior (nop_convert2? (negate (convert? (lt @0 integer_zerop))))
1927 (if (INTEGRAL_TYPE_P (type)
1928 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
1929 && !TYPE_UNSIGNED (TREE_TYPE (@0)))
1930 (if (TYPE_UNSIGNED (type))
1937 /* A few cases of fold-const.cc negate_expr_p predicate. */
1938 (match negate_expr_p
1940 (if ((INTEGRAL_TYPE_P (type)
1941 && TYPE_UNSIGNED (type))
1942 || (!TYPE_OVERFLOW_SANITIZED (type)
1943 && may_negate_without_overflow_p (t)))))
1944 (match negate_expr_p
1946 (match negate_expr_p
1948 (if (!TYPE_OVERFLOW_SANITIZED (type))))
1949 (match negate_expr_p
1951 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (t)))))
1952 /* VECTOR_CST handling of non-wrapping types would recurse in unsupported
1954 (match negate_expr_p
1956 (if (FLOAT_TYPE_P (TREE_TYPE (type)) || TYPE_OVERFLOW_WRAPS (type))))
1957 (match negate_expr_p
1959 (if ((ANY_INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_WRAPS (type))
1960 || (FLOAT_TYPE_P (type)
1961 && !HONOR_SIGN_DEPENDENT_ROUNDING (type)
1962 && !HONOR_SIGNED_ZEROS (type)))))
1964 /* (-A) * (-B) -> A * B */
1966 (mult:c (convert1? (negate @0)) (convert2? negate_expr_p@1))
1967 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
1968 && tree_nop_conversion_p (type, TREE_TYPE (@1)))
1969 (mult (convert @0) (convert (negate @1)))))
1971 /* -(A + B) -> (-B) - A. */
1973 (negate (plus:c @0 negate_expr_p@1))
1974 (if (!HONOR_SIGN_DEPENDENT_ROUNDING (type)
1975 && !HONOR_SIGNED_ZEROS (type))
1976 (minus (negate @1) @0)))
1978 /* -(A - B) -> B - A. */
1980 (negate (minus @0 @1))
1981 (if ((ANY_INTEGRAL_TYPE_P (type) && !TYPE_OVERFLOW_SANITIZED (type))
1982 || (FLOAT_TYPE_P (type)
1983 && !HONOR_SIGN_DEPENDENT_ROUNDING (type)
1984 && !HONOR_SIGNED_ZEROS (type)))
1987 (negate (pointer_diff @0 @1))
1988 (if (TYPE_OVERFLOW_UNDEFINED (type))
1989 (pointer_diff @1 @0)))
1991 /* A - B -> A + (-B) if B is easily negatable. */
1993 (minus @0 negate_expr_p@1)
1994 (if (!FIXED_POINT_TYPE_P (type))
1995 (plus @0 (negate @1))))
1997 /* 1 - a is a ^ 1 if a had a bool range. */
1998 /* This is only enabled for gimple as sometimes
1999 cfun is not set for the function which contains
2000 the SSA_NAME (e.g. while IPA passes are happening,
2001 fold might be called). */
2003 (minus integer_onep@0 SSA_NAME@1)
2004 (if (INTEGRAL_TYPE_P (type)
2005 && ssa_name_has_boolean_range (@1))
2008 /* Other simplifications of negation (c.f. fold_negate_expr_1). */
2010 (negate (mult:c@0 @1 negate_expr_p@2))
2011 (if (! TYPE_UNSIGNED (type)
2012 && ! HONOR_SIGN_DEPENDENT_ROUNDING (type)
2014 (mult @1 (negate @2))))
2017 (negate (rdiv@0 @1 negate_expr_p@2))
2018 (if (! HONOR_SIGN_DEPENDENT_ROUNDING (type)
2020 (rdiv @1 (negate @2))))
2023 (negate (rdiv@0 negate_expr_p@1 @2))
2024 (if (! HONOR_SIGN_DEPENDENT_ROUNDING (type)
2026 (rdiv (negate @1) @2)))
2028 /* Fold -((int)x >> (prec - 1)) into (unsigned)x >> (prec - 1). */
2030 (negate (convert? (rshift @0 INTEGER_CST@1)))
2031 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
2032 && wi::to_wide (@1) == element_precision (type) - 1)
2033 (with { tree stype = TREE_TYPE (@0);
2034 tree ntype = TYPE_UNSIGNED (stype) ? signed_type_for (stype)
2035 : unsigned_type_for (stype); }
2036 (if (VECTOR_TYPE_P (type))
2037 (view_convert (rshift (view_convert:ntype @0) @1))
2038 (convert (rshift (convert:ntype @0) @1))))))
2040 /* Try to fold (type) X op CST -> (type) (X op ((type-x) CST))
2042 For bitwise binary operations apply operand conversions to the
2043 binary operation result instead of to the operands. This allows
2044 to combine successive conversions and bitwise binary operations.
2045 We combine the above two cases by using a conditional convert. */
2046 (for bitop (bit_and bit_ior bit_xor)
2048 (bitop (convert@2 @0) (convert?@3 @1))
2049 (if (((TREE_CODE (@1) == INTEGER_CST
2050 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
2051 && (int_fits_type_p (@1, TREE_TYPE (@0))
2052 || tree_nop_conversion_p (TREE_TYPE (@0), type)))
2053 || types_match (@0, @1))
2054 && !POINTER_TYPE_P (TREE_TYPE (@0))
2055 && !VECTOR_TYPE_P (TREE_TYPE (@0))
2056 && TREE_CODE (TREE_TYPE (@0)) != OFFSET_TYPE
2057 /* ??? This transform conflicts with fold-const.cc doing
2058 Convert (T)(x & c) into (T)x & (T)c, if c is an integer
2059 constants (if x has signed type, the sign bit cannot be set
2060 in c). This folds extension into the BIT_AND_EXPR.
2061 Restrict it to GIMPLE to avoid endless recursions. */
2062 && (bitop != BIT_AND_EXPR || GIMPLE)
2063 && (/* That's a good idea if the conversion widens the operand, thus
2064 after hoisting the conversion the operation will be narrower.
2065 It is also a good if the conversion is a nop as moves the
2066 conversion to one side; allowing for combining of the conversions. */
2067 TYPE_PRECISION (TREE_TYPE (@0)) < TYPE_PRECISION (type)
2068 /* The conversion check for being a nop can only be done at the gimple
2069 level as fold_binary has some re-association code which can conflict
2070 with this if there is a "constant" which is not a full INTEGER_CST. */
2071 || (GIMPLE && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (type))
2072 /* It's also a good idea if the conversion is to a non-integer
2074 || GET_MODE_CLASS (TYPE_MODE (type)) != MODE_INT
2075 /* Or if the precision of TO is not the same as the precision
2077 || !type_has_mode_precision_p (type)
2078 /* In GIMPLE, getting rid of 2 conversions for one new results
2081 && TREE_CODE (@1) != INTEGER_CST
2082 && tree_nop_conversion_p (type, TREE_TYPE (@0))
2084 && single_use (@3))))
2085 (convert (bitop @0 (convert @1)))))
2086 /* In GIMPLE, getting rid of 2 conversions for one new results
2089 (convert (bitop:cs@2 (nop_convert:s @0) @1))
2091 && TREE_CODE (@1) != INTEGER_CST
2092 && tree_nop_conversion_p (type, TREE_TYPE (@2))
2093 && types_match (type, @0)
2094 && !POINTER_TYPE_P (TREE_TYPE (@0))
2095 && TREE_CODE (TREE_TYPE (@0)) != OFFSET_TYPE)
2096 (bitop @0 (convert @1)))))
2098 (for bitop (bit_and bit_ior)
2099 rbitop (bit_ior bit_and)
2100 /* (x | y) & x -> x */
2101 /* (x & y) | x -> x */
2103 (bitop:c (rbitop:c @0 @1) @0)
2105 /* (~x | y) & x -> x & y */
2106 /* (~x & y) | x -> x | y */
2108 (bitop:c (rbitop:c @2 @1) @0)
2109 (with { bool wascmp; }
2110 (if (bitwise_inverted_equal_p (@0, @2, wascmp)
2111 && (!wascmp || element_precision (type) == 1))
2113 /* (x | y) & (x & z) -> (x & z) */
2114 /* (x & y) | (x | z) -> (x | z) */
2116 (bitop:c (rbitop:c @0 @1) (bitop:c@3 @0 @2))
2118 /* (x | c) & ~(y | c) -> x & ~(y | c) */
2119 /* (x & c) | ~(y & c) -> x | ~(y & c) */
2121 (bitop:c (rbitop:c @0 @1) (bit_not@3 (rbitop:c @1 @2)))
2123 /* x & ~(y | x) -> 0 */
2124 /* x | ~(y & x) -> -1 */
2126 (bitop:c @0 (bit_not (rbitop:c @0 @1)))
2127 (if (bitop == BIT_AND_EXPR)
2128 { build_zero_cst (type); }
2129 { build_minus_one_cst (type); })))
2131 /* ((x | y) & z) | x -> (z & y) | x
2132 ((x ^ y) & z) | x -> (z & y) | x */
2133 (for op (bit_ior bit_xor)
2135 (bit_ior:c (nop_convert1?:s
2136 (bit_and:cs (nop_convert2?:s (op:cs @0 @1)) @2)) @3)
2137 (if (bitwise_equal_p (@0, @3))
2138 (convert (bit_ior (bit_and @1 (convert @2)) (convert @0))))))
2140 /* (x | CST1) & CST2 -> (x & CST2) | (CST1 & CST2) */
2142 (bit_and (bit_ior @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
2143 (bit_ior (bit_and @0 @2) (bit_and! @1 @2)))
2145 /* Combine successive equal operations with constants. */
2146 (for bitop (bit_and bit_ior bit_xor)
2148 (bitop (bitop @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
2149 (if (!CONSTANT_CLASS_P (@0))
2150 /* This is the canonical form regardless of whether (bitop @1 @2) can be
2151 folded to a constant. */
2152 (bitop @0 (bitop! @1 @2))
2153 /* In this case we have three constants and (bitop @0 @1) doesn't fold
2154 to a constant. This can happen if @0 or @1 is a POLY_INT_CST and if
2155 the values involved are such that the operation can't be decided at
2156 compile time. Try folding one of @0 or @1 with @2 to see whether
2157 that combination can be decided at compile time.
2159 Keep the existing form if both folds fail, to avoid endless
2161 (with { tree cst1 = const_binop (bitop, type, @0, @2); }
2163 (bitop @1 { cst1; })
2164 (with { tree cst2 = const_binop (bitop, type, @1, @2); }
2166 (bitop @0 { cst2; }))))))))
2168 /* Try simple folding for X op !X, and X op X with the help
2169 of the truth_valued_p and logical_inverted_value predicates. */
2170 (match truth_valued_p
2172 (if (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1)))
2173 (for op (tcc_comparison truth_and truth_andif truth_or truth_orif truth_xor)
2174 (match truth_valued_p
2176 (match truth_valued_p
2179 (match (logical_inverted_value @0)
2181 (match (logical_inverted_value @0)
2182 (bit_not truth_valued_p@0))
2183 (match (logical_inverted_value @0)
2184 (eq @0 integer_zerop))
2185 (match (logical_inverted_value @0)
2186 (ne truth_valued_p@0 integer_truep))
2187 (match (logical_inverted_value @0)
2188 (bit_xor truth_valued_p@0 integer_truep))
2192 (bit_and:c @0 (logical_inverted_value @0))
2193 { build_zero_cst (type); })
2194 /* X | !X and X ^ !X -> 1, , if X is truth-valued. */
2195 (for op (bit_ior bit_xor)
2197 (op:c truth_valued_p@0 (logical_inverted_value @0))
2198 { constant_boolean_node (true, type); }))
2199 /* X ==/!= !X is false/true. */
2202 (op:c truth_valued_p@0 (logical_inverted_value @0))
2203 { constant_boolean_node (op == NE_EXPR ? true : false, type); }))
2207 (bit_not (bit_not @0))
2210 /* zero_one_valued_p will match when a value is known to be either
2211 0 or 1 including constants 0 or 1.
2212 Signed 1-bits includes -1 so they cannot match here. */
2213 (match zero_one_valued_p
2215 (if (INTEGRAL_TYPE_P (type)
2216 && (TYPE_UNSIGNED (type)
2217 || TYPE_PRECISION (type) > 1)
2218 && wi::leu_p (tree_nonzero_bits (@0), 1))))
2219 (match zero_one_valued_p
2221 (if (INTEGRAL_TYPE_P (type)
2222 && (TYPE_UNSIGNED (type)
2223 || TYPE_PRECISION (type) > 1))))
2225 /* (a&1) is always [0,1] too. This is useful again when
2226 the range is not known. */
2227 /* Note this can't be recursive due to VN handling of equivalents,
2228 VN and would cause an infinite recursion. */
2229 (match zero_one_valued_p
2230 (bit_and:c@0 @1 integer_onep)
2231 (if (INTEGRAL_TYPE_P (type))))
2233 /* A conversion from an zero_one_valued_p is still a [0,1].
2234 This is useful when the range of a variable is not known */
2235 /* Note this matches can't be recursive because of the way VN handles
2236 nop conversions being equivalent and then recursive between them. */
2237 (match zero_one_valued_p
2239 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2240 && (TYPE_UNSIGNED (TREE_TYPE (@1))
2241 || TYPE_PRECISION (TREE_TYPE (@1)) > 1)
2242 && wi::leu_p (tree_nonzero_bits (@1), 1))))
2244 /* Transform { 0 or 1 } * { 0 or 1 } into { 0 or 1 } & { 0 or 1 }. */
2246 (mult zero_one_valued_p@0 zero_one_valued_p@1)
2247 (if (INTEGRAL_TYPE_P (type))
2250 (for cmp (tcc_comparison)
2251 icmp (inverted_tcc_comparison)
2252 /* Fold (((a < b) & c) | ((a >= b) & d)) into (a < b ? c : d) & 1. */
2255 (bit_and:c (convert? (cmp@0 @01 @02)) @3)
2256 (bit_and:c (convert? (icmp@4 @01 @02)) @5))
2257 (if (INTEGRAL_TYPE_P (type)
2258 && invert_tree_comparison (cmp, HONOR_NANS (@01)) == icmp
2259 /* The scalar version has to be canonicalized after vectorization
2260 because it makes unconditional loads conditional ones, which
2261 means we lose vectorization because the loads may trap. */
2262 && canonicalize_math_after_vectorization_p ())
2263 (bit_and (cond @0 @3 @5) { build_one_cst (type); })))
2265 /* Fold ((-(a < b) & c) | (-(a >= b) & d)) into a < b ? c : d. This is
2266 canonicalized further and we recognize the conditional form:
2267 (a < b ? c : 0) | (a >= b ? d : 0) into a < b ? c : d. */
2270 (cond (cmp@0 @01 @02) @3 zerop)
2271 (cond (icmp@4 @01 @02) @5 zerop))
2272 (if (INTEGRAL_TYPE_P (type)
2273 && invert_tree_comparison (cmp, HONOR_NANS (@01)) == icmp
2274 /* The scalar version has to be canonicalized after vectorization
2275 because it makes unconditional loads conditional ones, which
2276 means we lose vectorization because the loads may trap. */
2277 && canonicalize_math_after_vectorization_p ())
2280 /* Vector Fold (((a < b) & c) | ((a >= b) & d)) into a < b ? c : d.
2281 and ((~(a < b) & c) | (~(a >= b) & d)) into a < b ? c : d. */
2284 (bit_and:c (vec_cond:s (cmp@0 @6 @7) @4 @5) @2)
2285 (bit_and:c (vec_cond:s (icmp@1 @6 @7) @4 @5) @3))
2286 (if (integer_zerop (@5)
2287 && invert_tree_comparison (cmp, HONOR_NANS (@6)) == icmp)
2289 (if (integer_onep (@4))
2290 (bit_and (vec_cond @0 @2 @3) @4))
2291 (if (integer_minus_onep (@4))
2292 (vec_cond @0 @2 @3)))
2293 (if (integer_zerop (@4)
2294 && invert_tree_comparison (cmp, HONOR_NANS (@6)) == icmp)
2296 (if (integer_onep (@5))
2297 (bit_and (vec_cond @0 @3 @2) @5))
2298 (if (integer_minus_onep (@5))
2299 (vec_cond @0 @3 @2))))))
2301 /* Scalar Vectorized Fold ((-(a < b) & c) | (-(a >= b) & d))
2302 into a < b ? d : c. */
2305 (vec_cond:s (cmp@0 @4 @5) @2 integer_zerop)
2306 (vec_cond:s (icmp@1 @4 @5) @3 integer_zerop))
2307 (if (invert_tree_comparison (cmp, HONOR_NANS (@4)) == icmp)
2308 (vec_cond @0 @2 @3))))
2310 /* Transform X & -Y into X * Y when Y is { 0 or 1 }. */
2312 (bit_and:c (convert? (negate zero_one_valued_p@0)) @1)
2313 (if (INTEGRAL_TYPE_P (type)
2314 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
2315 && TREE_CODE (TREE_TYPE (@0)) != BOOLEAN_TYPE
2316 /* Sign extending of the neg or a truncation of the neg
2318 && (!TYPE_UNSIGNED (TREE_TYPE (@0))
2319 || TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))))
2320 (mult (convert @0) @1)))
2322 /* Narrow integer multiplication by a zero_one_valued_p operand.
2323 Multiplication by [0,1] is guaranteed not to overflow. */
2325 (convert (mult@0 zero_one_valued_p@1 INTEGER_CST@2))
2326 (if (INTEGRAL_TYPE_P (type)
2327 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
2328 && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (@0)))
2329 (mult (convert @1) (convert @2))))
2331 /* (X << C) != 0 can be simplified to X, when C is zero_one_valued_p.
2332 Check that the shift is well-defined (C is less than TYPE_PRECISION)
2333 as some targets (such as x86's SSE) may return zero for larger C. */
2335 (ne (lshift zero_one_valued_p@0 INTEGER_CST@1) integer_zerop@2)
2336 (if (tree_fits_shwi_p (@1)
2337 && tree_to_shwi (@1) > 0
2338 && tree_to_shwi (@1) < TYPE_PRECISION (TREE_TYPE (@0)))
2341 /* (X << C) == 0 can be simplified to X == 0, when C is zero_one_valued_p.
2342 Check that the shift is well-defined (C is less than TYPE_PRECISION)
2343 as some targets (such as x86's SSE) may return zero for larger C. */
2345 (eq (lshift zero_one_valued_p@0 INTEGER_CST@1) integer_zerop@2)
2346 (if (tree_fits_shwi_p (@1)
2347 && tree_to_shwi (@1) > 0
2348 && tree_to_shwi (@1) < TYPE_PRECISION (TREE_TYPE (@0)))
2351 /* Convert ~ (-A) to A - 1. */
2353 (bit_not (convert? (negate @0)))
2354 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
2355 || !TYPE_UNSIGNED (TREE_TYPE (@0)))
2356 (convert (minus @0 { build_each_one_cst (TREE_TYPE (@0)); }))))
2358 /* Convert - (~A) to A + 1. */
2360 (negate (nop_convert? (bit_not @0)))
2361 (plus (view_convert @0) { build_each_one_cst (type); }))
2363 /* (a & b) ^ (a == b) -> !(a | b) */
2364 /* (a & b) == (a ^ b) -> !(a | b) */
2365 (for first_op (bit_xor eq)
2366 second_op (eq bit_xor)
2368 (first_op:c (bit_and:c truth_valued_p@0 truth_valued_p@1) (second_op:c @0 @1))
2369 (bit_not (bit_ior @0 @1))))
2371 /* Convert ~ (A - 1) or ~ (A + -1) to -A. */
2373 (bit_not (convert? (minus @0 integer_each_onep)))
2374 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
2375 || !TYPE_UNSIGNED (TREE_TYPE (@0)))
2376 (convert (negate @0))))
2378 (bit_not (convert? (plus @0 integer_all_onesp)))
2379 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
2380 || !TYPE_UNSIGNED (TREE_TYPE (@0)))
2381 (convert (negate @0))))
2383 /* Part of convert ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify. */
2385 (bit_not (convert? (bit_xor @0 INTEGER_CST@1)))
2386 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2387 (convert (bit_xor @0 (bit_not @1)))))
2389 (bit_not (convert? (bit_xor:c (bit_not @0) @1)))
2390 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2391 (convert (bit_xor @0 @1))))
2393 /* Otherwise prefer ~(X ^ Y) to ~X ^ Y as more canonical. */
2395 (bit_xor:c (nop_convert?:s (bit_not:s @0)) @1)
2396 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2397 (bit_not (bit_xor (view_convert @0) @1))))
2399 /* ~(a ^ b) is a == b for truth valued a and b. */
2401 (bit_not (bit_xor:s truth_valued_p@0 truth_valued_p@1))
2402 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2403 && TYPE_PRECISION (TREE_TYPE (@0)) == 1)
2404 (convert (eq @0 @1))))
2406 /* (~a) == b is a ^ b for truth valued a and b. */
2408 (eq:c (bit_not:s truth_valued_p@0) truth_valued_p@1)
2409 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2410 && TYPE_PRECISION (TREE_TYPE (@0)) == 1)
2411 (convert (bit_xor @0 @1))))
2413 /* (x & ~m) | (y & m) -> ((x ^ y) & m) ^ x */
2415 (bit_ior:c (bit_and:cs @0 (bit_not @2)) (bit_and:cs @1 @2))
2416 (bit_xor (bit_and (bit_xor @0 @1) @2) @0))
2418 /* Fold A - (A & B) into ~B & A. */
2420 (minus (convert1? @0) (convert2?:s (bit_and:cs @@0 @1)))
2421 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
2422 && tree_nop_conversion_p (type, TREE_TYPE (@1)))
2423 (convert (bit_and (bit_not @1) @0))))
2425 /* (m1 CMP m2) * d -> (m1 CMP m2) ? d : 0 */
2426 (if (!canonicalize_math_p ())
2427 (for cmp (tcc_comparison)
2429 (mult:c (convert (cmp@0 @1 @2)) @3)
2430 (if (INTEGRAL_TYPE_P (type)
2431 && INTEGRAL_TYPE_P (TREE_TYPE (@0)))
2432 (cond @0 @3 { build_zero_cst (type); })))
2433 /* (-(m1 CMP m2)) & d -> (m1 CMP m2) ? d : 0 */
2435 (bit_and:c (negate (convert (cmp@0 @1 @2))) @3)
2436 (if (INTEGRAL_TYPE_P (type)
2437 && INTEGRAL_TYPE_P (TREE_TYPE (@0)))
2438 (cond @0 @3 { build_zero_cst (type); })))
2442 /* For integral types with undefined overflow and C != 0 fold
2443 x * C EQ/NE y * C into x EQ/NE y. */
2446 (cmp (mult:c @0 @1) (mult:c @2 @1))
2447 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2448 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2449 && tree_expr_nonzero_p (@1))
2452 /* For integral types with wrapping overflow and C odd fold
2453 x * C EQ/NE y * C into x EQ/NE y. */
2456 (cmp (mult @0 INTEGER_CST@1) (mult @2 @1))
2457 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2458 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))
2459 && (TREE_INT_CST_LOW (@1) & 1) != 0)
2462 /* For integral types with undefined overflow and C != 0 fold
2463 x * C RELOP y * C into:
2465 x RELOP y for nonnegative C
2466 y RELOP x for negative C */
2467 (for cmp (lt gt le ge)
2469 (cmp (mult:c @0 @1) (mult:c @2 @1))
2470 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2471 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
2472 (if (tree_expr_nonnegative_p (@1) && tree_expr_nonzero_p (@1))
2474 (if (TREE_CODE (@1) == INTEGER_CST
2475 && wi::neg_p (wi::to_wide (@1), TYPE_SIGN (TREE_TYPE (@1))))
2478 /* (X - 1U) <= INT_MAX-1U into (int) X > 0. */
2482 (cmp (plus @0 integer_minus_onep@1) INTEGER_CST@2)
2483 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2484 && TYPE_UNSIGNED (TREE_TYPE (@0))
2485 && TYPE_PRECISION (TREE_TYPE (@0)) > 1
2486 && (wi::to_wide (@2)
2487 == wi::max_value (TYPE_PRECISION (TREE_TYPE (@0)), SIGNED) - 1))
2488 (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
2489 (icmp (convert:stype @0) { build_int_cst (stype, 0); })))))
2491 /* X / 4 < Y / 4 iff X < Y when the division is known to be exact. */
2492 (for cmp (simple_comparison)
2494 (cmp (convert?@3 (exact_div @0 INTEGER_CST@2)) (convert? (exact_div @1 @2)))
2495 (if (element_precision (@3) >= element_precision (@0)
2496 && types_match (@0, @1))
2497 (if (wi::lt_p (wi::to_wide (@2), 0, TYPE_SIGN (TREE_TYPE (@2))))
2498 (if (!TYPE_UNSIGNED (TREE_TYPE (@3)))
2500 (if (tree_expr_nonzero_p (@0) && tree_expr_nonzero_p (@1))
2503 tree utype = unsigned_type_for (TREE_TYPE (@0));
2505 (cmp (convert:utype @1) (convert:utype @0)))))
2506 (if (wi::gt_p (wi::to_wide (@2), 1, TYPE_SIGN (TREE_TYPE (@2))))
2507 (if (TYPE_UNSIGNED (TREE_TYPE (@0)) || !TYPE_UNSIGNED (TREE_TYPE (@3)))
2511 tree utype = unsigned_type_for (TREE_TYPE (@0));
2513 (cmp (convert:utype @0) (convert:utype @1)))))))))
2515 /* X / C1 op C2 into a simple range test. */
2516 (for cmp (simple_comparison)
2518 (cmp (trunc_div:s @0 INTEGER_CST@1) INTEGER_CST@2)
2519 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2520 && integer_nonzerop (@1)
2521 && !TREE_OVERFLOW (@1)
2522 && !TREE_OVERFLOW (@2))
2523 (with { tree lo, hi; bool neg_overflow;
2524 enum tree_code code = fold_div_compare (cmp, @1, @2, &lo, &hi,
2527 (if (code == LT_EXPR || code == GE_EXPR)
2528 (if (TREE_OVERFLOW (lo))
2529 { build_int_cst (type, (code == LT_EXPR) ^ neg_overflow); }
2530 (if (code == LT_EXPR)
2533 (if (code == LE_EXPR || code == GT_EXPR)
2534 (if (TREE_OVERFLOW (hi))
2535 { build_int_cst (type, (code == LE_EXPR) ^ neg_overflow); }
2536 (if (code == LE_EXPR)
2540 { build_int_cst (type, code == NE_EXPR); })
2541 (if (code == EQ_EXPR && !hi)
2543 (if (code == EQ_EXPR && !lo)
2545 (if (code == NE_EXPR && !hi)
2547 (if (code == NE_EXPR && !lo)
2550 { build_range_check (UNKNOWN_LOCATION, type, @0, code == EQ_EXPR,
2554 tree etype = range_check_type (TREE_TYPE (@0));
2557 hi = fold_convert (etype, hi);
2558 lo = fold_convert (etype, lo);
2559 hi = const_binop (MINUS_EXPR, etype, hi, lo);
2562 (if (etype && hi && !TREE_OVERFLOW (hi))
2563 (if (code == EQ_EXPR)
2564 (le (minus (convert:etype @0) { lo; }) { hi; })
2565 (gt (minus (convert:etype @0) { lo; }) { hi; })))))))))
2567 /* X + Z < Y + Z is the same as X < Y when there is no overflow. */
2568 (for op (lt le ge gt)
2570 (op (plus:c @0 @2) (plus:c @1 @2))
2571 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2572 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
2575 /* As a special case, X + C < Y + C is the same as (signed) X < (signed) Y
2576 when C is an unsigned integer constant with only the MSB set, and X and
2577 Y have types of equal or lower integer conversion rank than C's. */
2578 (for op (lt le ge gt)
2580 (op (plus @1 INTEGER_CST@0) (plus @2 @0))
2581 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2582 && TYPE_UNSIGNED (TREE_TYPE (@0))
2583 && wi::only_sign_bit_p (wi::to_wide (@0)))
2584 (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
2585 (op (convert:stype @1) (convert:stype @2))))))
2587 /* For equality and subtraction, this is also true with wrapping overflow. */
2588 (for op (eq ne minus)
2590 (op (plus:c @0 @2) (plus:c @1 @2))
2591 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2592 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2593 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
2596 /* X - Z < Y - Z is the same as X < Y when there is no overflow. */
2597 (for op (lt le ge gt)
2599 (op (minus @0 @2) (minus @1 @2))
2600 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2601 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
2603 /* For equality and subtraction, this is also true with wrapping overflow. */
2604 (for op (eq ne minus)
2606 (op (minus @0 @2) (minus @1 @2))
2607 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2608 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2609 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
2611 /* And for pointers... */
2612 (for op (simple_comparison)
2614 (op (pointer_diff@3 @0 @2) (pointer_diff @1 @2))
2615 (if (!TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
2618 (minus (pointer_diff@3 @0 @2) (pointer_diff @1 @2))
2619 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@3))
2620 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
2621 (pointer_diff @0 @1)))
2623 /* Z - X < Z - Y is the same as Y < X when there is no overflow. */
2624 (for op (lt le ge gt)
2626 (op (minus @2 @0) (minus @2 @1))
2627 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2628 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
2630 /* For equality and subtraction, this is also true with wrapping overflow. */
2631 (for op (eq ne minus)
2633 (op (minus @2 @0) (minus @2 @1))
2634 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2635 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2636 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
2638 /* And for pointers... */
2639 (for op (simple_comparison)
2641 (op (pointer_diff@3 @2 @0) (pointer_diff @2 @1))
2642 (if (!TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
2645 (minus (pointer_diff@3 @2 @0) (pointer_diff @2 @1))
2646 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@3))
2647 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
2648 (pointer_diff @1 @0)))
2650 /* X + Y < Y is the same as X < 0 when there is no overflow. */
2651 (for op (lt le gt ge)
2653 (op:c (plus:c@2 @0 @1) @1)
2654 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2655 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2656 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0))
2657 && (CONSTANT_CLASS_P (@0) || single_use (@2)))
2658 (op @0 { build_zero_cst (TREE_TYPE (@0)); }))))
2659 /* For equality, this is also true with wrapping overflow. */
2662 (op:c (nop_convert?@3 (plus:c@2 @0 (convert1? @1))) (convert2? @1))
2663 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2664 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2665 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
2666 && (CONSTANT_CLASS_P (@0) || (single_use (@2) && single_use (@3)))
2667 && tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@2))
2668 && tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@1)))
2669 (op @0 { build_zero_cst (TREE_TYPE (@0)); })))
2671 (op:c (nop_convert?@3 (pointer_plus@2 (convert1? @0) @1)) (convert2? @0))
2672 (if (tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@0))
2673 && tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0))
2674 && (CONSTANT_CLASS_P (@1) || (single_use (@2) && single_use (@3))))
2675 (op @1 { build_zero_cst (TREE_TYPE (@1)); }))))
2677 /* (&a + b) !=/== (&a[1] + c) -> (&a[0] - &a[1]) + b !=/== c */
2680 (neeq:c ADDR_EXPR@0 (pointer_plus @2 @3))
2681 (with { poly_int64 diff; tree inner_type = TREE_TYPE (@3);}
2682 (if (ptr_difference_const (@0, @2, &diff))
2683 (neeq { build_int_cst_type (inner_type, diff); } @3))))
2685 (neeq (pointer_plus ADDR_EXPR@0 @1) (pointer_plus ADDR_EXPR@2 @3))
2686 (with { poly_int64 diff; tree inner_type = TREE_TYPE (@1);}
2687 (if (ptr_difference_const (@0, @2, &diff))
2688 (neeq (plus { build_int_cst_type (inner_type, diff); } @1) @3)))))
2690 /* X - Y < X is the same as Y > 0 when there is no overflow.
2691 For equality, this is also true with wrapping overflow. */
2692 (for op (simple_comparison)
2694 (op:c @0 (minus@2 @0 @1))
2695 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2696 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2697 || ((op == EQ_EXPR || op == NE_EXPR)
2698 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
2699 && (CONSTANT_CLASS_P (@1) || single_use (@2)))
2700 (op @1 { build_zero_cst (TREE_TYPE (@1)); }))))
2703 (X / Y) == 0 -> X < Y if X, Y are unsigned.
2704 (X / Y) != 0 -> X >= Y, if X, Y are unsigned. */
2708 (cmp (trunc_div @0 @1) integer_zerop)
2709 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
2710 /* Complex ==/!= is allowed, but not </>=. */
2711 && TREE_CODE (TREE_TYPE (@0)) != COMPLEX_TYPE
2712 && (VECTOR_TYPE_P (type) || !VECTOR_TYPE_P (TREE_TYPE (@0))))
2715 /* X == C - X can never be true if C is odd. */
2718 (cmp:c (convert? @0) (convert1? (minus INTEGER_CST@1 (convert2? @0))))
2719 (if (TREE_INT_CST_LOW (@1) & 1)
2720 { constant_boolean_node (cmp == NE_EXPR, type); })))
2725 U needs to be non-negative.
2729 U and N needs to be non-negative
2733 U needs to be non-negative and N needs to be a negative constant.
2735 (for cmp (lt ge le gt )
2736 bitop (bit_ior bit_ior bit_and bit_and)
2738 (cmp:c (bitop:c tree_expr_nonnegative_p@0 @1) @0)
2739 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)))
2740 (if (bitop == BIT_AND_EXPR || tree_expr_nonnegative_p (@1))
2741 { constant_boolean_node (cmp == GE_EXPR || cmp == LE_EXPR, type); }
2742 /* The sign is opposite now so the comparison is swapped around. */
2743 (if (TREE_CODE (@1) == INTEGER_CST && wi::neg_p (wi::to_wide (@1)))
2744 { constant_boolean_node (cmp == LT_EXPR, type); })))))
2746 /* Arguments on which one can call get_nonzero_bits to get the bits
2748 (match with_possible_nonzero_bits
2750 (match with_possible_nonzero_bits
2752 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))))
2753 /* Slightly extended version, do not make it recursive to keep it cheap. */
2754 (match (with_possible_nonzero_bits2 @0)
2755 with_possible_nonzero_bits@0)
2756 (match (with_possible_nonzero_bits2 @0)
2757 (bit_and:c with_possible_nonzero_bits@0 @2))
2759 /* Same for bits that are known to be set, but we do not have
2760 an equivalent to get_nonzero_bits yet. */
2761 (match (with_certain_nonzero_bits2 @0)
2763 (match (with_certain_nonzero_bits2 @0)
2764 (bit_ior @1 INTEGER_CST@0))
2766 /* X == C (or X & Z == Y | C) is impossible if ~nonzero(X) & C != 0. */
2769 (cmp:c (with_possible_nonzero_bits2 @0) (with_certain_nonzero_bits2 @1))
2770 (if (wi::bit_and_not (wi::to_wide (@1), get_nonzero_bits (@0)) != 0)
2771 { constant_boolean_node (cmp == NE_EXPR, type); })))
2773 /* ((X inner_op C0) outer_op C1)
2774 With X being a tree where value_range has reasoned certain bits to always be
2775 zero throughout its computed value range,
2776 inner_op = {|,^}, outer_op = {|,^} and inner_op != outer_op
2777 where zero_mask has 1's for all bits that are sure to be 0 in
2779 if (inner_op == '^') C0 &= ~C1;
2780 if ((C0 & ~zero_mask) == 0) then emit (X outer_op (C0 outer_op C1)
2781 if ((C1 & ~zero_mask) == 0) then emit (X inner_op (C0 outer_op C1)
2783 (for inner_op (bit_ior bit_xor)
2784 outer_op (bit_xor bit_ior)
2787 (inner_op:s @2 INTEGER_CST@0) INTEGER_CST@1)
2791 wide_int zero_mask_not;
2795 if (TREE_CODE (@2) == SSA_NAME)
2796 zero_mask_not = get_nonzero_bits (@2);
2800 if (inner_op == BIT_XOR_EXPR)
2802 C0 = wi::bit_and_not (wi::to_wide (@0), wi::to_wide (@1));
2803 cst_emit = C0 | wi::to_wide (@1);
2807 C0 = wi::to_wide (@0);
2808 cst_emit = C0 ^ wi::to_wide (@1);
2811 (if (!fail && (C0 & zero_mask_not) == 0)
2812 (outer_op @2 { wide_int_to_tree (type, cst_emit); })
2813 (if (!fail && (wi::to_wide (@1) & zero_mask_not) == 0)
2814 (inner_op @2 { wide_int_to_tree (type, cst_emit); }))))))
2816 /* Associate (p +p off1) +p off2 as (p +p (off1 + off2)). */
2818 (pointer_plus (pointer_plus:s @0 @1) @3)
2819 (pointer_plus @0 (plus @1 @3)))
2822 (pointer_plus (convert:s (pointer_plus:s @0 @1)) @3)
2823 (convert:type (pointer_plus @0 (plus @1 @3))))
2830 tem4 = (unsigned long) tem3;
2835 (pointer_plus @0 (convert?@2 (minus@3 (convert @1) (convert @0))))
2836 /* Conditionally look through a sign-changing conversion. */
2837 (if (TYPE_PRECISION (TREE_TYPE (@2)) == TYPE_PRECISION (TREE_TYPE (@3))
2838 && ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@1)))
2839 || (GENERIC && type == TREE_TYPE (@1))))
2842 (pointer_plus @0 (convert?@2 (pointer_diff@3 @1 @@0)))
2843 (if (TYPE_PRECISION (TREE_TYPE (@2)) >= TYPE_PRECISION (TREE_TYPE (@3)))
2847 tem = (sizetype) ptr;
2851 and produce the simpler and easier to analyze with respect to alignment
2852 ... = ptr & ~algn; */
2854 (pointer_plus @0 (negate (bit_and (convert @0) INTEGER_CST@1)))
2855 (with { tree algn = wide_int_to_tree (TREE_TYPE (@0), ~wi::to_wide (@1)); }
2856 (bit_and @0 { algn; })))
2858 /* Try folding difference of addresses. */
2860 (minus (convert ADDR_EXPR@0) (convert (pointer_plus @1 @2)))
2861 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2862 (with { poly_int64 diff; }
2863 (if (ptr_difference_const (@0, @1, &diff))
2864 (minus { build_int_cst_type (type, diff); } (convert @2))))))
2866 (minus (convert (pointer_plus @0 @2)) (convert ADDR_EXPR@1))
2867 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2868 (with { poly_int64 diff; }
2869 (if (ptr_difference_const (@0, @1, &diff))
2870 (plus (convert @2) { build_int_cst_type (type, diff); })))))
2872 (minus (convert ADDR_EXPR@0) (convert @1))
2873 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2874 (with { poly_int64 diff; }
2875 (if (ptr_difference_const (@0, @1, &diff))
2876 { build_int_cst_type (type, diff); }))))
2878 (minus (convert @0) (convert ADDR_EXPR@1))
2879 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2880 (with { poly_int64 diff; }
2881 (if (ptr_difference_const (@0, @1, &diff))
2882 { build_int_cst_type (type, diff); }))))
2884 (pointer_diff (convert?@2 ADDR_EXPR@0) (convert1?@3 @1))
2885 (if (tree_nop_conversion_p (TREE_TYPE(@2), TREE_TYPE (@0))
2886 && tree_nop_conversion_p (TREE_TYPE(@3), TREE_TYPE (@1)))
2887 (with { poly_int64 diff; }
2888 (if (ptr_difference_const (@0, @1, &diff))
2889 { build_int_cst_type (type, diff); }))))
2891 (pointer_diff (convert?@2 @0) (convert1?@3 ADDR_EXPR@1))
2892 (if (tree_nop_conversion_p (TREE_TYPE(@2), TREE_TYPE (@0))
2893 && tree_nop_conversion_p (TREE_TYPE(@3), TREE_TYPE (@1)))
2894 (with { poly_int64 diff; }
2895 (if (ptr_difference_const (@0, @1, &diff))
2896 { build_int_cst_type (type, diff); }))))
2898 /* (&a+b) - (&a[1] + c) -> sizeof(a[0]) + (b - c) */
2900 (pointer_diff (pointer_plus ADDR_EXPR@0 @1) (pointer_plus ADDR_EXPR@2 @3))
2901 (with { poly_int64 diff; }
2902 (if (ptr_difference_const (@0, @2, &diff))
2903 (plus { build_int_cst_type (type, diff); } (convert (minus @1 @3))))))
2904 /* (p + b) - &p->d -> offsetof (*p, d) + b */
2906 (pointer_diff (pointer_plus @0 @1) ADDR_EXPR@2)
2907 (with { poly_int64 diff; }
2908 (if (ptr_difference_const (@0, @2, &diff))
2909 (plus { build_int_cst_type (type, diff); } (convert @1)))))
2911 (pointer_diff ADDR_EXPR@0 (pointer_plus @1 @2))
2912 (with { poly_int64 diff; }
2913 (if (ptr_difference_const (@0, @1, &diff))
2914 (minus { build_int_cst_type (type, diff); } (convert @2)))))
2916 /* Canonicalize (T *)(ptr - ptr-cst) to &MEM[ptr + -ptr-cst]. */
2918 (convert (pointer_diff @0 INTEGER_CST@1))
2919 (if (POINTER_TYPE_P (type))
2920 { build_fold_addr_expr_with_type
2921 (build2 (MEM_REF, char_type_node, @0,
2922 wide_int_to_tree (ptr_type_node, wi::neg (wi::to_wide (@1)))),
2925 /* If arg0 is derived from the address of an object or function, we may
2926 be able to fold this expression using the object or function's
2929 (bit_and (convert? @0) INTEGER_CST@1)
2930 (if (POINTER_TYPE_P (TREE_TYPE (@0))
2931 && tree_nop_conversion_p (type, TREE_TYPE (@0)))
2935 unsigned HOST_WIDE_INT bitpos;
2936 get_pointer_alignment_1 (@0, &align, &bitpos);
2938 (if (wi::ltu_p (wi::to_wide (@1), align / BITS_PER_UNIT))
2939 { wide_int_to_tree (type, (wi::to_wide (@1)
2940 & (bitpos / BITS_PER_UNIT))); }))))
2943 uniform_integer_cst_p
2945 tree int_cst = uniform_integer_cst_p (t);
2946 tree inner_type = TREE_TYPE (int_cst);
2948 (if ((INTEGRAL_TYPE_P (inner_type)
2949 || POINTER_TYPE_P (inner_type))
2950 && wi::eq_p (wi::to_wide (int_cst), wi::min_value (inner_type))))))
2953 uniform_integer_cst_p
2955 tree int_cst = uniform_integer_cst_p (t);
2956 tree itype = TREE_TYPE (int_cst);
2958 (if ((INTEGRAL_TYPE_P (itype)
2959 || POINTER_TYPE_P (itype))
2960 && wi::eq_p (wi::to_wide (int_cst), wi::max_value (itype))))))
2962 /* x > y && x != XXX_MIN --> x > y
2963 x > y && x == XXX_MIN --> false . */
2966 (bit_and:c (gt:c@2 @0 @1) (eqne @0 min_value))
2968 (if (eqne == EQ_EXPR)
2969 { constant_boolean_node (false, type); })
2970 (if (eqne == NE_EXPR)
2974 /* x < y && x != XXX_MAX --> x < y
2975 x < y && x == XXX_MAX --> false. */
2978 (bit_and:c (lt:c@2 @0 @1) (eqne @0 max_value))
2980 (if (eqne == EQ_EXPR)
2981 { constant_boolean_node (false, type); })
2982 (if (eqne == NE_EXPR)
2986 /* x <= y && x == XXX_MIN --> x == XXX_MIN. */
2988 (bit_and:c (le:c @0 @1) (eq@2 @0 min_value))
2991 /* x >= y && x == XXX_MAX --> x == XXX_MAX. */
2993 (bit_and:c (ge:c @0 @1) (eq@2 @0 max_value))
2996 /* x > y || x != XXX_MIN --> x != XXX_MIN. */
2998 (bit_ior:c (gt:c @0 @1) (ne@2 @0 min_value))
3001 /* x <= y || x != XXX_MIN --> true. */
3003 (bit_ior:c (le:c @0 @1) (ne @0 min_value))
3004 { constant_boolean_node (true, type); })
3006 /* x <= y || x == XXX_MIN --> x <= y. */
3008 (bit_ior:c (le:c@2 @0 @1) (eq @0 min_value))
3011 /* x < y || x != XXX_MAX --> x != XXX_MAX. */
3013 (bit_ior:c (lt:c @0 @1) (ne@2 @0 max_value))
3016 /* x >= y || x != XXX_MAX --> true
3017 x >= y || x == XXX_MAX --> x >= y. */
3020 (bit_ior:c (ge:c@2 @0 @1) (eqne @0 max_value))
3022 (if (eqne == EQ_EXPR)
3024 (if (eqne == NE_EXPR)
3025 { constant_boolean_node (true, type); }))))
3027 /* y == XXX_MIN || x < y --> x <= y - 1 */
3029 (bit_ior:c (eq:s @1 min_value) (lt:cs @0 @1))
3030 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
3031 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
3032 (le @0 (minus @1 { build_int_cst (TREE_TYPE (@1), 1); }))))
3034 /* y != XXX_MIN && x >= y --> x > y - 1 */
3036 (bit_and:c (ne:s @1 min_value) (ge:cs @0 @1))
3037 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
3038 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
3039 (gt @0 (minus @1 { build_int_cst (TREE_TYPE (@1), 1); }))))
3041 /* Convert (X == CST1) && ((other)X OP2 CST2) to a known value
3042 based on CST1 OP2 CST2. Similarly for (X != CST1). */
3043 /* Convert (X == Y) && (X OP2 Y) to a known value if X is an integral type.
3044 Similarly for (X != Y). */
3047 (for code2 (eq ne lt gt le ge)
3049 (bit_and:c (code1:c@3 @0 @1) (code2:c@4 (convert?@c0 @0) @2))
3050 (if ((TREE_CODE (@1) == INTEGER_CST
3051 && TREE_CODE (@2) == INTEGER_CST)
3052 || ((INTEGRAL_TYPE_P (TREE_TYPE (@1))
3053 || POINTER_TYPE_P (TREE_TYPE (@1)))
3054 && bitwise_equal_p (@1, @2)))
3057 bool one_before = false;
3058 bool one_after = false;
3060 bool allbits = true;
3061 if (TREE_CODE (@1) == INTEGER_CST
3062 && TREE_CODE (@2) == INTEGER_CST)
3064 allbits = TYPE_PRECISION (TREE_TYPE (@1)) <= TYPE_PRECISION (TREE_TYPE (@2));
3065 auto t1 = wi::to_wide (fold_convert (TREE_TYPE (@2), @1));
3066 auto t2 = wi::to_wide (@2);
3067 cmp = wi::cmp (t1, t2, TYPE_SIGN (TREE_TYPE (@2)));
3078 case EQ_EXPR: val = (cmp == 0); break;
3079 case NE_EXPR: val = (cmp != 0); break;
3080 case LT_EXPR: val = (cmp < 0); break;
3081 case GT_EXPR: val = (cmp > 0); break;
3082 case LE_EXPR: val = (cmp <= 0); break;
3083 case GE_EXPR: val = (cmp >= 0); break;
3084 default: gcc_unreachable ();
3088 (if (code1 == EQ_EXPR && val) @3)
3089 (if (code1 == EQ_EXPR && !val) { constant_boolean_node (false, type); })
3090 (if (code1 == NE_EXPR && !val && allbits) @4)
3091 (if (code1 == NE_EXPR
3095 (gt @c0 (convert @1)))
3096 (if (code1 == NE_EXPR
3100 (lt @c0 (convert @1)))
3101 /* (a != (b+1)) & (a > b) -> a > (b+1) */
3102 (if (code1 == NE_EXPR
3106 (gt @c0 (convert @1)))
3107 /* (a != (b-1)) & (a < b) -> a < (b-1) */
3108 (if (code1 == NE_EXPR
3112 (lt @c0 (convert @1)))
3120 /* Convert (X OP1 CST1) && (X OP2 CST2).
3121 Convert (X OP1 Y) && (X OP2 Y). */
3123 (for code1 (lt le gt ge)
3124 (for code2 (lt le gt ge)
3126 (bit_and (code1:c@3 @0 @1) (code2:c@4 @0 @2))
3127 (if ((TREE_CODE (@1) == INTEGER_CST
3128 && TREE_CODE (@2) == INTEGER_CST)
3129 || ((INTEGRAL_TYPE_P (TREE_TYPE (@1))
3130 || POINTER_TYPE_P (TREE_TYPE (@1)))
3131 && operand_equal_p (@1, @2)))
3135 if (TREE_CODE (@1) == INTEGER_CST
3136 && TREE_CODE (@2) == INTEGER_CST)
3137 cmp = tree_int_cst_compare (@1, @2);
3140 /* Choose the more restrictive of two < or <= comparisons. */
3141 (if ((code1 == LT_EXPR || code1 == LE_EXPR)
3142 && (code2 == LT_EXPR || code2 == LE_EXPR))
3143 (if ((cmp < 0) || (cmp == 0 && code1 == LT_EXPR))
3146 /* Likewise chose the more restrictive of two > or >= comparisons. */
3147 (if ((code1 == GT_EXPR || code1 == GE_EXPR)
3148 && (code2 == GT_EXPR || code2 == GE_EXPR))
3149 (if ((cmp > 0) || (cmp == 0 && code1 == GT_EXPR))
3152 /* Check for singleton ranges. */
3154 && ((code1 == LE_EXPR && code2 == GE_EXPR)
3155 || (code1 == GE_EXPR && code2 == LE_EXPR)))
3157 /* Check for disjoint ranges. */
3159 && (code1 == LT_EXPR || code1 == LE_EXPR)
3160 && (code2 == GT_EXPR || code2 == GE_EXPR))
3161 { constant_boolean_node (false, type); })
3163 && (code1 == GT_EXPR || code1 == GE_EXPR)
3164 && (code2 == LT_EXPR || code2 == LE_EXPR))
3165 { constant_boolean_node (false, type); })
3168 /* Convert (X == CST1) || (X OP2 CST2) to a known value
3169 based on CST1 OP2 CST2. Similarly for (X != CST1). */
3170 /* Convert (X == Y) || (X OP2 Y) to a known value if X is an integral type.
3171 Similarly for (X != Y). */
3174 (for code2 (eq ne lt gt le ge)
3176 (bit_ior:c (code1:c@3 @0 @1) (code2:c@4 (convert?@c0 @0) @2))
3177 (if ((TREE_CODE (@1) == INTEGER_CST
3178 && TREE_CODE (@2) == INTEGER_CST)
3179 || ((INTEGRAL_TYPE_P (TREE_TYPE (@1))
3180 || POINTER_TYPE_P (TREE_TYPE (@1)))
3181 && bitwise_equal_p (@1, @2)))
3184 bool one_before = false;
3185 bool one_after = false;
3187 bool allbits = true;
3188 if (TREE_CODE (@1) == INTEGER_CST
3189 && TREE_CODE (@2) == INTEGER_CST)
3191 allbits = TYPE_PRECISION (TREE_TYPE (@1)) <= TYPE_PRECISION (TREE_TYPE (@2));
3192 auto t1 = wi::to_wide (fold_convert (TREE_TYPE (@2), @1));
3193 auto t2 = wi::to_wide (@2);
3194 cmp = wi::cmp (t1, t2, TYPE_SIGN (TREE_TYPE (@2)));
3205 case EQ_EXPR: val = (cmp == 0); break;
3206 case NE_EXPR: val = (cmp != 0); break;
3207 case LT_EXPR: val = (cmp < 0); break;
3208 case GT_EXPR: val = (cmp > 0); break;
3209 case LE_EXPR: val = (cmp <= 0); break;
3210 case GE_EXPR: val = (cmp >= 0); break;
3211 default: gcc_unreachable ();
3215 (if (code1 == EQ_EXPR && val) @4)
3216 (if (code1 == NE_EXPR && val && allbits) { constant_boolean_node (true, type); })
3217 (if (code1 == NE_EXPR && !val && allbits) @3)
3218 (if (code1 == EQ_EXPR
3223 (if (code1 == EQ_EXPR
3228 /* (a == (b-1)) | (a >= b) -> a >= (b-1) */
3229 (if (code1 == EQ_EXPR
3233 (ge @c0 (convert @1)))
3234 /* (a == (b+1)) | (a <= b) -> a <= (b-1) */
3235 (if (code1 == EQ_EXPR
3239 (le @c0 (convert @1)))
3247 /* Convert (X OP1 CST1) || (X OP2 CST2).
3248 Convert (X OP1 Y) || (X OP2 Y). */
3250 (for code1 (lt le gt ge)
3251 (for code2 (lt le gt ge)
3253 (bit_ior (code1@3 @0 @1) (code2@4 @0 @2))
3254 (if ((TREE_CODE (@1) == INTEGER_CST
3255 && TREE_CODE (@2) == INTEGER_CST)
3256 || ((INTEGRAL_TYPE_P (TREE_TYPE (@1))
3257 || POINTER_TYPE_P (TREE_TYPE (@1)))
3258 && operand_equal_p (@1, @2)))
3262 if (TREE_CODE (@1) == INTEGER_CST
3263 && TREE_CODE (@2) == INTEGER_CST)
3264 cmp = tree_int_cst_compare (@1, @2);
3267 /* Choose the more restrictive of two < or <= comparisons. */
3268 (if ((code1 == LT_EXPR || code1 == LE_EXPR)
3269 && (code2 == LT_EXPR || code2 == LE_EXPR))
3270 (if ((cmp < 0) || (cmp == 0 && code1 == LT_EXPR))
3273 /* Likewise chose the more restrictive of two > or >= comparisons. */
3274 (if ((code1 == GT_EXPR || code1 == GE_EXPR)
3275 && (code2 == GT_EXPR || code2 == GE_EXPR))
3276 (if ((cmp > 0) || (cmp == 0 && code1 == GT_EXPR))
3279 /* Check for singleton ranges. */
3281 && ((code1 == LT_EXPR && code2 == GT_EXPR)
3282 || (code1 == GT_EXPR && code2 == LT_EXPR)))
3284 /* Check for disjoint ranges. */
3286 && (code1 == LT_EXPR || code1 == LE_EXPR)
3287 && (code2 == GT_EXPR || code2 == GE_EXPR))
3288 { constant_boolean_node (true, type); })
3290 && (code1 == GT_EXPR || code1 == GE_EXPR)
3291 && (code2 == LT_EXPR || code2 == LE_EXPR))
3292 { constant_boolean_node (true, type); })
3295 /* Optimize (a CMP b) ^ (a CMP b) */
3296 /* Optimize (a CMP b) != (a CMP b) */
3297 (for op (bit_xor ne)
3298 (for cmp1 (lt lt lt le le le)
3299 cmp2 (gt eq ne ge eq ne)
3300 rcmp (ne le gt ne lt ge)
3302 (op:c (cmp1:c @0 @1) (cmp2:c @0 @1))
3303 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
3306 /* Optimize (a CMP b) == (a CMP b) */
3307 (for cmp1 (lt lt lt le le le)
3308 cmp2 (gt eq ne ge eq ne)
3309 rcmp (eq gt le eq ge lt)
3311 (eq:c (cmp1:c @0 @1) (cmp2:c @0 @1))
3312 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
3315 /* We can't reassociate at all for saturating types. */
3316 (if (!TYPE_SATURATING (type))
3318 /* Contract negates. */
3319 /* A + (-B) -> A - B */
3321 (plus:c @0 (convert? (negate @1)))
3322 /* Apply STRIP_NOPS on the negate. */
3323 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
3324 && !TYPE_OVERFLOW_SANITIZED (type))
3328 if (INTEGRAL_TYPE_P (type)
3329 && TYPE_OVERFLOW_WRAPS (type) != TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
3330 t1 = TYPE_OVERFLOW_WRAPS (type) ? type : TREE_TYPE (@1);
3332 (convert (minus (convert:t1 @0) (convert:t1 @1))))))
3333 /* A - (-B) -> A + B */
3335 (minus @0 (convert? (negate @1)))
3336 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
3337 && !TYPE_OVERFLOW_SANITIZED (type))
3341 if (INTEGRAL_TYPE_P (type)
3342 && TYPE_OVERFLOW_WRAPS (type) != TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
3343 t1 = TYPE_OVERFLOW_WRAPS (type) ? type : TREE_TYPE (@1);
3345 (convert (plus (convert:t1 @0) (convert:t1 @1))))))
3347 Sign-extension is ok except for INT_MIN, which thankfully cannot
3348 happen without overflow. */
3350 (negate (convert (negate @1)))
3351 (if (INTEGRAL_TYPE_P (type)
3352 && (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@1))
3353 || (!TYPE_UNSIGNED (TREE_TYPE (@1))
3354 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))))
3355 && !TYPE_OVERFLOW_SANITIZED (type)
3356 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@1)))
3359 (negate (convert negate_expr_p@1))
3360 (if (SCALAR_FLOAT_TYPE_P (type)
3361 && ((DECIMAL_FLOAT_TYPE_P (type)
3362 == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1))
3363 && TYPE_PRECISION (type) >= TYPE_PRECISION (TREE_TYPE (@1)))
3364 || !HONOR_SIGN_DEPENDENT_ROUNDING (type)))
3365 (convert (negate @1))))
3367 (negate (nop_convert? (negate @1)))
3368 (if (!TYPE_OVERFLOW_SANITIZED (type)
3369 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@1)))
3372 /* We can't reassociate floating-point unless -fassociative-math
3373 or fixed-point plus or minus because of saturation to +-Inf. */
3374 (if ((!FLOAT_TYPE_P (type) || flag_associative_math)
3375 && !FIXED_POINT_TYPE_P (type))
3377 /* Match patterns that allow contracting a plus-minus pair
3378 irrespective of overflow issues. */
3379 /* (A +- B) - A -> +- B */
3380 /* (A +- B) -+ B -> A */
3381 /* A - (A +- B) -> -+ B */
3382 /* A +- (B -+ A) -> +- B */
3384 (minus (nop_convert1? (plus:c (nop_convert2? @0) @1)) @0)
3387 (minus (nop_convert1? (minus (nop_convert2? @0) @1)) @0)
3388 (if (!ANY_INTEGRAL_TYPE_P (type)
3389 || TYPE_OVERFLOW_WRAPS (type))
3390 (negate (view_convert @1))
3391 (view_convert (negate @1))))
3393 (plus:c (nop_convert1? (minus @0 (nop_convert2? @1))) @1)
3396 (minus @0 (nop_convert1? (plus:c (nop_convert2? @0) @1)))
3397 (if (!ANY_INTEGRAL_TYPE_P (type)
3398 || TYPE_OVERFLOW_WRAPS (type))
3399 (negate (view_convert @1))
3400 (view_convert (negate @1))))
3402 (minus @0 (nop_convert1? (minus (nop_convert2? @0) @1)))
3404 /* (A +- B) + (C - A) -> C +- B */
3405 /* (A + B) - (A - C) -> B + C */
3406 /* More cases are handled with comparisons. */
3408 (plus:c (plus:c @0 @1) (minus @2 @0))
3411 (plus:c (minus @0 @1) (minus @2 @0))
3414 (plus:c (pointer_diff @0 @1) (pointer_diff @2 @0))
3415 (if (TYPE_OVERFLOW_UNDEFINED (type)
3416 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0)))
3417 (pointer_diff @2 @1)))
3419 (minus (plus:c @0 @1) (minus @0 @2))
3422 /* (A +- CST1) +- CST2 -> A + CST3
3423 Use view_convert because it is safe for vectors and equivalent for
3425 (for outer_op (plus minus)
3426 (for inner_op (plus minus)
3427 neg_inner_op (minus plus)
3429 (outer_op (nop_convert? (inner_op @0 CONSTANT_CLASS_P@1))
3431 /* If one of the types wraps, use that one. */
3432 (if (!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_WRAPS (type))
3433 /* If all 3 captures are CONSTANT_CLASS_P, punt, as we might recurse
3434 forever if something doesn't simplify into a constant. */
3435 (if (!CONSTANT_CLASS_P (@0))
3436 (if (outer_op == PLUS_EXPR)
3437 (plus (view_convert @0) (inner_op! @2 (view_convert @1)))
3438 (minus (view_convert @0) (neg_inner_op! @2 (view_convert @1)))))
3439 (if (!ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3440 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
3441 (if (outer_op == PLUS_EXPR)
3442 (view_convert (plus @0 (inner_op! (view_convert @2) @1)))
3443 (view_convert (minus @0 (neg_inner_op! (view_convert @2) @1))))
3444 /* If the constant operation overflows we cannot do the transform
3445 directly as we would introduce undefined overflow, for example
3446 with (a - 1) + INT_MIN. */
3447 (if (types_match (type, @0) && !TYPE_OVERFLOW_SANITIZED (type))
3448 (with { tree cst = const_binop (outer_op == inner_op
3449 ? PLUS_EXPR : MINUS_EXPR,
3452 (if (INTEGRAL_TYPE_P (type) && !TREE_OVERFLOW (cst))
3453 (inner_op @0 { cst; } )
3454 /* X+INT_MAX+1 is X-INT_MIN. */
3455 (if (INTEGRAL_TYPE_P (type)
3456 && wi::to_wide (cst) == wi::min_value (type))
3457 (neg_inner_op @0 { wide_int_to_tree (type, wi::to_wide (cst)); })
3458 /* Last resort, use some unsigned type. */
3459 (with { tree utype = unsigned_type_for (type); }
3461 (view_convert (inner_op
3462 (view_convert:utype @0)
3464 { TREE_OVERFLOW (cst)
3465 ? drop_tree_overflow (cst) : cst; })))))))))))))))
3467 /* (CST1 - A) +- CST2 -> CST3 - A */
3468 (for outer_op (plus minus)
3470 (outer_op (nop_convert? (minus CONSTANT_CLASS_P@1 @0)) CONSTANT_CLASS_P@2)
3471 /* If one of the types wraps, use that one. */
3472 (if (!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_WRAPS (type))
3473 /* If all 3 captures are CONSTANT_CLASS_P, punt, as we might recurse
3474 forever if something doesn't simplify into a constant. */
3475 (if (!CONSTANT_CLASS_P (@0))
3476 (minus (outer_op! (view_convert @1) @2) (view_convert @0)))
3477 (if (!ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3478 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
3479 (view_convert (minus (outer_op! @1 (view_convert @2)) @0))
3480 (if (types_match (type, @0) && !TYPE_OVERFLOW_SANITIZED (type))
3481 (with { tree cst = const_binop (outer_op, type, @1, @2); }
3482 (if (cst && !TREE_OVERFLOW (cst))
3483 (minus { cst; } @0))))))))
3485 /* CST1 - (CST2 - A) -> CST3 + A
3486 Use view_convert because it is safe for vectors and equivalent for
3489 (minus CONSTANT_CLASS_P@1 (nop_convert? (minus CONSTANT_CLASS_P@2 @0)))
3490 /* If one of the types wraps, use that one. */
3491 (if (!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_WRAPS (type))
3492 /* If all 3 captures are CONSTANT_CLASS_P, punt, as we might recurse
3493 forever if something doesn't simplify into a constant. */
3494 (if (!CONSTANT_CLASS_P (@0))
3495 (plus (view_convert @0) (minus! @1 (view_convert @2))))
3496 (if (!ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3497 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
3498 (view_convert (plus @0 (minus! (view_convert @1) @2)))
3499 (if (types_match (type, @0) && !TYPE_OVERFLOW_SANITIZED (type))
3500 (with { tree cst = const_binop (MINUS_EXPR, type, @1, @2); }
3501 (if (cst && !TREE_OVERFLOW (cst))
3502 (plus { cst; } @0)))))))
3504 /* ((T)(A)) + CST -> (T)(A + CST) */
3507 (plus (convert:s SSA_NAME@0) INTEGER_CST@1)
3508 (if (TREE_CODE (TREE_TYPE (@0)) == INTEGER_TYPE
3509 && TREE_CODE (type) == INTEGER_TYPE
3510 && TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (@0))
3511 && int_fits_type_p (@1, TREE_TYPE (@0)))
3512 /* Perform binary operation inside the cast if the constant fits
3513 and (A + CST)'s range does not overflow. */
3516 wi::overflow_type min_ovf = wi::OVF_OVERFLOW,
3517 max_ovf = wi::OVF_OVERFLOW;
3518 tree inner_type = TREE_TYPE (@0);
3521 = wide_int::from (wi::to_wide (@1), TYPE_PRECISION (inner_type),
3522 TYPE_SIGN (inner_type));
3525 if (get_global_range_query ()->range_of_expr (vr, @0)
3526 && !vr.varying_p () && !vr.undefined_p ())
3528 wide_int wmin0 = vr.lower_bound ();
3529 wide_int wmax0 = vr.upper_bound ();
3530 wi::add (wmin0, w1, TYPE_SIGN (inner_type), &min_ovf);
3531 wi::add (wmax0, w1, TYPE_SIGN (inner_type), &max_ovf);
3534 (if (min_ovf == wi::OVF_NONE && max_ovf == wi::OVF_NONE)
3535 (convert (plus @0 { wide_int_to_tree (TREE_TYPE (@0), w1); } )))
3539 /* ((T)(A + CST1)) + CST2 -> (T)(A) + (T)CST1 + CST2 */
3541 (for op (plus minus)
3543 (plus (convert:s (op:s @0 INTEGER_CST@1)) INTEGER_CST@2)
3544 (if (TREE_CODE (TREE_TYPE (@0)) == INTEGER_TYPE
3545 && TREE_CODE (type) == INTEGER_TYPE
3546 && TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (@0))
3547 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
3548 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0))
3549 && TYPE_OVERFLOW_WRAPS (type))
3550 (plus (convert @0) (op @2 (convert @1))))))
3553 /* (T)(A) +- (T)(B) -> (T)(A +- B) only when (A +- B) could be simplified
3554 to a simple value. */
3555 (for op (plus minus)
3557 (op (convert @0) (convert @1))
3558 (if (INTEGRAL_TYPE_P (type)
3559 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
3560 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))
3561 && types_match (TREE_TYPE (@0), TREE_TYPE (@1))
3562 && !TYPE_OVERFLOW_TRAPS (type)
3563 && !TYPE_OVERFLOW_SANITIZED (type))
3564 (convert (op! @0 @1)))))
3568 (plus:c (convert? (bit_not @0)) (convert? @0))
3569 (if (!TYPE_OVERFLOW_TRAPS (type))
3570 (convert { build_all_ones_cst (TREE_TYPE (@0)); })))
3574 (plus (convert? (bit_not @0)) integer_each_onep)
3575 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
3576 (negate (convert @0))))
3580 (minus (convert? (negate @0)) integer_each_onep)
3581 (if (!TYPE_OVERFLOW_TRAPS (type)
3582 && TREE_CODE (type) != COMPLEX_TYPE
3583 && tree_nop_conversion_p (type, TREE_TYPE (@0)))
3584 (bit_not (convert @0))))
3588 (minus integer_all_onesp @0)
3589 (if (TREE_CODE (type) != COMPLEX_TYPE)
3592 /* (T)(P + A) - (T)P -> (T) A */
3594 (minus (convert (plus:c @@0 @1))
3596 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
3597 /* For integer types, if A has a smaller type
3598 than T the result depends on the possible
3600 E.g. T=size_t, A=(unsigned)429497295, P>0.
3601 However, if an overflow in P + A would cause
3602 undefined behavior, we can assume that there
3604 || (INTEGRAL_TYPE_P (TREE_TYPE (@1))
3605 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))))
3608 (minus (convert (pointer_plus @@0 @1))
3610 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
3611 /* For pointer types, if the conversion of A to the
3612 final type requires a sign- or zero-extension,
3613 then we have to punt - it is not defined which
3615 || (POINTER_TYPE_P (TREE_TYPE (@0))
3616 && TREE_CODE (@1) == INTEGER_CST
3617 && tree_int_cst_sign_bit (@1) == 0))
3620 (pointer_diff (pointer_plus @@0 @1) @0)
3621 /* The second argument of pointer_plus must be interpreted as signed, and
3622 thus sign-extended if necessary. */
3623 (with { tree stype = signed_type_for (TREE_TYPE (@1)); }
3624 /* Use view_convert instead of convert here, as POINTER_PLUS_EXPR
3625 second arg is unsigned even when we need to consider it as signed,
3626 we don't want to diagnose overflow here. */
3627 (convert (view_convert:stype @1))))
3629 /* (T)P - (T)(P + A) -> -(T) A */
3631 (minus (convert? @0)
3632 (convert (plus:c @@0 @1)))
3633 (if (INTEGRAL_TYPE_P (type)
3634 && TYPE_OVERFLOW_UNDEFINED (type)
3635 /* For integer literals, using an intermediate unsigned type to avoid
3636 an overflow at run time is counter-productive because it introduces
3637 spurious overflows at compile time, in the form of TREE_OVERFLOW on
3638 the result, which may be problematic in GENERIC for some front-ends:
3639 (T)P - (T)(P + 4) -> (T)(-(U)4) -> (T)(4294967292) -> -4(OVF)
3640 so we use the direct path for them. */
3641 && TREE_CODE (@1) != INTEGER_CST
3642 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
3643 (with { tree utype = unsigned_type_for (type); }
3644 (convert (negate (convert:utype @1))))
3645 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
3646 /* For integer types, if A has a smaller type
3647 than T the result depends on the possible
3649 E.g. T=size_t, A=(unsigned)429497295, P>0.
3650 However, if an overflow in P + A would cause
3651 undefined behavior, we can assume that there
3653 || (INTEGRAL_TYPE_P (TREE_TYPE (@1))
3654 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))))
3655 (negate (convert @1)))))
3658 (convert (pointer_plus @@0 @1)))
3659 (if (INTEGRAL_TYPE_P (type)
3660 && TYPE_OVERFLOW_UNDEFINED (type)
3661 /* See above the rationale for this condition. */
3662 && TREE_CODE (@1) != INTEGER_CST
3663 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
3664 (with { tree utype = unsigned_type_for (type); }
3665 (convert (negate (convert:utype @1))))
3666 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
3667 /* For pointer types, if the conversion of A to the
3668 final type requires a sign- or zero-extension,
3669 then we have to punt - it is not defined which
3671 || (POINTER_TYPE_P (TREE_TYPE (@0))
3672 && TREE_CODE (@1) == INTEGER_CST
3673 && tree_int_cst_sign_bit (@1) == 0))
3674 (negate (convert @1)))))
3676 (pointer_diff @0 (pointer_plus @@0 @1))
3677 /* The second argument of pointer_plus must be interpreted as signed, and
3678 thus sign-extended if necessary. */
3679 (with { tree stype = signed_type_for (TREE_TYPE (@1)); }
3680 /* Use view_convert instead of convert here, as POINTER_PLUS_EXPR
3681 second arg is unsigned even when we need to consider it as signed,
3682 we don't want to diagnose overflow here. */
3683 (negate (convert (view_convert:stype @1)))))
3685 /* (T)(P + A) - (T)(P + B) -> (T)A - (T)B */
3687 (minus (convert (plus:c @@0 @1))
3688 (convert (plus:c @0 @2)))
3689 (if (INTEGRAL_TYPE_P (type)
3690 && TYPE_OVERFLOW_UNDEFINED (type)
3691 && element_precision (type) <= element_precision (TREE_TYPE (@1))
3692 && element_precision (type) <= element_precision (TREE_TYPE (@2)))
3693 (with { tree utype = unsigned_type_for (type); }
3694 (convert (minus (convert:utype @1) (convert:utype @2))))
3695 (if (((element_precision (type) <= element_precision (TREE_TYPE (@1)))
3696 == (element_precision (type) <= element_precision (TREE_TYPE (@2))))
3697 && (element_precision (type) <= element_precision (TREE_TYPE (@1))
3698 /* For integer types, if A has a smaller type
3699 than T the result depends on the possible
3701 E.g. T=size_t, A=(unsigned)429497295, P>0.
3702 However, if an overflow in P + A would cause
3703 undefined behavior, we can assume that there
3705 || (INTEGRAL_TYPE_P (TREE_TYPE (@1))
3706 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
3707 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))
3708 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@2)))))
3709 (minus (convert @1) (convert @2)))))
3711 (minus (convert (pointer_plus @@0 @1))
3712 (convert (pointer_plus @0 @2)))
3713 (if (INTEGRAL_TYPE_P (type)
3714 && TYPE_OVERFLOW_UNDEFINED (type)
3715 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
3716 (with { tree utype = unsigned_type_for (type); }
3717 (convert (minus (convert:utype @1) (convert:utype @2))))
3718 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
3719 /* For pointer types, if the conversion of A to the
3720 final type requires a sign- or zero-extension,
3721 then we have to punt - it is not defined which
3723 || (POINTER_TYPE_P (TREE_TYPE (@0))
3724 && TREE_CODE (@1) == INTEGER_CST
3725 && tree_int_cst_sign_bit (@1) == 0
3726 && TREE_CODE (@2) == INTEGER_CST
3727 && tree_int_cst_sign_bit (@2) == 0))
3728 (minus (convert @1) (convert @2)))))
3730 (pointer_diff (pointer_plus @0 @2) (pointer_plus @1 @2))
3731 (pointer_diff @0 @1))
3733 (pointer_diff (pointer_plus @@0 @1) (pointer_plus @0 @2))
3734 /* The second argument of pointer_plus must be interpreted as signed, and
3735 thus sign-extended if necessary. */
3736 (with { tree stype = signed_type_for (TREE_TYPE (@1)); }
3737 /* Use view_convert instead of convert here, as POINTER_PLUS_EXPR
3738 second arg is unsigned even when we need to consider it as signed,
3739 we don't want to diagnose overflow here. */
3740 (minus (convert (view_convert:stype @1))
3741 (convert (view_convert:stype @2)))))))
3743 /* (A * C) +- (B * C) -> (A+-B) * C and (A * C) +- A -> A * (C+-1).
3744 Modeled after fold_plusminus_mult_expr. */
3745 (if (!TYPE_SATURATING (type)
3746 && (!FLOAT_TYPE_P (type) || flag_associative_math))
3747 (for plusminus (plus minus)
3749 (plusminus (mult:cs@3 @0 @1) (mult:cs@4 @0 @2))
3750 (if (!ANY_INTEGRAL_TYPE_P (type)
3751 || TYPE_OVERFLOW_WRAPS (type)
3752 || (INTEGRAL_TYPE_P (type)
3753 && tree_expr_nonzero_p (@0)
3754 && expr_not_equal_to (@0, wi::minus_one (TYPE_PRECISION (type)))))
3755 (if (single_use (@3) || single_use (@4))
3756 /* If @1 +- @2 is constant require a hard single-use on either
3757 original operand (but not on both). */
3758 (mult (plusminus @1 @2) @0)
3759 (mult! (plusminus @1 @2) @0)
3761 /* We cannot generate constant 1 for fract. */
3762 (if (!ALL_FRACT_MODE_P (TYPE_MODE (type)))
3764 (plusminus @0 (mult:c@3 @0 @2))
3765 (if ((!ANY_INTEGRAL_TYPE_P (type)
3766 || TYPE_OVERFLOW_WRAPS (type)
3767 /* For @0 + @0*@2 this transformation would introduce UB
3768 (where there was none before) for @0 in [-1,0] and @2 max.
3769 For @0 - @0*@2 this transformation would introduce UB
3770 for @0 0 and @2 in [min,min+1] or @0 -1 and @2 min+1. */
3771 || (INTEGRAL_TYPE_P (type)
3772 && ((tree_expr_nonzero_p (@0)
3773 && expr_not_equal_to (@0,
3774 wi::minus_one (TYPE_PRECISION (type))))
3775 || (plusminus == PLUS_EXPR
3776 ? expr_not_equal_to (@2,
3777 wi::max_value (TYPE_PRECISION (type), SIGNED))
3778 /* Let's ignore the @0 -1 and @2 min case. */
3779 : (expr_not_equal_to (@2,
3780 wi::min_value (TYPE_PRECISION (type), SIGNED))
3781 && expr_not_equal_to (@2,
3782 wi::min_value (TYPE_PRECISION (type), SIGNED)
3785 (mult (plusminus { build_one_cst (type); } @2) @0)))
3787 (plusminus (mult:c@3 @0 @2) @0)
3788 (if ((!ANY_INTEGRAL_TYPE_P (type)
3789 || TYPE_OVERFLOW_WRAPS (type)
3790 /* For @0*@2 + @0 this transformation would introduce UB
3791 (where there was none before) for @0 in [-1,0] and @2 max.
3792 For @0*@2 - @0 this transformation would introduce UB
3793 for @0 0 and @2 min. */
3794 || (INTEGRAL_TYPE_P (type)
3795 && ((tree_expr_nonzero_p (@0)
3796 && (plusminus == MINUS_EXPR
3797 || expr_not_equal_to (@0,
3798 wi::minus_one (TYPE_PRECISION (type)))))
3799 || expr_not_equal_to (@2,
3800 (plusminus == PLUS_EXPR
3801 ? wi::max_value (TYPE_PRECISION (type), SIGNED)
3802 : wi::min_value (TYPE_PRECISION (type), SIGNED))))))
3804 (mult (plusminus @2 { build_one_cst (type); }) @0))))))
3807 /* Canonicalize X + (X << C) into X * (1 + (1 << C)) and
3808 (X << C1) + (X << C2) into X * ((1 << C1) + (1 << C2)). */
3810 (plus:c @0 (lshift:s @0 INTEGER_CST@1))
3811 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3812 && tree_fits_uhwi_p (@1)
3813 && tree_to_uhwi (@1) < element_precision (type)
3814 && (INTEGRAL_TYPE_P (TREE_TYPE (@0))
3815 || optab_handler (smul_optab,
3816 TYPE_MODE (type)) != CODE_FOR_nothing))
3817 (with { tree t = type;
3818 if (!TYPE_OVERFLOW_WRAPS (t)) t = unsigned_type_for (t);
3819 wide_int w = wi::set_bit_in_zero (tree_to_uhwi (@1),
3820 element_precision (type));
3822 tree cst = wide_int_to_tree (VECTOR_TYPE_P (t) ? TREE_TYPE (t)
3824 cst = build_uniform_cst (t, cst); }
3825 (convert (mult (convert:t @0) { cst; })))))
3827 (plus (lshift:s @0 INTEGER_CST@1) (lshift:s @0 INTEGER_CST@2))
3828 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3829 && tree_fits_uhwi_p (@1)
3830 && tree_to_uhwi (@1) < element_precision (type)
3831 && tree_fits_uhwi_p (@2)
3832 && tree_to_uhwi (@2) < element_precision (type)
3833 && (INTEGRAL_TYPE_P (TREE_TYPE (@0))
3834 || optab_handler (smul_optab,
3835 TYPE_MODE (type)) != CODE_FOR_nothing))
3836 (with { tree t = type;
3837 if (!TYPE_OVERFLOW_WRAPS (t)) t = unsigned_type_for (t);
3838 unsigned int prec = element_precision (type);
3839 wide_int w = wi::set_bit_in_zero (tree_to_uhwi (@1), prec);
3840 w += wi::set_bit_in_zero (tree_to_uhwi (@2), prec);
3841 tree cst = wide_int_to_tree (VECTOR_TYPE_P (t) ? TREE_TYPE (t)
3843 cst = build_uniform_cst (t, cst); }
3844 (convert (mult (convert:t @0) { cst; })))))
3847 /* Canonicalize (X*C1)|(X*C2) and (X*C1)^(X*C2) to (C1+C2)*X when
3848 tree_nonzero_bits allows IOR and XOR to be treated like PLUS.
3849 Likewise, handle (X<<C3) and X as legitimate variants of X*C. */
3850 (for op (bit_ior bit_xor)
3852 (op (mult:s@0 @1 INTEGER_CST@2)
3853 (mult:s@3 @1 INTEGER_CST@4))
3854 (if (INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_WRAPS (type)
3855 && (tree_nonzero_bits (@0) & tree_nonzero_bits (@3)) == 0)
3857 { wide_int_to_tree (type, wi::to_wide (@2) + wi::to_wide (@4)); })))
3859 (op:c (mult:s@0 @1 INTEGER_CST@2)
3860 (lshift:s@3 @1 INTEGER_CST@4))
3861 (if (INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_WRAPS (type)
3862 && tree_int_cst_sgn (@4) > 0
3863 && (tree_nonzero_bits (@0) & tree_nonzero_bits (@3)) == 0)
3864 (with { wide_int wone = wi::one (TYPE_PRECISION (type));
3865 wide_int c = wi::add (wi::to_wide (@2),
3866 wi::lshift (wone, wi::to_wide (@4))); }
3867 (mult @1 { wide_int_to_tree (type, c); }))))
3869 (op:c (mult:s@0 @1 INTEGER_CST@2)
3871 (if (INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_WRAPS (type)
3872 && (tree_nonzero_bits (@0) & tree_nonzero_bits (@1)) == 0)
3874 { wide_int_to_tree (type,
3875 wi::add (wi::to_wide (@2), 1)); })))
3877 (op (lshift:s@0 @1 INTEGER_CST@2)
3878 (lshift:s@3 @1 INTEGER_CST@4))
3879 (if (INTEGRAL_TYPE_P (type)
3880 && tree_int_cst_sgn (@2) > 0
3881 && tree_int_cst_sgn (@4) > 0
3882 && (tree_nonzero_bits (@0) & tree_nonzero_bits (@3)) == 0)
3883 (with { tree t = type;
3884 if (!TYPE_OVERFLOW_WRAPS (t))
3885 t = unsigned_type_for (t);
3886 wide_int wone = wi::one (TYPE_PRECISION (t));
3887 wide_int c = wi::add (wi::lshift (wone, wi::to_wide (@2)),
3888 wi::lshift (wone, wi::to_wide (@4))); }
3889 (convert (mult:t (convert:t @1) { wide_int_to_tree (t,c); })))))
3891 (op:c (lshift:s@0 @1 INTEGER_CST@2)
3893 (if (INTEGRAL_TYPE_P (type)
3894 && tree_int_cst_sgn (@2) > 0
3895 && (tree_nonzero_bits (@0) & tree_nonzero_bits (@1)) == 0)
3896 (with { tree t = type;
3897 if (!TYPE_OVERFLOW_WRAPS (t))
3898 t = unsigned_type_for (t);
3899 wide_int wone = wi::one (TYPE_PRECISION (t));
3900 wide_int c = wi::add (wi::lshift (wone, wi::to_wide (@2)), wone); }
3901 (convert (mult:t (convert:t @1) { wide_int_to_tree (t, c); }))))))
3903 /* Simplifications of MIN_EXPR, MAX_EXPR, fmin() and fmax(). */
3905 (for minmax (min max)
3909 /* max(max(x,y),x) -> max(x,y) */
3911 (minmax:c (minmax:c@2 @0 @1) @0)
3913 /* For fmin() and fmax(), skip folding when both are sNaN. */
3914 (for minmax (FMIN_ALL FMAX_ALL)
3917 (if (!tree_expr_maybe_signaling_nan_p (@0))
3919 /* min(max(x,y),y) -> y. */
3921 (min:c (max:c @0 @1) @1)
3923 /* max(min(x,y),y) -> y. */
3925 (max:c (min:c @0 @1) @1)
3927 /* max(a,-a) -> abs(a). */
3929 (max:c @0 (negate @0))
3930 (if (TREE_CODE (type) != COMPLEX_TYPE
3931 && (! ANY_INTEGRAL_TYPE_P (type)
3932 || TYPE_OVERFLOW_UNDEFINED (type)))
3934 /* min(a,-a) -> -abs(a). */
3936 (min:c @0 (negate @0))
3937 (if (TREE_CODE (type) != COMPLEX_TYPE
3938 && (! ANY_INTEGRAL_TYPE_P (type)
3939 || TYPE_OVERFLOW_UNDEFINED (type)))
3944 (if (INTEGRAL_TYPE_P (type)
3945 && TYPE_MIN_VALUE (type)
3946 && operand_equal_p (@1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
3948 (if (INTEGRAL_TYPE_P (type)
3949 && TYPE_MAX_VALUE (type)
3950 && operand_equal_p (@1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
3955 (if (INTEGRAL_TYPE_P (type)
3956 && TYPE_MAX_VALUE (type)
3957 && operand_equal_p (@1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
3959 (if (INTEGRAL_TYPE_P (type)
3960 && TYPE_MIN_VALUE (type)
3961 && operand_equal_p (@1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
3964 /* max (a, a + CST) -> a + CST where CST is positive. */
3965 /* max (a, a + CST) -> a where CST is negative. */
3967 (max:c @0 (plus@2 @0 INTEGER_CST@1))
3968 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
3969 (if (tree_int_cst_sgn (@1) > 0)
3973 /* min (a, a + CST) -> a where CST is positive. */
3974 /* min (a, a + CST) -> a + CST where CST is negative. */
3976 (min:c @0 (plus@2 @0 INTEGER_CST@1))
3977 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
3978 (if (tree_int_cst_sgn (@1) > 0)
3982 /* Simplify min (&var[off0], &var[off1]) etc. depending on whether
3983 the addresses are known to be less, equal or greater. */
3984 (for minmax (min max)
3987 (minmax (convert1?@2 addr@0) (convert2?@3 addr@1))
3990 poly_int64 off0, off1;
3992 int equal = address_compare (cmp, TREE_TYPE (@2), @0, @1, base0, base1,
3993 off0, off1, GENERIC);
3996 (if (minmax == MIN_EXPR)
3997 (if (known_le (off0, off1))
3999 (if (known_gt (off0, off1))
4001 (if (known_ge (off0, off1))
4003 (if (known_lt (off0, off1))
4006 /* (convert (minmax ((convert (x) c)))) -> minmax (x c) if x is promoted
4007 and the outer convert demotes the expression back to x's type. */
4008 (for minmax (min max)
4010 (convert (minmax@0 (convert @1) INTEGER_CST@2))
4011 (if (INTEGRAL_TYPE_P (type)
4012 && types_match (@1, type) && int_fits_type_p (@2, type)
4013 && TYPE_SIGN (TREE_TYPE (@0)) == TYPE_SIGN (type)
4014 && TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type))
4015 (minmax @1 (convert @2)))))
4017 (for minmax (FMIN_ALL FMAX_ALL)
4018 /* If either argument is NaN and other one is not sNaN, return the other
4019 one. Avoid the transformation if we get (and honor) a signalling NaN. */
4021 (minmax:c @0 REAL_CST@1)
4022 (if (real_isnan (TREE_REAL_CST_PTR (@1))
4023 && (!HONOR_SNANS (@1) || !TREE_REAL_CST (@1).signalling)
4024 && !tree_expr_maybe_signaling_nan_p (@0))
4026 /* Convert fmin/fmax to MIN_EXPR/MAX_EXPR. C99 requires these
4027 functions to return the numeric arg if the other one is NaN.
4028 MIN and MAX don't honor that, so only transform if -ffinite-math-only
4029 is set. C99 doesn't require -0.0 to be handled, so we don't have to
4030 worry about it either. */
4031 (if (flag_finite_math_only)
4038 /* min (-A, -B) -> -max (A, B) */
4039 (for minmax (min max FMIN_ALL FMAX_ALL)
4040 maxmin (max min FMAX_ALL FMIN_ALL)
4042 (minmax (negate:s@2 @0) (negate:s@3 @1))
4043 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
4044 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
4045 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
4046 (negate (maxmin @0 @1)))))
4047 /* MIN (~X, ~Y) -> ~MAX (X, Y)
4048 MAX (~X, ~Y) -> ~MIN (X, Y) */
4049 (for minmax (min max)
4052 (minmax (bit_not:s@2 @0) (bit_not:s@3 @1))
4053 (bit_not (maxmin @0 @1)))
4054 /* ~MAX(~X, Y) --> MIN(X, ~Y) */
4055 /* ~MIN(~X, Y) --> MAX(X, ~Y) */
4057 (bit_not (minmax:cs (bit_not @0) @1))
4058 (maxmin @0 (bit_not @1))))
4060 /* MIN (X, Y) == X -> X <= Y */
4061 /* MIN (X, Y) < X -> X > Y */
4062 /* MIN (X, Y) >= X -> X <= Y */
4063 (for minmax (min min min min max max max max)
4064 cmp (eq ne lt ge eq ne gt le )
4065 out (le gt gt le ge lt lt ge )
4067 (cmp:c (minmax:c @0 @1) @0)
4068 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0)))
4070 /* MIN (X, 5) == 0 -> X == 0
4071 MIN (X, 5) == 7 -> false */
4074 (cmp (min @0 INTEGER_CST@1) INTEGER_CST@2)
4075 (if (wi::lt_p (wi::to_wide (@1), wi::to_wide (@2),
4076 TYPE_SIGN (TREE_TYPE (@0))))
4077 { constant_boolean_node (cmp == NE_EXPR, type); }
4078 (if (wi::gt_p (wi::to_wide (@1), wi::to_wide (@2),
4079 TYPE_SIGN (TREE_TYPE (@0))))
4083 (cmp (max @0 INTEGER_CST@1) INTEGER_CST@2)
4084 (if (wi::gt_p (wi::to_wide (@1), wi::to_wide (@2),
4085 TYPE_SIGN (TREE_TYPE (@0))))
4086 { constant_boolean_node (cmp == NE_EXPR, type); }
4087 (if (wi::lt_p (wi::to_wide (@1), wi::to_wide (@2),
4088 TYPE_SIGN (TREE_TYPE (@0))))
4091 /* X <= MAX(X, Y) -> true
4092 X > MAX(X, Y) -> false
4093 X >= MIN(X, Y) -> true
4094 X < MIN(X, Y) -> false */
4095 (for minmax (min min max max )
4098 (cmp:c @0 (minmax:c @0 @1))
4099 { constant_boolean_node (cmp == GE_EXPR || cmp == LE_EXPR, type); } ))
4101 /* MIN (X, C1) < C2 -> X < C2 || C1 < C2 */
4102 (for minmax (min min max max min min max max )
4103 cmp (lt le gt ge gt ge lt le )
4104 comb (bit_ior bit_ior bit_ior bit_ior bit_and bit_and bit_and bit_and)
4106 (cmp (minmax @0 INTEGER_CST@1) INTEGER_CST@2)
4107 (comb (cmp @0 @2) (cmp @1 @2))))
4109 /* Undo fancy ways of writing max/min or other ?: expressions, like
4110 a - ((a - b) & -(a < b)) and a - (a - b) * (a < b) into (a < b) ? b : a.
4111 People normally use ?: and that is what we actually try to optimize. */
4112 /* Transform A + (B-A)*cmp into cmp ? B : A. */
4114 (plus:c @0 (mult:c (minus @1 @0) zero_one_valued_p@2))
4115 (if (INTEGRAL_TYPE_P (type)
4116 && (GIMPLE || !TREE_SIDE_EFFECTS (@1)))
4117 (cond (convert:boolean_type_node @2) @1 @0)))
4118 /* Transform A - (A-B)*cmp into cmp ? B : A. */
4120 (minus @0 (mult:c (minus @0 @1) zero_one_valued_p@2))
4121 (if (INTEGRAL_TYPE_P (type)
4122 && (GIMPLE || !TREE_SIDE_EFFECTS (@1)))
4123 (cond (convert:boolean_type_node @2) @1 @0)))
4124 /* Transform A ^ (A^B)*cmp into cmp ? B : A. */
4126 (bit_xor:c @0 (mult:c (bit_xor:c @0 @1) zero_one_valued_p@2))
4127 (if (INTEGRAL_TYPE_P (type)
4128 && (GIMPLE || !TREE_SIDE_EFFECTS (@1)))
4129 (cond (convert:boolean_type_node @2) @1 @0)))
4131 /* (x <= 0 ? -x : 0) -> max(-x, 0). */
4133 (cond (le @0 integer_zerop@1) (negate@2 @0) integer_zerop@1)
4136 /* (zero_one == 0) ? y : z <op> y -> ((typeof(y))zero_one * z) <op> y */
4137 (for op (bit_xor bit_ior plus)
4139 (cond (eq zero_one_valued_p@0
4143 (if (INTEGRAL_TYPE_P (type)
4144 && TYPE_PRECISION (type) > 1
4145 && (INTEGRAL_TYPE_P (TREE_TYPE (@0))))
4146 (op (mult (convert:type @0) @2) @1))))
4148 /* (zero_one != 0) ? z <op> y : y -> ((typeof(y))zero_one * z) <op> y */
4149 (for op (bit_xor bit_ior plus)
4151 (cond (ne zero_one_valued_p@0
4155 (if (INTEGRAL_TYPE_P (type)
4156 && TYPE_PRECISION (type) > 1
4157 && (INTEGRAL_TYPE_P (TREE_TYPE (@0))))
4158 (op (mult (convert:type @0) @2) @1))))
4160 /* ?: Value replacement. */
4161 /* a == 0 ? b : b + a -> b + a */
4162 (for op (plus bit_ior bit_xor)
4164 (cond (eq @0 integer_zerop) @1 (op:c@2 @1 @0))
4166 /* a == 0 ? b : b - a -> b - a */
4167 /* a == 0 ? b : b ptr+ a -> b ptr+ a */
4168 /* a == 0 ? b : b shift/rotate a -> b shift/rotate a */
4169 (for op (lrotate rrotate lshift rshift minus pointer_plus)
4171 (cond (eq @0 integer_zerop) @1 (op@2 @1 @0))
4174 /* a == 1 ? b : b / a -> b / a */
4175 (for op (trunc_div ceil_div floor_div round_div exact_div)
4177 (cond (eq @0 integer_onep) @1 (op@2 @1 @0))
4180 /* a == 1 ? b : a * b -> a * b */
4183 (cond (eq @0 integer_onep) @1 (op:c@2 @1 @0))
4186 /* a == -1 ? b : a & b -> a & b */
4189 (cond (eq @0 integer_all_onesp) @1 (op:c@2 @1 @0))
4192 /* Simplifications of shift and rotates. */
4194 (for rotate (lrotate rrotate)
4196 (rotate integer_all_onesp@0 @1)
4199 /* Optimize -1 >> x for arithmetic right shifts. */
4201 (rshift integer_all_onesp@0 @1)
4202 (if (!TYPE_UNSIGNED (type))
4205 /* Optimize (x >> c) << c into x & (-1<<c). */
4207 (lshift (nop_convert? (rshift @0 INTEGER_CST@1)) @1)
4208 (if (wi::ltu_p (wi::to_wide (@1), element_precision (type)))
4209 /* It doesn't matter if the right shift is arithmetic or logical. */
4210 (bit_and (view_convert @0) (lshift { build_minus_one_cst (type); } @1))))
4213 (lshift (convert (convert@2 (rshift @0 INTEGER_CST@1))) @1)
4214 (if (wi::ltu_p (wi::to_wide (@1), element_precision (type))
4215 /* Allow intermediate conversion to integral type with whatever sign, as
4216 long as the low TYPE_PRECISION (type)
4217 - TYPE_PRECISION (TREE_TYPE (@2)) bits are preserved. */
4218 && INTEGRAL_TYPE_P (type)
4219 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
4220 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
4221 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (@0))
4222 && (TYPE_PRECISION (TREE_TYPE (@2)) >= TYPE_PRECISION (type)
4223 || wi::geu_p (wi::to_wide (@1),
4224 TYPE_PRECISION (type)
4225 - TYPE_PRECISION (TREE_TYPE (@2)))))
4226 (bit_and (convert @0) (lshift { build_minus_one_cst (type); } @1))))
4228 /* For (x << c) >> c, optimize into x & ((unsigned)-1 >> c) for
4229 unsigned x OR truncate into the precision(type) - c lowest bits
4230 of signed x (if they have mode precision or a precision of 1). */
4232 (rshift (nop_convert? (lshift @0 INTEGER_CST@1)) @@1)
4233 (if (wi::ltu_p (wi::to_wide (@1), element_precision (type)))
4234 (if (TYPE_UNSIGNED (type))
4235 (bit_and (convert @0) (rshift { build_minus_one_cst (type); } @1))
4236 (if (INTEGRAL_TYPE_P (type))
4238 int width = element_precision (type) - tree_to_uhwi (@1);
4239 tree stype = NULL_TREE;
4240 if (width <= MAX_FIXED_MODE_SIZE)
4241 stype = build_nonstandard_integer_type (width, 0);
4243 (if (stype && (width == 1 || type_has_mode_precision_p (stype)))
4244 (convert (convert:stype @0))))))))
4246 /* Optimize x >> x into 0 */
4249 { build_zero_cst (type); })
4251 (for shiftrotate (lrotate rrotate lshift rshift)
4253 (shiftrotate @0 integer_zerop)
4256 (shiftrotate integer_zerop@0 @1)
4258 /* Prefer vector1 << scalar to vector1 << vector2
4259 if vector2 is uniform. */
4260 (for vec (VECTOR_CST CONSTRUCTOR)
4262 (shiftrotate @0 vec@1)
4263 (with { tree tem = uniform_vector_p (@1); }
4265 (shiftrotate @0 { tem; }))))))
4267 /* Simplify X << Y where Y's low width bits are 0 to X, as only valid
4268 Y is 0. Similarly for X >> Y. */
4270 (for shift (lshift rshift)
4272 (shift @0 SSA_NAME@1)
4273 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1)))
4275 int width = ceil_log2 (element_precision (TREE_TYPE (@0)));
4276 int prec = TYPE_PRECISION (TREE_TYPE (@1));
4278 (if ((get_nonzero_bits (@1) & wi::mask (width, false, prec)) == 0)
4282 /* Rewrite an LROTATE_EXPR by a constant into an
4283 RROTATE_EXPR by a new constant. */
4285 (lrotate @0 INTEGER_CST@1)
4286 (rrotate @0 { const_binop (MINUS_EXPR, TREE_TYPE (@1),
4287 build_int_cst (TREE_TYPE (@1),
4288 element_precision (type)), @1); }))
4290 /* Turn (a OP c1) OP c2 into a OP (c1+c2). */
4291 (for op (lrotate rrotate rshift lshift)
4293 (op (op @0 INTEGER_CST@1) INTEGER_CST@2)
4294 (with { unsigned int prec = element_precision (type); }
4295 (if (wi::ge_p (wi::to_wide (@1), 0, TYPE_SIGN (TREE_TYPE (@1)))
4296 && wi::lt_p (wi::to_wide (@1), prec, TYPE_SIGN (TREE_TYPE (@1)))
4297 && wi::ge_p (wi::to_wide (@2), 0, TYPE_SIGN (TREE_TYPE (@2)))
4298 && wi::lt_p (wi::to_wide (@2), prec, TYPE_SIGN (TREE_TYPE (@2))))
4299 (with { unsigned int low = (tree_to_uhwi (@1)
4300 + tree_to_uhwi (@2)); }
4301 /* Deal with a OP (c1 + c2) being undefined but (a OP c1) OP c2
4302 being well defined. */
4304 (if (op == LROTATE_EXPR || op == RROTATE_EXPR)
4305 (op @0 { build_int_cst (TREE_TYPE (@1), low % prec); })
4306 (if (TYPE_UNSIGNED (type) || op == LSHIFT_EXPR)
4307 { build_zero_cst (type); }
4308 (op @0 { build_int_cst (TREE_TYPE (@1), prec - 1); })))
4309 (op @0 { build_int_cst (TREE_TYPE (@1), low); })))))))
4312 /* Simplify (CST << x) & 1 to 0 if CST is even or to x == 0 if it is odd. */
4314 (bit_and (lshift INTEGER_CST@1 @0) integer_onep)
4315 (if ((wi::to_wide (@1) & 1) != 0)
4316 (convert (eq:boolean_type_node @0 { build_zero_cst (TREE_TYPE (@0)); }))
4317 { build_zero_cst (type); }))
4319 /* Simplify ((C << x) & D) != 0 where C and D are power of two constants,
4320 either to false if D is smaller (unsigned comparison) than C, or to
4321 x == log2 (D) - log2 (C). Similarly for right shifts.
4322 Note for `(1 >> x)`, the & 1 has been removed so matching that seperately. */
4326 (cmp (bit_and (lshift integer_pow2p@1 @0) integer_pow2p@2) integer_zerop)
4327 (with { int c1 = wi::clz (wi::to_wide (@1));
4328 int c2 = wi::clz (wi::to_wide (@2)); }
4330 { constant_boolean_node (cmp == NE_EXPR ? false : true, type); }
4331 (icmp @0 { build_int_cst (TREE_TYPE (@0), c1 - c2); }))))
4333 (cmp (bit_and (rshift integer_pow2p@1 @0) integer_pow2p@2) integer_zerop)
4334 (if (tree_int_cst_sgn (@1) > 0)
4335 (with { int c1 = wi::clz (wi::to_wide (@1));
4336 int c2 = wi::clz (wi::to_wide (@2)); }
4338 { constant_boolean_node (cmp == NE_EXPR ? false : true, type); }
4339 (icmp @0 { build_int_cst (TREE_TYPE (@0), c2 - c1); })))))
4340 /* `(1 >> X) != 0` -> `X == 0` */
4341 /* `(1 >> X) == 0` -> `X != 0` */
4343 (cmp (rshift integer_onep@1 @0) integer_zerop)
4344 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1)))
4345 (icmp @0 { build_zero_cst (TREE_TYPE (@0)); }))))
4347 /* (CST1 << A) == CST2 -> A == ctz (CST2) - ctz (CST1)
4348 (CST1 << A) != CST2 -> A != ctz (CST2) - ctz (CST1)
4352 (cmp (lshift INTEGER_CST@0 @1) INTEGER_CST@2)
4353 (with { int cand = wi::ctz (wi::to_wide (@2)) - wi::ctz (wi::to_wide (@0)); }
4355 || (!integer_zerop (@2)
4356 && wi::lshift (wi::to_wide (@0), cand) != wi::to_wide (@2)))
4357 { constant_boolean_node (cmp == NE_EXPR, type); }
4358 (if (!integer_zerop (@2)
4359 && wi::lshift (wi::to_wide (@0), cand) == wi::to_wide (@2))
4360 (cmp @1 { build_int_cst (TREE_TYPE (@1), cand); }))))))
4362 /* Fold ((X << C1) & C2) cmp C3 into (X & (C2 >> C1)) cmp (C3 >> C1)
4363 ((X >> C1) & C2) cmp C3 into (X & (C2 << C1)) cmp (C3 << C1). */
4366 (cmp (bit_and:s (lshift:s @0 INTEGER_CST@1) INTEGER_CST@2) INTEGER_CST@3)
4367 (if (tree_fits_shwi_p (@1)
4368 && tree_to_shwi (@1) > 0
4369 && tree_to_shwi (@1) < TYPE_PRECISION (TREE_TYPE (@0)))
4370 (if (tree_to_shwi (@1) > wi::ctz (wi::to_wide (@3)))
4371 { constant_boolean_node (cmp == NE_EXPR, type); }
4372 (with { wide_int c1 = wi::to_wide (@1);
4373 wide_int c2 = wi::lrshift (wi::to_wide (@2), c1);
4374 wide_int c3 = wi::lrshift (wi::to_wide (@3), c1); }
4375 (cmp (bit_and @0 { wide_int_to_tree (TREE_TYPE (@0), c2); })
4376 { wide_int_to_tree (TREE_TYPE (@0), c3); })))))
4378 (cmp (bit_and:s (rshift:s @0 INTEGER_CST@1) INTEGER_CST@2) INTEGER_CST@3)
4379 (if (tree_fits_shwi_p (@1)
4380 && tree_to_shwi (@1) > 0
4381 && tree_to_shwi (@1) < TYPE_PRECISION (TREE_TYPE (@0)))
4382 (with { tree t0 = TREE_TYPE (@0);
4383 unsigned int prec = TYPE_PRECISION (t0);
4384 wide_int c1 = wi::to_wide (@1);
4385 wide_int c2 = wi::to_wide (@2);
4386 wide_int c3 = wi::to_wide (@3);
4387 wide_int sb = wi::set_bit_in_zero (prec - 1, prec); }
4388 (if ((c2 & c3) != c3)
4389 { constant_boolean_node (cmp == NE_EXPR, type); }
4390 (if (TYPE_UNSIGNED (t0))
4391 (if ((c3 & wi::arshift (sb, c1 - 1)) != 0)
4392 { constant_boolean_node (cmp == NE_EXPR, type); }
4393 (cmp (bit_and @0 { wide_int_to_tree (t0, c2 << c1); })
4394 { wide_int_to_tree (t0, c3 << c1); }))
4395 (with { wide_int smask = wi::arshift (sb, c1); }
4397 (if ((c2 & smask) == 0)
4398 (cmp (bit_and @0 { wide_int_to_tree (t0, c2 << c1); })
4399 { wide_int_to_tree (t0, c3 << c1); }))
4400 (if ((c3 & smask) == 0)
4401 (cmp (bit_and @0 { wide_int_to_tree (t0, (c2 << c1) | sb); })
4402 { wide_int_to_tree (t0, c3 << c1); }))
4403 (if ((c2 & smask) != (c3 & smask))
4404 { constant_boolean_node (cmp == NE_EXPR, type); })
4405 (cmp (bit_and @0 { wide_int_to_tree (t0, (c2 << c1) | sb); })
4406 { wide_int_to_tree (t0, (c3 << c1) | sb); })))))))))
4408 /* Fold (X << C1) & C2 into (X << C1) & (C2 | ((1 << C1) - 1))
4409 (X >> C1) & C2 into (X >> C1) & (C2 | ~((type) -1 >> C1))
4410 if the new mask might be further optimized. */
4411 (for shift (lshift rshift)
4413 (bit_and (convert?:s@4 (shift:s@5 (convert1?@3 @0) INTEGER_CST@1))
4415 (if (tree_nop_conversion_p (TREE_TYPE (@4), TREE_TYPE (@5))
4416 && TYPE_PRECISION (type) <= HOST_BITS_PER_WIDE_INT
4417 && tree_fits_uhwi_p (@1)
4418 && tree_to_uhwi (@1) > 0
4419 && tree_to_uhwi (@1) < TYPE_PRECISION (type))
4422 unsigned int shiftc = tree_to_uhwi (@1);
4423 unsigned HOST_WIDE_INT mask = TREE_INT_CST_LOW (@2);
4424 unsigned HOST_WIDE_INT newmask, zerobits = 0;
4425 tree shift_type = TREE_TYPE (@3);
4428 if (shift == LSHIFT_EXPR)
4429 zerobits = ((HOST_WIDE_INT_1U << shiftc) - 1);
4430 else if (shift == RSHIFT_EXPR
4431 && type_has_mode_precision_p (shift_type))
4433 prec = TYPE_PRECISION (TREE_TYPE (@3));
4435 /* See if more bits can be proven as zero because of
4438 && TYPE_UNSIGNED (TREE_TYPE (@0)))
4440 tree inner_type = TREE_TYPE (@0);
4441 if (type_has_mode_precision_p (inner_type)
4442 && TYPE_PRECISION (inner_type) < prec)
4444 prec = TYPE_PRECISION (inner_type);
4445 /* See if we can shorten the right shift. */
4447 shift_type = inner_type;
4448 /* Otherwise X >> C1 is all zeros, so we'll optimize
4449 it into (X, 0) later on by making sure zerobits
4453 zerobits = HOST_WIDE_INT_M1U;
4456 zerobits >>= HOST_BITS_PER_WIDE_INT - shiftc;
4457 zerobits <<= prec - shiftc;
4459 /* For arithmetic shift if sign bit could be set, zerobits
4460 can contain actually sign bits, so no transformation is
4461 possible, unless MASK masks them all away. In that
4462 case the shift needs to be converted into logical shift. */
4463 if (!TYPE_UNSIGNED (TREE_TYPE (@3))
4464 && prec == TYPE_PRECISION (TREE_TYPE (@3)))
4466 if ((mask & zerobits) == 0)
4467 shift_type = unsigned_type_for (TREE_TYPE (@3));
4473 /* ((X << 16) & 0xff00) is (X, 0). */
4474 (if ((mask & zerobits) == mask)
4475 { build_int_cst (type, 0); }
4476 (with { newmask = mask | zerobits; }
4477 (if (newmask != mask && (newmask & (newmask + 1)) == 0)
4480 /* Only do the transformation if NEWMASK is some integer
4482 for (prec = BITS_PER_UNIT;
4483 prec < HOST_BITS_PER_WIDE_INT; prec <<= 1)
4484 if (newmask == (HOST_WIDE_INT_1U << prec) - 1)
4487 (if (prec < HOST_BITS_PER_WIDE_INT
4488 || newmask == HOST_WIDE_INT_M1U)
4490 { tree newmaskt = build_int_cst_type (TREE_TYPE (@2), newmask); }
4491 (if (!tree_int_cst_equal (newmaskt, @2))
4492 (if (shift_type != TREE_TYPE (@3))
4493 (bit_and (convert (shift:shift_type (convert @3) @1)) { newmaskt; })
4494 (bit_and @4 { newmaskt; })))))))))))))
4496 /* ((1 << n) & M) != 0 -> n == log2 (M) */
4502 (nop_convert? (lshift integer_onep @0)) integer_pow2p@1) integer_zerop)
4503 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)))
4504 (icmp @0 { wide_int_to_tree (TREE_TYPE (@0),
4505 wi::exact_log2 (wi::to_wide (@1))); }))))
4507 /* Fold (X {&,^,|} C2) << C1 into (X << C1) {&,^,|} (C2 << C1)
4508 (X {&,^,|} C2) >> C1 into (X >> C1) & (C2 >> C1). */
4509 (for shift (lshift rshift)
4510 (for bit_op (bit_and bit_xor bit_ior)
4512 (shift (convert?:s (bit_op:s @0 INTEGER_CST@2)) INTEGER_CST@1)
4513 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
4514 (with { tree mask = int_const_binop (shift, fold_convert (type, @2), @1); }
4516 (bit_op (shift (convert @0) @1) { mask; })))))))
4518 /* ~(~X >> Y) -> X >> Y (for arithmetic shift). */
4520 (bit_not (convert1?:s (rshift:s (convert2?@0 (bit_not @1)) @2)))
4521 (if (!TYPE_UNSIGNED (TREE_TYPE (@0))
4522 && (element_precision (TREE_TYPE (@0))
4523 <= element_precision (TREE_TYPE (@1))
4524 || !TYPE_UNSIGNED (TREE_TYPE (@1))))
4526 { tree shift_type = TREE_TYPE (@0); }
4527 (convert (rshift (convert:shift_type @1) @2)))))
4529 /* ~(~X >>r Y) -> X >>r Y
4530 ~(~X <<r Y) -> X <<r Y */
4531 (for rotate (lrotate rrotate)
4533 (bit_not (convert1?:s (rotate:s (convert2?@0 (bit_not @1)) @2)))
4534 (if ((element_precision (TREE_TYPE (@0))
4535 <= element_precision (TREE_TYPE (@1))
4536 || !TYPE_UNSIGNED (TREE_TYPE (@1)))
4537 && (element_precision (type) <= element_precision (TREE_TYPE (@0))
4538 || !TYPE_UNSIGNED (TREE_TYPE (@0))))
4540 { tree rotate_type = TREE_TYPE (@0); }
4541 (convert (rotate (convert:rotate_type @1) @2))))))
4544 (for rotate (lrotate rrotate)
4545 invrot (rrotate lrotate)
4546 /* (X >>r Y) cmp (Z >>r Y) may simplify to X cmp Y. */
4548 (cmp (rotate @1 @0) (rotate @2 @0))
4550 /* (X >>r C1) cmp C2 may simplify to X cmp C3. */
4552 (cmp (rotate @0 INTEGER_CST@1) INTEGER_CST@2)
4553 (cmp @0 { const_binop (invrot, TREE_TYPE (@0), @2, @1); }))
4554 /* (X >>r Y) cmp C where C is 0 or ~0, may simplify to X cmp C. */
4556 (cmp (rotate @0 @1) INTEGER_CST@2)
4557 (if (integer_zerop (@2) || integer_all_onesp (@2))
4560 /* Narrow a lshift by constant. */
4562 (convert (lshift:s@0 @1 INTEGER_CST@2))
4563 (if (INTEGRAL_TYPE_P (type)
4564 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
4565 && !integer_zerop (@2)
4566 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0)))
4567 (if (TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (@0))
4568 || wi::ltu_p (wi::to_wide (@2), TYPE_PRECISION (type)))
4569 (lshift (convert @1) @2)
4570 (if (wi::ltu_p (wi::to_wide (@2), TYPE_PRECISION (TREE_TYPE (@0))))
4571 { build_zero_cst (type); }))))
4573 /* Simplifications of conversions. */
4575 /* Basic strip-useless-type-conversions / strip_nops. */
4576 (for cvt (convert view_convert float fix_trunc)
4579 (if ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@0)))
4580 || (GENERIC && type == TREE_TYPE (@0)))
4583 /* Contract view-conversions. */
4585 (view_convert (view_convert @0))
4588 /* For integral conversions with the same precision or pointer
4589 conversions use a NOP_EXPR instead. */
4592 (if ((INTEGRAL_TYPE_P (type) || POINTER_TYPE_P (type))
4593 && (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
4594 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (@0)))
4597 /* Strip inner integral conversions that do not change precision or size, or
4598 zero-extend while keeping the same size (for bool-to-char). */
4600 (view_convert (convert@0 @1))
4601 (if ((INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
4602 && (INTEGRAL_TYPE_P (TREE_TYPE (@1)) || POINTER_TYPE_P (TREE_TYPE (@1)))
4603 && TYPE_SIZE (TREE_TYPE (@0)) == TYPE_SIZE (TREE_TYPE (@1))
4604 && (TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1))
4605 || (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (TREE_TYPE (@1))
4606 && TYPE_UNSIGNED (TREE_TYPE (@1)))))
4609 /* Simplify a view-converted empty or single-element constructor. */
4611 (view_convert CONSTRUCTOR@0)
4613 { tree ctor = (TREE_CODE (@0) == SSA_NAME
4614 ? gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0)) : @0); }
4616 (if (CONSTRUCTOR_NELTS (ctor) == 0)
4617 { build_zero_cst (type); })
4618 (if (CONSTRUCTOR_NELTS (ctor) == 1
4619 && VECTOR_TYPE_P (TREE_TYPE (ctor))
4620 && operand_equal_p (TYPE_SIZE (type),
4621 TYPE_SIZE (TREE_TYPE
4622 (CONSTRUCTOR_ELT (ctor, 0)->value))))
4623 (view_convert { CONSTRUCTOR_ELT (ctor, 0)->value; })))))
4625 /* Re-association barriers around constants and other re-association
4626 barriers can be removed. */
4628 (paren CONSTANT_CLASS_P@0)
4631 (paren (paren@1 @0))
4634 /* Handle cases of two conversions in a row. */
4635 (for ocvt (convert float fix_trunc)
4636 (for icvt (convert float)
4641 tree inside_type = TREE_TYPE (@0);
4642 tree inter_type = TREE_TYPE (@1);
4643 int inside_int = INTEGRAL_TYPE_P (inside_type);
4644 int inside_ptr = POINTER_TYPE_P (inside_type);
4645 int inside_float = FLOAT_TYPE_P (inside_type);
4646 int inside_vec = VECTOR_TYPE_P (inside_type);
4647 unsigned int inside_prec = element_precision (inside_type);
4648 int inside_unsignedp = TYPE_UNSIGNED (inside_type);
4649 int inter_int = INTEGRAL_TYPE_P (inter_type);
4650 int inter_ptr = POINTER_TYPE_P (inter_type);
4651 int inter_float = FLOAT_TYPE_P (inter_type);
4652 int inter_vec = VECTOR_TYPE_P (inter_type);
4653 unsigned int inter_prec = element_precision (inter_type);
4654 int inter_unsignedp = TYPE_UNSIGNED (inter_type);
4655 int final_int = INTEGRAL_TYPE_P (type);
4656 int final_ptr = POINTER_TYPE_P (type);
4657 int final_float = FLOAT_TYPE_P (type);
4658 int final_vec = VECTOR_TYPE_P (type);
4659 unsigned int final_prec = element_precision (type);
4660 int final_unsignedp = TYPE_UNSIGNED (type);
4663 /* In addition to the cases of two conversions in a row
4664 handled below, if we are converting something to its own
4665 type via an object of identical or wider precision, neither
4666 conversion is needed. */
4667 (if (((GIMPLE && useless_type_conversion_p (type, inside_type))
4669 && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (inside_type)))
4670 && (((inter_int || inter_ptr) && final_int)
4671 || (inter_float && final_float))
4672 && inter_prec >= final_prec)
4675 /* Likewise, if the intermediate and initial types are either both
4676 float or both integer, we don't need the middle conversion if the
4677 former is wider than the latter and doesn't change the signedness
4678 (for integers). Avoid this if the final type is a pointer since
4679 then we sometimes need the middle conversion. */
4680 (if (((inter_int && inside_int) || (inter_float && inside_float))
4681 && (final_int || final_float)
4682 && inter_prec >= inside_prec
4683 && (inter_float || inter_unsignedp == inside_unsignedp))
4686 /* If we have a sign-extension of a zero-extended value, we can
4687 replace that by a single zero-extension. Likewise if the
4688 final conversion does not change precision we can drop the
4689 intermediate conversion. */
4690 (if (inside_int && inter_int && final_int
4691 && ((inside_prec < inter_prec && inter_prec < final_prec
4692 && inside_unsignedp && !inter_unsignedp)
4693 || final_prec == inter_prec))
4696 /* Two conversions in a row are not needed unless:
4697 - some conversion is floating-point (overstrict for now), or
4698 - some conversion is a vector (overstrict for now), or
4699 - the intermediate type is narrower than both initial and
4701 - the intermediate type and innermost type differ in signedness,
4702 and the outermost type is wider than the intermediate, or
4703 - the initial type is a pointer type and the precisions of the
4704 intermediate and final types differ, or
4705 - the final type is a pointer type and the precisions of the
4706 initial and intermediate types differ. */
4707 (if (! inside_float && ! inter_float && ! final_float
4708 && ! inside_vec && ! inter_vec && ! final_vec
4709 && (inter_prec >= inside_prec || inter_prec >= final_prec)
4710 && ! (inside_int && inter_int
4711 && inter_unsignedp != inside_unsignedp
4712 && inter_prec < final_prec)
4713 && ((inter_unsignedp && inter_prec > inside_prec)
4714 == (final_unsignedp && final_prec > inter_prec))
4715 && ! (inside_ptr && inter_prec != final_prec)
4716 && ! (final_ptr && inside_prec != inter_prec))
4719 /* `(outer:M)(inter:N) a:O`
4720 can be converted to `(outer:M) a`
4721 if M <= O && N >= O. No matter what signedness of the casts,
4722 as the final is either a truncation from the original or just
4723 a sign change of the type. */
4724 (if (inside_int && inter_int && final_int
4725 && final_prec <= inside_prec
4726 && inter_prec >= inside_prec)
4729 /* A truncation to an unsigned type (a zero-extension) should be
4730 canonicalized as bitwise and of a mask. */
4731 (if (GIMPLE /* PR70366: doing this in GENERIC breaks -Wconversion. */
4732 && final_int && inter_int && inside_int
4733 && final_prec == inside_prec
4734 && final_prec > inter_prec
4736 (convert (bit_and @0 { wide_int_to_tree
4738 wi::mask (inter_prec, false,
4739 TYPE_PRECISION (inside_type))); })))
4741 /* If we are converting an integer to a floating-point that can
4742 represent it exactly and back to an integer, we can skip the
4743 floating-point conversion. */
4744 (if (GIMPLE /* PR66211 */
4745 && inside_int && inter_float && final_int &&
4746 (unsigned) significand_size (TYPE_MODE (inter_type))
4747 >= inside_prec - !inside_unsignedp)
4750 /* (float_type)(integer_type) x -> trunc (x) if the type of x matches
4751 float_type. Only do the transformation if we do not need to preserve
4752 trapping behaviour, so require !flag_trapping_math. */
4755 (float (fix_trunc @0))
4756 (if (!flag_trapping_math
4757 && types_match (type, TREE_TYPE (@0))
4758 && direct_internal_fn_supported_p (IFN_TRUNC, type,
4763 /* If we have a narrowing conversion to an integral type that is fed by a
4764 BIT_AND_EXPR, we might be able to remove the BIT_AND_EXPR if it merely
4765 masks off bits outside the final type (and nothing else). */
4767 (convert (bit_and @0 INTEGER_CST@1))
4768 (if (INTEGRAL_TYPE_P (type)
4769 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
4770 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))
4771 && operand_equal_p (@1, build_low_bits_mask (TREE_TYPE (@1),
4772 TYPE_PRECISION (type)), 0))
4776 /* (X /[ex] A) * A -> X. */
4778 (mult (convert1? (exact_div @0 @@1)) (convert2? @1))
4781 /* Simplify (A / B) * B + (A % B) -> A. */
4782 (for div (trunc_div ceil_div floor_div round_div)
4783 mod (trunc_mod ceil_mod floor_mod round_mod)
4785 (plus:c (mult:c (div @0 @1) @1) (mod @0 @1))
4788 /* x / y * y == x -> x % y == 0. */
4790 (eq:c (mult:c (trunc_div:s @0 @1) @1) @0)
4791 (if (TREE_CODE (TREE_TYPE (@0)) != COMPLEX_TYPE)
4792 (eq (trunc_mod @0 @1) { build_zero_cst (TREE_TYPE (@0)); })))
4794 /* ((X /[ex] A) +- B) * A --> X +- A * B. */
4795 (for op (plus minus)
4797 (mult (convert1? (op (convert2? (exact_div @0 INTEGER_CST@@1)) INTEGER_CST@2)) @1)
4798 (if (tree_nop_conversion_p (type, TREE_TYPE (@2))
4799 && tree_nop_conversion_p (TREE_TYPE (@0), TREE_TYPE (@2)))
4802 wi::overflow_type overflow;
4803 wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@2),
4804 TYPE_SIGN (type), &overflow);
4806 (if (types_match (type, TREE_TYPE (@2))
4807 && types_match (TREE_TYPE (@0), TREE_TYPE (@2)) && !overflow)
4808 (op @0 { wide_int_to_tree (type, mul); })
4809 (with { tree utype = unsigned_type_for (type); }
4810 (convert (op (convert:utype @0)
4811 (mult (convert:utype @1) (convert:utype @2))))))))))
4813 /* Canonicalization of binary operations. */
4815 /* Convert X + -C into X - C. */
4817 (plus @0 REAL_CST@1)
4818 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
4819 (with { tree tem = const_unop (NEGATE_EXPR, type, @1); }
4820 (if (!TREE_OVERFLOW (tem) || !flag_trapping_math)
4821 (minus @0 { tem; })))))
4823 /* Convert x+x into x*2. */
4826 (if (SCALAR_FLOAT_TYPE_P (type))
4827 (mult @0 { build_real (type, dconst2); })
4828 (if (INTEGRAL_TYPE_P (type))
4829 (mult @0 { build_int_cst (type, 2); }))))
4833 (minus integer_zerop @1)
4836 (pointer_diff integer_zerop @1)
4837 (negate (convert @1)))
4839 /* (ARG0 - ARG1) is the same as (-ARG1 + ARG0). So check whether
4840 ARG0 is zero and X + ARG0 reduces to X, since that would mean
4841 (-ARG1 + ARG0) reduces to -ARG1. */
4843 (minus real_zerop@0 @1)
4844 (if (fold_real_zero_addition_p (type, @1, @0, 0))
4847 /* Transform x * -1 into -x. */
4849 (mult @0 integer_minus_onep)
4852 /* Reassociate (X * CST) * Y to (X * Y) * CST. This does not introduce
4853 signed overflow for CST != 0 && CST != -1. */
4855 (mult:c (mult:s@3 @0 INTEGER_CST@1) @2)
4856 (if (TREE_CODE (@2) != INTEGER_CST
4858 && !integer_zerop (@1) && !integer_minus_onep (@1))
4859 (mult (mult @0 @2) @1)))
4861 /* True if we can easily extract the real and imaginary parts of a complex
4863 (match compositional_complex
4864 (convert? (complex @0 @1)))
4866 /* COMPLEX_EXPR and REALPART/IMAGPART_EXPR cancellations. */
4868 (complex (realpart @0) (imagpart @0))
4871 (realpart (complex @0 @1))
4874 (imagpart (complex @0 @1))
4877 /* Sometimes we only care about half of a complex expression. */
4879 (realpart (convert?:s (conj:s @0)))
4880 (convert (realpart @0)))
4882 (imagpart (convert?:s (conj:s @0)))
4883 (convert (negate (imagpart @0))))
4884 (for part (realpart imagpart)
4885 (for op (plus minus)
4887 (part (convert?:s@2 (op:s @0 @1)))
4888 (convert (op (part @0) (part @1))))))
4890 (realpart (convert?:s (CEXPI:s @0)))
4893 (imagpart (convert?:s (CEXPI:s @0)))
4896 /* conj(conj(x)) -> x */
4898 (conj (convert? (conj @0)))
4899 (if (tree_nop_conversion_p (TREE_TYPE (@0), type))
4902 /* conj({x,y}) -> {x,-y} */
4904 (conj (convert?:s (complex:s @0 @1)))
4905 (with { tree itype = TREE_TYPE (type); }
4906 (complex (convert:itype @0) (negate (convert:itype @1)))))
4908 /* BSWAP simplifications, transforms checked by gcc.dg/builtin-bswap-8.c. */
4914 (bswap (bit_not (bswap @0)))
4916 (for bitop (bit_xor bit_ior bit_and)
4918 (bswap (bitop:c (bswap @0) @1))
4919 (bitop @0 (bswap @1))))
4922 (cmp (bswap@2 @0) (bswap @1))
4923 (with { tree ctype = TREE_TYPE (@2); }
4924 (cmp (convert:ctype @0) (convert:ctype @1))))
4926 (cmp (bswap @0) INTEGER_CST@1)
4927 (with { tree ctype = TREE_TYPE (@1); }
4928 (cmp (convert:ctype @0) (bswap! @1)))))
4929 /* (bswap(x) >> C1) & C2 can sometimes be simplified to (x >> C3) & C2. */
4931 (bit_and (convert1? (rshift@0 (convert2? (bswap@4 @1)) INTEGER_CST@2))
4933 (if (BITS_PER_UNIT == 8
4934 && tree_fits_uhwi_p (@2)
4935 && tree_fits_uhwi_p (@3))
4938 unsigned HOST_WIDE_INT prec = TYPE_PRECISION (TREE_TYPE (@4));
4939 unsigned HOST_WIDE_INT bits = tree_to_uhwi (@2);
4940 unsigned HOST_WIDE_INT mask = tree_to_uhwi (@3);
4941 unsigned HOST_WIDE_INT lo = bits & 7;
4942 unsigned HOST_WIDE_INT hi = bits - lo;
4945 && mask < (256u>>lo)
4946 && bits < TYPE_PRECISION (TREE_TYPE(@0)))
4947 (with { unsigned HOST_WIDE_INT ns = (prec - (hi + 8)) + lo; }
4949 (bit_and (convert @1) @3)
4952 tree utype = unsigned_type_for (TREE_TYPE (@1));
4953 tree nst = build_int_cst (integer_type_node, ns);
4955 (bit_and (convert (rshift:utype (convert:utype @1) {nst;})) @3))))))))
4956 /* bswap(x) >> C1 can sometimes be simplified to (T)x >> C2. */
4958 (rshift (convert? (bswap@2 @0)) INTEGER_CST@1)
4959 (if (BITS_PER_UNIT == 8
4960 && CHAR_TYPE_SIZE == 8
4961 && tree_fits_uhwi_p (@1))
4964 unsigned HOST_WIDE_INT prec = TYPE_PRECISION (TREE_TYPE (@2));
4965 unsigned HOST_WIDE_INT bits = tree_to_uhwi (@1);
4966 /* If the bswap was extended before the original shift, this
4967 byte (shift) has the sign of the extension, not the sign of
4968 the original shift. */
4969 tree st = TYPE_PRECISION (type) > prec ? TREE_TYPE (@2) : type;
4971 /* Special case: logical right shift of sign-extended bswap.
4972 (unsigned)(short)bswap16(x)>>12 is (unsigned)((short)x<<8)>>12. */
4973 (if (TYPE_PRECISION (type) > prec
4974 && !TYPE_UNSIGNED (TREE_TYPE (@2))
4975 && TYPE_UNSIGNED (type)
4976 && bits < prec && bits + 8 >= prec)
4977 (with { tree nst = build_int_cst (integer_type_node, prec - 8); }
4978 (rshift (convert (lshift:st (convert:st @0) {nst;})) @1))
4979 (if (bits + 8 == prec)
4980 (if (TYPE_UNSIGNED (st))
4981 (convert (convert:unsigned_char_type_node @0))
4982 (convert (convert:signed_char_type_node @0)))
4983 (if (bits < prec && bits + 8 > prec)
4986 tree nst = build_int_cst (integer_type_node, bits & 7);
4987 tree bt = TYPE_UNSIGNED (st) ? unsigned_char_type_node
4988 : signed_char_type_node;
4990 (convert (rshift:bt (convert:bt @0) {nst;})))))))))
4991 /* bswap(x) & C1 can sometimes be simplified to (x >> C2) & C1. */
4993 (bit_and (convert? (bswap@2 @0)) INTEGER_CST@1)
4994 (if (BITS_PER_UNIT == 8
4995 && tree_fits_uhwi_p (@1)
4996 && tree_to_uhwi (@1) < 256)
4999 unsigned HOST_WIDE_INT prec = TYPE_PRECISION (TREE_TYPE (@2));
5000 tree utype = unsigned_type_for (TREE_TYPE (@0));
5001 tree nst = build_int_cst (integer_type_node, prec - 8);
5003 (bit_and (convert (rshift:utype (convert:utype @0) {nst;})) @1)))))
5006 /* Combine COND_EXPRs and VEC_COND_EXPRs. */
5008 /* Simplify constant conditions.
5009 Only optimize constant conditions when the selected branch
5010 has the same type as the COND_EXPR. This avoids optimizing
5011 away "c ? x : throw", where the throw has a void type.
5012 Note that we cannot throw away the fold-const.cc variant nor
5013 this one as we depend on doing this transform before possibly
5014 A ? B : B -> B triggers and the fold-const.cc one can optimize
5015 0 ? A : B to B even if A has side-effects. Something
5016 genmatch cannot handle. */
5018 (cond INTEGER_CST@0 @1 @2)
5019 (if (integer_zerop (@0))
5020 (if (!VOID_TYPE_P (TREE_TYPE (@2)) || VOID_TYPE_P (type))
5022 (if (!VOID_TYPE_P (TREE_TYPE (@1)) || VOID_TYPE_P (type))
5025 (vec_cond VECTOR_CST@0 @1 @2)
5026 (if (integer_all_onesp (@0))
5028 (if (integer_zerop (@0))
5031 /* Sink unary operations to branches, but only if we do fold both. */
5032 (for op (negate bit_not abs absu)
5034 (op (vec_cond:s @0 @1 @2))
5035 (vec_cond @0 (op! @1) (op! @2))))
5037 /* Sink unary conversions to branches, but only if we do fold both
5038 and the target's truth type is the same as we already have. */
5040 (convert (vec_cond:s @0 @1 @2))
5041 (if (VECTOR_TYPE_P (type)
5042 && types_match (TREE_TYPE (@0), truth_type_for (type)))
5043 (vec_cond @0 (convert! @1) (convert! @2))))
5045 /* Likewise for view_convert of nop_conversions. */
5047 (view_convert (vec_cond:s @0 @1 @2))
5048 (if (VECTOR_TYPE_P (type) && VECTOR_TYPE_P (TREE_TYPE (@1))
5049 && known_eq (TYPE_VECTOR_SUBPARTS (type),
5050 TYPE_VECTOR_SUBPARTS (TREE_TYPE (@1)))
5051 && tree_nop_conversion_p (TREE_TYPE (type), TREE_TYPE (TREE_TYPE (@1))))
5052 (vec_cond @0 (view_convert! @1) (view_convert! @2))))
5054 /* Sink binary operation to branches, but only if we can fold it. */
5055 (for op (tcc_comparison plus minus mult bit_and bit_ior bit_xor
5056 lshift rshift rdiv trunc_div ceil_div floor_div round_div
5057 trunc_mod ceil_mod floor_mod round_mod min max)
5058 /* (c ? a : b) op (c ? d : e) --> c ? (a op d) : (b op e) */
5060 (op (vec_cond:s @0 @1 @2) (vec_cond:s @0 @3 @4))
5061 (vec_cond @0 (op! @1 @3) (op! @2 @4)))
5063 /* (c ? a : b) op d --> c ? (a op d) : (b op d) */
5065 (op (vec_cond:s @0 @1 @2) @3)
5066 (vec_cond @0 (op! @1 @3) (op! @2 @3)))
5068 (op @3 (vec_cond:s @0 @1 @2))
5069 (vec_cond @0 (op! @3 @1) (op! @3 @2))))
5072 (match (nop_atomic_bit_test_and_p @0 @1 @4)
5073 (bit_and (convert?@4 (ATOMIC_FETCH_OR_XOR_N @2 INTEGER_CST@0 @3))
5076 int ibit = tree_log2 (@0);
5077 int ibit2 = tree_log2 (@1);
5081 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))))))
5083 (match (nop_atomic_bit_test_and_p @0 @1 @3)
5084 (bit_and (convert?@3 (SYNC_FETCH_OR_XOR_N @2 INTEGER_CST@0))
5087 int ibit = tree_log2 (@0);
5088 int ibit2 = tree_log2 (@1);
5092 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))))))
5094 (match (nop_atomic_bit_test_and_p @0 @0 @4)
5097 (ATOMIC_FETCH_OR_XOR_N @2 (nop_convert? (lshift@0 integer_onep@5 @6)) @3))
5099 (if (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0)))))
5101 (match (nop_atomic_bit_test_and_p @0 @0 @4)
5104 (SYNC_FETCH_OR_XOR_N @2 (nop_convert? (lshift@0 integer_onep@3 @5))))
5106 (if (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0)))))
5108 (match (nop_atomic_bit_test_and_p @0 @1 @3)
5109 (bit_and@4 (convert?@3 (ATOMIC_FETCH_AND_N @2 INTEGER_CST@0 @5))
5112 int ibit = wi::exact_log2 (wi::zext (wi::bit_not (wi::to_wide (@0)),
5113 TYPE_PRECISION(type)));
5114 int ibit2 = tree_log2 (@1);
5118 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))))))
5120 (match (nop_atomic_bit_test_and_p @0 @1 @3)
5122 (convert?@3 (SYNC_FETCH_AND_AND_N @2 INTEGER_CST@0))
5125 int ibit = wi::exact_log2 (wi::zext (wi::bit_not (wi::to_wide (@0)),
5126 TYPE_PRECISION(type)));
5127 int ibit2 = tree_log2 (@1);
5131 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))))))
5133 (match (nop_atomic_bit_test_and_p @4 @0 @3)
5136 (ATOMIC_FETCH_AND_N @2 (nop_convert?@4 (bit_not (lshift@0 integer_onep@6 @7))) @5))
5138 (if (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@4)))))
5140 (match (nop_atomic_bit_test_and_p @4 @0 @3)
5143 (SYNC_FETCH_AND_AND_N @2 (nop_convert?@4 (bit_not (lshift@0 integer_onep@6 @7)))))
5145 (if (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@4)))))
5149 /* (v ? w : 0) ? a : b is just (v & w) ? a : b
5150 Currently disabled after pass lvec because ARM understands
5151 VEC_COND_EXPR<v==w,-1,0> but not a plain v==w fed to BIT_IOR_EXPR. */
5153 /* These can only be done in gimple as fold likes to convert:
5154 (CMP) & N into (CMP) ? N : 0
5155 and we try to match the same pattern again and again. */
5157 (vec_cond (vec_cond:s @0 @3 integer_zerop) @1 @2)
5158 (if (optimize_vectors_before_lowering_p () && types_match (@0, @3))
5159 (vec_cond (bit_and @0 @3) @1 @2)))
5161 (vec_cond (vec_cond:s @0 integer_all_onesp @3) @1 @2)
5162 (if (optimize_vectors_before_lowering_p () && types_match (@0, @3))
5163 (vec_cond (bit_ior @0 @3) @1 @2)))
5165 (vec_cond (vec_cond:s @0 integer_zerop @3) @1 @2)
5166 (if (optimize_vectors_before_lowering_p () && types_match (@0, @3))
5167 (vec_cond (bit_ior @0 (bit_not @3)) @2 @1)))
5169 (vec_cond (vec_cond:s @0 @3 integer_all_onesp) @1 @2)
5170 (if (optimize_vectors_before_lowering_p () && types_match (@0, @3))
5171 (vec_cond (bit_and @0 (bit_not @3)) @2 @1)))
5173 /* c1 ? c2 ? a : b : b --> (c1 & c2) ? a : b */
5175 (vec_cond @0 (vec_cond:s @1 @2 @3) @3)
5176 (if (optimize_vectors_before_lowering_p () && types_match (@0, @1))
5177 (vec_cond (bit_and @0 @1) @2 @3)))
5179 (vec_cond @0 @2 (vec_cond:s @1 @2 @3))
5180 (if (optimize_vectors_before_lowering_p () && types_match (@0, @1))
5181 (vec_cond (bit_ior @0 @1) @2 @3)))
5183 (vec_cond @0 (vec_cond:s @1 @2 @3) @2)
5184 (if (optimize_vectors_before_lowering_p () && types_match (@0, @1))
5185 (vec_cond (bit_ior (bit_not @0) @1) @2 @3)))
5187 (vec_cond @0 @3 (vec_cond:s @1 @2 @3))
5188 (if (optimize_vectors_before_lowering_p () && types_match (@0, @1))
5189 (vec_cond (bit_and (bit_not @0) @1) @2 @3)))
5192 /* Canonicalize mask ? { 0, ... } : { -1, ...} to ~mask if the mask
5193 types are compatible. */
5195 (vec_cond @0 VECTOR_CST@1 VECTOR_CST@2)
5196 (if (VECTOR_BOOLEAN_TYPE_P (type)
5197 && types_match (type, TREE_TYPE (@0)))
5198 (if (integer_zerop (@1) && integer_all_onesp (@2))
5200 (if (integer_all_onesp (@1) && integer_zerop (@2))
5203 /* A few simplifications of "a ? CST1 : CST2". */
5204 /* NOTE: Only do this on gimple as the if-chain-to-switch
5205 optimization depends on the gimple to have if statements in it. */
5208 (cond @0 INTEGER_CST@1 INTEGER_CST@2)
5210 (if (integer_zerop (@2))
5212 /* a ? 1 : 0 -> a if 0 and 1 are integral types. */
5213 (if (integer_onep (@1))
5214 (convert (convert:boolean_type_node @0)))
5215 /* a ? -1 : 0 -> -a. */
5216 (if (INTEGRAL_TYPE_P (type) && integer_all_onesp (@1))
5217 (if (TYPE_PRECISION (type) == 1)
5218 /* For signed 1-bit precision just cast bool to the type. */
5219 (convert (convert:boolean_type_node @0))
5220 (if (TREE_CODE (type) == BOOLEAN_TYPE)
5222 tree intt = build_nonstandard_integer_type (TYPE_PRECISION (type),
5223 TYPE_UNSIGNED (type));
5225 (convert (negate (convert:intt (convert:boolean_type_node @0)))))
5226 (negate (convert:type (convert:boolean_type_node @0))))))
5227 /* a ? powerof2cst : 0 -> a << (log2(powerof2cst)) */
5228 (if (INTEGRAL_TYPE_P (type) && integer_pow2p (@1))
5230 tree shift = build_int_cst (integer_type_node, tree_log2 (@1));
5232 (lshift (convert (convert:boolean_type_node @0)) { shift; })))))
5233 (if (integer_zerop (@1))
5235 /* a ? 0 : 1 -> !a. */
5236 (if (integer_onep (@2))
5237 (convert (bit_xor (convert:boolean_type_node @0) { boolean_true_node; })))
5238 /* a ? 0 : -1 -> -(!a). */
5239 (if (INTEGRAL_TYPE_P (type) && integer_all_onesp (@2))
5240 (if (TYPE_PRECISION (type) == 1)
5241 /* For signed 1-bit precision just cast bool to the type. */
5242 (convert (bit_xor (convert:boolean_type_node @0) { boolean_true_node; }))
5243 (if (TREE_CODE (type) == BOOLEAN_TYPE)
5245 tree intt = build_nonstandard_integer_type (TYPE_PRECISION (type),
5246 TYPE_UNSIGNED (type));
5248 (convert (negate (convert:intt (bit_xor (convert:boolean_type_node @0)
5249 { boolean_true_node; })))))
5250 (negate (convert:type (bit_xor (convert:boolean_type_node @0)
5251 { boolean_true_node; }))))))
5252 /* a ? 0 : powerof2cst -> (!a) << (log2(powerof2cst)) */
5253 (if (INTEGRAL_TYPE_P (type) && integer_pow2p (@2))
5255 tree shift = build_int_cst (integer_type_node, tree_log2 (@2));
5257 (lshift (convert (bit_xor (convert:boolean_type_node @0)
5258 { boolean_true_node; })) { shift; })))))))
5260 /* (a > 1) ? 0 : (cast)a is the same as (cast)(a == 1)
5261 for unsigned types. */
5263 (cond (gt @0 integer_onep@1) integer_zerop (convert? @2))
5264 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
5265 && bitwise_equal_p (@0, @2))
5266 (convert (eq @0 @1))
5270 /* (a <= 1) & (cast)a is the same as (cast)(a == 1)
5271 for unsigned types. */
5273 (bit_and:c (convert1? (le @0 integer_onep@1)) (convert2? @2))
5274 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
5275 && bitwise_equal_p (@0, @2))
5276 (convert (eq @0 @1))
5280 /* `(a == CST) & a` can be simplified to `0` or `(a == CST)` depending
5281 on the first bit of the CST. */
5283 (bit_and:c (convert@2 (eq @0 INTEGER_CST@1)) (convert? @0))
5284 (if ((wi::to_wide (@1) & 1) != 0)
5286 { build_zero_cst (type); }))
5289 # x_5 in range [cst1, cst2] where cst2 = cst1 + 1
5290 x_5 == cstN ? cst4 : cst3
5291 # op is == or != and N is 1 or 2
5292 to r_6 = x_5 + (min (cst3, cst4) - cst1) or
5293 r_6 = (min (cst3, cst4) + cst1) - x_5 depending on op, N and which
5294 of cst3 and cst4 is smaller.
5295 This was originally done by two_value_replacement in phiopt (PR 88676). */
5298 (cond (eqne SSA_NAME@0 INTEGER_CST@1) INTEGER_CST@2 INTEGER_CST@3)
5299 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
5300 && INTEGRAL_TYPE_P (type)
5301 && (wi::to_widest (@2) + 1 == wi::to_widest (@3)
5302 || wi::to_widest (@2) == wi::to_widest (@3) + 1))
5305 get_range_query (cfun)->range_of_expr (r, @0);
5306 if (r.undefined_p ())
5307 r.set_varying (TREE_TYPE (@0));
5309 wide_int min = r.lower_bound ();
5310 wide_int max = r.upper_bound ();
5313 && (wi::to_wide (@1) == min
5314 || wi::to_wide (@1) == max))
5316 tree arg0 = @2, arg1 = @3;
5318 if ((eqne == EQ_EXPR) ^ (wi::to_wide (@1) == min))
5319 std::swap (arg0, arg1);
5320 if (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type))
5321 type1 = TREE_TYPE (@0);
5324 auto prec = TYPE_PRECISION (type1);
5325 auto unsign = TYPE_UNSIGNED (type1);
5326 if (TREE_CODE (type1) == BOOLEAN_TYPE)
5327 type1 = build_nonstandard_integer_type (prec, unsign);
5328 min = wide_int::from (min, prec,
5329 TYPE_SIGN (TREE_TYPE (@0)));
5330 wide_int a = wide_int::from (wi::to_wide (arg0), prec,
5332 enum tree_code code;
5333 wi::overflow_type ovf;
5334 if (tree_int_cst_lt (arg0, arg1))
5340 /* lhs is known to be in range [min, min+1] and we want to add a
5341 to it. Check if that operation can overflow for those 2 values
5342 and if yes, force unsigned type. */
5343 wi::add (min + (wi::neg_p (a) ? 0 : 1), a, SIGNED, &ovf);
5345 type1 = unsigned_type_for (type1);
5354 /* lhs is known to be in range [min, min+1] and we want to subtract
5355 it from a. Check if that operation can overflow for those 2
5356 values and if yes, force unsigned type. */
5357 wi::sub (a, min + (wi::neg_p (min) ? 0 : 1), SIGNED, &ovf);
5359 type1 = unsigned_type_for (type1);
5362 tree arg = wide_int_to_tree (type1, a);
5364 (if (code == PLUS_EXPR)
5365 (convert (plus (convert:type1 @0) { arg; }))
5366 (convert (minus { arg; } (convert:type1 @0))))))))))
5370 (convert (cond@0 @1 INTEGER_CST@2 INTEGER_CST@3))
5371 (if (INTEGRAL_TYPE_P (type)
5372 && INTEGRAL_TYPE_P (TREE_TYPE (@0)))
5373 (cond @1 (convert @2) (convert @3))))
5375 /* Simplification moved from fold_cond_expr_with_comparison. It may also
5377 /* This pattern implements two kinds simplification:
5380 (cond (cmp (convert1? x) c1) (convert2? x) c2) -> (minmax (x c)) if:
5381 1) Conversions are type widening from smaller type.
5382 2) Const c1 equals to c2 after canonicalizing comparison.
5383 3) Comparison has tree code LT, LE, GT or GE.
5384 This specific pattern is needed when (cmp (convert x) c) may not
5385 be simplified by comparison patterns because of multiple uses of
5386 x. It also makes sense here because simplifying across multiple
5387 referred var is always benefitial for complicated cases.
5390 (cond (eq (convert1? x) c1) (convert2? x) c2) -> (cond (eq x c1) c1 c2). */
5391 (for cmp (lt le gt ge eq ne)
5393 (cond (cmp (convert1? @1) INTEGER_CST@3) (convert2? @1) INTEGER_CST@2)
5396 tree from_type = TREE_TYPE (@1);
5397 tree c1_type = TREE_TYPE (@3), c2_type = TREE_TYPE (@2);
5398 enum tree_code code = ERROR_MARK;
5400 if (INTEGRAL_TYPE_P (from_type)
5401 && int_fits_type_p (@2, from_type)
5402 && (types_match (c1_type, from_type)
5403 || (TYPE_PRECISION (c1_type) > TYPE_PRECISION (from_type)
5404 && (TYPE_UNSIGNED (from_type)
5405 || TYPE_SIGN (c1_type) == TYPE_SIGN (from_type))))
5406 && (types_match (c2_type, from_type)
5407 || (TYPE_PRECISION (c2_type) > TYPE_PRECISION (from_type)
5408 && (TYPE_UNSIGNED (from_type)
5409 || TYPE_SIGN (c2_type) == TYPE_SIGN (from_type)))))
5412 code = minmax_from_comparison (cmp, @1, @3, @1, @2);
5413 /* Can do A == C1 ? A : C2 -> A == C1 ? C1 : C2? */
5414 else if (int_fits_type_p (@3, from_type))
5418 (if (code == MAX_EXPR)
5419 (convert (max @1 (convert @2)))
5420 (if (code == MIN_EXPR)
5421 (convert (min @1 (convert @2)))
5422 (if (code == EQ_EXPR)
5423 (convert (cond (eq @1 (convert @3))
5424 (convert:from_type @3) (convert:from_type @2)))))))))
5426 /* (cond (cmp (convert? x) c1) (op x c2) c3) -> (op (minmax x c1) c2) if:
5428 1) OP is PLUS or MINUS.
5429 2) CMP is LT, LE, GT or GE.
5430 3) C3 == (C1 op C2), and computation doesn't have undefined behavior.
5432 This pattern also handles special cases like:
5434 A) Operand x is a unsigned to signed type conversion and c1 is
5435 integer zero. In this case,
5436 (signed type)x < 0 <=> x > MAX_VAL(signed type)
5437 (signed type)x >= 0 <=> x <= MAX_VAL(signed type)
5438 B) Const c1 may not equal to (C3 op' C2). In this case we also
5439 check equality for (c1+1) and (c1-1) by adjusting comparison
5442 TODO: Though signed type is handled by this pattern, it cannot be
5443 simplified at the moment because C standard requires additional
5444 type promotion. In order to match&simplify it here, the IR needs
5445 to be cleaned up by other optimizers, i.e, VRP. */
5446 (for op (plus minus)
5447 (for cmp (lt le gt ge)
5449 (cond (cmp (convert? @X) INTEGER_CST@1) (op @X INTEGER_CST@2) INTEGER_CST@3)
5450 (with { tree from_type = TREE_TYPE (@X), to_type = TREE_TYPE (@1); }
5451 (if (types_match (from_type, to_type)
5452 /* Check if it is special case A). */
5453 || (TYPE_UNSIGNED (from_type)
5454 && !TYPE_UNSIGNED (to_type)
5455 && TYPE_PRECISION (from_type) == TYPE_PRECISION (to_type)
5456 && integer_zerop (@1)
5457 && (cmp == LT_EXPR || cmp == GE_EXPR)))
5460 wi::overflow_type overflow = wi::OVF_NONE;
5461 enum tree_code code, cmp_code = cmp;
5463 wide_int c1 = wi::to_wide (@1);
5464 wide_int c2 = wi::to_wide (@2);
5465 wide_int c3 = wi::to_wide (@3);
5466 signop sgn = TYPE_SIGN (from_type);
5468 /* Handle special case A), given x of unsigned type:
5469 ((signed type)x < 0) <=> (x > MAX_VAL(signed type))
5470 ((signed type)x >= 0) <=> (x <= MAX_VAL(signed type)) */
5471 if (!types_match (from_type, to_type))
5473 if (cmp_code == LT_EXPR)
5475 if (cmp_code == GE_EXPR)
5477 c1 = wi::max_value (to_type);
5479 /* To simplify this pattern, we require c3 = (c1 op c2). Here we
5480 compute (c3 op' c2) and check if it equals to c1 with op' being
5481 the inverted operator of op. Make sure overflow doesn't happen
5482 if it is undefined. */
5483 if (op == PLUS_EXPR)
5484 real_c1 = wi::sub (c3, c2, sgn, &overflow);
5486 real_c1 = wi::add (c3, c2, sgn, &overflow);
5489 if (!overflow || !TYPE_OVERFLOW_UNDEFINED (from_type))
5491 /* Check if c1 equals to real_c1. Boundary condition is handled
5492 by adjusting comparison operation if necessary. */
5493 if (!wi::cmp (wi::sub (real_c1, 1, sgn, &overflow), c1, sgn)
5496 /* X <= Y - 1 equals to X < Y. */
5497 if (cmp_code == LE_EXPR)
5499 /* X > Y - 1 equals to X >= Y. */
5500 if (cmp_code == GT_EXPR)
5503 if (!wi::cmp (wi::add (real_c1, 1, sgn, &overflow), c1, sgn)
5506 /* X < Y + 1 equals to X <= Y. */
5507 if (cmp_code == LT_EXPR)
5509 /* X >= Y + 1 equals to X > Y. */
5510 if (cmp_code == GE_EXPR)
5513 if (code != cmp_code || !wi::cmp (real_c1, c1, sgn))
5515 if (cmp_code == LT_EXPR || cmp_code == LE_EXPR)
5517 if (cmp_code == GT_EXPR || cmp_code == GE_EXPR)
5522 (if (code == MAX_EXPR)
5523 (op (max @X { wide_int_to_tree (from_type, real_c1); })
5524 { wide_int_to_tree (from_type, c2); })
5525 (if (code == MIN_EXPR)
5526 (op (min @X { wide_int_to_tree (from_type, real_c1); })
5527 { wide_int_to_tree (from_type, c2); })))))))))
5530 /* A >= B ? A : B -> max (A, B) and friends. The code is still
5531 in fold_cond_expr_with_comparison for GENERIC folding with
5532 some extra constraints. */
5533 (for cmp (eq ne le lt unle unlt ge gt unge ungt uneq ltgt)
5535 (cond (cmp:c (nop_convert1?@c0 @0) (nop_convert2?@c1 @1))
5536 (convert3? @0) (convert4? @1))
5537 (if (!HONOR_SIGNED_ZEROS (type)
5538 && (/* Allow widening conversions of the compare operands as data. */
5539 (INTEGRAL_TYPE_P (type)
5540 && types_match (TREE_TYPE (@c0), TREE_TYPE (@0))
5541 && types_match (TREE_TYPE (@c1), TREE_TYPE (@1))
5542 && TYPE_PRECISION (TREE_TYPE (@0)) <= TYPE_PRECISION (type)
5543 && TYPE_PRECISION (TREE_TYPE (@1)) <= TYPE_PRECISION (type))
5544 /* Or sign conversions for the comparison. */
5545 || (types_match (type, TREE_TYPE (@0))
5546 && types_match (type, TREE_TYPE (@1)))))
5548 (if (cmp == EQ_EXPR)
5549 (if (VECTOR_TYPE_P (type))
5552 (if (cmp == NE_EXPR)
5553 (if (VECTOR_TYPE_P (type))
5556 (if (cmp == LE_EXPR || cmp == UNLE_EXPR || cmp == LT_EXPR || cmp == UNLT_EXPR)
5557 (if (!HONOR_NANS (type))
5558 (if (VECTOR_TYPE_P (type))
5559 (view_convert (min @c0 @c1))
5560 (convert (min @c0 @c1)))))
5561 (if (cmp == GE_EXPR || cmp == UNGE_EXPR || cmp == GT_EXPR || cmp == UNGT_EXPR)
5562 (if (!HONOR_NANS (type))
5563 (if (VECTOR_TYPE_P (type))
5564 (view_convert (max @c0 @c1))
5565 (convert (max @c0 @c1)))))
5566 (if (cmp == UNEQ_EXPR)
5567 (if (!HONOR_NANS (type))
5568 (if (VECTOR_TYPE_P (type))
5571 (if (cmp == LTGT_EXPR)
5572 (if (!HONOR_NANS (type))
5573 (if (VECTOR_TYPE_P (type))
5575 (convert @c0))))))))
5578 (for cnd (cond vec_cond)
5579 /* (a != b) ? (a - b) : 0 -> (a - b) */
5581 (cnd (ne:c @0 @1) (minus@2 @0 @1) integer_zerop)
5583 /* (a != b) ? (a ^ b) : 0 -> (a ^ b) */
5585 (cnd (ne:c @0 @1) (bit_xor:c@2 @0 @1) integer_zerop)
5587 /* (a != b) ? (a & b) : a -> (a & b) */
5588 /* (a != b) ? (a | b) : a -> (a | b) */
5589 /* (a != b) ? min(a,b) : a -> min(a,b) */
5590 /* (a != b) ? max(a,b) : a -> max(a,b) */
5591 (for op (bit_and bit_ior min max)
5593 (cnd (ne:c @0 @1) (op:c@2 @0 @1) @0)
5595 /* (a != b) ? (a * b) : (a * a) -> (a * b) */
5596 /* (a != b) ? (a + b) : (a + a) -> (a + b) */
5599 (cnd (ne:c @0 @1) (op@2 @0 @1) (op @0 @0))
5600 (if (ANY_INTEGRAL_TYPE_P (type))
5602 /* (a != b) ? (a + b) : (2 * a) -> (a + b) */
5604 (cnd (ne:c @0 @1) (plus@2 @0 @1) (mult @0 uniform_integer_cst_p@3))
5605 (if (wi::to_wide (uniform_integer_cst_p (@3)) == 2)
5609 /* These was part of minmax phiopt. */
5610 /* Optimize (a CMP b) ? minmax<a, c> : minmax<b, c>
5611 to minmax<min/max<a, b>, c> */
5612 (for minmax (min max)
5613 (for cmp (lt le gt ge ne)
5615 (cond (cmp:c @1 @3) (minmax:c @1 @4) (minmax:c @2 @4))
5618 tree_code code = minmax_from_comparison (cmp, @1, @2, @1, @3);
5620 (if (code == MIN_EXPR)
5621 (minmax (min @1 @2) @4)
5622 (if (code == MAX_EXPR)
5623 (minmax (max @1 @2) @4)))))))
5625 /* Optimize (a CMP CST1) ? max<a,CST2> : a */
5626 (for cmp (gt ge lt le)
5627 minmax (min min max max)
5629 (cond (cmp:c @0 @1) (minmax:c@2 @0 @3) @4)
5632 tree_code code = minmax_from_comparison (cmp, @0, @1, @0, @4);
5634 (if ((cmp == LT_EXPR || cmp == LE_EXPR)
5636 && integer_nonzerop (fold_build2 (LE_EXPR, boolean_type_node, @3, @4)))
5638 (if ((cmp == GT_EXPR || cmp == GE_EXPR)
5640 && integer_nonzerop (fold_build2 (GE_EXPR, boolean_type_node, @3, @4)))
5644 /* These patterns should be after min/max detection as simplifications
5645 of `(type)(zero_one ==/!= 0)` to `(type)(zero_one)`
5646 and `(type)(zero_one^1)` are not done yet. See PR 110637.
5647 Even without those, reaching min/max/and/ior faster is better. */
5649 (cond @0 zero_one_valued_p@1 zero_one_valued_p@2)
5651 /* bool0 ? bool1 : 0 -> bool0 & bool1 */
5652 (if (integer_zerop (@2))
5653 (bit_and (convert @0) @1))
5654 /* bool0 ? 0 : bool2 -> (bool0^1) & bool2 */
5655 (if (integer_zerop (@1))
5656 (bit_and (bit_xor (convert @0) { build_one_cst (type); } ) @2))
5657 /* bool0 ? 1 : bool2 -> bool0 | bool2 */
5658 (if (integer_onep (@1))
5659 (bit_ior (convert @0) @2))
5660 /* bool0 ? bool1 : 1 -> (bool0^1) | bool1 */
5661 (if (integer_onep (@2))
5662 (bit_ior (bit_xor (convert @0) @2) @1))
5667 /* X != C1 ? -X : C2 simplifies to -X when -C1 == C2. */
5669 (cond (ne @0 INTEGER_CST@1) (negate@3 @0) INTEGER_CST@2)
5670 (if (!TYPE_SATURATING (type)
5671 && (TYPE_OVERFLOW_WRAPS (type)
5672 || !wi::only_sign_bit_p (wi::to_wide (@1)))
5673 && wi::eq_p (wi::neg (wi::to_wide (@1)), wi::to_wide (@2)))
5676 /* X != C1 ? ~X : C2 simplifies to ~X when ~C1 == C2. */
5678 (cond (ne @0 INTEGER_CST@1) (bit_not@3 @0) INTEGER_CST@2)
5679 (if (wi::eq_p (wi::bit_not (wi::to_wide (@1)), wi::to_wide (@2)))
5682 /* X != C1 ? abs(X) : C2 simplifies to abs(x) when abs(C1) == C2. */
5685 (cond (ne @0 INTEGER_CST@1) (op@3 @0) INTEGER_CST@2)
5686 (if (wi::abs (wi::to_wide (@1)) == wi::to_wide (@2))
5687 (if (op != ABSU_EXPR && wi::only_sign_bit_p (wi::to_wide (@1)))
5688 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
5689 (convert (absu:utype @0)))
5692 /* (X + 1) > Y ? -X : 1 simplifies to X >= Y ? -X : 1 when
5693 X is unsigned, as when X + 1 overflows, X is -1, so -X == 1. */
5695 (cond (gt (plus @0 integer_onep) @1) (negate @0) integer_onep@2)
5696 (if (TYPE_UNSIGNED (type))
5697 (cond (ge @0 @1) (negate @0) @2)))
5699 (for cnd (cond vec_cond)
5700 /* A ? B : (A ? X : C) -> A ? B : C. */
5702 (cnd @0 (cnd @0 @1 @2) @3)
5705 (cnd @0 @1 (cnd @0 @2 @3))
5707 /* A ? B : (!A ? C : X) -> A ? B : C. */
5708 /* ??? This matches embedded conditions open-coded because genmatch
5709 would generate matching code for conditions in separate stmts only.
5710 The following is still important to merge then and else arm cases
5711 from if-conversion. */
5713 (cnd @0 @1 (cnd @2 @3 @4))
5714 (if (inverse_conditions_p (@0, @2))
5717 (cnd @0 (cnd @1 @2 @3) @4)
5718 (if (inverse_conditions_p (@0, @1))
5721 /* A ? B : B -> B. */
5726 /* !A ? B : C -> A ? C : B. */
5728 (cnd (logical_inverted_value truth_valued_p@0) @1 @2)
5731 /* abs/negative simplifications moved from fold_cond_expr_with_comparison.
5733 None of these transformations work for modes with signed
5734 zeros. If A is +/-0, the first two transformations will
5735 change the sign of the result (from +0 to -0, or vice
5736 versa). The last four will fix the sign of the result,
5737 even though the original expressions could be positive or
5738 negative, depending on the sign of A.
5740 Note that all these transformations are correct if A is
5741 NaN, since the two alternatives (A and -A) are also NaNs. */
5743 (for cnd (cond vec_cond)
5744 /* A == 0 ? A : -A same as -A */
5747 (cnd (cmp @0 zerop) @2 (negate@1 @2))
5748 (if (!HONOR_SIGNED_ZEROS (type)
5749 && bitwise_equal_p (@0, @2))
5752 (cnd (cmp @0 zerop) zerop (negate@1 @2))
5753 (if (!HONOR_SIGNED_ZEROS (type)
5754 && bitwise_equal_p (@0, @2))
5757 /* A != 0 ? A : -A same as A */
5760 (cnd (cmp @0 zerop) @1 (negate @1))
5761 (if (!HONOR_SIGNED_ZEROS (type)
5762 && bitwise_equal_p (@0, @1))
5765 (cnd (cmp @0 zerop) @1 integer_zerop)
5766 (if (!HONOR_SIGNED_ZEROS (type)
5767 && bitwise_equal_p (@0, @1))
5770 /* A >=/> 0 ? A : -A same as abs (A) */
5773 (cnd (cmp @0 zerop) @1 (negate @1))
5774 (if (!HONOR_SIGNED_ZEROS (TREE_TYPE(@0))
5775 && !TYPE_UNSIGNED (TREE_TYPE(@0))
5776 && bitwise_equal_p (@0, @1))
5777 (if (TYPE_UNSIGNED (type))
5780 /* A <=/< 0 ? A : -A same as -abs (A) */
5783 (cnd (cmp @0 zerop) @1 (negate @1))
5784 (if (!HONOR_SIGNED_ZEROS (TREE_TYPE(@0))
5785 && !TYPE_UNSIGNED (TREE_TYPE(@0))
5786 && bitwise_equal_p (@0, @1))
5787 (if ((ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
5788 && !TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
5789 || TYPE_UNSIGNED (type))
5791 tree utype = unsigned_type_for (TREE_TYPE(@0));
5793 (convert (negate (absu:utype @0))))
5794 (negate (abs @0)))))
5797 /* (A - B) == 0 ? (A - B) : (B - A) same as (B - A) */
5800 (cnd (cmp (minus@0 @1 @2) zerop) @0 (minus@3 @2 @1))
5801 (if (!HONOR_SIGNED_ZEROS (type))
5804 (cnd (cmp (minus@0 @1 @2) integer_zerop) integer_zerop (minus@3 @2 @1))
5807 /* (A - B) != 0 ? (A - B) : (B - A) same as (A - B) */
5810 (cnd (cmp (minus@0 @1 @2) zerop) @0 (minus @2 @1))
5811 (if (!HONOR_SIGNED_ZEROS (type))
5814 (cnd (cmp (minus@0 @1 @2) integer_zerop) @0 integer_zerop)
5817 /* (A - B) >=/> 0 ? (A - B) : (B - A) same as abs (A - B) */
5820 (cnd (cmp (minus@0 @1 @2) zerop) @0 (minus @2 @1))
5821 (if (!HONOR_SIGNED_ZEROS (type)
5822 && !TYPE_UNSIGNED (type))
5824 /* (A - B) <=/< 0 ? (A - B) : (B - A) same as -abs (A - B) */
5827 (cnd (cmp (minus@0 @1 @2) zerop) @0 (minus @2 @1))
5828 (if (!HONOR_SIGNED_ZEROS (type)
5829 && !TYPE_UNSIGNED (type))
5830 (if (ANY_INTEGRAL_TYPE_P (type)
5831 && !TYPE_OVERFLOW_WRAPS (type))
5833 tree utype = unsigned_type_for (type);
5835 (convert (negate (absu:utype @0))))
5836 (negate (abs @0)))))
5840 /* -(type)!A -> (type)A - 1. */
5842 (negate (convert?:s (logical_inverted_value:s @0)))
5843 (if (INTEGRAL_TYPE_P (type)
5844 && TREE_CODE (type) != BOOLEAN_TYPE
5845 && TYPE_PRECISION (type) > 1
5846 && TREE_CODE (@0) == SSA_NAME
5847 && ssa_name_has_boolean_range (@0))
5848 (plus (convert:type @0) { build_all_ones_cst (type); })))
5850 /* A + (B vcmp C ? 1 : 0) -> A - (B vcmp C ? -1 : 0), since vector comparisons
5851 return all -1 or all 0 results. */
5852 /* ??? We could instead convert all instances of the vec_cond to negate,
5853 but that isn't necessarily a win on its own. */
5855 (plus:c @3 (view_convert? (vec_cond:s @0 integer_each_onep@1 integer_zerop@2)))
5856 (if (VECTOR_TYPE_P (type)
5857 && known_eq (TYPE_VECTOR_SUBPARTS (type),
5858 TYPE_VECTOR_SUBPARTS (TREE_TYPE (@1)))
5859 && (TYPE_MODE (TREE_TYPE (type))
5860 == TYPE_MODE (TREE_TYPE (TREE_TYPE (@1)))))
5861 (minus @3 (view_convert (vec_cond @0 (negate @1) @2)))))
5863 /* ... likewise A - (B vcmp C ? 1 : 0) -> A + (B vcmp C ? -1 : 0). */
5865 (minus @3 (view_convert? (vec_cond:s @0 integer_each_onep@1 integer_zerop@2)))
5866 (if (VECTOR_TYPE_P (type)
5867 && known_eq (TYPE_VECTOR_SUBPARTS (type),
5868 TYPE_VECTOR_SUBPARTS (TREE_TYPE (@1)))
5869 && (TYPE_MODE (TREE_TYPE (type))
5870 == TYPE_MODE (TREE_TYPE (TREE_TYPE (@1)))))
5871 (plus @3 (view_convert (vec_cond @0 (negate @1) @2)))))
5874 /* Simplifications of comparisons. */
5876 /* See if we can reduce the magnitude of a constant involved in a
5877 comparison by changing the comparison code. This is a canonicalization
5878 formerly done by maybe_canonicalize_comparison_1. */
5882 (cmp @0 uniform_integer_cst_p@1)
5883 (with { tree cst = uniform_integer_cst_p (@1); }
5884 (if (tree_int_cst_sgn (cst) == -1)
5885 (acmp @0 { build_uniform_cst (TREE_TYPE (@1),
5886 wide_int_to_tree (TREE_TYPE (cst),
5892 (cmp @0 uniform_integer_cst_p@1)
5893 (with { tree cst = uniform_integer_cst_p (@1); }
5894 (if (tree_int_cst_sgn (cst) == 1)
5895 (acmp @0 { build_uniform_cst (TREE_TYPE (@1),
5896 wide_int_to_tree (TREE_TYPE (cst),
5897 wi::to_wide (cst) - 1)); })))))
5899 /* We can simplify a logical negation of a comparison to the
5900 inverted comparison. As we cannot compute an expression
5901 operator using invert_tree_comparison we have to simulate
5902 that with expression code iteration. */
5903 (for cmp (tcc_comparison)
5904 icmp (inverted_tcc_comparison)
5905 ncmp (inverted_tcc_comparison_with_nans)
5906 /* Ideally we'd like to combine the following two patterns
5907 and handle some more cases by using
5908 (logical_inverted_value (cmp @0 @1))
5909 here but for that genmatch would need to "inline" that.
5910 For now implement what forward_propagate_comparison did. */
5912 (bit_not (cmp @0 @1))
5913 (if (VECTOR_TYPE_P (type)
5914 || (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1))
5915 /* Comparison inversion may be impossible for trapping math,
5916 invert_tree_comparison will tell us. But we can't use
5917 a computed operator in the replacement tree thus we have
5918 to play the trick below. */
5919 (with { enum tree_code ic = invert_tree_comparison
5920 (cmp, HONOR_NANS (@0)); }
5926 (bit_xor (cmp @0 @1) integer_truep)
5927 (with { enum tree_code ic = invert_tree_comparison
5928 (cmp, HONOR_NANS (@0)); }
5933 /* The following bits are handled by fold_binary_op_with_conditional_arg. */
5935 (ne (cmp@2 @0 @1) integer_zerop)
5936 (if (types_match (type, TREE_TYPE (@2)))
5939 (eq (cmp@2 @0 @1) integer_truep)
5940 (if (types_match (type, TREE_TYPE (@2)))
5943 (ne (cmp@2 @0 @1) integer_truep)
5944 (if (types_match (type, TREE_TYPE (@2)))
5945 (with { enum tree_code ic = invert_tree_comparison
5946 (cmp, HONOR_NANS (@0)); }
5952 (eq (cmp@2 @0 @1) integer_zerop)
5953 (if (types_match (type, TREE_TYPE (@2)))
5954 (with { enum tree_code ic = invert_tree_comparison
5955 (cmp, HONOR_NANS (@0)); }
5961 /* Transform comparisons of the form X - Y CMP 0 to X CMP Y.
5962 ??? The transformation is valid for the other operators if overflow
5963 is undefined for the type, but performing it here badly interacts
5964 with the transformation in fold_cond_expr_with_comparison which
5965 attempts to synthetize ABS_EXPR. */
5967 (for sub (minus pointer_diff)
5969 (cmp (sub@2 @0 @1) integer_zerop)
5970 (if (single_use (@2))
5973 /* Simplify (x < 0) ^ (y < 0) to (x ^ y) < 0 and
5974 (x >= 0) ^ (y >= 0) to (x ^ y) < 0. */
5977 (bit_xor (cmp:s @0 integer_zerop) (cmp:s @1 integer_zerop))
5978 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
5979 && !TYPE_UNSIGNED (TREE_TYPE (@0))
5980 && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
5981 (lt (bit_xor @0 @1) { build_zero_cst (TREE_TYPE (@0)); }))))
5982 /* Simplify (x < 0) ^ (y >= 0) to (x ^ y) >= 0 and
5983 (x >= 0) ^ (y < 0) to (x ^ y) >= 0. */
5985 (bit_xor:c (lt:s @0 integer_zerop) (ge:s @1 integer_zerop))
5986 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
5987 && !TYPE_UNSIGNED (TREE_TYPE (@0))
5988 && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
5989 (ge (bit_xor @0 @1) { build_zero_cst (TREE_TYPE (@0)); })))
5991 /* Transform comparisons of the form X * C1 CMP 0 to X CMP 0 in the
5992 signed arithmetic case. That form is created by the compiler
5993 often enough for folding it to be of value. One example is in
5994 computing loop trip counts after Operator Strength Reduction. */
5995 (for cmp (simple_comparison)
5996 scmp (swapped_simple_comparison)
5998 (cmp (mult@3 @0 INTEGER_CST@1) integer_zerop@2)
5999 /* Handle unfolded multiplication by zero. */
6000 (if (integer_zerop (@1))
6002 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
6003 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
6005 /* If @1 is negative we swap the sense of the comparison. */
6006 (if (tree_int_cst_sgn (@1) < 0)
6010 /* For integral types with undefined overflow fold
6011 x * C1 == C2 into x == C2 / C1 or false.
6012 If overflow wraps and C1 is odd, simplify to x == C2 / C1 in the ring
6016 (cmp (mult @0 INTEGER_CST@1) INTEGER_CST@2)
6017 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6018 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
6019 && wi::to_wide (@1) != 0)
6020 (with { widest_int quot; }
6021 (if (wi::multiple_of_p (wi::to_widest (@2), wi::to_widest (@1),
6022 TYPE_SIGN (TREE_TYPE (@0)), "))
6023 (cmp @0 { wide_int_to_tree (TREE_TYPE (@0), quot); })
6024 { constant_boolean_node (cmp == NE_EXPR, type); }))
6025 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6026 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))
6027 && (wi::bit_and (wi::to_wide (@1), 1) == 1))
6030 tree itype = TREE_TYPE (@0);
6031 int p = TYPE_PRECISION (itype);
6032 wide_int m = wi::one (p + 1) << p;
6033 wide_int a = wide_int::from (wi::to_wide (@1), p + 1, UNSIGNED);
6034 wide_int i = wide_int::from (wi::mod_inv (a, m),
6035 p, TYPE_SIGN (itype));
6036 wide_int_to_tree (itype, wi::mul (i, wi::to_wide (@2)));
6039 /* Simplify comparison of something with itself. For IEEE
6040 floating-point, we can only do some of these simplifications. */
6044 (if (! FLOAT_TYPE_P (TREE_TYPE (@0))
6045 || ! tree_expr_maybe_nan_p (@0))
6046 { constant_boolean_node (true, type); }
6048 /* With -ftrapping-math conversion to EQ loses an exception. */
6049 && (! FLOAT_TYPE_P (TREE_TYPE (@0))
6050 || ! flag_trapping_math))
6056 || ! FLOAT_TYPE_P (TREE_TYPE (@0))
6057 || ! tree_expr_maybe_nan_p (@0))
6058 { constant_boolean_node (false, type); })))
6059 (for cmp (unle unge uneq)
6062 { constant_boolean_node (true, type); }))
6063 (for cmp (unlt ungt)
6069 (if (!flag_trapping_math || !tree_expr_maybe_nan_p (@0))
6070 { constant_boolean_node (false, type); }))
6072 /* x == ~x -> false */
6073 /* x != ~x -> true */
6076 (cmp:c @0 (bit_not @0))
6077 { constant_boolean_node (cmp == NE_EXPR, type); }))
6079 /* Fold ~X op ~Y as Y op X. */
6080 (for cmp (simple_comparison)
6082 (cmp (nop_convert1?@4 (bit_not@2 @0)) (nop_convert2? (bit_not@3 @1)))
6083 (if (single_use (@2) && single_use (@3))
6084 (with { tree otype = TREE_TYPE (@4); }
6085 (cmp (convert:otype @1) (convert:otype @0))))))
6087 /* Fold ~X op C as X op' ~C, where op' is the swapped comparison. */
6088 (for cmp (simple_comparison)
6089 scmp (swapped_simple_comparison)
6091 (cmp (nop_convert? (bit_not@2 @0)) CONSTANT_CLASS_P@1)
6092 (if (single_use (@2)
6093 && (TREE_CODE (@1) == INTEGER_CST || TREE_CODE (@1) == VECTOR_CST))
6094 (with { tree otype = TREE_TYPE (@1); }
6095 (scmp (convert:otype @0) (bit_not @1))))))
6097 (for cmp (simple_comparison)
6100 /* IEEE doesn't distinguish +0 and -0 in comparisons. */
6102 /* a CMP (-0) -> a CMP 0 */
6103 (if (REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@1)))
6104 (cmp @0 { build_real (TREE_TYPE (@1), dconst0); }))
6105 /* (-0) CMP b -> 0 CMP b. */
6106 (if (TREE_CODE (@0) == REAL_CST
6107 && REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@0)))
6108 (cmp { build_real (TREE_TYPE (@0), dconst0); } @1))
6109 /* x != NaN is always true, other ops are always false. */
6110 (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
6111 && (cmp == EQ_EXPR || cmp == NE_EXPR || !flag_trapping_math)
6112 && !tree_expr_signaling_nan_p (@1)
6113 && !tree_expr_maybe_signaling_nan_p (@0))
6114 { constant_boolean_node (cmp == NE_EXPR, type); })
6115 /* NaN != y is always true, other ops are always false. */
6116 (if (TREE_CODE (@0) == REAL_CST
6117 && REAL_VALUE_ISNAN (TREE_REAL_CST (@0))
6118 && (cmp == EQ_EXPR || cmp == NE_EXPR || !flag_trapping_math)
6119 && !tree_expr_signaling_nan_p (@0)
6120 && !tree_expr_signaling_nan_p (@1))
6121 { constant_boolean_node (cmp == NE_EXPR, type); })
6122 /* Fold comparisons against infinity. */
6123 (if (REAL_VALUE_ISINF (TREE_REAL_CST (@1))
6124 && MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (@1))))
6127 REAL_VALUE_TYPE max;
6128 enum tree_code code = cmp;
6129 bool neg = REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1));
6131 code = swap_tree_comparison (code);
6134 /* x > +Inf is always false, if we ignore NaNs or exceptions. */
6135 (if (code == GT_EXPR
6136 && !(HONOR_NANS (@0) && flag_trapping_math))
6137 { constant_boolean_node (false, type); })
6138 (if (code == LE_EXPR)
6139 /* x <= +Inf is always true, if we don't care about NaNs. */
6140 (if (! HONOR_NANS (@0))
6141 { constant_boolean_node (true, type); }
6142 /* x <= +Inf is the same as x == x, i.e. !isnan(x), but this loses
6143 an "invalid" exception. */
6144 (if (!flag_trapping_math)
6146 /* x == +Inf and x >= +Inf are always equal to x > DBL_MAX, but
6147 for == this introduces an exception for x a NaN. */
6148 (if ((code == EQ_EXPR && !(HONOR_NANS (@0) && flag_trapping_math))
6150 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
6152 (lt @0 { build_real (TREE_TYPE (@0), max); })
6153 (gt @0 { build_real (TREE_TYPE (@0), max); }))))
6154 /* x < +Inf is always equal to x <= DBL_MAX. */
6155 (if (code == LT_EXPR)
6156 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
6158 (ge @0 { build_real (TREE_TYPE (@0), max); })
6159 (le @0 { build_real (TREE_TYPE (@0), max); }))))
6160 /* x != +Inf is always equal to !(x > DBL_MAX), but this introduces
6161 an exception for x a NaN so use an unordered comparison. */
6162 (if (code == NE_EXPR)
6163 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
6164 (if (! HONOR_NANS (@0))
6166 (ge @0 { build_real (TREE_TYPE (@0), max); })
6167 (le @0 { build_real (TREE_TYPE (@0), max); }))
6169 (unge @0 { build_real (TREE_TYPE (@0), max); })
6170 (unle @0 { build_real (TREE_TYPE (@0), max); }))))))))))
6172 /* If this is a comparison of a real constant with a PLUS_EXPR
6173 or a MINUS_EXPR of a real constant, we can convert it into a
6174 comparison with a revised real constant as long as no overflow
6175 occurs when unsafe_math_optimizations are enabled. */
6176 (if (flag_unsafe_math_optimizations)
6177 (for op (plus minus)
6179 (cmp (op @0 REAL_CST@1) REAL_CST@2)
6182 tree tem = const_binop (op == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR,
6183 TREE_TYPE (@1), @2, @1);
6185 (if (tem && !TREE_OVERFLOW (tem))
6186 (cmp @0 { tem; }))))))
6188 /* Likewise, we can simplify a comparison of a real constant with
6189 a MINUS_EXPR whose first operand is also a real constant, i.e.
6190 (c1 - x) < c2 becomes x > c1-c2. Reordering is allowed on
6191 floating-point types only if -fassociative-math is set. */
6192 (if (flag_associative_math)
6194 (cmp (minus REAL_CST@0 @1) REAL_CST@2)
6195 (with { tree tem = const_binop (MINUS_EXPR, TREE_TYPE (@1), @0, @2); }
6196 (if (tem && !TREE_OVERFLOW (tem))
6197 (cmp { tem; } @1)))))
6199 /* Fold comparisons against built-in math functions. */
6200 (if (flag_unsafe_math_optimizations && ! flag_errno_math)
6203 (cmp (sq @0) REAL_CST@1)
6205 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
6207 /* sqrt(x) < y is always false, if y is negative. */
6208 (if (cmp == EQ_EXPR || cmp == LT_EXPR || cmp == LE_EXPR)
6209 { constant_boolean_node (false, type); })
6210 /* sqrt(x) > y is always true, if y is negative and we
6211 don't care about NaNs, i.e. negative values of x. */
6212 (if (cmp == NE_EXPR || !HONOR_NANS (@0))
6213 { constant_boolean_node (true, type); })
6214 /* sqrt(x) > y is the same as x >= 0, if y is negative. */
6215 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })))
6216 (if (real_equal (TREE_REAL_CST_PTR (@1), &dconst0))
6218 /* sqrt(x) < 0 is always false. */
6219 (if (cmp == LT_EXPR)
6220 { constant_boolean_node (false, type); })
6221 /* sqrt(x) >= 0 is always true if we don't care about NaNs. */
6222 (if (cmp == GE_EXPR && !HONOR_NANS (@0))
6223 { constant_boolean_node (true, type); })
6224 /* sqrt(x) <= 0 -> x == 0. */
6225 (if (cmp == LE_EXPR)
6227 /* Otherwise sqrt(x) cmp 0 -> x cmp 0. Here cmp can be >=, >,
6228 == or !=. In the last case:
6230 (sqrt(x) != 0) == (NaN != 0) == true == (x != 0)
6232 if x is negative or NaN. Due to -funsafe-math-optimizations,
6233 the results for other x follow from natural arithmetic. */
6235 (if ((cmp == LT_EXPR
6239 && !REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
6240 /* Give up for -frounding-math. */
6241 && !HONOR_SIGN_DEPENDENT_ROUNDING (TREE_TYPE (@0)))
6245 enum tree_code ncmp = cmp;
6246 const real_format *fmt
6247 = REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (@0)));
6248 real_arithmetic (&c2, MULT_EXPR,
6249 &TREE_REAL_CST (@1), &TREE_REAL_CST (@1));
6250 real_convert (&c2, fmt, &c2);
6251 /* See PR91734: if c2 is inexact and sqrt(c2) < c (or sqrt(c2) >= c),
6252 then change LT_EXPR into LE_EXPR or GE_EXPR into GT_EXPR. */
6253 if (!REAL_VALUE_ISINF (c2))
6255 tree c3 = fold_const_call (CFN_SQRT, TREE_TYPE (@0),
6256 build_real (TREE_TYPE (@0), c2));
6257 if (c3 == NULL_TREE || TREE_CODE (c3) != REAL_CST)
6259 else if ((cmp == LT_EXPR || cmp == GE_EXPR)
6260 && real_less (&TREE_REAL_CST (c3), &TREE_REAL_CST (@1)))
6261 ncmp = cmp == LT_EXPR ? LE_EXPR : GT_EXPR;
6262 else if ((cmp == LE_EXPR || cmp == GT_EXPR)
6263 && real_less (&TREE_REAL_CST (@1), &TREE_REAL_CST (c3)))
6264 ncmp = cmp == LE_EXPR ? LT_EXPR : GE_EXPR;
6267 /* With rounding to even, sqrt of up to 3 different values
6268 gives the same normal result, so in some cases c2 needs
6270 REAL_VALUE_TYPE c2alt, tow;
6271 if (cmp == LT_EXPR || cmp == GE_EXPR)
6275 real_nextafter (&c2alt, fmt, &c2, &tow);
6276 real_convert (&c2alt, fmt, &c2alt);
6277 if (REAL_VALUE_ISINF (c2alt))
6281 c3 = fold_const_call (CFN_SQRT, TREE_TYPE (@0),
6282 build_real (TREE_TYPE (@0), c2alt));
6283 if (c3 == NULL_TREE || TREE_CODE (c3) != REAL_CST)
6285 else if (real_equal (&TREE_REAL_CST (c3),
6286 &TREE_REAL_CST (@1)))
6292 (if (cmp == GT_EXPR || cmp == GE_EXPR)
6293 (if (REAL_VALUE_ISINF (c2))
6294 /* sqrt(x) > y is x == +Inf, when y is very large. */
6295 (if (HONOR_INFINITIES (@0))
6296 (eq @0 { build_real (TREE_TYPE (@0), c2); })
6297 { constant_boolean_node (false, type); })
6298 /* sqrt(x) > c is the same as x > c*c. */
6299 (if (ncmp != ERROR_MARK)
6300 (if (ncmp == GE_EXPR)
6301 (ge @0 { build_real (TREE_TYPE (@0), c2); })
6302 (gt @0 { build_real (TREE_TYPE (@0), c2); }))))
6303 /* else if (cmp == LT_EXPR || cmp == LE_EXPR) */
6304 (if (REAL_VALUE_ISINF (c2))
6306 /* sqrt(x) < y is always true, when y is a very large
6307 value and we don't care about NaNs or Infinities. */
6308 (if (! HONOR_NANS (@0) && ! HONOR_INFINITIES (@0))
6309 { constant_boolean_node (true, type); })
6310 /* sqrt(x) < y is x != +Inf when y is very large and we
6311 don't care about NaNs. */
6312 (if (! HONOR_NANS (@0))
6313 (ne @0 { build_real (TREE_TYPE (@0), c2); }))
6314 /* sqrt(x) < y is x >= 0 when y is very large and we
6315 don't care about Infinities. */
6316 (if (! HONOR_INFINITIES (@0))
6317 (ge @0 { build_real (TREE_TYPE (@0), dconst0); }))
6318 /* sqrt(x) < y is x >= 0 && x != +Inf, when y is large. */
6321 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
6322 (ne @0 { build_real (TREE_TYPE (@0), c2); }))))
6323 /* sqrt(x) < c is the same as x < c*c, if we ignore NaNs. */
6324 (if (ncmp != ERROR_MARK && ! HONOR_NANS (@0))
6325 (if (ncmp == LT_EXPR)
6326 (lt @0 { build_real (TREE_TYPE (@0), c2); })
6327 (le @0 { build_real (TREE_TYPE (@0), c2); }))
6328 /* sqrt(x) < c is the same as x >= 0 && x < c*c. */
6329 (if (ncmp != ERROR_MARK && GENERIC)
6330 (if (ncmp == LT_EXPR)
6332 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
6333 (lt @0 { build_real (TREE_TYPE (@0), c2); }))
6335 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
6336 (le @0 { build_real (TREE_TYPE (@0), c2); })))))))))))
6337 /* Transform sqrt(x) cmp sqrt(y) -> x cmp y. */
6339 (cmp (sq @0) (sq @1))
6340 (if (! HONOR_NANS (@0))
6343 /* Optimize various special cases of (FTYPE) N CMP (FTYPE) M. */
6344 (for cmp (lt le eq ne ge gt unordered ordered unlt unle ungt unge uneq ltgt)
6345 icmp (lt le eq ne ge gt unordered ordered lt le gt ge eq ne)
6347 (cmp (float@0 @1) (float @2))
6348 (if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (@0))
6349 && ! DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0)))
6352 format_helper fmt (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (@0))));
6353 tree type1 = TREE_TYPE (@1);
6354 bool type1_signed_p = TYPE_SIGN (type1) == SIGNED;
6355 tree type2 = TREE_TYPE (@2);
6356 bool type2_signed_p = TYPE_SIGN (type2) == SIGNED;
6358 (if (fmt.can_represent_integral_type_p (type1)
6359 && fmt.can_represent_integral_type_p (type2))
6360 (if (cmp == ORDERED_EXPR || cmp == UNORDERED_EXPR)
6361 { constant_boolean_node (cmp == ORDERED_EXPR, type); }
6362 (if (TYPE_PRECISION (type1) > TYPE_PRECISION (type2)
6363 && type1_signed_p >= type2_signed_p)
6364 (icmp @1 (convert @2))
6365 (if (TYPE_PRECISION (type1) < TYPE_PRECISION (type2)
6366 && type1_signed_p <= type2_signed_p)
6367 (icmp (convert:type2 @1) @2)
6368 (if (TYPE_PRECISION (type1) == TYPE_PRECISION (type2)
6369 && type1_signed_p == type2_signed_p)
6370 (icmp @1 @2))))))))))
6372 /* Optimize various special cases of (FTYPE) N CMP CST. */
6373 (for cmp (lt le eq ne ge gt)
6374 icmp (le le eq ne ge ge)
6376 (cmp (float @0) REAL_CST@1)
6377 (if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (@1))
6378 && ! DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1)))
6381 tree itype = TREE_TYPE (@0);
6382 format_helper fmt (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (@1))));
6383 const REAL_VALUE_TYPE *cst = TREE_REAL_CST_PTR (@1);
6384 /* Be careful to preserve any potential exceptions due to
6385 NaNs. qNaNs are ok in == or != context.
6386 TODO: relax under -fno-trapping-math or
6387 -fno-signaling-nans. */
6389 = real_isnan (cst) && (cst->signalling
6390 || (cmp != EQ_EXPR && cmp != NE_EXPR));
6392 /* TODO: allow non-fitting itype and SNaNs when
6393 -fno-trapping-math. */
6394 (if (fmt.can_represent_integral_type_p (itype) && ! exception_p)
6397 signop isign = TYPE_SIGN (itype);
6398 REAL_VALUE_TYPE imin, imax;
6399 real_from_integer (&imin, fmt, wi::min_value (itype), isign);
6400 real_from_integer (&imax, fmt, wi::max_value (itype), isign);
6402 REAL_VALUE_TYPE icst;
6403 if (cmp == GT_EXPR || cmp == GE_EXPR)
6404 real_ceil (&icst, fmt, cst);
6405 else if (cmp == LT_EXPR || cmp == LE_EXPR)
6406 real_floor (&icst, fmt, cst);
6408 real_trunc (&icst, fmt, cst);
6410 bool cst_int_p = !real_isnan (cst) && real_identical (&icst, cst);
6412 bool overflow_p = false;
6414 = real_to_integer (&icst, &overflow_p, TYPE_PRECISION (itype));
6417 /* Optimize cases when CST is outside of ITYPE's range. */
6418 (if (real_compare (LT_EXPR, cst, &imin))
6419 { constant_boolean_node (cmp == GT_EXPR || cmp == GE_EXPR || cmp == NE_EXPR,
6421 (if (real_compare (GT_EXPR, cst, &imax))
6422 { constant_boolean_node (cmp == LT_EXPR || cmp == LE_EXPR || cmp == NE_EXPR,
6424 /* Remove cast if CST is an integer representable by ITYPE. */
6426 (cmp @0 { gcc_assert (!overflow_p);
6427 wide_int_to_tree (itype, icst_val); })
6429 /* When CST is fractional, optimize
6430 (FTYPE) N == CST -> 0
6431 (FTYPE) N != CST -> 1. */
6432 (if (cmp == EQ_EXPR || cmp == NE_EXPR)
6433 { constant_boolean_node (cmp == NE_EXPR, type); })
6434 /* Otherwise replace with sensible integer constant. */
6437 gcc_checking_assert (!overflow_p);
6439 (icmp @0 { wide_int_to_tree (itype, icst_val); })))))))))
6441 /* Fold A /[ex] B CMP C to A CMP B * C. */
6444 (cmp (exact_div @0 @1) INTEGER_CST@2)
6445 (if (!integer_zerop (@1))
6446 (if (wi::to_wide (@2) == 0)
6448 (if (TREE_CODE (@1) == INTEGER_CST)
6451 wi::overflow_type ovf;
6452 wide_int prod = wi::mul (wi::to_wide (@2), wi::to_wide (@1),
6453 TYPE_SIGN (TREE_TYPE (@1)), &ovf);
6456 { constant_boolean_node (cmp == NE_EXPR, type); }
6457 (cmp @0 { wide_int_to_tree (TREE_TYPE (@0), prod); }))))))))
6458 (for cmp (lt le gt ge)
6460 (cmp (exact_div @0 INTEGER_CST@1) INTEGER_CST@2)
6461 (if (wi::gt_p (wi::to_wide (@1), 0, TYPE_SIGN (TREE_TYPE (@1))))
6464 wi::overflow_type ovf;
6465 wide_int prod = wi::mul (wi::to_wide (@2), wi::to_wide (@1),
6466 TYPE_SIGN (TREE_TYPE (@1)), &ovf);
6469 { constant_boolean_node (wi::lt_p (wi::to_wide (@2), 0,
6470 TYPE_SIGN (TREE_TYPE (@2)))
6471 != (cmp == LT_EXPR || cmp == LE_EXPR), type); }
6472 (cmp @0 { wide_int_to_tree (TREE_TYPE (@0), prod); }))))))
6474 /* Fold (size_t)(A /[ex] B) CMP C to (size_t)A CMP (size_t)B * C or A CMP' 0.
6476 For small C (less than max/B), this is (size_t)A CMP (size_t)B * C.
6477 For large C (more than min/B+2^size), this is also true, with the
6478 multiplication computed modulo 2^size.
6479 For intermediate C, this just tests the sign of A. */
6480 (for cmp (lt le gt ge)
6483 (cmp (convert (exact_div @0 INTEGER_CST@1)) INTEGER_CST@2)
6484 (if (tree_nop_conversion_p (TREE_TYPE (@0), TREE_TYPE (@2))
6485 && TYPE_UNSIGNED (TREE_TYPE (@2)) && !TYPE_UNSIGNED (TREE_TYPE (@0))
6486 && wi::gt_p (wi::to_wide (@1), 0, TYPE_SIGN (TREE_TYPE (@1))))
6489 tree utype = TREE_TYPE (@2);
6490 wide_int denom = wi::to_wide (@1);
6491 wide_int right = wi::to_wide (@2);
6492 wide_int smax = wi::sdiv_trunc (wi::max_value (TREE_TYPE (@0)), denom);
6493 wide_int smin = wi::sdiv_trunc (wi::min_value (TREE_TYPE (@0)), denom);
6494 bool small = wi::leu_p (right, smax);
6495 bool large = wi::geu_p (right, smin);
6497 (if (small || large)
6498 (cmp (convert:utype @0) (mult @2 (convert @1)))
6499 (cmp2 @0 { build_zero_cst (TREE_TYPE (@0)); }))))))
6501 /* Unordered tests if either argument is a NaN. */
6503 (bit_ior (unordered @0 @0) (unordered @1 @1))
6504 (if (types_match (@0, @1))
6507 (bit_and (ordered @0 @0) (ordered @1 @1))
6508 (if (types_match (@0, @1))
6511 (bit_ior:c (unordered @0 @0) (unordered:c@2 @0 @1))
6514 (bit_and:c (ordered @0 @0) (ordered:c@2 @0 @1))
6517 /* A & (2**N - 1) <= 2**K - 1 -> A & (2**N - 2**K) == 0
6518 A & (2**N - 1) > 2**K - 1 -> A & (2**N - 2**K) != 0
6520 Note that comparisons
6521 A & (2**N - 1) < 2**K -> A & (2**N - 2**K) == 0
6522 A & (2**N - 1) >= 2**K -> A & (2**N - 2**K) != 0
6523 will be canonicalized to above so there's no need to
6530 (cmp (bit_and@0 @1 INTEGER_CST@2) INTEGER_CST@3)
6531 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)))
6534 tree ty = TREE_TYPE (@0);
6535 unsigned prec = TYPE_PRECISION (ty);
6536 wide_int mask = wi::to_wide (@2, prec);
6537 wide_int rhs = wi::to_wide (@3, prec);
6538 signop sgn = TYPE_SIGN (ty);
6540 (if ((mask & (mask + 1)) == 0 && wi::gt_p (rhs, 0, sgn)
6541 && (rhs & (rhs + 1)) == 0 && wi::ge_p (mask, rhs, sgn))
6542 (eqcmp (bit_and @1 { wide_int_to_tree (ty, mask - rhs); })
6543 { build_zero_cst (ty); }))))))
6545 /* -A CMP -B -> B CMP A. */
6546 (for cmp (tcc_comparison)
6547 scmp (swapped_tcc_comparison)
6549 (cmp (negate @0) (negate @1))
6550 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
6551 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
6554 || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))))
6557 (cmp (negate @0) CONSTANT_CLASS_P@1)
6558 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
6559 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
6562 || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))))
6563 (with { tree tem = const_unop (NEGATE_EXPR, TREE_TYPE (@0), @1); }
6564 (if (tem && !TREE_OVERFLOW (tem))
6565 (scmp @0 { tem; }))))))
6567 /* Convert ABS[U]_EXPR<x> == 0 or ABS[U]_EXPR<x> != 0 to x == 0 or x != 0. */
6571 (eqne (op @0) zerop@1)
6572 (eqne @0 { build_zero_cst (TREE_TYPE (@0)); }))))
6574 /* From fold_sign_changed_comparison and fold_widened_comparison.
6575 FIXME: the lack of symmetry is disturbing. */
6576 (for cmp (simple_comparison)
6578 (cmp (convert@0 @00) (convert?@1 @10))
6579 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6580 /* Disable this optimization if we're casting a function pointer
6581 type on targets that require function pointer canonicalization. */
6582 && !(targetm.have_canonicalize_funcptr_for_compare ()
6583 && ((POINTER_TYPE_P (TREE_TYPE (@00))
6584 && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@00))))
6585 || (POINTER_TYPE_P (TREE_TYPE (@10))
6586 && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@10))))))
6588 (if (TYPE_PRECISION (TREE_TYPE (@00)) == TYPE_PRECISION (TREE_TYPE (@0))
6589 && (TREE_CODE (@10) == INTEGER_CST
6591 && (TYPE_UNSIGNED (TREE_TYPE (@00)) == TYPE_UNSIGNED (TREE_TYPE (@0))
6594 && !POINTER_TYPE_P (TREE_TYPE (@00))
6595 /* (int)bool:32 != (int)uint is not the same as
6596 bool:32 != (bool:32)uint since boolean types only have two valid
6597 values independent of their precision. */
6598 && (TREE_CODE (TREE_TYPE (@00)) != BOOLEAN_TYPE
6599 || TREE_CODE (TREE_TYPE (@10)) == BOOLEAN_TYPE))
6600 /* ??? The special-casing of INTEGER_CST conversion was in the original
6601 code and here to avoid a spurious overflow flag on the resulting
6602 constant which fold_convert produces. */
6603 (if (TREE_CODE (@1) == INTEGER_CST)
6604 (cmp @00 { force_fit_type (TREE_TYPE (@00),
6605 wide_int::from (wi::to_wide (@1),
6606 MAX (TYPE_PRECISION (TREE_TYPE (@1)),
6607 TYPE_PRECISION (TREE_TYPE (@00))),
6608 TYPE_SIGN (TREE_TYPE (@1))),
6609 0, TREE_OVERFLOW (@1)); })
6610 (cmp @00 (convert @1)))
6612 (if (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (TREE_TYPE (@00)))
6613 /* If possible, express the comparison in the shorter mode. */
6614 (if ((cmp == EQ_EXPR || cmp == NE_EXPR
6615 || TYPE_UNSIGNED (TREE_TYPE (@0)) == TYPE_UNSIGNED (TREE_TYPE (@00))
6616 || (!TYPE_UNSIGNED (TREE_TYPE (@0))
6617 && TYPE_UNSIGNED (TREE_TYPE (@00))))
6618 && (types_match (TREE_TYPE (@10), TREE_TYPE (@00))
6619 || ((TYPE_PRECISION (TREE_TYPE (@00))
6620 >= TYPE_PRECISION (TREE_TYPE (@10)))
6621 && (TYPE_UNSIGNED (TREE_TYPE (@00))
6622 == TYPE_UNSIGNED (TREE_TYPE (@10))))
6623 || (TREE_CODE (@10) == INTEGER_CST
6624 && INTEGRAL_TYPE_P (TREE_TYPE (@00))
6625 && int_fits_type_p (@10, TREE_TYPE (@00)))))
6626 (cmp @00 (convert @10))
6627 (if (TREE_CODE (@10) == INTEGER_CST
6628 && INTEGRAL_TYPE_P (TREE_TYPE (@00))
6629 && !int_fits_type_p (@10, TREE_TYPE (@00)))
6632 tree min = lower_bound_in_type (TREE_TYPE (@10), TREE_TYPE (@00));
6633 tree max = upper_bound_in_type (TREE_TYPE (@10), TREE_TYPE (@00));
6634 bool above = integer_nonzerop (const_binop (LT_EXPR, type, max, @10));
6635 bool below = integer_nonzerop (const_binop (LT_EXPR, type, @10, min));
6637 (if (above || below)
6638 (if (cmp == EQ_EXPR || cmp == NE_EXPR)
6639 { constant_boolean_node (cmp == EQ_EXPR ? false : true, type); }
6640 (if (cmp == LT_EXPR || cmp == LE_EXPR)
6641 { constant_boolean_node (above ? true : false, type); }
6642 (if (cmp == GT_EXPR || cmp == GE_EXPR)
6643 { constant_boolean_node (above ? false : true, type); })))))))))
6644 /* Fold (double)float1 CMP (double)float2 into float1 CMP float2. */
6645 (if (FLOAT_TYPE_P (TREE_TYPE (@00))
6646 && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0))
6647 == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@00)))
6648 && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0))
6649 == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@10))))
6652 tree type1 = TREE_TYPE (@10);
6653 if (TREE_CODE (@10) == REAL_CST && !DECIMAL_FLOAT_TYPE_P (type1))
6655 REAL_VALUE_TYPE orig = TREE_REAL_CST (@10);
6656 if (TYPE_PRECISION (type1) > TYPE_PRECISION (float_type_node)
6657 && exact_real_truncate (TYPE_MODE (float_type_node), &orig))
6658 type1 = float_type_node;
6659 if (TYPE_PRECISION (type1) > TYPE_PRECISION (double_type_node)
6660 && exact_real_truncate (TYPE_MODE (double_type_node), &orig))
6661 type1 = double_type_node;
6664 = (element_precision (TREE_TYPE (@00)) > element_precision (type1)
6665 ? TREE_TYPE (@00) : type1);
6667 (if (element_precision (TREE_TYPE (@0)) > element_precision (newtype))
6668 (cmp (convert:newtype @00) (convert:newtype @10))))))))
6673 /* SSA names are canonicalized to 2nd place. */
6674 (cmp addr@0 SSA_NAME@1)
6677 poly_int64 off; tree base;
6678 tree addr = (TREE_CODE (@0) == SSA_NAME
6679 ? gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0)) : @0);
6681 /* A local variable can never be pointed to by
6682 the default SSA name of an incoming parameter. */
6683 (if (SSA_NAME_IS_DEFAULT_DEF (@1)
6684 && TREE_CODE (SSA_NAME_VAR (@1)) == PARM_DECL
6685 && (base = get_base_address (TREE_OPERAND (addr, 0)))
6686 && TREE_CODE (base) == VAR_DECL
6687 && auto_var_in_fn_p (base, current_function_decl))
6688 (if (cmp == NE_EXPR)
6689 { constant_boolean_node (true, type); }
6690 { constant_boolean_node (false, type); })
6691 /* If the address is based on @1 decide using the offset. */
6692 (if ((base = get_addr_base_and_unit_offset (TREE_OPERAND (addr, 0), &off))
6693 && TREE_CODE (base) == MEM_REF
6694 && TREE_OPERAND (base, 0) == @1)
6695 (with { off += mem_ref_offset (base).force_shwi (); }
6696 (if (known_ne (off, 0))
6697 { constant_boolean_node (cmp == NE_EXPR, type); }
6698 (if (known_eq (off, 0))
6699 { constant_boolean_node (cmp == EQ_EXPR, type); }))))))))
6701 /* Equality compare simplifications from fold_binary */
6704 /* If we have (A | C) == D where C & ~D != 0, convert this into 0.
6705 Similarly for NE_EXPR. */
6707 (cmp (convert?@3 (bit_ior @0 INTEGER_CST@1)) INTEGER_CST@2)
6708 (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0))
6709 && wi::bit_and_not (wi::to_wide (@1), wi::to_wide (@2)) != 0)
6710 { constant_boolean_node (cmp == NE_EXPR, type); }))
6712 /* (X ^ Y) == 0 becomes X == Y, and (X ^ Y) != 0 becomes X != Y. */
6714 (cmp (bit_xor @0 @1) integer_zerop)
6717 /* (X ^ Y) == Y becomes X == 0.
6718 Likewise (X ^ Y) == X becomes Y == 0. */
6720 (cmp:c (bit_xor:c @0 @1) @0)
6721 (cmp @1 { build_zero_cst (TREE_TYPE (@1)); }))
6723 /* (X & Y) == X becomes (X & ~Y) == 0. */
6725 (cmp:c (bit_and:c @0 @1) @0)
6726 (cmp (bit_and @0 (bit_not! @1)) { build_zero_cst (TREE_TYPE (@0)); }))
6728 (cmp:c (convert@3 (bit_and (convert@2 @0) INTEGER_CST@1)) (convert @0))
6729 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6730 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
6731 && INTEGRAL_TYPE_P (TREE_TYPE (@3))
6732 && TYPE_PRECISION (TREE_TYPE (@2)) == TYPE_PRECISION (TREE_TYPE (@0))
6733 && TYPE_PRECISION (TREE_TYPE (@3)) > TYPE_PRECISION (TREE_TYPE (@2))
6734 && !wi::neg_p (wi::to_wide (@1)))
6735 (cmp (bit_and @0 (convert (bit_not @1)))
6736 { build_zero_cst (TREE_TYPE (@0)); })))
6738 /* (X | Y) == Y becomes (X & ~Y) == 0. */
6740 (cmp:c (bit_ior:c @0 @1) @1)
6741 (cmp (bit_and @0 (bit_not! @1)) { build_zero_cst (TREE_TYPE (@0)); }))
6743 /* (X ^ C1) op C2 can be rewritten as X op (C1 ^ C2). */
6745 (cmp (convert?@3 (bit_xor @0 INTEGER_CST@1)) INTEGER_CST@2)
6746 (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0)))
6747 (cmp @0 (bit_xor @1 (convert @2)))))
6750 (cmp (nop_convert? @0) integer_zerop)
6751 (if (tree_expr_nonzero_p (@0))
6752 { constant_boolean_node (cmp == NE_EXPR, type); }))
6754 /* (X & C) op (Y & C) into (X ^ Y) & C op 0. */
6756 (cmp (bit_and:cs @0 @2) (bit_and:cs @1 @2))
6757 (cmp (bit_and (bit_xor @0 @1) @2) { build_zero_cst (TREE_TYPE (@2)); })))
6759 /* (X < 0) != (Y < 0) into (X ^ Y) < 0.
6760 (X >= 0) != (Y >= 0) into (X ^ Y) < 0.
6761 (X < 0) == (Y < 0) into (X ^ Y) >= 0.
6762 (X >= 0) == (Y >= 0) into (X ^ Y) >= 0. */
6767 (cmp (sgncmp @0 integer_zerop@2) (sgncmp @1 integer_zerop))
6768 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
6769 && !TYPE_UNSIGNED (TREE_TYPE (@0))
6770 && types_match (@0, @1))
6771 (ncmp (bit_xor @0 @1) @2)))))
6772 /* (X < 0) == (Y >= 0) into (X ^ Y) < 0.
6773 (X < 0) != (Y >= 0) into (X ^ Y) >= 0. */
6777 (cmp:c (lt @0 integer_zerop@2) (ge @1 integer_zerop))
6778 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
6779 && !TYPE_UNSIGNED (TREE_TYPE (@0))
6780 && types_match (@0, @1))
6781 (ncmp (bit_xor @0 @1) @2))))
6783 /* If we have (A & C) == C where C is a power of 2, convert this into
6784 (A & C) != 0. Similarly for NE_EXPR. */
6788 (cmp (bit_and@2 @0 integer_pow2p@1) @1)
6789 (icmp @2 { build_zero_cst (TREE_TYPE (@0)); })))
6792 /* From fold_binary_op_with_conditional_arg handle the case of
6793 rewriting (a ? b : c) > d to a ? (b > d) : (c > d) when the
6794 compares simplify. */
6795 (for cmp (simple_comparison)
6797 (cmp:c (cond @0 @1 @2) @3)
6798 /* Do not move possibly trapping operations into the conditional as this
6799 pessimizes code and causes gimplification issues when applied late. */
6800 (if (!FLOAT_TYPE_P (TREE_TYPE (@3))
6801 || !operation_could_trap_p (cmp, true, false, @3))
6802 (cond @0 (cmp! @1 @3) (cmp! @2 @3)))))
6806 /* x < 0 ? ~y : y into (x >> (prec-1)) ^ y. */
6807 /* x >= 0 ? ~y : y into ~((x >> (prec-1)) ^ y). */
6809 (cond (cmp @0 integer_zerop) (bit_not @1) @1)
6810 (if (INTEGRAL_TYPE_P (type)
6811 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
6812 && !TYPE_UNSIGNED (TREE_TYPE (@0))
6813 && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (type))
6816 tree shifter = build_int_cst (integer_type_node, TYPE_PRECISION (type) - 1);
6818 (if (cmp == LT_EXPR)
6819 (bit_xor (convert (rshift @0 {shifter;})) @1)
6820 (bit_not (bit_xor (convert (rshift @0 {shifter;})) @1))))))
6821 /* x < 0 ? y : ~y into ~((x >> (prec-1)) ^ y). */
6822 /* x >= 0 ? y : ~y into (x >> (prec-1)) ^ y. */
6824 (cond (cmp @0 integer_zerop) @1 (bit_not @1))
6825 (if (INTEGRAL_TYPE_P (type)
6826 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
6827 && !TYPE_UNSIGNED (TREE_TYPE (@0))
6828 && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (type))
6831 tree shifter = build_int_cst (integer_type_node, TYPE_PRECISION (type) - 1);
6833 (if (cmp == GE_EXPR)
6834 (bit_xor (convert (rshift @0 {shifter;})) @1)
6835 (bit_not (bit_xor (convert (rshift @0 {shifter;})) @1)))))))
6837 /* If we have (A & C) != 0 ? D : 0 where C and D are powers of 2,
6838 convert this into a shift followed by ANDing with D. */
6841 (ne (bit_and @0 integer_pow2p@1) integer_zerop)
6842 INTEGER_CST@2 integer_zerop)
6843 (if (!POINTER_TYPE_P (type) && integer_pow2p (@2))
6845 int shift = (wi::exact_log2 (wi::to_wide (@2))
6846 - wi::exact_log2 (wi::to_wide (@1)));
6850 (lshift (convert @0) { build_int_cst (integer_type_node, shift); }) @2)
6852 (convert (rshift @0 { build_int_cst (integer_type_node, -shift); }))
6855 /* If we have (A & C) != 0 where C is the sign bit of A, convert
6856 this into A < 0. Similarly for (A & C) == 0 into A >= 0. */
6860 (cmp (bit_and (convert?@2 @0) integer_pow2p@1) integer_zerop)
6861 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6862 && type_has_mode_precision_p (TREE_TYPE (@0))
6863 && element_precision (@2) >= element_precision (@0)
6864 && wi::only_sign_bit_p (wi::to_wide (@1), element_precision (@0)))
6865 (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
6866 (ncmp (convert:stype @0) { build_zero_cst (stype); })))))
6868 /* If we have A < 0 ? C : 0 where C is a power of 2, convert
6869 this into a right shift or sign extension followed by ANDing with C. */
6872 (lt @0 integer_zerop)
6873 INTEGER_CST@1 integer_zerop)
6874 (if (integer_pow2p (@1)
6875 && !TYPE_UNSIGNED (TREE_TYPE (@0)))
6877 int shift = element_precision (@0) - wi::exact_log2 (wi::to_wide (@1)) - 1;
6881 (convert (rshift @0 { build_int_cst (integer_type_node, shift); }))
6883 /* Otherwise ctype must be wider than TREE_TYPE (@0) and pure
6884 sign extension followed by AND with C will achieve the effect. */
6885 (bit_and (convert @0) @1)))))
6887 /* When the addresses are not directly of decls compare base and offset.
6888 This implements some remaining parts of fold_comparison address
6889 comparisons but still no complete part of it. Still it is good
6890 enough to make fold_stmt not regress when not dispatching to fold_binary. */
6891 (for cmp (simple_comparison)
6893 (cmp (convert1?@2 addr@0) (convert2? addr@1))
6896 poly_int64 off0, off1;
6898 int equal = address_compare (cmp, TREE_TYPE (@2), @0, @1, base0, base1,
6899 off0, off1, GENERIC);
6903 (if (cmp == EQ_EXPR && (known_eq (off0, off1) || known_ne (off0, off1)))
6904 { constant_boolean_node (known_eq (off0, off1), type); })
6905 (if (cmp == NE_EXPR && (known_eq (off0, off1) || known_ne (off0, off1)))
6906 { constant_boolean_node (known_ne (off0, off1), type); })
6907 (if (cmp == LT_EXPR && (known_lt (off0, off1) || known_ge (off0, off1)))
6908 { constant_boolean_node (known_lt (off0, off1), type); })
6909 (if (cmp == LE_EXPR && (known_le (off0, off1) || known_gt (off0, off1)))
6910 { constant_boolean_node (known_le (off0, off1), type); })
6911 (if (cmp == GE_EXPR && (known_ge (off0, off1) || known_lt (off0, off1)))
6912 { constant_boolean_node (known_ge (off0, off1), type); })
6913 (if (cmp == GT_EXPR && (known_gt (off0, off1) || known_le (off0, off1)))
6914 { constant_boolean_node (known_gt (off0, off1), type); }))
6917 (if (cmp == EQ_EXPR)
6918 { constant_boolean_node (false, type); })
6919 (if (cmp == NE_EXPR)
6920 { constant_boolean_node (true, type); })))))))
6923 /* a?~t:t -> (-(a))^t */
6926 (with { bool wascmp; }
6927 (if (INTEGRAL_TYPE_P (type)
6928 && bitwise_inverted_equal_p (@1, @2, wascmp)
6929 && (!wascmp || TYPE_PRECISION (type) == 1))
6930 (if ((!TYPE_UNSIGNED (type) && TREE_CODE (type) == BOOLEAN_TYPE)
6931 || TYPE_PRECISION (type) == 1)
6932 (bit_xor (convert:type @0) @2)
6933 (bit_xor (negate (convert:type @0)) @2)))))
6936 /* Simplify pointer equality compares using PTA. */
6940 (if (POINTER_TYPE_P (TREE_TYPE (@0))
6941 && ptrs_compare_unequal (@0, @1))
6942 { constant_boolean_node (neeq != EQ_EXPR, type); })))
6944 /* PR70920: Transform (intptr_t)x eq/ne CST to x eq/ne (typeof x) CST.
6945 and (typeof ptr_cst) x eq/ne ptr_cst to x eq/ne (typeof x) CST.
6946 Disable the transform if either operand is pointer to function.
6947 This broke pr22051-2.c for arm where function pointer
6948 canonicalizaion is not wanted. */
6952 (cmp (convert @0) INTEGER_CST@1)
6953 (if (((POINTER_TYPE_P (TREE_TYPE (@0))
6954 && !FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@0)))
6955 && INTEGRAL_TYPE_P (TREE_TYPE (@1))
6956 /* Don't perform this optimization in GENERIC if @0 has reference
6957 type when sanitizing. See PR101210. */
6959 && TREE_CODE (TREE_TYPE (@0)) == REFERENCE_TYPE
6960 && (flag_sanitize & (SANITIZE_NULL | SANITIZE_ALIGNMENT))))
6961 || (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6962 && POINTER_TYPE_P (TREE_TYPE (@1))
6963 && !FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@1)))))
6964 && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
6965 (cmp @0 (convert @1)))))
6967 /* Non-equality compare simplifications from fold_binary */
6968 (for cmp (lt gt le ge)
6969 /* Comparisons with the highest or lowest possible integer of
6970 the specified precision will have known values. */
6972 (cmp (convert?@2 @0) uniform_integer_cst_p@1)
6973 (if ((INTEGRAL_TYPE_P (TREE_TYPE (@1))
6974 || POINTER_TYPE_P (TREE_TYPE (@1))
6975 || VECTOR_INTEGER_TYPE_P (TREE_TYPE (@1)))
6976 && tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@0)))
6979 tree cst = uniform_integer_cst_p (@1);
6980 tree arg1_type = TREE_TYPE (cst);
6981 unsigned int prec = TYPE_PRECISION (arg1_type);
6982 wide_int max = wi::max_value (arg1_type);
6983 wide_int signed_max = wi::max_value (prec, SIGNED);
6984 wide_int min = wi::min_value (arg1_type);
6987 (if (wi::to_wide (cst) == max)
6989 (if (cmp == GT_EXPR)
6990 { constant_boolean_node (false, type); })
6991 (if (cmp == GE_EXPR)
6993 (if (cmp == LE_EXPR)
6994 { constant_boolean_node (true, type); })
6995 (if (cmp == LT_EXPR)
6997 (if (wi::to_wide (cst) == min)
6999 (if (cmp == LT_EXPR)
7000 { constant_boolean_node (false, type); })
7001 (if (cmp == LE_EXPR)
7003 (if (cmp == GE_EXPR)
7004 { constant_boolean_node (true, type); })
7005 (if (cmp == GT_EXPR)
7007 (if (wi::to_wide (cst) == max - 1)
7009 (if (cmp == GT_EXPR)
7010 (eq @2 { build_uniform_cst (TREE_TYPE (@1),
7011 wide_int_to_tree (TREE_TYPE (cst),
7014 (if (cmp == LE_EXPR)
7015 (ne @2 { build_uniform_cst (TREE_TYPE (@1),
7016 wide_int_to_tree (TREE_TYPE (cst),
7019 (if (wi::to_wide (cst) == min + 1)
7021 (if (cmp == GE_EXPR)
7022 (ne @2 { build_uniform_cst (TREE_TYPE (@1),
7023 wide_int_to_tree (TREE_TYPE (cst),
7026 (if (cmp == LT_EXPR)
7027 (eq @2 { build_uniform_cst (TREE_TYPE (@1),
7028 wide_int_to_tree (TREE_TYPE (cst),
7031 (if (wi::to_wide (cst) == signed_max
7032 && TYPE_UNSIGNED (arg1_type)
7033 && TYPE_MODE (arg1_type) != BLKmode
7034 /* We will flip the signedness of the comparison operator
7035 associated with the mode of @1, so the sign bit is
7036 specified by this mode. Check that @1 is the signed
7037 max associated with this sign bit. */
7038 && prec == GET_MODE_PRECISION (SCALAR_INT_TYPE_MODE (arg1_type))
7039 /* signed_type does not work on pointer types. */
7040 && INTEGRAL_TYPE_P (arg1_type))
7041 /* The following case also applies to X < signed_max+1
7042 and X >= signed_max+1 because previous transformations. */
7043 (if (cmp == LE_EXPR || cmp == GT_EXPR)
7044 (with { tree st = signed_type_for (TREE_TYPE (@1)); }
7046 (if (cst == @1 && cmp == LE_EXPR)
7047 (ge (convert:st @0) { build_zero_cst (st); }))
7048 (if (cst == @1 && cmp == GT_EXPR)
7049 (lt (convert:st @0) { build_zero_cst (st); }))
7050 (if (cmp == LE_EXPR)
7051 (ge (view_convert:st @0) { build_zero_cst (st); }))
7052 (if (cmp == GT_EXPR)
7053 (lt (view_convert:st @0) { build_zero_cst (st); })))))))))))
7055 /* unsigned < (typeof unsigned)(unsigned != 0) is always false. */
7057 (lt:c @0 (convert (ne @0 integer_zerop)))
7058 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
7059 { constant_boolean_node (false, type); }))
7061 /* x != (typeof x)(x == CST) -> CST == 0 ? 1 : (CST == 1 ? (x!=0&&x!=1) : x != 0) */
7062 /* x != (typeof x)(x != CST) -> CST == 1 ? 1 : (CST == 0 ? (x!=0&&x!=1) : x != 1) */
7063 /* x == (typeof x)(x == CST) -> CST == 0 ? 0 : (CST == 1 ? (x==0||x==1) : x == 0) */
7064 /* x == (typeof x)(x != CST) -> CST == 1 ? 0 : (CST == 0 ? (x==0||x==1) : x == 1) */
7068 (outer:c @0 (convert (inner @0 INTEGER_CST@1)))
7070 bool cst1 = integer_onep (@1);
7071 bool cst0 = integer_zerop (@1);
7072 bool innereq = inner == EQ_EXPR;
7073 bool outereq = outer == EQ_EXPR;
7076 (if (innereq ? cst0 : cst1)
7077 { constant_boolean_node (!outereq, type); })
7078 (if (innereq ? cst1 : cst0)
7080 tree utype = unsigned_type_for (TREE_TYPE (@0));
7081 tree ucst1 = build_one_cst (utype);
7084 (gt (convert:utype @0) { ucst1; })
7085 (le (convert:utype @0) { ucst1; })
7090 tree value = build_int_cst (TREE_TYPE (@0), !innereq);
7103 (for cmp (unordered ordered unlt unle ungt unge uneq ltgt)
7104 /* If the second operand is NaN, the result is constant. */
7107 (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
7108 && (cmp != LTGT_EXPR || ! flag_trapping_math))
7109 { constant_boolean_node (cmp == ORDERED_EXPR || cmp == LTGT_EXPR
7110 ? false : true, type); })))
7112 /* Fold UNORDERED if either operand must be NaN, or neither can be. */
7116 (if (tree_expr_nan_p (@0) || tree_expr_nan_p (@1))
7117 { constant_boolean_node (true, type); })
7118 (if (!tree_expr_maybe_nan_p (@0) && !tree_expr_maybe_nan_p (@1))
7119 { constant_boolean_node (false, type); })))
7121 /* Fold ORDERED if either operand must be NaN, or neither can be. */
7125 (if (tree_expr_nan_p (@0) || tree_expr_nan_p (@1))
7126 { constant_boolean_node (false, type); })
7127 (if (!tree_expr_maybe_nan_p (@0) && !tree_expr_maybe_nan_p (@1))
7128 { constant_boolean_node (true, type); })))
7130 /* bool_var != 0 becomes bool_var. */
7132 (ne @0 integer_zerop)
7133 (if (TREE_CODE (TREE_TYPE (@0)) == BOOLEAN_TYPE
7134 && types_match (type, TREE_TYPE (@0)))
7136 /* bool_var == 1 becomes bool_var. */
7138 (eq @0 integer_onep)
7139 (if (TREE_CODE (TREE_TYPE (@0)) == BOOLEAN_TYPE
7140 && types_match (type, TREE_TYPE (@0)))
7143 bool_var == 0 becomes !bool_var or
7144 bool_var != 1 becomes !bool_var
7145 here because that only is good in assignment context as long
7146 as we require a tcc_comparison in GIMPLE_CONDs where we'd
7147 replace if (x == 0) with tem = ~x; if (tem != 0) which is
7148 clearly less optimal and which we'll transform again in forwprop. */
7150 /* Transform comparisons of the form (X & Y) CMP 0 to X CMP2 Z
7151 where ~Y + 1 == pow2 and Z = ~Y. */
7152 (for cst (VECTOR_CST INTEGER_CST)
7156 (cmp (bit_and:c@2 @0 cst@1) integer_zerop)
7157 (with { tree csts = bitmask_inv_cst_vector_p (@1); }
7158 (if (csts && (VECTOR_TYPE_P (TREE_TYPE (@1)) || single_use (@2)))
7159 (with { auto optab = VECTOR_TYPE_P (TREE_TYPE (@1))
7160 ? optab_vector : optab_default;
7161 tree utype = unsigned_type_for (TREE_TYPE (@1)); }
7162 (if (target_supports_op_p (utype, icmp, optab)
7163 || (optimize_vectors_before_lowering_p ()
7164 && (!target_supports_op_p (type, cmp, optab)
7165 || !target_supports_op_p (type, BIT_AND_EXPR, optab))))
7166 (if (TYPE_UNSIGNED (TREE_TYPE (@1)))
7168 (icmp (view_convert:utype @0) { csts; })))))))))
7170 /* When one argument is a constant, overflow detection can be simplified.
7171 Currently restricted to single use so as not to interfere too much with
7172 ADD_OVERFLOW detection in tree-ssa-math-opts.cc.
7173 CONVERT?(CONVERT?(A) + CST) CMP A -> A CMP' CST' */
7174 (for cmp (lt le ge gt)
7177 (cmp:c (convert?@3 (plus@2 (convert?@4 @0) INTEGER_CST@1)) @0)
7178 (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@2))
7179 && types_match (TREE_TYPE (@0), TREE_TYPE (@3))
7180 && tree_nop_conversion_p (TREE_TYPE (@4), TREE_TYPE (@0))
7181 && wi::to_wide (@1) != 0
7184 unsigned int prec = TYPE_PRECISION (TREE_TYPE (@0));
7185 signop sign = TYPE_SIGN (TREE_TYPE (@0));
7187 (out @0 { wide_int_to_tree (TREE_TYPE (@0),
7188 wi::max_value (prec, sign)
7189 - wi::to_wide (@1)); })))))
7191 /* To detect overflow in unsigned A - B, A < B is simpler than A - B > A.
7192 However, the detection logic for SUB_OVERFLOW in tree-ssa-math-opts.cc
7193 expects the long form, so we restrict the transformation for now. */
7196 (cmp:c (minus@2 @0 @1) @0)
7197 (if (single_use (@2)
7198 && ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
7199 && TYPE_UNSIGNED (TREE_TYPE (@0)))
7202 /* Optimize A - B + -1 >= A into B >= A for unsigned comparisons. */
7205 (cmp:c (plus (minus @0 @1) integer_minus_onep) @0)
7206 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
7207 && TYPE_UNSIGNED (TREE_TYPE (@0)))
7210 /* Testing for overflow is unnecessary if we already know the result. */
7215 (cmp:c (realpart (IFN_SUB_OVERFLOW@2 @0 @1)) @0)
7216 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
7217 && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
7218 (out (imagpart @2) { build_zero_cst (TREE_TYPE (@0)); }))))
7223 (cmp:c (realpart (IFN_ADD_OVERFLOW:c@2 @0 @1)) @0)
7224 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
7225 && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
7226 (out (imagpart @2) { build_zero_cst (TREE_TYPE (@0)); }))))
7228 /* For unsigned operands, -1 / B < A checks whether A * B would overflow.
7229 Simplify it to __builtin_mul_overflow (A, B, <unused>). */
7233 (cmp:c (trunc_div:s integer_all_onesp @1) @0)
7234 (if (TYPE_UNSIGNED (TREE_TYPE (@0)) && !VECTOR_TYPE_P (TREE_TYPE (@0)))
7235 (with { tree t = TREE_TYPE (@0), cpx = build_complex_type (t); }
7236 (out (imagpart (IFN_MUL_OVERFLOW:cpx @0 @1)) { build_zero_cst (t); })))))
7238 /* Similarly, for unsigned operands, (((type) A * B) >> prec) != 0 where type
7239 is at least twice as wide as type of A and B, simplify to
7240 __builtin_mul_overflow (A, B, <unused>). */
7243 (cmp (rshift (mult:s (convert@3 @0) (convert @1)) INTEGER_CST@2)
7245 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
7246 && INTEGRAL_TYPE_P (TREE_TYPE (@3))
7247 && TYPE_UNSIGNED (TREE_TYPE (@0))
7248 && (TYPE_PRECISION (TREE_TYPE (@3))
7249 >= 2 * TYPE_PRECISION (TREE_TYPE (@0)))
7250 && tree_fits_uhwi_p (@2)
7251 && tree_to_uhwi (@2) == TYPE_PRECISION (TREE_TYPE (@0))
7252 && types_match (@0, @1)
7253 && type_has_mode_precision_p (TREE_TYPE (@0))
7254 && (optab_handler (umulv4_optab, TYPE_MODE (TREE_TYPE (@0)))
7255 != CODE_FOR_nothing))
7256 (with { tree t = TREE_TYPE (@0), cpx = build_complex_type (t); }
7257 (cmp (imagpart (IFN_MUL_OVERFLOW:cpx @0 @1)) { build_zero_cst (t); })))))
7259 /* Demote operands of IFN_{ADD,SUB,MUL}_OVERFLOW. */
7260 (for ovf (IFN_ADD_OVERFLOW IFN_SUB_OVERFLOW IFN_MUL_OVERFLOW)
7262 (ovf (convert@2 @0) @1)
7263 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
7264 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
7265 && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
7266 && (!TYPE_UNSIGNED (TREE_TYPE (@2)) || TYPE_UNSIGNED (TREE_TYPE (@0))))
7269 (ovf @1 (convert@2 @0))
7270 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
7271 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
7272 && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
7273 && (!TYPE_UNSIGNED (TREE_TYPE (@2)) || TYPE_UNSIGNED (TREE_TYPE (@0))))
7276 /* Optimize __builtin_mul_overflow_p (x, cst, (utype) 0) if all 3 types
7277 are unsigned to x > (umax / cst). Similarly for signed type, but
7278 in that case it needs to be outside of a range. */
7280 (imagpart (IFN_MUL_OVERFLOW:cs@2 @0 integer_nonzerop@1))
7281 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
7282 && TYPE_MAX_VALUE (TREE_TYPE (@0))
7283 && types_match (TREE_TYPE (@0), TREE_TYPE (TREE_TYPE (@2)))
7284 && int_fits_type_p (@1, TREE_TYPE (@0)))
7285 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
7286 (convert (gt @0 (trunc_div! { TYPE_MAX_VALUE (TREE_TYPE (@0)); } @1)))
7287 (if (TYPE_MIN_VALUE (TREE_TYPE (@0)))
7288 (if (integer_minus_onep (@1))
7289 (convert (eq @0 { TYPE_MIN_VALUE (TREE_TYPE (@0)); }))
7292 tree div = fold_convert (TREE_TYPE (@0), @1);
7293 tree lo = int_const_binop (TRUNC_DIV_EXPR,
7294 TYPE_MIN_VALUE (TREE_TYPE (@0)), div);
7295 tree hi = int_const_binop (TRUNC_DIV_EXPR,
7296 TYPE_MAX_VALUE (TREE_TYPE (@0)), div);
7297 tree etype = range_check_type (TREE_TYPE (@0));
7300 if (wi::neg_p (wi::to_wide (div)))
7302 lo = fold_convert (etype, lo);
7303 hi = fold_convert (etype, hi);
7304 hi = int_const_binop (MINUS_EXPR, hi, lo);
7308 (convert (gt (minus (convert:etype @0) { lo; }) { hi; })))))))))
7310 /* Simplification of math builtins. These rules must all be optimizations
7311 as well as IL simplifications. If there is a possibility that the new
7312 form could be a pessimization, the rule should go in the canonicalization
7313 section that follows this one.
7315 Rules can generally go in this section if they satisfy one of
7318 - the rule describes an identity
7320 - the rule replaces calls with something as simple as addition or
7323 - the rule contains unary calls only and simplifies the surrounding
7324 arithmetic. (The idea here is to exclude non-unary calls in which
7325 one operand is constant and in which the call is known to be cheap
7326 when the operand has that value.) */
7328 (if (flag_unsafe_math_optimizations)
7329 /* Simplify sqrt(x) * sqrt(x) -> x. */
7331 (mult (SQRT_ALL@1 @0) @1)
7332 (if (!tree_expr_maybe_signaling_nan_p (@0))
7335 (for op (plus minus)
7336 /* Simplify (A / C) +- (B / C) -> (A +- B) / C. */
7340 (rdiv (op @0 @2) @1)))
7342 (for cmp (lt le gt ge)
7343 neg_cmp (gt ge lt le)
7344 /* Simplify (x * C1) cmp C2 -> x cmp (C2 / C1), where C1 != 0. */
7346 (cmp (mult @0 REAL_CST@1) REAL_CST@2)
7348 { tree tem = const_binop (RDIV_EXPR, type, @2, @1); }
7350 && !(REAL_VALUE_ISINF (TREE_REAL_CST (tem))
7351 || (real_zerop (tem) && !real_zerop (@1))))
7353 (if (real_less (&dconst0, TREE_REAL_CST_PTR (@1)))
7355 (if (real_less (TREE_REAL_CST_PTR (@1), &dconst0))
7356 (neg_cmp @0 { tem; })))))))
7358 /* Simplify sqrt(x) * sqrt(y) -> sqrt(x*y). */
7359 (for root (SQRT CBRT)
7361 (mult (root:s @0) (root:s @1))
7362 (root (mult @0 @1))))
7364 /* Simplify expN(x) * expN(y) -> expN(x+y). */
7365 (for exps (EXP EXP2 EXP10 POW10)
7367 (mult (exps:s @0) (exps:s @1))
7368 (exps (plus @0 @1))))
7370 /* Simplify a/root(b/c) into a*root(c/b). */
7371 (for root (SQRT CBRT)
7373 (rdiv @0 (root:s (rdiv:s @1 @2)))
7374 (mult @0 (root (rdiv @2 @1)))))
7376 /* Simplify x/expN(y) into x*expN(-y). */
7377 (for exps (EXP EXP2 EXP10 POW10)
7379 (rdiv @0 (exps:s @1))
7380 (mult @0 (exps (negate @1)))))
7382 (for logs (LOG LOG2 LOG10 LOG10)
7383 exps (EXP EXP2 EXP10 POW10)
7384 /* logN(expN(x)) -> x. */
7388 /* expN(logN(x)) -> x. */
7393 /* Optimize logN(func()) for various exponential functions. We
7394 want to determine the value "x" and the power "exponent" in
7395 order to transform logN(x**exponent) into exponent*logN(x). */
7396 (for logs (LOG LOG LOG LOG2 LOG2 LOG2 LOG10 LOG10)
7397 exps (EXP2 EXP10 POW10 EXP EXP10 POW10 EXP EXP2)
7400 (if (SCALAR_FLOAT_TYPE_P (type))
7406 /* Prepare to do logN(exp(exponent)) -> exponent*logN(e). */
7407 x = build_real_truncate (type, dconst_e ());
7410 /* Prepare to do logN(exp2(exponent)) -> exponent*logN(2). */
7411 x = build_real (type, dconst2);
7415 /* Prepare to do logN(exp10(exponent)) -> exponent*logN(10). */
7417 REAL_VALUE_TYPE dconst10;
7418 real_from_integer (&dconst10, VOIDmode, 10, SIGNED);
7419 x = build_real (type, dconst10);
7426 (mult (logs { x; }) @0)))))
7434 (if (SCALAR_FLOAT_TYPE_P (type))
7440 /* Prepare to do logN(sqrt(x)) -> 0.5*logN(x). */
7441 x = build_real (type, dconsthalf);
7444 /* Prepare to do logN(cbrt(x)) -> (1/3)*logN(x). */
7445 x = build_real_truncate (type, dconst_third ());
7451 (mult { x; } (logs @0))))))
7453 /* logN(pow(x,exponent)) -> exponent*logN(x). */
7454 (for logs (LOG LOG2 LOG10)
7458 (mult @1 (logs @0))))
7460 /* pow(C,x) -> exp(log(C)*x) if C > 0,
7461 or if C is a positive power of 2,
7462 pow(C,x) -> exp2(log2(C)*x). */
7470 (pows REAL_CST@0 @1)
7471 (if (real_compare (GT_EXPR, TREE_REAL_CST_PTR (@0), &dconst0)
7472 && real_isfinite (TREE_REAL_CST_PTR (@0))
7473 /* As libmvec doesn't have a vectorized exp2, defer optimizing
7474 the use_exp2 case until after vectorization. It seems actually
7475 beneficial for all constants to postpone this until later,
7476 because exp(log(C)*x), while faster, will have worse precision
7477 and if x folds into a constant too, that is unnecessary
7479 && canonicalize_math_after_vectorization_p ())
7481 const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (@0);
7482 bool use_exp2 = false;
7483 if (targetm.libc_has_function (function_c99_misc, TREE_TYPE (@0))
7484 && value->cl == rvc_normal)
7486 REAL_VALUE_TYPE frac_rvt = *value;
7487 SET_REAL_EXP (&frac_rvt, 1);
7488 if (real_equal (&frac_rvt, &dconst1))
7493 (if (optimize_pow_to_exp (@0, @1))
7494 (exps (mult (logs @0) @1)))
7495 (exp2s (mult (log2s @0) @1)))))))
7498 /* pow(C,x)*expN(y) -> expN(logN(C)*x+y) if C > 0. */
7500 exps (EXP EXP2 EXP10 POW10)
7501 logs (LOG LOG2 LOG10 LOG10)
7503 (mult:c (pows:s REAL_CST@0 @1) (exps:s @2))
7504 (if (real_compare (GT_EXPR, TREE_REAL_CST_PTR (@0), &dconst0)
7505 && real_isfinite (TREE_REAL_CST_PTR (@0)))
7506 (exps (plus (mult (logs @0) @1) @2)))))
7511 exps (EXP EXP2 EXP10 POW10)
7512 /* sqrt(expN(x)) -> expN(x*0.5). */
7515 (exps (mult @0 { build_real (type, dconsthalf); })))
7516 /* cbrt(expN(x)) -> expN(x/3). */
7519 (exps (mult @0 { build_real_truncate (type, dconst_third ()); })))
7520 /* pow(expN(x), y) -> expN(x*y). */
7523 (exps (mult @0 @1))))
7525 /* tan(atan(x)) -> x. */
7532 /* Simplify sin(atan(x)) -> x / sqrt(x*x + 1). */
7536 copysigns (COPYSIGN)
7541 REAL_VALUE_TYPE r_cst;
7542 build_sinatan_real (&r_cst, type);
7543 tree t_cst = build_real (type, r_cst);
7544 tree t_one = build_one_cst (type);
7546 (if (SCALAR_FLOAT_TYPE_P (type))
7547 (cond (lt (abs @0) { t_cst; })
7548 (rdiv @0 (sqrts (plus (mult @0 @0) { t_one; })))
7549 (copysigns { t_one; } @0))))))
7551 /* Simplify cos(atan(x)) -> 1 / sqrt(x*x + 1). */
7555 copysigns (COPYSIGN)
7560 REAL_VALUE_TYPE r_cst;
7561 build_sinatan_real (&r_cst, type);
7562 tree t_cst = build_real (type, r_cst);
7563 tree t_one = build_one_cst (type);
7564 tree t_zero = build_zero_cst (type);
7566 (if (SCALAR_FLOAT_TYPE_P (type))
7567 (cond (lt (abs @0) { t_cst; })
7568 (rdiv { t_one; } (sqrts (plus (mult @0 @0) { t_one; })))
7569 (copysigns { t_zero; } @0))))))
7571 (if (!flag_errno_math)
7572 /* Simplify sinh(atanh(x)) -> x / sqrt((1 - x)*(1 + x)). */
7577 (sinhs (atanhs:s @0))
7578 (with { tree t_one = build_one_cst (type); }
7579 (rdiv @0 (sqrts (mult (minus { t_one; } @0) (plus { t_one; } @0)))))))
7581 /* Simplify cosh(atanh(x)) -> 1 / sqrt((1 - x)*(1 + x)) */
7586 (coshs (atanhs:s @0))
7587 (with { tree t_one = build_one_cst (type); }
7588 (rdiv { t_one; } (sqrts (mult (minus { t_one; } @0) (plus { t_one; } @0))))))))
7590 /* cabs(x+0i) or cabs(0+xi) -> abs(x). */
7592 (CABS (complex:C @0 real_zerop@1))
7595 /* trunc(trunc(x)) -> trunc(x), etc. */
7596 (for fns (TRUNC_ALL FLOOR_ALL CEIL_ALL ROUND_ALL NEARBYINT_ALL RINT_ALL)
7600 /* f(x) -> x if x is integer valued and f does nothing for such values. */
7601 (for fns (TRUNC_ALL FLOOR_ALL CEIL_ALL ROUND_ALL NEARBYINT_ALL RINT_ALL)
7603 (fns integer_valued_real_p@0)
7606 /* hypot(x,0) and hypot(0,x) -> abs(x). */
7608 (HYPOT:c @0 real_zerop@1)
7611 /* pow(1,x) -> 1. */
7613 (POW real_onep@0 @1)
7617 /* copysign(x,x) -> x. */
7618 (COPYSIGN_ALL @0 @0)
7622 /* copysign(x,-x) -> -x. */
7623 (COPYSIGN_ALL @0 (negate@1 @0))
7627 /* copysign(x,y) -> fabs(x) if y is nonnegative. */
7628 (COPYSIGN_ALL @0 tree_expr_nonnegative_p@1)
7632 /* fabs (copysign(x, y)) -> fabs (x). */
7633 (abs (COPYSIGN_ALL @0 @1))
7636 (for scale (LDEXP SCALBN SCALBLN)
7637 /* ldexp(0, x) -> 0. */
7639 (scale real_zerop@0 @1)
7641 /* ldexp(x, 0) -> x. */
7643 (scale @0 integer_zerop@1)
7645 /* ldexp(x, y) -> x if x is +-Inf or NaN. */
7647 (scale REAL_CST@0 @1)
7648 (if (!real_isfinite (TREE_REAL_CST_PTR (@0)))
7651 /* Canonicalization of sequences of math builtins. These rules represent
7652 IL simplifications but are not necessarily optimizations.
7654 The sincos pass is responsible for picking "optimal" implementations
7655 of math builtins, which may be more complicated and can sometimes go
7656 the other way, e.g. converting pow into a sequence of sqrts.
7657 We only want to do these canonicalizations before the pass has run. */
7659 (if (flag_unsafe_math_optimizations && canonicalize_math_p ())
7660 /* Simplify tan(x) * cos(x) -> sin(x). */
7662 (mult:c (TAN:s @0) (COS:s @0))
7665 /* Simplify x * pow(x,c) -> pow(x,c+1). */
7667 (mult:c @0 (POW:s @0 REAL_CST@1))
7668 (if (!TREE_OVERFLOW (@1))
7669 (POW @0 (plus @1 { build_one_cst (type); }))))
7671 /* Simplify sin(x) / cos(x) -> tan(x). */
7673 (rdiv (SIN:s @0) (COS:s @0))
7676 /* Simplify sinh(x) / cosh(x) -> tanh(x). */
7678 (rdiv (SINH:s @0) (COSH:s @0))
7681 /* Simplify tanh (x) / sinh (x) -> 1.0 / cosh (x). */
7683 (rdiv (TANH:s @0) (SINH:s @0))
7684 (rdiv {build_one_cst (type);} (COSH @0)))
7686 /* Simplify cos(x) / sin(x) -> 1 / tan(x). */
7688 (rdiv (COS:s @0) (SIN:s @0))
7689 (rdiv { build_one_cst (type); } (TAN @0)))
7691 /* Simplify sin(x) / tan(x) -> cos(x). */
7693 (rdiv (SIN:s @0) (TAN:s @0))
7694 (if (! HONOR_NANS (@0)
7695 && ! HONOR_INFINITIES (@0))
7698 /* Simplify tan(x) / sin(x) -> 1.0 / cos(x). */
7700 (rdiv (TAN:s @0) (SIN:s @0))
7701 (if (! HONOR_NANS (@0)
7702 && ! HONOR_INFINITIES (@0))
7703 (rdiv { build_one_cst (type); } (COS @0))))
7705 /* Simplify pow(x,y) * pow(x,z) -> pow(x,y+z). */
7707 (mult (POW:s @0 @1) (POW:s @0 @2))
7708 (POW @0 (plus @1 @2)))
7710 /* Simplify pow(x,y) * pow(z,y) -> pow(x*z,y). */
7712 (mult (POW:s @0 @1) (POW:s @2 @1))
7713 (POW (mult @0 @2) @1))
7715 /* Simplify powi(x,y) * powi(z,y) -> powi(x*z,y). */
7717 (mult (POWI:s @0 @1) (POWI:s @2 @1))
7718 (POWI (mult @0 @2) @1))
7720 /* Simplify pow(x,c) / x -> pow(x,c-1). */
7722 (rdiv (POW:s @0 REAL_CST@1) @0)
7723 (if (!TREE_OVERFLOW (@1))
7724 (POW @0 (minus @1 { build_one_cst (type); }))))
7726 /* Simplify x / pow (y,z) -> x * pow(y,-z). */
7728 (rdiv @0 (POW:s @1 @2))
7729 (mult @0 (POW @1 (negate @2))))
7734 /* sqrt(sqrt(x)) -> pow(x,1/4). */
7737 (pows @0 { build_real (type, dconst_quarter ()); }))
7738 /* sqrt(cbrt(x)) -> pow(x,1/6). */
7741 (pows @0 { build_real_truncate (type, dconst_sixth ()); }))
7742 /* cbrt(sqrt(x)) -> pow(x,1/6). */
7745 (pows @0 { build_real_truncate (type, dconst_sixth ()); }))
7746 /* cbrt(cbrt(x)) -> pow(x,1/9), iff x is nonnegative. */
7748 (cbrts (cbrts tree_expr_nonnegative_p@0))
7749 (pows @0 { build_real_truncate (type, dconst_ninth ()); }))
7750 /* sqrt(pow(x,y)) -> pow(|x|,y*0.5). */
7752 (sqrts (pows @0 @1))
7753 (pows (abs @0) (mult @1 { build_real (type, dconsthalf); })))
7754 /* cbrt(pow(x,y)) -> pow(x,y/3), iff x is nonnegative. */
7756 (cbrts (pows tree_expr_nonnegative_p@0 @1))
7757 (pows @0 (mult @1 { build_real_truncate (type, dconst_third ()); })))
7758 /* pow(sqrt(x),y) -> pow(x,y*0.5). */
7760 (pows (sqrts @0) @1)
7761 (pows @0 (mult @1 { build_real (type, dconsthalf); })))
7762 /* pow(cbrt(x),y) -> pow(x,y/3) iff x is nonnegative. */
7764 (pows (cbrts tree_expr_nonnegative_p@0) @1)
7765 (pows @0 (mult @1 { build_real_truncate (type, dconst_third ()); })))
7766 /* pow(pow(x,y),z) -> pow(x,y*z) iff x is nonnegative. */
7768 (pows (pows tree_expr_nonnegative_p@0 @1) @2)
7769 (pows @0 (mult @1 @2))))
7771 /* cabs(x+xi) -> fabs(x)*sqrt(2). */
7773 (CABS (complex @0 @0))
7774 (mult (abs @0) { build_real_truncate (type, dconst_sqrt2 ()); }))
7776 /* hypot(x,x) -> fabs(x)*sqrt(2). */
7779 (mult (abs @0) { build_real_truncate (type, dconst_sqrt2 ()); }))
7781 /* cexp(x+yi) -> exp(x)*cexpi(y). */
7786 (cexps compositional_complex@0)
7787 (if (targetm.libc_has_function (function_c99_math_complex, TREE_TYPE (@0)))
7789 (mult (exps@1 (realpart @0)) (realpart (cexpis:type@2 (imagpart @0))))
7790 (mult @1 (imagpart @2)))))))
7792 (if (canonicalize_math_p ())
7793 /* floor(x) -> trunc(x) if x is nonnegative. */
7794 (for floors (FLOOR_ALL)
7797 (floors tree_expr_nonnegative_p@0)
7800 (match double_value_p
7802 (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == double_type_node)))
7803 (for froms (BUILT_IN_TRUNCL
7815 /* truncl(extend(x)) -> extend(trunc(x)), etc., if x is a double. */
7816 (if (optimize && canonicalize_math_p ())
7818 (froms (convert double_value_p@0))
7819 (convert (tos @0)))))
7821 (match float_value_p
7823 (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == float_type_node)))
7824 (for froms (BUILT_IN_TRUNCL BUILT_IN_TRUNC
7825 BUILT_IN_FLOORL BUILT_IN_FLOOR
7826 BUILT_IN_CEILL BUILT_IN_CEIL
7827 BUILT_IN_ROUNDL BUILT_IN_ROUND
7828 BUILT_IN_NEARBYINTL BUILT_IN_NEARBYINT
7829 BUILT_IN_RINTL BUILT_IN_RINT)
7830 tos (BUILT_IN_TRUNCF BUILT_IN_TRUNCF
7831 BUILT_IN_FLOORF BUILT_IN_FLOORF
7832 BUILT_IN_CEILF BUILT_IN_CEILF
7833 BUILT_IN_ROUNDF BUILT_IN_ROUNDF
7834 BUILT_IN_NEARBYINTF BUILT_IN_NEARBYINTF
7835 BUILT_IN_RINTF BUILT_IN_RINTF)
7836 /* truncl(extend(x)) and trunc(extend(x)) -> extend(truncf(x)), etc.,
7838 (if (optimize && canonicalize_math_p ()
7839 && targetm.libc_has_function (function_c99_misc, NULL_TREE))
7841 (froms (convert float_value_p@0))
7842 (convert (tos @0)))))
7845 (match float16_value_p
7847 (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == float16_type_node)))
7848 (for froms (BUILT_IN_TRUNCL BUILT_IN_TRUNC BUILT_IN_TRUNCF
7849 BUILT_IN_FLOORL BUILT_IN_FLOOR BUILT_IN_FLOORF
7850 BUILT_IN_CEILL BUILT_IN_CEIL BUILT_IN_CEILF
7851 BUILT_IN_ROUNDEVENL BUILT_IN_ROUNDEVEN BUILT_IN_ROUNDEVENF
7852 BUILT_IN_ROUNDL BUILT_IN_ROUND BUILT_IN_ROUNDF
7853 BUILT_IN_NEARBYINTL BUILT_IN_NEARBYINT BUILT_IN_NEARBYINTF
7854 BUILT_IN_RINTL BUILT_IN_RINT BUILT_IN_RINTF
7855 BUILT_IN_SQRTL BUILT_IN_SQRT BUILT_IN_SQRTF)
7856 tos (IFN_TRUNC IFN_TRUNC IFN_TRUNC
7857 IFN_FLOOR IFN_FLOOR IFN_FLOOR
7858 IFN_CEIL IFN_CEIL IFN_CEIL
7859 IFN_ROUNDEVEN IFN_ROUNDEVEN IFN_ROUNDEVEN
7860 IFN_ROUND IFN_ROUND IFN_ROUND
7861 IFN_NEARBYINT IFN_NEARBYINT IFN_NEARBYINT
7862 IFN_RINT IFN_RINT IFN_RINT
7863 IFN_SQRT IFN_SQRT IFN_SQRT)
7864 /* (_Float16) round ((doube) x) -> __built_in_roundf16 (x), etc.,
7865 if x is a _Float16. */
7867 (convert (froms (convert float16_value_p@0)))
7869 && types_match (type, TREE_TYPE (@0))
7870 && direct_internal_fn_supported_p (as_internal_fn (tos),
7871 type, OPTIMIZE_FOR_BOTH))
7874 /* Simplify (trunc)copysign ((extend)x, (extend)y) to copysignf (x, y),
7875 x,y is float value, similar for _Float16/double. */
7876 (for copysigns (COPYSIGN_ALL)
7878 (convert (copysigns (convert@2 @0) (convert @1)))
7880 && !HONOR_SNANS (@2)
7881 && types_match (type, TREE_TYPE (@0))
7882 && types_match (type, TREE_TYPE (@1))
7883 && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (@2))
7884 && direct_internal_fn_supported_p (IFN_COPYSIGN,
7885 type, OPTIMIZE_FOR_BOTH))
7886 (IFN_COPYSIGN @0 @1))))
7888 (for froms (BUILT_IN_FMAF BUILT_IN_FMA BUILT_IN_FMAL)
7889 tos (IFN_FMA IFN_FMA IFN_FMA)
7891 (convert (froms (convert@3 @0) (convert @1) (convert @2)))
7892 (if (flag_unsafe_math_optimizations
7894 && FLOAT_TYPE_P (type)
7895 && FLOAT_TYPE_P (TREE_TYPE (@3))
7896 && types_match (type, TREE_TYPE (@0))
7897 && types_match (type, TREE_TYPE (@1))
7898 && types_match (type, TREE_TYPE (@2))
7899 && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (@3))
7900 && direct_internal_fn_supported_p (as_internal_fn (tos),
7901 type, OPTIMIZE_FOR_BOTH))
7904 (for maxmin (max min)
7906 (convert (maxmin (convert@2 @0) (convert @1)))
7908 && FLOAT_TYPE_P (type)
7909 && FLOAT_TYPE_P (TREE_TYPE (@2))
7910 && types_match (type, TREE_TYPE (@0))
7911 && types_match (type, TREE_TYPE (@1))
7912 && element_precision (type) < element_precision (TREE_TYPE (@2)))
7916 (for froms (XFLOORL XCEILL XROUNDL XRINTL)
7917 tos (XFLOOR XCEIL XROUND XRINT)
7918 /* llfloorl(extend(x)) -> llfloor(x), etc., if x is a double. */
7919 (if (optimize && canonicalize_math_p ())
7921 (froms (convert double_value_p@0))
7924 (for froms (XFLOORL XCEILL XROUNDL XRINTL
7925 XFLOOR XCEIL XROUND XRINT)
7926 tos (XFLOORF XCEILF XROUNDF XRINTF)
7927 /* llfloorl(extend(x)) and llfloor(extend(x)) -> llfloorf(x), etc.,
7929 (if (optimize && canonicalize_math_p ())
7931 (froms (convert float_value_p@0))
7934 (if (canonicalize_math_p ())
7935 /* xfloor(x) -> fix_trunc(x) if x is nonnegative. */
7936 (for floors (IFLOOR LFLOOR LLFLOOR)
7938 (floors tree_expr_nonnegative_p@0)
7941 (if (canonicalize_math_p ())
7942 /* xfloor(x) -> fix_trunc(x), etc., if x is integer valued. */
7943 (for fns (IFLOOR LFLOOR LLFLOOR
7945 IROUND LROUND LLROUND)
7947 (fns integer_valued_real_p@0)
7949 (if (!flag_errno_math)
7950 /* xrint(x) -> fix_trunc(x), etc., if x is integer valued. */
7951 (for rints (IRINT LRINT LLRINT)
7953 (rints integer_valued_real_p@0)
7956 (if (canonicalize_math_p ())
7957 (for ifn (IFLOOR ICEIL IROUND IRINT)
7958 lfn (LFLOOR LCEIL LROUND LRINT)
7959 llfn (LLFLOOR LLCEIL LLROUND LLRINT)
7960 /* Canonicalize iround (x) to lround (x) on ILP32 targets where
7961 sizeof (int) == sizeof (long). */
7962 (if (TYPE_PRECISION (integer_type_node)
7963 == TYPE_PRECISION (long_integer_type_node))
7966 (lfn:long_integer_type_node @0)))
7967 /* Canonicalize llround (x) to lround (x) on LP64 targets where
7968 sizeof (long long) == sizeof (long). */
7969 (if (TYPE_PRECISION (long_long_integer_type_node)
7970 == TYPE_PRECISION (long_integer_type_node))
7973 (lfn:long_integer_type_node @0)))))
7975 /* cproj(x) -> x if we're ignoring infinities. */
7978 (if (!HONOR_INFINITIES (type))
7981 /* If the real part is inf and the imag part is known to be
7982 nonnegative, return (inf + 0i). */
7984 (CPROJ (complex REAL_CST@0 tree_expr_nonnegative_p@1))
7985 (if (real_isinf (TREE_REAL_CST_PTR (@0)))
7986 { build_complex_inf (type, false); }))
7988 /* If the imag part is inf, return (inf+I*copysign(0,imag)). */
7990 (CPROJ (complex @0 REAL_CST@1))
7991 (if (real_isinf (TREE_REAL_CST_PTR (@1)))
7992 { build_complex_inf (type, TREE_REAL_CST_PTR (@1)->sign); }))
7998 (pows @0 REAL_CST@1)
8000 const REAL_VALUE_TYPE *value = TREE_REAL_CST_PTR (@1);
8001 REAL_VALUE_TYPE tmp;
8004 /* pow(x,0) -> 1. */
8005 (if (real_equal (value, &dconst0))
8006 { build_real (type, dconst1); })
8007 /* pow(x,1) -> x. */
8008 (if (real_equal (value, &dconst1))
8010 /* pow(x,-1) -> 1/x. */
8011 (if (real_equal (value, &dconstm1))
8012 (rdiv { build_real (type, dconst1); } @0))
8013 /* pow(x,0.5) -> sqrt(x). */
8014 (if (flag_unsafe_math_optimizations
8015 && canonicalize_math_p ()
8016 && real_equal (value, &dconsthalf))
8018 /* pow(x,1/3) -> cbrt(x). */
8019 (if (flag_unsafe_math_optimizations
8020 && canonicalize_math_p ()
8021 && (tmp = real_value_truncate (TYPE_MODE (type), dconst_third ()),
8022 real_equal (value, &tmp)))
8025 /* powi(1,x) -> 1. */
8027 (POWI real_onep@0 @1)
8031 (POWI @0 INTEGER_CST@1)
8033 /* powi(x,0) -> 1. */
8034 (if (wi::to_wide (@1) == 0)
8035 { build_real (type, dconst1); })
8036 /* powi(x,1) -> x. */
8037 (if (wi::to_wide (@1) == 1)
8039 /* powi(x,-1) -> 1/x. */
8040 (if (wi::to_wide (@1) == -1)
8041 (rdiv { build_real (type, dconst1); } @0))))
8043 /* Narrowing of arithmetic and logical operations.
8045 These are conceptually similar to the transformations performed for
8046 the C/C++ front-ends by shorten_binary_op and shorten_compare. Long
8047 term we want to move all that code out of the front-ends into here. */
8049 /* Convert (outertype)((innertype0)a+(innertype1)b)
8050 into ((newtype)a+(newtype)b) where newtype
8051 is the widest mode from all of these. */
8052 (for op (plus minus mult rdiv)
8054 (convert (op:s@0 (convert1?@3 @1) (convert2?@4 @2)))
8055 /* If we have a narrowing conversion of an arithmetic operation where
8056 both operands are widening conversions from the same type as the outer
8057 narrowing conversion. Then convert the innermost operands to a
8058 suitable unsigned type (to avoid introducing undefined behavior),
8059 perform the operation and convert the result to the desired type. */
8060 (if (INTEGRAL_TYPE_P (type)
8063 /* We check for type compatibility between @0 and @1 below,
8064 so there's no need to check that @2/@4 are integral types. */
8065 && INTEGRAL_TYPE_P (TREE_TYPE (@1))
8066 && INTEGRAL_TYPE_P (TREE_TYPE (@3))
8067 /* The precision of the type of each operand must match the
8068 precision of the mode of each operand, similarly for the
8070 && type_has_mode_precision_p (TREE_TYPE (@1))
8071 && type_has_mode_precision_p (TREE_TYPE (@2))
8072 && type_has_mode_precision_p (type)
8073 /* The inner conversion must be a widening conversion. */
8074 && TYPE_PRECISION (TREE_TYPE (@3)) > TYPE_PRECISION (TREE_TYPE (@1))
8075 && types_match (@1, type)
8076 && (types_match (@1, @2)
8077 /* Or the second operand is const integer or converted const
8078 integer from valueize. */
8079 || poly_int_tree_p (@4)))
8080 (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
8081 (op @1 (convert @2))
8082 (with { tree utype = unsigned_type_for (TREE_TYPE (@1)); }
8083 (convert (op (convert:utype @1)
8084 (convert:utype @2)))))
8085 (if (FLOAT_TYPE_P (type)
8086 && DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0))
8087 == DECIMAL_FLOAT_TYPE_P (type))
8088 (with { tree arg0 = strip_float_extensions (@1);
8089 tree arg1 = strip_float_extensions (@2);
8090 tree itype = TREE_TYPE (@0);
8091 tree ty1 = TREE_TYPE (arg0);
8092 tree ty2 = TREE_TYPE (arg1);
8093 enum tree_code code = TREE_CODE (itype); }
8094 (if (FLOAT_TYPE_P (ty1)
8095 && FLOAT_TYPE_P (ty2))
8096 (with { tree newtype = type;
8097 if (TYPE_MODE (ty1) == SDmode
8098 || TYPE_MODE (ty2) == SDmode
8099 || TYPE_MODE (type) == SDmode)
8100 newtype = dfloat32_type_node;
8101 if (TYPE_MODE (ty1) == DDmode
8102 || TYPE_MODE (ty2) == DDmode
8103 || TYPE_MODE (type) == DDmode)
8104 newtype = dfloat64_type_node;
8105 if (TYPE_MODE (ty1) == TDmode
8106 || TYPE_MODE (ty2) == TDmode
8107 || TYPE_MODE (type) == TDmode)
8108 newtype = dfloat128_type_node; }
8109 (if ((newtype == dfloat32_type_node
8110 || newtype == dfloat64_type_node
8111 || newtype == dfloat128_type_node)
8113 && types_match (newtype, type))
8114 (op (convert:newtype @1) (convert:newtype @2))
8115 (with { if (element_precision (ty1) > element_precision (newtype))
8117 if (element_precision (ty2) > element_precision (newtype))
8119 /* Sometimes this transformation is safe (cannot
8120 change results through affecting double rounding
8121 cases) and sometimes it is not. If NEWTYPE is
8122 wider than TYPE, e.g. (float)((long double)double
8123 + (long double)double) converted to
8124 (float)(double + double), the transformation is
8125 unsafe regardless of the details of the types
8126 involved; double rounding can arise if the result
8127 of NEWTYPE arithmetic is a NEWTYPE value half way
8128 between two representable TYPE values but the
8129 exact value is sufficiently different (in the
8130 right direction) for this difference to be
8131 visible in ITYPE arithmetic. If NEWTYPE is the
8132 same as TYPE, however, the transformation may be
8133 safe depending on the types involved: it is safe
8134 if the ITYPE has strictly more than twice as many
8135 mantissa bits as TYPE, can represent infinities
8136 and NaNs if the TYPE can, and has sufficient
8137 exponent range for the product or ratio of two
8138 values representable in the TYPE to be within the
8139 range of normal values of ITYPE. */
8140 (if (element_precision (newtype) < element_precision (itype)
8141 && (!VECTOR_MODE_P (TYPE_MODE (newtype))
8142 || target_supports_op_p (newtype, op, optab_default))
8143 && (flag_unsafe_math_optimizations
8144 || (element_precision (newtype) == element_precision (type)
8145 && real_can_shorten_arithmetic (element_mode (itype),
8146 element_mode (type))
8147 && !excess_precision_type (newtype)))
8148 && !types_match (itype, newtype))
8149 (convert:type (op (convert:newtype @1)
8150 (convert:newtype @2)))
8155 /* This is another case of narrowing, specifically when there's an outer
8156 BIT_AND_EXPR which masks off bits outside the type of the innermost
8157 operands. Like the previous case we have to convert the operands
8158 to unsigned types to avoid introducing undefined behavior for the
8159 arithmetic operation. */
8160 (for op (minus plus)
8162 (bit_and (op:s (convert@2 @0) (convert@3 @1)) INTEGER_CST@4)
8163 (if (INTEGRAL_TYPE_P (type)
8164 /* We check for type compatibility between @0 and @1 below,
8165 so there's no need to check that @1/@3 are integral types. */
8166 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
8167 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
8168 /* The precision of the type of each operand must match the
8169 precision of the mode of each operand, similarly for the
8171 && type_has_mode_precision_p (TREE_TYPE (@0))
8172 && type_has_mode_precision_p (TREE_TYPE (@1))
8173 && type_has_mode_precision_p (type)
8174 /* The inner conversion must be a widening conversion. */
8175 && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
8176 && types_match (@0, @1)
8177 && (tree_int_cst_min_precision (@4, TYPE_SIGN (TREE_TYPE (@0)))
8178 <= TYPE_PRECISION (TREE_TYPE (@0)))
8179 && (wi::to_wide (@4)
8180 & wi::mask (TYPE_PRECISION (TREE_TYPE (@0)),
8181 true, TYPE_PRECISION (type))) == 0)
8182 (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
8183 (with { tree ntype = TREE_TYPE (@0); }
8184 (convert (bit_and (op @0 @1) (convert:ntype @4))))
8185 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
8186 (convert (bit_and (op (convert:utype @0) (convert:utype @1))
8187 (convert:utype @4))))))))
8189 /* Transform (@0 < @1 and @0 < @2) to use min,
8190 (@0 > @1 and @0 > @2) to use max */
8191 (for logic (bit_and bit_and bit_and bit_and bit_ior bit_ior bit_ior bit_ior)
8192 op (lt le gt ge lt le gt ge )
8193 ext (min min max max max max min min )
8195 (logic (op:cs @0 @1) (op:cs @0 @2))
8196 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
8197 && TREE_CODE (@0) != INTEGER_CST)
8198 (op @0 (ext @1 @2)))))
8200 /* Max<bool0, bool1> -> bool0 | bool1
8201 Min<bool0, bool1> -> bool0 & bool1 */
8203 logic (bit_ior bit_and)
8205 (op zero_one_valued_p@0 zero_one_valued_p@1)
8208 /* signbit(x) != 0 ? -x : x -> abs(x)
8209 signbit(x) == 0 ? -x : x -> -abs(x) */
8213 (cond (neeq (sign @0) integer_zerop) (negate @0) @0)
8214 (if (neeq == NE_EXPR)
8216 (negate (abs @0))))))
8219 /* signbit(x) -> 0 if x is nonnegative. */
8220 (SIGNBIT tree_expr_nonnegative_p@0)
8221 { integer_zero_node; })
8224 /* signbit(x) -> x<0 if x doesn't have signed zeros. */
8226 (if (!HONOR_SIGNED_ZEROS (@0))
8227 (convert (lt @0 { build_real (TREE_TYPE (@0), dconst0); }))))
8229 /* Transform comparisons of the form X +- C1 CMP C2 to X CMP C2 -+ C1. */
8231 (for op (plus minus)
8234 (cmp (op@3 @0 INTEGER_CST@1) INTEGER_CST@2)
8235 (if (!TREE_OVERFLOW (@1) && !TREE_OVERFLOW (@2)
8236 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0))
8237 && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@0))
8238 && !TYPE_SATURATING (TREE_TYPE (@0)))
8239 (with { tree res = int_const_binop (rop, @2, @1); }
8240 (if (TREE_OVERFLOW (res)
8241 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
8242 { constant_boolean_node (cmp == NE_EXPR, type); }
8243 (if (single_use (@3))
8244 (cmp @0 { TREE_OVERFLOW (res)
8245 ? drop_tree_overflow (res) : res; }))))))))
8246 (for cmp (lt le gt ge)
8247 (for op (plus minus)
8250 (cmp (op@3 @0 INTEGER_CST@1) INTEGER_CST@2)
8251 (if (!TREE_OVERFLOW (@1) && !TREE_OVERFLOW (@2)
8252 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
8253 (with { tree res = int_const_binop (rop, @2, @1); }
8254 (if (TREE_OVERFLOW (res))
8256 fold_overflow_warning (("assuming signed overflow does not occur "
8257 "when simplifying conditional to constant"),
8258 WARN_STRICT_OVERFLOW_CONDITIONAL);
8259 bool less = cmp == LE_EXPR || cmp == LT_EXPR;
8260 /* wi::ges_p (@2, 0) should be sufficient for a signed type. */
8261 bool ovf_high = wi::lt_p (wi::to_wide (@1), 0,
8262 TYPE_SIGN (TREE_TYPE (@1)))
8263 != (op == MINUS_EXPR);
8264 constant_boolean_node (less == ovf_high, type);
8266 (if (single_use (@3))
8269 fold_overflow_warning (("assuming signed overflow does not occur "
8270 "when changing X +- C1 cmp C2 to "
8272 WARN_STRICT_OVERFLOW_COMPARISON);
8274 (cmp @0 { res; })))))))))
8276 /* Canonicalizations of BIT_FIELD_REFs. */
8279 (BIT_FIELD_REF (BIT_FIELD_REF @0 @1 @2) @3 @4)
8280 (BIT_FIELD_REF @0 @3 { const_binop (PLUS_EXPR, bitsizetype, @2, @4); }))
8283 (BIT_FIELD_REF (view_convert @0) @1 @2)
8284 (BIT_FIELD_REF @0 @1 @2))
8287 (BIT_FIELD_REF @0 @1 integer_zerop)
8288 (if (tree_int_cst_equal (@1, TYPE_SIZE (TREE_TYPE (@0))))
8292 (BIT_FIELD_REF @0 @1 @2)
8294 (if (TREE_CODE (TREE_TYPE (@0)) == COMPLEX_TYPE
8295 && tree_int_cst_equal (@1, TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))))
8297 (if (integer_zerop (@2))
8298 (view_convert (realpart @0)))
8299 (if (tree_int_cst_equal (@2, TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))))
8300 (view_convert (imagpart @0)))))
8301 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
8302 && INTEGRAL_TYPE_P (type)
8303 /* On GIMPLE this should only apply to register arguments. */
8304 && (! GIMPLE || is_gimple_reg (@0))
8305 /* A bit-field-ref that referenced the full argument can be stripped. */
8306 && ((compare_tree_int (@1, TYPE_PRECISION (TREE_TYPE (@0))) == 0
8307 && integer_zerop (@2))
8308 /* Low-parts can be reduced to integral conversions.
8309 ??? The following doesn't work for PDP endian. */
8310 || (BYTES_BIG_ENDIAN == WORDS_BIG_ENDIAN
8311 /* But only do this after vectorization. */
8312 && canonicalize_math_after_vectorization_p ()
8313 /* Don't even think about BITS_BIG_ENDIAN. */
8314 && TYPE_PRECISION (TREE_TYPE (@0)) % BITS_PER_UNIT == 0
8315 && TYPE_PRECISION (type) % BITS_PER_UNIT == 0
8316 && compare_tree_int (@2, (BYTES_BIG_ENDIAN
8317 ? (TYPE_PRECISION (TREE_TYPE (@0))
8318 - TYPE_PRECISION (type))
8322 /* Simplify vector extracts. */
8325 (BIT_FIELD_REF CONSTRUCTOR@0 @1 @2)
8326 (if (VECTOR_TYPE_P (TREE_TYPE (@0))
8327 && tree_fits_uhwi_p (TYPE_SIZE (type))
8328 && ((tree_to_uhwi (TYPE_SIZE (type))
8329 == tree_to_uhwi (TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))))
8330 || (VECTOR_TYPE_P (type)
8331 && (tree_to_uhwi (TYPE_SIZE (TREE_TYPE (type)))
8332 == tree_to_uhwi (TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0))))))))
8335 tree ctor = (TREE_CODE (@0) == SSA_NAME
8336 ? gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0)) : @0);
8337 tree eltype = TREE_TYPE (TREE_TYPE (ctor));
8338 unsigned HOST_WIDE_INT width = tree_to_uhwi (TYPE_SIZE (eltype));
8339 unsigned HOST_WIDE_INT n = tree_to_uhwi (@1);
8340 unsigned HOST_WIDE_INT idx = tree_to_uhwi (@2);
8343 && (idx % width) == 0
8345 && known_le ((idx + n) / width,
8346 TYPE_VECTOR_SUBPARTS (TREE_TYPE (ctor))))
8351 /* Constructor elements can be subvectors. */
8353 if (CONSTRUCTOR_NELTS (ctor) != 0)
8355 tree cons_elem = TREE_TYPE (CONSTRUCTOR_ELT (ctor, 0)->value);
8356 if (TREE_CODE (cons_elem) == VECTOR_TYPE)
8357 k = TYPE_VECTOR_SUBPARTS (cons_elem);
8359 unsigned HOST_WIDE_INT elt, count, const_k;
8362 /* We keep an exact subset of the constructor elements. */
8363 (if (multiple_p (idx, k, &elt) && multiple_p (n, k, &count))
8364 (if (CONSTRUCTOR_NELTS (ctor) == 0)
8365 { build_zero_cst (type); }
8367 (if (elt < CONSTRUCTOR_NELTS (ctor))
8368 (view_convert { CONSTRUCTOR_ELT (ctor, elt)->value; })
8369 { build_zero_cst (type); })
8370 /* We don't want to emit new CTORs unless the old one goes away.
8371 ??? Eventually allow this if the CTOR ends up constant or
8373 (if (single_use (@0))
8376 vec<constructor_elt, va_gc> *vals;
8377 vec_alloc (vals, count);
8378 bool constant_p = true;
8380 for (unsigned i = 0;
8381 i < count && elt + i < CONSTRUCTOR_NELTS (ctor); ++i)
8383 tree e = CONSTRUCTOR_ELT (ctor, elt + i)->value;
8384 CONSTRUCTOR_APPEND_ELT (vals, NULL_TREE, e);
8385 if (!CONSTANT_CLASS_P (e))
8388 tree evtype = (types_match (TREE_TYPE (type),
8389 TREE_TYPE (TREE_TYPE (ctor)))
8391 : build_vector_type (TREE_TYPE (TREE_TYPE (ctor)),
8393 /* We used to build a CTOR in the non-constant case here
8394 but that's not a GIMPLE value. We'd have to expose this
8395 operation somehow so the code generation can properly
8396 split it out to a separate stmt. */
8397 res = (constant_p ? build_vector_from_ctor (evtype, vals)
8398 : (GIMPLE ? NULL_TREE : build_constructor (evtype, vals)));
8401 (view_convert { res; })))))))
8402 /* The bitfield references a single constructor element. */
8403 (if (k.is_constant (&const_k)
8404 && idx + n <= (idx / const_k + 1) * const_k)
8406 (if (CONSTRUCTOR_NELTS (ctor) <= idx / const_k)
8407 { build_zero_cst (type); })
8409 (view_convert { CONSTRUCTOR_ELT (ctor, idx / const_k)->value; }))
8410 (BIT_FIELD_REF { CONSTRUCTOR_ELT (ctor, idx / const_k)->value; }
8411 @1 { bitsize_int ((idx % const_k) * width); })))))))))
8413 /* Simplify a bit extraction from a bit insertion for the cases with
8414 the inserted element fully covering the extraction or the insertion
8415 not touching the extraction. */
8417 (BIT_FIELD_REF (bit_insert @0 @1 @ipos) @rsize @rpos)
8420 unsigned HOST_WIDE_INT isize;
8421 if (INTEGRAL_TYPE_P (TREE_TYPE (@1)))
8422 isize = TYPE_PRECISION (TREE_TYPE (@1));
8424 isize = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (@1)));
8427 (if ((!INTEGRAL_TYPE_P (TREE_TYPE (@1))
8428 || type_has_mode_precision_p (TREE_TYPE (@1)))
8429 && wi::leu_p (wi::to_wide (@ipos), wi::to_wide (@rpos))
8430 && wi::leu_p (wi::to_wide (@rpos) + wi::to_wide (@rsize),
8431 wi::to_wide (@ipos) + isize))
8432 (BIT_FIELD_REF @1 @rsize { wide_int_to_tree (bitsizetype,
8434 - wi::to_wide (@ipos)); }))
8435 (if (wi::eq_p (wi::to_wide (@ipos), wi::to_wide (@rpos))
8436 && compare_tree_int (@rsize, isize) == 0)
8438 (if (wi::geu_p (wi::to_wide (@ipos),
8439 wi::to_wide (@rpos) + wi::to_wide (@rsize))
8440 || wi::geu_p (wi::to_wide (@rpos),
8441 wi::to_wide (@ipos) + isize))
8442 (BIT_FIELD_REF @0 @rsize @rpos)))))
8444 /* Simplify vector inserts of other vector extracts to a permute. */
8446 (bit_insert @0 (BIT_FIELD_REF@2 @1 @rsize @rpos) @ipos)
8447 (if (VECTOR_TYPE_P (type)
8448 && types_match (@0, @1)
8449 && types_match (TREE_TYPE (TREE_TYPE (@0)), TREE_TYPE (@2))
8450 && TYPE_VECTOR_SUBPARTS (type).is_constant ())
8453 unsigned HOST_WIDE_INT elsz
8454 = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (TREE_TYPE (@1))));
8455 poly_uint64 relt = exact_div (tree_to_poly_uint64 (@rpos), elsz);
8456 poly_uint64 ielt = exact_div (tree_to_poly_uint64 (@ipos), elsz);
8457 unsigned nunits = TYPE_VECTOR_SUBPARTS (type).to_constant ();
8458 vec_perm_builder builder;
8459 builder.new_vector (nunits, nunits, 1);
8460 for (unsigned i = 0; i < nunits; ++i)
8461 builder.quick_push (known_eq (ielt, i) ? nunits + relt : i);
8462 vec_perm_indices sel (builder, 2, nunits);
8464 (if (!VECTOR_MODE_P (TYPE_MODE (type))
8465 || can_vec_perm_const_p (TYPE_MODE (type), TYPE_MODE (type), sel, false))
8466 (vec_perm @0 @1 { vec_perm_indices_to_tree
8467 (build_vector_type (ssizetype, nunits), sel); })))))
8469 (if (canonicalize_math_after_vectorization_p ())
8472 (fmas:c (negate @0) @1 @2)
8473 (IFN_FNMA @0 @1 @2))
8475 (fmas @0 @1 (negate @2))
8478 (fmas:c (negate @0) @1 (negate @2))
8479 (IFN_FNMS @0 @1 @2))
8481 (negate (fmas@3 @0 @1 @2))
8482 (if (single_use (@3))
8483 (IFN_FNMS @0 @1 @2))))
8486 (IFN_FMS:c (negate @0) @1 @2)
8487 (IFN_FNMS @0 @1 @2))
8489 (IFN_FMS @0 @1 (negate @2))
8492 (IFN_FMS:c (negate @0) @1 (negate @2))
8493 (IFN_FNMA @0 @1 @2))
8495 (negate (IFN_FMS@3 @0 @1 @2))
8496 (if (single_use (@3))
8497 (IFN_FNMA @0 @1 @2)))
8500 (IFN_FNMA:c (negate @0) @1 @2)
8503 (IFN_FNMA @0 @1 (negate @2))
8504 (IFN_FNMS @0 @1 @2))
8506 (IFN_FNMA:c (negate @0) @1 (negate @2))
8509 (negate (IFN_FNMA@3 @0 @1 @2))
8510 (if (single_use (@3))
8511 (IFN_FMS @0 @1 @2)))
8514 (IFN_FNMS:c (negate @0) @1 @2)
8517 (IFN_FNMS @0 @1 (negate @2))
8518 (IFN_FNMA @0 @1 @2))
8520 (IFN_FNMS:c (negate @0) @1 (negate @2))
8523 (negate (IFN_FNMS@3 @0 @1 @2))
8524 (if (single_use (@3))
8525 (IFN_FMA @0 @1 @2))))
8527 /* CLZ simplifications. */
8532 (op (clz:s@2 @0) INTEGER_CST@1)
8533 (if (integer_zerop (@1) && single_use (@2))
8534 /* clz(X) == 0 is (int)X < 0 and clz(X) != 0 is (int)X >= 0. */
8535 (with { tree type0 = TREE_TYPE (@0);
8536 tree stype = signed_type_for (type0);
8537 HOST_WIDE_INT val = 0;
8538 /* Punt on hypothetical weird targets. */
8540 && CLZ_DEFINED_VALUE_AT_ZERO (SCALAR_TYPE_MODE (type0),
8546 (cmp (convert:stype @0) { build_zero_cst (stype); })))
8547 /* clz(X) == (prec-1) is X == 1 and clz(X) != (prec-1) is X != 1. */
8548 (with { bool ok = true;
8549 HOST_WIDE_INT val = 0;
8550 tree type0 = TREE_TYPE (@0);
8551 /* Punt on hypothetical weird targets. */
8553 && CLZ_DEFINED_VALUE_AT_ZERO (SCALAR_TYPE_MODE (type0),
8555 && val == TYPE_PRECISION (type0) - 1)
8558 (if (ok && wi::to_wide (@1) == (TYPE_PRECISION (type0) - 1))
8559 (op @0 { build_one_cst (type0); })))))))
8561 /* CTZ simplifications. */
8563 (for op (ge gt le lt)
8566 /* __builtin_ctz (x) >= C -> (x & ((1 << C) - 1)) == 0. */
8567 (op (ctz:s @0) INTEGER_CST@1)
8568 (with { bool ok = true;
8569 HOST_WIDE_INT val = 0;
8570 if (!tree_fits_shwi_p (@1))
8574 val = tree_to_shwi (@1);
8575 /* Canonicalize to >= or <. */
8576 if (op == GT_EXPR || op == LE_EXPR)
8578 if (val == HOST_WIDE_INT_MAX)
8584 bool zero_res = false;
8585 HOST_WIDE_INT zero_val = 0;
8586 tree type0 = TREE_TYPE (@0);
8587 int prec = TYPE_PRECISION (type0);
8589 && CTZ_DEFINED_VALUE_AT_ZERO (SCALAR_TYPE_MODE (type0),
8594 (if (ok && (!zero_res || zero_val >= val))
8595 { constant_boolean_node (cmp == EQ_EXPR ? true : false, type); })
8597 (if (ok && (!zero_res || zero_val < val))
8598 { constant_boolean_node (cmp == EQ_EXPR ? false : true, type); })
8599 (if (ok && (!zero_res || zero_val < 0 || zero_val >= prec))
8600 (cmp (bit_and @0 { wide_int_to_tree (type0,
8601 wi::mask (val, false, prec)); })
8602 { build_zero_cst (type0); })))))))
8605 /* __builtin_ctz (x) == C -> (x & ((1 << (C + 1)) - 1)) == (1 << C). */
8606 (op (ctz:s @0) INTEGER_CST@1)
8607 (with { bool zero_res = false;
8608 HOST_WIDE_INT zero_val = 0;
8609 tree type0 = TREE_TYPE (@0);
8610 int prec = TYPE_PRECISION (type0);
8612 && CTZ_DEFINED_VALUE_AT_ZERO (SCALAR_TYPE_MODE (type0),
8616 (if (tree_int_cst_sgn (@1) < 0 || wi::to_widest (@1) >= prec)
8617 (if (!zero_res || zero_val != wi::to_widest (@1))
8618 { constant_boolean_node (op == EQ_EXPR ? false : true, type); })
8619 (if (!zero_res || zero_val < 0 || zero_val >= prec)
8620 (op (bit_and @0 { wide_int_to_tree (type0,
8621 wi::mask (tree_to_uhwi (@1) + 1,
8623 { wide_int_to_tree (type0,
8624 wi::shifted_mask (tree_to_uhwi (@1), 1,
8625 false, prec)); })))))))
8627 /* POPCOUNT simplifications. */
8628 /* popcount(X) + popcount(Y) is popcount(X|Y) when X&Y must be zero. */
8630 (plus (POPCOUNT:s @0) (POPCOUNT:s @1))
8631 (if (INTEGRAL_TYPE_P (type)
8632 && wi::bit_and (tree_nonzero_bits (@0), tree_nonzero_bits (@1)) == 0)
8633 (POPCOUNT (bit_ior @0 @1))))
8635 /* popcount(X) == 0 is X == 0, and related (in)equalities. */
8636 (for popcount (POPCOUNT)
8637 (for cmp (le eq ne gt)
8640 (cmp (popcount @0) integer_zerop)
8641 (rep @0 { build_zero_cst (TREE_TYPE (@0)); }))))
8643 /* popcount(bswap(x)) is popcount(x). */
8644 (for popcount (POPCOUNT)
8645 (for bswap (BUILT_IN_BSWAP16 BUILT_IN_BSWAP32
8646 BUILT_IN_BSWAP64 BUILT_IN_BSWAP128)
8648 (popcount (convert?@0 (bswap:s@1 @2)))
8649 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
8650 && INTEGRAL_TYPE_P (TREE_TYPE (@1)))
8651 (with { tree type0 = TREE_TYPE (@0);
8652 tree type1 = TREE_TYPE (@1);
8653 unsigned int prec0 = TYPE_PRECISION (type0);
8654 unsigned int prec1 = TYPE_PRECISION (type1); }
8655 (if (prec0 == prec1 || (prec0 > prec1 && TYPE_UNSIGNED (type1)))
8656 (popcount (convert:type0 (convert:type1 @2)))))))))
8658 /* popcount(rotate(X Y)) is popcount(X). */
8659 (for popcount (POPCOUNT)
8660 (for rot (lrotate rrotate)
8662 (popcount (convert?@0 (rot:s@1 @2 @3)))
8663 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
8664 && INTEGRAL_TYPE_P (TREE_TYPE (@1))
8665 && (GIMPLE || !TREE_SIDE_EFFECTS (@3)))
8666 (with { tree type0 = TREE_TYPE (@0);
8667 tree type1 = TREE_TYPE (@1);
8668 unsigned int prec0 = TYPE_PRECISION (type0);
8669 unsigned int prec1 = TYPE_PRECISION (type1); }
8670 (if (prec0 == prec1 || (prec0 > prec1 && TYPE_UNSIGNED (type1)))
8671 (popcount (convert:type0 @2))))))))
8673 /* Canonicalize POPCOUNT(x)&1 as PARITY(X). */
8675 (bit_and (POPCOUNT @0) integer_onep)
8678 /* popcount(X&Y) + popcount(X|Y) is popcount(x) + popcount(Y). */
8680 (plus:c (POPCOUNT:s (bit_and:s @0 @1)) (POPCOUNT:s (bit_ior:cs @0 @1)))
8681 (plus (POPCOUNT:type @0) (POPCOUNT:type @1)))
8683 /* popcount(X) + popcount(Y) - popcount(X&Y) is popcount(X|Y). */
8684 /* popcount(X) + popcount(Y) - popcount(X|Y) is popcount(X&Y). */
8685 (for popcount (POPCOUNT)
8686 (for log1 (bit_and bit_ior)
8687 log2 (bit_ior bit_and)
8689 (minus (plus:s (popcount:s @0) (popcount:s @1))
8690 (popcount:s (log1:cs @0 @1)))
8691 (popcount (log2 @0 @1)))
8693 (plus:c (minus:s (popcount:s @0) (popcount:s (log1:cs @0 @1)))
8695 (popcount (log2 @0 @1)))))
8697 /* PARITY simplifications. */
8698 /* parity(~X) is parity(X). */
8700 (PARITY (bit_not @0))
8703 /* parity(bswap(x)) is parity(x). */
8704 (for parity (PARITY)
8705 (for bswap (BUILT_IN_BSWAP16 BUILT_IN_BSWAP32
8706 BUILT_IN_BSWAP64 BUILT_IN_BSWAP128)
8708 (parity (convert?@0 (bswap:s@1 @2)))
8709 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
8710 && INTEGRAL_TYPE_P (TREE_TYPE (@1))
8711 && TYPE_PRECISION (TREE_TYPE (@0))
8712 >= TYPE_PRECISION (TREE_TYPE (@1)))
8713 (with { tree type0 = TREE_TYPE (@0);
8714 tree type1 = TREE_TYPE (@1); }
8715 (parity (convert:type0 (convert:type1 @2))))))))
8717 /* parity(rotate(X Y)) is parity(X). */
8718 (for parity (PARITY)
8719 (for rot (lrotate rrotate)
8721 (parity (convert?@0 (rot:s@1 @2 @3)))
8722 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
8723 && INTEGRAL_TYPE_P (TREE_TYPE (@1))
8724 && (GIMPLE || !TREE_SIDE_EFFECTS (@3))
8725 && TYPE_PRECISION (TREE_TYPE (@0))
8726 >= TYPE_PRECISION (TREE_TYPE (@1)))
8727 (with { tree type0 = TREE_TYPE (@0); }
8728 (parity (convert:type0 @2)))))))
8730 /* parity(X)^parity(Y) is parity(X^Y). */
8732 (bit_xor (PARITY:s @0) (PARITY:s @1))
8733 (PARITY (bit_xor @0 @1)))
8735 /* a != 0 ? FUN(a) : 0 -> Fun(a) for some builtin functions. */
8736 (for func (POPCOUNT BSWAP FFS PARITY)
8738 (cond (ne @0 integer_zerop@1) (func@3 (convert? @0)) integer_zerop@2)
8741 /* a != 0 ? FUN(a) : CST -> Fun(a) for some CLRSB builtins
8742 where CST is precision-1. */
8745 (cond (ne @0 integer_zerop@1) (func@4 (convert?@3 @0)) INTEGER_CST@2)
8746 (if (wi::to_widest (@2) == TYPE_PRECISION (TREE_TYPE (@3)) - 1)
8750 /* a != 0 ? CLZ(a) : CST -> .CLZ(a) where CST is the result of the internal function for 0. */
8753 (cond (ne @0 integer_zerop@1) (func (convert?@3 @0)) INTEGER_CST@2)
8755 internal_fn ifn = IFN_LAST;
8756 if (direct_internal_fn_supported_p (IFN_CLZ, type, OPTIMIZE_FOR_BOTH)
8757 && CLZ_DEFINED_VALUE_AT_ZERO (SCALAR_INT_TYPE_MODE (type),
8761 (if (ifn == IFN_CLZ && wi::to_widest (@2) == val)
8764 /* a != 0 ? CTZ(a) : CST -> .CTZ(a) where CST is the result of the internal function for 0. */
8767 (cond (ne @0 integer_zerop@1) (func (convert?@3 @0)) INTEGER_CST@2)
8769 internal_fn ifn = IFN_LAST;
8770 if (direct_internal_fn_supported_p (IFN_CTZ, type, OPTIMIZE_FOR_BOTH)
8771 && CTZ_DEFINED_VALUE_AT_ZERO (SCALAR_INT_TYPE_MODE (type),
8775 (if (ifn == IFN_CTZ && wi::to_widest (@2) == val)
8779 /* Common POPCOUNT/PARITY simplifications. */
8780 /* popcount(X&C1) is (X>>C2)&1 when C1 == 1<<C2. Same for parity(X&C1). */
8781 (for pfun (POPCOUNT PARITY)
8784 (if (INTEGRAL_TYPE_P (type))
8785 (with { wide_int nz = tree_nonzero_bits (@0); }
8789 (if (wi::popcount (nz) == 1)
8790 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
8791 (convert (rshift:utype (convert:utype @0)
8792 { build_int_cst (integer_type_node,
8793 wi::ctz (nz)); })))))))))
8796 /* 64- and 32-bits branchless implementations of popcount are detected:
8798 int popcount64c (uint64_t x)
8800 x -= (x >> 1) & 0x5555555555555555ULL;
8801 x = (x & 0x3333333333333333ULL) + ((x >> 2) & 0x3333333333333333ULL);
8802 x = (x + (x >> 4)) & 0x0f0f0f0f0f0f0f0fULL;
8803 return (x * 0x0101010101010101ULL) >> 56;
8806 int popcount32c (uint32_t x)
8808 x -= (x >> 1) & 0x55555555;
8809 x = (x & 0x33333333) + ((x >> 2) & 0x33333333);
8810 x = (x + (x >> 4)) & 0x0f0f0f0f;
8811 return (x * 0x01010101) >> 24;
8818 (rshift @8 INTEGER_CST@5)
8820 (bit_and @6 INTEGER_CST@7)
8824 (bit_and (rshift @0 INTEGER_CST@4) INTEGER_CST@11))
8830 /* Check constants and optab. */
8831 (with { unsigned prec = TYPE_PRECISION (type);
8832 int shift = (64 - prec) & 63;
8833 unsigned HOST_WIDE_INT c1
8834 = HOST_WIDE_INT_UC (0x0101010101010101) >> shift;
8835 unsigned HOST_WIDE_INT c2
8836 = HOST_WIDE_INT_UC (0x0F0F0F0F0F0F0F0F) >> shift;
8837 unsigned HOST_WIDE_INT c3
8838 = HOST_WIDE_INT_UC (0x3333333333333333) >> shift;
8839 unsigned HOST_WIDE_INT c4
8840 = HOST_WIDE_INT_UC (0x5555555555555555) >> shift;
8845 && TYPE_UNSIGNED (type)
8846 && integer_onep (@4)
8847 && wi::to_widest (@10) == 2
8848 && wi::to_widest (@5) == 4
8849 && wi::to_widest (@1) == prec - 8
8850 && tree_to_uhwi (@2) == c1
8851 && tree_to_uhwi (@3) == c2
8852 && tree_to_uhwi (@9) == c3
8853 && tree_to_uhwi (@7) == c3
8854 && tree_to_uhwi (@11) == c4)
8855 (if (direct_internal_fn_supported_p (IFN_POPCOUNT, type,
8857 (convert (IFN_POPCOUNT:type @0))
8858 /* Try to do popcount in two halves. PREC must be at least
8859 five bits for this to work without extension before adding. */
8861 tree half_type = NULL_TREE;
8862 opt_machine_mode m = mode_for_size ((prec + 1) / 2, MODE_INT, 1);
8865 && m.require () != TYPE_MODE (type))
8867 half_prec = GET_MODE_PRECISION (as_a <scalar_int_mode> (m));
8868 half_type = build_nonstandard_integer_type (half_prec, 1);
8870 gcc_assert (half_prec > 2);
8872 (if (half_type != NULL_TREE
8873 && direct_internal_fn_supported_p (IFN_POPCOUNT, half_type,
8876 (IFN_POPCOUNT:half_type (convert @0))
8877 (IFN_POPCOUNT:half_type (convert (rshift @0
8878 { build_int_cst (integer_type_node, half_prec); } )))))))))))
8880 /* __builtin_ffs needs to deal on many targets with the possible zero
8881 argument. If we know the argument is always non-zero, __builtin_ctz + 1
8882 should lead to better code. */
8884 (FFS tree_expr_nonzero_p@0)
8885 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
8886 && direct_internal_fn_supported_p (IFN_CTZ, TREE_TYPE (@0),
8887 OPTIMIZE_FOR_SPEED))
8888 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
8889 (plus (CTZ:type (convert:utype @0)) { build_one_cst (type); }))))
8892 (for ffs (BUILT_IN_FFS BUILT_IN_FFSL BUILT_IN_FFSLL
8894 /* __builtin_ffs (X) == 0 -> X == 0.
8895 __builtin_ffs (X) == 6 -> (X & 63) == 32. */
8898 (cmp (ffs@2 @0) INTEGER_CST@1)
8899 (with { int prec = TYPE_PRECISION (TREE_TYPE (@0)); }
8901 (if (integer_zerop (@1))
8902 (cmp @0 { build_zero_cst (TREE_TYPE (@0)); }))
8903 (if (tree_int_cst_sgn (@1) < 0 || wi::to_widest (@1) > prec)
8904 { constant_boolean_node (cmp == NE_EXPR ? true : false, type); })
8905 (if (single_use (@2))
8906 (cmp (bit_and @0 { wide_int_to_tree (TREE_TYPE (@0),
8907 wi::mask (tree_to_uhwi (@1),
8909 { wide_int_to_tree (TREE_TYPE (@0),
8910 wi::shifted_mask (tree_to_uhwi (@1) - 1, 1,
8911 false, prec)); }))))))
8913 /* __builtin_ffs (X) > 6 -> X != 0 && (X & 63) == 0. */
8917 bit_op (bit_and bit_ior)
8919 (cmp (ffs@2 @0) INTEGER_CST@1)
8920 (with { int prec = TYPE_PRECISION (TREE_TYPE (@0)); }
8922 (if (integer_zerop (@1))
8923 (cmp2 @0 { build_zero_cst (TREE_TYPE (@0)); }))
8924 (if (tree_int_cst_sgn (@1) < 0)
8925 { constant_boolean_node (cmp == GT_EXPR ? true : false, type); })
8926 (if (wi::to_widest (@1) >= prec)
8927 { constant_boolean_node (cmp == GT_EXPR ? false : true, type); })
8928 (if (wi::to_widest (@1) == prec - 1)
8929 (cmp3 @0 { wide_int_to_tree (TREE_TYPE (@0),
8930 wi::shifted_mask (prec - 1, 1,
8932 (if (single_use (@2))
8933 (bit_op (cmp2 @0 { build_zero_cst (TREE_TYPE (@0)); })
8935 { wide_int_to_tree (TREE_TYPE (@0),
8936 wi::mask (tree_to_uhwi (@1),
8938 { build_zero_cst (TREE_TYPE (@0)); }))))))))
8945 --> r = .COND_FN (cond, a, b)
8949 --> r = .COND_FN (~cond, b, a). */
8951 (for uncond_op (UNCOND_UNARY)
8952 cond_op (COND_UNARY)
8954 (vec_cond @0 (view_convert? (uncond_op@3 @1)) @2)
8955 (with { tree op_type = TREE_TYPE (@3); }
8956 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
8957 && is_truth_type_for (op_type, TREE_TYPE (@0)))
8958 (cond_op @0 @1 @2))))
8960 (vec_cond @0 @1 (view_convert? (uncond_op@3 @2)))
8961 (with { tree op_type = TREE_TYPE (@3); }
8962 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
8963 && is_truth_type_for (op_type, TREE_TYPE (@0)))
8964 (cond_op (bit_not @0) @2 @1)))))
8966 (for uncond_op (UNCOND_UNARY)
8967 cond_op (COND_LEN_UNARY)
8969 (IFN_VCOND_MASK_LEN @0 (view_convert? (uncond_op@3 @1)) @2 @4 @5)
8970 (with { tree op_type = TREE_TYPE (@3); }
8971 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
8972 && is_truth_type_for (op_type, TREE_TYPE (@0)))
8973 (cond_op @0 @1 @2 @4 @5))))
8975 (IFN_VCOND_MASK_LEN @0 @1 (view_convert? (uncond_op@3 @2)) @4 @5)
8976 (with { tree op_type = TREE_TYPE (@3); }
8977 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
8978 && is_truth_type_for (op_type, TREE_TYPE (@0)))
8979 (cond_op (bit_not @0) @2 @1 @4 @5)))))
8981 /* `(a ? -1 : 0) ^ b` can be converted into a conditional not. */
8983 (bit_xor:c (vec_cond @0 uniform_integer_cst_p@1 uniform_integer_cst_p@2) @3)
8984 (if (canonicalize_math_after_vectorization_p ()
8985 && vectorized_internal_fn_supported_p (IFN_COND_NOT, type)
8986 && is_truth_type_for (type, TREE_TYPE (@0)))
8987 (if (integer_all_onesp (@1) && integer_zerop (@2))
8988 (IFN_COND_NOT @0 @3 @3))
8989 (if (integer_all_onesp (@2) && integer_zerop (@1))
8990 (IFN_COND_NOT (bit_not @0) @3 @3))))
8999 r = c ? a1 op a2 : b;
9001 if the target can do it in one go. This makes the operation conditional
9002 on c, so could drop potentially-trapping arithmetic, but that's a valid
9003 simplification if the result of the operation isn't needed.
9005 Avoid speculatively generating a stand-alone vector comparison
9006 on targets that might not support them. Any target implementing
9007 conditional internal functions must support the same comparisons
9008 inside and outside a VEC_COND_EXPR. */
9010 (for uncond_op (UNCOND_BINARY)
9011 cond_op (COND_BINARY)
9013 (vec_cond @0 (view_convert? (uncond_op@4 @1 @2)) @3)
9014 (with { tree op_type = TREE_TYPE (@4); }
9015 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
9016 && is_truth_type_for (op_type, TREE_TYPE (@0))
9018 (view_convert (cond_op @0 @1 @2 (view_convert:op_type @3))))))
9020 (vec_cond @0 @1 (view_convert? (uncond_op@4 @2 @3)))
9021 (with { tree op_type = TREE_TYPE (@4); }
9022 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
9023 && is_truth_type_for (op_type, TREE_TYPE (@0))
9025 (view_convert (cond_op (bit_not @0) @2 @3 (view_convert:op_type @1)))))))
9027 (for uncond_op (UNCOND_BINARY)
9028 cond_op (COND_LEN_BINARY)
9030 (IFN_VCOND_MASK_LEN @0 (view_convert? (uncond_op@4 @1 @2)) @3 @5 @6)
9031 (with { tree op_type = TREE_TYPE (@4); }
9032 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
9033 && is_truth_type_for (op_type, TREE_TYPE (@0))
9035 (view_convert (cond_op @0 @1 @2 (view_convert:op_type @3) @5 @6)))))
9037 (IFN_VCOND_MASK_LEN @0 @1 (view_convert? (uncond_op@4 @2 @3)) @5 @6)
9038 (with { tree op_type = TREE_TYPE (@4); }
9039 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
9040 && is_truth_type_for (op_type, TREE_TYPE (@0))
9042 (view_convert (cond_op (bit_not @0) @2 @3 (view_convert:op_type @1) @5 @6))))))
9044 /* Same for ternary operations. */
9045 (for uncond_op (UNCOND_TERNARY)
9046 cond_op (COND_TERNARY)
9048 (vec_cond @0 (view_convert? (uncond_op@5 @1 @2 @3)) @4)
9049 (with { tree op_type = TREE_TYPE (@5); }
9050 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
9051 && is_truth_type_for (op_type, TREE_TYPE (@0))
9053 (view_convert (cond_op @0 @1 @2 @3 (view_convert:op_type @4))))))
9055 (vec_cond @0 @1 (view_convert? (uncond_op@5 @2 @3 @4)))
9056 (with { tree op_type = TREE_TYPE (@5); }
9057 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
9058 && is_truth_type_for (op_type, TREE_TYPE (@0))
9060 (view_convert (cond_op (bit_not @0) @2 @3 @4
9061 (view_convert:op_type @1)))))))
9063 (for uncond_op (UNCOND_TERNARY)
9064 cond_op (COND_LEN_TERNARY)
9066 (IFN_VCOND_MASK_LEN @0 (view_convert? (uncond_op@5 @1 @2 @3)) @4 @6 @7)
9067 (with { tree op_type = TREE_TYPE (@5); }
9068 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
9069 && is_truth_type_for (op_type, TREE_TYPE (@0))
9071 (view_convert (cond_op @0 @1 @2 @3 (view_convert:op_type @4) @6 @7)))))
9073 (IFN_VCOND_MASK_LEN @0 @1 (view_convert? (uncond_op@5 @2 @3 @4 @6 @7)))
9074 (with { tree op_type = TREE_TYPE (@5); }
9075 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
9076 && is_truth_type_for (op_type, TREE_TYPE (@0))
9078 (view_convert (cond_op (bit_not @0) @2 @3 @4 (view_convert:op_type @1) @6 @7))))))
9081 /* Detect cases in which a VEC_COND_EXPR effectively replaces the
9082 "else" value of an IFN_COND_*. */
9083 (for cond_op (COND_BINARY)
9085 (vec_cond @0 (view_convert? (cond_op @0 @1 @2 @3)) @4)
9086 (with { tree op_type = TREE_TYPE (@3); }
9087 (if (element_precision (type) == element_precision (op_type))
9088 (view_convert (cond_op @0 @1 @2 (view_convert:op_type @4))))))
9090 (vec_cond @0 @1 (view_convert? (cond_op @2 @3 @4 @5)))
9091 (with { tree op_type = TREE_TYPE (@5); }
9092 (if (inverse_conditions_p (@0, @2)
9093 && element_precision (type) == element_precision (op_type))
9094 (view_convert (cond_op @2 @3 @4 (view_convert:op_type @1)))))))
9096 /* Same for ternary operations. */
9097 (for cond_op (COND_TERNARY)
9099 (vec_cond @0 (view_convert? (cond_op @0 @1 @2 @3 @4)) @5)
9100 (with { tree op_type = TREE_TYPE (@4); }
9101 (if (element_precision (type) == element_precision (op_type))
9102 (view_convert (cond_op @0 @1 @2 @3 (view_convert:op_type @5))))))
9104 (vec_cond @0 @1 (view_convert? (cond_op @2 @3 @4 @5 @6)))
9105 (with { tree op_type = TREE_TYPE (@6); }
9106 (if (inverse_conditions_p (@0, @2)
9107 && element_precision (type) == element_precision (op_type))
9108 (view_convert (cond_op @2 @3 @4 @5 (view_convert:op_type @1)))))))
9110 /* Detect cases in which a VEC_COND_EXPR effectively replaces the
9111 "else" value of an IFN_COND_LEN_*. */
9112 (for cond_len_op (COND_LEN_BINARY)
9114 (vec_cond @0 (view_convert? (cond_len_op @0 @1 @2 @3 @4 @5)) @6)
9115 (with { tree op_type = TREE_TYPE (@3); }
9116 (if (element_precision (type) == element_precision (op_type))
9117 (view_convert (cond_len_op @0 @1 @2 (view_convert:op_type @6) @4 @5)))))
9119 (vec_cond @0 @1 (view_convert? (cond_len_op @2 @3 @4 @5 @6 @7)))
9120 (with { tree op_type = TREE_TYPE (@5); }
9121 (if (inverse_conditions_p (@0, @2)
9122 && element_precision (type) == element_precision (op_type))
9123 (view_convert (cond_len_op @2 @3 @4 (view_convert:op_type @1) @6 @7))))))
9125 /* Same for ternary operations. */
9126 (for cond_len_op (COND_LEN_TERNARY)
9128 (vec_cond @0 (view_convert? (cond_len_op @0 @1 @2 @3 @4 @5 @6)) @7)
9129 (with { tree op_type = TREE_TYPE (@4); }
9130 (if (element_precision (type) == element_precision (op_type))
9131 (view_convert (cond_len_op @0 @1 @2 @3 (view_convert:op_type @7) @5 @6)))))
9133 (vec_cond @0 @1 (view_convert? (cond_len_op @2 @3 @4 @5 @6 @7 @8)))
9134 (with { tree op_type = TREE_TYPE (@6); }
9135 (if (inverse_conditions_p (@0, @2)
9136 && element_precision (type) == element_precision (op_type))
9137 (view_convert (cond_len_op @2 @3 @4 @5 (view_convert:op_type @1) @7 @8))))))
9139 /* Detect simplication for a conditional reduction where
9142 c = mask2 ? d + a : d
9146 c = mask1 && mask2 ? d + b : d. */
9148 (IFN_COND_ADD @0 @1 (vec_cond @2 @3 zerop@4) @1)
9149 (if (ANY_INTEGRAL_TYPE_P (type)
9150 || (FLOAT_TYPE_P (type)
9151 && fold_real_zero_addition_p (type, NULL_TREE, @4, 0)))
9152 (IFN_COND_ADD (bit_and @0 @2) @1 @3 @1)))
9154 /* Detect simplication for a conditional length reduction where
9157 c = i < len + bias ? d + a : d
9161 c = mask && i < len + bias ? d + b : d. */
9163 (IFN_COND_LEN_ADD integer_truep @0 (vec_cond @1 @2 zerop@5) @0 @3 @4)
9164 (if (ANY_INTEGRAL_TYPE_P (type)
9165 || (FLOAT_TYPE_P (type)
9166 && fold_real_zero_addition_p (type, NULL_TREE, @5, 0)))
9167 (IFN_COND_LEN_ADD @1 @0 @2 @0 @3 @4)))
9169 /* Detect simplification for vector condition folding where
9171 c = mask1 ? (masked_op mask2 a b) : b
9175 c = masked_op (mask1 & mask2) a b
9177 where the operation can be partially applied to one operand. */
9179 (for cond_op (COND_BINARY)
9182 (cond_op:s @1 @2 @3 @4) @3)
9183 (cond_op (bit_and @1 @0) @2 @3 @4)))
9185 /* And same for ternary expressions. */
9187 (for cond_op (COND_TERNARY)
9190 (cond_op:s @1 @2 @3 @4 @5) @4)
9191 (cond_op (bit_and @1 @0) @2 @3 @4 @5)))
9193 /* For pointers @0 and @2 and nonnegative constant offset @1, look for
9196 A: (@0 + @1 < @2) | (@2 + @1 < @0)
9197 B: (@0 + @1 <= @2) | (@2 + @1 <= @0)
9199 If pointers are known not to wrap, B checks whether @1 bytes starting
9200 at @0 and @2 do not overlap, while A tests the same thing for @1 + 1
9201 bytes. A is more efficiently tested as:
9203 A: (sizetype) (@0 + @1 - @2) > @1 * 2
9205 The equivalent expression for B is given by replacing @1 with @1 - 1:
9207 B: (sizetype) (@0 + (@1 - 1) - @2) > (@1 - 1) * 2
9209 @0 and @2 can be swapped in both expressions without changing the result.
9211 The folds rely on sizetype's being unsigned (which is always true)
9212 and on its being the same width as the pointer (which we have to check).
9214 The fold replaces two pointer_plus expressions, two comparisons and
9215 an IOR with a pointer_plus, a pointer_diff, and a comparison, so in
9216 the best case it's a saving of two operations. The A fold retains one
9217 of the original pointer_pluses, so is a win even if both pointer_pluses
9218 are used elsewhere. The B fold is a wash if both pointer_pluses are
9219 used elsewhere, since all we end up doing is replacing a comparison with
9220 a pointer_plus. We do still apply the fold under those circumstances
9221 though, in case applying it to other conditions eventually makes one of the
9222 pointer_pluses dead. */
9223 (for ior (truth_orif truth_or bit_ior)
9226 (ior (cmp:cs (pointer_plus@3 @0 INTEGER_CST@1) @2)
9227 (cmp:cs (pointer_plus@4 @2 @1) @0))
9228 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
9229 && TYPE_OVERFLOW_WRAPS (sizetype)
9230 && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (sizetype))
9231 /* Calculate the rhs constant. */
9232 (with { offset_int off = wi::to_offset (@1) - (cmp == LE_EXPR ? 1 : 0);
9233 offset_int rhs = off * 2; }
9234 /* Always fails for negative values. */
9235 (if (wi::min_precision (rhs, UNSIGNED) <= TYPE_PRECISION (sizetype))
9236 /* Since the order of @0 and @2 doesn't matter, let tree_swap_operands_p
9237 pick a canonical order. This increases the chances of using the
9238 same pointer_plus in multiple checks. */
9239 (with { bool swap_p = tree_swap_operands_p (@0, @2);
9240 tree rhs_tree = wide_int_to_tree (sizetype, rhs); }
9241 (if (cmp == LT_EXPR)
9242 (gt (convert:sizetype
9243 (pointer_diff:ssizetype { swap_p ? @4 : @3; }
9244 { swap_p ? @0 : @2; }))
9246 (gt (convert:sizetype
9247 (pointer_diff:ssizetype
9248 (pointer_plus { swap_p ? @2 : @0; }
9249 { wide_int_to_tree (sizetype, off); })
9250 { swap_p ? @0 : @2; }))
9251 { rhs_tree; })))))))))
9253 /* Fold REDUC (@0 & @1) -> @0[I] & @1[I] if element I is the only nonzero
9255 (for reduc (IFN_REDUC_PLUS IFN_REDUC_IOR IFN_REDUC_XOR)
9256 (simplify (reduc (view_convert? (bit_and @0 VECTOR_CST@1)))
9257 (with { int i = single_nonzero_element (@1); }
9259 (with { tree elt = vector_cst_elt (@1, i);
9260 tree elt_type = TREE_TYPE (elt);
9261 unsigned int elt_bits = tree_to_uhwi (TYPE_SIZE (elt_type));
9262 tree size = bitsize_int (elt_bits);
9263 tree pos = bitsize_int (elt_bits * i); }
9266 (BIT_FIELD_REF:elt_type @0 { size; } { pos; })
9269 /* Fold reduction of a single nonzero element constructor. */
9270 (for reduc (IFN_REDUC_PLUS IFN_REDUC_IOR IFN_REDUC_XOR)
9271 (simplify (reduc (CONSTRUCTOR@0))
9272 (with { tree ctor = (TREE_CODE (@0) == SSA_NAME
9273 ? gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0)) : @0);
9274 tree elt = ctor_single_nonzero_element (ctor); }
9276 && !HONOR_SNANS (type)
9277 && !HONOR_SIGNED_ZEROS (type))
9280 /* Fold REDUC (@0 op VECTOR_CST) as REDUC (@0) op REDUC (VECTOR_CST). */
9281 (for reduc (IFN_REDUC_PLUS IFN_REDUC_MAX IFN_REDUC_MIN IFN_REDUC_FMAX
9282 IFN_REDUC_FMIN IFN_REDUC_AND IFN_REDUC_IOR IFN_REDUC_XOR)
9283 op (plus max min IFN_FMAX IFN_FMIN bit_and bit_ior bit_xor)
9284 (simplify (reduc (op @0 VECTOR_CST@1))
9285 (op (reduc:type @0) (reduc:type @1))))
9287 /* Simplify vector floating point operations of alternating sub/add pairs
9288 into using an fneg of a wider element type followed by a normal add.
9289 under IEEE 754 the fneg of the wider type will negate every even entry
9290 and when doing an add we get a sub of the even and add of every odd
9292 (for plusminus (plus minus)
9293 minusplus (minus plus)
9295 (vec_perm (plusminus @0 @1) (minusplus @2 @3) VECTOR_CST@4)
9296 (if (!VECTOR_INTEGER_TYPE_P (type)
9297 && !FLOAT_WORDS_BIG_ENDIAN
9298 /* plus is commutative, while minus is not, so :c can't be used.
9299 Do equality comparisons by hand and at the end pick the operands
9301 && (operand_equal_p (@0, @2, 0)
9302 ? operand_equal_p (@1, @3, 0)
9303 : operand_equal_p (@0, @3, 0) && operand_equal_p (@1, @2, 0)))
9306 /* Build a vector of integers from the tree mask. */
9307 vec_perm_builder builder;
9309 (if (tree_to_vec_perm_builder (&builder, @4))
9312 /* Create a vec_perm_indices for the integer vector. */
9313 poly_uint64 nelts = TYPE_VECTOR_SUBPARTS (type);
9314 vec_perm_indices sel (builder, 2, nelts);
9315 machine_mode vec_mode = TYPE_MODE (type);
9316 machine_mode wide_mode;
9317 scalar_mode wide_elt_mode;
9318 poly_uint64 wide_nunits;
9319 scalar_mode inner_mode = GET_MODE_INNER (vec_mode);
9321 (if (VECTOR_MODE_P (vec_mode)
9322 && sel.series_p (0, 2, 0, 2)
9323 && sel.series_p (1, 2, nelts + 1, 2)
9324 && GET_MODE_2XWIDER_MODE (inner_mode).exists (&wide_elt_mode)
9325 && multiple_p (GET_MODE_NUNITS (vec_mode), 2, &wide_nunits)
9326 && related_vector_mode (vec_mode, wide_elt_mode,
9327 wide_nunits).exists (&wide_mode))
9331 = lang_hooks.types.type_for_mode (GET_MODE_INNER (wide_mode),
9332 TYPE_UNSIGNED (type));
9333 tree ntype = build_vector_type_for_mode (stype, wide_mode);
9335 /* The format has to be a non-extended ieee format. */
9336 const struct real_format *fmt_old = FLOAT_MODE_FORMAT (vec_mode);
9337 const struct real_format *fmt_new = FLOAT_MODE_FORMAT (wide_mode);
9339 (if (TYPE_MODE (stype) != BLKmode
9340 && VECTOR_TYPE_P (ntype)
9345 /* If the target doesn't support v1xx vectors, try using
9346 scalar mode xx instead. */
9347 if (known_eq (GET_MODE_NUNITS (wide_mode), 1)
9348 && !target_supports_op_p (ntype, NEGATE_EXPR, optab_vector))
9351 (if (fmt_new->signbit_rw
9352 == fmt_old->signbit_rw + GET_MODE_UNIT_BITSIZE (vec_mode)
9353 && fmt_new->signbit_rw == fmt_new->signbit_ro
9354 && targetm.can_change_mode_class (TYPE_MODE (ntype),
9355 TYPE_MODE (type), ALL_REGS)
9356 && ((optimize_vectors_before_lowering_p ()
9357 && VECTOR_TYPE_P (ntype))
9358 || target_supports_op_p (ntype, NEGATE_EXPR, optab_vector)))
9359 (if (plusminus == PLUS_EXPR)
9360 (plus (view_convert:type (negate (view_convert:ntype @3))) @2)
9361 (minus @0 (view_convert:type
9362 (negate (view_convert:ntype @1))))))))))))))))
9365 (vec_perm @0 @1 VECTOR_CST@2)
9368 tree op0 = @0, op1 = @1, op2 = @2;
9369 machine_mode result_mode = TYPE_MODE (type);
9370 machine_mode op_mode = TYPE_MODE (TREE_TYPE (op0));
9372 /* Build a vector of integers from the tree mask. */
9373 vec_perm_builder builder;
9375 (if (tree_to_vec_perm_builder (&builder, op2))
9378 /* Create a vec_perm_indices for the integer vector. */
9379 poly_uint64 nelts = TYPE_VECTOR_SUBPARTS (type);
9380 bool single_arg = (op0 == op1);
9381 vec_perm_indices sel (builder, single_arg ? 1 : 2, nelts);
9383 (if (sel.series_p (0, 1, 0, 1))
9385 (if (sel.series_p (0, 1, nelts, 1))
9391 if (sel.all_from_input_p (0))
9393 else if (sel.all_from_input_p (1))
9396 sel.rotate_inputs (1);
9398 else if (known_ge (poly_uint64 (sel[0]), nelts))
9400 std::swap (op0, op1);
9401 sel.rotate_inputs (1);
9405 tree cop0 = op0, cop1 = op1;
9406 if (TREE_CODE (op0) == SSA_NAME
9407 && (def = dyn_cast <gassign *> (SSA_NAME_DEF_STMT (op0)))
9408 && gimple_assign_rhs_code (def) == CONSTRUCTOR)
9409 cop0 = gimple_assign_rhs1 (def);
9410 if (TREE_CODE (op1) == SSA_NAME
9411 && (def = dyn_cast <gassign *> (SSA_NAME_DEF_STMT (op1)))
9412 && gimple_assign_rhs_code (def) == CONSTRUCTOR)
9413 cop1 = gimple_assign_rhs1 (def);
9416 (if ((TREE_CODE (cop0) == VECTOR_CST
9417 || TREE_CODE (cop0) == CONSTRUCTOR)
9418 && (TREE_CODE (cop1) == VECTOR_CST
9419 || TREE_CODE (cop1) == CONSTRUCTOR)
9420 && (t = fold_vec_perm (type, cop0, cop1, sel)))
9424 bool changed = (op0 == op1 && !single_arg);
9425 tree ins = NULL_TREE;
9428 /* See if the permutation is performing a single element
9429 insert from a CONSTRUCTOR or constant and use a BIT_INSERT_EXPR
9430 in that case. But only if the vector mode is supported,
9431 otherwise this is invalid GIMPLE. */
9432 if (op_mode != BLKmode
9433 && (TREE_CODE (cop0) == VECTOR_CST
9434 || TREE_CODE (cop0) == CONSTRUCTOR
9435 || TREE_CODE (cop1) == VECTOR_CST
9436 || TREE_CODE (cop1) == CONSTRUCTOR))
9438 bool insert_first_p = sel.series_p (1, 1, nelts + 1, 1);
9441 /* After canonicalizing the first elt to come from the
9442 first vector we only can insert the first elt from
9443 the first vector. */
9445 if ((ins = fold_read_from_vector (cop0, sel[0])))
9448 /* The above can fail for two-element vectors which always
9449 appear to insert the first element, so try inserting
9450 into the second lane as well. For more than two
9451 elements that's wasted time. */
9452 if (!insert_first_p || (!ins && maybe_eq (nelts, 2u)))
9454 unsigned int encoded_nelts = sel.encoding ().encoded_nelts ();
9455 for (at = 0; at < encoded_nelts; ++at)
9456 if (maybe_ne (sel[at], at))
9458 if (at < encoded_nelts
9459 && (known_eq (at + 1, nelts)
9460 || sel.series_p (at + 1, 1, at + 1, 1)))
9462 if (known_lt (poly_uint64 (sel[at]), nelts))
9463 ins = fold_read_from_vector (cop0, sel[at]);
9465 ins = fold_read_from_vector (cop1, sel[at] - nelts);
9470 /* Generate a canonical form of the selector. */
9471 if (!ins && sel.encoding () != builder)
9473 /* Some targets are deficient and fail to expand a single
9474 argument permutation while still allowing an equivalent
9475 2-argument version. */
9477 if (sel.ninputs () == 2
9478 || can_vec_perm_const_p (result_mode, op_mode, sel, false))
9479 op2 = vec_perm_indices_to_tree (TREE_TYPE (op2), sel);
9482 vec_perm_indices sel2 (builder, 2, nelts);
9483 if (can_vec_perm_const_p (result_mode, op_mode, sel2, false))
9484 op2 = vec_perm_indices_to_tree (TREE_TYPE (op2), sel2);
9486 /* Not directly supported with either encoding,
9487 so use the preferred form. */
9488 op2 = vec_perm_indices_to_tree (TREE_TYPE (op2), sel);
9490 if (!operand_equal_p (op2, oldop2, 0))
9495 (bit_insert { op0; } { ins; }
9496 { bitsize_int (at * vector_element_bits (type)); })
9498 (vec_perm { op0; } { op1; } { op2; }))))))))))))
9500 /* VEC_PERM_EXPR (v, v, mask) -> v where v contains same element. */
9502 (match vec_same_elem_p
9505 (match vec_same_elem_p
9507 (if (TREE_CODE (@0) == SSA_NAME
9508 && uniform_vector_p (gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0))))))
9510 (match vec_same_elem_p
9512 (if (uniform_vector_p (@0))))
9516 (vec_perm vec_same_elem_p@0 @0 @1)
9517 (if (types_match (type, TREE_TYPE (@0)))
9521 tree elem = uniform_vector_p (@0);
9524 { build_vector_from_val (type, elem); }))))
9526 /* Push VEC_PERM earlier if that may help FMA perception (PR101895). */
9528 (plus:c (vec_perm:s (mult:c@0 @1 vec_same_elem_p@2) @0 @3) @4)
9529 (if (TREE_CODE (@0) == SSA_NAME && num_imm_uses (@0) == 2)
9530 (plus (mult (vec_perm @1 @1 @3) @2) @4)))
9532 (minus (vec_perm:s (mult:c@0 @1 vec_same_elem_p@2) @0 @3) @4)
9533 (if (TREE_CODE (@0) == SSA_NAME && num_imm_uses (@0) == 2)
9534 (minus (mult (vec_perm @1 @1 @3) @2) @4)))
9538 c = VEC_PERM_EXPR <a, b, VCST0>;
9539 d = VEC_PERM_EXPR <c, c, VCST1>;
9541 d = VEC_PERM_EXPR <a, b, NEW_VCST>; */
9544 (vec_perm (vec_perm@0 @1 @2 VECTOR_CST@3) @0 VECTOR_CST@4)
9545 (if (TYPE_VECTOR_SUBPARTS (type).is_constant ())
9548 machine_mode result_mode = TYPE_MODE (type);
9549 machine_mode op_mode = TYPE_MODE (TREE_TYPE (@1));
9550 int nelts = TYPE_VECTOR_SUBPARTS (type).to_constant ();
9551 vec_perm_builder builder0;
9552 vec_perm_builder builder1;
9553 vec_perm_builder builder2 (nelts, nelts, 1);
9555 (if (tree_to_vec_perm_builder (&builder0, @3)
9556 && tree_to_vec_perm_builder (&builder1, @4))
9559 vec_perm_indices sel0 (builder0, 2, nelts);
9560 vec_perm_indices sel1 (builder1, 1, nelts);
9562 for (int i = 0; i < nelts; i++)
9563 builder2.quick_push (sel0[sel1[i].to_constant ()]);
9565 vec_perm_indices sel2 (builder2, 2, nelts);
9567 tree op0 = NULL_TREE;
9568 /* If the new VEC_PERM_EXPR can't be handled but both
9569 original VEC_PERM_EXPRs can, punt.
9570 If one or both of the original VEC_PERM_EXPRs can't be
9571 handled and the new one can't be either, don't increase
9572 number of VEC_PERM_EXPRs that can't be handled. */
9573 if (can_vec_perm_const_p (result_mode, op_mode, sel2, false)
9575 ? (!can_vec_perm_const_p (result_mode, op_mode, sel0, false)
9576 || !can_vec_perm_const_p (result_mode, op_mode, sel1, false))
9577 : !can_vec_perm_const_p (result_mode, op_mode, sel1, false)))
9578 op0 = vec_perm_indices_to_tree (TREE_TYPE (@4), sel2);
9581 (vec_perm @1 @2 { op0; })))))))
9584 c = VEC_PERM_EXPR <a, b, VCST0>;
9585 d = VEC_PERM_EXPR <x, c, VCST1>;
9587 d = VEC_PERM_EXPR <x, {a,b}, NEW_VCST>;
9588 when all elements from a or b are replaced by the later
9592 (vec_perm @5 (vec_perm@0 @1 @2 VECTOR_CST@3) VECTOR_CST@4)
9593 (if (TYPE_VECTOR_SUBPARTS (type).is_constant ())
9596 machine_mode result_mode = TYPE_MODE (type);
9597 machine_mode op_mode = TYPE_MODE (TREE_TYPE (@1));
9598 int nelts = TYPE_VECTOR_SUBPARTS (type).to_constant ();
9599 vec_perm_builder builder0;
9600 vec_perm_builder builder1;
9601 vec_perm_builder builder2 (nelts, nelts, 2);
9603 (if (tree_to_vec_perm_builder (&builder0, @3)
9604 && tree_to_vec_perm_builder (&builder1, @4))
9607 vec_perm_indices sel0 (builder0, 2, nelts);
9608 vec_perm_indices sel1 (builder1, 2, nelts);
9609 bool use_1 = false, use_2 = false;
9611 for (int i = 0; i < nelts; i++)
9613 if (known_lt ((poly_uint64)sel1[i], sel1.nelts_per_input ()))
9614 builder2.quick_push (sel1[i]);
9617 poly_uint64 j = sel0[(sel1[i] - sel1.nelts_per_input ())
9619 if (known_lt (j, sel0.nelts_per_input ()))
9624 j -= sel0.nelts_per_input ();
9626 builder2.quick_push (j + sel1.nelts_per_input ());
9633 vec_perm_indices sel2 (builder2, 2, nelts);
9634 tree op0 = NULL_TREE;
9635 /* If the new VEC_PERM_EXPR can't be handled but both
9636 original VEC_PERM_EXPRs can, punt.
9637 If one or both of the original VEC_PERM_EXPRs can't be
9638 handled and the new one can't be either, don't increase
9639 number of VEC_PERM_EXPRs that can't be handled. */
9640 if (can_vec_perm_const_p (result_mode, op_mode, sel2, false)
9642 ? (!can_vec_perm_const_p (result_mode, op_mode, sel0, false)
9643 || !can_vec_perm_const_p (result_mode, op_mode, sel1, false))
9644 : !can_vec_perm_const_p (result_mode, op_mode, sel1, false)))
9645 op0 = vec_perm_indices_to_tree (TREE_TYPE (@4), sel2);
9650 (vec_perm @5 @1 { op0; }))
9652 (vec_perm @5 @2 { op0; })))))))))))
9654 /* And the case with swapped outer permute sources. */
9657 (vec_perm (vec_perm@0 @1 @2 VECTOR_CST@3) @5 VECTOR_CST@4)
9658 (if (TYPE_VECTOR_SUBPARTS (type).is_constant ())
9661 machine_mode result_mode = TYPE_MODE (type);
9662 machine_mode op_mode = TYPE_MODE (TREE_TYPE (@1));
9663 int nelts = TYPE_VECTOR_SUBPARTS (type).to_constant ();
9664 vec_perm_builder builder0;
9665 vec_perm_builder builder1;
9666 vec_perm_builder builder2 (nelts, nelts, 2);
9668 (if (tree_to_vec_perm_builder (&builder0, @3)
9669 && tree_to_vec_perm_builder (&builder1, @4))
9672 vec_perm_indices sel0 (builder0, 2, nelts);
9673 vec_perm_indices sel1 (builder1, 2, nelts);
9674 bool use_1 = false, use_2 = false;
9676 for (int i = 0; i < nelts; i++)
9678 if (known_ge ((poly_uint64)sel1[i], sel1.nelts_per_input ()))
9679 builder2.quick_push (sel1[i]);
9682 poly_uint64 j = sel0[sel1[i].to_constant ()];
9683 if (known_lt (j, sel0.nelts_per_input ()))
9688 j -= sel0.nelts_per_input ();
9690 builder2.quick_push (j);
9697 vec_perm_indices sel2 (builder2, 2, nelts);
9698 tree op0 = NULL_TREE;
9699 /* If the new VEC_PERM_EXPR can't be handled but both
9700 original VEC_PERM_EXPRs can, punt.
9701 If one or both of the original VEC_PERM_EXPRs can't be
9702 handled and the new one can't be either, don't increase
9703 number of VEC_PERM_EXPRs that can't be handled. */
9704 if (can_vec_perm_const_p (result_mode, op_mode, sel2, false)
9706 ? (!can_vec_perm_const_p (result_mode, op_mode, sel0, false)
9707 || !can_vec_perm_const_p (result_mode, op_mode, sel1, false))
9708 : !can_vec_perm_const_p (result_mode, op_mode, sel1, false)))
9709 op0 = vec_perm_indices_to_tree (TREE_TYPE (@4), sel2);
9714 (vec_perm @1 @5 { op0; }))
9716 (vec_perm @2 @5 { op0; })))))))))))
9719 /* Match count trailing zeroes for simplify_count_trailing_zeroes in fwprop.
9720 The canonical form is array[((x & -x) * C) >> SHIFT] where C is a magic
9721 constant which when multiplied by a power of 2 contains a unique value
9722 in the top 5 or 6 bits. This is then indexed into a table which maps it
9723 to the number of trailing zeroes. */
9724 (match (ctz_table_index @1 @2 @3)
9725 (rshift (mult (bit_and:c (negate @1) @1) INTEGER_CST@2) INTEGER_CST@3))
9727 (match (cond_expr_convert_p @0 @2 @3 @6)
9728 (cond (simple_comparison@6 @0 @1) (convert@4 @2) (convert@5 @3))
9729 (if (INTEGRAL_TYPE_P (type)
9730 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
9731 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
9732 && INTEGRAL_TYPE_P (TREE_TYPE (@3))
9733 && TYPE_PRECISION (type) != TYPE_PRECISION (TREE_TYPE (@0))
9734 && TYPE_PRECISION (TREE_TYPE (@0))
9735 == TYPE_PRECISION (TREE_TYPE (@2))
9736 && TYPE_PRECISION (TREE_TYPE (@0))
9737 == TYPE_PRECISION (TREE_TYPE (@3))
9738 /* For vect_recog_cond_expr_convert_pattern, @2 and @3 can differ in
9739 signess when convert is truncation, but not ok for extension since
9740 it's sign_extend vs zero_extend. */
9741 && (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type)
9742 || (TYPE_UNSIGNED (TREE_TYPE (@2))
9743 == TYPE_UNSIGNED (TREE_TYPE (@3))))
9745 && single_use (@5))))
9747 (for bit_op (bit_and bit_ior bit_xor)
9748 (match (bitwise_induction_p @0 @2 @3)
9750 (nop_convert1? (bit_not2?@0 (convert3? (lshift integer_onep@1 @2))))
9753 (match (bitwise_induction_p @0 @2 @3)
9755 (nop_convert1? (bit_xor@0 (convert2? (lshift integer_onep@1 @2)) @3))))
9757 /* n - (((n > C1) ? n : C1) & -C2) -> n & C1 for unsigned case.
9758 n - (((n > C1) ? n : C1) & -C2) -> (n <= C1) ? n : (n & C1) for signed case. */
9760 (minus @0 (bit_and (max @0 INTEGER_CST@1) INTEGER_CST@2))
9761 (with { auto i = wi::neg (wi::to_wide (@2)); }
9762 /* Check if -C2 is a power of 2 and C1 = -C2 - 1. */
9763 (if (wi::popcount (i) == 1
9764 && (wi::to_wide (@1)) == (i - 1))
9765 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
9767 (cond (le @0 @1) @0 (bit_and @0 @1))))))
9769 /* -x & 1 -> x & 1. */
9771 (bit_and (negate @0) integer_onep@1)
9772 (if (!TYPE_OVERFLOW_SANITIZED (type))
9775 /* `-a` is just `a` if the type is 1bit wide or when converting
9776 to a 1bit type; similar to the above transformation of `(-x)&1`.
9777 This is used mostly with the transformation of
9778 `a ? ~b : b` into `(-a)^b`.
9779 It also can show up with bitfields. */
9781 (convert? (negate @0))
9782 (if (INTEGRAL_TYPE_P (type)
9783 && TYPE_PRECISION (type) == 1
9784 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0)))
9788 c1 = VEC_PERM_EXPR (a, a, mask)
9789 c2 = VEC_PERM_EXPR (b, b, mask)
9793 c3 = VEC_PERM_EXPR (c, c, mask)
9794 For all integer non-div operations. */
9795 (for op (plus minus mult bit_and bit_ior bit_xor
9798 (op (vec_perm @0 @0 @2) (vec_perm @1 @1 @2))
9799 (if (VECTOR_INTEGER_TYPE_P (type))
9800 (vec_perm (op@3 @0 @1) @3 @2))))
9802 /* Similar for float arithmetic when permutation constant covers
9803 all vector elements. */
9804 (for op (plus minus mult)
9806 (op (vec_perm @0 @0 VECTOR_CST@2) (vec_perm @1 @1 VECTOR_CST@2))
9807 (if (VECTOR_FLOAT_TYPE_P (type)
9808 && TYPE_VECTOR_SUBPARTS (type).is_constant ())
9812 vec_perm_builder builder;
9813 bool full_perm_p = false;
9814 if (tree_to_vec_perm_builder (&builder, perm_cst))
9816 unsigned HOST_WIDE_INT nelts;
9818 nelts = TYPE_VECTOR_SUBPARTS (type).to_constant ();
9819 /* Create a vec_perm_indices for the VECTOR_CST. */
9820 vec_perm_indices sel (builder, 1, nelts);
9822 /* Check if perm indices covers all vector elements. */
9823 if (sel.encoding ().encoded_full_vector_p ())
9825 auto_sbitmap seen (nelts);
9826 bitmap_clear (seen);
9828 unsigned HOST_WIDE_INT count = 0, i;
9830 for (i = 0; i < nelts; i++)
9832 if (!bitmap_set_bit (seen, sel[i].to_constant ()))
9836 full_perm_p = count == nelts;
9841 (vec_perm (op@3 @0 @1) @3 @2))))))