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