1 /* Match-and-simplify patterns for shared GENERIC and GIMPLE folding.
2 This file is consumed by genmatch which produces gimple-match.cc
3 and generic-match.cc from it.
5 Copyright (C) 2014-2024 Free Software Foundation, Inc.
6 Contributed by Richard Biener <rguenther@suse.de>
7 and Prathamesh Kulkarni <bilbotheelffriend@gmail.com>
9 This file is part of GCC.
11 GCC is free software; you can redistribute it and/or modify it under
12 the terms of the GNU General Public License as published by the Free
13 Software Foundation; either version 3, or (at your option) any later
16 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
17 WARRANTY; without even the implied warranty of MERCHANTABILITY or
18 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21 You should have received a copy of the GNU General Public License
22 along with GCC; see the file COPYING3. If not see
23 <http://www.gnu.org/licenses/>. */
26 /* Generic tree predicates we inherit. */
28 integer_onep integer_zerop integer_all_onesp integer_minus_onep
29 integer_each_onep integer_truep integer_nonzerop
30 real_zerop real_onep real_minus_onep
32 initializer_each_zero_or_onep
34 tree_expr_nonnegative_p
42 bitmask_inv_cst_vector_p)
45 (define_operator_list tcc_comparison
46 lt le eq ne ge gt unordered ordered unlt unle ungt unge uneq ltgt)
47 (define_operator_list inverted_tcc_comparison
48 ge gt ne eq lt le ordered unordered ge gt le lt ltgt uneq)
49 (define_operator_list inverted_tcc_comparison_with_nans
50 unge ungt ne eq unlt unle ordered unordered ge gt le lt ltgt uneq)
51 (define_operator_list swapped_tcc_comparison
52 gt ge eq ne le lt unordered ordered ungt unge unlt unle uneq ltgt)
53 (define_operator_list simple_comparison lt le eq ne ge gt)
54 (define_operator_list swapped_simple_comparison gt ge eq ne le lt)
55 (define_operator_list BSWAP BUILT_IN_BSWAP16 BUILT_IN_BSWAP32
56 BUILT_IN_BSWAP64 BUILT_IN_BSWAP128)
58 #include "cfn-operators.pd"
60 /* Define operand lists for math rounding functions {,i,l,ll}FN,
61 where the versions prefixed with "i" return an int, those prefixed with
62 "l" return a long and those prefixed with "ll" return a long long.
64 Also define operand lists:
66 X<FN>F for all float functions, in the order i, l, ll
67 X<FN> for all double functions, in the same order
68 X<FN>L for all long double functions, in the same order. */
69 #define DEFINE_INT_AND_FLOAT_ROUND_FN(FN) \
70 (define_operator_list X##FN##F BUILT_IN_I##FN##F \
73 (define_operator_list X##FN BUILT_IN_I##FN \
76 (define_operator_list X##FN##L BUILT_IN_I##FN##L \
80 DEFINE_INT_AND_FLOAT_ROUND_FN (FLOOR)
81 DEFINE_INT_AND_FLOAT_ROUND_FN (CEIL)
82 DEFINE_INT_AND_FLOAT_ROUND_FN (ROUND)
83 DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
85 /* Unary operations and their associated IFN_COND_* function. */
86 (define_operator_list UNCOND_UNARY
88 (define_operator_list COND_UNARY
89 IFN_COND_NEG IFN_COND_NOT)
90 (define_operator_list COND_LEN_UNARY
91 IFN_COND_LEN_NEG IFN_COND_LEN_NOT)
93 /* Binary operations and their associated IFN_COND_* function. */
94 (define_operator_list UNCOND_BINARY
96 mult trunc_div trunc_mod rdiv
98 IFN_FMIN IFN_FMAX IFN_COPYSIGN
99 bit_and bit_ior bit_xor
101 (define_operator_list COND_BINARY
102 IFN_COND_ADD IFN_COND_SUB
103 IFN_COND_MUL IFN_COND_DIV IFN_COND_MOD IFN_COND_RDIV
104 IFN_COND_MIN IFN_COND_MAX
105 IFN_COND_FMIN IFN_COND_FMAX IFN_COND_COPYSIGN
106 IFN_COND_AND IFN_COND_IOR IFN_COND_XOR
107 IFN_COND_SHL IFN_COND_SHR)
108 (define_operator_list COND_LEN_BINARY
109 IFN_COND_LEN_ADD IFN_COND_LEN_SUB
110 IFN_COND_LEN_MUL IFN_COND_LEN_DIV
111 IFN_COND_LEN_MOD IFN_COND_LEN_RDIV
112 IFN_COND_LEN_MIN IFN_COND_LEN_MAX
113 IFN_COND_LEN_FMIN IFN_COND_LEN_FMAX IFN_COND_LEN_COPYSIGN
114 IFN_COND_LEN_AND IFN_COND_LEN_IOR IFN_COND_LEN_XOR
115 IFN_COND_LEN_SHL IFN_COND_LEN_SHR)
117 /* Same for ternary operations. */
118 (define_operator_list UNCOND_TERNARY
119 IFN_FMA IFN_FMS IFN_FNMA IFN_FNMS)
120 (define_operator_list COND_TERNARY
121 IFN_COND_FMA IFN_COND_FMS IFN_COND_FNMA IFN_COND_FNMS)
122 (define_operator_list COND_LEN_TERNARY
123 IFN_COND_LEN_FMA IFN_COND_LEN_FMS IFN_COND_LEN_FNMA IFN_COND_LEN_FNMS)
125 /* __atomic_fetch_or_*, __atomic_fetch_xor_*, __atomic_xor_fetch_* */
126 (define_operator_list ATOMIC_FETCH_OR_XOR_N
127 BUILT_IN_ATOMIC_FETCH_OR_1 BUILT_IN_ATOMIC_FETCH_OR_2
128 BUILT_IN_ATOMIC_FETCH_OR_4 BUILT_IN_ATOMIC_FETCH_OR_8
129 BUILT_IN_ATOMIC_FETCH_OR_16
130 BUILT_IN_ATOMIC_FETCH_XOR_1 BUILT_IN_ATOMIC_FETCH_XOR_2
131 BUILT_IN_ATOMIC_FETCH_XOR_4 BUILT_IN_ATOMIC_FETCH_XOR_8
132 BUILT_IN_ATOMIC_FETCH_XOR_16
133 BUILT_IN_ATOMIC_XOR_FETCH_1 BUILT_IN_ATOMIC_XOR_FETCH_2
134 BUILT_IN_ATOMIC_XOR_FETCH_4 BUILT_IN_ATOMIC_XOR_FETCH_8
135 BUILT_IN_ATOMIC_XOR_FETCH_16)
136 /* __sync_fetch_and_or_*, __sync_fetch_and_xor_*, __sync_xor_and_fetch_* */
137 (define_operator_list SYNC_FETCH_OR_XOR_N
138 BUILT_IN_SYNC_FETCH_AND_OR_1 BUILT_IN_SYNC_FETCH_AND_OR_2
139 BUILT_IN_SYNC_FETCH_AND_OR_4 BUILT_IN_SYNC_FETCH_AND_OR_8
140 BUILT_IN_SYNC_FETCH_AND_OR_16
141 BUILT_IN_SYNC_FETCH_AND_XOR_1 BUILT_IN_SYNC_FETCH_AND_XOR_2
142 BUILT_IN_SYNC_FETCH_AND_XOR_4 BUILT_IN_SYNC_FETCH_AND_XOR_8
143 BUILT_IN_SYNC_FETCH_AND_XOR_16
144 BUILT_IN_SYNC_XOR_AND_FETCH_1 BUILT_IN_SYNC_XOR_AND_FETCH_2
145 BUILT_IN_SYNC_XOR_AND_FETCH_4 BUILT_IN_SYNC_XOR_AND_FETCH_8
146 BUILT_IN_SYNC_XOR_AND_FETCH_16)
147 /* __atomic_fetch_and_*. */
148 (define_operator_list ATOMIC_FETCH_AND_N
149 BUILT_IN_ATOMIC_FETCH_AND_1 BUILT_IN_ATOMIC_FETCH_AND_2
150 BUILT_IN_ATOMIC_FETCH_AND_4 BUILT_IN_ATOMIC_FETCH_AND_8
151 BUILT_IN_ATOMIC_FETCH_AND_16)
152 /* __sync_fetch_and_and_*. */
153 (define_operator_list SYNC_FETCH_AND_AND_N
154 BUILT_IN_SYNC_FETCH_AND_AND_1 BUILT_IN_SYNC_FETCH_AND_AND_2
155 BUILT_IN_SYNC_FETCH_AND_AND_4 BUILT_IN_SYNC_FETCH_AND_AND_8
156 BUILT_IN_SYNC_FETCH_AND_AND_16)
158 /* With nop_convert? combine convert? and view_convert? in one pattern
159 plus conditionalize on tree_nop_conversion_p conversions. */
160 (match (nop_convert @0)
162 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))))
163 (match (nop_convert @0)
165 (if (VECTOR_TYPE_P (type) && VECTOR_TYPE_P (TREE_TYPE (@0))
166 && known_eq (TYPE_VECTOR_SUBPARTS (type),
167 TYPE_VECTOR_SUBPARTS (TREE_TYPE (@0)))
168 && tree_nop_conversion_p (TREE_TYPE (type), TREE_TYPE (TREE_TYPE (@0))))))
170 /* These are used by gimple_bitwise_inverted_equal_p to simplify
171 detection of BIT_NOT and comparisons. */
172 (match (bit_not_with_nop @0)
174 (match (bit_not_with_nop @0)
175 (convert (bit_not @0))
176 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))))
177 (match (bit_xor_cst @0 @1)
178 (bit_xor @0 uniform_integer_cst_p@1))
179 (for cmp (tcc_comparison)
180 (match (maybe_cmp @0)
182 (match (maybe_cmp @0)
183 (convert (cmp@0 @1 @2))
184 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))))
186 /* `a ^ b` is another form of `a != b` when the type
187 is a 1bit precission integer. */
188 (match (maybe_cmp @0)
190 (if (INTEGRAL_TYPE_P (type)
191 && TYPE_PRECISION (type) == 1)))
192 /* maybe_bit_not is used to match what
193 is acceptable for bitwise_inverted_equal_p. */
194 (match (maybe_bit_not @0)
195 (bit_not_with_nop@0 @1))
196 (match (maybe_bit_not @0)
198 (match (maybe_bit_not @0)
200 (match (maybe_bit_not @0)
201 (bit_xor_cst@0 @1 @2))
204 (match (maybe_truncate @0)
206 (if (INTEGRAL_TYPE_P (type)
207 && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (@0)))))
210 /* Transform likes of (char) ABS_EXPR <(int) x> into (char) ABSU_EXPR <x>
211 ABSU_EXPR returns unsigned absolute value of the operand and the operand
212 of the ABSU_EXPR will have the corresponding signed type. */
213 (simplify (abs (convert @0))
214 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
215 && !TYPE_UNSIGNED (TREE_TYPE (@0))
216 && element_precision (type) > element_precision (TREE_TYPE (@0)))
217 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
218 (convert (absu:utype @0)))))
221 /* Optimize (X + (X >> (prec - 1))) ^ (X >> (prec - 1)) into abs (X). */
223 (bit_xor:c (plus:c @0 (rshift@2 @0 INTEGER_CST@1)) @2)
224 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
225 && !TYPE_UNSIGNED (TREE_TYPE (@0))
226 && wi::to_widest (@1) == element_precision (TREE_TYPE (@0)) - 1)
230 /* Simplifications of operations with one constant operand and
231 simplifications to constants or single values. */
233 (for op (plus pointer_plus minus bit_ior bit_xor)
235 (op @0 integer_zerop)
238 /* 0 +p index -> (type)index */
240 (pointer_plus integer_zerop @1)
241 (non_lvalue (convert @1)))
243 /* ptr - 0 -> (type)ptr */
245 (pointer_diff @0 integer_zerop)
248 /* See if ARG1 is zero and X + ARG1 reduces to X.
249 Likewise if the operands are reversed. */
251 (plus:c @0 real_zerop@1)
252 (if (fold_real_zero_addition_p (type, @0, @1, 0))
255 /* See if ARG1 is zero and X - ARG1 reduces to X. */
257 (minus @0 real_zerop@1)
258 (if (fold_real_zero_addition_p (type, @0, @1, 1))
261 /* Even if the fold_real_zero_addition_p can't simplify X + 0.0
262 into X, we can optimize (X + 0.0) + 0.0 or (X + 0.0) - 0.0
263 or (X - 0.0) + 0.0 into X + 0.0 and (X - 0.0) - 0.0 into X - 0.0
264 if not -frounding-math. For sNaNs the first operation would raise
265 exceptions but turn the result into qNan, so the second operation
266 would not raise it. */
267 (for inner_op (plus minus)
268 (for outer_op (plus minus)
270 (outer_op (inner_op@3 @0 REAL_CST@1) REAL_CST@2)
273 && !HONOR_SIGN_DEPENDENT_ROUNDING (type))
274 (with { bool inner_plus = ((inner_op == PLUS_EXPR)
275 ^ REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@1)));
277 = ((outer_op == PLUS_EXPR)
278 ^ REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@2))); }
279 (if (outer_plus && !inner_plus)
284 This is unsafe for certain floats even in non-IEEE formats.
285 In IEEE, it is unsafe because it does wrong for NaNs.
286 PR middle-end/98420: x - x may be -0.0 with FE_DOWNWARD.
287 Also note that operand_equal_p is always false if an operand
291 (if (!FLOAT_TYPE_P (type)
292 || (!tree_expr_maybe_nan_p (@0)
293 && !tree_expr_maybe_infinite_p (@0)
294 && (!HONOR_SIGN_DEPENDENT_ROUNDING (type)
295 || !HONOR_SIGNED_ZEROS (type))))
296 { build_zero_cst (type); }))
298 (pointer_diff @@0 @0)
299 { build_zero_cst (type); })
302 (mult @0 integer_zerop@1)
305 /* -x == x -> x == 0 */
308 (cmp:c @0 (negate @0))
309 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
310 && !TYPE_OVERFLOW_WRAPS (TREE_TYPE(@0)))
311 (cmp @0 { build_zero_cst (TREE_TYPE(@0)); }))))
313 /* Maybe fold x * 0 to 0. The expressions aren't the same
314 when x is NaN, since x * 0 is also NaN. Nor are they the
315 same in modes with signed zeros, since multiplying a
316 negative value by 0 gives -0, not +0. Nor when x is +-Inf,
317 since x * 0 is NaN. */
319 (mult @0 real_zerop@1)
320 (if (!tree_expr_maybe_nan_p (@0)
321 && (!HONOR_NANS (type) || !tree_expr_maybe_infinite_p (@0))
322 && (!HONOR_SIGNED_ZEROS (type) || tree_expr_nonnegative_p (@0)))
325 /* In IEEE floating point, x*1 is not equivalent to x for snans.
326 Likewise for complex arithmetic with signed zeros. */
329 (if (!tree_expr_maybe_signaling_nan_p (@0)
330 && (!HONOR_SIGNED_ZEROS (type)
331 || !COMPLEX_FLOAT_TYPE_P (type)))
334 /* Transform x * -1.0 into -x. */
336 (mult @0 real_minus_onep)
337 (if (!tree_expr_maybe_signaling_nan_p (@0)
338 && (!HONOR_SIGNED_ZEROS (type)
339 || !COMPLEX_FLOAT_TYPE_P (type)))
342 /* Transform x * { 0 or 1, 0 or 1, ... } into x & { 0 or -1, 0 or -1, ...},
343 unless the target has native support for the former but not the latter. */
345 (mult @0 VECTOR_CST@1)
346 (if (initializer_each_zero_or_onep (@1)
347 && !HONOR_SNANS (type)
348 && !HONOR_SIGNED_ZEROS (type))
349 (with { tree itype = FLOAT_TYPE_P (type) ? unsigned_type_for (type) : type; }
351 && (!VECTOR_MODE_P (TYPE_MODE (type))
352 || (VECTOR_MODE_P (TYPE_MODE (itype))
353 && optab_handler (and_optab,
354 TYPE_MODE (itype)) != CODE_FOR_nothing)))
355 (view_convert (bit_and:itype (view_convert @0)
356 (ne @1 { build_zero_cst (type); })))))))
358 /* In SWAR (SIMD within a register) code a signed comparison of packed data
359 can be constructed with a particular combination of shift, bitwise and,
360 and multiplication by constants. If that code is vectorized we can
361 convert this pattern into a more efficient vector comparison. */
363 (mult (bit_and (rshift @0 uniform_integer_cst_p@1)
364 uniform_integer_cst_p@2)
365 uniform_integer_cst_p@3)
367 tree rshift_cst = uniform_integer_cst_p (@1);
368 tree bit_and_cst = uniform_integer_cst_p (@2);
369 tree mult_cst = uniform_integer_cst_p (@3);
371 /* Make sure we're working with vectors and uniform vector constants. */
372 (if (VECTOR_TYPE_P (type)
373 && tree_fits_uhwi_p (rshift_cst)
374 && tree_fits_uhwi_p (mult_cst)
375 && tree_fits_uhwi_p (bit_and_cst))
376 /* Compute what constants would be needed for this to represent a packed
377 comparison based on the shift amount denoted by RSHIFT_CST. */
379 HOST_WIDE_INT vec_elem_bits = vector_element_bits (type);
380 poly_int64 vec_nelts = TYPE_VECTOR_SUBPARTS (type);
381 poly_int64 vec_bits = vec_elem_bits * vec_nelts;
382 unsigned HOST_WIDE_INT cmp_bits_i, bit_and_i, mult_i;
383 unsigned HOST_WIDE_INT target_mult_i, target_bit_and_i;
384 cmp_bits_i = tree_to_uhwi (rshift_cst) + 1;
385 mult_i = tree_to_uhwi (mult_cst);
386 target_mult_i = (HOST_WIDE_INT_1U << cmp_bits_i) - 1;
387 bit_and_i = tree_to_uhwi (bit_and_cst);
388 target_bit_and_i = 0;
390 /* The bit pattern in BIT_AND_I should be a mask for the least
391 significant bit of each packed element that is CMP_BITS wide. */
392 for (unsigned i = 0; i < vec_elem_bits / cmp_bits_i; i++)
393 target_bit_and_i = (target_bit_and_i << cmp_bits_i) | 1U;
395 (if ((exact_log2 (cmp_bits_i)) >= 0
396 && cmp_bits_i < HOST_BITS_PER_WIDE_INT
397 && multiple_p (vec_bits, cmp_bits_i)
398 && vec_elem_bits <= HOST_BITS_PER_WIDE_INT
399 && target_mult_i == mult_i
400 && target_bit_and_i == bit_and_i)
401 /* Compute the vector shape for the comparison and check if the target is
402 able to expand the comparison with that type. */
404 /* We're doing a signed comparison. */
405 tree cmp_type = build_nonstandard_integer_type (cmp_bits_i, 0);
406 poly_int64 vector_type_nelts = exact_div (vec_bits, cmp_bits_i);
407 tree vec_cmp_type = build_vector_type (cmp_type, vector_type_nelts);
408 tree vec_truth_type = truth_type_for (vec_cmp_type);
409 tree zeros = build_zero_cst (vec_cmp_type);
410 tree ones = build_all_ones_cst (vec_cmp_type);
412 (if (expand_vec_cmp_expr_p (vec_cmp_type, vec_truth_type, LT_EXPR)
413 && expand_vec_cond_expr_p (vec_cmp_type, vec_truth_type, LT_EXPR))
414 (view_convert:type (vec_cond (lt:vec_truth_type
415 (view_convert:vec_cmp_type @0)
417 { ones; } { zeros; })))))))))
419 (for cmp (gt ge lt le)
420 outp (convert convert negate negate)
421 outn (negate negate convert convert)
422 /* Transform X * (X > 0.0 ? 1.0 : -1.0) into abs(X). */
423 /* Transform X * (X >= 0.0 ? 1.0 : -1.0) into abs(X). */
424 /* Transform X * (X < 0.0 ? 1.0 : -1.0) into -abs(X). */
425 /* Transform X * (X <= 0.0 ? 1.0 : -1.0) into -abs(X). */
427 (mult:c @0 (cond (cmp @0 real_zerop) real_onep@1 real_minus_onep))
428 (if (!tree_expr_maybe_nan_p (@0) && !HONOR_SIGNED_ZEROS (type))
430 /* Transform X * (X > 0.0 ? -1.0 : 1.0) into -abs(X). */
431 /* Transform X * (X >= 0.0 ? -1.0 : 1.0) into -abs(X). */
432 /* Transform X * (X < 0.0 ? -1.0 : 1.0) into abs(X). */
433 /* Transform X * (X <= 0.0 ? -1.0 : 1.0) into abs(X). */
435 (mult:c @0 (cond (cmp @0 real_zerop) real_minus_onep real_onep@1))
436 (if (!tree_expr_maybe_nan_p (@0) && !HONOR_SIGNED_ZEROS (type))
439 /* Transform X * copysign (1.0, X) into abs(X). */
441 (mult:c @0 (COPYSIGN_ALL real_onep @0))
442 (if (!tree_expr_maybe_nan_p (@0) && !HONOR_SIGNED_ZEROS (type))
445 /* Transform X * copysign (1.0, -X) into -abs(X). */
447 (mult:c @0 (COPYSIGN_ALL real_onep (negate @0)))
448 (if (!tree_expr_maybe_nan_p (@0) && !HONOR_SIGNED_ZEROS (type))
451 /* Transform copysign (CST, X) into copysign (ABS(CST), X). */
453 (COPYSIGN_ALL REAL_CST@0 @1)
454 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@0)))
455 (COPYSIGN_ALL (negate @0) @1)))
457 /* Transform c ? x * copysign (1, y) : z to c ? x ^ signs(y) : z.
458 tree-ssa-math-opts.cc does the corresponding optimization for
459 unconditional multiplications (via xorsign). */
461 (IFN_COND_MUL:c @0 @1 (IFN_COPYSIGN real_onep @2) @3)
462 (with { tree signs = sign_mask_for (type); }
464 (with { tree inttype = TREE_TYPE (signs); }
466 (IFN_COND_XOR:inttype @0
467 (view_convert:inttype @1)
468 (bit_and (view_convert:inttype @2) { signs; })
469 (view_convert:inttype @3)))))))
471 /* (x >= 0 ? x : 0) + (x <= 0 ? -x : 0) -> abs x. */
473 (plus:c (max @0 integer_zerop) (max (negate @0) integer_zerop))
474 (if (ANY_INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_UNDEFINED (type))
477 /* X * 1, X / 1 -> X. */
478 (for op (mult trunc_div ceil_div floor_div round_div exact_div)
483 /* (A / (1 << B)) -> (A >> B).
484 Only for unsigned A. For signed A, this would not preserve rounding
486 For example: (-1 / ( 1 << B)) != -1 >> B.
487 Also handle widening conversions, like:
488 (A / (unsigned long long) (1U << B)) -> (A >> B)
490 (A / (unsigned long long) (1 << B)) -> (A >> B).
491 If the left shift is signed, it can be done only if the upper bits
492 of A starting from shift's type sign bit are zero, as
493 (unsigned long long) (1 << 31) is -2147483648ULL, not 2147483648ULL,
494 so it is valid only if A >> 31 is zero. */
496 (trunc_div (convert?@0 @3) (convert2? (lshift integer_onep@1 @2)))
497 (if ((TYPE_UNSIGNED (type) || tree_expr_nonnegative_p (@0))
498 && (!VECTOR_TYPE_P (type)
499 || target_supports_op_p (type, RSHIFT_EXPR, optab_vector)
500 || target_supports_op_p (type, RSHIFT_EXPR, optab_scalar))
501 && (useless_type_conversion_p (type, TREE_TYPE (@1))
502 || (element_precision (type) >= element_precision (TREE_TYPE (@1))
503 && (TYPE_UNSIGNED (TREE_TYPE (@1))
504 || (element_precision (type)
505 == element_precision (TREE_TYPE (@1)))
506 || (INTEGRAL_TYPE_P (type)
507 && (tree_nonzero_bits (@0)
508 & wi::mask (element_precision (TREE_TYPE (@1)) - 1,
510 element_precision (type))) == 0)))))
511 (if (!VECTOR_TYPE_P (type)
512 && useless_type_conversion_p (TREE_TYPE (@3), TREE_TYPE (@1))
513 && element_precision (TREE_TYPE (@3)) < element_precision (type))
514 (convert (rshift @3 @2))
517 /* Preserve explicit divisions by 0: the C++ front-end wants to detect
518 undefined behavior in constexpr evaluation, and assuming that the division
519 traps enables better optimizations than these anyway. */
520 (for div (trunc_div ceil_div floor_div round_div exact_div)
521 /* 0 / X is always zero. */
523 (div integer_zerop@0 @1)
524 /* But not for 0 / 0 so that we can get the proper warnings and errors. */
525 (if (!integer_zerop (@1))
529 (div @0 integer_minus_onep@1)
530 (if (!TYPE_UNSIGNED (type))
532 /* X / bool_range_Y is X. */
535 (if (INTEGRAL_TYPE_P (type)
536 && ssa_name_has_boolean_range (@1)
537 && !flag_non_call_exceptions)
542 /* But not for 0 / 0 so that we can get the proper warnings and errors.
543 And not for _Fract types where we can't build 1. */
544 (if (!ALL_FRACT_MODE_P (TYPE_MODE (type))
545 && !integer_zerop (@0)
546 && (!flag_non_call_exceptions || tree_expr_nonzero_p (@0)))
547 { build_one_cst (type); }))
548 /* X / abs (X) is X < 0 ? -1 : 1. */
551 (if (INTEGRAL_TYPE_P (type)
552 && TYPE_OVERFLOW_UNDEFINED (type)
553 && !integer_zerop (@0)
554 && (!flag_non_call_exceptions || tree_expr_nonzero_p (@0)))
555 (cond (lt @0 { build_zero_cst (type); })
556 { build_minus_one_cst (type); } { build_one_cst (type); })))
559 (div:C @0 (negate @0))
560 (if ((INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
561 && TYPE_OVERFLOW_UNDEFINED (type)
562 && !integer_zerop (@0)
563 && (!flag_non_call_exceptions || tree_expr_nonzero_p (@0)))
564 { build_minus_one_cst (type); })))
566 /* For unsigned integral types, FLOOR_DIV_EXPR is the same as
567 TRUNC_DIV_EXPR. Rewrite into the latter in this case. Similarly
568 for MOD instead of DIV. */
569 (for floor_divmod (floor_div floor_mod)
570 trunc_divmod (trunc_div trunc_mod)
573 (if ((INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
574 && TYPE_UNSIGNED (type))
575 (trunc_divmod @0 @1))))
577 /* 1 / X -> X == 1 for unsigned integer X.
578 1 / X -> X >= -1 && X <= 1 ? X : 0 for signed integer X.
579 But not for 1 / 0 so that we can get proper warnings and errors,
580 and not for 1-bit integers as they are edge cases better handled
581 elsewhere. Delay the conversion of the signed division until late
582 because `1 / X` is simplier to handle than the resulting COND_EXPR. */
584 (trunc_div integer_onep@0 @1)
585 (if (INTEGRAL_TYPE_P (type)
586 && TYPE_PRECISION (type) > 1
587 && !integer_zerop (@1)
588 && (!flag_non_call_exceptions || tree_expr_nonzero_p (@1)))
589 (if (TYPE_UNSIGNED (type))
590 (convert (eq:boolean_type_node @1 { build_one_cst (type); }))
591 (if (!canonicalize_math_p ())
592 (with { tree utype = unsigned_type_for (type); }
593 (cond (le (plus (convert:utype @1) { build_one_cst (utype); })
594 { build_int_cst (utype, 2); })
595 @1 { build_zero_cst (type); }))))))
597 /* Combine two successive divisions. Note that combining ceil_div
598 and floor_div is trickier and combining round_div even more so. */
599 (for div (trunc_div exact_div)
601 (div (div@3 @0 INTEGER_CST@1) INTEGER_CST@2)
603 wi::overflow_type overflow;
604 wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@2),
605 TYPE_SIGN (type), &overflow);
607 (if (div == EXACT_DIV_EXPR
608 || optimize_successive_divisions_p (@2, @3))
610 (div @0 { wide_int_to_tree (type, mul); })
611 (if (TYPE_UNSIGNED (type)
612 || mul != wi::min_value (TYPE_PRECISION (type), SIGNED))
613 { build_zero_cst (type); }))))))
615 /* Combine successive multiplications. Similar to above, but handling
616 overflow is different. */
618 (mult (mult @0 INTEGER_CST@1) INTEGER_CST@2)
620 wi::overflow_type overflow;
621 wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@2),
622 TYPE_SIGN (type), &overflow);
624 /* Skip folding on overflow: the only special case is @1 * @2 == -INT_MIN,
625 otherwise undefined overflow implies that @0 must be zero. */
626 (if (!overflow || TYPE_OVERFLOW_WRAPS (type))
627 (mult @0 { wide_int_to_tree (type, mul); }))))
629 /* Similar to above, but there could be an extra add/sub between
630 successive multuiplications. */
632 (mult (plus:s (mult:s@4 @0 INTEGER_CST@1) INTEGER_CST@2) INTEGER_CST@3)
634 bool overflowed = true;
635 wi::overflow_type ovf1, ovf2;
636 wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@3),
637 TYPE_SIGN (type), &ovf1);
638 wide_int add = wi::mul (wi::to_wide (@2), wi::to_wide (@3),
639 TYPE_SIGN (type), &ovf2);
640 if (TYPE_OVERFLOW_UNDEFINED (type))
644 if (ovf1 == wi::OVF_NONE && ovf2 == wi::OVF_NONE
645 && get_global_range_query ()->range_of_expr (vr0, @4)
646 && !vr0.varying_p () && !vr0.undefined_p ())
648 wide_int wmin0 = vr0.lower_bound ();
649 wide_int wmax0 = vr0.upper_bound ();
650 wmin0 = wi::mul (wmin0, wi::to_wide (@3), TYPE_SIGN (type), &ovf1);
651 wmax0 = wi::mul (wmax0, wi::to_wide (@3), TYPE_SIGN (type), &ovf2);
652 if (ovf1 == wi::OVF_NONE && ovf2 == wi::OVF_NONE)
654 wi::add (wmin0, add, TYPE_SIGN (type), &ovf1);
655 wi::add (wmax0, add, TYPE_SIGN (type), &ovf2);
656 if (ovf1 == wi::OVF_NONE && ovf2 == wi::OVF_NONE)
665 /* Skip folding on overflow. */
667 (plus (mult @0 { wide_int_to_tree (type, mul); })
668 { wide_int_to_tree (type, add); }))))
670 /* Similar to above, but a multiplication between successive additions. */
672 (plus (mult:s (plus:s @0 INTEGER_CST@1) INTEGER_CST@2) INTEGER_CST@3)
674 bool overflowed = true;
675 wi::overflow_type ovf1;
676 wi::overflow_type ovf2;
677 wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@2),
678 TYPE_SIGN (type), &ovf1);
679 wide_int add = wi::add (mul, wi::to_wide (@3),
680 TYPE_SIGN (type), &ovf2);
681 if (TYPE_OVERFLOW_UNDEFINED (type))
685 if (ovf1 == wi::OVF_NONE && ovf2 == wi::OVF_NONE
686 && get_global_range_query ()->range_of_expr (vr0, @0)
687 && !vr0.varying_p () && !vr0.undefined_p ())
689 wide_int wmin0 = vr0.lower_bound ();
690 wide_int wmax0 = vr0.upper_bound ();
691 wmin0 = wi::mul (wmin0, wi::to_wide (@2), TYPE_SIGN (type), &ovf1);
692 wmax0 = wi::mul (wmax0, wi::to_wide (@2), TYPE_SIGN (type), &ovf2);
693 if (ovf1 == wi::OVF_NONE && ovf2 == wi::OVF_NONE)
695 wi::add (wmin0, mul, TYPE_SIGN (type), &ovf1);
696 wi::add (wmax0, mul, TYPE_SIGN (type), &ovf2);
697 if (ovf1 == wi::OVF_NONE && ovf2 == wi::OVF_NONE)
706 /* Skip folding on overflow. */
708 (plus (mult @0 @2) { wide_int_to_tree (type, add); }))))
710 /* Optimize A / A to 1.0 if we don't care about
711 NaNs or Infinities. */
714 (if (FLOAT_TYPE_P (type)
715 && ! HONOR_NANS (type)
716 && ! HONOR_INFINITIES (type))
717 { build_one_cst (type); }))
719 /* Optimize -A / A to -1.0 if we don't care about
720 NaNs or Infinities. */
722 (rdiv:C @0 (negate @0))
723 (if (FLOAT_TYPE_P (type)
724 && ! HONOR_NANS (type)
725 && ! HONOR_INFINITIES (type))
726 { build_minus_one_cst (type); }))
728 /* PR71078: x / abs(x) -> copysign (1.0, x) */
730 (rdiv:C (convert? @0) (convert? (abs @0)))
731 (if (SCALAR_FLOAT_TYPE_P (type)
732 && ! HONOR_NANS (type)
733 && ! HONOR_INFINITIES (type))
735 (if (types_match (type, float_type_node))
736 (BUILT_IN_COPYSIGNF { build_one_cst (type); } (convert @0)))
737 (if (types_match (type, double_type_node))
738 (BUILT_IN_COPYSIGN { build_one_cst (type); } (convert @0)))
739 (if (types_match (type, long_double_type_node))
740 (BUILT_IN_COPYSIGNL { build_one_cst (type); } (convert @0))))))
742 /* In IEEE floating point, x/1 is not equivalent to x for snans. */
745 (if (!tree_expr_maybe_signaling_nan_p (@0))
748 /* In IEEE floating point, x/-1 is not equivalent to -x for snans. */
750 (rdiv @0 real_minus_onep)
751 (if (!tree_expr_maybe_signaling_nan_p (@0))
754 (if (flag_reciprocal_math)
755 /* Convert (A/B)/C to A/(B*C). */
757 (rdiv (rdiv:s @0 @1) @2)
758 (rdiv @0 (mult @1 @2)))
760 /* Canonicalize x / (C1 * y) to (x * C2) / y. */
762 (rdiv @0 (mult:s @1 REAL_CST@2))
764 { tree tem = const_binop (RDIV_EXPR, type, build_one_cst (type), @2); }
766 (rdiv (mult @0 { tem; } ) @1))))
768 /* Convert A/(B/C) to (A/B)*C */
770 (rdiv @0 (rdiv:s @1 @2))
771 (mult (rdiv @0 @1) @2)))
773 /* Simplify x / (- y) to -x / y. */
775 (rdiv @0 (negate @1))
776 (rdiv (negate @0) @1))
778 (if (flag_unsafe_math_optimizations)
779 /* Simplify (C / x op 0.0) to x op 0.0 for C != 0, C != Inf/Nan.
780 Since C / x may underflow to zero, do this only for unsafe math. */
781 (for op (lt le gt ge)
784 (op (rdiv REAL_CST@0 @1) real_zerop@2)
785 (if (!HONOR_SIGNED_ZEROS (@1) && !HONOR_INFINITIES (@1))
787 (if (real_less (&dconst0, TREE_REAL_CST_PTR (@0)))
789 /* For C < 0, use the inverted operator. */
790 (if (real_less (TREE_REAL_CST_PTR (@0), &dconst0))
793 /* Optimize (X & (-A)) / A where A is a power of 2, to X >> log2(A) */
794 (for div (trunc_div ceil_div floor_div round_div exact_div)
796 (div (convert? (bit_and @0 INTEGER_CST@1)) INTEGER_CST@2)
797 (if (integer_pow2p (@2)
798 && tree_int_cst_sgn (@2) > 0
799 && tree_nop_conversion_p (type, TREE_TYPE (@0))
800 && wi::to_wide (@2) + wi::to_wide (@1) == 0)
802 { build_int_cst (integer_type_node,
803 wi::exact_log2 (wi::to_wide (@2))); }))))
805 /* If ARG1 is a constant, we can convert this to a multiply by the
806 reciprocal. This does not have the same rounding properties,
807 so only do this if -freciprocal-math. We can actually
808 always safely do it if ARG1 is a power of two, but it's hard to
809 tell if it is or not in a portable manner. */
810 (for cst (REAL_CST COMPLEX_CST VECTOR_CST)
814 (if (flag_reciprocal_math
817 { tree tem = const_binop (RDIV_EXPR, type, build_one_cst (type), @1); }
819 (mult @0 { tem; } )))
820 (if (cst != COMPLEX_CST)
821 (with { tree inverse = exact_inverse (type, @1); }
823 (mult @0 { inverse; } ))))))))
825 (for mod (ceil_mod floor_mod round_mod trunc_mod)
826 /* 0 % X is always zero. */
828 (mod integer_zerop@0 @1)
829 /* But not for 0 % 0 so that we can get the proper warnings and errors. */
830 (if (!integer_zerop (@1))
832 /* X % 1 is always zero. */
834 (mod @0 integer_onep)
835 { build_zero_cst (type); })
836 /* X % -1 is zero. */
838 (mod @0 integer_minus_onep@1)
839 (if (!TYPE_UNSIGNED (type))
840 { build_zero_cst (type); }))
844 /* But not for 0 % 0 so that we can get the proper warnings and errors. */
845 (if (!integer_zerop (@0))
846 { build_zero_cst (type); }))
847 /* (X % Y) % Y is just X % Y. */
849 (mod (mod@2 @0 @1) @1)
851 /* From extract_muldiv_1: (X * C1) % C2 is zero if C1 is a multiple of C2. */
853 (mod (mult @0 INTEGER_CST@1) INTEGER_CST@2)
854 (if (ANY_INTEGRAL_TYPE_P (type)
855 && TYPE_OVERFLOW_UNDEFINED (type)
856 && wi::multiple_of_p (wi::to_wide (@1), wi::to_wide (@2),
858 { build_zero_cst (type); }))
859 /* For (X % C) == 0, if X is signed and C is power of 2, use unsigned
860 modulo and comparison, since it is simpler and equivalent. */
863 (cmp (mod @0 integer_pow2p@2) integer_zerop@1)
864 (if (!TYPE_UNSIGNED (TREE_TYPE (@0)))
865 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
866 (cmp (mod (convert:utype @0) (convert:utype @2)) (convert:utype @1)))))))
868 /* X % -C is the same as X % C. */
870 (trunc_mod @0 INTEGER_CST@1)
871 (if (TYPE_SIGN (type) == SIGNED
872 && !TREE_OVERFLOW (@1)
873 && wi::neg_p (wi::to_wide (@1))
874 && !TYPE_OVERFLOW_TRAPS (type)
875 /* Avoid this transformation if C is INT_MIN, i.e. C == -C. */
876 && !sign_bit_p (@1, @1))
877 (trunc_mod @0 (negate @1))))
879 /* X % -Y is the same as X % Y. */
881 (trunc_mod @0 (convert? (negate @1)))
882 (if (INTEGRAL_TYPE_P (type)
883 && !TYPE_UNSIGNED (type)
884 && !TYPE_OVERFLOW_TRAPS (type)
885 && tree_nop_conversion_p (type, TREE_TYPE (@1))
886 /* Avoid this transformation if X might be INT_MIN or
887 Y might be -1, because we would then change valid
888 INT_MIN % -(-1) into invalid INT_MIN % -1. */
889 && (expr_not_equal_to (@0, wi::to_wide (TYPE_MIN_VALUE (type)))
890 || expr_not_equal_to (@1, wi::minus_one (TYPE_PRECISION
892 (trunc_mod @0 (convert @1))))
894 /* X - (X / Y) * Y is the same as X % Y. */
896 (minus (convert1? @0) (convert2? (mult:c (trunc_div @@0 @@1) @1)))
897 (if (INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
898 (convert (trunc_mod @0 @1))))
900 /* x * (1 + y / x) - y -> x - y % x */
902 (minus (mult:cs @0 (plus:s (trunc_div:s @1 @0) integer_onep)) @1)
903 (if (INTEGRAL_TYPE_P (type))
904 (minus @0 (trunc_mod @1 @0))))
906 /* Optimize TRUNC_MOD_EXPR by a power of two into a BIT_AND_EXPR,
907 i.e. "X % C" into "X & (C - 1)", if X and C are positive.
908 Also optimize A % (C << N) where C is a power of 2,
909 to A & ((C << N) - 1).
910 Also optimize "A shift (B % C)", if C is a power of 2, to
911 "A shift (B & (C - 1))". SHIFT operation include "<<" and ">>"
912 and assume (B % C) is nonnegative as shifts negative values would
914 (match (power_of_two_cand @1)
916 (match (power_of_two_cand @1)
917 (lshift INTEGER_CST@1 @2))
918 (for mod (trunc_mod floor_mod)
919 (for shift (lshift rshift)
921 (shift @0 (mod @1 (power_of_two_cand@2 @3)))
922 (if (integer_pow2p (@3) && tree_int_cst_sgn (@3) > 0)
923 (shift @0 (bit_and @1 (minus @2 { build_int_cst (TREE_TYPE (@2),
926 (mod @0 (convert? (power_of_two_cand@1 @2)))
927 (if ((TYPE_UNSIGNED (type) || tree_expr_nonnegative_p (@0))
928 /* Allow any integral conversions of the divisor, except
929 conversion from narrower signed to wider unsigned type
930 where if @1 would be negative power of two, the divisor
931 would not be a power of two. */
932 && INTEGRAL_TYPE_P (type)
933 && INTEGRAL_TYPE_P (TREE_TYPE (@1))
934 && (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@1))
935 || TYPE_UNSIGNED (TREE_TYPE (@1))
936 || !TYPE_UNSIGNED (type))
937 && integer_pow2p (@2) && tree_int_cst_sgn (@2) > 0)
938 (with { tree utype = TREE_TYPE (@1);
939 if (!TYPE_OVERFLOW_WRAPS (utype))
940 utype = unsigned_type_for (utype); }
941 (bit_and @0 (convert (minus (convert:utype @1)
942 { build_one_cst (utype); })))))))
944 /* Simplify (unsigned t * 2)/2 -> unsigned t & 0x7FFFFFFF. */
946 (trunc_div (mult @0 integer_pow2p@1) @1)
947 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) && TYPE_UNSIGNED (TREE_TYPE (@0)))
948 (bit_and @0 { wide_int_to_tree
949 (type, wi::mask (TYPE_PRECISION (type)
950 - wi::exact_log2 (wi::to_wide (@1)),
951 false, TYPE_PRECISION (type))); })))
953 /* Simplify (unsigned t / 2) * 2 -> unsigned t & ~1. */
955 (mult (trunc_div @0 integer_pow2p@1) @1)
956 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) && TYPE_UNSIGNED (TREE_TYPE (@0)))
957 (bit_and @0 (negate @1))))
959 (for div (trunc_div ceil_div floor_div round_div exact_div)
960 /* Simplify (t * u) / u -> t. */
962 (div (mult:c @0 @1) @1)
963 (if (ANY_INTEGRAL_TYPE_P (type))
964 (if (TYPE_OVERFLOW_UNDEFINED (type) && !TYPE_OVERFLOW_SANITIZED (type))
967 (with {int_range_max vr0, vr1;}
968 (if (INTEGRAL_TYPE_P (type)
969 && get_range_query (cfun)->range_of_expr (vr0, @0)
970 && get_range_query (cfun)->range_of_expr (vr1, @1)
971 && range_op_handler (MULT_EXPR).overflow_free_p (vr0, vr1))
976 /* Simplify (t * u) / v -> t * (u / v) if u is multiple of v. */
978 (div (mult @0 INTEGER_CST@1) INTEGER_CST@2)
979 (if (INTEGRAL_TYPE_P (type)
980 && wi::multiple_of_p (wi::to_widest (@1), wi::to_widest (@2), SIGNED))
981 (if (TYPE_OVERFLOW_UNDEFINED (type) && !TYPE_OVERFLOW_SANITIZED (type))
982 (mult @0 (div! @1 @2))
983 (with {int_range_max vr0, vr1;}
984 (if (get_range_query (cfun)->range_of_expr (vr0, @0)
985 && get_range_query (cfun)->range_of_expr (vr1, @1)
986 && range_op_handler (MULT_EXPR).overflow_free_p (vr0, vr1))
987 (mult @0 (div! @1 @2))))
990 /* Simplify (t * u) / (t * v) -> (u / v) if u is multiple of v. */
992 (div (mult @0 INTEGER_CST@1) (mult @0 INTEGER_CST@2))
993 (if (INTEGRAL_TYPE_P (type)
994 && wi::multiple_of_p (wi::to_widest (@1), wi::to_widest (@2), SIGNED))
995 (if (TYPE_OVERFLOW_UNDEFINED (type) && !TYPE_OVERFLOW_SANITIZED (type))
998 (with {int_range_max vr0, vr1, vr2;}
999 (if (get_range_query (cfun)->range_of_expr (vr0, @0)
1000 && get_range_query (cfun)->range_of_expr (vr1, @1)
1001 && get_range_query (cfun)->range_of_expr (vr2, @2)
1002 && range_op_handler (MULT_EXPR).overflow_free_p (vr0, vr1)
1003 && range_op_handler (MULT_EXPR).overflow_free_p (vr0, vr2))
1009 (for div (trunc_div exact_div)
1010 /* Simplify (X + M*N) / N -> X / N + M. */
1012 (div (plus:c@4 @0 (mult:c@3 @1 @2)) @2)
1013 (with {int_range_max vr0, vr1, vr2, vr3, vr4;}
1014 (if (INTEGRAL_TYPE_P (type)
1015 && get_range_query (cfun)->range_of_expr (vr1, @1)
1016 && get_range_query (cfun)->range_of_expr (vr2, @2)
1017 /* "N*M" doesn't overflow. */
1018 && range_op_handler (MULT_EXPR).overflow_free_p (vr1, vr2)
1019 && get_range_query (cfun)->range_of_expr (vr0, @0)
1020 && get_range_query (cfun)->range_of_expr (vr3, @3)
1021 /* "X+(N*M)" doesn't overflow. */
1022 && range_op_handler (PLUS_EXPR).overflow_free_p (vr0, vr3)
1023 && get_range_query (cfun)->range_of_expr (vr4, @4)
1024 && !vr4.undefined_p ()
1025 /* "X+N*M" is not with opposite sign as "X". */
1026 && (TYPE_UNSIGNED (type)
1027 || (vr0.nonnegative_p () && vr4.nonnegative_p ())
1028 || (vr0.nonpositive_p () && vr4.nonpositive_p ())))
1029 (plus (div @0 @2) @1))))
1031 /* Simplify (X - M*N) / N -> X / N - M. */
1033 (div (minus@4 @0 (mult:c@3 @1 @2)) @2)
1034 (with {int_range_max vr0, vr1, vr2, vr3, vr4;}
1035 (if (INTEGRAL_TYPE_P (type)
1036 && get_range_query (cfun)->range_of_expr (vr1, @1)
1037 && get_range_query (cfun)->range_of_expr (vr2, @2)
1038 /* "N * M" doesn't overflow. */
1039 && range_op_handler (MULT_EXPR).overflow_free_p (vr1, vr2)
1040 && get_range_query (cfun)->range_of_expr (vr0, @0)
1041 && get_range_query (cfun)->range_of_expr (vr3, @3)
1042 /* "X - (N*M)" doesn't overflow. */
1043 && range_op_handler (MINUS_EXPR).overflow_free_p (vr0, vr3)
1044 && get_range_query (cfun)->range_of_expr (vr4, @4)
1045 && !vr4.undefined_p ()
1046 /* "X-N*M" is not with opposite sign as "X". */
1047 && (TYPE_UNSIGNED (type)
1048 || (vr0.nonnegative_p () && vr4.nonnegative_p ())
1049 || (vr0.nonpositive_p () && vr4.nonpositive_p ())))
1050 (minus (div @0 @2) @1)))))
1053 (X + C) / N -> X / N + C / N where C is multiple of N.
1054 (X + C) >> N -> X >> N + C>>N if low N bits of C is 0. */
1055 (for op (trunc_div exact_div rshift)
1057 (op (plus@3 @0 INTEGER_CST@1) INTEGER_CST@2)
1060 wide_int c = wi::to_wide (@1);
1061 wide_int n = wi::to_wide (@2);
1062 bool shift = op == RSHIFT_EXPR;
1063 #define plus_op1(v) (shift ? wi::rshift (v, n, TYPE_SIGN (type)) \
1064 : wi::div_trunc (v, n, TYPE_SIGN (type)))
1065 #define exact_mod(v) (shift ? wi::ctz (v) >= n.to_shwi () \
1066 : wi::multiple_of_p (v, n, TYPE_SIGN (type)))
1067 int_range_max vr0, vr1, vr3;
1069 (if (INTEGRAL_TYPE_P (type)
1070 && get_range_query (cfun)->range_of_expr (vr0, @0))
1072 && get_range_query (cfun)->range_of_expr (vr1, @1)
1073 /* "X+C" doesn't overflow. */
1074 && range_op_handler (PLUS_EXPR).overflow_free_p (vr0, vr1)
1075 && get_range_query (cfun)->range_of_expr (vr3, @3)
1076 && !vr3.undefined_p ()
1077 /* "X+C" and "X" are not of opposite sign. */
1078 && (TYPE_UNSIGNED (type)
1079 || (vr0.nonnegative_p () && vr3.nonnegative_p ())
1080 || (vr0.nonpositive_p () && vr3.nonpositive_p ())))
1081 (plus (op @0 @2) { wide_int_to_tree (type, plus_op1 (c)); })
1082 (if (!vr0.undefined_p () && TYPE_UNSIGNED (type) && c.sign_mask () < 0
1084 /* unsigned "X-(-C)" doesn't underflow. */
1085 && wi::geu_p (vr0.lower_bound (), -c))
1086 (plus (op @0 @2) { wide_int_to_tree (type, -plus_op1 (-c)); })))))))
1091 /* (nop_outer_cast)-(inner_cast)var -> -(outer_cast)(var)
1092 if var is smaller in precision.
1093 This is always safe for both doing the negative in signed or unsigned
1094 as the value for undefined will not show up.
1095 Note the outer cast cannot be a boolean type as the only valid values
1096 are 0,-1/1 (depending on the signedness of the boolean) and the negative
1097 is there to get the correct value. */
1099 (convert (negate:s@1 (convert:s @0)))
1100 (if (INTEGRAL_TYPE_P (type)
1101 && tree_nop_conversion_p (type, TREE_TYPE (@1))
1102 && TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (@0))
1103 && TREE_CODE (type) != BOOLEAN_TYPE)
1104 (negate (convert @0))))
1106 (for op (negate abs)
1107 /* Simplify cos(-x) and cos(|x|) -> cos(x). Similarly for cosh. */
1108 (for coss (COS COSH)
1112 /* Simplify pow(-x, y) and pow(|x|,y) -> pow(x,y) if y is an even integer. */
1115 (pows (op @0) REAL_CST@1)
1116 (with { HOST_WIDE_INT n; }
1117 (if (real_isinteger (&TREE_REAL_CST (@1), &n) && (n & 1) == 0)
1119 /* Likewise for powi. */
1122 (pows (op @0) INTEGER_CST@1)
1123 (if ((wi::to_wide (@1) & 1) == 0)
1125 /* Strip negate and abs from both operands of hypot. */
1133 /* copysign(-x, y) and copysign(abs(x), y) -> copysign(x, y). */
1134 (for copysigns (COPYSIGN_ALL)
1136 (copysigns (op @0) @1)
1137 (copysigns @0 @1))))
1139 /* abs(x)*abs(x) -> x*x. Should be valid for all types. */
1141 (mult (abs@1 @0) @1)
1144 /* Convert absu(x)*absu(x) -> x*x. */
1146 (mult (absu@1 @0) @1)
1147 (mult (convert@2 @0) @2))
1149 /* cos(copysign(x, y)) -> cos(x). Similarly for cosh. */
1150 (for coss (COS COSH)
1151 (for copysigns (COPYSIGN)
1153 (coss (copysigns @0 @1))
1156 /* pow(copysign(x, y), z) -> pow(x, z) if z is an even integer. */
1158 (for copysigns (COPYSIGN)
1160 (pows (copysigns @0 @2) REAL_CST@1)
1161 (with { HOST_WIDE_INT n; }
1162 (if (real_isinteger (&TREE_REAL_CST (@1), &n) && (n & 1) == 0)
1164 /* Likewise for powi. */
1166 (for copysigns (COPYSIGN)
1168 (pows (copysigns @0 @2) INTEGER_CST@1)
1169 (if ((wi::to_wide (@1) & 1) == 0)
1173 (for copysigns (COPYSIGN)
1174 /* hypot(copysign(x, y), z) -> hypot(x, z). */
1176 (hypots (copysigns @0 @1) @2)
1178 /* hypot(x, copysign(y, z)) -> hypot(x, y). */
1180 (hypots @0 (copysigns @1 @2))
1183 /* copysign(x, CST) -> abs (x). If the target does not
1184 support the copysign optab then canonicalize
1185 copysign(x, -CST) -> fneg (abs (x)). */
1186 (for copysigns (COPYSIGN_ALL)
1188 (copysigns @0 REAL_CST@1)
1189 (if (!REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
1192 (if (!direct_internal_fn_supported_p (IFN_COPYSIGN, type,
1199 /* Transform fneg (fabs (X)) -> copysign (X, -1) as the canonical
1200 representation if the target supports the copysign optab. */
1203 (if (direct_internal_fn_supported_p (IFN_COPYSIGN, type,
1205 (IFN_COPYSIGN @0 { build_minus_one_cst (type); })))
1207 /* copysign(copysign(x, y), z) -> copysign(x, z). */
1208 (for copysigns (COPYSIGN_ALL)
1210 (copysigns (copysigns @0 @1) @2)
1213 /* copysign(x,y)*copysign(x,y) -> x*x. */
1214 (for copysigns (COPYSIGN_ALL)
1216 (mult (copysigns@2 @0 @1) @2)
1219 /* ccos(-x) -> ccos(x). Similarly for ccosh. */
1220 (for ccoss (CCOS CCOSH)
1225 /* cabs(-x) and cos(conj(x)) -> cabs(x). */
1226 (for ops (conj negate)
1232 /* Fold (a * (1 << b)) into (a << b) */
1234 (mult:c @0 (convert? (lshift integer_onep@1 @2)))
1235 (if (! FLOAT_TYPE_P (type)
1236 && tree_nop_conversion_p (type, TREE_TYPE (@1)))
1239 /* Fold a * !a into 0. */
1241 (mult:c @0 (convert? (eq @0 integer_zerop)))
1242 { build_zero_cst (type); })
1244 (mult:c @0 (vec_cond (eq @0 integer_zerop) @1 integer_zerop))
1245 { build_zero_cst (type); })
1247 (mult:c @0 (vec_cond (ne @0 integer_zerop) integer_zerop @1))
1248 { build_zero_cst (type); })
1250 /* Shifts by precision or greater result in zero. */
1251 (for shift (lshift rshift)
1253 (shift @0 uniform_integer_cst_p@1)
1254 (if ((GIMPLE || !sanitize_flags_p (SANITIZE_SHIFT_EXPONENT))
1255 /* Leave arithmetic right shifts of possibly negative values alone. */
1256 && (TYPE_UNSIGNED (type)
1257 || shift == LSHIFT_EXPR
1258 || tree_expr_nonnegative_p (@0))
1259 /* Use a signed compare to leave negative shift counts alone. */
1260 && wi::ges_p (wi::to_wide (uniform_integer_cst_p (@1)),
1261 element_precision (type)))
1262 { build_zero_cst (type); })))
1264 /* Shifts by constants distribute over several binary operations,
1265 hence (X << C) + (Y << C) can be simplified to (X + Y) << C. */
1266 (for op (plus minus)
1268 (op (lshift:s @0 @1) (lshift:s @2 @1))
1269 (if (INTEGRAL_TYPE_P (type)
1270 && TYPE_OVERFLOW_WRAPS (type)
1271 && !TYPE_SATURATING (type))
1272 (lshift (op @0 @2) @1))))
1274 (for op (bit_and bit_ior bit_xor)
1276 (op (lshift:s @0 @1) (lshift:s @2 @1))
1277 (if (INTEGRAL_TYPE_P (type))
1278 (lshift (op @0 @2) @1)))
1280 (op (rshift:s @0 @1) (rshift:s @2 @1))
1281 (if (INTEGRAL_TYPE_P (type))
1282 (rshift (op @0 @2) @1))))
1284 /* Fold (1 << (C - x)) where C = precision(type) - 1
1285 into ((1 << C) >> x). */
1287 (lshift integer_onep@0 (minus@1 INTEGER_CST@2 @3))
1288 (if (INTEGRAL_TYPE_P (type)
1289 && wi::eq_p (wi::to_wide (@2), TYPE_PRECISION (type) - 1)
1291 (if (TYPE_UNSIGNED (type))
1292 (rshift (lshift @0 @2) @3)
1294 { tree utype = unsigned_type_for (type); }
1295 (convert (rshift (lshift (convert:utype @0) @2) @3))))))
1297 /* Fold ((type)(a<0)) << SIGNBITOFA into ((type)a) & signbit. */
1299 (lshift (convert (lt @0 integer_zerop@1)) INTEGER_CST@2)
1300 (if (TYPE_SIGN (TREE_TYPE (@0)) == SIGNED
1301 && wi::eq_p (wi::to_wide (@2), TYPE_PRECISION (TREE_TYPE (@0)) - 1))
1302 (with { wide_int wone = wi::one (TYPE_PRECISION (type)); }
1303 (bit_and (convert @0)
1304 { wide_int_to_tree (type,
1305 wi::lshift (wone, wi::to_wide (@2))); }))))
1307 /* Fold (-x >> C) into -(x > 0) where C = precision(type) - 1. */
1308 (for cst (INTEGER_CST VECTOR_CST)
1310 (rshift (negate:s @0) cst@1)
1311 (if (!TYPE_UNSIGNED (type)
1312 && TYPE_OVERFLOW_UNDEFINED (type))
1313 (with { tree stype = TREE_TYPE (@1);
1314 tree bt = truth_type_for (type);
1315 tree zeros = build_zero_cst (type);
1316 tree cst = NULL_TREE; }
1318 /* Handle scalar case. */
1319 (if (INTEGRAL_TYPE_P (type)
1320 /* If we apply the rule to the scalar type before vectorization
1321 we will enforce the result of the comparison being a bool
1322 which will require an extra AND on the result that will be
1323 indistinguishable from when the user did actually want 0
1324 or 1 as the result so it can't be removed. */
1325 && canonicalize_math_after_vectorization_p ()
1326 && wi::eq_p (wi::to_wide (@1), TYPE_PRECISION (type) - 1))
1327 (negate (convert (gt @0 { zeros; }))))
1328 /* Handle vector case. */
1329 (if (VECTOR_INTEGER_TYPE_P (type)
1330 /* First check whether the target has the same mode for vector
1331 comparison results as it's operands do. */
1332 && TYPE_MODE (bt) == TYPE_MODE (type)
1333 /* Then check to see if the target is able to expand the comparison
1334 with the given type later on, otherwise we may ICE. */
1335 && expand_vec_cmp_expr_p (type, bt, GT_EXPR)
1336 && (cst = uniform_integer_cst_p (@1)) != NULL
1337 && wi::eq_p (wi::to_wide (cst), element_precision (type) - 1))
1338 (view_convert (gt:bt @0 { zeros; }))))))))
1340 /* Fold (C1/X)*C2 into (C1*C2)/X. */
1342 (mult (rdiv@3 REAL_CST@0 @1) REAL_CST@2)
1343 (if (flag_associative_math
1346 { tree tem = const_binop (MULT_EXPR, type, @0, @2); }
1348 (rdiv { tem; } @1)))))
1350 /* Simplify ~X & X as zero. */
1352 (bit_and:c (convert? @0) (convert? (maybe_bit_not @1)))
1353 (with { bool wascmp; }
1354 (if (types_match (TREE_TYPE (@0), TREE_TYPE (@1))
1355 && bitwise_inverted_equal_p (@0, @1, wascmp))
1356 { wascmp ? constant_boolean_node (false, type) : build_zero_cst (type); })))
1358 /* PR71636: Transform x & ((1U << b) - 1) -> x & ~(~0U << b); */
1360 (bit_and:c @0 (plus:s (lshift:s integer_onep @1) integer_minus_onep))
1361 (if (TYPE_UNSIGNED (type))
1362 (bit_and @0 (bit_not (lshift { build_all_ones_cst (type); } @1)))))
1364 (for bitop (bit_and bit_ior)
1366 /* PR35691: Transform
1367 (x == 0 & y == 0) -> (x | typeof(x)(y)) == 0.
1368 (x != 0 | y != 0) -> (x | typeof(x)(y)) != 0. */
1370 (bitop (cmp @0 integer_zerop@2) (cmp @1 integer_zerop))
1371 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1372 && INTEGRAL_TYPE_P (TREE_TYPE (@1))
1373 && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
1374 (cmp (bit_ior @0 (convert @1)) @2)))
1376 (x == -1 & y == -1) -> (x & typeof(x)(y)) == -1.
1377 (x != -1 | y != -1) -> (x & typeof(x)(y)) != -1. */
1379 (bitop (cmp @0 integer_all_onesp@2) (cmp @1 integer_all_onesp))
1380 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1381 && INTEGRAL_TYPE_P (TREE_TYPE (@1))
1382 && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
1383 (cmp (bit_and @0 (convert @1)) @2))))
1385 /* Fold (A & ~B) - (A & B) into (A ^ B) - B. */
1387 (minus (bit_and:cs @0 (bit_not @1)) (bit_and:cs @0 @1))
1388 (minus (bit_xor @0 @1) @1))
1390 (minus (bit_and:s @0 INTEGER_CST@2) (bit_and:s @0 INTEGER_CST@1))
1391 (if (~wi::to_wide (@2) == wi::to_wide (@1))
1392 (minus (bit_xor @0 @1) @1)))
1394 /* Fold (A & B) - (A & ~B) into B - (A ^ B). */
1396 (minus (bit_and:cs @0 @1) (bit_and:cs @0 (bit_not @1)))
1397 (minus @1 (bit_xor @0 @1)))
1399 /* Simplify (X & ~Y) |^+ (~X & Y) -> X ^ Y. */
1400 (for op (bit_ior bit_xor plus)
1402 (op (bit_and:c @0 @2) (bit_and:c @3 @1))
1403 (with { bool wascmp0, wascmp1; }
1404 (if (bitwise_inverted_equal_p (@2, @1, wascmp0)
1405 && bitwise_inverted_equal_p (@0, @3, wascmp1)
1406 && ((!wascmp0 && !wascmp1)
1407 || element_precision (type) == 1))
1410 /* PR53979: Transform ((a ^ b) | a) -> (a | b) */
1412 (bit_ior:c (bit_xor:c @0 @1) @0)
1415 /* (a & ~b) | (a ^ b) --> a ^ b */
1417 (bit_ior:c (bit_and:c @0 (bit_not @1)) (bit_xor:c@2 @0 @1))
1420 /* (a & ~b) ^ ~a --> ~(a & b) */
1422 (bit_xor:c (bit_and:cs @0 (bit_not @1)) (bit_not @0))
1423 (bit_not (bit_and @0 @1)))
1425 /* (~a & b) ^ a --> (a | b) */
1427 (bit_xor:c (bit_and:cs (bit_not @0) @1) @0)
1430 /* (a | b) & ~(a ^ b) --> a & b */
1432 (bit_and:c (bit_ior @0 @1) (bit_not (bit_xor:c @0 @1)))
1435 /* (a | b) & (a == b) --> a & b (boolean version of the above). */
1437 (bit_and:c (bit_ior @0 @1) (nop_convert? (eq:c @0 @1)))
1438 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1439 && TYPE_PRECISION (TREE_TYPE (@0)) == 1)
1442 /* a | ~(a ^ b) --> a | ~b */
1444 (bit_ior:c @0 (bit_not:s (bit_xor:c @0 @1)))
1445 (bit_ior @0 (bit_not @1)))
1447 /* a | (a == b) --> a | (b^1) (boolean version of the above). */
1449 (bit_ior:c @0 (nop_convert? (eq:c @0 @1)))
1450 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1451 && TYPE_PRECISION (TREE_TYPE (@0)) == 1)
1452 (bit_ior @0 (bit_xor @1 { build_one_cst (type); }))))
1454 /* a | ((~a) ^ b) --> a | (~b) (alt version of the above 2) */
1456 (bit_ior:c @0 (bit_xor:cs @1 @2))
1457 (with { bool wascmp; }
1458 (if (bitwise_inverted_equal_p (@0, @1, wascmp)
1459 && (!wascmp || element_precision (type) == 1))
1460 (bit_ior @0 (bit_not @2)))))
1462 /* a & ~(a ^ b) --> a & b */
1464 (bit_and:c @0 (bit_not (bit_xor:c @0 @1)))
1467 /* a & (a == b) --> a & b (boolean version of the above). */
1469 (bit_and:c @0 (nop_convert? (eq:c @0 @1)))
1470 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1471 && TYPE_PRECISION (TREE_TYPE (@0)) == 1)
1474 /* a & ((~a) ^ b) --> a & b (alt version of the above 2) */
1476 (bit_and:c @0 (bit_xor:c @1 @2))
1477 (with { bool wascmp; }
1478 (if (bitwise_inverted_equal_p (@0, @1, wascmp)
1479 && (!wascmp || element_precision (type) == 1))
1482 /* (a | b) | (a &^ b) --> a | b */
1483 (for op (bit_and bit_xor)
1485 (bit_ior:c (bit_ior@2 @0 @1) (op:c @0 @1))
1488 /* (a & b) | ~(a ^ b) --> ~(a ^ b) */
1490 (bit_ior:c (bit_and:c @0 @1) (bit_not@2 (bit_xor @0 @1)))
1493 /* (a & b) | (a == b) --> a == b */
1495 (bit_ior:c (bit_and:c @0 @1) (nop_convert?@2 (eq @0 @1)))
1496 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1497 && TYPE_PRECISION (TREE_TYPE (@0)) == 1)
1500 /* ~(~a & b) --> a | ~b */
1502 (bit_not (bit_and:cs (bit_not @0) @1))
1503 (bit_ior @0 (bit_not @1)))
1505 /* ~(~a | b) --> a & ~b */
1507 (bit_not (bit_ior:cs (bit_not @0) @1))
1508 (bit_and @0 (bit_not @1)))
1510 /* (a ^ b) & ((b ^ c) ^ a) --> (a ^ b) & ~c */
1512 (bit_and:c (bit_xor:c@3 @0 @1) (bit_xor:cs (bit_xor:cs @1 @2) @0))
1513 (bit_and @3 (bit_not @2)))
1515 /* (a ^ b) | ((b ^ c) ^ a) --> (a ^ b) | c */
1517 (bit_ior:c (bit_xor:c@3 @0 @1) (bit_xor:c (bit_xor:c @1 @2) @0))
1520 /* (~X | C) ^ D -> (X | C) ^ (~D ^ C) if (~D ^ C) can be simplified. */
1522 (bit_xor:c (bit_ior:cs (bit_not:s @0) @1) @2)
1523 (bit_xor (bit_ior @0 @1) (bit_xor! (bit_not! @2) @1)))
1525 /* (~X & C) ^ D -> (X & C) ^ (D ^ C) if (D ^ C) can be simplified. */
1527 (bit_xor:c (bit_and:cs (bit_not:s @0) @1) @2)
1528 (bit_xor (bit_and @0 @1) (bit_xor! @2 @1)))
1530 /* Simplify (~X & Y) to X ^ Y if we know that (X & ~Y) is 0. */
1532 (bit_and (bit_not SSA_NAME@0) INTEGER_CST@1)
1533 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1534 && wi::bit_and_not (get_nonzero_bits (@0), wi::to_wide (@1)) == 0)
1537 /* For constants M and N, if M == (1LL << cst) - 1 && (N & M) == M,
1538 ((A & N) + B) & M -> (A + B) & M
1539 Similarly if (N & M) == 0,
1540 ((A | N) + B) & M -> (A + B) & M
1541 and for - instead of + (or unary - instead of +)
1542 and/or ^ instead of |.
1543 If B is constant and (B & M) == 0, fold into A & M. */
1544 (for op (plus minus)
1545 (for bitop (bit_and bit_ior bit_xor)
1547 (bit_and (op:s (bitop:s@0 @3 INTEGER_CST@4) @1) INTEGER_CST@2)
1550 tree utype = fold_bit_and_mask (TREE_TYPE (@0), @2, op, @0, bitop,
1551 @3, @4, @1, ERROR_MARK, NULL_TREE,
1554 (convert (bit_and (op (convert:utype { pmop[0]; })
1555 (convert:utype { pmop[1]; }))
1556 (convert:utype @2))))))
1558 (bit_and (op:s @0 (bitop:s@1 @3 INTEGER_CST@4)) INTEGER_CST@2)
1561 tree utype = fold_bit_and_mask (TREE_TYPE (@0), @2, op, @0, ERROR_MARK,
1562 NULL_TREE, NULL_TREE, @1, bitop, @3,
1565 (convert (bit_and (op (convert:utype { pmop[0]; })
1566 (convert:utype { pmop[1]; }))
1567 (convert:utype @2)))))))
1569 (bit_and (op:s @0 @1) INTEGER_CST@2)
1572 tree utype = fold_bit_and_mask (TREE_TYPE (@0), @2, op, @0, ERROR_MARK,
1573 NULL_TREE, NULL_TREE, @1, ERROR_MARK,
1574 NULL_TREE, NULL_TREE, pmop); }
1576 (convert (bit_and (op (convert:utype { pmop[0]; })
1577 (convert:utype { pmop[1]; }))
1578 (convert:utype @2)))))))
1579 (for bitop (bit_and bit_ior bit_xor)
1581 (bit_and (negate:s (bitop:s@0 @2 INTEGER_CST@3)) INTEGER_CST@1)
1584 tree utype = fold_bit_and_mask (TREE_TYPE (@0), @1, NEGATE_EXPR, @0,
1585 bitop, @2, @3, NULL_TREE, ERROR_MARK,
1586 NULL_TREE, NULL_TREE, pmop); }
1588 (convert (bit_and (negate (convert:utype { pmop[0]; }))
1589 (convert:utype @1)))))))
1591 /* X % Y is smaller than Y. */
1594 (cmp:c (trunc_mod @0 @1) @1)
1595 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
1596 { constant_boolean_node (cmp == LT_EXPR, type); })))
1600 (bit_ior @0 integer_all_onesp@1)
1605 (bit_ior @0 integer_zerop)
1610 (bit_and @0 integer_zerop@1)
1615 (for op (bit_ior bit_xor)
1617 (op:c (convert? @0) (convert? (maybe_bit_not @1)))
1618 (with { bool wascmp; }
1619 (if (types_match (TREE_TYPE (@0), TREE_TYPE (@1))
1620 && bitwise_inverted_equal_p (@0, @1, wascmp))
1623 ? constant_boolean_node (true, type)
1624 : build_all_ones_cst (TREE_TYPE (@0)); })))))
1629 { build_zero_cst (type); })
1631 /* Canonicalize X ^ ~0 to ~X. */
1633 (bit_xor @0 integer_all_onesp@1)
1638 (bit_and @0 integer_all_onesp)
1641 /* x & x -> x, x | x -> x */
1642 (for bitop (bit_and bit_ior)
1647 /* x & C -> x if we know that x & ~C == 0. */
1650 (bit_and SSA_NAME@0 INTEGER_CST@1)
1651 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1652 && wi::bit_and_not (get_nonzero_bits (@0), wi::to_wide (@1)) == 0)
1655 /* `a & (x | CST)` -> a if we know that (a & ~CST) == 0 */
1657 (bit_and:c SSA_NAME@0 (bit_ior @1 INTEGER_CST@2))
1658 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1659 && wi::bit_and_not (get_nonzero_bits (@0), wi::to_wide (@2)) == 0)
1662 /* x | C -> C if we know that x & ~C == 0. */
1664 (bit_ior SSA_NAME@0 INTEGER_CST@1)
1665 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1666 && wi::bit_and_not (get_nonzero_bits (@0), wi::to_wide (@1)) == 0)
1670 /* ~(~X - Y) -> X + Y and ~(~X + Y) -> X - Y. */
1672 (bit_not (minus (bit_not @0) @1))
1675 (bit_not (plus:c (bit_not @0) @1))
1677 /* (~X - ~Y) -> Y - X. */
1679 (minus (bit_not @0) (bit_not @1))
1680 (if (!TYPE_OVERFLOW_SANITIZED (type))
1681 (with { tree utype = unsigned_type_for (type); }
1682 (convert (minus (convert:utype @1) (convert:utype @0))))))
1684 /* ~(X - Y) -> ~X + Y. */
1686 (bit_not (minus:s @0 @1))
1687 (plus (bit_not @0) @1))
1689 (bit_not (plus:s @0 INTEGER_CST@1))
1690 (if ((INTEGRAL_TYPE_P (type)
1691 && TYPE_UNSIGNED (type))
1692 || (!TYPE_OVERFLOW_SANITIZED (type)
1693 && may_negate_without_overflow_p (@1)))
1694 (plus (bit_not @0) { const_unop (NEGATE_EXPR, type, @1); })))
1697 /* ~X + Y -> (Y - X) - 1. */
1699 (plus:c (bit_not @0) @1)
1700 (if (ANY_INTEGRAL_TYPE_P (type)
1701 && TYPE_OVERFLOW_WRAPS (type)
1702 /* -1 - X is folded to ~X, so we'd recurse endlessly. */
1703 && !integer_all_onesp (@1))
1704 (plus (minus @1 @0) { build_minus_one_cst (type); })
1705 (if (INTEGRAL_TYPE_P (type)
1706 && TREE_CODE (@1) == INTEGER_CST
1707 && wi::to_wide (@1) != wi::min_value (TYPE_PRECISION (type),
1709 (minus (plus @1 { build_minus_one_cst (type); }) @0))))
1712 /* ~(X >> Y) -> ~X >> Y if ~X can be simplified. */
1714 (bit_not (rshift:s @0 @1))
1715 (if (!TYPE_UNSIGNED (TREE_TYPE (@0)))
1716 (rshift (bit_not! @0) @1)
1717 /* For logical right shifts, this is possible only if @0 doesn't
1718 have MSB set and the logical right shift is changed into
1719 arithmetic shift. */
1720 (if (INTEGRAL_TYPE_P (type)
1721 && !wi::neg_p (tree_nonzero_bits (@0)))
1722 (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
1723 (convert (rshift (bit_not! (convert:stype @0)) @1))))))
1725 /* x + (x & 1) -> (x + 1) & ~1 */
1727 (plus:c @0 (bit_and:s @0 integer_onep@1))
1728 (bit_and (plus @0 @1) (bit_not @1)))
1730 /* x & ~(x & y) -> x & ~y */
1731 /* x | ~(x | y) -> x | ~y */
1732 (for bitop (bit_and bit_ior)
1734 (bitop:c @0 (bit_not (bitop:cs @0 @1)))
1735 (bitop @0 (bit_not @1))))
1737 /* (~x & y) | ~(x | y) -> ~x */
1739 (bit_ior:c (bit_and:c (bit_not@2 @0) @1) (bit_not (bit_ior:c @0 @1)))
1742 /* (x | y) ^ (x | ~y) -> ~x */
1744 (bit_xor:c (bit_ior:c @0 @1) (bit_ior:c @0 (bit_not @1)))
1747 /* (x & y) | ~(x | y) -> ~(x ^ y) */
1749 (bit_ior:c (bit_and:s @0 @1) (bit_not:s (bit_ior:s @0 @1)))
1750 (bit_not (bit_xor @0 @1)))
1752 /* (~x | y) ^ (x ^ y) -> x | ~y */
1754 (bit_xor:c (bit_ior:cs (bit_not @0) @1) (bit_xor:s @0 @1))
1755 (bit_ior @0 (bit_not @1)))
1757 /* (x ^ y) | ~(x | y) -> ~(x & y) */
1759 (bit_ior:c (bit_xor:s @0 @1) (bit_not:s (bit_ior:s @0 @1)))
1760 (bit_not (bit_and @0 @1)))
1762 /* (x & y) ^ (x | y) -> x ^ y */
1764 (bit_xor:c (bit_and @0 @1) (bit_ior @0 @1))
1767 /* (x ^ y) ^ (x | y) -> x & y */
1769 (bit_xor:c (bit_xor @0 @1) (bit_ior @0 @1))
1772 /* (x & y) + (x ^ y) -> x | y */
1773 /* (x & y) | (x ^ y) -> x | y */
1774 /* (x & y) ^ (x ^ y) -> x | y */
1775 (for op (plus bit_ior bit_xor)
1777 (op:c (bit_and @0 @1) (bit_xor @0 @1))
1780 /* (x & y) + (x | y) -> x + y */
1782 (plus:c (bit_and @0 @1) (bit_ior @0 @1))
1785 /* (x + y) - (x | y) -> x & y */
1787 (minus (plus @0 @1) (bit_ior @0 @1))
1788 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1789 && !TYPE_SATURATING (type))
1792 /* (x + y) - (x & y) -> x | y */
1794 (minus (plus @0 @1) (bit_and @0 @1))
1795 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1796 && !TYPE_SATURATING (type))
1799 /* (x | y) - y -> (x & ~y) */
1801 (minus (bit_ior:cs @0 @1) @1)
1802 (bit_and @0 (bit_not @1)))
1804 /* (x | y) - (x ^ y) -> x & y */
1806 (minus (bit_ior @0 @1) (bit_xor @0 @1))
1809 /* (x | y) - (x & y) -> x ^ y */
1811 (minus (bit_ior @0 @1) (bit_and @0 @1))
1814 /* (x | y) & ~(x & y) -> x ^ y */
1816 (bit_and:c (bit_ior @0 @1) (bit_not (bit_and @0 @1)))
1819 /* (x | y) & (~x ^ y) -> x & y */
1821 (bit_and:c (bit_ior:c @0 @1) (bit_xor:c @1 @2))
1822 (with { bool wascmp; }
1823 (if (bitwise_inverted_equal_p (@0, @2, wascmp)
1824 && (!wascmp || element_precision (type) == 1))
1827 /* (~x | y) & (x | ~y) -> ~(x ^ y) */
1829 (bit_and (bit_ior:cs (bit_not @0) @1) (bit_ior:cs @0 (bit_not @1)))
1830 (bit_not (bit_xor @0 @1)))
1832 /* (~x | y) ^ (x | ~y) -> x ^ y */
1834 (bit_xor (bit_ior:c (bit_not @0) @1) (bit_ior:c @0 (bit_not @1)))
1837 /* ((x & y) - (x | y)) - 1 -> ~(x ^ y) */
1839 (plus (nop_convert1? (minus@2 (nop_convert2? (bit_and:c @0 @1))
1840 (nop_convert2? (bit_ior @0 @1))))
1842 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1843 && !TYPE_SATURATING (type) && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2))
1844 && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@2))
1845 && !TYPE_SATURATING (TREE_TYPE (@2)))
1846 (bit_not (convert (bit_xor @0 @1)))))
1848 (minus (nop_convert1? (plus@2 (nop_convert2? (bit_and:c @0 @1))
1850 (nop_convert3? (bit_ior @0 @1)))
1851 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1852 && !TYPE_SATURATING (type) && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2))
1853 && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@2))
1854 && !TYPE_SATURATING (TREE_TYPE (@2)))
1855 (bit_not (convert (bit_xor @0 @1)))))
1857 (minus (nop_convert1? (bit_and @0 @1))
1858 (nop_convert2? (plus@2 (nop_convert3? (bit_ior:c @0 @1))
1860 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1861 && !TYPE_SATURATING (type) && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2))
1862 && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@2))
1863 && !TYPE_SATURATING (TREE_TYPE (@2)))
1864 (bit_not (convert (bit_xor @0 @1)))))
1866 /* ~x & ~y -> ~(x | y)
1867 ~x | ~y -> ~(x & y) */
1868 (for op (bit_and bit_ior)
1869 rop (bit_ior bit_and)
1871 (op (convert1? (bit_not @0)) (convert2? (bit_not @1)))
1872 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1873 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
1874 (bit_not (rop (convert @0) (convert @1))))))
1876 /* If we are XORing or adding two BIT_AND_EXPR's, both of which are and'ing
1877 with a constant, and the two constants have no bits in common,
1878 we should treat this as a BIT_IOR_EXPR since this may produce more
1880 (for op (bit_xor plus)
1882 (op (convert1? (bit_and@4 @0 INTEGER_CST@1))
1883 (convert2? (bit_and@5 @2 INTEGER_CST@3)))
1884 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
1885 && tree_nop_conversion_p (type, TREE_TYPE (@2))
1886 && (wi::to_wide (@1) & wi::to_wide (@3)) == 0)
1887 (bit_ior (convert @4) (convert @5)))))
1889 /* (X | Y) ^ X -> Y & ~ X*/
1891 (bit_xor:c (convert1? (bit_ior:c @@0 @1)) (convert2? @0))
1892 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1893 (convert (bit_and @1 (bit_not @0)))))
1895 /* (~X | Y) ^ X -> ~(X & Y). */
1897 (bit_xor:c (nop_convert1? (bit_ior:c (nop_convert2? (bit_not @0)) @1)) @2)
1898 (if (bitwise_equal_p (@0, @2))
1899 (convert (bit_not (bit_and @0 (convert @1))))))
1901 /* Convert ~X ^ ~Y to X ^ Y. */
1903 (bit_xor (convert1? (bit_not @0)) (convert2? (bit_not @1)))
1904 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1905 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
1906 (bit_xor (convert @0) (convert @1))))
1908 /* Convert ~X ^ C to X ^ ~C. */
1910 (bit_xor (convert? (bit_not @0)) INTEGER_CST@1)
1911 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1912 (bit_xor (convert @0) (bit_not @1))))
1914 /* Fold (X & Y) ^ Y and (X ^ Y) & Y as ~X & Y. */
1915 (for opo (bit_and bit_xor)
1916 opi (bit_xor bit_and)
1918 (opo:c (opi:cs @0 @1) @1)
1919 (bit_and (bit_not @0) @1)))
1921 /* Given a bit-wise operation CODE applied to ARG0 and ARG1, see if both
1922 operands are another bit-wise operation with a common input. If so,
1923 distribute the bit operations to save an operation and possibly two if
1924 constants are involved. For example, convert
1925 (A | B) & (A | C) into A | (B & C)
1926 Further simplification will occur if B and C are constants. */
1927 (for op (bit_and bit_ior bit_xor)
1928 rop (bit_ior bit_and bit_and)
1930 (op (convert? (rop:c @@0 @1)) (convert? (rop:c @0 @2)))
1931 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1932 && tree_nop_conversion_p (type, TREE_TYPE (@2)))
1933 (rop (convert @0) (op (convert @1) (convert @2))))))
1935 /* Some simple reassociation for bit operations, also handled in reassoc. */
1936 /* (X & Y) & Y -> X & Y
1937 (X | Y) | Y -> X | Y */
1938 (for op (bit_and bit_ior)
1940 (op:c (convert1?@2 (op:c @0 @@1)) (convert2? @1))
1942 /* (X ^ Y) ^ Y -> X */
1944 (bit_xor:c (convert1? (bit_xor:c @0 @@1)) (convert2? @1))
1947 /* (X & ~Y) & Y -> 0 */
1949 (bit_and:c (bit_and @0 @1) @2)
1950 (with { bool wascmp; }
1951 (if (bitwise_inverted_equal_p (@0, @2, wascmp)
1952 || bitwise_inverted_equal_p (@1, @2, wascmp))
1953 { wascmp ? constant_boolean_node (false, type) : build_zero_cst (type); })))
1954 /* (X | ~Y) | Y -> -1 */
1956 (bit_ior:c (bit_ior @0 @1) @2)
1957 (with { bool wascmp; }
1958 (if ((bitwise_inverted_equal_p (@0, @2, wascmp)
1959 || bitwise_inverted_equal_p (@1, @2, wascmp))
1960 && (!wascmp || element_precision (type) == 1))
1961 { build_all_ones_cst (TREE_TYPE (@0)); })))
1963 /* (X & Y) & (X & Z) -> (X & Y) & Z
1964 (X | Y) | (X | Z) -> (X | Y) | Z */
1965 (for op (bit_and bit_ior)
1967 (op (convert1?@3 (op:c@4 @0 @1)) (convert2?@5 (op:c@6 @0 @2)))
1968 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1969 && tree_nop_conversion_p (type, TREE_TYPE (@2)))
1970 (if (single_use (@5) && single_use (@6))
1971 (op @3 (convert @2))
1972 (if (single_use (@3) && single_use (@4))
1973 (op (convert @1) @5))))))
1974 /* (X ^ Y) ^ (X ^ Z) -> Y ^ Z */
1976 (bit_xor (convert1? (bit_xor:c @0 @1)) (convert2? (bit_xor:c @0 @2)))
1977 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1978 && tree_nop_conversion_p (type, TREE_TYPE (@2)))
1979 (bit_xor (convert @1) (convert @2))))
1981 /* Convert abs (abs (X)) into abs (X).
1982 also absu (absu (X)) into absu (X). */
1988 (absu (convert@2 (absu@1 @0)))
1989 (if (tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@1)))
1992 /* Convert abs[u] (-X) -> abs[u] (X). */
2001 /* Convert abs[u] (X) where X is nonnegative -> (X). */
2003 (abs tree_expr_nonnegative_p@0)
2007 (absu tree_expr_nonnegative_p@0)
2010 /* Simplify (-(X < 0) | 1) * X into abs (X) or absu(X). */
2012 (mult:c (nop_convert1?
2013 (bit_ior (nop_convert2? (negate (convert? (lt @0 integer_zerop))))
2016 (if (INTEGRAL_TYPE_P (type)
2017 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
2018 && !TYPE_UNSIGNED (TREE_TYPE (@0)))
2019 (if (TYPE_UNSIGNED (type))
2026 /* A few cases of fold-const.cc negate_expr_p predicate. */
2027 (match negate_expr_p
2029 (if ((INTEGRAL_TYPE_P (type)
2030 && TYPE_UNSIGNED (type))
2031 || (!TYPE_OVERFLOW_SANITIZED (type)
2032 && may_negate_without_overflow_p (t)))))
2033 (match negate_expr_p
2035 (match negate_expr_p
2037 (if (!TYPE_OVERFLOW_SANITIZED (type))))
2038 (match negate_expr_p
2040 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (t)))))
2041 /* VECTOR_CST handling of non-wrapping types would recurse in unsupported
2043 (match negate_expr_p
2045 (if (FLOAT_TYPE_P (TREE_TYPE (type)) || TYPE_OVERFLOW_WRAPS (type))))
2046 (match negate_expr_p
2048 (if ((ANY_INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_WRAPS (type))
2049 || (FLOAT_TYPE_P (type)
2050 && !HONOR_SIGN_DEPENDENT_ROUNDING (type)
2051 && !HONOR_SIGNED_ZEROS (type)))))
2053 /* (-A) * (-B) -> A * B */
2055 (mult:c (convert1? (negate @0)) (convert2? negate_expr_p@1))
2056 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
2057 && tree_nop_conversion_p (type, TREE_TYPE (@1)))
2058 (mult (convert @0) (convert (negate @1)))))
2060 /* -(A + B) -> (-B) - A. */
2062 (negate (plus:c @0 negate_expr_p@1))
2063 (if (!HONOR_SIGN_DEPENDENT_ROUNDING (type)
2064 && !HONOR_SIGNED_ZEROS (type))
2065 (minus (negate @1) @0)))
2067 /* -(A - B) -> B - A. */
2069 (negate (minus @0 @1))
2070 (if ((ANY_INTEGRAL_TYPE_P (type) && !TYPE_OVERFLOW_SANITIZED (type))
2071 || (FLOAT_TYPE_P (type)
2072 && !HONOR_SIGN_DEPENDENT_ROUNDING (type)
2073 && !HONOR_SIGNED_ZEROS (type)))
2076 (negate (pointer_diff @0 @1))
2077 (if (TYPE_OVERFLOW_UNDEFINED (type))
2078 (pointer_diff @1 @0)))
2080 /* A - B -> A + (-B) if B is easily negatable. */
2082 (minus @0 negate_expr_p@1)
2083 (if (!FIXED_POINT_TYPE_P (type))
2084 (plus @0 (negate @1))))
2086 /* 1 - a is a ^ 1 if a had a bool range. */
2087 /* This is only enabled for gimple as sometimes
2088 cfun is not set for the function which contains
2089 the SSA_NAME (e.g. while IPA passes are happening,
2090 fold might be called). */
2092 (minus integer_onep@0 SSA_NAME@1)
2093 (if (INTEGRAL_TYPE_P (type)
2094 && ssa_name_has_boolean_range (@1))
2097 /* Other simplifications of negation (c.f. fold_negate_expr_1). */
2099 (negate (mult:c@0 @1 negate_expr_p@2))
2100 (if (! TYPE_UNSIGNED (type)
2101 && ! HONOR_SIGN_DEPENDENT_ROUNDING (type)
2103 (mult @1 (negate @2))))
2106 (negate (rdiv@0 @1 negate_expr_p@2))
2107 (if (! HONOR_SIGN_DEPENDENT_ROUNDING (type)
2109 (rdiv @1 (negate @2))))
2112 (negate (rdiv@0 negate_expr_p@1 @2))
2113 (if (! HONOR_SIGN_DEPENDENT_ROUNDING (type)
2115 (rdiv (negate @1) @2)))
2117 /* Fold -((int)x >> (prec - 1)) into (unsigned)x >> (prec - 1). */
2119 (negate (convert? (rshift @0 INTEGER_CST@1)))
2120 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
2121 && wi::to_wide (@1) == element_precision (type) - 1)
2122 (with { tree stype = TREE_TYPE (@0);
2123 tree ntype = TYPE_UNSIGNED (stype) ? signed_type_for (stype)
2124 : unsigned_type_for (stype); }
2125 (if (VECTOR_TYPE_P (type))
2126 (view_convert (rshift (view_convert:ntype @0) @1))
2127 (convert (rshift (convert:ntype @0) @1))))))
2129 /* Try to fold (type) X op CST -> (type) (X op ((type-x) CST))
2131 For bitwise binary operations apply operand conversions to the
2132 binary operation result instead of to the operands. This allows
2133 to combine successive conversions and bitwise binary operations.
2134 We combine the above two cases by using a conditional convert. */
2135 (for bitop (bit_and bit_ior bit_xor)
2137 (bitop (convert@2 @0) (convert?@3 @1))
2138 (if (((TREE_CODE (@1) == INTEGER_CST
2139 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
2140 && (int_fits_type_p (@1, TREE_TYPE (@0))
2141 || tree_nop_conversion_p (TREE_TYPE (@0), type)))
2142 || types_match (@0, @1))
2143 && !POINTER_TYPE_P (TREE_TYPE (@0))
2144 && !VECTOR_TYPE_P (TREE_TYPE (@0))
2145 && TREE_CODE (TREE_TYPE (@0)) != OFFSET_TYPE
2146 /* ??? This transform conflicts with fold-const.cc doing
2147 Convert (T)(x & c) into (T)x & (T)c, if c is an integer
2148 constants (if x has signed type, the sign bit cannot be set
2149 in c). This folds extension into the BIT_AND_EXPR.
2150 Restrict it to GIMPLE to avoid endless recursions. */
2151 && (bitop != BIT_AND_EXPR || GIMPLE)
2152 && (/* That's a good idea if the conversion widens the operand, thus
2153 after hoisting the conversion the operation will be narrower.
2154 It is also a good if the conversion is a nop as moves the
2155 conversion to one side; allowing for combining of the conversions. */
2156 TYPE_PRECISION (TREE_TYPE (@0)) < TYPE_PRECISION (type)
2157 /* The conversion check for being a nop can only be done at the gimple
2158 level as fold_binary has some re-association code which can conflict
2159 with this if there is a "constant" which is not a full INTEGER_CST. */
2160 || (GIMPLE && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (type))
2161 /* It's also a good idea if the conversion is to a non-integer
2163 || GET_MODE_CLASS (TYPE_MODE (type)) != MODE_INT
2164 /* Or if the precision of TO is not the same as the precision
2166 || !type_has_mode_precision_p (type)
2167 /* In GIMPLE, getting rid of 2 conversions for one new results
2170 && TREE_CODE (@1) != INTEGER_CST
2171 && tree_nop_conversion_p (type, TREE_TYPE (@0))
2173 && single_use (@3))))
2174 (convert (bitop @0 (convert @1)))))
2175 /* In GIMPLE, getting rid of 2 conversions for one new results
2178 (convert (bitop:cs@2 (nop_convert:s @0) @1))
2180 && TREE_CODE (@1) != INTEGER_CST
2181 && tree_nop_conversion_p (type, TREE_TYPE (@2))
2182 && types_match (type, @0)
2183 && !POINTER_TYPE_P (TREE_TYPE (@0))
2184 && TREE_CODE (TREE_TYPE (@0)) != OFFSET_TYPE)
2185 (bitop @0 (convert @1)))))
2187 (for bitop (bit_and bit_ior)
2188 rbitop (bit_ior bit_and)
2189 /* (x | y) & x -> x */
2190 /* (x & y) | x -> x */
2192 (bitop:c (rbitop:c @0 @1) @0)
2194 /* (~x | y) & x -> x & y */
2195 /* (~x & y) | x -> x | y */
2197 (bitop:c (rbitop:c @2 @1) @0)
2198 (with { bool wascmp; }
2199 (if (bitwise_inverted_equal_p (@0, @2, wascmp)
2200 && (!wascmp || element_precision (type) == 1))
2202 /* (x | y) & (x & z) -> (x & z) */
2203 /* (x & y) | (x | z) -> (x | z) */
2205 (bitop:c (rbitop:c @0 @1) (bitop:c@3 @0 @2))
2207 /* (x | c) & ~(y | c) -> x & ~(y | c) */
2208 /* (x & c) | ~(y & c) -> x | ~(y & c) */
2210 (bitop:c (rbitop:c @0 @1) (bit_not@3 (rbitop:c @1 @2)))
2212 /* x & ~(y | x) -> 0 */
2213 /* x | ~(y & x) -> -1 */
2215 (bitop:c @0 (bit_not (rbitop:c @0 @1)))
2216 (if (bitop == BIT_AND_EXPR)
2217 { build_zero_cst (type); }
2218 { build_minus_one_cst (type); })))
2220 /* ((x | y) & z) | x -> (z & y) | x
2221 ((x ^ y) & z) | x -> (z & y) | x */
2222 (for op (bit_ior bit_xor)
2224 (bit_ior:c (nop_convert1?:s
2225 (bit_and:cs (nop_convert2?:s (op:cs @0 @1)) @2)) @3)
2226 (if (bitwise_equal_p (@0, @3))
2227 (convert (bit_ior (bit_and @1 (convert @2)) (convert @0))))))
2229 /* (x | CST1) & CST2 -> (x & CST2) | (CST1 & CST2) */
2231 (bit_and (bit_ior @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
2232 (bit_ior (bit_and @0 @2) (bit_and! @1 @2)))
2234 /* Combine successive equal operations with constants. */
2235 (for bitop (bit_and bit_ior bit_xor)
2237 (bitop (bitop @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
2238 (if (!CONSTANT_CLASS_P (@0))
2239 /* This is the canonical form regardless of whether (bitop @1 @2) can be
2240 folded to a constant. */
2241 (bitop @0 (bitop! @1 @2))
2242 /* In this case we have three constants and (bitop @0 @1) doesn't fold
2243 to a constant. This can happen if @0 or @1 is a POLY_INT_CST and if
2244 the values involved are such that the operation can't be decided at
2245 compile time. Try folding one of @0 or @1 with @2 to see whether
2246 that combination can be decided at compile time.
2248 Keep the existing form if both folds fail, to avoid endless
2250 (with { tree cst1 = const_binop (bitop, type, @0, @2); }
2252 (bitop @1 { cst1; })
2253 (with { tree cst2 = const_binop (bitop, type, @1, @2); }
2255 (bitop @0 { cst2; }))))))))
2257 /* Try simple folding for X op !X, and X op X with the help
2258 of the truth_valued_p and logical_inverted_value predicates. */
2259 (match truth_valued_p
2261 (if (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1)))
2262 (for op (tcc_comparison truth_and truth_andif truth_or truth_orif truth_xor)
2263 (match truth_valued_p
2265 (match truth_valued_p
2268 (match (logical_inverted_value @0)
2270 (match (logical_inverted_value @0)
2271 (bit_not truth_valued_p@0))
2272 (match (logical_inverted_value @0)
2273 (eq @0 integer_zerop))
2274 (match (logical_inverted_value @0)
2275 (ne truth_valued_p@0 integer_truep))
2276 (match (logical_inverted_value @0)
2277 (bit_xor truth_valued_p@0 integer_truep))
2281 (bit_and:c @0 (logical_inverted_value @0))
2282 { build_zero_cst (type); })
2283 /* X | !X and X ^ !X -> 1, , if X is truth-valued. */
2284 (for op (bit_ior bit_xor)
2286 (op:c truth_valued_p@0 (logical_inverted_value @0))
2287 { constant_boolean_node (true, type); }))
2288 /* X ==/!= !X is false/true. */
2291 (op:c truth_valued_p@0 (logical_inverted_value @0))
2292 { constant_boolean_node (op == NE_EXPR ? true : false, type); }))
2296 (bit_not (bit_not @0))
2299 /* zero_one_valued_p will match when a value is known to be either
2300 0 or 1 including constants 0 or 1.
2301 Signed 1-bits includes -1 so they cannot match here. */
2302 (match zero_one_valued_p
2304 (if (INTEGRAL_TYPE_P (type)
2305 && (TYPE_UNSIGNED (type)
2306 || TYPE_PRECISION (type) > 1)
2307 && wi::leu_p (tree_nonzero_bits (@0), 1))))
2308 (match zero_one_valued_p
2310 (if (INTEGRAL_TYPE_P (type)
2311 && (TYPE_UNSIGNED (type)
2312 || TYPE_PRECISION (type) > 1))))
2314 /* (a&1) is always [0,1] too. This is useful again when
2315 the range is not known. */
2316 /* Note this can't be recursive due to VN handling of equivalents,
2317 VN and would cause an infinite recursion. */
2318 (match zero_one_valued_p
2319 (bit_and:c@0 @1 integer_onep)
2320 (if (INTEGRAL_TYPE_P (type))))
2322 /* A conversion from an zero_one_valued_p is still a [0,1].
2323 This is useful when the range of a variable is not known */
2324 /* Note this matches can't be recursive because of the way VN handles
2325 nop conversions being equivalent and then recursive between them. */
2326 (match zero_one_valued_p
2328 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2329 && (TYPE_UNSIGNED (TREE_TYPE (@1))
2330 || TYPE_PRECISION (TREE_TYPE (@1)) > 1)
2331 && INTEGRAL_TYPE_P (type)
2332 && (TYPE_UNSIGNED (type)
2333 || TYPE_PRECISION (type) > 1)
2334 && wi::leu_p (tree_nonzero_bits (@1), 1))))
2336 /* Transform { 0 or 1 } * { 0 or 1 } into { 0 or 1 } & { 0 or 1 }. */
2338 (mult zero_one_valued_p@0 zero_one_valued_p@1)
2339 (if (INTEGRAL_TYPE_P (type))
2342 (for cmp (tcc_comparison)
2343 icmp (inverted_tcc_comparison)
2344 /* Fold (((a < b) & c) | ((a >= b) & d)) into (a < b ? c : d) & 1. */
2347 (bit_and:c (convert? (cmp@0 @01 @02)) @3)
2348 (bit_and:c (convert? (icmp@4 @01 @02)) @5))
2349 (if (INTEGRAL_TYPE_P (type)
2350 && invert_tree_comparison (cmp, HONOR_NANS (@01)) == icmp
2351 /* The scalar version has to be canonicalized after vectorization
2352 because it makes unconditional loads conditional ones, which
2353 means we lose vectorization because the loads may trap. */
2354 && canonicalize_math_after_vectorization_p ())
2355 (bit_and (cond @0 @3 @5) { build_one_cst (type); })))
2357 /* Fold ((-(a < b) & c) | (-(a >= b) & d)) into a < b ? c : d. This is
2358 canonicalized further and we recognize the conditional form:
2359 (a < b ? c : 0) | (a >= b ? d : 0) into a < b ? c : d. */
2362 (cond (cmp@0 @01 @02) @3 zerop)
2363 (cond (icmp@4 @01 @02) @5 zerop))
2364 (if (INTEGRAL_TYPE_P (type)
2365 && invert_tree_comparison (cmp, HONOR_NANS (@01)) == icmp
2366 /* The scalar version has to be canonicalized after vectorization
2367 because it makes unconditional loads conditional ones, which
2368 means we lose vectorization because the loads may trap. */
2369 && canonicalize_math_after_vectorization_p ())
2372 /* Vector Fold (((a < b) & c) | ((a >= b) & d)) into a < b ? c : d.
2373 and ((~(a < b) & c) | (~(a >= b) & d)) into a < b ? c : d. */
2376 (bit_and:c (vec_cond:s (cmp@0 @6 @7) @4 @5) @2)
2377 (bit_and:c (vec_cond:s (icmp@1 @6 @7) @4 @5) @3))
2378 (if (integer_zerop (@5)
2379 && invert_tree_comparison (cmp, HONOR_NANS (@6)) == icmp)
2381 (if (integer_onep (@4))
2382 (bit_and (vec_cond @0 @2 @3) @4))
2383 (if (integer_minus_onep (@4))
2384 (vec_cond @0 @2 @3)))
2385 (if (integer_zerop (@4)
2386 && invert_tree_comparison (cmp, HONOR_NANS (@6)) == icmp)
2388 (if (integer_onep (@5))
2389 (bit_and (vec_cond @0 @3 @2) @5))
2390 (if (integer_minus_onep (@5))
2391 (vec_cond @0 @3 @2))))))
2393 /* Scalar Vectorized Fold ((-(a < b) & c) | (-(a >= b) & d))
2394 into a < b ? d : c. */
2397 (vec_cond:s (cmp@0 @4 @5) @2 integer_zerop)
2398 (vec_cond:s (icmp@1 @4 @5) @3 integer_zerop))
2399 (if (invert_tree_comparison (cmp, HONOR_NANS (@4)) == icmp)
2400 (vec_cond @0 @2 @3))))
2402 /* Transform X & -Y into X * Y when Y is { 0 or 1 }. */
2404 (bit_and:c (convert? (negate zero_one_valued_p@0)) @1)
2405 (if (INTEGRAL_TYPE_P (type)
2406 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
2407 && TREE_CODE (TREE_TYPE (@0)) != BOOLEAN_TYPE
2408 /* Sign extending of the neg or a truncation of the neg
2410 && (!TYPE_UNSIGNED (TREE_TYPE (@0))
2411 || TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))))
2412 (mult (convert @0) @1)))
2414 /* Narrow integer multiplication by a zero_one_valued_p operand.
2415 Multiplication by [0,1] is guaranteed not to overflow except for
2416 1bit signed types. */
2418 (convert (mult@0 zero_one_valued_p@1 INTEGER_CST@2))
2419 (if (INTEGRAL_TYPE_P (type)
2420 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
2421 && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (@0))
2422 && (TYPE_UNSIGNED (type) || TYPE_PRECISION (type) > 1))
2423 (mult (convert @1) (convert @2))))
2425 /* (X << C) != 0 can be simplified to X, when C is zero_one_valued_p.
2426 Check that the shift is well-defined (C is less than TYPE_PRECISION)
2427 as some targets (such as x86's SSE) may return zero for larger C. */
2429 (ne (lshift zero_one_valued_p@0 INTEGER_CST@1) integer_zerop@2)
2430 (if (tree_fits_shwi_p (@1)
2431 && tree_to_shwi (@1) > 0
2432 && tree_to_shwi (@1) < TYPE_PRECISION (TREE_TYPE (@0)))
2435 /* (X << C) == 0 can be simplified to X == 0, when C is zero_one_valued_p.
2436 Check that the shift is well-defined (C is less than TYPE_PRECISION)
2437 as some targets (such as x86's SSE) may return zero for larger C. */
2439 (eq (lshift zero_one_valued_p@0 INTEGER_CST@1) integer_zerop@2)
2440 (if (tree_fits_shwi_p (@1)
2441 && tree_to_shwi (@1) > 0
2442 && tree_to_shwi (@1) < TYPE_PRECISION (TREE_TYPE (@0)))
2445 /* Convert ~ (-A) to A - 1. */
2447 (bit_not (convert? (negate @0)))
2448 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
2449 || !TYPE_UNSIGNED (TREE_TYPE (@0)))
2450 (convert (minus @0 { build_each_one_cst (TREE_TYPE (@0)); }))))
2452 /* Convert - (~A) to A + 1. */
2454 (negate (nop_convert? (bit_not @0)))
2455 (plus (view_convert @0) { build_each_one_cst (type); }))
2457 /* (a & b) ^ (a == b) -> !(a | b) */
2458 /* (a & b) == (a ^ b) -> !(a | b) */
2459 (for first_op (bit_xor eq)
2460 second_op (eq bit_xor)
2462 (first_op:c (bit_and:c truth_valued_p@0 truth_valued_p@1) (second_op:c @0 @1))
2463 (bit_not (bit_ior @0 @1))))
2465 /* Convert ~ (A - 1) or ~ (A + -1) to -A. */
2467 (bit_not (convert? (minus @0 integer_each_onep)))
2468 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
2469 || !TYPE_UNSIGNED (TREE_TYPE (@0)))
2470 (convert (negate @0))))
2472 (bit_not (convert? (plus @0 integer_all_onesp)))
2473 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
2474 || !TYPE_UNSIGNED (TREE_TYPE (@0)))
2475 (convert (negate @0))))
2477 /* Part of convert ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify. */
2479 (bit_not (convert? (bit_xor @0 INTEGER_CST@1)))
2480 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2481 (convert (bit_xor @0 (bit_not @1)))))
2483 (bit_not (convert? (bit_xor:c (bit_not @0) @1)))
2484 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2485 (convert (bit_xor @0 @1))))
2487 /* Otherwise prefer ~(X ^ Y) to ~X ^ Y as more canonical. */
2489 (bit_xor:c (nop_convert?:s (bit_not:s @0)) @1)
2490 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2491 (bit_not (bit_xor (view_convert @0) @1))))
2493 /* ~(a ^ b) is a == b for truth valued a and b. */
2495 (bit_not (bit_xor:s truth_valued_p@0 truth_valued_p@1))
2496 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2497 && TYPE_PRECISION (TREE_TYPE (@0)) == 1)
2498 (convert (eq @0 @1))))
2500 /* (~a) == b is a ^ b for truth valued a and b. */
2502 (eq:c (bit_not:s truth_valued_p@0) truth_valued_p@1)
2503 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2504 && TYPE_PRECISION (TREE_TYPE (@0)) == 1)
2505 (convert (bit_xor @0 @1))))
2507 /* (x & ~m) | (y & m) -> ((x ^ y) & m) ^ x */
2509 (bit_ior:c (bit_and:cs @0 (bit_not @2)) (bit_and:cs @1 @2))
2510 (bit_xor (bit_and (bit_xor @0 @1) @2) @0))
2512 /* Fold A - (A & B) into ~B & A. */
2514 (minus (convert1? @0) (convert2?:s (bit_and:cs @@0 @1)))
2515 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
2516 && tree_nop_conversion_p (type, TREE_TYPE (@1)))
2517 (convert (bit_and (bit_not @1) @0))))
2519 /* (m1 CMP m2) * d -> (m1 CMP m2) ? d : 0 */
2520 (if (!canonicalize_math_p ())
2521 (for cmp (tcc_comparison)
2523 (mult:c (convert (cmp@0 @1 @2)) @3)
2524 (if (INTEGRAL_TYPE_P (type)
2525 && INTEGRAL_TYPE_P (TREE_TYPE (@0)))
2526 (cond @0 @3 { build_zero_cst (type); })))
2527 /* (-(m1 CMP m2)) & d -> (m1 CMP m2) ? d : 0 */
2529 (bit_and:c (negate (convert (cmp@0 @1 @2))) @3)
2530 (if (INTEGRAL_TYPE_P (type)
2531 && INTEGRAL_TYPE_P (TREE_TYPE (@0)))
2532 (cond @0 @3 { build_zero_cst (type); })))
2536 /* For integral types with undefined overflow and C != 0 fold
2537 x * C EQ/NE y * C into x EQ/NE y. */
2540 (cmp (mult:c @0 @1) (mult:c @2 @1))
2541 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2542 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2543 && tree_expr_nonzero_p (@1))
2546 /* For integral types with wrapping overflow and C odd fold
2547 x * C EQ/NE y * C into x EQ/NE y. */
2550 (cmp (mult @0 INTEGER_CST@1) (mult @2 @1))
2551 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2552 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))
2553 && (TREE_INT_CST_LOW (@1) & 1) != 0)
2556 /* For integral types with undefined overflow and C != 0 fold
2557 x * C RELOP y * C into:
2559 x RELOP y for nonnegative C
2560 y RELOP x for negative C */
2561 (for cmp (lt gt le ge)
2563 (cmp (mult:c @0 @1) (mult:c @2 @1))
2564 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2565 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
2566 (if (tree_expr_nonnegative_p (@1) && tree_expr_nonzero_p (@1))
2568 (if (TREE_CODE (@1) == INTEGER_CST
2569 && wi::neg_p (wi::to_wide (@1), TYPE_SIGN (TREE_TYPE (@1))))
2572 /* (X - 1U) <= INT_MAX-1U into (int) X > 0. */
2576 (cmp (plus @0 integer_minus_onep@1) INTEGER_CST@2)
2577 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2578 && TYPE_UNSIGNED (TREE_TYPE (@0))
2579 && TYPE_PRECISION (TREE_TYPE (@0)) > 1
2580 && (wi::to_wide (@2)
2581 == wi::max_value (TYPE_PRECISION (TREE_TYPE (@0)), SIGNED) - 1))
2582 (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
2583 (icmp (convert:stype @0) { build_int_cst (stype, 0); })))))
2585 /* X / 4 < Y / 4 iff X < Y when the division is known to be exact. */
2586 (for cmp (simple_comparison)
2588 (cmp (convert?@3 (exact_div @0 INTEGER_CST@2)) (convert? (exact_div @1 @2)))
2589 (if (element_precision (@3) >= element_precision (@0)
2590 && types_match (@0, @1))
2591 (if (wi::lt_p (wi::to_wide (@2), 0, TYPE_SIGN (TREE_TYPE (@2))))
2592 (if (!TYPE_UNSIGNED (TREE_TYPE (@3)))
2594 (if (tree_expr_nonzero_p (@0) && tree_expr_nonzero_p (@1))
2597 tree utype = unsigned_type_for (TREE_TYPE (@0));
2599 (cmp (convert:utype @1) (convert:utype @0)))))
2600 (if (wi::gt_p (wi::to_wide (@2), 1, TYPE_SIGN (TREE_TYPE (@2))))
2601 (if (TYPE_UNSIGNED (TREE_TYPE (@0)) || !TYPE_UNSIGNED (TREE_TYPE (@3)))
2605 tree utype = unsigned_type_for (TREE_TYPE (@0));
2607 (cmp (convert:utype @0) (convert:utype @1)))))))))
2609 /* X / C1 op C2 into a simple range test. */
2610 (for cmp (simple_comparison)
2612 (cmp (trunc_div:s @0 INTEGER_CST@1) INTEGER_CST@2)
2613 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2614 && integer_nonzerop (@1)
2615 && !TREE_OVERFLOW (@1)
2616 && !TREE_OVERFLOW (@2))
2617 (with { tree lo, hi; bool neg_overflow;
2618 enum tree_code code = fold_div_compare (cmp, @1, @2, &lo, &hi,
2621 (if (code == LT_EXPR || code == GE_EXPR)
2622 (if (TREE_OVERFLOW (lo))
2623 { build_int_cst (type, (code == LT_EXPR) ^ neg_overflow); }
2624 (if (code == LT_EXPR)
2627 (if (code == LE_EXPR || code == GT_EXPR)
2628 (if (TREE_OVERFLOW (hi))
2629 { build_int_cst (type, (code == LE_EXPR) ^ neg_overflow); }
2630 (if (code == LE_EXPR)
2634 { build_int_cst (type, code == NE_EXPR); })
2635 (if (code == EQ_EXPR && !hi)
2637 (if (code == EQ_EXPR && !lo)
2639 (if (code == NE_EXPR && !hi)
2641 (if (code == NE_EXPR && !lo)
2644 { build_range_check (UNKNOWN_LOCATION, type, @0, code == EQ_EXPR,
2648 tree etype = range_check_type (TREE_TYPE (@0));
2651 hi = fold_convert (etype, hi);
2652 lo = fold_convert (etype, lo);
2653 hi = const_binop (MINUS_EXPR, etype, hi, lo);
2656 (if (etype && hi && !TREE_OVERFLOW (hi))
2657 (if (code == EQ_EXPR)
2658 (le (minus (convert:etype @0) { lo; }) { hi; })
2659 (gt (minus (convert:etype @0) { lo; }) { hi; })))))))))
2661 /* X + Z < Y + Z is the same as X < Y when there is no overflow. */
2662 (for op (lt le ge gt)
2664 (op (plus:c @0 @2) (plus:c @1 @2))
2665 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2666 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
2669 /* As a special case, X + C < Y + C is the same as (signed) X < (signed) Y
2670 when C is an unsigned integer constant with only the MSB set, and X and
2671 Y have types of equal or lower integer conversion rank than C's. */
2672 (for op (lt le ge gt)
2674 (op (plus @1 INTEGER_CST@0) (plus @2 @0))
2675 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2676 && TYPE_UNSIGNED (TREE_TYPE (@0))
2677 && wi::only_sign_bit_p (wi::to_wide (@0)))
2678 (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
2679 (op (convert:stype @1) (convert:stype @2))))))
2681 /* For equality and subtraction, this is also true with wrapping overflow. */
2682 (for op (eq ne minus)
2684 (op (plus:c @0 @2) (plus:c @1 @2))
2685 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2686 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2687 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
2689 /* And similar for pointers. */
2692 (op (pointer_plus @0 @1) (pointer_plus @0 @2))
2695 (pointer_diff (pointer_plus @0 @1) (pointer_plus @0 @2))
2696 (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
2697 (convert (minus @1 @2))))
2699 /* X - Z < Y - Z is the same as X < Y when there is no overflow. */
2700 (for op (lt le ge gt)
2702 (op (minus @0 @2) (minus @1 @2))
2703 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2704 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
2706 /* For equality and subtraction, this is also true with wrapping overflow. */
2707 (for op (eq ne minus)
2709 (op (minus @0 @2) (minus @1 @2))
2710 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2711 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2712 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
2714 /* And for pointers... */
2715 (for op (simple_comparison)
2717 (op (pointer_diff@3 @0 @2) (pointer_diff @1 @2))
2718 (if (!TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
2721 (minus (pointer_diff@3 @0 @2) (pointer_diff @1 @2))
2722 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@3))
2723 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
2724 (pointer_diff @0 @1)))
2726 /* Z - X < Z - Y is the same as Y < X when there is no overflow. */
2727 (for op (lt le ge gt)
2729 (op (minus @2 @0) (minus @2 @1))
2730 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2731 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
2733 /* For equality and subtraction, this is also true with wrapping overflow. */
2734 (for op (eq ne minus)
2736 (op (minus @2 @0) (minus @2 @1))
2737 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2738 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2739 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
2741 /* And for pointers... */
2742 (for op (simple_comparison)
2744 (op (pointer_diff@3 @2 @0) (pointer_diff @2 @1))
2745 (if (!TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
2748 (minus (pointer_diff@3 @2 @0) (pointer_diff @2 @1))
2749 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@3))
2750 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
2751 (pointer_diff @1 @0)))
2753 /* X + Y < Y is the same as X < 0 when there is no overflow. */
2754 (for op (lt le gt ge)
2756 (op:c (plus:c@2 @0 @1) @1)
2757 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2758 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2759 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0))
2760 && (CONSTANT_CLASS_P (@0) || single_use (@2)))
2761 (op @0 { build_zero_cst (TREE_TYPE (@0)); }))))
2762 /* For equality, this is also true with wrapping overflow. */
2765 (op:c (nop_convert?@3 (plus:c@2 @0 (convert1? @1))) (convert2? @1))
2766 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2767 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2768 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
2769 && (CONSTANT_CLASS_P (@0) || (single_use (@2) && single_use (@3)))
2770 && tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@2))
2771 && tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@1)))
2772 (op @0 { build_zero_cst (TREE_TYPE (@0)); })))
2774 (op:c (nop_convert?@3 (pointer_plus@2 (convert1? @0) @1)) (convert2? @0))
2775 (if (tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@0))
2776 && tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0))
2777 && (CONSTANT_CLASS_P (@1) || (single_use (@2) && single_use (@3))))
2778 (op @1 { build_zero_cst (TREE_TYPE (@1)); }))))
2780 /* (&a + b) !=/== (&a[1] + c) -> (&a[0] - &a[1]) + b !=/== c */
2783 (neeq:c ADDR_EXPR@0 (pointer_plus @2 @3))
2784 (with { poly_int64 diff; tree inner_type = TREE_TYPE (@3);}
2785 (if (ptr_difference_const (@0, @2, &diff))
2786 (neeq { build_int_cst_type (inner_type, diff); } @3))))
2788 (neeq (pointer_plus ADDR_EXPR@0 @1) (pointer_plus ADDR_EXPR@2 @3))
2789 (with { poly_int64 diff; tree inner_type = TREE_TYPE (@1);}
2790 (if (ptr_difference_const (@0, @2, &diff))
2791 (neeq (plus { build_int_cst_type (inner_type, diff); } @1) @3)))))
2793 /* X - Y < X is the same as Y > 0 when there is no overflow.
2794 For equality, this is also true with wrapping overflow. */
2795 (for op (simple_comparison)
2797 (op:c @0 (minus@2 @0 @1))
2798 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2799 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2800 || ((op == EQ_EXPR || op == NE_EXPR)
2801 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
2802 && (CONSTANT_CLASS_P (@1) || single_use (@2)))
2803 (op @1 { build_zero_cst (TREE_TYPE (@1)); }))))
2806 (X / Y) == 0 -> X < Y if X, Y are unsigned.
2807 (X / Y) != 0 -> X >= Y, if X, Y are unsigned. */
2811 (cmp (trunc_div @0 @1) integer_zerop)
2812 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
2813 /* Complex ==/!= is allowed, but not </>=. */
2814 && TREE_CODE (TREE_TYPE (@0)) != COMPLEX_TYPE
2815 && (VECTOR_TYPE_P (type) || !VECTOR_TYPE_P (TREE_TYPE (@0))))
2818 /* X == C - X can never be true if C is odd. */
2821 (cmp:c (convert? @0) (convert1? (minus INTEGER_CST@1 (convert2? @0))))
2822 (if (TREE_INT_CST_LOW (@1) & 1)
2823 { constant_boolean_node (cmp == NE_EXPR, type); })))
2828 U needs to be non-negative.
2832 U and N needs to be non-negative
2836 U needs to be non-negative and N needs to be a negative constant.
2838 (for cmp (lt ge le gt )
2839 bitop (bit_ior bit_ior bit_and bit_and)
2841 (cmp:c (bitop:c tree_expr_nonnegative_p@0 @1) @0)
2842 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)))
2843 (if (bitop == BIT_AND_EXPR || tree_expr_nonnegative_p (@1))
2844 { constant_boolean_node (cmp == GE_EXPR || cmp == LE_EXPR, type); }
2845 /* The sign is opposite now so the comparison is swapped around. */
2846 (if (TREE_CODE (@1) == INTEGER_CST && wi::neg_p (wi::to_wide (@1)))
2847 { constant_boolean_node (cmp == LT_EXPR, type); })))))
2849 /* Arguments on which one can call get_nonzero_bits to get the bits
2851 (match with_possible_nonzero_bits
2853 (match with_possible_nonzero_bits
2855 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))))
2856 /* Slightly extended version, do not make it recursive to keep it cheap. */
2857 (match (with_possible_nonzero_bits2 @0)
2858 with_possible_nonzero_bits@0)
2859 (match (with_possible_nonzero_bits2 @0)
2860 (bit_and:c with_possible_nonzero_bits@0 @2))
2862 /* Same for bits that are known to be set, but we do not have
2863 an equivalent to get_nonzero_bits yet. */
2864 (match (with_certain_nonzero_bits2 @0)
2866 (match (with_certain_nonzero_bits2 @0)
2867 (bit_ior @1 INTEGER_CST@0))
2869 /* X == C (or X & Z == Y | C) is impossible if ~nonzero(X) & C != 0. */
2872 (cmp:c (with_possible_nonzero_bits2 @0) (with_certain_nonzero_bits2 @1))
2873 (if (wi::bit_and_not (wi::to_wide (@1), get_nonzero_bits (@0)) != 0)
2874 { constant_boolean_node (cmp == NE_EXPR, type); })))
2876 /* ((X inner_op C0) outer_op C1)
2877 With X being a tree where range has reasoned certain bits to always be
2878 zero throughout its computed value range,
2879 inner_op = {|,^}, outer_op = {|,^} and inner_op != outer_op
2880 where zero_mask has 1's for all bits that are sure to be 0 in
2882 if (inner_op == '^') C0 &= ~C1;
2883 if ((C0 & ~zero_mask) == 0) then emit (X outer_op (C0 outer_op C1)
2884 if ((C1 & ~zero_mask) == 0) then emit (X inner_op (C0 outer_op C1)
2886 (for inner_op (bit_ior bit_xor)
2887 outer_op (bit_xor bit_ior)
2890 (inner_op:s @2 INTEGER_CST@0) INTEGER_CST@1)
2894 wide_int zero_mask_not;
2898 if (TREE_CODE (@2) == SSA_NAME)
2899 zero_mask_not = get_nonzero_bits (@2);
2903 if (inner_op == BIT_XOR_EXPR)
2905 C0 = wi::bit_and_not (wi::to_wide (@0), wi::to_wide (@1));
2906 cst_emit = C0 | wi::to_wide (@1);
2910 C0 = wi::to_wide (@0);
2911 cst_emit = C0 ^ wi::to_wide (@1);
2914 (if (!fail && (C0 & zero_mask_not) == 0)
2915 (outer_op @2 { wide_int_to_tree (type, cst_emit); })
2916 (if (!fail && (wi::to_wide (@1) & zero_mask_not) == 0)
2917 (inner_op @2 { wide_int_to_tree (type, cst_emit); }))))))
2919 /* Associate (p +p off1) +p off2 as (p +p (off1 + off2)). */
2921 (pointer_plus (pointer_plus:s @0 @1) @3)
2922 (pointer_plus @0 (plus @1 @3)))
2925 (pointer_plus (convert:s (pointer_plus:s @0 @1)) @3)
2926 (convert:type (pointer_plus @0 (plus @1 @3))))
2933 tem4 = (unsigned long) tem3;
2938 (pointer_plus @0 (convert?@2 (minus@3 (convert @1) (convert @0))))
2939 /* Conditionally look through a sign-changing conversion. */
2940 (if (TYPE_PRECISION (TREE_TYPE (@2)) == TYPE_PRECISION (TREE_TYPE (@3))
2941 && ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@1)))
2942 || (GENERIC && type == TREE_TYPE (@1))))
2945 (pointer_plus @0 (convert?@2 (pointer_diff@3 @1 @@0)))
2946 (if (TYPE_PRECISION (TREE_TYPE (@2)) >= TYPE_PRECISION (TREE_TYPE (@3)))
2950 tem = (sizetype) ptr;
2954 and produce the simpler and easier to analyze with respect to alignment
2955 ... = ptr & ~algn; */
2957 (pointer_plus @0 (negate (bit_and (convert @0) INTEGER_CST@1)))
2958 (with { tree algn = wide_int_to_tree (TREE_TYPE (@0), ~wi::to_wide (@1)); }
2959 (bit_and @0 { algn; })))
2961 /* Try folding difference of addresses. */
2963 (minus (convert ADDR_EXPR@0) (convert (pointer_plus @1 @2)))
2964 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2965 (with { poly_int64 diff; }
2966 (if (ptr_difference_const (@0, @1, &diff))
2967 (minus { build_int_cst_type (type, diff); } (convert @2))))))
2969 (minus (convert (pointer_plus @0 @2)) (convert ADDR_EXPR@1))
2970 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2971 (with { poly_int64 diff; }
2972 (if (ptr_difference_const (@0, @1, &diff))
2973 (plus (convert @2) { build_int_cst_type (type, diff); })))))
2975 (minus (convert ADDR_EXPR@0) (convert @1))
2976 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2977 (with { poly_int64 diff; }
2978 (if (ptr_difference_const (@0, @1, &diff))
2979 { build_int_cst_type (type, diff); }))))
2981 (minus (convert @0) (convert ADDR_EXPR@1))
2982 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2983 (with { poly_int64 diff; }
2984 (if (ptr_difference_const (@0, @1, &diff))
2985 { build_int_cst_type (type, diff); }))))
2987 (pointer_diff (convert?@2 ADDR_EXPR@0) (convert1?@3 @1))
2988 (if (tree_nop_conversion_p (TREE_TYPE(@2), TREE_TYPE (@0))
2989 && tree_nop_conversion_p (TREE_TYPE(@3), TREE_TYPE (@1)))
2990 (with { poly_int64 diff; }
2991 (if (ptr_difference_const (@0, @1, &diff))
2992 { build_int_cst_type (type, diff); }))))
2994 (pointer_diff (convert?@2 @0) (convert1?@3 ADDR_EXPR@1))
2995 (if (tree_nop_conversion_p (TREE_TYPE(@2), TREE_TYPE (@0))
2996 && tree_nop_conversion_p (TREE_TYPE(@3), TREE_TYPE (@1)))
2997 (with { poly_int64 diff; }
2998 (if (ptr_difference_const (@0, @1, &diff))
2999 { build_int_cst_type (type, diff); }))))
3001 /* (&a+b) - (&a[1] + c) -> sizeof(a[0]) + (b - c) */
3003 (pointer_diff (pointer_plus ADDR_EXPR@0 @1) (pointer_plus ADDR_EXPR@2 @3))
3004 (with { poly_int64 diff; }
3005 (if (ptr_difference_const (@0, @2, &diff))
3006 (plus { build_int_cst_type (type, diff); } (convert (minus @1 @3))))))
3007 /* (p + b) - &p->d -> offsetof (*p, d) + b */
3009 (pointer_diff (pointer_plus @0 @1) ADDR_EXPR@2)
3010 (with { poly_int64 diff; }
3011 (if (ptr_difference_const (@0, @2, &diff))
3012 (plus { build_int_cst_type (type, diff); } (convert @1)))))
3014 (pointer_diff ADDR_EXPR@0 (pointer_plus @1 @2))
3015 (with { poly_int64 diff; }
3016 (if (ptr_difference_const (@0, @1, &diff))
3017 (minus { build_int_cst_type (type, diff); } (convert @2)))))
3019 /* Canonicalize (T *)(ptr - ptr-cst) to &MEM[ptr + -ptr-cst]. */
3021 (convert (pointer_diff @0 INTEGER_CST@1))
3022 (if (POINTER_TYPE_P (type))
3023 { build_fold_addr_expr_with_type
3024 (build2 (MEM_REF, char_type_node, @0,
3025 wide_int_to_tree (ptr_type_node, wi::neg (wi::to_wide (@1)))),
3028 /* If arg0 is derived from the address of an object or function, we may
3029 be able to fold this expression using the object or function's
3032 (bit_and (convert? @0) INTEGER_CST@1)
3033 (if (POINTER_TYPE_P (TREE_TYPE (@0))
3034 && tree_nop_conversion_p (type, TREE_TYPE (@0)))
3038 unsigned HOST_WIDE_INT bitpos;
3039 get_pointer_alignment_1 (@0, &align, &bitpos);
3041 (if (wi::ltu_p (wi::to_wide (@1), align / BITS_PER_UNIT))
3042 { wide_int_to_tree (type, (wi::to_wide (@1)
3043 & (bitpos / BITS_PER_UNIT))); }))))
3046 uniform_integer_cst_p
3048 tree int_cst = uniform_integer_cst_p (t);
3049 tree inner_type = TREE_TYPE (int_cst);
3051 (if ((INTEGRAL_TYPE_P (inner_type)
3052 || POINTER_TYPE_P (inner_type))
3053 && wi::eq_p (wi::to_wide (int_cst), wi::min_value (inner_type))))))
3056 uniform_integer_cst_p
3058 tree int_cst = uniform_integer_cst_p (t);
3059 tree itype = TREE_TYPE (int_cst);
3061 (if ((INTEGRAL_TYPE_P (itype)
3062 || POINTER_TYPE_P (itype))
3063 && wi::eq_p (wi::to_wide (int_cst), wi::max_value (itype))))))
3065 /* Unsigned Saturation Add */
3066 /* SAT_ADD = usadd_left_part_1 | usadd_right_part_1, aka:
3067 SAT_ADD = (X + Y) | -((X + Y) < X) */
3068 (match (usadd_left_part_1 @0 @1)
3070 (if (INTEGRAL_TYPE_P (type) && TYPE_UNSIGNED (type)
3071 && types_match (type, @0, @1))))
3073 /* SAT_ADD = usadd_left_part_2 | usadd_right_part_2, aka:
3074 SAT_ADD = REALPART_EXPR <.ADD_OVERFLOW> | (IMAGPART_EXPR <.ADD_OVERFLOW> != 0) */
3075 (match (usadd_left_part_2 @0 @1)
3076 (realpart (IFN_ADD_OVERFLOW:c @0 @1))
3077 (if (INTEGRAL_TYPE_P (type) && TYPE_UNSIGNED (type)
3078 && types_match (type, @0, @1))))
3080 /* SAT_ADD = usadd_left_part_1 | usadd_right_part_1, aka:
3081 SAT_ADD = (X + Y) | -((type)(X + Y) < X) */
3082 (match (usadd_right_part_1 @0 @1)
3083 (negate (convert (lt (plus:c @0 @1) @0)))
3084 (if (INTEGRAL_TYPE_P (type) && TYPE_UNSIGNED (type)
3085 && types_match (type, @0, @1))))
3087 /* SAT_ADD = usadd_left_part_1 | usadd_right_part_1, aka:
3088 SAT_ADD = (X + Y) | -(X > (X + Y)) */
3089 (match (usadd_right_part_1 @0 @1)
3090 (negate (convert (gt @0 (plus:c @0 @1))))
3091 (if (INTEGRAL_TYPE_P (type) && TYPE_UNSIGNED (type)
3092 && types_match (type, @0, @1))))
3094 /* SAT_ADD = usadd_left_part_2 | usadd_right_part_2, aka:
3095 SAT_ADD = REALPART_EXPR <.ADD_OVERFLOW> | (IMAGPART_EXPR <.ADD_OVERFLOW> != 0) */
3096 (match (usadd_right_part_2 @0 @1)
3097 (negate (convert (ne (imagpart (IFN_ADD_OVERFLOW:c @0 @1)) integer_zerop)))
3098 (if (INTEGRAL_TYPE_P (type) && TYPE_UNSIGNED (type)
3099 && types_match (type, @0, @1))))
3101 /* SAT_ADD = usadd_left_part_2 | usadd_right_part_2, aka:
3102 SAT_ADD = REALPART_EXPR <.ADD_OVERFLOW> | -IMAGPART_EXPR <.ADD_OVERFLOW> */
3103 (match (usadd_right_part_2 @0 @1)
3104 (negate (imagpart (IFN_ADD_OVERFLOW:c @0 @1)))
3105 (if (INTEGRAL_TYPE_P (type) && TYPE_UNSIGNED (type)
3106 && types_match (type, @0, @1))))
3108 /* We cannot merge or overload usadd_left_part_1 and usadd_left_part_2
3109 because the sub part of left_part_2 cannot work with right_part_1.
3110 For example, left_part_2 pattern focus one .ADD_OVERFLOW but the
3111 right_part_1 has nothing to do with .ADD_OVERFLOW. */
3113 /* Unsigned saturation add, case 1 (branchless):
3114 SAT_U_ADD = (X + Y) | - ((X + Y) < X) or
3115 SAT_U_ADD = (X + Y) | - (X > (X + Y)). */
3116 (match (unsigned_integer_sat_add @0 @1)
3117 (bit_ior:c (usadd_left_part_1 @0 @1) (usadd_right_part_1 @0 @1)))
3119 /* Unsigned saturation add, case 2 (branchless with .ADD_OVERFLOW):
3120 SAT_ADD = REALPART_EXPR <.ADD_OVERFLOW> | -IMAGPART_EXPR <.ADD_OVERFLOW> or
3121 SAT_ADD = REALPART_EXPR <.ADD_OVERFLOW> | (IMAGPART_EXPR <.ADD_OVERFLOW> != 0) */
3122 (match (unsigned_integer_sat_add @0 @1)
3123 (bit_ior:c (usadd_left_part_2 @0 @1) (usadd_right_part_2 @0 @1)))
3125 /* Unsigned saturation add, case 3 (branch with ge):
3126 SAT_U_ADD = (X + Y) >= x ? (X + Y) : -1. */
3127 (match (unsigned_integer_sat_add @0 @1)
3128 (cond^ (ge (usadd_left_part_1@2 @0 @1) @0) @2 integer_minus_onep))
3130 /* Unsigned saturation add, case 4 (branch with lt):
3131 SAT_U_ADD = (X + Y) < x ? -1 : (X + Y). */
3132 (match (unsigned_integer_sat_add @0 @1)
3133 (cond^ (lt (usadd_left_part_1@2 @0 @1) @0) integer_minus_onep @2))
3135 /* Unsigned saturation add, case 5 (branch with eq .ADD_OVERFLOW):
3136 SAT_U_ADD = REALPART_EXPR <.ADD_OVERFLOW> == 0 ? .ADD_OVERFLOW : -1. */
3137 (match (unsigned_integer_sat_add @0 @1)
3138 (cond^ (eq (imagpart (IFN_ADD_OVERFLOW:c @0 @1)) integer_zerop)
3139 (usadd_left_part_2 @0 @1) integer_minus_onep))
3141 /* Unsigned saturation add, case 6 (branch with ne .ADD_OVERFLOW):
3142 SAT_U_ADD = REALPART_EXPR <.ADD_OVERFLOW> != 0 ? -1 : .ADD_OVERFLOW. */
3143 (match (unsigned_integer_sat_add @0 @1)
3144 (cond^ (ne (imagpart (IFN_ADD_OVERFLOW:c @0 @1)) integer_zerop)
3145 integer_minus_onep (usadd_left_part_2 @0 @1)))
3147 /* Unsigned saturation add, case 7 (branch with le):
3148 SAT_ADD = x <= (X + Y) ? (X + Y) : -1. */
3149 (match (unsigned_integer_sat_add @0 @1)
3150 (cond^ (le @0 (usadd_left_part_1@2 @0 @1)) @2 integer_minus_onep))
3152 /* Unsigned saturation add, case 8 (branch with gt):
3153 SAT_ADD = x > (X + Y) ? -1 : (X + Y). */
3154 (match (unsigned_integer_sat_add @0 @1)
3155 (cond^ (gt @0 (usadd_left_part_1@2 @0 @1)) integer_minus_onep @2))
3157 /* Unsigned saturation add, case 9 (one op is imm):
3158 SAT_U_ADD = (X + 3) >= x ? (X + 3) : -1. */
3159 (match (unsigned_integer_sat_add @0 @1)
3160 (plus (min @0 INTEGER_CST@2) INTEGER_CST@1)
3161 (if (INTEGRAL_TYPE_P (type) && TYPE_UNSIGNED (type)
3162 && types_match (type, @0, @1))
3165 unsigned precision = TYPE_PRECISION (type);
3166 wide_int cst_1 = wi::to_wide (@1);
3167 wide_int cst_2 = wi::to_wide (@2);
3168 wide_int max = wi::mask (precision, false, precision);
3169 wide_int sum = wi::add (cst_1, cst_2);
3171 (if (wi::eq_p (max, sum))))))
3173 /* Unsigned saturation add, case 10 (one op is imm):
3174 SAT_U_ADD = __builtin_add_overflow (X, 3, &ret) == 0 ? ret : -1. */
3175 (match (unsigned_integer_sat_add @0 @1)
3176 (cond^ (ne (imagpart (IFN_ADD_OVERFLOW@2 @0 INTEGER_CST@1)) integer_zerop)
3177 integer_minus_onep (realpart @2))
3178 (if (INTEGRAL_TYPE_P (type) && TYPE_UNSIGNED (type)
3179 && types_match (type, @0))))
3181 /* Unsigned saturation sub, case 1 (branch with gt):
3182 SAT_U_SUB = X > Y ? X - Y : 0 */
3183 (match (unsigned_integer_sat_sub @0 @1)
3184 (cond^ (gt @0 @1) (minus @0 @1) integer_zerop)
3185 (if (INTEGRAL_TYPE_P (type) && TYPE_UNSIGNED (type)
3186 && types_match (type, @0, @1))))
3188 /* Unsigned saturation sub, case 2 (branch with ge):
3189 SAT_U_SUB = X >= Y ? X - Y : 0. */
3190 (match (unsigned_integer_sat_sub @0 @1)
3191 (cond^ (ge @0 @1) (convert? (minus (convert1? @0) (convert1? @1))) integer_zerop)
3192 (if (INTEGRAL_TYPE_P (type) && TYPE_UNSIGNED (type)
3193 && TYPE_UNSIGNED (TREE_TYPE (@0)) && types_match (@0, @1))))
3195 /* Unsigned saturation sub, case 3 (branchless with gt):
3196 SAT_U_SUB = (X - Y) * (X > Y). */
3197 (match (unsigned_integer_sat_sub @0 @1)
3198 (mult:c (minus @0 @1) (convert (gt @0 @1)))
3199 (if (INTEGRAL_TYPE_P (type) && TYPE_UNSIGNED (type)
3200 && types_match (type, @0, @1))))
3202 /* Unsigned saturation sub, case 4 (branchless with ge):
3203 SAT_U_SUB = (X - Y) * (X >= Y). */
3204 (match (unsigned_integer_sat_sub @0 @1)
3205 (mult:c (minus @0 @1) (convert (ge @0 @1)))
3206 (if (INTEGRAL_TYPE_P (type) && TYPE_UNSIGNED (type)
3207 && types_match (type, @0, @1))))
3209 /* Unsigned saturation sub, case 5 (branchless bit_and with .SUB_OVERFLOW). */
3210 (match (unsigned_integer_sat_sub @0 @1)
3211 (bit_and:c (realpart (IFN_SUB_OVERFLOW@2 @0 @1))
3212 (plus (imagpart @2) integer_minus_onep))
3213 (if (INTEGRAL_TYPE_P (type) && TYPE_UNSIGNED (type)
3214 && types_match (type, @0, @1))))
3216 /* Unsigned saturation sub, case 6 (branchless mult with .SUB_OVERFLOW). */
3217 (match (unsigned_integer_sat_sub @0 @1)
3218 (mult:c (realpart (IFN_SUB_OVERFLOW@2 @0 @1))
3219 (bit_xor (imagpart @2) integer_onep))
3220 (if (INTEGRAL_TYPE_P (type) && TYPE_UNSIGNED (type)
3221 && types_match (type, @0, @1))))
3223 /* Unsigned saturation sub, case 7 (branch eq with .SUB_OVERFLOW). */
3224 (match (unsigned_integer_sat_sub @0 @1)
3225 (cond^ (eq (imagpart (IFN_SUB_OVERFLOW@2 @0 @1)) integer_zerop)
3226 (realpart @2) integer_zerop)
3227 (if (INTEGRAL_TYPE_P (type) && TYPE_UNSIGNED (type)
3228 && types_match (type, @0, @1))))
3230 /* Unsigned saturation sub, case 8 (branch ne with .SUB_OVERFLOW). */
3231 (match (unsigned_integer_sat_sub @0 @1)
3232 (cond^ (ne (imagpart (IFN_SUB_OVERFLOW@2 @0 @1)) integer_zerop)
3233 integer_zerop (realpart @2))
3234 (if (INTEGRAL_TYPE_P (type) && TYPE_UNSIGNED (type)
3235 && types_match (type, @0, @1))))
3237 /* Unsigned saturation truncate, case 1, sizeof (WT) > sizeof (NT).
3238 SAT_U_TRUNC = (NT)x | (NT)(-(X > (WT)(NT)(-1))). */
3239 (match (unsigned_integer_sat_trunc @0)
3240 (bit_ior:c (negate (convert (gt @0 INTEGER_CST@1)))
3242 (if (INTEGRAL_TYPE_P (type) && TYPE_UNSIGNED (type)
3243 && TYPE_UNSIGNED (TREE_TYPE (@0)))
3246 unsigned itype_precision = TYPE_PRECISION (TREE_TYPE (@0));
3247 unsigned otype_precision = TYPE_PRECISION (type);
3248 wide_int trunc_max = wi::mask (otype_precision, false, itype_precision);
3249 wide_int int_cst = wi::to_wide (@1, itype_precision);
3251 (if (otype_precision < itype_precision && wi::eq_p (trunc_max, int_cst))))))
3253 /* Unsigned saturation truncate, case 2, sizeof (WT) > sizeof (NT).
3254 SAT_U_TRUNC = (NT)(MIN_EXPR (X, 255)). */
3255 /* If Op_0 def is MIN_EXPR and not single_use. Aka below pattern:
3257 _18 = MIN_EXPR <left_8, 4294967295>; // op_0 def
3258 iftmp.0_11 = (unsigned int) _18; // op_0
3259 stream.avail_out = iftmp.0_11;
3260 left_37 = left_8 - _18; // op_0 use
3262 Transfer to .SAT_TRUNC will have MIN_EXPR still live. Then the backend
3263 (for example x86/riscv) will have 2-3 more insns generation for .SAT_TRUNC
3264 besides the MIN_EXPR. Thus, keep the normal truncation as is should be
3265 the better choose. */
3266 (match (unsigned_integer_sat_trunc @0)
3267 (convert (min@2 @0 INTEGER_CST@1))
3268 (if (INTEGRAL_TYPE_P (type) && TYPE_UNSIGNED (type)
3269 && TYPE_UNSIGNED (TREE_TYPE (@0)) && single_use (@2))
3272 unsigned itype_precision = TYPE_PRECISION (TREE_TYPE (@0));
3273 unsigned otype_precision = TYPE_PRECISION (type);
3274 wide_int trunc_max = wi::mask (otype_precision, false, itype_precision);
3275 wide_int int_cst = wi::to_wide (@1, itype_precision);
3277 (if (otype_precision < itype_precision && wi::eq_p (trunc_max, int_cst))))))
3279 /* x > y && x != XXX_MIN --> x > y
3280 x > y && x == XXX_MIN --> false . */
3283 (bit_and:c (gt:c@2 @0 @1) (eqne @0 min_value))
3285 (if (eqne == EQ_EXPR)
3286 { constant_boolean_node (false, type); })
3287 (if (eqne == NE_EXPR)
3291 /* x < y && x != XXX_MAX --> x < y
3292 x < y && x == XXX_MAX --> false. */
3295 (bit_and:c (lt:c@2 @0 @1) (eqne @0 max_value))
3297 (if (eqne == EQ_EXPR)
3298 { constant_boolean_node (false, type); })
3299 (if (eqne == NE_EXPR)
3303 /* x <= y && x == XXX_MIN --> x == XXX_MIN. */
3305 (bit_and:c (le:c @0 @1) (eq@2 @0 min_value))
3308 /* x >= y && x == XXX_MAX --> x == XXX_MAX. */
3310 (bit_and:c (ge:c @0 @1) (eq@2 @0 max_value))
3313 /* x > y || x != XXX_MIN --> x != XXX_MIN. */
3315 (bit_ior:c (gt:c @0 @1) (ne@2 @0 min_value))
3318 /* x <= y || x != XXX_MIN --> true. */
3320 (bit_ior:c (le:c @0 @1) (ne @0 min_value))
3321 { constant_boolean_node (true, type); })
3323 /* x <= y || x == XXX_MIN --> x <= y. */
3325 (bit_ior:c (le:c@2 @0 @1) (eq @0 min_value))
3328 /* x < y || x != XXX_MAX --> x != XXX_MAX. */
3330 (bit_ior:c (lt:c @0 @1) (ne@2 @0 max_value))
3333 /* x >= y || x != XXX_MAX --> true
3334 x >= y || x == XXX_MAX --> x >= y. */
3337 (bit_ior:c (ge:c@2 @0 @1) (eqne @0 max_value))
3339 (if (eqne == EQ_EXPR)
3341 (if (eqne == NE_EXPR)
3342 { constant_boolean_node (true, type); }))))
3344 /* y == XXX_MIN || x < y --> x <= y - 1 */
3346 (bit_ior:c (eq:s @1 min_value) (lt:cs @0 @1))
3347 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
3348 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
3349 (le @0 (minus @1 { build_int_cst (TREE_TYPE (@1), 1); }))))
3351 /* y != XXX_MIN && x >= y --> x > y - 1 */
3353 (bit_and:c (ne:s @1 min_value) (ge:cs @0 @1))
3354 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
3355 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
3356 (gt @0 (minus @1 { build_int_cst (TREE_TYPE (@1), 1); }))))
3358 /* Convert (X == CST1) && ((other)X OP2 CST2) to a known value
3359 based on CST1 OP2 CST2. Similarly for (X != CST1). */
3360 /* Convert (X == Y) && (X OP2 Y) to a known value if X is an integral type.
3361 Similarly for (X != Y). */
3364 (for code2 (eq ne lt gt le ge)
3366 (bit_and:c (code1:c@3 @0 @1) (code2:c@4 (convert?@c0 @0) @2))
3367 (if ((TREE_CODE (@1) == INTEGER_CST
3368 && TREE_CODE (@2) == INTEGER_CST)
3369 || ((INTEGRAL_TYPE_P (TREE_TYPE (@1))
3370 || POINTER_TYPE_P (TREE_TYPE (@1)))
3371 && bitwise_equal_p (@1, @2)))
3374 bool one_before = false;
3375 bool one_after = false;
3377 bool allbits = true;
3378 if (TREE_CODE (@1) == INTEGER_CST
3379 && TREE_CODE (@2) == INTEGER_CST)
3381 allbits = TYPE_PRECISION (TREE_TYPE (@1)) <= TYPE_PRECISION (TREE_TYPE (@2));
3382 auto t1 = wi::to_wide (fold_convert (TREE_TYPE (@2), @1));
3383 auto t2 = wi::to_wide (@2);
3384 cmp = wi::cmp (t1, t2, TYPE_SIGN (TREE_TYPE (@2)));
3395 case EQ_EXPR: val = (cmp == 0); break;
3396 case NE_EXPR: val = (cmp != 0); break;
3397 case LT_EXPR: val = (cmp < 0); break;
3398 case GT_EXPR: val = (cmp > 0); break;
3399 case LE_EXPR: val = (cmp <= 0); break;
3400 case GE_EXPR: val = (cmp >= 0); break;
3401 default: gcc_unreachable ();
3405 (if (code1 == EQ_EXPR && val) @3)
3406 (if (code1 == EQ_EXPR && !val) { constant_boolean_node (false, type); })
3407 (if (code1 == NE_EXPR && !val && allbits) @4)
3408 (if (code1 == NE_EXPR
3412 (gt @c0 (convert @1)))
3413 (if (code1 == NE_EXPR
3417 (lt @c0 (convert @1)))
3418 /* (a != (b+1)) & (a > b) -> a > (b+1) */
3419 (if (code1 == NE_EXPR
3423 (gt @c0 (convert @1)))
3424 /* (a != (b-1)) & (a < b) -> a < (b-1) */
3425 (if (code1 == NE_EXPR
3429 (lt @c0 (convert @1)))
3437 /* Convert (X OP1 CST1) && (X OP2 CST2).
3438 Convert (X OP1 Y) && (X OP2 Y). */
3440 (for code1 (lt le gt ge)
3441 (for code2 (lt le gt ge)
3443 (bit_and (code1:c@3 @0 @1) (code2:c@4 @0 @2))
3444 (if ((TREE_CODE (@1) == INTEGER_CST
3445 && TREE_CODE (@2) == INTEGER_CST)
3446 || ((INTEGRAL_TYPE_P (TREE_TYPE (@1))
3447 || POINTER_TYPE_P (TREE_TYPE (@1)))
3448 && operand_equal_p (@1, @2)))
3452 if (TREE_CODE (@1) == INTEGER_CST
3453 && TREE_CODE (@2) == INTEGER_CST)
3454 cmp = tree_int_cst_compare (@1, @2);
3457 /* Choose the more restrictive of two < or <= comparisons. */
3458 (if ((code1 == LT_EXPR || code1 == LE_EXPR)
3459 && (code2 == LT_EXPR || code2 == LE_EXPR))
3460 (if ((cmp < 0) || (cmp == 0 && code1 == LT_EXPR))
3463 /* Likewise chose the more restrictive of two > or >= comparisons. */
3464 (if ((code1 == GT_EXPR || code1 == GE_EXPR)
3465 && (code2 == GT_EXPR || code2 == GE_EXPR))
3466 (if ((cmp > 0) || (cmp == 0 && code1 == GT_EXPR))
3469 /* Check for singleton ranges. */
3471 && ((code1 == LE_EXPR && code2 == GE_EXPR)
3472 || (code1 == GE_EXPR && code2 == LE_EXPR)))
3474 /* Check for disjoint ranges. */
3476 && (code1 == LT_EXPR || code1 == LE_EXPR)
3477 && (code2 == GT_EXPR || code2 == GE_EXPR))
3478 { constant_boolean_node (false, type); })
3480 && (code1 == GT_EXPR || code1 == GE_EXPR)
3481 && (code2 == LT_EXPR || code2 == LE_EXPR))
3482 { constant_boolean_node (false, type); })
3485 /* Convert (X == CST1) || (X OP2 CST2) to a known value
3486 based on CST1 OP2 CST2. Similarly for (X != CST1). */
3487 /* Convert (X == Y) || (X OP2 Y) to a known value if X is an integral type.
3488 Similarly for (X != Y). */
3491 (for code2 (eq ne lt gt le ge)
3493 (bit_ior:c (code1:c@3 @0 @1) (code2:c@4 (convert?@c0 @0) @2))
3494 (if ((TREE_CODE (@1) == INTEGER_CST
3495 && TREE_CODE (@2) == INTEGER_CST)
3496 || ((INTEGRAL_TYPE_P (TREE_TYPE (@1))
3497 || POINTER_TYPE_P (TREE_TYPE (@1)))
3498 && bitwise_equal_p (@1, @2)))
3501 bool one_before = false;
3502 bool one_after = false;
3504 bool allbits = true;
3505 if (TREE_CODE (@1) == INTEGER_CST
3506 && TREE_CODE (@2) == INTEGER_CST)
3508 allbits = TYPE_PRECISION (TREE_TYPE (@1)) <= TYPE_PRECISION (TREE_TYPE (@2));
3509 auto t1 = wi::to_wide (fold_convert (TREE_TYPE (@2), @1));
3510 auto t2 = wi::to_wide (@2);
3511 cmp = wi::cmp (t1, t2, TYPE_SIGN (TREE_TYPE (@2)));
3522 case EQ_EXPR: val = (cmp == 0); break;
3523 case NE_EXPR: val = (cmp != 0); break;
3524 case LT_EXPR: val = (cmp < 0); break;
3525 case GT_EXPR: val = (cmp > 0); break;
3526 case LE_EXPR: val = (cmp <= 0); break;
3527 case GE_EXPR: val = (cmp >= 0); break;
3528 default: gcc_unreachable ();
3532 (if (code1 == EQ_EXPR && val) @4)
3533 (if (code1 == NE_EXPR && val && allbits) { constant_boolean_node (true, type); })
3534 (if (code1 == NE_EXPR && !val && allbits) @3)
3535 (if (code1 == EQ_EXPR
3540 (if (code1 == EQ_EXPR
3545 /* (a == (b-1)) | (a >= b) -> a >= (b-1) */
3546 (if (code1 == EQ_EXPR
3550 (ge @c0 (convert @1)))
3551 /* (a == (b+1)) | (a <= b) -> a <= (b-1) */
3552 (if (code1 == EQ_EXPR
3556 (le @c0 (convert @1)))
3564 /* Convert (X OP1 CST1) || (X OP2 CST2).
3565 Convert (X OP1 Y) || (X OP2 Y). */
3567 (for code1 (lt le gt ge)
3568 (for code2 (lt le gt ge)
3570 (bit_ior (code1@3 @0 @1) (code2@4 @0 @2))
3571 (if ((TREE_CODE (@1) == INTEGER_CST
3572 && TREE_CODE (@2) == INTEGER_CST)
3573 || ((INTEGRAL_TYPE_P (TREE_TYPE (@1))
3574 || POINTER_TYPE_P (TREE_TYPE (@1)))
3575 && operand_equal_p (@1, @2)))
3579 if (TREE_CODE (@1) == INTEGER_CST
3580 && TREE_CODE (@2) == INTEGER_CST)
3581 cmp = tree_int_cst_compare (@1, @2);
3584 /* Choose the more restrictive of two < or <= comparisons. */
3585 (if ((code1 == LT_EXPR || code1 == LE_EXPR)
3586 && (code2 == LT_EXPR || code2 == LE_EXPR))
3587 (if ((cmp < 0) || (cmp == 0 && code1 == LT_EXPR))
3590 /* Likewise chose the more restrictive of two > or >= comparisons. */
3591 (if ((code1 == GT_EXPR || code1 == GE_EXPR)
3592 && (code2 == GT_EXPR || code2 == GE_EXPR))
3593 (if ((cmp > 0) || (cmp == 0 && code1 == GT_EXPR))
3596 /* Check for singleton ranges. */
3598 && ((code1 == LT_EXPR && code2 == GT_EXPR)
3599 || (code1 == GT_EXPR && code2 == LT_EXPR)))
3601 /* Check for disjoint ranges. */
3603 && (code1 == LT_EXPR || code1 == LE_EXPR)
3604 && (code2 == GT_EXPR || code2 == GE_EXPR))
3605 { constant_boolean_node (true, type); })
3607 && (code1 == GT_EXPR || code1 == GE_EXPR)
3608 && (code2 == LT_EXPR || code2 == LE_EXPR))
3609 { constant_boolean_node (true, type); })
3612 /* Optimize (a CMP b) ^ (a CMP b) */
3613 /* Optimize (a CMP b) != (a CMP b) */
3614 (for op (bit_xor ne)
3615 (for cmp1 (lt lt lt le le le)
3616 cmp2 (gt eq ne ge eq ne)
3617 rcmp (ne le gt ne lt ge)
3619 (op:c (cmp1:c @0 @1) (cmp2:c @0 @1))
3620 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
3623 /* Optimize (a CMP b) == (a CMP b) */
3624 (for cmp1 (lt lt lt le le le)
3625 cmp2 (gt eq ne ge eq ne)
3626 rcmp (eq gt le eq ge lt)
3628 (eq:c (cmp1:c @0 @1) (cmp2:c @0 @1))
3629 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
3632 /* (type)([0,1]@a != 0) -> (type)a
3633 (type)([0,1]@a == 1) -> (type)a
3634 (type)([0,1]@a == 0) -> a ^ 1
3635 (type)([0,1]@a != 1) -> a ^ 1. */
3638 (convert (eqne zero_one_valued_p@0 INTEGER_CST@1))
3639 (if ((integer_zerop (@1) || integer_onep (@1)))
3640 (if ((eqne == EQ_EXPR) ^ integer_zerop (@1))
3642 /* Only do this if the types match as (type)(a == 0) is
3643 canonical form normally, while `a ^ 1` is canonical when
3644 there is no type change. */
3645 (if (types_match (type, TREE_TYPE (@0)))
3646 (bit_xor @0 { build_one_cst (type); } ))))))
3648 /* We can't reassociate at all for saturating types. */
3649 (if (!TYPE_SATURATING (type))
3651 /* Contract negates. */
3652 /* A + (-B) -> A - B */
3654 (plus:c @0 (convert? (negate @1)))
3655 /* Apply STRIP_NOPS on the negate. */
3656 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
3657 && !TYPE_OVERFLOW_SANITIZED (type))
3661 if (INTEGRAL_TYPE_P (type)
3662 && TYPE_OVERFLOW_WRAPS (type) != TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
3663 t1 = TYPE_OVERFLOW_WRAPS (type) ? type : TREE_TYPE (@1);
3665 (convert (minus (convert:t1 @0) (convert:t1 @1))))))
3666 /* A - (-B) -> A + B */
3668 (minus @0 (convert? (negate @1)))
3669 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
3670 && !TYPE_OVERFLOW_SANITIZED (type))
3674 if (INTEGRAL_TYPE_P (type)
3675 && TYPE_OVERFLOW_WRAPS (type) != TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
3676 t1 = TYPE_OVERFLOW_WRAPS (type) ? type : TREE_TYPE (@1);
3678 (convert (plus (convert:t1 @0) (convert:t1 @1))))))
3680 Sign-extension is ok except for INT_MIN, which thankfully cannot
3681 happen without overflow. */
3683 (negate (convert (negate @1)))
3684 (if (INTEGRAL_TYPE_P (type)
3685 && (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@1))
3686 || (!TYPE_UNSIGNED (TREE_TYPE (@1))
3687 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))))
3688 && !TYPE_OVERFLOW_SANITIZED (type)
3689 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@1)))
3692 (negate (convert negate_expr_p@1))
3693 (if (SCALAR_FLOAT_TYPE_P (type)
3694 && ((DECIMAL_FLOAT_TYPE_P (type)
3695 == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1))
3696 && TYPE_PRECISION (type) >= TYPE_PRECISION (TREE_TYPE (@1)))
3697 || !HONOR_SIGN_DEPENDENT_ROUNDING (type)))
3698 (convert (negate @1))))
3700 (negate (nop_convert? (negate @1)))
3701 (if (!TYPE_OVERFLOW_SANITIZED (type)
3702 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@1)))
3705 /* We can't reassociate floating-point unless -fassociative-math
3706 or fixed-point plus or minus because of saturation to +-Inf. */
3707 (if ((!FLOAT_TYPE_P (type) || flag_associative_math)
3708 && !FIXED_POINT_TYPE_P (type))
3710 /* Match patterns that allow contracting a plus-minus pair
3711 irrespective of overflow issues. */
3712 /* (A +- B) - A -> +- B */
3713 /* (A +- B) -+ B -> A */
3714 /* A - (A +- B) -> -+ B */
3715 /* A +- (B -+ A) -> +- B */
3717 (minus (nop_convert1? (plus:c (nop_convert2? @0) @1)) @0)
3720 (minus (nop_convert1? (minus (nop_convert2? @0) @1)) @0)
3721 (if (!ANY_INTEGRAL_TYPE_P (type)
3722 || TYPE_OVERFLOW_WRAPS (type))
3723 (negate (view_convert @1))
3724 (view_convert (negate @1))))
3726 (plus:c (nop_convert1? (minus @0 (nop_convert2? @1))) @1)
3729 (minus @0 (nop_convert1? (plus:c (nop_convert2? @0) @1)))
3730 (if (!ANY_INTEGRAL_TYPE_P (type)
3731 || TYPE_OVERFLOW_WRAPS (type))
3732 (negate (view_convert @1))
3733 (view_convert (negate @1))))
3735 (minus @0 (nop_convert1? (minus (nop_convert2? @0) @1)))
3737 /* (A +- B) + (C - A) -> C +- B */
3738 /* (A + B) - (A - C) -> B + C */
3739 /* More cases are handled with comparisons. */
3741 (plus:c (plus:c @0 @1) (minus @2 @0))
3744 (plus:c (minus @0 @1) (minus @2 @0))
3747 (plus:c (pointer_diff @0 @1) (pointer_diff @2 @0))
3748 (if (TYPE_OVERFLOW_UNDEFINED (type)
3749 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0)))
3750 (pointer_diff @2 @1)))
3752 (minus (plus:c @0 @1) (minus @0 @2))
3755 /* (A +- CST1) +- CST2 -> A + CST3
3756 Use view_convert because it is safe for vectors and equivalent for
3758 (for outer_op (plus minus)
3759 (for inner_op (plus minus)
3760 neg_inner_op (minus plus)
3762 (outer_op (nop_convert? (inner_op @0 CONSTANT_CLASS_P@1))
3764 /* If one of the types wraps, use that one. */
3765 (if (!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_WRAPS (type))
3766 /* If all 3 captures are CONSTANT_CLASS_P, punt, as we might recurse
3767 forever if something doesn't simplify into a constant. */
3768 (if (!CONSTANT_CLASS_P (@0))
3769 (if (outer_op == PLUS_EXPR)
3770 (plus (view_convert @0) (inner_op! @2 (view_convert @1)))
3771 (minus (view_convert @0) (neg_inner_op! @2 (view_convert @1)))))
3772 (if (!ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3773 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
3774 (if (outer_op == PLUS_EXPR)
3775 (view_convert (plus @0 (inner_op! (view_convert @2) @1)))
3776 (view_convert (minus @0 (neg_inner_op! (view_convert @2) @1))))
3777 /* If the constant operation overflows we cannot do the transform
3778 directly as we would introduce undefined overflow, for example
3779 with (a - 1) + INT_MIN. */
3780 (if (types_match (type, @0) && !TYPE_OVERFLOW_SANITIZED (type))
3781 (with { tree cst = const_binop (outer_op == inner_op
3782 ? PLUS_EXPR : MINUS_EXPR,
3785 (if (INTEGRAL_TYPE_P (type) && !TREE_OVERFLOW (cst))
3786 (inner_op @0 { cst; } )
3787 /* X+INT_MAX+1 is X-INT_MIN. */
3788 (if (INTEGRAL_TYPE_P (type)
3789 && wi::to_wide (cst) == wi::min_value (type))
3790 (neg_inner_op @0 { wide_int_to_tree (type, wi::to_wide (cst)); })
3791 /* Last resort, use some unsigned type. */
3792 (with { tree utype = unsigned_type_for (type); }
3794 (view_convert (inner_op
3795 (view_convert:utype @0)
3797 { TREE_OVERFLOW (cst)
3798 ? drop_tree_overflow (cst) : cst; })))))))))))))))
3800 /* (CST1 - A) +- CST2 -> CST3 - A */
3801 (for outer_op (plus minus)
3803 (outer_op (nop_convert? (minus CONSTANT_CLASS_P@1 @0)) CONSTANT_CLASS_P@2)
3804 /* If one of the types wraps, use that one. */
3805 (if (!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_WRAPS (type))
3806 /* If all 3 captures are CONSTANT_CLASS_P, punt, as we might recurse
3807 forever if something doesn't simplify into a constant. */
3808 (if (!CONSTANT_CLASS_P (@0))
3809 (minus (outer_op! (view_convert @1) @2) (view_convert @0)))
3810 (if (!ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3811 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
3812 (view_convert (minus (outer_op! @1 (view_convert @2)) @0))
3813 (if (types_match (type, @0) && !TYPE_OVERFLOW_SANITIZED (type))
3814 (with { tree cst = const_binop (outer_op, type, @1, @2); }
3815 (if (cst && !TREE_OVERFLOW (cst))
3816 (minus { cst; } @0))))))))
3818 /* CST1 - (CST2 - A) -> CST3 + A
3819 Use view_convert because it is safe for vectors and equivalent for
3822 (minus CONSTANT_CLASS_P@1 (nop_convert? (minus CONSTANT_CLASS_P@2 @0)))
3823 /* If one of the types wraps, use that one. */
3824 (if (!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_WRAPS (type))
3825 /* If all 3 captures are CONSTANT_CLASS_P, punt, as we might recurse
3826 forever if something doesn't simplify into a constant. */
3827 (if (!CONSTANT_CLASS_P (@0))
3828 (plus (view_convert @0) (minus! @1 (view_convert @2))))
3829 (if (!ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3830 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
3831 (view_convert (plus @0 (minus! (view_convert @1) @2)))
3832 (if (types_match (type, @0) && !TYPE_OVERFLOW_SANITIZED (type))
3833 (with { tree cst = const_binop (MINUS_EXPR, type, @1, @2); }
3834 (if (cst && !TREE_OVERFLOW (cst))
3835 (plus { cst; } @0)))))))
3837 /* ((T)(A)) + CST -> (T)(A + CST) */
3840 (plus (convert:s SSA_NAME@0) INTEGER_CST@1)
3841 (if (TREE_CODE (TREE_TYPE (@0)) == INTEGER_TYPE
3842 && TREE_CODE (type) == INTEGER_TYPE
3843 && TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (@0))
3844 && int_fits_type_p (@1, TREE_TYPE (@0)))
3845 /* Perform binary operation inside the cast if the constant fits
3846 and (A + CST)'s range does not overflow. */
3849 wi::overflow_type min_ovf = wi::OVF_OVERFLOW,
3850 max_ovf = wi::OVF_OVERFLOW;
3851 tree inner_type = TREE_TYPE (@0);
3854 = wide_int::from (wi::to_wide (@1), TYPE_PRECISION (inner_type),
3855 TYPE_SIGN (inner_type));
3858 if (get_global_range_query ()->range_of_expr (vr, @0)
3859 && !vr.varying_p () && !vr.undefined_p ())
3861 wide_int wmin0 = vr.lower_bound ();
3862 wide_int wmax0 = vr.upper_bound ();
3863 wi::add (wmin0, w1, TYPE_SIGN (inner_type), &min_ovf);
3864 wi::add (wmax0, w1, TYPE_SIGN (inner_type), &max_ovf);
3867 (if (min_ovf == wi::OVF_NONE && max_ovf == wi::OVF_NONE)
3868 (convert (plus @0 { wide_int_to_tree (TREE_TYPE (@0), w1); } )))
3872 /* ((T)(A + CST1)) + CST2 -> (T)(A) + (T)CST1 + CST2 */
3874 (for op (plus minus)
3876 (plus (convert:s (op:s @0 INTEGER_CST@1)) INTEGER_CST@2)
3877 (if (TREE_CODE (TREE_TYPE (@0)) == INTEGER_TYPE
3878 && TREE_CODE (type) == INTEGER_TYPE
3879 && TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (@0))
3880 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
3881 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0))
3882 && TYPE_OVERFLOW_WRAPS (type))
3883 (plus (convert @0) (op @2 (convert @1))))))
3886 /* (T)(A) +- (T)(B) -> (T)(A +- B) only when (A +- B) could be simplified
3887 to a simple value. */
3888 (for op (plus minus)
3890 (op (convert @0) (convert @1))
3891 (if (INTEGRAL_TYPE_P (type)
3892 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
3893 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))
3894 && types_match (TREE_TYPE (@0), TREE_TYPE (@1))
3895 && !TYPE_OVERFLOW_TRAPS (type)
3896 && !TYPE_OVERFLOW_SANITIZED (type))
3897 (convert (op! @0 @1)))))
3901 (plus:c (convert? (bit_not @0)) (convert? @0))
3902 (if (!TYPE_OVERFLOW_TRAPS (type))
3903 (convert { build_all_ones_cst (TREE_TYPE (@0)); })))
3907 (plus (convert? (bit_not @0)) integer_each_onep)
3908 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
3909 (negate (convert @0))))
3913 (minus (convert? (negate @0)) integer_each_onep)
3914 (if (!TYPE_OVERFLOW_TRAPS (type)
3915 && TREE_CODE (type) != COMPLEX_TYPE
3916 && tree_nop_conversion_p (type, TREE_TYPE (@0)))
3917 (bit_not (convert @0))))
3921 (minus integer_all_onesp @0)
3922 (if (TREE_CODE (type) != COMPLEX_TYPE)
3925 /* (T)(P + A) - (T)P -> (T) A */
3927 (minus (convert (plus:c @@0 @1))
3929 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
3930 /* For integer types, if A has a smaller type
3931 than T the result depends on the possible
3933 E.g. T=size_t, A=(unsigned)429497295, P>0.
3934 However, if an overflow in P + A would cause
3935 undefined behavior, we can assume that there
3937 || (INTEGRAL_TYPE_P (TREE_TYPE (@1))
3938 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))))
3941 (minus (convert (pointer_plus @@0 @1))
3943 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
3944 /* For pointer types, if the conversion of A to the
3945 final type requires a sign- or zero-extension,
3946 then we have to punt - it is not defined which
3948 || (POINTER_TYPE_P (TREE_TYPE (@0))
3949 && TREE_CODE (@1) == INTEGER_CST
3950 && tree_int_cst_sign_bit (@1) == 0))
3953 (pointer_diff (pointer_plus @@0 @1) @0)
3954 /* The second argument of pointer_plus must be interpreted as signed, and
3955 thus sign-extended if necessary. */
3956 (with { tree stype = signed_type_for (TREE_TYPE (@1)); }
3957 /* Use view_convert instead of convert here, as POINTER_PLUS_EXPR
3958 second arg is unsigned even when we need to consider it as signed,
3959 we don't want to diagnose overflow here. */
3960 (convert (view_convert:stype @1))))
3962 /* (T)P - (T)(P + A) -> -(T) A */
3964 (minus (convert? @0)
3965 (convert (plus:c @@0 @1)))
3966 (if (INTEGRAL_TYPE_P (type)
3967 && TYPE_OVERFLOW_UNDEFINED (type)
3968 /* For integer literals, using an intermediate unsigned type to avoid
3969 an overflow at run time is counter-productive because it introduces
3970 spurious overflows at compile time, in the form of TREE_OVERFLOW on
3971 the result, which may be problematic in GENERIC for some front-ends:
3972 (T)P - (T)(P + 4) -> (T)(-(U)4) -> (T)(4294967292) -> -4(OVF)
3973 so we use the direct path for them. */
3974 && TREE_CODE (@1) != INTEGER_CST
3975 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
3976 (with { tree utype = unsigned_type_for (type); }
3977 (convert (negate (convert:utype @1))))
3978 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
3979 /* For integer types, if A has a smaller type
3980 than T the result depends on the possible
3982 E.g. T=size_t, A=(unsigned)429497295, P>0.
3983 However, if an overflow in P + A would cause
3984 undefined behavior, we can assume that there
3986 || (INTEGRAL_TYPE_P (TREE_TYPE (@1))
3987 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))))
3988 (negate (convert @1)))))
3991 (convert (pointer_plus @@0 @1)))
3992 (if (INTEGRAL_TYPE_P (type)
3993 && TYPE_OVERFLOW_UNDEFINED (type)
3994 /* See above the rationale for this condition. */
3995 && TREE_CODE (@1) != INTEGER_CST
3996 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
3997 (with { tree utype = unsigned_type_for (type); }
3998 (convert (negate (convert:utype @1))))
3999 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
4000 /* For pointer types, if the conversion of A to the
4001 final type requires a sign- or zero-extension,
4002 then we have to punt - it is not defined which
4004 || (POINTER_TYPE_P (TREE_TYPE (@0))
4005 && TREE_CODE (@1) == INTEGER_CST
4006 && tree_int_cst_sign_bit (@1) == 0))
4007 (negate (convert @1)))))
4009 (pointer_diff @0 (pointer_plus @@0 @1))
4010 /* The second argument of pointer_plus must be interpreted as signed, and
4011 thus sign-extended if necessary. */
4012 (with { tree stype = signed_type_for (TREE_TYPE (@1)); }
4013 /* Use view_convert instead of convert here, as POINTER_PLUS_EXPR
4014 second arg is unsigned even when we need to consider it as signed,
4015 we don't want to diagnose overflow here. */
4016 (negate (convert (view_convert:stype @1)))))
4018 /* (T)(P + A) - (T)(P + B) -> (T)A - (T)B */
4020 (minus (convert (plus:c @@0 @1))
4021 (convert (plus:c @0 @2)))
4022 (if (INTEGRAL_TYPE_P (type)
4023 && TYPE_OVERFLOW_UNDEFINED (type)
4024 && element_precision (type) <= element_precision (TREE_TYPE (@1))
4025 && element_precision (type) <= element_precision (TREE_TYPE (@2)))
4026 (with { tree utype = unsigned_type_for (type); }
4027 (convert (minus (convert:utype @1) (convert:utype @2))))
4028 (if (((element_precision (type) <= element_precision (TREE_TYPE (@1)))
4029 == (element_precision (type) <= element_precision (TREE_TYPE (@2))))
4030 && (element_precision (type) <= element_precision (TREE_TYPE (@1))
4031 /* For integer types, if A has a smaller type
4032 than T the result depends on the possible
4034 E.g. T=size_t, A=(unsigned)429497295, P>0.
4035 However, if an overflow in P + A would cause
4036 undefined behavior, we can assume that there
4038 || (INTEGRAL_TYPE_P (TREE_TYPE (@1))
4039 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
4040 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))
4041 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@2)))))
4042 (minus (convert @1) (convert @2)))))
4044 (minus (convert (pointer_plus @@0 @1))
4045 (convert (pointer_plus @0 @2)))
4046 (if (INTEGRAL_TYPE_P (type)
4047 && TYPE_OVERFLOW_UNDEFINED (type)
4048 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
4049 (with { tree utype = unsigned_type_for (type); }
4050 (convert (minus (convert:utype @1) (convert:utype @2))))
4051 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
4052 /* For pointer types, if the conversion of A to the
4053 final type requires a sign- or zero-extension,
4054 then we have to punt - it is not defined which
4056 || (POINTER_TYPE_P (TREE_TYPE (@0))
4057 && TREE_CODE (@1) == INTEGER_CST
4058 && tree_int_cst_sign_bit (@1) == 0
4059 && TREE_CODE (@2) == INTEGER_CST
4060 && tree_int_cst_sign_bit (@2) == 0))
4061 (minus (convert @1) (convert @2)))))
4063 (pointer_diff (pointer_plus @0 @2) (pointer_plus @1 @2))
4064 (pointer_diff @0 @1))
4066 (pointer_diff (pointer_plus @@0 @1) (pointer_plus @0 @2))
4067 /* The second argument of pointer_plus must be interpreted as signed, and
4068 thus sign-extended if necessary. */
4069 (with { tree stype = signed_type_for (TREE_TYPE (@1)); }
4070 /* Use view_convert instead of convert here, as POINTER_PLUS_EXPR
4071 second arg is unsigned even when we need to consider it as signed,
4072 we don't want to diagnose overflow here. */
4073 (minus (convert (view_convert:stype @1))
4074 (convert (view_convert:stype @2)))))))
4076 /* (A * C) +- (B * C) -> (A+-B) * C and (A * C) +- A -> A * (C+-1).
4077 Modeled after fold_plusminus_mult_expr. */
4078 (if (!TYPE_SATURATING (type)
4079 && (!FLOAT_TYPE_P (type) || flag_associative_math))
4080 (for plusminus (plus minus)
4082 (plusminus (mult:cs@3 @0 @1) (mult:cs@4 @0 @2))
4083 (if (!ANY_INTEGRAL_TYPE_P (type)
4084 || TYPE_OVERFLOW_WRAPS (type)
4085 || (INTEGRAL_TYPE_P (type)
4086 && tree_expr_nonzero_p (@0)
4087 && expr_not_equal_to (@0, wi::minus_one (TYPE_PRECISION (type)))))
4088 (if (single_use (@3) || single_use (@4))
4089 /* If @1 +- @2 is constant require a hard single-use on either
4090 original operand (but not on both). */
4091 (mult (plusminus @1 @2) @0)
4092 (mult! (plusminus @1 @2) @0)
4094 /* We cannot generate constant 1 for fract. */
4095 (if (!ALL_FRACT_MODE_P (TYPE_MODE (type)))
4097 (plusminus @0 (mult:c@3 @0 @2))
4098 (if ((!ANY_INTEGRAL_TYPE_P (type)
4099 || TYPE_OVERFLOW_WRAPS (type)
4100 /* For @0 + @0*@2 this transformation would introduce UB
4101 (where there was none before) for @0 in [-1,0] and @2 max.
4102 For @0 - @0*@2 this transformation would introduce UB
4103 for @0 0 and @2 in [min,min+1] or @0 -1 and @2 min+1. */
4104 || (INTEGRAL_TYPE_P (type)
4105 && ((tree_expr_nonzero_p (@0)
4106 && expr_not_equal_to (@0,
4107 wi::minus_one (TYPE_PRECISION (type))))
4108 || (plusminus == PLUS_EXPR
4109 ? expr_not_equal_to (@2,
4110 wi::max_value (TYPE_PRECISION (type), SIGNED))
4111 /* Let's ignore the @0 -1 and @2 min case. */
4112 : (expr_not_equal_to (@2,
4113 wi::min_value (TYPE_PRECISION (type), SIGNED))
4114 && expr_not_equal_to (@2,
4115 wi::min_value (TYPE_PRECISION (type), SIGNED)
4118 (mult (plusminus { build_one_cst (type); } @2) @0)))
4120 (plusminus (mult:c@3 @0 @2) @0)
4121 (if ((!ANY_INTEGRAL_TYPE_P (type)
4122 || TYPE_OVERFLOW_WRAPS (type)
4123 /* For @0*@2 + @0 this transformation would introduce UB
4124 (where there was none before) for @0 in [-1,0] and @2 max.
4125 For @0*@2 - @0 this transformation would introduce UB
4126 for @0 0 and @2 min. */
4127 || (INTEGRAL_TYPE_P (type)
4128 && ((tree_expr_nonzero_p (@0)
4129 && (plusminus == MINUS_EXPR
4130 || expr_not_equal_to (@0,
4131 wi::minus_one (TYPE_PRECISION (type)))))
4132 || expr_not_equal_to (@2,
4133 (plusminus == PLUS_EXPR
4134 ? wi::max_value (TYPE_PRECISION (type), SIGNED)
4135 : wi::min_value (TYPE_PRECISION (type), SIGNED))))))
4137 (mult (plusminus @2 { build_one_cst (type); }) @0))))))
4140 /* Canonicalize X + (X << C) into X * (1 + (1 << C)) and
4141 (X << C1) + (X << C2) into X * ((1 << C1) + (1 << C2)). */
4143 (plus:c @0 (lshift:s @0 INTEGER_CST@1))
4144 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
4145 && tree_fits_uhwi_p (@1)
4146 && tree_to_uhwi (@1) < element_precision (type)
4147 && (INTEGRAL_TYPE_P (TREE_TYPE (@0))
4148 || optab_handler (smul_optab,
4149 TYPE_MODE (type)) != CODE_FOR_nothing))
4150 (with { tree t = type;
4151 if (!TYPE_OVERFLOW_WRAPS (t)) t = unsigned_type_for (t);
4152 wide_int w = wi::set_bit_in_zero (tree_to_uhwi (@1),
4153 element_precision (type));
4155 tree cst = wide_int_to_tree (VECTOR_TYPE_P (t) ? TREE_TYPE (t)
4157 cst = build_uniform_cst (t, cst); }
4158 (convert (mult (convert:t @0) { cst; })))))
4160 (plus (lshift:s @0 INTEGER_CST@1) (lshift:s @0 INTEGER_CST@2))
4161 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
4162 && tree_fits_uhwi_p (@1)
4163 && tree_to_uhwi (@1) < element_precision (type)
4164 && tree_fits_uhwi_p (@2)
4165 && tree_to_uhwi (@2) < element_precision (type)
4166 && (INTEGRAL_TYPE_P (TREE_TYPE (@0))
4167 || optab_handler (smul_optab,
4168 TYPE_MODE (type)) != CODE_FOR_nothing))
4169 (with { tree t = type;
4170 if (!TYPE_OVERFLOW_WRAPS (t)) t = unsigned_type_for (t);
4171 unsigned int prec = element_precision (type);
4172 wide_int w = wi::set_bit_in_zero (tree_to_uhwi (@1), prec);
4173 w += wi::set_bit_in_zero (tree_to_uhwi (@2), prec);
4174 tree cst = wide_int_to_tree (VECTOR_TYPE_P (t) ? TREE_TYPE (t)
4176 cst = build_uniform_cst (t, cst); }
4177 (convert (mult (convert:t @0) { cst; })))))
4180 /* Canonicalize (X*C1)|(X*C2) and (X*C1)^(X*C2) to (C1+C2)*X when
4181 tree_nonzero_bits allows IOR and XOR to be treated like PLUS.
4182 Likewise, handle (X<<C3) and X as legitimate variants of X*C. */
4183 (for op (bit_ior bit_xor)
4185 (op (nop_convert?:s@6 (mult:s@0 (nop_convert? @1) INTEGER_CST@2))
4186 (nop_convert?:s@7 (mult:s@3 (nop_convert? @5) INTEGER_CST@4)))
4187 (if (INTEGRAL_TYPE_P (type)
4188 && operand_equal_p (@1, @5, 0)
4189 && (tree_nonzero_bits (@6) & tree_nonzero_bits (@7)) == 0)
4190 (with { tree t = type;
4191 if (!TYPE_OVERFLOW_WRAPS (t))
4192 t = unsigned_type_for (t);
4193 wide_int c = wi::add (wi::to_wide (@2), wi::to_wide (@4)); }
4194 (convert (mult:t (convert:t @1) { wide_int_to_tree (t, c); })))))
4196 (op:c (nop_convert?:s@5 (mult:s@0 (nop_convert? @1) INTEGER_CST@2))
4197 (lshift:s@3 @1 INTEGER_CST@4))
4198 (if (INTEGRAL_TYPE_P (type)
4199 && tree_int_cst_sgn (@4) > 0
4200 && (tree_nonzero_bits (@5) & tree_nonzero_bits (@3)) == 0)
4201 (with { tree t = type;
4202 if (!TYPE_OVERFLOW_WRAPS (t))
4203 t = unsigned_type_for (t);
4204 wide_int wone = wi::one (TYPE_PRECISION (type));
4205 wide_int c = wi::add (wi::to_wide (@2),
4206 wi::lshift (wone, wi::to_wide (@4))); }
4207 (convert (mult:t (convert:t @1) { wide_int_to_tree (t, c); })))))
4209 (op:c (nop_convert?:s@3 (mult:s@0 (nop_convert? @1) INTEGER_CST@2))
4211 (if (INTEGRAL_TYPE_P (type)
4212 && (tree_nonzero_bits (@3) & tree_nonzero_bits (@1)) == 0)
4213 (with { tree t = type;
4214 if (!TYPE_OVERFLOW_WRAPS (t))
4215 t = unsigned_type_for (t);
4216 wide_int c = wi::add (wi::to_wide (@2), 1); }
4217 (convert (mult:t (convert:t @1) { wide_int_to_tree (t, c); })))))
4219 (op (lshift:s@0 @1 INTEGER_CST@2)
4220 (lshift:s@3 @1 INTEGER_CST@4))
4221 (if (INTEGRAL_TYPE_P (type)
4222 && tree_int_cst_sgn (@2) > 0
4223 && tree_int_cst_sgn (@4) > 0
4224 && (tree_nonzero_bits (@0) & tree_nonzero_bits (@3)) == 0)
4225 (with { tree t = type;
4226 if (!TYPE_OVERFLOW_WRAPS (t))
4227 t = unsigned_type_for (t);
4228 wide_int wone = wi::one (TYPE_PRECISION (t));
4229 wide_int c = wi::add (wi::lshift (wone, wi::to_wide (@2)),
4230 wi::lshift (wone, wi::to_wide (@4))); }
4231 (convert (mult:t (convert:t @1) { wide_int_to_tree (t,c); })))))
4233 (op:c (lshift:s@0 @1 INTEGER_CST@2)
4235 (if (INTEGRAL_TYPE_P (type)
4236 && tree_int_cst_sgn (@2) > 0
4237 && (tree_nonzero_bits (@0) & tree_nonzero_bits (@1)) == 0)
4238 (with { tree t = type;
4239 if (!TYPE_OVERFLOW_WRAPS (t))
4240 t = unsigned_type_for (t);
4241 wide_int wone = wi::one (TYPE_PRECISION (t));
4242 wide_int c = wi::add (wi::lshift (wone, wi::to_wide (@2)), wone); }
4243 (convert (mult:t (convert:t @1) { wide_int_to_tree (t, c); }))))))
4245 /* Simplifications of MIN_EXPR, MAX_EXPR, fmin() and fmax(). */
4247 (for minmax (min max)
4251 /* max(max(x,y),x) -> max(x,y) */
4253 (minmax:c (minmax:c@2 @0 @1) @0)
4255 /* For fmin() and fmax(), skip folding when both are sNaN. */
4256 (for minmax (FMIN_ALL FMAX_ALL)
4259 (if (!tree_expr_maybe_signaling_nan_p (@0))
4261 /* min(max(x,y),y) -> y. */
4263 (min:c (max:c @0 @1) @1)
4265 /* max(min(x,y),y) -> y. */
4267 (max:c (min:c @0 @1) @1)
4269 /* max(a,-a) -> abs(a). */
4271 (max:c @0 (negate @0))
4272 (if (TREE_CODE (type) != COMPLEX_TYPE
4273 && (! ANY_INTEGRAL_TYPE_P (type)
4274 || TYPE_OVERFLOW_UNDEFINED (type)))
4276 /* min(a,-a) -> -abs(a). */
4278 (min:c @0 (negate @0))
4279 (if (TREE_CODE (type) != COMPLEX_TYPE
4280 && (! ANY_INTEGRAL_TYPE_P (type)
4281 || TYPE_OVERFLOW_UNDEFINED (type)))
4286 (if (INTEGRAL_TYPE_P (type)
4287 && TYPE_MIN_VALUE (type)
4288 && operand_equal_p (@1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
4290 (if (INTEGRAL_TYPE_P (type)
4291 && TYPE_MAX_VALUE (type)
4292 && operand_equal_p (@1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
4297 (if (INTEGRAL_TYPE_P (type)
4298 && TYPE_MAX_VALUE (type)
4299 && operand_equal_p (@1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
4301 (if (INTEGRAL_TYPE_P (type)
4302 && TYPE_MIN_VALUE (type)
4303 && operand_equal_p (@1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
4306 /* max (a, a + CST) -> a + CST where CST is positive. */
4307 /* max (a, a + CST) -> a where CST is negative. */
4309 (max:c @0 (plus@2 @0 INTEGER_CST@1))
4310 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
4311 (if (tree_int_cst_sgn (@1) > 0)
4315 /* min (a, a + CST) -> a where CST is positive. */
4316 /* min (a, a + CST) -> a + CST where CST is negative. */
4318 (min:c @0 (plus@2 @0 INTEGER_CST@1))
4319 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
4320 (if (tree_int_cst_sgn (@1) > 0)
4324 /* Simplify min (&var[off0], &var[off1]) etc. depending on whether
4325 the addresses are known to be less, equal or greater. */
4326 (for minmax (min max)
4329 (minmax (convert1?@2 addr@0) (convert2?@3 addr@1))
4332 poly_int64 off0, off1;
4334 int equal = address_compare (cmp, TREE_TYPE (@2), @0, @1, base0, base1,
4335 off0, off1, GENERIC);
4338 (if (minmax == MIN_EXPR)
4339 (if (known_le (off0, off1))
4341 (if (known_gt (off0, off1))
4343 (if (known_ge (off0, off1))
4345 (if (known_lt (off0, off1))
4348 /* (convert (minmax ((convert (x) c)))) -> minmax (x c) if x is promoted
4349 and the outer convert demotes the expression back to x's type. */
4350 (for minmax (min max)
4352 (convert (minmax@0 (convert @1) INTEGER_CST@2))
4353 (if (INTEGRAL_TYPE_P (type)
4354 && types_match (@1, type) && int_fits_type_p (@2, type)
4355 && TYPE_SIGN (TREE_TYPE (@0)) == TYPE_SIGN (type)
4356 && TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type))
4357 (minmax @1 (convert @2)))))
4359 (for minmax (FMIN_ALL FMAX_ALL)
4360 /* If either argument is NaN and other one is not sNaN, return the other
4361 one. Avoid the transformation if we get (and honor) a signalling NaN. */
4363 (minmax:c @0 REAL_CST@1)
4364 (if (real_isnan (TREE_REAL_CST_PTR (@1))
4365 && (!HONOR_SNANS (@1) || !TREE_REAL_CST (@1).signalling)
4366 && !tree_expr_maybe_signaling_nan_p (@0))
4368 /* Convert fmin/fmax to MIN_EXPR/MAX_EXPR. C99 requires these
4369 functions to return the numeric arg if the other one is NaN.
4370 MIN and MAX don't honor that, so only transform if -ffinite-math-only
4371 is set. C99 doesn't require -0.0 to be handled, so we don't have to
4372 worry about it either. */
4373 (if (flag_finite_math_only)
4380 /* min (-A, -B) -> -max (A, B) */
4381 (for minmax (min max FMIN_ALL FMAX_ALL)
4382 maxmin (max min FMAX_ALL FMIN_ALL)
4384 (minmax (negate:s@2 @0) (negate:s@3 @1))
4385 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
4386 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
4387 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
4388 (negate (maxmin @0 @1)))))
4389 /* MIN (~X, ~Y) -> ~MAX (X, Y)
4390 MAX (~X, ~Y) -> ~MIN (X, Y) */
4391 (for minmax (min max)
4394 (minmax (bit_not:s@2 @0) (bit_not:s@3 @1))
4395 (bit_not (maxmin @0 @1)))
4396 /* ~MAX(~X, Y) --> MIN(X, ~Y) */
4397 /* ~MIN(~X, Y) --> MAX(X, ~Y) */
4399 (bit_not (minmax:cs (bit_not @0) @1))
4400 (maxmin @0 (bit_not @1))))
4402 /* MIN (X, Y) == X -> X <= Y */
4403 /* MIN (X, Y) < X -> X > Y */
4404 /* MIN (X, Y) >= X -> X <= Y */
4405 (for minmax (min min min min max max max max)
4406 cmp (eq ne lt ge eq ne gt le )
4407 out (le gt gt le ge lt lt ge )
4409 (cmp:c (minmax:c @0 @1) @0)
4410 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0)))
4412 /* MIN (X, 5) == 0 -> X == 0
4413 MIN (X, 5) == 7 -> false */
4416 (cmp (min @0 INTEGER_CST@1) INTEGER_CST@2)
4417 (if (wi::lt_p (wi::to_wide (@1), wi::to_wide (@2),
4418 TYPE_SIGN (TREE_TYPE (@0))))
4419 { constant_boolean_node (cmp == NE_EXPR, type); }
4420 (if (wi::gt_p (wi::to_wide (@1), wi::to_wide (@2),
4421 TYPE_SIGN (TREE_TYPE (@0))))
4425 (cmp (max @0 INTEGER_CST@1) INTEGER_CST@2)
4426 (if (wi::gt_p (wi::to_wide (@1), wi::to_wide (@2),
4427 TYPE_SIGN (TREE_TYPE (@0))))
4428 { constant_boolean_node (cmp == NE_EXPR, type); }
4429 (if (wi::lt_p (wi::to_wide (@1), wi::to_wide (@2),
4430 TYPE_SIGN (TREE_TYPE (@0))))
4433 /* X <= MAX(X, Y) -> true
4434 X > MAX(X, Y) -> false
4435 X >= MIN(X, Y) -> true
4436 X < MIN(X, Y) -> false */
4437 (for minmax (min min max max )
4440 (cmp:c @0 (minmax:c @0 @1))
4441 { constant_boolean_node (cmp == GE_EXPR || cmp == LE_EXPR, type); } ))
4443 /* MIN (X, C1) < C2 -> X < C2 || C1 < C2 */
4444 (for minmax (min min max max min min max max )
4445 cmp (lt le gt ge gt ge lt le )
4446 comb (bit_ior bit_ior bit_ior bit_ior bit_and bit_and bit_and bit_and)
4448 (cmp (minmax @0 INTEGER_CST@1) INTEGER_CST@2)
4449 (comb (cmp @0 @2) (cmp @1 @2))))
4451 /* Undo fancy ways of writing max/min or other ?: expressions, like
4452 a - ((a - b) & -(a < b)) and a - (a - b) * (a < b) into (a < b) ? b : a.
4453 People normally use ?: and that is what we actually try to optimize. */
4454 /* Transform A + (B-A)*cmp into cmp ? B : A. */
4456 (plus:c @0 (mult:c (minus @1 @0) zero_one_valued_p@2))
4457 (if (INTEGRAL_TYPE_P (type)
4458 && (GIMPLE || !TREE_SIDE_EFFECTS (@1)))
4459 (cond (convert:boolean_type_node @2) @1 @0)))
4460 /* Transform A - (A-B)*cmp into cmp ? B : A. */
4462 (minus @0 (mult:c (minus @0 @1) zero_one_valued_p@2))
4463 (if (INTEGRAL_TYPE_P (type)
4464 && (GIMPLE || !TREE_SIDE_EFFECTS (@1)))
4465 (cond (convert:boolean_type_node @2) @1 @0)))
4466 /* Transform A ^ (A^B)*cmp into cmp ? B : A. */
4468 (bit_xor:c @0 (mult:c (bit_xor:c @0 @1) zero_one_valued_p@2))
4469 (if (INTEGRAL_TYPE_P (type)
4470 && (GIMPLE || !TREE_SIDE_EFFECTS (@1)))
4471 (cond (convert:boolean_type_node @2) @1 @0)))
4473 /* (x <= 0 ? -x : 0) -> max(-x, 0). */
4475 (cond (le @0 integer_zerop@1) (negate@2 @0) integer_zerop@1)
4476 (if (ANY_INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_UNDEFINED (type))
4479 /* (zero_one == 0) ? y : z <op> y -> ((typeof(y))zero_one * z) <op> y */
4480 (for op (bit_xor bit_ior plus)
4482 (cond (eq zero_one_valued_p@0
4486 (if (INTEGRAL_TYPE_P (type)
4487 && TYPE_PRECISION (type) > 1
4488 && (INTEGRAL_TYPE_P (TREE_TYPE (@0))))
4489 (op (mult (convert:type @0) @2) @1))))
4491 /* (zero_one != 0) ? z <op> y : y -> ((typeof(y))zero_one * z) <op> y */
4492 (for op (bit_xor bit_ior plus)
4494 (cond (ne zero_one_valued_p@0
4498 (if (INTEGRAL_TYPE_P (type)
4499 && TYPE_PRECISION (type) > 1
4500 && (INTEGRAL_TYPE_P (TREE_TYPE (@0))))
4501 (op (mult (convert:type @0) @2) @1))))
4503 /* ?: Value replacement. */
4504 /* a == 0 ? b : b + a -> b + a */
4505 (for op (plus bit_ior bit_xor)
4507 (cond (eq @0 integer_zerop) @1 (op:c@2 @1 @0))
4509 /* a == 0 ? b : b - a -> b - a */
4510 /* a == 0 ? b : b ptr+ a -> b ptr+ a */
4511 /* a == 0 ? b : b shift/rotate a -> b shift/rotate a */
4512 (for op (lrotate rrotate lshift rshift minus pointer_plus)
4514 (cond (eq @0 integer_zerop) @1 (op@2 @1 @0))
4517 /* a == 1 ? b : b / a -> b / a */
4518 (for op (trunc_div ceil_div floor_div round_div exact_div)
4520 (cond (eq @0 integer_onep) @1 (op@2 @1 @0))
4523 /* a == 1 ? b : a * b -> a * b */
4526 (cond (eq @0 integer_onep) @1 (op:c@2 @1 @0))
4529 /* a == -1 ? b : a & b -> a & b */
4532 (cond (eq @0 integer_all_onesp) @1 (op:c@2 @1 @0))
4535 /* a != 0 ? a / b : 0 -> a / b iff b is nonzero. */
4536 (for op (trunc_div ceil_div floor_div round_div exact_div)
4538 (cond (ne @0 integer_zerop) (op@2 @3 @1) integer_zerop )
4539 (if (bitwise_equal_p (@0, @3)
4540 && tree_expr_nonzero_p (@1))
4543 /* Note we prefer the != case here
4544 as (a != 0) * (a * b) will generate that version. */
4545 /* a != 0 ? a * b : 0 -> a * b */
4546 /* a != 0 ? a & b : 0 -> a & b */
4547 (for op (mult bit_and)
4549 (cond (ne @0 integer_zerop) (op:c@2 @1 @3) integer_zerop)
4550 (if (bitwise_equal_p (@0, @3))
4553 /* Simplifications of shift and rotates. */
4555 (for rotate (lrotate rrotate)
4557 (rotate integer_all_onesp@0 @1)
4560 /* Optimize -1 >> x for arithmetic right shifts. */
4562 (rshift integer_all_onesp@0 @1)
4563 (if (!TYPE_UNSIGNED (type))
4566 /* Optimize (x >> c) << c into x & (-1<<c). */
4568 (lshift (nop_convert? (rshift @0 INTEGER_CST@1)) @1)
4569 (if (wi::ltu_p (wi::to_wide (@1), element_precision (type)))
4570 /* It doesn't matter if the right shift is arithmetic or logical. */
4571 (bit_and (view_convert @0) (lshift { build_minus_one_cst (type); } @1))))
4574 (lshift (convert (convert@2 (rshift @0 INTEGER_CST@1))) @1)
4575 (if (wi::ltu_p (wi::to_wide (@1), element_precision (type))
4576 /* Allow intermediate conversion to integral type with whatever sign, as
4577 long as the low TYPE_PRECISION (type)
4578 - TYPE_PRECISION (TREE_TYPE (@2)) bits are preserved. */
4579 && INTEGRAL_TYPE_P (type)
4580 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
4581 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
4582 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (@0))
4583 && (TYPE_PRECISION (TREE_TYPE (@2)) >= TYPE_PRECISION (type)
4584 || wi::geu_p (wi::to_wide (@1),
4585 TYPE_PRECISION (type)
4586 - TYPE_PRECISION (TREE_TYPE (@2)))))
4587 (bit_and (convert @0) (lshift { build_minus_one_cst (type); } @1))))
4589 /* For (x << c) >> c, optimize into x & ((unsigned)-1 >> c) for
4590 unsigned x OR truncate into the precision(type) - c lowest bits
4591 of signed x (if they have mode precision or a precision of 1). */
4593 (rshift (nop_convert? (lshift @0 INTEGER_CST@1)) @@1)
4594 (if (wi::ltu_p (wi::to_wide (@1), element_precision (type)))
4595 (if (TYPE_UNSIGNED (type))
4596 (bit_and (convert @0) (rshift { build_minus_one_cst (type); } @1))
4597 (if (INTEGRAL_TYPE_P (type))
4599 int width = element_precision (type) - tree_to_uhwi (@1);
4600 tree stype = NULL_TREE;
4601 if (width <= MAX_FIXED_MODE_SIZE)
4602 stype = build_nonstandard_integer_type (width, 0);
4604 (if (stype && (width == 1 || type_has_mode_precision_p (stype)))
4605 (convert (convert:stype @0))))))))
4607 /* Optimize x >> x into 0 */
4610 { build_zero_cst (type); })
4612 (for shiftrotate (lrotate rrotate lshift rshift)
4614 (shiftrotate @0 integer_zerop)
4617 (shiftrotate integer_zerop@0 @1)
4619 /* Prefer vector1 << scalar to vector1 << vector2
4620 if vector2 is uniform. */
4621 (for vec (VECTOR_CST CONSTRUCTOR)
4623 (shiftrotate @0 vec@1)
4624 (with { tree tem = uniform_vector_p (@1); }
4626 (shiftrotate @0 { tem; }))))))
4628 /* Simplify X << Y where Y's low width bits are 0 to X, as only valid
4629 Y is 0. Similarly for X >> Y. */
4631 (for shift (lshift rshift)
4633 (shift @0 SSA_NAME@1)
4634 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1)))
4636 int width = ceil_log2 (element_precision (TREE_TYPE (@0)));
4637 int prec = TYPE_PRECISION (TREE_TYPE (@1));
4639 (if ((get_nonzero_bits (@1) & wi::mask (width, false, prec)) == 0)
4643 /* Rewrite an LROTATE_EXPR by a constant into an
4644 RROTATE_EXPR by a new constant. */
4646 (lrotate @0 INTEGER_CST@1)
4647 (rrotate @0 { const_binop (MINUS_EXPR, TREE_TYPE (@1),
4648 build_int_cst (TREE_TYPE (@1),
4649 element_precision (type)), @1); }))
4651 /* Turn (a OP c1) OP c2 into a OP (c1+c2). */
4652 (for op (lrotate rrotate rshift lshift)
4654 (op (op @0 INTEGER_CST@1) INTEGER_CST@2)
4655 (with { unsigned int prec = element_precision (type); }
4656 (if (wi::ge_p (wi::to_wide (@1), 0, TYPE_SIGN (TREE_TYPE (@1)))
4657 && wi::lt_p (wi::to_wide (@1), prec, TYPE_SIGN (TREE_TYPE (@1)))
4658 && wi::ge_p (wi::to_wide (@2), 0, TYPE_SIGN (TREE_TYPE (@2)))
4659 && wi::lt_p (wi::to_wide (@2), prec, TYPE_SIGN (TREE_TYPE (@2))))
4660 (with { unsigned int low = (tree_to_uhwi (@1)
4661 + tree_to_uhwi (@2)); }
4662 /* Deal with a OP (c1 + c2) being undefined but (a OP c1) OP c2
4663 being well defined. */
4665 (if (op == LROTATE_EXPR || op == RROTATE_EXPR)
4666 (op @0 { build_int_cst (TREE_TYPE (@1), low % prec); })
4667 (if (TYPE_UNSIGNED (type) || op == LSHIFT_EXPR)
4668 { build_zero_cst (type); }
4669 (op @0 { build_int_cst (TREE_TYPE (@1), prec - 1); })))
4670 (op @0 { build_int_cst (TREE_TYPE (@1), low); })))))))
4673 /* Simplify (CST << x) & 1 to 0 if CST is even or to x == 0 if it is odd. */
4675 (bit_and (lshift INTEGER_CST@1 @0) integer_onep)
4676 (if ((wi::to_wide (@1) & 1) != 0)
4677 (convert (eq:boolean_type_node @0 { build_zero_cst (TREE_TYPE (@0)); }))
4678 { build_zero_cst (type); }))
4680 /* Simplify ((C << x) & D) != 0 where C and D are power of two constants,
4681 either to false if D is smaller (unsigned comparison) than C, or to
4682 x == log2 (D) - log2 (C). Similarly for right shifts.
4683 Note for `(1 >> x)`, the & 1 has been removed so matching that seperately. */
4687 (cmp (bit_and (lshift integer_pow2p@1 @0) integer_pow2p@2) integer_zerop)
4688 (with { int c1 = wi::clz (wi::to_wide (@1));
4689 int c2 = wi::clz (wi::to_wide (@2)); }
4691 { constant_boolean_node (cmp == NE_EXPR ? false : true, type); }
4692 (icmp @0 { build_int_cst (TREE_TYPE (@0), c1 - c2); }))))
4694 (cmp (bit_and (rshift integer_pow2p@1 @0) integer_pow2p@2) integer_zerop)
4695 (if (tree_int_cst_sgn (@1) > 0)
4696 (with { int c1 = wi::clz (wi::to_wide (@1));
4697 int c2 = wi::clz (wi::to_wide (@2)); }
4699 { constant_boolean_node (cmp == NE_EXPR ? false : true, type); }
4700 (icmp @0 { build_int_cst (TREE_TYPE (@0), c2 - c1); })))))
4701 /* `(1 >> X) != 0` -> `X == 0` */
4702 /* `(1 >> X) == 0` -> `X != 0` */
4704 (cmp (rshift integer_onep@1 @0) integer_zerop)
4705 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1)))
4706 (icmp @0 { build_zero_cst (TREE_TYPE (@0)); }))))
4708 /* (CST1 << A) == CST2 -> A == ctz (CST2) - ctz (CST1)
4709 (CST1 << A) != CST2 -> A != ctz (CST2) - ctz (CST1)
4713 (cmp (lshift INTEGER_CST@0 @1) INTEGER_CST@2)
4714 (with { int cand = wi::ctz (wi::to_wide (@2)) - wi::ctz (wi::to_wide (@0)); }
4716 || (!integer_zerop (@2)
4717 && wi::lshift (wi::to_wide (@0), cand) != wi::to_wide (@2)))
4718 { constant_boolean_node (cmp == NE_EXPR, type); }
4719 (if (!integer_zerop (@2)
4720 && wi::lshift (wi::to_wide (@0), cand) == wi::to_wide (@2))
4721 (cmp @1 { build_int_cst (TREE_TYPE (@1), cand); }))))))
4723 /* Fold ((X << C1) & C2) cmp C3 into (X & (C2 >> C1)) cmp (C3 >> C1)
4724 ((X >> C1) & C2) cmp C3 into (X & (C2 << C1)) cmp (C3 << C1). */
4727 (cmp (bit_and:s (lshift:s @0 INTEGER_CST@1) INTEGER_CST@2) INTEGER_CST@3)
4728 (if (tree_fits_shwi_p (@1)
4729 && tree_to_shwi (@1) > 0
4730 && tree_to_shwi (@1) < TYPE_PRECISION (TREE_TYPE (@0)))
4731 (if (tree_to_shwi (@1) > wi::ctz (wi::to_wide (@3)))
4732 { constant_boolean_node (cmp == NE_EXPR, type); }
4733 (with { wide_int c1 = wi::to_wide (@1);
4734 wide_int c2 = wi::lrshift (wi::to_wide (@2), c1);
4735 wide_int c3 = wi::lrshift (wi::to_wide (@3), c1); }
4736 (cmp (bit_and @0 { wide_int_to_tree (TREE_TYPE (@0), c2); })
4737 { wide_int_to_tree (TREE_TYPE (@0), c3); })))))
4739 (cmp (bit_and:s (rshift:s @0 INTEGER_CST@1) INTEGER_CST@2) INTEGER_CST@3)
4740 (if (tree_fits_shwi_p (@1)
4741 && tree_to_shwi (@1) > 0
4742 && tree_to_shwi (@1) < TYPE_PRECISION (TREE_TYPE (@0)))
4743 (with { tree t0 = TREE_TYPE (@0);
4744 unsigned int prec = TYPE_PRECISION (t0);
4745 wide_int c1 = wi::to_wide (@1);
4746 wide_int c2 = wi::to_wide (@2);
4747 wide_int c3 = wi::to_wide (@3);
4748 wide_int sb = wi::set_bit_in_zero (prec - 1, prec); }
4749 (if ((c2 & c3) != c3)
4750 { constant_boolean_node (cmp == NE_EXPR, type); }
4751 (if (TYPE_UNSIGNED (t0))
4752 (if ((c3 & wi::arshift (sb, c1 - 1)) != 0)
4753 { constant_boolean_node (cmp == NE_EXPR, type); }
4754 (cmp (bit_and @0 { wide_int_to_tree (t0, c2 << c1); })
4755 { wide_int_to_tree (t0, c3 << c1); }))
4756 (with { wide_int smask = wi::arshift (sb, c1); }
4758 (if ((c2 & smask) == 0)
4759 (cmp (bit_and @0 { wide_int_to_tree (t0, c2 << c1); })
4760 { wide_int_to_tree (t0, c3 << c1); }))
4761 (if ((c3 & smask) == 0)
4762 (cmp (bit_and @0 { wide_int_to_tree (t0, (c2 << c1) | sb); })
4763 { wide_int_to_tree (t0, c3 << c1); }))
4764 (if ((c2 & smask) != (c3 & smask))
4765 { constant_boolean_node (cmp == NE_EXPR, type); })
4766 (cmp (bit_and @0 { wide_int_to_tree (t0, (c2 << c1) | sb); })
4767 { wide_int_to_tree (t0, (c3 << c1) | sb); })))))))))
4769 /* Fold (X << C1) & C2 into (X << C1) & (C2 | ((1 << C1) - 1))
4770 (X >> C1) & C2 into (X >> C1) & (C2 | ~((type) -1 >> C1))
4771 if the new mask might be further optimized. */
4772 (for shift (lshift rshift)
4774 (bit_and (convert?:s@4 (shift:s@5 (convert1?@3 @0) INTEGER_CST@1))
4776 (if (tree_nop_conversion_p (TREE_TYPE (@4), TREE_TYPE (@5))
4777 && TYPE_PRECISION (type) <= HOST_BITS_PER_WIDE_INT
4778 && tree_fits_uhwi_p (@1)
4779 && tree_to_uhwi (@1) > 0
4780 && tree_to_uhwi (@1) < TYPE_PRECISION (type))
4783 unsigned int shiftc = tree_to_uhwi (@1);
4784 unsigned HOST_WIDE_INT mask = TREE_INT_CST_LOW (@2);
4785 unsigned HOST_WIDE_INT newmask, zerobits = 0;
4786 tree shift_type = TREE_TYPE (@3);
4789 if (shift == LSHIFT_EXPR)
4790 zerobits = ((HOST_WIDE_INT_1U << shiftc) - 1);
4791 else if (shift == RSHIFT_EXPR
4792 && type_has_mode_precision_p (shift_type))
4794 prec = TYPE_PRECISION (TREE_TYPE (@3));
4796 /* See if more bits can be proven as zero because of
4799 && TYPE_UNSIGNED (TREE_TYPE (@0)))
4801 tree inner_type = TREE_TYPE (@0);
4802 if (type_has_mode_precision_p (inner_type)
4803 && TYPE_PRECISION (inner_type) < prec)
4805 prec = TYPE_PRECISION (inner_type);
4806 /* See if we can shorten the right shift. */
4808 shift_type = inner_type;
4809 /* Otherwise X >> C1 is all zeros, so we'll optimize
4810 it into (X, 0) later on by making sure zerobits
4814 zerobits = HOST_WIDE_INT_M1U;
4817 zerobits >>= HOST_BITS_PER_WIDE_INT - shiftc;
4818 zerobits <<= prec - shiftc;
4820 /* For arithmetic shift if sign bit could be set, zerobits
4821 can contain actually sign bits, so no transformation is
4822 possible, unless MASK masks them all away. In that
4823 case the shift needs to be converted into logical shift. */
4824 if (!TYPE_UNSIGNED (TREE_TYPE (@3))
4825 && prec == TYPE_PRECISION (TREE_TYPE (@3)))
4827 if ((mask & zerobits) == 0)
4828 shift_type = unsigned_type_for (TREE_TYPE (@3));
4834 /* ((X << 16) & 0xff00) is (X, 0). */
4835 (if ((mask & zerobits) == mask)
4836 { build_int_cst (type, 0); }
4837 (with { newmask = mask | zerobits; }
4838 (if (newmask != mask && (newmask & (newmask + 1)) == 0)
4841 /* Only do the transformation if NEWMASK is some integer
4843 for (prec = BITS_PER_UNIT;
4844 prec < HOST_BITS_PER_WIDE_INT; prec <<= 1)
4845 if (newmask == (HOST_WIDE_INT_1U << prec) - 1)
4848 (if (prec < HOST_BITS_PER_WIDE_INT
4849 || newmask == HOST_WIDE_INT_M1U)
4851 { tree newmaskt = build_int_cst_type (TREE_TYPE (@2), newmask); }
4852 (if (!tree_int_cst_equal (newmaskt, @2))
4853 (if (shift_type != TREE_TYPE (@3))
4854 (bit_and (convert (shift:shift_type (convert @3) @1)) { newmaskt; })
4855 (bit_and @4 { newmaskt; })))))))))))))
4857 /* ((1 << n) & M) != 0 -> n == log2 (M) */
4863 (nop_convert? (lshift integer_onep @0)) integer_pow2p@1) integer_zerop)
4864 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)))
4865 (icmp @0 { wide_int_to_tree (TREE_TYPE (@0),
4866 wi::exact_log2 (wi::to_wide (@1))); }))))
4868 /* Fold (X {&,^,|} C2) << C1 into (X << C1) {&,^,|} (C2 << C1)
4869 (X {&,^,|} C2) >> C1 into (X >> C1) & (C2 >> C1). */
4870 (for shift (lshift rshift)
4871 (for bit_op (bit_and bit_xor bit_ior)
4873 (shift (convert?:s (bit_op:s @0 INTEGER_CST@2)) INTEGER_CST@1)
4874 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
4875 (with { tree mask = int_const_binop (shift, fold_convert (type, @2), @1); }
4877 (bit_op (shift (convert @0) @1) { mask; })))))))
4879 /* ~(~X >> Y) -> X >> Y (for arithmetic shift). */
4881 (bit_not (convert1?:s (rshift:s (convert2?@0 (bit_not @1)) @2)))
4882 (if (!TYPE_UNSIGNED (TREE_TYPE (@0))
4883 && (element_precision (TREE_TYPE (@0))
4884 <= element_precision (TREE_TYPE (@1))
4885 || !TYPE_UNSIGNED (TREE_TYPE (@1))))
4887 { tree shift_type = TREE_TYPE (@0); }
4888 (convert (rshift (convert:shift_type @1) @2)))))
4890 /* ~(~X >>r Y) -> X >>r Y
4891 ~(~X <<r Y) -> X <<r Y */
4892 (for rotate (lrotate rrotate)
4894 (bit_not (convert1?:s (rotate:s (convert2?@0 (bit_not @1)) @2)))
4895 (if ((element_precision (TREE_TYPE (@0))
4896 <= element_precision (TREE_TYPE (@1))
4897 || !TYPE_UNSIGNED (TREE_TYPE (@1)))
4898 && (element_precision (type) <= element_precision (TREE_TYPE (@0))
4899 || !TYPE_UNSIGNED (TREE_TYPE (@0))))
4901 { tree rotate_type = TREE_TYPE (@0); }
4902 (convert (rotate (convert:rotate_type @1) @2))))))
4905 (for rotate (lrotate rrotate)
4906 invrot (rrotate lrotate)
4907 /* (X >>r Y) cmp (Z >>r Y) may simplify to X cmp Y. */
4909 (cmp (rotate @1 @0) (rotate @2 @0))
4911 /* (X >>r C1) cmp C2 may simplify to X cmp C3. */
4913 (cmp (rotate @0 INTEGER_CST@1) INTEGER_CST@2)
4914 (cmp @0 { const_binop (invrot, TREE_TYPE (@0), @2, @1); }))
4915 /* (X >>r Y) cmp C where C is 0 or ~0, may simplify to X cmp C. */
4917 (cmp (rotate @0 @1) INTEGER_CST@2)
4918 (if (integer_zerop (@2) || integer_all_onesp (@2))
4921 /* Narrow a lshift by constant. */
4923 (convert (lshift:s@0 @1 INTEGER_CST@2))
4924 (if (INTEGRAL_TYPE_P (type)
4925 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
4926 && !integer_zerop (@2)
4927 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0)))
4928 (if (TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (@0))
4929 || wi::ltu_p (wi::to_wide (@2), TYPE_PRECISION (type)))
4930 (lshift (convert @1) @2)
4931 (if (wi::ltu_p (wi::to_wide (@2), TYPE_PRECISION (TREE_TYPE (@0))))
4932 { build_zero_cst (type); }))))
4934 /* Simplifications of conversions. */
4936 /* Basic strip-useless-type-conversions / strip_nops. */
4937 (for cvt (convert view_convert float fix_trunc)
4940 (if ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@0)))
4941 || (GENERIC && type == TREE_TYPE (@0)))
4944 /* Contract view-conversions. */
4946 (view_convert (view_convert @0))
4949 /* For integral conversions with the same precision or pointer
4950 conversions use a NOP_EXPR instead. */
4953 (if ((INTEGRAL_TYPE_P (type) || POINTER_TYPE_P (type))
4954 && (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
4955 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (@0)))
4958 /* Strip inner integral conversions that do not change precision or size, or
4959 zero-extend while keeping the same size (for bool-to-char). */
4961 (view_convert (convert@0 @1))
4962 (if ((INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
4963 && (INTEGRAL_TYPE_P (TREE_TYPE (@1)) || POINTER_TYPE_P (TREE_TYPE (@1)))
4964 && TYPE_SIZE (TREE_TYPE (@0)) == TYPE_SIZE (TREE_TYPE (@1))
4965 && (TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1))
4966 || (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (TREE_TYPE (@1))
4967 && TYPE_UNSIGNED (TREE_TYPE (@1)))))
4970 /* Simplify a view-converted empty or single-element constructor. */
4972 (view_convert CONSTRUCTOR@0)
4974 { tree ctor = (TREE_CODE (@0) == SSA_NAME
4975 ? gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0)) : @0); }
4977 (if (CONSTRUCTOR_NELTS (ctor) == 0)
4978 { build_zero_cst (type); })
4979 (if (CONSTRUCTOR_NELTS (ctor) == 1
4980 && VECTOR_TYPE_P (TREE_TYPE (ctor))
4981 && operand_equal_p (TYPE_SIZE (type),
4982 TYPE_SIZE (TREE_TYPE
4983 (CONSTRUCTOR_ELT (ctor, 0)->value))))
4984 (view_convert { CONSTRUCTOR_ELT (ctor, 0)->value; })))))
4986 /* Re-association barriers around constants and other re-association
4987 barriers can be removed. */
4989 (paren CONSTANT_CLASS_P@0)
4992 (paren (paren@1 @0))
4995 /* Handle cases of two conversions in a row. */
4996 (for ocvt (convert float fix_trunc)
4997 (for icvt (convert float)
5002 tree inside_type = TREE_TYPE (@0);
5003 tree inter_type = TREE_TYPE (@1);
5004 int inside_int = INTEGRAL_TYPE_P (inside_type);
5005 int inside_ptr = POINTER_TYPE_P (inside_type);
5006 int inside_float = FLOAT_TYPE_P (inside_type);
5007 int inside_vec = VECTOR_TYPE_P (inside_type);
5008 unsigned int inside_prec = element_precision (inside_type);
5009 int inside_unsignedp = TYPE_UNSIGNED (inside_type);
5010 int inter_int = INTEGRAL_TYPE_P (inter_type);
5011 int inter_ptr = POINTER_TYPE_P (inter_type);
5012 int inter_float = FLOAT_TYPE_P (inter_type);
5013 int inter_vec = VECTOR_TYPE_P (inter_type);
5014 unsigned int inter_prec = element_precision (inter_type);
5015 int inter_unsignedp = TYPE_UNSIGNED (inter_type);
5016 int final_int = INTEGRAL_TYPE_P (type);
5017 int final_ptr = POINTER_TYPE_P (type);
5018 int final_float = FLOAT_TYPE_P (type);
5019 int final_vec = VECTOR_TYPE_P (type);
5020 unsigned int final_prec = element_precision (type);
5021 int final_unsignedp = TYPE_UNSIGNED (type);
5024 /* In addition to the cases of two conversions in a row
5025 handled below, if we are converting something to its own
5026 type via an object of identical or wider precision, neither
5027 conversion is needed. */
5028 (if (((GIMPLE && useless_type_conversion_p (type, inside_type))
5030 && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (inside_type)))
5031 && (((inter_int || inter_ptr) && final_int)
5032 || (inter_float && final_float))
5033 && inter_prec >= final_prec)
5036 /* Likewise, if the intermediate and initial types are either both
5037 float or both integer, we don't need the middle conversion if the
5038 former is wider than the latter and doesn't change the signedness
5039 (for integers). Avoid this if the final type is a pointer since
5040 then we sometimes need the middle conversion. */
5041 (if (((inter_int && inside_int) || (inter_float && inside_float))
5042 && (final_int || final_float)
5043 && inter_prec >= inside_prec
5044 && (inter_float || inter_unsignedp == inside_unsignedp))
5047 /* If we have a sign-extension of a zero-extended value, we can
5048 replace that by a single zero-extension. Likewise if the
5049 final conversion does not change precision we can drop the
5050 intermediate conversion. Similarly truncation of a sign-extension
5051 can be replaced by a single sign-extension. */
5052 (if (inside_int && inter_int && final_int
5053 && ((inside_prec < inter_prec && inter_prec < final_prec
5054 && inside_unsignedp && !inter_unsignedp)
5055 || final_prec == inter_prec
5056 || (inside_prec < inter_prec && inter_prec > final_prec
5057 && !inside_unsignedp && inter_unsignedp)))
5060 /* Two conversions in a row are not needed unless:
5061 - some conversion is floating-point (overstrict for now), or
5062 - some conversion is a vector (overstrict for now), or
5063 - the intermediate type is narrower than both initial and
5065 - the intermediate type and innermost type differ in signedness,
5066 and the outermost type is wider than the intermediate, or
5067 - the initial type is a pointer type and the precisions of the
5068 intermediate and final types differ, or
5069 - the final type is a pointer type and the precisions of the
5070 initial and intermediate types differ. */
5071 (if (! inside_float && ! inter_float && ! final_float
5072 && ! inside_vec && ! inter_vec && ! final_vec
5073 && (inter_prec >= inside_prec || inter_prec >= final_prec)
5074 && ! (inside_int && inter_int
5075 && inter_unsignedp != inside_unsignedp
5076 && inter_prec < final_prec)
5077 && ((inter_unsignedp && inter_prec > inside_prec)
5078 == (final_unsignedp && final_prec > inter_prec))
5079 && ! (inside_ptr && inter_prec != final_prec)
5080 && ! (final_ptr && inside_prec != inter_prec))
5083 /* `(outer:M)(inter:N) a:O`
5084 can be converted to `(outer:M) a`
5085 if M <= O && N >= O. No matter what signedness of the casts,
5086 as the final is either a truncation from the original or just
5087 a sign change of the type. */
5088 (if (inside_int && inter_int && final_int
5089 && final_prec <= inside_prec
5090 && inter_prec >= inside_prec)
5093 /* A truncation to an unsigned type (a zero-extension) should be
5094 canonicalized as bitwise and of a mask. */
5095 (if (GIMPLE /* PR70366: doing this in GENERIC breaks -Wconversion. */
5096 && final_int && inter_int && inside_int
5097 && final_prec == inside_prec
5098 && final_prec > inter_prec
5100 (convert (bit_and @0 { wide_int_to_tree
5102 wi::mask (inter_prec, false,
5103 TYPE_PRECISION (inside_type))); })))
5105 /* If we are converting an integer to a floating-point that can
5106 represent it exactly and back to an integer, we can skip the
5107 floating-point conversion. */
5108 (if (GIMPLE /* PR66211 */
5109 && inside_int && inter_float && final_int &&
5110 (unsigned) significand_size (TYPE_MODE (inter_type))
5111 >= inside_prec - !inside_unsignedp)
5114 /* (float_type)(integer_type) x -> trunc (x) if the type of x matches
5115 float_type. Only do the transformation if we do not need to preserve
5116 trapping behaviour, so require !flag_trapping_math. */
5119 (float (fix_trunc @0))
5120 (if (!flag_trapping_math
5121 && !HONOR_SIGNED_ZEROS (type)
5122 && types_match (type, TREE_TYPE (@0))
5123 && direct_internal_fn_supported_p (IFN_TRUNC, type,
5128 /* If we have a narrowing conversion to an integral type that is fed by a
5129 BIT_AND_EXPR, we might be able to remove the BIT_AND_EXPR if it merely
5130 masks off bits outside the final type (and nothing else). */
5132 (convert (bit_and @0 INTEGER_CST@1))
5133 (if (INTEGRAL_TYPE_P (type)
5134 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
5135 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))
5136 && operand_equal_p (@1, build_low_bits_mask (TREE_TYPE (@1),
5137 TYPE_PRECISION (type)), 0))
5141 /* (X /[ex] A) * A -> X. */
5143 (mult (convert1? (exact_div @0 @@1)) (convert2? @1))
5146 /* Simplify (A / B) * B + (A % B) -> A. */
5147 (for div (trunc_div ceil_div floor_div round_div)
5148 mod (trunc_mod ceil_mod floor_mod round_mod)
5150 (plus:c (mult:c (div @0 @1) @1) (mod @0 @1))
5153 /* x / y * y == x -> x % y == 0. */
5155 (eq:c (mult:c (trunc_div:s @0 @1) @1) @0)
5156 (if (TREE_CODE (TREE_TYPE (@0)) != COMPLEX_TYPE)
5157 (eq (trunc_mod @0 @1) { build_zero_cst (TREE_TYPE (@0)); })))
5159 /* ((X /[ex] A) +- B) * A --> X +- A * B. */
5160 (for op (plus minus)
5162 (mult (convert1? (op (convert2? (exact_div @0 INTEGER_CST@@1)) INTEGER_CST@2)) @1)
5163 (if (tree_nop_conversion_p (type, TREE_TYPE (@2))
5164 && tree_nop_conversion_p (TREE_TYPE (@0), TREE_TYPE (@2)))
5167 wi::overflow_type overflow;
5168 wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@2),
5169 TYPE_SIGN (type), &overflow);
5171 (if (types_match (type, TREE_TYPE (@2))
5172 && types_match (TREE_TYPE (@0), TREE_TYPE (@2)) && !overflow)
5173 (op @0 { wide_int_to_tree (type, mul); })
5174 (with { tree utype = unsigned_type_for (type); }
5175 (convert (op (convert:utype @0)
5176 (mult (convert:utype @1) (convert:utype @2))))))))))
5178 /* Canonicalization of binary operations. */
5180 /* Convert X + -C into X - C. */
5182 (plus @0 REAL_CST@1)
5183 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
5184 (with { tree tem = const_unop (NEGATE_EXPR, type, @1); }
5185 (if (!TREE_OVERFLOW (tem) || !flag_trapping_math)
5186 (minus @0 { tem; })))))
5188 /* Convert x+x into x*2. */
5191 (if (SCALAR_FLOAT_TYPE_P (type))
5192 (mult @0 { build_real (type, dconst2); })
5193 (if (INTEGRAL_TYPE_P (type))
5194 (mult @0 { build_int_cst (type, 2); }))))
5198 (minus integer_zerop @1)
5201 (pointer_diff integer_zerop @1)
5202 (negate (convert @1)))
5204 /* (ARG0 - ARG1) is the same as (-ARG1 + ARG0). So check whether
5205 ARG0 is zero and X + ARG0 reduces to X, since that would mean
5206 (-ARG1 + ARG0) reduces to -ARG1. */
5208 (minus real_zerop@0 @1)
5209 (if (fold_real_zero_addition_p (type, @1, @0, 0))
5212 /* Transform x * -1 into -x. */
5214 (mult @0 integer_minus_onep)
5217 /* Reassociate (X * CST) * Y to (X * Y) * CST. This does not introduce
5218 signed overflow for CST != 0 && CST != -1. */
5220 (mult:c (mult:s@3 @0 INTEGER_CST@1) @2)
5221 (if (TREE_CODE (@2) != INTEGER_CST
5223 && !integer_zerop (@1) && !integer_minus_onep (@1))
5224 (mult (mult @0 @2) @1)))
5226 /* True if we can easily extract the real and imaginary parts of a complex
5228 (match compositional_complex
5229 (convert? (complex @0 @1)))
5231 /* COMPLEX_EXPR and REALPART/IMAGPART_EXPR cancellations. */
5233 (complex (realpart @0) (imagpart @0))
5236 (realpart (complex @0 @1))
5239 (imagpart (complex @0 @1))
5242 /* Sometimes we only care about half of a complex expression. */
5244 (realpart (convert?:s (conj:s @0)))
5245 (convert (realpart @0)))
5247 (imagpart (convert?:s (conj:s @0)))
5248 (convert (negate (imagpart @0))))
5249 (for part (realpart imagpart)
5250 (for op (plus minus)
5252 (part (convert?:s@2 (op:s @0 @1)))
5253 (convert (op (part @0) (part @1))))))
5255 (realpart (convert?:s (CEXPI:s @0)))
5258 (imagpart (convert?:s (CEXPI:s @0)))
5261 /* conj(conj(x)) -> x */
5263 (conj (convert? (conj @0)))
5264 (if (tree_nop_conversion_p (TREE_TYPE (@0), type))
5267 /* conj({x,y}) -> {x,-y} */
5269 (conj (convert?:s (complex:s @0 @1)))
5270 (with { tree itype = TREE_TYPE (type); }
5271 (complex (convert:itype @0) (negate (convert:itype @1)))))
5273 /* BSWAP simplifications, transforms checked by gcc.dg/builtin-bswap-8.c. */
5279 (bswap (bit_not (bswap @0)))
5281 (for bitop (bit_xor bit_ior bit_and)
5283 (bswap (bitop:c (bswap @0) @1))
5284 (bitop @0 (bswap @1))))
5287 (cmp (bswap@2 @0) (bswap @1))
5288 (with { tree ctype = TREE_TYPE (@2); }
5289 (cmp (convert:ctype @0) (convert:ctype @1))))
5291 (cmp (bswap @0) INTEGER_CST@1)
5292 (with { tree ctype = TREE_TYPE (@1); }
5293 (cmp (convert:ctype @0) (bswap! @1)))))
5294 /* (bswap(x) >> C1) & C2 can sometimes be simplified to (x >> C3) & C2. */
5296 (bit_and (convert1? (rshift@0 (convert2? (bswap@4 @1)) INTEGER_CST@2))
5298 (if (BITS_PER_UNIT == 8
5299 && tree_fits_uhwi_p (@2)
5300 && tree_fits_uhwi_p (@3))
5303 unsigned HOST_WIDE_INT prec = TYPE_PRECISION (TREE_TYPE (@4));
5304 unsigned HOST_WIDE_INT bits = tree_to_uhwi (@2);
5305 unsigned HOST_WIDE_INT mask = tree_to_uhwi (@3);
5306 unsigned HOST_WIDE_INT lo = bits & 7;
5307 unsigned HOST_WIDE_INT hi = bits - lo;
5310 && mask < (256u>>lo)
5311 && bits < TYPE_PRECISION (TREE_TYPE(@0)))
5312 (with { unsigned HOST_WIDE_INT ns = (prec - (hi + 8)) + lo; }
5314 (bit_and (convert @1) @3)
5317 tree utype = unsigned_type_for (TREE_TYPE (@1));
5318 tree nst = build_int_cst (integer_type_node, ns);
5320 (bit_and (convert (rshift:utype (convert:utype @1) {nst;})) @3))))))))
5321 /* bswap(x) >> C1 can sometimes be simplified to (T)x >> C2. */
5323 (rshift (convert? (bswap@2 @0)) INTEGER_CST@1)
5324 (if (BITS_PER_UNIT == 8
5325 && CHAR_TYPE_SIZE == 8
5326 && tree_fits_uhwi_p (@1))
5329 unsigned HOST_WIDE_INT prec = TYPE_PRECISION (TREE_TYPE (@2));
5330 unsigned HOST_WIDE_INT bits = tree_to_uhwi (@1);
5331 /* If the bswap was extended before the original shift, this
5332 byte (shift) has the sign of the extension, not the sign of
5333 the original shift. */
5334 tree st = TYPE_PRECISION (type) > prec ? TREE_TYPE (@2) : type;
5336 /* Special case: logical right shift of sign-extended bswap.
5337 (unsigned)(short)bswap16(x)>>12 is (unsigned)((short)x<<8)>>12. */
5338 (if (TYPE_PRECISION (type) > prec
5339 && !TYPE_UNSIGNED (TREE_TYPE (@2))
5340 && TYPE_UNSIGNED (type)
5341 && bits < prec && bits + 8 >= prec)
5342 (with { tree nst = build_int_cst (integer_type_node, prec - 8); }
5343 (rshift (convert (lshift:st (convert:st @0) {nst;})) @1))
5344 (if (bits + 8 == prec)
5345 (if (TYPE_UNSIGNED (st))
5346 (convert (convert:unsigned_char_type_node @0))
5347 (convert (convert:signed_char_type_node @0)))
5348 (if (bits < prec && bits + 8 > prec)
5351 tree nst = build_int_cst (integer_type_node, bits & 7);
5352 tree bt = TYPE_UNSIGNED (st) ? unsigned_char_type_node
5353 : signed_char_type_node;
5355 (convert (rshift:bt (convert:bt @0) {nst;})))))))))
5356 /* bswap(x) & C1 can sometimes be simplified to (x >> C2) & C1. */
5358 (bit_and (convert? (bswap@2 @0)) INTEGER_CST@1)
5359 (if (BITS_PER_UNIT == 8
5360 && tree_fits_uhwi_p (@1)
5361 && tree_to_uhwi (@1) < 256)
5364 unsigned HOST_WIDE_INT prec = TYPE_PRECISION (TREE_TYPE (@2));
5365 tree utype = unsigned_type_for (TREE_TYPE (@0));
5366 tree nst = build_int_cst (integer_type_node, prec - 8);
5368 (bit_and (convert (rshift:utype (convert:utype @0) {nst;})) @1)))))
5371 /* Combine COND_EXPRs and VEC_COND_EXPRs. */
5373 /* Simplify constant conditions.
5374 Only optimize constant conditions when the selected branch
5375 has the same type as the COND_EXPR. This avoids optimizing
5376 away "c ? x : throw", where the throw has a void type.
5377 Note that we cannot throw away the fold-const.cc variant nor
5378 this one as we depend on doing this transform before possibly
5379 A ? B : B -> B triggers and the fold-const.cc one can optimize
5380 0 ? A : B to B even if A has side-effects. Something
5381 genmatch cannot handle. */
5383 (cond INTEGER_CST@0 @1 @2)
5384 (if (integer_zerop (@0))
5385 (if (!VOID_TYPE_P (TREE_TYPE (@2)) || VOID_TYPE_P (type))
5387 (if (!VOID_TYPE_P (TREE_TYPE (@1)) || VOID_TYPE_P (type))
5390 (vec_cond VECTOR_CST@0 @1 @2)
5391 (if (integer_all_onesp (@0))
5393 (if (integer_zerop (@0))
5396 /* Sink unary operations to branches, but only if we do fold both. */
5397 (for op (negate bit_not abs absu)
5399 (op (vec_cond:s @0 @1 @2))
5400 (vec_cond @0 (op! @1) (op! @2))))
5402 /* Sink unary conversions to branches, but only if we do fold both
5403 and the target's truth type is the same as we already have. */
5405 (convert (vec_cond:s @0 @1 @2))
5406 (if (VECTOR_TYPE_P (type)
5407 && types_match (TREE_TYPE (@0), truth_type_for (type)))
5408 (vec_cond @0 (convert! @1) (convert! @2))))
5410 /* Likewise for view_convert of nop_conversions. */
5412 (view_convert (vec_cond:s @0 @1 @2))
5413 (if (VECTOR_TYPE_P (type) && VECTOR_TYPE_P (TREE_TYPE (@1))
5414 && known_eq (TYPE_VECTOR_SUBPARTS (type),
5415 TYPE_VECTOR_SUBPARTS (TREE_TYPE (@1)))
5416 && tree_nop_conversion_p (TREE_TYPE (type), TREE_TYPE (TREE_TYPE (@1))))
5417 (vec_cond @0 (view_convert! @1) (view_convert! @2))))
5419 /* Sink binary operation to branches, but only if we can fold it. */
5420 (for op (tcc_comparison plus minus mult bit_and bit_ior bit_xor
5421 lshift rshift rdiv trunc_div ceil_div floor_div round_div
5422 trunc_mod ceil_mod floor_mod round_mod min max)
5423 /* (c ? a : b) op (c ? d : e) --> c ? (a op d) : (b op e) */
5425 (op (vec_cond:s @0 @1 @2) (vec_cond:s @0 @3 @4))
5426 (if (TREE_CODE_CLASS (op) != tcc_comparison
5427 || types_match (type, TREE_TYPE (@1))
5428 || expand_vec_cond_expr_p (type, TREE_TYPE (@0), ERROR_MARK)
5429 || (optimize_vectors_before_lowering_p ()
5430 /* The following is optimistic on the side of non-support, we are
5431 missing the legacy vcond{,u,eq} cases. Do this only when
5432 lowering will be able to fixup.. */
5433 && !expand_vec_cond_expr_p (TREE_TYPE (@1),
5434 TREE_TYPE (@0), ERROR_MARK)))
5435 (vec_cond @0 (op! @1 @3) (op! @2 @4))))
5437 /* (c ? a : b) op d --> c ? (a op d) : (b op d) */
5439 (op (vec_cond:s @0 @1 @2) @3)
5440 (if (TREE_CODE_CLASS (op) != tcc_comparison
5441 || types_match (type, TREE_TYPE (@1))
5442 || expand_vec_cond_expr_p (type, TREE_TYPE (@0), ERROR_MARK)
5443 || (optimize_vectors_before_lowering_p ()
5444 && !expand_vec_cond_expr_p (TREE_TYPE (@1),
5445 TREE_TYPE (@0), ERROR_MARK)))
5446 (vec_cond @0 (op! @1 @3) (op! @2 @3))))
5448 (op @3 (vec_cond:s @0 @1 @2))
5449 (if (TREE_CODE_CLASS (op) != tcc_comparison
5450 || types_match (type, TREE_TYPE (@1))
5451 || expand_vec_cond_expr_p (type, TREE_TYPE (@0), ERROR_MARK)
5452 || (optimize_vectors_before_lowering_p ()
5453 && !expand_vec_cond_expr_p (TREE_TYPE (@1),
5454 TREE_TYPE (@0), ERROR_MARK)))
5455 (vec_cond @0 (op! @3 @1) (op! @3 @2)))))
5458 (match (nop_atomic_bit_test_and_p @0 @1 @4)
5459 (bit_and (convert?@4 (ATOMIC_FETCH_OR_XOR_N @2 INTEGER_CST@0 @3))
5462 int ibit = tree_log2 (@0);
5463 int ibit2 = tree_log2 (@1);
5467 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))))))
5469 (match (nop_atomic_bit_test_and_p @0 @1 @3)
5470 (bit_and (convert?@3 (SYNC_FETCH_OR_XOR_N @2 INTEGER_CST@0))
5473 int ibit = tree_log2 (@0);
5474 int ibit2 = tree_log2 (@1);
5478 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))))))
5480 (match (nop_atomic_bit_test_and_p @0 @0 @4)
5483 (ATOMIC_FETCH_OR_XOR_N @2 (nop_convert? (lshift@0 integer_onep@5 @6)) @3))
5485 (if (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0)))))
5487 (match (nop_atomic_bit_test_and_p @0 @0 @4)
5490 (SYNC_FETCH_OR_XOR_N @2 (nop_convert? (lshift@0 integer_onep@3 @5))))
5492 (if (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0)))))
5494 (match (nop_atomic_bit_test_and_p @0 @1 @3)
5495 (bit_and@4 (convert?@3 (ATOMIC_FETCH_AND_N @2 INTEGER_CST@0 @5))
5498 int ibit = wi::exact_log2 (wi::zext (wi::bit_not (wi::to_wide (@0)),
5499 TYPE_PRECISION(type)));
5500 int ibit2 = tree_log2 (@1);
5504 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))))))
5506 (match (nop_atomic_bit_test_and_p @0 @1 @3)
5508 (convert?@3 (SYNC_FETCH_AND_AND_N @2 INTEGER_CST@0))
5511 int ibit = wi::exact_log2 (wi::zext (wi::bit_not (wi::to_wide (@0)),
5512 TYPE_PRECISION(type)));
5513 int ibit2 = tree_log2 (@1);
5517 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))))))
5519 (match (nop_atomic_bit_test_and_p @4 @0 @3)
5522 (ATOMIC_FETCH_AND_N @2 (nop_convert?@4 (bit_not (lshift@0 integer_onep@6 @7))) @5))
5524 (if (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@4)))))
5526 (match (nop_atomic_bit_test_and_p @4 @0 @3)
5529 (SYNC_FETCH_AND_AND_N @2 (nop_convert?@4 (bit_not (lshift@0 integer_onep@6 @7)))))
5531 (if (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@4)))))
5535 /* (v ? w : 0) ? a : b is just (v & w) ? a : b
5536 Currently disabled after pass lvec because ARM understands
5537 VEC_COND_EXPR<v==w,-1,0> but not a plain v==w fed to BIT_IOR_EXPR. */
5539 /* These can only be done in gimple as fold likes to convert:
5540 (CMP) & N into (CMP) ? N : 0
5541 and we try to match the same pattern again and again. */
5543 (vec_cond (vec_cond:s @0 @3 integer_zerop) @1 @2)
5544 (if (optimize_vectors_before_lowering_p () && types_match (@0, @3))
5545 (vec_cond (bit_and @0 @3) @1 @2)))
5547 (vec_cond (vec_cond:s @0 integer_all_onesp @3) @1 @2)
5548 (if (optimize_vectors_before_lowering_p () && types_match (@0, @3))
5549 (vec_cond (bit_ior @0 @3) @1 @2)))
5551 (vec_cond (vec_cond:s @0 integer_zerop @3) @1 @2)
5552 (if (optimize_vectors_before_lowering_p () && types_match (@0, @3))
5553 (vec_cond (bit_ior @0 (bit_not @3)) @2 @1)))
5555 (vec_cond (vec_cond:s @0 @3 integer_all_onesp) @1 @2)
5556 (if (optimize_vectors_before_lowering_p () && types_match (@0, @3))
5557 (vec_cond (bit_and @0 (bit_not @3)) @2 @1)))
5559 /* ((VCE (a cmp b ? -1 : 0)) < 0) ? c : d is just
5560 (VCE ((a cmp b) ? (VCE c) : (VCE d))) when TYPE_PRECISION of the
5561 component type of the outer vec_cond is greater equal the inner one. */
5562 (for cmp (simple_comparison)
5565 (lt (view_convert@5 (vec_cond@6 (cmp@4 @0 @1)
5568 integer_zerop) @2 @3)
5569 (if (VECTOR_INTEGER_TYPE_P (TREE_TYPE (@0))
5570 && VECTOR_INTEGER_TYPE_P (TREE_TYPE (@5))
5571 && !TYPE_UNSIGNED (TREE_TYPE (@5))
5572 && VECTOR_TYPE_P (TREE_TYPE (@6))
5573 && VECTOR_TYPE_P (type)
5574 && tree_int_cst_le (TYPE_SIZE (TREE_TYPE (type)),
5575 TYPE_SIZE (TREE_TYPE (TREE_TYPE (@6))))
5576 && TYPE_SIZE (type) == TYPE_SIZE (TREE_TYPE (@6)))
5577 (with { tree vtype = TREE_TYPE (@6);}
5579 (vec_cond @4 (view_convert:vtype @2) (view_convert:vtype @3)))))))
5581 /* c1 ? c2 ? a : b : b --> (c1 & c2) ? a : b */
5583 (vec_cond @0 (vec_cond:s @1 @2 @3) @3)
5584 (if (optimize_vectors_before_lowering_p () && types_match (@0, @1))
5585 (vec_cond (bit_and @0 @1) @2 @3)))
5587 (vec_cond @0 @2 (vec_cond:s @1 @2 @3))
5588 (if (optimize_vectors_before_lowering_p () && types_match (@0, @1))
5589 (vec_cond (bit_ior @0 @1) @2 @3)))
5591 (vec_cond @0 (vec_cond:s @1 @2 @3) @2)
5592 (if (optimize_vectors_before_lowering_p () && types_match (@0, @1))
5593 (vec_cond (bit_ior (bit_not @0) @1) @2 @3)))
5595 (vec_cond @0 @3 (vec_cond:s @1 @2 @3))
5596 (if (optimize_vectors_before_lowering_p () && types_match (@0, @1))
5597 (vec_cond (bit_and (bit_not @0) @1) @2 @3)))
5600 /* (a ? x : y) != (b ? x : y) --> (a^b) ? TRUE : FALSE */
5601 /* (a ? x : y) == (b ? x : y) --> (a^b) ? FALSE : TRUE */
5602 /* (a ? x : y) != (b ? y : x) --> (a^b) ? FALSE : TRUE */
5603 /* (a ? x : y) == (b ? y : x) --> (a^b) ? TRUE : FALSE */
5604 (for cnd (cond vec_cond)
5607 (eqne:c (cnd @0 @1 @2) (cnd @3 @1 @2))
5608 (cnd (bit_xor @0 @3) { constant_boolean_node (eqne == NE_EXPR, type); }
5609 { constant_boolean_node (eqne != NE_EXPR, type); }))
5611 (eqne:c (cnd @0 @1 @2) (cnd @3 @2 @1))
5612 (cnd (bit_xor @0 @3) { constant_boolean_node (eqne != NE_EXPR, type); }
5613 { constant_boolean_node (eqne == NE_EXPR, type); }))))
5615 /* Canonicalize mask ? { 0, ... } : { -1, ...} to ~mask if the mask
5616 types are compatible. */
5618 (vec_cond @0 VECTOR_CST@1 VECTOR_CST@2)
5619 (if (VECTOR_BOOLEAN_TYPE_P (type)
5620 && types_match (type, TREE_TYPE (@0)))
5621 (if (integer_zerop (@1) && integer_all_onesp (@2))
5623 (if (integer_all_onesp (@1) && integer_zerop (@2))
5626 /* A few simplifications of "a ? CST1 : CST2". */
5627 /* NOTE: Only do this on gimple as the if-chain-to-switch
5628 optimization depends on the gimple to have if statements in it. */
5631 (cond @0 INTEGER_CST@1 INTEGER_CST@2)
5633 (if (integer_zerop (@2))
5635 /* a ? 1 : 0 -> a if 0 and 1 are integral types. */
5636 (if (integer_onep (@1))
5637 (convert (convert:boolean_type_node @0)))
5638 /* a ? -1 : 0 -> -a. */
5639 (if (INTEGRAL_TYPE_P (type) && integer_all_onesp (@1))
5640 (if (TYPE_PRECISION (type) == 1)
5641 /* For signed 1-bit precision just cast bool to the type. */
5642 (convert (convert:boolean_type_node @0))
5643 (if (TREE_CODE (type) == BOOLEAN_TYPE)
5645 tree intt = build_nonstandard_integer_type (TYPE_PRECISION (type),
5646 TYPE_UNSIGNED (type));
5648 (convert (negate (convert:intt (convert:boolean_type_node @0)))))
5649 (negate (convert:type (convert:boolean_type_node @0))))))
5650 /* a ? powerof2cst : 0 -> a << (log2(powerof2cst)) */
5651 (if (INTEGRAL_TYPE_P (type) && integer_pow2p (@1))
5653 tree shift = build_int_cst (integer_type_node, tree_log2 (@1));
5655 (lshift (convert (convert:boolean_type_node @0)) { shift; })))))
5656 (if (integer_zerop (@1))
5658 /* a ? 0 : 1 -> !a. */
5659 (if (integer_onep (@2))
5660 (convert (bit_xor (convert:boolean_type_node @0) { boolean_true_node; })))
5661 /* a ? 0 : -1 -> -(!a). */
5662 (if (INTEGRAL_TYPE_P (type) && integer_all_onesp (@2))
5663 (if (TYPE_PRECISION (type) == 1)
5664 /* For signed 1-bit precision just cast bool to the type. */
5665 (convert (bit_xor (convert:boolean_type_node @0) { boolean_true_node; }))
5666 (if (TREE_CODE (type) == BOOLEAN_TYPE)
5668 tree intt = build_nonstandard_integer_type (TYPE_PRECISION (type),
5669 TYPE_UNSIGNED (type));
5671 (convert (negate (convert:intt (bit_xor (convert:boolean_type_node @0)
5672 { boolean_true_node; })))))
5673 (negate (convert:type (bit_xor (convert:boolean_type_node @0)
5674 { boolean_true_node; }))))))
5675 /* a ? 0 : powerof2cst -> (!a) << (log2(powerof2cst)) */
5676 (if (INTEGRAL_TYPE_P (type) && integer_pow2p (@2))
5678 tree shift = build_int_cst (integer_type_node, tree_log2 (@2));
5680 (lshift (convert (bit_xor (convert:boolean_type_node @0)
5681 { boolean_true_node; })) { shift; })))))))
5683 /* (a > 1) ? 0 : (cast)a is the same as (cast)(a == 1)
5684 for unsigned types. */
5686 (cond (gt @0 integer_onep@1) integer_zerop (convert? @2))
5687 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
5688 && bitwise_equal_p (@0, @2))
5689 (convert (eq @0 @1))
5693 /* (a <= 1) & (cast)a is the same as (cast)(a == 1)
5694 for unsigned types. */
5696 (bit_and:c (convert1? (le @0 integer_onep@1)) (convert2? @2))
5697 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
5698 && bitwise_equal_p (@0, @2))
5699 (convert (eq @0 @1))
5703 /* `(a == CST) & a` can be simplified to `0` or `(a == CST)` depending
5704 on the first bit of the CST. */
5706 (bit_and:c (convert@2 (eq @0 INTEGER_CST@1)) (convert? @0))
5707 (if ((wi::to_wide (@1) & 1) != 0)
5709 { build_zero_cst (type); }))
5712 # x_5 in range [cst1, cst2] where cst2 = cst1 + 1
5713 x_5 == cstN ? cst4 : cst3
5714 # op is == or != and N is 1 or 2
5715 to r_6 = x_5 + (min (cst3, cst4) - cst1) or
5716 r_6 = (min (cst3, cst4) + cst1) - x_5 depending on op, N and which
5717 of cst3 and cst4 is smaller.
5718 This was originally done by two_value_replacement in phiopt (PR 88676). */
5721 (cond (eqne SSA_NAME@0 INTEGER_CST@1) INTEGER_CST@2 INTEGER_CST@3)
5722 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
5723 && INTEGRAL_TYPE_P (type)
5724 && (wi::to_widest (@2) + 1 == wi::to_widest (@3)
5725 || wi::to_widest (@2) == wi::to_widest (@3) + 1))
5728 get_range_query (cfun)->range_of_expr (r, @0);
5729 if (r.undefined_p ())
5730 r.set_varying (TREE_TYPE (@0));
5732 wide_int min = r.lower_bound ();
5733 wide_int max = r.upper_bound ();
5736 && (wi::to_wide (@1) == min
5737 || wi::to_wide (@1) == max))
5739 tree arg0 = @2, arg1 = @3;
5741 if ((eqne == EQ_EXPR) ^ (wi::to_wide (@1) == min))
5742 std::swap (arg0, arg1);
5743 if (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type))
5744 type1 = TREE_TYPE (@0);
5747 auto prec = TYPE_PRECISION (type1);
5748 auto unsign = TYPE_UNSIGNED (type1);
5749 if (TREE_CODE (type1) == BOOLEAN_TYPE)
5750 type1 = build_nonstandard_integer_type (prec, unsign);
5751 min = wide_int::from (min, prec,
5752 TYPE_SIGN (TREE_TYPE (@0)));
5753 wide_int a = wide_int::from (wi::to_wide (arg0), prec,
5755 enum tree_code code;
5756 wi::overflow_type ovf;
5757 if (tree_int_cst_lt (arg0, arg1))
5763 /* lhs is known to be in range [min, min+1] and we want to add a
5764 to it. Check if that operation can overflow for those 2 values
5765 and if yes, force unsigned type. */
5766 wi::add (min + (wi::neg_p (a) ? 0 : 1), a, SIGNED, &ovf);
5768 type1 = unsigned_type_for (type1);
5777 /* lhs is known to be in range [min, min+1] and we want to subtract
5778 it from a. Check if that operation can overflow for those 2
5779 values and if yes, force unsigned type. */
5780 wi::sub (a, min + (wi::neg_p (min) ? 0 : 1), SIGNED, &ovf);
5782 type1 = unsigned_type_for (type1);
5785 tree arg = wide_int_to_tree (type1, a);
5787 (if (code == PLUS_EXPR)
5788 (convert (plus (convert:type1 @0) { arg; }))
5789 (convert (minus { arg; } (convert:type1 @0))))))))))
5793 (convert (cond@0 @1 INTEGER_CST@2 INTEGER_CST@3))
5794 (if (INTEGRAL_TYPE_P (type)
5795 && INTEGRAL_TYPE_P (TREE_TYPE (@0)))
5796 (cond @1 (convert @2) (convert @3))))
5798 /* Simplification moved from fold_cond_expr_with_comparison. It may also
5800 /* This pattern implements two kinds simplification:
5803 (cond (cmp (convert1? x) c1) (convert2? x) c2) -> (minmax (x c)) if:
5804 1) Conversions are type widening from smaller type.
5805 2) Const c1 equals to c2 after canonicalizing comparison.
5806 3) Comparison has tree code LT, LE, GT or GE.
5807 This specific pattern is needed when (cmp (convert x) c) may not
5808 be simplified by comparison patterns because of multiple uses of
5809 x. It also makes sense here because simplifying across multiple
5810 referred var is always benefitial for complicated cases.
5813 (cond (eq (convert1? x) c1) (convert2? x) c2) -> (cond (eq x c1) c1 c2). */
5814 (for cmp (lt le gt ge eq ne)
5816 (cond (cmp (convert1? @1) INTEGER_CST@3) (convert2? @1) INTEGER_CST@2)
5819 tree from_type = TREE_TYPE (@1);
5820 tree c1_type = TREE_TYPE (@3), c2_type = TREE_TYPE (@2);
5821 enum tree_code code = ERROR_MARK;
5823 if (INTEGRAL_TYPE_P (from_type)
5824 && int_fits_type_p (@2, from_type)
5825 && (types_match (c1_type, from_type)
5826 || (TYPE_PRECISION (c1_type) > TYPE_PRECISION (from_type)
5827 && (TYPE_UNSIGNED (from_type)
5828 || TYPE_SIGN (c1_type) == TYPE_SIGN (from_type))))
5829 && (types_match (c2_type, from_type)
5830 || (TYPE_PRECISION (c2_type) > TYPE_PRECISION (from_type)
5831 && (TYPE_UNSIGNED (from_type)
5832 || TYPE_SIGN (c2_type) == TYPE_SIGN (from_type)))))
5835 code = minmax_from_comparison (cmp, @1, @3, @1, @2);
5836 /* Can do A == C1 ? A : C2 -> A == C1 ? C1 : C2? */
5837 else if (int_fits_type_p (@3, from_type))
5841 (if (code == MAX_EXPR)
5842 (convert (max @1 (convert @2)))
5843 (if (code == MIN_EXPR)
5844 (convert (min @1 (convert @2)))
5845 (if (code == EQ_EXPR)
5846 (convert (cond (eq @1 (convert @3))
5847 (convert:from_type @3) (convert:from_type @2)))))))))
5849 /* (cond (cmp (convert? x) c1) (op x c2) c3) -> (op (minmax x c1) c2) if:
5851 1) OP is PLUS or MINUS.
5852 2) CMP is LT, LE, GT or GE.
5853 3) C3 == (C1 op C2), and computation doesn't have undefined behavior.
5855 This pattern also handles special cases like:
5857 A) Operand x is a unsigned to signed type conversion and c1 is
5858 integer zero. In this case,
5859 (signed type)x < 0 <=> x > MAX_VAL(signed type)
5860 (signed type)x >= 0 <=> x <= MAX_VAL(signed type)
5861 B) Const c1 may not equal to (C3 op' C2). In this case we also
5862 check equality for (c1+1) and (c1-1) by adjusting comparison
5865 TODO: Though signed type is handled by this pattern, it cannot be
5866 simplified at the moment because C standard requires additional
5867 type promotion. In order to match&simplify it here, the IR needs
5868 to be cleaned up by other optimizers, i.e, VRP. */
5869 (for op (plus minus)
5870 (for cmp (lt le gt ge)
5872 (cond (cmp (convert? @X) INTEGER_CST@1) (op @X INTEGER_CST@2) INTEGER_CST@3)
5873 (with { tree from_type = TREE_TYPE (@X), to_type = TREE_TYPE (@1); }
5874 (if (types_match (from_type, to_type)
5875 /* Check if it is special case A). */
5876 || (TYPE_UNSIGNED (from_type)
5877 && !TYPE_UNSIGNED (to_type)
5878 && TYPE_PRECISION (from_type) == TYPE_PRECISION (to_type)
5879 && integer_zerop (@1)
5880 && (cmp == LT_EXPR || cmp == GE_EXPR)))
5883 wi::overflow_type overflow = wi::OVF_NONE;
5884 enum tree_code code, cmp_code = cmp;
5886 wide_int c1 = wi::to_wide (@1);
5887 wide_int c2 = wi::to_wide (@2);
5888 wide_int c3 = wi::to_wide (@3);
5889 signop sgn = TYPE_SIGN (from_type);
5891 /* Handle special case A), given x of unsigned type:
5892 ((signed type)x < 0) <=> (x > MAX_VAL(signed type))
5893 ((signed type)x >= 0) <=> (x <= MAX_VAL(signed type)) */
5894 if (!types_match (from_type, to_type))
5896 if (cmp_code == LT_EXPR)
5898 if (cmp_code == GE_EXPR)
5900 c1 = wi::max_value (to_type);
5902 /* To simplify this pattern, we require c3 = (c1 op c2). Here we
5903 compute (c3 op' c2) and check if it equals to c1 with op' being
5904 the inverted operator of op. Make sure overflow doesn't happen
5905 if it is undefined. */
5906 if (op == PLUS_EXPR)
5907 real_c1 = wi::sub (c3, c2, sgn, &overflow);
5909 real_c1 = wi::add (c3, c2, sgn, &overflow);
5912 if (!overflow || !TYPE_OVERFLOW_UNDEFINED (from_type))
5914 /* Check if c1 equals to real_c1. Boundary condition is handled
5915 by adjusting comparison operation if necessary. */
5916 if (!wi::cmp (wi::sub (real_c1, 1, sgn, &overflow), c1, sgn)
5919 /* X <= Y - 1 equals to X < Y. */
5920 if (cmp_code == LE_EXPR)
5922 /* X > Y - 1 equals to X >= Y. */
5923 if (cmp_code == GT_EXPR)
5926 if (!wi::cmp (wi::add (real_c1, 1, sgn, &overflow), c1, sgn)
5929 /* X < Y + 1 equals to X <= Y. */
5930 if (cmp_code == LT_EXPR)
5932 /* X >= Y + 1 equals to X > Y. */
5933 if (cmp_code == GE_EXPR)
5936 if (code != cmp_code || !wi::cmp (real_c1, c1, sgn))
5938 if (cmp_code == LT_EXPR || cmp_code == LE_EXPR)
5940 if (cmp_code == GT_EXPR || cmp_code == GE_EXPR)
5945 (if (code == MAX_EXPR)
5946 (op (max @X { wide_int_to_tree (from_type, real_c1); })
5947 { wide_int_to_tree (from_type, c2); })
5948 (if (code == MIN_EXPR)
5949 (op (min @X { wide_int_to_tree (from_type, real_c1); })
5950 { wide_int_to_tree (from_type, c2); })))))))))
5953 /* A >= B ? A : B -> max (A, B) and friends. The code is still
5954 in fold_cond_expr_with_comparison for GENERIC folding with
5955 some extra constraints. */
5956 (for cmp (eq ne le lt unle unlt ge gt unge ungt uneq ltgt)
5958 (cond (cmp:c (nop_convert1?@c0 @0) (nop_convert2?@c1 @1))
5959 (convert3? @0) (convert4? @1))
5960 (if (!HONOR_SIGNED_ZEROS (type)
5961 && (/* Allow widening conversions of the compare operands as data. */
5962 (INTEGRAL_TYPE_P (type)
5963 && types_match (TREE_TYPE (@c0), TREE_TYPE (@0))
5964 && types_match (TREE_TYPE (@c1), TREE_TYPE (@1))
5965 && TYPE_PRECISION (TREE_TYPE (@0)) <= TYPE_PRECISION (type)
5966 && TYPE_PRECISION (TREE_TYPE (@1)) <= TYPE_PRECISION (type))
5967 /* Or sign conversions for the comparison. */
5968 || (types_match (type, TREE_TYPE (@0))
5969 && types_match (type, TREE_TYPE (@1)))))
5971 (if (cmp == EQ_EXPR)
5972 (if (VECTOR_TYPE_P (type))
5975 (if (cmp == NE_EXPR)
5976 (if (VECTOR_TYPE_P (type))
5979 (if (cmp == LE_EXPR || cmp == UNLE_EXPR || cmp == LT_EXPR || cmp == UNLT_EXPR)
5980 (if (!HONOR_NANS (type))
5981 (if (VECTOR_TYPE_P (type))
5982 (view_convert (min @c0 @c1))
5983 (convert (min @c0 @c1)))))
5984 (if (cmp == GE_EXPR || cmp == UNGE_EXPR || cmp == GT_EXPR || cmp == UNGT_EXPR)
5985 (if (!HONOR_NANS (type))
5986 (if (VECTOR_TYPE_P (type))
5987 (view_convert (max @c0 @c1))
5988 (convert (max @c0 @c1)))))
5989 (if (cmp == UNEQ_EXPR)
5990 (if (!HONOR_NANS (type))
5991 (if (VECTOR_TYPE_P (type))
5994 (if (cmp == LTGT_EXPR)
5995 (if (!HONOR_NANS (type))
5996 (if (VECTOR_TYPE_P (type))
5998 (convert @c0))))))))
6000 /* This is for VEC_COND_EXPR
6001 Optimize A < B ? A : B to MIN (A, B)
6002 A > B ? A : B to MAX (A, B). */
6003 (for cmp (lt le ungt unge gt ge unlt unle)
6004 minmax (min min min min max max max max)
6005 MINMAX (MIN_EXPR MIN_EXPR MIN_EXPR MIN_EXPR MAX_EXPR MAX_EXPR MAX_EXPR MAX_EXPR)
6007 (vec_cond (cmp @0 @1) @0 @1)
6008 (if (VECTOR_INTEGER_TYPE_P (type)
6009 && target_supports_op_p (type, MINMAX, optab_vector))
6012 (for cmp (lt le ungt unge gt ge unlt unle)
6013 minmax (max max max max min min min min)
6014 MINMAX (MAX_EXPR MAX_EXPR MAX_EXPR MAX_EXPR MIN_EXPR MIN_EXPR MIN_EXPR MIN_EXPR)
6016 (vec_cond (cmp @0 @1) @1 @0)
6017 (if (VECTOR_INTEGER_TYPE_P (type)
6018 && target_supports_op_p (type, MINMAX, optab_vector))
6021 /* Try to optimize x < 0 ? -1 : 0 into (signed) x >> 31
6022 and x < 0 ? 1 : 0 into (unsigned) x >> 31. */
6024 (vec_cond (lt @0 integer_zerop) integer_all_onesp integer_zerop)
6025 (if (VECTOR_INTEGER_TYPE_P (TREE_TYPE (@0))
6026 && !TYPE_UNSIGNED (TREE_TYPE (@0))
6027 && tree_nop_conversion_p (type, TREE_TYPE (@0))
6028 && target_supports_op_p (TREE_TYPE (@0), RSHIFT_EXPR, optab_scalar))
6031 unsigned int prec = element_precision (TREE_TYPE (@0));
6034 (rshift @0 { build_int_cst (integer_type_node, prec - 1);})))))
6037 (vec_cond (lt @0 integer_zerop) integer_onep integer_zerop)
6038 (if (VECTOR_INTEGER_TYPE_P (TREE_TYPE (@0))
6039 && !TYPE_UNSIGNED (TREE_TYPE (@0))
6040 && tree_nop_conversion_p (type, TREE_TYPE (@0)))
6043 unsigned int prec = element_precision (TREE_TYPE (@0));
6044 tree utype = unsigned_type_for (TREE_TYPE (@0));
6046 (if (target_supports_op_p (utype, RSHIFT_EXPR, optab_scalar))
6048 (rshift (view_convert:utype @0)
6049 { build_int_cst (integer_type_node, prec - 1);}))))))
6052 (for cnd (cond vec_cond)
6053 /* (a != b) ? (a - b) : 0 -> (a - b) */
6055 (cnd (ne:c @0 @1) (minus@2 @0 @1) integer_zerop)
6057 /* (a != b) ? (a ^ b) : 0 -> (a ^ b) */
6059 (cnd (ne:c @0 @1) (bit_xor:c@2 @0 @1) integer_zerop)
6061 /* (a != b) ? (a & b) : a -> (a & b) */
6062 /* (a != b) ? (a | b) : a -> (a | b) */
6063 /* (a != b) ? min(a,b) : a -> min(a,b) */
6064 /* (a != b) ? max(a,b) : a -> max(a,b) */
6065 (for op (bit_and bit_ior min max)
6067 (cnd (ne:c @0 @1) (op:c@2 @0 @1) @0)
6069 /* (a != b) ? (a * b) : (a * a) -> (a * b) */
6070 /* (a != b) ? (a + b) : (a + a) -> (a + b) */
6073 (cnd (ne:c @0 @1) (op@2 @0 @1) (op @0 @0))
6074 (if (ANY_INTEGRAL_TYPE_P (type))
6076 /* (a != b) ? (a + b) : (2 * a) -> (a + b) */
6078 (cnd (ne:c @0 @1) (plus:c@2 @0 @1) (mult @0 uniform_integer_cst_p@3))
6079 (if (wi::to_wide (uniform_integer_cst_p (@3)) == 2)
6083 /* These was part of minmax phiopt. */
6084 /* Optimize (a CMP b) ? minmax<a, c> : minmax<b, c>
6085 to minmax<min/max<a, b>, c> */
6086 (for minmax (min max)
6087 (for cmp (lt le gt ge ne)
6089 (cond (cmp:c @1 @3) (minmax:c @1 @4) (minmax:c @2 @4))
6092 tree_code code = minmax_from_comparison (cmp, @1, @2, @1, @3);
6094 (if (code == MIN_EXPR)
6095 (minmax (min @1 @2) @4)
6096 (if (code == MAX_EXPR)
6097 (minmax (max @1 @2) @4)))))))
6099 /* Optimize (a CMP CST1) ? max<a,CST2> : a */
6100 (for cmp (gt ge lt le)
6101 minmax (min min max max)
6103 (cond (cmp:c @0 @1) (minmax:c@2 @0 @3) @4)
6106 tree_code code = minmax_from_comparison (cmp, @0, @1, @0, @4);
6108 (if ((cmp == LT_EXPR || cmp == LE_EXPR)
6110 && integer_nonzerop (fold_build2 (LE_EXPR, boolean_type_node, @3, @4)))
6112 (if ((cmp == GT_EXPR || cmp == GE_EXPR)
6114 && integer_nonzerop (fold_build2 (GE_EXPR, boolean_type_node, @3, @4)))
6118 /* These patterns should be after min/max detection as simplifications
6119 of `(type)(zero_one ==/!= 0)` to `(type)(zero_one)`
6120 and `(type)(zero_one^1)` are not done yet. See PR 110637.
6121 Even without those, reaching min/max/and/ior faster is better. */
6123 (cond @0 zero_one_valued_p@1 zero_one_valued_p@2)
6125 /* bool0 ? bool1 : 0 -> bool0 & bool1 */
6126 (if (integer_zerop (@2))
6127 (bit_and (convert @0) @1))
6128 /* bool0 ? 0 : bool2 -> (bool0^1) & bool2 */
6129 (if (integer_zerop (@1))
6130 (bit_and (bit_xor (convert @0) { build_one_cst (type); } ) @2))
6131 /* bool0 ? 1 : bool2 -> bool0 | bool2 */
6132 (if (integer_onep (@1))
6133 (bit_ior (convert @0) @2))
6134 /* bool0 ? bool1 : 1 -> (bool0^1) | bool1 */
6135 (if (integer_onep (@2))
6136 (bit_ior (bit_xor (convert @0) @2) @1))
6141 /* X != C1 ? -X : C2 simplifies to -X when -C1 == C2. */
6143 (cond (ne @0 INTEGER_CST@1) (negate@3 @0) INTEGER_CST@2)
6144 (if (!TYPE_SATURATING (type)
6145 && (TYPE_OVERFLOW_WRAPS (type)
6146 || !wi::only_sign_bit_p (wi::to_wide (@1)))
6147 && wi::eq_p (wi::neg (wi::to_wide (@1)), wi::to_wide (@2)))
6150 /* X != C1 ? ~X : C2 simplifies to ~X when ~C1 == C2. */
6152 (cond (ne @0 INTEGER_CST@1) (bit_not@3 @0) INTEGER_CST@2)
6153 (if (wi::eq_p (wi::bit_not (wi::to_wide (@1)), wi::to_wide (@2)))
6156 /* X != C1 ? abs(X) : C2 simplifies to abs(x) when abs(C1) == C2. */
6159 (cond (ne @0 INTEGER_CST@1) (op@3 @0) INTEGER_CST@2)
6160 (if (wi::abs (wi::to_wide (@1)) == wi::to_wide (@2))
6161 (if (op != ABSU_EXPR && wi::only_sign_bit_p (wi::to_wide (@1)))
6162 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
6163 (convert (absu:utype @0)))
6166 /* X > Positive ? X : ABS(X) -> ABS(X) */
6167 /* X >= Positive ? X : ABS(X) -> ABS(X) */
6168 /* X == Positive ? X : ABS(X) -> ABS(X) */
6171 (cond (cmp:c @0 tree_expr_nonnegative_p@1) @0 (abs@3 @0))
6172 (if (INTEGRAL_TYPE_P (type))
6175 /* X == Positive ? Positive : ABS(X) -> ABS(X) */
6177 (cond (eq:c @0 tree_expr_nonnegative_p@1) @1 (abs@3 @0))
6178 (if (INTEGRAL_TYPE_P (type))
6181 /* (X + 1) > Y ? -X : 1 simplifies to X >= Y ? -X : 1 when
6182 X is unsigned, as when X + 1 overflows, X is -1, so -X == 1. */
6184 (cond (gt (plus @0 integer_onep) @1) (negate @0) integer_onep@2)
6185 (if (TYPE_UNSIGNED (type))
6186 (cond (ge @0 @1) (negate @0) @2)))
6188 (for cnd (cond vec_cond)
6189 /* A ? B : (A ? X : C) -> A ? B : C. */
6191 (cnd @0 (cnd @0 @1 @2) @3)
6194 (cnd @0 @1 (cnd @0 @2 @3))
6196 /* A ? B : (!A ? C : X) -> A ? B : C. */
6197 /* ??? This matches embedded conditions open-coded because genmatch
6198 would generate matching code for conditions in separate stmts only.
6199 The following is still important to merge then and else arm cases
6200 from if-conversion. */
6202 (cnd @0 @1 (cnd @2 @3 @4))
6203 (if (inverse_conditions_p (@0, @2))
6206 (cnd @0 (cnd @1 @2 @3) @4)
6207 (if (inverse_conditions_p (@0, @1))
6210 /* A ? B : B -> B. */
6215 /* !A ? B : C -> A ? C : B. */
6217 (cnd (logical_inverted_value truth_valued_p@0) @1 @2)
6218 /* For CONDs, don't handle signed values here. */
6219 (if (cnd == VEC_COND_EXPR
6220 || TYPE_UNSIGNED (TREE_TYPE (@0)))
6223 /* abs/negative simplifications moved from fold_cond_expr_with_comparison.
6225 None of these transformations work for modes with signed
6226 zeros. If A is +/-0, the first two transformations will
6227 change the sign of the result (from +0 to -0, or vice
6228 versa). The last four will fix the sign of the result,
6229 even though the original expressions could be positive or
6230 negative, depending on the sign of A.
6232 Note that all these transformations are correct if A is
6233 NaN, since the two alternatives (A and -A) are also NaNs. */
6235 (for cnd (cond vec_cond)
6236 /* A == 0 ? A : -A same as -A */
6239 (cnd (cmp @0 zerop) @2 (negate@1 @2))
6240 (if (!HONOR_SIGNED_ZEROS (type)
6241 && bitwise_equal_p (@0, @2))
6244 (cnd (cmp @0 zerop) zerop (negate@1 @2))
6245 (if (!HONOR_SIGNED_ZEROS (type)
6246 && bitwise_equal_p (@0, @2))
6249 /* A != 0 ? A : -A same as A */
6252 (cnd (cmp @0 zerop) @1 (negate @1))
6253 (if (!HONOR_SIGNED_ZEROS (type)
6254 && bitwise_equal_p (@0, @1))
6257 (cnd (cmp @0 zerop) @1 integer_zerop)
6258 (if (!HONOR_SIGNED_ZEROS (type)
6259 && bitwise_equal_p (@0, @1))
6262 /* A >=/> 0 ? A : -A same as abs (A) */
6265 (cnd (cmp @0 zerop) @1 (negate @1))
6266 (if (!HONOR_SIGNED_ZEROS (TREE_TYPE(@0))
6267 && !TYPE_UNSIGNED (TREE_TYPE(@0))
6268 && bitwise_equal_p (@0, @1))
6269 (if (TYPE_UNSIGNED (type))
6272 /* A <=/< 0 ? A : -A same as -abs (A) */
6275 (cnd (cmp @0 zerop) @1 (negate @1))
6276 (if (!HONOR_SIGNED_ZEROS (TREE_TYPE(@0))
6277 && !TYPE_UNSIGNED (TREE_TYPE(@0))
6278 && bitwise_equal_p (@0, @1))
6279 (if ((ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
6280 && !TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
6281 || TYPE_UNSIGNED (type))
6283 tree utype = unsigned_type_for (TREE_TYPE(@0));
6285 (convert (negate (absu:utype @0))))
6286 (negate (abs @0)))))
6289 /* (A - B) == 0 ? (A - B) : (B - A) same as (B - A) */
6292 (cnd (cmp (minus@0 @1 @2) zerop) @0 (minus@3 @2 @1))
6293 (if (!HONOR_SIGNED_ZEROS (type))
6296 (cnd (cmp (minus@0 @1 @2) integer_zerop) integer_zerop (minus@3 @2 @1))
6299 /* (A - B) != 0 ? (A - B) : (B - A) same as (A - B) */
6302 (cnd (cmp (minus@0 @1 @2) zerop) @0 (minus @2 @1))
6303 (if (!HONOR_SIGNED_ZEROS (type))
6306 (cnd (cmp (minus@0 @1 @2) integer_zerop) @0 integer_zerop)
6309 /* (A - B) >=/> 0 ? (A - B) : (B - A) same as abs (A - B) */
6312 (cnd (cmp (minus@0 @1 @2) zerop) @0 (minus @2 @1))
6313 (if (!HONOR_SIGNED_ZEROS (type)
6314 && !TYPE_UNSIGNED (type))
6316 /* (A - B) <=/< 0 ? (A - B) : (B - A) same as -abs (A - B) */
6319 (cnd (cmp (minus@0 @1 @2) zerop) @0 (minus @2 @1))
6320 (if (!HONOR_SIGNED_ZEROS (type)
6321 && !TYPE_UNSIGNED (type))
6322 (if (ANY_INTEGRAL_TYPE_P (type)
6323 && !TYPE_OVERFLOW_WRAPS (type))
6325 tree utype = unsigned_type_for (type);
6327 (convert (negate (absu:utype @0))))
6328 (negate (abs @0)))))
6332 /* -(type)!A -> (type)A - 1. */
6334 (negate (convert?:s (logical_inverted_value:s @0)))
6335 (if (INTEGRAL_TYPE_P (type)
6336 && TREE_CODE (type) != BOOLEAN_TYPE
6337 && TYPE_PRECISION (type) > 1
6338 && TREE_CODE (@0) == SSA_NAME
6339 && ssa_name_has_boolean_range (@0))
6340 (plus (convert:type @0) { build_all_ones_cst (type); })))
6342 /* A + (B vcmp C ? 1 : 0) -> A - (B vcmp C ? -1 : 0), since vector comparisons
6343 return all -1 or all 0 results. */
6344 /* ??? We could instead convert all instances of the vec_cond to negate,
6345 but that isn't necessarily a win on its own. */
6347 (plus:c @3 (view_convert? (vec_cond:s @0 integer_each_onep@1 integer_zerop@2)))
6348 (if (VECTOR_TYPE_P (type)
6349 && known_eq (TYPE_VECTOR_SUBPARTS (type),
6350 TYPE_VECTOR_SUBPARTS (TREE_TYPE (@1)))
6351 && (TYPE_MODE (TREE_TYPE (type))
6352 == TYPE_MODE (TREE_TYPE (TREE_TYPE (@1)))))
6353 (minus @3 (view_convert (vec_cond @0 (negate @1) @2)))))
6355 /* ... likewise A - (B vcmp C ? 1 : 0) -> A + (B vcmp C ? -1 : 0). */
6357 (minus @3 (view_convert? (vec_cond:s @0 integer_each_onep@1 integer_zerop@2)))
6358 (if (VECTOR_TYPE_P (type)
6359 && known_eq (TYPE_VECTOR_SUBPARTS (type),
6360 TYPE_VECTOR_SUBPARTS (TREE_TYPE (@1)))
6361 && (TYPE_MODE (TREE_TYPE (type))
6362 == TYPE_MODE (TREE_TYPE (TREE_TYPE (@1)))))
6363 (plus @3 (view_convert (vec_cond @0 (negate @1) @2)))))
6366 /* Simplifications of comparisons. */
6368 /* See if we can reduce the magnitude of a constant involved in a
6369 comparison by changing the comparison code. This is a canonicalization
6370 formerly done by maybe_canonicalize_comparison_1. */
6374 (cmp @0 uniform_integer_cst_p@1)
6375 (with { tree cst = uniform_integer_cst_p (@1); }
6376 (if (tree_int_cst_sgn (cst) == -1)
6377 (acmp @0 { build_uniform_cst (TREE_TYPE (@1),
6378 wide_int_to_tree (TREE_TYPE (cst),
6384 (cmp @0 uniform_integer_cst_p@1)
6385 (with { tree cst = uniform_integer_cst_p (@1); }
6386 (if (tree_int_cst_sgn (cst) == 1)
6387 (acmp @0 { build_uniform_cst (TREE_TYPE (@1),
6388 wide_int_to_tree (TREE_TYPE (cst),
6389 wi::to_wide (cst) - 1)); })))))
6391 /* We can simplify a logical negation of a comparison to the
6392 inverted comparison. As we cannot compute an expression
6393 operator using invert_tree_comparison we have to simulate
6394 that with expression code iteration. */
6395 (for cmp (tcc_comparison)
6396 icmp (inverted_tcc_comparison)
6397 ncmp (inverted_tcc_comparison_with_nans)
6398 /* Ideally we'd like to combine the following two patterns
6399 and handle some more cases by using
6400 (logical_inverted_value (cmp @0 @1))
6401 here but for that genmatch would need to "inline" that.
6402 For now implement what forward_propagate_comparison did. */
6404 (bit_not (cmp @0 @1))
6405 (if (VECTOR_TYPE_P (type)
6406 || (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1))
6407 /* Comparison inversion may be impossible for trapping math,
6408 invert_tree_comparison will tell us. But we can't use
6409 a computed operator in the replacement tree thus we have
6410 to play the trick below. */
6411 (with { enum tree_code ic = invert_tree_comparison
6412 (cmp, HONOR_NANS (@0)); }
6418 (bit_xor (cmp @0 @1) integer_truep)
6419 (with { enum tree_code ic = invert_tree_comparison
6420 (cmp, HONOR_NANS (@0)); }
6425 /* The following bits are handled by fold_binary_op_with_conditional_arg. */
6427 (ne (cmp@2 @0 @1) integer_zerop)
6428 (if (types_match (type, TREE_TYPE (@2)))
6431 (eq (cmp@2 @0 @1) integer_truep)
6432 (if (types_match (type, TREE_TYPE (@2)))
6435 (ne (cmp@2 @0 @1) integer_truep)
6436 (if (types_match (type, TREE_TYPE (@2)))
6437 (with { enum tree_code ic = invert_tree_comparison
6438 (cmp, HONOR_NANS (@0)); }
6444 (eq (cmp@2 @0 @1) integer_zerop)
6445 (if (types_match (type, TREE_TYPE (@2)))
6446 (with { enum tree_code ic = invert_tree_comparison
6447 (cmp, HONOR_NANS (@0)); }
6453 /* Transform comparisons of the form X - Y CMP 0 to X CMP Y.
6454 ??? The transformation is valid for the other operators if overflow
6455 is undefined for the type, but performing it here badly interacts
6456 with the transformation in fold_cond_expr_with_comparison which
6457 attempts to synthetize ABS_EXPR. */
6459 (for sub (minus pointer_diff)
6461 (cmp (sub@2 @0 @1) integer_zerop)
6462 (if (single_use (@2))
6465 /* Simplify (x < 0) ^ (y < 0) to (x ^ y) < 0 and
6466 (x >= 0) ^ (y >= 0) to (x ^ y) < 0. */
6469 (bit_xor (cmp:s @0 integer_zerop) (cmp:s @1 integer_zerop))
6470 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6471 && !TYPE_UNSIGNED (TREE_TYPE (@0))
6472 && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
6473 (lt (bit_xor @0 @1) { build_zero_cst (TREE_TYPE (@0)); }))))
6474 /* Simplify (x < 0) ^ (y >= 0) to (x ^ y) >= 0 and
6475 (x >= 0) ^ (y < 0) to (x ^ y) >= 0. */
6477 (bit_xor:c (lt:s @0 integer_zerop) (ge:s @1 integer_zerop))
6478 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6479 && !TYPE_UNSIGNED (TREE_TYPE (@0))
6480 && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
6481 (ge (bit_xor @0 @1) { build_zero_cst (TREE_TYPE (@0)); })))
6483 /* Transform comparisons of the form X * C1 CMP 0 to X CMP 0 in the
6484 signed arithmetic case. That form is created by the compiler
6485 often enough for folding it to be of value. One example is in
6486 computing loop trip counts after Operator Strength Reduction. */
6487 (for cmp (simple_comparison)
6488 scmp (swapped_simple_comparison)
6490 (cmp (mult@3 @0 INTEGER_CST@1) integer_zerop@2)
6491 /* Handle unfolded multiplication by zero. */
6492 (if (integer_zerop (@1))
6494 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
6495 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
6497 /* If @1 is negative we swap the sense of the comparison. */
6498 (if (tree_int_cst_sgn (@1) < 0)
6502 /* For integral types with undefined overflow fold
6503 x * C1 == C2 into x == C2 / C1 or false.
6504 If overflow wraps and C1 is odd, simplify to x == C2 / C1 in the ring
6508 (cmp (mult @0 INTEGER_CST@1) INTEGER_CST@2)
6509 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6510 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
6511 && wi::to_wide (@1) != 0)
6512 (with { widest_int quot; }
6513 (if (wi::multiple_of_p (wi::to_widest (@2), wi::to_widest (@1),
6514 TYPE_SIGN (TREE_TYPE (@0)), "))
6515 (cmp @0 { wide_int_to_tree (TREE_TYPE (@0), quot); })
6516 { constant_boolean_node (cmp == NE_EXPR, type); }))
6517 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6518 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))
6519 && (wi::bit_and (wi::to_wide (@1), 1) == 1))
6522 tree itype = TREE_TYPE (@0);
6523 int p = TYPE_PRECISION (itype);
6524 wide_int m = wi::one (p + 1) << p;
6525 wide_int a = wide_int::from (wi::to_wide (@1), p + 1, UNSIGNED);
6526 wide_int i = wide_int::from (wi::mod_inv (a, m),
6527 p, TYPE_SIGN (itype));
6528 wide_int_to_tree (itype, wi::mul (i, wi::to_wide (@2)));
6531 /* Simplify comparison of something with itself. For IEEE
6532 floating-point, we can only do some of these simplifications. */
6536 (if (! FLOAT_TYPE_P (TREE_TYPE (@0))
6537 || ! tree_expr_maybe_nan_p (@0))
6538 { constant_boolean_node (true, type); }
6540 /* With -ftrapping-math conversion to EQ loses an exception. */
6541 && (! FLOAT_TYPE_P (TREE_TYPE (@0))
6542 || ! flag_trapping_math))
6548 || ! FLOAT_TYPE_P (TREE_TYPE (@0))
6549 || ! tree_expr_maybe_nan_p (@0))
6550 { constant_boolean_node (false, type); })))
6551 (for cmp (unle unge uneq)
6554 { constant_boolean_node (true, type); }))
6555 (for cmp (unlt ungt)
6561 (if (!flag_trapping_math || !tree_expr_maybe_nan_p (@0))
6562 { constant_boolean_node (false, type); }))
6564 /* x == ~x -> false */
6565 /* x != ~x -> true */
6568 (cmp:c @0 (bit_not @0))
6569 { constant_boolean_node (cmp == NE_EXPR, type); }))
6571 /* Fold ~X op ~Y as Y op X. */
6572 (for cmp (simple_comparison)
6574 (cmp (nop_convert1?@4 (bit_not@2 @0)) (nop_convert2? (bit_not@3 @1)))
6575 (if (single_use (@2) && single_use (@3))
6576 (with { tree otype = TREE_TYPE (@4); }
6577 (cmp (convert:otype @1) (convert:otype @0))))))
6579 /* Fold ~X op C as X op' ~C, where op' is the swapped comparison. */
6580 (for cmp (simple_comparison)
6581 scmp (swapped_simple_comparison)
6583 (cmp (nop_convert? (bit_not@2 @0)) CONSTANT_CLASS_P@1)
6584 (if (single_use (@2)
6585 && (TREE_CODE (@1) == INTEGER_CST || TREE_CODE (@1) == VECTOR_CST))
6586 (with { tree otype = TREE_TYPE (@1); }
6587 (scmp (convert:otype @0) (bit_not @1))))))
6589 (for cmp (simple_comparison)
6592 /* IEEE doesn't distinguish +0 and -0 in comparisons. */
6594 /* a CMP (-0) -> a CMP 0 */
6595 (if (REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@1)))
6596 (cmp @0 { build_real (TREE_TYPE (@1), dconst0); }))
6597 /* (-0) CMP b -> 0 CMP b. */
6598 (if (TREE_CODE (@0) == REAL_CST
6599 && REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@0)))
6600 (cmp { build_real (TREE_TYPE (@0), dconst0); } @1))
6601 /* x != NaN is always true, other ops are always false. */
6602 (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
6603 && (cmp == EQ_EXPR || cmp == NE_EXPR || !flag_trapping_math)
6604 && !tree_expr_signaling_nan_p (@1)
6605 && !tree_expr_maybe_signaling_nan_p (@0))
6606 { constant_boolean_node (cmp == NE_EXPR, type); })
6607 /* NaN != y is always true, other ops are always false. */
6608 (if (TREE_CODE (@0) == REAL_CST
6609 && REAL_VALUE_ISNAN (TREE_REAL_CST (@0))
6610 && (cmp == EQ_EXPR || cmp == NE_EXPR || !flag_trapping_math)
6611 && !tree_expr_signaling_nan_p (@0)
6612 && !tree_expr_signaling_nan_p (@1))
6613 { constant_boolean_node (cmp == NE_EXPR, type); })
6614 /* Fold comparisons against infinity. */
6615 (if (REAL_VALUE_ISINF (TREE_REAL_CST (@1))
6616 && MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (@1))))
6619 REAL_VALUE_TYPE max;
6620 enum tree_code code = cmp;
6621 bool neg = REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1));
6623 code = swap_tree_comparison (code);
6626 /* x > +Inf is always false, if we ignore NaNs or exceptions. */
6627 (if (code == GT_EXPR
6628 && !(HONOR_NANS (@0) && flag_trapping_math))
6629 { constant_boolean_node (false, type); })
6630 (if (code == LE_EXPR)
6631 /* x <= +Inf is always true, if we don't care about NaNs. */
6632 (if (! HONOR_NANS (@0))
6633 { constant_boolean_node (true, type); }
6634 /* x <= +Inf is the same as x == x, i.e. !isnan(x), but this loses
6635 an "invalid" exception. */
6636 (if (!flag_trapping_math)
6638 /* x == +Inf and x >= +Inf are always equal to x > DBL_MAX, but
6639 for == this introduces an exception for x a NaN. */
6640 (if ((code == EQ_EXPR && !(HONOR_NANS (@0) && flag_trapping_math))
6642 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
6644 (lt @0 { build_real (TREE_TYPE (@0), max); })
6645 (gt @0 { build_real (TREE_TYPE (@0), max); }))))
6646 /* x < +Inf is always equal to x <= DBL_MAX. */
6647 (if (code == LT_EXPR)
6648 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
6650 (ge @0 { build_real (TREE_TYPE (@0), max); })
6651 (le @0 { build_real (TREE_TYPE (@0), max); }))))
6652 /* x != +Inf is always equal to !(x > DBL_MAX), but this introduces
6653 an exception for x a NaN so use an unordered comparison. */
6654 (if (code == NE_EXPR)
6655 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
6656 (if (! HONOR_NANS (@0))
6658 (ge @0 { build_real (TREE_TYPE (@0), max); })
6659 (le @0 { build_real (TREE_TYPE (@0), max); }))
6661 (unge @0 { build_real (TREE_TYPE (@0), max); })
6662 (unle @0 { build_real (TREE_TYPE (@0), max); }))))))))))
6664 /* If this is a comparison of a real constant with a PLUS_EXPR
6665 or a MINUS_EXPR of a real constant, we can convert it into a
6666 comparison with a revised real constant as long as no overflow
6667 occurs when unsafe_math_optimizations are enabled. */
6668 (if (flag_unsafe_math_optimizations)
6669 (for op (plus minus)
6671 (cmp (op @0 REAL_CST@1) REAL_CST@2)
6674 tree tem = const_binop (op == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR,
6675 TREE_TYPE (@1), @2, @1);
6677 (if (tem && !TREE_OVERFLOW (tem))
6678 (cmp @0 { tem; }))))))
6680 /* Likewise, we can simplify a comparison of a real constant with
6681 a MINUS_EXPR whose first operand is also a real constant, i.e.
6682 (c1 - x) < c2 becomes x > c1-c2. Reordering is allowed on
6683 floating-point types only if -fassociative-math is set. */
6684 (if (flag_associative_math)
6686 (cmp (minus REAL_CST@0 @1) REAL_CST@2)
6687 (with { tree tem = const_binop (MINUS_EXPR, TREE_TYPE (@1), @0, @2); }
6688 (if (tem && !TREE_OVERFLOW (tem))
6689 (cmp { tem; } @1)))))
6691 /* Fold comparisons against built-in math functions. */
6692 (if (flag_unsafe_math_optimizations && ! flag_errno_math)
6695 (cmp (sq @0) REAL_CST@1)
6697 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
6699 /* sqrt(x) < y is always false, if y is negative. */
6700 (if (cmp == EQ_EXPR || cmp == LT_EXPR || cmp == LE_EXPR)
6701 { constant_boolean_node (false, type); })
6702 /* sqrt(x) > y is always true, if y is negative and we
6703 don't care about NaNs, i.e. negative values of x. */
6704 (if (cmp == NE_EXPR || !HONOR_NANS (@0))
6705 { constant_boolean_node (true, type); })
6706 /* sqrt(x) > y is the same as x >= 0, if y is negative. */
6707 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })))
6708 (if (real_equal (TREE_REAL_CST_PTR (@1), &dconst0))
6710 /* sqrt(x) < 0 is always false. */
6711 (if (cmp == LT_EXPR)
6712 { constant_boolean_node (false, type); })
6713 /* sqrt(x) >= 0 is always true if we don't care about NaNs. */
6714 (if (cmp == GE_EXPR && !HONOR_NANS (@0))
6715 { constant_boolean_node (true, type); })
6716 /* sqrt(x) <= 0 -> x == 0. */
6717 (if (cmp == LE_EXPR)
6719 /* Otherwise sqrt(x) cmp 0 -> x cmp 0. Here cmp can be >=, >,
6720 == or !=. In the last case:
6722 (sqrt(x) != 0) == (NaN != 0) == true == (x != 0)
6724 if x is negative or NaN. Due to -funsafe-math-optimizations,
6725 the results for other x follow from natural arithmetic. */
6727 (if ((cmp == LT_EXPR
6731 && !REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
6732 /* Give up for -frounding-math. */
6733 && !HONOR_SIGN_DEPENDENT_ROUNDING (TREE_TYPE (@0)))
6737 enum tree_code ncmp = cmp;
6738 const real_format *fmt
6739 = REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (@0)));
6740 real_arithmetic (&c2, MULT_EXPR,
6741 &TREE_REAL_CST (@1), &TREE_REAL_CST (@1));
6742 real_convert (&c2, fmt, &c2);
6743 /* See PR91734: if c2 is inexact and sqrt(c2) < c (or sqrt(c2) >= c),
6744 then change LT_EXPR into LE_EXPR or GE_EXPR into GT_EXPR. */
6745 if (!REAL_VALUE_ISINF (c2))
6747 tree c3 = fold_const_call (CFN_SQRT, TREE_TYPE (@0),
6748 build_real (TREE_TYPE (@0), c2));
6749 if (c3 == NULL_TREE || TREE_CODE (c3) != REAL_CST)
6751 else if ((cmp == LT_EXPR || cmp == GE_EXPR)
6752 && real_less (&TREE_REAL_CST (c3), &TREE_REAL_CST (@1)))
6753 ncmp = cmp == LT_EXPR ? LE_EXPR : GT_EXPR;
6754 else if ((cmp == LE_EXPR || cmp == GT_EXPR)
6755 && real_less (&TREE_REAL_CST (@1), &TREE_REAL_CST (c3)))
6756 ncmp = cmp == LE_EXPR ? LT_EXPR : GE_EXPR;
6759 /* With rounding to even, sqrt of up to 3 different values
6760 gives the same normal result, so in some cases c2 needs
6762 REAL_VALUE_TYPE c2alt, tow;
6763 if (cmp == LT_EXPR || cmp == GE_EXPR)
6767 real_nextafter (&c2alt, fmt, &c2, &tow);
6768 real_convert (&c2alt, fmt, &c2alt);
6769 if (REAL_VALUE_ISINF (c2alt))
6773 c3 = fold_const_call (CFN_SQRT, TREE_TYPE (@0),
6774 build_real (TREE_TYPE (@0), c2alt));
6775 if (c3 == NULL_TREE || TREE_CODE (c3) != REAL_CST)
6777 else if (real_equal (&TREE_REAL_CST (c3),
6778 &TREE_REAL_CST (@1)))
6784 (if (cmp == GT_EXPR || cmp == GE_EXPR)
6785 (if (REAL_VALUE_ISINF (c2))
6786 /* sqrt(x) > y is x == +Inf, when y is very large. */
6787 (if (HONOR_INFINITIES (@0))
6788 (eq @0 { build_real (TREE_TYPE (@0), c2); })
6789 { constant_boolean_node (false, type); })
6790 /* sqrt(x) > c is the same as x > c*c. */
6791 (if (ncmp != ERROR_MARK)
6792 (if (ncmp == GE_EXPR)
6793 (ge @0 { build_real (TREE_TYPE (@0), c2); })
6794 (gt @0 { build_real (TREE_TYPE (@0), c2); }))))
6795 /* else if (cmp == LT_EXPR || cmp == LE_EXPR) */
6796 (if (REAL_VALUE_ISINF (c2))
6798 /* sqrt(x) < y is always true, when y is a very large
6799 value and we don't care about NaNs or Infinities. */
6800 (if (! HONOR_NANS (@0) && ! HONOR_INFINITIES (@0))
6801 { constant_boolean_node (true, type); })
6802 /* sqrt(x) < y is x != +Inf when y is very large and we
6803 don't care about NaNs. */
6804 (if (! HONOR_NANS (@0))
6805 (ne @0 { build_real (TREE_TYPE (@0), c2); }))
6806 /* sqrt(x) < y is x >= 0 when y is very large and we
6807 don't care about Infinities. */
6808 (if (! HONOR_INFINITIES (@0))
6809 (ge @0 { build_real (TREE_TYPE (@0), dconst0); }))
6810 /* sqrt(x) < y is x >= 0 && x != +Inf, when y is large. */
6813 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
6814 (ne @0 { build_real (TREE_TYPE (@0), c2); }))))
6815 /* sqrt(x) < c is the same as x < c*c, if we ignore NaNs. */
6816 (if (ncmp != ERROR_MARK && ! HONOR_NANS (@0))
6817 (if (ncmp == LT_EXPR)
6818 (lt @0 { build_real (TREE_TYPE (@0), c2); })
6819 (le @0 { build_real (TREE_TYPE (@0), c2); }))
6820 /* sqrt(x) < c is the same as x >= 0 && x < c*c. */
6821 (if (ncmp != ERROR_MARK && GENERIC)
6822 (if (ncmp == LT_EXPR)
6824 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
6825 (lt @0 { build_real (TREE_TYPE (@0), c2); }))
6827 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
6828 (le @0 { build_real (TREE_TYPE (@0), c2); })))))))))))
6829 /* Transform sqrt(x) cmp sqrt(y) -> x cmp y. */
6831 (cmp (sq @0) (sq @1))
6832 (if (! HONOR_NANS (@0))
6835 /* Optimize various special cases of (FTYPE) N CMP (FTYPE) M. */
6836 (for cmp (lt le eq ne ge gt unordered ordered unlt unle ungt unge uneq ltgt)
6837 icmp (lt le eq ne ge gt unordered ordered lt le gt ge eq ne)
6839 (cmp (float@0 @1) (float @2))
6840 (if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (@0))
6841 && ! DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0)))
6844 format_helper fmt (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (@0))));
6845 tree type1 = TREE_TYPE (@1);
6846 bool type1_signed_p = TYPE_SIGN (type1) == SIGNED;
6847 tree type2 = TREE_TYPE (@2);
6848 bool type2_signed_p = TYPE_SIGN (type2) == SIGNED;
6850 (if (fmt.can_represent_integral_type_p (type1)
6851 && fmt.can_represent_integral_type_p (type2))
6852 (if (cmp == ORDERED_EXPR || cmp == UNORDERED_EXPR)
6853 { constant_boolean_node (cmp == ORDERED_EXPR, type); }
6854 (if (TYPE_PRECISION (type1) > TYPE_PRECISION (type2)
6855 && type1_signed_p >= type2_signed_p)
6856 (icmp @1 (convert @2))
6857 (if (TYPE_PRECISION (type1) < TYPE_PRECISION (type2)
6858 && type1_signed_p <= type2_signed_p)
6859 (icmp (convert:type2 @1) @2)
6860 (if (TYPE_PRECISION (type1) == TYPE_PRECISION (type2)
6861 && type1_signed_p == type2_signed_p)
6862 (icmp @1 @2))))))))))
6864 /* Optimize various special cases of (FTYPE) N CMP CST. */
6865 (for cmp (lt le eq ne ge gt)
6866 icmp (le le eq ne ge ge)
6868 (cmp (float @0) REAL_CST@1)
6869 (if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (@1))
6870 && ! DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1)))
6873 tree itype = TREE_TYPE (@0);
6874 format_helper fmt (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (@1))));
6875 const REAL_VALUE_TYPE *cst = TREE_REAL_CST_PTR (@1);
6876 /* Be careful to preserve any potential exceptions due to NaNs.
6877 qNaNs are ok in == or != context. */
6878 bool exception_p = real_isnan (cst)
6879 && flag_trapping_math
6880 && ((cmp != EQ_EXPR && cmp != NE_EXPR)
6882 && HONOR_SNANS (TREE_TYPE (@1))));
6884 /* TODO: allow non-fitting itype and SNaNs when
6885 -fno-trapping-math. */
6886 (if (fmt.can_represent_integral_type_p (itype) && ! exception_p)
6889 signop isign = TYPE_SIGN (itype);
6890 REAL_VALUE_TYPE imin, imax;
6891 real_from_integer (&imin, fmt, wi::min_value (itype), isign);
6892 real_from_integer (&imax, fmt, wi::max_value (itype), isign);
6894 REAL_VALUE_TYPE icst;
6895 if (cmp == GT_EXPR || cmp == GE_EXPR)
6896 real_ceil (&icst, fmt, cst);
6897 else if (cmp == LT_EXPR || cmp == LE_EXPR)
6898 real_floor (&icst, fmt, cst);
6900 real_trunc (&icst, fmt, cst);
6902 bool cst_int_p = !real_isnan (cst) && real_identical (&icst, cst);
6904 bool overflow_p = false;
6906 = real_to_integer (&icst, &overflow_p, TYPE_PRECISION (itype));
6909 /* Optimize cases when CST is outside of ITYPE's range. */
6910 (if (real_compare (LT_EXPR, cst, &imin))
6911 { constant_boolean_node (cmp == GT_EXPR || cmp == GE_EXPR || cmp == NE_EXPR,
6913 (if (real_compare (GT_EXPR, cst, &imax))
6914 { constant_boolean_node (cmp == LT_EXPR || cmp == LE_EXPR || cmp == NE_EXPR,
6916 /* Remove cast if CST is an integer representable by ITYPE. */
6918 (cmp @0 { gcc_assert (!overflow_p);
6919 wide_int_to_tree (itype, icst_val); })
6921 /* When CST is fractional, optimize
6922 (FTYPE) N == CST -> 0
6923 (FTYPE) N != CST -> 1. */
6924 (if (cmp == EQ_EXPR || cmp == NE_EXPR)
6925 { constant_boolean_node (cmp == NE_EXPR, type); })
6926 /* Otherwise replace with sensible integer constant. */
6929 gcc_checking_assert (!overflow_p);
6931 (icmp @0 { wide_int_to_tree (itype, icst_val); })))))))))
6933 /* Fold A /[ex] B CMP C to A CMP B * C. */
6936 (cmp (exact_div @0 @1) INTEGER_CST@2)
6937 (if (!integer_zerop (@1))
6938 (if (wi::to_wide (@2) == 0)
6940 (if (TREE_CODE (@1) == INTEGER_CST)
6943 wi::overflow_type ovf;
6944 wide_int prod = wi::mul (wi::to_wide (@2), wi::to_wide (@1),
6945 TYPE_SIGN (TREE_TYPE (@1)), &ovf);
6948 { constant_boolean_node (cmp == NE_EXPR, type); }
6949 (cmp @0 { wide_int_to_tree (TREE_TYPE (@0), prod); }))))))))
6950 (for cmp (lt le gt ge)
6952 (cmp (exact_div @0 INTEGER_CST@1) INTEGER_CST@2)
6953 (if (wi::gt_p (wi::to_wide (@1), 0, TYPE_SIGN (TREE_TYPE (@1))))
6956 wi::overflow_type ovf;
6957 wide_int prod = wi::mul (wi::to_wide (@2), wi::to_wide (@1),
6958 TYPE_SIGN (TREE_TYPE (@1)), &ovf);
6961 { constant_boolean_node (wi::lt_p (wi::to_wide (@2), 0,
6962 TYPE_SIGN (TREE_TYPE (@2)))
6963 != (cmp == LT_EXPR || cmp == LE_EXPR), type); }
6964 (cmp @0 { wide_int_to_tree (TREE_TYPE (@0), prod); }))))))
6966 /* Fold (size_t)(A /[ex] B) CMP C to (size_t)A CMP (size_t)B * C or A CMP' 0.
6968 For small C (less than max/B), this is (size_t)A CMP (size_t)B * C.
6969 For large C (more than min/B+2^size), this is also true, with the
6970 multiplication computed modulo 2^size.
6971 For intermediate C, this just tests the sign of A. */
6972 (for cmp (lt le gt ge)
6975 (cmp (convert (exact_div @0 INTEGER_CST@1)) INTEGER_CST@2)
6976 (if (tree_nop_conversion_p (TREE_TYPE (@0), TREE_TYPE (@2))
6977 && TYPE_UNSIGNED (TREE_TYPE (@2)) && !TYPE_UNSIGNED (TREE_TYPE (@0))
6978 && wi::gt_p (wi::to_wide (@1), 0, TYPE_SIGN (TREE_TYPE (@1))))
6981 tree utype = TREE_TYPE (@2);
6982 wide_int denom = wi::to_wide (@1);
6983 wide_int right = wi::to_wide (@2);
6984 wide_int smax = wi::sdiv_trunc (wi::max_value (TREE_TYPE (@0)), denom);
6985 wide_int smin = wi::sdiv_trunc (wi::min_value (TREE_TYPE (@0)), denom);
6986 bool small = wi::leu_p (right, smax);
6987 bool large = wi::geu_p (right, smin);
6989 (if (small || large)
6990 (cmp (convert:utype @0) (mult @2 (convert @1)))
6991 (cmp2 @0 { build_zero_cst (TREE_TYPE (@0)); }))))))
6993 /* Unordered tests if either argument is a NaN. */
6995 (bit_ior (unordered @0 @0) (unordered @1 @1))
6996 (if (types_match (@0, @1))
6999 (bit_and (ordered @0 @0) (ordered @1 @1))
7000 (if (types_match (@0, @1))
7003 (bit_ior:c (unordered @0 @0) (unordered:c@2 @0 @1))
7006 (bit_and:c (ordered @0 @0) (ordered:c@2 @0 @1))
7009 /* A & (2**N - 1) <= 2**K - 1 -> A & (2**N - 2**K) == 0
7010 A & (2**N - 1) > 2**K - 1 -> A & (2**N - 2**K) != 0
7012 Note that comparisons
7013 A & (2**N - 1) < 2**K -> A & (2**N - 2**K) == 0
7014 A & (2**N - 1) >= 2**K -> A & (2**N - 2**K) != 0
7015 will be canonicalized to above so there's no need to
7022 (cmp (bit_and@0 @1 INTEGER_CST@2) INTEGER_CST@3)
7023 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)))
7026 tree ty = TREE_TYPE (@0);
7027 unsigned prec = TYPE_PRECISION (ty);
7028 wide_int mask = wi::to_wide (@2, prec);
7029 wide_int rhs = wi::to_wide (@3, prec);
7030 signop sgn = TYPE_SIGN (ty);
7032 (if ((mask & (mask + 1)) == 0 && wi::gt_p (rhs, 0, sgn)
7033 && (rhs & (rhs + 1)) == 0 && wi::ge_p (mask, rhs, sgn))
7034 (eqcmp (bit_and @1 { wide_int_to_tree (ty, mask - rhs); })
7035 { build_zero_cst (ty); }))))))
7037 /* -A CMP -B -> B CMP A. */
7038 (for cmp (tcc_comparison)
7039 scmp (swapped_tcc_comparison)
7041 (cmp (negate @0) (negate @1))
7042 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
7043 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
7046 || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))))
7049 (cmp (negate @0) CONSTANT_CLASS_P@1)
7050 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
7051 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
7054 || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))))
7055 (with { tree tem = const_unop (NEGATE_EXPR, TREE_TYPE (@0), @1); }
7056 (if (tem && !TREE_OVERFLOW (tem))
7057 (scmp @0 { tem; }))))))
7059 /* Convert ABS[U]_EXPR<x> == 0 or ABS[U]_EXPR<x> != 0 to x == 0 or x != 0. */
7063 (eqne (op @0) zerop@1)
7064 (eqne @0 { build_zero_cst (TREE_TYPE (@0)); }))))
7066 /* From fold_sign_changed_comparison and fold_widened_comparison.
7067 FIXME: the lack of symmetry is disturbing. */
7068 (for cmp (simple_comparison)
7070 (cmp (convert@0 @00) (convert?@1 @10))
7071 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
7072 /* Disable this optimization if we're casting a function pointer
7073 type on targets that require function pointer canonicalization. */
7074 && !(targetm.have_canonicalize_funcptr_for_compare ()
7075 && ((POINTER_TYPE_P (TREE_TYPE (@00))
7076 && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@00))))
7077 || (POINTER_TYPE_P (TREE_TYPE (@10))
7078 && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@10))))))
7080 (if (TYPE_PRECISION (TREE_TYPE (@00)) == TYPE_PRECISION (TREE_TYPE (@0))
7081 && (TREE_CODE (@10) == INTEGER_CST
7083 && (TYPE_UNSIGNED (TREE_TYPE (@00)) == TYPE_UNSIGNED (TREE_TYPE (@0))
7086 && !POINTER_TYPE_P (TREE_TYPE (@00))
7087 /* (int)bool:32 != (int)uint is not the same as
7088 bool:32 != (bool:32)uint since boolean types only have two valid
7089 values independent of their precision. */
7090 && (TREE_CODE (TREE_TYPE (@00)) != BOOLEAN_TYPE
7091 || TREE_CODE (TREE_TYPE (@10)) == BOOLEAN_TYPE))
7092 /* ??? The special-casing of INTEGER_CST conversion was in the original
7093 code and here to avoid a spurious overflow flag on the resulting
7094 constant which fold_convert produces. */
7095 (if (TREE_CODE (@1) == INTEGER_CST)
7096 (cmp @00 { force_fit_type (TREE_TYPE (@00),
7097 wide_int::from (wi::to_wide (@1),
7098 MAX (TYPE_PRECISION (TREE_TYPE (@1)),
7099 TYPE_PRECISION (TREE_TYPE (@00))),
7100 TYPE_SIGN (TREE_TYPE (@1))),
7101 0, TREE_OVERFLOW (@1)); })
7102 (cmp @00 (convert @1)))
7104 (if (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (TREE_TYPE (@00)))
7105 /* If possible, express the comparison in the shorter mode. */
7106 (if ((cmp == EQ_EXPR || cmp == NE_EXPR
7107 || TYPE_UNSIGNED (TREE_TYPE (@0)) == TYPE_UNSIGNED (TREE_TYPE (@00))
7108 || (!TYPE_UNSIGNED (TREE_TYPE (@0))
7109 && TYPE_UNSIGNED (TREE_TYPE (@00))))
7110 && (types_match (TREE_TYPE (@10), TREE_TYPE (@00))
7111 || ((TYPE_PRECISION (TREE_TYPE (@00))
7112 >= TYPE_PRECISION (TREE_TYPE (@10)))
7113 && (TYPE_UNSIGNED (TREE_TYPE (@00))
7114 == TYPE_UNSIGNED (TREE_TYPE (@10))))
7115 || (TREE_CODE (@1) == INTEGER_CST
7116 && INTEGRAL_TYPE_P (TREE_TYPE (@00))
7117 && int_fits_type_p (@1, TREE_TYPE (@00)))))
7118 (cmp @00 (convert @10))
7119 (if (TREE_CODE (@1) == INTEGER_CST
7120 && INTEGRAL_TYPE_P (TREE_TYPE (@00))
7121 && !int_fits_type_p (@1, TREE_TYPE (@00)))
7124 tree min = lower_bound_in_type (TREE_TYPE (@10), TREE_TYPE (@00));
7125 tree max = upper_bound_in_type (TREE_TYPE (@10), TREE_TYPE (@00));
7126 bool above = integer_nonzerop (const_binop (LT_EXPR, type, max, @1));
7127 bool below = integer_nonzerop (const_binop (LT_EXPR, type, @1, min));
7129 (if (above || below)
7130 (if (cmp == EQ_EXPR || cmp == NE_EXPR)
7131 { constant_boolean_node (cmp == EQ_EXPR ? false : true, type); }
7132 (if (cmp == LT_EXPR || cmp == LE_EXPR)
7133 { constant_boolean_node (above ? true : false, type); }
7134 (if (cmp == GT_EXPR || cmp == GE_EXPR)
7135 { constant_boolean_node (above ? false : true, type); })))))))))
7136 /* Fold (double)float1 CMP (double)float2 into float1 CMP float2. */
7137 (if (FLOAT_TYPE_P (TREE_TYPE (@00))
7138 && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0))
7139 == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@00)))
7140 && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0))
7141 == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@10))))
7144 tree type1 = TREE_TYPE (@10);
7145 if (TREE_CODE (@10) == REAL_CST && !DECIMAL_FLOAT_TYPE_P (type1))
7147 REAL_VALUE_TYPE orig = TREE_REAL_CST (@10);
7148 if (TYPE_PRECISION (type1) > TYPE_PRECISION (float_type_node)
7149 && exact_real_truncate (TYPE_MODE (float_type_node), &orig))
7150 type1 = float_type_node;
7151 if (TYPE_PRECISION (type1) > TYPE_PRECISION (double_type_node)
7152 && exact_real_truncate (TYPE_MODE (double_type_node), &orig))
7153 type1 = double_type_node;
7156 = (element_precision (TREE_TYPE (@00)) > element_precision (type1)
7157 ? TREE_TYPE (@00) : type1);
7159 (if (element_precision (TREE_TYPE (@0)) > element_precision (newtype)
7160 && (!VECTOR_TYPE_P (type) || is_truth_type_for (newtype, type)))
7161 (cmp (convert:newtype @00) (convert:newtype @10))))))))
7166 /* SSA names are canonicalized to 2nd place. */
7167 (cmp addr@0 SSA_NAME@1)
7170 poly_int64 off; tree base;
7171 tree addr = (TREE_CODE (@0) == SSA_NAME
7172 ? gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0)) : @0);
7174 /* A local variable can never be pointed to by
7175 the default SSA name of an incoming parameter. */
7176 (if (SSA_NAME_IS_DEFAULT_DEF (@1)
7177 && TREE_CODE (SSA_NAME_VAR (@1)) == PARM_DECL
7178 && (base = get_base_address (TREE_OPERAND (addr, 0)))
7179 && TREE_CODE (base) == VAR_DECL
7180 && auto_var_in_fn_p (base, current_function_decl))
7181 (if (cmp == NE_EXPR)
7182 { constant_boolean_node (true, type); }
7183 { constant_boolean_node (false, type); })
7184 /* If the address is based on @1 decide using the offset. */
7185 (if ((base = get_addr_base_and_unit_offset (TREE_OPERAND (addr, 0), &off))
7186 && TREE_CODE (base) == MEM_REF
7187 && TREE_OPERAND (base, 0) == @1)
7188 (with { off += mem_ref_offset (base).force_shwi (); }
7189 (if (known_ne (off, 0))
7190 { constant_boolean_node (cmp == NE_EXPR, type); }
7191 (if (known_eq (off, 0))
7192 { constant_boolean_node (cmp == EQ_EXPR, type); }))))))))
7194 /* Equality compare simplifications from fold_binary */
7197 /* If we have (A | C) == D where C & ~D != 0, convert this into 0.
7198 Similarly for NE_EXPR. */
7200 (cmp (convert?@3 (bit_ior @0 INTEGER_CST@1)) INTEGER_CST@2)
7201 (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0))
7202 && wi::bit_and_not (wi::to_wide (@1), wi::to_wide (@2)) != 0)
7203 { constant_boolean_node (cmp == NE_EXPR, type); }))
7205 /* (X ^ Y) == 0 becomes X == Y, and (X ^ Y) != 0 becomes X != Y. */
7207 (cmp (bit_xor @0 @1) integer_zerop)
7210 /* (X ^ Y) == Y becomes X == 0.
7211 Likewise (X ^ Y) == X becomes Y == 0. */
7213 (cmp:c (bit_xor:c @0 @1) @0)
7214 (cmp @1 { build_zero_cst (TREE_TYPE (@1)); }))
7216 /* (X & Y) == X becomes (X & ~Y) == 0. */
7218 (cmp:c (bit_and:c @0 @1) @0)
7219 (cmp (bit_and @0 (bit_not! @1)) { build_zero_cst (TREE_TYPE (@0)); }))
7221 (cmp:c (convert@3 (bit_and (convert@2 @0) INTEGER_CST@1)) (convert @0))
7222 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
7223 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
7224 && INTEGRAL_TYPE_P (TREE_TYPE (@3))
7225 && TYPE_PRECISION (TREE_TYPE (@2)) == TYPE_PRECISION (TREE_TYPE (@0))
7226 && TYPE_PRECISION (TREE_TYPE (@3)) > TYPE_PRECISION (TREE_TYPE (@2))
7227 && !wi::neg_p (wi::to_wide (@1)))
7228 (cmp (bit_and @0 (convert (bit_not @1)))
7229 { build_zero_cst (TREE_TYPE (@0)); })))
7231 /* (X | Y) == Y becomes (X & ~Y) == 0. */
7233 (cmp:c (bit_ior:c @0 @1) @1)
7234 (cmp (bit_and @0 (bit_not! @1)) { build_zero_cst (TREE_TYPE (@0)); }))
7236 /* (X ^ C1) op C2 can be rewritten as X op (C1 ^ C2). */
7238 (cmp (convert?@3 (bit_xor @0 INTEGER_CST@1)) INTEGER_CST@2)
7239 (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0)))
7240 (cmp @0 (bit_xor @1 (convert @2)))))
7243 (cmp (nop_convert? @0) integer_zerop)
7244 (if (tree_expr_nonzero_p (@0))
7245 { constant_boolean_node (cmp == NE_EXPR, type); }))
7247 /* (X & C) op (Y & C) into (X ^ Y) & C op 0. */
7249 (cmp (bit_and:cs @0 @2) (bit_and:cs @1 @2))
7250 (cmp (bit_and (bit_xor @0 @1) @2) { build_zero_cst (TREE_TYPE (@2)); })))
7252 /* (X < 0) != (Y < 0) into (X ^ Y) < 0.
7253 (X >= 0) != (Y >= 0) into (X ^ Y) < 0.
7254 (X < 0) == (Y < 0) into (X ^ Y) >= 0.
7255 (X >= 0) == (Y >= 0) into (X ^ Y) >= 0. */
7260 (cmp (sgncmp @0 integer_zerop@2) (sgncmp @1 integer_zerop))
7261 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
7262 && !TYPE_UNSIGNED (TREE_TYPE (@0))
7263 && types_match (@0, @1))
7264 (ncmp (bit_xor @0 @1) @2)))))
7265 /* (X < 0) == (Y >= 0) into (X ^ Y) < 0.
7266 (X < 0) != (Y >= 0) into (X ^ Y) >= 0. */
7270 (cmp:c (lt @0 integer_zerop@2) (ge @1 integer_zerop))
7271 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
7272 && !TYPE_UNSIGNED (TREE_TYPE (@0))
7273 && types_match (@0, @1))
7274 (ncmp (bit_xor @0 @1) @2))))
7276 /* If we have (A & C) == C where C is a power of 2, convert this into
7277 (A & C) != 0. Similarly for NE_EXPR. */
7281 (cmp (bit_and@2 @0 integer_pow2p@1) @1)
7282 (icmp @2 { build_zero_cst (TREE_TYPE (@0)); })))
7285 /* From fold_binary_op_with_conditional_arg handle the case of
7286 rewriting (a ? b : c) > d to a ? (b > d) : (c > d) when the
7287 compares simplify. */
7288 (for cmp (simple_comparison)
7290 (cmp:c (cond @0 @1 @2) @3)
7291 /* Do not move possibly trapping operations into the conditional as this
7292 pessimizes code and causes gimplification issues when applied late. */
7293 (if (!FLOAT_TYPE_P (TREE_TYPE (@3))
7294 || !operation_could_trap_p (cmp, true, false, @3))
7295 (cond @0 (cmp! @1 @3) (cmp! @2 @3)))))
7299 /* x < 0 ? ~y : y into (x >> (prec-1)) ^ y. */
7300 /* x >= 0 ? ~y : y into ~((x >> (prec-1)) ^ y). */
7302 (cond (cmp @0 integer_zerop) (bit_not @1) @1)
7303 (if (INTEGRAL_TYPE_P (type)
7304 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
7305 && !TYPE_UNSIGNED (TREE_TYPE (@0))
7306 && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (type))
7309 tree shifter = build_int_cst (integer_type_node, TYPE_PRECISION (type) - 1);
7311 (if (cmp == LT_EXPR)
7312 (bit_xor (convert (rshift @0 {shifter;})) @1)
7313 (bit_not (bit_xor (convert (rshift @0 {shifter;})) @1))))))
7314 /* x < 0 ? y : ~y into ~((x >> (prec-1)) ^ y). */
7315 /* x >= 0 ? y : ~y into (x >> (prec-1)) ^ y. */
7317 (cond (cmp @0 integer_zerop) @1 (bit_not @1))
7318 (if (INTEGRAL_TYPE_P (type)
7319 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
7320 && !TYPE_UNSIGNED (TREE_TYPE (@0))
7321 && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (type))
7324 tree shifter = build_int_cst (integer_type_node, TYPE_PRECISION (type) - 1);
7326 (if (cmp == GE_EXPR)
7327 (bit_xor (convert (rshift @0 {shifter;})) @1)
7328 (bit_not (bit_xor (convert (rshift @0 {shifter;})) @1)))))))
7330 /* If we have (A & C) != 0 ? D : 0 where C and D are powers of 2,
7331 convert this into a shift followed by ANDing with D. */
7334 (ne (bit_and @0 integer_pow2p@1) integer_zerop)
7335 INTEGER_CST@2 integer_zerop)
7336 (if (!POINTER_TYPE_P (type) && integer_pow2p (@2))
7338 int shift = (wi::exact_log2 (wi::to_wide (@2))
7339 - wi::exact_log2 (wi::to_wide (@1)));
7343 (lshift (convert @0) { build_int_cst (integer_type_node, shift); }) @2)
7345 (convert (rshift @0 { build_int_cst (integer_type_node, -shift); }))
7348 /* If we have (A & C) != 0 where C is the sign bit of A, convert
7349 this into A < 0. Similarly for (A & C) == 0 into A >= 0. */
7353 (cmp (bit_and (convert?@2 @0) integer_pow2p@1) integer_zerop)
7354 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
7355 && type_has_mode_precision_p (TREE_TYPE (@0))
7356 && element_precision (@2) >= element_precision (@0)
7357 && wi::only_sign_bit_p (wi::to_wide (@1), element_precision (@0)))
7358 (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
7359 (ncmp (convert:stype @0) { build_zero_cst (stype); })))))
7361 /* If we have A < 0 ? C : 0 where C is a power of 2, convert
7362 this into a right shift or sign extension followed by ANDing with C. */
7365 (lt @0 integer_zerop)
7366 INTEGER_CST@1 integer_zerop)
7367 (if (integer_pow2p (@1)
7368 && !TYPE_UNSIGNED (TREE_TYPE (@0)))
7370 int shift = element_precision (@0) - wi::exact_log2 (wi::to_wide (@1)) - 1;
7374 (convert (rshift @0 { build_int_cst (integer_type_node, shift); }))
7376 /* Otherwise ctype must be wider than TREE_TYPE (@0) and pure
7377 sign extension followed by AND with C will achieve the effect. */
7378 (bit_and (convert @0) @1)))))
7380 /* When the addresses are not directly of decls compare base and offset.
7381 This implements some remaining parts of fold_comparison address
7382 comparisons but still no complete part of it. Still it is good
7383 enough to make fold_stmt not regress when not dispatching to fold_binary. */
7384 (for cmp (simple_comparison)
7386 (cmp (convert1?@2 addr@0) (convert2? addr@1))
7389 poly_int64 off0, off1;
7391 int equal = address_compare (cmp, TREE_TYPE (@2), @0, @1, base0, base1,
7392 off0, off1, GENERIC);
7396 (if (cmp == EQ_EXPR && (known_eq (off0, off1) || known_ne (off0, off1)))
7397 { constant_boolean_node (known_eq (off0, off1), type); })
7398 (if (cmp == NE_EXPR && (known_eq (off0, off1) || known_ne (off0, off1)))
7399 { constant_boolean_node (known_ne (off0, off1), type); })
7400 (if (cmp == LT_EXPR && (known_lt (off0, off1) || known_ge (off0, off1)))
7401 { constant_boolean_node (known_lt (off0, off1), type); })
7402 (if (cmp == LE_EXPR && (known_le (off0, off1) || known_gt (off0, off1)))
7403 { constant_boolean_node (known_le (off0, off1), type); })
7404 (if (cmp == GE_EXPR && (known_ge (off0, off1) || known_lt (off0, off1)))
7405 { constant_boolean_node (known_ge (off0, off1), type); })
7406 (if (cmp == GT_EXPR && (known_gt (off0, off1) || known_le (off0, off1)))
7407 { constant_boolean_node (known_gt (off0, off1), type); }))
7410 (if (cmp == EQ_EXPR)
7411 { constant_boolean_node (false, type); })
7412 (if (cmp == NE_EXPR)
7413 { constant_boolean_node (true, type); })))))))
7416 /* a?~t:t -> (-(a))^t */
7419 (with { bool wascmp; }
7420 (if (INTEGRAL_TYPE_P (type)
7421 && TYPE_UNSIGNED (TREE_TYPE (@0))
7422 && bitwise_inverted_equal_p (@1, @2, wascmp)
7423 && (!wascmp || TYPE_PRECISION (type) == 1))
7424 (if ((!TYPE_UNSIGNED (type) && TREE_CODE (type) == BOOLEAN_TYPE)
7425 || TYPE_PRECISION (type) == 1)
7426 (bit_xor (convert:type @0) @2)
7427 (bit_xor (negate (convert:type @0)) @2)))))
7430 /* Simplify pointer equality compares using PTA. */
7434 (if (POINTER_TYPE_P (TREE_TYPE (@0))
7435 && ptrs_compare_unequal (@0, @1))
7436 { constant_boolean_node (neeq != EQ_EXPR, type); })))
7438 /* PR70920: Transform (intptr_t)x eq/ne CST to x eq/ne (typeof x) CST.
7439 and (typeof ptr_cst) x eq/ne ptr_cst to x eq/ne (typeof x) CST.
7440 Disable the transform if either operand is pointer to function.
7441 This broke pr22051-2.c for arm where function pointer
7442 canonicalizaion is not wanted. */
7446 (cmp (convert @0) INTEGER_CST@1)
7447 (if (((POINTER_TYPE_P (TREE_TYPE (@0))
7448 && !FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@0)))
7449 && INTEGRAL_TYPE_P (TREE_TYPE (@1))
7450 /* Don't perform this optimization in GENERIC if @0 has reference
7451 type when sanitizing. See PR101210. */
7453 && TREE_CODE (TREE_TYPE (@0)) == REFERENCE_TYPE
7454 && (flag_sanitize & (SANITIZE_NULL | SANITIZE_ALIGNMENT))))
7455 || (INTEGRAL_TYPE_P (TREE_TYPE (@0))
7456 && POINTER_TYPE_P (TREE_TYPE (@1))
7457 && !FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@1)))))
7458 && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
7459 (cmp @0 (convert @1)))))
7461 /* Non-equality compare simplifications from fold_binary */
7462 (for cmp (lt gt le ge)
7463 /* Comparisons with the highest or lowest possible integer of
7464 the specified precision will have known values. */
7466 (cmp (convert?@2 @0) uniform_integer_cst_p@1)
7467 (if ((INTEGRAL_TYPE_P (TREE_TYPE (@1))
7468 || POINTER_TYPE_P (TREE_TYPE (@1))
7469 || VECTOR_INTEGER_TYPE_P (TREE_TYPE (@1)))
7470 && tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@0)))
7473 tree cst = uniform_integer_cst_p (@1);
7474 tree arg1_type = TREE_TYPE (cst);
7475 unsigned int prec = TYPE_PRECISION (arg1_type);
7476 wide_int max = wi::max_value (arg1_type);
7477 wide_int signed_max = wi::max_value (prec, SIGNED);
7478 wide_int min = wi::min_value (arg1_type);
7481 (if (wi::to_wide (cst) == max)
7483 (if (cmp == GT_EXPR)
7484 { constant_boolean_node (false, type); })
7485 (if (cmp == GE_EXPR)
7487 (if (cmp == LE_EXPR)
7488 { constant_boolean_node (true, type); })
7489 (if (cmp == LT_EXPR)
7491 (if (wi::to_wide (cst) == min)
7493 (if (cmp == LT_EXPR)
7494 { constant_boolean_node (false, type); })
7495 (if (cmp == LE_EXPR)
7497 (if (cmp == GE_EXPR)
7498 { constant_boolean_node (true, type); })
7499 (if (cmp == GT_EXPR)
7501 (if (wi::to_wide (cst) == max - 1)
7503 (if (cmp == GT_EXPR)
7504 (eq @2 { build_uniform_cst (TREE_TYPE (@1),
7505 wide_int_to_tree (TREE_TYPE (cst),
7508 (if (cmp == LE_EXPR)
7509 (ne @2 { build_uniform_cst (TREE_TYPE (@1),
7510 wide_int_to_tree (TREE_TYPE (cst),
7513 (if (wi::to_wide (cst) == min + 1)
7515 (if (cmp == GE_EXPR)
7516 (ne @2 { build_uniform_cst (TREE_TYPE (@1),
7517 wide_int_to_tree (TREE_TYPE (cst),
7520 (if (cmp == LT_EXPR)
7521 (eq @2 { build_uniform_cst (TREE_TYPE (@1),
7522 wide_int_to_tree (TREE_TYPE (cst),
7525 (if (wi::to_wide (cst) == signed_max
7526 && TYPE_UNSIGNED (arg1_type)
7527 && TYPE_MODE (arg1_type) != BLKmode
7528 /* We will flip the signedness of the comparison operator
7529 associated with the mode of @1, so the sign bit is
7530 specified by this mode. Check that @1 is the signed
7531 max associated with this sign bit. */
7532 && prec == GET_MODE_PRECISION (SCALAR_INT_TYPE_MODE (arg1_type))
7533 /* signed_type does not work on pointer types. */
7534 && INTEGRAL_TYPE_P (arg1_type))
7535 /* The following case also applies to X < signed_max+1
7536 and X >= signed_max+1 because previous transformations. */
7537 (if (cmp == LE_EXPR || cmp == GT_EXPR)
7538 (with { tree st = signed_type_for (TREE_TYPE (@1)); }
7540 (if (cst == @1 && cmp == LE_EXPR)
7541 (ge (convert:st @0) { build_zero_cst (st); }))
7542 (if (cst == @1 && cmp == GT_EXPR)
7543 (lt (convert:st @0) { build_zero_cst (st); }))
7544 (if (cmp == LE_EXPR)
7545 (ge (view_convert:st @0) { build_zero_cst (st); }))
7546 (if (cmp == GT_EXPR)
7547 (lt (view_convert:st @0) { build_zero_cst (st); })))))))))))
7549 /* unsigned < (typeof unsigned)(unsigned != 0) is always false. */
7551 (lt:c @0 (convert (ne @0 integer_zerop)))
7552 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
7553 { constant_boolean_node (false, type); }))
7555 /* x != (typeof x)(x == CST) -> CST == 0 ? 1 : (CST == 1 ? (x!=0&&x!=1) : x != 0) */
7556 /* x != (typeof x)(x != CST) -> CST == 1 ? 1 : (CST == 0 ? (x!=0&&x!=1) : x != 1) */
7557 /* x == (typeof x)(x == CST) -> CST == 0 ? 0 : (CST == 1 ? (x==0||x==1) : x == 0) */
7558 /* x == (typeof x)(x != CST) -> CST == 1 ? 0 : (CST == 0 ? (x==0||x==1) : x == 1) */
7562 (outer:c @0 (convert (inner @0 INTEGER_CST@1)))
7564 bool cst1 = integer_onep (@1);
7565 bool cst0 = integer_zerop (@1);
7566 bool innereq = inner == EQ_EXPR;
7567 bool outereq = outer == EQ_EXPR;
7570 (if (innereq ? cst0 : cst1)
7571 { constant_boolean_node (!outereq, type); })
7572 (if (innereq ? cst1 : cst0)
7574 tree utype = unsigned_type_for (TREE_TYPE (@0));
7575 tree ucst1 = build_one_cst (utype);
7578 (gt (convert:utype @0) { ucst1; })
7579 (le (convert:utype @0) { ucst1; })
7584 tree value = build_int_cst (TREE_TYPE (@0), !innereq);
7597 (for cmp (unordered ordered unlt unle ungt unge uneq ltgt)
7598 /* If the second operand is NaN, the result is constant. */
7601 (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
7602 && (cmp != LTGT_EXPR || ! flag_trapping_math))
7603 { constant_boolean_node (cmp == ORDERED_EXPR || cmp == LTGT_EXPR
7604 ? false : true, type); })))
7606 /* Fold UNORDERED if either operand must be NaN, or neither can be. */
7610 (if (tree_expr_nan_p (@0) || tree_expr_nan_p (@1))
7611 { constant_boolean_node (true, type); })
7612 (if (!tree_expr_maybe_nan_p (@0) && !tree_expr_maybe_nan_p (@1))
7613 { constant_boolean_node (false, type); })))
7615 /* Fold ORDERED if either operand must be NaN, or neither can be. */
7619 (if (tree_expr_nan_p (@0) || tree_expr_nan_p (@1))
7620 { constant_boolean_node (false, type); })
7621 (if (!tree_expr_maybe_nan_p (@0) && !tree_expr_maybe_nan_p (@1))
7622 { constant_boolean_node (true, type); })))
7624 /* bool_var != 0 becomes bool_var. */
7626 (ne @0 integer_zerop)
7627 (if (TREE_CODE (TREE_TYPE (@0)) == BOOLEAN_TYPE
7628 && types_match (type, TREE_TYPE (@0)))
7630 /* bool_var == 1 becomes bool_var. */
7632 (eq @0 integer_onep)
7633 (if (TREE_CODE (TREE_TYPE (@0)) == BOOLEAN_TYPE
7634 && types_match (type, TREE_TYPE (@0)))
7637 bool_var == 0 becomes !bool_var or
7638 bool_var != 1 becomes !bool_var
7639 here because that only is good in assignment context as long
7640 as we require a tcc_comparison in GIMPLE_CONDs where we'd
7641 replace if (x == 0) with tem = ~x; if (tem != 0) which is
7642 clearly less optimal and which we'll transform again in forwprop. */
7644 /* Transform comparisons of the form (X & Y) CMP 0 to X CMP2 Z
7645 where ~Y + 1 == pow2 and Z = ~Y. */
7646 (for cst (VECTOR_CST INTEGER_CST)
7650 (cmp (bit_and:c@2 @0 cst@1) integer_zerop)
7651 (with { tree csts = bitmask_inv_cst_vector_p (@1); }
7652 (if (csts && (VECTOR_TYPE_P (TREE_TYPE (@1)) || single_use (@2)))
7653 (with { auto optab = VECTOR_TYPE_P (TREE_TYPE (@1))
7654 ? optab_vector : optab_default;
7655 tree utype = unsigned_type_for (TREE_TYPE (@1)); }
7656 (if (target_supports_op_p (utype, icmp, optab)
7657 || (optimize_vectors_before_lowering_p ()
7658 && (!target_supports_op_p (type, cmp, optab)
7659 || !target_supports_op_p (type, BIT_AND_EXPR, optab))))
7660 (if (TYPE_UNSIGNED (TREE_TYPE (@1)))
7662 (icmp (view_convert:utype @0) { csts; })))))))))
7664 /* When one argument is a constant, overflow detection can be simplified.
7665 Currently restricted to single use so as not to interfere too much with
7666 ADD_OVERFLOW detection in tree-ssa-math-opts.cc.
7667 CONVERT?(CONVERT?(A) + CST) CMP A -> A CMP' CST' */
7668 (for cmp (lt le ge gt)
7671 (cmp:c (convert?@3 (plus@2 (convert?@4 @0) INTEGER_CST@1)) @0)
7672 (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@2))
7673 && types_match (TREE_TYPE (@0), TREE_TYPE (@3))
7674 && tree_nop_conversion_p (TREE_TYPE (@4), TREE_TYPE (@0))
7675 && wi::to_wide (@1) != 0
7678 unsigned int prec = TYPE_PRECISION (TREE_TYPE (@0));
7679 signop sign = TYPE_SIGN (TREE_TYPE (@0));
7681 (out @0 { wide_int_to_tree (TREE_TYPE (@0),
7682 wi::max_value (prec, sign)
7683 - wi::to_wide (@1)); })))))
7685 /* To detect overflow in unsigned A - B, A < B is simpler than A - B > A.
7686 However, the detection logic for SUB_OVERFLOW in tree-ssa-math-opts.cc
7687 expects the long form, so we restrict the transformation for now. */
7690 (cmp:c (minus@2 @0 @1) @0)
7691 (if (single_use (@2)
7692 && ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
7693 && TYPE_UNSIGNED (TREE_TYPE (@0)))
7696 /* Optimize A - B + -1 >= A into B >= A for unsigned comparisons. */
7699 (cmp:c (plus (minus @0 @1) integer_minus_onep) @0)
7700 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
7701 && TYPE_UNSIGNED (TREE_TYPE (@0)))
7704 /* Testing for overflow is unnecessary if we already know the result. */
7709 (cmp:c (realpart (IFN_SUB_OVERFLOW@2 @0 @1)) @0)
7710 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
7711 && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
7712 (out (imagpart @2) { build_zero_cst (TREE_TYPE (@0)); }))))
7717 (cmp:c (realpart (IFN_ADD_OVERFLOW:c@2 @0 @1)) @0)
7718 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
7719 && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
7720 (out (imagpart @2) { build_zero_cst (TREE_TYPE (@0)); }))))
7722 /* For unsigned operands, -1 / B < A checks whether A * B would overflow.
7723 Simplify it to __builtin_mul_overflow (A, B, <unused>). */
7727 (cmp:c (trunc_div:s integer_all_onesp @1) @0)
7728 (if (TYPE_UNSIGNED (TREE_TYPE (@0)) && !VECTOR_TYPE_P (TREE_TYPE (@0)))
7729 (with { tree t = TREE_TYPE (@0), cpx = build_complex_type (t); }
7730 (out (imagpart (IFN_MUL_OVERFLOW:cpx @0 @1)) { build_zero_cst (t); })))))
7732 /* Similarly, for unsigned operands, (((type) A * B) >> prec) != 0 where type
7733 is at least twice as wide as type of A and B, simplify to
7734 __builtin_mul_overflow (A, B, <unused>). */
7737 (cmp (rshift (mult:s (convert@3 @0) (convert @1)) INTEGER_CST@2)
7739 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
7740 && INTEGRAL_TYPE_P (TREE_TYPE (@3))
7741 && TYPE_UNSIGNED (TREE_TYPE (@0))
7742 && (TYPE_PRECISION (TREE_TYPE (@3))
7743 >= 2 * TYPE_PRECISION (TREE_TYPE (@0)))
7744 && tree_fits_uhwi_p (@2)
7745 && tree_to_uhwi (@2) == TYPE_PRECISION (TREE_TYPE (@0))
7746 && types_match (@0, @1)
7747 && type_has_mode_precision_p (TREE_TYPE (@0))
7748 && (optab_handler (umulv4_optab, TYPE_MODE (TREE_TYPE (@0)))
7749 != CODE_FOR_nothing))
7750 (with { tree t = TREE_TYPE (@0), cpx = build_complex_type (t); }
7751 (cmp (imagpart (IFN_MUL_OVERFLOW:cpx @0 @1)) { build_zero_cst (t); })))))
7753 /* Demote operands of IFN_{ADD,SUB,MUL}_OVERFLOW. */
7754 (for ovf (IFN_ADD_OVERFLOW IFN_SUB_OVERFLOW IFN_MUL_OVERFLOW)
7756 (ovf (convert@2 @0) @1)
7757 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
7758 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
7759 && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
7760 && (!TYPE_UNSIGNED (TREE_TYPE (@2)) || TYPE_UNSIGNED (TREE_TYPE (@0))))
7763 (ovf @1 (convert@2 @0))
7764 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
7765 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
7766 && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
7767 && (!TYPE_UNSIGNED (TREE_TYPE (@2)) || TYPE_UNSIGNED (TREE_TYPE (@0))))
7770 /* Optimize __builtin_mul_overflow_p (x, cst, (utype) 0) if all 3 types
7771 are unsigned to x > (umax / cst). Similarly for signed type, but
7772 in that case it needs to be outside of a range. */
7774 (imagpart (IFN_MUL_OVERFLOW:cs@2 @0 integer_nonzerop@1))
7775 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
7776 && TYPE_MAX_VALUE (TREE_TYPE (@0))
7777 && types_match (TREE_TYPE (@0), TREE_TYPE (TREE_TYPE (@2)))
7778 && int_fits_type_p (@1, TREE_TYPE (@0)))
7779 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
7780 (convert (gt @0 (trunc_div! { TYPE_MAX_VALUE (TREE_TYPE (@0)); } @1)))
7781 (if (TYPE_MIN_VALUE (TREE_TYPE (@0)))
7782 (if (integer_minus_onep (@1))
7783 (convert (eq @0 { TYPE_MIN_VALUE (TREE_TYPE (@0)); }))
7786 tree div = fold_convert (TREE_TYPE (@0), @1);
7787 tree lo = int_const_binop (TRUNC_DIV_EXPR,
7788 TYPE_MIN_VALUE (TREE_TYPE (@0)), div);
7789 tree hi = int_const_binop (TRUNC_DIV_EXPR,
7790 TYPE_MAX_VALUE (TREE_TYPE (@0)), div);
7791 tree etype = range_check_type (TREE_TYPE (@0));
7794 if (wi::neg_p (wi::to_wide (div)))
7796 lo = fold_convert (etype, lo);
7797 hi = fold_convert (etype, hi);
7798 hi = int_const_binop (MINUS_EXPR, hi, lo);
7802 (convert (gt (minus (convert:etype @0) { lo; }) { hi; })))))))))
7804 /* Simplification of math builtins. These rules must all be optimizations
7805 as well as IL simplifications. If there is a possibility that the new
7806 form could be a pessimization, the rule should go in the canonicalization
7807 section that follows this one.
7809 Rules can generally go in this section if they satisfy one of
7812 - the rule describes an identity
7814 - the rule replaces calls with something as simple as addition or
7817 - the rule contains unary calls only and simplifies the surrounding
7818 arithmetic. (The idea here is to exclude non-unary calls in which
7819 one operand is constant and in which the call is known to be cheap
7820 when the operand has that value.) */
7822 (if (flag_unsafe_math_optimizations)
7823 /* Simplify x / sqrt(x) -> sqrt(x). */
7825 (rdiv @0 (SQRT @0)) (SQRT @0))
7827 /* Simplify sqrt(x) * sqrt(x) -> x. */
7829 (mult (SQRT_ALL@1 @0) @1)
7830 (if (!tree_expr_maybe_signaling_nan_p (@0))
7833 (for op (plus minus)
7834 /* Simplify (A / C) +- (B / C) -> (A +- B) / C. */
7838 (rdiv (op @0 @2) @1)))
7840 (for cmp (lt le gt ge)
7841 neg_cmp (gt ge lt le)
7842 /* Simplify (x * C1) cmp C2 -> x cmp (C2 / C1), where C1 != 0. */
7844 (cmp (mult @0 REAL_CST@1) REAL_CST@2)
7846 { tree tem = const_binop (RDIV_EXPR, type, @2, @1); }
7848 && !(REAL_VALUE_ISINF (TREE_REAL_CST (tem))
7849 || (real_zerop (tem) && !real_zerop (@1))))
7851 (if (real_less (&dconst0, TREE_REAL_CST_PTR (@1)))
7853 (if (real_less (TREE_REAL_CST_PTR (@1), &dconst0))
7854 (neg_cmp @0 { tem; })))))))
7856 /* Simplify sqrt(x) * sqrt(y) -> sqrt(x*y). */
7857 (for root (SQRT CBRT)
7859 (mult (root:s @0) (root:s @1))
7860 (root (mult @0 @1))))
7862 /* Simplify expN(x) * expN(y) -> expN(x+y). */
7863 (for exps (EXP EXP2 EXP10 POW10)
7865 (mult (exps:s @0) (exps:s @1))
7866 (exps (plus @0 @1))))
7868 /* Simplify a/root(b/c) into a*root(c/b). */
7869 (for root (SQRT CBRT)
7871 (rdiv @0 (root:s (rdiv:s @1 @2)))
7872 (mult @0 (root (rdiv @2 @1)))))
7874 /* Simplify x/expN(y) into x*expN(-y). */
7875 (for exps (EXP EXP2 EXP10 POW10)
7877 (rdiv @0 (exps:s @1))
7878 (mult @0 (exps (negate @1)))))
7880 (for logs (LOG LOG2 LOG10 LOG10)
7881 exps (EXP EXP2 EXP10 POW10)
7882 /* logN(expN(x)) -> x. */
7886 /* expN(logN(x)) -> x. */
7891 /* Optimize logN(func()) for various exponential functions. We
7892 want to determine the value "x" and the power "exponent" in
7893 order to transform logN(x**exponent) into exponent*logN(x). */
7894 (for logs (LOG LOG LOG LOG2 LOG2 LOG2 LOG10 LOG10)
7895 exps (EXP2 EXP10 POW10 EXP EXP10 POW10 EXP EXP2)
7898 (if (SCALAR_FLOAT_TYPE_P (type))
7904 /* Prepare to do logN(exp(exponent)) -> exponent*logN(e). */
7905 x = build_real_truncate (type, dconst_e ());
7908 /* Prepare to do logN(exp2(exponent)) -> exponent*logN(2). */
7909 x = build_real (type, dconst2);
7913 /* Prepare to do logN(exp10(exponent)) -> exponent*logN(10). */
7915 REAL_VALUE_TYPE dconst10;
7916 real_from_integer (&dconst10, VOIDmode, 10, SIGNED);
7917 x = build_real (type, dconst10);
7924 (mult (logs { x; }) @0)))))
7932 (if (SCALAR_FLOAT_TYPE_P (type))
7938 /* Prepare to do logN(sqrt(x)) -> 0.5*logN(x). */
7939 x = build_real (type, dconsthalf);
7942 /* Prepare to do logN(cbrt(x)) -> (1/3)*logN(x). */
7943 x = build_real_truncate (type, dconst_third ());
7949 (mult { x; } (logs @0))))))
7951 /* logN(pow(x,exponent)) -> exponent*logN(x). */
7952 (for logs (LOG LOG2 LOG10)
7956 (mult @1 (logs @0))))
7958 /* pow(C,x) -> exp(log(C)*x) if C > 0,
7959 or if C is a positive power of 2,
7960 pow(C,x) -> exp2(log2(C)*x). */
7968 (pows REAL_CST@0 @1)
7969 (if (real_compare (GT_EXPR, TREE_REAL_CST_PTR (@0), &dconst0)
7970 && real_isfinite (TREE_REAL_CST_PTR (@0))
7971 /* As libmvec doesn't have a vectorized exp2, defer optimizing
7972 the use_exp2 case until after vectorization. It seems actually
7973 beneficial for all constants to postpone this until later,
7974 because exp(log(C)*x), while faster, will have worse precision
7975 and if x folds into a constant too, that is unnecessary
7977 && canonicalize_math_after_vectorization_p ())
7979 const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (@0);
7980 bool use_exp2 = false;
7981 if (targetm.libc_has_function (function_c99_misc, TREE_TYPE (@0))
7982 && value->cl == rvc_normal)
7984 REAL_VALUE_TYPE frac_rvt = *value;
7985 SET_REAL_EXP (&frac_rvt, 1);
7986 if (real_equal (&frac_rvt, &dconst1))
7991 (if (optimize_pow_to_exp (@0, @1))
7992 (exps (mult (logs @0) @1)))
7993 (exp2s (mult (log2s @0) @1)))))))
7996 /* pow(C,x)*expN(y) -> expN(logN(C)*x+y) if C > 0. */
7998 exps (EXP EXP2 EXP10 POW10)
7999 logs (LOG LOG2 LOG10 LOG10)
8001 (mult:c (pows:s REAL_CST@0 @1) (exps:s @2))
8002 (if (real_compare (GT_EXPR, TREE_REAL_CST_PTR (@0), &dconst0)
8003 && real_isfinite (TREE_REAL_CST_PTR (@0)))
8004 (exps (plus (mult (logs @0) @1) @2)))))
8009 exps (EXP EXP2 EXP10 POW10)
8010 /* sqrt(expN(x)) -> expN(x*0.5). */
8013 (exps (mult @0 { build_real (type, dconsthalf); })))
8014 /* cbrt(expN(x)) -> expN(x/3). */
8017 (exps (mult @0 { build_real_truncate (type, dconst_third ()); })))
8018 /* pow(expN(x), y) -> expN(x*y). */
8021 (exps (mult @0 @1))))
8023 /* tan(atan(x)) -> x. */
8030 /* Simplify sin(atan(x)) -> x / sqrt(x*x + 1). */
8034 copysigns (COPYSIGN)
8039 REAL_VALUE_TYPE r_cst;
8040 build_sinatan_real (&r_cst, type);
8041 tree t_cst = build_real (type, r_cst);
8042 tree t_one = build_one_cst (type);
8044 (if (SCALAR_FLOAT_TYPE_P (type))
8045 (cond (lt (abs @0) { t_cst; })
8046 (rdiv @0 (sqrts (plus (mult @0 @0) { t_one; })))
8047 (copysigns { t_one; } @0))))))
8049 /* Simplify cos(atan(x)) -> 1 / sqrt(x*x + 1). */
8053 copysigns (COPYSIGN)
8058 REAL_VALUE_TYPE r_cst;
8059 build_sinatan_real (&r_cst, type);
8060 tree t_cst = build_real (type, r_cst);
8061 tree t_one = build_one_cst (type);
8062 tree t_zero = build_zero_cst (type);
8064 (if (SCALAR_FLOAT_TYPE_P (type))
8065 (cond (lt (abs @0) { t_cst; })
8066 (rdiv { t_one; } (sqrts (plus (mult @0 @0) { t_one; })))
8067 (copysigns { t_zero; } @0))))))
8069 (if (!flag_errno_math)
8070 /* Simplify sinh(atanh(x)) -> x / sqrt((1 - x)*(1 + x)). */
8075 (sinhs (atanhs:s @0))
8076 (with { tree t_one = build_one_cst (type); }
8077 (rdiv @0 (sqrts (mult (minus { t_one; } @0) (plus { t_one; } @0)))))))
8079 /* Simplify cosh(atanh(x)) -> 1 / sqrt((1 - x)*(1 + x)) */
8084 (coshs (atanhs:s @0))
8085 (with { tree t_one = build_one_cst (type); }
8086 (rdiv { t_one; } (sqrts (mult (minus { t_one; } @0) (plus { t_one; } @0))))))))
8088 /* cabs(x+0i) or cabs(0+xi) -> abs(x). */
8090 (CABS (complex:C @0 real_zerop@1))
8093 /* trunc(trunc(x)) -> trunc(x), etc. */
8094 (for fns (TRUNC_ALL FLOOR_ALL CEIL_ALL ROUND_ALL NEARBYINT_ALL RINT_ALL)
8098 /* f(x) -> x if x is integer valued and f does nothing for such values. */
8099 (for fns (TRUNC_ALL FLOOR_ALL CEIL_ALL ROUND_ALL NEARBYINT_ALL RINT_ALL)
8101 (fns integer_valued_real_p@0)
8104 /* hypot(x,0) and hypot(0,x) -> abs(x). */
8106 (HYPOT:c @0 real_zerop@1)
8109 /* pow(1,x) -> 1. */
8111 (POW real_onep@0 @1)
8115 /* copysign(x,x) -> x. */
8116 (COPYSIGN_ALL @0 @0)
8120 /* copysign(x,-x) -> -x. */
8121 (COPYSIGN_ALL @0 (negate@1 @0))
8125 /* copysign(x,y) -> fabs(x) if y is nonnegative. */
8126 (COPYSIGN_ALL @0 tree_expr_nonnegative_p@1)
8130 /* fabs (copysign(x, y)) -> fabs (x). */
8131 (abs (COPYSIGN_ALL @0 @1))
8134 (for scale (LDEXP SCALBN SCALBLN)
8135 /* ldexp(0, x) -> 0. */
8137 (scale real_zerop@0 @1)
8139 /* ldexp(x, 0) -> x. */
8141 (scale @0 integer_zerop@1)
8143 /* ldexp(x, y) -> x if x is +-Inf or NaN. */
8145 (scale REAL_CST@0 @1)
8146 (if (!real_isfinite (TREE_REAL_CST_PTR (@0)))
8149 /* Canonicalization of sequences of math builtins. These rules represent
8150 IL simplifications but are not necessarily optimizations.
8152 The sincos pass is responsible for picking "optimal" implementations
8153 of math builtins, which may be more complicated and can sometimes go
8154 the other way, e.g. converting pow into a sequence of sqrts.
8155 We only want to do these canonicalizations before the pass has run. */
8157 (if (flag_unsafe_math_optimizations && canonicalize_math_p ())
8158 /* Simplify tan(x) * cos(x) -> sin(x). */
8160 (mult:c (TAN:s @0) (COS:s @0))
8163 /* Simplify x * pow(x,c) -> pow(x,c+1). */
8165 (mult:c @0 (POW:s @0 REAL_CST@1))
8166 (if (!TREE_OVERFLOW (@1))
8167 (POW @0 (plus @1 { build_one_cst (type); }))))
8169 /* Simplify sin(x) / cos(x) -> tan(x). */
8171 (rdiv (SIN:s @0) (COS:s @0))
8174 /* Simplify sinh(x) / cosh(x) -> tanh(x). */
8176 (rdiv (SINH:s @0) (COSH:s @0))
8179 /* Simplify tanh (x) / sinh (x) -> 1.0 / cosh (x). */
8181 (rdiv (TANH:s @0) (SINH:s @0))
8182 (rdiv {build_one_cst (type);} (COSH @0)))
8184 /* Simplify cos(x) / sin(x) -> 1 / tan(x). */
8186 (rdiv (COS:s @0) (SIN:s @0))
8187 (rdiv { build_one_cst (type); } (TAN @0)))
8189 /* Simplify sin(x) / tan(x) -> cos(x). */
8191 (rdiv (SIN:s @0) (TAN:s @0))
8192 (if (! HONOR_NANS (@0)
8193 && ! HONOR_INFINITIES (@0))
8196 /* Simplify tan(x) / sin(x) -> 1.0 / cos(x). */
8198 (rdiv (TAN:s @0) (SIN:s @0))
8199 (if (! HONOR_NANS (@0)
8200 && ! HONOR_INFINITIES (@0))
8201 (rdiv { build_one_cst (type); } (COS @0))))
8203 /* Simplify pow(x,y) * pow(x,z) -> pow(x,y+z). */
8205 (mult (POW:s @0 @1) (POW:s @0 @2))
8206 (POW @0 (plus @1 @2)))
8208 /* Simplify pow(x,y) * pow(z,y) -> pow(x*z,y). */
8210 (mult (POW:s @0 @1) (POW:s @2 @1))
8211 (POW (mult @0 @2) @1))
8213 /* Simplify powi(x,y) * powi(z,y) -> powi(x*z,y). */
8215 (mult (POWI:s @0 @1) (POWI:s @2 @1))
8216 (POWI (mult @0 @2) @1))
8218 /* Simplify pow(x,c) / x -> pow(x,c-1). */
8220 (rdiv (POW:s @0 REAL_CST@1) @0)
8221 (if (!TREE_OVERFLOW (@1))
8222 (POW @0 (minus @1 { build_one_cst (type); }))))
8224 /* Simplify x / pow (y,z) -> x * pow(y,-z). */
8226 (rdiv @0 (POW:s @1 @2))
8227 (mult @0 (POW @1 (negate @2))))
8232 /* sqrt(sqrt(x)) -> pow(x,1/4). */
8235 (pows @0 { build_real (type, dconst_quarter ()); }))
8236 /* sqrt(cbrt(x)) -> pow(x,1/6). */
8239 (pows @0 { build_real_truncate (type, dconst_sixth ()); }))
8240 /* cbrt(sqrt(x)) -> pow(x,1/6). */
8243 (pows @0 { build_real_truncate (type, dconst_sixth ()); }))
8244 /* cbrt(cbrt(x)) -> pow(x,1/9), iff x is nonnegative. */
8246 (cbrts (cbrts tree_expr_nonnegative_p@0))
8247 (pows @0 { build_real_truncate (type, dconst_ninth ()); }))
8248 /* sqrt(pow(x,y)) -> pow(|x|,y*0.5). */
8250 (sqrts (pows @0 @1))
8251 (pows (abs @0) (mult @1 { build_real (type, dconsthalf); })))
8252 /* cbrt(pow(x,y)) -> pow(x,y/3), iff x is nonnegative. */
8254 (cbrts (pows tree_expr_nonnegative_p@0 @1))
8255 (pows @0 (mult @1 { build_real_truncate (type, dconst_third ()); })))
8256 /* pow(sqrt(x),y) -> pow(x,y*0.5). */
8258 (pows (sqrts @0) @1)
8259 (pows @0 (mult @1 { build_real (type, dconsthalf); })))
8260 /* pow(cbrt(x),y) -> pow(x,y/3) iff x is nonnegative. */
8262 (pows (cbrts tree_expr_nonnegative_p@0) @1)
8263 (pows @0 (mult @1 { build_real_truncate (type, dconst_third ()); })))
8264 /* pow(pow(x,y),z) -> pow(x,y*z) iff x is nonnegative. */
8266 (pows (pows tree_expr_nonnegative_p@0 @1) @2)
8267 (pows @0 (mult @1 @2))))
8269 /* cabs(x+xi) -> fabs(x)*sqrt(2). */
8271 (CABS (complex @0 @0))
8272 (mult (abs @0) { build_real_truncate (type, dconst_sqrt2 ()); }))
8274 /* hypot(x,x) -> fabs(x)*sqrt(2). */
8277 (mult (abs @0) { build_real_truncate (type, dconst_sqrt2 ()); }))
8279 /* cexp(x+yi) -> exp(x)*cexpi(y). */
8284 (cexps compositional_complex@0)
8285 (if (targetm.libc_has_function (function_c99_math_complex, TREE_TYPE (@0)))
8287 (mult (exps@1 (realpart @0)) (realpart (cexpis:type@2 (imagpart @0))))
8288 (mult @1 (imagpart @2)))))))
8290 (if (canonicalize_math_p ())
8291 /* floor(x) -> trunc(x) if x is nonnegative. */
8292 (for floors (FLOOR_ALL)
8295 (floors tree_expr_nonnegative_p@0)
8298 (match double_value_p
8300 (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == double_type_node)))
8301 (for froms (BUILT_IN_TRUNCL
8313 /* truncl(extend(x)) -> extend(trunc(x)), etc., if x is a double. */
8314 (if (optimize && canonicalize_math_p ())
8316 (froms (convert double_value_p@0))
8317 (convert (tos @0)))))
8319 (match float_value_p
8321 (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == float_type_node)))
8322 (for froms (BUILT_IN_TRUNCL BUILT_IN_TRUNC
8323 BUILT_IN_FLOORL BUILT_IN_FLOOR
8324 BUILT_IN_CEILL BUILT_IN_CEIL
8325 BUILT_IN_ROUNDL BUILT_IN_ROUND
8326 BUILT_IN_NEARBYINTL BUILT_IN_NEARBYINT
8327 BUILT_IN_RINTL BUILT_IN_RINT)
8328 tos (BUILT_IN_TRUNCF BUILT_IN_TRUNCF
8329 BUILT_IN_FLOORF BUILT_IN_FLOORF
8330 BUILT_IN_CEILF BUILT_IN_CEILF
8331 BUILT_IN_ROUNDF BUILT_IN_ROUNDF
8332 BUILT_IN_NEARBYINTF BUILT_IN_NEARBYINTF
8333 BUILT_IN_RINTF BUILT_IN_RINTF)
8334 /* truncl(extend(x)) and trunc(extend(x)) -> extend(truncf(x)), etc.,
8336 (if (optimize && canonicalize_math_p ()
8337 && targetm.libc_has_function (function_c99_misc, NULL_TREE))
8339 (froms (convert float_value_p@0))
8340 (convert (tos @0)))))
8343 (match float16_value_p
8345 (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == float16_type_node)))
8346 (for froms (BUILT_IN_TRUNCL BUILT_IN_TRUNC BUILT_IN_TRUNCF
8347 BUILT_IN_FLOORL BUILT_IN_FLOOR BUILT_IN_FLOORF
8348 BUILT_IN_CEILL BUILT_IN_CEIL BUILT_IN_CEILF
8349 BUILT_IN_ROUNDEVENL BUILT_IN_ROUNDEVEN BUILT_IN_ROUNDEVENF
8350 BUILT_IN_ROUNDL BUILT_IN_ROUND BUILT_IN_ROUNDF
8351 BUILT_IN_NEARBYINTL BUILT_IN_NEARBYINT BUILT_IN_NEARBYINTF
8352 BUILT_IN_RINTL BUILT_IN_RINT BUILT_IN_RINTF
8353 BUILT_IN_SQRTL BUILT_IN_SQRT BUILT_IN_SQRTF)
8354 tos (IFN_TRUNC IFN_TRUNC IFN_TRUNC
8355 IFN_FLOOR IFN_FLOOR IFN_FLOOR
8356 IFN_CEIL IFN_CEIL IFN_CEIL
8357 IFN_ROUNDEVEN IFN_ROUNDEVEN IFN_ROUNDEVEN
8358 IFN_ROUND IFN_ROUND IFN_ROUND
8359 IFN_NEARBYINT IFN_NEARBYINT IFN_NEARBYINT
8360 IFN_RINT IFN_RINT IFN_RINT
8361 IFN_SQRT IFN_SQRT IFN_SQRT)
8362 /* (_Float16) round ((doube) x) -> __built_in_roundf16 (x), etc.,
8363 if x is a _Float16. */
8365 (convert (froms (convert float16_value_p@0)))
8367 && types_match (type, TREE_TYPE (@0))
8368 && direct_internal_fn_supported_p (as_internal_fn (tos),
8369 type, OPTIMIZE_FOR_BOTH))
8372 /* Simplify (trunc)copysign ((extend)x, (extend)y) to copysignf (x, y),
8373 x,y is float value, similar for _Float16/double. */
8374 (for copysigns (COPYSIGN_ALL)
8376 (convert (copysigns (convert@2 @0) (convert @1)))
8378 && !HONOR_SNANS (@2)
8379 && types_match (type, TREE_TYPE (@0))
8380 && types_match (type, TREE_TYPE (@1))
8381 && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (@2))
8382 && direct_internal_fn_supported_p (IFN_COPYSIGN,
8383 type, OPTIMIZE_FOR_BOTH))
8384 (IFN_COPYSIGN @0 @1))))
8386 (for froms (BUILT_IN_FMAF BUILT_IN_FMA BUILT_IN_FMAL)
8387 tos (IFN_FMA IFN_FMA IFN_FMA)
8389 (convert (froms (convert@3 @0) (convert @1) (convert @2)))
8390 (if (flag_unsafe_math_optimizations
8392 && FLOAT_TYPE_P (type)
8393 && FLOAT_TYPE_P (TREE_TYPE (@3))
8394 && types_match (type, TREE_TYPE (@0))
8395 && types_match (type, TREE_TYPE (@1))
8396 && types_match (type, TREE_TYPE (@2))
8397 && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (@3))
8398 && direct_internal_fn_supported_p (as_internal_fn (tos),
8399 type, OPTIMIZE_FOR_BOTH))
8402 (for maxmin (max min)
8404 (convert (maxmin (convert@2 @0) (convert @1)))
8406 && FLOAT_TYPE_P (type)
8407 && FLOAT_TYPE_P (TREE_TYPE (@2))
8408 && types_match (type, TREE_TYPE (@0))
8409 && types_match (type, TREE_TYPE (@1))
8410 && element_precision (type) < element_precision (TREE_TYPE (@2)))
8414 (for froms (XFLOORL XCEILL XROUNDL XRINTL)
8415 tos (XFLOOR XCEIL XROUND XRINT)
8416 /* llfloorl(extend(x)) -> llfloor(x), etc., if x is a double. */
8417 (if (optimize && canonicalize_math_p ())
8419 (froms (convert double_value_p@0))
8422 (for froms (XFLOORL XCEILL XROUNDL XRINTL
8423 XFLOOR XCEIL XROUND XRINT)
8424 tos (XFLOORF XCEILF XROUNDF XRINTF)
8425 /* llfloorl(extend(x)) and llfloor(extend(x)) -> llfloorf(x), etc.,
8427 (if (optimize && canonicalize_math_p ())
8429 (froms (convert float_value_p@0))
8432 (if (canonicalize_math_p ())
8433 /* xfloor(x) -> fix_trunc(x) if x is nonnegative. */
8434 (for floors (IFLOOR LFLOOR LLFLOOR)
8436 (floors tree_expr_nonnegative_p@0)
8439 (if (canonicalize_math_p ())
8440 /* xfloor(x) -> fix_trunc(x), etc., if x is integer valued. */
8441 (for fns (IFLOOR LFLOOR LLFLOOR
8443 IROUND LROUND LLROUND)
8445 (fns integer_valued_real_p@0)
8447 (if (!flag_errno_math)
8448 /* xrint(x) -> fix_trunc(x), etc., if x is integer valued. */
8449 (for rints (IRINT LRINT LLRINT)
8451 (rints integer_valued_real_p@0)
8454 (if (canonicalize_math_p ())
8455 (for ifn (IFLOOR ICEIL IROUND IRINT)
8456 lfn (LFLOOR LCEIL LROUND LRINT)
8457 llfn (LLFLOOR LLCEIL LLROUND LLRINT)
8458 /* Canonicalize iround (x) to lround (x) on ILP32 targets where
8459 sizeof (int) == sizeof (long). */
8460 (if (TYPE_PRECISION (integer_type_node)
8461 == TYPE_PRECISION (long_integer_type_node))
8464 (lfn:long_integer_type_node @0)))
8465 /* Canonicalize llround (x) to lround (x) on LP64 targets where
8466 sizeof (long long) == sizeof (long). */
8467 (if (TYPE_PRECISION (long_long_integer_type_node)
8468 == TYPE_PRECISION (long_integer_type_node))
8471 (lfn:long_integer_type_node @0)))))
8473 /* cproj(x) -> x if we're ignoring infinities. */
8476 (if (!HONOR_INFINITIES (type))
8479 /* If the real part is inf and the imag part is known to be
8480 nonnegative, return (inf + 0i). */
8482 (CPROJ (complex REAL_CST@0 tree_expr_nonnegative_p@1))
8483 (if (real_isinf (TREE_REAL_CST_PTR (@0)))
8484 { build_complex_inf (type, false); }))
8486 /* If the imag part is inf, return (inf+I*copysign(0,imag)). */
8488 (CPROJ (complex @0 REAL_CST@1))
8489 (if (real_isinf (TREE_REAL_CST_PTR (@1)))
8490 { build_complex_inf (type, TREE_REAL_CST_PTR (@1)->sign); }))
8496 (pows @0 REAL_CST@1)
8498 const REAL_VALUE_TYPE *value = TREE_REAL_CST_PTR (@1);
8499 REAL_VALUE_TYPE tmp;
8502 /* pow(x,0) -> 1. */
8503 (if (real_equal (value, &dconst0))
8504 { build_real (type, dconst1); })
8505 /* pow(x,1) -> x. */
8506 (if (real_equal (value, &dconst1))
8508 /* pow(x,-1) -> 1/x. */
8509 (if (real_equal (value, &dconstm1))
8510 (rdiv { build_real (type, dconst1); } @0))
8511 /* pow(x,0.5) -> sqrt(x). */
8512 (if (flag_unsafe_math_optimizations
8513 && canonicalize_math_p ()
8514 && real_equal (value, &dconsthalf))
8516 /* pow(x,1/3) -> cbrt(x). */
8517 (if (flag_unsafe_math_optimizations
8518 && canonicalize_math_p ()
8519 && (tmp = real_value_truncate (TYPE_MODE (type), dconst_third ()),
8520 real_equal (value, &tmp)))
8523 /* powi(1,x) -> 1. */
8525 (POWI real_onep@0 @1)
8529 (POWI @0 INTEGER_CST@1)
8531 /* powi(x,0) -> 1. */
8532 (if (wi::to_wide (@1) == 0)
8533 { build_real (type, dconst1); })
8534 /* powi(x,1) -> x. */
8535 (if (wi::to_wide (@1) == 1)
8537 /* powi(x,-1) -> 1/x. */
8538 (if (wi::to_wide (@1) == -1)
8539 (rdiv { build_real (type, dconst1); } @0))))
8541 /* Narrowing of arithmetic and logical operations.
8543 These are conceptually similar to the transformations performed for
8544 the C/C++ front-ends by shorten_binary_op and shorten_compare. Long
8545 term we want to move all that code out of the front-ends into here. */
8547 /* Convert (outertype)((innertype0)a+(innertype1)b)
8548 into ((newtype)a+(newtype)b) where newtype
8549 is the widest mode from all of these. */
8550 (for op (plus minus mult rdiv)
8552 (convert (op:s@0 (convert1?@3 @1) (convert2?@4 @2)))
8553 /* If we have a narrowing conversion of an arithmetic operation where
8554 both operands are widening conversions from the same type as the outer
8555 narrowing conversion. Then convert the innermost operands to a
8556 suitable unsigned type (to avoid introducing undefined behavior),
8557 perform the operation and convert the result to the desired type. */
8558 (if (INTEGRAL_TYPE_P (type)
8561 /* We check for type compatibility between @0 and @1 below,
8562 so there's no need to check that @2/@4 are integral types. */
8563 && INTEGRAL_TYPE_P (TREE_TYPE (@1))
8564 && INTEGRAL_TYPE_P (TREE_TYPE (@3))
8565 /* The precision of the type of each operand must match the
8566 precision of the mode of each operand, similarly for the
8568 && type_has_mode_precision_p (TREE_TYPE (@1))
8569 && type_has_mode_precision_p (TREE_TYPE (@2))
8570 && type_has_mode_precision_p (type)
8571 /* The inner conversion must be a widening conversion. */
8572 && TYPE_PRECISION (TREE_TYPE (@3)) > TYPE_PRECISION (TREE_TYPE (@1))
8573 && types_match (@1, type)
8574 && (types_match (@1, @2)
8575 /* Or the second operand is const integer or converted const
8576 integer from valueize. */
8577 || poly_int_tree_p (@4)))
8578 (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
8579 (op @1 (convert @2))
8580 (with { tree utype = unsigned_type_for (TREE_TYPE (@1)); }
8581 (convert (op (convert:utype @1)
8582 (convert:utype @2)))))
8583 (if (FLOAT_TYPE_P (type)
8584 && DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0))
8585 == DECIMAL_FLOAT_TYPE_P (type))
8586 (with { tree arg0 = strip_float_extensions (@1);
8587 tree arg1 = strip_float_extensions (@2);
8588 tree itype = TREE_TYPE (@0);
8589 tree ty1 = TREE_TYPE (arg0);
8590 tree ty2 = TREE_TYPE (arg1);
8591 enum tree_code code = TREE_CODE (itype); }
8592 (if (FLOAT_TYPE_P (ty1)
8593 && FLOAT_TYPE_P (ty2))
8594 (with { tree newtype = type;
8595 if (TYPE_MODE (ty1) == SDmode
8596 || TYPE_MODE (ty2) == SDmode
8597 || TYPE_MODE (type) == SDmode)
8598 newtype = dfloat32_type_node;
8599 if (TYPE_MODE (ty1) == DDmode
8600 || TYPE_MODE (ty2) == DDmode
8601 || TYPE_MODE (type) == DDmode)
8602 newtype = dfloat64_type_node;
8603 if (TYPE_MODE (ty1) == TDmode
8604 || TYPE_MODE (ty2) == TDmode
8605 || TYPE_MODE (type) == TDmode)
8606 newtype = dfloat128_type_node; }
8607 (if ((newtype == dfloat32_type_node
8608 || newtype == dfloat64_type_node
8609 || newtype == dfloat128_type_node)
8611 && types_match (newtype, type))
8612 (op (convert:newtype @1) (convert:newtype @2))
8613 (with { if (element_precision (ty1) > element_precision (newtype))
8615 if (element_precision (ty2) > element_precision (newtype))
8617 /* Sometimes this transformation is safe (cannot
8618 change results through affecting double rounding
8619 cases) and sometimes it is not. If NEWTYPE is
8620 wider than TYPE, e.g. (float)((long double)double
8621 + (long double)double) converted to
8622 (float)(double + double), the transformation is
8623 unsafe regardless of the details of the types
8624 involved; double rounding can arise if the result
8625 of NEWTYPE arithmetic is a NEWTYPE value half way
8626 between two representable TYPE values but the
8627 exact value is sufficiently different (in the
8628 right direction) for this difference to be
8629 visible in ITYPE arithmetic. If NEWTYPE is the
8630 same as TYPE, however, the transformation may be
8631 safe depending on the types involved: it is safe
8632 if the ITYPE has strictly more than twice as many
8633 mantissa bits as TYPE, can represent infinities
8634 and NaNs if the TYPE can, and has sufficient
8635 exponent range for the product or ratio of two
8636 values representable in the TYPE to be within the
8637 range of normal values of ITYPE. */
8638 (if (element_precision (newtype) < element_precision (itype)
8639 && (!VECTOR_MODE_P (TYPE_MODE (newtype))
8640 || target_supports_op_p (newtype, op, optab_default))
8641 && (flag_unsafe_math_optimizations
8642 || (element_precision (newtype) == element_precision (type)
8643 && real_can_shorten_arithmetic (element_mode (itype),
8644 element_mode (type))
8645 && !excess_precision_type (newtype)))
8646 && !types_match (itype, newtype))
8647 (convert:type (op (convert:newtype @1)
8648 (convert:newtype @2)))
8653 /* This is another case of narrowing, specifically when there's an outer
8654 BIT_AND_EXPR which masks off bits outside the type of the innermost
8655 operands. Like the previous case we have to convert the operands
8656 to unsigned types to avoid introducing undefined behavior for the
8657 arithmetic operation. */
8658 (for op (minus plus)
8660 (bit_and (op:s (convert@2 @0) (convert@3 @1)) INTEGER_CST@4)
8661 (if (INTEGRAL_TYPE_P (type)
8662 /* We check for type compatibility between @0 and @1 below,
8663 so there's no need to check that @1/@3 are integral types. */
8664 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
8665 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
8666 /* The precision of the type of each operand must match the
8667 precision of the mode of each operand, similarly for the
8669 && type_has_mode_precision_p (TREE_TYPE (@0))
8670 && type_has_mode_precision_p (TREE_TYPE (@1))
8671 && type_has_mode_precision_p (type)
8672 /* The inner conversion must be a widening conversion. */
8673 && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
8674 && types_match (@0, @1)
8675 && (tree_int_cst_min_precision (@4, TYPE_SIGN (TREE_TYPE (@0)))
8676 <= TYPE_PRECISION (TREE_TYPE (@0)))
8677 && (wi::to_wide (@4)
8678 & wi::mask (TYPE_PRECISION (TREE_TYPE (@0)),
8679 true, TYPE_PRECISION (type))) == 0)
8680 (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
8681 (with { tree ntype = TREE_TYPE (@0); }
8682 (convert (bit_and (op @0 @1) (convert:ntype @4))))
8683 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
8684 (convert (bit_and (op (convert:utype @0) (convert:utype @1))
8685 (convert:utype @4))))))))
8687 /* Transform (@0 < @1 and @0 < @2) to use min,
8688 (@0 > @1 and @0 > @2) to use max */
8689 (for logic (bit_and bit_and bit_and bit_and bit_ior bit_ior bit_ior bit_ior)
8690 op (lt le gt ge lt le gt ge )
8691 ext (min min max max max max min min )
8693 (logic (op:cs @0 @1) (op:cs @0 @2))
8694 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
8695 && TREE_CODE (@0) != INTEGER_CST)
8696 (op @0 (ext @1 @2)))))
8698 /* Max<bool0, bool1> -> bool0 | bool1
8699 Min<bool0, bool1> -> bool0 & bool1 */
8701 logic (bit_ior bit_and)
8703 (op zero_one_valued_p@0 zero_one_valued_p@1)
8706 /* signbit(x) != 0 ? -x : x -> abs(x)
8707 signbit(x) == 0 ? -x : x -> -abs(x) */
8711 (cond (neeq (sign @0) integer_zerop) (negate @0) @0)
8712 (if (neeq == NE_EXPR)
8714 (negate (abs @0))))))
8717 /* signbit(x) -> 0 if x is nonnegative. */
8718 (SIGNBIT tree_expr_nonnegative_p@0)
8719 { integer_zero_node; })
8722 /* signbit(x) -> x<0 if x doesn't have signed zeros. */
8724 (if (!HONOR_SIGNED_ZEROS (@0))
8725 (convert (lt @0 { build_real (TREE_TYPE (@0), dconst0); }))))
8727 /* Transform comparisons of the form X +- C1 CMP C2 to X CMP C2 -+ C1. */
8729 (for op (plus minus)
8732 (cmp (op@3 @0 INTEGER_CST@1) INTEGER_CST@2)
8733 (if (!TREE_OVERFLOW (@1) && !TREE_OVERFLOW (@2)
8734 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0))
8735 && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@0))
8736 && !TYPE_SATURATING (TREE_TYPE (@0)))
8737 (with { tree res = int_const_binop (rop, @2, @1); }
8738 (if (TREE_OVERFLOW (res)
8739 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
8740 { constant_boolean_node (cmp == NE_EXPR, type); }
8741 (if (single_use (@3))
8742 (cmp @0 { TREE_OVERFLOW (res)
8743 ? drop_tree_overflow (res) : res; }))))))))
8744 (for cmp (lt le gt ge)
8745 (for op (plus minus)
8748 (cmp (op@3 @0 INTEGER_CST@1) INTEGER_CST@2)
8749 (if (!TREE_OVERFLOW (@1) && !TREE_OVERFLOW (@2)
8750 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
8751 (with { tree res = int_const_binop (rop, @2, @1); }
8752 (if (TREE_OVERFLOW (res))
8754 fold_overflow_warning (("assuming signed overflow does not occur "
8755 "when simplifying conditional to constant"),
8756 WARN_STRICT_OVERFLOW_CONDITIONAL);
8757 bool less = cmp == LE_EXPR || cmp == LT_EXPR;
8758 /* wi::ges_p (@2, 0) should be sufficient for a signed type. */
8759 bool ovf_high = wi::lt_p (wi::to_wide (@1), 0,
8760 TYPE_SIGN (TREE_TYPE (@1)))
8761 != (op == MINUS_EXPR);
8762 constant_boolean_node (less == ovf_high, type);
8764 (if (single_use (@3))
8767 fold_overflow_warning (("assuming signed overflow does not occur "
8768 "when changing X +- C1 cmp C2 to "
8770 WARN_STRICT_OVERFLOW_COMPARISON);
8772 (cmp @0 { res; })))))))))
8774 /* Canonicalizations of BIT_FIELD_REFs. */
8777 (BIT_FIELD_REF (BIT_FIELD_REF @0 @1 @2) @3 @4)
8778 (BIT_FIELD_REF @0 @3 { const_binop (PLUS_EXPR, bitsizetype, @2, @4); }))
8781 (BIT_FIELD_REF (view_convert @0) @1 @2)
8782 (if (! INTEGRAL_TYPE_P (TREE_TYPE (@0))
8783 || type_has_mode_precision_p (TREE_TYPE (@0)))
8784 (BIT_FIELD_REF @0 @1 @2)))
8787 (BIT_FIELD_REF @0 @1 integer_zerop)
8788 (if (tree_int_cst_equal (@1, TYPE_SIZE (TREE_TYPE (@0))))
8792 (BIT_FIELD_REF @0 @1 @2)
8794 (if (TREE_CODE (TREE_TYPE (@0)) == COMPLEX_TYPE
8795 && tree_int_cst_equal (@1, TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))))
8797 (if (integer_zerop (@2))
8798 (view_convert (realpart @0)))
8799 (if (tree_int_cst_equal (@2, TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))))
8800 (view_convert (imagpart @0)))))
8801 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
8802 && INTEGRAL_TYPE_P (type)
8803 /* On GIMPLE this should only apply to register arguments. */
8804 && (! GIMPLE || is_gimple_reg (@0))
8805 /* A bit-field-ref that referenced the full argument can be stripped. */
8806 && ((compare_tree_int (@1, TYPE_PRECISION (TREE_TYPE (@0))) == 0
8807 && integer_zerop (@2))
8808 /* Low-parts can be reduced to integral conversions.
8809 ??? The following doesn't work for PDP endian. */
8810 || (BYTES_BIG_ENDIAN == WORDS_BIG_ENDIAN
8811 /* But only do this after vectorization. */
8812 && canonicalize_math_after_vectorization_p ()
8813 /* Don't even think about BITS_BIG_ENDIAN. */
8814 && TYPE_PRECISION (TREE_TYPE (@0)) % BITS_PER_UNIT == 0
8815 && TYPE_PRECISION (type) % BITS_PER_UNIT == 0
8816 && compare_tree_int (@2, (BYTES_BIG_ENDIAN
8817 ? (TYPE_PRECISION (TREE_TYPE (@0))
8818 - TYPE_PRECISION (type))
8822 /* Simplify vector extracts. */
8825 (BIT_FIELD_REF CONSTRUCTOR@0 @1 @2)
8826 (if (VECTOR_TYPE_P (TREE_TYPE (@0))
8827 && tree_fits_uhwi_p (TYPE_SIZE (type))
8828 && ((tree_to_uhwi (TYPE_SIZE (type))
8829 == tree_to_uhwi (TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))))
8830 || (VECTOR_TYPE_P (type)
8831 && (tree_to_uhwi (TYPE_SIZE (TREE_TYPE (type)))
8832 == tree_to_uhwi (TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0))))))))
8835 tree ctor = (TREE_CODE (@0) == SSA_NAME
8836 ? gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0)) : @0);
8837 tree eltype = TREE_TYPE (TREE_TYPE (ctor));
8838 unsigned HOST_WIDE_INT width = tree_to_uhwi (TYPE_SIZE (eltype));
8839 unsigned HOST_WIDE_INT n = tree_to_uhwi (@1);
8840 unsigned HOST_WIDE_INT idx = tree_to_uhwi (@2);
8843 && (idx % width) == 0
8845 && known_le ((idx + n) / width,
8846 TYPE_VECTOR_SUBPARTS (TREE_TYPE (ctor))))
8851 /* Constructor elements can be subvectors. */
8853 if (CONSTRUCTOR_NELTS (ctor) != 0)
8855 tree cons_elem = TREE_TYPE (CONSTRUCTOR_ELT (ctor, 0)->value);
8856 if (TREE_CODE (cons_elem) == VECTOR_TYPE)
8857 k = TYPE_VECTOR_SUBPARTS (cons_elem);
8859 unsigned HOST_WIDE_INT elt, count, const_k;
8862 /* We keep an exact subset of the constructor elements. */
8863 (if (multiple_p (idx, k, &elt) && multiple_p (n, k, &count))
8864 (if (CONSTRUCTOR_NELTS (ctor) == 0)
8865 { build_zero_cst (type); }
8867 (if (elt < CONSTRUCTOR_NELTS (ctor))
8868 (view_convert { CONSTRUCTOR_ELT (ctor, elt)->value; })
8869 { build_zero_cst (type); })
8870 /* We don't want to emit new CTORs unless the old one goes away.
8871 ??? Eventually allow this if the CTOR ends up constant or
8873 (if (single_use (@0))
8876 vec<constructor_elt, va_gc> *vals;
8877 vec_alloc (vals, count);
8878 bool constant_p = true;
8880 for (unsigned i = 0;
8881 i < count && elt + i < CONSTRUCTOR_NELTS (ctor); ++i)
8883 tree e = CONSTRUCTOR_ELT (ctor, elt + i)->value;
8884 CONSTRUCTOR_APPEND_ELT (vals, NULL_TREE, e);
8885 if (!CONSTANT_CLASS_P (e))
8888 tree evtype = (types_match (TREE_TYPE (type),
8889 TREE_TYPE (TREE_TYPE (ctor)))
8891 : build_vector_type (TREE_TYPE (TREE_TYPE (ctor)),
8893 /* We used to build a CTOR in the non-constant case here
8894 but that's not a GIMPLE value. We'd have to expose this
8895 operation somehow so the code generation can properly
8896 split it out to a separate stmt. */
8897 res = (constant_p ? build_vector_from_ctor (evtype, vals)
8898 : (GIMPLE ? NULL_TREE : build_constructor (evtype, vals)));
8901 (view_convert { res; })))))))
8902 /* The bitfield references a single constructor element. */
8903 (if (k.is_constant (&const_k)
8904 && idx + n <= (idx / const_k + 1) * const_k)
8906 (if (CONSTRUCTOR_NELTS (ctor) <= idx / const_k)
8907 { build_zero_cst (type); })
8909 (view_convert { CONSTRUCTOR_ELT (ctor, idx / const_k)->value; }))
8910 (BIT_FIELD_REF { CONSTRUCTOR_ELT (ctor, idx / const_k)->value; }
8911 @1 { bitsize_int ((idx % const_k) * width); })))))))))
8913 /* Simplify a bit extraction from a bit insertion for the cases with
8914 the inserted element fully covering the extraction or the insertion
8915 not touching the extraction. */
8917 (BIT_FIELD_REF (bit_insert @0 @1 @ipos) @rsize @rpos)
8920 unsigned HOST_WIDE_INT isize;
8921 if (INTEGRAL_TYPE_P (TREE_TYPE (@1)))
8922 isize = TYPE_PRECISION (TREE_TYPE (@1));
8924 isize = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (@1)));
8927 (if ((!INTEGRAL_TYPE_P (TREE_TYPE (@1))
8928 || type_has_mode_precision_p (TREE_TYPE (@1)))
8929 && wi::leu_p (wi::to_wide (@ipos), wi::to_wide (@rpos))
8930 && wi::leu_p (wi::to_wide (@rpos) + wi::to_wide (@rsize),
8931 wi::to_wide (@ipos) + isize))
8932 (BIT_FIELD_REF @1 @rsize { wide_int_to_tree (bitsizetype,
8934 - wi::to_wide (@ipos)); }))
8935 (if (wi::eq_p (wi::to_wide (@ipos), wi::to_wide (@rpos))
8936 && compare_tree_int (@rsize, isize) == 0)
8938 (if (wi::geu_p (wi::to_wide (@ipos),
8939 wi::to_wide (@rpos) + wi::to_wide (@rsize))
8940 || wi::geu_p (wi::to_wide (@rpos),
8941 wi::to_wide (@ipos) + isize))
8942 (BIT_FIELD_REF @0 @rsize @rpos)))))
8944 /* Simplify vector inserts of other vector extracts to a permute. */
8946 (bit_insert @0 (BIT_FIELD_REF@2 @1 @rsize @rpos) @ipos)
8947 (if (VECTOR_TYPE_P (type)
8948 && (VECTOR_MODE_P (TYPE_MODE (type))
8949 || optimize_vectors_before_lowering_p ())
8950 && types_match (@0, @1)
8951 && types_match (TREE_TYPE (TREE_TYPE (@0)), TREE_TYPE (@2))
8952 && TYPE_VECTOR_SUBPARTS (type).is_constant ()
8953 && multiple_p (wi::to_poly_offset (@rpos),
8954 wi::to_poly_offset (TYPE_SIZE (TREE_TYPE (type)))))
8957 unsigned HOST_WIDE_INT elsz
8958 = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (TREE_TYPE (@1))));
8959 poly_uint64 relt = exact_div (tree_to_poly_uint64 (@rpos), elsz);
8960 poly_uint64 ielt = exact_div (tree_to_poly_uint64 (@ipos), elsz);
8961 unsigned nunits = TYPE_VECTOR_SUBPARTS (type).to_constant ();
8962 vec_perm_builder builder;
8963 builder.new_vector (nunits, nunits, 1);
8964 for (unsigned i = 0; i < nunits; ++i)
8965 builder.quick_push (known_eq (ielt, i) ? nunits + relt : i);
8966 vec_perm_indices sel (builder, 2, nunits);
8968 (if (!VECTOR_MODE_P (TYPE_MODE (type))
8969 || can_vec_perm_const_p (TYPE_MODE (type), TYPE_MODE (type), sel, false))
8970 (vec_perm @0 @1 { vec_perm_indices_to_tree
8971 (build_vector_type (ssizetype, nunits), sel); })))))
8973 (if (canonicalize_math_after_vectorization_p ())
8976 (fmas:c (negate @0) @1 @2)
8977 (IFN_FNMA @0 @1 @2))
8979 (fmas @0 @1 (negate @2))
8982 (fmas:c (negate @0) @1 (negate @2))
8983 (IFN_FNMS @0 @1 @2))
8985 (negate (fmas@3 @0 @1 @2))
8986 (if (single_use (@3))
8987 (IFN_FNMS @0 @1 @2))))
8990 (IFN_FMS:c (negate @0) @1 @2)
8991 (IFN_FNMS @0 @1 @2))
8993 (IFN_FMS @0 @1 (negate @2))
8996 (IFN_FMS:c (negate @0) @1 (negate @2))
8997 (IFN_FNMA @0 @1 @2))
8999 (negate (IFN_FMS@3 @0 @1 @2))
9000 (if (single_use (@3))
9001 (IFN_FNMA @0 @1 @2)))
9004 (IFN_FNMA:c (negate @0) @1 @2)
9007 (IFN_FNMA @0 @1 (negate @2))
9008 (IFN_FNMS @0 @1 @2))
9010 (IFN_FNMA:c (negate @0) @1 (negate @2))
9013 (negate (IFN_FNMA@3 @0 @1 @2))
9014 (if (single_use (@3))
9015 (IFN_FMS @0 @1 @2)))
9018 (IFN_FNMS:c (negate @0) @1 @2)
9021 (IFN_FNMS @0 @1 (negate @2))
9022 (IFN_FNMA @0 @1 @2))
9024 (IFN_FNMS:c (negate @0) @1 (negate @2))
9027 (negate (IFN_FNMS@3 @0 @1 @2))
9028 (if (single_use (@3))
9029 (IFN_FMA @0 @1 @2))))
9031 /* CLZ simplifications. */
9036 (op (clz:s@2 @0) INTEGER_CST@1)
9037 (if (integer_zerop (@1) && single_use (@2))
9038 /* clz(X) == 0 is (int)X < 0 and clz(X) != 0 is (int)X >= 0. */
9039 (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
9040 (cmp (convert:stype @0) { build_zero_cst (stype); }))
9041 /* clz(X) == (prec-1) is X == 1 and clz(X) != (prec-1) is X != 1. */
9042 (if (wi::to_wide (@1) == TYPE_PRECISION (TREE_TYPE (@0)) - 1)
9043 (op @0 { build_one_cst (TREE_TYPE (@0)); }))))))
9047 (op (IFN_CLZ:s@2 @0 @3) INTEGER_CST@1)
9048 (if (integer_zerop (@1) && single_use (@2))
9049 /* clz(X) == 0 is (int)X < 0 and clz(X) != 0 is (int)X >= 0. */
9050 (with { tree type0 = TREE_TYPE (@0);
9051 tree stype = signed_type_for (TREE_TYPE (@0));
9052 /* Punt if clz(0) == 0. */
9053 if (integer_zerop (@3))
9057 (cmp (convert:stype @0) { build_zero_cst (stype); })))
9058 /* clz(X) == (prec-1) is X == 1 and clz(X) != (prec-1) is X != 1. */
9059 (with { bool ok = true;
9060 tree type0 = TREE_TYPE (@0);
9061 /* Punt if clz(0) == prec - 1. */
9062 if (wi::to_widest (@3) == TYPE_PRECISION (type0) - 1)
9065 (if (ok && wi::to_wide (@1) == (TYPE_PRECISION (type0) - 1))
9066 (op @0 { build_one_cst (type0); }))))))
9068 /* CTZ simplifications. */
9070 (for op (ge gt le lt)
9073 /* __builtin_ctz (x) >= C -> (x & ((1 << C) - 1)) == 0. */
9074 (op (ctz:s @0) INTEGER_CST@1)
9075 (with { bool ok = true;
9076 HOST_WIDE_INT val = 0;
9077 if (!tree_fits_shwi_p (@1))
9081 val = tree_to_shwi (@1);
9082 /* Canonicalize to >= or <. */
9083 if (op == GT_EXPR || op == LE_EXPR)
9085 if (val == HOST_WIDE_INT_MAX)
9091 tree type0 = TREE_TYPE (@0);
9092 int prec = TYPE_PRECISION (type0);
9094 (if (ok && prec <= MAX_FIXED_MODE_SIZE)
9096 { constant_boolean_node (cmp == EQ_EXPR ? true : false, type); }
9098 { constant_boolean_node (cmp == EQ_EXPR ? false : true, type); }
9099 (cmp (bit_and @0 { wide_int_to_tree (type0,
9100 wi::mask (val, false, prec)); })
9101 { build_zero_cst (type0); })))))))
9104 /* __builtin_ctz (x) == C -> (x & ((1 << (C + 1)) - 1)) == (1 << C). */
9105 (op (ctz:s @0) INTEGER_CST@1)
9106 (with { tree type0 = TREE_TYPE (@0);
9107 int prec = TYPE_PRECISION (type0);
9109 (if (prec <= MAX_FIXED_MODE_SIZE)
9110 (if (tree_int_cst_sgn (@1) < 0 || wi::to_widest (@1) >= prec)
9111 { constant_boolean_node (op == EQ_EXPR ? false : true, type); }
9112 (op (bit_and @0 { wide_int_to_tree (type0,
9113 wi::mask (tree_to_uhwi (@1) + 1,
9115 { wide_int_to_tree (type0,
9116 wi::shifted_mask (tree_to_uhwi (@1), 1,
9117 false, prec)); })))))))
9118 (for op (ge gt le lt)
9121 /* __builtin_ctz (x) >= C -> (x & ((1 << C) - 1)) == 0. */
9122 (op (IFN_CTZ:s @0 @2) INTEGER_CST@1)
9123 (with { bool ok = true;
9124 HOST_WIDE_INT val = 0;
9125 if (!tree_fits_shwi_p (@1))
9129 val = tree_to_shwi (@1);
9130 /* Canonicalize to >= or <. */
9131 if (op == GT_EXPR || op == LE_EXPR)
9133 if (val == HOST_WIDE_INT_MAX)
9139 HOST_WIDE_INT zero_val = tree_to_shwi (@2);
9140 tree type0 = TREE_TYPE (@0);
9141 int prec = TYPE_PRECISION (type0);
9142 if (prec > MAX_FIXED_MODE_SIZE)
9146 (if (ok && zero_val >= val)
9147 { constant_boolean_node (cmp == EQ_EXPR ? true : false, type); })
9149 (if (ok && zero_val < val)
9150 { constant_boolean_node (cmp == EQ_EXPR ? false : true, type); })
9151 (if (ok && (zero_val < 0 || zero_val >= prec))
9152 (cmp (bit_and @0 { wide_int_to_tree (type0,
9153 wi::mask (val, false, prec)); })
9154 { build_zero_cst (type0); })))))))
9157 /* __builtin_ctz (x) == C -> (x & ((1 << (C + 1)) - 1)) == (1 << C). */
9158 (op (IFN_CTZ:s @0 @2) INTEGER_CST@1)
9159 (with { HOST_WIDE_INT zero_val = tree_to_shwi (@2);
9160 tree type0 = TREE_TYPE (@0);
9161 int prec = TYPE_PRECISION (type0);
9163 (if (prec <= MAX_FIXED_MODE_SIZE)
9164 (if (tree_int_cst_sgn (@1) < 0 || wi::to_widest (@1) >= prec)
9165 (if (zero_val != wi::to_widest (@1))
9166 { constant_boolean_node (op == EQ_EXPR ? false : true, type); })
9167 (if (zero_val < 0 || zero_val >= prec)
9168 (op (bit_and @0 { wide_int_to_tree (type0,
9169 wi::mask (tree_to_uhwi (@1) + 1,
9171 { wide_int_to_tree (type0,
9172 wi::shifted_mask (tree_to_uhwi (@1), 1,
9173 false, prec)); })))))))
9176 /* ctz(ext(X)) == ctz(X). Valid just for the UB at zero cases though. */
9178 (CTZ (convert@1 @0))
9179 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
9180 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
9181 && TYPE_PRECISION (TREE_TYPE (@1)) > TYPE_PRECISION (TREE_TYPE (@0)))
9182 (with { combined_fn cfn = CFN_LAST;
9183 tree type0 = TREE_TYPE (@0);
9184 if (TREE_CODE (type0) == BITINT_TYPE)
9186 if (TYPE_PRECISION (type0) > MAX_FIXED_MODE_SIZE)
9190 = build_nonstandard_integer_type (TYPE_PRECISION (type0),
9193 type0 = unsigned_type_for (type0);
9195 && direct_internal_fn_supported_p (IFN_CTZ, type0,
9199 && TYPE_PRECISION (TREE_TYPE (@1)) > BITS_PER_WORD
9200 && !direct_internal_fn_supported_p (IFN_CTZ,
9204 if (TYPE_PRECISION (type0)
9205 == TYPE_PRECISION (unsigned_type_node))
9206 cfn = CFN_BUILT_IN_CTZ;
9207 else if (TYPE_PRECISION (type0)
9208 == TYPE_PRECISION (long_long_unsigned_type_node))
9209 cfn = CFN_BUILT_IN_CTZLL;
9211 (if (cfn == CFN_CTZ)
9212 (IFN_CTZ (convert:type0 @0))
9213 (if (cfn == CFN_BUILT_IN_CTZ)
9214 (BUILT_IN_CTZ (convert:type0 @0))
9215 (if (cfn == CFN_BUILT_IN_CTZLL)
9216 (BUILT_IN_CTZLL (convert:type0 @0))))))))
9219 /* POPCOUNT simplifications. */
9220 /* popcount(X) + popcount(Y) is popcount(X|Y) when X&Y must be zero. */
9222 (plus (POPCOUNT:s @0) (POPCOUNT:s @1))
9223 (if (INTEGRAL_TYPE_P (type)
9224 && (wi::bit_and (widest_int::from (tree_nonzero_bits (@0), UNSIGNED),
9225 widest_int::from (tree_nonzero_bits (@1), UNSIGNED))
9227 (with { tree utype = TREE_TYPE (@0);
9228 if (TYPE_PRECISION (utype) < TYPE_PRECISION (TREE_TYPE (@1)))
9229 utype = TREE_TYPE (@1); }
9230 (POPCOUNT (bit_ior (convert:utype @0) (convert:utype @1))))))
9232 /* popcount(X) == 0 is X == 0, and related (in)equalities. */
9233 (for popcount (POPCOUNT)
9234 (for cmp (le eq ne gt)
9237 (cmp (popcount @0) integer_zerop)
9238 (rep @0 { build_zero_cst (TREE_TYPE (@0)); }))))
9240 /* popcount(bswap(x)) is popcount(x). */
9241 (for popcount (POPCOUNT)
9242 (for bswap (BUILT_IN_BSWAP16 BUILT_IN_BSWAP32
9243 BUILT_IN_BSWAP64 BUILT_IN_BSWAP128)
9245 (popcount (convert?@0 (bswap:s@1 @2)))
9246 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
9247 && INTEGRAL_TYPE_P (TREE_TYPE (@1)))
9248 (with { tree type0 = TREE_TYPE (@0);
9249 tree type1 = TREE_TYPE (@1);
9250 unsigned int prec0 = TYPE_PRECISION (type0);
9251 unsigned int prec1 = TYPE_PRECISION (type1); }
9252 (if (prec0 == prec1 || (prec0 > prec1 && TYPE_UNSIGNED (type1)))
9253 (popcount (convert:type0 (convert:type1 @2)))))))))
9255 /* popcount(rotate(X Y)) is popcount(X). */
9256 (for popcount (POPCOUNT)
9257 (for rot (lrotate rrotate)
9259 (popcount (convert?@0 (rot:s@1 @2 @3)))
9260 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
9261 && INTEGRAL_TYPE_P (TREE_TYPE (@1))
9262 && (GIMPLE || !TREE_SIDE_EFFECTS (@3)))
9263 (with { tree type0 = TREE_TYPE (@0);
9264 tree type1 = TREE_TYPE (@1);
9265 unsigned int prec0 = TYPE_PRECISION (type0);
9266 unsigned int prec1 = TYPE_PRECISION (type1); }
9267 (if (prec0 == prec1 || (prec0 > prec1 && TYPE_UNSIGNED (type1)))
9268 (popcount (convert:type0 @2))))))))
9270 /* Canonicalize POPCOUNT(x)&1 as PARITY(X). */
9272 (bit_and (POPCOUNT @0) integer_onep)
9275 /* popcount(X&Y) + popcount(X|Y) is popcount(x) + popcount(Y). */
9277 (plus:c (POPCOUNT:s (bit_and:s @0 @1)) (POPCOUNT:s (bit_ior:cs @0 @1)))
9278 (plus (POPCOUNT:type @0) (POPCOUNT:type @1)))
9280 /* popcount(X) + popcount(Y) - popcount(X&Y) is popcount(X|Y). */
9281 /* popcount(X) + popcount(Y) - popcount(X|Y) is popcount(X&Y). */
9282 (for popcount (POPCOUNT)
9283 (for log1 (bit_and bit_ior)
9284 log2 (bit_ior bit_and)
9286 (minus (plus:s (popcount:s @0) (popcount:s @1))
9287 (popcount:s (log1:cs @0 @1)))
9288 (popcount (log2 @0 @1)))
9290 (plus:c (minus:s (popcount:s @0) (popcount:s (log1:cs @0 @1)))
9292 (popcount (log2 @0 @1)))))
9295 /* popcount(zext(X)) == popcount(X). */
9297 (POPCOUNT (convert@1 @0))
9298 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
9299 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
9300 && TYPE_UNSIGNED (TREE_TYPE (@0))
9301 && TYPE_PRECISION (TREE_TYPE (@1)) > TYPE_PRECISION (TREE_TYPE (@0)))
9302 (with { combined_fn cfn = CFN_LAST;
9303 tree type0 = TREE_TYPE (@0);
9304 if (TREE_CODE (type0) == BITINT_TYPE)
9306 if (TYPE_PRECISION (type0) > MAX_FIXED_MODE_SIZE)
9310 = build_nonstandard_integer_type (TYPE_PRECISION (type0),
9314 && direct_internal_fn_supported_p (IFN_POPCOUNT, type0,
9318 && TYPE_PRECISION (TREE_TYPE (@1)) > BITS_PER_WORD
9319 && !direct_internal_fn_supported_p (IFN_POPCOUNT,
9323 if (TYPE_PRECISION (type0)
9324 == TYPE_PRECISION (unsigned_type_node))
9325 cfn = CFN_BUILT_IN_POPCOUNT;
9326 else if (TYPE_PRECISION (type0)
9327 == TYPE_PRECISION (long_long_unsigned_type_node))
9328 cfn = CFN_BUILT_IN_POPCOUNTLL;
9330 (if (cfn == CFN_POPCOUNT)
9331 (IFN_POPCOUNT (convert:type0 @0))
9332 (if (cfn == CFN_BUILT_IN_POPCOUNT)
9333 (BUILT_IN_POPCOUNT (convert:type0 @0))
9334 (if (cfn == CFN_BUILT_IN_POPCOUNTLL)
9335 (BUILT_IN_POPCOUNTLL (convert:type0 @0))))))))
9338 /* PARITY simplifications. */
9339 /* parity(~X) is parity(X). */
9341 (PARITY (bit_not @0))
9344 /* parity(bswap(x)) is parity(x). */
9345 (for parity (PARITY)
9346 (for bswap (BUILT_IN_BSWAP16 BUILT_IN_BSWAP32
9347 BUILT_IN_BSWAP64 BUILT_IN_BSWAP128)
9349 (parity (convert?@0 (bswap:s@1 @2)))
9350 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
9351 && INTEGRAL_TYPE_P (TREE_TYPE (@1))
9352 && TYPE_PRECISION (TREE_TYPE (@0))
9353 >= TYPE_PRECISION (TREE_TYPE (@1)))
9354 (with { tree type0 = TREE_TYPE (@0);
9355 tree type1 = TREE_TYPE (@1); }
9356 (parity (convert:type0 (convert:type1 @2))))))))
9358 /* parity(rotate(X Y)) is parity(X). */
9359 (for parity (PARITY)
9360 (for rot (lrotate rrotate)
9362 (parity (convert?@0 (rot:s@1 @2 @3)))
9363 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
9364 && INTEGRAL_TYPE_P (TREE_TYPE (@1))
9365 && (GIMPLE || !TREE_SIDE_EFFECTS (@3))
9366 && TYPE_PRECISION (TREE_TYPE (@0))
9367 >= TYPE_PRECISION (TREE_TYPE (@1)))
9368 (with { tree type0 = TREE_TYPE (@0); }
9369 (parity (convert:type0 @2)))))))
9371 /* parity(X)^parity(Y) is parity(X^Y). */
9373 (bit_xor (PARITY:s @0) (PARITY:s @1))
9374 (if (types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
9375 (PARITY (bit_xor @0 @1))
9376 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
9377 && INTEGRAL_TYPE_P (TREE_TYPE (@1)))
9378 (with { tree utype = TREE_TYPE (@0);
9379 if (TYPE_PRECISION (utype) < TYPE_PRECISION (TREE_TYPE (@1)))
9380 utype = TREE_TYPE (@1); }
9381 (PARITY (bit_xor (convert:utype @0) (convert:utype @1)))))))
9384 /* parity(zext(X)) == parity(X). */
9385 /* parity(sext(X)) == parity(X) if the difference in precision is even. */
9387 (PARITY (convert@1 @0))
9388 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
9389 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
9390 && TYPE_PRECISION (TREE_TYPE (@1)) > TYPE_PRECISION (TREE_TYPE (@0))
9391 && (TYPE_UNSIGNED (TREE_TYPE (@0))
9392 || ((TYPE_PRECISION (TREE_TYPE (@1))
9393 - TYPE_PRECISION (TREE_TYPE (@0))) & 1) == 0))
9394 (with { combined_fn cfn = CFN_LAST;
9395 tree type0 = TREE_TYPE (@0);
9396 if (TREE_CODE (type0) == BITINT_TYPE)
9398 if (TYPE_PRECISION (type0) > MAX_FIXED_MODE_SIZE)
9402 = build_nonstandard_integer_type (TYPE_PRECISION (type0),
9405 type0 = unsigned_type_for (type0);
9407 && direct_internal_fn_supported_p (IFN_PARITY, type0,
9411 && TYPE_PRECISION (TREE_TYPE (@1)) > BITS_PER_WORD
9412 && !direct_internal_fn_supported_p (IFN_PARITY,
9416 if (TYPE_PRECISION (type0)
9417 == TYPE_PRECISION (unsigned_type_node))
9418 cfn = CFN_BUILT_IN_PARITY;
9419 else if (TYPE_PRECISION (type0)
9420 == TYPE_PRECISION (long_long_unsigned_type_node))
9421 cfn = CFN_BUILT_IN_PARITYLL;
9423 (if (cfn == CFN_PARITY)
9424 (IFN_PARITY (convert:type0 @0))
9425 (if (cfn == CFN_BUILT_IN_PARITY)
9426 (BUILT_IN_PARITY (convert:type0 @0))
9427 (if (cfn == CFN_BUILT_IN_PARITYLL)
9428 (BUILT_IN_PARITYLL (convert:type0 @0))))))))
9431 /* a != 0 ? FUN(a) : 0 -> Fun(a) for some builtin functions. */
9432 (for func (POPCOUNT BSWAP FFS PARITY)
9434 (cond (ne @0 integer_zerop@1) (func@3 (convert? @0)) integer_zerop@2)
9437 /* a != 0 ? FUN(a) : CST -> Fun(a) for some CLRSB builtins
9438 where CST is precision-1. */
9441 (cond (ne @0 integer_zerop@1) (func@4 (convert?@3 @0)) INTEGER_CST@2)
9442 (if (wi::to_widest (@2) == TYPE_PRECISION (TREE_TYPE (@3)) - 1)
9446 /* a != 0 ? CLZ(a) : CST -> .CLZ(a) where CST is the result of the internal function for 0. */
9449 (cond (ne @0 integer_zerop@1) (func (convert?@3 @0)) INTEGER_CST@2)
9451 internal_fn ifn = IFN_LAST;
9452 if (TREE_CODE (TREE_TYPE (@3)) == BITINT_TYPE)
9454 if (tree_fits_shwi_p (@2))
9456 HOST_WIDE_INT valw = tree_to_shwi (@2);
9457 if ((int) valw == valw)
9464 else if (direct_internal_fn_supported_p (IFN_CLZ, TREE_TYPE (@3),
9466 && CLZ_DEFINED_VALUE_AT_ZERO
9467 (SCALAR_INT_TYPE_MODE (TREE_TYPE (@3)), val) == 2)
9470 (if (ifn == IFN_CLZ && wi::to_widest (@2) == val)
9473 (cond (ne @0 integer_zerop@1) (IFN_CLZ (convert?@3 @0) INTEGER_CST@2) @2)
9475 internal_fn ifn = IFN_LAST;
9476 if (TREE_CODE (TREE_TYPE (@3)) == BITINT_TYPE)
9478 else if (direct_internal_fn_supported_p (IFN_CLZ, TREE_TYPE (@3),
9482 (if (ifn == IFN_CLZ)
9485 /* a != 0 ? CTZ(a) : CST -> .CTZ(a) where CST is the result of the internal function for 0. */
9488 (cond (ne @0 integer_zerop@1) (func (convert?@3 @0)) INTEGER_CST@2)
9490 internal_fn ifn = IFN_LAST;
9491 if (TREE_CODE (TREE_TYPE (@3)) == BITINT_TYPE)
9493 if (tree_fits_shwi_p (@2))
9495 HOST_WIDE_INT valw = tree_to_shwi (@2);
9496 if ((int) valw == valw)
9503 else if (direct_internal_fn_supported_p (IFN_CTZ, TREE_TYPE (@3),
9505 && CTZ_DEFINED_VALUE_AT_ZERO
9506 (SCALAR_INT_TYPE_MODE (TREE_TYPE (@3)), val) == 2)
9509 (if (ifn == IFN_CTZ && wi::to_widest (@2) == val)
9512 (cond (ne @0 integer_zerop@1) (IFN_CTZ (convert?@3 @0) INTEGER_CST@2) @2)
9514 internal_fn ifn = IFN_LAST;
9515 if (TREE_CODE (TREE_TYPE (@3)) == BITINT_TYPE)
9517 else if (direct_internal_fn_supported_p (IFN_CTZ, TREE_TYPE (@3),
9521 (if (ifn == IFN_CTZ)
9525 /* Common POPCOUNT/PARITY simplifications. */
9526 /* popcount(X&C1) is (X>>C2)&1 when C1 == 1<<C2. Same for parity(X&C1). */
9527 (for pfun (POPCOUNT PARITY)
9530 (if (INTEGRAL_TYPE_P (type))
9531 (with { wide_int nz = tree_nonzero_bits (@0); }
9535 (if (wi::popcount (nz) == 1)
9536 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
9537 (convert (rshift:utype (convert:utype @0)
9538 { build_int_cst (integer_type_node,
9539 wi::ctz (nz)); })))))))))
9542 /* 64- and 32-bits branchless implementations of popcount are detected:
9544 int popcount64c (uint64_t x)
9546 x -= (x >> 1) & 0x5555555555555555ULL;
9547 x = (x & 0x3333333333333333ULL) + ((x >> 2) & 0x3333333333333333ULL);
9548 x = (x + (x >> 4)) & 0x0f0f0f0f0f0f0f0fULL;
9549 return (x * 0x0101010101010101ULL) >> 56;
9552 int popcount32c (uint32_t x)
9554 x -= (x >> 1) & 0x55555555;
9555 x = (x & 0x33333333) + ((x >> 2) & 0x33333333);
9556 x = (x + (x >> 4)) & 0x0f0f0f0f;
9557 return (x * 0x01010101) >> 24;
9564 (rshift @8 INTEGER_CST@5)
9566 (bit_and @6 INTEGER_CST@7)
9570 (bit_and (rshift @0 INTEGER_CST@4) INTEGER_CST@11))
9576 /* Check constants and optab. */
9577 (with { unsigned prec = TYPE_PRECISION (type);
9578 int shift = (64 - prec) & 63;
9579 unsigned HOST_WIDE_INT c1
9580 = HOST_WIDE_INT_UC (0x0101010101010101) >> shift;
9581 unsigned HOST_WIDE_INT c2
9582 = HOST_WIDE_INT_UC (0x0F0F0F0F0F0F0F0F) >> shift;
9583 unsigned HOST_WIDE_INT c3
9584 = HOST_WIDE_INT_UC (0x3333333333333333) >> shift;
9585 unsigned HOST_WIDE_INT c4
9586 = HOST_WIDE_INT_UC (0x5555555555555555) >> shift;
9591 && TYPE_UNSIGNED (type)
9592 && integer_onep (@4)
9593 && wi::to_widest (@10) == 2
9594 && wi::to_widest (@5) == 4
9595 && wi::to_widest (@1) == prec - 8
9596 && tree_to_uhwi (@2) == c1
9597 && tree_to_uhwi (@3) == c2
9598 && tree_to_uhwi (@9) == c3
9599 && tree_to_uhwi (@7) == c3
9600 && tree_to_uhwi (@11) == c4)
9601 (if (direct_internal_fn_supported_p (IFN_POPCOUNT, type,
9603 (convert (IFN_POPCOUNT:type @0))
9604 /* Try to do popcount in two halves. PREC must be at least
9605 five bits for this to work without extension before adding. */
9607 tree half_type = NULL_TREE;
9608 opt_machine_mode m = mode_for_size ((prec + 1) / 2, MODE_INT, 1);
9611 && m.require () != TYPE_MODE (type))
9613 half_prec = GET_MODE_PRECISION (as_a <scalar_int_mode> (m));
9614 half_type = build_nonstandard_integer_type (half_prec, 1);
9616 gcc_assert (half_prec > 2);
9618 (if (half_type != NULL_TREE
9619 && direct_internal_fn_supported_p (IFN_POPCOUNT, half_type,
9622 (IFN_POPCOUNT:half_type (convert @0))
9623 (IFN_POPCOUNT:half_type (convert (rshift @0
9624 { build_int_cst (integer_type_node, half_prec); } )))))))))))
9626 /* __builtin_ffs needs to deal on many targets with the possible zero
9627 argument. If we know the argument is always non-zero, __builtin_ctz + 1
9628 should lead to better code. */
9630 (FFS tree_expr_nonzero_p@0)
9631 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
9632 && direct_internal_fn_supported_p (IFN_CTZ, TREE_TYPE (@0),
9633 OPTIMIZE_FOR_SPEED))
9634 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
9635 (plus (CTZ:type (convert:utype @0)) { build_one_cst (type); }))))
9639 /* __builtin_ffs (X) == 0 -> X == 0.
9640 __builtin_ffs (X) == 6 -> (X & 63) == 32. */
9643 (cmp (ffs@2 @0) INTEGER_CST@1)
9644 (with { int prec = TYPE_PRECISION (TREE_TYPE (@0)); }
9646 (if (integer_zerop (@1))
9647 (cmp @0 { build_zero_cst (TREE_TYPE (@0)); }))
9648 (if (tree_int_cst_sgn (@1) < 0 || wi::to_widest (@1) > prec)
9649 { constant_boolean_node (cmp == NE_EXPR ? true : false, type); })
9650 (if (single_use (@2))
9651 (cmp (bit_and @0 { wide_int_to_tree (TREE_TYPE (@0),
9652 wi::mask (tree_to_uhwi (@1),
9654 { wide_int_to_tree (TREE_TYPE (@0),
9655 wi::shifted_mask (tree_to_uhwi (@1) - 1, 1,
9656 false, prec)); }))))))
9658 /* __builtin_ffs (X) > 6 -> X != 0 && (X & 63) == 0. */
9662 bit_op (bit_and bit_ior)
9664 (cmp (ffs@2 @0) INTEGER_CST@1)
9665 (with { int prec = TYPE_PRECISION (TREE_TYPE (@0)); }
9667 (if (integer_zerop (@1))
9668 (cmp2 @0 { build_zero_cst (TREE_TYPE (@0)); }))
9669 (if (tree_int_cst_sgn (@1) < 0)
9670 { constant_boolean_node (cmp == GT_EXPR ? true : false, type); })
9671 (if (wi::to_widest (@1) >= prec)
9672 { constant_boolean_node (cmp == GT_EXPR ? false : true, type); })
9673 (if (wi::to_widest (@1) == prec - 1)
9674 (cmp3 @0 { wide_int_to_tree (TREE_TYPE (@0),
9675 wi::shifted_mask (prec - 1, 1,
9677 (if (single_use (@2))
9678 (bit_op (cmp2 @0 { build_zero_cst (TREE_TYPE (@0)); })
9680 { wide_int_to_tree (TREE_TYPE (@0),
9681 wi::mask (tree_to_uhwi (@1),
9683 { build_zero_cst (TREE_TYPE (@0)); }))))))))
9686 /* ffs(ext(X)) == ffs(X). */
9688 (FFS (convert@1 @0))
9689 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
9690 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
9691 && TYPE_PRECISION (TREE_TYPE (@1)) > TYPE_PRECISION (TREE_TYPE (@0)))
9692 (with { combined_fn cfn = CFN_LAST;
9693 tree type0 = TREE_TYPE (@0);
9694 if (TREE_CODE (type0) == BITINT_TYPE)
9696 if (TYPE_PRECISION (type0) > MAX_FIXED_MODE_SIZE)
9700 = build_nonstandard_integer_type (TYPE_PRECISION (type0),
9703 type0 = signed_type_for (type0);
9705 && direct_internal_fn_supported_p (IFN_FFS, type0,
9709 && TYPE_PRECISION (TREE_TYPE (@1)) > BITS_PER_WORD
9710 && !direct_internal_fn_supported_p (IFN_FFS,
9714 if (TYPE_PRECISION (type0)
9715 == TYPE_PRECISION (integer_type_node))
9716 cfn = CFN_BUILT_IN_FFS;
9717 else if (TYPE_PRECISION (type0)
9718 == TYPE_PRECISION (long_long_integer_type_node))
9719 cfn = CFN_BUILT_IN_FFSLL;
9721 (if (cfn == CFN_FFS)
9722 (IFN_FFS (convert:type0 @0))
9723 (if (cfn == CFN_BUILT_IN_FFS)
9724 (BUILT_IN_FFS (convert:type0 @0))
9725 (if (cfn == CFN_BUILT_IN_FFSLL)
9726 (BUILT_IN_FFSLL (convert:type0 @0))))))))
9734 --> r = .COND_FN (cond, a, b)
9738 --> r = .COND_FN (~cond, b, a). */
9740 (for uncond_op (UNCOND_UNARY)
9741 cond_op (COND_UNARY)
9743 (vec_cond @0 (view_convert? (uncond_op@3 @1)) @2)
9744 (with { tree op_type = TREE_TYPE (@3); }
9745 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
9746 && is_truth_type_for (op_type, TREE_TYPE (@0)))
9747 (cond_op @0 (view_convert @1) @2))))
9749 (vec_cond @0 @1 (view_convert? (uncond_op@3 @2)))
9750 (with { tree op_type = TREE_TYPE (@3); }
9751 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
9752 && is_truth_type_for (op_type, TREE_TYPE (@0)))
9753 (cond_op (bit_not @0) (view_convert @2) @1)))))
9755 (for uncond_op (UNCOND_UNARY)
9756 cond_op (COND_LEN_UNARY)
9758 (IFN_VCOND_MASK_LEN @0 (view_convert? (uncond_op@3 @1)) @2 @4 @5)
9759 (with { tree op_type = TREE_TYPE (@3); }
9760 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
9761 && is_truth_type_for (op_type, TREE_TYPE (@0)))
9762 (cond_op @0 (view_convert @1) @2 @4 @5))))
9764 (IFN_VCOND_MASK_LEN @0 @1 (view_convert? (uncond_op@3 @2)) @4 @5)
9765 (with { tree op_type = TREE_TYPE (@3); }
9766 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
9767 && is_truth_type_for (op_type, TREE_TYPE (@0)))
9768 (cond_op (bit_not @0) (view_convert @2) @1 @4 @5)))))
9770 /* `(a ? -1 : 0) ^ b` can be converted into a conditional not. */
9772 (bit_xor:c (vec_cond @0 uniform_integer_cst_p@1 uniform_integer_cst_p@2) @3)
9773 (if (canonicalize_math_after_vectorization_p ()
9774 && vectorized_internal_fn_supported_p (IFN_COND_NOT, type)
9775 && is_truth_type_for (type, TREE_TYPE (@0)))
9776 (if (integer_all_onesp (@1) && integer_zerop (@2))
9777 (IFN_COND_NOT @0 @3 @3))
9778 (if (integer_all_onesp (@2) && integer_zerop (@1))
9779 (IFN_COND_NOT (bit_not @0) @3 @3))))
9788 r = c ? a1 op a2 : b;
9790 if the target can do it in one go. This makes the operation conditional
9791 on c, so could drop potentially-trapping arithmetic, but that's a valid
9792 simplification if the result of the operation isn't needed.
9794 Avoid speculatively generating a stand-alone vector comparison
9795 on targets that might not support them. Any target implementing
9796 conditional internal functions must support the same comparisons
9797 inside and outside a VEC_COND_EXPR. */
9799 (for uncond_op (UNCOND_BINARY)
9800 cond_op (COND_BINARY)
9802 (vec_cond @0 (view_convert? (uncond_op@4 @1 @2)) @3)
9803 (with { tree op_type = TREE_TYPE (@4); }
9804 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
9805 && is_truth_type_for (op_type, TREE_TYPE (@0))
9807 (view_convert (cond_op @0 @1 @2 (view_convert:op_type @3))))))
9809 (vec_cond @0 @1 (view_convert? (uncond_op@4 @2 @3)))
9810 (with { tree op_type = TREE_TYPE (@4); }
9811 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
9812 && is_truth_type_for (op_type, TREE_TYPE (@0))
9814 (view_convert (cond_op (bit_not @0) @2 @3 (view_convert:op_type @1)))))))
9816 (for uncond_op (UNCOND_BINARY)
9817 cond_op (COND_LEN_BINARY)
9819 (IFN_VCOND_MASK_LEN @0 (view_convert? (uncond_op@4 @1 @2)) @3 @5 @6)
9820 (with { tree op_type = TREE_TYPE (@4); }
9821 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
9822 && is_truth_type_for (op_type, TREE_TYPE (@0))
9824 (view_convert (cond_op @0 @1 @2 (view_convert:op_type @3) @5 @6)))))
9826 (IFN_VCOND_MASK_LEN @0 @1 (view_convert? (uncond_op@4 @2 @3)) @5 @6)
9827 (with { tree op_type = TREE_TYPE (@4); }
9828 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
9829 && is_truth_type_for (op_type, TREE_TYPE (@0))
9831 (view_convert (cond_op (bit_not @0) @2 @3 (view_convert:op_type @1) @5 @6))))))
9833 /* Same for ternary operations. */
9834 (for uncond_op (UNCOND_TERNARY)
9835 cond_op (COND_TERNARY)
9837 (vec_cond @0 (view_convert? (uncond_op@5 @1 @2 @3)) @4)
9838 (with { tree op_type = TREE_TYPE (@5); }
9839 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
9840 && is_truth_type_for (op_type, TREE_TYPE (@0))
9842 (view_convert (cond_op @0 @1 @2 @3 (view_convert:op_type @4))))))
9844 (vec_cond @0 @1 (view_convert? (uncond_op@5 @2 @3 @4)))
9845 (with { tree op_type = TREE_TYPE (@5); }
9846 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
9847 && is_truth_type_for (op_type, TREE_TYPE (@0))
9849 (view_convert (cond_op (bit_not @0) @2 @3 @4
9850 (view_convert:op_type @1)))))))
9852 (for uncond_op (UNCOND_TERNARY)
9853 cond_op (COND_LEN_TERNARY)
9855 (IFN_VCOND_MASK_LEN @0 (view_convert? (uncond_op@5 @1 @2 @3)) @4 @6 @7)
9856 (with { tree op_type = TREE_TYPE (@5); }
9857 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
9858 && is_truth_type_for (op_type, TREE_TYPE (@0))
9860 (view_convert (cond_op @0 @1 @2 @3 (view_convert:op_type @4) @6 @7)))))
9862 (IFN_VCOND_MASK_LEN @0 @1 (view_convert? (uncond_op@5 @2 @3 @4 @6 @7)))
9863 (with { tree op_type = TREE_TYPE (@5); }
9864 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
9865 && is_truth_type_for (op_type, TREE_TYPE (@0))
9867 (view_convert (cond_op (bit_not @0) @2 @3 @4 (view_convert:op_type @1) @6 @7))))))
9870 /* Detect cases in which a VEC_COND_EXPR effectively replaces the
9871 "else" value of an IFN_COND_*. */
9872 (for cond_op (COND_BINARY)
9874 (vec_cond @0 (view_convert? (cond_op @0 @1 @2 @3)) @4)
9875 (with { tree op_type = TREE_TYPE (@3); }
9876 (if (element_precision (type) == element_precision (op_type))
9877 (view_convert (cond_op @0 @1 @2 (view_convert:op_type @4))))))
9879 (vec_cond @0 @1 (view_convert? (cond_op @2 @3 @4 @5)))
9880 (with { tree op_type = TREE_TYPE (@5); }
9881 (if (inverse_conditions_p (@0, @2)
9882 && element_precision (type) == element_precision (op_type))
9883 (view_convert (cond_op @2 @3 @4 (view_convert:op_type @1)))))))
9885 /* Same for ternary operations. */
9886 (for cond_op (COND_TERNARY)
9888 (vec_cond @0 (view_convert? (cond_op @0 @1 @2 @3 @4)) @5)
9889 (with { tree op_type = TREE_TYPE (@4); }
9890 (if (element_precision (type) == element_precision (op_type))
9891 (view_convert (cond_op @0 @1 @2 @3 (view_convert:op_type @5))))))
9893 (vec_cond @0 @1 (view_convert? (cond_op @2 @3 @4 @5 @6)))
9894 (with { tree op_type = TREE_TYPE (@6); }
9895 (if (inverse_conditions_p (@0, @2)
9896 && element_precision (type) == element_precision (op_type))
9897 (view_convert (cond_op @2 @3 @4 @5 (view_convert:op_type @1)))))))
9899 /* Detect cases in which a VEC_COND_EXPR effectively replaces the
9900 "else" value of an IFN_COND_LEN_*. */
9901 (for cond_len_op (COND_LEN_BINARY)
9903 (vec_cond @0 (view_convert? (cond_len_op @0 @1 @2 @3 @4 @5)) @6)
9904 (with { tree op_type = TREE_TYPE (@3); }
9905 (if (element_precision (type) == element_precision (op_type))
9906 (view_convert (cond_len_op @0 @1 @2 (view_convert:op_type @6) @4 @5)))))
9908 (vec_cond @0 @1 (view_convert? (cond_len_op @2 @3 @4 @5 @6 @7)))
9909 (with { tree op_type = TREE_TYPE (@5); }
9910 (if (inverse_conditions_p (@0, @2)
9911 && element_precision (type) == element_precision (op_type))
9912 (view_convert (cond_len_op @2 @3 @4 (view_convert:op_type @1) @6 @7))))))
9914 /* Same for ternary operations. */
9915 (for cond_len_op (COND_LEN_TERNARY)
9917 (vec_cond @0 (view_convert? (cond_len_op @0 @1 @2 @3 @4 @5 @6)) @7)
9918 (with { tree op_type = TREE_TYPE (@4); }
9919 (if (element_precision (type) == element_precision (op_type))
9920 (view_convert (cond_len_op @0 @1 @2 @3 (view_convert:op_type @7) @5 @6)))))
9922 (vec_cond @0 @1 (view_convert? (cond_len_op @2 @3 @4 @5 @6 @7 @8)))
9923 (with { tree op_type = TREE_TYPE (@6); }
9924 (if (inverse_conditions_p (@0, @2)
9925 && element_precision (type) == element_precision (op_type))
9926 (view_convert (cond_len_op @2 @3 @4 @5 (view_convert:op_type @1) @7 @8))))))
9928 /* Detect simplication for a conditional reduction where
9931 c = mask2 ? d + a : d
9935 c = mask1 && mask2 ? d + b : d. */
9937 (IFN_COND_ADD @0 @1 (vec_cond @2 @3 zerop@4) @1)
9938 (if (ANY_INTEGRAL_TYPE_P (type)
9939 || (FLOAT_TYPE_P (type)
9940 && fold_real_zero_addition_p (type, NULL_TREE, @4, 0)))
9941 (IFN_COND_ADD (bit_and @0 @2) @1 @3 @1)))
9943 /* Detect simplication for a conditional length reduction where
9946 c = i < len + bias ? d + a : d
9950 c = mask && i < len + bias ? d + b : d. */
9952 (IFN_COND_LEN_ADD integer_truep @0 (vec_cond @1 @2 zerop@5) @0 @3 @4)
9953 (if (ANY_INTEGRAL_TYPE_P (type)
9954 || (FLOAT_TYPE_P (type)
9955 && fold_real_zero_addition_p (type, NULL_TREE, @5, 0)))
9956 (IFN_COND_LEN_ADD @1 @0 @2 @0 @3 @4)))
9958 /* Detect simplification for vector condition folding where
9960 c = mask1 ? (masked_op mask2 a b els) : els
9964 c = masked_op (mask1 & mask2) a b els
9966 where the operation can be partially applied to one operand. */
9968 (for cond_op (COND_BINARY)
9971 (cond_op:s @1 @2 @3 @4) @4)
9972 (cond_op (bit_and @1 @0) @2 @3 @4)))
9974 /* And same for ternary expressions. */
9976 (for cond_op (COND_TERNARY)
9979 (cond_op:s @1 @2 @3 @4 @5) @5)
9980 (cond_op (bit_and @1 @0) @2 @3 @4 @5)))
9982 /* For pointers @0 and @2 and nonnegative constant offset @1, look for
9985 A: (@0 + @1 < @2) | (@2 + @1 < @0)
9986 B: (@0 + @1 <= @2) | (@2 + @1 <= @0)
9988 If pointers are known not to wrap, B checks whether @1 bytes starting
9989 at @0 and @2 do not overlap, while A tests the same thing for @1 + 1
9990 bytes. A is more efficiently tested as:
9992 A: (sizetype) (@0 + @1 - @2) > @1 * 2
9994 The equivalent expression for B is given by replacing @1 with @1 - 1:
9996 B: (sizetype) (@0 + (@1 - 1) - @2) > (@1 - 1) * 2
9998 @0 and @2 can be swapped in both expressions without changing the result.
10000 The folds rely on sizetype's being unsigned (which is always true)
10001 and on its being the same width as the pointer (which we have to check).
10003 The fold replaces two pointer_plus expressions, two comparisons and
10004 an IOR with a pointer_plus, a pointer_diff, and a comparison, so in
10005 the best case it's a saving of two operations. The A fold retains one
10006 of the original pointer_pluses, so is a win even if both pointer_pluses
10007 are used elsewhere. The B fold is a wash if both pointer_pluses are
10008 used elsewhere, since all we end up doing is replacing a comparison with
10009 a pointer_plus. We do still apply the fold under those circumstances
10010 though, in case applying it to other conditions eventually makes one of the
10011 pointer_pluses dead. */
10012 (for ior (truth_orif truth_or bit_ior)
10015 (ior (cmp:cs (pointer_plus@3 @0 INTEGER_CST@1) @2)
10016 (cmp:cs (pointer_plus@4 @2 @1) @0))
10017 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
10018 && TYPE_OVERFLOW_WRAPS (sizetype)
10019 && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (sizetype))
10020 /* Calculate the rhs constant. */
10021 (with { offset_int off = wi::to_offset (@1) - (cmp == LE_EXPR ? 1 : 0);
10022 offset_int rhs = off * 2; }
10023 /* Always fails for negative values. */
10024 (if (wi::min_precision (rhs, UNSIGNED) <= TYPE_PRECISION (sizetype))
10025 /* Since the order of @0 and @2 doesn't matter, let tree_swap_operands_p
10026 pick a canonical order. This increases the chances of using the
10027 same pointer_plus in multiple checks. */
10028 (with { bool swap_p = tree_swap_operands_p (@0, @2);
10029 tree rhs_tree = wide_int_to_tree (sizetype, rhs); }
10030 (if (cmp == LT_EXPR)
10031 (gt (convert:sizetype
10032 (pointer_diff:ssizetype { swap_p ? @4 : @3; }
10033 { swap_p ? @0 : @2; }))
10035 (gt (convert:sizetype
10036 (pointer_diff:ssizetype
10037 (pointer_plus { swap_p ? @2 : @0; }
10038 { wide_int_to_tree (sizetype, off); })
10039 { swap_p ? @0 : @2; }))
10040 { rhs_tree; })))))))))
10042 /* Fold REDUC (@0 & @1) -> @0[I] & @1[I] if element I is the only nonzero
10044 (for reduc (IFN_REDUC_PLUS IFN_REDUC_IOR IFN_REDUC_XOR)
10045 (simplify (reduc (view_convert? (bit_and @0 VECTOR_CST@1)))
10046 (with { int i = single_nonzero_element (@1); }
10048 (with { tree elt = vector_cst_elt (@1, i);
10049 tree elt_type = TREE_TYPE (elt);
10050 unsigned int elt_bits = tree_to_uhwi (TYPE_SIZE (elt_type));
10051 tree size = bitsize_int (elt_bits);
10052 tree pos = bitsize_int (elt_bits * i); }
10055 (BIT_FIELD_REF:elt_type @0 { size; } { pos; })
10058 /* Fold reduction of a single nonzero element constructor. */
10059 (for reduc (IFN_REDUC_PLUS IFN_REDUC_IOR IFN_REDUC_XOR)
10060 (simplify (reduc (CONSTRUCTOR@0))
10061 (with { tree ctor = (TREE_CODE (@0) == SSA_NAME
10062 ? gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0)) : @0);
10063 tree elt = ctor_single_nonzero_element (ctor); }
10065 && !HONOR_SNANS (type)
10066 && !HONOR_SIGNED_ZEROS (type))
10069 /* Fold REDUC (@0 op VECTOR_CST) as REDUC (@0) op REDUC (VECTOR_CST). */
10070 (for reduc (IFN_REDUC_PLUS IFN_REDUC_MAX IFN_REDUC_MIN IFN_REDUC_FMAX
10071 IFN_REDUC_FMIN IFN_REDUC_AND IFN_REDUC_IOR IFN_REDUC_XOR)
10072 op (plus max min IFN_FMAX IFN_FMIN bit_and bit_ior bit_xor)
10073 (simplify (reduc (op @0 VECTOR_CST@1))
10074 (op (reduc:type @0) (reduc:type @1))))
10076 /* Simplify vector floating point operations of alternating sub/add pairs
10077 into using an fneg of a wider element type followed by a normal add.
10078 under IEEE 754 the fneg of the wider type will negate every even entry
10079 and when doing an add we get a sub of the even and add of every odd
10081 (for plusminus (plus minus)
10082 minusplus (minus plus)
10084 (vec_perm (plusminus @0 @1) (minusplus @2 @3) VECTOR_CST@4)
10085 (if (!VECTOR_INTEGER_TYPE_P (type)
10086 && !FLOAT_WORDS_BIG_ENDIAN
10087 /* plus is commutative, while minus is not, so :c can't be used.
10088 Do equality comparisons by hand and at the end pick the operands
10090 && (operand_equal_p (@0, @2, 0)
10091 ? operand_equal_p (@1, @3, 0)
10092 : operand_equal_p (@0, @3, 0) && operand_equal_p (@1, @2, 0)))
10095 /* Build a vector of integers from the tree mask. */
10096 vec_perm_builder builder;
10098 (if (tree_to_vec_perm_builder (&builder, @4))
10101 /* Create a vec_perm_indices for the integer vector. */
10102 poly_uint64 nelts = TYPE_VECTOR_SUBPARTS (type);
10103 vec_perm_indices sel (builder, 2, nelts);
10104 machine_mode vec_mode = TYPE_MODE (type);
10105 machine_mode wide_mode;
10106 scalar_mode wide_elt_mode;
10107 poly_uint64 wide_nunits;
10108 scalar_mode inner_mode = GET_MODE_INNER (vec_mode);
10110 (if (VECTOR_MODE_P (vec_mode)
10111 && sel.series_p (0, 2, 0, 2)
10112 && sel.series_p (1, 2, nelts + 1, 2)
10113 && GET_MODE_2XWIDER_MODE (inner_mode).exists (&wide_elt_mode)
10114 && multiple_p (GET_MODE_NUNITS (vec_mode), 2, &wide_nunits)
10115 && related_vector_mode (vec_mode, wide_elt_mode,
10116 wide_nunits).exists (&wide_mode))
10120 = lang_hooks.types.type_for_mode (GET_MODE_INNER (wide_mode),
10121 TYPE_UNSIGNED (type));
10122 tree ntype = build_vector_type_for_mode (stype, wide_mode);
10124 /* The format has to be a non-extended ieee format. */
10125 const struct real_format *fmt_old = FLOAT_MODE_FORMAT (vec_mode);
10126 const struct real_format *fmt_new = FLOAT_MODE_FORMAT (wide_mode);
10128 (if (TYPE_MODE (stype) != BLKmode
10129 && VECTOR_TYPE_P (ntype)
10131 && fmt_new != NULL)
10134 /* If the target doesn't support v1xx vectors, try using
10135 scalar mode xx instead. */
10136 if (known_eq (GET_MODE_NUNITS (wide_mode), 1)
10137 && !target_supports_op_p (ntype, NEGATE_EXPR, optab_vector))
10140 (if (fmt_new->signbit_rw
10141 == fmt_old->signbit_rw + GET_MODE_UNIT_BITSIZE (vec_mode)
10142 && fmt_new->signbit_rw == fmt_new->signbit_ro
10143 && targetm.can_change_mode_class (TYPE_MODE (ntype),
10144 TYPE_MODE (type), ALL_REGS)
10145 && ((optimize_vectors_before_lowering_p ()
10146 && VECTOR_TYPE_P (ntype))
10147 || target_supports_op_p (ntype, NEGATE_EXPR, optab_vector)))
10148 (if (plusminus == PLUS_EXPR)
10149 (plus (view_convert:type (negate (view_convert:ntype @3))) @2)
10150 (minus @0 (view_convert:type
10151 (negate (view_convert:ntype @1))))))))))))))))
10154 (vec_perm @0 @1 VECTOR_CST@2)
10157 tree op0 = @0, op1 = @1, op2 = @2;
10158 machine_mode result_mode = TYPE_MODE (type);
10159 machine_mode op_mode = TYPE_MODE (TREE_TYPE (op0));
10161 /* Build a vector of integers from the tree mask. */
10162 vec_perm_builder builder;
10164 (if (tree_to_vec_perm_builder (&builder, op2))
10167 /* Create a vec_perm_indices for the integer vector. */
10168 poly_uint64 nelts = TYPE_VECTOR_SUBPARTS (type);
10169 bool single_arg = (op0 == op1);
10170 vec_perm_indices sel (builder, single_arg ? 1 : 2, nelts);
10172 (if (sel.series_p (0, 1, 0, 1))
10174 (if (sel.series_p (0, 1, nelts, 1))
10180 if (sel.all_from_input_p (0))
10182 else if (sel.all_from_input_p (1))
10185 sel.rotate_inputs (1);
10187 else if (known_ge (poly_uint64 (sel[0]), nelts))
10189 std::swap (op0, op1);
10190 sel.rotate_inputs (1);
10194 tree cop0 = op0, cop1 = op1;
10195 if (TREE_CODE (op0) == SSA_NAME
10196 && (def = dyn_cast <gassign *> (SSA_NAME_DEF_STMT (op0)))
10197 && gimple_assign_rhs_code (def) == CONSTRUCTOR)
10198 cop0 = gimple_assign_rhs1 (def);
10199 if (TREE_CODE (op1) == SSA_NAME
10200 && (def = dyn_cast <gassign *> (SSA_NAME_DEF_STMT (op1)))
10201 && gimple_assign_rhs_code (def) == CONSTRUCTOR)
10202 cop1 = gimple_assign_rhs1 (def);
10205 (if ((TREE_CODE (cop0) == VECTOR_CST
10206 || TREE_CODE (cop0) == CONSTRUCTOR)
10207 && (TREE_CODE (cop1) == VECTOR_CST
10208 || TREE_CODE (cop1) == CONSTRUCTOR)
10209 && (t = fold_vec_perm (type, cop0, cop1, sel)))
10213 bool changed = (op0 == op1 && !single_arg);
10214 tree ins = NULL_TREE;
10217 /* See if the permutation is performing a single element
10218 insert from a CONSTRUCTOR or constant and use a BIT_INSERT_EXPR
10219 in that case. But only if the vector mode is supported,
10220 otherwise this is invalid GIMPLE. */
10221 if (op_mode != BLKmode
10222 && (TREE_CODE (cop0) == VECTOR_CST
10223 || TREE_CODE (cop0) == CONSTRUCTOR
10224 || TREE_CODE (cop1) == VECTOR_CST
10225 || TREE_CODE (cop1) == CONSTRUCTOR))
10227 bool insert_first_p = sel.series_p (1, 1, nelts + 1, 1);
10228 if (insert_first_p)
10230 /* After canonicalizing the first elt to come from the
10231 first vector we only can insert the first elt from
10232 the first vector. */
10234 if ((ins = fold_read_from_vector (cop0, sel[0])))
10237 /* The above can fail for two-element vectors which always
10238 appear to insert the first element, so try inserting
10239 into the second lane as well. For more than two
10240 elements that's wasted time. */
10241 if (!insert_first_p || (!ins && maybe_eq (nelts, 2u)))
10243 unsigned int encoded_nelts = sel.encoding ().encoded_nelts ();
10244 for (at = 0; at < encoded_nelts; ++at)
10245 if (maybe_ne (sel[at], at))
10247 if (at < encoded_nelts
10248 && (known_eq (at + 1, nelts)
10249 || sel.series_p (at + 1, 1, at + 1, 1)))
10251 if (known_lt (poly_uint64 (sel[at]), nelts))
10252 ins = fold_read_from_vector (cop0, sel[at]);
10254 ins = fold_read_from_vector (cop1, sel[at] - nelts);
10259 /* Generate a canonical form of the selector. */
10260 if (!ins && sel.encoding () != builder)
10262 /* Some targets are deficient and fail to expand a single
10263 argument permutation while still allowing an equivalent
10264 2-argument version. */
10266 if (sel.ninputs () == 2
10267 || can_vec_perm_const_p (result_mode, op_mode, sel, false))
10268 op2 = vec_perm_indices_to_tree (TREE_TYPE (op2), sel);
10271 vec_perm_indices sel2 (builder, 2, nelts);
10272 if (can_vec_perm_const_p (result_mode, op_mode, sel2, false))
10273 op2 = vec_perm_indices_to_tree (TREE_TYPE (op2), sel2);
10275 /* Not directly supported with either encoding,
10276 so use the preferred form. */
10277 op2 = vec_perm_indices_to_tree (TREE_TYPE (op2), sel);
10279 if (!operand_equal_p (op2, oldop2, 0))
10284 (bit_insert { op0; } { ins; }
10285 { bitsize_int (at * vector_element_bits (type)); })
10287 (vec_perm { op0; } { op1; } { op2; }))))))))))))
10289 /* VEC_PERM_EXPR (v, v, mask) -> v where v contains same element. */
10291 (match vec_same_elem_p
10292 (vec_duplicate @0))
10294 (match vec_same_elem_p
10296 (if (TREE_CODE (@0) == SSA_NAME
10297 && uniform_vector_p (gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0))))))
10299 (match vec_same_elem_p
10301 (if (uniform_vector_p (@0))))
10305 (vec_perm vec_same_elem_p@0 @0 @1)
10306 (if (types_match (type, TREE_TYPE (@0)))
10310 tree elem = uniform_vector_p (@0);
10313 { build_vector_from_val (type, elem); }))))
10315 /* Push VEC_PERM earlier if that may help FMA perception (PR101895). */
10317 (plus:c (vec_perm:s (mult:c@0 @1 vec_same_elem_p@2) @0 @3) @4)
10318 (if (TREE_CODE (@0) == SSA_NAME && num_imm_uses (@0) == 2)
10319 (plus (mult (vec_perm @1 @1 @3) @2) @4)))
10321 (minus (vec_perm:s (mult:c@0 @1 vec_same_elem_p@2) @0 @3) @4)
10322 (if (TREE_CODE (@0) == SSA_NAME && num_imm_uses (@0) == 2)
10323 (minus (mult (vec_perm @1 @1 @3) @2) @4)))
10327 c = VEC_PERM_EXPR <a, b, VCST0>;
10328 d = VEC_PERM_EXPR <c, c, VCST1>;
10330 d = VEC_PERM_EXPR <a, b, NEW_VCST>; */
10333 (vec_perm (view_convert?@0 (vec_perm@1 @2 @3 VECTOR_CST@4)) @0 VECTOR_CST@5)
10334 (if (TYPE_VECTOR_SUBPARTS (type).is_constant ())
10337 machine_mode result_mode = TYPE_MODE (type);
10338 machine_mode op_mode = TYPE_MODE (TREE_TYPE (@2));
10339 int nelts = TYPE_VECTOR_SUBPARTS (type).to_constant ();
10340 vec_perm_builder builder0;
10341 vec_perm_builder builder1;
10342 vec_perm_builder builder2 (nelts, nelts, 1);
10344 (if (tree_to_vec_perm_builder (&builder0, @4)
10345 && tree_to_vec_perm_builder (&builder1, @5)
10346 && TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))
10347 == TYPE_SIZE (TREE_TYPE (TREE_TYPE (@1))))
10350 vec_perm_indices sel0 (builder0, 2, nelts);
10351 vec_perm_indices sel1 (builder1, 1, nelts);
10353 for (int i = 0; i < nelts; i++)
10354 builder2.quick_push (sel0[sel1[i].to_constant ()]);
10356 vec_perm_indices sel2 (builder2, 2, nelts);
10358 tree op0 = NULL_TREE;
10359 /* If the new VEC_PERM_EXPR can't be handled but both
10360 original VEC_PERM_EXPRs can, punt.
10361 If one or both of the original VEC_PERM_EXPRs can't be
10362 handled and the new one can't be either, don't increase
10363 number of VEC_PERM_EXPRs that can't be handled. */
10364 if (can_vec_perm_const_p (result_mode, op_mode, sel2, false)
10365 || (single_use (@0)
10366 ? (!can_vec_perm_const_p (result_mode, op_mode, sel0, false)
10367 || !can_vec_perm_const_p (result_mode, op_mode, sel1, false))
10368 : !can_vec_perm_const_p (result_mode, op_mode, sel1, false)))
10369 op0 = vec_perm_indices_to_tree (TREE_TYPE (@5), sel2);
10372 (view_convert (vec_perm @2 @3 { op0; }))))))))
10375 c = VEC_PERM_EXPR <a, b, VCST0>;
10376 d = VEC_PERM_EXPR <x, c, VCST1>;
10378 d = VEC_PERM_EXPR <x, {a,b}, NEW_VCST>;
10379 when all elements from a or b are replaced by the later
10383 (vec_perm @5 (vec_perm@0 @1 @2 VECTOR_CST@3) VECTOR_CST@4)
10384 (if (TYPE_VECTOR_SUBPARTS (type).is_constant ())
10387 machine_mode result_mode = TYPE_MODE (type);
10388 machine_mode op_mode = TYPE_MODE (TREE_TYPE (@1));
10389 int nelts = TYPE_VECTOR_SUBPARTS (type).to_constant ();
10390 vec_perm_builder builder0;
10391 vec_perm_builder builder1;
10392 vec_perm_builder builder2 (nelts, nelts, 2);
10394 (if (tree_to_vec_perm_builder (&builder0, @3)
10395 && tree_to_vec_perm_builder (&builder1, @4))
10398 vec_perm_indices sel0 (builder0, 2, nelts);
10399 vec_perm_indices sel1 (builder1, 2, nelts);
10400 bool use_1 = false, use_2 = false;
10402 for (int i = 0; i < nelts; i++)
10404 if (known_lt ((poly_uint64)sel1[i], sel1.nelts_per_input ()))
10405 builder2.quick_push (sel1[i]);
10408 poly_uint64 j = sel0[(sel1[i] - sel1.nelts_per_input ())
10410 if (known_lt (j, sel0.nelts_per_input ()))
10415 j -= sel0.nelts_per_input ();
10417 builder2.quick_push (j + sel1.nelts_per_input ());
10421 (if (use_1 ^ use_2)
10424 vec_perm_indices sel2 (builder2, 2, nelts);
10425 tree op0 = NULL_TREE;
10426 /* If the new VEC_PERM_EXPR can't be handled but both
10427 original VEC_PERM_EXPRs can, punt.
10428 If one or both of the original VEC_PERM_EXPRs can't be
10429 handled and the new one can't be either, don't increase
10430 number of VEC_PERM_EXPRs that can't be handled. */
10431 if (can_vec_perm_const_p (result_mode, op_mode, sel2, false)
10432 || (single_use (@0)
10433 ? (!can_vec_perm_const_p (result_mode, op_mode, sel0, false)
10434 || !can_vec_perm_const_p (result_mode, op_mode, sel1, false))
10435 : !can_vec_perm_const_p (result_mode, op_mode, sel1, false)))
10436 op0 = vec_perm_indices_to_tree (TREE_TYPE (@4), sel2);
10441 (vec_perm @5 @1 { op0; }))
10443 (vec_perm @5 @2 { op0; })))))))))))
10445 /* And the case with swapped outer permute sources. */
10448 (vec_perm (vec_perm@0 @1 @2 VECTOR_CST@3) @5 VECTOR_CST@4)
10449 (if (TYPE_VECTOR_SUBPARTS (type).is_constant ())
10452 machine_mode result_mode = TYPE_MODE (type);
10453 machine_mode op_mode = TYPE_MODE (TREE_TYPE (@1));
10454 int nelts = TYPE_VECTOR_SUBPARTS (type).to_constant ();
10455 vec_perm_builder builder0;
10456 vec_perm_builder builder1;
10457 vec_perm_builder builder2 (nelts, nelts, 2);
10459 (if (tree_to_vec_perm_builder (&builder0, @3)
10460 && tree_to_vec_perm_builder (&builder1, @4))
10463 vec_perm_indices sel0 (builder0, 2, nelts);
10464 vec_perm_indices sel1 (builder1, 2, nelts);
10465 bool use_1 = false, use_2 = false;
10467 for (int i = 0; i < nelts; i++)
10469 if (known_ge ((poly_uint64)sel1[i], sel1.nelts_per_input ()))
10470 builder2.quick_push (sel1[i]);
10473 poly_uint64 j = sel0[sel1[i].to_constant ()];
10474 if (known_lt (j, sel0.nelts_per_input ()))
10479 j -= sel0.nelts_per_input ();
10481 builder2.quick_push (j);
10485 (if (use_1 ^ use_2)
10488 vec_perm_indices sel2 (builder2, 2, nelts);
10489 tree op0 = NULL_TREE;
10490 /* If the new VEC_PERM_EXPR can't be handled but both
10491 original VEC_PERM_EXPRs can, punt.
10492 If one or both of the original VEC_PERM_EXPRs can't be
10493 handled and the new one can't be either, don't increase
10494 number of VEC_PERM_EXPRs that can't be handled. */
10495 if (can_vec_perm_const_p (result_mode, op_mode, sel2, false)
10496 || (single_use (@0)
10497 ? (!can_vec_perm_const_p (result_mode, op_mode, sel0, false)
10498 || !can_vec_perm_const_p (result_mode, op_mode, sel1, false))
10499 : !can_vec_perm_const_p (result_mode, op_mode, sel1, false)))
10500 op0 = vec_perm_indices_to_tree (TREE_TYPE (@4), sel2);
10505 (vec_perm @1 @5 { op0; }))
10507 (vec_perm @2 @5 { op0; })))))))))))
10510 /* Match count trailing zeroes for simplify_count_trailing_zeroes in fwprop.
10511 The canonical form is array[((x & -x) * C) >> SHIFT] where C is a magic
10512 constant which when multiplied by a power of 2 contains a unique value
10513 in the top 5 or 6 bits. This is then indexed into a table which maps it
10514 to the number of trailing zeroes. */
10515 (match (ctz_table_index @1 @2 @3)
10516 (rshift (mult (bit_and:c (negate @1) @1) INTEGER_CST@2) INTEGER_CST@3))
10518 (match (cond_expr_convert_p @0 @2 @3 @6)
10519 (cond (simple_comparison@6 @0 @1) (convert@4 @2) (convert@5 @3))
10520 (if (INTEGRAL_TYPE_P (type)
10521 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
10522 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
10523 && INTEGRAL_TYPE_P (TREE_TYPE (@3))
10524 && TYPE_PRECISION (type) != TYPE_PRECISION (TREE_TYPE (@0))
10525 && TYPE_PRECISION (TREE_TYPE (@0))
10526 == TYPE_PRECISION (TREE_TYPE (@2))
10527 && TYPE_PRECISION (TREE_TYPE (@0))
10528 == TYPE_PRECISION (TREE_TYPE (@3))
10529 /* For vect_recog_cond_expr_convert_pattern, @2 and @3 can differ in
10530 signess when convert is truncation, but not ok for extension since
10531 it's sign_extend vs zero_extend. */
10532 && (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type)
10533 || (TYPE_UNSIGNED (TREE_TYPE (@2))
10534 == TYPE_UNSIGNED (TREE_TYPE (@3))))
10536 && single_use (@5))))
10538 (for bit_op (bit_and bit_ior bit_xor)
10539 (match (bitwise_induction_p @0 @2 @3)
10541 (nop_convert1? (bit_not2?@0 (convert3? (lshift integer_onep@1 @2))))
10544 (match (bitwise_induction_p @0 @2 @3)
10546 (nop_convert1? (bit_xor@0 (convert2? (lshift integer_onep@1 @2)) @3))))
10548 /* n - (((n > C1) ? n : C1) & -C2) -> n & C1 for unsigned case.
10549 n - (((n > C1) ? n : C1) & -C2) -> (n <= C1) ? n : (n & C1) for signed case. */
10551 (minus @0 (bit_and (max @0 INTEGER_CST@1) INTEGER_CST@2))
10552 (with { auto i = wi::neg (wi::to_wide (@2)); }
10553 /* Check if -C2 is a power of 2 and C1 = -C2 - 1. */
10554 (if (wi::popcount (i) == 1
10555 && (wi::to_wide (@1)) == (i - 1))
10556 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
10558 (cond (le @0 @1) @0 (bit_and @0 @1))))))
10560 /* -x & 1 -> x & 1. */
10562 (bit_and (negate @0) integer_onep@1)
10563 (if (!TYPE_OVERFLOW_SANITIZED (type))
10566 /* `-a` is just `a` if the type is 1bit wide or when converting
10567 to a 1bit type; similar to the above transformation of `(-x)&1`.
10568 This is used mostly with the transformation of
10569 `a ? ~b : b` into `(-a)^b`.
10570 It also can show up with bitfields. */
10572 (convert? (negate @0))
10573 (if (INTEGRAL_TYPE_P (type)
10574 && TYPE_PRECISION (type) == 1
10575 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0)))
10579 c1 = VEC_PERM_EXPR (a, a, mask)
10580 c2 = VEC_PERM_EXPR (b, b, mask)
10584 c3 = VEC_PERM_EXPR (c, c, mask)
10585 For all integer non-div operations. */
10586 (for op (plus minus mult bit_and bit_ior bit_xor
10589 (op (vec_perm @0 @0 @2) (vec_perm @1 @1 @2))
10590 (if (VECTOR_INTEGER_TYPE_P (type))
10591 (vec_perm (op@3 @0 @1) @3 @2))))
10593 /* Similar for float arithmetic when permutation constant covers
10594 all vector elements. */
10595 (for op (plus minus mult)
10597 (op (vec_perm @0 @0 VECTOR_CST@2) (vec_perm @1 @1 VECTOR_CST@2))
10598 (if (VECTOR_FLOAT_TYPE_P (type)
10599 && TYPE_VECTOR_SUBPARTS (type).is_constant ())
10602 tree perm_cst = @2;
10603 vec_perm_builder builder;
10604 bool full_perm_p = false;
10605 if (tree_to_vec_perm_builder (&builder, perm_cst))
10607 unsigned HOST_WIDE_INT nelts;
10609 nelts = TYPE_VECTOR_SUBPARTS (type).to_constant ();
10610 /* Create a vec_perm_indices for the VECTOR_CST. */
10611 vec_perm_indices sel (builder, 1, nelts);
10613 /* Check if perm indices covers all vector elements. */
10614 if (sel.encoding ().encoded_full_vector_p ())
10616 auto_sbitmap seen (nelts);
10617 bitmap_clear (seen);
10619 unsigned HOST_WIDE_INT count = 0, i;
10621 for (i = 0; i < nelts; i++)
10623 if (!bitmap_set_bit (seen, sel[i].to_constant ()))
10627 full_perm_p = count == nelts;
10632 (vec_perm (op@3 @0 @1) @3 @2))))))