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