]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/match.pd
MATCH: [PR111349] add missing :c to cmp in the `(a CMP CST1) ? max<a,CST2> : a` pattern
[thirdparty/gcc.git] / gcc / match.pd
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-2023 Free Software Foundation, Inc.
6 Contributed by Richard Biener <rguenther@suse.de>
7 and Prathamesh Kulkarni <bilbotheelffriend@gmail.com>
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 tree_expr_nonnegative_p
35 tree_expr_nonzero_p
36 integer_valued_real_p
37 integer_pow2p
38 uniform_integer_cst_p
39 HONOR_NANS
40 uniform_vector_p
41 expand_vec_cmp_expr_p
42 bitmask_inv_cst_vector_p)
43
44 /* Operator lists. */
45 (define_operator_list tcc_comparison
46 lt le eq ne ge gt unordered ordered unlt unle ungt unge uneq ltgt)
47 (define_operator_list inverted_tcc_comparison
48 ge gt ne eq lt le ordered unordered ge gt le lt ltgt uneq)
49 (define_operator_list inverted_tcc_comparison_with_nans
50 unge ungt ne eq unlt unle ordered unordered ge gt le lt ltgt uneq)
51 (define_operator_list swapped_tcc_comparison
52 gt ge eq ne le lt unordered ordered ungt unge unlt unle uneq ltgt)
53 (define_operator_list simple_comparison lt le eq ne ge gt)
54 (define_operator_list swapped_simple_comparison gt ge eq ne le lt)
55 (define_operator_list BSWAP BUILT_IN_BSWAP16 BUILT_IN_BSWAP32
56 BUILT_IN_BSWAP64 BUILT_IN_BSWAP128)
57
58 #include "cfn-operators.pd"
59
60 /* Define operand lists for math rounding functions {,i,l,ll}FN,
61 where the versions prefixed with "i" return an int, those prefixed with
62 "l" return a long and those prefixed with "ll" return a long long.
63
64 Also define operand lists:
65
66 X<FN>F for all float functions, in the order i, l, ll
67 X<FN> for all double functions, in the same order
68 X<FN>L for all long double functions, in the same order. */
69 #define DEFINE_INT_AND_FLOAT_ROUND_FN(FN) \
70 (define_operator_list X##FN##F BUILT_IN_I##FN##F \
71 BUILT_IN_L##FN##F \
72 BUILT_IN_LL##FN##F) \
73 (define_operator_list X##FN BUILT_IN_I##FN \
74 BUILT_IN_L##FN \
75 BUILT_IN_LL##FN) \
76 (define_operator_list X##FN##L BUILT_IN_I##FN##L \
77 BUILT_IN_L##FN##L \
78 BUILT_IN_LL##FN##L)
79
80 DEFINE_INT_AND_FLOAT_ROUND_FN (FLOOR)
81 DEFINE_INT_AND_FLOAT_ROUND_FN (CEIL)
82 DEFINE_INT_AND_FLOAT_ROUND_FN (ROUND)
83 DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
84
85 /* Unary operations and their associated IFN_COND_* function. */
86 (define_operator_list UNCOND_UNARY
87 negate bit_not)
88 (define_operator_list COND_UNARY
89 IFN_COND_NEG IFN_COND_NOT)
90
91 /* Binary operations and their associated IFN_COND_* function. */
92 (define_operator_list UNCOND_BINARY
93 plus minus
94 mult trunc_div trunc_mod rdiv
95 min max
96 IFN_FMIN IFN_FMAX
97 bit_and bit_ior bit_xor
98 lshift rshift)
99 (define_operator_list COND_BINARY
100 IFN_COND_ADD IFN_COND_SUB
101 IFN_COND_MUL IFN_COND_DIV IFN_COND_MOD IFN_COND_RDIV
102 IFN_COND_MIN IFN_COND_MAX
103 IFN_COND_FMIN IFN_COND_FMAX
104 IFN_COND_AND IFN_COND_IOR IFN_COND_XOR
105 IFN_COND_SHL IFN_COND_SHR)
106
107 /* Same for ternary operations. */
108 (define_operator_list UNCOND_TERNARY
109 IFN_FMA IFN_FMS IFN_FNMA IFN_FNMS)
110 (define_operator_list COND_TERNARY
111 IFN_COND_FMA IFN_COND_FMS IFN_COND_FNMA IFN_COND_FNMS)
112
113 /* __atomic_fetch_or_*, __atomic_fetch_xor_*, __atomic_xor_fetch_* */
114 (define_operator_list ATOMIC_FETCH_OR_XOR_N
115 BUILT_IN_ATOMIC_FETCH_OR_1 BUILT_IN_ATOMIC_FETCH_OR_2
116 BUILT_IN_ATOMIC_FETCH_OR_4 BUILT_IN_ATOMIC_FETCH_OR_8
117 BUILT_IN_ATOMIC_FETCH_OR_16
118 BUILT_IN_ATOMIC_FETCH_XOR_1 BUILT_IN_ATOMIC_FETCH_XOR_2
119 BUILT_IN_ATOMIC_FETCH_XOR_4 BUILT_IN_ATOMIC_FETCH_XOR_8
120 BUILT_IN_ATOMIC_FETCH_XOR_16
121 BUILT_IN_ATOMIC_XOR_FETCH_1 BUILT_IN_ATOMIC_XOR_FETCH_2
122 BUILT_IN_ATOMIC_XOR_FETCH_4 BUILT_IN_ATOMIC_XOR_FETCH_8
123 BUILT_IN_ATOMIC_XOR_FETCH_16)
124 /* __sync_fetch_and_or_*, __sync_fetch_and_xor_*, __sync_xor_and_fetch_* */
125 (define_operator_list SYNC_FETCH_OR_XOR_N
126 BUILT_IN_SYNC_FETCH_AND_OR_1 BUILT_IN_SYNC_FETCH_AND_OR_2
127 BUILT_IN_SYNC_FETCH_AND_OR_4 BUILT_IN_SYNC_FETCH_AND_OR_8
128 BUILT_IN_SYNC_FETCH_AND_OR_16
129 BUILT_IN_SYNC_FETCH_AND_XOR_1 BUILT_IN_SYNC_FETCH_AND_XOR_2
130 BUILT_IN_SYNC_FETCH_AND_XOR_4 BUILT_IN_SYNC_FETCH_AND_XOR_8
131 BUILT_IN_SYNC_FETCH_AND_XOR_16
132 BUILT_IN_SYNC_XOR_AND_FETCH_1 BUILT_IN_SYNC_XOR_AND_FETCH_2
133 BUILT_IN_SYNC_XOR_AND_FETCH_4 BUILT_IN_SYNC_XOR_AND_FETCH_8
134 BUILT_IN_SYNC_XOR_AND_FETCH_16)
135 /* __atomic_fetch_and_*. */
136 (define_operator_list ATOMIC_FETCH_AND_N
137 BUILT_IN_ATOMIC_FETCH_AND_1 BUILT_IN_ATOMIC_FETCH_AND_2
138 BUILT_IN_ATOMIC_FETCH_AND_4 BUILT_IN_ATOMIC_FETCH_AND_8
139 BUILT_IN_ATOMIC_FETCH_AND_16)
140 /* __sync_fetch_and_and_*. */
141 (define_operator_list SYNC_FETCH_AND_AND_N
142 BUILT_IN_SYNC_FETCH_AND_AND_1 BUILT_IN_SYNC_FETCH_AND_AND_2
143 BUILT_IN_SYNC_FETCH_AND_AND_4 BUILT_IN_SYNC_FETCH_AND_AND_8
144 BUILT_IN_SYNC_FETCH_AND_AND_16)
145
146 /* With nop_convert? combine convert? and view_convert? in one pattern
147 plus conditionalize on tree_nop_conversion_p conversions. */
148 (match (nop_convert @0)
149 (convert @0)
150 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))))
151 (match (nop_convert @0)
152 (view_convert @0)
153 (if (VECTOR_TYPE_P (type) && VECTOR_TYPE_P (TREE_TYPE (@0))
154 && known_eq (TYPE_VECTOR_SUBPARTS (type),
155 TYPE_VECTOR_SUBPARTS (TREE_TYPE (@0)))
156 && tree_nop_conversion_p (TREE_TYPE (type), TREE_TYPE (TREE_TYPE (@0))))))
157
158 #if GIMPLE
159 /* These are used by gimple_bitwise_inverted_equal_p to simplify
160 detection of BIT_NOT and comparisons. */
161 (match (bit_not_with_nop @0)
162 (bit_not @0))
163 (match (bit_not_with_nop @0)
164 (convert (bit_not @0))
165 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))))
166 (for cmp (tcc_comparison)
167 (match (maybe_cmp @0)
168 (cmp@0 @1 @2))
169 (match (maybe_cmp @0)
170 (convert (cmp@0 @1 @2))
171 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))))
172 )
173 #endif
174
175 /* Transform likes of (char) ABS_EXPR <(int) x> into (char) ABSU_EXPR <x>
176 ABSU_EXPR returns unsigned absolute value of the operand and the operand
177 of the ABSU_EXPR will have the corresponding signed type. */
178 (simplify (abs (convert @0))
179 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
180 && !TYPE_UNSIGNED (TREE_TYPE (@0))
181 && element_precision (type) > element_precision (TREE_TYPE (@0)))
182 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
183 (convert (absu:utype @0)))))
184
185 #if GIMPLE
186 /* Optimize (X + (X >> (prec - 1))) ^ (X >> (prec - 1)) into abs (X). */
187 (simplify
188 (bit_xor:c (plus:c @0 (rshift@2 @0 INTEGER_CST@1)) @2)
189 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
190 && !TYPE_UNSIGNED (TREE_TYPE (@0))
191 && wi::to_widest (@1) == element_precision (TREE_TYPE (@0)) - 1)
192 (abs @0)))
193 #endif
194
195 /* Simplifications of operations with one constant operand and
196 simplifications to constants or single values. */
197
198 (for op (plus pointer_plus minus bit_ior bit_xor)
199 (simplify
200 (op @0 integer_zerop)
201 (non_lvalue @0)))
202
203 /* 0 +p index -> (type)index */
204 (simplify
205 (pointer_plus integer_zerop @1)
206 (non_lvalue (convert @1)))
207
208 /* ptr - 0 -> (type)ptr */
209 (simplify
210 (pointer_diff @0 integer_zerop)
211 (convert @0))
212
213 /* See if ARG1 is zero and X + ARG1 reduces to X.
214 Likewise if the operands are reversed. */
215 (simplify
216 (plus:c @0 real_zerop@1)
217 (if (fold_real_zero_addition_p (type, @0, @1, 0))
218 (non_lvalue @0)))
219
220 /* See if ARG1 is zero and X - ARG1 reduces to X. */
221 (simplify
222 (minus @0 real_zerop@1)
223 (if (fold_real_zero_addition_p (type, @0, @1, 1))
224 (non_lvalue @0)))
225
226 /* Even if the fold_real_zero_addition_p can't simplify X + 0.0
227 into X, we can optimize (X + 0.0) + 0.0 or (X + 0.0) - 0.0
228 or (X - 0.0) + 0.0 into X + 0.0 and (X - 0.0) - 0.0 into X - 0.0
229 if not -frounding-math. For sNaNs the first operation would raise
230 exceptions but turn the result into qNan, so the second operation
231 would not raise it. */
232 (for inner_op (plus minus)
233 (for outer_op (plus minus)
234 (simplify
235 (outer_op (inner_op@3 @0 REAL_CST@1) REAL_CST@2)
236 (if (real_zerop (@1)
237 && real_zerop (@2)
238 && !HONOR_SIGN_DEPENDENT_ROUNDING (type))
239 (with { bool inner_plus = ((inner_op == PLUS_EXPR)
240 ^ REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@1)));
241 bool outer_plus
242 = ((outer_op == PLUS_EXPR)
243 ^ REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@2))); }
244 (if (outer_plus && !inner_plus)
245 (outer_op @0 @2)
246 @3))))))
247
248 /* Simplify x - x.
249 This is unsafe for certain floats even in non-IEEE formats.
250 In IEEE, it is unsafe because it does wrong for NaNs.
251 PR middle-end/98420: x - x may be -0.0 with FE_DOWNWARD.
252 Also note that operand_equal_p is always false if an operand
253 is volatile. */
254 (simplify
255 (minus @0 @0)
256 (if (!FLOAT_TYPE_P (type)
257 || (!tree_expr_maybe_nan_p (@0)
258 && !tree_expr_maybe_infinite_p (@0)
259 && (!HONOR_SIGN_DEPENDENT_ROUNDING (type)
260 || !HONOR_SIGNED_ZEROS (type))))
261 { build_zero_cst (type); }))
262 (simplify
263 (pointer_diff @@0 @0)
264 { build_zero_cst (type); })
265
266 (simplify
267 (mult @0 integer_zerop@1)
268 @1)
269
270 /* -x == x -> x == 0 */
271 (for cmp (eq ne)
272 (simplify
273 (cmp:c @0 (negate @0))
274 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
275 && !TYPE_OVERFLOW_WRAPS (TREE_TYPE(@0)))
276 (cmp @0 { build_zero_cst (TREE_TYPE(@0)); }))))
277
278 /* Maybe fold x * 0 to 0. The expressions aren't the same
279 when x is NaN, since x * 0 is also NaN. Nor are they the
280 same in modes with signed zeros, since multiplying a
281 negative value by 0 gives -0, not +0. Nor when x is +-Inf,
282 since x * 0 is NaN. */
283 (simplify
284 (mult @0 real_zerop@1)
285 (if (!tree_expr_maybe_nan_p (@0)
286 && (!HONOR_NANS (type) || !tree_expr_maybe_infinite_p (@0))
287 && (!HONOR_SIGNED_ZEROS (type) || tree_expr_nonnegative_p (@0)))
288 @1))
289
290 /* In IEEE floating point, x*1 is not equivalent to x for snans.
291 Likewise for complex arithmetic with signed zeros. */
292 (simplify
293 (mult @0 real_onep)
294 (if (!tree_expr_maybe_signaling_nan_p (@0)
295 && (!HONOR_SIGNED_ZEROS (type)
296 || !COMPLEX_FLOAT_TYPE_P (type)))
297 (non_lvalue @0)))
298
299 /* Transform x * -1.0 into -x. */
300 (simplify
301 (mult @0 real_minus_onep)
302 (if (!tree_expr_maybe_signaling_nan_p (@0)
303 && (!HONOR_SIGNED_ZEROS (type)
304 || !COMPLEX_FLOAT_TYPE_P (type)))
305 (negate @0)))
306
307 /* Transform x * { 0 or 1, 0 or 1, ... } into x & { 0 or -1, 0 or -1, ...},
308 unless the target has native support for the former but not the latter. */
309 (simplify
310 (mult @0 VECTOR_CST@1)
311 (if (initializer_each_zero_or_onep (@1)
312 && !HONOR_SNANS (type)
313 && !HONOR_SIGNED_ZEROS (type))
314 (with { tree itype = FLOAT_TYPE_P (type) ? unsigned_type_for (type) : type; }
315 (if (itype
316 && (!VECTOR_MODE_P (TYPE_MODE (type))
317 || (VECTOR_MODE_P (TYPE_MODE (itype))
318 && optab_handler (and_optab,
319 TYPE_MODE (itype)) != CODE_FOR_nothing)))
320 (view_convert (bit_and:itype (view_convert @0)
321 (ne @1 { build_zero_cst (type); })))))))
322
323 /* In SWAR (SIMD within a register) code a signed comparison of packed data
324 can be constructed with a particular combination of shift, bitwise and,
325 and multiplication by constants. If that code is vectorized we can
326 convert this pattern into a more efficient vector comparison. */
327 (simplify
328 (mult (bit_and (rshift @0 uniform_integer_cst_p@1)
329 uniform_integer_cst_p@2)
330 uniform_integer_cst_p@3)
331 (with {
332 tree rshift_cst = uniform_integer_cst_p (@1);
333 tree bit_and_cst = uniform_integer_cst_p (@2);
334 tree mult_cst = uniform_integer_cst_p (@3);
335 }
336 /* Make sure we're working with vectors and uniform vector constants. */
337 (if (VECTOR_TYPE_P (type)
338 && tree_fits_uhwi_p (rshift_cst)
339 && tree_fits_uhwi_p (mult_cst)
340 && tree_fits_uhwi_p (bit_and_cst))
341 /* Compute what constants would be needed for this to represent a packed
342 comparison based on the shift amount denoted by RSHIFT_CST. */
343 (with {
344 HOST_WIDE_INT vec_elem_bits = vector_element_bits (type);
345 poly_int64 vec_nelts = TYPE_VECTOR_SUBPARTS (type);
346 poly_int64 vec_bits = vec_elem_bits * vec_nelts;
347 unsigned HOST_WIDE_INT cmp_bits_i, bit_and_i, mult_i;
348 unsigned HOST_WIDE_INT target_mult_i, target_bit_and_i;
349 cmp_bits_i = tree_to_uhwi (rshift_cst) + 1;
350 mult_i = tree_to_uhwi (mult_cst);
351 target_mult_i = (HOST_WIDE_INT_1U << cmp_bits_i) - 1;
352 bit_and_i = tree_to_uhwi (bit_and_cst);
353 target_bit_and_i = 0;
354
355 /* The bit pattern in BIT_AND_I should be a mask for the least
356 significant bit of each packed element that is CMP_BITS wide. */
357 for (unsigned i = 0; i < vec_elem_bits / cmp_bits_i; i++)
358 target_bit_and_i = (target_bit_and_i << cmp_bits_i) | 1U;
359 }
360 (if ((exact_log2 (cmp_bits_i)) >= 0
361 && cmp_bits_i < HOST_BITS_PER_WIDE_INT
362 && multiple_p (vec_bits, cmp_bits_i)
363 && vec_elem_bits <= HOST_BITS_PER_WIDE_INT
364 && target_mult_i == mult_i
365 && target_bit_and_i == bit_and_i)
366 /* Compute the vector shape for the comparison and check if the target is
367 able to expand the comparison with that type. */
368 (with {
369 /* We're doing a signed comparison. */
370 tree cmp_type = build_nonstandard_integer_type (cmp_bits_i, 0);
371 poly_int64 vector_type_nelts = exact_div (vec_bits, cmp_bits_i);
372 tree vec_cmp_type = build_vector_type (cmp_type, vector_type_nelts);
373 tree vec_truth_type = truth_type_for (vec_cmp_type);
374 tree zeros = build_zero_cst (vec_cmp_type);
375 tree ones = build_all_ones_cst (vec_cmp_type);
376 }
377 (if (expand_vec_cmp_expr_p (vec_cmp_type, vec_truth_type, LT_EXPR)
378 && expand_vec_cond_expr_p (vec_cmp_type, vec_truth_type, LT_EXPR))
379 (view_convert:type (vec_cond (lt:vec_truth_type
380 (view_convert:vec_cmp_type @0)
381 { zeros; })
382 { ones; } { zeros; })))))))))
383
384 (for cmp (gt ge lt le)
385 outp (convert convert negate negate)
386 outn (negate negate convert convert)
387 /* Transform X * (X > 0.0 ? 1.0 : -1.0) into abs(X). */
388 /* Transform X * (X >= 0.0 ? 1.0 : -1.0) into abs(X). */
389 /* Transform X * (X < 0.0 ? 1.0 : -1.0) into -abs(X). */
390 /* Transform X * (X <= 0.0 ? 1.0 : -1.0) into -abs(X). */
391 (simplify
392 (mult:c @0 (cond (cmp @0 real_zerop) real_onep@1 real_minus_onep))
393 (if (!tree_expr_maybe_nan_p (@0) && !HONOR_SIGNED_ZEROS (type))
394 (outp (abs @0))))
395 /* Transform X * (X > 0.0 ? -1.0 : 1.0) into -abs(X). */
396 /* Transform X * (X >= 0.0 ? -1.0 : 1.0) into -abs(X). */
397 /* Transform X * (X < 0.0 ? -1.0 : 1.0) into abs(X). */
398 /* Transform X * (X <= 0.0 ? -1.0 : 1.0) into abs(X). */
399 (simplify
400 (mult:c @0 (cond (cmp @0 real_zerop) real_minus_onep real_onep@1))
401 (if (!tree_expr_maybe_nan_p (@0) && !HONOR_SIGNED_ZEROS (type))
402 (outn (abs @0)))))
403
404 /* Transform X * copysign (1.0, X) into abs(X). */
405 (simplify
406 (mult:c @0 (COPYSIGN_ALL real_onep @0))
407 (if (!tree_expr_maybe_nan_p (@0) && !HONOR_SIGNED_ZEROS (type))
408 (abs @0)))
409
410 /* Transform X * copysign (1.0, -X) into -abs(X). */
411 (simplify
412 (mult:c @0 (COPYSIGN_ALL real_onep (negate @0)))
413 (if (!tree_expr_maybe_nan_p (@0) && !HONOR_SIGNED_ZEROS (type))
414 (negate (abs @0))))
415
416 /* Transform copysign (CST, X) into copysign (ABS(CST), X). */
417 (simplify
418 (COPYSIGN_ALL REAL_CST@0 @1)
419 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@0)))
420 (COPYSIGN_ALL (negate @0) @1)))
421
422 /* Transform c ? x * copysign (1, y) : z to c ? x ^ signs(y) : z.
423 tree-ssa-math-opts.cc does the corresponding optimization for
424 unconditional multiplications (via xorsign). */
425 (simplify
426 (IFN_COND_MUL:c @0 @1 (IFN_COPYSIGN real_onep @2) @3)
427 (with { tree signs = sign_mask_for (type); }
428 (if (signs)
429 (with { tree inttype = TREE_TYPE (signs); }
430 (view_convert:type
431 (IFN_COND_XOR:inttype @0
432 (view_convert:inttype @1)
433 (bit_and (view_convert:inttype @2) { signs; })
434 (view_convert:inttype @3)))))))
435
436 /* (x >= 0 ? x : 0) + (x <= 0 ? -x : 0) -> abs x. */
437 (simplify
438 (plus:c (max @0 integer_zerop) (max (negate @0) integer_zerop))
439 (abs @0))
440
441 /* X * 1, X / 1 -> X. */
442 (for op (mult trunc_div ceil_div floor_div round_div exact_div)
443 (simplify
444 (op @0 integer_onep)
445 (non_lvalue @0)))
446
447 /* (A / (1 << B)) -> (A >> B).
448 Only for unsigned A. For signed A, this would not preserve rounding
449 toward zero.
450 For example: (-1 / ( 1 << B)) != -1 >> B.
451 Also handle widening conversions, like:
452 (A / (unsigned long long) (1U << B)) -> (A >> B)
453 or
454 (A / (unsigned long long) (1 << B)) -> (A >> B).
455 If the left shift is signed, it can be done only if the upper bits
456 of A starting from shift's type sign bit are zero, as
457 (unsigned long long) (1 << 31) is -2147483648ULL, not 2147483648ULL,
458 so it is valid only if A >> 31 is zero. */
459 (simplify
460 (trunc_div (convert?@0 @3) (convert2? (lshift integer_onep@1 @2)))
461 (if ((TYPE_UNSIGNED (type) || tree_expr_nonnegative_p (@0))
462 && (!VECTOR_TYPE_P (type)
463 || target_supports_op_p (type, RSHIFT_EXPR, optab_vector)
464 || target_supports_op_p (type, RSHIFT_EXPR, optab_scalar))
465 && (useless_type_conversion_p (type, TREE_TYPE (@1))
466 || (element_precision (type) >= element_precision (TREE_TYPE (@1))
467 && (TYPE_UNSIGNED (TREE_TYPE (@1))
468 || (element_precision (type)
469 == element_precision (TREE_TYPE (@1)))
470 || (INTEGRAL_TYPE_P (type)
471 && (tree_nonzero_bits (@0)
472 & wi::mask (element_precision (TREE_TYPE (@1)) - 1,
473 true,
474 element_precision (type))) == 0)))))
475 (if (!VECTOR_TYPE_P (type)
476 && useless_type_conversion_p (TREE_TYPE (@3), TREE_TYPE (@1))
477 && element_precision (TREE_TYPE (@3)) < element_precision (type))
478 (convert (rshift @3 @2))
479 (rshift @0 @2))))
480
481 /* Preserve explicit divisions by 0: the C++ front-end wants to detect
482 undefined behavior in constexpr evaluation, and assuming that the division
483 traps enables better optimizations than these anyway. */
484 (for div (trunc_div ceil_div floor_div round_div exact_div)
485 /* 0 / X is always zero. */
486 (simplify
487 (div integer_zerop@0 @1)
488 /* But not for 0 / 0 so that we can get the proper warnings and errors. */
489 (if (!integer_zerop (@1))
490 @0))
491 /* X / -1 is -X. */
492 (simplify
493 (div @0 integer_minus_onep@1)
494 (if (!TYPE_UNSIGNED (type))
495 (negate @0)))
496 /* X / bool_range_Y is X. */
497 (simplify
498 (div @0 SSA_NAME@1)
499 (if (INTEGRAL_TYPE_P (type)
500 && ssa_name_has_boolean_range (@1)
501 && !flag_non_call_exceptions)
502 @0))
503 /* X / X is one. */
504 (simplify
505 (div @0 @0)
506 /* But not for 0 / 0 so that we can get the proper warnings and errors.
507 And not for _Fract types where we can't build 1. */
508 (if (!ALL_FRACT_MODE_P (TYPE_MODE (type))
509 && !integer_zerop (@0)
510 && (!flag_non_call_exceptions || tree_expr_nonzero_p (@0)))
511 { build_one_cst (type); }))
512 /* X / abs (X) is X < 0 ? -1 : 1. */
513 (simplify
514 (div:C @0 (abs @0))
515 (if (INTEGRAL_TYPE_P (type)
516 && TYPE_OVERFLOW_UNDEFINED (type)
517 && !integer_zerop (@0)
518 && (!flag_non_call_exceptions || tree_expr_nonzero_p (@0)))
519 (cond (lt @0 { build_zero_cst (type); })
520 { build_minus_one_cst (type); } { build_one_cst (type); })))
521 /* X / -X is -1. */
522 (simplify
523 (div:C @0 (negate @0))
524 (if ((INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
525 && TYPE_OVERFLOW_UNDEFINED (type)
526 && !integer_zerop (@0)
527 && (!flag_non_call_exceptions || tree_expr_nonzero_p (@0)))
528 { build_minus_one_cst (type); })))
529
530 /* For unsigned integral types, FLOOR_DIV_EXPR is the same as
531 TRUNC_DIV_EXPR. Rewrite into the latter in this case. Similarly
532 for MOD instead of DIV. */
533 (for floor_divmod (floor_div floor_mod)
534 trunc_divmod (trunc_div trunc_mod)
535 (simplify
536 (floor_divmod @0 @1)
537 (if ((INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
538 && TYPE_UNSIGNED (type))
539 (trunc_divmod @0 @1))))
540
541 /* 1 / X -> X == 1 for unsigned integer X.
542 1 / X -> X >= -1 && X <= 1 ? X : 0 for signed integer X.
543 But not for 1 / 0 so that we can get proper warnings and errors,
544 and not for 1-bit integers as they are edge cases better handled
545 elsewhere. */
546 (simplify
547 (trunc_div integer_onep@0 @1)
548 (if (INTEGRAL_TYPE_P (type)
549 && TYPE_PRECISION (type) > 1
550 && !integer_zerop (@1)
551 && (!flag_non_call_exceptions || tree_expr_nonzero_p (@1)))
552 (if (TYPE_UNSIGNED (type))
553 (convert (eq:boolean_type_node @1 { build_one_cst (type); }))
554 (with { tree utype = unsigned_type_for (type); }
555 (cond (le (plus (convert:utype @1) { build_one_cst (utype); })
556 { build_int_cst (utype, 2); })
557 @1 { build_zero_cst (type); })))))
558
559 /* Combine two successive divisions. Note that combining ceil_div
560 and floor_div is trickier and combining round_div even more so. */
561 (for div (trunc_div exact_div)
562 (simplify
563 (div (div@3 @0 INTEGER_CST@1) INTEGER_CST@2)
564 (with {
565 wi::overflow_type overflow;
566 wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@2),
567 TYPE_SIGN (type), &overflow);
568 }
569 (if (div == EXACT_DIV_EXPR
570 || optimize_successive_divisions_p (@2, @3))
571 (if (!overflow)
572 (div @0 { wide_int_to_tree (type, mul); })
573 (if (TYPE_UNSIGNED (type)
574 || mul != wi::min_value (TYPE_PRECISION (type), SIGNED))
575 { build_zero_cst (type); }))))))
576
577 /* Combine successive multiplications. Similar to above, but handling
578 overflow is different. */
579 (simplify
580 (mult (mult @0 INTEGER_CST@1) INTEGER_CST@2)
581 (with {
582 wi::overflow_type overflow;
583 wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@2),
584 TYPE_SIGN (type), &overflow);
585 }
586 /* Skip folding on overflow: the only special case is @1 * @2 == -INT_MIN,
587 otherwise undefined overflow implies that @0 must be zero. */
588 (if (!overflow || TYPE_OVERFLOW_WRAPS (type))
589 (mult @0 { wide_int_to_tree (type, mul); }))))
590
591 /* Similar to above, but there could be an extra add/sub between
592 successive multuiplications. */
593 (simplify
594 (mult (plus:s (mult:s@4 @0 INTEGER_CST@1) INTEGER_CST@2) INTEGER_CST@3)
595 (with {
596 bool overflowed = true;
597 wi::overflow_type ovf1, ovf2;
598 wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@3),
599 TYPE_SIGN (type), &ovf1);
600 wide_int add = wi::mul (wi::to_wide (@2), wi::to_wide (@3),
601 TYPE_SIGN (type), &ovf2);
602 if (TYPE_OVERFLOW_UNDEFINED (type))
603 {
604 #if GIMPLE
605 value_range vr0;
606 if (ovf1 == wi::OVF_NONE && ovf2 == wi::OVF_NONE
607 && get_global_range_query ()->range_of_expr (vr0, @4)
608 && !vr0.varying_p () && !vr0.undefined_p ())
609 {
610 wide_int wmin0 = vr0.lower_bound ();
611 wide_int wmax0 = vr0.upper_bound ();
612 wmin0 = wi::mul (wmin0, wi::to_wide (@3), TYPE_SIGN (type), &ovf1);
613 wmax0 = wi::mul (wmax0, wi::to_wide (@3), TYPE_SIGN (type), &ovf2);
614 if (ovf1 == wi::OVF_NONE && ovf2 == wi::OVF_NONE)
615 {
616 wi::add (wmin0, add, TYPE_SIGN (type), &ovf1);
617 wi::add (wmax0, add, TYPE_SIGN (type), &ovf2);
618 if (ovf1 == wi::OVF_NONE && ovf2 == wi::OVF_NONE)
619 overflowed = false;
620 }
621 }
622 #endif
623 }
624 else
625 overflowed = false;
626 }
627 /* Skip folding on overflow. */
628 (if (!overflowed)
629 (plus (mult @0 { wide_int_to_tree (type, mul); })
630 { wide_int_to_tree (type, add); }))))
631
632 /* Similar to above, but a multiplication between successive additions. */
633 (simplify
634 (plus (mult:s (plus:s @0 INTEGER_CST@1) INTEGER_CST@2) INTEGER_CST@3)
635 (with {
636 bool overflowed = true;
637 wi::overflow_type ovf1;
638 wi::overflow_type ovf2;
639 wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@2),
640 TYPE_SIGN (type), &ovf1);
641 wide_int add = wi::add (mul, wi::to_wide (@3),
642 TYPE_SIGN (type), &ovf2);
643 if (TYPE_OVERFLOW_UNDEFINED (type))
644 {
645 #if GIMPLE
646 value_range vr0;
647 if (ovf1 == wi::OVF_NONE && ovf2 == wi::OVF_NONE
648 && get_global_range_query ()->range_of_expr (vr0, @0)
649 && !vr0.varying_p () && !vr0.undefined_p ())
650 {
651 wide_int wmin0 = vr0.lower_bound ();
652 wide_int wmax0 = vr0.upper_bound ();
653 wmin0 = wi::mul (wmin0, wi::to_wide (@2), TYPE_SIGN (type), &ovf1);
654 wmax0 = wi::mul (wmax0, wi::to_wide (@2), TYPE_SIGN (type), &ovf2);
655 if (ovf1 == wi::OVF_NONE && ovf2 == wi::OVF_NONE)
656 {
657 wi::add (wmin0, mul, TYPE_SIGN (type), &ovf1);
658 wi::add (wmax0, mul, TYPE_SIGN (type), &ovf2);
659 if (ovf1 == wi::OVF_NONE && ovf2 == wi::OVF_NONE)
660 overflowed = false;
661 }
662 }
663 #endif
664 }
665 else
666 overflowed = false;
667 }
668 /* Skip folding on overflow. */
669 (if (!overflowed)
670 (plus (mult @0 @2) { wide_int_to_tree (type, add); }))))
671
672 /* Optimize A / A to 1.0 if we don't care about
673 NaNs or Infinities. */
674 (simplify
675 (rdiv @0 @0)
676 (if (FLOAT_TYPE_P (type)
677 && ! HONOR_NANS (type)
678 && ! HONOR_INFINITIES (type))
679 { build_one_cst (type); }))
680
681 /* Optimize -A / A to -1.0 if we don't care about
682 NaNs or Infinities. */
683 (simplify
684 (rdiv:C @0 (negate @0))
685 (if (FLOAT_TYPE_P (type)
686 && ! HONOR_NANS (type)
687 && ! HONOR_INFINITIES (type))
688 { build_minus_one_cst (type); }))
689
690 /* PR71078: x / abs(x) -> copysign (1.0, x) */
691 (simplify
692 (rdiv:C (convert? @0) (convert? (abs @0)))
693 (if (SCALAR_FLOAT_TYPE_P (type)
694 && ! HONOR_NANS (type)
695 && ! HONOR_INFINITIES (type))
696 (switch
697 (if (types_match (type, float_type_node))
698 (BUILT_IN_COPYSIGNF { build_one_cst (type); } (convert @0)))
699 (if (types_match (type, double_type_node))
700 (BUILT_IN_COPYSIGN { build_one_cst (type); } (convert @0)))
701 (if (types_match (type, long_double_type_node))
702 (BUILT_IN_COPYSIGNL { build_one_cst (type); } (convert @0))))))
703
704 /* In IEEE floating point, x/1 is not equivalent to x for snans. */
705 (simplify
706 (rdiv @0 real_onep)
707 (if (!tree_expr_maybe_signaling_nan_p (@0))
708 (non_lvalue @0)))
709
710 /* In IEEE floating point, x/-1 is not equivalent to -x for snans. */
711 (simplify
712 (rdiv @0 real_minus_onep)
713 (if (!tree_expr_maybe_signaling_nan_p (@0))
714 (negate @0)))
715
716 (if (flag_reciprocal_math)
717 /* Convert (A/B)/C to A/(B*C). */
718 (simplify
719 (rdiv (rdiv:s @0 @1) @2)
720 (rdiv @0 (mult @1 @2)))
721
722 /* Canonicalize x / (C1 * y) to (x * C2) / y. */
723 (simplify
724 (rdiv @0 (mult:s @1 REAL_CST@2))
725 (with
726 { tree tem = const_binop (RDIV_EXPR, type, build_one_cst (type), @2); }
727 (if (tem)
728 (rdiv (mult @0 { tem; } ) @1))))
729
730 /* Convert A/(B/C) to (A/B)*C */
731 (simplify
732 (rdiv @0 (rdiv:s @1 @2))
733 (mult (rdiv @0 @1) @2)))
734
735 /* Simplify x / (- y) to -x / y. */
736 (simplify
737 (rdiv @0 (negate @1))
738 (rdiv (negate @0) @1))
739
740 (if (flag_unsafe_math_optimizations)
741 /* Simplify (C / x op 0.0) to x op 0.0 for C != 0, C != Inf/Nan.
742 Since C / x may underflow to zero, do this only for unsafe math. */
743 (for op (lt le gt ge)
744 neg_op (gt ge lt le)
745 (simplify
746 (op (rdiv REAL_CST@0 @1) real_zerop@2)
747 (if (!HONOR_SIGNED_ZEROS (@1) && !HONOR_INFINITIES (@1))
748 (switch
749 (if (real_less (&dconst0, TREE_REAL_CST_PTR (@0)))
750 (op @1 @2))
751 /* For C < 0, use the inverted operator. */
752 (if (real_less (TREE_REAL_CST_PTR (@0), &dconst0))
753 (neg_op @1 @2)))))))
754
755 /* Optimize (X & (-A)) / A where A is a power of 2, to X >> log2(A) */
756 (for div (trunc_div ceil_div floor_div round_div exact_div)
757 (simplify
758 (div (convert? (bit_and @0 INTEGER_CST@1)) INTEGER_CST@2)
759 (if (integer_pow2p (@2)
760 && tree_int_cst_sgn (@2) > 0
761 && tree_nop_conversion_p (type, TREE_TYPE (@0))
762 && wi::to_wide (@2) + wi::to_wide (@1) == 0)
763 (rshift (convert @0)
764 { build_int_cst (integer_type_node,
765 wi::exact_log2 (wi::to_wide (@2))); }))))
766
767 /* If ARG1 is a constant, we can convert this to a multiply by the
768 reciprocal. This does not have the same rounding properties,
769 so only do this if -freciprocal-math. We can actually
770 always safely do it if ARG1 is a power of two, but it's hard to
771 tell if it is or not in a portable manner. */
772 (for cst (REAL_CST COMPLEX_CST VECTOR_CST)
773 (simplify
774 (rdiv @0 cst@1)
775 (if (optimize)
776 (if (flag_reciprocal_math
777 && !real_zerop (@1))
778 (with
779 { tree tem = const_binop (RDIV_EXPR, type, build_one_cst (type), @1); }
780 (if (tem)
781 (mult @0 { tem; } )))
782 (if (cst != COMPLEX_CST)
783 (with { tree inverse = exact_inverse (type, @1); }
784 (if (inverse)
785 (mult @0 { inverse; } ))))))))
786
787 (for mod (ceil_mod floor_mod round_mod trunc_mod)
788 /* 0 % X is always zero. */
789 (simplify
790 (mod integer_zerop@0 @1)
791 /* But not for 0 % 0 so that we can get the proper warnings and errors. */
792 (if (!integer_zerop (@1))
793 @0))
794 /* X % 1 is always zero. */
795 (simplify
796 (mod @0 integer_onep)
797 { build_zero_cst (type); })
798 /* X % -1 is zero. */
799 (simplify
800 (mod @0 integer_minus_onep@1)
801 (if (!TYPE_UNSIGNED (type))
802 { build_zero_cst (type); }))
803 /* X % X is zero. */
804 (simplify
805 (mod @0 @0)
806 /* But not for 0 % 0 so that we can get the proper warnings and errors. */
807 (if (!integer_zerop (@0))
808 { build_zero_cst (type); }))
809 /* (X % Y) % Y is just X % Y. */
810 (simplify
811 (mod (mod@2 @0 @1) @1)
812 @2)
813 /* From extract_muldiv_1: (X * C1) % C2 is zero if C1 is a multiple of C2. */
814 (simplify
815 (mod (mult @0 INTEGER_CST@1) INTEGER_CST@2)
816 (if (ANY_INTEGRAL_TYPE_P (type)
817 && TYPE_OVERFLOW_UNDEFINED (type)
818 && wi::multiple_of_p (wi::to_wide (@1), wi::to_wide (@2),
819 TYPE_SIGN (type)))
820 { build_zero_cst (type); }))
821 /* For (X % C) == 0, if X is signed and C is power of 2, use unsigned
822 modulo and comparison, since it is simpler and equivalent. */
823 (for cmp (eq ne)
824 (simplify
825 (cmp (mod @0 integer_pow2p@2) integer_zerop@1)
826 (if (!TYPE_UNSIGNED (TREE_TYPE (@0)))
827 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
828 (cmp (mod (convert:utype @0) (convert:utype @2)) (convert:utype @1)))))))
829
830 /* X % -C is the same as X % C. */
831 (simplify
832 (trunc_mod @0 INTEGER_CST@1)
833 (if (TYPE_SIGN (type) == SIGNED
834 && !TREE_OVERFLOW (@1)
835 && wi::neg_p (wi::to_wide (@1))
836 && !TYPE_OVERFLOW_TRAPS (type)
837 /* Avoid this transformation if C is INT_MIN, i.e. C == -C. */
838 && !sign_bit_p (@1, @1))
839 (trunc_mod @0 (negate @1))))
840
841 /* X % -Y is the same as X % Y. */
842 (simplify
843 (trunc_mod @0 (convert? (negate @1)))
844 (if (INTEGRAL_TYPE_P (type)
845 && !TYPE_UNSIGNED (type)
846 && !TYPE_OVERFLOW_TRAPS (type)
847 && tree_nop_conversion_p (type, TREE_TYPE (@1))
848 /* Avoid this transformation if X might be INT_MIN or
849 Y might be -1, because we would then change valid
850 INT_MIN % -(-1) into invalid INT_MIN % -1. */
851 && (expr_not_equal_to (@0, wi::to_wide (TYPE_MIN_VALUE (type)))
852 || expr_not_equal_to (@1, wi::minus_one (TYPE_PRECISION
853 (TREE_TYPE (@1))))))
854 (trunc_mod @0 (convert @1))))
855
856 /* X - (X / Y) * Y is the same as X % Y. */
857 (simplify
858 (minus (convert1? @0) (convert2? (mult:c (trunc_div @@0 @@1) @1)))
859 (if (INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
860 (convert (trunc_mod @0 @1))))
861
862 /* x * (1 + y / x) - y -> x - y % x */
863 (simplify
864 (minus (mult:cs @0 (plus:s (trunc_div:s @1 @0) integer_onep)) @1)
865 (if (INTEGRAL_TYPE_P (type))
866 (minus @0 (trunc_mod @1 @0))))
867
868 /* Optimize TRUNC_MOD_EXPR by a power of two into a BIT_AND_EXPR,
869 i.e. "X % C" into "X & (C - 1)", if X and C are positive.
870 Also optimize A % (C << N) where C is a power of 2,
871 to A & ((C << N) - 1).
872 Also optimize "A shift (B % C)", if C is a power of 2, to
873 "A shift (B & (C - 1))". SHIFT operation include "<<" and ">>"
874 and assume (B % C) is nonnegative as shifts negative values would
875 be UB. */
876 (match (power_of_two_cand @1)
877 INTEGER_CST@1)
878 (match (power_of_two_cand @1)
879 (lshift INTEGER_CST@1 @2))
880 (for mod (trunc_mod floor_mod)
881 (for shift (lshift rshift)
882 (simplify
883 (shift @0 (mod @1 (power_of_two_cand@2 @3)))
884 (if (integer_pow2p (@3) && tree_int_cst_sgn (@3) > 0)
885 (shift @0 (bit_and @1 (minus @2 { build_int_cst (TREE_TYPE (@2),
886 1); }))))))
887 (simplify
888 (mod @0 (convert? (power_of_two_cand@1 @2)))
889 (if ((TYPE_UNSIGNED (type) || tree_expr_nonnegative_p (@0))
890 /* Allow any integral conversions of the divisor, except
891 conversion from narrower signed to wider unsigned type
892 where if @1 would be negative power of two, the divisor
893 would not be a power of two. */
894 && INTEGRAL_TYPE_P (type)
895 && INTEGRAL_TYPE_P (TREE_TYPE (@1))
896 && (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@1))
897 || TYPE_UNSIGNED (TREE_TYPE (@1))
898 || !TYPE_UNSIGNED (type))
899 && integer_pow2p (@2) && tree_int_cst_sgn (@2) > 0)
900 (with { tree utype = TREE_TYPE (@1);
901 if (!TYPE_OVERFLOW_WRAPS (utype))
902 utype = unsigned_type_for (utype); }
903 (bit_and @0 (convert (minus (convert:utype @1)
904 { build_one_cst (utype); })))))))
905
906 /* Simplify (unsigned t * 2)/2 -> unsigned t & 0x7FFFFFFF. */
907 (simplify
908 (trunc_div (mult @0 integer_pow2p@1) @1)
909 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) && TYPE_UNSIGNED (TREE_TYPE (@0)))
910 (bit_and @0 { wide_int_to_tree
911 (type, wi::mask (TYPE_PRECISION (type)
912 - wi::exact_log2 (wi::to_wide (@1)),
913 false, TYPE_PRECISION (type))); })))
914
915 /* Simplify (unsigned t / 2) * 2 -> unsigned t & ~1. */
916 (simplify
917 (mult (trunc_div @0 integer_pow2p@1) @1)
918 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) && TYPE_UNSIGNED (TREE_TYPE (@0)))
919 (bit_and @0 (negate @1))))
920
921 /* Simplify (t * 2) / 2) -> t. */
922 (for div (trunc_div ceil_div floor_div round_div exact_div)
923 (simplify
924 (div (mult:c @0 @1) @1)
925 (if (ANY_INTEGRAL_TYPE_P (type))
926 (if (TYPE_OVERFLOW_UNDEFINED (type))
927 @0
928 #if GIMPLE
929 (with
930 {
931 bool overflowed = true;
932 value_range vr0, vr1;
933 if (INTEGRAL_TYPE_P (type)
934 && get_global_range_query ()->range_of_expr (vr0, @0)
935 && get_global_range_query ()->range_of_expr (vr1, @1)
936 && !vr0.varying_p () && !vr0.undefined_p ()
937 && !vr1.varying_p () && !vr1.undefined_p ())
938 {
939 wide_int wmin0 = vr0.lower_bound ();
940 wide_int wmax0 = vr0.upper_bound ();
941 wide_int wmin1 = vr1.lower_bound ();
942 wide_int wmax1 = vr1.upper_bound ();
943 /* If the multiplication can't overflow/wrap around, then
944 it can be optimized too. */
945 wi::overflow_type min_ovf, max_ovf;
946 wi::mul (wmin0, wmin1, TYPE_SIGN (type), &min_ovf);
947 wi::mul (wmax0, wmax1, TYPE_SIGN (type), &max_ovf);
948 if (min_ovf == wi::OVF_NONE && max_ovf == wi::OVF_NONE)
949 {
950 wi::mul (wmin0, wmax1, TYPE_SIGN (type), &min_ovf);
951 wi::mul (wmax0, wmin1, TYPE_SIGN (type), &max_ovf);
952 if (min_ovf == wi::OVF_NONE && max_ovf == wi::OVF_NONE)
953 overflowed = false;
954 }
955 }
956 }
957 (if (!overflowed)
958 @0))
959 #endif
960 ))))
961
962 #if GIMPLE
963 (for div (trunc_div exact_div)
964 /* Simplify (X + M*N) / N -> X / N + M. */
965 (simplify
966 (div (plus:c@4 @0 (mult:c@3 @1 @2)) @2)
967 (with {value_range vr0, vr1, vr2, vr3, vr4;}
968 (if (INTEGRAL_TYPE_P (type)
969 && get_range_query (cfun)->range_of_expr (vr1, @1)
970 && get_range_query (cfun)->range_of_expr (vr2, @2)
971 /* "N*M" doesn't overflow. */
972 && range_op_handler (MULT_EXPR).overflow_free_p (vr1, vr2)
973 && get_range_query (cfun)->range_of_expr (vr0, @0)
974 && get_range_query (cfun)->range_of_expr (vr3, @3)
975 /* "X+(N*M)" doesn't overflow. */
976 && range_op_handler (PLUS_EXPR).overflow_free_p (vr0, vr3)
977 && get_range_query (cfun)->range_of_expr (vr4, @4)
978 /* "X+N*M" is not with opposite sign as "X". */
979 && (TYPE_UNSIGNED (type)
980 || (vr0.nonnegative_p () && vr4.nonnegative_p ())
981 || (vr0.nonpositive_p () && vr4.nonpositive_p ())))
982 (plus (div @0 @2) @1))))
983
984 /* Simplify (X - M*N) / N -> X / N - M. */
985 (simplify
986 (div (minus@4 @0 (mult:c@3 @1 @2)) @2)
987 (with {value_range vr0, vr1, vr2, vr3, vr4;}
988 (if (INTEGRAL_TYPE_P (type)
989 && get_range_query (cfun)->range_of_expr (vr1, @1)
990 && get_range_query (cfun)->range_of_expr (vr2, @2)
991 /* "N * M" doesn't overflow. */
992 && range_op_handler (MULT_EXPR).overflow_free_p (vr1, vr2)
993 && get_range_query (cfun)->range_of_expr (vr0, @0)
994 && get_range_query (cfun)->range_of_expr (vr3, @3)
995 /* "X - (N*M)" doesn't overflow. */
996 && range_op_handler (MINUS_EXPR).overflow_free_p (vr0, vr3)
997 && get_range_query (cfun)->range_of_expr (vr4, @4)
998 /* "X-N*M" is not with opposite sign as "X". */
999 && (TYPE_UNSIGNED (type)
1000 || (vr0.nonnegative_p () && vr4.nonnegative_p ())
1001 || (vr0.nonpositive_p () && vr4.nonpositive_p ())))
1002 (minus (div @0 @2) @1)))))
1003
1004 /* Simplify
1005 (X + C) / N -> X / N + C / N where C is multiple of N.
1006 (X + C) >> N -> X >> N + C>>N if low N bits of C is 0. */
1007 (for op (trunc_div exact_div rshift)
1008 (simplify
1009 (op (plus@3 @0 INTEGER_CST@1) INTEGER_CST@2)
1010 (with
1011 {
1012 wide_int c = wi::to_wide (@1);
1013 wide_int n = wi::to_wide (@2);
1014 bool shift = op == RSHIFT_EXPR;
1015 #define plus_op1(v) (shift ? wi::rshift (v, n, TYPE_SIGN (type)) \
1016 : wi::div_trunc (v, n, TYPE_SIGN (type)))
1017 #define exact_mod(v) (shift ? wi::ctz (v) >= n.to_shwi () \
1018 : wi::multiple_of_p (v, n, TYPE_SIGN (type)))
1019 value_range vr0, vr1, vr3;
1020 }
1021 (if (INTEGRAL_TYPE_P (type)
1022 && get_range_query (cfun)->range_of_expr (vr0, @0))
1023 (if (exact_mod (c)
1024 && get_range_query (cfun)->range_of_expr (vr1, @1)
1025 /* "X+C" doesn't overflow. */
1026 && range_op_handler (PLUS_EXPR).overflow_free_p (vr0, vr1)
1027 && get_range_query (cfun)->range_of_expr (vr3, @3)
1028 /* "X+C" and "X" are not of opposite sign. */
1029 && (TYPE_UNSIGNED (type)
1030 || (vr0.nonnegative_p () && vr3.nonnegative_p ())
1031 || (vr0.nonpositive_p () && vr3.nonpositive_p ())))
1032 (plus (op @0 @2) { wide_int_to_tree (type, plus_op1 (c)); })
1033 (if (TYPE_UNSIGNED (type) && c.sign_mask () < 0
1034 && exact_mod (-c)
1035 /* unsigned "X-(-C)" doesn't underflow. */
1036 && wi::geu_p (vr0.lower_bound (), -c))
1037 (plus (op @0 @2) { wide_int_to_tree (type, -plus_op1 (-c)); })))))))
1038 #undef plus_op1
1039 #undef exact_mod
1040 #endif
1041
1042 /* (nop_outer_cast)-(inner_cast)var -> -(outer_cast)(var)
1043 if var is smaller in precision.
1044 This is always safe for both doing the negative in signed or unsigned
1045 as the value for undefined will not show up. */
1046 (simplify
1047 (convert (negate:s@1 (convert:s @0)))
1048 (if (INTEGRAL_TYPE_P (type)
1049 && tree_nop_conversion_p (type, TREE_TYPE (@1))
1050 && TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (@0)))
1051 (negate (convert @0))))
1052
1053 (for op (negate abs)
1054 /* Simplify cos(-x) and cos(|x|) -> cos(x). Similarly for cosh. */
1055 (for coss (COS COSH)
1056 (simplify
1057 (coss (op @0))
1058 (coss @0)))
1059 /* Simplify pow(-x, y) and pow(|x|,y) -> pow(x,y) if y is an even integer. */
1060 (for pows (POW)
1061 (simplify
1062 (pows (op @0) REAL_CST@1)
1063 (with { HOST_WIDE_INT n; }
1064 (if (real_isinteger (&TREE_REAL_CST (@1), &n) && (n & 1) == 0)
1065 (pows @0 @1)))))
1066 /* Likewise for powi. */
1067 (for pows (POWI)
1068 (simplify
1069 (pows (op @0) INTEGER_CST@1)
1070 (if ((wi::to_wide (@1) & 1) == 0)
1071 (pows @0 @1))))
1072 /* Strip negate and abs from both operands of hypot. */
1073 (for hypots (HYPOT)
1074 (simplify
1075 (hypots (op @0) @1)
1076 (hypots @0 @1))
1077 (simplify
1078 (hypots @0 (op @1))
1079 (hypots @0 @1)))
1080 /* copysign(-x, y) and copysign(abs(x), y) -> copysign(x, y). */
1081 (for copysigns (COPYSIGN_ALL)
1082 (simplify
1083 (copysigns (op @0) @1)
1084 (copysigns @0 @1))))
1085
1086 /* abs(x)*abs(x) -> x*x. Should be valid for all types. */
1087 (simplify
1088 (mult (abs@1 @0) @1)
1089 (mult @0 @0))
1090
1091 /* Convert absu(x)*absu(x) -> x*x. */
1092 (simplify
1093 (mult (absu@1 @0) @1)
1094 (mult (convert@2 @0) @2))
1095
1096 /* cos(copysign(x, y)) -> cos(x). Similarly for cosh. */
1097 (for coss (COS COSH)
1098 copysigns (COPYSIGN)
1099 (simplify
1100 (coss (copysigns @0 @1))
1101 (coss @0)))
1102
1103 /* pow(copysign(x, y), z) -> pow(x, z) if z is an even integer. */
1104 (for pows (POW)
1105 copysigns (COPYSIGN)
1106 (simplify
1107 (pows (copysigns @0 @2) REAL_CST@1)
1108 (with { HOST_WIDE_INT n; }
1109 (if (real_isinteger (&TREE_REAL_CST (@1), &n) && (n & 1) == 0)
1110 (pows @0 @1)))))
1111 /* Likewise for powi. */
1112 (for pows (POWI)
1113 copysigns (COPYSIGN)
1114 (simplify
1115 (pows (copysigns @0 @2) INTEGER_CST@1)
1116 (if ((wi::to_wide (@1) & 1) == 0)
1117 (pows @0 @1))))
1118
1119 (for hypots (HYPOT)
1120 copysigns (COPYSIGN)
1121 /* hypot(copysign(x, y), z) -> hypot(x, z). */
1122 (simplify
1123 (hypots (copysigns @0 @1) @2)
1124 (hypots @0 @2))
1125 /* hypot(x, copysign(y, z)) -> hypot(x, y). */
1126 (simplify
1127 (hypots @0 (copysigns @1 @2))
1128 (hypots @0 @1)))
1129
1130 /* copysign(x, CST) -> [-]abs (x). */
1131 (for copysigns (COPYSIGN_ALL)
1132 (simplify
1133 (copysigns @0 REAL_CST@1)
1134 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
1135 (negate (abs @0))
1136 (abs @0))))
1137
1138 /* copysign(copysign(x, y), z) -> copysign(x, z). */
1139 (for copysigns (COPYSIGN_ALL)
1140 (simplify
1141 (copysigns (copysigns @0 @1) @2)
1142 (copysigns @0 @2)))
1143
1144 /* copysign(x,y)*copysign(x,y) -> x*x. */
1145 (for copysigns (COPYSIGN_ALL)
1146 (simplify
1147 (mult (copysigns@2 @0 @1) @2)
1148 (mult @0 @0)))
1149
1150 /* ccos(-x) -> ccos(x). Similarly for ccosh. */
1151 (for ccoss (CCOS CCOSH)
1152 (simplify
1153 (ccoss (negate @0))
1154 (ccoss @0)))
1155
1156 /* cabs(-x) and cos(conj(x)) -> cabs(x). */
1157 (for ops (conj negate)
1158 (for cabss (CABS)
1159 (simplify
1160 (cabss (ops @0))
1161 (cabss @0))))
1162
1163 /* Fold (a * (1 << b)) into (a << b) */
1164 (simplify
1165 (mult:c @0 (convert? (lshift integer_onep@1 @2)))
1166 (if (! FLOAT_TYPE_P (type)
1167 && tree_nop_conversion_p (type, TREE_TYPE (@1)))
1168 (lshift @0 @2)))
1169
1170 /* Shifts by precision or greater result in zero. */
1171 (for shift (lshift rshift)
1172 (simplify
1173 (shift @0 uniform_integer_cst_p@1)
1174 (if ((GIMPLE || !sanitize_flags_p (SANITIZE_SHIFT_EXPONENT))
1175 /* Leave arithmetic right shifts of possibly negative values alone. */
1176 && (TYPE_UNSIGNED (type)
1177 || shift == LSHIFT_EXPR
1178 || tree_expr_nonnegative_p (@0))
1179 /* Use a signed compare to leave negative shift counts alone. */
1180 && wi::ges_p (wi::to_wide (uniform_integer_cst_p (@1)),
1181 element_precision (type)))
1182 { build_zero_cst (type); })))
1183
1184 /* Shifts by constants distribute over several binary operations,
1185 hence (X << C) + (Y << C) can be simplified to (X + Y) << C. */
1186 (for op (plus minus)
1187 (simplify
1188 (op (lshift:s @0 @1) (lshift:s @2 @1))
1189 (if (INTEGRAL_TYPE_P (type)
1190 && TYPE_OVERFLOW_WRAPS (type)
1191 && !TYPE_SATURATING (type))
1192 (lshift (op @0 @2) @1))))
1193
1194 (for op (bit_and bit_ior bit_xor)
1195 (simplify
1196 (op (lshift:s @0 @1) (lshift:s @2 @1))
1197 (if (INTEGRAL_TYPE_P (type))
1198 (lshift (op @0 @2) @1)))
1199 (simplify
1200 (op (rshift:s @0 @1) (rshift:s @2 @1))
1201 (if (INTEGRAL_TYPE_P (type))
1202 (rshift (op @0 @2) @1))))
1203
1204 /* Fold (1 << (C - x)) where C = precision(type) - 1
1205 into ((1 << C) >> x). */
1206 (simplify
1207 (lshift integer_onep@0 (minus@1 INTEGER_CST@2 @3))
1208 (if (INTEGRAL_TYPE_P (type)
1209 && wi::eq_p (wi::to_wide (@2), TYPE_PRECISION (type) - 1)
1210 && single_use (@1))
1211 (if (TYPE_UNSIGNED (type))
1212 (rshift (lshift @0 @2) @3)
1213 (with
1214 { tree utype = unsigned_type_for (type); }
1215 (convert (rshift (lshift (convert:utype @0) @2) @3))))))
1216
1217 /* Fold ((type)(a<0)) << SIGNBITOFA into ((type)a) & signbit. */
1218 (simplify
1219 (lshift (convert (lt @0 integer_zerop@1)) INTEGER_CST@2)
1220 (if (TYPE_SIGN (TREE_TYPE (@0)) == SIGNED
1221 && wi::eq_p (wi::to_wide (@2), TYPE_PRECISION (TREE_TYPE (@0)) - 1))
1222 (with { wide_int wone = wi::one (TYPE_PRECISION (type)); }
1223 (bit_and (convert @0)
1224 { wide_int_to_tree (type,
1225 wi::lshift (wone, wi::to_wide (@2))); }))))
1226
1227 /* Fold (-x >> C) into -(x > 0) where C = precision(type) - 1. */
1228 (for cst (INTEGER_CST VECTOR_CST)
1229 (simplify
1230 (rshift (negate:s @0) cst@1)
1231 (if (!TYPE_UNSIGNED (type)
1232 && TYPE_OVERFLOW_UNDEFINED (type))
1233 (with { tree stype = TREE_TYPE (@1);
1234 tree bt = truth_type_for (type);
1235 tree zeros = build_zero_cst (type);
1236 tree cst = NULL_TREE; }
1237 (switch
1238 /* Handle scalar case. */
1239 (if (INTEGRAL_TYPE_P (type)
1240 /* If we apply the rule to the scalar type before vectorization
1241 we will enforce the result of the comparison being a bool
1242 which will require an extra AND on the result that will be
1243 indistinguishable from when the user did actually want 0
1244 or 1 as the result so it can't be removed. */
1245 && canonicalize_math_after_vectorization_p ()
1246 && wi::eq_p (wi::to_wide (@1), TYPE_PRECISION (type) - 1))
1247 (negate (convert (gt @0 { zeros; }))))
1248 /* Handle vector case. */
1249 (if (VECTOR_INTEGER_TYPE_P (type)
1250 /* First check whether the target has the same mode for vector
1251 comparison results as it's operands do. */
1252 && TYPE_MODE (bt) == TYPE_MODE (type)
1253 /* Then check to see if the target is able to expand the comparison
1254 with the given type later on, otherwise we may ICE. */
1255 && expand_vec_cmp_expr_p (type, bt, GT_EXPR)
1256 && (cst = uniform_integer_cst_p (@1)) != NULL
1257 && wi::eq_p (wi::to_wide (cst), element_precision (type) - 1))
1258 (view_convert (gt:bt @0 { zeros; }))))))))
1259
1260 /* Fold (C1/X)*C2 into (C1*C2)/X. */
1261 (simplify
1262 (mult (rdiv@3 REAL_CST@0 @1) REAL_CST@2)
1263 (if (flag_associative_math
1264 && single_use (@3))
1265 (with
1266 { tree tem = const_binop (MULT_EXPR, type, @0, @2); }
1267 (if (tem)
1268 (rdiv { tem; } @1)))))
1269
1270 /* Simplify ~X & X as zero. */
1271 (simplify
1272 (bit_and (convert? @0) (convert? @1))
1273 (with { bool wascmp; }
1274 (if (types_match (TREE_TYPE (@0), TREE_TYPE (@1))
1275 && bitwise_inverted_equal_p (@0, @1, wascmp))
1276 { wascmp ? constant_boolean_node (false, type) : build_zero_cst (type); })))
1277
1278 /* PR71636: Transform x & ((1U << b) - 1) -> x & ~(~0U << b); */
1279 (simplify
1280 (bit_and:c @0 (plus:s (lshift:s integer_onep @1) integer_minus_onep))
1281 (if (TYPE_UNSIGNED (type))
1282 (bit_and @0 (bit_not (lshift { build_all_ones_cst (type); } @1)))))
1283
1284 (for bitop (bit_and bit_ior)
1285 cmp (eq ne)
1286 /* PR35691: Transform
1287 (x == 0 & y == 0) -> (x | typeof(x)(y)) == 0.
1288 (x != 0 | y != 0) -> (x | typeof(x)(y)) != 0. */
1289 (simplify
1290 (bitop (cmp @0 integer_zerop@2) (cmp @1 integer_zerop))
1291 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1292 && INTEGRAL_TYPE_P (TREE_TYPE (@1))
1293 && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
1294 (cmp (bit_ior @0 (convert @1)) @2)))
1295 /* Transform:
1296 (x == -1 & y == -1) -> (x & typeof(x)(y)) == -1.
1297 (x != -1 | y != -1) -> (x & typeof(x)(y)) != -1. */
1298 (simplify
1299 (bitop (cmp @0 integer_all_onesp@2) (cmp @1 integer_all_onesp))
1300 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1301 && INTEGRAL_TYPE_P (TREE_TYPE (@1))
1302 && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
1303 (cmp (bit_and @0 (convert @1)) @2))))
1304
1305 /* Fold (A & ~B) - (A & B) into (A ^ B) - B. */
1306 (simplify
1307 (minus (bit_and:cs @0 (bit_not @1)) (bit_and:cs @0 @1))
1308 (minus (bit_xor @0 @1) @1))
1309 (simplify
1310 (minus (bit_and:s @0 INTEGER_CST@2) (bit_and:s @0 INTEGER_CST@1))
1311 (if (~wi::to_wide (@2) == wi::to_wide (@1))
1312 (minus (bit_xor @0 @1) @1)))
1313
1314 /* Fold (A & B) - (A & ~B) into B - (A ^ B). */
1315 (simplify
1316 (minus (bit_and:cs @0 @1) (bit_and:cs @0 (bit_not @1)))
1317 (minus @1 (bit_xor @0 @1)))
1318
1319 /* Simplify (X & ~Y) |^+ (~X & Y) -> X ^ Y. */
1320 (for op (bit_ior bit_xor plus)
1321 (simplify
1322 (op (bit_and:c @0 @2) (bit_and:c @3 @1))
1323 (with { bool wascmp0, wascmp1; }
1324 (if (bitwise_inverted_equal_p (@2, @1, wascmp0)
1325 && bitwise_inverted_equal_p (@0, @3, wascmp1)
1326 && ((!wascmp0 && !wascmp1)
1327 || element_precision (type) == 1))
1328 (bit_xor @0 @1)))))
1329
1330 /* PR53979: Transform ((a ^ b) | a) -> (a | b) */
1331 (simplify
1332 (bit_ior:c (bit_xor:c @0 @1) @0)
1333 (bit_ior @0 @1))
1334
1335 /* (a & ~b) | (a ^ b) --> a ^ b */
1336 (simplify
1337 (bit_ior:c (bit_and:c @0 (bit_not @1)) (bit_xor:c@2 @0 @1))
1338 @2)
1339
1340 /* (a & ~b) ^ ~a --> ~(a & b) */
1341 (simplify
1342 (bit_xor:c (bit_and:cs @0 (bit_not @1)) (bit_not @0))
1343 (bit_not (bit_and @0 @1)))
1344
1345 /* (~a & b) ^ a --> (a | b) */
1346 (simplify
1347 (bit_xor:c (bit_and:cs (bit_not @0) @1) @0)
1348 (bit_ior @0 @1))
1349
1350 /* (a | b) & ~(a ^ b) --> a & b */
1351 (simplify
1352 (bit_and:c (bit_ior @0 @1) (bit_not (bit_xor:c @0 @1)))
1353 (bit_and @0 @1))
1354
1355 /* (a | b) & (a == b) --> a & b (boolean version of the above). */
1356 (simplify
1357 (bit_and:c (bit_ior @0 @1) (nop_convert? (eq:c @0 @1)))
1358 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1359 && TYPE_PRECISION (TREE_TYPE (@0)) == 1)
1360 (bit_and @0 @1)))
1361
1362 /* a | ~(a ^ b) --> a | ~b */
1363 (simplify
1364 (bit_ior:c @0 (bit_not:s (bit_xor:c @0 @1)))
1365 (bit_ior @0 (bit_not @1)))
1366
1367 /* a | (a == b) --> a | (b^1) (boolean version of the above). */
1368 (simplify
1369 (bit_ior:c @0 (nop_convert? (eq:c @0 @1)))
1370 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1371 && TYPE_PRECISION (TREE_TYPE (@0)) == 1)
1372 (bit_ior @0 (bit_xor @1 { build_one_cst (type); }))))
1373
1374 /* (a | b) | (a &^ b) --> a | b */
1375 (for op (bit_and bit_xor)
1376 (simplify
1377 (bit_ior:c (bit_ior@2 @0 @1) (op:c @0 @1))
1378 @2))
1379
1380 /* (a & b) | ~(a ^ b) --> ~(a ^ b) */
1381 (simplify
1382 (bit_ior:c (bit_and:c @0 @1) (bit_not@2 (bit_xor @0 @1)))
1383 @2)
1384
1385 /* (a & b) | (a == b) --> a == b */
1386 (simplify
1387 (bit_ior:c (bit_and:c @0 @1) (nop_convert?@2 (eq @0 @1)))
1388 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1389 && TYPE_PRECISION (TREE_TYPE (@0)) == 1)
1390 @2))
1391
1392 /* ~(~a & b) --> a | ~b */
1393 (simplify
1394 (bit_not (bit_and:cs (bit_not @0) @1))
1395 (bit_ior @0 (bit_not @1)))
1396
1397 /* ~(~a | b) --> a & ~b */
1398 (simplify
1399 (bit_not (bit_ior:cs (bit_not @0) @1))
1400 (bit_and @0 (bit_not @1)))
1401
1402 /* (a ^ b) & ((b ^ c) ^ a) --> (a ^ b) & ~c */
1403 (simplify
1404 (bit_and:c (bit_xor:c@3 @0 @1) (bit_xor:cs (bit_xor:cs @1 @2) @0))
1405 (bit_and @3 (bit_not @2)))
1406
1407 /* (a ^ b) | ((b ^ c) ^ a) --> (a ^ b) | c */
1408 (simplify
1409 (bit_ior:c (bit_xor:c@3 @0 @1) (bit_xor:c (bit_xor:c @1 @2) @0))
1410 (bit_ior @3 @2))
1411
1412 /* (~X | C) ^ D -> (X | C) ^ (~D ^ C) if (~D ^ C) can be simplified. */
1413 (simplify
1414 (bit_xor:c (bit_ior:cs (bit_not:s @0) @1) @2)
1415 (bit_xor (bit_ior @0 @1) (bit_xor! (bit_not! @2) @1)))
1416
1417 /* (~X & C) ^ D -> (X & C) ^ (D ^ C) if (D ^ C) can be simplified. */
1418 (simplify
1419 (bit_xor:c (bit_and:cs (bit_not:s @0) @1) @2)
1420 (bit_xor (bit_and @0 @1) (bit_xor! @2 @1)))
1421
1422 /* Simplify (~X & Y) to X ^ Y if we know that (X & ~Y) is 0. */
1423 (simplify
1424 (bit_and (bit_not SSA_NAME@0) INTEGER_CST@1)
1425 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1426 && wi::bit_and_not (get_nonzero_bits (@0), wi::to_wide (@1)) == 0)
1427 (bit_xor @0 @1)))
1428
1429 /* For constants M and N, if M == (1LL << cst) - 1 && (N & M) == M,
1430 ((A & N) + B) & M -> (A + B) & M
1431 Similarly if (N & M) == 0,
1432 ((A | N) + B) & M -> (A + B) & M
1433 and for - instead of + (or unary - instead of +)
1434 and/or ^ instead of |.
1435 If B is constant and (B & M) == 0, fold into A & M. */
1436 (for op (plus minus)
1437 (for bitop (bit_and bit_ior bit_xor)
1438 (simplify
1439 (bit_and (op:s (bitop:s@0 @3 INTEGER_CST@4) @1) INTEGER_CST@2)
1440 (with
1441 { tree pmop[2];
1442 tree utype = fold_bit_and_mask (TREE_TYPE (@0), @2, op, @0, bitop,
1443 @3, @4, @1, ERROR_MARK, NULL_TREE,
1444 NULL_TREE, pmop); }
1445 (if (utype)
1446 (convert (bit_and (op (convert:utype { pmop[0]; })
1447 (convert:utype { pmop[1]; }))
1448 (convert:utype @2))))))
1449 (simplify
1450 (bit_and (op:s @0 (bitop:s@1 @3 INTEGER_CST@4)) INTEGER_CST@2)
1451 (with
1452 { tree pmop[2];
1453 tree utype = fold_bit_and_mask (TREE_TYPE (@0), @2, op, @0, ERROR_MARK,
1454 NULL_TREE, NULL_TREE, @1, bitop, @3,
1455 @4, pmop); }
1456 (if (utype)
1457 (convert (bit_and (op (convert:utype { pmop[0]; })
1458 (convert:utype { pmop[1]; }))
1459 (convert:utype @2)))))))
1460 (simplify
1461 (bit_and (op:s @0 @1) INTEGER_CST@2)
1462 (with
1463 { tree pmop[2];
1464 tree utype = fold_bit_and_mask (TREE_TYPE (@0), @2, op, @0, ERROR_MARK,
1465 NULL_TREE, NULL_TREE, @1, ERROR_MARK,
1466 NULL_TREE, NULL_TREE, pmop); }
1467 (if (utype)
1468 (convert (bit_and (op (convert:utype { pmop[0]; })
1469 (convert:utype { pmop[1]; }))
1470 (convert:utype @2)))))))
1471 (for bitop (bit_and bit_ior bit_xor)
1472 (simplify
1473 (bit_and (negate:s (bitop:s@0 @2 INTEGER_CST@3)) INTEGER_CST@1)
1474 (with
1475 { tree pmop[2];
1476 tree utype = fold_bit_and_mask (TREE_TYPE (@0), @1, NEGATE_EXPR, @0,
1477 bitop, @2, @3, NULL_TREE, ERROR_MARK,
1478 NULL_TREE, NULL_TREE, pmop); }
1479 (if (utype)
1480 (convert (bit_and (negate (convert:utype { pmop[0]; }))
1481 (convert:utype @1)))))))
1482
1483 /* X % Y is smaller than Y. */
1484 (for cmp (lt ge)
1485 (simplify
1486 (cmp (trunc_mod @0 @1) @1)
1487 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
1488 { constant_boolean_node (cmp == LT_EXPR, type); })))
1489 (for cmp (gt le)
1490 (simplify
1491 (cmp @1 (trunc_mod @0 @1))
1492 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
1493 { constant_boolean_node (cmp == GT_EXPR, type); })))
1494
1495 /* x | ~0 -> ~0 */
1496 (simplify
1497 (bit_ior @0 integer_all_onesp@1)
1498 @1)
1499
1500 /* x | 0 -> x */
1501 (simplify
1502 (bit_ior @0 integer_zerop)
1503 @0)
1504
1505 /* x & 0 -> 0 */
1506 (simplify
1507 (bit_and @0 integer_zerop@1)
1508 @1)
1509
1510 /* ~x | x -> -1 */
1511 /* ~x ^ x -> -1 */
1512 (for op (bit_ior bit_xor)
1513 (simplify
1514 (op (convert? @0) (convert? @1))
1515 (with { bool wascmp; }
1516 (if (types_match (TREE_TYPE (@0), TREE_TYPE (@1))
1517 && bitwise_inverted_equal_p (@0, @1, wascmp))
1518 (convert
1519 { wascmp
1520 ? constant_boolean_node (true, type)
1521 : build_all_ones_cst (TREE_TYPE (@0)); })))))
1522
1523 /* x ^ x -> 0 */
1524 (simplify
1525 (bit_xor @0 @0)
1526 { build_zero_cst (type); })
1527
1528 /* Canonicalize X ^ ~0 to ~X. */
1529 (simplify
1530 (bit_xor @0 integer_all_onesp@1)
1531 (bit_not @0))
1532
1533 /* x & ~0 -> x */
1534 (simplify
1535 (bit_and @0 integer_all_onesp)
1536 (non_lvalue @0))
1537
1538 /* x & x -> x, x | x -> x */
1539 (for bitop (bit_and bit_ior)
1540 (simplify
1541 (bitop @0 @0)
1542 (non_lvalue @0)))
1543
1544 /* x & C -> x if we know that x & ~C == 0. */
1545 #if GIMPLE
1546 (simplify
1547 (bit_and SSA_NAME@0 INTEGER_CST@1)
1548 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1549 && wi::bit_and_not (get_nonzero_bits (@0), wi::to_wide (@1)) == 0)
1550 @0))
1551 /* x | C -> C if we know that x & ~C == 0. */
1552 (simplify
1553 (bit_ior SSA_NAME@0 INTEGER_CST@1)
1554 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1555 && wi::bit_and_not (get_nonzero_bits (@0), wi::to_wide (@1)) == 0)
1556 @1))
1557 #endif
1558
1559 /* ~(~X - Y) -> X + Y and ~(~X + Y) -> X - Y. */
1560 (simplify
1561 (bit_not (minus (bit_not @0) @1))
1562 (plus @0 @1))
1563 (simplify
1564 (bit_not (plus:c (bit_not @0) @1))
1565 (minus @0 @1))
1566 /* (~X - ~Y) -> Y - X. */
1567 (simplify
1568 (minus (bit_not @0) (bit_not @1))
1569 (if (!TYPE_OVERFLOW_SANITIZED (type))
1570 (with { tree utype = unsigned_type_for (type); }
1571 (convert (minus (convert:utype @1) (convert:utype @0))))))
1572
1573 /* ~(X - Y) -> ~X + Y. */
1574 (simplify
1575 (bit_not (minus:s @0 @1))
1576 (plus (bit_not @0) @1))
1577 (simplify
1578 (bit_not (plus:s @0 INTEGER_CST@1))
1579 (if ((INTEGRAL_TYPE_P (type)
1580 && TYPE_UNSIGNED (type))
1581 || (!TYPE_OVERFLOW_SANITIZED (type)
1582 && may_negate_without_overflow_p (@1)))
1583 (plus (bit_not @0) { const_unop (NEGATE_EXPR, type, @1); })))
1584
1585 #if GIMPLE
1586 /* ~X + Y -> (Y - X) - 1. */
1587 (simplify
1588 (plus:c (bit_not @0) @1)
1589 (if (ANY_INTEGRAL_TYPE_P (type)
1590 && TYPE_OVERFLOW_WRAPS (type)
1591 /* -1 - X is folded to ~X, so we'd recurse endlessly. */
1592 && !integer_all_onesp (@1))
1593 (plus (minus @1 @0) { build_minus_one_cst (type); })
1594 (if (INTEGRAL_TYPE_P (type)
1595 && TREE_CODE (@1) == INTEGER_CST
1596 && wi::to_wide (@1) != wi::min_value (TYPE_PRECISION (type),
1597 SIGNED))
1598 (minus (plus @1 { build_minus_one_cst (type); }) @0))))
1599 #endif
1600
1601 /* ~(X >> Y) -> ~X >> Y if ~X can be simplified. */
1602 (simplify
1603 (bit_not (rshift:s @0 @1))
1604 (if (!TYPE_UNSIGNED (TREE_TYPE (@0)))
1605 (rshift (bit_not! @0) @1)
1606 /* For logical right shifts, this is possible only if @0 doesn't
1607 have MSB set and the logical right shift is changed into
1608 arithmetic shift. */
1609 (if (INTEGRAL_TYPE_P (type)
1610 && !wi::neg_p (tree_nonzero_bits (@0)))
1611 (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
1612 (convert (rshift (bit_not! (convert:stype @0)) @1))))))
1613
1614 /* x + (x & 1) -> (x + 1) & ~1 */
1615 (simplify
1616 (plus:c @0 (bit_and:s @0 integer_onep@1))
1617 (bit_and (plus @0 @1) (bit_not @1)))
1618
1619 /* x & ~(x & y) -> x & ~y */
1620 /* x | ~(x | y) -> x | ~y */
1621 (for bitop (bit_and bit_ior)
1622 (simplify
1623 (bitop:c @0 (bit_not (bitop:cs @0 @1)))
1624 (bitop @0 (bit_not @1))))
1625
1626 /* (~x & y) | ~(x | y) -> ~x */
1627 (simplify
1628 (bit_ior:c (bit_and:c (bit_not@2 @0) @1) (bit_not (bit_ior:c @0 @1)))
1629 @2)
1630
1631 /* (x | y) ^ (x | ~y) -> ~x */
1632 (simplify
1633 (bit_xor:c (bit_ior:c @0 @1) (bit_ior:c @0 (bit_not @1)))
1634 (bit_not @0))
1635
1636 /* (x & y) | ~(x | y) -> ~(x ^ y) */
1637 (simplify
1638 (bit_ior:c (bit_and:s @0 @1) (bit_not:s (bit_ior:s @0 @1)))
1639 (bit_not (bit_xor @0 @1)))
1640
1641 /* (~x | y) ^ (x ^ y) -> x | ~y */
1642 (simplify
1643 (bit_xor:c (bit_ior:cs (bit_not @0) @1) (bit_xor:s @0 @1))
1644 (bit_ior @0 (bit_not @1)))
1645
1646 /* (x ^ y) | ~(x | y) -> ~(x & y) */
1647 (simplify
1648 (bit_ior:c (bit_xor:s @0 @1) (bit_not:s (bit_ior:s @0 @1)))
1649 (bit_not (bit_and @0 @1)))
1650
1651 /* (x & y) ^ (x | y) -> x ^ y */
1652 (simplify
1653 (bit_xor:c (bit_and @0 @1) (bit_ior @0 @1))
1654 (bit_xor @0 @1))
1655
1656 /* (x ^ y) ^ (x | y) -> x & y */
1657 (simplify
1658 (bit_xor:c (bit_xor @0 @1) (bit_ior @0 @1))
1659 (bit_and @0 @1))
1660
1661 /* (x & y) + (x ^ y) -> x | y */
1662 /* (x & y) | (x ^ y) -> x | y */
1663 /* (x & y) ^ (x ^ y) -> x | y */
1664 (for op (plus bit_ior bit_xor)
1665 (simplify
1666 (op:c (bit_and @0 @1) (bit_xor @0 @1))
1667 (bit_ior @0 @1)))
1668
1669 /* (x & y) + (x | y) -> x + y */
1670 (simplify
1671 (plus:c (bit_and @0 @1) (bit_ior @0 @1))
1672 (plus @0 @1))
1673
1674 /* (x + y) - (x | y) -> x & y */
1675 (simplify
1676 (minus (plus @0 @1) (bit_ior @0 @1))
1677 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1678 && !TYPE_SATURATING (type))
1679 (bit_and @0 @1)))
1680
1681 /* (x + y) - (x & y) -> x | y */
1682 (simplify
1683 (minus (plus @0 @1) (bit_and @0 @1))
1684 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1685 && !TYPE_SATURATING (type))
1686 (bit_ior @0 @1)))
1687
1688 /* (x | y) - y -> (x & ~y) */
1689 (simplify
1690 (minus (bit_ior:cs @0 @1) @1)
1691 (bit_and @0 (bit_not @1)))
1692
1693 /* (x | y) - (x ^ y) -> x & y */
1694 (simplify
1695 (minus (bit_ior @0 @1) (bit_xor @0 @1))
1696 (bit_and @0 @1))
1697
1698 /* (x | y) - (x & y) -> x ^ y */
1699 (simplify
1700 (minus (bit_ior @0 @1) (bit_and @0 @1))
1701 (bit_xor @0 @1))
1702
1703 /* (x | y) & ~(x & y) -> x ^ y */
1704 (simplify
1705 (bit_and:c (bit_ior @0 @1) (bit_not (bit_and @0 @1)))
1706 (bit_xor @0 @1))
1707
1708 /* (x | y) & (~x ^ y) -> x & y */
1709 (simplify
1710 (bit_and:c (bit_ior:c @0 @1) (bit_xor:c @1 @2))
1711 (with { bool wascmp; }
1712 (if (bitwise_inverted_equal_p (@0, @2, wascmp)
1713 && (!wascmp || element_precision (type) == 1))
1714 (bit_and @0 @1))))
1715
1716 /* (~x | y) & (x | ~y) -> ~(x ^ y) */
1717 (simplify
1718 (bit_and (bit_ior:cs (bit_not @0) @1) (bit_ior:cs @0 (bit_not @1)))
1719 (bit_not (bit_xor @0 @1)))
1720
1721 /* (~x | y) ^ (x | ~y) -> x ^ y */
1722 (simplify
1723 (bit_xor (bit_ior:c (bit_not @0) @1) (bit_ior:c @0 (bit_not @1)))
1724 (bit_xor @0 @1))
1725
1726 /* ((x & y) - (x | y)) - 1 -> ~(x ^ y) */
1727 (simplify
1728 (plus (nop_convert1? (minus@2 (nop_convert2? (bit_and:c @0 @1))
1729 (nop_convert2? (bit_ior @0 @1))))
1730 integer_all_onesp)
1731 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1732 && !TYPE_SATURATING (type) && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2))
1733 && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@2))
1734 && !TYPE_SATURATING (TREE_TYPE (@2)))
1735 (bit_not (convert (bit_xor @0 @1)))))
1736 (simplify
1737 (minus (nop_convert1? (plus@2 (nop_convert2? (bit_and:c @0 @1))
1738 integer_all_onesp))
1739 (nop_convert3? (bit_ior @0 @1)))
1740 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1741 && !TYPE_SATURATING (type) && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2))
1742 && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@2))
1743 && !TYPE_SATURATING (TREE_TYPE (@2)))
1744 (bit_not (convert (bit_xor @0 @1)))))
1745 (simplify
1746 (minus (nop_convert1? (bit_and @0 @1))
1747 (nop_convert2? (plus@2 (nop_convert3? (bit_ior:c @0 @1))
1748 integer_onep)))
1749 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1750 && !TYPE_SATURATING (type) && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2))
1751 && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@2))
1752 && !TYPE_SATURATING (TREE_TYPE (@2)))
1753 (bit_not (convert (bit_xor @0 @1)))))
1754
1755 /* ~x & ~y -> ~(x | y)
1756 ~x | ~y -> ~(x & y) */
1757 (for op (bit_and bit_ior)
1758 rop (bit_ior bit_and)
1759 (simplify
1760 (op (convert1? (bit_not @0)) (convert2? (bit_not @1)))
1761 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1762 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
1763 (bit_not (rop (convert @0) (convert @1))))))
1764
1765 /* If we are XORing or adding two BIT_AND_EXPR's, both of which are and'ing
1766 with a constant, and the two constants have no bits in common,
1767 we should treat this as a BIT_IOR_EXPR since this may produce more
1768 simplifications. */
1769 (for op (bit_xor plus)
1770 (simplify
1771 (op (convert1? (bit_and@4 @0 INTEGER_CST@1))
1772 (convert2? (bit_and@5 @2 INTEGER_CST@3)))
1773 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
1774 && tree_nop_conversion_p (type, TREE_TYPE (@2))
1775 && (wi::to_wide (@1) & wi::to_wide (@3)) == 0)
1776 (bit_ior (convert @4) (convert @5)))))
1777
1778 /* (X | Y) ^ X -> Y & ~ X*/
1779 (simplify
1780 (bit_xor:c (convert1? (bit_ior:c @@0 @1)) (convert2? @0))
1781 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1782 (convert (bit_and @1 (bit_not @0)))))
1783
1784 /* (~X | Y) ^ X -> ~(X & Y). */
1785 (simplify
1786 (bit_xor:c (nop_convert1? (bit_ior:c (nop_convert2? (bit_not @0)) @1)) @2)
1787 (if (bitwise_equal_p (@0, @2))
1788 (convert (bit_not (bit_and @0 (convert @1))))))
1789
1790 /* Convert ~X ^ ~Y to X ^ Y. */
1791 (simplify
1792 (bit_xor (convert1? (bit_not @0)) (convert2? (bit_not @1)))
1793 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1794 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
1795 (bit_xor (convert @0) (convert @1))))
1796
1797 /* Convert ~X ^ C to X ^ ~C. */
1798 (simplify
1799 (bit_xor (convert? (bit_not @0)) INTEGER_CST@1)
1800 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1801 (bit_xor (convert @0) (bit_not @1))))
1802
1803 /* Fold (X & Y) ^ Y and (X ^ Y) & Y as ~X & Y. */
1804 (for opo (bit_and bit_xor)
1805 opi (bit_xor bit_and)
1806 (simplify
1807 (opo:c (opi:cs @0 @1) @1)
1808 (bit_and (bit_not @0) @1)))
1809
1810 /* Given a bit-wise operation CODE applied to ARG0 and ARG1, see if both
1811 operands are another bit-wise operation with a common input. If so,
1812 distribute the bit operations to save an operation and possibly two if
1813 constants are involved. For example, convert
1814 (A | B) & (A | C) into A | (B & C)
1815 Further simplification will occur if B and C are constants. */
1816 (for op (bit_and bit_ior bit_xor)
1817 rop (bit_ior bit_and bit_and)
1818 (simplify
1819 (op (convert? (rop:c @@0 @1)) (convert? (rop:c @0 @2)))
1820 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1821 && tree_nop_conversion_p (type, TREE_TYPE (@2)))
1822 (rop (convert @0) (op (convert @1) (convert @2))))))
1823
1824 /* Some simple reassociation for bit operations, also handled in reassoc. */
1825 /* (X & Y) & Y -> X & Y
1826 (X | Y) | Y -> X | Y */
1827 (for op (bit_and bit_ior)
1828 (simplify
1829 (op:c (convert1?@2 (op:c @0 @@1)) (convert2? @1))
1830 @2))
1831 /* (X ^ Y) ^ Y -> X */
1832 (simplify
1833 (bit_xor:c (convert1? (bit_xor:c @0 @@1)) (convert2? @1))
1834 (convert @0))
1835 /* (X & Y) & (X & Z) -> (X & Y) & Z
1836 (X | Y) | (X | Z) -> (X | Y) | Z */
1837 (for op (bit_and bit_ior)
1838 (simplify
1839 (op (convert1?@3 (op:c@4 @0 @1)) (convert2?@5 (op:c@6 @0 @2)))
1840 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1841 && tree_nop_conversion_p (type, TREE_TYPE (@2)))
1842 (if (single_use (@5) && single_use (@6))
1843 (op @3 (convert @2))
1844 (if (single_use (@3) && single_use (@4))
1845 (op (convert @1) @5))))))
1846 /* (X ^ Y) ^ (X ^ Z) -> Y ^ Z */
1847 (simplify
1848 (bit_xor (convert1? (bit_xor:c @0 @1)) (convert2? (bit_xor:c @0 @2)))
1849 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1850 && tree_nop_conversion_p (type, TREE_TYPE (@2)))
1851 (bit_xor (convert @1) (convert @2))))
1852
1853 /* Convert abs (abs (X)) into abs (X).
1854 also absu (absu (X)) into absu (X). */
1855 (simplify
1856 (abs (abs@1 @0))
1857 @1)
1858
1859 (simplify
1860 (absu (convert@2 (absu@1 @0)))
1861 (if (tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@1)))
1862 @1))
1863
1864 /* Convert abs[u] (-X) -> abs[u] (X). */
1865 (simplify
1866 (abs (negate @0))
1867 (abs @0))
1868
1869 (simplify
1870 (absu (negate @0))
1871 (absu @0))
1872
1873 /* Convert abs[u] (X) where X is nonnegative -> (X). */
1874 (simplify
1875 (abs tree_expr_nonnegative_p@0)
1876 @0)
1877
1878 (simplify
1879 (absu tree_expr_nonnegative_p@0)
1880 (convert @0))
1881
1882 /* Simplify (-(X < 0) | 1) * X into abs (X) or absu(X). */
1883 (simplify
1884 (mult:c (nop_convert1?
1885 (bit_ior (nop_convert2? (negate (convert? (lt @0 integer_zerop))))
1886 integer_onep))
1887 (nop_convert3? @0))
1888 (if (INTEGRAL_TYPE_P (type)
1889 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
1890 && !TYPE_UNSIGNED (TREE_TYPE (@0)))
1891 (if (TYPE_UNSIGNED (type))
1892 (absu @0)
1893 (abs @0)
1894 )
1895 )
1896 )
1897
1898 /* A few cases of fold-const.cc negate_expr_p predicate. */
1899 (match negate_expr_p
1900 INTEGER_CST
1901 (if ((INTEGRAL_TYPE_P (type)
1902 && TYPE_UNSIGNED (type))
1903 || (!TYPE_OVERFLOW_SANITIZED (type)
1904 && may_negate_without_overflow_p (t)))))
1905 (match negate_expr_p
1906 FIXED_CST)
1907 (match negate_expr_p
1908 (negate @0)
1909 (if (!TYPE_OVERFLOW_SANITIZED (type))))
1910 (match negate_expr_p
1911 REAL_CST
1912 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (t)))))
1913 /* VECTOR_CST handling of non-wrapping types would recurse in unsupported
1914 ways. */
1915 (match negate_expr_p
1916 VECTOR_CST
1917 (if (FLOAT_TYPE_P (TREE_TYPE (type)) || TYPE_OVERFLOW_WRAPS (type))))
1918 (match negate_expr_p
1919 (minus @0 @1)
1920 (if ((ANY_INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_WRAPS (type))
1921 || (FLOAT_TYPE_P (type)
1922 && !HONOR_SIGN_DEPENDENT_ROUNDING (type)
1923 && !HONOR_SIGNED_ZEROS (type)))))
1924
1925 /* (-A) * (-B) -> A * B */
1926 (simplify
1927 (mult:c (convert1? (negate @0)) (convert2? negate_expr_p@1))
1928 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
1929 && tree_nop_conversion_p (type, TREE_TYPE (@1)))
1930 (mult (convert @0) (convert (negate @1)))))
1931
1932 /* -(A + B) -> (-B) - A. */
1933 (simplify
1934 (negate (plus:c @0 negate_expr_p@1))
1935 (if (!HONOR_SIGN_DEPENDENT_ROUNDING (type)
1936 && !HONOR_SIGNED_ZEROS (type))
1937 (minus (negate @1) @0)))
1938
1939 /* -(A - B) -> B - A. */
1940 (simplify
1941 (negate (minus @0 @1))
1942 (if ((ANY_INTEGRAL_TYPE_P (type) && !TYPE_OVERFLOW_SANITIZED (type))
1943 || (FLOAT_TYPE_P (type)
1944 && !HONOR_SIGN_DEPENDENT_ROUNDING (type)
1945 && !HONOR_SIGNED_ZEROS (type)))
1946 (minus @1 @0)))
1947 (simplify
1948 (negate (pointer_diff @0 @1))
1949 (if (TYPE_OVERFLOW_UNDEFINED (type))
1950 (pointer_diff @1 @0)))
1951
1952 /* A - B -> A + (-B) if B is easily negatable. */
1953 (simplify
1954 (minus @0 negate_expr_p@1)
1955 (if (!FIXED_POINT_TYPE_P (type))
1956 (plus @0 (negate @1))))
1957
1958 /* 1 - a is a ^ 1 if a had a bool range. */
1959 /* This is only enabled for gimple as sometimes
1960 cfun is not set for the function which contains
1961 the SSA_NAME (e.g. while IPA passes are happening,
1962 fold might be called). */
1963 (simplify
1964 (minus integer_onep@0 SSA_NAME@1)
1965 (if (INTEGRAL_TYPE_P (type)
1966 && ssa_name_has_boolean_range (@1))
1967 (bit_xor @1 @0)))
1968
1969 /* Other simplifications of negation (c.f. fold_negate_expr_1). */
1970 (simplify
1971 (negate (mult:c@0 @1 negate_expr_p@2))
1972 (if (! TYPE_UNSIGNED (type)
1973 && ! HONOR_SIGN_DEPENDENT_ROUNDING (type)
1974 && single_use (@0))
1975 (mult @1 (negate @2))))
1976
1977 (simplify
1978 (negate (rdiv@0 @1 negate_expr_p@2))
1979 (if (! HONOR_SIGN_DEPENDENT_ROUNDING (type)
1980 && single_use (@0))
1981 (rdiv @1 (negate @2))))
1982
1983 (simplify
1984 (negate (rdiv@0 negate_expr_p@1 @2))
1985 (if (! HONOR_SIGN_DEPENDENT_ROUNDING (type)
1986 && single_use (@0))
1987 (rdiv (negate @1) @2)))
1988
1989 /* Fold -((int)x >> (prec - 1)) into (unsigned)x >> (prec - 1). */
1990 (simplify
1991 (negate (convert? (rshift @0 INTEGER_CST@1)))
1992 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
1993 && wi::to_wide (@1) == element_precision (type) - 1)
1994 (with { tree stype = TREE_TYPE (@0);
1995 tree ntype = TYPE_UNSIGNED (stype) ? signed_type_for (stype)
1996 : unsigned_type_for (stype); }
1997 (if (VECTOR_TYPE_P (type))
1998 (view_convert (rshift (view_convert:ntype @0) @1))
1999 (convert (rshift (convert:ntype @0) @1))))))
2000
2001 /* Try to fold (type) X op CST -> (type) (X op ((type-x) CST))
2002 when profitable.
2003 For bitwise binary operations apply operand conversions to the
2004 binary operation result instead of to the operands. This allows
2005 to combine successive conversions and bitwise binary operations.
2006 We combine the above two cases by using a conditional convert. */
2007 (for bitop (bit_and bit_ior bit_xor)
2008 (simplify
2009 (bitop (convert@2 @0) (convert?@3 @1))
2010 (if (((TREE_CODE (@1) == INTEGER_CST
2011 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
2012 && (int_fits_type_p (@1, TREE_TYPE (@0))
2013 || tree_nop_conversion_p (TREE_TYPE (@0), type)))
2014 || types_match (@0, @1))
2015 && !POINTER_TYPE_P (TREE_TYPE (@0))
2016 && !VECTOR_TYPE_P (TREE_TYPE (@0))
2017 && TREE_CODE (TREE_TYPE (@0)) != OFFSET_TYPE
2018 /* ??? This transform conflicts with fold-const.cc doing
2019 Convert (T)(x & c) into (T)x & (T)c, if c is an integer
2020 constants (if x has signed type, the sign bit cannot be set
2021 in c). This folds extension into the BIT_AND_EXPR.
2022 Restrict it to GIMPLE to avoid endless recursions. */
2023 && (bitop != BIT_AND_EXPR || GIMPLE)
2024 && (/* That's a good idea if the conversion widens the operand, thus
2025 after hoisting the conversion the operation will be narrower.
2026 It is also a good if the conversion is a nop as moves the
2027 conversion to one side; allowing for combining of the conversions. */
2028 TYPE_PRECISION (TREE_TYPE (@0)) < TYPE_PRECISION (type)
2029 /* The conversion check for being a nop can only be done at the gimple
2030 level as fold_binary has some re-association code which can conflict
2031 with this if there is a "constant" which is not a full INTEGER_CST. */
2032 || (GIMPLE && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (type))
2033 /* It's also a good idea if the conversion is to a non-integer
2034 mode. */
2035 || GET_MODE_CLASS (TYPE_MODE (type)) != MODE_INT
2036 /* Or if the precision of TO is not the same as the precision
2037 of its mode. */
2038 || !type_has_mode_precision_p (type)
2039 /* In GIMPLE, getting rid of 2 conversions for one new results
2040 in smaller IL. */
2041 || (GIMPLE
2042 && TREE_CODE (@1) != INTEGER_CST
2043 && tree_nop_conversion_p (type, TREE_TYPE (@0))
2044 && single_use (@2)
2045 && single_use (@3))))
2046 (convert (bitop @0 (convert @1)))))
2047 /* In GIMPLE, getting rid of 2 conversions for one new results
2048 in smaller IL. */
2049 (simplify
2050 (convert (bitop:cs@2 (nop_convert:s @0) @1))
2051 (if (GIMPLE
2052 && TREE_CODE (@1) != INTEGER_CST
2053 && tree_nop_conversion_p (type, TREE_TYPE (@2))
2054 && types_match (type, @0)
2055 && !POINTER_TYPE_P (TREE_TYPE (@0))
2056 && TREE_CODE (TREE_TYPE (@0)) != OFFSET_TYPE)
2057 (bitop @0 (convert @1)))))
2058
2059 (for bitop (bit_and bit_ior)
2060 rbitop (bit_ior bit_and)
2061 /* (x | y) & x -> x */
2062 /* (x & y) | x -> x */
2063 (simplify
2064 (bitop:c (rbitop:c @0 @1) @0)
2065 @0)
2066 /* (~x | y) & x -> x & y */
2067 /* (~x & y) | x -> x | y */
2068 (simplify
2069 (bitop:c (rbitop:c @2 @1) @0)
2070 (with { bool wascmp; }
2071 (if (bitwise_inverted_equal_p (@0, @2, wascmp)
2072 && (!wascmp || element_precision (type) == 1))
2073 (bitop @0 @1))))
2074 /* (x | y) & (x & z) -> (x & z) */
2075 /* (x & y) | (x | z) -> (x | z) */
2076 (simplify
2077 (bitop:c (rbitop:c @0 @1) (bitop:c@3 @0 @2))
2078 @3)
2079 /* (x | c) & ~(y | c) -> x & ~(y | c) */
2080 /* (x & c) | ~(y & c) -> x | ~(y & c) */
2081 (simplify
2082 (bitop:c (rbitop:c @0 @1) (bit_not@3 (rbitop:c @1 @2)))
2083 (bitop @0 @3))
2084 /* x & ~(y | x) -> 0 */
2085 /* x | ~(y & x) -> -1 */
2086 (simplify
2087 (bitop:c @0 (bit_not (rbitop:c @0 @1)))
2088 (if (bitop == BIT_AND_EXPR)
2089 { build_zero_cst (type); }
2090 { build_minus_one_cst (type); })))
2091
2092 /* ((x | y) & z) | x -> (z & y) | x
2093 ((x ^ y) & z) | x -> (z & y) | x */
2094 (for op (bit_ior bit_xor)
2095 (simplify
2096 (bit_ior:c (nop_convert1?:s
2097 (bit_and:cs (nop_convert2?:s (op:cs @0 @1)) @2)) @3)
2098 (if (bitwise_equal_p (@0, @3))
2099 (convert (bit_ior (bit_and @1 (convert @2)) (convert @0))))))
2100
2101 /* (x | CST1) & CST2 -> (x & CST2) | (CST1 & CST2) */
2102 (simplify
2103 (bit_and (bit_ior @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
2104 (bit_ior (bit_and @0 @2) (bit_and! @1 @2)))
2105
2106 /* Combine successive equal operations with constants. */
2107 (for bitop (bit_and bit_ior bit_xor)
2108 (simplify
2109 (bitop (bitop @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
2110 (if (!CONSTANT_CLASS_P (@0))
2111 /* This is the canonical form regardless of whether (bitop @1 @2) can be
2112 folded to a constant. */
2113 (bitop @0 (bitop! @1 @2))
2114 /* In this case we have three constants and (bitop @0 @1) doesn't fold
2115 to a constant. This can happen if @0 or @1 is a POLY_INT_CST and if
2116 the values involved are such that the operation can't be decided at
2117 compile time. Try folding one of @0 or @1 with @2 to see whether
2118 that combination can be decided at compile time.
2119
2120 Keep the existing form if both folds fail, to avoid endless
2121 oscillation. */
2122 (with { tree cst1 = const_binop (bitop, type, @0, @2); }
2123 (if (cst1)
2124 (bitop @1 { cst1; })
2125 (with { tree cst2 = const_binop (bitop, type, @1, @2); }
2126 (if (cst2)
2127 (bitop @0 { cst2; }))))))))
2128
2129 /* Try simple folding for X op !X, and X op X with the help
2130 of the truth_valued_p and logical_inverted_value predicates. */
2131 (match truth_valued_p
2132 @0
2133 (if (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1)))
2134 (for op (tcc_comparison truth_and truth_andif truth_or truth_orif truth_xor)
2135 (match truth_valued_p
2136 (op @0 @1)))
2137 (match truth_valued_p
2138 (truth_not @0))
2139
2140 (match (logical_inverted_value @0)
2141 (truth_not @0))
2142 (match (logical_inverted_value @0)
2143 (bit_not truth_valued_p@0))
2144 (match (logical_inverted_value @0)
2145 (eq @0 integer_zerop))
2146 (match (logical_inverted_value @0)
2147 (ne truth_valued_p@0 integer_truep))
2148 (match (logical_inverted_value @0)
2149 (bit_xor truth_valued_p@0 integer_truep))
2150
2151 /* X & !X -> 0. */
2152 (simplify
2153 (bit_and:c @0 (logical_inverted_value @0))
2154 { build_zero_cst (type); })
2155 /* X | !X and X ^ !X -> 1, , if X is truth-valued. */
2156 (for op (bit_ior bit_xor)
2157 (simplify
2158 (op:c truth_valued_p@0 (logical_inverted_value @0))
2159 { constant_boolean_node (true, type); }))
2160 /* X ==/!= !X is false/true. */
2161 (for op (eq ne)
2162 (simplify
2163 (op:c truth_valued_p@0 (logical_inverted_value @0))
2164 { constant_boolean_node (op == NE_EXPR ? true : false, type); }))
2165
2166 /* ~~x -> x */
2167 (simplify
2168 (bit_not (bit_not @0))
2169 @0)
2170
2171 /* zero_one_valued_p will match when a value is known to be either
2172 0 or 1 including constants 0 or 1.
2173 Signed 1-bits includes -1 so they cannot match here. */
2174 (match zero_one_valued_p
2175 @0
2176 (if (INTEGRAL_TYPE_P (type)
2177 && (TYPE_UNSIGNED (type)
2178 || TYPE_PRECISION (type) > 1)
2179 && wi::leu_p (tree_nonzero_bits (@0), 1))))
2180 (match zero_one_valued_p
2181 truth_valued_p@0
2182 (if (INTEGRAL_TYPE_P (type)
2183 && (TYPE_UNSIGNED (type)
2184 || TYPE_PRECISION (type) > 1))))
2185
2186 /* Transform { 0 or 1 } * { 0 or 1 } into { 0 or 1 } & { 0 or 1 }. */
2187 (simplify
2188 (mult zero_one_valued_p@0 zero_one_valued_p@1)
2189 (if (INTEGRAL_TYPE_P (type))
2190 (bit_and @0 @1)))
2191
2192 (for cmp (tcc_comparison)
2193 icmp (inverted_tcc_comparison)
2194 /* Fold (((a < b) & c) | ((a >= b) & d)) into (a < b ? c : d) & 1. */
2195 (simplify
2196 (bit_ior
2197 (bit_and:c (convert? (cmp@0 @01 @02)) @3)
2198 (bit_and:c (convert? (icmp@4 @01 @02)) @5))
2199 (if (INTEGRAL_TYPE_P (type)
2200 && invert_tree_comparison (cmp, HONOR_NANS (@01)) == icmp
2201 /* The scalar version has to be canonicalized after vectorization
2202 because it makes unconditional loads conditional ones, which
2203 means we lose vectorization because the loads may trap. */
2204 && canonicalize_math_after_vectorization_p ())
2205 (bit_and (cond @0 @3 @5) { build_one_cst (type); })))
2206
2207 /* Fold ((-(a < b) & c) | (-(a >= b) & d)) into a < b ? c : d. This is
2208 canonicalized further and we recognize the conditional form:
2209 (a < b ? c : 0) | (a >= b ? d : 0) into a < b ? c : d. */
2210 (simplify
2211 (bit_ior
2212 (cond (cmp@0 @01 @02) @3 zerop)
2213 (cond (icmp@4 @01 @02) @5 zerop))
2214 (if (INTEGRAL_TYPE_P (type)
2215 && invert_tree_comparison (cmp, HONOR_NANS (@01)) == icmp
2216 /* The scalar version has to be canonicalized after vectorization
2217 because it makes unconditional loads conditional ones, which
2218 means we lose vectorization because the loads may trap. */
2219 && canonicalize_math_after_vectorization_p ())
2220 (cond @0 @3 @5)))
2221
2222 /* Vector Fold (((a < b) & c) | ((a >= b) & d)) into a < b ? c : d.
2223 and ((~(a < b) & c) | (~(a >= b) & d)) into a < b ? c : d. */
2224 (simplify
2225 (bit_ior
2226 (bit_and:c (vec_cond:s (cmp@0 @6 @7) @4 @5) @2)
2227 (bit_and:c (vec_cond:s (icmp@1 @6 @7) @4 @5) @3))
2228 (if (integer_zerop (@5)
2229 && invert_tree_comparison (cmp, HONOR_NANS (@6)) == icmp)
2230 (switch
2231 (if (integer_onep (@4))
2232 (bit_and (vec_cond @0 @2 @3) @4))
2233 (if (integer_minus_onep (@4))
2234 (vec_cond @0 @2 @3)))
2235 (if (integer_zerop (@4)
2236 && invert_tree_comparison (cmp, HONOR_NANS (@6)) == icmp)
2237 (switch
2238 (if (integer_onep (@5))
2239 (bit_and (vec_cond @0 @3 @2) @5))
2240 (if (integer_minus_onep (@5))
2241 (vec_cond @0 @3 @2))))))
2242
2243 /* Scalar Vectorized Fold ((-(a < b) & c) | (-(a >= b) & d))
2244 into a < b ? d : c. */
2245 (simplify
2246 (bit_ior
2247 (vec_cond:s (cmp@0 @4 @5) @2 integer_zerop)
2248 (vec_cond:s (icmp@1 @4 @5) @3 integer_zerop))
2249 (if (invert_tree_comparison (cmp, HONOR_NANS (@4)) == icmp)
2250 (vec_cond @0 @2 @3))))
2251
2252 /* Transform X & -Y into X * Y when Y is { 0 or 1 }. */
2253 (simplify
2254 (bit_and:c (convert? (negate zero_one_valued_p@0)) @1)
2255 (if (INTEGRAL_TYPE_P (type)
2256 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
2257 && TREE_CODE (TREE_TYPE (@0)) != BOOLEAN_TYPE
2258 /* Sign extending of the neg or a truncation of the neg
2259 is needed. */
2260 && (!TYPE_UNSIGNED (TREE_TYPE (@0))
2261 || TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))))
2262 (mult (convert @0) @1)))
2263
2264 /* Narrow integer multiplication by a zero_one_valued_p operand.
2265 Multiplication by [0,1] is guaranteed not to overflow. */
2266 (simplify
2267 (convert (mult@0 zero_one_valued_p@1 INTEGER_CST@2))
2268 (if (INTEGRAL_TYPE_P (type)
2269 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
2270 && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (@0)))
2271 (mult (convert @1) (convert @2))))
2272
2273 /* (X << C) != 0 can be simplified to X, when C is zero_one_valued_p.
2274 Check that the shift is well-defined (C is less than TYPE_PRECISION)
2275 as some targets (such as x86's SSE) may return zero for larger C. */
2276 (simplify
2277 (ne (lshift zero_one_valued_p@0 INTEGER_CST@1) integer_zerop@2)
2278 (if (tree_fits_shwi_p (@1)
2279 && tree_to_shwi (@1) > 0
2280 && tree_to_shwi (@1) < TYPE_PRECISION (TREE_TYPE (@0)))
2281 (convert @0)))
2282
2283 /* (X << C) == 0 can be simplified to X == 0, when C is zero_one_valued_p.
2284 Check that the shift is well-defined (C is less than TYPE_PRECISION)
2285 as some targets (such as x86's SSE) may return zero for larger C. */
2286 (simplify
2287 (eq (lshift zero_one_valued_p@0 INTEGER_CST@1) integer_zerop@2)
2288 (if (tree_fits_shwi_p (@1)
2289 && tree_to_shwi (@1) > 0
2290 && tree_to_shwi (@1) < TYPE_PRECISION (TREE_TYPE (@0)))
2291 (eq @0 @2)))
2292
2293 /* Convert ~ (-A) to A - 1. */
2294 (simplify
2295 (bit_not (convert? (negate @0)))
2296 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
2297 || !TYPE_UNSIGNED (TREE_TYPE (@0)))
2298 (convert (minus @0 { build_each_one_cst (TREE_TYPE (@0)); }))))
2299
2300 /* Convert - (~A) to A + 1. */
2301 (simplify
2302 (negate (nop_convert? (bit_not @0)))
2303 (plus (view_convert @0) { build_each_one_cst (type); }))
2304
2305 /* (a & b) ^ (a == b) -> !(a | b) */
2306 /* (a & b) == (a ^ b) -> !(a | b) */
2307 (for first_op (bit_xor eq)
2308 second_op (eq bit_xor)
2309 (simplify
2310 (first_op:c (bit_and:c truth_valued_p@0 truth_valued_p@1) (second_op:c @0 @1))
2311 (bit_not (bit_ior @0 @1))))
2312
2313 /* Convert ~ (A - 1) or ~ (A + -1) to -A. */
2314 (simplify
2315 (bit_not (convert? (minus @0 integer_each_onep)))
2316 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
2317 || !TYPE_UNSIGNED (TREE_TYPE (@0)))
2318 (convert (negate @0))))
2319 (simplify
2320 (bit_not (convert? (plus @0 integer_all_onesp)))
2321 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
2322 || !TYPE_UNSIGNED (TREE_TYPE (@0)))
2323 (convert (negate @0))))
2324
2325 /* Part of convert ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify. */
2326 (simplify
2327 (bit_not (convert? (bit_xor @0 INTEGER_CST@1)))
2328 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2329 (convert (bit_xor @0 (bit_not @1)))))
2330 (simplify
2331 (bit_not (convert? (bit_xor:c (bit_not @0) @1)))
2332 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2333 (convert (bit_xor @0 @1))))
2334
2335 /* Otherwise prefer ~(X ^ Y) to ~X ^ Y as more canonical. */
2336 (simplify
2337 (bit_xor:c (nop_convert?:s (bit_not:s @0)) @1)
2338 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2339 (bit_not (bit_xor (view_convert @0) @1))))
2340
2341 /* ~(a ^ b) is a == b for truth valued a and b. */
2342 (simplify
2343 (bit_not (bit_xor:s truth_valued_p@0 truth_valued_p@1))
2344 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2345 && TYPE_PRECISION (TREE_TYPE (@0)) == 1)
2346 (convert (eq @0 @1))))
2347
2348 /* (~a) == b is a ^ b for truth valued a and b. */
2349 (simplify
2350 (eq:c (bit_not:s truth_valued_p@0) truth_valued_p@1)
2351 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2352 && TYPE_PRECISION (TREE_TYPE (@0)) == 1)
2353 (convert (bit_xor @0 @1))))
2354
2355 /* (x & ~m) | (y & m) -> ((x ^ y) & m) ^ x */
2356 (simplify
2357 (bit_ior:c (bit_and:cs @0 (bit_not @2)) (bit_and:cs @1 @2))
2358 (bit_xor (bit_and (bit_xor @0 @1) @2) @0))
2359
2360 /* Fold A - (A & B) into ~B & A. */
2361 (simplify
2362 (minus (convert1? @0) (convert2?:s (bit_and:cs @@0 @1)))
2363 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
2364 && tree_nop_conversion_p (type, TREE_TYPE (@1)))
2365 (convert (bit_and (bit_not @1) @0))))
2366
2367 /* (m1 CMP m2) * d -> (m1 CMP m2) ? d : 0 */
2368 (if (!canonicalize_math_p ())
2369 (for cmp (tcc_comparison)
2370 (simplify
2371 (mult:c (convert (cmp@0 @1 @2)) @3)
2372 (if (INTEGRAL_TYPE_P (type)
2373 && INTEGRAL_TYPE_P (TREE_TYPE (@0)))
2374 (cond @0 @3 { build_zero_cst (type); })))
2375 /* (-(m1 CMP m2)) & d -> (m1 CMP m2) ? d : 0 */
2376 (simplify
2377 (bit_and:c (negate (convert (cmp@0 @1 @2))) @3)
2378 (if (INTEGRAL_TYPE_P (type)
2379 && INTEGRAL_TYPE_P (TREE_TYPE (@0)))
2380 (cond @0 @3 { build_zero_cst (type); })))
2381 )
2382 )
2383
2384 /* For integral types with undefined overflow and C != 0 fold
2385 x * C EQ/NE y * C into x EQ/NE y. */
2386 (for cmp (eq ne)
2387 (simplify
2388 (cmp (mult:c @0 @1) (mult:c @2 @1))
2389 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2390 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2391 && tree_expr_nonzero_p (@1))
2392 (cmp @0 @2))))
2393
2394 /* For integral types with wrapping overflow and C odd fold
2395 x * C EQ/NE y * C into x EQ/NE y. */
2396 (for cmp (eq ne)
2397 (simplify
2398 (cmp (mult @0 INTEGER_CST@1) (mult @2 @1))
2399 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2400 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))
2401 && (TREE_INT_CST_LOW (@1) & 1) != 0)
2402 (cmp @0 @2))))
2403
2404 /* For integral types with undefined overflow and C != 0 fold
2405 x * C RELOP y * C into:
2406
2407 x RELOP y for nonnegative C
2408 y RELOP x for negative C */
2409 (for cmp (lt gt le ge)
2410 (simplify
2411 (cmp (mult:c @0 @1) (mult:c @2 @1))
2412 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2413 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
2414 (if (tree_expr_nonnegative_p (@1) && tree_expr_nonzero_p (@1))
2415 (cmp @0 @2)
2416 (if (TREE_CODE (@1) == INTEGER_CST
2417 && wi::neg_p (wi::to_wide (@1), TYPE_SIGN (TREE_TYPE (@1))))
2418 (cmp @2 @0))))))
2419
2420 /* (X - 1U) <= INT_MAX-1U into (int) X > 0. */
2421 (for cmp (le gt)
2422 icmp (gt le)
2423 (simplify
2424 (cmp (plus @0 integer_minus_onep@1) INTEGER_CST@2)
2425 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2426 && TYPE_UNSIGNED (TREE_TYPE (@0))
2427 && TYPE_PRECISION (TREE_TYPE (@0)) > 1
2428 && (wi::to_wide (@2)
2429 == wi::max_value (TYPE_PRECISION (TREE_TYPE (@0)), SIGNED) - 1))
2430 (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
2431 (icmp (convert:stype @0) { build_int_cst (stype, 0); })))))
2432
2433 /* X / 4 < Y / 4 iff X < Y when the division is known to be exact. */
2434 (for cmp (simple_comparison)
2435 (simplify
2436 (cmp (convert?@3 (exact_div @0 INTEGER_CST@2)) (convert? (exact_div @1 @2)))
2437 (if (element_precision (@3) >= element_precision (@0)
2438 && types_match (@0, @1))
2439 (if (wi::lt_p (wi::to_wide (@2), 0, TYPE_SIGN (TREE_TYPE (@2))))
2440 (if (!TYPE_UNSIGNED (TREE_TYPE (@3)))
2441 (cmp @1 @0)
2442 (if (tree_expr_nonzero_p (@0) && tree_expr_nonzero_p (@1))
2443 (with
2444 {
2445 tree utype = unsigned_type_for (TREE_TYPE (@0));
2446 }
2447 (cmp (convert:utype @1) (convert:utype @0)))))
2448 (if (wi::gt_p (wi::to_wide (@2), 1, TYPE_SIGN (TREE_TYPE (@2))))
2449 (if (TYPE_UNSIGNED (TREE_TYPE (@0)) || !TYPE_UNSIGNED (TREE_TYPE (@3)))
2450 (cmp @0 @1)
2451 (with
2452 {
2453 tree utype = unsigned_type_for (TREE_TYPE (@0));
2454 }
2455 (cmp (convert:utype @0) (convert:utype @1)))))))))
2456
2457 /* X / C1 op C2 into a simple range test. */
2458 (for cmp (simple_comparison)
2459 (simplify
2460 (cmp (trunc_div:s @0 INTEGER_CST@1) INTEGER_CST@2)
2461 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2462 && integer_nonzerop (@1)
2463 && !TREE_OVERFLOW (@1)
2464 && !TREE_OVERFLOW (@2))
2465 (with { tree lo, hi; bool neg_overflow;
2466 enum tree_code code = fold_div_compare (cmp, @1, @2, &lo, &hi,
2467 &neg_overflow); }
2468 (switch
2469 (if (code == LT_EXPR || code == GE_EXPR)
2470 (if (TREE_OVERFLOW (lo))
2471 { build_int_cst (type, (code == LT_EXPR) ^ neg_overflow); }
2472 (if (code == LT_EXPR)
2473 (lt @0 { lo; })
2474 (ge @0 { lo; }))))
2475 (if (code == LE_EXPR || code == GT_EXPR)
2476 (if (TREE_OVERFLOW (hi))
2477 { build_int_cst (type, (code == LE_EXPR) ^ neg_overflow); }
2478 (if (code == LE_EXPR)
2479 (le @0 { hi; })
2480 (gt @0 { hi; }))))
2481 (if (!lo && !hi)
2482 { build_int_cst (type, code == NE_EXPR); })
2483 (if (code == EQ_EXPR && !hi)
2484 (ge @0 { lo; }))
2485 (if (code == EQ_EXPR && !lo)
2486 (le @0 { hi; }))
2487 (if (code == NE_EXPR && !hi)
2488 (lt @0 { lo; }))
2489 (if (code == NE_EXPR && !lo)
2490 (gt @0 { hi; }))
2491 (if (GENERIC)
2492 { build_range_check (UNKNOWN_LOCATION, type, @0, code == EQ_EXPR,
2493 lo, hi); })
2494 (with
2495 {
2496 tree etype = range_check_type (TREE_TYPE (@0));
2497 if (etype)
2498 {
2499 hi = fold_convert (etype, hi);
2500 lo = fold_convert (etype, lo);
2501 hi = const_binop (MINUS_EXPR, etype, hi, lo);
2502 }
2503 }
2504 (if (etype && hi && !TREE_OVERFLOW (hi))
2505 (if (code == EQ_EXPR)
2506 (le (minus (convert:etype @0) { lo; }) { hi; })
2507 (gt (minus (convert:etype @0) { lo; }) { hi; })))))))))
2508
2509 /* X + Z < Y + Z is the same as X < Y when there is no overflow. */
2510 (for op (lt le ge gt)
2511 (simplify
2512 (op (plus:c @0 @2) (plus:c @1 @2))
2513 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2514 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
2515 (op @0 @1))))
2516
2517 /* As a special case, X + C < Y + C is the same as (signed) X < (signed) Y
2518 when C is an unsigned integer constant with only the MSB set, and X and
2519 Y have types of equal or lower integer conversion rank than C's. */
2520 (for op (lt le ge gt)
2521 (simplify
2522 (op (plus @1 INTEGER_CST@0) (plus @2 @0))
2523 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2524 && TYPE_UNSIGNED (TREE_TYPE (@0))
2525 && wi::only_sign_bit_p (wi::to_wide (@0)))
2526 (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
2527 (op (convert:stype @1) (convert:stype @2))))))
2528
2529 /* For equality and subtraction, this is also true with wrapping overflow. */
2530 (for op (eq ne minus)
2531 (simplify
2532 (op (plus:c @0 @2) (plus:c @1 @2))
2533 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2534 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2535 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
2536 (op @0 @1))))
2537
2538 /* X - Z < Y - Z is the same as X < Y when there is no overflow. */
2539 (for op (lt le ge gt)
2540 (simplify
2541 (op (minus @0 @2) (minus @1 @2))
2542 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2543 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
2544 (op @0 @1))))
2545 /* For equality and subtraction, this is also true with wrapping overflow. */
2546 (for op (eq ne minus)
2547 (simplify
2548 (op (minus @0 @2) (minus @1 @2))
2549 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2550 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2551 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
2552 (op @0 @1))))
2553 /* And for pointers... */
2554 (for op (simple_comparison)
2555 (simplify
2556 (op (pointer_diff@3 @0 @2) (pointer_diff @1 @2))
2557 (if (!TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
2558 (op @0 @1))))
2559 (simplify
2560 (minus (pointer_diff@3 @0 @2) (pointer_diff @1 @2))
2561 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@3))
2562 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
2563 (pointer_diff @0 @1)))
2564
2565 /* Z - X < Z - Y is the same as Y < X when there is no overflow. */
2566 (for op (lt le ge gt)
2567 (simplify
2568 (op (minus @2 @0) (minus @2 @1))
2569 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2570 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
2571 (op @1 @0))))
2572 /* For equality and subtraction, this is also true with wrapping overflow. */
2573 (for op (eq ne minus)
2574 (simplify
2575 (op (minus @2 @0) (minus @2 @1))
2576 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2577 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2578 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
2579 (op @1 @0))))
2580 /* And for pointers... */
2581 (for op (simple_comparison)
2582 (simplify
2583 (op (pointer_diff@3 @2 @0) (pointer_diff @2 @1))
2584 (if (!TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
2585 (op @1 @0))))
2586 (simplify
2587 (minus (pointer_diff@3 @2 @0) (pointer_diff @2 @1))
2588 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@3))
2589 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
2590 (pointer_diff @1 @0)))
2591
2592 /* X + Y < Y is the same as X < 0 when there is no overflow. */
2593 (for op (lt le gt ge)
2594 (simplify
2595 (op:c (plus:c@2 @0 @1) @1)
2596 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2597 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2598 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0))
2599 && (CONSTANT_CLASS_P (@0) || single_use (@2)))
2600 (op @0 { build_zero_cst (TREE_TYPE (@0)); }))))
2601 /* For equality, this is also true with wrapping overflow. */
2602 (for op (eq ne)
2603 (simplify
2604 (op:c (nop_convert?@3 (plus:c@2 @0 (convert1? @1))) (convert2? @1))
2605 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2606 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2607 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
2608 && (CONSTANT_CLASS_P (@0) || (single_use (@2) && single_use (@3)))
2609 && tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@2))
2610 && tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@1)))
2611 (op @0 { build_zero_cst (TREE_TYPE (@0)); })))
2612 (simplify
2613 (op:c (nop_convert?@3 (pointer_plus@2 (convert1? @0) @1)) (convert2? @0))
2614 (if (tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@0))
2615 && tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0))
2616 && (CONSTANT_CLASS_P (@1) || (single_use (@2) && single_use (@3))))
2617 (op @1 { build_zero_cst (TREE_TYPE (@1)); }))))
2618
2619 /* (&a + b) !=/== (&a[1] + c) -> (&a[0] - &a[1]) + b !=/== c */
2620 (for neeq (ne eq)
2621 (simplify
2622 (neeq:c ADDR_EXPR@0 (pointer_plus @2 @3))
2623 (with { poly_int64 diff; tree inner_type = TREE_TYPE (@3);}
2624 (if (ptr_difference_const (@0, @2, &diff))
2625 (neeq { build_int_cst_type (inner_type, diff); } @3))))
2626 (simplify
2627 (neeq (pointer_plus ADDR_EXPR@0 @1) (pointer_plus ADDR_EXPR@2 @3))
2628 (with { poly_int64 diff; tree inner_type = TREE_TYPE (@1);}
2629 (if (ptr_difference_const (@0, @2, &diff))
2630 (neeq (plus { build_int_cst_type (inner_type, diff); } @1) @3)))))
2631
2632 /* X - Y < X is the same as Y > 0 when there is no overflow.
2633 For equality, this is also true with wrapping overflow. */
2634 (for op (simple_comparison)
2635 (simplify
2636 (op:c @0 (minus@2 @0 @1))
2637 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2638 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2639 || ((op == EQ_EXPR || op == NE_EXPR)
2640 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
2641 && (CONSTANT_CLASS_P (@1) || single_use (@2)))
2642 (op @1 { build_zero_cst (TREE_TYPE (@1)); }))))
2643
2644 /* Transform:
2645 (X / Y) == 0 -> X < Y if X, Y are unsigned.
2646 (X / Y) != 0 -> X >= Y, if X, Y are unsigned. */
2647 (for cmp (eq ne)
2648 ocmp (lt ge)
2649 (simplify
2650 (cmp (trunc_div @0 @1) integer_zerop)
2651 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
2652 /* Complex ==/!= is allowed, but not </>=. */
2653 && TREE_CODE (TREE_TYPE (@0)) != COMPLEX_TYPE
2654 && (VECTOR_TYPE_P (type) || !VECTOR_TYPE_P (TREE_TYPE (@0))))
2655 (ocmp @0 @1))))
2656
2657 /* X == C - X can never be true if C is odd. */
2658 (for cmp (eq ne)
2659 (simplify
2660 (cmp:c (convert? @0) (convert1? (minus INTEGER_CST@1 (convert2? @0))))
2661 (if (TREE_INT_CST_LOW (@1) & 1)
2662 { constant_boolean_node (cmp == NE_EXPR, type); })))
2663
2664 /* Arguments on which one can call get_nonzero_bits to get the bits
2665 possibly set. */
2666 (match with_possible_nonzero_bits
2667 INTEGER_CST@0)
2668 (match with_possible_nonzero_bits
2669 SSA_NAME@0
2670 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))))
2671 /* Slightly extended version, do not make it recursive to keep it cheap. */
2672 (match (with_possible_nonzero_bits2 @0)
2673 with_possible_nonzero_bits@0)
2674 (match (with_possible_nonzero_bits2 @0)
2675 (bit_and:c with_possible_nonzero_bits@0 @2))
2676
2677 /* Same for bits that are known to be set, but we do not have
2678 an equivalent to get_nonzero_bits yet. */
2679 (match (with_certain_nonzero_bits2 @0)
2680 INTEGER_CST@0)
2681 (match (with_certain_nonzero_bits2 @0)
2682 (bit_ior @1 INTEGER_CST@0))
2683
2684 /* X == C (or X & Z == Y | C) is impossible if ~nonzero(X) & C != 0. */
2685 (for cmp (eq ne)
2686 (simplify
2687 (cmp:c (with_possible_nonzero_bits2 @0) (with_certain_nonzero_bits2 @1))
2688 (if (wi::bit_and_not (wi::to_wide (@1), get_nonzero_bits (@0)) != 0)
2689 { constant_boolean_node (cmp == NE_EXPR, type); })))
2690
2691 /* ((X inner_op C0) outer_op C1)
2692 With X being a tree where value_range has reasoned certain bits to always be
2693 zero throughout its computed value range,
2694 inner_op = {|,^}, outer_op = {|,^} and inner_op != outer_op
2695 where zero_mask has 1's for all bits that are sure to be 0 in
2696 and 0's otherwise.
2697 if (inner_op == '^') C0 &= ~C1;
2698 if ((C0 & ~zero_mask) == 0) then emit (X outer_op (C0 outer_op C1)
2699 if ((C1 & ~zero_mask) == 0) then emit (X inner_op (C0 outer_op C1)
2700 */
2701 (for inner_op (bit_ior bit_xor)
2702 outer_op (bit_xor bit_ior)
2703 (simplify
2704 (outer_op
2705 (inner_op:s @2 INTEGER_CST@0) INTEGER_CST@1)
2706 (with
2707 {
2708 bool fail = false;
2709 wide_int zero_mask_not;
2710 wide_int C0;
2711 wide_int cst_emit;
2712
2713 if (TREE_CODE (@2) == SSA_NAME)
2714 zero_mask_not = get_nonzero_bits (@2);
2715 else
2716 fail = true;
2717
2718 if (inner_op == BIT_XOR_EXPR)
2719 {
2720 C0 = wi::bit_and_not (wi::to_wide (@0), wi::to_wide (@1));
2721 cst_emit = C0 | wi::to_wide (@1);
2722 }
2723 else
2724 {
2725 C0 = wi::to_wide (@0);
2726 cst_emit = C0 ^ wi::to_wide (@1);
2727 }
2728 }
2729 (if (!fail && (C0 & zero_mask_not) == 0)
2730 (outer_op @2 { wide_int_to_tree (type, cst_emit); })
2731 (if (!fail && (wi::to_wide (@1) & zero_mask_not) == 0)
2732 (inner_op @2 { wide_int_to_tree (type, cst_emit); }))))))
2733
2734 /* Associate (p +p off1) +p off2 as (p +p (off1 + off2)). */
2735 (simplify
2736 (pointer_plus (pointer_plus:s @0 @1) @3)
2737 (pointer_plus @0 (plus @1 @3)))
2738 #if GENERIC
2739 (simplify
2740 (pointer_plus (convert:s (pointer_plus:s @0 @1)) @3)
2741 (convert:type (pointer_plus @0 (plus @1 @3))))
2742 #endif
2743
2744 /* Pattern match
2745 tem1 = (long) ptr1;
2746 tem2 = (long) ptr2;
2747 tem3 = tem2 - tem1;
2748 tem4 = (unsigned long) tem3;
2749 tem5 = ptr1 + tem4;
2750 and produce
2751 tem5 = ptr2; */
2752 (simplify
2753 (pointer_plus @0 (convert?@2 (minus@3 (convert @1) (convert @0))))
2754 /* Conditionally look through a sign-changing conversion. */
2755 (if (TYPE_PRECISION (TREE_TYPE (@2)) == TYPE_PRECISION (TREE_TYPE (@3))
2756 && ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@1)))
2757 || (GENERIC && type == TREE_TYPE (@1))))
2758 @1))
2759 (simplify
2760 (pointer_plus @0 (convert?@2 (pointer_diff@3 @1 @@0)))
2761 (if (TYPE_PRECISION (TREE_TYPE (@2)) >= TYPE_PRECISION (TREE_TYPE (@3)))
2762 (convert @1)))
2763
2764 /* Pattern match
2765 tem = (sizetype) ptr;
2766 tem = tem & algn;
2767 tem = -tem;
2768 ... = ptr p+ tem;
2769 and produce the simpler and easier to analyze with respect to alignment
2770 ... = ptr & ~algn; */
2771 (simplify
2772 (pointer_plus @0 (negate (bit_and (convert @0) INTEGER_CST@1)))
2773 (with { tree algn = wide_int_to_tree (TREE_TYPE (@0), ~wi::to_wide (@1)); }
2774 (bit_and @0 { algn; })))
2775
2776 /* Try folding difference of addresses. */
2777 (simplify
2778 (minus (convert ADDR_EXPR@0) (convert (pointer_plus @1 @2)))
2779 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2780 (with { poly_int64 diff; }
2781 (if (ptr_difference_const (@0, @1, &diff))
2782 (minus { build_int_cst_type (type, diff); } (convert @2))))))
2783 (simplify
2784 (minus (convert (pointer_plus @0 @2)) (convert ADDR_EXPR@1))
2785 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2786 (with { poly_int64 diff; }
2787 (if (ptr_difference_const (@0, @1, &diff))
2788 (plus (convert @2) { build_int_cst_type (type, diff); })))))
2789 (simplify
2790 (minus (convert ADDR_EXPR@0) (convert @1))
2791 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2792 (with { poly_int64 diff; }
2793 (if (ptr_difference_const (@0, @1, &diff))
2794 { build_int_cst_type (type, diff); }))))
2795 (simplify
2796 (minus (convert @0) (convert ADDR_EXPR@1))
2797 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2798 (with { poly_int64 diff; }
2799 (if (ptr_difference_const (@0, @1, &diff))
2800 { build_int_cst_type (type, diff); }))))
2801 (simplify
2802 (pointer_diff (convert?@2 ADDR_EXPR@0) (convert1?@3 @1))
2803 (if (tree_nop_conversion_p (TREE_TYPE(@2), TREE_TYPE (@0))
2804 && tree_nop_conversion_p (TREE_TYPE(@3), TREE_TYPE (@1)))
2805 (with { poly_int64 diff; }
2806 (if (ptr_difference_const (@0, @1, &diff))
2807 { build_int_cst_type (type, diff); }))))
2808 (simplify
2809 (pointer_diff (convert?@2 @0) (convert1?@3 ADDR_EXPR@1))
2810 (if (tree_nop_conversion_p (TREE_TYPE(@2), TREE_TYPE (@0))
2811 && tree_nop_conversion_p (TREE_TYPE(@3), TREE_TYPE (@1)))
2812 (with { poly_int64 diff; }
2813 (if (ptr_difference_const (@0, @1, &diff))
2814 { build_int_cst_type (type, diff); }))))
2815
2816 /* (&a+b) - (&a[1] + c) -> sizeof(a[0]) + (b - c) */
2817 (simplify
2818 (pointer_diff (pointer_plus ADDR_EXPR@0 @1) (pointer_plus ADDR_EXPR@2 @3))
2819 (with { poly_int64 diff; }
2820 (if (ptr_difference_const (@0, @2, &diff))
2821 (plus { build_int_cst_type (type, diff); } (convert (minus @1 @3))))))
2822 /* (p + b) - &p->d -> offsetof (*p, d) + b */
2823 (simplify
2824 (pointer_diff (pointer_plus @0 @1) ADDR_EXPR@2)
2825 (with { poly_int64 diff; }
2826 (if (ptr_difference_const (@0, @2, &diff))
2827 (plus { build_int_cst_type (type, diff); } (convert @1)))))
2828 (simplify
2829 (pointer_diff ADDR_EXPR@0 (pointer_plus @1 @2))
2830 (with { poly_int64 diff; }
2831 (if (ptr_difference_const (@0, @1, &diff))
2832 (minus { build_int_cst_type (type, diff); } (convert @2)))))
2833
2834 /* Canonicalize (T *)(ptr - ptr-cst) to &MEM[ptr + -ptr-cst]. */
2835 (simplify
2836 (convert (pointer_diff @0 INTEGER_CST@1))
2837 (if (POINTER_TYPE_P (type))
2838 { build_fold_addr_expr_with_type
2839 (build2 (MEM_REF, char_type_node, @0,
2840 wide_int_to_tree (ptr_type_node, wi::neg (wi::to_wide (@1)))),
2841 type); }))
2842
2843 /* If arg0 is derived from the address of an object or function, we may
2844 be able to fold this expression using the object or function's
2845 alignment. */
2846 (simplify
2847 (bit_and (convert? @0) INTEGER_CST@1)
2848 (if (POINTER_TYPE_P (TREE_TYPE (@0))
2849 && tree_nop_conversion_p (type, TREE_TYPE (@0)))
2850 (with
2851 {
2852 unsigned int align;
2853 unsigned HOST_WIDE_INT bitpos;
2854 get_pointer_alignment_1 (@0, &align, &bitpos);
2855 }
2856 (if (wi::ltu_p (wi::to_wide (@1), align / BITS_PER_UNIT))
2857 { wide_int_to_tree (type, (wi::to_wide (@1)
2858 & (bitpos / BITS_PER_UNIT))); }))))
2859
2860 (match min_value
2861 uniform_integer_cst_p
2862 (with {
2863 tree int_cst = uniform_integer_cst_p (t);
2864 tree inner_type = TREE_TYPE (int_cst);
2865 }
2866 (if ((INTEGRAL_TYPE_P (inner_type)
2867 || POINTER_TYPE_P (inner_type))
2868 && wi::eq_p (wi::to_wide (int_cst), wi::min_value (inner_type))))))
2869
2870 (match max_value
2871 uniform_integer_cst_p
2872 (with {
2873 tree int_cst = uniform_integer_cst_p (t);
2874 tree itype = TREE_TYPE (int_cst);
2875 }
2876 (if ((INTEGRAL_TYPE_P (itype)
2877 || POINTER_TYPE_P (itype))
2878 && wi::eq_p (wi::to_wide (int_cst), wi::max_value (itype))))))
2879
2880 /* x > y && x != XXX_MIN --> x > y
2881 x > y && x == XXX_MIN --> false . */
2882 (for eqne (eq ne)
2883 (simplify
2884 (bit_and:c (gt:c@2 @0 @1) (eqne @0 min_value))
2885 (switch
2886 (if (eqne == EQ_EXPR)
2887 { constant_boolean_node (false, type); })
2888 (if (eqne == NE_EXPR)
2889 @2)
2890 )))
2891
2892 /* x < y && x != XXX_MAX --> x < y
2893 x < y && x == XXX_MAX --> false. */
2894 (for eqne (eq ne)
2895 (simplify
2896 (bit_and:c (lt:c@2 @0 @1) (eqne @0 max_value))
2897 (switch
2898 (if (eqne == EQ_EXPR)
2899 { constant_boolean_node (false, type); })
2900 (if (eqne == NE_EXPR)
2901 @2)
2902 )))
2903
2904 /* x <= y && x == XXX_MIN --> x == XXX_MIN. */
2905 (simplify
2906 (bit_and:c (le:c @0 @1) (eq@2 @0 min_value))
2907 @2)
2908
2909 /* x >= y && x == XXX_MAX --> x == XXX_MAX. */
2910 (simplify
2911 (bit_and:c (ge:c @0 @1) (eq@2 @0 max_value))
2912 @2)
2913
2914 /* x > y || x != XXX_MIN --> x != XXX_MIN. */
2915 (simplify
2916 (bit_ior:c (gt:c @0 @1) (ne@2 @0 min_value))
2917 @2)
2918
2919 /* x <= y || x != XXX_MIN --> true. */
2920 (simplify
2921 (bit_ior:c (le:c @0 @1) (ne @0 min_value))
2922 { constant_boolean_node (true, type); })
2923
2924 /* x <= y || x == XXX_MIN --> x <= y. */
2925 (simplify
2926 (bit_ior:c (le:c@2 @0 @1) (eq @0 min_value))
2927 @2)
2928
2929 /* x < y || x != XXX_MAX --> x != XXX_MAX. */
2930 (simplify
2931 (bit_ior:c (lt:c @0 @1) (ne@2 @0 max_value))
2932 @2)
2933
2934 /* x >= y || x != XXX_MAX --> true
2935 x >= y || x == XXX_MAX --> x >= y. */
2936 (for eqne (eq ne)
2937 (simplify
2938 (bit_ior:c (ge:c@2 @0 @1) (eqne @0 max_value))
2939 (switch
2940 (if (eqne == EQ_EXPR)
2941 @2)
2942 (if (eqne == NE_EXPR)
2943 { constant_boolean_node (true, type); }))))
2944
2945 /* y == XXX_MIN || x < y --> x <= y - 1 */
2946 (simplify
2947 (bit_ior:c (eq:s @1 min_value) (lt:cs @0 @1))
2948 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2949 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
2950 (le @0 (minus @1 { build_int_cst (TREE_TYPE (@1), 1); }))))
2951
2952 /* y != XXX_MIN && x >= y --> x > y - 1 */
2953 (simplify
2954 (bit_and:c (ne:s @1 min_value) (ge:cs @0 @1))
2955 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2956 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
2957 (gt @0 (minus @1 { build_int_cst (TREE_TYPE (@1), 1); }))))
2958
2959 /* Convert (X == CST1) && (X OP2 CST2) to a known value
2960 based on CST1 OP2 CST2. Similarly for (X != CST1). */
2961 /* Convert (X == Y) && (X OP2 Y) to a known value if X is an integral type.
2962 Similarly for (X != Y). */
2963
2964 (for code1 (eq ne)
2965 (for code2 (eq ne lt gt le ge)
2966 (simplify
2967 (bit_and:c (code1@3 @0 @1) (code2@4 @0 @2))
2968 (if ((TREE_CODE (@1) == INTEGER_CST
2969 && TREE_CODE (@2) == INTEGER_CST)
2970 || ((INTEGRAL_TYPE_P (TREE_TYPE (@1))
2971 || POINTER_TYPE_P (TREE_TYPE (@1)))
2972 && operand_equal_p (@1, @2)))
2973 (with
2974 {
2975 int cmp = 0;
2976 if (TREE_CODE (@1) == INTEGER_CST
2977 && TREE_CODE (@2) == INTEGER_CST)
2978 cmp = tree_int_cst_compare (@1, @2);
2979 bool val;
2980 switch (code2)
2981 {
2982 case EQ_EXPR: val = (cmp == 0); break;
2983 case NE_EXPR: val = (cmp != 0); break;
2984 case LT_EXPR: val = (cmp < 0); break;
2985 case GT_EXPR: val = (cmp > 0); break;
2986 case LE_EXPR: val = (cmp <= 0); break;
2987 case GE_EXPR: val = (cmp >= 0); break;
2988 default: gcc_unreachable ();
2989 }
2990 }
2991 (switch
2992 (if (code1 == EQ_EXPR && val) @3)
2993 (if (code1 == EQ_EXPR && !val) { constant_boolean_node (false, type); })
2994 (if (code1 == NE_EXPR && !val) @4)
2995 (if (code1 == NE_EXPR
2996 && code2 == GE_EXPR
2997 && cmp == 0)
2998 (gt @0 @1))
2999 (if (code1 == NE_EXPR
3000 && code2 == LE_EXPR
3001 && cmp == 0)
3002 (lt @0 @1))
3003 )
3004 )
3005 )
3006 )
3007 )
3008 )
3009
3010 /* Convert (X OP1 CST1) && (X OP2 CST2).
3011 Convert (X OP1 Y) && (X OP2 Y). */
3012
3013 (for code1 (lt le gt ge)
3014 (for code2 (lt le gt ge)
3015 (simplify
3016 (bit_and (code1:c@3 @0 @1) (code2:c@4 @0 @2))
3017 (if ((TREE_CODE (@1) == INTEGER_CST
3018 && TREE_CODE (@2) == INTEGER_CST)
3019 || ((INTEGRAL_TYPE_P (TREE_TYPE (@1))
3020 || POINTER_TYPE_P (TREE_TYPE (@1)))
3021 && operand_equal_p (@1, @2)))
3022 (with
3023 {
3024 int cmp = 0;
3025 if (TREE_CODE (@1) == INTEGER_CST
3026 && TREE_CODE (@2) == INTEGER_CST)
3027 cmp = tree_int_cst_compare (@1, @2);
3028 }
3029 (switch
3030 /* Choose the more restrictive of two < or <= comparisons. */
3031 (if ((code1 == LT_EXPR || code1 == LE_EXPR)
3032 && (code2 == LT_EXPR || code2 == LE_EXPR))
3033 (if ((cmp < 0) || (cmp == 0 && code1 == LT_EXPR))
3034 @3
3035 @4))
3036 /* Likewise chose the more restrictive of two > or >= comparisons. */
3037 (if ((code1 == GT_EXPR || code1 == GE_EXPR)
3038 && (code2 == GT_EXPR || code2 == GE_EXPR))
3039 (if ((cmp > 0) || (cmp == 0 && code1 == GT_EXPR))
3040 @3
3041 @4))
3042 /* Check for singleton ranges. */
3043 (if (cmp == 0
3044 && ((code1 == LE_EXPR && code2 == GE_EXPR)
3045 || (code1 == GE_EXPR && code2 == LE_EXPR)))
3046 (eq @0 @1))
3047 /* Check for disjoint ranges. */
3048 (if (cmp <= 0
3049 && (code1 == LT_EXPR || code1 == LE_EXPR)
3050 && (code2 == GT_EXPR || code2 == GE_EXPR))
3051 { constant_boolean_node (false, type); })
3052 (if (cmp >= 0
3053 && (code1 == GT_EXPR || code1 == GE_EXPR)
3054 && (code2 == LT_EXPR || code2 == LE_EXPR))
3055 { constant_boolean_node (false, type); })
3056 ))))))
3057
3058 /* Convert (X == CST1) || (X OP2 CST2) to a known value
3059 based on CST1 OP2 CST2. Similarly for (X != CST1). */
3060 /* Convert (X == Y) || (X OP2 Y) to a known value if X is an integral type.
3061 Similarly for (X != Y). */
3062
3063 (for code1 (eq ne)
3064 (for code2 (eq ne lt gt le ge)
3065 (simplify
3066 (bit_ior:c (code1@3 @0 @1) (code2@4 @0 @2))
3067 (if ((TREE_CODE (@1) == INTEGER_CST
3068 && TREE_CODE (@2) == INTEGER_CST)
3069 || ((INTEGRAL_TYPE_P (TREE_TYPE (@1))
3070 || POINTER_TYPE_P (TREE_TYPE (@1)))
3071 && operand_equal_p (@1, @2)))
3072 (with
3073 {
3074 int cmp = 0;
3075 if (TREE_CODE (@1) == INTEGER_CST
3076 && TREE_CODE (@2) == INTEGER_CST)
3077 cmp = tree_int_cst_compare (@1, @2);
3078 bool val;
3079 switch (code2)
3080 {
3081 case EQ_EXPR: val = (cmp == 0); break;
3082 case NE_EXPR: val = (cmp != 0); break;
3083 case LT_EXPR: val = (cmp < 0); break;
3084 case GT_EXPR: val = (cmp > 0); break;
3085 case LE_EXPR: val = (cmp <= 0); break;
3086 case GE_EXPR: val = (cmp >= 0); break;
3087 default: gcc_unreachable ();
3088 }
3089 }
3090 (switch
3091 (if (code1 == EQ_EXPR && val) @4)
3092 (if (code1 == NE_EXPR && val) { constant_boolean_node (true, type); })
3093 (if (code1 == NE_EXPR && !val) @3)
3094 (if (code1 == EQ_EXPR
3095 && code2 == GT_EXPR
3096 && cmp == 0)
3097 (ge @0 @1))
3098 (if (code1 == EQ_EXPR
3099 && code2 == LT_EXPR
3100 && cmp == 0)
3101 (le @0 @1))
3102 )
3103 )
3104 )
3105 )
3106 )
3107 )
3108
3109 /* Convert (X OP1 CST1) || (X OP2 CST2).
3110 Convert (X OP1 Y) || (X OP2 Y). */
3111
3112 (for code1 (lt le gt ge)
3113 (for code2 (lt le gt ge)
3114 (simplify
3115 (bit_ior (code1@3 @0 @1) (code2@4 @0 @2))
3116 (if ((TREE_CODE (@1) == INTEGER_CST
3117 && TREE_CODE (@2) == INTEGER_CST)
3118 || ((INTEGRAL_TYPE_P (TREE_TYPE (@1))
3119 || POINTER_TYPE_P (TREE_TYPE (@1)))
3120 && operand_equal_p (@1, @2)))
3121 (with
3122 {
3123 int cmp = 0;
3124 if (TREE_CODE (@1) == INTEGER_CST
3125 && TREE_CODE (@2) == INTEGER_CST)
3126 cmp = tree_int_cst_compare (@1, @2);
3127 }
3128 (switch
3129 /* Choose the more restrictive of two < or <= comparisons. */
3130 (if ((code1 == LT_EXPR || code1 == LE_EXPR)
3131 && (code2 == LT_EXPR || code2 == LE_EXPR))
3132 (if ((cmp < 0) || (cmp == 0 && code1 == LT_EXPR))
3133 @4
3134 @3))
3135 /* Likewise chose the more restrictive of two > or >= comparisons. */
3136 (if ((code1 == GT_EXPR || code1 == GE_EXPR)
3137 && (code2 == GT_EXPR || code2 == GE_EXPR))
3138 (if ((cmp > 0) || (cmp == 0 && code1 == GT_EXPR))
3139 @4
3140 @3))
3141 /* Check for singleton ranges. */
3142 (if (cmp == 0
3143 && ((code1 == LT_EXPR && code2 == GT_EXPR)
3144 || (code1 == GT_EXPR && code2 == LT_EXPR)))
3145 (ne @0 @2))
3146 /* Check for disjoint ranges. */
3147 (if (cmp >= 0
3148 && (code1 == LT_EXPR || code1 == LE_EXPR)
3149 && (code2 == GT_EXPR || code2 == GE_EXPR))
3150 { constant_boolean_node (true, type); })
3151 (if (cmp <= 0
3152 && (code1 == GT_EXPR || code1 == GE_EXPR)
3153 && (code2 == LT_EXPR || code2 == LE_EXPR))
3154 { constant_boolean_node (true, type); })
3155 ))))))
3156
3157 /* We can't reassociate at all for saturating types. */
3158 (if (!TYPE_SATURATING (type))
3159
3160 /* Contract negates. */
3161 /* A + (-B) -> A - B */
3162 (simplify
3163 (plus:c @0 (convert? (negate @1)))
3164 /* Apply STRIP_NOPS on the negate. */
3165 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
3166 && !TYPE_OVERFLOW_SANITIZED (type))
3167 (with
3168 {
3169 tree t1 = type;
3170 if (INTEGRAL_TYPE_P (type)
3171 && TYPE_OVERFLOW_WRAPS (type) != TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
3172 t1 = TYPE_OVERFLOW_WRAPS (type) ? type : TREE_TYPE (@1);
3173 }
3174 (convert (minus (convert:t1 @0) (convert:t1 @1))))))
3175 /* A - (-B) -> A + B */
3176 (simplify
3177 (minus @0 (convert? (negate @1)))
3178 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
3179 && !TYPE_OVERFLOW_SANITIZED (type))
3180 (with
3181 {
3182 tree t1 = type;
3183 if (INTEGRAL_TYPE_P (type)
3184 && TYPE_OVERFLOW_WRAPS (type) != TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
3185 t1 = TYPE_OVERFLOW_WRAPS (type) ? type : TREE_TYPE (@1);
3186 }
3187 (convert (plus (convert:t1 @0) (convert:t1 @1))))))
3188 /* -(T)(-A) -> (T)A
3189 Sign-extension is ok except for INT_MIN, which thankfully cannot
3190 happen without overflow. */
3191 (simplify
3192 (negate (convert (negate @1)))
3193 (if (INTEGRAL_TYPE_P (type)
3194 && (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@1))
3195 || (!TYPE_UNSIGNED (TREE_TYPE (@1))
3196 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))))
3197 && !TYPE_OVERFLOW_SANITIZED (type)
3198 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@1)))
3199 (convert @1)))
3200 (simplify
3201 (negate (convert negate_expr_p@1))
3202 (if (SCALAR_FLOAT_TYPE_P (type)
3203 && ((DECIMAL_FLOAT_TYPE_P (type)
3204 == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1))
3205 && TYPE_PRECISION (type) >= TYPE_PRECISION (TREE_TYPE (@1)))
3206 || !HONOR_SIGN_DEPENDENT_ROUNDING (type)))
3207 (convert (negate @1))))
3208 (simplify
3209 (negate (nop_convert? (negate @1)))
3210 (if (!TYPE_OVERFLOW_SANITIZED (type)
3211 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@1)))
3212 (view_convert @1)))
3213
3214 /* We can't reassociate floating-point unless -fassociative-math
3215 or fixed-point plus or minus because of saturation to +-Inf. */
3216 (if ((!FLOAT_TYPE_P (type) || flag_associative_math)
3217 && !FIXED_POINT_TYPE_P (type))
3218
3219 /* Match patterns that allow contracting a plus-minus pair
3220 irrespective of overflow issues. */
3221 /* (A +- B) - A -> +- B */
3222 /* (A +- B) -+ B -> A */
3223 /* A - (A +- B) -> -+ B */
3224 /* A +- (B -+ A) -> +- B */
3225 (simplify
3226 (minus (nop_convert1? (plus:c (nop_convert2? @0) @1)) @0)
3227 (view_convert @1))
3228 (simplify
3229 (minus (nop_convert1? (minus (nop_convert2? @0) @1)) @0)
3230 (if (!ANY_INTEGRAL_TYPE_P (type)
3231 || TYPE_OVERFLOW_WRAPS (type))
3232 (negate (view_convert @1))
3233 (view_convert (negate @1))))
3234 (simplify
3235 (plus:c (nop_convert1? (minus @0 (nop_convert2? @1))) @1)
3236 (view_convert @0))
3237 (simplify
3238 (minus @0 (nop_convert1? (plus:c (nop_convert2? @0) @1)))
3239 (if (!ANY_INTEGRAL_TYPE_P (type)
3240 || TYPE_OVERFLOW_WRAPS (type))
3241 (negate (view_convert @1))
3242 (view_convert (negate @1))))
3243 (simplify
3244 (minus @0 (nop_convert1? (minus (nop_convert2? @0) @1)))
3245 (view_convert @1))
3246 /* (A +- B) + (C - A) -> C +- B */
3247 /* (A + B) - (A - C) -> B + C */
3248 /* More cases are handled with comparisons. */
3249 (simplify
3250 (plus:c (plus:c @0 @1) (minus @2 @0))
3251 (plus @2 @1))
3252 (simplify
3253 (plus:c (minus @0 @1) (minus @2 @0))
3254 (minus @2 @1))
3255 (simplify
3256 (plus:c (pointer_diff @0 @1) (pointer_diff @2 @0))
3257 (if (TYPE_OVERFLOW_UNDEFINED (type)
3258 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0)))
3259 (pointer_diff @2 @1)))
3260 (simplify
3261 (minus (plus:c @0 @1) (minus @0 @2))
3262 (plus @1 @2))
3263
3264 /* (A +- CST1) +- CST2 -> A + CST3
3265 Use view_convert because it is safe for vectors and equivalent for
3266 scalars. */
3267 (for outer_op (plus minus)
3268 (for inner_op (plus minus)
3269 neg_inner_op (minus plus)
3270 (simplify
3271 (outer_op (nop_convert? (inner_op @0 CONSTANT_CLASS_P@1))
3272 CONSTANT_CLASS_P@2)
3273 /* If one of the types wraps, use that one. */
3274 (if (!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_WRAPS (type))
3275 /* If all 3 captures are CONSTANT_CLASS_P, punt, as we might recurse
3276 forever if something doesn't simplify into a constant. */
3277 (if (!CONSTANT_CLASS_P (@0))
3278 (if (outer_op == PLUS_EXPR)
3279 (plus (view_convert @0) (inner_op! @2 (view_convert @1)))
3280 (minus (view_convert @0) (neg_inner_op! @2 (view_convert @1)))))
3281 (if (!ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3282 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
3283 (if (outer_op == PLUS_EXPR)
3284 (view_convert (plus @0 (inner_op! (view_convert @2) @1)))
3285 (view_convert (minus @0 (neg_inner_op! (view_convert @2) @1))))
3286 /* If the constant operation overflows we cannot do the transform
3287 directly as we would introduce undefined overflow, for example
3288 with (a - 1) + INT_MIN. */
3289 (if (types_match (type, @0) && !TYPE_OVERFLOW_SANITIZED (type))
3290 (with { tree cst = const_binop (outer_op == inner_op
3291 ? PLUS_EXPR : MINUS_EXPR,
3292 type, @1, @2); }
3293 (if (cst)
3294 (if (INTEGRAL_TYPE_P (type) && !TREE_OVERFLOW (cst))
3295 (inner_op @0 { cst; } )
3296 /* X+INT_MAX+1 is X-INT_MIN. */
3297 (if (INTEGRAL_TYPE_P (type)
3298 && wi::to_wide (cst) == wi::min_value (type))
3299 (neg_inner_op @0 { wide_int_to_tree (type, wi::to_wide (cst)); })
3300 /* Last resort, use some unsigned type. */
3301 (with { tree utype = unsigned_type_for (type); }
3302 (if (utype)
3303 (view_convert (inner_op
3304 (view_convert:utype @0)
3305 (view_convert:utype
3306 { TREE_OVERFLOW (cst)
3307 ? drop_tree_overflow (cst) : cst; })))))))))))))))
3308
3309 /* (CST1 - A) +- CST2 -> CST3 - A */
3310 (for outer_op (plus minus)
3311 (simplify
3312 (outer_op (nop_convert? (minus CONSTANT_CLASS_P@1 @0)) CONSTANT_CLASS_P@2)
3313 /* If one of the types wraps, use that one. */
3314 (if (!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_WRAPS (type))
3315 /* If all 3 captures are CONSTANT_CLASS_P, punt, as we might recurse
3316 forever if something doesn't simplify into a constant. */
3317 (if (!CONSTANT_CLASS_P (@0))
3318 (minus (outer_op! (view_convert @1) @2) (view_convert @0)))
3319 (if (!ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3320 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
3321 (view_convert (minus (outer_op! @1 (view_convert @2)) @0))
3322 (if (types_match (type, @0) && !TYPE_OVERFLOW_SANITIZED (type))
3323 (with { tree cst = const_binop (outer_op, type, @1, @2); }
3324 (if (cst && !TREE_OVERFLOW (cst))
3325 (minus { cst; } @0))))))))
3326
3327 /* CST1 - (CST2 - A) -> CST3 + A
3328 Use view_convert because it is safe for vectors and equivalent for
3329 scalars. */
3330 (simplify
3331 (minus CONSTANT_CLASS_P@1 (nop_convert? (minus CONSTANT_CLASS_P@2 @0)))
3332 /* If one of the types wraps, use that one. */
3333 (if (!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_WRAPS (type))
3334 /* If all 3 captures are CONSTANT_CLASS_P, punt, as we might recurse
3335 forever if something doesn't simplify into a constant. */
3336 (if (!CONSTANT_CLASS_P (@0))
3337 (plus (view_convert @0) (minus! @1 (view_convert @2))))
3338 (if (!ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3339 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
3340 (view_convert (plus @0 (minus! (view_convert @1) @2)))
3341 (if (types_match (type, @0) && !TYPE_OVERFLOW_SANITIZED (type))
3342 (with { tree cst = const_binop (MINUS_EXPR, type, @1, @2); }
3343 (if (cst && !TREE_OVERFLOW (cst))
3344 (plus { cst; } @0)))))))
3345
3346 /* ((T)(A)) + CST -> (T)(A + CST) */
3347 #if GIMPLE
3348 (simplify
3349 (plus (convert:s SSA_NAME@0) INTEGER_CST@1)
3350 (if (TREE_CODE (TREE_TYPE (@0)) == INTEGER_TYPE
3351 && TREE_CODE (type) == INTEGER_TYPE
3352 && TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (@0))
3353 && int_fits_type_p (@1, TREE_TYPE (@0)))
3354 /* Perform binary operation inside the cast if the constant fits
3355 and (A + CST)'s range does not overflow. */
3356 (with
3357 {
3358 wi::overflow_type min_ovf = wi::OVF_OVERFLOW,
3359 max_ovf = wi::OVF_OVERFLOW;
3360 tree inner_type = TREE_TYPE (@0);
3361
3362 wide_int w1
3363 = wide_int::from (wi::to_wide (@1), TYPE_PRECISION (inner_type),
3364 TYPE_SIGN (inner_type));
3365
3366 value_range vr;
3367 if (get_global_range_query ()->range_of_expr (vr, @0)
3368 && !vr.varying_p () && !vr.undefined_p ())
3369 {
3370 wide_int wmin0 = vr.lower_bound ();
3371 wide_int wmax0 = vr.upper_bound ();
3372 wi::add (wmin0, w1, TYPE_SIGN (inner_type), &min_ovf);
3373 wi::add (wmax0, w1, TYPE_SIGN (inner_type), &max_ovf);
3374 }
3375 }
3376 (if (min_ovf == wi::OVF_NONE && max_ovf == wi::OVF_NONE)
3377 (convert (plus @0 { wide_int_to_tree (TREE_TYPE (@0), w1); } )))
3378 )))
3379 #endif
3380
3381 /* ((T)(A + CST1)) + CST2 -> (T)(A) + (T)CST1 + CST2 */
3382 #if GIMPLE
3383 (for op (plus minus)
3384 (simplify
3385 (plus (convert:s (op:s @0 INTEGER_CST@1)) INTEGER_CST@2)
3386 (if (TREE_CODE (TREE_TYPE (@0)) == INTEGER_TYPE
3387 && TREE_CODE (type) == INTEGER_TYPE
3388 && TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (@0))
3389 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
3390 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0))
3391 && TYPE_OVERFLOW_WRAPS (type))
3392 (plus (convert @0) (op @2 (convert @1))))))
3393 #endif
3394
3395 /* (T)(A) +- (T)(B) -> (T)(A +- B) only when (A +- B) could be simplified
3396 to a simple value. */
3397 (for op (plus minus)
3398 (simplify
3399 (op (convert @0) (convert @1))
3400 (if (INTEGRAL_TYPE_P (type)
3401 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
3402 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))
3403 && types_match (TREE_TYPE (@0), TREE_TYPE (@1))
3404 && !TYPE_OVERFLOW_TRAPS (type)
3405 && !TYPE_OVERFLOW_SANITIZED (type))
3406 (convert (op! @0 @1)))))
3407
3408 /* ~A + A -> -1 */
3409 (simplify
3410 (plus:c (convert? (bit_not @0)) (convert? @0))
3411 (if (!TYPE_OVERFLOW_TRAPS (type))
3412 (convert { build_all_ones_cst (TREE_TYPE (@0)); })))
3413
3414 /* ~A + 1 -> -A */
3415 (simplify
3416 (plus (convert? (bit_not @0)) integer_each_onep)
3417 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
3418 (negate (convert @0))))
3419
3420 /* -A - 1 -> ~A */
3421 (simplify
3422 (minus (convert? (negate @0)) integer_each_onep)
3423 (if (!TYPE_OVERFLOW_TRAPS (type)
3424 && TREE_CODE (type) != COMPLEX_TYPE
3425 && tree_nop_conversion_p (type, TREE_TYPE (@0)))
3426 (bit_not (convert @0))))
3427
3428 /* -1 - A -> ~A */
3429 (simplify
3430 (minus integer_all_onesp @0)
3431 (if (TREE_CODE (type) != COMPLEX_TYPE)
3432 (bit_not @0)))
3433
3434 /* (T)(P + A) - (T)P -> (T) A */
3435 (simplify
3436 (minus (convert (plus:c @@0 @1))
3437 (convert? @0))
3438 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
3439 /* For integer types, if A has a smaller type
3440 than T the result depends on the possible
3441 overflow in P + A.
3442 E.g. T=size_t, A=(unsigned)429497295, P>0.
3443 However, if an overflow in P + A would cause
3444 undefined behavior, we can assume that there
3445 is no overflow. */
3446 || (INTEGRAL_TYPE_P (TREE_TYPE (@1))
3447 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))))
3448 (convert @1)))
3449 (simplify
3450 (minus (convert (pointer_plus @@0 @1))
3451 (convert @0))
3452 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
3453 /* For pointer types, if the conversion of A to the
3454 final type requires a sign- or zero-extension,
3455 then we have to punt - it is not defined which
3456 one is correct. */
3457 || (POINTER_TYPE_P (TREE_TYPE (@0))
3458 && TREE_CODE (@1) == INTEGER_CST
3459 && tree_int_cst_sign_bit (@1) == 0))
3460 (convert @1)))
3461 (simplify
3462 (pointer_diff (pointer_plus @@0 @1) @0)
3463 /* The second argument of pointer_plus must be interpreted as signed, and
3464 thus sign-extended if necessary. */
3465 (with { tree stype = signed_type_for (TREE_TYPE (@1)); }
3466 /* Use view_convert instead of convert here, as POINTER_PLUS_EXPR
3467 second arg is unsigned even when we need to consider it as signed,
3468 we don't want to diagnose overflow here. */
3469 (convert (view_convert:stype @1))))
3470
3471 /* (T)P - (T)(P + A) -> -(T) A */
3472 (simplify
3473 (minus (convert? @0)
3474 (convert (plus:c @@0 @1)))
3475 (if (INTEGRAL_TYPE_P (type)
3476 && TYPE_OVERFLOW_UNDEFINED (type)
3477 /* For integer literals, using an intermediate unsigned type to avoid
3478 an overflow at run time is counter-productive because it introduces
3479 spurious overflows at compile time, in the form of TREE_OVERFLOW on
3480 the result, which may be problematic in GENERIC for some front-ends:
3481 (T)P - (T)(P + 4) -> (T)(-(U)4) -> (T)(4294967292) -> -4(OVF)
3482 so we use the direct path for them. */
3483 && TREE_CODE (@1) != INTEGER_CST
3484 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
3485 (with { tree utype = unsigned_type_for (type); }
3486 (convert (negate (convert:utype @1))))
3487 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
3488 /* For integer types, if A has a smaller type
3489 than T the result depends on the possible
3490 overflow in P + A.
3491 E.g. T=size_t, A=(unsigned)429497295, P>0.
3492 However, if an overflow in P + A would cause
3493 undefined behavior, we can assume that there
3494 is no overflow. */
3495 || (INTEGRAL_TYPE_P (TREE_TYPE (@1))
3496 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))))
3497 (negate (convert @1)))))
3498 (simplify
3499 (minus (convert @0)
3500 (convert (pointer_plus @@0 @1)))
3501 (if (INTEGRAL_TYPE_P (type)
3502 && TYPE_OVERFLOW_UNDEFINED (type)
3503 /* See above the rationale for this condition. */
3504 && TREE_CODE (@1) != INTEGER_CST
3505 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
3506 (with { tree utype = unsigned_type_for (type); }
3507 (convert (negate (convert:utype @1))))
3508 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
3509 /* For pointer types, if the conversion of A to the
3510 final type requires a sign- or zero-extension,
3511 then we have to punt - it is not defined which
3512 one is correct. */
3513 || (POINTER_TYPE_P (TREE_TYPE (@0))
3514 && TREE_CODE (@1) == INTEGER_CST
3515 && tree_int_cst_sign_bit (@1) == 0))
3516 (negate (convert @1)))))
3517 (simplify
3518 (pointer_diff @0 (pointer_plus @@0 @1))
3519 /* The second argument of pointer_plus must be interpreted as signed, and
3520 thus sign-extended if necessary. */
3521 (with { tree stype = signed_type_for (TREE_TYPE (@1)); }
3522 /* Use view_convert instead of convert here, as POINTER_PLUS_EXPR
3523 second arg is unsigned even when we need to consider it as signed,
3524 we don't want to diagnose overflow here. */
3525 (negate (convert (view_convert:stype @1)))))
3526
3527 /* (T)(P + A) - (T)(P + B) -> (T)A - (T)B */
3528 (simplify
3529 (minus (convert (plus:c @@0 @1))
3530 (convert (plus:c @0 @2)))
3531 (if (INTEGRAL_TYPE_P (type)
3532 && TYPE_OVERFLOW_UNDEFINED (type)
3533 && element_precision (type) <= element_precision (TREE_TYPE (@1))
3534 && element_precision (type) <= element_precision (TREE_TYPE (@2)))
3535 (with { tree utype = unsigned_type_for (type); }
3536 (convert (minus (convert:utype @1) (convert:utype @2))))
3537 (if (((element_precision (type) <= element_precision (TREE_TYPE (@1)))
3538 == (element_precision (type) <= element_precision (TREE_TYPE (@2))))
3539 && (element_precision (type) <= element_precision (TREE_TYPE (@1))
3540 /* For integer types, if A has a smaller type
3541 than T the result depends on the possible
3542 overflow in P + A.
3543 E.g. T=size_t, A=(unsigned)429497295, P>0.
3544 However, if an overflow in P + A would cause
3545 undefined behavior, we can assume that there
3546 is no overflow. */
3547 || (INTEGRAL_TYPE_P (TREE_TYPE (@1))
3548 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
3549 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))
3550 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@2)))))
3551 (minus (convert @1) (convert @2)))))
3552 (simplify
3553 (minus (convert (pointer_plus @@0 @1))
3554 (convert (pointer_plus @0 @2)))
3555 (if (INTEGRAL_TYPE_P (type)
3556 && TYPE_OVERFLOW_UNDEFINED (type)
3557 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
3558 (with { tree utype = unsigned_type_for (type); }
3559 (convert (minus (convert:utype @1) (convert:utype @2))))
3560 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
3561 /* For pointer types, if the conversion of A to the
3562 final type requires a sign- or zero-extension,
3563 then we have to punt - it is not defined which
3564 one is correct. */
3565 || (POINTER_TYPE_P (TREE_TYPE (@0))
3566 && TREE_CODE (@1) == INTEGER_CST
3567 && tree_int_cst_sign_bit (@1) == 0
3568 && TREE_CODE (@2) == INTEGER_CST
3569 && tree_int_cst_sign_bit (@2) == 0))
3570 (minus (convert @1) (convert @2)))))
3571 (simplify
3572 (pointer_diff (pointer_plus @0 @2) (pointer_plus @1 @2))
3573 (pointer_diff @0 @1))
3574 (simplify
3575 (pointer_diff (pointer_plus @@0 @1) (pointer_plus @0 @2))
3576 /* The second argument of pointer_plus must be interpreted as signed, and
3577 thus sign-extended if necessary. */
3578 (with { tree stype = signed_type_for (TREE_TYPE (@1)); }
3579 /* Use view_convert instead of convert here, as POINTER_PLUS_EXPR
3580 second arg is unsigned even when we need to consider it as signed,
3581 we don't want to diagnose overflow here. */
3582 (minus (convert (view_convert:stype @1))
3583 (convert (view_convert:stype @2)))))))
3584
3585 /* (A * C) +- (B * C) -> (A+-B) * C and (A * C) +- A -> A * (C+-1).
3586 Modeled after fold_plusminus_mult_expr. */
3587 (if (!TYPE_SATURATING (type)
3588 && (!FLOAT_TYPE_P (type) || flag_associative_math))
3589 (for plusminus (plus minus)
3590 (simplify
3591 (plusminus (mult:cs@3 @0 @1) (mult:cs@4 @0 @2))
3592 (if (!ANY_INTEGRAL_TYPE_P (type)
3593 || TYPE_OVERFLOW_WRAPS (type)
3594 || (INTEGRAL_TYPE_P (type)
3595 && tree_expr_nonzero_p (@0)
3596 && expr_not_equal_to (@0, wi::minus_one (TYPE_PRECISION (type)))))
3597 (if (single_use (@3) || single_use (@4))
3598 /* If @1 +- @2 is constant require a hard single-use on either
3599 original operand (but not on both). */
3600 (mult (plusminus @1 @2) @0)
3601 (mult! (plusminus @1 @2) @0)
3602 )))
3603 /* We cannot generate constant 1 for fract. */
3604 (if (!ALL_FRACT_MODE_P (TYPE_MODE (type)))
3605 (simplify
3606 (plusminus @0 (mult:c@3 @0 @2))
3607 (if ((!ANY_INTEGRAL_TYPE_P (type)
3608 || TYPE_OVERFLOW_WRAPS (type)
3609 /* For @0 + @0*@2 this transformation would introduce UB
3610 (where there was none before) for @0 in [-1,0] and @2 max.
3611 For @0 - @0*@2 this transformation would introduce UB
3612 for @0 0 and @2 in [min,min+1] or @0 -1 and @2 min+1. */
3613 || (INTEGRAL_TYPE_P (type)
3614 && ((tree_expr_nonzero_p (@0)
3615 && expr_not_equal_to (@0,
3616 wi::minus_one (TYPE_PRECISION (type))))
3617 || (plusminus == PLUS_EXPR
3618 ? expr_not_equal_to (@2,
3619 wi::max_value (TYPE_PRECISION (type), SIGNED))
3620 /* Let's ignore the @0 -1 and @2 min case. */
3621 : (expr_not_equal_to (@2,
3622 wi::min_value (TYPE_PRECISION (type), SIGNED))
3623 && expr_not_equal_to (@2,
3624 wi::min_value (TYPE_PRECISION (type), SIGNED)
3625 + 1))))))
3626 && single_use (@3))
3627 (mult (plusminus { build_one_cst (type); } @2) @0)))
3628 (simplify
3629 (plusminus (mult:c@3 @0 @2) @0)
3630 (if ((!ANY_INTEGRAL_TYPE_P (type)
3631 || TYPE_OVERFLOW_WRAPS (type)
3632 /* For @0*@2 + @0 this transformation would introduce UB
3633 (where there was none before) for @0 in [-1,0] and @2 max.
3634 For @0*@2 - @0 this transformation would introduce UB
3635 for @0 0 and @2 min. */
3636 || (INTEGRAL_TYPE_P (type)
3637 && ((tree_expr_nonzero_p (@0)
3638 && (plusminus == MINUS_EXPR
3639 || expr_not_equal_to (@0,
3640 wi::minus_one (TYPE_PRECISION (type)))))
3641 || expr_not_equal_to (@2,
3642 (plusminus == PLUS_EXPR
3643 ? wi::max_value (TYPE_PRECISION (type), SIGNED)
3644 : wi::min_value (TYPE_PRECISION (type), SIGNED))))))
3645 && single_use (@3))
3646 (mult (plusminus @2 { build_one_cst (type); }) @0))))))
3647
3648 #if GIMPLE
3649 /* Canonicalize X + (X << C) into X * (1 + (1 << C)) and
3650 (X << C1) + (X << C2) into X * ((1 << C1) + (1 << C2)). */
3651 (simplify
3652 (plus:c @0 (lshift:s @0 INTEGER_CST@1))
3653 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3654 && tree_fits_uhwi_p (@1)
3655 && tree_to_uhwi (@1) < element_precision (type)
3656 && (INTEGRAL_TYPE_P (TREE_TYPE (@0))
3657 || optab_handler (smul_optab,
3658 TYPE_MODE (type)) != CODE_FOR_nothing))
3659 (with { tree t = type;
3660 if (!TYPE_OVERFLOW_WRAPS (t)) t = unsigned_type_for (t);
3661 wide_int w = wi::set_bit_in_zero (tree_to_uhwi (@1),
3662 element_precision (type));
3663 w += 1;
3664 tree cst = wide_int_to_tree (VECTOR_TYPE_P (t) ? TREE_TYPE (t)
3665 : t, w);
3666 cst = build_uniform_cst (t, cst); }
3667 (convert (mult (convert:t @0) { cst; })))))
3668 (simplify
3669 (plus (lshift:s @0 INTEGER_CST@1) (lshift:s @0 INTEGER_CST@2))
3670 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3671 && tree_fits_uhwi_p (@1)
3672 && tree_to_uhwi (@1) < element_precision (type)
3673 && tree_fits_uhwi_p (@2)
3674 && tree_to_uhwi (@2) < element_precision (type)
3675 && (INTEGRAL_TYPE_P (TREE_TYPE (@0))
3676 || optab_handler (smul_optab,
3677 TYPE_MODE (type)) != CODE_FOR_nothing))
3678 (with { tree t = type;
3679 if (!TYPE_OVERFLOW_WRAPS (t)) t = unsigned_type_for (t);
3680 unsigned int prec = element_precision (type);
3681 wide_int w = wi::set_bit_in_zero (tree_to_uhwi (@1), prec);
3682 w += wi::set_bit_in_zero (tree_to_uhwi (@2), prec);
3683 tree cst = wide_int_to_tree (VECTOR_TYPE_P (t) ? TREE_TYPE (t)
3684 : t, w);
3685 cst = build_uniform_cst (t, cst); }
3686 (convert (mult (convert:t @0) { cst; })))))
3687 #endif
3688
3689 /* Canonicalize (X*C1)|(X*C2) and (X*C1)^(X*C2) to (C1+C2)*X when
3690 tree_nonzero_bits allows IOR and XOR to be treated like PLUS.
3691 Likewise, handle (X<<C3) and X as legitimate variants of X*C. */
3692 (for op (bit_ior bit_xor)
3693 (simplify
3694 (op (mult:s@0 @1 INTEGER_CST@2)
3695 (mult:s@3 @1 INTEGER_CST@4))
3696 (if (INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_WRAPS (type)
3697 && (tree_nonzero_bits (@0) & tree_nonzero_bits (@3)) == 0)
3698 (mult @1
3699 { wide_int_to_tree (type, wi::to_wide (@2) + wi::to_wide (@4)); })))
3700 (simplify
3701 (op:c (mult:s@0 @1 INTEGER_CST@2)
3702 (lshift:s@3 @1 INTEGER_CST@4))
3703 (if (INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_WRAPS (type)
3704 && tree_int_cst_sgn (@4) > 0
3705 && (tree_nonzero_bits (@0) & tree_nonzero_bits (@3)) == 0)
3706 (with { wide_int wone = wi::one (TYPE_PRECISION (type));
3707 wide_int c = wi::add (wi::to_wide (@2),
3708 wi::lshift (wone, wi::to_wide (@4))); }
3709 (mult @1 { wide_int_to_tree (type, c); }))))
3710 (simplify
3711 (op:c (mult:s@0 @1 INTEGER_CST@2)
3712 @1)
3713 (if (INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_WRAPS (type)
3714 && (tree_nonzero_bits (@0) & tree_nonzero_bits (@1)) == 0)
3715 (mult @1
3716 { wide_int_to_tree (type,
3717 wi::add (wi::to_wide (@2), 1)); })))
3718 (simplify
3719 (op (lshift:s@0 @1 INTEGER_CST@2)
3720 (lshift:s@3 @1 INTEGER_CST@4))
3721 (if (INTEGRAL_TYPE_P (type)
3722 && tree_int_cst_sgn (@2) > 0
3723 && tree_int_cst_sgn (@4) > 0
3724 && (tree_nonzero_bits (@0) & tree_nonzero_bits (@3)) == 0)
3725 (with { tree t = type;
3726 if (!TYPE_OVERFLOW_WRAPS (t))
3727 t = unsigned_type_for (t);
3728 wide_int wone = wi::one (TYPE_PRECISION (t));
3729 wide_int c = wi::add (wi::lshift (wone, wi::to_wide (@2)),
3730 wi::lshift (wone, wi::to_wide (@4))); }
3731 (convert (mult:t (convert:t @1) { wide_int_to_tree (t,c); })))))
3732 (simplify
3733 (op:c (lshift:s@0 @1 INTEGER_CST@2)
3734 @1)
3735 (if (INTEGRAL_TYPE_P (type)
3736 && tree_int_cst_sgn (@2) > 0
3737 && (tree_nonzero_bits (@0) & tree_nonzero_bits (@1)) == 0)
3738 (with { tree t = type;
3739 if (!TYPE_OVERFLOW_WRAPS (t))
3740 t = unsigned_type_for (t);
3741 wide_int wone = wi::one (TYPE_PRECISION (t));
3742 wide_int c = wi::add (wi::lshift (wone, wi::to_wide (@2)), wone); }
3743 (convert (mult:t (convert:t @1) { wide_int_to_tree (t, c); }))))))
3744
3745 /* Simplifications of MIN_EXPR, MAX_EXPR, fmin() and fmax(). */
3746
3747 (for minmax (min max)
3748 (simplify
3749 (minmax @0 @0)
3750 @0)
3751 /* max(max(x,y),x) -> max(x,y) */
3752 (simplify
3753 (minmax:c (minmax:c@2 @0 @1) @0)
3754 @2))
3755 /* For fmin() and fmax(), skip folding when both are sNaN. */
3756 (for minmax (FMIN_ALL FMAX_ALL)
3757 (simplify
3758 (minmax @0 @0)
3759 (if (!tree_expr_maybe_signaling_nan_p (@0))
3760 @0)))
3761 /* min(max(x,y),y) -> y. */
3762 (simplify
3763 (min:c (max:c @0 @1) @1)
3764 @1)
3765 /* max(min(x,y),y) -> y. */
3766 (simplify
3767 (max:c (min:c @0 @1) @1)
3768 @1)
3769 /* max(a,-a) -> abs(a). */
3770 (simplify
3771 (max:c @0 (negate @0))
3772 (if (TREE_CODE (type) != COMPLEX_TYPE
3773 && (! ANY_INTEGRAL_TYPE_P (type)
3774 || TYPE_OVERFLOW_UNDEFINED (type)))
3775 (abs @0)))
3776 /* min(a,-a) -> -abs(a). */
3777 (simplify
3778 (min:c @0 (negate @0))
3779 (if (TREE_CODE (type) != COMPLEX_TYPE
3780 && (! ANY_INTEGRAL_TYPE_P (type)
3781 || TYPE_OVERFLOW_UNDEFINED (type)))
3782 (negate (abs @0))))
3783 (simplify
3784 (min @0 @1)
3785 (switch
3786 (if (INTEGRAL_TYPE_P (type)
3787 && TYPE_MIN_VALUE (type)
3788 && operand_equal_p (@1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
3789 @1)
3790 (if (INTEGRAL_TYPE_P (type)
3791 && TYPE_MAX_VALUE (type)
3792 && operand_equal_p (@1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
3793 @0)))
3794 (simplify
3795 (max @0 @1)
3796 (switch
3797 (if (INTEGRAL_TYPE_P (type)
3798 && TYPE_MAX_VALUE (type)
3799 && operand_equal_p (@1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
3800 @1)
3801 (if (INTEGRAL_TYPE_P (type)
3802 && TYPE_MIN_VALUE (type)
3803 && operand_equal_p (@1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
3804 @0)))
3805
3806 /* max (a, a + CST) -> a + CST where CST is positive. */
3807 /* max (a, a + CST) -> a where CST is negative. */
3808 (simplify
3809 (max:c @0 (plus@2 @0 INTEGER_CST@1))
3810 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
3811 (if (tree_int_cst_sgn (@1) > 0)
3812 @2
3813 @0)))
3814
3815 /* min (a, a + CST) -> a where CST is positive. */
3816 /* min (a, a + CST) -> a + CST where CST is negative. */
3817 (simplify
3818 (min:c @0 (plus@2 @0 INTEGER_CST@1))
3819 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
3820 (if (tree_int_cst_sgn (@1) > 0)
3821 @0
3822 @2)))
3823
3824 /* Simplify min (&var[off0], &var[off1]) etc. depending on whether
3825 the addresses are known to be less, equal or greater. */
3826 (for minmax (min max)
3827 cmp (lt gt)
3828 (simplify
3829 (minmax (convert1?@2 addr@0) (convert2?@3 addr@1))
3830 (with
3831 {
3832 poly_int64 off0, off1;
3833 tree base0, base1;
3834 int equal = address_compare (cmp, TREE_TYPE (@2), @0, @1, base0, base1,
3835 off0, off1, GENERIC);
3836 }
3837 (if (equal == 1)
3838 (if (minmax == MIN_EXPR)
3839 (if (known_le (off0, off1))
3840 @2
3841 (if (known_gt (off0, off1))
3842 @3))
3843 (if (known_ge (off0, off1))
3844 @2
3845 (if (known_lt (off0, off1))
3846 @3)))))))
3847
3848 /* (convert (minmax ((convert (x) c)))) -> minmax (x c) if x is promoted
3849 and the outer convert demotes the expression back to x's type. */
3850 (for minmax (min max)
3851 (simplify
3852 (convert (minmax@0 (convert @1) INTEGER_CST@2))
3853 (if (INTEGRAL_TYPE_P (type)
3854 && types_match (@1, type) && int_fits_type_p (@2, type)
3855 && TYPE_SIGN (TREE_TYPE (@0)) == TYPE_SIGN (type)
3856 && TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type))
3857 (minmax @1 (convert @2)))))
3858
3859 (for minmax (FMIN_ALL FMAX_ALL)
3860 /* If either argument is NaN and other one is not sNaN, return the other
3861 one. Avoid the transformation if we get (and honor) a signalling NaN. */
3862 (simplify
3863 (minmax:c @0 REAL_CST@1)
3864 (if (real_isnan (TREE_REAL_CST_PTR (@1))
3865 && (!HONOR_SNANS (@1) || !TREE_REAL_CST (@1).signalling)
3866 && !tree_expr_maybe_signaling_nan_p (@0))
3867 @0)))
3868 /* Convert fmin/fmax to MIN_EXPR/MAX_EXPR. C99 requires these
3869 functions to return the numeric arg if the other one is NaN.
3870 MIN and MAX don't honor that, so only transform if -ffinite-math-only
3871 is set. C99 doesn't require -0.0 to be handled, so we don't have to
3872 worry about it either. */
3873 (if (flag_finite_math_only)
3874 (simplify
3875 (FMIN_ALL @0 @1)
3876 (min @0 @1))
3877 (simplify
3878 (FMAX_ALL @0 @1)
3879 (max @0 @1)))
3880 /* min (-A, -B) -> -max (A, B) */
3881 (for minmax (min max FMIN_ALL FMAX_ALL)
3882 maxmin (max min FMAX_ALL FMIN_ALL)
3883 (simplify
3884 (minmax (negate:s@2 @0) (negate:s@3 @1))
3885 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
3886 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3887 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
3888 (negate (maxmin @0 @1)))))
3889 /* MIN (~X, ~Y) -> ~MAX (X, Y)
3890 MAX (~X, ~Y) -> ~MIN (X, Y) */
3891 (for minmax (min max)
3892 maxmin (max min)
3893 (simplify
3894 (minmax (bit_not:s@2 @0) (bit_not:s@3 @1))
3895 (bit_not (maxmin @0 @1)))
3896 /* ~MAX(~X, Y) --> MIN(X, ~Y) */
3897 /* ~MIN(~X, Y) --> MAX(X, ~Y) */
3898 (simplify
3899 (bit_not (minmax:cs (bit_not @0) @1))
3900 (maxmin @0 (bit_not @1))))
3901
3902 /* MIN (X, Y) == X -> X <= Y */
3903 (for minmax (min min max max)
3904 cmp (eq ne eq ne )
3905 out (le gt ge lt )
3906 (simplify
3907 (cmp:c (minmax:c @0 @1) @0)
3908 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0)))
3909 (out @0 @1))))
3910 /* MIN (X, 5) == 0 -> X == 0
3911 MIN (X, 5) == 7 -> false */
3912 (for cmp (eq ne)
3913 (simplify
3914 (cmp (min @0 INTEGER_CST@1) INTEGER_CST@2)
3915 (if (wi::lt_p (wi::to_wide (@1), wi::to_wide (@2),
3916 TYPE_SIGN (TREE_TYPE (@0))))
3917 { constant_boolean_node (cmp == NE_EXPR, type); }
3918 (if (wi::gt_p (wi::to_wide (@1), wi::to_wide (@2),
3919 TYPE_SIGN (TREE_TYPE (@0))))
3920 (cmp @0 @2)))))
3921 (for cmp (eq ne)
3922 (simplify
3923 (cmp (max @0 INTEGER_CST@1) INTEGER_CST@2)
3924 (if (wi::gt_p (wi::to_wide (@1), wi::to_wide (@2),
3925 TYPE_SIGN (TREE_TYPE (@0))))
3926 { constant_boolean_node (cmp == NE_EXPR, type); }
3927 (if (wi::lt_p (wi::to_wide (@1), wi::to_wide (@2),
3928 TYPE_SIGN (TREE_TYPE (@0))))
3929 (cmp @0 @2)))))
3930 /* MIN (X, C1) < C2 -> X < C2 || C1 < C2 */
3931 (for minmax (min min max max min min max max )
3932 cmp (lt le gt ge gt ge lt le )
3933 comb (bit_ior bit_ior bit_ior bit_ior bit_and bit_and bit_and bit_and)
3934 (simplify
3935 (cmp (minmax @0 INTEGER_CST@1) INTEGER_CST@2)
3936 (comb (cmp @0 @2) (cmp @1 @2))))
3937
3938 /* X <= MAX(X, Y) -> true
3939 X > MAX(X, Y) -> false
3940 X >= MIN(X, Y) -> true
3941 X < MIN(X, Y) -> false */
3942 (for minmax (min min max max )
3943 cmp (ge lt le gt )
3944 (simplify
3945 (cmp:c @0 (minmax:c @0 @1))
3946 { constant_boolean_node (cmp == GE_EXPR || cmp == LE_EXPR, type); } ))
3947
3948 /* Undo fancy ways of writing max/min or other ?: expressions, like
3949 a - ((a - b) & -(a < b)) and a - (a - b) * (a < b) into (a < b) ? b : a.
3950 People normally use ?: and that is what we actually try to optimize. */
3951 /* Transform A + (B-A)*cmp into cmp ? B : A. */
3952 (simplify
3953 (plus:c @0 (mult:c (minus @1 @0) zero_one_valued_p@2))
3954 (if (INTEGRAL_TYPE_P (type)
3955 && (GIMPLE || !TREE_SIDE_EFFECTS (@1)))
3956 (cond (convert:boolean_type_node @2) @1 @0)))
3957 /* Transform A - (A-B)*cmp into cmp ? B : A. */
3958 (simplify
3959 (minus @0 (mult:c (minus @0 @1) zero_one_valued_p@2))
3960 (if (INTEGRAL_TYPE_P (type)
3961 && (GIMPLE || !TREE_SIDE_EFFECTS (@1)))
3962 (cond (convert:boolean_type_node @2) @1 @0)))
3963 /* Transform A ^ (A^B)*cmp into cmp ? B : A. */
3964 (simplify
3965 (bit_xor:c @0 (mult:c (bit_xor:c @0 @1) zero_one_valued_p@2))
3966 (if (INTEGRAL_TYPE_P (type)
3967 && (GIMPLE || !TREE_SIDE_EFFECTS (@1)))
3968 (cond (convert:boolean_type_node @2) @1 @0)))
3969
3970 /* (x <= 0 ? -x : 0) -> max(-x, 0). */
3971 (simplify
3972 (cond (le @0 integer_zerop@1) (negate@2 @0) integer_zerop@1)
3973 (max @2 @1))
3974
3975 /* (zero_one == 0) ? y : z <op> y -> ((typeof(y))zero_one * z) <op> y */
3976 (for op (bit_xor bit_ior plus)
3977 (simplify
3978 (cond (eq zero_one_valued_p@0
3979 integer_zerop)
3980 @1
3981 (op:c @2 @1))
3982 (if (INTEGRAL_TYPE_P (type)
3983 && TYPE_PRECISION (type) > 1
3984 && (INTEGRAL_TYPE_P (TREE_TYPE (@0))))
3985 (op (mult (convert:type @0) @2) @1))))
3986
3987 /* (zero_one != 0) ? z <op> y : y -> ((typeof(y))zero_one * z) <op> y */
3988 (for op (bit_xor bit_ior plus)
3989 (simplify
3990 (cond (ne zero_one_valued_p@0
3991 integer_zerop)
3992 (op:c @2 @1)
3993 @1)
3994 (if (INTEGRAL_TYPE_P (type)
3995 && TYPE_PRECISION (type) > 1
3996 && (INTEGRAL_TYPE_P (TREE_TYPE (@0))))
3997 (op (mult (convert:type @0) @2) @1))))
3998
3999 /* Simplifications of shift and rotates. */
4000
4001 (for rotate (lrotate rrotate)
4002 (simplify
4003 (rotate integer_all_onesp@0 @1)
4004 @0))
4005
4006 /* Optimize -1 >> x for arithmetic right shifts. */
4007 (simplify
4008 (rshift integer_all_onesp@0 @1)
4009 (if (!TYPE_UNSIGNED (type))
4010 @0))
4011
4012 /* Optimize (x >> c) << c into x & (-1<<c). */
4013 (simplify
4014 (lshift (nop_convert? (rshift @0 INTEGER_CST@1)) @1)
4015 (if (wi::ltu_p (wi::to_wide (@1), element_precision (type)))
4016 /* It doesn't matter if the right shift is arithmetic or logical. */
4017 (bit_and (view_convert @0) (lshift { build_minus_one_cst (type); } @1))))
4018
4019 (simplify
4020 (lshift (convert (convert@2 (rshift @0 INTEGER_CST@1))) @1)
4021 (if (wi::ltu_p (wi::to_wide (@1), element_precision (type))
4022 /* Allow intermediate conversion to integral type with whatever sign, as
4023 long as the low TYPE_PRECISION (type)
4024 - TYPE_PRECISION (TREE_TYPE (@2)) bits are preserved. */
4025 && INTEGRAL_TYPE_P (type)
4026 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
4027 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
4028 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (@0))
4029 && (TYPE_PRECISION (TREE_TYPE (@2)) >= TYPE_PRECISION (type)
4030 || wi::geu_p (wi::to_wide (@1),
4031 TYPE_PRECISION (type)
4032 - TYPE_PRECISION (TREE_TYPE (@2)))))
4033 (bit_and (convert @0) (lshift { build_minus_one_cst (type); } @1))))
4034
4035 /* For (x << c) >> c, optimize into x & ((unsigned)-1 >> c) for
4036 unsigned x OR truncate into the precision(type) - c lowest bits
4037 of signed x (if they have mode precision or a precision of 1). */
4038 (simplify
4039 (rshift (nop_convert? (lshift @0 INTEGER_CST@1)) @@1)
4040 (if (wi::ltu_p (wi::to_wide (@1), element_precision (type)))
4041 (if (TYPE_UNSIGNED (type))
4042 (bit_and (convert @0) (rshift { build_minus_one_cst (type); } @1))
4043 (if (INTEGRAL_TYPE_P (type))
4044 (with {
4045 int width = element_precision (type) - tree_to_uhwi (@1);
4046 tree stype = build_nonstandard_integer_type (width, 0);
4047 }
4048 (if (width == 1 || type_has_mode_precision_p (stype))
4049 (convert (convert:stype @0))))))))
4050
4051 /* Optimize x >> x into 0 */
4052 (simplify
4053 (rshift @0 @0)
4054 { build_zero_cst (type); })
4055
4056 (for shiftrotate (lrotate rrotate lshift rshift)
4057 (simplify
4058 (shiftrotate @0 integer_zerop)
4059 (non_lvalue @0))
4060 (simplify
4061 (shiftrotate integer_zerop@0 @1)
4062 @0)
4063 /* Prefer vector1 << scalar to vector1 << vector2
4064 if vector2 is uniform. */
4065 (for vec (VECTOR_CST CONSTRUCTOR)
4066 (simplify
4067 (shiftrotate @0 vec@1)
4068 (with { tree tem = uniform_vector_p (@1); }
4069 (if (tem)
4070 (shiftrotate @0 { tem; }))))))
4071
4072 /* Simplify X << Y where Y's low width bits are 0 to X, as only valid
4073 Y is 0. Similarly for X >> Y. */
4074 #if GIMPLE
4075 (for shift (lshift rshift)
4076 (simplify
4077 (shift @0 SSA_NAME@1)
4078 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1)))
4079 (with {
4080 int width = ceil_log2 (element_precision (TREE_TYPE (@0)));
4081 int prec = TYPE_PRECISION (TREE_TYPE (@1));
4082 }
4083 (if ((get_nonzero_bits (@1) & wi::mask (width, false, prec)) == 0)
4084 @0)))))
4085 #endif
4086
4087 /* Rewrite an LROTATE_EXPR by a constant into an
4088 RROTATE_EXPR by a new constant. */
4089 (simplify
4090 (lrotate @0 INTEGER_CST@1)
4091 (rrotate @0 { const_binop (MINUS_EXPR, TREE_TYPE (@1),
4092 build_int_cst (TREE_TYPE (@1),
4093 element_precision (type)), @1); }))
4094
4095 /* Turn (a OP c1) OP c2 into a OP (c1+c2). */
4096 (for op (lrotate rrotate rshift lshift)
4097 (simplify
4098 (op (op @0 INTEGER_CST@1) INTEGER_CST@2)
4099 (with { unsigned int prec = element_precision (type); }
4100 (if (wi::ge_p (wi::to_wide (@1), 0, TYPE_SIGN (TREE_TYPE (@1)))
4101 && wi::lt_p (wi::to_wide (@1), prec, TYPE_SIGN (TREE_TYPE (@1)))
4102 && wi::ge_p (wi::to_wide (@2), 0, TYPE_SIGN (TREE_TYPE (@2)))
4103 && wi::lt_p (wi::to_wide (@2), prec, TYPE_SIGN (TREE_TYPE (@2))))
4104 (with { unsigned int low = (tree_to_uhwi (@1)
4105 + tree_to_uhwi (@2)); }
4106 /* Deal with a OP (c1 + c2) being undefined but (a OP c1) OP c2
4107 being well defined. */
4108 (if (low >= prec)
4109 (if (op == LROTATE_EXPR || op == RROTATE_EXPR)
4110 (op @0 { build_int_cst (TREE_TYPE (@1), low % prec); })
4111 (if (TYPE_UNSIGNED (type) || op == LSHIFT_EXPR)
4112 { build_zero_cst (type); }
4113 (op @0 { build_int_cst (TREE_TYPE (@1), prec - 1); })))
4114 (op @0 { build_int_cst (TREE_TYPE (@1), low); })))))))
4115
4116
4117 /* Simplify (CST << x) & 1 to 0 if CST is even or to x == 0 if it is odd. */
4118 (simplify
4119 (bit_and (lshift INTEGER_CST@1 @0) integer_onep)
4120 (if ((wi::to_wide (@1) & 1) != 0)
4121 (convert (eq:boolean_type_node @0 { build_zero_cst (TREE_TYPE (@0)); }))
4122 { build_zero_cst (type); }))
4123
4124 /* Simplify ((C << x) & D) != 0 where C and D are power of two constants,
4125 either to false if D is smaller (unsigned comparison) than C, or to
4126 x == log2 (D) - log2 (C). Similarly for right shifts.
4127 Note for `(1 >> x)`, the & 1 has been removed so matching that seperately. */
4128 (for cmp (ne eq)
4129 icmp (eq ne)
4130 (simplify
4131 (cmp (bit_and (lshift integer_pow2p@1 @0) integer_pow2p@2) integer_zerop)
4132 (with { int c1 = wi::clz (wi::to_wide (@1));
4133 int c2 = wi::clz (wi::to_wide (@2)); }
4134 (if (c1 < c2)
4135 { constant_boolean_node (cmp == NE_EXPR ? false : true, type); }
4136 (icmp @0 { build_int_cst (TREE_TYPE (@0), c1 - c2); }))))
4137 (simplify
4138 (cmp (bit_and (rshift integer_pow2p@1 @0) integer_pow2p@2) integer_zerop)
4139 (if (tree_int_cst_sgn (@1) > 0)
4140 (with { int c1 = wi::clz (wi::to_wide (@1));
4141 int c2 = wi::clz (wi::to_wide (@2)); }
4142 (if (c1 > c2)
4143 { constant_boolean_node (cmp == NE_EXPR ? false : true, type); }
4144 (icmp @0 { build_int_cst (TREE_TYPE (@0), c2 - c1); })))))
4145 /* `(1 >> X) != 0` -> `X == 0` */
4146 /* `(1 >> X) == 0` -> `X != 0` */
4147 (simplify
4148 (cmp (rshift integer_onep @0) integer_zerop)
4149 (icmp @0 { build_zero_cst (TREE_TYPE (@0)); })))
4150
4151 /* (CST1 << A) == CST2 -> A == ctz (CST2) - ctz (CST1)
4152 (CST1 << A) != CST2 -> A != ctz (CST2) - ctz (CST1)
4153 if CST2 != 0. */
4154 (for cmp (ne eq)
4155 (simplify
4156 (cmp (lshift INTEGER_CST@0 @1) INTEGER_CST@2)
4157 (with { int cand = wi::ctz (wi::to_wide (@2)) - wi::ctz (wi::to_wide (@0)); }
4158 (if (cand < 0
4159 || (!integer_zerop (@2)
4160 && wi::lshift (wi::to_wide (@0), cand) != wi::to_wide (@2)))
4161 { constant_boolean_node (cmp == NE_EXPR, type); }
4162 (if (!integer_zerop (@2)
4163 && wi::lshift (wi::to_wide (@0), cand) == wi::to_wide (@2))
4164 (cmp @1 { build_int_cst (TREE_TYPE (@1), cand); }))))))
4165
4166 /* Fold ((X << C1) & C2) cmp C3 into (X & (C2 >> C1)) cmp (C3 >> C1)
4167 ((X >> C1) & C2) cmp C3 into (X & (C2 << C1)) cmp (C3 << C1). */
4168 (for cmp (ne eq)
4169 (simplify
4170 (cmp (bit_and:s (lshift:s @0 INTEGER_CST@1) INTEGER_CST@2) INTEGER_CST@3)
4171 (if (tree_fits_shwi_p (@1)
4172 && tree_to_shwi (@1) > 0
4173 && tree_to_shwi (@1) < TYPE_PRECISION (TREE_TYPE (@0)))
4174 (if (tree_to_shwi (@1) > wi::ctz (wi::to_wide (@3)))
4175 { constant_boolean_node (cmp == NE_EXPR, type); }
4176 (with { wide_int c1 = wi::to_wide (@1);
4177 wide_int c2 = wi::lrshift (wi::to_wide (@2), c1);
4178 wide_int c3 = wi::lrshift (wi::to_wide (@3), c1); }
4179 (cmp (bit_and @0 { wide_int_to_tree (TREE_TYPE (@0), c2); })
4180 { wide_int_to_tree (TREE_TYPE (@0), c3); })))))
4181 (simplify
4182 (cmp (bit_and:s (rshift:s @0 INTEGER_CST@1) INTEGER_CST@2) INTEGER_CST@3)
4183 (if (tree_fits_shwi_p (@1)
4184 && tree_to_shwi (@1) > 0
4185 && tree_to_shwi (@1) < TYPE_PRECISION (TREE_TYPE (@0)))
4186 (with { tree t0 = TREE_TYPE (@0);
4187 unsigned int prec = TYPE_PRECISION (t0);
4188 wide_int c1 = wi::to_wide (@1);
4189 wide_int c2 = wi::to_wide (@2);
4190 wide_int c3 = wi::to_wide (@3);
4191 wide_int sb = wi::set_bit_in_zero (prec - 1, prec); }
4192 (if ((c2 & c3) != c3)
4193 { constant_boolean_node (cmp == NE_EXPR, type); }
4194 (if (TYPE_UNSIGNED (t0))
4195 (if ((c3 & wi::arshift (sb, c1 - 1)) != 0)
4196 { constant_boolean_node (cmp == NE_EXPR, type); }
4197 (cmp (bit_and @0 { wide_int_to_tree (t0, c2 << c1); })
4198 { wide_int_to_tree (t0, c3 << c1); }))
4199 (with { wide_int smask = wi::arshift (sb, c1); }
4200 (switch
4201 (if ((c2 & smask) == 0)
4202 (cmp (bit_and @0 { wide_int_to_tree (t0, c2 << c1); })
4203 { wide_int_to_tree (t0, c3 << c1); }))
4204 (if ((c3 & smask) == 0)
4205 (cmp (bit_and @0 { wide_int_to_tree (t0, (c2 << c1) | sb); })
4206 { wide_int_to_tree (t0, c3 << c1); }))
4207 (if ((c2 & smask) != (c3 & smask))
4208 { constant_boolean_node (cmp == NE_EXPR, type); })
4209 (cmp (bit_and @0 { wide_int_to_tree (t0, (c2 << c1) | sb); })
4210 { wide_int_to_tree (t0, (c3 << c1) | sb); })))))))))
4211
4212 /* Fold (X << C1) & C2 into (X << C1) & (C2 | ((1 << C1) - 1))
4213 (X >> C1) & C2 into (X >> C1) & (C2 | ~((type) -1 >> C1))
4214 if the new mask might be further optimized. */
4215 (for shift (lshift rshift)
4216 (simplify
4217 (bit_and (convert?:s@4 (shift:s@5 (convert1?@3 @0) INTEGER_CST@1))
4218 INTEGER_CST@2)
4219 (if (tree_nop_conversion_p (TREE_TYPE (@4), TREE_TYPE (@5))
4220 && TYPE_PRECISION (type) <= HOST_BITS_PER_WIDE_INT
4221 && tree_fits_uhwi_p (@1)
4222 && tree_to_uhwi (@1) > 0
4223 && tree_to_uhwi (@1) < TYPE_PRECISION (type))
4224 (with
4225 {
4226 unsigned int shiftc = tree_to_uhwi (@1);
4227 unsigned HOST_WIDE_INT mask = TREE_INT_CST_LOW (@2);
4228 unsigned HOST_WIDE_INT newmask, zerobits = 0;
4229 tree shift_type = TREE_TYPE (@3);
4230 unsigned int prec;
4231
4232 if (shift == LSHIFT_EXPR)
4233 zerobits = ((HOST_WIDE_INT_1U << shiftc) - 1);
4234 else if (shift == RSHIFT_EXPR
4235 && type_has_mode_precision_p (shift_type))
4236 {
4237 prec = TYPE_PRECISION (TREE_TYPE (@3));
4238 tree arg00 = @0;
4239 /* See if more bits can be proven as zero because of
4240 zero extension. */
4241 if (@3 != @0
4242 && TYPE_UNSIGNED (TREE_TYPE (@0)))
4243 {
4244 tree inner_type = TREE_TYPE (@0);
4245 if (type_has_mode_precision_p (inner_type)
4246 && TYPE_PRECISION (inner_type) < prec)
4247 {
4248 prec = TYPE_PRECISION (inner_type);
4249 /* See if we can shorten the right shift. */
4250 if (shiftc < prec)
4251 shift_type = inner_type;
4252 /* Otherwise X >> C1 is all zeros, so we'll optimize
4253 it into (X, 0) later on by making sure zerobits
4254 is all ones. */
4255 }
4256 }
4257 zerobits = HOST_WIDE_INT_M1U;
4258 if (shiftc < prec)
4259 {
4260 zerobits >>= HOST_BITS_PER_WIDE_INT - shiftc;
4261 zerobits <<= prec - shiftc;
4262 }
4263 /* For arithmetic shift if sign bit could be set, zerobits
4264 can contain actually sign bits, so no transformation is
4265 possible, unless MASK masks them all away. In that
4266 case the shift needs to be converted into logical shift. */
4267 if (!TYPE_UNSIGNED (TREE_TYPE (@3))
4268 && prec == TYPE_PRECISION (TREE_TYPE (@3)))
4269 {
4270 if ((mask & zerobits) == 0)
4271 shift_type = unsigned_type_for (TREE_TYPE (@3));
4272 else
4273 zerobits = 0;
4274 }
4275 }
4276 }
4277 /* ((X << 16) & 0xff00) is (X, 0). */
4278 (if ((mask & zerobits) == mask)
4279 { build_int_cst (type, 0); }
4280 (with { newmask = mask | zerobits; }
4281 (if (newmask != mask && (newmask & (newmask + 1)) == 0)
4282 (with
4283 {
4284 /* Only do the transformation if NEWMASK is some integer
4285 mode's mask. */
4286 for (prec = BITS_PER_UNIT;
4287 prec < HOST_BITS_PER_WIDE_INT; prec <<= 1)
4288 if (newmask == (HOST_WIDE_INT_1U << prec) - 1)
4289 break;
4290 }
4291 (if (prec < HOST_BITS_PER_WIDE_INT
4292 || newmask == HOST_WIDE_INT_M1U)
4293 (with
4294 { tree newmaskt = build_int_cst_type (TREE_TYPE (@2), newmask); }
4295 (if (!tree_int_cst_equal (newmaskt, @2))
4296 (if (shift_type != TREE_TYPE (@3))
4297 (bit_and (convert (shift:shift_type (convert @3) @1)) { newmaskt; })
4298 (bit_and @4 { newmaskt; })))))))))))))
4299
4300 /* ((1 << n) & M) != 0 -> n == log2 (M) */
4301 (for cmp (ne eq)
4302 icmp (eq ne)
4303 (simplify
4304 (cmp
4305 (bit_and
4306 (nop_convert? (lshift integer_onep @0)) integer_pow2p@1) integer_zerop)
4307 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)))
4308 (icmp @0 { wide_int_to_tree (TREE_TYPE (@0),
4309 wi::exact_log2 (wi::to_wide (@1))); }))))
4310
4311 /* Fold (X {&,^,|} C2) << C1 into (X << C1) {&,^,|} (C2 << C1)
4312 (X {&,^,|} C2) >> C1 into (X >> C1) & (C2 >> C1). */
4313 (for shift (lshift rshift)
4314 (for bit_op (bit_and bit_xor bit_ior)
4315 (simplify
4316 (shift (convert?:s (bit_op:s @0 INTEGER_CST@2)) INTEGER_CST@1)
4317 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
4318 (with { tree mask = int_const_binop (shift, fold_convert (type, @2), @1); }
4319 (if (mask)
4320 (bit_op (shift (convert @0) @1) { mask; })))))))
4321
4322 /* ~(~X >> Y) -> X >> Y (for arithmetic shift). */
4323 (simplify
4324 (bit_not (convert1?:s (rshift:s (convert2?@0 (bit_not @1)) @2)))
4325 (if (!TYPE_UNSIGNED (TREE_TYPE (@0))
4326 && (element_precision (TREE_TYPE (@0))
4327 <= element_precision (TREE_TYPE (@1))
4328 || !TYPE_UNSIGNED (TREE_TYPE (@1))))
4329 (with
4330 { tree shift_type = TREE_TYPE (@0); }
4331 (convert (rshift (convert:shift_type @1) @2)))))
4332
4333 /* ~(~X >>r Y) -> X >>r Y
4334 ~(~X <<r Y) -> X <<r Y */
4335 (for rotate (lrotate rrotate)
4336 (simplify
4337 (bit_not (convert1?:s (rotate:s (convert2?@0 (bit_not @1)) @2)))
4338 (if ((element_precision (TREE_TYPE (@0))
4339 <= element_precision (TREE_TYPE (@1))
4340 || !TYPE_UNSIGNED (TREE_TYPE (@1)))
4341 && (element_precision (type) <= element_precision (TREE_TYPE (@0))
4342 || !TYPE_UNSIGNED (TREE_TYPE (@0))))
4343 (with
4344 { tree rotate_type = TREE_TYPE (@0); }
4345 (convert (rotate (convert:rotate_type @1) @2))))))
4346
4347 (for cmp (eq ne)
4348 (for rotate (lrotate rrotate)
4349 invrot (rrotate lrotate)
4350 /* (X >>r Y) cmp (Z >>r Y) may simplify to X cmp Y. */
4351 (simplify
4352 (cmp (rotate @1 @0) (rotate @2 @0))
4353 (cmp @1 @2))
4354 /* (X >>r C1) cmp C2 may simplify to X cmp C3. */
4355 (simplify
4356 (cmp (rotate @0 INTEGER_CST@1) INTEGER_CST@2)
4357 (cmp @0 { const_binop (invrot, TREE_TYPE (@0), @2, @1); }))
4358 /* (X >>r Y) cmp C where C is 0 or ~0, may simplify to X cmp C. */
4359 (simplify
4360 (cmp (rotate @0 @1) INTEGER_CST@2)
4361 (if (integer_zerop (@2) || integer_all_onesp (@2))
4362 (cmp @0 @2)))))
4363
4364 /* Narrow a lshift by constant. */
4365 (simplify
4366 (convert (lshift:s@0 @1 INTEGER_CST@2))
4367 (if (INTEGRAL_TYPE_P (type)
4368 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
4369 && !integer_zerop (@2)
4370 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0)))
4371 (if (TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (@0))
4372 || wi::ltu_p (wi::to_wide (@2), TYPE_PRECISION (type)))
4373 (lshift (convert @1) @2)
4374 (if (wi::ltu_p (wi::to_wide (@2), TYPE_PRECISION (TREE_TYPE (@0))))
4375 { build_zero_cst (type); }))))
4376
4377 /* Simplifications of conversions. */
4378
4379 /* Basic strip-useless-type-conversions / strip_nops. */
4380 (for cvt (convert view_convert float fix_trunc)
4381 (simplify
4382 (cvt @0)
4383 (if ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@0)))
4384 || (GENERIC && type == TREE_TYPE (@0)))
4385 @0)))
4386
4387 /* Contract view-conversions. */
4388 (simplify
4389 (view_convert (view_convert @0))
4390 (view_convert @0))
4391
4392 /* For integral conversions with the same precision or pointer
4393 conversions use a NOP_EXPR instead. */
4394 (simplify
4395 (view_convert @0)
4396 (if ((INTEGRAL_TYPE_P (type) || POINTER_TYPE_P (type))
4397 && (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
4398 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (@0)))
4399 (convert @0)))
4400
4401 /* Strip inner integral conversions that do not change precision or size, or
4402 zero-extend while keeping the same size (for bool-to-char). */
4403 (simplify
4404 (view_convert (convert@0 @1))
4405 (if ((INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
4406 && (INTEGRAL_TYPE_P (TREE_TYPE (@1)) || POINTER_TYPE_P (TREE_TYPE (@1)))
4407 && TYPE_SIZE (TREE_TYPE (@0)) == TYPE_SIZE (TREE_TYPE (@1))
4408 && (TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1))
4409 || (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (TREE_TYPE (@1))
4410 && TYPE_UNSIGNED (TREE_TYPE (@1)))))
4411 (view_convert @1)))
4412
4413 /* Simplify a view-converted empty or single-element constructor. */
4414 (simplify
4415 (view_convert CONSTRUCTOR@0)
4416 (with
4417 { tree ctor = (TREE_CODE (@0) == SSA_NAME
4418 ? gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0)) : @0); }
4419 (switch
4420 (if (CONSTRUCTOR_NELTS (ctor) == 0)
4421 { build_zero_cst (type); })
4422 (if (CONSTRUCTOR_NELTS (ctor) == 1
4423 && VECTOR_TYPE_P (TREE_TYPE (ctor))
4424 && operand_equal_p (TYPE_SIZE (type),
4425 TYPE_SIZE (TREE_TYPE
4426 (CONSTRUCTOR_ELT (ctor, 0)->value))))
4427 (view_convert { CONSTRUCTOR_ELT (ctor, 0)->value; })))))
4428
4429 /* Re-association barriers around constants and other re-association
4430 barriers can be removed. */
4431 (simplify
4432 (paren CONSTANT_CLASS_P@0)
4433 @0)
4434 (simplify
4435 (paren (paren@1 @0))
4436 @1)
4437
4438 /* Handle cases of two conversions in a row. */
4439 (for ocvt (convert float fix_trunc)
4440 (for icvt (convert float)
4441 (simplify
4442 (ocvt (icvt@1 @0))
4443 (with
4444 {
4445 tree inside_type = TREE_TYPE (@0);
4446 tree inter_type = TREE_TYPE (@1);
4447 int inside_int = INTEGRAL_TYPE_P (inside_type);
4448 int inside_ptr = POINTER_TYPE_P (inside_type);
4449 int inside_float = FLOAT_TYPE_P (inside_type);
4450 int inside_vec = VECTOR_TYPE_P (inside_type);
4451 unsigned int inside_prec = element_precision (inside_type);
4452 int inside_unsignedp = TYPE_UNSIGNED (inside_type);
4453 int inter_int = INTEGRAL_TYPE_P (inter_type);
4454 int inter_ptr = POINTER_TYPE_P (inter_type);
4455 int inter_float = FLOAT_TYPE_P (inter_type);
4456 int inter_vec = VECTOR_TYPE_P (inter_type);
4457 unsigned int inter_prec = element_precision (inter_type);
4458 int inter_unsignedp = TYPE_UNSIGNED (inter_type);
4459 int final_int = INTEGRAL_TYPE_P (type);
4460 int final_ptr = POINTER_TYPE_P (type);
4461 int final_float = FLOAT_TYPE_P (type);
4462 int final_vec = VECTOR_TYPE_P (type);
4463 unsigned int final_prec = element_precision (type);
4464 int final_unsignedp = TYPE_UNSIGNED (type);
4465 }
4466 (switch
4467 /* In addition to the cases of two conversions in a row
4468 handled below, if we are converting something to its own
4469 type via an object of identical or wider precision, neither
4470 conversion is needed. */
4471 (if (((GIMPLE && useless_type_conversion_p (type, inside_type))
4472 || (GENERIC
4473 && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (inside_type)))
4474 && (((inter_int || inter_ptr) && final_int)
4475 || (inter_float && final_float))
4476 && inter_prec >= final_prec)
4477 (ocvt @0))
4478
4479 /* Likewise, if the intermediate and initial types are either both
4480 float or both integer, we don't need the middle conversion if the
4481 former is wider than the latter and doesn't change the signedness
4482 (for integers). Avoid this if the final type is a pointer since
4483 then we sometimes need the middle conversion. */
4484 (if (((inter_int && inside_int) || (inter_float && inside_float))
4485 && (final_int || final_float)
4486 && inter_prec >= inside_prec
4487 && (inter_float || inter_unsignedp == inside_unsignedp))
4488 (ocvt @0))
4489
4490 /* If we have a sign-extension of a zero-extended value, we can
4491 replace that by a single zero-extension. Likewise if the
4492 final conversion does not change precision we can drop the
4493 intermediate conversion. */
4494 (if (inside_int && inter_int && final_int
4495 && ((inside_prec < inter_prec && inter_prec < final_prec
4496 && inside_unsignedp && !inter_unsignedp)
4497 || final_prec == inter_prec))
4498 (ocvt @0))
4499
4500 /* Two conversions in a row are not needed unless:
4501 - some conversion is floating-point (overstrict for now), or
4502 - some conversion is a vector (overstrict for now), or
4503 - the intermediate type is narrower than both initial and
4504 final, or
4505 - the intermediate type and innermost type differ in signedness,
4506 and the outermost type is wider than the intermediate, or
4507 - the initial type is a pointer type and the precisions of the
4508 intermediate and final types differ, or
4509 - the final type is a pointer type and the precisions of the
4510 initial and intermediate types differ. */
4511 (if (! inside_float && ! inter_float && ! final_float
4512 && ! inside_vec && ! inter_vec && ! final_vec
4513 && (inter_prec >= inside_prec || inter_prec >= final_prec)
4514 && ! (inside_int && inter_int
4515 && inter_unsignedp != inside_unsignedp
4516 && inter_prec < final_prec)
4517 && ((inter_unsignedp && inter_prec > inside_prec)
4518 == (final_unsignedp && final_prec > inter_prec))
4519 && ! (inside_ptr && inter_prec != final_prec)
4520 && ! (final_ptr && inside_prec != inter_prec))
4521 (ocvt @0))
4522
4523 /* `(outer:M)(inter:N) a:O`
4524 can be converted to `(outer:M) a`
4525 if M <= O && N >= O. No matter what signedness of the casts,
4526 as the final is either a truncation from the original or just
4527 a sign change of the type. */
4528 (if (inside_int && inter_int && final_int
4529 && final_prec <= inside_prec
4530 && inter_prec >= inside_prec)
4531 (convert @0))
4532
4533 /* A truncation to an unsigned type (a zero-extension) should be
4534 canonicalized as bitwise and of a mask. */
4535 (if (GIMPLE /* PR70366: doing this in GENERIC breaks -Wconversion. */
4536 && final_int && inter_int && inside_int
4537 && final_prec == inside_prec
4538 && final_prec > inter_prec
4539 && inter_unsignedp)
4540 (convert (bit_and @0 { wide_int_to_tree
4541 (inside_type,
4542 wi::mask (inter_prec, false,
4543 TYPE_PRECISION (inside_type))); })))
4544
4545 /* If we are converting an integer to a floating-point that can
4546 represent it exactly and back to an integer, we can skip the
4547 floating-point conversion. */
4548 (if (GIMPLE /* PR66211 */
4549 && inside_int && inter_float && final_int &&
4550 (unsigned) significand_size (TYPE_MODE (inter_type))
4551 >= inside_prec - !inside_unsignedp)
4552 (convert @0)))))))
4553
4554 /* (float_type)(integer_type) x -> trunc (x) if the type of x matches
4555 float_type. Only do the transformation if we do not need to preserve
4556 trapping behaviour, so require !flag_trapping_math. */
4557 #if GIMPLE
4558 (simplify
4559 (float (fix_trunc @0))
4560 (if (!flag_trapping_math
4561 && types_match (type, TREE_TYPE (@0))
4562 && direct_internal_fn_supported_p (IFN_TRUNC, type,
4563 OPTIMIZE_FOR_BOTH))
4564 (IFN_TRUNC @0)))
4565 #endif
4566
4567 /* If we have a narrowing conversion to an integral type that is fed by a
4568 BIT_AND_EXPR, we might be able to remove the BIT_AND_EXPR if it merely
4569 masks off bits outside the final type (and nothing else). */
4570 (simplify
4571 (convert (bit_and @0 INTEGER_CST@1))
4572 (if (INTEGRAL_TYPE_P (type)
4573 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
4574 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))
4575 && operand_equal_p (@1, build_low_bits_mask (TREE_TYPE (@1),
4576 TYPE_PRECISION (type)), 0))
4577 (convert @0)))
4578
4579
4580 /* (X /[ex] A) * A -> X. */
4581 (simplify
4582 (mult (convert1? (exact_div @0 @@1)) (convert2? @1))
4583 (convert @0))
4584
4585 /* Simplify (A / B) * B + (A % B) -> A. */
4586 (for div (trunc_div ceil_div floor_div round_div)
4587 mod (trunc_mod ceil_mod floor_mod round_mod)
4588 (simplify
4589 (plus:c (mult:c (div @0 @1) @1) (mod @0 @1))
4590 @0))
4591
4592 /* x / y * y == x -> x % y == 0. */
4593 (simplify
4594 (eq:c (mult:c (trunc_div:s @0 @1) @1) @0)
4595 (if (TREE_CODE (TREE_TYPE (@0)) != COMPLEX_TYPE)
4596 (eq (trunc_mod @0 @1) { build_zero_cst (TREE_TYPE (@0)); })))
4597
4598 /* ((X /[ex] A) +- B) * A --> X +- A * B. */
4599 (for op (plus minus)
4600 (simplify
4601 (mult (convert1? (op (convert2? (exact_div @0 INTEGER_CST@@1)) INTEGER_CST@2)) @1)
4602 (if (tree_nop_conversion_p (type, TREE_TYPE (@2))
4603 && tree_nop_conversion_p (TREE_TYPE (@0), TREE_TYPE (@2)))
4604 (with
4605 {
4606 wi::overflow_type overflow;
4607 wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@2),
4608 TYPE_SIGN (type), &overflow);
4609 }
4610 (if (types_match (type, TREE_TYPE (@2))
4611 && types_match (TREE_TYPE (@0), TREE_TYPE (@2)) && !overflow)
4612 (op @0 { wide_int_to_tree (type, mul); })
4613 (with { tree utype = unsigned_type_for (type); }
4614 (convert (op (convert:utype @0)
4615 (mult (convert:utype @1) (convert:utype @2))))))))))
4616
4617 /* Canonicalization of binary operations. */
4618
4619 /* Convert X + -C into X - C. */
4620 (simplify
4621 (plus @0 REAL_CST@1)
4622 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
4623 (with { tree tem = const_unop (NEGATE_EXPR, type, @1); }
4624 (if (!TREE_OVERFLOW (tem) || !flag_trapping_math)
4625 (minus @0 { tem; })))))
4626
4627 /* Convert x+x into x*2. */
4628 (simplify
4629 (plus @0 @0)
4630 (if (SCALAR_FLOAT_TYPE_P (type))
4631 (mult @0 { build_real (type, dconst2); })
4632 (if (INTEGRAL_TYPE_P (type))
4633 (mult @0 { build_int_cst (type, 2); }))))
4634
4635 /* 0 - X -> -X. */
4636 (simplify
4637 (minus integer_zerop @1)
4638 (negate @1))
4639 (simplify
4640 (pointer_diff integer_zerop @1)
4641 (negate (convert @1)))
4642
4643 /* (ARG0 - ARG1) is the same as (-ARG1 + ARG0). So check whether
4644 ARG0 is zero and X + ARG0 reduces to X, since that would mean
4645 (-ARG1 + ARG0) reduces to -ARG1. */
4646 (simplify
4647 (minus real_zerop@0 @1)
4648 (if (fold_real_zero_addition_p (type, @1, @0, 0))
4649 (negate @1)))
4650
4651 /* Transform x * -1 into -x. */
4652 (simplify
4653 (mult @0 integer_minus_onep)
4654 (negate @0))
4655
4656 /* Reassociate (X * CST) * Y to (X * Y) * CST. This does not introduce
4657 signed overflow for CST != 0 && CST != -1. */
4658 (simplify
4659 (mult:c (mult:s@3 @0 INTEGER_CST@1) @2)
4660 (if (TREE_CODE (@2) != INTEGER_CST
4661 && single_use (@3)
4662 && !integer_zerop (@1) && !integer_minus_onep (@1))
4663 (mult (mult @0 @2) @1)))
4664
4665 /* True if we can easily extract the real and imaginary parts of a complex
4666 number. */
4667 (match compositional_complex
4668 (convert? (complex @0 @1)))
4669
4670 /* COMPLEX_EXPR and REALPART/IMAGPART_EXPR cancellations. */
4671 (simplify
4672 (complex (realpart @0) (imagpart @0))
4673 @0)
4674 (simplify
4675 (realpart (complex @0 @1))
4676 @0)
4677 (simplify
4678 (imagpart (complex @0 @1))
4679 @1)
4680
4681 /* Sometimes we only care about half of a complex expression. */
4682 (simplify
4683 (realpart (convert?:s (conj:s @0)))
4684 (convert (realpart @0)))
4685 (simplify
4686 (imagpart (convert?:s (conj:s @0)))
4687 (convert (negate (imagpart @0))))
4688 (for part (realpart imagpart)
4689 (for op (plus minus)
4690 (simplify
4691 (part (convert?:s@2 (op:s @0 @1)))
4692 (convert (op (part @0) (part @1))))))
4693 (simplify
4694 (realpart (convert?:s (CEXPI:s @0)))
4695 (convert (COS @0)))
4696 (simplify
4697 (imagpart (convert?:s (CEXPI:s @0)))
4698 (convert (SIN @0)))
4699
4700 /* conj(conj(x)) -> x */
4701 (simplify
4702 (conj (convert? (conj @0)))
4703 (if (tree_nop_conversion_p (TREE_TYPE (@0), type))
4704 (convert @0)))
4705
4706 /* conj({x,y}) -> {x,-y} */
4707 (simplify
4708 (conj (convert?:s (complex:s @0 @1)))
4709 (with { tree itype = TREE_TYPE (type); }
4710 (complex (convert:itype @0) (negate (convert:itype @1)))))
4711
4712 /* BSWAP simplifications, transforms checked by gcc.dg/builtin-bswap-8.c. */
4713 (for bswap (BSWAP)
4714 (simplify
4715 (bswap (bswap @0))
4716 @0)
4717 (simplify
4718 (bswap (bit_not (bswap @0)))
4719 (bit_not @0))
4720 (for bitop (bit_xor bit_ior bit_and)
4721 (simplify
4722 (bswap (bitop:c (bswap @0) @1))
4723 (bitop @0 (bswap @1))))
4724 (for cmp (eq ne)
4725 (simplify
4726 (cmp (bswap@2 @0) (bswap @1))
4727 (with { tree ctype = TREE_TYPE (@2); }
4728 (cmp (convert:ctype @0) (convert:ctype @1))))
4729 (simplify
4730 (cmp (bswap @0) INTEGER_CST@1)
4731 (with { tree ctype = TREE_TYPE (@1); }
4732 (cmp (convert:ctype @0) (bswap! @1)))))
4733 /* (bswap(x) >> C1) & C2 can sometimes be simplified to (x >> C3) & C2. */
4734 (simplify
4735 (bit_and (convert1? (rshift@0 (convert2? (bswap@4 @1)) INTEGER_CST@2))
4736 INTEGER_CST@3)
4737 (if (BITS_PER_UNIT == 8
4738 && tree_fits_uhwi_p (@2)
4739 && tree_fits_uhwi_p (@3))
4740 (with
4741 {
4742 unsigned HOST_WIDE_INT prec = TYPE_PRECISION (TREE_TYPE (@4));
4743 unsigned HOST_WIDE_INT bits = tree_to_uhwi (@2);
4744 unsigned HOST_WIDE_INT mask = tree_to_uhwi (@3);
4745 unsigned HOST_WIDE_INT lo = bits & 7;
4746 unsigned HOST_WIDE_INT hi = bits - lo;
4747 }
4748 (if (bits < prec
4749 && mask < (256u>>lo)
4750 && bits < TYPE_PRECISION (TREE_TYPE(@0)))
4751 (with { unsigned HOST_WIDE_INT ns = (prec - (hi + 8)) + lo; }
4752 (if (ns == 0)
4753 (bit_and (convert @1) @3)
4754 (with
4755 {
4756 tree utype = unsigned_type_for (TREE_TYPE (@1));
4757 tree nst = build_int_cst (integer_type_node, ns);
4758 }
4759 (bit_and (convert (rshift:utype (convert:utype @1) {nst;})) @3))))))))
4760 /* bswap(x) >> C1 can sometimes be simplified to (T)x >> C2. */
4761 (simplify
4762 (rshift (convert? (bswap@2 @0)) INTEGER_CST@1)
4763 (if (BITS_PER_UNIT == 8
4764 && CHAR_TYPE_SIZE == 8
4765 && tree_fits_uhwi_p (@1))
4766 (with
4767 {
4768 unsigned HOST_WIDE_INT prec = TYPE_PRECISION (TREE_TYPE (@2));
4769 unsigned HOST_WIDE_INT bits = tree_to_uhwi (@1);
4770 /* If the bswap was extended before the original shift, this
4771 byte (shift) has the sign of the extension, not the sign of
4772 the original shift. */
4773 tree st = TYPE_PRECISION (type) > prec ? TREE_TYPE (@2) : type;
4774 }
4775 /* Special case: logical right shift of sign-extended bswap.
4776 (unsigned)(short)bswap16(x)>>12 is (unsigned)((short)x<<8)>>12. */
4777 (if (TYPE_PRECISION (type) > prec
4778 && !TYPE_UNSIGNED (TREE_TYPE (@2))
4779 && TYPE_UNSIGNED (type)
4780 && bits < prec && bits + 8 >= prec)
4781 (with { tree nst = build_int_cst (integer_type_node, prec - 8); }
4782 (rshift (convert (lshift:st (convert:st @0) {nst;})) @1))
4783 (if (bits + 8 == prec)
4784 (if (TYPE_UNSIGNED (st))
4785 (convert (convert:unsigned_char_type_node @0))
4786 (convert (convert:signed_char_type_node @0)))
4787 (if (bits < prec && bits + 8 > prec)
4788 (with
4789 {
4790 tree nst = build_int_cst (integer_type_node, bits & 7);
4791 tree bt = TYPE_UNSIGNED (st) ? unsigned_char_type_node
4792 : signed_char_type_node;
4793 }
4794 (convert (rshift:bt (convert:bt @0) {nst;})))))))))
4795 /* bswap(x) & C1 can sometimes be simplified to (x >> C2) & C1. */
4796 (simplify
4797 (bit_and (convert? (bswap@2 @0)) INTEGER_CST@1)
4798 (if (BITS_PER_UNIT == 8
4799 && tree_fits_uhwi_p (@1)
4800 && tree_to_uhwi (@1) < 256)
4801 (with
4802 {
4803 unsigned HOST_WIDE_INT prec = TYPE_PRECISION (TREE_TYPE (@2));
4804 tree utype = unsigned_type_for (TREE_TYPE (@0));
4805 tree nst = build_int_cst (integer_type_node, prec - 8);
4806 }
4807 (bit_and (convert (rshift:utype (convert:utype @0) {nst;})) @1)))))
4808
4809
4810 /* Combine COND_EXPRs and VEC_COND_EXPRs. */
4811
4812 /* Simplify constant conditions.
4813 Only optimize constant conditions when the selected branch
4814 has the same type as the COND_EXPR. This avoids optimizing
4815 away "c ? x : throw", where the throw has a void type.
4816 Note that we cannot throw away the fold-const.cc variant nor
4817 this one as we depend on doing this transform before possibly
4818 A ? B : B -> B triggers and the fold-const.cc one can optimize
4819 0 ? A : B to B even if A has side-effects. Something
4820 genmatch cannot handle. */
4821 (simplify
4822 (cond INTEGER_CST@0 @1 @2)
4823 (if (integer_zerop (@0))
4824 (if (!VOID_TYPE_P (TREE_TYPE (@2)) || VOID_TYPE_P (type))
4825 @2)
4826 (if (!VOID_TYPE_P (TREE_TYPE (@1)) || VOID_TYPE_P (type))
4827 @1)))
4828 (simplify
4829 (vec_cond VECTOR_CST@0 @1 @2)
4830 (if (integer_all_onesp (@0))
4831 @1
4832 (if (integer_zerop (@0))
4833 @2)))
4834
4835 /* Sink unary operations to branches, but only if we do fold both. */
4836 (for op (negate bit_not abs absu)
4837 (simplify
4838 (op (vec_cond:s @0 @1 @2))
4839 (vec_cond @0 (op! @1) (op! @2))))
4840
4841 /* Sink unary conversions to branches, but only if we do fold both
4842 and the target's truth type is the same as we already have. */
4843 (simplify
4844 (convert (vec_cond:s @0 @1 @2))
4845 (if (VECTOR_TYPE_P (type)
4846 && types_match (TREE_TYPE (@0), truth_type_for (type)))
4847 (vec_cond @0 (convert! @1) (convert! @2))))
4848
4849 /* Likewise for view_convert of nop_conversions. */
4850 (simplify
4851 (view_convert (vec_cond:s @0 @1 @2))
4852 (if (VECTOR_TYPE_P (type) && VECTOR_TYPE_P (TREE_TYPE (@1))
4853 && known_eq (TYPE_VECTOR_SUBPARTS (type),
4854 TYPE_VECTOR_SUBPARTS (TREE_TYPE (@1)))
4855 && tree_nop_conversion_p (TREE_TYPE (type), TREE_TYPE (TREE_TYPE (@1))))
4856 (vec_cond @0 (view_convert! @1) (view_convert! @2))))
4857
4858 /* Sink binary operation to branches, but only if we can fold it. */
4859 (for op (tcc_comparison plus minus mult bit_and bit_ior bit_xor
4860 lshift rshift rdiv trunc_div ceil_div floor_div round_div
4861 trunc_mod ceil_mod floor_mod round_mod min max)
4862 /* (c ? a : b) op (c ? d : e) --> c ? (a op d) : (b op e) */
4863 (simplify
4864 (op (vec_cond:s @0 @1 @2) (vec_cond:s @0 @3 @4))
4865 (vec_cond @0 (op! @1 @3) (op! @2 @4)))
4866
4867 /* (c ? a : b) op d --> c ? (a op d) : (b op d) */
4868 (simplify
4869 (op (vec_cond:s @0 @1 @2) @3)
4870 (vec_cond @0 (op! @1 @3) (op! @2 @3)))
4871 (simplify
4872 (op @3 (vec_cond:s @0 @1 @2))
4873 (vec_cond @0 (op! @3 @1) (op! @3 @2))))
4874
4875 #if GIMPLE
4876 (match (nop_atomic_bit_test_and_p @0 @1 @4)
4877 (bit_and (convert?@4 (ATOMIC_FETCH_OR_XOR_N @2 INTEGER_CST@0 @3))
4878 INTEGER_CST@1)
4879 (with {
4880 int ibit = tree_log2 (@0);
4881 int ibit2 = tree_log2 (@1);
4882 }
4883 (if (ibit == ibit2
4884 && ibit >= 0
4885 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))))))
4886
4887 (match (nop_atomic_bit_test_and_p @0 @1 @3)
4888 (bit_and (convert?@3 (SYNC_FETCH_OR_XOR_N @2 INTEGER_CST@0))
4889 INTEGER_CST@1)
4890 (with {
4891 int ibit = tree_log2 (@0);
4892 int ibit2 = tree_log2 (@1);
4893 }
4894 (if (ibit == ibit2
4895 && ibit >= 0
4896 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))))))
4897
4898 (match (nop_atomic_bit_test_and_p @0 @0 @4)
4899 (bit_and:c
4900 (convert1?@4
4901 (ATOMIC_FETCH_OR_XOR_N @2 (nop_convert? (lshift@0 integer_onep@5 @6)) @3))
4902 (convert2? @0))
4903 (if (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0)))))
4904
4905 (match (nop_atomic_bit_test_and_p @0 @0 @4)
4906 (bit_and:c
4907 (convert1?@4
4908 (SYNC_FETCH_OR_XOR_N @2 (nop_convert? (lshift@0 integer_onep@3 @5))))
4909 (convert2? @0))
4910 (if (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0)))))
4911
4912 (match (nop_atomic_bit_test_and_p @0 @1 @3)
4913 (bit_and@4 (convert?@3 (ATOMIC_FETCH_AND_N @2 INTEGER_CST@0 @5))
4914 INTEGER_CST@1)
4915 (with {
4916 int ibit = wi::exact_log2 (wi::zext (wi::bit_not (wi::to_wide (@0)),
4917 TYPE_PRECISION(type)));
4918 int ibit2 = tree_log2 (@1);
4919 }
4920 (if (ibit == ibit2
4921 && ibit >= 0
4922 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))))))
4923
4924 (match (nop_atomic_bit_test_and_p @0 @1 @3)
4925 (bit_and@4
4926 (convert?@3 (SYNC_FETCH_AND_AND_N @2 INTEGER_CST@0))
4927 INTEGER_CST@1)
4928 (with {
4929 int ibit = wi::exact_log2 (wi::zext (wi::bit_not (wi::to_wide (@0)),
4930 TYPE_PRECISION(type)));
4931 int ibit2 = tree_log2 (@1);
4932 }
4933 (if (ibit == ibit2
4934 && ibit >= 0
4935 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))))))
4936
4937 (match (nop_atomic_bit_test_and_p @4 @0 @3)
4938 (bit_and:c
4939 (convert1?@3
4940 (ATOMIC_FETCH_AND_N @2 (nop_convert?@4 (bit_not (lshift@0 integer_onep@6 @7))) @5))
4941 (convert2? @0))
4942 (if (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@4)))))
4943
4944 (match (nop_atomic_bit_test_and_p @4 @0 @3)
4945 (bit_and:c
4946 (convert1?@3
4947 (SYNC_FETCH_AND_AND_N @2 (nop_convert?@4 (bit_not (lshift@0 integer_onep@6 @7)))))
4948 (convert2? @0))
4949 (if (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@4)))))
4950
4951 #endif
4952
4953 /* (v ? w : 0) ? a : b is just (v & w) ? a : b
4954 Currently disabled after pass lvec because ARM understands
4955 VEC_COND_EXPR<v==w,-1,0> but not a plain v==w fed to BIT_IOR_EXPR. */
4956 (simplify
4957 (vec_cond (vec_cond:s @0 @3 integer_zerop) @1 @2)
4958 (if (optimize_vectors_before_lowering_p () && types_match (@0, @3))
4959 (vec_cond (bit_and @0 @3) @1 @2)))
4960 (simplify
4961 (vec_cond (vec_cond:s @0 integer_all_onesp @3) @1 @2)
4962 (if (optimize_vectors_before_lowering_p () && types_match (@0, @3))
4963 (vec_cond (bit_ior @0 @3) @1 @2)))
4964 (simplify
4965 (vec_cond (vec_cond:s @0 integer_zerop @3) @1 @2)
4966 (if (optimize_vectors_before_lowering_p () && types_match (@0, @3))
4967 (vec_cond (bit_ior @0 (bit_not @3)) @2 @1)))
4968 (simplify
4969 (vec_cond (vec_cond:s @0 @3 integer_all_onesp) @1 @2)
4970 (if (optimize_vectors_before_lowering_p () && types_match (@0, @3))
4971 (vec_cond (bit_and @0 (bit_not @3)) @2 @1)))
4972
4973 /* c1 ? c2 ? a : b : b --> (c1 & c2) ? a : b */
4974 (simplify
4975 (vec_cond @0 (vec_cond:s @1 @2 @3) @3)
4976 (if (optimize_vectors_before_lowering_p () && types_match (@0, @1))
4977 (vec_cond (bit_and @0 @1) @2 @3)))
4978 (simplify
4979 (vec_cond @0 @2 (vec_cond:s @1 @2 @3))
4980 (if (optimize_vectors_before_lowering_p () && types_match (@0, @1))
4981 (vec_cond (bit_ior @0 @1) @2 @3)))
4982 (simplify
4983 (vec_cond @0 (vec_cond:s @1 @2 @3) @2)
4984 (if (optimize_vectors_before_lowering_p () && types_match (@0, @1))
4985 (vec_cond (bit_ior (bit_not @0) @1) @2 @3)))
4986 (simplify
4987 (vec_cond @0 @3 (vec_cond:s @1 @2 @3))
4988 (if (optimize_vectors_before_lowering_p () && types_match (@0, @1))
4989 (vec_cond (bit_and (bit_not @0) @1) @2 @3)))
4990
4991 /* Canonicalize mask ? { 0, ... } : { -1, ...} to ~mask if the mask
4992 types are compatible. */
4993 (simplify
4994 (vec_cond @0 VECTOR_CST@1 VECTOR_CST@2)
4995 (if (VECTOR_BOOLEAN_TYPE_P (type)
4996 && types_match (type, TREE_TYPE (@0)))
4997 (if (integer_zerop (@1) && integer_all_onesp (@2))
4998 (bit_not @0)
4999 (if (integer_all_onesp (@1) && integer_zerop (@2))
5000 @0))))
5001
5002 /* A few simplifications of "a ? CST1 : CST2". */
5003 /* NOTE: Only do this on gimple as the if-chain-to-switch
5004 optimization depends on the gimple to have if statements in it. */
5005 #if GIMPLE
5006 (simplify
5007 (cond @0 INTEGER_CST@1 INTEGER_CST@2)
5008 (switch
5009 (if (integer_zerop (@2))
5010 (switch
5011 /* a ? 1 : 0 -> a if 0 and 1 are integral types. */
5012 (if (integer_onep (@1))
5013 (convert (convert:boolean_type_node @0)))
5014 /* a ? powerof2cst : 0 -> a << (log2(powerof2cst)) */
5015 (if (INTEGRAL_TYPE_P (type) && integer_pow2p (@1))
5016 (with {
5017 tree shift = build_int_cst (integer_type_node, tree_log2 (@1));
5018 }
5019 (lshift (convert (convert:boolean_type_node @0)) { shift; })))
5020 /* a ? -1 : 0 -> -a. No need to check the TYPE_PRECISION not being 1
5021 here as the powerof2cst case above will handle that case correctly. */
5022 (if (INTEGRAL_TYPE_P (type) && integer_all_onesp (@1))
5023 (with {
5024 auto prec = TYPE_PRECISION (type);
5025 auto unsign = TYPE_UNSIGNED (type);
5026 tree inttype = build_nonstandard_integer_type (prec, unsign);
5027 }
5028 (convert (negate (convert:inttype (convert:boolean_type_node @0))))))))
5029 (if (integer_zerop (@1))
5030 (with {
5031 tree booltrue = constant_boolean_node (true, boolean_type_node);
5032 }
5033 (switch
5034 /* a ? 0 : 1 -> !a. */
5035 (if (integer_onep (@2))
5036 (convert (bit_xor (convert:boolean_type_node @0) { booltrue; } )))
5037 /* a ? powerof2cst : 0 -> (!a) << (log2(powerof2cst)) */
5038 (if (INTEGRAL_TYPE_P (type) && integer_pow2p (@2))
5039 (with {
5040 tree shift = build_int_cst (integer_type_node, tree_log2 (@2));
5041 }
5042 (lshift (convert (bit_xor (convert:boolean_type_node @0) { booltrue; } ))
5043 { shift; })))
5044 /* a ? -1 : 0 -> -(!a). No need to check the TYPE_PRECISION not being 1
5045 here as the powerof2cst case above will handle that case correctly. */
5046 (if (INTEGRAL_TYPE_P (type) && integer_all_onesp (@2))
5047 (with {
5048 auto prec = TYPE_PRECISION (type);
5049 auto unsign = TYPE_UNSIGNED (type);
5050 tree inttype = build_nonstandard_integer_type (prec, unsign);
5051 }
5052 (convert
5053 (negate
5054 (convert:inttype
5055 (bit_xor (convert:boolean_type_node @0) { booltrue; } )
5056 )
5057 )
5058 )
5059 )
5060 )
5061 )
5062 )
5063 )
5064 )
5065 )
5066
5067 /* (a > 1) ? 0 : (cast)a is the same as (cast)(a == 1)
5068 for unsigned types. */
5069 (simplify
5070 (cond (gt @0 integer_onep@1) integer_zerop (convert? @2))
5071 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
5072 && bitwise_equal_p (@0, @2))
5073 (convert (eq @0 @1))
5074 )
5075 )
5076
5077 /* (a <= 1) & (cast)a is the same as (cast)(a == 1)
5078 for unsigned types. */
5079 (simplify
5080 (bit_and:c (convert1? (le @0 integer_onep@1)) (convert2? @2))
5081 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
5082 && bitwise_equal_p (@0, @2))
5083 (convert (eq @0 @1))
5084 )
5085 )
5086
5087 /* Optimize
5088 # x_5 in range [cst1, cst2] where cst2 = cst1 + 1
5089 x_5 ? cstN ? cst4 : cst3
5090 # op is == or != and N is 1 or 2
5091 to r_6 = x_5 + (min (cst3, cst4) - cst1) or
5092 r_6 = (min (cst3, cst4) + cst1) - x_5 depending on op, N and which
5093 of cst3 and cst4 is smaller.
5094 This was originally done by two_value_replacement in phiopt (PR 88676). */
5095 (for eqne (ne eq)
5096 (simplify
5097 (cond (eqne SSA_NAME@0 INTEGER_CST@1) INTEGER_CST@2 INTEGER_CST@3)
5098 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
5099 && INTEGRAL_TYPE_P (type)
5100 && (wi::to_widest (@2) + 1 == wi::to_widest (@3)
5101 || wi::to_widest (@2) == wi::to_widest (@3) + 1))
5102 (with {
5103 value_range r;
5104 get_range_query (cfun)->range_of_expr (r, @0);
5105 if (r.undefined_p ())
5106 r.set_varying (TREE_TYPE (@0));
5107
5108 wide_int min = r.lower_bound ();
5109 wide_int max = r.upper_bound ();
5110 }
5111 (if (min + 1 == max
5112 && (wi::to_wide (@1) == min
5113 || wi::to_wide (@1) == max))
5114 (with {
5115 tree arg0 = @2, arg1 = @3;
5116 tree type1;
5117 if ((eqne == EQ_EXPR) ^ (wi::to_wide (@1) == min))
5118 std::swap (arg0, arg1);
5119 if (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type))
5120 type1 = TREE_TYPE (@0);
5121 else
5122 type1 = type;
5123 auto prec = TYPE_PRECISION (type1);
5124 auto unsign = TYPE_UNSIGNED (type1);
5125 type1 = build_nonstandard_integer_type (prec, unsign);
5126 min = wide_int::from (min, prec,
5127 TYPE_SIGN (TREE_TYPE (@0)));
5128 wide_int a = wide_int::from (wi::to_wide (arg0), prec,
5129 TYPE_SIGN (type));
5130 enum tree_code code;
5131 wi::overflow_type ovf;
5132 if (tree_int_cst_lt (arg0, arg1))
5133 {
5134 code = PLUS_EXPR;
5135 a -= min;
5136 if (!unsign)
5137 {
5138 /* lhs is known to be in range [min, min+1] and we want to add a
5139 to it. Check if that operation can overflow for those 2 values
5140 and if yes, force unsigned type. */
5141 wi::add (min + (wi::neg_p (a) ? 0 : 1), a, SIGNED, &ovf);
5142 if (ovf)
5143 type1 = unsigned_type_for (type1);
5144 }
5145 }
5146 else
5147 {
5148 code = MINUS_EXPR;
5149 a += min;
5150 if (!unsign)
5151 {
5152 /* lhs is known to be in range [min, min+1] and we want to subtract
5153 it from a. Check if that operation can overflow for those 2
5154 values and if yes, force unsigned type. */
5155 wi::sub (a, min + (wi::neg_p (min) ? 0 : 1), SIGNED, &ovf);
5156 if (ovf)
5157 type1 = unsigned_type_for (type1);
5158 }
5159 }
5160 tree arg = wide_int_to_tree (type1, a);
5161 }
5162 (if (code == PLUS_EXPR)
5163 (convert (plus (convert:type1 @0) { arg; }))
5164 (convert (minus { arg; } (convert:type1 @0)))
5165 )
5166 )
5167 )
5168 )
5169 )
5170 )
5171 )
5172 #endif
5173
5174 (simplify
5175 (convert (cond@0 @1 INTEGER_CST@2 INTEGER_CST@3))
5176 (if (INTEGRAL_TYPE_P (type)
5177 && INTEGRAL_TYPE_P (TREE_TYPE (@0)))
5178 (cond @1 (convert @2) (convert @3))))
5179
5180 /* Simplification moved from fold_cond_expr_with_comparison. It may also
5181 be extended. */
5182 /* This pattern implements two kinds simplification:
5183
5184 Case 1)
5185 (cond (cmp (convert1? x) c1) (convert2? x) c2) -> (minmax (x c)) if:
5186 1) Conversions are type widening from smaller type.
5187 2) Const c1 equals to c2 after canonicalizing comparison.
5188 3) Comparison has tree code LT, LE, GT or GE.
5189 This specific pattern is needed when (cmp (convert x) c) may not
5190 be simplified by comparison patterns because of multiple uses of
5191 x. It also makes sense here because simplifying across multiple
5192 referred var is always benefitial for complicated cases.
5193
5194 Case 2)
5195 (cond (eq (convert1? x) c1) (convert2? x) c2) -> (cond (eq x c1) c1 c2). */
5196 (for cmp (lt le gt ge eq ne)
5197 (simplify
5198 (cond (cmp (convert1? @1) INTEGER_CST@3) (convert2? @1) INTEGER_CST@2)
5199 (with
5200 {
5201 tree from_type = TREE_TYPE (@1);
5202 tree c1_type = TREE_TYPE (@3), c2_type = TREE_TYPE (@2);
5203 enum tree_code code = ERROR_MARK;
5204
5205 if (INTEGRAL_TYPE_P (from_type)
5206 && int_fits_type_p (@2, from_type)
5207 && (types_match (c1_type, from_type)
5208 || (TYPE_PRECISION (c1_type) > TYPE_PRECISION (from_type)
5209 && (TYPE_UNSIGNED (from_type)
5210 || TYPE_SIGN (c1_type) == TYPE_SIGN (from_type))))
5211 && (types_match (c2_type, from_type)
5212 || (TYPE_PRECISION (c2_type) > TYPE_PRECISION (from_type)
5213 && (TYPE_UNSIGNED (from_type)
5214 || TYPE_SIGN (c2_type) == TYPE_SIGN (from_type)))))
5215 {
5216 if (cmp != EQ_EXPR)
5217 code = minmax_from_comparison (cmp, @1, @3, @1, @2);
5218 /* Can do A == C1 ? A : C2 -> A == C1 ? C1 : C2? */
5219 else if (int_fits_type_p (@3, from_type))
5220 code = EQ_EXPR;
5221 }
5222 }
5223 (if (code == MAX_EXPR)
5224 (convert (max @1 (convert @2)))
5225 (if (code == MIN_EXPR)
5226 (convert (min @1 (convert @2)))
5227 (if (code == EQ_EXPR)
5228 (convert (cond (eq @1 (convert @3))
5229 (convert:from_type @3) (convert:from_type @2)))))))))
5230
5231 /* (cond (cmp (convert? x) c1) (op x c2) c3) -> (op (minmax x c1) c2) if:
5232
5233 1) OP is PLUS or MINUS.
5234 2) CMP is LT, LE, GT or GE.
5235 3) C3 == (C1 op C2), and computation doesn't have undefined behavior.
5236
5237 This pattern also handles special cases like:
5238
5239 A) Operand x is a unsigned to signed type conversion and c1 is
5240 integer zero. In this case,
5241 (signed type)x < 0 <=> x > MAX_VAL(signed type)
5242 (signed type)x >= 0 <=> x <= MAX_VAL(signed type)
5243 B) Const c1 may not equal to (C3 op' C2). In this case we also
5244 check equality for (c1+1) and (c1-1) by adjusting comparison
5245 code.
5246
5247 TODO: Though signed type is handled by this pattern, it cannot be
5248 simplified at the moment because C standard requires additional
5249 type promotion. In order to match&simplify it here, the IR needs
5250 to be cleaned up by other optimizers, i.e, VRP. */
5251 (for op (plus minus)
5252 (for cmp (lt le gt ge)
5253 (simplify
5254 (cond (cmp (convert? @X) INTEGER_CST@1) (op @X INTEGER_CST@2) INTEGER_CST@3)
5255 (with { tree from_type = TREE_TYPE (@X), to_type = TREE_TYPE (@1); }
5256 (if (types_match (from_type, to_type)
5257 /* Check if it is special case A). */
5258 || (TYPE_UNSIGNED (from_type)
5259 && !TYPE_UNSIGNED (to_type)
5260 && TYPE_PRECISION (from_type) == TYPE_PRECISION (to_type)
5261 && integer_zerop (@1)
5262 && (cmp == LT_EXPR || cmp == GE_EXPR)))
5263 (with
5264 {
5265 wi::overflow_type overflow = wi::OVF_NONE;
5266 enum tree_code code, cmp_code = cmp;
5267 wide_int real_c1;
5268 wide_int c1 = wi::to_wide (@1);
5269 wide_int c2 = wi::to_wide (@2);
5270 wide_int c3 = wi::to_wide (@3);
5271 signop sgn = TYPE_SIGN (from_type);
5272
5273 /* Handle special case A), given x of unsigned type:
5274 ((signed type)x < 0) <=> (x > MAX_VAL(signed type))
5275 ((signed type)x >= 0) <=> (x <= MAX_VAL(signed type)) */
5276 if (!types_match (from_type, to_type))
5277 {
5278 if (cmp_code == LT_EXPR)
5279 cmp_code = GT_EXPR;
5280 if (cmp_code == GE_EXPR)
5281 cmp_code = LE_EXPR;
5282 c1 = wi::max_value (to_type);
5283 }
5284 /* To simplify this pattern, we require c3 = (c1 op c2). Here we
5285 compute (c3 op' c2) and check if it equals to c1 with op' being
5286 the inverted operator of op. Make sure overflow doesn't happen
5287 if it is undefined. */
5288 if (op == PLUS_EXPR)
5289 real_c1 = wi::sub (c3, c2, sgn, &overflow);
5290 else
5291 real_c1 = wi::add (c3, c2, sgn, &overflow);
5292
5293 code = cmp_code;
5294 if (!overflow || !TYPE_OVERFLOW_UNDEFINED (from_type))
5295 {
5296 /* Check if c1 equals to real_c1. Boundary condition is handled
5297 by adjusting comparison operation if necessary. */
5298 if (!wi::cmp (wi::sub (real_c1, 1, sgn, &overflow), c1, sgn)
5299 && !overflow)
5300 {
5301 /* X <= Y - 1 equals to X < Y. */
5302 if (cmp_code == LE_EXPR)
5303 code = LT_EXPR;
5304 /* X > Y - 1 equals to X >= Y. */
5305 if (cmp_code == GT_EXPR)
5306 code = GE_EXPR;
5307 }
5308 if (!wi::cmp (wi::add (real_c1, 1, sgn, &overflow), c1, sgn)
5309 && !overflow)
5310 {
5311 /* X < Y + 1 equals to X <= Y. */
5312 if (cmp_code == LT_EXPR)
5313 code = LE_EXPR;
5314 /* X >= Y + 1 equals to X > Y. */
5315 if (cmp_code == GE_EXPR)
5316 code = GT_EXPR;
5317 }
5318 if (code != cmp_code || !wi::cmp (real_c1, c1, sgn))
5319 {
5320 if (cmp_code == LT_EXPR || cmp_code == LE_EXPR)
5321 code = MIN_EXPR;
5322 if (cmp_code == GT_EXPR || cmp_code == GE_EXPR)
5323 code = MAX_EXPR;
5324 }
5325 }
5326 }
5327 (if (code == MAX_EXPR)
5328 (op (max @X { wide_int_to_tree (from_type, real_c1); })
5329 { wide_int_to_tree (from_type, c2); })
5330 (if (code == MIN_EXPR)
5331 (op (min @X { wide_int_to_tree (from_type, real_c1); })
5332 { wide_int_to_tree (from_type, c2); })))))))))
5333
5334 #if GIMPLE
5335 /* A >= B ? A : B -> max (A, B) and friends. The code is still
5336 in fold_cond_expr_with_comparison for GENERIC folding with
5337 some extra constraints. */
5338 (for cmp (eq ne le lt unle unlt ge gt unge ungt uneq ltgt)
5339 (simplify
5340 (cond (cmp:c (nop_convert1?@c0 @0) (nop_convert2?@c1 @1))
5341 (convert3? @0) (convert4? @1))
5342 (if (!HONOR_SIGNED_ZEROS (type)
5343 && (/* Allow widening conversions of the compare operands as data. */
5344 (INTEGRAL_TYPE_P (type)
5345 && types_match (TREE_TYPE (@c0), TREE_TYPE (@0))
5346 && types_match (TREE_TYPE (@c1), TREE_TYPE (@1))
5347 && TYPE_PRECISION (TREE_TYPE (@0)) <= TYPE_PRECISION (type)
5348 && TYPE_PRECISION (TREE_TYPE (@1)) <= TYPE_PRECISION (type))
5349 /* Or sign conversions for the comparison. */
5350 || (types_match (type, TREE_TYPE (@0))
5351 && types_match (type, TREE_TYPE (@1)))))
5352 (switch
5353 (if (cmp == EQ_EXPR)
5354 (if (VECTOR_TYPE_P (type))
5355 (view_convert @c1)
5356 (convert @c1)))
5357 (if (cmp == NE_EXPR)
5358 (if (VECTOR_TYPE_P (type))
5359 (view_convert @c0)
5360 (convert @c0)))
5361 (if (cmp == LE_EXPR || cmp == UNLE_EXPR || cmp == LT_EXPR || cmp == UNLT_EXPR)
5362 (if (!HONOR_NANS (type))
5363 (if (VECTOR_TYPE_P (type))
5364 (view_convert (min @c0 @c1))
5365 (convert (min @c0 @c1)))))
5366 (if (cmp == GE_EXPR || cmp == UNGE_EXPR || cmp == GT_EXPR || cmp == UNGT_EXPR)
5367 (if (!HONOR_NANS (type))
5368 (if (VECTOR_TYPE_P (type))
5369 (view_convert (max @c0 @c1))
5370 (convert (max @c0 @c1)))))
5371 (if (cmp == UNEQ_EXPR)
5372 (if (!HONOR_NANS (type))
5373 (if (VECTOR_TYPE_P (type))
5374 (view_convert @c1)
5375 (convert @c1))))
5376 (if (cmp == LTGT_EXPR)
5377 (if (!HONOR_NANS (type))
5378 (if (VECTOR_TYPE_P (type))
5379 (view_convert @c0)
5380 (convert @c0))))))))
5381 #endif
5382
5383 (for cnd (cond vec_cond)
5384 /* (a != b) ? (a - b) : 0 -> (a - b) */
5385 (simplify
5386 (cnd (ne:c @0 @1) (minus@2 @0 @1) integer_zerop)
5387 @2)
5388 /* (a != b) ? (a ^ b) : 0 -> (a ^ b) */
5389 (simplify
5390 (cnd (ne:c @0 @1) (bit_xor:c@2 @0 @1) integer_zerop)
5391 @2)
5392 /* (a != b) ? (a & b) : a -> (a & b) */
5393 /* (a != b) ? (a | b) : a -> (a | b) */
5394 /* (a != b) ? min(a,b) : a -> min(a,b) */
5395 /* (a != b) ? max(a,b) : a -> max(a,b) */
5396 (for op (bit_and bit_ior min max)
5397 (simplify
5398 (cnd (ne:c @0 @1) (op:c@2 @0 @1) @0)
5399 @2))
5400 /* (a != b) ? (a * b) : (a * a) -> (a * b) */
5401 /* (a != b) ? (a + b) : (a + a) -> (a + b) */
5402 (for op (mult plus)
5403 (simplify
5404 (cnd (ne:c @0 @1) (op@2 @0 @1) (op @0 @0))
5405 (if (ANY_INTEGRAL_TYPE_P (type))
5406 @2)))
5407 /* (a != b) ? (a + b) : (2 * a) -> (a + b) */
5408 (simplify
5409 (cnd (ne:c @0 @1) (plus@2 @0 @1) (mult @0 uniform_integer_cst_p@3))
5410 (if (wi::to_wide (uniform_integer_cst_p (@3)) == 2)
5411 @2))
5412 )
5413
5414 /* These was part of minmax phiopt. */
5415 /* Optimize (a CMP b) ? minmax<a, c> : minmax<b, c>
5416 to minmax<min/max<a, b>, c> */
5417 (for minmax (min max)
5418 (for cmp (lt le gt ge ne)
5419 (simplify
5420 (cond (cmp @1 @3) (minmax:c @1 @4) (minmax:c @2 @4))
5421 (with
5422 {
5423 tree_code code = minmax_from_comparison (cmp, @1, @2, @1, @3);
5424 }
5425 (if (code == MIN_EXPR)
5426 (minmax (min @1 @2) @4)
5427 (if (code == MAX_EXPR)
5428 (minmax (max @1 @2) @4)))))))
5429
5430 /* Optimize (a CMP CST1) ? max<a,CST2> : a */
5431 (for cmp (gt ge lt le)
5432 minmax (min min max max)
5433 (simplify
5434 (cond (cmp:c @0 @1) (minmax:c@2 @0 @3) @4)
5435 (with
5436 {
5437 tree_code code = minmax_from_comparison (cmp, @0, @1, @0, @4);
5438 }
5439 (if ((cmp == LT_EXPR || cmp == LE_EXPR)
5440 && code == MIN_EXPR
5441 && integer_nonzerop (fold_build2 (LE_EXPR, boolean_type_node, @3, @4)))
5442 (min @2 @4)
5443 (if ((cmp == GT_EXPR || cmp == GE_EXPR)
5444 && code == MAX_EXPR
5445 && integer_nonzerop (fold_build2 (GE_EXPR, boolean_type_node, @3, @4)))
5446 (max @2 @4))))))
5447
5448 #if GIMPLE
5449 /* These patterns should be after min/max detection as simplifications
5450 of `(type)(zero_one ==/!= 0)` to `(type)(zero_one)`
5451 and `(type)(zero_one^1)` are not done yet. See PR 110637.
5452 Even without those, reaching min/max/and/ior faster is better. */
5453 (simplify
5454 (cond @0 zero_one_valued_p@1 zero_one_valued_p@2)
5455 (switch
5456 /* bool0 ? bool1 : 0 -> bool0 & bool1 */
5457 (if (integer_zerop (@2))
5458 (bit_and (convert @0) @1))
5459 /* bool0 ? 0 : bool2 -> (bool0^1) & bool2 */
5460 (if (integer_zerop (@1))
5461 (bit_and (bit_xor (convert @0) { build_one_cst (type); } ) @2))
5462 /* bool0 ? 1 : bool2 -> bool0 | bool2 */
5463 (if (integer_onep (@1))
5464 (bit_ior (convert @0) @2))
5465 /* bool0 ? bool1 : 1 -> (bool0^1) | bool1 */
5466 (if (integer_onep (@2))
5467 (bit_ior (bit_xor (convert @0) @2) @1))
5468 )
5469 )
5470 #endif
5471
5472 /* X != C1 ? -X : C2 simplifies to -X when -C1 == C2. */
5473 (simplify
5474 (cond (ne @0 INTEGER_CST@1) (negate@3 @0) INTEGER_CST@2)
5475 (if (!TYPE_SATURATING (type)
5476 && (TYPE_OVERFLOW_WRAPS (type)
5477 || !wi::only_sign_bit_p (wi::to_wide (@1)))
5478 && wi::eq_p (wi::neg (wi::to_wide (@1)), wi::to_wide (@2)))
5479 @3))
5480
5481 /* X != C1 ? ~X : C2 simplifies to ~X when ~C1 == C2. */
5482 (simplify
5483 (cond (ne @0 INTEGER_CST@1) (bit_not@3 @0) INTEGER_CST@2)
5484 (if (wi::eq_p (wi::bit_not (wi::to_wide (@1)), wi::to_wide (@2)))
5485 @3))
5486
5487 /* (X + 1) > Y ? -X : 1 simplifies to X >= Y ? -X : 1 when
5488 X is unsigned, as when X + 1 overflows, X is -1, so -X == 1. */
5489 (simplify
5490 (cond (gt (plus @0 integer_onep) @1) (negate @0) integer_onep@2)
5491 (if (TYPE_UNSIGNED (type))
5492 (cond (ge @0 @1) (negate @0) @2)))
5493
5494 (for cnd (cond vec_cond)
5495 /* A ? B : (A ? X : C) -> A ? B : C. */
5496 (simplify
5497 (cnd @0 (cnd @0 @1 @2) @3)
5498 (cnd @0 @1 @3))
5499 (simplify
5500 (cnd @0 @1 (cnd @0 @2 @3))
5501 (cnd @0 @1 @3))
5502 /* A ? B : (!A ? C : X) -> A ? B : C. */
5503 /* ??? This matches embedded conditions open-coded because genmatch
5504 would generate matching code for conditions in separate stmts only.
5505 The following is still important to merge then and else arm cases
5506 from if-conversion. */
5507 (simplify
5508 (cnd @0 @1 (cnd @2 @3 @4))
5509 (if (inverse_conditions_p (@0, @2))
5510 (cnd @0 @1 @3)))
5511 (simplify
5512 (cnd @0 (cnd @1 @2 @3) @4)
5513 (if (inverse_conditions_p (@0, @1))
5514 (cnd @0 @3 @4)))
5515
5516 /* A ? B : B -> B. */
5517 (simplify
5518 (cnd @0 @1 @1)
5519 @1)
5520
5521 /* !A ? B : C -> A ? C : B. */
5522 (simplify
5523 (cnd (logical_inverted_value truth_valued_p@0) @1 @2)
5524 (cnd @0 @2 @1)))
5525
5526 /* abs/negative simplifications moved from fold_cond_expr_with_comparison,
5527 Need to handle (A - B) case as fold_cond_expr_with_comparison does.
5528 Need to handle UN* comparisons.
5529
5530 None of these transformations work for modes with signed
5531 zeros. If A is +/-0, the first two transformations will
5532 change the sign of the result (from +0 to -0, or vice
5533 versa). The last four will fix the sign of the result,
5534 even though the original expressions could be positive or
5535 negative, depending on the sign of A.
5536
5537 Note that all these transformations are correct if A is
5538 NaN, since the two alternatives (A and -A) are also NaNs. */
5539
5540 (for cnd (cond vec_cond)
5541 /* A == 0 ? A : -A same as -A */
5542 (for cmp (eq uneq)
5543 (simplify
5544 (cnd (cmp @0 zerop) @0 (negate@1 @0))
5545 (if (!HONOR_SIGNED_ZEROS (type))
5546 @1))
5547 (simplify
5548 (cnd (cmp @0 zerop) zerop (negate@1 @0))
5549 (if (!HONOR_SIGNED_ZEROS (type))
5550 @1))
5551 )
5552 /* A != 0 ? A : -A same as A */
5553 (for cmp (ne ltgt)
5554 (simplify
5555 (cnd (cmp @0 zerop) @0 (negate @0))
5556 (if (!HONOR_SIGNED_ZEROS (type))
5557 @0))
5558 (simplify
5559 (cnd (cmp @0 zerop) @0 integer_zerop)
5560 (if (!HONOR_SIGNED_ZEROS (type))
5561 @0))
5562 )
5563 /* A >=/> 0 ? A : -A same as abs (A) */
5564 (for cmp (ge gt)
5565 (simplify
5566 (cnd (cmp @0 zerop) @0 (negate @0))
5567 (if (!HONOR_SIGNED_ZEROS (type)
5568 && !TYPE_UNSIGNED (type))
5569 (abs @0))))
5570 /* A <=/< 0 ? A : -A same as -abs (A) */
5571 (for cmp (le lt)
5572 (simplify
5573 (cnd (cmp @0 zerop) @0 (negate @0))
5574 (if (!HONOR_SIGNED_ZEROS (type)
5575 && !TYPE_UNSIGNED (type))
5576 (if (ANY_INTEGRAL_TYPE_P (type)
5577 && !TYPE_OVERFLOW_WRAPS (type))
5578 (with {
5579 tree utype = unsigned_type_for (type);
5580 }
5581 (convert (negate (absu:utype @0))))
5582 (negate (abs @0)))))
5583 )
5584 )
5585
5586 /* -(type)!A -> (type)A - 1. */
5587 (simplify
5588 (negate (convert?:s (logical_inverted_value:s @0)))
5589 (if (INTEGRAL_TYPE_P (type)
5590 && TREE_CODE (type) != BOOLEAN_TYPE
5591 && TYPE_PRECISION (type) > 1
5592 && TREE_CODE (@0) == SSA_NAME
5593 && ssa_name_has_boolean_range (@0))
5594 (plus (convert:type @0) { build_all_ones_cst (type); })))
5595
5596 /* A + (B vcmp C ? 1 : 0) -> A - (B vcmp C ? -1 : 0), since vector comparisons
5597 return all -1 or all 0 results. */
5598 /* ??? We could instead convert all instances of the vec_cond to negate,
5599 but that isn't necessarily a win on its own. */
5600 (simplify
5601 (plus:c @3 (view_convert? (vec_cond:s @0 integer_each_onep@1 integer_zerop@2)))
5602 (if (VECTOR_TYPE_P (type)
5603 && known_eq (TYPE_VECTOR_SUBPARTS (type),
5604 TYPE_VECTOR_SUBPARTS (TREE_TYPE (@1)))
5605 && (TYPE_MODE (TREE_TYPE (type))
5606 == TYPE_MODE (TREE_TYPE (TREE_TYPE (@1)))))
5607 (minus @3 (view_convert (vec_cond @0 (negate @1) @2)))))
5608
5609 /* ... likewise A - (B vcmp C ? 1 : 0) -> A + (B vcmp C ? -1 : 0). */
5610 (simplify
5611 (minus @3 (view_convert? (vec_cond:s @0 integer_each_onep@1 integer_zerop@2)))
5612 (if (VECTOR_TYPE_P (type)
5613 && known_eq (TYPE_VECTOR_SUBPARTS (type),
5614 TYPE_VECTOR_SUBPARTS (TREE_TYPE (@1)))
5615 && (TYPE_MODE (TREE_TYPE (type))
5616 == TYPE_MODE (TREE_TYPE (TREE_TYPE (@1)))))
5617 (plus @3 (view_convert (vec_cond @0 (negate @1) @2)))))
5618
5619
5620 /* Simplifications of comparisons. */
5621
5622 /* See if we can reduce the magnitude of a constant involved in a
5623 comparison by changing the comparison code. This is a canonicalization
5624 formerly done by maybe_canonicalize_comparison_1. */
5625 (for cmp (le gt)
5626 acmp (lt ge)
5627 (simplify
5628 (cmp @0 uniform_integer_cst_p@1)
5629 (with { tree cst = uniform_integer_cst_p (@1); }
5630 (if (tree_int_cst_sgn (cst) == -1)
5631 (acmp @0 { build_uniform_cst (TREE_TYPE (@1),
5632 wide_int_to_tree (TREE_TYPE (cst),
5633 wi::to_wide (cst)
5634 + 1)); })))))
5635 (for cmp (ge lt)
5636 acmp (gt le)
5637 (simplify
5638 (cmp @0 uniform_integer_cst_p@1)
5639 (with { tree cst = uniform_integer_cst_p (@1); }
5640 (if (tree_int_cst_sgn (cst) == 1)
5641 (acmp @0 { build_uniform_cst (TREE_TYPE (@1),
5642 wide_int_to_tree (TREE_TYPE (cst),
5643 wi::to_wide (cst) - 1)); })))))
5644
5645 /* We can simplify a logical negation of a comparison to the
5646 inverted comparison. As we cannot compute an expression
5647 operator using invert_tree_comparison we have to simulate
5648 that with expression code iteration. */
5649 (for cmp (tcc_comparison)
5650 icmp (inverted_tcc_comparison)
5651 ncmp (inverted_tcc_comparison_with_nans)
5652 /* Ideally we'd like to combine the following two patterns
5653 and handle some more cases by using
5654 (logical_inverted_value (cmp @0 @1))
5655 here but for that genmatch would need to "inline" that.
5656 For now implement what forward_propagate_comparison did. */
5657 (simplify
5658 (bit_not (cmp @0 @1))
5659 (if (VECTOR_TYPE_P (type)
5660 || (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1))
5661 /* Comparison inversion may be impossible for trapping math,
5662 invert_tree_comparison will tell us. But we can't use
5663 a computed operator in the replacement tree thus we have
5664 to play the trick below. */
5665 (with { enum tree_code ic = invert_tree_comparison
5666 (cmp, HONOR_NANS (@0)); }
5667 (if (ic == icmp)
5668 (icmp @0 @1)
5669 (if (ic == ncmp)
5670 (ncmp @0 @1))))))
5671 (simplify
5672 (bit_xor (cmp @0 @1) integer_truep)
5673 (with { enum tree_code ic = invert_tree_comparison
5674 (cmp, HONOR_NANS (@0)); }
5675 (if (ic == icmp)
5676 (icmp @0 @1)
5677 (if (ic == ncmp)
5678 (ncmp @0 @1)))))
5679 /* The following bits are handled by fold_binary_op_with_conditional_arg. */
5680 (simplify
5681 (ne (cmp@2 @0 @1) integer_zerop)
5682 (if (types_match (type, TREE_TYPE (@2)))
5683 (cmp @0 @1)))
5684 (simplify
5685 (eq (cmp@2 @0 @1) integer_truep)
5686 (if (types_match (type, TREE_TYPE (@2)))
5687 (cmp @0 @1)))
5688 (simplify
5689 (ne (cmp@2 @0 @1) integer_truep)
5690 (if (types_match (type, TREE_TYPE (@2)))
5691 (with { enum tree_code ic = invert_tree_comparison
5692 (cmp, HONOR_NANS (@0)); }
5693 (if (ic == icmp)
5694 (icmp @0 @1)
5695 (if (ic == ncmp)
5696 (ncmp @0 @1))))))
5697 (simplify
5698 (eq (cmp@2 @0 @1) integer_zerop)
5699 (if (types_match (type, TREE_TYPE (@2)))
5700 (with { enum tree_code ic = invert_tree_comparison
5701 (cmp, HONOR_NANS (@0)); }
5702 (if (ic == icmp)
5703 (icmp @0 @1)
5704 (if (ic == ncmp)
5705 (ncmp @0 @1)))))))
5706
5707 /* Transform comparisons of the form X - Y CMP 0 to X CMP Y.
5708 ??? The transformation is valid for the other operators if overflow
5709 is undefined for the type, but performing it here badly interacts
5710 with the transformation in fold_cond_expr_with_comparison which
5711 attempts to synthetize ABS_EXPR. */
5712 (for cmp (eq ne)
5713 (for sub (minus pointer_diff)
5714 (simplify
5715 (cmp (sub@2 @0 @1) integer_zerop)
5716 (if (single_use (@2))
5717 (cmp @0 @1)))))
5718
5719 /* Simplify (x < 0) ^ (y < 0) to (x ^ y) < 0 and
5720 (x >= 0) ^ (y >= 0) to (x ^ y) < 0. */
5721 (for cmp (lt ge)
5722 (simplify
5723 (bit_xor (cmp:s @0 integer_zerop) (cmp:s @1 integer_zerop))
5724 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
5725 && !TYPE_UNSIGNED (TREE_TYPE (@0))
5726 && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
5727 (lt (bit_xor @0 @1) { build_zero_cst (TREE_TYPE (@0)); }))))
5728 /* Simplify (x < 0) ^ (y >= 0) to (x ^ y) >= 0 and
5729 (x >= 0) ^ (y < 0) to (x ^ y) >= 0. */
5730 (simplify
5731 (bit_xor:c (lt:s @0 integer_zerop) (ge:s @1 integer_zerop))
5732 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
5733 && !TYPE_UNSIGNED (TREE_TYPE (@0))
5734 && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
5735 (ge (bit_xor @0 @1) { build_zero_cst (TREE_TYPE (@0)); })))
5736
5737 /* Transform comparisons of the form X * C1 CMP 0 to X CMP 0 in the
5738 signed arithmetic case. That form is created by the compiler
5739 often enough for folding it to be of value. One example is in
5740 computing loop trip counts after Operator Strength Reduction. */
5741 (for cmp (simple_comparison)
5742 scmp (swapped_simple_comparison)
5743 (simplify
5744 (cmp (mult@3 @0 INTEGER_CST@1) integer_zerop@2)
5745 /* Handle unfolded multiplication by zero. */
5746 (if (integer_zerop (@1))
5747 (cmp @1 @2)
5748 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
5749 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
5750 && single_use (@3))
5751 /* If @1 is negative we swap the sense of the comparison. */
5752 (if (tree_int_cst_sgn (@1) < 0)
5753 (scmp @0 @2)
5754 (cmp @0 @2))))))
5755
5756 /* For integral types with undefined overflow fold
5757 x * C1 == C2 into x == C2 / C1 or false.
5758 If overflow wraps and C1 is odd, simplify to x == C2 / C1 in the ring
5759 Z / 2^n Z. */
5760 (for cmp (eq ne)
5761 (simplify
5762 (cmp (mult @0 INTEGER_CST@1) INTEGER_CST@2)
5763 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
5764 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
5765 && wi::to_wide (@1) != 0)
5766 (with { widest_int quot; }
5767 (if (wi::multiple_of_p (wi::to_widest (@2), wi::to_widest (@1),
5768 TYPE_SIGN (TREE_TYPE (@0)), &quot))
5769 (cmp @0 { wide_int_to_tree (TREE_TYPE (@0), quot); })
5770 { constant_boolean_node (cmp == NE_EXPR, type); }))
5771 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
5772 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))
5773 && (wi::bit_and (wi::to_wide (@1), 1) == 1))
5774 (cmp @0
5775 {
5776 tree itype = TREE_TYPE (@0);
5777 int p = TYPE_PRECISION (itype);
5778 wide_int m = wi::one (p + 1) << p;
5779 wide_int a = wide_int::from (wi::to_wide (@1), p + 1, UNSIGNED);
5780 wide_int i = wide_int::from (wi::mod_inv (a, m),
5781 p, TYPE_SIGN (itype));
5782 wide_int_to_tree (itype, wi::mul (i, wi::to_wide (@2)));
5783 })))))
5784
5785 /* Simplify comparison of something with itself. For IEEE
5786 floating-point, we can only do some of these simplifications. */
5787 (for cmp (eq ge le)
5788 (simplify
5789 (cmp @0 @0)
5790 (if (! FLOAT_TYPE_P (TREE_TYPE (@0))
5791 || ! tree_expr_maybe_nan_p (@0))
5792 { constant_boolean_node (true, type); }
5793 (if (cmp != EQ_EXPR
5794 /* With -ftrapping-math conversion to EQ loses an exception. */
5795 && (! FLOAT_TYPE_P (TREE_TYPE (@0))
5796 || ! flag_trapping_math))
5797 (eq @0 @0)))))
5798 (for cmp (ne gt lt)
5799 (simplify
5800 (cmp @0 @0)
5801 (if (cmp != NE_EXPR
5802 || ! FLOAT_TYPE_P (TREE_TYPE (@0))
5803 || ! tree_expr_maybe_nan_p (@0))
5804 { constant_boolean_node (false, type); })))
5805 (for cmp (unle unge uneq)
5806 (simplify
5807 (cmp @0 @0)
5808 { constant_boolean_node (true, type); }))
5809 (for cmp (unlt ungt)
5810 (simplify
5811 (cmp @0 @0)
5812 (unordered @0 @0)))
5813 (simplify
5814 (ltgt @0 @0)
5815 (if (!flag_trapping_math || !tree_expr_maybe_nan_p (@0))
5816 { constant_boolean_node (false, type); }))
5817
5818 /* x == ~x -> false */
5819 /* x != ~x -> true */
5820 (for cmp (eq ne)
5821 (simplify
5822 (cmp:c @0 (bit_not @0))
5823 { constant_boolean_node (cmp == NE_EXPR, type); }))
5824
5825 /* Fold ~X op ~Y as Y op X. */
5826 (for cmp (simple_comparison)
5827 (simplify
5828 (cmp (bit_not@2 @0) (bit_not@3 @1))
5829 (if (single_use (@2) && single_use (@3))
5830 (cmp @1 @0))))
5831
5832 /* Fold ~X op C as X op' ~C, where op' is the swapped comparison. */
5833 (for cmp (simple_comparison)
5834 scmp (swapped_simple_comparison)
5835 (simplify
5836 (cmp (bit_not@2 @0) CONSTANT_CLASS_P@1)
5837 (if (single_use (@2)
5838 && (TREE_CODE (@1) == INTEGER_CST || TREE_CODE (@1) == VECTOR_CST))
5839 (scmp @0 (bit_not @1)))))
5840
5841 (for cmp (simple_comparison)
5842 (simplify
5843 (cmp @0 REAL_CST@1)
5844 /* IEEE doesn't distinguish +0 and -0 in comparisons. */
5845 (switch
5846 /* a CMP (-0) -> a CMP 0 */
5847 (if (REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@1)))
5848 (cmp @0 { build_real (TREE_TYPE (@1), dconst0); }))
5849 /* (-0) CMP b -> 0 CMP b. */
5850 (if (TREE_CODE (@0) == REAL_CST
5851 && REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@0)))
5852 (cmp { build_real (TREE_TYPE (@0), dconst0); } @1))
5853 /* x != NaN is always true, other ops are always false. */
5854 (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
5855 && (cmp == EQ_EXPR || cmp == NE_EXPR || !flag_trapping_math)
5856 && !tree_expr_signaling_nan_p (@1)
5857 && !tree_expr_maybe_signaling_nan_p (@0))
5858 { constant_boolean_node (cmp == NE_EXPR, type); })
5859 /* NaN != y is always true, other ops are always false. */
5860 (if (TREE_CODE (@0) == REAL_CST
5861 && REAL_VALUE_ISNAN (TREE_REAL_CST (@0))
5862 && (cmp == EQ_EXPR || cmp == NE_EXPR || !flag_trapping_math)
5863 && !tree_expr_signaling_nan_p (@0)
5864 && !tree_expr_signaling_nan_p (@1))
5865 { constant_boolean_node (cmp == NE_EXPR, type); })
5866 /* Fold comparisons against infinity. */
5867 (if (REAL_VALUE_ISINF (TREE_REAL_CST (@1))
5868 && MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (@1))))
5869 (with
5870 {
5871 REAL_VALUE_TYPE max;
5872 enum tree_code code = cmp;
5873 bool neg = REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1));
5874 if (neg)
5875 code = swap_tree_comparison (code);
5876 }
5877 (switch
5878 /* x > +Inf is always false, if we ignore NaNs or exceptions. */
5879 (if (code == GT_EXPR
5880 && !(HONOR_NANS (@0) && flag_trapping_math))
5881 { constant_boolean_node (false, type); })
5882 (if (code == LE_EXPR)
5883 /* x <= +Inf is always true, if we don't care about NaNs. */
5884 (if (! HONOR_NANS (@0))
5885 { constant_boolean_node (true, type); }
5886 /* x <= +Inf is the same as x == x, i.e. !isnan(x), but this loses
5887 an "invalid" exception. */
5888 (if (!flag_trapping_math)
5889 (eq @0 @0))))
5890 /* x == +Inf and x >= +Inf are always equal to x > DBL_MAX, but
5891 for == this introduces an exception for x a NaN. */
5892 (if ((code == EQ_EXPR && !(HONOR_NANS (@0) && flag_trapping_math))
5893 || code == GE_EXPR)
5894 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
5895 (if (neg)
5896 (lt @0 { build_real (TREE_TYPE (@0), max); })
5897 (gt @0 { build_real (TREE_TYPE (@0), max); }))))
5898 /* x < +Inf is always equal to x <= DBL_MAX. */
5899 (if (code == LT_EXPR)
5900 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
5901 (if (neg)
5902 (ge @0 { build_real (TREE_TYPE (@0), max); })
5903 (le @0 { build_real (TREE_TYPE (@0), max); }))))
5904 /* x != +Inf is always equal to !(x > DBL_MAX), but this introduces
5905 an exception for x a NaN so use an unordered comparison. */
5906 (if (code == NE_EXPR)
5907 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
5908 (if (! HONOR_NANS (@0))
5909 (if (neg)
5910 (ge @0 { build_real (TREE_TYPE (@0), max); })
5911 (le @0 { build_real (TREE_TYPE (@0), max); }))
5912 (if (neg)
5913 (unge @0 { build_real (TREE_TYPE (@0), max); })
5914 (unle @0 { build_real (TREE_TYPE (@0), max); }))))))))))
5915
5916 /* If this is a comparison of a real constant with a PLUS_EXPR
5917 or a MINUS_EXPR of a real constant, we can convert it into a
5918 comparison with a revised real constant as long as no overflow
5919 occurs when unsafe_math_optimizations are enabled. */
5920 (if (flag_unsafe_math_optimizations)
5921 (for op (plus minus)
5922 (simplify
5923 (cmp (op @0 REAL_CST@1) REAL_CST@2)
5924 (with
5925 {
5926 tree tem = const_binop (op == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR,
5927 TREE_TYPE (@1), @2, @1);
5928 }
5929 (if (tem && !TREE_OVERFLOW (tem))
5930 (cmp @0 { tem; }))))))
5931
5932 /* Likewise, we can simplify a comparison of a real constant with
5933 a MINUS_EXPR whose first operand is also a real constant, i.e.
5934 (c1 - x) < c2 becomes x > c1-c2. Reordering is allowed on
5935 floating-point types only if -fassociative-math is set. */
5936 (if (flag_associative_math)
5937 (simplify
5938 (cmp (minus REAL_CST@0 @1) REAL_CST@2)
5939 (with { tree tem = const_binop (MINUS_EXPR, TREE_TYPE (@1), @0, @2); }
5940 (if (tem && !TREE_OVERFLOW (tem))
5941 (cmp { tem; } @1)))))
5942
5943 /* Fold comparisons against built-in math functions. */
5944 (if (flag_unsafe_math_optimizations && ! flag_errno_math)
5945 (for sq (SQRT)
5946 (simplify
5947 (cmp (sq @0) REAL_CST@1)
5948 (switch
5949 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
5950 (switch
5951 /* sqrt(x) < y is always false, if y is negative. */
5952 (if (cmp == EQ_EXPR || cmp == LT_EXPR || cmp == LE_EXPR)
5953 { constant_boolean_node (false, type); })
5954 /* sqrt(x) > y is always true, if y is negative and we
5955 don't care about NaNs, i.e. negative values of x. */
5956 (if (cmp == NE_EXPR || !HONOR_NANS (@0))
5957 { constant_boolean_node (true, type); })
5958 /* sqrt(x) > y is the same as x >= 0, if y is negative. */
5959 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })))
5960 (if (real_equal (TREE_REAL_CST_PTR (@1), &dconst0))
5961 (switch
5962 /* sqrt(x) < 0 is always false. */
5963 (if (cmp == LT_EXPR)
5964 { constant_boolean_node (false, type); })
5965 /* sqrt(x) >= 0 is always true if we don't care about NaNs. */
5966 (if (cmp == GE_EXPR && !HONOR_NANS (@0))
5967 { constant_boolean_node (true, type); })
5968 /* sqrt(x) <= 0 -> x == 0. */
5969 (if (cmp == LE_EXPR)
5970 (eq @0 @1))
5971 /* Otherwise sqrt(x) cmp 0 -> x cmp 0. Here cmp can be >=, >,
5972 == or !=. In the last case:
5973
5974 (sqrt(x) != 0) == (NaN != 0) == true == (x != 0)
5975
5976 if x is negative or NaN. Due to -funsafe-math-optimizations,
5977 the results for other x follow from natural arithmetic. */
5978 (cmp @0 @1)))
5979 (if ((cmp == LT_EXPR
5980 || cmp == LE_EXPR
5981 || cmp == GT_EXPR
5982 || cmp == GE_EXPR)
5983 && !REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
5984 /* Give up for -frounding-math. */
5985 && !HONOR_SIGN_DEPENDENT_ROUNDING (TREE_TYPE (@0)))
5986 (with
5987 {
5988 REAL_VALUE_TYPE c2;
5989 enum tree_code ncmp = cmp;
5990 const real_format *fmt
5991 = REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (@0)));
5992 real_arithmetic (&c2, MULT_EXPR,
5993 &TREE_REAL_CST (@1), &TREE_REAL_CST (@1));
5994 real_convert (&c2, fmt, &c2);
5995 /* See PR91734: if c2 is inexact and sqrt(c2) < c (or sqrt(c2) >= c),
5996 then change LT_EXPR into LE_EXPR or GE_EXPR into GT_EXPR. */
5997 if (!REAL_VALUE_ISINF (c2))
5998 {
5999 tree c3 = fold_const_call (CFN_SQRT, TREE_TYPE (@0),
6000 build_real (TREE_TYPE (@0), c2));
6001 if (c3 == NULL_TREE || TREE_CODE (c3) != REAL_CST)
6002 ncmp = ERROR_MARK;
6003 else if ((cmp == LT_EXPR || cmp == GE_EXPR)
6004 && real_less (&TREE_REAL_CST (c3), &TREE_REAL_CST (@1)))
6005 ncmp = cmp == LT_EXPR ? LE_EXPR : GT_EXPR;
6006 else if ((cmp == LE_EXPR || cmp == GT_EXPR)
6007 && real_less (&TREE_REAL_CST (@1), &TREE_REAL_CST (c3)))
6008 ncmp = cmp == LE_EXPR ? LT_EXPR : GE_EXPR;
6009 else
6010 {
6011 /* With rounding to even, sqrt of up to 3 different values
6012 gives the same normal result, so in some cases c2 needs
6013 to be adjusted. */
6014 REAL_VALUE_TYPE c2alt, tow;
6015 if (cmp == LT_EXPR || cmp == GE_EXPR)
6016 tow = dconst0;
6017 else
6018 tow = dconstinf;
6019 real_nextafter (&c2alt, fmt, &c2, &tow);
6020 real_convert (&c2alt, fmt, &c2alt);
6021 if (REAL_VALUE_ISINF (c2alt))
6022 ncmp = ERROR_MARK;
6023 else
6024 {
6025 c3 = fold_const_call (CFN_SQRT, TREE_TYPE (@0),
6026 build_real (TREE_TYPE (@0), c2alt));
6027 if (c3 == NULL_TREE || TREE_CODE (c3) != REAL_CST)
6028 ncmp = ERROR_MARK;
6029 else if (real_equal (&TREE_REAL_CST (c3),
6030 &TREE_REAL_CST (@1)))
6031 c2 = c2alt;
6032 }
6033 }
6034 }
6035 }
6036 (if (cmp == GT_EXPR || cmp == GE_EXPR)
6037 (if (REAL_VALUE_ISINF (c2))
6038 /* sqrt(x) > y is x == +Inf, when y is very large. */
6039 (if (HONOR_INFINITIES (@0))
6040 (eq @0 { build_real (TREE_TYPE (@0), c2); })
6041 { constant_boolean_node (false, type); })
6042 /* sqrt(x) > c is the same as x > c*c. */
6043 (if (ncmp != ERROR_MARK)
6044 (if (ncmp == GE_EXPR)
6045 (ge @0 { build_real (TREE_TYPE (@0), c2); })
6046 (gt @0 { build_real (TREE_TYPE (@0), c2); }))))
6047 /* else if (cmp == LT_EXPR || cmp == LE_EXPR) */
6048 (if (REAL_VALUE_ISINF (c2))
6049 (switch
6050 /* sqrt(x) < y is always true, when y is a very large
6051 value and we don't care about NaNs or Infinities. */
6052 (if (! HONOR_NANS (@0) && ! HONOR_INFINITIES (@0))
6053 { constant_boolean_node (true, type); })
6054 /* sqrt(x) < y is x != +Inf when y is very large and we
6055 don't care about NaNs. */
6056 (if (! HONOR_NANS (@0))
6057 (ne @0 { build_real (TREE_TYPE (@0), c2); }))
6058 /* sqrt(x) < y is x >= 0 when y is very large and we
6059 don't care about Infinities. */
6060 (if (! HONOR_INFINITIES (@0))
6061 (ge @0 { build_real (TREE_TYPE (@0), dconst0); }))
6062 /* sqrt(x) < y is x >= 0 && x != +Inf, when y is large. */
6063 (if (GENERIC)
6064 (truth_andif
6065 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
6066 (ne @0 { build_real (TREE_TYPE (@0), c2); }))))
6067 /* sqrt(x) < c is the same as x < c*c, if we ignore NaNs. */
6068 (if (ncmp != ERROR_MARK && ! HONOR_NANS (@0))
6069 (if (ncmp == LT_EXPR)
6070 (lt @0 { build_real (TREE_TYPE (@0), c2); })
6071 (le @0 { build_real (TREE_TYPE (@0), c2); }))
6072 /* sqrt(x) < c is the same as x >= 0 && x < c*c. */
6073 (if (ncmp != ERROR_MARK && GENERIC)
6074 (if (ncmp == LT_EXPR)
6075 (truth_andif
6076 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
6077 (lt @0 { build_real (TREE_TYPE (@0), c2); }))
6078 (truth_andif
6079 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
6080 (le @0 { build_real (TREE_TYPE (@0), c2); })))))))))))
6081 /* Transform sqrt(x) cmp sqrt(y) -> x cmp y. */
6082 (simplify
6083 (cmp (sq @0) (sq @1))
6084 (if (! HONOR_NANS (@0))
6085 (cmp @0 @1))))))
6086
6087 /* Optimize various special cases of (FTYPE) N CMP (FTYPE) M. */
6088 (for cmp (lt le eq ne ge gt unordered ordered unlt unle ungt unge uneq ltgt)
6089 icmp (lt le eq ne ge gt unordered ordered lt le gt ge eq ne)
6090 (simplify
6091 (cmp (float@0 @1) (float @2))
6092 (if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (@0))
6093 && ! DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0)))
6094 (with
6095 {
6096 format_helper fmt (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (@0))));
6097 tree type1 = TREE_TYPE (@1);
6098 bool type1_signed_p = TYPE_SIGN (type1) == SIGNED;
6099 tree type2 = TREE_TYPE (@2);
6100 bool type2_signed_p = TYPE_SIGN (type2) == SIGNED;
6101 }
6102 (if (fmt.can_represent_integral_type_p (type1)
6103 && fmt.can_represent_integral_type_p (type2))
6104 (if (cmp == ORDERED_EXPR || cmp == UNORDERED_EXPR)
6105 { constant_boolean_node (cmp == ORDERED_EXPR, type); }
6106 (if (TYPE_PRECISION (type1) > TYPE_PRECISION (type2)
6107 && type1_signed_p >= type2_signed_p)
6108 (icmp @1 (convert @2))
6109 (if (TYPE_PRECISION (type1) < TYPE_PRECISION (type2)
6110 && type1_signed_p <= type2_signed_p)
6111 (icmp (convert:type2 @1) @2)
6112 (if (TYPE_PRECISION (type1) == TYPE_PRECISION (type2)
6113 && type1_signed_p == type2_signed_p)
6114 (icmp @1 @2))))))))))
6115
6116 /* Optimize various special cases of (FTYPE) N CMP CST. */
6117 (for cmp (lt le eq ne ge gt)
6118 icmp (le le eq ne ge ge)
6119 (simplify
6120 (cmp (float @0) REAL_CST@1)
6121 (if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (@1))
6122 && ! DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1)))
6123 (with
6124 {
6125 tree itype = TREE_TYPE (@0);
6126 format_helper fmt (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (@1))));
6127 const REAL_VALUE_TYPE *cst = TREE_REAL_CST_PTR (@1);
6128 /* Be careful to preserve any potential exceptions due to
6129 NaNs. qNaNs are ok in == or != context.
6130 TODO: relax under -fno-trapping-math or
6131 -fno-signaling-nans. */
6132 bool exception_p
6133 = real_isnan (cst) && (cst->signalling
6134 || (cmp != EQ_EXPR && cmp != NE_EXPR));
6135 }
6136 /* TODO: allow non-fitting itype and SNaNs when
6137 -fno-trapping-math. */
6138 (if (fmt.can_represent_integral_type_p (itype) && ! exception_p)
6139 (with
6140 {
6141 signop isign = TYPE_SIGN (itype);
6142 REAL_VALUE_TYPE imin, imax;
6143 real_from_integer (&imin, fmt, wi::min_value (itype), isign);
6144 real_from_integer (&imax, fmt, wi::max_value (itype), isign);
6145
6146 REAL_VALUE_TYPE icst;
6147 if (cmp == GT_EXPR || cmp == GE_EXPR)
6148 real_ceil (&icst, fmt, cst);
6149 else if (cmp == LT_EXPR || cmp == LE_EXPR)
6150 real_floor (&icst, fmt, cst);
6151 else
6152 real_trunc (&icst, fmt, cst);
6153
6154 bool cst_int_p = !real_isnan (cst) && real_identical (&icst, cst);
6155
6156 bool overflow_p = false;
6157 wide_int icst_val
6158 = real_to_integer (&icst, &overflow_p, TYPE_PRECISION (itype));
6159 }
6160 (switch
6161 /* Optimize cases when CST is outside of ITYPE's range. */
6162 (if (real_compare (LT_EXPR, cst, &imin))
6163 { constant_boolean_node (cmp == GT_EXPR || cmp == GE_EXPR || cmp == NE_EXPR,
6164 type); })
6165 (if (real_compare (GT_EXPR, cst, &imax))
6166 { constant_boolean_node (cmp == LT_EXPR || cmp == LE_EXPR || cmp == NE_EXPR,
6167 type); })
6168 /* Remove cast if CST is an integer representable by ITYPE. */
6169 (if (cst_int_p)
6170 (cmp @0 { gcc_assert (!overflow_p);
6171 wide_int_to_tree (itype, icst_val); })
6172 )
6173 /* When CST is fractional, optimize
6174 (FTYPE) N == CST -> 0
6175 (FTYPE) N != CST -> 1. */
6176 (if (cmp == EQ_EXPR || cmp == NE_EXPR)
6177 { constant_boolean_node (cmp == NE_EXPR, type); })
6178 /* Otherwise replace with sensible integer constant. */
6179 (with
6180 {
6181 gcc_checking_assert (!overflow_p);
6182 }
6183 (icmp @0 { wide_int_to_tree (itype, icst_val); })))))))))
6184
6185 /* Fold A /[ex] B CMP C to A CMP B * C. */
6186 (for cmp (eq ne)
6187 (simplify
6188 (cmp (exact_div @0 @1) INTEGER_CST@2)
6189 (if (!integer_zerop (@1))
6190 (if (wi::to_wide (@2) == 0)
6191 (cmp @0 @2)
6192 (if (TREE_CODE (@1) == INTEGER_CST)
6193 (with
6194 {
6195 wi::overflow_type ovf;
6196 wide_int prod = wi::mul (wi::to_wide (@2), wi::to_wide (@1),
6197 TYPE_SIGN (TREE_TYPE (@1)), &ovf);
6198 }
6199 (if (ovf)
6200 { constant_boolean_node (cmp == NE_EXPR, type); }
6201 (cmp @0 { wide_int_to_tree (TREE_TYPE (@0), prod); }))))))))
6202 (for cmp (lt le gt ge)
6203 (simplify
6204 (cmp (exact_div @0 INTEGER_CST@1) INTEGER_CST@2)
6205 (if (wi::gt_p (wi::to_wide (@1), 0, TYPE_SIGN (TREE_TYPE (@1))))
6206 (with
6207 {
6208 wi::overflow_type ovf;
6209 wide_int prod = wi::mul (wi::to_wide (@2), wi::to_wide (@1),
6210 TYPE_SIGN (TREE_TYPE (@1)), &ovf);
6211 }
6212 (if (ovf)
6213 { constant_boolean_node (wi::lt_p (wi::to_wide (@2), 0,
6214 TYPE_SIGN (TREE_TYPE (@2)))
6215 != (cmp == LT_EXPR || cmp == LE_EXPR), type); }
6216 (cmp @0 { wide_int_to_tree (TREE_TYPE (@0), prod); }))))))
6217
6218 /* Fold (size_t)(A /[ex] B) CMP C to (size_t)A CMP (size_t)B * C or A CMP' 0.
6219
6220 For small C (less than max/B), this is (size_t)A CMP (size_t)B * C.
6221 For large C (more than min/B+2^size), this is also true, with the
6222 multiplication computed modulo 2^size.
6223 For intermediate C, this just tests the sign of A. */
6224 (for cmp (lt le gt ge)
6225 cmp2 (ge ge lt lt)
6226 (simplify
6227 (cmp (convert (exact_div @0 INTEGER_CST@1)) INTEGER_CST@2)
6228 (if (tree_nop_conversion_p (TREE_TYPE (@0), TREE_TYPE (@2))
6229 && TYPE_UNSIGNED (TREE_TYPE (@2)) && !TYPE_UNSIGNED (TREE_TYPE (@0))
6230 && wi::gt_p (wi::to_wide (@1), 0, TYPE_SIGN (TREE_TYPE (@1))))
6231 (with
6232 {
6233 tree utype = TREE_TYPE (@2);
6234 wide_int denom = wi::to_wide (@1);
6235 wide_int right = wi::to_wide (@2);
6236 wide_int smax = wi::sdiv_trunc (wi::max_value (TREE_TYPE (@0)), denom);
6237 wide_int smin = wi::sdiv_trunc (wi::min_value (TREE_TYPE (@0)), denom);
6238 bool small = wi::leu_p (right, smax);
6239 bool large = wi::geu_p (right, smin);
6240 }
6241 (if (small || large)
6242 (cmp (convert:utype @0) (mult @2 (convert @1)))
6243 (cmp2 @0 { build_zero_cst (TREE_TYPE (@0)); }))))))
6244
6245 /* Unordered tests if either argument is a NaN. */
6246 (simplify
6247 (bit_ior (unordered @0 @0) (unordered @1 @1))
6248 (if (types_match (@0, @1))
6249 (unordered @0 @1)))
6250 (simplify
6251 (bit_and (ordered @0 @0) (ordered @1 @1))
6252 (if (types_match (@0, @1))
6253 (ordered @0 @1)))
6254 (simplify
6255 (bit_ior:c (unordered @0 @0) (unordered:c@2 @0 @1))
6256 @2)
6257 (simplify
6258 (bit_and:c (ordered @0 @0) (ordered:c@2 @0 @1))
6259 @2)
6260
6261 /* A & (2**N - 1) <= 2**K - 1 -> A & (2**N - 2**K) == 0
6262 A & (2**N - 1) > 2**K - 1 -> A & (2**N - 2**K) != 0
6263
6264 Note that comparisons
6265 A & (2**N - 1) < 2**K -> A & (2**N - 2**K) == 0
6266 A & (2**N - 1) >= 2**K -> A & (2**N - 2**K) != 0
6267 will be canonicalized to above so there's no need to
6268 consider them here.
6269 */
6270
6271 (for cmp (le gt)
6272 eqcmp (eq ne)
6273 (simplify
6274 (cmp (bit_and@0 @1 INTEGER_CST@2) INTEGER_CST@3)
6275 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)))
6276 (with
6277 {
6278 tree ty = TREE_TYPE (@0);
6279 unsigned prec = TYPE_PRECISION (ty);
6280 wide_int mask = wi::to_wide (@2, prec);
6281 wide_int rhs = wi::to_wide (@3, prec);
6282 signop sgn = TYPE_SIGN (ty);
6283 }
6284 (if ((mask & (mask + 1)) == 0 && wi::gt_p (rhs, 0, sgn)
6285 && (rhs & (rhs + 1)) == 0 && wi::ge_p (mask, rhs, sgn))
6286 (eqcmp (bit_and @1 { wide_int_to_tree (ty, mask - rhs); })
6287 { build_zero_cst (ty); }))))))
6288
6289 /* -A CMP -B -> B CMP A. */
6290 (for cmp (tcc_comparison)
6291 scmp (swapped_tcc_comparison)
6292 (simplify
6293 (cmp (negate @0) (negate @1))
6294 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
6295 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
6296 && (cmp == EQ_EXPR
6297 || cmp == NE_EXPR
6298 || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))))
6299 (scmp @0 @1)))
6300 (simplify
6301 (cmp (negate @0) CONSTANT_CLASS_P@1)
6302 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
6303 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
6304 && (cmp == EQ_EXPR
6305 || cmp == NE_EXPR
6306 || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))))
6307 (with { tree tem = const_unop (NEGATE_EXPR, TREE_TYPE (@0), @1); }
6308 (if (tem && !TREE_OVERFLOW (tem))
6309 (scmp @0 { tem; }))))))
6310
6311 /* Convert ABS[U]_EXPR<x> == 0 or ABS[U]_EXPR<x> != 0 to x == 0 or x != 0. */
6312 (for op (abs absu)
6313 (for eqne (eq ne)
6314 (simplify
6315 (eqne (op @0) zerop@1)
6316 (eqne @0 { build_zero_cst (TREE_TYPE (@0)); }))))
6317
6318 /* From fold_sign_changed_comparison and fold_widened_comparison.
6319 FIXME: the lack of symmetry is disturbing. */
6320 (for cmp (simple_comparison)
6321 (simplify
6322 (cmp (convert@0 @00) (convert?@1 @10))
6323 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6324 /* Disable this optimization if we're casting a function pointer
6325 type on targets that require function pointer canonicalization. */
6326 && !(targetm.have_canonicalize_funcptr_for_compare ()
6327 && ((POINTER_TYPE_P (TREE_TYPE (@00))
6328 && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@00))))
6329 || (POINTER_TYPE_P (TREE_TYPE (@10))
6330 && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@10))))))
6331 && single_use (@0))
6332 (if (TYPE_PRECISION (TREE_TYPE (@00)) == TYPE_PRECISION (TREE_TYPE (@0))
6333 && (TREE_CODE (@10) == INTEGER_CST
6334 || @1 != @10)
6335 && (TYPE_UNSIGNED (TREE_TYPE (@00)) == TYPE_UNSIGNED (TREE_TYPE (@0))
6336 || cmp == NE_EXPR
6337 || cmp == EQ_EXPR)
6338 && !POINTER_TYPE_P (TREE_TYPE (@00))
6339 /* (int)bool:32 != (int)uint is not the same as
6340 bool:32 != (bool:32)uint since boolean types only have two valid
6341 values independent of their precision. */
6342 && (TREE_CODE (TREE_TYPE (@00)) != BOOLEAN_TYPE
6343 || TREE_CODE (TREE_TYPE (@10)) == BOOLEAN_TYPE))
6344 /* ??? The special-casing of INTEGER_CST conversion was in the original
6345 code and here to avoid a spurious overflow flag on the resulting
6346 constant which fold_convert produces. */
6347 (if (TREE_CODE (@1) == INTEGER_CST)
6348 (cmp @00 { force_fit_type (TREE_TYPE (@00), wi::to_widest (@1), 0,
6349 TREE_OVERFLOW (@1)); })
6350 (cmp @00 (convert @1)))
6351
6352 (if (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (TREE_TYPE (@00)))
6353 /* If possible, express the comparison in the shorter mode. */
6354 (if ((cmp == EQ_EXPR || cmp == NE_EXPR
6355 || TYPE_UNSIGNED (TREE_TYPE (@0)) == TYPE_UNSIGNED (TREE_TYPE (@00))
6356 || (!TYPE_UNSIGNED (TREE_TYPE (@0))
6357 && TYPE_UNSIGNED (TREE_TYPE (@00))))
6358 && (types_match (TREE_TYPE (@10), TREE_TYPE (@00))
6359 || ((TYPE_PRECISION (TREE_TYPE (@00))
6360 >= TYPE_PRECISION (TREE_TYPE (@10)))
6361 && (TYPE_UNSIGNED (TREE_TYPE (@00))
6362 == TYPE_UNSIGNED (TREE_TYPE (@10))))
6363 || (TREE_CODE (@10) == INTEGER_CST
6364 && INTEGRAL_TYPE_P (TREE_TYPE (@00))
6365 && int_fits_type_p (@10, TREE_TYPE (@00)))))
6366 (cmp @00 (convert @10))
6367 (if (TREE_CODE (@10) == INTEGER_CST
6368 && INTEGRAL_TYPE_P (TREE_TYPE (@00))
6369 && !int_fits_type_p (@10, TREE_TYPE (@00)))
6370 (with
6371 {
6372 tree min = lower_bound_in_type (TREE_TYPE (@10), TREE_TYPE (@00));
6373 tree max = upper_bound_in_type (TREE_TYPE (@10), TREE_TYPE (@00));
6374 bool above = integer_nonzerop (const_binop (LT_EXPR, type, max, @10));
6375 bool below = integer_nonzerop (const_binop (LT_EXPR, type, @10, min));
6376 }
6377 (if (above || below)
6378 (if (cmp == EQ_EXPR || cmp == NE_EXPR)
6379 { constant_boolean_node (cmp == EQ_EXPR ? false : true, type); }
6380 (if (cmp == LT_EXPR || cmp == LE_EXPR)
6381 { constant_boolean_node (above ? true : false, type); }
6382 (if (cmp == GT_EXPR || cmp == GE_EXPR)
6383 { constant_boolean_node (above ? false : true, type); })))))))))
6384 /* Fold (double)float1 CMP (double)float2 into float1 CMP float2. */
6385 (if (FLOAT_TYPE_P (TREE_TYPE (@00))
6386 && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0))
6387 == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@00)))
6388 && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0))
6389 == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@10))))
6390 (with
6391 {
6392 tree type1 = TREE_TYPE (@10);
6393 if (TREE_CODE (@10) == REAL_CST && !DECIMAL_FLOAT_TYPE_P (type1))
6394 {
6395 REAL_VALUE_TYPE orig = TREE_REAL_CST (@10);
6396 if (TYPE_PRECISION (type1) > TYPE_PRECISION (float_type_node)
6397 && exact_real_truncate (TYPE_MODE (float_type_node), &orig))
6398 type1 = float_type_node;
6399 if (TYPE_PRECISION (type1) > TYPE_PRECISION (double_type_node)
6400 && exact_real_truncate (TYPE_MODE (double_type_node), &orig))
6401 type1 = double_type_node;
6402 }
6403 tree newtype
6404 = (element_precision (TREE_TYPE (@00)) > element_precision (type1)
6405 ? TREE_TYPE (@00) : type1);
6406 }
6407 (if (element_precision (TREE_TYPE (@0)) > element_precision (newtype))
6408 (cmp (convert:newtype @00) (convert:newtype @10))))))))
6409
6410
6411 (for cmp (eq ne)
6412 (simplify
6413 /* SSA names are canonicalized to 2nd place. */
6414 (cmp addr@0 SSA_NAME@1)
6415 (with
6416 {
6417 poly_int64 off; tree base;
6418 tree addr = (TREE_CODE (@0) == SSA_NAME
6419 ? gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0)) : @0);
6420 }
6421 /* A local variable can never be pointed to by
6422 the default SSA name of an incoming parameter. */
6423 (if (SSA_NAME_IS_DEFAULT_DEF (@1)
6424 && TREE_CODE (SSA_NAME_VAR (@1)) == PARM_DECL
6425 && (base = get_base_address (TREE_OPERAND (addr, 0)))
6426 && TREE_CODE (base) == VAR_DECL
6427 && auto_var_in_fn_p (base, current_function_decl))
6428 (if (cmp == NE_EXPR)
6429 { constant_boolean_node (true, type); }
6430 { constant_boolean_node (false, type); })
6431 /* If the address is based on @1 decide using the offset. */
6432 (if ((base = get_addr_base_and_unit_offset (TREE_OPERAND (addr, 0), &off))
6433 && TREE_CODE (base) == MEM_REF
6434 && TREE_OPERAND (base, 0) == @1)
6435 (with { off += mem_ref_offset (base).force_shwi (); }
6436 (if (known_ne (off, 0))
6437 { constant_boolean_node (cmp == NE_EXPR, type); }
6438 (if (known_eq (off, 0))
6439 { constant_boolean_node (cmp == EQ_EXPR, type); }))))))))
6440
6441 /* Equality compare simplifications from fold_binary */
6442 (for cmp (eq ne)
6443
6444 /* If we have (A | C) == D where C & ~D != 0, convert this into 0.
6445 Similarly for NE_EXPR. */
6446 (simplify
6447 (cmp (convert?@3 (bit_ior @0 INTEGER_CST@1)) INTEGER_CST@2)
6448 (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0))
6449 && wi::bit_and_not (wi::to_wide (@1), wi::to_wide (@2)) != 0)
6450 { constant_boolean_node (cmp == NE_EXPR, type); }))
6451
6452 /* (X ^ Y) == 0 becomes X == Y, and (X ^ Y) != 0 becomes X != Y. */
6453 (simplify
6454 (cmp (bit_xor @0 @1) integer_zerop)
6455 (cmp @0 @1))
6456
6457 /* (X ^ Y) == Y becomes X == 0.
6458 Likewise (X ^ Y) == X becomes Y == 0. */
6459 (simplify
6460 (cmp:c (bit_xor:c @0 @1) @0)
6461 (cmp @1 { build_zero_cst (TREE_TYPE (@1)); }))
6462
6463 /* (X & Y) == X becomes (X & ~Y) == 0. */
6464 (simplify
6465 (cmp:c (bit_and:c @0 @1) @0)
6466 (cmp (bit_and @0 (bit_not! @1)) { build_zero_cst (TREE_TYPE (@0)); }))
6467 (simplify
6468 (cmp:c (convert@3 (bit_and (convert@2 @0) INTEGER_CST@1)) (convert @0))
6469 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6470 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
6471 && INTEGRAL_TYPE_P (TREE_TYPE (@3))
6472 && TYPE_PRECISION (TREE_TYPE (@2)) == TYPE_PRECISION (TREE_TYPE (@0))
6473 && TYPE_PRECISION (TREE_TYPE (@3)) > TYPE_PRECISION (TREE_TYPE (@2))
6474 && !wi::neg_p (wi::to_wide (@1)))
6475 (cmp (bit_and @0 (convert (bit_not @1)))
6476 { build_zero_cst (TREE_TYPE (@0)); })))
6477
6478 /* (X | Y) == Y becomes (X & ~Y) == 0. */
6479 (simplify
6480 (cmp:c (bit_ior:c @0 @1) @1)
6481 (cmp (bit_and @0 (bit_not! @1)) { build_zero_cst (TREE_TYPE (@0)); }))
6482
6483 /* (X ^ C1) op C2 can be rewritten as X op (C1 ^ C2). */
6484 (simplify
6485 (cmp (convert?@3 (bit_xor @0 INTEGER_CST@1)) INTEGER_CST@2)
6486 (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0)))
6487 (cmp @0 (bit_xor @1 (convert @2)))))
6488
6489 (simplify
6490 (cmp (nop_convert? @0) integer_zerop)
6491 (if (tree_expr_nonzero_p (@0))
6492 { constant_boolean_node (cmp == NE_EXPR, type); }))
6493
6494 /* (X & C) op (Y & C) into (X ^ Y) & C op 0. */
6495 (simplify
6496 (cmp (bit_and:cs @0 @2) (bit_and:cs @1 @2))
6497 (cmp (bit_and (bit_xor @0 @1) @2) { build_zero_cst (TREE_TYPE (@2)); })))
6498
6499 /* (X < 0) != (Y < 0) into (X ^ Y) < 0.
6500 (X >= 0) != (Y >= 0) into (X ^ Y) < 0.
6501 (X < 0) == (Y < 0) into (X ^ Y) >= 0.
6502 (X >= 0) == (Y >= 0) into (X ^ Y) >= 0. */
6503 (for cmp (eq ne)
6504 ncmp (ge lt)
6505 (for sgncmp (ge lt)
6506 (simplify
6507 (cmp (sgncmp @0 integer_zerop@2) (sgncmp @1 integer_zerop))
6508 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
6509 && !TYPE_UNSIGNED (TREE_TYPE (@0))
6510 && types_match (@0, @1))
6511 (ncmp (bit_xor @0 @1) @2)))))
6512 /* (X < 0) == (Y >= 0) into (X ^ Y) < 0.
6513 (X < 0) != (Y >= 0) into (X ^ Y) >= 0. */
6514 (for cmp (eq ne)
6515 ncmp (lt ge)
6516 (simplify
6517 (cmp:c (lt @0 integer_zerop@2) (ge @1 integer_zerop))
6518 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
6519 && !TYPE_UNSIGNED (TREE_TYPE (@0))
6520 && types_match (@0, @1))
6521 (ncmp (bit_xor @0 @1) @2))))
6522
6523 /* If we have (A & C) == C where C is a power of 2, convert this into
6524 (A & C) != 0. Similarly for NE_EXPR. */
6525 (for cmp (eq ne)
6526 icmp (ne eq)
6527 (simplify
6528 (cmp (bit_and@2 @0 integer_pow2p@1) @1)
6529 (icmp @2 { build_zero_cst (TREE_TYPE (@0)); })))
6530
6531 #if GIMPLE
6532 /* From fold_binary_op_with_conditional_arg handle the case of
6533 rewriting (a ? b : c) > d to a ? (b > d) : (c > d) when the
6534 compares simplify. */
6535 (for cmp (simple_comparison)
6536 (simplify
6537 (cmp:c (cond @0 @1 @2) @3)
6538 /* Do not move possibly trapping operations into the conditional as this
6539 pessimizes code and causes gimplification issues when applied late. */
6540 (if (!FLOAT_TYPE_P (TREE_TYPE (@3))
6541 || !operation_could_trap_p (cmp, true, false, @3))
6542 (cond @0 (cmp! @1 @3) (cmp! @2 @3)))))
6543 #endif
6544
6545 (for cmp (ge lt)
6546 /* x < 0 ? ~y : y into (x >> (prec-1)) ^ y. */
6547 /* x >= 0 ? ~y : y into ~((x >> (prec-1)) ^ y). */
6548 (simplify
6549 (cond (cmp @0 integer_zerop) (bit_not @1) @1)
6550 (if (INTEGRAL_TYPE_P (type)
6551 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
6552 && !TYPE_UNSIGNED (TREE_TYPE (@0))
6553 && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (type))
6554 (with
6555 {
6556 tree shifter = build_int_cst (integer_type_node, TYPE_PRECISION (type) - 1);
6557 }
6558 (if (cmp == LT_EXPR)
6559 (bit_xor (convert (rshift @0 {shifter;})) @1)
6560 (bit_not (bit_xor (convert (rshift @0 {shifter;})) @1))))))
6561 /* x < 0 ? y : ~y into ~((x >> (prec-1)) ^ y). */
6562 /* x >= 0 ? y : ~y into (x >> (prec-1)) ^ y. */
6563 (simplify
6564 (cond (cmp @0 integer_zerop) @1 (bit_not @1))
6565 (if (INTEGRAL_TYPE_P (type)
6566 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
6567 && !TYPE_UNSIGNED (TREE_TYPE (@0))
6568 && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (type))
6569 (with
6570 {
6571 tree shifter = build_int_cst (integer_type_node, TYPE_PRECISION (type) - 1);
6572 }
6573 (if (cmp == GE_EXPR)
6574 (bit_xor (convert (rshift @0 {shifter;})) @1)
6575 (bit_not (bit_xor (convert (rshift @0 {shifter;})) @1)))))))
6576
6577 /* If we have (A & C) != 0 ? D : 0 where C and D are powers of 2,
6578 convert this into a shift followed by ANDing with D. */
6579 (simplify
6580 (cond
6581 (ne (bit_and @0 integer_pow2p@1) integer_zerop)
6582 INTEGER_CST@2 integer_zerop)
6583 (if (!POINTER_TYPE_P (type) && integer_pow2p (@2))
6584 (with {
6585 int shift = (wi::exact_log2 (wi::to_wide (@2))
6586 - wi::exact_log2 (wi::to_wide (@1)));
6587 }
6588 (if (shift > 0)
6589 (bit_and
6590 (lshift (convert @0) { build_int_cst (integer_type_node, shift); }) @2)
6591 (bit_and
6592 (convert (rshift @0 { build_int_cst (integer_type_node, -shift); }))
6593 @2)))))
6594
6595 /* If we have (A & C) != 0 where C is the sign bit of A, convert
6596 this into A < 0. Similarly for (A & C) == 0 into A >= 0. */
6597 (for cmp (eq ne)
6598 ncmp (ge lt)
6599 (simplify
6600 (cmp (bit_and (convert?@2 @0) integer_pow2p@1) integer_zerop)
6601 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6602 && type_has_mode_precision_p (TREE_TYPE (@0))
6603 && element_precision (@2) >= element_precision (@0)
6604 && wi::only_sign_bit_p (wi::to_wide (@1), element_precision (@0)))
6605 (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
6606 (ncmp (convert:stype @0) { build_zero_cst (stype); })))))
6607
6608 /* If we have A < 0 ? C : 0 where C is a power of 2, convert
6609 this into a right shift or sign extension followed by ANDing with C. */
6610 (simplify
6611 (cond
6612 (lt @0 integer_zerop)
6613 INTEGER_CST@1 integer_zerop)
6614 (if (integer_pow2p (@1)
6615 && !TYPE_UNSIGNED (TREE_TYPE (@0)))
6616 (with {
6617 int shift = element_precision (@0) - wi::exact_log2 (wi::to_wide (@1)) - 1;
6618 }
6619 (if (shift >= 0)
6620 (bit_and
6621 (convert (rshift @0 { build_int_cst (integer_type_node, shift); }))
6622 @1)
6623 /* Otherwise ctype must be wider than TREE_TYPE (@0) and pure
6624 sign extension followed by AND with C will achieve the effect. */
6625 (bit_and (convert @0) @1)))))
6626
6627 /* When the addresses are not directly of decls compare base and offset.
6628 This implements some remaining parts of fold_comparison address
6629 comparisons but still no complete part of it. Still it is good
6630 enough to make fold_stmt not regress when not dispatching to fold_binary. */
6631 (for cmp (simple_comparison)
6632 (simplify
6633 (cmp (convert1?@2 addr@0) (convert2? addr@1))
6634 (with
6635 {
6636 poly_int64 off0, off1;
6637 tree base0, base1;
6638 int equal = address_compare (cmp, TREE_TYPE (@2), @0, @1, base0, base1,
6639 off0, off1, GENERIC);
6640 }
6641 (if (equal == 1)
6642 (switch
6643 (if (cmp == EQ_EXPR && (known_eq (off0, off1) || known_ne (off0, off1)))
6644 { constant_boolean_node (known_eq (off0, off1), type); })
6645 (if (cmp == NE_EXPR && (known_eq (off0, off1) || known_ne (off0, off1)))
6646 { constant_boolean_node (known_ne (off0, off1), type); })
6647 (if (cmp == LT_EXPR && (known_lt (off0, off1) || known_ge (off0, off1)))
6648 { constant_boolean_node (known_lt (off0, off1), type); })
6649 (if (cmp == LE_EXPR && (known_le (off0, off1) || known_gt (off0, off1)))
6650 { constant_boolean_node (known_le (off0, off1), type); })
6651 (if (cmp == GE_EXPR && (known_ge (off0, off1) || known_lt (off0, off1)))
6652 { constant_boolean_node (known_ge (off0, off1), type); })
6653 (if (cmp == GT_EXPR && (known_gt (off0, off1) || known_le (off0, off1)))
6654 { constant_boolean_node (known_gt (off0, off1), type); }))
6655 (if (equal == 0)
6656 (switch
6657 (if (cmp == EQ_EXPR)
6658 { constant_boolean_node (false, type); })
6659 (if (cmp == NE_EXPR)
6660 { constant_boolean_node (true, type); })))))))
6661
6662 #if GIMPLE
6663 /* a?~t:t -> (-(a))^t */
6664 (simplify
6665 (cond @0 @1 @2)
6666 (with { bool wascmp; }
6667 (if (INTEGRAL_TYPE_P (type)
6668 && bitwise_inverted_equal_p (@1, @2, wascmp)
6669 && (!wascmp || element_precision (type) == 1))
6670 (with {
6671 auto prec = TYPE_PRECISION (type);
6672 auto unsign = TYPE_UNSIGNED (type);
6673 tree inttype = build_nonstandard_integer_type (prec, unsign);
6674 }
6675 (convert (bit_xor (negate (convert:inttype @0)) (convert:inttype @2)))))))
6676 #endif
6677
6678 /* Simplify pointer equality compares using PTA. */
6679 (for neeq (ne eq)
6680 (simplify
6681 (neeq @0 @1)
6682 (if (POINTER_TYPE_P (TREE_TYPE (@0))
6683 && ptrs_compare_unequal (@0, @1))
6684 { constant_boolean_node (neeq != EQ_EXPR, type); })))
6685
6686 /* PR70920: Transform (intptr_t)x eq/ne CST to x eq/ne (typeof x) CST.
6687 and (typeof ptr_cst) x eq/ne ptr_cst to x eq/ne (typeof x) CST.
6688 Disable the transform if either operand is pointer to function.
6689 This broke pr22051-2.c for arm where function pointer
6690 canonicalizaion is not wanted. */
6691
6692 (for cmp (ne eq)
6693 (simplify
6694 (cmp (convert @0) INTEGER_CST@1)
6695 (if (((POINTER_TYPE_P (TREE_TYPE (@0))
6696 && !FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@0)))
6697 && INTEGRAL_TYPE_P (TREE_TYPE (@1))
6698 /* Don't perform this optimization in GENERIC if @0 has reference
6699 type when sanitizing. See PR101210. */
6700 && !(GENERIC
6701 && TREE_CODE (TREE_TYPE (@0)) == REFERENCE_TYPE
6702 && (flag_sanitize & (SANITIZE_NULL | SANITIZE_ALIGNMENT))))
6703 || (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6704 && POINTER_TYPE_P (TREE_TYPE (@1))
6705 && !FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@1)))))
6706 && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
6707 (cmp @0 (convert @1)))))
6708
6709 /* Non-equality compare simplifications from fold_binary */
6710 (for cmp (lt gt le ge)
6711 /* Comparisons with the highest or lowest possible integer of
6712 the specified precision will have known values. */
6713 (simplify
6714 (cmp (convert?@2 @0) uniform_integer_cst_p@1)
6715 (if ((INTEGRAL_TYPE_P (TREE_TYPE (@1))
6716 || POINTER_TYPE_P (TREE_TYPE (@1))
6717 || VECTOR_INTEGER_TYPE_P (TREE_TYPE (@1)))
6718 && tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@0)))
6719 (with
6720 {
6721 tree cst = uniform_integer_cst_p (@1);
6722 tree arg1_type = TREE_TYPE (cst);
6723 unsigned int prec = TYPE_PRECISION (arg1_type);
6724 wide_int max = wi::max_value (arg1_type);
6725 wide_int signed_max = wi::max_value (prec, SIGNED);
6726 wide_int min = wi::min_value (arg1_type);
6727 }
6728 (switch
6729 (if (wi::to_wide (cst) == max)
6730 (switch
6731 (if (cmp == GT_EXPR)
6732 { constant_boolean_node (false, type); })
6733 (if (cmp == GE_EXPR)
6734 (eq @2 @1))
6735 (if (cmp == LE_EXPR)
6736 { constant_boolean_node (true, type); })
6737 (if (cmp == LT_EXPR)
6738 (ne @2 @1))))
6739 (if (wi::to_wide (cst) == min)
6740 (switch
6741 (if (cmp == LT_EXPR)
6742 { constant_boolean_node (false, type); })
6743 (if (cmp == LE_EXPR)
6744 (eq @2 @1))
6745 (if (cmp == GE_EXPR)
6746 { constant_boolean_node (true, type); })
6747 (if (cmp == GT_EXPR)
6748 (ne @2 @1))))
6749 (if (wi::to_wide (cst) == max - 1)
6750 (switch
6751 (if (cmp == GT_EXPR)
6752 (eq @2 { build_uniform_cst (TREE_TYPE (@1),
6753 wide_int_to_tree (TREE_TYPE (cst),
6754 wi::to_wide (cst)
6755 + 1)); }))
6756 (if (cmp == LE_EXPR)
6757 (ne @2 { build_uniform_cst (TREE_TYPE (@1),
6758 wide_int_to_tree (TREE_TYPE (cst),
6759 wi::to_wide (cst)
6760 + 1)); }))))
6761 (if (wi::to_wide (cst) == min + 1)
6762 (switch
6763 (if (cmp == GE_EXPR)
6764 (ne @2 { build_uniform_cst (TREE_TYPE (@1),
6765 wide_int_to_tree (TREE_TYPE (cst),
6766 wi::to_wide (cst)
6767 - 1)); }))
6768 (if (cmp == LT_EXPR)
6769 (eq @2 { build_uniform_cst (TREE_TYPE (@1),
6770 wide_int_to_tree (TREE_TYPE (cst),
6771 wi::to_wide (cst)
6772 - 1)); }))))
6773 (if (wi::to_wide (cst) == signed_max
6774 && TYPE_UNSIGNED (arg1_type)
6775 && TYPE_MODE (arg1_type) != BLKmode
6776 /* We will flip the signedness of the comparison operator
6777 associated with the mode of @1, so the sign bit is
6778 specified by this mode. Check that @1 is the signed
6779 max associated with this sign bit. */
6780 && prec == GET_MODE_PRECISION (SCALAR_INT_TYPE_MODE (arg1_type))
6781 /* signed_type does not work on pointer types. */
6782 && INTEGRAL_TYPE_P (arg1_type))
6783 /* The following case also applies to X < signed_max+1
6784 and X >= signed_max+1 because previous transformations. */
6785 (if (cmp == LE_EXPR || cmp == GT_EXPR)
6786 (with { tree st = signed_type_for (TREE_TYPE (@1)); }
6787 (switch
6788 (if (cst == @1 && cmp == LE_EXPR)
6789 (ge (convert:st @0) { build_zero_cst (st); }))
6790 (if (cst == @1 && cmp == GT_EXPR)
6791 (lt (convert:st @0) { build_zero_cst (st); }))
6792 (if (cmp == LE_EXPR)
6793 (ge (view_convert:st @0) { build_zero_cst (st); }))
6794 (if (cmp == GT_EXPR)
6795 (lt (view_convert:st @0) { build_zero_cst (st); })))))))))))
6796
6797 /* unsigned < (typeof unsigned)(unsigned != 0) is always false. */
6798 (simplify
6799 (lt:c @0 (convert (ne @0 integer_zerop)))
6800 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
6801 { constant_boolean_node (false, type); }))
6802
6803 /* x != (typeof x)(x == CST) -> CST == 0 ? 1 : (CST == 1 ? (x!=0&&x!=1) : x != 0) */
6804 /* x != (typeof x)(x != CST) -> CST == 1 ? 1 : (CST == 0 ? (x!=0&&x!=1) : x != 1) */
6805 /* x == (typeof x)(x == CST) -> CST == 0 ? 0 : (CST == 1 ? (x==0||x==1) : x == 0) */
6806 /* x == (typeof x)(x != CST) -> CST == 1 ? 0 : (CST == 0 ? (x==0||x==1) : x == 1) */
6807 (for outer (ne eq)
6808 (for inner (ne eq)
6809 (simplify
6810 (outer:c @0 (convert (inner @0 INTEGER_CST@1)))
6811 (with {
6812 bool cst1 = integer_onep (@1);
6813 bool cst0 = integer_zerop (@1);
6814 bool innereq = inner == EQ_EXPR;
6815 bool outereq = outer == EQ_EXPR;
6816 }
6817 (switch
6818 (if (innereq ? cst0 : cst1)
6819 { constant_boolean_node (!outereq, type); })
6820 (if (innereq ? cst1 : cst0)
6821 (with {
6822 tree utype = unsigned_type_for (TREE_TYPE (@0));
6823 tree ucst1 = build_one_cst (utype);
6824 }
6825 (if (!outereq)
6826 (gt (convert:utype @0) { ucst1; })
6827 (le (convert:utype @0) { ucst1; })
6828 )
6829 )
6830 )
6831 (with {
6832 tree value = build_int_cst (TREE_TYPE (@0), !innereq);
6833 }
6834 (if (outereq)
6835 (eq @0 { value; })
6836 (ne @0 { value; })
6837 )
6838 )
6839 )
6840 )
6841 )
6842 )
6843 )
6844
6845 (for cmp (unordered ordered unlt unle ungt unge uneq ltgt)
6846 /* If the second operand is NaN, the result is constant. */
6847 (simplify
6848 (cmp @0 REAL_CST@1)
6849 (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
6850 && (cmp != LTGT_EXPR || ! flag_trapping_math))
6851 { constant_boolean_node (cmp == ORDERED_EXPR || cmp == LTGT_EXPR
6852 ? false : true, type); })))
6853
6854 /* Fold UNORDERED if either operand must be NaN, or neither can be. */
6855 (simplify
6856 (unordered @0 @1)
6857 (switch
6858 (if (tree_expr_nan_p (@0) || tree_expr_nan_p (@1))
6859 { constant_boolean_node (true, type); })
6860 (if (!tree_expr_maybe_nan_p (@0) && !tree_expr_maybe_nan_p (@1))
6861 { constant_boolean_node (false, type); })))
6862
6863 /* Fold ORDERED if either operand must be NaN, or neither can be. */
6864 (simplify
6865 (ordered @0 @1)
6866 (switch
6867 (if (tree_expr_nan_p (@0) || tree_expr_nan_p (@1))
6868 { constant_boolean_node (false, type); })
6869 (if (!tree_expr_maybe_nan_p (@0) && !tree_expr_maybe_nan_p (@1))
6870 { constant_boolean_node (true, type); })))
6871
6872 /* bool_var != 0 becomes bool_var. */
6873 (simplify
6874 (ne @0 integer_zerop)
6875 (if (TREE_CODE (TREE_TYPE (@0)) == BOOLEAN_TYPE
6876 && types_match (type, TREE_TYPE (@0)))
6877 (non_lvalue @0)))
6878 /* bool_var == 1 becomes bool_var. */
6879 (simplify
6880 (eq @0 integer_onep)
6881 (if (TREE_CODE (TREE_TYPE (@0)) == BOOLEAN_TYPE
6882 && types_match (type, TREE_TYPE (@0)))
6883 (non_lvalue @0)))
6884 /* Do not handle
6885 bool_var == 0 becomes !bool_var or
6886 bool_var != 1 becomes !bool_var
6887 here because that only is good in assignment context as long
6888 as we require a tcc_comparison in GIMPLE_CONDs where we'd
6889 replace if (x == 0) with tem = ~x; if (tem != 0) which is
6890 clearly less optimal and which we'll transform again in forwprop. */
6891
6892 /* Transform comparisons of the form (X & Y) CMP 0 to X CMP2 Z
6893 where ~Y + 1 == pow2 and Z = ~Y. */
6894 (for cst (VECTOR_CST INTEGER_CST)
6895 (for cmp (eq ne)
6896 icmp (le gt)
6897 (simplify
6898 (cmp (bit_and:c@2 @0 cst@1) integer_zerop)
6899 (with { tree csts = bitmask_inv_cst_vector_p (@1); }
6900 (if (csts && (VECTOR_TYPE_P (TREE_TYPE (@1)) || single_use (@2)))
6901 (with { auto optab = VECTOR_TYPE_P (TREE_TYPE (@1))
6902 ? optab_vector : optab_default;
6903 tree utype = unsigned_type_for (TREE_TYPE (@1)); }
6904 (if (target_supports_op_p (utype, icmp, optab)
6905 || (optimize_vectors_before_lowering_p ()
6906 && (!target_supports_op_p (type, cmp, optab)
6907 || !target_supports_op_p (type, BIT_AND_EXPR, optab))))
6908 (if (TYPE_UNSIGNED (TREE_TYPE (@1)))
6909 (icmp @0 { csts; })
6910 (icmp (view_convert:utype @0) { csts; })))))))))
6911
6912 /* When one argument is a constant, overflow detection can be simplified.
6913 Currently restricted to single use so as not to interfere too much with
6914 ADD_OVERFLOW detection in tree-ssa-math-opts.cc.
6915 CONVERT?(CONVERT?(A) + CST) CMP A -> A CMP' CST' */
6916 (for cmp (lt le ge gt)
6917 out (gt gt le le)
6918 (simplify
6919 (cmp:c (convert?@3 (plus@2 (convert?@4 @0) INTEGER_CST@1)) @0)
6920 (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@2))
6921 && types_match (TREE_TYPE (@0), TREE_TYPE (@3))
6922 && tree_nop_conversion_p (TREE_TYPE (@4), TREE_TYPE (@0))
6923 && wi::to_wide (@1) != 0
6924 && single_use (@2))
6925 (with {
6926 unsigned int prec = TYPE_PRECISION (TREE_TYPE (@0));
6927 signop sign = TYPE_SIGN (TREE_TYPE (@0));
6928 }
6929 (out @0 { wide_int_to_tree (TREE_TYPE (@0),
6930 wi::max_value (prec, sign)
6931 - wi::to_wide (@1)); })))))
6932
6933 /* To detect overflow in unsigned A - B, A < B is simpler than A - B > A.
6934 However, the detection logic for SUB_OVERFLOW in tree-ssa-math-opts.cc
6935 expects the long form, so we restrict the transformation for now. */
6936 (for cmp (gt le)
6937 (simplify
6938 (cmp:c (minus@2 @0 @1) @0)
6939 (if (single_use (@2)
6940 && ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
6941 && TYPE_UNSIGNED (TREE_TYPE (@0)))
6942 (cmp @1 @0))))
6943
6944 /* Optimize A - B + -1 >= A into B >= A for unsigned comparisons. */
6945 (for cmp (ge lt)
6946 (simplify
6947 (cmp:c (plus (minus @0 @1) integer_minus_onep) @0)
6948 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
6949 && TYPE_UNSIGNED (TREE_TYPE (@0)))
6950 (cmp @1 @0))))
6951
6952 /* Testing for overflow is unnecessary if we already know the result. */
6953 /* A - B > A */
6954 (for cmp (gt le)
6955 out (ne eq)
6956 (simplify
6957 (cmp:c (realpart (IFN_SUB_OVERFLOW@2 @0 @1)) @0)
6958 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
6959 && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
6960 (out (imagpart @2) { build_zero_cst (TREE_TYPE (@0)); }))))
6961 /* A + B < A */
6962 (for cmp (lt ge)
6963 out (ne eq)
6964 (simplify
6965 (cmp:c (realpart (IFN_ADD_OVERFLOW:c@2 @0 @1)) @0)
6966 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
6967 && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
6968 (out (imagpart @2) { build_zero_cst (TREE_TYPE (@0)); }))))
6969
6970 /* For unsigned operands, -1 / B < A checks whether A * B would overflow.
6971 Simplify it to __builtin_mul_overflow (A, B, <unused>). */
6972 (for cmp (lt ge)
6973 out (ne eq)
6974 (simplify
6975 (cmp:c (trunc_div:s integer_all_onesp @1) @0)
6976 (if (TYPE_UNSIGNED (TREE_TYPE (@0)) && !VECTOR_TYPE_P (TREE_TYPE (@0)))
6977 (with { tree t = TREE_TYPE (@0), cpx = build_complex_type (t); }
6978 (out (imagpart (IFN_MUL_OVERFLOW:cpx @0 @1)) { build_zero_cst (t); })))))
6979
6980 /* Similarly, for unsigned operands, (((type) A * B) >> prec) != 0 where type
6981 is at least twice as wide as type of A and B, simplify to
6982 __builtin_mul_overflow (A, B, <unused>). */
6983 (for cmp (eq ne)
6984 (simplify
6985 (cmp (rshift (mult:s (convert@3 @0) (convert @1)) INTEGER_CST@2)
6986 integer_zerop)
6987 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6988 && INTEGRAL_TYPE_P (TREE_TYPE (@3))
6989 && TYPE_UNSIGNED (TREE_TYPE (@0))
6990 && (TYPE_PRECISION (TREE_TYPE (@3))
6991 >= 2 * TYPE_PRECISION (TREE_TYPE (@0)))
6992 && tree_fits_uhwi_p (@2)
6993 && tree_to_uhwi (@2) == TYPE_PRECISION (TREE_TYPE (@0))
6994 && types_match (@0, @1)
6995 && type_has_mode_precision_p (TREE_TYPE (@0))
6996 && (optab_handler (umulv4_optab, TYPE_MODE (TREE_TYPE (@0)))
6997 != CODE_FOR_nothing))
6998 (with { tree t = TREE_TYPE (@0), cpx = build_complex_type (t); }
6999 (cmp (imagpart (IFN_MUL_OVERFLOW:cpx @0 @1)) { build_zero_cst (t); })))))
7000
7001 /* Demote operands of IFN_{ADD,SUB,MUL}_OVERFLOW. */
7002 (for ovf (IFN_ADD_OVERFLOW IFN_SUB_OVERFLOW IFN_MUL_OVERFLOW)
7003 (simplify
7004 (ovf (convert@2 @0) @1)
7005 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
7006 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
7007 && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
7008 && (!TYPE_UNSIGNED (TREE_TYPE (@2)) || TYPE_UNSIGNED (TREE_TYPE (@0))))
7009 (ovf @0 @1)))
7010 (simplify
7011 (ovf @1 (convert@2 @0))
7012 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
7013 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
7014 && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
7015 && (!TYPE_UNSIGNED (TREE_TYPE (@2)) || TYPE_UNSIGNED (TREE_TYPE (@0))))
7016 (ovf @1 @0))))
7017
7018 /* Optimize __builtin_mul_overflow_p (x, cst, (utype) 0) if all 3 types
7019 are unsigned to x > (umax / cst). Similarly for signed type, but
7020 in that case it needs to be outside of a range. */
7021 (simplify
7022 (imagpart (IFN_MUL_OVERFLOW:cs@2 @0 integer_nonzerop@1))
7023 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
7024 && TYPE_MAX_VALUE (TREE_TYPE (@0))
7025 && types_match (TREE_TYPE (@0), TREE_TYPE (TREE_TYPE (@2)))
7026 && int_fits_type_p (@1, TREE_TYPE (@0)))
7027 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
7028 (convert (gt @0 (trunc_div! { TYPE_MAX_VALUE (TREE_TYPE (@0)); } @1)))
7029 (if (TYPE_MIN_VALUE (TREE_TYPE (@0)))
7030 (if (integer_minus_onep (@1))
7031 (convert (eq @0 { TYPE_MIN_VALUE (TREE_TYPE (@0)); }))
7032 (with
7033 {
7034 tree div = fold_convert (TREE_TYPE (@0), @1);
7035 tree lo = int_const_binop (TRUNC_DIV_EXPR,
7036 TYPE_MIN_VALUE (TREE_TYPE (@0)), div);
7037 tree hi = int_const_binop (TRUNC_DIV_EXPR,
7038 TYPE_MAX_VALUE (TREE_TYPE (@0)), div);
7039 tree etype = range_check_type (TREE_TYPE (@0));
7040 if (etype)
7041 {
7042 if (wi::neg_p (wi::to_wide (div)))
7043 std::swap (lo, hi);
7044 lo = fold_convert (etype, lo);
7045 hi = fold_convert (etype, hi);
7046 hi = int_const_binop (MINUS_EXPR, hi, lo);
7047 }
7048 }
7049 (if (etype)
7050 (convert (gt (minus (convert:etype @0) { lo; }) { hi; })))))))))
7051
7052 /* Simplification of math builtins. These rules must all be optimizations
7053 as well as IL simplifications. If there is a possibility that the new
7054 form could be a pessimization, the rule should go in the canonicalization
7055 section that follows this one.
7056
7057 Rules can generally go in this section if they satisfy one of
7058 the following:
7059
7060 - the rule describes an identity
7061
7062 - the rule replaces calls with something as simple as addition or
7063 multiplication
7064
7065 - the rule contains unary calls only and simplifies the surrounding
7066 arithmetic. (The idea here is to exclude non-unary calls in which
7067 one operand is constant and in which the call is known to be cheap
7068 when the operand has that value.) */
7069
7070 (if (flag_unsafe_math_optimizations)
7071 /* Simplify sqrt(x) * sqrt(x) -> x. */
7072 (simplify
7073 (mult (SQRT_ALL@1 @0) @1)
7074 (if (!tree_expr_maybe_signaling_nan_p (@0))
7075 @0))
7076
7077 (for op (plus minus)
7078 /* Simplify (A / C) +- (B / C) -> (A +- B) / C. */
7079 (simplify
7080 (op (rdiv @0 @1)
7081 (rdiv @2 @1))
7082 (rdiv (op @0 @2) @1)))
7083
7084 (for cmp (lt le gt ge)
7085 neg_cmp (gt ge lt le)
7086 /* Simplify (x * C1) cmp C2 -> x cmp (C2 / C1), where C1 != 0. */
7087 (simplify
7088 (cmp (mult @0 REAL_CST@1) REAL_CST@2)
7089 (with
7090 { tree tem = const_binop (RDIV_EXPR, type, @2, @1); }
7091 (if (tem
7092 && !(REAL_VALUE_ISINF (TREE_REAL_CST (tem))
7093 || (real_zerop (tem) && !real_zerop (@1))))
7094 (switch
7095 (if (real_less (&dconst0, TREE_REAL_CST_PTR (@1)))
7096 (cmp @0 { tem; }))
7097 (if (real_less (TREE_REAL_CST_PTR (@1), &dconst0))
7098 (neg_cmp @0 { tem; })))))))
7099
7100 /* Simplify sqrt(x) * sqrt(y) -> sqrt(x*y). */
7101 (for root (SQRT CBRT)
7102 (simplify
7103 (mult (root:s @0) (root:s @1))
7104 (root (mult @0 @1))))
7105
7106 /* Simplify expN(x) * expN(y) -> expN(x+y). */
7107 (for exps (EXP EXP2 EXP10 POW10)
7108 (simplify
7109 (mult (exps:s @0) (exps:s @1))
7110 (exps (plus @0 @1))))
7111
7112 /* Simplify a/root(b/c) into a*root(c/b). */
7113 (for root (SQRT CBRT)
7114 (simplify
7115 (rdiv @0 (root:s (rdiv:s @1 @2)))
7116 (mult @0 (root (rdiv @2 @1)))))
7117
7118 /* Simplify x/expN(y) into x*expN(-y). */
7119 (for exps (EXP EXP2 EXP10 POW10)
7120 (simplify
7121 (rdiv @0 (exps:s @1))
7122 (mult @0 (exps (negate @1)))))
7123
7124 (for logs (LOG LOG2 LOG10 LOG10)
7125 exps (EXP EXP2 EXP10 POW10)
7126 /* logN(expN(x)) -> x. */
7127 (simplify
7128 (logs (exps @0))
7129 @0)
7130 /* expN(logN(x)) -> x. */
7131 (simplify
7132 (exps (logs @0))
7133 @0))
7134
7135 /* Optimize logN(func()) for various exponential functions. We
7136 want to determine the value "x" and the power "exponent" in
7137 order to transform logN(x**exponent) into exponent*logN(x). */
7138 (for logs (LOG LOG LOG LOG2 LOG2 LOG2 LOG10 LOG10)
7139 exps (EXP2 EXP10 POW10 EXP EXP10 POW10 EXP EXP2)
7140 (simplify
7141 (logs (exps @0))
7142 (if (SCALAR_FLOAT_TYPE_P (type))
7143 (with {
7144 tree x;
7145 switch (exps)
7146 {
7147 CASE_CFN_EXP:
7148 /* Prepare to do logN(exp(exponent)) -> exponent*logN(e). */
7149 x = build_real_truncate (type, dconst_e ());
7150 break;
7151 CASE_CFN_EXP2:
7152 /* Prepare to do logN(exp2(exponent)) -> exponent*logN(2). */
7153 x = build_real (type, dconst2);
7154 break;
7155 CASE_CFN_EXP10:
7156 CASE_CFN_POW10:
7157 /* Prepare to do logN(exp10(exponent)) -> exponent*logN(10). */
7158 {
7159 REAL_VALUE_TYPE dconst10;
7160 real_from_integer (&dconst10, VOIDmode, 10, SIGNED);
7161 x = build_real (type, dconst10);
7162 }
7163 break;
7164 default:
7165 gcc_unreachable ();
7166 }
7167 }
7168 (mult (logs { x; }) @0)))))
7169
7170 (for logs (LOG LOG
7171 LOG2 LOG2
7172 LOG10 LOG10)
7173 exps (SQRT CBRT)
7174 (simplify
7175 (logs (exps @0))
7176 (if (SCALAR_FLOAT_TYPE_P (type))
7177 (with {
7178 tree x;
7179 switch (exps)
7180 {
7181 CASE_CFN_SQRT:
7182 /* Prepare to do logN(sqrt(x)) -> 0.5*logN(x). */
7183 x = build_real (type, dconsthalf);
7184 break;
7185 CASE_CFN_CBRT:
7186 /* Prepare to do logN(cbrt(x)) -> (1/3)*logN(x). */
7187 x = build_real_truncate (type, dconst_third ());
7188 break;
7189 default:
7190 gcc_unreachable ();
7191 }
7192 }
7193 (mult { x; } (logs @0))))))
7194
7195 /* logN(pow(x,exponent)) -> exponent*logN(x). */
7196 (for logs (LOG LOG2 LOG10)
7197 pows (POW)
7198 (simplify
7199 (logs (pows @0 @1))
7200 (mult @1 (logs @0))))
7201
7202 /* pow(C,x) -> exp(log(C)*x) if C > 0,
7203 or if C is a positive power of 2,
7204 pow(C,x) -> exp2(log2(C)*x). */
7205 #if GIMPLE
7206 (for pows (POW)
7207 exps (EXP)
7208 logs (LOG)
7209 exp2s (EXP2)
7210 log2s (LOG2)
7211 (simplify
7212 (pows REAL_CST@0 @1)
7213 (if (real_compare (GT_EXPR, TREE_REAL_CST_PTR (@0), &dconst0)
7214 && real_isfinite (TREE_REAL_CST_PTR (@0))
7215 /* As libmvec doesn't have a vectorized exp2, defer optimizing
7216 the use_exp2 case until after vectorization. It seems actually
7217 beneficial for all constants to postpone this until later,
7218 because exp(log(C)*x), while faster, will have worse precision
7219 and if x folds into a constant too, that is unnecessary
7220 pessimization. */
7221 && canonicalize_math_after_vectorization_p ())
7222 (with {
7223 const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (@0);
7224 bool use_exp2 = false;
7225 if (targetm.libc_has_function (function_c99_misc, TREE_TYPE (@0))
7226 && value->cl == rvc_normal)
7227 {
7228 REAL_VALUE_TYPE frac_rvt = *value;
7229 SET_REAL_EXP (&frac_rvt, 1);
7230 if (real_equal (&frac_rvt, &dconst1))
7231 use_exp2 = true;
7232 }
7233 }
7234 (if (!use_exp2)
7235 (if (optimize_pow_to_exp (@0, @1))
7236 (exps (mult (logs @0) @1)))
7237 (exp2s (mult (log2s @0) @1)))))))
7238 #endif
7239
7240 /* pow(C,x)*expN(y) -> expN(logN(C)*x+y) if C > 0. */
7241 (for pows (POW)
7242 exps (EXP EXP2 EXP10 POW10)
7243 logs (LOG LOG2 LOG10 LOG10)
7244 (simplify
7245 (mult:c (pows:s REAL_CST@0 @1) (exps:s @2))
7246 (if (real_compare (GT_EXPR, TREE_REAL_CST_PTR (@0), &dconst0)
7247 && real_isfinite (TREE_REAL_CST_PTR (@0)))
7248 (exps (plus (mult (logs @0) @1) @2)))))
7249
7250 (for sqrts (SQRT)
7251 cbrts (CBRT)
7252 pows (POW)
7253 exps (EXP EXP2 EXP10 POW10)
7254 /* sqrt(expN(x)) -> expN(x*0.5). */
7255 (simplify
7256 (sqrts (exps @0))
7257 (exps (mult @0 { build_real (type, dconsthalf); })))
7258 /* cbrt(expN(x)) -> expN(x/3). */
7259 (simplify
7260 (cbrts (exps @0))
7261 (exps (mult @0 { build_real_truncate (type, dconst_third ()); })))
7262 /* pow(expN(x), y) -> expN(x*y). */
7263 (simplify
7264 (pows (exps @0) @1)
7265 (exps (mult @0 @1))))
7266
7267 /* tan(atan(x)) -> x. */
7268 (for tans (TAN)
7269 atans (ATAN)
7270 (simplify
7271 (tans (atans @0))
7272 @0)))
7273
7274 /* Simplify sin(atan(x)) -> x / sqrt(x*x + 1). */
7275 (for sins (SIN)
7276 atans (ATAN)
7277 sqrts (SQRT)
7278 copysigns (COPYSIGN)
7279 (simplify
7280 (sins (atans:s @0))
7281 (with
7282 {
7283 REAL_VALUE_TYPE r_cst;
7284 build_sinatan_real (&r_cst, type);
7285 tree t_cst = build_real (type, r_cst);
7286 tree t_one = build_one_cst (type);
7287 }
7288 (if (SCALAR_FLOAT_TYPE_P (type))
7289 (cond (lt (abs @0) { t_cst; })
7290 (rdiv @0 (sqrts (plus (mult @0 @0) { t_one; })))
7291 (copysigns { t_one; } @0))))))
7292
7293 /* Simplify cos(atan(x)) -> 1 / sqrt(x*x + 1). */
7294 (for coss (COS)
7295 atans (ATAN)
7296 sqrts (SQRT)
7297 copysigns (COPYSIGN)
7298 (simplify
7299 (coss (atans:s @0))
7300 (with
7301 {
7302 REAL_VALUE_TYPE r_cst;
7303 build_sinatan_real (&r_cst, type);
7304 tree t_cst = build_real (type, r_cst);
7305 tree t_one = build_one_cst (type);
7306 tree t_zero = build_zero_cst (type);
7307 }
7308 (if (SCALAR_FLOAT_TYPE_P (type))
7309 (cond (lt (abs @0) { t_cst; })
7310 (rdiv { t_one; } (sqrts (plus (mult @0 @0) { t_one; })))
7311 (copysigns { t_zero; } @0))))))
7312
7313 (if (!flag_errno_math)
7314 /* Simplify sinh(atanh(x)) -> x / sqrt((1 - x)*(1 + x)). */
7315 (for sinhs (SINH)
7316 atanhs (ATANH)
7317 sqrts (SQRT)
7318 (simplify
7319 (sinhs (atanhs:s @0))
7320 (with { tree t_one = build_one_cst (type); }
7321 (rdiv @0 (sqrts (mult (minus { t_one; } @0) (plus { t_one; } @0)))))))
7322
7323 /* Simplify cosh(atanh(x)) -> 1 / sqrt((1 - x)*(1 + x)) */
7324 (for coshs (COSH)
7325 atanhs (ATANH)
7326 sqrts (SQRT)
7327 (simplify
7328 (coshs (atanhs:s @0))
7329 (with { tree t_one = build_one_cst (type); }
7330 (rdiv { t_one; } (sqrts (mult (minus { t_one; } @0) (plus { t_one; } @0))))))))
7331
7332 /* cabs(x+0i) or cabs(0+xi) -> abs(x). */
7333 (simplify
7334 (CABS (complex:C @0 real_zerop@1))
7335 (abs @0))
7336
7337 /* trunc(trunc(x)) -> trunc(x), etc. */
7338 (for fns (TRUNC_ALL FLOOR_ALL CEIL_ALL ROUND_ALL NEARBYINT_ALL RINT_ALL)
7339 (simplify
7340 (fns (fns @0))
7341 (fns @0)))
7342 /* f(x) -> x if x is integer valued and f does nothing for such values. */
7343 (for fns (TRUNC_ALL FLOOR_ALL CEIL_ALL ROUND_ALL NEARBYINT_ALL RINT_ALL)
7344 (simplify
7345 (fns integer_valued_real_p@0)
7346 @0))
7347
7348 /* hypot(x,0) and hypot(0,x) -> abs(x). */
7349 (simplify
7350 (HYPOT:c @0 real_zerop@1)
7351 (abs @0))
7352
7353 /* pow(1,x) -> 1. */
7354 (simplify
7355 (POW real_onep@0 @1)
7356 @0)
7357
7358 (simplify
7359 /* copysign(x,x) -> x. */
7360 (COPYSIGN_ALL @0 @0)
7361 @0)
7362
7363 (simplify
7364 /* copysign(x,-x) -> -x. */
7365 (COPYSIGN_ALL @0 (negate@1 @0))
7366 @1)
7367
7368 (simplify
7369 /* copysign(x,y) -> fabs(x) if y is nonnegative. */
7370 (COPYSIGN_ALL @0 tree_expr_nonnegative_p@1)
7371 (abs @0))
7372
7373 (for scale (LDEXP SCALBN SCALBLN)
7374 /* ldexp(0, x) -> 0. */
7375 (simplify
7376 (scale real_zerop@0 @1)
7377 @0)
7378 /* ldexp(x, 0) -> x. */
7379 (simplify
7380 (scale @0 integer_zerop@1)
7381 @0)
7382 /* ldexp(x, y) -> x if x is +-Inf or NaN. */
7383 (simplify
7384 (scale REAL_CST@0 @1)
7385 (if (!real_isfinite (TREE_REAL_CST_PTR (@0)))
7386 @0)))
7387
7388 /* Canonicalization of sequences of math builtins. These rules represent
7389 IL simplifications but are not necessarily optimizations.
7390
7391 The sincos pass is responsible for picking "optimal" implementations
7392 of math builtins, which may be more complicated and can sometimes go
7393 the other way, e.g. converting pow into a sequence of sqrts.
7394 We only want to do these canonicalizations before the pass has run. */
7395
7396 (if (flag_unsafe_math_optimizations && canonicalize_math_p ())
7397 /* Simplify tan(x) * cos(x) -> sin(x). */
7398 (simplify
7399 (mult:c (TAN:s @0) (COS:s @0))
7400 (SIN @0))
7401
7402 /* Simplify x * pow(x,c) -> pow(x,c+1). */
7403 (simplify
7404 (mult:c @0 (POW:s @0 REAL_CST@1))
7405 (if (!TREE_OVERFLOW (@1))
7406 (POW @0 (plus @1 { build_one_cst (type); }))))
7407
7408 /* Simplify sin(x) / cos(x) -> tan(x). */
7409 (simplify
7410 (rdiv (SIN:s @0) (COS:s @0))
7411 (TAN @0))
7412
7413 /* Simplify sinh(x) / cosh(x) -> tanh(x). */
7414 (simplify
7415 (rdiv (SINH:s @0) (COSH:s @0))
7416 (TANH @0))
7417
7418 /* Simplify tanh (x) / sinh (x) -> 1.0 / cosh (x). */
7419 (simplify
7420 (rdiv (TANH:s @0) (SINH:s @0))
7421 (rdiv {build_one_cst (type);} (COSH @0)))
7422
7423 /* Simplify cos(x) / sin(x) -> 1 / tan(x). */
7424 (simplify
7425 (rdiv (COS:s @0) (SIN:s @0))
7426 (rdiv { build_one_cst (type); } (TAN @0)))
7427
7428 /* Simplify sin(x) / tan(x) -> cos(x). */
7429 (simplify
7430 (rdiv (SIN:s @0) (TAN:s @0))
7431 (if (! HONOR_NANS (@0)
7432 && ! HONOR_INFINITIES (@0))
7433 (COS @0)))
7434
7435 /* Simplify tan(x) / sin(x) -> 1.0 / cos(x). */
7436 (simplify
7437 (rdiv (TAN:s @0) (SIN:s @0))
7438 (if (! HONOR_NANS (@0)
7439 && ! HONOR_INFINITIES (@0))
7440 (rdiv { build_one_cst (type); } (COS @0))))
7441
7442 /* Simplify pow(x,y) * pow(x,z) -> pow(x,y+z). */
7443 (simplify
7444 (mult (POW:s @0 @1) (POW:s @0 @2))
7445 (POW @0 (plus @1 @2)))
7446
7447 /* Simplify pow(x,y) * pow(z,y) -> pow(x*z,y). */
7448 (simplify
7449 (mult (POW:s @0 @1) (POW:s @2 @1))
7450 (POW (mult @0 @2) @1))
7451
7452 /* Simplify powi(x,y) * powi(z,y) -> powi(x*z,y). */
7453 (simplify
7454 (mult (POWI:s @0 @1) (POWI:s @2 @1))
7455 (POWI (mult @0 @2) @1))
7456
7457 /* Simplify pow(x,c) / x -> pow(x,c-1). */
7458 (simplify
7459 (rdiv (POW:s @0 REAL_CST@1) @0)
7460 (if (!TREE_OVERFLOW (@1))
7461 (POW @0 (minus @1 { build_one_cst (type); }))))
7462
7463 /* Simplify x / pow (y,z) -> x * pow(y,-z). */
7464 (simplify
7465 (rdiv @0 (POW:s @1 @2))
7466 (mult @0 (POW @1 (negate @2))))
7467
7468 (for sqrts (SQRT)
7469 cbrts (CBRT)
7470 pows (POW)
7471 /* sqrt(sqrt(x)) -> pow(x,1/4). */
7472 (simplify
7473 (sqrts (sqrts @0))
7474 (pows @0 { build_real (type, dconst_quarter ()); }))
7475 /* sqrt(cbrt(x)) -> pow(x,1/6). */
7476 (simplify
7477 (sqrts (cbrts @0))
7478 (pows @0 { build_real_truncate (type, dconst_sixth ()); }))
7479 /* cbrt(sqrt(x)) -> pow(x,1/6). */
7480 (simplify
7481 (cbrts (sqrts @0))
7482 (pows @0 { build_real_truncate (type, dconst_sixth ()); }))
7483 /* cbrt(cbrt(x)) -> pow(x,1/9), iff x is nonnegative. */
7484 (simplify
7485 (cbrts (cbrts tree_expr_nonnegative_p@0))
7486 (pows @0 { build_real_truncate (type, dconst_ninth ()); }))
7487 /* sqrt(pow(x,y)) -> pow(|x|,y*0.5). */
7488 (simplify
7489 (sqrts (pows @0 @1))
7490 (pows (abs @0) (mult @1 { build_real (type, dconsthalf); })))
7491 /* cbrt(pow(x,y)) -> pow(x,y/3), iff x is nonnegative. */
7492 (simplify
7493 (cbrts (pows tree_expr_nonnegative_p@0 @1))
7494 (pows @0 (mult @1 { build_real_truncate (type, dconst_third ()); })))
7495 /* pow(sqrt(x),y) -> pow(x,y*0.5). */
7496 (simplify
7497 (pows (sqrts @0) @1)
7498 (pows @0 (mult @1 { build_real (type, dconsthalf); })))
7499 /* pow(cbrt(x),y) -> pow(x,y/3) iff x is nonnegative. */
7500 (simplify
7501 (pows (cbrts tree_expr_nonnegative_p@0) @1)
7502 (pows @0 (mult @1 { build_real_truncate (type, dconst_third ()); })))
7503 /* pow(pow(x,y),z) -> pow(x,y*z) iff x is nonnegative. */
7504 (simplify
7505 (pows (pows tree_expr_nonnegative_p@0 @1) @2)
7506 (pows @0 (mult @1 @2))))
7507
7508 /* cabs(x+xi) -> fabs(x)*sqrt(2). */
7509 (simplify
7510 (CABS (complex @0 @0))
7511 (mult (abs @0) { build_real_truncate (type, dconst_sqrt2 ()); }))
7512
7513 /* hypot(x,x) -> fabs(x)*sqrt(2). */
7514 (simplify
7515 (HYPOT @0 @0)
7516 (mult (abs @0) { build_real_truncate (type, dconst_sqrt2 ()); }))
7517
7518 /* cexp(x+yi) -> exp(x)*cexpi(y). */
7519 (for cexps (CEXP)
7520 exps (EXP)
7521 cexpis (CEXPI)
7522 (simplify
7523 (cexps compositional_complex@0)
7524 (if (targetm.libc_has_function (function_c99_math_complex, TREE_TYPE (@0)))
7525 (complex
7526 (mult (exps@1 (realpart @0)) (realpart (cexpis:type@2 (imagpart @0))))
7527 (mult @1 (imagpart @2)))))))
7528
7529 (if (canonicalize_math_p ())
7530 /* floor(x) -> trunc(x) if x is nonnegative. */
7531 (for floors (FLOOR_ALL)
7532 truncs (TRUNC_ALL)
7533 (simplify
7534 (floors tree_expr_nonnegative_p@0)
7535 (truncs @0))))
7536
7537 (match double_value_p
7538 @0
7539 (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == double_type_node)))
7540 (for froms (BUILT_IN_TRUNCL
7541 BUILT_IN_FLOORL
7542 BUILT_IN_CEILL
7543 BUILT_IN_ROUNDL
7544 BUILT_IN_NEARBYINTL
7545 BUILT_IN_RINTL)
7546 tos (BUILT_IN_TRUNC
7547 BUILT_IN_FLOOR
7548 BUILT_IN_CEIL
7549 BUILT_IN_ROUND
7550 BUILT_IN_NEARBYINT
7551 BUILT_IN_RINT)
7552 /* truncl(extend(x)) -> extend(trunc(x)), etc., if x is a double. */
7553 (if (optimize && canonicalize_math_p ())
7554 (simplify
7555 (froms (convert double_value_p@0))
7556 (convert (tos @0)))))
7557
7558 (match float_value_p
7559 @0
7560 (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == float_type_node)))
7561 (for froms (BUILT_IN_TRUNCL BUILT_IN_TRUNC
7562 BUILT_IN_FLOORL BUILT_IN_FLOOR
7563 BUILT_IN_CEILL BUILT_IN_CEIL
7564 BUILT_IN_ROUNDL BUILT_IN_ROUND
7565 BUILT_IN_NEARBYINTL BUILT_IN_NEARBYINT
7566 BUILT_IN_RINTL BUILT_IN_RINT)
7567 tos (BUILT_IN_TRUNCF BUILT_IN_TRUNCF
7568 BUILT_IN_FLOORF BUILT_IN_FLOORF
7569 BUILT_IN_CEILF BUILT_IN_CEILF
7570 BUILT_IN_ROUNDF BUILT_IN_ROUNDF
7571 BUILT_IN_NEARBYINTF BUILT_IN_NEARBYINTF
7572 BUILT_IN_RINTF BUILT_IN_RINTF)
7573 /* truncl(extend(x)) and trunc(extend(x)) -> extend(truncf(x)), etc.,
7574 if x is a float. */
7575 (if (optimize && canonicalize_math_p ()
7576 && targetm.libc_has_function (function_c99_misc, NULL_TREE))
7577 (simplify
7578 (froms (convert float_value_p@0))
7579 (convert (tos @0)))))
7580
7581 #if GIMPLE
7582 (match float16_value_p
7583 @0
7584 (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == float16_type_node)))
7585 (for froms (BUILT_IN_TRUNCL BUILT_IN_TRUNC BUILT_IN_TRUNCF
7586 BUILT_IN_FLOORL BUILT_IN_FLOOR BUILT_IN_FLOORF
7587 BUILT_IN_CEILL BUILT_IN_CEIL BUILT_IN_CEILF
7588 BUILT_IN_ROUNDEVENL BUILT_IN_ROUNDEVEN BUILT_IN_ROUNDEVENF
7589 BUILT_IN_ROUNDL BUILT_IN_ROUND BUILT_IN_ROUNDF
7590 BUILT_IN_NEARBYINTL BUILT_IN_NEARBYINT BUILT_IN_NEARBYINTF
7591 BUILT_IN_RINTL BUILT_IN_RINT BUILT_IN_RINTF
7592 BUILT_IN_SQRTL BUILT_IN_SQRT BUILT_IN_SQRTF)
7593 tos (IFN_TRUNC IFN_TRUNC IFN_TRUNC
7594 IFN_FLOOR IFN_FLOOR IFN_FLOOR
7595 IFN_CEIL IFN_CEIL IFN_CEIL
7596 IFN_ROUNDEVEN IFN_ROUNDEVEN IFN_ROUNDEVEN
7597 IFN_ROUND IFN_ROUND IFN_ROUND
7598 IFN_NEARBYINT IFN_NEARBYINT IFN_NEARBYINT
7599 IFN_RINT IFN_RINT IFN_RINT
7600 IFN_SQRT IFN_SQRT IFN_SQRT)
7601 /* (_Float16) round ((doube) x) -> __built_in_roundf16 (x), etc.,
7602 if x is a _Float16. */
7603 (simplify
7604 (convert (froms (convert float16_value_p@0)))
7605 (if (optimize
7606 && types_match (type, TREE_TYPE (@0))
7607 && direct_internal_fn_supported_p (as_internal_fn (tos),
7608 type, OPTIMIZE_FOR_BOTH))
7609 (tos @0))))
7610
7611 /* Simplify (trunc)copysign ((extend)x, (extend)y) to copysignf (x, y),
7612 x,y is float value, similar for _Float16/double. */
7613 (for copysigns (COPYSIGN_ALL)
7614 (simplify
7615 (convert (copysigns (convert@2 @0) (convert @1)))
7616 (if (optimize
7617 && !HONOR_SNANS (@2)
7618 && types_match (type, TREE_TYPE (@0))
7619 && types_match (type, TREE_TYPE (@1))
7620 && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (@2))
7621 && direct_internal_fn_supported_p (IFN_COPYSIGN,
7622 type, OPTIMIZE_FOR_BOTH))
7623 (IFN_COPYSIGN @0 @1))))
7624
7625 (for froms (BUILT_IN_FMAF BUILT_IN_FMA BUILT_IN_FMAL)
7626 tos (IFN_FMA IFN_FMA IFN_FMA)
7627 (simplify
7628 (convert (froms (convert@3 @0) (convert @1) (convert @2)))
7629 (if (flag_unsafe_math_optimizations
7630 && optimize
7631 && FLOAT_TYPE_P (type)
7632 && FLOAT_TYPE_P (TREE_TYPE (@3))
7633 && types_match (type, TREE_TYPE (@0))
7634 && types_match (type, TREE_TYPE (@1))
7635 && types_match (type, TREE_TYPE (@2))
7636 && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (@3))
7637 && direct_internal_fn_supported_p (as_internal_fn (tos),
7638 type, OPTIMIZE_FOR_BOTH))
7639 (tos @0 @1 @2))))
7640
7641 (for maxmin (max min)
7642 (simplify
7643 (convert (maxmin (convert@2 @0) (convert @1)))
7644 (if (optimize
7645 && FLOAT_TYPE_P (type)
7646 && FLOAT_TYPE_P (TREE_TYPE (@2))
7647 && types_match (type, TREE_TYPE (@0))
7648 && types_match (type, TREE_TYPE (@1))
7649 && element_precision (type) < element_precision (TREE_TYPE (@2)))
7650 (maxmin @0 @1))))
7651 #endif
7652
7653 (for froms (XFLOORL XCEILL XROUNDL XRINTL)
7654 tos (XFLOOR XCEIL XROUND XRINT)
7655 /* llfloorl(extend(x)) -> llfloor(x), etc., if x is a double. */
7656 (if (optimize && canonicalize_math_p ())
7657 (simplify
7658 (froms (convert double_value_p@0))
7659 (tos @0))))
7660
7661 (for froms (XFLOORL XCEILL XROUNDL XRINTL
7662 XFLOOR XCEIL XROUND XRINT)
7663 tos (XFLOORF XCEILF XROUNDF XRINTF)
7664 /* llfloorl(extend(x)) and llfloor(extend(x)) -> llfloorf(x), etc.,
7665 if x is a float. */
7666 (if (optimize && canonicalize_math_p ())
7667 (simplify
7668 (froms (convert float_value_p@0))
7669 (tos @0))))
7670
7671 (if (canonicalize_math_p ())
7672 /* xfloor(x) -> fix_trunc(x) if x is nonnegative. */
7673 (for floors (IFLOOR LFLOOR LLFLOOR)
7674 (simplify
7675 (floors tree_expr_nonnegative_p@0)
7676 (fix_trunc @0))))
7677
7678 (if (canonicalize_math_p ())
7679 /* xfloor(x) -> fix_trunc(x), etc., if x is integer valued. */
7680 (for fns (IFLOOR LFLOOR LLFLOOR
7681 ICEIL LCEIL LLCEIL
7682 IROUND LROUND LLROUND)
7683 (simplify
7684 (fns integer_valued_real_p@0)
7685 (fix_trunc @0)))
7686 (if (!flag_errno_math)
7687 /* xrint(x) -> fix_trunc(x), etc., if x is integer valued. */
7688 (for rints (IRINT LRINT LLRINT)
7689 (simplify
7690 (rints integer_valued_real_p@0)
7691 (fix_trunc @0)))))
7692
7693 (if (canonicalize_math_p ())
7694 (for ifn (IFLOOR ICEIL IROUND IRINT)
7695 lfn (LFLOOR LCEIL LROUND LRINT)
7696 llfn (LLFLOOR LLCEIL LLROUND LLRINT)
7697 /* Canonicalize iround (x) to lround (x) on ILP32 targets where
7698 sizeof (int) == sizeof (long). */
7699 (if (TYPE_PRECISION (integer_type_node)
7700 == TYPE_PRECISION (long_integer_type_node))
7701 (simplify
7702 (ifn @0)
7703 (lfn:long_integer_type_node @0)))
7704 /* Canonicalize llround (x) to lround (x) on LP64 targets where
7705 sizeof (long long) == sizeof (long). */
7706 (if (TYPE_PRECISION (long_long_integer_type_node)
7707 == TYPE_PRECISION (long_integer_type_node))
7708 (simplify
7709 (llfn @0)
7710 (lfn:long_integer_type_node @0)))))
7711
7712 /* cproj(x) -> x if we're ignoring infinities. */
7713 (simplify
7714 (CPROJ @0)
7715 (if (!HONOR_INFINITIES (type))
7716 @0))
7717
7718 /* If the real part is inf and the imag part is known to be
7719 nonnegative, return (inf + 0i). */
7720 (simplify
7721 (CPROJ (complex REAL_CST@0 tree_expr_nonnegative_p@1))
7722 (if (real_isinf (TREE_REAL_CST_PTR (@0)))
7723 { build_complex_inf (type, false); }))
7724
7725 /* If the imag part is inf, return (inf+I*copysign(0,imag)). */
7726 (simplify
7727 (CPROJ (complex @0 REAL_CST@1))
7728 (if (real_isinf (TREE_REAL_CST_PTR (@1)))
7729 { build_complex_inf (type, TREE_REAL_CST_PTR (@1)->sign); }))
7730
7731 (for pows (POW)
7732 sqrts (SQRT)
7733 cbrts (CBRT)
7734 (simplify
7735 (pows @0 REAL_CST@1)
7736 (with {
7737 const REAL_VALUE_TYPE *value = TREE_REAL_CST_PTR (@1);
7738 REAL_VALUE_TYPE tmp;
7739 }
7740 (switch
7741 /* pow(x,0) -> 1. */
7742 (if (real_equal (value, &dconst0))
7743 { build_real (type, dconst1); })
7744 /* pow(x,1) -> x. */
7745 (if (real_equal (value, &dconst1))
7746 @0)
7747 /* pow(x,-1) -> 1/x. */
7748 (if (real_equal (value, &dconstm1))
7749 (rdiv { build_real (type, dconst1); } @0))
7750 /* pow(x,0.5) -> sqrt(x). */
7751 (if (flag_unsafe_math_optimizations
7752 && canonicalize_math_p ()
7753 && real_equal (value, &dconsthalf))
7754 (sqrts @0))
7755 /* pow(x,1/3) -> cbrt(x). */
7756 (if (flag_unsafe_math_optimizations
7757 && canonicalize_math_p ()
7758 && (tmp = real_value_truncate (TYPE_MODE (type), dconst_third ()),
7759 real_equal (value, &tmp)))
7760 (cbrts @0))))))
7761
7762 /* powi(1,x) -> 1. */
7763 (simplify
7764 (POWI real_onep@0 @1)
7765 @0)
7766
7767 (simplify
7768 (POWI @0 INTEGER_CST@1)
7769 (switch
7770 /* powi(x,0) -> 1. */
7771 (if (wi::to_wide (@1) == 0)
7772 { build_real (type, dconst1); })
7773 /* powi(x,1) -> x. */
7774 (if (wi::to_wide (@1) == 1)
7775 @0)
7776 /* powi(x,-1) -> 1/x. */
7777 (if (wi::to_wide (@1) == -1)
7778 (rdiv { build_real (type, dconst1); } @0))))
7779
7780 /* Narrowing of arithmetic and logical operations.
7781
7782 These are conceptually similar to the transformations performed for
7783 the C/C++ front-ends by shorten_binary_op and shorten_compare. Long
7784 term we want to move all that code out of the front-ends into here. */
7785
7786 /* Convert (outertype)((innertype0)a+(innertype1)b)
7787 into ((newtype)a+(newtype)b) where newtype
7788 is the widest mode from all of these. */
7789 (for op (plus minus mult rdiv)
7790 (simplify
7791 (convert (op:s@0 (convert1?@3 @1) (convert2?@4 @2)))
7792 /* If we have a narrowing conversion of an arithmetic operation where
7793 both operands are widening conversions from the same type as the outer
7794 narrowing conversion. Then convert the innermost operands to a
7795 suitable unsigned type (to avoid introducing undefined behavior),
7796 perform the operation and convert the result to the desired type. */
7797 (if (INTEGRAL_TYPE_P (type)
7798 && op != MULT_EXPR
7799 && op != RDIV_EXPR
7800 /* We check for type compatibility between @0 and @1 below,
7801 so there's no need to check that @2/@4 are integral types. */
7802 && INTEGRAL_TYPE_P (TREE_TYPE (@1))
7803 && INTEGRAL_TYPE_P (TREE_TYPE (@3))
7804 /* The precision of the type of each operand must match the
7805 precision of the mode of each operand, similarly for the
7806 result. */
7807 && type_has_mode_precision_p (TREE_TYPE (@1))
7808 && type_has_mode_precision_p (TREE_TYPE (@2))
7809 && type_has_mode_precision_p (type)
7810 /* The inner conversion must be a widening conversion. */
7811 && TYPE_PRECISION (TREE_TYPE (@3)) > TYPE_PRECISION (TREE_TYPE (@1))
7812 && types_match (@1, type)
7813 && (types_match (@1, @2)
7814 /* Or the second operand is const integer or converted const
7815 integer from valueize. */
7816 || poly_int_tree_p (@4)))
7817 (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
7818 (op @1 (convert @2))
7819 (with { tree utype = unsigned_type_for (TREE_TYPE (@1)); }
7820 (convert (op (convert:utype @1)
7821 (convert:utype @2)))))
7822 (if (FLOAT_TYPE_P (type)
7823 && DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0))
7824 == DECIMAL_FLOAT_TYPE_P (type))
7825 (with { tree arg0 = strip_float_extensions (@1);
7826 tree arg1 = strip_float_extensions (@2);
7827 tree itype = TREE_TYPE (@0);
7828 tree ty1 = TREE_TYPE (arg0);
7829 tree ty2 = TREE_TYPE (arg1);
7830 enum tree_code code = TREE_CODE (itype); }
7831 (if (FLOAT_TYPE_P (ty1)
7832 && FLOAT_TYPE_P (ty2))
7833 (with { tree newtype = type;
7834 if (TYPE_MODE (ty1) == SDmode
7835 || TYPE_MODE (ty2) == SDmode
7836 || TYPE_MODE (type) == SDmode)
7837 newtype = dfloat32_type_node;
7838 if (TYPE_MODE (ty1) == DDmode
7839 || TYPE_MODE (ty2) == DDmode
7840 || TYPE_MODE (type) == DDmode)
7841 newtype = dfloat64_type_node;
7842 if (TYPE_MODE (ty1) == TDmode
7843 || TYPE_MODE (ty2) == TDmode
7844 || TYPE_MODE (type) == TDmode)
7845 newtype = dfloat128_type_node; }
7846 (if ((newtype == dfloat32_type_node
7847 || newtype == dfloat64_type_node
7848 || newtype == dfloat128_type_node)
7849 && newtype == type
7850 && types_match (newtype, type))
7851 (op (convert:newtype @1) (convert:newtype @2))
7852 (with { if (element_precision (ty1) > element_precision (newtype))
7853 newtype = ty1;
7854 if (element_precision (ty2) > element_precision (newtype))
7855 newtype = ty2; }
7856 /* Sometimes this transformation is safe (cannot
7857 change results through affecting double rounding
7858 cases) and sometimes it is not. If NEWTYPE is
7859 wider than TYPE, e.g. (float)((long double)double
7860 + (long double)double) converted to
7861 (float)(double + double), the transformation is
7862 unsafe regardless of the details of the types
7863 involved; double rounding can arise if the result
7864 of NEWTYPE arithmetic is a NEWTYPE value half way
7865 between two representable TYPE values but the
7866 exact value is sufficiently different (in the
7867 right direction) for this difference to be
7868 visible in ITYPE arithmetic. If NEWTYPE is the
7869 same as TYPE, however, the transformation may be
7870 safe depending on the types involved: it is safe
7871 if the ITYPE has strictly more than twice as many
7872 mantissa bits as TYPE, can represent infinities
7873 and NaNs if the TYPE can, and has sufficient
7874 exponent range for the product or ratio of two
7875 values representable in the TYPE to be within the
7876 range of normal values of ITYPE. */
7877 (if (element_precision (newtype) < element_precision (itype)
7878 && (!VECTOR_MODE_P (TYPE_MODE (newtype))
7879 || target_supports_op_p (newtype, op, optab_default))
7880 && (flag_unsafe_math_optimizations
7881 || (element_precision (newtype) == element_precision (type)
7882 && real_can_shorten_arithmetic (element_mode (itype),
7883 element_mode (type))
7884 && !excess_precision_type (newtype)))
7885 && !types_match (itype, newtype))
7886 (convert:type (op (convert:newtype @1)
7887 (convert:newtype @2)))
7888 )))) )
7889 ))
7890 )))
7891
7892 /* This is another case of narrowing, specifically when there's an outer
7893 BIT_AND_EXPR which masks off bits outside the type of the innermost
7894 operands. Like the previous case we have to convert the operands
7895 to unsigned types to avoid introducing undefined behavior for the
7896 arithmetic operation. */
7897 (for op (minus plus)
7898 (simplify
7899 (bit_and (op:s (convert@2 @0) (convert@3 @1)) INTEGER_CST@4)
7900 (if (INTEGRAL_TYPE_P (type)
7901 /* We check for type compatibility between @0 and @1 below,
7902 so there's no need to check that @1/@3 are integral types. */
7903 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
7904 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
7905 /* The precision of the type of each operand must match the
7906 precision of the mode of each operand, similarly for the
7907 result. */
7908 && type_has_mode_precision_p (TREE_TYPE (@0))
7909 && type_has_mode_precision_p (TREE_TYPE (@1))
7910 && type_has_mode_precision_p (type)
7911 /* The inner conversion must be a widening conversion. */
7912 && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
7913 && types_match (@0, @1)
7914 && (tree_int_cst_min_precision (@4, TYPE_SIGN (TREE_TYPE (@0)))
7915 <= TYPE_PRECISION (TREE_TYPE (@0)))
7916 && (wi::to_wide (@4)
7917 & wi::mask (TYPE_PRECISION (TREE_TYPE (@0)),
7918 true, TYPE_PRECISION (type))) == 0)
7919 (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
7920 (with { tree ntype = TREE_TYPE (@0); }
7921 (convert (bit_and (op @0 @1) (convert:ntype @4))))
7922 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
7923 (convert (bit_and (op (convert:utype @0) (convert:utype @1))
7924 (convert:utype @4))))))))
7925
7926 /* Transform (@0 < @1 and @0 < @2) to use min,
7927 (@0 > @1 and @0 > @2) to use max */
7928 (for logic (bit_and bit_and bit_and bit_and bit_ior bit_ior bit_ior bit_ior)
7929 op (lt le gt ge lt le gt ge )
7930 ext (min min max max max max min min )
7931 (simplify
7932 (logic (op:cs @0 @1) (op:cs @0 @2))
7933 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
7934 && TREE_CODE (@0) != INTEGER_CST)
7935 (op @0 (ext @1 @2)))))
7936
7937 /* Max<bool0, bool1> -> bool0 | bool1
7938 Min<bool0, bool1> -> bool0 & bool1 */
7939 (for op (max min)
7940 logic (bit_ior bit_and)
7941 (simplify
7942 (op zero_one_valued_p@0 zero_one_valued_p@1)
7943 (logic @0 @1)))
7944
7945 /* signbit(x) != 0 ? -x : x -> abs(x)
7946 signbit(x) == 0 ? -x : x -> -abs(x) */
7947 (for sign (SIGNBIT)
7948 (for neeq (ne eq)
7949 (simplify
7950 (cond (neeq (sign @0) integer_zerop) (negate @0) @0)
7951 (if (neeq == NE_EXPR)
7952 (abs @0)
7953 (negate (abs @0))))))
7954
7955 (simplify
7956 /* signbit(x) -> 0 if x is nonnegative. */
7957 (SIGNBIT tree_expr_nonnegative_p@0)
7958 { integer_zero_node; })
7959
7960 (simplify
7961 /* signbit(x) -> x<0 if x doesn't have signed zeros. */
7962 (SIGNBIT @0)
7963 (if (!HONOR_SIGNED_ZEROS (@0))
7964 (convert (lt @0 { build_real (TREE_TYPE (@0), dconst0); }))))
7965
7966 /* Transform comparisons of the form X +- C1 CMP C2 to X CMP C2 -+ C1. */
7967 (for cmp (eq ne)
7968 (for op (plus minus)
7969 rop (minus plus)
7970 (simplify
7971 (cmp (op@3 @0 INTEGER_CST@1) INTEGER_CST@2)
7972 (if (!TREE_OVERFLOW (@1) && !TREE_OVERFLOW (@2)
7973 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0))
7974 && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@0))
7975 && !TYPE_SATURATING (TREE_TYPE (@0)))
7976 (with { tree res = int_const_binop (rop, @2, @1); }
7977 (if (TREE_OVERFLOW (res)
7978 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
7979 { constant_boolean_node (cmp == NE_EXPR, type); }
7980 (if (single_use (@3))
7981 (cmp @0 { TREE_OVERFLOW (res)
7982 ? drop_tree_overflow (res) : res; }))))))))
7983 (for cmp (lt le gt ge)
7984 (for op (plus minus)
7985 rop (minus plus)
7986 (simplify
7987 (cmp (op@3 @0 INTEGER_CST@1) INTEGER_CST@2)
7988 (if (!TREE_OVERFLOW (@1) && !TREE_OVERFLOW (@2)
7989 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
7990 (with { tree res = int_const_binop (rop, @2, @1); }
7991 (if (TREE_OVERFLOW (res))
7992 {
7993 fold_overflow_warning (("assuming signed overflow does not occur "
7994 "when simplifying conditional to constant"),
7995 WARN_STRICT_OVERFLOW_CONDITIONAL);
7996 bool less = cmp == LE_EXPR || cmp == LT_EXPR;
7997 /* wi::ges_p (@2, 0) should be sufficient for a signed type. */
7998 bool ovf_high = wi::lt_p (wi::to_wide (@1), 0,
7999 TYPE_SIGN (TREE_TYPE (@1)))
8000 != (op == MINUS_EXPR);
8001 constant_boolean_node (less == ovf_high, type);
8002 }
8003 (if (single_use (@3))
8004 (with
8005 {
8006 fold_overflow_warning (("assuming signed overflow does not occur "
8007 "when changing X +- C1 cmp C2 to "
8008 "X cmp C2 -+ C1"),
8009 WARN_STRICT_OVERFLOW_COMPARISON);
8010 }
8011 (cmp @0 { res; })))))))))
8012
8013 /* Canonicalizations of BIT_FIELD_REFs. */
8014
8015 (simplify
8016 (BIT_FIELD_REF (BIT_FIELD_REF @0 @1 @2) @3 @4)
8017 (BIT_FIELD_REF @0 @3 { const_binop (PLUS_EXPR, bitsizetype, @2, @4); }))
8018
8019 (simplify
8020 (BIT_FIELD_REF (view_convert @0) @1 @2)
8021 (BIT_FIELD_REF @0 @1 @2))
8022
8023 (simplify
8024 (BIT_FIELD_REF @0 @1 integer_zerop)
8025 (if (tree_int_cst_equal (@1, TYPE_SIZE (TREE_TYPE (@0))))
8026 (view_convert @0)))
8027
8028 (simplify
8029 (BIT_FIELD_REF @0 @1 @2)
8030 (switch
8031 (if (TREE_CODE (TREE_TYPE (@0)) == COMPLEX_TYPE
8032 && tree_int_cst_equal (@1, TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))))
8033 (switch
8034 (if (integer_zerop (@2))
8035 (view_convert (realpart @0)))
8036 (if (tree_int_cst_equal (@2, TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))))
8037 (view_convert (imagpart @0)))))
8038 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
8039 && INTEGRAL_TYPE_P (type)
8040 /* On GIMPLE this should only apply to register arguments. */
8041 && (! GIMPLE || is_gimple_reg (@0))
8042 /* A bit-field-ref that referenced the full argument can be stripped. */
8043 && ((compare_tree_int (@1, TYPE_PRECISION (TREE_TYPE (@0))) == 0
8044 && integer_zerop (@2))
8045 /* Low-parts can be reduced to integral conversions.
8046 ??? The following doesn't work for PDP endian. */
8047 || (BYTES_BIG_ENDIAN == WORDS_BIG_ENDIAN
8048 /* But only do this after vectorization. */
8049 && canonicalize_math_after_vectorization_p ()
8050 /* Don't even think about BITS_BIG_ENDIAN. */
8051 && TYPE_PRECISION (TREE_TYPE (@0)) % BITS_PER_UNIT == 0
8052 && TYPE_PRECISION (type) % BITS_PER_UNIT == 0
8053 && compare_tree_int (@2, (BYTES_BIG_ENDIAN
8054 ? (TYPE_PRECISION (TREE_TYPE (@0))
8055 - TYPE_PRECISION (type))
8056 : 0)) == 0)))
8057 (convert @0))))
8058
8059 /* Simplify vector extracts. */
8060
8061 (simplify
8062 (BIT_FIELD_REF CONSTRUCTOR@0 @1 @2)
8063 (if (VECTOR_TYPE_P (TREE_TYPE (@0))
8064 && tree_fits_uhwi_p (TYPE_SIZE (type))
8065 && ((tree_to_uhwi (TYPE_SIZE (type))
8066 == tree_to_uhwi (TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))))
8067 || (VECTOR_TYPE_P (type)
8068 && (tree_to_uhwi (TYPE_SIZE (TREE_TYPE (type)))
8069 == tree_to_uhwi (TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0))))))))
8070 (with
8071 {
8072 tree ctor = (TREE_CODE (@0) == SSA_NAME
8073 ? gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0)) : @0);
8074 tree eltype = TREE_TYPE (TREE_TYPE (ctor));
8075 unsigned HOST_WIDE_INT width = tree_to_uhwi (TYPE_SIZE (eltype));
8076 unsigned HOST_WIDE_INT n = tree_to_uhwi (@1);
8077 unsigned HOST_WIDE_INT idx = tree_to_uhwi (@2);
8078 }
8079 (if (n != 0
8080 && (idx % width) == 0
8081 && (n % width) == 0
8082 && known_le ((idx + n) / width,
8083 TYPE_VECTOR_SUBPARTS (TREE_TYPE (ctor))))
8084 (with
8085 {
8086 idx = idx / width;
8087 n = n / width;
8088 /* Constructor elements can be subvectors. */
8089 poly_uint64 k = 1;
8090 if (CONSTRUCTOR_NELTS (ctor) != 0)
8091 {
8092 tree cons_elem = TREE_TYPE (CONSTRUCTOR_ELT (ctor, 0)->value);
8093 if (TREE_CODE (cons_elem) == VECTOR_TYPE)
8094 k = TYPE_VECTOR_SUBPARTS (cons_elem);
8095 }
8096 unsigned HOST_WIDE_INT elt, count, const_k;
8097 }
8098 (switch
8099 /* We keep an exact subset of the constructor elements. */
8100 (if (multiple_p (idx, k, &elt) && multiple_p (n, k, &count))
8101 (if (CONSTRUCTOR_NELTS (ctor) == 0)
8102 { build_zero_cst (type); }
8103 (if (count == 1)
8104 (if (elt < CONSTRUCTOR_NELTS (ctor))
8105 (view_convert { CONSTRUCTOR_ELT (ctor, elt)->value; })
8106 { build_zero_cst (type); })
8107 /* We don't want to emit new CTORs unless the old one goes away.
8108 ??? Eventually allow this if the CTOR ends up constant or
8109 uniform. */
8110 (if (single_use (@0))
8111 (with
8112 {
8113 vec<constructor_elt, va_gc> *vals;
8114 vec_alloc (vals, count);
8115 bool constant_p = true;
8116 tree res;
8117 for (unsigned i = 0;
8118 i < count && elt + i < CONSTRUCTOR_NELTS (ctor); ++i)
8119 {
8120 tree e = CONSTRUCTOR_ELT (ctor, elt + i)->value;
8121 CONSTRUCTOR_APPEND_ELT (vals, NULL_TREE, e);
8122 if (!CONSTANT_CLASS_P (e))
8123 constant_p = false;
8124 }
8125 tree evtype = (types_match (TREE_TYPE (type),
8126 TREE_TYPE (TREE_TYPE (ctor)))
8127 ? type
8128 : build_vector_type (TREE_TYPE (TREE_TYPE (ctor)),
8129 count * k));
8130 /* We used to build a CTOR in the non-constant case here
8131 but that's not a GIMPLE value. We'd have to expose this
8132 operation somehow so the code generation can properly
8133 split it out to a separate stmt. */
8134 res = (constant_p ? build_vector_from_ctor (evtype, vals)
8135 : (GIMPLE ? NULL_TREE : build_constructor (evtype, vals)));
8136 }
8137 (if (res)
8138 (view_convert { res; })))))))
8139 /* The bitfield references a single constructor element. */
8140 (if (k.is_constant (&const_k)
8141 && idx + n <= (idx / const_k + 1) * const_k)
8142 (switch
8143 (if (CONSTRUCTOR_NELTS (ctor) <= idx / const_k)
8144 { build_zero_cst (type); })
8145 (if (n == const_k)
8146 (view_convert { CONSTRUCTOR_ELT (ctor, idx / const_k)->value; }))
8147 (BIT_FIELD_REF { CONSTRUCTOR_ELT (ctor, idx / const_k)->value; }
8148 @1 { bitsize_int ((idx % const_k) * width); })))))))))
8149
8150 /* Simplify a bit extraction from a bit insertion for the cases with
8151 the inserted element fully covering the extraction or the insertion
8152 not touching the extraction. */
8153 (simplify
8154 (BIT_FIELD_REF (bit_insert @0 @1 @ipos) @rsize @rpos)
8155 (with
8156 {
8157 unsigned HOST_WIDE_INT isize;
8158 if (INTEGRAL_TYPE_P (TREE_TYPE (@1)))
8159 isize = TYPE_PRECISION (TREE_TYPE (@1));
8160 else
8161 isize = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (@1)));
8162 }
8163 (switch
8164 (if ((!INTEGRAL_TYPE_P (TREE_TYPE (@1))
8165 || type_has_mode_precision_p (TREE_TYPE (@1)))
8166 && wi::leu_p (wi::to_wide (@ipos), wi::to_wide (@rpos))
8167 && wi::leu_p (wi::to_wide (@rpos) + wi::to_wide (@rsize),
8168 wi::to_wide (@ipos) + isize))
8169 (BIT_FIELD_REF @1 @rsize { wide_int_to_tree (bitsizetype,
8170 wi::to_wide (@rpos)
8171 - wi::to_wide (@ipos)); }))
8172 (if (wi::eq_p (wi::to_wide (@ipos), wi::to_wide (@rpos))
8173 && compare_tree_int (@rsize, isize) == 0)
8174 (convert @1))
8175 (if (wi::geu_p (wi::to_wide (@ipos),
8176 wi::to_wide (@rpos) + wi::to_wide (@rsize))
8177 || wi::geu_p (wi::to_wide (@rpos),
8178 wi::to_wide (@ipos) + isize))
8179 (BIT_FIELD_REF @0 @rsize @rpos)))))
8180
8181 /* Simplify vector inserts of other vector extracts to a permute. */
8182 (simplify
8183 (bit_insert @0 (BIT_FIELD_REF@2 @1 @rsize @rpos) @ipos)
8184 (if (VECTOR_TYPE_P (type)
8185 && types_match (@0, @1)
8186 && types_match (TREE_TYPE (TREE_TYPE (@0)), TREE_TYPE (@2))
8187 && TYPE_VECTOR_SUBPARTS (type).is_constant ())
8188 (with
8189 {
8190 unsigned HOST_WIDE_INT elsz
8191 = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (TREE_TYPE (@1))));
8192 poly_uint64 relt = exact_div (tree_to_poly_uint64 (@rpos), elsz);
8193 poly_uint64 ielt = exact_div (tree_to_poly_uint64 (@ipos), elsz);
8194 unsigned nunits = TYPE_VECTOR_SUBPARTS (type).to_constant ();
8195 vec_perm_builder builder;
8196 builder.new_vector (nunits, nunits, 1);
8197 for (unsigned i = 0; i < nunits; ++i)
8198 builder.quick_push (known_eq (ielt, i) ? nunits + relt : i);
8199 vec_perm_indices sel (builder, 2, nunits);
8200 }
8201 (if (!VECTOR_MODE_P (TYPE_MODE (type))
8202 || can_vec_perm_const_p (TYPE_MODE (type), TYPE_MODE (type), sel, false))
8203 (vec_perm @0 @1 { vec_perm_indices_to_tree
8204 (build_vector_type (ssizetype, nunits), sel); })))))
8205
8206 (if (canonicalize_math_after_vectorization_p ())
8207 (for fmas (FMA)
8208 (simplify
8209 (fmas:c (negate @0) @1 @2)
8210 (IFN_FNMA @0 @1 @2))
8211 (simplify
8212 (fmas @0 @1 (negate @2))
8213 (IFN_FMS @0 @1 @2))
8214 (simplify
8215 (fmas:c (negate @0) @1 (negate @2))
8216 (IFN_FNMS @0 @1 @2))
8217 (simplify
8218 (negate (fmas@3 @0 @1 @2))
8219 (if (single_use (@3))
8220 (IFN_FNMS @0 @1 @2))))
8221
8222 (simplify
8223 (IFN_FMS:c (negate @0) @1 @2)
8224 (IFN_FNMS @0 @1 @2))
8225 (simplify
8226 (IFN_FMS @0 @1 (negate @2))
8227 (IFN_FMA @0 @1 @2))
8228 (simplify
8229 (IFN_FMS:c (negate @0) @1 (negate @2))
8230 (IFN_FNMA @0 @1 @2))
8231 (simplify
8232 (negate (IFN_FMS@3 @0 @1 @2))
8233 (if (single_use (@3))
8234 (IFN_FNMA @0 @1 @2)))
8235
8236 (simplify
8237 (IFN_FNMA:c (negate @0) @1 @2)
8238 (IFN_FMA @0 @1 @2))
8239 (simplify
8240 (IFN_FNMA @0 @1 (negate @2))
8241 (IFN_FNMS @0 @1 @2))
8242 (simplify
8243 (IFN_FNMA:c (negate @0) @1 (negate @2))
8244 (IFN_FMS @0 @1 @2))
8245 (simplify
8246 (negate (IFN_FNMA@3 @0 @1 @2))
8247 (if (single_use (@3))
8248 (IFN_FMS @0 @1 @2)))
8249
8250 (simplify
8251 (IFN_FNMS:c (negate @0) @1 @2)
8252 (IFN_FMS @0 @1 @2))
8253 (simplify
8254 (IFN_FNMS @0 @1 (negate @2))
8255 (IFN_FNMA @0 @1 @2))
8256 (simplify
8257 (IFN_FNMS:c (negate @0) @1 (negate @2))
8258 (IFN_FMA @0 @1 @2))
8259 (simplify
8260 (negate (IFN_FNMS@3 @0 @1 @2))
8261 (if (single_use (@3))
8262 (IFN_FMA @0 @1 @2))))
8263
8264 /* CLZ simplifications. */
8265 (for clz (CLZ)
8266 (for op (eq ne)
8267 cmp (lt ge)
8268 (simplify
8269 (op (clz:s@2 @0) INTEGER_CST@1)
8270 (if (integer_zerop (@1) && single_use (@2))
8271 /* clz(X) == 0 is (int)X < 0 and clz(X) != 0 is (int)X >= 0. */
8272 (with { tree type0 = TREE_TYPE (@0);
8273 tree stype = signed_type_for (type0);
8274 HOST_WIDE_INT val = 0;
8275 /* Punt on hypothetical weird targets. */
8276 if (clz == CFN_CLZ
8277 && CLZ_DEFINED_VALUE_AT_ZERO (SCALAR_TYPE_MODE (type0),
8278 val) == 2
8279 && val == 0)
8280 stype = NULL_TREE;
8281 }
8282 (if (stype)
8283 (cmp (convert:stype @0) { build_zero_cst (stype); })))
8284 /* clz(X) == (prec-1) is X == 1 and clz(X) != (prec-1) is X != 1. */
8285 (with { bool ok = true;
8286 HOST_WIDE_INT val = 0;
8287 tree type0 = TREE_TYPE (@0);
8288 /* Punt on hypothetical weird targets. */
8289 if (clz == CFN_CLZ
8290 && CLZ_DEFINED_VALUE_AT_ZERO (SCALAR_TYPE_MODE (type0),
8291 val) == 2
8292 && val == TYPE_PRECISION (type0) - 1)
8293 ok = false;
8294 }
8295 (if (ok && wi::to_wide (@1) == (TYPE_PRECISION (type0) - 1))
8296 (op @0 { build_one_cst (type0); })))))))
8297
8298 /* CTZ simplifications. */
8299 (for ctz (CTZ)
8300 (for op (ge gt le lt)
8301 cmp (eq eq ne ne)
8302 (simplify
8303 /* __builtin_ctz (x) >= C -> (x & ((1 << C) - 1)) == 0. */
8304 (op (ctz:s @0) INTEGER_CST@1)
8305 (with { bool ok = true;
8306 HOST_WIDE_INT val = 0;
8307 if (!tree_fits_shwi_p (@1))
8308 ok = false;
8309 else
8310 {
8311 val = tree_to_shwi (@1);
8312 /* Canonicalize to >= or <. */
8313 if (op == GT_EXPR || op == LE_EXPR)
8314 {
8315 if (val == HOST_WIDE_INT_MAX)
8316 ok = false;
8317 else
8318 val++;
8319 }
8320 }
8321 bool zero_res = false;
8322 HOST_WIDE_INT zero_val = 0;
8323 tree type0 = TREE_TYPE (@0);
8324 int prec = TYPE_PRECISION (type0);
8325 if (ctz == CFN_CTZ
8326 && CTZ_DEFINED_VALUE_AT_ZERO (SCALAR_TYPE_MODE (type0),
8327 zero_val) == 2)
8328 zero_res = true;
8329 }
8330 (if (val <= 0)
8331 (if (ok && (!zero_res || zero_val >= val))
8332 { constant_boolean_node (cmp == EQ_EXPR ? true : false, type); })
8333 (if (val >= prec)
8334 (if (ok && (!zero_res || zero_val < val))
8335 { constant_boolean_node (cmp == EQ_EXPR ? false : true, type); })
8336 (if (ok && (!zero_res || zero_val < 0 || zero_val >= prec))
8337 (cmp (bit_and @0 { wide_int_to_tree (type0,
8338 wi::mask (val, false, prec)); })
8339 { build_zero_cst (type0); })))))))
8340 (for op (eq ne)
8341 (simplify
8342 /* __builtin_ctz (x) == C -> (x & ((1 << (C + 1)) - 1)) == (1 << C). */
8343 (op (ctz:s @0) INTEGER_CST@1)
8344 (with { bool zero_res = false;
8345 HOST_WIDE_INT zero_val = 0;
8346 tree type0 = TREE_TYPE (@0);
8347 int prec = TYPE_PRECISION (type0);
8348 if (ctz == CFN_CTZ
8349 && CTZ_DEFINED_VALUE_AT_ZERO (SCALAR_TYPE_MODE (type0),
8350 zero_val) == 2)
8351 zero_res = true;
8352 }
8353 (if (tree_int_cst_sgn (@1) < 0 || wi::to_widest (@1) >= prec)
8354 (if (!zero_res || zero_val != wi::to_widest (@1))
8355 { constant_boolean_node (op == EQ_EXPR ? false : true, type); })
8356 (if (!zero_res || zero_val < 0 || zero_val >= prec)
8357 (op (bit_and @0 { wide_int_to_tree (type0,
8358 wi::mask (tree_to_uhwi (@1) + 1,
8359 false, prec)); })
8360 { wide_int_to_tree (type0,
8361 wi::shifted_mask (tree_to_uhwi (@1), 1,
8362 false, prec)); })))))))
8363
8364 /* POPCOUNT simplifications. */
8365 /* popcount(X) + popcount(Y) is popcount(X|Y) when X&Y must be zero. */
8366 (simplify
8367 (plus (POPCOUNT:s @0) (POPCOUNT:s @1))
8368 (if (INTEGRAL_TYPE_P (type)
8369 && wi::bit_and (tree_nonzero_bits (@0), tree_nonzero_bits (@1)) == 0)
8370 (POPCOUNT (bit_ior @0 @1))))
8371
8372 /* popcount(X) == 0 is X == 0, and related (in)equalities. */
8373 (for popcount (POPCOUNT)
8374 (for cmp (le eq ne gt)
8375 rep (eq eq ne ne)
8376 (simplify
8377 (cmp (popcount @0) integer_zerop)
8378 (rep @0 { build_zero_cst (TREE_TYPE (@0)); }))))
8379
8380 /* popcount(bswap(x)) is popcount(x). */
8381 (for popcount (POPCOUNT)
8382 (for bswap (BUILT_IN_BSWAP16 BUILT_IN_BSWAP32
8383 BUILT_IN_BSWAP64 BUILT_IN_BSWAP128)
8384 (simplify
8385 (popcount (convert?@0 (bswap:s@1 @2)))
8386 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
8387 && INTEGRAL_TYPE_P (TREE_TYPE (@1)))
8388 (with { tree type0 = TREE_TYPE (@0);
8389 tree type1 = TREE_TYPE (@1);
8390 unsigned int prec0 = TYPE_PRECISION (type0);
8391 unsigned int prec1 = TYPE_PRECISION (type1); }
8392 (if (prec0 == prec1 || (prec0 > prec1 && TYPE_UNSIGNED (type1)))
8393 (popcount (convert:type0 (convert:type1 @2)))))))))
8394
8395 /* popcount(rotate(X Y)) is popcount(X). */
8396 (for popcount (POPCOUNT)
8397 (for rot (lrotate rrotate)
8398 (simplify
8399 (popcount (convert?@0 (rot:s@1 @2 @3)))
8400 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
8401 && INTEGRAL_TYPE_P (TREE_TYPE (@1))
8402 && (GIMPLE || !TREE_SIDE_EFFECTS (@3)))
8403 (with { tree type0 = TREE_TYPE (@0);
8404 tree type1 = TREE_TYPE (@1);
8405 unsigned int prec0 = TYPE_PRECISION (type0);
8406 unsigned int prec1 = TYPE_PRECISION (type1); }
8407 (if (prec0 == prec1 || (prec0 > prec1 && TYPE_UNSIGNED (type1)))
8408 (popcount (convert:type0 @2))))))))
8409
8410 /* Canonicalize POPCOUNT(x)&1 as PARITY(X). */
8411 (simplify
8412 (bit_and (POPCOUNT @0) integer_onep)
8413 (PARITY @0))
8414
8415 /* popcount(X&Y) + popcount(X|Y) is popcount(x) + popcount(Y). */
8416 (simplify
8417 (plus:c (POPCOUNT:s (bit_and:s @0 @1)) (POPCOUNT:s (bit_ior:cs @0 @1)))
8418 (plus (POPCOUNT @0) (POPCOUNT @1)))
8419
8420 /* popcount(X) + popcount(Y) - popcount(X&Y) is popcount(X|Y). */
8421 /* popcount(X) + popcount(Y) - popcount(X|Y) is popcount(X&Y). */
8422 (for popcount (POPCOUNT)
8423 (for log1 (bit_and bit_ior)
8424 log2 (bit_ior bit_and)
8425 (simplify
8426 (minus (plus:s (popcount:s @0) (popcount:s @1))
8427 (popcount:s (log1:cs @0 @1)))
8428 (popcount (log2 @0 @1)))
8429 (simplify
8430 (plus:c (minus:s (popcount:s @0) (popcount:s (log1:cs @0 @1)))
8431 (popcount:s @1))
8432 (popcount (log2 @0 @1)))))
8433
8434 /* PARITY simplifications. */
8435 /* parity(~X) is parity(X). */
8436 (simplify
8437 (PARITY (bit_not @0))
8438 (PARITY @0))
8439
8440 /* parity(bswap(x)) is parity(x). */
8441 (for parity (PARITY)
8442 (for bswap (BUILT_IN_BSWAP16 BUILT_IN_BSWAP32
8443 BUILT_IN_BSWAP64 BUILT_IN_BSWAP128)
8444 (simplify
8445 (parity (convert?@0 (bswap:s@1 @2)))
8446 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
8447 && INTEGRAL_TYPE_P (TREE_TYPE (@1))
8448 && TYPE_PRECISION (TREE_TYPE (@0))
8449 >= TYPE_PRECISION (TREE_TYPE (@1)))
8450 (with { tree type0 = TREE_TYPE (@0);
8451 tree type1 = TREE_TYPE (@1); }
8452 (parity (convert:type0 (convert:type1 @2))))))))
8453
8454 /* parity(rotate(X Y)) is parity(X). */
8455 (for parity (PARITY)
8456 (for rot (lrotate rrotate)
8457 (simplify
8458 (parity (convert?@0 (rot:s@1 @2 @3)))
8459 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
8460 && INTEGRAL_TYPE_P (TREE_TYPE (@1))
8461 && (GIMPLE || !TREE_SIDE_EFFECTS (@3))
8462 && TYPE_PRECISION (TREE_TYPE (@0))
8463 >= TYPE_PRECISION (TREE_TYPE (@1)))
8464 (with { tree type0 = TREE_TYPE (@0); }
8465 (parity (convert:type0 @2)))))))
8466
8467 /* parity(X)^parity(Y) is parity(X^Y). */
8468 (simplify
8469 (bit_xor (PARITY:s @0) (PARITY:s @1))
8470 (PARITY (bit_xor @0 @1)))
8471
8472 /* a != 0 ? FUN(a) : 0 -> Fun(a) for some builtin functions. */
8473 (for func (POPCOUNT BSWAP FFS PARITY)
8474 (simplify
8475 (cond (ne @0 integer_zerop@1) (func@3 (convert? @0)) integer_zerop@2)
8476 @3))
8477
8478 /* a != 0 ? FUN(a) : CST -> Fun(a) for some CLRSB builtins
8479 where CST is precision-1. */
8480 (for func (CLRSB)
8481 (simplify
8482 (cond (ne @0 integer_zerop@1) (func@4 (convert?@3 @0)) INTEGER_CST@2)
8483 (if (wi::to_widest (@2) == TYPE_PRECISION (TREE_TYPE (@3)) - 1)
8484 @4)))
8485
8486 #if GIMPLE
8487 /* a != 0 ? CLZ(a) : CST -> .CLZ(a) where CST is the result of the internal function for 0. */
8488 (for func (CLZ)
8489 (simplify
8490 (cond (ne @0 integer_zerop@1) (func (convert?@3 @0)) INTEGER_CST@2)
8491 (with { int val;
8492 internal_fn ifn = IFN_LAST;
8493 if (direct_internal_fn_supported_p (IFN_CLZ, type, OPTIMIZE_FOR_BOTH)
8494 && CLZ_DEFINED_VALUE_AT_ZERO (SCALAR_INT_TYPE_MODE (type),
8495 val) == 2)
8496 ifn = IFN_CLZ;
8497 }
8498 (if (ifn == IFN_CLZ && wi::to_widest (@2) == val)
8499 (IFN_CLZ @3)))))
8500
8501 /* a != 0 ? CTZ(a) : CST -> .CTZ(a) where CST is the result of the internal function for 0. */
8502 (for func (CTZ)
8503 (simplify
8504 (cond (ne @0 integer_zerop@1) (func (convert?@3 @0)) INTEGER_CST@2)
8505 (with { int val;
8506 internal_fn ifn = IFN_LAST;
8507 if (direct_internal_fn_supported_p (IFN_CTZ, type, OPTIMIZE_FOR_BOTH)
8508 && CTZ_DEFINED_VALUE_AT_ZERO (SCALAR_INT_TYPE_MODE (type),
8509 val) == 2)
8510 ifn = IFN_CTZ;
8511 }
8512 (if (ifn == IFN_CTZ && wi::to_widest (@2) == val)
8513 (IFN_CTZ @3)))))
8514 #endif
8515
8516 /* Common POPCOUNT/PARITY simplifications. */
8517 /* popcount(X&C1) is (X>>C2)&1 when C1 == 1<<C2. Same for parity(X&C1). */
8518 (for pfun (POPCOUNT PARITY)
8519 (simplify
8520 (pfun @0)
8521 (if (INTEGRAL_TYPE_P (type))
8522 (with { wide_int nz = tree_nonzero_bits (@0); }
8523 (switch
8524 (if (nz == 1)
8525 (convert @0))
8526 (if (wi::popcount (nz) == 1)
8527 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
8528 (convert (rshift:utype (convert:utype @0)
8529 { build_int_cst (integer_type_node,
8530 wi::ctz (nz)); })))))))))
8531
8532 #if GIMPLE
8533 /* 64- and 32-bits branchless implementations of popcount are detected:
8534
8535 int popcount64c (uint64_t x)
8536 {
8537 x -= (x >> 1) & 0x5555555555555555ULL;
8538 x = (x & 0x3333333333333333ULL) + ((x >> 2) & 0x3333333333333333ULL);
8539 x = (x + (x >> 4)) & 0x0f0f0f0f0f0f0f0fULL;
8540 return (x * 0x0101010101010101ULL) >> 56;
8541 }
8542
8543 int popcount32c (uint32_t x)
8544 {
8545 x -= (x >> 1) & 0x55555555;
8546 x = (x & 0x33333333) + ((x >> 2) & 0x33333333);
8547 x = (x + (x >> 4)) & 0x0f0f0f0f;
8548 return (x * 0x01010101) >> 24;
8549 } */
8550 (simplify
8551 (rshift
8552 (mult
8553 (bit_and
8554 (plus:c
8555 (rshift @8 INTEGER_CST@5)
8556 (plus:c@8
8557 (bit_and @6 INTEGER_CST@7)
8558 (bit_and
8559 (rshift
8560 (minus@6 @0
8561 (bit_and (rshift @0 INTEGER_CST@4) INTEGER_CST@11))
8562 INTEGER_CST@10)
8563 INTEGER_CST@9)))
8564 INTEGER_CST@3)
8565 INTEGER_CST@2)
8566 INTEGER_CST@1)
8567 /* Check constants and optab. */
8568 (with { unsigned prec = TYPE_PRECISION (type);
8569 int shift = (64 - prec) & 63;
8570 unsigned HOST_WIDE_INT c1
8571 = HOST_WIDE_INT_UC (0x0101010101010101) >> shift;
8572 unsigned HOST_WIDE_INT c2
8573 = HOST_WIDE_INT_UC (0x0F0F0F0F0F0F0F0F) >> shift;
8574 unsigned HOST_WIDE_INT c3
8575 = HOST_WIDE_INT_UC (0x3333333333333333) >> shift;
8576 unsigned HOST_WIDE_INT c4
8577 = HOST_WIDE_INT_UC (0x5555555555555555) >> shift;
8578 }
8579 (if (prec >= 16
8580 && prec <= 64
8581 && pow2p_hwi (prec)
8582 && TYPE_UNSIGNED (type)
8583 && integer_onep (@4)
8584 && wi::to_widest (@10) == 2
8585 && wi::to_widest (@5) == 4
8586 && wi::to_widest (@1) == prec - 8
8587 && tree_to_uhwi (@2) == c1
8588 && tree_to_uhwi (@3) == c2
8589 && tree_to_uhwi (@9) == c3
8590 && tree_to_uhwi (@7) == c3
8591 && tree_to_uhwi (@11) == c4)
8592 (if (direct_internal_fn_supported_p (IFN_POPCOUNT, type,
8593 OPTIMIZE_FOR_BOTH))
8594 (convert (IFN_POPCOUNT:type @0))
8595 /* Try to do popcount in two halves. PREC must be at least
8596 five bits for this to work without extension before adding. */
8597 (with {
8598 tree half_type = NULL_TREE;
8599 opt_machine_mode m = mode_for_size ((prec + 1) / 2, MODE_INT, 1);
8600 int half_prec = 8;
8601 if (m.exists ()
8602 && m.require () != TYPE_MODE (type))
8603 {
8604 half_prec = GET_MODE_PRECISION (as_a <scalar_int_mode> (m));
8605 half_type = build_nonstandard_integer_type (half_prec, 1);
8606 }
8607 gcc_assert (half_prec > 2);
8608 }
8609 (if (half_type != NULL_TREE
8610 && direct_internal_fn_supported_p (IFN_POPCOUNT, half_type,
8611 OPTIMIZE_FOR_BOTH))
8612 (convert (plus
8613 (IFN_POPCOUNT:half_type (convert @0))
8614 (IFN_POPCOUNT:half_type (convert (rshift @0
8615 { build_int_cst (integer_type_node, half_prec); } )))))))))))
8616
8617 /* __builtin_ffs needs to deal on many targets with the possible zero
8618 argument. If we know the argument is always non-zero, __builtin_ctz + 1
8619 should lead to better code. */
8620 (simplify
8621 (FFS tree_expr_nonzero_p@0)
8622 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
8623 && direct_internal_fn_supported_p (IFN_CTZ, TREE_TYPE (@0),
8624 OPTIMIZE_FOR_SPEED))
8625 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
8626 (plus (CTZ:type (convert:utype @0)) { build_one_cst (type); }))))
8627 #endif
8628
8629 (for ffs (BUILT_IN_FFS BUILT_IN_FFSL BUILT_IN_FFSLL
8630 BUILT_IN_FFSIMAX)
8631 /* __builtin_ffs (X) == 0 -> X == 0.
8632 __builtin_ffs (X) == 6 -> (X & 63) == 32. */
8633 (for cmp (eq ne)
8634 (simplify
8635 (cmp (ffs@2 @0) INTEGER_CST@1)
8636 (with { int prec = TYPE_PRECISION (TREE_TYPE (@0)); }
8637 (switch
8638 (if (integer_zerop (@1))
8639 (cmp @0 { build_zero_cst (TREE_TYPE (@0)); }))
8640 (if (tree_int_cst_sgn (@1) < 0 || wi::to_widest (@1) > prec)
8641 { constant_boolean_node (cmp == NE_EXPR ? true : false, type); })
8642 (if (single_use (@2))
8643 (cmp (bit_and @0 { wide_int_to_tree (TREE_TYPE (@0),
8644 wi::mask (tree_to_uhwi (@1),
8645 false, prec)); })
8646 { wide_int_to_tree (TREE_TYPE (@0),
8647 wi::shifted_mask (tree_to_uhwi (@1) - 1, 1,
8648 false, prec)); }))))))
8649
8650 /* __builtin_ffs (X) > 6 -> X != 0 && (X & 63) == 0. */
8651 (for cmp (gt le)
8652 cmp2 (ne eq)
8653 cmp3 (eq ne)
8654 bit_op (bit_and bit_ior)
8655 (simplify
8656 (cmp (ffs@2 @0) INTEGER_CST@1)
8657 (with { int prec = TYPE_PRECISION (TREE_TYPE (@0)); }
8658 (switch
8659 (if (integer_zerop (@1))
8660 (cmp2 @0 { build_zero_cst (TREE_TYPE (@0)); }))
8661 (if (tree_int_cst_sgn (@1) < 0)
8662 { constant_boolean_node (cmp == GT_EXPR ? true : false, type); })
8663 (if (wi::to_widest (@1) >= prec)
8664 { constant_boolean_node (cmp == GT_EXPR ? false : true, type); })
8665 (if (wi::to_widest (@1) == prec - 1)
8666 (cmp3 @0 { wide_int_to_tree (TREE_TYPE (@0),
8667 wi::shifted_mask (prec - 1, 1,
8668 false, prec)); }))
8669 (if (single_use (@2))
8670 (bit_op (cmp2 @0 { build_zero_cst (TREE_TYPE (@0)); })
8671 (cmp3 (bit_and @0
8672 { wide_int_to_tree (TREE_TYPE (@0),
8673 wi::mask (tree_to_uhwi (@1),
8674 false, prec)); })
8675 { build_zero_cst (TREE_TYPE (@0)); }))))))))
8676
8677 #if GIMPLE
8678
8679 /* Simplify:
8680 a = op a1
8681 r = cond ? a : b
8682 --> r = .COND_FN (cond, a, b)
8683 and,
8684 a = op a1
8685 r = cond ? b : a
8686 --> r = .COND_FN (~cond, b, a). */
8687
8688 (for uncond_op (UNCOND_UNARY)
8689 cond_op (COND_UNARY)
8690 (simplify
8691 (vec_cond @0 (view_convert? (uncond_op@3 @1)) @2)
8692 (with { tree op_type = TREE_TYPE (@3); }
8693 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
8694 && is_truth_type_for (op_type, TREE_TYPE (@0)))
8695 (cond_op @0 @1 @2))))
8696 (simplify
8697 (vec_cond @0 @1 (view_convert? (uncond_op@3 @2)))
8698 (with { tree op_type = TREE_TYPE (@3); }
8699 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
8700 && is_truth_type_for (op_type, TREE_TYPE (@0)))
8701 (cond_op (bit_not @0) @2 @1)))))
8702
8703 /* `(a ? -1 : 0) ^ b` can be converted into a conditional not. */
8704 (simplify
8705 (bit_xor:c (vec_cond @0 uniform_integer_cst_p@1 uniform_integer_cst_p@2) @3)
8706 (if (canonicalize_math_after_vectorization_p ()
8707 && vectorized_internal_fn_supported_p (IFN_COND_NOT, type)
8708 && is_truth_type_for (type, TREE_TYPE (@0)))
8709 (if (integer_all_onesp (@1) && integer_zerop (@2))
8710 (IFN_COND_NOT @0 @3 @3))
8711 (if (integer_all_onesp (@2) && integer_zerop (@1))
8712 (IFN_COND_NOT (bit_not @0) @3 @3))))
8713
8714 /* Simplify:
8715
8716 a = a1 op a2
8717 r = c ? a : b;
8718
8719 to:
8720
8721 r = c ? a1 op a2 : b;
8722
8723 if the target can do it in one go. This makes the operation conditional
8724 on c, so could drop potentially-trapping arithmetic, but that's a valid
8725 simplification if the result of the operation isn't needed.
8726
8727 Avoid speculatively generating a stand-alone vector comparison
8728 on targets that might not support them. Any target implementing
8729 conditional internal functions must support the same comparisons
8730 inside and outside a VEC_COND_EXPR. */
8731
8732 (for uncond_op (UNCOND_BINARY)
8733 cond_op (COND_BINARY)
8734 (simplify
8735 (vec_cond @0 (view_convert? (uncond_op@4 @1 @2)) @3)
8736 (with { tree op_type = TREE_TYPE (@4); }
8737 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
8738 && is_truth_type_for (op_type, TREE_TYPE (@0))
8739 && single_use (@4))
8740 (view_convert (cond_op @0 @1 @2 (view_convert:op_type @3))))))
8741 (simplify
8742 (vec_cond @0 @1 (view_convert? (uncond_op@4 @2 @3)))
8743 (with { tree op_type = TREE_TYPE (@4); }
8744 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
8745 && is_truth_type_for (op_type, TREE_TYPE (@0))
8746 && single_use (@4))
8747 (view_convert (cond_op (bit_not @0) @2 @3 (view_convert:op_type @1)))))))
8748
8749 /* Same for ternary operations. */
8750 (for uncond_op (UNCOND_TERNARY)
8751 cond_op (COND_TERNARY)
8752 (simplify
8753 (vec_cond @0 (view_convert? (uncond_op@5 @1 @2 @3)) @4)
8754 (with { tree op_type = TREE_TYPE (@5); }
8755 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
8756 && is_truth_type_for (op_type, TREE_TYPE (@0))
8757 && single_use (@5))
8758 (view_convert (cond_op @0 @1 @2 @3 (view_convert:op_type @4))))))
8759 (simplify
8760 (vec_cond @0 @1 (view_convert? (uncond_op@5 @2 @3 @4)))
8761 (with { tree op_type = TREE_TYPE (@5); }
8762 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
8763 && is_truth_type_for (op_type, TREE_TYPE (@0))
8764 && single_use (@5))
8765 (view_convert (cond_op (bit_not @0) @2 @3 @4
8766 (view_convert:op_type @1)))))))
8767 #endif
8768
8769 /* Detect cases in which a VEC_COND_EXPR effectively replaces the
8770 "else" value of an IFN_COND_*. */
8771 (for cond_op (COND_BINARY)
8772 (simplify
8773 (vec_cond @0 (view_convert? (cond_op @0 @1 @2 @3)) @4)
8774 (with { tree op_type = TREE_TYPE (@3); }
8775 (if (element_precision (type) == element_precision (op_type))
8776 (view_convert (cond_op @0 @1 @2 (view_convert:op_type @4))))))
8777 (simplify
8778 (vec_cond @0 @1 (view_convert? (cond_op @2 @3 @4 @5)))
8779 (with { tree op_type = TREE_TYPE (@5); }
8780 (if (inverse_conditions_p (@0, @2)
8781 && element_precision (type) == element_precision (op_type))
8782 (view_convert (cond_op @2 @3 @4 (view_convert:op_type @1)))))))
8783
8784 /* Same for ternary operations. */
8785 (for cond_op (COND_TERNARY)
8786 (simplify
8787 (vec_cond @0 (view_convert? (cond_op @0 @1 @2 @3 @4)) @5)
8788 (with { tree op_type = TREE_TYPE (@4); }
8789 (if (element_precision (type) == element_precision (op_type))
8790 (view_convert (cond_op @0 @1 @2 @3 (view_convert:op_type @5))))))
8791 (simplify
8792 (vec_cond @0 @1 (view_convert? (cond_op @2 @3 @4 @5 @6)))
8793 (with { tree op_type = TREE_TYPE (@6); }
8794 (if (inverse_conditions_p (@0, @2)
8795 && element_precision (type) == element_precision (op_type))
8796 (view_convert (cond_op @2 @3 @4 @5 (view_convert:op_type @1)))))))
8797
8798 /* Detect simplication for a conditional reduction where
8799
8800 a = mask1 ? b : 0
8801 c = mask2 ? d + a : d
8802
8803 is turned into
8804
8805 c = mask1 && mask2 ? d + b : d. */
8806 (simplify
8807 (IFN_COND_ADD @0 @1 (vec_cond @2 @3 integer_zerop) @1)
8808 (IFN_COND_ADD (bit_and @0 @2) @1 @3 @1))
8809
8810 /* For pointers @0 and @2 and nonnegative constant offset @1, look for
8811 expressions like:
8812
8813 A: (@0 + @1 < @2) | (@2 + @1 < @0)
8814 B: (@0 + @1 <= @2) | (@2 + @1 <= @0)
8815
8816 If pointers are known not to wrap, B checks whether @1 bytes starting
8817 at @0 and @2 do not overlap, while A tests the same thing for @1 + 1
8818 bytes. A is more efficiently tested as:
8819
8820 A: (sizetype) (@0 + @1 - @2) > @1 * 2
8821
8822 The equivalent expression for B is given by replacing @1 with @1 - 1:
8823
8824 B: (sizetype) (@0 + (@1 - 1) - @2) > (@1 - 1) * 2
8825
8826 @0 and @2 can be swapped in both expressions without changing the result.
8827
8828 The folds rely on sizetype's being unsigned (which is always true)
8829 and on its being the same width as the pointer (which we have to check).
8830
8831 The fold replaces two pointer_plus expressions, two comparisons and
8832 an IOR with a pointer_plus, a pointer_diff, and a comparison, so in
8833 the best case it's a saving of two operations. The A fold retains one
8834 of the original pointer_pluses, so is a win even if both pointer_pluses
8835 are used elsewhere. The B fold is a wash if both pointer_pluses are
8836 used elsewhere, since all we end up doing is replacing a comparison with
8837 a pointer_plus. We do still apply the fold under those circumstances
8838 though, in case applying it to other conditions eventually makes one of the
8839 pointer_pluses dead. */
8840 (for ior (truth_orif truth_or bit_ior)
8841 (for cmp (le lt)
8842 (simplify
8843 (ior (cmp:cs (pointer_plus@3 @0 INTEGER_CST@1) @2)
8844 (cmp:cs (pointer_plus@4 @2 @1) @0))
8845 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
8846 && TYPE_OVERFLOW_WRAPS (sizetype)
8847 && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (sizetype))
8848 /* Calculate the rhs constant. */
8849 (with { offset_int off = wi::to_offset (@1) - (cmp == LE_EXPR ? 1 : 0);
8850 offset_int rhs = off * 2; }
8851 /* Always fails for negative values. */
8852 (if (wi::min_precision (rhs, UNSIGNED) <= TYPE_PRECISION (sizetype))
8853 /* Since the order of @0 and @2 doesn't matter, let tree_swap_operands_p
8854 pick a canonical order. This increases the chances of using the
8855 same pointer_plus in multiple checks. */
8856 (with { bool swap_p = tree_swap_operands_p (@0, @2);
8857 tree rhs_tree = wide_int_to_tree (sizetype, rhs); }
8858 (if (cmp == LT_EXPR)
8859 (gt (convert:sizetype
8860 (pointer_diff:ssizetype { swap_p ? @4 : @3; }
8861 { swap_p ? @0 : @2; }))
8862 { rhs_tree; })
8863 (gt (convert:sizetype
8864 (pointer_diff:ssizetype
8865 (pointer_plus { swap_p ? @2 : @0; }
8866 { wide_int_to_tree (sizetype, off); })
8867 { swap_p ? @0 : @2; }))
8868 { rhs_tree; })))))))))
8869
8870 /* Fold REDUC (@0 & @1) -> @0[I] & @1[I] if element I is the only nonzero
8871 element of @1. */
8872 (for reduc (IFN_REDUC_PLUS IFN_REDUC_IOR IFN_REDUC_XOR)
8873 (simplify (reduc (view_convert? (bit_and @0 VECTOR_CST@1)))
8874 (with { int i = single_nonzero_element (@1); }
8875 (if (i >= 0)
8876 (with { tree elt = vector_cst_elt (@1, i);
8877 tree elt_type = TREE_TYPE (elt);
8878 unsigned int elt_bits = tree_to_uhwi (TYPE_SIZE (elt_type));
8879 tree size = bitsize_int (elt_bits);
8880 tree pos = bitsize_int (elt_bits * i); }
8881 (view_convert
8882 (bit_and:elt_type
8883 (BIT_FIELD_REF:elt_type @0 { size; } { pos; })
8884 { elt; })))))))
8885
8886 /* Fold reduction of a single nonzero element constructor. */
8887 (for reduc (IFN_REDUC_PLUS IFN_REDUC_IOR IFN_REDUC_XOR)
8888 (simplify (reduc (CONSTRUCTOR@0))
8889 (with { tree ctor = (TREE_CODE (@0) == SSA_NAME
8890 ? gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0)) : @0);
8891 tree elt = ctor_single_nonzero_element (ctor); }
8892 (if (elt
8893 && !HONOR_SNANS (type)
8894 && !HONOR_SIGNED_ZEROS (type))
8895 { elt; }))))
8896
8897 /* Fold REDUC (@0 op VECTOR_CST) as REDUC (@0) op REDUC (VECTOR_CST). */
8898 (for reduc (IFN_REDUC_PLUS IFN_REDUC_MAX IFN_REDUC_MIN IFN_REDUC_FMAX
8899 IFN_REDUC_FMIN IFN_REDUC_AND IFN_REDUC_IOR IFN_REDUC_XOR)
8900 op (plus max min IFN_FMAX IFN_FMIN bit_and bit_ior bit_xor)
8901 (simplify (reduc (op @0 VECTOR_CST@1))
8902 (op (reduc:type @0) (reduc:type @1))))
8903
8904 /* Simplify vector floating point operations of alternating sub/add pairs
8905 into using an fneg of a wider element type followed by a normal add.
8906 under IEEE 754 the fneg of the wider type will negate every even entry
8907 and when doing an add we get a sub of the even and add of every odd
8908 elements. */
8909 (for plusminus (plus minus)
8910 minusplus (minus plus)
8911 (simplify
8912 (vec_perm (plusminus @0 @1) (minusplus @2 @3) VECTOR_CST@4)
8913 (if (!VECTOR_INTEGER_TYPE_P (type)
8914 && !FLOAT_WORDS_BIG_ENDIAN
8915 /* plus is commutative, while minus is not, so :c can't be used.
8916 Do equality comparisons by hand and at the end pick the operands
8917 from the minus. */
8918 && (operand_equal_p (@0, @2, 0)
8919 ? operand_equal_p (@1, @3, 0)
8920 : operand_equal_p (@0, @3, 0) && operand_equal_p (@1, @2, 0)))
8921 (with
8922 {
8923 /* Build a vector of integers from the tree mask. */
8924 vec_perm_builder builder;
8925 }
8926 (if (tree_to_vec_perm_builder (&builder, @4))
8927 (with
8928 {
8929 /* Create a vec_perm_indices for the integer vector. */
8930 poly_uint64 nelts = TYPE_VECTOR_SUBPARTS (type);
8931 vec_perm_indices sel (builder, 2, nelts);
8932 machine_mode vec_mode = TYPE_MODE (type);
8933 machine_mode wide_mode;
8934 scalar_mode wide_elt_mode;
8935 poly_uint64 wide_nunits;
8936 scalar_mode inner_mode = GET_MODE_INNER (vec_mode);
8937 }
8938 (if (VECTOR_MODE_P (vec_mode)
8939 && sel.series_p (0, 2, 0, 2)
8940 && sel.series_p (1, 2, nelts + 1, 2)
8941 && GET_MODE_2XWIDER_MODE (inner_mode).exists (&wide_elt_mode)
8942 && multiple_p (GET_MODE_NUNITS (vec_mode), 2, &wide_nunits)
8943 && related_vector_mode (vec_mode, wide_elt_mode,
8944 wide_nunits).exists (&wide_mode))
8945 (with
8946 {
8947 tree stype
8948 = lang_hooks.types.type_for_mode (GET_MODE_INNER (wide_mode),
8949 TYPE_UNSIGNED (type));
8950 tree ntype = build_vector_type_for_mode (stype, wide_mode);
8951
8952 /* The format has to be a non-extended ieee format. */
8953 const struct real_format *fmt_old = FLOAT_MODE_FORMAT (vec_mode);
8954 const struct real_format *fmt_new = FLOAT_MODE_FORMAT (wide_mode);
8955 }
8956 (if (TYPE_MODE (stype) != BLKmode
8957 && VECTOR_TYPE_P (ntype)
8958 && fmt_old != NULL
8959 && fmt_new != NULL)
8960 (with
8961 {
8962 /* If the target doesn't support v1xx vectors, try using
8963 scalar mode xx instead. */
8964 if (known_eq (GET_MODE_NUNITS (wide_mode), 1)
8965 && !target_supports_op_p (ntype, NEGATE_EXPR, optab_vector))
8966 ntype = stype;
8967 }
8968 (if (fmt_new->signbit_rw
8969 == fmt_old->signbit_rw + GET_MODE_UNIT_BITSIZE (vec_mode)
8970 && fmt_new->signbit_rw == fmt_new->signbit_ro
8971 && targetm.can_change_mode_class (TYPE_MODE (ntype),
8972 TYPE_MODE (type), ALL_REGS)
8973 && ((optimize_vectors_before_lowering_p ()
8974 && VECTOR_TYPE_P (ntype))
8975 || target_supports_op_p (ntype, NEGATE_EXPR, optab_vector)))
8976 (if (plusminus == PLUS_EXPR)
8977 (plus (view_convert:type (negate (view_convert:ntype @3))) @2)
8978 (minus @0 (view_convert:type
8979 (negate (view_convert:ntype @1))))))))))))))))
8980
8981 (simplify
8982 (vec_perm @0 @1 VECTOR_CST@2)
8983 (with
8984 {
8985 tree op0 = @0, op1 = @1, op2 = @2;
8986 machine_mode result_mode = TYPE_MODE (type);
8987 machine_mode op_mode = TYPE_MODE (TREE_TYPE (op0));
8988
8989 /* Build a vector of integers from the tree mask. */
8990 vec_perm_builder builder;
8991 }
8992 (if (tree_to_vec_perm_builder (&builder, op2))
8993 (with
8994 {
8995 /* Create a vec_perm_indices for the integer vector. */
8996 poly_uint64 nelts = TYPE_VECTOR_SUBPARTS (type);
8997 bool single_arg = (op0 == op1);
8998 vec_perm_indices sel (builder, single_arg ? 1 : 2, nelts);
8999 }
9000 (if (sel.series_p (0, 1, 0, 1))
9001 { op0; }
9002 (if (sel.series_p (0, 1, nelts, 1))
9003 { op1; }
9004 (with
9005 {
9006 if (!single_arg)
9007 {
9008 if (sel.all_from_input_p (0))
9009 op1 = op0;
9010 else if (sel.all_from_input_p (1))
9011 {
9012 op0 = op1;
9013 sel.rotate_inputs (1);
9014 }
9015 else if (known_ge (poly_uint64 (sel[0]), nelts))
9016 {
9017 std::swap (op0, op1);
9018 sel.rotate_inputs (1);
9019 }
9020 }
9021 gassign *def;
9022 tree cop0 = op0, cop1 = op1;
9023 if (TREE_CODE (op0) == SSA_NAME
9024 && (def = dyn_cast <gassign *> (SSA_NAME_DEF_STMT (op0)))
9025 && gimple_assign_rhs_code (def) == CONSTRUCTOR)
9026 cop0 = gimple_assign_rhs1 (def);
9027 if (TREE_CODE (op1) == SSA_NAME
9028 && (def = dyn_cast <gassign *> (SSA_NAME_DEF_STMT (op1)))
9029 && gimple_assign_rhs_code (def) == CONSTRUCTOR)
9030 cop1 = gimple_assign_rhs1 (def);
9031 tree t;
9032 }
9033 (if ((TREE_CODE (cop0) == VECTOR_CST
9034 || TREE_CODE (cop0) == CONSTRUCTOR)
9035 && (TREE_CODE (cop1) == VECTOR_CST
9036 || TREE_CODE (cop1) == CONSTRUCTOR)
9037 && (t = fold_vec_perm (type, cop0, cop1, sel)))
9038 { t; }
9039 (with
9040 {
9041 bool changed = (op0 == op1 && !single_arg);
9042 tree ins = NULL_TREE;
9043 unsigned at = 0;
9044
9045 /* See if the permutation is performing a single element
9046 insert from a CONSTRUCTOR or constant and use a BIT_INSERT_EXPR
9047 in that case. But only if the vector mode is supported,
9048 otherwise this is invalid GIMPLE. */
9049 if (op_mode != BLKmode
9050 && (TREE_CODE (cop0) == VECTOR_CST
9051 || TREE_CODE (cop0) == CONSTRUCTOR
9052 || TREE_CODE (cop1) == VECTOR_CST
9053 || TREE_CODE (cop1) == CONSTRUCTOR))
9054 {
9055 bool insert_first_p = sel.series_p (1, 1, nelts + 1, 1);
9056 if (insert_first_p)
9057 {
9058 /* After canonicalizing the first elt to come from the
9059 first vector we only can insert the first elt from
9060 the first vector. */
9061 at = 0;
9062 if ((ins = fold_read_from_vector (cop0, sel[0])))
9063 op0 = op1;
9064 }
9065 /* The above can fail for two-element vectors which always
9066 appear to insert the first element, so try inserting
9067 into the second lane as well. For more than two
9068 elements that's wasted time. */
9069 if (!insert_first_p || (!ins && maybe_eq (nelts, 2u)))
9070 {
9071 unsigned int encoded_nelts = sel.encoding ().encoded_nelts ();
9072 for (at = 0; at < encoded_nelts; ++at)
9073 if (maybe_ne (sel[at], at))
9074 break;
9075 if (at < encoded_nelts
9076 && (known_eq (at + 1, nelts)
9077 || sel.series_p (at + 1, 1, at + 1, 1)))
9078 {
9079 if (known_lt (poly_uint64 (sel[at]), nelts))
9080 ins = fold_read_from_vector (cop0, sel[at]);
9081 else
9082 ins = fold_read_from_vector (cop1, sel[at] - nelts);
9083 }
9084 }
9085 }
9086
9087 /* Generate a canonical form of the selector. */
9088 if (!ins && sel.encoding () != builder)
9089 {
9090 /* Some targets are deficient and fail to expand a single
9091 argument permutation while still allowing an equivalent
9092 2-argument version. */
9093 tree oldop2 = op2;
9094 if (sel.ninputs () == 2
9095 || can_vec_perm_const_p (result_mode, op_mode, sel, false))
9096 op2 = vec_perm_indices_to_tree (TREE_TYPE (op2), sel);
9097 else
9098 {
9099 vec_perm_indices sel2 (builder, 2, nelts);
9100 if (can_vec_perm_const_p (result_mode, op_mode, sel2, false))
9101 op2 = vec_perm_indices_to_tree (TREE_TYPE (op2), sel2);
9102 else
9103 /* Not directly supported with either encoding,
9104 so use the preferred form. */
9105 op2 = vec_perm_indices_to_tree (TREE_TYPE (op2), sel);
9106 }
9107 if (!operand_equal_p (op2, oldop2, 0))
9108 changed = true;
9109 }
9110 }
9111 (if (ins)
9112 (bit_insert { op0; } { ins; }
9113 { bitsize_int (at * vector_element_bits (type)); })
9114 (if (changed)
9115 (vec_perm { op0; } { op1; } { op2; }))))))))))))
9116
9117 /* VEC_PERM_EXPR (v, v, mask) -> v where v contains same element. */
9118
9119 (match vec_same_elem_p
9120 (vec_duplicate @0))
9121
9122 (match vec_same_elem_p
9123 CONSTRUCTOR@0
9124 (if (TREE_CODE (@0) == SSA_NAME
9125 && uniform_vector_p (gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0))))))
9126
9127 (match vec_same_elem_p
9128 @0
9129 (if (uniform_vector_p (@0))))
9130
9131
9132 (simplify
9133 (vec_perm vec_same_elem_p@0 @0 @1)
9134 (if (types_match (type, TREE_TYPE (@0)))
9135 @0
9136 (with
9137 {
9138 tree elem = uniform_vector_p (@0);
9139 }
9140 (if (elem)
9141 { build_vector_from_val (type, elem); }))))
9142
9143 /* Push VEC_PERM earlier if that may help FMA perception (PR101895). */
9144 (simplify
9145 (plus:c (vec_perm:s (mult:c@0 @1 vec_same_elem_p@2) @0 @3) @4)
9146 (if (TREE_CODE (@0) == SSA_NAME && num_imm_uses (@0) == 2)
9147 (plus (mult (vec_perm @1 @1 @3) @2) @4)))
9148 (simplify
9149 (minus (vec_perm:s (mult:c@0 @1 vec_same_elem_p@2) @0 @3) @4)
9150 (if (TREE_CODE (@0) == SSA_NAME && num_imm_uses (@0) == 2)
9151 (minus (mult (vec_perm @1 @1 @3) @2) @4)))
9152
9153
9154 /* Merge
9155 c = VEC_PERM_EXPR <a, b, VCST0>;
9156 d = VEC_PERM_EXPR <c, c, VCST1>;
9157 to
9158 d = VEC_PERM_EXPR <a, b, NEW_VCST>; */
9159
9160 (simplify
9161 (vec_perm (vec_perm@0 @1 @2 VECTOR_CST@3) @0 VECTOR_CST@4)
9162 (if (TYPE_VECTOR_SUBPARTS (type).is_constant ())
9163 (with
9164 {
9165 machine_mode result_mode = TYPE_MODE (type);
9166 machine_mode op_mode = TYPE_MODE (TREE_TYPE (@1));
9167 int nelts = TYPE_VECTOR_SUBPARTS (type).to_constant ();
9168 vec_perm_builder builder0;
9169 vec_perm_builder builder1;
9170 vec_perm_builder builder2 (nelts, nelts, 1);
9171 }
9172 (if (tree_to_vec_perm_builder (&builder0, @3)
9173 && tree_to_vec_perm_builder (&builder1, @4))
9174 (with
9175 {
9176 vec_perm_indices sel0 (builder0, 2, nelts);
9177 vec_perm_indices sel1 (builder1, 1, nelts);
9178
9179 for (int i = 0; i < nelts; i++)
9180 builder2.quick_push (sel0[sel1[i].to_constant ()]);
9181
9182 vec_perm_indices sel2 (builder2, 2, nelts);
9183
9184 tree op0 = NULL_TREE;
9185 /* If the new VEC_PERM_EXPR can't be handled but both
9186 original VEC_PERM_EXPRs can, punt.
9187 If one or both of the original VEC_PERM_EXPRs can't be
9188 handled and the new one can't be either, don't increase
9189 number of VEC_PERM_EXPRs that can't be handled. */
9190 if (can_vec_perm_const_p (result_mode, op_mode, sel2, false)
9191 || (single_use (@0)
9192 ? (!can_vec_perm_const_p (result_mode, op_mode, sel0, false)
9193 || !can_vec_perm_const_p (result_mode, op_mode, sel1, false))
9194 : !can_vec_perm_const_p (result_mode, op_mode, sel1, false)))
9195 op0 = vec_perm_indices_to_tree (TREE_TYPE (@4), sel2);
9196 }
9197 (if (op0)
9198 (vec_perm @1 @2 { op0; })))))))
9199
9200 /* Merge
9201 c = VEC_PERM_EXPR <a, b, VCST0>;
9202 d = VEC_PERM_EXPR <x, c, VCST1>;
9203 to
9204 d = VEC_PERM_EXPR <x, {a,b}, NEW_VCST>;
9205 when all elements from a or b are replaced by the later
9206 permutation. */
9207
9208 (simplify
9209 (vec_perm @5 (vec_perm@0 @1 @2 VECTOR_CST@3) VECTOR_CST@4)
9210 (if (TYPE_VECTOR_SUBPARTS (type).is_constant ())
9211 (with
9212 {
9213 machine_mode result_mode = TYPE_MODE (type);
9214 machine_mode op_mode = TYPE_MODE (TREE_TYPE (@1));
9215 int nelts = TYPE_VECTOR_SUBPARTS (type).to_constant ();
9216 vec_perm_builder builder0;
9217 vec_perm_builder builder1;
9218 vec_perm_builder builder2 (nelts, nelts, 2);
9219 }
9220 (if (tree_to_vec_perm_builder (&builder0, @3)
9221 && tree_to_vec_perm_builder (&builder1, @4))
9222 (with
9223 {
9224 vec_perm_indices sel0 (builder0, 2, nelts);
9225 vec_perm_indices sel1 (builder1, 2, nelts);
9226 bool use_1 = false, use_2 = false;
9227
9228 for (int i = 0; i < nelts; i++)
9229 {
9230 if (known_lt ((poly_uint64)sel1[i], sel1.nelts_per_input ()))
9231 builder2.quick_push (sel1[i]);
9232 else
9233 {
9234 poly_uint64 j = sel0[(sel1[i] - sel1.nelts_per_input ())
9235 .to_constant ()];
9236 if (known_lt (j, sel0.nelts_per_input ()))
9237 use_1 = true;
9238 else
9239 {
9240 use_2 = true;
9241 j -= sel0.nelts_per_input ();
9242 }
9243 builder2.quick_push (j + sel1.nelts_per_input ());
9244 }
9245 }
9246 }
9247 (if (use_1 ^ use_2)
9248 (with
9249 {
9250 vec_perm_indices sel2 (builder2, 2, nelts);
9251 tree op0 = NULL_TREE;
9252 /* If the new VEC_PERM_EXPR can't be handled but both
9253 original VEC_PERM_EXPRs can, punt.
9254 If one or both of the original VEC_PERM_EXPRs can't be
9255 handled and the new one can't be either, don't increase
9256 number of VEC_PERM_EXPRs that can't be handled. */
9257 if (can_vec_perm_const_p (result_mode, op_mode, sel2, false)
9258 || (single_use (@0)
9259 ? (!can_vec_perm_const_p (result_mode, op_mode, sel0, false)
9260 || !can_vec_perm_const_p (result_mode, op_mode, sel1, false))
9261 : !can_vec_perm_const_p (result_mode, op_mode, sel1, false)))
9262 op0 = vec_perm_indices_to_tree (TREE_TYPE (@4), sel2);
9263 }
9264 (if (op0)
9265 (switch
9266 (if (use_1)
9267 (vec_perm @5 @1 { op0; }))
9268 (if (use_2)
9269 (vec_perm @5 @2 { op0; })))))))))))
9270
9271 /* And the case with swapped outer permute sources. */
9272
9273 (simplify
9274 (vec_perm (vec_perm@0 @1 @2 VECTOR_CST@3) @5 VECTOR_CST@4)
9275 (if (TYPE_VECTOR_SUBPARTS (type).is_constant ())
9276 (with
9277 {
9278 machine_mode result_mode = TYPE_MODE (type);
9279 machine_mode op_mode = TYPE_MODE (TREE_TYPE (@1));
9280 int nelts = TYPE_VECTOR_SUBPARTS (type).to_constant ();
9281 vec_perm_builder builder0;
9282 vec_perm_builder builder1;
9283 vec_perm_builder builder2 (nelts, nelts, 2);
9284 }
9285 (if (tree_to_vec_perm_builder (&builder0, @3)
9286 && tree_to_vec_perm_builder (&builder1, @4))
9287 (with
9288 {
9289 vec_perm_indices sel0 (builder0, 2, nelts);
9290 vec_perm_indices sel1 (builder1, 2, nelts);
9291 bool use_1 = false, use_2 = false;
9292
9293 for (int i = 0; i < nelts; i++)
9294 {
9295 if (known_ge ((poly_uint64)sel1[i], sel1.nelts_per_input ()))
9296 builder2.quick_push (sel1[i]);
9297 else
9298 {
9299 poly_uint64 j = sel0[sel1[i].to_constant ()];
9300 if (known_lt (j, sel0.nelts_per_input ()))
9301 use_1 = true;
9302 else
9303 {
9304 use_2 = true;
9305 j -= sel0.nelts_per_input ();
9306 }
9307 builder2.quick_push (j);
9308 }
9309 }
9310 }
9311 (if (use_1 ^ use_2)
9312 (with
9313 {
9314 vec_perm_indices sel2 (builder2, 2, nelts);
9315 tree op0 = NULL_TREE;
9316 /* If the new VEC_PERM_EXPR can't be handled but both
9317 original VEC_PERM_EXPRs can, punt.
9318 If one or both of the original VEC_PERM_EXPRs can't be
9319 handled and the new one can't be either, don't increase
9320 number of VEC_PERM_EXPRs that can't be handled. */
9321 if (can_vec_perm_const_p (result_mode, op_mode, sel2, false)
9322 || (single_use (@0)
9323 ? (!can_vec_perm_const_p (result_mode, op_mode, sel0, false)
9324 || !can_vec_perm_const_p (result_mode, op_mode, sel1, false))
9325 : !can_vec_perm_const_p (result_mode, op_mode, sel1, false)))
9326 op0 = vec_perm_indices_to_tree (TREE_TYPE (@4), sel2);
9327 }
9328 (if (op0)
9329 (switch
9330 (if (use_1)
9331 (vec_perm @1 @5 { op0; }))
9332 (if (use_2)
9333 (vec_perm @2 @5 { op0; })))))))))))
9334
9335
9336 /* Match count trailing zeroes for simplify_count_trailing_zeroes in fwprop.
9337 The canonical form is array[((x & -x) * C) >> SHIFT] where C is a magic
9338 constant which when multiplied by a power of 2 contains a unique value
9339 in the top 5 or 6 bits. This is then indexed into a table which maps it
9340 to the number of trailing zeroes. */
9341 (match (ctz_table_index @1 @2 @3)
9342 (rshift (mult (bit_and:c (negate @1) @1) INTEGER_CST@2) INTEGER_CST@3))
9343
9344 (match (cond_expr_convert_p @0 @2 @3 @6)
9345 (cond (simple_comparison@6 @0 @1) (convert@4 @2) (convert@5 @3))
9346 (if (INTEGRAL_TYPE_P (type)
9347 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
9348 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
9349 && INTEGRAL_TYPE_P (TREE_TYPE (@3))
9350 && TYPE_PRECISION (type) != TYPE_PRECISION (TREE_TYPE (@0))
9351 && TYPE_PRECISION (TREE_TYPE (@0))
9352 == TYPE_PRECISION (TREE_TYPE (@2))
9353 && TYPE_PRECISION (TREE_TYPE (@0))
9354 == TYPE_PRECISION (TREE_TYPE (@3))
9355 /* For vect_recog_cond_expr_convert_pattern, @2 and @3 can differ in
9356 signess when convert is truncation, but not ok for extension since
9357 it's sign_extend vs zero_extend. */
9358 && (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type)
9359 || (TYPE_UNSIGNED (TREE_TYPE (@2))
9360 == TYPE_UNSIGNED (TREE_TYPE (@3))))
9361 && single_use (@4)
9362 && single_use (@5))))
9363
9364 (for bit_op (bit_and bit_ior bit_xor)
9365 (match (bitwise_induction_p @0 @2 @3)
9366 (bit_op:c
9367 (nop_convert1? (bit_not2?@0 (convert3? (lshift integer_onep@1 @2))))
9368 @3)))
9369
9370 (match (bitwise_induction_p @0 @2 @3)
9371 (bit_not
9372 (nop_convert1? (bit_xor@0 (convert2? (lshift integer_onep@1 @2)) @3))))
9373
9374 /* n - (((n > C1) ? n : C1) & -C2) -> n & C1 for unsigned case.
9375 n - (((n > C1) ? n : C1) & -C2) -> (n <= C1) ? n : (n & C1) for signed case. */
9376 (simplify
9377 (minus @0 (bit_and (max @0 INTEGER_CST@1) INTEGER_CST@2))
9378 (with { auto i = wi::neg (wi::to_wide (@2)); }
9379 /* Check if -C2 is a power of 2 and C1 = -C2 - 1. */
9380 (if (wi::popcount (i) == 1
9381 && (wi::to_wide (@1)) == (i - 1))
9382 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
9383 (bit_and @0 @1)
9384 (cond (le @0 @1) @0 (bit_and @0 @1))))))
9385
9386 /* -x & 1 -> x & 1. */
9387 (simplify
9388 (bit_and (negate @0) integer_onep@1)
9389 (if (!TYPE_OVERFLOW_SANITIZED (type))
9390 (bit_and @0 @1)))
9391
9392 /* `-a` is just `a` if the type is 1bit wide or when converting
9393 to a 1bit type; similar to the above transformation of `(-x)&1`.
9394 This is used mostly with the transformation of
9395 `a ? ~b : b` into `(-a)^b`.
9396 It also can show up with bitfields. */
9397 (simplify
9398 (convert? (negate @0))
9399 (if (INTEGRAL_TYPE_P (type)
9400 && TYPE_PRECISION (type) == 1
9401 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0)))
9402 (convert @0)))
9403
9404 /* Optimize
9405 c1 = VEC_PERM_EXPR (a, a, mask)
9406 c2 = VEC_PERM_EXPR (b, b, mask)
9407 c3 = c1 op c2
9408 -->
9409 c = a op b
9410 c3 = VEC_PERM_EXPR (c, c, mask)
9411 For all integer non-div operations. */
9412 (for op (plus minus mult bit_and bit_ior bit_xor
9413 lshift rshift)
9414 (simplify
9415 (op (vec_perm @0 @0 @2) (vec_perm @1 @1 @2))
9416 (if (VECTOR_INTEGER_TYPE_P (type))
9417 (vec_perm (op@3 @0 @1) @3 @2))))
9418
9419 /* Similar for float arithmetic when permutation constant covers
9420 all vector elements. */
9421 (for op (plus minus mult)
9422 (simplify
9423 (op (vec_perm @0 @0 VECTOR_CST@2) (vec_perm @1 @1 VECTOR_CST@2))
9424 (if (VECTOR_FLOAT_TYPE_P (type)
9425 && TYPE_VECTOR_SUBPARTS (type).is_constant ())
9426 (with
9427 {
9428 tree perm_cst = @2;
9429 vec_perm_builder builder;
9430 bool full_perm_p = false;
9431 if (tree_to_vec_perm_builder (&builder, perm_cst))
9432 {
9433 unsigned HOST_WIDE_INT nelts;
9434
9435 nelts = TYPE_VECTOR_SUBPARTS (type).to_constant ();
9436 /* Create a vec_perm_indices for the VECTOR_CST. */
9437 vec_perm_indices sel (builder, 1, nelts);
9438
9439 /* Check if perm indices covers all vector elements. */
9440 if (sel.encoding ().encoded_full_vector_p ())
9441 {
9442 auto_sbitmap seen (nelts);
9443 bitmap_clear (seen);
9444
9445 unsigned HOST_WIDE_INT count = 0, i;
9446
9447 for (i = 0; i < nelts; i++)
9448 {
9449 if (!bitmap_set_bit (seen, sel[i].to_constant ()))
9450 break;
9451 count++;
9452 }
9453 full_perm_p = count == nelts;
9454 }
9455 }
9456 }
9457 (if (full_perm_p)
9458 (vec_perm (op@3 @0 @1) @3 @2))))))