618b2ec3da83c6a9a2bcbc91710825144482b8c3
[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. */
1836 (simplify
1837 (view_convert (convert@0 @1))
1838 (if ((INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
1839 && (INTEGRAL_TYPE_P (TREE_TYPE (@1)) || POINTER_TYPE_P (TREE_TYPE (@1)))
1840 && (TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
1841 && (TYPE_SIZE (TREE_TYPE (@0)) == TYPE_SIZE (TREE_TYPE (@1))))
1842 (view_convert @1)))
1843
1844 /* Re-association barriers around constants and other re-association
1845 barriers can be removed. */
1846 (simplify
1847 (paren CONSTANT_CLASS_P@0)
1848 @0)
1849 (simplify
1850 (paren (paren@1 @0))
1851 @1)
1852
1853 /* Handle cases of two conversions in a row. */
1854 (for ocvt (convert float fix_trunc)
1855 (for icvt (convert float)
1856 (simplify
1857 (ocvt (icvt@1 @0))
1858 (with
1859 {
1860 tree inside_type = TREE_TYPE (@0);
1861 tree inter_type = TREE_TYPE (@1);
1862 int inside_int = INTEGRAL_TYPE_P (inside_type);
1863 int inside_ptr = POINTER_TYPE_P (inside_type);
1864 int inside_float = FLOAT_TYPE_P (inside_type);
1865 int inside_vec = VECTOR_TYPE_P (inside_type);
1866 unsigned int inside_prec = TYPE_PRECISION (inside_type);
1867 int inside_unsignedp = TYPE_UNSIGNED (inside_type);
1868 int inter_int = INTEGRAL_TYPE_P (inter_type);
1869 int inter_ptr = POINTER_TYPE_P (inter_type);
1870 int inter_float = FLOAT_TYPE_P (inter_type);
1871 int inter_vec = VECTOR_TYPE_P (inter_type);
1872 unsigned int inter_prec = TYPE_PRECISION (inter_type);
1873 int inter_unsignedp = TYPE_UNSIGNED (inter_type);
1874 int final_int = INTEGRAL_TYPE_P (type);
1875 int final_ptr = POINTER_TYPE_P (type);
1876 int final_float = FLOAT_TYPE_P (type);
1877 int final_vec = VECTOR_TYPE_P (type);
1878 unsigned int final_prec = TYPE_PRECISION (type);
1879 int final_unsignedp = TYPE_UNSIGNED (type);
1880 }
1881 (switch
1882 /* In addition to the cases of two conversions in a row
1883 handled below, if we are converting something to its own
1884 type via an object of identical or wider precision, neither
1885 conversion is needed. */
1886 (if (((GIMPLE && useless_type_conversion_p (type, inside_type))
1887 || (GENERIC
1888 && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (inside_type)))
1889 && (((inter_int || inter_ptr) && final_int)
1890 || (inter_float && final_float))
1891 && inter_prec >= final_prec)
1892 (ocvt @0))
1893
1894 /* Likewise, if the intermediate and initial types are either both
1895 float or both integer, we don't need the middle conversion if the
1896 former is wider than the latter and doesn't change the signedness
1897 (for integers). Avoid this if the final type is a pointer since
1898 then we sometimes need the middle conversion. */
1899 (if (((inter_int && inside_int) || (inter_float && inside_float))
1900 && (final_int || final_float)
1901 && inter_prec >= inside_prec
1902 && (inter_float || inter_unsignedp == inside_unsignedp))
1903 (ocvt @0))
1904
1905 /* If we have a sign-extension of a zero-extended value, we can
1906 replace that by a single zero-extension. Likewise if the
1907 final conversion does not change precision we can drop the
1908 intermediate conversion. */
1909 (if (inside_int && inter_int && final_int
1910 && ((inside_prec < inter_prec && inter_prec < final_prec
1911 && inside_unsignedp && !inter_unsignedp)
1912 || final_prec == inter_prec))
1913 (ocvt @0))
1914
1915 /* Two conversions in a row are not needed unless:
1916 - some conversion is floating-point (overstrict for now), or
1917 - some conversion is a vector (overstrict for now), or
1918 - the intermediate type is narrower than both initial and
1919 final, or
1920 - the intermediate type and innermost type differ in signedness,
1921 and the outermost type is wider than the intermediate, or
1922 - the initial type is a pointer type and the precisions of the
1923 intermediate and final types differ, or
1924 - the final type is a pointer type and the precisions of the
1925 initial and intermediate types differ. */
1926 (if (! inside_float && ! inter_float && ! final_float
1927 && ! inside_vec && ! inter_vec && ! final_vec
1928 && (inter_prec >= inside_prec || inter_prec >= final_prec)
1929 && ! (inside_int && inter_int
1930 && inter_unsignedp != inside_unsignedp
1931 && inter_prec < final_prec)
1932 && ((inter_unsignedp && inter_prec > inside_prec)
1933 == (final_unsignedp && final_prec > inter_prec))
1934 && ! (inside_ptr && inter_prec != final_prec)
1935 && ! (final_ptr && inside_prec != inter_prec))
1936 (ocvt @0))
1937
1938 /* A truncation to an unsigned type (a zero-extension) should be
1939 canonicalized as bitwise and of a mask. */
1940 (if (GIMPLE /* PR70366: doing this in GENERIC breaks -Wconversion. */
1941 && final_int && inter_int && inside_int
1942 && final_prec == inside_prec
1943 && final_prec > inter_prec
1944 && inter_unsignedp)
1945 (convert (bit_and @0 { wide_int_to_tree
1946 (inside_type,
1947 wi::mask (inter_prec, false,
1948 TYPE_PRECISION (inside_type))); })))
1949
1950 /* If we are converting an integer to a floating-point that can
1951 represent it exactly and back to an integer, we can skip the
1952 floating-point conversion. */
1953 (if (GIMPLE /* PR66211 */
1954 && inside_int && inter_float && final_int &&
1955 (unsigned) significand_size (TYPE_MODE (inter_type))
1956 >= inside_prec - !inside_unsignedp)
1957 (convert @0)))))))
1958
1959 /* If we have a narrowing conversion to an integral type that is fed by a
1960 BIT_AND_EXPR, we might be able to remove the BIT_AND_EXPR if it merely
1961 masks off bits outside the final type (and nothing else). */
1962 (simplify
1963 (convert (bit_and @0 INTEGER_CST@1))
1964 (if (INTEGRAL_TYPE_P (type)
1965 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
1966 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))
1967 && operand_equal_p (@1, build_low_bits_mask (TREE_TYPE (@1),
1968 TYPE_PRECISION (type)), 0))
1969 (convert @0)))
1970
1971
1972 /* (X /[ex] A) * A -> X. */
1973 (simplify
1974 (mult (convert1? (exact_div @0 @@1)) (convert2? @1))
1975 (convert @0))
1976
1977 /* Canonicalization of binary operations. */
1978
1979 /* Convert X + -C into X - C. */
1980 (simplify
1981 (plus @0 REAL_CST@1)
1982 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
1983 (with { tree tem = const_unop (NEGATE_EXPR, type, @1); }
1984 (if (!TREE_OVERFLOW (tem) || !flag_trapping_math)
1985 (minus @0 { tem; })))))
1986
1987 /* Convert x+x into x*2. */
1988 (simplify
1989 (plus @0 @0)
1990 (if (SCALAR_FLOAT_TYPE_P (type))
1991 (mult @0 { build_real (type, dconst2); })
1992 (if (INTEGRAL_TYPE_P (type))
1993 (mult @0 { build_int_cst (type, 2); }))))
1994
1995 (simplify
1996 (minus integer_zerop @1)
1997 (negate @1))
1998
1999 /* (ARG0 - ARG1) is the same as (-ARG1 + ARG0). So check whether
2000 ARG0 is zero and X + ARG0 reduces to X, since that would mean
2001 (-ARG1 + ARG0) reduces to -ARG1. */
2002 (simplify
2003 (minus real_zerop@0 @1)
2004 (if (fold_real_zero_addition_p (type, @0, 0))
2005 (negate @1)))
2006
2007 /* Transform x * -1 into -x. */
2008 (simplify
2009 (mult @0 integer_minus_onep)
2010 (negate @0))
2011
2012 /* True if we can easily extract the real and imaginary parts of a complex
2013 number. */
2014 (match compositional_complex
2015 (convert? (complex @0 @1)))
2016
2017 /* COMPLEX_EXPR and REALPART/IMAGPART_EXPR cancellations. */
2018 (simplify
2019 (complex (realpart @0) (imagpart @0))
2020 @0)
2021 (simplify
2022 (realpart (complex @0 @1))
2023 @0)
2024 (simplify
2025 (imagpart (complex @0 @1))
2026 @1)
2027
2028 /* Sometimes we only care about half of a complex expression. */
2029 (simplify
2030 (realpart (convert?:s (conj:s @0)))
2031 (convert (realpart @0)))
2032 (simplify
2033 (imagpart (convert?:s (conj:s @0)))
2034 (convert (negate (imagpart @0))))
2035 (for part (realpart imagpart)
2036 (for op (plus minus)
2037 (simplify
2038 (part (convert?:s@2 (op:s @0 @1)))
2039 (convert (op (part @0) (part @1))))))
2040 (simplify
2041 (realpart (convert?:s (CEXPI:s @0)))
2042 (convert (COS @0)))
2043 (simplify
2044 (imagpart (convert?:s (CEXPI:s @0)))
2045 (convert (SIN @0)))
2046
2047 /* conj(conj(x)) -> x */
2048 (simplify
2049 (conj (convert? (conj @0)))
2050 (if (tree_nop_conversion_p (TREE_TYPE (@0), type))
2051 (convert @0)))
2052
2053 /* conj({x,y}) -> {x,-y} */
2054 (simplify
2055 (conj (convert?:s (complex:s @0 @1)))
2056 (with { tree itype = TREE_TYPE (type); }
2057 (complex (convert:itype @0) (negate (convert:itype @1)))))
2058
2059 /* BSWAP simplifications, transforms checked by gcc.dg/builtin-bswap-8.c. */
2060 (for bswap (BUILT_IN_BSWAP16 BUILT_IN_BSWAP32 BUILT_IN_BSWAP64)
2061 (simplify
2062 (bswap (bswap @0))
2063 @0)
2064 (simplify
2065 (bswap (bit_not (bswap @0)))
2066 (bit_not @0))
2067 (for bitop (bit_xor bit_ior bit_and)
2068 (simplify
2069 (bswap (bitop:c (bswap @0) @1))
2070 (bitop @0 (bswap @1)))))
2071
2072
2073 /* Combine COND_EXPRs and VEC_COND_EXPRs. */
2074
2075 /* Simplify constant conditions.
2076 Only optimize constant conditions when the selected branch
2077 has the same type as the COND_EXPR. This avoids optimizing
2078 away "c ? x : throw", where the throw has a void type.
2079 Note that we cannot throw away the fold-const.c variant nor
2080 this one as we depend on doing this transform before possibly
2081 A ? B : B -> B triggers and the fold-const.c one can optimize
2082 0 ? A : B to B even if A has side-effects. Something
2083 genmatch cannot handle. */
2084 (simplify
2085 (cond INTEGER_CST@0 @1 @2)
2086 (if (integer_zerop (@0))
2087 (if (!VOID_TYPE_P (TREE_TYPE (@2)) || VOID_TYPE_P (type))
2088 @2)
2089 (if (!VOID_TYPE_P (TREE_TYPE (@1)) || VOID_TYPE_P (type))
2090 @1)))
2091 (simplify
2092 (vec_cond VECTOR_CST@0 @1 @2)
2093 (if (integer_all_onesp (@0))
2094 @1
2095 (if (integer_zerop (@0))
2096 @2)))
2097
2098 /* Simplification moved from fold_cond_expr_with_comparison. It may also
2099 be extended. */
2100 /* This pattern implements two kinds simplification:
2101
2102 Case 1)
2103 (cond (cmp (convert1? x) c1) (convert2? x) c2) -> (minmax (x c)) if:
2104 1) Conversions are type widening from smaller type.
2105 2) Const c1 equals to c2 after canonicalizing comparison.
2106 3) Comparison has tree code LT, LE, GT or GE.
2107 This specific pattern is needed when (cmp (convert x) c) may not
2108 be simplified by comparison patterns because of multiple uses of
2109 x. It also makes sense here because simplifying across multiple
2110 referred var is always benefitial for complicated cases.
2111
2112 Case 2)
2113 (cond (eq (convert1? x) c1) (convert2? x) c2) -> (cond (eq x c1) c1 c2). */
2114 (for cmp (lt le gt ge eq)
2115 (simplify
2116 (cond (cmp (convert1? @1) INTEGER_CST@3) (convert2? @1) INTEGER_CST@2)
2117 (with
2118 {
2119 tree from_type = TREE_TYPE (@1);
2120 tree c1_type = TREE_TYPE (@3), c2_type = TREE_TYPE (@2);
2121 enum tree_code code = ERROR_MARK;
2122
2123 if (INTEGRAL_TYPE_P (from_type)
2124 && int_fits_type_p (@2, from_type)
2125 && (types_match (c1_type, from_type)
2126 || (TYPE_PRECISION (c1_type) > TYPE_PRECISION (from_type)
2127 && (TYPE_UNSIGNED (from_type)
2128 || TYPE_SIGN (c1_type) == TYPE_SIGN (from_type))))
2129 && (types_match (c2_type, from_type)
2130 || (TYPE_PRECISION (c2_type) > TYPE_PRECISION (from_type)
2131 && (TYPE_UNSIGNED (from_type)
2132 || TYPE_SIGN (c2_type) == TYPE_SIGN (from_type)))))
2133 {
2134 if (cmp != EQ_EXPR)
2135 {
2136 if (wi::to_widest (@3) == (wi::to_widest (@2) - 1))
2137 {
2138 /* X <= Y - 1 equals to X < Y. */
2139 if (cmp == LE_EXPR)
2140 code = LT_EXPR;
2141 /* X > Y - 1 equals to X >= Y. */
2142 if (cmp == GT_EXPR)
2143 code = GE_EXPR;
2144 }
2145 if (wi::to_widest (@3) == (wi::to_widest (@2) + 1))
2146 {
2147 /* X < Y + 1 equals to X <= Y. */
2148 if (cmp == LT_EXPR)
2149 code = LE_EXPR;
2150 /* X >= Y + 1 equals to X > Y. */
2151 if (cmp == GE_EXPR)
2152 code = GT_EXPR;
2153 }
2154 if (code != ERROR_MARK
2155 || wi::to_widest (@2) == wi::to_widest (@3))
2156 {
2157 if (cmp == LT_EXPR || cmp == LE_EXPR)
2158 code = MIN_EXPR;
2159 if (cmp == GT_EXPR || cmp == GE_EXPR)
2160 code = MAX_EXPR;
2161 }
2162 }
2163 /* Can do A == C1 ? A : C2 -> A == C1 ? C1 : C2? */
2164 else if (int_fits_type_p (@3, from_type))
2165 code = EQ_EXPR;
2166 }
2167 }
2168 (if (code == MAX_EXPR)
2169 (convert (max @1 (convert @2)))
2170 (if (code == MIN_EXPR)
2171 (convert (min @1 (convert @2)))
2172 (if (code == EQ_EXPR)
2173 (convert (cond (eq @1 (convert @3))
2174 (convert:from_type @3) (convert:from_type @2)))))))))
2175
2176 /* (cond (cmp (convert? x) c1) (op x c2) c3) -> (op (minmax x c1) c2) if:
2177
2178 1) OP is PLUS or MINUS.
2179 2) CMP is LT, LE, GT or GE.
2180 3) C3 == (C1 op C2), and computation doesn't have undefined behavior.
2181
2182 This pattern also handles special cases like:
2183
2184 A) Operand x is a unsigned to signed type conversion and c1 is
2185 integer zero. In this case,
2186 (signed type)x < 0 <=> x > MAX_VAL(signed type)
2187 (signed type)x >= 0 <=> x <= MAX_VAL(signed type)
2188 B) Const c1 may not equal to (C3 op' C2). In this case we also
2189 check equality for (c1+1) and (c1-1) by adjusting comparison
2190 code.
2191
2192 TODO: Though signed type is handled by this pattern, it cannot be
2193 simplified at the moment because C standard requires additional
2194 type promotion. In order to match&simplify it here, the IR needs
2195 to be cleaned up by other optimizers, i.e, VRP. */
2196 (for op (plus minus)
2197 (for cmp (lt le gt ge)
2198 (simplify
2199 (cond (cmp (convert? @X) INTEGER_CST@1) (op @X INTEGER_CST@2) INTEGER_CST@3)
2200 (with { tree from_type = TREE_TYPE (@X), to_type = TREE_TYPE (@1); }
2201 (if (types_match (from_type, to_type)
2202 /* Check if it is special case A). */
2203 || (TYPE_UNSIGNED (from_type)
2204 && !TYPE_UNSIGNED (to_type)
2205 && TYPE_PRECISION (from_type) == TYPE_PRECISION (to_type)
2206 && integer_zerop (@1)
2207 && (cmp == LT_EXPR || cmp == GE_EXPR)))
2208 (with
2209 {
2210 bool overflow = false;
2211 enum tree_code code, cmp_code = cmp;
2212 wide_int real_c1, c1 = @1, c2 = @2, c3 = @3;
2213 signop sgn = TYPE_SIGN (from_type);
2214
2215 /* Handle special case A), given x of unsigned type:
2216 ((signed type)x < 0) <=> (x > MAX_VAL(signed type))
2217 ((signed type)x >= 0) <=> (x <= MAX_VAL(signed type)) */
2218 if (!types_match (from_type, to_type))
2219 {
2220 if (cmp_code == LT_EXPR)
2221 cmp_code = GT_EXPR;
2222 if (cmp_code == GE_EXPR)
2223 cmp_code = LE_EXPR;
2224 c1 = wi::max_value (to_type);
2225 }
2226 /* To simplify this pattern, we require c3 = (c1 op c2). Here we
2227 compute (c3 op' c2) and check if it equals to c1 with op' being
2228 the inverted operator of op. Make sure overflow doesn't happen
2229 if it is undefined. */
2230 if (op == PLUS_EXPR)
2231 real_c1 = wi::sub (c3, c2, sgn, &overflow);
2232 else
2233 real_c1 = wi::add (c3, c2, sgn, &overflow);
2234
2235 code = cmp_code;
2236 if (!overflow || !TYPE_OVERFLOW_UNDEFINED (from_type))
2237 {
2238 /* Check if c1 equals to real_c1. Boundary condition is handled
2239 by adjusting comparison operation if necessary. */
2240 if (!wi::cmp (wi::sub (real_c1, 1, sgn, &overflow), c1, sgn)
2241 && !overflow)
2242 {
2243 /* X <= Y - 1 equals to X < Y. */
2244 if (cmp_code == LE_EXPR)
2245 code = LT_EXPR;
2246 /* X > Y - 1 equals to X >= Y. */
2247 if (cmp_code == GT_EXPR)
2248 code = GE_EXPR;
2249 }
2250 if (!wi::cmp (wi::add (real_c1, 1, sgn, &overflow), c1, sgn)
2251 && !overflow)
2252 {
2253 /* X < Y + 1 equals to X <= Y. */
2254 if (cmp_code == LT_EXPR)
2255 code = LE_EXPR;
2256 /* X >= Y + 1 equals to X > Y. */
2257 if (cmp_code == GE_EXPR)
2258 code = GT_EXPR;
2259 }
2260 if (code != cmp_code || !wi::cmp (real_c1, c1, sgn))
2261 {
2262 if (cmp_code == LT_EXPR || cmp_code == LE_EXPR)
2263 code = MIN_EXPR;
2264 if (cmp_code == GT_EXPR || cmp_code == GE_EXPR)
2265 code = MAX_EXPR;
2266 }
2267 }
2268 }
2269 (if (code == MAX_EXPR)
2270 (op (max @X { wide_int_to_tree (from_type, real_c1); })
2271 { wide_int_to_tree (from_type, c2); })
2272 (if (code == MIN_EXPR)
2273 (op (min @X { wide_int_to_tree (from_type, real_c1); })
2274 { wide_int_to_tree (from_type, c2); })))))))))
2275
2276 (for cnd (cond vec_cond)
2277 /* A ? B : (A ? X : C) -> A ? B : C. */
2278 (simplify
2279 (cnd @0 (cnd @0 @1 @2) @3)
2280 (cnd @0 @1 @3))
2281 (simplify
2282 (cnd @0 @1 (cnd @0 @2 @3))
2283 (cnd @0 @1 @3))
2284 /* A ? B : (!A ? C : X) -> A ? B : C. */
2285 /* ??? This matches embedded conditions open-coded because genmatch
2286 would generate matching code for conditions in separate stmts only.
2287 The following is still important to merge then and else arm cases
2288 from if-conversion. */
2289 (simplify
2290 (cnd @0 @1 (cnd @2 @3 @4))
2291 (if (COMPARISON_CLASS_P (@0)
2292 && COMPARISON_CLASS_P (@2)
2293 && invert_tree_comparison
2294 (TREE_CODE (@0), HONOR_NANS (TREE_OPERAND (@0, 0))) == TREE_CODE (@2)
2295 && operand_equal_p (TREE_OPERAND (@0, 0), TREE_OPERAND (@2, 0), 0)
2296 && operand_equal_p (TREE_OPERAND (@0, 1), TREE_OPERAND (@2, 1), 0))
2297 (cnd @0 @1 @3)))
2298 (simplify
2299 (cnd @0 (cnd @1 @2 @3) @4)
2300 (if (COMPARISON_CLASS_P (@0)
2301 && COMPARISON_CLASS_P (@1)
2302 && invert_tree_comparison
2303 (TREE_CODE (@0), HONOR_NANS (TREE_OPERAND (@0, 0))) == TREE_CODE (@1)
2304 && operand_equal_p (TREE_OPERAND (@0, 0), TREE_OPERAND (@1, 0), 0)
2305 && operand_equal_p (TREE_OPERAND (@0, 1), TREE_OPERAND (@1, 1), 0))
2306 (cnd @0 @3 @4)))
2307
2308 /* A ? B : B -> B. */
2309 (simplify
2310 (cnd @0 @1 @1)
2311 @1)
2312
2313 /* !A ? B : C -> A ? C : B. */
2314 (simplify
2315 (cnd (logical_inverted_value truth_valued_p@0) @1 @2)
2316 (cnd @0 @2 @1)))
2317
2318 /* A + (B vcmp C ? 1 : 0) -> A - (B vcmp C ? -1 : 0), since vector comparisons
2319 return all -1 or all 0 results. */
2320 /* ??? We could instead convert all instances of the vec_cond to negate,
2321 but that isn't necessarily a win on its own. */
2322 (simplify
2323 (plus:c @3 (view_convert? (vec_cond:s @0 integer_each_onep@1 integer_zerop@2)))
2324 (if (VECTOR_TYPE_P (type)
2325 && TYPE_VECTOR_SUBPARTS (type) == TYPE_VECTOR_SUBPARTS (TREE_TYPE (@1))
2326 && (TYPE_MODE (TREE_TYPE (type))
2327 == TYPE_MODE (TREE_TYPE (TREE_TYPE (@1)))))
2328 (minus @3 (view_convert (vec_cond @0 (negate @1) @2)))))
2329
2330 /* ... likewise A - (B vcmp C ? 1 : 0) -> A + (B vcmp C ? -1 : 0). */
2331 (simplify
2332 (minus @3 (view_convert? (vec_cond:s @0 integer_each_onep@1 integer_zerop@2)))
2333 (if (VECTOR_TYPE_P (type)
2334 && TYPE_VECTOR_SUBPARTS (type) == TYPE_VECTOR_SUBPARTS (TREE_TYPE (@1))
2335 && (TYPE_MODE (TREE_TYPE (type))
2336 == TYPE_MODE (TREE_TYPE (TREE_TYPE (@1)))))
2337 (plus @3 (view_convert (vec_cond @0 (negate @1) @2)))))
2338
2339
2340 /* Simplifications of comparisons. */
2341
2342 /* See if we can reduce the magnitude of a constant involved in a
2343 comparison by changing the comparison code. This is a canonicalization
2344 formerly done by maybe_canonicalize_comparison_1. */
2345 (for cmp (le gt)
2346 acmp (lt ge)
2347 (simplify
2348 (cmp @0 INTEGER_CST@1)
2349 (if (tree_int_cst_sgn (@1) == -1)
2350 (acmp @0 { wide_int_to_tree (TREE_TYPE (@1), wi::add (@1, 1)); }))))
2351 (for cmp (ge lt)
2352 acmp (gt le)
2353 (simplify
2354 (cmp @0 INTEGER_CST@1)
2355 (if (tree_int_cst_sgn (@1) == 1)
2356 (acmp @0 { wide_int_to_tree (TREE_TYPE (@1), wi::sub (@1, 1)); }))))
2357
2358
2359 /* We can simplify a logical negation of a comparison to the
2360 inverted comparison. As we cannot compute an expression
2361 operator using invert_tree_comparison we have to simulate
2362 that with expression code iteration. */
2363 (for cmp (tcc_comparison)
2364 icmp (inverted_tcc_comparison)
2365 ncmp (inverted_tcc_comparison_with_nans)
2366 /* Ideally we'd like to combine the following two patterns
2367 and handle some more cases by using
2368 (logical_inverted_value (cmp @0 @1))
2369 here but for that genmatch would need to "inline" that.
2370 For now implement what forward_propagate_comparison did. */
2371 (simplify
2372 (bit_not (cmp @0 @1))
2373 (if (VECTOR_TYPE_P (type)
2374 || (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1))
2375 /* Comparison inversion may be impossible for trapping math,
2376 invert_tree_comparison will tell us. But we can't use
2377 a computed operator in the replacement tree thus we have
2378 to play the trick below. */
2379 (with { enum tree_code ic = invert_tree_comparison
2380 (cmp, HONOR_NANS (@0)); }
2381 (if (ic == icmp)
2382 (icmp @0 @1)
2383 (if (ic == ncmp)
2384 (ncmp @0 @1))))))
2385 (simplify
2386 (bit_xor (cmp @0 @1) integer_truep)
2387 (with { enum tree_code ic = invert_tree_comparison
2388 (cmp, HONOR_NANS (@0)); }
2389 (if (ic == icmp)
2390 (icmp @0 @1)
2391 (if (ic == ncmp)
2392 (ncmp @0 @1))))))
2393
2394 /* Transform comparisons of the form X - Y CMP 0 to X CMP Y.
2395 ??? The transformation is valid for the other operators if overflow
2396 is undefined for the type, but performing it here badly interacts
2397 with the transformation in fold_cond_expr_with_comparison which
2398 attempts to synthetize ABS_EXPR. */
2399 (for cmp (eq ne)
2400 (simplify
2401 (cmp (minus@2 @0 @1) integer_zerop)
2402 (if (single_use (@2))
2403 (cmp @0 @1))))
2404
2405 /* Transform comparisons of the form X * C1 CMP 0 to X CMP 0 in the
2406 signed arithmetic case. That form is created by the compiler
2407 often enough for folding it to be of value. One example is in
2408 computing loop trip counts after Operator Strength Reduction. */
2409 (for cmp (simple_comparison)
2410 scmp (swapped_simple_comparison)
2411 (simplify
2412 (cmp (mult@3 @0 INTEGER_CST@1) integer_zerop@2)
2413 /* Handle unfolded multiplication by zero. */
2414 (if (integer_zerop (@1))
2415 (cmp @1 @2)
2416 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2417 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2418 && single_use (@3))
2419 /* If @1 is negative we swap the sense of the comparison. */
2420 (if (tree_int_cst_sgn (@1) < 0)
2421 (scmp @0 @2)
2422 (cmp @0 @2))))))
2423
2424 /* Simplify comparison of something with itself. For IEEE
2425 floating-point, we can only do some of these simplifications. */
2426 (for cmp (eq ge le)
2427 (simplify
2428 (cmp @0 @0)
2429 (if (! FLOAT_TYPE_P (TREE_TYPE (@0))
2430 || ! HONOR_NANS (@0))
2431 { constant_boolean_node (true, type); }
2432 (if (cmp != EQ_EXPR)
2433 (eq @0 @0)))))
2434 (for cmp (ne gt lt)
2435 (simplify
2436 (cmp @0 @0)
2437 (if (cmp != NE_EXPR
2438 || ! FLOAT_TYPE_P (TREE_TYPE (@0))
2439 || ! HONOR_NANS (@0))
2440 { constant_boolean_node (false, type); })))
2441 (for cmp (unle unge uneq)
2442 (simplify
2443 (cmp @0 @0)
2444 { constant_boolean_node (true, type); }))
2445 (for cmp (unlt ungt)
2446 (simplify
2447 (cmp @0 @0)
2448 (unordered @0 @0)))
2449 (simplify
2450 (ltgt @0 @0)
2451 (if (!flag_trapping_math)
2452 { constant_boolean_node (false, type); }))
2453
2454 /* Fold ~X op ~Y as Y op X. */
2455 (for cmp (simple_comparison)
2456 (simplify
2457 (cmp (bit_not@2 @0) (bit_not@3 @1))
2458 (if (single_use (@2) && single_use (@3))
2459 (cmp @1 @0))))
2460
2461 /* Fold ~X op C as X op' ~C, where op' is the swapped comparison. */
2462 (for cmp (simple_comparison)
2463 scmp (swapped_simple_comparison)
2464 (simplify
2465 (cmp (bit_not@2 @0) CONSTANT_CLASS_P@1)
2466 (if (single_use (@2)
2467 && (TREE_CODE (@1) == INTEGER_CST || TREE_CODE (@1) == VECTOR_CST))
2468 (scmp @0 (bit_not @1)))))
2469
2470 (for cmp (simple_comparison)
2471 /* Fold (double)float1 CMP (double)float2 into float1 CMP float2. */
2472 (simplify
2473 (cmp (convert@2 @0) (convert? @1))
2474 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
2475 && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@2))
2476 == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0)))
2477 && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@2))
2478 == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1))))
2479 (with
2480 {
2481 tree type1 = TREE_TYPE (@1);
2482 if (TREE_CODE (@1) == REAL_CST && !DECIMAL_FLOAT_TYPE_P (type1))
2483 {
2484 REAL_VALUE_TYPE orig = TREE_REAL_CST (@1);
2485 if (TYPE_PRECISION (type1) > TYPE_PRECISION (float_type_node)
2486 && exact_real_truncate (TYPE_MODE (float_type_node), &orig))
2487 type1 = float_type_node;
2488 if (TYPE_PRECISION (type1) > TYPE_PRECISION (double_type_node)
2489 && exact_real_truncate (TYPE_MODE (double_type_node), &orig))
2490 type1 = double_type_node;
2491 }
2492 tree newtype
2493 = (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type1)
2494 ? TREE_TYPE (@0) : type1);
2495 }
2496 (if (TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (newtype))
2497 (cmp (convert:newtype @0) (convert:newtype @1))))))
2498
2499 (simplify
2500 (cmp @0 REAL_CST@1)
2501 /* IEEE doesn't distinguish +0 and -0 in comparisons. */
2502 (switch
2503 /* a CMP (-0) -> a CMP 0 */
2504 (if (REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@1)))
2505 (cmp @0 { build_real (TREE_TYPE (@1), dconst0); }))
2506 /* x != NaN is always true, other ops are always false. */
2507 (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
2508 && ! HONOR_SNANS (@1))
2509 { constant_boolean_node (cmp == NE_EXPR, type); })
2510 /* Fold comparisons against infinity. */
2511 (if (REAL_VALUE_ISINF (TREE_REAL_CST (@1))
2512 && MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (@1))))
2513 (with
2514 {
2515 REAL_VALUE_TYPE max;
2516 enum tree_code code = cmp;
2517 bool neg = REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1));
2518 if (neg)
2519 code = swap_tree_comparison (code);
2520 }
2521 (switch
2522 /* x > +Inf is always false, if with ignore sNANs. */
2523 (if (code == GT_EXPR
2524 && ! HONOR_SNANS (@0))
2525 { constant_boolean_node (false, type); })
2526 (if (code == LE_EXPR)
2527 /* x <= +Inf is always true, if we don't case about NaNs. */
2528 (if (! HONOR_NANS (@0))
2529 { constant_boolean_node (true, type); }
2530 /* x <= +Inf is the same as x == x, i.e. !isnan(x). */
2531 (eq @0 @0)))
2532 /* x == +Inf and x >= +Inf are always equal to x > DBL_MAX. */
2533 (if (code == EQ_EXPR || code == GE_EXPR)
2534 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
2535 (if (neg)
2536 (lt @0 { build_real (TREE_TYPE (@0), max); })
2537 (gt @0 { build_real (TREE_TYPE (@0), max); }))))
2538 /* x < +Inf is always equal to x <= DBL_MAX. */
2539 (if (code == LT_EXPR)
2540 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
2541 (if (neg)
2542 (ge @0 { build_real (TREE_TYPE (@0), max); })
2543 (le @0 { build_real (TREE_TYPE (@0), max); }))))
2544 /* x != +Inf is always equal to !(x > DBL_MAX). */
2545 (if (code == NE_EXPR)
2546 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
2547 (if (! HONOR_NANS (@0))
2548 (if (neg)
2549 (ge @0 { build_real (TREE_TYPE (@0), max); })
2550 (le @0 { build_real (TREE_TYPE (@0), max); }))
2551 (if (neg)
2552 (bit_xor (lt @0 { build_real (TREE_TYPE (@0), max); })
2553 { build_one_cst (type); })
2554 (bit_xor (gt @0 { build_real (TREE_TYPE (@0), max); })
2555 { build_one_cst (type); }))))))))))
2556
2557 /* If this is a comparison of a real constant with a PLUS_EXPR
2558 or a MINUS_EXPR of a real constant, we can convert it into a
2559 comparison with a revised real constant as long as no overflow
2560 occurs when unsafe_math_optimizations are enabled. */
2561 (if (flag_unsafe_math_optimizations)
2562 (for op (plus minus)
2563 (simplify
2564 (cmp (op @0 REAL_CST@1) REAL_CST@2)
2565 (with
2566 {
2567 tree tem = const_binop (op == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR,
2568 TREE_TYPE (@1), @2, @1);
2569 }
2570 (if (tem && !TREE_OVERFLOW (tem))
2571 (cmp @0 { tem; }))))))
2572
2573 /* Likewise, we can simplify a comparison of a real constant with
2574 a MINUS_EXPR whose first operand is also a real constant, i.e.
2575 (c1 - x) < c2 becomes x > c1-c2. Reordering is allowed on
2576 floating-point types only if -fassociative-math is set. */
2577 (if (flag_associative_math)
2578 (simplify
2579 (cmp (minus REAL_CST@0 @1) REAL_CST@2)
2580 (with { tree tem = const_binop (MINUS_EXPR, TREE_TYPE (@1), @0, @2); }
2581 (if (tem && !TREE_OVERFLOW (tem))
2582 (cmp { tem; } @1)))))
2583
2584 /* Fold comparisons against built-in math functions. */
2585 (if (flag_unsafe_math_optimizations
2586 && ! flag_errno_math)
2587 (for sq (SQRT)
2588 (simplify
2589 (cmp (sq @0) REAL_CST@1)
2590 (switch
2591 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
2592 (switch
2593 /* sqrt(x) < y is always false, if y is negative. */
2594 (if (cmp == EQ_EXPR || cmp == LT_EXPR || cmp == LE_EXPR)
2595 { constant_boolean_node (false, type); })
2596 /* sqrt(x) > y is always true, if y is negative and we
2597 don't care about NaNs, i.e. negative values of x. */
2598 (if (cmp == NE_EXPR || !HONOR_NANS (@0))
2599 { constant_boolean_node (true, type); })
2600 /* sqrt(x) > y is the same as x >= 0, if y is negative. */
2601 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })))
2602 (if (real_equal (TREE_REAL_CST_PTR (@1), &dconst0))
2603 (switch
2604 /* sqrt(x) < 0 is always false. */
2605 (if (cmp == LT_EXPR)
2606 { constant_boolean_node (false, type); })
2607 /* sqrt(x) >= 0 is always true if we don't care about NaNs. */
2608 (if (cmp == GE_EXPR && !HONOR_NANS (@0))
2609 { constant_boolean_node (true, type); })
2610 /* sqrt(x) <= 0 -> x == 0. */
2611 (if (cmp == LE_EXPR)
2612 (eq @0 @1))
2613 /* Otherwise sqrt(x) cmp 0 -> x cmp 0. Here cmp can be >=, >,
2614 == or !=. In the last case:
2615
2616 (sqrt(x) != 0) == (NaN != 0) == true == (x != 0)
2617
2618 if x is negative or NaN. Due to -funsafe-math-optimizations,
2619 the results for other x follow from natural arithmetic. */
2620 (cmp @0 @1)))
2621 (if (cmp == GT_EXPR || cmp == GE_EXPR)
2622 (with
2623 {
2624 REAL_VALUE_TYPE c2;
2625 real_arithmetic (&c2, MULT_EXPR,
2626 &TREE_REAL_CST (@1), &TREE_REAL_CST (@1));
2627 real_convert (&c2, TYPE_MODE (TREE_TYPE (@0)), &c2);
2628 }
2629 (if (REAL_VALUE_ISINF (c2))
2630 /* sqrt(x) > y is x == +Inf, when y is very large. */
2631 (if (HONOR_INFINITIES (@0))
2632 (eq @0 { build_real (TREE_TYPE (@0), c2); })
2633 { constant_boolean_node (false, type); })
2634 /* sqrt(x) > c is the same as x > c*c. */
2635 (cmp @0 { build_real (TREE_TYPE (@0), c2); }))))
2636 (if (cmp == LT_EXPR || cmp == LE_EXPR)
2637 (with
2638 {
2639 REAL_VALUE_TYPE c2;
2640 real_arithmetic (&c2, MULT_EXPR,
2641 &TREE_REAL_CST (@1), &TREE_REAL_CST (@1));
2642 real_convert (&c2, TYPE_MODE (TREE_TYPE (@0)), &c2);
2643 }
2644 (if (REAL_VALUE_ISINF (c2))
2645 (switch
2646 /* sqrt(x) < y is always true, when y is a very large
2647 value and we don't care about NaNs or Infinities. */
2648 (if (! HONOR_NANS (@0) && ! HONOR_INFINITIES (@0))
2649 { constant_boolean_node (true, type); })
2650 /* sqrt(x) < y is x != +Inf when y is very large and we
2651 don't care about NaNs. */
2652 (if (! HONOR_NANS (@0))
2653 (ne @0 { build_real (TREE_TYPE (@0), c2); }))
2654 /* sqrt(x) < y is x >= 0 when y is very large and we
2655 don't care about Infinities. */
2656 (if (! HONOR_INFINITIES (@0))
2657 (ge @0 { build_real (TREE_TYPE (@0), dconst0); }))
2658 /* sqrt(x) < y is x >= 0 && x != +Inf, when y is large. */
2659 (if (GENERIC)
2660 (truth_andif
2661 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
2662 (ne @0 { build_real (TREE_TYPE (@0), c2); }))))
2663 /* sqrt(x) < c is the same as x < c*c, if we ignore NaNs. */
2664 (if (! HONOR_NANS (@0))
2665 (cmp @0 { build_real (TREE_TYPE (@0), c2); })
2666 /* sqrt(x) < c is the same as x >= 0 && x < c*c. */
2667 (if (GENERIC)
2668 (truth_andif
2669 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
2670 (cmp @0 { build_real (TREE_TYPE (@0), c2); })))))))))
2671 /* Transform sqrt(x) cmp sqrt(y) -> x cmp y. */
2672 (simplify
2673 (cmp (sq @0) (sq @1))
2674 (if (! HONOR_NANS (@0))
2675 (cmp @0 @1))))))
2676
2677 /* Fold A /[ex] B CMP C to A CMP B * C. */
2678 (for cmp (eq ne)
2679 (simplify
2680 (cmp (exact_div @0 @1) INTEGER_CST@2)
2681 (if (!integer_zerop (@1))
2682 (if (wi::eq_p (@2, 0))
2683 (cmp @0 @2)
2684 (if (TREE_CODE (@1) == INTEGER_CST)
2685 (with
2686 {
2687 bool ovf;
2688 wide_int prod = wi::mul (@2, @1, TYPE_SIGN (TREE_TYPE (@1)), &ovf);
2689 }
2690 (if (ovf)
2691 { constant_boolean_node (cmp == NE_EXPR, type); }
2692 (cmp @0 { wide_int_to_tree (TREE_TYPE (@0), prod); }))))))))
2693 (for cmp (lt le gt ge)
2694 (simplify
2695 (cmp (exact_div @0 INTEGER_CST@1) INTEGER_CST@2)
2696 (if (wi::gt_p (@1, 0, TYPE_SIGN (TREE_TYPE (@1))))
2697 (with
2698 {
2699 bool ovf;
2700 wide_int prod = wi::mul (@2, @1, TYPE_SIGN (TREE_TYPE (@1)), &ovf);
2701 }
2702 (if (ovf)
2703 { constant_boolean_node (wi::lt_p (@2, 0, TYPE_SIGN (TREE_TYPE (@2)))
2704 != (cmp == LT_EXPR || cmp == LE_EXPR), type); }
2705 (cmp @0 { wide_int_to_tree (TREE_TYPE (@0), prod); }))))))
2706
2707 /* Unordered tests if either argument is a NaN. */
2708 (simplify
2709 (bit_ior (unordered @0 @0) (unordered @1 @1))
2710 (if (types_match (@0, @1))
2711 (unordered @0 @1)))
2712 (simplify
2713 (bit_and (ordered @0 @0) (ordered @1 @1))
2714 (if (types_match (@0, @1))
2715 (ordered @0 @1)))
2716 (simplify
2717 (bit_ior:c (unordered @0 @0) (unordered:c@2 @0 @1))
2718 @2)
2719 (simplify
2720 (bit_and:c (ordered @0 @0) (ordered:c@2 @0 @1))
2721 @2)
2722
2723 /* Simple range test simplifications. */
2724 /* A < B || A >= B -> true. */
2725 (for test1 (lt le le le ne ge)
2726 test2 (ge gt ge ne eq ne)
2727 (simplify
2728 (bit_ior:c (test1 @0 @1) (test2 @0 @1))
2729 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2730 || VECTOR_INTEGER_TYPE_P (TREE_TYPE (@0)))
2731 { constant_boolean_node (true, type); })))
2732 /* A < B && A >= B -> false. */
2733 (for test1 (lt lt lt le ne eq)
2734 test2 (ge gt eq gt eq gt)
2735 (simplify
2736 (bit_and:c (test1 @0 @1) (test2 @0 @1))
2737 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2738 || VECTOR_INTEGER_TYPE_P (TREE_TYPE (@0)))
2739 { constant_boolean_node (false, type); })))
2740
2741 /* -A CMP -B -> B CMP A. */
2742 (for cmp (tcc_comparison)
2743 scmp (swapped_tcc_comparison)
2744 (simplify
2745 (cmp (negate @0) (negate @1))
2746 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
2747 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2748 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
2749 (scmp @0 @1)))
2750 (simplify
2751 (cmp (negate @0) CONSTANT_CLASS_P@1)
2752 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
2753 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2754 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
2755 (with { tree tem = const_unop (NEGATE_EXPR, TREE_TYPE (@0), @1); }
2756 (if (tem && !TREE_OVERFLOW (tem))
2757 (scmp @0 { tem; }))))))
2758
2759 /* Convert ABS_EXPR<x> == 0 or ABS_EXPR<x> != 0 to x == 0 or x != 0. */
2760 (for op (eq ne)
2761 (simplify
2762 (op (abs @0) zerop@1)
2763 (op @0 @1)))
2764
2765 /* From fold_sign_changed_comparison and fold_widened_comparison. */
2766 (for cmp (simple_comparison)
2767 (simplify
2768 (cmp (convert@0 @00) (convert?@1 @10))
2769 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2770 /* Disable this optimization if we're casting a function pointer
2771 type on targets that require function pointer canonicalization. */
2772 && !(targetm.have_canonicalize_funcptr_for_compare ()
2773 && TREE_CODE (TREE_TYPE (@00)) == POINTER_TYPE
2774 && TREE_CODE (TREE_TYPE (TREE_TYPE (@00))) == FUNCTION_TYPE)
2775 && single_use (@0))
2776 (if (TYPE_PRECISION (TREE_TYPE (@00)) == TYPE_PRECISION (TREE_TYPE (@0))
2777 && (TREE_CODE (@10) == INTEGER_CST
2778 || (@1 != @10 && types_match (TREE_TYPE (@10), TREE_TYPE (@00))))
2779 && (TYPE_UNSIGNED (TREE_TYPE (@00)) == TYPE_UNSIGNED (TREE_TYPE (@0))
2780 || cmp == NE_EXPR
2781 || cmp == EQ_EXPR)
2782 && (POINTER_TYPE_P (TREE_TYPE (@00)) == POINTER_TYPE_P (TREE_TYPE (@0))))
2783 /* ??? The special-casing of INTEGER_CST conversion was in the original
2784 code and here to avoid a spurious overflow flag on the resulting
2785 constant which fold_convert produces. */
2786 (if (TREE_CODE (@1) == INTEGER_CST)
2787 (cmp @00 { force_fit_type (TREE_TYPE (@00), wi::to_widest (@1), 0,
2788 TREE_OVERFLOW (@1)); })
2789 (cmp @00 (convert @1)))
2790
2791 (if (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (TREE_TYPE (@00)))
2792 /* If possible, express the comparison in the shorter mode. */
2793 (if ((cmp == EQ_EXPR || cmp == NE_EXPR
2794 || TYPE_UNSIGNED (TREE_TYPE (@0)) == TYPE_UNSIGNED (TREE_TYPE (@00))
2795 || (!TYPE_UNSIGNED (TREE_TYPE (@0))
2796 && TYPE_UNSIGNED (TREE_TYPE (@00))))
2797 && (types_match (TREE_TYPE (@10), TREE_TYPE (@00))
2798 || ((TYPE_PRECISION (TREE_TYPE (@00))
2799 >= TYPE_PRECISION (TREE_TYPE (@10)))
2800 && (TYPE_UNSIGNED (TREE_TYPE (@00))
2801 == TYPE_UNSIGNED (TREE_TYPE (@10))))
2802 || (TREE_CODE (@10) == INTEGER_CST
2803 && INTEGRAL_TYPE_P (TREE_TYPE (@00))
2804 && int_fits_type_p (@10, TREE_TYPE (@00)))))
2805 (cmp @00 (convert @10))
2806 (if (TREE_CODE (@10) == INTEGER_CST
2807 && INTEGRAL_TYPE_P (TREE_TYPE (@00))
2808 && !int_fits_type_p (@10, TREE_TYPE (@00)))
2809 (with
2810 {
2811 tree min = lower_bound_in_type (TREE_TYPE (@10), TREE_TYPE (@00));
2812 tree max = upper_bound_in_type (TREE_TYPE (@10), TREE_TYPE (@00));
2813 bool above = integer_nonzerop (const_binop (LT_EXPR, type, max, @10));
2814 bool below = integer_nonzerop (const_binop (LT_EXPR, type, @10, min));
2815 }
2816 (if (above || below)
2817 (if (cmp == EQ_EXPR || cmp == NE_EXPR)
2818 { constant_boolean_node (cmp == EQ_EXPR ? false : true, type); }
2819 (if (cmp == LT_EXPR || cmp == LE_EXPR)
2820 { constant_boolean_node (above ? true : false, type); }
2821 (if (cmp == GT_EXPR || cmp == GE_EXPR)
2822 { constant_boolean_node (above ? false : true, type); }))))))))))))
2823
2824 (for cmp (eq ne)
2825 /* A local variable can never be pointed to by
2826 the default SSA name of an incoming parameter.
2827 SSA names are canonicalized to 2nd place. */
2828 (simplify
2829 (cmp addr@0 SSA_NAME@1)
2830 (if (SSA_NAME_IS_DEFAULT_DEF (@1)
2831 && TREE_CODE (SSA_NAME_VAR (@1)) == PARM_DECL)
2832 (with { tree base = get_base_address (TREE_OPERAND (@0, 0)); }
2833 (if (TREE_CODE (base) == VAR_DECL
2834 && auto_var_in_fn_p (base, current_function_decl))
2835 (if (cmp == NE_EXPR)
2836 { constant_boolean_node (true, type); }
2837 { constant_boolean_node (false, type); }))))))
2838
2839 /* Equality compare simplifications from fold_binary */
2840 (for cmp (eq ne)
2841
2842 /* If we have (A | C) == D where C & ~D != 0, convert this into 0.
2843 Similarly for NE_EXPR. */
2844 (simplify
2845 (cmp (convert?@3 (bit_ior @0 INTEGER_CST@1)) INTEGER_CST@2)
2846 (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0))
2847 && wi::bit_and_not (@1, @2) != 0)
2848 { constant_boolean_node (cmp == NE_EXPR, type); }))
2849
2850 /* (X ^ Y) == 0 becomes X == Y, and (X ^ Y) != 0 becomes X != Y. */
2851 (simplify
2852 (cmp (bit_xor @0 @1) integer_zerop)
2853 (cmp @0 @1))
2854
2855 /* (X ^ Y) == Y becomes X == 0.
2856 Likewise (X ^ Y) == X becomes Y == 0. */
2857 (simplify
2858 (cmp:c (bit_xor:c @0 @1) @0)
2859 (cmp @1 { build_zero_cst (TREE_TYPE (@1)); }))
2860
2861 /* (X ^ C1) op C2 can be rewritten as X op (C1 ^ C2). */
2862 (simplify
2863 (cmp (convert?@3 (bit_xor @0 INTEGER_CST@1)) INTEGER_CST@2)
2864 (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0)))
2865 (cmp @0 (bit_xor @1 (convert @2)))))
2866
2867 (simplify
2868 (cmp (convert? addr@0) integer_zerop)
2869 (if (tree_single_nonzero_warnv_p (@0, NULL))
2870 { constant_boolean_node (cmp == NE_EXPR, type); })))
2871
2872 /* If we have (A & C) == C where C is a power of 2, convert this into
2873 (A & C) != 0. Similarly for NE_EXPR. */
2874 (for cmp (eq ne)
2875 icmp (ne eq)
2876 (simplify
2877 (cmp (bit_and@2 @0 integer_pow2p@1) @1)
2878 (icmp @2 { build_zero_cst (TREE_TYPE (@0)); })))
2879
2880 /* If we have (A & C) != 0 ? D : 0 where C and D are powers of 2,
2881 convert this into a shift followed by ANDing with D. */
2882 (simplify
2883 (cond
2884 (ne (bit_and @0 integer_pow2p@1) integer_zerop)
2885 integer_pow2p@2 integer_zerop)
2886 (with {
2887 int shift = wi::exact_log2 (@2) - wi::exact_log2 (@1);
2888 }
2889 (if (shift > 0)
2890 (bit_and
2891 (lshift (convert @0) { build_int_cst (integer_type_node, shift); }) @2)
2892 (bit_and
2893 (convert (rshift @0 { build_int_cst (integer_type_node, -shift); })) @2))))
2894
2895 /* If we have (A & C) != 0 where C is the sign bit of A, convert
2896 this into A < 0. Similarly for (A & C) == 0 into A >= 0. */
2897 (for cmp (eq ne)
2898 ncmp (ge lt)
2899 (simplify
2900 (cmp (bit_and (convert?@2 @0) integer_pow2p@1) integer_zerop)
2901 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2902 && (TYPE_PRECISION (TREE_TYPE (@0))
2903 == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (@0))))
2904 && element_precision (@2) >= element_precision (@0)
2905 && wi::only_sign_bit_p (@1, element_precision (@0)))
2906 (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
2907 (ncmp (convert:stype @0) { build_zero_cst (stype); })))))
2908
2909 /* If we have A < 0 ? C : 0 where C is a power of 2, convert
2910 this into a right shift or sign extension followed by ANDing with C. */
2911 (simplify
2912 (cond
2913 (lt @0 integer_zerop)
2914 integer_pow2p@1 integer_zerop)
2915 (if (!TYPE_UNSIGNED (TREE_TYPE (@0)))
2916 (with {
2917 int shift = element_precision (@0) - wi::exact_log2 (@1) - 1;
2918 }
2919 (if (shift >= 0)
2920 (bit_and
2921 (convert (rshift @0 { build_int_cst (integer_type_node, shift); }))
2922 @1)
2923 /* Otherwise ctype must be wider than TREE_TYPE (@0) and pure
2924 sign extension followed by AND with C will achieve the effect. */
2925 (bit_and (convert @0) @1)))))
2926
2927 /* When the addresses are not directly of decls compare base and offset.
2928 This implements some remaining parts of fold_comparison address
2929 comparisons but still no complete part of it. Still it is good
2930 enough to make fold_stmt not regress when not dispatching to fold_binary. */
2931 (for cmp (simple_comparison)
2932 (simplify
2933 (cmp (convert1?@2 addr@0) (convert2? addr@1))
2934 (with
2935 {
2936 HOST_WIDE_INT off0, off1;
2937 tree base0 = get_addr_base_and_unit_offset (TREE_OPERAND (@0, 0), &off0);
2938 tree base1 = get_addr_base_and_unit_offset (TREE_OPERAND (@1, 0), &off1);
2939 if (base0 && TREE_CODE (base0) == MEM_REF)
2940 {
2941 off0 += mem_ref_offset (base0).to_short_addr ();
2942 base0 = TREE_OPERAND (base0, 0);
2943 }
2944 if (base1 && TREE_CODE (base1) == MEM_REF)
2945 {
2946 off1 += mem_ref_offset (base1).to_short_addr ();
2947 base1 = TREE_OPERAND (base1, 0);
2948 }
2949 }
2950 (if (base0 && base1)
2951 (with
2952 {
2953 int equal = 2;
2954 /* Punt in GENERIC on variables with value expressions;
2955 the value expressions might point to fields/elements
2956 of other vars etc. */
2957 if (GENERIC
2958 && ((VAR_P (base0) && DECL_HAS_VALUE_EXPR_P (base0))
2959 || (VAR_P (base1) && DECL_HAS_VALUE_EXPR_P (base1))))
2960 ;
2961 else if (decl_in_symtab_p (base0)
2962 && decl_in_symtab_p (base1))
2963 equal = symtab_node::get_create (base0)
2964 ->equal_address_to (symtab_node::get_create (base1));
2965 else if ((DECL_P (base0)
2966 || TREE_CODE (base0) == SSA_NAME
2967 || TREE_CODE (base0) == STRING_CST)
2968 && (DECL_P (base1)
2969 || TREE_CODE (base1) == SSA_NAME
2970 || TREE_CODE (base1) == STRING_CST))
2971 equal = (base0 == base1);
2972 }
2973 (if (equal == 1
2974 && (cmp == EQ_EXPR || cmp == NE_EXPR
2975 /* If the offsets are equal we can ignore overflow. */
2976 || off0 == off1
2977 || POINTER_TYPE_OVERFLOW_UNDEFINED
2978 /* Or if we compare using pointers to decls or strings. */
2979 || (POINTER_TYPE_P (TREE_TYPE (@2))
2980 && (DECL_P (base0) || TREE_CODE (base0) == STRING_CST))))
2981 (switch
2982 (if (cmp == EQ_EXPR)
2983 { constant_boolean_node (off0 == off1, type); })
2984 (if (cmp == NE_EXPR)
2985 { constant_boolean_node (off0 != off1, type); })
2986 (if (cmp == LT_EXPR)
2987 { constant_boolean_node (off0 < off1, type); })
2988 (if (cmp == LE_EXPR)
2989 { constant_boolean_node (off0 <= off1, type); })
2990 (if (cmp == GE_EXPR)
2991 { constant_boolean_node (off0 >= off1, type); })
2992 (if (cmp == GT_EXPR)
2993 { constant_boolean_node (off0 > off1, type); }))
2994 (if (equal == 0
2995 && DECL_P (base0) && DECL_P (base1)
2996 /* If we compare this as integers require equal offset. */
2997 && (!INTEGRAL_TYPE_P (TREE_TYPE (@2))
2998 || off0 == off1))
2999 (switch
3000 (if (cmp == EQ_EXPR)
3001 { constant_boolean_node (false, type); })
3002 (if (cmp == NE_EXPR)
3003 { constant_boolean_node (true, type); })))))))))
3004
3005 /* Simplify pointer equality compares using PTA. */
3006 (for neeq (ne eq)
3007 (simplify
3008 (neeq @0 @1)
3009 (if (POINTER_TYPE_P (TREE_TYPE (@0))
3010 && ptrs_compare_unequal (@0, @1))
3011 { neeq == EQ_EXPR ? boolean_false_node : boolean_true_node; })))
3012
3013 /* PR70920: Transform (intptr_t)x eq/ne CST to x eq/ne (typeof x) CST.
3014 and (typeof ptr_cst) x eq/ne ptr_cst to x eq/ne (typeof x) CST.
3015 Disable the transform if either operand is pointer to function.
3016 This broke pr22051-2.c for arm where function pointer
3017 canonicalizaion is not wanted. */
3018
3019 (for cmp (ne eq)
3020 (simplify
3021 (cmp (convert @0) INTEGER_CST@1)
3022 (if ((POINTER_TYPE_P (TREE_TYPE (@0)) && !FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@0)))
3023 && INTEGRAL_TYPE_P (TREE_TYPE (@1)))
3024 || (INTEGRAL_TYPE_P (TREE_TYPE (@0)) && POINTER_TYPE_P (TREE_TYPE (@1))
3025 && !FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@1)))))
3026 (cmp @0 (convert @1)))))
3027
3028 /* Non-equality compare simplifications from fold_binary */
3029 (for cmp (lt gt le ge)
3030 /* Comparisons with the highest or lowest possible integer of
3031 the specified precision will have known values. */
3032 (simplify
3033 (cmp (convert?@2 @0) INTEGER_CST@1)
3034 (if ((INTEGRAL_TYPE_P (TREE_TYPE (@1)) || POINTER_TYPE_P (TREE_TYPE (@1)))
3035 && tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@0)))
3036 (with
3037 {
3038 tree arg1_type = TREE_TYPE (@1);
3039 unsigned int prec = TYPE_PRECISION (arg1_type);
3040 wide_int max = wi::max_value (arg1_type);
3041 wide_int signed_max = wi::max_value (prec, SIGNED);
3042 wide_int min = wi::min_value (arg1_type);
3043 }
3044 (switch
3045 (if (wi::eq_p (@1, max))
3046 (switch
3047 (if (cmp == GT_EXPR)
3048 { constant_boolean_node (false, type); })
3049 (if (cmp == GE_EXPR)
3050 (eq @2 @1))
3051 (if (cmp == LE_EXPR)
3052 { constant_boolean_node (true, type); })
3053 (if (cmp == LT_EXPR)
3054 (ne @2 @1))))
3055 (if (wi::eq_p (@1, min))
3056 (switch
3057 (if (cmp == LT_EXPR)
3058 { constant_boolean_node (false, type); })
3059 (if (cmp == LE_EXPR)
3060 (eq @2 @1))
3061 (if (cmp == GE_EXPR)
3062 { constant_boolean_node (true, type); })
3063 (if (cmp == GT_EXPR)
3064 (ne @2 @1))))
3065 (if (wi::eq_p (@1, max - 1))
3066 (switch
3067 (if (cmp == GT_EXPR)
3068 (eq @2 { wide_int_to_tree (TREE_TYPE (@1), wi::add (@1, 1)); }))
3069 (if (cmp == LE_EXPR)
3070 (ne @2 { wide_int_to_tree (TREE_TYPE (@1), wi::add (@1, 1)); }))))
3071 (if (wi::eq_p (@1, min + 1))
3072 (switch
3073 (if (cmp == GE_EXPR)
3074 (ne @2 { wide_int_to_tree (TREE_TYPE (@1), wi::sub (@1, 1)); }))
3075 (if (cmp == LT_EXPR)
3076 (eq @2 { wide_int_to_tree (TREE_TYPE (@1), wi::sub (@1, 1)); }))))
3077 (if (wi::eq_p (@1, signed_max)
3078 && TYPE_UNSIGNED (arg1_type)
3079 /* We will flip the signedness of the comparison operator
3080 associated with the mode of @1, so the sign bit is
3081 specified by this mode. Check that @1 is the signed
3082 max associated with this sign bit. */
3083 && prec == GET_MODE_PRECISION (TYPE_MODE (arg1_type))
3084 /* signed_type does not work on pointer types. */
3085 && INTEGRAL_TYPE_P (arg1_type))
3086 /* The following case also applies to X < signed_max+1
3087 and X >= signed_max+1 because previous transformations. */
3088 (if (cmp == LE_EXPR || cmp == GT_EXPR)
3089 (with { tree st = signed_type_for (arg1_type); }
3090 (if (cmp == LE_EXPR)
3091 (ge (convert:st @0) { build_zero_cst (st); })
3092 (lt (convert:st @0) { build_zero_cst (st); }))))))))))
3093
3094 (for cmp (unordered ordered unlt unle ungt unge uneq ltgt)
3095 /* If the second operand is NaN, the result is constant. */
3096 (simplify
3097 (cmp @0 REAL_CST@1)
3098 (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
3099 && (cmp != LTGT_EXPR || ! flag_trapping_math))
3100 { constant_boolean_node (cmp == ORDERED_EXPR || cmp == LTGT_EXPR
3101 ? false : true, type); })))
3102
3103 /* bool_var != 0 becomes bool_var. */
3104 (simplify
3105 (ne @0 integer_zerop)
3106 (if (TREE_CODE (TREE_TYPE (@0)) == BOOLEAN_TYPE
3107 && types_match (type, TREE_TYPE (@0)))
3108 (non_lvalue @0)))
3109 /* bool_var == 1 becomes bool_var. */
3110 (simplify
3111 (eq @0 integer_onep)
3112 (if (TREE_CODE (TREE_TYPE (@0)) == BOOLEAN_TYPE
3113 && types_match (type, TREE_TYPE (@0)))
3114 (non_lvalue @0)))
3115 /* Do not handle
3116 bool_var == 0 becomes !bool_var or
3117 bool_var != 1 becomes !bool_var
3118 here because that only is good in assignment context as long
3119 as we require a tcc_comparison in GIMPLE_CONDs where we'd
3120 replace if (x == 0) with tem = ~x; if (tem != 0) which is
3121 clearly less optimal and which we'll transform again in forwprop. */
3122
3123 /* When one argument is a constant, overflow detection can be simplified.
3124 Currently restricted to single use so as not to interfere too much with
3125 ADD_OVERFLOW detection in tree-ssa-math-opts.c.
3126 A + CST CMP A -> A CMP' CST' */
3127 (for cmp (lt le ge gt)
3128 out (gt gt le le)
3129 (simplify
3130 (cmp:c (plus@2 @0 INTEGER_CST@1) @0)
3131 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
3132 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))
3133 && wi::ne_p (@1, 0)
3134 && single_use (@2))
3135 (out @0 { wide_int_to_tree (TREE_TYPE (@0), wi::max_value
3136 (TYPE_PRECISION (TREE_TYPE (@0)), UNSIGNED) - @1); }))))
3137
3138 /* To detect overflow in unsigned A - B, A < B is simpler than A - B > A.
3139 However, the detection logic for SUB_OVERFLOW in tree-ssa-math-opts.c
3140 expects the long form, so we restrict the transformation for now. */
3141 (for cmp (gt le)
3142 (simplify
3143 (cmp:c (minus@2 @0 @1) @0)
3144 (if (single_use (@2)
3145 && ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3146 && TYPE_UNSIGNED (TREE_TYPE (@0))
3147 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
3148 (cmp @1 @0))))
3149
3150 /* Testing for overflow is unnecessary if we already know the result. */
3151 /* A - B > A */
3152 (for cmp (gt le)
3153 out (ne eq)
3154 (simplify
3155 (cmp:c (realpart (IFN_SUB_OVERFLOW@2 @0 @1)) @0)
3156 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
3157 && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
3158 (out (imagpart @2) { build_zero_cst (TREE_TYPE (@0)); }))))
3159 /* A + B < A */
3160 (for cmp (lt ge)
3161 out (ne eq)
3162 (simplify
3163 (cmp:c (realpart (IFN_ADD_OVERFLOW:c@2 @0 @1)) @0)
3164 (if (TYPE_UNSIGNED (TREE_TYPE (@0))
3165 && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
3166 (out (imagpart @2) { build_zero_cst (TREE_TYPE (@0)); }))))
3167
3168 /* For unsigned operands, -1 / B < A checks whether A * B would overflow.
3169 Simplify it to __builtin_mul_overflow (A, B, <unused>). */
3170 (for cmp (lt ge)
3171 out (ne eq)
3172 (simplify
3173 (cmp:c (trunc_div:s integer_all_onesp @1) @0)
3174 (if (TYPE_UNSIGNED (TREE_TYPE (@0)) && !VECTOR_TYPE_P (TREE_TYPE (@0)))
3175 (with { tree t = TREE_TYPE (@0), cpx = build_complex_type (t); }
3176 (out (imagpart (IFN_MUL_OVERFLOW:cpx @0 @1)) { build_zero_cst (t); })))))
3177
3178 /* Simplification of math builtins. These rules must all be optimizations
3179 as well as IL simplifications. If there is a possibility that the new
3180 form could be a pessimization, the rule should go in the canonicalization
3181 section that follows this one.
3182
3183 Rules can generally go in this section if they satisfy one of
3184 the following:
3185
3186 - the rule describes an identity
3187
3188 - the rule replaces calls with something as simple as addition or
3189 multiplication
3190
3191 - the rule contains unary calls only and simplifies the surrounding
3192 arithmetic. (The idea here is to exclude non-unary calls in which
3193 one operand is constant and in which the call is known to be cheap
3194 when the operand has that value.) */
3195
3196 (if (flag_unsafe_math_optimizations)
3197 /* Simplify sqrt(x) * sqrt(x) -> x. */
3198 (simplify
3199 (mult (SQRT@1 @0) @1)
3200 (if (!HONOR_SNANS (type))
3201 @0))
3202
3203 /* Simplify sqrt(x) * sqrt(y) -> sqrt(x*y). */
3204 (for root (SQRT CBRT)
3205 (simplify
3206 (mult (root:s @0) (root:s @1))
3207 (root (mult @0 @1))))
3208
3209 /* Simplify expN(x) * expN(y) -> expN(x+y). */
3210 (for exps (EXP EXP2 EXP10 POW10)
3211 (simplify
3212 (mult (exps:s @0) (exps:s @1))
3213 (exps (plus @0 @1))))
3214
3215 /* Simplify a/root(b/c) into a*root(c/b). */
3216 (for root (SQRT CBRT)
3217 (simplify
3218 (rdiv @0 (root:s (rdiv:s @1 @2)))
3219 (mult @0 (root (rdiv @2 @1)))))
3220
3221 /* Simplify x/expN(y) into x*expN(-y). */
3222 (for exps (EXP EXP2 EXP10 POW10)
3223 (simplify
3224 (rdiv @0 (exps:s @1))
3225 (mult @0 (exps (negate @1)))))
3226
3227 (for logs (LOG LOG2 LOG10 LOG10)
3228 exps (EXP EXP2 EXP10 POW10)
3229 /* logN(expN(x)) -> x. */
3230 (simplify
3231 (logs (exps @0))
3232 @0)
3233 /* expN(logN(x)) -> x. */
3234 (simplify
3235 (exps (logs @0))
3236 @0))
3237
3238 /* Optimize logN(func()) for various exponential functions. We
3239 want to determine the value "x" and the power "exponent" in
3240 order to transform logN(x**exponent) into exponent*logN(x). */
3241 (for logs (LOG LOG LOG LOG2 LOG2 LOG2 LOG10 LOG10)
3242 exps (EXP2 EXP10 POW10 EXP EXP10 POW10 EXP EXP2)
3243 (simplify
3244 (logs (exps @0))
3245 (if (SCALAR_FLOAT_TYPE_P (type))
3246 (with {
3247 tree x;
3248 switch (exps)
3249 {
3250 CASE_CFN_EXP:
3251 /* Prepare to do logN(exp(exponent)) -> exponent*logN(e). */
3252 x = build_real_truncate (type, dconst_e ());
3253 break;
3254 CASE_CFN_EXP2:
3255 /* Prepare to do logN(exp2(exponent)) -> exponent*logN(2). */
3256 x = build_real (type, dconst2);
3257 break;
3258 CASE_CFN_EXP10:
3259 CASE_CFN_POW10:
3260 /* Prepare to do logN(exp10(exponent)) -> exponent*logN(10). */
3261 {
3262 REAL_VALUE_TYPE dconst10;
3263 real_from_integer (&dconst10, VOIDmode, 10, SIGNED);
3264 x = build_real (type, dconst10);
3265 }
3266 break;
3267 default:
3268 gcc_unreachable ();
3269 }
3270 }
3271 (mult (logs { x; }) @0)))))
3272
3273 (for logs (LOG LOG
3274 LOG2 LOG2
3275 LOG10 LOG10)
3276 exps (SQRT CBRT)
3277 (simplify
3278 (logs (exps @0))
3279 (if (SCALAR_FLOAT_TYPE_P (type))
3280 (with {
3281 tree x;
3282 switch (exps)
3283 {
3284 CASE_CFN_SQRT:
3285 /* Prepare to do logN(sqrt(x)) -> 0.5*logN(x). */
3286 x = build_real (type, dconsthalf);
3287 break;
3288 CASE_CFN_CBRT:
3289 /* Prepare to do logN(cbrt(x)) -> (1/3)*logN(x). */
3290 x = build_real_truncate (type, dconst_third ());
3291 break;
3292 default:
3293 gcc_unreachable ();
3294 }
3295 }
3296 (mult { x; } (logs @0))))))
3297
3298 /* logN(pow(x,exponent)) -> exponent*logN(x). */
3299 (for logs (LOG LOG2 LOG10)
3300 pows (POW)
3301 (simplify
3302 (logs (pows @0 @1))
3303 (mult @1 (logs @0))))
3304
3305 (for sqrts (SQRT)
3306 cbrts (CBRT)
3307 pows (POW)
3308 exps (EXP EXP2 EXP10 POW10)
3309 /* sqrt(expN(x)) -> expN(x*0.5). */
3310 (simplify
3311 (sqrts (exps @0))
3312 (exps (mult @0 { build_real (type, dconsthalf); })))
3313 /* cbrt(expN(x)) -> expN(x/3). */
3314 (simplify
3315 (cbrts (exps @0))
3316 (exps (mult @0 { build_real_truncate (type, dconst_third ()); })))
3317 /* pow(expN(x), y) -> expN(x*y). */
3318 (simplify
3319 (pows (exps @0) @1)
3320 (exps (mult @0 @1))))
3321
3322 /* tan(atan(x)) -> x. */
3323 (for tans (TAN)
3324 atans (ATAN)
3325 (simplify
3326 (tans (atans @0))
3327 @0)))
3328
3329 /* cabs(x+0i) or cabs(0+xi) -> abs(x). */
3330 (simplify
3331 (CABS (complex:C @0 real_zerop@1))
3332 (abs @0))
3333
3334 /* trunc(trunc(x)) -> trunc(x), etc. */
3335 (for fns (TRUNC FLOOR CEIL ROUND NEARBYINT RINT)
3336 (simplify
3337 (fns (fns @0))
3338 (fns @0)))
3339 /* f(x) -> x if x is integer valued and f does nothing for such values. */
3340 (for fns (TRUNC FLOOR CEIL ROUND NEARBYINT RINT)
3341 (simplify
3342 (fns integer_valued_real_p@0)
3343 @0))
3344
3345 /* hypot(x,0) and hypot(0,x) -> abs(x). */
3346 (simplify
3347 (HYPOT:c @0 real_zerop@1)
3348 (abs @0))
3349
3350 /* pow(1,x) -> 1. */
3351 (simplify
3352 (POW real_onep@0 @1)
3353 @0)
3354
3355 (simplify
3356 /* copysign(x,x) -> x. */
3357 (COPYSIGN @0 @0)
3358 @0)
3359
3360 (simplify
3361 /* copysign(x,y) -> fabs(x) if y is nonnegative. */
3362 (COPYSIGN @0 tree_expr_nonnegative_p@1)
3363 (abs @0))
3364
3365 (for scale (LDEXP SCALBN SCALBLN)
3366 /* ldexp(0, x) -> 0. */
3367 (simplify
3368 (scale real_zerop@0 @1)
3369 @0)
3370 /* ldexp(x, 0) -> x. */
3371 (simplify
3372 (scale @0 integer_zerop@1)
3373 @0)
3374 /* ldexp(x, y) -> x if x is +-Inf or NaN. */
3375 (simplify
3376 (scale REAL_CST@0 @1)
3377 (if (!real_isfinite (TREE_REAL_CST_PTR (@0)))
3378 @0)))
3379
3380 /* Canonicalization of sequences of math builtins. These rules represent
3381 IL simplifications but are not necessarily optimizations.
3382
3383 The sincos pass is responsible for picking "optimal" implementations
3384 of math builtins, which may be more complicated and can sometimes go
3385 the other way, e.g. converting pow into a sequence of sqrts.
3386 We only want to do these canonicalizations before the pass has run. */
3387
3388 (if (flag_unsafe_math_optimizations && canonicalize_math_p ())
3389 /* Simplify tan(x) * cos(x) -> sin(x). */
3390 (simplify
3391 (mult:c (TAN:s @0) (COS:s @0))
3392 (SIN @0))
3393
3394 /* Simplify x * pow(x,c) -> pow(x,c+1). */
3395 (simplify
3396 (mult:c @0 (POW:s @0 REAL_CST@1))
3397 (if (!TREE_OVERFLOW (@1))
3398 (POW @0 (plus @1 { build_one_cst (type); }))))
3399
3400 /* Simplify sin(x) / cos(x) -> tan(x). */
3401 (simplify
3402 (rdiv (SIN:s @0) (COS:s @0))
3403 (TAN @0))
3404
3405 /* Simplify cos(x) / sin(x) -> 1 / tan(x). */
3406 (simplify
3407 (rdiv (COS:s @0) (SIN:s @0))
3408 (rdiv { build_one_cst (type); } (TAN @0)))
3409
3410 /* Simplify sin(x) / tan(x) -> cos(x). */
3411 (simplify
3412 (rdiv (SIN:s @0) (TAN:s @0))
3413 (if (! HONOR_NANS (@0)
3414 && ! HONOR_INFINITIES (@0))
3415 (COS @0)))
3416
3417 /* Simplify tan(x) / sin(x) -> 1.0 / cos(x). */
3418 (simplify
3419 (rdiv (TAN:s @0) (SIN:s @0))
3420 (if (! HONOR_NANS (@0)
3421 && ! HONOR_INFINITIES (@0))
3422 (rdiv { build_one_cst (type); } (COS @0))))
3423
3424 /* Simplify pow(x,y) * pow(x,z) -> pow(x,y+z). */
3425 (simplify
3426 (mult (POW:s @0 @1) (POW:s @0 @2))
3427 (POW @0 (plus @1 @2)))
3428
3429 /* Simplify pow(x,y) * pow(z,y) -> pow(x*z,y). */
3430 (simplify
3431 (mult (POW:s @0 @1) (POW:s @2 @1))
3432 (POW (mult @0 @2) @1))
3433
3434 /* Simplify powi(x,y) * powi(z,y) -> powi(x*z,y). */
3435 (simplify
3436 (mult (POWI:s @0 @1) (POWI:s @2 @1))
3437 (POWI (mult @0 @2) @1))
3438
3439 /* Simplify pow(x,c) / x -> pow(x,c-1). */
3440 (simplify
3441 (rdiv (POW:s @0 REAL_CST@1) @0)
3442 (if (!TREE_OVERFLOW (@1))
3443 (POW @0 (minus @1 { build_one_cst (type); }))))
3444
3445 /* Simplify x / pow (y,z) -> x * pow(y,-z). */
3446 (simplify
3447 (rdiv @0 (POW:s @1 @2))
3448 (mult @0 (POW @1 (negate @2))))
3449
3450 (for sqrts (SQRT)
3451 cbrts (CBRT)
3452 pows (POW)
3453 /* sqrt(sqrt(x)) -> pow(x,1/4). */
3454 (simplify
3455 (sqrts (sqrts @0))
3456 (pows @0 { build_real (type, dconst_quarter ()); }))
3457 /* sqrt(cbrt(x)) -> pow(x,1/6). */
3458 (simplify
3459 (sqrts (cbrts @0))
3460 (pows @0 { build_real_truncate (type, dconst_sixth ()); }))
3461 /* cbrt(sqrt(x)) -> pow(x,1/6). */
3462 (simplify
3463 (cbrts (sqrts @0))
3464 (pows @0 { build_real_truncate (type, dconst_sixth ()); }))
3465 /* cbrt(cbrt(x)) -> pow(x,1/9), iff x is nonnegative. */
3466 (simplify
3467 (cbrts (cbrts tree_expr_nonnegative_p@0))
3468 (pows @0 { build_real_truncate (type, dconst_ninth ()); }))
3469 /* sqrt(pow(x,y)) -> pow(|x|,y*0.5). */
3470 (simplify
3471 (sqrts (pows @0 @1))
3472 (pows (abs @0) (mult @1 { build_real (type, dconsthalf); })))
3473 /* cbrt(pow(x,y)) -> pow(x,y/3), iff x is nonnegative. */
3474 (simplify
3475 (cbrts (pows tree_expr_nonnegative_p@0 @1))
3476 (pows @0 (mult @1 { build_real_truncate (type, dconst_third ()); })))
3477 /* pow(sqrt(x),y) -> pow(x,y*0.5). */
3478 (simplify
3479 (pows (sqrts @0) @1)
3480 (pows @0 (mult @1 { build_real (type, dconsthalf); })))
3481 /* pow(cbrt(x),y) -> pow(x,y/3) iff x is nonnegative. */
3482 (simplify
3483 (pows (cbrts tree_expr_nonnegative_p@0) @1)
3484 (pows @0 (mult @1 { build_real_truncate (type, dconst_third ()); })))
3485 /* pow(pow(x,y),z) -> pow(x,y*z) iff x is nonnegative. */
3486 (simplify
3487 (pows (pows tree_expr_nonnegative_p@0 @1) @2)
3488 (pows @0 (mult @1 @2))))
3489
3490 /* cabs(x+xi) -> fabs(x)*sqrt(2). */
3491 (simplify
3492 (CABS (complex @0 @0))
3493 (mult (abs @0) { build_real_truncate (type, dconst_sqrt2 ()); }))
3494
3495 /* hypot(x,x) -> fabs(x)*sqrt(2). */
3496 (simplify
3497 (HYPOT @0 @0)
3498 (mult (abs @0) { build_real_truncate (type, dconst_sqrt2 ()); }))
3499
3500 /* cexp(x+yi) -> exp(x)*cexpi(y). */
3501 (for cexps (CEXP)
3502 exps (EXP)
3503 cexpis (CEXPI)
3504 (simplify
3505 (cexps compositional_complex@0)
3506 (if (targetm.libc_has_function (function_c99_math_complex))
3507 (complex
3508 (mult (exps@1 (realpart @0)) (realpart (cexpis:type@2 (imagpart @0))))
3509 (mult @1 (imagpart @2)))))))
3510
3511 (if (canonicalize_math_p ())
3512 /* floor(x) -> trunc(x) if x is nonnegative. */
3513 (for floors (FLOOR)
3514 truncs (TRUNC)
3515 (simplify
3516 (floors tree_expr_nonnegative_p@0)
3517 (truncs @0))))
3518
3519 (match double_value_p
3520 @0
3521 (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == double_type_node)))
3522 (for froms (BUILT_IN_TRUNCL
3523 BUILT_IN_FLOORL
3524 BUILT_IN_CEILL
3525 BUILT_IN_ROUNDL
3526 BUILT_IN_NEARBYINTL
3527 BUILT_IN_RINTL)
3528 tos (BUILT_IN_TRUNC
3529 BUILT_IN_FLOOR
3530 BUILT_IN_CEIL
3531 BUILT_IN_ROUND
3532 BUILT_IN_NEARBYINT
3533 BUILT_IN_RINT)
3534 /* truncl(extend(x)) -> extend(trunc(x)), etc., if x is a double. */
3535 (if (optimize && canonicalize_math_p ())
3536 (simplify
3537 (froms (convert double_value_p@0))
3538 (convert (tos @0)))))
3539
3540 (match float_value_p
3541 @0
3542 (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == float_type_node)))
3543 (for froms (BUILT_IN_TRUNCL BUILT_IN_TRUNC
3544 BUILT_IN_FLOORL BUILT_IN_FLOOR
3545 BUILT_IN_CEILL BUILT_IN_CEIL
3546 BUILT_IN_ROUNDL BUILT_IN_ROUND
3547 BUILT_IN_NEARBYINTL BUILT_IN_NEARBYINT
3548 BUILT_IN_RINTL BUILT_IN_RINT)
3549 tos (BUILT_IN_TRUNCF BUILT_IN_TRUNCF
3550 BUILT_IN_FLOORF BUILT_IN_FLOORF
3551 BUILT_IN_CEILF BUILT_IN_CEILF
3552 BUILT_IN_ROUNDF BUILT_IN_ROUNDF
3553 BUILT_IN_NEARBYINTF BUILT_IN_NEARBYINTF
3554 BUILT_IN_RINTF BUILT_IN_RINTF)
3555 /* truncl(extend(x)) and trunc(extend(x)) -> extend(truncf(x)), etc.,
3556 if x is a float. */
3557 (if (optimize && canonicalize_math_p ()
3558 && targetm.libc_has_function (function_c99_misc))
3559 (simplify
3560 (froms (convert float_value_p@0))
3561 (convert (tos @0)))))
3562
3563 (for froms (XFLOORL XCEILL XROUNDL XRINTL)
3564 tos (XFLOOR XCEIL XROUND XRINT)
3565 /* llfloorl(extend(x)) -> llfloor(x), etc., if x is a double. */
3566 (if (optimize && canonicalize_math_p ())
3567 (simplify
3568 (froms (convert double_value_p@0))
3569 (tos @0))))
3570
3571 (for froms (XFLOORL XCEILL XROUNDL XRINTL
3572 XFLOOR XCEIL XROUND XRINT)
3573 tos (XFLOORF XCEILF XROUNDF XRINTF)
3574 /* llfloorl(extend(x)) and llfloor(extend(x)) -> llfloorf(x), etc.,
3575 if x is a float. */
3576 (if (optimize && canonicalize_math_p ())
3577 (simplify
3578 (froms (convert float_value_p@0))
3579 (tos @0))))
3580
3581 (if (canonicalize_math_p ())
3582 /* xfloor(x) -> fix_trunc(x) if x is nonnegative. */
3583 (for floors (IFLOOR LFLOOR LLFLOOR)
3584 (simplify
3585 (floors tree_expr_nonnegative_p@0)
3586 (fix_trunc @0))))
3587
3588 (if (canonicalize_math_p ())
3589 /* xfloor(x) -> fix_trunc(x), etc., if x is integer valued. */
3590 (for fns (IFLOOR LFLOOR LLFLOOR
3591 ICEIL LCEIL LLCEIL
3592 IROUND LROUND LLROUND)
3593 (simplify
3594 (fns integer_valued_real_p@0)
3595 (fix_trunc @0)))
3596 (if (!flag_errno_math)
3597 /* xrint(x) -> fix_trunc(x), etc., if x is integer valued. */
3598 (for rints (IRINT LRINT LLRINT)
3599 (simplify
3600 (rints integer_valued_real_p@0)
3601 (fix_trunc @0)))))
3602
3603 (if (canonicalize_math_p ())
3604 (for ifn (IFLOOR ICEIL IROUND IRINT)
3605 lfn (LFLOOR LCEIL LROUND LRINT)
3606 llfn (LLFLOOR LLCEIL LLROUND LLRINT)
3607 /* Canonicalize iround (x) to lround (x) on ILP32 targets where
3608 sizeof (int) == sizeof (long). */
3609 (if (TYPE_PRECISION (integer_type_node)
3610 == TYPE_PRECISION (long_integer_type_node))
3611 (simplify
3612 (ifn @0)
3613 (lfn:long_integer_type_node @0)))
3614 /* Canonicalize llround (x) to lround (x) on LP64 targets where
3615 sizeof (long long) == sizeof (long). */
3616 (if (TYPE_PRECISION (long_long_integer_type_node)
3617 == TYPE_PRECISION (long_integer_type_node))
3618 (simplify
3619 (llfn @0)
3620 (lfn:long_integer_type_node @0)))))
3621
3622 /* cproj(x) -> x if we're ignoring infinities. */
3623 (simplify
3624 (CPROJ @0)
3625 (if (!HONOR_INFINITIES (type))
3626 @0))
3627
3628 /* If the real part is inf and the imag part is known to be
3629 nonnegative, return (inf + 0i). */
3630 (simplify
3631 (CPROJ (complex REAL_CST@0 tree_expr_nonnegative_p@1))
3632 (if (real_isinf (TREE_REAL_CST_PTR (@0)))
3633 { build_complex_inf (type, false); }))
3634
3635 /* If the imag part is inf, return (inf+I*copysign(0,imag)). */
3636 (simplify
3637 (CPROJ (complex @0 REAL_CST@1))
3638 (if (real_isinf (TREE_REAL_CST_PTR (@1)))
3639 { build_complex_inf (type, TREE_REAL_CST_PTR (@1)->sign); }))
3640
3641 (for pows (POW)
3642 sqrts (SQRT)
3643 cbrts (CBRT)
3644 (simplify
3645 (pows @0 REAL_CST@1)
3646 (with {
3647 const REAL_VALUE_TYPE *value = TREE_REAL_CST_PTR (@1);
3648 REAL_VALUE_TYPE tmp;
3649 }
3650 (switch
3651 /* pow(x,0) -> 1. */
3652 (if (real_equal (value, &dconst0))
3653 { build_real (type, dconst1); })
3654 /* pow(x,1) -> x. */
3655 (if (real_equal (value, &dconst1))
3656 @0)
3657 /* pow(x,-1) -> 1/x. */
3658 (if (real_equal (value, &dconstm1))
3659 (rdiv { build_real (type, dconst1); } @0))
3660 /* pow(x,0.5) -> sqrt(x). */
3661 (if (flag_unsafe_math_optimizations
3662 && canonicalize_math_p ()
3663 && real_equal (value, &dconsthalf))
3664 (sqrts @0))
3665 /* pow(x,1/3) -> cbrt(x). */
3666 (if (flag_unsafe_math_optimizations
3667 && canonicalize_math_p ()
3668 && (tmp = real_value_truncate (TYPE_MODE (type), dconst_third ()),
3669 real_equal (value, &tmp)))
3670 (cbrts @0))))))
3671
3672 /* powi(1,x) -> 1. */
3673 (simplify
3674 (POWI real_onep@0 @1)
3675 @0)
3676
3677 (simplify
3678 (POWI @0 INTEGER_CST@1)
3679 (switch
3680 /* powi(x,0) -> 1. */
3681 (if (wi::eq_p (@1, 0))
3682 { build_real (type, dconst1); })
3683 /* powi(x,1) -> x. */
3684 (if (wi::eq_p (@1, 1))
3685 @0)
3686 /* powi(x,-1) -> 1/x. */
3687 (if (wi::eq_p (@1, -1))
3688 (rdiv { build_real (type, dconst1); } @0))))
3689
3690 /* Narrowing of arithmetic and logical operations.
3691
3692 These are conceptually similar to the transformations performed for
3693 the C/C++ front-ends by shorten_binary_op and shorten_compare. Long
3694 term we want to move all that code out of the front-ends into here. */
3695
3696 /* If we have a narrowing conversion of an arithmetic operation where
3697 both operands are widening conversions from the same type as the outer
3698 narrowing conversion. Then convert the innermost operands to a suitable
3699 unsigned type (to avoid introducing undefined behavior), perform the
3700 operation and convert the result to the desired type. */
3701 (for op (plus minus)
3702 (simplify
3703 (convert (op:s (convert@2 @0) (convert?@3 @1)))
3704 (if (INTEGRAL_TYPE_P (type)
3705 /* We check for type compatibility between @0 and @1 below,
3706 so there's no need to check that @1/@3 are integral types. */
3707 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
3708 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
3709 /* The precision of the type of each operand must match the
3710 precision of the mode of each operand, similarly for the
3711 result. */
3712 && (TYPE_PRECISION (TREE_TYPE (@0))
3713 == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (@0))))
3714 && (TYPE_PRECISION (TREE_TYPE (@1))
3715 == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (@1))))
3716 && TYPE_PRECISION (type) == GET_MODE_PRECISION (TYPE_MODE (type))
3717 /* The inner conversion must be a widening conversion. */
3718 && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
3719 && types_match (@0, type)
3720 && (types_match (@0, @1)
3721 /* Or the second operand is const integer or converted const
3722 integer from valueize. */
3723 || TREE_CODE (@1) == INTEGER_CST))
3724 (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
3725 (op @0 (convert @1))
3726 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
3727 (convert (op (convert:utype @0)
3728 (convert:utype @1))))))))
3729
3730 /* This is another case of narrowing, specifically when there's an outer
3731 BIT_AND_EXPR which masks off bits outside the type of the innermost
3732 operands. Like the previous case we have to convert the operands
3733 to unsigned types to avoid introducing undefined behavior for the
3734 arithmetic operation. */
3735 (for op (minus plus)
3736 (simplify
3737 (bit_and (op:s (convert@2 @0) (convert@3 @1)) INTEGER_CST@4)
3738 (if (INTEGRAL_TYPE_P (type)
3739 /* We check for type compatibility between @0 and @1 below,
3740 so there's no need to check that @1/@3 are integral types. */
3741 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
3742 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
3743 /* The precision of the type of each operand must match the
3744 precision of the mode of each operand, similarly for the
3745 result. */
3746 && (TYPE_PRECISION (TREE_TYPE (@0))
3747 == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (@0))))
3748 && (TYPE_PRECISION (TREE_TYPE (@1))
3749 == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (@1))))
3750 && TYPE_PRECISION (type) == GET_MODE_PRECISION (TYPE_MODE (type))
3751 /* The inner conversion must be a widening conversion. */
3752 && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
3753 && types_match (@0, @1)
3754 && (tree_int_cst_min_precision (@4, TYPE_SIGN (TREE_TYPE (@0)))
3755 <= TYPE_PRECISION (TREE_TYPE (@0)))
3756 && (wi::bit_and (@4, wi::mask (TYPE_PRECISION (TREE_TYPE (@0)),
3757 true, TYPE_PRECISION (type))) == 0))
3758 (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
3759 (with { tree ntype = TREE_TYPE (@0); }
3760 (convert (bit_and (op @0 @1) (convert:ntype @4))))
3761 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
3762 (convert (bit_and (op (convert:utype @0) (convert:utype @1))
3763 (convert:utype @4))))))))
3764
3765 /* Transform (@0 < @1 and @0 < @2) to use min,
3766 (@0 > @1 and @0 > @2) to use max */
3767 (for op (lt le gt ge)
3768 ext (min min max max)
3769 (simplify
3770 (bit_and (op:cs @0 @1) (op:cs @0 @2))
3771 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
3772 && TREE_CODE (@0) != INTEGER_CST)
3773 (op @0 (ext @1 @2)))))
3774
3775 (simplify
3776 /* signbit(x) -> 0 if x is nonnegative. */
3777 (SIGNBIT tree_expr_nonnegative_p@0)
3778 { integer_zero_node; })
3779
3780 (simplify
3781 /* signbit(x) -> x<0 if x doesn't have signed zeros. */
3782 (SIGNBIT @0)
3783 (if (!HONOR_SIGNED_ZEROS (@0))
3784 (convert (lt @0 { build_real (TREE_TYPE (@0), dconst0); }))))
3785
3786 /* Transform comparisons of the form X +- C1 CMP C2 to X CMP C2 -+ C1. */
3787 (for cmp (eq ne)
3788 (for op (plus minus)
3789 rop (minus plus)
3790 (simplify
3791 (cmp (op@3 @0 INTEGER_CST@1) INTEGER_CST@2)
3792 (if (!TREE_OVERFLOW (@1) && !TREE_OVERFLOW (@2)
3793 && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0))
3794 && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@0))
3795 && !TYPE_SATURATING (TREE_TYPE (@0)))
3796 (with { tree res = int_const_binop (rop, @2, @1); }
3797 (if (TREE_OVERFLOW (res)
3798 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
3799 { constant_boolean_node (cmp == NE_EXPR, type); }
3800 (if (single_use (@3))
3801 (cmp @0 { res; }))))))))
3802 (for cmp (lt le gt ge)
3803 (for op (plus minus)
3804 rop (minus plus)
3805 (simplify
3806 (cmp (op@3 @0 INTEGER_CST@1) INTEGER_CST@2)
3807 (if (!TREE_OVERFLOW (@1) && !TREE_OVERFLOW (@2)
3808 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
3809 (with { tree res = int_const_binop (rop, @2, @1); }
3810 (if (TREE_OVERFLOW (res))
3811 {
3812 fold_overflow_warning (("assuming signed overflow does not occur "
3813 "when simplifying conditional to constant"),
3814 WARN_STRICT_OVERFLOW_CONDITIONAL);
3815 bool less = cmp == LE_EXPR || cmp == LT_EXPR;
3816 /* wi::ges_p (@2, 0) should be sufficient for a signed type. */
3817 bool ovf_high = wi::lt_p (@1, 0, TYPE_SIGN (TREE_TYPE (@1)))
3818 != (op == MINUS_EXPR);
3819 constant_boolean_node (less == ovf_high, type);
3820 }
3821 (if (single_use (@3))
3822 (with
3823 {
3824 fold_overflow_warning (("assuming signed overflow does not occur "
3825 "when changing X +- C1 cmp C2 to "
3826 "X cmp C2 -+ C1"),
3827 WARN_STRICT_OVERFLOW_COMPARISON);
3828 }
3829 (cmp @0 { res; })))))))))
3830
3831 /* Canonicalizations of BIT_FIELD_REFs. */
3832
3833 (simplify
3834 (BIT_FIELD_REF @0 @1 @2)
3835 (switch
3836 (if (TREE_CODE (TREE_TYPE (@0)) == COMPLEX_TYPE
3837 && tree_int_cst_equal (@1, TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))))
3838 (switch
3839 (if (integer_zerop (@2))
3840 (view_convert (realpart @0)))
3841 (if (tree_int_cst_equal (@2, TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))))
3842 (view_convert (imagpart @0)))))
3843 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
3844 && INTEGRAL_TYPE_P (type)
3845 /* On GIMPLE this should only apply to register arguments. */
3846 && (! GIMPLE || is_gimple_reg (@0))
3847 /* A bit-field-ref that referenced the full argument can be stripped. */
3848 && ((compare_tree_int (@1, TYPE_PRECISION (TREE_TYPE (@0))) == 0
3849 && integer_zerop (@2))
3850 /* Low-parts can be reduced to integral conversions.
3851 ??? The following doesn't work for PDP endian. */
3852 || (BYTES_BIG_ENDIAN == WORDS_BIG_ENDIAN
3853 /* Don't even think about BITS_BIG_ENDIAN. */
3854 && TYPE_PRECISION (TREE_TYPE (@0)) % BITS_PER_UNIT == 0
3855 && TYPE_PRECISION (type) % BITS_PER_UNIT == 0
3856 && compare_tree_int (@2, (BYTES_BIG_ENDIAN
3857 ? (TYPE_PRECISION (TREE_TYPE (@0))
3858 - TYPE_PRECISION (type))
3859 : 0)) == 0)))
3860 (convert @0))))
3861
3862 /* Simplify vector extracts. */
3863
3864 (simplify
3865 (BIT_FIELD_REF CONSTRUCTOR@0 @1 @2)
3866 (if (VECTOR_TYPE_P (TREE_TYPE (@0))
3867 && (types_match (type, TREE_TYPE (TREE_TYPE (@0)))
3868 || (VECTOR_TYPE_P (type)
3869 && types_match (TREE_TYPE (type), TREE_TYPE (TREE_TYPE (@0))))))
3870 (with
3871 {
3872 tree ctor = (TREE_CODE (@0) == SSA_NAME
3873 ? gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0)) : @0);
3874 tree eltype = TREE_TYPE (TREE_TYPE (ctor));
3875 unsigned HOST_WIDE_INT width = tree_to_uhwi (TYPE_SIZE (eltype));
3876 unsigned HOST_WIDE_INT n = tree_to_uhwi (@1);
3877 unsigned HOST_WIDE_INT idx = tree_to_uhwi (@2);
3878 }
3879 (if (n != 0
3880 && (idx % width) == 0
3881 && (n % width) == 0
3882 && ((idx + n) / width) <= TYPE_VECTOR_SUBPARTS (TREE_TYPE (ctor)))
3883 (with
3884 {
3885 idx = idx / width;
3886 n = n / width;
3887 /* Constructor elements can be subvectors. */
3888 unsigned HOST_WIDE_INT k = 1;
3889 if (CONSTRUCTOR_NELTS (ctor) != 0)
3890 {
3891 tree cons_elem = TREE_TYPE (CONSTRUCTOR_ELT (ctor, 0)->value);
3892 if (TREE_CODE (cons_elem) == VECTOR_TYPE)
3893 k = TYPE_VECTOR_SUBPARTS (cons_elem);
3894 }
3895 }
3896 (switch
3897 /* We keep an exact subset of the constructor elements. */
3898 (if ((idx % k) == 0 && (n % k) == 0)
3899 (if (CONSTRUCTOR_NELTS (ctor) == 0)
3900 { build_constructor (type, NULL); }
3901 (with
3902 {
3903 idx /= k;
3904 n /= k;
3905 }
3906 (if (n == 1)
3907 (if (idx < CONSTRUCTOR_NELTS (ctor))
3908 { CONSTRUCTOR_ELT (ctor, idx)->value; }
3909 { build_zero_cst (type); })
3910 {
3911 vec<constructor_elt, va_gc> *vals;
3912 vec_alloc (vals, n);
3913 for (unsigned i = 0;
3914 i < n && idx + i < CONSTRUCTOR_NELTS (ctor); ++i)
3915 CONSTRUCTOR_APPEND_ELT (vals, NULL_TREE,
3916 CONSTRUCTOR_ELT (ctor, idx + i)->value);
3917 build_constructor (type, vals);
3918 }))))
3919 /* The bitfield references a single constructor element. */
3920 (if (idx + n <= (idx / k + 1) * k)
3921 (switch
3922 (if (CONSTRUCTOR_NELTS (ctor) <= idx / k)
3923 { build_zero_cst (type); })
3924 (if (n == k)
3925 { CONSTRUCTOR_ELT (ctor, idx / k)->value; })
3926 (BIT_FIELD_REF { CONSTRUCTOR_ELT (ctor, idx / k)->value; }
3927 @1 { bitsize_int ((idx % k) * width); })))))))))