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