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