PR71206: inconsistent types after match.pd transformation
[official-gcc.git] / gcc / match.pd
blobf11ce7c9712f06a66783025272d103a731774107
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        && tree_nop_conversion_p (type, TREE_TYPE (@1)))
458    (lshift @0 @2)))
460 /* Fold (C1/X)*C2 into (C1*C2)/X.  */
461 (simplify
462  (mult (rdiv@3 REAL_CST@0 @1) REAL_CST@2)
463   (if (flag_associative_math
464        && single_use (@3))
465    (with
466     { tree tem = const_binop (MULT_EXPR, type, @0, @2); }
467     (if (tem)
468      (rdiv { tem; } @1)))))
470 /* Convert C1/(X*C2) into (C1/C2)/X  */
471 (simplify
472  (rdiv REAL_CST@0 (mult @1 REAL_CST@2))
473   (if (flag_reciprocal_math)
474    (with
475     { tree tem = const_binop (RDIV_EXPR, type, @0, @2); }
476     (if (tem)
477      (rdiv { tem; } @1)))))
479 /* Simplify ~X & X as zero.  */
480 (simplify
481  (bit_and:c (convert? @0) (convert? (bit_not @0)))
482   { build_zero_cst (type); })
484 /* Fold (A & ~B) - (A & B) into (A ^ B) - B.  */
485 (simplify
486  (minus (bit_and:cs @0 (bit_not @1)) (bit_and:cs @0 @1))
487   (minus (bit_xor @0 @1) @1))
488 (simplify
489  (minus (bit_and:s @0 INTEGER_CST@2) (bit_and:s @0 INTEGER_CST@1))
490  (if (wi::bit_not (@2) == @1)
491   (minus (bit_xor @0 @1) @1)))
493 /* Fold (A & B) - (A & ~B) into B - (A ^ B).  */
494 (simplify
495  (minus (bit_and:s @0 @1) (bit_and:cs @0 (bit_not @1)))
496   (minus @1 (bit_xor @0 @1)))
498 /* Simplify (X & ~Y) | (~X & Y) -> X ^ Y.  */
499 (simplify
500  (bit_ior (bit_and:c @0 (bit_not @1)) (bit_and:c (bit_not @0) @1))
501   (bit_xor @0 @1))
502 (simplify
503  (bit_ior:c (bit_and @0 INTEGER_CST@2) (bit_and (bit_not @0) INTEGER_CST@1))
504  (if (wi::bit_not (@2) == @1)
505   (bit_xor @0 @1)))
506 /* Simplify (~X & Y) to X ^ Y if we know that (X & ~Y) is 0.  */
507 #if GIMPLE
508 (simplify
509  (bit_and (bit_not SSA_NAME@0) INTEGER_CST@1)
510  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
511       && (get_nonzero_bits (@0) & wi::bit_not (@1)) == 0)
512   (bit_xor @0 @1)))
513 #endif
515 /* X % Y is smaller than Y.  */
516 (for cmp (lt ge)
517  (simplify
518   (cmp (trunc_mod @0 @1) @1)
519   (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
520    { constant_boolean_node (cmp == LT_EXPR, type); })))
521 (for cmp (gt le)
522  (simplify
523   (cmp @1 (trunc_mod @0 @1))
524   (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
525    { constant_boolean_node (cmp == GT_EXPR, type); })))
527 /* x | ~0 -> ~0  */
528 (simplify
529   (bit_ior @0 integer_all_onesp@1)
530   @1)
532 /* x & 0 -> 0  */
533 (simplify
534   (bit_and @0 integer_zerop@1)
535   @1)
537 /* ~x | x -> -1 */
538 /* ~x ^ x -> -1 */
539 /* ~x + x -> -1 */
540 (for op (bit_ior bit_xor plus)
541  (simplify
542   (op:c (convert? @0) (convert? (bit_not @0)))
543   (convert { build_all_ones_cst (TREE_TYPE (@0)); })))
545 /* x ^ x -> 0 */
546 (simplify
547   (bit_xor @0 @0)
548   { build_zero_cst (type); })
550 /* Canonicalize X ^ ~0 to ~X.  */
551 (simplify
552   (bit_xor @0 integer_all_onesp@1)
553   (bit_not @0))
555 /* x & ~0 -> x  */
556 (simplify
557  (bit_and @0 integer_all_onesp)
558   (non_lvalue @0))
560 /* x & x -> x,  x | x -> x  */
561 (for bitop (bit_and bit_ior)
562  (simplify
563   (bitop @0 @0)
564   (non_lvalue @0)))
566 /* x & C -> x if we know that x & ~C == 0.  */
567 #if GIMPLE
568 (simplify
569  (bit_and SSA_NAME@0 INTEGER_CST@1)
570  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
571       && (get_nonzero_bits (@0) & wi::bit_not (@1)) == 0)
572   @0))
573 #endif
575 /* x + (x & 1) -> (x + 1) & ~1 */
576 (simplify
577  (plus:c @0 (bit_and:s @0 integer_onep@1))
578  (bit_and (plus @0 @1) (bit_not @1)))
580 /* x & ~(x & y) -> x & ~y */
581 /* x | ~(x | y) -> x | ~y  */
582 (for bitop (bit_and bit_ior)
583  (simplify
584   (bitop:c @0 (bit_not (bitop:cs @0 @1)))
585   (bitop @0 (bit_not @1))))
587 /* (x | y) & ~x -> y & ~x */
588 /* (x & y) | ~x -> y | ~x */
589 (for bitop (bit_and bit_ior)
590      rbitop (bit_ior bit_and)
591  (simplify
592   (bitop:c (rbitop:c @0 @1) (bit_not@2 @0))
593   (bitop @1 @2)))
595 /* (x & y) ^ (x | y) -> x ^ y */
596 (simplify
597  (bit_xor:c (bit_and @0 @1) (bit_ior @0 @1))
598  (bit_xor @0 @1))
600 /* (x ^ y) ^ (x | y) -> x & y */
601 (simplify
602  (bit_xor:c (bit_xor @0 @1) (bit_ior @0 @1))
603  (bit_and @0 @1))
605 /* (x & y) + (x ^ y) -> x | y */
606 /* (x & y) | (x ^ y) -> x | y */
607 /* (x & y) ^ (x ^ y) -> x | y */
608 (for op (plus bit_ior bit_xor)
609  (simplify
610   (op:c (bit_and @0 @1) (bit_xor @0 @1))
611   (bit_ior @0 @1)))
613 /* (x & y) + (x | y) -> x + y */
614 (simplify
615  (plus:c (bit_and @0 @1) (bit_ior @0 @1))
616  (plus @0 @1))
618 /* (x + y) - (x | y) -> x & y */
619 (simplify
620  (minus (plus @0 @1) (bit_ior @0 @1))
621  (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
622       && !TYPE_SATURATING (type))
623   (bit_and @0 @1)))
625 /* (x + y) - (x & y) -> x | y */
626 (simplify
627  (minus (plus @0 @1) (bit_and @0 @1))
628  (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
629       && !TYPE_SATURATING (type))
630   (bit_ior @0 @1)))
632 /* (x | y) - (x ^ y) -> x & y */
633 (simplify
634  (minus (bit_ior @0 @1) (bit_xor @0 @1))
635  (bit_and @0 @1))
637 /* (x | y) - (x & y) -> x ^ y */
638 (simplify
639  (minus (bit_ior @0 @1) (bit_and @0 @1))
640  (bit_xor @0 @1))
642 /* (x | y) & ~(x & y) -> x ^ y */
643 (simplify
644  (bit_and:c (bit_ior @0 @1) (bit_not (bit_and @0 @1)))
645  (bit_xor @0 @1))
647 /* (x | y) & (~x ^ y) -> x & y */
648 (simplify
649  (bit_and:c (bit_ior:c @0 @1) (bit_xor:c @1 (bit_not @0)))
650  (bit_and @0 @1))
652 /* ~x & ~y -> ~(x | y)
653    ~x | ~y -> ~(x & y) */
654 (for op (bit_and bit_ior)
655      rop (bit_ior bit_and)
656  (simplify
657   (op (convert1? (bit_not @0)) (convert2? (bit_not @1)))
658   (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
659        && tree_nop_conversion_p (type, TREE_TYPE (@1)))
660    (bit_not (rop (convert @0) (convert @1))))))
662 /* If we are XORing or adding two BIT_AND_EXPR's, both of which are and'ing
663    with a constant, and the two constants have no bits in common,
664    we should treat this as a BIT_IOR_EXPR since this may produce more
665    simplifications.  */
666 (for op (bit_xor plus)
667  (simplify
668   (op (convert1? (bit_and@4 @0 INTEGER_CST@1))
669       (convert2? (bit_and@5 @2 INTEGER_CST@3)))
670   (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
671        && tree_nop_conversion_p (type, TREE_TYPE (@2))
672        && wi::bit_and (@1, @3) == 0)
673    (bit_ior (convert @4) (convert @5)))))
675 /* (X | Y) ^ X -> Y & ~ X*/
676 (simplify
677  (bit_xor:c (convert? (bit_ior:c @0 @1)) (convert? @0))
678  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
679   (convert (bit_and @1 (bit_not @0)))))
681 /* Convert ~X ^ ~Y to X ^ Y.  */
682 (simplify
683  (bit_xor (convert1? (bit_not @0)) (convert2? (bit_not @1)))
684  (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
685       && tree_nop_conversion_p (type, TREE_TYPE (@1)))
686   (bit_xor (convert @0) (convert @1))))
688 /* Convert ~X ^ C to X ^ ~C.  */
689 (simplify
690  (bit_xor (convert? (bit_not @0)) INTEGER_CST@1)
691  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
692   (bit_xor (convert @0) (bit_not @1))))
694 /* Fold (X & Y) ^ Y and (X ^ Y) & Y as ~X & Y.  */
695 (for opo (bit_and bit_xor)
696      opi (bit_xor bit_and)
697  (simplify
698   (opo:c (opi:c @0 @1) @1) 
699   (bit_and (bit_not @0) @1)))
701 /* Given a bit-wise operation CODE applied to ARG0 and ARG1, see if both
702    operands are another bit-wise operation with a common input.  If so,
703    distribute the bit operations to save an operation and possibly two if
704    constants are involved.  For example, convert
705      (A | B) & (A | C) into A | (B & C)
706    Further simplification will occur if B and C are constants.  */
707 (for op (bit_and bit_ior bit_xor)
708      rop (bit_ior bit_and bit_and)
709  (simplify
710   (op (convert? (rop:c @0 @1)) (convert? (rop:c @0 @2)))
711   (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
712        && tree_nop_conversion_p (type, TREE_TYPE (@2)))
713    (rop (convert @0) (op (convert @1) (convert @2))))))
715 /* Some simple reassociation for bit operations, also handled in reassoc.  */
716 /* (X & Y) & Y -> X & Y
717    (X | Y) | Y -> X | Y  */
718 (for op (bit_and bit_ior)
719  (simplify
720   (op:c (convert?@2 (op:c @0 @1)) (convert? @1))
721   @2))
722 /* (X ^ Y) ^ Y -> X  */
723 (simplify
724  (bit_xor:c (convert? (bit_xor:c @0 @1)) (convert? @1))
725  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
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    (lt @0 @1)))
904 (simplify
905   (ne (bit_ior:c (bit_not @0) @1) integer_zerop)
906   (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
907        && TYPE_PRECISION (TREE_TYPE (@1)) == 1)
908    (le @0 @1)))
910 /* ~~x -> x */
911 (simplify
912   (bit_not (bit_not @0))
913   @0)
915 /* Convert ~ (-A) to A - 1.  */
916 (simplify
917  (bit_not (convert? (negate @0)))
918  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
919   (convert (minus @0 { build_each_one_cst (TREE_TYPE (@0)); }))))
921 /* Convert ~ (A - 1) or ~ (A + -1) to -A.  */
922 (simplify
923  (bit_not (convert? (minus @0 integer_each_onep)))
924  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
925   (convert (negate @0))))
926 (simplify
927  (bit_not (convert? (plus @0 integer_all_onesp)))
928  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
929   (convert (negate @0))))
931 /* Part of convert ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify.  */
932 (simplify
933  (bit_not (convert? (bit_xor @0 INTEGER_CST@1)))
934  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
935   (convert (bit_xor @0 (bit_not @1)))))
936 (simplify
937  (bit_not (convert? (bit_xor:c (bit_not @0) @1)))
938  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
939   (convert (bit_xor @0 @1))))
941 /* (x & ~m) | (y & m) -> ((x ^ y) & m) ^ x */
942 (simplify
943  (bit_ior:c (bit_and:cs @0 (bit_not @2)) (bit_and:cs @1 @2))
944  (bit_xor (bit_and (bit_xor @0 @1) @2) @0))
946 /* Fold A - (A & B) into ~B & A.  */
947 (simplify
948  (minus (convert? @0) (convert?:s (bit_and:cs @0 @1)))
949  (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
950       && tree_nop_conversion_p (type, TREE_TYPE (@1)))
951   (convert (bit_and (bit_not @1) @0))))
955 /* ((X inner_op C0) outer_op C1)
956    With X being a tree where value_range has reasoned certain bits to always be
957    zero throughout its computed value range,
958    inner_op = {|,^}, outer_op = {|,^} and inner_op != outer_op
959    where zero_mask has 1's for all bits that are sure to be 0 in
960    and 0's otherwise.
961    if (inner_op == '^') C0 &= ~C1;
962    if ((C0 & ~zero_mask) == 0) then emit (X outer_op (C0 outer_op C1)
963    if ((C1 & ~zero_mask) == 0) then emit (X inner_op (C0 outer_op C1)
965 (for inner_op (bit_ior bit_xor)
966      outer_op (bit_xor bit_ior)
967 (simplify
968  (outer_op
969   (inner_op:s @2 INTEGER_CST@0) INTEGER_CST@1)
970  (with
971   {
972     bool fail = false;
973     wide_int zero_mask_not;
974     wide_int C0;
975     wide_int cst_emit;
977     if (TREE_CODE (@2) == SSA_NAME)
978       zero_mask_not = get_nonzero_bits (@2);
979     else
980       fail = true;
982     if (inner_op == BIT_XOR_EXPR)
983       {
984         C0 = wi::bit_and_not (@0, @1);
985         cst_emit = wi::bit_or (C0, @1);
986       }
987     else
988       {
989         C0 = @0;
990         cst_emit = wi::bit_xor (@0, @1);
991       }
992   }
993   (if (!fail && wi::bit_and (C0, zero_mask_not) == 0)
994    (outer_op @2 { wide_int_to_tree (type, cst_emit); })
995    (if (!fail && wi::bit_and (@1, zero_mask_not) == 0)
996     (inner_op @2 { wide_int_to_tree (type, cst_emit); }))))))
998 /* Associate (p +p off1) +p off2 as (p +p (off1 + off2)).  */
999 (simplify
1000   (pointer_plus (pointer_plus:s @0 @1) @3)
1001   (pointer_plus @0 (plus @1 @3)))
1003 /* Pattern match
1004      tem1 = (long) ptr1;
1005      tem2 = (long) ptr2;
1006      tem3 = tem2 - tem1;
1007      tem4 = (unsigned long) tem3;
1008      tem5 = ptr1 + tem4;
1009    and produce
1010      tem5 = ptr2;  */
1011 (simplify
1012   (pointer_plus @0 (convert?@2 (minus@3 (convert @1) (convert @0))))
1013   /* Conditionally look through a sign-changing conversion.  */
1014   (if (TYPE_PRECISION (TREE_TYPE (@2)) == TYPE_PRECISION (TREE_TYPE (@3))
1015        && ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@1)))
1016             || (GENERIC && type == TREE_TYPE (@1))))
1017    @1))
1019 /* Pattern match
1020      tem = (sizetype) ptr;
1021      tem = tem & algn;
1022      tem = -tem;
1023      ... = ptr p+ tem;
1024    and produce the simpler and easier to analyze with respect to alignment
1025      ... = ptr & ~algn;  */
1026 (simplify
1027   (pointer_plus @0 (negate (bit_and (convert @0) INTEGER_CST@1)))
1028   (with { tree algn = wide_int_to_tree (TREE_TYPE (@0), wi::bit_not (@1)); }
1029    (bit_and @0 { algn; })))
1031 /* Try folding difference of addresses.  */
1032 (simplify
1033  (minus (convert ADDR_EXPR@0) (convert @1))
1034  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1035   (with { HOST_WIDE_INT diff; }
1036    (if (ptr_difference_const (@0, @1, &diff))
1037     { build_int_cst_type (type, diff); }))))
1038 (simplify
1039  (minus (convert @0) (convert ADDR_EXPR@1))
1040  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1041   (with { HOST_WIDE_INT diff; }
1042    (if (ptr_difference_const (@0, @1, &diff))
1043     { build_int_cst_type (type, diff); }))))
1045 /* If arg0 is derived from the address of an object or function, we may
1046    be able to fold this expression using the object or function's
1047    alignment.  */
1048 (simplify
1049  (bit_and (convert? @0) INTEGER_CST@1)
1050  (if (POINTER_TYPE_P (TREE_TYPE (@0))
1051       && tree_nop_conversion_p (type, TREE_TYPE (@0)))
1052   (with
1053    {
1054      unsigned int align;
1055      unsigned HOST_WIDE_INT bitpos;
1056      get_pointer_alignment_1 (@0, &align, &bitpos);
1057    }
1058    (if (wi::ltu_p (@1, align / BITS_PER_UNIT))
1059     { wide_int_to_tree (type, wi::bit_and (@1, bitpos / BITS_PER_UNIT)); }))))
1062 /* We can't reassociate at all for saturating types.  */
1063 (if (!TYPE_SATURATING (type))
1065  /* Contract negates.  */
1066  /* A + (-B) -> A - B */
1067  (simplify
1068   (plus:c (convert1? @0) (convert2? (negate @1)))
1069   /* Apply STRIP_NOPS on @0 and the negate.  */
1070   (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
1071        && tree_nop_conversion_p (type, TREE_TYPE (@1))
1072        && !TYPE_OVERFLOW_SANITIZED (type))
1073    (minus (convert @0) (convert @1))))
1074  /* A - (-B) -> A + B */
1075  (simplify
1076   (minus (convert1? @0) (convert2? (negate @1)))
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    (plus (convert @0) (convert @1))))
1081  /* -(-A) -> A */
1082  (simplify
1083   (negate (convert? (negate @1)))
1084   (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1085        && !TYPE_OVERFLOW_SANITIZED (type))
1086    (convert @1)))
1088  /* We can't reassociate floating-point unless -fassociative-math
1089     or fixed-point plus or minus because of saturation to +-Inf.  */
1090  (if ((!FLOAT_TYPE_P (type) || flag_associative_math)
1091       && !FIXED_POINT_TYPE_P (type))
1093   /* Match patterns that allow contracting a plus-minus pair
1094      irrespective of overflow issues.  */
1095   /* (A +- B) - A       ->  +- B */
1096   /* (A +- B) -+ B      ->  A */
1097   /* A - (A +- B)       -> -+ B */
1098   /* A +- (B -+ A)      ->  +- B */
1099   (simplify
1100     (minus (plus:c @0 @1) @0)
1101     @1)
1102   (simplify
1103     (minus (minus @0 @1) @0)
1104     (negate @1))
1105   (simplify
1106     (plus:c (minus @0 @1) @1)
1107     @0)
1108   (simplify
1109    (minus @0 (plus:c @0 @1))
1110    (negate @1))
1111   (simplify
1112    (minus @0 (minus @0 @1))
1113    @1)
1115   /* (A +- CST) +- CST -> A + CST  */
1116   (for outer_op (plus minus)
1117    (for inner_op (plus minus)
1118     (simplify
1119      (outer_op (inner_op @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
1120      /* If the constant operation overflows we cannot do the transform
1121         as we would introduce undefined overflow, for example
1122         with (a - 1) + INT_MIN.  */
1123      (with { tree cst = const_binop (outer_op == inner_op
1124                                      ? PLUS_EXPR : MINUS_EXPR, type, @1, @2); }
1125       (if (cst && !TREE_OVERFLOW (cst))
1126        (inner_op @0 { cst; } ))))))
1128   /* (CST - A) +- CST -> CST - A  */
1129   (for outer_op (plus minus)
1130    (simplify
1131     (outer_op (minus CONSTANT_CLASS_P@1 @0) CONSTANT_CLASS_P@2)
1132     (with { tree cst = const_binop (outer_op, type, @1, @2); }
1133      (if (cst && !TREE_OVERFLOW (cst))
1134       (minus { cst; } @0)))))
1136   /* ~A + A -> -1 */
1137   (simplify
1138    (plus:c (bit_not @0) @0)
1139    (if (!TYPE_OVERFLOW_TRAPS (type))
1140     { build_all_ones_cst (type); }))
1142   /* ~A + 1 -> -A */
1143   (simplify
1144    (plus (convert? (bit_not @0)) integer_each_onep)
1145    (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1146     (negate (convert @0))))
1148   /* -A - 1 -> ~A */
1149   (simplify
1150    (minus (convert? (negate @0)) integer_each_onep)
1151    (if (!TYPE_OVERFLOW_TRAPS (type)
1152         && tree_nop_conversion_p (type, TREE_TYPE (@0)))
1153     (bit_not (convert @0))))
1155   /* -1 - A -> ~A */
1156   (simplify
1157    (minus integer_all_onesp @0)
1158    (bit_not @0))
1160   /* (T)(P + A) - (T)P -> (T) A */
1161   (for add (plus pointer_plus)
1162    (simplify
1163     (minus (convert (add @0 @1))
1164      (convert @0))
1165     (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
1166          /* For integer types, if A has a smaller type
1167             than T the result depends on the possible
1168             overflow in P + A.
1169             E.g. T=size_t, A=(unsigned)429497295, P>0.
1170             However, if an overflow in P + A would cause
1171             undefined behavior, we can assume that there
1172             is no overflow.  */
1173          || (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1174              && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1175          /* For pointer types, if the conversion of A to the
1176             final type requires a sign- or zero-extension,
1177             then we have to punt - it is not defined which
1178             one is correct.  */
1179          || (POINTER_TYPE_P (TREE_TYPE (@0))
1180              && TREE_CODE (@1) == INTEGER_CST
1181              && tree_int_cst_sign_bit (@1) == 0))
1182      (convert @1))))
1184   /* (T)P - (T)(P + A) -> -(T) A */
1185   (for add (plus pointer_plus)
1186    (simplify
1187     (minus (convert @0)
1188      (convert (add @0 @1)))
1189     (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
1190          /* For integer types, if A has a smaller type
1191             than T the result depends on the possible
1192             overflow in P + A.
1193             E.g. T=size_t, A=(unsigned)429497295, P>0.
1194             However, if an overflow in P + A would cause
1195             undefined behavior, we can assume that there
1196             is no overflow.  */
1197          || (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1198              && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1199          /* For pointer types, if the conversion of A to the
1200             final type requires a sign- or zero-extension,
1201             then we have to punt - it is not defined which
1202             one is correct.  */
1203          || (POINTER_TYPE_P (TREE_TYPE (@0))
1204              && TREE_CODE (@1) == INTEGER_CST
1205              && tree_int_cst_sign_bit (@1) == 0))
1206      (negate (convert @1)))))
1208   /* (T)(P + A) - (T)(P + B) -> (T)A - (T)B */
1209   (for add (plus pointer_plus)
1210    (simplify
1211     (minus (convert (add @0 @1))
1212      (convert (add @0 @2)))
1213     (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
1214          /* For integer types, if A has a smaller type
1215             than T the result depends on the possible
1216             overflow in P + A.
1217             E.g. T=size_t, A=(unsigned)429497295, P>0.
1218             However, if an overflow in P + A would cause
1219             undefined behavior, we can assume that there
1220             is no overflow.  */
1221          || (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1222              && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1223          /* For pointer types, if the conversion of A to the
1224             final type requires a sign- or zero-extension,
1225             then we have to punt - it is not defined which
1226             one is correct.  */
1227          || (POINTER_TYPE_P (TREE_TYPE (@0))
1228              && TREE_CODE (@1) == INTEGER_CST
1229              && tree_int_cst_sign_bit (@1) == 0
1230              && TREE_CODE (@2) == INTEGER_CST
1231              && tree_int_cst_sign_bit (@2) == 0))
1232      (minus (convert @1) (convert @2)))))))
1235 /* Simplifications of MIN_EXPR, MAX_EXPR, fmin() and fmax().  */
1237 (for minmax (min max FMIN FMAX)
1238  (simplify
1239   (minmax @0 @0)
1240   @0))
1241 /* min(max(x,y),y) -> y.  */
1242 (simplify
1243  (min:c (max:c @0 @1) @1)
1244  @1)
1245 /* max(min(x,y),y) -> y.  */
1246 (simplify
1247  (max:c (min:c @0 @1) @1)
1248  @1)
1249 (simplify
1250  (min @0 @1)
1251  (switch
1252   (if (INTEGRAL_TYPE_P (type)
1253        && TYPE_MIN_VALUE (type)
1254        && operand_equal_p (@1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
1255    @1)
1256   (if (INTEGRAL_TYPE_P (type)
1257        && TYPE_MAX_VALUE (type)
1258        && operand_equal_p (@1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
1259    @0)))
1260 (simplify
1261  (max @0 @1)
1262  (switch
1263   (if (INTEGRAL_TYPE_P (type)
1264        && TYPE_MAX_VALUE (type)
1265        && operand_equal_p (@1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
1266    @1)
1267   (if (INTEGRAL_TYPE_P (type)
1268        && TYPE_MIN_VALUE (type)
1269        && operand_equal_p (@1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
1270    @0)))
1271 (for minmax (FMIN FMAX)
1272  /* If either argument is NaN, return the other one.  Avoid the
1273     transformation if we get (and honor) a signalling NaN.  */
1274  (simplify
1275   (minmax:c @0 REAL_CST@1)
1276   (if (real_isnan (TREE_REAL_CST_PTR (@1))
1277        && (!HONOR_SNANS (@1) || !TREE_REAL_CST (@1).signalling))
1278    @0)))
1279 /* Convert fmin/fmax to MIN_EXPR/MAX_EXPR.  C99 requires these
1280    functions to return the numeric arg if the other one is NaN.
1281    MIN and MAX don't honor that, so only transform if -ffinite-math-only
1282    is set.  C99 doesn't require -0.0 to be handled, so we don't have to
1283    worry about it either.  */
1284 (if (flag_finite_math_only)
1285  (simplify
1286   (FMIN @0 @1)
1287   (min @0 @1))
1288  (simplify
1289   (FMAX @0 @1)
1290   (max @0 @1)))
1291 /* min (-A, -B) -> -max (A, B)  */
1292 (for minmax (min max FMIN FMAX)
1293      maxmin (max min FMAX FMIN)
1294  (simplify
1295   (minmax (negate:s@2 @0) (negate:s@3 @1))
1296   (if (FLOAT_TYPE_P (TREE_TYPE (@0))
1297        || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1298            && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
1299    (negate (maxmin @0 @1)))))
1300 /* MIN (~X, ~Y) -> ~MAX (X, Y)
1301    MAX (~X, ~Y) -> ~MIN (X, Y)  */
1302 (for minmax (min max)
1303  maxmin (max min)
1304  (simplify
1305   (minmax (bit_not:s@2 @0) (bit_not:s@3 @1))
1306   (bit_not (maxmin @0 @1))))
1308 /* Simplifications of shift and rotates.  */
1310 (for rotate (lrotate rrotate)
1311  (simplify
1312   (rotate integer_all_onesp@0 @1)
1313   @0))
1315 /* Optimize -1 >> x for arithmetic right shifts.  */
1316 (simplify
1317  (rshift integer_all_onesp@0 @1)
1318  (if (!TYPE_UNSIGNED (type)
1319       && tree_expr_nonnegative_p (@1))
1320   @0))
1322 /* Optimize (x >> c) << c into x & (-1<<c).  */
1323 (simplify
1324  (lshift (rshift @0 INTEGER_CST@1) @1)
1325  (if (wi::ltu_p (@1, element_precision (type)))
1326   (bit_and @0 (lshift { build_minus_one_cst (type); } @1))))
1328 /* Optimize (x << c) >> c into x & ((unsigned)-1 >> c) for unsigned
1329    types.  */
1330 (simplify
1331  (rshift (lshift @0 INTEGER_CST@1) @1)
1332  (if (TYPE_UNSIGNED (type)
1333       && (wi::ltu_p (@1, element_precision (type))))
1334   (bit_and @0 (rshift { build_minus_one_cst (type); } @1))))
1336 (for shiftrotate (lrotate rrotate lshift rshift)
1337  (simplify
1338   (shiftrotate @0 integer_zerop)
1339   (non_lvalue @0))
1340  (simplify
1341   (shiftrotate integer_zerop@0 @1)
1342   @0)
1343  /* Prefer vector1 << scalar to vector1 << vector2
1344     if vector2 is uniform.  */
1345  (for vec (VECTOR_CST CONSTRUCTOR)
1346   (simplify
1347    (shiftrotate @0 vec@1)
1348    (with { tree tem = uniform_vector_p (@1); }
1349     (if (tem)
1350      (shiftrotate @0 { tem; }))))))
1352 /* Rewrite an LROTATE_EXPR by a constant into an
1353    RROTATE_EXPR by a new constant.  */
1354 (simplify
1355  (lrotate @0 INTEGER_CST@1)
1356  (rrotate @0 { const_binop (MINUS_EXPR, TREE_TYPE (@1),
1357                             build_int_cst (TREE_TYPE (@1),
1358                                            element_precision (type)), @1); }))
1360 /* Turn (a OP c1) OP c2 into a OP (c1+c2).  */
1361 (for op (lrotate rrotate rshift lshift)
1362  (simplify
1363   (op (op @0 INTEGER_CST@1) INTEGER_CST@2)
1364   (with { unsigned int prec = element_precision (type); }
1365    (if (wi::ge_p (@1, 0, TYPE_SIGN (TREE_TYPE (@1)))
1366         && wi::lt_p (@1, prec, TYPE_SIGN (TREE_TYPE (@1)))
1367         && wi::ge_p (@2, 0, TYPE_SIGN (TREE_TYPE (@2)))
1368         && wi::lt_p (@2, prec, TYPE_SIGN (TREE_TYPE (@2))))
1369     (with { unsigned int low = wi::add (@1, @2).to_uhwi (); }
1370      /* Deal with a OP (c1 + c2) being undefined but (a OP c1) OP c2
1371         being well defined.  */
1372      (if (low >= prec)
1373       (if (op == LROTATE_EXPR || op == RROTATE_EXPR)
1374        (op @0 { build_int_cst (TREE_TYPE (@1), low % prec); })
1375        (if (TYPE_UNSIGNED (type) || op == LSHIFT_EXPR)
1376         { build_zero_cst (type); }
1377         (op @0 { build_int_cst (TREE_TYPE (@1), prec - 1); })))
1378       (op @0 { build_int_cst (TREE_TYPE (@1), low); })))))))
1381 /* ((1 << A) & 1) != 0 -> A == 0
1382    ((1 << A) & 1) == 0 -> A != 0 */
1383 (for cmp (ne eq)
1384      icmp (eq ne)
1385  (simplify
1386   (cmp (bit_and (lshift integer_onep @0) integer_onep) integer_zerop)
1387   (icmp @0 { build_zero_cst (TREE_TYPE (@0)); })))
1389 /* (CST1 << A) == CST2 -> A == ctz (CST2) - ctz (CST1)
1390    (CST1 << A) != CST2 -> A != ctz (CST2) - ctz (CST1)
1391    if CST2 != 0.  */
1392 (for cmp (ne eq)
1393  (simplify
1394   (cmp (lshift INTEGER_CST@0 @1) INTEGER_CST@2)
1395   (with { int cand = wi::ctz (@2) - wi::ctz (@0); }
1396    (if (cand < 0
1397         || (!integer_zerop (@2)
1398             && wi::ne_p (wi::lshift (@0, cand), @2)))
1399     { constant_boolean_node (cmp == NE_EXPR, type); }
1400     (if (!integer_zerop (@2)
1401          && wi::eq_p (wi::lshift (@0, cand), @2))
1402      (cmp @1 { build_int_cst (TREE_TYPE (@1), cand); }))))))
1404 /* Fold (X << C1) & C2 into (X << C1) & (C2 | ((1 << C1) - 1))
1405         (X >> C1) & C2 into (X >> C1) & (C2 | ~((type) -1 >> C1))
1406    if the new mask might be further optimized.  */
1407 (for shift (lshift rshift)
1408  (simplify
1409   (bit_and (convert?:s@4 (shift:s@5 (convert1?@3 @0) INTEGER_CST@1))
1410            INTEGER_CST@2)
1411    (if (tree_nop_conversion_p (TREE_TYPE (@4), TREE_TYPE (@5))
1412         && TYPE_PRECISION (type) <= HOST_BITS_PER_WIDE_INT
1413         && tree_fits_uhwi_p (@1)
1414         && tree_to_uhwi (@1) > 0
1415         && tree_to_uhwi (@1) < TYPE_PRECISION (type))
1416     (with
1417      {
1418        unsigned int shiftc = tree_to_uhwi (@1);
1419        unsigned HOST_WIDE_INT mask = TREE_INT_CST_LOW (@2);
1420        unsigned HOST_WIDE_INT newmask, zerobits = 0;
1421        tree shift_type = TREE_TYPE (@3);
1422        unsigned int prec;
1424        if (shift == LSHIFT_EXPR)
1425          zerobits = ((((unsigned HOST_WIDE_INT) 1) << shiftc) - 1);
1426        else if (shift == RSHIFT_EXPR
1427                 && (TYPE_PRECISION (shift_type)
1428                     == GET_MODE_PRECISION (TYPE_MODE (shift_type))))
1429          {
1430            prec = TYPE_PRECISION (TREE_TYPE (@3));
1431            tree arg00 = @0;
1432            /* See if more bits can be proven as zero because of
1433               zero extension.  */
1434            if (@3 != @0
1435                && TYPE_UNSIGNED (TREE_TYPE (@0)))
1436              {
1437                tree inner_type = TREE_TYPE (@0);
1438                if ((TYPE_PRECISION (inner_type)
1439                     == GET_MODE_PRECISION (TYPE_MODE (inner_type)))
1440                    && TYPE_PRECISION (inner_type) < prec)
1441                  {
1442                    prec = TYPE_PRECISION (inner_type);
1443                    /* See if we can shorten the right shift.  */
1444                    if (shiftc < prec)
1445                      shift_type = inner_type;
1446                    /* Otherwise X >> C1 is all zeros, so we'll optimize
1447                       it into (X, 0) later on by making sure zerobits
1448                       is all ones.  */
1449                  }
1450              }
1451            zerobits = ~(unsigned HOST_WIDE_INT) 0;
1452            if (shiftc < prec)
1453              {
1454                zerobits >>= HOST_BITS_PER_WIDE_INT - shiftc;
1455                zerobits <<= prec - shiftc;
1456              }
1457            /* For arithmetic shift if sign bit could be set, zerobits
1458               can contain actually sign bits, so no transformation is
1459               possible, unless MASK masks them all away.  In that
1460               case the shift needs to be converted into logical shift.  */
1461            if (!TYPE_UNSIGNED (TREE_TYPE (@3))
1462                && prec == TYPE_PRECISION (TREE_TYPE (@3)))
1463              {
1464                if ((mask & zerobits) == 0)
1465                  shift_type = unsigned_type_for (TREE_TYPE (@3));
1466                else
1467                  zerobits = 0;
1468              }
1469          }
1470      }
1471      /* ((X << 16) & 0xff00) is (X, 0).  */
1472      (if ((mask & zerobits) == mask)
1473       { build_int_cst (type, 0); }
1474       (with { newmask = mask | zerobits; }
1475        (if (newmask != mask && (newmask & (newmask + 1)) == 0)
1476         (with
1477          {
1478            /* Only do the transformation if NEWMASK is some integer
1479               mode's mask.  */
1480            for (prec = BITS_PER_UNIT;
1481                 prec < HOST_BITS_PER_WIDE_INT; prec <<= 1)
1482              if (newmask == (((unsigned HOST_WIDE_INT) 1) << prec) - 1)
1483                break;
1484          }
1485          (if (prec < HOST_BITS_PER_WIDE_INT
1486               || newmask == ~(unsigned HOST_WIDE_INT) 0)
1487           (with
1488            { tree newmaskt = build_int_cst_type (TREE_TYPE (@2), newmask); }
1489            (if (!tree_int_cst_equal (newmaskt, @2))
1490             (if (shift_type != TREE_TYPE (@3))
1491              (bit_and (convert (shift:shift_type (convert @3) @1)) { newmaskt; })
1492              (bit_and @4 { newmaskt; })))))))))))))
1494 /* Fold (X {&,^,|} C2) << C1 into (X << C1) {&,^,|} (C2 << C1)
1495    (X {&,^,|} C2) >> C1 into (X >> C1) & (C2 >> C1).  */
1496 (for shift (lshift rshift)
1497  (for bit_op (bit_and bit_xor bit_ior)
1498   (simplify
1499    (shift (convert?:s (bit_op:s @0 INTEGER_CST@2)) INTEGER_CST@1)
1500    (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1501     (with { tree mask = int_const_binop (shift, fold_convert (type, @2), @1); }
1502      (bit_op (shift (convert @0) @1) { mask; }))))))
1504 /* ~(~X >> Y) -> X >> Y (for arithmetic shift).  */
1505 (simplify
1506  (bit_not (convert1?:s (rshift:s (convert2?@0 (bit_not @1)) @2)))
1507   (if (!TYPE_UNSIGNED (TREE_TYPE (@0))
1508        && element_precision (TREE_TYPE (@0))
1509           <= element_precision (TREE_TYPE (@1))
1510        && element_precision (type) <= element_precision (TREE_TYPE (@0)))
1511    (with
1512     { tree shift_type = TREE_TYPE (@0); }
1513      (convert (rshift (convert:shift_type @1) @2)))))
1515 /* ~(~X >>r Y) -> X >>r Y
1516    ~(~X <<r Y) -> X <<r Y */
1517 (for rotate (lrotate rrotate)
1518  (simplify
1519   (bit_not (convert1?:s (rotate:s (convert2?@0 (bit_not @1)) @2)))
1520    (if (element_precision (TREE_TYPE (@0)) <= element_precision (TREE_TYPE (@1))
1521         && element_precision (type) <= element_precision (TREE_TYPE (@0)))
1522     (with
1523      { tree rotate_type = TREE_TYPE (@0); }
1524       (convert (rotate (convert:rotate_type @1) @2))))))
1526 /* Simplifications of conversions.  */
1528 /* Basic strip-useless-type-conversions / strip_nops.  */
1529 (for cvt (convert view_convert float fix_trunc)
1530  (simplify
1531   (cvt @0)
1532   (if ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@0)))
1533        || (GENERIC && type == TREE_TYPE (@0)))
1534    @0)))
1536 /* Contract view-conversions.  */
1537 (simplify
1538   (view_convert (view_convert @0))
1539   (view_convert @0))
1541 /* For integral conversions with the same precision or pointer
1542    conversions use a NOP_EXPR instead.  */
1543 (simplify
1544   (view_convert @0)
1545   (if ((INTEGRAL_TYPE_P (type) || POINTER_TYPE_P (type))
1546        && (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
1547        && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (@0)))
1548    (convert @0)))
1550 /* Strip inner integral conversions that do not change precision or size.  */
1551 (simplify
1552   (view_convert (convert@0 @1))
1553   (if ((INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
1554        && (INTEGRAL_TYPE_P (TREE_TYPE (@1)) || POINTER_TYPE_P (TREE_TYPE (@1)))
1555        && (TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
1556        && (TYPE_SIZE (TREE_TYPE (@0)) == TYPE_SIZE (TREE_TYPE (@1))))
1557    (view_convert @1)))
1559 /* Re-association barriers around constants and other re-association
1560    barriers can be removed.  */
1561 (simplify
1562  (paren CONSTANT_CLASS_P@0)
1563  @0)
1564 (simplify
1565  (paren (paren@1 @0))
1566  @1)
1568 /* Handle cases of two conversions in a row.  */
1569 (for ocvt (convert float fix_trunc)
1570  (for icvt (convert float)
1571   (simplify
1572    (ocvt (icvt@1 @0))
1573    (with
1574     {
1575       tree inside_type = TREE_TYPE (@0);
1576       tree inter_type = TREE_TYPE (@1);
1577       int inside_int = INTEGRAL_TYPE_P (inside_type);
1578       int inside_ptr = POINTER_TYPE_P (inside_type);
1579       int inside_float = FLOAT_TYPE_P (inside_type);
1580       int inside_vec = VECTOR_TYPE_P (inside_type);
1581       unsigned int inside_prec = TYPE_PRECISION (inside_type);
1582       int inside_unsignedp = TYPE_UNSIGNED (inside_type);
1583       int inter_int = INTEGRAL_TYPE_P (inter_type);
1584       int inter_ptr = POINTER_TYPE_P (inter_type);
1585       int inter_float = FLOAT_TYPE_P (inter_type);
1586       int inter_vec = VECTOR_TYPE_P (inter_type);
1587       unsigned int inter_prec = TYPE_PRECISION (inter_type);
1588       int inter_unsignedp = TYPE_UNSIGNED (inter_type);
1589       int final_int = INTEGRAL_TYPE_P (type);
1590       int final_ptr = POINTER_TYPE_P (type);
1591       int final_float = FLOAT_TYPE_P (type);
1592       int final_vec = VECTOR_TYPE_P (type);
1593       unsigned int final_prec = TYPE_PRECISION (type);
1594       int final_unsignedp = TYPE_UNSIGNED (type);
1595     }
1596    (switch
1597     /* In addition to the cases of two conversions in a row
1598        handled below, if we are converting something to its own
1599        type via an object of identical or wider precision, neither
1600        conversion is needed.  */
1601     (if (((GIMPLE && useless_type_conversion_p (type, inside_type))
1602           || (GENERIC
1603               && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (inside_type)))
1604          && (((inter_int || inter_ptr) && final_int)
1605              || (inter_float && final_float))
1606          && inter_prec >= final_prec)
1607      (ocvt @0))
1609     /* Likewise, if the intermediate and initial types are either both
1610        float or both integer, we don't need the middle conversion if the
1611        former is wider than the latter and doesn't change the signedness
1612        (for integers).  Avoid this if the final type is a pointer since
1613        then we sometimes need the middle conversion.  Likewise if the
1614        final type has a precision not equal to the size of its mode.  */
1615     (if (((inter_int && inside_int) || (inter_float && inside_float))
1616          && (final_int || final_float)
1617          && inter_prec >= inside_prec
1618          && (inter_float || inter_unsignedp == inside_unsignedp)
1619          && ! (final_prec != GET_MODE_PRECISION (TYPE_MODE (type))
1620                && TYPE_MODE (type) == TYPE_MODE (inter_type)))
1621      (ocvt @0))
1623     /* If we have a sign-extension of a zero-extended value, we can
1624        replace that by a single zero-extension.  Likewise if the
1625        final conversion does not change precision we can drop the
1626        intermediate conversion.  */
1627     (if (inside_int && inter_int && final_int
1628          && ((inside_prec < inter_prec && inter_prec < final_prec
1629               && inside_unsignedp && !inter_unsignedp)
1630              || final_prec == inter_prec))
1631      (ocvt @0))
1633     /* Two conversions in a row are not needed unless:
1634         - some conversion is floating-point (overstrict for now), or
1635         - some conversion is a vector (overstrict for now), or
1636         - the intermediate type is narrower than both initial and
1637           final, or
1638         - the intermediate type and innermost type differ in signedness,
1639           and the outermost type is wider than the intermediate, or
1640         - the initial type is a pointer type and the precisions of the
1641           intermediate and final types differ, or
1642         - the final type is a pointer type and the precisions of the
1643           initial and intermediate types differ.  */
1644     (if (! inside_float && ! inter_float && ! final_float
1645          && ! inside_vec && ! inter_vec && ! final_vec
1646          && (inter_prec >= inside_prec || inter_prec >= final_prec)
1647          && ! (inside_int && inter_int
1648                && inter_unsignedp != inside_unsignedp
1649                && inter_prec < final_prec)
1650          && ((inter_unsignedp && inter_prec > inside_prec)
1651              == (final_unsignedp && final_prec > inter_prec))
1652          && ! (inside_ptr && inter_prec != final_prec)
1653          && ! (final_ptr && inside_prec != inter_prec)
1654          && ! (final_prec != GET_MODE_PRECISION (TYPE_MODE (type))
1655                && TYPE_MODE (type) == TYPE_MODE (inter_type)))
1656      (ocvt @0))
1658     /* A truncation to an unsigned type (a zero-extension) should be
1659        canonicalized as bitwise and of a mask.  */
1660     (if (GIMPLE /* PR70366: doing this in GENERIC breaks -Wconversion.  */
1661          && final_int && inter_int && inside_int
1662          && final_prec == inside_prec
1663          && final_prec > inter_prec
1664          && inter_unsignedp)
1665      (convert (bit_and @0 { wide_int_to_tree
1666                               (inside_type,
1667                                wi::mask (inter_prec, false,
1668                                          TYPE_PRECISION (inside_type))); })))
1670     /* If we are converting an integer to a floating-point that can
1671        represent it exactly and back to an integer, we can skip the
1672        floating-point conversion.  */
1673     (if (GIMPLE /* PR66211 */
1674          && inside_int && inter_float && final_int &&
1675          (unsigned) significand_size (TYPE_MODE (inter_type))
1676          >= inside_prec - !inside_unsignedp)
1677      (convert @0)))))))
1679 /* If we have a narrowing conversion to an integral type that is fed by a
1680    BIT_AND_EXPR, we might be able to remove the BIT_AND_EXPR if it merely
1681    masks off bits outside the final type (and nothing else).  */
1682 (simplify
1683   (convert (bit_and @0 INTEGER_CST@1))
1684   (if (INTEGRAL_TYPE_P (type)
1685        && INTEGRAL_TYPE_P (TREE_TYPE (@0))
1686        && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))
1687        && operand_equal_p (@1, build_low_bits_mask (TREE_TYPE (@1),
1688                                                     TYPE_PRECISION (type)), 0))
1689    (convert @0)))
1692 /* (X /[ex] A) * A -> X.  */
1693 (simplify
1694   (mult (convert? (exact_div @0 @1)) @1)
1695   /* Look through a sign-changing conversion.  */
1696   (convert @0))
1698 /* Canonicalization of binary operations.  */
1700 /* Convert X + -C into X - C.  */
1701 (simplify
1702  (plus @0 REAL_CST@1)
1703  (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
1704   (with { tree tem = const_unop (NEGATE_EXPR, type, @1); }
1705    (if (!TREE_OVERFLOW (tem) || !flag_trapping_math)
1706     (minus @0 { tem; })))))
1708 /* Convert x+x into x*2.  */
1709 (simplify
1710  (plus @0 @0)
1711  (if (SCALAR_FLOAT_TYPE_P (type))
1712   (mult @0 { build_real (type, dconst2); })
1713   (if (INTEGRAL_TYPE_P (type))
1714    (mult @0 { build_int_cst (type, 2); }))))
1716 (simplify
1717  (minus integer_zerop @1)
1718  (negate @1))
1720 /* (ARG0 - ARG1) is the same as (-ARG1 + ARG0).  So check whether
1721    ARG0 is zero and X + ARG0 reduces to X, since that would mean
1722    (-ARG1 + ARG0) reduces to -ARG1.  */
1723 (simplify
1724  (minus real_zerop@0 @1)
1725  (if (fold_real_zero_addition_p (type, @0, 0))
1726   (negate @1)))
1728 /* Transform x * -1 into -x.  */
1729 (simplify
1730  (mult @0 integer_minus_onep)
1731  (negate @0))
1733 /* True if we can easily extract the real and imaginary parts of a complex
1734    number.  */
1735 (match compositional_complex
1736  (convert? (complex @0 @1)))
1738 /* COMPLEX_EXPR and REALPART/IMAGPART_EXPR cancellations.  */
1739 (simplify
1740  (complex (realpart @0) (imagpart @0))
1741  @0)
1742 (simplify
1743  (realpart (complex @0 @1))
1744  @0)
1745 (simplify
1746  (imagpart (complex @0 @1))
1747  @1)
1749 /* Sometimes we only care about half of a complex expression.  */
1750 (simplify
1751  (realpart (convert?:s (conj:s @0)))
1752  (convert (realpart @0)))
1753 (simplify
1754  (imagpart (convert?:s (conj:s @0)))
1755  (convert (negate (imagpart @0))))
1756 (for part (realpart imagpart)
1757  (for op (plus minus)
1758   (simplify
1759    (part (convert?:s@2 (op:s @0 @1)))
1760    (convert (op (part @0) (part @1))))))
1761 (simplify
1762  (realpart (convert?:s (CEXPI:s @0)))
1763  (convert (COS @0)))
1764 (simplify
1765  (imagpart (convert?:s (CEXPI:s @0)))
1766  (convert (SIN @0)))
1768 /* conj(conj(x)) -> x  */
1769 (simplify
1770  (conj (convert? (conj @0)))
1771  (if (tree_nop_conversion_p (TREE_TYPE (@0), type))
1772   (convert @0)))
1774 /* conj({x,y}) -> {x,-y}  */
1775 (simplify
1776  (conj (convert?:s (complex:s @0 @1)))
1777  (with { tree itype = TREE_TYPE (type); }
1778   (complex (convert:itype @0) (negate (convert:itype @1)))))
1780 /* BSWAP simplifications, transforms checked by gcc.dg/builtin-bswap-8.c.  */
1781 (for bswap (BUILT_IN_BSWAP16 BUILT_IN_BSWAP32 BUILT_IN_BSWAP64)
1782  (simplify
1783   (bswap (bswap @0))
1784   @0)
1785  (simplify
1786   (bswap (bit_not (bswap @0)))
1787   (bit_not @0))
1788  (for bitop (bit_xor bit_ior bit_and)
1789   (simplify
1790    (bswap (bitop:c (bswap @0) @1))
1791    (bitop @0 (bswap @1)))))
1794 /* Combine COND_EXPRs and VEC_COND_EXPRs.  */
1796 /* Simplify constant conditions.
1797    Only optimize constant conditions when the selected branch
1798    has the same type as the COND_EXPR.  This avoids optimizing
1799    away "c ? x : throw", where the throw has a void type.
1800    Note that we cannot throw away the fold-const.c variant nor
1801    this one as we depend on doing this transform before possibly
1802    A ? B : B -> B triggers and the fold-const.c one can optimize
1803    0 ? A : B to B even if A has side-effects.  Something
1804    genmatch cannot handle.  */
1805 (simplify
1806  (cond INTEGER_CST@0 @1 @2)
1807  (if (integer_zerop (@0))
1808   (if (!VOID_TYPE_P (TREE_TYPE (@2)) || VOID_TYPE_P (type))
1809    @2)
1810   (if (!VOID_TYPE_P (TREE_TYPE (@1)) || VOID_TYPE_P (type))
1811    @1)))
1812 (simplify
1813  (vec_cond VECTOR_CST@0 @1 @2)
1814  (if (integer_all_onesp (@0))
1815   @1
1816   (if (integer_zerop (@0))
1817    @2)))
1819 (for cnd (cond vec_cond)
1820  /* A ? B : (A ? X : C) -> A ? B : C.  */
1821  (simplify
1822   (cnd @0 (cnd @0 @1 @2) @3)
1823   (cnd @0 @1 @3))
1824  (simplify
1825   (cnd @0 @1 (cnd @0 @2 @3))
1826   (cnd @0 @1 @3))
1827  /* A ? B : (!A ? C : X) -> A ? B : C.  */
1828  /* ???  This matches embedded conditions open-coded because genmatch
1829     would generate matching code for conditions in separate stmts only.
1830     The following is still important to merge then and else arm cases
1831     from if-conversion.  */
1832  (simplify
1833   (cnd @0 @1 (cnd @2 @3 @4))
1834   (if (COMPARISON_CLASS_P (@0)
1835        && COMPARISON_CLASS_P (@2)
1836        && invert_tree_comparison
1837            (TREE_CODE (@0), HONOR_NANS (TREE_OPERAND (@0, 0))) == TREE_CODE (@2)
1838        && operand_equal_p (TREE_OPERAND (@0, 0), TREE_OPERAND (@2, 0), 0)
1839        && operand_equal_p (TREE_OPERAND (@0, 1), TREE_OPERAND (@2, 1), 0))
1840    (cnd @0 @1 @3)))
1841  (simplify
1842   (cnd @0 (cnd @1 @2 @3) @4)
1843   (if (COMPARISON_CLASS_P (@0)
1844        && COMPARISON_CLASS_P (@1)
1845        && invert_tree_comparison
1846            (TREE_CODE (@0), HONOR_NANS (TREE_OPERAND (@0, 0))) == TREE_CODE (@1)
1847        && operand_equal_p (TREE_OPERAND (@0, 0), TREE_OPERAND (@1, 0), 0)
1848        && operand_equal_p (TREE_OPERAND (@0, 1), TREE_OPERAND (@1, 1), 0))
1849    (cnd @0 @3 @4)))
1851  /* A ? B : B -> B.  */
1852  (simplify
1853   (cnd @0 @1 @1)
1854   @1)
1856  /* !A ? B : C -> A ? C : B.  */
1857  (simplify
1858   (cnd (logical_inverted_value truth_valued_p@0) @1 @2)
1859   (cnd @0 @2 @1)))
1861 /* A + (B vcmp C ? 1 : 0) -> A - (B vcmp C ? -1 : 0), since vector comparisons
1862    return all -1 or all 0 results.  */
1863 /* ??? We could instead convert all instances of the vec_cond to negate,
1864    but that isn't necessarily a win on its own.  */
1865 (simplify
1866  (plus:c @3 (view_convert? (vec_cond:s @0 integer_each_onep@1 integer_zerop@2)))
1867  (if (VECTOR_TYPE_P (type)
1868       && TYPE_VECTOR_SUBPARTS (type) == TYPE_VECTOR_SUBPARTS (TREE_TYPE (@1))
1869       && (TYPE_MODE (TREE_TYPE (type))
1870           == TYPE_MODE (TREE_TYPE (TREE_TYPE (@1)))))
1871   (minus @3 (view_convert (vec_cond @0 (negate @1) @2)))))
1873 /* ... likewise A - (B vcmp C ? 1 : 0) -> A + (B vcmp C ? -1 : 0).  */
1874 (simplify
1875  (minus @3 (view_convert? (vec_cond:s @0 integer_each_onep@1 integer_zerop@2)))
1876  (if (VECTOR_TYPE_P (type)
1877       && TYPE_VECTOR_SUBPARTS (type) == TYPE_VECTOR_SUBPARTS (TREE_TYPE (@1))
1878       && (TYPE_MODE (TREE_TYPE (type))
1879           == TYPE_MODE (TREE_TYPE (TREE_TYPE (@1)))))
1880   (plus @3 (view_convert (vec_cond @0 (negate @1) @2)))))
1883 /* Simplifications of comparisons.  */
1885 /* See if we can reduce the magnitude of a constant involved in a
1886    comparison by changing the comparison code.  This is a canonicalization
1887    formerly done by maybe_canonicalize_comparison_1.  */
1888 (for cmp  (le gt)
1889      acmp (lt ge)
1890  (simplify
1891   (cmp @0 INTEGER_CST@1)
1892   (if (tree_int_cst_sgn (@1) == -1)
1893    (acmp @0 { wide_int_to_tree (TREE_TYPE (@1), wi::add (@1, 1)); }))))
1894 (for cmp  (ge lt)
1895      acmp (gt le)
1896  (simplify
1897   (cmp @0 INTEGER_CST@1)
1898   (if (tree_int_cst_sgn (@1) == 1)
1899    (acmp @0 { wide_int_to_tree (TREE_TYPE (@1), wi::sub (@1, 1)); }))))
1902 /* We can simplify a logical negation of a comparison to the
1903    inverted comparison.  As we cannot compute an expression
1904    operator using invert_tree_comparison we have to simulate
1905    that with expression code iteration.  */
1906 (for cmp (tcc_comparison)
1907      icmp (inverted_tcc_comparison)
1908      ncmp (inverted_tcc_comparison_with_nans)
1909  /* Ideally we'd like to combine the following two patterns
1910     and handle some more cases by using
1911       (logical_inverted_value (cmp @0 @1))
1912     here but for that genmatch would need to "inline" that.
1913     For now implement what forward_propagate_comparison did.  */
1914  (simplify
1915   (bit_not (cmp @0 @1))
1916   (if (VECTOR_TYPE_P (type)
1917        || (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1))
1918    /* Comparison inversion may be impossible for trapping math,
1919       invert_tree_comparison will tell us.  But we can't use
1920       a computed operator in the replacement tree thus we have
1921       to play the trick below.  */
1922    (with { enum tree_code ic = invert_tree_comparison
1923              (cmp, HONOR_NANS (@0)); }
1924     (if (ic == icmp)
1925      (icmp @0 @1)
1926      (if (ic == ncmp)
1927       (ncmp @0 @1))))))
1928  (simplify
1929   (bit_xor (cmp @0 @1) integer_truep)
1930   (with { enum tree_code ic = invert_tree_comparison
1931             (cmp, HONOR_NANS (@0)); }
1932    (if (ic == icmp)
1933     (icmp @0 @1)
1934     (if (ic == ncmp)
1935      (ncmp @0 @1))))))
1937 /* Transform comparisons of the form X - Y CMP 0 to X CMP Y.
1938    ??? The transformation is valid for the other operators if overflow
1939    is undefined for the type, but performing it here badly interacts
1940    with the transformation in fold_cond_expr_with_comparison which
1941    attempts to synthetize ABS_EXPR.  */
1942 (for cmp (eq ne)
1943  (simplify
1944   (cmp (minus@2 @0 @1) integer_zerop)
1945   (if (single_use (@2))
1946    (cmp @0 @1))))
1948 /* Transform comparisons of the form X * C1 CMP 0 to X CMP 0 in the
1949    signed arithmetic case.  That form is created by the compiler
1950    often enough for folding it to be of value.  One example is in
1951    computing loop trip counts after Operator Strength Reduction.  */
1952 (for cmp (simple_comparison)
1953      scmp (swapped_simple_comparison)
1954  (simplify
1955   (cmp (mult@3 @0 INTEGER_CST@1) integer_zerop@2)
1956   /* Handle unfolded multiplication by zero.  */
1957   (if (integer_zerop (@1))
1958    (cmp @1 @2)
1959    (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1960         && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1961         && single_use (@3))
1962     /* If @1 is negative we swap the sense of the comparison.  */
1963     (if (tree_int_cst_sgn (@1) < 0)
1964      (scmp @0 @2)
1965      (cmp @0 @2))))))
1967 /* Simplify comparison of something with itself.  For IEEE
1968    floating-point, we can only do some of these simplifications.  */
1969 (for cmp (eq ge le)
1970  (simplify
1971   (cmp @0 @0)
1972   (if (! FLOAT_TYPE_P (TREE_TYPE (@0))
1973        || ! HONOR_NANS (@0))
1974    { constant_boolean_node (true, type); }
1975    (if (cmp != EQ_EXPR)
1976     (eq @0 @0)))))
1977 (for cmp (ne gt lt)
1978  (simplify
1979   (cmp @0 @0)
1980   (if (cmp != NE_EXPR
1981        || ! FLOAT_TYPE_P (TREE_TYPE (@0))
1982        || ! HONOR_NANS (@0))
1983    { constant_boolean_node (false, type); })))
1984 (for cmp (unle unge uneq)
1985  (simplify
1986   (cmp @0 @0)
1987   { constant_boolean_node (true, type); }))
1988 (for cmp (unlt ungt)
1989  (simplify
1990   (cmp @0 @0)
1991   (unordered @0 @0)))
1992 (simplify
1993  (ltgt @0 @0)
1994  (if (!flag_trapping_math)
1995   { constant_boolean_node (false, type); }))
1997 /* Fold ~X op ~Y as Y op X.  */
1998 (for cmp (simple_comparison)
1999  (simplify
2000   (cmp (bit_not@2 @0) (bit_not@3 @1))
2001   (if (single_use (@2) && single_use (@3))
2002    (cmp @1 @0))))
2004 /* Fold ~X op C as X op' ~C, where op' is the swapped comparison.  */
2005 (for cmp (simple_comparison)
2006      scmp (swapped_simple_comparison)
2007  (simplify
2008   (cmp (bit_not@2 @0) CONSTANT_CLASS_P@1)
2009   (if (single_use (@2)
2010        && (TREE_CODE (@1) == INTEGER_CST || TREE_CODE (@1) == VECTOR_CST))
2011    (scmp @0 (bit_not @1)))))
2013 (for cmp (simple_comparison)
2014  /* Fold (double)float1 CMP (double)float2 into float1 CMP float2.  */
2015  (simplify
2016   (cmp (convert@2 @0) (convert? @1))
2017   (if (FLOAT_TYPE_P (TREE_TYPE (@0))
2018        && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@2))
2019            == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0)))
2020        && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@2))
2021            == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1))))
2022    (with
2023     {
2024       tree type1 = TREE_TYPE (@1);
2025       if (TREE_CODE (@1) == REAL_CST && !DECIMAL_FLOAT_TYPE_P (type1))
2026         {
2027           REAL_VALUE_TYPE orig = TREE_REAL_CST (@1);
2028           if (TYPE_PRECISION (type1) > TYPE_PRECISION (float_type_node)
2029               && exact_real_truncate (TYPE_MODE (float_type_node), &orig))
2030             type1 = float_type_node;
2031           if (TYPE_PRECISION (type1) > TYPE_PRECISION (double_type_node)
2032               && exact_real_truncate (TYPE_MODE (double_type_node), &orig))
2033             type1 = double_type_node;
2034         }
2035       tree newtype
2036         = (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type1)
2037            ? TREE_TYPE (@0) : type1); 
2038     }
2039     (if (TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (newtype))
2040      (cmp (convert:newtype @0) (convert:newtype @1))))))
2042  (simplify
2043   (cmp @0 REAL_CST@1)
2044   /* IEEE doesn't distinguish +0 and -0 in comparisons.  */
2045   (switch
2046    /* a CMP (-0) -> a CMP 0  */
2047    (if (REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@1)))
2048     (cmp @0 { build_real (TREE_TYPE (@1), dconst0); }))
2049    /* x != NaN is always true, other ops are always false.  */
2050    (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
2051         && ! HONOR_SNANS (@1))
2052     { constant_boolean_node (cmp == NE_EXPR, type); })
2053    /* Fold comparisons against infinity.  */
2054    (if (REAL_VALUE_ISINF (TREE_REAL_CST (@1))
2055         && MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (@1))))
2056     (with
2057      {
2058        REAL_VALUE_TYPE max;
2059        enum tree_code code = cmp;
2060        bool neg = REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1));
2061        if (neg)
2062          code = swap_tree_comparison (code);
2063      }
2064      (switch
2065       /* x > +Inf is always false, if with ignore sNANs.  */
2066       (if (code == GT_EXPR
2067            && ! HONOR_SNANS (@0))
2068        { constant_boolean_node (false, type); })
2069       (if (code == LE_EXPR)
2070        /* x <= +Inf is always true, if we don't case about NaNs.  */
2071        (if (! HONOR_NANS (@0))
2072         { constant_boolean_node (true, type); }
2073         /* x <= +Inf is the same as x == x, i.e. !isnan(x).  */
2074         (eq @0 @0)))
2075       /* x == +Inf and x >= +Inf are always equal to x > DBL_MAX.  */
2076       (if (code == EQ_EXPR || code == GE_EXPR)
2077        (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
2078         (if (neg)
2079          (lt @0 { build_real (TREE_TYPE (@0), max); })
2080          (gt @0 { build_real (TREE_TYPE (@0), max); }))))
2081       /* x < +Inf is always equal to x <= DBL_MAX.  */
2082       (if (code == LT_EXPR)
2083        (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
2084         (if (neg)
2085          (ge @0 { build_real (TREE_TYPE (@0), max); })
2086          (le @0 { build_real (TREE_TYPE (@0), max); }))))
2087       /* x != +Inf is always equal to !(x > DBL_MAX).  */
2088       (if (code == NE_EXPR)
2089        (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
2090         (if (! HONOR_NANS (@0))
2091          (if (neg)
2092           (ge @0 { build_real (TREE_TYPE (@0), max); })
2093           (le @0 { build_real (TREE_TYPE (@0), max); }))
2094          (if (neg)
2095           (bit_xor (lt @0 { build_real (TREE_TYPE (@0), max); })
2096            { build_one_cst (type); })
2097           (bit_xor (gt @0 { build_real (TREE_TYPE (@0), max); })
2098            { build_one_cst (type); }))))))))))
2100  /* If this is a comparison of a real constant with a PLUS_EXPR
2101     or a MINUS_EXPR of a real constant, we can convert it into a
2102     comparison with a revised real constant as long as no overflow
2103     occurs when unsafe_math_optimizations are enabled.  */
2104  (if (flag_unsafe_math_optimizations)
2105   (for op (plus minus)
2106    (simplify
2107     (cmp (op @0 REAL_CST@1) REAL_CST@2)
2108     (with
2109      {
2110        tree tem = const_binop (op == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR,
2111                                TREE_TYPE (@1), @2, @1);
2112      }
2113      (if (tem && !TREE_OVERFLOW (tem))
2114       (cmp @0 { tem; }))))))
2116  /* Likewise, we can simplify a comparison of a real constant with
2117     a MINUS_EXPR whose first operand is also a real constant, i.e.
2118     (c1 - x) < c2 becomes x > c1-c2.  Reordering is allowed on
2119     floating-point types only if -fassociative-math is set.  */
2120  (if (flag_associative_math)
2121   (simplify
2122    (cmp (minus REAL_CST@0 @1) REAL_CST@2)
2123    (with { tree tem = const_binop (MINUS_EXPR, TREE_TYPE (@1), @0, @2); }
2124     (if (tem && !TREE_OVERFLOW (tem))
2125      (cmp { tem; } @1)))))
2127  /* Fold comparisons against built-in math functions.  */
2128  (if (flag_unsafe_math_optimizations
2129       && ! flag_errno_math)
2130   (for sq (SQRT)
2131    (simplify
2132     (cmp (sq @0) REAL_CST@1)
2133     (switch
2134      (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
2135       (switch
2136        /* sqrt(x) < y is always false, if y is negative.  */
2137        (if (cmp == EQ_EXPR || cmp == LT_EXPR || cmp == LE_EXPR)
2138         { constant_boolean_node (false, type); })
2139        /* sqrt(x) > y is always true, if y is negative and we
2140           don't care about NaNs, i.e. negative values of x.  */
2141        (if (cmp == NE_EXPR || !HONOR_NANS (@0))
2142         { constant_boolean_node (true, type); })
2143        /* sqrt(x) > y is the same as x >= 0, if y is negative.  */
2144        (ge @0 { build_real (TREE_TYPE (@0), dconst0); })))
2145      (if (real_equal (TREE_REAL_CST_PTR (@1), &dconst0))
2146       (switch
2147        /* sqrt(x) < 0 is always false.  */
2148        (if (cmp == LT_EXPR)
2149         { constant_boolean_node (false, type); })
2150        /* sqrt(x) >= 0 is always true if we don't care about NaNs.  */
2151        (if (cmp == GE_EXPR && !HONOR_NANS (@0))
2152         { constant_boolean_node (true, type); })
2153        /* sqrt(x) <= 0 -> x == 0.  */
2154        (if (cmp == LE_EXPR)
2155         (eq @0 @1))
2156        /* Otherwise sqrt(x) cmp 0 -> x cmp 0.  Here cmp can be >=, >,
2157           == or !=.  In the last case:
2159             (sqrt(x) != 0) == (NaN != 0) == true == (x != 0)
2161           if x is negative or NaN.  Due to -funsafe-math-optimizations,
2162           the results for other x follow from natural arithmetic.  */
2163        (cmp @0 @1)))
2164      (if (cmp == GT_EXPR || cmp == GE_EXPR)
2165       (with
2166        {
2167          REAL_VALUE_TYPE c2;
2168          real_arithmetic (&c2, MULT_EXPR,
2169                           &TREE_REAL_CST (@1), &TREE_REAL_CST (@1));
2170          real_convert (&c2, TYPE_MODE (TREE_TYPE (@0)), &c2);
2171        }
2172        (if (REAL_VALUE_ISINF (c2))
2173         /* sqrt(x) > y is x == +Inf, when y is very large.  */
2174         (if (HONOR_INFINITIES (@0))
2175          (eq @0 { build_real (TREE_TYPE (@0), c2); })
2176          { constant_boolean_node (false, type); })
2177         /* sqrt(x) > c is the same as x > c*c.  */
2178         (cmp @0 { build_real (TREE_TYPE (@0), c2); }))))
2179      (if (cmp == LT_EXPR || cmp == LE_EXPR)
2180       (with
2181        {
2182          REAL_VALUE_TYPE c2;
2183          real_arithmetic (&c2, MULT_EXPR,
2184                           &TREE_REAL_CST (@1), &TREE_REAL_CST (@1));
2185          real_convert (&c2, TYPE_MODE (TREE_TYPE (@0)), &c2);
2186        }
2187        (if (REAL_VALUE_ISINF (c2))
2188         (switch
2189          /* sqrt(x) < y is always true, when y is a very large
2190             value and we don't care about NaNs or Infinities.  */
2191          (if (! HONOR_NANS (@0) && ! HONOR_INFINITIES (@0))
2192           { constant_boolean_node (true, type); })
2193          /* sqrt(x) < y is x != +Inf when y is very large and we
2194             don't care about NaNs.  */
2195          (if (! HONOR_NANS (@0))
2196           (ne @0 { build_real (TREE_TYPE (@0), c2); }))
2197          /* sqrt(x) < y is x >= 0 when y is very large and we
2198             don't care about Infinities.  */
2199          (if (! HONOR_INFINITIES (@0))
2200           (ge @0 { build_real (TREE_TYPE (@0), dconst0); }))
2201          /* sqrt(x) < y is x >= 0 && x != +Inf, when y is large.  */
2202          (if (GENERIC)
2203           (truth_andif
2204            (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
2205            (ne @0 { build_real (TREE_TYPE (@0), c2); }))))
2206         /* sqrt(x) < c is the same as x < c*c, if we ignore NaNs.  */
2207         (if (! HONOR_NANS (@0))
2208          (cmp @0 { build_real (TREE_TYPE (@0), c2); })
2209          /* sqrt(x) < c is the same as x >= 0 && x < c*c.  */
2210          (if (GENERIC)
2211           (truth_andif
2212            (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
2213            (cmp @0 { build_real (TREE_TYPE (@0), c2); }))))))))))))
2215 /* Unordered tests if either argument is a NaN.  */
2216 (simplify
2217  (bit_ior (unordered @0 @0) (unordered @1 @1))
2218  (if (types_match (@0, @1))
2219   (unordered @0 @1)))
2220 (simplify
2221  (bit_and (ordered @0 @0) (ordered @1 @1))
2222  (if (types_match (@0, @1))
2223   (ordered @0 @1)))
2224 (simplify
2225  (bit_ior:c (unordered @0 @0) (unordered:c@2 @0 @1))
2226  @2)
2227 (simplify
2228  (bit_and:c (ordered @0 @0) (ordered:c@2 @0 @1))
2229  @2)
2231 /* Simple range test simplifications.  */
2232 /* A < B || A >= B -> true.  */
2233 (for test1 (lt le le le ne ge)
2234      test2 (ge gt ge ne eq ne)
2235  (simplify
2236   (bit_ior:c (test1 @0 @1) (test2 @0 @1))
2237   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2238        || VECTOR_INTEGER_TYPE_P (TREE_TYPE (@0)))
2239    { constant_boolean_node (true, type); })))
2240 /* A < B && A >= B -> false.  */
2241 (for test1 (lt lt lt le ne eq)
2242      test2 (ge gt eq gt eq gt)
2243  (simplify
2244   (bit_and:c (test1 @0 @1) (test2 @0 @1))
2245   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2246        || VECTOR_INTEGER_TYPE_P (TREE_TYPE (@0)))
2247    { constant_boolean_node (false, type); })))
2249 /* -A CMP -B -> B CMP A.  */
2250 (for cmp (tcc_comparison)
2251      scmp (swapped_tcc_comparison)
2252  (simplify
2253   (cmp (negate @0) (negate @1))
2254   (if (FLOAT_TYPE_P (TREE_TYPE (@0))
2255        || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2256            && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
2257    (scmp @0 @1)))
2258  (simplify
2259   (cmp (negate @0) CONSTANT_CLASS_P@1)
2260   (if (FLOAT_TYPE_P (TREE_TYPE (@0))
2261        || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2262            && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
2263    (with { tree tem = const_unop (NEGATE_EXPR, TREE_TYPE (@0), @1); }
2264     (if (tem && !TREE_OVERFLOW (tem))
2265      (scmp @0 { tem; }))))))
2267 /* Convert ABS_EXPR<x> == 0 or ABS_EXPR<x> != 0 to x == 0 or x != 0.  */
2268 (for op (eq ne)
2269  (simplify
2270   (op (abs @0) zerop@1)
2271   (op @0 @1)))
2273 /* From fold_sign_changed_comparison and fold_widened_comparison.  */
2274 (for cmp (simple_comparison)
2275  (simplify
2276   (cmp (convert@0 @00) (convert?@1 @10))
2277   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2278        /* Disable this optimization if we're casting a function pointer
2279           type on targets that require function pointer canonicalization.  */
2280        && !(targetm.have_canonicalize_funcptr_for_compare ()
2281             && TREE_CODE (TREE_TYPE (@00)) == POINTER_TYPE
2282             && TREE_CODE (TREE_TYPE (TREE_TYPE (@00))) == FUNCTION_TYPE)
2283        && single_use (@0))
2284    (if (TYPE_PRECISION (TREE_TYPE (@00)) == TYPE_PRECISION (TREE_TYPE (@0))
2285         && (TREE_CODE (@10) == INTEGER_CST
2286             || (@1 != @10 && types_match (TREE_TYPE (@10), TREE_TYPE (@00))))
2287         && (TYPE_UNSIGNED (TREE_TYPE (@00)) == TYPE_UNSIGNED (TREE_TYPE (@0))
2288             || cmp == NE_EXPR
2289             || cmp == EQ_EXPR)
2290         && (POINTER_TYPE_P (TREE_TYPE (@00)) == POINTER_TYPE_P (TREE_TYPE (@0))))
2291     /* ???  The special-casing of INTEGER_CST conversion was in the original
2292        code and here to avoid a spurious overflow flag on the resulting
2293        constant which fold_convert produces.  */
2294     (if (TREE_CODE (@1) == INTEGER_CST)
2295      (cmp @00 { force_fit_type (TREE_TYPE (@00), wi::to_widest (@1), 0,
2296                                 TREE_OVERFLOW (@1)); })
2297      (cmp @00 (convert @1)))
2299     (if (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (TREE_TYPE (@00)))
2300      /* If possible, express the comparison in the shorter mode.  */
2301      (if ((cmp == EQ_EXPR || cmp == NE_EXPR
2302            || TYPE_UNSIGNED (TREE_TYPE (@0)) == TYPE_UNSIGNED (TREE_TYPE (@00)))
2303           && (types_match (TREE_TYPE (@10), TREE_TYPE (@00))
2304               || ((TYPE_PRECISION (TREE_TYPE (@00))
2305                    >= TYPE_PRECISION (TREE_TYPE (@10)))
2306                   && (TYPE_UNSIGNED (TREE_TYPE (@00))
2307                       == TYPE_UNSIGNED (TREE_TYPE (@10))))
2308               || (TREE_CODE (@10) == INTEGER_CST
2309                   && INTEGRAL_TYPE_P (TREE_TYPE (@00))
2310                   && int_fits_type_p (@10, TREE_TYPE (@00)))))
2311       (cmp @00 (convert @10))
2312       (if (TREE_CODE (@10) == INTEGER_CST
2313            && INTEGRAL_TYPE_P (TREE_TYPE (@00))
2314            && !int_fits_type_p (@10, TREE_TYPE (@00)))
2315        (with
2316         {
2317           tree min = lower_bound_in_type (TREE_TYPE (@10), TREE_TYPE (@00));
2318           tree max = upper_bound_in_type (TREE_TYPE (@10), TREE_TYPE (@00));
2319           bool above = integer_nonzerop (const_binop (LT_EXPR, type, max, @10));
2320           bool below = integer_nonzerop (const_binop (LT_EXPR, type, @10, min));
2321         }
2322         (if (above || below)
2323          (if (cmp == EQ_EXPR || cmp == NE_EXPR)
2324           { constant_boolean_node (cmp == EQ_EXPR ? false : true, type); }
2325           (if (cmp == LT_EXPR || cmp == LE_EXPR)
2326            { constant_boolean_node (above ? true : false, type); }
2327            (if (cmp == GT_EXPR || cmp == GE_EXPR)
2328             { constant_boolean_node (above ? false : true, type); }))))))))))))
2330 (for cmp (eq ne)
2331  /* A local variable can never be pointed to by
2332     the default SSA name of an incoming parameter.
2333     SSA names are canonicalized to 2nd place.  */
2334  (simplify
2335   (cmp addr@0 SSA_NAME@1)
2336   (if (SSA_NAME_IS_DEFAULT_DEF (@1)
2337        && TREE_CODE (SSA_NAME_VAR (@1)) == PARM_DECL)
2338    (with { tree base = get_base_address (TREE_OPERAND (@0, 0)); }
2339     (if (TREE_CODE (base) == VAR_DECL
2340          && auto_var_in_fn_p (base, current_function_decl))
2341      (if (cmp == NE_EXPR)
2342       { constant_boolean_node (true, type); }
2343       { constant_boolean_node (false, type); }))))))
2345 /* Equality compare simplifications from fold_binary  */
2346 (for cmp (eq ne)
2348  /* If we have (A | C) == D where C & ~D != 0, convert this into 0.
2349     Similarly for NE_EXPR.  */
2350  (simplify
2351   (cmp (convert?@3 (bit_ior @0 INTEGER_CST@1)) INTEGER_CST@2)
2352   (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0))
2353        && wi::bit_and_not (@1, @2) != 0)
2354    { constant_boolean_node (cmp == NE_EXPR, type); }))
2356  /* (X ^ Y) == 0 becomes X == Y, and (X ^ Y) != 0 becomes X != Y.  */
2357  (simplify
2358   (cmp (bit_xor @0 @1) integer_zerop)
2359   (cmp @0 @1))
2361  /* (X ^ Y) == Y becomes X == 0.
2362     Likewise (X ^ Y) == X becomes Y == 0.  */
2363  (simplify
2364   (cmp:c (bit_xor:c @0 @1) @0)
2365   (cmp @1 { build_zero_cst (TREE_TYPE (@1)); }))
2367  /* (X ^ C1) op C2 can be rewritten as X op (C1 ^ C2).  */
2368  (simplify
2369   (cmp (convert?@3 (bit_xor @0 INTEGER_CST@1)) INTEGER_CST@2)
2370   (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0)))
2371    (cmp @0 (bit_xor @1 (convert @2)))))
2373  (simplify
2374   (cmp (convert? addr@0) integer_zerop)
2375   (if (tree_single_nonzero_warnv_p (@0, NULL))
2376    { constant_boolean_node (cmp == NE_EXPR, type); })))
2378 /* If we have (A & C) == C where C is a power of 2, convert this into
2379    (A & C) != 0.  Similarly for NE_EXPR.  */
2380 (for cmp (eq ne)
2381      icmp (ne eq)
2382  (simplify
2383   (cmp (bit_and@2 @0 integer_pow2p@1) @1)
2384   (icmp @2 { build_zero_cst (TREE_TYPE (@0)); })))
2386 /* If we have (A & C) != 0 where C is the sign bit of A, convert
2387    this into A < 0.  Similarly for (A & C) == 0 into A >= 0.  */
2388 (for cmp (eq ne)
2389      ncmp (ge lt)
2390  (simplify
2391   (cmp (bit_and (convert?@2 @0) integer_pow2p@1) integer_zerop)
2392   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2393        && (TYPE_PRECISION (TREE_TYPE (@0))
2394            == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (@0))))
2395        && element_precision (@2) >= element_precision (@0)
2396        && wi::only_sign_bit_p (@1, element_precision (@0)))
2397    (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
2398     (ncmp (convert:stype @0) { build_zero_cst (stype); })))))
2400 /* When the addresses are not directly of decls compare base and offset.
2401    This implements some remaining parts of fold_comparison address
2402    comparisons but still no complete part of it.  Still it is good
2403    enough to make fold_stmt not regress when not dispatching to fold_binary.  */
2404 (for cmp (simple_comparison)
2405  (simplify
2406   (cmp (convert1?@2 addr@0) (convert2? addr@1))
2407   (with
2408    {
2409      HOST_WIDE_INT off0, off1;
2410      tree base0 = get_addr_base_and_unit_offset (TREE_OPERAND (@0, 0), &off0);
2411      tree base1 = get_addr_base_and_unit_offset (TREE_OPERAND (@1, 0), &off1);
2412      if (base0 && TREE_CODE (base0) == MEM_REF)
2413        {
2414          off0 += mem_ref_offset (base0).to_short_addr ();
2415          base0 = TREE_OPERAND (base0, 0);
2416        }
2417      if (base1 && TREE_CODE (base1) == MEM_REF)
2418        {
2419          off1 += mem_ref_offset (base1).to_short_addr ();
2420          base1 = TREE_OPERAND (base1, 0);
2421        }
2422    }
2423    (if (base0 && base1)
2424     (with
2425      {
2426        int equal = 2;
2427        if (decl_in_symtab_p (base0)
2428            && decl_in_symtab_p (base1))
2429          equal = symtab_node::get_create (base0)
2430                    ->equal_address_to (symtab_node::get_create (base1));
2431        else if ((DECL_P (base0)
2432                  || TREE_CODE (base0) == SSA_NAME
2433                  || TREE_CODE (base0) == STRING_CST)
2434                 && (DECL_P (base1)
2435                     || TREE_CODE (base1) == SSA_NAME
2436                     || TREE_CODE (base1) == STRING_CST))
2437          equal = (base0 == base1);
2438      }
2439      (if (equal == 1
2440           && (cmp == EQ_EXPR || cmp == NE_EXPR
2441               /* If the offsets are equal we can ignore overflow.  */
2442               || off0 == off1
2443               || POINTER_TYPE_OVERFLOW_UNDEFINED
2444               /* Or if we compare using pointers to decls or strings.  */
2445               || (POINTER_TYPE_P (TREE_TYPE (@2))
2446                   && (DECL_P (base0) || TREE_CODE (base0) == STRING_CST))))
2447       (switch
2448        (if (cmp == EQ_EXPR)
2449         { constant_boolean_node (off0 == off1, type); })
2450        (if (cmp == NE_EXPR)
2451         { constant_boolean_node (off0 != off1, type); })
2452        (if (cmp == LT_EXPR)
2453         { constant_boolean_node (off0 < off1, type); })
2454        (if (cmp == LE_EXPR)
2455         { constant_boolean_node (off0 <= off1, type); })
2456        (if (cmp == GE_EXPR)
2457         { constant_boolean_node (off0 >= off1, type); })
2458        (if (cmp == GT_EXPR)
2459         { constant_boolean_node (off0 > off1, type); }))
2460       (if (equal == 0
2461            && DECL_P (base0) && DECL_P (base1)
2462            /* If we compare this as integers require equal offset.  */
2463            && (!INTEGRAL_TYPE_P (TREE_TYPE (@2))
2464                || off0 == off1))
2465        (switch
2466         (if (cmp == EQ_EXPR)
2467          { constant_boolean_node (false, type); })
2468         (if (cmp == NE_EXPR)
2469          { constant_boolean_node (true, type); })))))))))
2471 /* Simplify pointer equality compares using PTA.  */
2472 (for neeq (ne eq)
2473  (simplify
2474   (neeq @0 @1)
2475   (if (POINTER_TYPE_P (TREE_TYPE (@0))
2476        && ptrs_compare_unequal (@0, @1))
2477    { neeq == EQ_EXPR ? boolean_false_node : boolean_true_node; })))
2479 /* Non-equality compare simplifications from fold_binary  */
2480 (for cmp (lt gt le ge)
2481  /* Comparisons with the highest or lowest possible integer of
2482     the specified precision will have known values.  */
2483  (simplify
2484   (cmp (convert?@2 @0) INTEGER_CST@1)
2485   (if ((INTEGRAL_TYPE_P (TREE_TYPE (@1)) || POINTER_TYPE_P (TREE_TYPE (@1)))
2486        && tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@0)))
2487    (with
2488     {
2489       tree arg1_type = TREE_TYPE (@1);
2490       unsigned int prec = TYPE_PRECISION (arg1_type);
2491       wide_int max = wi::max_value (arg1_type);
2492       wide_int signed_max = wi::max_value (prec, SIGNED);
2493       wide_int min = wi::min_value (arg1_type);
2494     }
2495     (switch
2496      (if (wi::eq_p (@1, max))
2497       (switch
2498        (if (cmp == GT_EXPR)
2499         { constant_boolean_node (false, type); })
2500        (if (cmp == GE_EXPR)
2501         (eq @2 @1))
2502        (if (cmp == LE_EXPR)
2503         { constant_boolean_node (true, type); })
2504        (if (cmp == LT_EXPR)
2505         (ne @2 @1))))
2506      (if (wi::eq_p (@1, min))
2507       (switch
2508        (if (cmp == LT_EXPR)
2509         { constant_boolean_node (false, type); })
2510        (if (cmp == LE_EXPR)
2511         (eq @2 @1))
2512        (if (cmp == GE_EXPR)
2513         { constant_boolean_node (true, type); })
2514        (if (cmp == GT_EXPR)
2515         (ne @2 @1))))
2516      (if (wi::eq_p (@1, max - 1))
2517       (switch
2518        (if (cmp == GT_EXPR)
2519         (eq @2 { wide_int_to_tree (TREE_TYPE (@1), wi::add (@1, 1)); }))
2520        (if (cmp == LE_EXPR)
2521         (ne @2 { wide_int_to_tree (TREE_TYPE (@1), wi::add (@1, 1)); }))))
2522      (if (wi::eq_p (@1, min + 1))
2523       (switch
2524        (if (cmp == GE_EXPR)
2525         (ne @2 { wide_int_to_tree (TREE_TYPE (@1), wi::sub (@1, 1)); }))
2526        (if (cmp == LT_EXPR)
2527         (eq @2 { wide_int_to_tree (TREE_TYPE (@1), wi::sub (@1, 1)); }))))
2528      (if (wi::eq_p (@1, signed_max)
2529           && TYPE_UNSIGNED (arg1_type)
2530           /* We will flip the signedness of the comparison operator
2531              associated with the mode of @1, so the sign bit is
2532              specified by this mode.  Check that @1 is the signed
2533              max associated with this sign bit.  */
2534           && prec == GET_MODE_PRECISION (TYPE_MODE (arg1_type))
2535           /* signed_type does not work on pointer types.  */
2536           && INTEGRAL_TYPE_P (arg1_type))
2537       /* The following case also applies to X < signed_max+1
2538          and X >= signed_max+1 because previous transformations.  */
2539       (if (cmp == LE_EXPR || cmp == GT_EXPR)
2540        (with { tree st = signed_type_for (arg1_type); }
2541         (if (cmp == LE_EXPR)
2542          (ge (convert:st @0) { build_zero_cst (st); })
2543          (lt (convert:st @0) { build_zero_cst (st); }))))))))))
2545 (for cmp (unordered ordered unlt unle ungt unge uneq ltgt)
2546  /* If the second operand is NaN, the result is constant.  */
2547  (simplify
2548   (cmp @0 REAL_CST@1)
2549   (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
2550        && (cmp != LTGT_EXPR || ! flag_trapping_math))
2551    { constant_boolean_node (cmp == ORDERED_EXPR || cmp == LTGT_EXPR
2552                             ? false : true, type); })))
2554 /* bool_var != 0 becomes bool_var.  */
2555 (simplify
2556  (ne @0 integer_zerop)
2557  (if (TREE_CODE (TREE_TYPE (@0)) == BOOLEAN_TYPE
2558       && types_match (type, TREE_TYPE (@0)))
2559   (non_lvalue @0)))
2560 /* bool_var == 1 becomes bool_var.  */
2561 (simplify
2562  (eq @0 integer_onep)
2563  (if (TREE_CODE (TREE_TYPE (@0)) == BOOLEAN_TYPE
2564       && types_match (type, TREE_TYPE (@0)))
2565   (non_lvalue @0)))
2566 /* Do not handle
2567    bool_var == 0 becomes !bool_var or
2568    bool_var != 1 becomes !bool_var
2569    here because that only is good in assignment context as long
2570    as we require a tcc_comparison in GIMPLE_CONDs where we'd
2571    replace if (x == 0) with tem = ~x; if (tem != 0) which is
2572    clearly less optimal and which we'll transform again in forwprop.  */
2574 /* When one argument is a constant, overflow detection can be simplified.
2575    Currently restricted to single use so as not to interfere too much with
2576    ADD_OVERFLOW detection in tree-ssa-math-opts.c.
2577    A + CST CMP A  ->  A CMP' CST' */
2578 (for cmp (lt le ge gt)
2579      out (gt gt le le)
2580  (simplify
2581   (cmp (plus@2 @0 INTEGER_CST@1) @0)
2582   (if (TYPE_UNSIGNED (TREE_TYPE (@0))
2583        && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))
2584        && wi::ne_p (@1, 0)
2585        && single_use (@2))
2586    (out @0 { wide_int_to_tree (TREE_TYPE (@0), wi::max_value
2587                (TYPE_PRECISION (TREE_TYPE (@0)), UNSIGNED) - @1); }))))
2588 /* A CMP A + CST  ->  A CMP' CST' */
2589 (for cmp (gt ge le lt)
2590      out (gt gt le le)
2591  (simplify
2592   (cmp @0 (plus@2 @0 INTEGER_CST@1))
2593   (if (TYPE_UNSIGNED (TREE_TYPE (@0))
2594        && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))
2595        && wi::ne_p (@1, 0)
2596        && single_use (@2))
2597    (out @0 { wide_int_to_tree (TREE_TYPE (@0), wi::max_value
2598                (TYPE_PRECISION (TREE_TYPE (@0)), UNSIGNED) - @1); }))))
2600 /* To detect overflow in unsigned A - B, A < B is simpler than A - B > A.
2601    However, the detection logic for SUB_OVERFLOW in tree-ssa-math-opts.c
2602    expects the long form, so we restrict the transformation for now.  */
2603 (for cmp (gt le)
2604  (simplify
2605   (cmp (minus@2 @0 @1) @0)
2606   (if (single_use (@2)
2607        && ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2608        && TYPE_UNSIGNED (TREE_TYPE (@0))
2609        && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
2610    (cmp @1 @0))))
2611 (for cmp (lt ge)
2612  (simplify
2613   (cmp @0 (minus@2 @0 @1))
2614   (if (single_use (@2)
2615        && ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2616        && TYPE_UNSIGNED (TREE_TYPE (@0))
2617        && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
2618    (cmp @0 @1))))
2620 /* Testing for overflow is unnecessary if we already know the result.  */
2621 /* A < A - B  */
2622 (for cmp (lt ge)
2623      out (ne eq)
2624  (simplify
2625   (cmp @0 (realpart (IFN_SUB_OVERFLOW@2 @0 @1)))
2626   (if (TYPE_UNSIGNED (TREE_TYPE (@0))
2627        && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
2628    (out (imagpart @2) { build_zero_cst (TREE_TYPE (@0)); }))))
2629 /* A - B > A  */
2630 (for cmp (gt le)
2631      out (ne eq)
2632  (simplify
2633   (cmp (realpart (IFN_SUB_OVERFLOW@2 @0 @1)) @0)
2634   (if (TYPE_UNSIGNED (TREE_TYPE (@0))
2635        && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
2636    (out (imagpart @2) { build_zero_cst (TREE_TYPE (@0)); }))))
2637 /* A + B < A  */
2638 (for cmp (lt ge)
2639      out (ne eq)
2640  (simplify
2641   (cmp (realpart (IFN_ADD_OVERFLOW:c@2 @0 @1)) @0)
2642   (if (TYPE_UNSIGNED (TREE_TYPE (@0))
2643        && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
2644    (out (imagpart @2) { build_zero_cst (TREE_TYPE (@0)); }))))
2645 /* A > A + B  */
2646 (for cmp (gt le)
2647      out (ne eq)
2648  (simplify
2649   (cmp @0 (realpart (IFN_ADD_OVERFLOW:c@2 @0 @1)))
2650   (if (TYPE_UNSIGNED (TREE_TYPE (@0))
2651        && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
2652    (out (imagpart @2) { build_zero_cst (TREE_TYPE (@0)); }))))
2655 /* Simplification of math builtins.  These rules must all be optimizations
2656    as well as IL simplifications.  If there is a possibility that the new
2657    form could be a pessimization, the rule should go in the canonicalization
2658    section that follows this one.
2660    Rules can generally go in this section if they satisfy one of
2661    the following:
2663    - the rule describes an identity
2665    - the rule replaces calls with something as simple as addition or
2666      multiplication
2668    - the rule contains unary calls only and simplifies the surrounding
2669      arithmetic.  (The idea here is to exclude non-unary calls in which
2670      one operand is constant and in which the call is known to be cheap
2671      when the operand has that value.)  */
2673 (if (flag_unsafe_math_optimizations)
2674  /* Simplify sqrt(x) * sqrt(x) -> x.  */
2675  (simplify
2676   (mult (SQRT@1 @0) @1)
2677   (if (!HONOR_SNANS (type))
2678    @0))
2680  /* Simplify sqrt(x) * sqrt(y) -> sqrt(x*y).  */
2681  (for root (SQRT CBRT)
2682   (simplify
2683    (mult (root:s @0) (root:s @1))
2684     (root (mult @0 @1))))
2686  /* Simplify expN(x) * expN(y) -> expN(x+y). */
2687  (for exps (EXP EXP2 EXP10 POW10)
2688   (simplify
2689    (mult (exps:s @0) (exps:s @1))
2690     (exps (plus @0 @1))))
2692  /* Simplify a/root(b/c) into a*root(c/b).  */
2693  (for root (SQRT CBRT)
2694   (simplify
2695    (rdiv @0 (root:s (rdiv:s @1 @2)))
2696     (mult @0 (root (rdiv @2 @1)))))
2698  /* Simplify x/expN(y) into x*expN(-y).  */
2699  (for exps (EXP EXP2 EXP10 POW10)
2700   (simplify
2701    (rdiv @0 (exps:s @1))
2702     (mult @0 (exps (negate @1)))))
2704  (for logs (LOG LOG2 LOG10 LOG10)
2705       exps (EXP EXP2 EXP10 POW10)
2706   /* logN(expN(x)) -> x.  */
2707   (simplify
2708    (logs (exps @0))
2709    @0)
2710   /* expN(logN(x)) -> x.  */
2711   (simplify
2712    (exps (logs @0))
2713    @0))
2715  /* Optimize logN(func()) for various exponential functions.  We
2716     want to determine the value "x" and the power "exponent" in
2717     order to transform logN(x**exponent) into exponent*logN(x).  */
2718  (for logs (LOG  LOG   LOG   LOG2 LOG2  LOG2  LOG10 LOG10)
2719       exps (EXP2 EXP10 POW10 EXP  EXP10 POW10 EXP   EXP2)
2720   (simplify
2721    (logs (exps @0))
2722    (if (SCALAR_FLOAT_TYPE_P (type))
2723     (with {
2724       tree x;
2725       switch (exps)
2726         {
2727         CASE_CFN_EXP:
2728           /* Prepare to do logN(exp(exponent)) -> exponent*logN(e).  */
2729           x = build_real_truncate (type, dconst_e ());
2730           break;
2731         CASE_CFN_EXP2:
2732           /* Prepare to do logN(exp2(exponent)) -> exponent*logN(2).  */
2733           x = build_real (type, dconst2);
2734           break;
2735         CASE_CFN_EXP10:
2736         CASE_CFN_POW10:
2737           /* Prepare to do logN(exp10(exponent)) -> exponent*logN(10).  */
2738           {
2739             REAL_VALUE_TYPE dconst10;
2740             real_from_integer (&dconst10, VOIDmode, 10, SIGNED);
2741             x = build_real (type, dconst10);
2742           }
2743           break;
2744         default:
2745           gcc_unreachable ();
2746         }
2747       }
2748      (mult (logs { x; }) @0)))))
2750  (for logs (LOG LOG
2751             LOG2 LOG2
2752             LOG10 LOG10)
2753       exps (SQRT CBRT)
2754   (simplify
2755    (logs (exps @0))
2756    (if (SCALAR_FLOAT_TYPE_P (type))
2757     (with {
2758       tree x;
2759       switch (exps)
2760         {
2761         CASE_CFN_SQRT:
2762           /* Prepare to do logN(sqrt(x)) -> 0.5*logN(x).  */
2763           x = build_real (type, dconsthalf);
2764           break;
2765         CASE_CFN_CBRT:
2766           /* Prepare to do logN(cbrt(x)) -> (1/3)*logN(x).  */
2767           x = build_real_truncate (type, dconst_third ());
2768           break;
2769         default:
2770           gcc_unreachable ();
2771         }
2772       }
2773      (mult { x; } (logs @0))))))
2775  /* logN(pow(x,exponent)) -> exponent*logN(x).  */
2776  (for logs (LOG LOG2 LOG10)
2777       pows (POW)
2778   (simplify
2779    (logs (pows @0 @1))
2780    (mult @1 (logs @0))))
2782  (for sqrts (SQRT)
2783       cbrts (CBRT)
2784       pows (POW)
2785       exps (EXP EXP2 EXP10 POW10)
2786   /* sqrt(expN(x)) -> expN(x*0.5).  */
2787   (simplify
2788    (sqrts (exps @0))
2789    (exps (mult @0 { build_real (type, dconsthalf); })))
2790   /* cbrt(expN(x)) -> expN(x/3).  */
2791   (simplify
2792    (cbrts (exps @0))
2793    (exps (mult @0 { build_real_truncate (type, dconst_third ()); })))
2794   /* pow(expN(x), y) -> expN(x*y).  */
2795   (simplify
2796    (pows (exps @0) @1)
2797    (exps (mult @0 @1))))
2799  /* tan(atan(x)) -> x.  */
2800  (for tans (TAN)
2801       atans (ATAN)
2802   (simplify
2803    (tans (atans @0))
2804    @0)))
2806 /* cabs(x+0i) or cabs(0+xi) -> abs(x).  */
2807 (simplify
2808  (CABS (complex:c @0 real_zerop@1))
2809  (abs @0))
2811 /* trunc(trunc(x)) -> trunc(x), etc.  */
2812 (for fns (TRUNC FLOOR CEIL ROUND NEARBYINT RINT)
2813  (simplify
2814   (fns (fns @0))
2815   (fns @0)))
2816 /* f(x) -> x if x is integer valued and f does nothing for such values.  */
2817 (for fns (TRUNC FLOOR CEIL ROUND NEARBYINT RINT)
2818  (simplify
2819   (fns integer_valued_real_p@0)
2820   @0))
2822 /* hypot(x,0) and hypot(0,x) -> abs(x).  */
2823 (simplify
2824  (HYPOT:c @0 real_zerop@1)
2825  (abs @0))
2827 /* pow(1,x) -> 1.  */
2828 (simplify
2829  (POW real_onep@0 @1)
2830  @0)
2832 (simplify
2833  /* copysign(x,x) -> x.  */
2834  (COPYSIGN @0 @0)
2835  @0)
2837 (simplify
2838  /* copysign(x,y) -> fabs(x) if y is nonnegative.  */
2839  (COPYSIGN @0 tree_expr_nonnegative_p@1)
2840  (abs @0))
2842 (for scale (LDEXP SCALBN SCALBLN)
2843  /* ldexp(0, x) -> 0.  */
2844  (simplify
2845   (scale real_zerop@0 @1)
2846   @0)
2847  /* ldexp(x, 0) -> x.  */
2848  (simplify
2849   (scale @0 integer_zerop@1)
2850   @0)
2851  /* ldexp(x, y) -> x if x is +-Inf or NaN.  */
2852  (simplify
2853   (scale REAL_CST@0 @1)
2854   (if (!real_isfinite (TREE_REAL_CST_PTR (@0)))
2855    @0)))
2857 /* Canonicalization of sequences of math builtins.  These rules represent
2858    IL simplifications but are not necessarily optimizations.
2860    The sincos pass is responsible for picking "optimal" implementations
2861    of math builtins, which may be more complicated and can sometimes go
2862    the other way, e.g. converting pow into a sequence of sqrts.
2863    We only want to do these canonicalizations before the pass has run.  */
2865 (if (flag_unsafe_math_optimizations && canonicalize_math_p ())
2866  /* Simplify tan(x) * cos(x) -> sin(x). */
2867  (simplify
2868   (mult:c (TAN:s @0) (COS:s @0))
2869    (SIN @0))
2871  /* Simplify x * pow(x,c) -> pow(x,c+1). */
2872  (simplify
2873   (mult:c @0 (POW:s @0 REAL_CST@1))
2874   (if (!TREE_OVERFLOW (@1))
2875    (POW @0 (plus @1 { build_one_cst (type); }))))
2877  /* Simplify sin(x) / cos(x) -> tan(x). */
2878  (simplify
2879   (rdiv (SIN:s @0) (COS:s @0))
2880    (TAN @0))
2882  /* Simplify cos(x) / sin(x) -> 1 / tan(x). */
2883  (simplify
2884   (rdiv (COS:s @0) (SIN:s @0))
2885    (rdiv { build_one_cst (type); } (TAN @0)))
2887  /* Simplify sin(x) / tan(x) -> cos(x). */
2888  (simplify
2889   (rdiv (SIN:s @0) (TAN:s @0))
2890   (if (! HONOR_NANS (@0)
2891        && ! HONOR_INFINITIES (@0))
2892    (COS @0)))
2894  /* Simplify tan(x) / sin(x) -> 1.0 / cos(x). */
2895  (simplify
2896   (rdiv (TAN:s @0) (SIN:s @0))
2897   (if (! HONOR_NANS (@0)
2898        && ! HONOR_INFINITIES (@0))
2899    (rdiv { build_one_cst (type); } (COS @0))))
2901  /* Simplify pow(x,y) * pow(x,z) -> pow(x,y+z). */
2902  (simplify
2903   (mult (POW:s @0 @1) (POW:s @0 @2))
2904    (POW @0 (plus @1 @2)))
2906  /* Simplify pow(x,y) * pow(z,y) -> pow(x*z,y). */
2907  (simplify
2908   (mult (POW:s @0 @1) (POW:s @2 @1))
2909    (POW (mult @0 @2) @1))
2911  /* Simplify powi(x,y) * powi(z,y) -> powi(x*z,y). */
2912  (simplify
2913   (mult (POWI:s @0 @1) (POWI:s @2 @1))
2914    (POWI (mult @0 @2) @1))
2916  /* Simplify pow(x,c) / x -> pow(x,c-1). */
2917  (simplify
2918   (rdiv (POW:s @0 REAL_CST@1) @0)
2919   (if (!TREE_OVERFLOW (@1))
2920    (POW @0 (minus @1 { build_one_cst (type); }))))
2922  /* Simplify x / pow (y,z) -> x * pow(y,-z). */
2923  (simplify
2924   (rdiv @0 (POW:s @1 @2))
2925    (mult @0 (POW @1 (negate @2))))
2927  (for sqrts (SQRT)
2928       cbrts (CBRT)
2929       pows (POW)
2930   /* sqrt(sqrt(x)) -> pow(x,1/4).  */
2931   (simplify
2932    (sqrts (sqrts @0))
2933    (pows @0 { build_real (type, dconst_quarter ()); }))
2934   /* sqrt(cbrt(x)) -> pow(x,1/6).  */
2935   (simplify
2936    (sqrts (cbrts @0))
2937    (pows @0 { build_real_truncate (type, dconst_sixth ()); }))
2938   /* cbrt(sqrt(x)) -> pow(x,1/6).  */
2939   (simplify
2940    (cbrts (sqrts @0))
2941    (pows @0 { build_real_truncate (type, dconst_sixth ()); }))
2942   /* cbrt(cbrt(x)) -> pow(x,1/9), iff x is nonnegative.  */
2943   (simplify
2944    (cbrts (cbrts tree_expr_nonnegative_p@0))
2945    (pows @0 { build_real_truncate (type, dconst_ninth ()); }))
2946   /* sqrt(pow(x,y)) -> pow(|x|,y*0.5).  */
2947   (simplify
2948    (sqrts (pows @0 @1))
2949    (pows (abs @0) (mult @1 { build_real (type, dconsthalf); })))
2950   /* cbrt(pow(x,y)) -> pow(x,y/3), iff x is nonnegative.  */
2951   (simplify
2952    (cbrts (pows tree_expr_nonnegative_p@0 @1))
2953    (pows @0 (mult @1 { build_real_truncate (type, dconst_third ()); })))
2954   /* pow(sqrt(x),y) -> pow(x,y*0.5).  */
2955   (simplify
2956    (pows (sqrts @0) @1)
2957    (pows @0 (mult @1 { build_real (type, dconsthalf); })))
2958   /* pow(cbrt(x),y) -> pow(x,y/3) iff x is nonnegative.  */
2959   (simplify
2960    (pows (cbrts tree_expr_nonnegative_p@0) @1)
2961    (pows @0 (mult @1 { build_real_truncate (type, dconst_third ()); })))
2962   /* pow(pow(x,y),z) -> pow(x,y*z) iff x is nonnegative.  */
2963   (simplify
2964    (pows (pows tree_expr_nonnegative_p@0 @1) @2)
2965    (pows @0 (mult @1 @2))))
2967  /* cabs(x+xi) -> fabs(x)*sqrt(2).  */
2968  (simplify
2969   (CABS (complex @0 @0))
2970   (mult (abs @0) { build_real_truncate (type, dconst_sqrt2 ()); }))
2972  /* hypot(x,x) -> fabs(x)*sqrt(2).  */
2973  (simplify
2974   (HYPOT @0 @0)
2975   (mult (abs @0) { build_real_truncate (type, dconst_sqrt2 ()); }))
2977  /* cexp(x+yi) -> exp(x)*cexpi(y).  */
2978  (for cexps (CEXP)
2979       exps (EXP)
2980       cexpis (CEXPI)
2981   (simplify
2982    (cexps compositional_complex@0)
2983    (if (targetm.libc_has_function (function_c99_math_complex))
2984     (complex
2985      (mult (exps@1 (realpart @0)) (realpart (cexpis:type@2 (imagpart @0))))
2986      (mult @1 (imagpart @2)))))))
2988 (if (canonicalize_math_p ())
2989  /* floor(x) -> trunc(x) if x is nonnegative.  */
2990  (for floors (FLOOR)
2991       truncs (TRUNC)
2992   (simplify
2993    (floors tree_expr_nonnegative_p@0)
2994    (truncs @0))))
2996 (match double_value_p
2997  @0
2998  (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == double_type_node)))
2999 (for froms (BUILT_IN_TRUNCL
3000             BUILT_IN_FLOORL
3001             BUILT_IN_CEILL
3002             BUILT_IN_ROUNDL
3003             BUILT_IN_NEARBYINTL
3004             BUILT_IN_RINTL)
3005      tos (BUILT_IN_TRUNC
3006           BUILT_IN_FLOOR
3007           BUILT_IN_CEIL
3008           BUILT_IN_ROUND
3009           BUILT_IN_NEARBYINT
3010           BUILT_IN_RINT)
3011  /* truncl(extend(x)) -> extend(trunc(x)), etc., if x is a double.  */
3012  (if (optimize && canonicalize_math_p ())
3013   (simplify
3014    (froms (convert double_value_p@0))
3015    (convert (tos @0)))))
3017 (match float_value_p
3018  @0
3019  (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == float_type_node)))
3020 (for froms (BUILT_IN_TRUNCL BUILT_IN_TRUNC
3021             BUILT_IN_FLOORL BUILT_IN_FLOOR
3022             BUILT_IN_CEILL BUILT_IN_CEIL
3023             BUILT_IN_ROUNDL BUILT_IN_ROUND
3024             BUILT_IN_NEARBYINTL BUILT_IN_NEARBYINT
3025             BUILT_IN_RINTL BUILT_IN_RINT)
3026      tos (BUILT_IN_TRUNCF BUILT_IN_TRUNCF
3027           BUILT_IN_FLOORF BUILT_IN_FLOORF
3028           BUILT_IN_CEILF BUILT_IN_CEILF
3029           BUILT_IN_ROUNDF BUILT_IN_ROUNDF
3030           BUILT_IN_NEARBYINTF BUILT_IN_NEARBYINTF
3031           BUILT_IN_RINTF BUILT_IN_RINTF)
3032  /* truncl(extend(x)) and trunc(extend(x)) -> extend(truncf(x)), etc.,
3033     if x is a float.  */
3034  (if (optimize && canonicalize_math_p ()
3035       && targetm.libc_has_function (function_c99_misc))
3036   (simplify
3037    (froms (convert float_value_p@0))
3038    (convert (tos @0)))))
3040 (for froms (XFLOORL XCEILL XROUNDL XRINTL)
3041      tos (XFLOOR XCEIL XROUND XRINT)
3042  /* llfloorl(extend(x)) -> llfloor(x), etc., if x is a double.  */
3043  (if (optimize && canonicalize_math_p ())
3044   (simplify
3045    (froms (convert double_value_p@0))
3046    (tos @0))))
3048 (for froms (XFLOORL XCEILL XROUNDL XRINTL
3049             XFLOOR XCEIL XROUND XRINT)
3050      tos (XFLOORF XCEILF XROUNDF XRINTF)
3051  /* llfloorl(extend(x)) and llfloor(extend(x)) -> llfloorf(x), etc.,
3052     if x is a float.  */
3053  (if (optimize && canonicalize_math_p ())
3054   (simplify
3055    (froms (convert float_value_p@0))
3056    (tos @0))))
3058 (if (canonicalize_math_p ())
3059  /* xfloor(x) -> fix_trunc(x) if x is nonnegative.  */
3060  (for floors (IFLOOR LFLOOR LLFLOOR)
3061   (simplify
3062    (floors tree_expr_nonnegative_p@0)
3063    (fix_trunc @0))))
3065 (if (canonicalize_math_p ())
3066  /* xfloor(x) -> fix_trunc(x), etc., if x is integer valued.  */
3067  (for fns (IFLOOR LFLOOR LLFLOOR
3068            ICEIL LCEIL LLCEIL
3069            IROUND LROUND LLROUND)
3070   (simplify
3071    (fns integer_valued_real_p@0)
3072    (fix_trunc @0)))
3073  (if (!flag_errno_math)
3074   /* xrint(x) -> fix_trunc(x), etc., if x is integer valued.  */
3075   (for rints (IRINT LRINT LLRINT)
3076    (simplify
3077     (rints integer_valued_real_p@0)
3078     (fix_trunc @0)))))
3080 (if (canonicalize_math_p ())
3081  (for ifn (IFLOOR ICEIL IROUND IRINT)
3082       lfn (LFLOOR LCEIL LROUND LRINT)
3083       llfn (LLFLOOR LLCEIL LLROUND LLRINT)
3084   /* Canonicalize iround (x) to lround (x) on ILP32 targets where
3085      sizeof (int) == sizeof (long).  */
3086   (if (TYPE_PRECISION (integer_type_node)
3087        == TYPE_PRECISION (long_integer_type_node))
3088    (simplify
3089     (ifn @0)
3090     (lfn:long_integer_type_node @0)))
3091   /* Canonicalize llround (x) to lround (x) on LP64 targets where
3092      sizeof (long long) == sizeof (long).  */
3093   (if (TYPE_PRECISION (long_long_integer_type_node)
3094        == TYPE_PRECISION (long_integer_type_node))
3095    (simplify
3096     (llfn @0)
3097     (lfn:long_integer_type_node @0)))))
3099 /* cproj(x) -> x if we're ignoring infinities.  */
3100 (simplify
3101  (CPROJ @0)
3102  (if (!HONOR_INFINITIES (type))
3103    @0))
3105 /* If the real part is inf and the imag part is known to be
3106    nonnegative, return (inf + 0i).  */
3107 (simplify
3108  (CPROJ (complex REAL_CST@0 tree_expr_nonnegative_p@1))
3109  (if (real_isinf (TREE_REAL_CST_PTR (@0)))
3110   { build_complex_inf (type, false); }))
3112 /* If the imag part is inf, return (inf+I*copysign(0,imag)).  */
3113 (simplify
3114  (CPROJ (complex @0 REAL_CST@1))
3115  (if (real_isinf (TREE_REAL_CST_PTR (@1)))
3116   { build_complex_inf (type, TREE_REAL_CST_PTR (@1)->sign); }))
3118 (for pows (POW)
3119      sqrts (SQRT)
3120      cbrts (CBRT)
3121  (simplify
3122   (pows @0 REAL_CST@1)
3123   (with {
3124     const REAL_VALUE_TYPE *value = TREE_REAL_CST_PTR (@1);
3125     REAL_VALUE_TYPE tmp;
3126    }
3127    (switch
3128     /* pow(x,0) -> 1.  */
3129     (if (real_equal (value, &dconst0))
3130      { build_real (type, dconst1); })
3131     /* pow(x,1) -> x.  */
3132     (if (real_equal (value, &dconst1))
3133      @0)
3134     /* pow(x,-1) -> 1/x.  */
3135     (if (real_equal (value, &dconstm1))
3136      (rdiv { build_real (type, dconst1); } @0))
3137     /* pow(x,0.5) -> sqrt(x).  */
3138     (if (flag_unsafe_math_optimizations
3139          && canonicalize_math_p ()
3140          && real_equal (value, &dconsthalf))
3141      (sqrts @0))
3142     /* pow(x,1/3) -> cbrt(x).  */
3143     (if (flag_unsafe_math_optimizations
3144          && canonicalize_math_p ()
3145          && (tmp = real_value_truncate (TYPE_MODE (type), dconst_third ()),
3146              real_equal (value, &tmp)))
3147      (cbrts @0))))))
3149 /* powi(1,x) -> 1.  */
3150 (simplify
3151  (POWI real_onep@0 @1)
3152  @0)
3154 (simplify
3155  (POWI @0 INTEGER_CST@1)
3156  (switch
3157   /* powi(x,0) -> 1.  */
3158   (if (wi::eq_p (@1, 0))
3159    { build_real (type, dconst1); })
3160   /* powi(x,1) -> x.  */
3161   (if (wi::eq_p (@1, 1))
3162    @0)
3163   /* powi(x,-1) -> 1/x.  */
3164   (if (wi::eq_p (@1, -1))
3165    (rdiv { build_real (type, dconst1); } @0))))
3167 /* Narrowing of arithmetic and logical operations. 
3169    These are conceptually similar to the transformations performed for
3170    the C/C++ front-ends by shorten_binary_op and shorten_compare.  Long
3171    term we want to move all that code out of the front-ends into here.  */
3173 /* If we have a narrowing conversion of an arithmetic operation where
3174    both operands are widening conversions from the same type as the outer
3175    narrowing conversion.  Then convert the innermost operands to a suitable
3176    unsigned type (to avoid introducing undefined behavior), perform the
3177    operation and convert the result to the desired type.  */
3178 (for op (plus minus)
3179   (simplify
3180     (convert (op:s (convert@2 @0) (convert@3 @1)))
3181     (if (INTEGRAL_TYPE_P (type)
3182          /* We check for type compatibility between @0 and @1 below,
3183             so there's no need to check that @1/@3 are integral types.  */
3184          && INTEGRAL_TYPE_P (TREE_TYPE (@0))
3185          && INTEGRAL_TYPE_P (TREE_TYPE (@2))
3186          /* The precision of the type of each operand must match the
3187             precision of the mode of each operand, similarly for the
3188             result.  */
3189          && (TYPE_PRECISION (TREE_TYPE (@0))
3190              == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (@0))))
3191          && (TYPE_PRECISION (TREE_TYPE (@1))
3192              == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (@1))))
3193          && TYPE_PRECISION (type) == GET_MODE_PRECISION (TYPE_MODE (type))
3194          /* The inner conversion must be a widening conversion.  */
3195          && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
3196          && types_match (@0, @1)
3197          && types_match (@0, type))
3198       (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
3199         (convert (op @0 @1))
3200         (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
3201          (convert (op (convert:utype @0) (convert:utype @1))))))))
3203 /* This is another case of narrowing, specifically when there's an outer
3204    BIT_AND_EXPR which masks off bits outside the type of the innermost
3205    operands.   Like the previous case we have to convert the operands
3206    to unsigned types to avoid introducing undefined behavior for the
3207    arithmetic operation.  */
3208 (for op (minus plus)
3209  (simplify
3210   (bit_and (op:s (convert@2 @0) (convert@3 @1)) INTEGER_CST@4)
3211   (if (INTEGRAL_TYPE_P (type)
3212        /* We check for type compatibility between @0 and @1 below,
3213           so there's no need to check that @1/@3 are integral types.  */
3214        && INTEGRAL_TYPE_P (TREE_TYPE (@0))
3215        && INTEGRAL_TYPE_P (TREE_TYPE (@2))
3216        /* The precision of the type of each operand must match the
3217           precision of the mode of each operand, similarly for the
3218           result.  */
3219        && (TYPE_PRECISION (TREE_TYPE (@0))
3220            == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (@0))))
3221        && (TYPE_PRECISION (TREE_TYPE (@1))
3222            == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (@1))))
3223        && TYPE_PRECISION (type) == GET_MODE_PRECISION (TYPE_MODE (type))
3224        /* The inner conversion must be a widening conversion.  */
3225        && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
3226        && types_match (@0, @1)
3227        && (tree_int_cst_min_precision (@4, TYPE_SIGN (TREE_TYPE (@0)))
3228            <= TYPE_PRECISION (TREE_TYPE (@0)))
3229        && (wi::bit_and (@4, wi::mask (TYPE_PRECISION (TREE_TYPE (@0)),
3230                         true, TYPE_PRECISION (type))) == 0))
3231    (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
3232     (with { tree ntype = TREE_TYPE (@0); }
3233      (convert (bit_and (op @0 @1) (convert:ntype @4))))
3234     (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
3235      (convert (bit_and (op (convert:utype @0) (convert:utype @1))
3236                (convert:utype @4))))))))
3238 /* Transform (@0 < @1 and @0 < @2) to use min, 
3239    (@0 > @1 and @0 > @2) to use max */
3240 (for op (lt le gt ge)
3241      ext (min min max max)
3242  (simplify
3243   (bit_and (op:s @0 @1) (op:s @0 @2))
3244   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)))
3245    (op @0 (ext @1 @2)))))
3247 (simplify
3248  /* signbit(x) -> 0 if x is nonnegative.  */
3249  (SIGNBIT tree_expr_nonnegative_p@0)
3250  { integer_zero_node; })
3252 (simplify
3253  /* signbit(x) -> x<0 if x doesn't have signed zeros.  */
3254  (SIGNBIT @0)
3255  (if (!HONOR_SIGNED_ZEROS (@0))
3256   (convert (lt @0 { build_real (TREE_TYPE (@0), dconst0); }))))
3258 /* Transform comparisons of the form X +- C1 CMP C2 to X CMP C2 -+ C1.  */
3259 (for cmp (eq ne)
3260  (for op (plus minus)
3261       rop (minus plus)
3262   (simplify
3263    (cmp (op@3 @0 INTEGER_CST@1) INTEGER_CST@2)
3264    (if (!TREE_OVERFLOW (@1) && !TREE_OVERFLOW (@2)
3265         && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0))
3266         && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@0))
3267         && !TYPE_SATURATING (TREE_TYPE (@0)))
3268     (with { tree res = int_const_binop (rop, @2, @1); }
3269      (if (TREE_OVERFLOW (res))
3270       { constant_boolean_node (cmp == NE_EXPR, type); }
3271       (if (single_use (@3))
3272        (cmp @0 { res; }))))))))
3273 (for cmp (lt le gt ge)
3274  (for op (plus minus)
3275       rop (minus plus)
3276   (simplify
3277    (cmp (op@3 @0 INTEGER_CST@1) INTEGER_CST@2)
3278    (if (!TREE_OVERFLOW (@1) && !TREE_OVERFLOW (@2)
3279         && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
3280     (with { tree res = int_const_binop (rop, @2, @1); }
3281      (if (TREE_OVERFLOW (res))
3282       {
3283         fold_overflow_warning (("assuming signed overflow does not occur "
3284                                 "when simplifying conditional to constant"),
3285                                WARN_STRICT_OVERFLOW_CONDITIONAL);
3286         bool less = cmp == LE_EXPR || cmp == LT_EXPR;
3287         /* wi::ges_p (@2, 0) should be sufficient for a signed type.  */
3288         bool ovf_high = wi::lt_p (@1, 0, TYPE_SIGN (TREE_TYPE (@1)))
3289                         != (op == MINUS_EXPR);
3290         constant_boolean_node (less == ovf_high, type);
3291       }
3292       (if (single_use (@3))
3293        (with
3294         {
3295           fold_overflow_warning (("assuming signed overflow does not occur "
3296                                   "when changing X +- C1 cmp C2 to "
3297                                   "X cmp C2 -+ C1"),
3298                                  WARN_STRICT_OVERFLOW_COMPARISON);
3299         }
3300         (cmp @0 { res; })))))))))
3302 /* Canonicalizations of BIT_FIELD_REFs.  */
3304 (simplify
3305  (BIT_FIELD_REF @0 @1 @2)
3306  (switch
3307   (if (TREE_CODE (TREE_TYPE (@0)) == COMPLEX_TYPE
3308        && tree_int_cst_equal (@1, TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))))
3309    (switch
3310     (if (integer_zerop (@2))
3311      (view_convert (realpart @0)))
3312     (if (tree_int_cst_equal (@2, TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))))
3313      (view_convert (imagpart @0)))))
3314   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
3315        && INTEGRAL_TYPE_P (type)
3316        /* On GIMPLE this should only apply to register arguments.  */
3317        && (! GIMPLE || is_gimple_reg (@0))
3318        /* A bit-field-ref that referenced the full argument can be stripped.  */
3319        && ((compare_tree_int (@1, TYPE_PRECISION (TREE_TYPE (@0))) == 0
3320             && integer_zerop (@2))
3321            /* Low-parts can be reduced to integral conversions.
3322               ???  The following doesn't work for PDP endian.  */
3323            || (BYTES_BIG_ENDIAN == WORDS_BIG_ENDIAN
3324                /* Don't even think about BITS_BIG_ENDIAN.  */
3325                && TYPE_PRECISION (TREE_TYPE (@0)) % BITS_PER_UNIT == 0
3326                && TYPE_PRECISION (type) % BITS_PER_UNIT == 0
3327                && compare_tree_int (@2, (BYTES_BIG_ENDIAN
3328                                          ? (TYPE_PRECISION (TREE_TYPE (@0))
3329                                             - TYPE_PRECISION (type))
3330                                          : 0)) == 0)))
3331    (convert @0))))
3333 /* Simplify vector extracts.  */
3335 (simplify
3336  (BIT_FIELD_REF CONSTRUCTOR@0 @1 @2)
3337  (if (VECTOR_TYPE_P (TREE_TYPE (@0))
3338       && (types_match (type, TREE_TYPE (TREE_TYPE (@0)))
3339           || (VECTOR_TYPE_P (type)
3340               && types_match (TREE_TYPE (type), TREE_TYPE (TREE_TYPE (@0))))))
3341   (with
3342    {
3343      tree ctor = (TREE_CODE (@0) == SSA_NAME
3344                   ? gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0)) : @0);
3345      tree eltype = TREE_TYPE (TREE_TYPE (ctor));
3346      unsigned HOST_WIDE_INT width = tree_to_uhwi (TYPE_SIZE (eltype));
3347      unsigned HOST_WIDE_INT n = tree_to_uhwi (@1);
3348      unsigned HOST_WIDE_INT idx = tree_to_uhwi (@2);
3349    }
3350    (if (n != 0
3351         && (idx % width) == 0
3352         && (n % width) == 0
3353         && ((idx + n) / width) <= TYPE_VECTOR_SUBPARTS (TREE_TYPE (ctor)))
3354     (with
3355      {
3356        idx = idx / width;
3357        n = n / width;
3358        /* Constructor elements can be subvectors.  */
3359        unsigned HOST_WIDE_INT k = 1;
3360        if (CONSTRUCTOR_NELTS (ctor) != 0)
3361          {
3362            tree cons_elem = TREE_TYPE (CONSTRUCTOR_ELT (ctor, 0)->value);
3363            if (TREE_CODE (cons_elem) == VECTOR_TYPE)
3364              k = TYPE_VECTOR_SUBPARTS (cons_elem);
3365          }
3366      }
3367      (switch
3368       /* We keep an exact subset of the constructor elements.  */
3369       (if ((idx % k) == 0 && (n % k) == 0)
3370        (if (CONSTRUCTOR_NELTS (ctor) == 0)
3371         { build_constructor (type, NULL); }
3372         (with
3373          {
3374            idx /= k;
3375            n /= k;
3376          }
3377          (if (n == 1)
3378           (if (idx < CONSTRUCTOR_NELTS (ctor))
3379            { CONSTRUCTOR_ELT (ctor, idx)->value; }
3380            { build_zero_cst (type); })
3381           {
3382             vec<constructor_elt, va_gc> *vals;
3383             vec_alloc (vals, n);
3384             for (unsigned i = 0;
3385                  i < n && idx + i < CONSTRUCTOR_NELTS (ctor); ++i)
3386               CONSTRUCTOR_APPEND_ELT (vals, NULL_TREE,
3387                                       CONSTRUCTOR_ELT (ctor, idx + i)->value);
3388             build_constructor (type, vals);
3389           }))))
3390       /* The bitfield references a single constructor element.  */
3391       (if (idx + n <= (idx / k + 1) * k)
3392        (switch
3393         (if (CONSTRUCTOR_NELTS (ctor) <= idx / k)
3394          { build_zero_cst (type); })
3395         (if (n == k)
3396          { CONSTRUCTOR_ELT (ctor, idx / k)->value; })
3397         (BIT_FIELD_REF { CONSTRUCTOR_ELT (ctor, idx / k)->value; }
3398                        @1 { bitsize_int ((idx % k) * width); })))))))))