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