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