Daily bump.
[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-2017 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 CONSTANT_CLASS_P
33 tree_expr_nonnegative_p
34 tree_expr_nonzero_p
35 integer_valued_real_p
36 integer_pow2p
37 HONOR_NANS)
38
39 /* Operator lists. */
40 (define_operator_list tcc_comparison
41 lt le eq ne ge gt unordered ordered unlt unle ungt unge uneq ltgt)
42 (define_operator_list inverted_tcc_comparison
43 ge gt ne eq lt le ordered unordered ge gt le lt ltgt uneq)
44 (define_operator_list inverted_tcc_comparison_with_nans
45 unge ungt ne eq unlt unle ordered unordered ge gt le lt ltgt uneq)
46 (define_operator_list swapped_tcc_comparison
47 gt ge eq ne le lt unordered ordered ungt unge unlt unle uneq ltgt)
48 (define_operator_list simple_comparison lt le eq ne ge gt)
49 (define_operator_list swapped_simple_comparison gt ge eq ne le lt)
50
51 #include "cfn-operators.pd"
52
53 /* Define operand lists for math rounding functions {,i,l,ll}FN,
54 where the versions prefixed with "i" return an int, those prefixed with
55 "l" return a long and those prefixed with "ll" return a long long.
56
57 Also define operand lists:
58
59 X<FN>F for all float functions, in the order i, l, ll
60 X<FN> for all double functions, in the same order
61 X<FN>L for all long double functions, in the same order. */
62 #define DEFINE_INT_AND_FLOAT_ROUND_FN(FN) \
63 (define_operator_list X##FN##F BUILT_IN_I##FN##F \
64 BUILT_IN_L##FN##F \
65 BUILT_IN_LL##FN##F) \
66 (define_operator_list X##FN BUILT_IN_I##FN \
67 BUILT_IN_L##FN \
68 BUILT_IN_LL##FN) \
69 (define_operator_list X##FN##L BUILT_IN_I##FN##L \
70 BUILT_IN_L##FN##L \
71 BUILT_IN_LL##FN##L)
72
73 DEFINE_INT_AND_FLOAT_ROUND_FN (FLOOR)
74 DEFINE_INT_AND_FLOAT_ROUND_FN (CEIL)
75 DEFINE_INT_AND_FLOAT_ROUND_FN (ROUND)
76 DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
77
78 /* Simplifications of operations with one constant operand and
79 simplifications to constants or single values. */
80
81 (for op (plus pointer_plus minus bit_ior bit_xor)
82 (simplify
83 (op @0 integer_zerop)
84 (non_lvalue @0)))
85
86 /* 0 +p index -> (type)index */
87 (simplify
88 (pointer_plus integer_zerop @1)
89 (non_lvalue (convert @1)))
90
91 /* See if ARG1 is zero and X + ARG1 reduces to X.
92 Likewise if the operands are reversed. */
93 (simplify
94 (plus:c @0 real_zerop@1)
95 (if (fold_real_zero_addition_p (type, @1, 0))
96 (non_lvalue @0)))
97
98 /* See if ARG1 is zero and X - ARG1 reduces to X. */
99 (simplify
100 (minus @0 real_zerop@1)
101 (if (fold_real_zero_addition_p (type, @1, 1))
102 (non_lvalue @0)))
103
104 /* Simplify x - x.
105 This is unsafe for certain floats even in non-IEEE formats.
106 In IEEE, it is unsafe because it does wrong for NaNs.
107 Also note that operand_equal_p is always false if an operand
108 is volatile. */
109 (simplify
110 (minus @0 @0)
111 (if (!FLOAT_TYPE_P (type) || !HONOR_NANS (type))
112 { build_zero_cst (type); }))
113
114 (simplify
115 (mult @0 integer_zerop@1)
116 @1)
117
118 /* Maybe fold x * 0 to 0. The expressions aren't the same
119 when x is NaN, since x * 0 is also NaN. Nor are they the
120 same in modes with signed zeros, since multiplying a
121 negative value by 0 gives -0, not +0. */
122 (simplify
123 (mult @0 real_zerop@1)
124 (if (!HONOR_NANS (type) && !HONOR_SIGNED_ZEROS (type))
125 @1))
126
127 /* In IEEE floating point, x*1 is not equivalent to x for snans.
128 Likewise for complex arithmetic with signed zeros. */
129 (simplify
130 (mult @0 real_onep)
131 (if (!HONOR_SNANS (type)
132 && (!HONOR_SIGNED_ZEROS (type)
133 || !COMPLEX_FLOAT_TYPE_P (type)))
134 (non_lvalue @0)))
135
136 /* Transform x * -1.0 into -x. */
137 (simplify
138 (mult @0 real_minus_onep)
139 (if (!HONOR_SNANS (type)
140 && (!HONOR_SIGNED_ZEROS (type)
141 || !COMPLEX_FLOAT_TYPE_P (type)))
142 (negate @0)))
143
144 /* X * 1, X / 1 -> X. */
145 (for op (mult trunc_div ceil_div floor_div round_div exact_div)
146 (simplify
147 (op @0 integer_onep)
148 (non_lvalue @0)))
149
150 /* Preserve explicit divisions by 0: the C++ front-end wants to detect
151 undefined behavior in constexpr evaluation, and assuming that the division
152 traps enables better optimizations than these anyway. */
153 (for div (trunc_div ceil_div floor_div round_div exact_div)
154 /* 0 / X is always zero. */
155 (simplify
156 (div integer_zerop@0 @1)
157 /* But not for 0 / 0 so that we can get the proper warnings and errors. */
158 (if (!integer_zerop (@1))
159 @0))
160 /* X / -1 is -X. */
161 (simplify
162 (div @0 integer_minus_onep@1)
163 (if (!TYPE_UNSIGNED (type))
164 (negate @0)))
165 /* X / X is one. */
166 (simplify
167 (div @0 @0)
168 /* But not for 0 / 0 so that we can get the proper warnings and errors. */
169 (if (!integer_zerop (@0))
170 { build_one_cst (type); }))
171 /* X / abs (X) is X < 0 ? -1 : 1. */
172 (simplify
173 (div:C @0 (abs @0))
174 (if (INTEGRAL_TYPE_P (type)
175 && TYPE_OVERFLOW_UNDEFINED (type))
176 (cond (lt @0 { build_zero_cst (type); })
177 { build_minus_one_cst (type); } { build_one_cst (type); })))
178 /* X / -X is -1. */
179 (simplify
180 (div:C @0 (negate @0))
181 (if ((INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
182 && TYPE_OVERFLOW_UNDEFINED (type))
183 { build_minus_one_cst (type); })))
184
185 /* For unsigned integral types, FLOOR_DIV_EXPR is the same as
186 TRUNC_DIV_EXPR. Rewrite into the latter in this case. */
187 (simplify
188 (floor_div @0 @1)
189 (if ((INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
190 && TYPE_UNSIGNED (type))
191 (trunc_div @0 @1)))
192
193 /* Combine two successive divisions. Note that combining ceil_div
194 and floor_div is trickier and combining round_div even more so. */
195 (for div (trunc_div exact_div)
196 (simplify
197 (div (div @0 INTEGER_CST@1) INTEGER_CST@2)
198 (with {
199 bool overflow_p;
200 wide_int mul = wi::mul (@1, @2, TYPE_SIGN (type), &overflow_p);
201 }
202 (if (!overflow_p)
203 (div @0 { wide_int_to_tree (type, mul); })
204 (if (TYPE_UNSIGNED (type)
205 || mul != wi::min_value (TYPE_PRECISION (type), SIGNED))
206 { build_zero_cst (type); })))))
207
208 /* Optimize A / A to 1.0 if we don't care about
209 NaNs or Infinities. */
210 (simplify
211 (rdiv @0 @0)
212 (if (FLOAT_TYPE_P (type)
213 && ! HONOR_NANS (type)
214 && ! HONOR_INFINITIES (type))
215 { build_one_cst (type); }))
216
217 /* Optimize -A / A to -1.0 if we don't care about
218 NaNs or Infinities. */
219 (simplify
220 (rdiv:C @0 (negate @0))
221 (if (FLOAT_TYPE_P (type)
222 && ! HONOR_NANS (type)
223 && ! HONOR_INFINITIES (type))
224 { build_minus_one_cst (type); }))
225
226 /* PR71078: x / abs(x) -> copysign (1.0, x) */
227 (simplify
228 (rdiv:C (convert? @0) (convert? (abs @0)))
229 (if (SCALAR_FLOAT_TYPE_P (type)
230 && ! HONOR_NANS (type)
231 && ! HONOR_INFINITIES (type))
232 (switch
233 (if (types_match (type, float_type_node))
234 (BUILT_IN_COPYSIGNF { build_one_cst (type); } (convert @0)))
235 (if (types_match (type, double_type_node))
236 (BUILT_IN_COPYSIGN { build_one_cst (type); } (convert @0)))
237 (if (types_match (type, long_double_type_node))
238 (BUILT_IN_COPYSIGNL { build_one_cst (type); } (convert @0))))))
239
240 /* In IEEE floating point, x/1 is not equivalent to x for snans. */
241 (simplify
242 (rdiv @0 real_onep)
243 (if (!HONOR_SNANS (type))
244 (non_lvalue @0)))
245
246 /* In IEEE floating point, x/-1 is not equivalent to -x for snans. */
247 (simplify
248 (rdiv @0 real_minus_onep)
249 (if (!HONOR_SNANS (type))
250 (negate @0)))
251
252 (if (flag_reciprocal_math)
253 /* Convert (A/B)/C to A/(B*C) */
254 (simplify
255 (rdiv (rdiv:s @0 @1) @2)
256 (rdiv @0 (mult @1 @2)))
257
258 /* Convert A/(B/C) to (A/B)*C */
259 (simplify
260 (rdiv @0 (rdiv:s @1 @2))
261 (mult (rdiv @0 @1) @2)))
262
263 /* Optimize (X & (-A)) / A where A is a power of 2, to X >> log2(A) */
264 (for div (trunc_div ceil_div floor_div round_div exact_div)
265 (simplify
266 (div (convert? (bit_and @0 INTEGER_CST@1)) INTEGER_CST@2)
267 (if (integer_pow2p (@2)
268 && tree_int_cst_sgn (@2) > 0
269 && wi::add (@2, @1) == 0
270 && tree_nop_conversion_p (type, TREE_TYPE (@0)))
271 (rshift (convert @0) { build_int_cst (integer_type_node,
272 wi::exact_log2 (@2)); }))))
273
274 /* If ARG1 is a constant, we can convert this to a multiply by the
275 reciprocal. This does not have the same rounding properties,
276 so only do this if -freciprocal-math. We can actually
277 always safely do it if ARG1 is a power of two, but it's hard to
278 tell if it is or not in a portable manner. */
279 (for cst (REAL_CST COMPLEX_CST VECTOR_CST)
280 (simplify
281 (rdiv @0 cst@1)
282 (if (optimize)
283 (if (flag_reciprocal_math
284 && !real_zerop (@1))
285 (with
286 { tree tem = const_binop (RDIV_EXPR, type, build_one_cst (type), @1); }
287 (if (tem)
288 (mult @0 { tem; } )))
289 (if (cst != COMPLEX_CST)
290 (with { tree inverse = exact_inverse (type, @1); }
291 (if (inverse)
292 (mult @0 { inverse; } ))))))))
293
294 (for mod (ceil_mod floor_mod round_mod trunc_mod)
295 /* 0 % X is always zero. */
296 (simplify
297 (mod integer_zerop@0 @1)
298 /* But not for 0 % 0 so that we can get the proper warnings and errors. */
299 (if (!integer_zerop (@1))
300 @0))
301 /* X % 1 is always zero. */
302 (simplify
303 (mod @0 integer_onep)
304 { build_zero_cst (type); })
305 /* X % -1 is zero. */
306 (simplify
307 (mod @0 integer_minus_onep@1)
308 (if (!TYPE_UNSIGNED (type))
309 { build_zero_cst (type); }))
310 /* X % X is zero. */
311 (simplify
312 (mod @0 @0)
313 /* But not for 0 % 0 so that we can get the proper warnings and errors. */
314 (if (!integer_zerop (@0))
315 { build_zero_cst (type); }))
316 /* (X % Y) % Y is just X % Y. */
317 (simplify
318 (mod (mod@2 @0 @1) @1)
319 @2)
320 /* From extract_muldiv_1: (X * C1) % C2 is zero if C1 is a multiple of C2. */
321 (simplify
322 (mod (mult @0 INTEGER_CST@1) INTEGER_CST@2)
323 (if (ANY_INTEGRAL_TYPE_P (type)
324 && TYPE_OVERFLOW_UNDEFINED (type)
325 && wi::multiple_of_p (@1, @2, TYPE_SIGN (type)))
326 { build_zero_cst (type); })))
327
328 /* X % -C is the same as X % C. */
329 (simplify
330 (trunc_mod @0 INTEGER_CST@1)
331 (if (TYPE_SIGN (type) == SIGNED
332 && !TREE_OVERFLOW (@1)
333 && wi::neg_p (@1)
334 && !TYPE_OVERFLOW_TRAPS (type)
335 /* Avoid this transformation if C is INT_MIN, i.e. C == -C. */
336 && !sign_bit_p (@1, @1))
337 (trunc_mod @0 (negate @1))))
338
339 /* X % -Y is the same as X % Y. */
340 (simplify
341 (trunc_mod @0 (convert? (negate @1)))
342 (if (INTEGRAL_TYPE_P (type)
343 && !TYPE_UNSIGNED (type)
344 && !TYPE_OVERFLOW_TRAPS (type)
345 && tree_nop_conversion_p (type, TREE_TYPE (@1))
346 /* Avoid this transformation if X might be INT_MIN or
347 Y might be -1, because we would then change valid
348 INT_MIN % -(-1) into invalid INT_MIN % -1. */
349 && (expr_not_equal_to (@0, TYPE_MIN_VALUE (type))
350 || expr_not_equal_to (@1, wi::minus_one (TYPE_PRECISION
351 (TREE_TYPE (@1))))))
352 (trunc_mod @0 (convert @1))))
353
354 /* X - (X / Y) * Y is the same as X % Y. */
355 (simplify
356 (minus (convert1? @0) (convert2? (mult:c (trunc_div @@0 @@1) @1)))
357 (if (INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
358 (convert (trunc_mod @0 @1))))
359
360 /* Optimize TRUNC_MOD_EXPR by a power of two into a BIT_AND_EXPR,
361 i.e. "X % C" into "X & (C - 1)", if X and C are positive.
362 Also optimize A % (C << N) where C is a power of 2,
363 to A & ((C << N) - 1). */
364 (match (power_of_two_cand @1)
365 INTEGER_CST@1)
366 (match (power_of_two_cand @1)
367 (lshift INTEGER_CST@1 @2))
368 (for mod (trunc_mod floor_mod)
369 (simplify
370 (mod @0 (convert?@3 (power_of_two_cand@1 @2)))
371 (if ((TYPE_UNSIGNED (type)
372 || tree_expr_nonnegative_p (@0))
373 && tree_nop_conversion_p (type, TREE_TYPE (@3))
374 && integer_pow2p (@2) && tree_int_cst_sgn (@2) > 0)
375 (bit_and @0 (convert (minus @1 { build_int_cst (TREE_TYPE (@1), 1); }))))))
376
377 /* Simplify (unsigned t * 2)/2 -> unsigned t & 0x7FFFFFFF. */
378 (simplify
379 (trunc_div (mult @0 integer_pow2p@1) @1)
380 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
381 (bit_and @0 { wide_int_to_tree
382 (type, wi::mask (TYPE_PRECISION (type) - wi::exact_log2 (@1),
383 false, TYPE_PRECISION (type))); })))
384
385 /* Simplify (unsigned t / 2) * 2 -> unsigned t & ~1. */
386 (simplify
387 (mult (trunc_div @0 integer_pow2p@1) @1)
388 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
389 (bit_and @0 (negate @1))))
390
391 /* Simplify (t * 2) / 2) -> t. */
392 (for div (trunc_div ceil_div floor_div round_div exact_div)
393 (simplify
394 (div (mult @0 @1) @1)
395 (if (ANY_INTEGRAL_TYPE_P (type)
396 && TYPE_OVERFLOW_UNDEFINED (type))
397 @0)))
398
399 (for op (negate abs)
400 /* Simplify cos(-x) and cos(|x|) -> cos(x). Similarly for cosh. */
401 (for coss (COS COSH)
402 (simplify
403 (coss (op @0))
404 (coss @0)))
405 /* Simplify pow(-x, y) and pow(|x|,y) -> pow(x,y) if y is an even integer. */
406 (for pows (POW)
407 (simplify
408 (pows (op @0) REAL_CST@1)
409 (with { HOST_WIDE_INT n; }
410 (if (real_isinteger (&TREE_REAL_CST (@1), &n) && (n & 1) == 0)
411 (pows @0 @1)))))
412 /* Likewise for powi. */
413 (for pows (POWI)
414 (simplify
415 (pows (op @0) INTEGER_CST@1)
416 (if (wi::bit_and (@1, 1) == 0)
417 (pows @0 @1))))
418 /* Strip negate and abs from both operands of hypot. */
419 (for hypots (HYPOT)
420 (simplify
421 (hypots (op @0) @1)
422 (hypots @0 @1))
423 (simplify
424 (hypots @0 (op @1))
425 (hypots @0 @1)))
426 /* copysign(-x, y) and copysign(abs(x), y) -> copysign(x, y). */
427 (for copysigns (COPYSIGN)
428 (simplify
429 (copysigns (op @0) @1)
430 (copysigns @0 @1))))
431
432 /* abs(x)*abs(x) -> x*x. Should be valid for all types. */
433 (simplify
434 (mult (abs@1 @0) @1)
435 (mult @0 @0))
436
437 /* cos(copysign(x, y)) -> cos(x). Similarly for cosh. */
438 (for coss (COS COSH)
439 copysigns (COPYSIGN)
440 (simplify
441 (coss (copysigns @0 @1))
442 (coss @0)))
443
444 /* pow(copysign(x, y), z) -> pow(x, z) if z is an even integer. */
445 (for pows (POW)
446 copysigns (COPYSIGN)
447 (simplify
448 (pows (copysigns @0 @2) REAL_CST@1)
449 (with { HOST_WIDE_INT n; }
450 (if (real_isinteger (&TREE_REAL_CST (@1), &n) && (n & 1) == 0)
451 (pows @0 @1)))))
452 /* Likewise for powi. */
453 (for pows (POWI)
454 copysigns (COPYSIGN)
455 (simplify
456 (pows (copysigns @0 @2) INTEGER_CST@1)
457 (if (wi::bit_and (@1, 1) == 0)
458 (pows @0 @1))))
459
460 (for hypots (HYPOT)
461 copysigns (COPYSIGN)
462 /* hypot(copysign(x, y), z) -> hypot(x, z). */
463 (simplify
464 (hypots (copysigns @0 @1) @2)
465 (hypots @0 @2))
466 /* hypot(x, copysign(y, z)) -> hypot(x, y). */
467 (simplify
468 (hypots @0 (copysigns @1 @2))
469 (hypots @0 @1)))
470
471 /* copysign(x, CST) -> [-]abs (x). */
472 (for copysigns (COPYSIGN)
473 (simplify
474 (copysigns @0 REAL_CST@1)
475 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
476 (negate (abs @0))
477 (abs @0))))
478
479 /* copysign(copysign(x, y), z) -> copysign(x, z). */
480 (for copysigns (COPYSIGN)
481 (simplify
482 (copysigns (copysigns @0 @1) @2)
483 (copysigns @0 @2)))
484
485 /* copysign(x,y)*copysign(x,y) -> x*x. */
486 (for copysigns (COPYSIGN)
487 (simplify
488 (mult (copysigns@2 @0 @1) @2)
489 (mult @0 @0)))
490
491 /* ccos(-x) -> ccos(x). Similarly for ccosh. */
492 (for ccoss (CCOS CCOSH)
493 (simplify
494 (ccoss (negate @0))
495 (ccoss @0)))
496
497 /* cabs(-x) and cos(conj(x)) -> cabs(x). */
498 (for ops (conj negate)
499 (for cabss (CABS)
500 (simplify
501 (cabss (ops @0))
502 (cabss @0))))
503
504 /* Fold (a * (1 << b)) into (a << b) */
505 (simplify
506 (mult:c @0 (convert? (lshift integer_onep@1 @2)))
507 (if (! FLOAT_TYPE_P (type)
508 && tree_nop_conversion_p (type, TREE_TYPE (@1)))
509 (lshift @0 @2)))
510
511 /* Fold (C1/X)*C2 into (C1*C2)/X. */
512 (simplify
513 (mult (rdiv@3 REAL_CST@0 @1) REAL_CST@2)
514 (if (flag_associative_math
515 && single_use (@3))
516 (with
517 { tree tem = const_binop (MULT_EXPR, type, @0, @2); }
518 (if (tem)
519 (rdiv { tem; } @1)))))
520
521 /* Convert C1/(X*C2) into (C1/C2)/X */
522 (simplify
523 (rdiv REAL_CST@0 (mult @1 REAL_CST@2))
524 (if (flag_reciprocal_math)
525 (with
526 { tree tem = const_binop (RDIV_EXPR, type, @0, @2); }
527 (if (tem)
528 (rdiv { tem; } @1)))))
529
530 /* Simplify ~X & X as zero. */
531 (simplify
532 (bit_and:c (convert? @0) (convert? (bit_not @0)))
533 { build_zero_cst (type); })
534
535 /* PR71636: Transform x & ((1U << b) - 1) -> x & ~(~0U << b); */
536 (simplify
537 (bit_and:c @0 (plus:s (lshift:s integer_onep @1) integer_minus_onep))
538 (if (TYPE_UNSIGNED (type))
539 (bit_and @0 (bit_not (lshift { build_all_ones_cst (type); } @1)))))
540
541 /* PR35691: Transform
542 (x == 0 & y == 0) -> (x | typeof(x)(y)) == 0.
543 (x != 0 | y != 0) -> (x | typeof(x)(y)) != 0. */
544 (for bitop (bit_and bit_ior)
545 cmp (eq ne)
546 (simplify
547 (bitop (cmp @0 integer_zerop@2) (cmp @1 integer_zerop))
548 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
549 && INTEGRAL_TYPE_P (TREE_TYPE (@1))
550 && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
551 (cmp (bit_ior @0 (convert @1)) @2))))
552
553 /* Fold (A & ~B) - (A & B) into (A ^ B) - B. */
554 (simplify
555 (minus (bit_and:cs @0 (bit_not @1)) (bit_and:cs @0 @1))
556 (minus (bit_xor @0 @1) @1))
557 (simplify
558 (minus (bit_and:s @0 INTEGER_CST@2) (bit_and:s @0 INTEGER_CST@1))
559 (if (wi::bit_not (@2) == @1)
560 (minus (bit_xor @0 @1) @1)))
561
562 /* Fold (A & B) - (A & ~B) into B - (A ^ B). */
563 (simplify
564 (minus (bit_and:cs @0 @1) (bit_and:cs @0 (bit_not @1)))
565 (minus @1 (bit_xor @0 @1)))
566
567 /* Simplify (X & ~Y) | (~X & Y) -> X ^ Y. */
568 (simplify
569 (bit_ior (bit_and:c @0 (bit_not @1)) (bit_and:c (bit_not @0) @1))
570 (bit_xor @0 @1))
571 (simplify
572 (bit_ior:c (bit_and @0 INTEGER_CST@2) (bit_and (bit_not @0) INTEGER_CST@1))
573 (if (wi::bit_not (@2) == @1)
574 (bit_xor @0 @1)))
575
576 /* PR53979: Transform ((a ^ b) | a) -> (a | b) */
577 (simplify
578 (bit_ior:c (bit_xor:c @0 @1) @0)
579 (bit_ior @0 @1))
580
581 /* Simplify (~X & Y) to X ^ Y if we know that (X & ~Y) is 0. */
582 #if GIMPLE
583 (simplify
584 (bit_and (bit_not SSA_NAME@0) INTEGER_CST@1)
585 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
586 && (get_nonzero_bits (@0) & wi::bit_not (@1)) == 0)
587 (bit_xor @0 @1)))
588 #endif
589
590 /* X % Y is smaller than Y. */
591 (for cmp (lt ge)
592 (simplify
593 (cmp (trunc_mod @0 @1) @1)
594 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
595 { constant_boolean_node (cmp == LT_EXPR, type); })))
596 (for cmp (gt le)
597 (simplify
598 (cmp @1 (trunc_mod @0 @1))
599 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
600 { constant_boolean_node (cmp == GT_EXPR, type); })))
601
602 /* x | ~0 -> ~0 */
603 (simplify
604 (bit_ior @0 integer_all_onesp@1)
605 @1)
606
607 /* x | 0 -> x */
608 (simplify
609 (bit_ior @0 integer_zerop)
610 @0)
611
612 /* x & 0 -> 0 */
613 (simplify
614 (bit_and @0 integer_zerop@1)
615 @1)
616
617 /* ~x | x -> -1 */
618 /* ~x ^ x -> -1 */
619 /* ~x + x -> -1 */
620 (for op (bit_ior bit_xor plus)
621 (simplify
622 (op:c (convert? @0) (convert? (bit_not @0)))
623 (convert { build_all_ones_cst (TREE_TYPE (@0)); })))
624
625 /* x ^ x -> 0 */
626 (simplify
627 (bit_xor @0 @0)
628 { build_zero_cst (type); })
629
630 /* Canonicalize X ^ ~0 to ~X. */
631 (simplify
632 (bit_xor @0 integer_all_onesp@1)
633 (bit_not @0))
634
635 /* x & ~0 -> x */
636 (simplify
637 (bit_and @0 integer_all_onesp)
638 (non_lvalue @0))
639
640 /* x & x -> x, x | x -> x */
641 (for bitop (bit_and bit_ior)
642 (simplify
643 (bitop @0 @0)
644 (non_lvalue @0)))
645
646 /* x & C -> x if we know that x & ~C == 0. */
647 #if GIMPLE
648 (simplify
649 (bit_and SSA_NAME@0 INTEGER_CST@1)
650 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
651 && (get_nonzero_bits (@0) & wi::bit_not (@1)) == 0)
652 @0))
653 #endif
654
655 /* x + (x & 1) -> (x + 1) & ~1 */
656 (simplify
657 (plus:c @0 (bit_and:s @0 integer_onep@1))
658 (bit_and (plus @0 @1) (bit_not @1)))
659
660 /* x & ~(x & y) -> x & ~y */
661 /* x | ~(x | y) -> x | ~y */
662 (for bitop (bit_and bit_ior)
663 (simplify
664 (bitop:c @0 (bit_not (bitop:cs @0 @1)))
665 (bitop @0 (bit_not @1))))
666
667 /* (x | y) & ~x -> y & ~x */
668 /* (x & y) | ~x -> y | ~x */
669 (for bitop (bit_and bit_ior)
670 rbitop (bit_ior bit_and)
671 (simplify
672 (bitop:c (rbitop:c @0 @1) (bit_not@2 @0))
673 (bitop @1 @2)))
674
675 /* (x & y) ^ (x | y) -> x ^ y */
676 (simplify
677 (bit_xor:c (bit_and @0 @1) (bit_ior @0 @1))
678 (bit_xor @0 @1))
679
680 /* (x ^ y) ^ (x | y) -> x & y */
681 (simplify
682 (bit_xor:c (bit_xor @0 @1) (bit_ior @0 @1))
683 (bit_and @0 @1))
684
685 /* (x & y) + (x ^ y) -> x | y */
686 /* (x & y) | (x ^ y) -> x | y */
687 /* (x & y) ^ (x ^ y) -> x | y */
688 (for op (plus bit_ior bit_xor)
689 (simplify
690 (op:c (bit_and @0 @1) (bit_xor @0 @1))
691 (bit_ior @0 @1)))
692
693 /* (x & y) + (x | y) -> x + y */
694 (simplify
695 (plus:c (bit_and @0 @1) (bit_ior @0 @1))
696 (plus @0 @1))
697
698 /* (x + y) - (x | y) -> x & y */
699 (simplify
700 (minus (plus @0 @1) (bit_ior @0 @1))
701 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
702 && !TYPE_SATURATING (type))
703 (bit_and @0 @1)))
704
705 /* (x + y) - (x & y) -> x | y */
706 (simplify
707 (minus (plus @0 @1) (bit_and @0 @1))
708 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
709 && !TYPE_SATURATING (type))
710 (bit_ior @0 @1)))
711
712 /* (x | y) - (x ^ y) -> x & y */
713 (simplify
714 (minus (bit_ior @0 @1) (bit_xor @0 @1))
715 (bit_and @0 @1))
716
717 /* (x | y) - (x & y) -> x ^ y */
718 (simplify
719 (minus (bit_ior @0 @1) (bit_and @0 @1))
720 (bit_xor @0 @1))
721
722 /* (x | y) & ~(x & y) -> x ^ y */
723 (simplify
724 (bit_and:c (bit_ior @0 @1) (bit_not (bit_and @0 @1)))
725 (bit_xor @0 @1))
726
727 /* (x | y) & (~x ^ y) -> x & y */
728 (simplify
729 (bit_and:c (bit_ior:c @0 @1) (bit_xor:c @1 (bit_not @0)))
730 (bit_and @0 @1))
731
732 /* ~x & ~y -> ~(x | y)
733 ~x | ~y -> ~(x & y) */
734 (for op (bit_and bit_ior)
735 rop (bit_ior bit_and)
736 (simplify
737 (op (convert1? (bit_not @0)) (convert2? (bit_not @1)))
738 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
739 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
740 (bit_not (rop (convert @0) (convert @1))))))
741
742 /* If we are XORing or adding two BIT_AND_EXPR's, both of which are and'ing
743 with a constant, and the two constants have no bits in common,
744 we should treat this as a BIT_IOR_EXPR since this may produce more
745 simplifications. */
746 (for op (bit_xor plus)
747 (simplify
748 (op (convert1? (bit_and@4 @0 INTEGER_CST@1))
749 (convert2? (bit_and@5 @2 INTEGER_CST@3)))
750 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
751 && tree_nop_conversion_p (type, TREE_TYPE (@2))
752 && wi::bit_and (@1, @3) == 0)
753 (bit_ior (convert @4) (convert @5)))))
754
755 /* (X | Y) ^ X -> Y & ~ X*/
756 (simplify
757 (bit_xor:c (convert1? (bit_ior:c @@0 @1)) (convert2? @0))
758 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
759 (convert (bit_and @1 (bit_not @0)))))
760
761 /* Convert ~X ^ ~Y to X ^ Y. */
762 (simplify
763 (bit_xor (convert1? (bit_not @0)) (convert2? (bit_not @1)))
764 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
765 && element_precision (type) <= element_precision (TREE_TYPE (@1)))
766 (bit_xor (convert @0) (convert @1))))
767
768 /* Convert ~X ^ C to X ^ ~C. */
769 (simplify
770 (bit_xor (convert? (bit_not @0)) INTEGER_CST@1)
771 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
772 (bit_xor (convert @0) (bit_not @1))))
773
774 /* Fold (X & Y) ^ Y and (X ^ Y) & Y as ~X & Y. */
775 (for opo (bit_and bit_xor)
776 opi (bit_xor bit_and)
777 (simplify
778 (opo:c (opi:c @0 @1) @1)
779 (bit_and (bit_not @0) @1)))
780
781 /* Given a bit-wise operation CODE applied to ARG0 and ARG1, see if both
782 operands are another bit-wise operation with a common input. If so,
783 distribute the bit operations to save an operation and possibly two if
784 constants are involved. For example, convert
785 (A | B) & (A | C) into A | (B & C)
786 Further simplification will occur if B and C are constants. */
787 (for op (bit_and bit_ior bit_xor)
788 rop (bit_ior bit_and bit_and)
789 (simplify
790 (op (convert? (rop:c @@0 @1)) (convert? (rop:c @0 @2)))
791 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
792 && tree_nop_conversion_p (type, TREE_TYPE (@2)))
793 (rop (convert @0) (op (convert @1) (convert @2))))))
794
795 /* Some simple reassociation for bit operations, also handled in reassoc. */
796 /* (X & Y) & Y -> X & Y
797 (X | Y) | Y -> X | Y */
798 (for op (bit_and bit_ior)
799 (simplify
800 (op:c (convert1?@2 (op:c @0 @@1)) (convert2? @1))
801 @2))
802 /* (X ^ Y) ^ Y -> X */
803 (simplify
804 (bit_xor:c (convert1? (bit_xor:c @0 @@1)) (convert2? @1))
805 (convert @0))
806 /* (X & Y) & (X & Z) -> (X & Y) & Z
807 (X | Y) | (X | Z) -> (X | Y) | Z */
808 (for op (bit_and bit_ior)
809 (simplify
810 (op:c (convert1?@3 (op:c@4 @0 @1)) (convert2?@5 (op:c@6 @0 @2)))
811 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
812 && tree_nop_conversion_p (type, TREE_TYPE (@2)))
813 (if (single_use (@5) && single_use (@6))
814 (op @3 (convert @2))
815 (if (single_use (@3) && single_use (@4))
816 (op (convert @1) @5))))))
817 /* (X ^ Y) ^ (X ^ Z) -> Y ^ Z */
818 (simplify
819 (bit_xor (convert1? (bit_xor:c @0 @1)) (convert2? (bit_xor:c @0 @2)))
820 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
821 && tree_nop_conversion_p (type, TREE_TYPE (@2)))
822 (bit_xor (convert @1) (convert @2))))
823
824 (simplify
825 (abs (abs@1 @0))
826 @1)
827 (simplify
828 (abs (negate @0))
829 (abs @0))
830 (simplify
831 (abs tree_expr_nonnegative_p@0)
832 @0)
833
834 /* A few cases of fold-const.c negate_expr_p predicate. */
835 (match negate_expr_p
836 INTEGER_CST
837 (if ((INTEGRAL_TYPE_P (type)
838 && TYPE_OVERFLOW_WRAPS (type))
839 || (!TYPE_OVERFLOW_SANITIZED (type)
840 && may_negate_without_overflow_p (t)))))
841 (match negate_expr_p
842 FIXED_CST)
843 (match negate_expr_p
844 (negate @0)
845 (if (!TYPE_OVERFLOW_SANITIZED (type))))
846 (match negate_expr_p
847 REAL_CST
848 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (t)))))
849 /* VECTOR_CST handling of non-wrapping types would recurse in unsupported
850 ways. */
851 (match negate_expr_p
852 VECTOR_CST
853 (if (FLOAT_TYPE_P (TREE_TYPE (type)) || TYPE_OVERFLOW_WRAPS (type))))
854
855 /* (-A) * (-B) -> A * B */
856 (simplify
857 (mult:c (convert1? (negate @0)) (convert2? negate_expr_p@1))
858 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
859 && tree_nop_conversion_p (type, TREE_TYPE (@1)))
860 (mult (convert @0) (convert (negate @1)))))
861
862 /* -(A + B) -> (-B) - A. */
863 (simplify
864 (negate (plus:c @0 negate_expr_p@1))
865 (if (!HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type))
866 && !HONOR_SIGNED_ZEROS (element_mode (type)))
867 (minus (negate @1) @0)))
868
869 /* A - B -> A + (-B) if B is easily negatable. */
870 (simplify
871 (minus @0 negate_expr_p@1)
872 (if (!FIXED_POINT_TYPE_P (type))
873 (plus @0 (negate @1))))
874
875 /* Try to fold (type) X op CST -> (type) (X op ((type-x) CST))
876 when profitable.
877 For bitwise binary operations apply operand conversions to the
878 binary operation result instead of to the operands. This allows
879 to combine successive conversions and bitwise binary operations.
880 We combine the above two cases by using a conditional convert. */
881 (for bitop (bit_and bit_ior bit_xor)
882 (simplify
883 (bitop (convert @0) (convert? @1))
884 (if (((TREE_CODE (@1) == INTEGER_CST
885 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
886 && int_fits_type_p (@1, TREE_TYPE (@0)))
887 || types_match (@0, @1))
888 /* ??? This transform conflicts with fold-const.c doing
889 Convert (T)(x & c) into (T)x & (T)c, if c is an integer
890 constants (if x has signed type, the sign bit cannot be set
891 in c). This folds extension into the BIT_AND_EXPR.
892 Restrict it to GIMPLE to avoid endless recursions. */
893 && (bitop != BIT_AND_EXPR || GIMPLE)
894 && (/* That's a good idea if the conversion widens the operand, thus
895 after hoisting the conversion the operation will be narrower. */
896 TYPE_PRECISION (TREE_TYPE (@0)) < TYPE_PRECISION (type)
897 /* It's also a good idea if the conversion is to a non-integer
898 mode. */
899 || GET_MODE_CLASS (TYPE_MODE (type)) != MODE_INT
900 /* Or if the precision of TO is not the same as the precision
901 of its mode. */
902 || TYPE_PRECISION (type) != GET_MODE_PRECISION (TYPE_MODE (type))))
903 (convert (bitop @0 (convert @1))))))
904
905 (for bitop (bit_and bit_ior)
906 rbitop (bit_ior bit_and)
907 /* (x | y) & x -> x */
908 /* (x & y) | x -> x */
909 (simplify
910 (bitop:c (rbitop:c @0 @1) @0)
911 @0)
912 /* (~x | y) & x -> x & y */
913 /* (~x & y) | x -> x | y */
914 (simplify
915 (bitop:c (rbitop:c (bit_not @0) @1) @0)
916 (bitop @0 @1)))
917
918 /* (x | CST1) & CST2 -> (x & CST2) | (CST1 & CST2) */
919 (simplify
920 (bit_and (bit_ior @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
921 (bit_ior (bit_and @0 @2) (bit_and @1 @2)))
922
923 /* Combine successive equal operations with constants. */
924 (for bitop (bit_and bit_ior bit_xor)
925 (simplify
926 (bitop (bitop @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
927 (bitop @0 (bitop @1 @2))))
928
929 /* Try simple folding for X op !X, and X op X with the help
930 of the truth_valued_p and logical_inverted_value predicates. */
931 (match truth_valued_p
932 @0
933 (if (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1)))
934 (for op (tcc_comparison truth_and truth_andif truth_or truth_orif truth_xor)
935 (match truth_valued_p
936 (op @0 @1)))
937 (match truth_valued_p
938 (truth_not @0))
939
940 (match (logical_inverted_value @0)
941 (truth_not @0))
942 (match (logical_inverted_value @0)
943 (bit_not truth_valued_p@0))
944 (match (logical_inverted_value @0)
945 (eq @0 integer_zerop))
946 (match (logical_inverted_value @0)
947 (ne truth_valued_p@0 integer_truep))
948 (match (logical_inverted_value @0)
949 (bit_xor truth_valued_p@0 integer_truep))
950
951 /* X & !X -> 0. */
952 (simplify
953 (bit_and:c @0 (logical_inverted_value @0))
954 { build_zero_cst (type); })
955 /* X | !X and X ^ !X -> 1, , if X is truth-valued. */
956 (for op (bit_ior bit_xor)
957 (simplify
958 (op:c truth_valued_p@0 (logical_inverted_value @0))
959 { constant_boolean_node (true, type); }))
960 /* X ==/!= !X is false/true. */
961 (for op (eq ne)
962 (simplify
963 (op:c truth_valued_p@0 (logical_inverted_value @0))
964 { constant_boolean_node (op == NE_EXPR ? true : false, type); }))
965
966 /* ~~x -> x */
967 (simplify
968 (bit_not (bit_not @0))
969 @0)
970
971 /* Convert ~ (-A) to A - 1. */
972 (simplify
973 (bit_not (convert? (negate @0)))
974 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
975 || !TYPE_UNSIGNED (TREE_TYPE (@0)))
976 (convert (minus @0 { build_each_one_cst (TREE_TYPE (@0)); }))))
977
978 /* Convert ~ (A - 1) or ~ (A + -1) to -A. */
979 (simplify
980 (bit_not (convert? (minus @0 integer_each_onep)))
981 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
982 || !TYPE_UNSIGNED (TREE_TYPE (@0)))
983 (convert (negate @0))))
984 (simplify
985 (bit_not (convert? (plus @0 integer_all_onesp)))
986 (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
987 || !TYPE_UNSIGNED (TREE_TYPE (@0)))
988 (convert (negate @0))))
989
990 /* Part of convert ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify. */
991 (simplify
992 (bit_not (convert? (bit_xor @0 INTEGER_CST@1)))
993 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
994 (convert (bit_xor @0 (bit_not @1)))))
995 (simplify
996 (bit_not (convert? (bit_xor:c (bit_not @0) @1)))
997 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
998 (convert (bit_xor @0 @1))))
999
1000 /* (x & ~m) | (y & m) -> ((x ^ y) & m) ^ x */
1001 (simplify
1002 (bit_ior:c (bit_and:cs @0 (bit_not @2)) (bit_and:cs @1 @2))
1003 (bit_xor (bit_and (bit_xor @0 @1) @2) @0))
1004
1005 /* Fold A - (A & B) into ~B & A. */
1006 (simplify
1007 (minus (convert1? @0) (convert2?:s (bit_and:cs @@0 @1)))
1008 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
1009 && tree_nop_conversion_p (type, TREE_TYPE (@1)))
1010 (convert (bit_and (bit_not @1) @0))))
1011
1012 /* For integral types with undefined overflow and C != 0 fold
1013 x * C EQ/NE y * C into x EQ/NE y. */
1014 (for cmp (eq ne)
1015 (simplify
1016 (cmp (mult:c @0 @1) (mult:c @2 @1))
1017 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
1018 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1019 && tree_expr_nonzero_p (@1))
1020 (cmp @0 @2))))
1021
1022 /* For integral types with undefined overflow and C != 0 fold
1023 x * C RELOP y * C into:
1024
1025 x RELOP y for nonnegative C
1026 y RELOP x for negative C */
1027 (for cmp (lt gt le ge)
1028 (simplify
1029 (cmp (mult:c @0 @1) (mult:c @2 @1))
1030 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
1031 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1032 (if (tree_expr_nonnegative_p (@1) && tree_expr_nonzero_p (@1))
1033 (cmp @0 @2)
1034 (if (TREE_CODE (@1) == INTEGER_CST
1035 && wi::neg_p (@1, TYPE_SIGN (TREE_TYPE (@1))))
1036 (cmp @2 @0))))))
1037
1038 /* X / 4 < Y / 4 iff X < Y when the division is known to be exact. */
1039 (for cmp (simple_comparison)
1040 (simplify
1041 (cmp (exact_div @0 INTEGER_CST@2) (exact_div @1 @2))
1042 (if (wi::gt_p(@2, 0, TYPE_SIGN (TREE_TYPE (@2))))
1043 (cmp @0 @1))))
1044
1045 /* X + Z < Y + Z is the same as X < Y when there is no overflow. */
1046 (for op (lt le ge gt)
1047 (simplify
1048 (op (plus:c @0 @2) (plus:c @1 @2))
1049 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1050 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1051 (op @0 @1))))
1052 /* For equality and subtraction, this is also true with wrapping overflow. */
1053 (for op (eq ne minus)
1054 (simplify
1055 (op (plus:c @0 @2) (plus:c @1 @2))
1056 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1057 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1058 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
1059 (op @0 @1))))
1060
1061 /* X - Z < Y - Z is the same as X < Y when there is no overflow. */
1062 (for op (lt le ge gt)
1063 (simplify
1064 (op (minus @0 @2) (minus @1 @2))
1065 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1066 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1067 (op @0 @1))))
1068 /* For equality and subtraction, this is also true with wrapping overflow. */
1069 (for op (eq ne minus)
1070 (simplify
1071 (op (minus @0 @2) (minus @1 @2))
1072 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1073 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1074 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
1075 (op @0 @1))))
1076
1077 /* Z - X < Z - Y is the same as Y < X when there is no overflow. */
1078 (for op (lt le ge gt)
1079 (simplify
1080 (op (minus @2 @0) (minus @2 @1))
1081 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1082 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1083 (op @1 @0))))
1084 /* For equality and subtraction, this is also true with wrapping overflow. */
1085 (for op (eq ne minus)
1086 (simplify
1087 (op (minus @2 @0) (minus @2 @1))
1088 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1089 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1090 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
1091 (op @1 @0))))
1092
1093 /* X == C - X can never be true if C is odd. */
1094 (for cmp (eq ne)
1095 (simplify
1096 (cmp:c (convert? @0) (convert1? (minus INTEGER_CST@1 (convert2? @0))))
1097 (if (TREE_INT_CST_LOW (@1) & 1)
1098 { constant_boolean_node (cmp == NE_EXPR, type); })))
1099
1100 /* Arguments on which one can call get_nonzero_bits to get the bits
1101 possibly set. */
1102 (match with_possible_nonzero_bits
1103 INTEGER_CST@0)
1104 (match with_possible_nonzero_bits
1105 SSA_NAME@0
1106 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))))
1107 /* Slightly extended version, do not make it recursive to keep it cheap. */
1108 (match (with_possible_nonzero_bits2 @0)
1109 with_possible_nonzero_bits@0)
1110 (match (with_possible_nonzero_bits2 @0)
1111 (bit_and:c with_possible_nonzero_bits@0 @2))
1112
1113 /* Same for bits that are known to be set, but we do not have
1114 an equivalent to get_nonzero_bits yet. */
1115 (match (with_certain_nonzero_bits2 @0)
1116 INTEGER_CST@0)
1117 (match (with_certain_nonzero_bits2 @0)
1118 (bit_ior @1 INTEGER_CST@0))
1119
1120 /* X == C (or X & Z == Y | C) is impossible if ~nonzero(X) & C != 0. */
1121 (for cmp (eq ne)
1122 (simplify
1123 (cmp:c (with_possible_nonzero_bits2 @0) (with_certain_nonzero_bits2 @1))
1124 (if ((~get_nonzero_bits (@0) & @1) != 0)
1125 { constant_boolean_node (cmp == NE_EXPR, type); })))
1126
1127 /* ((X inner_op C0) outer_op C1)
1128 With X being a tree where value_range has reasoned certain bits to always be
1129 zero throughout its computed value range,
1130 inner_op = {|,^}, outer_op = {|,^} and inner_op != outer_op
1131 where zero_mask has 1's for all bits that are sure to be 0 in
1132 and 0's otherwise.
1133 if (inner_op == '^') C0 &= ~C1;
1134 if ((C0 & ~zero_mask) == 0) then emit (X outer_op (C0 outer_op C1)
1135 if ((C1 & ~zero_mask) == 0) then emit (X inner_op (C0 outer_op C1)
1136 */
1137 (for inner_op (bit_ior bit_xor)
1138 outer_op (bit_xor bit_ior)
1139 (simplify
1140 (outer_op
1141 (inner_op:s @2 INTEGER_CST@0) INTEGER_CST@1)
1142 (with
1143 {
1144 bool fail = false;
1145 wide_int zero_mask_not;
1146 wide_int C0;
1147 wide_int cst_emit;
1148
1149 if (TREE_CODE (@2) == SSA_NAME)
1150 zero_mask_not = get_nonzero_bits (@2);
1151 else
1152 fail = true;
1153
1154 if (inner_op == BIT_XOR_EXPR)
1155 {
1156 C0 = wi::bit_and_not (@0, @1);
1157 cst_emit = wi::bit_or (C0, @1);
1158 }
1159 else
1160 {
1161 C0 = @0;
1162 cst_emit = wi::bit_xor (@0, @1);
1163 }
1164 }
1165 (if (!fail && wi::bit_and (C0, zero_mask_not) == 0)
1166 (outer_op @2 { wide_int_to_tree (type, cst_emit); })
1167 (if (!fail && wi::bit_and (@1, zero_mask_not) == 0)
1168 (inner_op @2 { wide_int_to_tree (type, cst_emit); }))))))
1169
1170 /* Associate (p +p off1) +p off2 as (p +p (off1 + off2)). */
1171 (simplify
1172 (pointer_plus (pointer_plus:s @0 @1) @3)
1173 (pointer_plus @0 (plus @1 @3)))
1174
1175 /* Pattern match
1176 tem1 = (long) ptr1;
1177 tem2 = (long) ptr2;
1178 tem3 = tem2 - tem1;
1179 tem4 = (unsigned long) tem3;
1180 tem5 = ptr1 + tem4;
1181 and produce
1182 tem5 = ptr2; */
1183 (simplify
1184 (pointer_plus @0 (convert?@2 (minus@3 (convert @1) (convert @0))))
1185 /* Conditionally look through a sign-changing conversion. */
1186 (if (TYPE_PRECISION (TREE_TYPE (@2)) == TYPE_PRECISION (TREE_TYPE (@3))
1187 && ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@1)))
1188 || (GENERIC && type == TREE_TYPE (@1))))
1189 @1))
1190
1191 /* Pattern match
1192 tem = (sizetype) ptr;
1193 tem = tem & algn;
1194 tem = -tem;
1195 ... = ptr p+ tem;
1196 and produce the simpler and easier to analyze with respect to alignment
1197 ... = ptr & ~algn; */
1198 (simplify
1199 (pointer_plus @0 (negate (bit_and (convert @0) INTEGER_CST@1)))
1200 (with { tree algn = wide_int_to_tree (TREE_TYPE (@0), wi::bit_not (@1)); }
1201 (bit_and @0 { algn; })))
1202
1203 /* Try folding difference of addresses. */
1204 (simplify
1205 (minus (convert ADDR_EXPR@0) (convert @1))
1206 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1207 (with { HOST_WIDE_INT diff; }
1208 (if (ptr_difference_const (@0, @1, &diff))
1209 { build_int_cst_type (type, diff); }))))
1210 (simplify
1211 (minus (convert @0) (convert ADDR_EXPR@1))
1212 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1213 (with { HOST_WIDE_INT diff; }
1214 (if (ptr_difference_const (@0, @1, &diff))
1215 { build_int_cst_type (type, diff); }))))
1216
1217 /* If arg0 is derived from the address of an object or function, we may
1218 be able to fold this expression using the object or function's
1219 alignment. */
1220 (simplify
1221 (bit_and (convert? @0) INTEGER_CST@1)
1222 (if (POINTER_TYPE_P (TREE_TYPE (@0))
1223 && tree_nop_conversion_p (type, TREE_TYPE (@0)))
1224 (with
1225 {
1226 unsigned int align;
1227 unsigned HOST_WIDE_INT bitpos;
1228 get_pointer_alignment_1 (@0, &align, &bitpos);
1229 }
1230 (if (wi::ltu_p (@1, align / BITS_PER_UNIT))
1231 { wide_int_to_tree (type, wi::bit_and (@1, bitpos / BITS_PER_UNIT)); }))))
1232
1233
1234 /* We can't reassociate at all for saturating types. */
1235 (if (!TYPE_SATURATING (type))
1236
1237 /* Contract negates. */
1238 /* A + (-B) -> A - B */
1239 (simplify
1240 (plus:c @0 (convert? (negate @1)))
1241 /* Apply STRIP_NOPS on the negate. */
1242 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1243 && !TYPE_OVERFLOW_SANITIZED (type))
1244 (with
1245 {
1246 tree t1 = type;
1247 if (INTEGRAL_TYPE_P (type)
1248 && TYPE_OVERFLOW_WRAPS (type) != TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
1249 t1 = TYPE_OVERFLOW_WRAPS (type) ? type : TREE_TYPE (@1);
1250 }
1251 (convert (minus (convert:t1 @0) (convert:t1 @1))))))
1252 /* A - (-B) -> A + B */
1253 (simplify
1254 (minus @0 (convert? (negate @1)))
1255 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1256 && !TYPE_OVERFLOW_SANITIZED (type))
1257 (with
1258 {
1259 tree t1 = type;
1260 if (INTEGRAL_TYPE_P (type)
1261 && TYPE_OVERFLOW_WRAPS (type) != TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
1262 t1 = TYPE_OVERFLOW_WRAPS (type) ? type : TREE_TYPE (@1);
1263 }
1264 (convert (plus (convert:t1 @0) (convert:t1 @1))))))
1265 /* -(-A) -> A */
1266 (simplify
1267 (negate (convert? (negate @1)))
1268 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1269 && !TYPE_OVERFLOW_SANITIZED (type))
1270 (convert @1)))
1271
1272 /* We can't reassociate floating-point unless -fassociative-math
1273 or fixed-point plus or minus because of saturation to +-Inf. */
1274 (if ((!FLOAT_TYPE_P (type) || flag_associative_math)
1275 && !FIXED_POINT_TYPE_P (type))
1276
1277 /* Match patterns that allow contracting a plus-minus pair
1278 irrespective of overflow issues. */
1279 /* (A +- B) - A -> +- B */
1280 /* (A +- B) -+ B -> A */
1281 /* A - (A +- B) -> -+ B */
1282 /* A +- (B -+ A) -> +- B */
1283 (simplify
1284 (minus (plus:c @0 @1) @0)
1285 @1)
1286 (simplify
1287 (minus (minus @0 @1) @0)
1288 (negate @1))
1289 (simplify
1290 (plus:c (minus @0 @1) @1)
1291 @0)
1292 (simplify
1293 (minus @0 (plus:c @0 @1))
1294 (negate @1))
1295 (simplify
1296 (minus @0 (minus @0 @1))
1297 @1)
1298
1299 /* (A +- CST1) +- CST2 -> A + CST3 */
1300 (for outer_op (plus minus)
1301 (for inner_op (plus minus)
1302 (simplify
1303 (outer_op (inner_op @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
1304 /* If the constant operation overflows we cannot do the transform
1305 as we would introduce undefined overflow, for example
1306 with (a - 1) + INT_MIN. */
1307 (with { tree cst = const_binop (outer_op == inner_op
1308 ? PLUS_EXPR : MINUS_EXPR, type, @1, @2); }
1309 (if (cst && !TREE_OVERFLOW (cst))
1310 (inner_op @0 { cst; } ))))))
1311
1312 /* (CST1 - A) +- CST2 -> CST3 - A */
1313 (for outer_op (plus minus)
1314 (simplify
1315 (outer_op (minus CONSTANT_CLASS_P@1 @0) CONSTANT_CLASS_P@2)
1316 (with { tree cst = const_binop (outer_op, type, @1, @2); }
1317 (if (cst && !TREE_OVERFLOW (cst))
1318 (minus { cst; } @0)))))
1319
1320 /* CST1 - (CST2 - A) -> CST3 + A */
1321 (simplify
1322 (minus CONSTANT_CLASS_P@1 (minus CONSTANT_CLASS_P@2 @0))
1323 (with { tree cst = const_binop (MINUS_EXPR, type, @1, @2); }
1324 (if (cst && !TREE_OVERFLOW (cst))
1325 (plus { cst; } @0))))
1326
1327 /* ~A + A -> -1 */
1328 (simplify
1329 (plus:c (bit_not @0) @0)
1330 (if (!TYPE_OVERFLOW_TRAPS (type))
1331 { build_all_ones_cst (type); }))
1332
1333 /* ~A + 1 -> -A */
1334 (simplify
1335 (plus (convert? (bit_not @0)) integer_each_onep)
1336 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1337 (negate (convert @0))))
1338
1339 /* -A - 1 -> ~A */
1340 (simplify
1341 (minus (convert? (negate @0)) integer_each_onep)
1342 (if (!TYPE_OVERFLOW_TRAPS (type)
1343 && tree_nop_conversion_p (type, TREE_TYPE (@0)))
1344 (bit_not (convert @0))))
1345
1346 /* -1 - A -> ~A */
1347 (simplify
1348 (minus integer_all_onesp @0)
1349 (bit_not @0))
1350
1351 /* (T)(P + A) - (T)P -> (T) A */
1352 (for add (plus pointer_plus)
1353 (simplify
1354 (minus (convert (add @@0 @1))
1355 (convert @0))
1356 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
1357 /* For integer types, if A has a smaller type
1358 than T the result depends on the possible
1359 overflow in P + A.
1360 E.g. T=size_t, A=(unsigned)429497295, P>0.
1361 However, if an overflow in P + A would cause
1362 undefined behavior, we can assume that there
1363 is no overflow. */
1364 || (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1365 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1366 /* For pointer types, if the conversion of A to the
1367 final type requires a sign- or zero-extension,
1368 then we have to punt - it is not defined which
1369 one is correct. */
1370 || (POINTER_TYPE_P (TREE_TYPE (@0))
1371 && TREE_CODE (@1) == INTEGER_CST
1372 && tree_int_cst_sign_bit (@1) == 0))
1373 (convert @1))))
1374
1375 /* (T)P - (T)(P + A) -> -(T) A */
1376 (for add (plus pointer_plus)
1377 (simplify
1378 (minus (convert @0)
1379 (convert (add @@0 @1)))
1380 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
1381 /* For integer types, if A has a smaller type
1382 than T the result depends on the possible
1383 overflow in P + A.
1384 E.g. T=size_t, A=(unsigned)429497295, P>0.
1385 However, if an overflow in P + A would cause
1386 undefined behavior, we can assume that there
1387 is no overflow. */
1388 || (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1389 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1390 /* For pointer types, if the conversion of A to the
1391 final type requires a sign- or zero-extension,
1392 then we have to punt - it is not defined which
1393 one is correct. */
1394 || (POINTER_TYPE_P (TREE_TYPE (@0))
1395 && TREE_CODE (@1) == INTEGER_CST
1396 && tree_int_cst_sign_bit (@1) == 0))
1397 (negate (convert @1)))))
1398
1399 /* (T)(P + A) - (T)(P + B) -> (T)A - (T)B */
1400 (for add (plus pointer_plus)
1401 (simplify
1402 (minus (convert (add @@0 @1))
1403 (convert (add @0 @2)))
1404 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
1405 /* For integer types, if A has a smaller type
1406 than T the result depends on the possible
1407 overflow in P + A.
1408 E.g. T=size_t, A=(unsigned)429497295, P>0.
1409 However, if an overflow in P + A would cause
1410 undefined behavior, we can assume that there
1411 is no overflow. */
1412 || (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1413 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1414 /* For pointer types, if the conversion of A to the
1415 final type requires a sign- or zero-extension,
1416 then we have to punt - it is not defined which
1417 one is correct. */
1418 || (POINTER_TYPE_P (TREE_TYPE (@0))
1419 && TREE_CODE (@1) == INTEGER_CST
1420 && tree_int_cst_sign_bit (@1) == 0
1421 && TREE_CODE (@2) == INTEGER_CST
1422 && tree_int_cst_sign_bit (@2) == 0))
1423 (minus (convert @1) (convert @2)))))))
1424
1425
1426 /* Simplifications of MIN_EXPR, MAX_EXPR, fmin() and fmax(). */
1427
1428 (for minmax (min max FMIN FMAX)
1429 (simplify
1430 (minmax @0 @0)
1431 @0))
1432 /* min(max(x,y),y) -> y. */
1433 (simplify
1434 (min:c (max:c @0 @1) @1)
1435 @1)
1436 /* max(min(x,y),y) -> y. */
1437 (simplify
1438 (max:c (min:c @0 @1) @1)
1439 @1)
1440 /* max(a,-a) -> abs(a). */
1441 (simplify
1442 (max:c @0 (negate @0))
1443 (if (TREE_CODE (type) != COMPLEX_TYPE
1444 && (! ANY_INTEGRAL_TYPE_P (type)
1445 || TYPE_OVERFLOW_UNDEFINED (type)))
1446 (abs @0)))
1447 /* min(a,-a) -> -abs(a). */
1448 (simplify
1449 (min:c @0 (negate @0))
1450 (if (TREE_CODE (type) != COMPLEX_TYPE
1451 && (! ANY_INTEGRAL_TYPE_P (type)
1452 || TYPE_OVERFLOW_UNDEFINED (type)))
1453 (negate (abs @0))))
1454 (simplify
1455 (min @0 @1)
1456 (switch
1457 (if (INTEGRAL_TYPE_P (type)
1458 && TYPE_MIN_VALUE (type)
1459 && operand_equal_p (@1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
1460 @1)
1461 (if (INTEGRAL_TYPE_P (type)
1462 && TYPE_MAX_VALUE (type)
1463 && operand_equal_p (@1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
1464 @0)))
1465 (simplify
1466 (max @0 @1)
1467 (switch
1468 (if (INTEGRAL_TYPE_P (type)
1469 && TYPE_MAX_VALUE (type)
1470 && operand_equal_p (@1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
1471 @1)
1472 (if (INTEGRAL_TYPE_P (type)
1473 && TYPE_MIN_VALUE (type)
1474 && operand_equal_p (@1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
1475 @0)))
1476
1477 /* max (a, a + CST) -> a + CST where CST is positive. */
1478 /* max (a, a + CST) -> a where CST is negative. */
1479 (simplify
1480 (max:c @0 (plus@2 @0 INTEGER_CST@1))
1481 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1482 (if (tree_int_cst_sgn (@1) > 0)
1483 @2
1484 @0)))
1485
1486 /* min (a, a + CST) -> a where CST is positive. */
1487 /* min (a, a + CST) -> a + CST where CST is negative. */
1488 (simplify
1489 (min:c @0 (plus@2 @0 INTEGER_CST@1))
1490 (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1491 (if (tree_int_cst_sgn (@1) > 0)
1492 @0
1493 @2)))
1494
1495 /* (convert (minmax ((convert (x) c)))) -> minmax (x c) if x is promoted
1496 and the outer convert demotes the expression back to x's type. */
1497 (for minmax (min max)
1498 (simplify
1499 (convert (minmax@0 (convert @1) INTEGER_CST@2))
1500 (if (INTEGRAL_TYPE_P (type)
1501 && types_match (@1, type) && int_fits_type_p (@2, type)
1502 && TYPE_SIGN (TREE_TYPE (@0)) == TYPE_SIGN (type)
1503 && TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type))
1504 (minmax @1 (convert @2)))))
1505
1506 (for minmax (FMIN FMAX)
1507 /* If either argument is NaN, return the other one. Avoid the
1508 transformation if we get (and honor) a signalling NaN. */
1509 (simplify
1510 (minmax:c @0 REAL_CST@1)
1511 (if (real_isnan (TREE_REAL_CST_PTR (@1))
1512 && (!HONOR_SNANS (@1) || !TREE_REAL_CST (@1).signalling))
1513 @0)))
1514 /* Convert fmin/fmax to MIN_EXPR/MAX_EXPR. C99 requires these
1515 functions to return the numeric arg if the other one is NaN.
1516 MIN and MAX don't honor that, so only transform if -ffinite-math-only
1517 is set. C99 doesn't require -0.0 to be handled, so we don't have to
1518 worry about it either. */
1519 (if (flag_finite_math_only)
1520 (simplify
1521 (FMIN @0 @1)
1522 (min @0 @1))
1523 (simplify
1524 (FMAX @0 @1)
1525 (max @0 @1)))
1526 /* min (-A, -B) -> -max (A, B) */
1527 (for minmax (min max FMIN FMAX)
1528 maxmin (max min FMAX FMIN)
1529 (simplify
1530 (minmax (negate:s@2 @0) (negate:s@3 @1))
1531 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
1532 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1533 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
1534 (negate (maxmin @0 @1)))))
1535 /* MIN (~X, ~Y) -> ~MAX (X, Y)
1536 MAX (~X, ~Y) -> ~MIN (X, Y) */
1537 (for minmax (min max)
1538 maxmin (max min)
1539 (simplify
1540 (minmax (bit_not:s@2 @0) (bit_not:s@3 @1))
1541 (bit_not (maxmin @0 @1))))
1542
1543 /* MIN (X, Y) == X -> X <= Y */
1544 (for minmax (min min max max)
1545 cmp (eq ne eq ne )
1546 out (le gt ge lt )
1547 (simplify
1548 (cmp:c (minmax:c @0 @1) @0)
1549 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0)))
1550 (out @0 @1))))
1551 /* MIN (X, 5) == 0 -> X == 0
1552 MIN (X, 5) == 7 -> false */
1553 (for cmp (eq ne)
1554 (simplify
1555 (cmp (min @0 INTEGER_CST@1) INTEGER_CST@2)
1556 (if (wi::lt_p (@1, @2, TYPE_SIGN (TREE_TYPE (@0))))
1557 { constant_boolean_node (cmp == NE_EXPR, type); }
1558 (if (wi::gt_p (@1, @2, TYPE_SIGN (TREE_TYPE (@0))))
1559 (cmp @0 @2)))))
1560 (for cmp (eq ne)
1561 (simplify
1562 (cmp (max @0 INTEGER_CST@1) INTEGER_CST@2)
1563 (if (wi::gt_p (@1, @2, TYPE_SIGN (TREE_TYPE (@0))))
1564 { constant_boolean_node (cmp == NE_EXPR, type); }
1565 (if (wi::lt_p (@1, @2, TYPE_SIGN (TREE_TYPE (@0))))
1566 (cmp @0 @2)))))
1567 /* MIN (X, C1) < C2 -> X < C2 || C1 < C2 */
1568 (for minmax (min min max max min min max max )
1569 cmp (lt le gt ge gt ge lt le )
1570 comb (bit_ior bit_ior bit_ior bit_ior bit_and bit_and bit_and bit_and)
1571 (simplify
1572 (cmp (minmax @0 INTEGER_CST@1) INTEGER_CST@2)
1573 (comb (cmp @0 @2) (cmp @1 @2))))
1574
1575 /* Simplifications of shift and rotates. */
1576
1577 (for rotate (lrotate rrotate)
1578 (simplify
1579 (rotate integer_all_onesp@0 @1)
1580 @0))
1581
1582 /* Optimize -1 >> x for arithmetic right shifts. */
1583 (simplify
1584 (rshift integer_all_onesp@0 @1)
1585 (if (!TYPE_UNSIGNED (type)
1586 && tree_expr_nonnegative_p (@1))
1587 @0))
1588
1589 /* Optimize (x >> c) << c into x & (-1<<c). */
1590 (simplify
1591 (lshift (rshift @0 INTEGER_CST@1) @1)
1592 (if (wi::ltu_p (@1, element_precision (type)))
1593 (bit_and @0 (lshift { build_minus_one_cst (type); } @1))))
1594
1595 /* Optimize (x << c) >> c into x & ((unsigned)-1 >> c) for unsigned
1596 types. */
1597 (simplify
1598 (rshift (lshift @0 INTEGER_CST@1) @1)
1599 (if (TYPE_UNSIGNED (type)
1600 && (wi::ltu_p (@1, element_precision (type))))
1601 (bit_and @0 (rshift { build_minus_one_cst (type); } @1))))
1602
1603 (for shiftrotate (lrotate rrotate lshift rshift)
1604 (simplify
1605 (shiftrotate @0 integer_zerop)
1606 (non_lvalue @0))
1607 (simplify
1608 (shiftrotate integer_zerop@0 @1)
1609 @0)
1610 /* Prefer vector1 << scalar to vector1 << vector2
1611 if vector2 is uniform. */
1612 (for vec (VECTOR_CST CONSTRUCTOR)
1613 (simplify
1614 (shiftrotate @0 vec@1)
1615 (with { tree tem = uniform_vector_p (@1); }
1616 (if (tem)
1617 (shiftrotate @0 { tem; }))))))
1618
1619 /* Simplify X << Y where Y's low width bits are 0 to X, as only valid
1620 Y is 0. Similarly for X >> Y. */
1621 #if GIMPLE
1622 (for shift (lshift rshift)
1623 (simplify
1624 (shift @0 SSA_NAME@1)
1625 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1)))
1626 (with {
1627 int width = ceil_log2 (element_precision (TREE_TYPE (@0)));
1628 int prec = TYPE_PRECISION (TREE_TYPE (@1));
1629 }
1630 (if ((get_nonzero_bits (@1) & wi::mask (width, false, prec)) == 0)
1631 @0)))))
1632 #endif
1633
1634 /* Rewrite an LROTATE_EXPR by a constant into an
1635 RROTATE_EXPR by a new constant. */
1636 (simplify
1637 (lrotate @0 INTEGER_CST@1)
1638 (rrotate @0 { const_binop (MINUS_EXPR, TREE_TYPE (@1),
1639 build_int_cst (TREE_TYPE (@1),
1640 element_precision (type)), @1); }))
1641
1642 /* Turn (a OP c1) OP c2 into a OP (c1+c2). */
1643 (for op (lrotate rrotate rshift lshift)
1644 (simplify
1645 (op (op @0 INTEGER_CST@1) INTEGER_CST@2)
1646 (with { unsigned int prec = element_precision (type); }
1647 (if (wi::ge_p (@1, 0, TYPE_SIGN (TREE_TYPE (@1)))
1648 && wi::lt_p (@1, prec, TYPE_SIGN (TREE_TYPE (@1)))
1649 && wi::ge_p (@2, 0, TYPE_SIGN (TREE_TYPE (@2)))
1650 && wi::lt_p (@2, prec, TYPE_SIGN (TREE_TYPE (@2))))
1651 (with { unsigned int low = wi::add (@1, @2).to_uhwi (); }
1652 /* Deal with a OP (c1 + c2) being undefined but (a OP c1) OP c2
1653 being well defined. */
1654 (if (low >= prec)
1655 (if (op == LROTATE_EXPR || op == RROTATE_EXPR)
1656 (op @0 { build_int_cst (TREE_TYPE (@1), low % prec); })
1657 (if (TYPE_UNSIGNED (type) || op == LSHIFT_EXPR)
1658 { build_zero_cst (type); }
1659 (op @0 { build_int_cst (TREE_TYPE (@1), prec - 1); })))
1660 (op @0 { build_int_cst (TREE_TYPE (@1), low); })))))))
1661
1662
1663 /* ((1 << A) & 1) != 0 -> A == 0
1664 ((1 << A) & 1) == 0 -> A != 0 */
1665 (for cmp (ne eq)
1666 icmp (eq ne)
1667 (simplify
1668 (cmp (bit_and (lshift integer_onep @0) integer_onep) integer_zerop)
1669 (icmp @0 { build_zero_cst (TREE_TYPE (@0)); })))
1670
1671 /* (CST1 << A) == CST2 -> A == ctz (CST2) - ctz (CST1)
1672 (CST1 << A) != CST2 -> A != ctz (CST2) - ctz (CST1)
1673 if CST2 != 0. */
1674 (for cmp (ne eq)
1675 (simplify
1676 (cmp (lshift INTEGER_CST@0 @1) INTEGER_CST@2)
1677 (with { int cand = wi::ctz (@2) - wi::ctz (@0); }
1678 (if (cand < 0
1679 || (!integer_zerop (@2)
1680 && wi::ne_p (wi::lshift (@0, cand), @2)))
1681 { constant_boolean_node (cmp == NE_EXPR, type); }
1682 (if (!integer_zerop (@2)
1683 && wi::eq_p (wi::lshift (@0, cand), @2))
1684 (cmp @1 { build_int_cst (TREE_TYPE (@1), cand); }))))))
1685
1686 /* Fold (X << C1) & C2 into (X << C1) & (C2 | ((1 << C1) - 1))
1687 (X >> C1) & C2 into (X >> C1) & (C2 | ~((type) -1 >> C1))
1688 if the new mask might be further optimized. */
1689 (for shift (lshift rshift)
1690 (simplify
1691 (bit_and (convert?:s@4 (shift:s@5 (convert1?@3 @0) INTEGER_CST@1))
1692 INTEGER_CST@2)
1693 (if (tree_nop_conversion_p (TREE_TYPE (@4), TREE_TYPE (@5))
1694 && TYPE_PRECISION (type) <= HOST_BITS_PER_WIDE_INT
1695 && tree_fits_uhwi_p (@1)
1696 && tree_to_uhwi (@1) > 0
1697 && tree_to_uhwi (@1) < TYPE_PRECISION (type))
1698 (with
1699 {
1700 unsigned int shiftc = tree_to_uhwi (@1);
1701 unsigned HOST_WIDE_INT mask = TREE_INT_CST_LOW (@2);
1702 unsigned HOST_WIDE_INT newmask, zerobits = 0;
1703 tree shift_type = TREE_TYPE (@3);
1704 unsigned int prec;
1705
1706 if (shift == LSHIFT_EXPR)
1707 zerobits = ((HOST_WIDE_INT_1U << shiftc) - 1);
1708 else if (shift == RSHIFT_EXPR
1709 && (TYPE_PRECISION (shift_type)
1710 == GET_MODE_PRECISION (TYPE_MODE (shift_type))))
1711 {
1712 prec = TYPE_PRECISION (TREE_TYPE (@3));
1713 tree arg00 = @0;
1714 /* See if more bits can be proven as zero because of
1715 zero extension. */
1716 if (@3 != @0
1717 && TYPE_UNSIGNED (TREE_TYPE (@0)))
1718 {
1719 tree inner_type = TREE_TYPE (@0);
1720 if ((TYPE_PRECISION (inner_type)
1721 == GET_MODE_PRECISION (TYPE_MODE (inner_type)))
1722 && TYPE_PRECISION (inner_type) < prec)
1723 {
1724 prec = TYPE_PRECISION (inner_type);
1725 /* See if we can shorten the right shift. */
1726 if (shiftc < prec)
1727 shift_type = inner_type;
1728 /* Otherwise X >> C1 is all zeros, so we'll optimize
1729 it into (X, 0) later on by making sure zerobits
1730 is all ones. */
1731 }
1732 }
1733 zerobits = HOST_WIDE_INT_M1U;
1734 if (shiftc < prec)
1735 {
1736 zerobits >>= HOST_BITS_PER_WIDE_INT - shiftc;
1737 zerobits <<= prec - shiftc;
1738 }
1739 /* For arithmetic shift if sign bit could be set, zerobits
1740 can contain actually sign bits, so no transformation is
1741 possible, unless MASK masks them all away. In that
1742 case the shift needs to be converted into logical shift. */
1743 if (!TYPE_UNSIGNED (TREE_TYPE (@3))
1744 && prec == TYPE_PRECISION (TREE_TYPE (@3)))
1745 {
1746 if ((mask & zerobits) == 0)
1747 shift_type = unsigned_type_for (TREE_TYPE (@3));
1748 else
1749 zerobits = 0;
1750 }
1751 }
1752 }
1753 /* ((X << 16) & 0xff00) is (X, 0). */
1754 (if ((mask & zerobits) == mask)
1755 { build_int_cst (type, 0); }
1756 (with { newmask = mask | zerobits; }
1757 (if (newmask != mask && (newmask & (newmask + 1)) == 0)
1758 (with
1759 {
1760 /* Only do the transformation if NEWMASK is some integer
1761 mode's mask. */
1762 for (prec = BITS_PER_UNIT;
1763 prec < HOST_BITS_PER_WIDE_INT; prec <<= 1)
1764 if (newmask == (HOST_WIDE_INT_1U << prec) - 1)
1765 break;
1766 }
1767 (if (prec < HOST_BITS_PER_WIDE_INT
1768 || newmask == HOST_WIDE_INT_M1U)
1769 (with
1770 { tree newmaskt = build_int_cst_type (TREE_TYPE (@2), newmask); }
1771 (if (!tree_int_cst_equal (newmaskt, @2))
1772 (if (shift_type != TREE_TYPE (@3))
1773 (bit_and (convert (shift:shift_type (convert @3) @1)) { newmaskt; })
1774 (bit_and @4 { newmaskt; })))))))))))))
1775
1776 /* Fold (X {&,^,|} C2) << C1 into (X << C1) {&,^,|} (C2 << C1)
1777 (X {&,^,|} C2) >> C1 into (X >> C1) & (C2 >> C1). */
1778 (for shift (lshift rshift)
1779 (for bit_op (bit_and bit_xor bit_ior)
1780 (simplify
1781 (shift (convert?:s (bit_op:s @0 INTEGER_CST@2)) INTEGER_CST@1)
1782 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1783 (with { tree mask = int_const_binop (shift, fold_convert (type, @2), @1); }
1784 (bit_op (shift (convert @0) @1) { mask; }))))))
1785
1786 /* ~(~X >> Y) -> X >> Y (for arithmetic shift). */
1787 (simplify
1788 (bit_not (convert1?:s (rshift:s (convert2?@0 (bit_not @1)) @2)))
1789 (if (!TYPE_UNSIGNED (TREE_TYPE (@0))
1790 && (element_precision (TREE_TYPE (@0))
1791 <= element_precision (TREE_TYPE (@1))
1792 || !TYPE_UNSIGNED (TREE_TYPE (@1))))
1793 (with
1794 { tree shift_type = TREE_TYPE (@0); }
1795 (convert (rshift (convert:shift_type @1) @2)))))
1796
1797 /* ~(~X >>r Y) -> X >>r Y
1798 ~(~X <<r Y) -> X <<r Y */
1799 (for rotate (lrotate rrotate)
1800 (simplify
1801 (bit_not (convert1?:s (rotate:s (convert2?@0 (bit_not @1)) @2)))
1802 (if ((element_precision (TREE_TYPE (@0))
1803 <= element_precision (TREE_TYPE (@1))
1804 || !TYPE_UNSIGNED (TREE_TYPE (@1)))
1805 && (element_precision (type) <= element_precision (TREE_TYPE (@0))
1806 || !TYPE_UNSIGNED (TREE_TYPE (@0))))
1807 (with
1808 { tree rotate_type = TREE_TYPE (@0); }
1809 (convert (rotate (convert:rotate_type @1) @2))))))
1810
1811 /* Simplifications of conversions. */
1812
1813 /* Basic strip-useless-type-conversions / strip_nops. */
1814 (for cvt (convert view_convert float fix_trunc)
1815 (simplify
1816 (cvt @0)
1817 (if ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@0)))
1818 || (GENERIC && type == TREE_TYPE (@0)))
1819 @0)))
1820
1821 /* Contract view-conversions. */
1822 (simplify
1823 (view_convert (view_convert @0))
1824 (view_convert @0))
1825
1826 /* For integral conversions with the same precision or pointer
1827 conversions use a NOP_EXPR instead. */
1828 (simplify
1829 (view_convert @0)
1830 (if ((INTEGRAL_TYPE_P (type) || POINTER_TYPE_P (type))
1831 && (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
1832 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (@0)))
1833 (convert @0)))
1834
1835 /* Strip inner integral conversions that do not change precision or size, or
1836 zero-extend while keeping the same size (for bool-to-char). */
1837 (simplify
1838 (view_convert (convert@0 @1))
1839 (if ((INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
1840 && (INTEGRAL_TYPE_P (TREE_TYPE (@1)) || POINTER_TYPE_P (TREE_TYPE (@1)))
1841 && TYPE_SIZE (TREE_TYPE (@0)) == TYPE_SIZE (TREE_TYPE (@1))
1842 && (TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1))
1843 || (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (TREE_TYPE (@1))
1844 && TYPE_UNSIGNED (TREE_TYPE (@1)))))
1845 (view_convert @1)))
1846
1847 /* Re-association barriers around constants and other re-association
1848 barriers can be removed. */
1849 (simplify
1850 (paren CONSTANT_CLASS_P@0)
1851 @0)
1852 (simplify
1853 (paren (paren@1 @0))
1854 @1)
1855
1856 /* Handle cases of two conversions in a row. */
1857 (for ocvt (convert float fix_trunc)
1858 (for icvt (convert float)
1859 (simplify
1860 (ocvt (icvt@1 @0))
1861 (with
1862 {
1863 tree inside_type = TREE_TYPE (@0);
1864 tree inter_type = TREE_TYPE (@1);
1865 int inside_int = INTEGRAL_TYPE_P (inside_type);
1866 int inside_ptr = POINTER_TYPE_P (inside_type);
1867 int inside_float = FLOAT_TYPE_P (inside_type);
1868 int inside_vec = VECTOR_TYPE_P (inside_type);
1869 unsigned int inside_prec = TYPE_PRECISION (inside_type);
1870 int inside_unsignedp = TYPE_UNSIGNED (inside_type);
1871 int inter_int = INTEGRAL_TYPE_P (inter_type);
1872 int inter_ptr = POINTER_TYPE_P (inter_type);
1873 int inter_float = FLOAT_TYPE_P (inter_type);
1874 int inter_vec = VECTOR_TYPE_P (inter_type);
1875 unsigned int inter_prec = TYPE_PRECISION (inter_type);
1876 int inter_unsignedp = TYPE_UNSIGNED (inter_type);
1877 int final_int = INTEGRAL_TYPE_P (type);
1878 int final_ptr = POINTER_TYPE_P (type);
1879 int final_float = FLOAT_TYPE_P (type);
1880 int final_vec = VECTOR_TYPE_P (type);
1881 unsigned int final_prec = TYPE_PRECISION (type);
1882 int final_unsignedp = TYPE_UNSIGNED (type);
1883 }
1884 (switch
1885 /* In addition to the cases of two conversions in a row
1886 handled below, if we are converting something to its own
1887 type via an object of identical or wider precision, neither
1888 conversion is needed. */
1889 (if (((GIMPLE && useless_type_conversion_p (type, inside_type))
1890 || (GENERIC
1891 && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (inside_type)))
1892 && (((inter_int || inter_ptr) && final_int)
1893 || (inter_float && final_float))
1894 && inter_prec >= final_prec)
1895 (ocvt @0))
1896
1897 /* Likewise, if the intermediate and initial types are either both
1898 float or both integer, we don't need the middle conversion if the
1899 former is wider than the latter and doesn't change the signedness
1900 (for integers). Avoid this if the final type is a pointer since
1901 then we sometimes need the middle conversion. */
1902 (if (((inter_int && inside_int) || (inter_float && inside_float))
1903 && (final_int || final_float)
1904 && inter_prec >= inside_prec
1905 && (inter_float || inter_unsignedp == inside_unsignedp))
1906 (ocvt @0))
1907
1908 /* If we have a sign-extension of a zero-extended value, we can
1909 replace that by a single zero-extension. Likewise if the
1910 final conversion does not change precision we can drop the
1911 intermediate conversion. */
1912 (if (inside_int && inter_int && final_int
1913 && ((inside_prec < inter_prec && inter_prec < final_prec
1914 && inside_unsignedp && !inter_unsignedp)
1915 || final_prec == inter_prec))
1916 (ocvt @0))
1917
1918 /* Two conversions in a row are not needed unless:
1919 - some conversion is floating-point (overstrict for now), or
1920 - some conversion is a vector (overstrict for now), or
1921 - the intermediate type is narrower than both initial and
1922 final, or
1923 - the intermediate type and innermost type differ in signedness,
1924 and the outermost type is wider than the intermediate, or
1925 - the initial type is a pointer type and the precisions of the
1926 intermediate and final types differ, or
1927 - the final type is a pointer type and the precisions of the
1928 initial and intermediate types differ. */
1929 (if (! inside_float && ! inter_float && ! final_float
1930 && ! inside_vec && ! inter_vec && ! final_vec
1931 && (inter_prec >= inside_prec || inter_prec >= final_prec)
1932 && ! (inside_int && inter_int
1933 && inter_unsignedp != inside_unsignedp
1934 && inter_prec < final_prec)
1935 && ((inter_unsignedp && inter_prec > inside_prec)
1936 == (final_unsignedp && final_prec > inter_prec))
1937 && ! (inside_ptr && inter_prec != final_prec)
1938 && ! (final_ptr && inside_prec != inter_prec))
1939 (ocvt @0))
1940
1941 /* A truncation to an unsigned type (a zero-extension) should be
1942 canonicalized as bitwise and of a mask. */
1943 (if (GIMPLE /* PR70366: doing this in GENERIC breaks -Wconversion. */
1944 && final_int && inter_int && inside_int
1945 && final_prec == inside_prec
1946 && final_prec > inter_prec
1947 && inter_unsignedp)
1948 (convert (bit_and @0 { wide_int_to_tree
1949 (inside_type,
1950 wi::mask (inter_prec, false,
1951 TYPE_PRECISION (inside_type))); })))
1952
1953 /* If we are converting an integer to a floating-point that can
1954 represent it exactly and back to an integer, we can skip the
1955 floating-point conversion. */
1956 (if (GIMPLE /* PR66211 */
1957 && inside_int && inter_float && final_int &&
1958 (unsigned) significand_size (TYPE_MODE (inter_type))
1959 >= inside_prec - !inside_unsignedp)
1960 (convert @0)))))))
1961
1962 /* If we have a narrowing conversion to an integral type that is fed by a
1963 BIT_AND_EXPR, we might be able to remove the BIT_AND_EXPR if it merely
1964 masks off bits outside the final type (and nothing else). */
1965 (simplify
1966 (convert (bit_and @0 INTEGER_CST@1))
1967 (if (INTEGRAL_TYPE_P (type)
1968 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
1969 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))
1970 && operand_equal_p (@1, build_low_bits_mask (TREE_TYPE (@1),
1971 TYPE_PRECISION (type)), 0))
1972 (convert @0)))
1973
1974
1975 /* (X /[ex] A) * A -> X. */
1976 (simplify
1977 (mult (convert1? (exact_div @0 @@1)) (convert2? @1))
1978 (convert @0))
1979
1980 /* Canonicalization of binary operations. */
1981
1982 /* Convert X + -C into X - C. */
1983 (simplify
1984 (plus @0 REAL_CST@1)
1985 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
1986 (with { tree tem = const_unop (NEGATE_EXPR, type, @1); }
1987 (if (!TREE_OVERFLOW (tem) || !flag_trapping_math)
1988 (minus @0 { tem; })))))
1989
1990 /* Convert x+x into x*2. */
1991 (simplify
1992 (plus @0 @0)
1993 (if (SCALAR_FLOAT_TYPE_P (type))
1994 (mult @0 { build_real (type, dconst2); })
1995 (if (INTEGRAL_TYPE_P (type))
1996 (mult @0 { build_int_cst (type, 2); }))))
1997
1998 (simplify
1999 (minus integer_zerop @1)
2000 (negate @1))
2001
2002 /* (ARG0 - ARG1) is the same as (-ARG1 + ARG0). So check whether
2003 ARG0 is zero and X + ARG0 reduces to X, since that would mean
2004 (-ARG1 + ARG0) reduces to -ARG1. */
2005 (simplify
2006 (minus real_zerop@0 @1)
2007 (if (fold_real_zero_addition_p (type, @0, 0))
2008 (negate @1)))
2009
2010 /* Transform x * -1 into -x. */
2011 (simplify
2012 (mult @0 integer_minus_onep)
2013 (negate @0))
2014
2015 /* True if we can easily extract the real and imaginary parts of a complex
2016 number. */
2017 (match compositional_complex
2018 (convert? (complex @0 @1)))
2019
2020 /* COMPLEX_EXPR and REALPART/IMAGPART_EXPR cancellations. */
2021 (simplify
2022 (complex (realpart @0) (imagpart @0))
2023 @0)
2024 (simplify
2025 (realpart (complex @0 @1))
2026 @0)
2027 (simplify
2028 (imagpart (complex @0 @1))
2029 @1)
2030
2031 /* Sometimes we only care about half of a complex expression. */
2032 (simplify
2033 (realpart (convert?:s (conj:s @0)))
2034 (convert (realpart @0)))
2035 (simplify
2036 (imagpart (convert?:s (conj:s @0)))
2037 (convert (negate (imagpart @0))))
2038 (for part (realpart imagpart)
2039 (for op (plus minus)
2040 (simplify
2041 (part (convert?:s@2 (op:s @0 @1)))
2042 (convert (op (part @0) (part @1))))))
2043 (simplify
2044 (realpart (convert?:s (CEXPI:s @0)))
2045 (convert (COS @0)))
2046 (simplify
2047 (imagpart (convert?:s (CEXPI:s @0)))
2048 (convert (SIN @0)))
2049
2050 /* conj(conj(x)) -> x */
2051 (simplify
2052 (conj (convert? (conj @0)))
2053 (if (tree_nop_conversion_p (TREE_TYPE (@0), type))
2054 (convert @0)))
2055
2056 /* conj({x,y}) -> {x,-y} */
2057 (simplify
2058 (conj (convert?:s (complex:s @0 @1)))
2059 (with { tree itype = TREE_TYPE (type); }
2060 (complex (convert:itype @0) (negate (convert:itype @1)))))
2061
2062 /* BSWAP simplifications, transforms checked by gcc.dg/builtin-bswap-8.c. */
2063 (for bswap (BUILT_IN_BSWAP16 BUILT_IN_BSWAP32 BUILT_IN_BSWAP64)
2064 (simplify
2065 (bswap (bswap @0))
2066 @0)
2067 (simplify
2068 (bswap (bit_not (bswap @0)))
2069 (bit_not @0))
2070 (for bitop (bit_xor bit_ior bit_and)
2071 (simplify
2072 (bswap (bitop:c (bswap @0) @1))
2073 (bitop @0 (bswap @1)))))
2074
2075
2076 /* Combine COND_EXPRs and VEC_COND_EXPRs. */
2077
2078 /* Simplify constant conditions.
2079 Only optimize constant conditions when the selected branch
2080 has the same type as the COND_EXPR. This avoids optimizing
2081 away "c ? x : throw", where the throw has a void type.
2082 Note that we cannot throw away the fold-const.c variant nor
2083 this one as we depend on doing this transform before possibly
2084 A ? B : B -> B triggers and the fold-const.c one can optimize
2085 0 ? A : B to B even if A has side-effects. Something
2086 genmatch cannot handle. */
2087 (simplify
2088 (cond INTEGER_CST@0 @1 @2)
2089 (if (integer_zerop (@0))
2090 (if (!VOID_TYPE_P (TREE_TYPE (@2)) || VOID_TYPE_P (type))
2091 @2)
2092 (if (!VOID_TYPE_P (TREE_TYPE (@1)) || VOID_TYPE_P (type))
2093 @1)))
2094 (simplify
2095 (vec_cond VECTOR_CST@0 @1 @2)
2096 (if (integer_all_onesp (@0))
2097 @1
2098 (if (integer_zerop (@0))
2099 @2)))
2100
2101 /* Simplification moved from fold_cond_expr_with_comparison. It may also
2102 be extended. */
2103 /* This pattern implements two kinds simplification:
2104
2105 Case 1)
2106 (cond (cmp (convert1? x) c1) (convert2? x) c2) -> (minmax (x c)) if:
2107 1) Conversions are type widening from smaller type.
2108 2) Const c1 equals to c2 after canonicalizing comparison.
2109 3) Comparison has tree code LT, LE, GT or GE.
2110 This specific pattern is needed when (cmp (convert x) c) may not
2111 be simplified by comparison patterns because of multiple uses of
2112 x. It also makes sense here because simplifying across multiple
2113 referred var is always benefitial for complicated cases.
2114
2115 Case 2)
2116 (cond (eq (convert1? x) c1) (convert2? x) c2) -> (cond (eq x c1) c1 c2). */
2117 (for cmp (lt le gt ge eq)
2118 (simplify
2119 (cond (cmp (convert1? @1) INTEGER_CST@3) (convert2? @1) INTEGER_CST@2)
2120 (with
2121 {
2122 tree from_type = TREE_TYPE (@1);
2123 tree c1_type = TREE_TYPE (@3), c2_type = TREE_TYPE (@2);
2124 enum tree_code code = ERROR_MARK;
2125
2126 if (INTEGRAL_TYPE_P (from_type)
2127 && int_fits_type_p (@2, from_type)
2128 && (types_match (c1_type, from_type)
2129 || (TYPE_PRECISION (c1_type) > TYPE_PRECISION (from_type)
2130 && (TYPE_UNSIGNED (from_type)
2131 || TYPE_SIGN (c1_type) == TYPE_SIGN (from_type))))
2132 && (types_match (c2_type, from_type)
2133 || (TYPE_PRECISION (c2_type) > TYPE_PRECISION (from_type)
2134 && (TYPE_UNSIGNED (from_type)
2135 || TYPE_SIGN (c2_type) == TYPE_SIGN (from_type)))))
2136 {
2137 if (cmp != EQ_EXPR)
2138 {
2139 if (wi::to_widest (@3) == (wi::to_widest (@2) - 1))
2140 {
2141 /* X <= Y - 1 equals to X < Y. */
2142 if (cmp == LE_EXPR)
2143 code = LT_EXPR;
2144 /* X > Y - 1 equals to X >= Y. */
2145 if (cmp == GT_EXPR)
2146 code = GE_EXPR;
2147 }
2148 if (wi::to_widest (@3) == (wi::to_widest (@2) + 1))
2149 {
2150 /* X < Y + 1 equals to X <= Y. */
2151 if (cmp == LT_EXPR)
2152 code = LE_EXPR;
2153 /* X >= Y + 1 equals to X > Y. */
2154 if (cmp == GE_EXPR)
2155 code = GT_EXPR;
2156 }
2157 if (code != ERROR_MARK
2158 || wi::to_widest (@2) == wi::to_widest (@3))
2159 {
2160 if (cmp == LT_EXPR || cmp == LE_EXPR)
2161 code = MIN_EXPR;
2162 if (cmp == GT_EXPR || cmp == GE_EXPR)
2163 code = MAX_EXPR;
2164 }
2165 }
2166 /* Can do A == C1 ? A : C2 -> A == C1 ? C1 : C2? */
2167 else if (int_fits_type_p (@3, from_type))
2168 code = EQ_EXPR;
2169 }
2170 }
2171 (if (code == MAX_EXPR)
2172 (convert (max @1 (convert @2)))
2173 (if (code == MIN_EXPR)
2174 (convert (min @1 (convert @2)))
2175 (if (code == EQ_EXPR)
2176 (convert (cond (eq @1 (convert @3))
2177 (convert:from_type @3) (convert:from_type @2)))))))))
2178
2179 /* (cond (cmp (convert? x) c1) (op x c2) c3) -> (op (minmax x c1) c2) if:
2180
2181 1) OP is PLUS or MINUS.
2182 2) CMP is LT, LE, GT or GE.
2183 3) C3 == (C1 op C2), and computation doesn't have undefined behavior.
2184
2185 This pattern also handles special cases like:
2186
2187 A) Operand x is a unsigned to signed type conversion and c1 is
2188 integer zero. In this case,
2189 (signed type)x < 0 <=> x > MAX_VAL(signed type)
2190 (signed type)x >= 0 <=> x <= MAX_VAL(signed type)
2191 B) Const c1 may not equal to (C3 op' C2). In this case we also
2192 check equality for (c1+1) and (c1-1) by adjusting comparison
2193 code.
2194
2195 TODO: Though signed type is handled by this pattern, it cannot be
2196 simplified at the moment because C standard requires additional
2197 type promotion. In order to match&simplify it here, the IR needs
2198 to be cleaned up by other optimizers, i.e, VRP. */
2199 (for op (plus minus)
2200 (for cmp (lt le gt ge)
2201 (simplify
2202 (cond (cmp (convert? @X) INTEGER_CST@1) (op @X INTEGER_CST@2) INTEGER_CST@3)
2203 (with { tree from_type = TREE_TYPE (@X), to_type = TREE_TYPE (@1); }
2204 (if (types_match (from_type, to_type)
2205 /* Check if it is special case A). */
2206 || (TYPE_UNSIGNED (from_type)
2207 && !TYPE_UNSIGNED (to_type)
2208 && TYPE_PRECISION (from_type) == TYPE_PRECISION (to_type)
2209 && integer_zerop (@1)
2210 && (cmp == LT_EXPR || cmp == GE_EXPR)))
2211 (with
2212 {
2213 bool overflow = false;
2214 enum tree_code code, cmp_code = cmp;
2215 wide_int real_c1, c1 = @1, c2 = @2, c3 = @3;
2216 signop sgn = TYPE_SIGN (from_type);
2217
2218 /* Handle special case A), given x of unsigned type:
2219 ((signed type)x < 0) <=> (x > MAX_VAL(signed type))
2220 ((signed type)x >= 0) <=> (x <= MAX_VAL(signed type)) */
2221 if (!types_match (from_type, to_type))
2222 {
2223 if (cmp_code == LT_EXPR)
2224 cmp_code = GT_EXPR;
2225 if (cmp_code == GE_EXPR)
2226 cmp_code = LE_EXPR;
2227 c1 = wi::max_value (to_type);
2228 }
2229 /* To simplify this pattern, we require c3 = (c1 op c2). Here we
2230 compute (c3 op' c2) and check if it equals to c1 with op' being
2231 the inverted operator of op. Make sure overflow doesn't happen
2232 if it is undefined. */
2233 if (op == PLUS_EXPR)
2234 real_c1 = wi::sub (c3, c2, sgn, &overflow);
2235 else
2236 real_c1 = wi::add (c3, c2, sgn, &overflow);
2237
2238 code = cmp_code;
2239 if (!overflow || !TYPE_OVERFLOW_UNDEFINED (from_type))
2240 {
2241 /* Check if c1 equals to real_c1. Boundary condition is handled
2242 by adjusting comparison operation if necessary. */
2243 if (!wi::cmp (wi::sub (real_c1, 1, sgn, &overflow), c1, sgn)
2244 && !overflow)
2245 {
2246 /* X <= Y - 1 equals to X < Y. */
2247 if (cmp_code == LE_EXPR)
2248 code = LT_EXPR;
2249 /* X > Y - 1 equals to X >= Y. */
2250 if (cmp_code == GT_EXPR)
2251 code = GE_EXPR;
2252 }
2253 if (!wi::cmp (wi::add (real_c1, 1, sgn, &overflow), c1, sgn)
2254 && !overflow)
2255 {
2256 /* X < Y + 1 equals to X <= Y. */
2257 if (cmp_code == LT_EXPR)
2258 code = LE_EXPR;
2259 /* X >= Y + 1 equals to X > Y. */
2260 if (cmp_code == GE_EXPR)
2261 code = GT_EXPR;
2262 }
2263 if (code != cmp_code || !wi::cmp (real_c1, c1, sgn))
2264 {
2265 if (cmp_code == LT_EXPR || cmp_code == LE_EXPR)
2266 code = MIN_EXPR;
2267 if (cmp_code == GT_EXPR || cmp_code == GE_EXPR)
2268 code = MAX_EXPR;
2269 }
2270 }
2271 }
2272 (if (code == MAX_EXPR)
2273 (op (max @X { wide_int_to_tree (from_type, real_c1); })
2274 { wide_int_to_tree (from_type, c2); })
2275 (if (code == MIN_EXPR)
2276 (op (min @X { wide_int_to_tree (from_type, real_c1); })
2277 { wide_int_to_tree (from_type, c2); })))))))))
2278
2279 (for cnd (cond vec_cond)
2280 /* A ? B : (A ? X : C) -> A ? B : C. */
2281 (simplify
2282 (cnd @0 (cnd @0 @1 @2) @3)
2283 (cnd @0 @1 @3))
2284 (simplify
2285 (cnd @0 @1 (cnd @0 @2 @3))
2286 (cnd @0 @1 @3))
2287 /* A ? B : (!A ? C : X) -> A ? B : C. */
2288 /* ??? This matches embedded conditions open-coded because genmatch
2289 would generate matching code for conditions in separate stmts only.
2290 The following is still important to merge then and else arm cases
2291 from if-conversion. */
2292 (simplify
2293 (cnd @0 @1 (cnd @2 @3 @4))
2294 (if (COMPARISON_CLASS_P (@0)
2295 && COMPARISON_CLASS_P (@2)
2296 && invert_tree_comparison
2297 (TREE_CODE (@0), HONOR_NANS (TREE_OPERAND (@0, 0))) == TREE_CODE (@2)
2298 && operand_equal_p (TREE_OPERAND (@0, 0), TREE_OPERAND (@2, 0), 0)
2299 && operand_equal_p (TREE_OPERAND (@0, 1), TREE_OPERAND (@2, 1), 0))
2300 (cnd @0 @1 @3)))
2301 (simplify
2302 (cnd @0 (cnd @1 @2 @3) @4)
2303 (if (COMPARISON_CLASS_P (@0)
2304 && COMPARISON_CLASS_P (@1)
2305 && invert_tree_comparison
2306 (TREE_CODE (@0), HONOR_NANS (TREE_OPERAND (@0, 0))) == TREE_CODE (@1)
2307 && operand_equal_p (TREE_OPERAND (@0, 0), TREE_OPERAND (@1, 0), 0)
2308 && operand_equal_p (TREE_OPERAND (@0, 1), TREE_OPERAND (@1, 1), 0))
2309 (cnd @0 @3 @4)))
2310
2311 /* A ? B : B -> B. */
2312 (simplify
2313 (cnd @0 @1 @1)
2314 @1)
2315
2316 /* !A ? B : C -> A ? C : B. */
2317 (simplify
2318 (cnd (logical_inverted_value truth_valued_p@0) @1 @2)
2319 (cnd @0 @2 @1)))
2320
2321 /* A + (B vcmp C ? 1 : 0) -> A - (B vcmp C ? -1 : 0), since vector comparisons
2322 return all -1 or all 0 results. */
2323 /* ??? We could instead convert all instances of the vec_cond to negate,
2324 but that isn't necessarily a win on its own. */
2325 (simplify
2326 (plus:c @3 (view_convert? (vec_cond:s @0 integer_each_onep@1 integer_zerop@2)))
2327 (if (VECTOR_TYPE_P (type)
2328 && TYPE_VECTOR_SUBPARTS (type) == TYPE_VECTOR_SUBPARTS (TREE_TYPE (@1))
2329 && (TYPE_MODE (TREE_TYPE (type))
2330 == TYPE_MODE (TREE_TYPE (TREE_TYPE (@1)))))
2331 (minus @3 (view_convert (vec_cond @0 (negate @1) @2)))))
2332
2333 /* ... likewise A - (B vcmp C ? 1 : 0) -> A + (B vcmp C ? -1 : 0). */
2334 (simplify
2335 (minus @3 (view_convert? (vec_cond:s @0 integer_each_onep@1 integer_zerop@2)))
2336 (if (VECTOR_TYPE_P (type)
2337 && TYPE_VECTOR_SUBPARTS (type) == TYPE_VECTOR_SUBPARTS (TREE_TYPE (@1))
2338 && (TYPE_MODE (TREE_TYPE (type))
2339 == TYPE_MODE (TREE_TYPE (TREE_TYPE (@1)))))
2340 (plus @3 (view_convert (vec_cond @0 (negate @1) @2)))))
2341
2342
2343 /* Simplifications of comparisons. */
2344
2345 /* See if we can reduce the magnitude of a constant involved in a
2346 comparison by changing the comparison code. This is a canonicalization
2347 formerly done by maybe_canonicalize_comparison_1. */
2348 (for cmp (le gt)
2349 acmp (lt ge)
2350 (simplify
2351 (cmp @0 INTEGER_CST@1)
2352 (if (tree_int_cst_sgn (@1) == -1)
2353 (acmp @0 { wide_int_to_tree (TREE_TYPE (@1), wi::add (@1, 1)); }))))
2354 (for cmp (ge lt)
2355 acmp (gt le)
2356 (simplify
2357 (cmp @0 INTEGER_CST@1)
2358 (if (tree_int_cst_sgn (@1) == 1)
2359 (acmp @0 { wide_int_to_tree (TREE_TYPE (@1), wi::sub (@1, 1)); }))))
2360
2361
2362 /* We can simplify a logical negation of a comparison to the
2363 inverted comparison. As we cannot compute an expression
2364 operator using invert_tree_comparison we have to simulate
2365 that with expression code iteration. */
2366 (for cmp (tcc_comparison)
2367 icmp (inverted_tcc_comparison)
2368 ncmp (inverted_tcc_comparison_with_nans)
2369 /* Ideally we'd like to combine the following two patterns
2370 and handle some more cases by using
2371 (logical_inverted_value (cmp @0 @1))
2372 here but for that genmatch would need to "inline" that.
2373 For now implement what forward_propagate_comparison did. */
2374 (simplify
2375 (bit_not (cmp @0 @1))
2376 (if (VECTOR_TYPE_P (type)
2377 || (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1))
2378 /* Comparison inversion may be impossible for trapping math,
2379 invert_tree_comparison will tell us. But we can't use
2380 a computed operator in the replacement tree thus we have
2381 to play the trick below. */
2382 (with { enum tree_code ic = invert_tree_comparison
2383 (cmp, HONOR_NANS (@0)); }
2384 (if (ic == icmp)
2385 (icmp @0 @1)
2386 (if (ic == ncmp)
2387 (ncmp @0 @1))))))
2388 (simplify
2389 (bit_xor (cmp @0 @1) integer_truep)
2390 (with { enum tree_code ic = invert_tree_comparison
2391 (cmp, HONOR_NANS (@0)); }
2392 (if (ic == icmp)
2393 (icmp @0 @1)
2394 (if (ic == ncmp)
2395 (ncmp @0 @1))))))
2396
2397 /* Transform comparisons of the form X - Y CMP 0 to X CMP Y.
2398 ??? The transformation is valid for the other operators if overflow
2399 is undefined for the type, but performing it here badly interacts
2400 with the transformation in fold_cond_expr_with_comparison which
2401 attempts to synthetize ABS_EXPR. */
2402 (for cmp (eq ne)
2403 (simplify
2404 (cmp (minus@2 @0 @1) integer_zerop)
2405 (if (single_use (@2))
2406 (cmp @0 @1))))
2407
2408 /* Transform comparisons of the form X * C1 CMP 0 to X CMP 0 in the
2409 signed arithmetic case. That form is created by the compiler
2410 often enough for folding it to be of value. One example is in
2411 computing loop trip counts after Operator Strength Reduction. */
2412 (for cmp (simple_comparison)
2413 scmp (swapped_simple_comparison)
2414 (simplify
2415 (cmp (mult@3 @0 INTEGER_CST@1) integer_zerop@2)
2416 /* Handle unfolded multiplication by zero. */
2417 (if (integer_zerop (@1))
2418 (cmp @1 @2)
2419 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2420 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2421 && single_use (@3))
2422 /* If @1 is negative we swap the sense of the comparison. */
2423 (if (tree_int_cst_sgn (@1) < 0)
2424 (scmp @0 @2)
2425 (cmp @0 @2))))))
2426
2427 /* Simplify comparison of something with itself. For IEEE
2428 floating-point, we can only do some of these simplifications. */
2429 (for cmp (eq ge le)
2430 (simplify
2431 (cmp @0 @0)
2432 (if (! FLOAT_TYPE_P (TREE_TYPE (@0))
2433 || ! HONOR_NANS (@0))
2434 { constant_boolean_node (true, type); }
2435 (if (cmp != EQ_EXPR)
2436 (eq @0 @0)))))
2437 (for cmp (ne gt lt)
2438 (simplify
2439 (cmp @0 @0)
2440 (if (cmp != NE_EXPR
2441 || ! FLOAT_TYPE_P (TREE_TYPE (@0))
2442 || ! HONOR_NANS (@0))
2443 { constant_boolean_node (false, type); })))
2444 (for cmp (unle unge uneq)
2445 (simplify
2446 (cmp @0 @0)
2447 { constant_boolean_node (true, type); }))
2448 (for cmp (unlt ungt)
2449 (simplify
2450 (cmp @0 @0)
2451 (unordered @0 @0)))
2452 (simplify
2453 (ltgt @0 @0)
2454 (if (!flag_trapping_math)
2455 { constant_boolean_node (false, type); }))
2456
2457 /* Fold ~X op ~Y as Y op X. */
2458 (for cmp (simple_comparison)
2459 (simplify
2460 (cmp (bit_not@2 @0) (bit_not@3 @1))
2461 (if (single_use (@2) && single_use (@3))
2462 (cmp @1 @0))))
2463
2464 /* Fold ~X op C as X op' ~C, where op' is the swapped comparison. */
2465 (for cmp (simple_comparison)
2466 scmp (swapped_simple_comparison)
2467 (simplify
2468 (cmp (bit_not@2 @0) CONSTANT_CLASS_P@1)
2469 (if (single_use (@2)
2470 && (TREE_CODE (@1) == INTEGER_CST || TREE_CODE (@1) == VECTOR_CST))
2471 (scmp @0 (bit_not @1)))))
2472
2473 (for cmp (simple_comparison)
2474 /* Fold (double)float1 CMP (double)float2 into float1 CMP float2. */
2475 (simplify
2476 (cmp (convert@2 @0) (convert? @1))
2477 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
2478 && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@2))
2479 == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0)))
2480 && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@2))
2481 == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1))))
2482 (with
2483 {
2484 tree type1 = TREE_TYPE (@1);
2485 if (TREE_CODE (@1) == REAL_CST && !DECIMAL_FLOAT_TYPE_P (type1))
2486 {
2487 REAL_VALUE_TYPE orig = TREE_REAL_CST (@1);
2488 if (TYPE_PRECISION (type1) > TYPE_PRECISION (float_type_node)
2489 && exact_real_truncate (TYPE_MODE (float_type_node), &orig))
2490 type1 = float_type_node;
2491 if (TYPE_PRECISION (type1) > TYPE_PRECISION (double_type_node)
2492 && exact_real_truncate (TYPE_MODE (double_type_node), &orig))
2493 type1 = double_type_node;
2494 }
2495 tree newtype
2496 = (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type1)
2497 ? TREE_TYPE (@0) : type1);
2498 }
2499 (if (TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (newtype))
2500 (cmp (convert:newtype @0) (convert:newtype @1))))))
2501
2502 (simplify
2503 (cmp @0 REAL_CST@1)
2504 /* IEEE doesn't distinguish +0 and -0 in comparisons. */
2505 (switch
2506 /* a CMP (-0) -> a CMP 0 */
2507 (if (REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@1)))
2508 (cmp @0 { build_real (TREE_TYPE (@1), dconst0); }))
2509 /* x != NaN is always true, other ops are always false. */
2510 (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
2511 && ! HONOR_SNANS (@1))
2512 { constant_boolean_node (cmp == NE_EXPR, type); })
2513 /* Fold comparisons against infinity. */
2514 (if (REAL_VALUE_ISINF (TREE_REAL_CST (@1))
2515 && MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (@1))))
2516 (with
2517 {
2518 REAL_VALUE_TYPE max;
2519 enum tree_code code = cmp;
2520 bool neg = REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1));
2521 if (neg)
2522 code = swap_tree_comparison (code);
2523 }
2524 (switch
2525 /* x > +Inf is always false, if with ignore sNANs. */
2526 (if (code == GT_EXPR
2527 && ! HONOR_SNANS (@0))
2528 { constant_boolean_node (false, type); })
2529 (if (code == LE_EXPR)
2530 /* x <= +Inf is always true, if we don't case about NaNs. */
2531 (if (! HONOR_NANS (@0))
2532 { constant_boolean_node (true, type); }
2533 /* x <= +Inf is the same as x == x, i.e. !isnan(x). */
2534 (eq @0 @0)))
2535 /* x == +Inf and x >= +Inf are always equal to x > DBL_MAX. */
2536 (if (code == EQ_EXPR || code == GE_EXPR)
2537 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
2538 (if (neg)
2539 (lt @0 { build_real (TREE_TYPE (@0), max); })
2540 (gt @0 { build_real (TREE_TYPE (@0), max); }))))
2541 /* x < +Inf is always equal to x <= DBL_MAX. */
2542 (if (code == LT_EXPR)
2543 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
2544 (if (neg)
2545 (ge @0 { build_real (TREE_TYPE (@0), max); })
2546 (le @0 { build_real (TREE_TYPE (@0), max); }))))
2547 /* x != +Inf is always equal to !(x > DBL_MAX). */
2548 (if (code == NE_EXPR)
2549 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
2550 (if (! HONOR_NANS (@0))
2551 (if (neg)
2552 (ge @0 { build_real (TREE_TYPE (@0), max); })
2553 (le @0 { build_real (TREE_TYPE (@0), max); }))
2554 (if (neg)
2555 (bit_xor (lt @0 { build_real (TREE_TYPE (@0), max); })
2556 { build_one_cst (type); })
2557 (bit_xor (gt @0 { build_real (TREE_TYPE (@0), max); })
2558 { build_one_cst (type); }))))))))))
2559
2560 /* If this is a comparison of a real constant with a PLUS_EXPR
2561 or a MINUS_EXPR of a real constant, we can convert it into a
2562 comparison with a revised real constant as long as no overflow
2563 occurs when unsafe_math_optimizations are enabled. */
2564 (if (flag_unsafe_math_optimizations)
2565 (for op (plus minus)
2566 (simplify
2567 (cmp (op @0 REAL_CST@1) REAL_CST@2)
2568 (with
2569 {
2570 tree tem = const_binop (op == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR,
2571 TREE_TYPE (@1), @2, @1);
2572 }
2573 (if (tem && !TREE_OVERFLOW (tem))
2574 (cmp @0 { tem; }))))))
2575
2576 /* Likewise, we can simplify a comparison of a real constant with
2577 a MINUS_EXPR whose first operand is also a real constant, i.e.
2578 (c1 - x) < c2 becomes x > c1-c2. Reordering is allowed on
2579 floating-point types only if -fassociative-math is set. */
2580 (if (flag_associative_math)
2581 (simplify
2582 (cmp (minus REAL_CST@0 @1) REAL_CST@2)
2583 (with { tree tem = const_binop (MINUS_EXPR, TREE_TYPE (@1), @0, @2); }
2584 (if (tem && !TREE_OVERFLOW (tem))
2585 (cmp { tem; } @1)))))
2586
2587 /* Fold comparisons against built-in math functions. */
2588 (if (flag_unsafe_math_optimizations
2589 && ! flag_errno_math)
2590 (for sq (SQRT)
2591 (simplify
2592 (cmp (sq @0) REAL_CST@1)
2593 (switch
2594 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
2595 (switch
2596 /* sqrt(x) < y is always false, if y is negative. */
2597 (if (cmp == EQ_EXPR || cmp == LT_EXPR || cmp == LE_EXPR)
2598 { constant_boolean_node (false, type); })
2599 /* sqrt(x) > y is always true, if y is negative and we
2600 don't care about NaNs, i.e. negative values of x. */
2601 (if (cmp == NE_EXPR || !HONOR_NANS (@0))
2602 { constant_boolean_node (true, type); })
2603 /* sqrt(x) > y is the same as x >= 0, if y is negative. */
2604 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })))
2605 (if (real_equal (TREE_REAL_CST_PTR (@1), &dconst0))
2606 (switch
2607 /* sqrt(x) < 0 is always false. */
2608 (if (cmp == LT_EXPR)
2609 { constant_boolean_node (false, type); })
2610 /* sqrt(x) >= 0 is always true if we don't care about NaNs. */
2611 (if (cmp == GE_EXPR && !HONOR_NANS (@0))
2612 { constant_boolean_node (true, type); })
2613 /* sqrt(x) <= 0 -> x == 0. */
2614 (if (cmp == LE_EXPR)
2615 (eq @0 @1))
2616 /* Otherwise sqrt(x) cmp 0 -> x cmp 0. Here cmp can be >=, >,
2617 == or !=. In the last case:
2618
2619 (sqrt(x) != 0) == (NaN != 0) == true == (x != 0)
2620
2621 if x is negative or NaN. Due to -funsafe-math-optimizations,
2622 the results for other x follow from natural arithmetic. */
2623 (cmp @0 @1)))
2624 (if (cmp == GT_EXPR || cmp == GE_EXPR)
2625 (with
2626 {
2627 REAL_VALUE_TYPE c2;
2628 real_arithmetic (&c2, MULT_EXPR,
2629 &TREE_REAL_CST (@1), &TREE_REAL_CST (@1));
2630 real_convert (&c2, TYPE_MODE (TREE_TYPE (@0)), &c2);
2631 }
2632 (if (REAL_VALUE_ISINF (c2))
2633 /* sqrt(x) > y is x == +Inf, when y is very large. */
2634 (if (HONOR_INFINITIES (@0))
2635 (eq @0 { build_real (TREE_TYPE (@0), c2); })
2636 { constant_boolean_node (false, type); })
2637 /* sqrt(x) > c is the same as x > c*c. */
2638 (cmp @0 { build_real (TREE_TYPE (@0), c2); }))))
2639 (if (cmp == LT_EXPR || cmp == LE_EXPR)
2640 (with
2641 {
2642 REAL_VALUE_TYPE c2;
2643 real_arithmetic (&c2, MULT_EXPR,
2644 &TREE_REAL_CST (@1), &TREE_REAL_CST (@1));
2645 real_convert (&c2, TYPE_MODE (TREE_TYPE (@0)), &c2);
2646 }
2647 (if (REAL_VALUE_ISINF (c2))
2648 (switch
2649 /* sqrt(x) < y is always true, when y is a very large
2650 value and we don't care about NaNs or Infinities. */
2651 (if (! HONOR_NANS (@0) && ! HONOR_INFINITIES (@0))
2652 { constant_boolean_node (true, type); })
2653 /* sqrt(x) < y is x != +Inf when y is very large and we
2654 don't care about NaNs. */
2655 (if (! HONOR_NANS (@0))
2656 (ne @0 { build_real (TREE_TYPE (@0), c2); }))
2657 /* sqrt(x) < y is x >= 0 when y is very large and we
2658 don't care about Infinities. */
2659 (if (! HONOR_INFINITIES (@0))
2660 (ge @0 { build_real (TREE_TYPE (@0), dconst0); }))
2661 /* sqrt(x) < y is x >= 0 && x != +Inf, when y is large. */
2662 (if (GENERIC)
2663 (truth_andif
2664 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
2665 (ne @0 { build_real (TREE_TYPE (@0), c2); }))))
2666 /* sqrt(x) < c is the same as x < c*c, if we ignore NaNs. */
2667 (if (! HONOR_NANS (@0))
2668 (cmp @0 { build_real (TREE_TYPE (@0), c2); })
2669 /* sqrt(x) < c is the same as x >= 0 && x < c*c. */
2670 (if (GENERIC)
2671 (truth_andif
2672 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
2673 (cmp @0 { build_real (TREE_TYPE (@0), c2); })))))))))
2674 /* Transform sqrt(x) cmp sqrt(y) -> x cmp y. */
2675 (simplify
2676 (cmp (sq @0) (sq @1))
2677 (if (! HONOR_NANS (@0))
2678 (cmp @0 @1))))))
2679
2680 /* Fold A /[ex] B CMP C to A CMP B * C. */
2681 (for cmp (eq ne)
2682 (simplify
2683 (cmp (exact_div @0 @1) INTEGER_CST@2)
2684 (if (!integer_zerop (@1))
2685 (if (wi::eq_p (@2, 0))
2686 (cmp @0 @2)
2687 (if (TREE_CODE (@1) == INTEGER_CST)
2688 (with
2689 {
2690 bool ovf;
2691 wide_int prod = wi::mul (@2, @1, TYPE_SIGN (TREE_TYPE (@1)), &ovf);
2692 }
2693 (if (ovf)
2694 { constant_boolean_node (cmp == NE_EXPR, type); }
2695 (cmp @0 { wide_int_to_tree (TREE_TYPE (@0), prod); }))))))))
2696 (for cmp (lt le gt ge)
2697 (simplify
2698 (cmp (exact_div @0 INTEGER_CST@1) INTEGER_CST@2)
2699 (if (wi::gt_p (@1, 0, TYPE_SIGN (TREE_TYPE (@1))))
2700 (with
2701 {
2702 bool ovf;
2703 wide_int prod = wi::mul (@2, @1, TYPE_SIGN (TREE_TYPE (@1)), &ovf);
2704 }
2705 (if (ovf)
2706 { constant_boolean_node (wi::lt_p (@2, 0, TYPE_SIGN (TREE_TYPE (@2)))
2707 != (cmp == LT_EXPR || cmp == LE_EXPR), type); }
2708 (cmp @0 { wide_int_to_tree (TREE_TYPE (@0), prod); }))))))
2709
2710 /* Unordered tests if either argument is a NaN. */
2711 (simplify
2712 (bit_ior (unordered @0 @0) (unordered @1 @1))
2713 (if (types_match (@0, @1))
2714 (unordered @0 @1)))
2715 (simplify
2716 (bit_and (ordered @0 @0) (ordered @1 @1))
2717 (if (types_match (@0, @1))
2718 (ordered @0 @1)))
2719 (simplify
2720 (bit_ior:c (unordered @0 @0) (unordered:c@2 @0 @1))
2721 @2)
2722 (simplify
2723 (bit_and:c (ordered @0 @0) (ordered:c@2 @0 @1))
2724 @2)
2725
2726 /* Simple range test simplifications. */
2727 /* A < B || A >= B -> true. */
2728 (for test1 (lt le le le ne ge)
2729 test2 (ge gt ge ne eq ne)
2730 (simplify
2731 (bit_ior:c (test1 @0 @1) (test2 @0 @1))
2732 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2733 || VECTOR_INTEGER_TYPE_P (TREE_TYPE (@0)))
2734 { constant_boolean_node (true, type); })))
2735 /* A < B && A >= B -> false. */
2736 (for test1 (lt lt lt le ne eq)
2737 test2 (ge gt eq gt eq gt)
2738 (simplify
2739 (bit_and:c (test1 @0 @1) (test2 @0 @1))
2740 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2741 || VECTOR_INTEGER_TYPE_P (TREE_TYPE (@0)))
2742 { constant_boolean_node (false, type); })))
2743
2744 /* -A CMP -B -> B CMP A. */
2745 (for cmp (tcc_comparison)
2746 scmp (swapped_tcc_comparison)
2747 (simplify
2748 (cmp (negate @0) (negate @1))
2749 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
2750 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2751 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
2752 (scmp @0 @1)))
2753 (simplify
2754 (cmp (negate @0) CONSTANT_CLASS_P@1)
2755 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
2756 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2757 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
2758 (with { tree tem = const_unop (NEGATE_EXPR, TREE_TYPE (@0), @1); }
2759 (if (tem && !TREE_OVERFLOW (tem))
2760 (scmp @0 { tem; }))))))
2761
2762 /* Convert ABS_EXPR<x> == 0 or ABS_EXPR<x> != 0 to x == 0 or x != 0. */
2763 (for op (eq ne)
2764 (simplify
2765 (op (abs @0) zerop@1)
2766 (op @0 @1)))
2767
2768 /* From fold_sign_changed_comparison and fold_widened_comparison. */
2769 (for cmp (simple_comparison)
2770 (simplify
2771 (cmp (convert@0 @00) (convert?@1 @10))
2772 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2773 /* Disable this optimization if we're casting a function pointer
2774 type on targets that require function pointer canonicalization. */
2775 && !(targetm.have_canonicalize_funcptr_for_compare ()
2776 && TREE_CODE (TREE_TYPE (@00)) == POINTER_TYPE
2777 && TREE_CODE (TREE_TYPE (TREE_TYPE (@00))) == FUNCTION_TYPE)
2778 && single_use (@0))
2779 (if (TYPE_PRECISION (TREE_TYPE (@00)) == TYPE_PRECISION (TREE_TYPE (@0))
2780 && (TREE_CODE (@10) == INTEGER_CST
2781 || (@1 != @10 && types_match (TREE_TYPE (@10), TREE_TYPE (@00))))
2782 && (TYPE_UNSIGNED (TREE_TYPE (@00)) == TYPE_UNSIGNED (TREE_TYPE (@0))
2783 || cmp == NE_EXPR
2784 || cmp == EQ_EXPR)
2785 && (POINTER_TYPE_P (TREE_TYPE (@00)) == POINTER_TYPE_P (TREE_TYPE (@0))))
2786 /* ??? The special-casing of INTEGER_CST conversion was in the original
2787 code and here to avoid a spurious overflow flag on the resulting
2788 constant which fold_convert produces. */
2789 (if (TREE_CODE (@1) == INTEGER_CST)
2790 (cmp @00 { force_fit_type (TREE_TYPE (@00), wi::to_widest (@1), 0,
2791 TREE_OVERFLOW (@1)); })
2792 (cmp @00 (convert @1)))
2793
2794 (if (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (TREE_TYPE (@00)))
2795 /* If possible, express the comparison in the shorter mode. */
2796 (if ((cmp == EQ_EXPR || cmp == NE_EXPR
2797 || TYPE_UNSIGNED (TREE_TYPE (@0)) == TYPE_UNSIGNED (TREE_TYPE (@00))
2798 || (!TYPE_UNSIGNED (TREE_TYPE (@0))
2799 && TYPE_UNSIGNED (TREE_TYPE (@00))))
2800 && (types_match (TREE_TYPE (@10), TREE_TYPE (@00))
2801 || ((TYPE_PRECISION (TREE_TYPE (@00))
2802 >= TYPE_PRECISION (TREE_TYPE (@10)))
2803 && (TYPE_UNSIGNED (TREE_TYPE (@00))
2804 == TYPE_UNSIGNED (TREE_TYPE (@10))))
2805 || (TREE_CODE (@10) == INTEGER_CST
2806 && INTEGRAL_TYPE_P (TREE_TYPE (@00))
2807 && int_fits_type_p (@10, TREE_TYPE (@00)))))
2808 (cmp @00 (convert @10))
2809 (if (TREE_CODE (@10) == INTEGER_CST
2810 && INTEGRAL_TYPE_P (TREE_TYPE (@00))
2811 && !int_fits_type_p (@10, TREE_TYPE (@00)))
2812 (with
2813 {
2814 tree min = lower_bound_in_type (TREE_TYPE (@10), TREE_TYPE (@00));
2815 tree max = upper_bound_in_type (TREE_TYPE (@10), TREE_TYPE (@00));
2816 bool above = integer_nonzerop (const_binop (LT_EXPR, type, max, @10));
2817 bool below = integer_nonzerop (const_binop (LT_EXPR, type, @10, min));
2818 }
2819 (if (above || below)
2820 (if (cmp == EQ_EXPR || cmp == NE_EXPR)
2821 { constant_boolean_node (cmp == EQ_EXPR ? false : true, type); }
2822 (if (cmp == LT_EXPR || cmp == LE_EXPR)
2823 { constant_boolean_node (above ? true : false, type); }
2824 (if (cmp == GT_EXPR || cmp == GE_EXPR)
2825 { constant_boolean_node (above ? false : true, type); }))))))))))))
2826
2827 (for cmp (eq ne)
2828 /* A local variable can never be pointed to by
2829 the default SSA name of an incoming parameter.
2830 SSA names are canonicalized to 2nd place. */
2831 (simplify
2832 (cmp addr@0 SSA_NAME@1)
2833 (if (SSA_NAME_IS_DEFAULT_DEF (@1)
2834 && TREE_CODE (SSA_NAME_VAR (@1)) == PARM_DECL)
2835 (with { tree base = get_base_address (TREE_OPERAND (@0, 0)); }
2836 (if (TREE_CODE (base) == VAR_DECL
2837 && auto_var_in_fn_p (base, current_function_decl))
2838 (if (cmp == NE_EXPR)
2839 { constant_boolean_node (true, type); }
2840 { constant_boolean_node (false, type); }))))))
2841
2842 /* Equality compare simplifications from fold_binary */
2843 (for cmp (eq ne)
2844
2845 /* If we have (A | C) == D where C & ~D != 0, convert this into 0.
2846 Similarly for NE_EXPR. */
2847 (simplify
2848 (cmp (convert?@3 (bit_ior @0 INTEGER_CST@1)) INTEGER_CST@2)
2849 (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0))
2850 && wi::bit_and_not (@1, @2) != 0)
2851 { constant_boolean_node (cmp == NE_EXPR, type); }))
2852
2853 /* (X ^ Y) == 0 becomes X == Y, and (X ^ Y) != 0 becomes X != Y. */
2854 (simplify
2855 (cmp (bit_xor @0 @1) integer_zerop)
2856 (cmp @0 @1))
2857
2858 /* (X ^ Y) == Y becomes X == 0.
2859 Likewise (X ^ Y) == X becomes Y == 0. */
2860 (simplify
2861 (cmp:c (bit_xor:c @0 @1) @0)
2862 (cmp @1 { build_zero_cst (TREE_TYPE (@1)); }))
2863
2864 /* (X ^ C1) op C2 can be rewritten as X op (C1 ^ C2). */
2865 (simplify
2866 (cmp (convert?@3 (bit_xor @0 INTEGER_CST@1)) INTEGER_CST@2)
2867 (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0)))
2868 (cmp @0 (bit_xor @1 (convert @2)))))
2869
2870 (simplify
2871 (cmp (convert? addr@0) integer_zerop)
2872 (if (tree_single_nonzero_warnv_p (@0, NULL))
2873 { constant_boolean_node (cmp == NE_EXPR, type); })))
2874
2875 /* If we have (A & C) == C where C is a power of 2, convert this into
2876 (A & C) != 0. Similarly for NE_EXPR. */
2877 (for cmp (eq ne)
2878 icmp (ne eq)
2879 (simplify
2880 (cmp (bit_and@2 @0 integer_pow2p@1) @1)
2881 (icmp @2 { build_zero_cst (TREE_TYPE (@0)); })))
2882
2883 /* If we have (A & C) != 0 ? D : 0 where C and D are powers of 2,
2884 convert this into a shift followed by ANDing with D. */
2885 (simplify
2886 (cond
2887 (ne (bit_and @0 integer_pow2p@1) integer_zerop)
2888 integer_pow2p@2 integer_zerop)
2889 (with {
2890 int shift = wi::exact_log2 (@2) - wi::exact_log2 (@1);
2891 }
2892 (if (shift > 0)
2893 (bit_and
2894 (lshift (convert @0) { build_int_cst (integer_type_node, shift); }) @2)
2895 (bit_and
2896 (convert (rshift @0 { build_int_cst (integer_type_node, -shift); })) @2))))
2897
2898 /* If we have (A & C) != 0 where C is the sign bit of A, convert
2899 this into A < 0. Similarly for (A & C) == 0 into A >= 0. */
2900 (for cmp (eq ne)
2901 ncmp (ge lt)
2902 (simplify
2903 (cmp (bit_and (convert?@2 @0) integer_pow2p@1) integer_zerop)
2904 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2905 && (TYPE_PRECISION (TREE_TYPE (@0))
2906 == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (@0))))
2907 && element_precision (@2) >= element_precision (@0)
2908 && wi::only_sign_bit_p (@1, element_precision (@0)))
2909 (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
2910 (ncmp (convert:stype @0) { build_zero_cst (stype); })))))
2911
2912 /* If we have A < 0 ? C : 0 where C is a power of 2, convert
2913 this into a right shift or sign extension followed by ANDing with C. */
2914 (simplify
2915 (cond
2916 (lt @0 integer_zerop)
2917 integer_pow2p@1 integer_zerop)
2918 (if (!TYPE_UNSIGNED (TREE_TYPE (@0)))
2919 (with {
2920 int shift = element_precision (@0) - wi::exact_log2 (@1) - 1;
2921 }
2922 (if (shift >= 0)
2923 (bit_and
2924 (convert (rshift @0 { build_int_cst (integer_type_node, shift); }))
2925 @1)
2926 /* Otherwise ctype must be wider than TREE_TYPE (@0) and pure
2927 sign extension followed by AND with C will achieve the effect. */
2928 (bit_and (convert @0) @1)))))
2929
2930 /* When the addresses are not directly of decls compare base and offset.
2931 This implements some remaining parts of fold_comparison address
2932 comparisons but still no complete part of it. Still it is good
2933 enough to make fold_stmt not regress when not dispatching to fold_binary. */
2934 (for cmp (simple_comparison)
2935 (simplify
2936 (cmp (convert1?@2 addr@0) (convert2? addr@1))
2937 (with
2938 {
2939 HOST_WIDE_INT off0, off1;
2940 tree base0 = get_addr_base_and_unit_offset (TREE_OPERAND (@0, 0), &off0);
2941 tree base1 = get_addr_base_and_unit_offset (TREE_OPERAND (@1, 0), &off1);
2942 if (base0 && TREE_CODE (base0) == MEM_REF)
2943 {
2944 off0 += mem_ref_offset (base0).to_short_addr ();
2945 base0 = TREE_OPERAND (base0, 0);
2946 }
2947 if (base1 && TREE_CODE (base1) == MEM_REF)
2948 {
2949 off1 += mem_ref_offset (base1).to_short_addr ();
2950 base1 = TREE_OPERAND (base1, 0);
2951 }
2952 }
2953 (if (base0 && base1)
2954 (with
2955 {
2956 int equal = 2;
2957 /* Punt in GENERIC on variables with value expressions;
2958 the value expressions might point to fields/elements
2959 of other vars etc. */
2960 if (GENERIC
2961 && ((VAR_P (base0) && DECL_HAS_VALUE_EXPR_P (base0))
2962 || (VAR_P (base1) && DECL_HAS_VALUE_EXPR_P (base1))))
2963 ;
2964 else if (decl_in_symtab_p (base0)
2965 && decl_in_symtab_p (base1))
2966 equal = symtab_node::get_create (base0)
2967 ->equal_address_to (symtab_node::get_create (base1));
2968 else if ((DECL_P (base0)
2969 || TREE_CODE (base0) == SSA_NAME
2970 || TREE_CODE (base0) == STRING_CST)
2971 && (DECL_P (base1)
2972 || TREE_CODE (base1) == SSA_NAME
2973 || TREE_CODE (base1) == STRING_CST))
2974 equal = (base0 == base1);
2975 }
2976 (if (equal == 1
2977 && (cmp == EQ_EXPR || cmp == NE_EXPR
2978 /* If the offsets are equal we can ignore overflow. */
2979 || off0 == off1
2980 || POINTER_TYPE_OVERFLOW_UNDEFINED
2981 /* Or if we compare using pointers to decls or strings. */
2982 || (POINTER_TYPE_P (TREE_TYPE (@2))
2983 && (DECL_P (base0) || TREE_CODE (base0) == STRING_CST))))
2984 (switch
2985 (if (cmp == EQ_EXPR)
2986 { constant_boolean_node (off0 == off1, type); })
2987 (if (cmp == NE_EXPR)
2988 { constant_boolean_node (off0 != off1, type); })
2989 (if (cmp == LT_EXPR)
2990 { constant_boolean_node (off0 < off1, type); })
2991 (if (cmp == LE_EXPR)
2992 { constant_boolean_node (off0 <= off1, type); })
2993 (if (cmp == GE_EXPR)
2994 { constant_boolean_node (off0 >= off1, type); })
2995 (if (cmp == GT_EXPR)
2996 { constant_boolean_node (off0 > off1, type); }))
2997 (if (equal == 0
2998 && DECL_P (base0) && DECL_P (base1)
2999 /* If we compare this as integers require equal offset. */
3000 && (!INTEGRAL_TYPE_P (TREE_TYPE (@2))
3001 || off0 == off1))
3002 (switch
3003 (if (cmp == EQ_EXPR)
3004 { constant_boolean_node (false, type); })
3005 (if (cmp == NE_EXPR)
3006 { constant_boolean_node (true, type); })))))))))
3007
3008 /* Simplify pointer equality compares using PTA. */
3009 (for neeq (ne eq)
3010 (simplify
3011 (neeq @0 @1)
3012 (if (POINTER_TYPE_P (TREE_TYPE (@0))
3013 && ptrs_compare_unequal (@0, @1))
3014 { neeq == EQ_EXPR ? boolean_false_node : boolean_true_node; })))
3015
3016 /* PR70920: Transform (intptr_t)x eq/ne CST to x eq/ne (typeof x) CST.
3017 and (typeof ptr_cst) x eq/ne ptr_cst to x eq/ne (typeof x) CST.
3018 Disable the transform if either operand is pointer to function.
3019 This broke pr22051-2.c for arm where function pointer
3020 canonicalizaion is not wanted. */
3021
3022 (for cmp (ne eq)
3023 (simplify
3024 (cmp (convert @0) INTEGER_CST@1)
3025 (if ((POINTER_TYPE_P (TREE_TYPE (@0)) && !FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@0)))
3026 && INTEGRAL_TYPE_P (TREE_TYPE (@1)))
3027 || (INTEGRAL_TYPE_P (TREE_TYPE (@0)) && POINTER_TYPE_P (TREE_TYPE (@1))
3028 && !FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@1)))))
3029 (cmp @0 (convert @1)))))
3030
3031 /* Non-equality compare simplifications from fold_binary */
3032 (for cmp (lt gt le ge)
3033 /* Comparisons with the highest or lowest possible integer of
3034 the specified precision will have known values. */
3035 (simplify
3036 (cmp (convert?@2 @0) INTEGER_CST@1)
3037 (if ((INTEGRAL_TYPE_P (TREE_TYPE (@1)) || POINTER_TYPE_P (TREE_TYPE (@1)))
3038 && tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@0)))
3039 (with
3040 {
3041 tree arg1_type = TREE_TYPE (@1);
3042 unsigned int prec = TYPE_PRECISION (arg1_type);
3043 wide_int max = wi::max_value (arg1_type);
3044 wide_int signed_max = wi::max_value (prec, SIGNED);
3045 wide_int min = wi::min_value (arg1_type);
3046 }
3047 (switch
3048 (if (wi::eq_p (@1, max))
3049 (switch
3050 (if (cmp == GT_EXPR)
3051 { constant_boolean_node (false, type); })
3052 (if (cmp == GE_EXPR)
3053 (eq @2 @1))
3054 (if (cmp == LE_EXPR)
3055 { constant_boolean_node (true, type); })
3056 (if (cmp == LT_EXPR)
3057 (ne @2 @1))))
3058 (if (wi::eq_p (@1, min))
3059 (switch
3060 (if (cmp == LT_EXPR)
3061 { constant_boolean_node (false, type); })
3062 (if (cmp == LE_EXPR)
3063 (eq @2 @1))
3064 (if (cmp == GE_EXPR)
3065 { constant_boolean_node (true, type); })
3066 (if (cmp == GT_EXPR)
3067 (ne @2 @1))))
3068 (if (wi::eq_p (@1, max - 1))
3069 (switch
3070 (if (cmp == GT_EXPR)
3071 (eq @2 { wide_int_to_tree (TREE_TYPE (@1), wi::add (@1, 1)); }))
3072 (if (cmp == LE_EXPR)
3073 (ne @2 { wide_int_to_tree (TREE_TYPE (@1), wi::add (@1, 1)); }))))
3074 (if (wi::eq_p (@1, min + 1))
3075 (switch
3076 (if (cmp == GE_EXPR)
3077 (ne @2 { wide_int_to_tree (TREE_TYPE (@1), wi::sub (@1, 1)); }))
3078 (if (cmp == LT_EXPR)
3079 (eq @2 { wide_int_to_tree (TREE_TYPE (@1), wi::sub (@1, 1)); }))))
3080 (if (wi::eq_p (@1, signed_max)
3081 && TYPE_UNSIGNED (arg1_type)
3082 /* We will flip the signedness of the comparison operator
3083 associated with the mode of @1, so the sign bit is
3084 specified by this mode. Check that @1 is the signed
3085 max associated with this sign bit. */
3086 && prec == GET_MODE_PRECISION (TYPE_MODE (arg1_type))
3087 /* signed_type does not work on pointer types. */
3088 && INTEGRAL_TYPE_P (arg1_type))
3089 /* The following case also applies to X < signed_max+1
3090 and X >= signed_max+1 because previous transformations. */
3091 (if (cmp == LE_EXPR || cmp == GT_EXPR)
3092 (with { tree st = signed_type_for (arg1_type); }
3093 (if (cmp == LE_EXPR)
3094 (ge (convert:st @0) { build_zero_cst (st); })
3095 (lt (convert:st @0) { build_zero_cst (st); }))))))))))
3096
3097 (for cmp (unordered ordered unlt unle ungt unge uneq ltgt)
3098 /* If the second operand is NaN, the result is constant. */
3099 (simplify
3100 (cmp @0 REAL_CST@1)
3101 (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
3102 && (cmp != LTGT_EXPR || ! flag_trapping_math))
3103 { constant_boolean_node (cmp == ORDERED_EXPR || cmp == LTGT_EXPR
3104 ? false : true, type); })))
3105
3106 /* bool_var != 0 becomes bool_var. */
3107 (simplify
3108 (ne @0 integer_zerop)
3109 (if (TREE_CODE (TREE_TYPE (@0)) == BOOLEAN_TYPE
3110 && types_match (type, TREE_TYPE (@0)))
3111 (non_lvalue @0)))
3112 /* bool_var == 1 becomes bool_var. */
3113 (simplify
3114 (eq @0 integer_onep)
3115 (if (TREE_CODE (TREE_TYPE (@0)) == BOOLEAN_TYPE
3116 && types_match (type, TREE_TYPE (@0)))
3117 (non_lvalue @0)))
3118 /* Do not handle
3119 bool_var == 0 becomes !bool_var or
3120 bool_var != 1 becomes !bool_var
3121 here because that only is good in assignment context as long
3122 as we require a tcc_comparison in GIMPLE_CONDs where we'd
3123 replace if (x == 0) with tem = ~x; if (tem != 0) which is
3124 clearly less optimal and which we'll transform again in forwprop. */
3125
3126 /* When one argument is a constant, overflow detection can be simplified.
3127 Currently restricted to single use so as not to interfere too much with
3128 ADD_OVERFLOW detection in tree-ssa-math-opts.c.
3129 A + CST CMP A -> A CMP' CST' */
3130 (for cmp (lt le ge gt)
3131 out (gt gt le le)
3132 (simplify
3133 (cmp:c (plus@2 @0 INTEGER_CST@1) @0)
3134 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
3135 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))
3136 && wi::ne_p (@1, 0)
3137 && single_use (@2))
3138 (out @0 { wide_int_to_tree (TREE_TYPE (@0), wi::max_value
3139 (TYPE_PRECISION (TREE_TYPE (@0)), UNSIGNED) - @1); }))))
3140
3141 /* To detect overflow in unsigned A - B, A < B is simpler than A - B > A.
3142 However, the detection logic for SUB_OVERFLOW in tree-ssa-math-opts.c
3143 expects the long form, so we restrict the transformation for now. */
3144 (for cmp (gt le)
3145 (simplify
3146 (cmp:c (minus@2 @0 @1) @0)
3147 (if (single_use (@2)
3148 && ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3149 && TYPE_UNSIGNED (TREE_TYPE (@0))
3150 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
3151 (cmp @1 @0))))
3152
3153 /* Testing for overflow is unnecessary if we already know the result. */
3154 /* A - B > A */
3155 (for cmp (gt le)
3156 out (ne eq)
3157 (simplify
3158 (cmp:c (realpart (IFN_SUB_OVERFLOW@2 @0 @1)) @0)
3159 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
3160 && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
3161 (out (imagpart @2) { build_zero_cst (TREE_TYPE (@0)); }))))
3162 /* A + B < A */
3163 (for cmp (lt ge)
3164 out (ne eq)
3165 (simplify
3166 (cmp:c (realpart (IFN_ADD_OVERFLOW:c@2 @0 @1)) @0)
3167 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
3168 && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
3169 (out (imagpart @2) { build_zero_cst (TREE_TYPE (@0)); }))))
3170
3171 /* For unsigned operands, -1 / B < A checks whether A * B would overflow.
3172 Simplify it to __builtin_mul_overflow (A, B, <unused>). */
3173 (for cmp (lt ge)
3174 out (ne eq)
3175 (simplify
3176 (cmp:c (trunc_div:s integer_all_onesp @1) @0)
3177 (if (TYPE_UNSIGNED (TREE_TYPE (@0)) && !VECTOR_TYPE_P (TREE_TYPE (@0)))
3178 (with { tree t = TREE_TYPE (@0), cpx = build_complex_type (t); }
3179 (out (imagpart (IFN_MUL_OVERFLOW:cpx @0 @1)) { build_zero_cst (t); })))))
3180
3181 /* Simplification of math builtins. These rules must all be optimizations
3182 as well as IL simplifications. If there is a possibility that the new
3183 form could be a pessimization, the rule should go in the canonicalization
3184 section that follows this one.
3185
3186 Rules can generally go in this section if they satisfy one of
3187 the following:
3188
3189 - the rule describes an identity
3190
3191 - the rule replaces calls with something as simple as addition or
3192 multiplication
3193
3194 - the rule contains unary calls only and simplifies the surrounding
3195 arithmetic. (The idea here is to exclude non-unary calls in which
3196 one operand is constant and in which the call is known to be cheap
3197 when the operand has that value.) */
3198
3199 (if (flag_unsafe_math_optimizations)
3200 /* Simplify sqrt(x) * sqrt(x) -> x. */
3201 (simplify
3202 (mult (SQRT@1 @0) @1)
3203 (if (!HONOR_SNANS (type))
3204 @0))
3205
3206 /* Simplify sqrt(x) * sqrt(y) -> sqrt(x*y). */
3207 (for root (SQRT CBRT)
3208 (simplify
3209 (mult (root:s @0) (root:s @1))
3210 (root (mult @0 @1))))
3211
3212 /* Simplify expN(x) * expN(y) -> expN(x+y). */
3213 (for exps (EXP EXP2 EXP10 POW10)
3214 (simplify
3215 (mult (exps:s @0) (exps:s @1))
3216 (exps (plus @0 @1))))
3217
3218 /* Simplify a/root(b/c) into a*root(c/b). */
3219 (for root (SQRT CBRT)
3220 (simplify
3221 (rdiv @0 (root:s (rdiv:s @1 @2)))
3222 (mult @0 (root (rdiv @2 @1)))))
3223
3224 /* Simplify x/expN(y) into x*expN(-y). */
3225 (for exps (EXP EXP2 EXP10 POW10)
3226 (simplify
3227 (rdiv @0 (exps:s @1))
3228 (mult @0 (exps (negate @1)))))
3229
3230 (for logs (LOG LOG2 LOG10 LOG10)
3231 exps (EXP EXP2 EXP10 POW10)
3232 /* logN(expN(x)) -> x. */
3233 (simplify
3234 (logs (exps @0))
3235 @0)
3236 /* expN(logN(x)) -> x. */
3237 (simplify
3238 (exps (logs @0))
3239 @0))
3240
3241 /* Optimize logN(func()) for various exponential functions. We
3242 want to determine the value "x" and the power "exponent" in
3243 order to transform logN(x**exponent) into exponent*logN(x). */
3244 (for logs (LOG LOG LOG LOG2 LOG2 LOG2 LOG10 LOG10)
3245 exps (EXP2 EXP10 POW10 EXP EXP10 POW10 EXP EXP2)
3246 (simplify
3247 (logs (exps @0))
3248 (if (SCALAR_FLOAT_TYPE_P (type))
3249 (with {
3250 tree x;
3251 switch (exps)
3252 {
3253 CASE_CFN_EXP:
3254 /* Prepare to do logN(exp(exponent)) -> exponent*logN(e). */
3255 x = build_real_truncate (type, dconst_e ());
3256 break;
3257 CASE_CFN_EXP2:
3258 /* Prepare to do logN(exp2(exponent)) -> exponent*logN(2). */
3259 x = build_real (type, dconst2);
3260 break;
3261 CASE_CFN_EXP10:
3262 CASE_CFN_POW10:
3263 /* Prepare to do logN(exp10(exponent)) -> exponent*logN(10). */
3264 {
3265 REAL_VALUE_TYPE dconst10;
3266 real_from_integer (&dconst10, VOIDmode, 10, SIGNED);
3267 x = build_real (type, dconst10);
3268 }
3269 break;
3270 default:
3271 gcc_unreachable ();
3272 }
3273 }
3274 (mult (logs { x; }) @0)))))
3275
3276 (for logs (LOG LOG
3277 LOG2 LOG2
3278 LOG10 LOG10)
3279 exps (SQRT CBRT)
3280 (simplify
3281 (logs (exps @0))
3282 (if (SCALAR_FLOAT_TYPE_P (type))
3283 (with {
3284 tree x;
3285 switch (exps)
3286 {
3287 CASE_CFN_SQRT:
3288 /* Prepare to do logN(sqrt(x)) -> 0.5*logN(x). */
3289 x = build_real (type, dconsthalf);
3290 break;
3291 CASE_CFN_CBRT:
3292 /* Prepare to do logN(cbrt(x)) -> (1/3)*logN(x). */
3293 x = build_real_truncate (type, dconst_third ());
3294 break;
3295 default:
3296 gcc_unreachable ();
3297 }
3298 }
3299 (mult { x; } (logs @0))))))
3300
3301 /* logN(pow(x,exponent)) -> exponent*logN(x). */
3302 (for logs (LOG LOG2 LOG10)
3303 pows (POW)
3304 (simplify
3305 (logs (pows @0 @1))
3306 (mult @1 (logs @0))))
3307
3308 (for sqrts (SQRT)
3309 cbrts (CBRT)
3310 pows (POW)
3311 exps (EXP EXP2 EXP10 POW10)
3312 /* sqrt(expN(x)) -> expN(x*0.5). */
3313 (simplify
3314 (sqrts (exps @0))
3315 (exps (mult @0 { build_real (type, dconsthalf); })))
3316 /* cbrt(expN(x)) -> expN(x/3). */
3317 (simplify
3318 (cbrts (exps @0))
3319 (exps (mult @0 { build_real_truncate (type, dconst_third ()); })))
3320 /* pow(expN(x), y) -> expN(x*y). */
3321 (simplify
3322 (pows (exps @0) @1)
3323 (exps (mult @0 @1))))
3324
3325 /* tan(atan(x)) -> x. */
3326 (for tans (TAN)
3327 atans (ATAN)
3328 (simplify
3329 (tans (atans @0))
3330 @0)))
3331
3332 /* cabs(x+0i) or cabs(0+xi) -> abs(x). */
3333 (simplify
3334 (CABS (complex:C @0 real_zerop@1))
3335 (abs @0))
3336
3337 /* trunc(trunc(x)) -> trunc(x), etc. */
3338 (for fns (TRUNC FLOOR CEIL ROUND NEARBYINT RINT)
3339 (simplify
3340 (fns (fns @0))
3341 (fns @0)))
3342 /* f(x) -> x if x is integer valued and f does nothing for such values. */
3343 (for fns (TRUNC FLOOR CEIL ROUND NEARBYINT RINT)
3344 (simplify
3345 (fns integer_valued_real_p@0)
3346 @0))
3347
3348 /* hypot(x,0) and hypot(0,x) -> abs(x). */
3349 (simplify
3350 (HYPOT:c @0 real_zerop@1)
3351 (abs @0))
3352
3353 /* pow(1,x) -> 1. */
3354 (simplify
3355 (POW real_onep@0 @1)
3356 @0)
3357
3358 (simplify
3359 /* copysign(x,x) -> x. */
3360 (COPYSIGN @0 @0)
3361 @0)
3362
3363 (simplify
3364 /* copysign(x,y) -> fabs(x) if y is nonnegative. */
3365 (COPYSIGN @0 tree_expr_nonnegative_p@1)
3366 (abs @0))
3367
3368 (for scale (LDEXP SCALBN SCALBLN)
3369 /* ldexp(0, x) -> 0. */
3370 (simplify
3371 (scale real_zerop@0 @1)
3372 @0)
3373 /* ldexp(x, 0) -> x. */
3374 (simplify
3375 (scale @0 integer_zerop@1)
3376 @0)
3377 /* ldexp(x, y) -> x if x is +-Inf or NaN. */
3378 (simplify
3379 (scale REAL_CST@0 @1)
3380 (if (!real_isfinite (TREE_REAL_CST_PTR (@0)))
3381 @0)))
3382
3383 /* Canonicalization of sequences of math builtins. These rules represent
3384 IL simplifications but are not necessarily optimizations.
3385
3386 The sincos pass is responsible for picking "optimal" implementations
3387 of math builtins, which may be more complicated and can sometimes go
3388 the other way, e.g. converting pow into a sequence of sqrts.
3389 We only want to do these canonicalizations before the pass has run. */
3390
3391 (if (flag_unsafe_math_optimizations && canonicalize_math_p ())
3392 /* Simplify tan(x) * cos(x) -> sin(x). */
3393 (simplify
3394 (mult:c (TAN:s @0) (COS:s @0))
3395 (SIN @0))
3396
3397 /* Simplify x * pow(x,c) -> pow(x,c+1). */
3398 (simplify
3399 (mult:c @0 (POW:s @0 REAL_CST@1))
3400 (if (!TREE_OVERFLOW (@1))
3401 (POW @0 (plus @1 { build_one_cst (type); }))))
3402
3403 /* Simplify sin(x) / cos(x) -> tan(x). */
3404 (simplify
3405 (rdiv (SIN:s @0) (COS:s @0))
3406 (TAN @0))
3407
3408 /* Simplify cos(x) / sin(x) -> 1 / tan(x). */
3409 (simplify
3410 (rdiv (COS:s @0) (SIN:s @0))
3411 (rdiv { build_one_cst (type); } (TAN @0)))
3412
3413 /* Simplify sin(x) / tan(x) -> cos(x). */
3414 (simplify
3415 (rdiv (SIN:s @0) (TAN:s @0))
3416 (if (! HONOR_NANS (@0)
3417 && ! HONOR_INFINITIES (@0))
3418 (COS @0)))
3419
3420 /* Simplify tan(x) / sin(x) -> 1.0 / cos(x). */
3421 (simplify
3422 (rdiv (TAN:s @0) (SIN:s @0))
3423 (if (! HONOR_NANS (@0)
3424 && ! HONOR_INFINITIES (@0))
3425 (rdiv { build_one_cst (type); } (COS @0))))
3426
3427 /* Simplify pow(x,y) * pow(x,z) -> pow(x,y+z). */
3428 (simplify
3429 (mult (POW:s @0 @1) (POW:s @0 @2))
3430 (POW @0 (plus @1 @2)))
3431
3432 /* Simplify pow(x,y) * pow(z,y) -> pow(x*z,y). */
3433 (simplify
3434 (mult (POW:s @0 @1) (POW:s @2 @1))
3435 (POW (mult @0 @2) @1))
3436
3437 /* Simplify powi(x,y) * powi(z,y) -> powi(x*z,y). */
3438 (simplify
3439 (mult (POWI:s @0 @1) (POWI:s @2 @1))
3440 (POWI (mult @0 @2) @1))
3441
3442 /* Simplify pow(x,c) / x -> pow(x,c-1). */
3443 (simplify
3444 (rdiv (POW:s @0 REAL_CST@1) @0)
3445 (if (!TREE_OVERFLOW (@1))
3446 (POW @0 (minus @1 { build_one_cst (type); }))))
3447
3448 /* Simplify x / pow (y,z) -> x * pow(y,-z). */
3449 (simplify
3450 (rdiv @0 (POW:s @1 @2))
3451 (mult @0 (POW @1 (negate @2))))
3452
3453 (for sqrts (SQRT)
3454 cbrts (CBRT)
3455 pows (POW)
3456 /* sqrt(sqrt(x)) -> pow(x,1/4). */
3457 (simplify
3458 (sqrts (sqrts @0))
3459 (pows @0 { build_real (type, dconst_quarter ()); }))
3460 /* sqrt(cbrt(x)) -> pow(x,1/6). */
3461 (simplify
3462 (sqrts (cbrts @0))
3463 (pows @0 { build_real_truncate (type, dconst_sixth ()); }))
3464 /* cbrt(sqrt(x)) -> pow(x,1/6). */
3465 (simplify
3466 (cbrts (sqrts @0))
3467 (pows @0 { build_real_truncate (type, dconst_sixth ()); }))
3468 /* cbrt(cbrt(x)) -> pow(x,1/9), iff x is nonnegative. */
3469 (simplify
3470 (cbrts (cbrts tree_expr_nonnegative_p@0))
3471 (pows @0 { build_real_truncate (type, dconst_ninth ()); }))
3472 /* sqrt(pow(x,y)) -> pow(|x|,y*0.5). */
3473 (simplify
3474 (sqrts (pows @0 @1))
3475 (pows (abs @0) (mult @1 { build_real (type, dconsthalf); })))
3476 /* cbrt(pow(x,y)) -> pow(x,y/3), iff x is nonnegative. */
3477 (simplify
3478 (cbrts (pows tree_expr_nonnegative_p@0 @1))
3479 (pows @0 (mult @1 { build_real_truncate (type, dconst_third ()); })))
3480 /* pow(sqrt(x),y) -> pow(x,y*0.5). */
3481 (simplify
3482 (pows (sqrts @0) @1)
3483 (pows @0 (mult @1 { build_real (type, dconsthalf); })))
3484 /* pow(cbrt(x),y) -> pow(x,y/3) iff x is nonnegative. */
3485 (simplify
3486 (pows (cbrts tree_expr_nonnegative_p@0) @1)
3487 (pows @0 (mult @1 { build_real_truncate (type, dconst_third ()); })))
3488 /* pow(pow(x,y),z) -> pow(x,y*z) iff x is nonnegative. */
3489 (simplify
3490 (pows (pows tree_expr_nonnegative_p@0 @1) @2)
3491 (pows @0 (mult @1 @2))))
3492
3493 /* cabs(x+xi) -> fabs(x)*sqrt(2). */
3494 (simplify
3495 (CABS (complex @0 @0))
3496 (mult (abs @0) { build_real_truncate (type, dconst_sqrt2 ()); }))
3497
3498 /* hypot(x,x) -> fabs(x)*sqrt(2). */
3499 (simplify
3500 (HYPOT @0 @0)
3501 (mult (abs @0) { build_real_truncate (type, dconst_sqrt2 ()); }))
3502
3503 /* cexp(x+yi) -> exp(x)*cexpi(y). */
3504 (for cexps (CEXP)
3505 exps (EXP)
3506 cexpis (CEXPI)
3507 (simplify
3508 (cexps compositional_complex@0)
3509 (if (targetm.libc_has_function (function_c99_math_complex))
3510 (complex
3511 (mult (exps@1 (realpart @0)) (realpart (cexpis:type@2 (imagpart @0))))
3512 (mult @1 (imagpart @2)))))))
3513
3514 (if (canonicalize_math_p ())
3515 /* floor(x) -> trunc(x) if x is nonnegative. */
3516 (for floors (FLOOR)
3517 truncs (TRUNC)
3518 (simplify
3519 (floors tree_expr_nonnegative_p@0)
3520 (truncs @0))))
3521
3522 (match double_value_p
3523 @0
3524 (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == double_type_node)))
3525 (for froms (BUILT_IN_TRUNCL
3526 BUILT_IN_FLOORL
3527 BUILT_IN_CEILL
3528 BUILT_IN_ROUNDL
3529 BUILT_IN_NEARBYINTL
3530 BUILT_IN_RINTL)
3531 tos (BUILT_IN_TRUNC
3532 BUILT_IN_FLOOR
3533 BUILT_IN_CEIL
3534 BUILT_IN_ROUND
3535 BUILT_IN_NEARBYINT
3536 BUILT_IN_RINT)
3537 /* truncl(extend(x)) -> extend(trunc(x)), etc., if x is a double. */
3538 (if (optimize && canonicalize_math_p ())
3539 (simplify
3540 (froms (convert double_value_p@0))
3541 (convert (tos @0)))))
3542
3543 (match float_value_p
3544 @0
3545 (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == float_type_node)))
3546 (for froms (BUILT_IN_TRUNCL BUILT_IN_TRUNC
3547 BUILT_IN_FLOORL BUILT_IN_FLOOR
3548 BUILT_IN_CEILL BUILT_IN_CEIL
3549 BUILT_IN_ROUNDL BUILT_IN_ROUND
3550 BUILT_IN_NEARBYINTL BUILT_IN_NEARBYINT
3551 BUILT_IN_RINTL BUILT_IN_RINT)
3552 tos (BUILT_IN_TRUNCF BUILT_IN_TRUNCF
3553 BUILT_IN_FLOORF BUILT_IN_FLOORF
3554 BUILT_IN_CEILF BUILT_IN_CEILF
3555 BUILT_IN_ROUNDF BUILT_IN_ROUNDF
3556 BUILT_IN_NEARBYINTF BUILT_IN_NEARBYINTF
3557 BUILT_IN_RINTF BUILT_IN_RINTF)
3558 /* truncl(extend(x)) and trunc(extend(x)) -> extend(truncf(x)), etc.,
3559 if x is a float. */
3560 (if (optimize && canonicalize_math_p ()
3561 && targetm.libc_has_function (function_c99_misc))
3562 (simplify
3563 (froms (convert float_value_p@0))
3564 (convert (tos @0)))))
3565
3566 (for froms (XFLOORL XCEILL XROUNDL XRINTL)
3567 tos (XFLOOR XCEIL XROUND XRINT)
3568 /* llfloorl(extend(x)) -> llfloor(x), etc., if x is a double. */
3569 (if (optimize && canonicalize_math_p ())
3570 (simplify
3571 (froms (convert double_value_p@0))
3572 (tos @0))))
3573
3574 (for froms (XFLOORL XCEILL XROUNDL XRINTL
3575 XFLOOR XCEIL XROUND XRINT)
3576 tos (XFLOORF XCEILF XROUNDF XRINTF)
3577 /* llfloorl(extend(x)) and llfloor(extend(x)) -> llfloorf(x), etc.,
3578 if x is a float. */
3579 (if (optimize && canonicalize_math_p ())
3580 (simplify
3581 (froms (convert float_value_p@0))
3582 (tos @0))))
3583
3584 (if (canonicalize_math_p ())
3585 /* xfloor(x) -> fix_trunc(x) if x is nonnegative. */
3586 (for floors (IFLOOR LFLOOR LLFLOOR)
3587 (simplify
3588 (floors tree_expr_nonnegative_p@0)
3589 (fix_trunc @0))))
3590
3591 (if (canonicalize_math_p ())
3592 /* xfloor(x) -> fix_trunc(x), etc., if x is integer valued. */
3593 (for fns (IFLOOR LFLOOR LLFLOOR
3594 ICEIL LCEIL LLCEIL
3595 IROUND LROUND LLROUND)
3596 (simplify
3597 (fns integer_valued_real_p@0)
3598 (fix_trunc @0)))
3599 (if (!flag_errno_math)
3600 /* xrint(x) -> fix_trunc(x), etc., if x is integer valued. */
3601 (for rints (IRINT LRINT LLRINT)
3602 (simplify
3603 (rints integer_valued_real_p@0)
3604 (fix_trunc @0)))))
3605
3606 (if (canonicalize_math_p ())
3607 (for ifn (IFLOOR ICEIL IROUND IRINT)
3608 lfn (LFLOOR LCEIL LROUND LRINT)
3609 llfn (LLFLOOR LLCEIL LLROUND LLRINT)
3610 /* Canonicalize iround (x) to lround (x) on ILP32 targets where
3611 sizeof (int) == sizeof (long). */
3612 (if (TYPE_PRECISION (integer_type_node)
3613 == TYPE_PRECISION (long_integer_type_node))
3614 (simplify
3615 (ifn @0)
3616 (lfn:long_integer_type_node @0)))
3617 /* Canonicalize llround (x) to lround (x) on LP64 targets where
3618 sizeof (long long) == sizeof (long). */
3619 (if (TYPE_PRECISION (long_long_integer_type_node)
3620 == TYPE_PRECISION (long_integer_type_node))
3621 (simplify
3622 (llfn @0)
3623 (lfn:long_integer_type_node @0)))))
3624
3625 /* cproj(x) -> x if we're ignoring infinities. */
3626 (simplify
3627 (CPROJ @0)
3628 (if (!HONOR_INFINITIES (type))
3629 @0))
3630
3631 /* If the real part is inf and the imag part is known to be
3632 nonnegative, return (inf + 0i). */
3633 (simplify
3634 (CPROJ (complex REAL_CST@0 tree_expr_nonnegative_p@1))
3635 (if (real_isinf (TREE_REAL_CST_PTR (@0)))
3636 { build_complex_inf (type, false); }))
3637
3638 /* If the imag part is inf, return (inf+I*copysign(0,imag)). */
3639 (simplify
3640 (CPROJ (complex @0 REAL_CST@1))
3641 (if (real_isinf (TREE_REAL_CST_PTR (@1)))
3642 { build_complex_inf (type, TREE_REAL_CST_PTR (@1)->sign); }))
3643
3644 (for pows (POW)
3645 sqrts (SQRT)
3646 cbrts (CBRT)
3647 (simplify
3648 (pows @0 REAL_CST@1)
3649 (with {
3650 const REAL_VALUE_TYPE *value = TREE_REAL_CST_PTR (@1);
3651 REAL_VALUE_TYPE tmp;
3652 }
3653 (switch
3654 /* pow(x,0) -> 1. */
3655 (if (real_equal (value, &dconst0))
3656 { build_real (type, dconst1); })
3657 /* pow(x,1) -> x. */
3658 (if (real_equal (value, &dconst1))
3659 @0)
3660 /* pow(x,-1) -> 1/x. */
3661 (if (real_equal (value, &dconstm1))
3662 (rdiv { build_real (type, dconst1); } @0))
3663 /* pow(x,0.5) -> sqrt(x). */
3664 (if (flag_unsafe_math_optimizations
3665 && canonicalize_math_p ()
3666 && real_equal (value, &dconsthalf))
3667 (sqrts @0))
3668 /* pow(x,1/3) -> cbrt(x). */
3669 (if (flag_unsafe_math_optimizations
3670 && canonicalize_math_p ()
3671 && (tmp = real_value_truncate (TYPE_MODE (type), dconst_third ()),
3672 real_equal (value, &tmp)))
3673 (cbrts @0))))))
3674
3675 /* powi(1,x) -> 1. */
3676 (simplify
3677 (POWI real_onep@0 @1)
3678 @0)
3679
3680 (simplify
3681 (POWI @0 INTEGER_CST@1)
3682 (switch
3683 /* powi(x,0) -> 1. */
3684 (if (wi::eq_p (@1, 0))
3685 { build_real (type, dconst1); })
3686 /* powi(x,1) -> x. */
3687 (if (wi::eq_p (@1, 1))
3688 @0)
3689 /* powi(x,-1) -> 1/x. */
3690 (if (wi::eq_p (@1, -1))
3691 (rdiv { build_real (type, dconst1); } @0))))
3692
3693 /* Narrowing of arithmetic and logical operations.
3694
3695 These are conceptually similar to the transformations performed for
3696 the C/C++ front-ends by shorten_binary_op and shorten_compare. Long
3697 term we want to move all that code out of the front-ends into here. */
3698
3699 /* If we have a narrowing conversion of an arithmetic operation where
3700 both operands are widening conversions from the same type as the outer
3701 narrowing conversion. Then convert the innermost operands to a suitable
3702 unsigned type (to avoid introducing undefined behavior), perform the
3703 operation and convert the result to the desired type. */
3704 (for op (plus minus)
3705 (simplify
3706 (convert (op:s (convert@2 @0) (convert?@3 @1)))
3707 (if (INTEGRAL_TYPE_P (type)
3708 /* We check for type compatibility between @0 and @1 below,
3709 so there's no need to check that @1/@3 are integral types. */
3710 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
3711 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
3712 /* The precision of the type of each operand must match the
3713 precision of the mode of each operand, similarly for the
3714 result. */
3715 && (TYPE_PRECISION (TREE_TYPE (@0))
3716 == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (@0))))
3717 && (TYPE_PRECISION (TREE_TYPE (@1))
3718 == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (@1))))
3719 && TYPE_PRECISION (type) == GET_MODE_PRECISION (TYPE_MODE (type))
3720 /* The inner conversion must be a widening conversion. */
3721 && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
3722 && types_match (@0, type)
3723 && (types_match (@0, @1)
3724 /* Or the second operand is const integer or converted const
3725 integer from valueize. */
3726 || TREE_CODE (@1) == INTEGER_CST))
3727 (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
3728 (op @0 (convert @1))
3729 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
3730 (convert (op (convert:utype @0)
3731 (convert:utype @1))))))))
3732
3733 /* This is another case of narrowing, specifically when there's an outer
3734 BIT_AND_EXPR which masks off bits outside the type of the innermost
3735 operands. Like the previous case we have to convert the operands
3736 to unsigned types to avoid introducing undefined behavior for the
3737 arithmetic operation. */
3738 (for op (minus plus)
3739 (simplify
3740 (bit_and (op:s (convert@2 @0) (convert@3 @1)) INTEGER_CST@4)
3741 (if (INTEGRAL_TYPE_P (type)
3742 /* We check for type compatibility between @0 and @1 below,
3743 so there's no need to check that @1/@3 are integral types. */
3744 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
3745 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
3746 /* The precision of the type of each operand must match the
3747 precision of the mode of each operand, similarly for the
3748 result. */
3749 && (TYPE_PRECISION (TREE_TYPE (@0))
3750 == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (@0))))
3751 && (TYPE_PRECISION (TREE_TYPE (@1))
3752 == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (@1))))
3753 && TYPE_PRECISION (type) == GET_MODE_PRECISION (TYPE_MODE (type))
3754 /* The inner conversion must be a widening conversion. */
3755 && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
3756 && types_match (@0, @1)
3757 && (tree_int_cst_min_precision (@4, TYPE_SIGN (TREE_TYPE (@0)))
3758 <= TYPE_PRECISION (TREE_TYPE (@0)))
3759 && (wi::bit_and (@4, wi::mask (TYPE_PRECISION (TREE_TYPE (@0)),
3760 true, TYPE_PRECISION (type))) == 0))
3761 (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
3762 (with { tree ntype = TREE_TYPE (@0); }
3763 (convert (bit_and (op @0 @1) (convert:ntype @4))))
3764 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
3765 (convert (bit_and (op (convert:utype @0) (convert:utype @1))
3766 (convert:utype @4))))))))
3767
3768 /* Transform (@0 < @1 and @0 < @2) to use min,
3769 (@0 > @1 and @0 > @2) to use max */
3770 (for op (lt le gt ge)
3771 ext (min min max max)
3772 (simplify
3773 (bit_and (op:cs @0 @1) (op:cs @0 @2))
3774 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
3775 && TREE_CODE (@0) != INTEGER_CST)
3776 (op @0 (ext @1 @2)))))
3777
3778 (simplify
3779 /* signbit(x) -> 0 if x is nonnegative. */
3780 (SIGNBIT tree_expr_nonnegative_p@0)
3781 { integer_zero_node; })
3782
3783 (simplify
3784 /* signbit(x) -> x<0 if x doesn't have signed zeros. */
3785 (SIGNBIT @0)
3786 (if (!HONOR_SIGNED_ZEROS (@0))
3787 (convert (lt @0 { build_real (TREE_TYPE (@0), dconst0); }))))
3788
3789 /* Transform comparisons of the form X +- C1 CMP C2 to X CMP C2 -+ C1. */
3790 (for cmp (eq ne)
3791 (for op (plus minus)
3792 rop (minus plus)
3793 (simplify
3794 (cmp (op@3 @0 INTEGER_CST@1) INTEGER_CST@2)
3795 (if (!TREE_OVERFLOW (@1) && !TREE_OVERFLOW (@2)
3796 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0))
3797 && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@0))
3798 && !TYPE_SATURATING (TREE_TYPE (@0)))
3799 (with { tree res = int_const_binop (rop, @2, @1); }
3800 (if (TREE_OVERFLOW (res)
3801 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
3802 { constant_boolean_node (cmp == NE_EXPR, type); }
3803 (if (single_use (@3))
3804 (cmp @0 { res; }))))))))
3805 (for cmp (lt le gt ge)
3806 (for op (plus minus)
3807 rop (minus plus)
3808 (simplify
3809 (cmp (op@3 @0 INTEGER_CST@1) INTEGER_CST@2)
3810 (if (!TREE_OVERFLOW (@1) && !TREE_OVERFLOW (@2)
3811 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
3812 (with { tree res = int_const_binop (rop, @2, @1); }
3813 (if (TREE_OVERFLOW (res))
3814 {
3815 fold_overflow_warning (("assuming signed overflow does not occur "
3816 "when simplifying conditional to constant"),
3817 WARN_STRICT_OVERFLOW_CONDITIONAL);
3818 bool less = cmp == LE_EXPR || cmp == LT_EXPR;
3819 /* wi::ges_p (@2, 0) should be sufficient for a signed type. */
3820 bool ovf_high = wi::lt_p (@1, 0, TYPE_SIGN (TREE_TYPE (@1)))
3821 != (op == MINUS_EXPR);
3822 constant_boolean_node (less == ovf_high, type);
3823 }
3824 (if (single_use (@3))
3825 (with
3826 {
3827 fold_overflow_warning (("assuming signed overflow does not occur "
3828 "when changing X +- C1 cmp C2 to "
3829 "X cmp C2 -+ C1"),
3830 WARN_STRICT_OVERFLOW_COMPARISON);
3831 }
3832 (cmp @0 { res; })))))))))
3833
3834 /* Canonicalizations of BIT_FIELD_REFs. */
3835
3836 (simplify
3837 (BIT_FIELD_REF @0 @1 @2)
3838 (switch
3839 (if (TREE_CODE (TREE_TYPE (@0)) == COMPLEX_TYPE
3840 && tree_int_cst_equal (@1, TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))))
3841 (switch
3842 (if (integer_zerop (@2))
3843 (view_convert (realpart @0)))
3844 (if (tree_int_cst_equal (@2, TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))))
3845 (view_convert (imagpart @0)))))
3846 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
3847 && INTEGRAL_TYPE_P (type)
3848 /* On GIMPLE this should only apply to register arguments. */
3849 && (! GIMPLE || is_gimple_reg (@0))
3850 /* A bit-field-ref that referenced the full argument can be stripped. */
3851 && ((compare_tree_int (@1, TYPE_PRECISION (TREE_TYPE (@0))) == 0
3852 && integer_zerop (@2))
3853 /* Low-parts can be reduced to integral conversions.
3854 ??? The following doesn't work for PDP endian. */
3855 || (BYTES_BIG_ENDIAN == WORDS_BIG_ENDIAN
3856 /* Don't even think about BITS_BIG_ENDIAN. */
3857 && TYPE_PRECISION (TREE_TYPE (@0)) % BITS_PER_UNIT == 0
3858 && TYPE_PRECISION (type) % BITS_PER_UNIT == 0
3859 && compare_tree_int (@2, (BYTES_BIG_ENDIAN
3860 ? (TYPE_PRECISION (TREE_TYPE (@0))
3861 - TYPE_PRECISION (type))
3862 : 0)) == 0)))
3863 (convert @0))))
3864
3865 /* Simplify vector extracts. */
3866
3867 (simplify
3868 (BIT_FIELD_REF CONSTRUCTOR@0 @1 @2)
3869 (if (VECTOR_TYPE_P (TREE_TYPE (@0))
3870 && (types_match (type, TREE_TYPE (TREE_TYPE (@0)))
3871 || (VECTOR_TYPE_P (type)
3872 && types_match (TREE_TYPE (type), TREE_TYPE (TREE_TYPE (@0))))))
3873 (with
3874 {
3875 tree ctor = (TREE_CODE (@0) == SSA_NAME
3876 ? gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0)) : @0);
3877 tree eltype = TREE_TYPE (TREE_TYPE (ctor));
3878 unsigned HOST_WIDE_INT width = tree_to_uhwi (TYPE_SIZE (eltype));
3879 unsigned HOST_WIDE_INT n = tree_to_uhwi (@1);
3880 unsigned HOST_WIDE_INT idx = tree_to_uhwi (@2);
3881 }
3882 (if (n != 0
3883 && (idx % width) == 0
3884 && (n % width) == 0
3885 && ((idx + n) / width) <= TYPE_VECTOR_SUBPARTS (TREE_TYPE (ctor)))
3886 (with
3887 {
3888 idx = idx / width;
3889 n = n / width;
3890 /* Constructor elements can be subvectors. */
3891 unsigned HOST_WIDE_INT k = 1;
3892 if (CONSTRUCTOR_NELTS (ctor) != 0)
3893 {
3894 tree cons_elem = TREE_TYPE (CONSTRUCTOR_ELT (ctor, 0)->value);
3895 if (TREE_CODE (cons_elem) == VECTOR_TYPE)
3896 k = TYPE_VECTOR_SUBPARTS (cons_elem);
3897 }
3898 }
3899 (switch
3900 /* We keep an exact subset of the constructor elements. */
3901 (if ((idx % k) == 0 && (n % k) == 0)
3902 (if (CONSTRUCTOR_NELTS (ctor) == 0)
3903 { build_constructor (type, NULL); }
3904 (with
3905 {
3906 idx /= k;
3907 n /= k;
3908 }
3909 (if (n == 1)
3910 (if (idx < CONSTRUCTOR_NELTS (ctor))
3911 { CONSTRUCTOR_ELT (ctor, idx)->value; }
3912 { build_zero_cst (type); })
3913 {
3914 vec<constructor_elt, va_gc> *vals;
3915 vec_alloc (vals, n);
3916 for (unsigned i = 0;
3917 i < n && idx + i < CONSTRUCTOR_NELTS (ctor); ++i)
3918 CONSTRUCTOR_APPEND_ELT (vals, NULL_TREE,
3919 CONSTRUCTOR_ELT (ctor, idx + i)->value);
3920 build_constructor (type, vals);
3921 }))))
3922 /* The bitfield references a single constructor element. */
3923 (if (idx + n <= (idx / k + 1) * k)
3924 (switch
3925 (if (CONSTRUCTOR_NELTS (ctor) <= idx / k)
3926 { build_zero_cst (type); })
3927 (if (n == k)
3928 { CONSTRUCTOR_ELT (ctor, idx / k)->value; })
3929 (BIT_FIELD_REF { CONSTRUCTOR_ELT (ctor, idx / k)->value; }
3930 @1 { bitsize_int ((idx % k) * width); })))))))))