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