Debian Codegraph @

gcc/match.pd

raw
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.
4
5 Copyright (C) 2014-2025 Free Software Foundation, Inc.
6 Contributed by Richard Biener <[email protected]>
7 and Prathamesh Kulkarni <[email protected]>
8
9 This file is part of GCC.
10
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
14 version.
15
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
19 for more details.
20
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/>. */
24
25
26 /* Generic tree predicates we inherit. */
27 (define_predicates
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
31 zerop
32 initializer_each_zero_or_onep
33 CONSTANT_CLASS_P
34 poly_int_tree_p
35 tree_expr_nonnegative_p
36 tree_expr_nonzero_p
37 integer_valued_real_p
38 integer_pow2p
39 uniform_integer_cst_p
40 HONOR_NANS
41 uniform_vector_p
42 expand_vec_cmp_expr_p
43 bitmask_inv_cst_vector_p)
44
45 /* Operator lists. */
46 (define_operator_list tcc_comparison
47 lt le eq ne ge gt unordered ordered unlt unle ungt unge uneq ltgt)
48 (define_operator_list inverted_tcc_comparison
49 ge gt ne eq lt le ordered unordered ge gt le lt ltgt uneq)
50 (define_operator_list inverted_tcc_comparison_with_nans
51 unge ungt ne eq unlt unle ordered unordered ge gt le lt ltgt uneq)
52 (define_operator_list swapped_tcc_comparison
53 gt ge eq ne le lt unordered ordered ungt unge unlt unle uneq ltgt)
54 (define_operator_list simple_comparison lt le eq ne ge gt)
55 (define_operator_list swapped_simple_comparison gt ge eq ne le lt)
56 (define_operator_list BSWAP BUILT_IN_BSWAP16 BUILT_IN_BSWAP32
57 BUILT_IN_BSWAP64 BUILT_IN_BSWAP128)
58
59 #include "cfn-operators.pd"
60
61 /* Define operand lists for math rounding functions {,i,l,ll}FN,
62 where the versions prefixed with "i" return an int, those prefixed with
63 "l" return a long and those prefixed with "ll" return a long long.
64
65 Also define operand lists:
66
67 X<FN>F for all float functions, in the order i, l, ll
68 X<FN> for all double functions, in the same order
69 X<FN>L for all long double functions, in the same order. */
70 #define DEFINE_INT_AND_FLOAT_ROUND_FN(FN) \
71 (define_operator_list X##FN##F BUILT_IN_I##FN##F \
72 BUILT_IN_L##FN##F \
73 BUILT_IN_LL##FN##F) \
74 (define_operator_list X##FN BUILT_IN_I##FN \
75 BUILT_IN_L##FN \
76 BUILT_IN_LL##FN) \
77 (define_operator_list X##FN##L BUILT_IN_I##FN##L \
78 BUILT_IN_L##FN##L \
79 BUILT_IN_LL##FN##L)
80
81 DEFINE_INT_AND_FLOAT_ROUND_FN (FLOOR)
82 DEFINE_INT_AND_FLOAT_ROUND_FN (CEIL)
83 DEFINE_INT_AND_FLOAT_ROUND_FN (ROUND)
84 DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
85
86 /* Unary operations and their associated IFN_COND_* function. */
87 (define_operator_list UNCOND_UNARY
88 negate bit_not)
89 (define_operator_list COND_UNARY
90 IFN_COND_NEG IFN_COND_NOT)
91 (define_operator_list COND_LEN_UNARY
92 IFN_COND_LEN_NEG IFN_COND_LEN_NOT)
93
94 /* Binary operations and their associated IFN_COND_* function. */
95 (define_operator_list UNCOND_BINARY
96 plus minus
97 mult trunc_div trunc_mod rdiv
98 min max
99 IFN_FMIN IFN_FMAX IFN_COPYSIGN
100 bit_and bit_ior bit_xor
101 lshift rshift)
102 (define_operator_list COND_BINARY
103 IFN_COND_ADD IFN_COND_SUB
104 IFN_COND_MUL IFN_COND_DIV IFN_COND_MOD IFN_COND_RDIV
105 IFN_COND_MIN IFN_COND_MAX
106 IFN_COND_FMIN IFN_COND_FMAX IFN_COND_COPYSIGN
107 IFN_COND_AND IFN_COND_IOR IFN_COND_XOR
108 IFN_COND_SHL IFN_COND_SHR)
109 (define_operator_list COND_LEN_BINARY
110 IFN_COND_LEN_ADD IFN_COND_LEN_SUB
111 IFN_COND_LEN_MUL IFN_COND_LEN_DIV
112 IFN_COND_LEN_MOD IFN_COND_LEN_RDIV
113 IFN_COND_LEN_MIN IFN_COND_LEN_MAX
114 IFN_COND_LEN_FMIN IFN_COND_LEN_FMAX IFN_COND_LEN_COPYSIGN
115 IFN_COND_LEN_AND IFN_COND_LEN_IOR IFN_COND_LEN_XOR
116 IFN_COND_LEN_SHL IFN_COND_LEN_SHR)
117
118 /* Same for ternary operations. */
119 (define_operator_list UNCOND_TERNARY
120 IFN_FMA IFN_FMS IFN_FNMA IFN_FNMS)
121 (define_operator_list COND_TERNARY
122 IFN_COND_FMA IFN_COND_FMS IFN_COND_FNMA IFN_COND_FNMS)
123 (define_operator_list COND_LEN_TERNARY
124 IFN_COND_LEN_FMA IFN_COND_LEN_FMS IFN_COND_LEN_FNMA IFN_COND_LEN_FNMS)
125
126 /* __atomic_fetch_or_*, __atomic_fetch_xor_*, __atomic_xor_fetch_* */
127 (define_operator_list ATOMIC_FETCH_OR_XOR_N
128 BUILT_IN_ATOMIC_FETCH_OR_1 BUILT_IN_ATOMIC_FETCH_OR_2
129 BUILT_IN_ATOMIC_FETCH_OR_4 BUILT_IN_ATOMIC_FETCH_OR_8
130 BUILT_IN_ATOMIC_FETCH_OR_16
131 BUILT_IN_ATOMIC_FETCH_XOR_1 BUILT_IN_ATOMIC_FETCH_XOR_2
132 BUILT_IN_ATOMIC_FETCH_XOR_4 BUILT_IN_ATOMIC_FETCH_XOR_8
133 BUILT_IN_ATOMIC_FETCH_XOR_16
134 BUILT_IN_ATOMIC_XOR_FETCH_1 BUILT_IN_ATOMIC_XOR_FETCH_2
135 BUILT_IN_ATOMIC_XOR_FETCH_4 BUILT_IN_ATOMIC_XOR_FETCH_8
136 BUILT_IN_ATOMIC_XOR_FETCH_16)
137 /* __sync_fetch_and_or_*, __sync_fetch_and_xor_*, __sync_xor_and_fetch_* */
138 (define_operator_list SYNC_FETCH_OR_XOR_N
139 BUILT_IN_SYNC_FETCH_AND_OR_1 BUILT_IN_SYNC_FETCH_AND_OR_2
140 BUILT_IN_SYNC_FETCH_AND_OR_4 BUILT_IN_SYNC_FETCH_AND_OR_8
141 BUILT_IN_SYNC_FETCH_AND_OR_16
142 BUILT_IN_SYNC_FETCH_AND_XOR_1 BUILT_IN_SYNC_FETCH_AND_XOR_2
143 BUILT_IN_SYNC_FETCH_AND_XOR_4 BUILT_IN_SYNC_FETCH_AND_XOR_8
144 BUILT_IN_SYNC_FETCH_AND_XOR_16
145 BUILT_IN_SYNC_XOR_AND_FETCH_1 BUILT_IN_SYNC_XOR_AND_FETCH_2
146 BUILT_IN_SYNC_XOR_AND_FETCH_4 BUILT_IN_SYNC_XOR_AND_FETCH_8
147 BUILT_IN_SYNC_XOR_AND_FETCH_16)
148 /* __atomic_fetch_and_*. */
149 (define_operator_list ATOMIC_FETCH_AND_N
150 BUILT_IN_ATOMIC_FETCH_AND_1 BUILT_IN_ATOMIC_FETCH_AND_2
151 BUILT_IN_ATOMIC_FETCH_AND_4 BUILT_IN_ATOMIC_FETCH_AND_8
152 BUILT_IN_ATOMIC_FETCH_AND_16)
153 /* __sync_fetch_and_and_*. */
154 (define_operator_list SYNC_FETCH_AND_AND_N
155 BUILT_IN_SYNC_FETCH_AND_AND_1 BUILT_IN_SYNC_FETCH_AND_AND_2
156 BUILT_IN_SYNC_FETCH_AND_AND_4 BUILT_IN_SYNC_FETCH_AND_AND_8
157 BUILT_IN_SYNC_FETCH_AND_AND_16)
158
159 /* With nop_convert? combine convert? and view_convert? in one pattern
160 plus conditionalize on tree_nop_conversion_p conversions. */
161 (match (nop_convert @0)
162 (convert @0)
163 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))))
164 (match (nop_convert @0)
165 (view_convert @0)
166 (if (VECTOR_TYPE_P (type) && VECTOR_TYPE_P (TREE_TYPE (@0))
167 && known_eq (TYPE_VECTOR_SUBPARTS (type),
168 TYPE_VECTOR_SUBPARTS (TREE_TYPE (@0)))
169 && tree_nop_conversion_p (TREE_TYPE (type), TREE_TYPE (TREE_TYPE (@0))))))
170
171 /* These are used by gimple_bitwise_inverted_equal_p to simplify
172 detection of BIT_NOT and comparisons. */
173 (match (bit_not_with_nop @0)
174 (bit_not @0))
175 (match (bit_not_with_nop @0)
176 (convert (bit_not @0))
177 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))))
178 (match (bit_xor_cst @0 @1)
179 (bit_xor @0 uniform_integer_cst_p@1))
180 (for cmp (tcc_comparison)
181 (match (maybe_cmp @0)
182 (cmp@0 @1 @2))
183 (match (maybe_cmp @0)
184 (convert (cmp@0 @1 @2))
185 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))))
186 )
187 /* `a ^ b` is another form of `a != b` when the type
188 is a 1bit precission integer. */
189 (match (maybe_cmp @0)
190 (bit_xor@0 @1 @2)
191 (if (INTEGRAL_TYPE_P (type)
192 && TYPE_PRECISION (type) == 1)))
193 /* maybe_bit_not is used to match what
194 is acceptable for bitwise_inverted_equal_p. */
195 (match (maybe_bit_not @0)
196 (bit_not_with_nop@0 @1))
197 (match (maybe_bit_not @0)
198 (INTEGER_CST@0))
199 (match (maybe_bit_not @0)
200 (maybe_cmp@0 @1))
201 (match (maybe_bit_not @0)
202 (bit_xor_cst@0 @1 @2))
203
204 #if GIMPLE
205 (match (maybe_truncate @0)
206 (convert @0)
207 (if (INTEGRAL_TYPE_P (type)
208 && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (@0)))))
209 #endif
210
211 /* Transform likes of (char) ABS_EXPR <(int) x> into (char) ABSU_EXPR <x>
212 ABSU_EXPR returns unsigned absolute value of the operand and the operand
213 of the ABSU_EXPR will have the corresponding signed type. */
214 (simplify (abs (convert @0))
215 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
216 && !TYPE_UNSIGNED (TREE_TYPE (@0))
217 && element_precision (type) > element_precision (TREE_TYPE (@0))
218 && (!VECTOR_TYPE_P (type)
219 || target_supports_op_p (TREE_TYPE (@0), ABSU_EXPR, optab_vector)))
220 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
221 (convert (absu:utype @0)))))
222
223 #if GIMPLE
224 /* Optimize (X + (X >> (prec - 1))) ^ (X >> (prec - 1)) into abs (X). */
225 (simplify
226 (bit_xor:c (plus:c @0 (rshift@2 @0 INTEGER_CST@1)) @2)
227 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
228 && !TYPE_UNSIGNED (TREE_TYPE (@0))
229 && wi::to_widest (@1) == element_precision (TREE_TYPE (@0)) - 1)
230 (abs @0)))
231 #endif
232
233 /* Simplifications of operations with one constant operand and
234 simplifications to constants or single values. */
235
236 (for op (plus pointer_plus minus bit_ior bit_xor)
237 (simplify
238 (op @0 integer_zerop)
239 (non_lvalue @0)))
240
241 /* 0 +p index -> (type)index */
242 (simplify
243 (pointer_plus integer_zerop @1)
244 (non_lvalue (convert @1)))
245
246 /* ptr - 0 -> (type)ptr */
247 (simplify
248 (pointer_diff @0 integer_zerop)
249 (convert @0))
250
251 /* See if ARG1 is zero and X + ARG1 reduces to X.
252 Likewise if the operands are reversed. */
253 (simplify
254 (plus:c @0 real_zerop@1)
255 (if (fold_real_zero_addition_p (type, @0, @1, 0))
256 (non_lvalue @0)))
257
258 /* See if ARG1 is zero and X - ARG1 reduces to X. */
259 (simplify
260 (minus @0 real_zerop@1)
261 (if (fold_real_zero_addition_p (type, @0, @1, 1))
262 (non_lvalue @0)))
263
264 /* Even if the fold_real_zero_addition_p can't simplify X + 0.0
265 into X, we can optimize (X + 0.0) + 0.0 or (X + 0.0) - 0.0
266 or (X - 0.0) + 0.0 into X + 0.0 and (X - 0.0) - 0.0 into X - 0.0
267 if not -frounding-math. For sNaNs the first operation would raise
268 exceptions but turn the result into qNan, so the second operation
269 would not raise it. */
270 (for inner_op (plus minus)
271 (for outer_op (plus minus)
272 (simplify
273 (outer_op (inner_op@3 @0 REAL_CST@1) REAL_CST@2)
274 (if (real_zerop (@1)
275 && real_zerop (@2)
276 && !HONOR_SIGN_DEPENDENT_ROUNDING (type))
277 (with { bool inner_plus = ((inner_op == PLUS_EXPR)
278 ^ REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@1)));
279 bool outer_plus
280 = ((outer_op == PLUS_EXPR)
281 ^ REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@2))); }
282 (if (outer_plus && !inner_plus)
283 (outer_op @0 @2)
284 @3))))))
285
286 /* Simplify x - x.
287 This is unsafe for certain floats even in non-IEEE formats.
288 In IEEE, it is unsafe because it does wrong for NaNs.
289 PR middle-end/98420: x - x may be -0.0 with FE_DOWNWARD.
290 Also note that operand_equal_p is always false if an operand
291 is volatile. */
292 (simplify
293 (minus @0 @0)
294 (if (!FLOAT_TYPE_P (type)
295 || (!tree_expr_maybe_nan_p (@0)
296 && !tree_expr_maybe_infinite_p (@0)
297 && (!HONOR_SIGN_DEPENDENT_ROUNDING (type)
298 || !HONOR_SIGNED_ZEROS (type))))
299 { build_zero_cst (type); }))
300 (simplify
301 (pointer_diff @@0 @0)
302 { build_zero_cst (type); })
303
304 (simplify
305 (mult @0 integer_zerop@1)
306 @1)
307
308 #if GIMPLE
309 /* When multiplying a value by a boolean involving the value, we may
310 be able to simplify further.
311 a * ((a || b) != 0) -> a
312 a * ((a || b) == 0) -> 0
313
314 There are also bit-and cases which don't show up in practice yet.
315 a * ((a && b) != 0) -> a * b
316 a * ((a && b) == 0) -> b != 0 ? a : b */
317 (for neeq (ne eq)
318 (simplify
319 (mult:c (convert? (neeq (bit_ior:c @0 @1) integer_zerop@2)) @0)
320 (if (neeq == EQ_EXPR)
321 { build_zero_cst (type); }
322 @0)))
323 #endif
324
325 /* -x == x -> x == 0 */
326 (for cmp (eq ne)
327 (simplify
328 (cmp:c @0 (negate @0))
329 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
330 && !TYPE_OVERFLOW_WRAPS (TREE_TYPE(@0)))
331 (cmp @0 { build_zero_cst (TREE_TYPE(@0)); }))))
332
333 /* Maybe fold x * 0 to 0. The expressions aren't the same
334 when x is NaN, since x * 0 is also NaN. Nor are they the
335 same in modes with signed zeros, since multiplying a
336 negative value by 0 gives -0, not +0. Nor when x is +-Inf,
337 since x * 0 is NaN. */
338 (simplify
339 (mult @0 real_zerop@1)
340 (if (!tree_expr_maybe_nan_p (@0)
341 && (!HONOR_NANS (type) || !tree_expr_maybe_infinite_p (@0))
342 && (!HONOR_SIGNED_ZEROS (type) || tree_expr_nonnegative_p (@0)))
343 @1))
344
345 /* In IEEE floating point, x*1 is not equivalent to x for snans.
346 Likewise for complex arithmetic with signed zeros. */
347 (simplify
348 (mult @0 real_onep)
349 (if (!tree_expr_maybe_signaling_nan_p (@0)
350 && (!HONOR_SIGNED_ZEROS (type)
351 || !COMPLEX_FLOAT_TYPE_P (type)))
352 (non_lvalue @0)))
353
354 /* Transform x * -1.0 into -x. */
355 (simplify
356 (mult @0 real_minus_onep)
357 (if (!tree_expr_maybe_signaling_nan_p (@0)
358 && (!HONOR_SIGNED_ZEROS (type)
359 || !COMPLEX_FLOAT_TYPE_P (type)))
360 (negate @0)))
361
362 /* Transform x * { 0 or 1, 0 or 1, ... } into x & { 0 or -1, 0 or -1, ...},
363 unless the target has native support for the former but not the latter. */
364 (simplify
365 (mult @0 VECTOR_CST@1)
366 (if (initializer_each_zero_or_onep (@1)
367 && !HONOR_SNANS (type)
368 && !HONOR_SIGNED_ZEROS (type))
369 (with { tree itype = FLOAT_TYPE_P (type) ? unsigned_type_for (type) : type; }
370 (if (itype
371 && (!VECTOR_MODE_P (TYPE_MODE (type))
372 || (VECTOR_MODE_P (TYPE_MODE (itype))
373 && optab_handler (and_optab,
374 TYPE_MODE (itype)) != CODE_FOR_nothing)))
375 (view_convert (bit_and:itype (view_convert @0)
376 (ne @1 { build_zero_cst (type); })))))))
377
378 /* In SWAR (SIMD within a register) code a signed comparison of packed data
379 can be constructed with a particular combination of shift, bitwise and,
380 and multiplication by constants. If that code is vectorized we can
381 convert this pattern into a more efficient vector comparison. */
382 (simplify
383 (mult (bit_and (rshift @0 uniform_integer_cst_p@1)
384 uniform_integer_cst_p@2)
385 uniform_integer_cst_p@3)
386 (with {
387 tree rshift_cst = uniform_integer_cst_p (@1);
388 tree bit_and_cst = uniform_integer_cst_p (@2);
389 tree mult_cst = uniform_integer_cst_p (@3);
390 }
391 /* Make sure we're working with vectors and uniform vector constants. */
392 (if (VECTOR_TYPE_P (type)
393 && tree_fits_uhwi_p (rshift_cst)
394 && tree_fits_uhwi_p (mult_cst)
395 && tree_fits_uhwi_p (bit_and_cst))
396 /* Compute what constants would be needed for this to represent a packed
397 comparison based on the shift amount denoted by RSHIFT_CST. */
398 (with {
399 HOST_WIDE_INT vec_elem_bits = vector_element_bits (type);
400 poly_int64 vec_nelts = TYPE_VECTOR_SUBPARTS (type);
401 poly_int64 vec_bits = vec_elem_bits * vec_nelts;
402 unsigned HOST_WIDE_INT cmp_bits_i, bit_and_i, mult_i;
403 unsigned HOST_WIDE_INT target_mult_i, target_bit_and_i;
404 cmp_bits_i = tree_to_uhwi (rshift_cst) + 1;
405 mult_i = tree_to_uhwi (mult_cst);
406 target_mult_i = (HOST_WIDE_INT_1U << cmp_bits_i) - 1;
407 bit_and_i = tree_to_uhwi (bit_and_cst);
408 target_bit_and_i = 0;
409
410 /* The bit pattern in BIT_AND_I should be a mask for the least
411 significant bit of each packed element that is CMP_BITS wide. */
412 for (unsigned i = 0; i < vec_elem_bits / cmp_bits_i; i++)
413 target_bit_and_i = (target_bit_and_i << cmp_bits_i) | 1U;
414 }
415 (if ((exact_log2 (cmp_bits_i)) >= 0
416 && cmp_bits_i < HOST_BITS_PER_WIDE_INT
417 && multiple_p (vec_bits, cmp_bits_i)
418 && vec_elem_bits <= HOST_BITS_PER_WIDE_INT
419 && target_mult_i == mult_i
420 && target_bit_and_i == bit_and_i)
421 /* Compute the vector shape for the comparison and check if the target is
422 able to expand the comparison with that type. */
423 (with {
424 /* We're doing a signed comparison. */
425 tree cmp_type = build_nonstandard_integer_type (cmp_bits_i, 0);
426 poly_int64 vector_type_nelts = exact_div (vec_bits, cmp_bits_i);
427 tree vec_cmp_type = build_vector_type (cmp_type, vector_type_nelts);
428 tree vec_truth_type = truth_type_for (vec_cmp_type);
429 tree zeros = build_zero_cst (vec_cmp_type);
430 tree ones = build_all_ones_cst (vec_cmp_type);
431 }
432 (if (expand_vec_cmp_expr_p (vec_cmp_type, vec_truth_type, LT_EXPR)
433 && expand_vec_cond_expr_p (vec_cmp_type, vec_truth_type))
434 (view_convert:type (vec_cond (lt:vec_truth_type
435 (view_convert:vec_cmp_type @0)
436 { zeros; })
437 { ones; } { zeros; })))))))))
438
439 (for cmp (gt ge lt le)
440 outp (convert convert negate negate)
441 outn (negate negate convert convert)
442 /* Transform X * (X > 0.0 ? 1.0 : -1.0) into abs(X). */
443 /* Transform X * (X >= 0.0 ? 1.0 : -1.0) into abs(X). */
444 /* Transform X * (X < 0.0 ? 1.0 : -1.0) into -abs(X). */
445 /* Transform X * (X <= 0.0 ? 1.0 : -1.0) into -abs(X). */
446 (simplify
447 (mult:c @0 (cond (cmp @0 real_zerop) real_onep@1 real_minus_onep))
448 (if (!tree_expr_maybe_nan_p (@0) && !HONOR_SIGNED_ZEROS (type))
449 (outp (abs @0))))
450 /* Transform X * (X > 0.0 ? -1.0 : 1.0) into -abs(X). */
451 /* Transform X * (X >= 0.0 ? -1.0 : 1.0) into -abs(X). */
452 /* Transform X * (X < 0.0 ? -1.0 : 1.0) into abs(X). */
453 /* Transform X * (X <= 0.0 ? -1.0 : 1.0) into abs(X). */
454 (simplify
455 (mult:c @0 (cond (cmp @0 real_zerop) real_minus_onep real_onep@1))
456 (if (!tree_expr_maybe_nan_p (@0) && !HONOR_SIGNED_ZEROS (type))
457 (outn (abs @0)))))
458
459 /* Transform X * copysign (1.0, X) into abs(X). */
460 (simplify
461 (mult:c @0 (COPYSIGN_ALL real_onep @0))
462 (if (!tree_expr_maybe_nan_p (@0) && !HONOR_SIGNED_ZEROS (type))
463 (abs @0)))
464
465 /* Transform X * copysign (1.0, -X) into -abs(X). */
466 (simplify
467 (mult:c @0 (COPYSIGN_ALL real_onep (negate @0)))
468 (if (!tree_expr_maybe_nan_p (@0) && !HONOR_SIGNED_ZEROS (type))
469 (negate (abs @0))))
470
471 /* Transform copysign (CST, X) into copysign (ABS(CST), X). */
472 (simplify
473 (COPYSIGN_ALL REAL_CST@0 @1)
474 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@0)))
475 (COPYSIGN_ALL (negate @0) @1)))
476
477 /* Transform c ? x * copysign (1, y) : z to c ? x ^ signs(y) : z.
478 tree-ssa-math-opts.cc does the corresponding optimization for
479 unconditional multiplications (via xorsign). */
480 (simplify
481 (IFN_COND_MUL:c @0 @1 (IFN_COPYSIGN real_onep @2) @3)
482 (with { tree signs = sign_mask_for (type); }
483 (if (signs)
484 (with { tree inttype = TREE_TYPE (signs); }
485 (view_convert:type
486 (IFN_COND_XOR:inttype @0
487 (view_convert:inttype @1)
488 (bit_and (view_convert:inttype @2) { signs; })
489 (view_convert:inttype @3)))))))
490
491 /* (x >= 0 ? x : 0) + (x <= 0 ? -x : 0) -> abs x. */
492 (simplify
493 (plus:c (max @0 integer_zerop) (max (negate @0) integer_zerop))
494 (if (ANY_INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_UNDEFINED (type))
495 (abs @0)))
496
497 /* X * 1, X / 1 -> X. */
498 (for op (mult trunc_div ceil_div floor_div round_div exact_div)
499 (simplify
500 (op @0 integer_onep)
501 (non_lvalue @0)))
502
503 /* (A / (1 << B)) -> (A >> B).
504 Only for unsigned A. For signed A, this would not preserve rounding
505 toward zero.
506 For example: (-1 / ( 1 << B)) != -1 >> B.
507 Also handle widening conversions, like:
508 (A / (unsigned long long) (1U << B)) -> (A >> B)
509 or
510 (A / (unsigned long long) (1 << B)) -> (A >> B).
511 If the left shift is signed, it can be done only if the upper bits
512 of A starting from shift's type sign bit are zero, as
513 (unsigned long long) (1 << 31) is -2147483648ULL, not 2147483648ULL,
514 so it is valid only if A >> 31 is zero. */
515 (for div (trunc_div exact_div)
516 (simplify
517 (div (convert?@0 @3) (convert2? (lshift integer_onep@1 @2)))
518 (if ((TYPE_UNSIGNED (type) || tree_expr_nonnegative_p (@0))
519 && (!VECTOR_TYPE_P (type)
520 || target_supports_op_p (type, RSHIFT_EXPR, optab_vector)
521 || target_supports_op_p (type, RSHIFT_EXPR, optab_scalar))
522 && (useless_type_conversion_p (type, TREE_TYPE (@1))
523 || (element_precision (type) >= element_precision (TREE_TYPE (@1))
524 && (TYPE_UNSIGNED (TREE_TYPE (@1))
525 || (element_precision (type)
526 == element_precision (TREE_TYPE (@1)))
527 || (INTEGRAL_TYPE_P (type)
528 && (tree_nonzero_bits (@0)
529 & wi::mask (element_precision (TREE_TYPE (@1)) - 1,
530 true,
531 element_precision (type))) == 0)))))
532 (if (!VECTOR_TYPE_P (type)
533 && useless_type_conversion_p (TREE_TYPE (@3), TREE_TYPE (@1))
534 && element_precision (TREE_TYPE (@3)) < element_precision (type))
535 (convert (rshift @3 @2))
536 (rshift @0 @2)))))
537
538 /* Preserve explicit divisions by 0: the C++ front-end wants to detect
539 undefined behavior in constexpr evaluation, and assuming that the division
540 traps enables better optimizations than these anyway. */
541 (for div (trunc_div ceil_div floor_div round_div exact_div)
542 /* 0 / X is always zero. */
543 (simplify
544 (div integer_zerop@0 @1)
545 /* But not for 0 / 0 so that we can get the proper warnings and errors. */
546 (if (!integer_zerop (@1))
547 @0))
548 /* X / -1 is -X. */
549 (simplify
550 (div @0 integer_minus_onep@1)
551 (if (!TYPE_UNSIGNED (type))
552 (negate @0)))
553 /* X / bool_range_Y is X. */
554 (simplify
555 (div @0 SSA_NAME@1)
556 (if (INTEGRAL_TYPE_P (type)
557 && ssa_name_has_boolean_range (@1)
558 && !flag_non_call_exceptions)
559 @0))
560 /* X / X is one. */
561 (simplify
562 (div @0 @0)
563 /* But not for 0 / 0 so that we can get the proper warnings and errors.
564 And not for _Fract types where we can't build 1. */
565 (if (!ALL_FRACT_MODE_P (TYPE_MODE (type))
566 && !integer_zerop (@0)
567 && (!flag_non_call_exceptions || tree_expr_nonzero_p (@0)))
568 { build_one_cst (type); }))
569 /* X / abs (X) is X < 0 ? -1 : 1. */
570 (simplify
571 (div:C @0 (abs @0))
572 (if (INTEGRAL_TYPE_P (type)
573 && TYPE_OVERFLOW_UNDEFINED (type)
574 && !integer_zerop (@0)
575 && (!flag_non_call_exceptions || tree_expr_nonzero_p (@0)))
576 (cond (lt @0 { build_zero_cst (type); })
577 { build_minus_one_cst (type); } { build_one_cst (type); })))
578 /* X / -X is -1. */
579 (simplify
580 (div:C @0 (negate @0))
581 (if ((INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
582 && TYPE_OVERFLOW_UNDEFINED (type)
583 && !integer_zerop (@0)
584 && (!flag_non_call_exceptions || tree_expr_nonzero_p (@0)))
585 { build_minus_one_cst (type); })))
586
587 /* For unsigned integral types, FLOOR_DIV_EXPR is the same as
588 TRUNC_DIV_EXPR. Rewrite into the latter in this case. Similarly
589 for MOD instead of DIV. */
590 (for floor_divmod (floor_div floor_mod)
591 trunc_divmod (trunc_div trunc_mod)
592 (simplify
593 (floor_divmod @0 @1)
594 (if ((INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
595 && TYPE_UNSIGNED (type))
596 (trunc_divmod @0 @1))))
597
598 /* 1 / X -> X == 1 for unsigned integer X.
599 1 / X -> X >= -1 && X <= 1 ? X : 0 for signed integer X.
600 But not for 1 / 0 so that we can get proper warnings and errors,
601 and not for 1-bit integers as they are edge cases better handled
602 elsewhere. Delay the conversion of the signed division until late
603 because `1 / X` is simplier to handle than the resulting COND_EXPR. */
604 (simplify
605 (trunc_div integer_onep@0 @1)
606 (if (INTEGRAL_TYPE_P (type)
607 && TYPE_PRECISION (type) > 1
608 && !integer_zerop (@1)
609 && (!flag_non_call_exceptions || tree_expr_nonzero_p (@1)))
610 (if (TYPE_UNSIGNED (type))
611 (convert (eq:boolean_type_node @1 { build_one_cst (type); }))
612 (if (!canonicalize_math_p ())
613 (with { tree utype = unsigned_type_for (type); }
614 (cond (le (plus (convert:utype @1) { build_one_cst (utype); })
615 { build_int_cst (utype, 2); })
616 @1 { build_zero_cst (type); }))))))
617
618 /* Combine two successive divisions. Note that combining ceil_div
619 and floor_div is trickier and combining round_div even more so. */
620 (for div (trunc_div exact_div)
621 (simplify
622 (div (div@3 @0 INTEGER_CST@1) INTEGER_CST@2)
623 (with {
624 wi::overflow_type overflow;
625 wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@2),
626 TYPE_SIGN (type), &overflow);
627 }
628 (if (div == EXACT_DIV_EXPR
629 || optimize_successive_divisions_p (@2, @3))
630 (if (!overflow)
631 (div @0 { wide_int_to_tree (type, mul); })
632 (if (TYPE_UNSIGNED (type)
633 || mul != wi::min_value (TYPE_PRECISION (type), SIGNED))
634 { build_zero_cst (type); }))))))
635
636 /* Combine successive multiplications. Similar to above, but handling
637 overflow is different. */
638 (simplify
639 (mult (mult @0 INTEGER_CST@1) INTEGER_CST@2)
640 (with {
641 wi::overflow_type overflow;
642 wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@2),
643 TYPE_SIGN (type), &overflow);
644 }
645 /* Skip folding on overflow: the only special case is @1 * @2 == -INT_MIN,
646 otherwise undefined overflow implies that @0 must be zero. */
647 (if (!overflow || TYPE_OVERFLOW_WRAPS (type))
648 (mult @0 { wide_int_to_tree (type, mul); }))))
649
650 /* Similar to above, but there could be an extra add/sub between
651 successive multuiplications. */
652 (simplify
653 (mult (plus:s (mult:s@4 @0 INTEGER_CST@1) INTEGER_CST@2) INTEGER_CST@3)
654 (with {
655 bool overflowed = true;
656 wi::overflow_type ovf1, ovf2;
657 wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@3),
658 TYPE_SIGN (type), &ovf1);
659 wide_int add = wi::mul (wi::to_wide (@2), wi::to_wide (@3),
660 TYPE_SIGN (type), &ovf2);
661 if (TYPE_OVERFLOW_UNDEFINED (type))
662 {
663 #if GIMPLE
664 int_range_max vr0;
665 if (ovf1 == wi::OVF_NONE && ovf2 == wi::OVF_NONE
666 && get_global_range_query ()->range_of_expr (vr0, @4)
667 && !vr0.varying_p () && !vr0.undefined_p ())
668 {
669 wide_int wmin0 = vr0.lower_bound ();
670 wide_int wmax0 = vr0.upper_bound ();
671 wmin0 = wi::mul (wmin0, wi::to_wide (@3), TYPE_SIGN (type), &ovf1);
672 wmax0 = wi::mul (wmax0, wi::to_wide (@3), TYPE_SIGN (type), &ovf2);
673 if (ovf1 == wi::OVF_NONE && ovf2 == wi::OVF_NONE)
674 {
675 wi::add (wmin0, add, TYPE_SIGN (type), &ovf1);
676 wi::add (wmax0, add, TYPE_SIGN (type), &ovf2);
677 if (ovf1 == wi::OVF_NONE && ovf2 == wi::OVF_NONE)
678 overflowed = false;
679 }
680 }
681 #endif
682 }
683 else
684 overflowed = false;
685 }
686 /* Skip folding on overflow. */
687 (if (!overflowed)
688 (plus (mult @0 { wide_int_to_tree (type, mul); })
689 { wide_int_to_tree (type, add); }))))
690
691 /* Similar to above, but a multiplication between successive additions. */
692 (simplify
693 (plus (mult:s (plus:s @0 INTEGER_CST@1) INTEGER_CST@2) INTEGER_CST@3)
694 (with {
695 bool overflowed = true;
696 wi::overflow_type ovf1;
697 wi::overflow_type ovf2;
698 wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@2),
699 TYPE_SIGN (type), &ovf1);
700 wide_int add = wi::add (mul, wi::to_wide (@3),
701 TYPE_SIGN (type), &ovf2);
702 if (TYPE_OVERFLOW_UNDEFINED (type))
703 {
704 #if GIMPLE
705 int_range_max vr0;
706 if (ovf1 == wi::OVF_NONE && ovf2 == wi::OVF_NONE
707 && get_global_range_query ()->range_of_expr (vr0, @0)
708 && !vr0.varying_p () && !vr0.undefined_p ())
709 {
710 wide_int wmin0 = vr0.lower_bound ();
711 wide_int wmax0 = vr0.upper_bound ();
712 wmin0 = wi::mul (wmin0, wi::to_wide (@2), TYPE_SIGN (type), &ovf1);
713 wmax0 = wi::mul (wmax0, wi::to_wide (@2), TYPE_SIGN (type), &ovf2);
714 if (ovf1 == wi::OVF_NONE && ovf2 == wi::OVF_NONE)
715 {
716 wi::add (wmin0, mul, TYPE_SIGN (type), &ovf1);
717 wi::add (wmax0, mul, TYPE_SIGN (type), &ovf2);
718 if (ovf1 == wi::OVF_NONE && ovf2 == wi::OVF_NONE)
719 overflowed = false;
720 }
721 }
722 #endif
723 }
724 else
725 overflowed = false;
726 }
727 /* Skip folding on overflow. */
728 (if (!overflowed)
729 (plus (mult @0 @2) { wide_int_to_tree (type, add); }))))
730
731 /* Optimize A / A to 1.0 if we don't care about
732 NaNs or Infinities. */
733 (simplify
734 (rdiv @0 @0)
735 (if (FLOAT_TYPE_P (type)
736 && ! HONOR_NANS (type)
737 && ! HONOR_INFINITIES (type))
738 { build_one_cst (type); }))
739
740 /* Optimize -A / A to -1.0 if we don't care about
741 NaNs or Infinities. */
742 (simplify
743 (rdiv:C @0 (negate @0))
744 (if (FLOAT_TYPE_P (type)
745 && ! HONOR_NANS (type)
746 && ! HONOR_INFINITIES (type))
747 { build_minus_one_cst (type); }))
748
749 /* PR71078: x / abs(x) -> copysign (1.0, x) */
750 (simplify
751 (rdiv:C (convert? @0) (convert? (abs @0)))
752 (if (SCALAR_FLOAT_TYPE_P (type)
753 && ! HONOR_NANS (type)
754 && ! HONOR_INFINITIES (type))
755 (switch
756 (if (types_match (type, float_type_node))
757 (BUILT_IN_COPYSIGNF { build_one_cst (type); } (convert @0)))
758 (if (types_match (type, double_type_node))
759 (BUILT_IN_COPYSIGN { build_one_cst (type); } (convert @0)))
760 (if (types_match (type, long_double_type_node))
761 (BUILT_IN_COPYSIGNL { build_one_cst (type); } (convert @0))))))
762
763 /* In IEEE floating point, x/1 is not equivalent to x for snans. */
764 (simplify
765 (rdiv @0 real_onep)
766 (if (!tree_expr_maybe_signaling_nan_p (@0))
767 (non_lvalue @0)))
768
769 /* In IEEE floating point, x/-1 is not equivalent to -x for snans. */
770 (simplify
771 (rdiv @0 real_minus_onep)
772 (if (!tree_expr_maybe_signaling_nan_p (@0))
773 (negate @0)))
774
775 (if (flag_reciprocal_math)
776 /* Convert (A/B)/C to A/(B*C). */
777 (simplify
778 (rdiv (rdiv:s @0 @1) @2)
779 (rdiv @0 (mult @1 @2)))
780
781 /* Canonicalize x / (C1 * y) to (x * C2) / y. */
782 (simplify
783 (rdiv @0 (mult:s @1 REAL_CST@2))
784 (with
785 { tree tem = const_binop (RDIV_EXPR, type, build_one_cst (type), @2); }
786 (if (tem)
787 (rdiv (mult @0 { tem; } ) @1))))
788
789 /* Convert A/(B/C) to (A/B)*C */
790 (simplify
791 (rdiv @0 (rdiv:s @1 @2))
792 (mult (rdiv @0 @1) @2)))
793
794 /* Simplify x / (- y) to -x / y. */
795 (simplify
796 (rdiv @0 (negate @1))
797 (rdiv (negate @0) @1))
798
799 (if (flag_unsafe_math_optimizations)
800 /* Simplify (C / x op 0.0) to x op 0.0 for C != 0, C != Inf/Nan.
801 Since C / x may underflow to zero, do this only for unsafe math. */
802 (for op (lt le gt ge)
803 neg_op (gt ge lt le)
804 (simplify
805 (op (rdiv REAL_CST@0 @1) real_zerop@2)
806 (if (!HONOR_SIGNED_ZEROS (@1) && !HONOR_INFINITIES (@1))
807 (switch
808 (if (real_less (&dconst0, TREE_REAL_CST_PTR (@0)))
809 (op @1 @2))
810 /* For C < 0, use the inverted operator. */
811 (if (real_less (TREE_REAL_CST_PTR (@0), &dconst0))
812 (neg_op @1 @2)))))))
813
814 /* Optimize (X & (-A)) / A where A is a power of 2, to X >> log2(A) */
815 (for div (trunc_div ceil_div floor_div round_div exact_div)
816 (simplify
817 (div (convert? (bit_and @0 INTEGER_CST@1)) INTEGER_CST@2)
818 (if (integer_pow2p (@2)
819 && tree_int_cst_sgn (@2) > 0
820 && tree_nop_conversion_p (type, TREE_TYPE (@0))
821 && wi::to_wide (@2) + wi::to_wide (@1) == 0)
822 (rshift (convert @0)
823 { build_int_cst (integer_type_node,
824 wi::exact_log2 (wi::to_wide (@2))); }))))
825
826 /* If ARG1 is a constant, we can convert this to a multiply by the
827 reciprocal. This does not have the same rounding properties,
828 so only do this if -freciprocal-math. We can actually
829 always safely do it if ARG1 is a power of two, but it's hard to
830 tell if it is or not in a portable manner. */
831 (for cst (REAL_CST COMPLEX_CST VECTOR_CST)
832 (simplify
833 (rdiv @0 cst@1)
834 (if (optimize)
835 (if (flag_reciprocal_math
836 && !real_zerop (@1))
837 (with
838 { tree tem = const_binop (RDIV_EXPR, type, build_one_cst (type), @1); }
839 (if (tem)
840 (mult @0 { tem; } )))
841 (if (cst != COMPLEX_CST)
842 (with { tree inverse = exact_inverse (type, @1); }
843 (if (inverse)
844 (mult @0 { inverse; } ))))))))
845
846 (for mod (ceil_mod floor_mod round_mod trunc_mod)
847 /* 0 % X is always zero. */
848 (simplify
849 (mod integer_zerop@0 @1)
850 /* But not for 0 % 0 so that we can get the proper warnings and errors. */
851 (if (!integer_zerop (@1))
852 @0))
853 /* X % 1 is always zero. */
854 (simplify
855 (mod @0 integer_onep)
856 { build_zero_cst (type); })
857 /* X % -1 is zero. */
858 (simplify
859 (mod @0 integer_minus_onep@1)
860 (if (!TYPE_UNSIGNED (type))
861 { build_zero_cst (type); }))
862 /* X % X is zero. */
863 (simplify
864 (mod @0 @0)
865 /* But not for 0 % 0 so that we can get the proper warnings and errors. */
866 (if (!integer_zerop (@0))
867 { build_zero_cst (type); }))
868 /* (X % Y) % Y is just X % Y. */
869 (simplify
870 (mod (mod@2 @0 @1) @1)
871 @2)
872 /* From extract_muldiv_1: (X * C1) % C2 is zero if C1 is a multiple of C2. */
873 (simplify
874 (mod (mult @0 INTEGER_CST@1) INTEGER_CST@2)
875 (if (ANY_INTEGRAL_TYPE_P (type)
876 && TYPE_OVERFLOW_UNDEFINED (type)
877 && wi::multiple_of_p (wi::to_wide (@1), wi::to_wide (@2),
878 TYPE_SIGN (type)))
879 { build_zero_cst (type); }))
880 /* For (X % C) == 0, if X is signed and C is power of 2, use unsigned
881 modulo and comparison, since it is simpler and equivalent. */
882 (for cmp (eq ne)
883 (simplify
884 (cmp (mod @0 integer_pow2p@2) integer_zerop@1)
885 (if (!TYPE_UNSIGNED (TREE_TYPE (@0)))
886 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
887 (cmp (mod (convert:utype @0) (convert:utype @2)) (convert:utype @1)))))))
888
889 /* X % -C is the same as X % C. */
890 (simplify
891 (trunc_mod @0 INTEGER_CST@1)
892 (if (TYPE_SIGN (type) == SIGNED
893 && !TREE_OVERFLOW (@1)
894 && wi::neg_p (wi::to_wide (@1))
895 && !TYPE_OVERFLOW_TRAPS (type)
896 /* Avoid this transformation if C is INT_MIN, i.e. C == -C. */
897 && !sign_bit_p (@1, @1))
898 (trunc_mod @0 (negate @1))))
899
900 /* X % -Y is the same as X % Y. */
901 (simplify
902 (trunc_mod @0 (convert? (negate @1)))
903 (if (INTEGRAL_TYPE_P (type)
904 && !TYPE_UNSIGNED (type)
905 && !TYPE_OVERFLOW_TRAPS (type)
906 && tree_nop_conversion_p (type, TREE_TYPE (@1))
907 /* Avoid this transformation if X might be INT_MIN or
908 Y might be -1, because we would then change valid
909 INT_MIN % -(-1) into invalid INT_MIN % -1. */
910 && (expr_not_equal_to (@0, wi::to_wide (TYPE_MIN_VALUE (type)))
911 || expr_not_equal_to (@1, wi::minus_one (TYPE_PRECISION
912 (TREE_TYPE (@1))))))
913 (trunc_mod @0 (convert @1))))
914
915 /* X - (X / Y) * Y is the same as X % Y. */
916 (simplify
917 (minus (convert1? @0) (convert2? (mult:c (trunc_div @@0 @@1) @1)))
918 (if (INTEGRAL_TYPE_P (type)
919 || (VECTOR_INTEGER_TYPE_P (type)
920 && ((optimize_vectors_before_lowering_p ()
921 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
922 || target_supports_op_p (type, TRUNC_MOD_EXPR,
923 optab_vector))))
924 (convert (trunc_mod @0 @1))))
925
926 /* x * (1 + y / x) - y -> x - y % x */
927 (simplify
928 (minus (mult:cs @0 (plus:s (trunc_div:s @1 @0) integer_onep)) @1)
929 (if (INTEGRAL_TYPE_P (type))
930 (minus @0 (trunc_mod @1 @0))))
931
932 /* Optimize TRUNC_MOD_EXPR by a power of two into a BIT_AND_EXPR,
933 i.e. "X % C" into "X & (C - 1)", if X and C are positive.
934 Also optimize A % (C << N) where C is a power of 2,
935 to A & ((C << N) - 1).
936 Also optimize "A shift (B % C)", if C is a power of 2, to
937 "A shift (B & (C - 1))". SHIFT operation include "<<" and ">>"
938 and assume (B % C) is nonnegative as shifts negative values would
939 be UB. */
940 (match (power_of_two_cand @1)
941 INTEGER_CST@1)
942 (match (power_of_two_cand @1)
943 (lshift INTEGER_CST@1 @2))
944 (for mod (trunc_mod floor_mod)
945 (for shift (lshift rshift)
946 (simplify
947 (shift @0 (mod @1 (power_of_two_cand@2 @3)))
948 (if (integer_pow2p (@3) && tree_int_cst_sgn (@3) > 0)
949 (shift @0 (bit_and @1 (minus @2 { build_int_cst (TREE_TYPE (@2),
950 1); }))))))
951 (simplify
952 (mod @0 (convert? (power_of_two_cand@1 @2)))
953 (if ((TYPE_UNSIGNED (type) || tree_expr_nonnegative_p (@0))
954 /* Allow any integral conversions of the divisor, except
955 conversion from narrower signed to wider unsigned type
956 where if @1 would be negative power of two, the divisor
957 would not be a power of two. */
958 && INTEGRAL_TYPE_P (type)
959 && INTEGRAL_TYPE_P (TREE_TYPE (@1))
960 && (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@1))
961 || TYPE_UNSIGNED (TREE_TYPE (@1))
962 || !TYPE_UNSIGNED (type))
963 && integer_pow2p (@2) && tree_int_cst_sgn (@2) > 0)
964 (with { tree utype = TREE_TYPE (@1);
965 if (!TYPE_OVERFLOW_WRAPS (utype))
966 utype = unsigned_type_for (utype); }
967 (bit_and @0 (convert (minus (convert:utype @1)
968 { build_one_cst (utype); })))))))
969
970 /* Simplify (unsigned t * 2)/2 -> unsigned t & 0x7FFFFFFF. */
971 (for div (trunc_div exact_div)
972 (simplify
973 (div (mult @0 integer_pow2p@1) @1)
974 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) && TYPE_UNSIGNED (TREE_TYPE (@0)))
975 (bit_and @0 { wide_int_to_tree
976 (type, wi::mask (TYPE_PRECISION (type)
977 - wi::exact_log2 (wi::to_wide (@1)),
978 false, TYPE_PRECISION (type))); }))))
979
980 /* Simplify (unsigned t / 2) * 2 -> unsigned t & ~1. */
981 (simplify
982 (mult (trunc_div @0 integer_pow2p@1) @1)
983 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) && TYPE_UNSIGNED (TREE_TYPE (@0)))
984 (bit_and @0 (negate @1))))
985
986 /* Canonicalize unsigned t / 4 to t >> 2. */
987 #if GIMPLE
988 (for div (trunc_div floor_div exact_div)
989 (simplify
990 (div @0 integer_pow2p@1)
991 (if (INTEGRAL_TYPE_P (type)
992 && (TYPE_UNSIGNED (type) || tree_expr_nonnegative_p (@0)))
993 (rshift @0 { build_int_cst (integer_type_node,
994 wi::exact_log2 (wi::to_wide (@1))); }))))
995 #endif
996
997 (for div (trunc_div ceil_div floor_div round_div exact_div)
998 /* Simplify (t * u) / u -> t. */
999 (simplify
1000 (div (mult:c @0 @1) @1)
1001 (if (ANY_INTEGRAL_TYPE_P (type))
1002 (if (TYPE_OVERFLOW_UNDEFINED (type) && !TYPE_OVERFLOW_SANITIZED (type))
1003 @0
1004 #if GIMPLE
1005 (with {int_range_max vr0, vr1;}
1006 (if (INTEGRAL_TYPE_P (type)
1007 && get_range_query (cfun)->range_of_expr (vr0, @0)
1008 && get_range_query (cfun)->range_of_expr (vr1, @1)
1009 && range_op_handler (MULT_EXPR).overflow_free_p (vr0, vr1))
1010 @0))
1011 #endif
1012 )))
1013 #if GIMPLE
1014 /* Simplify (t * u) / v -> t * (u / v) if u is multiple of v. */
1015 (simplify
1016 (div (mult @0 INTEGER_CST@1) INTEGER_CST@2)
1017 (if (INTEGRAL_TYPE_P (type)
1018 && wi::multiple_of_p (wi::to_widest (@1), wi::to_widest (@2), SIGNED))
1019 (if (TYPE_OVERFLOW_UNDEFINED (type) && !TYPE_OVERFLOW_SANITIZED (type))
1020 (mult @0 (div! @1 @2))
1021 (with {int_range_max vr0, vr1;}
1022 (if (get_range_query (cfun)->range_of_expr (vr0, @0)
1023 && get_range_query (cfun)->range_of_expr (vr1, @1)
1024 && range_op_handler (MULT_EXPR).overflow_free_p (vr0, vr1))
1025 (mult @0 (div! @1 @2))))
1026 )))
1027 #endif
1028 /* Simplify (t * u) / (t * v) -> (u / v) if u is multiple of v. */
1029 (simplify
1030 (div (mult @0 INTEGER_CST@1) (mult @0 INTEGER_CST@2))
1031 (if (INTEGRAL_TYPE_P (type)
1032 && wi::multiple_of_p (wi::to_widest (@1), wi::to_widest (@2), SIGNED))
1033 (if (TYPE_OVERFLOW_UNDEFINED (type) && !TYPE_OVERFLOW_SANITIZED (type))
1034 (div @1 @2)
1035 #if GIMPLE
1036 (with {int_range_max vr0, vr1, vr2;}
1037 (if (get_range_query (cfun)->range_of_expr (vr0, @0)
1038 && get_range_query (cfun)->range_of_expr (vr1, @1)
1039 && get_range_query (cfun)->range_of_expr (vr2, @2)
1040 && range_op_handler (MULT_EXPR).overflow_free_p (vr0, vr1)
1041 && range_op_handler (MULT_EXPR).overflow_free_p (vr0, vr2))
1042 (div @1 @2)))
1043 #endif
1044 ))))
1045
1046 #if GIMPLE
1047 (for div (trunc_div exact_div)
1048 /* Simplify (X + M*N) / N -> X / N + M. */
1049 (simplify
1050 (div (plus:c@4 @0 (mult:c@3 @1 @2)) @2)
1051 (with {int_range_max vr0, vr1, vr2, vr3, vr4;}
1052 (if (INTEGRAL_TYPE_P (type)
1053 && get_range_query (cfun)->range_of_expr (vr1, @1)
1054 && get_range_query (cfun)->range_of_expr (vr2, @2)
1055 /* "N*M" doesn't overflow. */
1056 && range_op_handler (MULT_EXPR).overflow_free_p (vr1, vr2)
1057 && get_range_query (cfun)->range_of_expr (vr0, @0)
1058 && get_range_query (cfun)->range_of_expr (vr3, @3)
1059 /* "X+(N*M)" doesn't overflow. */
1060 && range_op_handler (PLUS_EXPR).overflow_free_p (vr0, vr3)
1061 && get_range_query (cfun)->range_of_expr (vr4, @4)
1062 && !vr4.undefined_p ()
1063 /* "X+N*M" is not with opposite sign as "X". */
1064 && (TYPE_UNSIGNED (type)
1065 || (vr0.nonnegative_p () && vr4.nonnegative_p ())
1066 || (vr0.nonpositive_p () && vr4.nonpositive_p ())))
1067 (plus (div @0 @2) @1))))
1068
1069 /* Simplify (X - M*N) / N -> X / N - M. */
1070 (simplify
1071 (div (minus@4 @0 (mult:c@3 @1 @2)) @2)
1072 (with {int_range_max vr0, vr1, vr2, vr3, vr4;}
1073 (if (INTEGRAL_TYPE_P (type)
1074 && get_range_query (cfun)->range_of_expr (vr1, @1)
1075 && get_range_query (cfun)->range_of_expr (vr2, @2)
1076 /* "N * M" doesn't overflow. */
1077 && range_op_handler (MULT_EXPR).overflow_free_p (vr1, vr2)
1078 && get_range_query (cfun)->range_of_expr (vr0, @0)
1079 && get_range_query (cfun)->range_of_expr (vr3, @3)
1080 /* "X - (N*M)" doesn't overflow. */
1081 && range_op_handler (MINUS_EXPR).overflow_free_p (vr0, vr3)
1082 && get_range_query (cfun)->range_of_expr (vr4, @4)
1083 && !vr4.undefined_p ()
1084 /* "X-N*M" is not with opposite sign as "X". */
1085 && (TYPE_UNSIGNED (type)
1086 || (vr0.nonnegative_p () && vr4.nonnegative_p ())
1087 || (vr0.nonpositive_p () && vr4.nonpositive_p ())))
1088 (minus (div @0 @2) @1)))))
1089
1090 /* Simplify
1091 (X + C) / N -> X / N + C / N where C is multiple of N.
1092 (X + C) >> N -> X >> N + C>>N if low N bits of C is 0. */
1093 (for op (trunc_div exact_div rshift)
1094 (simplify
1095 (op (plus@3 @0 INTEGER_CST@1) INTEGER_CST@2)
1096 (with
1097 {
1098 wide_int c = wi::to_wide (@1);
1099 wide_int n = wi::to_wide (@2);
1100 bool shift = op == RSHIFT_EXPR;
1101 #define plus_op1(v) (shift ? wi::rshift (v, n, TYPE_SIGN (type)) \
1102 : wi::div_trunc (v, n, TYPE_SIGN (type)))
1103 #define exact_mod(v) (shift ? wi::ctz (v) >= n.to_shwi () \
1104 : wi::multiple_of_p (v, n, TYPE_SIGN (type)))
1105 int_range_max vr0, vr1, vr3;
1106 }
1107 (if (INTEGRAL_TYPE_P (type)
1108 && get_range_query (cfun)->range_of_expr (vr0, @0))
1109 (if (exact_mod (c)
1110 && get_range_query (cfun)->range_of_expr (vr1, @1)
1111 /* "X+C" doesn't overflow. */
1112 && range_op_handler (PLUS_EXPR).overflow_free_p (vr0, vr1)
1113 && get_range_query (cfun)->range_of_expr (vr3, @3)
1114 && !vr3.undefined_p ()
1115 /* "X+C" and "X" are not of opposite sign. */
1116 && (TYPE_UNSIGNED (type)
1117 || (vr0.nonnegative_p () && vr3.nonnegative_p ())
1118 || (vr0.nonpositive_p () && vr3.nonpositive_p ())))
1119 (plus (op @0 @2) { wide_int_to_tree (type, plus_op1 (c)); })
1120 (if (!vr0.undefined_p () && TYPE_UNSIGNED (type) && c.sign_mask () < 0
1121 && exact_mod (-c)
1122 /* unsigned "X-(-C)" doesn't underflow. */
1123 && wi::geu_p (vr0.lower_bound (), -c))
1124 (plus (op @0 @2) { wide_int_to_tree (type, -plus_op1 (-c)); })))))))
1125 #undef plus_op1
1126 #undef exact_mod
1127 #endif
1128
1129 /* (nop_outer_cast)-(inner_cast)var -> -(outer_cast)(var)
1130 if var is smaller in precision.
1131 This is always safe for both doing the negative in signed or unsigned
1132 as the value for undefined will not show up.
1133 Note the outer cast cannot be a boolean type as the only valid values
1134 are 0,-1/1 (depending on the signedness of the boolean) and the negative
1135 is there to get the correct value. */
1136 (simplify
1137 (convert (negate:s@1 (convert:s @0)))
1138 (if (INTEGRAL_TYPE_P (type)
1139 && tree_nop_conversion_p (type, TREE_TYPE (@1))
1140 && TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (@0))
1141 && TREE_CODE (type) != BOOLEAN_TYPE)
1142 (negate (convert @0))))
1143
1144 (for op (negate abs)
1145 /* Simplify cos(-x) and cos(|x|) -> cos(x). Similarly for cosh. */
1146 (for coss (COS COSH)
1147 (simplify
1148 (coss (op @0))
1149 (coss @0)))
1150 /* Simplify pow(-x, y) and pow(|x|,y) -> pow(x,y) if y is an even integer. */
1151 (for pows (POW)
1152 (simplify
1153 (pows (op @0) REAL_CST@1)
1154 (with { HOST_WIDE_INT n; }
1155 (if (real_isinteger (&TREE_REAL_CST (@1), &n) && (n & 1) == 0)
1156 (pows @0 @1)))))
1157 /* Likewise for powi. */
1158 (for pows (POWI)
1159 (simplify
1160 (pows (op @0) INTEGER_CST@1)
1161 (if ((wi::to_wide (@1) & 1) == 0)
1162 (pows @0 @1))))
1163 /* Strip negate and abs from both operands of hypot. */
1164 (for hypots (HYPOT)
1165 (simplify
1166 (hypots (op @0) @1)
1167 (hypots @0 @1))
1168 (simplify
1169 (hypots @0 (op @1))
1170 (hypots @0 @1)))
1171 /* copysign(-x, y) and copysign(abs(x), y) -> copysign(x, y). */
1172 (for copysigns (COPYSIGN_ALL)
1173 (simplify
1174 (copysigns (op @0) @1)
1175 (copysigns @0 @1))))
1176
1177 /* abs(x)*abs(x) -> x*x. Should be valid for all types. */
1178 (simplify
1179 (mult (abs@1 @0) @1)
1180 (mult @0 @0))
1181
1182 /* Convert absu(x)*absu(x) -> x*x. */
1183 (simplify
1184 (mult (absu@1 @0) @1)
1185 (mult (convert@2 @0) @2))
1186
1187 /* cos(copysign(x, y)) -> cos(x). Similarly for cosh. */
1188 (for coss (COS COSH)
1189 (for copysigns (COPYSIGN)
1190 (simplify
1191 (coss (copysigns @0 @1))
1192 (coss @0))))
1193
1194 /* pow(copysign(x, y), z) -> pow(x, z) if z is an even integer. */
1195 (for pows (POW)
1196 (for copysigns (COPYSIGN)
1197 (simplify
1198 (pows (copysigns @0 @2) REAL_CST@1)
1199 (with { HOST_WIDE_INT n; }
1200 (if (real_isinteger (&TREE_REAL_CST (@1), &n) && (n & 1) == 0)
1201 (pows @0 @1))))))
1202 /* Likewise for powi. */
1203 (for pows (POWI)
1204 (for copysigns (COPYSIGN)
1205 (simplify
1206 (pows (copysigns @0 @2) INTEGER_CST@1)
1207 (if ((wi::to_wide (@1) & 1) == 0)
1208 (pows @0 @1)))))
1209
1210 (for hypots (HYPOT)
1211 (for copysigns (COPYSIGN)
1212 /* hypot(copysign(x, y), z) -> hypot(x, z). */
1213 (simplify
1214 (hypots (copysigns @0 @1) @2)
1215 (hypots @0 @2))
1216 /* hypot(x, copysign(y, z)) -> hypot(x, y). */
1217 (simplify
1218 (hypots @0 (copysigns @1 @2))
1219 (hypots @0 @1))))
1220
1221 /* copysign(x, CST) -> abs (x). If the target does not
1222 support the copysign optab then canonicalize
1223 copysign(x, -CST) -> fneg (abs (x)). */
1224 (for copysigns (COPYSIGN_ALL)
1225 (simplify
1226 (copysigns @0 REAL_CST@1)
1227 (if (!REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
1228 (abs @0)
1229 #if GIMPLE
1230 (if (!direct_internal_fn_supported_p (IFN_COPYSIGN, type,
1231 OPTIMIZE_FOR_BOTH))
1232 (negate (abs @0)))
1233 #endif
1234 )))
1235
1236 #if GIMPLE
1237 /* Transform fneg (fabs (X)) -> copysign (X, -1) as the canonical
1238 representation if the target supports the copysign optab. */
1239 (simplify
1240 (negate (abs @0))
1241 (if (direct_internal_fn_supported_p (IFN_COPYSIGN, type,
1242 OPTIMIZE_FOR_BOTH))
1243 (IFN_COPYSIGN @0 { build_minus_one_cst (type); })))
1244 #endif
1245 /* copysign(copysign(x, y), z) -> copysign(x, z). */
1246 (for copysigns (COPYSIGN_ALL)
1247 (simplify
1248 (copysigns (copysigns @0 @1) @2)
1249 (copysigns @0 @2)))
1250
1251 /* copysign(x,y)*copysign(x,y) -> x*x. */
1252 (for copysigns (COPYSIGN_ALL)
1253 (simplify
1254 (mult (copysigns@2 @0 @1) @2)
1255 (mult @0 @0)))
1256
1257 /* ccos(-x) -> ccos(x). Similarly for ccosh. */
1258 (for ccoss (CCOS CCOSH)
1259 (simplify
1260 (ccoss (negate @0))
1261 (ccoss @0)))
1262
1263 /* cabs(-x) and cos(conj(x)) -> cabs(x). */
1264 (for ops (conj negate)
1265 (for cabss (CABS)
1266 (simplify
1267 (cabss (ops @0))
1268 (cabss @0))))
1269
1270 /* Fold (a * (1 << b)) into (a << b) */
1271 (simplify
1272 (mult:c @0 (convert? (lshift integer_onep@1 @2)))
1273 (if (! FLOAT_TYPE_P (type)
1274 && tree_nop_conversion_p (type, TREE_TYPE (@1)))
1275 (lshift @0 @2)))
1276
1277 /* Fold a * !a into 0. */
1278 (simplify
1279 (mult:c @0 (convert? (eq @0 integer_zerop)))
1280 { build_zero_cst (type); })
1281 (simplify
1282 (mult:c @0 (vec_cond (eq @0 integer_zerop) @1 integer_zerop))
1283 { build_zero_cst (type); })
1284 (simplify
1285 (mult:c @0 (vec_cond (ne @0 integer_zerop) integer_zerop @1))
1286 { build_zero_cst (type); })
1287
1288 /* Shifts by precision or greater result in zero. */
1289 (for shift (lshift rshift)
1290 (simplify
1291 (shift @0 uniform_integer_cst_p@1)
1292 (if ((GIMPLE || !sanitize_flags_p (SANITIZE_SHIFT_EXPONENT))
1293 /* Leave arithmetic right shifts of possibly negative values alone. */
1294 && (TYPE_UNSIGNED (type)
1295 || shift == LSHIFT_EXPR
1296 || tree_expr_nonnegative_p (@0))
1297 /* Use a signed compare to leave negative shift counts alone. */
1298 && wi::ges_p (wi::to_wide (uniform_integer_cst_p (@1)),
1299 element_precision (type)))
1300 { build_zero_cst (type); })))
1301
1302 /* Shifts by constants distribute over several binary operations,
1303 hence (X << C) + (Y << C) can be simplified to (X + Y) << C. */
1304 (for op (plus minus)
1305 (simplify
1306 (op (lshift:s @0 @1) (lshift:s @2 @1))
1307 (if (INTEGRAL_TYPE_P (type)
1308 && TYPE_OVERFLOW_WRAPS (type)
1309 && !TYPE_SATURATING (type))
1310 (lshift (op @0 @2) @1))))
1311
1312 (for op (bit_and bit_ior bit_xor)
1313 (simplify
1314 (op (lshift:s @0 @1) (lshift:s @2 @1))
1315 (if (INTEGRAL_TYPE_P (type))
1316 (lshift (op @0 @2) @1)))
1317 (simplify
1318 (op (rshift:s @0 @1) (rshift:s @2 @1))
1319 (if (INTEGRAL_TYPE_P (type))
1320 (rshift (op @0 @2) @1))))
1321
1322 /* Fold (1 << (C - x)) where C = precision(type) - 1
1323 into ((1 << C) >> x). */
1324 (simplify
1325 (lshift integer_onep@0 (minus@1 INTEGER_CST@2 @3))
1326 (if (INTEGRAL_TYPE_P (type)
1327 && wi::eq_p (wi::to_wide (@2), TYPE_PRECISION (type) - 1)
1328 && single_use (@1))
1329 (if (TYPE_UNSIGNED (type))
1330 (rshift (lshift @0 @2) @3)
1331 (with
1332 { tree utype = unsigned_type_for (type); }
1333 (convert (rshift (lshift (convert:utype @0) @2) @3))))))
1334
1335 /* Fold ((type)(a<0)) << SIGNBITOFA into ((type)a) & signbit. */
1336 (simplify
1337 (lshift (convert (lt @0 integer_zerop@1)) INTEGER_CST@2)
1338 (if (TYPE_SIGN (TREE_TYPE (@0)) == SIGNED
1339 && wi::eq_p (wi::to_wide (@2), TYPE_PRECISION (TREE_TYPE (@0)) - 1))
1340 (with { wide_int wone = wi::one (TYPE_PRECISION (type)); }
1341 (bit_and (convert @0)
1342 { wide_int_to_tree (type,
1343 wi::lshift (wone, wi::to_wide (@2))); }))))
1344
1345 /* Fold (-x >> C) into -(x > 0) where C = precision(type) - 1. */
1346 (for cst (INTEGER_CST VECTOR_CST)
1347 (simplify
1348 (rshift (negate:s @0) cst@1)
1349 (if (!TYPE_UNSIGNED (type)
1350 && TYPE_OVERFLOW_UNDEFINED (type))
1351 (with { tree stype = TREE_TYPE (@1);
1352 tree bt = truth_type_for (type);
1353 tree zeros = build_zero_cst (type);
1354 tree cst = NULL_TREE; }
1355 (switch
1356 /* Handle scalar case. */
1357 (if (INTEGRAL_TYPE_P (type)
1358 /* If we apply the rule to the scalar type before vectorization
1359 we will enforce the result of the comparison being a bool
1360 which will require an extra AND on the result that will be
1361 indistinguishable from when the user did actually want 0
1362 or 1 as the result so it can't be removed. */
1363 && canonicalize_math_after_vectorization_p ()
1364 && wi::eq_p (wi::to_wide (@1), TYPE_PRECISION (type) - 1))
1365 (negate (convert (gt @0 { zeros; }))))
1366 /* Handle vector case. */
1367 (if (VECTOR_INTEGER_TYPE_P (type)
1368 /* First check whether the target has the same mode for vector
1369 comparison results as it's operands do. */
1370 && TYPE_MODE (bt) == TYPE_MODE (type)
1371 /* Then check to see if the target is able to expand the comparison
1372 with the given type later on, otherwise we may ICE. */
1373 && expand_vec_cmp_expr_p (type, bt, GT_EXPR)
1374 && (cst = uniform_integer_cst_p (@1)) != NULL
1375 && wi::eq_p (wi::to_wide (cst), element_precision (type) - 1))
1376 (view_convert (gt:bt @0 { zeros; }))))))))
1377
1378 /* Fold (C1/X)*C2 into (C1*C2)/X. */
1379 (simplify
1380 (mult (rdiv@3 REAL_CST@0 @1) REAL_CST@2)
1381 (if (flag_associative_math
1382 && single_use (@3))
1383 (with
1384 { tree tem = const_binop (MULT_EXPR, type, @0, @2); }
1385 (if (tem)
1386 (rdiv { tem; } @1)))))
1387
1388 /* Simplify ~X & X as zero. */
1389 (simplify
1390 (bit_and:c (convert? @0) (convert? (maybe_bit_not @1)))
1391 (with { bool wascmp; }
1392 (if (types_match (TREE_TYPE (@0), TREE_TYPE (@1))
1393 && bitwise_inverted_equal_p (@0, @1, wascmp))
1394 { wascmp ? constant_boolean_node (false, type) : build_zero_cst (type); })))
1395
1396 /* PR71636: Transform x & ((1U << b) - 1) -> x & ~(~0U << b); */
1397 (simplify
1398 (bit_and:c @0 (plus:s (lshift:s integer_onep @1) integer_minus_onep))
1399 (if (TYPE_UNSIGNED (type))
1400 (bit_and @0 (bit_not (lshift { build_all_ones_cst (type); } @1)))))
1401
1402 (for bitop (bit_and bit_ior)
1403 cmp (eq ne)
1404 /* PR35691: Transform
1405 (x == 0 & y == 0) -> (x | typeof(x)(y)) == 0.
1406 (x != 0 | y != 0) -> (x | typeof(x)(y)) != 0. */
1407 (simplify
1408 (bitop (cmp @0 integer_zerop@2) (cmp @1 integer_zerop))
1409 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1410 && INTEGRAL_TYPE_P (TREE_TYPE (@1))
1411 && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
1412 (cmp (bit_ior @0 (convert @1)) @2)))
1413 /* Transform:
1414 (x == -1 & y == -1) -> (x & typeof(x)(y)) == -1.
1415 (x != -1 | y != -1) -> (x & typeof(x)(y)) != -1. */
1416 (simplify
1417 (bitop (cmp @0 integer_all_onesp@2) (cmp @1 integer_all_onesp))
1418 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1419 && INTEGRAL_TYPE_P (TREE_TYPE (@1))
1420 && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
1421 (cmp (bit_and @0 (convert @1)) @2))))
1422
1423 /* Fold (A & ~B) - (A & B) into (A ^ B) - B. */
1424 (simplify
1425 (minus (bit_and:cs @0 (bit_not @1)) (bit_and:cs @0 @1))
1426 (minus (bit_xor @0 @1) @1))
1427 (simplify
1428 (minus (bit_and:s @0 INTEGER_CST@2) (bit_and:s @0 INTEGER_CST@1))
1429 (if (~wi::to_wide (@2) == wi::to_wide (@1))
1430 (minus (bit_xor @0 @1) @1)))
1431
1432 /* Fold (A & B) - (A & ~B) into B - (A ^ B). */
1433 (simplify
1434 (minus (bit_and:cs @0 @1) (bit_and:cs @0 (bit_not @1)))
1435 (minus @1 (bit_xor @0 @1)))
1436
1437 /* Simplify (X & ~Y) |^+ (~X & Y) -> X ^ Y. */
1438 (for op (bit_ior bit_xor plus)
1439 (simplify
1440 (op (bit_and:c @0 @2) (bit_and:c @3 @1))
1441 (with { bool wascmp0, wascmp1; }
1442 (if (bitwise_inverted_equal_p (@2, @1, wascmp0)
1443 && bitwise_inverted_equal_p (@0, @3, wascmp1)
1444 && ((!wascmp0 && !wascmp1)
1445 || element_precision (type) == 1))
1446 (bit_xor @0 @1)))))
1447
1448 /* PR53979: Transform ((a ^ b) | a) -> (a | b) */
1449 (simplify
1450 (bit_ior:c (bit_xor:c @0 @1) @0)
1451 (bit_ior @0 @1))
1452
1453 /* (a & ~b) | (a ^ b) --> a ^ b */
1454 (simplify
1455 (bit_ior:c (bit_and:c @0 (bit_not @1)) (bit_xor:c@2 @0 @1))
1456 @2)
1457
1458 /* (a & ~b) ^ ~a --> ~(a & b) */
1459 (simplify
1460 (bit_xor:c (bit_and:cs @0 (bit_not @1)) (bit_not @0))
1461 (bit_not (bit_and @0 @1)))
1462
1463 /* (~a & b) ^ a --> (a | b) */
1464 (simplify
1465 (bit_xor:c (bit_and:cs (bit_not @0) @1) @0)
1466 (bit_ior @0 @1))
1467
1468 /* (a | b) & ~(a ^ b) --> a & b */
1469 (simplify
1470 (bit_and:c (bit_ior @0 @1) (bit_not (bit_xor:c @0 @1)))
1471 (bit_and @0 @1))
1472
1473 /* (a | b) & (a == b) --> a & b (boolean version of the above). */
1474 (simplify
1475 (bit_and:c (bit_ior @0 @1) (nop_convert? (eq:c @0 @1)))
1476 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1477 && TYPE_PRECISION (TREE_TYPE (@0)) == 1)
1478 (bit_and @0 @1)))
1479
1480 /* a | ~(a ^ b) --> a | ~b */
1481 (simplify
1482 (bit_ior:c @0 (bit_not:s (bit_xor:c @0 @1)))
1483 (bit_ior @0 (bit_not @1)))
1484
1485 /* a | (a == b) --> a | (b^1) (boolean version of the above). */
1486 (simplify
1487 (bit_ior:c @0 (nop_convert? (eq:c @0 @1)))
1488 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1489 && TYPE_PRECISION (TREE_TYPE (@0)) == 1)
1490 (bit_ior @0 (bit_xor @1 { build_one_cst (type); }))))
1491
1492 /* a | ((~a) ^ b) --> a | (~b) (alt version of the above 2) */
1493 (simplify
1494 (bit_ior:c @0 (bit_xor:cs @1 @2))
1495 (with { bool wascmp; }
1496 (if (bitwise_inverted_equal_p (@0, @1, wascmp)
1497 && (!wascmp || element_precision (type) == 1))
1498 (bit_ior @0 (bit_not @2)))))
1499
1500 /* a & ~(a ^ b) --> a & b */
1501 (simplify
1502 (bit_and:c @0 (bit_not (bit_xor:c @0 @1)))
1503 (bit_and @0 @1))
1504
1505 /* Transform:
1506 (a - 1) & -a -> 0.
1507 (a - 1) | -a -> -1.
1508 (a - 1) ^ -a -> -1. */
1509 (for bit_op (bit_ior bit_xor bit_and)
1510 (simplify
1511 (bit_op:c (plus @0 integer_minus_onep) (negate @0))
1512 (if (bit_op == BIT_AND_EXPR)
1513 { build_zero_cst (type); }
1514 { build_minus_one_cst (type); }))
1515 (simplify
1516 (bit_op:c (minus @0 integer_onep) (negate @0))
1517 (if (bit_op == BIT_AND_EXPR)
1518 { build_zero_cst (type); }
1519 { build_minus_one_cst (type); })))
1520
1521 /* a & (a == b) --> a & b (boolean version of the above). */
1522 (simplify
1523 (bit_and:c @0 (nop_convert? (eq:c @0 @1)))
1524 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1525 && TYPE_PRECISION (TREE_TYPE (@0)) == 1)
1526 (bit_and @0 @1)))
1527
1528 /* a & ((~a) ^ b) --> a & b (alt version of the above 2) */
1529 (simplify
1530 (bit_and:c @0 (bit_xor:c @1 @2))
1531 (with { bool wascmp; }
1532 (if (bitwise_inverted_equal_p (@0, @1, wascmp)
1533 && (!wascmp || element_precision (type) == 1))
1534 (bit_and @0 @2))))
1535
1536 /* (a | b) | (a &^ b) --> a | b */
1537 (for op (bit_and bit_xor)
1538 (simplify
1539 (bit_ior:c (bit_ior@2 @0 @1) (op:c @0 @1))
1540 @2))
1541
1542 /* (a & b) | ~(a ^ b) --> ~(a ^ b) */
1543 (simplify
1544 (bit_ior:c (bit_and:c @0 @1) (bit_not@2 (bit_xor @0 @1)))
1545 @2)
1546
1547 /* (a & b) | (a == b) --> a == b */
1548 (simplify
1549 (bit_ior:c (bit_and:c @0 @1) (nop_convert?@2 (eq @0 @1)))
1550 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1551 && TYPE_PRECISION (TREE_TYPE (@0)) == 1)
1552 @2))
1553
1554 /* ~(~a & b) --> a | ~b */
1555 (simplify
1556 (bit_not (bit_and:cs (bit_not @0) @1))
1557 (bit_ior @0 (bit_not @1)))
1558
1559 /* ~(~a | b) --> a & ~b */
1560 (simplify
1561 (bit_not (bit_ior:cs (bit_not @0) @1))
1562 (bit_and @0 (bit_not @1)))
1563
1564 /* (a ^ b) & ((b ^ c) ^ a) --> (a ^ b) & ~c */
1565 (simplify
1566 (bit_and:c (bit_xor:c@3 @0 @1) (bit_xor:cs (bit_xor:cs @1 @2) @0))
1567 (bit_and @3 (bit_not @2)))
1568
1569 /* (a ^ b) | ((b ^ c) ^ a) --> (a ^ b) | c */
1570 (simplify
1571 (bit_ior:c (bit_xor:c@3 @0 @1) (bit_xor:c (bit_xor:c @1 @2) @0))
1572 (bit_ior @3 @2))
1573
1574 /* (~X | C) ^ D -> (X | C) ^ (~D ^ C) if (~D ^ C) can be simplified. */
1575 (simplify
1576 (bit_xor:c (bit_ior:cs (bit_not:s @0) @1) @2)
1577 (bit_xor (bit_ior @0 @1) (bit_xor! (bit_not! @2) @1)))
1578
1579 /* (~X & C) ^ D -> (X & C) ^ (D ^ C) if (D ^ C) can be simplified. */
1580 (simplify
1581 (bit_xor:c (bit_and:cs (bit_not:s @0) @1) @2)
1582 (bit_xor (bit_and @0 @1) (bit_xor! @2 @1)))
1583
1584 /* Simplify (~X & Y) to X ^ Y if we know that (X & ~Y) is 0. */
1585 (simplify
1586 (bit_and (bit_not SSA_NAME@0) INTEGER_CST@1)
1587 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1588 && wi::bit_and_not (get_nonzero_bits (@0), wi::to_wide (@1)) == 0)
1589 (bit_xor @0 @1)))
1590
1591 /* For constants M and N, if M == (1LL << cst) - 1 && (N & M) == M,
1592 ((A & N) + B) & M -> (A + B) & M
1593 Similarly if (N & M) == 0,
1594 ((A | N) + B) & M -> (A + B) & M
1595 and for - instead of + (or unary - instead of +)
1596 and/or ^ instead of |.
1597 If B is constant and (B & M) == 0, fold into A & M. */
1598 (for op (plus minus)
1599 (for bitop (bit_and bit_ior bit_xor)
1600 (simplify
1601 (bit_and (op:s (bitop:s@0 @3 INTEGER_CST@4) @1) INTEGER_CST@2)
1602 (with
1603 { tree pmop[2];
1604 tree utype = fold_bit_and_mask (TREE_TYPE (@0), @2, op, @0, bitop,
1605 @3, @4, @1, ERROR_MARK, NULL_TREE,
1606 NULL_TREE, pmop); }
1607 (if (utype)
1608 (convert (bit_and (op (convert:utype { pmop[0]; })
1609 (convert:utype { pmop[1]; }))
1610 (convert:utype @2))))))
1611 (simplify
1612 (bit_and (op:s @0 (bitop:s@1 @3 INTEGER_CST@4)) INTEGER_CST@2)
1613 (with
1614 { tree pmop[2];
1615 tree utype = fold_bit_and_mask (TREE_TYPE (@0), @2, op, @0, ERROR_MARK,
1616 NULL_TREE, NULL_TREE, @1, bitop, @3,
1617 @4, pmop); }
1618 (if (utype)
1619 (convert (bit_and (op (convert:utype { pmop[0]; })
1620 (convert:utype { pmop[1]; }))
1621 (convert:utype @2)))))))
1622 (simplify
1623 (bit_and (op:s @0 @1) INTEGER_CST@2)
1624 (with
1625 { tree pmop[2];
1626 tree utype = fold_bit_and_mask (TREE_TYPE (@0), @2, op, @0, ERROR_MARK,
1627 NULL_TREE, NULL_TREE, @1, ERROR_MARK,
1628 NULL_TREE, NULL_TREE, pmop); }
1629 (if (utype)
1630 (convert (bit_and (op (convert:utype { pmop[0]; })
1631 (convert:utype { pmop[1]; }))
1632 (convert:utype @2)))))))
1633 (for bitop (bit_and bit_ior bit_xor)
1634 (simplify
1635 (bit_and (negate:s (bitop:s@0 @2 INTEGER_CST@3)) INTEGER_CST@1)
1636 (with
1637 { tree pmop[2];
1638 tree utype = fold_bit_and_mask (TREE_TYPE (@0), @1, NEGATE_EXPR, @0,
1639 bitop, @2, @3, NULL_TREE, ERROR_MARK,
1640 NULL_TREE, NULL_TREE, pmop); }
1641 (if (utype)
1642 (convert (bit_and (negate (convert:utype { pmop[0]; }))
1643 (convert:utype @1)))))))
1644
1645 /* X % Y is smaller than Y. */
1646 (for cmp (lt ge)
1647 (simplify
1648 (cmp:c (trunc_mod @0 @1) @1)
1649 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
1650 { constant_boolean_node (cmp == LT_EXPR, type); })))
1651
1652 /* x | ~0 -> ~0 */
1653 (simplify
1654 (bit_ior @0 integer_all_onesp@1)
1655 @1)
1656
1657 /* x | 0 -> x */
1658 (simplify
1659 (bit_ior @0 integer_zerop)
1660 @0)
1661
1662 /* x & 0 -> 0 */
1663 (simplify
1664 (bit_and @0 integer_zerop@1)
1665 @1)
1666
1667 /* ~x | x -> -1 */
1668 /* ~x ^ x -> -1 */
1669 (for op (bit_ior bit_xor)
1670 (simplify
1671 (op:c (convert? @0) (convert? (maybe_bit_not @1)))
1672 (with { bool wascmp; }
1673 (if (types_match (TREE_TYPE (@0), TREE_TYPE (@1))
1674 && bitwise_inverted_equal_p (@0, @1, wascmp))
1675 (convert
1676 { wascmp
1677 ? constant_boolean_node (true, type)
1678 : build_all_ones_cst (TREE_TYPE (@0)); })))))
1679
1680 /* x ^ x -> 0 */
1681 (simplify
1682 (bit_xor @0 @0)
1683 { build_zero_cst (type); })
1684
1685 /* Canonicalize X ^ ~0 to ~X. */
1686 (simplify
1687 (bit_xor @0 integer_all_onesp@1)
1688 (bit_not @0))
1689
1690 /* x & ~0 -> x */
1691 (simplify
1692 (bit_and @0 integer_all_onesp)
1693 (non_lvalue @0))
1694
1695 /* x & x -> x, x | x -> x */
1696 (for bitop (bit_and bit_ior)
1697 (simplify
1698 (bitop @0 @0)
1699 (non_lvalue @0)))
1700
1701 /* x & C -> x if we know that x & ~C == 0. */
1702 #if GIMPLE
1703 (simplify
1704 (bit_and SSA_NAME@0 INTEGER_CST@1)
1705 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1706 && wi::bit_and_not (get_nonzero_bits (@0), wi::to_wide (@1)) == 0)
1707 @0))
1708
1709 /* `a & (x | CST)` -> a if we know that (a & ~CST) == 0 */
1710 (simplify
1711 (bit_and:c SSA_NAME@0 (bit_ior @1 INTEGER_CST@2))
1712 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1713 && wi::bit_and_not (get_nonzero_bits (@0), wi::to_wide (@2)) == 0)
1714 @0))
1715
1716 /* x | C -> C if we know that x & ~C == 0. */
1717 (simplify
1718 (bit_ior SSA_NAME@0 INTEGER_CST@1)
1719 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1720 && wi::bit_and_not (get_nonzero_bits (@0), wi::to_wide (@1)) == 0)
1721 @1))
1722 #endif
1723
1724 /* ~(~X - Y) -> X + Y and ~(~X + Y) -> X - Y. */
1725 (simplify
1726 (bit_not (minus (bit_not @0) @1))
1727 (plus @0 @1))
1728 (simplify
1729 (bit_not (plus:c (bit_not @0) @1))
1730 (minus @0 @1))
1731 /* (~X - ~Y) -> Y - X. */
1732 (simplify
1733 (minus (bit_not @0) (bit_not @1))
1734 (if (!TYPE_OVERFLOW_SANITIZED (type))
1735 (with { tree utype = unsigned_type_for (type); }
1736 (convert (minus (convert:utype @1) (convert:utype @0))))))
1737
1738 /* ~(X - Y) -> ~X + Y. */
1739 (simplify
1740 (bit_not (minus:s @0 @1))
1741 (plus (bit_not @0) @1))
1742 (simplify
1743 (bit_not (plus:s @0 INTEGER_CST@1))
1744 (if ((INTEGRAL_TYPE_P (type)
1745 && TYPE_UNSIGNED (type))
1746 || (!TYPE_OVERFLOW_SANITIZED (type)
1747 && may_negate_without_overflow_p (@1)))
1748 (plus (bit_not @0) { const_unop (NEGATE_EXPR, type, @1); })))
1749
1750 #if GIMPLE
1751 /* ~X + Y -> (Y - X) - 1. */
1752 (simplify
1753 (plus:c (bit_not @0) @1)
1754 (if (ANY_INTEGRAL_TYPE_P (type)
1755 && TYPE_OVERFLOW_WRAPS (type)
1756 /* -1 - X is folded to ~X, so we'd recurse endlessly. */
1757 && !integer_all_onesp (@1))
1758 (plus (minus @1 @0) { build_minus_one_cst (type); })
1759 (if (INTEGRAL_TYPE_P (type)
1760 && TREE_CODE (@1) == INTEGER_CST
1761 && wi::to_wide (@1) != wi::min_value (TYPE_PRECISION (type),
1762 SIGNED))
1763 (minus (plus @1 { build_minus_one_cst (type); }) @0))))
1764 #endif
1765
1766 /* ~(X >> Y) -> ~X >> Y if ~X can be simplified. */
1767 (simplify
1768 (bit_not (rshift:s @0 @1))
1769 (if (!TYPE_UNSIGNED (TREE_TYPE (@0)))
1770 (rshift (bit_not! @0) @1)
1771 /* For logical right shifts, this is possible only if @0 doesn't
1772 have MSB set and the logical right shift is changed into
1773 arithmetic shift. */
1774 (if (INTEGRAL_TYPE_P (type)
1775 && !wi::neg_p (tree_nonzero_bits (@0)))
1776 (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
1777 (convert (rshift (bit_not! (convert:stype @0)) @1))))))
1778
1779 /* x + (x & 1) -> (x + 1) & ~1 */
1780 (simplify
1781 (plus:c @0 (bit_and:s @0 integer_onep@1))
1782 (bit_and (plus @0 @1) (bit_not @1)))
1783
1784 /* x & ~(x & y) -> x & ~y */
1785 /* x | ~(x | y) -> x | ~y */
1786 (for bitop (bit_and bit_ior)
1787 (simplify
1788 (bitop:c @0 (bit_not (bitop:cs @0 @1)))
1789 (bitop @0 (bit_not @1))))
1790
1791 /* (~x & y) | ~(x | y) -> ~x */
1792 (simplify
1793 (bit_ior:c (bit_and:c (bit_not@2 @0) @1) (bit_not (bit_ior:c @0 @1)))
1794 @2)
1795
1796 /* (x | y) ^ (x | ~y) -> ~x */
1797 (simplify
1798 (bit_xor:c (bit_ior:c @0 @1) (bit_ior:c @0 (bit_not @1)))
1799 (bit_not @0))
1800
1801 /* (x & y) | ~(x | y) -> ~(x ^ y) */
1802 (simplify
1803 (bit_ior:c (bit_and:s @0 @1) (bit_not:s (bit_ior:s @0 @1)))
1804 (bit_not (bit_xor @0 @1)))
1805
1806 /* (~x | y) ^ (x ^ y) -> x | ~y */
1807 (simplify
1808 (bit_xor:c (bit_ior:cs (bit_not @0) @1) (bit_xor:s @0 @1))
1809 (bit_ior @0 (bit_not @1)))
1810
1811 /* (x ^ y) | ~(x | y) -> ~(x & y) */
1812 (simplify
1813 (bit_ior:c (bit_xor:s @0 @1) (bit_not:s (bit_ior:s @0 @1)))
1814 (bit_not (bit_and @0 @1)))
1815
1816 /* (x & y) ^ (x | y) -> x ^ y */
1817 (simplify
1818 (bit_xor:c (bit_and @0 @1) (bit_ior @0 @1))
1819 (bit_xor @0 @1))
1820
1821 /* (x ^ y) ^ (x | y) -> x & y */
1822 (simplify
1823 (bit_xor:c (bit_xor @0 @1) (bit_ior @0 @1))
1824 (bit_and @0 @1))
1825
1826 /* (x & y) + (x ^ y) -> x | y */
1827 /* (x & y) | (x ^ y) -> x | y */
1828 /* (x & y) ^ (x ^ y) -> x | y */
1829 (for op (plus bit_ior bit_xor)
1830 (simplify
1831 (op:c (bit_and @0 @1) (bit_xor @0 @1))
1832 (bit_ior @0 @1)))
1833
1834 /* (x & y) + (x | y) -> x + y */
1835 (simplify
1836 (plus:c (bit_and @0 @1) (bit_ior @0 @1))
1837 (plus @0 @1))
1838
1839 /* (x + y) - (x | y) -> x & y */
1840 (simplify
1841 (minus (plus @0 @1) (bit_ior @0 @1))
1842 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1843 && !TYPE_SATURATING (type))
1844 (bit_and @0 @1)))
1845
1846 /* (x + y) - (x & y) -> x | y */
1847 (simplify
1848 (minus (plus @0 @1) (bit_and @0 @1))
1849 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1850 && !TYPE_SATURATING (type))
1851 (bit_ior @0 @1)))
1852
1853 /* (x | y) - y -> (x & ~y) */
1854 (simplify
1855 (minus (bit_ior:cs @0 @1) @1)
1856 (bit_and @0 (bit_not @1)))
1857
1858 /* (x | y) - (x ^ y) -> x & y */
1859 (simplify
1860 (minus (bit_ior @0 @1) (bit_xor @0 @1))
1861 (bit_and @0 @1))
1862
1863 /* (x | y) - (x & y) -> x ^ y */
1864 (simplify
1865 (minus (bit_ior @0 @1) (bit_and @0 @1))
1866 (bit_xor @0 @1))
1867
1868 /* (x | y) & ~(x & y) -> x ^ y */
1869 (simplify
1870 (bit_and:c (bit_ior @0 @1) (bit_not (bit_and @0 @1)))
1871 (bit_xor @0 @1))
1872
1873 /* (x | y) & (~x ^ y) -> x & y */
1874 (simplify
1875 (bit_and:c (bit_ior:c @0 @1) (bit_xor:c @1 @2))
1876 (with { bool wascmp; }
1877 (if (bitwise_inverted_equal_p (@0, @2, wascmp)
1878 && (!wascmp || element_precision (type) == 1))
1879 (bit_and @0 @1))))
1880
1881 /* (~x | y) & (x | ~y) -> ~(x ^ y) */
1882 (simplify
1883 (bit_and (bit_ior:cs (bit_not @0) @1) (bit_ior:cs @0 (bit_not @1)))
1884 (bit_not (bit_xor @0 @1)))
1885
1886 /* (~x | y) ^ (x | ~y) -> x ^ y */
1887 (simplify
1888 (bit_xor (bit_ior:c (bit_not @0) @1) (bit_ior:c @0 (bit_not @1)))
1889 (bit_xor @0 @1))
1890
1891 /* ((x & y) - (x | y)) - 1 -> ~(x ^ y) */
1892 (simplify
1893 (plus (nop_convert1? (minus@2 (nop_convert2? (bit_and:c @0 @1))
1894 (nop_convert2? (bit_ior @0 @1))))
1895 integer_all_onesp)
1896 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1897 && !TYPE_SATURATING (type) && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2))
1898 && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@2))
1899 && !TYPE_SATURATING (TREE_TYPE (@2)))
1900 (bit_not (convert (bit_xor @0 @1)))))
1901 (simplify
1902 (minus (nop_convert1? (plus@2 (nop_convert2? (bit_and:c @0 @1))
1903 integer_all_onesp))
1904 (nop_convert3? (bit_ior @0 @1)))
1905 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1906 && !TYPE_SATURATING (type) && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2))
1907 && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@2))
1908 && !TYPE_SATURATING (TREE_TYPE (@2)))
1909 (bit_not (convert (bit_xor @0 @1)))))
1910 (simplify
1911 (minus (nop_convert1? (bit_and @0 @1))
1912 (nop_convert2? (plus@2 (nop_convert3? (bit_ior:c @0 @1))
1913 integer_onep)))
1914 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1915 && !TYPE_SATURATING (type) && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2))
1916 && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@2))
1917 && !TYPE_SATURATING (TREE_TYPE (@2)))
1918 (bit_not (convert (bit_xor @0 @1)))))
1919
1920 /* ~x & ~y -> ~(x | y)
1921 ~x | ~y -> ~(x & y) */
1922 (for op (bit_and bit_ior)
1923 rop (bit_ior bit_and)
1924 (simplify
1925 (op (convert1? (bit_not @0)) (convert2? (bit_not @1)))
1926 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1927 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
1928 (bit_not (rop (convert @0) (convert @1))))))
1929
1930 /* If we are XORing or adding two BIT_AND_EXPR's, both of which are and'ing
1931 with a constant, and the two constants have no bits in common,
1932 we should treat this as a BIT_IOR_EXPR since this may produce more
1933 simplifications. */
1934 (for op (bit_xor plus)
1935 (simplify
1936 (op (convert1? (bit_and@4 @0 INTEGER_CST@1))
1937 (convert2? (bit_and@5 @2 INTEGER_CST@3)))
1938 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
1939 && tree_nop_conversion_p (type, TREE_TYPE (@2))
1940 && (wi::to_wide (@1) & wi::to_wide (@3)) == 0)
1941 (bit_ior (convert @4) (convert @5)))))
1942
1943 /* (X | Y) ^ X -> Y & ~ X*/
1944 (simplify
1945 (bit_xor:c (convert1? (bit_ior:c @@0 @1)) (convert2? @0))
1946 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1947 (convert (bit_and @1 (bit_not @0)))))
1948
1949 /* (~X | Y) ^ X -> ~(X & Y). */
1950 (simplify
1951 (bit_xor:c (nop_convert1? (bit_ior:c (nop_convert2? (bit_not @0)) @1)) @2)
1952 (if (bitwise_equal_p (@0, @2))
1953 (convert (bit_not (bit_and @0 (convert @1))))))
1954
1955 /* Convert ~X ^ ~Y to X ^ Y. */
1956 (simplify
1957 (bit_xor (convert1? (bit_not @0)) (convert2? (bit_not @1)))
1958 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1959 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
1960 (bit_xor (convert @0) (convert @1))))
1961
1962 /* Convert ~X ^ C to X ^ ~C. */
1963 (simplify
1964 (bit_xor (convert? (bit_not @0)) INTEGER_CST@1)
1965 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1966 (bit_xor (convert @0) (bit_not @1))))
1967
1968 /* Fold (X & Y) ^ Y and (X ^ Y) & Y as ~X & Y. */
1969 (for opo (bit_and bit_xor)
1970 opi (bit_xor bit_and)
1971 (simplify
1972 (opo:c (opi:cs @0 @1) @1)
1973 (bit_and (bit_not @0) @1)))
1974
1975 /* Given a bit-wise operation CODE applied to ARG0 and ARG1, see if both
1976 operands are another bit-wise operation with a common input. If so,
1977 distribute the bit operations to save an operation and possibly two if
1978 constants are involved. For example, convert
1979 (A | B) & (A | C) into A | (B & C)
1980 Further simplification will occur if B and C are constants. */
1981 (for op (bit_and bit_ior bit_xor)
1982 rop (bit_ior bit_and bit_and)
1983 (simplify
1984 (op (convert? (rop:c @@0 @1)) (convert? (rop:c @0 @2)))
1985 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1986 && tree_nop_conversion_p (type, TREE_TYPE (@2)))
1987 (rop (convert @0) (op (convert @1) (convert @2))))))
1988
1989 /* Some simple reassociation for bit operations, also handled in reassoc. */
1990 /* (X & Y) & Y -> X & Y
1991 (X | Y) | Y -> X | Y */
1992 (for op (bit_and bit_ior)
1993 (simplify
1994 (op:c (convert1?@2 (op:c @0 @@1)) (convert2? @1))
1995 @2))
1996 /* (X ^ Y) ^ Y -> X */
1997 (simplify
1998 (bit_xor:c (convert1? (bit_xor:c @0 @@1)) (convert2? @1))
1999 (convert @0))
2000
2001 /* (X & ~Y) & Y -> 0 */
2002 (simplify
2003 (bit_and:c (bit_and @0 @1) @2)
2004 (with { bool wascmp; }
2005 (if (bitwise_inverted_equal_p (@0, @2, wascmp)
2006 || bitwise_inverted_equal_p (@1, @2, wascmp))
2007 { wascmp ? constant_boolean_node (false, type) : build_zero_cst (type); })))
2008 /* (X | ~Y) | Y -> -1 */
2009 (simplify
2010 (bit_ior:c (bit_ior @0 @1) @2)
2011 (with { bool wascmp; }
2012 (if ((bitwise_inverted_equal_p (@0, @2, wascmp)
2013 || bitwise_inverted_equal_p (@1, @2, wascmp))
2014 && (!wascmp || element_precision (type) == 1))
2015 { build_all_ones_cst (TREE_TYPE (@0)); })))
2016
2017 /* (X & Y) & (X & Z) -> (X & Y) & Z
2018 (X | Y) | (X | Z) -> (X | Y) | Z */
2019 (for op (bit_and bit_ior)
2020 (simplify
2021 (op (convert1?@3 (op:c@4 @0 @1)) (convert2?@5 (op:c@6 @0 @2)))
2022 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
2023 && tree_nop_conversion_p (type, TREE_TYPE (@2)))
2024 (if (single_use (@5) && single_use (@6))
2025 (op @3 (convert @2))
2026 (if (single_use (@3) && single_use (@4))
2027 (op (convert @1) @5))))))
2028 /* (X ^ Y) ^ (X ^ Z) -> Y ^ Z */
2029 (simplify
2030 (bit_xor (convert1? (bit_xor:c @0 @1)) (convert2? (bit_xor:c @0 @2)))
2031 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
2032 && tree_nop_conversion_p (type, TREE_TYPE (@2)))
2033 (bit_xor (convert @1) (convert @2))))
2034
2035 /* Convert abs (abs (X)) into abs (X).
2036 also absu (absu (X)) into absu (X). */
2037 (simplify
2038 (abs (abs@1 @0))
2039 @1)
2040
2041 (simplify
2042 (absu (convert@2 (absu@1 @0)))
2043 (if (tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@1)))
2044 @1))
2045
2046 /* Convert abs[u] (-X) -> abs[u] (X). */
2047 (simplify
2048 (abs (negate @0))
2049 (abs @0))
2050
2051 (simplify
2052 (absu (negate @0))
2053 (absu @0))
2054
2055 /* Convert abs[u] (X) where X is nonnegative -> (X). */
2056 (simplify
2057 (abs tree_expr_nonnegative_p@0)
2058 @0)
2059
2060 (simplify
2061 (absu tree_expr_nonnegative_p@0)
2062 (convert @0))
2063
2064 /* Simplify (-(X < 0) | 1) * X into abs (X) or absu(X). */
2065 (simplify
2066 (mult:c (nop_convert1?
2067 (bit_ior (nop_convert2? (negate (convert? (lt @0 integer_zerop))))
2068 integer_onep))
2069 (nop_convert3? @0))
2070 (if (INTEGRAL_TYPE_P (type)
2071 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
2072 && !TYPE_UNSIGNED (TREE_TYPE (@0)))
2073 (if (TYPE_UNSIGNED (type))
2074 (absu @0)
2075 (abs @0)
2076 )
2077 )
2078 )
2079
2080 /* A few cases of fold-const.cc negate_expr_p predicate. */
2081 (match negate_expr_p
2082 INTEGER_CST
2083 (if ((INTEGRAL_TYPE_P (type)
2084 && TYPE_UNSIGNED (type))
2085 || (!TYPE_OVERFLOW_SANITIZED (type)
2086 && may_negate_without_overflow_p (t)))))
2087 (match negate_expr_p
2088 FIXED_CST)
2089 (match negate_expr_p
2090 (negate @0)
2091 (if (!TYPE_OVERFLOW_SANITIZED (type))))
2092 (match negate_expr_p
2093 REAL_CST
2094 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (t)))))
2095 /* VECTOR_CST handling of non-wrapping types would recurse in unsupported
2096 ways. */
2097 (match negate_expr_p
2098 VECTOR_CST
2099 (if (FLOAT_TYPE_P (TREE_TYPE (type)) || TYPE_OVERFLOW_WRAPS (type))))
2100 (match negate_expr_p
2101 (minus @0 @1)
2102 (if ((ANY_INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_WRAPS (type))
2103 || (FLOAT_TYPE_P (type)
2104 && !HONOR_SIGN_DEPENDENT_ROUNDING (type)
2105 && !HONOR_SIGNED_ZEROS (type)))))
2106
2107 /* (-A) * (-B) -> A * B */
2108 (simplify
2109 (mult:c (convert1? (negate @0)) (convert2? negate_expr_p@1))
2110 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
2111 && tree_nop_conversion_p (type, TREE_TYPE (@1)))
2112 (mult (convert @0) (convert (negate @1)))))
2113
2114 /* -(A + B) -> (-B) - A. */
2115 (simplify
2116 (negate (plus:c @0 negate_expr_p@1))
2117 (if (!HONOR_SIGN_DEPENDENT_ROUNDING (type)
2118 && !HONOR_SIGNED_ZEROS (type))
2119 (minus (negate @1) @0)))
2120
2121 /* -(A - B) -> B - A. */
2122 (simplify
2123 (negate (minus @0 @1))
2124 (if ((ANY_INTEGRAL_TYPE_P (type) && !TYPE_OVERFLOW_SANITIZED (type))
2125 || (FLOAT_TYPE_P (type)
2126 && !HONOR_SIGN_DEPENDENT_ROUNDING (type)
2127 && !HONOR_SIGNED_ZEROS (type)))
2128 (minus @1 @0)))
2129 (simplify
2130 (negate (pointer_diff @0 @1))
2131 (if (TYPE_OVERFLOW_UNDEFINED (type))
2132 (pointer_diff @1 @0)))
2133
2134 /* A - B -> A + (-B) if B is easily negatable. */
2135 (simplify
2136 (minus @0 negate_expr_p@1)
2137 (if (!FIXED_POINT_TYPE_P (type))
2138 (plus @0 (negate @1))))
2139
2140 /* 1 - a is a ^ 1 if a had a bool range. */
2141 /* This is only enabled for gimple as sometimes
2142 cfun is not set for the function which contains
2143 the SSA_NAME (e.g. while IPA passes are happening,
2144 fold might be called). */
2145 (simplify
2146 (minus integer_onep@0 SSA_NAME@1)
2147 (if (INTEGRAL_TYPE_P (type)
2148 && ssa_name_has_boolean_range (@1))
2149 (bit_xor @1 @0)))
2150
2151 /* Other simplifications of negation (c.f. fold_negate_expr_1). */
2152 (simplify
2153 (negate (mult:c@0 @1 negate_expr_p@2))
2154 (if (! TYPE_UNSIGNED (type)
2155 && ! HONOR_SIGN_DEPENDENT_ROUNDING (type)
2156 && single_use (@0))
2157 (mult @1 (negate @2))))
2158
2159 (simplify
2160 (negate (rdiv@0 @1 negate_expr_p@2))
2161 (if (! HONOR_SIGN_DEPENDENT_ROUNDING (type)
2162 && single_use (@0))
2163 (rdiv @1 (negate @2))))
2164
2165 (simplify
2166 (negate (rdiv@0 negate_expr_p@1 @2))
2167 (if (! HONOR_SIGN_DEPENDENT_ROUNDING (type)
2168 && single_use (@0))
2169 (rdiv (negate @1) @2)))
2170
2171 /* Fold -((int)x >> (prec - 1)) into (unsigned)x >> (prec - 1). */
2172 (simplify
2173 (negate (convert? (rshift @0 INTEGER_CST@1)))
2174 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
2175 && wi::to_wide (@1) == element_precision (type) - 1)
2176 (with { tree stype = TREE_TYPE (@0);
2177 tree ntype = TYPE_UNSIGNED (stype) ? signed_type_for (stype)
2178 : unsigned_type_for (stype); }
2179 (if (VECTOR_TYPE_P (type))
2180 (view_convert (rshift (view_convert:ntype @0) @1))
2181 (convert (rshift (convert:ntype @0) @1))))))
2182
2183 /* Try to fold (type) X op CST -> (type) (X op ((type-x) CST))
2184 when profitable.
2185 For bitwise binary operations apply operand conversions to the
2186 binary operation result instead of to the operands. This allows
2187 to combine successive conversions and bitwise binary operations.
2188 We combine the above two cases by using a conditional convert. */
2189 (for bitop (bit_and bit_ior bit_xor)
2190 (simplify
2191 (bitop (convert@2 @0) (convert?@3 @1))
2192 (if (((TREE_CODE (@1) == INTEGER_CST
2193 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
2194 && (int_fits_type_p (@1, TREE_TYPE (@0))
2195 || tree_nop_conversion_p (TREE_TYPE (@0), type)))
2196 || types_match (@0, @1))
2197 && !POINTER_TYPE_P (TREE_TYPE (@0))
2198 && !VECTOR_TYPE_P (TREE_TYPE (@0))
2199 && TREE_CODE (TREE_TYPE (@0)) != OFFSET_TYPE
2200 /* ??? This transform conflicts with fold-const.cc doing
2201 Convert (T)(x & c) into (T)x & (T)c, if c is an integer
2202 constants (if x has signed type, the sign bit cannot be set
2203 in c). This folds extension into the BIT_AND_EXPR.
2204 Restrict it to GIMPLE to avoid endless recursions. */
2205 && (bitop != BIT_AND_EXPR || GIMPLE)
2206 && (/* That's a good idea if the conversion widens the operand, thus
2207 after hoisting the conversion the operation will be narrower.
2208 It is also a good if the conversion is a nop as moves the
2209 conversion to one side; allowing for combining of the conversions. */
2210 TYPE_PRECISION (TREE_TYPE (@0)) < TYPE_PRECISION (type)
2211 /* The conversion check for being a nop can only be done at the gimple
2212 level as fold_binary has some re-association code which can conflict
2213 with this if there is a "constant" which is not a full INTEGER_CST. */
2214 || (GIMPLE && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (type))
2215 /* It's also a good idea if the conversion is to a non-integer
2216 mode. */
2217 || GET_MODE_CLASS (TYPE_MODE (type)) != MODE_INT
2218 /* Or if the precision of TO is not the same as the precision
2219 of its mode. */
2220 || !type_has_mode_precision_p (type)
2221 /* In GIMPLE, getting rid of 2 conversions for one new results
2222 in smaller IL. */
2223 || (GIMPLE
2224 && TREE_CODE (@1) != INTEGER_CST
2225 && tree_nop_conversion_p (type, TREE_TYPE (@0))
2226 && single_use (@2)
2227 && single_use (@3))))
2228 (convert (bitop @0 (convert @1)))))
2229 /* In GIMPLE, getting rid of 2 conversions for one new results
2230 in smaller IL. */
2231 (simplify
2232 (convert (bitop:cs@2 (nop_convert:s @0) @1))
2233 (if (GIMPLE
2234 && TREE_CODE (@1) != INTEGER_CST
2235 && tree_nop_conversion_p (type, TREE_TYPE (@2))
2236 && types_match (type, @0)
2237 && !POINTER_TYPE_P (TREE_TYPE (@0))
2238 && TREE_CODE (TREE_TYPE (@0)) != OFFSET_TYPE)
2239 (bitop @0 (convert @1)))))
2240
2241 (for bitop (bit_and bit_ior)
2242 rbitop (bit_ior bit_and)
2243 /* (x | y) & x -> x */
2244 /* (x & y) | x -> x */
2245 (simplify
2246 (bitop:c (rbitop:c @0 @1) @0)
2247 @0)
2248 /* (~x | y) & x -> x & y */
2249 /* (~x & y) | x -> x | y */
2250 (simplify
2251 (bitop:c (rbitop:c @2 @1) @0)
2252 (with { bool wascmp; }
2253 (if (bitwise_inverted_equal_p (@0, @2, wascmp)
2254 && (!wascmp || element_precision (type) == 1))
2255 (bitop @0 @1))))
2256 /* (x | y) & (x & z) -> (x & z) */
2257 /* (x & y) | (x | z) -> (x | z) */
2258 (simplify
2259 (bitop:c (rbitop:c @0 @1) (bitop:c@3 @0 @2))
2260 @3)
2261 /* (x | c) & ~(y | c) -> x & ~(y | c) */
2262 /* (x & c) | ~(y & c) -> x | ~(y & c) */
2263 (simplify
2264 (bitop:c (rbitop:c @0 @1) (bit_not@3 (rbitop:c @1 @2)))
2265 (bitop @0 @3))
2266 /* x & ~(y | x) -> 0 */
2267 /* x | ~(y & x) -> -1 */
2268 (simplify
2269 (bitop:c @0 (bit_not (rbitop:c @0 @1)))
2270 (if (bitop == BIT_AND_EXPR)
2271 { build_zero_cst (type); }
2272 { build_minus_one_cst (type); })))
2273
2274 /* ((x | y) & z) | x -> (z & y) | x
2275 ((x ^ y) & z) | x -> (z & y) | x */
2276 (for op (bit_ior bit_xor)
2277 (simplify
2278 (bit_ior:c (nop_convert1?:s
2279 (bit_and:cs (nop_convert2?:s (op:cs @0 @1)) @2)) @3)
2280 (if (bitwise_equal_p (@0, @3))
2281 (convert (bit_ior (bit_and @1 (convert @2)) (convert @0))))))
2282
2283 /* (x | CST1) & CST2 -> (x & CST2) | (CST1 & CST2) */
2284 (simplify
2285 (bit_and (bit_ior @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
2286 (bit_ior (bit_and @0 @2) (bit_and! @1 @2)))
2287
2288 /* Combine successive equal operations with constants. */
2289 (for bitop (bit_and bit_ior bit_xor)
2290 (simplify
2291 (bitop (bitop @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
2292 (if (!CONSTANT_CLASS_P (@0))
2293 /* This is the canonical form regardless of whether (bitop @1 @2) can be
2294 folded to a constant. */
2295 (bitop @0 (bitop! @1 @2))
2296 /* In this case we have three constants and (bitop @0 @1) doesn't fold
2297 to a constant. This can happen if @0 or @1 is a POLY_INT_CST and if
2298 the values involved are such that the operation can't be decided at
2299 compile time. Try folding one of @0 or @1 with @2 to see whether
2300 that combination can be decided at compile time.
2301
2302 Keep the existing form if both folds fail, to avoid endless
2303 oscillation. */
2304 (with { tree cst1 = const_binop (bitop, type, @0, @2); }
2305 (if (cst1)
2306 (bitop @1 { cst1; })
2307 (with { tree cst2 = const_binop (bitop, type, @1, @2); }
2308 (if (cst2)
2309 (bitop @0 { cst2; }))))))))
2310
2311 /* Try simple folding for X op !X, and X op X with the help
2312 of the truth_valued_p and logical_inverted_value predicates. */
2313 (match truth_valued_p
2314 @0
2315 (if (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1)))
2316 (for op (tcc_comparison truth_and truth_andif truth_or truth_orif truth_xor)
2317 (match truth_valued_p
2318 (op @0 @1)))
2319 (match truth_valued_p
2320 (truth_not @0))
2321
2322 (match (logical_inverted_value @0)
2323 (truth_not @0))
2324 (match (logical_inverted_value @0)
2325 (bit_not truth_valued_p@0))
2326 (match (logical_inverted_value @0)
2327 (eq @0 integer_zerop))
2328 (match (logical_inverted_value @0)
2329 (ne truth_valued_p@0 integer_truep))
2330 (match (logical_inverted_value @0)
2331 (bit_xor truth_valued_p@0 integer_truep))
2332
2333 /* X & !X -> 0. */
2334 (simplify
2335 (bit_and:c @0 (logical_inverted_value @0))
2336 { build_zero_cst (type); })
2337 /* X | !X and X ^ !X -> 1, , if X is truth-valued. */
2338 (for op (bit_ior bit_xor)
2339 (simplify
2340 (op:c truth_valued_p@0 (logical_inverted_value @0))
2341 { constant_boolean_node (true, type); }))
2342 /* X ==/!= !X is false/true. */
2343 (for op (eq ne)
2344 (simplify
2345 (op:c truth_valued_p@0 (logical_inverted_value @0))
2346 { constant_boolean_node (op == NE_EXPR ? true : false, type); }))
2347
2348 /* ~~x -> x */
2349 (simplify
2350 (bit_not (bit_not @0))
2351 @0)
2352
2353 /* zero_one_valued_p will match when a value is known to be either
2354 0 or 1 including constants 0 or 1.
2355 Signed 1-bits includes -1 so they cannot match here. */
2356 (match zero_one_valued_p
2357 @0
2358 (if (INTEGRAL_TYPE_P (type)
2359 && (TYPE_UNSIGNED (type)
2360 || TYPE_PRECISION (type) > 1)
2361 && wi::leu_p (tree_nonzero_bits (@0), 1))))
2362 (match zero_one_valued_p
2363 truth_valued_p@0
2364 (if (INTEGRAL_TYPE_P (type)
2365 && (TYPE_UNSIGNED (type)
2366 || TYPE_PRECISION (type) > 1))))
2367
2368 /* (a&1) is always [0,1] too. This is useful again when
2369 the range is not known. */
2370 /* Note this can't be recursive due to VN handling of equivalents,
2371 VN and would cause an infinite recursion. */
2372 (match zero_one_valued_p
2373 (bit_and:c@0 @1 integer_onep)
2374 (if (INTEGRAL_TYPE_P (type))))
2375
2376 /* A conversion from an zero_one_valued_p is still a [0,1].
2377 This is useful when the range of a variable is not known */
2378 /* Note this matches can't be recursive because of the way VN handles
2379 nop conversions being equivalent and then recursive between them. */
2380 (match zero_one_valued_p
2381 (convert@0 @1)
2382 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2383 && (TYPE_UNSIGNED (TREE_TYPE (@1))
2384 || TYPE_PRECISION (TREE_TYPE (@1)) > 1)
2385 && INTEGRAL_TYPE_P (type)
2386 && (TYPE_UNSIGNED (type)
2387 || TYPE_PRECISION (type) > 1)
2388 && wi::leu_p (tree_nonzero_bits (@1), 1))))
2389
2390 /* Transform { 0 or 1 } * { 0 or 1 } into { 0 or 1 } & { 0 or 1 }. */
2391 (simplify
2392 (mult zero_one_valued_p@0 zero_one_valued_p@1)
2393 (if (INTEGRAL_TYPE_P (type))
2394 (bit_and @0 @1)))
2395
2396 /* Fold `(a ? b : 0) | (a ? 0 : c)` into (a ? b : c).
2397 Handle also ^ and + in replacement of `|`. */
2398 (for cnd (cond vec_cond)
2399 (for op (bit_ior bit_xor plus)
2400 (simplify
2401 (op:c
2402 (cnd:s @0 @00 integer_zerop)
2403 (cnd:s @0 integer_zerop @01))
2404 (cnd @0 @00 @01))))
2405
2406 (for cmp (tcc_comparison)
2407 icmp (inverted_tcc_comparison)
2408 /* Fold (((a < b) & c) | ((a >= b) & d)) into (a < b ? c : d) & 1. */
2409 (simplify
2410 (bit_ior
2411 (bit_and:c (convert? (cmp@0 @01 @02)) @3)
2412 (bit_and:c (convert? (icmp@4 @01 @02)) @5))
2413 (if (INTEGRAL_TYPE_P (type)
2414 && invert_tree_comparison (cmp, HONOR_NANS (@01)) == icmp
2415 /* The scalar version has to be canonicalized after vectorization
2416 because it makes unconditional loads conditional ones, which
2417 means we lose vectorization because the loads may trap. */
2418 && canonicalize_math_after_vectorization_p ())
2419 (bit_and (cond @0 @3 @5) { build_one_cst (type); })))
2420
2421 /* Fold ((-(a < b) & c) | (-(a >= b) & d)) into a < b ? c : d. This is
2422 canonicalized further and we recognize the conditional form:
2423 (a < b ? c : 0) | (a >= b ? d : 0) into a < b ? c : d.
2424 Handle also ^ and + in replacement of `|`. */
2425 (for op (bit_ior bit_xor plus)
2426 (simplify
2427 (op
2428 (cond (cmp@0 @01 @02) @3 zerop)
2429 (cond (icmp@4 @01 @02) @5 zerop))
2430 (if (INTEGRAL_TYPE_P (type)
2431 && invert_tree_comparison (cmp, HONOR_NANS (@01)) == icmp
2432 /* The scalar version has to be canonicalized after vectorization
2433 because it makes unconditional loads conditional ones, which
2434 means we lose vectorization because the loads may trap. */
2435 && canonicalize_math_after_vectorization_p ())
2436 (cond @0 @3 @5))))
2437
2438 /* Vector Fold (((a < b) & c) | ((a >= b) & d)) into a < b ? c : d.
2439 and ((~(a < b) & c) | (~(a >= b) & d)) into a < b ? c : d. */
2440 (simplify
2441 (bit_ior
2442 (bit_and:c (vec_cond:s (cmp@0 @6 @7) @4 @5) @2)
2443 (bit_and:c (vec_cond:s (icmp@1 @6 @7) @4 @5) @3))
2444 (if (integer_zerop (@5)
2445 && invert_tree_comparison (cmp, HONOR_NANS (@6)) == icmp)
2446 (switch
2447 (if (integer_onep (@4))
2448 (bit_and (vec_cond @0 @2 @3) @4))
2449 (if (integer_minus_onep (@4))
2450 (vec_cond @0 @2 @3)))
2451 (if (integer_zerop (@4)
2452 && invert_tree_comparison (cmp, HONOR_NANS (@6)) == icmp)
2453 (switch
2454 (if (integer_onep (@5))
2455 (bit_and (vec_cond @0 @3 @2) @5))
2456 (if (integer_minus_onep (@5))
2457 (vec_cond @0 @3 @2))))))
2458
2459 /* Scalar Vectorized Fold ((-(a < b) & c) | (-(a >= b) & d))
2460 into a < b ? d : c.
2461 Handle also ^ and + in replacement of `|`. */
2462 (for op (bit_ior bit_xor plus)
2463 (simplify
2464 (op
2465 (vec_cond:s (cmp@0 @4 @5) @2 integer_zerop)
2466 (vec_cond:s (icmp@1 @4 @5) @3 integer_zerop))
2467 (if (invert_tree_comparison (cmp, HONOR_NANS (@4)) == icmp)
2468 (vec_cond @0 @2 @3)))))
2469
2470 /* Transform X & -Y into X * Y when Y is { 0 or 1 }. */
2471 (simplify
2472 (bit_and:c (convert? (negate zero_one_valued_p@0)) @1)
2473 (if (INTEGRAL_TYPE_P (type)
2474 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
2475 && TREE_CODE (TREE_TYPE (@0)) != BOOLEAN_TYPE
2476 /* Sign extending of the neg or a truncation of the neg
2477 is needed. */
2478 && (!TYPE_UNSIGNED (TREE_TYPE (@0))
2479 || TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))))
2480 (mult (convert @0) @1)))
2481
2482 /* Narrow integer multiplication by a zero_one_valued_p operand.
2483 Multiplication by [0,1] is guaranteed not to overflow except for
2484 1bit signed types. */
2485 (simplify
2486 (convert (mult@0 zero_one_valued_p@1 INTEGER_CST@2))
2487 (if (INTEGRAL_TYPE_P (type)
2488 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
2489 && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (@0))
2490 && (TYPE_UNSIGNED (type) || TYPE_PRECISION (type) > 1))
2491 (mult (convert @1) (convert @2))))
2492
2493 /* (X << C) != 0 can be simplified to X, when C is zero_one_valued_p.
2494 Check that the shift is well-defined (C is less than TYPE_PRECISION)
2495 as some targets (such as x86's SSE) may return zero for larger C. */
2496 (simplify
2497 (ne (lshift zero_one_valued_p@0 INTEGER_CST@1) integer_zerop@2)
2498 (if (tree_fits_shwi_p (@1)
2499 && tree_to_shwi (@1) > 0
2500 && tree_to_shwi (@1) < TYPE_PRECISION (TREE_TYPE (@0)))
2501 (convert @0)))
2502
2503 /* (X << C) == 0 can be simplified to X == 0, when C is zero_one_valued_p.
2504 Check that the shift is well-defined (C is less than TYPE_PRECISION)
2505 as some targets (such as x86's SSE) may return zero for larger C. */
2506 (simplify
2507 (eq (lshift zero_one_valued_p@0 INTEGER_CST@1) integer_zerop@2)
2508 (if (tree_fits_shwi_p (@1)
2509 && tree_to_shwi (@1) > 0
2510 && tree_to_shwi (@1) < TYPE_PRECISION (TREE_TYPE (@0)))
2511 (eq @0 @2)))
2512
2513 /* Convert ~ (-A) to A - 1. */
2514 (simplify
2515 (bit_not (convert? (negate @0)))
2516 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
2517 || !TYPE_UNSIGNED (TREE_TYPE (@0)))
2518 (convert (minus @0 { build_each_one_cst (TREE_TYPE (@0)); }))))
2519
2520 /* Convert - (~A) to A + 1. */
2521 (simplify
2522 (negate (nop_convert? (bit_not @0)))
2523 (plus (view_convert @0) { build_each_one_cst (type); }))
2524
2525 /* (a & b) ^ (a == b) -> !(a | b) */
2526 /* (a & b) == (a ^ b) -> !(a | b) */
2527 (for first_op (bit_xor eq)
2528 second_op (eq bit_xor)
2529 (simplify
2530 (first_op:c (bit_and:c truth_valued_p@0 truth_valued_p@1) (second_op @0 @1))
2531 (bit_not (bit_ior @0 @1))))
2532
2533 /* Convert ~ (A - 1) or ~ (A + -1) to -A. */
2534 (simplify
2535 (bit_not (convert? (minus @0 integer_each_onep)))
2536 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
2537 || !TYPE_UNSIGNED (TREE_TYPE (@0)))
2538 (convert (negate @0))))
2539 (simplify
2540 (bit_not (convert? (plus @0 integer_all_onesp)))
2541 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
2542 || !TYPE_UNSIGNED (TREE_TYPE (@0)))
2543 (convert (negate @0))))
2544
2545 /* Part of convert ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify. */
2546 (simplify
2547 (bit_not (convert? (bit_xor @0 INTEGER_CST@1)))
2548 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2549 (convert (bit_xor @0 (bit_not @1)))))
2550 (simplify
2551 (bit_not (convert? (bit_xor:c (bit_not @0) @1)))
2552 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2553 (convert (bit_xor @0 @1))))
2554
2555 /* Otherwise prefer ~(X ^ Y) to ~X ^ Y as more canonical. */
2556 (simplify
2557 (bit_xor:c (nop_convert?:s (bit_not:s @0)) @1)
2558 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2559 (bit_not (bit_xor (view_convert @0) @1))))
2560
2561 /* ~(a ^ b) is a == b for truth valued a and b. */
2562 (simplify
2563 (bit_not (bit_xor:s truth_valued_p@0 truth_valued_p@1))
2564 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2565 && TYPE_PRECISION (TREE_TYPE (@0)) == 1)
2566 (convert (eq @0 @1))))
2567
2568 /* (~a) == b is a ^ b for truth valued a and b. */
2569 (simplify
2570 (eq:c (bit_not:s truth_valued_p@0) truth_valued_p@1)
2571 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2572 && TYPE_PRECISION (TREE_TYPE (@0)) == 1)
2573 (convert (bit_xor @0 @1))))
2574
2575 /* (x & ~m) | (y & m) -> ((x ^ y) & m) ^ x */
2576 (simplify
2577 (bit_ior:c (bit_and:cs @0 (bit_not @2)) (bit_and:cs @1 @2))
2578 (bit_xor (bit_and (bit_xor @0 @1) @2) @0))
2579
2580 /* Fold A - (A & B) into ~B & A. */
2581 (simplify
2582 (minus (convert1? @0) (convert2?:s (bit_and:cs @@0 @1)))
2583 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
2584 && tree_nop_conversion_p (type, TREE_TYPE (@1)))
2585 (convert (bit_and (bit_not @1) @0))))
2586
2587 /* (m1 CMP m2) * d -> (m1 CMP m2) ? d : 0 */
2588 (if (!canonicalize_math_p ())
2589 (for cmp (tcc_comparison)
2590 (simplify
2591 (mult:c (convert (cmp@0 @1 @2)) @3)
2592 (if (INTEGRAL_TYPE_P (type)
2593 && INTEGRAL_TYPE_P (TREE_TYPE (@0)))
2594 (cond @0 @3 { build_zero_cst (type); })))
2595 /* (-(m1 CMP m2)) & d -> (m1 CMP m2) ? d : 0 */
2596 (simplify
2597 (bit_and:c (negate (convert (cmp@0 @1 @2))) @3)
2598 (if (INTEGRAL_TYPE_P (type)
2599 && INTEGRAL_TYPE_P (TREE_TYPE (@0)))
2600 (cond @0 @3 { build_zero_cst (type); })))
2601 )
2602 )
2603
2604 /* For integral types with undefined overflow and C != 0 fold
2605 x * C EQ/NE y * C into x EQ/NE y. */
2606 (for cmp (eq ne)
2607 (simplify
2608 (cmp (mult:c @0 @1) (mult:c @2 @1))
2609 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2610 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2611 && tree_expr_nonzero_p (@1))
2612 (cmp @0 @2))))
2613
2614 /* For integral types with wrapping overflow and C odd fold
2615 x * C EQ/NE y * C into x EQ/NE y. */
2616 (for cmp (eq ne)
2617 (simplify
2618 (cmp (mult @0 INTEGER_CST@1) (mult @2 @1))
2619 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2620 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))
2621 && (TREE_INT_CST_LOW (@1) & 1) != 0)
2622 (cmp @0 @2))))
2623
2624 /* For integral types with undefined overflow and C != 0 fold
2625 x * C RELOP y * C into:
2626
2627 x RELOP y for nonnegative C
2628 y RELOP x for negative C */
2629 (for cmp (lt gt le ge)
2630 (simplify
2631 (cmp (mult:c @0 @1) (mult:c @2 @1))
2632 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2633 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
2634 (if (tree_expr_nonnegative_p (@1) && tree_expr_nonzero_p (@1))
2635 (cmp @0 @2)
2636 (if (TREE_CODE (@1) == INTEGER_CST
2637 && wi::neg_p (wi::to_wide (@1), TYPE_SIGN (TREE_TYPE (@1))))
2638 (cmp @2 @0))))))
2639
2640 /* (X - 1U) <= INT_MAX-1U into (int) X > 0. */
2641 (for cmp (le gt)
2642 icmp (gt le)
2643 (simplify
2644 (cmp (plus @0 integer_minus_onep@1) INTEGER_CST@2)
2645 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2646 && TYPE_UNSIGNED (TREE_TYPE (@0))
2647 && TYPE_PRECISION (TREE_TYPE (@0)) > 1
2648 && (wi::to_wide (@2)
2649 == wi::max_value (TYPE_PRECISION (TREE_TYPE (@0)), SIGNED) - 1))
2650 (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
2651 (icmp (convert:stype @0) { build_int_cst (stype, 0); })))))
2652
2653 /* X / 4 < Y / 4 iff X < Y when the division is known to be exact. */
2654 (for cmp (simple_comparison)
2655 (simplify
2656 (cmp (convert?@3 (exact_div @0 INTEGER_CST@2)) (convert? (exact_div @1 @2)))
2657 (if (element_precision (@3) >= element_precision (@0)
2658 && types_match (@0, @1))
2659 (if (wi::lt_p (wi::to_wide (@2), 0, TYPE_SIGN (TREE_TYPE (@2))))
2660 (if (!TYPE_UNSIGNED (TREE_TYPE (@3)))
2661 (cmp @1 @0)
2662 (if (tree_expr_nonzero_p (@0) && tree_expr_nonzero_p (@1))
2663 (with
2664 {
2665 tree utype = unsigned_type_for (TREE_TYPE (@0));
2666 }
2667 (cmp (convert:utype @1) (convert:utype @0)))))
2668 (if (wi::gt_p (wi::to_wide (@2), 1, TYPE_SIGN (TREE_TYPE (@2))))
2669 (if (TYPE_UNSIGNED (TREE_TYPE (@0)) || !TYPE_UNSIGNED (TREE_TYPE (@3)))
2670 (cmp @0 @1)
2671 (with
2672 {
2673 tree utype = unsigned_type_for (TREE_TYPE (@0));
2674 }
2675 (cmp (convert:utype @0) (convert:utype @1)))))))))
2676
2677 /* X / C1 op C2 into a simple range test. */
2678 (for cmp (simple_comparison)
2679 (simplify
2680 (cmp (trunc_div:s @0 INTEGER_CST@1) INTEGER_CST@2)
2681 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2682 && integer_nonzerop (@1)
2683 && !TREE_OVERFLOW (@1)
2684 && !TREE_OVERFLOW (@2))
2685 (with { tree lo, hi; bool neg_overflow;
2686 enum tree_code code = fold_div_compare (cmp, @1, @2, &lo, &hi,
2687 &neg_overflow); }
2688 (switch
2689 (if (code == LT_EXPR || code == GE_EXPR)
2690 (if (TREE_OVERFLOW (lo))
2691 { build_int_cst (type, (code == LT_EXPR) ^ neg_overflow); }
2692 (if (code == LT_EXPR)
2693 (lt @0 { lo; })
2694 (ge @0 { lo; }))))
2695 (if (code == LE_EXPR || code == GT_EXPR)
2696 (if (TREE_OVERFLOW (hi))
2697 { build_int_cst (type, (code == LE_EXPR) ^ neg_overflow); }
2698 (if (code == LE_EXPR)
2699 (le @0 { hi; })
2700 (gt @0 { hi; }))))
2701 (if (!lo && !hi)
2702 { build_int_cst (type, code == NE_EXPR); })
2703 (if (code == EQ_EXPR && !hi)
2704 (ge @0 { lo; }))
2705 (if (code == EQ_EXPR && !lo)
2706 (le @0 { hi; }))
2707 (if (code == NE_EXPR && !hi)
2708 (lt @0 { lo; }))
2709 (if (code == NE_EXPR && !lo)
2710 (gt @0 { hi; }))
2711 (if (GENERIC)
2712 { build_range_check (UNKNOWN_LOCATION, type, @0, code == EQ_EXPR,
2713 lo, hi); })
2714 (with
2715 {
2716 tree etype = range_check_type (TREE_TYPE (@0));
2717 if (etype)
2718 {
2719 hi = fold_convert (etype, hi);
2720 lo = fold_convert (etype, lo);
2721 hi = const_binop (MINUS_EXPR, etype, hi, lo);
2722 }
2723 }
2724 (if (etype && hi && !TREE_OVERFLOW (hi))
2725 (if (code == EQ_EXPR)
2726 (le (minus (convert:etype @0) { lo; }) { hi; })
2727 (gt (minus (convert:etype @0) { lo; }) { hi; })))))))))
2728
2729 /* X + Z < Y + Z is the same as X < Y when there is no overflow. */
2730 (for op (lt le ge gt)
2731 (simplify
2732 (op (plus:c @0 @2) (plus:c @1 @2))
2733 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2734 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
2735 (op @0 @1))))
2736
2737 /* As a special case, X + C < Y + C is the same as (signed) X < (signed) Y
2738 when C is an unsigned integer constant with only the MSB set, and X and
2739 Y have types of equal or lower integer conversion rank than C's. */
2740 (for op (lt le ge gt)
2741 (simplify
2742 (op (plus @1 INTEGER_CST@0) (plus @2 @0))
2743 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2744 && TYPE_UNSIGNED (TREE_TYPE (@0))
2745 && wi::only_sign_bit_p (wi::to_wide (@0)))
2746 (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
2747 (op (convert:stype @1) (convert:stype @2))))))
2748
2749 /* For equality and subtraction, this is also true with wrapping overflow. */
2750 (for op (eq ne minus)
2751 (simplify
2752 (op (plus:c @0 @2) (plus:c @1 @2))
2753 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2754 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2755 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
2756 (op @0 @1))))
2757 /* And similar for pointers. */
2758 (for op (eq ne)
2759 (simplify
2760 (op (pointer_plus @0 @1) (pointer_plus @0 @2))
2761 (op @1 @2)))
2762 (simplify
2763 (pointer_diff (pointer_plus @0 @1) (pointer_plus @0 @2))
2764 (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
2765 (convert (minus @1 @2))))
2766
2767 /* X - Z < Y - Z is the same as X < Y when there is no overflow. */
2768 (for op (lt le ge gt)
2769 (simplify
2770 (op (minus @0 @2) (minus @1 @2))
2771 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2772 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
2773 (op @0 @1))))
2774 /* For equality and subtraction, this is also true with wrapping overflow. */
2775 (for op (eq ne minus)
2776 (simplify
2777 (op (minus @0 @2) (minus @1 @2))
2778 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2779 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2780 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
2781 (op @0 @1))))
2782 /* And for pointers... */
2783 (for op (simple_comparison)
2784 (simplify
2785 (op (pointer_diff@3 @0 @2) (pointer_diff @1 @2))
2786 (if (!TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
2787 (op @0 @1))))
2788 (simplify
2789 (minus (pointer_diff@3 @0 @2) (pointer_diff @1 @2))
2790 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@3))
2791 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
2792 (pointer_diff @0 @1)))
2793
2794 /* Z - X < Z - Y is the same as Y < X when there is no overflow. */
2795 (for op (lt le ge gt)
2796 (simplify
2797 (op (minus @2 @0) (minus @2 @1))
2798 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2799 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
2800 (op @1 @0))))
2801 /* For equality and subtraction, this is also true with wrapping overflow. */
2802 (for op (eq ne minus)
2803 (simplify
2804 (op (minus @2 @0) (minus @2 @1))
2805 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2806 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2807 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
2808 (op @1 @0))))
2809 /* And for pointers... */
2810 (for op (simple_comparison)
2811 (simplify
2812 (op (pointer_diff@3 @2 @0) (pointer_diff @2 @1))
2813 (if (!TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
2814 (op @1 @0))))
2815 (simplify
2816 (minus (pointer_diff@3 @2 @0) (pointer_diff @2 @1))
2817 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@3))
2818 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
2819 (pointer_diff @1 @0)))
2820
2821 /* X + Y < Y is the same as X < 0 when there is no overflow. */
2822 (for op (lt le gt ge)
2823 (simplify
2824 (op:c (plus:c@2 @0 @1) @1)
2825 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2826 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2827 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0))
2828 && (CONSTANT_CLASS_P (@0) || single_use (@2)))
2829 (op @0 { build_zero_cst (TREE_TYPE (@0)); }))))
2830 /* For equality, this is also true with wrapping overflow. */
2831 (for op (eq ne)
2832 (simplify
2833 (op:c (nop_convert?@3 (plus:c@2 @0 (convert1? @1))) (convert2? @1))
2834 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2835 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2836 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
2837 && (CONSTANT_CLASS_P (@0) || (single_use (@2) && single_use (@3)))
2838 && tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@2))
2839 && tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@1)))
2840 (op @0 { build_zero_cst (TREE_TYPE (@0)); })))
2841 (simplify
2842 (op:c (nop_convert?@3 (pointer_plus@2 (convert1? @0) @1)) (convert2? @0))
2843 (if (tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@0))
2844 && tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0))
2845 && (CONSTANT_CLASS_P (@1) || (single_use (@2) && single_use (@3))))
2846 (op @1 { build_zero_cst (TREE_TYPE (@1)); }))))
2847
2848 /* (&a + b) !=/== (&a[1] + c) -> (&a[0] - &a[1]) + b !=/== c */
2849 (for neeq (ne eq)
2850 (simplify
2851 (neeq:c ADDR_EXPR@0 (pointer_plus @2 @3))
2852 (with { poly_int64 diff; tree inner_type = TREE_TYPE (@3);}
2853 (if (ptr_difference_const (@0, @2, &diff))
2854 (neeq { build_int_cst_type (inner_type, diff); } @3))))
2855 (simplify
2856 (neeq (pointer_plus ADDR_EXPR@0 @1) (pointer_plus ADDR_EXPR@2 @3))
2857 (with { poly_int64 diff; tree inner_type = TREE_TYPE (@1);}
2858 (if (ptr_difference_const (@0, @2, &diff))
2859 (neeq (plus { build_int_cst_type (inner_type, diff); } @1) @3)))))
2860
2861 /* X - Y < X is the same as Y > 0 when there is no overflow.
2862 For equality, this is also true with wrapping overflow. */
2863 (for op (simple_comparison)
2864 (simplify
2865 (op:c @0 (minus@2 @0 @1))
2866 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2867 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2868 || ((op == EQ_EXPR || op == NE_EXPR)
2869 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
2870 && (CONSTANT_CLASS_P (@1) || single_use (@2)))
2871 (op @1 { build_zero_cst (TREE_TYPE (@1)); }))))
2872
2873 /* Transform:
2874 (X / Y) == 0 -> X < Y if X, Y are unsigned.
2875 (X / Y) != 0 -> X >= Y, if X, Y are unsigned. */
2876 (for cmp (eq ne)
2877 ocmp (lt ge)
2878 (simplify
2879 (cmp (trunc_div @0 @1) integer_zerop)
2880 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
2881 /* Complex ==/!= is allowed, but not </>=. */
2882 && TREE_CODE (TREE_TYPE (@0)) != COMPLEX_TYPE
2883 && (VECTOR_TYPE_P (type) || !VECTOR_TYPE_P (TREE_TYPE (@0))))
2884 (ocmp @0 @1))))
2885
2886 /* X == C - X can never be true if C is odd. */
2887 (for cmp (eq ne)
2888 (simplify
2889 (cmp:c (convert? @0) (convert1? (minus INTEGER_CST@1 (convert2? @0))))
2890 (if (TREE_INT_CST_LOW (@1) & 1)
2891 { constant_boolean_node (cmp == NE_EXPR, type); })))
2892
2893 /*
2894 U & N <= U -> true
2895 U & N > U -> false
2896 U needs to be non-negative.
2897
2898 U | N < U -> false
2899 U | N >= U -> true
2900 U and N needs to be non-negative
2901
2902 U | N < U -> true
2903 U | N >= U -> false
2904 U needs to be non-negative and N needs to be a negative constant.
2905 */
2906 (for cmp (lt ge le gt )
2907 bitop (bit_ior bit_ior bit_and bit_and)
2908 (simplify
2909 (cmp:c (bitop:c tree_expr_nonnegative_p@0 @1) @0)
2910 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)))
2911 (if (bitop == BIT_AND_EXPR || tree_expr_nonnegative_p (@1))
2912 { constant_boolean_node (cmp == GE_EXPR || cmp == LE_EXPR, type); }
2913 /* The sign is opposite now so the comparison is swapped around. */
2914 (if (TREE_CODE (@1) == INTEGER_CST && wi::neg_p (wi::to_wide (@1)))
2915 { constant_boolean_node (cmp == LT_EXPR, type); })))))
2916
2917 /* Arguments on which one can call get_nonzero_bits to get the bits
2918 possibly set. with_possible_nonzero_bits_1 is an internal version,
2919 use with_possible_nonzero_bits. */
2920 (match with_possible_nonzero_bits_1
2921 INTEGER_CST@0)
2922 (match with_possible_nonzero_bits_1
2923 POLY_INT_CST@0)
2924 (match with_possible_nonzero_bits_1
2925 SSA_NAME@0
2926 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))))
2927 /* Slightly extended version, do not make it recursive to keep it cheap. */
2928 (match with_possible_nonzero_bits
2929 with_possible_nonzero_bits_1@0)
2930 #if GENERIC
2931 (match with_possible_nonzero_bits
2932 (bit_and:c with_possible_nonzero_bits_1@0 @1))
2933 #endif
2934
2935 /* Arguments on which one can call get_known_nonzero_bits to get the
2936 bits known to be set. with_known_nonzero_bits_1 is an internal version,
2937 use with_known_nonzero_bits. */
2938 (match with_known_nonzero_bits_1
2939 INTEGER_CST@0)
2940 (match with_known_nonzero_bits_1
2941 SSA_NAME@0
2942 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)))))
2943 /* Slightly extended version, do not make it recursive to keep it cheap. */
2944 (match with_known_nonzero_bits
2945 with_known_nonzero_bits_1@0)
2946 #if GENERIC
2947 (match with_known_nonzero_bits
2948 (bit_ior:c with_known_nonzero_bits_1@0 @1))
2949 #endif
2950
2951 /* X == C (or X & Z == Y | C) is impossible if ~nonzero(X) & C != 0. */
2952 (for cmp (eq ne)
2953 (simplify
2954 (cmp:c with_possible_nonzero_bits@0 with_known_nonzero_bits@1)
2955 (if (wi::bit_and_not (get_known_nonzero_bits (@1),
2956 get_nonzero_bits (@0)) != 0)
2957 { constant_boolean_node (cmp == NE_EXPR, type); })))
2958
2959 /* ((X inner_op C0) outer_op C1)
2960 With X being a tree where range has reasoned certain bits to always be
2961 zero throughout its computed value range,
2962 inner_op = {|,^}, outer_op = {|,^} and inner_op != outer_op
2963 where zero_mask has 1's for all bits that are sure to be 0 in
2964 and 0's otherwise.
2965 if (inner_op == '^') C0 &= ~C1;
2966 if ((C0 & ~zero_mask) == 0) then emit (X outer_op (C0 outer_op C1)
2967 if ((C1 & ~zero_mask) == 0) then emit (X inner_op (C0 outer_op C1)
2968 */
2969 (for inner_op (bit_ior bit_xor)
2970 outer_op (bit_xor bit_ior)
2971 (simplify
2972 (outer_op
2973 (inner_op:s @2 INTEGER_CST@0) INTEGER_CST@1)
2974 (with
2975 {
2976 bool fail = false;
2977 wide_int zero_mask_not;
2978 wide_int C0;
2979 wide_int cst_emit;
2980
2981 if (TREE_CODE (@2) == SSA_NAME)
2982 zero_mask_not = get_nonzero_bits (@2);
2983 else
2984 fail = true;
2985
2986 if (inner_op == BIT_XOR_EXPR)
2987 {
2988 C0 = wi::bit_and_not (wi::to_wide (@0), wi::to_wide (@1));
2989 cst_emit = C0 | wi::to_wide (@1);
2990 }
2991 else
2992 {
2993 C0 = wi::to_wide (@0);
2994 cst_emit = C0 ^ wi::to_wide (@1);
2995 }
2996 }
2997 (if (!fail && (C0 & zero_mask_not) == 0)
2998 (outer_op @2 { wide_int_to_tree (type, cst_emit); })
2999 (if (!fail && (wi::to_wide (@1) & zero_mask_not) == 0)
3000 (inner_op @2 { wide_int_to_tree (type, cst_emit); }))))))
3001
3002 /* Associate (p +p off1) +p off2 as (p +p (off1 + off2)). */
3003 (simplify
3004 (pointer_plus (pointer_plus:s @0 @1) @3)
3005 (pointer_plus @0 (plus @1 @3)))
3006 #if GENERIC
3007 (simplify
3008 (pointer_plus (convert:s (pointer_plus:s @0 @1)) @3)
3009 (pointer_plus (convert:type @0) (plus @1 @3)))
3010 #endif
3011
3012 /* Pattern match
3013 tem1 = (long) ptr1;
3014 tem2 = (long) ptr2;
3015 tem3 = tem2 - tem1;
3016 tem4 = (unsigned long) tem3;
3017 tem5 = ptr1 + tem4;
3018 and produce
3019 tem5 = ptr2; */
3020 (simplify
3021 (pointer_plus @0 (convert?@2 (minus@3 (convert @1) (convert @0))))
3022 /* Conditionally look through a sign-changing conversion. */
3023 (if (TYPE_PRECISION (TREE_TYPE (@2)) == TYPE_PRECISION (TREE_TYPE (@3))
3024 && ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@1)))
3025 || (GENERIC && type == TREE_TYPE (@1))))
3026 @1))
3027 (simplify
3028 (pointer_plus @0 (convert?@2 (pointer_diff@3 @1 @@0)))
3029 (if (TYPE_PRECISION (TREE_TYPE (@2)) >= TYPE_PRECISION (TREE_TYPE (@3)))
3030 (convert @1)))
3031
3032 /* Pattern match
3033 tem = (sizetype) ptr;
3034 tem = tem & algn;
3035 tem = -tem;
3036 ... = ptr p+ tem;
3037 and produce the simpler and easier to analyze with respect to alignment
3038 ... = ptr & ~algn; */
3039 (simplify
3040 (pointer_plus @0 (negate (bit_and (convert @0) INTEGER_CST@1)))
3041 (with { tree algn = wide_int_to_tree (TREE_TYPE (@0), ~wi::to_wide (@1)); }
3042 (bit_and @0 { algn; })))
3043
3044 /* Also match cases in which a constant is applied:
3045
3046 (1) tem = (sizetype) ptr; ---> tem = (sizetype) (ptr + CST);
3047 (2) tem = -tem ---> tem = CST - tem;
3048
3049 and where "& align" masks only trailing zeros of CST. (1) then has no
3050 effect, whereas (2) adds CST to the result. */
3051 (simplify
3052 (pointer_plus @0 (negate (bit_and (convert (pointer_plus @0 INTEGER_CST@1))
3053 INTEGER_CST@2)))
3054 (if (tree_int_cst_min_precision (@2, UNSIGNED) <= tree_ctz (@1))
3055 (with { tree algn = wide_int_to_tree (TREE_TYPE (@0), ~wi::to_wide (@2)); }
3056 (bit_and @0 { algn; }))))
3057 (simplify
3058 (pointer_plus @0 (minus:s INTEGER_CST@1 (bit_and (convert @0) INTEGER_CST@2)))
3059 (if (tree_int_cst_min_precision (@2, UNSIGNED) <= tree_ctz (@1))
3060 (with { tree algn = wide_int_to_tree (TREE_TYPE (@0), ~wi::to_wide (@2)); }
3061 (pointer_plus (bit_and @0 { algn; }) @1))))
3062 (simplify
3063 (pointer_plus @0 (minus:s INTEGER_CST@1
3064 (bit_and (convert (pointer_plus @0 INTEGER_CST@2))
3065 INTEGER_CST@3)))
3066 (with { auto mask_width = tree_int_cst_min_precision (@3, UNSIGNED); }
3067 (if (mask_width <= tree_ctz (@1) && mask_width <= tree_ctz (@2))
3068 (with { tree algn = wide_int_to_tree (TREE_TYPE (@0), ~wi::to_wide (@3)); }
3069 (pointer_plus (bit_and @0 { algn; }) @1)))))
3070
3071 /* Try folding difference of addresses. */
3072 (simplify
3073 (minus (convert ADDR_EXPR@0) (convert (pointer_plus @1 @2)))
3074 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
3075 (with { poly_int64 diff; }
3076 (if (ptr_difference_const (@0, @1, &diff))
3077 (minus { build_int_cst_type (type, diff); } (convert @2))))))
3078 (simplify
3079 (minus (convert (pointer_plus @0 @2)) (convert ADDR_EXPR@1))
3080 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
3081 (with { poly_int64 diff; }
3082 (if (ptr_difference_const (@0, @1, &diff))
3083 (plus (convert @2) { build_int_cst_type (type, diff); })))))
3084 (simplify
3085 (minus (convert ADDR_EXPR@0) (convert @1))
3086 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
3087 (with { poly_int64 diff; }
3088 (if (ptr_difference_const (@0, @1, &diff))
3089 { build_int_cst_type (type, diff); }))))
3090 (simplify
3091 (minus (convert @0) (convert ADDR_EXPR@1))
3092 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
3093 (with { poly_int64 diff; }
3094 (if (ptr_difference_const (@0, @1, &diff))
3095 { build_int_cst_type (type, diff); }))))
3096 (simplify
3097 (pointer_diff (convert?@2 ADDR_EXPR@0) (convert1?@3 @1))
3098 (if (tree_nop_conversion_p (TREE_TYPE(@2), TREE_TYPE (@0))
3099 && tree_nop_conversion_p (TREE_TYPE(@3), TREE_TYPE (@1)))
3100 (with { poly_int64 diff; }
3101 (if (ptr_difference_const (@0, @1, &diff))
3102 { build_int_cst_type (type, diff); }))))
3103 (simplify
3104 (pointer_diff (convert?@2 @0) (convert1?@3 ADDR_EXPR@1))
3105 (if (tree_nop_conversion_p (TREE_TYPE(@2), TREE_TYPE (@0))
3106 && tree_nop_conversion_p (TREE_TYPE(@3), TREE_TYPE (@1)))
3107 (with { poly_int64 diff; }
3108 (if (ptr_difference_const (@0, @1, &diff))
3109 { build_int_cst_type (type, diff); }))))
3110
3111 /* (&a+b) - (&a[1] + c) -> sizeof(a[0]) + (b - c) */
3112 (simplify
3113 (pointer_diff (pointer_plus ADDR_EXPR@0 @1) (pointer_plus ADDR_EXPR@2 @3))
3114 (with { poly_int64 diff; }
3115 (if (ptr_difference_const (@0, @2, &diff))
3116 (plus { build_int_cst_type (type, diff); } (convert (minus @1 @3))))))
3117 /* (p + b) - &p->d -> offsetof (*p, d) + b */
3118 (simplify
3119 (pointer_diff (pointer_plus @0 @1) ADDR_EXPR@2)
3120 (with { poly_int64 diff; }
3121 (if (ptr_difference_const (@0, @2, &diff))
3122 (plus { build_int_cst_type (type, diff); } (convert @1)))))
3123 (simplify
3124 (pointer_diff ADDR_EXPR@0 (pointer_plus @1 @2))
3125 (with { poly_int64 diff; }
3126 (if (ptr_difference_const (@0, @1, &diff))
3127 (minus { build_int_cst_type (type, diff); } (convert @2)))))
3128
3129 /* Canonicalize (T *)(ptr - ptr-cst) to &MEM[ptr + -ptr-cst]. */
3130 (simplify
3131 (convert (pointer_diff @0 INTEGER_CST@1))
3132 (if (POINTER_TYPE_P (type))
3133 { build_fold_addr_expr_with_type
3134 (build2 (MEM_REF, char_type_node, @0,
3135 wide_int_to_tree (ptr_type_node, wi::neg (wi::to_wide (@1)))),
3136 type); }))
3137
3138 /* If arg0 is derived from the address of an object or function, we may
3139 be able to fold this expression using the object or function's
3140 alignment. */
3141 (simplify
3142 (bit_and (convert? @0) INTEGER_CST@1)
3143 (if (POINTER_TYPE_P (TREE_TYPE (@0))
3144 && tree_nop_conversion_p (type, TREE_TYPE (@0)))
3145 (with
3146 {
3147 unsigned int align;
3148 unsigned HOST_WIDE_INT bitpos;
3149 get_pointer_alignment_1 (@0, &align, &bitpos);
3150 }
3151 (if (wi::ltu_p (wi::to_wide (@1), align / BITS_PER_UNIT))
3152 { wide_int_to_tree (type, (wi::to_wide (@1)
3153 & (bitpos / BITS_PER_UNIT))); }))))
3154
3155 (match min_value
3156 uniform_integer_cst_p
3157 (with {
3158 tree int_cst = uniform_integer_cst_p (t);
3159 tree inner_type = TREE_TYPE (int_cst);
3160 }
3161 (if ((INTEGRAL_TYPE_P (inner_type)
3162 || POINTER_TYPE_P (inner_type))
3163 && wi::eq_p (wi::to_wide (int_cst), wi::min_value (inner_type))))))
3164
3165 (match max_value
3166 uniform_integer_cst_p
3167 (with {
3168 tree int_cst = uniform_integer_cst_p (t);
3169 tree itype = TREE_TYPE (int_cst);
3170 }
3171 (if ((INTEGRAL_TYPE_P (itype)
3172 || POINTER_TYPE_P (itype))
3173 && wi::eq_p (wi::to_wide (int_cst), wi::max_value (itype))))))
3174
3175 /* Saturation add for unsigned integer. */
3176 (if (INTEGRAL_TYPE_P (type) && TYPE_UNSIGNED (type))
3177 (match (usadd_overflow_mask @0 @1)
3178 /* SAT_U_ADD = (X + Y) | -(X > (X + Y)).
3179 Overflow_Mask = -(X > (X + Y)). */
3180 (negate (convert (gt @0 (plus:c @0 @1))))
3181 (if (types_match (type, @0, @1))))
3182 (match (usadd_overflow_mask @0 @1)
3183 /* SAT_U_ADD = (X + Y) | -(X > (X + Y)).
3184 Overflow_Mask = -((X + Y) < X). */
3185 (negate (convert (lt (plus:c @0 @1) @0)))
3186 (if (types_match (type, @0, @1))))
3187 (match (unsigned_integer_sat_add @0 @1)
3188 /* SAT_U_ADD = (X + Y) | Overflow_Mask */
3189 (bit_ior:c (plus:c @0 @1) (usadd_overflow_mask @0 @1))
3190 (if (types_match (type, @0, @1))))
3191 (match (unsigned_integer_sat_add @0 @1)
3192 /* SAT_U_ADD = (X + Y) >= X ? (X + Y) : -1 */
3193 (cond^ (ge (plus:c@2 @0 @1) @0) @2 integer_minus_onep)
3194 (if (types_match (type, @0, @1))))
3195 (match (unsigned_integer_sat_add @0 @1)
3196 /* SAT_U_ADD = (X + Y) < X ? -1 : (X + Y) */
3197 (cond^ (lt (plus:c@2 @0 @1) @0) integer_minus_onep @2)
3198 (if (types_match (type, @0, @1))))
3199 (match (unsigned_integer_sat_add @0 @1)
3200 /* SAT_U_ADD = X <= (X + Y) ? (X + Y) : -1 */
3201 (cond^ (le @0 (plus:c@2 @0 @1)) @2 integer_minus_onep)
3202 (if (types_match (type, @0, @1))))
3203 (match (unsigned_integer_sat_add @0 @1)
3204 /* SAT_U_ADD = X > (X + Y) ? -1 : (X + Y) */
3205 (cond^ (gt @0 (plus:c@2 @0 @1)) integer_minus_onep @2)
3206 (if (types_match (type, @0, @1))))
3207 (match (unsigned_integer_sat_add @0 @1)
3208 /* SAT_U_ADD = (X + IMM) >= x ? (X + IMM) : -1 */
3209 (plus (min @0 INTEGER_CST@2) INTEGER_CST@1)
3210 (if (types_match (type, @0, @1))
3211 (with
3212 {
3213 unsigned precision = TYPE_PRECISION (type);
3214 wide_int cst_1 = wi::to_wide (@1);
3215 wide_int cst_2 = wi::to_wide (@2);
3216 wide_int max = wi::mask (precision, false, precision);
3217 wide_int sum = wi::add (cst_1, cst_2);
3218 }
3219 (if (wi::eq_p (max, sum))))))
3220 (match (unsigned_integer_sat_add @0 @1)
3221 /* SUM = ADD_OVERFLOW (X, Y)
3222 SAT_U_ADD = REALPART (SUM) | -IMAGPART (SUM) */
3223 (bit_ior:c (realpart (IFN_ADD_OVERFLOW@2 @0 @1)) (negate (imagpart @2)))
3224 (if (types_match (type, @0, @1))))
3225 (match (unsigned_integer_sat_add @0 @1)
3226 /* SUM = ADD_OVERFLOW (X, Y)
3227 SAT_U_ADD = REALPART (SUM) | -(IMAGPART (SUM) != 0) */
3228 (bit_ior:c (realpart (IFN_ADD_OVERFLOW@2 @0 @1))
3229 (negate (convert (ne (imagpart @2) integer_zerop))))
3230 (if (types_match (type, @0, @1))))
3231 (match (unsigned_integer_sat_add @0 @1)
3232 /* SUM = ADD_OVERFLOW (X, Y)
3233 SAT_U_ADD = IMAGPART (SUM) == 0 ? REALPART (SUM) : -1 */
3234 (cond^ (eq (imagpart (IFN_ADD_OVERFLOW@2 @0 @1)) integer_zerop)
3235 (realpart @2) integer_minus_onep)
3236 (if (types_match (type, @0, @1))))
3237 (match (unsigned_integer_sat_add @0 @1)
3238 /* SUM = ADD_OVERFLOW (X, Y)
3239 SAT_U_ADD = IMAGPART (SUM) != 0 ? -1 : REALPART (SUM) */
3240 (cond^ (ne (imagpart (IFN_ADD_OVERFLOW@2 @0 @1)) integer_zerop)
3241 integer_minus_onep (realpart @2))
3242 (if (types_match (type, @0, @1))))
3243 (match (unsigned_integer_sat_add @0 @1)
3244 /* SUM = ADD_OVERFLOW (X, IMM)
3245 SAT_U_ADD = IMAGPART (SUM) != 0 ? -1 : REALPART (SUM) */
3246 (cond^ (ne (imagpart (IFN_ADD_OVERFLOW@2 @0 INTEGER_CST@1)) integer_zerop)
3247 integer_minus_onep (realpart @2))
3248 (if (types_match (type, @0) && int_fits_type_p (@1, type)))))
3249
3250 /* Saturation sub for unsigned integer. */
3251 (if (INTEGRAL_TYPE_P (type) && TYPE_UNSIGNED (type))
3252 (match (unsigned_integer_sat_sub @0 @1)
3253 /* SAT_U_SUB = X > Y ? X - Y : 0 */
3254 (cond^ (gt @0 @1) (minus @0 @1) integer_zerop)
3255 (if (types_match (type, @0, @1))))
3256 (match (unsigned_integer_sat_sub @0 @1)
3257 /* SAT_U_SUB = X >= Y ? X - Y : 0 */
3258 (cond^ (ge @0 @1) (convert? (minus (convert1? @0) (convert1? @1)))
3259 integer_zerop)
3260 (if (TYPE_UNSIGNED (TREE_TYPE (@0)) && types_match (@0, @1))))
3261 (match (unsigned_integer_sat_sub @0 @1)
3262 /* SAT_U_SUB = (X - Y) * (X > Y) */
3263 (mult:c (minus @0 @1) (convert (gt @0 @1)))
3264 (if (types_match (type, @0, @1))))
3265 (match (unsigned_integer_sat_sub @0 @1)
3266 /* SAT_U_SUB = (X - Y) * (X >= Y) */
3267 (mult:c (minus @0 @1) (convert (ge @0 @1)))
3268 (if (types_match (type, @0, @1))))
3269 (match (unsigned_integer_sat_sub @0 @1)
3270 /* DIFF = SUB_OVERFLOW (X, Y)
3271 SAT_U_SUB = REALPART (DIFF) | (IMAGPART (DIFF) + (-1)) */
3272 (bit_and:c (realpart (IFN_SUB_OVERFLOW@2 @0 @1))
3273 (plus (imagpart @2) integer_minus_onep))
3274 (if (types_match (type, @0, @1))))
3275 (match (unsigned_integer_sat_sub @0 @1)
3276 /* DIFF = SUB_OVERFLOW (X, Y)
3277 SAT_U_SUB = REALPART (DIFF) * (IMAGPART (DIFF) ^ (1)) */
3278 (mult:c (realpart (IFN_SUB_OVERFLOW@2 @0 @1))
3279 (bit_xor (imagpart @2) integer_onep))
3280 (if (types_match (type, @0, @1))))
3281 (match (unsigned_integer_sat_sub @0 @1)
3282 /* DIFF = SUB_OVERFLOW (X, Y)
3283 SAT_U_SUB = IMAGPART (DIFF) == 0 ? REALPART (DIFF) : 0 */
3284 (cond^ (eq (imagpart (IFN_SUB_OVERFLOW@2 @0 @1)) integer_zerop)
3285 (realpart @2) integer_zerop)
3286 (if (types_match (type, @0, @1))))
3287 (match (unsigned_integer_sat_sub @0 @1)
3288 /* DIFF = SUB_OVERFLOW (X, Y)
3289 SAT_U_SUB = IMAGPART (DIFF) != 0 ? 0 : REALPART (DIFF) */
3290 (cond^ (ne (imagpart (IFN_SUB_OVERFLOW@2 @0 @1)) integer_zerop)
3291 integer_zerop (realpart @2))
3292 (if (types_match (type, @0, @1))))
3293 (match (unsigned_integer_sat_sub @0 @1)
3294 /* SAT_U_SUB = IMM > Y ? (IMM - Y) : 0
3295 SAT_U_SUB = IMM >= Y ? (IMM - Y) : 0 */
3296 (cond^ (le @1 INTEGER_CST@2) (minus INTEGER_CST@0 @1) integer_zerop)
3297 (if (types_match (type, @1) && int_fits_type_p (@0, type))
3298 (with
3299 {
3300 unsigned precision = TYPE_PRECISION (type);
3301 wide_int max = wi::mask (precision, false, precision);
3302 wide_int c0 = wi::to_wide (@0);
3303 wide_int c2 = wi::to_wide (@2);
3304 wide_int c2_add_1 = wi::add (c2, wi::uhwi (1, precision));
3305 bool equal_p = wi::eq_p (c0, c2);
3306 bool less_than_1_p = !wi::eq_p (c2, max) && wi::eq_p (c2_add_1, c0);
3307 }
3308 (if (equal_p || less_than_1_p)))))
3309 (match (unsigned_integer_sat_sub @0 @1)
3310 /* SAT_U_SUB = (MAX - 1) >= Y ? ((MAX - 1) - Y) : 0 */
3311 (cond^ (ne @1 INTEGER_CST@2) (minus INTEGER_CST@0 @1) integer_zerop)
3312 (if (types_match (type, @1))
3313 (with
3314 {
3315 unsigned precision = TYPE_PRECISION (type);
3316 wide_int max = wi::mask (precision, false, precision);
3317 wide_int c0 = wi::to_wide (@0);
3318 wide_int c2 = wi::to_wide (@2);
3319 wide_int c0_add_1 = wi::add (c0, wi::uhwi (1, precision));
3320 }
3321 (if (wi::eq_p (c2, max) && wi::eq_p (c0_add_1, max))))))
3322 (match (unsigned_integer_sat_sub @0 @1)
3323 /* SAT_U_SUB = 1 >= Y ? (1 - Y) : 0 */
3324 (cond^ (le @1 integer_onep@0) (bit_xor @1 integer_onep@0) integer_zerop)
3325 (if (types_match (type, @1))))
3326 (match (unsigned_integer_sat_sub @0 @1)
3327 /* SAT_U_SUB = X > IMM ? (X - IMM) : 0.
3328 SAT_U_SUB = X >= IMM ? (X - IMM) : 0. */
3329 (plus (max @0 INTEGER_CST@1) INTEGER_CST@2)
3330 (if (types_match (type, @1) && int_fits_type_p (@1, type))
3331 (with
3332 {
3333 unsigned precision = TYPE_PRECISION (type);
3334 wide_int c1 = wi::to_wide (@1);
3335 wide_int c2 = wi::to_wide (@2);
3336 wide_int sum = wi::add (c1, c2);
3337 }
3338 (if (wi::eq_p (sum, wi::uhwi (0, precision))))))))
3339
3340 /* Saturation truncate for unsigned integer. */
3341 (if (INTEGRAL_TYPE_P (type) && TYPE_UNSIGNED (type))
3342 (match (unsigned_integer_sat_trunc @0)
3343 /* SAT_U_TRUNC = (NT)x | (NT)(-(X > (WT)(NT)(-1))) */
3344 (bit_ior:c (negate (convert (gt @0 INTEGER_CST@1))) (convert @0))
3345 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
3346 (with
3347 {
3348 unsigned itype_precision = TYPE_PRECISION (TREE_TYPE (@0));
3349 unsigned otype_precision = TYPE_PRECISION (type);
3350 wide_int trunc_max = wi::mask (otype_precision, false, itype_precision);
3351 wide_int int_cst = wi::to_wide (@1, itype_precision);
3352 }
3353 (if (otype_precision < itype_precision && wi::eq_p (trunc_max, int_cst))))))
3354 (match (unsigned_integer_sat_trunc @0)
3355 /* SAT_U_TRUNC = (NT)(MIN_EXPR (X, IMM))
3356 If Op_0 def is MIN_EXPR and not single_use. Aka below pattern:
3357
3358 _18 = MIN_EXPR <left_8, 4294967295>; // op_0 def
3359 iftmp.0_11 = (unsigned int) _18; // op_0
3360 stream.avail_out = iftmp.0_11;
3361 left_37 = left_8 - _18; // op_0 use
3362
3363 Transfer to .SAT_TRUNC will have MIN_EXPR still live. Then the backend
3364 (for example x86/riscv) will have 2-3 more insns generation for .SAT_TRUNC
3365 besides the MIN_EXPR. Thus, keep the normal truncation as is should be
3366 the better choose. */
3367 (convert (min@2 @0 INTEGER_CST@1))
3368 (if (TYPE_UNSIGNED (TREE_TYPE (@0)) && single_use (@2))
3369 (with
3370 {
3371 unsigned itype_precision = TYPE_PRECISION (TREE_TYPE (@0));
3372 unsigned otype_precision = TYPE_PRECISION (type);
3373 wide_int trunc_max = wi::mask (otype_precision, false, itype_precision);
3374 wide_int int_cst = wi::to_wide (@1, itype_precision);
3375 }
3376 (if (otype_precision < itype_precision && wi::eq_p (trunc_max, int_cst))))))
3377 (match (unsigned_integer_sat_trunc @0)
3378 /* SAT_U_TRUNC = (NT)X | ((NT)(X <= (WT)-1) + (NT)-1) */
3379 (bit_ior:c (plus:c (convert (le @0 INTEGER_CST@1)) INTEGER_CST@2)
3380 (convert @0))
3381 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
3382 (with
3383 {
3384 unsigned itype_precision = TYPE_PRECISION (TREE_TYPE (@0));
3385 unsigned otype_precision = TYPE_PRECISION (type);
3386 wide_int trunc_max = wi::mask (otype_precision, false, itype_precision);
3387 wide_int max = wi::mask (otype_precision, false, otype_precision);
3388 wide_int int_cst_1 = wi::to_wide (@1);
3389 wide_int int_cst_2 = wi::to_wide (@2);
3390 }
3391 (if (wi::eq_p (trunc_max, int_cst_1) && wi::eq_p (max, int_cst_2)))))))
3392
3393 (if (INTEGRAL_TYPE_P (type) && !TYPE_UNSIGNED (type))
3394 /* SAT_VAL = (-(T)(X < 0) ^ MAX) */
3395 (match (signed_integer_sat_val @0)
3396 (bit_xor:c (nop_convert? (negate
3397 (nop_convert? (convert (lt @0 integer_zerop)))))
3398 max_value)))
3399
3400 /* Saturation add for signed integer. */
3401 (if (INTEGRAL_TYPE_P (type) && !TYPE_UNSIGNED (type))
3402 (match (signed_integer_sat_add @0 @1)
3403 /* T SUM = (T)((UT)X + (UT)Y)
3404 SAT_S_ADD = (X ^ SUM) & !(X ^ Y) < 0 ? (-(T)(X < 0) ^ MAX) : SUM */
3405 (cond^ (lt (bit_and:c (bit_xor:c @0 (nop_convert@2 (plus (nop_convert @0)
3406 (nop_convert @1))))
3407 (bit_not (bit_xor:c @0 @1)))
3408 integer_zerop)
3409 (signed_integer_sat_val @0)
3410 @2))
3411 (match (signed_integer_sat_add @0 @1)
3412 /* T SUM = (T)((UT)X + (UT)Y)
3413 SAT_S_ADD = (X ^ SUM) & !(X ^ Y) >= 0 ? SUM : (-(T)(X < 0) ^ MAX) */
3414 (cond^ (ge (bit_and:c (bit_xor @0 (nop_convert@2 (plus (nop_convert @0)
3415 (nop_convert @1))))
3416 (bit_not (bit_xor:c @0 @1)))
3417 integer_zerop)
3418 @2
3419 (bit_xor:c (negate (convert (lt @0 integer_zerop))) max_value)))
3420 (match (signed_integer_sat_add @0 @1)
3421 /* T SUM = (T)((UT)X + (UT)Y)
3422 SAT_S_ADD = (X ^ Y) < 0 && (X ^ SUM) >= 0 ? (-(T)(X < 0) ^ MAX) : SUM */
3423 (cond^ (bit_and:c (lt (bit_xor @0 (nop_convert@2 (plus (nop_convert @0)
3424 (nop_convert @1))))
3425 integer_zerop)
3426 (ge (bit_xor:c @0 @1) integer_zerop))
3427 (signed_integer_sat_val @0)
3428 @2))
3429 (match (signed_integer_sat_add @0 @1)
3430 /* SUM = .ADD_OVERFLOW (X, Y)
3431 SAT_S_ADD = IMAGPART_EXPR (SUM) != 0 ? (-(T)(X < 0) ^ MAX) : SUM */
3432 (cond^ (ne (imagpart (IFN_ADD_OVERFLOW:c@2 @0 @1)) integer_zerop)
3433 (signed_integer_sat_val @0)
3434 (realpart @2)))
3435 (match (signed_integer_sat_add @0 @1)
3436 /* T SUM = (T)((UT)X + (UT)Y)
3437 SAT_S_ADD = (X ^ SUM) < 0 & ~((X ^ Y) < 0) ? (-(T)(X < 0) ^ MAX) : SUM */
3438 (cond^ (bit_and:c (lt (bit_xor @0 (nop_convert@2 (plus (nop_convert @0)
3439 (nop_convert @1))))
3440 integer_zerop)
3441 (bit_not (lt (bit_xor:c @0 @1) integer_zerop)))
3442 (signed_integer_sat_val @0)
3443 @2))
3444 (match (signed_integer_sat_add @0 @1)
3445 /* T SUM = (T)((UT)X + (UT)IMM);
3446 SAT_S_ADD = (X ^ IMM) < 0 ? SUM : (X ^ SUM) >= 0 ? SUM
3447 : (x < 0) ? MIN : MAX */
3448 (cond^ (lt (bit_and:c (bit_xor:c @0 (nop_convert@2 (plus (nop_convert @0)
3449 INTEGER_CST@1)))
3450 (bit_xor:c @0 INTEGER_CST@3)) integer_zerop)
3451 (signed_integer_sat_val @0)
3452 @2)
3453 (if (wi::bit_and (wi::to_wide (@1), wi::to_wide (@3)) == 0))))
3454
3455 /* Saturation sub for signed integer. */
3456 (if (INTEGRAL_TYPE_P (type) && !TYPE_UNSIGNED (type))
3457 (match (signed_integer_sat_sub @0 @1)
3458 /* T Z = (T)((UT)X - (UT)Y);
3459 SAT_S_SUB = (X ^ Y) & (X ^ Z) < 0 ? (-(T)(X < 0) ^ MAX) : Z */
3460 (cond^ (lt (bit_and:c (bit_xor:c @0 @1)
3461 (bit_xor @0 (nop_convert@2 (minus (nop_convert @0)
3462 (nop_convert @1)))))
3463 integer_zerop)
3464 (signed_integer_sat_val @0)
3465 @2))
3466 (match (signed_integer_sat_sub @0 @1)
3467 /* T Z = (T)((UT)X - (UT)Y);
3468 SAT_S_SUB = (X ^ Y) & (X ^ Z) >= 0 ? Z : (-(T)(X < 0) ^ MAX) */
3469 (cond^ (ge (bit_and:c (bit_xor:c @0 @1)
3470 (bit_xor @0 (nop_convert@2 (minus (nop_convert @0)
3471 (nop_convert @1)))))
3472 integer_zerop)
3473 @2
3474 (signed_integer_sat_val @0)))
3475 (match (signed_integer_sat_sub @0 @1)
3476 /* T Z = (T)((UT)X - (UT)Y);
3477 SAT_S_SUB = (X ^ Y) < 0 & (X ^ Z) < 0 ? (-(T)(X < 0) ^ MAX) : Z */
3478 (cond^ (bit_and:c (lt (bit_xor @0 (nop_convert@2 (minus (nop_convert @0)
3479 (nop_convert @1))))
3480 integer_zerop)
3481 (lt (bit_xor:c @0 @1) integer_zerop))
3482 (signed_integer_sat_val @0)
3483 @2))
3484 (match (signed_integer_sat_sub @0 @1)
3485 /* Z = .SUB_OVERFLOW (X, Y)
3486 SAT_S_SUB = IMAGPART (Z) != 0 ? (-(T)(X < 0) ^ MAX) : REALPART (Z) */
3487 (cond^ (ne (imagpart (IFN_SUB_OVERFLOW@2 @0 @1)) integer_zerop)
3488 (signed_integer_sat_val @0)
3489 (realpart @2))
3490 (if (types_match (type, @0, @1)))))
3491
3492 /* Saturation truncate for signed integer. */
3493 (if (INTEGRAL_TYPE_P (type) && !TYPE_UNSIGNED (type))
3494 (match (signed_integer_sat_trunc @0)
3495 /* SAT_S_TRUNC(X) = (unsigned)X + NT_MAX + 1 > Unsigned_MAX ? (NT)X */
3496 (cond^ (gt (plus:c (convert@4 @0) INTEGER_CST@1) INTEGER_CST@2)
3497 (bit_xor:c (nop_convert?
3498 (negate (nop_convert? (convert (lt @0 integer_zerop)))))
3499 INTEGER_CST@3)
3500 (convert @0))
3501 (if (!TYPE_UNSIGNED (TREE_TYPE (@0)) && TYPE_UNSIGNED (TREE_TYPE (@4)))
3502 (with
3503 {
3504 unsigned itype_prec = TYPE_PRECISION (TREE_TYPE (@0));
3505 unsigned otype_prec = TYPE_PRECISION (type);
3506 wide_int offset = wi::uhwi (HOST_WIDE_INT_1U << (otype_prec - 1),
3507 itype_prec); // Aka 128 for int8_t
3508 wide_int limit_0 = wi::mask (otype_prec, false, itype_prec); // Aka 255
3509 wide_int limit_1 = wi::uhwi ((HOST_WIDE_INT_1U << otype_prec) - 3,
3510 itype_prec); // Aka 253
3511 wide_int limit_2 = wi::uhwi ((HOST_WIDE_INT_1U << otype_prec) - 2,
3512 itype_prec); // Aka 254
3513 wide_int otype_max = wi::mask (otype_prec - 1, false, otype_prec);
3514 wide_int itype_max = wi::mask (otype_prec - 1, false, itype_prec);
3515 wide_int int_cst_1 = wi::to_wide (@1);
3516 wide_int int_cst_2 = wi::to_wide (@2);
3517 wide_int int_cst_3 = wi::to_wide (@3);
3518 }
3519 (if (((wi::eq_p (int_cst_1, offset) && wi::eq_p (int_cst_2, limit_0))
3520 || (wi::eq_p (int_cst_1, itype_max) && wi::eq_p (int_cst_2, limit_2))
3521 || (wi::eq_p (int_cst_1, offset) && wi::eq_p (int_cst_2, limit_2))
3522 || (wi::eq_p (int_cst_1, itype_max) && wi::eq_p (int_cst_2, limit_1)))
3523 && wi::eq_p (int_cst_3, otype_max)))))))
3524
3525 /* The boundary condition for case 10: IMM = 1:
3526 SAT_U_SUB = X >= IMM ? (X - IMM) : 0.
3527 simplify (X != 0 ? X + ~0 : 0) to X - (X != 0). */
3528 (simplify
3529 (cond (ne@1 @0 integer_zerop)
3530 (nop_convert1? (plus (nop_convert2?@2 @0) integer_all_onesp))
3531 integer_zerop)
3532 (if (INTEGRAL_TYPE_P (type))
3533 (with { tree itype = TREE_TYPE (@2); }
3534 (convert (minus @2 (convert:itype @1))))))
3535
3536 /* x > y && x != XXX_MIN --> x > y
3537 x > y && x == XXX_MIN --> false . */
3538 (for eqne (eq ne)
3539 (simplify
3540 (bit_and:c (gt:c@2 @0 @1) (eqne @0 min_value))
3541 (switch
3542 (if (eqne == EQ_EXPR)
3543 { constant_boolean_node (false, type); })
3544 (if (eqne == NE_EXPR)
3545 @2)
3546 )))
3547
3548 /* x < y && x != XXX_MAX --> x < y
3549 x < y && x == XXX_MAX --> false. */
3550 (for eqne (eq ne)
3551 (simplify
3552 (bit_and:c (lt:c@2 @0 @1) (eqne @0 max_value))
3553 (switch
3554 (if (eqne == EQ_EXPR)
3555 { constant_boolean_node (false, type); })
3556 (if (eqne == NE_EXPR)
3557 @2)
3558 )))
3559
3560 /* x <= y && x == XXX_MIN --> x == XXX_MIN. */
3561 (simplify
3562 (bit_and:c (le:c @0 @1) (eq@2 @0 min_value))
3563 @2)
3564
3565 /* x >= y && x == XXX_MAX --> x == XXX_MAX. */
3566 (simplify
3567 (bit_and:c (ge:c @0 @1) (eq@2 @0 max_value))
3568 @2)
3569
3570 /* x > y || x != XXX_MIN --> x != XXX_MIN. */
3571 (simplify
3572 (bit_ior:c (gt:c @0 @1) (ne@2 @0 min_value))
3573 @2)
3574
3575 /* x <= y || x != XXX_MIN --> true. */
3576 (simplify
3577 (bit_ior:c (le:c @0 @1) (ne @0 min_value))
3578 { constant_boolean_node (true, type); })
3579
3580 /* x <= y || x == XXX_MIN --> x <= y. */
3581 (simplify
3582 (bit_ior:c (le:c@2 @0 @1) (eq @0 min_value))
3583 @2)
3584
3585 /* x < y || x != XXX_MAX --> x != XXX_MAX. */
3586 (simplify
3587 (bit_ior:c (lt:c @0 @1) (ne@2 @0 max_value))
3588 @2)
3589
3590 /* x >= y || x != XXX_MAX --> true
3591 x >= y || x == XXX_MAX --> x >= y. */
3592 (for eqne (eq ne)
3593 (simplify
3594 (bit_ior:c (ge:c@2 @0 @1) (eqne @0 max_value))
3595 (switch
3596 (if (eqne == EQ_EXPR)
3597 @2)
3598 (if (eqne == NE_EXPR)
3599 { constant_boolean_node (true, type); }))))
3600
3601 /* y == XXX_MIN || x < y --> x <= y - 1 */
3602 (simplify
3603 (bit_ior:c (eq:s @1 min_value) (lt:cs @0 @1))
3604 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
3605 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
3606 (le @0 (minus @1 { build_int_cst (TREE_TYPE (@1), 1); }))))
3607
3608 /* y != XXX_MIN && x >= y --> x > y - 1 */
3609 (simplify
3610 (bit_and:c (ne:s @1 min_value) (ge:cs @0 @1))
3611 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
3612 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
3613 (gt @0 (minus @1 { build_int_cst (TREE_TYPE (@1), 1); }))))
3614
3615 /* Convert (X == CST1) && ((other)X OP2 CST2) to a known value
3616 based on CST1 OP2 CST2. Similarly for (X != CST1). */
3617 /* Convert (X == Y) && (X OP2 Y) to a known value if X is an integral type.
3618 Similarly for (X != Y). */
3619
3620 (for code1 (eq ne)
3621 (for code2 (eq ne lt gt le ge)
3622 (simplify
3623 (bit_and:c (code1:c@3 @0 @1) (code2:c@4 (convert?@c0 @0) @2))
3624 (if ((TREE_CODE (@1) == INTEGER_CST
3625 && TREE_CODE (@2) == INTEGER_CST)
3626 || ((INTEGRAL_TYPE_P (TREE_TYPE (@1))
3627 || POINTER_TYPE_P (TREE_TYPE (@1)))
3628 && bitwise_equal_p (@1, @2)))
3629 (with
3630 {
3631 bool one_before = false;
3632 bool one_after = false;
3633 int cmp = 0;
3634 bool allbits = true;
3635 if (TREE_CODE (@1) == INTEGER_CST
3636 && TREE_CODE (@2) == INTEGER_CST)
3637 {
3638 allbits = TYPE_PRECISION (TREE_TYPE (@1)) <= TYPE_PRECISION (TREE_TYPE (@2));
3639 auto t1 = wi::to_wide (fold_convert (TREE_TYPE (@2), @1));
3640 auto t2 = wi::to_wide (@2);
3641 cmp = wi::cmp (t1, t2, TYPE_SIGN (TREE_TYPE (@2)));
3642 if (cmp < 0
3643 && t1 == t2 - 1)
3644 one_before = true;
3645 if (cmp > 0
3646 && t1 == t2 + 1)
3647 one_after = true;
3648 }
3649 bool val;
3650 switch (code2)
3651 {
3652 case EQ_EXPR: val = (cmp == 0); break;
3653 case NE_EXPR: val = (cmp != 0); break;
3654 case LT_EXPR: val = (cmp < 0); break;
3655 case GT_EXPR: val = (cmp > 0); break;
3656 case LE_EXPR: val = (cmp <= 0); break;
3657 case GE_EXPR: val = (cmp >= 0); break;
3658 default: gcc_unreachable ();
3659 }
3660 }
3661 (switch
3662 (if (code1 == EQ_EXPR && val) @3)
3663 (if (code1 == EQ_EXPR && !val) { constant_boolean_node (false, type); })
3664 (if (code1 == NE_EXPR && !val && allbits) @4)
3665 (if (code1 == NE_EXPR
3666 && code2 == GE_EXPR
3667 && cmp == 0
3668 && allbits)
3669 (gt @c0 (convert @1)))
3670 (if (code1 == NE_EXPR
3671 && code2 == LE_EXPR
3672 && cmp == 0
3673 && allbits)
3674 (lt @c0 (convert @1)))
3675 /* (a != (b+1)) & (a > b) -> a > (b+1) */
3676 (if (code1 == NE_EXPR
3677 && code2 == GT_EXPR
3678 && one_after
3679 && allbits)
3680 (gt @c0 (convert @1)))
3681 /* (a != (b-1)) & (a < b) -> a < (b-1) */
3682 (if (code1 == NE_EXPR
3683 && code2 == LT_EXPR
3684 && one_before
3685 && allbits)
3686 (lt @c0 (convert @1)))
3687 )
3688 )
3689 )
3690 )
3691 )
3692 )
3693
3694 /* Convert (X OP1 CST1) && (X OP2 CST2).
3695 Convert (X OP1 Y) && (X OP2 Y). */
3696
3697 (for code1 (lt le gt ge)
3698 (for code2 (lt le gt ge)
3699 (simplify
3700 (bit_and (code1:c@3 @0 @1) (code2:c@4 @0 @2))
3701 (if ((TREE_CODE (@1) == INTEGER_CST
3702 && TREE_CODE (@2) == INTEGER_CST)
3703 || ((INTEGRAL_TYPE_P (TREE_TYPE (@1))
3704 || POINTER_TYPE_P (TREE_TYPE (@1)))
3705 && operand_equal_p (@1, @2)))
3706 (with
3707 {
3708 int cmp = 0;
3709 if (TREE_CODE (@1) == INTEGER_CST
3710 && TREE_CODE (@2) == INTEGER_CST)
3711 cmp = tree_int_cst_compare (@1, @2);
3712 }
3713 (switch
3714 /* Choose the more restrictive of two < or <= comparisons. */
3715 (if ((code1 == LT_EXPR || code1 == LE_EXPR)
3716 && (code2 == LT_EXPR || code2 == LE_EXPR))
3717 (if ((cmp < 0) || (cmp == 0 && code1 == LT_EXPR))
3718 @3
3719 @4))
3720 /* Likewise chose the more restrictive of two > or >= comparisons. */
3721 (if ((code1 == GT_EXPR || code1 == GE_EXPR)
3722 && (code2 == GT_EXPR || code2 == GE_EXPR))
3723 (if ((cmp > 0) || (cmp == 0 && code1 == GT_EXPR))
3724 @3
3725 @4))
3726 /* Check for singleton ranges. */
3727 (if (cmp == 0
3728 && ((code1 == LE_EXPR && code2 == GE_EXPR)
3729 || (code1 == GE_EXPR && code2 == LE_EXPR)))
3730 (eq @0 @1))
3731 /* Check for disjoint ranges. */
3732 (if (cmp <= 0
3733 && (code1 == LT_EXPR || code1 == LE_EXPR)
3734 && (code2 == GT_EXPR || code2 == GE_EXPR))
3735 { constant_boolean_node (false, type); })
3736 (if (cmp >= 0
3737 && (code1 == GT_EXPR || code1 == GE_EXPR)
3738 && (code2 == LT_EXPR || code2 == LE_EXPR))
3739 { constant_boolean_node (false, type); })
3740 ))))))
3741
3742 /* Convert (X == CST1) || (X OP2 CST2) to a known value
3743 based on CST1 OP2 CST2. Similarly for (X != CST1). */
3744 /* Convert (X == Y) || (X OP2 Y) to a known value if X is an integral type.
3745 Similarly for (X != Y). */
3746
3747 (for code1 (eq ne)
3748 (for code2 (eq ne lt gt le ge)
3749 (simplify
3750 (bit_ior:c (code1:c@3 @0 @1) (code2:c@4 (convert?@c0 @0) @2))
3751 (if ((TREE_CODE (@1) == INTEGER_CST
3752 && TREE_CODE (@2) == INTEGER_CST)
3753 || ((INTEGRAL_TYPE_P (TREE_TYPE (@1))
3754 || POINTER_TYPE_P (TREE_TYPE (@1)))
3755 && bitwise_equal_p (@1, @2)))
3756 (with
3757 {
3758 bool one_before = false;
3759 bool one_after = false;
3760 int cmp = 0;
3761 bool allbits = true;
3762 if (TREE_CODE (@1) == INTEGER_CST
3763 && TREE_CODE (@2) == INTEGER_CST)
3764 {
3765 allbits = TYPE_PRECISION (TREE_TYPE (@1)) <= TYPE_PRECISION (TREE_TYPE (@2));
3766 auto t1 = wi::to_wide (fold_convert (TREE_TYPE (@2), @1));
3767 auto t2 = wi::to_wide (@2);
3768 cmp = wi::cmp (t1, t2, TYPE_SIGN (TREE_TYPE (@2)));
3769 if (cmp < 0
3770 && t1 == t2 - 1)
3771 one_before = true;
3772 if (cmp > 0
3773 && t1 == t2 + 1)
3774 one_after = true;
3775 }
3776 bool val;
3777 switch (code2)
3778 {
3779 case EQ_EXPR: val = (cmp == 0); break;
3780 case NE_EXPR: val = (cmp != 0); break;
3781 case LT_EXPR: val = (cmp < 0); break;
3782 case GT_EXPR: val = (cmp > 0); break;
3783 case LE_EXPR: val = (cmp <= 0); break;
3784 case GE_EXPR: val = (cmp >= 0); break;
3785 default: gcc_unreachable ();
3786 }
3787 }
3788 (switch
3789 (if (code1 == EQ_EXPR && val) @4)
3790 (if (code1 == NE_EXPR && val && allbits) { constant_boolean_node (true, type); })
3791 (if (code1 == NE_EXPR && !val && allbits) @3)
3792 (if (code1 == EQ_EXPR
3793 && code2 == GT_EXPR
3794 && cmp == 0
3795 && allbits)
3796 (ge @c0 @2))
3797 (if (code1 == EQ_EXPR
3798 && code2 == LT_EXPR
3799 && cmp == 0
3800 && allbits)
3801 (le @c0 @2))
3802 /* (a == (b-1)) | (a >= b) -> a >= (b-1) */
3803 (if (code1 == EQ_EXPR
3804 && code2 == GE_EXPR
3805 && one_before
3806 && allbits)
3807 (ge @c0 (convert @1)))
3808 /* (a == (b+1)) | (a <= b) -> a <= (b-1) */
3809 (if (code1 == EQ_EXPR
3810 && code2 == LE_EXPR
3811 && one_after
3812 && allbits)
3813 (le @c0 (convert @1)))
3814 )
3815 )
3816 )
3817 )
3818 )
3819 )
3820
3821 /* Convert (X OP1 CST1) || (X OP2 CST2).
3822 Convert (X OP1 Y) || (X OP2 Y). */
3823
3824 (for code1 (lt le gt ge)
3825 (for code2 (lt le gt ge)
3826 (simplify
3827 (bit_ior (code1@3 @0 @1) (code2@4 @0 @2))
3828 (if ((TREE_CODE (@1) == INTEGER_CST
3829 && TREE_CODE (@2) == INTEGER_CST)
3830 || ((INTEGRAL_TYPE_P (TREE_TYPE (@1))
3831 || POINTER_TYPE_P (TREE_TYPE (@1)))
3832 && operand_equal_p (@1, @2)))
3833 (with
3834 {
3835 int cmp = 0;
3836 if (TREE_CODE (@1) == INTEGER_CST
3837 && TREE_CODE (@2) == INTEGER_CST)
3838 cmp = tree_int_cst_compare (@1, @2);
3839 }
3840 (switch
3841 /* Choose the more restrictive of two < or <= comparisons. */
3842 (if ((code1 == LT_EXPR || code1 == LE_EXPR)
3843 && (code2 == LT_EXPR || code2 == LE_EXPR))
3844 (if ((cmp < 0) || (cmp == 0 && code1 == LT_EXPR))
3845 @4
3846 @3))
3847 /* Likewise chose the more restrictive of two > or >= comparisons. */
3848 (if ((code1 == GT_EXPR || code1 == GE_EXPR)
3849 && (code2 == GT_EXPR || code2 == GE_EXPR))
3850 (if ((cmp > 0) || (cmp == 0 && code1 == GT_EXPR))
3851 @4
3852 @3))
3853 /* Check for singleton ranges. */
3854 (if (cmp == 0
3855 && ((code1 == LT_EXPR && code2 == GT_EXPR)
3856 || (code1 == GT_EXPR && code2 == LT_EXPR)))
3857 (ne @0 @2))
3858 /* Check for disjoint ranges. */
3859 (if (cmp >= 0
3860 && (code1 == LT_EXPR || code1 == LE_EXPR)
3861 && (code2 == GT_EXPR || code2 == GE_EXPR))
3862 { constant_boolean_node (true, type); })
3863 (if (cmp <= 0
3864 && (code1 == GT_EXPR || code1 == GE_EXPR)
3865 && (code2 == LT_EXPR || code2 == LE_EXPR))
3866 { constant_boolean_node (true, type); })
3867 ))))))
3868
3869 /* Optimize (a CMP b) ^ (a CMP b) */
3870 /* Optimize (a CMP b) != (a CMP b) */
3871 (for op (bit_xor ne)
3872 (for cmp1 (lt lt lt le le le)
3873 cmp2 (gt eq ne ge eq ne)
3874 rcmp (ne le gt ne lt ge)
3875 (simplify
3876 (op:c (cmp1:c @0 @1) (cmp2 @0 @1))
3877 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
3878 (rcmp @0 @1)))))
3879
3880 /* Optimize (a CMP b) == (a CMP b) */
3881 (for cmp1 (lt lt lt le le le)
3882 cmp2 (gt eq ne ge eq ne)
3883 rcmp (eq gt le eq ge lt)
3884 (simplify
3885 (eq:c (cmp1:c @0 @1) (cmp2 @0 @1))
3886 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
3887 (rcmp @0 @1))))
3888
3889 /* (type)([0,1]@a != 0) -> (type)a
3890 (type)([0,1]@a == 1) -> (type)a
3891 (type)([0,1]@a == 0) -> a ^ 1
3892 (type)([0,1]@a != 1) -> a ^ 1. */
3893 (for eqne (eq ne)
3894 (simplify
3895 (convert (eqne zero_one_valued_p@0 INTEGER_CST@1))
3896 (if ((integer_zerop (@1) || integer_onep (@1)))
3897 (if ((eqne == EQ_EXPR) ^ integer_zerop (@1))
3898 (convert @0)
3899 /* Only do this if the types match as (type)(a == 0) is
3900 canonical form normally, while `a ^ 1` is canonical when
3901 there is no type change. */
3902 (if (types_match (type, TREE_TYPE (@0)))
3903 (bit_xor @0 { build_one_cst (type); } ))))))
3904
3905 /* ((a ^ b) & c) cmp d || a != b --> (0 cmp d || a != b). */
3906 (for cmp (simple_comparison)
3907 (simplify
3908 (bit_ior
3909 (cmp:c
3910 (bit_and:c
3911 (bit_xor:c @0 @1)
3912 tree_expr_nonzero_p@2)
3913 @3)
3914 (ne@4 @0 @1))
3915 (bit_ior
3916 (cmp
3917 { build_zero_cst (TREE_TYPE (@0)); }
3918 @3)
3919 @4)))
3920
3921 /* (a ^ b) cmp c || a != b --> (0 cmp c || a != b). */
3922 (for cmp (simple_comparison)
3923 (simplify
3924 (bit_ior
3925 (cmp:c
3926 (bit_xor:c @0 @1)
3927 @2)
3928 (ne@3 @0 @1))
3929 (bit_ior
3930 (cmp
3931 { build_zero_cst (TREE_TYPE (@0)); }
3932 @2)
3933 @3)))
3934
3935 /* We can't reassociate at all for saturating types. */
3936 (if (!TYPE_SATURATING (type))
3937
3938 /* Contract negates. */
3939 /* A + (-B) -> A - B */
3940 (simplify
3941 (plus:c @0 (convert? (negate @1)))
3942 /* Apply STRIP_NOPS on the negate. */
3943 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
3944 && !TYPE_OVERFLOW_SANITIZED (type))
3945 (with
3946 {
3947 tree t1 = type;
3948 if (INTEGRAL_TYPE_P (type)
3949 && TYPE_OVERFLOW_WRAPS (type) != TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
3950 t1 = TYPE_OVERFLOW_WRAPS (type) ? type : TREE_TYPE (@1);
3951 }
3952 (convert (minus (convert:t1 @0) (convert:t1 @1))))))
3953 /* A - (-B) -> A + B */
3954 (simplify
3955 (minus @0 (convert? (negate @1)))
3956 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
3957 && !TYPE_OVERFLOW_SANITIZED (type))
3958 (with
3959 {
3960 tree t1 = type;
3961 if (INTEGRAL_TYPE_P (type)
3962 && TYPE_OVERFLOW_WRAPS (type) != TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
3963 t1 = TYPE_OVERFLOW_WRAPS (type) ? type : TREE_TYPE (@1);
3964 }
3965 (convert (plus (convert:t1 @0) (convert:t1 @1))))))
3966 /* -(T)(-A) -> (T)A
3967 Sign-extension is ok except for INT_MIN, which thankfully cannot
3968 happen without overflow. */
3969 (simplify
3970 (negate (convert (negate @1)))
3971 (if (INTEGRAL_TYPE_P (type)
3972 && (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@1))
3973 || (!TYPE_UNSIGNED (TREE_TYPE (@1))
3974 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))))
3975 && !TYPE_OVERFLOW_SANITIZED (type)
3976 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@1)))
3977 (convert @1)))
3978 (simplify
3979 (negate (convert negate_expr_p@1))
3980 (if (SCALAR_FLOAT_TYPE_P (type)
3981 && ((DECIMAL_FLOAT_TYPE_P (type)
3982 == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1))
3983 && TYPE_PRECISION (type) >= TYPE_PRECISION (TREE_TYPE (@1)))
3984 || !HONOR_SIGN_DEPENDENT_ROUNDING (type)))
3985 (convert (negate @1))))
3986 (simplify
3987 (negate (nop_convert? (negate @1)))
3988 (if (!TYPE_OVERFLOW_SANITIZED (type)
3989 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@1)))
3990 (view_convert @1)))
3991
3992 /* We can't reassociate floating-point unless -fassociative-math
3993 or fixed-point plus or minus because of saturation to +-Inf. */
3994 (if ((!FLOAT_TYPE_P (type) || flag_associative_math)
3995 && !FIXED_POINT_TYPE_P (type))
3996
3997 /* Match patterns that allow contracting a plus-minus pair
3998 irrespective of overflow issues. */
3999 /* (A +- B) - A -> +- B */
4000 /* (A +- B) -+ B -> A */
4001 /* A - (A +- B) -> -+ B */
4002 /* A +- (B -+ A) -> +- B */
4003 (simplify
4004 (minus (nop_convert1? (plus:c (nop_convert2? @0) @1)) @0)
4005 (view_convert @1))
4006 (simplify
4007 (minus (nop_convert1? (minus (nop_convert2? @0) @1)) @0)
4008 (if (!ANY_INTEGRAL_TYPE_P (type)
4009 || TYPE_OVERFLOW_WRAPS (type))
4010 (negate (view_convert @1))
4011 (view_convert (negate @1))))
4012 (simplify
4013 (plus:c (nop_convert1? (minus @0 (nop_convert2? @1))) @1)
4014 (view_convert @0))
4015 (simplify
4016 (minus @0 (nop_convert1? (plus:c (nop_convert2? @0) @1)))
4017 (if (!ANY_INTEGRAL_TYPE_P (type)
4018 || TYPE_OVERFLOW_WRAPS (type))
4019 (negate (view_convert @1))
4020 (view_convert (negate @1))))
4021 (simplify
4022 (minus @0 (nop_convert1? (minus (nop_convert2? @0) @1)))
4023 (view_convert @1))
4024 /* (A +- B) + (C - A) -> C +- B */
4025 /* (A + B) - (A - C) -> B + C */
4026 /* More cases are handled with comparisons. */
4027 (simplify
4028 (plus:c (plus:c @0 @1) (minus @2 @0))
4029 (plus @2 @1))
4030 (simplify
4031 (plus:c (minus @0 @1) (minus @2 @0))
4032 (minus @2 @1))
4033 (simplify
4034 (plus:c (pointer_diff @0 @1) (pointer_diff @2 @0))
4035 (if (TYPE_OVERFLOW_UNDEFINED (type)
4036 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0)))
4037 (pointer_diff @2 @1)))
4038 (simplify
4039 (minus (plus:c @0 @1) (minus @0 @2))
4040 (plus @1 @2))
4041
4042 /* (A +- CST1) +- CST2 -> A + CST3
4043 Use view_convert because it is safe for vectors and equivalent for
4044 scalars. */
4045 (for outer_op (plus minus)
4046 (for inner_op (plus minus)
4047 neg_inner_op (minus plus)
4048 (simplify
4049 (outer_op (nop_convert? (inner_op @0 CONSTANT_CLASS_P@1))
4050 CONSTANT_CLASS_P@2)
4051 /* If one of the types wraps, use that one. */
4052 (if (!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_WRAPS (type))
4053 /* If all 3 captures are CONSTANT_CLASS_P, punt, as we might recurse
4054 forever if something doesn't simplify into a constant. */
4055 (if (!CONSTANT_CLASS_P (@0))
4056 (if (outer_op == PLUS_EXPR)
4057 (plus (view_convert @0) (inner_op! @2 (view_convert @1)))
4058 (minus (view_convert @0) (neg_inner_op! @2 (view_convert @1)))))
4059 (if (!ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
4060 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
4061 (if (outer_op == PLUS_EXPR)
4062 (view_convert (plus @0 (inner_op! (view_convert @2) @1)))
4063 (view_convert (minus @0 (neg_inner_op! (view_convert @2) @1))))
4064 /* If the constant operation overflows we cannot do the transform
4065 directly as we would introduce undefined overflow, for example
4066 with (a - 1) + INT_MIN. */
4067 (if (types_match (type, @0) && !TYPE_OVERFLOW_SANITIZED (type))
4068 (with { tree cst = const_binop (outer_op == inner_op
4069 ? PLUS_EXPR : MINUS_EXPR,
4070 type, @1, @2); }
4071 (if (cst)
4072 (if (INTEGRAL_TYPE_P (type) && !TREE_OVERFLOW (cst))
4073 (inner_op @0 { cst; } )
4074 /* X+INT_MAX+1 is X-INT_MIN. */
4075 (if (INTEGRAL_TYPE_P (type)
4076 && wi::to_wide (cst) == wi::min_value (type))
4077 (neg_inner_op @0 { wide_int_to_tree (type, wi::to_wide (cst)); })
4078 /* Last resort, use some unsigned type. */
4079 (with { tree utype = unsigned_type_for (type); }
4080 (if (utype)
4081 (view_convert (inner_op
4082 (view_convert:utype @0)
4083 (view_convert:utype
4084 { TREE_OVERFLOW (cst)
4085 ? drop_tree_overflow (cst) : cst; })))))))))))))))
4086
4087 /* (CST1 - A) +- CST2 -> CST3 - A */
4088 (for outer_op (plus minus)
4089 (simplify
4090 (outer_op (nop_convert? (minus CONSTANT_CLASS_P@1 @0)) CONSTANT_CLASS_P@2)
4091 /* If one of the types wraps, use that one. */
4092 (if (!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_WRAPS (type))
4093 /* If all 3 captures are CONSTANT_CLASS_P, punt, as we might recurse
4094 forever if something doesn't simplify into a constant. */
4095 (if (!CONSTANT_CLASS_P (@0))
4096 (minus (outer_op! (view_convert @1) @2) (view_convert @0)))
4097 (if (!ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
4098 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
4099 (view_convert (minus (outer_op! @1 (view_convert @2)) @0))
4100 (if (types_match (type, @0) && !TYPE_OVERFLOW_SANITIZED (type))
4101 (with { tree cst = const_binop (outer_op, type, @1, @2); }
4102 (if (cst && !TREE_OVERFLOW (cst))
4103 (minus { cst; } @0))))))))
4104
4105 /* CST1 - (CST2 - A) -> CST3 + A
4106 Use view_convert because it is safe for vectors and equivalent for
4107 scalars. */
4108 (simplify
4109 (minus CONSTANT_CLASS_P@1 (nop_convert? (minus CONSTANT_CLASS_P@2 @0)))
4110 /* If one of the types wraps, use that one. */
4111 (if (!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_WRAPS (type))
4112 /* If all 3 captures are CONSTANT_CLASS_P, punt, as we might recurse
4113 forever if something doesn't simplify into a constant. */
4114 (if (!CONSTANT_CLASS_P (@0))
4115 (plus (view_convert @0) (minus! @1 (view_convert @2))))
4116 (if (!ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
4117 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
4118 (view_convert (plus @0 (minus! (view_convert @1) @2)))
4119 (if (types_match (type, @0) && !TYPE_OVERFLOW_SANITIZED (type))
4120 (with { tree cst = const_binop (MINUS_EXPR, type, @1, @2); }
4121 (if (cst && !TREE_OVERFLOW (cst))
4122 (plus { cst; } @0)))))))
4123
4124 /* ((T)(A)) + CST -> (T)(A + CST) */
4125 #if GIMPLE
4126 (simplify
4127 (plus (convert:s SSA_NAME@0) INTEGER_CST@1)
4128 (if (TREE_CODE (TREE_TYPE (@0)) == INTEGER_TYPE
4129 && TREE_CODE (type) == INTEGER_TYPE
4130 && TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (@0))
4131 && int_fits_type_p (@1, TREE_TYPE (@0)))
4132 /* Perform binary operation inside the cast if the constant fits
4133 and (A + CST)'s range does not overflow. */
4134 (with
4135 {
4136 wi::overflow_type min_ovf = wi::OVF_OVERFLOW,
4137 max_ovf = wi::OVF_OVERFLOW;
4138 tree inner_type = TREE_TYPE (@0);
4139
4140 wide_int w1
4141 = wide_int::from (wi::to_wide (@1), TYPE_PRECISION (inner_type),
4142 TYPE_SIGN (inner_type));
4143
4144 int_range_max vr;
4145 if (get_global_range_query ()->range_of_expr (vr, @0)
4146 && !vr.varying_p () && !vr.undefined_p ())
4147 {
4148 wide_int wmin0 = vr.lower_bound ();
4149 wide_int wmax0 = vr.upper_bound ();
4150 wi::add (wmin0, w1, TYPE_SIGN (inner_type), &min_ovf);
4151 wi::add (wmax0, w1, TYPE_SIGN (inner_type), &max_ovf);
4152 }
4153 }
4154 (if (min_ovf == wi::OVF_NONE && max_ovf == wi::OVF_NONE)
4155 (convert (plus @0 { wide_int_to_tree (TREE_TYPE (@0), w1); } )))
4156 )))
4157 #endif
4158
4159 /* ((T)(A + CST1)) + CST2 -> (T)(A) + (T)CST1 + CST2 */
4160 #if GIMPLE
4161 (for op (plus minus)
4162 (simplify
4163 (plus (convert:s (op:s @0 INTEGER_CST@1)) INTEGER_CST@2)
4164 (if (TREE_CODE (TREE_TYPE (@0)) == INTEGER_TYPE
4165 && TREE_CODE (type) == INTEGER_TYPE
4166 && TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (@0))
4167 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
4168 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0))
4169 && TYPE_OVERFLOW_WRAPS (type))
4170 (plus (convert @0) (op @2 (convert @1))))))
4171 #endif
4172
4173 /* (T)(A) +- (T)(B) -> (T)(A +- B) only when (A +- B) could be simplified
4174 to a simple value. */
4175 (for op (plus minus)
4176 (simplify
4177 (op (convert @0) (convert @1))
4178 (if (INTEGRAL_TYPE_P (type)
4179 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
4180 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))
4181 && types_match (TREE_TYPE (@0), TREE_TYPE (@1))
4182 && !TYPE_OVERFLOW_TRAPS (type)
4183 && !TYPE_OVERFLOW_SANITIZED (type))
4184 (convert (op! @0 @1)))))
4185
4186 /* ~A + A -> -1 */
4187 (simplify
4188 (plus:c (convert? (bit_not @0)) (convert? @0))
4189 (if (!TYPE_OVERFLOW_TRAPS (type))
4190 (convert { build_all_ones_cst (TREE_TYPE (@0)); })))
4191
4192 /* ~A + 1 -> -A */
4193 (simplify
4194 (plus (convert? (bit_not @0)) integer_each_onep)
4195 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
4196 (negate (convert @0))))
4197
4198 /* -A - 1 -> ~A */
4199 (simplify
4200 (minus (convert? (negate @0)) integer_each_onep)
4201 (if (!TYPE_OVERFLOW_TRAPS (type)
4202 && TREE_CODE (type) != COMPLEX_TYPE
4203 && tree_nop_conversion_p (type, TREE_TYPE (@0)))
4204 (bit_not (convert @0))))
4205
4206 /* -1 - A -> ~A */
4207 (simplify
4208 (minus integer_all_onesp @0)
4209 (if (TREE_CODE (type) != COMPLEX_TYPE)
4210 (bit_not @0)))
4211
4212 /* (T)(P + A) - (T)P -> (T) A */
4213 (simplify
4214 (minus (convert (plus:c @@0 @1))
4215 (convert? @0))
4216 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
4217 /* For integer types, if A has a smaller type
4218 than T the result depends on the possible
4219 overflow in P + A.
4220 E.g. T=size_t, A=(unsigned)429497295, P>0.
4221 However, if an overflow in P + A would cause
4222 undefined behavior, we can assume that there
4223 is no overflow. */
4224 || (INTEGRAL_TYPE_P (TREE_TYPE (@1))
4225 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))))
4226 (convert @1)))
4227 (simplify
4228 (minus (convert (pointer_plus @@0 @1))
4229 (convert @0))
4230 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
4231 /* For pointer types, if the conversion of A to the
4232 final type requires a sign- or zero-extension,
4233 then we have to punt - it is not defined which
4234 one is correct. */
4235 || (POINTER_TYPE_P (TREE_TYPE (@0))
4236 && TREE_CODE (@1) == INTEGER_CST
4237 && tree_int_cst_sign_bit (@1) == 0))
4238 (convert @1)))
4239 (simplify
4240 (pointer_diff (pointer_plus @@0 @1) @0)
4241 /* The second argument of pointer_plus must be interpreted as signed, and
4242 thus sign-extended if necessary. */
4243 (with { tree stype = signed_type_for (TREE_TYPE (@1)); }
4244 /* Use view_convert instead of convert here, as POINTER_PLUS_EXPR
4245 second arg is unsigned even when we need to consider it as signed,
4246 we don't want to diagnose overflow here. */
4247 (convert (view_convert:stype @1))))
4248
4249 /* (T)P - (T)(P + A) -> -(T) A */
4250 (simplify
4251 (minus (convert? @0)
4252 (convert (plus:c @@0 @1)))
4253 (if (INTEGRAL_TYPE_P (type)
4254 && TYPE_OVERFLOW_UNDEFINED (type)
4255 /* For integer literals, using an intermediate unsigned type to avoid
4256 an overflow at run time is counter-productive because it introduces
4257 spurious overflows at compile time, in the form of TREE_OVERFLOW on
4258 the result, which may be problematic in GENERIC for some front-ends:
4259 (T)P - (T)(P + 4) -> (T)(-(U)4) -> (T)(4294967292) -> -4(OVF)
4260 so we use the direct path for them. */
4261 && TREE_CODE (@1) != INTEGER_CST
4262 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
4263 (with { tree utype = unsigned_type_for (type); }
4264 (convert (negate (convert:utype @1))))
4265 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
4266 /* For integer types, if A has a smaller type
4267 than T the result depends on the possible
4268 overflow in P + A.
4269 E.g. T=size_t, A=(unsigned)429497295, P>0.
4270 However, if an overflow in P + A would cause
4271 undefined behavior, we can assume that there
4272 is no overflow. */
4273 || (INTEGRAL_TYPE_P (TREE_TYPE (@1))
4274 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))))
4275 (negate (convert @1)))))
4276 (simplify
4277 (minus (convert @0)
4278 (convert (pointer_plus @@0 @1)))
4279 (if (INTEGRAL_TYPE_P (type)
4280 && TYPE_OVERFLOW_UNDEFINED (type)
4281 /* See above the rationale for this condition. */
4282 && TREE_CODE (@1) != INTEGER_CST
4283 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
4284 (with { tree utype = unsigned_type_for (type); }
4285 (convert (negate (convert:utype @1))))
4286 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
4287 /* For pointer types, if the conversion of A to the
4288 final type requires a sign- or zero-extension,
4289 then we have to punt - it is not defined which
4290 one is correct. */
4291 || (POINTER_TYPE_P (TREE_TYPE (@0))
4292 && TREE_CODE (@1) == INTEGER_CST
4293 && tree_int_cst_sign_bit (@1) == 0))
4294 (negate (convert @1)))))
4295 (simplify
4296 (pointer_diff @0 (pointer_plus @@0 @1))
4297 /* The second argument of pointer_plus must be interpreted as signed, and
4298 thus sign-extended if necessary. */
4299 (with { tree stype = signed_type_for (TREE_TYPE (@1)); }
4300 /* Use view_convert instead of convert here, as POINTER_PLUS_EXPR
4301 second arg is unsigned even when we need to consider it as signed,
4302 we don't want to diagnose overflow here. */
4303 (negate (convert (view_convert:stype @1)))))
4304
4305 /* (T)(P + A) - (T)(P + B) -> (T)A - (T)B */
4306 (simplify
4307 (minus (convert (plus:c @@0 @1))
4308 (convert (plus:c @0 @2)))
4309 (if (INTEGRAL_TYPE_P (type)
4310 && TYPE_OVERFLOW_UNDEFINED (type)
4311 && element_precision (type) <= element_precision (TREE_TYPE (@1))
4312 && element_precision (type) <= element_precision (TREE_TYPE (@2)))
4313 (with { tree utype = unsigned_type_for (type); }
4314 (convert (minus (convert:utype @1) (convert:utype @2))))
4315 (if (((element_precision (type) <= element_precision (TREE_TYPE (@1)))
4316 == (element_precision (type) <= element_precision (TREE_TYPE (@2))))
4317 && (element_precision (type) <= element_precision (TREE_TYPE (@1))
4318 /* For integer types, if A has a smaller type
4319 than T the result depends on the possible
4320 overflow in P + A.
4321 E.g. T=size_t, A=(unsigned)429497295, P>0.
4322 However, if an overflow in P + A would cause
4323 undefined behavior, we can assume that there
4324 is no overflow. */
4325 || (INTEGRAL_TYPE_P (TREE_TYPE (@1))
4326 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
4327 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))
4328 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@2)))))
4329 (minus (convert @1) (convert @2)))))
4330 (simplify
4331 (minus (convert (pointer_plus @@0 @1))
4332 (convert (pointer_plus @0 @2)))
4333 (if (INTEGRAL_TYPE_P (type)
4334 && TYPE_OVERFLOW_UNDEFINED (type)
4335 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
4336 (with { tree utype = unsigned_type_for (type); }
4337 (convert (minus (convert:utype @1) (convert:utype @2))))
4338 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
4339 /* For pointer types, if the conversion of A to the
4340 final type requires a sign- or zero-extension,
4341 then we have to punt - it is not defined which
4342 one is correct. */
4343 || (POINTER_TYPE_P (TREE_TYPE (@0))
4344 && TREE_CODE (@1) == INTEGER_CST
4345 && tree_int_cst_sign_bit (@1) == 0
4346 && TREE_CODE (@2) == INTEGER_CST
4347 && tree_int_cst_sign_bit (@2) == 0))
4348 (minus (convert @1) (convert @2)))))
4349 (simplify
4350 (pointer_diff (pointer_plus @0 @2) (pointer_plus @1 @2))
4351 (pointer_diff @0 @1))
4352 (simplify
4353 (pointer_diff (pointer_plus @@0 @1) (pointer_plus @0 @2))
4354 /* The second argument of pointer_plus must be interpreted as signed, and
4355 thus sign-extended if necessary. */
4356 (with { tree stype = signed_type_for (TREE_TYPE (@1)); }
4357 /* Use view_convert instead of convert here, as POINTER_PLUS_EXPR
4358 second arg is unsigned even when we need to consider it as signed,
4359 we don't want to diagnose overflow here. */
4360 (minus (convert (view_convert:stype @1))
4361 (convert (view_convert:stype @2)))))))
4362
4363 /* (A * C) +- (B * C) -> (A+-B) * C and (A * C) +- A -> A * (C+-1).
4364 Modeled after fold_plusminus_mult_expr. */
4365 (if (!TYPE_SATURATING (type)
4366 && (!FLOAT_TYPE_P (type) || flag_associative_math))
4367 (for plusminus (plus minus)
4368 (simplify
4369 (plusminus (mult:cs@3 @0 @1) (mult:cs@4 @0 @2))
4370 (if (!ANY_INTEGRAL_TYPE_P (type)
4371 || TYPE_OVERFLOW_WRAPS (type)
4372 || (INTEGRAL_TYPE_P (type)
4373 && tree_expr_nonzero_p (@0)
4374 && expr_not_equal_to (@0, wi::minus_one (TYPE_PRECISION (type)))))
4375 (if (single_use (@3) || single_use (@4))
4376 /* If @1 +- @2 is constant require a hard single-use on either
4377 original operand (but not on both). */
4378 (mult (plusminus @1 @2) @0)
4379 (mult! (plusminus @1 @2) @0)
4380 )))
4381 /* We cannot generate constant 1 for fract. */
4382 (if (!ALL_FRACT_MODE_P (TYPE_MODE (type)))
4383 (simplify
4384 (plusminus @0 (mult:c@3 @0 @2))
4385 (if ((!ANY_INTEGRAL_TYPE_P (type)
4386 || TYPE_OVERFLOW_WRAPS (type)
4387 /* For @0 + @0*@2 this transformation would introduce UB
4388 (where there was none before) for @0 in [-1,0] and @2 max.
4389 For @0 - @0*@2 this transformation would introduce UB
4390 for @0 0 and @2 in [min,min+1] or @0 -1 and @2 min+1. */
4391 || (INTEGRAL_TYPE_P (type)
4392 && ((tree_expr_nonzero_p (@0)
4393 && expr_not_equal_to (@0,
4394 wi::minus_one (TYPE_PRECISION (type))))
4395 || (plusminus == PLUS_EXPR
4396 ? expr_not_equal_to (@2,
4397 wi::max_value (TYPE_PRECISION (type), SIGNED))
4398 /* Let's ignore the @0 -1 and @2 min case. */
4399 : (expr_not_equal_to (@2,
4400 wi::min_value (TYPE_PRECISION (type), SIGNED))
4401 && expr_not_equal_to (@2,
4402 wi::min_value (TYPE_PRECISION (type), SIGNED)
4403 + 1))))))
4404 && single_use (@3))
4405 (mult (plusminus { build_one_cst (type); } @2) @0)))
4406 (simplify
4407 (plusminus (mult:c@3 @0 @2) @0)
4408 (if ((!ANY_INTEGRAL_TYPE_P (type)
4409 || TYPE_OVERFLOW_WRAPS (type)
4410 /* For @0*@2 + @0 this transformation would introduce UB
4411 (where there was none before) for @0 in [-1,0] and @2 max.
4412 For @0*@2 - @0 this transformation would introduce UB
4413 for @0 0 and @2 min. */
4414 || (INTEGRAL_TYPE_P (type)
4415 && ((tree_expr_nonzero_p (@0)
4416 && (plusminus == MINUS_EXPR
4417 || expr_not_equal_to (@0,
4418 wi::minus_one (TYPE_PRECISION (type)))))
4419 || expr_not_equal_to (@2,
4420 (plusminus == PLUS_EXPR
4421 ? wi::max_value (TYPE_PRECISION (type), SIGNED)
4422 : wi::min_value (TYPE_PRECISION (type), SIGNED))))))
4423 && single_use (@3))
4424 (mult (plusminus @2 { build_one_cst (type); }) @0)))))
4425 /* (A * B) + (-C) -> (B - C/A) * A, if C is a multiple of A. */
4426 (if (!ALL_FRACT_MODE_P (TYPE_MODE (type)))
4427 (simplify
4428 (plus (mult:cs integer_nonzerop@0 @1) INTEGER_CST@2)
4429 /* Exclude the case that @2 == min to prevent UB when calculating abs
4430 and (B - C/A). */
4431 (if (TREE_CODE (type) == INTEGER_TYPE
4432 && wi::neg_p (wi::to_wide (@2))
4433 && wi::to_wide (@2) != wi::min_value (TYPE_PRECISION (type), SIGNED))
4434 (with {
4435 wide_int c0 = wi::to_wide (@0);
4436 wide_int c2 = wi::to_wide (@2);
4437 wide_int c2_abs = wi::abs (c2); }
4438 (if (wi::multiple_of_p (c2_abs, c0, TYPE_SIGN (type)))
4439 (with {
4440 /* Calculate @2 / @0 in order to factorize the expression. */
4441 wide_int div_res = wi::sdiv_trunc (c2, c0);
4442 tree div_cst = wide_int_to_tree (type, div_res); }
4443 (mult (plus @1 { div_cst; }) @0))))))))
4444
4445 #if GIMPLE
4446 /* Canonicalize X + (X << C) into X * (1 + (1 << C)) and
4447 (X << C1) + (X << C2) into X * ((1 << C1) + (1 << C2)). */
4448 (simplify
4449 (plus:c @0 (lshift:s @0 INTEGER_CST@1))
4450 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
4451 && tree_fits_uhwi_p (@1)
4452 && tree_to_uhwi (@1) < element_precision (type)
4453 && (INTEGRAL_TYPE_P (TREE_TYPE (@0))
4454 || optab_handler (smul_optab,
4455 TYPE_MODE (type)) != CODE_FOR_nothing))
4456 (with { tree t = type;
4457 if (!TYPE_OVERFLOW_WRAPS (t)) t = unsigned_type_for (t);
4458 wide_int w = wi::set_bit_in_zero (tree_to_uhwi (@1),
4459 element_precision (type));
4460 w += 1;
4461 tree cst = wide_int_to_tree (VECTOR_TYPE_P (t) ? TREE_TYPE (t)
4462 : t, w);
4463 cst = build_uniform_cst (t, cst); }
4464 (convert (mult (convert:t @0) { cst; })))))
4465 (simplify
4466 (plus (lshift:s @0 INTEGER_CST@1) (lshift:s @0 INTEGER_CST@2))
4467 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
4468 && tree_fits_uhwi_p (@1)
4469 && tree_to_uhwi (@1) < element_precision (type)
4470 && tree_fits_uhwi_p (@2)
4471 && tree_to_uhwi (@2) < element_precision (type)
4472 && (INTEGRAL_TYPE_P (TREE_TYPE (@0))
4473 || optab_handler (smul_optab,
4474 TYPE_MODE (type)) != CODE_FOR_nothing))
4475 (with { tree t = type;
4476 if (!TYPE_OVERFLOW_WRAPS (t)) t = unsigned_type_for (t);
4477 unsigned int prec = element_precision (type);
4478 wide_int w = wi::set_bit_in_zero (tree_to_uhwi (@1), prec);
4479 w += wi::set_bit_in_zero (tree_to_uhwi (@2), prec);
4480 tree cst = wide_int_to_tree (VECTOR_TYPE_P (t) ? TREE_TYPE (t)
4481 : t, w);
4482 cst = build_uniform_cst (t, cst); }
4483 (convert (mult (convert:t @0) { cst; })))))
4484 #endif
4485
4486 /* Canonicalize (X*C1)|(X*C2) and (X*C1)^(X*C2) to (C1+C2)*X when
4487 tree_nonzero_bits allows IOR and XOR to be treated like PLUS.
4488 Likewise, handle (X<<C3) and X as legitimate variants of X*C. */
4489 (for op (bit_ior bit_xor)
4490 (simplify
4491 (op (nop_convert?:s@6 (mult:s@0 (nop_convert? @1) INTEGER_CST@2))
4492 (nop_convert?:s@7 (mult:s@3 (nop_convert? @5) INTEGER_CST@4)))
4493 (if (INTEGRAL_TYPE_P (type)
4494 && operand_equal_p (@1, @5, 0)
4495 && (tree_nonzero_bits (@6) & tree_nonzero_bits (@7)) == 0)
4496 (with { tree t = type;
4497 if (!TYPE_OVERFLOW_WRAPS (t))
4498 t = unsigned_type_for (t);
4499 wide_int c = wi::add (wi::to_wide (@2), wi::to_wide (@4)); }
4500 (convert (mult:t (convert:t @1) { wide_int_to_tree (t, c); })))))
4501 (simplify
4502 (op:c (nop_convert?:s@5 (mult:s@0 (nop_convert? @1) INTEGER_CST@2))
4503 (lshift:s@3 @1 INTEGER_CST@4))
4504 (if (INTEGRAL_TYPE_P (type)
4505 && tree_int_cst_sgn (@4) > 0
4506 && (tree_nonzero_bits (@5) & tree_nonzero_bits (@3)) == 0)
4507 (with { tree t = type;
4508 if (!TYPE_OVERFLOW_WRAPS (t))
4509 t = unsigned_type_for (t);
4510 wide_int wone = wi::one (TYPE_PRECISION (type));
4511 wide_int c = wi::add (wi::to_wide (@2),
4512 wi::lshift (wone, wi::to_wide (@4))); }
4513 (convert (mult:t (convert:t @1) { wide_int_to_tree (t, c); })))))
4514 (simplify
4515 (op:c (nop_convert?:s@3 (mult:s@0 (nop_convert? @1) INTEGER_CST@2))
4516 @1)
4517 (if (INTEGRAL_TYPE_P (type)
4518 && (tree_nonzero_bits (@3) & tree_nonzero_bits (@1)) == 0)
4519 (with { tree t = type;
4520 if (!TYPE_OVERFLOW_WRAPS (t))
4521 t = unsigned_type_for (t);
4522 wide_int c = wi::add (wi::to_wide (@2), 1); }
4523 (convert (mult:t (convert:t @1) { wide_int_to_tree (t, c); })))))
4524 (simplify
4525 (op (lshift:s@0 @1 INTEGER_CST@2)
4526 (lshift:s@3 @1 INTEGER_CST@4))
4527 (if (INTEGRAL_TYPE_P (type)
4528 && tree_int_cst_sgn (@2) > 0
4529 && tree_int_cst_sgn (@4) > 0
4530 && (tree_nonzero_bits (@0) & tree_nonzero_bits (@3)) == 0)
4531 (with { tree t = type;
4532 if (!TYPE_OVERFLOW_WRAPS (t))
4533 t = unsigned_type_for (t);
4534 wide_int wone = wi::one (TYPE_PRECISION (t));
4535 wide_int c = wi::add (wi::lshift (wone, wi::to_wide (@2)),
4536 wi::lshift (wone, wi::to_wide (@4))); }
4537 (convert (mult:t (convert:t @1) { wide_int_to_tree (t,c); })))))
4538 (simplify
4539 (op:c (lshift:s@0 @1 INTEGER_CST@2)
4540 @1)
4541 (if (INTEGRAL_TYPE_P (type)
4542 && tree_int_cst_sgn (@2) > 0
4543 && (tree_nonzero_bits (@0) & tree_nonzero_bits (@1)) == 0)
4544 (with { tree t = type;
4545 if (!TYPE_OVERFLOW_WRAPS (t))
4546 t = unsigned_type_for (t);
4547 wide_int wone = wi::one (TYPE_PRECISION (t));
4548 wide_int c = wi::add (wi::lshift (wone, wi::to_wide (@2)), wone); }
4549 (convert (mult:t (convert:t @1) { wide_int_to_tree (t, c); }))))))
4550
4551 /* Simplifications of MIN_EXPR, MAX_EXPR, fmin() and fmax(). */
4552
4553 (for minmax (min max)
4554 (simplify
4555 (minmax @0 @0)
4556 @0)
4557 /* max(max(x,y),x) -> max(x,y) */
4558 (simplify
4559 (minmax:c (minmax:c@2 @0 @1) @0)
4560 @2))
4561 /* For fmin() and fmax(), skip folding when both are sNaN. */
4562 (for minmax (FMIN_ALL FMAX_ALL)
4563 (simplify
4564 (minmax @0 @0)
4565 (if (!tree_expr_maybe_signaling_nan_p (@0))
4566 @0)))
4567 /* min(max(x,y),y) -> y. */
4568 (simplify
4569 (min:c (max:c @0 @1) @1)
4570 @1)
4571 /* max(min(x,y),y) -> y. */
4572 (simplify
4573 (max:c (min:c @0 @1) @1)
4574 @1)
4575 /* max(a,-a) -> abs(a). */
4576 (simplify
4577 (max:c @0 (negate @0))
4578 (if (TREE_CODE (type) != COMPLEX_TYPE
4579 && (! ANY_INTEGRAL_TYPE_P (type)
4580 || TYPE_OVERFLOW_UNDEFINED (type)))
4581 (abs @0)))
4582 /* min(a,-a) -> -abs(a). */
4583 (simplify
4584 (min:c @0 (negate @0))
4585 (if (TREE_CODE (type) != COMPLEX_TYPE
4586 && (! ANY_INTEGRAL_TYPE_P (type)
4587 || TYPE_OVERFLOW_UNDEFINED (type)))
4588 (negate (abs @0))))
4589 (simplify
4590 (min @0 @1)
4591 (switch
4592 (if (INTEGRAL_TYPE_P (type)
4593 && TYPE_MIN_VALUE (type)
4594 && operand_equal_p (@1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
4595 @1)
4596 (if (INTEGRAL_TYPE_P (type)
4597 && TYPE_MAX_VALUE (type)
4598 && operand_equal_p (@1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
4599 @0)))
4600 (simplify
4601 (max @0 @1)
4602 (switch
4603 (if (INTEGRAL_TYPE_P (type)
4604 && TYPE_MAX_VALUE (type)
4605 && operand_equal_p (@1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
4606 @1)
4607 (if (INTEGRAL_TYPE_P (type)
4608 && TYPE_MIN_VALUE (type)
4609 && operand_equal_p (@1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
4610 @0)))
4611
4612 /* max (a, a + CST) -> a + CST where CST is positive. */
4613 /* max (a, a + CST) -> a where CST is negative. */
4614 (simplify
4615 (max:c @0 (plus@2 @0 INTEGER_CST@1))
4616 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
4617 (if (tree_int_cst_sgn (@1) > 0)
4618 @2
4619 @0)))
4620
4621 /* min (a, a + CST) -> a where CST is positive. */
4622 /* min (a, a + CST) -> a + CST where CST is negative. */
4623 (simplify
4624 (min:c @0 (plus@2 @0 INTEGER_CST@1))
4625 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
4626 (if (tree_int_cst_sgn (@1) > 0)
4627 @0
4628 @2)))
4629
4630 /* min (a, b) op max (a, b) -> a op b */
4631 (for op (plus mult bit_and bit_xor bit_ior eq ne min max)
4632 (simplify
4633 (op:c (min:c @0 @1) (max @0 @1))
4634 (if (!HONOR_NANS (@0))
4635 (op @0 @1))))
4636
4637 /* Simplify min (&var[off0], &var[off1]) etc. depending on whether
4638 the addresses are known to be less, equal or greater. */
4639 (for minmax (min max)
4640 cmp (lt gt)
4641 (simplify
4642 (minmax (convert1?@2 addr@0) (convert2?@3 addr@1))
4643 (with
4644 {
4645 poly_int64 off0, off1;
4646 tree base0, base1;
4647 int equal = address_compare (cmp, TREE_TYPE (@2), @0, @1, base0, base1,
4648 off0, off1, GENERIC);
4649 }
4650 (if (equal == 1)
4651 (if (minmax == MIN_EXPR)
4652 (if (known_le (off0, off1))
4653 @2
4654 (if (known_gt (off0, off1))
4655 @3))
4656 (if (known_ge (off0, off1))
4657 @2
4658 (if (known_lt (off0, off1))
4659 @3)))))))
4660
4661 /* (convert (minmax ((convert (x) c)))) -> minmax (x c) if x is promoted
4662 and the outer convert demotes the expression back to x's type. */
4663 (for minmax (min max)
4664 (simplify
4665 (convert (minmax@0 (convert @1) INTEGER_CST@2))
4666 (if (INTEGRAL_TYPE_P (type)
4667 && types_match (@1, type) && int_fits_type_p (@2, type)
4668 && TYPE_SIGN (TREE_TYPE (@0)) == TYPE_SIGN (type)
4669 && TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type))
4670 (minmax @1 (convert @2)))))
4671
4672 (for minmax (FMIN_ALL FMAX_ALL)
4673 /* If either argument is NaN and other one is not sNaN, return the other
4674 one. Avoid the transformation if we get (and honor) a signalling NaN. */
4675 (simplify
4676 (minmax:C @0 REAL_CST@1)
4677 (if (real_isnan (TREE_REAL_CST_PTR (@1))
4678 && (!HONOR_SNANS (@1) || !TREE_REAL_CST (@1).signalling)
4679 && !tree_expr_maybe_signaling_nan_p (@0))
4680 @0)))
4681 /* Convert fmin/fmax to MIN_EXPR/MAX_EXPR. C99 requires these
4682 functions to return the numeric arg if the other one is NaN.
4683 MIN and MAX don't honor that, so only transform if -ffinite-math-only
4684 is set. C99 doesn't require -0.0 to be handled, so we don't have to
4685 worry about it either. */
4686 (if (flag_finite_math_only)
4687 (simplify
4688 (FMIN_ALL @0 @1)
4689 (min @0 @1))
4690 (simplify
4691 (FMAX_ALL @0 @1)
4692 (max @0 @1)))
4693 /* min (-A, -B) -> -max (A, B) */
4694 (for minmax (min max FMIN_ALL FMAX_ALL)
4695 maxmin (max min FMAX_ALL FMIN_ALL)
4696 (simplify
4697 (minmax (negate:s@2 @0) (negate:s@3 @1))
4698 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
4699 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
4700 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
4701 (negate (maxmin @0 @1)))))
4702 /* MIN (~X, ~Y) -> ~MAX (X, Y)
4703 MAX (~X, ~Y) -> ~MIN (X, Y) */
4704 (for minmax (min max)
4705 maxmin (max min)
4706 (simplify
4707 (minmax (bit_not:s@2 @0) (bit_not:s@3 @1))
4708 (bit_not (maxmin @0 @1)))
4709 /* ~MAX(~X, Y) --> MIN(X, ~Y) */
4710 /* ~MIN(~X, Y) --> MAX(X, ~Y) */
4711 (simplify
4712 (bit_not (minmax:cs (bit_not @0) @1))
4713 (maxmin @0 (bit_not @1))))
4714
4715 /* MIN (X, Y) == X -> X <= Y */
4716 /* MIN (X, Y) < X -> X > Y */
4717 /* MIN (X, Y) >= X -> X <= Y */
4718 (for minmax (min min min min max max max max)
4719 cmp (eq ne lt ge eq ne gt le )
4720 out (le gt gt le ge lt lt ge )
4721 (simplify
4722 (cmp:c (minmax:c @0 @1) @0)
4723 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
4724 && (!VECTOR_TYPE_P (TREE_TYPE (@0))
4725 || (VECTOR_TYPE_P (type)
4726 && (!expand_vec_cmp_expr_p (TREE_TYPE (@0), type, cmp)
4727 || expand_vec_cmp_expr_p (TREE_TYPE (@0), type, out)))))
4728 (out @0 @1))))
4729 /* MIN (X, 5) == 0 -> X == 0
4730 MIN (X, 5) == 7 -> false */
4731 (for cmp (eq ne)
4732 (simplify
4733 (cmp (min @0 INTEGER_CST@1) INTEGER_CST@2)
4734 (if (wi::lt_p (wi::to_wide (@1), wi::to_wide (@2),
4735 TYPE_SIGN (TREE_TYPE (@0))))
4736 { constant_boolean_node (cmp == NE_EXPR, type); }
4737 (if (wi::gt_p (wi::to_wide (@1), wi::to_wide (@2),
4738 TYPE_SIGN (TREE_TYPE (@0))))
4739 (cmp @0 @2)))))
4740 (for cmp (eq ne)
4741 (simplify
4742 (cmp (max @0 INTEGER_CST@1) INTEGER_CST@2)
4743 (if (wi::gt_p (wi::to_wide (@1), wi::to_wide (@2),
4744 TYPE_SIGN (TREE_TYPE (@0))))
4745 { constant_boolean_node (cmp == NE_EXPR, type); }
4746 (if (wi::lt_p (wi::to_wide (@1), wi::to_wide (@2),
4747 TYPE_SIGN (TREE_TYPE (@0))))
4748 (cmp @0 @2)))))
4749
4750 /* X <= MAX(X, Y) -> true
4751 X > MAX(X, Y) -> false
4752 X >= MIN(X, Y) -> true
4753 X < MIN(X, Y) -> false */
4754 (for minmax (min min max max )
4755 cmp (ge lt le gt )
4756 (simplify
4757 (cmp:c @0 (minmax:c @0 @1))
4758 { constant_boolean_node (cmp == GE_EXPR || cmp == LE_EXPR, type); } ))
4759
4760 /* MIN (X, C1) < C2 -> X < C2 || C1 < C2 */
4761 (for minmax (min min max max min min max max )
4762 cmp (lt le gt ge gt ge lt le )
4763 comb (bit_ior bit_ior bit_ior bit_ior bit_and bit_and bit_and bit_and)
4764 (simplify
4765 (cmp (minmax @0 INTEGER_CST@1) INTEGER_CST@2)
4766 (comb (cmp @0 @2) (cmp @1 @2))))
4767
4768 /* MAX (A, B) == 0 -> (A|B) == 0 iff unsigned.
4769 MAX (A, B) != 0 -> (A|B) != 0 iff unsigned. */
4770 (for cmp (eq ne)
4771 (simplify
4772 (cmp (max @0 @1) integer_zerop)
4773 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
4774 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
4775 (cmp (bit_ior (convert:utype @0) (convert:utype @1))
4776 { build_zero_cst (utype); } )))))
4777
4778 /* Undo fancy ways of writing max/min or other ?: expressions, like
4779 a - ((a - b) & -(a < b)) and a - (a - b) * (a < b) into (a < b) ? b : a.
4780 People normally use ?: and that is what we actually try to optimize. */
4781 /* Transform A + (B-A)*cmp into cmp ? B : A. */
4782 (simplify
4783 (plus:c @0 (mult:c (minus @1 @0) zero_one_valued_p@2))
4784 (if (INTEGRAL_TYPE_P (type)
4785 && (GIMPLE || !TREE_SIDE_EFFECTS (@1)))
4786 (cond (convert:boolean_type_node @2) @1 @0)))
4787 /* Transform A - (A-B)*cmp into cmp ? B : A. */
4788 (simplify
4789 (minus @0 (mult:c (minus @0 @1) zero_one_valued_p@2))
4790 (if (INTEGRAL_TYPE_P (type)
4791 && (GIMPLE || !TREE_SIDE_EFFECTS (@1)))
4792 (cond (convert:boolean_type_node @2) @1 @0)))
4793 /* Transform A ^ (A^B)*cmp into cmp ? B : A. */
4794 (simplify
4795 (bit_xor:c @0 (mult:c (bit_xor:c @0 @1) zero_one_valued_p@2))
4796 (if (INTEGRAL_TYPE_P (type)
4797 && (GIMPLE || !TREE_SIDE_EFFECTS (@1)))
4798 (cond (convert:boolean_type_node @2) @1 @0)))
4799
4800 /* (x <= 0 ? -x : 0) -> max(-x, 0). */
4801 (simplify
4802 (cond (le @0 integer_zerop@1) (negate@2 @0) integer_zerop@1)
4803 (if (ANY_INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_UNDEFINED (type))
4804 (max @2 @1)))
4805
4806 /* (zero_one == 0) ? y : z <op> y -> ((typeof(y))zero_one * z) <op> y */
4807 (for op (bit_xor bit_ior plus)
4808 (simplify
4809 (cond (eq zero_one_valued_p@0
4810 integer_zerop)
4811 @1
4812 (op:c @2 @1))
4813 (if (INTEGRAL_TYPE_P (type)
4814 && TYPE_PRECISION (type) > 1
4815 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
4816 && expr_no_side_effects_p (@2))
4817 (op (mult (convert:type @0) @2) @1))))
4818
4819 /* (zero_one != 0) ? z <op> y : y -> ((typeof(y))zero_one * z) <op> y */
4820 (for op (bit_xor bit_ior plus)
4821 (simplify
4822 (cond (ne zero_one_valued_p@0
4823 integer_zerop)
4824 (op:c @2 @1)
4825 @1)
4826 (if (INTEGRAL_TYPE_P (type)
4827 && TYPE_PRECISION (type) > 1
4828 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
4829 && expr_no_side_effects_p (@2))
4830 (op (mult (convert:type @0) @2) @1))))
4831
4832 /* ?: Value replacement. */
4833 /* a == 0 ? b : b + a -> b + a */
4834 (for op (plus bit_ior bit_xor)
4835 (simplify
4836 (cond (eq @0 integer_zerop) @1 (op:c@2 @1 @0))
4837 @2))
4838 /* a == 0 ? b : b - a -> b - a */
4839 /* a == 0 ? b : b ptr+ a -> b ptr+ a */
4840 /* a == 0 ? b : b shift/rotate a -> b shift/rotate a */
4841 (for op (lrotate rrotate lshift rshift minus pointer_plus)
4842 (simplify
4843 (cond (eq @0 integer_zerop) @1 (op@2 @1 @0))
4844 @2))
4845
4846 /* a == 1 ? b : b / a -> b / a */
4847 (for op (trunc_div ceil_div floor_div round_div exact_div)
4848 (simplify
4849 (cond (eq @0 integer_onep) @1 (op@2 @1 @0))
4850 @2))
4851
4852 /* a == 1 ? b : a * b -> a * b */
4853 (for op (mult)
4854 (simplify
4855 (cond (eq @0 integer_onep) @1 (op:c@2 @1 @0))
4856 @2))
4857
4858 /* a == -1 ? b : a & b -> a & b */
4859 (for op (bit_and)
4860 (simplify
4861 (cond (eq @0 integer_all_onesp) @1 (op:c@2 @1 @0))
4862 @2))
4863
4864 /* a != 0 ? a / b : 0 -> a / b iff b is nonzero. */
4865 (for op (trunc_div ceil_div floor_div round_div exact_div)
4866 (simplify
4867 (cond (ne @0 integer_zerop) (op@2 @3 @1) integer_zerop )
4868 (if (bitwise_equal_p (@0, @3)
4869 && tree_expr_nonzero_p (@1)
4870 /* Cannot make a expression with side effects
4871 unconditional. */
4872 && expr_no_side_effects_p (@3))
4873 @2)))
4874
4875 /* Note we prefer the != case here
4876 as (a != 0) * (a * b) will generate that version. */
4877 /* a != 0 ? a * b : 0 -> a * b */
4878 /* a != 0 ? a & b : 0 -> a & b */
4879 (for op (mult bit_and)
4880 (simplify
4881 (cond (ne @0 integer_zerop) (op:c@2 @1 @3) integer_zerop)
4882 (if (bitwise_equal_p (@0, @3)
4883 /* Cannot make a expression with side effects
4884 unconditional. */
4885 && expr_no_side_effects_p (@1))
4886 @2)))
4887
4888 /* Simplifications of shift and rotates. */
4889
4890 (for rotate (lrotate rrotate)
4891 (simplify
4892 (rotate integer_all_onesp@0 @1)
4893 @0))
4894
4895 /* Optimize -1 >> x for arithmetic right shifts. */
4896 (simplify
4897 (rshift integer_all_onesp@0 @1)
4898 (if (!TYPE_UNSIGNED (type))
4899 @0))
4900
4901 /* Optimize (x >> c) << c into x & (-1<<c). */
4902 (simplify
4903 (lshift (nop_convert? (rshift @0 INTEGER_CST@1)) @1)
4904 (if (wi::ltu_p (wi::to_wide (@1), element_precision (type)))
4905 /* It doesn't matter if the right shift is arithmetic or logical. */
4906 (bit_and (view_convert @0) (lshift { build_minus_one_cst (type); } @1))))
4907
4908 (simplify
4909 (lshift (convert (convert@2 (rshift @0 INTEGER_CST@1))) @1)
4910 (if (wi::ltu_p (wi::to_wide (@1), element_precision (type))
4911 /* Allow intermediate conversion to integral type with whatever sign, as
4912 long as the low TYPE_PRECISION (type)
4913 - TYPE_PRECISION (TREE_TYPE (@2)) bits are preserved. */
4914 && INTEGRAL_TYPE_P (type)
4915 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
4916 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
4917 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (@0))
4918 && (TYPE_PRECISION (TREE_TYPE (@2)) >= TYPE_PRECISION (type)
4919 || wi::geu_p (wi::to_wide (@1),
4920 TYPE_PRECISION (type)
4921 - TYPE_PRECISION (TREE_TYPE (@2)))))
4922 (bit_and (convert @0) (lshift { build_minus_one_cst (type); } @1))))
4923
4924 #if GIMPLE
4925 /* (X >> C1) << (C1 + C2) -> X << C2 if the low C1 bits of X are zero. */
4926 (simplify
4927 (lshift (convert? (rshift with_possible_nonzero_bits@0 INTEGER_CST@1))
4928 INTEGER_CST@2)
4929 (if (INTEGRAL_TYPE_P (type)
4930 && wi::ltu_p (wi::to_wide (@1), element_precision (type))
4931 && wi::ltu_p (wi::to_wide (@2), element_precision (type))
4932 && wi::to_widest (@2) >= wi::to_widest (@1)
4933 && wi::to_widest (@1) <= wi::ctz (get_nonzero_bits (@0)))
4934 (lshift (convert @0) (minus @2 @1))))
4935
4936 /* (X >> C1) * (C2 << C1) -> X * C2 if the low C1 bits of X are zero. */
4937 (simplify
4938 (mult (convert? (rshift with_possible_nonzero_bits@0 INTEGER_CST@1))
4939 poly_int_tree_p@2)
4940 (with { poly_widest_int factor; }
4941 (if (INTEGRAL_TYPE_P (type)
4942 && wi::ltu_p (wi::to_wide (@1), element_precision (type))
4943 && wi::to_widest (@1) <= wi::ctz (get_nonzero_bits (@0))
4944 && multiple_p (wi::to_poly_widest (@2),
4945 widest_int (1) << tree_to_uhwi (@1),
4946 &factor))
4947 (mult (convert @0) { wide_int_to_tree (type, factor); }))))
4948 #endif
4949
4950 /* For (x << c) >> c, optimize into x & ((unsigned)-1 >> c) for
4951 unsigned x OR truncate into the precision(type) - c lowest bits
4952 of signed x (if they have mode precision or a precision of 1). */
4953 (simplify
4954 (rshift (nop_convert? (lshift @0 INTEGER_CST@1)) @@1)
4955 (if (wi::ltu_p (wi::to_wide (@1), element_precision (type)))
4956 (if (TYPE_UNSIGNED (type))
4957 (bit_and (convert @0) (rshift { build_minus_one_cst (type); } @1))
4958 (if (INTEGRAL_TYPE_P (type))
4959 (with {
4960 int width = element_precision (type) - tree_to_uhwi (@1);
4961 tree stype = NULL_TREE;
4962 if (width <= MAX_FIXED_MODE_SIZE)
4963 stype = build_nonstandard_integer_type (width, 0);
4964 }
4965 (if (stype && (width == 1 || type_has_mode_precision_p (stype)))
4966 (convert (convert:stype @0))))))))
4967
4968 /* Optimize x >> x into 0 */
4969 (simplify
4970 (rshift @0 @0)
4971 { build_zero_cst (type); })
4972
4973 (for shiftrotate (lrotate rrotate lshift rshift)
4974 (simplify
4975 (shiftrotate @0 integer_zerop)
4976 (non_lvalue @0))
4977 (simplify
4978 (shiftrotate integer_zerop@0 @1)
4979 @0)
4980 /* Prefer vector1 << scalar to vector1 << vector2
4981 if vector2 is uniform. */
4982 (for vec (VECTOR_CST CONSTRUCTOR)
4983 (simplify
4984 (shiftrotate @0 vec@1)
4985 (with { tree tem = uniform_vector_p (@1); }
4986 (if (tem)
4987 (shiftrotate @0 { tem; }))))))
4988
4989 /* Simplify X << Y where Y's low width bits are 0 to X, as only valid
4990 Y is 0. Similarly for X >> Y. */
4991 #if GIMPLE
4992 (for shift (lshift rshift)
4993 (simplify
4994 (shift @0 SSA_NAME@1)
4995 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1)))
4996 (with {
4997 int width = ceil_log2 (element_precision (TREE_TYPE (@0)));
4998 int prec = TYPE_PRECISION (TREE_TYPE (@1));
4999 }
5000 (if ((get_nonzero_bits (@1) & wi::mask (width, false, prec)) == 0)
5001 @0)))))
5002 #endif
5003
5004 /* Rewrite an LROTATE_EXPR by a constant into an
5005 RROTATE_EXPR by a new constant. */
5006 (simplify
5007 (lrotate @0 INTEGER_CST@1)
5008 (rrotate @0 { const_binop (MINUS_EXPR, TREE_TYPE (@1),
5009 build_int_cst (TREE_TYPE (@1),
5010 element_precision (type)), @1); }))
5011
5012 /* a rrotate (bitsize-b) -> a lrotate b */
5013 /* a lrotate (bitsize-b) -> a rrotate b */
5014 /* Only do the above when it is known that b != bitsize.
5015 Otherwise canonicalize to a orotate (b & mask) when the subtraction
5016 has single use and prec is a power of two. */
5017 (for rotate (lrotate rrotate)
5018 orotate (rrotate lrotate)
5019 (simplify
5020 (rotate @0 (minus@3 INTEGER_CST@1 @2))
5021 (with { auto prec = element_precision (TREE_TYPE (@0)); }
5022 (if (prec == wi::to_wide (@1))
5023 (switch
5024 (if (expr_not_equal_to (@2, wi::uhwi (prec,
5025 TYPE_PRECISION (TREE_TYPE (@2)))))
5026 (orotate @0 @2))
5027 (if (single_use (@3)
5028 && pow2p_hwi (prec)
5029 /* Defer this case until last forwprop, so that VRP could be run and
5030 expr_not_equal_to had a chance to match. Otherwise we'd do
5031 pretty much always just the second case. */
5032 && cfun
5033 && ((cfun->curr_properties & PROP_last_full_fold) != 0
5034 || !flag_tree_vrp
5035 || optimize_debug))
5036 (orotate @0
5037 (bit_and @2 { build_int_cst (TREE_TYPE (@2), prec - 1); }))))))))
5038
5039 /* Turn (a OP c1) OP c2 into a OP (c1+c2). */
5040 (for op (lrotate rrotate rshift lshift)
5041 (simplify
5042 (op (op @0 INTEGER_CST@1) INTEGER_CST@2)
5043 (with { unsigned int prec = element_precision (type); }
5044 (if (wi::ge_p (wi::to_wide (@1), 0, TYPE_SIGN (TREE_TYPE (@1)))
5045 && wi::lt_p (wi::to_wide (@1), prec, TYPE_SIGN (TREE_TYPE (@1)))
5046 && wi::ge_p (wi::to_wide (@2), 0, TYPE_SIGN (TREE_TYPE (@2)))
5047 && wi::lt_p (wi::to_wide (@2), prec, TYPE_SIGN (TREE_TYPE (@2))))
5048 (with { unsigned int low = (tree_to_uhwi (@1)
5049 + tree_to_uhwi (@2)); }
5050 /* Deal with a OP (c1 + c2) being undefined but (a OP c1) OP c2
5051 being well defined. */
5052 (if (low >= prec)
5053 (if (op == LROTATE_EXPR || op == RROTATE_EXPR)
5054 (op @0 { build_int_cst (TREE_TYPE (@1), low % prec); })
5055 (if (TYPE_UNSIGNED (type) || op == LSHIFT_EXPR)
5056 { build_zero_cst (type); }
5057 (op @0 { build_int_cst (TREE_TYPE (@1), prec - 1); })))
5058 (op @0 { build_int_cst (TREE_TYPE (@1), low); })))))))
5059
5060 /* Fold `1 >> a` into `a == 0` for scalar integral types. */
5061 (simplify
5062 (rshift integer_onep @2)
5063 (if (INTEGRAL_TYPE_P (type))
5064 (convert (eq:boolean_type_node @2 { build_zero_cst (TREE_TYPE (@2)); }))))
5065
5066 /* Simplify (CST << x) & 1 to 0 if CST is even or to x == 0 if it is odd. */
5067 (simplify
5068 (bit_and (lshift INTEGER_CST@1 @0) integer_onep)
5069 (if ((wi::to_wide (@1) & 1) != 0)
5070 (convert (eq:boolean_type_node @0 { build_zero_cst (TREE_TYPE (@0)); }))
5071 { build_zero_cst (type); }))
5072
5073 /* Simplify ((C << x) & D) != 0 where C and D are power of two constants,
5074 either to false if D is smaller (unsigned comparison) than C, or to
5075 x == log2 (D) - log2 (C). Similarly for right shifts.
5076 Note `(1 >> x)`, the & 1 has been removed so but will have been
5077 already folded above. */
5078 (for cmp (ne eq)
5079 icmp (eq ne)
5080 (simplify
5081 (cmp (bit_and (lshift integer_pow2p@1 @0) integer_pow2p@2) integer_zerop)
5082 (with { int c1 = wi::clz (wi::to_wide (@1));
5083 int c2 = wi::clz (wi::to_wide (@2)); }
5084 (if (c1 < c2)
5085 { constant_boolean_node (cmp == NE_EXPR ? false : true, type); }
5086 (icmp @0 { build_int_cst (TREE_TYPE (@0), c1 - c2); }))))
5087 (simplify
5088 (cmp (bit_and (rshift integer_pow2p@1 @0) integer_pow2p@2) integer_zerop)
5089 (if (tree_int_cst_sgn (@1) > 0)
5090 (with { int c1 = wi::clz (wi::to_wide (@1));
5091 int c2 = wi::clz (wi::to_wide (@2)); }
5092 (if (c1 > c2)
5093 { constant_boolean_node (cmp == NE_EXPR ? false : true, type); }
5094 (icmp @0 { build_int_cst (TREE_TYPE (@0), c2 - c1); }))))))
5095
5096 /* (CST1 << A) == CST2 -> A == ctz (CST2) - ctz (CST1)
5097 (CST1 << A) != CST2 -> A != ctz (CST2) - ctz (CST1)
5098 if CST2 != 0. */
5099 (for cmp (ne eq)
5100 (simplify
5101 (cmp (lshift INTEGER_CST@0 @1) INTEGER_CST@2)
5102 (with { int cand = wi::ctz (wi::to_wide (@2)) - wi::ctz (wi::to_wide (@0)); }
5103 (if (cand < 0
5104 || (!integer_zerop (@2)
5105 && wi::lshift (wi::to_wide (@0), cand) != wi::to_wide (@2)))
5106 { constant_boolean_node (cmp == NE_EXPR, type); }
5107 (if (!integer_zerop (@2)
5108 && wi::lshift (wi::to_wide (@0), cand) == wi::to_wide (@2))
5109 (cmp @1 { build_int_cst (TREE_TYPE (@1), cand); }))))))
5110
5111 /* Fold ((X << C1) & C2) cmp C3 into (X & (C2 >> C1)) cmp (C3 >> C1)
5112 ((X >> C1) & C2) cmp C3 into (X & (C2 << C1)) cmp (C3 << C1). */
5113 (for cmp (ne eq)
5114 (simplify
5115 (cmp (bit_and:s (lshift:s @0 INTEGER_CST@1) INTEGER_CST@2) INTEGER_CST@3)
5116 (if (tree_fits_shwi_p (@1)
5117 && tree_to_shwi (@1) > 0
5118 && tree_to_shwi (@1) < TYPE_PRECISION (TREE_TYPE (@0)))
5119 (if (tree_to_shwi (@1) > wi::ctz (wi::to_wide (@3)))
5120 { constant_boolean_node (cmp == NE_EXPR, type); }
5121 (with { wide_int c1 = wi::to_wide (@1);
5122 wide_int c2 = wi::lrshift (wi::to_wide (@2), c1);
5123 wide_int c3 = wi::lrshift (wi::to_wide (@3), c1); }
5124 (cmp (bit_and @0 { wide_int_to_tree (TREE_TYPE (@0), c2); })
5125 { wide_int_to_tree (TREE_TYPE (@0), c3); })))))
5126 (simplify
5127 (cmp (bit_and:s (rshift:s @0 INTEGER_CST@1) INTEGER_CST@2) INTEGER_CST@3)
5128 (if (tree_fits_shwi_p (@1)
5129 && tree_to_shwi (@1) > 0
5130 && tree_to_shwi (@1) < TYPE_PRECISION (TREE_TYPE (@0)))
5131 (with { tree t0 = TREE_TYPE (@0);
5132 unsigned int prec = TYPE_PRECISION (t0);
5133 wide_int c1 = wi::to_wide (@1);
5134 wide_int c2 = wi::to_wide (@2);
5135 wide_int c3 = wi::to_wide (@3);
5136 wide_int sb = wi::set_bit_in_zero (prec - 1, prec); }
5137 (if ((c2 & c3) != c3)
5138 { constant_boolean_node (cmp == NE_EXPR, type); }
5139 (if (TYPE_UNSIGNED (t0))
5140 (if ((c3 & wi::arshift (sb, c1 - 1)) != 0)
5141 { constant_boolean_node (cmp == NE_EXPR, type); }
5142 (cmp (bit_and @0 { wide_int_to_tree (t0, c2 << c1); })
5143 { wide_int_to_tree (t0, c3 << c1); }))
5144 (with { wide_int smask = wi::arshift (sb, c1); }
5145 (switch
5146 (if ((c2 & smask) == 0)
5147 (cmp (bit_and @0 { wide_int_to_tree (t0, c2 << c1); })
5148 { wide_int_to_tree (t0, c3 << c1); }))
5149 (if ((c3 & smask) == 0)
5150 (cmp (bit_and @0 { wide_int_to_tree (t0, (c2 << c1) | sb); })
5151 { wide_int_to_tree (t0, c3 << c1); }))
5152 (if ((c2 & smask) != (c3 & smask))
5153 { constant_boolean_node (cmp == NE_EXPR, type); })
5154 (cmp (bit_and @0 { wide_int_to_tree (t0, (c2 << c1) | sb); })
5155 { wide_int_to_tree (t0, (c3 << c1) | sb); })))))))))
5156
5157 /* Fold (X << C1) & C2 into (X << C1) & (C2 | ((1 << C1) - 1))
5158 (X >> C1) & C2 into (X >> C1) & (C2 | ~((type) -1 >> C1))
5159 if the new mask might be further optimized. */
5160 (for shift (lshift rshift)
5161 (simplify
5162 (bit_and (convert?:s@4 (shift:s@5 (convert1?@3 @0) INTEGER_CST@1))
5163 INTEGER_CST@2)
5164 (if (tree_nop_conversion_p (TREE_TYPE (@4), TREE_TYPE (@5))
5165 && TYPE_PRECISION (type) <= HOST_BITS_PER_WIDE_INT
5166 && tree_fits_uhwi_p (@1)
5167 && tree_to_uhwi (@1) > 0
5168 && tree_to_uhwi (@1) < TYPE_PRECISION (type))
5169 (with
5170 {
5171 unsigned int shiftc = tree_to_uhwi (@1);
5172 unsigned HOST_WIDE_INT mask = TREE_INT_CST_LOW (@2);
5173 unsigned HOST_WIDE_INT newmask, zerobits = 0;
5174 tree shift_type = TREE_TYPE (@3);
5175 unsigned int prec;
5176
5177 if (shift == LSHIFT_EXPR)
5178 zerobits = ((HOST_WIDE_INT_1U << shiftc) - 1);
5179 else if (shift == RSHIFT_EXPR
5180 && type_has_mode_precision_p (shift_type))
5181 {
5182 prec = TYPE_PRECISION (TREE_TYPE (@3));
5183 tree arg00 = @0;
5184 /* See if more bits can be proven as zero because of
5185 zero extension. */
5186 if (@3 != @0
5187 && TYPE_UNSIGNED (TREE_TYPE (@0)))
5188 {
5189 tree inner_type = TREE_TYPE (@0);
5190 if (type_has_mode_precision_p (inner_type)
5191 && TYPE_PRECISION (inner_type) < prec)
5192 {
5193 prec = TYPE_PRECISION (inner_type);
5194 /* See if we can shorten the right shift. */
5195 if (shiftc < prec)
5196 shift_type = inner_type;
5197 /* Otherwise X >> C1 is all zeros, so we'll optimize
5198 it into (X, 0) later on by making sure zerobits
5199 is all ones. */
5200 }
5201 }
5202 zerobits = HOST_WIDE_INT_M1U;
5203 if (shiftc < prec)
5204 {
5205 zerobits >>= HOST_BITS_PER_WIDE_INT - shiftc;
5206 zerobits <<= prec - shiftc;
5207 }
5208 /* For arithmetic shift if sign bit could be set, zerobits
5209 can contain actually sign bits, so no transformation is
5210 possible, unless MASK masks them all away. In that
5211 case the shift needs to be converted into logical shift. */
5212 if (!TYPE_UNSIGNED (TREE_TYPE (@3))
5213 && prec == TYPE_PRECISION (TREE_TYPE (@3)))
5214 {
5215 if ((mask & zerobits) == 0)
5216 shift_type = unsigned_type_for (TREE_TYPE (@3));
5217 else
5218 zerobits = 0;
5219 }
5220 }
5221 }
5222 /* ((X << 16) & 0xff00) is (X, 0). */
5223 (if ((mask & zerobits) == mask)
5224 { build_int_cst (type, 0); }
5225 (with { newmask = mask | zerobits; }
5226 (if (newmask != mask && (newmask & (newmask + 1)) == 0)
5227 (with
5228 {
5229 /* Only do the transformation if NEWMASK is some integer
5230 mode's mask. */
5231 for (prec = BITS_PER_UNIT;
5232 prec < HOST_BITS_PER_WIDE_INT; prec <<= 1)
5233 if (newmask == (HOST_WIDE_INT_1U << prec) - 1)
5234 break;
5235 }
5236 (if (prec < HOST_BITS_PER_WIDE_INT
5237 || newmask == HOST_WIDE_INT_M1U)
5238 (with
5239 { tree newmaskt = build_int_cst_type (TREE_TYPE (@2), newmask); }
5240 (if (!tree_int_cst_equal (newmaskt, @2))
5241 (if (shift_type != TREE_TYPE (@3))
5242 (bit_and (convert (shift:shift_type (convert @3) @1)) { newmaskt; })
5243 (bit_and @4 { newmaskt; })))))))))))))
5244
5245 /* ((1 << n) & M) != 0 -> n == log2 (M) */
5246 (for cmp (ne eq)
5247 icmp (eq ne)
5248 (simplify
5249 (cmp
5250 (bit_and
5251 (nop_convert? (lshift integer_onep @0)) integer_pow2p@1) integer_zerop)
5252 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)))
5253 (icmp @0 { wide_int_to_tree (TREE_TYPE (@0),
5254 wi::exact_log2 (wi::to_wide (@1))); }))))
5255
5256 /* Fold (X {&,^,|} C2) << C1 into (X << C1) {&,^,|} (C2 << C1)
5257 (X {&,^,|} C2) >> C1 into (X >> C1) & (C2 >> C1). */
5258 (for shift (lshift rshift)
5259 (for bit_op (bit_and bit_xor bit_ior)
5260 (simplify
5261 (shift (convert?:s (bit_op:s @0 INTEGER_CST@2)) INTEGER_CST@1)
5262 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
5263 (with { tree mask = int_const_binop (shift, fold_convert (type, @2), @1); }
5264 (if (mask)
5265 (bit_op (shift (convert @0) @1) { mask; })))))))
5266
5267 /* Fold ((X >> C1) & C2) * (1 << C1) into X & (C2 << C1), including cases where
5268 the & happens in a different type. It is the conversion case that isn't
5269 a composition of other folds.
5270
5271 Let the type of the * and >> be T1 and the type of the & be T2.
5272 The fold is valid if the conversion to T2 preserves all information;
5273 that is, if T2 is wider than T1 or drops no more than C1 bits from T1.
5274 In that case, the & might operate on bits that are dropped by the
5275 later conversion to T1 and the multiplication by (1 << C1), but those
5276 bits are also dropped by ANDing with C2 << C1 (converted to T1).
5277
5278 If the conversion to T2 is not information-preserving, we have to be
5279 careful about the later conversion to T1 acting as a sign extension.
5280 We need either T2 to be unsigned or the top (sign) bit of C2 to be clear.
5281 That is equivalent to testing whether C2 is nonnegative. */
5282 (simplify
5283 (mult (convert? (bit_and (convert? (rshift @0 INTEGER_CST@1)) INTEGER_CST@2))
5284 INTEGER_CST@3)
5285 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
5286 && wi::ltu_p (wi::to_widest (@1), TYPE_PRECISION (type)))
5287 (with { unsigned int shift = tree_to_uhwi (@1);
5288 unsigned int prec = TYPE_PRECISION (type); }
5289 (if ((prec <= TYPE_PRECISION (TREE_TYPE (@2)) + shift
5290 || tree_int_cst_sgn (@2) >= 0)
5291 && wi::to_wide (@3) == wi::set_bit_in_zero (shift, prec))
5292 (with { auto mask = wide_int::from (wi::to_wide (@2), prec, UNSIGNED); }
5293 (bit_and (convert @0) { wide_int_to_tree (type, mask << shift); }))))))
5294
5295 /* ~(~X >> Y) -> X >> Y (for arithmetic shift). */
5296 (simplify
5297 (bit_not (convert1?:s (rshift:s (convert2?@0 (bit_not @1)) @2)))
5298 (if (!TYPE_UNSIGNED (TREE_TYPE (@0))
5299 && (element_precision (TREE_TYPE (@0))
5300 <= element_precision (TREE_TYPE (@1))
5301 || !TYPE_UNSIGNED (TREE_TYPE (@1))))
5302 (with
5303 { tree shift_type = TREE_TYPE (@0); }
5304 (convert (rshift (convert:shift_type @1) @2)))))
5305
5306 /* ~(~X >>r Y) -> X >>r Y
5307 ~(~X <<r Y) -> X <<r Y */
5308 (for rotate (lrotate rrotate)
5309 (simplify
5310 (bit_not (convert1?:s (rotate:s (convert2?@0 (bit_not @1)) @2)))
5311 (if ((element_precision (TREE_TYPE (@0))
5312 <= element_precision (TREE_TYPE (@1))
5313 || !TYPE_UNSIGNED (TREE_TYPE (@1)))
5314 && (element_precision (type) <= element_precision (TREE_TYPE (@0))
5315 || !TYPE_UNSIGNED (TREE_TYPE (@0))))
5316 (with
5317 { tree rotate_type = TREE_TYPE (@0); }
5318 (convert (rotate (convert:rotate_type @1) @2))))))
5319
5320 (for cmp (eq ne)
5321 (for rotate (lrotate rrotate)
5322 invrot (rrotate lrotate)
5323 /* (X >>r Y) cmp (Z >>r Y) may simplify to X cmp Y. */
5324 (simplify
5325 (cmp (rotate @1 @0) (rotate @2 @0))
5326 (cmp @1 @2))
5327 /* (X >>r C1) cmp C2 may simplify to X cmp C3. */
5328 (simplify
5329 (cmp (rotate @0 INTEGER_CST@1) INTEGER_CST@2)
5330 (cmp @0 { const_binop (invrot, TREE_TYPE (@0), @2, @1); }))
5331 /* (X >>r Y) cmp C where C is 0 or ~0, may simplify to X cmp C. */
5332 (simplify
5333 (cmp (rotate @0 @1) INTEGER_CST@2)
5334 (if (integer_zerop (@2) || integer_all_onesp (@2))
5335 (cmp @0 @2)))))
5336
5337 /* Narrow a lshift by constant. */
5338 (simplify
5339 (convert (lshift:s@0 @1 INTEGER_CST@2))
5340 (if (INTEGRAL_TYPE_P (type)
5341 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
5342 && !integer_zerop (@2)
5343 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0)))
5344 (if (TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (@0))
5345 || wi::ltu_p (wi::to_wide (@2), TYPE_PRECISION (type)))
5346 (lshift (convert @1) @2)
5347 (if (wi::ltu_p (wi::to_wide (@2), TYPE_PRECISION (TREE_TYPE (@0))))
5348 { build_zero_cst (type); }))))
5349
5350 /* Simplifications of conversions. */
5351
5352 /* Basic strip-useless-type-conversions / strip_nops. */
5353 (for cvt (convert view_convert float fix_trunc)
5354 (simplify
5355 (cvt @0)
5356 (if ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@0)))
5357 || (GENERIC && type == TREE_TYPE (@0)))
5358 @0)))
5359
5360 /* Contract view-conversions. */
5361 (simplify
5362 (view_convert (view_convert @0))
5363 (view_convert @0))
5364
5365 /* For integral conversions with the same precision or pointer
5366 conversions use a NOP_EXPR instead. */
5367 (simplify
5368 (view_convert @0)
5369 (if ((INTEGRAL_TYPE_P (type) || POINTER_TYPE_P (type))
5370 && (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
5371 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (@0)))
5372 (convert @0)))
5373
5374 /* Strip inner integral conversions that do not change precision or size, or
5375 zero-extend while keeping the same size (for bool-to-char). */
5376 (simplify
5377 (view_convert (convert@0 @1))
5378 (if ((INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
5379 && (INTEGRAL_TYPE_P (TREE_TYPE (@1)) || POINTER_TYPE_P (TREE_TYPE (@1)))
5380 && TYPE_SIZE (TREE_TYPE (@0)) == TYPE_SIZE (TREE_TYPE (@1))
5381 && (TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1))
5382 || (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (TREE_TYPE (@1))
5383 && TYPE_UNSIGNED (TREE_TYPE (@1)))))
5384 (view_convert @1)))
5385
5386 /* Simplify a view-converted empty or single-element constructor. */
5387 (simplify
5388 (view_convert CONSTRUCTOR@0)
5389 (with
5390 { tree ctor = (TREE_CODE (@0) == SSA_NAME
5391 ? gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0)) : @0); }
5392 (switch
5393 (if (CONSTRUCTOR_NELTS (ctor) == 0)
5394 { build_zero_cst (type); })
5395 (if (CONSTRUCTOR_NELTS (ctor) == 1
5396 && VECTOR_TYPE_P (TREE_TYPE (ctor))
5397 && operand_equal_p (TYPE_SIZE (type),
5398 TYPE_SIZE (TREE_TYPE
5399 (CONSTRUCTOR_ELT (ctor, 0)->value))))
5400 (view_convert { CONSTRUCTOR_ELT (ctor, 0)->value; })))))
5401
5402 /* Re-association barriers around constants and other re-association
5403 barriers can be removed. */
5404 (simplify
5405 (paren CONSTANT_CLASS_P@0)
5406 @0)
5407 (simplify
5408 (paren (paren@1 @0))
5409 @1)
5410
5411 /* Handle cases of two conversions in a row. */
5412 (for ocvt (convert float fix_trunc)
5413 (for icvt (convert float)
5414 (simplify
5415 (ocvt (icvt@1 @0))
5416 (with
5417 {
5418 tree inside_type = TREE_TYPE (@0);
5419 tree inter_type = TREE_TYPE (@1);
5420 int inside_int = INTEGRAL_TYPE_P (inside_type);
5421 int inside_ptr = POINTER_TYPE_P (inside_type);
5422 int inside_float = FLOAT_TYPE_P (inside_type);
5423 int inside_vec = VECTOR_TYPE_P (inside_type);
5424 unsigned int inside_prec = element_precision (inside_type);
5425 int inside_unsignedp = TYPE_UNSIGNED (inside_type);
5426 int inter_int = INTEGRAL_TYPE_P (inter_type);
5427 int inter_ptr = POINTER_TYPE_P (inter_type);
5428 int inter_float = FLOAT_TYPE_P (inter_type);
5429 int inter_vec = VECTOR_TYPE_P (inter_type);
5430 unsigned int inter_prec = element_precision (inter_type);
5431 int inter_unsignedp = TYPE_UNSIGNED (inter_type);
5432 int final_int = INTEGRAL_TYPE_P (type);
5433 int final_ptr = POINTER_TYPE_P (type);
5434 int final_float = FLOAT_TYPE_P (type);
5435 int final_vec = VECTOR_TYPE_P (type);
5436 unsigned int final_prec = element_precision (type);
5437 int final_unsignedp = TYPE_UNSIGNED (type);
5438 }
5439 (switch
5440 /* In addition to the cases of two conversions in a row
5441 handled below, if we are converting something to its own
5442 type via an object of identical or wider precision, neither
5443 conversion is needed. */
5444 (if (((GIMPLE && useless_type_conversion_p (type, inside_type))
5445 || (GENERIC
5446 && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (inside_type)))
5447 && (((inter_int || inter_ptr) && final_int)
5448 || (inter_float && final_float))
5449 && inter_prec >= final_prec)
5450 (ocvt @0))
5451
5452 /* Likewise, if the intermediate and initial types are either both
5453 float or both integer, we don't need the middle conversion if the
5454 former is wider than the latter and doesn't change the signedness
5455 (for integers). Avoid this if the final type is a pointer since
5456 then we sometimes need the middle conversion. */
5457 (if (((inter_int && inside_int) || (inter_float && inside_float))
5458 && (final_int || final_float)
5459 && inter_prec >= inside_prec
5460 && (inter_float || inter_unsignedp == inside_unsignedp))
5461 (ocvt @0))
5462
5463 /* If we have a sign-extension of a zero-extended value, we can
5464 replace that by a single zero-extension. Likewise if the
5465 final conversion does not change precision we can drop the
5466 intermediate conversion. Similarly truncation of a sign-extension
5467 can be replaced by a single sign-extension. */
5468 (if (inside_int && inter_int && final_int
5469 && ((inside_prec < inter_prec && inter_prec < final_prec
5470 && inside_unsignedp && !inter_unsignedp)
5471 || final_prec == inter_prec
5472 || (inside_prec < inter_prec && inter_prec > final_prec
5473 && !inside_unsignedp && inter_unsignedp)))
5474 (ocvt @0))
5475
5476 /* Two conversions in a row are not needed unless:
5477 - some conversion is floating-point (overstrict for now), or
5478 - some conversion is a vector (overstrict for now), or
5479 - the intermediate type is narrower than both initial and
5480 final, or
5481 - the intermediate type and innermost type differ in signedness,
5482 and the outermost type is wider than the intermediate, or
5483 - the initial type is a pointer type and the precisions of the
5484 intermediate and final types differ, or
5485 - the final type is a pointer type and the precisions of the
5486 initial and intermediate types differ. */
5487 (if (! inside_float && ! inter_float && ! final_float
5488 && ! inside_vec && ! inter_vec && ! final_vec
5489 && (inter_prec >= inside_prec || inter_prec >= final_prec)
5490 && ! (inside_int && inter_int
5491 && inter_unsignedp != inside_unsignedp
5492 && inter_prec < final_prec)
5493 && ((inter_unsignedp && inter_prec > inside_prec)
5494 == (final_unsignedp && final_prec > inter_prec))
5495 && ! (inside_ptr && inter_prec != final_prec)
5496 && ! (final_ptr && inside_prec != inter_prec))
5497 (ocvt @0))
5498
5499 /* `(outer:M)(inter:N) a:O`
5500 can be converted to `(outer:M) a`
5501 if M <= O && N >= O. No matter what signedness of the casts,
5502 as the final is either a truncation from the original or just
5503 a sign change of the type. */
5504 (if (inside_int && inter_int && final_int
5505 && final_prec <= inside_prec
5506 && inter_prec >= inside_prec)
5507 (convert @0))
5508
5509 /* A truncation to an unsigned type (a zero-extension) should be
5510 canonicalized as bitwise and of a mask. */
5511 (if (GIMPLE /* PR70366: doing this in GENERIC breaks -Wconversion. */
5512 && final_int && inter_int && inside_int
5513 && final_prec >= inside_prec
5514 && inside_prec > inter_prec
5515 && inter_unsignedp)
5516 (convert (bit_and @0 { wide_int_to_tree
5517 (inside_type,
5518 wi::mask (inter_prec, false,
5519 TYPE_PRECISION (inside_type))); })))
5520
5521 /* If we are converting an integer to a floating-point that can
5522 represent it exactly and back to an integer, we can skip the
5523 floating-point conversion. */
5524 (if (GIMPLE /* PR66211 */
5525 && inside_int && inter_float && final_int &&
5526 (unsigned) significand_size (TYPE_MODE (inter_type))
5527 >= inside_prec - !inside_unsignedp)
5528 (convert @0)))))))
5529
5530 /* (float_type)(integer_type) x -> trunc (x) if the type of x matches
5531 float_type. Only do the transformation if we do not need to preserve
5532 trapping behaviour, so require !flag_trapping_math. */
5533 #if GIMPLE
5534 (simplify
5535 (float (fix_trunc @0))
5536 (if (!flag_trapping_math
5537 && !HONOR_SIGNED_ZEROS (type)
5538 && types_match (type, TREE_TYPE (@0))
5539 && direct_internal_fn_supported_p (IFN_TRUNC, type,
5540 OPTIMIZE_FOR_BOTH))
5541 (IFN_TRUNC @0)))
5542 #endif
5543
5544 /* If we have a narrowing conversion to an integral type that is fed by a
5545 BIT_AND_EXPR, we might be able to remove the BIT_AND_EXPR if it merely
5546 masks off bits outside the final type (and nothing else). */
5547 (simplify
5548 (convert (bit_and @0 INTEGER_CST@1))
5549 (if (INTEGRAL_TYPE_P (type)
5550 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
5551 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))
5552 && operand_equal_p (@1, build_low_bits_mask (TREE_TYPE (@1),
5553 TYPE_PRECISION (type)), 0))
5554 (convert @0)))
5555
5556 #if GIMPLE
5557 /* X / (1 << C) -> X /[ex] (1 << C) if the low C bits of X are clear. */
5558 (simplify
5559 (trunc_div with_possible_nonzero_bits@0 integer_pow2p@1)
5560 (if (INTEGRAL_TYPE_P (type)
5561 && !TYPE_UNSIGNED (type)
5562 && wi::multiple_of_p (get_nonzero_bits (@0), wi::to_wide (@1), SIGNED))
5563 (exact_div @0 @1)))
5564 #endif
5565
5566 /* (X /[ex] A) * A -> X. */
5567 (simplify
5568 (mult (convert1? (exact_div @0 @@1)) (convert2? @1))
5569 (convert @0))
5570
5571 /* (X /[ex] C1) * (C1 * C2) -> X * C2. */
5572 (simplify
5573 (mult (convert? (exact_div @0 INTEGER_CST@1)) poly_int_tree_p@2)
5574 (with { poly_widest_int factor; }
5575 (if (multiple_p (wi::to_poly_widest (@2), wi::to_widest (@1), &factor))
5576 (mult (convert @0) { wide_int_to_tree (type, factor); }))))
5577
5578 /* Simplify (A / B) * B + (A % B) -> A. */
5579 (for div (trunc_div ceil_div floor_div round_div)
5580 mod (trunc_mod ceil_mod floor_mod round_mod)
5581 (simplify
5582 (plus:c (mult:c (div @0 @1) @1) (mod @0 @1))
5583 @0))
5584
5585 /* x / y * y == x -> x % y == 0. */
5586 (simplify
5587 (eq:c (mult:c (trunc_div:s @0 @1) @1) @0)
5588 (if (TREE_CODE (TREE_TYPE (@0)) != COMPLEX_TYPE
5589 && (!VECTOR_MODE_P (TYPE_MODE (TREE_TYPE (@0)))
5590 || !target_supports_op_p (TREE_TYPE (@0), TRUNC_DIV_EXPR,
5591 optab_vector)
5592 || target_supports_op_p (TREE_TYPE (@0), TRUNC_MOD_EXPR,
5593 optab_vector)))
5594 (eq (trunc_mod @0 @1) { build_zero_cst (TREE_TYPE (@0)); })))
5595
5596 /* ((X /[ex] C1) +- C2) * (C1 * C3) --> (X * C3) +- (C1 * C2 * C3). */
5597 (for op (plus minus)
5598 (simplify
5599 (mult (convert1? (op (convert2? (exact_div @0 INTEGER_CST@1))
5600 poly_int_tree_p@2))
5601 poly_int_tree_p@3)
5602 (with { poly_widest_int factor; }
5603 (if (tree_nop_conversion_p (type, TREE_TYPE (@2))
5604 && tree_nop_conversion_p (TREE_TYPE (@0), TREE_TYPE (@2))
5605 && multiple_p (wi::to_poly_widest (@3), wi::to_widest (@1), &factor))
5606 (with
5607 {
5608 wi::overflow_type overflow;
5609 wide_int mul;
5610 }
5611 (if (types_match (type, TREE_TYPE (@2))
5612 && types_match (TREE_TYPE (@0), TREE_TYPE (@2))
5613 && TREE_CODE (@2) == INTEGER_CST
5614 && TREE_CODE (@3) == INTEGER_CST
5615 && (mul = wi::mul (wi::to_wide (@2), wi::to_wide (@3),
5616 TYPE_SIGN (type), &overflow),
5617 !overflow)
5618 && (TYPE_UNSIGNED (type)
5619 /* Not using unsigned arithmetics is unsafe if factor
5620 isn't 1 and if for op plus @0 and @2 could have different
5621 sign or for op minus @0 and @2 could have the same sign. */
5622 || known_eq (factor, 1)
5623 || (get_range_pos_neg (@0)
5624 | (((op == PLUS_EXPR) ^ (tree_int_cst_sgn (@2) < 0))
5625 ? 1 : 2)) != 3))
5626 (op (mult @0 { wide_int_to_tree (type, factor); })
5627 { wide_int_to_tree (type, mul); })
5628 (with { tree utype = unsigned_type_for (type); }
5629 (convert (op (mult (convert:utype @0)
5630 { wide_int_to_tree (utype, factor); })
5631 (mult (convert:utype @3) (convert:utype @2)))))))))))
5632
5633 /* Canonicalization of binary operations. */
5634
5635 /* Convert X + -C into X - C. */
5636 (simplify
5637 (plus @0 REAL_CST@1)
5638 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
5639 (with { tree tem = const_unop (NEGATE_EXPR, type, @1); }
5640 (if (!TREE_OVERFLOW (tem) || !flag_trapping_math)
5641 (minus @0 { tem; })))))
5642
5643 /* Convert x+x into x*2. */
5644 (simplify
5645 (plus @0 @0)
5646 (if (SCALAR_FLOAT_TYPE_P (type))
5647 (mult @0 { build_real (type, dconst2); })
5648 (if (INTEGRAL_TYPE_P (type))
5649 (mult @0 { build_int_cst (type, 2); }))))
5650
5651 /* 0 - X -> -X. */
5652 (simplify
5653 (minus integer_zerop @1)
5654 (negate @1))
5655 (simplify
5656 (pointer_diff integer_zerop @1)
5657 (negate (convert @1)))
5658
5659 /* (ARG0 - ARG1) is the same as (-ARG1 + ARG0). So check whether
5660 ARG0 is zero and X + ARG0 reduces to X, since that would mean
5661 (-ARG1 + ARG0) reduces to -ARG1. */
5662 (simplify
5663 (minus real_zerop@0 @1)
5664 (if (fold_real_zero_addition_p (type, @1, @0, 0))
5665 (negate @1)))
5666
5667 /* Transform x * -1 into -x. */
5668 (simplify
5669 (mult @0 integer_minus_onep)
5670 (negate @0))
5671
5672 /* Reassociate (X * CST) * Y to (X * Y) * CST. This does not introduce
5673 signed overflow for CST != 0 && CST != -1. */
5674 (simplify
5675 (mult:c (mult:s@3 @0 INTEGER_CST@1) @2)
5676 (if (TREE_CODE (@2) != INTEGER_CST
5677 && single_use (@3)
5678 && !integer_zerop (@1) && !integer_minus_onep (@1))
5679 (mult (mult @0 @2) @1)))
5680
5681 /* True if we can easily extract the real and imaginary parts of a complex
5682 number. */
5683 (match compositional_complex
5684 (convert? (complex @0 @1)))
5685
5686 /* COMPLEX_EXPR and REALPART/IMAGPART_EXPR cancellations. */
5687 (simplify
5688 (complex (realpart @0) (imagpart @0))
5689 @0)
5690 (simplify
5691 (realpart (complex @0 @1))
5692 @0)
5693 (simplify
5694 (imagpart (complex @0 @1))
5695 @1)
5696
5697 /* Sometimes we only care about half of a complex expression. */
5698 (simplify
5699 (realpart (convert?:s (conj:s @0)))
5700 (convert (realpart @0)))
5701 (simplify
5702 (imagpart (convert?:s (conj:s @0)))
5703 (convert (negate (imagpart @0))))
5704 (for part (realpart imagpart)
5705 (for op (plus minus)
5706 (simplify
5707 (part (convert?:s@2 (op:s @0 @1)))
5708 (convert (op (part @0) (part @1))))))
5709 (simplify
5710 (realpart (convert?:s (CEXPI:s @0)))
5711 (convert (COS @0)))
5712 (simplify
5713 (imagpart (convert?:s (CEXPI:s @0)))
5714 (convert (SIN @0)))
5715
5716 /* conj(conj(x)) -> x */
5717 (simplify
5718 (conj (convert? (conj @0)))
5719 (if (tree_nop_conversion_p (TREE_TYPE (@0), type))
5720 (convert @0)))
5721
5722 /* conj({x,y}) -> {x,-y} */
5723 (simplify
5724 (conj (convert?:s (complex:s @0 @1)))
5725 (with { tree itype = TREE_TYPE (type); }
5726 (complex (convert:itype @0) (negate (convert:itype @1)))))
5727
5728 /* BSWAP simplifications, transforms checked by gcc.dg/builtin-bswap-8.c. */
5729 (for bswap (BSWAP)
5730 (simplify
5731 (bswap (bswap @0))
5732 @0)
5733 (simplify
5734 (bswap (bit_not (bswap @0)))
5735 (bit_not @0))
5736 (for bitop (bit_xor bit_ior bit_and)
5737 (simplify
5738 (bswap (bitop:c (bswap @0) @1))
5739 (bitop @0 (bswap @1))))
5740 (for cmp (eq ne)
5741 (simplify
5742 (cmp (bswap@2 @0) (bswap @1))
5743 (with { tree ctype = TREE_TYPE (@2); }
5744 (cmp (convert:ctype @0) (convert:ctype @1))))
5745 (simplify
5746 (cmp (bswap @0) INTEGER_CST@1)
5747 (with { tree ctype = TREE_TYPE (@1); }
5748 (cmp (convert:ctype @0) (bswap! @1)))))
5749 /* (bswap(x) >> C1) & C2 can sometimes be simplified to (x >> C3) & C2. */
5750 (simplify
5751 (bit_and (convert1? (rshift@0 (convert2? (bswap@4 @1)) INTEGER_CST@2))
5752 INTEGER_CST@3)
5753 (if (BITS_PER_UNIT == 8
5754 && tree_fits_uhwi_p (@2)
5755 && tree_fits_uhwi_p (@3))
5756 (with
5757 {
5758 unsigned HOST_WIDE_INT prec = TYPE_PRECISION (TREE_TYPE (@4));
5759 unsigned HOST_WIDE_INT bits = tree_to_uhwi (@2);
5760 unsigned HOST_WIDE_INT mask = tree_to_uhwi (@3);
5761 unsigned HOST_WIDE_INT lo = bits & 7;
5762 unsigned HOST_WIDE_INT hi = bits - lo;
5763 }
5764 (if (bits < prec
5765 && mask < (256u>>lo)
5766 && bits < TYPE_PRECISION (TREE_TYPE(@0)))
5767 (with { unsigned HOST_WIDE_INT ns = (prec - (hi + 8)) + lo; }
5768 (if (ns == 0)
5769 (bit_and (convert @1) @3)
5770 (with
5771 {
5772 tree utype = unsigned_type_for (TREE_TYPE (@1));
5773 tree nst = build_int_cst (integer_type_node, ns);
5774 }
5775 (bit_and (convert (rshift:utype (convert:utype @1) {nst;})) @3))))))))
5776 /* bswap(x) >> C1 can sometimes be simplified to (T)x >> C2. */
5777 (simplify
5778 (rshift (convert? (bswap@2 @0)) INTEGER_CST@1)
5779 (if (BITS_PER_UNIT == 8
5780 && CHAR_TYPE_SIZE == 8
5781 && tree_fits_uhwi_p (@1))
5782 (with
5783 {
5784 unsigned HOST_WIDE_INT prec = TYPE_PRECISION (TREE_TYPE (@2));
5785 unsigned HOST_WIDE_INT bits = tree_to_uhwi (@1);
5786 /* If the bswap was extended before the original shift, this
5787 byte (shift) has the sign of the extension, not the sign of
5788 the original shift. */
5789 tree st = TYPE_PRECISION (type) > prec ? TREE_TYPE (@2) : type;
5790 }
5791 /* Special case: logical right shift of sign-extended bswap.
5792 (unsigned)(short)bswap16(x)>>12 is (unsigned)((short)x<<8)>>12. */
5793 (if (TYPE_PRECISION (type) > prec
5794 && !TYPE_UNSIGNED (TREE_TYPE (@2))
5795 && TYPE_UNSIGNED (type)
5796 && bits < prec && bits + 8 >= prec)
5797 (with { tree nst = build_int_cst (integer_type_node, prec - 8); }
5798 (rshift (convert (lshift:st (convert:st @0) {nst;})) @1))
5799 (if (bits + 8 == prec)
5800 (if (TYPE_UNSIGNED (st))
5801 (convert (convert:unsigned_char_type_node @0))
5802 (convert (convert:signed_char_type_node @0)))
5803 (if (bits < prec && bits + 8 > prec)
5804 (with
5805 {
5806 tree nst = build_int_cst (integer_type_node, bits & 7);
5807 tree bt = TYPE_UNSIGNED (st) ? unsigned_char_type_node
5808 : signed_char_type_node;
5809 }
5810 (convert (rshift:bt (convert:bt @0) {nst;})))))))))
5811 /* bswap(x) & C1 can sometimes be simplified to (x >> C2) & C1. */
5812 (simplify
5813 (bit_and (convert? (bswap@2 @0)) INTEGER_CST@1)
5814 (if (BITS_PER_UNIT == 8
5815 && tree_fits_uhwi_p (@1)
5816 && tree_to_uhwi (@1) < 256)
5817 (with
5818 {
5819 unsigned HOST_WIDE_INT prec = TYPE_PRECISION (TREE_TYPE (@2));
5820 tree utype = unsigned_type_for (TREE_TYPE (@0));
5821 tree nst = build_int_cst (integer_type_node, prec - 8);
5822 }
5823 (bit_and (convert (rshift:utype (convert:utype @0) {nst;})) @1)))))
5824
5825
5826 /* Combine COND_EXPRs and VEC_COND_EXPRs. */
5827
5828 /* Simplify constant conditions.
5829 Only optimize constant conditions when the selected branch
5830 has the same type as the COND_EXPR. This avoids optimizing
5831 away "c ? x : throw", where the throw has a void type.
5832 Note that we cannot throw away the fold-const.cc variant nor
5833 this one as we depend on doing this transform before possibly
5834 A ? B : B -> B triggers and the fold-const.cc one can optimize
5835 0 ? A : B to B even if A has side-effects. Something
5836 genmatch cannot handle. */
5837 (simplify
5838 (cond INTEGER_CST@0 @1 @2)
5839 (if (integer_zerop (@0))
5840 (if (!VOID_TYPE_P (TREE_TYPE (@2)) || VOID_TYPE_P (type))
5841 @2)
5842 (if (!VOID_TYPE_P (TREE_TYPE (@1)) || VOID_TYPE_P (type))
5843 @1)))
5844 (simplify
5845 (vec_cond VECTOR_CST@0 @1 @2)
5846 (if (integer_all_onesp (@0))
5847 @1
5848 (if (integer_zerop (@0))
5849 @2)))
5850
5851 /* Sink unary operations to branches, but only if we do fold both. */
5852 (for op (negate bit_not abs absu)
5853 (simplify
5854 (op (vec_cond:s @0 @1 @2))
5855 (vec_cond @0 (op! @1) (op! @2))))
5856
5857 /* Sink unary conversions to branches, but only if we do fold both
5858 and the target's truth type is the same as we already have. */
5859 (simplify
5860 (convert (vec_cond:s @0 @1 @2))
5861 (if (VECTOR_TYPE_P (type)
5862 && types_match (TREE_TYPE (@0), truth_type_for (type)))
5863 (vec_cond @0 (convert! @1) (convert! @2))))
5864
5865 /* Likewise for view_convert of nop_conversions. */
5866 (simplify
5867 (view_convert (vec_cond:s @0 @1 @2))
5868 (if (VECTOR_TYPE_P (type) && VECTOR_TYPE_P (TREE_TYPE (@1))
5869 && known_eq (TYPE_VECTOR_SUBPARTS (type),
5870 TYPE_VECTOR_SUBPARTS (TREE_TYPE (@1)))
5871 && tree_nop_conversion_p (TREE_TYPE (type), TREE_TYPE (TREE_TYPE (@1)))
5872 && (expand_vec_cond_expr_p (type, TREE_TYPE (@0))
5873 || !expand_vec_cond_expr_p (TREE_TYPE (@1), TREE_TYPE (@0))))
5874 (vec_cond @0 (view_convert! @1) (view_convert! @2))))
5875
5876 /* Sink binary operation to branches, but only if we can fold it. */
5877 (for op (tcc_comparison plus minus mult bit_and bit_ior bit_xor
5878 lshift rshift rdiv trunc_div ceil_div floor_div round_div exact_div
5879 trunc_mod ceil_mod floor_mod round_mod min max)
5880 /* (c ? a : b) op (c ? d : e) --> c ? (a op d) : (b op e) */
5881 (simplify
5882 (op (vec_cond:s @0 @1 @2) (vec_cond:s @0 @3 @4))
5883 (if (VECTOR_TYPE_P (type)
5884 && (TREE_CODE_CLASS (op) != tcc_comparison
5885 || types_match (type, TREE_TYPE (@1))
5886 || expand_vec_cond_expr_p (type, TREE_TYPE (@0))
5887 || (optimize_vectors_before_lowering_p ()
5888 /* The following is optimistic on the side of non-support, we are
5889 missing the legacy vcond{,u,eq} cases. Do this only when
5890 lowering will be able to fixup.. */
5891 && !expand_vec_cond_expr_p (TREE_TYPE (@1), TREE_TYPE (@0)))))
5892 (vec_cond @0 (op! @1 @3) (op! @2 @4))))
5893
5894 /* (c ? a : b) op d --> c ? (a op d) : (b op d) */
5895 (simplify
5896 (op (vec_cond:s @0 @1 @2) @3)
5897 (if (VECTOR_TYPE_P (type)
5898 && (TREE_CODE_CLASS (op) != tcc_comparison
5899 || types_match (type, TREE_TYPE (@1))
5900 || expand_vec_cond_expr_p (type, TREE_TYPE (@0))
5901 || (optimize_vectors_before_lowering_p ()
5902 && !expand_vec_cond_expr_p (TREE_TYPE (@1), TREE_TYPE (@0)))))
5903 (vec_cond @0 (op! @1 @3) (op! @2 @3))))
5904 (simplify
5905 (op @3 (vec_cond:s @0 @1 @2))
5906 (if (VECTOR_TYPE_P (type)
5907 && (TREE_CODE_CLASS (op) != tcc_comparison
5908 || types_match (type, TREE_TYPE (@1))
5909 || expand_vec_cond_expr_p (type, TREE_TYPE (@0))
5910 || (optimize_vectors_before_lowering_p ()
5911 && !expand_vec_cond_expr_p (TREE_TYPE (@1), TREE_TYPE (@0)))))
5912 (vec_cond @0 (op! @3 @1) (op! @3 @2)))))
5913
5914 #if GIMPLE
5915 (match (nop_atomic_bit_test_and_p @0 @1 @4)
5916 (bit_and (convert?@4 (ATOMIC_FETCH_OR_XOR_N @2 INTEGER_CST@0 @3))
5917 INTEGER_CST@1)
5918 (with {
5919 int ibit = tree_log2 (@0);
5920 int ibit2 = tree_log2 (@1);
5921 }
5922 (if (ibit == ibit2
5923 && ibit >= 0
5924 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))))))
5925
5926 (match (nop_atomic_bit_test_and_p @0 @1 @3)
5927 (bit_and (convert?@3 (SYNC_FETCH_OR_XOR_N @2 INTEGER_CST@0))
5928 INTEGER_CST@1)
5929 (with {
5930 int ibit = tree_log2 (@0);
5931 int ibit2 = tree_log2 (@1);
5932 }
5933 (if (ibit == ibit2
5934 && ibit >= 0
5935 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))))))
5936
5937 (match (nop_atomic_bit_test_and_p @0 @0 @4)
5938 (bit_and:c
5939 (convert1?@4
5940 (ATOMIC_FETCH_OR_XOR_N @2 (nop_convert? (lshift@0 integer_onep@5 @6)) @3))
5941 (convert2? @0))
5942 (if (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0)))))
5943
5944 (match (nop_atomic_bit_test_and_p @0 @0 @4)
5945 (bit_and:c
5946 (convert1?@4
5947 (SYNC_FETCH_OR_XOR_N @2 (nop_convert? (lshift@0 integer_onep@3 @5))))
5948 (convert2? @0))
5949 (if (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0)))))
5950
5951 (match (nop_atomic_bit_test_and_p @0 @1 @3)
5952 (bit_and@4 (convert?@3 (ATOMIC_FETCH_AND_N @2 INTEGER_CST@0 @5))
5953 INTEGER_CST@1)
5954 (with {
5955 int ibit = wi::exact_log2 (wi::zext (wi::bit_not (wi::to_wide (@0)),
5956 TYPE_PRECISION(type)));
5957 int ibit2 = tree_log2 (@1);
5958 }
5959 (if (ibit == ibit2
5960 && ibit >= 0
5961 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))))))
5962
5963 (match (nop_atomic_bit_test_and_p @0 @1 @3)
5964 (bit_and@4
5965 (convert?@3 (SYNC_FETCH_AND_AND_N @2 INTEGER_CST@0))
5966 INTEGER_CST@1)
5967 (with {
5968 int ibit = wi::exact_log2 (wi::zext (wi::bit_not (wi::to_wide (@0)),
5969 TYPE_PRECISION(type)));
5970 int ibit2 = tree_log2 (@1);
5971 }
5972 (if (ibit == ibit2
5973 && ibit >= 0
5974 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))))))
5975
5976 (match (nop_atomic_bit_test_and_p @4 @0 @3)
5977 (bit_and:c
5978 (convert1?@3
5979 (ATOMIC_FETCH_AND_N @2 (nop_convert?@4 (bit_not (lshift@0 integer_onep@6 @7))) @5))
5980 (convert2? @0))
5981 (if (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@4)))))
5982
5983 (match (nop_atomic_bit_test_and_p @4 @0 @3)
5984 (bit_and:c
5985 (convert1?@3
5986 (SYNC_FETCH_AND_AND_N @2 (nop_convert?@4 (bit_not (lshift@0 integer_onep@6 @7)))))
5987 (convert2? @0))
5988 (if (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@4)))))
5989
5990 #endif
5991
5992 /* (v ? w : 0) ? a : b is just (v & w) ? a : b
5993 Currently disabled after pass lvec because ARM understands
5994 VEC_COND_EXPR<v==w,-1,0> but not a plain v==w fed to BIT_IOR_EXPR. */
5995 #if GIMPLE
5996 /* These can only be done in gimple as fold likes to convert:
5997 (CMP) & N into (CMP) ? N : 0
5998 and we try to match the same pattern again and again. */
5999 (simplify
6000 (vec_cond (vec_cond:s @0 @3 integer_zerop) @1 @2)
6001 (if (optimize_vectors_before_lowering_p () && types_match (@0, @3))
6002 (vec_cond (bit_and @0 @3) @1 @2)))
6003 (simplify
6004 (vec_cond (vec_cond:s @0 integer_all_onesp @3) @1 @2)
6005 (if (optimize_vectors_before_lowering_p () && types_match (@0, @3))
6006 (vec_cond (bit_ior @0 @3) @1 @2)))
6007 (simplify
6008 (vec_cond (vec_cond:s @0 integer_zerop @3) @1 @2)
6009 (if (optimize_vectors_before_lowering_p () && types_match (@0, @3))
6010 (vec_cond (bit_ior @0 (bit_not @3)) @2 @1)))
6011 (simplify
6012 (vec_cond (vec_cond:s @0 @3 integer_all_onesp) @1 @2)
6013 (if (optimize_vectors_before_lowering_p () && types_match (@0, @3))
6014 (vec_cond (bit_and @0 (bit_not @3)) @2 @1)))
6015
6016 /* ((VCE (a cmp b ? -1 : 0)) < 0) ? c : d is just
6017 (VCE ((a cmp b) ? (VCE c) : (VCE d))) when TYPE_PRECISION of the
6018 component type of the outer vec_cond is greater equal the inner one. */
6019 (for cmp (simple_comparison)
6020 (simplify
6021 (vec_cond
6022 (lt (view_convert@5 (vec_cond@6 (cmp@4 @0 @1)
6023 integer_all_onesp
6024 integer_zerop))
6025 integer_zerop) @2 @3)
6026 (if (VECTOR_INTEGER_TYPE_P (TREE_TYPE (@0))
6027 && VECTOR_INTEGER_TYPE_P (TREE_TYPE (@5))
6028 && !TYPE_UNSIGNED (TREE_TYPE (@5))
6029 && VECTOR_TYPE_P (TREE_TYPE (@6))
6030 && VECTOR_TYPE_P (type)
6031 && tree_int_cst_le (TYPE_SIZE (TREE_TYPE (type)),
6032 TYPE_SIZE (TREE_TYPE (TREE_TYPE (@6))))
6033 && TYPE_SIZE (type) == TYPE_SIZE (TREE_TYPE (@6)))
6034 (with { tree vtype = TREE_TYPE (@6);}
6035 (view_convert:type
6036 (vec_cond @4 (view_convert:vtype @2) (view_convert:vtype @3)))))))
6037
6038 /* c1 ? c2 ? a : b : b --> (c1 & c2) ? a : b */
6039 (simplify
6040 (vec_cond @0 (vec_cond:s @1 @2 @3) @3)
6041 (if (optimize_vectors_before_lowering_p () && types_match (@0, @1))
6042 (vec_cond (bit_and @0 @1) @2 @3)))
6043 (simplify
6044 (vec_cond @0 @2 (vec_cond:s @1 @2 @3))
6045 (if (optimize_vectors_before_lowering_p () && types_match (@0, @1))
6046 (vec_cond (bit_ior @0 @1) @2 @3)))
6047 (simplify
6048 (vec_cond @0 (vec_cond:s @1 @2 @3) @2)
6049 (if (optimize_vectors_before_lowering_p () && types_match (@0, @1))
6050 (vec_cond (bit_ior (bit_not @0) @1) @2 @3)))
6051 (simplify
6052 (vec_cond @0 @3 (vec_cond:s @1 @2 @3))
6053 (if (optimize_vectors_before_lowering_p () && types_match (@0, @1))
6054 (vec_cond (bit_and (bit_not @0) @1) @2 @3)))
6055 #endif
6056
6057 /* (a ? x : y) != (b ? x : y) --> (a^b & (x != y)) ? TRUE : FALSE */
6058 /* (a ? x : y) == (b ? x : y) --> (a^b & (x != y)) ? FALSE : TRUE */
6059 /* (a ? x : y) != (b ? y : x) --> (a^b | (x == y)) ? FALSE : TRUE */
6060 /* (a ? x : y) == (b ? y : x) --> (a^b | (x == y)) ? TRUE : FALSE */
6061 /* These are only valid if x and y don't have NaNs. */
6062 (for cnd (cond vec_cond)
6063 (for eqne (eq ne)
6064 (simplify
6065 (eqne (cnd @0 @1 @2) (cnd @3 @1 @2))
6066 (if (!HONOR_NANS (@1)
6067 && types_match (TREE_TYPE (@0), TREE_TYPE (@3))
6068 && types_match (type, TREE_TYPE (@0))
6069 && expr_no_side_effects_p (@1)
6070 && expr_no_side_effects_p (@2))
6071 (cnd (bit_and (bit_xor @0 @3) (ne:type @1 @2))
6072 { constant_boolean_node (eqne == NE_EXPR, type); }
6073 { constant_boolean_node (eqne != NE_EXPR, type); })))
6074 (simplify
6075 (eqne (cnd @0 @1 @2) (cnd @3 @2 @1))
6076 (if (!HONOR_NANS (@1)
6077 && types_match (TREE_TYPE (@0), TREE_TYPE (@3))
6078 && types_match (type, TREE_TYPE (@0))
6079 && expr_no_side_effects_p (@1)
6080 && expr_no_side_effects_p (@2))
6081 (cnd (bit_ior (bit_xor @0 @3) (eq:type @1 @2))
6082 { constant_boolean_node (eqne != NE_EXPR, type); }
6083 { constant_boolean_node (eqne == NE_EXPR, type); })))))
6084
6085 /* Canonicalize mask ? { 0, ... } : { -1, ...} to ~mask if the mask
6086 types are compatible. */
6087 (simplify
6088 (vec_cond @0 VECTOR_CST@1 VECTOR_CST@2)
6089 (if (VECTOR_BOOLEAN_TYPE_P (type)
6090 && types_match (type, TREE_TYPE (@0)))
6091 (if (integer_zerop (@1) && integer_all_onesp (@2))
6092 (bit_not @0)
6093 (if (integer_all_onesp (@1) && integer_zerop (@2))
6094 @0))))
6095
6096 /* A few simplifications of "a ? CST1 : CST2". */
6097 /* NOTE: Only do this on gimple as the if-chain-to-switch
6098 optimization depends on the gimple to have if statements in it. */
6099 #if GIMPLE
6100 (simplify
6101 (cond @0 INTEGER_CST@1 INTEGER_CST@2)
6102 (switch
6103 (if (integer_zerop (@2))
6104 (switch
6105 /* a ? 1 : 0 -> a if 0 and 1 are integral types. */
6106 (if (integer_onep (@1))
6107 (convert (convert:boolean_type_node @0)))
6108 /* a ? -1 : 0 -> -a. */
6109 (if (INTEGRAL_TYPE_P (type) && integer_all_onesp (@1))
6110 (if (TYPE_PRECISION (type) == 1)
6111 /* For signed 1-bit precision just cast bool to the type. */
6112 (convert (convert:boolean_type_node @0))
6113 (if (TREE_CODE (type) == BOOLEAN_TYPE)
6114 (with {
6115 tree intt = build_nonstandard_integer_type (TYPE_PRECISION (type),
6116 TYPE_UNSIGNED (type));
6117 }
6118 (convert (negate (convert:intt (convert:boolean_type_node @0)))))
6119 (negate (convert:type (convert:boolean_type_node @0))))))
6120 /* a ? powerof2cst : 0 -> a << (log2(powerof2cst)) */
6121 (if (INTEGRAL_TYPE_P (type) && integer_pow2p (@1))
6122 (with {
6123 tree shift = build_int_cst (integer_type_node, tree_log2 (@1));
6124 }
6125 (lshift (convert (convert:boolean_type_node @0)) { shift; })))))
6126 (if (integer_zerop (@1))
6127 (switch
6128 /* a ? 0 : 1 -> !a. */
6129 (if (integer_onep (@2))
6130 (convert (bit_xor (convert:boolean_type_node @0) { boolean_true_node; })))
6131 /* a ? 0 : -1 -> -(!a). */
6132 (if (INTEGRAL_TYPE_P (type) && integer_all_onesp (@2))
6133 (if (TYPE_PRECISION (type) == 1)
6134 /* For signed 1-bit precision just cast bool to the type. */
6135 (convert (bit_xor (convert:boolean_type_node @0) { boolean_true_node; }))
6136 (if (TREE_CODE (type) == BOOLEAN_TYPE)
6137 (with {
6138 tree intt = build_nonstandard_integer_type (TYPE_PRECISION (type),
6139 TYPE_UNSIGNED (type));
6140 }
6141 (convert (negate (convert:intt (bit_xor (convert:boolean_type_node @0)
6142 { boolean_true_node; })))))
6143 (negate (convert:type (bit_xor (convert:boolean_type_node @0)
6144 { boolean_true_node; }))))))
6145 /* a ? 0 : powerof2cst -> (!a) << (log2(powerof2cst)) */
6146 (if (INTEGRAL_TYPE_P (type) && integer_pow2p (@2))
6147 (with {
6148 tree shift = build_int_cst (integer_type_node, tree_log2 (@2));
6149 }
6150 (lshift (convert (bit_xor (convert:boolean_type_node @0)
6151 { boolean_true_node; })) { shift; })))))))
6152
6153 /* (a > 1) ? 0 : (cast)a is the same as (cast)(a == 1)
6154 for unsigned types. */
6155 (simplify
6156 (cond (gt @0 integer_onep@1) integer_zerop (convert? @2))
6157 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
6158 && bitwise_equal_p (@0, @2))
6159 (convert (eq @0 @1))
6160 )
6161 )
6162
6163 /* (a <= 1) & (cast)a is the same as (cast)(a == 1)
6164 for unsigned types. */
6165 (simplify
6166 (bit_and:c (convert1? (le @0 integer_onep@1)) (convert2? @2))
6167 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
6168 && bitwise_equal_p (@0, @2))
6169 (convert (eq @0 @1))
6170 )
6171 )
6172
6173 /* `(a == CST) & a` can be simplified to `0` or `(a == CST)` depending
6174 on the first bit of the CST. */
6175 (simplify
6176 (bit_and:c (convert@2 (eq @0 INTEGER_CST@1)) (convert? @0))
6177 (if ((wi::to_wide (@1) & 1) != 0)
6178 @2
6179 { build_zero_cst (type); }))
6180
6181 /* Optimize
6182 # x_5 in range [cst1, cst2] where cst2 = cst1 + 1
6183 x_5 == cstN ? cst4 : cst3
6184 # op is == or != and N is 1 or 2
6185 to r_6 = x_5 + (min (cst3, cst4) - cst1) or
6186 r_6 = (min (cst3, cst4) + cst1) - x_5 depending on op, N and which
6187 of cst3 and cst4 is smaller.
6188 This was originally done by two_value_replacement in phiopt (PR 88676). */
6189 (for eqne (ne eq)
6190 (simplify
6191 (cond (eqne SSA_NAME@0 INTEGER_CST@1) INTEGER_CST@2 INTEGER_CST@3)
6192 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6193 && INTEGRAL_TYPE_P (type)
6194 && (wi::to_widest (@2) + 1 == wi::to_widest (@3)
6195 || wi::to_widest (@2) == wi::to_widest (@3) + 1))
6196 (with {
6197 int_range_max r;
6198 get_range_query (cfun)->range_of_expr (r, @0);
6199 if (r.undefined_p ())
6200 r.set_varying (TREE_TYPE (@0));
6201
6202 wide_int min = r.lower_bound ();
6203 wide_int max = r.upper_bound ();
6204 }
6205 (if (min + 1 == max
6206 && (wi::to_wide (@1) == min
6207 || wi::to_wide (@1) == max))
6208 (with {
6209 tree arg0 = @2, arg1 = @3;
6210 tree type1;
6211 if ((eqne == EQ_EXPR) ^ (wi::to_wide (@1) == min))
6212 std::swap (arg0, arg1);
6213 if (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type))
6214 type1 = TREE_TYPE (@0);
6215 else
6216 type1 = type;
6217 auto prec = TYPE_PRECISION (type1);
6218 auto unsign = TYPE_UNSIGNED (type1);
6219 if (TREE_CODE (type1) == BOOLEAN_TYPE)
6220 type1 = build_nonstandard_integer_type (prec, unsign);
6221 min = wide_int::from (min, prec,
6222 TYPE_SIGN (TREE_TYPE (@0)));
6223 wide_int a = wide_int::from (wi::to_wide (arg0), prec,
6224 TYPE_SIGN (type));
6225 enum tree_code code;
6226 wi::overflow_type ovf;
6227 if (tree_int_cst_lt (arg0, arg1))
6228 {
6229 code = PLUS_EXPR;
6230 a -= min;
6231 if (!unsign)
6232 {
6233 /* lhs is known to be in range [min, min+1] and we want to add a
6234 to it. Check if that operation can overflow for those 2 values
6235 and if yes, force unsigned type. */
6236 wi::add (min + (wi::neg_p (a) ? 0 : 1), a, SIGNED, &ovf);
6237 if (ovf)
6238 type1 = unsigned_type_for (type1);
6239 }
6240 }
6241 else
6242 {
6243 code = MINUS_EXPR;
6244 a += min;
6245 if (!unsign)
6246 {
6247 /* lhs is known to be in range [min, min+1] and we want to subtract
6248 it from a. Check if that operation can overflow for those 2
6249 values and if yes, force unsigned type. */
6250 wi::sub (a, min + (wi::neg_p (min) ? 0 : 1), SIGNED, &ovf);
6251 if (ovf)
6252 type1 = unsigned_type_for (type1);
6253 }
6254 }
6255 tree arg = wide_int_to_tree (type1, a);
6256 }
6257 (if (code == PLUS_EXPR)
6258 (convert (plus (convert:type1 @0) { arg; }))
6259 (convert (minus { arg; } (convert:type1 @0))))))))))
6260 #endif
6261
6262 (simplify
6263 (convert (cond@0 @1 INTEGER_CST@2 INTEGER_CST@3))
6264 (if (INTEGRAL_TYPE_P (type)
6265 && INTEGRAL_TYPE_P (TREE_TYPE (@0)))
6266 (cond @1 (convert @2) (convert @3))))
6267
6268 /* Simplification moved from fold_cond_expr_with_comparison. It may also
6269 be extended. */
6270 /* This pattern implements two kinds simplification:
6271
6272 Case 1)
6273 (cond (cmp (convert1? x) c1) (convert2? x) c2) -> (minmax (x c)) if:
6274 1) Conversions are type widening from smaller type.
6275 2) Const c1 equals to c2 after canonicalizing comparison.
6276 3) Comparison has tree code LT, LE, GT or GE.
6277 This specific pattern is needed when (cmp (convert x) c) may not
6278 be simplified by comparison patterns because of multiple uses of
6279 x. It also makes sense here because simplifying across multiple
6280 referred var is always benefitial for complicated cases.
6281
6282 Case 2)
6283 (cond (eq (convert1? x) c1) (convert2? x) c2) -> (cond (eq x c1) c1 c2). */
6284 (for cmp (lt le gt ge eq ne)
6285 (simplify
6286 (cond (cmp (convert1? @1) INTEGER_CST@3) (convert2? @1) INTEGER_CST@2)
6287 (with
6288 {
6289 tree from_type = TREE_TYPE (@1);
6290 tree c1_type = TREE_TYPE (@3), c2_type = TREE_TYPE (@2);
6291 enum tree_code code = ERROR_MARK;
6292
6293 if (INTEGRAL_TYPE_P (from_type)
6294 && int_fits_type_p (@2, from_type)
6295 && (types_match (c1_type, from_type)
6296 || (TYPE_PRECISION (c1_type) > TYPE_PRECISION (from_type)
6297 && (TYPE_UNSIGNED (from_type)
6298 || TYPE_SIGN (c1_type) == TYPE_SIGN (from_type))))
6299 && (types_match (c2_type, from_type)
6300 || (TYPE_PRECISION (c2_type) > TYPE_PRECISION (from_type)
6301 && (TYPE_UNSIGNED (from_type)
6302 || TYPE_SIGN (c2_type) == TYPE_SIGN (from_type)))))
6303 {
6304 if (cmp != EQ_EXPR)
6305 code = minmax_from_comparison (cmp, @1, @3, @1, @2);
6306 /* Can do A == C1 ? A : C2 -> A == C1 ? C1 : C2? */
6307 else if (int_fits_type_p (@3, from_type))
6308 code = EQ_EXPR;
6309 }
6310 }
6311 (if (code == MAX_EXPR)
6312 (convert (max @1 (convert @2)))
6313 (if (code == MIN_EXPR)
6314 (convert (min @1 (convert @2)))
6315 (if (code == EQ_EXPR)
6316 (convert (cond (eq @1 (convert @3))
6317 (convert:from_type @3) (convert:from_type @2)))))))))
6318
6319 /* (cond (cmp (convert? x) c1) (op x c2) c3) -> (op (minmax x c1) c2) if:
6320
6321 1) OP is PLUS or MINUS.
6322 2) CMP is LT, LE, GT or GE.
6323 3) C3 == (C1 op C2), and computation doesn't have undefined behavior.
6324
6325 This pattern also handles special cases like:
6326
6327 A) Operand x is a unsigned to signed type conversion and c1 is
6328 integer zero. In this case,
6329 (signed type)x < 0 <=> x > MAX_VAL(signed type)
6330 (signed type)x >= 0 <=> x <= MAX_VAL(signed type)
6331 B) Const c1 may not equal to (C3 op' C2). In this case we also
6332 check equality for (c1+1) and (c1-1) by adjusting comparison
6333 code.
6334
6335 TODO: Though signed type is handled by this pattern, it cannot be
6336 simplified at the moment because C standard requires additional
6337 type promotion. In order to match&simplify it here, the IR needs
6338 to be cleaned up by other optimizers, i.e, VRP. */
6339 (for op (plus minus)
6340 (for cmp (lt le gt ge)
6341 (simplify
6342 (cond (cmp (convert? @X) INTEGER_CST@1) (op @X INTEGER_CST@2) INTEGER_CST@3)
6343 (with { tree from_type = TREE_TYPE (@X), to_type = TREE_TYPE (@1); }
6344 (if (types_match (from_type, to_type)
6345 /* Check if it is special case A). */
6346 || (TYPE_UNSIGNED (from_type)
6347 && !TYPE_UNSIGNED (to_type)
6348 && TYPE_PRECISION (from_type) == TYPE_PRECISION (to_type)
6349 && integer_zerop (@1)
6350 && (cmp == LT_EXPR || cmp == GE_EXPR)))
6351 (with
6352 {
6353 wi::overflow_type overflow = wi::OVF_NONE;
6354 enum tree_code code, cmp_code = cmp;
6355 wide_int real_c1;
6356 wide_int c1 = wi::to_wide (@1);
6357 wide_int c2 = wi::to_wide (@2);
6358 wide_int c3 = wi::to_wide (@3);
6359 signop sgn = TYPE_SIGN (from_type);
6360
6361 /* Handle special case A), given x of unsigned type:
6362 ((signed type)x < 0) <=> (x > MAX_VAL(signed type))
6363 ((signed type)x >= 0) <=> (x <= MAX_VAL(signed type)) */
6364 if (!types_match (from_type, to_type))
6365 {
6366 if (cmp_code == LT_EXPR)
6367 cmp_code = GT_EXPR;
6368 if (cmp_code == GE_EXPR)
6369 cmp_code = LE_EXPR;
6370 c1 = wi::max_value (to_type);
6371 }
6372 /* To simplify this pattern, we require c3 = (c1 op c2). Here we
6373 compute (c3 op' c2) and check if it equals to c1 with op' being
6374 the inverted operator of op. Make sure overflow doesn't happen
6375 if it is undefined. */
6376 if (op == PLUS_EXPR)
6377 real_c1 = wi::sub (c3, c2, sgn, &overflow);
6378 else
6379 real_c1 = wi::add (c3, c2, sgn, &overflow);
6380
6381 code = cmp_code;
6382 if (!overflow || !TYPE_OVERFLOW_UNDEFINED (from_type))
6383 {
6384 /* Check if c1 equals to real_c1. Boundary condition is handled
6385 by adjusting comparison operation if necessary. */
6386 if (!wi::cmp (wi::sub (real_c1, 1, sgn, &overflow), c1, sgn)
6387 && !overflow)
6388 {
6389 /* X <= Y - 1 equals to X < Y. */
6390 if (cmp_code == LE_EXPR)
6391 code = LT_EXPR;
6392 /* X > Y - 1 equals to X >= Y. */
6393 if (cmp_code == GT_EXPR)
6394 code = GE_EXPR;
6395 }
6396 if (!wi::cmp (wi::add (real_c1, 1, sgn, &overflow), c1, sgn)
6397 && !overflow)
6398 {
6399 /* X < Y + 1 equals to X <= Y. */
6400 if (cmp_code == LT_EXPR)
6401 code = LE_EXPR;
6402 /* X >= Y + 1 equals to X > Y. */
6403 if (cmp_code == GE_EXPR)
6404 code = GT_EXPR;
6405 }
6406 if (code != cmp_code || !wi::cmp (real_c1, c1, sgn))
6407 {
6408 if (cmp_code == LT_EXPR || cmp_code == LE_EXPR)
6409 code = MIN_EXPR;
6410 if (cmp_code == GT_EXPR || cmp_code == GE_EXPR)
6411 code = MAX_EXPR;
6412 }
6413 }
6414 }
6415 (if (code == MAX_EXPR)
6416 (op (max @X { wide_int_to_tree (from_type, real_c1); })
6417 { wide_int_to_tree (from_type, c2); })
6418 (if (code == MIN_EXPR)
6419 (op (min @X { wide_int_to_tree (from_type, real_c1); })
6420 { wide_int_to_tree (from_type, c2); })))))))))
6421
6422 #if GIMPLE
6423 /* A >= B ? A : B -> max (A, B) and friends. The code is still
6424 in fold_cond_expr_with_comparison for GENERIC folding with
6425 some extra constraints. */
6426 (for cmp (eq ne le lt unle unlt ge gt unge ungt uneq ltgt)
6427 (simplify
6428 (cond (cmp:c (nop_convert1?@c0 @0) (nop_convert2?@c1 @1))
6429 (convert3? @0) (convert4? @1))
6430 (if (!HONOR_SIGNED_ZEROS (type)
6431 && (/* Allow widening conversions of the compare operands as data. */
6432 (INTEGRAL_TYPE_P (type)
6433 && types_match (TREE_TYPE (@c0), TREE_TYPE (@0))
6434 && types_match (TREE_TYPE (@c1), TREE_TYPE (@1))
6435 && TYPE_PRECISION (TREE_TYPE (@0)) <= TYPE_PRECISION (type)
6436 && TYPE_PRECISION (TREE_TYPE (@1)) <= TYPE_PRECISION (type))
6437 /* Or sign conversions for the comparison. */
6438 || (types_match (type, TREE_TYPE (@0))
6439 && types_match (type, TREE_TYPE (@1)))))
6440 (switch
6441 (if (cmp == EQ_EXPR)
6442 (if (VECTOR_TYPE_P (type))
6443 (view_convert @c1)
6444 (convert @c1)))
6445 (if (cmp == NE_EXPR)
6446 (if (VECTOR_TYPE_P (type))
6447 (view_convert @c0)
6448 (convert @c0)))
6449 (if (cmp == LE_EXPR || cmp == UNLE_EXPR || cmp == LT_EXPR || cmp == UNLT_EXPR)
6450 (if (!HONOR_NANS (type))
6451 (if (VECTOR_TYPE_P (type))
6452 (view_convert (min @c0 @c1))
6453 (convert (min @c0 @c1)))))
6454 (if (cmp == GE_EXPR || cmp == UNGE_EXPR || cmp == GT_EXPR || cmp == UNGT_EXPR)
6455 (if (!HONOR_NANS (type))
6456 (if (VECTOR_TYPE_P (type))
6457 (view_convert (max @c0 @c1))
6458 (convert (max @c0 @c1)))))
6459 (if (cmp == UNEQ_EXPR)
6460 (if (!HONOR_NANS (type))
6461 (if (VECTOR_TYPE_P (type))
6462 (view_convert @c1)
6463 (convert @c1))))
6464 (if (cmp == LTGT_EXPR)
6465 (if (!HONOR_NANS (type))
6466 (if (VECTOR_TYPE_P (type))
6467 (view_convert @c0)
6468 (convert @c0))))))))
6469
6470 /* This is for VEC_COND_EXPR
6471 Optimize A < B ? A : B to MIN (A, B)
6472 A > B ? A : B to MAX (A, B). */
6473 (for cmp (lt le ungt unge gt ge unlt unle)
6474 minmax (min min min min max max max max)
6475 MINMAX (MIN_EXPR MIN_EXPR MIN_EXPR MIN_EXPR MAX_EXPR MAX_EXPR MAX_EXPR MAX_EXPR)
6476 (simplify
6477 (vec_cond (cmp @0 @1) @0 @1)
6478 (if (VECTOR_INTEGER_TYPE_P (type)
6479 && target_supports_op_p (type, MINMAX, optab_vector))
6480 (minmax @0 @1))))
6481
6482 (for cmp (lt le ungt unge gt ge unlt unle)
6483 minmax (max max max max min min min min)
6484 MINMAX (MAX_EXPR MAX_EXPR MAX_EXPR MAX_EXPR MIN_EXPR MIN_EXPR MIN_EXPR MIN_EXPR)
6485 (simplify
6486 (vec_cond (cmp @0 @1) @1 @0)
6487 (if (VECTOR_INTEGER_TYPE_P (type)
6488 && target_supports_op_p (type, MINMAX, optab_vector))
6489 (minmax @0 @1))))
6490
6491 /* Try to optimize x < 0 ? -1 : 0 into (signed) x >> 31
6492 and x < 0 ? 1 : 0 into (unsigned) x >> 31. */
6493 (simplify
6494 (vec_cond (lt @0 integer_zerop) integer_all_onesp integer_zerop)
6495 (if (VECTOR_INTEGER_TYPE_P (TREE_TYPE (@0))
6496 && !TYPE_UNSIGNED (TREE_TYPE (@0))
6497 && tree_nop_conversion_p (type, TREE_TYPE (@0))
6498 && target_supports_op_p (TREE_TYPE (@0), RSHIFT_EXPR, optab_scalar))
6499 (with
6500 {
6501 unsigned int prec = element_precision (TREE_TYPE (@0));
6502 }
6503 (view_convert
6504 (rshift @0 { build_int_cst (integer_type_node, prec - 1);})))))
6505
6506 (simplify
6507 (vec_cond (lt @0 integer_zerop) integer_onep integer_zerop)
6508 (if (VECTOR_INTEGER_TYPE_P (TREE_TYPE (@0))
6509 && !TYPE_UNSIGNED (TREE_TYPE (@0))
6510 && tree_nop_conversion_p (type, TREE_TYPE (@0)))
6511 (with
6512 {
6513 unsigned int prec = element_precision (TREE_TYPE (@0));
6514 tree utype = unsigned_type_for (TREE_TYPE (@0));
6515 }
6516 (if (target_supports_op_p (utype, RSHIFT_EXPR, optab_scalar))
6517 (view_convert
6518 (rshift (view_convert:utype @0)
6519 { build_int_cst (integer_type_node, prec - 1);}))))))
6520 #endif
6521
6522 (for cnd (cond vec_cond)
6523 /* (a != b) ? (a - b) : 0 -> (a - b) */
6524 (simplify
6525 (cnd (ne:c @0 @1) (minus@2 @0 @1) integer_zerop)
6526 @2)
6527 /* (a != b) ? (a ^ b) : 0 -> (a ^ b) */
6528 (simplify
6529 (cnd (ne:c @0 @1) (bit_xor@2 @0 @1) integer_zerop)
6530 @2)
6531 /* (a != b) ? (a & b) : a -> (a & b) */
6532 /* (a != b) ? (a | b) : a -> (a | b) */
6533 /* (a != b) ? min(a,b) : a -> min(a,b) */
6534 /* (a != b) ? max(a,b) : a -> max(a,b) */
6535 (for op (bit_and bit_ior min max)
6536 (simplify
6537 (cnd (ne:c @0 @1) (op:c@2 @0 @1) @0)
6538 @2))
6539 /* (a != b) ? (a * b) : (a * a) -> (a * b) */
6540 /* (a != b) ? (a + b) : (a + a) -> (a + b) */
6541 (for op (mult plus)
6542 (simplify
6543 (cnd (ne:c @0 @1) (op@2 @0 @1) (op @0 @0))
6544 (if (ANY_INTEGRAL_TYPE_P (type))
6545 @2)))
6546 /* (a != b) ? (a + b) : (2 * a) -> (a + b) */
6547 (simplify
6548 (cnd (ne:c @0 @1) (plus:c@2 @0 @1) (mult @0 uniform_integer_cst_p@3))
6549 (if (wi::to_wide (uniform_integer_cst_p (@3)) == 2)
6550 @2))
6551 )
6552
6553 /* These was part of minmax phiopt. */
6554 /* Optimize (a CMP b) ? minmax<a, c> : minmax<b, c>
6555 to minmax<min/max<a, b>, c> */
6556 (for minmax (min max)
6557 (for cmp (lt le gt ge ne)
6558 (simplify
6559 (cond (cmp:c @1 @3) (minmax:c @1 @4) (minmax:c @2 @4))
6560 (with
6561 {
6562 tree_code code = minmax_from_comparison (cmp, @1, @2, @1, @3);
6563 }
6564 (if (code == MIN_EXPR)
6565 (minmax (min @1 @2) @4)
6566 (if (code == MAX_EXPR)
6567 (minmax (max @1 @2) @4)))))))
6568
6569 /* Optimize (a CMP CST1) ? max<a,CST2> : a */
6570 (for cmp (gt ge lt le)
6571 minmax (min min max max)
6572 (simplify
6573 (cond (cmp:c @0 @1) (minmax:c@2 @0 @3) @4)
6574 (with
6575 {
6576 tree_code code = minmax_from_comparison (cmp, @0, @1, @0, @4);
6577 }
6578 (if ((cmp == LT_EXPR || cmp == LE_EXPR)
6579 && code == MIN_EXPR
6580 && integer_nonzerop (fold_build2 (LE_EXPR, boolean_type_node, @3, @4)))
6581 (min @2 @4)
6582 (if ((cmp == GT_EXPR || cmp == GE_EXPR)
6583 && code == MAX_EXPR
6584 && integer_nonzerop (fold_build2 (GE_EXPR, boolean_type_node, @3, @4)))
6585 (max @2 @4))))))
6586
6587 #if GIMPLE
6588 /* These patterns should be after min/max detection as simplifications
6589 of `(type)(zero_one ==/!= 0)` to `(type)(zero_one)`
6590 and `(type)(zero_one^1)` are not done yet. See PR 110637.
6591 Even without those, reaching min/max/and/ior faster is better. */
6592 (simplify
6593 (cond @0 zero_one_valued_p@1 zero_one_valued_p@2)
6594 (switch
6595 /* bool0 ? bool1 : 0 -> bool0 & bool1 */
6596 (if (integer_zerop (@2))
6597 (bit_and (convert @0) @1))
6598 /* bool0 ? 0 : bool2 -> (bool0^1) & bool2 */
6599 (if (integer_zerop (@1))
6600 (bit_and (bit_xor (convert @0) { build_one_cst (type); } ) @2))
6601 /* bool0 ? 1 : bool2 -> bool0 | bool2 */
6602 (if (integer_onep (@1))
6603 (bit_ior (convert @0) @2))
6604 /* bool0 ? bool1 : 1 -> (bool0^1) | bool1 */
6605 (if (integer_onep (@2))
6606 (bit_ior (bit_xor (convert @0) @2) @1))
6607 )
6608 )
6609 #endif
6610
6611 /* X != C1 ? -X : C2 simplifies to -X when -C1 == C2. */
6612 (simplify
6613 (cond (ne @0 INTEGER_CST@1) (negate@3 @0) INTEGER_CST@2)
6614 (if (!TYPE_SATURATING (type)
6615 && (TYPE_OVERFLOW_WRAPS (type)
6616 || !wi::only_sign_bit_p (wi::to_wide (@1)))
6617 && wi::eq_p (wi::neg (wi::to_wide (@1)), wi::to_wide (@2)))
6618 @3))
6619
6620 /* X != C1 ? ~X : C2 simplifies to ~X when ~C1 == C2. */
6621 (simplify
6622 (cond (ne @0 INTEGER_CST@1) (bit_not@3 @0) INTEGER_CST@2)
6623 (if (wi::eq_p (wi::bit_not (wi::to_wide (@1)), wi::to_wide (@2)))
6624 @3))
6625
6626 /* X != C1 ? abs(X) : C2 simplifies to abs(x) when abs(C1) == C2. */
6627 (for op (abs absu)
6628 (simplify
6629 (cond (ne @0 INTEGER_CST@1) (op@3 @0) INTEGER_CST@2)
6630 (if (wi::abs (wi::to_wide (@1)) == wi::to_wide (@2))
6631 (if (op != ABSU_EXPR && wi::only_sign_bit_p (wi::to_wide (@1)))
6632 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
6633 (convert (absu:utype @0)))
6634 @3))))
6635
6636 /* X > Positive ? X : ABS(X) -> ABS(X) */
6637 /* X >= Positive ? X : ABS(X) -> ABS(X) */
6638 /* X == Positive ? X : ABS(X) -> ABS(X) */
6639 (for cmp (eq gt ge)
6640 (simplify
6641 (cond (cmp:c @0 tree_expr_nonnegative_p@1) @0 (abs@3 @0))
6642 (if (INTEGRAL_TYPE_P (type))
6643 @3)))
6644
6645 /* X == Positive ? Positive : ABS(X) -> ABS(X) */
6646 (simplify
6647 (cond (eq:c @0 tree_expr_nonnegative_p@1) @1 (abs@3 @0))
6648 (if (INTEGRAL_TYPE_P (type))
6649 @3))
6650
6651 /* (X + 1) > Y ? -X : 1 simplifies to X >= Y ? -X : 1 when
6652 X is unsigned, as when X + 1 overflows, X is -1, so -X == 1. */
6653 (simplify
6654 (cond (gt (plus @0 integer_onep) @1) (negate @0) integer_onep@2)
6655 (if (TYPE_UNSIGNED (type))
6656 (cond (ge @0 @1) (negate @0) @2)))
6657
6658 (for cnd (cond vec_cond)
6659 /* A ? B : (A ? X : C) -> A ? B : C. */
6660 (simplify
6661 (cnd @0 (cnd @0 @1 @2) @3)
6662 (cnd @0 @1 @3))
6663 (simplify
6664 (cnd @0 @1 (cnd @0 @2 @3))
6665 (cnd @0 @1 @3))
6666 /* A ? B : (!A ? C : X) -> A ? B : C. */
6667 /* ??? This matches embedded conditions open-coded because genmatch
6668 would generate matching code for conditions in separate stmts only.
6669 The following is still important to merge then and else arm cases
6670 from if-conversion. */
6671 (simplify
6672 (cnd @0 @1 (cnd @2 @3 @4))
6673 (if (inverse_conditions_p (@0, @2))
6674 (cnd @0 @1 @3)))
6675 (simplify
6676 (cnd @0 (cnd @1 @2 @3) @4)
6677 (if (inverse_conditions_p (@0, @1))
6678 (cnd @0 @3 @4)))
6679
6680 /* A ? B : B -> B. */
6681 (simplify
6682 (cnd @0 @1 @1)
6683 @1)
6684
6685 /* !A ? B : C -> A ? C : B. */
6686 (simplify
6687 (cnd (logical_inverted_value truth_valued_p@0) @1 @2)
6688 /* For CONDs, don't handle signed values here. */
6689 (if (cnd == VEC_COND_EXPR
6690 || TYPE_UNSIGNED (TREE_TYPE (@0)))
6691 (cnd @0 @2 @1))))
6692
6693 /* abs/negative simplifications moved from fold_cond_expr_with_comparison.
6694
6695 None of these transformations work for modes with signed
6696 zeros. If A is +/-0, the first two transformations will
6697 change the sign of the result (from +0 to -0, or vice
6698 versa). The last four will fix the sign of the result,
6699 even though the original expressions could be positive or
6700 negative, depending on the sign of A.
6701
6702 Note that all these transformations are correct if A is
6703 NaN, since the two alternatives (A and -A) are also NaNs. */
6704
6705 (for cnd (cond vec_cond)
6706 /* A == 0 ? A : -A same as -A */
6707 (for cmp (eq uneq)
6708 (simplify
6709 (cnd (cmp @0 zerop) @2 (negate@1 @2))
6710 (if (!HONOR_SIGNED_ZEROS (type)
6711 && bitwise_equal_p (@0, @2))
6712 @1))
6713 (simplify
6714 (cnd (cmp @0 zerop) zerop (negate@1 @2))
6715 (if (!HONOR_SIGNED_ZEROS (type)
6716 && bitwise_equal_p (@0, @2))
6717 @1))
6718 )
6719 /* A != 0 ? A : -A same as A */
6720 (for cmp (ne ltgt)
6721 (simplify
6722 (cnd (cmp @0 zerop) @1 (negate @1))
6723 (if (!HONOR_SIGNED_ZEROS (type)
6724 && bitwise_equal_p (@0, @1))
6725 @1))
6726 (simplify
6727 (cnd (cmp @0 zerop) @1 integer_zerop)
6728 (if (!HONOR_SIGNED_ZEROS (type)
6729 && bitwise_equal_p (@0, @1))
6730 @1))
6731 )
6732 /* (type)A >=/> 0 ? A : -A same as abs (A) */
6733 (for cmp (ge gt)
6734 (simplify
6735 (cnd (cmp (convert?@0 @1) zerop) @2 (negate @2))
6736 (if (!HONOR_SIGNED_ZEROS (TREE_TYPE (@1))
6737 /* Support SEXT of @0 only. */
6738 && !TYPE_UNSIGNED (TREE_TYPE (@1))
6739 && element_precision (@1)
6740 <= element_precision (@0)
6741 && bitwise_equal_p (@1, @2)
6742 && (!VECTOR_TYPE_P (type)
6743 || target_supports_op_p (type, ABS_EXPR, optab_vector)))
6744 (if (TYPE_UNSIGNED (TREE_TYPE (@2)))
6745 (with {
6746 tree stype = signed_type_for (TREE_TYPE (@2));
6747 }
6748 (if (types_match (@0, stype))
6749 (absu @0)
6750 (absu (convert:stype @2))))
6751 (abs @2)))))
6752 /* (type)A <=/< 0 ? A : -A same as -abs (A) */
6753 (for cmp (le lt)
6754 (simplify
6755 (cnd (cmp (convert?@0 @1) zerop) @2 (negate @2))
6756 (if (!HONOR_SIGNED_ZEROS (TREE_TYPE (@1))
6757 /* Support SEXT of @0 only. */
6758 && !TYPE_UNSIGNED (TREE_TYPE (@1))
6759 && element_precision (@1)
6760 <= element_precision (@0)
6761 && bitwise_equal_p (@1, @2)
6762 && (!VECTOR_TYPE_P (type)
6763 || (target_supports_op_p (type, ABS_EXPR, optab_vector)
6764 && target_supports_op_p (type, NEGATE_EXPR, optab_vector))))
6765 (if ((ANY_INTEGRAL_TYPE_P (TREE_TYPE (@2))
6766 && !TYPE_OVERFLOW_WRAPS (TREE_TYPE (@2)))
6767 || TYPE_UNSIGNED (TREE_TYPE (@2)))
6768 (with {
6769 tree stype = signed_type_for (TREE_TYPE (@2));
6770 tree utype = unsigned_type_for (TREE_TYPE (@2));
6771 }
6772 (if (types_match (@0, stype))
6773 (convert (negate (absu:utype @0)))
6774 (convert (negate (absu:utype (convert:stype @2))))))
6775 (convert (negate (abs @2))))))
6776 )
6777
6778 /* (A - B) == 0 ? (A - B) : (B - A) same as (B - A) */
6779 (for cmp (eq uneq)
6780 (simplify
6781 (cnd (cmp (minus@0 @1 @2) zerop) @0 (minus@3 @2 @1))
6782 (if (!HONOR_SIGNED_ZEROS (type))
6783 @3))
6784 (simplify
6785 (cnd (cmp (minus@0 @1 @2) integer_zerop) integer_zerop (minus@3 @2 @1))
6786 @3)
6787 )
6788 /* (A - B) != 0 ? (A - B) : (B - A) same as (A - B) */
6789 (for cmp (ne ltgt)
6790 (simplify
6791 (cnd (cmp (minus@0 @1 @2) zerop) @0 (minus @2 @1))
6792 (if (!HONOR_SIGNED_ZEROS (type))
6793 @0))
6794 (simplify
6795 (cnd (cmp (minus@0 @1 @2) integer_zerop) @0 integer_zerop)
6796 @0)
6797 )
6798 /* (A - B) >=/> 0 ? (A - B) : (B - A) same as abs (A - B) */
6799 (for cmp (ge gt)
6800 (simplify
6801 (cnd (cmp (minus@0 @1 @2) zerop) @0 (minus @2 @1))
6802 (if (!HONOR_SIGNED_ZEROS (type)
6803 && !TYPE_UNSIGNED (type)
6804 && (!VECTOR_TYPE_P (type)
6805 || target_supports_op_p (type, ABS_EXPR, optab_vector)))
6806 (abs @0))))
6807 /* (A - B) <=/< 0 ? (A - B) : (B - A) same as -abs (A - B) */
6808 (for cmp (le lt)
6809 (simplify
6810 (cnd (cmp (minus@0 @1 @2) zerop) @0 (minus @2 @1))
6811 (if (!HONOR_SIGNED_ZEROS (type)
6812 && !TYPE_UNSIGNED (type))
6813 (if (ANY_INTEGRAL_TYPE_P (type)
6814 && !TYPE_OVERFLOW_WRAPS (type)
6815 && (!VECTOR_TYPE_P (type)
6816 || (target_supports_op_p (type, ABS_EXPR, optab_vector)
6817 && target_supports_op_p (type, NEGATE_EXPR, optab_vector))))
6818 (with {
6819 tree utype = unsigned_type_for (type);
6820 }
6821 (convert (negate (absu:utype @0))))
6822 (negate (abs @0)))))
6823 )
6824 )
6825
6826 /* -(type)!A -> (type)A - 1. */
6827 (simplify
6828 (negate (convert?:s (logical_inverted_value:s @0)))
6829 (if (INTEGRAL_TYPE_P (type)
6830 && TREE_CODE (type) != BOOLEAN_TYPE
6831 && TYPE_PRECISION (type) > 1
6832 && TREE_CODE (@0) == SSA_NAME
6833 && ssa_name_has_boolean_range (@0))
6834 (plus (convert:type @0) { build_all_ones_cst (type); })))
6835
6836 /* A + (B vcmp C ? 1 : 0) -> A - (B vcmp C ? -1 : 0), since vector comparisons
6837 return all -1 or all 0 results. */
6838 /* ??? We could instead convert all instances of the vec_cond to negate,
6839 but that isn't necessarily a win on its own. */
6840 (simplify
6841 (plus:c @3 (view_convert? (vec_cond:s @0 integer_each_onep@1 integer_zerop@2)))
6842 (if (VECTOR_TYPE_P (type)
6843 && known_eq (TYPE_VECTOR_SUBPARTS (type),
6844 TYPE_VECTOR_SUBPARTS (TREE_TYPE (@1)))
6845 && (TYPE_MODE (TREE_TYPE (type))
6846 == TYPE_MODE (TREE_TYPE (TREE_TYPE (@1)))))
6847 (minus @3 (view_convert (vec_cond @0 (negate @1) @2)))))
6848
6849 /* ... likewise A - (B vcmp C ? 1 : 0) -> A + (B vcmp C ? -1 : 0). */
6850 (simplify
6851 (minus @3 (view_convert? (vec_cond:s @0 integer_each_onep@1 integer_zerop@2)))
6852 (if (VECTOR_TYPE_P (type)
6853 && known_eq (TYPE_VECTOR_SUBPARTS (type),
6854 TYPE_VECTOR_SUBPARTS (TREE_TYPE (@1)))
6855 && (TYPE_MODE (TREE_TYPE (type))
6856 == TYPE_MODE (TREE_TYPE (TREE_TYPE (@1)))))
6857 (plus @3 (view_convert (vec_cond @0 (negate @1) @2)))))
6858
6859
6860 /* Simplifications of comparisons. */
6861
6862 /* See if we can reduce the magnitude of a constant involved in a
6863 comparison by changing the comparison code. This is a canonicalization
6864 formerly done by maybe_canonicalize_comparison_1. */
6865 (for cmp (le gt)
6866 acmp (lt ge)
6867 (simplify
6868 (cmp @0 uniform_integer_cst_p@1)
6869 (with { tree cst = uniform_integer_cst_p (@1); }
6870 (if (tree_int_cst_sgn (cst) == -1)
6871 (acmp @0 { build_uniform_cst (TREE_TYPE (@1),
6872 wide_int_to_tree (TREE_TYPE (cst),
6873 wi::to_wide (cst)
6874 + 1)); })))))
6875 (for cmp (ge lt)
6876 acmp (gt le)
6877 (simplify
6878 (cmp @0 uniform_integer_cst_p@1)
6879 (with { tree cst = uniform_integer_cst_p (@1); }
6880 (if (tree_int_cst_sgn (cst) == 1)
6881 (acmp @0 { build_uniform_cst (TREE_TYPE (@1),
6882 wide_int_to_tree (TREE_TYPE (cst),
6883 wi::to_wide (cst) - 1)); })))))
6884
6885 /* We can simplify a logical negation of a comparison to the
6886 inverted comparison. As we cannot compute an expression
6887 operator using invert_tree_comparison we have to simulate
6888 that with expression code iteration. */
6889 (for cmp (tcc_comparison)
6890 icmp (inverted_tcc_comparison)
6891 ncmp (inverted_tcc_comparison_with_nans)
6892 /* Ideally we'd like to combine the following two patterns
6893 and handle some more cases by using
6894 (logical_inverted_value (cmp @0 @1))
6895 here but for that genmatch would need to "inline" that.
6896 For now implement what forward_propagate_comparison did. */
6897 (simplify
6898 (bit_not (cmp @0 @1))
6899 (if (VECTOR_TYPE_P (type)
6900 || (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1))
6901 /* Comparison inversion may be impossible for trapping math,
6902 invert_tree_comparison will tell us. But we can't use
6903 a computed operator in the replacement tree thus we have
6904 to play the trick below. */
6905 (with { enum tree_code ic = invert_tree_comparison
6906 (cmp, HONOR_NANS (@0)); }
6907 (if (ic == icmp)
6908 (icmp @0 @1)
6909 (if (ic == ncmp)
6910 (ncmp @0 @1))))))
6911 (simplify
6912 (bit_xor (cmp @0 @1) integer_truep)
6913 (with { enum tree_code ic = invert_tree_comparison
6914 (cmp, HONOR_NANS (@0)); }
6915 (if (ic == icmp)
6916 (icmp @0 @1)
6917 (if (ic == ncmp)
6918 (ncmp @0 @1)))))
6919 /* The following bits are handled by fold_binary_op_with_conditional_arg. */
6920 (simplify
6921 (ne (cmp@2 @0 @1) integer_zerop)
6922 (if (types_match (type, TREE_TYPE (@2)))
6923 (cmp @0 @1)))
6924 (simplify
6925 (eq (cmp@2 @0 @1) integer_truep)
6926 (if (types_match (type, TREE_TYPE (@2)))
6927 (cmp @0 @1)))
6928 (simplify
6929 (ne (cmp@2 @0 @1) integer_truep)
6930 (if (types_match (type, TREE_TYPE (@2)))
6931 (with { enum tree_code ic = invert_tree_comparison
6932 (cmp, HONOR_NANS (@0)); }
6933 (if (ic == icmp)
6934 (icmp @0 @1)
6935 (if (ic == ncmp)
6936 (ncmp @0 @1))))))
6937 (simplify
6938 (eq (cmp@2 @0 @1) integer_zerop)
6939 (if (types_match (type, TREE_TYPE (@2)))
6940 (with { enum tree_code ic = invert_tree_comparison
6941 (cmp, HONOR_NANS (@0)); }
6942 (if (ic == icmp)
6943 (icmp @0 @1)
6944 (if (ic == ncmp)
6945 (ncmp @0 @1)))))))
6946
6947 /* Transform comparisons of the form X - Y CMP 0 to X CMP Y.
6948 ??? The transformation is valid for the other operators if overflow
6949 is undefined for the type, but performing it here badly interacts
6950 with the transformation in fold_cond_expr_with_comparison which
6951 attempts to synthetize ABS_EXPR. */
6952 (for cmp (eq ne)
6953 (for sub (minus pointer_diff)
6954 (simplify
6955 (cmp (sub@2 @0 @1) integer_zerop)
6956 (if (single_use (@2))
6957 (cmp @0 @1)))))
6958
6959 /* Simplify (x < 0) ^ (y < 0) to (x ^ y) < 0 and
6960 (x >= 0) ^ (y >= 0) to (x ^ y) < 0. */
6961 (for cmp (lt ge)
6962 (simplify
6963 (bit_xor (cmp:s @0 integer_zerop) (cmp:s @1 integer_zerop))
6964 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6965 && !TYPE_UNSIGNED (TREE_TYPE (@0))
6966 && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
6967 (lt (bit_xor @0 @1) { build_zero_cst (TREE_TYPE (@0)); }))))
6968 /* Simplify (x < 0) ^ (y >= 0) to (x ^ y) >= 0 and
6969 (x >= 0) ^ (y < 0) to (x ^ y) >= 0. */
6970 (simplify
6971 (bit_xor:c (lt:s @0 integer_zerop) (ge:s @1 integer_zerop))
6972 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6973 && !TYPE_UNSIGNED (TREE_TYPE (@0))
6974 && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
6975 (ge (bit_xor @0 @1) { build_zero_cst (TREE_TYPE (@0)); })))
6976
6977 /* Transform comparisons of the form X * C1 CMP 0 to X CMP 0 in the
6978 signed arithmetic case. That form is created by the compiler
6979 often enough for folding it to be of value. One example is in
6980 computing loop trip counts after Operator Strength Reduction. */
6981 (for cmp (simple_comparison)
6982 scmp (swapped_simple_comparison)
6983 (simplify
6984 (cmp (mult@3 @0 INTEGER_CST@1) integer_zerop@2)
6985 /* Handle unfolded multiplication by zero. */
6986 (if (integer_zerop (@1))
6987 (cmp @1 @2)
6988 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
6989 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
6990 && single_use (@3))
6991 /* If @1 is negative we swap the sense of the comparison. */
6992 (if (tree_int_cst_sgn (@1) < 0)
6993 (scmp @0 @2)
6994 (cmp @0 @2))))))
6995
6996 /* For integral types with undefined overflow fold
6997 x * C1 == C2 into x == C2 / C1 or false.
6998 If overflow wraps and C1 is odd, simplify to x == C2 / C1 in the ring
6999 Z / 2^n Z. */
7000 (for cmp (eq ne)
7001 (simplify
7002 (cmp (mult @0 INTEGER_CST@1) INTEGER_CST@2)
7003 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
7004 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
7005 && wi::to_wide (@1) != 0)
7006 (with { widest_int quot; }
7007 (if (wi::multiple_of_p (wi::to_widest (@2), wi::to_widest (@1),
7008 TYPE_SIGN (TREE_TYPE (@0)), &quot))
7009 (cmp @0 { wide_int_to_tree (TREE_TYPE (@0), quot); })
7010 { constant_boolean_node (cmp == NE_EXPR, type); }))
7011 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
7012 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))
7013 && (wi::bit_and (wi::to_wide (@1), 1) == 1))
7014 (cmp @0
7015 {
7016 tree itype = TREE_TYPE (@0);
7017 int p = TYPE_PRECISION (itype);
7018 wide_int m = wi::one (p + 1) << p;
7019 wide_int a = wide_int::from (wi::to_wide (@1), p + 1, UNSIGNED);
7020 wide_int i = wide_int::from (wi::mod_inv (a, m),
7021 p, TYPE_SIGN (itype));
7022 wide_int_to_tree (itype, wi::mul (i, wi::to_wide (@2)));
7023 })))))
7024
7025 /* Simplify comparison of something with itself. For IEEE
7026 floating-point, we can only do some of these simplifications. */
7027 (for cmp (eq ge le)
7028 (simplify
7029 (cmp @0 @0)
7030 (if (! FLOAT_TYPE_P (TREE_TYPE (@0))
7031 || ! tree_expr_maybe_nan_p (@0))
7032 { constant_boolean_node (true, type); }
7033 (if (cmp != EQ_EXPR
7034 /* With -ftrapping-math conversion to EQ loses an exception. */
7035 && (! FLOAT_TYPE_P (TREE_TYPE (@0))
7036 || ! flag_trapping_math))
7037 (eq @0 @0)))))
7038 (for cmp (ne gt lt)
7039 (simplify
7040 (cmp @0 @0)
7041 (if (cmp != NE_EXPR
7042 || ! FLOAT_TYPE_P (TREE_TYPE (@0))
7043 || ! tree_expr_maybe_nan_p (@0))
7044 { constant_boolean_node (false, type); })))
7045 (for cmp (unle unge uneq)
7046 (simplify
7047 (cmp @0 @0)
7048 { constant_boolean_node (true, type); }))
7049 (for cmp (unlt ungt)
7050 (simplify
7051 (cmp @0 @0)
7052 (unordered @0 @0)))
7053 (simplify
7054 (ltgt @0 @0)
7055 (if (!flag_trapping_math || !tree_expr_maybe_nan_p (@0))
7056 { constant_boolean_node (false, type); }))
7057 (simplify
7058 (bit_and:c (ordered @0 @1) (ne @0 @1))
7059 (ltgt @0 @1))
7060
7061 /* x == ~x -> false */
7062 /* x != ~x -> true */
7063 (for cmp (eq ne)
7064 (simplify
7065 (cmp:c (convert? @0) (convert? (maybe_bit_not @1)))
7066 (with { bool wascmp; }
7067 (if (types_match (TREE_TYPE (@0), TREE_TYPE (@1))
7068 && bitwise_inverted_equal_p (@0, @1, wascmp))
7069 { constant_boolean_node (cmp == NE_EXPR, type); }))))
7070
7071 /* Fold ~X op ~Y as Y op X. */
7072 (for cmp (simple_comparison)
7073 (simplify
7074 (cmp (nop_convert1?@4 (bit_not@2 @0)) (nop_convert2? (bit_not@3 @1)))
7075 (if (single_use (@2) && single_use (@3))
7076 (with { tree otype = TREE_TYPE (@4); }
7077 (cmp (convert:otype @1) (convert:otype @0))))))
7078
7079 /* Fold ~X op C as X op' ~C, where op' is the swapped comparison. */
7080 (for cmp (simple_comparison)
7081 scmp (swapped_simple_comparison)
7082 (simplify
7083 (cmp (nop_convert? (bit_not@2 @0)) CONSTANT_CLASS_P@1)
7084 (if (single_use (@2)
7085 && (TREE_CODE (@1) == INTEGER_CST || TREE_CODE (@1) == VECTOR_CST))
7086 (with { tree otype = TREE_TYPE (@1); }
7087 (scmp (convert:otype @0) (bit_not @1))))))
7088
7089 (for cmp (simple_comparison)
7090 (simplify
7091 (cmp @0 REAL_CST@1)
7092 /* IEEE doesn't distinguish +0 and -0 in comparisons. */
7093 (switch
7094 /* a CMP (-0) -> a CMP 0 */
7095 (if (REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@1)))
7096 (cmp @0 { build_real (TREE_TYPE (@1), dconst0); }))
7097 /* (-0) CMP b -> 0 CMP b. */
7098 (if (TREE_CODE (@0) == REAL_CST
7099 && REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@0)))
7100 (cmp { build_real (TREE_TYPE (@0), dconst0); } @1))
7101 /* x != NaN is always true, other ops are always false. */
7102 (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
7103 && (cmp == EQ_EXPR || cmp == NE_EXPR || !flag_trapping_math)
7104 && !tree_expr_signaling_nan_p (@1)
7105 && !tree_expr_maybe_signaling_nan_p (@0))
7106 { constant_boolean_node (cmp == NE_EXPR, type); })
7107 /* NaN != y is always true, other ops are always false. */
7108 (if (TREE_CODE (@0) == REAL_CST
7109 && REAL_VALUE_ISNAN (TREE_REAL_CST (@0))
7110 && (cmp == EQ_EXPR || cmp == NE_EXPR || !flag_trapping_math)
7111 && !tree_expr_signaling_nan_p (@0)
7112 && !tree_expr_signaling_nan_p (@1))
7113 { constant_boolean_node (cmp == NE_EXPR, type); })
7114 /* Fold comparisons against infinity. */
7115 (if (REAL_VALUE_ISINF (TREE_REAL_CST (@1))
7116 && MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (@1))))
7117 (with
7118 {
7119 REAL_VALUE_TYPE max;
7120 enum tree_code code = cmp;
7121 bool neg = REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1));
7122 if (neg)
7123 code = swap_tree_comparison (code);
7124 }
7125 (switch
7126 /* x > +Inf is always false, if we ignore NaNs or exceptions. */
7127 (if (code == GT_EXPR
7128 && !(HONOR_NANS (@0) && flag_trapping_math))
7129 { constant_boolean_node (false, type); })
7130 (if (code == LE_EXPR)
7131 /* x <= +Inf is always true, if we don't care about NaNs. */
7132 (if (! HONOR_NANS (@0))
7133 { constant_boolean_node (true, type); }
7134 /* x <= +Inf is the same as x == x, i.e. !isnan(x), but this loses
7135 an "invalid" exception. */
7136 (if (!flag_trapping_math)
7137 (eq @0 @0))))
7138 /* x == +Inf and x >= +Inf are always equal to x > DBL_MAX, but
7139 for == this introduces an exception for x a NaN. */
7140 (if ((code == EQ_EXPR && !(HONOR_NANS (@0) && flag_trapping_math))
7141 || code == GE_EXPR)
7142 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
7143 (if (neg)
7144 (lt @0 { build_real (TREE_TYPE (@0), max); })
7145 (gt @0 { build_real (TREE_TYPE (@0), max); }))))
7146 /* x < +Inf is always equal to x <= DBL_MAX. */
7147 (if (code == LT_EXPR)
7148 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
7149 (if (neg)
7150 (ge @0 { build_real (TREE_TYPE (@0), max); })
7151 (le @0 { build_real (TREE_TYPE (@0), max); }))))
7152 /* x != +Inf is always equal to !(x > DBL_MAX), but this introduces
7153 an exception for x a NaN so use an unordered comparison. */
7154 (if (code == NE_EXPR)
7155 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
7156 (if (! HONOR_NANS (@0))
7157 (if (neg)
7158 (ge @0 { build_real (TREE_TYPE (@0), max); })
7159 (le @0 { build_real (TREE_TYPE (@0), max); }))
7160 (if (neg)
7161 (unge @0 { build_real (TREE_TYPE (@0), max); })
7162 (unle @0 { build_real (TREE_TYPE (@0), max); }))))))))))
7163
7164 /* If this is a comparison of a real constant with a PLUS_EXPR
7165 or a MINUS_EXPR of a real constant, we can convert it into a
7166 comparison with a revised real constant as long as no overflow
7167 occurs when unsafe_math_optimizations are enabled. */
7168 (if (flag_unsafe_math_optimizations)
7169 (for op (plus minus)
7170 (simplify
7171 (cmp (op @0 REAL_CST@1) REAL_CST@2)
7172 (with
7173 {
7174 tree tem = const_binop (op == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR,
7175 TREE_TYPE (@1), @2, @1);
7176 }
7177 (if (tem && !TREE_OVERFLOW (tem))
7178 (cmp @0 { tem; }))))))
7179
7180 /* Likewise, we can simplify a comparison of a real constant with
7181 a MINUS_EXPR whose first operand is also a real constant, i.e.
7182 (c1 - x) < c2 becomes x > c1-c2. Reordering is allowed on
7183 floating-point types only if -fassociative-math is set. */
7184 (if (flag_associative_math)
7185 (simplify
7186 (cmp (minus REAL_CST@0 @1) REAL_CST@2)
7187 (with { tree tem = const_binop (MINUS_EXPR, TREE_TYPE (@1), @0, @2); }
7188 (if (tem && !TREE_OVERFLOW (tem))
7189 (cmp { tem; } @1)))))
7190
7191 /* Fold comparisons against built-in math functions. */
7192 (if (flag_unsafe_math_optimizations && ! flag_errno_math)
7193 (for sq (SQRT)
7194 (simplify
7195 (cmp (sq @0) REAL_CST@1)
7196 (switch
7197 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
7198 (switch
7199 /* sqrt(x) < y is always false, if y is negative. */
7200 (if (cmp == EQ_EXPR || cmp == LT_EXPR || cmp == LE_EXPR)
7201 { constant_boolean_node (false, type); })
7202 /* sqrt(x) > y is always true, if y is negative and we
7203 don't care about NaNs, i.e. negative values of x. */
7204 (if (cmp == NE_EXPR || !HONOR_NANS (@0))
7205 { constant_boolean_node (true, type); })
7206 /* sqrt(x) > y is the same as x >= 0, if y is negative. */
7207 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })))
7208 (if (real_equal (TREE_REAL_CST_PTR (@1), &dconst0))
7209 (switch
7210 /* sqrt(x) < 0 is always false. */
7211 (if (cmp == LT_EXPR)
7212 { constant_boolean_node (false, type); })
7213 /* sqrt(x) >= 0 is always true if we don't care about NaNs. */
7214 (if (cmp == GE_EXPR && !HONOR_NANS (@0))
7215 { constant_boolean_node (true, type); })
7216 /* sqrt(x) <= 0 -> x == 0. */
7217 (if (cmp == LE_EXPR)
7218 (eq @0 @1))
7219 /* Otherwise sqrt(x) cmp 0 -> x cmp 0. Here cmp can be >=, >,
7220 == or !=. In the last case:
7221
7222 (sqrt(x) != 0) == (NaN != 0) == true == (x != 0)
7223
7224 if x is negative or NaN. Due to -funsafe-math-optimizations,
7225 the results for other x follow from natural arithmetic. */
7226 (cmp @0 @1)))
7227 (if ((cmp == LT_EXPR
7228 || cmp == LE_EXPR
7229 || cmp == GT_EXPR
7230 || cmp == GE_EXPR)
7231 && !REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
7232 /* Give up for -frounding-math. */
7233 && !HONOR_SIGN_DEPENDENT_ROUNDING (TREE_TYPE (@0)))
7234 (with
7235 {
7236 REAL_VALUE_TYPE c2;
7237 enum tree_code ncmp = cmp;
7238 const real_format *fmt
7239 = REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (@0)));
7240 real_arithmetic (&c2, MULT_EXPR,
7241 &TREE_REAL_CST (@1), &TREE_REAL_CST (@1));
7242 real_convert (&c2, fmt, &c2);
7243 /* See PR91734: if c2 is inexact and sqrt(c2) < c (or sqrt(c2) >= c),
7244 then change LT_EXPR into LE_EXPR or GE_EXPR into GT_EXPR. */
7245 if (!REAL_VALUE_ISINF (c2))
7246 {
7247 tree c3 = fold_const_call (CFN_SQRT, TREE_TYPE (@0),
7248 build_real (TREE_TYPE (@0), c2));
7249 if (c3 == NULL_TREE || TREE_CODE (c3) != REAL_CST)
7250 ncmp = ERROR_MARK;
7251 else if ((cmp == LT_EXPR || cmp == GE_EXPR)
7252 && real_less (&TREE_REAL_CST (c3), &TREE_REAL_CST (@1)))
7253 ncmp = cmp == LT_EXPR ? LE_EXPR : GT_EXPR;
7254 else if ((cmp == LE_EXPR || cmp == GT_EXPR)
7255 && real_less (&TREE_REAL_CST (@1), &TREE_REAL_CST (c3)))
7256 ncmp = cmp == LE_EXPR ? LT_EXPR : GE_EXPR;
7257 else
7258 {
7259 /* With rounding to even, sqrt of up to 3 different values
7260 gives the same normal result, so in some cases c2 needs
7261 to be adjusted. */
7262 REAL_VALUE_TYPE c2alt, tow;
7263 if (cmp == LT_EXPR || cmp == GE_EXPR)
7264 tow = dconst0;
7265 else
7266 tow = dconstinf;
7267 real_nextafter (&c2alt, fmt, &c2, &tow);
7268 real_convert (&c2alt, fmt, &c2alt);
7269 if (REAL_VALUE_ISINF (c2alt))
7270 ncmp = ERROR_MARK;
7271 else
7272 {
7273 c3 = fold_const_call (CFN_SQRT, TREE_TYPE (@0),
7274 build_real (TREE_TYPE (@0), c2alt));
7275 if (c3 == NULL_TREE || TREE_CODE (c3) != REAL_CST)
7276 ncmp = ERROR_MARK;
7277 else if (real_equal (&TREE_REAL_CST (c3),
7278 &TREE_REAL_CST (@1)))
7279 c2 = c2alt;
7280 }
7281 }
7282 }
7283 }
7284 (if (cmp == GT_EXPR || cmp == GE_EXPR)
7285 (if (REAL_VALUE_ISINF (c2))
7286 /* sqrt(x) > y is x == +Inf, when y is very large. */
7287 (if (HONOR_INFINITIES (@0))
7288 (eq @0 { build_real (TREE_TYPE (@0), c2); })
7289 { constant_boolean_node (false, type); })
7290 /* sqrt(x) > c is the same as x > c*c. */
7291 (if (ncmp != ERROR_MARK)
7292 (if (ncmp == GE_EXPR)
7293 (ge @0 { build_real (TREE_TYPE (@0), c2); })
7294 (gt @0 { build_real (TREE_TYPE (@0), c2); }))))
7295 /* else if (cmp == LT_EXPR || cmp == LE_EXPR) */
7296 (if (REAL_VALUE_ISINF (c2))
7297 (switch
7298 /* sqrt(x) < y is always true, when y is a very large
7299 value and we don't care about NaNs or Infinities. */
7300 (if (! HONOR_NANS (@0) && ! HONOR_INFINITIES (@0))
7301 { constant_boolean_node (true, type); })
7302 /* sqrt(x) < y is x != +Inf when y is very large and we
7303 don't care about NaNs. */
7304 (if (! HONOR_NANS (@0))
7305 (ne @0 { build_real (TREE_TYPE (@0), c2); }))
7306 /* sqrt(x) < y is x >= 0 when y is very large and we
7307 don't care about Infinities. */
7308 (if (! HONOR_INFINITIES (@0))
7309 (ge @0 { build_real (TREE_TYPE (@0), dconst0); }))
7310 /* sqrt(x) < y is x >= 0 && x != +Inf, when y is large. */
7311 (if (GENERIC)
7312 (truth_andif
7313 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
7314 (ne @0 { build_real (TREE_TYPE (@0), c2); }))))
7315 /* sqrt(x) < c is the same as x < c*c, if we ignore NaNs. */
7316 (if (ncmp != ERROR_MARK && ! HONOR_NANS (@0))
7317 (if (ncmp == LT_EXPR)
7318 (lt @0 { build_real (TREE_TYPE (@0), c2); })
7319 (le @0 { build_real (TREE_TYPE (@0), c2); }))
7320 /* sqrt(x) < c is the same as x >= 0 && x < c*c. */
7321 (if (ncmp != ERROR_MARK && GENERIC)
7322 (if (ncmp == LT_EXPR)
7323 (truth_andif
7324 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
7325 (lt @0 { build_real (TREE_TYPE (@0), c2); }))
7326 (truth_andif
7327 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
7328 (le @0 { build_real (TREE_TYPE (@0), c2); })))))))))))
7329 /* Transform sqrt(x) cmp sqrt(y) -> x cmp y. */
7330 (simplify
7331 (cmp (sq @0) (sq @1))
7332 (if (! HONOR_NANS (@0))
7333 (cmp @0 @1))))))
7334
7335 /* Optimize various special cases of (FTYPE) N CMP (FTYPE) M. */
7336 (for cmp (lt le eq ne ge gt unordered ordered unlt unle ungt unge uneq ltgt)
7337 icmp (lt le eq ne ge gt unordered ordered lt le gt ge eq ne)
7338 (simplify
7339 (cmp (float@0 @1) (float @2))
7340 (if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (@0))
7341 && ! DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0)))
7342 (with
7343 {
7344 format_helper fmt (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (@0))));
7345 tree type1 = TREE_TYPE (@1);
7346 bool type1_signed_p = TYPE_SIGN (type1) == SIGNED;
7347 tree type2 = TREE_TYPE (@2);
7348 bool type2_signed_p = TYPE_SIGN (type2) == SIGNED;
7349 }
7350 (if (fmt.can_represent_integral_type_p (type1)
7351 && fmt.can_represent_integral_type_p (type2))
7352 (if (cmp == ORDERED_EXPR || cmp == UNORDERED_EXPR)
7353 { constant_boolean_node (cmp == ORDERED_EXPR, type); }
7354 (if (TYPE_PRECISION (type1) > TYPE_PRECISION (type2)
7355 && type1_signed_p >= type2_signed_p)
7356 (icmp @1 (convert @2))
7357 (if (TYPE_PRECISION (type1) < TYPE_PRECISION (type2)
7358 && type1_signed_p <= type2_signed_p)
7359 (icmp (convert:type2 @1) @2)
7360 (if (TYPE_PRECISION (type1) == TYPE_PRECISION (type2)
7361 && type1_signed_p == type2_signed_p)
7362 (icmp @1 (convert @2)))))))))))
7363
7364 /* Optimize various special cases of (FTYPE) N CMP CST. */
7365 (for cmp (lt le eq ne ge gt)
7366 icmp (le le eq ne ge ge)
7367 (simplify
7368 (cmp (float @0) REAL_CST@1)
7369 (if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (@1))
7370 && ! DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1)))
7371 (with
7372 {
7373 tree itype = TREE_TYPE (@0);
7374 format_helper fmt (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (@1))));
7375 const REAL_VALUE_TYPE *cst = TREE_REAL_CST_PTR (@1);
7376 /* Be careful to preserve any potential exceptions due to NaNs.
7377 qNaNs are ok in == or != context. */
7378 bool exception_p = real_isnan (cst)
7379 && flag_trapping_math
7380 && ((cmp != EQ_EXPR && cmp != NE_EXPR)
7381 || (cst->signalling
7382 && HONOR_SNANS (TREE_TYPE (@1))));
7383 }
7384 /* TODO: allow non-fitting itype and SNaNs when
7385 -fno-trapping-math. */
7386 (if (fmt.can_represent_integral_type_p (itype) && ! exception_p)
7387 (with
7388 {
7389 signop isign = TYPE_SIGN (itype);
7390 REAL_VALUE_TYPE imin, imax;
7391 real_from_integer (&imin, fmt, wi::min_value (itype), isign);
7392 real_from_integer (&imax, fmt, wi::max_value (itype), isign);
7393
7394 REAL_VALUE_TYPE icst;
7395 if (cmp == GT_EXPR || cmp == GE_EXPR)
7396 real_ceil (&icst, fmt, cst);
7397 else if (cmp == LT_EXPR || cmp == LE_EXPR)
7398 real_floor (&icst, fmt, cst);
7399 else
7400 real_trunc (&icst, fmt, cst);
7401
7402 bool cst_int_p = !real_isnan (cst) && real_identical (&icst, cst);
7403
7404 bool overflow_p = false;
7405 wide_int icst_val
7406 = real_to_integer (&icst, &overflow_p, TYPE_PRECISION (itype));
7407 }
7408 (switch
7409 /* Optimize cases when CST is outside of ITYPE's range. */
7410 (if (real_compare (LT_EXPR, cst, &imin))
7411 { constant_boolean_node (cmp == GT_EXPR || cmp == GE_EXPR || cmp == NE_EXPR,
7412 type); })
7413 (if (real_compare (GT_EXPR, cst, &imax))
7414 { constant_boolean_node (cmp == LT_EXPR || cmp == LE_EXPR || cmp == NE_EXPR,
7415 type); })
7416 /* Remove cast if CST is an integer representable by ITYPE. */
7417 (if (cst_int_p)
7418 (cmp @0 { gcc_assert (!overflow_p);
7419 wide_int_to_tree (itype, icst_val); })
7420 )
7421 /* When CST is fractional, optimize
7422 (FTYPE) N == CST -> 0
7423 (FTYPE) N != CST -> 1. */
7424 (if (cmp == EQ_EXPR || cmp == NE_EXPR)
7425 { constant_boolean_node (cmp == NE_EXPR, type); })
7426 /* Otherwise replace with sensible integer constant. */
7427 (with
7428 {
7429 gcc_checking_assert (!overflow_p);
7430 }
7431 (icmp @0 { wide_int_to_tree (itype, icst_val); })))))))))
7432
7433 /* Fold A /[ex] B CMP C to A CMP B * C. */
7434 (for cmp (eq ne)
7435 (simplify
7436 (cmp (exact_div @0 @1) INTEGER_CST@2)
7437 (if (!integer_zerop (@1))
7438 (if (wi::to_wide (@2) == 0)
7439 (cmp @0 @2)
7440 (if (TREE_CODE (@1) == INTEGER_CST)
7441 (with
7442 {
7443 wi::overflow_type ovf;
7444 wide_int prod = wi::mul (wi::to_wide (@2), wi::to_wide (@1),
7445 TYPE_SIGN (TREE_TYPE (@1)), &ovf);
7446 }
7447 (if (ovf)
7448 { constant_boolean_node (cmp == NE_EXPR, type); }
7449 (cmp @0 { wide_int_to_tree (TREE_TYPE (@0), prod); }))))))))
7450 (for cmp (lt le gt ge)
7451 (simplify
7452 (cmp (exact_div @0 INTEGER_CST@1) INTEGER_CST@2)
7453 (if (wi::gt_p (wi::to_wide (@1), 0, TYPE_SIGN (TREE_TYPE (@1))))
7454 (with
7455 {
7456 wi::overflow_type ovf;
7457 wide_int prod = wi::mul (wi::to_wide (@2), wi::to_wide (@1),
7458 TYPE_SIGN (TREE_TYPE (@1)), &ovf);
7459 }
7460 (if (ovf)
7461 { constant_boolean_node (wi::lt_p (wi::to_wide (@2), 0,
7462 TYPE_SIGN (TREE_TYPE (@2)))
7463 != (cmp == LT_EXPR || cmp == LE_EXPR), type); }
7464 (cmp @0 { wide_int_to_tree (TREE_TYPE (@0), prod); }))))))
7465
7466 /* Fold (size_t)(A /[ex] B) CMP C to (size_t)A CMP (size_t)B * C or A CMP' 0.
7467
7468 For small C (less than max/B), this is (size_t)A CMP (size_t)B * C.
7469 For large C (more than min/B+2^size), this is also true, with the
7470 multiplication computed modulo 2^size.
7471 For intermediate C, this just tests the sign of A. */
7472 (for cmp (lt le gt ge)
7473 cmp2 (ge ge lt lt)
7474 (simplify
7475 (cmp (convert (exact_div @0 INTEGER_CST@1)) INTEGER_CST@2)
7476 (if (tree_nop_conversion_p (TREE_TYPE (@0), TREE_TYPE (@2))
7477 && TYPE_UNSIGNED (TREE_TYPE (@2)) && !TYPE_UNSIGNED (TREE_TYPE (@0))
7478 && wi::gt_p (wi::to_wide (@1), 0, TYPE_SIGN (TREE_TYPE (@1))))
7479 (with
7480 {
7481 tree utype = TREE_TYPE (@2);
7482 wide_int denom = wi::to_wide (@1);
7483 wide_int right = wi::to_wide (@2);
7484 wide_int smax = wi::sdiv_trunc (wi::max_value (TREE_TYPE (@0)), denom);
7485 wide_int smin = wi::sdiv_trunc (wi::min_value (TREE_TYPE (@0)), denom);
7486 bool small = wi::leu_p (right, smax);
7487 bool large = wi::geu_p (right, smin);
7488 }
7489 (if (small || large)
7490 (cmp (convert:utype @0) (mult @2 (convert @1)))
7491 (cmp2 @0 { build_zero_cst (TREE_TYPE (@0)); }))))))
7492
7493 /* Unordered tests if either argument is a NaN. */
7494 (simplify
7495 (bit_ior (unordered @0 @0) (unordered @1 @1))
7496 (if (types_match (@0, @1))
7497 (unordered @0 @1)))
7498 (simplify
7499 (bit_and (ordered @0 @0) (ordered @1 @1))
7500 (if (types_match (@0, @1))
7501 (ordered @0 @1)))
7502 (simplify
7503 (bit_ior:c (unordered @0 @0) (unordered:c@2 @0 @1))
7504 @2)
7505 (simplify
7506 (bit_and:c (ordered @0 @0) (ordered:c@2 @0 @1))
7507 @2)
7508
7509 /* A & (2**N - 1) <= 2**K - 1 -> A & (2**N - 2**K) == 0
7510 A & (2**N - 1) > 2**K - 1 -> A & (2**N - 2**K) != 0
7511
7512 Note that comparisons
7513 A & (2**N - 1) < 2**K -> A & (2**N - 2**K) == 0
7514 A & (2**N - 1) >= 2**K -> A & (2**N - 2**K) != 0
7515 will be canonicalized to above so there's no need to
7516 consider them here.
7517 */
7518
7519 (for cmp (le gt)
7520 eqcmp (eq ne)
7521 (simplify
7522 (cmp (bit_and@0 @1 INTEGER_CST@2) INTEGER_CST@3)
7523 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)))
7524 (with
7525 {
7526 tree ty = TREE_TYPE (@0);
7527 unsigned prec = TYPE_PRECISION (ty);
7528 wide_int mask = wi::to_wide (@2, prec);
7529 wide_int rhs = wi::to_wide (@3, prec);
7530 signop sgn = TYPE_SIGN (ty);
7531 }
7532 (if ((mask & (mask + 1)) == 0 && wi::gt_p (rhs, 0, sgn)
7533 && (rhs & (rhs + 1)) == 0 && wi::ge_p (mask, rhs, sgn))
7534 (eqcmp (bit_and @1 { wide_int_to_tree (ty, mask - rhs); })
7535 { build_zero_cst (ty); }))))))
7536
7537 /* -A CMP -B -> B CMP A. */
7538 (for cmp (tcc_comparison)
7539 scmp (swapped_tcc_comparison)
7540 (simplify
7541 (cmp (negate @0) (negate @1))
7542 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
7543 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
7544 && (cmp == EQ_EXPR
7545 || cmp == NE_EXPR
7546 || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))))
7547 (scmp @0 @1)))
7548 (simplify
7549 (cmp (negate @0) CONSTANT_CLASS_P@1)
7550 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
7551 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
7552 && (cmp == EQ_EXPR
7553 || cmp == NE_EXPR
7554 || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))))
7555 (with { tree tem = const_unop (NEGATE_EXPR, TREE_TYPE (@0), @1); }
7556 (if (tem && !TREE_OVERFLOW (tem))
7557 (scmp @0 { tem; }))))))
7558
7559 /* Convert ABS[U]_EXPR<x> == 0 or ABS[U]_EXPR<x> != 0 to x == 0 or x != 0. */
7560 (for op (abs absu)
7561 (for eqne (eq ne)
7562 (simplify
7563 (eqne (op @0) zerop@1)
7564 (eqne @0 { build_zero_cst (TREE_TYPE (@0)); }))))
7565
7566 /* From fold_sign_changed_comparison and fold_widened_comparison.
7567 FIXME: the lack of symmetry is disturbing. */
7568 (for cmp (simple_comparison)
7569 (simplify
7570 (cmp (convert@0 @00) (convert?@1 @10))
7571 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
7572 /* Disable this optimization if we're casting a function pointer
7573 type on targets that require function pointer canonicalization. */
7574 && !(targetm.have_canonicalize_funcptr_for_compare ()
7575 && ((POINTER_TYPE_P (TREE_TYPE (@00))
7576 && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@00))))
7577 || (POINTER_TYPE_P (TREE_TYPE (@10))
7578 && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@10))))))
7579 && single_use (@0))
7580 (if (TYPE_PRECISION (TREE_TYPE (@00)) == TYPE_PRECISION (TREE_TYPE (@0))
7581 && (TREE_CODE (@10) == INTEGER_CST
7582 || @1 != @10)
7583 && (TYPE_UNSIGNED (TREE_TYPE (@00)) == TYPE_UNSIGNED (TREE_TYPE (@0))
7584 || cmp == NE_EXPR
7585 || cmp == EQ_EXPR)
7586 && !POINTER_TYPE_P (TREE_TYPE (@00))
7587 /* (int)bool:32 != (int)uint is not the same as
7588 bool:32 != (bool:32)uint since boolean types only have two valid
7589 values independent of their precision. */
7590 && (TREE_CODE (TREE_TYPE (@00)) != BOOLEAN_TYPE
7591 || TREE_CODE (TREE_TYPE (@10)) == BOOLEAN_TYPE))
7592 /* ??? The special-casing of INTEGER_CST conversion was in the original
7593 code and here to avoid a spurious overflow flag on the resulting
7594 constant which fold_convert produces. */
7595 (if (TREE_CODE (@1) == INTEGER_CST)
7596 (cmp @00 { force_fit_type (TREE_TYPE (@00),
7597 wide_int::from (wi::to_wide (@1),
7598 MAX (TYPE_PRECISION (TREE_TYPE (@1)),
7599 TYPE_PRECISION (TREE_TYPE (@00))),
7600 TYPE_SIGN (TREE_TYPE (@1))),
7601 0, TREE_OVERFLOW (@1)); })
7602 (cmp @00 (convert @1)))
7603
7604 (if (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (TREE_TYPE (@00)))
7605 /* If possible, express the comparison in the shorter mode. */
7606 (if ((cmp == EQ_EXPR || cmp == NE_EXPR
7607 || TYPE_UNSIGNED (TREE_TYPE (@0)) == TYPE_UNSIGNED (TREE_TYPE (@00))
7608 || (!TYPE_UNSIGNED (TREE_TYPE (@0))
7609 && TYPE_UNSIGNED (TREE_TYPE (@00))))
7610 && (types_match (TREE_TYPE (@10), TREE_TYPE (@00))
7611 || ((TYPE_PRECISION (TREE_TYPE (@00))
7612 >= TYPE_PRECISION (TREE_TYPE (@10)))
7613 && (TYPE_UNSIGNED (TREE_TYPE (@00))
7614 == TYPE_UNSIGNED (TREE_TYPE (@10))))
7615 || (TREE_CODE (@1) == INTEGER_CST
7616 && INTEGRAL_TYPE_P (TREE_TYPE (@00))
7617 && int_fits_type_p (@1, TREE_TYPE (@00)))))
7618 (cmp @00 (convert @10))
7619 (if (TREE_CODE (@1) == INTEGER_CST
7620 && INTEGRAL_TYPE_P (TREE_TYPE (@00))
7621 && !int_fits_type_p (@1, TREE_TYPE (@00)))
7622 (with
7623 {
7624 tree min = lower_bound_in_type (TREE_TYPE (@10), TREE_TYPE (@00));
7625 tree max = upper_bound_in_type (TREE_TYPE (@10), TREE_TYPE (@00));
7626 bool above = integer_nonzerop (const_binop (LT_EXPR, type, max, @1));
7627 bool below = integer_nonzerop (const_binop (LT_EXPR, type, @1, min));
7628 }
7629 (if (above || below)
7630 (if (cmp == EQ_EXPR || cmp == NE_EXPR)
7631 { constant_boolean_node (cmp == EQ_EXPR ? false : true, type); }
7632 (if (cmp == LT_EXPR || cmp == LE_EXPR)
7633 { constant_boolean_node (above ? true : false, type); }
7634 (if (cmp == GT_EXPR || cmp == GE_EXPR)
7635 { constant_boolean_node (above ? false : true, type); })))))))))
7636 /* Fold (double)float1 CMP (double)float2 into float1 CMP float2. */
7637 (if (FLOAT_TYPE_P (TREE_TYPE (@00))
7638 && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0))
7639 == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@00)))
7640 && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0))
7641 == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@10))))
7642 (with
7643 {
7644 tree type1 = TREE_TYPE (@10);
7645 if (TREE_CODE (@10) == REAL_CST && !DECIMAL_FLOAT_TYPE_P (type1))
7646 {
7647 REAL_VALUE_TYPE orig = TREE_REAL_CST (@10);
7648 if (TYPE_PRECISION (type1) > TYPE_PRECISION (float_type_node)
7649 && exact_real_truncate (TYPE_MODE (float_type_node), &orig))
7650 type1 = float_type_node;
7651 if (TYPE_PRECISION (type1) > TYPE_PRECISION (double_type_node)
7652 && exact_real_truncate (TYPE_MODE (double_type_node), &orig))
7653 type1 = double_type_node;
7654 }
7655 tree newtype
7656 = (element_precision (TREE_TYPE (@00)) > element_precision (type1)
7657 ? TREE_TYPE (@00) : type1);
7658 }
7659 (if (element_precision (TREE_TYPE (@0)) > element_precision (newtype)
7660 && (!VECTOR_TYPE_P (type) || is_truth_type_for (newtype, type)))
7661 (cmp (convert:newtype @00) (convert:newtype @10))))))))
7662
7663
7664 (for cmp (eq ne)
7665 (simplify
7666 /* SSA names are canonicalized to 2nd place. */
7667 (cmp addr@0 SSA_NAME@1)
7668 (with
7669 {
7670 poly_int64 off; tree base;
7671 tree addr = (TREE_CODE (@0) == SSA_NAME
7672 ? gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0)) : @0);
7673 }
7674 /* A local variable can never be pointed to by
7675 the default SSA name of an incoming parameter. */
7676 (if (SSA_NAME_IS_DEFAULT_DEF (@1)
7677 && TREE_CODE (SSA_NAME_VAR (@1)) == PARM_DECL
7678 && (base = get_base_address (TREE_OPERAND (addr, 0)))
7679 && TREE_CODE (base) == VAR_DECL
7680 && auto_var_in_fn_p (base, current_function_decl))
7681 (if (cmp == NE_EXPR)
7682 { constant_boolean_node (true, type); }
7683 { constant_boolean_node (false, type); })
7684 /* If the address is based on @1 decide using the offset. */
7685 (if ((base = get_addr_base_and_unit_offset (TREE_OPERAND (addr, 0), &off))
7686 && TREE_CODE (base) == MEM_REF
7687 && TREE_OPERAND (base, 0) == @1)
7688 (with { off += mem_ref_offset (base).force_shwi (); }
7689 (if (known_ne (off, 0))
7690 { constant_boolean_node (cmp == NE_EXPR, type); }
7691 (if (known_eq (off, 0))
7692 { constant_boolean_node (cmp == EQ_EXPR, type); }))))))))
7693
7694 /* Equality compare simplifications from fold_binary */
7695 (for cmp (eq ne)
7696
7697 /* If we have (A | C) == D where C & ~D != 0, convert this into 0.
7698 Similarly for NE_EXPR. */
7699 (simplify
7700 (cmp (convert?@3 (bit_ior @0 INTEGER_CST@1)) INTEGER_CST@2)
7701 (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0))
7702 && wi::bit_and_not (wi::to_wide (@1), wi::to_wide (@2)) != 0)
7703 { constant_boolean_node (cmp == NE_EXPR, type); }))
7704
7705 /* (X ^ Y) == 0 becomes X == Y, and (X ^ Y) != 0 becomes X != Y. */
7706 (simplify
7707 (cmp (bit_xor @0 @1) integer_zerop)
7708 (cmp @0 @1))
7709
7710 /* (X ^ Y) == Y becomes X == 0.
7711 Likewise (X ^ Y) == X becomes Y == 0. */
7712 (simplify
7713 (cmp:c (bit_xor:c @0 @1) @0)
7714 (cmp @1 { build_zero_cst (TREE_TYPE (@1)); }))
7715
7716 /* (X & Y) == X becomes (X & ~Y) == 0. */
7717 (simplify
7718 (cmp:c (bit_and:c @0 @1) @0)
7719 (cmp (bit_and @0 (bit_not! @1)) { build_zero_cst (TREE_TYPE (@0)); }))
7720 (simplify
7721 (cmp:c (convert@3 (bit_and (convert@2 @0) INTEGER_CST@1)) (convert @0))
7722 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
7723 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
7724 && INTEGRAL_TYPE_P (TREE_TYPE (@3))
7725 && TYPE_PRECISION (TREE_TYPE (@2)) == TYPE_PRECISION (TREE_TYPE (@0))
7726 && TYPE_PRECISION (TREE_TYPE (@3)) > TYPE_PRECISION (TREE_TYPE (@2))
7727 && !wi::neg_p (wi::to_wide (@1)))
7728 (cmp (bit_and @0 (convert (bit_not @1)))
7729 { build_zero_cst (TREE_TYPE (@0)); })))
7730
7731 /* (X | Y) == Y becomes (X & ~Y) == 0. */
7732 (simplify
7733 (cmp:c (bit_ior:c @0 @1) @1)
7734 (cmp (bit_and @0 (bit_not! @1)) { build_zero_cst (TREE_TYPE (@0)); }))
7735
7736 /* (X ^ C1) op C2 can be rewritten as X op (C1 ^ C2). */
7737 (simplify
7738 (cmp (convert?@3 (bit_xor @0 INTEGER_CST@1)) INTEGER_CST@2)
7739 (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0)))
7740 (cmp @0 (bit_xor @1 (convert @2)))))
7741
7742 (simplify
7743 (cmp (nop_convert? @0) integer_zerop)
7744 (if (tree_expr_nonzero_p (@0))
7745 { constant_boolean_node (cmp == NE_EXPR, type); }))
7746
7747 /* (X & C) op (Y & C) into (X ^ Y) & C op 0. */
7748 (simplify
7749 (cmp (bit_and:cs @0 @2) (bit_and:cs @1 @2))
7750 (cmp (bit_and (bit_xor @0 @1) @2) { build_zero_cst (TREE_TYPE (@2)); })))
7751
7752 /* (X < 0) != (Y < 0) into (X ^ Y) < 0.
7753 (X >= 0) != (Y >= 0) into (X ^ Y) < 0.
7754 (X < 0) == (Y < 0) into (X ^ Y) >= 0.
7755 (X >= 0) == (Y >= 0) into (X ^ Y) >= 0. */
7756 (for cmp (eq ne)
7757 ncmp (ge lt)
7758 (for sgncmp (ge lt)
7759 (simplify
7760 (cmp (sgncmp @0 integer_zerop@2) (sgncmp @1 integer_zerop))
7761 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
7762 && !TYPE_UNSIGNED (TREE_TYPE (@0))
7763 && types_match (@0, @1))
7764 (ncmp (bit_xor @0 @1) @2)))))
7765 /* (X < 0) == (Y >= 0) into (X ^ Y) < 0.
7766 (X < 0) != (Y >= 0) into (X ^ Y) >= 0. */
7767 (for cmp (eq ne)
7768 ncmp (lt ge)
7769 (simplify
7770 (cmp:c (lt @0 integer_zerop@2) (ge @1 integer_zerop))
7771 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
7772 && !TYPE_UNSIGNED (TREE_TYPE (@0))
7773 && types_match (@0, @1))
7774 (ncmp (bit_xor @0 @1) @2))))
7775
7776 /* If we have (A & C) == C where C is a power of 2, convert this into
7777 (A & C) != 0. Similarly for NE_EXPR. */
7778 (for cmp (eq ne)
7779 icmp (ne eq)
7780 (simplify
7781 (cmp (bit_and@2 @0 integer_pow2p@1) @1)
7782 (icmp @2 { build_zero_cst (TREE_TYPE (@0)); })))
7783
7784 #if GIMPLE
7785 /* From fold_binary_op_with_conditional_arg handle the case of
7786 rewriting (a ? b : c) > d to a ? (b > d) : (c > d) when the
7787 compares simplify. */
7788 (for cmp (simple_comparison)
7789 (simplify
7790 (cmp:c (cond @0 @1 @2) @3)
7791 /* Do not move possibly trapping operations into the conditional as this
7792 pessimizes code and causes gimplification issues when applied late. */
7793 (if (!FLOAT_TYPE_P (TREE_TYPE (@3))
7794 || !operation_could_trap_p (cmp, true, false, @3))
7795 (cond @0 (cmp! @1 @3) (cmp! @2 @3)))))
7796 #endif
7797
7798 (for cmp (ge lt)
7799 /* x < 0 ? ~y : y into (x >> (prec-1)) ^ y. */
7800 /* x >= 0 ? ~y : y into ~((x >> (prec-1)) ^ y). */
7801 (simplify
7802 (cond (cmp @0 integer_zerop) (bit_not @1) @1)
7803 (if (INTEGRAL_TYPE_P (type)
7804 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
7805 && !TYPE_UNSIGNED (TREE_TYPE (@0))
7806 && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (type))
7807 (with
7808 {
7809 tree shifter = build_int_cst (integer_type_node, TYPE_PRECISION (type) - 1);
7810 }
7811 (if (cmp == LT_EXPR)
7812 (bit_xor (convert (rshift @0 {shifter;})) @1)
7813 (bit_not (bit_xor (convert (rshift @0 {shifter;})) @1))))))
7814 /* x < 0 ? y : ~y into ~((x >> (prec-1)) ^ y). */
7815 /* x >= 0 ? y : ~y into (x >> (prec-1)) ^ y. */
7816 (simplify
7817 (cond (cmp @0 integer_zerop) @1 (bit_not @1))
7818 (if (INTEGRAL_TYPE_P (type)
7819 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
7820 && !TYPE_UNSIGNED (TREE_TYPE (@0))
7821 && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (type))
7822 (with
7823 {
7824 tree shifter = build_int_cst (integer_type_node, TYPE_PRECISION (type) - 1);
7825 }
7826 (if (cmp == GE_EXPR)
7827 (bit_xor (convert (rshift @0 {shifter;})) @1)
7828 (bit_not (bit_xor (convert (rshift @0 {shifter;})) @1)))))))
7829
7830 /* If we have (A & C) != 0 ? D : 0 where C and D are powers of 2,
7831 convert this into a shift followed by ANDing with D. */
7832 (simplify
7833 (cond
7834 (ne (bit_and @0 integer_pow2p@1) integer_zerop)
7835 INTEGER_CST@2 integer_zerop)
7836 (if (!POINTER_TYPE_P (type) && integer_pow2p (@2))
7837 (with {
7838 int shift = (wi::exact_log2 (wi::to_wide (@2))
7839 - wi::exact_log2 (wi::to_wide (@1)));
7840 }
7841 (if (shift > 0)
7842 (bit_and
7843 (lshift (convert @0) { build_int_cst (integer_type_node, shift); }) @2)
7844 (bit_and
7845 (convert (rshift @0 { build_int_cst (integer_type_node, -shift); }))
7846 @2)))))
7847
7848 /* If we have (A & C) != 0 where C is the sign bit of A, convert
7849 this into A < 0. Similarly for (A & C) == 0 into A >= 0. */
7850 (for cmp (eq ne)
7851 ncmp (ge lt)
7852 (simplify
7853 (cmp (bit_and (convert?@2 @0) integer_pow2p@1) integer_zerop)
7854 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
7855 && type_has_mode_precision_p (TREE_TYPE (@0))
7856 && element_precision (@2) >= element_precision (@0)
7857 && wi::only_sign_bit_p (wi::to_wide (@1), element_precision (@0)))
7858 (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
7859 (ncmp (convert:stype @0) { build_zero_cst (stype); })))))
7860
7861 /* If we have A < 0 ? C : 0 where C is a power of 2, convert
7862 this into a right shift or sign extension followed by ANDing with C. */
7863 (simplify
7864 (cond
7865 (lt @0 integer_zerop)
7866 INTEGER_CST@1 integer_zerop)
7867 (if (integer_pow2p (@1)
7868 && !TYPE_UNSIGNED (TREE_TYPE (@0)))
7869 (with {
7870 int shift = element_precision (@0) - wi::exact_log2 (wi::to_wide (@1)) - 1;
7871 }
7872 (if (shift >= 0)
7873 (bit_and
7874 (convert (rshift @0 { build_int_cst (integer_type_node, shift); }))
7875 @1)
7876 /* Otherwise ctype must be wider than TREE_TYPE (@0) and pure
7877 sign extension followed by AND with C will achieve the effect. */
7878 (bit_and (convert @0) @1)))))
7879
7880 /* When the addresses are not directly of decls compare base and offset.
7881 This implements some remaining parts of fold_comparison address
7882 comparisons but still no complete part of it. Still it is good
7883 enough to make fold_stmt not regress when not dispatching to fold_binary. */
7884 (for cmp (simple_comparison)
7885 (simplify
7886 (cmp (convert1?@2 addr@0) (convert2? addr@1))
7887 (with
7888 {
7889 poly_int64 off0, off1;
7890 tree base0, base1;
7891 int equal = address_compare (cmp, TREE_TYPE (@2), @0, @1, base0, base1,
7892 off0, off1, GENERIC);
7893 }
7894 (if (equal == 1)
7895 (switch
7896 (if (cmp == EQ_EXPR && (known_eq (off0, off1) || known_ne (off0, off1)))
7897 { constant_boolean_node (known_eq (off0, off1), type); })
7898 (if (cmp == NE_EXPR && (known_eq (off0, off1) || known_ne (off0, off1)))
7899 { constant_boolean_node (known_ne (off0, off1), type); })
7900 (if (cmp == LT_EXPR && (known_lt (off0, off1) || known_ge (off0, off1)))
7901 { constant_boolean_node (known_lt (off0, off1), type); })
7902 (if (cmp == LE_EXPR && (known_le (off0, off1) || known_gt (off0, off1)))
7903 { constant_boolean_node (known_le (off0, off1), type); })
7904 (if (cmp == GE_EXPR && (known_ge (off0, off1) || known_lt (off0, off1)))
7905 { constant_boolean_node (known_ge (off0, off1), type); })
7906 (if (cmp == GT_EXPR && (known_gt (off0, off1) || known_le (off0, off1)))
7907 { constant_boolean_node (known_gt (off0, off1), type); }))
7908 (if (equal == 0)
7909 (switch
7910 (if (cmp == EQ_EXPR)
7911 { constant_boolean_node (false, type); })
7912 (if (cmp == NE_EXPR)
7913 { constant_boolean_node (true, type); })))))))
7914
7915 #if GIMPLE
7916 /* a?~t:t -> (-(a))^t */
7917 (simplify
7918 (cond @0 @1 @2)
7919 (with { bool wascmp; }
7920 (if (INTEGRAL_TYPE_P (type)
7921 && TYPE_UNSIGNED (TREE_TYPE (@0))
7922 && bitwise_inverted_equal_p (@1, @2, wascmp)
7923 && (!wascmp || TYPE_PRECISION (type) == 1))
7924 (if ((!TYPE_UNSIGNED (type) && TREE_CODE (type) == BOOLEAN_TYPE)
7925 || TYPE_PRECISION (type) == 1)
7926 (bit_xor (convert:type @0) @2)
7927 (bit_xor (negate (convert:type @0)) @2)))))
7928 #endif
7929
7930 /* Simplify pointer equality compares using PTA. */
7931 (for neeq (ne eq)
7932 (simplify
7933 (neeq @0 @1)
7934 (if (POINTER_TYPE_P (TREE_TYPE (@0))
7935 && ptrs_compare_unequal (@0, @1))
7936 { constant_boolean_node (neeq != EQ_EXPR, type); })))
7937
7938 /* PR70920: Transform (intptr_t)x eq/ne CST to x eq/ne (typeof x) CST.
7939 and (typeof ptr_cst) x eq/ne ptr_cst to x eq/ne (typeof x) CST.
7940 Disable the transform if either operand is pointer to function.
7941 This broke pr22051-2.c for arm where function pointer
7942 canonicalizaion is not wanted. */
7943
7944 (for cmp (ne eq)
7945 (simplify
7946 (cmp (convert @0) INTEGER_CST@1)
7947 (if (((POINTER_TYPE_P (TREE_TYPE (@0))
7948 && !FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@0)))
7949 && INTEGRAL_TYPE_P (TREE_TYPE (@1))
7950 /* Don't perform this optimization in GENERIC if @0 has reference
7951 type when sanitizing. See PR101210. */
7952 && !(GENERIC
7953 && TREE_CODE (TREE_TYPE (@0)) == REFERENCE_TYPE
7954 && (flag_sanitize & (SANITIZE_NULL | SANITIZE_ALIGNMENT))))
7955 || (INTEGRAL_TYPE_P (TREE_TYPE (@0))
7956 && POINTER_TYPE_P (TREE_TYPE (@1))
7957 && !FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@1)))))
7958 && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
7959 (cmp @0 (convert @1)))))
7960
7961 /* Non-equality compare simplifications from fold_binary */
7962 (for cmp (lt gt le ge)
7963 /* Comparisons with the highest or lowest possible integer of
7964 the specified precision will have known values. */
7965 (simplify
7966 (cmp (convert?@2 @0) uniform_integer_cst_p@1)
7967 (if ((INTEGRAL_TYPE_P (TREE_TYPE (@1))
7968 || POINTER_TYPE_P (TREE_TYPE (@1))
7969 || VECTOR_INTEGER_TYPE_P (TREE_TYPE (@1)))
7970 && tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@0)))
7971 (with
7972 {
7973 tree cst = uniform_integer_cst_p (@1);
7974 tree arg1_type = TREE_TYPE (cst);
7975 unsigned int prec = TYPE_PRECISION (arg1_type);
7976 wide_int max = wi::max_value (arg1_type);
7977 wide_int signed_max = wi::max_value (prec, SIGNED);
7978 wide_int min = wi::min_value (arg1_type);
7979 }
7980 (switch
7981 (if (wi::to_wide (cst) == max)
7982 (switch
7983 (if (cmp == GT_EXPR)
7984 { constant_boolean_node (false, type); })
7985 (if (cmp == GE_EXPR)
7986 (eq @2 @1))
7987 (if (cmp == LE_EXPR)
7988 { constant_boolean_node (true, type); })
7989 (if (cmp == LT_EXPR)
7990 (ne @2 @1))))
7991 (if (wi::to_wide (cst) == min)
7992 (switch
7993 (if (cmp == LT_EXPR)
7994 { constant_boolean_node (false, type); })
7995 (if (cmp == LE_EXPR)
7996 (eq @2 @1))
7997 (if (cmp == GE_EXPR)
7998 { constant_boolean_node (true, type); })
7999 (if (cmp == GT_EXPR)
8000 (ne @2 @1))))
8001 (if (wi::to_wide (cst) == max - 1)
8002 (switch
8003 (if (cmp == GT_EXPR)
8004 (eq @2 { build_uniform_cst (TREE_TYPE (@1),
8005 wide_int_to_tree (TREE_TYPE (cst),
8006 wi::to_wide (cst)
8007 + 1)); }))
8008 (if (cmp == LE_EXPR)
8009 (ne @2 { build_uniform_cst (TREE_TYPE (@1),
8010 wide_int_to_tree (TREE_TYPE (cst),
8011 wi::to_wide (cst)
8012 + 1)); }))))
8013 (if (wi::to_wide (cst) == min + 1)
8014 (switch
8015 (if (cmp == GE_EXPR)
8016 (ne @2 { build_uniform_cst (TREE_TYPE (@1),
8017 wide_int_to_tree (TREE_TYPE (cst),
8018 wi::to_wide (cst)
8019 - 1)); }))
8020 (if (cmp == LT_EXPR)
8021 (eq @2 { build_uniform_cst (TREE_TYPE (@1),
8022 wide_int_to_tree (TREE_TYPE (cst),
8023 wi::to_wide (cst)
8024 - 1)); }))))
8025 (if (wi::to_wide (cst) == signed_max
8026 && TYPE_UNSIGNED (arg1_type)
8027 && TYPE_MODE (arg1_type) != BLKmode
8028 /* We will flip the signedness of the comparison operator
8029 associated with the mode of @1, so the sign bit is
8030 specified by this mode. Check that @1 is the signed
8031 max associated with this sign bit. */
8032 && prec == GET_MODE_PRECISION (SCALAR_INT_TYPE_MODE (arg1_type))
8033 /* signed_type does not work on pointer types. */
8034 && INTEGRAL_TYPE_P (arg1_type))
8035 /* The following case also applies to X < signed_max+1
8036 and X >= signed_max+1 because previous transformations. */
8037 (if (cmp == LE_EXPR || cmp == GT_EXPR)
8038 (with { tree st = signed_type_for (TREE_TYPE (@1)); }
8039 (switch
8040 (if (cst == @1 && cmp == LE_EXPR)
8041 (ge (convert:st @0) { build_zero_cst (st); }))
8042 (if (cst == @1 && cmp == GT_EXPR)
8043 (lt (convert:st @0) { build_zero_cst (st); }))
8044 (if (cmp == LE_EXPR)
8045 (ge (view_convert:st @0) { build_zero_cst (st); }))
8046 (if (cmp == GT_EXPR)
8047 (lt (view_convert:st @0) { build_zero_cst (st); })))))))))))
8048
8049 /* unsigned < (typeof unsigned)(unsigned != 0) is always false. */
8050 (simplify
8051 (lt:c @0 (convert (ne @0 integer_zerop)))
8052 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
8053 { constant_boolean_node (false, type); }))
8054
8055 /* x != (typeof x)(x == CST) -> CST == 0 ? 1 : (CST == 1 ? (x!=0&&x!=1) : x != 0) */
8056 /* x != (typeof x)(x != CST) -> CST == 1 ? 1 : (CST == 0 ? (x!=0&&x!=1) : x != 1) */
8057 /* x == (typeof x)(x == CST) -> CST == 0 ? 0 : (CST == 1 ? (x==0||x==1) : x == 0) */
8058 /* x == (typeof x)(x != CST) -> CST == 1 ? 0 : (CST == 0 ? (x==0||x==1) : x == 1) */
8059 (for outer (ne eq)
8060 (for inner (ne eq)
8061 (simplify
8062 (outer:c @0 (convert (inner @0 INTEGER_CST@1)))
8063 (with {
8064 bool cst1 = integer_onep (@1);
8065 bool cst0 = integer_zerop (@1);
8066 bool innereq = inner == EQ_EXPR;
8067 bool outereq = outer == EQ_EXPR;
8068 }
8069 (switch
8070 (if (innereq ? cst0 : cst1)
8071 { constant_boolean_node (!outereq, type); })
8072 (if (innereq ? cst1 : cst0)
8073 (with {
8074 tree utype = unsigned_type_for (TREE_TYPE (@0));
8075 tree ucst1 = build_one_cst (utype);
8076 }
8077 (if (!outereq)
8078 (gt (convert:utype @0) { ucst1; })
8079 (le (convert:utype @0) { ucst1; })
8080 )
8081 )
8082 )
8083 (with {
8084 tree value = build_int_cst (TREE_TYPE (@0), !innereq);
8085 }
8086 (if (outereq)
8087 (eq @0 { value; })
8088 (ne @0 { value; })
8089 )
8090 )
8091 )
8092 )
8093 )
8094 )
8095 )
8096
8097 (for cmp (unordered ordered unlt unle ungt unge uneq ltgt)
8098 /* If the second operand is NaN, the result is constant. */
8099 (simplify
8100 (cmp @0 REAL_CST@1)
8101 (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
8102 && (cmp != LTGT_EXPR || ! flag_trapping_math))
8103 { constant_boolean_node (cmp == ORDERED_EXPR || cmp == LTGT_EXPR
8104 ? false : true, type); })))
8105
8106 /* Fold UNORDERED if either operand must be NaN, or neither can be. */
8107 (simplify
8108 (unordered @0 @1)
8109 (switch
8110 (if (tree_expr_nan_p (@0) || tree_expr_nan_p (@1))
8111 { constant_boolean_node (true, type); })
8112 (if (!tree_expr_maybe_nan_p (@0) && !tree_expr_maybe_nan_p (@1))
8113 { constant_boolean_node (false, type); })))
8114
8115 /* Fold ORDERED if either operand must be NaN, or neither can be. */
8116 (simplify
8117 (ordered @0 @1)
8118 (switch
8119 (if (tree_expr_nan_p (@0) || tree_expr_nan_p (@1))
8120 { constant_boolean_node (false, type); })
8121 (if (!tree_expr_maybe_nan_p (@0) && !tree_expr_maybe_nan_p (@1))
8122 { constant_boolean_node (true, type); })))
8123
8124 /* bool_var != 0 becomes bool_var. */
8125 (simplify
8126 (ne @0 integer_zerop)
8127 (if (TREE_CODE (TREE_TYPE (@0)) == BOOLEAN_TYPE
8128 && types_match (type, TREE_TYPE (@0)))
8129 (non_lvalue @0)))
8130 /* bool_var == 1 becomes bool_var. */
8131 (simplify
8132 (eq @0 integer_onep)
8133 (if (TREE_CODE (TREE_TYPE (@0)) == BOOLEAN_TYPE
8134 && types_match (type, TREE_TYPE (@0)))
8135 (non_lvalue @0)))
8136 /* Do not handle
8137 bool_var == 0 becomes !bool_var or
8138 bool_var != 1 becomes !bool_var
8139 here because that only is good in assignment context as long
8140 as we require a tcc_comparison in GIMPLE_CONDs where we'd
8141 replace if (x == 0) with tem = ~x; if (tem != 0) which is
8142 clearly less optimal and which we'll transform again in forwprop. */
8143
8144 /* Transform comparisons of the form (X & Y) CMP 0 to X CMP2 Z
8145 where ~Y + 1 == pow2 and Z = ~Y. */
8146 (for cst (VECTOR_CST INTEGER_CST)
8147 (for cmp (eq ne)
8148 icmp (le gt)
8149 (simplify
8150 (cmp (bit_and:c@2 @0 cst@1) integer_zerop)
8151 (with { tree csts = bitmask_inv_cst_vector_p (@1); }
8152 (if (csts && (VECTOR_TYPE_P (TREE_TYPE (@1)) || single_use (@2)))
8153 (with { auto optab = VECTOR_TYPE_P (TREE_TYPE (@1))
8154 ? optab_vector : optab_default;
8155 tree utype = unsigned_type_for (TREE_TYPE (@1)); }
8156 (if (target_supports_op_p (utype, icmp, optab)
8157 || (optimize_vectors_before_lowering_p ()
8158 && (!target_supports_op_p (type, cmp, optab)
8159 || !target_supports_op_p (type, BIT_AND_EXPR, optab))))
8160 (if (TYPE_UNSIGNED (TREE_TYPE (@1)))
8161 (icmp @0 { csts; })
8162 (icmp (view_convert:utype @0) { csts; })))))))))
8163
8164 /* When one argument is a constant, overflow detection can be simplified.
8165 Currently restricted to single use so as not to interfere too much with
8166 ADD_OVERFLOW detection in tree-ssa-math-opts.cc.
8167 CONVERT?(CONVERT?(A) + CST) CMP A -> A CMP' CST' */
8168 (for cmp (lt le ge gt)
8169 out (gt gt le le)
8170 (simplify
8171 (cmp:c (convert?@3 (plus@2 (convert?@4 @0) INTEGER_CST@1)) @0)
8172 (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@2))
8173 && types_match (TREE_TYPE (@0), TREE_TYPE (@3))
8174 && tree_nop_conversion_p (TREE_TYPE (@4), TREE_TYPE (@0))
8175 && wi::to_wide (@1) != 0
8176 && single_use (@2))
8177 (with {
8178 unsigned int prec = TYPE_PRECISION (TREE_TYPE (@0));
8179 signop sign = TYPE_SIGN (TREE_TYPE (@0));
8180 }
8181 (out @0 { wide_int_to_tree (TREE_TYPE (@0),
8182 wi::max_value (prec, sign)
8183 - wi::to_wide (@1)); })))))
8184
8185 /* To detect overflow in unsigned A - B, A < B is simpler than A - B > A.
8186 However, the detection logic for SUB_OVERFLOW in tree-ssa-math-opts.cc
8187 expects the long form, so we restrict the transformation for now. */
8188 (for cmp (gt le)
8189 (simplify
8190 (cmp:c (minus@2 @0 @1) @0)
8191 (if (single_use (@2)
8192 && ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
8193 && TYPE_UNSIGNED (TREE_TYPE (@0)))
8194 (cmp @1 @0))))
8195
8196 /* Optimize A - B + -1 >= A into B >= A for unsigned comparisons. */
8197 (for cmp (ge lt)
8198 (simplify
8199 (cmp:c (plus (minus @0 @1) integer_minus_onep) @0)
8200 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
8201 && TYPE_UNSIGNED (TREE_TYPE (@0)))
8202 (cmp @1 @0))))
8203
8204 /* Testing for overflow is unnecessary if we already know the result. */
8205 /* A - B > A */
8206 (for cmp (gt le)
8207 out (ne eq)
8208 (simplify
8209 (cmp:c (realpart (IFN_SUB_OVERFLOW@2 @0 @1)) @0)
8210 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
8211 && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
8212 (out (imagpart @2) { build_zero_cst (TREE_TYPE (@0)); }))))
8213 /* A + B < A */
8214 (for cmp (lt ge)
8215 out (ne eq)
8216 (simplify
8217 (cmp:c (realpart (IFN_ADD_OVERFLOW:c@2 @0 @1)) @0)
8218 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
8219 && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
8220 (out (imagpart @2) { build_zero_cst (TREE_TYPE (@0)); }))))
8221
8222 /* For unsigned operands, -1 / B < A checks whether A * B would overflow.
8223 Simplify it to __builtin_mul_overflow (A, B, <unused>). */
8224 (for cmp (lt ge)
8225 out (ne eq)
8226 (simplify
8227 (cmp:c (trunc_div:s integer_all_onesp @1) @0)
8228 (if (TYPE_UNSIGNED (TREE_TYPE (@0)) && !VECTOR_TYPE_P (TREE_TYPE (@0)))
8229 (with { tree t = TREE_TYPE (@0), cpx = build_complex_type (t); }
8230 (out (imagpart (IFN_MUL_OVERFLOW:cpx @0 @1)) { build_zero_cst (t); })))))
8231
8232 /* Similarly, for unsigned operands, (((type) A * B) >> prec) != 0 where type
8233 is at least twice as wide as type of A and B, simplify to
8234 __builtin_mul_overflow (A, B, <unused>). */
8235 (for cmp (eq ne)
8236 (simplify
8237 (cmp (rshift (mult:s (convert@3 @0) (convert @1)) INTEGER_CST@2)
8238 integer_zerop)
8239 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
8240 && INTEGRAL_TYPE_P (TREE_TYPE (@3))
8241 && TYPE_UNSIGNED (TREE_TYPE (@0))
8242 && (TYPE_PRECISION (TREE_TYPE (@3))
8243 >= 2 * TYPE_PRECISION (TREE_TYPE (@0)))
8244 && tree_fits_uhwi_p (@2)
8245 && tree_to_uhwi (@2) == TYPE_PRECISION (TREE_TYPE (@0))
8246 && types_match (@0, @1)
8247 && type_has_mode_precision_p (TREE_TYPE (@0))
8248 && (optab_handler (umulv4_optab, TYPE_MODE (TREE_TYPE (@0)))
8249 != CODE_FOR_nothing))
8250 (with { tree t = TREE_TYPE (@0), cpx = build_complex_type (t); }
8251 (cmp (imagpart (IFN_MUL_OVERFLOW:cpx @0 @1)) { build_zero_cst (t); })))))
8252
8253 /* Demote operands of IFN_{ADD,SUB,MUL}_OVERFLOW. */
8254 (for ovf (IFN_ADD_OVERFLOW IFN_SUB_OVERFLOW IFN_MUL_OVERFLOW)
8255 (simplify
8256 (ovf (convert@2 @0) @1)
8257 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
8258 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
8259 && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
8260 && (!TYPE_UNSIGNED (TREE_TYPE (@2)) || TYPE_UNSIGNED (TREE_TYPE (@0))))
8261 (ovf @0 @1)))
8262 (simplify
8263 (ovf @1 (convert@2 @0))
8264 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
8265 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
8266 && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
8267 && (!TYPE_UNSIGNED (TREE_TYPE (@2)) || TYPE_UNSIGNED (TREE_TYPE (@0))))
8268 (ovf @1 @0))))
8269
8270 /* Optimize __builtin_mul_overflow_p (x, cst, (utype) 0) if all 3 types
8271 are unsigned to x > (umax / cst). Similarly for signed type, but
8272 in that case it needs to be outside of a range. */
8273 (simplify
8274 (imagpart (IFN_MUL_OVERFLOW:cs@2 @0 integer_nonzerop@1))
8275 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
8276 && TYPE_MAX_VALUE (TREE_TYPE (@0))
8277 && types_match (TREE_TYPE (@0), TREE_TYPE (TREE_TYPE (@2)))
8278 && int_fits_type_p (@1, TREE_TYPE (@0)))
8279 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
8280 (convert (gt @0 (trunc_div! { TYPE_MAX_VALUE (TREE_TYPE (@0)); } @1)))
8281 (if (TYPE_MIN_VALUE (TREE_TYPE (@0)))
8282 (if (!TYPE_UNSIGNED (TREE_TYPE (@1)) && integer_minus_onep (@1))
8283 (convert (eq @0 { TYPE_MIN_VALUE (TREE_TYPE (@0)); }))
8284 (with
8285 {
8286 tree div = fold_convert (TREE_TYPE (@0), @1);
8287 tree lo = int_const_binop (TRUNC_DIV_EXPR,
8288 TYPE_MIN_VALUE (TREE_TYPE (@0)), div);
8289 tree hi = int_const_binop (TRUNC_DIV_EXPR,
8290 TYPE_MAX_VALUE (TREE_TYPE (@0)), div);
8291 tree etype = range_check_type (TREE_TYPE (@0));
8292 if (etype)
8293 {
8294 if (wi::neg_p (wi::to_wide (div)))
8295 std::swap (lo, hi);
8296 lo = fold_convert (etype, lo);
8297 hi = fold_convert (etype, hi);
8298 hi = int_const_binop (MINUS_EXPR, hi, lo);
8299 }
8300 }
8301 (if (etype)
8302 (convert (gt (minus (convert:etype @0) { lo; }) { hi; })))))))))
8303
8304 /* Simplification of math builtins. These rules must all be optimizations
8305 as well as IL simplifications. If there is a possibility that the new
8306 form could be a pessimization, the rule should go in the canonicalization
8307 section that follows this one.
8308
8309 Rules can generally go in this section if they satisfy one of
8310 the following:
8311
8312 - the rule describes an identity
8313
8314 - the rule replaces calls with something as simple as addition or
8315 multiplication
8316
8317 - the rule contains unary calls only and simplifies the surrounding
8318 arithmetic. (The idea here is to exclude non-unary calls in which
8319 one operand is constant and in which the call is known to be cheap
8320 when the operand has that value.) */
8321
8322 (if (flag_unsafe_math_optimizations)
8323 /* Simplify x / sqrt(x) -> sqrt(x). */
8324 (simplify
8325 (rdiv @0 (SQRT @0)) (SQRT @0))
8326
8327 /* Simplify sqrt(x) * sqrt(x) -> x. */
8328 (simplify
8329 (mult (SQRT_ALL@1 @0) @1)
8330 (if (!tree_expr_maybe_signaling_nan_p (@0))
8331 @0))
8332
8333 (for op (plus minus)
8334 /* Simplify (A / C) +- (B / C) -> (A +- B) / C. */
8335 (simplify
8336 (op (rdiv @0 @1)
8337 (rdiv @2 @1))
8338 (rdiv (op @0 @2) @1)))
8339
8340 (for cmp (lt le gt ge)
8341 neg_cmp (gt ge lt le)
8342 /* Simplify (x * C1) cmp C2 -> x cmp (C2 / C1), where C1 != 0. */
8343 (simplify
8344 (cmp (mult @0 REAL_CST@1) REAL_CST@2)
8345 (with
8346 { tree tem = const_binop (RDIV_EXPR, type, @2, @1); }
8347 (if (tem
8348 && !(REAL_VALUE_ISINF (TREE_REAL_CST (tem))
8349 || (real_zerop (tem) && !real_zerop (@1))))
8350 (switch
8351 (if (real_less (&dconst0, TREE_REAL_CST_PTR (@1)))
8352 (cmp @0 { tem; }))
8353 (if (real_less (TREE_REAL_CST_PTR (@1), &dconst0))
8354 (neg_cmp @0 { tem; })))))))
8355
8356 /* Simplify sqrt(x) * sqrt(y) -> sqrt(x*y). */
8357 (for root (SQRT CBRT)
8358 (simplify
8359 (mult (root:s @0) (root:s @1))
8360 (root (mult @0 @1))))
8361
8362 /* Simplify expN(x) * expN(y) -> expN(x+y). */
8363 (for exps (EXP EXP2 EXP10 POW10)
8364 (simplify
8365 (mult (exps:s @0) (exps:s @1))
8366 (exps (plus @0 @1))))
8367
8368 /* Simplify a/root(b/c) into a*root(c/b). */
8369 (for root (SQRT CBRT)
8370 (simplify
8371 (rdiv @0 (root:s (rdiv:s @1 @2)))
8372 (mult @0 (root (rdiv @2 @1)))))
8373
8374 /* Simplify x/expN(y) into x*expN(-y). */
8375 (for exps (EXP EXP2 EXP10 POW10)
8376 (simplify
8377 (rdiv @0 (exps:s @1))
8378 (mult @0 (exps (negate @1)))))
8379
8380 (for pow (POW_ALL)
8381 (if (! HONOR_INFINITIES (type)
8382 && ! flag_trapping_math
8383 && ! flag_errno_math)
8384 /* Simplify pow(1.0/x, y) into pow(x, -y). */
8385 (simplify
8386 (pow (rdiv:s real_onep@0 @1) @2)
8387 (pow @1 (negate @2)))
8388
8389 /* Simplify pow(0.0, x) into 0.0. */
8390 (if (! HONOR_NANS (type) && ! HONOR_SIGNED_ZEROS (type))
8391 (simplify
8392 (pow real_zerop@0 @1)
8393 @0))))
8394
8395 (if (! HONOR_SIGN_DEPENDENT_ROUNDING (type)
8396 && ! HONOR_NANS (type) && ! HONOR_INFINITIES (type)
8397 && ! flag_trapping_math
8398 && ! flag_errno_math)
8399 (for logs (LOG LOG2 LOG10)
8400 /* Simplify logN(1.0/a) into -logN(a). */
8401 (simplify
8402 (logs (rdiv:s real_onep@0 @1))
8403 (negate (logs @1)))
8404
8405 /* Simplify logN(C/a) into logN(C)-logN(a). */
8406 (simplify
8407 (logs (rdiv:s REAL_CST@0 @1))
8408 (minus (logs! @0) (logs @1)))
8409
8410 /* Simplify logN(a)+logN(b) into logN(a*b). */
8411 (simplify
8412 (plus (logs:s @0) (logs:s @1))
8413 (logs (mult @0 @1)))
8414
8415 /* Simplify logN(a)-logN(b) into logN(a/b). */
8416 (simplify
8417 (minus (logs:s @0) (logs:s @1))
8418 (logs (rdiv @0 @1))))
8419
8420 (for cmp (le ge)
8421 (for logs (LOG LOG2 LOG10)
8422 exps (EXP EXP2 EXP10)
8423 /* Simplify logN (x) CMP CST into x CMP expN (CST) */
8424 (simplify
8425 (cmp:c (logs:s @0) REAL_CST@1)
8426 (cmp @0 (exps! @1)))
8427
8428 /* Simplify expN (x) CMP CST into x CMP logN (CST) */
8429 (simplify
8430 (cmp:c (exps:s @0) REAL_CST@1)
8431 (cmp @0 (logs! @1))))))
8432
8433 (for logs (LOG LOG2 LOG10 LOG10)
8434 exps (EXP EXP2 EXP10 POW10)
8435 /* logN(expN(x)) -> x. */
8436 (simplify
8437 (logs (exps @0))
8438 @0)
8439 /* expN(logN(x)) -> x. */
8440 (simplify
8441 (exps (logs @0))
8442 @0))
8443
8444 /* Optimize logN(func()) for various exponential functions. We
8445 want to determine the value "x" and the power "exponent" in
8446 order to transform logN(x**exponent) into exponent*logN(x). */
8447 (for logs (LOG LOG LOG LOG2 LOG2 LOG2 LOG10 LOG10)
8448 exps (EXP2 EXP10 POW10 EXP EXP10 POW10 EXP EXP2)
8449 (simplify
8450 (logs (exps @0))
8451 (if (SCALAR_FLOAT_TYPE_P (type))
8452 (with {
8453 tree x;
8454 switch (exps)
8455 {
8456 CASE_CFN_EXP:
8457 /* Prepare to do logN(exp(exponent)) -> exponent*logN(e). */
8458 x = build_real_truncate (type, dconst_e ());
8459 break;
8460 CASE_CFN_EXP2:
8461 /* Prepare to do logN(exp2(exponent)) -> exponent*logN(2). */
8462 x = build_real (type, dconst2);
8463 break;
8464 CASE_CFN_EXP10:
8465 CASE_CFN_POW10:
8466 /* Prepare to do logN(exp10(exponent)) -> exponent*logN(10). */
8467 {
8468 REAL_VALUE_TYPE dconst10;
8469 real_from_integer (&dconst10, VOIDmode, 10, SIGNED);
8470 x = build_real (type, dconst10);
8471 }
8472 break;
8473 default:
8474 gcc_unreachable ();
8475 }
8476 }
8477 (mult (logs { x; }) @0)))))
8478
8479 (for logs (LOG LOG
8480 LOG2 LOG2
8481 LOG10 LOG10)
8482 exps (SQRT CBRT)
8483 (simplify
8484 (logs (exps @0))
8485 (if (SCALAR_FLOAT_TYPE_P (type))
8486 (with {
8487 tree x;
8488 switch (exps)
8489 {
8490 CASE_CFN_SQRT:
8491 /* Prepare to do logN(sqrt(x)) -> 0.5*logN(x). */
8492 x = build_real (type, dconsthalf);
8493 break;
8494 CASE_CFN_CBRT:
8495 /* Prepare to do logN(cbrt(x)) -> (1/3)*logN(x). */
8496 x = build_real_truncate (type, dconst_third ());
8497 break;
8498 default:
8499 gcc_unreachable ();
8500 }
8501 }
8502 (mult { x; } (logs @0))))))
8503
8504 /* logN(pow(x,exponent)) -> exponent*logN(x). */
8505 (for logs (LOG LOG2 LOG10)
8506 pows (POW)
8507 (simplify
8508 (logs (pows @0 @1))
8509 (mult @1 (logs @0))))
8510
8511 /* pow(C,x) -> exp(log(C)*x) if C > 0,
8512 or if C is a positive power of 2,
8513 pow(C,x) -> exp2(log2(C)*x). */
8514 #if GIMPLE
8515 (for pows (POW)
8516 exps (EXP)
8517 logs (LOG)
8518 exp2s (EXP2)
8519 log2s (LOG2)
8520 (simplify
8521 (pows REAL_CST@0 @1)
8522 (if (real_compare (GT_EXPR, TREE_REAL_CST_PTR (@0), &dconst0)
8523 && real_isfinite (TREE_REAL_CST_PTR (@0))
8524 /* As libmvec doesn't have a vectorized exp2, defer optimizing
8525 the use_exp2 case until after vectorization. It seems actually
8526 beneficial for all constants to postpone this until later,
8527 because exp(log(C)*x), while faster, will have worse precision
8528 and if x folds into a constant too, that is unnecessary
8529 pessimization. */
8530 && canonicalize_math_after_vectorization_p ())
8531 (with {
8532 const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (@0);
8533 bool use_exp2 = false;
8534 if (targetm.libc_has_function (function_c99_misc, TREE_TYPE (@0))
8535 && value->cl == rvc_normal)
8536 {
8537 REAL_VALUE_TYPE frac_rvt = *value;
8538 SET_REAL_EXP (&frac_rvt, 1);
8539 if (real_equal (&frac_rvt, &dconst1))
8540 use_exp2 = true;
8541 }
8542 }
8543 (if (!use_exp2)
8544 (if (optimize_pow_to_exp (@0, @1))
8545 (exps (mult (logs @0) @1)))
8546 (exp2s (mult (log2s @0) @1)))))))
8547 #endif
8548
8549 /* pow(C,x)*expN(y) -> expN(logN(C)*x+y) if C > 0. */
8550 (for pows (POW)
8551 exps (EXP EXP2 EXP10 POW10)
8552 logs (LOG LOG2 LOG10 LOG10)
8553 (simplify
8554 (mult:c (pows:s REAL_CST@0 @1) (exps:s @2))
8555 (if (real_compare (GT_EXPR, TREE_REAL_CST_PTR (@0), &dconst0)
8556 && real_isfinite (TREE_REAL_CST_PTR (@0)))
8557 (exps (plus (mult (logs @0) @1) @2)))))
8558
8559 /* Simplify powi (powof2, i) to ldexp (1, i * log2 (powof2)). */
8560 (simplify
8561 (POWI REAL_CST@0 @1)
8562 (with { HOST_WIDE_INT tmp = 0; }
8563 (if (real_isinteger (&TREE_REAL_CST (@0), &tmp)
8564 && tmp > 0 && pow2p_hwi (tmp))
8565 (LDEXP { build_one_cst (type); }
8566 (mult @1 {build_int_cst (integer_type_node,
8567 exact_log2 (tmp)); })))))
8568
8569 /* Simplify powof2 * ldexp (x, i) to ldexp (x, i + log2 (powof2)) */
8570 (simplify
8571 (mult:c REAL_CST@0 (LDEXP @1 @2))
8572 (with { HOST_WIDE_INT tmp = 0; }
8573 (if (real_isinteger (&TREE_REAL_CST (@0), &tmp)
8574 && tmp > 0 && pow2p_hwi (tmp))
8575 (LDEXP @1 (plus {build_int_cst (integer_type_node,
8576 exact_log2 (tmp)); } @2)))))
8577
8578 /* Simplify a * ldexp (1., i) to ldexp (a, i). */
8579 (simplify
8580 (mult:c @0 (LDEXP REAL_CST@1 @2))
8581 (if (real_equal (TREE_REAL_CST_PTR (@1), &dconst1))
8582 (LDEXP @0 @2)))
8583
8584 (for sqrts (SQRT)
8585 cbrts (CBRT)
8586 pows (POW)
8587 exps (EXP EXP2 EXP10 POW10)
8588 /* sqrt(expN(x)) -> expN(x*0.5). */
8589 (simplify
8590 (sqrts (exps @0))
8591 (exps (mult @0 { build_real (type, dconsthalf); })))
8592 /* cbrt(expN(x)) -> expN(x/3). */
8593 (simplify
8594 (cbrts (exps @0))
8595 (exps (mult @0 { build_real_truncate (type, dconst_third ()); })))
8596 /* pow(expN(x), y) -> expN(x*y). */
8597 (simplify
8598 (pows (exps @0) @1)
8599 (exps (mult @0 @1))))
8600
8601 /* tan(atan(x)) -> x. */
8602 (for tans (TAN)
8603 atans (ATAN)
8604 (simplify
8605 (tans (atans @0))
8606 @0)))
8607
8608 /* Simplify sin(atan(x)) -> x / sqrt(x*x + 1). */
8609 (for sins (SIN)
8610 atans (ATAN)
8611 sqrts (SQRT)
8612 copysigns (COPYSIGN)
8613 (simplify
8614 (sins (atans:s @0))
8615 (with
8616 {
8617 REAL_VALUE_TYPE r_cst;
8618 build_sinatan_real (&r_cst, type);
8619 tree t_cst = build_real (type, r_cst);
8620 tree t_one = build_one_cst (type);
8621 }
8622 (if (SCALAR_FLOAT_TYPE_P (type))
8623 (cond (lt (abs @0) { t_cst; })
8624 (rdiv @0 (sqrts (plus (mult @0 @0) { t_one; })))
8625 (copysigns { t_one; } @0))))))
8626
8627 /* Simplify cos(atan(x)) -> 1 / sqrt(x*x + 1). */
8628 (for coss (COS)
8629 atans (ATAN)
8630 sqrts (SQRT)
8631 copysigns (COPYSIGN)
8632 (simplify
8633 (coss (atans:s @0))
8634 (with
8635 {
8636 REAL_VALUE_TYPE r_cst;
8637 build_sinatan_real (&r_cst, type);
8638 tree t_cst = build_real (type, r_cst);
8639 tree t_one = build_one_cst (type);
8640 tree t_zero = build_zero_cst (type);
8641 }
8642 (if (SCALAR_FLOAT_TYPE_P (type))
8643 (cond (lt (abs @0) { t_cst; })
8644 (rdiv { t_one; } (sqrts (plus (mult @0 @0) { t_one; })))
8645 (copysigns { t_zero; } @0))))))
8646
8647 (if (!flag_errno_math)
8648 /* Simplify sinh(atanh(x)) -> x / sqrt((1 - x)*(1 + x)). */
8649 (for sinhs (SINH)
8650 atanhs (ATANH)
8651 sqrts (SQRT)
8652 (simplify
8653 (sinhs (atanhs:s @0))
8654 (with { tree t_one = build_one_cst (type); }
8655 (rdiv @0 (sqrts (mult (minus { t_one; } @0) (plus { t_one; } @0)))))))
8656
8657 /* Simplify cosh(atanh(x)) -> 1 / sqrt((1 - x)*(1 + x)) */
8658 (for coshs (COSH)
8659 atanhs (ATANH)
8660 sqrts (SQRT)
8661 (simplify
8662 (coshs (atanhs:s @0))
8663 (with { tree t_one = build_one_cst (type); }
8664 (rdiv { t_one; } (sqrts (mult (minus { t_one; } @0) (plus { t_one; } @0))))))))
8665
8666 /* cabs(x+0i) or cabs(0+xi) -> abs(x). */
8667 (simplify
8668 (CABS (complex:C @0 real_zerop@1))
8669 (abs @0))
8670
8671 /* trunc(trunc(x)) -> trunc(x), etc. */
8672 (for fns (TRUNC_ALL FLOOR_ALL CEIL_ALL ROUND_ALL NEARBYINT_ALL RINT_ALL)
8673 (simplify
8674 (fns (fns @0))
8675 (fns @0)))
8676 /* f(x) -> x if x is integer valued and f does nothing for such values. */
8677 (for fns (TRUNC_ALL FLOOR_ALL CEIL_ALL ROUND_ALL NEARBYINT_ALL RINT_ALL)
8678 (simplify
8679 (fns integer_valued_real_p@0)
8680 @0))
8681
8682 /* hypot(x,0) and hypot(0,x) -> abs(x). */
8683 (simplify
8684 (HYPOT:c @0 real_zerop@1)
8685 (abs @0))
8686
8687 /* pow(1,x) -> 1. */
8688 (simplify
8689 (POW real_onep@0 @1)
8690 @0)
8691
8692 (simplify
8693 /* copysign(x,x) -> x. */
8694 (COPYSIGN_ALL @0 @0)
8695 @0)
8696
8697 (simplify
8698 /* copysign(x,-x) -> -x. */
8699 (COPYSIGN_ALL @0 (negate@1 @0))
8700 @1)
8701
8702 (simplify
8703 /* copysign(x,y) -> fabs(x) if y is nonnegative. */
8704 (COPYSIGN_ALL @0 tree_expr_nonnegative_p@1)
8705 (abs @0))
8706
8707 (simplify
8708 /* fabs (copysign(x, y)) -> fabs (x). */
8709 (abs (COPYSIGN_ALL @0 @1))
8710 (abs @0))
8711
8712 (for scale (LDEXP SCALBN SCALBLN)
8713 /* ldexp(0, x) -> 0. */
8714 (simplify
8715 (scale real_zerop@0 @1)
8716 @0)
8717 /* ldexp(x, 0) -> x. */
8718 (simplify
8719 (scale @0 integer_zerop@1)
8720 @0)
8721 /* ldexp(x, y) -> x if x is +-Inf or NaN. */
8722 (simplify
8723 (scale REAL_CST@0 @1)
8724 (if (!real_isfinite (TREE_REAL_CST_PTR (@0)))
8725 @0)))
8726
8727 /* Canonicalization of sequences of math builtins. These rules represent
8728 IL simplifications but are not necessarily optimizations.
8729
8730 The sincos pass is responsible for picking "optimal" implementations
8731 of math builtins, which may be more complicated and can sometimes go
8732 the other way, e.g. converting pow into a sequence of sqrts.
8733 We only want to do these canonicalizations before the pass has run. */
8734
8735 (if (flag_unsafe_math_optimizations && canonicalize_math_p ())
8736 /* Simplify tan(x) * cos(x) -> sin(x). */
8737 (simplify
8738 (mult:c (TAN:s @0) (COS:s @0))
8739 (SIN @0))
8740
8741 /* Simplify x * pow(x,c) -> pow(x,c+1). */
8742 (simplify
8743 (mult:c @0 (POW:s @0 REAL_CST@1))
8744 (if (!TREE_OVERFLOW (@1))
8745 (POW @0 (plus @1 { build_one_cst (type); }))))
8746
8747 /* Simplify sin(x) / cos(x) -> tan(x). */
8748 (simplify
8749 (rdiv (SIN:s @0) (COS:s @0))
8750 (TAN @0))
8751
8752 /* Simplify sinh(x) / cosh(x) -> tanh(x). */
8753 (simplify
8754 (rdiv (SINH:s @0) (COSH:s @0))
8755 (TANH @0))
8756
8757 /* Simplify tanh (x) / sinh (x) -> 1.0 / cosh (x). */
8758 (simplify
8759 (rdiv (TANH:s @0) (SINH:s @0))
8760 (rdiv {build_one_cst (type);} (COSH @0)))
8761
8762 /* Simplify cos(x) / sin(x) -> 1 / tan(x). */
8763 (simplify
8764 (rdiv (COS:s @0) (SIN:s @0))
8765 (rdiv { build_one_cst (type); } (TAN @0)))
8766
8767 /* Simplify sin(x) / tan(x) -> cos(x). */
8768 (simplify
8769 (rdiv (SIN:s @0) (TAN:s @0))
8770 (if (! HONOR_NANS (@0)
8771 && ! HONOR_INFINITIES (@0))
8772 (COS @0)))
8773
8774 /* Simplify tan(x) / sin(x) -> 1.0 / cos(x). */
8775 (simplify
8776 (rdiv (TAN:s @0) (SIN:s @0))
8777 (if (! HONOR_NANS (@0)
8778 && ! HONOR_INFINITIES (@0))
8779 (rdiv { build_one_cst (type); } (COS @0))))
8780
8781 /* Simplify pow(x,y) * pow(x,z) -> pow(x,y+z). */
8782 (simplify
8783 (mult (POW:s @0 @1) (POW:s @0 @2))
8784 (POW @0 (plus @1 @2)))
8785
8786 /* Simplify pow(x,y) * pow(z,y) -> pow(x*z,y). */
8787 (simplify
8788 (mult (POW:s @0 @1) (POW:s @2 @1))
8789 (POW (mult @0 @2) @1))
8790
8791 (if (! HONOR_INFINITIES (type) && ! flag_trapping_math)
8792 /* Simplify powi(1.0/x, y) into powi(x, -y). */
8793 (simplify
8794 (POWI (rdiv@3 real_onep@0 @1) @2)
8795 (if (single_use (@3))
8796 (POWI @1 (negate @2))))
8797
8798 /* Simplify powi(0.0, x) into 0.0. */
8799 (if (! HONOR_NANS (type) && ! HONOR_SIGNED_ZEROS (type))
8800 (simplify
8801 (POWI real_zerop@0 @1)
8802 @0)))
8803
8804 /* Simplify powi(x,y) * powi(z,y) -> powi(x*z,y). */
8805 (simplify
8806 (mult (POWI:s @0 @1) (POWI:s @2 @1))
8807 (POWI (mult @0 @2) @1))
8808
8809 /* Simplify pow(x,c) / x -> pow(x,c-1). */
8810 (simplify
8811 (rdiv (POW:s @0 REAL_CST@1) @0)
8812 (if (!TREE_OVERFLOW (@1))
8813 (POW @0 (minus @1 { build_one_cst (type); }))))
8814
8815 /* Simplify x / pow (y,z) -> x * pow(y,-z). */
8816 (simplify
8817 (rdiv @0 (POW:s @1 @2))
8818 (mult @0 (POW @1 (negate @2))))
8819
8820 (for sqrts (SQRT)
8821 cbrts (CBRT)
8822 pows (POW)
8823 /* sqrt(sqrt(x)) -> pow(x,1/4). */
8824 (simplify
8825 (sqrts (sqrts @0))
8826 (pows @0 { build_real (type, dconst_quarter ()); }))
8827 /* sqrt(cbrt(x)) -> pow(x,1/6). */
8828 (simplify
8829 (sqrts (cbrts @0))
8830 (pows @0 { build_real_truncate (type, dconst_sixth ()); }))
8831 /* cbrt(sqrt(x)) -> pow(x,1/6). */
8832 (simplify
8833 (cbrts (sqrts @0))
8834 (pows @0 { build_real_truncate (type, dconst_sixth ()); }))
8835 /* cbrt(cbrt(x)) -> pow(x,1/9), iff x is nonnegative. */
8836 (simplify
8837 (cbrts (cbrts tree_expr_nonnegative_p@0))
8838 (pows @0 { build_real_truncate (type, dconst_ninth ()); }))
8839 /* sqrt(pow(x,y)) -> pow(|x|,y*0.5). */
8840 (simplify
8841 (sqrts (pows @0 @1))
8842 (pows (abs @0) (mult @1 { build_real (type, dconsthalf); })))
8843 /* cbrt(pow(x,y)) -> pow(x,y/3), iff x is nonnegative. */
8844 (simplify
8845 (cbrts (pows tree_expr_nonnegative_p@0 @1))
8846 (pows @0 (mult @1 { build_real_truncate (type, dconst_third ()); })))
8847 /* pow(sqrt(x),y) -> pow(x,y*0.5). */
8848 (simplify
8849 (pows (sqrts @0) @1)
8850 (pows @0 (mult @1 { build_real (type, dconsthalf); })))
8851 /* pow(cbrt(x),y) -> pow(x,y/3) iff x is nonnegative. */
8852 (simplify
8853 (pows (cbrts tree_expr_nonnegative_p@0) @1)
8854 (pows @0 (mult @1 { build_real_truncate (type, dconst_third ()); })))
8855 /* pow(pow(x,y),z) -> pow(x,y*z) iff x is nonnegative. */
8856 (simplify
8857 (pows (pows tree_expr_nonnegative_p@0 @1) @2)
8858 (pows @0 (mult @1 @2))))
8859
8860 /* cabs(x+xi) -> fabs(x)*sqrt(2). */
8861 (simplify
8862 (CABS (complex @0 @0))
8863 (mult (abs @0) { build_real_truncate (type, dconst_sqrt2 ()); }))
8864
8865 /* hypot(x,x) -> fabs(x)*sqrt(2). */
8866 (simplify
8867 (HYPOT @0 @0)
8868 (mult (abs @0) { build_real_truncate (type, dconst_sqrt2 ()); }))
8869
8870 /* cexp(x+yi) -> exp(x)*cexpi(y). */
8871 (for cexps (CEXP)
8872 exps (EXP)
8873 cexpis (CEXPI)
8874 (simplify
8875 (cexps compositional_complex@0)
8876 (if (targetm.libc_has_function (function_c99_math_complex, TREE_TYPE (@0)))
8877 (complex
8878 (mult (exps@1 (realpart @0)) (realpart (cexpis:type@2 (imagpart @0))))
8879 (mult @1 (imagpart @2)))))))
8880
8881 (if (canonicalize_math_p ())
8882 /* floor(x) -> trunc(x) if x is nonnegative. */
8883 (for floors (FLOOR_ALL)
8884 truncs (TRUNC_ALL)
8885 (simplify
8886 (floors tree_expr_nonnegative_p@0)
8887 (truncs @0))))
8888
8889 (match double_value_p
8890 @0
8891 (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == double_type_node)))
8892 (for froms (BUILT_IN_TRUNCL
8893 BUILT_IN_FLOORL
8894 BUILT_IN_CEILL
8895 BUILT_IN_ROUNDL
8896 BUILT_IN_NEARBYINTL
8897 BUILT_IN_RINTL)
8898 tos (BUILT_IN_TRUNC
8899 BUILT_IN_FLOOR
8900 BUILT_IN_CEIL
8901 BUILT_IN_ROUND
8902 BUILT_IN_NEARBYINT
8903 BUILT_IN_RINT)
8904 /* truncl(extend(x)) -> extend(trunc(x)), etc., if x is a double. */
8905 (if (optimize && canonicalize_math_p ())
8906 (simplify
8907 (froms (convert double_value_p@0))
8908 (convert (tos @0)))))
8909
8910 (match float_value_p
8911 @0
8912 (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == float_type_node)))
8913 (for froms (BUILT_IN_TRUNCL BUILT_IN_TRUNC
8914 BUILT_IN_FLOORL BUILT_IN_FLOOR
8915 BUILT_IN_CEILL BUILT_IN_CEIL
8916 BUILT_IN_ROUNDL BUILT_IN_ROUND
8917 BUILT_IN_NEARBYINTL BUILT_IN_NEARBYINT
8918 BUILT_IN_RINTL BUILT_IN_RINT)
8919 tos (BUILT_IN_TRUNCF BUILT_IN_TRUNCF
8920 BUILT_IN_FLOORF BUILT_IN_FLOORF
8921 BUILT_IN_CEILF BUILT_IN_CEILF
8922 BUILT_IN_ROUNDF BUILT_IN_ROUNDF
8923 BUILT_IN_NEARBYINTF BUILT_IN_NEARBYINTF
8924 BUILT_IN_RINTF BUILT_IN_RINTF)
8925 /* truncl(extend(x)) and trunc(extend(x)) -> extend(truncf(x)), etc.,
8926 if x is a float. */
8927 (if (optimize && canonicalize_math_p ()
8928 && targetm.libc_has_function (function_c99_misc, NULL_TREE))
8929 (simplify
8930 (froms (convert float_value_p@0))
8931 (convert (tos @0)))))
8932
8933 #if GIMPLE
8934 (match float16_value_p
8935 @0
8936 (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == float16_type_node)))
8937 (for froms (BUILT_IN_TRUNCL BUILT_IN_TRUNC BUILT_IN_TRUNCF
8938 BUILT_IN_FLOORL BUILT_IN_FLOOR BUILT_IN_FLOORF
8939 BUILT_IN_CEILL BUILT_IN_CEIL BUILT_IN_CEILF
8940 BUILT_IN_ROUNDEVENL BUILT_IN_ROUNDEVEN BUILT_IN_ROUNDEVENF
8941 BUILT_IN_ROUNDL BUILT_IN_ROUND BUILT_IN_ROUNDF
8942 BUILT_IN_NEARBYINTL BUILT_IN_NEARBYINT BUILT_IN_NEARBYINTF
8943 BUILT_IN_RINTL BUILT_IN_RINT BUILT_IN_RINTF
8944 BUILT_IN_SQRTL BUILT_IN_SQRT BUILT_IN_SQRTF)
8945 tos (IFN_TRUNC IFN_TRUNC IFN_TRUNC
8946 IFN_FLOOR IFN_FLOOR IFN_FLOOR
8947 IFN_CEIL IFN_CEIL IFN_CEIL
8948 IFN_ROUNDEVEN IFN_ROUNDEVEN IFN_ROUNDEVEN
8949 IFN_ROUND IFN_ROUND IFN_ROUND
8950 IFN_NEARBYINT IFN_NEARBYINT IFN_NEARBYINT
8951 IFN_RINT IFN_RINT IFN_RINT
8952 IFN_SQRT IFN_SQRT IFN_SQRT)
8953 /* (_Float16) round ((doube) x) -> __built_in_roundf16 (x), etc.,
8954 if x is a _Float16. */
8955 (simplify
8956 (convert (froms (convert float16_value_p@0)))
8957 (if (optimize
8958 && types_match (type, TREE_TYPE (@0))
8959 && direct_internal_fn_supported_p (as_internal_fn (tos),
8960 type, OPTIMIZE_FOR_BOTH))
8961 (tos @0))))
8962
8963 /* Simplify (trunc)copysign ((extend)x, (extend)y) to copysignf (x, y),
8964 x,y is float value, similar for _Float16/double. */
8965 (for copysigns (COPYSIGN_ALL)
8966 (simplify
8967 (convert (copysigns (convert@2 @0) (convert @1)))
8968 (if (optimize
8969 && !HONOR_SNANS (@2)
8970 && types_match (type, TREE_TYPE (@0))
8971 && types_match (type, TREE_TYPE (@1))
8972 && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (@2))
8973 && direct_internal_fn_supported_p (IFN_COPYSIGN,
8974 type, OPTIMIZE_FOR_BOTH))
8975 (IFN_COPYSIGN @0 @1))))
8976
8977 (for froms (BUILT_IN_FMAF BUILT_IN_FMA BUILT_IN_FMAL)
8978 tos (IFN_FMA IFN_FMA IFN_FMA)
8979 (simplify
8980 (convert (froms (convert@3 @0) (convert @1) (convert @2)))
8981 (if (flag_unsafe_math_optimizations
8982 && optimize
8983 && FLOAT_TYPE_P (type)
8984 && FLOAT_TYPE_P (TREE_TYPE (@3))
8985 && types_match (type, TREE_TYPE (@0))
8986 && types_match (type, TREE_TYPE (@1))
8987 && types_match (type, TREE_TYPE (@2))
8988 && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (@3))
8989 && direct_internal_fn_supported_p (as_internal_fn (tos),
8990 type, OPTIMIZE_FOR_BOTH))
8991 (tos @0 @1 @2))))
8992
8993 (for maxmin (max min)
8994 (simplify
8995 (convert (maxmin (convert@2 @0) (convert @1)))
8996 (if (optimize
8997 && FLOAT_TYPE_P (type)
8998 && FLOAT_TYPE_P (TREE_TYPE (@2))
8999 && types_match (type, TREE_TYPE (@0))
9000 && types_match (type, TREE_TYPE (@1))
9001 && element_precision (type) < element_precision (TREE_TYPE (@2)))
9002 (maxmin @0 @1))))
9003 #endif
9004
9005 (for froms (XFLOORL XCEILL XROUNDL XRINTL)
9006 tos (XFLOOR XCEIL XROUND XRINT)
9007 /* llfloorl(extend(x)) -> llfloor(x), etc., if x is a double. */
9008 (if (optimize && canonicalize_math_p ())
9009 (simplify
9010 (froms (convert double_value_p@0))
9011 (tos @0))))
9012
9013 (for froms (XFLOORL XCEILL XROUNDL XRINTL
9014 XFLOOR XCEIL XROUND XRINT)
9015 tos (XFLOORF XCEILF XROUNDF XRINTF)
9016 /* llfloorl(extend(x)) and llfloor(extend(x)) -> llfloorf(x), etc.,
9017 if x is a float. */
9018 (if (optimize && canonicalize_math_p ())
9019 (simplify
9020 (froms (convert float_value_p@0))
9021 (tos @0))))
9022
9023 (if (canonicalize_math_p ())
9024 /* xfloor(x) -> fix_trunc(x) if x is nonnegative. */
9025 (for floors (IFLOOR LFLOOR LLFLOOR)
9026 (simplify
9027 (floors tree_expr_nonnegative_p@0)
9028 (fix_trunc @0))))
9029
9030 (if (canonicalize_math_p ())
9031 /* xfloor(x) -> fix_trunc(x), etc., if x is integer valued. */
9032 (for fns (IFLOOR LFLOOR LLFLOOR
9033 ICEIL LCEIL LLCEIL
9034 IROUND LROUND LLROUND)
9035 (simplify
9036 (fns integer_valued_real_p@0)
9037 (fix_trunc @0)))
9038 (if (!flag_errno_math)
9039 /* xrint(x) -> fix_trunc(x), etc., if x is integer valued. */
9040 (for rints (IRINT LRINT LLRINT)
9041 (simplify
9042 (rints integer_valued_real_p@0)
9043 (fix_trunc @0)))))
9044
9045 (if (canonicalize_math_p ())
9046 (for ifn (IFLOOR ICEIL IROUND IRINT)
9047 lfn (LFLOOR LCEIL LROUND LRINT)
9048 llfn (LLFLOOR LLCEIL LLROUND LLRINT)
9049 /* Canonicalize iround (x) to lround (x) on ILP32 targets where
9050 sizeof (int) == sizeof (long). */
9051 (if (TYPE_PRECISION (integer_type_node)
9052 == TYPE_PRECISION (long_integer_type_node))
9053 (simplify
9054 (ifn @0)
9055 (lfn:long_integer_type_node @0)))
9056 /* Canonicalize llround (x) to lround (x) on LP64 targets where
9057 sizeof (long long) == sizeof (long). */
9058 (if (TYPE_PRECISION (long_long_integer_type_node)
9059 == TYPE_PRECISION (long_integer_type_node))
9060 (simplify
9061 (llfn @0)
9062 (lfn:long_integer_type_node @0)))))
9063
9064 /* cproj(x) -> x if we're ignoring infinities. */
9065 (simplify
9066 (CPROJ @0)
9067 (if (!HONOR_INFINITIES (type))
9068 @0))
9069
9070 /* If the real part is inf and the imag part is known to be
9071 nonnegative, return (inf + 0i). */
9072 (simplify
9073 (CPROJ (complex REAL_CST@0 tree_expr_nonnegative_p@1))
9074 (if (real_isinf (TREE_REAL_CST_PTR (@0)))
9075 { build_complex_inf (type, false); }))
9076
9077 /* If the imag part is inf, return (inf+I*copysign(0,imag)). */
9078 (simplify
9079 (CPROJ (complex @0 REAL_CST@1))
9080 (if (real_isinf (TREE_REAL_CST_PTR (@1)))
9081 { build_complex_inf (type, TREE_REAL_CST_PTR (@1)->sign); }))
9082
9083 (for pows (POW)
9084 sqrts (SQRT)
9085 cbrts (CBRT)
9086 (simplify
9087 (pows @0 REAL_CST@1)
9088 (with {
9089 const REAL_VALUE_TYPE *value = TREE_REAL_CST_PTR (@1);
9090 REAL_VALUE_TYPE tmp;
9091 }
9092 (switch
9093 /* pow(x,0) -> 1. */
9094 (if (real_equal (value, &dconst0))
9095 { build_real (type, dconst1); })
9096 /* pow(x,1) -> x. */
9097 (if (real_equal (value, &dconst1))
9098 @0)
9099 /* pow(x,-1) -> 1/x. */
9100 (if (real_equal (value, &dconstm1))
9101 (rdiv { build_real (type, dconst1); } @0))
9102 /* pow(x,0.5) -> sqrt(x). */
9103 (if (flag_unsafe_math_optimizations
9104 && canonicalize_math_p ()
9105 && real_equal (value, &dconsthalf))
9106 (sqrts @0))
9107 /* pow(x,1/3) -> cbrt(x). */
9108 (if (flag_unsafe_math_optimizations
9109 && canonicalize_math_p ()
9110 && (tmp = real_value_truncate (TYPE_MODE (type), dconst_third ()),
9111 real_equal (value, &tmp)))
9112 (cbrts @0))))))
9113
9114 /* powi(1,x) -> 1. */
9115 (simplify
9116 (POWI real_onep@0 @1)
9117 @0)
9118
9119 (simplify
9120 (POWI @0 INTEGER_CST@1)
9121 (switch
9122 /* powi(x,0) -> 1. */
9123 (if (wi::to_wide (@1) == 0)
9124 { build_real (type, dconst1); })
9125 /* powi(x,1) -> x. */
9126 (if (wi::to_wide (@1) == 1)
9127 @0)
9128 /* powi(x,-1) -> 1/x. */
9129 (if (wi::to_wide (@1) == -1)
9130 (rdiv { build_real (type, dconst1); } @0))))
9131
9132 /* Narrowing of arithmetic and logical operations.
9133
9134 These are conceptually similar to the transformations performed for
9135 the C/C++ front-ends by shorten_binary_op and shorten_compare. Long
9136 term we want to move all that code out of the front-ends into here. */
9137
9138 /* Convert (outertype)((innertype0)a+(innertype1)b)
9139 into ((newtype)a+(newtype)b) where newtype
9140 is the widest mode from all of these. */
9141 (for op (plus minus mult rdiv)
9142 (simplify
9143 (convert (op:s@0 (convert1?@3 @1) (convert2?@4 @2)))
9144 /* If we have a narrowing conversion of an arithmetic operation where
9145 both operands are widening conversions from the same type as the outer
9146 narrowing conversion. Then convert the innermost operands to a
9147 suitable unsigned type (to avoid introducing undefined behavior),
9148 perform the operation and convert the result to the desired type. */
9149 (if (INTEGRAL_TYPE_P (type)
9150 && op != MULT_EXPR
9151 && op != RDIV_EXPR
9152 /* We check for type compatibility between @0 and @1 below,
9153 so there's no need to check that @2/@4 are integral types. */
9154 && INTEGRAL_TYPE_P (TREE_TYPE (@1))
9155 && INTEGRAL_TYPE_P (TREE_TYPE (@3))
9156 /* The precision of the type of each operand must match the
9157 precision of the mode of each operand, similarly for the
9158 result. */
9159 && type_has_mode_precision_p (TREE_TYPE (@1))
9160 && type_has_mode_precision_p (TREE_TYPE (@2))
9161 && type_has_mode_precision_p (type)
9162 /* The inner conversion must be a widening conversion. */
9163 && TYPE_PRECISION (TREE_TYPE (@3)) > TYPE_PRECISION (TREE_TYPE (@1))
9164 && types_match (@1, type)
9165 && (types_match (@1, @2)
9166 /* Or the second operand is const integer or converted const
9167 integer from valueize. */
9168 || poly_int_tree_p (@4)))
9169 (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
9170 (op @1 (convert @2))
9171 (with { tree utype = unsigned_type_for (TREE_TYPE (@1)); }
9172 (convert (op (convert:utype @1)
9173 (convert:utype @2)))))
9174 (if (FLOAT_TYPE_P (type)
9175 && DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0))
9176 == DECIMAL_FLOAT_TYPE_P (type))
9177 (with { tree arg0 = strip_float_extensions (@1);
9178 tree arg1 = strip_float_extensions (@2);
9179 tree itype = TREE_TYPE (@0);
9180 tree ty1 = TREE_TYPE (arg0);
9181 tree ty2 = TREE_TYPE (arg1);
9182 enum tree_code code = TREE_CODE (itype); }
9183 (if (FLOAT_TYPE_P (ty1)
9184 && FLOAT_TYPE_P (ty2))
9185 (with { tree newtype = type;
9186 if (TYPE_MODE (ty1) == SDmode
9187 || TYPE_MODE (ty2) == SDmode
9188 || TYPE_MODE (type) == SDmode)
9189 newtype = dfloat32_type_node;
9190 if (TYPE_MODE (ty1) == DDmode
9191 || TYPE_MODE (ty2) == DDmode
9192 || TYPE_MODE (type) == DDmode)
9193 newtype = dfloat64_type_node;
9194 if (TYPE_MODE (ty1) == TDmode
9195 || TYPE_MODE (ty2) == TDmode
9196 || TYPE_MODE (type) == TDmode)
9197 newtype = dfloat128_type_node; }
9198 (if ((newtype == dfloat32_type_node
9199 || newtype == dfloat64_type_node
9200 || newtype == dfloat128_type_node)
9201 && newtype == type
9202 && types_match (newtype, type))
9203 (op (convert:newtype @1) (convert:newtype @2))
9204 (with { if (element_precision (ty1) > element_precision (newtype))
9205 newtype = ty1;
9206 if (element_precision (ty2) > element_precision (newtype))
9207 newtype = ty2; }
9208 /* Sometimes this transformation is safe (cannot
9209 change results through affecting double rounding
9210 cases) and sometimes it is not. If NEWTYPE is
9211 wider than TYPE, e.g. (float)((long double)double
9212 + (long double)double) converted to
9213 (float)(double + double), the transformation is
9214 unsafe regardless of the details of the types
9215 involved; double rounding can arise if the result
9216 of NEWTYPE arithmetic is a NEWTYPE value half way
9217 between two representable TYPE values but the
9218 exact value is sufficiently different (in the
9219 right direction) for this difference to be
9220 visible in ITYPE arithmetic. If NEWTYPE is the
9221 same as TYPE, however, the transformation may be
9222 safe depending on the types involved: it is safe
9223 if the ITYPE has strictly more than twice as many
9224 mantissa bits as TYPE, can represent infinities
9225 and NaNs if the TYPE can, and has sufficient
9226 exponent range for the product or ratio of two
9227 values representable in the TYPE to be within the
9228 range of normal values of ITYPE. */
9229 (if (element_precision (newtype) < element_precision (itype)
9230 && (!VECTOR_MODE_P (TYPE_MODE (newtype))
9231 || target_supports_op_p (newtype, op, optab_default))
9232 && (flag_unsafe_math_optimizations
9233 || (element_precision (newtype) == element_precision (type)
9234 && real_can_shorten_arithmetic (element_mode (itype),
9235 element_mode (type))
9236 && !excess_precision_type (newtype)))
9237 && !types_match (itype, newtype))
9238 (convert:type (op (convert:newtype @1)
9239 (convert:newtype @2)))
9240 )))) )
9241 ))
9242 )))
9243
9244 /* This is another case of narrowing, specifically when there's an outer
9245 BIT_AND_EXPR which masks off bits outside the type of the innermost
9246 operands. Like the previous case we have to convert the operands
9247 to unsigned types to avoid introducing undefined behavior for the
9248 arithmetic operation. */
9249 (for op (minus plus)
9250 (simplify
9251 (bit_and (op:s (convert@2 @0) (convert@3 @1)) INTEGER_CST@4)
9252 (if (INTEGRAL_TYPE_P (type)
9253 /* We check for type compatibility between @0 and @1 below,
9254 so there's no need to check that @1/@3 are integral types. */
9255 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
9256 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
9257 /* The precision of the type of each operand must match the
9258 precision of the mode of each operand, similarly for the
9259 result. */
9260 && type_has_mode_precision_p (TREE_TYPE (@0))
9261 && type_has_mode_precision_p (TREE_TYPE (@1))
9262 && type_has_mode_precision_p (type)
9263 /* The inner conversion must be a widening conversion. */
9264 && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
9265 && types_match (@0, @1)
9266 && (tree_int_cst_min_precision (@4, TYPE_SIGN (TREE_TYPE (@0)))
9267 <= TYPE_PRECISION (TREE_TYPE (@0)))
9268 && (wi::to_wide (@4)
9269 & wi::mask (TYPE_PRECISION (TREE_TYPE (@0)),
9270 true, TYPE_PRECISION (type))) == 0)
9271 (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
9272 (with { tree ntype = TREE_TYPE (@0); }
9273 (convert (bit_and (op @0 @1) (convert:ntype @4))))
9274 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
9275 (convert (bit_and (op (convert:utype @0) (convert:utype @1))
9276 (convert:utype @4))))))))
9277
9278 /* Transform (@0 < @1 and @0 < @2) to use min,
9279 (@0 > @1 and @0 > @2) to use max */
9280 (for logic (bit_and bit_and bit_and bit_and bit_ior bit_ior bit_ior bit_ior)
9281 op (lt le gt ge lt le gt ge )
9282 ext (min min max max max max min min )
9283 (simplify
9284 (logic (op:cs @0 @1) (op:cs @0 @2))
9285 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
9286 && TREE_CODE (@0) != INTEGER_CST)
9287 (op @0 (ext @1 @2)))))
9288
9289 /* Max<bool0, bool1> -> bool0 | bool1
9290 Min<bool0, bool1> -> bool0 & bool1 */
9291 (for op (max min)
9292 logic (bit_ior bit_and)
9293 (simplify
9294 (op zero_one_valued_p@0 zero_one_valued_p@1)
9295 (logic @0 @1)))
9296
9297 /* signbit(x) != 0 ? -x : x -> abs(x)
9298 signbit(x) == 0 ? -x : x -> -abs(x) */
9299 (for sign (SIGNBIT)
9300 (for neeq (ne eq)
9301 (simplify
9302 (cond (neeq (sign @0) integer_zerop) (negate @0) @0)
9303 (if (neeq == NE_EXPR)
9304 (abs @0)
9305 (negate (abs @0))))))
9306
9307 (simplify
9308 /* signbit(x) -> 0 if x is nonnegative. */
9309 (SIGNBIT tree_expr_nonnegative_p@0)
9310 { integer_zero_node; })
9311
9312 (simplify
9313 /* signbit(x) -> x<0 if x doesn't have signed zeros. */
9314 (SIGNBIT @0)
9315 (if (!HONOR_SIGNED_ZEROS (@0))
9316 (convert (lt @0 { build_real (TREE_TYPE (@0), dconst0); }))))
9317
9318 /* Transform comparisons of the form X +- C1 CMP C2 to X CMP C2 -+ C1. */
9319 (for cmp (eq ne)
9320 (for op (plus minus)
9321 rop (minus plus)
9322 (simplify
9323 (cmp (op@3 @0 INTEGER_CST@1) INTEGER_CST@2)
9324 (if (!TREE_OVERFLOW (@1) && !TREE_OVERFLOW (@2)
9325 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0))
9326 && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@0))
9327 && !TYPE_SATURATING (TREE_TYPE (@0)))
9328 (with { tree res = int_const_binop (rop, @2, @1); }
9329 (if (TREE_OVERFLOW (res)
9330 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
9331 { constant_boolean_node (cmp == NE_EXPR, type); }
9332 (if (single_use (@3))
9333 (cmp @0 { TREE_OVERFLOW (res)
9334 ? drop_tree_overflow (res) : res; }))))))))
9335 (for cmp (lt le gt ge)
9336 rcmp (gt ge lt le)
9337 (for op (plus minus)
9338 rop (minus plus)
9339 (simplify
9340 (cmp (op@3 @0 INTEGER_CST@1) INTEGER_CST@2)
9341 (if (!TREE_OVERFLOW (@1) && !TREE_OVERFLOW (@2)
9342 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
9343 (with { tree res = int_const_binop (rop, @2, @1); }
9344 (if (TREE_OVERFLOW (res))
9345 {
9346 fold_overflow_warning (("assuming signed overflow does not occur "
9347 "when simplifying conditional to constant"),
9348 WARN_STRICT_OVERFLOW_CONDITIONAL);
9349 bool less = cmp == LE_EXPR || cmp == LT_EXPR;
9350 /* wi::ges_p (@2, 0) should be sufficient for a signed type. */
9351 bool ovf_high = wi::lt_p (wi::to_wide (@1), 0,
9352 TYPE_SIGN (TREE_TYPE (@1)))
9353 != (op == MINUS_EXPR);
9354 constant_boolean_node (less == ovf_high, type);
9355 }
9356 (if (single_use (@3))
9357 (with
9358 {
9359 fold_overflow_warning (("assuming signed overflow does not occur "
9360 "when changing X +- C1 cmp C2 to "
9361 "X cmp C2 -+ C1"),
9362 WARN_STRICT_OVERFLOW_COMPARISON);
9363 }
9364 (cmp @0 { res; })))))
9365 /* For wrapping types, simplify the following cases of X +- C1 CMP C2:
9366
9367 (a) If CMP is <= and C2 -+ C1 == +INF (1), simplify to X >= -INF -+ C1
9368 by observing the following:
9369
9370 X +- C1 <= C2
9371 ==> -INF <= X +- C1 <= C2 (add left hand side which holds for any X, C1)
9372 ==> -INF -+ C1 <= X <= C2 -+ C1 (add -+C1 to all 3 expressions)
9373 ==> -INF -+ C1 <= X <= +INF (due to (1))
9374 ==> -INF -+ C1 <= X (eliminate the right hand side since it holds for any X)
9375
9376 (b) Similarly, if CMP is >= and C2 -+ C1 == -INF (1):
9377
9378 X +- C1 >= C2
9379 ==> +INF >= X +- C1 >= C2 (add left hand side which holds for any X, C1)
9380 ==> +INF -+ C1 >= X >= C2 -+ C1 (add -+C1 to all 3 expressions)
9381 ==> +INF -+ C1 >= X >= -INF (due to (1))
9382 ==> +INF -+ C1 >= X (eliminate the right hand side since it holds for any X)
9383
9384 (c) The > and < cases are negations of (a) and (b), respectively. */
9385 (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
9386 (with
9387 {
9388 wide_int max = wi::max_value (TREE_TYPE (@0));
9389 wide_int min = wi::min_value (TREE_TYPE (@0));
9390
9391 wide_int c2 = rop == PLUS_EXPR
9392 ? wi::add (wi::to_wide (@2), wi::to_wide (@1))
9393 : wi::sub (wi::to_wide (@2), wi::to_wide (@1));
9394 }
9395 (if (((cmp == LE_EXPR || cmp == GT_EXPR) && wi::eq_p (c2, max))
9396 || ((cmp == LT_EXPR || cmp == GE_EXPR) && wi::eq_p (c2, min)))
9397 (with
9398 {
9399 wide_int c1 = rop == PLUS_EXPR
9400 ? wi::add (wi::bit_not (c2), wi::to_wide (@1))
9401 : wi::sub (wi::bit_not (c2), wi::to_wide (@1));
9402 tree c1_cst = wide_int_to_tree (TREE_TYPE (@0), c1);
9403 }
9404 (rcmp @0 { c1_cst; })))))))))
9405
9406 /* Invert sign of X in comparisons of the form C1 - X CMP C2. */
9407
9408 (for cmp (lt le gt ge eq ne)
9409 rcmp (gt ge lt le eq ne)
9410 (simplify
9411 (cmp (minus INTEGER_CST@0 @1) INTEGER_CST@2)
9412 /* For UB-on-overflow types, simply switch sides for X and C2
9413 to arrive at X RCMP C1 - C2, handling the case when the latter
9414 expression overflows. */
9415 (if (!TREE_OVERFLOW (@0) && !TREE_OVERFLOW (@2)
9416 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1)))
9417 (with { tree res = int_const_binop (MINUS_EXPR, @0, @2); }
9418 (if (TREE_OVERFLOW (res))
9419 (switch
9420 (if (cmp == NE_EXPR)
9421 { constant_boolean_node (true, type); })
9422 (if (cmp == EQ_EXPR)
9423 { constant_boolean_node (false, type); })
9424 {
9425 bool less = cmp == LE_EXPR || cmp == LT_EXPR;
9426 bool ovf_high = wi::lt_p (wi::to_wide (@0), 0,
9427 TYPE_SIGN (TREE_TYPE (@0)));
9428 constant_boolean_node (less == ovf_high, type);
9429 })
9430 (rcmp @1 { res; })))
9431 /* For unsigned types, transform like so (using < as example):
9432 C1 - X < C2
9433 ==> C1 - X = { 0, 1, ..., C2 - 1 }
9434 ==> X = { C1 - (C2 - 1), ..., C1 + 1, C1 }
9435 ==> X - (C1 - (C2 - 1)) = { 0, 1, ..., C1 - (C1 - (C2 - 1)) }
9436 ==> X - (C1 - C2 + 1) = { 0, 1, ..., C2 - 1 }
9437 ==> X - (C1 - C2 + 1) < C2.
9438
9439 Similarly,
9440 C1 - X <= C2 ==> X - (C1 - C2) <= C2;
9441 C1 - X >= C2 ==> X - (C1 - C2 + 1) >= C2;
9442 C1 - X > C2 ==> X - (C1 - C2) > C2. */
9443 (if (TYPE_UNSIGNED (TREE_TYPE (@1)))
9444 (switch
9445 (if (cmp == EQ_EXPR || cmp == NE_EXPR)
9446 (cmp @1 (minus @0 @2)))
9447 (if (cmp == LE_EXPR || cmp == GT_EXPR)
9448 (cmp (plus @1 (minus @2 @0)) @2))
9449 (if (cmp == LT_EXPR || cmp == GE_EXPR)
9450 (cmp (plus @1 (minus @2
9451 (plus @0 { build_one_cst (TREE_TYPE (@1)); }))) @2)))
9452 /* For wrapping signed types (-fwrapv), transform like so (using < as example):
9453 C1 - X < C2
9454 ==> C1 - X = { -INF, -INF + 1, ..., C2 - 1 }
9455 ==> X = { C1 - (-INF), C1 - (-INF + 1), ..., C1 - C2 + 1 }
9456 ==> X - (C1 + 1) = { - (-INF) - 1, - (-INF) - 2, ..., -C2 }
9457 ==> X - (C1 + 1) = { +INF, +INF - 1, ..., -C2 }
9458 ==> X - (C1 + 1) > -C2 - 1
9459 ==> X - (C1 + 1) > bit_not (C2)
9460
9461 Similarly,
9462 C1 - X <= C2 ==> X - (C1 + 1) >= bit_not (C2);
9463 C1 - X >= C2 ==> X - (C1 + 1) <= bit_not (C2);
9464 C1 - X > C2 ==> X - (C1 + 1) < bit_not (C2). */
9465 (if (!TYPE_UNSIGNED (TREE_TYPE (@1))
9466 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
9467 (if (cmp == EQ_EXPR || cmp == NE_EXPR)
9468 (cmp @1 (minus @0 @2))
9469 (rcmp (minus @1 (plus @0 { build_one_cst (TREE_TYPE (@1)); }))
9470 (bit_not @2))))))))
9471
9472 /* Canonicalizations of BIT_FIELD_REFs. */
9473
9474 (simplify
9475 (BIT_FIELD_REF (BIT_FIELD_REF @0 @1 @2) @3 @4)
9476 (BIT_FIELD_REF @0 @3 { const_binop (PLUS_EXPR, bitsizetype, @2, @4); }))
9477
9478 (simplify
9479 (BIT_FIELD_REF (view_convert @0) @1 @2)
9480 (if (! INTEGRAL_TYPE_P (TREE_TYPE (@0))
9481 || type_has_mode_precision_p (TREE_TYPE (@0)))
9482 (BIT_FIELD_REF @0 @1 @2)))
9483
9484 (simplify
9485 (BIT_FIELD_REF @0 @1 integer_zerop)
9486 (if (tree_int_cst_equal (@1, TYPE_SIZE (TREE_TYPE (@0))))
9487 (view_convert @0)))
9488
9489 (simplify
9490 (BIT_FIELD_REF @0 @1 @2)
9491 (switch
9492 (if (TREE_CODE (TREE_TYPE (@0)) == COMPLEX_TYPE
9493 && tree_int_cst_equal (@1, TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))))
9494 (switch
9495 (if (integer_zerop (@2))
9496 (view_convert (realpart @0)))
9497 (if (tree_int_cst_equal (@2, TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))))
9498 (view_convert (imagpart @0)))))
9499 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
9500 && INTEGRAL_TYPE_P (type)
9501 /* On GIMPLE this should only apply to register arguments. */
9502 && (! GIMPLE || is_gimple_reg (@0))
9503 /* A bit-field-ref that referenced the full argument can be stripped. */
9504 && ((compare_tree_int (@1, TYPE_PRECISION (TREE_TYPE (@0))) == 0
9505 && integer_zerop (@2))
9506 /* Low-parts can be reduced to integral conversions.
9507 ??? The following doesn't work for PDP endian. */
9508 || (BYTES_BIG_ENDIAN == WORDS_BIG_ENDIAN
9509 /* But only do this after vectorization. */
9510 && canonicalize_math_after_vectorization_p ()
9511 /* Don't even think about BITS_BIG_ENDIAN. */
9512 && TYPE_PRECISION (TREE_TYPE (@0)) % BITS_PER_UNIT == 0
9513 && TYPE_PRECISION (type) % BITS_PER_UNIT == 0
9514 && compare_tree_int (@2, (BYTES_BIG_ENDIAN
9515 ? (TYPE_PRECISION (TREE_TYPE (@0))
9516 - TYPE_PRECISION (type))
9517 : 0)) == 0)))
9518 (convert @0))))
9519
9520 /* Simplify vector extracts. */
9521
9522 (simplify
9523 (BIT_FIELD_REF CONSTRUCTOR@0 @1 @2)
9524 (if (VECTOR_TYPE_P (TREE_TYPE (@0))
9525 && tree_fits_uhwi_p (TYPE_SIZE (type))
9526 && ((tree_to_uhwi (TYPE_SIZE (type))
9527 == tree_to_uhwi (TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))))
9528 || (VECTOR_TYPE_P (type)
9529 && (tree_to_uhwi (TYPE_SIZE (TREE_TYPE (type)))
9530 == tree_to_uhwi (TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0))))))))
9531 (with
9532 {
9533 tree ctor = (TREE_CODE (@0) == SSA_NAME
9534 ? gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0)) : @0);
9535 tree eltype = TREE_TYPE (TREE_TYPE (ctor));
9536 unsigned HOST_WIDE_INT width = tree_to_uhwi (TYPE_SIZE (eltype));
9537 unsigned HOST_WIDE_INT n = tree_to_uhwi (@1);
9538 unsigned HOST_WIDE_INT idx = tree_to_uhwi (@2);
9539 }
9540 (if (n != 0
9541 && (idx % width) == 0
9542 && (n % width) == 0
9543 && known_le ((idx + n) / width,
9544 TYPE_VECTOR_SUBPARTS (TREE_TYPE (ctor))))
9545 (with
9546 {
9547 idx = idx / width;
9548 n = n / width;
9549 /* Constructor elements can be subvectors. */
9550 poly_uint64 k = 1;
9551 if (CONSTRUCTOR_NELTS (ctor) != 0)
9552 {
9553 tree cons_elem = TREE_TYPE (CONSTRUCTOR_ELT (ctor, 0)->value);
9554 if (TREE_CODE (cons_elem) == VECTOR_TYPE)
9555 k = TYPE_VECTOR_SUBPARTS (cons_elem);
9556 }
9557 unsigned HOST_WIDE_INT elt, count, const_k;
9558 }
9559 (switch
9560 /* We keep an exact subset of the constructor elements. */
9561 (if (multiple_p (idx, k, &elt) && multiple_p (n, k, &count))
9562 (if (CONSTRUCTOR_NELTS (ctor) == 0)
9563 { build_zero_cst (type); }
9564 (if (count == 1)
9565 (if (elt < CONSTRUCTOR_NELTS (ctor))
9566 (view_convert { CONSTRUCTOR_ELT (ctor, elt)->value; })
9567 { build_zero_cst (type); })
9568 /* We don't want to emit new CTORs unless the old one goes away.
9569 ??? Eventually allow this if the CTOR ends up constant or
9570 uniform. */
9571 (if (single_use (@0))
9572 (with
9573 {
9574 vec<constructor_elt, va_gc> *vals;
9575 vec_alloc (vals, count);
9576 bool constant_p = true;
9577 tree res;
9578 for (unsigned i = 0;
9579 i < count && elt + i < CONSTRUCTOR_NELTS (ctor); ++i)
9580 {
9581 tree e = CONSTRUCTOR_ELT (ctor, elt + i)->value;
9582 CONSTRUCTOR_APPEND_ELT (vals, NULL_TREE, e);
9583 if (!CONSTANT_CLASS_P (e))
9584 constant_p = false;
9585 }
9586 tree evtype = (types_match (TREE_TYPE (type),
9587 TREE_TYPE (TREE_TYPE (ctor)))
9588 ? type
9589 : build_vector_type (TREE_TYPE (TREE_TYPE (ctor)),
9590 count * k));
9591 /* We used to build a CTOR in the non-constant case here
9592 but that's not a GIMPLE value. We'd have to expose this
9593 operation somehow so the code generation can properly
9594 split it out to a separate stmt. */
9595 res = (constant_p ? build_vector_from_ctor (evtype, vals)
9596 : (GIMPLE ? NULL_TREE : build_constructor (evtype, vals)));
9597 }
9598 (if (res)
9599 (view_convert { res; })))))))
9600 /* The bitfield references a single constructor element. */
9601 (if (k.is_constant (&const_k)
9602 && idx + n <= (idx / const_k + 1) * const_k)
9603 (switch
9604 (if (CONSTRUCTOR_NELTS (ctor) <= idx / const_k)
9605 { build_zero_cst (type); })
9606 (if (n == const_k)
9607 (view_convert { CONSTRUCTOR_ELT (ctor, idx / const_k)->value; }))
9608 (BIT_FIELD_REF { CONSTRUCTOR_ELT (ctor, idx / const_k)->value; }
9609 @1 { bitsize_int ((idx % const_k) * width); })))))))))
9610
9611 (simplify
9612 (BIT_FIELD_REF (vec_perm@0 @1 @2 VECTOR_CST@3) @rsize @rpos)
9613 (with
9614 {
9615 tree elem_type = TREE_TYPE (TREE_TYPE (@0));
9616 poly_uint64 elem_size = tree_to_poly_uint64 (TYPE_SIZE (elem_type));
9617 poly_uint64 size = tree_to_poly_uint64 (TYPE_SIZE (type));
9618 unsigned HOST_WIDE_INT nelts, idx;
9619 unsigned HOST_WIDE_INT nelts_op = 0;
9620 }
9621 (if (constant_multiple_p (tree_to_poly_uint64 (@rpos), elem_size, &idx)
9622 && VECTOR_CST_NELTS (@3).is_constant (&nelts)
9623 && (known_eq (size, elem_size)
9624 || (constant_multiple_p (size, elem_size, &nelts_op)
9625 && pow2p_hwi (nelts_op))))
9626 (with
9627 {
9628 bool ok = true;
9629 /* One element. */
9630 if (known_eq (size, elem_size))
9631 idx = TREE_INT_CST_LOW (VECTOR_CST_ELT (@3, idx)) % (2 * nelts);
9632 else
9633 {
9634 /* Clamp vec_perm_expr index. */
9635 unsigned start
9636 = TREE_INT_CST_LOW (vector_cst_elt (@3, idx)) % (2 * nelts);
9637 unsigned end
9638 = (TREE_INT_CST_LOW (vector_cst_elt (@3, idx + nelts_op - 1))
9639 % (2 * nelts));
9640 /* Be in the same vector. */
9641 if ((start < nelts) != (end < nelts))
9642 ok = false;
9643 else
9644 for (unsigned HOST_WIDE_INT i = 1; i != nelts_op; i++)
9645 {
9646 /* Continuous area. */
9647 if ((TREE_INT_CST_LOW (vector_cst_elt (@3, idx + i))
9648 % (2 * nelts) - 1)
9649 != (TREE_INT_CST_LOW (vector_cst_elt (@3, idx + i - 1))
9650 % (2 * nelts)))
9651 {
9652 ok = false;
9653 break;
9654 }
9655 }
9656 /* Alignment not worse than before. */
9657 if (start % nelts_op)
9658 ok = false;
9659 idx = start;
9660 }
9661 }
9662 (if (ok)
9663 (if (idx < nelts)
9664 (BIT_FIELD_REF @1 @rsize { bitsize_int (idx * elem_size); })
9665 (BIT_FIELD_REF @2 @rsize { bitsize_int ((idx - nelts) * elem_size); })))))))
9666
9667 /* Simplify a bit extraction from a bit insertion for the cases with
9668 the inserted element fully covering the extraction or the insertion
9669 not touching the extraction. */
9670 (simplify
9671 (BIT_FIELD_REF (bit_insert @0 @1 @ipos) @rsize @rpos)
9672 (with
9673 {
9674 unsigned HOST_WIDE_INT isize;
9675 if (INTEGRAL_TYPE_P (TREE_TYPE (@1)))
9676 isize = TYPE_PRECISION (TREE_TYPE (@1));
9677 else
9678 isize = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (@1)));
9679 }
9680 (switch
9681 (if ((!INTEGRAL_TYPE_P (TREE_TYPE (@1))
9682 || type_has_mode_precision_p (TREE_TYPE (@1)))
9683 && wi::leu_p (wi::to_wide (@ipos), wi::to_wide (@rpos))
9684 && wi::leu_p (wi::to_wide (@rpos) + wi::to_wide (@rsize),
9685 wi::to_wide (@ipos) + isize))
9686 (BIT_FIELD_REF @1 @rsize { wide_int_to_tree (bitsizetype,
9687 wi::to_wide (@rpos)
9688 - wi::to_wide (@ipos)); }))
9689 (if (wi::eq_p (wi::to_wide (@ipos), wi::to_wide (@rpos))
9690 && compare_tree_int (@rsize, isize) == 0)
9691 (convert @1))
9692 (if (wi::geu_p (wi::to_wide (@ipos),
9693 wi::to_wide (@rpos) + wi::to_wide (@rsize))
9694 || wi::geu_p (wi::to_wide (@rpos),
9695 wi::to_wide (@ipos) + isize))
9696 (BIT_FIELD_REF @0 @rsize @rpos)))))
9697
9698 /* Simplify vector inserts of other vector extracts to a permute. */
9699 (simplify
9700 (bit_insert @0 (BIT_FIELD_REF@2 @1 @rsize @rpos) @ipos)
9701 (if (VECTOR_TYPE_P (type)
9702 && (VECTOR_MODE_P (TYPE_MODE (type))
9703 || optimize_vectors_before_lowering_p ())
9704 && operand_equal_p (TYPE_SIZE (TREE_TYPE (@0)),
9705 TYPE_SIZE (TREE_TYPE (@1)), 0)
9706 && types_match (TREE_TYPE (TREE_TYPE (@0)), TREE_TYPE (@2))
9707 && TYPE_VECTOR_SUBPARTS (type).is_constant ()
9708 && multiple_p (wi::to_poly_offset (@rpos),
9709 wi::to_poly_offset (TYPE_SIZE (TREE_TYPE (type)))))
9710 (with
9711 {
9712 unsigned HOST_WIDE_INT elsz
9713 = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0))));
9714 poly_uint64 relt = exact_div (tree_to_poly_uint64 (@rpos), elsz);
9715 poly_uint64 ielt = exact_div (tree_to_poly_uint64 (@ipos), elsz);
9716 unsigned nunits = TYPE_VECTOR_SUBPARTS (type).to_constant ();
9717 vec_perm_builder builder;
9718 builder.new_vector (nunits, nunits, 1);
9719 for (unsigned i = 0; i < nunits; ++i)
9720 builder.quick_push (known_eq (ielt, i) ? nunits + relt : i);
9721 vec_perm_indices sel (builder, 2, nunits);
9722 }
9723 (if (!VECTOR_MODE_P (TYPE_MODE (type))
9724 || can_vec_perm_const_p (TYPE_MODE (type),
9725 TYPE_MODE (type), sel, false))
9726 (vec_perm @0 (view_convert @1)
9727 { vec_perm_indices_to_tree (build_vector_type (ssizetype, nunits),
9728 sel); })))))
9729
9730 (if (canonicalize_math_after_vectorization_p ())
9731 (for fmas (FMA)
9732 (simplify
9733 (fmas:c (negate @0) @1 @2)
9734 (IFN_FNMA @0 @1 @2))
9735 (simplify
9736 (fmas @0 @1 (negate @2))
9737 (IFN_FMS @0 @1 @2))
9738 (simplify
9739 (fmas:c (negate @0) @1 (negate @2))
9740 (IFN_FNMS @0 @1 @2))
9741 (simplify
9742 (negate (fmas@3 @0 @1 @2))
9743 (if (!HONOR_SIGN_DEPENDENT_ROUNDING (type) && single_use (@3))
9744 (IFN_FNMS @0 @1 @2))))
9745
9746 (simplify
9747 (IFN_FMS:c (negate @0) @1 @2)
9748 (IFN_FNMS @0 @1 @2))
9749 (simplify
9750 (IFN_FMS @0 @1 (negate @2))
9751 (IFN_FMA @0 @1 @2))
9752 (simplify
9753 (IFN_FMS:c (negate @0) @1 (negate @2))
9754 (IFN_FNMA @0 @1 @2))
9755 (simplify
9756 (negate (IFN_FMS@3 @0 @1 @2))
9757 (if (!HONOR_SIGN_DEPENDENT_ROUNDING (type) && single_use (@3))
9758 (IFN_FNMA @0 @1 @2)))
9759
9760 (simplify
9761 (IFN_FNMA:c (negate @0) @1 @2)
9762 (IFN_FMA @0 @1 @2))
9763 (simplify
9764 (IFN_FNMA @0 @1 (negate @2))
9765 (IFN_FNMS @0 @1 @2))
9766 (simplify
9767 (IFN_FNMA:c (negate @0) @1 (negate @2))
9768 (IFN_FMS @0 @1 @2))
9769 (simplify
9770 (negate (IFN_FNMA@3 @0 @1 @2))
9771 (if (!HONOR_SIGN_DEPENDENT_ROUNDING (type) && single_use (@3))
9772 (IFN_FMS @0 @1 @2)))
9773
9774 (simplify
9775 (IFN_FNMS:c (negate @0) @1 @2)
9776 (IFN_FMS @0 @1 @2))
9777 (simplify
9778 (IFN_FNMS @0 @1 (negate @2))
9779 (IFN_FNMA @0 @1 @2))
9780 (simplify
9781 (IFN_FNMS:c (negate @0) @1 (negate @2))
9782 (IFN_FMA @0 @1 @2))
9783 (simplify
9784 (negate (IFN_FNMS@3 @0 @1 @2))
9785 (if (!HONOR_SIGN_DEPENDENT_ROUNDING (type) && single_use (@3))
9786 (IFN_FMA @0 @1 @2))))
9787
9788 /* CLZ simplifications. */
9789 (for clz (CLZ)
9790 (for op (eq ne)
9791 cmp (lt ge)
9792 (simplify
9793 (op (clz:s@2 @0) INTEGER_CST@1)
9794 (if (!sanitize_flags_p (SANITIZE_BUILTIN)
9795 /* For -fsanitize=builtin give ubsan pass a chance
9796 to instrument it first. */
9797 || (cfun && (cfun->curr_properties & PROP_ssa) != 0))
9798 (if (integer_zerop (@1) && single_use (@2))
9799 /* clz(X) == 0 is (int)X < 0 and clz(X) != 0 is (int)X >= 0. */
9800 (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
9801 (cmp (convert:stype @0) { build_zero_cst (stype); }))
9802 /* clz(X) == (prec-1) is X == 1 and clz(X) != (prec-1) is X != 1. */
9803 (if (wi::to_wide (@1) == TYPE_PRECISION (TREE_TYPE (@0)) - 1)
9804 (op @0 { build_one_cst (TREE_TYPE (@0)); })))))))
9805 (for op (eq ne)
9806 cmp (lt ge)
9807 (simplify
9808 (op (IFN_CLZ:s@2 @0 @3) INTEGER_CST@1)
9809 (if (integer_zerop (@1) && single_use (@2))
9810 /* clz(X) == 0 is (int)X < 0 and clz(X) != 0 is (int)X >= 0. */
9811 (with { tree type0 = TREE_TYPE (@0);
9812 tree stype = signed_type_for (TREE_TYPE (@0));
9813 /* Punt if clz(0) == 0. */
9814 if (integer_zerop (@3))
9815 stype = NULL_TREE;
9816 }
9817 (if (stype)
9818 (cmp (convert:stype @0) { build_zero_cst (stype); })))
9819 /* clz(X) == (prec-1) is X == 1 and clz(X) != (prec-1) is X != 1. */
9820 (with { bool ok = true;
9821 tree type0 = TREE_TYPE (@0);
9822 /* Punt if clz(0) == prec - 1. */
9823 if (wi::to_widest (@3) == TYPE_PRECISION (type0) - 1)
9824 ok = false;
9825 }
9826 (if (ok && wi::to_wide (@1) == (TYPE_PRECISION (type0) - 1))
9827 (op @0 { build_one_cst (type0); }))))))
9828
9829 /* CTZ simplifications. */
9830 (for ctz (CTZ)
9831 /* ctz (-X) => ctz (X). ctz (abs (X)) => ctz (X). */
9832 (for op (negate abs)
9833 (simplify
9834 (ctz (nop_convert?@0 (op @1)))
9835 (with { tree t = TREE_TYPE (@0); }
9836 (ctz (convert:t @1)))))
9837 (for op (ge gt le lt)
9838 cmp (eq eq ne ne)
9839 (simplify
9840 /* __builtin_ctz (x) >= C -> (x & ((1 << C) - 1)) == 0. */
9841 (op (ctz:s @0) INTEGER_CST@1)
9842 (with { bool ok = true;
9843 HOST_WIDE_INT val = 0;
9844 if (sanitize_flags_p (SANITIZE_BUILTIN)
9845 /* For -fsanitize=builtin give ubsan pass a chance
9846 to instrument it first. */
9847 && (!cfun
9848 || (cfun->curr_properties & PROP_ssa) == 0))
9849 ok = false;
9850 else if (!tree_fits_shwi_p (@1))
9851 ok = false;
9852 else
9853 {
9854 val = tree_to_shwi (@1);
9855 /* Canonicalize to >= or <. */
9856 if (op == GT_EXPR || op == LE_EXPR)
9857 {
9858 if (val == HOST_WIDE_INT_MAX)
9859 ok = false;
9860 else
9861 val++;
9862 }
9863 }
9864 tree type0 = TREE_TYPE (@0);
9865 int prec = TYPE_PRECISION (type0);
9866 }
9867 (if (ok && prec <= MAX_FIXED_MODE_SIZE)
9868 (if (val <= 0)
9869 { constant_boolean_node (cmp == EQ_EXPR ? true : false, type); }
9870 (if (val >= prec)
9871 { constant_boolean_node (cmp == EQ_EXPR ? false : true, type); }
9872 (cmp (bit_and @0 { wide_int_to_tree (type0,
9873 wi::mask (val, false, prec)); })
9874 { build_zero_cst (type0); })))))))
9875 (for op (eq ne)
9876 (simplify
9877 /* __builtin_ctz (x) == C -> (x & ((1 << (C + 1)) - 1)) == (1 << C). */
9878 (op (ctz:s @0) INTEGER_CST@1)
9879 (with { tree type0 = TREE_TYPE (@0);
9880 int prec = TYPE_PRECISION (type0);
9881 bool ok = true;
9882 if (sanitize_flags_p (SANITIZE_BUILTIN)
9883 /* For -fsanitize=builtin give ubsan pass a chance
9884 to instrument it first. */
9885 && (!cfun
9886 || (cfun->curr_properties & PROP_ssa) == 0))
9887 ok = false;
9888 }
9889 (if (ok && prec <= MAX_FIXED_MODE_SIZE)
9890 (if (tree_int_cst_sgn (@1) < 0 || wi::to_widest (@1) >= prec)
9891 { constant_boolean_node (op == EQ_EXPR ? false : true, type); }
9892 (op (bit_and @0 { wide_int_to_tree (type0,
9893 wi::mask (tree_to_uhwi (@1) + 1,
9894 false, prec)); })
9895 { wide_int_to_tree (type0,
9896 wi::shifted_mask (tree_to_uhwi (@1), 1,
9897 false, prec)); })))))))
9898 (for op (ge gt le lt)
9899 cmp (eq eq ne ne)
9900 (simplify
9901 /* __builtin_ctz (x) >= C -> (x & ((1 << C) - 1)) == 0. */
9902 (op (IFN_CTZ:s @0 @2) INTEGER_CST@1)
9903 (with { bool ok = true;
9904 HOST_WIDE_INT val = 0;
9905 if (!tree_fits_shwi_p (@1))
9906 ok = false;
9907 else
9908 {
9909 val = tree_to_shwi (@1);
9910 /* Canonicalize to >= or <. */
9911 if (op == GT_EXPR || op == LE_EXPR)
9912 {
9913 if (val == HOST_WIDE_INT_MAX)
9914 ok = false;
9915 else
9916 val++;
9917 }
9918 }
9919 HOST_WIDE_INT zero_val = tree_to_shwi (@2);
9920 tree type0 = TREE_TYPE (@0);
9921 int prec = TYPE_PRECISION (type0);
9922 if (prec > MAX_FIXED_MODE_SIZE)
9923 ok = false;
9924 }
9925 (if (val <= 0)
9926 (if (ok && zero_val >= val)
9927 { constant_boolean_node (cmp == EQ_EXPR ? true : false, type); })
9928 (if (val >= prec)
9929 (if (ok && zero_val < val)
9930 { constant_boolean_node (cmp == EQ_EXPR ? false : true, type); })
9931 (if (ok && (zero_val < 0 || zero_val >= prec))
9932 (cmp (bit_and @0 { wide_int_to_tree (type0,
9933 wi::mask (val, false, prec)); })
9934 { build_zero_cst (type0); })))))))
9935 (for op (eq ne)
9936 (simplify
9937 /* __builtin_ctz (x) == C -> (x & ((1 << (C + 1)) - 1)) == (1 << C). */
9938 (op (IFN_CTZ:s @0 @2) INTEGER_CST@1)
9939 (with { HOST_WIDE_INT zero_val = tree_to_shwi (@2);
9940 tree type0 = TREE_TYPE (@0);
9941 int prec = TYPE_PRECISION (type0);
9942 }
9943 (if (prec <= MAX_FIXED_MODE_SIZE)
9944 (if (tree_int_cst_sgn (@1) < 0 || wi::to_widest (@1) >= prec)
9945 (if (zero_val != wi::to_widest (@1))
9946 { constant_boolean_node (op == EQ_EXPR ? false : true, type); })
9947 (if (zero_val < 0 || zero_val >= prec)
9948 (op (bit_and @0 { wide_int_to_tree (type0,
9949 wi::mask (tree_to_uhwi (@1) + 1,
9950 false, prec)); })
9951 { wide_int_to_tree (type0,
9952 wi::shifted_mask (tree_to_uhwi (@1), 1,
9953 false, prec)); })))))))
9954
9955 #if GIMPLE
9956 /* ctz(ext(X)) == ctz(X). Valid just for the UB at zero cases though. */
9957 (simplify
9958 (CTZ (convert@1 @0))
9959 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
9960 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
9961 && TYPE_PRECISION (TREE_TYPE (@1)) > TYPE_PRECISION (TREE_TYPE (@0)))
9962 (with { combined_fn cfn = CFN_LAST;
9963 tree type0 = TREE_TYPE (@0);
9964 if (TREE_CODE (type0) == BITINT_TYPE)
9965 {
9966 if (TYPE_PRECISION (type0) > MAX_FIXED_MODE_SIZE)
9967 cfn = CFN_CTZ;
9968 else
9969 type0
9970 = build_nonstandard_integer_type (TYPE_PRECISION (type0),
9971 1);
9972 }
9973 type0 = unsigned_type_for (type0);
9974 if (cfn == CFN_LAST
9975 && direct_internal_fn_supported_p (IFN_CTZ, type0,
9976 OPTIMIZE_FOR_BOTH))
9977 cfn = CFN_CTZ;
9978 if (cfn == CFN_LAST
9979 && TYPE_PRECISION (TREE_TYPE (@1)) > BITS_PER_WORD
9980 && !direct_internal_fn_supported_p (IFN_CTZ,
9981 TREE_TYPE (@1),
9982 OPTIMIZE_FOR_BOTH))
9983 {
9984 if (TYPE_PRECISION (type0)
9985 == TYPE_PRECISION (unsigned_type_node))
9986 cfn = CFN_BUILT_IN_CTZ;
9987 else if (TYPE_PRECISION (type0)
9988 == TYPE_PRECISION (long_long_unsigned_type_node))
9989 cfn = CFN_BUILT_IN_CTZLL;
9990 }
9991 if (sanitize_flags_p (SANITIZE_BUILTIN)
9992 /* For -fsanitize=builtin give ubsan pass a chance
9993 to instrument it first. */
9994 && (!cfun
9995 || (cfun->curr_properties & PROP_ssa) == 0))
9996 cfn = CFN_LAST; }
9997 (if (cfn == CFN_CTZ)
9998 (IFN_CTZ (convert:type0 @0))
9999 (if (cfn == CFN_BUILT_IN_CTZ)
10000 (BUILT_IN_CTZ (convert:type0 @0))
10001 (if (cfn == CFN_BUILT_IN_CTZLL)
10002 (BUILT_IN_CTZLL (convert:type0 @0))))))))
10003 #endif
10004
10005 /* POPCOUNT simplifications. */
10006 /* popcount(X) + popcount(Y) is popcount(X|Y) when X&Y must be zero. */
10007 (simplify
10008 (plus (POPCOUNT:s @0) (POPCOUNT:s @1))
10009 (if (INTEGRAL_TYPE_P (type)
10010 && (wi::bit_and (widest_int::from (tree_nonzero_bits (@0), UNSIGNED),
10011 widest_int::from (tree_nonzero_bits (@1), UNSIGNED))
10012 == 0))
10013 (with { tree utype = TREE_TYPE (@0);
10014 if (TYPE_PRECISION (utype) < TYPE_PRECISION (TREE_TYPE (@1)))
10015 utype = TREE_TYPE (@1); }
10016 (POPCOUNT (bit_ior (convert:utype @0) (convert:utype @1))))))
10017
10018 /* popcount(X) == 0 is X == 0, and related (in)equalities. */
10019 (for popcount (POPCOUNT)
10020 (for cmp (le eq ne gt)
10021 rep (eq eq ne ne)
10022 (simplify
10023 (cmp (popcount @0) integer_zerop)
10024 (rep @0 { build_zero_cst (TREE_TYPE (@0)); }))))
10025
10026 /* popcount(bswap(x)) is popcount(x). */
10027 (for popcount (POPCOUNT)
10028 (for bswap (BUILT_IN_BSWAP16 BUILT_IN_BSWAP32
10029 BUILT_IN_BSWAP64 BUILT_IN_BSWAP128)
10030 (simplify
10031 (popcount (convert?@0 (bswap:s@1 @2)))
10032 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
10033 && INTEGRAL_TYPE_P (TREE_TYPE (@1)))
10034 (with { tree type0 = TREE_TYPE (@0);
10035 tree type1 = TREE_TYPE (@1);
10036 unsigned int prec0 = TYPE_PRECISION (type0);
10037 unsigned int prec1 = TYPE_PRECISION (type1); }
10038 (if (prec0 == prec1 || (prec0 > prec1 && TYPE_UNSIGNED (type1)))
10039 (popcount (convert:type0 (convert:type1 @2)))))))))
10040
10041 /* popcount(rotate(X Y)) is popcount(X). */
10042 (for popcount (POPCOUNT)
10043 (for rot (lrotate rrotate)
10044 (simplify
10045 (popcount (convert?@0 (rot:s@1 @2 @3)))
10046 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
10047 && INTEGRAL_TYPE_P (TREE_TYPE (@1))
10048 && (GIMPLE || !TREE_SIDE_EFFECTS (@3)))
10049 (with { tree type0 = TREE_TYPE (@0);
10050 tree type1 = TREE_TYPE (@1);
10051 unsigned int prec0 = TYPE_PRECISION (type0);
10052 unsigned int prec1 = TYPE_PRECISION (type1); }
10053 (if (prec0 == prec1 || (prec0 > prec1 && TYPE_UNSIGNED (type1)))
10054 (popcount (convert:type0 @2))))))))
10055
10056 /* Canonicalize POPCOUNT(x)&1 as PARITY(X). */
10057 (simplify
10058 (bit_and (POPCOUNT @0) integer_onep)
10059 (PARITY @0))
10060
10061 /* popcount(X&Y) + popcount(X|Y) is popcount(x) + popcount(Y). */
10062 (simplify
10063 (plus:c (POPCOUNT:s (bit_and:s @0 @1)) (POPCOUNT:s (bit_ior:cs @0 @1)))
10064 (plus (POPCOUNT:type @0) (POPCOUNT:type @1)))
10065
10066 /* popcount(X) + popcount(Y) - popcount(X&Y) is popcount(X|Y). */
10067 /* popcount(X) + popcount(Y) - popcount(X|Y) is popcount(X&Y). */
10068 (for popcount (POPCOUNT)
10069 (for log1 (bit_and bit_ior)
10070 log2 (bit_ior bit_and)
10071 (simplify
10072 (minus (plus:s (popcount:s @0) (popcount:s @1))
10073 (popcount:s (log1:cs @0 @1)))
10074 (popcount (log2 @0 @1)))
10075 (simplify
10076 (plus:c (minus:s (popcount:s @0) (popcount:s (log1:cs @0 @1)))
10077 (popcount:s @1))
10078 (popcount (log2 @0 @1)))))
10079
10080 #if GIMPLE
10081 /* popcount(zext(X)) == popcount(X). */
10082 (simplify
10083 (POPCOUNT (convert@1 @0))
10084 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
10085 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
10086 && TYPE_UNSIGNED (TREE_TYPE (@0))
10087 && TYPE_PRECISION (TREE_TYPE (@1)) > TYPE_PRECISION (TREE_TYPE (@0)))
10088 (with { combined_fn cfn = CFN_LAST;
10089 tree type0 = TREE_TYPE (@0);
10090 if (TREE_CODE (type0) == BITINT_TYPE)
10091 {
10092 if (TYPE_PRECISION (type0) > MAX_FIXED_MODE_SIZE)
10093 cfn = CFN_POPCOUNT;
10094 else
10095 type0
10096 = build_nonstandard_integer_type (TYPE_PRECISION (type0),
10097 1);
10098 }
10099 if (cfn == CFN_LAST
10100 && direct_internal_fn_supported_p (IFN_POPCOUNT, type0,
10101 OPTIMIZE_FOR_BOTH))
10102 cfn = CFN_POPCOUNT;
10103 if (cfn == CFN_LAST
10104 && TYPE_PRECISION (TREE_TYPE (@1)) > BITS_PER_WORD
10105 && !direct_internal_fn_supported_p (IFN_POPCOUNT,
10106 TREE_TYPE (@1),
10107 OPTIMIZE_FOR_BOTH))
10108 {
10109 if (TYPE_PRECISION (type0)
10110 == TYPE_PRECISION (unsigned_type_node))
10111 cfn = CFN_BUILT_IN_POPCOUNT;
10112 else if (TYPE_PRECISION (type0)
10113 == TYPE_PRECISION (long_long_unsigned_type_node))
10114 cfn = CFN_BUILT_IN_POPCOUNTLL;
10115 } }
10116 (if (cfn == CFN_POPCOUNT)
10117 (IFN_POPCOUNT (convert:type0 @0))
10118 (if (cfn == CFN_BUILT_IN_POPCOUNT)
10119 (BUILT_IN_POPCOUNT (convert:type0 @0))
10120 (if (cfn == CFN_BUILT_IN_POPCOUNTLL)
10121 (BUILT_IN_POPCOUNTLL (convert:type0 @0))))))))
10122 #endif
10123
10124 /* PARITY simplifications. */
10125 /* parity(~X) is parity(X). */
10126 (simplify
10127 (PARITY (bit_not @0))
10128 (PARITY @0))
10129
10130 /* parity(bswap(x)) is parity(x). */
10131 (for parity (PARITY)
10132 (for bswap (BUILT_IN_BSWAP16 BUILT_IN_BSWAP32
10133 BUILT_IN_BSWAP64 BUILT_IN_BSWAP128)
10134 (simplify
10135 (parity (convert?@0 (bswap:s@1 @2)))
10136 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
10137 && INTEGRAL_TYPE_P (TREE_TYPE (@1))
10138 && TYPE_PRECISION (TREE_TYPE (@0))
10139 >= TYPE_PRECISION (TREE_TYPE (@1)))
10140 (with { tree type0 = TREE_TYPE (@0);
10141 tree type1 = TREE_TYPE (@1); }
10142 (parity (convert:type0 (convert:type1 @2))))))))
10143
10144 /* parity(rotate(X Y)) is parity(X). */
10145 (for parity (PARITY)
10146 (for rot (lrotate rrotate)
10147 (simplify
10148 (parity (convert?@0 (rot:s@1 @2 @3)))
10149 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
10150 && INTEGRAL_TYPE_P (TREE_TYPE (@1))
10151 && (GIMPLE || !TREE_SIDE_EFFECTS (@3))
10152 && TYPE_PRECISION (TREE_TYPE (@0))
10153 >= TYPE_PRECISION (TREE_TYPE (@1)))
10154 (with { tree type0 = TREE_TYPE (@0); }
10155 (parity (convert:type0 @2)))))))
10156
10157 /* parity(X)^parity(Y) is parity(X^Y). */
10158 (simplify
10159 (bit_xor (PARITY:s @0) (PARITY:s @1))
10160 (if (types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
10161 (PARITY (bit_xor @0 @1))
10162 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
10163 && INTEGRAL_TYPE_P (TREE_TYPE (@1)))
10164 (with { tree utype = TREE_TYPE (@0);
10165 if (TYPE_PRECISION (utype) < TYPE_PRECISION (TREE_TYPE (@1)))
10166 utype = TREE_TYPE (@1); }
10167 (PARITY (bit_xor (convert:utype @0) (convert:utype @1)))))))
10168
10169 #if GIMPLE
10170 /* parity(zext(X)) == parity(X). */
10171 /* parity(sext(X)) == parity(X) if the difference in precision is even. */
10172 (simplify
10173 (PARITY (convert@1 @0))
10174 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
10175 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
10176 && TYPE_PRECISION (TREE_TYPE (@1)) > TYPE_PRECISION (TREE_TYPE (@0))
10177 && (TYPE_UNSIGNED (TREE_TYPE (@0))
10178 || ((TYPE_PRECISION (TREE_TYPE (@1))
10179 - TYPE_PRECISION (TREE_TYPE (@0))) & 1) == 0))
10180 (with { combined_fn cfn = CFN_LAST;
10181 tree type0 = TREE_TYPE (@0);
10182 if (TREE_CODE (type0) == BITINT_TYPE)
10183 {
10184 if (TYPE_PRECISION (type0) > MAX_FIXED_MODE_SIZE)
10185 cfn = CFN_PARITY;
10186 else
10187 type0
10188 = build_nonstandard_integer_type (TYPE_PRECISION (type0),
10189 1);
10190 }
10191 type0 = unsigned_type_for (type0);
10192 if (cfn == CFN_LAST
10193 && direct_internal_fn_supported_p (IFN_PARITY, type0,
10194 OPTIMIZE_FOR_BOTH))
10195 cfn = CFN_PARITY;
10196 if (cfn == CFN_LAST
10197 && TYPE_PRECISION (TREE_TYPE (@1)) > BITS_PER_WORD
10198 && !direct_internal_fn_supported_p (IFN_PARITY,
10199 TREE_TYPE (@1),
10200 OPTIMIZE_FOR_BOTH))
10201 {
10202 if (TYPE_PRECISION (type0)
10203 == TYPE_PRECISION (unsigned_type_node))
10204 cfn = CFN_BUILT_IN_PARITY;
10205 else if (TYPE_PRECISION (type0)
10206 == TYPE_PRECISION (long_long_unsigned_type_node))
10207 cfn = CFN_BUILT_IN_PARITYLL;
10208 } }
10209 (if (cfn == CFN_PARITY)
10210 (IFN_PARITY (convert:type0 @0))
10211 (if (cfn == CFN_BUILT_IN_PARITY)
10212 (BUILT_IN_PARITY (convert:type0 @0))
10213 (if (cfn == CFN_BUILT_IN_PARITYLL)
10214 (BUILT_IN_PARITYLL (convert:type0 @0))))))))
10215 #endif
10216
10217 /* a != 0 ? FUN(a) : 0 -> Fun(a) for some builtin functions. */
10218 (for func (POPCOUNT BSWAP FFS PARITY)
10219 (simplify
10220 (cond (ne @0 integer_zerop@1) (func@3 (convert? @0)) integer_zerop@2)
10221 @3))
10222
10223 /* a != 0 ? FUN(a) : CST -> Fun(a) for some CLRSB builtins
10224 where CST is precision-1. */
10225 (for func (CLRSB)
10226 (simplify
10227 (cond (ne @0 integer_zerop@1) (func@4 (convert?@3 @0)) INTEGER_CST@2)
10228 (if (wi::to_widest (@2) == TYPE_PRECISION (TREE_TYPE (@3)) - 1)
10229 @4)))
10230
10231 #if GIMPLE
10232 /* a != 0 ? CLZ(a) : CST -> .CLZ(a) where CST is the result of the internal function for 0. */
10233 (for func (CLZ)
10234 (simplify
10235 (cond (ne @0 integer_zerop@1) (func (convert?@3 @0)) INTEGER_CST@2)
10236 (with { int val;
10237 internal_fn ifn = IFN_LAST;
10238 if (TREE_CODE (TREE_TYPE (@3)) == BITINT_TYPE)
10239 {
10240 if (tree_fits_shwi_p (@2))
10241 {
10242 HOST_WIDE_INT valw = tree_to_shwi (@2);
10243 if ((int) valw == valw)
10244 {
10245 val = valw;
10246 ifn = IFN_CLZ;
10247 }
10248 }
10249 }
10250 else if (direct_internal_fn_supported_p (IFN_CLZ, TREE_TYPE (@3),
10251 OPTIMIZE_FOR_BOTH)
10252 && CLZ_DEFINED_VALUE_AT_ZERO
10253 (SCALAR_INT_TYPE_MODE (TREE_TYPE (@3)), val) == 2)
10254 ifn = IFN_CLZ;
10255 }
10256 (if (ifn == IFN_CLZ && wi::to_widest (@2) == val)
10257 (IFN_CLZ @3 @2)))))
10258 (simplify
10259 (cond (ne @0 integer_zerop@1) (IFN_CLZ (convert?@3 @0) INTEGER_CST@2) @2)
10260 (with { int val;
10261 internal_fn ifn = IFN_LAST;
10262 if (TREE_CODE (TREE_TYPE (@3)) == BITINT_TYPE)
10263 ifn = IFN_CLZ;
10264 else if (direct_internal_fn_supported_p (IFN_CLZ, TREE_TYPE (@3),
10265 OPTIMIZE_FOR_BOTH))
10266 ifn = IFN_CLZ;
10267 }
10268 (if (ifn == IFN_CLZ)
10269 (IFN_CLZ @3 @2))))
10270
10271 /* a != 0 ? CTZ(a) : CST -> .CTZ(a) where CST is the result of the internal function for 0. */
10272 (for func (CTZ)
10273 (simplify
10274 (cond (ne @0 integer_zerop@1) (func (convert?@3 @0)) INTEGER_CST@2)
10275 (with { int val;
10276 internal_fn ifn = IFN_LAST;
10277 if (TREE_CODE (TREE_TYPE (@3)) == BITINT_TYPE)
10278 {
10279 if (tree_fits_shwi_p (@2))
10280 {
10281 HOST_WIDE_INT valw = tree_to_shwi (@2);
10282 if ((int) valw == valw)
10283 {
10284 val = valw;
10285 ifn = IFN_CTZ;
10286 }
10287 }
10288 }
10289 else if (direct_internal_fn_supported_p (IFN_CTZ, TREE_TYPE (@3),
10290 OPTIMIZE_FOR_BOTH)
10291 && CTZ_DEFINED_VALUE_AT_ZERO
10292 (SCALAR_INT_TYPE_MODE (TREE_TYPE (@3)), val) == 2)
10293 ifn = IFN_CTZ;
10294 }
10295 (if (ifn == IFN_CTZ && wi::to_widest (@2) == val)
10296 (IFN_CTZ @3 @2)))))
10297 (simplify
10298 (cond (ne @0 integer_zerop@1) (IFN_CTZ (convert?@3 @0) INTEGER_CST@2) @2)
10299 (with { int val;
10300 internal_fn ifn = IFN_LAST;
10301 if (TREE_CODE (TREE_TYPE (@3)) == BITINT_TYPE)
10302 ifn = IFN_CTZ;
10303 else if (direct_internal_fn_supported_p (IFN_CTZ, TREE_TYPE (@3),
10304 OPTIMIZE_FOR_BOTH))
10305 ifn = IFN_CTZ;
10306 }
10307 (if (ifn == IFN_CTZ)
10308 (IFN_CTZ @3 @2))))
10309 #endif
10310
10311 /* Common POPCOUNT/PARITY simplifications. */
10312 /* popcount(X&C1) is (X>>C2)&1 when C1 == 1<<C2. Same for parity(X&C1). */
10313 (for pfun (POPCOUNT PARITY)
10314 (simplify
10315 (pfun @0)
10316 (if (INTEGRAL_TYPE_P (type))
10317 (with { wide_int nz = tree_nonzero_bits (@0); }
10318 (switch
10319 (if (nz == 1)
10320 (convert @0))
10321 (if (wi::popcount (nz) == 1)
10322 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
10323 (convert (rshift:utype (convert:utype @0)
10324 { build_int_cst (integer_type_node,
10325 wi::ctz (nz)); })))))))))
10326
10327 #if GIMPLE
10328 /* 64- and 32-bits branchless implementations of popcount are detected:
10329
10330 int popcount64c (uint64_t x)
10331 {
10332 x -= (x >> 1) & 0x5555555555555555ULL;
10333 x = (x & 0x3333333333333333ULL) + ((x >> 2) & 0x3333333333333333ULL);
10334 x = (x + (x >> 4)) & 0x0f0f0f0f0f0f0f0fULL;
10335 return (x * 0x0101010101010101ULL) >> 56;
10336 }
10337
10338 int popcount32c (uint32_t x)
10339 {
10340 x -= (x >> 1) & 0x55555555;
10341 x = (x & 0x33333333) + ((x >> 2) & 0x33333333);
10342 x = (x + (x >> 4)) & 0x0f0f0f0f;
10343 return (x * 0x01010101) >> 24;
10344 } */
10345 (simplify
10346 (rshift
10347 (mult
10348 (bit_and
10349 (plus:c
10350 (rshift @8 INTEGER_CST@5)
10351 (plus:c@8
10352 (bit_and @6 INTEGER_CST@7)
10353 (bit_and
10354 (rshift
10355 (minus@6 @0
10356 (bit_and (rshift @0 INTEGER_CST@4) INTEGER_CST@11))
10357 INTEGER_CST@10)
10358 INTEGER_CST@9)))
10359 INTEGER_CST@3)
10360 INTEGER_CST@2)
10361 INTEGER_CST@1)
10362 /* Check constants and optab. */
10363 (with { unsigned prec = TYPE_PRECISION (type);
10364 int shift = (64 - prec) & 63;
10365 unsigned HOST_WIDE_INT c1
10366 = HOST_WIDE_INT_UC (0x0101010101010101) >> shift;
10367 unsigned HOST_WIDE_INT c2
10368 = HOST_WIDE_INT_UC (0x0F0F0F0F0F0F0F0F) >> shift;
10369 unsigned HOST_WIDE_INT c3
10370 = HOST_WIDE_INT_UC (0x3333333333333333) >> shift;
10371 unsigned HOST_WIDE_INT c4
10372 = HOST_WIDE_INT_UC (0x5555555555555555) >> shift;
10373 }
10374 (if (prec >= 16
10375 && prec <= 64
10376 && pow2p_hwi (prec)
10377 && TYPE_UNSIGNED (type)
10378 && integer_onep (@4)
10379 && wi::to_widest (@10) == 2
10380 && wi::to_widest (@5) == 4
10381 && wi::to_widest (@1) == prec - 8
10382 && tree_to_uhwi (@2) == c1
10383 && tree_to_uhwi (@3) == c2
10384 && tree_to_uhwi (@9) == c3
10385 && tree_to_uhwi (@7) == c3
10386 && tree_to_uhwi (@11) == c4)
10387 (if (direct_internal_fn_supported_p (IFN_POPCOUNT, type,
10388 OPTIMIZE_FOR_BOTH))
10389 (convert (IFN_POPCOUNT:type @0))
10390 /* Try to do popcount in two halves. PREC must be at least
10391 five bits for this to work without extension before adding. */
10392 (with {
10393 tree half_type = NULL_TREE;
10394 opt_machine_mode m = mode_for_size ((prec + 1) / 2, MODE_INT, 1);
10395 int half_prec = 8;
10396 if (m.exists ()
10397 && m.require () != TYPE_MODE (type))
10398 {
10399 half_prec = GET_MODE_PRECISION (as_a <scalar_int_mode> (m));
10400 half_type = build_nonstandard_integer_type (half_prec, 1);
10401 }
10402 gcc_assert (half_prec > 2);
10403 }
10404 (if (half_type != NULL_TREE
10405 && direct_internal_fn_supported_p (IFN_POPCOUNT, half_type,
10406 OPTIMIZE_FOR_BOTH))
10407 (convert (plus
10408 (IFN_POPCOUNT:half_type (convert @0))
10409 (IFN_POPCOUNT:half_type (convert (rshift @0
10410 { build_int_cst (integer_type_node, half_prec); } )))))))))))
10411
10412 /* __builtin_ffs needs to deal on many targets with the possible zero
10413 argument. If we know the argument is always non-zero, __builtin_ctz + 1
10414 should lead to better code. */
10415 (simplify
10416 (FFS tree_expr_nonzero_p@0)
10417 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
10418 && direct_internal_fn_supported_p (IFN_CTZ, TREE_TYPE (@0),
10419 OPTIMIZE_FOR_SPEED))
10420 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
10421 (plus (CTZ:type (convert:utype @0)) { build_one_cst (type); }))))
10422 #endif
10423
10424 (for ffs (FFS)
10425 /* __builtin_ffs (X) == 0 -> X == 0.
10426 __builtin_ffs (X) == 6 -> (X & 63) == 32. */
10427 (for cmp (eq ne)
10428 (simplify
10429 (cmp (ffs@2 @0) INTEGER_CST@1)
10430 (with { int prec = TYPE_PRECISION (TREE_TYPE (@0)); }
10431 (switch
10432 (if (integer_zerop (@1))
10433 (cmp @0 { build_zero_cst (TREE_TYPE (@0)); }))
10434 (if (tree_int_cst_sgn (@1) < 0 || wi::to_widest (@1) > prec)
10435 { constant_boolean_node (cmp == NE_EXPR ? true : false, type); })
10436 (if (single_use (@2))
10437 (cmp (bit_and @0 { wide_int_to_tree (TREE_TYPE (@0),
10438 wi::mask (tree_to_uhwi (@1),
10439 false, prec)); })
10440 { wide_int_to_tree (TREE_TYPE (@0),
10441 wi::shifted_mask (tree_to_uhwi (@1) - 1, 1,
10442 false, prec)); }))))))
10443
10444 /* __builtin_ffs (X) > 6 -> X != 0 && (X & 63) == 0. */
10445 (for cmp (gt le)
10446 cmp2 (ne eq)
10447 cmp3 (eq ne)
10448 bit_op (bit_and bit_ior)
10449 (simplify
10450 (cmp (ffs@2 @0) INTEGER_CST@1)
10451 (with { int prec = TYPE_PRECISION (TREE_TYPE (@0)); }
10452 (switch
10453 (if (integer_zerop (@1))
10454 (cmp2 @0 { build_zero_cst (TREE_TYPE (@0)); }))
10455 (if (tree_int_cst_sgn (@1) < 0)
10456 { constant_boolean_node (cmp == GT_EXPR ? true : false, type); })
10457 (if (wi::to_widest (@1) >= prec)
10458 { constant_boolean_node (cmp == GT_EXPR ? false : true, type); })
10459 (if (wi::to_widest (@1) == prec - 1)
10460 (cmp3 @0 { wide_int_to_tree (TREE_TYPE (@0),
10461 wi::shifted_mask (prec - 1, 1,
10462 false, prec)); }))
10463 (if (single_use (@2))
10464 (bit_op (cmp2 @0 { build_zero_cst (TREE_TYPE (@0)); })
10465 (cmp3 (bit_and @0
10466 { wide_int_to_tree (TREE_TYPE (@0),
10467 wi::mask (tree_to_uhwi (@1),
10468 false, prec)); })
10469 { build_zero_cst (TREE_TYPE (@0)); }))))))))
10470
10471 #if GIMPLE
10472 /* ffs(ext(X)) == ffs(X). */
10473 (simplify
10474 (FFS (convert@1 @0))
10475 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
10476 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
10477 && TYPE_PRECISION (TREE_TYPE (@1)) > TYPE_PRECISION (TREE_TYPE (@0)))
10478 (with { combined_fn cfn = CFN_LAST;
10479 tree type0 = TREE_TYPE (@0);
10480 if (TREE_CODE (type0) == BITINT_TYPE)
10481 {
10482 if (TYPE_PRECISION (type0) > MAX_FIXED_MODE_SIZE)
10483 cfn = CFN_FFS;
10484 else
10485 type0
10486 = build_nonstandard_integer_type (TYPE_PRECISION (type0),
10487 0);
10488 }
10489 type0 = signed_type_for (type0);
10490 if (cfn == CFN_LAST
10491 && direct_internal_fn_supported_p (IFN_FFS, type0,
10492 OPTIMIZE_FOR_BOTH))
10493 cfn = CFN_FFS;
10494 if (cfn == CFN_LAST
10495 && TYPE_PRECISION (TREE_TYPE (@1)) > BITS_PER_WORD
10496 && !direct_internal_fn_supported_p (IFN_FFS,
10497 TREE_TYPE (@1),
10498 OPTIMIZE_FOR_BOTH))
10499 {
10500 if (TYPE_PRECISION (type0)
10501 == TYPE_PRECISION (integer_type_node))
10502 cfn = CFN_BUILT_IN_FFS;
10503 else if (TYPE_PRECISION (type0)
10504 == TYPE_PRECISION (long_long_integer_type_node))
10505 cfn = CFN_BUILT_IN_FFSLL;
10506 } }
10507 (if (cfn == CFN_FFS)
10508 (IFN_FFS (convert:type0 @0))
10509 (if (cfn == CFN_BUILT_IN_FFS)
10510 (BUILT_IN_FFS (convert:type0 @0))
10511 (if (cfn == CFN_BUILT_IN_FFSLL)
10512 (BUILT_IN_FFSLL (convert:type0 @0))))))))
10513 #endif
10514
10515 #if GIMPLE
10516
10517 /* Simplify:
10518 a = op a1
10519 r = cond ? a : b
10520 --> r = .COND_FN (cond, a, b)
10521 and,
10522 a = op a1
10523 r = cond ? b : a
10524 --> r = .COND_FN (~cond, b, a). */
10525
10526 (for uncond_op (UNCOND_UNARY)
10527 cond_op (COND_UNARY)
10528 (simplify
10529 (vec_cond @0 (view_convert? (uncond_op@3 @1)) @2)
10530 (with { tree op_type = TREE_TYPE (@3); }
10531 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
10532 && is_truth_type_for (op_type, TREE_TYPE (@0)))
10533 (cond_op @0 (view_convert @1) @2))))
10534 (simplify
10535 (vec_cond @0 @1 (view_convert? (uncond_op@3 @2)))
10536 (with { tree op_type = TREE_TYPE (@3); }
10537 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
10538 && is_truth_type_for (op_type, TREE_TYPE (@0)))
10539 (cond_op (bit_not @0) (view_convert @2) @1)))))
10540
10541 (for uncond_op (UNCOND_UNARY)
10542 cond_op (COND_LEN_UNARY)
10543 (simplify
10544 (IFN_VCOND_MASK_LEN @0 (view_convert? (uncond_op@3 @1)) @2 @4 @5)
10545 (with { tree op_type = TREE_TYPE (@3); }
10546 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
10547 && is_truth_type_for (op_type, TREE_TYPE (@0)))
10548 (cond_op @0 (view_convert @1) @2 @4 @5))))
10549 (simplify
10550 (IFN_VCOND_MASK_LEN @0 @1 (view_convert? (uncond_op@3 @2)) @4 @5)
10551 (with { tree op_type = TREE_TYPE (@3); }
10552 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
10553 && is_truth_type_for (op_type, TREE_TYPE (@0)))
10554 (cond_op (bit_not @0) (view_convert @2) @1 @4 @5)))))
10555
10556 /* `(a ? -1 : 0) ^ b` can be converted into a conditional not. */
10557 (simplify
10558 (bit_xor:c (vec_cond @0 uniform_integer_cst_p@1 uniform_integer_cst_p@2) @3)
10559 (if (canonicalize_math_after_vectorization_p ()
10560 && vectorized_internal_fn_supported_p (IFN_COND_NOT, type)
10561 && is_truth_type_for (type, TREE_TYPE (@0)))
10562 (if (integer_all_onesp (@1) && integer_zerop (@2))
10563 (IFN_COND_NOT @0 @3 @3))
10564 (if (integer_all_onesp (@2) && integer_zerop (@1))
10565 (IFN_COND_NOT (bit_not @0) @3 @3))))
10566
10567 /* Simplify:
10568
10569 a = a1 op a2
10570 r = c ? a : b;
10571
10572 to:
10573
10574 r = c ? a1 op a2 : b;
10575
10576 if the target can do it in one go. This makes the operation conditional
10577 on c, so could drop potentially-trapping arithmetic, but that's a valid
10578 simplification if the result of the operation isn't needed.
10579
10580 Avoid speculatively generating a stand-alone vector comparison
10581 on targets that might not support them. Any target implementing
10582 conditional internal functions must support the same comparisons
10583 inside and outside a VEC_COND_EXPR. */
10584
10585 (for uncond_op (UNCOND_BINARY)
10586 cond_op (COND_BINARY)
10587 (simplify
10588 (vec_cond @0 (view_convert? (uncond_op@4 @1 @2)) @3)
10589 (with { tree op_type = TREE_TYPE (@4); }
10590 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
10591 && is_truth_type_for (op_type, TREE_TYPE (@0))
10592 && single_use (@4))
10593 (view_convert (cond_op @0 @1 @2 (view_convert:op_type @3))))))
10594 (simplify
10595 (vec_cond @0 @1 (view_convert? (uncond_op@4 @2 @3)))
10596 (with { tree op_type = TREE_TYPE (@4); }
10597 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
10598 && is_truth_type_for (op_type, TREE_TYPE (@0))
10599 && single_use (@4))
10600 (view_convert (cond_op (bit_not @0) @2 @3 (view_convert:op_type @1)))))))
10601
10602 (for uncond_op (UNCOND_BINARY)
10603 cond_op (COND_LEN_BINARY)
10604 (simplify
10605 (IFN_VCOND_MASK_LEN @0 (view_convert? (uncond_op@4 @1 @2)) @3 @5 @6)
10606 (with { tree op_type = TREE_TYPE (@4); }
10607 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
10608 && is_truth_type_for (op_type, TREE_TYPE (@0))
10609 && single_use (@4))
10610 (view_convert (cond_op @0 @1 @2 (view_convert:op_type @3) @5 @6)))))
10611 (simplify
10612 (IFN_VCOND_MASK_LEN @0 @1 (view_convert? (uncond_op@4 @2 @3)) @5 @6)
10613 (with { tree op_type = TREE_TYPE (@4); }
10614 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
10615 && is_truth_type_for (op_type, TREE_TYPE (@0))
10616 && single_use (@4))
10617 (view_convert (cond_op (bit_not @0) @2 @3 (view_convert:op_type @1) @5 @6))))))
10618
10619 /* Same for ternary operations. */
10620 (for uncond_op (UNCOND_TERNARY)
10621 cond_op (COND_TERNARY)
10622 (simplify
10623 (vec_cond @0 (view_convert? (uncond_op@5 @1 @2 @3)) @4)
10624 (with { tree op_type = TREE_TYPE (@5); }
10625 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
10626 && is_truth_type_for (op_type, TREE_TYPE (@0))
10627 && single_use (@5))
10628 (view_convert (cond_op @0 @1 @2 @3 (view_convert:op_type @4))))))
10629 (simplify
10630 (vec_cond @0 @1 (view_convert? (uncond_op@5 @2 @3 @4)))
10631 (with { tree op_type = TREE_TYPE (@5); }
10632 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
10633 && is_truth_type_for (op_type, TREE_TYPE (@0))
10634 && single_use (@5))
10635 (view_convert (cond_op (bit_not @0) @2 @3 @4
10636 (view_convert:op_type @1)))))))
10637
10638 (for uncond_op (UNCOND_TERNARY)
10639 cond_op (COND_LEN_TERNARY)
10640 (simplify
10641 (IFN_VCOND_MASK_LEN @0 (view_convert? (uncond_op@5 @1 @2 @3)) @4 @6 @7)
10642 (with { tree op_type = TREE_TYPE (@5); }
10643 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
10644 && is_truth_type_for (op_type, TREE_TYPE (@0))
10645 && single_use (@5))
10646 (view_convert (cond_op @0 @1 @2 @3 (view_convert:op_type @4) @6 @7)))))
10647 (simplify
10648 (IFN_VCOND_MASK_LEN @0 @1 (view_convert? (uncond_op@5 @2 @3 @4 @6 @7)))
10649 (with { tree op_type = TREE_TYPE (@5); }
10650 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
10651 && is_truth_type_for (op_type, TREE_TYPE (@0))
10652 && single_use (@5))
10653 (view_convert (cond_op (bit_not @0) @2 @3 @4 (view_convert:op_type @1) @6 @7))))))
10654 #endif
10655
10656 /* Detect cases in which a VEC_COND_EXPR effectively replaces the
10657 "else" value of an IFN_COND_*. */
10658 (for cond_op (COND_BINARY)
10659 (simplify
10660 (vec_cond @0 (view_convert? (cond_op @0 @1 @2 @3)) @4)
10661 (with { tree op_type = TREE_TYPE (@3); }
10662 (if (element_precision (type) == element_precision (op_type))
10663 (view_convert (cond_op @0 @1 @2 (view_convert:op_type @4))))))
10664 (simplify
10665 (vec_cond @0 @1 (view_convert? (cond_op @2 @3 @4 @5)))
10666 (with { tree op_type = TREE_TYPE (@5); }
10667 (if (inverse_conditions_p (@0, @2)
10668 && element_precision (type) == element_precision (op_type))
10669 (view_convert (cond_op @2 @3 @4 (view_convert:op_type @1)))))))
10670
10671 /* Same for ternary operations. */
10672 (for cond_op (COND_TERNARY)
10673 (simplify
10674 (vec_cond @0 (view_convert? (cond_op @0 @1 @2 @3 @4)) @5)
10675 (with { tree op_type = TREE_TYPE (@4); }
10676 (if (element_precision (type) == element_precision (op_type))
10677 (view_convert (cond_op @0 @1 @2 @3 (view_convert:op_type @5))))))
10678 (simplify
10679 (vec_cond @0 @1 (view_convert? (cond_op @2 @3 @4 @5 @6)))
10680 (with { tree op_type = TREE_TYPE (@6); }
10681 (if (inverse_conditions_p (@0, @2)
10682 && element_precision (type) == element_precision (op_type))
10683 (view_convert (cond_op @2 @3 @4 @5 (view_convert:op_type @1)))))))
10684
10685 /* Detect cases in which a VEC_COND_EXPR effectively replaces the
10686 "else" value of an IFN_COND_LEN_*. */
10687 (for cond_len_op (COND_LEN_BINARY)
10688 (simplify
10689 (vec_cond @0 (view_convert? (cond_len_op @0 @1 @2 @3 @4 @5)) @6)
10690 (with { tree op_type = TREE_TYPE (@3); }
10691 (if (element_precision (type) == element_precision (op_type))
10692 (view_convert (cond_len_op @0 @1 @2 (view_convert:op_type @6) @4 @5)))))
10693 (simplify
10694 (vec_cond @0 @1 (view_convert? (cond_len_op @2 @3 @4 @5 @6 @7)))
10695 (with { tree op_type = TREE_TYPE (@5); }
10696 (if (inverse_conditions_p (@0, @2)
10697 && element_precision (type) == element_precision (op_type))
10698 (view_convert (cond_len_op @2 @3 @4 (view_convert:op_type @1) @6 @7))))))
10699
10700 /* Same for ternary operations. */
10701 (for cond_len_op (COND_LEN_TERNARY)
10702 (simplify
10703 (vec_cond @0 (view_convert? (cond_len_op @0 @1 @2 @3 @4 @5 @6)) @7)
10704 (with { tree op_type = TREE_TYPE (@4); }
10705 (if (element_precision (type) == element_precision (op_type))
10706 (view_convert (cond_len_op @0 @1 @2 @3 (view_convert:op_type @7) @5 @6)))))
10707 (simplify
10708 (vec_cond @0 @1 (view_convert? (cond_len_op @2 @3 @4 @5 @6 @7 @8)))
10709 (with { tree op_type = TREE_TYPE (@6); }
10710 (if (inverse_conditions_p (@0, @2)
10711 && element_precision (type) == element_precision (op_type))
10712 (view_convert (cond_len_op @2 @3 @4 @5 (view_convert:op_type @1) @7 @8))))))
10713
10714 /* Detect simplication for a conditional reduction where
10715
10716 a = mask1 ? b : 0
10717 c = mask2 ? d + a : d
10718
10719 is turned into
10720
10721 c = mask1 && mask2 ? d + b : d. */
10722 (simplify
10723 (IFN_COND_ADD @0 @1 (vec_cond @2 @3 zerop@4) @1)
10724 (if (ANY_INTEGRAL_TYPE_P (type)
10725 || (FLOAT_TYPE_P (type)
10726 && fold_real_zero_addition_p (type, NULL_TREE, @4, 0)))
10727 (IFN_COND_ADD (bit_and @0 @2) @1 @3 @1)))
10728
10729 /* Detect simplication for a conditional length reduction where
10730
10731 a = mask ? b : 0
10732 c = i < len + bias ? d + a : d
10733
10734 is turned into
10735
10736 c = mask && i < len + bias ? d + b : d. */
10737 (simplify
10738 (IFN_COND_LEN_ADD integer_truep @0 (vec_cond @1 @2 zerop@5) @0 @3 @4)
10739 (if (ANY_INTEGRAL_TYPE_P (type)
10740 || (FLOAT_TYPE_P (type)
10741 && fold_real_zero_addition_p (type, NULL_TREE, @5, 0)))
10742 (IFN_COND_LEN_ADD @1 @0 @2 @0 @3 @4)))
10743
10744 /* Detect simplification for vector condition folding where
10745
10746 c = mask1 ? (masked_op mask2 a b els) : els
10747
10748 into
10749
10750 c = masked_op (mask1 & mask2) a b els
10751
10752 where the operation can be partially applied to one operand. */
10753
10754 (for cond_op (COND_BINARY)
10755 (simplify
10756 (vec_cond @0
10757 (cond_op:s @1 @2 @3 @4) @4)
10758 (cond_op (bit_and @1 @0) @2 @3 @4)))
10759
10760 /* And same for ternary expressions. */
10761
10762 (for cond_op (COND_TERNARY)
10763 (simplify
10764 (vec_cond @0
10765 (cond_op:s @1 @2 @3 @4 @5) @5)
10766 (cond_op (bit_and @1 @0) @2 @3 @4 @5)))
10767
10768 /* For pointers @0 and @2 and nonnegative constant offset @1, look for
10769 expressions like:
10770
10771 A: (@0 + @1 < @2) | (@2 + @1 < @0)
10772 B: (@0 + @1 <= @2) | (@2 + @1 <= @0)
10773
10774 If pointers are known not to wrap, B checks whether @1 bytes starting
10775 at @0 and @2 do not overlap, while A tests the same thing for @1 + 1
10776 bytes. A is more efficiently tested as:
10777
10778 A: (sizetype) (@0 + @1 - @2) > @1 * 2
10779
10780 The equivalent expression for B is given by replacing @1 with @1 - 1:
10781
10782 B: (sizetype) (@0 + (@1 - 1) - @2) > (@1 - 1) * 2
10783
10784 @0 and @2 can be swapped in both expressions without changing the result.
10785
10786 The folds rely on sizetype's being unsigned (which is always true)
10787 and on its being the same width as the pointer (which we have to check).
10788
10789 The fold replaces two pointer_plus expressions, two comparisons and
10790 an IOR with a pointer_plus, a pointer_diff, and a comparison, so in
10791 the best case it's a saving of two operations. The A fold retains one
10792 of the original pointer_pluses, so is a win even if both pointer_pluses
10793 are used elsewhere. The B fold is a wash if both pointer_pluses are
10794 used elsewhere, since all we end up doing is replacing a comparison with
10795 a pointer_plus. We do still apply the fold under those circumstances
10796 though, in case applying it to other conditions eventually makes one of the
10797 pointer_pluses dead. */
10798 (for ior (truth_orif truth_or bit_ior)
10799 (for cmp (le lt)
10800 (simplify
10801 (ior (cmp:cs (pointer_plus@3 @0 INTEGER_CST@1) @2)
10802 (cmp:cs (pointer_plus@4 @2 @1) @0))
10803 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
10804 && TYPE_OVERFLOW_WRAPS (sizetype)
10805 && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (sizetype))
10806 /* Calculate the rhs constant. */
10807 (with { offset_int off = wi::to_offset (@1) - (cmp == LE_EXPR ? 1 : 0);
10808 offset_int rhs = off * 2; }
10809 /* Always fails for negative values. */
10810 (if (wi::min_precision (rhs, UNSIGNED) <= TYPE_PRECISION (sizetype))
10811 /* Since the order of @0 and @2 doesn't matter, let tree_swap_operands_p
10812 pick a canonical order. This increases the chances of using the
10813 same pointer_plus in multiple checks. */
10814 (with { bool swap_p = tree_swap_operands_p (@0, @2);
10815 tree rhs_tree = wide_int_to_tree (sizetype, rhs); }
10816 (if (cmp == LT_EXPR)
10817 (gt (convert:sizetype
10818 (pointer_diff:ssizetype { swap_p ? @4 : @3; }
10819 { swap_p ? @0 : @2; }))
10820 { rhs_tree; })
10821 (gt (convert:sizetype
10822 (pointer_diff:ssizetype
10823 (pointer_plus { swap_p ? @2 : @0; }
10824 { wide_int_to_tree (sizetype, off); })
10825 { swap_p ? @0 : @2; }))
10826 { rhs_tree; })))))))))
10827
10828 /* Fold REDUC (@0 & @1) -> @0[I] & @1[I] if element I is the only nonzero
10829 element of @1. */
10830 (for reduc (IFN_REDUC_PLUS IFN_REDUC_IOR IFN_REDUC_XOR)
10831 (simplify (reduc (view_convert? (bit_and @0 VECTOR_CST@1)))
10832 (with { int i = single_nonzero_element (@1); }
10833 (if (i >= 0)
10834 (with { tree elt = vector_cst_elt (@1, i);
10835 tree elt_type = TREE_TYPE (elt);
10836 unsigned int elt_bits = tree_to_uhwi (TYPE_SIZE (elt_type));
10837 tree size = bitsize_int (elt_bits);
10838 tree pos = bitsize_int (elt_bits * i); }
10839 (if (compare_tree_int (TYPE_SIZE (type), elt_bits) == 0)
10840 (view_convert
10841 (bit_and:elt_type
10842 (BIT_FIELD_REF:elt_type @0 { size; } { pos; })
10843 { elt; }))))))))
10844
10845 /* Fold reduction of a single nonzero element constructor. */
10846 (for reduc (IFN_REDUC_PLUS IFN_REDUC_IOR IFN_REDUC_XOR)
10847 (simplify (reduc (CONSTRUCTOR@0))
10848 (with { tree ctor = (TREE_CODE (@0) == SSA_NAME
10849 ? gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0)) : @0);
10850 tree elt = ctor_single_nonzero_element (ctor); }
10851 (if (elt
10852 && types_match (type, TREE_TYPE (elt))
10853 && !HONOR_SNANS (type)
10854 && !HONOR_SIGNED_ZEROS (type))
10855 { elt; }))))
10856
10857 /* Fold REDUC (@0 op VECTOR_CST) as REDUC (@0) op REDUC (VECTOR_CST). */
10858 (for reduc (IFN_REDUC_PLUS IFN_REDUC_MAX IFN_REDUC_MIN IFN_REDUC_FMAX
10859 IFN_REDUC_FMIN IFN_REDUC_AND IFN_REDUC_IOR IFN_REDUC_XOR)
10860 op (plus max min IFN_FMAX IFN_FMIN bit_and bit_ior bit_xor)
10861 (simplify (reduc (op @0 VECTOR_CST@1))
10862 (op (reduc:type @0) (reduc:type @1))))
10863
10864 /* Simplify .REDUC_IOR (@0) ==/!= 0 to @0 ==/!= 0. */
10865 (for cmp (eq ne)
10866 (simplify
10867 (cmp (IFN_REDUC_IOR @0) integer_zerop)
10868 (if (VECTOR_MODE_P (TYPE_MODE (TREE_TYPE (@0)))
10869 && can_compare_p (cmp == EQ_EXPR ? EQ : NE, TYPE_MODE (TREE_TYPE (@0)),
10870 ccp_jump))
10871 (cmp @0 { build_zero_cst (TREE_TYPE (@0)); }))))
10872
10873 /* Simplify vector floating point operations of alternating sub/add pairs
10874 into using an fneg of a wider element type followed by a normal add.
10875 under IEEE 754 the fneg of the wider type will negate every even entry
10876 and when doing an add we get a sub of the even and add of every odd
10877 elements. */
10878 (for plusminus (plus minus)
10879 minusplus (minus plus)
10880 (simplify
10881 (vec_perm (plusminus @0 @1) (minusplus @2 @3) VECTOR_CST@4)
10882 (if (!VECTOR_INTEGER_TYPE_P (type)
10883 && !FLOAT_WORDS_BIG_ENDIAN
10884 /* plus is commutative, while minus is not, so :c can't be used.
10885 Do equality comparisons by hand and at the end pick the operands
10886 from the minus. */
10887 && (operand_equal_p (@0, @2, 0)
10888 ? operand_equal_p (@1, @3, 0)
10889 : operand_equal_p (@0, @3, 0) && operand_equal_p (@1, @2, 0)))
10890 (with
10891 {
10892 /* Build a vector of integers from the tree mask. */
10893 vec_perm_builder builder;
10894 }
10895 (if (tree_to_vec_perm_builder (&builder, @4))
10896 (with
10897 {
10898 /* Create a vec_perm_indices for the integer vector. */
10899 poly_uint64 nelts = TYPE_VECTOR_SUBPARTS (type);
10900 vec_perm_indices sel (builder, 2, nelts);
10901 machine_mode vec_mode = TYPE_MODE (type);
10902 machine_mode wide_mode;
10903 scalar_mode wide_elt_mode;
10904 poly_uint64 wide_nunits;
10905 scalar_mode inner_mode = GET_MODE_INNER (vec_mode);
10906 }
10907 (if (VECTOR_MODE_P (vec_mode)
10908 && sel.series_p (0, 2, 0, 2)
10909 && sel.series_p (1, 2, nelts + 1, 2)
10910 && GET_MODE_2XWIDER_MODE (inner_mode).exists (&wide_elt_mode)
10911 && multiple_p (GET_MODE_NUNITS (vec_mode), 2, &wide_nunits)
10912 && related_vector_mode (vec_mode, wide_elt_mode,
10913 wide_nunits).exists (&wide_mode))
10914 (with
10915 {
10916 tree stype
10917 = lang_hooks.types.type_for_mode (GET_MODE_INNER (wide_mode),
10918 TYPE_UNSIGNED (type));
10919 tree ntype = build_vector_type_for_mode (stype, wide_mode);
10920
10921 /* The format has to be a non-extended ieee format. */
10922 const struct real_format *fmt_old = FLOAT_MODE_FORMAT (vec_mode);
10923 const struct real_format *fmt_new = FLOAT_MODE_FORMAT (wide_mode);
10924 }
10925 (if (TYPE_MODE (stype) != BLKmode
10926 && VECTOR_TYPE_P (ntype)
10927 && fmt_old != NULL
10928 && fmt_new != NULL)
10929 (with
10930 {
10931 /* If the target doesn't support v1xx vectors, try using
10932 scalar mode xx instead. */
10933 if (known_eq (GET_MODE_NUNITS (wide_mode), 1)
10934 && !target_supports_op_p (ntype, NEGATE_EXPR, optab_vector))
10935 ntype = stype;
10936 }
10937 (if (fmt_new->signbit_rw
10938 == fmt_old->signbit_rw + GET_MODE_UNIT_BITSIZE (vec_mode)
10939 && fmt_new->signbit_rw == fmt_new->signbit_ro
10940 && targetm.can_change_mode_class (TYPE_MODE (ntype),
10941 TYPE_MODE (type), ALL_REGS)
10942 && ((optimize_vectors_before_lowering_p ()
10943 && VECTOR_TYPE_P (ntype))
10944 || target_supports_op_p (ntype, NEGATE_EXPR, optab_vector)))
10945 (if (plusminus == PLUS_EXPR)
10946 (plus (view_convert:type (negate (view_convert:ntype @3))) @2)
10947 (minus @0 (view_convert:type
10948 (negate (view_convert:ntype @1))))))))))))))))
10949
10950 (simplify
10951 (vec_perm @0 @1 VECTOR_CST@2)
10952 (with
10953 {
10954 tree op0 = @0, op1 = @1, op2 = @2;
10955 machine_mode result_mode = TYPE_MODE (type);
10956 machine_mode op_mode = TYPE_MODE (TREE_TYPE (op0));
10957
10958 /* Build a vector of integers from the tree mask. */
10959 vec_perm_builder builder;
10960 }
10961 (if (tree_to_vec_perm_builder (&builder, op2))
10962 (with
10963 {
10964 /* Create a vec_perm_indices for the integer vector. */
10965 poly_uint64 nelts = TYPE_VECTOR_SUBPARTS (type);
10966 poly_uint64 nelts_in = TYPE_VECTOR_SUBPARTS (TREE_TYPE (op0));
10967 bool single_arg = (op0 == op1);
10968 vec_perm_indices sel (builder, single_arg ? 1 : 2, nelts_in);
10969 }
10970 (if (known_eq (nelts, nelts_in) && sel.series_p (0, 1, 0, 1))
10971 (view_convert @0)
10972 (if (known_eq (nelts, nelts_in) && sel.series_p (0, 1, nelts_in, 1))
10973 (view_convert @1)
10974 (with
10975 {
10976 if (!single_arg)
10977 {
10978 if (sel.all_from_input_p (0))
10979 op1 = op0;
10980 else if (sel.all_from_input_p (1))
10981 {
10982 op0 = op1;
10983 sel.rotate_inputs (1);
10984 }
10985 else if (known_ge (poly_uint64 (sel[0]), nelts_in))
10986 {
10987 std::swap (op0, op1);
10988 sel.rotate_inputs (1);
10989 }
10990 }
10991 gassign *def;
10992 tree cop0 = op0, cop1 = op1;
10993 if (TREE_CODE (op0) == SSA_NAME
10994 && (def = dyn_cast <gassign *> (SSA_NAME_DEF_STMT (op0)))
10995 && gimple_assign_rhs_code (def) == CONSTRUCTOR)
10996 cop0 = gimple_assign_rhs1 (def);
10997 if (TREE_CODE (op1) == SSA_NAME
10998 && (def = dyn_cast <gassign *> (SSA_NAME_DEF_STMT (op1)))
10999 && gimple_assign_rhs_code (def) == CONSTRUCTOR)
11000 cop1 = gimple_assign_rhs1 (def);
11001 tree t;
11002 }
11003 (if ((TREE_CODE (cop0) == VECTOR_CST
11004 || TREE_CODE (cop0) == CONSTRUCTOR)
11005 && (TREE_CODE (cop1) == VECTOR_CST
11006 || TREE_CODE (cop1) == CONSTRUCTOR)
11007 && (t = fold_vec_perm (type, cop0, cop1, sel)))
11008 { t; }
11009 (with
11010 {
11011 bool changed = (op0 == op1 && !single_arg);
11012 tree ins = NULL_TREE;
11013 unsigned at = 0;
11014
11015 /* See if the permutation is performing a single element
11016 insert from a CONSTRUCTOR or constant and use a BIT_INSERT_EXPR
11017 in that case. But only if the vector mode is supported,
11018 otherwise this is invalid GIMPLE. */
11019 if (op_mode != BLKmode
11020 && known_eq (nelts, nelts_in)
11021 && (TREE_CODE (cop0) == VECTOR_CST
11022 || TREE_CODE (cop0) == CONSTRUCTOR
11023 || TREE_CODE (cop1) == VECTOR_CST
11024 || TREE_CODE (cop1) == CONSTRUCTOR))
11025 {
11026 bool insert_first_p = sel.series_p (1, 1, nelts + 1, 1);
11027 if (insert_first_p)
11028 {
11029 /* After canonicalizing the first elt to come from the
11030 first vector we only can insert the first elt from
11031 the first vector. */
11032 at = 0;
11033 if ((ins = fold_read_from_vector (cop0, sel[0])))
11034 op0 = op1;
11035 }
11036 /* The above can fail for two-element vectors which always
11037 appear to insert the first element, so try inserting
11038 into the second lane as well. For more than two
11039 elements that's wasted time. */
11040 if (!insert_first_p || (!ins && maybe_eq (nelts, 2u)))
11041 {
11042 unsigned int encoded_nelts = sel.encoding ().encoded_nelts ();
11043 for (at = 0; at < encoded_nelts; ++at)
11044 if (maybe_ne (sel[at], at))
11045 break;
11046 if (at < encoded_nelts
11047 && (known_eq (at + 1, nelts)
11048 || sel.series_p (at + 1, 1, at + 1, 1)))
11049 {
11050 if (known_lt (poly_uint64 (sel[at]), nelts))
11051 ins = fold_read_from_vector (cop0, sel[at]);
11052 else
11053 ins = fold_read_from_vector (cop1, sel[at] - nelts);
11054 }
11055 }
11056 }
11057
11058 /* Generate a canonical form of the selector. */
11059 if (!ins && sel.encoding () != builder)
11060 {
11061 /* Some targets are deficient and fail to expand a single
11062 argument permutation while still allowing an equivalent
11063 2-argument version. */
11064 tree oldop2 = op2;
11065 if (sel.ninputs () == 2
11066 || can_vec_perm_const_p (result_mode, op_mode, sel, false))
11067 op2 = vec_perm_indices_to_tree (TREE_TYPE (op2), sel);
11068 else
11069 {
11070 vec_perm_indices sel2 (builder, 2, nelts);
11071 if (can_vec_perm_const_p (result_mode, op_mode, sel2, false))
11072 op2 = vec_perm_indices_to_tree (TREE_TYPE (op2), sel2);
11073 else
11074 /* Not directly supported with either encoding,
11075 so use the preferred form. */
11076 op2 = vec_perm_indices_to_tree (TREE_TYPE (op2), sel);
11077 }
11078 if (!operand_equal_p (op2, oldop2, 0))
11079 changed = true;
11080 }
11081 }
11082 (if (ins)
11083 (bit_insert { op0; } { ins; }
11084 { bitsize_int (at * vector_element_bits (type)); })
11085 (if (changed)
11086 (vec_perm { op0; } { op1; } { op2; }))))))))))))
11087
11088 /* VEC_PERM_EXPR (v, v, mask) -> v where v contains same element. */
11089
11090 (match vec_same_elem_p
11091 (vec_duplicate @0))
11092
11093 (match vec_same_elem_p
11094 CONSTRUCTOR@0
11095 (if (TREE_CODE (@0) == SSA_NAME
11096 && uniform_vector_p (gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0))))))
11097
11098 (match vec_same_elem_p
11099 @0
11100 (if (uniform_vector_p (@0))))
11101
11102
11103 (simplify
11104 (vec_perm vec_same_elem_p@0 @0 @1)
11105 (if (types_match (type, TREE_TYPE (@0)))
11106 @0
11107 (with
11108 {
11109 tree elem = uniform_vector_p (@0);
11110 }
11111 (if (elem)
11112 { build_vector_from_val (type, elem); }))))
11113
11114 /* Push VEC_PERM earlier if that may help FMA perception (PR101895). */
11115 (simplify
11116 (plus:c (vec_perm:s (mult:c@0 @1 vec_same_elem_p@2) @0 @3) @4)
11117 (if (TREE_CODE (@0) == SSA_NAME && num_imm_uses (@0) == 2)
11118 (plus (mult (vec_perm @1 @1 @3) @2) @4)))
11119 (simplify
11120 (minus (vec_perm:s (mult:c@0 @1 vec_same_elem_p@2) @0 @3) @4)
11121 (if (TREE_CODE (@0) == SSA_NAME && num_imm_uses (@0) == 2)
11122 (minus (mult (vec_perm @1 @1 @3) @2) @4)))
11123
11124
11125 /* Merge
11126 c = VEC_PERM_EXPR <a, b, VCST0>;
11127 d = VEC_PERM_EXPR <c, c, VCST1>;
11128 to
11129 d = VEC_PERM_EXPR <a, b, NEW_VCST>; */
11130
11131 (simplify
11132 (vec_perm (view_convert?@0 (vec_perm@1 @2 @3 VECTOR_CST@4)) @0 VECTOR_CST@5)
11133 (if (TYPE_VECTOR_SUBPARTS (type).is_constant ())
11134 (with
11135 {
11136 machine_mode result_mode = TYPE_MODE (type);
11137 machine_mode op_mode = TYPE_MODE (TREE_TYPE (@2));
11138 int nelts = TYPE_VECTOR_SUBPARTS (type).to_constant ();
11139 vec_perm_builder builder0;
11140 vec_perm_builder builder1;
11141 vec_perm_builder builder2 (nelts, nelts, 1);
11142 }
11143 (if (tree_to_vec_perm_builder (&builder0, @4)
11144 && tree_to_vec_perm_builder (&builder1, @5)
11145 && TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))
11146 == TYPE_SIZE (TREE_TYPE (TREE_TYPE (@1))))
11147 (with
11148 {
11149 vec_perm_indices sel0 (builder0, 2, nelts);
11150 vec_perm_indices sel1 (builder1, 2, nelts);
11151
11152 for (int i = 0; i < nelts; i++)
11153 builder2.quick_push (sel0[sel1[i].to_constant ()]);
11154
11155 vec_perm_indices sel2 (builder2, 2, nelts);
11156
11157 tree op0 = NULL_TREE;
11158 /* If the new VEC_PERM_EXPR can't be handled but both
11159 original VEC_PERM_EXPRs can, punt.
11160 If one or both of the original VEC_PERM_EXPRs can't be
11161 handled and the new one can't be either, don't increase
11162 number of VEC_PERM_EXPRs that can't be handled. */
11163 if (can_vec_perm_const_p (result_mode, op_mode, sel2, false)
11164 || (single_use (@0)
11165 ? (!can_vec_perm_const_p (result_mode, op_mode, sel0, false)
11166 || !can_vec_perm_const_p (result_mode, op_mode, sel1, false))
11167 : !can_vec_perm_const_p (result_mode, op_mode, sel1, false)))
11168 op0 = vec_perm_indices_to_tree (TREE_TYPE (@5), sel2);
11169 }
11170 (if (op0)
11171 (view_convert (vec_perm @2 @3 { op0; }))))))))
11172
11173 /* Merge
11174 c = VEC_PERM_EXPR <a, b, VCST0>;
11175 d = VEC_PERM_EXPR <x, c, VCST1>;
11176 to
11177 d = VEC_PERM_EXPR <x, {a,b}, NEW_VCST>;
11178 when all elements from a or b are replaced by the later
11179 permutation. */
11180
11181 (simplify
11182 (vec_perm @5 (vec_perm@0 @1 @2 VECTOR_CST@3) VECTOR_CST@4)
11183 (if (TYPE_VECTOR_SUBPARTS (type).is_constant ())
11184 (with
11185 {
11186 machine_mode result_mode = TYPE_MODE (type);
11187 machine_mode op_mode = TYPE_MODE (TREE_TYPE (@1));
11188 int nelts = TYPE_VECTOR_SUBPARTS (type).to_constant ();
11189 vec_perm_builder builder0;
11190 vec_perm_builder builder1;
11191 vec_perm_builder builder2 (nelts, nelts, 2);
11192 }
11193 (if (tree_to_vec_perm_builder (&builder0, @3)
11194 && tree_to_vec_perm_builder (&builder1, @4))
11195 (with
11196 {
11197 vec_perm_indices sel0 (builder0, 2, nelts);
11198 vec_perm_indices sel1 (builder1, 2, nelts);
11199 bool use_1 = false, use_2 = false;
11200
11201 for (int i = 0; i < nelts; i++)
11202 {
11203 if (known_lt ((poly_uint64)sel1[i], sel1.nelts_per_input ()))
11204 builder2.quick_push (sel1[i]);
11205 else
11206 {
11207 poly_uint64 j = sel0[(sel1[i] - sel1.nelts_per_input ())
11208 .to_constant ()];
11209 if (known_lt (j, sel0.nelts_per_input ()))
11210 use_1 = true;
11211 else
11212 {
11213 use_2 = true;
11214 j -= sel0.nelts_per_input ();
11215 }
11216 builder2.quick_push (j + sel1.nelts_per_input ());
11217 }
11218 }
11219 }
11220 (if (use_1 ^ use_2)
11221 (with
11222 {
11223 vec_perm_indices sel2 (builder2, 2, nelts);
11224 tree op0 = NULL_TREE;
11225 /* If the new VEC_PERM_EXPR can't be handled but both
11226 original VEC_PERM_EXPRs can, punt.
11227 If one or both of the original VEC_PERM_EXPRs can't be
11228 handled and the new one can't be either, don't increase
11229 number of VEC_PERM_EXPRs that can't be handled. */
11230 if (can_vec_perm_const_p (result_mode, op_mode, sel2, false)
11231 || (single_use (@0)
11232 ? (!can_vec_perm_const_p (result_mode, op_mode, sel0, false)
11233 || !can_vec_perm_const_p (result_mode, op_mode, sel1, false))
11234 : !can_vec_perm_const_p (result_mode, op_mode, sel1, false)))
11235 op0 = vec_perm_indices_to_tree (TREE_TYPE (@4), sel2);
11236 }
11237 (if (op0)
11238 (switch
11239 (if (use_1)
11240 (vec_perm @5 @1 { op0; }))
11241 (if (use_2)
11242 (vec_perm @5 @2 { op0; })))))))))))
11243
11244 /* And the case with swapped outer permute sources. */
11245
11246 (simplify
11247 (vec_perm (vec_perm@0 @1 @2 VECTOR_CST@3) @5 VECTOR_CST@4)
11248 (if (TYPE_VECTOR_SUBPARTS (type).is_constant ())
11249 (with
11250 {
11251 machine_mode result_mode = TYPE_MODE (type);
11252 machine_mode op_mode = TYPE_MODE (TREE_TYPE (@1));
11253 int nelts = TYPE_VECTOR_SUBPARTS (type).to_constant ();
11254 vec_perm_builder builder0;
11255 vec_perm_builder builder1;
11256 vec_perm_builder builder2 (nelts, nelts, 2);
11257 }
11258 (if (tree_to_vec_perm_builder (&builder0, @3)
11259 && tree_to_vec_perm_builder (&builder1, @4))
11260 (with
11261 {
11262 vec_perm_indices sel0 (builder0, 2, nelts);
11263 vec_perm_indices sel1 (builder1, 2, nelts);
11264 bool use_1 = false, use_2 = false;
11265
11266 for (int i = 0; i < nelts; i++)
11267 {
11268 if (known_ge ((poly_uint64)sel1[i], sel1.nelts_per_input ()))
11269 builder2.quick_push (sel1[i]);
11270 else
11271 {
11272 poly_uint64 j = sel0[sel1[i].to_constant ()];
11273 if (known_lt (j, sel0.nelts_per_input ()))
11274 use_1 = true;
11275 else
11276 {
11277 use_2 = true;
11278 j -= sel0.nelts_per_input ();
11279 }
11280 builder2.quick_push (j);
11281 }
11282 }
11283 }
11284 (if (use_1 ^ use_2)
11285 (with
11286 {
11287 vec_perm_indices sel2 (builder2, 2, nelts);
11288 tree op0 = NULL_TREE;
11289 /* If the new VEC_PERM_EXPR can't be handled but both
11290 original VEC_PERM_EXPRs can, punt.
11291 If one or both of the original VEC_PERM_EXPRs can't be
11292 handled and the new one can't be either, don't increase
11293 number of VEC_PERM_EXPRs that can't be handled. */
11294 if (can_vec_perm_const_p (result_mode, op_mode, sel2, false)
11295 || (single_use (@0)
11296 ? (!can_vec_perm_const_p (result_mode, op_mode, sel0, false)
11297 || !can_vec_perm_const_p (result_mode, op_mode, sel1, false))
11298 : !can_vec_perm_const_p (result_mode, op_mode, sel1, false)))
11299 op0 = vec_perm_indices_to_tree (TREE_TYPE (@4), sel2);
11300 }
11301 (if (op0)
11302 (switch
11303 (if (use_1)
11304 (vec_perm @1 @5 { op0; }))
11305 (if (use_2)
11306 (vec_perm @2 @5 { op0; })))))))))))
11307
11308
11309 /* Match count trailing zeroes for simplify_count_trailing_zeroes in fwprop.
11310 The canonical form is array[((x & -x) * C) >> SHIFT] where C is a magic
11311 constant which when multiplied by a power of 2 contains a unique value
11312 in the top 5 or 6 bits. This is then indexed into a table which maps it
11313 to the number of trailing zeroes. */
11314 (match (ctz_table_index @1 @2 @3)
11315 (rshift (mult (bit_and:c (negate @1) @1) INTEGER_CST@2) INTEGER_CST@3))
11316
11317 (match (cond_expr_convert_p @0 @2 @3 @6)
11318 (cond (simple_comparison@6 @0 @1) (convert@4 @2) (convert@5 @3))
11319 (if (INTEGRAL_TYPE_P (type)
11320 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
11321 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
11322 && INTEGRAL_TYPE_P (TREE_TYPE (@3))
11323 && TYPE_PRECISION (type) != TYPE_PRECISION (TREE_TYPE (@0))
11324 && TYPE_PRECISION (TREE_TYPE (@0))
11325 == TYPE_PRECISION (TREE_TYPE (@2))
11326 && TYPE_PRECISION (TREE_TYPE (@0))
11327 == TYPE_PRECISION (TREE_TYPE (@3))
11328 /* For vect_recog_cond_expr_convert_pattern, @2 and @3 can differ in
11329 signess when convert is truncation, but not ok for extension since
11330 it's sign_extend vs zero_extend. */
11331 && (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type)
11332 || (TYPE_UNSIGNED (TREE_TYPE (@2))
11333 == TYPE_UNSIGNED (TREE_TYPE (@3))))
11334 && single_use (@4)
11335 && single_use (@5))))
11336
11337 (for bit_op (bit_and bit_ior bit_xor)
11338 (match (bitwise_induction_p @0 @2 @3)
11339 (bit_op:c
11340 (nop_convert1? (bit_not2?@0 (convert3? (lshift integer_onep@1 @2))))
11341 @3)))
11342
11343 (match (bitwise_induction_p @0 @2 @3)
11344 (bit_not
11345 (nop_convert1? (bit_xor@0 (convert2? (lshift integer_onep@1 @2)) @3))))
11346
11347 /* n - (((n > C1) ? n : C1) & -C2) -> n & C1 for unsigned case.
11348 n - (((n > C1) ? n : C1) & -C2) -> (n <= C1) ? n : (n & C1) for signed case. */
11349 (simplify
11350 (minus @0 (bit_and (max @0 INTEGER_CST@1) INTEGER_CST@2))
11351 (with { auto i = wi::neg (wi::to_wide (@2)); }
11352 /* Check if -C2 is a power of 2 and C1 = -C2 - 1. */
11353 (if (wi::popcount (i) == 1
11354 && (wi::to_wide (@1)) == (i - 1))
11355 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
11356 (bit_and @0 @1)
11357 (cond (le @0 @1) @0 (bit_and @0 @1))))))
11358
11359 /* -x & 1 -> x & 1. */
11360 (simplify
11361 (bit_and (negate @0) integer_onep@1)
11362 (if (!TYPE_OVERFLOW_SANITIZED (type))
11363 (bit_and @0 @1)))
11364
11365 /* `-a` is just `a` if the type is 1bit wide or when converting
11366 to a 1bit type; similar to the above transformation of `(-x)&1`.
11367 This is used mostly with the transformation of
11368 `a ? ~b : b` into `(-a)^b`.
11369 It also can show up with bitfields. */
11370 (simplify
11371 (convert? (negate @0))
11372 (if (INTEGRAL_TYPE_P (type)
11373 && TYPE_PRECISION (type) == 1
11374 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0)))
11375 (convert @0)))
11376
11377 /* Optimize
11378 c1 = VEC_PERM_EXPR (a, a, mask)
11379 c2 = VEC_PERM_EXPR (b, b, mask)
11380 c3 = c1 op c2
11381 -->
11382 c = a op b
11383 c3 = VEC_PERM_EXPR (c, c, mask)
11384 For all integer non-div operations. */
11385 (for op (plus minus mult bit_and bit_ior bit_xor
11386 lshift rshift)
11387 (simplify
11388 (op (vec_perm @0 @0 @2) (vec_perm @1 @1 @2))
11389 (if (VECTOR_INTEGER_TYPE_P (type))
11390 (vec_perm (op@3 @0 @1) @3 @2))))
11391
11392 /* Similar for float arithmetic when permutation constant covers
11393 all vector elements. */
11394 (for op (plus minus mult)
11395 (simplify
11396 (op (vec_perm @0 @0 VECTOR_CST@2) (vec_perm @1 @1 VECTOR_CST@2))
11397 (if (VECTOR_FLOAT_TYPE_P (type)
11398 && TYPE_VECTOR_SUBPARTS (type).is_constant ())
11399 (with
11400 {
11401 tree perm_cst = @2;
11402 vec_perm_builder builder;
11403 bool full_perm_p = false;
11404 if (tree_to_vec_perm_builder (&builder, perm_cst))
11405 {
11406 unsigned HOST_WIDE_INT nelts;
11407
11408 nelts = TYPE_VECTOR_SUBPARTS (type).to_constant ();
11409 /* Create a vec_perm_indices for the VECTOR_CST. */
11410 vec_perm_indices sel (builder, 1, nelts);
11411
11412 /* Check if perm indices covers all vector elements. */
11413 if (sel.encoding ().encoded_full_vector_p ())
11414 {
11415 auto_sbitmap seen (nelts);
11416 bitmap_clear (seen);
11417
11418 unsigned HOST_WIDE_INT count = 0, i;
11419
11420 for (i = 0; i < nelts; i++)
11421 {
11422 if (!bitmap_set_bit (seen, sel[i].to_constant ()))
11423 break;
11424 count++;
11425 }
11426 full_perm_p = count == nelts;
11427 }
11428 }
11429 }
11430 (if (full_perm_p)
11431 (vec_perm (op@3 @0 @1) @3 @2))))))
11432
11433 /* Strip nop-conversions __builtin_constant_p arguments. */
11434 (simplify
11435 (BUILT_IN_CONSTANT_P (nop_convert@1 @0))
11436 (BUILT_IN_CONSTANT_P @0))