]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/match.pd
Correct a function pre/postcondition [PR102403].
[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.c
3 and generic-match.c from it.
4
5 Copyright (C) 2014-2021 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
42 /* Operator lists. */
43 (define_operator_list tcc_comparison
44 lt le eq ne ge gt unordered ordered unlt unle ungt unge uneq ltgt)
45 (define_operator_list inverted_tcc_comparison
46 ge gt ne eq lt le ordered unordered ge gt le lt ltgt uneq)
47 (define_operator_list inverted_tcc_comparison_with_nans
48 unge ungt ne eq unlt unle ordered unordered ge gt le lt ltgt uneq)
49 (define_operator_list swapped_tcc_comparison
50 gt ge eq ne le lt unordered ordered ungt unge unlt unle uneq ltgt)
51 (define_operator_list simple_comparison lt le eq ne ge gt)
52 (define_operator_list swapped_simple_comparison gt ge eq ne le lt)
53
54 #include "cfn-operators.pd"
55
56 /* Define operand lists for math rounding functions {,i,l,ll}FN,
57 where the versions prefixed with "i" return an int, those prefixed with
58 "l" return a long and those prefixed with "ll" return a long long.
59
60 Also define operand lists:
61
62 X<FN>F for all float functions, in the order i, l, ll
63 X<FN> for all double functions, in the same order
64 X<FN>L for all long double functions, in the same order. */
65 #define DEFINE_INT_AND_FLOAT_ROUND_FN(FN) \
66 (define_operator_list X##FN##F BUILT_IN_I##FN##F \
67 BUILT_IN_L##FN##F \
68 BUILT_IN_LL##FN##F) \
69 (define_operator_list X##FN BUILT_IN_I##FN \
70 BUILT_IN_L##FN \
71 BUILT_IN_LL##FN) \
72 (define_operator_list X##FN##L BUILT_IN_I##FN##L \
73 BUILT_IN_L##FN##L \
74 BUILT_IN_LL##FN##L)
75
76 DEFINE_INT_AND_FLOAT_ROUND_FN (FLOOR)
77 DEFINE_INT_AND_FLOAT_ROUND_FN (CEIL)
78 DEFINE_INT_AND_FLOAT_ROUND_FN (ROUND)
79 DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
80
81 /* Binary operations and their associated IFN_COND_* function. */
82 (define_operator_list UNCOND_BINARY
83 plus minus
84 mult trunc_div trunc_mod rdiv
85 min max
86 bit_and bit_ior bit_xor
87 lshift rshift)
88 (define_operator_list COND_BINARY
89 IFN_COND_ADD IFN_COND_SUB
90 IFN_COND_MUL IFN_COND_DIV IFN_COND_MOD IFN_COND_RDIV
91 IFN_COND_MIN IFN_COND_MAX
92 IFN_COND_AND IFN_COND_IOR IFN_COND_XOR
93 IFN_COND_SHL IFN_COND_SHR)
94
95 /* Same for ternary operations. */
96 (define_operator_list UNCOND_TERNARY
97 IFN_FMA IFN_FMS IFN_FNMA IFN_FNMS)
98 (define_operator_list COND_TERNARY
99 IFN_COND_FMA IFN_COND_FMS IFN_COND_FNMA IFN_COND_FNMS)
100
101 /* With nop_convert? combine convert? and view_convert? in one pattern
102 plus conditionalize on tree_nop_conversion_p conversions. */
103 (match (nop_convert @0)
104 (convert @0)
105 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))))
106 (match (nop_convert @0)
107 (view_convert @0)
108 (if (VECTOR_TYPE_P (type) && VECTOR_TYPE_P (TREE_TYPE (@0))
109 && known_eq (TYPE_VECTOR_SUBPARTS (type),
110 TYPE_VECTOR_SUBPARTS (TREE_TYPE (@0)))
111 && tree_nop_conversion_p (TREE_TYPE (type), TREE_TYPE (TREE_TYPE (@0))))))
112
113 /* Transform likes of (char) ABS_EXPR <(int) x> into (char) ABSU_EXPR <x>
114 ABSU_EXPR returns unsigned absolute value of the operand and the operand
115 of the ABSU_EXPR will have the corresponding signed type. */
116 (simplify (abs (convert @0))
117 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
118 && !TYPE_UNSIGNED (TREE_TYPE (@0))
119 && element_precision (type) > element_precision (TREE_TYPE (@0)))
120 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
121 (convert (absu:utype @0)))))
122
123 #if GIMPLE
124 /* Optimize (X + (X >> (prec - 1))) ^ (X >> (prec - 1)) into abs (X). */
125 (simplify
126 (bit_xor:c (plus:c @0 (rshift@2 @0 INTEGER_CST@1)) @2)
127 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
128 && !TYPE_UNSIGNED (TREE_TYPE (@0))
129 && wi::to_widest (@1) == element_precision (TREE_TYPE (@0)) - 1)
130 (abs @0)))
131 #endif
132
133 /* Simplifications of operations with one constant operand and
134 simplifications to constants or single values. */
135
136 (for op (plus pointer_plus minus bit_ior bit_xor)
137 (simplify
138 (op @0 integer_zerop)
139 (non_lvalue @0)))
140
141 /* 0 +p index -> (type)index */
142 (simplify
143 (pointer_plus integer_zerop @1)
144 (non_lvalue (convert @1)))
145
146 /* ptr - 0 -> (type)ptr */
147 (simplify
148 (pointer_diff @0 integer_zerop)
149 (convert @0))
150
151 /* See if ARG1 is zero and X + ARG1 reduces to X.
152 Likewise if the operands are reversed. */
153 (simplify
154 (plus:c @0 real_zerop@1)
155 (if (fold_real_zero_addition_p (type, @0, @1, 0))
156 (non_lvalue @0)))
157
158 /* See if ARG1 is zero and X - ARG1 reduces to X. */
159 (simplify
160 (minus @0 real_zerop@1)
161 (if (fold_real_zero_addition_p (type, @0, @1, 1))
162 (non_lvalue @0)))
163
164 /* Even if the fold_real_zero_addition_p can't simplify X + 0.0
165 into X, we can optimize (X + 0.0) + 0.0 or (X + 0.0) - 0.0
166 or (X - 0.0) + 0.0 into X + 0.0 and (X - 0.0) - 0.0 into X - 0.0
167 if not -frounding-math. For sNaNs the first operation would raise
168 exceptions but turn the result into qNan, so the second operation
169 would not raise it. */
170 (for inner_op (plus minus)
171 (for outer_op (plus minus)
172 (simplify
173 (outer_op (inner_op@3 @0 REAL_CST@1) REAL_CST@2)
174 (if (real_zerop (@1)
175 && real_zerop (@2)
176 && !HONOR_SIGN_DEPENDENT_ROUNDING (type))
177 (with { bool inner_plus = ((inner_op == PLUS_EXPR)
178 ^ REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@1)));
179 bool outer_plus
180 = ((outer_op == PLUS_EXPR)
181 ^ REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@2))); }
182 (if (outer_plus && !inner_plus)
183 (outer_op @0 @2)
184 @3))))))
185
186 /* Simplify x - x.
187 This is unsafe for certain floats even in non-IEEE formats.
188 In IEEE, it is unsafe because it does wrong for NaNs.
189 Also note that operand_equal_p is always false if an operand
190 is volatile. */
191 (simplify
192 (minus @0 @0)
193 (if (!FLOAT_TYPE_P (type) || !tree_expr_maybe_nan_p (@0))
194 { build_zero_cst (type); }))
195 (simplify
196 (pointer_diff @@0 @0)
197 { build_zero_cst (type); })
198
199 (simplify
200 (mult @0 integer_zerop@1)
201 @1)
202
203 /* Maybe fold x * 0 to 0. The expressions aren't the same
204 when x is NaN, since x * 0 is also NaN. Nor are they the
205 same in modes with signed zeros, since multiplying a
206 negative value by 0 gives -0, not +0. */
207 (simplify
208 (mult @0 real_zerop@1)
209 (if (!tree_expr_maybe_nan_p (@0)
210 && !tree_expr_maybe_real_minus_zero_p (@0)
211 && !tree_expr_maybe_real_minus_zero_p (@1))
212 @1))
213
214 /* In IEEE floating point, x*1 is not equivalent to x for snans.
215 Likewise for complex arithmetic with signed zeros. */
216 (simplify
217 (mult @0 real_onep)
218 (if (!tree_expr_maybe_signaling_nan_p (@0)
219 && (!HONOR_SIGNED_ZEROS (type)
220 || !COMPLEX_FLOAT_TYPE_P (type)))
221 (non_lvalue @0)))
222
223 /* Transform x * -1.0 into -x. */
224 (simplify
225 (mult @0 real_minus_onep)
226 (if (!tree_expr_maybe_signaling_nan_p (@0)
227 && (!HONOR_SIGNED_ZEROS (type)
228 || !COMPLEX_FLOAT_TYPE_P (type)))
229 (negate @0)))
230
231 /* Transform { 0 or 1 } * { 0 or 1 } into { 0 or 1 } & { 0 or 1 } */
232 (simplify
233 (mult SSA_NAME@1 SSA_NAME@2)
234 (if (INTEGRAL_TYPE_P (type)
235 && get_nonzero_bits (@1) == 1
236 && get_nonzero_bits (@2) == 1)
237 (bit_and @1 @2)))
238
239 /* Transform x * { 0 or 1, 0 or 1, ... } into x & { 0 or -1, 0 or -1, ...},
240 unless the target has native support for the former but not the latter. */
241 (simplify
242 (mult @0 VECTOR_CST@1)
243 (if (initializer_each_zero_or_onep (@1)
244 && !HONOR_SNANS (type)
245 && !HONOR_SIGNED_ZEROS (type))
246 (with { tree itype = FLOAT_TYPE_P (type) ? unsigned_type_for (type) : type; }
247 (if (itype
248 && (!VECTOR_MODE_P (TYPE_MODE (type))
249 || (VECTOR_MODE_P (TYPE_MODE (itype))
250 && optab_handler (and_optab,
251 TYPE_MODE (itype)) != CODE_FOR_nothing)))
252 (view_convert (bit_and:itype (view_convert @0)
253 (ne @1 { build_zero_cst (type); })))))))
254
255 (for cmp (gt ge lt le)
256 outp (convert convert negate negate)
257 outn (negate negate convert convert)
258 /* Transform X * (X > 0.0 ? 1.0 : -1.0) into abs(X). */
259 /* Transform X * (X >= 0.0 ? 1.0 : -1.0) into abs(X). */
260 /* Transform X * (X < 0.0 ? 1.0 : -1.0) into -abs(X). */
261 /* Transform X * (X <= 0.0 ? 1.0 : -1.0) into -abs(X). */
262 (simplify
263 (mult:c @0 (cond (cmp @0 real_zerop) real_onep@1 real_minus_onep))
264 (if (!tree_expr_maybe_nan_p (@0) && !HONOR_SIGNED_ZEROS (type))
265 (outp (abs @0))))
266 /* Transform X * (X > 0.0 ? -1.0 : 1.0) into -abs(X). */
267 /* Transform X * (X >= 0.0 ? -1.0 : 1.0) into -abs(X). */
268 /* Transform X * (X < 0.0 ? -1.0 : 1.0) into abs(X). */
269 /* Transform X * (X <= 0.0 ? -1.0 : 1.0) into abs(X). */
270 (simplify
271 (mult:c @0 (cond (cmp @0 real_zerop) real_minus_onep real_onep@1))
272 (if (!tree_expr_maybe_nan_p (@0) && !HONOR_SIGNED_ZEROS (type))
273 (outn (abs @0)))))
274
275 /* Transform X * copysign (1.0, X) into abs(X). */
276 (simplify
277 (mult:c @0 (COPYSIGN_ALL real_onep @0))
278 (if (!tree_expr_maybe_nan_p (@0) && !HONOR_SIGNED_ZEROS (type))
279 (abs @0)))
280
281 /* Transform X * copysign (1.0, -X) into -abs(X). */
282 (simplify
283 (mult:c @0 (COPYSIGN_ALL real_onep (negate @0)))
284 (if (!tree_expr_maybe_nan_p (@0) && !HONOR_SIGNED_ZEROS (type))
285 (negate (abs @0))))
286
287 /* Transform copysign (CST, X) into copysign (ABS(CST), X). */
288 (simplify
289 (COPYSIGN_ALL REAL_CST@0 @1)
290 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@0)))
291 (COPYSIGN_ALL (negate @0) @1)))
292
293 /* X * 1, X / 1 -> X. */
294 (for op (mult trunc_div ceil_div floor_div round_div exact_div)
295 (simplify
296 (op @0 integer_onep)
297 (non_lvalue @0)))
298
299 /* (A / (1 << B)) -> (A >> B).
300 Only for unsigned A. For signed A, this would not preserve rounding
301 toward zero.
302 For example: (-1 / ( 1 << B)) != -1 >> B.
303 Also also widening conversions, like:
304 (A / (unsigned long long) (1U << B)) -> (A >> B)
305 or
306 (A / (unsigned long long) (1 << B)) -> (A >> B).
307 If the left shift is signed, it can be done only if the upper bits
308 of A starting from shift's type sign bit are zero, as
309 (unsigned long long) (1 << 31) is -2147483648ULL, not 2147483648ULL,
310 so it is valid only if A >> 31 is zero. */
311 (simplify
312 (trunc_div (convert?@0 @3) (convert2? (lshift integer_onep@1 @2)))
313 (if ((TYPE_UNSIGNED (type) || tree_expr_nonnegative_p (@0))
314 && (!VECTOR_TYPE_P (type)
315 || target_supports_op_p (type, RSHIFT_EXPR, optab_vector)
316 || target_supports_op_p (type, RSHIFT_EXPR, optab_scalar))
317 && (useless_type_conversion_p (type, TREE_TYPE (@1))
318 || (element_precision (type) >= element_precision (TREE_TYPE (@1))
319 && (TYPE_UNSIGNED (TREE_TYPE (@1))
320 || (element_precision (type)
321 == element_precision (TREE_TYPE (@1)))
322 || (INTEGRAL_TYPE_P (type)
323 && (tree_nonzero_bits (@0)
324 & wi::mask (element_precision (TREE_TYPE (@1)) - 1,
325 true,
326 element_precision (type))) == 0)))))
327 (if (!VECTOR_TYPE_P (type)
328 && useless_type_conversion_p (TREE_TYPE (@3), TREE_TYPE (@1))
329 && element_precision (TREE_TYPE (@3)) < element_precision (type))
330 (convert (rshift @3 @2))
331 (rshift @0 @2))))
332
333 /* Preserve explicit divisions by 0: the C++ front-end wants to detect
334 undefined behavior in constexpr evaluation, and assuming that the division
335 traps enables better optimizations than these anyway. */
336 (for div (trunc_div ceil_div floor_div round_div exact_div)
337 /* 0 / X is always zero. */
338 (simplify
339 (div integer_zerop@0 @1)
340 /* But not for 0 / 0 so that we can get the proper warnings and errors. */
341 (if (!integer_zerop (@1))
342 @0))
343 /* X / -1 is -X. */
344 (simplify
345 (div @0 integer_minus_onep@1)
346 (if (!TYPE_UNSIGNED (type))
347 (negate @0)))
348 /* X / bool_range_Y is X. */
349 (simplify
350 (div @0 SSA_NAME@1)
351 (if (INTEGRAL_TYPE_P (type) && ssa_name_has_boolean_range (@1))
352 @0))
353 /* X / X is one. */
354 (simplify
355 (div @0 @0)
356 /* But not for 0 / 0 so that we can get the proper warnings and errors.
357 And not for _Fract types where we can't build 1. */
358 (if (!integer_zerop (@0) && !ALL_FRACT_MODE_P (TYPE_MODE (type)))
359 { build_one_cst (type); }))
360 /* X / abs (X) is X < 0 ? -1 : 1. */
361 (simplify
362 (div:C @0 (abs @0))
363 (if (INTEGRAL_TYPE_P (type)
364 && TYPE_OVERFLOW_UNDEFINED (type))
365 (cond (lt @0 { build_zero_cst (type); })
366 { build_minus_one_cst (type); } { build_one_cst (type); })))
367 /* X / -X is -1. */
368 (simplify
369 (div:C @0 (negate @0))
370 (if ((INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
371 && TYPE_OVERFLOW_UNDEFINED (type))
372 { build_minus_one_cst (type); })))
373
374 /* For unsigned integral types, FLOOR_DIV_EXPR is the same as
375 TRUNC_DIV_EXPR. Rewrite into the latter in this case. */
376 (simplify
377 (floor_div @0 @1)
378 (if ((INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
379 && TYPE_UNSIGNED (type))
380 (trunc_div @0 @1)))
381
382 /* Combine two successive divisions. Note that combining ceil_div
383 and floor_div is trickier and combining round_div even more so. */
384 (for div (trunc_div exact_div)
385 (simplify
386 (div (div@3 @0 INTEGER_CST@1) INTEGER_CST@2)
387 (with {
388 wi::overflow_type overflow;
389 wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@2),
390 TYPE_SIGN (type), &overflow);
391 }
392 (if (div == EXACT_DIV_EXPR
393 || optimize_successive_divisions_p (@2, @3))
394 (if (!overflow)
395 (div @0 { wide_int_to_tree (type, mul); })
396 (if (TYPE_UNSIGNED (type)
397 || mul != wi::min_value (TYPE_PRECISION (type), SIGNED))
398 { build_zero_cst (type); }))))))
399
400 /* Combine successive multiplications. Similar to above, but handling
401 overflow is different. */
402 (simplify
403 (mult (mult @0 INTEGER_CST@1) INTEGER_CST@2)
404 (with {
405 wi::overflow_type overflow;
406 wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@2),
407 TYPE_SIGN (type), &overflow);
408 }
409 /* Skip folding on overflow: the only special case is @1 * @2 == -INT_MIN,
410 otherwise undefined overflow implies that @0 must be zero. */
411 (if (!overflow || TYPE_OVERFLOW_WRAPS (type))
412 (mult @0 { wide_int_to_tree (type, mul); }))))
413
414 /* Optimize A / A to 1.0 if we don't care about
415 NaNs or Infinities. */
416 (simplify
417 (rdiv @0 @0)
418 (if (FLOAT_TYPE_P (type)
419 && ! HONOR_NANS (type)
420 && ! HONOR_INFINITIES (type))
421 { build_one_cst (type); }))
422
423 /* Optimize -A / A to -1.0 if we don't care about
424 NaNs or Infinities. */
425 (simplify
426 (rdiv:C @0 (negate @0))
427 (if (FLOAT_TYPE_P (type)
428 && ! HONOR_NANS (type)
429 && ! HONOR_INFINITIES (type))
430 { build_minus_one_cst (type); }))
431
432 /* PR71078: x / abs(x) -> copysign (1.0, x) */
433 (simplify
434 (rdiv:C (convert? @0) (convert? (abs @0)))
435 (if (SCALAR_FLOAT_TYPE_P (type)
436 && ! HONOR_NANS (type)
437 && ! HONOR_INFINITIES (type))
438 (switch
439 (if (types_match (type, float_type_node))
440 (BUILT_IN_COPYSIGNF { build_one_cst (type); } (convert @0)))
441 (if (types_match (type, double_type_node))
442 (BUILT_IN_COPYSIGN { build_one_cst (type); } (convert @0)))
443 (if (types_match (type, long_double_type_node))
444 (BUILT_IN_COPYSIGNL { build_one_cst (type); } (convert @0))))))
445
446 /* In IEEE floating point, x/1 is not equivalent to x for snans. */
447 (simplify
448 (rdiv @0 real_onep)
449 (if (!tree_expr_maybe_signaling_nan_p (@0))
450 (non_lvalue @0)))
451
452 /* In IEEE floating point, x/-1 is not equivalent to -x for snans. */
453 (simplify
454 (rdiv @0 real_minus_onep)
455 (if (!tree_expr_maybe_signaling_nan_p (@0))
456 (negate @0)))
457
458 (if (flag_reciprocal_math)
459 /* Convert (A/B)/C to A/(B*C). */
460 (simplify
461 (rdiv (rdiv:s @0 @1) @2)
462 (rdiv @0 (mult @1 @2)))
463
464 /* Canonicalize x / (C1 * y) to (x * C2) / y. */
465 (simplify
466 (rdiv @0 (mult:s @1 REAL_CST@2))
467 (with
468 { tree tem = const_binop (RDIV_EXPR, type, build_one_cst (type), @2); }
469 (if (tem)
470 (rdiv (mult @0 { tem; } ) @1))))
471
472 /* Convert A/(B/C) to (A/B)*C */
473 (simplify
474 (rdiv @0 (rdiv:s @1 @2))
475 (mult (rdiv @0 @1) @2)))
476
477 /* Simplify x / (- y) to -x / y. */
478 (simplify
479 (rdiv @0 (negate @1))
480 (rdiv (negate @0) @1))
481
482 (if (flag_unsafe_math_optimizations)
483 /* Simplify (C / x op 0.0) to x op 0.0 for C != 0, C != Inf/Nan.
484 Since C / x may underflow to zero, do this only for unsafe math. */
485 (for op (lt le gt ge)
486 neg_op (gt ge lt le)
487 (simplify
488 (op (rdiv REAL_CST@0 @1) real_zerop@2)
489 (if (!HONOR_SIGNED_ZEROS (@1) && !HONOR_INFINITIES (@1))
490 (switch
491 (if (real_less (&dconst0, TREE_REAL_CST_PTR (@0)))
492 (op @1 @2))
493 /* For C < 0, use the inverted operator. */
494 (if (real_less (TREE_REAL_CST_PTR (@0), &dconst0))
495 (neg_op @1 @2)))))))
496
497 /* Optimize (X & (-A)) / A where A is a power of 2, to X >> log2(A) */
498 (for div (trunc_div ceil_div floor_div round_div exact_div)
499 (simplify
500 (div (convert? (bit_and @0 INTEGER_CST@1)) INTEGER_CST@2)
501 (if (integer_pow2p (@2)
502 && tree_int_cst_sgn (@2) > 0
503 && tree_nop_conversion_p (type, TREE_TYPE (@0))
504 && wi::to_wide (@2) + wi::to_wide (@1) == 0)
505 (rshift (convert @0)
506 { build_int_cst (integer_type_node,
507 wi::exact_log2 (wi::to_wide (@2))); }))))
508
509 /* If ARG1 is a constant, we can convert this to a multiply by the
510 reciprocal. This does not have the same rounding properties,
511 so only do this if -freciprocal-math. We can actually
512 always safely do it if ARG1 is a power of two, but it's hard to
513 tell if it is or not in a portable manner. */
514 (for cst (REAL_CST COMPLEX_CST VECTOR_CST)
515 (simplify
516 (rdiv @0 cst@1)
517 (if (optimize)
518 (if (flag_reciprocal_math
519 && !real_zerop (@1))
520 (with
521 { tree tem = const_binop (RDIV_EXPR, type, build_one_cst (type), @1); }
522 (if (tem)
523 (mult @0 { tem; } )))
524 (if (cst != COMPLEX_CST)
525 (with { tree inverse = exact_inverse (type, @1); }
526 (if (inverse)
527 (mult @0 { inverse; } ))))))))
528
529 (for mod (ceil_mod floor_mod round_mod trunc_mod)
530 /* 0 % X is always zero. */
531 (simplify
532 (mod integer_zerop@0 @1)
533 /* But not for 0 % 0 so that we can get the proper warnings and errors. */
534 (if (!integer_zerop (@1))
535 @0))
536 /* X % 1 is always zero. */
537 (simplify
538 (mod @0 integer_onep)
539 { build_zero_cst (type); })
540 /* X % -1 is zero. */
541 (simplify
542 (mod @0 integer_minus_onep@1)
543 (if (!TYPE_UNSIGNED (type))
544 { build_zero_cst (type); }))
545 /* X % X is zero. */
546 (simplify
547 (mod @0 @0)
548 /* But not for 0 % 0 so that we can get the proper warnings and errors. */
549 (if (!integer_zerop (@0))
550 { build_zero_cst (type); }))
551 /* (X % Y) % Y is just X % Y. */
552 (simplify
553 (mod (mod@2 @0 @1) @1)
554 @2)
555 /* From extract_muldiv_1: (X * C1) % C2 is zero if C1 is a multiple of C2. */
556 (simplify
557 (mod (mult @0 INTEGER_CST@1) INTEGER_CST@2)
558 (if (ANY_INTEGRAL_TYPE_P (type)
559 && TYPE_OVERFLOW_UNDEFINED (type)
560 && wi::multiple_of_p (wi::to_wide (@1), wi::to_wide (@2),
561 TYPE_SIGN (type)))
562 { build_zero_cst (type); }))
563 /* For (X % C) == 0, if X is signed and C is power of 2, use unsigned
564 modulo and comparison, since it is simpler and equivalent. */
565 (for cmp (eq ne)
566 (simplify
567 (cmp (mod @0 integer_pow2p@2) integer_zerop@1)
568 (if (!TYPE_UNSIGNED (TREE_TYPE (@0)))
569 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
570 (cmp (mod (convert:utype @0) (convert:utype @2)) (convert:utype @1)))))))
571
572 /* X % -C is the same as X % C. */
573 (simplify
574 (trunc_mod @0 INTEGER_CST@1)
575 (if (TYPE_SIGN (type) == SIGNED
576 && !TREE_OVERFLOW (@1)
577 && wi::neg_p (wi::to_wide (@1))
578 && !TYPE_OVERFLOW_TRAPS (type)
579 /* Avoid this transformation if C is INT_MIN, i.e. C == -C. */
580 && !sign_bit_p (@1, @1))
581 (trunc_mod @0 (negate @1))))
582
583 /* X % -Y is the same as X % Y. */
584 (simplify
585 (trunc_mod @0 (convert? (negate @1)))
586 (if (INTEGRAL_TYPE_P (type)
587 && !TYPE_UNSIGNED (type)
588 && !TYPE_OVERFLOW_TRAPS (type)
589 && tree_nop_conversion_p (type, TREE_TYPE (@1))
590 /* Avoid this transformation if X might be INT_MIN or
591 Y might be -1, because we would then change valid
592 INT_MIN % -(-1) into invalid INT_MIN % -1. */
593 && (expr_not_equal_to (@0, wi::to_wide (TYPE_MIN_VALUE (type)))
594 || expr_not_equal_to (@1, wi::minus_one (TYPE_PRECISION
595 (TREE_TYPE (@1))))))
596 (trunc_mod @0 (convert @1))))
597
598 /* X - (X / Y) * Y is the same as X % Y. */
599 (simplify
600 (minus (convert1? @0) (convert2? (mult:c (trunc_div @@0 @@1) @1)))
601 (if (INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
602 (convert (trunc_mod @0 @1))))
603
604 /* Optimize TRUNC_MOD_EXPR by a power of two into a BIT_AND_EXPR,
605 i.e. "X % C" into "X & (C - 1)", if X and C are positive.
606 Also optimize A % (C << N) where C is a power of 2,
607 to A & ((C << N) - 1).
608 Also optimize "A shift (B % C)", if C is a power of 2, to
609 "A shift (B & (C - 1))". SHIFT operation include "<<" and ">>"
610 and assume (B % C) is nonnegative as shifts negative values would
611 be UB. */
612 (match (power_of_two_cand @1)
613 INTEGER_CST@1)
614 (match (power_of_two_cand @1)
615 (lshift INTEGER_CST@1 @2))
616 (for mod (trunc_mod floor_mod)
617 (for shift (lshift rshift)
618 (simplify
619 (shift @0 (mod @1 (power_of_two_cand@2 @3)))
620 (if (integer_pow2p (@3) && tree_int_cst_sgn (@3) > 0)
621 (shift @0 (bit_and @1 (minus @2 { build_int_cst (TREE_TYPE (@2),
622 1); }))))))
623 (simplify
624 (mod @0 (convert? (power_of_two_cand@1 @2)))
625 (if ((TYPE_UNSIGNED (type) || tree_expr_nonnegative_p (@0))
626 /* Allow any integral conversions of the divisor, except
627 conversion from narrower signed to wider unsigned type
628 where if @1 would be negative power of two, the divisor
629 would not be a power of two. */
630 && INTEGRAL_TYPE_P (type)
631 && INTEGRAL_TYPE_P (TREE_TYPE (@1))
632 && (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@1))
633 || TYPE_UNSIGNED (TREE_TYPE (@1))
634 || !TYPE_UNSIGNED (type))
635 && integer_pow2p (@2) && tree_int_cst_sgn (@2) > 0)
636 (with { tree utype = TREE_TYPE (@1);
637 if (!TYPE_OVERFLOW_WRAPS (utype))
638 utype = unsigned_type_for (utype); }
639 (bit_and @0 (convert (minus (convert:utype @1)
640 { build_one_cst (utype); })))))))
641
642 /* Simplify (unsigned t * 2)/2 -> unsigned t & 0x7FFFFFFF. */
643 (simplify
644 (trunc_div (mult @0 integer_pow2p@1) @1)
645 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
646 (bit_and @0 { wide_int_to_tree
647 (type, wi::mask (TYPE_PRECISION (type)
648 - wi::exact_log2 (wi::to_wide (@1)),
649 false, TYPE_PRECISION (type))); })))
650
651 /* Simplify (unsigned t / 2) * 2 -> unsigned t & ~1. */
652 (simplify
653 (mult (trunc_div @0 integer_pow2p@1) @1)
654 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
655 (bit_and @0 (negate @1))))
656
657 /* Simplify (t * 2) / 2) -> t. */
658 (for div (trunc_div ceil_div floor_div round_div exact_div)
659 (simplify
660 (div (mult:c @0 @1) @1)
661 (if (ANY_INTEGRAL_TYPE_P (type))
662 (if (TYPE_OVERFLOW_UNDEFINED (type))
663 @0
664 #if GIMPLE
665 (with
666 {
667 bool overflowed = true;
668 value_range vr0, vr1;
669 if (INTEGRAL_TYPE_P (type)
670 && get_global_range_query ()->range_of_expr (vr0, @0)
671 && get_global_range_query ()->range_of_expr (vr1, @1)
672 && vr0.kind () == VR_RANGE
673 && vr1.kind () == VR_RANGE)
674 {
675 wide_int wmin0 = vr0.lower_bound ();
676 wide_int wmax0 = vr0.upper_bound ();
677 wide_int wmin1 = vr1.lower_bound ();
678 wide_int wmax1 = vr1.upper_bound ();
679 /* If the multiplication can't overflow/wrap around, then
680 it can be optimized too. */
681 wi::overflow_type min_ovf, max_ovf;
682 wi::mul (wmin0, wmin1, TYPE_SIGN (type), &min_ovf);
683 wi::mul (wmax0, wmax1, TYPE_SIGN (type), &max_ovf);
684 if (min_ovf == wi::OVF_NONE && max_ovf == wi::OVF_NONE)
685 {
686 wi::mul (wmin0, wmax1, TYPE_SIGN (type), &min_ovf);
687 wi::mul (wmax0, wmin1, TYPE_SIGN (type), &max_ovf);
688 if (min_ovf == wi::OVF_NONE && max_ovf == wi::OVF_NONE)
689 overflowed = false;
690 }
691 }
692 }
693 (if (!overflowed)
694 @0))
695 #endif
696 ))))
697
698 (for op (negate abs)
699 /* Simplify cos(-x) and cos(|x|) -> cos(x). Similarly for cosh. */
700 (for coss (COS COSH)
701 (simplify
702 (coss (op @0))
703 (coss @0)))
704 /* Simplify pow(-x, y) and pow(|x|,y) -> pow(x,y) if y is an even integer. */
705 (for pows (POW)
706 (simplify
707 (pows (op @0) REAL_CST@1)
708 (with { HOST_WIDE_INT n; }
709 (if (real_isinteger (&TREE_REAL_CST (@1), &n) && (n & 1) == 0)
710 (pows @0 @1)))))
711 /* Likewise for powi. */
712 (for pows (POWI)
713 (simplify
714 (pows (op @0) INTEGER_CST@1)
715 (if ((wi::to_wide (@1) & 1) == 0)
716 (pows @0 @1))))
717 /* Strip negate and abs from both operands of hypot. */
718 (for hypots (HYPOT)
719 (simplify
720 (hypots (op @0) @1)
721 (hypots @0 @1))
722 (simplify
723 (hypots @0 (op @1))
724 (hypots @0 @1)))
725 /* copysign(-x, y) and copysign(abs(x), y) -> copysign(x, y). */
726 (for copysigns (COPYSIGN_ALL)
727 (simplify
728 (copysigns (op @0) @1)
729 (copysigns @0 @1))))
730
731 /* abs(x)*abs(x) -> x*x. Should be valid for all types. */
732 (simplify
733 (mult (abs@1 @0) @1)
734 (mult @0 @0))
735
736 /* Convert absu(x)*absu(x) -> x*x. */
737 (simplify
738 (mult (absu@1 @0) @1)
739 (mult (convert@2 @0) @2))
740
741 /* cos(copysign(x, y)) -> cos(x). Similarly for cosh. */
742 (for coss (COS COSH)
743 copysigns (COPYSIGN)
744 (simplify
745 (coss (copysigns @0 @1))
746 (coss @0)))
747
748 /* pow(copysign(x, y), z) -> pow(x, z) if z is an even integer. */
749 (for pows (POW)
750 copysigns (COPYSIGN)
751 (simplify
752 (pows (copysigns @0 @2) REAL_CST@1)
753 (with { HOST_WIDE_INT n; }
754 (if (real_isinteger (&TREE_REAL_CST (@1), &n) && (n & 1) == 0)
755 (pows @0 @1)))))
756 /* Likewise for powi. */
757 (for pows (POWI)
758 copysigns (COPYSIGN)
759 (simplify
760 (pows (copysigns @0 @2) INTEGER_CST@1)
761 (if ((wi::to_wide (@1) & 1) == 0)
762 (pows @0 @1))))
763
764 (for hypots (HYPOT)
765 copysigns (COPYSIGN)
766 /* hypot(copysign(x, y), z) -> hypot(x, z). */
767 (simplify
768 (hypots (copysigns @0 @1) @2)
769 (hypots @0 @2))
770 /* hypot(x, copysign(y, z)) -> hypot(x, y). */
771 (simplify
772 (hypots @0 (copysigns @1 @2))
773 (hypots @0 @1)))
774
775 /* copysign(x, CST) -> [-]abs (x). */
776 (for copysigns (COPYSIGN_ALL)
777 (simplify
778 (copysigns @0 REAL_CST@1)
779 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
780 (negate (abs @0))
781 (abs @0))))
782
783 /* copysign(copysign(x, y), z) -> copysign(x, z). */
784 (for copysigns (COPYSIGN_ALL)
785 (simplify
786 (copysigns (copysigns @0 @1) @2)
787 (copysigns @0 @2)))
788
789 /* copysign(x,y)*copysign(x,y) -> x*x. */
790 (for copysigns (COPYSIGN_ALL)
791 (simplify
792 (mult (copysigns@2 @0 @1) @2)
793 (mult @0 @0)))
794
795 /* ccos(-x) -> ccos(x). Similarly for ccosh. */
796 (for ccoss (CCOS CCOSH)
797 (simplify
798 (ccoss (negate @0))
799 (ccoss @0)))
800
801 /* cabs(-x) and cos(conj(x)) -> cabs(x). */
802 (for ops (conj negate)
803 (for cabss (CABS)
804 (simplify
805 (cabss (ops @0))
806 (cabss @0))))
807
808 /* Fold (a * (1 << b)) into (a << b) */
809 (simplify
810 (mult:c @0 (convert? (lshift integer_onep@1 @2)))
811 (if (! FLOAT_TYPE_P (type)
812 && tree_nop_conversion_p (type, TREE_TYPE (@1)))
813 (lshift @0 @2)))
814
815 /* Fold (1 << (C - x)) where C = precision(type) - 1
816 into ((1 << C) >> x). */
817 (simplify
818 (lshift integer_onep@0 (minus@1 INTEGER_CST@2 @3))
819 (if (INTEGRAL_TYPE_P (type)
820 && wi::eq_p (wi::to_wide (@2), TYPE_PRECISION (type) - 1)
821 && single_use (@1))
822 (if (TYPE_UNSIGNED (type))
823 (rshift (lshift @0 @2) @3)
824 (with
825 { tree utype = unsigned_type_for (type); }
826 (convert (rshift (lshift (convert:utype @0) @2) @3))))))
827
828 /* Fold (C1/X)*C2 into (C1*C2)/X. */
829 (simplify
830 (mult (rdiv@3 REAL_CST@0 @1) REAL_CST@2)
831 (if (flag_associative_math
832 && single_use (@3))
833 (with
834 { tree tem = const_binop (MULT_EXPR, type, @0, @2); }
835 (if (tem)
836 (rdiv { tem; } @1)))))
837
838 /* Simplify ~X & X as zero. */
839 (simplify
840 (bit_and:c (convert? @0) (convert? (bit_not @0)))
841 { build_zero_cst (type); })
842
843 /* PR71636: Transform x & ((1U << b) - 1) -> x & ~(~0U << b); */
844 (simplify
845 (bit_and:c @0 (plus:s (lshift:s integer_onep @1) integer_minus_onep))
846 (if (TYPE_UNSIGNED (type))
847 (bit_and @0 (bit_not (lshift { build_all_ones_cst (type); } @1)))))
848
849 (for bitop (bit_and bit_ior)
850 cmp (eq ne)
851 /* PR35691: Transform
852 (x == 0 & y == 0) -> (x | typeof(x)(y)) == 0.
853 (x != 0 | y != 0) -> (x | typeof(x)(y)) != 0. */
854 (simplify
855 (bitop (cmp @0 integer_zerop@2) (cmp @1 integer_zerop))
856 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
857 && INTEGRAL_TYPE_P (TREE_TYPE (@1))
858 && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
859 (cmp (bit_ior @0 (convert @1)) @2)))
860 /* Transform:
861 (x == -1 & y == -1) -> (x & typeof(x)(y)) == -1.
862 (x != -1 | y != -1) -> (x & typeof(x)(y)) != -1. */
863 (simplify
864 (bitop (cmp @0 integer_all_onesp@2) (cmp @1 integer_all_onesp))
865 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
866 && INTEGRAL_TYPE_P (TREE_TYPE (@1))
867 && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
868 (cmp (bit_and @0 (convert @1)) @2))))
869
870 /* Fold (A & ~B) - (A & B) into (A ^ B) - B. */
871 (simplify
872 (minus (bit_and:cs @0 (bit_not @1)) (bit_and:cs @0 @1))
873 (minus (bit_xor @0 @1) @1))
874 (simplify
875 (minus (bit_and:s @0 INTEGER_CST@2) (bit_and:s @0 INTEGER_CST@1))
876 (if (~wi::to_wide (@2) == wi::to_wide (@1))
877 (minus (bit_xor @0 @1) @1)))
878
879 /* Fold (A & B) - (A & ~B) into B - (A ^ B). */
880 (simplify
881 (minus (bit_and:cs @0 @1) (bit_and:cs @0 (bit_not @1)))
882 (minus @1 (bit_xor @0 @1)))
883
884 /* Simplify (X & ~Y) |^+ (~X & Y) -> X ^ Y. */
885 (for op (bit_ior bit_xor plus)
886 (simplify
887 (op (bit_and:c @0 (bit_not @1)) (bit_and:c (bit_not @0) @1))
888 (bit_xor @0 @1))
889 (simplify
890 (op:c (bit_and @0 INTEGER_CST@2) (bit_and (bit_not @0) INTEGER_CST@1))
891 (if (~wi::to_wide (@2) == wi::to_wide (@1))
892 (bit_xor @0 @1))))
893
894 /* PR53979: Transform ((a ^ b) | a) -> (a | b) */
895 (simplify
896 (bit_ior:c (bit_xor:c @0 @1) @0)
897 (bit_ior @0 @1))
898
899 /* (a & ~b) | (a ^ b) --> a ^ b */
900 (simplify
901 (bit_ior:c (bit_and:c @0 (bit_not @1)) (bit_xor:c@2 @0 @1))
902 @2)
903
904 /* (a & ~b) ^ ~a --> ~(a & b) */
905 (simplify
906 (bit_xor:c (bit_and:cs @0 (bit_not @1)) (bit_not @0))
907 (bit_not (bit_and @0 @1)))
908
909 /* (~a & b) ^ a --> (a | b) */
910 (simplify
911 (bit_xor:c (bit_and:cs (bit_not @0) @1) @0)
912 (bit_ior @0 @1))
913
914 /* (a | b) & ~(a ^ b) --> a & b */
915 (simplify
916 (bit_and:c (bit_ior @0 @1) (bit_not (bit_xor:c @0 @1)))
917 (bit_and @0 @1))
918
919 /* a | ~(a ^ b) --> a | ~b */
920 (simplify
921 (bit_ior:c @0 (bit_not:s (bit_xor:c @0 @1)))
922 (bit_ior @0 (bit_not @1)))
923
924 /* (a | b) | (a &^ b) --> a | b */
925 (for op (bit_and bit_xor)
926 (simplify
927 (bit_ior:c (bit_ior@2 @0 @1) (op:c @0 @1))
928 @2))
929
930 /* (a & b) | ~(a ^ b) --> ~(a ^ b) */
931 (simplify
932 (bit_ior:c (bit_and:c @0 @1) (bit_not@2 (bit_xor @0 @1)))
933 @2)
934
935 /* ~(~a & b) --> a | ~b */
936 (simplify
937 (bit_not (bit_and:cs (bit_not @0) @1))
938 (bit_ior @0 (bit_not @1)))
939
940 /* ~(~a | b) --> a & ~b */
941 (simplify
942 (bit_not (bit_ior:cs (bit_not @0) @1))
943 (bit_and @0 (bit_not @1)))
944
945 /* (a ^ b) & ((b ^ c) ^ a) --> (a ^ b) & ~c */
946 (simplify
947 (bit_and:c (bit_xor:c@3 @0 @1) (bit_xor:cs (bit_xor:cs @1 @2) @0))
948 (bit_and @3 (bit_not @2)))
949
950 /* (a ^ b) | ((b ^ c) ^ a) --> (a ^ b) | c */
951 (simplify
952 (bit_ior:c (bit_xor:c@3 @0 @1) (bit_xor:c (bit_xor:c @1 @2) @0))
953 (bit_ior @3 @2))
954
955 #if GIMPLE
956 /* (~X | C) ^ D -> (X | C) ^ (~D ^ C) if (~D ^ C) can be simplified. */
957 (simplify
958 (bit_xor:c (bit_ior:cs (bit_not:s @0) @1) @2)
959 (bit_xor (bit_ior @0 @1) (bit_xor! (bit_not! @2) @1)))
960
961 /* (~X & C) ^ D -> (X & C) ^ (D ^ C) if (D ^ C) can be simplified. */
962 (simplify
963 (bit_xor:c (bit_and:cs (bit_not:s @0) @1) @2)
964 (bit_xor (bit_and @0 @1) (bit_xor! @2 @1)))
965
966 /* Simplify (~X & Y) to X ^ Y if we know that (X & ~Y) is 0. */
967 (simplify
968 (bit_and (bit_not SSA_NAME@0) INTEGER_CST@1)
969 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
970 && wi::bit_and_not (get_nonzero_bits (@0), wi::to_wide (@1)) == 0)
971 (bit_xor @0 @1)))
972 #endif
973
974 /* For constants M and N, if M == (1LL << cst) - 1 && (N & M) == M,
975 ((A & N) + B) & M -> (A + B) & M
976 Similarly if (N & M) == 0,
977 ((A | N) + B) & M -> (A + B) & M
978 and for - instead of + (or unary - instead of +)
979 and/or ^ instead of |.
980 If B is constant and (B & M) == 0, fold into A & M. */
981 (for op (plus minus)
982 (for bitop (bit_and bit_ior bit_xor)
983 (simplify
984 (bit_and (op:s (bitop:s@0 @3 INTEGER_CST@4) @1) INTEGER_CST@2)
985 (with
986 { tree pmop[2];
987 tree utype = fold_bit_and_mask (TREE_TYPE (@0), @2, op, @0, bitop,
988 @3, @4, @1, ERROR_MARK, NULL_TREE,
989 NULL_TREE, pmop); }
990 (if (utype)
991 (convert (bit_and (op (convert:utype { pmop[0]; })
992 (convert:utype { pmop[1]; }))
993 (convert:utype @2))))))
994 (simplify
995 (bit_and (op:s @0 (bitop:s@1 @3 INTEGER_CST@4)) INTEGER_CST@2)
996 (with
997 { tree pmop[2];
998 tree utype = fold_bit_and_mask (TREE_TYPE (@0), @2, op, @0, ERROR_MARK,
999 NULL_TREE, NULL_TREE, @1, bitop, @3,
1000 @4, pmop); }
1001 (if (utype)
1002 (convert (bit_and (op (convert:utype { pmop[0]; })
1003 (convert:utype { pmop[1]; }))
1004 (convert:utype @2)))))))
1005 (simplify
1006 (bit_and (op:s @0 @1) INTEGER_CST@2)
1007 (with
1008 { tree pmop[2];
1009 tree utype = fold_bit_and_mask (TREE_TYPE (@0), @2, op, @0, ERROR_MARK,
1010 NULL_TREE, NULL_TREE, @1, ERROR_MARK,
1011 NULL_TREE, NULL_TREE, pmop); }
1012 (if (utype)
1013 (convert (bit_and (op (convert:utype { pmop[0]; })
1014 (convert:utype { pmop[1]; }))
1015 (convert:utype @2)))))))
1016 (for bitop (bit_and bit_ior bit_xor)
1017 (simplify
1018 (bit_and (negate:s (bitop:s@0 @2 INTEGER_CST@3)) INTEGER_CST@1)
1019 (with
1020 { tree pmop[2];
1021 tree utype = fold_bit_and_mask (TREE_TYPE (@0), @1, NEGATE_EXPR, @0,
1022 bitop, @2, @3, NULL_TREE, ERROR_MARK,
1023 NULL_TREE, NULL_TREE, pmop); }
1024 (if (utype)
1025 (convert (bit_and (negate (convert:utype { pmop[0]; }))
1026 (convert:utype @1)))))))
1027
1028 /* X % Y is smaller than Y. */
1029 (for cmp (lt ge)
1030 (simplify
1031 (cmp (trunc_mod @0 @1) @1)
1032 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
1033 { constant_boolean_node (cmp == LT_EXPR, type); })))
1034 (for cmp (gt le)
1035 (simplify
1036 (cmp @1 (trunc_mod @0 @1))
1037 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
1038 { constant_boolean_node (cmp == GT_EXPR, type); })))
1039
1040 /* x | ~0 -> ~0 */
1041 (simplify
1042 (bit_ior @0 integer_all_onesp@1)
1043 @1)
1044
1045 /* x | 0 -> x */
1046 (simplify
1047 (bit_ior @0 integer_zerop)
1048 @0)
1049
1050 /* x & 0 -> 0 */
1051 (simplify
1052 (bit_and @0 integer_zerop@1)
1053 @1)
1054
1055 /* ~x | x -> -1 */
1056 /* ~x ^ x -> -1 */
1057 /* ~x + x -> -1 */
1058 (for op (bit_ior bit_xor plus)
1059 (simplify
1060 (op:c (convert? @0) (convert? (bit_not @0)))
1061 (convert { build_all_ones_cst (TREE_TYPE (@0)); })))
1062
1063 /* x ^ x -> 0 */
1064 (simplify
1065 (bit_xor @0 @0)
1066 { build_zero_cst (type); })
1067
1068 /* Canonicalize X ^ ~0 to ~X. */
1069 (simplify
1070 (bit_xor @0 integer_all_onesp@1)
1071 (bit_not @0))
1072
1073 /* x & ~0 -> x */
1074 (simplify
1075 (bit_and @0 integer_all_onesp)
1076 (non_lvalue @0))
1077
1078 /* x & x -> x, x | x -> x */
1079 (for bitop (bit_and bit_ior)
1080 (simplify
1081 (bitop @0 @0)
1082 (non_lvalue @0)))
1083
1084 /* x & C -> x if we know that x & ~C == 0. */
1085 #if GIMPLE
1086 (simplify
1087 (bit_and SSA_NAME@0 INTEGER_CST@1)
1088 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1089 && wi::bit_and_not (get_nonzero_bits (@0), wi::to_wide (@1)) == 0)
1090 @0))
1091 #endif
1092
1093 /* ~(~X - Y) -> X + Y and ~(~X + Y) -> X - Y. */
1094 (simplify
1095 (bit_not (minus (bit_not @0) @1))
1096 (plus @0 @1))
1097 (simplify
1098 (bit_not (plus:c (bit_not @0) @1))
1099 (minus @0 @1))
1100
1101 /* ~(X - Y) -> ~X + Y. */
1102 (simplify
1103 (bit_not (minus:s @0 @1))
1104 (plus (bit_not @0) @1))
1105 (simplify
1106 (bit_not (plus:s @0 INTEGER_CST@1))
1107 (if ((INTEGRAL_TYPE_P (type)
1108 && TYPE_UNSIGNED (type))
1109 || (!TYPE_OVERFLOW_SANITIZED (type)
1110 && may_negate_without_overflow_p (@1)))
1111 (plus (bit_not @0) { const_unop (NEGATE_EXPR, type, @1); })))
1112
1113 #if GIMPLE
1114 /* ~X + Y -> (Y - X) - 1. */
1115 (simplify
1116 (plus:c (bit_not @0) @1)
1117 (if (ANY_INTEGRAL_TYPE_P (type)
1118 && TYPE_OVERFLOW_WRAPS (type)
1119 /* -1 - X is folded to ~X, so we'd recurse endlessly. */
1120 && !integer_all_onesp (@1))
1121 (plus (minus @1 @0) { build_minus_one_cst (type); })
1122 (if (INTEGRAL_TYPE_P (type)
1123 && TREE_CODE (@1) == INTEGER_CST
1124 && wi::to_wide (@1) != wi::min_value (TYPE_PRECISION (type),
1125 SIGNED))
1126 (minus (plus @1 { build_minus_one_cst (type); }) @0))))
1127
1128 /* ~(X >> Y) -> ~X >> Y if ~X can be simplified. */
1129 (simplify
1130 (bit_not (rshift:s @0 @1))
1131 (if (!TYPE_UNSIGNED (TREE_TYPE (@0)))
1132 (rshift (bit_not! @0) @1)
1133 /* For logical right shifts, this is possible only if @0 doesn't
1134 have MSB set and the logical right shift is changed into
1135 arithmetic shift. */
1136 (if (!wi::neg_p (tree_nonzero_bits (@0)))
1137 (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
1138 (convert (rshift (bit_not! (convert:stype @0)) @1))))))
1139 #endif
1140
1141 /* x + (x & 1) -> (x + 1) & ~1 */
1142 (simplify
1143 (plus:c @0 (bit_and:s @0 integer_onep@1))
1144 (bit_and (plus @0 @1) (bit_not @1)))
1145
1146 /* x & ~(x & y) -> x & ~y */
1147 /* x | ~(x | y) -> x | ~y */
1148 (for bitop (bit_and bit_ior)
1149 (simplify
1150 (bitop:c @0 (bit_not (bitop:cs @0 @1)))
1151 (bitop @0 (bit_not @1))))
1152
1153 /* (~x & y) | ~(x | y) -> ~x */
1154 (simplify
1155 (bit_ior:c (bit_and:c (bit_not@2 @0) @1) (bit_not (bit_ior:c @0 @1)))
1156 @2)
1157
1158 /* (x | y) ^ (x | ~y) -> ~x */
1159 (simplify
1160 (bit_xor:c (bit_ior:c @0 @1) (bit_ior:c @0 (bit_not @1)))
1161 (bit_not @0))
1162
1163 /* (x & y) | ~(x | y) -> ~(x ^ y) */
1164 (simplify
1165 (bit_ior:c (bit_and:s @0 @1) (bit_not:s (bit_ior:s @0 @1)))
1166 (bit_not (bit_xor @0 @1)))
1167
1168 /* (~x | y) ^ (x ^ y) -> x | ~y */
1169 (simplify
1170 (bit_xor:c (bit_ior:cs (bit_not @0) @1) (bit_xor:s @0 @1))
1171 (bit_ior @0 (bit_not @1)))
1172
1173 /* (x ^ y) | ~(x | y) -> ~(x & y) */
1174 (simplify
1175 (bit_ior:c (bit_xor:s @0 @1) (bit_not:s (bit_ior:s @0 @1)))
1176 (bit_not (bit_and @0 @1)))
1177
1178 /* (x | y) & ~x -> y & ~x */
1179 /* (x & y) | ~x -> y | ~x */
1180 (for bitop (bit_and bit_ior)
1181 rbitop (bit_ior bit_and)
1182 (simplify
1183 (bitop:c (rbitop:c @0 @1) (bit_not@2 @0))
1184 (bitop @1 @2)))
1185
1186 /* (x & y) ^ (x | y) -> x ^ y */
1187 (simplify
1188 (bit_xor:c (bit_and @0 @1) (bit_ior @0 @1))
1189 (bit_xor @0 @1))
1190
1191 /* (x ^ y) ^ (x | y) -> x & y */
1192 (simplify
1193 (bit_xor:c (bit_xor @0 @1) (bit_ior @0 @1))
1194 (bit_and @0 @1))
1195
1196 /* (x & y) + (x ^ y) -> x | y */
1197 /* (x & y) | (x ^ y) -> x | y */
1198 /* (x & y) ^ (x ^ y) -> x | y */
1199 (for op (plus bit_ior bit_xor)
1200 (simplify
1201 (op:c (bit_and @0 @1) (bit_xor @0 @1))
1202 (bit_ior @0 @1)))
1203
1204 /* (x & y) + (x | y) -> x + y */
1205 (simplify
1206 (plus:c (bit_and @0 @1) (bit_ior @0 @1))
1207 (plus @0 @1))
1208
1209 /* (x + y) - (x | y) -> x & y */
1210 (simplify
1211 (minus (plus @0 @1) (bit_ior @0 @1))
1212 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1213 && !TYPE_SATURATING (type))
1214 (bit_and @0 @1)))
1215
1216 /* (x + y) - (x & y) -> x | y */
1217 (simplify
1218 (minus (plus @0 @1) (bit_and @0 @1))
1219 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1220 && !TYPE_SATURATING (type))
1221 (bit_ior @0 @1)))
1222
1223 /* (x | y) - y -> (x & ~y) */
1224 (simplify
1225 (minus (bit_ior:cs @0 @1) @1)
1226 (bit_and @0 (bit_not @1)))
1227
1228 /* (x | y) - (x ^ y) -> x & y */
1229 (simplify
1230 (minus (bit_ior @0 @1) (bit_xor @0 @1))
1231 (bit_and @0 @1))
1232
1233 /* (x | y) - (x & y) -> x ^ y */
1234 (simplify
1235 (minus (bit_ior @0 @1) (bit_and @0 @1))
1236 (bit_xor @0 @1))
1237
1238 /* (x | y) & ~(x & y) -> x ^ y */
1239 (simplify
1240 (bit_and:c (bit_ior @0 @1) (bit_not (bit_and @0 @1)))
1241 (bit_xor @0 @1))
1242
1243 /* (x | y) & (~x ^ y) -> x & y */
1244 (simplify
1245 (bit_and:c (bit_ior:c @0 @1) (bit_xor:c @1 (bit_not @0)))
1246 (bit_and @0 @1))
1247
1248 /* (~x | y) & (x | ~y) -> ~(x ^ y) */
1249 (simplify
1250 (bit_and (bit_ior:cs (bit_not @0) @1) (bit_ior:cs @0 (bit_not @1)))
1251 (bit_not (bit_xor @0 @1)))
1252
1253 /* (~x | y) ^ (x | ~y) -> x ^ y */
1254 (simplify
1255 (bit_xor (bit_ior:c (bit_not @0) @1) (bit_ior:c @0 (bit_not @1)))
1256 (bit_xor @0 @1))
1257
1258 /* ((x & y) - (x | y)) - 1 -> ~(x ^ y) */
1259 (simplify
1260 (plus (nop_convert1? (minus@2 (nop_convert2? (bit_and:c @0 @1))
1261 (nop_convert2? (bit_ior @0 @1))))
1262 integer_all_onesp)
1263 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1264 && !TYPE_SATURATING (type) && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2))
1265 && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@2))
1266 && !TYPE_SATURATING (TREE_TYPE (@2)))
1267 (bit_not (convert (bit_xor @0 @1)))))
1268 (simplify
1269 (minus (nop_convert1? (plus@2 (nop_convert2? (bit_and:c @0 @1))
1270 integer_all_onesp))
1271 (nop_convert3? (bit_ior @0 @1)))
1272 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1273 && !TYPE_SATURATING (type) && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2))
1274 && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@2))
1275 && !TYPE_SATURATING (TREE_TYPE (@2)))
1276 (bit_not (convert (bit_xor @0 @1)))))
1277 (simplify
1278 (minus (nop_convert1? (bit_and @0 @1))
1279 (nop_convert2? (plus@2 (nop_convert3? (bit_ior:c @0 @1))
1280 integer_onep)))
1281 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1282 && !TYPE_SATURATING (type) && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2))
1283 && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@2))
1284 && !TYPE_SATURATING (TREE_TYPE (@2)))
1285 (bit_not (convert (bit_xor @0 @1)))))
1286
1287 /* ~x & ~y -> ~(x | y)
1288 ~x | ~y -> ~(x & y) */
1289 (for op (bit_and bit_ior)
1290 rop (bit_ior bit_and)
1291 (simplify
1292 (op (convert1? (bit_not @0)) (convert2? (bit_not @1)))
1293 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1294 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
1295 (bit_not (rop (convert @0) (convert @1))))))
1296
1297 /* If we are XORing or adding two BIT_AND_EXPR's, both of which are and'ing
1298 with a constant, and the two constants have no bits in common,
1299 we should treat this as a BIT_IOR_EXPR since this may produce more
1300 simplifications. */
1301 (for op (bit_xor plus)
1302 (simplify
1303 (op (convert1? (bit_and@4 @0 INTEGER_CST@1))
1304 (convert2? (bit_and@5 @2 INTEGER_CST@3)))
1305 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
1306 && tree_nop_conversion_p (type, TREE_TYPE (@2))
1307 && (wi::to_wide (@1) & wi::to_wide (@3)) == 0)
1308 (bit_ior (convert @4) (convert @5)))))
1309
1310 /* (X | Y) ^ X -> Y & ~ X*/
1311 (simplify
1312 (bit_xor:c (convert1? (bit_ior:c @@0 @1)) (convert2? @0))
1313 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1314 (convert (bit_and @1 (bit_not @0)))))
1315
1316 /* Convert ~X ^ ~Y to X ^ Y. */
1317 (simplify
1318 (bit_xor (convert1? (bit_not @0)) (convert2? (bit_not @1)))
1319 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1320 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
1321 (bit_xor (convert @0) (convert @1))))
1322
1323 /* Convert ~X ^ C to X ^ ~C. */
1324 (simplify
1325 (bit_xor (convert? (bit_not @0)) INTEGER_CST@1)
1326 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1327 (bit_xor (convert @0) (bit_not @1))))
1328
1329 /* Fold (X & Y) ^ Y and (X ^ Y) & Y as ~X & Y. */
1330 (for opo (bit_and bit_xor)
1331 opi (bit_xor bit_and)
1332 (simplify
1333 (opo:c (opi:cs @0 @1) @1)
1334 (bit_and (bit_not @0) @1)))
1335
1336 /* Given a bit-wise operation CODE applied to ARG0 and ARG1, see if both
1337 operands are another bit-wise operation with a common input. If so,
1338 distribute the bit operations to save an operation and possibly two if
1339 constants are involved. For example, convert
1340 (A | B) & (A | C) into A | (B & C)
1341 Further simplification will occur if B and C are constants. */
1342 (for op (bit_and bit_ior bit_xor)
1343 rop (bit_ior bit_and bit_and)
1344 (simplify
1345 (op (convert? (rop:c @@0 @1)) (convert? (rop:c @0 @2)))
1346 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1347 && tree_nop_conversion_p (type, TREE_TYPE (@2)))
1348 (rop (convert @0) (op (convert @1) (convert @2))))))
1349
1350 /* Some simple reassociation for bit operations, also handled in reassoc. */
1351 /* (X & Y) & Y -> X & Y
1352 (X | Y) | Y -> X | Y */
1353 (for op (bit_and bit_ior)
1354 (simplify
1355 (op:c (convert1?@2 (op:c @0 @@1)) (convert2? @1))
1356 @2))
1357 /* (X ^ Y) ^ Y -> X */
1358 (simplify
1359 (bit_xor:c (convert1? (bit_xor:c @0 @@1)) (convert2? @1))
1360 (convert @0))
1361 /* (X & Y) & (X & Z) -> (X & Y) & Z
1362 (X | Y) | (X | Z) -> (X | Y) | Z */
1363 (for op (bit_and bit_ior)
1364 (simplify
1365 (op (convert1?@3 (op:c@4 @0 @1)) (convert2?@5 (op:c@6 @0 @2)))
1366 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1367 && tree_nop_conversion_p (type, TREE_TYPE (@2)))
1368 (if (single_use (@5) && single_use (@6))
1369 (op @3 (convert @2))
1370 (if (single_use (@3) && single_use (@4))
1371 (op (convert @1) @5))))))
1372 /* (X ^ Y) ^ (X ^ Z) -> Y ^ Z */
1373 (simplify
1374 (bit_xor (convert1? (bit_xor:c @0 @1)) (convert2? (bit_xor:c @0 @2)))
1375 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1376 && tree_nop_conversion_p (type, TREE_TYPE (@2)))
1377 (bit_xor (convert @1) (convert @2))))
1378
1379 /* Convert abs (abs (X)) into abs (X).
1380 also absu (absu (X)) into absu (X). */
1381 (simplify
1382 (abs (abs@1 @0))
1383 @1)
1384
1385 (simplify
1386 (absu (convert@2 (absu@1 @0)))
1387 (if (tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@1)))
1388 @1))
1389
1390 /* Convert abs[u] (-X) -> abs[u] (X). */
1391 (simplify
1392 (abs (negate @0))
1393 (abs @0))
1394
1395 (simplify
1396 (absu (negate @0))
1397 (absu @0))
1398
1399 /* Convert abs[u] (X) where X is nonnegative -> (X). */
1400 (simplify
1401 (abs tree_expr_nonnegative_p@0)
1402 @0)
1403
1404 (simplify
1405 (absu tree_expr_nonnegative_p@0)
1406 (convert @0))
1407
1408 /* Simplify (-(X < 0) | 1) * X into abs (X). */
1409 (simplify
1410 (mult:c (bit_ior (negate (convert? (lt @0 integer_zerop))) integer_onep) @0)
1411 (if (INTEGRAL_TYPE_P (type) && !TYPE_UNSIGNED (type))
1412 (abs @0)))
1413
1414 /* Similarly (-(X < 0) | 1U) * X into absu (X). */
1415 (simplify
1416 (mult:c (bit_ior (nop_convert (negate (convert? (lt @0 integer_zerop))))
1417 integer_onep) (nop_convert @0))
1418 (if (INTEGRAL_TYPE_P (type)
1419 && TYPE_UNSIGNED (type)
1420 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
1421 && !TYPE_UNSIGNED (TREE_TYPE (@0)))
1422 (absu @0)))
1423
1424 /* A few cases of fold-const.c negate_expr_p predicate. */
1425 (match negate_expr_p
1426 INTEGER_CST
1427 (if ((INTEGRAL_TYPE_P (type)
1428 && TYPE_UNSIGNED (type))
1429 || (!TYPE_OVERFLOW_SANITIZED (type)
1430 && may_negate_without_overflow_p (t)))))
1431 (match negate_expr_p
1432 FIXED_CST)
1433 (match negate_expr_p
1434 (negate @0)
1435 (if (!TYPE_OVERFLOW_SANITIZED (type))))
1436 (match negate_expr_p
1437 REAL_CST
1438 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (t)))))
1439 /* VECTOR_CST handling of non-wrapping types would recurse in unsupported
1440 ways. */
1441 (match negate_expr_p
1442 VECTOR_CST
1443 (if (FLOAT_TYPE_P (TREE_TYPE (type)) || TYPE_OVERFLOW_WRAPS (type))))
1444 (match negate_expr_p
1445 (minus @0 @1)
1446 (if ((ANY_INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_WRAPS (type))
1447 || (FLOAT_TYPE_P (type)
1448 && !HONOR_SIGN_DEPENDENT_ROUNDING (type)
1449 && !HONOR_SIGNED_ZEROS (type)))))
1450
1451 /* (-A) * (-B) -> A * B */
1452 (simplify
1453 (mult:c (convert1? (negate @0)) (convert2? negate_expr_p@1))
1454 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
1455 && tree_nop_conversion_p (type, TREE_TYPE (@1)))
1456 (mult (convert @0) (convert (negate @1)))))
1457
1458 /* -(A + B) -> (-B) - A. */
1459 (simplify
1460 (negate (plus:c @0 negate_expr_p@1))
1461 (if (!HONOR_SIGN_DEPENDENT_ROUNDING (type)
1462 && !HONOR_SIGNED_ZEROS (type))
1463 (minus (negate @1) @0)))
1464
1465 /* -(A - B) -> B - A. */
1466 (simplify
1467 (negate (minus @0 @1))
1468 (if ((ANY_INTEGRAL_TYPE_P (type) && !TYPE_OVERFLOW_SANITIZED (type))
1469 || (FLOAT_TYPE_P (type)
1470 && !HONOR_SIGN_DEPENDENT_ROUNDING (type)
1471 && !HONOR_SIGNED_ZEROS (type)))
1472 (minus @1 @0)))
1473 (simplify
1474 (negate (pointer_diff @0 @1))
1475 (if (TYPE_OVERFLOW_UNDEFINED (type))
1476 (pointer_diff @1 @0)))
1477
1478 /* A - B -> A + (-B) if B is easily negatable. */
1479 (simplify
1480 (minus @0 negate_expr_p@1)
1481 (if (!FIXED_POINT_TYPE_P (type))
1482 (plus @0 (negate @1))))
1483
1484 /* Try to fold (type) X op CST -> (type) (X op ((type-x) CST))
1485 when profitable.
1486 For bitwise binary operations apply operand conversions to the
1487 binary operation result instead of to the operands. This allows
1488 to combine successive conversions and bitwise binary operations.
1489 We combine the above two cases by using a conditional convert. */
1490 (for bitop (bit_and bit_ior bit_xor)
1491 (simplify
1492 (bitop (convert@2 @0) (convert?@3 @1))
1493 (if (((TREE_CODE (@1) == INTEGER_CST
1494 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
1495 && int_fits_type_p (@1, TREE_TYPE (@0)))
1496 || types_match (@0, @1))
1497 /* ??? This transform conflicts with fold-const.c doing
1498 Convert (T)(x & c) into (T)x & (T)c, if c is an integer
1499 constants (if x has signed type, the sign bit cannot be set
1500 in c). This folds extension into the BIT_AND_EXPR.
1501 Restrict it to GIMPLE to avoid endless recursions. */
1502 && (bitop != BIT_AND_EXPR || GIMPLE)
1503 && (/* That's a good idea if the conversion widens the operand, thus
1504 after hoisting the conversion the operation will be narrower. */
1505 TYPE_PRECISION (TREE_TYPE (@0)) < TYPE_PRECISION (type)
1506 /* It's also a good idea if the conversion is to a non-integer
1507 mode. */
1508 || GET_MODE_CLASS (TYPE_MODE (type)) != MODE_INT
1509 /* Or if the precision of TO is not the same as the precision
1510 of its mode. */
1511 || !type_has_mode_precision_p (type)
1512 /* In GIMPLE, getting rid of 2 conversions for one new results
1513 in smaller IL. */
1514 || (GIMPLE
1515 && TREE_CODE (@1) != INTEGER_CST
1516 && tree_nop_conversion_p (type, TREE_TYPE (@0))
1517 && single_use (@2)
1518 && single_use (@3))))
1519 (convert (bitop @0 (convert @1)))))
1520 /* In GIMPLE, getting rid of 2 conversions for one new results
1521 in smaller IL. */
1522 (simplify
1523 (convert (bitop:cs@2 (nop_convert:s @0) @1))
1524 (if (GIMPLE
1525 && TREE_CODE (@1) != INTEGER_CST
1526 && tree_nop_conversion_p (type, TREE_TYPE (@2))
1527 && types_match (type, @0))
1528 (bitop @0 (convert @1)))))
1529
1530 (for bitop (bit_and bit_ior)
1531 rbitop (bit_ior bit_and)
1532 /* (x | y) & x -> x */
1533 /* (x & y) | x -> x */
1534 (simplify
1535 (bitop:c (rbitop:c @0 @1) @0)
1536 @0)
1537 /* (~x | y) & x -> x & y */
1538 /* (~x & y) | x -> x | y */
1539 (simplify
1540 (bitop:c (rbitop:c (bit_not @0) @1) @0)
1541 (bitop @0 @1)))
1542
1543 /* ((x | y) & z) | x -> (z & y) | x */
1544 (simplify
1545 (bit_ior:c (bit_and:cs (bit_ior:cs @0 @1) @2) @0)
1546 (bit_ior (bit_and @2 @1) @0))
1547
1548 /* (x | CST1) & CST2 -> (x & CST2) | (CST1 & CST2) */
1549 (simplify
1550 (bit_and (bit_ior @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
1551 (bit_ior (bit_and @0 @2) (bit_and @1 @2)))
1552
1553 /* Combine successive equal operations with constants. */
1554 (for bitop (bit_and bit_ior bit_xor)
1555 (simplify
1556 (bitop (bitop @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
1557 (if (!CONSTANT_CLASS_P (@0))
1558 /* This is the canonical form regardless of whether (bitop @1 @2) can be
1559 folded to a constant. */
1560 (bitop @0 (bitop @1 @2))
1561 /* In this case we have three constants and (bitop @0 @1) doesn't fold
1562 to a constant. This can happen if @0 or @1 is a POLY_INT_CST and if
1563 the values involved are such that the operation can't be decided at
1564 compile time. Try folding one of @0 or @1 with @2 to see whether
1565 that combination can be decided at compile time.
1566
1567 Keep the existing form if both folds fail, to avoid endless
1568 oscillation. */
1569 (with { tree cst1 = const_binop (bitop, type, @0, @2); }
1570 (if (cst1)
1571 (bitop @1 { cst1; })
1572 (with { tree cst2 = const_binop (bitop, type, @1, @2); }
1573 (if (cst2)
1574 (bitop @0 { cst2; }))))))))
1575
1576 /* Try simple folding for X op !X, and X op X with the help
1577 of the truth_valued_p and logical_inverted_value predicates. */
1578 (match truth_valued_p
1579 @0
1580 (if (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1)))
1581 (for op (tcc_comparison truth_and truth_andif truth_or truth_orif truth_xor)
1582 (match truth_valued_p
1583 (op @0 @1)))
1584 (match truth_valued_p
1585 (truth_not @0))
1586
1587 (match (logical_inverted_value @0)
1588 (truth_not @0))
1589 (match (logical_inverted_value @0)
1590 (bit_not truth_valued_p@0))
1591 (match (logical_inverted_value @0)
1592 (eq @0 integer_zerop))
1593 (match (logical_inverted_value @0)
1594 (ne truth_valued_p@0 integer_truep))
1595 (match (logical_inverted_value @0)
1596 (bit_xor truth_valued_p@0 integer_truep))
1597
1598 /* X & !X -> 0. */
1599 (simplify
1600 (bit_and:c @0 (logical_inverted_value @0))
1601 { build_zero_cst (type); })
1602 /* X | !X and X ^ !X -> 1, , if X is truth-valued. */
1603 (for op (bit_ior bit_xor)
1604 (simplify
1605 (op:c truth_valued_p@0 (logical_inverted_value @0))
1606 { constant_boolean_node (true, type); }))
1607 /* X ==/!= !X is false/true. */
1608 (for op (eq ne)
1609 (simplify
1610 (op:c truth_valued_p@0 (logical_inverted_value @0))
1611 { constant_boolean_node (op == NE_EXPR ? true : false, type); }))
1612
1613 /* ~~x -> x */
1614 (simplify
1615 (bit_not (bit_not @0))
1616 @0)
1617
1618 /* Convert ~ (-A) to A - 1. */
1619 (simplify
1620 (bit_not (convert? (negate @0)))
1621 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1622 || !TYPE_UNSIGNED (TREE_TYPE (@0)))
1623 (convert (minus @0 { build_each_one_cst (TREE_TYPE (@0)); }))))
1624
1625 /* Convert - (~A) to A + 1. */
1626 (simplify
1627 (negate (nop_convert? (bit_not @0)))
1628 (plus (view_convert @0) { build_each_one_cst (type); }))
1629
1630 /* Convert ~ (A - 1) or ~ (A + -1) to -A. */
1631 (simplify
1632 (bit_not (convert? (minus @0 integer_each_onep)))
1633 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1634 || !TYPE_UNSIGNED (TREE_TYPE (@0)))
1635 (convert (negate @0))))
1636 (simplify
1637 (bit_not (convert? (plus @0 integer_all_onesp)))
1638 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1639 || !TYPE_UNSIGNED (TREE_TYPE (@0)))
1640 (convert (negate @0))))
1641
1642 /* Part of convert ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify. */
1643 (simplify
1644 (bit_not (convert? (bit_xor @0 INTEGER_CST@1)))
1645 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1646 (convert (bit_xor @0 (bit_not @1)))))
1647 (simplify
1648 (bit_not (convert? (bit_xor:c (bit_not @0) @1)))
1649 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1650 (convert (bit_xor @0 @1))))
1651
1652 /* Otherwise prefer ~(X ^ Y) to ~X ^ Y as more canonical. */
1653 (simplify
1654 (bit_xor:c (nop_convert?:s (bit_not:s @0)) @1)
1655 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1656 (bit_not (bit_xor (view_convert @0) @1))))
1657
1658 /* (x & ~m) | (y & m) -> ((x ^ y) & m) ^ x */
1659 (simplify
1660 (bit_ior:c (bit_and:cs @0 (bit_not @2)) (bit_and:cs @1 @2))
1661 (bit_xor (bit_and (bit_xor @0 @1) @2) @0))
1662
1663 /* Fold A - (A & B) into ~B & A. */
1664 (simplify
1665 (minus (convert1? @0) (convert2?:s (bit_and:cs @@0 @1)))
1666 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
1667 && tree_nop_conversion_p (type, TREE_TYPE (@1)))
1668 (convert (bit_and (bit_not @1) @0))))
1669
1670 /* (m1 CMP m2) * d -> (m1 CMP m2) ? d : 0 */
1671 (for cmp (gt lt ge le)
1672 (simplify
1673 (mult (convert (cmp @0 @1)) @2)
1674 (if (GIMPLE || !TREE_SIDE_EFFECTS (@2))
1675 (cond (cmp @0 @1) @2 { build_zero_cst (type); }))))
1676
1677 /* For integral types with undefined overflow and C != 0 fold
1678 x * C EQ/NE y * C into x EQ/NE y. */
1679 (for cmp (eq ne)
1680 (simplify
1681 (cmp (mult:c @0 @1) (mult:c @2 @1))
1682 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
1683 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1684 && tree_expr_nonzero_p (@1))
1685 (cmp @0 @2))))
1686
1687 /* For integral types with wrapping overflow and C odd fold
1688 x * C EQ/NE y * C into x EQ/NE y. */
1689 (for cmp (eq ne)
1690 (simplify
1691 (cmp (mult @0 INTEGER_CST@1) (mult @2 @1))
1692 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
1693 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))
1694 && (TREE_INT_CST_LOW (@1) & 1) != 0)
1695 (cmp @0 @2))))
1696
1697 /* For integral types with undefined overflow and C != 0 fold
1698 x * C RELOP y * C into:
1699
1700 x RELOP y for nonnegative C
1701 y RELOP x for negative C */
1702 (for cmp (lt gt le ge)
1703 (simplify
1704 (cmp (mult:c @0 @1) (mult:c @2 @1))
1705 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
1706 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1707 (if (tree_expr_nonnegative_p (@1) && tree_expr_nonzero_p (@1))
1708 (cmp @0 @2)
1709 (if (TREE_CODE (@1) == INTEGER_CST
1710 && wi::neg_p (wi::to_wide (@1), TYPE_SIGN (TREE_TYPE (@1))))
1711 (cmp @2 @0))))))
1712
1713 /* (X - 1U) <= INT_MAX-1U into (int) X > 0. */
1714 (for cmp (le gt)
1715 icmp (gt le)
1716 (simplify
1717 (cmp (plus @0 integer_minus_onep@1) INTEGER_CST@2)
1718 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1719 && TYPE_UNSIGNED (TREE_TYPE (@0))
1720 && TYPE_PRECISION (TREE_TYPE (@0)) > 1
1721 && (wi::to_wide (@2)
1722 == wi::max_value (TYPE_PRECISION (TREE_TYPE (@0)), SIGNED) - 1))
1723 (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
1724 (icmp (convert:stype @0) { build_int_cst (stype, 0); })))))
1725
1726 /* X / 4 < Y / 4 iff X < Y when the division is known to be exact. */
1727 (for cmp (simple_comparison)
1728 (simplify
1729 (cmp (convert?@3 (exact_div @0 INTEGER_CST@2)) (convert? (exact_div @1 @2)))
1730 (if (element_precision (@3) >= element_precision (@0)
1731 && types_match (@0, @1))
1732 (if (wi::lt_p (wi::to_wide (@2), 0, TYPE_SIGN (TREE_TYPE (@2))))
1733 (if (!TYPE_UNSIGNED (TREE_TYPE (@3)))
1734 (cmp @1 @0)
1735 (if (tree_expr_nonzero_p (@0) && tree_expr_nonzero_p (@1))
1736 (with
1737 {
1738 tree utype = unsigned_type_for (TREE_TYPE (@0));
1739 }
1740 (cmp (convert:utype @1) (convert:utype @0)))))
1741 (if (wi::gt_p (wi::to_wide (@2), 1, TYPE_SIGN (TREE_TYPE (@2))))
1742 (if (TYPE_UNSIGNED (TREE_TYPE (@0)) || !TYPE_UNSIGNED (TREE_TYPE (@3)))
1743 (cmp @0 @1)
1744 (with
1745 {
1746 tree utype = unsigned_type_for (TREE_TYPE (@0));
1747 }
1748 (cmp (convert:utype @0) (convert:utype @1)))))))))
1749
1750 /* X / C1 op C2 into a simple range test. */
1751 (for cmp (simple_comparison)
1752 (simplify
1753 (cmp (trunc_div:s @0 INTEGER_CST@1) INTEGER_CST@2)
1754 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1755 && integer_nonzerop (@1)
1756 && !TREE_OVERFLOW (@1)
1757 && !TREE_OVERFLOW (@2))
1758 (with { tree lo, hi; bool neg_overflow;
1759 enum tree_code code = fold_div_compare (cmp, @1, @2, &lo, &hi,
1760 &neg_overflow); }
1761 (switch
1762 (if (code == LT_EXPR || code == GE_EXPR)
1763 (if (TREE_OVERFLOW (lo))
1764 { build_int_cst (type, (code == LT_EXPR) ^ neg_overflow); }
1765 (if (code == LT_EXPR)
1766 (lt @0 { lo; })
1767 (ge @0 { lo; }))))
1768 (if (code == LE_EXPR || code == GT_EXPR)
1769 (if (TREE_OVERFLOW (hi))
1770 { build_int_cst (type, (code == LE_EXPR) ^ neg_overflow); }
1771 (if (code == LE_EXPR)
1772 (le @0 { hi; })
1773 (gt @0 { hi; }))))
1774 (if (!lo && !hi)
1775 { build_int_cst (type, code == NE_EXPR); })
1776 (if (code == EQ_EXPR && !hi)
1777 (ge @0 { lo; }))
1778 (if (code == EQ_EXPR && !lo)
1779 (le @0 { hi; }))
1780 (if (code == NE_EXPR && !hi)
1781 (lt @0 { lo; }))
1782 (if (code == NE_EXPR && !lo)
1783 (gt @0 { hi; }))
1784 (if (GENERIC)
1785 { build_range_check (UNKNOWN_LOCATION, type, @0, code == EQ_EXPR,
1786 lo, hi); })
1787 (with
1788 {
1789 tree etype = range_check_type (TREE_TYPE (@0));
1790 if (etype)
1791 {
1792 hi = fold_convert (etype, hi);
1793 lo = fold_convert (etype, lo);
1794 hi = const_binop (MINUS_EXPR, etype, hi, lo);
1795 }
1796 }
1797 (if (etype && hi && !TREE_OVERFLOW (hi))
1798 (if (code == EQ_EXPR)
1799 (le (minus (convert:etype @0) { lo; }) { hi; })
1800 (gt (minus (convert:etype @0) { lo; }) { hi; })))))))))
1801
1802 /* X + Z < Y + Z is the same as X < Y when there is no overflow. */
1803 (for op (lt le ge gt)
1804 (simplify
1805 (op (plus:c @0 @2) (plus:c @1 @2))
1806 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1807 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1808 (op @0 @1))))
1809 /* For equality and subtraction, this is also true with wrapping overflow. */
1810 (for op (eq ne minus)
1811 (simplify
1812 (op (plus:c @0 @2) (plus:c @1 @2))
1813 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1814 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1815 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
1816 (op @0 @1))))
1817
1818 /* X - Z < Y - Z is the same as X < Y when there is no overflow. */
1819 (for op (lt le ge gt)
1820 (simplify
1821 (op (minus @0 @2) (minus @1 @2))
1822 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1823 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1824 (op @0 @1))))
1825 /* For equality and subtraction, this is also true with wrapping overflow. */
1826 (for op (eq ne minus)
1827 (simplify
1828 (op (minus @0 @2) (minus @1 @2))
1829 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1830 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1831 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
1832 (op @0 @1))))
1833 /* And for pointers... */
1834 (for op (simple_comparison)
1835 (simplify
1836 (op (pointer_diff@3 @0 @2) (pointer_diff @1 @2))
1837 (if (!TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
1838 (op @0 @1))))
1839 (simplify
1840 (minus (pointer_diff@3 @0 @2) (pointer_diff @1 @2))
1841 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@3))
1842 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
1843 (pointer_diff @0 @1)))
1844
1845 /* Z - X < Z - Y is the same as Y < X when there is no overflow. */
1846 (for op (lt le ge gt)
1847 (simplify
1848 (op (minus @2 @0) (minus @2 @1))
1849 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1850 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1851 (op @1 @0))))
1852 /* For equality and subtraction, this is also true with wrapping overflow. */
1853 (for op (eq ne minus)
1854 (simplify
1855 (op (minus @2 @0) (minus @2 @1))
1856 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1857 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1858 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
1859 (op @1 @0))))
1860 /* And for pointers... */
1861 (for op (simple_comparison)
1862 (simplify
1863 (op (pointer_diff@3 @2 @0) (pointer_diff @2 @1))
1864 (if (!TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
1865 (op @1 @0))))
1866 (simplify
1867 (minus (pointer_diff@3 @2 @0) (pointer_diff @2 @1))
1868 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@3))
1869 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
1870 (pointer_diff @1 @0)))
1871
1872 /* X + Y < Y is the same as X < 0 when there is no overflow. */
1873 (for op (lt le gt ge)
1874 (simplify
1875 (op:c (plus:c@2 @0 @1) @1)
1876 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1877 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1878 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0))
1879 && (CONSTANT_CLASS_P (@0) || single_use (@2)))
1880 (op @0 { build_zero_cst (TREE_TYPE (@0)); }))))
1881 /* For equality, this is also true with wrapping overflow. */
1882 (for op (eq ne)
1883 (simplify
1884 (op:c (nop_convert?@3 (plus:c@2 @0 (convert1? @1))) (convert2? @1))
1885 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1886 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1887 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
1888 && (CONSTANT_CLASS_P (@0) || (single_use (@2) && single_use (@3)))
1889 && tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@2))
1890 && tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@1)))
1891 (op @0 { build_zero_cst (TREE_TYPE (@0)); })))
1892 (simplify
1893 (op:c (nop_convert?@3 (pointer_plus@2 (convert1? @0) @1)) (convert2? @0))
1894 (if (tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@0))
1895 && tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0))
1896 && (CONSTANT_CLASS_P (@1) || (single_use (@2) && single_use (@3))))
1897 (op @1 { build_zero_cst (TREE_TYPE (@1)); }))))
1898
1899 /* X - Y < X is the same as Y > 0 when there is no overflow.
1900 For equality, this is also true with wrapping overflow. */
1901 (for op (simple_comparison)
1902 (simplify
1903 (op:c @0 (minus@2 @0 @1))
1904 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1905 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1906 || ((op == EQ_EXPR || op == NE_EXPR)
1907 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
1908 && (CONSTANT_CLASS_P (@1) || single_use (@2)))
1909 (op @1 { build_zero_cst (TREE_TYPE (@1)); }))))
1910
1911 /* Transform:
1912 (X / Y) == 0 -> X < Y if X, Y are unsigned.
1913 (X / Y) != 0 -> X >= Y, if X, Y are unsigned. */
1914 (for cmp (eq ne)
1915 ocmp (lt ge)
1916 (simplify
1917 (cmp (trunc_div @0 @1) integer_zerop)
1918 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
1919 /* Complex ==/!= is allowed, but not </>=. */
1920 && TREE_CODE (TREE_TYPE (@0)) != COMPLEX_TYPE
1921 && (VECTOR_TYPE_P (type) || !VECTOR_TYPE_P (TREE_TYPE (@0))))
1922 (ocmp @0 @1))))
1923
1924 /* X == C - X can never be true if C is odd. */
1925 (for cmp (eq ne)
1926 (simplify
1927 (cmp:c (convert? @0) (convert1? (minus INTEGER_CST@1 (convert2? @0))))
1928 (if (TREE_INT_CST_LOW (@1) & 1)
1929 { constant_boolean_node (cmp == NE_EXPR, type); })))
1930
1931 /* Arguments on which one can call get_nonzero_bits to get the bits
1932 possibly set. */
1933 (match with_possible_nonzero_bits
1934 INTEGER_CST@0)
1935 (match with_possible_nonzero_bits
1936 SSA_NAME@0
1937 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))))
1938 /* Slightly extended version, do not make it recursive to keep it cheap. */
1939 (match (with_possible_nonzero_bits2 @0)
1940 with_possible_nonzero_bits@0)
1941 (match (with_possible_nonzero_bits2 @0)
1942 (bit_and:c with_possible_nonzero_bits@0 @2))
1943
1944 /* Same for bits that are known to be set, but we do not have
1945 an equivalent to get_nonzero_bits yet. */
1946 (match (with_certain_nonzero_bits2 @0)
1947 INTEGER_CST@0)
1948 (match (with_certain_nonzero_bits2 @0)
1949 (bit_ior @1 INTEGER_CST@0))
1950
1951 /* X == C (or X & Z == Y | C) is impossible if ~nonzero(X) & C != 0. */
1952 (for cmp (eq ne)
1953 (simplify
1954 (cmp:c (with_possible_nonzero_bits2 @0) (with_certain_nonzero_bits2 @1))
1955 (if (wi::bit_and_not (wi::to_wide (@1), get_nonzero_bits (@0)) != 0)
1956 { constant_boolean_node (cmp == NE_EXPR, type); })))
1957
1958 /* ((X inner_op C0) outer_op C1)
1959 With X being a tree where value_range has reasoned certain bits to always be
1960 zero throughout its computed value range,
1961 inner_op = {|,^}, outer_op = {|,^} and inner_op != outer_op
1962 where zero_mask has 1's for all bits that are sure to be 0 in
1963 and 0's otherwise.
1964 if (inner_op == '^') C0 &= ~C1;
1965 if ((C0 & ~zero_mask) == 0) then emit (X outer_op (C0 outer_op C1)
1966 if ((C1 & ~zero_mask) == 0) then emit (X inner_op (C0 outer_op C1)
1967 */
1968 (for inner_op (bit_ior bit_xor)
1969 outer_op (bit_xor bit_ior)
1970 (simplify
1971 (outer_op
1972 (inner_op:s @2 INTEGER_CST@0) INTEGER_CST@1)
1973 (with
1974 {
1975 bool fail = false;
1976 wide_int zero_mask_not;
1977 wide_int C0;
1978 wide_int cst_emit;
1979
1980 if (TREE_CODE (@2) == SSA_NAME)
1981 zero_mask_not = get_nonzero_bits (@2);
1982 else
1983 fail = true;
1984
1985 if (inner_op == BIT_XOR_EXPR)
1986 {
1987 C0 = wi::bit_and_not (wi::to_wide (@0), wi::to_wide (@1));
1988 cst_emit = C0 | wi::to_wide (@1);
1989 }
1990 else
1991 {
1992 C0 = wi::to_wide (@0);
1993 cst_emit = C0 ^ wi::to_wide (@1);
1994 }
1995 }
1996 (if (!fail && (C0 & zero_mask_not) == 0)
1997 (outer_op @2 { wide_int_to_tree (type, cst_emit); })
1998 (if (!fail && (wi::to_wide (@1) & zero_mask_not) == 0)
1999 (inner_op @2 { wide_int_to_tree (type, cst_emit); }))))))
2000
2001 /* Associate (p +p off1) +p off2 as (p +p (off1 + off2)). */
2002 (simplify
2003 (pointer_plus (pointer_plus:s @0 @1) @3)
2004 (pointer_plus @0 (plus @1 @3)))
2005
2006 /* Pattern match
2007 tem1 = (long) ptr1;
2008 tem2 = (long) ptr2;
2009 tem3 = tem2 - tem1;
2010 tem4 = (unsigned long) tem3;
2011 tem5 = ptr1 + tem4;
2012 and produce
2013 tem5 = ptr2; */
2014 (simplify
2015 (pointer_plus @0 (convert?@2 (minus@3 (convert @1) (convert @0))))
2016 /* Conditionally look through a sign-changing conversion. */
2017 (if (TYPE_PRECISION (TREE_TYPE (@2)) == TYPE_PRECISION (TREE_TYPE (@3))
2018 && ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@1)))
2019 || (GENERIC && type == TREE_TYPE (@1))))
2020 @1))
2021 (simplify
2022 (pointer_plus @0 (convert?@2 (pointer_diff@3 @1 @@0)))
2023 (if (TYPE_PRECISION (TREE_TYPE (@2)) >= TYPE_PRECISION (TREE_TYPE (@3)))
2024 (convert @1)))
2025
2026 /* Pattern match
2027 tem = (sizetype) ptr;
2028 tem = tem & algn;
2029 tem = -tem;
2030 ... = ptr p+ tem;
2031 and produce the simpler and easier to analyze with respect to alignment
2032 ... = ptr & ~algn; */
2033 (simplify
2034 (pointer_plus @0 (negate (bit_and (convert @0) INTEGER_CST@1)))
2035 (with { tree algn = wide_int_to_tree (TREE_TYPE (@0), ~wi::to_wide (@1)); }
2036 (bit_and @0 { algn; })))
2037
2038 /* Try folding difference of addresses. */
2039 (simplify
2040 (minus (convert ADDR_EXPR@0) (convert @1))
2041 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2042 (with { poly_int64 diff; }
2043 (if (ptr_difference_const (@0, @1, &diff))
2044 { build_int_cst_type (type, diff); }))))
2045 (simplify
2046 (minus (convert @0) (convert ADDR_EXPR@1))
2047 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2048 (with { poly_int64 diff; }
2049 (if (ptr_difference_const (@0, @1, &diff))
2050 { build_int_cst_type (type, diff); }))))
2051 (simplify
2052 (pointer_diff (convert?@2 ADDR_EXPR@0) (convert1?@3 @1))
2053 (if (tree_nop_conversion_p (TREE_TYPE(@2), TREE_TYPE (@0))
2054 && tree_nop_conversion_p (TREE_TYPE(@3), TREE_TYPE (@1)))
2055 (with { poly_int64 diff; }
2056 (if (ptr_difference_const (@0, @1, &diff))
2057 { build_int_cst_type (type, diff); }))))
2058 (simplify
2059 (pointer_diff (convert?@2 @0) (convert1?@3 ADDR_EXPR@1))
2060 (if (tree_nop_conversion_p (TREE_TYPE(@2), TREE_TYPE (@0))
2061 && tree_nop_conversion_p (TREE_TYPE(@3), TREE_TYPE (@1)))
2062 (with { poly_int64 diff; }
2063 (if (ptr_difference_const (@0, @1, &diff))
2064 { build_int_cst_type (type, diff); }))))
2065
2066 /* (&a+b) - (&a[1] + c) -> sizeof(a[0]) + (b - c) */
2067 (simplify
2068 (pointer_diff (pointer_plus ADDR_EXPR@0 @1) (pointer_plus ADDR_EXPR@2 @3))
2069 (with { poly_int64 diff; }
2070 (if (ptr_difference_const (@0, @2, &diff))
2071 (plus { build_int_cst_type (type, diff); } (convert (minus @1 @3))))))
2072
2073 /* (&a+b) !=/== (&a[1] + c) -> sizeof(a[0]) + b !=/== c */
2074 (for neeq (ne eq)
2075 (simplify
2076 (neeq (pointer_plus ADDR_EXPR@0 @1) (pointer_plus ADDR_EXPR@2 @3))
2077 (with { poly_int64 diff; tree inner_type = TREE_TYPE (@1);}
2078 (if (ptr_difference_const (@0, @2, &diff))
2079 (neeq (plus { build_int_cst_type (inner_type, diff); } @1) @3)))))
2080
2081 /* Canonicalize (T *)(ptr - ptr-cst) to &MEM[ptr + -ptr-cst]. */
2082 (simplify
2083 (convert (pointer_diff @0 INTEGER_CST@1))
2084 (if (POINTER_TYPE_P (type))
2085 { build_fold_addr_expr_with_type
2086 (build2 (MEM_REF, char_type_node, @0,
2087 wide_int_to_tree (ptr_type_node, wi::neg (wi::to_wide (@1)))),
2088 type); }))
2089
2090 /* If arg0 is derived from the address of an object or function, we may
2091 be able to fold this expression using the object or function's
2092 alignment. */
2093 (simplify
2094 (bit_and (convert? @0) INTEGER_CST@1)
2095 (if (POINTER_TYPE_P (TREE_TYPE (@0))
2096 && tree_nop_conversion_p (type, TREE_TYPE (@0)))
2097 (with
2098 {
2099 unsigned int align;
2100 unsigned HOST_WIDE_INT bitpos;
2101 get_pointer_alignment_1 (@0, &align, &bitpos);
2102 }
2103 (if (wi::ltu_p (wi::to_wide (@1), align / BITS_PER_UNIT))
2104 { wide_int_to_tree (type, (wi::to_wide (@1)
2105 & (bitpos / BITS_PER_UNIT))); }))))
2106
2107 (match min_value
2108 INTEGER_CST
2109 (if (INTEGRAL_TYPE_P (type)
2110 && wi::eq_p (wi::to_wide (t), wi::min_value (type)))))
2111
2112 (match max_value
2113 INTEGER_CST
2114 (if (INTEGRAL_TYPE_P (type)
2115 && wi::eq_p (wi::to_wide (t), wi::max_value (type)))))
2116
2117 /* x > y && x != XXX_MIN --> x > y
2118 x > y && x == XXX_MIN --> false . */
2119 (for eqne (eq ne)
2120 (simplify
2121 (bit_and:c (gt:c@2 @0 @1) (eqne @0 min_value))
2122 (switch
2123 (if (eqne == EQ_EXPR)
2124 { constant_boolean_node (false, type); })
2125 (if (eqne == NE_EXPR)
2126 @2)
2127 )))
2128
2129 /* x < y && x != XXX_MAX --> x < y
2130 x < y && x == XXX_MAX --> false. */
2131 (for eqne (eq ne)
2132 (simplify
2133 (bit_and:c (lt:c@2 @0 @1) (eqne @0 max_value))
2134 (switch
2135 (if (eqne == EQ_EXPR)
2136 { constant_boolean_node (false, type); })
2137 (if (eqne == NE_EXPR)
2138 @2)
2139 )))
2140
2141 /* x <= y && x == XXX_MIN --> x == XXX_MIN. */
2142 (simplify
2143 (bit_and:c (le:c @0 @1) (eq@2 @0 min_value))
2144 @2)
2145
2146 /* x >= y && x == XXX_MAX --> x == XXX_MAX. */
2147 (simplify
2148 (bit_and:c (ge:c @0 @1) (eq@2 @0 max_value))
2149 @2)
2150
2151 /* x > y || x != XXX_MIN --> x != XXX_MIN. */
2152 (simplify
2153 (bit_ior:c (gt:c @0 @1) (ne@2 @0 min_value))
2154 @2)
2155
2156 /* x <= y || x != XXX_MIN --> true. */
2157 (simplify
2158 (bit_ior:c (le:c @0 @1) (ne @0 min_value))
2159 { constant_boolean_node (true, type); })
2160
2161 /* x <= y || x == XXX_MIN --> x <= y. */
2162 (simplify
2163 (bit_ior:c (le:c@2 @0 @1) (eq @0 min_value))
2164 @2)
2165
2166 /* x < y || x != XXX_MAX --> x != XXX_MAX. */
2167 (simplify
2168 (bit_ior:c (lt:c @0 @1) (ne@2 @0 max_value))
2169 @2)
2170
2171 /* x >= y || x != XXX_MAX --> true
2172 x >= y || x == XXX_MAX --> x >= y. */
2173 (for eqne (eq ne)
2174 (simplify
2175 (bit_ior:c (ge:c@2 @0 @1) (eqne @0 max_value))
2176 (switch
2177 (if (eqne == EQ_EXPR)
2178 @2)
2179 (if (eqne == NE_EXPR)
2180 { constant_boolean_node (true, type); }))))
2181
2182 /* y == XXX_MIN || x < y --> x <= y - 1 */
2183 (simplify
2184 (bit_ior:c (eq:s @1 min_value) (lt:s @0 @1))
2185 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2186 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
2187 (le @0 (minus @1 { build_int_cst (TREE_TYPE (@1), 1); }))))
2188
2189 /* y != XXX_MIN && x >= y --> x > y - 1 */
2190 (simplify
2191 (bit_and:c (ne:s @1 min_value) (ge:s @0 @1))
2192 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2193 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
2194 (gt @0 (minus @1 { build_int_cst (TREE_TYPE (@1), 1); }))))
2195
2196 /* Convert (X == CST1) && (X OP2 CST2) to a known value
2197 based on CST1 OP2 CST2. Similarly for (X != CST1). */
2198
2199 (for code1 (eq ne)
2200 (for code2 (eq ne lt gt le ge)
2201 (simplify
2202 (bit_and:c (code1@3 @0 INTEGER_CST@1) (code2@4 @0 INTEGER_CST@2))
2203 (with
2204 {
2205 int cmp = tree_int_cst_compare (@1, @2);
2206 bool val;
2207 switch (code2)
2208 {
2209 case EQ_EXPR: val = (cmp == 0); break;
2210 case NE_EXPR: val = (cmp != 0); break;
2211 case LT_EXPR: val = (cmp < 0); break;
2212 case GT_EXPR: val = (cmp > 0); break;
2213 case LE_EXPR: val = (cmp <= 0); break;
2214 case GE_EXPR: val = (cmp >= 0); break;
2215 default: gcc_unreachable ();
2216 }
2217 }
2218 (switch
2219 (if (code1 == EQ_EXPR && val) @3)
2220 (if (code1 == EQ_EXPR && !val) { constant_boolean_node (false, type); })
2221 (if (code1 == NE_EXPR && !val) @4))))))
2222
2223 /* Convert (X OP1 CST1) && (X OP2 CST2). */
2224
2225 (for code1 (lt le gt ge)
2226 (for code2 (lt le gt ge)
2227 (simplify
2228 (bit_and (code1:c@3 @0 INTEGER_CST@1) (code2:c@4 @0 INTEGER_CST@2))
2229 (with
2230 {
2231 int cmp = tree_int_cst_compare (@1, @2);
2232 }
2233 (switch
2234 /* Choose the more restrictive of two < or <= comparisons. */
2235 (if ((code1 == LT_EXPR || code1 == LE_EXPR)
2236 && (code2 == LT_EXPR || code2 == LE_EXPR))
2237 (if ((cmp < 0) || (cmp == 0 && code1 == LT_EXPR))
2238 @3
2239 @4))
2240 /* Likewise chose the more restrictive of two > or >= comparisons. */
2241 (if ((code1 == GT_EXPR || code1 == GE_EXPR)
2242 && (code2 == GT_EXPR || code2 == GE_EXPR))
2243 (if ((cmp > 0) || (cmp == 0 && code1 == GT_EXPR))
2244 @3
2245 @4))
2246 /* Check for singleton ranges. */
2247 (if (cmp == 0
2248 && ((code1 == LE_EXPR && code2 == GE_EXPR)
2249 || (code1 == GE_EXPR && code2 == LE_EXPR)))
2250 (eq @0 @1))
2251 /* Check for disjoint ranges. */
2252 (if (cmp <= 0
2253 && (code1 == LT_EXPR || code1 == LE_EXPR)
2254 && (code2 == GT_EXPR || code2 == GE_EXPR))
2255 { constant_boolean_node (false, type); })
2256 (if (cmp >= 0
2257 && (code1 == GT_EXPR || code1 == GE_EXPR)
2258 && (code2 == LT_EXPR || code2 == LE_EXPR))
2259 { constant_boolean_node (false, type); })
2260 )))))
2261
2262 /* Convert (X == CST1) || (X OP2 CST2) to a known value
2263 based on CST1 OP2 CST2. Similarly for (X != CST1). */
2264
2265 (for code1 (eq ne)
2266 (for code2 (eq ne lt gt le ge)
2267 (simplify
2268 (bit_ior:c (code1@3 @0 INTEGER_CST@1) (code2@4 @0 INTEGER_CST@2))
2269 (with
2270 {
2271 int cmp = tree_int_cst_compare (@1, @2);
2272 bool val;
2273 switch (code2)
2274 {
2275 case EQ_EXPR: val = (cmp == 0); break;
2276 case NE_EXPR: val = (cmp != 0); break;
2277 case LT_EXPR: val = (cmp < 0); break;
2278 case GT_EXPR: val = (cmp > 0); break;
2279 case LE_EXPR: val = (cmp <= 0); break;
2280 case GE_EXPR: val = (cmp >= 0); break;
2281 default: gcc_unreachable ();
2282 }
2283 }
2284 (switch
2285 (if (code1 == EQ_EXPR && val) @4)
2286 (if (code1 == NE_EXPR && val) { constant_boolean_node (true, type); })
2287 (if (code1 == NE_EXPR && !val) @3))))))
2288
2289 /* Convert (X OP1 CST1) || (X OP2 CST2). */
2290
2291 (for code1 (lt le gt ge)
2292 (for code2 (lt le gt ge)
2293 (simplify
2294 (bit_ior (code1@3 @0 INTEGER_CST@1) (code2@4 @0 INTEGER_CST@2))
2295 (with
2296 {
2297 int cmp = tree_int_cst_compare (@1, @2);
2298 }
2299 (switch
2300 /* Choose the more restrictive of two < or <= comparisons. */
2301 (if ((code1 == LT_EXPR || code1 == LE_EXPR)
2302 && (code2 == LT_EXPR || code2 == LE_EXPR))
2303 (if ((cmp < 0) || (cmp == 0 && code1 == LT_EXPR))
2304 @4
2305 @3))
2306 /* Likewise chose the more restrictive of two > or >= comparisons. */
2307 (if ((code1 == GT_EXPR || code1 == GE_EXPR)
2308 && (code2 == GT_EXPR || code2 == GE_EXPR))
2309 (if ((cmp > 0) || (cmp == 0 && code1 == GT_EXPR))
2310 @4
2311 @3))
2312 /* Check for singleton ranges. */
2313 (if (cmp == 0
2314 && ((code1 == LT_EXPR && code2 == GT_EXPR)
2315 || (code1 == GT_EXPR && code2 == LT_EXPR)))
2316 (ne @0 @2))
2317 /* Check for disjoint ranges. */
2318 (if (cmp >= 0
2319 && (code1 == LT_EXPR || code1 == LE_EXPR)
2320 && (code2 == GT_EXPR || code2 == GE_EXPR))
2321 { constant_boolean_node (true, type); })
2322 (if (cmp <= 0
2323 && (code1 == GT_EXPR || code1 == GE_EXPR)
2324 && (code2 == LT_EXPR || code2 == LE_EXPR))
2325 { constant_boolean_node (true, type); })
2326 )))))
2327
2328 /* We can't reassociate at all for saturating types. */
2329 (if (!TYPE_SATURATING (type))
2330
2331 /* Contract negates. */
2332 /* A + (-B) -> A - B */
2333 (simplify
2334 (plus:c @0 (convert? (negate @1)))
2335 /* Apply STRIP_NOPS on the negate. */
2336 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
2337 && !TYPE_OVERFLOW_SANITIZED (type))
2338 (with
2339 {
2340 tree t1 = type;
2341 if (INTEGRAL_TYPE_P (type)
2342 && TYPE_OVERFLOW_WRAPS (type) != TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
2343 t1 = TYPE_OVERFLOW_WRAPS (type) ? type : TREE_TYPE (@1);
2344 }
2345 (convert (minus (convert:t1 @0) (convert:t1 @1))))))
2346 /* A - (-B) -> A + B */
2347 (simplify
2348 (minus @0 (convert? (negate @1)))
2349 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
2350 && !TYPE_OVERFLOW_SANITIZED (type))
2351 (with
2352 {
2353 tree t1 = type;
2354 if (INTEGRAL_TYPE_P (type)
2355 && TYPE_OVERFLOW_WRAPS (type) != TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
2356 t1 = TYPE_OVERFLOW_WRAPS (type) ? type : TREE_TYPE (@1);
2357 }
2358 (convert (plus (convert:t1 @0) (convert:t1 @1))))))
2359 /* -(T)(-A) -> (T)A
2360 Sign-extension is ok except for INT_MIN, which thankfully cannot
2361 happen without overflow. */
2362 (simplify
2363 (negate (convert (negate @1)))
2364 (if (INTEGRAL_TYPE_P (type)
2365 && (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@1))
2366 || (!TYPE_UNSIGNED (TREE_TYPE (@1))
2367 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))))
2368 && !TYPE_OVERFLOW_SANITIZED (type)
2369 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@1)))
2370 (convert @1)))
2371 (simplify
2372 (negate (convert negate_expr_p@1))
2373 (if (SCALAR_FLOAT_TYPE_P (type)
2374 && ((DECIMAL_FLOAT_TYPE_P (type)
2375 == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1))
2376 && TYPE_PRECISION (type) >= TYPE_PRECISION (TREE_TYPE (@1)))
2377 || !HONOR_SIGN_DEPENDENT_ROUNDING (type)))
2378 (convert (negate @1))))
2379 (simplify
2380 (negate (nop_convert? (negate @1)))
2381 (if (!TYPE_OVERFLOW_SANITIZED (type)
2382 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@1)))
2383 (view_convert @1)))
2384
2385 /* We can't reassociate floating-point unless -fassociative-math
2386 or fixed-point plus or minus because of saturation to +-Inf. */
2387 (if ((!FLOAT_TYPE_P (type) || flag_associative_math)
2388 && !FIXED_POINT_TYPE_P (type))
2389
2390 /* Match patterns that allow contracting a plus-minus pair
2391 irrespective of overflow issues. */
2392 /* (A +- B) - A -> +- B */
2393 /* (A +- B) -+ B -> A */
2394 /* A - (A +- B) -> -+ B */
2395 /* A +- (B -+ A) -> +- B */
2396 (simplify
2397 (minus (nop_convert1? (plus:c (nop_convert2? @0) @1)) @0)
2398 (view_convert @1))
2399 (simplify
2400 (minus (nop_convert1? (minus (nop_convert2? @0) @1)) @0)
2401 (if (!ANY_INTEGRAL_TYPE_P (type)
2402 || TYPE_OVERFLOW_WRAPS (type))
2403 (negate (view_convert @1))
2404 (view_convert (negate @1))))
2405 (simplify
2406 (plus:c (nop_convert1? (minus @0 (nop_convert2? @1))) @1)
2407 (view_convert @0))
2408 (simplify
2409 (minus @0 (nop_convert1? (plus:c (nop_convert2? @0) @1)))
2410 (if (!ANY_INTEGRAL_TYPE_P (type)
2411 || TYPE_OVERFLOW_WRAPS (type))
2412 (negate (view_convert @1))
2413 (view_convert (negate @1))))
2414 (simplify
2415 (minus @0 (nop_convert1? (minus (nop_convert2? @0) @1)))
2416 (view_convert @1))
2417 /* (A +- B) + (C - A) -> C +- B */
2418 /* (A + B) - (A - C) -> B + C */
2419 /* More cases are handled with comparisons. */
2420 (simplify
2421 (plus:c (plus:c @0 @1) (minus @2 @0))
2422 (plus @2 @1))
2423 (simplify
2424 (plus:c (minus @0 @1) (minus @2 @0))
2425 (minus @2 @1))
2426 (simplify
2427 (plus:c (pointer_diff @0 @1) (pointer_diff @2 @0))
2428 (if (TYPE_OVERFLOW_UNDEFINED (type)
2429 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0)))
2430 (pointer_diff @2 @1)))
2431 (simplify
2432 (minus (plus:c @0 @1) (minus @0 @2))
2433 (plus @1 @2))
2434
2435 /* (A +- CST1) +- CST2 -> A + CST3
2436 Use view_convert because it is safe for vectors and equivalent for
2437 scalars. */
2438 (for outer_op (plus minus)
2439 (for inner_op (plus minus)
2440 neg_inner_op (minus plus)
2441 (simplify
2442 (outer_op (nop_convert? (inner_op @0 CONSTANT_CLASS_P@1))
2443 CONSTANT_CLASS_P@2)
2444 /* If one of the types wraps, use that one. */
2445 (if (!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_WRAPS (type))
2446 /* If all 3 captures are CONSTANT_CLASS_P, punt, as we might recurse
2447 forever if something doesn't simplify into a constant. */
2448 (if (!CONSTANT_CLASS_P (@0))
2449 (if (outer_op == PLUS_EXPR)
2450 (plus (view_convert @0) (inner_op @2 (view_convert @1)))
2451 (minus (view_convert @0) (neg_inner_op @2 (view_convert @1)))))
2452 (if (!ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2453 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
2454 (if (outer_op == PLUS_EXPR)
2455 (view_convert (plus @0 (inner_op (view_convert @2) @1)))
2456 (view_convert (minus @0 (neg_inner_op (view_convert @2) @1))))
2457 /* If the constant operation overflows we cannot do the transform
2458 directly as we would introduce undefined overflow, for example
2459 with (a - 1) + INT_MIN. */
2460 (if (types_match (type, @0))
2461 (with { tree cst = const_binop (outer_op == inner_op
2462 ? PLUS_EXPR : MINUS_EXPR,
2463 type, @1, @2); }
2464 (if (cst && !TREE_OVERFLOW (cst))
2465 (inner_op @0 { cst; } )
2466 /* X+INT_MAX+1 is X-INT_MIN. */
2467 (if (INTEGRAL_TYPE_P (type) && cst
2468 && wi::to_wide (cst) == wi::min_value (type))
2469 (neg_inner_op @0 { wide_int_to_tree (type, wi::to_wide (cst)); })
2470 /* Last resort, use some unsigned type. */
2471 (with { tree utype = unsigned_type_for (type); }
2472 (if (utype)
2473 (view_convert (inner_op
2474 (view_convert:utype @0)
2475 (view_convert:utype
2476 { drop_tree_overflow (cst); }))))))))))))))
2477
2478 /* (CST1 - A) +- CST2 -> CST3 - A */
2479 (for outer_op (plus minus)
2480 (simplify
2481 (outer_op (nop_convert? (minus CONSTANT_CLASS_P@1 @0)) CONSTANT_CLASS_P@2)
2482 /* If one of the types wraps, use that one. */
2483 (if (!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_WRAPS (type))
2484 /* If all 3 captures are CONSTANT_CLASS_P, punt, as we might recurse
2485 forever if something doesn't simplify into a constant. */
2486 (if (!CONSTANT_CLASS_P (@0))
2487 (minus (outer_op (view_convert @1) @2) (view_convert @0)))
2488 (if (!ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2489 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
2490 (view_convert (minus (outer_op @1 (view_convert @2)) @0))
2491 (if (types_match (type, @0))
2492 (with { tree cst = const_binop (outer_op, type, @1, @2); }
2493 (if (cst && !TREE_OVERFLOW (cst))
2494 (minus { cst; } @0))))))))
2495
2496 /* CST1 - (CST2 - A) -> CST3 + A
2497 Use view_convert because it is safe for vectors and equivalent for
2498 scalars. */
2499 (simplify
2500 (minus CONSTANT_CLASS_P@1 (nop_convert? (minus CONSTANT_CLASS_P@2 @0)))
2501 /* If one of the types wraps, use that one. */
2502 (if (!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_WRAPS (type))
2503 /* If all 3 captures are CONSTANT_CLASS_P, punt, as we might recurse
2504 forever if something doesn't simplify into a constant. */
2505 (if (!CONSTANT_CLASS_P (@0))
2506 (plus (view_convert @0) (minus @1 (view_convert @2))))
2507 (if (!ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2508 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
2509 (view_convert (plus @0 (minus (view_convert @1) @2)))
2510 (if (types_match (type, @0))
2511 (with { tree cst = const_binop (MINUS_EXPR, type, @1, @2); }
2512 (if (cst && !TREE_OVERFLOW (cst))
2513 (plus { cst; } @0)))))))
2514
2515 /* ((T)(A)) + CST -> (T)(A + CST) */
2516 #if GIMPLE
2517 (simplify
2518 (plus (convert:s SSA_NAME@0) INTEGER_CST@1)
2519 (if (TREE_CODE (TREE_TYPE (@0)) == INTEGER_TYPE
2520 && TREE_CODE (type) == INTEGER_TYPE
2521 && TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (@0))
2522 && int_fits_type_p (@1, TREE_TYPE (@0)))
2523 /* Perform binary operation inside the cast if the constant fits
2524 and (A + CST)'s range does not overflow. */
2525 (with
2526 {
2527 wi::overflow_type min_ovf = wi::OVF_OVERFLOW,
2528 max_ovf = wi::OVF_OVERFLOW;
2529 tree inner_type = TREE_TYPE (@0);
2530
2531 wide_int w1
2532 = wide_int::from (wi::to_wide (@1), TYPE_PRECISION (inner_type),
2533 TYPE_SIGN (inner_type));
2534
2535 value_range vr;
2536 if (get_global_range_query ()->range_of_expr (vr, @0)
2537 && vr.kind () == VR_RANGE)
2538 {
2539 wide_int wmin0 = vr.lower_bound ();
2540 wide_int wmax0 = vr.upper_bound ();
2541 wi::add (wmin0, w1, TYPE_SIGN (inner_type), &min_ovf);
2542 wi::add (wmax0, w1, TYPE_SIGN (inner_type), &max_ovf);
2543 }
2544 }
2545 (if (min_ovf == wi::OVF_NONE && max_ovf == wi::OVF_NONE)
2546 (convert (plus @0 { wide_int_to_tree (TREE_TYPE (@0), w1); } )))
2547 )))
2548 #endif
2549
2550 /* ((T)(A + CST1)) + CST2 -> (T)(A) + (T)CST1 + CST2 */
2551 #if GIMPLE
2552 (for op (plus minus)
2553 (simplify
2554 (plus (convert:s (op:s @0 INTEGER_CST@1)) INTEGER_CST@2)
2555 (if (TREE_CODE (TREE_TYPE (@0)) == INTEGER_TYPE
2556 && TREE_CODE (type) == INTEGER_TYPE
2557 && TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (@0))
2558 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2559 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0))
2560 && TYPE_OVERFLOW_WRAPS (type))
2561 (plus (convert @0) (op @2 (convert @1))))))
2562 #endif
2563
2564 /* (T)(A) +- (T)(B) -> (T)(A +- B) only when (A +- B) could be simplified
2565 to a simple value. */
2566 #if GIMPLE
2567 (for op (plus minus)
2568 (simplify
2569 (op (convert @0) (convert @1))
2570 (if (INTEGRAL_TYPE_P (type)
2571 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
2572 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))
2573 && types_match (TREE_TYPE (@0), TREE_TYPE (@1))
2574 && !TYPE_OVERFLOW_TRAPS (type)
2575 && !TYPE_OVERFLOW_SANITIZED (type))
2576 (convert (op! @0 @1)))))
2577 #endif
2578
2579 /* ~A + A -> -1 */
2580 (simplify
2581 (plus:c (bit_not @0) @0)
2582 (if (!TYPE_OVERFLOW_TRAPS (type))
2583 { build_all_ones_cst (type); }))
2584
2585 /* ~A + 1 -> -A */
2586 (simplify
2587 (plus (convert? (bit_not @0)) integer_each_onep)
2588 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2589 (negate (convert @0))))
2590
2591 /* -A - 1 -> ~A */
2592 (simplify
2593 (minus (convert? (negate @0)) integer_each_onep)
2594 (if (!TYPE_OVERFLOW_TRAPS (type)
2595 && tree_nop_conversion_p (type, TREE_TYPE (@0)))
2596 (bit_not (convert @0))))
2597
2598 /* -1 - A -> ~A */
2599 (simplify
2600 (minus integer_all_onesp @0)
2601 (bit_not @0))
2602
2603 /* (T)(P + A) - (T)P -> (T) A */
2604 (simplify
2605 (minus (convert (plus:c @@0 @1))
2606 (convert? @0))
2607 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
2608 /* For integer types, if A has a smaller type
2609 than T the result depends on the possible
2610 overflow in P + A.
2611 E.g. T=size_t, A=(unsigned)429497295, P>0.
2612 However, if an overflow in P + A would cause
2613 undefined behavior, we can assume that there
2614 is no overflow. */
2615 || (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2616 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))))
2617 (convert @1)))
2618 (simplify
2619 (minus (convert (pointer_plus @@0 @1))
2620 (convert @0))
2621 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
2622 /* For pointer types, if the conversion of A to the
2623 final type requires a sign- or zero-extension,
2624 then we have to punt - it is not defined which
2625 one is correct. */
2626 || (POINTER_TYPE_P (TREE_TYPE (@0))
2627 && TREE_CODE (@1) == INTEGER_CST
2628 && tree_int_cst_sign_bit (@1) == 0))
2629 (convert @1)))
2630 (simplify
2631 (pointer_diff (pointer_plus @@0 @1) @0)
2632 /* The second argument of pointer_plus must be interpreted as signed, and
2633 thus sign-extended if necessary. */
2634 (with { tree stype = signed_type_for (TREE_TYPE (@1)); }
2635 /* Use view_convert instead of convert here, as POINTER_PLUS_EXPR
2636 second arg is unsigned even when we need to consider it as signed,
2637 we don't want to diagnose overflow here. */
2638 (convert (view_convert:stype @1))))
2639
2640 /* (T)P - (T)(P + A) -> -(T) A */
2641 (simplify
2642 (minus (convert? @0)
2643 (convert (plus:c @@0 @1)))
2644 (if (INTEGRAL_TYPE_P (type)
2645 && TYPE_OVERFLOW_UNDEFINED (type)
2646 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
2647 (with { tree utype = unsigned_type_for (type); }
2648 (convert (negate (convert:utype @1))))
2649 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
2650 /* For integer types, if A has a smaller type
2651 than T the result depends on the possible
2652 overflow in P + A.
2653 E.g. T=size_t, A=(unsigned)429497295, P>0.
2654 However, if an overflow in P + A would cause
2655 undefined behavior, we can assume that there
2656 is no overflow. */
2657 || (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2658 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))))
2659 (negate (convert @1)))))
2660 (simplify
2661 (minus (convert @0)
2662 (convert (pointer_plus @@0 @1)))
2663 (if (INTEGRAL_TYPE_P (type)
2664 && TYPE_OVERFLOW_UNDEFINED (type)
2665 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
2666 (with { tree utype = unsigned_type_for (type); }
2667 (convert (negate (convert:utype @1))))
2668 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
2669 /* For pointer types, if the conversion of A to the
2670 final type requires a sign- or zero-extension,
2671 then we have to punt - it is not defined which
2672 one is correct. */
2673 || (POINTER_TYPE_P (TREE_TYPE (@0))
2674 && TREE_CODE (@1) == INTEGER_CST
2675 && tree_int_cst_sign_bit (@1) == 0))
2676 (negate (convert @1)))))
2677 (simplify
2678 (pointer_diff @0 (pointer_plus @@0 @1))
2679 /* The second argument of pointer_plus must be interpreted as signed, and
2680 thus sign-extended if necessary. */
2681 (with { tree stype = signed_type_for (TREE_TYPE (@1)); }
2682 /* Use view_convert instead of convert here, as POINTER_PLUS_EXPR
2683 second arg is unsigned even when we need to consider it as signed,
2684 we don't want to diagnose overflow here. */
2685 (negate (convert (view_convert:stype @1)))))
2686
2687 /* (T)(P + A) - (T)(P + B) -> (T)A - (T)B */
2688 (simplify
2689 (minus (convert (plus:c @@0 @1))
2690 (convert (plus:c @0 @2)))
2691 (if (INTEGRAL_TYPE_P (type)
2692 && TYPE_OVERFLOW_UNDEFINED (type)
2693 && element_precision (type) <= element_precision (TREE_TYPE (@1))
2694 && element_precision (type) <= element_precision (TREE_TYPE (@2)))
2695 (with { tree utype = unsigned_type_for (type); }
2696 (convert (minus (convert:utype @1) (convert:utype @2))))
2697 (if (((element_precision (type) <= element_precision (TREE_TYPE (@1)))
2698 == (element_precision (type) <= element_precision (TREE_TYPE (@2))))
2699 && (element_precision (type) <= element_precision (TREE_TYPE (@1))
2700 /* For integer types, if A has a smaller type
2701 than T the result depends on the possible
2702 overflow in P + A.
2703 E.g. T=size_t, A=(unsigned)429497295, P>0.
2704 However, if an overflow in P + A would cause
2705 undefined behavior, we can assume that there
2706 is no overflow. */
2707 || (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2708 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
2709 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))
2710 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@2)))))
2711 (minus (convert @1) (convert @2)))))
2712 (simplify
2713 (minus (convert (pointer_plus @@0 @1))
2714 (convert (pointer_plus @0 @2)))
2715 (if (INTEGRAL_TYPE_P (type)
2716 && TYPE_OVERFLOW_UNDEFINED (type)
2717 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
2718 (with { tree utype = unsigned_type_for (type); }
2719 (convert (minus (convert:utype @1) (convert:utype @2))))
2720 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
2721 /* For pointer types, if the conversion of A to the
2722 final type requires a sign- or zero-extension,
2723 then we have to punt - it is not defined which
2724 one is correct. */
2725 || (POINTER_TYPE_P (TREE_TYPE (@0))
2726 && TREE_CODE (@1) == INTEGER_CST
2727 && tree_int_cst_sign_bit (@1) == 0
2728 && TREE_CODE (@2) == INTEGER_CST
2729 && tree_int_cst_sign_bit (@2) == 0))
2730 (minus (convert @1) (convert @2)))))
2731 (simplify
2732 (pointer_diff (pointer_plus @0 @2) (pointer_plus @1 @2))
2733 (pointer_diff @0 @1))
2734 (simplify
2735 (pointer_diff (pointer_plus @@0 @1) (pointer_plus @0 @2))
2736 /* The second argument of pointer_plus must be interpreted as signed, and
2737 thus sign-extended if necessary. */
2738 (with { tree stype = signed_type_for (TREE_TYPE (@1)); }
2739 /* Use view_convert instead of convert here, as POINTER_PLUS_EXPR
2740 second arg is unsigned even when we need to consider it as signed,
2741 we don't want to diagnose overflow here. */
2742 (minus (convert (view_convert:stype @1))
2743 (convert (view_convert:stype @2)))))))
2744
2745 /* (A * C) +- (B * C) -> (A+-B) * C and (A * C) +- A -> A * (C+-1).
2746 Modeled after fold_plusminus_mult_expr. */
2747 (if (!TYPE_SATURATING (type)
2748 && (!FLOAT_TYPE_P (type) || flag_associative_math))
2749 (for plusminus (plus minus)
2750 (simplify
2751 (plusminus (mult:cs@3 @0 @1) (mult:cs@4 @0 @2))
2752 (if (!ANY_INTEGRAL_TYPE_P (type)
2753 || TYPE_OVERFLOW_WRAPS (type)
2754 || (INTEGRAL_TYPE_P (type)
2755 && tree_expr_nonzero_p (@0)
2756 && expr_not_equal_to (@0, wi::minus_one (TYPE_PRECISION (type)))))
2757 (if (single_use (@3) || single_use (@4))
2758 /* If @1 +- @2 is constant require a hard single-use on either
2759 original operand (but not on both). */
2760 (mult (plusminus @1 @2) @0)
2761 #if GIMPLE
2762 (mult! (plusminus @1 @2) @0)
2763 #endif
2764 )))
2765 /* We cannot generate constant 1 for fract. */
2766 (if (!ALL_FRACT_MODE_P (TYPE_MODE (type)))
2767 (simplify
2768 (plusminus @0 (mult:c@3 @0 @2))
2769 (if ((!ANY_INTEGRAL_TYPE_P (type)
2770 || TYPE_OVERFLOW_WRAPS (type)
2771 /* For @0 + @0*@2 this transformation would introduce UB
2772 (where there was none before) for @0 in [-1,0] and @2 max.
2773 For @0 - @0*@2 this transformation would introduce UB
2774 for @0 0 and @2 in [min,min+1] or @0 -1 and @2 min+1. */
2775 || (INTEGRAL_TYPE_P (type)
2776 && ((tree_expr_nonzero_p (@0)
2777 && expr_not_equal_to (@0,
2778 wi::minus_one (TYPE_PRECISION (type))))
2779 || (plusminus == PLUS_EXPR
2780 ? expr_not_equal_to (@2,
2781 wi::max_value (TYPE_PRECISION (type), SIGNED))
2782 /* Let's ignore the @0 -1 and @2 min case. */
2783 : (expr_not_equal_to (@2,
2784 wi::min_value (TYPE_PRECISION (type), SIGNED))
2785 && expr_not_equal_to (@2,
2786 wi::min_value (TYPE_PRECISION (type), SIGNED)
2787 + 1))))))
2788 && single_use (@3))
2789 (mult (plusminus { build_one_cst (type); } @2) @0)))
2790 (simplify
2791 (plusminus (mult:c@3 @0 @2) @0)
2792 (if ((!ANY_INTEGRAL_TYPE_P (type)
2793 || TYPE_OVERFLOW_WRAPS (type)
2794 /* For @0*@2 + @0 this transformation would introduce UB
2795 (where there was none before) for @0 in [-1,0] and @2 max.
2796 For @0*@2 - @0 this transformation would introduce UB
2797 for @0 0 and @2 min. */
2798 || (INTEGRAL_TYPE_P (type)
2799 && ((tree_expr_nonzero_p (@0)
2800 && (plusminus == MINUS_EXPR
2801 || expr_not_equal_to (@0,
2802 wi::minus_one (TYPE_PRECISION (type)))))
2803 || expr_not_equal_to (@2,
2804 (plusminus == PLUS_EXPR
2805 ? wi::max_value (TYPE_PRECISION (type), SIGNED)
2806 : wi::min_value (TYPE_PRECISION (type), SIGNED))))))
2807 && single_use (@3))
2808 (mult (plusminus @2 { build_one_cst (type); }) @0))))))
2809
2810 #if GIMPLE
2811 /* Canonicalize X + (X << C) into X * (1 + (1 << C)) and
2812 (X << C1) + (X << C2) into X * ((1 << C1) + (1 << C2)). */
2813 (simplify
2814 (plus:c @0 (lshift:s @0 INTEGER_CST@1))
2815 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2816 && tree_fits_uhwi_p (@1)
2817 && tree_to_uhwi (@1) < element_precision (type)
2818 && (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2819 || optab_handler (smul_optab,
2820 TYPE_MODE (type)) != CODE_FOR_nothing))
2821 (with { tree t = type;
2822 if (!TYPE_OVERFLOW_WRAPS (t)) t = unsigned_type_for (t);
2823 wide_int w = wi::set_bit_in_zero (tree_to_uhwi (@1),
2824 element_precision (type));
2825 w += 1;
2826 tree cst = wide_int_to_tree (VECTOR_TYPE_P (t) ? TREE_TYPE (t)
2827 : t, w);
2828 cst = build_uniform_cst (t, cst); }
2829 (convert (mult (convert:t @0) { cst; })))))
2830 (simplify
2831 (plus (lshift:s @0 INTEGER_CST@1) (lshift:s @0 INTEGER_CST@2))
2832 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2833 && tree_fits_uhwi_p (@1)
2834 && tree_to_uhwi (@1) < element_precision (type)
2835 && tree_fits_uhwi_p (@2)
2836 && tree_to_uhwi (@2) < element_precision (type)
2837 && (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2838 || optab_handler (smul_optab,
2839 TYPE_MODE (type)) != CODE_FOR_nothing))
2840 (with { tree t = type;
2841 if (!TYPE_OVERFLOW_WRAPS (t)) t = unsigned_type_for (t);
2842 unsigned int prec = element_precision (type);
2843 wide_int w = wi::set_bit_in_zero (tree_to_uhwi (@1), prec);
2844 w += wi::set_bit_in_zero (tree_to_uhwi (@2), prec);
2845 tree cst = wide_int_to_tree (VECTOR_TYPE_P (t) ? TREE_TYPE (t)
2846 : t, w);
2847 cst = build_uniform_cst (t, cst); }
2848 (convert (mult (convert:t @0) { cst; })))))
2849 #endif
2850
2851 /* Canonicalize (X*C1)|(X*C2) and (X*C1)^(X*C2) to (C1+C2)*X when
2852 tree_nonzero_bits allows IOR and XOR to be treated like PLUS.
2853 Likewise, handle (X<<C3) and X as legitimate variants of X*C. */
2854 (for op (bit_ior bit_xor)
2855 (simplify
2856 (op (mult:s@0 @1 INTEGER_CST@2)
2857 (mult:s@3 @1 INTEGER_CST@4))
2858 (if (INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_WRAPS (type)
2859 && (tree_nonzero_bits (@0) & tree_nonzero_bits (@3)) == 0)
2860 (mult @1
2861 { wide_int_to_tree (type, wi::to_wide (@2) + wi::to_wide (@4)); })))
2862 (simplify
2863 (op:c (mult:s@0 @1 INTEGER_CST@2)
2864 (lshift:s@3 @1 INTEGER_CST@4))
2865 (if (INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_WRAPS (type)
2866 && tree_int_cst_sgn (@4) > 0
2867 && (tree_nonzero_bits (@0) & tree_nonzero_bits (@3)) == 0)
2868 (with { wide_int wone = wi::one (TYPE_PRECISION (type));
2869 wide_int c = wi::add (wi::to_wide (@2),
2870 wi::lshift (wone, wi::to_wide (@4))); }
2871 (mult @1 { wide_int_to_tree (type, c); }))))
2872 (simplify
2873 (op:c (mult:s@0 @1 INTEGER_CST@2)
2874 @1)
2875 (if (INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_WRAPS (type)
2876 && (tree_nonzero_bits (@0) & tree_nonzero_bits (@1)) == 0)
2877 (mult @1
2878 { wide_int_to_tree (type,
2879 wi::add (wi::to_wide (@2), 1)); })))
2880 (simplify
2881 (op (lshift:s@0 @1 INTEGER_CST@2)
2882 (lshift:s@3 @1 INTEGER_CST@4))
2883 (if (INTEGRAL_TYPE_P (type)
2884 && tree_int_cst_sgn (@2) > 0
2885 && tree_int_cst_sgn (@4) > 0
2886 && (tree_nonzero_bits (@0) & tree_nonzero_bits (@3)) == 0)
2887 (with { tree t = type;
2888 if (!TYPE_OVERFLOW_WRAPS (t))
2889 t = unsigned_type_for (t);
2890 wide_int wone = wi::one (TYPE_PRECISION (t));
2891 wide_int c = wi::add (wi::lshift (wone, wi::to_wide (@2)),
2892 wi::lshift (wone, wi::to_wide (@4))); }
2893 (convert (mult:t (convert:t @1) { wide_int_to_tree (t,c); })))))
2894 (simplify
2895 (op:c (lshift:s@0 @1 INTEGER_CST@2)
2896 @1)
2897 (if (INTEGRAL_TYPE_P (type)
2898 && tree_int_cst_sgn (@2) > 0
2899 && (tree_nonzero_bits (@0) & tree_nonzero_bits (@1)) == 0)
2900 (with { tree t = type;
2901 if (!TYPE_OVERFLOW_WRAPS (t))
2902 t = unsigned_type_for (t);
2903 wide_int wone = wi::one (TYPE_PRECISION (t));
2904 wide_int c = wi::add (wi::lshift (wone, wi::to_wide (@2)), wone); }
2905 (convert (mult:t (convert:t @1) { wide_int_to_tree (t, c); }))))))
2906
2907 /* Simplifications of MIN_EXPR, MAX_EXPR, fmin() and fmax(). */
2908
2909 (for minmax (min max FMIN_ALL FMAX_ALL)
2910 (simplify
2911 (minmax @0 @0)
2912 @0))
2913 /* min(max(x,y),y) -> y. */
2914 (simplify
2915 (min:c (max:c @0 @1) @1)
2916 @1)
2917 /* max(min(x,y),y) -> y. */
2918 (simplify
2919 (max:c (min:c @0 @1) @1)
2920 @1)
2921 /* max(a,-a) -> abs(a). */
2922 (simplify
2923 (max:c @0 (negate @0))
2924 (if (TREE_CODE (type) != COMPLEX_TYPE
2925 && (! ANY_INTEGRAL_TYPE_P (type)
2926 || TYPE_OVERFLOW_UNDEFINED (type)))
2927 (abs @0)))
2928 /* min(a,-a) -> -abs(a). */
2929 (simplify
2930 (min:c @0 (negate @0))
2931 (if (TREE_CODE (type) != COMPLEX_TYPE
2932 && (! ANY_INTEGRAL_TYPE_P (type)
2933 || TYPE_OVERFLOW_UNDEFINED (type)))
2934 (negate (abs @0))))
2935 (simplify
2936 (min @0 @1)
2937 (switch
2938 (if (INTEGRAL_TYPE_P (type)
2939 && TYPE_MIN_VALUE (type)
2940 && operand_equal_p (@1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
2941 @1)
2942 (if (INTEGRAL_TYPE_P (type)
2943 && TYPE_MAX_VALUE (type)
2944 && operand_equal_p (@1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
2945 @0)))
2946 (simplify
2947 (max @0 @1)
2948 (switch
2949 (if (INTEGRAL_TYPE_P (type)
2950 && TYPE_MAX_VALUE (type)
2951 && operand_equal_p (@1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
2952 @1)
2953 (if (INTEGRAL_TYPE_P (type)
2954 && TYPE_MIN_VALUE (type)
2955 && operand_equal_p (@1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
2956 @0)))
2957
2958 /* max (a, a + CST) -> a + CST where CST is positive. */
2959 /* max (a, a + CST) -> a where CST is negative. */
2960 (simplify
2961 (max:c @0 (plus@2 @0 INTEGER_CST@1))
2962 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
2963 (if (tree_int_cst_sgn (@1) > 0)
2964 @2
2965 @0)))
2966
2967 /* min (a, a + CST) -> a where CST is positive. */
2968 /* min (a, a + CST) -> a + CST where CST is negative. */
2969 (simplify
2970 (min:c @0 (plus@2 @0 INTEGER_CST@1))
2971 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
2972 (if (tree_int_cst_sgn (@1) > 0)
2973 @0
2974 @2)))
2975
2976 /* (convert (minmax ((convert (x) c)))) -> minmax (x c) if x is promoted
2977 and the outer convert demotes the expression back to x's type. */
2978 (for minmax (min max)
2979 (simplify
2980 (convert (minmax@0 (convert @1) INTEGER_CST@2))
2981 (if (INTEGRAL_TYPE_P (type)
2982 && types_match (@1, type) && int_fits_type_p (@2, type)
2983 && TYPE_SIGN (TREE_TYPE (@0)) == TYPE_SIGN (type)
2984 && TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type))
2985 (minmax @1 (convert @2)))))
2986
2987 (for minmax (FMIN_ALL FMAX_ALL)
2988 /* If either argument is NaN, return the other one. Avoid the
2989 transformation if we get (and honor) a signalling NaN. */
2990 (simplify
2991 (minmax:c @0 REAL_CST@1)
2992 (if (real_isnan (TREE_REAL_CST_PTR (@1))
2993 && (!HONOR_SNANS (@1) || !TREE_REAL_CST (@1).signalling))
2994 @0)))
2995 /* Convert fmin/fmax to MIN_EXPR/MAX_EXPR. C99 requires these
2996 functions to return the numeric arg if the other one is NaN.
2997 MIN and MAX don't honor that, so only transform if -ffinite-math-only
2998 is set. C99 doesn't require -0.0 to be handled, so we don't have to
2999 worry about it either. */
3000 (if (flag_finite_math_only)
3001 (simplify
3002 (FMIN_ALL @0 @1)
3003 (min @0 @1))
3004 (simplify
3005 (FMAX_ALL @0 @1)
3006 (max @0 @1)))
3007 /* min (-A, -B) -> -max (A, B) */
3008 (for minmax (min max FMIN_ALL FMAX_ALL)
3009 maxmin (max min FMAX_ALL FMIN_ALL)
3010 (simplify
3011 (minmax (negate:s@2 @0) (negate:s@3 @1))
3012 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
3013 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3014 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
3015 (negate (maxmin @0 @1)))))
3016 /* MIN (~X, ~Y) -> ~MAX (X, Y)
3017 MAX (~X, ~Y) -> ~MIN (X, Y) */
3018 (for minmax (min max)
3019 maxmin (max min)
3020 (simplify
3021 (minmax (bit_not:s@2 @0) (bit_not:s@3 @1))
3022 (bit_not (maxmin @0 @1))))
3023
3024 /* MIN (X, Y) == X -> X <= Y */
3025 (for minmax (min min max max)
3026 cmp (eq ne eq ne )
3027 out (le gt ge lt )
3028 (simplify
3029 (cmp:c (minmax:c @0 @1) @0)
3030 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0)))
3031 (out @0 @1))))
3032 /* MIN (X, 5) == 0 -> X == 0
3033 MIN (X, 5) == 7 -> false */
3034 (for cmp (eq ne)
3035 (simplify
3036 (cmp (min @0 INTEGER_CST@1) INTEGER_CST@2)
3037 (if (wi::lt_p (wi::to_wide (@1), wi::to_wide (@2),
3038 TYPE_SIGN (TREE_TYPE (@0))))
3039 { constant_boolean_node (cmp == NE_EXPR, type); }
3040 (if (wi::gt_p (wi::to_wide (@1), wi::to_wide (@2),
3041 TYPE_SIGN (TREE_TYPE (@0))))
3042 (cmp @0 @2)))))
3043 (for cmp (eq ne)
3044 (simplify
3045 (cmp (max @0 INTEGER_CST@1) INTEGER_CST@2)
3046 (if (wi::gt_p (wi::to_wide (@1), wi::to_wide (@2),
3047 TYPE_SIGN (TREE_TYPE (@0))))
3048 { constant_boolean_node (cmp == NE_EXPR, type); }
3049 (if (wi::lt_p (wi::to_wide (@1), wi::to_wide (@2),
3050 TYPE_SIGN (TREE_TYPE (@0))))
3051 (cmp @0 @2)))))
3052 /* MIN (X, C1) < C2 -> X < C2 || C1 < C2 */
3053 (for minmax (min min max max min min max max )
3054 cmp (lt le gt ge gt ge lt le )
3055 comb (bit_ior bit_ior bit_ior bit_ior bit_and bit_and bit_and bit_and)
3056 (simplify
3057 (cmp (minmax @0 INTEGER_CST@1) INTEGER_CST@2)
3058 (comb (cmp @0 @2) (cmp @1 @2))))
3059
3060 /* X <= MAX(X, Y) -> true
3061 X > MAX(X, Y) -> false
3062 X >= MIN(X, Y) -> true
3063 X < MIN(X, Y) -> false */
3064 (for minmax (min min max max )
3065 cmp (ge lt le gt )
3066 (simplify
3067 (cmp @0 (minmax:c @0 @1))
3068 { constant_boolean_node (cmp == GE_EXPR || cmp == LE_EXPR, type); } ))
3069
3070 /* Undo fancy way of writing max/min or other ?: expressions,
3071 like a - ((a - b) & -(a < b)), in this case into (a < b) ? b : a.
3072 People normally use ?: and that is what we actually try to optimize. */
3073 (for cmp (simple_comparison)
3074 (simplify
3075 (minus @0 (bit_and:c (minus @0 @1)
3076 (convert? (negate@4 (convert? (cmp@5 @2 @3))))))
3077 (if (INTEGRAL_TYPE_P (type)
3078 && INTEGRAL_TYPE_P (TREE_TYPE (@4))
3079 && TREE_CODE (TREE_TYPE (@4)) != BOOLEAN_TYPE
3080 && INTEGRAL_TYPE_P (TREE_TYPE (@5))
3081 && (TYPE_PRECISION (TREE_TYPE (@4)) >= TYPE_PRECISION (type)
3082 || !TYPE_UNSIGNED (TREE_TYPE (@4)))
3083 && (GIMPLE || !TREE_SIDE_EFFECTS (@1)))
3084 (cond (cmp @2 @3) @1 @0)))
3085 (simplify
3086 (plus:c @0 (bit_and:c (minus @1 @0)
3087 (convert? (negate@4 (convert? (cmp@5 @2 @3))))))
3088 (if (INTEGRAL_TYPE_P (type)
3089 && INTEGRAL_TYPE_P (TREE_TYPE (@4))
3090 && TREE_CODE (TREE_TYPE (@4)) != BOOLEAN_TYPE
3091 && INTEGRAL_TYPE_P (TREE_TYPE (@5))
3092 && (TYPE_PRECISION (TREE_TYPE (@4)) >= TYPE_PRECISION (type)
3093 || !TYPE_UNSIGNED (TREE_TYPE (@4)))
3094 && (GIMPLE || !TREE_SIDE_EFFECTS (@1)))
3095 (cond (cmp @2 @3) @1 @0)))
3096 /* Similarly with ^ instead of - though in that case with :c. */
3097 (simplify
3098 (bit_xor:c @0 (bit_and:c (bit_xor:c @0 @1)
3099 (convert? (negate@4 (convert? (cmp@5 @2 @3))))))
3100 (if (INTEGRAL_TYPE_P (type)
3101 && INTEGRAL_TYPE_P (TREE_TYPE (@4))
3102 && TREE_CODE (TREE_TYPE (@4)) != BOOLEAN_TYPE
3103 && INTEGRAL_TYPE_P (TREE_TYPE (@5))
3104 && (TYPE_PRECISION (TREE_TYPE (@4)) >= TYPE_PRECISION (type)
3105 || !TYPE_UNSIGNED (TREE_TYPE (@4)))
3106 && (GIMPLE || !TREE_SIDE_EFFECTS (@1)))
3107 (cond (cmp @2 @3) @1 @0))))
3108
3109 /* Simplifications of shift and rotates. */
3110
3111 (for rotate (lrotate rrotate)
3112 (simplify
3113 (rotate integer_all_onesp@0 @1)
3114 @0))
3115
3116 /* Optimize -1 >> x for arithmetic right shifts. */
3117 (simplify
3118 (rshift integer_all_onesp@0 @1)
3119 (if (!TYPE_UNSIGNED (type))
3120 @0))
3121
3122 /* Optimize (x >> c) << c into x & (-1<<c). */
3123 (simplify
3124 (lshift (nop_convert? (rshift @0 INTEGER_CST@1)) @1)
3125 (if (wi::ltu_p (wi::to_wide (@1), element_precision (type)))
3126 /* It doesn't matter if the right shift is arithmetic or logical. */
3127 (bit_and (view_convert @0) (lshift { build_minus_one_cst (type); } @1))))
3128
3129 (simplify
3130 (lshift (convert (convert@2 (rshift @0 INTEGER_CST@1))) @1)
3131 (if (wi::ltu_p (wi::to_wide (@1), element_precision (type))
3132 /* Allow intermediate conversion to integral type with whatever sign, as
3133 long as the low TYPE_PRECISION (type)
3134 - TYPE_PRECISION (TREE_TYPE (@2)) bits are preserved. */
3135 && INTEGRAL_TYPE_P (type)
3136 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
3137 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
3138 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (@0))
3139 && (TYPE_PRECISION (TREE_TYPE (@2)) >= TYPE_PRECISION (type)
3140 || wi::geu_p (wi::to_wide (@1),
3141 TYPE_PRECISION (type)
3142 - TYPE_PRECISION (TREE_TYPE (@2)))))
3143 (bit_and (convert @0) (lshift { build_minus_one_cst (type); } @1))))
3144
3145 /* Optimize (x << c) >> c into x & ((unsigned)-1 >> c) for unsigned
3146 types. */
3147 (simplify
3148 (rshift (lshift @0 INTEGER_CST@1) @1)
3149 (if (TYPE_UNSIGNED (type)
3150 && (wi::ltu_p (wi::to_wide (@1), element_precision (type))))
3151 (bit_and @0 (rshift { build_minus_one_cst (type); } @1))))
3152
3153 /* Optimize x >> x into 0 */
3154 (simplify
3155 (rshift @0 @0)
3156 { build_zero_cst (type); })
3157
3158 (for shiftrotate (lrotate rrotate lshift rshift)
3159 (simplify
3160 (shiftrotate @0 integer_zerop)
3161 (non_lvalue @0))
3162 (simplify
3163 (shiftrotate integer_zerop@0 @1)
3164 @0)
3165 /* Prefer vector1 << scalar to vector1 << vector2
3166 if vector2 is uniform. */
3167 (for vec (VECTOR_CST CONSTRUCTOR)
3168 (simplify
3169 (shiftrotate @0 vec@1)
3170 (with { tree tem = uniform_vector_p (@1); }
3171 (if (tem)
3172 (shiftrotate @0 { tem; }))))))
3173
3174 /* Simplify X << Y where Y's low width bits are 0 to X, as only valid
3175 Y is 0. Similarly for X >> Y. */
3176 #if GIMPLE
3177 (for shift (lshift rshift)
3178 (simplify
3179 (shift @0 SSA_NAME@1)
3180 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1)))
3181 (with {
3182 int width = ceil_log2 (element_precision (TREE_TYPE (@0)));
3183 int prec = TYPE_PRECISION (TREE_TYPE (@1));
3184 }
3185 (if ((get_nonzero_bits (@1) & wi::mask (width, false, prec)) == 0)
3186 @0)))))
3187 #endif
3188
3189 /* Rewrite an LROTATE_EXPR by a constant into an
3190 RROTATE_EXPR by a new constant. */
3191 (simplify
3192 (lrotate @0 INTEGER_CST@1)
3193 (rrotate @0 { const_binop (MINUS_EXPR, TREE_TYPE (@1),
3194 build_int_cst (TREE_TYPE (@1),
3195 element_precision (type)), @1); }))
3196
3197 /* Turn (a OP c1) OP c2 into a OP (c1+c2). */
3198 (for op (lrotate rrotate rshift lshift)
3199 (simplify
3200 (op (op @0 INTEGER_CST@1) INTEGER_CST@2)
3201 (with { unsigned int prec = element_precision (type); }
3202 (if (wi::ge_p (wi::to_wide (@1), 0, TYPE_SIGN (TREE_TYPE (@1)))
3203 && wi::lt_p (wi::to_wide (@1), prec, TYPE_SIGN (TREE_TYPE (@1)))
3204 && wi::ge_p (wi::to_wide (@2), 0, TYPE_SIGN (TREE_TYPE (@2)))
3205 && wi::lt_p (wi::to_wide (@2), prec, TYPE_SIGN (TREE_TYPE (@2))))
3206 (with { unsigned int low = (tree_to_uhwi (@1)
3207 + tree_to_uhwi (@2)); }
3208 /* Deal with a OP (c1 + c2) being undefined but (a OP c1) OP c2
3209 being well defined. */
3210 (if (low >= prec)
3211 (if (op == LROTATE_EXPR || op == RROTATE_EXPR)
3212 (op @0 { build_int_cst (TREE_TYPE (@1), low % prec); })
3213 (if (TYPE_UNSIGNED (type) || op == LSHIFT_EXPR)
3214 { build_zero_cst (type); }
3215 (op @0 { build_int_cst (TREE_TYPE (@1), prec - 1); })))
3216 (op @0 { build_int_cst (TREE_TYPE (@1), low); })))))))
3217
3218
3219 /* Simplify (CST << x) & 1 to 0 if CST is even or to x == 0 if it is odd. */
3220 (simplify
3221 (bit_and (lshift INTEGER_CST@1 @0) integer_onep)
3222 (if ((wi::to_wide (@1) & 1) != 0)
3223 (convert (eq:boolean_type_node @0 { build_zero_cst (TREE_TYPE (@0)); }))
3224 { build_zero_cst (type); }))
3225
3226 /* Simplify ((C << x) & D) != 0 where C and D are power of two constants,
3227 either to false if D is smaller (unsigned comparison) than C, or to
3228 x == log2 (D) - log2 (C). Similarly for right shifts. */
3229 (for cmp (ne eq)
3230 icmp (eq ne)
3231 (simplify
3232 (cmp (bit_and (lshift integer_pow2p@1 @0) integer_pow2p@2) integer_zerop)
3233 (with { int c1 = wi::clz (wi::to_wide (@1));
3234 int c2 = wi::clz (wi::to_wide (@2)); }
3235 (if (c1 < c2)
3236 { constant_boolean_node (cmp == NE_EXPR ? false : true, type); }
3237 (icmp @0 { build_int_cst (TREE_TYPE (@0), c1 - c2); }))))
3238 (simplify
3239 (cmp (bit_and (rshift integer_pow2p@1 @0) integer_pow2p@2) integer_zerop)
3240 (if (tree_int_cst_sgn (@1) > 0)
3241 (with { int c1 = wi::clz (wi::to_wide (@1));
3242 int c2 = wi::clz (wi::to_wide (@2)); }
3243 (if (c1 > c2)
3244 { constant_boolean_node (cmp == NE_EXPR ? false : true, type); }
3245 (icmp @0 { build_int_cst (TREE_TYPE (@0), c2 - c1); }))))))
3246
3247 /* (CST1 << A) == CST2 -> A == ctz (CST2) - ctz (CST1)
3248 (CST1 << A) != CST2 -> A != ctz (CST2) - ctz (CST1)
3249 if CST2 != 0. */
3250 (for cmp (ne eq)
3251 (simplify
3252 (cmp (lshift INTEGER_CST@0 @1) INTEGER_CST@2)
3253 (with { int cand = wi::ctz (wi::to_wide (@2)) - wi::ctz (wi::to_wide (@0)); }
3254 (if (cand < 0
3255 || (!integer_zerop (@2)
3256 && wi::lshift (wi::to_wide (@0), cand) != wi::to_wide (@2)))
3257 { constant_boolean_node (cmp == NE_EXPR, type); }
3258 (if (!integer_zerop (@2)
3259 && wi::lshift (wi::to_wide (@0), cand) == wi::to_wide (@2))
3260 (cmp @1 { build_int_cst (TREE_TYPE (@1), cand); }))))))
3261
3262 /* Fold (X << C1) & C2 into (X << C1) & (C2 | ((1 << C1) - 1))
3263 (X >> C1) & C2 into (X >> C1) & (C2 | ~((type) -1 >> C1))
3264 if the new mask might be further optimized. */
3265 (for shift (lshift rshift)
3266 (simplify
3267 (bit_and (convert?:s@4 (shift:s@5 (convert1?@3 @0) INTEGER_CST@1))
3268 INTEGER_CST@2)
3269 (if (tree_nop_conversion_p (TREE_TYPE (@4), TREE_TYPE (@5))
3270 && TYPE_PRECISION (type) <= HOST_BITS_PER_WIDE_INT
3271 && tree_fits_uhwi_p (@1)
3272 && tree_to_uhwi (@1) > 0
3273 && tree_to_uhwi (@1) < TYPE_PRECISION (type))
3274 (with
3275 {
3276 unsigned int shiftc = tree_to_uhwi (@1);
3277 unsigned HOST_WIDE_INT mask = TREE_INT_CST_LOW (@2);
3278 unsigned HOST_WIDE_INT newmask, zerobits = 0;
3279 tree shift_type = TREE_TYPE (@3);
3280 unsigned int prec;
3281
3282 if (shift == LSHIFT_EXPR)
3283 zerobits = ((HOST_WIDE_INT_1U << shiftc) - 1);
3284 else if (shift == RSHIFT_EXPR
3285 && type_has_mode_precision_p (shift_type))
3286 {
3287 prec = TYPE_PRECISION (TREE_TYPE (@3));
3288 tree arg00 = @0;
3289 /* See if more bits can be proven as zero because of
3290 zero extension. */
3291 if (@3 != @0
3292 && TYPE_UNSIGNED (TREE_TYPE (@0)))
3293 {
3294 tree inner_type = TREE_TYPE (@0);
3295 if (type_has_mode_precision_p (inner_type)
3296 && TYPE_PRECISION (inner_type) < prec)
3297 {
3298 prec = TYPE_PRECISION (inner_type);
3299 /* See if we can shorten the right shift. */
3300 if (shiftc < prec)
3301 shift_type = inner_type;
3302 /* Otherwise X >> C1 is all zeros, so we'll optimize
3303 it into (X, 0) later on by making sure zerobits
3304 is all ones. */
3305 }
3306 }
3307 zerobits = HOST_WIDE_INT_M1U;
3308 if (shiftc < prec)
3309 {
3310 zerobits >>= HOST_BITS_PER_WIDE_INT - shiftc;
3311 zerobits <<= prec - shiftc;
3312 }
3313 /* For arithmetic shift if sign bit could be set, zerobits
3314 can contain actually sign bits, so no transformation is
3315 possible, unless MASK masks them all away. In that
3316 case the shift needs to be converted into logical shift. */
3317 if (!TYPE_UNSIGNED (TREE_TYPE (@3))
3318 && prec == TYPE_PRECISION (TREE_TYPE (@3)))
3319 {
3320 if ((mask & zerobits) == 0)
3321 shift_type = unsigned_type_for (TREE_TYPE (@3));
3322 else
3323 zerobits = 0;
3324 }
3325 }
3326 }
3327 /* ((X << 16) & 0xff00) is (X, 0). */
3328 (if ((mask & zerobits) == mask)
3329 { build_int_cst (type, 0); }
3330 (with { newmask = mask | zerobits; }
3331 (if (newmask != mask && (newmask & (newmask + 1)) == 0)
3332 (with
3333 {
3334 /* Only do the transformation if NEWMASK is some integer
3335 mode's mask. */
3336 for (prec = BITS_PER_UNIT;
3337 prec < HOST_BITS_PER_WIDE_INT; prec <<= 1)
3338 if (newmask == (HOST_WIDE_INT_1U << prec) - 1)
3339 break;
3340 }
3341 (if (prec < HOST_BITS_PER_WIDE_INT
3342 || newmask == HOST_WIDE_INT_M1U)
3343 (with
3344 { tree newmaskt = build_int_cst_type (TREE_TYPE (@2), newmask); }
3345 (if (!tree_int_cst_equal (newmaskt, @2))
3346 (if (shift_type != TREE_TYPE (@3))
3347 (bit_and (convert (shift:shift_type (convert @3) @1)) { newmaskt; })
3348 (bit_and @4 { newmaskt; })))))))))))))
3349
3350 /* Fold (X {&,^,|} C2) << C1 into (X << C1) {&,^,|} (C2 << C1)
3351 (X {&,^,|} C2) >> C1 into (X >> C1) & (C2 >> C1). */
3352 (for shift (lshift rshift)
3353 (for bit_op (bit_and bit_xor bit_ior)
3354 (simplify
3355 (shift (convert?:s (bit_op:s @0 INTEGER_CST@2)) INTEGER_CST@1)
3356 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
3357 (with { tree mask = int_const_binop (shift, fold_convert (type, @2), @1); }
3358 (if (mask)
3359 (bit_op (shift (convert @0) @1) { mask; })))))))
3360
3361 /* ~(~X >> Y) -> X >> Y (for arithmetic shift). */
3362 (simplify
3363 (bit_not (convert1?:s (rshift:s (convert2?@0 (bit_not @1)) @2)))
3364 (if (!TYPE_UNSIGNED (TREE_TYPE (@0))
3365 && (element_precision (TREE_TYPE (@0))
3366 <= element_precision (TREE_TYPE (@1))
3367 || !TYPE_UNSIGNED (TREE_TYPE (@1))))
3368 (with
3369 { tree shift_type = TREE_TYPE (@0); }
3370 (convert (rshift (convert:shift_type @1) @2)))))
3371
3372 /* ~(~X >>r Y) -> X >>r Y
3373 ~(~X <<r Y) -> X <<r Y */
3374 (for rotate (lrotate rrotate)
3375 (simplify
3376 (bit_not (convert1?:s (rotate:s (convert2?@0 (bit_not @1)) @2)))
3377 (if ((element_precision (TREE_TYPE (@0))
3378 <= element_precision (TREE_TYPE (@1))
3379 || !TYPE_UNSIGNED (TREE_TYPE (@1)))
3380 && (element_precision (type) <= element_precision (TREE_TYPE (@0))
3381 || !TYPE_UNSIGNED (TREE_TYPE (@0))))
3382 (with
3383 { tree rotate_type = TREE_TYPE (@0); }
3384 (convert (rotate (convert:rotate_type @1) @2))))))
3385
3386 (for cmp (eq ne)
3387 (for rotate (lrotate rrotate)
3388 invrot (rrotate lrotate)
3389 /* (X >>r Y) cmp (Z >>r Y) may simplify to X cmp Y. */
3390 (simplify
3391 (cmp (rotate @1 @0) (rotate @2 @0))
3392 (cmp @1 @2))
3393 /* (X >>r C1) cmp C2 may simplify to X cmp C3. */
3394 (simplify
3395 (cmp (rotate @0 INTEGER_CST@1) INTEGER_CST@2)
3396 (cmp @0 { const_binop (invrot, TREE_TYPE (@0), @2, @1); }))
3397 /* (X >>r Y) cmp C where C is 0 or ~0, may simplify to X cmp C. */
3398 (simplify
3399 (cmp (rotate @0 @1) INTEGER_CST@2)
3400 (if (integer_zerop (@2) || integer_all_onesp (@2))
3401 (cmp @0 @2)))))
3402
3403 /* Both signed and unsigned lshift produce the same result, so use
3404 the form that minimizes the number of conversions. Postpone this
3405 transformation until after shifts by zero have been folded. */
3406 (simplify
3407 (convert (lshift:s@0 (convert:s@1 @2) INTEGER_CST@3))
3408 (if (INTEGRAL_TYPE_P (type)
3409 && tree_nop_conversion_p (type, TREE_TYPE (@0))
3410 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
3411 && TYPE_PRECISION (TREE_TYPE (@2)) <= TYPE_PRECISION (type)
3412 && !integer_zerop (@3))
3413 (lshift (convert @2) @3)))
3414
3415 /* Simplifications of conversions. */
3416
3417 /* Basic strip-useless-type-conversions / strip_nops. */
3418 (for cvt (convert view_convert float fix_trunc)
3419 (simplify
3420 (cvt @0)
3421 (if ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@0)))
3422 || (GENERIC && type == TREE_TYPE (@0)))
3423 @0)))
3424
3425 /* Contract view-conversions. */
3426 (simplify
3427 (view_convert (view_convert @0))
3428 (view_convert @0))
3429
3430 /* For integral conversions with the same precision or pointer
3431 conversions use a NOP_EXPR instead. */
3432 (simplify
3433 (view_convert @0)
3434 (if ((INTEGRAL_TYPE_P (type) || POINTER_TYPE_P (type))
3435 && (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
3436 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (@0)))
3437 (convert @0)))
3438
3439 /* Strip inner integral conversions that do not change precision or size, or
3440 zero-extend while keeping the same size (for bool-to-char). */
3441 (simplify
3442 (view_convert (convert@0 @1))
3443 (if ((INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
3444 && (INTEGRAL_TYPE_P (TREE_TYPE (@1)) || POINTER_TYPE_P (TREE_TYPE (@1)))
3445 && TYPE_SIZE (TREE_TYPE (@0)) == TYPE_SIZE (TREE_TYPE (@1))
3446 && (TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1))
3447 || (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (TREE_TYPE (@1))
3448 && TYPE_UNSIGNED (TREE_TYPE (@1)))))
3449 (view_convert @1)))
3450
3451 /* Simplify a view-converted empty constructor. */
3452 (simplify
3453 (view_convert CONSTRUCTOR@0)
3454 (if (TREE_CODE (@0) != SSA_NAME
3455 && CONSTRUCTOR_NELTS (@0) == 0)
3456 { build_zero_cst (type); }))
3457
3458 /* Re-association barriers around constants and other re-association
3459 barriers can be removed. */
3460 (simplify
3461 (paren CONSTANT_CLASS_P@0)
3462 @0)
3463 (simplify
3464 (paren (paren@1 @0))
3465 @1)
3466
3467 /* Handle cases of two conversions in a row. */
3468 (for ocvt (convert float fix_trunc)
3469 (for icvt (convert float)
3470 (simplify
3471 (ocvt (icvt@1 @0))
3472 (with
3473 {
3474 tree inside_type = TREE_TYPE (@0);
3475 tree inter_type = TREE_TYPE (@1);
3476 int inside_int = INTEGRAL_TYPE_P (inside_type);
3477 int inside_ptr = POINTER_TYPE_P (inside_type);
3478 int inside_float = FLOAT_TYPE_P (inside_type);
3479 int inside_vec = VECTOR_TYPE_P (inside_type);
3480 unsigned int inside_prec = TYPE_PRECISION (inside_type);
3481 int inside_unsignedp = TYPE_UNSIGNED (inside_type);
3482 int inter_int = INTEGRAL_TYPE_P (inter_type);
3483 int inter_ptr = POINTER_TYPE_P (inter_type);
3484 int inter_float = FLOAT_TYPE_P (inter_type);
3485 int inter_vec = VECTOR_TYPE_P (inter_type);
3486 unsigned int inter_prec = TYPE_PRECISION (inter_type);
3487 int inter_unsignedp = TYPE_UNSIGNED (inter_type);
3488 int final_int = INTEGRAL_TYPE_P (type);
3489 int final_ptr = POINTER_TYPE_P (type);
3490 int final_float = FLOAT_TYPE_P (type);
3491 int final_vec = VECTOR_TYPE_P (type);
3492 unsigned int final_prec = TYPE_PRECISION (type);
3493 int final_unsignedp = TYPE_UNSIGNED (type);
3494 }
3495 (switch
3496 /* In addition to the cases of two conversions in a row
3497 handled below, if we are converting something to its own
3498 type via an object of identical or wider precision, neither
3499 conversion is needed. */
3500 (if (((GIMPLE && useless_type_conversion_p (type, inside_type))
3501 || (GENERIC
3502 && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (inside_type)))
3503 && (((inter_int || inter_ptr) && final_int)
3504 || (inter_float && final_float))
3505 && inter_prec >= final_prec)
3506 (ocvt @0))
3507
3508 /* Likewise, if the intermediate and initial types are either both
3509 float or both integer, we don't need the middle conversion if the
3510 former is wider than the latter and doesn't change the signedness
3511 (for integers). Avoid this if the final type is a pointer since
3512 then we sometimes need the middle conversion. */
3513 (if (((inter_int && inside_int) || (inter_float && inside_float))
3514 && (final_int || final_float)
3515 && inter_prec >= inside_prec
3516 && (inter_float || inter_unsignedp == inside_unsignedp))
3517 (ocvt @0))
3518
3519 /* If we have a sign-extension of a zero-extended value, we can
3520 replace that by a single zero-extension. Likewise if the
3521 final conversion does not change precision we can drop the
3522 intermediate conversion. */
3523 (if (inside_int && inter_int && final_int
3524 && ((inside_prec < inter_prec && inter_prec < final_prec
3525 && inside_unsignedp && !inter_unsignedp)
3526 || final_prec == inter_prec))
3527 (ocvt @0))
3528
3529 /* Two conversions in a row are not needed unless:
3530 - some conversion is floating-point (overstrict for now), or
3531 - some conversion is a vector (overstrict for now), or
3532 - the intermediate type is narrower than both initial and
3533 final, or
3534 - the intermediate type and innermost type differ in signedness,
3535 and the outermost type is wider than the intermediate, or
3536 - the initial type is a pointer type and the precisions of the
3537 intermediate and final types differ, or
3538 - the final type is a pointer type and the precisions of the
3539 initial and intermediate types differ. */
3540 (if (! inside_float && ! inter_float && ! final_float
3541 && ! inside_vec && ! inter_vec && ! final_vec
3542 && (inter_prec >= inside_prec || inter_prec >= final_prec)
3543 && ! (inside_int && inter_int
3544 && inter_unsignedp != inside_unsignedp
3545 && inter_prec < final_prec)
3546 && ((inter_unsignedp && inter_prec > inside_prec)
3547 == (final_unsignedp && final_prec > inter_prec))
3548 && ! (inside_ptr && inter_prec != final_prec)
3549 && ! (final_ptr && inside_prec != inter_prec))
3550 (ocvt @0))
3551
3552 /* A truncation to an unsigned type (a zero-extension) should be
3553 canonicalized as bitwise and of a mask. */
3554 (if (GIMPLE /* PR70366: doing this in GENERIC breaks -Wconversion. */
3555 && final_int && inter_int && inside_int
3556 && final_prec == inside_prec
3557 && final_prec > inter_prec
3558 && inter_unsignedp)
3559 (convert (bit_and @0 { wide_int_to_tree
3560 (inside_type,
3561 wi::mask (inter_prec, false,
3562 TYPE_PRECISION (inside_type))); })))
3563
3564 /* If we are converting an integer to a floating-point that can
3565 represent it exactly and back to an integer, we can skip the
3566 floating-point conversion. */
3567 (if (GIMPLE /* PR66211 */
3568 && inside_int && inter_float && final_int &&
3569 (unsigned) significand_size (TYPE_MODE (inter_type))
3570 >= inside_prec - !inside_unsignedp)
3571 (convert @0)))))))
3572
3573 /* If we have a narrowing conversion to an integral type that is fed by a
3574 BIT_AND_EXPR, we might be able to remove the BIT_AND_EXPR if it merely
3575 masks off bits outside the final type (and nothing else). */
3576 (simplify
3577 (convert (bit_and @0 INTEGER_CST@1))
3578 (if (INTEGRAL_TYPE_P (type)
3579 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
3580 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))
3581 && operand_equal_p (@1, build_low_bits_mask (TREE_TYPE (@1),
3582 TYPE_PRECISION (type)), 0))
3583 (convert @0)))
3584
3585
3586 /* (X /[ex] A) * A -> X. */
3587 (simplify
3588 (mult (convert1? (exact_div @0 @@1)) (convert2? @1))
3589 (convert @0))
3590
3591 /* Simplify (A / B) * B + (A % B) -> A. */
3592 (for div (trunc_div ceil_div floor_div round_div)
3593 mod (trunc_mod ceil_mod floor_mod round_mod)
3594 (simplify
3595 (plus:c (mult:c (div @0 @1) @1) (mod @0 @1))
3596 @0))
3597
3598 /* ((X /[ex] A) +- B) * A --> X +- A * B. */
3599 (for op (plus minus)
3600 (simplify
3601 (mult (convert1? (op (convert2? (exact_div @0 INTEGER_CST@@1)) INTEGER_CST@2)) @1)
3602 (if (tree_nop_conversion_p (type, TREE_TYPE (@2))
3603 && tree_nop_conversion_p (TREE_TYPE (@0), TREE_TYPE (@2)))
3604 (with
3605 {
3606 wi::overflow_type overflow;
3607 wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@2),
3608 TYPE_SIGN (type), &overflow);
3609 }
3610 (if (types_match (type, TREE_TYPE (@2))
3611 && types_match (TREE_TYPE (@0), TREE_TYPE (@2)) && !overflow)
3612 (op @0 { wide_int_to_tree (type, mul); })
3613 (with { tree utype = unsigned_type_for (type); }
3614 (convert (op (convert:utype @0)
3615 (mult (convert:utype @1) (convert:utype @2))))))))))
3616
3617 /* Canonicalization of binary operations. */
3618
3619 /* Convert X + -C into X - C. */
3620 (simplify
3621 (plus @0 REAL_CST@1)
3622 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
3623 (with { tree tem = const_unop (NEGATE_EXPR, type, @1); }
3624 (if (!TREE_OVERFLOW (tem) || !flag_trapping_math)
3625 (minus @0 { tem; })))))
3626
3627 /* Convert x+x into x*2. */
3628 (simplify
3629 (plus @0 @0)
3630 (if (SCALAR_FLOAT_TYPE_P (type))
3631 (mult @0 { build_real (type, dconst2); })
3632 (if (INTEGRAL_TYPE_P (type))
3633 (mult @0 { build_int_cst (type, 2); }))))
3634
3635 /* 0 - X -> -X. */
3636 (simplify
3637 (minus integer_zerop @1)
3638 (negate @1))
3639 (simplify
3640 (pointer_diff integer_zerop @1)
3641 (negate (convert @1)))
3642
3643 /* (ARG0 - ARG1) is the same as (-ARG1 + ARG0). So check whether
3644 ARG0 is zero and X + ARG0 reduces to X, since that would mean
3645 (-ARG1 + ARG0) reduces to -ARG1. */
3646 (simplify
3647 (minus real_zerop@0 @1)
3648 (if (fold_real_zero_addition_p (type, @1, @0, 0))
3649 (negate @1)))
3650
3651 /* Transform x * -1 into -x. */
3652 (simplify
3653 (mult @0 integer_minus_onep)
3654 (negate @0))
3655
3656 /* Reassociate (X * CST) * Y to (X * Y) * CST. This does not introduce
3657 signed overflow for CST != 0 && CST != -1. */
3658 (simplify
3659 (mult:c (mult:s@3 @0 INTEGER_CST@1) @2)
3660 (if (TREE_CODE (@2) != INTEGER_CST
3661 && single_use (@3)
3662 && !integer_zerop (@1) && !integer_minus_onep (@1))
3663 (mult (mult @0 @2) @1)))
3664
3665 /* True if we can easily extract the real and imaginary parts of a complex
3666 number. */
3667 (match compositional_complex
3668 (convert? (complex @0 @1)))
3669
3670 /* COMPLEX_EXPR and REALPART/IMAGPART_EXPR cancellations. */
3671 (simplify
3672 (complex (realpart @0) (imagpart @0))
3673 @0)
3674 (simplify
3675 (realpart (complex @0 @1))
3676 @0)
3677 (simplify
3678 (imagpart (complex @0 @1))
3679 @1)
3680
3681 /* Sometimes we only care about half of a complex expression. */
3682 (simplify
3683 (realpart (convert?:s (conj:s @0)))
3684 (convert (realpart @0)))
3685 (simplify
3686 (imagpart (convert?:s (conj:s @0)))
3687 (convert (negate (imagpart @0))))
3688 (for part (realpart imagpart)
3689 (for op (plus minus)
3690 (simplify
3691 (part (convert?:s@2 (op:s @0 @1)))
3692 (convert (op (part @0) (part @1))))))
3693 (simplify
3694 (realpart (convert?:s (CEXPI:s @0)))
3695 (convert (COS @0)))
3696 (simplify
3697 (imagpart (convert?:s (CEXPI:s @0)))
3698 (convert (SIN @0)))
3699
3700 /* conj(conj(x)) -> x */
3701 (simplify
3702 (conj (convert? (conj @0)))
3703 (if (tree_nop_conversion_p (TREE_TYPE (@0), type))
3704 (convert @0)))
3705
3706 /* conj({x,y}) -> {x,-y} */
3707 (simplify
3708 (conj (convert?:s (complex:s @0 @1)))
3709 (with { tree itype = TREE_TYPE (type); }
3710 (complex (convert:itype @0) (negate (convert:itype @1)))))
3711
3712 /* BSWAP simplifications, transforms checked by gcc.dg/builtin-bswap-8.c. */
3713 (for bswap (BUILT_IN_BSWAP16 BUILT_IN_BSWAP32
3714 BUILT_IN_BSWAP64 BUILT_IN_BSWAP128)
3715 (simplify
3716 (bswap (bswap @0))
3717 @0)
3718 (simplify
3719 (bswap (bit_not (bswap @0)))
3720 (bit_not @0))
3721 (for bitop (bit_xor bit_ior bit_and)
3722 (simplify
3723 (bswap (bitop:c (bswap @0) @1))
3724 (bitop @0 (bswap @1))))
3725 (for cmp (eq ne)
3726 (simplify
3727 (cmp (bswap@2 @0) (bswap @1))
3728 (with { tree ctype = TREE_TYPE (@2); }
3729 (cmp (convert:ctype @0) (convert:ctype @1))))
3730 (simplify
3731 (cmp (bswap @0) INTEGER_CST@1)
3732 (with { tree ctype = TREE_TYPE (@1); }
3733 (cmp (convert:ctype @0) (bswap @1)))))
3734 /* (bswap(x) >> C1) & C2 can sometimes be simplified to (x >> C3) & C2. */
3735 (simplify
3736 (bit_and (convert1? (rshift@0 (convert2? (bswap@4 @1)) INTEGER_CST@2))
3737 INTEGER_CST@3)
3738 (if (BITS_PER_UNIT == 8
3739 && tree_fits_uhwi_p (@2)
3740 && tree_fits_uhwi_p (@3))
3741 (with
3742 {
3743 unsigned HOST_WIDE_INT prec = TYPE_PRECISION (TREE_TYPE (@4));
3744 unsigned HOST_WIDE_INT bits = tree_to_uhwi (@2);
3745 unsigned HOST_WIDE_INT mask = tree_to_uhwi (@3);
3746 unsigned HOST_WIDE_INT lo = bits & 7;
3747 unsigned HOST_WIDE_INT hi = bits - lo;
3748 }
3749 (if (bits < prec
3750 && mask < (256u>>lo)
3751 && bits < TYPE_PRECISION (TREE_TYPE(@0)))
3752 (with { unsigned HOST_WIDE_INT ns = (prec - (hi + 8)) + lo; }
3753 (if (ns == 0)
3754 (bit_and (convert @1) @3)
3755 (with
3756 {
3757 tree utype = unsigned_type_for (TREE_TYPE (@1));
3758 tree nst = build_int_cst (integer_type_node, ns);
3759 }
3760 (bit_and (convert (rshift:utype (convert:utype @1) {nst;})) @3))))))))
3761 /* bswap(x) >> C1 can sometimes be simplified to (T)x >> C2. */
3762 (simplify
3763 (rshift (convert? (bswap@2 @0)) INTEGER_CST@1)
3764 (if (BITS_PER_UNIT == 8
3765 && CHAR_TYPE_SIZE == 8
3766 && tree_fits_uhwi_p (@1))
3767 (with
3768 {
3769 unsigned HOST_WIDE_INT prec = TYPE_PRECISION (TREE_TYPE (@2));
3770 unsigned HOST_WIDE_INT bits = tree_to_uhwi (@1);
3771 /* If the bswap was extended before the original shift, this
3772 byte (shift) has the sign of the extension, not the sign of
3773 the original shift. */
3774 tree st = TYPE_PRECISION (type) > prec ? TREE_TYPE (@2) : type;
3775 }
3776 /* Special case: logical right shift of sign-extended bswap.
3777 (unsigned)(short)bswap16(x)>>12 is (unsigned)((short)x<<8)>>12. */
3778 (if (TYPE_PRECISION (type) > prec
3779 && !TYPE_UNSIGNED (TREE_TYPE (@2))
3780 && TYPE_UNSIGNED (type)
3781 && bits < prec && bits + 8 >= prec)
3782 (with { tree nst = build_int_cst (integer_type_node, prec - 8); }
3783 (rshift (convert (lshift:st (convert:st @0) {nst;})) @1))
3784 (if (bits + 8 == prec)
3785 (if (TYPE_UNSIGNED (st))
3786 (convert (convert:unsigned_char_type_node @0))
3787 (convert (convert:signed_char_type_node @0)))
3788 (if (bits < prec && bits + 8 > prec)
3789 (with
3790 {
3791 tree nst = build_int_cst (integer_type_node, bits & 7);
3792 tree bt = TYPE_UNSIGNED (st) ? unsigned_char_type_node
3793 : signed_char_type_node;
3794 }
3795 (convert (rshift:bt (convert:bt @0) {nst;})))))))))
3796 /* bswap(x) & C1 can sometimes be simplified to (x >> C2) & C1. */
3797 (simplify
3798 (bit_and (convert? (bswap@2 @0)) INTEGER_CST@1)
3799 (if (BITS_PER_UNIT == 8
3800 && tree_fits_uhwi_p (@1)
3801 && tree_to_uhwi (@1) < 256)
3802 (with
3803 {
3804 unsigned HOST_WIDE_INT prec = TYPE_PRECISION (TREE_TYPE (@2));
3805 tree utype = unsigned_type_for (TREE_TYPE (@0));
3806 tree nst = build_int_cst (integer_type_node, prec - 8);
3807 }
3808 (bit_and (convert (rshift:utype (convert:utype @0) {nst;})) @1)))))
3809
3810
3811 /* Combine COND_EXPRs and VEC_COND_EXPRs. */
3812
3813 /* Simplify constant conditions.
3814 Only optimize constant conditions when the selected branch
3815 has the same type as the COND_EXPR. This avoids optimizing
3816 away "c ? x : throw", where the throw has a void type.
3817 Note that we cannot throw away the fold-const.c variant nor
3818 this one as we depend on doing this transform before possibly
3819 A ? B : B -> B triggers and the fold-const.c one can optimize
3820 0 ? A : B to B even if A has side-effects. Something
3821 genmatch cannot handle. */
3822 (simplify
3823 (cond INTEGER_CST@0 @1 @2)
3824 (if (integer_zerop (@0))
3825 (if (!VOID_TYPE_P (TREE_TYPE (@2)) || VOID_TYPE_P (type))
3826 @2)
3827 (if (!VOID_TYPE_P (TREE_TYPE (@1)) || VOID_TYPE_P (type))
3828 @1)))
3829 (simplify
3830 (vec_cond VECTOR_CST@0 @1 @2)
3831 (if (integer_all_onesp (@0))
3832 @1
3833 (if (integer_zerop (@0))
3834 @2)))
3835
3836 #if GIMPLE
3837 /* Sink unary operations to branches, but only if we do fold both. */
3838 (for op (negate bit_not abs absu)
3839 (simplify
3840 (op (vec_cond:s @0 @1 @2))
3841 (vec_cond @0 (op! @1) (op! @2))))
3842
3843 /* Sink binary operation to branches, but only if we can fold it. */
3844 (for op (tcc_comparison plus minus mult bit_and bit_ior bit_xor
3845 lshift rshift rdiv trunc_div ceil_div floor_div round_div
3846 trunc_mod ceil_mod floor_mod round_mod min max)
3847 /* (c ? a : b) op (c ? d : e) --> c ? (a op d) : (b op e) */
3848 (simplify
3849 (op (vec_cond:s @0 @1 @2) (vec_cond:s @0 @3 @4))
3850 (vec_cond @0 (op! @1 @3) (op! @2 @4)))
3851
3852 /* (c ? a : b) op d --> c ? (a op d) : (b op d) */
3853 (simplify
3854 (op (vec_cond:s @0 @1 @2) @3)
3855 (vec_cond @0 (op! @1 @3) (op! @2 @3)))
3856 (simplify
3857 (op @3 (vec_cond:s @0 @1 @2))
3858 (vec_cond @0 (op! @3 @1) (op! @3 @2))))
3859 #endif
3860
3861 /* (v ? w : 0) ? a : b is just (v & w) ? a : b
3862 Currently disabled after pass lvec because ARM understands
3863 VEC_COND_EXPR<v==w,-1,0> but not a plain v==w fed to BIT_IOR_EXPR. */
3864 (simplify
3865 (vec_cond (vec_cond:s @0 @3 integer_zerop) @1 @2)
3866 (if (optimize_vectors_before_lowering_p () && types_match (@0, @3))
3867 (vec_cond (bit_and @0 @3) @1 @2)))
3868 (simplify
3869 (vec_cond (vec_cond:s @0 integer_all_onesp @3) @1 @2)
3870 (if (optimize_vectors_before_lowering_p () && types_match (@0, @3))
3871 (vec_cond (bit_ior @0 @3) @1 @2)))
3872 (simplify
3873 (vec_cond (vec_cond:s @0 integer_zerop @3) @1 @2)
3874 (if (optimize_vectors_before_lowering_p () && types_match (@0, @3))
3875 (vec_cond (bit_ior @0 (bit_not @3)) @2 @1)))
3876 (simplify
3877 (vec_cond (vec_cond:s @0 @3 integer_all_onesp) @1 @2)
3878 (if (optimize_vectors_before_lowering_p () && types_match (@0, @3))
3879 (vec_cond (bit_and @0 (bit_not @3)) @2 @1)))
3880
3881 /* c1 ? c2 ? a : b : b --> (c1 & c2) ? a : b */
3882 (simplify
3883 (vec_cond @0 (vec_cond:s @1 @2 @3) @3)
3884 (if (optimize_vectors_before_lowering_p () && types_match (@0, @1))
3885 (vec_cond (bit_and @0 @1) @2 @3)))
3886 (simplify
3887 (vec_cond @0 @2 (vec_cond:s @1 @2 @3))
3888 (if (optimize_vectors_before_lowering_p () && types_match (@0, @1))
3889 (vec_cond (bit_ior @0 @1) @2 @3)))
3890 (simplify
3891 (vec_cond @0 (vec_cond:s @1 @2 @3) @2)
3892 (if (optimize_vectors_before_lowering_p () && types_match (@0, @1))
3893 (vec_cond (bit_ior (bit_not @0) @1) @2 @3)))
3894 (simplify
3895 (vec_cond @0 @3 (vec_cond:s @1 @2 @3))
3896 (if (optimize_vectors_before_lowering_p () && types_match (@0, @1))
3897 (vec_cond (bit_and (bit_not @0) @1) @2 @3)))
3898
3899 /* Canonicalize mask ? { 0, ... } : { -1, ...} to ~mask if the mask
3900 types are compatible. */
3901 (simplify
3902 (vec_cond @0 VECTOR_CST@1 VECTOR_CST@2)
3903 (if (VECTOR_BOOLEAN_TYPE_P (type)
3904 && types_match (type, TREE_TYPE (@0)))
3905 (if (integer_zerop (@1) && integer_all_onesp (@2))
3906 (bit_not @0)
3907 (if (integer_all_onesp (@1) && integer_zerop (@2))
3908 @0))))
3909
3910 /* A few simplifications of "a ? CST1 : CST2". */
3911 /* NOTE: Only do this on gimple as the if-chain-to-switch
3912 optimization depends on the gimple to have if statements in it. */
3913 #if GIMPLE
3914 (simplify
3915 (cond @0 INTEGER_CST@1 INTEGER_CST@2)
3916 (switch
3917 (if (integer_zerop (@2))
3918 (switch
3919 /* a ? 1 : 0 -> a if 0 and 1 are integral types. */
3920 (if (integer_onep (@1))
3921 (convert (convert:boolean_type_node @0)))
3922 /* a ? -1 : 0 -> -a. */
3923 (if (INTEGRAL_TYPE_P (type) && integer_all_onesp (@1))
3924 (negate (convert (convert:boolean_type_node @0))))
3925 /* a ? powerof2cst : 0 -> a << (log2(powerof2cst)) */
3926 (if (INTEGRAL_TYPE_P (type) && integer_pow2p (@1))
3927 (with {
3928 tree shift = build_int_cst (integer_type_node, tree_log2 (@1));
3929 }
3930 (lshift (convert (convert:boolean_type_node @0)) { shift; })))))
3931 (if (integer_zerop (@1))
3932 (with {
3933 tree booltrue = constant_boolean_node (true, boolean_type_node);
3934 }
3935 (switch
3936 /* a ? 0 : 1 -> !a. */
3937 (if (integer_onep (@2))
3938 (convert (bit_xor (convert:boolean_type_node @0) { booltrue; } )))
3939 /* a ? -1 : 0 -> -(!a). */
3940 (if (INTEGRAL_TYPE_P (type) && integer_all_onesp (@2))
3941 (negate (convert (bit_xor (convert:boolean_type_node @0) { booltrue; } ))))
3942 /* a ? powerof2cst : 0 -> (!a) << (log2(powerof2cst)) */
3943 (if (INTEGRAL_TYPE_P (type) && integer_pow2p (@2))
3944 (with {
3945 tree shift = build_int_cst (integer_type_node, tree_log2 (@2));
3946 }
3947 (lshift (convert (bit_xor (convert:boolean_type_node @0) { booltrue; } ))
3948 { shift; }))))))))
3949 #endif
3950
3951 /* Simplification moved from fold_cond_expr_with_comparison. It may also
3952 be extended. */
3953 /* This pattern implements two kinds simplification:
3954
3955 Case 1)
3956 (cond (cmp (convert1? x) c1) (convert2? x) c2) -> (minmax (x c)) if:
3957 1) Conversions are type widening from smaller type.
3958 2) Const c1 equals to c2 after canonicalizing comparison.
3959 3) Comparison has tree code LT, LE, GT or GE.
3960 This specific pattern is needed when (cmp (convert x) c) may not
3961 be simplified by comparison patterns because of multiple uses of
3962 x. It also makes sense here because simplifying across multiple
3963 referred var is always benefitial for complicated cases.
3964
3965 Case 2)
3966 (cond (eq (convert1? x) c1) (convert2? x) c2) -> (cond (eq x c1) c1 c2). */
3967 (for cmp (lt le gt ge eq)
3968 (simplify
3969 (cond (cmp (convert1? @1) INTEGER_CST@3) (convert2? @1) INTEGER_CST@2)
3970 (with
3971 {
3972 tree from_type = TREE_TYPE (@1);
3973 tree c1_type = TREE_TYPE (@3), c2_type = TREE_TYPE (@2);
3974 enum tree_code code = ERROR_MARK;
3975
3976 if (INTEGRAL_TYPE_P (from_type)
3977 && int_fits_type_p (@2, from_type)
3978 && (types_match (c1_type, from_type)
3979 || (TYPE_PRECISION (c1_type) > TYPE_PRECISION (from_type)
3980 && (TYPE_UNSIGNED (from_type)
3981 || TYPE_SIGN (c1_type) == TYPE_SIGN (from_type))))
3982 && (types_match (c2_type, from_type)
3983 || (TYPE_PRECISION (c2_type) > TYPE_PRECISION (from_type)
3984 && (TYPE_UNSIGNED (from_type)
3985 || TYPE_SIGN (c2_type) == TYPE_SIGN (from_type)))))
3986 {
3987 if (cmp != EQ_EXPR)
3988 {
3989 if (wi::to_widest (@3) == (wi::to_widest (@2) - 1))
3990 {
3991 /* X <= Y - 1 equals to X < Y. */
3992 if (cmp == LE_EXPR)
3993 code = LT_EXPR;
3994 /* X > Y - 1 equals to X >= Y. */
3995 if (cmp == GT_EXPR)
3996 code = GE_EXPR;
3997 }
3998 if (wi::to_widest (@3) == (wi::to_widest (@2) + 1))
3999 {
4000 /* X < Y + 1 equals to X <= Y. */
4001 if (cmp == LT_EXPR)
4002 code = LE_EXPR;
4003 /* X >= Y + 1 equals to X > Y. */
4004 if (cmp == GE_EXPR)
4005 code = GT_EXPR;
4006 }
4007 if (code != ERROR_MARK
4008 || wi::to_widest (@2) == wi::to_widest (@3))
4009 {
4010 if (cmp == LT_EXPR || cmp == LE_EXPR)
4011 code = MIN_EXPR;
4012 if (cmp == GT_EXPR || cmp == GE_EXPR)
4013 code = MAX_EXPR;
4014 }
4015 }
4016 /* Can do A == C1 ? A : C2 -> A == C1 ? C1 : C2? */
4017 else if (int_fits_type_p (@3, from_type))
4018 code = EQ_EXPR;
4019 }
4020 }
4021 (if (code == MAX_EXPR)
4022 (convert (max @1 (convert @2)))
4023 (if (code == MIN_EXPR)
4024 (convert (min @1 (convert @2)))
4025 (if (code == EQ_EXPR)
4026 (convert (cond (eq @1 (convert @3))
4027 (convert:from_type @3) (convert:from_type @2)))))))))
4028
4029 /* (cond (cmp (convert? x) c1) (op x c2) c3) -> (op (minmax x c1) c2) if:
4030
4031 1) OP is PLUS or MINUS.
4032 2) CMP is LT, LE, GT or GE.
4033 3) C3 == (C1 op C2), and computation doesn't have undefined behavior.
4034
4035 This pattern also handles special cases like:
4036
4037 A) Operand x is a unsigned to signed type conversion and c1 is
4038 integer zero. In this case,
4039 (signed type)x < 0 <=> x > MAX_VAL(signed type)
4040 (signed type)x >= 0 <=> x <= MAX_VAL(signed type)
4041 B) Const c1 may not equal to (C3 op' C2). In this case we also
4042 check equality for (c1+1) and (c1-1) by adjusting comparison
4043 code.
4044
4045 TODO: Though signed type is handled by this pattern, it cannot be
4046 simplified at the moment because C standard requires additional
4047 type promotion. In order to match&simplify it here, the IR needs
4048 to be cleaned up by other optimizers, i.e, VRP. */
4049 (for op (plus minus)
4050 (for cmp (lt le gt ge)
4051 (simplify
4052 (cond (cmp (convert? @X) INTEGER_CST@1) (op @X INTEGER_CST@2) INTEGER_CST@3)
4053 (with { tree from_type = TREE_TYPE (@X), to_type = TREE_TYPE (@1); }
4054 (if (types_match (from_type, to_type)
4055 /* Check if it is special case A). */
4056 || (TYPE_UNSIGNED (from_type)
4057 && !TYPE_UNSIGNED (to_type)
4058 && TYPE_PRECISION (from_type) == TYPE_PRECISION (to_type)
4059 && integer_zerop (@1)
4060 && (cmp == LT_EXPR || cmp == GE_EXPR)))
4061 (with
4062 {
4063 wi::overflow_type overflow = wi::OVF_NONE;
4064 enum tree_code code, cmp_code = cmp;
4065 wide_int real_c1;
4066 wide_int c1 = wi::to_wide (@1);
4067 wide_int c2 = wi::to_wide (@2);
4068 wide_int c3 = wi::to_wide (@3);
4069 signop sgn = TYPE_SIGN (from_type);
4070
4071 /* Handle special case A), given x of unsigned type:
4072 ((signed type)x < 0) <=> (x > MAX_VAL(signed type))
4073 ((signed type)x >= 0) <=> (x <= MAX_VAL(signed type)) */
4074 if (!types_match (from_type, to_type))
4075 {
4076 if (cmp_code == LT_EXPR)
4077 cmp_code = GT_EXPR;
4078 if (cmp_code == GE_EXPR)
4079 cmp_code = LE_EXPR;
4080 c1 = wi::max_value (to_type);
4081 }
4082 /* To simplify this pattern, we require c3 = (c1 op c2). Here we
4083 compute (c3 op' c2) and check if it equals to c1 with op' being
4084 the inverted operator of op. Make sure overflow doesn't happen
4085 if it is undefined. */
4086 if (op == PLUS_EXPR)
4087 real_c1 = wi::sub (c3, c2, sgn, &overflow);
4088 else
4089 real_c1 = wi::add (c3, c2, sgn, &overflow);
4090
4091 code = cmp_code;
4092 if (!overflow || !TYPE_OVERFLOW_UNDEFINED (from_type))
4093 {
4094 /* Check if c1 equals to real_c1. Boundary condition is handled
4095 by adjusting comparison operation if necessary. */
4096 if (!wi::cmp (wi::sub (real_c1, 1, sgn, &overflow), c1, sgn)
4097 && !overflow)
4098 {
4099 /* X <= Y - 1 equals to X < Y. */
4100 if (cmp_code == LE_EXPR)
4101 code = LT_EXPR;
4102 /* X > Y - 1 equals to X >= Y. */
4103 if (cmp_code == GT_EXPR)
4104 code = GE_EXPR;
4105 }
4106 if (!wi::cmp (wi::add (real_c1, 1, sgn, &overflow), c1, sgn)
4107 && !overflow)
4108 {
4109 /* X < Y + 1 equals to X <= Y. */
4110 if (cmp_code == LT_EXPR)
4111 code = LE_EXPR;
4112 /* X >= Y + 1 equals to X > Y. */
4113 if (cmp_code == GE_EXPR)
4114 code = GT_EXPR;
4115 }
4116 if (code != cmp_code || !wi::cmp (real_c1, c1, sgn))
4117 {
4118 if (cmp_code == LT_EXPR || cmp_code == LE_EXPR)
4119 code = MIN_EXPR;
4120 if (cmp_code == GT_EXPR || cmp_code == GE_EXPR)
4121 code = MAX_EXPR;
4122 }
4123 }
4124 }
4125 (if (code == MAX_EXPR)
4126 (op (max @X { wide_int_to_tree (from_type, real_c1); })
4127 { wide_int_to_tree (from_type, c2); })
4128 (if (code == MIN_EXPR)
4129 (op (min @X { wide_int_to_tree (from_type, real_c1); })
4130 { wide_int_to_tree (from_type, c2); })))))))))
4131
4132 (for cnd (cond vec_cond)
4133 /* A ? B : (A ? X : C) -> A ? B : C. */
4134 (simplify
4135 (cnd @0 (cnd @0 @1 @2) @3)
4136 (cnd @0 @1 @3))
4137 (simplify
4138 (cnd @0 @1 (cnd @0 @2 @3))
4139 (cnd @0 @1 @3))
4140 /* A ? B : (!A ? C : X) -> A ? B : C. */
4141 /* ??? This matches embedded conditions open-coded because genmatch
4142 would generate matching code for conditions in separate stmts only.
4143 The following is still important to merge then and else arm cases
4144 from if-conversion. */
4145 (simplify
4146 (cnd @0 @1 (cnd @2 @3 @4))
4147 (if (inverse_conditions_p (@0, @2))
4148 (cnd @0 @1 @3)))
4149 (simplify
4150 (cnd @0 (cnd @1 @2 @3) @4)
4151 (if (inverse_conditions_p (@0, @1))
4152 (cnd @0 @3 @4)))
4153
4154 /* A ? B : B -> B. */
4155 (simplify
4156 (cnd @0 @1 @1)
4157 @1)
4158
4159 /* !A ? B : C -> A ? C : B. */
4160 (simplify
4161 (cnd (logical_inverted_value truth_valued_p@0) @1 @2)
4162 (cnd @0 @2 @1)))
4163
4164 /* abs/negative simplifications moved from fold_cond_expr_with_comparison,
4165 Need to handle (A - B) case as fold_cond_expr_with_comparison does.
4166 Need to handle UN* comparisons.
4167
4168 None of these transformations work for modes with signed
4169 zeros. If A is +/-0, the first two transformations will
4170 change the sign of the result (from +0 to -0, or vice
4171 versa). The last four will fix the sign of the result,
4172 even though the original expressions could be positive or
4173 negative, depending on the sign of A.
4174
4175 Note that all these transformations are correct if A is
4176 NaN, since the two alternatives (A and -A) are also NaNs. */
4177
4178 (for cnd (cond vec_cond)
4179 /* A == 0 ? A : -A same as -A */
4180 (for cmp (eq uneq)
4181 (simplify
4182 (cnd (cmp @0 zerop) @0 (negate@1 @0))
4183 (if (!HONOR_SIGNED_ZEROS (type))
4184 @1))
4185 (simplify
4186 (cnd (cmp @0 zerop) integer_zerop (negate@1 @0))
4187 (if (!HONOR_SIGNED_ZEROS (type))
4188 @1))
4189 )
4190 /* A != 0 ? A : -A same as A */
4191 (for cmp (ne ltgt)
4192 (simplify
4193 (cnd (cmp @0 zerop) @0 (negate @0))
4194 (if (!HONOR_SIGNED_ZEROS (type))
4195 @0))
4196 (simplify
4197 (cnd (cmp @0 zerop) @0 integer_zerop)
4198 (if (!HONOR_SIGNED_ZEROS (type))
4199 @0))
4200 )
4201 /* A >=/> 0 ? A : -A same as abs (A) */
4202 (for cmp (ge gt)
4203 (simplify
4204 (cnd (cmp @0 zerop) @0 (negate @0))
4205 (if (!HONOR_SIGNED_ZEROS (type)
4206 && !TYPE_UNSIGNED (type))
4207 (abs @0))))
4208 /* A <=/< 0 ? A : -A same as -abs (A) */
4209 (for cmp (le lt)
4210 (simplify
4211 (cnd (cmp @0 zerop) @0 (negate @0))
4212 (if (!HONOR_SIGNED_ZEROS (type)
4213 && !TYPE_UNSIGNED (type))
4214 (if (ANY_INTEGRAL_TYPE_P (type)
4215 && !TYPE_OVERFLOW_WRAPS (type))
4216 (with {
4217 tree utype = unsigned_type_for (type);
4218 }
4219 (convert (negate (absu:utype @0))))
4220 (negate (abs @0)))))
4221 )
4222 )
4223
4224 /* -(type)!A -> (type)A - 1. */
4225 (simplify
4226 (negate (convert?:s (logical_inverted_value:s @0)))
4227 (if (INTEGRAL_TYPE_P (type)
4228 && TREE_CODE (type) != BOOLEAN_TYPE
4229 && TYPE_PRECISION (type) > 1
4230 && TREE_CODE (@0) == SSA_NAME
4231 && ssa_name_has_boolean_range (@0))
4232 (plus (convert:type @0) { build_all_ones_cst (type); })))
4233
4234 /* A + (B vcmp C ? 1 : 0) -> A - (B vcmp C ? -1 : 0), since vector comparisons
4235 return all -1 or all 0 results. */
4236 /* ??? We could instead convert all instances of the vec_cond to negate,
4237 but that isn't necessarily a win on its own. */
4238 (simplify
4239 (plus:c @3 (view_convert? (vec_cond:s @0 integer_each_onep@1 integer_zerop@2)))
4240 (if (VECTOR_TYPE_P (type)
4241 && known_eq (TYPE_VECTOR_SUBPARTS (type),
4242 TYPE_VECTOR_SUBPARTS (TREE_TYPE (@1)))
4243 && (TYPE_MODE (TREE_TYPE (type))
4244 == TYPE_MODE (TREE_TYPE (TREE_TYPE (@1)))))
4245 (minus @3 (view_convert (vec_cond @0 (negate @1) @2)))))
4246
4247 /* ... likewise A - (B vcmp C ? 1 : 0) -> A + (B vcmp C ? -1 : 0). */
4248 (simplify
4249 (minus @3 (view_convert? (vec_cond:s @0 integer_each_onep@1 integer_zerop@2)))
4250 (if (VECTOR_TYPE_P (type)
4251 && known_eq (TYPE_VECTOR_SUBPARTS (type),
4252 TYPE_VECTOR_SUBPARTS (TREE_TYPE (@1)))
4253 && (TYPE_MODE (TREE_TYPE (type))
4254 == TYPE_MODE (TREE_TYPE (TREE_TYPE (@1)))))
4255 (plus @3 (view_convert (vec_cond @0 (negate @1) @2)))))
4256
4257
4258 /* Simplifications of comparisons. */
4259
4260 /* See if we can reduce the magnitude of a constant involved in a
4261 comparison by changing the comparison code. This is a canonicalization
4262 formerly done by maybe_canonicalize_comparison_1. */
4263 (for cmp (le gt)
4264 acmp (lt ge)
4265 (simplify
4266 (cmp @0 uniform_integer_cst_p@1)
4267 (with { tree cst = uniform_integer_cst_p (@1); }
4268 (if (tree_int_cst_sgn (cst) == -1)
4269 (acmp @0 { build_uniform_cst (TREE_TYPE (@1),
4270 wide_int_to_tree (TREE_TYPE (cst),
4271 wi::to_wide (cst)
4272 + 1)); })))))
4273 (for cmp (ge lt)
4274 acmp (gt le)
4275 (simplify
4276 (cmp @0 uniform_integer_cst_p@1)
4277 (with { tree cst = uniform_integer_cst_p (@1); }
4278 (if (tree_int_cst_sgn (cst) == 1)
4279 (acmp @0 { build_uniform_cst (TREE_TYPE (@1),
4280 wide_int_to_tree (TREE_TYPE (cst),
4281 wi::to_wide (cst) - 1)); })))))
4282
4283 /* We can simplify a logical negation of a comparison to the
4284 inverted comparison. As we cannot compute an expression
4285 operator using invert_tree_comparison we have to simulate
4286 that with expression code iteration. */
4287 (for cmp (tcc_comparison)
4288 icmp (inverted_tcc_comparison)
4289 ncmp (inverted_tcc_comparison_with_nans)
4290 /* Ideally we'd like to combine the following two patterns
4291 and handle some more cases by using
4292 (logical_inverted_value (cmp @0 @1))
4293 here but for that genmatch would need to "inline" that.
4294 For now implement what forward_propagate_comparison did. */
4295 (simplify
4296 (bit_not (cmp @0 @1))
4297 (if (VECTOR_TYPE_P (type)
4298 || (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1))
4299 /* Comparison inversion may be impossible for trapping math,
4300 invert_tree_comparison will tell us. But we can't use
4301 a computed operator in the replacement tree thus we have
4302 to play the trick below. */
4303 (with { enum tree_code ic = invert_tree_comparison
4304 (cmp, HONOR_NANS (@0)); }
4305 (if (ic == icmp)
4306 (icmp @0 @1)
4307 (if (ic == ncmp)
4308 (ncmp @0 @1))))))
4309 (simplify
4310 (bit_xor (cmp @0 @1) integer_truep)
4311 (with { enum tree_code ic = invert_tree_comparison
4312 (cmp, HONOR_NANS (@0)); }
4313 (if (ic == icmp)
4314 (icmp @0 @1)
4315 (if (ic == ncmp)
4316 (ncmp @0 @1))))))
4317
4318 /* Transform comparisons of the form X - Y CMP 0 to X CMP Y.
4319 ??? The transformation is valid for the other operators if overflow
4320 is undefined for the type, but performing it here badly interacts
4321 with the transformation in fold_cond_expr_with_comparison which
4322 attempts to synthetize ABS_EXPR. */
4323 (for cmp (eq ne)
4324 (for sub (minus pointer_diff)
4325 (simplify
4326 (cmp (sub@2 @0 @1) integer_zerop)
4327 (if (single_use (@2))
4328 (cmp @0 @1)))))
4329
4330 /* Simplify (x < 0) ^ (y < 0) to (x ^ y) < 0 and
4331 (x >= 0) ^ (y >= 0) to (x ^ y) < 0. */
4332 (for cmp (lt ge)
4333 (simplify
4334 (bit_xor (cmp:s @0 integer_zerop) (cmp:s @1 integer_zerop))
4335 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
4336 && !TYPE_UNSIGNED (TREE_TYPE (@0))
4337 && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
4338 (lt (bit_xor @0 @1) { build_zero_cst (TREE_TYPE (@0)); }))))
4339 /* Simplify (x < 0) ^ (y >= 0) to (x ^ y) >= 0 and
4340 (x >= 0) ^ (y < 0) to (x ^ y) >= 0. */
4341 (simplify
4342 (bit_xor:c (lt:s @0 integer_zerop) (ge:s @1 integer_zerop))
4343 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
4344 && !TYPE_UNSIGNED (TREE_TYPE (@0))
4345 && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
4346 (ge (bit_xor @0 @1) { build_zero_cst (TREE_TYPE (@0)); })))
4347
4348 /* Transform comparisons of the form X * C1 CMP 0 to X CMP 0 in the
4349 signed arithmetic case. That form is created by the compiler
4350 often enough for folding it to be of value. One example is in
4351 computing loop trip counts after Operator Strength Reduction. */
4352 (for cmp (simple_comparison)
4353 scmp (swapped_simple_comparison)
4354 (simplify
4355 (cmp (mult@3 @0 INTEGER_CST@1) integer_zerop@2)
4356 /* Handle unfolded multiplication by zero. */
4357 (if (integer_zerop (@1))
4358 (cmp @1 @2)
4359 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
4360 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
4361 && single_use (@3))
4362 /* If @1 is negative we swap the sense of the comparison. */
4363 (if (tree_int_cst_sgn (@1) < 0)
4364 (scmp @0 @2)
4365 (cmp @0 @2))))))
4366
4367 /* For integral types with undefined overflow fold
4368 x * C1 == C2 into x == C2 / C1 or false.
4369 If overflow wraps and C1 is odd, simplify to x == C2 / C1 in the ring
4370 Z / 2^n Z. */
4371 (for cmp (eq ne)
4372 (simplify
4373 (cmp (mult @0 INTEGER_CST@1) INTEGER_CST@2)
4374 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
4375 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
4376 && wi::to_wide (@1) != 0)
4377 (with { widest_int quot; }
4378 (if (wi::multiple_of_p (wi::to_widest (@2), wi::to_widest (@1),
4379 TYPE_SIGN (TREE_TYPE (@0)), &quot))
4380 (cmp @0 { wide_int_to_tree (TREE_TYPE (@0), quot); })
4381 { constant_boolean_node (cmp == NE_EXPR, type); }))
4382 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
4383 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))
4384 && (wi::bit_and (wi::to_wide (@1), 1) == 1))
4385 (cmp @0
4386 {
4387 tree itype = TREE_TYPE (@0);
4388 int p = TYPE_PRECISION (itype);
4389 wide_int m = wi::one (p + 1) << p;
4390 wide_int a = wide_int::from (wi::to_wide (@1), p + 1, UNSIGNED);
4391 wide_int i = wide_int::from (wi::mod_inv (a, m),
4392 p, TYPE_SIGN (itype));
4393 wide_int_to_tree (itype, wi::mul (i, wi::to_wide (@2)));
4394 })))))
4395
4396 /* Simplify comparison of something with itself. For IEEE
4397 floating-point, we can only do some of these simplifications. */
4398 (for cmp (eq ge le)
4399 (simplify
4400 (cmp @0 @0)
4401 (if (! FLOAT_TYPE_P (TREE_TYPE (@0))
4402 || ! HONOR_NANS (@0))
4403 { constant_boolean_node (true, type); }
4404 (if (cmp != EQ_EXPR)
4405 (eq @0 @0)))))
4406 (for cmp (ne gt lt)
4407 (simplify
4408 (cmp @0 @0)
4409 (if (cmp != NE_EXPR
4410 || ! FLOAT_TYPE_P (TREE_TYPE (@0))
4411 || ! HONOR_NANS (@0))
4412 { constant_boolean_node (false, type); })))
4413 (for cmp (unle unge uneq)
4414 (simplify
4415 (cmp @0 @0)
4416 { constant_boolean_node (true, type); }))
4417 (for cmp (unlt ungt)
4418 (simplify
4419 (cmp @0 @0)
4420 (unordered @0 @0)))
4421 (simplify
4422 (ltgt @0 @0)
4423 (if (!flag_trapping_math)
4424 { constant_boolean_node (false, type); }))
4425
4426 /* x == ~x -> false */
4427 /* x != ~x -> true */
4428 (for cmp (eq ne)
4429 (simplify
4430 (cmp:c @0 (bit_not @0))
4431 { constant_boolean_node (cmp == NE_EXPR, type); }))
4432
4433 /* Fold ~X op ~Y as Y op X. */
4434 (for cmp (simple_comparison)
4435 (simplify
4436 (cmp (bit_not@2 @0) (bit_not@3 @1))
4437 (if (single_use (@2) && single_use (@3))
4438 (cmp @1 @0))))
4439
4440 /* Fold ~X op C as X op' ~C, where op' is the swapped comparison. */
4441 (for cmp (simple_comparison)
4442 scmp (swapped_simple_comparison)
4443 (simplify
4444 (cmp (bit_not@2 @0) CONSTANT_CLASS_P@1)
4445 (if (single_use (@2)
4446 && (TREE_CODE (@1) == INTEGER_CST || TREE_CODE (@1) == VECTOR_CST))
4447 (scmp @0 (bit_not @1)))))
4448
4449 (for cmp (simple_comparison)
4450 /* Fold (double)float1 CMP (double)float2 into float1 CMP float2. */
4451 (simplify
4452 (cmp (convert@2 @0) (convert? @1))
4453 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
4454 && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@2))
4455 == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0)))
4456 && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@2))
4457 == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1))))
4458 (with
4459 {
4460 tree type1 = TREE_TYPE (@1);
4461 if (TREE_CODE (@1) == REAL_CST && !DECIMAL_FLOAT_TYPE_P (type1))
4462 {
4463 REAL_VALUE_TYPE orig = TREE_REAL_CST (@1);
4464 if (TYPE_PRECISION (type1) > TYPE_PRECISION (float_type_node)
4465 && exact_real_truncate (TYPE_MODE (float_type_node), &orig))
4466 type1 = float_type_node;
4467 if (TYPE_PRECISION (type1) > TYPE_PRECISION (double_type_node)
4468 && exact_real_truncate (TYPE_MODE (double_type_node), &orig))
4469 type1 = double_type_node;
4470 }
4471 tree newtype
4472 = (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type1)
4473 ? TREE_TYPE (@0) : type1);
4474 }
4475 (if (TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (newtype))
4476 (cmp (convert:newtype @0) (convert:newtype @1))))))
4477
4478 (simplify
4479 (cmp @0 REAL_CST@1)
4480 /* IEEE doesn't distinguish +0 and -0 in comparisons. */
4481 (switch
4482 /* a CMP (-0) -> a CMP 0 */
4483 (if (REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@1)))
4484 (cmp @0 { build_real (TREE_TYPE (@1), dconst0); }))
4485 /* (-0) CMP b -> 0 CMP b. */
4486 (if (TREE_CODE (@0) == REAL_CST
4487 && REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@0)))
4488 (cmp { build_real (TREE_TYPE (@0), dconst0); } @1))
4489 /* x != NaN is always true, other ops are always false. */
4490 (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
4491 && !tree_expr_signaling_nan_p (@1)
4492 && !tree_expr_maybe_signaling_nan_p (@0))
4493 { constant_boolean_node (cmp == NE_EXPR, type); })
4494 /* NaN != y is always true, other ops are always false. */
4495 (if (TREE_CODE (@0) == REAL_CST
4496 && REAL_VALUE_ISNAN (TREE_REAL_CST (@0))
4497 && !tree_expr_signaling_nan_p (@0)
4498 && !tree_expr_signaling_nan_p (@1))
4499 { constant_boolean_node (cmp == NE_EXPR, type); })
4500 /* Fold comparisons against infinity. */
4501 (if (REAL_VALUE_ISINF (TREE_REAL_CST (@1))
4502 && MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (@1))))
4503 (with
4504 {
4505 REAL_VALUE_TYPE max;
4506 enum tree_code code = cmp;
4507 bool neg = REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1));
4508 if (neg)
4509 code = swap_tree_comparison (code);
4510 }
4511 (switch
4512 /* x > +Inf is always false, if we ignore NaNs or exceptions. */
4513 (if (code == GT_EXPR
4514 && !(HONOR_NANS (@0) && flag_trapping_math))
4515 { constant_boolean_node (false, type); })
4516 (if (code == LE_EXPR)
4517 /* x <= +Inf is always true, if we don't care about NaNs. */
4518 (if (! HONOR_NANS (@0))
4519 { constant_boolean_node (true, type); }
4520 /* x <= +Inf is the same as x == x, i.e. !isnan(x), but this loses
4521 an "invalid" exception. */
4522 (if (!flag_trapping_math)
4523 (eq @0 @0))))
4524 /* x == +Inf and x >= +Inf are always equal to x > DBL_MAX, but
4525 for == this introduces an exception for x a NaN. */
4526 (if ((code == EQ_EXPR && !(HONOR_NANS (@0) && flag_trapping_math))
4527 || code == GE_EXPR)
4528 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
4529 (if (neg)
4530 (lt @0 { build_real (TREE_TYPE (@0), max); })
4531 (gt @0 { build_real (TREE_TYPE (@0), max); }))))
4532 /* x < +Inf is always equal to x <= DBL_MAX. */
4533 (if (code == LT_EXPR)
4534 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
4535 (if (neg)
4536 (ge @0 { build_real (TREE_TYPE (@0), max); })
4537 (le @0 { build_real (TREE_TYPE (@0), max); }))))
4538 /* x != +Inf is always equal to !(x > DBL_MAX), but this introduces
4539 an exception for x a NaN so use an unordered comparison. */
4540 (if (code == NE_EXPR)
4541 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
4542 (if (! HONOR_NANS (@0))
4543 (if (neg)
4544 (ge @0 { build_real (TREE_TYPE (@0), max); })
4545 (le @0 { build_real (TREE_TYPE (@0), max); }))
4546 (if (neg)
4547 (unge @0 { build_real (TREE_TYPE (@0), max); })
4548 (unle @0 { build_real (TREE_TYPE (@0), max); }))))))))))
4549
4550 /* If this is a comparison of a real constant with a PLUS_EXPR
4551 or a MINUS_EXPR of a real constant, we can convert it into a
4552 comparison with a revised real constant as long as no overflow
4553 occurs when unsafe_math_optimizations are enabled. */
4554 (if (flag_unsafe_math_optimizations)
4555 (for op (plus minus)
4556 (simplify
4557 (cmp (op @0 REAL_CST@1) REAL_CST@2)
4558 (with
4559 {
4560 tree tem = const_binop (op == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR,
4561 TREE_TYPE (@1), @2, @1);
4562 }
4563 (if (tem && !TREE_OVERFLOW (tem))
4564 (cmp @0 { tem; }))))))
4565
4566 /* Likewise, we can simplify a comparison of a real constant with
4567 a MINUS_EXPR whose first operand is also a real constant, i.e.
4568 (c1 - x) < c2 becomes x > c1-c2. Reordering is allowed on
4569 floating-point types only if -fassociative-math is set. */
4570 (if (flag_associative_math)
4571 (simplify
4572 (cmp (minus REAL_CST@0 @1) REAL_CST@2)
4573 (with { tree tem = const_binop (MINUS_EXPR, TREE_TYPE (@1), @0, @2); }
4574 (if (tem && !TREE_OVERFLOW (tem))
4575 (cmp { tem; } @1)))))
4576
4577 /* Fold comparisons against built-in math functions. */
4578 (if (flag_unsafe_math_optimizations && ! flag_errno_math)
4579 (for sq (SQRT)
4580 (simplify
4581 (cmp (sq @0) REAL_CST@1)
4582 (switch
4583 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
4584 (switch
4585 /* sqrt(x) < y is always false, if y is negative. */
4586 (if (cmp == EQ_EXPR || cmp == LT_EXPR || cmp == LE_EXPR)
4587 { constant_boolean_node (false, type); })
4588 /* sqrt(x) > y is always true, if y is negative and we
4589 don't care about NaNs, i.e. negative values of x. */
4590 (if (cmp == NE_EXPR || !HONOR_NANS (@0))
4591 { constant_boolean_node (true, type); })
4592 /* sqrt(x) > y is the same as x >= 0, if y is negative. */
4593 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })))
4594 (if (real_equal (TREE_REAL_CST_PTR (@1), &dconst0))
4595 (switch
4596 /* sqrt(x) < 0 is always false. */
4597 (if (cmp == LT_EXPR)
4598 { constant_boolean_node (false, type); })
4599 /* sqrt(x) >= 0 is always true if we don't care about NaNs. */
4600 (if (cmp == GE_EXPR && !HONOR_NANS (@0))
4601 { constant_boolean_node (true, type); })
4602 /* sqrt(x) <= 0 -> x == 0. */
4603 (if (cmp == LE_EXPR)
4604 (eq @0 @1))
4605 /* Otherwise sqrt(x) cmp 0 -> x cmp 0. Here cmp can be >=, >,
4606 == or !=. In the last case:
4607
4608 (sqrt(x) != 0) == (NaN != 0) == true == (x != 0)
4609
4610 if x is negative or NaN. Due to -funsafe-math-optimizations,
4611 the results for other x follow from natural arithmetic. */
4612 (cmp @0 @1)))
4613 (if ((cmp == LT_EXPR
4614 || cmp == LE_EXPR
4615 || cmp == GT_EXPR
4616 || cmp == GE_EXPR)
4617 && !REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
4618 /* Give up for -frounding-math. */
4619 && !HONOR_SIGN_DEPENDENT_ROUNDING (TREE_TYPE (@0)))
4620 (with
4621 {
4622 REAL_VALUE_TYPE c2;
4623 enum tree_code ncmp = cmp;
4624 const real_format *fmt
4625 = REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (@0)));
4626 real_arithmetic (&c2, MULT_EXPR,
4627 &TREE_REAL_CST (@1), &TREE_REAL_CST (@1));
4628 real_convert (&c2, fmt, &c2);
4629 /* See PR91734: if c2 is inexact and sqrt(c2) < c (or sqrt(c2) >= c),
4630 then change LT_EXPR into LE_EXPR or GE_EXPR into GT_EXPR. */
4631 if (!REAL_VALUE_ISINF (c2))
4632 {
4633 tree c3 = fold_const_call (CFN_SQRT, TREE_TYPE (@0),
4634 build_real (TREE_TYPE (@0), c2));
4635 if (c3 == NULL_TREE || TREE_CODE (c3) != REAL_CST)
4636 ncmp = ERROR_MARK;
4637 else if ((cmp == LT_EXPR || cmp == GE_EXPR)
4638 && real_less (&TREE_REAL_CST (c3), &TREE_REAL_CST (@1)))
4639 ncmp = cmp == LT_EXPR ? LE_EXPR : GT_EXPR;
4640 else if ((cmp == LE_EXPR || cmp == GT_EXPR)
4641 && real_less (&TREE_REAL_CST (@1), &TREE_REAL_CST (c3)))
4642 ncmp = cmp == LE_EXPR ? LT_EXPR : GE_EXPR;
4643 else
4644 {
4645 /* With rounding to even, sqrt of up to 3 different values
4646 gives the same normal result, so in some cases c2 needs
4647 to be adjusted. */
4648 REAL_VALUE_TYPE c2alt, tow;
4649 if (cmp == LT_EXPR || cmp == GE_EXPR)
4650 tow = dconst0;
4651 else
4652 real_inf (&tow);
4653 real_nextafter (&c2alt, fmt, &c2, &tow);
4654 real_convert (&c2alt, fmt, &c2alt);
4655 if (REAL_VALUE_ISINF (c2alt))
4656 ncmp = ERROR_MARK;
4657 else
4658 {
4659 c3 = fold_const_call (CFN_SQRT, TREE_TYPE (@0),
4660 build_real (TREE_TYPE (@0), c2alt));
4661 if (c3 == NULL_TREE || TREE_CODE (c3) != REAL_CST)
4662 ncmp = ERROR_MARK;
4663 else if (real_equal (&TREE_REAL_CST (c3),
4664 &TREE_REAL_CST (@1)))
4665 c2 = c2alt;
4666 }
4667 }
4668 }
4669 }
4670 (if (cmp == GT_EXPR || cmp == GE_EXPR)
4671 (if (REAL_VALUE_ISINF (c2))
4672 /* sqrt(x) > y is x == +Inf, when y is very large. */
4673 (if (HONOR_INFINITIES (@0))
4674 (eq @0 { build_real (TREE_TYPE (@0), c2); })
4675 { constant_boolean_node (false, type); })
4676 /* sqrt(x) > c is the same as x > c*c. */
4677 (if (ncmp != ERROR_MARK)
4678 (if (ncmp == GE_EXPR)
4679 (ge @0 { build_real (TREE_TYPE (@0), c2); })
4680 (gt @0 { build_real (TREE_TYPE (@0), c2); }))))
4681 /* else if (cmp == LT_EXPR || cmp == LE_EXPR) */
4682 (if (REAL_VALUE_ISINF (c2))
4683 (switch
4684 /* sqrt(x) < y is always true, when y is a very large
4685 value and we don't care about NaNs or Infinities. */
4686 (if (! HONOR_NANS (@0) && ! HONOR_INFINITIES (@0))
4687 { constant_boolean_node (true, type); })
4688 /* sqrt(x) < y is x != +Inf when y is very large and we
4689 don't care about NaNs. */
4690 (if (! HONOR_NANS (@0))
4691 (ne @0 { build_real (TREE_TYPE (@0), c2); }))
4692 /* sqrt(x) < y is x >= 0 when y is very large and we
4693 don't care about Infinities. */
4694 (if (! HONOR_INFINITIES (@0))
4695 (ge @0 { build_real (TREE_TYPE (@0), dconst0); }))
4696 /* sqrt(x) < y is x >= 0 && x != +Inf, when y is large. */
4697 (if (GENERIC)
4698 (truth_andif
4699 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
4700 (ne @0 { build_real (TREE_TYPE (@0), c2); }))))
4701 /* sqrt(x) < c is the same as x < c*c, if we ignore NaNs. */
4702 (if (ncmp != ERROR_MARK && ! HONOR_NANS (@0))
4703 (if (ncmp == LT_EXPR)
4704 (lt @0 { build_real (TREE_TYPE (@0), c2); })
4705 (le @0 { build_real (TREE_TYPE (@0), c2); }))
4706 /* sqrt(x) < c is the same as x >= 0 && x < c*c. */
4707 (if (ncmp != ERROR_MARK && GENERIC)
4708 (if (ncmp == LT_EXPR)
4709 (truth_andif
4710 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
4711 (lt @0 { build_real (TREE_TYPE (@0), c2); }))
4712 (truth_andif
4713 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
4714 (le @0 { build_real (TREE_TYPE (@0), c2); })))))))))))
4715 /* Transform sqrt(x) cmp sqrt(y) -> x cmp y. */
4716 (simplify
4717 (cmp (sq @0) (sq @1))
4718 (if (! HONOR_NANS (@0))
4719 (cmp @0 @1))))))
4720
4721 /* Optimize various special cases of (FTYPE) N CMP (FTYPE) M. */
4722 (for cmp (lt le eq ne ge gt unordered ordered unlt unle ungt unge uneq ltgt)
4723 icmp (lt le eq ne ge gt unordered ordered lt le gt ge eq ne)
4724 (simplify
4725 (cmp (float@0 @1) (float @2))
4726 (if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (@0))
4727 && ! DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0)))
4728 (with
4729 {
4730 format_helper fmt (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (@0))));
4731 tree type1 = TREE_TYPE (@1);
4732 bool type1_signed_p = TYPE_SIGN (type1) == SIGNED;
4733 tree type2 = TREE_TYPE (@2);
4734 bool type2_signed_p = TYPE_SIGN (type2) == SIGNED;
4735 }
4736 (if (fmt.can_represent_integral_type_p (type1)
4737 && fmt.can_represent_integral_type_p (type2))
4738 (if (cmp == ORDERED_EXPR || cmp == UNORDERED_EXPR)
4739 { constant_boolean_node (cmp == ORDERED_EXPR, type); }
4740 (if (TYPE_PRECISION (type1) > TYPE_PRECISION (type2)
4741 && type1_signed_p >= type2_signed_p)
4742 (icmp @1 (convert @2))
4743 (if (TYPE_PRECISION (type1) < TYPE_PRECISION (type2)
4744 && type1_signed_p <= type2_signed_p)
4745 (icmp (convert:type2 @1) @2)
4746 (if (TYPE_PRECISION (type1) == TYPE_PRECISION (type2)
4747 && type1_signed_p == type2_signed_p)
4748 (icmp @1 @2))))))))))
4749
4750 /* Optimize various special cases of (FTYPE) N CMP CST. */
4751 (for cmp (lt le eq ne ge gt)
4752 icmp (le le eq ne ge ge)
4753 (simplify
4754 (cmp (float @0) REAL_CST@1)
4755 (if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (@1))
4756 && ! DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1)))
4757 (with
4758 {
4759 tree itype = TREE_TYPE (@0);
4760 format_helper fmt (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (@1))));
4761 const REAL_VALUE_TYPE *cst = TREE_REAL_CST_PTR (@1);
4762 /* Be careful to preserve any potential exceptions due to
4763 NaNs. qNaNs are ok in == or != context.
4764 TODO: relax under -fno-trapping-math or
4765 -fno-signaling-nans. */
4766 bool exception_p
4767 = real_isnan (cst) && (cst->signalling
4768 || (cmp != EQ_EXPR && cmp != NE_EXPR));
4769 }
4770 /* TODO: allow non-fitting itype and SNaNs when
4771 -fno-trapping-math. */
4772 (if (fmt.can_represent_integral_type_p (itype) && ! exception_p)
4773 (with
4774 {
4775 signop isign = TYPE_SIGN (itype);
4776 REAL_VALUE_TYPE imin, imax;
4777 real_from_integer (&imin, fmt, wi::min_value (itype), isign);
4778 real_from_integer (&imax, fmt, wi::max_value (itype), isign);
4779
4780 REAL_VALUE_TYPE icst;
4781 if (cmp == GT_EXPR || cmp == GE_EXPR)
4782 real_ceil (&icst, fmt, cst);
4783 else if (cmp == LT_EXPR || cmp == LE_EXPR)
4784 real_floor (&icst, fmt, cst);
4785 else
4786 real_trunc (&icst, fmt, cst);
4787
4788 bool cst_int_p = !real_isnan (cst) && real_identical (&icst, cst);
4789
4790 bool overflow_p = false;
4791 wide_int icst_val
4792 = real_to_integer (&icst, &overflow_p, TYPE_PRECISION (itype));
4793 }
4794 (switch
4795 /* Optimize cases when CST is outside of ITYPE's range. */
4796 (if (real_compare (LT_EXPR, cst, &imin))
4797 { constant_boolean_node (cmp == GT_EXPR || cmp == GE_EXPR || cmp == NE_EXPR,
4798 type); })
4799 (if (real_compare (GT_EXPR, cst, &imax))
4800 { constant_boolean_node (cmp == LT_EXPR || cmp == LE_EXPR || cmp == NE_EXPR,
4801 type); })
4802 /* Remove cast if CST is an integer representable by ITYPE. */
4803 (if (cst_int_p)
4804 (cmp @0 { gcc_assert (!overflow_p);
4805 wide_int_to_tree (itype, icst_val); })
4806 )
4807 /* When CST is fractional, optimize
4808 (FTYPE) N == CST -> 0
4809 (FTYPE) N != CST -> 1. */
4810 (if (cmp == EQ_EXPR || cmp == NE_EXPR)
4811 { constant_boolean_node (cmp == NE_EXPR, type); })
4812 /* Otherwise replace with sensible integer constant. */
4813 (with
4814 {
4815 gcc_checking_assert (!overflow_p);
4816 }
4817 (icmp @0 { wide_int_to_tree (itype, icst_val); })))))))))
4818
4819 /* Fold A /[ex] B CMP C to A CMP B * C. */
4820 (for cmp (eq ne)
4821 (simplify
4822 (cmp (exact_div @0 @1) INTEGER_CST@2)
4823 (if (!integer_zerop (@1))
4824 (if (wi::to_wide (@2) == 0)
4825 (cmp @0 @2)
4826 (if (TREE_CODE (@1) == INTEGER_CST)
4827 (with
4828 {
4829 wi::overflow_type ovf;
4830 wide_int prod = wi::mul (wi::to_wide (@2), wi::to_wide (@1),
4831 TYPE_SIGN (TREE_TYPE (@1)), &ovf);
4832 }
4833 (if (ovf)
4834 { constant_boolean_node (cmp == NE_EXPR, type); }
4835 (cmp @0 { wide_int_to_tree (TREE_TYPE (@0), prod); }))))))))
4836 (for cmp (lt le gt ge)
4837 (simplify
4838 (cmp (exact_div @0 INTEGER_CST@1) INTEGER_CST@2)
4839 (if (wi::gt_p (wi::to_wide (@1), 0, TYPE_SIGN (TREE_TYPE (@1))))
4840 (with
4841 {
4842 wi::overflow_type ovf;
4843 wide_int prod = wi::mul (wi::to_wide (@2), wi::to_wide (@1),
4844 TYPE_SIGN (TREE_TYPE (@1)), &ovf);
4845 }
4846 (if (ovf)
4847 { constant_boolean_node (wi::lt_p (wi::to_wide (@2), 0,
4848 TYPE_SIGN (TREE_TYPE (@2)))
4849 != (cmp == LT_EXPR || cmp == LE_EXPR), type); }
4850 (cmp @0 { wide_int_to_tree (TREE_TYPE (@0), prod); }))))))
4851
4852 /* Fold (size_t)(A /[ex] B) CMP C to (size_t)A CMP (size_t)B * C or A CMP' 0.
4853
4854 For small C (less than max/B), this is (size_t)A CMP (size_t)B * C.
4855 For large C (more than min/B+2^size), this is also true, with the
4856 multiplication computed modulo 2^size.
4857 For intermediate C, this just tests the sign of A. */
4858 (for cmp (lt le gt ge)
4859 cmp2 (ge ge lt lt)
4860 (simplify
4861 (cmp (convert (exact_div @0 INTEGER_CST@1)) INTEGER_CST@2)
4862 (if (tree_nop_conversion_p (TREE_TYPE (@0), TREE_TYPE (@2))
4863 && TYPE_UNSIGNED (TREE_TYPE (@2)) && !TYPE_UNSIGNED (TREE_TYPE (@0))
4864 && wi::gt_p (wi::to_wide (@1), 0, TYPE_SIGN (TREE_TYPE (@1))))
4865 (with
4866 {
4867 tree utype = TREE_TYPE (@2);
4868 wide_int denom = wi::to_wide (@1);
4869 wide_int right = wi::to_wide (@2);
4870 wide_int smax = wi::sdiv_trunc (wi::max_value (TREE_TYPE (@0)), denom);
4871 wide_int smin = wi::sdiv_trunc (wi::min_value (TREE_TYPE (@0)), denom);
4872 bool small = wi::leu_p (right, smax);
4873 bool large = wi::geu_p (right, smin);
4874 }
4875 (if (small || large)
4876 (cmp (convert:utype @0) (mult @2 (convert @1)))
4877 (cmp2 @0 { build_zero_cst (TREE_TYPE (@0)); }))))))
4878
4879 /* Unordered tests if either argument is a NaN. */
4880 (simplify
4881 (bit_ior (unordered @0 @0) (unordered @1 @1))
4882 (if (types_match (@0, @1))
4883 (unordered @0 @1)))
4884 (simplify
4885 (bit_and (ordered @0 @0) (ordered @1 @1))
4886 (if (types_match (@0, @1))
4887 (ordered @0 @1)))
4888 (simplify
4889 (bit_ior:c (unordered @0 @0) (unordered:c@2 @0 @1))
4890 @2)
4891 (simplify
4892 (bit_and:c (ordered @0 @0) (ordered:c@2 @0 @1))
4893 @2)
4894
4895 /* Simple range test simplifications. */
4896 /* A < B || A >= B -> true. */
4897 (for test1 (lt le le le ne ge)
4898 test2 (ge gt ge ne eq ne)
4899 (simplify
4900 (bit_ior:c (test1 @0 @1) (test2 @0 @1))
4901 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
4902 || VECTOR_INTEGER_TYPE_P (TREE_TYPE (@0)))
4903 { constant_boolean_node (true, type); })))
4904 /* A < B && A >= B -> false. */
4905 (for test1 (lt lt lt le ne eq)
4906 test2 (ge gt eq gt eq gt)
4907 (simplify
4908 (bit_and:c (test1 @0 @1) (test2 @0 @1))
4909 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
4910 || VECTOR_INTEGER_TYPE_P (TREE_TYPE (@0)))
4911 { constant_boolean_node (false, type); })))
4912
4913 /* A & (2**N - 1) <= 2**K - 1 -> A & (2**N - 2**K) == 0
4914 A & (2**N - 1) > 2**K - 1 -> A & (2**N - 2**K) != 0
4915
4916 Note that comparisons
4917 A & (2**N - 1) < 2**K -> A & (2**N - 2**K) == 0
4918 A & (2**N - 1) >= 2**K -> A & (2**N - 2**K) != 0
4919 will be canonicalized to above so there's no need to
4920 consider them here.
4921 */
4922
4923 (for cmp (le gt)
4924 eqcmp (eq ne)
4925 (simplify
4926 (cmp (bit_and@0 @1 INTEGER_CST@2) INTEGER_CST@3)
4927 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)))
4928 (with
4929 {
4930 tree ty = TREE_TYPE (@0);
4931 unsigned prec = TYPE_PRECISION (ty);
4932 wide_int mask = wi::to_wide (@2, prec);
4933 wide_int rhs = wi::to_wide (@3, prec);
4934 signop sgn = TYPE_SIGN (ty);
4935 }
4936 (if ((mask & (mask + 1)) == 0 && wi::gt_p (rhs, 0, sgn)
4937 && (rhs & (rhs + 1)) == 0 && wi::ge_p (mask, rhs, sgn))
4938 (eqcmp (bit_and @1 { wide_int_to_tree (ty, mask - rhs); })
4939 { build_zero_cst (ty); }))))))
4940
4941 /* -A CMP -B -> B CMP A. */
4942 (for cmp (tcc_comparison)
4943 scmp (swapped_tcc_comparison)
4944 (simplify
4945 (cmp (negate @0) (negate @1))
4946 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
4947 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
4948 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
4949 (scmp @0 @1)))
4950 (simplify
4951 (cmp (negate @0) CONSTANT_CLASS_P@1)
4952 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
4953 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
4954 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
4955 (with { tree tem = const_unop (NEGATE_EXPR, TREE_TYPE (@0), @1); }
4956 (if (tem && !TREE_OVERFLOW (tem))
4957 (scmp @0 { tem; }))))))
4958
4959 /* Convert ABS_EXPR<x> == 0 or ABS_EXPR<x> != 0 to x == 0 or x != 0. */
4960 (for op (eq ne)
4961 (simplify
4962 (op (abs @0) zerop@1)
4963 (op @0 @1)))
4964
4965 /* From fold_sign_changed_comparison and fold_widened_comparison.
4966 FIXME: the lack of symmetry is disturbing. */
4967 (for cmp (simple_comparison)
4968 (simplify
4969 (cmp (convert@0 @00) (convert?@1 @10))
4970 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
4971 /* Disable this optimization if we're casting a function pointer
4972 type on targets that require function pointer canonicalization. */
4973 && !(targetm.have_canonicalize_funcptr_for_compare ()
4974 && ((POINTER_TYPE_P (TREE_TYPE (@00))
4975 && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@00))))
4976 || (POINTER_TYPE_P (TREE_TYPE (@10))
4977 && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@10))))))
4978 && single_use (@0))
4979 (if (TYPE_PRECISION (TREE_TYPE (@00)) == TYPE_PRECISION (TREE_TYPE (@0))
4980 && (TREE_CODE (@10) == INTEGER_CST
4981 || @1 != @10)
4982 && (TYPE_UNSIGNED (TREE_TYPE (@00)) == TYPE_UNSIGNED (TREE_TYPE (@0))
4983 || cmp == NE_EXPR
4984 || cmp == EQ_EXPR)
4985 && !POINTER_TYPE_P (TREE_TYPE (@00)))
4986 /* ??? The special-casing of INTEGER_CST conversion was in the original
4987 code and here to avoid a spurious overflow flag on the resulting
4988 constant which fold_convert produces. */
4989 (if (TREE_CODE (@1) == INTEGER_CST)
4990 (cmp @00 { force_fit_type (TREE_TYPE (@00), wi::to_widest (@1), 0,
4991 TREE_OVERFLOW (@1)); })
4992 (cmp @00 (convert @1)))
4993
4994 (if (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (TREE_TYPE (@00)))
4995 /* If possible, express the comparison in the shorter mode. */
4996 (if ((cmp == EQ_EXPR || cmp == NE_EXPR
4997 || TYPE_UNSIGNED (TREE_TYPE (@0)) == TYPE_UNSIGNED (TREE_TYPE (@00))
4998 || (!TYPE_UNSIGNED (TREE_TYPE (@0))
4999 && TYPE_UNSIGNED (TREE_TYPE (@00))))
5000 && (types_match (TREE_TYPE (@10), TREE_TYPE (@00))
5001 || ((TYPE_PRECISION (TREE_TYPE (@00))
5002 >= TYPE_PRECISION (TREE_TYPE (@10)))
5003 && (TYPE_UNSIGNED (TREE_TYPE (@00))
5004 == TYPE_UNSIGNED (TREE_TYPE (@10))))
5005 || (TREE_CODE (@10) == INTEGER_CST
5006 && INTEGRAL_TYPE_P (TREE_TYPE (@00))
5007 && int_fits_type_p (@10, TREE_TYPE (@00)))))
5008 (cmp @00 (convert @10))
5009 (if (TREE_CODE (@10) == INTEGER_CST
5010 && INTEGRAL_TYPE_P (TREE_TYPE (@00))
5011 && !int_fits_type_p (@10, TREE_TYPE (@00)))
5012 (with
5013 {
5014 tree min = lower_bound_in_type (TREE_TYPE (@10), TREE_TYPE (@00));
5015 tree max = upper_bound_in_type (TREE_TYPE (@10), TREE_TYPE (@00));
5016 bool above = integer_nonzerop (const_binop (LT_EXPR, type, max, @10));
5017 bool below = integer_nonzerop (const_binop (LT_EXPR, type, @10, min));
5018 }
5019 (if (above || below)
5020 (if (cmp == EQ_EXPR || cmp == NE_EXPR)
5021 { constant_boolean_node (cmp == EQ_EXPR ? false : true, type); }
5022 (if (cmp == LT_EXPR || cmp == LE_EXPR)
5023 { constant_boolean_node (above ? true : false, type); }
5024 (if (cmp == GT_EXPR || cmp == GE_EXPR)
5025 { constant_boolean_node (above ? false : true, type); }))))))))))))
5026
5027 (for cmp (eq ne)
5028 (simplify
5029 /* SSA names are canonicalized to 2nd place. */
5030 (cmp addr@0 SSA_NAME@1)
5031 (with
5032 { poly_int64 off; tree base; }
5033 /* A local variable can never be pointed to by
5034 the default SSA name of an incoming parameter. */
5035 (if (SSA_NAME_IS_DEFAULT_DEF (@1)
5036 && TREE_CODE (SSA_NAME_VAR (@1)) == PARM_DECL
5037 && (base = get_base_address (TREE_OPERAND (@0, 0)))
5038 && TREE_CODE (base) == VAR_DECL
5039 && auto_var_in_fn_p (base, current_function_decl))
5040 (if (cmp == NE_EXPR)
5041 { constant_boolean_node (true, type); }
5042 { constant_boolean_node (false, type); })
5043 /* If the address is based on @1 decide using the offset. */
5044 (if ((base = get_addr_base_and_unit_offset (TREE_OPERAND (@0, 0), &off))
5045 && TREE_CODE (base) == MEM_REF
5046 && TREE_OPERAND (base, 0) == @1)
5047 (with { off += mem_ref_offset (base).force_shwi (); }
5048 (if (known_ne (off, 0))
5049 { constant_boolean_node (cmp == NE_EXPR, type); }
5050 (if (known_eq (off, 0))
5051 { constant_boolean_node (cmp == EQ_EXPR, type); }))))))))
5052
5053 /* Equality compare simplifications from fold_binary */
5054 (for cmp (eq ne)
5055
5056 /* If we have (A | C) == D where C & ~D != 0, convert this into 0.
5057 Similarly for NE_EXPR. */
5058 (simplify
5059 (cmp (convert?@3 (bit_ior @0 INTEGER_CST@1)) INTEGER_CST@2)
5060 (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0))
5061 && wi::bit_and_not (wi::to_wide (@1), wi::to_wide (@2)) != 0)
5062 { constant_boolean_node (cmp == NE_EXPR, type); }))
5063
5064 /* (X ^ Y) == 0 becomes X == Y, and (X ^ Y) != 0 becomes X != Y. */
5065 (simplify
5066 (cmp (bit_xor @0 @1) integer_zerop)
5067 (cmp @0 @1))
5068
5069 /* (X ^ Y) == Y becomes X == 0.
5070 Likewise (X ^ Y) == X becomes Y == 0. */
5071 (simplify
5072 (cmp:c (bit_xor:c @0 @1) @0)
5073 (cmp @1 { build_zero_cst (TREE_TYPE (@1)); }))
5074
5075 #if GIMPLE
5076 /* (X & Y) == X becomes (X & ~Y) == 0. */
5077 (simplify
5078 (cmp:c (bit_and:c @0 @1) @0)
5079 (cmp (bit_and @0 (bit_not! @1)) { build_zero_cst (TREE_TYPE (@0)); }))
5080 (simplify
5081 (cmp:c (convert@3 (bit_and (convert@2 @0) INTEGER_CST@1)) (convert @0))
5082 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
5083 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
5084 && INTEGRAL_TYPE_P (TREE_TYPE (@3))
5085 && TYPE_PRECISION (TREE_TYPE (@2)) == TYPE_PRECISION (TREE_TYPE (@0))
5086 && TYPE_PRECISION (TREE_TYPE (@3)) > TYPE_PRECISION (TREE_TYPE (@2))
5087 && !wi::neg_p (wi::to_wide (@1)))
5088 (cmp (bit_and @0 (convert (bit_not @1)))
5089 { build_zero_cst (TREE_TYPE (@0)); })))
5090
5091 /* (X | Y) == Y becomes (X & ~Y) == 0. */
5092 (simplify
5093 (cmp:c (bit_ior:c @0 @1) @1)
5094 (cmp (bit_and @0 (bit_not! @1)) { build_zero_cst (TREE_TYPE (@0)); }))
5095 #endif
5096
5097 /* (X ^ C1) op C2 can be rewritten as X op (C1 ^ C2). */
5098 (simplify
5099 (cmp (convert?@3 (bit_xor @0 INTEGER_CST@1)) INTEGER_CST@2)
5100 (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0)))
5101 (cmp @0 (bit_xor @1 (convert @2)))))
5102
5103 (simplify
5104 (cmp (convert? addr@0) integer_zerop)
5105 (if (tree_single_nonzero_warnv_p (@0, NULL))
5106 { constant_boolean_node (cmp == NE_EXPR, type); }))
5107
5108 /* (X & C) op (Y & C) into (X ^ Y) & C op 0. */
5109 (simplify
5110 (cmp (bit_and:cs @0 @2) (bit_and:cs @1 @2))
5111 (cmp (bit_and (bit_xor @0 @1) @2) { build_zero_cst (TREE_TYPE (@2)); })))
5112
5113 /* (X < 0) != (Y < 0) into (X ^ Y) < 0.
5114 (X >= 0) != (Y >= 0) into (X ^ Y) < 0.
5115 (X < 0) == (Y < 0) into (X ^ Y) >= 0.
5116 (X >= 0) == (Y >= 0) into (X ^ Y) >= 0. */
5117 (for cmp (eq ne)
5118 ncmp (ge lt)
5119 (for sgncmp (ge lt)
5120 (simplify
5121 (cmp (sgncmp @0 integer_zerop@2) (sgncmp @1 integer_zerop))
5122 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
5123 && !TYPE_UNSIGNED (TREE_TYPE (@0))
5124 && types_match (@0, @1))
5125 (ncmp (bit_xor @0 @1) @2)))))
5126 /* (X < 0) == (Y >= 0) into (X ^ Y) < 0.
5127 (X < 0) != (Y >= 0) into (X ^ Y) >= 0. */
5128 (for cmp (eq ne)
5129 ncmp (lt ge)
5130 (simplify
5131 (cmp:c (lt @0 integer_zerop@2) (ge @1 integer_zerop))
5132 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
5133 && !TYPE_UNSIGNED (TREE_TYPE (@0))
5134 && types_match (@0, @1))
5135 (ncmp (bit_xor @0 @1) @2))))
5136
5137 /* If we have (A & C) == C where C is a power of 2, convert this into
5138 (A & C) != 0. Similarly for NE_EXPR. */
5139 (for cmp (eq ne)
5140 icmp (ne eq)
5141 (simplify
5142 (cmp (bit_and@2 @0 integer_pow2p@1) @1)
5143 (icmp @2 { build_zero_cst (TREE_TYPE (@0)); })))
5144
5145 (for cmp (ge lt)
5146 /* x < 0 ? ~y : y into (x >> (prec-1)) ^ y. */
5147 /* x >= 0 ? ~y : y into ~((x >> (prec-1)) ^ y). */
5148 (simplify
5149 (cond (cmp @0 integer_zerop) (bit_not @1) @1)
5150 (if (INTEGRAL_TYPE_P (type)
5151 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
5152 && !TYPE_UNSIGNED (TREE_TYPE (@0))
5153 && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (type))
5154 (with
5155 {
5156 tree shifter = build_int_cst (integer_type_node, TYPE_PRECISION (type) - 1);
5157 }
5158 (if (cmp == LT_EXPR)
5159 (bit_xor (convert (rshift @0 {shifter;})) @1)
5160 (bit_not (bit_xor (convert (rshift @0 {shifter;})) @1))))))
5161 /* x < 0 ? y : ~y into ~((x >> (prec-1)) ^ y). */
5162 /* x >= 0 ? y : ~y into (x >> (prec-1)) ^ y. */
5163 (simplify
5164 (cond (cmp @0 integer_zerop) @1 (bit_not @1))
5165 (if (INTEGRAL_TYPE_P (type)
5166 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
5167 && !TYPE_UNSIGNED (TREE_TYPE (@0))
5168 && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (type))
5169 (with
5170 {
5171 tree shifter = build_int_cst (integer_type_node, TYPE_PRECISION (type) - 1);
5172 }
5173 (if (cmp == GE_EXPR)
5174 (bit_xor (convert (rshift @0 {shifter;})) @1)
5175 (bit_not (bit_xor (convert (rshift @0 {shifter;})) @1)))))))
5176
5177 /* If we have (A & C) != 0 ? D : 0 where C and D are powers of 2,
5178 convert this into a shift followed by ANDing with D. */
5179 (simplify
5180 (cond
5181 (ne (bit_and @0 integer_pow2p@1) integer_zerop)
5182 INTEGER_CST@2 integer_zerop)
5183 (if (!POINTER_TYPE_P (type) && integer_pow2p (@2))
5184 (with {
5185 int shift = (wi::exact_log2 (wi::to_wide (@2))
5186 - wi::exact_log2 (wi::to_wide (@1)));
5187 }
5188 (if (shift > 0)
5189 (bit_and
5190 (lshift (convert @0) { build_int_cst (integer_type_node, shift); }) @2)
5191 (bit_and
5192 (convert (rshift @0 { build_int_cst (integer_type_node, -shift); }))
5193 @2)))))
5194
5195 /* If we have (A & C) != 0 where C is the sign bit of A, convert
5196 this into A < 0. Similarly for (A & C) == 0 into A >= 0. */
5197 (for cmp (eq ne)
5198 ncmp (ge lt)
5199 (simplify
5200 (cmp (bit_and (convert?@2 @0) integer_pow2p@1) integer_zerop)
5201 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
5202 && type_has_mode_precision_p (TREE_TYPE (@0))
5203 && element_precision (@2) >= element_precision (@0)
5204 && wi::only_sign_bit_p (wi::to_wide (@1), element_precision (@0)))
5205 (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
5206 (ncmp (convert:stype @0) { build_zero_cst (stype); })))))
5207
5208 /* If we have A < 0 ? C : 0 where C is a power of 2, convert
5209 this into a right shift or sign extension followed by ANDing with C. */
5210 (simplify
5211 (cond
5212 (lt @0 integer_zerop)
5213 INTEGER_CST@1 integer_zerop)
5214 (if (integer_pow2p (@1)
5215 && !TYPE_UNSIGNED (TREE_TYPE (@0)))
5216 (with {
5217 int shift = element_precision (@0) - wi::exact_log2 (wi::to_wide (@1)) - 1;
5218 }
5219 (if (shift >= 0)
5220 (bit_and
5221 (convert (rshift @0 { build_int_cst (integer_type_node, shift); }))
5222 @1)
5223 /* Otherwise ctype must be wider than TREE_TYPE (@0) and pure
5224 sign extension followed by AND with C will achieve the effect. */
5225 (bit_and (convert @0) @1)))))
5226
5227 /* When the addresses are not directly of decls compare base and offset.
5228 This implements some remaining parts of fold_comparison address
5229 comparisons but still no complete part of it. Still it is good
5230 enough to make fold_stmt not regress when not dispatching to fold_binary. */
5231 (for cmp (simple_comparison)
5232 (simplify
5233 (cmp (convert1?@2 addr@0) (convert2? addr@1))
5234 (with
5235 {
5236 poly_int64 off0, off1;
5237 tree base0 = get_addr_base_and_unit_offset (TREE_OPERAND (@0, 0), &off0);
5238 tree base1 = get_addr_base_and_unit_offset (TREE_OPERAND (@1, 0), &off1);
5239 if (base0 && TREE_CODE (base0) == MEM_REF)
5240 {
5241 off0 += mem_ref_offset (base0).force_shwi ();
5242 base0 = TREE_OPERAND (base0, 0);
5243 }
5244 if (base1 && TREE_CODE (base1) == MEM_REF)
5245 {
5246 off1 += mem_ref_offset (base1).force_shwi ();
5247 base1 = TREE_OPERAND (base1, 0);
5248 }
5249 }
5250 (if (base0 && base1)
5251 (with
5252 {
5253 int equal = 2;
5254 /* Punt in GENERIC on variables with value expressions;
5255 the value expressions might point to fields/elements
5256 of other vars etc. */
5257 if (GENERIC
5258 && ((VAR_P (base0) && DECL_HAS_VALUE_EXPR_P (base0))
5259 || (VAR_P (base1) && DECL_HAS_VALUE_EXPR_P (base1))))
5260 ;
5261 else if (decl_in_symtab_p (base0)
5262 && decl_in_symtab_p (base1))
5263 equal = symtab_node::get_create (base0)
5264 ->equal_address_to (symtab_node::get_create (base1));
5265 else if ((DECL_P (base0)
5266 || TREE_CODE (base0) == SSA_NAME
5267 || TREE_CODE (base0) == STRING_CST)
5268 && (DECL_P (base1)
5269 || TREE_CODE (base1) == SSA_NAME
5270 || TREE_CODE (base1) == STRING_CST))
5271 equal = (base0 == base1);
5272 if (equal == 0)
5273 {
5274 HOST_WIDE_INT ioff0 = -1, ioff1 = -1;
5275 off0.is_constant (&ioff0);
5276 off1.is_constant (&ioff1);
5277 if ((DECL_P (base0) && TREE_CODE (base1) == STRING_CST)
5278 || (TREE_CODE (base0) == STRING_CST && DECL_P (base1))
5279 || (TREE_CODE (base0) == STRING_CST
5280 && TREE_CODE (base1) == STRING_CST
5281 && ioff0 >= 0 && ioff1 >= 0
5282 && ioff0 < TREE_STRING_LENGTH (base0)
5283 && ioff1 < TREE_STRING_LENGTH (base1)
5284 /* This is a too conservative test that the STRING_CSTs
5285 will not end up being string-merged. */
5286 && strncmp (TREE_STRING_POINTER (base0) + ioff0,
5287 TREE_STRING_POINTER (base1) + ioff1,
5288 MIN (TREE_STRING_LENGTH (base0) - ioff0,
5289 TREE_STRING_LENGTH (base1) - ioff1)) != 0))
5290 ;
5291 else if (!DECL_P (base0) || !DECL_P (base1))
5292 equal = 2;
5293 else if (cmp != EQ_EXPR && cmp != NE_EXPR)
5294 equal = 2;
5295 /* If this is a pointer comparison, ignore for now even
5296 valid equalities where one pointer is the offset zero
5297 of one object and the other to one past end of another one. */
5298 else if (!INTEGRAL_TYPE_P (TREE_TYPE (@2)))
5299 ;
5300 /* Assume that automatic variables can't be adjacent to global
5301 variables. */
5302 else if (is_global_var (base0) != is_global_var (base1))
5303 ;
5304 else
5305 {
5306 tree sz0 = DECL_SIZE_UNIT (base0);
5307 tree sz1 = DECL_SIZE_UNIT (base1);
5308 /* If sizes are unknown, e.g. VLA or not representable,
5309 punt. */
5310 if (!tree_fits_poly_int64_p (sz0)
5311 || !tree_fits_poly_int64_p (sz1))
5312 equal = 2;
5313 else
5314 {
5315 poly_int64 size0 = tree_to_poly_int64 (sz0);
5316 poly_int64 size1 = tree_to_poly_int64 (sz1);
5317 /* If one offset is pointing (or could be) to the beginning
5318 of one object and the other is pointing to one past the
5319 last byte of the other object, punt. */
5320 if (maybe_eq (off0, 0) && maybe_eq (off1, size1))
5321 equal = 2;
5322 else if (maybe_eq (off1, 0) && maybe_eq (off0, size0))
5323 equal = 2;
5324 /* If both offsets are the same, there are some cases
5325 we know that are ok. Either if we know they aren't
5326 zero, or if we know both sizes are no zero. */
5327 if (equal == 2
5328 && known_eq (off0, off1)
5329 && (known_ne (off0, 0)
5330 || (known_ne (size0, 0) && known_ne (size1, 0))))
5331 equal = 0;
5332 }
5333 }
5334 }
5335 }
5336 (if (equal == 1
5337 && (cmp == EQ_EXPR || cmp == NE_EXPR
5338 /* If the offsets are equal we can ignore overflow. */
5339 || known_eq (off0, off1)
5340 || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
5341 /* Or if we compare using pointers to decls or strings. */
5342 || (POINTER_TYPE_P (TREE_TYPE (@2))
5343 && (DECL_P (base0) || TREE_CODE (base0) == STRING_CST))))
5344 (switch
5345 (if (cmp == EQ_EXPR && (known_eq (off0, off1) || known_ne (off0, off1)))
5346 { constant_boolean_node (known_eq (off0, off1), type); })
5347 (if (cmp == NE_EXPR && (known_eq (off0, off1) || known_ne (off0, off1)))
5348 { constant_boolean_node (known_ne (off0, off1), type); })
5349 (if (cmp == LT_EXPR && (known_lt (off0, off1) || known_ge (off0, off1)))
5350 { constant_boolean_node (known_lt (off0, off1), type); })
5351 (if (cmp == LE_EXPR && (known_le (off0, off1) || known_gt (off0, off1)))
5352 { constant_boolean_node (known_le (off0, off1), type); })
5353 (if (cmp == GE_EXPR && (known_ge (off0, off1) || known_lt (off0, off1)))
5354 { constant_boolean_node (known_ge (off0, off1), type); })
5355 (if (cmp == GT_EXPR && (known_gt (off0, off1) || known_le (off0, off1)))
5356 { constant_boolean_node (known_gt (off0, off1), type); }))
5357 (if (equal == 0)
5358 (switch
5359 (if (cmp == EQ_EXPR)
5360 { constant_boolean_node (false, type); })
5361 (if (cmp == NE_EXPR)
5362 { constant_boolean_node (true, type); })))))))))
5363
5364 /* Simplify pointer equality compares using PTA. */
5365 (for neeq (ne eq)
5366 (simplify
5367 (neeq @0 @1)
5368 (if (POINTER_TYPE_P (TREE_TYPE (@0))
5369 && ptrs_compare_unequal (@0, @1))
5370 { constant_boolean_node (neeq != EQ_EXPR, type); })))
5371
5372 /* PR70920: Transform (intptr_t)x eq/ne CST to x eq/ne (typeof x) CST.
5373 and (typeof ptr_cst) x eq/ne ptr_cst to x eq/ne (typeof x) CST.
5374 Disable the transform if either operand is pointer to function.
5375 This broke pr22051-2.c for arm where function pointer
5376 canonicalizaion is not wanted. */
5377
5378 (for cmp (ne eq)
5379 (simplify
5380 (cmp (convert @0) INTEGER_CST@1)
5381 (if (((POINTER_TYPE_P (TREE_TYPE (@0))
5382 && !FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@0)))
5383 && INTEGRAL_TYPE_P (TREE_TYPE (@1))
5384 /* Don't perform this optimization in GENERIC if @0 has reference
5385 type when sanitizing. See PR101210. */
5386 && !(GENERIC
5387 && TREE_CODE (TREE_TYPE (@0)) == REFERENCE_TYPE
5388 && (flag_sanitize & (SANITIZE_NULL | SANITIZE_ALIGNMENT))))
5389 || (INTEGRAL_TYPE_P (TREE_TYPE (@0))
5390 && POINTER_TYPE_P (TREE_TYPE (@1))
5391 && !FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@1)))))
5392 && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
5393 (cmp @0 (convert @1)))))
5394
5395 /* Non-equality compare simplifications from fold_binary */
5396 (for cmp (lt gt le ge)
5397 /* Comparisons with the highest or lowest possible integer of
5398 the specified precision will have known values. */
5399 (simplify
5400 (cmp (convert?@2 @0) uniform_integer_cst_p@1)
5401 (if ((INTEGRAL_TYPE_P (TREE_TYPE (@1))
5402 || POINTER_TYPE_P (TREE_TYPE (@1))
5403 || VECTOR_INTEGER_TYPE_P (TREE_TYPE (@1)))
5404 && tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@0)))
5405 (with
5406 {
5407 tree cst = uniform_integer_cst_p (@1);
5408 tree arg1_type = TREE_TYPE (cst);
5409 unsigned int prec = TYPE_PRECISION (arg1_type);
5410 wide_int max = wi::max_value (arg1_type);
5411 wide_int signed_max = wi::max_value (prec, SIGNED);
5412 wide_int min = wi::min_value (arg1_type);
5413 }
5414 (switch
5415 (if (wi::to_wide (cst) == max)
5416 (switch
5417 (if (cmp == GT_EXPR)
5418 { constant_boolean_node (false, type); })
5419 (if (cmp == GE_EXPR)
5420 (eq @2 @1))
5421 (if (cmp == LE_EXPR)
5422 { constant_boolean_node (true, type); })
5423 (if (cmp == LT_EXPR)
5424 (ne @2 @1))))
5425 (if (wi::to_wide (cst) == min)
5426 (switch
5427 (if (cmp == LT_EXPR)
5428 { constant_boolean_node (false, type); })
5429 (if (cmp == LE_EXPR)
5430 (eq @2 @1))
5431 (if (cmp == GE_EXPR)
5432 { constant_boolean_node (true, type); })
5433 (if (cmp == GT_EXPR)
5434 (ne @2 @1))))
5435 (if (wi::to_wide (cst) == max - 1)
5436 (switch
5437 (if (cmp == GT_EXPR)
5438 (eq @2 { build_uniform_cst (TREE_TYPE (@1),
5439 wide_int_to_tree (TREE_TYPE (cst),
5440 wi::to_wide (cst)
5441 + 1)); }))
5442 (if (cmp == LE_EXPR)
5443 (ne @2 { build_uniform_cst (TREE_TYPE (@1),
5444 wide_int_to_tree (TREE_TYPE (cst),
5445 wi::to_wide (cst)
5446 + 1)); }))))
5447 (if (wi::to_wide (cst) == min + 1)
5448 (switch
5449 (if (cmp == GE_EXPR)
5450 (ne @2 { build_uniform_cst (TREE_TYPE (@1),
5451 wide_int_to_tree (TREE_TYPE (cst),
5452 wi::to_wide (cst)
5453 - 1)); }))
5454 (if (cmp == LT_EXPR)
5455 (eq @2 { build_uniform_cst (TREE_TYPE (@1),
5456 wide_int_to_tree (TREE_TYPE (cst),
5457 wi::to_wide (cst)
5458 - 1)); }))))
5459 (if (wi::to_wide (cst) == signed_max
5460 && TYPE_UNSIGNED (arg1_type)
5461 /* We will flip the signedness of the comparison operator
5462 associated with the mode of @1, so the sign bit is
5463 specified by this mode. Check that @1 is the signed
5464 max associated with this sign bit. */
5465 && prec == GET_MODE_PRECISION (SCALAR_INT_TYPE_MODE (arg1_type))
5466 /* signed_type does not work on pointer types. */
5467 && INTEGRAL_TYPE_P (arg1_type))
5468 /* The following case also applies to X < signed_max+1
5469 and X >= signed_max+1 because previous transformations. */
5470 (if (cmp == LE_EXPR || cmp == GT_EXPR)
5471 (with { tree st = signed_type_for (TREE_TYPE (@1)); }
5472 (switch
5473 (if (cst == @1 && cmp == LE_EXPR)
5474 (ge (convert:st @0) { build_zero_cst (st); }))
5475 (if (cst == @1 && cmp == GT_EXPR)
5476 (lt (convert:st @0) { build_zero_cst (st); }))
5477 (if (cmp == LE_EXPR)
5478 (ge (view_convert:st @0) { build_zero_cst (st); }))
5479 (if (cmp == GT_EXPR)
5480 (lt (view_convert:st @0) { build_zero_cst (st); })))))))))))
5481
5482 (for cmp (unordered ordered unlt unle ungt unge uneq ltgt)
5483 /* If the second operand is NaN, the result is constant. */
5484 (simplify
5485 (cmp @0 REAL_CST@1)
5486 (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
5487 && (cmp != LTGT_EXPR || ! flag_trapping_math))
5488 { constant_boolean_node (cmp == ORDERED_EXPR || cmp == LTGT_EXPR
5489 ? false : true, type); })))
5490
5491 /* Fold UNORDERED if either operand must be NaN, or neither can be. */
5492 (simplify
5493 (unordered @0 @1)
5494 (switch
5495 (if (tree_expr_nan_p (@0) || tree_expr_nan_p (@1))
5496 { constant_boolean_node (true, type); })
5497 (if (!tree_expr_maybe_nan_p (@0) && !tree_expr_maybe_nan_p (@1))
5498 { constant_boolean_node (false, type); })))
5499
5500 /* Fold ORDERED if either operand must be NaN, or neither can be. */
5501 (simplify
5502 (ordered @0 @1)
5503 (switch
5504 (if (tree_expr_nan_p (@0) || tree_expr_nan_p (@1))
5505 { constant_boolean_node (false, type); })
5506 (if (!tree_expr_maybe_nan_p (@0) && !tree_expr_maybe_nan_p (@1))
5507 { constant_boolean_node (true, type); })))
5508
5509 /* bool_var != 0 becomes bool_var. */
5510 (simplify
5511 (ne @0 integer_zerop)
5512 (if (TREE_CODE (TREE_TYPE (@0)) == BOOLEAN_TYPE
5513 && types_match (type, TREE_TYPE (@0)))
5514 (non_lvalue @0)))
5515 /* bool_var == 1 becomes bool_var. */
5516 (simplify
5517 (eq @0 integer_onep)
5518 (if (TREE_CODE (TREE_TYPE (@0)) == BOOLEAN_TYPE
5519 && types_match (type, TREE_TYPE (@0)))
5520 (non_lvalue @0)))
5521 /* Do not handle
5522 bool_var == 0 becomes !bool_var or
5523 bool_var != 1 becomes !bool_var
5524 here because that only is good in assignment context as long
5525 as we require a tcc_comparison in GIMPLE_CONDs where we'd
5526 replace if (x == 0) with tem = ~x; if (tem != 0) which is
5527 clearly less optimal and which we'll transform again in forwprop. */
5528
5529 /* When one argument is a constant, overflow detection can be simplified.
5530 Currently restricted to single use so as not to interfere too much with
5531 ADD_OVERFLOW detection in tree-ssa-math-opts.c.
5532 CONVERT?(CONVERT?(A) + CST) CMP A -> A CMP' CST' */
5533 (for cmp (lt le ge gt)
5534 out (gt gt le le)
5535 (simplify
5536 (cmp:c (convert?@3 (plus@2 (convert?@4 @0) INTEGER_CST@1)) @0)
5537 (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@2))
5538 && types_match (TREE_TYPE (@0), TREE_TYPE (@3))
5539 && tree_nop_conversion_p (TREE_TYPE (@4), TREE_TYPE (@0))
5540 && wi::to_wide (@1) != 0
5541 && single_use (@2))
5542 (with {
5543 unsigned int prec = TYPE_PRECISION (TREE_TYPE (@0));
5544 signop sign = TYPE_SIGN (TREE_TYPE (@0));
5545 }
5546 (out @0 { wide_int_to_tree (TREE_TYPE (@0),
5547 wi::max_value (prec, sign)
5548 - wi::to_wide (@1)); })))))
5549
5550 /* To detect overflow in unsigned A - B, A < B is simpler than A - B > A.
5551 However, the detection logic for SUB_OVERFLOW in tree-ssa-math-opts.c
5552 expects the long form, so we restrict the transformation for now. */
5553 (for cmp (gt le)
5554 (simplify
5555 (cmp:c (minus@2 @0 @1) @0)
5556 (if (single_use (@2)
5557 && ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
5558 && TYPE_UNSIGNED (TREE_TYPE (@0)))
5559 (cmp @1 @0))))
5560
5561 /* Optimize A - B + -1 >= A into B >= A for unsigned comparisons. */
5562 (for cmp (ge lt)
5563 (simplify
5564 (cmp:c (plus (minus @0 @1) integer_minus_onep) @0)
5565 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
5566 && TYPE_UNSIGNED (TREE_TYPE (@0)))
5567 (cmp @1 @0))))
5568
5569 /* Testing for overflow is unnecessary if we already know the result. */
5570 /* A - B > A */
5571 (for cmp (gt le)
5572 out (ne eq)
5573 (simplify
5574 (cmp:c (realpart (IFN_SUB_OVERFLOW@2 @0 @1)) @0)
5575 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
5576 && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
5577 (out (imagpart @2) { build_zero_cst (TREE_TYPE (@0)); }))))
5578 /* A + B < A */
5579 (for cmp (lt ge)
5580 out (ne eq)
5581 (simplify
5582 (cmp:c (realpart (IFN_ADD_OVERFLOW:c@2 @0 @1)) @0)
5583 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
5584 && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
5585 (out (imagpart @2) { build_zero_cst (TREE_TYPE (@0)); }))))
5586
5587 /* For unsigned operands, -1 / B < A checks whether A * B would overflow.
5588 Simplify it to __builtin_mul_overflow (A, B, <unused>). */
5589 (for cmp (lt ge)
5590 out (ne eq)
5591 (simplify
5592 (cmp:c (trunc_div:s integer_all_onesp @1) @0)
5593 (if (TYPE_UNSIGNED (TREE_TYPE (@0)) && !VECTOR_TYPE_P (TREE_TYPE (@0)))
5594 (with { tree t = TREE_TYPE (@0), cpx = build_complex_type (t); }
5595 (out (imagpart (IFN_MUL_OVERFLOW:cpx @0 @1)) { build_zero_cst (t); })))))
5596
5597 /* Similarly, for unsigned operands, (((type) A * B) >> prec) != 0 where type
5598 is at least twice as wide as type of A and B, simplify to
5599 __builtin_mul_overflow (A, B, <unused>). */
5600 (for cmp (eq ne)
5601 (simplify
5602 (cmp (rshift (mult:s (convert@3 @0) (convert @1)) INTEGER_CST@2)
5603 integer_zerop)
5604 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
5605 && INTEGRAL_TYPE_P (TREE_TYPE (@3))
5606 && TYPE_UNSIGNED (TREE_TYPE (@0))
5607 && (TYPE_PRECISION (TREE_TYPE (@3))
5608 >= 2 * TYPE_PRECISION (TREE_TYPE (@0)))
5609 && tree_fits_uhwi_p (@2)
5610 && tree_to_uhwi (@2) == TYPE_PRECISION (TREE_TYPE (@0))
5611 && types_match (@0, @1)
5612 && type_has_mode_precision_p (TREE_TYPE (@0))
5613 && (optab_handler (umulv4_optab, TYPE_MODE (TREE_TYPE (@0)))
5614 != CODE_FOR_nothing))
5615 (with { tree t = TREE_TYPE (@0), cpx = build_complex_type (t); }
5616 (cmp (imagpart (IFN_MUL_OVERFLOW:cpx @0 @1)) { build_zero_cst (t); })))))
5617
5618 /* Demote operands of IFN_{ADD,SUB,MUL}_OVERFLOW. */
5619 (for ovf (IFN_ADD_OVERFLOW IFN_SUB_OVERFLOW IFN_MUL_OVERFLOW)
5620 (simplify
5621 (ovf (convert@2 @0) @1)
5622 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
5623 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
5624 && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
5625 && (!TYPE_UNSIGNED (TREE_TYPE (@2)) || TYPE_UNSIGNED (TREE_TYPE (@0))))
5626 (ovf @0 @1)))
5627 (simplify
5628 (ovf @1 (convert@2 @0))
5629 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
5630 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
5631 && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
5632 && (!TYPE_UNSIGNED (TREE_TYPE (@2)) || TYPE_UNSIGNED (TREE_TYPE (@0))))
5633 (ovf @1 @0))))
5634
5635 /* Simplification of math builtins. These rules must all be optimizations
5636 as well as IL simplifications. If there is a possibility that the new
5637 form could be a pessimization, the rule should go in the canonicalization
5638 section that follows this one.
5639
5640 Rules can generally go in this section if they satisfy one of
5641 the following:
5642
5643 - the rule describes an identity
5644
5645 - the rule replaces calls with something as simple as addition or
5646 multiplication
5647
5648 - the rule contains unary calls only and simplifies the surrounding
5649 arithmetic. (The idea here is to exclude non-unary calls in which
5650 one operand is constant and in which the call is known to be cheap
5651 when the operand has that value.) */
5652
5653 (if (flag_unsafe_math_optimizations)
5654 /* Simplify sqrt(x) * sqrt(x) -> x. */
5655 (simplify
5656 (mult (SQRT_ALL@1 @0) @1)
5657 (if (!tree_expr_maybe_signaling_nan_p (@0))
5658 @0))
5659
5660 (for op (plus minus)
5661 /* Simplify (A / C) +- (B / C) -> (A +- B) / C. */
5662 (simplify
5663 (op (rdiv @0 @1)
5664 (rdiv @2 @1))
5665 (rdiv (op @0 @2) @1)))
5666
5667 (for cmp (lt le gt ge)
5668 neg_cmp (gt ge lt le)
5669 /* Simplify (x * C1) cmp C2 -> x cmp (C2 / C1), where C1 != 0. */
5670 (simplify
5671 (cmp (mult @0 REAL_CST@1) REAL_CST@2)
5672 (with
5673 { tree tem = const_binop (RDIV_EXPR, type, @2, @1); }
5674 (if (tem
5675 && !(REAL_VALUE_ISINF (TREE_REAL_CST (tem))
5676 || (real_zerop (tem) && !real_zerop (@1))))
5677 (switch
5678 (if (real_less (&dconst0, TREE_REAL_CST_PTR (@1)))
5679 (cmp @0 { tem; }))
5680 (if (real_less (TREE_REAL_CST_PTR (@1), &dconst0))
5681 (neg_cmp @0 { tem; })))))))
5682
5683 /* Simplify sqrt(x) * sqrt(y) -> sqrt(x*y). */
5684 (for root (SQRT CBRT)
5685 (simplify
5686 (mult (root:s @0) (root:s @1))
5687 (root (mult @0 @1))))
5688
5689 /* Simplify expN(x) * expN(y) -> expN(x+y). */
5690 (for exps (EXP EXP2 EXP10 POW10)
5691 (simplify
5692 (mult (exps:s @0) (exps:s @1))
5693 (exps (plus @0 @1))))
5694
5695 /* Simplify a/root(b/c) into a*root(c/b). */
5696 (for root (SQRT CBRT)
5697 (simplify
5698 (rdiv @0 (root:s (rdiv:s @1 @2)))
5699 (mult @0 (root (rdiv @2 @1)))))
5700
5701 /* Simplify x/expN(y) into x*expN(-y). */
5702 (for exps (EXP EXP2 EXP10 POW10)
5703 (simplify
5704 (rdiv @0 (exps:s @1))
5705 (mult @0 (exps (negate @1)))))
5706
5707 (for logs (LOG LOG2 LOG10 LOG10)
5708 exps (EXP EXP2 EXP10 POW10)
5709 /* logN(expN(x)) -> x. */
5710 (simplify
5711 (logs (exps @0))
5712 @0)
5713 /* expN(logN(x)) -> x. */
5714 (simplify
5715 (exps (logs @0))
5716 @0))
5717
5718 /* Optimize logN(func()) for various exponential functions. We
5719 want to determine the value "x" and the power "exponent" in
5720 order to transform logN(x**exponent) into exponent*logN(x). */
5721 (for logs (LOG LOG LOG LOG2 LOG2 LOG2 LOG10 LOG10)
5722 exps (EXP2 EXP10 POW10 EXP EXP10 POW10 EXP EXP2)
5723 (simplify
5724 (logs (exps @0))
5725 (if (SCALAR_FLOAT_TYPE_P (type))
5726 (with {
5727 tree x;
5728 switch (exps)
5729 {
5730 CASE_CFN_EXP:
5731 /* Prepare to do logN(exp(exponent)) -> exponent*logN(e). */
5732 x = build_real_truncate (type, dconst_e ());
5733 break;
5734 CASE_CFN_EXP2:
5735 /* Prepare to do logN(exp2(exponent)) -> exponent*logN(2). */
5736 x = build_real (type, dconst2);
5737 break;
5738 CASE_CFN_EXP10:
5739 CASE_CFN_POW10:
5740 /* Prepare to do logN(exp10(exponent)) -> exponent*logN(10). */
5741 {
5742 REAL_VALUE_TYPE dconst10;
5743 real_from_integer (&dconst10, VOIDmode, 10, SIGNED);
5744 x = build_real (type, dconst10);
5745 }
5746 break;
5747 default:
5748 gcc_unreachable ();
5749 }
5750 }
5751 (mult (logs { x; }) @0)))))
5752
5753 (for logs (LOG LOG
5754 LOG2 LOG2
5755 LOG10 LOG10)
5756 exps (SQRT CBRT)
5757 (simplify
5758 (logs (exps @0))
5759 (if (SCALAR_FLOAT_TYPE_P (type))
5760 (with {
5761 tree x;
5762 switch (exps)
5763 {
5764 CASE_CFN_SQRT:
5765 /* Prepare to do logN(sqrt(x)) -> 0.5*logN(x). */
5766 x = build_real (type, dconsthalf);
5767 break;
5768 CASE_CFN_CBRT:
5769 /* Prepare to do logN(cbrt(x)) -> (1/3)*logN(x). */
5770 x = build_real_truncate (type, dconst_third ());
5771 break;
5772 default:
5773 gcc_unreachable ();
5774 }
5775 }
5776 (mult { x; } (logs @0))))))
5777
5778 /* logN(pow(x,exponent)) -> exponent*logN(x). */
5779 (for logs (LOG LOG2 LOG10)
5780 pows (POW)
5781 (simplify
5782 (logs (pows @0 @1))
5783 (mult @1 (logs @0))))
5784
5785 /* pow(C,x) -> exp(log(C)*x) if C > 0,
5786 or if C is a positive power of 2,
5787 pow(C,x) -> exp2(log2(C)*x). */
5788 #if GIMPLE
5789 (for pows (POW)
5790 exps (EXP)
5791 logs (LOG)
5792 exp2s (EXP2)
5793 log2s (LOG2)
5794 (simplify
5795 (pows REAL_CST@0 @1)
5796 (if (real_compare (GT_EXPR, TREE_REAL_CST_PTR (@0), &dconst0)
5797 && real_isfinite (TREE_REAL_CST_PTR (@0))
5798 /* As libmvec doesn't have a vectorized exp2, defer optimizing
5799 the use_exp2 case until after vectorization. It seems actually
5800 beneficial for all constants to postpone this until later,
5801 because exp(log(C)*x), while faster, will have worse precision
5802 and if x folds into a constant too, that is unnecessary
5803 pessimization. */
5804 && canonicalize_math_after_vectorization_p ())
5805 (with {
5806 const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (@0);
5807 bool use_exp2 = false;
5808 if (targetm.libc_has_function (function_c99_misc, TREE_TYPE (@0))
5809 && value->cl == rvc_normal)
5810 {
5811 REAL_VALUE_TYPE frac_rvt = *value;
5812 SET_REAL_EXP (&frac_rvt, 1);
5813 if (real_equal (&frac_rvt, &dconst1))
5814 use_exp2 = true;
5815 }
5816 }
5817 (if (!use_exp2)
5818 (if (optimize_pow_to_exp (@0, @1))
5819 (exps (mult (logs @0) @1)))
5820 (exp2s (mult (log2s @0) @1)))))))
5821 #endif
5822
5823 /* pow(C,x)*expN(y) -> expN(logN(C)*x+y) if C > 0. */
5824 (for pows (POW)
5825 exps (EXP EXP2 EXP10 POW10)
5826 logs (LOG LOG2 LOG10 LOG10)
5827 (simplify
5828 (mult:c (pows:s REAL_CST@0 @1) (exps:s @2))
5829 (if (real_compare (GT_EXPR, TREE_REAL_CST_PTR (@0), &dconst0)
5830 && real_isfinite (TREE_REAL_CST_PTR (@0)))
5831 (exps (plus (mult (logs @0) @1) @2)))))
5832
5833 (for sqrts (SQRT)
5834 cbrts (CBRT)
5835 pows (POW)
5836 exps (EXP EXP2 EXP10 POW10)
5837 /* sqrt(expN(x)) -> expN(x*0.5). */
5838 (simplify
5839 (sqrts (exps @0))
5840 (exps (mult @0 { build_real (type, dconsthalf); })))
5841 /* cbrt(expN(x)) -> expN(x/3). */
5842 (simplify
5843 (cbrts (exps @0))
5844 (exps (mult @0 { build_real_truncate (type, dconst_third ()); })))
5845 /* pow(expN(x), y) -> expN(x*y). */
5846 (simplify
5847 (pows (exps @0) @1)
5848 (exps (mult @0 @1))))
5849
5850 /* tan(atan(x)) -> x. */
5851 (for tans (TAN)
5852 atans (ATAN)
5853 (simplify
5854 (tans (atans @0))
5855 @0)))
5856
5857 /* Simplify sin(atan(x)) -> x / sqrt(x*x + 1). */
5858 (for sins (SIN)
5859 atans (ATAN)
5860 sqrts (SQRT)
5861 copysigns (COPYSIGN)
5862 (simplify
5863 (sins (atans:s @0))
5864 (with
5865 {
5866 REAL_VALUE_TYPE r_cst;
5867 build_sinatan_real (&r_cst, type);
5868 tree t_cst = build_real (type, r_cst);
5869 tree t_one = build_one_cst (type);
5870 }
5871 (if (SCALAR_FLOAT_TYPE_P (type))
5872 (cond (lt (abs @0) { t_cst; })
5873 (rdiv @0 (sqrts (plus (mult @0 @0) { t_one; })))
5874 (copysigns { t_one; } @0))))))
5875
5876 /* Simplify cos(atan(x)) -> 1 / sqrt(x*x + 1). */
5877 (for coss (COS)
5878 atans (ATAN)
5879 sqrts (SQRT)
5880 copysigns (COPYSIGN)
5881 (simplify
5882 (coss (atans:s @0))
5883 (with
5884 {
5885 REAL_VALUE_TYPE r_cst;
5886 build_sinatan_real (&r_cst, type);
5887 tree t_cst = build_real (type, r_cst);
5888 tree t_one = build_one_cst (type);
5889 tree t_zero = build_zero_cst (type);
5890 }
5891 (if (SCALAR_FLOAT_TYPE_P (type))
5892 (cond (lt (abs @0) { t_cst; })
5893 (rdiv { t_one; } (sqrts (plus (mult @0 @0) { t_one; })))
5894 (copysigns { t_zero; } @0))))))
5895
5896 (if (!flag_errno_math)
5897 /* Simplify sinh(atanh(x)) -> x / sqrt((1 - x)*(1 + x)). */
5898 (for sinhs (SINH)
5899 atanhs (ATANH)
5900 sqrts (SQRT)
5901 (simplify
5902 (sinhs (atanhs:s @0))
5903 (with { tree t_one = build_one_cst (type); }
5904 (rdiv @0 (sqrts (mult (minus { t_one; } @0) (plus { t_one; } @0)))))))
5905
5906 /* Simplify cosh(atanh(x)) -> 1 / sqrt((1 - x)*(1 + x)) */
5907 (for coshs (COSH)
5908 atanhs (ATANH)
5909 sqrts (SQRT)
5910 (simplify
5911 (coshs (atanhs:s @0))
5912 (with { tree t_one = build_one_cst (type); }
5913 (rdiv { t_one; } (sqrts (mult (minus { t_one; } @0) (plus { t_one; } @0))))))))
5914
5915 /* cabs(x+0i) or cabs(0+xi) -> abs(x). */
5916 (simplify
5917 (CABS (complex:C @0 real_zerop@1))
5918 (abs @0))
5919
5920 /* trunc(trunc(x)) -> trunc(x), etc. */
5921 (for fns (TRUNC_ALL FLOOR_ALL CEIL_ALL ROUND_ALL NEARBYINT_ALL RINT_ALL)
5922 (simplify
5923 (fns (fns @0))
5924 (fns @0)))
5925 /* f(x) -> x if x is integer valued and f does nothing for such values. */
5926 (for fns (TRUNC_ALL FLOOR_ALL CEIL_ALL ROUND_ALL NEARBYINT_ALL RINT_ALL)
5927 (simplify
5928 (fns integer_valued_real_p@0)
5929 @0))
5930
5931 /* hypot(x,0) and hypot(0,x) -> abs(x). */
5932 (simplify
5933 (HYPOT:c @0 real_zerop@1)
5934 (abs @0))
5935
5936 /* pow(1,x) -> 1. */
5937 (simplify
5938 (POW real_onep@0 @1)
5939 @0)
5940
5941 (simplify
5942 /* copysign(x,x) -> x. */
5943 (COPYSIGN_ALL @0 @0)
5944 @0)
5945
5946 (simplify
5947 /* copysign(x,-x) -> -x. */
5948 (COPYSIGN_ALL @0 (negate@1 @0))
5949 @1)
5950
5951 (simplify
5952 /* copysign(x,y) -> fabs(x) if y is nonnegative. */
5953 (COPYSIGN_ALL @0 tree_expr_nonnegative_p@1)
5954 (abs @0))
5955
5956 (for scale (LDEXP SCALBN SCALBLN)
5957 /* ldexp(0, x) -> 0. */
5958 (simplify
5959 (scale real_zerop@0 @1)
5960 @0)
5961 /* ldexp(x, 0) -> x. */
5962 (simplify
5963 (scale @0 integer_zerop@1)
5964 @0)
5965 /* ldexp(x, y) -> x if x is +-Inf or NaN. */
5966 (simplify
5967 (scale REAL_CST@0 @1)
5968 (if (!real_isfinite (TREE_REAL_CST_PTR (@0)))
5969 @0)))
5970
5971 /* Canonicalization of sequences of math builtins. These rules represent
5972 IL simplifications but are not necessarily optimizations.
5973
5974 The sincos pass is responsible for picking "optimal" implementations
5975 of math builtins, which may be more complicated and can sometimes go
5976 the other way, e.g. converting pow into a sequence of sqrts.
5977 We only want to do these canonicalizations before the pass has run. */
5978
5979 (if (flag_unsafe_math_optimizations && canonicalize_math_p ())
5980 /* Simplify tan(x) * cos(x) -> sin(x). */
5981 (simplify
5982 (mult:c (TAN:s @0) (COS:s @0))
5983 (SIN @0))
5984
5985 /* Simplify x * pow(x,c) -> pow(x,c+1). */
5986 (simplify
5987 (mult:c @0 (POW:s @0 REAL_CST@1))
5988 (if (!TREE_OVERFLOW (@1))
5989 (POW @0 (plus @1 { build_one_cst (type); }))))
5990
5991 /* Simplify sin(x) / cos(x) -> tan(x). */
5992 (simplify
5993 (rdiv (SIN:s @0) (COS:s @0))
5994 (TAN @0))
5995
5996 /* Simplify sinh(x) / cosh(x) -> tanh(x). */
5997 (simplify
5998 (rdiv (SINH:s @0) (COSH:s @0))
5999 (TANH @0))
6000
6001 /* Simplify tanh (x) / sinh (x) -> 1.0 / cosh (x). */
6002 (simplify
6003 (rdiv (TANH:s @0) (SINH:s @0))
6004 (rdiv {build_one_cst (type);} (COSH @0)))
6005
6006 /* Simplify cos(x) / sin(x) -> 1 / tan(x). */
6007 (simplify
6008 (rdiv (COS:s @0) (SIN:s @0))
6009 (rdiv { build_one_cst (type); } (TAN @0)))
6010
6011 /* Simplify sin(x) / tan(x) -> cos(x). */
6012 (simplify
6013 (rdiv (SIN:s @0) (TAN:s @0))
6014 (if (! HONOR_NANS (@0)
6015 && ! HONOR_INFINITIES (@0))
6016 (COS @0)))
6017
6018 /* Simplify tan(x) / sin(x) -> 1.0 / cos(x). */
6019 (simplify
6020 (rdiv (TAN:s @0) (SIN:s @0))
6021 (if (! HONOR_NANS (@0)
6022 && ! HONOR_INFINITIES (@0))
6023 (rdiv { build_one_cst (type); } (COS @0))))
6024
6025 /* Simplify pow(x,y) * pow(x,z) -> pow(x,y+z). */
6026 (simplify
6027 (mult (POW:s @0 @1) (POW:s @0 @2))
6028 (POW @0 (plus @1 @2)))
6029
6030 /* Simplify pow(x,y) * pow(z,y) -> pow(x*z,y). */
6031 (simplify
6032 (mult (POW:s @0 @1) (POW:s @2 @1))
6033 (POW (mult @0 @2) @1))
6034
6035 /* Simplify powi(x,y) * powi(z,y) -> powi(x*z,y). */
6036 (simplify
6037 (mult (POWI:s @0 @1) (POWI:s @2 @1))
6038 (POWI (mult @0 @2) @1))
6039
6040 /* Simplify pow(x,c) / x -> pow(x,c-1). */
6041 (simplify
6042 (rdiv (POW:s @0 REAL_CST@1) @0)
6043 (if (!TREE_OVERFLOW (@1))
6044 (POW @0 (minus @1 { build_one_cst (type); }))))
6045
6046 /* Simplify x / pow (y,z) -> x * pow(y,-z). */
6047 (simplify
6048 (rdiv @0 (POW:s @1 @2))
6049 (mult @0 (POW @1 (negate @2))))
6050
6051 (for sqrts (SQRT)
6052 cbrts (CBRT)
6053 pows (POW)
6054 /* sqrt(sqrt(x)) -> pow(x,1/4). */
6055 (simplify
6056 (sqrts (sqrts @0))
6057 (pows @0 { build_real (type, dconst_quarter ()); }))
6058 /* sqrt(cbrt(x)) -> pow(x,1/6). */
6059 (simplify
6060 (sqrts (cbrts @0))
6061 (pows @0 { build_real_truncate (type, dconst_sixth ()); }))
6062 /* cbrt(sqrt(x)) -> pow(x,1/6). */
6063 (simplify
6064 (cbrts (sqrts @0))
6065 (pows @0 { build_real_truncate (type, dconst_sixth ()); }))
6066 /* cbrt(cbrt(x)) -> pow(x,1/9), iff x is nonnegative. */
6067 (simplify
6068 (cbrts (cbrts tree_expr_nonnegative_p@0))
6069 (pows @0 { build_real_truncate (type, dconst_ninth ()); }))
6070 /* sqrt(pow(x,y)) -> pow(|x|,y*0.5). */
6071 (simplify
6072 (sqrts (pows @0 @1))
6073 (pows (abs @0) (mult @1 { build_real (type, dconsthalf); })))
6074 /* cbrt(pow(x,y)) -> pow(x,y/3), iff x is nonnegative. */
6075 (simplify
6076 (cbrts (pows tree_expr_nonnegative_p@0 @1))
6077 (pows @0 (mult @1 { build_real_truncate (type, dconst_third ()); })))
6078 /* pow(sqrt(x),y) -> pow(x,y*0.5). */
6079 (simplify
6080 (pows (sqrts @0) @1)
6081 (pows @0 (mult @1 { build_real (type, dconsthalf); })))
6082 /* pow(cbrt(x),y) -> pow(x,y/3) iff x is nonnegative. */
6083 (simplify
6084 (pows (cbrts tree_expr_nonnegative_p@0) @1)
6085 (pows @0 (mult @1 { build_real_truncate (type, dconst_third ()); })))
6086 /* pow(pow(x,y),z) -> pow(x,y*z) iff x is nonnegative. */
6087 (simplify
6088 (pows (pows tree_expr_nonnegative_p@0 @1) @2)
6089 (pows @0 (mult @1 @2))))
6090
6091 /* cabs(x+xi) -> fabs(x)*sqrt(2). */
6092 (simplify
6093 (CABS (complex @0 @0))
6094 (mult (abs @0) { build_real_truncate (type, dconst_sqrt2 ()); }))
6095
6096 /* hypot(x,x) -> fabs(x)*sqrt(2). */
6097 (simplify
6098 (HYPOT @0 @0)
6099 (mult (abs @0) { build_real_truncate (type, dconst_sqrt2 ()); }))
6100
6101 /* cexp(x+yi) -> exp(x)*cexpi(y). */
6102 (for cexps (CEXP)
6103 exps (EXP)
6104 cexpis (CEXPI)
6105 (simplify
6106 (cexps compositional_complex@0)
6107 (if (targetm.libc_has_function (function_c99_math_complex, TREE_TYPE (@0)))
6108 (complex
6109 (mult (exps@1 (realpart @0)) (realpart (cexpis:type@2 (imagpart @0))))
6110 (mult @1 (imagpart @2)))))))
6111
6112 (if (canonicalize_math_p ())
6113 /* floor(x) -> trunc(x) if x is nonnegative. */
6114 (for floors (FLOOR_ALL)
6115 truncs (TRUNC_ALL)
6116 (simplify
6117 (floors tree_expr_nonnegative_p@0)
6118 (truncs @0))))
6119
6120 (match double_value_p
6121 @0
6122 (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == double_type_node)))
6123 (for froms (BUILT_IN_TRUNCL
6124 BUILT_IN_FLOORL
6125 BUILT_IN_CEILL
6126 BUILT_IN_ROUNDL
6127 BUILT_IN_NEARBYINTL
6128 BUILT_IN_RINTL)
6129 tos (BUILT_IN_TRUNC
6130 BUILT_IN_FLOOR
6131 BUILT_IN_CEIL
6132 BUILT_IN_ROUND
6133 BUILT_IN_NEARBYINT
6134 BUILT_IN_RINT)
6135 /* truncl(extend(x)) -> extend(trunc(x)), etc., if x is a double. */
6136 (if (optimize && canonicalize_math_p ())
6137 (simplify
6138 (froms (convert double_value_p@0))
6139 (convert (tos @0)))))
6140
6141 (match float_value_p
6142 @0
6143 (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == float_type_node)))
6144 (for froms (BUILT_IN_TRUNCL BUILT_IN_TRUNC
6145 BUILT_IN_FLOORL BUILT_IN_FLOOR
6146 BUILT_IN_CEILL BUILT_IN_CEIL
6147 BUILT_IN_ROUNDL BUILT_IN_ROUND
6148 BUILT_IN_NEARBYINTL BUILT_IN_NEARBYINT
6149 BUILT_IN_RINTL BUILT_IN_RINT)
6150 tos (BUILT_IN_TRUNCF BUILT_IN_TRUNCF
6151 BUILT_IN_FLOORF BUILT_IN_FLOORF
6152 BUILT_IN_CEILF BUILT_IN_CEILF
6153 BUILT_IN_ROUNDF BUILT_IN_ROUNDF
6154 BUILT_IN_NEARBYINTF BUILT_IN_NEARBYINTF
6155 BUILT_IN_RINTF BUILT_IN_RINTF)
6156 /* truncl(extend(x)) and trunc(extend(x)) -> extend(truncf(x)), etc.,
6157 if x is a float. */
6158 (if (optimize && canonicalize_math_p ()
6159 && targetm.libc_has_function (function_c99_misc, NULL_TREE))
6160 (simplify
6161 (froms (convert float_value_p@0))
6162 (convert (tos @0)))))
6163
6164 (for froms (XFLOORL XCEILL XROUNDL XRINTL)
6165 tos (XFLOOR XCEIL XROUND XRINT)
6166 /* llfloorl(extend(x)) -> llfloor(x), etc., if x is a double. */
6167 (if (optimize && canonicalize_math_p ())
6168 (simplify
6169 (froms (convert double_value_p@0))
6170 (tos @0))))
6171
6172 (for froms (XFLOORL XCEILL XROUNDL XRINTL
6173 XFLOOR XCEIL XROUND XRINT)
6174 tos (XFLOORF XCEILF XROUNDF XRINTF)
6175 /* llfloorl(extend(x)) and llfloor(extend(x)) -> llfloorf(x), etc.,
6176 if x is a float. */
6177 (if (optimize && canonicalize_math_p ())
6178 (simplify
6179 (froms (convert float_value_p@0))
6180 (tos @0))))
6181
6182 (if (canonicalize_math_p ())
6183 /* xfloor(x) -> fix_trunc(x) if x is nonnegative. */
6184 (for floors (IFLOOR LFLOOR LLFLOOR)
6185 (simplify
6186 (floors tree_expr_nonnegative_p@0)
6187 (fix_trunc @0))))
6188
6189 (if (canonicalize_math_p ())
6190 /* xfloor(x) -> fix_trunc(x), etc., if x is integer valued. */
6191 (for fns (IFLOOR LFLOOR LLFLOOR
6192 ICEIL LCEIL LLCEIL
6193 IROUND LROUND LLROUND)
6194 (simplify
6195 (fns integer_valued_real_p@0)
6196 (fix_trunc @0)))
6197 (if (!flag_errno_math)
6198 /* xrint(x) -> fix_trunc(x), etc., if x is integer valued. */
6199 (for rints (IRINT LRINT LLRINT)
6200 (simplify
6201 (rints integer_valued_real_p@0)
6202 (fix_trunc @0)))))
6203
6204 (if (canonicalize_math_p ())
6205 (for ifn (IFLOOR ICEIL IROUND IRINT)
6206 lfn (LFLOOR LCEIL LROUND LRINT)
6207 llfn (LLFLOOR LLCEIL LLROUND LLRINT)
6208 /* Canonicalize iround (x) to lround (x) on ILP32 targets where
6209 sizeof (int) == sizeof (long). */
6210 (if (TYPE_PRECISION (integer_type_node)
6211 == TYPE_PRECISION (long_integer_type_node))
6212 (simplify
6213 (ifn @0)
6214 (lfn:long_integer_type_node @0)))
6215 /* Canonicalize llround (x) to lround (x) on LP64 targets where
6216 sizeof (long long) == sizeof (long). */
6217 (if (TYPE_PRECISION (long_long_integer_type_node)
6218 == TYPE_PRECISION (long_integer_type_node))
6219 (simplify
6220 (llfn @0)
6221 (lfn:long_integer_type_node @0)))))
6222
6223 /* cproj(x) -> x if we're ignoring infinities. */
6224 (simplify
6225 (CPROJ @0)
6226 (if (!HONOR_INFINITIES (type))
6227 @0))
6228
6229 /* If the real part is inf and the imag part is known to be
6230 nonnegative, return (inf + 0i). */
6231 (simplify
6232 (CPROJ (complex REAL_CST@0 tree_expr_nonnegative_p@1))
6233 (if (real_isinf (TREE_REAL_CST_PTR (@0)))
6234 { build_complex_inf (type, false); }))
6235
6236 /* If the imag part is inf, return (inf+I*copysign(0,imag)). */
6237 (simplify
6238 (CPROJ (complex @0 REAL_CST@1))
6239 (if (real_isinf (TREE_REAL_CST_PTR (@1)))
6240 { build_complex_inf (type, TREE_REAL_CST_PTR (@1)->sign); }))
6241
6242 (for pows (POW)
6243 sqrts (SQRT)
6244 cbrts (CBRT)
6245 (simplify
6246 (pows @0 REAL_CST@1)
6247 (with {
6248 const REAL_VALUE_TYPE *value = TREE_REAL_CST_PTR (@1);
6249 REAL_VALUE_TYPE tmp;
6250 }
6251 (switch
6252 /* pow(x,0) -> 1. */
6253 (if (real_equal (value, &dconst0))
6254 { build_real (type, dconst1); })
6255 /* pow(x,1) -> x. */
6256 (if (real_equal (value, &dconst1))
6257 @0)
6258 /* pow(x,-1) -> 1/x. */
6259 (if (real_equal (value, &dconstm1))
6260 (rdiv { build_real (type, dconst1); } @0))
6261 /* pow(x,0.5) -> sqrt(x). */
6262 (if (flag_unsafe_math_optimizations
6263 && canonicalize_math_p ()
6264 && real_equal (value, &dconsthalf))
6265 (sqrts @0))
6266 /* pow(x,1/3) -> cbrt(x). */
6267 (if (flag_unsafe_math_optimizations
6268 && canonicalize_math_p ()
6269 && (tmp = real_value_truncate (TYPE_MODE (type), dconst_third ()),
6270 real_equal (value, &tmp)))
6271 (cbrts @0))))))
6272
6273 /* powi(1,x) -> 1. */
6274 (simplify
6275 (POWI real_onep@0 @1)
6276 @0)
6277
6278 (simplify
6279 (POWI @0 INTEGER_CST@1)
6280 (switch
6281 /* powi(x,0) -> 1. */
6282 (if (wi::to_wide (@1) == 0)
6283 { build_real (type, dconst1); })
6284 /* powi(x,1) -> x. */
6285 (if (wi::to_wide (@1) == 1)
6286 @0)
6287 /* powi(x,-1) -> 1/x. */
6288 (if (wi::to_wide (@1) == -1)
6289 (rdiv { build_real (type, dconst1); } @0))))
6290
6291 /* Narrowing of arithmetic and logical operations.
6292
6293 These are conceptually similar to the transformations performed for
6294 the C/C++ front-ends by shorten_binary_op and shorten_compare. Long
6295 term we want to move all that code out of the front-ends into here. */
6296
6297 /* Convert (outertype)((innertype0)a+(innertype1)b)
6298 into ((newtype)a+(newtype)b) where newtype
6299 is the widest mode from all of these. */
6300 (for op (plus minus mult rdiv)
6301 (simplify
6302 (convert (op:s@0 (convert1?@3 @1) (convert2?@4 @2)))
6303 /* If we have a narrowing conversion of an arithmetic operation where
6304 both operands are widening conversions from the same type as the outer
6305 narrowing conversion. Then convert the innermost operands to a
6306 suitable unsigned type (to avoid introducing undefined behavior),
6307 perform the operation and convert the result to the desired type. */
6308 (if (INTEGRAL_TYPE_P (type)
6309 && op != MULT_EXPR
6310 && op != RDIV_EXPR
6311 /* We check for type compatibility between @0 and @1 below,
6312 so there's no need to check that @2/@4 are integral types. */
6313 && INTEGRAL_TYPE_P (TREE_TYPE (@1))
6314 && INTEGRAL_TYPE_P (TREE_TYPE (@3))
6315 /* The precision of the type of each operand must match the
6316 precision of the mode of each operand, similarly for the
6317 result. */
6318 && type_has_mode_precision_p (TREE_TYPE (@1))
6319 && type_has_mode_precision_p (TREE_TYPE (@2))
6320 && type_has_mode_precision_p (type)
6321 /* The inner conversion must be a widening conversion. */
6322 && TYPE_PRECISION (TREE_TYPE (@3)) > TYPE_PRECISION (TREE_TYPE (@1))
6323 && types_match (@1, type)
6324 && (types_match (@1, @2)
6325 /* Or the second operand is const integer or converted const
6326 integer from valueize. */
6327 || poly_int_tree_p (@4)))
6328 (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
6329 (op @1 (convert @2))
6330 (with { tree utype = unsigned_type_for (TREE_TYPE (@1)); }
6331 (convert (op (convert:utype @1)
6332 (convert:utype @2)))))
6333 (if (FLOAT_TYPE_P (type)
6334 && DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0))
6335 == DECIMAL_FLOAT_TYPE_P (type))
6336 (with { tree arg0 = strip_float_extensions (@1);
6337 tree arg1 = strip_float_extensions (@2);
6338 tree itype = TREE_TYPE (@0);
6339 tree ty1 = TREE_TYPE (arg0);
6340 tree ty2 = TREE_TYPE (arg1);
6341 enum tree_code code = TREE_CODE (itype); }
6342 (if (FLOAT_TYPE_P (ty1)
6343 && FLOAT_TYPE_P (ty2))
6344 (with { tree newtype = type;
6345 if (TYPE_MODE (ty1) == SDmode
6346 || TYPE_MODE (ty2) == SDmode
6347 || TYPE_MODE (type) == SDmode)
6348 newtype = dfloat32_type_node;
6349 if (TYPE_MODE (ty1) == DDmode
6350 || TYPE_MODE (ty2) == DDmode
6351 || TYPE_MODE (type) == DDmode)
6352 newtype = dfloat64_type_node;
6353 if (TYPE_MODE (ty1) == TDmode
6354 || TYPE_MODE (ty2) == TDmode
6355 || TYPE_MODE (type) == TDmode)
6356 newtype = dfloat128_type_node; }
6357 (if ((newtype == dfloat32_type_node
6358 || newtype == dfloat64_type_node
6359 || newtype == dfloat128_type_node)
6360 && newtype == type
6361 && types_match (newtype, type))
6362 (op (convert:newtype @1) (convert:newtype @2))
6363 (with { if (TYPE_PRECISION (ty1) > TYPE_PRECISION (newtype))
6364 newtype = ty1;
6365 if (TYPE_PRECISION (ty2) > TYPE_PRECISION (newtype))
6366 newtype = ty2; }
6367 /* Sometimes this transformation is safe (cannot
6368 change results through affecting double rounding
6369 cases) and sometimes it is not. If NEWTYPE is
6370 wider than TYPE, e.g. (float)((long double)double
6371 + (long double)double) converted to
6372 (float)(double + double), the transformation is
6373 unsafe regardless of the details of the types
6374 involved; double rounding can arise if the result
6375 of NEWTYPE arithmetic is a NEWTYPE value half way
6376 between two representable TYPE values but the
6377 exact value is sufficiently different (in the
6378 right direction) for this difference to be
6379 visible in ITYPE arithmetic. If NEWTYPE is the
6380 same as TYPE, however, the transformation may be
6381 safe depending on the types involved: it is safe
6382 if the ITYPE has strictly more than twice as many
6383 mantissa bits as TYPE, can represent infinities
6384 and NaNs if the TYPE can, and has sufficient
6385 exponent range for the product or ratio of two
6386 values representable in the TYPE to be within the
6387 range of normal values of ITYPE. */
6388 (if (TYPE_PRECISION (newtype) < TYPE_PRECISION (itype)
6389 && (flag_unsafe_math_optimizations
6390 || (TYPE_PRECISION (newtype) == TYPE_PRECISION (type)
6391 && real_can_shorten_arithmetic (TYPE_MODE (itype),
6392 TYPE_MODE (type))
6393 && !excess_precision_type (newtype)))
6394 && !types_match (itype, newtype))
6395 (convert:type (op (convert:newtype @1)
6396 (convert:newtype @2)))
6397 )))) )
6398 ))
6399 )))
6400
6401 /* This is another case of narrowing, specifically when there's an outer
6402 BIT_AND_EXPR which masks off bits outside the type of the innermost
6403 operands. Like the previous case we have to convert the operands
6404 to unsigned types to avoid introducing undefined behavior for the
6405 arithmetic operation. */
6406 (for op (minus plus)
6407 (simplify
6408 (bit_and (op:s (convert@2 @0) (convert@3 @1)) INTEGER_CST@4)
6409 (if (INTEGRAL_TYPE_P (type)
6410 /* We check for type compatibility between @0 and @1 below,
6411 so there's no need to check that @1/@3 are integral types. */
6412 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
6413 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
6414 /* The precision of the type of each operand must match the
6415 precision of the mode of each operand, similarly for the
6416 result. */
6417 && type_has_mode_precision_p (TREE_TYPE (@0))
6418 && type_has_mode_precision_p (TREE_TYPE (@1))
6419 && type_has_mode_precision_p (type)
6420 /* The inner conversion must be a widening conversion. */
6421 && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
6422 && types_match (@0, @1)
6423 && (tree_int_cst_min_precision (@4, TYPE_SIGN (TREE_TYPE (@0)))
6424 <= TYPE_PRECISION (TREE_TYPE (@0)))
6425 && (wi::to_wide (@4)
6426 & wi::mask (TYPE_PRECISION (TREE_TYPE (@0)),
6427 true, TYPE_PRECISION (type))) == 0)
6428 (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
6429 (with { tree ntype = TREE_TYPE (@0); }
6430 (convert (bit_and (op @0 @1) (convert:ntype @4))))
6431 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
6432 (convert (bit_and (op (convert:utype @0) (convert:utype @1))
6433 (convert:utype @4))))))))
6434
6435 /* Transform (@0 < @1 and @0 < @2) to use min,
6436 (@0 > @1 and @0 > @2) to use max */
6437 (for logic (bit_and bit_and bit_and bit_and bit_ior bit_ior bit_ior bit_ior)
6438 op (lt le gt ge lt le gt ge )
6439 ext (min min max max max max min min )
6440 (simplify
6441 (logic (op:cs @0 @1) (op:cs @0 @2))
6442 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6443 && TREE_CODE (@0) != INTEGER_CST)
6444 (op @0 (ext @1 @2)))))
6445
6446 (simplify
6447 /* signbit(x) -> 0 if x is nonnegative. */
6448 (SIGNBIT tree_expr_nonnegative_p@0)
6449 { integer_zero_node; })
6450
6451 (simplify
6452 /* signbit(x) -> x<0 if x doesn't have signed zeros. */
6453 (SIGNBIT @0)
6454 (if (!HONOR_SIGNED_ZEROS (@0))
6455 (convert (lt @0 { build_real (TREE_TYPE (@0), dconst0); }))))
6456
6457 /* Transform comparisons of the form X +- C1 CMP C2 to X CMP C2 -+ C1. */
6458 (for cmp (eq ne)
6459 (for op (plus minus)
6460 rop (minus plus)
6461 (simplify
6462 (cmp (op@3 @0 INTEGER_CST@1) INTEGER_CST@2)
6463 (if (!TREE_OVERFLOW (@1) && !TREE_OVERFLOW (@2)
6464 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0))
6465 && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@0))
6466 && !TYPE_SATURATING (TREE_TYPE (@0)))
6467 (with { tree res = int_const_binop (rop, @2, @1); }
6468 (if (TREE_OVERFLOW (res)
6469 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
6470 { constant_boolean_node (cmp == NE_EXPR, type); }
6471 (if (single_use (@3))
6472 (cmp @0 { TREE_OVERFLOW (res)
6473 ? drop_tree_overflow (res) : res; }))))))))
6474 (for cmp (lt le gt ge)
6475 (for op (plus minus)
6476 rop (minus plus)
6477 (simplify
6478 (cmp (op@3 @0 INTEGER_CST@1) INTEGER_CST@2)
6479 (if (!TREE_OVERFLOW (@1) && !TREE_OVERFLOW (@2)
6480 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
6481 (with { tree res = int_const_binop (rop, @2, @1); }
6482 (if (TREE_OVERFLOW (res))
6483 {
6484 fold_overflow_warning (("assuming signed overflow does not occur "
6485 "when simplifying conditional to constant"),
6486 WARN_STRICT_OVERFLOW_CONDITIONAL);
6487 bool less = cmp == LE_EXPR || cmp == LT_EXPR;
6488 /* wi::ges_p (@2, 0) should be sufficient for a signed type. */
6489 bool ovf_high = wi::lt_p (wi::to_wide (@1), 0,
6490 TYPE_SIGN (TREE_TYPE (@1)))
6491 != (op == MINUS_EXPR);
6492 constant_boolean_node (less == ovf_high, type);
6493 }
6494 (if (single_use (@3))
6495 (with
6496 {
6497 fold_overflow_warning (("assuming signed overflow does not occur "
6498 "when changing X +- C1 cmp C2 to "
6499 "X cmp C2 -+ C1"),
6500 WARN_STRICT_OVERFLOW_COMPARISON);
6501 }
6502 (cmp @0 { res; })))))))))
6503
6504 /* Canonicalizations of BIT_FIELD_REFs. */
6505
6506 (simplify
6507 (BIT_FIELD_REF (BIT_FIELD_REF @0 @1 @2) @3 @4)
6508 (BIT_FIELD_REF @0 @3 { const_binop (PLUS_EXPR, bitsizetype, @2, @4); }))
6509
6510 (simplify
6511 (BIT_FIELD_REF (view_convert @0) @1 @2)
6512 (BIT_FIELD_REF @0 @1 @2))
6513
6514 (simplify
6515 (BIT_FIELD_REF @0 @1 integer_zerop)
6516 (if (tree_int_cst_equal (@1, TYPE_SIZE (TREE_TYPE (@0))))
6517 (view_convert @0)))
6518
6519 (simplify
6520 (BIT_FIELD_REF @0 @1 @2)
6521 (switch
6522 (if (TREE_CODE (TREE_TYPE (@0)) == COMPLEX_TYPE
6523 && tree_int_cst_equal (@1, TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))))
6524 (switch
6525 (if (integer_zerop (@2))
6526 (view_convert (realpart @0)))
6527 (if (tree_int_cst_equal (@2, TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))))
6528 (view_convert (imagpart @0)))))
6529 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6530 && INTEGRAL_TYPE_P (type)
6531 /* On GIMPLE this should only apply to register arguments. */
6532 && (! GIMPLE || is_gimple_reg (@0))
6533 /* A bit-field-ref that referenced the full argument can be stripped. */
6534 && ((compare_tree_int (@1, TYPE_PRECISION (TREE_TYPE (@0))) == 0
6535 && integer_zerop (@2))
6536 /* Low-parts can be reduced to integral conversions.
6537 ??? The following doesn't work for PDP endian. */
6538 || (BYTES_BIG_ENDIAN == WORDS_BIG_ENDIAN
6539 /* But only do this after vectorization. */
6540 && canonicalize_math_after_vectorization_p ()
6541 /* Don't even think about BITS_BIG_ENDIAN. */
6542 && TYPE_PRECISION (TREE_TYPE (@0)) % BITS_PER_UNIT == 0
6543 && TYPE_PRECISION (type) % BITS_PER_UNIT == 0
6544 && compare_tree_int (@2, (BYTES_BIG_ENDIAN
6545 ? (TYPE_PRECISION (TREE_TYPE (@0))
6546 - TYPE_PRECISION (type))
6547 : 0)) == 0)))
6548 (convert @0))))
6549
6550 /* Simplify vector extracts. */
6551
6552 (simplify
6553 (BIT_FIELD_REF CONSTRUCTOR@0 @1 @2)
6554 (if (VECTOR_TYPE_P (TREE_TYPE (@0))
6555 && tree_fits_uhwi_p (TYPE_SIZE (type))
6556 && ((tree_to_uhwi (TYPE_SIZE (type))
6557 == tree_to_uhwi (TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))))
6558 || (VECTOR_TYPE_P (type)
6559 && (tree_to_uhwi (TYPE_SIZE (TREE_TYPE (type)))
6560 == tree_to_uhwi (TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0))))))))
6561 (with
6562 {
6563 tree ctor = (TREE_CODE (@0) == SSA_NAME
6564 ? gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0)) : @0);
6565 tree eltype = TREE_TYPE (TREE_TYPE (ctor));
6566 unsigned HOST_WIDE_INT width = tree_to_uhwi (TYPE_SIZE (eltype));
6567 unsigned HOST_WIDE_INT n = tree_to_uhwi (@1);
6568 unsigned HOST_WIDE_INT idx = tree_to_uhwi (@2);
6569 }
6570 (if (n != 0
6571 && (idx % width) == 0
6572 && (n % width) == 0
6573 && known_le ((idx + n) / width,
6574 TYPE_VECTOR_SUBPARTS (TREE_TYPE (ctor))))
6575 (with
6576 {
6577 idx = idx / width;
6578 n = n / width;
6579 /* Constructor elements can be subvectors. */
6580 poly_uint64 k = 1;
6581 if (CONSTRUCTOR_NELTS (ctor) != 0)
6582 {
6583 tree cons_elem = TREE_TYPE (CONSTRUCTOR_ELT (ctor, 0)->value);
6584 if (TREE_CODE (cons_elem) == VECTOR_TYPE)
6585 k = TYPE_VECTOR_SUBPARTS (cons_elem);
6586 }
6587 unsigned HOST_WIDE_INT elt, count, const_k;
6588 }
6589 (switch
6590 /* We keep an exact subset of the constructor elements. */
6591 (if (multiple_p (idx, k, &elt) && multiple_p (n, k, &count))
6592 (if (CONSTRUCTOR_NELTS (ctor) == 0)
6593 { build_zero_cst (type); }
6594 (if (count == 1)
6595 (if (elt < CONSTRUCTOR_NELTS (ctor))
6596 (view_convert { CONSTRUCTOR_ELT (ctor, elt)->value; })
6597 { build_zero_cst (type); })
6598 /* We don't want to emit new CTORs unless the old one goes away.
6599 ??? Eventually allow this if the CTOR ends up constant or
6600 uniform. */
6601 (if (single_use (@0))
6602 (with
6603 {
6604 vec<constructor_elt, va_gc> *vals;
6605 vec_alloc (vals, count);
6606 bool constant_p = true;
6607 tree res;
6608 for (unsigned i = 0;
6609 i < count && elt + i < CONSTRUCTOR_NELTS (ctor); ++i)
6610 {
6611 tree e = CONSTRUCTOR_ELT (ctor, elt + i)->value;
6612 CONSTRUCTOR_APPEND_ELT (vals, NULL_TREE, e);
6613 if (!CONSTANT_CLASS_P (e))
6614 constant_p = false;
6615 }
6616 tree evtype = (types_match (TREE_TYPE (type),
6617 TREE_TYPE (TREE_TYPE (ctor)))
6618 ? type
6619 : build_vector_type (TREE_TYPE (TREE_TYPE (ctor)),
6620 count));
6621 res = (constant_p ? build_vector_from_ctor (evtype, vals)
6622 : build_constructor (evtype, vals));
6623 }
6624 (view_convert { res; }))))))
6625 /* The bitfield references a single constructor element. */
6626 (if (k.is_constant (&const_k)
6627 && idx + n <= (idx / const_k + 1) * const_k)
6628 (switch
6629 (if (CONSTRUCTOR_NELTS (ctor) <= idx / const_k)
6630 { build_zero_cst (type); })
6631 (if (n == const_k)
6632 (view_convert { CONSTRUCTOR_ELT (ctor, idx / const_k)->value; }))
6633 (BIT_FIELD_REF { CONSTRUCTOR_ELT (ctor, idx / const_k)->value; }
6634 @1 { bitsize_int ((idx % const_k) * width); })))))))))
6635
6636 /* Simplify a bit extraction from a bit insertion for the cases with
6637 the inserted element fully covering the extraction or the insertion
6638 not touching the extraction. */
6639 (simplify
6640 (BIT_FIELD_REF (bit_insert @0 @1 @ipos) @rsize @rpos)
6641 (with
6642 {
6643 unsigned HOST_WIDE_INT isize;
6644 if (INTEGRAL_TYPE_P (TREE_TYPE (@1)))
6645 isize = TYPE_PRECISION (TREE_TYPE (@1));
6646 else
6647 isize = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (@1)));
6648 }
6649 (switch
6650 (if (wi::leu_p (wi::to_wide (@ipos), wi::to_wide (@rpos))
6651 && wi::leu_p (wi::to_wide (@rpos) + wi::to_wide (@rsize),
6652 wi::to_wide (@ipos) + isize))
6653 (BIT_FIELD_REF @1 @rsize { wide_int_to_tree (bitsizetype,
6654 wi::to_wide (@rpos)
6655 - wi::to_wide (@ipos)); }))
6656 (if (wi::geu_p (wi::to_wide (@ipos),
6657 wi::to_wide (@rpos) + wi::to_wide (@rsize))
6658 || wi::geu_p (wi::to_wide (@rpos),
6659 wi::to_wide (@ipos) + isize))
6660 (BIT_FIELD_REF @0 @rsize @rpos)))))
6661
6662 (if (canonicalize_math_after_vectorization_p ())
6663 (for fmas (FMA)
6664 (simplify
6665 (fmas:c (negate @0) @1 @2)
6666 (IFN_FNMA @0 @1 @2))
6667 (simplify
6668 (fmas @0 @1 (negate @2))
6669 (IFN_FMS @0 @1 @2))
6670 (simplify
6671 (fmas:c (negate @0) @1 (negate @2))
6672 (IFN_FNMS @0 @1 @2))
6673 (simplify
6674 (negate (fmas@3 @0 @1 @2))
6675 (if (single_use (@3))
6676 (IFN_FNMS @0 @1 @2))))
6677
6678 (simplify
6679 (IFN_FMS:c (negate @0) @1 @2)
6680 (IFN_FNMS @0 @1 @2))
6681 (simplify
6682 (IFN_FMS @0 @1 (negate @2))
6683 (IFN_FMA @0 @1 @2))
6684 (simplify
6685 (IFN_FMS:c (negate @0) @1 (negate @2))
6686 (IFN_FNMA @0 @1 @2))
6687 (simplify
6688 (negate (IFN_FMS@3 @0 @1 @2))
6689 (if (single_use (@3))
6690 (IFN_FNMA @0 @1 @2)))
6691
6692 (simplify
6693 (IFN_FNMA:c (negate @0) @1 @2)
6694 (IFN_FMA @0 @1 @2))
6695 (simplify
6696 (IFN_FNMA @0 @1 (negate @2))
6697 (IFN_FNMS @0 @1 @2))
6698 (simplify
6699 (IFN_FNMA:c (negate @0) @1 (negate @2))
6700 (IFN_FMS @0 @1 @2))
6701 (simplify
6702 (negate (IFN_FNMA@3 @0 @1 @2))
6703 (if (single_use (@3))
6704 (IFN_FMS @0 @1 @2)))
6705
6706 (simplify
6707 (IFN_FNMS:c (negate @0) @1 @2)
6708 (IFN_FMS @0 @1 @2))
6709 (simplify
6710 (IFN_FNMS @0 @1 (negate @2))
6711 (IFN_FNMA @0 @1 @2))
6712 (simplify
6713 (IFN_FNMS:c (negate @0) @1 (negate @2))
6714 (IFN_FMA @0 @1 @2))
6715 (simplify
6716 (negate (IFN_FNMS@3 @0 @1 @2))
6717 (if (single_use (@3))
6718 (IFN_FMA @0 @1 @2))))
6719
6720 /* CLZ simplifications. */
6721 (for clz (CLZ)
6722 (for op (eq ne)
6723 cmp (lt ge)
6724 (simplify
6725 (op (clz:s@2 @0) INTEGER_CST@1)
6726 (if (integer_zerop (@1) && single_use (@2))
6727 /* clz(X) == 0 is (int)X < 0 and clz(X) != 0 is (int)X >= 0. */
6728 (with { tree type0 = TREE_TYPE (@0);
6729 tree stype = signed_type_for (type0);
6730 HOST_WIDE_INT val = 0;
6731 /* Punt on hypothetical weird targets. */
6732 if (clz == CFN_CLZ
6733 && CLZ_DEFINED_VALUE_AT_ZERO (SCALAR_TYPE_MODE (type0),
6734 val) == 2
6735 && val == 0)
6736 stype = NULL_TREE;
6737 }
6738 (if (stype)
6739 (cmp (convert:stype @0) { build_zero_cst (stype); })))
6740 /* clz(X) == (prec-1) is X == 1 and clz(X) != (prec-1) is X != 1. */
6741 (with { bool ok = true;
6742 HOST_WIDE_INT val = 0;
6743 tree type0 = TREE_TYPE (@0);
6744 /* Punt on hypothetical weird targets. */
6745 if (clz == CFN_CLZ
6746 && CLZ_DEFINED_VALUE_AT_ZERO (SCALAR_TYPE_MODE (type0),
6747 val) == 2
6748 && val == TYPE_PRECISION (type0) - 1)
6749 ok = false;
6750 }
6751 (if (ok && wi::to_wide (@1) == (TYPE_PRECISION (type0) - 1))
6752 (op @0 { build_one_cst (type0); })))))))
6753
6754 /* CTZ simplifications. */
6755 (for ctz (CTZ)
6756 (for op (ge gt le lt)
6757 cmp (eq eq ne ne)
6758 (simplify
6759 /* __builtin_ctz (x) >= C -> (x & ((1 << C) - 1)) == 0. */
6760 (op (ctz:s @0) INTEGER_CST@1)
6761 (with { bool ok = true;
6762 HOST_WIDE_INT val = 0;
6763 if (!tree_fits_shwi_p (@1))
6764 ok = false;
6765 else
6766 {
6767 val = tree_to_shwi (@1);
6768 /* Canonicalize to >= or <. */
6769 if (op == GT_EXPR || op == LE_EXPR)
6770 {
6771 if (val == HOST_WIDE_INT_MAX)
6772 ok = false;
6773 else
6774 val++;
6775 }
6776 }
6777 bool zero_res = false;
6778 HOST_WIDE_INT zero_val = 0;
6779 tree type0 = TREE_TYPE (@0);
6780 int prec = TYPE_PRECISION (type0);
6781 if (ctz == CFN_CTZ
6782 && CTZ_DEFINED_VALUE_AT_ZERO (SCALAR_TYPE_MODE (type0),
6783 zero_val) == 2)
6784 zero_res = true;
6785 }
6786 (if (val <= 0)
6787 (if (ok && (!zero_res || zero_val >= val))
6788 { constant_boolean_node (cmp == EQ_EXPR ? true : false, type); })
6789 (if (val >= prec)
6790 (if (ok && (!zero_res || zero_val < val))
6791 { constant_boolean_node (cmp == EQ_EXPR ? false : true, type); })
6792 (if (ok && (!zero_res || zero_val < 0 || zero_val >= prec))
6793 (cmp (bit_and @0 { wide_int_to_tree (type0,
6794 wi::mask (val, false, prec)); })
6795 { build_zero_cst (type0); })))))))
6796 (for op (eq ne)
6797 (simplify
6798 /* __builtin_ctz (x) == C -> (x & ((1 << (C + 1)) - 1)) == (1 << C). */
6799 (op (ctz:s @0) INTEGER_CST@1)
6800 (with { bool zero_res = false;
6801 HOST_WIDE_INT zero_val = 0;
6802 tree type0 = TREE_TYPE (@0);
6803 int prec = TYPE_PRECISION (type0);
6804 if (ctz == CFN_CTZ
6805 && CTZ_DEFINED_VALUE_AT_ZERO (SCALAR_TYPE_MODE (type0),
6806 zero_val) == 2)
6807 zero_res = true;
6808 }
6809 (if (tree_int_cst_sgn (@1) < 0 || wi::to_widest (@1) >= prec)
6810 (if (!zero_res || zero_val != wi::to_widest (@1))
6811 { constant_boolean_node (op == EQ_EXPR ? false : true, type); })
6812 (if (!zero_res || zero_val < 0 || zero_val >= prec)
6813 (op (bit_and @0 { wide_int_to_tree (type0,
6814 wi::mask (tree_to_uhwi (@1) + 1,
6815 false, prec)); })
6816 { wide_int_to_tree (type0,
6817 wi::shifted_mask (tree_to_uhwi (@1), 1,
6818 false, prec)); })))))))
6819
6820 /* POPCOUNT simplifications. */
6821 /* popcount(X) + popcount(Y) is popcount(X|Y) when X&Y must be zero. */
6822 (simplify
6823 (plus (POPCOUNT:s @0) (POPCOUNT:s @1))
6824 (if (wi::bit_and (tree_nonzero_bits (@0), tree_nonzero_bits (@1)) == 0)
6825 (POPCOUNT (bit_ior @0 @1))))
6826
6827 /* popcount(X) == 0 is X == 0, and related (in)equalities. */
6828 (for popcount (POPCOUNT)
6829 (for cmp (le eq ne gt)
6830 rep (eq eq ne ne)
6831 (simplify
6832 (cmp (popcount @0) integer_zerop)
6833 (rep @0 { build_zero_cst (TREE_TYPE (@0)); }))))
6834
6835 /* Canonicalize POPCOUNT(x)&1 as PARITY(X). */
6836 (simplify
6837 (bit_and (POPCOUNT @0) integer_onep)
6838 (PARITY @0))
6839
6840 /* PARITY simplifications. */
6841 /* parity(~X) is parity(X). */
6842 (simplify
6843 (PARITY (bit_not @0))
6844 (PARITY @0))
6845
6846 /* parity(X)^parity(Y) is parity(X^Y). */
6847 (simplify
6848 (bit_xor (PARITY:s @0) (PARITY:s @1))
6849 (PARITY (bit_xor @0 @1)))
6850
6851 /* Common POPCOUNT/PARITY simplifications. */
6852 /* popcount(X&C1) is (X>>C2)&1 when C1 == 1<<C2. Same for parity(X&C1). */
6853 (for pfun (POPCOUNT PARITY)
6854 (simplify
6855 (pfun @0)
6856 (with { wide_int nz = tree_nonzero_bits (@0); }
6857 (switch
6858 (if (nz == 1)
6859 (convert @0))
6860 (if (wi::popcount (nz) == 1)
6861 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
6862 (convert (rshift:utype (convert:utype @0)
6863 { build_int_cst (integer_type_node,
6864 wi::ctz (nz)); }))))))))
6865
6866 #if GIMPLE
6867 /* 64- and 32-bits branchless implementations of popcount are detected:
6868
6869 int popcount64c (uint64_t x)
6870 {
6871 x -= (x >> 1) & 0x5555555555555555ULL;
6872 x = (x & 0x3333333333333333ULL) + ((x >> 2) & 0x3333333333333333ULL);
6873 x = (x + (x >> 4)) & 0x0f0f0f0f0f0f0f0fULL;
6874 return (x * 0x0101010101010101ULL) >> 56;
6875 }
6876
6877 int popcount32c (uint32_t x)
6878 {
6879 x -= (x >> 1) & 0x55555555;
6880 x = (x & 0x33333333) + ((x >> 2) & 0x33333333);
6881 x = (x + (x >> 4)) & 0x0f0f0f0f;
6882 return (x * 0x01010101) >> 24;
6883 } */
6884 (simplify
6885 (rshift
6886 (mult
6887 (bit_and
6888 (plus:c
6889 (rshift @8 INTEGER_CST@5)
6890 (plus:c@8
6891 (bit_and @6 INTEGER_CST@7)
6892 (bit_and
6893 (rshift
6894 (minus@6 @0
6895 (bit_and (rshift @0 INTEGER_CST@4) INTEGER_CST@11))
6896 INTEGER_CST@10)
6897 INTEGER_CST@9)))
6898 INTEGER_CST@3)
6899 INTEGER_CST@2)
6900 INTEGER_CST@1)
6901 /* Check constants and optab. */
6902 (with { unsigned prec = TYPE_PRECISION (type);
6903 int shift = (64 - prec) & 63;
6904 unsigned HOST_WIDE_INT c1
6905 = HOST_WIDE_INT_UC (0x0101010101010101) >> shift;
6906 unsigned HOST_WIDE_INT c2
6907 = HOST_WIDE_INT_UC (0x0F0F0F0F0F0F0F0F) >> shift;
6908 unsigned HOST_WIDE_INT c3
6909 = HOST_WIDE_INT_UC (0x3333333333333333) >> shift;
6910 unsigned HOST_WIDE_INT c4
6911 = HOST_WIDE_INT_UC (0x5555555555555555) >> shift;
6912 }
6913 (if (prec >= 16
6914 && prec <= 64
6915 && pow2p_hwi (prec)
6916 && TYPE_UNSIGNED (type)
6917 && integer_onep (@4)
6918 && wi::to_widest (@10) == 2
6919 && wi::to_widest (@5) == 4
6920 && wi::to_widest (@1) == prec - 8
6921 && tree_to_uhwi (@2) == c1
6922 && tree_to_uhwi (@3) == c2
6923 && tree_to_uhwi (@9) == c3
6924 && tree_to_uhwi (@7) == c3
6925 && tree_to_uhwi (@11) == c4)
6926 (if (direct_internal_fn_supported_p (IFN_POPCOUNT, type,
6927 OPTIMIZE_FOR_BOTH))
6928 (convert (IFN_POPCOUNT:type @0))
6929 /* Try to do popcount in two halves. PREC must be at least
6930 five bits for this to work without extension before adding. */
6931 (with {
6932 tree half_type = NULL_TREE;
6933 opt_machine_mode m = mode_for_size ((prec + 1) / 2, MODE_INT, 1);
6934 int half_prec = 8;
6935 if (m.exists ()
6936 && m.require () != TYPE_MODE (type))
6937 {
6938 half_prec = GET_MODE_PRECISION (as_a <scalar_int_mode> (m));
6939 half_type = build_nonstandard_integer_type (half_prec, 1);
6940 }
6941 gcc_assert (half_prec > 2);
6942 }
6943 (if (half_type != NULL_TREE
6944 && direct_internal_fn_supported_p (IFN_POPCOUNT, half_type,
6945 OPTIMIZE_FOR_BOTH))
6946 (convert (plus
6947 (IFN_POPCOUNT:half_type (convert @0))
6948 (IFN_POPCOUNT:half_type (convert (rshift @0
6949 { build_int_cst (integer_type_node, half_prec); } )))))))))))
6950
6951 /* __builtin_ffs needs to deal on many targets with the possible zero
6952 argument. If we know the argument is always non-zero, __builtin_ctz + 1
6953 should lead to better code. */
6954 (simplify
6955 (FFS tree_expr_nonzero_p@0)
6956 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6957 && direct_internal_fn_supported_p (IFN_CTZ, TREE_TYPE (@0),
6958 OPTIMIZE_FOR_SPEED))
6959 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
6960 (plus (CTZ:type (convert:utype @0)) { build_one_cst (type); }))))
6961 #endif
6962
6963 (for ffs (BUILT_IN_FFS BUILT_IN_FFSL BUILT_IN_FFSLL
6964 BUILT_IN_FFSIMAX)
6965 /* __builtin_ffs (X) == 0 -> X == 0.
6966 __builtin_ffs (X) == 6 -> (X & 63) == 32. */
6967 (for cmp (eq ne)
6968 (simplify
6969 (cmp (ffs@2 @0) INTEGER_CST@1)
6970 (with { int prec = TYPE_PRECISION (TREE_TYPE (@0)); }
6971 (switch
6972 (if (integer_zerop (@1))
6973 (cmp @0 { build_zero_cst (TREE_TYPE (@0)); }))
6974 (if (tree_int_cst_sgn (@1) < 0 || wi::to_widest (@1) > prec)
6975 { constant_boolean_node (cmp == NE_EXPR ? true : false, type); })
6976 (if (single_use (@2))
6977 (cmp (bit_and @0 { wide_int_to_tree (TREE_TYPE (@0),
6978 wi::mask (tree_to_uhwi (@1),
6979 false, prec)); })
6980 { wide_int_to_tree (TREE_TYPE (@0),
6981 wi::shifted_mask (tree_to_uhwi (@1) - 1, 1,
6982 false, prec)); }))))))
6983
6984 /* __builtin_ffs (X) > 6 -> X != 0 && (X & 63) == 0. */
6985 (for cmp (gt le)
6986 cmp2 (ne eq)
6987 cmp3 (eq ne)
6988 bit_op (bit_and bit_ior)
6989 (simplify
6990 (cmp (ffs@2 @0) INTEGER_CST@1)
6991 (with { int prec = TYPE_PRECISION (TREE_TYPE (@0)); }
6992 (switch
6993 (if (integer_zerop (@1))
6994 (cmp2 @0 { build_zero_cst (TREE_TYPE (@0)); }))
6995 (if (tree_int_cst_sgn (@1) < 0)
6996 { constant_boolean_node (cmp == GT_EXPR ? true : false, type); })
6997 (if (wi::to_widest (@1) >= prec)
6998 { constant_boolean_node (cmp == GT_EXPR ? false : true, type); })
6999 (if (wi::to_widest (@1) == prec - 1)
7000 (cmp3 @0 { wide_int_to_tree (TREE_TYPE (@0),
7001 wi::shifted_mask (prec - 1, 1,
7002 false, prec)); }))
7003 (if (single_use (@2))
7004 (bit_op (cmp2 @0 { build_zero_cst (TREE_TYPE (@0)); })
7005 (cmp3 (bit_and @0
7006 { wide_int_to_tree (TREE_TYPE (@0),
7007 wi::mask (tree_to_uhwi (@1),
7008 false, prec)); })
7009 { build_zero_cst (TREE_TYPE (@0)); }))))))))
7010
7011 /* Simplify:
7012
7013 a = a1 op a2
7014 r = c ? a : b;
7015
7016 to:
7017
7018 r = c ? a1 op a2 : b;
7019
7020 if the target can do it in one go. This makes the operation conditional
7021 on c, so could drop potentially-trapping arithmetic, but that's a valid
7022 simplification if the result of the operation isn't needed.
7023
7024 Avoid speculatively generating a stand-alone vector comparison
7025 on targets that might not support them. Any target implementing
7026 conditional internal functions must support the same comparisons
7027 inside and outside a VEC_COND_EXPR. */
7028
7029 #if GIMPLE
7030 (for uncond_op (UNCOND_BINARY)
7031 cond_op (COND_BINARY)
7032 (simplify
7033 (vec_cond @0 (view_convert? (uncond_op@4 @1 @2)) @3)
7034 (with { tree op_type = TREE_TYPE (@4); }
7035 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
7036 && is_truth_type_for (op_type, TREE_TYPE (@0)))
7037 (view_convert (cond_op @0 @1 @2 (view_convert:op_type @3))))))
7038 (simplify
7039 (vec_cond @0 @1 (view_convert? (uncond_op@4 @2 @3)))
7040 (with { tree op_type = TREE_TYPE (@4); }
7041 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
7042 && is_truth_type_for (op_type, TREE_TYPE (@0)))
7043 (view_convert (cond_op (bit_not @0) @2 @3 (view_convert:op_type @1)))))))
7044
7045 /* Same for ternary operations. */
7046 (for uncond_op (UNCOND_TERNARY)
7047 cond_op (COND_TERNARY)
7048 (simplify
7049 (vec_cond @0 (view_convert? (uncond_op@5 @1 @2 @3)) @4)
7050 (with { tree op_type = TREE_TYPE (@5); }
7051 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
7052 && is_truth_type_for (op_type, TREE_TYPE (@0)))
7053 (view_convert (cond_op @0 @1 @2 @3 (view_convert:op_type @4))))))
7054 (simplify
7055 (vec_cond @0 @1 (view_convert? (uncond_op@5 @2 @3 @4)))
7056 (with { tree op_type = TREE_TYPE (@5); }
7057 (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
7058 && is_truth_type_for (op_type, TREE_TYPE (@0)))
7059 (view_convert (cond_op (bit_not @0) @2 @3 @4
7060 (view_convert:op_type @1)))))))
7061 #endif
7062
7063 /* Detect cases in which a VEC_COND_EXPR effectively replaces the
7064 "else" value of an IFN_COND_*. */
7065 (for cond_op (COND_BINARY)
7066 (simplify
7067 (vec_cond @0 (view_convert? (cond_op @0 @1 @2 @3)) @4)
7068 (with { tree op_type = TREE_TYPE (@3); }
7069 (if (element_precision (type) == element_precision (op_type))
7070 (view_convert (cond_op @0 @1 @2 (view_convert:op_type @4))))))
7071 (simplify
7072 (vec_cond @0 @1 (view_convert? (cond_op @2 @3 @4 @5)))
7073 (with { tree op_type = TREE_TYPE (@5); }
7074 (if (inverse_conditions_p (@0, @2)
7075 && element_precision (type) == element_precision (op_type))
7076 (view_convert (cond_op @2 @3 @4 (view_convert:op_type @1)))))))
7077
7078 /* Same for ternary operations. */
7079 (for cond_op (COND_TERNARY)
7080 (simplify
7081 (vec_cond @0 (view_convert? (cond_op @0 @1 @2 @3 @4)) @5)
7082 (with { tree op_type = TREE_TYPE (@4); }
7083 (if (element_precision (type) == element_precision (op_type))
7084 (view_convert (cond_op @0 @1 @2 @3 (view_convert:op_type @5))))))
7085 (simplify
7086 (vec_cond @0 @1 (view_convert? (cond_op @2 @3 @4 @5 @6)))
7087 (with { tree op_type = TREE_TYPE (@6); }
7088 (if (inverse_conditions_p (@0, @2)
7089 && element_precision (type) == element_precision (op_type))
7090 (view_convert (cond_op @2 @3 @4 @5 (view_convert:op_type @1)))))))
7091
7092 /* For pointers @0 and @2 and nonnegative constant offset @1, look for
7093 expressions like:
7094
7095 A: (@0 + @1 < @2) | (@2 + @1 < @0)
7096 B: (@0 + @1 <= @2) | (@2 + @1 <= @0)
7097
7098 If pointers are known not to wrap, B checks whether @1 bytes starting
7099 at @0 and @2 do not overlap, while A tests the same thing for @1 + 1
7100 bytes. A is more efficiently tested as:
7101
7102 A: (sizetype) (@0 + @1 - @2) > @1 * 2
7103
7104 The equivalent expression for B is given by replacing @1 with @1 - 1:
7105
7106 B: (sizetype) (@0 + (@1 - 1) - @2) > (@1 - 1) * 2
7107
7108 @0 and @2 can be swapped in both expressions without changing the result.
7109
7110 The folds rely on sizetype's being unsigned (which is always true)
7111 and on its being the same width as the pointer (which we have to check).
7112
7113 The fold replaces two pointer_plus expressions, two comparisons and
7114 an IOR with a pointer_plus, a pointer_diff, and a comparison, so in
7115 the best case it's a saving of two operations. The A fold retains one
7116 of the original pointer_pluses, so is a win even if both pointer_pluses
7117 are used elsewhere. The B fold is a wash if both pointer_pluses are
7118 used elsewhere, since all we end up doing is replacing a comparison with
7119 a pointer_plus. We do still apply the fold under those circumstances
7120 though, in case applying it to other conditions eventually makes one of the
7121 pointer_pluses dead. */
7122 (for ior (truth_orif truth_or bit_ior)
7123 (for cmp (le lt)
7124 (simplify
7125 (ior (cmp:cs (pointer_plus@3 @0 INTEGER_CST@1) @2)
7126 (cmp:cs (pointer_plus@4 @2 @1) @0))
7127 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
7128 && TYPE_OVERFLOW_WRAPS (sizetype)
7129 && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (sizetype))
7130 /* Calculate the rhs constant. */
7131 (with { offset_int off = wi::to_offset (@1) - (cmp == LE_EXPR ? 1 : 0);
7132 offset_int rhs = off * 2; }
7133 /* Always fails for negative values. */
7134 (if (wi::min_precision (rhs, UNSIGNED) <= TYPE_PRECISION (sizetype))
7135 /* Since the order of @0 and @2 doesn't matter, let tree_swap_operands_p
7136 pick a canonical order. This increases the chances of using the
7137 same pointer_plus in multiple checks. */
7138 (with { bool swap_p = tree_swap_operands_p (@0, @2);
7139 tree rhs_tree = wide_int_to_tree (sizetype, rhs); }
7140 (if (cmp == LT_EXPR)
7141 (gt (convert:sizetype
7142 (pointer_diff:ssizetype { swap_p ? @4 : @3; }
7143 { swap_p ? @0 : @2; }))
7144 { rhs_tree; })
7145 (gt (convert:sizetype
7146 (pointer_diff:ssizetype
7147 (pointer_plus { swap_p ? @2 : @0; }
7148 { wide_int_to_tree (sizetype, off); })
7149 { swap_p ? @0 : @2; }))
7150 { rhs_tree; })))))))))
7151
7152 /* Fold REDUC (@0 & @1) -> @0[I] & @1[I] if element I is the only nonzero
7153 element of @1. */
7154 (for reduc (IFN_REDUC_PLUS IFN_REDUC_IOR IFN_REDUC_XOR)
7155 (simplify (reduc (view_convert? (bit_and @0 VECTOR_CST@1)))
7156 (with { int i = single_nonzero_element (@1); }
7157 (if (i >= 0)
7158 (with { tree elt = vector_cst_elt (@1, i);
7159 tree elt_type = TREE_TYPE (elt);
7160 unsigned int elt_bits = tree_to_uhwi (TYPE_SIZE (elt_type));
7161 tree size = bitsize_int (elt_bits);
7162 tree pos = bitsize_int (elt_bits * i); }
7163 (view_convert
7164 (bit_and:elt_type
7165 (BIT_FIELD_REF:elt_type @0 { size; } { pos; })
7166 { elt; })))))))
7167
7168 (simplify
7169 (vec_perm @0 @1 VECTOR_CST@2)
7170 (with
7171 {
7172 tree op0 = @0, op1 = @1, op2 = @2;
7173
7174 /* Build a vector of integers from the tree mask. */
7175 vec_perm_builder builder;
7176 if (!tree_to_vec_perm_builder (&builder, op2))
7177 return NULL_TREE;
7178
7179 /* Create a vec_perm_indices for the integer vector. */
7180 poly_uint64 nelts = TYPE_VECTOR_SUBPARTS (type);
7181 bool single_arg = (op0 == op1);
7182 vec_perm_indices sel (builder, single_arg ? 1 : 2, nelts);
7183 }
7184 (if (sel.series_p (0, 1, 0, 1))
7185 { op0; }
7186 (if (sel.series_p (0, 1, nelts, 1))
7187 { op1; }
7188 (with
7189 {
7190 if (!single_arg)
7191 {
7192 if (sel.all_from_input_p (0))
7193 op1 = op0;
7194 else if (sel.all_from_input_p (1))
7195 {
7196 op0 = op1;
7197 sel.rotate_inputs (1);
7198 }
7199 else if (known_ge (poly_uint64 (sel[0]), nelts))
7200 {
7201 std::swap (op0, op1);
7202 sel.rotate_inputs (1);
7203 }
7204 }
7205 gassign *def;
7206 tree cop0 = op0, cop1 = op1;
7207 if (TREE_CODE (op0) == SSA_NAME
7208 && (def = dyn_cast <gassign *> (SSA_NAME_DEF_STMT (op0)))
7209 && gimple_assign_rhs_code (def) == CONSTRUCTOR)
7210 cop0 = gimple_assign_rhs1 (def);
7211 if (TREE_CODE (op1) == SSA_NAME
7212 && (def = dyn_cast <gassign *> (SSA_NAME_DEF_STMT (op1)))
7213 && gimple_assign_rhs_code (def) == CONSTRUCTOR)
7214 cop1 = gimple_assign_rhs1 (def);
7215
7216 tree t;
7217 }
7218 (if ((TREE_CODE (cop0) == VECTOR_CST
7219 || TREE_CODE (cop0) == CONSTRUCTOR)
7220 && (TREE_CODE (cop1) == VECTOR_CST
7221 || TREE_CODE (cop1) == CONSTRUCTOR)
7222 && (t = fold_vec_perm (type, cop0, cop1, sel)))
7223 { t; }
7224 (with
7225 {
7226 bool changed = (op0 == op1 && !single_arg);
7227 tree ins = NULL_TREE;
7228 unsigned at = 0;
7229
7230 /* See if the permutation is performing a single element
7231 insert from a CONSTRUCTOR or constant and use a BIT_INSERT_EXPR
7232 in that case. But only if the vector mode is supported,
7233 otherwise this is invalid GIMPLE. */
7234 if (TYPE_MODE (type) != BLKmode
7235 && (TREE_CODE (cop0) == VECTOR_CST
7236 || TREE_CODE (cop0) == CONSTRUCTOR
7237 || TREE_CODE (cop1) == VECTOR_CST
7238 || TREE_CODE (cop1) == CONSTRUCTOR))
7239 {
7240 bool insert_first_p = sel.series_p (1, 1, nelts + 1, 1);
7241 if (insert_first_p)
7242 {
7243 /* After canonicalizing the first elt to come from the
7244 first vector we only can insert the first elt from
7245 the first vector. */
7246 at = 0;
7247 if ((ins = fold_read_from_vector (cop0, sel[0])))
7248 op0 = op1;
7249 }
7250 /* The above can fail for two-element vectors which always
7251 appear to insert the first element, so try inserting
7252 into the second lane as well. For more than two
7253 elements that's wasted time. */
7254 if (!insert_first_p || (!ins && maybe_eq (nelts, 2u)))
7255 {
7256 unsigned int encoded_nelts = sel.encoding ().encoded_nelts ();
7257 for (at = 0; at < encoded_nelts; ++at)
7258 if (maybe_ne (sel[at], at))
7259 break;
7260 if (at < encoded_nelts
7261 && (known_eq (at + 1, nelts)
7262 || sel.series_p (at + 1, 1, at + 1, 1)))
7263 {
7264 if (known_lt (poly_uint64 (sel[at]), nelts))
7265 ins = fold_read_from_vector (cop0, sel[at]);
7266 else
7267 ins = fold_read_from_vector (cop1, sel[at] - nelts);
7268 }
7269 }
7270 }
7271
7272 /* Generate a canonical form of the selector. */
7273 if (!ins && sel.encoding () != builder)
7274 {
7275 /* Some targets are deficient and fail to expand a single
7276 argument permutation while still allowing an equivalent
7277 2-argument version. */
7278 tree oldop2 = op2;
7279 if (sel.ninputs () == 2
7280 || can_vec_perm_const_p (TYPE_MODE (type), sel, false))
7281 op2 = vec_perm_indices_to_tree (TREE_TYPE (op2), sel);
7282 else
7283 {
7284 vec_perm_indices sel2 (builder, 2, nelts);
7285 if (can_vec_perm_const_p (TYPE_MODE (type), sel2, false))
7286 op2 = vec_perm_indices_to_tree (TREE_TYPE (op2), sel2);
7287 else
7288 /* Not directly supported with either encoding,
7289 so use the preferred form. */
7290 op2 = vec_perm_indices_to_tree (TREE_TYPE (op2), sel);
7291 }
7292 if (!operand_equal_p (op2, oldop2, 0))
7293 changed = true;
7294 }
7295 }
7296 (if (ins)
7297 (bit_insert { op0; } { ins; }
7298 { bitsize_int (at * vector_element_bits (type)); })
7299 (if (changed)
7300 (vec_perm { op0; } { op1; } { op2; }))))))))))
7301
7302 /* VEC_PERM_EXPR (v, v, mask) -> v where v contains same element. */
7303
7304 (match vec_same_elem_p
7305 @0
7306 (if (uniform_vector_p (@0))))
7307
7308 (match vec_same_elem_p
7309 (vec_duplicate @0))
7310
7311 (simplify
7312 (vec_perm vec_same_elem_p@0 @0 @1)
7313 @0)
7314
7315 /* Match count trailing zeroes for simplify_count_trailing_zeroes in fwprop.
7316 The canonical form is array[((x & -x) * C) >> SHIFT] where C is a magic
7317 constant which when multiplied by a power of 2 contains a unique value
7318 in the top 5 or 6 bits. This is then indexed into a table which maps it
7319 to the number of trailing zeroes. */
7320 (match (ctz_table_index @1 @2 @3)
7321 (rshift (mult (bit_and:c (negate @1) @1) INTEGER_CST@2) INTEGER_CST@3))