fold-const.c (fold_binary_loc): Move (-A) * (-B) -> A * B to match.pd.
[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-2015 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 integer_pow2p
35 HONOR_NANS)
36
37 /* Operator lists. */
38 (define_operator_list tcc_comparison
39 lt le eq ne ge gt unordered ordered unlt unle ungt unge uneq ltgt)
40 (define_operator_list inverted_tcc_comparison
41 ge gt ne eq lt le ordered unordered ge gt le lt ltgt uneq)
42 (define_operator_list inverted_tcc_comparison_with_nans
43 unge ungt ne eq unlt unle ordered unordered ge gt le lt ltgt uneq)
44 (define_operator_list swapped_tcc_comparison
45 gt ge eq ne le lt unordered ordered ungt unge unlt unle uneq ltgt)
46 (define_operator_list simple_comparison lt le eq ne ge gt)
47 (define_operator_list swapped_simple_comparison gt ge eq ne le lt)
48
49 (define_operator_list LOG BUILT_IN_LOGF BUILT_IN_LOG BUILT_IN_LOGL)
50 (define_operator_list EXP BUILT_IN_EXPF BUILT_IN_EXP BUILT_IN_EXPL)
51 (define_operator_list LOG2 BUILT_IN_LOG2F BUILT_IN_LOG2 BUILT_IN_LOG2L)
52 (define_operator_list EXP2 BUILT_IN_EXP2F BUILT_IN_EXP2 BUILT_IN_EXP2L)
53 (define_operator_list LOG10 BUILT_IN_LOG10F BUILT_IN_LOG10 BUILT_IN_LOG10L)
54 (define_operator_list EXP10 BUILT_IN_EXP10F BUILT_IN_EXP10 BUILT_IN_EXP10L)
55 (define_operator_list POW BUILT_IN_POWF BUILT_IN_POW BUILT_IN_POWL)
56 (define_operator_list POW10 BUILT_IN_POW10F BUILT_IN_POW10 BUILT_IN_POW10L)
57 (define_operator_list SQRT BUILT_IN_SQRTF BUILT_IN_SQRT BUILT_IN_SQRTL)
58 (define_operator_list CBRT BUILT_IN_CBRTF BUILT_IN_CBRT BUILT_IN_CBRTL)
59 (define_operator_list SIN BUILT_IN_SINF BUILT_IN_SIN BUILT_IN_SINL)
60 (define_operator_list COS BUILT_IN_COSF BUILT_IN_COS BUILT_IN_COSL)
61 (define_operator_list TAN BUILT_IN_TANF BUILT_IN_TAN BUILT_IN_TANL)
62 (define_operator_list COSH BUILT_IN_COSHF BUILT_IN_COSH BUILT_IN_COSHL)
63 (define_operator_list CEXPI BUILT_IN_CEXPIF BUILT_IN_CEXPI BUILT_IN_CEXPIL)
64 (define_operator_list CPROJ BUILT_IN_CPROJF BUILT_IN_CPROJ BUILT_IN_CPROJL)
65
66 /* Simplifications of operations with one constant operand and
67 simplifications to constants or single values. */
68
69 (for op (plus pointer_plus minus bit_ior bit_xor)
70 (simplify
71 (op @0 integer_zerop)
72 (non_lvalue @0)))
73
74 /* 0 +p index -> (type)index */
75 (simplify
76 (pointer_plus integer_zerop @1)
77 (non_lvalue (convert @1)))
78
79 /* See if ARG1 is zero and X + ARG1 reduces to X.
80 Likewise if the operands are reversed. */
81 (simplify
82 (plus:c @0 real_zerop@1)
83 (if (fold_real_zero_addition_p (type, @1, 0))
84 (non_lvalue @0)))
85
86 /* See if ARG1 is zero and X - ARG1 reduces to X. */
87 (simplify
88 (minus @0 real_zerop@1)
89 (if (fold_real_zero_addition_p (type, @1, 1))
90 (non_lvalue @0)))
91
92 /* Simplify x - x.
93 This is unsafe for certain floats even in non-IEEE formats.
94 In IEEE, it is unsafe because it does wrong for NaNs.
95 Also note that operand_equal_p is always false if an operand
96 is volatile. */
97 (simplify
98 (minus @0 @0)
99 (if (!FLOAT_TYPE_P (type) || !HONOR_NANS (type))
100 { build_zero_cst (type); }))
101
102 (simplify
103 (mult @0 integer_zerop@1)
104 @1)
105
106 /* Maybe fold x * 0 to 0. The expressions aren't the same
107 when x is NaN, since x * 0 is also NaN. Nor are they the
108 same in modes with signed zeros, since multiplying a
109 negative value by 0 gives -0, not +0. */
110 (simplify
111 (mult @0 real_zerop@1)
112 (if (!HONOR_NANS (type) && !HONOR_SIGNED_ZEROS (type))
113 @1))
114
115 /* In IEEE floating point, x*1 is not equivalent to x for snans.
116 Likewise for complex arithmetic with signed zeros. */
117 (simplify
118 (mult @0 real_onep)
119 (if (!HONOR_SNANS (type)
120 && (!HONOR_SIGNED_ZEROS (type)
121 || !COMPLEX_FLOAT_TYPE_P (type)))
122 (non_lvalue @0)))
123
124 /* Transform x * -1.0 into -x. */
125 (simplify
126 (mult @0 real_minus_onep)
127 (if (!HONOR_SNANS (type)
128 && (!HONOR_SIGNED_ZEROS (type)
129 || !COMPLEX_FLOAT_TYPE_P (type)))
130 (negate @0)))
131
132 /* Make sure to preserve divisions by zero. This is the reason why
133 we don't simplify x / x to 1 or 0 / x to 0. */
134 (for op (mult trunc_div ceil_div floor_div round_div exact_div)
135 (simplify
136 (op @0 integer_onep)
137 (non_lvalue @0)))
138
139 /* X / -1 is -X. */
140 (for div (trunc_div ceil_div floor_div round_div exact_div)
141 (simplify
142 (div @0 integer_minus_onep@1)
143 (if (!TYPE_UNSIGNED (type))
144 (negate @0))))
145
146 /* For unsigned integral types, FLOOR_DIV_EXPR is the same as
147 TRUNC_DIV_EXPR. Rewrite into the latter in this case. */
148 (simplify
149 (floor_div @0 @1)
150 (if ((INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
151 && TYPE_UNSIGNED (type))
152 (trunc_div @0 @1)))
153
154 /* Combine two successive divisions. Note that combining ceil_div
155 and floor_div is trickier and combining round_div even more so. */
156 (for div (trunc_div exact_div)
157 (simplify
158 (div (div @0 INTEGER_CST@1) INTEGER_CST@2)
159 (with {
160 bool overflow_p;
161 wide_int mul = wi::mul (@1, @2, TYPE_SIGN (type), &overflow_p);
162 }
163 (if (!overflow_p)
164 (div @0 { wide_int_to_tree (type, mul); })
165 (if (TYPE_UNSIGNED (type)
166 || mul != wi::min_value (TYPE_PRECISION (type), SIGNED))
167 { build_zero_cst (type); })))))
168
169 /* Optimize A / A to 1.0 if we don't care about
170 NaNs or Infinities. */
171 (simplify
172 (rdiv @0 @0)
173 (if (FLOAT_TYPE_P (type)
174 && ! HONOR_NANS (type)
175 && ! HONOR_INFINITIES (type))
176 { build_one_cst (type); }))
177
178 /* Optimize -A / A to -1.0 if we don't care about
179 NaNs or Infinities. */
180 (simplify
181 (rdiv:c @0 (negate @0))
182 (if (FLOAT_TYPE_P (type)
183 && ! HONOR_NANS (type)
184 && ! HONOR_INFINITIES (type))
185 { build_minus_one_cst (type); }))
186
187 /* In IEEE floating point, x/1 is not equivalent to x for snans. */
188 (simplify
189 (rdiv @0 real_onep)
190 (if (!HONOR_SNANS (type))
191 (non_lvalue @0)))
192
193 /* In IEEE floating point, x/-1 is not equivalent to -x for snans. */
194 (simplify
195 (rdiv @0 real_minus_onep)
196 (if (!HONOR_SNANS (type))
197 (negate @0)))
198
199 /* If ARG1 is a constant, we can convert this to a multiply by the
200 reciprocal. This does not have the same rounding properties,
201 so only do this if -freciprocal-math. We can actually
202 always safely do it if ARG1 is a power of two, but it's hard to
203 tell if it is or not in a portable manner. */
204 (for cst (REAL_CST COMPLEX_CST VECTOR_CST)
205 (simplify
206 (rdiv @0 cst@1)
207 (if (optimize)
208 (if (flag_reciprocal_math
209 && !real_zerop (@1))
210 (with
211 { tree tem = const_binop (RDIV_EXPR, type, build_one_cst (type), @1); }
212 (if (tem)
213 (mult @0 { tem; } )))
214 (if (cst != COMPLEX_CST)
215 (with { tree inverse = exact_inverse (type, @1); }
216 (if (inverse)
217 (mult @0 { inverse; } ))))))))
218
219 /* Same applies to modulo operations, but fold is inconsistent here
220 and simplifies 0 % x to 0, only preserving literal 0 % 0. */
221 (for mod (ceil_mod floor_mod round_mod trunc_mod)
222 /* 0 % X is always zero. */
223 (simplify
224 (mod integer_zerop@0 @1)
225 /* But not for 0 % 0 so that we can get the proper warnings and errors. */
226 (if (!integer_zerop (@1))
227 @0))
228 /* X % 1 is always zero. */
229 (simplify
230 (mod @0 integer_onep)
231 { build_zero_cst (type); })
232 /* X % -1 is zero. */
233 (simplify
234 (mod @0 integer_minus_onep@1)
235 (if (!TYPE_UNSIGNED (type))
236 { build_zero_cst (type); }))
237 /* (X % Y) % Y is just X % Y. */
238 (simplify
239 (mod (mod@2 @0 @1) @1)
240 @2)
241 /* From extract_muldiv_1: (X * C1) % C2 is zero if C1 is a multiple of C2. */
242 (simplify
243 (mod (mult @0 INTEGER_CST@1) INTEGER_CST@2)
244 (if (ANY_INTEGRAL_TYPE_P (type)
245 && TYPE_OVERFLOW_UNDEFINED (type)
246 && wi::multiple_of_p (@1, @2, TYPE_SIGN (type)))
247 { build_zero_cst (type); })))
248
249 /* X % -C is the same as X % C. */
250 (simplify
251 (trunc_mod @0 INTEGER_CST@1)
252 (if (TYPE_SIGN (type) == SIGNED
253 && !TREE_OVERFLOW (@1)
254 && wi::neg_p (@1)
255 && !TYPE_OVERFLOW_TRAPS (type)
256 /* Avoid this transformation if C is INT_MIN, i.e. C == -C. */
257 && !sign_bit_p (@1, @1))
258 (trunc_mod @0 (negate @1))))
259
260 /* X % -Y is the same as X % Y. */
261 (simplify
262 (trunc_mod @0 (convert? (negate @1)))
263 (if (!TYPE_UNSIGNED (type)
264 && !TYPE_OVERFLOW_TRAPS (type)
265 && tree_nop_conversion_p (type, TREE_TYPE (@1)))
266 (trunc_mod @0 (convert @1))))
267
268 /* X - (X / Y) * Y is the same as X % Y. */
269 (simplify
270 (minus (convert1? @0) (convert2? (mult (trunc_div @0 @1) @1)))
271 (if ((INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
272 && TYPE_UNSIGNED (TREE_TYPE (@0)) == TYPE_UNSIGNED (type))
273 (trunc_mod (convert @0) (convert @1))))
274
275 /* Optimize TRUNC_MOD_EXPR by a power of two into a BIT_AND_EXPR,
276 i.e. "X % C" into "X & (C - 1)", if X and C are positive.
277 Also optimize A % (C << N) where C is a power of 2,
278 to A & ((C << N) - 1). */
279 (match (power_of_two_cand @1)
280 INTEGER_CST@1)
281 (match (power_of_two_cand @1)
282 (lshift INTEGER_CST@1 @2))
283 (for mod (trunc_mod floor_mod)
284 (simplify
285 (mod @0 (convert?@3 (power_of_two_cand@1 @2)))
286 (if ((TYPE_UNSIGNED (type)
287 || tree_expr_nonnegative_p (@0))
288 && tree_nop_conversion_p (type, TREE_TYPE (@3))
289 && integer_pow2p (@2) && tree_int_cst_sgn (@2) > 0)
290 (bit_and @0 (convert (minus @1 { build_int_cst (TREE_TYPE (@1), 1); }))))))
291
292 /* Simplify (unsigned t * 2)/2 -> unsigned t & 0x7FFFFFFF. */
293 (simplify
294 (trunc_div (mult @0 integer_pow2p@1) @1)
295 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
296 (bit_and @0 { wide_int_to_tree
297 (type, wi::mask (TYPE_PRECISION (type) - wi::exact_log2 (@1),
298 false, TYPE_PRECISION (type))); })))
299
300 /* Simplify (unsigned t / 2) * 2 -> unsigned t & ~1. */
301 (simplify
302 (mult (trunc_div @0 integer_pow2p@1) @1)
303 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
304 (bit_and @0 (negate @1))))
305
306 /* Simplify (t * 2) / 2) -> t. */
307 (for div (trunc_div ceil_div floor_div round_div exact_div)
308 (simplify
309 (div (mult @0 @1) @1)
310 (if (ANY_INTEGRAL_TYPE_P (type)
311 && TYPE_OVERFLOW_UNDEFINED (type))
312 @0)))
313
314 (for op (negate abs)
315 /* Simplify cos(-x) and cos(|x|) -> cos(x). Similarly for cosh. */
316 (for coss (COS COSH)
317 (simplify
318 (coss (op @0))
319 (coss @0)))
320 /* Simplify pow(-x, y) and pow(|x|,y) -> pow(x,y) if y is an even integer. */
321 (for pows (POW)
322 (simplify
323 (pows (op @0) REAL_CST@1)
324 (with { HOST_WIDE_INT n; }
325 (if (real_isinteger (&TREE_REAL_CST (@1), &n) && (n & 1) == 0)
326 (pows @0 @1))))))
327
328 /* Fold (a * (1 << b)) into (a << b) */
329 (simplify
330 (mult:c @0 (convert? (lshift integer_onep@1 @2)))
331 (if (! FLOAT_TYPE_P (type)
332 && tree_nop_conversion_p (type, TREE_TYPE (@1)))
333 (lshift @0 @2)))
334
335 /* Fold (C1/X)*C2 into (C1*C2)/X. */
336 (simplify
337 (mult (rdiv:s REAL_CST@0 @1) REAL_CST@2)
338 (if (flag_associative_math)
339 (with
340 { tree tem = const_binop (MULT_EXPR, type, @0, @2); }
341 (if (tem)
342 (rdiv { tem; } @1)))))
343
344 /* Simplify ~X & X as zero. */
345 (simplify
346 (bit_and:c (convert? @0) (convert? (bit_not @0)))
347 { build_zero_cst (type); })
348
349 /* X % Y is smaller than Y. */
350 (for cmp (lt ge)
351 (simplify
352 (cmp (trunc_mod @0 @1) @1)
353 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
354 { constant_boolean_node (cmp == LT_EXPR, type); })))
355 (for cmp (gt le)
356 (simplify
357 (cmp @1 (trunc_mod @0 @1))
358 (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
359 { constant_boolean_node (cmp == GT_EXPR, type); })))
360
361 /* x | ~0 -> ~0 */
362 (simplify
363 (bit_ior @0 integer_all_onesp@1)
364 @1)
365
366 /* x & 0 -> 0 */
367 (simplify
368 (bit_and @0 integer_zerop@1)
369 @1)
370
371 /* ~x | x -> -1 */
372 /* ~x ^ x -> -1 */
373 /* ~x + x -> -1 */
374 (for op (bit_ior bit_xor plus)
375 (simplify
376 (op:c (convert? @0) (convert? (bit_not @0)))
377 (convert { build_all_ones_cst (TREE_TYPE (@0)); })))
378
379 /* x ^ x -> 0 */
380 (simplify
381 (bit_xor @0 @0)
382 { build_zero_cst (type); })
383
384 /* Canonicalize X ^ ~0 to ~X. */
385 (simplify
386 (bit_xor @0 integer_all_onesp@1)
387 (bit_not @0))
388
389 /* x & ~0 -> x */
390 (simplify
391 (bit_and @0 integer_all_onesp)
392 (non_lvalue @0))
393
394 /* x & x -> x, x | x -> x */
395 (for bitop (bit_and bit_ior)
396 (simplify
397 (bitop @0 @0)
398 (non_lvalue @0)))
399
400 /* x + (x & 1) -> (x + 1) & ~1 */
401 (simplify
402 (plus:c @0 (bit_and:s @0 integer_onep@1))
403 (bit_and (plus @0 @1) (bit_not @1)))
404
405 /* x & ~(x & y) -> x & ~y */
406 /* x | ~(x | y) -> x | ~y */
407 (for bitop (bit_and bit_ior)
408 (simplify
409 (bitop:c @0 (bit_not (bitop:cs @0 @1)))
410 (bitop @0 (bit_not @1))))
411
412 /* (x | y) & ~x -> y & ~x */
413 /* (x & y) | ~x -> y | ~x */
414 (for bitop (bit_and bit_ior)
415 rbitop (bit_ior bit_and)
416 (simplify
417 (bitop:c (rbitop:c @0 @1) (bit_not@2 @0))
418 (bitop @1 @2)))
419
420 /* (x & y) ^ (x | y) -> x ^ y */
421 (simplify
422 (bit_xor:c (bit_and @0 @1) (bit_ior @0 @1))
423 (bit_xor @0 @1))
424
425 /* (x ^ y) ^ (x | y) -> x & y */
426 (simplify
427 (bit_xor:c (bit_xor @0 @1) (bit_ior @0 @1))
428 (bit_and @0 @1))
429
430 /* (x & y) + (x ^ y) -> x | y */
431 /* (x & y) | (x ^ y) -> x | y */
432 /* (x & y) ^ (x ^ y) -> x | y */
433 (for op (plus bit_ior bit_xor)
434 (simplify
435 (op:c (bit_and @0 @1) (bit_xor @0 @1))
436 (bit_ior @0 @1)))
437
438 /* (x & y) + (x | y) -> x + y */
439 (simplify
440 (plus:c (bit_and @0 @1) (bit_ior @0 @1))
441 (plus @0 @1))
442
443 /* (x + y) - (x | y) -> x & y */
444 (simplify
445 (minus (plus @0 @1) (bit_ior @0 @1))
446 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
447 && !TYPE_SATURATING (type))
448 (bit_and @0 @1)))
449
450 /* (x + y) - (x & y) -> x | y */
451 (simplify
452 (minus (plus @0 @1) (bit_and @0 @1))
453 (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
454 && !TYPE_SATURATING (type))
455 (bit_ior @0 @1)))
456
457 /* (x | y) - (x ^ y) -> x & y */
458 (simplify
459 (minus (bit_ior @0 @1) (bit_xor @0 @1))
460 (bit_and @0 @1))
461
462 /* (x | y) - (x & y) -> x ^ y */
463 (simplify
464 (minus (bit_ior @0 @1) (bit_and @0 @1))
465 (bit_xor @0 @1))
466
467 /* (x | y) & ~(x & y) -> x ^ y */
468 (simplify
469 (bit_and:c (bit_ior @0 @1) (bit_not (bit_and @0 @1)))
470 (bit_xor @0 @1))
471
472 /* (x | y) & (~x ^ y) -> x & y */
473 (simplify
474 (bit_and:c (bit_ior:c @0 @1) (bit_xor:c @1 (bit_not @0)))
475 (bit_and @0 @1))
476
477 /* ~x & ~y -> ~(x | y)
478 ~x | ~y -> ~(x & y) */
479 (for op (bit_and bit_ior)
480 rop (bit_ior bit_and)
481 (simplify
482 (op (convert1? (bit_not @0)) (convert2? (bit_not @1)))
483 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
484 && tree_nop_conversion_p (type, TREE_TYPE (@1)))
485 (bit_not (rop (convert @0) (convert @1))))))
486
487 /* If we are XORing or adding two BIT_AND_EXPR's, both of which are and'ing
488 with a constant, and the two constants have no bits in common,
489 we should treat this as a BIT_IOR_EXPR since this may produce more
490 simplifications. */
491 (for op (bit_xor plus)
492 (simplify
493 (op (convert1? (bit_and@4 @0 INTEGER_CST@1))
494 (convert2? (bit_and@5 @2 INTEGER_CST@3)))
495 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
496 && tree_nop_conversion_p (type, TREE_TYPE (@2))
497 && wi::bit_and (@1, @3) == 0)
498 (bit_ior (convert @4) (convert @5)))))
499
500 /* (X | Y) ^ X -> Y & ~ X*/
501 (simplify
502 (bit_xor:c (convert? (bit_ior:c @0 @1)) (convert? @0))
503 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
504 (convert (bit_and @1 (bit_not @0)))))
505
506 /* Convert ~X ^ ~Y to X ^ Y. */
507 (simplify
508 (bit_xor (convert1? (bit_not @0)) (convert2? (bit_not @1)))
509 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
510 && tree_nop_conversion_p (type, TREE_TYPE (@1)))
511 (bit_xor (convert @0) (convert @1))))
512
513 /* Convert ~X ^ C to X ^ ~C. */
514 (simplify
515 (bit_xor (convert? (bit_not @0)) INTEGER_CST@1)
516 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
517 (bit_xor (convert @0) (bit_not @1))))
518
519 /* Fold (X & Y) ^ Y as ~X & Y. */
520 (simplify
521 (bit_xor:c (bit_and:c @0 @1) @1)
522 (bit_and (bit_not @0) @1))
523
524 /* Given a bit-wise operation CODE applied to ARG0 and ARG1, see if both
525 operands are another bit-wise operation with a common input. If so,
526 distribute the bit operations to save an operation and possibly two if
527 constants are involved. For example, convert
528 (A | B) & (A | C) into A | (B & C)
529 Further simplification will occur if B and C are constants. */
530 (for op (bit_and bit_ior)
531 rop (bit_ior bit_and)
532 (simplify
533 (op (convert? (rop:c @0 @1)) (convert? (rop @0 @2)))
534 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
535 (rop (convert @0) (op (convert @1) (convert @2))))))
536
537
538 (simplify
539 (abs (abs@1 @0))
540 @1)
541 (simplify
542 (abs (negate @0))
543 (abs @0))
544 (simplify
545 (abs tree_expr_nonnegative_p@0)
546 @0)
547
548 /* A few cases of fold-const.c negate_expr_p predicate. */
549 (match negate_expr_p
550 INTEGER_CST
551 (if ((INTEGRAL_TYPE_P (type)
552 && TYPE_OVERFLOW_WRAPS (type))
553 || (!TYPE_OVERFLOW_SANITIZED (type)
554 && may_negate_without_overflow_p (t)))))
555 (match negate_expr_p
556 FIXED_CST)
557 (match negate_expr_p
558 (negate @0)
559 (if (!TYPE_OVERFLOW_SANITIZED (type))))
560 (match negate_expr_p
561 REAL_CST
562 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (t)))))
563 /* VECTOR_CST handling of non-wrapping types would recurse in unsupported
564 ways. */
565 (match negate_expr_p
566 VECTOR_CST
567 (if (FLOAT_TYPE_P (TREE_TYPE (type)) || TYPE_OVERFLOW_WRAPS (type))))
568
569 /* (-A) * (-B) -> A * B */
570 (simplify
571 (mult:c (convert1? (negate @0)) (convert2? negate_expr_p@1))
572 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
573 && tree_nop_conversion_p (type, TREE_TYPE (@1)))
574 (mult (convert @0) (convert (negate @1)))))
575
576 /* -(A + B) -> (-B) - A. */
577 (simplify
578 (negate (plus:c @0 negate_expr_p@1))
579 (if (!HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type))
580 && !HONOR_SIGNED_ZEROS (element_mode (type)))
581 (minus (negate @1) @0)))
582
583 /* A - B -> A + (-B) if B is easily negatable. */
584 (simplify
585 (minus @0 negate_expr_p@1)
586 (if (!FIXED_POINT_TYPE_P (type))
587 (plus @0 (negate @1))))
588
589 /* Try to fold (type) X op CST -> (type) (X op ((type-x) CST))
590 when profitable.
591 For bitwise binary operations apply operand conversions to the
592 binary operation result instead of to the operands. This allows
593 to combine successive conversions and bitwise binary operations.
594 We combine the above two cases by using a conditional convert. */
595 (for bitop (bit_and bit_ior bit_xor)
596 (simplify
597 (bitop (convert @0) (convert? @1))
598 (if (((TREE_CODE (@1) == INTEGER_CST
599 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
600 && int_fits_type_p (@1, TREE_TYPE (@0)))
601 || types_match (@0, @1))
602 /* ??? This transform conflicts with fold-const.c doing
603 Convert (T)(x & c) into (T)x & (T)c, if c is an integer
604 constants (if x has signed type, the sign bit cannot be set
605 in c). This folds extension into the BIT_AND_EXPR.
606 Restrict it to GIMPLE to avoid endless recursions. */
607 && (bitop != BIT_AND_EXPR || GIMPLE)
608 && (/* That's a good idea if the conversion widens the operand, thus
609 after hoisting the conversion the operation will be narrower. */
610 TYPE_PRECISION (TREE_TYPE (@0)) < TYPE_PRECISION (type)
611 /* It's also a good idea if the conversion is to a non-integer
612 mode. */
613 || GET_MODE_CLASS (TYPE_MODE (type)) != MODE_INT
614 /* Or if the precision of TO is not the same as the precision
615 of its mode. */
616 || TYPE_PRECISION (type) != GET_MODE_PRECISION (TYPE_MODE (type))))
617 (convert (bitop @0 (convert @1))))))
618
619 (for bitop (bit_and bit_ior)
620 rbitop (bit_ior bit_and)
621 /* (x | y) & x -> x */
622 /* (x & y) | x -> x */
623 (simplify
624 (bitop:c (rbitop:c @0 @1) @0)
625 @0)
626 /* (~x | y) & x -> x & y */
627 /* (~x & y) | x -> x | y */
628 (simplify
629 (bitop:c (rbitop:c (bit_not @0) @1) @0)
630 (bitop @0 @1)))
631
632 /* Simplify (A & B) OP0 (C & B) to (A OP0 C) & B. */
633 (for bitop (bit_and bit_ior bit_xor)
634 (simplify
635 (bitop (bit_and:c @0 @1) (bit_and @2 @1))
636 (bit_and (bitop @0 @2) @1)))
637
638 /* (x | CST1) & CST2 -> (x & CST2) | (CST1 & CST2) */
639 (simplify
640 (bit_and (bit_ior @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
641 (bit_ior (bit_and @0 @2) (bit_and @1 @2)))
642
643 /* Combine successive equal operations with constants. */
644 (for bitop (bit_and bit_ior bit_xor)
645 (simplify
646 (bitop (bitop @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
647 (bitop @0 (bitop @1 @2))))
648
649 /* Try simple folding for X op !X, and X op X with the help
650 of the truth_valued_p and logical_inverted_value predicates. */
651 (match truth_valued_p
652 @0
653 (if (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1)))
654 (for op (tcc_comparison truth_and truth_andif truth_or truth_orif truth_xor)
655 (match truth_valued_p
656 (op @0 @1)))
657 (match truth_valued_p
658 (truth_not @0))
659
660 (match (logical_inverted_value @0)
661 (truth_not @0))
662 (match (logical_inverted_value @0)
663 (bit_not truth_valued_p@0))
664 (match (logical_inverted_value @0)
665 (eq @0 integer_zerop))
666 (match (logical_inverted_value @0)
667 (ne truth_valued_p@0 integer_truep))
668 (match (logical_inverted_value @0)
669 (bit_xor truth_valued_p@0 integer_truep))
670
671 /* X & !X -> 0. */
672 (simplify
673 (bit_and:c @0 (logical_inverted_value @0))
674 { build_zero_cst (type); })
675 /* X | !X and X ^ !X -> 1, , if X is truth-valued. */
676 (for op (bit_ior bit_xor)
677 (simplify
678 (op:c truth_valued_p@0 (logical_inverted_value @0))
679 { constant_boolean_node (true, type); }))
680 /* X ==/!= !X is false/true. */
681 (for op (eq ne)
682 (simplify
683 (op:c truth_valued_p@0 (logical_inverted_value @0))
684 { constant_boolean_node (op == NE_EXPR ? true : false, type); }))
685
686 /* If arg1 and arg2 are booleans (or any single bit type)
687 then try to simplify:
688
689 (~X & Y) -> X < Y
690 (X & ~Y) -> Y < X
691 (~X | Y) -> X <= Y
692 (X | ~Y) -> Y <= X
693
694 But only do this if our result feeds into a comparison as
695 this transformation is not always a win, particularly on
696 targets with and-not instructions.
697 -> simplify_bitwise_binary_boolean */
698 (simplify
699 (ne (bit_and:c (bit_not @0) @1) integer_zerop)
700 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
701 && TYPE_PRECISION (TREE_TYPE (@1)) == 1)
702 (lt @0 @1)))
703 (simplify
704 (ne (bit_ior:c (bit_not @0) @1) integer_zerop)
705 (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
706 && TYPE_PRECISION (TREE_TYPE (@1)) == 1)
707 (le @0 @1)))
708
709 /* ~~x -> x */
710 (simplify
711 (bit_not (bit_not @0))
712 @0)
713
714 /* Convert ~ (-A) to A - 1. */
715 (simplify
716 (bit_not (convert? (negate @0)))
717 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
718 (convert (minus @0 { build_each_one_cst (TREE_TYPE (@0)); }))))
719
720 /* Convert ~ (A - 1) or ~ (A + -1) to -A. */
721 (simplify
722 (bit_not (convert? (minus @0 integer_each_onep)))
723 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
724 (convert (negate @0))))
725 (simplify
726 (bit_not (convert? (plus @0 integer_all_onesp)))
727 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
728 (convert (negate @0))))
729
730 /* Part of convert ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify. */
731 (simplify
732 (bit_not (convert? (bit_xor @0 INTEGER_CST@1)))
733 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
734 (convert (bit_xor @0 (bit_not @1)))))
735 (simplify
736 (bit_not (convert? (bit_xor:c (bit_not @0) @1)))
737 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
738 (convert (bit_xor @0 @1))))
739
740 /* (x & ~m) | (y & m) -> ((x ^ y) & m) ^ x */
741 (simplify
742 (bit_ior:c (bit_and:cs @0 (bit_not @2)) (bit_and:cs @1 @2))
743 (bit_xor (bit_and (bit_xor @0 @1) @2) @0))
744
745 /* Fold A - (A & B) into ~B & A. */
746 (simplify
747 (minus (convert? @0) (convert?:s (bit_and:cs @0 @1)))
748 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
749 && tree_nop_conversion_p (type, TREE_TYPE (@1)))
750 (convert (bit_and (bit_not @1) @0))))
751
752
753
754 /* ((X inner_op C0) outer_op C1)
755 With X being a tree where value_range has reasoned certain bits to always be
756 zero throughout its computed value range,
757 inner_op = {|,^}, outer_op = {|,^} and inner_op != outer_op
758 where zero_mask has 1's for all bits that are sure to be 0 in
759 and 0's otherwise.
760 if (inner_op == '^') C0 &= ~C1;
761 if ((C0 & ~zero_mask) == 0) then emit (X outer_op (C0 outer_op C1)
762 if ((C1 & ~zero_mask) == 0) then emit (X inner_op (C0 outer_op C1)
763 */
764 (for inner_op (bit_ior bit_xor)
765 outer_op (bit_xor bit_ior)
766 (simplify
767 (outer_op
768 (inner_op:s @2 INTEGER_CST@0) INTEGER_CST@1)
769 (with
770 {
771 bool fail = false;
772 wide_int zero_mask_not;
773 wide_int C0;
774 wide_int cst_emit;
775
776 if (TREE_CODE (@2) == SSA_NAME)
777 zero_mask_not = get_nonzero_bits (@2);
778 else
779 fail = true;
780
781 if (inner_op == BIT_XOR_EXPR)
782 {
783 C0 = wi::bit_and_not (@0, @1);
784 cst_emit = wi::bit_or (C0, @1);
785 }
786 else
787 {
788 C0 = @0;
789 cst_emit = wi::bit_xor (@0, @1);
790 }
791 }
792 (if (!fail && wi::bit_and (C0, zero_mask_not) == 0)
793 (outer_op @2 { wide_int_to_tree (type, cst_emit); })
794 (if (!fail && wi::bit_and (@1, zero_mask_not) == 0)
795 (inner_op @2 { wide_int_to_tree (type, cst_emit); }))))))
796
797 /* Associate (p +p off1) +p off2 as (p +p (off1 + off2)). */
798 (simplify
799 (pointer_plus (pointer_plus:s @0 @1) @3)
800 (pointer_plus @0 (plus @1 @3)))
801
802 /* Pattern match
803 tem1 = (long) ptr1;
804 tem2 = (long) ptr2;
805 tem3 = tem2 - tem1;
806 tem4 = (unsigned long) tem3;
807 tem5 = ptr1 + tem4;
808 and produce
809 tem5 = ptr2; */
810 (simplify
811 (pointer_plus @0 (convert?@2 (minus@3 (convert @1) (convert @0))))
812 /* Conditionally look through a sign-changing conversion. */
813 (if (TYPE_PRECISION (TREE_TYPE (@2)) == TYPE_PRECISION (TREE_TYPE (@3))
814 && ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@1)))
815 || (GENERIC && type == TREE_TYPE (@1))))
816 @1))
817
818 /* Pattern match
819 tem = (sizetype) ptr;
820 tem = tem & algn;
821 tem = -tem;
822 ... = ptr p+ tem;
823 and produce the simpler and easier to analyze with respect to alignment
824 ... = ptr & ~algn; */
825 (simplify
826 (pointer_plus @0 (negate (bit_and (convert @0) INTEGER_CST@1)))
827 (with { tree algn = wide_int_to_tree (TREE_TYPE (@0), wi::bit_not (@1)); }
828 (bit_and @0 { algn; })))
829
830 /* Try folding difference of addresses. */
831 (simplify
832 (minus (convert ADDR_EXPR@0) (convert @1))
833 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
834 (with { HOST_WIDE_INT diff; }
835 (if (ptr_difference_const (@0, @1, &diff))
836 { build_int_cst_type (type, diff); }))))
837 (simplify
838 (minus (convert @0) (convert ADDR_EXPR@1))
839 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
840 (with { HOST_WIDE_INT diff; }
841 (if (ptr_difference_const (@0, @1, &diff))
842 { build_int_cst_type (type, diff); }))))
843
844 /* If arg0 is derived from the address of an object or function, we may
845 be able to fold this expression using the object or function's
846 alignment. */
847 (simplify
848 (bit_and (convert? @0) INTEGER_CST@1)
849 (if (POINTER_TYPE_P (TREE_TYPE (@0))
850 && tree_nop_conversion_p (type, TREE_TYPE (@0)))
851 (with
852 {
853 unsigned int align;
854 unsigned HOST_WIDE_INT bitpos;
855 get_pointer_alignment_1 (@0, &align, &bitpos);
856 }
857 (if (wi::ltu_p (@1, align / BITS_PER_UNIT))
858 { wide_int_to_tree (type, wi::bit_and (@1, bitpos / BITS_PER_UNIT)); }))))
859
860
861 /* We can't reassociate at all for saturating types. */
862 (if (!TYPE_SATURATING (type))
863
864 /* Contract negates. */
865 /* A + (-B) -> A - B */
866 (simplify
867 (plus:c (convert1? @0) (convert2? (negate @1)))
868 /* Apply STRIP_NOPS on @0 and the negate. */
869 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
870 && tree_nop_conversion_p (type, TREE_TYPE (@1))
871 && !TYPE_OVERFLOW_SANITIZED (type))
872 (minus (convert @0) (convert @1))))
873 /* A - (-B) -> A + B */
874 (simplify
875 (minus (convert1? @0) (convert2? (negate @1)))
876 (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
877 && tree_nop_conversion_p (type, TREE_TYPE (@1))
878 && !TYPE_OVERFLOW_SANITIZED (type))
879 (plus (convert @0) (convert @1))))
880 /* -(-A) -> A */
881 (simplify
882 (negate (convert? (negate @1)))
883 (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
884 && !TYPE_OVERFLOW_SANITIZED (type))
885 (convert @1)))
886
887 /* We can't reassociate floating-point unless -fassociative-math
888 or fixed-point plus or minus because of saturation to +-Inf. */
889 (if ((!FLOAT_TYPE_P (type) || flag_associative_math)
890 && !FIXED_POINT_TYPE_P (type))
891
892 /* Match patterns that allow contracting a plus-minus pair
893 irrespective of overflow issues. */
894 /* (A +- B) - A -> +- B */
895 /* (A +- B) -+ B -> A */
896 /* A - (A +- B) -> -+ B */
897 /* A +- (B -+ A) -> +- B */
898 (simplify
899 (minus (plus:c @0 @1) @0)
900 @1)
901 (simplify
902 (minus (minus @0 @1) @0)
903 (negate @1))
904 (simplify
905 (plus:c (minus @0 @1) @1)
906 @0)
907 (simplify
908 (minus @0 (plus:c @0 @1))
909 (negate @1))
910 (simplify
911 (minus @0 (minus @0 @1))
912 @1)
913
914 /* (A +- CST) +- CST -> A + CST */
915 (for outer_op (plus minus)
916 (for inner_op (plus minus)
917 (simplify
918 (outer_op (inner_op @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
919 /* If the constant operation overflows we cannot do the transform
920 as we would introduce undefined overflow, for example
921 with (a - 1) + INT_MIN. */
922 (with { tree cst = fold_binary (outer_op == inner_op
923 ? PLUS_EXPR : MINUS_EXPR, type, @1, @2); }
924 (if (cst && !TREE_OVERFLOW (cst))
925 (inner_op @0 { cst; } ))))))
926
927 /* (CST - A) +- CST -> CST - A */
928 (for outer_op (plus minus)
929 (simplify
930 (outer_op (minus CONSTANT_CLASS_P@1 @0) CONSTANT_CLASS_P@2)
931 (with { tree cst = fold_binary (outer_op, type, @1, @2); }
932 (if (cst && !TREE_OVERFLOW (cst))
933 (minus { cst; } @0)))))
934
935 /* ~A + A -> -1 */
936 (simplify
937 (plus:c (bit_not @0) @0)
938 (if (!TYPE_OVERFLOW_TRAPS (type))
939 { build_all_ones_cst (type); }))
940
941 /* ~A + 1 -> -A */
942 (simplify
943 (plus (convert? (bit_not @0)) integer_each_onep)
944 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
945 (negate (convert @0))))
946
947 /* -A - 1 -> ~A */
948 (simplify
949 (minus (convert? (negate @0)) integer_each_onep)
950 (if (!TYPE_OVERFLOW_TRAPS (type)
951 && tree_nop_conversion_p (type, TREE_TYPE (@0)))
952 (bit_not (convert @0))))
953
954 /* -1 - A -> ~A */
955 (simplify
956 (minus integer_all_onesp @0)
957 (bit_not @0))
958
959 /* (T)(P + A) - (T)P -> (T) A */
960 (for add (plus pointer_plus)
961 (simplify
962 (minus (convert (add @0 @1))
963 (convert @0))
964 (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
965 /* For integer types, if A has a smaller type
966 than T the result depends on the possible
967 overflow in P + A.
968 E.g. T=size_t, A=(unsigned)429497295, P>0.
969 However, if an overflow in P + A would cause
970 undefined behavior, we can assume that there
971 is no overflow. */
972 || (INTEGRAL_TYPE_P (TREE_TYPE (@0))
973 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
974 /* For pointer types, if the conversion of A to the
975 final type requires a sign- or zero-extension,
976 then we have to punt - it is not defined which
977 one is correct. */
978 || (POINTER_TYPE_P (TREE_TYPE (@0))
979 && TREE_CODE (@1) == INTEGER_CST
980 && tree_int_cst_sign_bit (@1) == 0))
981 (convert @1))))))
982
983
984 /* Simplifications of MIN_EXPR and MAX_EXPR. */
985
986 (for minmax (min max)
987 (simplify
988 (minmax @0 @0)
989 @0))
990 (simplify
991 (min @0 @1)
992 (if (INTEGRAL_TYPE_P (type)
993 && TYPE_MIN_VALUE (type)
994 && operand_equal_p (@1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
995 @1))
996 (simplify
997 (max @0 @1)
998 (if (INTEGRAL_TYPE_P (type)
999 && TYPE_MAX_VALUE (type)
1000 && operand_equal_p (@1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
1001 @1))
1002
1003
1004 /* Simplifications of shift and rotates. */
1005
1006 (for rotate (lrotate rrotate)
1007 (simplify
1008 (rotate integer_all_onesp@0 @1)
1009 @0))
1010
1011 /* Optimize -1 >> x for arithmetic right shifts. */
1012 (simplify
1013 (rshift integer_all_onesp@0 @1)
1014 (if (!TYPE_UNSIGNED (type)
1015 && tree_expr_nonnegative_p (@1))
1016 @0))
1017
1018 /* Optimize (x >> c) << c into x & (-1<<c). */
1019 (simplify
1020 (lshift (rshift @0 INTEGER_CST@1) @1)
1021 (if (wi::ltu_p (@1, element_precision (type)))
1022 (bit_and @0 (lshift { build_minus_one_cst (type); } @1))))
1023
1024 /* Optimize (x << c) >> c into x & ((unsigned)-1 >> c) for unsigned
1025 types. */
1026 (simplify
1027 (rshift (lshift @0 INTEGER_CST@1) @1)
1028 (if (TYPE_UNSIGNED (type)
1029 && (wi::ltu_p (@1, element_precision (type))))
1030 (bit_and @0 (rshift { build_minus_one_cst (type); } @1))))
1031
1032 (for shiftrotate (lrotate rrotate lshift rshift)
1033 (simplify
1034 (shiftrotate @0 integer_zerop)
1035 (non_lvalue @0))
1036 (simplify
1037 (shiftrotate integer_zerop@0 @1)
1038 @0)
1039 /* Prefer vector1 << scalar to vector1 << vector2
1040 if vector2 is uniform. */
1041 (for vec (VECTOR_CST CONSTRUCTOR)
1042 (simplify
1043 (shiftrotate @0 vec@1)
1044 (with { tree tem = uniform_vector_p (@1); }
1045 (if (tem)
1046 (shiftrotate @0 { tem; }))))))
1047
1048 /* Rewrite an LROTATE_EXPR by a constant into an
1049 RROTATE_EXPR by a new constant. */
1050 (simplify
1051 (lrotate @0 INTEGER_CST@1)
1052 (rrotate @0 { fold_binary (MINUS_EXPR, TREE_TYPE (@1),
1053 build_int_cst (TREE_TYPE (@1),
1054 element_precision (type)), @1); }))
1055
1056 /* Turn (a OP c1) OP c2 into a OP (c1+c2). */
1057 (for op (lrotate rrotate rshift lshift)
1058 (simplify
1059 (op (op @0 INTEGER_CST@1) INTEGER_CST@2)
1060 (with { unsigned int prec = element_precision (type); }
1061 (if (wi::ge_p (@1, 0, TYPE_SIGN (TREE_TYPE (@1)))
1062 && wi::lt_p (@1, prec, TYPE_SIGN (TREE_TYPE (@1)))
1063 && wi::ge_p (@2, 0, TYPE_SIGN (TREE_TYPE (@2)))
1064 && wi::lt_p (@2, prec, TYPE_SIGN (TREE_TYPE (@2))))
1065 (with { unsigned int low = wi::add (@1, @2).to_uhwi (); }
1066 /* Deal with a OP (c1 + c2) being undefined but (a OP c1) OP c2
1067 being well defined. */
1068 (if (low >= prec)
1069 (if (op == LROTATE_EXPR || op == RROTATE_EXPR)
1070 (op @0 { build_int_cst (TREE_TYPE (@1), low % prec); })
1071 (if (TYPE_UNSIGNED (type) || op == LSHIFT_EXPR)
1072 { build_zero_cst (type); }
1073 (op @0 { build_int_cst (TREE_TYPE (@1), prec - 1); })))
1074 (op @0 { build_int_cst (TREE_TYPE (@1), low); })))))))
1075
1076
1077 /* ((1 << A) & 1) != 0 -> A == 0
1078 ((1 << A) & 1) == 0 -> A != 0 */
1079 (for cmp (ne eq)
1080 icmp (eq ne)
1081 (simplify
1082 (cmp (bit_and (lshift integer_onep @0) integer_onep) integer_zerop)
1083 (icmp @0 { build_zero_cst (TREE_TYPE (@0)); })))
1084
1085 /* (CST1 << A) == CST2 -> A == ctz (CST2) - ctz (CST1)
1086 (CST1 << A) != CST2 -> A != ctz (CST2) - ctz (CST1)
1087 if CST2 != 0. */
1088 (for cmp (ne eq)
1089 (simplify
1090 (cmp (lshift INTEGER_CST@0 @1) INTEGER_CST@2)
1091 (with { int cand = wi::ctz (@2) - wi::ctz (@0); }
1092 (if (cand < 0
1093 || (!integer_zerop (@2)
1094 && wi::ne_p (wi::lshift (@0, cand), @2)))
1095 { constant_boolean_node (cmp == NE_EXPR, type); }
1096 (if (!integer_zerop (@2)
1097 && wi::eq_p (wi::lshift (@0, cand), @2))
1098 (cmp @1 { build_int_cst (TREE_TYPE (@1), cand); }))))))
1099
1100 /* Fold (X << C1) & C2 into (X << C1) & (C2 | ((1 << C1) - 1))
1101 (X >> C1) & C2 into (X >> C1) & (C2 | ~((type) -1 >> C1))
1102 if the new mask might be further optimized. */
1103 (for shift (lshift rshift)
1104 (simplify
1105 (bit_and (convert?:s@4 (shift:s@5 (convert1?@3 @0) INTEGER_CST@1))
1106 INTEGER_CST@2)
1107 (if (tree_nop_conversion_p (TREE_TYPE (@4), TREE_TYPE (@5))
1108 && TYPE_PRECISION (type) <= HOST_BITS_PER_WIDE_INT
1109 && tree_fits_uhwi_p (@1)
1110 && tree_to_uhwi (@1) > 0
1111 && tree_to_uhwi (@1) < TYPE_PRECISION (type))
1112 (with
1113 {
1114 unsigned int shiftc = tree_to_uhwi (@1);
1115 unsigned HOST_WIDE_INT mask = TREE_INT_CST_LOW (@2);
1116 unsigned HOST_WIDE_INT newmask, zerobits = 0;
1117 tree shift_type = TREE_TYPE (@3);
1118 unsigned int prec;
1119
1120 if (shift == LSHIFT_EXPR)
1121 zerobits = ((((unsigned HOST_WIDE_INT) 1) << shiftc) - 1);
1122 else if (shift == RSHIFT_EXPR
1123 && (TYPE_PRECISION (shift_type)
1124 == GET_MODE_PRECISION (TYPE_MODE (shift_type))))
1125 {
1126 prec = TYPE_PRECISION (TREE_TYPE (@3));
1127 tree arg00 = @0;
1128 /* See if more bits can be proven as zero because of
1129 zero extension. */
1130 if (@3 != @0
1131 && TYPE_UNSIGNED (TREE_TYPE (@0)))
1132 {
1133 tree inner_type = TREE_TYPE (@0);
1134 if ((TYPE_PRECISION (inner_type)
1135 == GET_MODE_PRECISION (TYPE_MODE (inner_type)))
1136 && TYPE_PRECISION (inner_type) < prec)
1137 {
1138 prec = TYPE_PRECISION (inner_type);
1139 /* See if we can shorten the right shift. */
1140 if (shiftc < prec)
1141 shift_type = inner_type;
1142 /* Otherwise X >> C1 is all zeros, so we'll optimize
1143 it into (X, 0) later on by making sure zerobits
1144 is all ones. */
1145 }
1146 }
1147 zerobits = ~(unsigned HOST_WIDE_INT) 0;
1148 if (shiftc < prec)
1149 {
1150 zerobits >>= HOST_BITS_PER_WIDE_INT - shiftc;
1151 zerobits <<= prec - shiftc;
1152 }
1153 /* For arithmetic shift if sign bit could be set, zerobits
1154 can contain actually sign bits, so no transformation is
1155 possible, unless MASK masks them all away. In that
1156 case the shift needs to be converted into logical shift. */
1157 if (!TYPE_UNSIGNED (TREE_TYPE (@3))
1158 && prec == TYPE_PRECISION (TREE_TYPE (@3)))
1159 {
1160 if ((mask & zerobits) == 0)
1161 shift_type = unsigned_type_for (TREE_TYPE (@3));
1162 else
1163 zerobits = 0;
1164 }
1165 }
1166 }
1167 /* ((X << 16) & 0xff00) is (X, 0). */
1168 (if ((mask & zerobits) == mask)
1169 { build_int_cst (type, 0); }
1170 (with { newmask = mask | zerobits; }
1171 (if (newmask != mask && (newmask & (newmask + 1)) == 0)
1172 (with
1173 {
1174 /* Only do the transformation if NEWMASK is some integer
1175 mode's mask. */
1176 for (prec = BITS_PER_UNIT;
1177 prec < HOST_BITS_PER_WIDE_INT; prec <<= 1)
1178 if (newmask == (((unsigned HOST_WIDE_INT) 1) << prec) - 1)
1179 break;
1180 }
1181 (if (prec < HOST_BITS_PER_WIDE_INT
1182 || newmask == ~(unsigned HOST_WIDE_INT) 0)
1183 (with
1184 { tree newmaskt = build_int_cst_type (TREE_TYPE (@2), newmask); }
1185 (if (!tree_int_cst_equal (newmaskt, @2))
1186 (if (shift_type != TREE_TYPE (@3))
1187 (bit_and (convert (shift:shift_type (convert @3) @1)) { newmaskt; })
1188 (bit_and @4 { newmaskt; })))))))))))))
1189
1190 /* Fold (X {&,^,|} C2) << C1 into (X << C1) {&,^,|} (C2 << C1)
1191 (X {&,^,|} C2) >> C1 into (X >> C1) & (C2 >> C1). */
1192 (for shift (lshift rshift)
1193 (for bit_op (bit_and bit_xor bit_ior)
1194 (simplify
1195 (shift (convert?:s (bit_op:s @0 INTEGER_CST@2)) INTEGER_CST@1)
1196 (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1197 (with { tree mask = int_const_binop (shift, fold_convert (type, @2), @1); }
1198 (bit_op (shift (convert @0) @1) { mask; }))))))
1199
1200
1201 /* Simplifications of conversions. */
1202
1203 /* Basic strip-useless-type-conversions / strip_nops. */
1204 (for cvt (convert view_convert float fix_trunc)
1205 (simplify
1206 (cvt @0)
1207 (if ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@0)))
1208 || (GENERIC && type == TREE_TYPE (@0)))
1209 @0)))
1210
1211 /* Contract view-conversions. */
1212 (simplify
1213 (view_convert (view_convert @0))
1214 (view_convert @0))
1215
1216 /* For integral conversions with the same precision or pointer
1217 conversions use a NOP_EXPR instead. */
1218 (simplify
1219 (view_convert @0)
1220 (if ((INTEGRAL_TYPE_P (type) || POINTER_TYPE_P (type))
1221 && (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
1222 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (@0)))
1223 (convert @0)))
1224
1225 /* Strip inner integral conversions that do not change precision or size. */
1226 (simplify
1227 (view_convert (convert@0 @1))
1228 (if ((INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
1229 && (INTEGRAL_TYPE_P (TREE_TYPE (@1)) || POINTER_TYPE_P (TREE_TYPE (@1)))
1230 && (TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
1231 && (TYPE_SIZE (TREE_TYPE (@0)) == TYPE_SIZE (TREE_TYPE (@1))))
1232 (view_convert @1)))
1233
1234 /* Re-association barriers around constants and other re-association
1235 barriers can be removed. */
1236 (simplify
1237 (paren CONSTANT_CLASS_P@0)
1238 @0)
1239 (simplify
1240 (paren (paren@1 @0))
1241 @1)
1242
1243 /* Handle cases of two conversions in a row. */
1244 (for ocvt (convert float fix_trunc)
1245 (for icvt (convert float)
1246 (simplify
1247 (ocvt (icvt@1 @0))
1248 (with
1249 {
1250 tree inside_type = TREE_TYPE (@0);
1251 tree inter_type = TREE_TYPE (@1);
1252 int inside_int = INTEGRAL_TYPE_P (inside_type);
1253 int inside_ptr = POINTER_TYPE_P (inside_type);
1254 int inside_float = FLOAT_TYPE_P (inside_type);
1255 int inside_vec = VECTOR_TYPE_P (inside_type);
1256 unsigned int inside_prec = TYPE_PRECISION (inside_type);
1257 int inside_unsignedp = TYPE_UNSIGNED (inside_type);
1258 int inter_int = INTEGRAL_TYPE_P (inter_type);
1259 int inter_ptr = POINTER_TYPE_P (inter_type);
1260 int inter_float = FLOAT_TYPE_P (inter_type);
1261 int inter_vec = VECTOR_TYPE_P (inter_type);
1262 unsigned int inter_prec = TYPE_PRECISION (inter_type);
1263 int inter_unsignedp = TYPE_UNSIGNED (inter_type);
1264 int final_int = INTEGRAL_TYPE_P (type);
1265 int final_ptr = POINTER_TYPE_P (type);
1266 int final_float = FLOAT_TYPE_P (type);
1267 int final_vec = VECTOR_TYPE_P (type);
1268 unsigned int final_prec = TYPE_PRECISION (type);
1269 int final_unsignedp = TYPE_UNSIGNED (type);
1270 }
1271 (switch
1272 /* In addition to the cases of two conversions in a row
1273 handled below, if we are converting something to its own
1274 type via an object of identical or wider precision, neither
1275 conversion is needed. */
1276 (if (((GIMPLE && useless_type_conversion_p (type, inside_type))
1277 || (GENERIC
1278 && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (inside_type)))
1279 && (((inter_int || inter_ptr) && final_int)
1280 || (inter_float && final_float))
1281 && inter_prec >= final_prec)
1282 (ocvt @0))
1283
1284 /* Likewise, if the intermediate and initial types are either both
1285 float or both integer, we don't need the middle conversion if the
1286 former is wider than the latter and doesn't change the signedness
1287 (for integers). Avoid this if the final type is a pointer since
1288 then we sometimes need the middle conversion. Likewise if the
1289 final type has a precision not equal to the size of its mode. */
1290 (if (((inter_int && inside_int) || (inter_float && inside_float))
1291 && (final_int || final_float)
1292 && inter_prec >= inside_prec
1293 && (inter_float || inter_unsignedp == inside_unsignedp)
1294 && ! (final_prec != GET_MODE_PRECISION (TYPE_MODE (type))
1295 && TYPE_MODE (type) == TYPE_MODE (inter_type)))
1296 (ocvt @0))
1297
1298 /* If we have a sign-extension of a zero-extended value, we can
1299 replace that by a single zero-extension. Likewise if the
1300 final conversion does not change precision we can drop the
1301 intermediate conversion. */
1302 (if (inside_int && inter_int && final_int
1303 && ((inside_prec < inter_prec && inter_prec < final_prec
1304 && inside_unsignedp && !inter_unsignedp)
1305 || final_prec == inter_prec))
1306 (ocvt @0))
1307
1308 /* Two conversions in a row are not needed unless:
1309 - some conversion is floating-point (overstrict for now), or
1310 - some conversion is a vector (overstrict for now), or
1311 - the intermediate type is narrower than both initial and
1312 final, or
1313 - the intermediate type and innermost type differ in signedness,
1314 and the outermost type is wider than the intermediate, or
1315 - the initial type is a pointer type and the precisions of the
1316 intermediate and final types differ, or
1317 - the final type is a pointer type and the precisions of the
1318 initial and intermediate types differ. */
1319 (if (! inside_float && ! inter_float && ! final_float
1320 && ! inside_vec && ! inter_vec && ! final_vec
1321 && (inter_prec >= inside_prec || inter_prec >= final_prec)
1322 && ! (inside_int && inter_int
1323 && inter_unsignedp != inside_unsignedp
1324 && inter_prec < final_prec)
1325 && ((inter_unsignedp && inter_prec > inside_prec)
1326 == (final_unsignedp && final_prec > inter_prec))
1327 && ! (inside_ptr && inter_prec != final_prec)
1328 && ! (final_ptr && inside_prec != inter_prec)
1329 && ! (final_prec != GET_MODE_PRECISION (TYPE_MODE (type))
1330 && TYPE_MODE (type) == TYPE_MODE (inter_type)))
1331 (ocvt @0))
1332
1333 /* A truncation to an unsigned type (a zero-extension) should be
1334 canonicalized as bitwise and of a mask. */
1335 (if (final_int && inter_int && inside_int
1336 && final_prec == inside_prec
1337 && final_prec > inter_prec
1338 && inter_unsignedp)
1339 (convert (bit_and @0 { wide_int_to_tree
1340 (inside_type,
1341 wi::mask (inter_prec, false,
1342 TYPE_PRECISION (inside_type))); })))
1343
1344 /* If we are converting an integer to a floating-point that can
1345 represent it exactly and back to an integer, we can skip the
1346 floating-point conversion. */
1347 (if (GIMPLE /* PR66211 */
1348 && inside_int && inter_float && final_int &&
1349 (unsigned) significand_size (TYPE_MODE (inter_type))
1350 >= inside_prec - !inside_unsignedp)
1351 (convert @0)))))))
1352
1353 /* If we have a narrowing conversion to an integral type that is fed by a
1354 BIT_AND_EXPR, we might be able to remove the BIT_AND_EXPR if it merely
1355 masks off bits outside the final type (and nothing else). */
1356 (simplify
1357 (convert (bit_and @0 INTEGER_CST@1))
1358 (if (INTEGRAL_TYPE_P (type)
1359 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
1360 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))
1361 && operand_equal_p (@1, build_low_bits_mask (TREE_TYPE (@1),
1362 TYPE_PRECISION (type)), 0))
1363 (convert @0)))
1364
1365
1366 /* (X /[ex] A) * A -> X. */
1367 (simplify
1368 (mult (convert? (exact_div @0 @1)) @1)
1369 /* Look through a sign-changing conversion. */
1370 (convert @0))
1371
1372 /* Canonicalization of binary operations. */
1373
1374 /* Convert X + -C into X - C. */
1375 (simplify
1376 (plus @0 REAL_CST@1)
1377 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
1378 (with { tree tem = fold_unary (NEGATE_EXPR, type, @1); }
1379 (if (!TREE_OVERFLOW (tem) || !flag_trapping_math)
1380 (minus @0 { tem; })))))
1381
1382 /* Convert x+x into x*2.0. */
1383 (simplify
1384 (plus @0 @0)
1385 (if (SCALAR_FLOAT_TYPE_P (type))
1386 (mult @0 { build_real (type, dconst2); })))
1387
1388 (simplify
1389 (minus integer_zerop @1)
1390 (negate @1))
1391
1392 /* (ARG0 - ARG1) is the same as (-ARG1 + ARG0). So check whether
1393 ARG0 is zero and X + ARG0 reduces to X, since that would mean
1394 (-ARG1 + ARG0) reduces to -ARG1. */
1395 (simplify
1396 (minus real_zerop@0 @1)
1397 (if (fold_real_zero_addition_p (type, @0, 0))
1398 (negate @1)))
1399
1400 /* Transform x * -1 into -x. */
1401 (simplify
1402 (mult @0 integer_minus_onep)
1403 (negate @0))
1404
1405 /* COMPLEX_EXPR and REALPART/IMAGPART_EXPR cancellations. */
1406 (simplify
1407 (complex (realpart @0) (imagpart @0))
1408 @0)
1409 (simplify
1410 (realpart (complex @0 @1))
1411 @0)
1412 (simplify
1413 (imagpart (complex @0 @1))
1414 @1)
1415
1416 /* Sometimes we only care about half of a complex expression. */
1417 (simplify
1418 (realpart (convert?:s (conj:s @0)))
1419 (convert (realpart @0)))
1420 (simplify
1421 (imagpart (convert?:s (conj:s @0)))
1422 (convert (negate (imagpart @0))))
1423 (for part (realpart imagpart)
1424 (for op (plus minus)
1425 (simplify
1426 (part (convert?:s@2 (op:s @0 @1)))
1427 (convert (op (part @0) (part @1))))))
1428 (simplify
1429 (realpart (convert?:s (CEXPI:s @0)))
1430 (convert (COS @0)))
1431 (simplify
1432 (imagpart (convert?:s (CEXPI:s @0)))
1433 (convert (SIN @0)))
1434
1435 /* conj(conj(x)) -> x */
1436 (simplify
1437 (conj (convert? (conj @0)))
1438 (if (tree_nop_conversion_p (TREE_TYPE (@0), type))
1439 (convert @0)))
1440
1441 /* conj({x,y}) -> {x,-y} */
1442 (simplify
1443 (conj (convert?:s (complex:s @0 @1)))
1444 (with { tree itype = TREE_TYPE (type); }
1445 (complex (convert:itype @0) (negate (convert:itype @1)))))
1446
1447 /* BSWAP simplifications, transforms checked by gcc.dg/builtin-bswap-8.c. */
1448 (for bswap (BUILT_IN_BSWAP16 BUILT_IN_BSWAP32 BUILT_IN_BSWAP64)
1449 (simplify
1450 (bswap (bswap @0))
1451 @0)
1452 (simplify
1453 (bswap (bit_not (bswap @0)))
1454 (bit_not @0))
1455 (for bitop (bit_xor bit_ior bit_and)
1456 (simplify
1457 (bswap (bitop:c (bswap @0) @1))
1458 (bitop @0 (bswap @1)))))
1459
1460
1461 /* Combine COND_EXPRs and VEC_COND_EXPRs. */
1462
1463 /* Simplify constant conditions.
1464 Only optimize constant conditions when the selected branch
1465 has the same type as the COND_EXPR. This avoids optimizing
1466 away "c ? x : throw", where the throw has a void type.
1467 Note that we cannot throw away the fold-const.c variant nor
1468 this one as we depend on doing this transform before possibly
1469 A ? B : B -> B triggers and the fold-const.c one can optimize
1470 0 ? A : B to B even if A has side-effects. Something
1471 genmatch cannot handle. */
1472 (simplify
1473 (cond INTEGER_CST@0 @1 @2)
1474 (if (integer_zerop (@0))
1475 (if (!VOID_TYPE_P (TREE_TYPE (@2)) || VOID_TYPE_P (type))
1476 @2)
1477 (if (!VOID_TYPE_P (TREE_TYPE (@1)) || VOID_TYPE_P (type))
1478 @1)))
1479 (simplify
1480 (vec_cond VECTOR_CST@0 @1 @2)
1481 (if (integer_all_onesp (@0))
1482 @1
1483 (if (integer_zerop (@0))
1484 @2)))
1485
1486 (for cnd (cond vec_cond)
1487 /* A ? B : (A ? X : C) -> A ? B : C. */
1488 (simplify
1489 (cnd @0 (cnd @0 @1 @2) @3)
1490 (cnd @0 @1 @3))
1491 (simplify
1492 (cnd @0 @1 (cnd @0 @2 @3))
1493 (cnd @0 @1 @3))
1494
1495 /* A ? B : B -> B. */
1496 (simplify
1497 (cnd @0 @1 @1)
1498 @1)
1499
1500 /* !A ? B : C -> A ? C : B. */
1501 (simplify
1502 (cnd (logical_inverted_value truth_valued_p@0) @1 @2)
1503 (cnd @0 @2 @1)))
1504
1505 /* A + (B vcmp C ? 1 : 0) -> A - (B vcmp C), since vector comparisons
1506 return all-1 or all-0 results. */
1507 /* ??? We could instead convert all instances of the vec_cond to negate,
1508 but that isn't necessarily a win on its own. */
1509 (simplify
1510 (plus:c @3 (view_convert? (vec_cond @0 integer_each_onep@1 integer_zerop@2)))
1511 (if (VECTOR_TYPE_P (type)
1512 && TYPE_VECTOR_SUBPARTS (type) == TYPE_VECTOR_SUBPARTS (TREE_TYPE (@0))
1513 && (TYPE_MODE (TREE_TYPE (type))
1514 == TYPE_MODE (TREE_TYPE (TREE_TYPE (@0)))))
1515 (minus @3 (view_convert @0))))
1516
1517 /* ... likewise A - (B vcmp C ? 1 : 0) -> A + (B vcmp C). */
1518 (simplify
1519 (minus @3 (view_convert? (vec_cond @0 integer_each_onep@1 integer_zerop@2)))
1520 (if (VECTOR_TYPE_P (type)
1521 && TYPE_VECTOR_SUBPARTS (type) == TYPE_VECTOR_SUBPARTS (TREE_TYPE (@0))
1522 && (TYPE_MODE (TREE_TYPE (type))
1523 == TYPE_MODE (TREE_TYPE (TREE_TYPE (@0)))))
1524 (plus @3 (view_convert @0))))
1525
1526
1527 /* Simplifications of comparisons. */
1528
1529 /* See if we can reduce the magnitude of a constant involved in a
1530 comparison by changing the comparison code. This is a canonicalization
1531 formerly done by maybe_canonicalize_comparison_1. */
1532 (for cmp (le gt)
1533 acmp (lt ge)
1534 (simplify
1535 (cmp @0 INTEGER_CST@1)
1536 (if (tree_int_cst_sgn (@1) == -1)
1537 (acmp @0 { wide_int_to_tree (TREE_TYPE (@1), wi::add (@1, 1)); }))))
1538 (for cmp (ge lt)
1539 acmp (gt le)
1540 (simplify
1541 (cmp @0 INTEGER_CST@1)
1542 (if (tree_int_cst_sgn (@1) == 1)
1543 (acmp @0 { wide_int_to_tree (TREE_TYPE (@1), wi::sub (@1, 1)); }))))
1544
1545
1546 /* We can simplify a logical negation of a comparison to the
1547 inverted comparison. As we cannot compute an expression
1548 operator using invert_tree_comparison we have to simulate
1549 that with expression code iteration. */
1550 (for cmp (tcc_comparison)
1551 icmp (inverted_tcc_comparison)
1552 ncmp (inverted_tcc_comparison_with_nans)
1553 /* Ideally we'd like to combine the following two patterns
1554 and handle some more cases by using
1555 (logical_inverted_value (cmp @0 @1))
1556 here but for that genmatch would need to "inline" that.
1557 For now implement what forward_propagate_comparison did. */
1558 (simplify
1559 (bit_not (cmp @0 @1))
1560 (if (VECTOR_TYPE_P (type)
1561 || (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1))
1562 /* Comparison inversion may be impossible for trapping math,
1563 invert_tree_comparison will tell us. But we can't use
1564 a computed operator in the replacement tree thus we have
1565 to play the trick below. */
1566 (with { enum tree_code ic = invert_tree_comparison
1567 (cmp, HONOR_NANS (@0)); }
1568 (if (ic == icmp)
1569 (icmp @0 @1)
1570 (if (ic == ncmp)
1571 (ncmp @0 @1))))))
1572 (simplify
1573 (bit_xor (cmp @0 @1) integer_truep)
1574 (with { enum tree_code ic = invert_tree_comparison
1575 (cmp, HONOR_NANS (@0)); }
1576 (if (ic == icmp)
1577 (icmp @0 @1)
1578 (if (ic == ncmp)
1579 (ncmp @0 @1))))))
1580
1581 /* Transform comparisons of the form X - Y CMP 0 to X CMP Y.
1582 ??? The transformation is valid for the other operators if overflow
1583 is undefined for the type, but performing it here badly interacts
1584 with the transformation in fold_cond_expr_with_comparison which
1585 attempts to synthetize ABS_EXPR. */
1586 (for cmp (eq ne)
1587 (simplify
1588 (cmp (minus@2 @0 @1) integer_zerop)
1589 (if (single_use (@2))
1590 (cmp @0 @1))))
1591
1592 /* Transform comparisons of the form X * C1 CMP 0 to X CMP 0 in the
1593 signed arithmetic case. That form is created by the compiler
1594 often enough for folding it to be of value. One example is in
1595 computing loop trip counts after Operator Strength Reduction. */
1596 (for cmp (simple_comparison)
1597 scmp (swapped_simple_comparison)
1598 (simplify
1599 (cmp (mult @0 INTEGER_CST@1) integer_zerop@2)
1600 /* Handle unfolded multiplication by zero. */
1601 (if (integer_zerop (@1))
1602 (cmp @1 @2)
1603 (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1604 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1605 /* If @1 is negative we swap the sense of the comparison. */
1606 (if (tree_int_cst_sgn (@1) < 0)
1607 (scmp @0 @2)
1608 (cmp @0 @2))))))
1609
1610 /* Simplify comparison of something with itself. For IEEE
1611 floating-point, we can only do some of these simplifications. */
1612 (simplify
1613 (eq @0 @0)
1614 (if (! FLOAT_TYPE_P (TREE_TYPE (@0))
1615 || ! HONOR_NANS (TYPE_MODE (TREE_TYPE (@0))))
1616 { constant_boolean_node (true, type); }))
1617 (for cmp (ge le)
1618 (simplify
1619 (cmp @0 @0)
1620 (eq @0 @0)))
1621 (for cmp (ne gt lt)
1622 (simplify
1623 (cmp @0 @0)
1624 (if (cmp != NE_EXPR
1625 || ! FLOAT_TYPE_P (TREE_TYPE (@0))
1626 || ! HONOR_NANS (TYPE_MODE (TREE_TYPE (@0))))
1627 { constant_boolean_node (false, type); })))
1628 (for cmp (unle unge uneq)
1629 (simplify
1630 (cmp @0 @0)
1631 { constant_boolean_node (true, type); }))
1632 (simplify
1633 (ltgt @0 @0)
1634 (if (!flag_trapping_math)
1635 { constant_boolean_node (false, type); }))
1636
1637 /* Fold ~X op ~Y as Y op X. */
1638 (for cmp (simple_comparison)
1639 (simplify
1640 (cmp (bit_not @0) (bit_not @1))
1641 (cmp @1 @0)))
1642
1643 /* Fold ~X op C as X op' ~C, where op' is the swapped comparison. */
1644 (for cmp (simple_comparison)
1645 scmp (swapped_simple_comparison)
1646 (simplify
1647 (cmp (bit_not @0) CONSTANT_CLASS_P@1)
1648 (if (TREE_CODE (@1) == INTEGER_CST || TREE_CODE (@1) == VECTOR_CST)
1649 (scmp @0 (bit_not @1)))))
1650
1651 (for cmp (simple_comparison)
1652 /* Fold (double)float1 CMP (double)float2 into float1 CMP float2. */
1653 (simplify
1654 (cmp (convert@2 @0) (convert? @1))
1655 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
1656 && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@2))
1657 == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0)))
1658 && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@2))
1659 == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1))))
1660 (with
1661 {
1662 tree type1 = TREE_TYPE (@1);
1663 if (TREE_CODE (@1) == REAL_CST && !DECIMAL_FLOAT_TYPE_P (type1))
1664 {
1665 REAL_VALUE_TYPE orig = TREE_REAL_CST (@1);
1666 if (TYPE_PRECISION (type1) > TYPE_PRECISION (float_type_node)
1667 && exact_real_truncate (TYPE_MODE (float_type_node), &orig))
1668 type1 = float_type_node;
1669 if (TYPE_PRECISION (type1) > TYPE_PRECISION (double_type_node)
1670 && exact_real_truncate (TYPE_MODE (double_type_node), &orig))
1671 type1 = double_type_node;
1672 }
1673 tree newtype
1674 = (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type1)
1675 ? TREE_TYPE (@0) : type1);
1676 }
1677 (if (TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (newtype))
1678 (cmp (convert:newtype @0) (convert:newtype @1))))))
1679
1680 (simplify
1681 (cmp @0 REAL_CST@1)
1682 /* IEEE doesn't distinguish +0 and -0 in comparisons. */
1683 (switch
1684 /* a CMP (-0) -> a CMP 0 */
1685 (if (REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@1)))
1686 (cmp @0 { build_real (TREE_TYPE (@1), dconst0); }))
1687 /* x != NaN is always true, other ops are always false. */
1688 (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
1689 && ! HONOR_SNANS (@1))
1690 { constant_boolean_node (cmp == NE_EXPR, type); })
1691 /* Fold comparisons against infinity. */
1692 (if (REAL_VALUE_ISINF (TREE_REAL_CST (@1))
1693 && MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (@1))))
1694 (with
1695 {
1696 REAL_VALUE_TYPE max;
1697 enum tree_code code = cmp;
1698 bool neg = REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1));
1699 if (neg)
1700 code = swap_tree_comparison (code);
1701 }
1702 (switch
1703 /* x > +Inf is always false, if with ignore sNANs. */
1704 (if (code == GT_EXPR
1705 && ! HONOR_SNANS (@0))
1706 { constant_boolean_node (false, type); })
1707 (if (code == LE_EXPR)
1708 /* x <= +Inf is always true, if we don't case about NaNs. */
1709 (if (! HONOR_NANS (@0))
1710 { constant_boolean_node (true, type); }
1711 /* x <= +Inf is the same as x == x, i.e. !isnan(x). */
1712 (eq @0 @0)))
1713 /* x == +Inf and x >= +Inf are always equal to x > DBL_MAX. */
1714 (if (code == EQ_EXPR || code == GE_EXPR)
1715 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
1716 (if (neg)
1717 (lt @0 { build_real (TREE_TYPE (@0), max); })
1718 (gt @0 { build_real (TREE_TYPE (@0), max); }))))
1719 /* x < +Inf is always equal to x <= DBL_MAX. */
1720 (if (code == LT_EXPR)
1721 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
1722 (if (neg)
1723 (ge @0 { build_real (TREE_TYPE (@0), max); })
1724 (le @0 { build_real (TREE_TYPE (@0), max); }))))
1725 /* x != +Inf is always equal to !(x > DBL_MAX). */
1726 (if (code == NE_EXPR)
1727 (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
1728 (if (! HONOR_NANS (@0))
1729 (if (neg)
1730 (ge @0 { build_real (TREE_TYPE (@0), max); })
1731 (le @0 { build_real (TREE_TYPE (@0), max); }))
1732 (if (neg)
1733 (bit_xor (lt @0 { build_real (TREE_TYPE (@0), max); })
1734 { build_one_cst (type); })
1735 (bit_xor (gt @0 { build_real (TREE_TYPE (@0), max); })
1736 { build_one_cst (type); }))))))))))
1737
1738 /* If this is a comparison of a real constant with a PLUS_EXPR
1739 or a MINUS_EXPR of a real constant, we can convert it into a
1740 comparison with a revised real constant as long as no overflow
1741 occurs when unsafe_math_optimizations are enabled. */
1742 (if (flag_unsafe_math_optimizations)
1743 (for op (plus minus)
1744 (simplify
1745 (cmp (op @0 REAL_CST@1) REAL_CST@2)
1746 (with
1747 {
1748 tree tem = const_binop (op == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR,
1749 TREE_TYPE (@1), @2, @1);
1750 }
1751 (if (tem && !TREE_OVERFLOW (tem))
1752 (cmp @0 { tem; }))))))
1753
1754 /* Likewise, we can simplify a comparison of a real constant with
1755 a MINUS_EXPR whose first operand is also a real constant, i.e.
1756 (c1 - x) < c2 becomes x > c1-c2. Reordering is allowed on
1757 floating-point types only if -fassociative-math is set. */
1758 (if (flag_associative_math)
1759 (simplify
1760 (cmp (minus REAL_CST@0 @1) REAL_CST@2)
1761 (with { tree tem = const_binop (MINUS_EXPR, TREE_TYPE (@1), @0, @2); }
1762 (if (tem && !TREE_OVERFLOW (tem))
1763 (cmp { tem; } @1)))))
1764
1765 /* Fold comparisons against built-in math functions. */
1766 (if (flag_unsafe_math_optimizations
1767 && ! flag_errno_math)
1768 (for sq (SQRT)
1769 (simplify
1770 (cmp (sq @0) REAL_CST@1)
1771 (switch
1772 (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
1773 (switch
1774 /* sqrt(x) < y is always false, if y is negative. */
1775 (if (cmp == EQ_EXPR || cmp == LT_EXPR || cmp == LE_EXPR)
1776 { constant_boolean_node (false, type); })
1777 /* sqrt(x) > y is always true, if y is negative and we
1778 don't care about NaNs, i.e. negative values of x. */
1779 (if (cmp == NE_EXPR || !HONOR_NANS (@0))
1780 { constant_boolean_node (true, type); })
1781 /* sqrt(x) > y is the same as x >= 0, if y is negative. */
1782 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })))
1783 (if (cmp == GT_EXPR || cmp == GE_EXPR)
1784 (with
1785 {
1786 REAL_VALUE_TYPE c2;
1787 real_arithmetic (&c2, MULT_EXPR,
1788 &TREE_REAL_CST (@1), &TREE_REAL_CST (@1));
1789 real_convert (&c2, TYPE_MODE (TREE_TYPE (@0)), &c2);
1790 }
1791 (if (REAL_VALUE_ISINF (c2))
1792 /* sqrt(x) > y is x == +Inf, when y is very large. */
1793 (if (HONOR_INFINITIES (@0))
1794 (eq @0 { build_real (TREE_TYPE (@0), c2); })
1795 { constant_boolean_node (false, type); })
1796 /* sqrt(x) > c is the same as x > c*c. */
1797 (cmp @0 { build_real (TREE_TYPE (@0), c2); }))))
1798 (if (cmp == LT_EXPR || cmp == LE_EXPR)
1799 (with
1800 {
1801 REAL_VALUE_TYPE c2;
1802 real_arithmetic (&c2, MULT_EXPR,
1803 &TREE_REAL_CST (@1), &TREE_REAL_CST (@1));
1804 real_convert (&c2, TYPE_MODE (TREE_TYPE (@0)), &c2);
1805 }
1806 (if (REAL_VALUE_ISINF (c2))
1807 (switch
1808 /* sqrt(x) < y is always true, when y is a very large
1809 value and we don't care about NaNs or Infinities. */
1810 (if (! HONOR_NANS (@0) && ! HONOR_INFINITIES (@0))
1811 { constant_boolean_node (true, type); })
1812 /* sqrt(x) < y is x != +Inf when y is very large and we
1813 don't care about NaNs. */
1814 (if (! HONOR_NANS (@0))
1815 (ne @0 { build_real (TREE_TYPE (@0), c2); }))
1816 /* sqrt(x) < y is x >= 0 when y is very large and we
1817 don't care about Infinities. */
1818 (if (! HONOR_INFINITIES (@0))
1819 (ge @0 { build_real (TREE_TYPE (@0), dconst0); }))
1820 /* sqrt(x) < y is x >= 0 && x != +Inf, when y is large. */
1821 (if (GENERIC)
1822 (truth_andif
1823 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
1824 (ne @0 { build_real (TREE_TYPE (@0), c2); }))))
1825 /* sqrt(x) < c is the same as x < c*c, if we ignore NaNs. */
1826 (if (! HONOR_NANS (@0))
1827 (cmp @0 { build_real (TREE_TYPE (@0), c2); })
1828 /* sqrt(x) < c is the same as x >= 0 && x < c*c. */
1829 (if (GENERIC)
1830 (truth_andif
1831 (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
1832 (cmp @0 { build_real (TREE_TYPE (@0), c2); }))))))))))))
1833
1834 /* Unordered tests if either argument is a NaN. */
1835 (simplify
1836 (bit_ior (unordered @0 @0) (unordered @1 @1))
1837 (if (types_match (@0, @1))
1838 (unordered @0 @1)))
1839 (simplify
1840 (bit_and (ordered @0 @0) (ordered @1 @1))
1841 (if (types_match (@0, @1))
1842 (ordered @0 @1)))
1843 (simplify
1844 (bit_ior:c (unordered @0 @0) (unordered:c@2 @0 @1))
1845 @2)
1846 (simplify
1847 (bit_and:c (ordered @0 @0) (ordered:c@2 @0 @1))
1848 @2)
1849
1850 /* -A CMP -B -> B CMP A. */
1851 (for cmp (tcc_comparison)
1852 scmp (swapped_tcc_comparison)
1853 (simplify
1854 (cmp (negate @0) (negate @1))
1855 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
1856 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1857 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
1858 (scmp @0 @1)))
1859 (simplify
1860 (cmp (negate @0) CONSTANT_CLASS_P@1)
1861 (if (FLOAT_TYPE_P (TREE_TYPE (@0))
1862 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1863 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
1864 (with { tree tem = fold_unary (NEGATE_EXPR, TREE_TYPE (@0), @1); }
1865 (if (tem && !TREE_OVERFLOW (tem))
1866 (scmp @0 { tem; }))))))
1867
1868 /* Convert ABS_EXPR<x> == 0 or ABS_EXPR<x> != 0 to x == 0 or x != 0. */
1869 (for op (eq ne)
1870 (simplify
1871 (op (abs @0) zerop@1)
1872 (op @0 @1)))
1873
1874 /* From fold_sign_changed_comparison and fold_widened_comparison. */
1875 (for cmp (simple_comparison)
1876 (simplify
1877 (cmp (convert@0 @00) (convert?@1 @10))
1878 (if (TREE_CODE (TREE_TYPE (@0)) == INTEGER_TYPE
1879 /* Disable this optimization if we're casting a function pointer
1880 type on targets that require function pointer canonicalization. */
1881 && !(targetm.have_canonicalize_funcptr_for_compare ()
1882 && TREE_CODE (TREE_TYPE (@00)) == POINTER_TYPE
1883 && TREE_CODE (TREE_TYPE (TREE_TYPE (@00))) == FUNCTION_TYPE)
1884 && single_use (@0))
1885 (if (TYPE_PRECISION (TREE_TYPE (@00)) == TYPE_PRECISION (TREE_TYPE (@0))
1886 && (TREE_CODE (@10) == INTEGER_CST
1887 || (@1 != @10 && types_match (TREE_TYPE (@10), TREE_TYPE (@00))))
1888 && (TYPE_UNSIGNED (TREE_TYPE (@00)) == TYPE_UNSIGNED (TREE_TYPE (@0))
1889 || cmp == NE_EXPR
1890 || cmp == EQ_EXPR)
1891 && (POINTER_TYPE_P (TREE_TYPE (@00)) == POINTER_TYPE_P (TREE_TYPE (@0))))
1892 /* ??? The special-casing of INTEGER_CST conversion was in the original
1893 code and here to avoid a spurious overflow flag on the resulting
1894 constant which fold_convert produces. */
1895 (if (TREE_CODE (@1) == INTEGER_CST)
1896 (cmp @00 { force_fit_type (TREE_TYPE (@00), wi::to_widest (@1), 0,
1897 TREE_OVERFLOW (@1)); })
1898 (cmp @00 (convert @1)))
1899
1900 (if (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (TREE_TYPE (@00)))
1901 /* If possible, express the comparison in the shorter mode. */
1902 (if ((cmp == EQ_EXPR || cmp == NE_EXPR
1903 || TYPE_UNSIGNED (TREE_TYPE (@0)) == TYPE_UNSIGNED (TREE_TYPE (@00)))
1904 && (types_match (TREE_TYPE (@10), TREE_TYPE (@00))
1905 || ((TYPE_PRECISION (TREE_TYPE (@00))
1906 >= TYPE_PRECISION (TREE_TYPE (@10)))
1907 && (TYPE_UNSIGNED (TREE_TYPE (@00))
1908 == TYPE_UNSIGNED (TREE_TYPE (@10))))
1909 || (TREE_CODE (@10) == INTEGER_CST
1910 && (TREE_CODE (TREE_TYPE (@00)) == INTEGER_TYPE
1911 || TREE_CODE (TREE_TYPE (@00)) == BOOLEAN_TYPE)
1912 && int_fits_type_p (@10, TREE_TYPE (@00)))))
1913 (cmp @00 (convert @10))
1914 (if (TREE_CODE (@10) == INTEGER_CST
1915 && TREE_CODE (TREE_TYPE (@00)) == INTEGER_TYPE
1916 && !int_fits_type_p (@10, TREE_TYPE (@00)))
1917 (with
1918 {
1919 tree min = lower_bound_in_type (TREE_TYPE (@10), TREE_TYPE (@00));
1920 tree max = upper_bound_in_type (TREE_TYPE (@10), TREE_TYPE (@00));
1921 bool above = integer_nonzerop (const_binop (LT_EXPR, type, max, @10));
1922 bool below = integer_nonzerop (const_binop (LT_EXPR, type, @10, min));
1923 }
1924 (if (above || below)
1925 (if (cmp == EQ_EXPR || cmp == NE_EXPR)
1926 { constant_boolean_node (cmp == EQ_EXPR ? false : true, type); }
1927 (if (cmp == LT_EXPR || cmp == LE_EXPR)
1928 { constant_boolean_node (above ? true : false, type); }
1929 (if (cmp == GT_EXPR || cmp == GE_EXPR)
1930 { constant_boolean_node (above ? false : true, type); }))))))))))))
1931
1932 (for cmp (eq ne)
1933 /* A local variable can never be pointed to by
1934 the default SSA name of an incoming parameter.
1935 SSA names are canonicalized to 2nd place. */
1936 (simplify
1937 (cmp addr@0 SSA_NAME@1)
1938 (if (SSA_NAME_IS_DEFAULT_DEF (@1)
1939 && TREE_CODE (SSA_NAME_VAR (@1)) == PARM_DECL)
1940 (with { tree base = get_base_address (TREE_OPERAND (@0, 0)); }
1941 (if (TREE_CODE (base) == VAR_DECL
1942 && auto_var_in_fn_p (base, current_function_decl))
1943 (if (cmp == NE_EXPR)
1944 { constant_boolean_node (true, type); }
1945 { constant_boolean_node (false, type); }))))))
1946
1947 /* Equality compare simplifications from fold_binary */
1948 (for cmp (eq ne)
1949
1950 /* If we have (A | C) == D where C & ~D != 0, convert this into 0.
1951 Similarly for NE_EXPR. */
1952 (simplify
1953 (cmp (convert?@3 (bit_ior @0 INTEGER_CST@1)) INTEGER_CST@2)
1954 (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0))
1955 && wi::bit_and_not (@1, @2) != 0)
1956 { constant_boolean_node (cmp == NE_EXPR, type); }))
1957
1958 /* (X ^ Y) == 0 becomes X == Y, and (X ^ Y) != 0 becomes X != Y. */
1959 (simplify
1960 (cmp (bit_xor @0 @1) integer_zerop)
1961 (cmp @0 @1))
1962
1963 /* (X ^ Y) == Y becomes X == 0.
1964 Likewise (X ^ Y) == X becomes Y == 0. */
1965 (simplify
1966 (cmp:c (bit_xor:c @0 @1) @0)
1967 (cmp @1 { build_zero_cst (TREE_TYPE (@1)); }))
1968
1969 /* (X ^ C1) op C2 can be rewritten as X op (C1 ^ C2). */
1970 (simplify
1971 (cmp (convert?@3 (bit_xor @0 INTEGER_CST@1)) INTEGER_CST@2)
1972 (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0)))
1973 (cmp @0 (bit_xor @1 (convert @2)))))
1974
1975 (simplify
1976 (cmp (convert? addr@0) integer_zerop)
1977 (if (tree_single_nonzero_warnv_p (@0, NULL))
1978 { constant_boolean_node (cmp == NE_EXPR, type); })))
1979
1980 /* If we have (A & C) == C where C is a power of 2, convert this into
1981 (A & C) != 0. Similarly for NE_EXPR. */
1982 (for cmp (eq ne)
1983 icmp (ne eq)
1984 (simplify
1985 (cmp (bit_and@2 @0 integer_pow2p@1) @1)
1986 (icmp @2 { build_zero_cst (TREE_TYPE (@0)); })))
1987
1988 /* If we have (A & C) != 0 where C is the sign bit of A, convert
1989 this into A < 0. Similarly for (A & C) == 0 into A >= 0. */
1990 (for cmp (eq ne)
1991 ncmp (ge lt)
1992 (simplify
1993 (cmp (bit_and (convert?@2 @0) integer_pow2p@1) integer_zerop)
1994 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1995 && (TYPE_PRECISION (TREE_TYPE (@0))
1996 == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (@0))))
1997 && element_precision (@2) >= element_precision (@0)
1998 && wi::only_sign_bit_p (@1, element_precision (@0)))
1999 (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
2000 (ncmp (convert:stype @0) { build_zero_cst (stype); })))))
2001
2002 /* When the addresses are not directly of decls compare base and offset.
2003 This implements some remaining parts of fold_comparison address
2004 comparisons but still no complete part of it. Still it is good
2005 enough to make fold_stmt not regress when not dispatching to fold_binary. */
2006 (for cmp (simple_comparison)
2007 (simplify
2008 (cmp (convert1?@2 addr@0) (convert2? addr@1))
2009 (with
2010 {
2011 HOST_WIDE_INT off0, off1;
2012 tree base0 = get_addr_base_and_unit_offset (TREE_OPERAND (@0, 0), &off0);
2013 tree base1 = get_addr_base_and_unit_offset (TREE_OPERAND (@1, 0), &off1);
2014 if (base0 && TREE_CODE (base0) == MEM_REF)
2015 {
2016 off0 += mem_ref_offset (base0).to_short_addr ();
2017 base0 = TREE_OPERAND (base0, 0);
2018 }
2019 if (base1 && TREE_CODE (base1) == MEM_REF)
2020 {
2021 off1 += mem_ref_offset (base1).to_short_addr ();
2022 base1 = TREE_OPERAND (base1, 0);
2023 }
2024 }
2025 (if (base0 && base1)
2026 (with
2027 {
2028 int equal = 2;
2029 if (decl_in_symtab_p (base0)
2030 && decl_in_symtab_p (base1))
2031 equal = symtab_node::get_create (base0)
2032 ->equal_address_to (symtab_node::get_create (base1));
2033 else if ((DECL_P (base0)
2034 || TREE_CODE (base0) == SSA_NAME
2035 || TREE_CODE (base0) == STRING_CST)
2036 && (DECL_P (base1)
2037 || TREE_CODE (base1) == SSA_NAME
2038 || TREE_CODE (base1) == STRING_CST))
2039 equal = (base0 == base1);
2040 }
2041 (if (equal == 1
2042 && (cmp == EQ_EXPR || cmp == NE_EXPR
2043 /* If the offsets are equal we can ignore overflow. */
2044 || off0 == off1
2045 || POINTER_TYPE_OVERFLOW_UNDEFINED
2046 /* Or if we compare using pointers to decls or strings. */
2047 || (POINTER_TYPE_P (TREE_TYPE (@2))
2048 && (DECL_P (base0) || TREE_CODE (base0) == STRING_CST))))
2049 (switch
2050 (if (cmp == EQ_EXPR)
2051 { constant_boolean_node (off0 == off1, type); })
2052 (if (cmp == NE_EXPR)
2053 { constant_boolean_node (off0 != off1, type); })
2054 (if (cmp == LT_EXPR)
2055 { constant_boolean_node (off0 < off1, type); })
2056 (if (cmp == LE_EXPR)
2057 { constant_boolean_node (off0 <= off1, type); })
2058 (if (cmp == GE_EXPR)
2059 { constant_boolean_node (off0 >= off1, type); })
2060 (if (cmp == GT_EXPR)
2061 { constant_boolean_node (off0 > off1, type); }))
2062 (if (equal == 0
2063 && DECL_P (base0) && DECL_P (base1)
2064 /* If we compare this as integers require equal offset. */
2065 && (!INTEGRAL_TYPE_P (TREE_TYPE (@2))
2066 || off0 == off1))
2067 (switch
2068 (if (cmp == EQ_EXPR)
2069 { constant_boolean_node (false, type); })
2070 (if (cmp == NE_EXPR)
2071 { constant_boolean_node (true, type); })))))))))
2072
2073 /* Non-equality compare simplifications from fold_binary */
2074 (for cmp (lt gt le ge)
2075 /* Comparisons with the highest or lowest possible integer of
2076 the specified precision will have known values. */
2077 (simplify
2078 (cmp (convert?@2 @0) INTEGER_CST@1)
2079 (if ((INTEGRAL_TYPE_P (TREE_TYPE (@1)) || POINTER_TYPE_P (TREE_TYPE (@1)))
2080 && tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@0)))
2081 (with
2082 {
2083 tree arg1_type = TREE_TYPE (@1);
2084 unsigned int prec = TYPE_PRECISION (arg1_type);
2085 wide_int max = wi::max_value (arg1_type);
2086 wide_int signed_max = wi::max_value (prec, SIGNED);
2087 wide_int min = wi::min_value (arg1_type);
2088 }
2089 (switch
2090 (if (wi::eq_p (@1, max))
2091 (switch
2092 (if (cmp == GT_EXPR)
2093 { constant_boolean_node (false, type); })
2094 (if (cmp == GE_EXPR)
2095 (eq @2 @1))
2096 (if (cmp == LE_EXPR)
2097 { constant_boolean_node (true, type); })
2098 (if (cmp == LT_EXPR)
2099 (ne @2 @1))))
2100 (if (wi::eq_p (@1, min))
2101 (switch
2102 (if (cmp == LT_EXPR)
2103 { constant_boolean_node (false, type); })
2104 (if (cmp == LE_EXPR)
2105 (eq @2 @1))
2106 (if (cmp == GE_EXPR)
2107 { constant_boolean_node (true, type); })
2108 (if (cmp == GT_EXPR)
2109 (ne @2 @1))))
2110 (if (wi::eq_p (@1, max - 1))
2111 (switch
2112 (if (cmp == GT_EXPR)
2113 (eq @2 { wide_int_to_tree (TREE_TYPE (@1), wi::add (@1, 1)); }))
2114 (if (cmp == LE_EXPR)
2115 (ne @2 { wide_int_to_tree (TREE_TYPE (@1), wi::add (@1, 1)); }))))
2116 (if (wi::eq_p (@1, min + 1))
2117 (switch
2118 (if (cmp == GE_EXPR)
2119 (ne @2 { wide_int_to_tree (TREE_TYPE (@1), wi::sub (@1, 1)); }))
2120 (if (cmp == LT_EXPR)
2121 (eq @2 { wide_int_to_tree (TREE_TYPE (@1), wi::sub (@1, 1)); }))))
2122 (if (wi::eq_p (@1, signed_max)
2123 && TYPE_UNSIGNED (arg1_type)
2124 /* We will flip the signedness of the comparison operator
2125 associated with the mode of @1, so the sign bit is
2126 specified by this mode. Check that @1 is the signed
2127 max associated with this sign bit. */
2128 && prec == GET_MODE_PRECISION (TYPE_MODE (arg1_type))
2129 /* signed_type does not work on pointer types. */
2130 && INTEGRAL_TYPE_P (arg1_type))
2131 /* The following case also applies to X < signed_max+1
2132 and X >= signed_max+1 because previous transformations. */
2133 (if (cmp == LE_EXPR || cmp == GT_EXPR)
2134 (with { tree st = signed_type_for (arg1_type); }
2135 (if (cmp == LE_EXPR)
2136 (ge (convert:st @0) { build_zero_cst (st); })
2137 (lt (convert:st @0) { build_zero_cst (st); }))))))))))
2138
2139 (for cmp (unordered ordered unlt unle ungt unge uneq ltgt)
2140 /* If the second operand is NaN, the result is constant. */
2141 (simplify
2142 (cmp @0 REAL_CST@1)
2143 (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
2144 && (cmp != LTGT_EXPR || ! flag_trapping_math))
2145 { constant_boolean_node (cmp == ORDERED_EXPR || cmp == LTGT_EXPR
2146 ? false : true, type); })))
2147
2148 /* bool_var != 0 becomes bool_var. */
2149 (simplify
2150 (ne @0 integer_zerop)
2151 (if (TREE_CODE (TREE_TYPE (@0)) == BOOLEAN_TYPE
2152 && types_match (type, TREE_TYPE (@0)))
2153 (non_lvalue @0)))
2154 /* bool_var == 1 becomes bool_var. */
2155 (simplify
2156 (eq @0 integer_onep)
2157 (if (TREE_CODE (TREE_TYPE (@0)) == BOOLEAN_TYPE
2158 && types_match (type, TREE_TYPE (@0)))
2159 (non_lvalue @0)))
2160 /* Do not handle
2161 bool_var == 0 becomes !bool_var or
2162 bool_var != 1 becomes !bool_var
2163 here because that only is good in assignment context as long
2164 as we require a tcc_comparison in GIMPLE_CONDs where we'd
2165 replace if (x == 0) with tem = ~x; if (tem != 0) which is
2166 clearly less optimal and which we'll transform again in forwprop. */
2167
2168
2169 /* Simplification of math builtins. These rules must all be optimizations
2170 as well as IL simplifications. If there is a possibility that the new
2171 form could be a pessimization, the rule should go in the canonicalization
2172 section that follows this one.
2173
2174 Rules can generally go in this section if they satisfy one of
2175 the following:
2176
2177 - the rule describes an identity
2178
2179 - the rule replaces calls with something as simple as addition or
2180 multiplication
2181
2182 - the rule contains unary calls only and simplifies the surrounding
2183 arithmetic. (The idea here is to exclude non-unary calls in which
2184 one operand is constant and in which the call is known to be cheap
2185 when the operand has that value.) */
2186
2187 (if (flag_unsafe_math_optimizations)
2188 /* Simplify sqrt(x) * sqrt(x) -> x. */
2189 (simplify
2190 (mult (SQRT@1 @0) @1)
2191 (if (!HONOR_SNANS (type))
2192 @0))
2193
2194 /* Simplify sqrt(x) * sqrt(y) -> sqrt(x*y). */
2195 (for root (SQRT CBRT)
2196 (simplify
2197 (mult (root:s @0) (root:s @1))
2198 (root (mult @0 @1))))
2199
2200 /* Simplify expN(x) * expN(y) -> expN(x+y). */
2201 (for exps (EXP EXP2 EXP10 POW10)
2202 (simplify
2203 (mult (exps:s @0) (exps:s @1))
2204 (exps (plus @0 @1))))
2205
2206 /* Simplify a/root(b/c) into a*root(c/b). */
2207 (for root (SQRT CBRT)
2208 (simplify
2209 (rdiv @0 (root:s (rdiv:s @1 @2)))
2210 (mult @0 (root (rdiv @2 @1)))))
2211
2212 /* Simplify x/expN(y) into x*expN(-y). */
2213 (for exps (EXP EXP2 EXP10 POW10)
2214 (simplify
2215 (rdiv @0 (exps:s @1))
2216 (mult @0 (exps (negate @1)))))
2217
2218 /* Special case, optimize logN(expN(x)) = x. */
2219 (for logs (LOG LOG2 LOG10 LOG10)
2220 exps (EXP EXP2 EXP10 POW10)
2221 (simplify
2222 (logs (exps @0))
2223 @0))
2224
2225 /* Optimize logN(func()) for various exponential functions. We
2226 want to determine the value "x" and the power "exponent" in
2227 order to transform logN(x**exponent) into exponent*logN(x). */
2228 (for logs (LOG LOG LOG LOG2 LOG2 LOG2 LOG10 LOG10)
2229 exps (EXP2 EXP10 POW10 EXP EXP10 POW10 EXP EXP2)
2230 (simplify
2231 (logs (exps @0))
2232 (with {
2233 tree x;
2234 switch (exps)
2235 {
2236 CASE_FLT_FN (BUILT_IN_EXP):
2237 /* Prepare to do logN(exp(exponent)) -> exponent*logN(e). */
2238 x = build_real_truncate (type, dconst_e ());
2239 break;
2240 CASE_FLT_FN (BUILT_IN_EXP2):
2241 /* Prepare to do logN(exp2(exponent)) -> exponent*logN(2). */
2242 x = build_real (type, dconst2);
2243 break;
2244 CASE_FLT_FN (BUILT_IN_EXP10):
2245 CASE_FLT_FN (BUILT_IN_POW10):
2246 /* Prepare to do logN(exp10(exponent)) -> exponent*logN(10). */
2247 {
2248 REAL_VALUE_TYPE dconst10;
2249 real_from_integer (&dconst10, VOIDmode, 10, SIGNED);
2250 x = build_real (type, dconst10);
2251 }
2252 break;
2253 default:
2254 gcc_unreachable ();
2255 }
2256 }
2257 (mult (logs { x; }) @0))))
2258
2259 (for logs (LOG LOG
2260 LOG2 LOG2
2261 LOG10 LOG10)
2262 exps (SQRT CBRT)
2263 (simplify
2264 (logs (exps @0))
2265 (with {
2266 tree x;
2267 switch (exps)
2268 {
2269 CASE_FLT_FN (BUILT_IN_SQRT):
2270 /* Prepare to do logN(sqrt(x)) -> 0.5*logN(x). */
2271 x = build_real (type, dconsthalf);
2272 break;
2273 CASE_FLT_FN (BUILT_IN_CBRT):
2274 /* Prepare to do logN(cbrt(x)) -> (1/3)*logN(x). */
2275 x = build_real_truncate (type, dconst_third ());
2276 break;
2277 default:
2278 gcc_unreachable ();
2279 }
2280 }
2281 (mult { x; } (logs @0)))))
2282
2283 /* logN(pow(x,exponent)) -> exponent*logN(x). */
2284 (for logs (LOG LOG2 LOG10)
2285 pows (POW)
2286 (simplify
2287 (logs (pows @0 @1))
2288 (mult @1 (logs @0))))
2289
2290 (for sqrts (SQRT)
2291 cbrts (CBRT)
2292 exps (EXP EXP2 EXP10 POW10)
2293 /* sqrt(expN(x)) -> expN(x*0.5). */
2294 (simplify
2295 (sqrts (exps @0))
2296 (exps (mult @0 { build_real (type, dconsthalf); })))
2297 /* cbrt(expN(x)) -> expN(x/3). */
2298 (simplify
2299 (cbrts (exps @0))
2300 (exps (mult @0 { build_real_truncate (type, dconst_third ()); })))))
2301
2302 /* Canonicalization of sequences of math builtins. These rules represent
2303 IL simplifications but are not necessarily optimizations.
2304
2305 The sincos pass is responsible for picking "optimal" implementations
2306 of math builtins, which may be more complicated and can sometimes go
2307 the other way, e.g. converting pow into a sequence of sqrts.
2308 We only want to do these canonicalizations before the pass has run. */
2309
2310 (if (flag_unsafe_math_optimizations && canonicalize_math_p ())
2311 /* Simplify tan(x) * cos(x) -> sin(x). */
2312 (simplify
2313 (mult:c (TAN:s @0) (COS:s @0))
2314 (SIN @0))
2315
2316 /* Simplify x * pow(x,c) -> pow(x,c+1). */
2317 (simplify
2318 (mult @0 (POW:s @0 REAL_CST@1))
2319 (if (!TREE_OVERFLOW (@1))
2320 (POW @0 (plus @1 { build_one_cst (type); }))))
2321
2322 /* Simplify sin(x) / cos(x) -> tan(x). */
2323 (simplify
2324 (rdiv (SIN:s @0) (COS:s @0))
2325 (TAN @0))
2326
2327 /* Simplify cos(x) / sin(x) -> 1 / tan(x). */
2328 (simplify
2329 (rdiv (COS:s @0) (SIN:s @0))
2330 (rdiv { build_one_cst (type); } (TAN @0)))
2331
2332 /* Simplify sin(x) / tan(x) -> cos(x). */
2333 (simplify
2334 (rdiv (SIN:s @0) (TAN:s @0))
2335 (if (! HONOR_NANS (@0)
2336 && ! HONOR_INFINITIES (@0))
2337 (cos @0)))
2338
2339 /* Simplify tan(x) / sin(x) -> 1.0 / cos(x). */
2340 (simplify
2341 (rdiv (TAN:s @0) (SIN:s @0))
2342 (if (! HONOR_NANS (@0)
2343 && ! HONOR_INFINITIES (@0))
2344 (rdiv { build_one_cst (type); } (COS @0))))
2345
2346 /* Simplify pow(x,y) * pow(x,z) -> pow(x,y+z). */
2347 (simplify
2348 (mult (POW:s @0 @1) (POW:s @0 @2))
2349 (POW @0 (plus @1 @2)))
2350
2351 /* Simplify pow(x,y) * pow(z,y) -> pow(x*z,y). */
2352 (simplify
2353 (mult (POW:s @0 @1) (POW:s @2 @1))
2354 (POW (mult @0 @2) @1))
2355
2356 /* Simplify pow(x,c) / x -> pow(x,c-1). */
2357 (simplify
2358 (rdiv (POW:s @0 REAL_CST@1) @0)
2359 (if (!TREE_OVERFLOW (@1))
2360 (POW @0 (minus @1 { build_one_cst (type); }))))
2361
2362 /* Simplify x / pow (y,z) -> x * pow(y,-z). */
2363 (simplify
2364 (rdiv @0 (POW:s @1 @2))
2365 (mult @0 (POW @1 (negate @2))))
2366
2367 (for sqrts (SQRT)
2368 cbrts (CBRT)
2369 pows (POW)
2370 /* sqrt(sqrt(x)) -> pow(x,1/4). */
2371 (simplify
2372 (sqrts (sqrts @0))
2373 (pows @0 { build_real (type, dconst_quarter ()); }))
2374 /* sqrt(cbrt(x)) -> pow(x,1/6). */
2375 (simplify
2376 (sqrts (cbrts @0))
2377 (pows @0 { build_real_truncate (type, dconst_sixth ()); }))
2378 /* cbrt(sqrt(x)) -> pow(x,1/6). */
2379 (simplify
2380 (cbrts (sqrts @0))
2381 (pows @0 { build_real_truncate (type, dconst_sixth ()); }))
2382 /* cbrt(cbrt(x)) -> pow(x,1/9), iff x is nonnegative. */
2383 (simplify
2384 (cbrts (cbrts tree_expr_nonnegative_p@0))
2385 (pows @0 { build_real_truncate (type, dconst_ninth ()); }))
2386 /* sqrt(pow(x,y)) -> pow(|x|,y*0.5). */
2387 (simplify
2388 (sqrts (pows @0 @1))
2389 (pows (abs @0) (mult @1 { build_real (type, dconsthalf); })))
2390 /* cbrt(pow(x,y)) -> pow(x,y/3), iff x is nonnegative. */
2391 (simplify
2392 (cbrts (pows tree_expr_nonnegative_p@0 @1))
2393 (pows @0 (mult @1 { build_real_truncate (type, dconst_third ()); })))))
2394
2395 /* If the real part is inf and the imag part is known to be
2396 nonnegative, return (inf + 0i). */
2397 (simplify
2398 (CPROJ (complex REAL_CST@0 tree_expr_nonnegative_p@1))
2399 (if (real_isinf (TREE_REAL_CST_PTR (@0)))
2400 (with
2401 {
2402 REAL_VALUE_TYPE rinf;
2403 real_inf (&rinf);
2404 }
2405 { build_complex (type, build_real (TREE_TYPE (type), rinf),
2406 build_zero_cst (TREE_TYPE (type))); })))
2407 /* If the imag part is inf, return (inf+I*copysign(0,imag)). */
2408 (simplify
2409 (CPROJ (complex @0 REAL_CST@1))
2410 (if (real_isinf (TREE_REAL_CST_PTR (@1)))
2411 (with
2412 {
2413 REAL_VALUE_TYPE rinf, rzero = dconst0;
2414 real_inf (&rinf);
2415 rzero.sign = TREE_REAL_CST_PTR (@1)->sign;
2416 }
2417 { build_complex (type, build_real (TREE_TYPE (type), rinf),
2418 build_real (TREE_TYPE (type), rzero)); })))
2419
2420
2421 /* Narrowing of arithmetic and logical operations.
2422
2423 These are conceptually similar to the transformations performed for
2424 the C/C++ front-ends by shorten_binary_op and shorten_compare. Long
2425 term we want to move all that code out of the front-ends into here. */
2426
2427 /* If we have a narrowing conversion of an arithmetic operation where
2428 both operands are widening conversions from the same type as the outer
2429 narrowing conversion. Then convert the innermost operands to a suitable
2430 unsigned type (to avoid introducing undefined behaviour), perform the
2431 operation and convert the result to the desired type. */
2432 (for op (plus minus)
2433 (simplify
2434 (convert (op:s (convert@2 @0) (convert@3 @1)))
2435 (if (INTEGRAL_TYPE_P (type)
2436 /* We check for type compatibility between @0 and @1 below,
2437 so there's no need to check that @1/@3 are integral types. */
2438 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
2439 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
2440 /* The precision of the type of each operand must match the
2441 precision of the mode of each operand, similarly for the
2442 result. */
2443 && (TYPE_PRECISION (TREE_TYPE (@0))
2444 == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (@0))))
2445 && (TYPE_PRECISION (TREE_TYPE (@1))
2446 == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (@1))))
2447 && TYPE_PRECISION (type) == GET_MODE_PRECISION (TYPE_MODE (type))
2448 /* The inner conversion must be a widening conversion. */
2449 && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
2450 && types_match (@0, @1)
2451 && types_match (@0, type))
2452 (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
2453 (convert (op @0 @1))
2454 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
2455 (convert (op (convert:utype @0) (convert:utype @1))))))))
2456
2457 /* This is another case of narrowing, specifically when there's an outer
2458 BIT_AND_EXPR which masks off bits outside the type of the innermost
2459 operands. Like the previous case we have to convert the operands
2460 to unsigned types to avoid introducing undefined behaviour for the
2461 arithmetic operation. */
2462 (for op (minus plus)
2463 (simplify
2464 (bit_and (op:s (convert@2 @0) (convert@3 @1)) INTEGER_CST@4)
2465 (if (INTEGRAL_TYPE_P (type)
2466 /* We check for type compatibility between @0 and @1 below,
2467 so there's no need to check that @1/@3 are integral types. */
2468 && INTEGRAL_TYPE_P (TREE_TYPE (@0))
2469 && INTEGRAL_TYPE_P (TREE_TYPE (@2))
2470 /* The precision of the type of each operand must match the
2471 precision of the mode of each operand, similarly for the
2472 result. */
2473 && (TYPE_PRECISION (TREE_TYPE (@0))
2474 == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (@0))))
2475 && (TYPE_PRECISION (TREE_TYPE (@1))
2476 == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (@1))))
2477 && TYPE_PRECISION (type) == GET_MODE_PRECISION (TYPE_MODE (type))
2478 /* The inner conversion must be a widening conversion. */
2479 && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
2480 && types_match (@0, @1)
2481 && (tree_int_cst_min_precision (@4, TYPE_SIGN (TREE_TYPE (@0)))
2482 <= TYPE_PRECISION (TREE_TYPE (@0)))
2483 && (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))
2484 || tree_int_cst_sgn (@4) >= 0))
2485 (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
2486 (with { tree ntype = TREE_TYPE (@0); }
2487 (convert (bit_and (op @0 @1) (convert:ntype @4))))
2488 (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
2489 (convert (bit_and (op (convert:utype @0) (convert:utype @1))
2490 (convert:utype @4))))))))
2491
2492 /* Transform (@0 < @1 and @0 < @2) to use min,
2493 (@0 > @1 and @0 > @2) to use max */
2494 (for op (lt le gt ge)
2495 ext (min min max max)
2496 (simplify
2497 (bit_and (op:s @0 @1) (op:s @0 @2))
2498 (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)))
2499 (op @0 (ext @1 @2)))))
2500