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