[PATCH c++/86881] -Wshadow-local-compatible ICE
[official-gcc.git] / gcc / match.pd
blob74244f348a03951cbb4683a00a844d696f90ffad
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-2018 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    tree_expr_nonzero_p
35    integer_valued_real_p
36    integer_pow2p
37    HONOR_NANS)
39 /* Operator lists.  */
40 (define_operator_list tcc_comparison
41   lt   le   eq ne ge   gt   unordered ordered   unlt unle ungt unge uneq ltgt)
42 (define_operator_list inverted_tcc_comparison
43   ge   gt   ne eq lt   le   ordered   unordered ge   gt   le   lt   ltgt uneq)
44 (define_operator_list inverted_tcc_comparison_with_nans
45   unge ungt ne eq unlt unle ordered   unordered ge   gt   le   lt   ltgt uneq)
46 (define_operator_list swapped_tcc_comparison
47   gt   ge   eq ne le   lt   unordered ordered   ungt unge unlt unle uneq ltgt)
48 (define_operator_list simple_comparison         lt   le   eq ne ge   gt)
49 (define_operator_list swapped_simple_comparison gt   ge   eq ne le   lt)
51 #include "cfn-operators.pd"
53 /* Define operand lists for math rounding functions {,i,l,ll}FN,
54    where the versions prefixed with "i" return an int, those prefixed with
55    "l" return a long and those prefixed with "ll" return a long long.
57    Also define operand lists:
59      X<FN>F for all float functions, in the order i, l, ll
60      X<FN> for all double functions, in the same order
61      X<FN>L for all long double functions, in the same order.  */
62 #define DEFINE_INT_AND_FLOAT_ROUND_FN(FN) \
63   (define_operator_list X##FN##F BUILT_IN_I##FN##F \
64                                  BUILT_IN_L##FN##F \
65                                  BUILT_IN_LL##FN##F) \
66   (define_operator_list X##FN BUILT_IN_I##FN \
67                               BUILT_IN_L##FN \
68                               BUILT_IN_LL##FN) \
69   (define_operator_list X##FN##L BUILT_IN_I##FN##L \
70                                  BUILT_IN_L##FN##L \
71                                  BUILT_IN_LL##FN##L)
73 DEFINE_INT_AND_FLOAT_ROUND_FN (FLOOR)
74 DEFINE_INT_AND_FLOAT_ROUND_FN (CEIL)
75 DEFINE_INT_AND_FLOAT_ROUND_FN (ROUND)
76 DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
78 /* Binary operations and their associated IFN_COND_* function.  */
79 (define_operator_list UNCOND_BINARY
80   plus minus
81   mult trunc_div trunc_mod rdiv
82   min max
83   bit_and bit_ior bit_xor)
84 (define_operator_list COND_BINARY
85   IFN_COND_ADD IFN_COND_SUB
86   IFN_COND_MUL IFN_COND_DIV IFN_COND_MOD IFN_COND_RDIV
87   IFN_COND_MIN IFN_COND_MAX
88   IFN_COND_AND IFN_COND_IOR IFN_COND_XOR)
90 /* Same for ternary operations.  */
91 (define_operator_list UNCOND_TERNARY
92   IFN_FMA IFN_FMS IFN_FNMA IFN_FNMS)
93 (define_operator_list COND_TERNARY
94   IFN_COND_FMA IFN_COND_FMS IFN_COND_FNMA IFN_COND_FNMS)
95     
96 /* As opposed to convert?, this still creates a single pattern, so
97    it is not a suitable replacement for convert? in all cases.  */
98 (match (nop_convert @0)
99  (convert @0)
100  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))))
101 (match (nop_convert @0)
102  (view_convert @0)
103  (if (VECTOR_TYPE_P (type) && VECTOR_TYPE_P (TREE_TYPE (@0))
104       && known_eq (TYPE_VECTOR_SUBPARTS (type),
105                    TYPE_VECTOR_SUBPARTS (TREE_TYPE (@0)))
106       && tree_nop_conversion_p (TREE_TYPE (type), TREE_TYPE (TREE_TYPE (@0))))))
107 /* This one has to be last, or it shadows the others.  */
108 (match (nop_convert @0)
109  @0) 
111 /* Transform likes of (char) ABS_EXPR <(int) x> into (char) ABSU_EXPR <x>
112    ABSU_EXPR returns unsigned absolute value of the operand and the operand
113    of the ABSU_EXPR will have the corresponding signed type.  */
114 (simplify (abs (convert @0))
115  (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
116       && !TYPE_UNSIGNED (TREE_TYPE (@0))
117       && element_precision (type) > element_precision (TREE_TYPE (@0)))
118   (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
119    (convert (absu:utype @0)))))
122 /* Simplifications of operations with one constant operand and
123    simplifications to constants or single values.  */
125 (for op (plus pointer_plus minus bit_ior bit_xor)
126   (simplify
127     (op @0 integer_zerop)
128     (non_lvalue @0)))
130 /* 0 +p index -> (type)index */
131 (simplify
132  (pointer_plus integer_zerop @1)
133  (non_lvalue (convert @1)))
135 /* ptr - 0 -> (type)ptr */
136 (simplify
137  (pointer_diff @0 integer_zerop)
138  (convert @0))
140 /* See if ARG1 is zero and X + ARG1 reduces to X.
141    Likewise if the operands are reversed.  */
142 (simplify
143  (plus:c @0 real_zerop@1)
144  (if (fold_real_zero_addition_p (type, @1, 0))
145   (non_lvalue @0)))
147 /* See if ARG1 is zero and X - ARG1 reduces to X.  */
148 (simplify
149  (minus @0 real_zerop@1)
150  (if (fold_real_zero_addition_p (type, @1, 1))
151   (non_lvalue @0)))
153 /* Simplify x - x.
154    This is unsafe for certain floats even in non-IEEE formats.
155    In IEEE, it is unsafe because it does wrong for NaNs.
156    Also note that operand_equal_p is always false if an operand
157    is volatile.  */
158 (simplify
159  (minus @0 @0)
160  (if (!FLOAT_TYPE_P (type) || !HONOR_NANS (type))
161   { build_zero_cst (type); }))
162 (simplify
163  (pointer_diff @@0 @0)
164  { build_zero_cst (type); })
166 (simplify
167  (mult @0 integer_zerop@1)
168  @1)
170 /* Maybe fold x * 0 to 0.  The expressions aren't the same
171    when x is NaN, since x * 0 is also NaN.  Nor are they the
172    same in modes with signed zeros, since multiplying a
173    negative value by 0 gives -0, not +0.  */
174 (simplify
175  (mult @0 real_zerop@1)
176  (if (!HONOR_NANS (type) && !HONOR_SIGNED_ZEROS (type))
177   @1))
179 /* In IEEE floating point, x*1 is not equivalent to x for snans.
180    Likewise for complex arithmetic with signed zeros.  */
181 (simplify
182  (mult @0 real_onep)
183  (if (!HONOR_SNANS (type)
184       && (!HONOR_SIGNED_ZEROS (type)
185           || !COMPLEX_FLOAT_TYPE_P (type)))
186   (non_lvalue @0)))
188 /* Transform x * -1.0 into -x.  */
189 (simplify
190  (mult @0 real_minus_onep)
191   (if (!HONOR_SNANS (type)
192        && (!HONOR_SIGNED_ZEROS (type)
193            || !COMPLEX_FLOAT_TYPE_P (type)))
194    (negate @0)))
196 (for cmp (gt ge lt le)
197      outp (convert convert negate negate)
198      outn (negate negate convert convert)
199  /* Transform (X > 0.0 ? 1.0 : -1.0) into copysign(1, X). */
200  /* Transform (X >= 0.0 ? 1.0 : -1.0) into copysign(1, X). */
201  /* Transform (X < 0.0 ? 1.0 : -1.0) into copysign(1,-X). */
202  /* Transform (X <= 0.0 ? 1.0 : -1.0) into copysign(1,-X). */
203  (simplify
204   (cond (cmp @0 real_zerop) real_onep@1 real_minus_onep)
205   (if (!HONOR_NANS (type) && !HONOR_SIGNED_ZEROS (type)
206        && types_match (type, TREE_TYPE (@0)))
207    (switch
208     (if (types_match (type, float_type_node))
209      (BUILT_IN_COPYSIGNF @1 (outp @0)))
210     (if (types_match (type, double_type_node))
211      (BUILT_IN_COPYSIGN @1 (outp @0)))
212     (if (types_match (type, long_double_type_node))
213      (BUILT_IN_COPYSIGNL @1 (outp @0))))))
214  /* Transform (X > 0.0 ? -1.0 : 1.0) into copysign(1,-X). */
215  /* Transform (X >= 0.0 ? -1.0 : 1.0) into copysign(1,-X). */
216  /* Transform (X < 0.0 ? -1.0 : 1.0) into copysign(1,X). */
217  /* Transform (X <= 0.0 ? -1.0 : 1.0) into copysign(1,X). */
218  (simplify
219   (cond (cmp @0 real_zerop) real_minus_onep real_onep@1)
220   (if (!HONOR_NANS (type) && !HONOR_SIGNED_ZEROS (type)
221        && types_match (type, TREE_TYPE (@0)))
222    (switch
223     (if (types_match (type, float_type_node))
224      (BUILT_IN_COPYSIGNF @1 (outn @0)))
225     (if (types_match (type, double_type_node))
226      (BUILT_IN_COPYSIGN @1 (outn @0)))
227     (if (types_match (type, long_double_type_node))
228      (BUILT_IN_COPYSIGNL @1 (outn @0)))))))
230 /* Transform X * copysign (1.0, X) into abs(X). */
231 (simplify
232  (mult:c @0 (COPYSIGN_ALL real_onep @0))
233  (if (!HONOR_NANS (type) && !HONOR_SIGNED_ZEROS (type))
234   (abs @0)))
236 /* Transform X * copysign (1.0, -X) into -abs(X). */
237 (simplify
238  (mult:c @0 (COPYSIGN_ALL real_onep (negate @0)))
239  (if (!HONOR_NANS (type) && !HONOR_SIGNED_ZEROS (type))
240   (negate (abs @0))))
242 /* Transform copysign (CST, X) into copysign (ABS(CST), X). */
243 (simplify
244  (COPYSIGN_ALL REAL_CST@0 @1)
245  (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@0)))
246   (COPYSIGN_ALL (negate @0) @1)))
248 /* X * 1, X / 1 -> X.  */
249 (for op (mult trunc_div ceil_div floor_div round_div exact_div)
250   (simplify
251     (op @0 integer_onep)
252     (non_lvalue @0)))
254 /* (A / (1 << B)) -> (A >> B).
255    Only for unsigned A.  For signed A, this would not preserve rounding
256    toward zero.
257    For example: (-1 / ( 1 << B)) !=  -1 >> B.  */
258 (simplify
259  (trunc_div @0 (lshift integer_onep@1 @2))
260  (if ((TYPE_UNSIGNED (type) || tree_expr_nonnegative_p (@0))
261       && (!VECTOR_TYPE_P (type)
262           || target_supports_op_p (type, RSHIFT_EXPR, optab_vector)
263           || target_supports_op_p (type, RSHIFT_EXPR, optab_scalar)))
264   (rshift @0 @2)))
266 /* Preserve explicit divisions by 0: the C++ front-end wants to detect
267    undefined behavior in constexpr evaluation, and assuming that the division
268    traps enables better optimizations than these anyway.  */
269 (for div (trunc_div ceil_div floor_div round_div exact_div)
270  /* 0 / X is always zero.  */
271  (simplify
272   (div integer_zerop@0 @1)
273   /* But not for 0 / 0 so that we can get the proper warnings and errors.  */
274   (if (!integer_zerop (@1))
275    @0))
276   /* X / -1 is -X.  */
277  (simplify
278    (div @0 integer_minus_onep@1)
279    (if (!TYPE_UNSIGNED (type))
280     (negate @0)))
281  /* X / X is one.  */
282  (simplify
283   (div @0 @0)
284   /* But not for 0 / 0 so that we can get the proper warnings and errors.
285      And not for _Fract types where we can't build 1.  */
286   (if (!integer_zerop (@0) && !ALL_FRACT_MODE_P (TYPE_MODE (type)))
287    { build_one_cst (type); }))
288  /* X / abs (X) is X < 0 ? -1 : 1.  */ 
289  (simplify
290    (div:C @0 (abs @0))
291    (if (INTEGRAL_TYPE_P (type)
292         && TYPE_OVERFLOW_UNDEFINED (type))
293     (cond (lt @0 { build_zero_cst (type); })
294           { build_minus_one_cst (type); } { build_one_cst (type); })))
295  /* X / -X is -1.  */
296  (simplify
297    (div:C @0 (negate @0))
298    (if ((INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
299         && TYPE_OVERFLOW_UNDEFINED (type))
300     { build_minus_one_cst (type); })))
302 /* For unsigned integral types, FLOOR_DIV_EXPR is the same as
303    TRUNC_DIV_EXPR.  Rewrite into the latter in this case.  */
304 (simplify
305  (floor_div @0 @1)
306  (if ((INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
307       && TYPE_UNSIGNED (type))
308   (trunc_div @0 @1)))
310 /* Combine two successive divisions.  Note that combining ceil_div
311    and floor_div is trickier and combining round_div even more so.  */
312 (for div (trunc_div exact_div)
313  (simplify
314   (div (div @0 INTEGER_CST@1) INTEGER_CST@2)
315   (with {
316     wi::overflow_type overflow;
317     wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@2),
318                             TYPE_SIGN (type), &overflow);
319    }
320    (if (!overflow)
321     (div @0 { wide_int_to_tree (type, mul); })
322     (if (TYPE_UNSIGNED (type)
323          || mul != wi::min_value (TYPE_PRECISION (type), SIGNED))
324      { build_zero_cst (type); })))))
326 /* Combine successive multiplications.  Similar to above, but handling
327    overflow is different.  */
328 (simplify
329  (mult (mult @0 INTEGER_CST@1) INTEGER_CST@2)
330  (with {
331    wi::overflow_type overflow;
332    wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@2),
333                            TYPE_SIGN (type), &overflow);
334   }
335   /* Skip folding on overflow: the only special case is @1 * @2 == -INT_MIN,
336      otherwise undefined overflow implies that @0 must be zero.  */
337   (if (!overflow || TYPE_OVERFLOW_WRAPS (type))
338    (mult @0 { wide_int_to_tree (type, mul); }))))
340 /* Optimize A / A to 1.0 if we don't care about
341    NaNs or Infinities.  */
342 (simplify
343  (rdiv @0 @0)
344  (if (FLOAT_TYPE_P (type)
345       && ! HONOR_NANS (type)
346       && ! HONOR_INFINITIES (type))
347   { build_one_cst (type); }))
349 /* Optimize -A / A to -1.0 if we don't care about
350    NaNs or Infinities.  */
351 (simplify
352  (rdiv:C @0 (negate @0))
353  (if (FLOAT_TYPE_P (type)
354       && ! HONOR_NANS (type)
355       && ! HONOR_INFINITIES (type))
356   { build_minus_one_cst (type); }))
358 /* PR71078: x / abs(x) -> copysign (1.0, x) */
359 (simplify
360  (rdiv:C (convert? @0) (convert? (abs @0)))
361   (if (SCALAR_FLOAT_TYPE_P (type)
362        && ! HONOR_NANS (type)
363        && ! HONOR_INFINITIES (type))
364    (switch
365     (if (types_match (type, float_type_node))
366      (BUILT_IN_COPYSIGNF { build_one_cst (type); } (convert @0)))
367     (if (types_match (type, double_type_node))
368      (BUILT_IN_COPYSIGN { build_one_cst (type); } (convert @0)))
369     (if (types_match (type, long_double_type_node))
370      (BUILT_IN_COPYSIGNL { build_one_cst (type); } (convert @0))))))
372 /* In IEEE floating point, x/1 is not equivalent to x for snans.  */
373 (simplify
374  (rdiv @0 real_onep)
375  (if (!HONOR_SNANS (type))
376   (non_lvalue @0)))
378 /* In IEEE floating point, x/-1 is not equivalent to -x for snans.  */
379 (simplify
380  (rdiv @0 real_minus_onep)
381  (if (!HONOR_SNANS (type))
382   (negate @0)))
384 (if (flag_reciprocal_math)
385  /* Convert (A/B)/C to A/(B*C). */
386  (simplify
387   (rdiv (rdiv:s @0 @1) @2)
388   (rdiv @0 (mult @1 @2)))
390  /* Canonicalize x / (C1 * y) to (x * C2) / y.  */
391  (simplify
392   (rdiv @0 (mult:s @1 REAL_CST@2))
393   (with
394    { tree tem = const_binop (RDIV_EXPR, type, build_one_cst (type), @2); }
395    (if (tem)
396     (rdiv (mult @0 { tem; } ) @1))))
398  /* Convert A/(B/C) to (A/B)*C  */
399  (simplify
400   (rdiv @0 (rdiv:s @1 @2))
401    (mult (rdiv @0 @1) @2)))
403 /* Simplify x / (- y) to -x / y.  */
404 (simplify
405  (rdiv @0 (negate @1))
406  (rdiv (negate @0) @1))
408 /* Optimize (X & (-A)) / A where A is a power of 2, to X >> log2(A) */
409 (for div (trunc_div ceil_div floor_div round_div exact_div)
410  (simplify
411   (div (convert? (bit_and @0 INTEGER_CST@1)) INTEGER_CST@2)
412   (if (integer_pow2p (@2)
413        && tree_int_cst_sgn (@2) > 0
414        && tree_nop_conversion_p (type, TREE_TYPE (@0))
415        && wi::to_wide (@2) + wi::to_wide (@1) == 0)
416    (rshift (convert @0)
417            { build_int_cst (integer_type_node,
418                             wi::exact_log2 (wi::to_wide (@2))); }))))
420 /* If ARG1 is a constant, we can convert this to a multiply by the
421    reciprocal.  This does not have the same rounding properties,
422    so only do this if -freciprocal-math.  We can actually
423    always safely do it if ARG1 is a power of two, but it's hard to
424    tell if it is or not in a portable manner.  */
425 (for cst (REAL_CST COMPLEX_CST VECTOR_CST)
426  (simplify
427   (rdiv @0 cst@1)
428   (if (optimize)
429    (if (flag_reciprocal_math
430         && !real_zerop (@1))
431     (with
432      { tree tem = const_binop (RDIV_EXPR, type, build_one_cst (type), @1); }
433      (if (tem)
434       (mult @0 { tem; } )))
435     (if (cst != COMPLEX_CST)
436      (with { tree inverse = exact_inverse (type, @1); }
437       (if (inverse)
438        (mult @0 { inverse; } ))))))))
440 (for mod (ceil_mod floor_mod round_mod trunc_mod)
441  /* 0 % X is always zero.  */
442  (simplify
443   (mod integer_zerop@0 @1)
444   /* But not for 0 % 0 so that we can get the proper warnings and errors.  */
445   (if (!integer_zerop (@1))
446    @0))
447  /* X % 1 is always zero.  */
448  (simplify
449   (mod @0 integer_onep)
450   { build_zero_cst (type); })
451  /* X % -1 is zero.  */
452  (simplify
453   (mod @0 integer_minus_onep@1)
454   (if (!TYPE_UNSIGNED (type))
455    { build_zero_cst (type); }))
456  /* X % X is zero.  */
457  (simplify
458   (mod @0 @0)
459   /* But not for 0 % 0 so that we can get the proper warnings and errors.  */
460   (if (!integer_zerop (@0))
461    { build_zero_cst (type); }))
462  /* (X % Y) % Y is just X % Y.  */
463  (simplify
464   (mod (mod@2 @0 @1) @1)
465   @2)
466  /* From extract_muldiv_1: (X * C1) % C2 is zero if C1 is a multiple of C2.  */
467  (simplify
468   (mod (mult @0 INTEGER_CST@1) INTEGER_CST@2)
469   (if (ANY_INTEGRAL_TYPE_P (type)
470        && TYPE_OVERFLOW_UNDEFINED (type)
471        && wi::multiple_of_p (wi::to_wide (@1), wi::to_wide (@2),
472                              TYPE_SIGN (type)))
473    { build_zero_cst (type); }))
474  /* For (X % C) == 0, if X is signed and C is power of 2, use unsigned
475     modulo and comparison, since it is simpler and equivalent.  */
476  (for cmp (eq ne)
477   (simplify
478    (cmp (mod @0 integer_pow2p@2) integer_zerop@1)
479    (if (!TYPE_UNSIGNED (TREE_TYPE (@0)))
480     (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
481      (cmp (mod (convert:utype @0) (convert:utype @2)) (convert:utype @1)))))))
483 /* X % -C is the same as X % C.  */
484 (simplify
485  (trunc_mod @0 INTEGER_CST@1)
486   (if (TYPE_SIGN (type) == SIGNED
487        && !TREE_OVERFLOW (@1)
488        && wi::neg_p (wi::to_wide (@1))
489        && !TYPE_OVERFLOW_TRAPS (type)
490        /* Avoid this transformation if C is INT_MIN, i.e. C == -C.  */
491        && !sign_bit_p (@1, @1))
492    (trunc_mod @0 (negate @1))))
494 /* X % -Y is the same as X % Y.  */
495 (simplify
496  (trunc_mod @0 (convert? (negate @1)))
497  (if (INTEGRAL_TYPE_P (type)
498       && !TYPE_UNSIGNED (type)
499       && !TYPE_OVERFLOW_TRAPS (type)
500       && tree_nop_conversion_p (type, TREE_TYPE (@1))
501       /* Avoid this transformation if X might be INT_MIN or
502          Y might be -1, because we would then change valid
503          INT_MIN % -(-1) into invalid INT_MIN % -1.  */
504       && (expr_not_equal_to (@0, wi::to_wide (TYPE_MIN_VALUE (type)))
505           || expr_not_equal_to (@1, wi::minus_one (TYPE_PRECISION
506                                                         (TREE_TYPE (@1))))))
507   (trunc_mod @0 (convert @1))))
509 /* X - (X / Y) * Y is the same as X % Y.  */
510 (simplify
511  (minus (convert1? @0) (convert2? (mult:c (trunc_div @@0 @@1) @1)))
512  (if (INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
513   (convert (trunc_mod @0 @1))))
515 /* Optimize TRUNC_MOD_EXPR by a power of two into a BIT_AND_EXPR,
516    i.e. "X % C" into "X & (C - 1)", if X and C are positive.
517    Also optimize A % (C << N)  where C is a power of 2,
518    to A & ((C << N) - 1).  */
519 (match (power_of_two_cand @1)
520  INTEGER_CST@1)
521 (match (power_of_two_cand @1)
522  (lshift INTEGER_CST@1 @2))
523 (for mod (trunc_mod floor_mod)
524  (simplify
525   (mod @0 (convert?@3 (power_of_two_cand@1 @2)))
526   (if ((TYPE_UNSIGNED (type)
527         || tree_expr_nonnegative_p (@0))
528         && tree_nop_conversion_p (type, TREE_TYPE (@3))
529         && integer_pow2p (@2) && tree_int_cst_sgn (@2) > 0)
530    (bit_and @0 (convert (minus @1 { build_int_cst (TREE_TYPE (@1), 1); }))))))
532 /* Simplify (unsigned t * 2)/2 -> unsigned t & 0x7FFFFFFF.  */
533 (simplify
534  (trunc_div (mult @0 integer_pow2p@1) @1)
535  (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
536   (bit_and @0 { wide_int_to_tree
537                 (type, wi::mask (TYPE_PRECISION (type)
538                                  - wi::exact_log2 (wi::to_wide (@1)),
539                                  false, TYPE_PRECISION (type))); })))
541 /* Simplify (unsigned t / 2) * 2 -> unsigned t & ~1.  */
542 (simplify
543  (mult (trunc_div @0 integer_pow2p@1) @1)
544  (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
545   (bit_and @0 (negate @1))))
547 /* Simplify (t * 2) / 2) -> t.  */
548 (for div (trunc_div ceil_div floor_div round_div exact_div)
549  (simplify
550   (div (mult:c @0 @1) @1)
551   (if (ANY_INTEGRAL_TYPE_P (type)
552        && TYPE_OVERFLOW_UNDEFINED (type))
553    @0)))
555 (for op (negate abs)
556  /* Simplify cos(-x) and cos(|x|) -> cos(x).  Similarly for cosh.  */
557  (for coss (COS COSH)
558   (simplify
559    (coss (op @0))
560     (coss @0)))
561  /* Simplify pow(-x, y) and pow(|x|,y) -> pow(x,y) if y is an even integer.  */
562  (for pows (POW)
563   (simplify
564    (pows (op @0) REAL_CST@1)
565    (with { HOST_WIDE_INT n; }
566     (if (real_isinteger (&TREE_REAL_CST (@1), &n) && (n & 1) == 0)
567      (pows @0 @1)))))
568  /* Likewise for powi.  */
569  (for pows (POWI)
570   (simplify
571    (pows (op @0) INTEGER_CST@1)
572    (if ((wi::to_wide (@1) & 1) == 0)
573     (pows @0 @1))))
574  /* Strip negate and abs from both operands of hypot.  */
575  (for hypots (HYPOT)
576   (simplify
577    (hypots (op @0) @1)
578    (hypots @0 @1))
579   (simplify
580    (hypots @0 (op @1))
581    (hypots @0 @1)))
582  /* copysign(-x, y) and copysign(abs(x), y) -> copysign(x, y).  */
583  (for copysigns (COPYSIGN_ALL)
584   (simplify
585    (copysigns (op @0) @1)
586    (copysigns @0 @1))))
588 /* abs(x)*abs(x) -> x*x.  Should be valid for all types.  */
589 (simplify
590  (mult (abs@1 @0) @1)
591  (mult @0 @0))
593 /* cos(copysign(x, y)) -> cos(x).  Similarly for cosh.  */
594 (for coss (COS COSH)
595      copysigns (COPYSIGN)
596  (simplify
597   (coss (copysigns @0 @1))
598    (coss @0)))
600 /* pow(copysign(x, y), z) -> pow(x, z) if z is an even integer.  */
601 (for pows (POW)
602      copysigns (COPYSIGN)
603  (simplify
604   (pows (copysigns @0 @2) REAL_CST@1)
605   (with { HOST_WIDE_INT n; }
606    (if (real_isinteger (&TREE_REAL_CST (@1), &n) && (n & 1) == 0)
607     (pows @0 @1)))))
608 /* Likewise for powi.  */
609 (for pows (POWI)
610      copysigns (COPYSIGN)
611  (simplify
612   (pows (copysigns @0 @2) INTEGER_CST@1)
613   (if ((wi::to_wide (@1) & 1) == 0)
614    (pows @0 @1))))
616 (for hypots (HYPOT)
617      copysigns (COPYSIGN)
618  /* hypot(copysign(x, y), z) -> hypot(x, z).  */
619  (simplify
620   (hypots (copysigns @0 @1) @2)
621   (hypots @0 @2))
622  /* hypot(x, copysign(y, z)) -> hypot(x, y).  */
623  (simplify
624   (hypots @0 (copysigns @1 @2))
625   (hypots @0 @1)))
627 /* copysign(x, CST) -> [-]abs (x).  */
628 (for copysigns (COPYSIGN_ALL)
629  (simplify
630   (copysigns @0 REAL_CST@1)
631   (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
632    (negate (abs @0))
633    (abs @0))))
635 /* copysign(copysign(x, y), z) -> copysign(x, z).  */
636 (for copysigns (COPYSIGN_ALL)
637  (simplify
638   (copysigns (copysigns @0 @1) @2)
639   (copysigns @0 @2)))
641 /* copysign(x,y)*copysign(x,y) -> x*x.  */
642 (for copysigns (COPYSIGN_ALL)
643  (simplify
644   (mult (copysigns@2 @0 @1) @2)
645   (mult @0 @0)))
647 /* ccos(-x) -> ccos(x).  Similarly for ccosh.  */
648 (for ccoss (CCOS CCOSH)
649  (simplify
650   (ccoss (negate @0))
651    (ccoss @0)))
653 /* cabs(-x) and cos(conj(x)) -> cabs(x).  */
654 (for ops (conj negate)
655  (for cabss (CABS)
656   (simplify
657    (cabss (ops @0))
658    (cabss @0))))
660 /* Fold (a * (1 << b)) into (a << b)  */
661 (simplify
662  (mult:c @0 (convert? (lshift integer_onep@1 @2)))
663   (if (! FLOAT_TYPE_P (type)
664        && tree_nop_conversion_p (type, TREE_TYPE (@1)))
665    (lshift @0 @2)))
667 /* Fold (1 << (C - x)) where C = precision(type) - 1
668    into ((1 << C) >> x). */
669 (simplify
670  (lshift integer_onep@0 (minus@1 INTEGER_CST@2 @3))
671   (if (INTEGRAL_TYPE_P (type)
672        && wi::eq_p (wi::to_wide (@2), TYPE_PRECISION (type) - 1)
673        && single_use (@1))
674    (if (TYPE_UNSIGNED (type))
675      (rshift (lshift @0 @2) @3)
676    (with
677     { tree utype = unsigned_type_for (type); }
678     (convert (rshift (lshift (convert:utype @0) @2) @3))))))
680 /* Fold (C1/X)*C2 into (C1*C2)/X.  */
681 (simplify
682  (mult (rdiv@3 REAL_CST@0 @1) REAL_CST@2)
683   (if (flag_associative_math
684        && single_use (@3))
685    (with
686     { tree tem = const_binop (MULT_EXPR, type, @0, @2); }
687     (if (tem)
688      (rdiv { tem; } @1)))))
690 /* Simplify ~X & X as zero.  */
691 (simplify
692  (bit_and:c (convert? @0) (convert? (bit_not @0)))
693   { build_zero_cst (type); })
695 /* PR71636: Transform x & ((1U << b) - 1) -> x & ~(~0U << b);  */
696 (simplify
697   (bit_and:c @0 (plus:s (lshift:s integer_onep @1) integer_minus_onep))
698   (if (TYPE_UNSIGNED (type))
699     (bit_and @0 (bit_not (lshift { build_all_ones_cst (type); } @1)))))
701 (for bitop (bit_and bit_ior)
702      cmp (eq ne)
703  /* PR35691: Transform
704     (x == 0 & y == 0) -> (x | typeof(x)(y)) == 0.
705     (x != 0 | y != 0) -> (x | typeof(x)(y)) != 0.  */
706  (simplify
707   (bitop (cmp @0 integer_zerop@2) (cmp @1 integer_zerop))
708    (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
709         && INTEGRAL_TYPE_P (TREE_TYPE (@1))
710         && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
711     (cmp (bit_ior @0 (convert @1)) @2)))
712  /* Transform:
713     (x == -1 & y == -1) -> (x & typeof(x)(y)) == -1.
714     (x != -1 | y != -1) -> (x & typeof(x)(y)) != -1.  */
715  (simplify
716   (bitop (cmp @0 integer_all_onesp@2) (cmp @1 integer_all_onesp))
717    (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
718         && INTEGRAL_TYPE_P (TREE_TYPE (@1))
719         && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
720     (cmp (bit_and @0 (convert @1)) @2))))
722 /* Fold (A & ~B) - (A & B) into (A ^ B) - B.  */
723 (simplify
724  (minus (bit_and:cs @0 (bit_not @1)) (bit_and:cs @0 @1))
725   (minus (bit_xor @0 @1) @1))
726 (simplify
727  (minus (bit_and:s @0 INTEGER_CST@2) (bit_and:s @0 INTEGER_CST@1))
728  (if (~wi::to_wide (@2) == wi::to_wide (@1))
729   (minus (bit_xor @0 @1) @1)))
731 /* Fold (A & B) - (A & ~B) into B - (A ^ B).  */
732 (simplify
733  (minus (bit_and:cs @0 @1) (bit_and:cs @0 (bit_not @1)))
734   (minus @1 (bit_xor @0 @1)))
736 /* Simplify (X & ~Y) |^+ (~X & Y) -> X ^ Y.  */
737 (for op (bit_ior bit_xor plus)
738  (simplify
739   (op (bit_and:c @0 (bit_not @1)) (bit_and:c (bit_not @0) @1))
740    (bit_xor @0 @1))
741  (simplify
742   (op:c (bit_and @0 INTEGER_CST@2) (bit_and (bit_not @0) INTEGER_CST@1))
743   (if (~wi::to_wide (@2) == wi::to_wide (@1))
744    (bit_xor @0 @1))))
746 /* PR53979: Transform ((a ^ b) | a) -> (a | b) */
747 (simplify
748   (bit_ior:c (bit_xor:c @0 @1) @0)
749   (bit_ior @0 @1))
751 /* (a & ~b) | (a ^ b)  -->  a ^ b  */
752 (simplify
753  (bit_ior:c (bit_and:c @0 (bit_not @1)) (bit_xor:c@2 @0 @1))
754  @2)
756 /* (a & ~b) ^ ~a  -->  ~(a & b)  */
757 (simplify
758  (bit_xor:c (bit_and:cs @0 (bit_not @1)) (bit_not @0))
759  (bit_not (bit_and @0 @1)))
761 /* (a | b) & ~(a ^ b)  -->  a & b  */
762 (simplify
763  (bit_and:c (bit_ior @0 @1) (bit_not (bit_xor:c @0 @1)))
764  (bit_and @0 @1))
766 /* a | ~(a ^ b)  -->  a | ~b  */
767 (simplify
768  (bit_ior:c @0 (bit_not:s (bit_xor:c @0 @1)))
769  (bit_ior @0 (bit_not @1)))
771 /* (a | b) | (a &^ b)  -->  a | b  */
772 (for op (bit_and bit_xor)
773  (simplify
774   (bit_ior:c (bit_ior@2 @0 @1) (op:c @0 @1))
775   @2))
777 /* (a & b) | ~(a ^ b)  -->  ~(a ^ b)  */
778 (simplify
779  (bit_ior:c (bit_and:c @0 @1) (bit_not@2 (bit_xor @0 @1)))
780  @2)
782 /* ~(~a & b)  -->  a | ~b  */
783 (simplify
784  (bit_not (bit_and:cs (bit_not @0) @1))
785  (bit_ior @0 (bit_not @1)))
787 /* ~(~a | b) --> a & ~b */
788 (simplify
789  (bit_not (bit_ior:cs (bit_not @0) @1))
790  (bit_and @0 (bit_not @1)))
792 /* Simplify (~X & Y) to X ^ Y if we know that (X & ~Y) is 0.  */
793 #if GIMPLE
794 (simplify
795  (bit_and (bit_not SSA_NAME@0) INTEGER_CST@1)
796  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
797       && wi::bit_and_not (get_nonzero_bits (@0), wi::to_wide (@1)) == 0)
798   (bit_xor @0 @1)))
799 #endif
801 /* For constants M and N, if M == (1LL << cst) - 1 && (N & M) == M,
802    ((A & N) + B) & M -> (A + B) & M
803    Similarly if (N & M) == 0,
804    ((A | N) + B) & M -> (A + B) & M
805    and for - instead of + (or unary - instead of +)
806    and/or ^ instead of |.
807    If B is constant and (B & M) == 0, fold into A & M.  */
808 (for op (plus minus)
809  (for bitop (bit_and bit_ior bit_xor)
810   (simplify
811    (bit_and (op:s (bitop:s@0 @3 INTEGER_CST@4) @1) INTEGER_CST@2)
812     (with
813      { tree pmop[2];
814        tree utype = fold_bit_and_mask (TREE_TYPE (@0), @2, op, @0, bitop,
815                                        @3, @4, @1, ERROR_MARK, NULL_TREE,
816                                        NULL_TREE, pmop); }
817      (if (utype)
818       (convert (bit_and (op (convert:utype { pmop[0]; })
819                             (convert:utype { pmop[1]; }))
820                         (convert:utype @2))))))
821   (simplify
822    (bit_and (op:s @0 (bitop:s@1 @3 INTEGER_CST@4)) INTEGER_CST@2)
823     (with
824      { tree pmop[2];
825        tree utype = fold_bit_and_mask (TREE_TYPE (@0), @2, op, @0, ERROR_MARK,
826                                        NULL_TREE, NULL_TREE, @1, bitop, @3,
827                                        @4, pmop); }
828      (if (utype)
829       (convert (bit_and (op (convert:utype { pmop[0]; })
830                             (convert:utype { pmop[1]; }))
831                         (convert:utype @2)))))))
832  (simplify
833   (bit_and (op:s @0 @1) INTEGER_CST@2)
834    (with
835     { tree pmop[2];
836       tree utype = fold_bit_and_mask (TREE_TYPE (@0), @2, op, @0, ERROR_MARK,
837                                       NULL_TREE, NULL_TREE, @1, ERROR_MARK,
838                                       NULL_TREE, NULL_TREE, pmop); }
839     (if (utype)
840      (convert (bit_and (op (convert:utype { pmop[0]; })
841                            (convert:utype { pmop[1]; }))
842                        (convert:utype @2)))))))
843 (for bitop (bit_and bit_ior bit_xor)
844  (simplify
845   (bit_and (negate:s (bitop:s@0 @2 INTEGER_CST@3)) INTEGER_CST@1)
846    (with
847     { tree pmop[2];
848       tree utype = fold_bit_and_mask (TREE_TYPE (@0), @1, NEGATE_EXPR, @0,
849                                       bitop, @2, @3, NULL_TREE, ERROR_MARK,
850                                       NULL_TREE, NULL_TREE, pmop); }
851     (if (utype)
852      (convert (bit_and (negate (convert:utype { pmop[0]; }))
853                        (convert:utype @1)))))))
855 /* X % Y is smaller than Y.  */
856 (for cmp (lt ge)
857  (simplify
858   (cmp (trunc_mod @0 @1) @1)
859   (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
860    { constant_boolean_node (cmp == LT_EXPR, type); })))
861 (for cmp (gt le)
862  (simplify
863   (cmp @1 (trunc_mod @0 @1))
864   (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
865    { constant_boolean_node (cmp == GT_EXPR, type); })))
867 /* x | ~0 -> ~0  */
868 (simplify
869  (bit_ior @0 integer_all_onesp@1)
870  @1)
872 /* x | 0 -> x  */
873 (simplify
874  (bit_ior @0 integer_zerop)
875  @0)
877 /* x & 0 -> 0  */
878 (simplify
879  (bit_and @0 integer_zerop@1)
880  @1)
882 /* ~x | x -> -1 */
883 /* ~x ^ x -> -1 */
884 /* ~x + x -> -1 */
885 (for op (bit_ior bit_xor plus)
886  (simplify
887   (op:c (convert? @0) (convert? (bit_not @0)))
888   (convert { build_all_ones_cst (TREE_TYPE (@0)); })))
890 /* x ^ x -> 0 */
891 (simplify
892   (bit_xor @0 @0)
893   { build_zero_cst (type); })
895 /* Canonicalize X ^ ~0 to ~X.  */
896 (simplify
897   (bit_xor @0 integer_all_onesp@1)
898   (bit_not @0))
900 /* x & ~0 -> x  */
901 (simplify
902  (bit_and @0 integer_all_onesp)
903   (non_lvalue @0))
905 /* x & x -> x,  x | x -> x  */
906 (for bitop (bit_and bit_ior)
907  (simplify
908   (bitop @0 @0)
909   (non_lvalue @0)))
911 /* x & C -> x if we know that x & ~C == 0.  */
912 #if GIMPLE
913 (simplify
914  (bit_and SSA_NAME@0 INTEGER_CST@1)
915  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
916       && wi::bit_and_not (get_nonzero_bits (@0), wi::to_wide (@1)) == 0)
917   @0))
918 #endif
920 /* x + (x & 1) -> (x + 1) & ~1 */
921 (simplify
922  (plus:c @0 (bit_and:s @0 integer_onep@1))
923  (bit_and (plus @0 @1) (bit_not @1)))
925 /* x & ~(x & y) -> x & ~y */
926 /* x | ~(x | y) -> x | ~y  */
927 (for bitop (bit_and bit_ior)
928  (simplify
929   (bitop:c @0 (bit_not (bitop:cs @0 @1)))
930   (bitop @0 (bit_not @1))))
932 /* (x | y) & ~x -> y & ~x */
933 /* (x & y) | ~x -> y | ~x */
934 (for bitop (bit_and bit_ior)
935      rbitop (bit_ior bit_and)
936  (simplify
937   (bitop:c (rbitop:c @0 @1) (bit_not@2 @0))
938   (bitop @1 @2)))
940 /* (x & y) ^ (x | y) -> x ^ y */
941 (simplify
942  (bit_xor:c (bit_and @0 @1) (bit_ior @0 @1))
943  (bit_xor @0 @1))
945 /* (x ^ y) ^ (x | y) -> x & y */
946 (simplify
947  (bit_xor:c (bit_xor @0 @1) (bit_ior @0 @1))
948  (bit_and @0 @1))
950 /* (x & y) + (x ^ y) -> x | y */
951 /* (x & y) | (x ^ y) -> x | y */
952 /* (x & y) ^ (x ^ y) -> x | y */
953 (for op (plus bit_ior bit_xor)
954  (simplify
955   (op:c (bit_and @0 @1) (bit_xor @0 @1))
956   (bit_ior @0 @1)))
958 /* (x & y) + (x | y) -> x + y */
959 (simplify
960  (plus:c (bit_and @0 @1) (bit_ior @0 @1))
961  (plus @0 @1))
963 /* (x + y) - (x | y) -> x & y */
964 (simplify
965  (minus (plus @0 @1) (bit_ior @0 @1))
966  (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
967       && !TYPE_SATURATING (type))
968   (bit_and @0 @1)))
970 /* (x + y) - (x & y) -> x | y */
971 (simplify
972  (minus (plus @0 @1) (bit_and @0 @1))
973  (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
974       && !TYPE_SATURATING (type))
975   (bit_ior @0 @1)))
977 /* (x | y) - (x ^ y) -> x & y */
978 (simplify
979  (minus (bit_ior @0 @1) (bit_xor @0 @1))
980  (bit_and @0 @1))
982 /* (x | y) - (x & y) -> x ^ y */
983 (simplify
984  (minus (bit_ior @0 @1) (bit_and @0 @1))
985  (bit_xor @0 @1))
987 /* (x | y) & ~(x & y) -> x ^ y */
988 (simplify
989  (bit_and:c (bit_ior @0 @1) (bit_not (bit_and @0 @1)))
990  (bit_xor @0 @1))
992 /* (x | y) & (~x ^ y) -> x & y */
993 (simplify
994  (bit_and:c (bit_ior:c @0 @1) (bit_xor:c @1 (bit_not @0)))
995  (bit_and @0 @1))
997 /* (~x | y) & (x | ~y) -> ~(x ^ y) */
998 (simplify
999  (bit_and (bit_ior:cs (bit_not @0) @1) (bit_ior:cs @0 (bit_not @1)))
1000  (bit_not (bit_xor @0 @1)))
1002 /* (~x | y) ^ (x | ~y) -> x ^ y */
1003 (simplify
1004  (bit_xor (bit_ior:c (bit_not @0) @1) (bit_ior:c @0 (bit_not @1)))
1005  (bit_xor @0 @1))
1007 /* ~x & ~y -> ~(x | y)
1008    ~x | ~y -> ~(x & y) */
1009 (for op (bit_and bit_ior)
1010      rop (bit_ior bit_and)
1011  (simplify
1012   (op (convert1? (bit_not @0)) (convert2? (bit_not @1)))
1013   (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1014        && element_precision (type) <= element_precision (TREE_TYPE (@1)))
1015    (bit_not (rop (convert @0) (convert @1))))))
1017 /* If we are XORing or adding two BIT_AND_EXPR's, both of which are and'ing
1018    with a constant, and the two constants have no bits in common,
1019    we should treat this as a BIT_IOR_EXPR since this may produce more
1020    simplifications.  */
1021 (for op (bit_xor plus)
1022  (simplify
1023   (op (convert1? (bit_and@4 @0 INTEGER_CST@1))
1024       (convert2? (bit_and@5 @2 INTEGER_CST@3)))
1025   (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
1026        && tree_nop_conversion_p (type, TREE_TYPE (@2))
1027        && (wi::to_wide (@1) & wi::to_wide (@3)) == 0)
1028    (bit_ior (convert @4) (convert @5)))))
1030 /* (X | Y) ^ X -> Y & ~ X*/
1031 (simplify
1032  (bit_xor:c (convert1? (bit_ior:c @@0 @1)) (convert2? @0))
1033  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1034   (convert (bit_and @1 (bit_not @0)))))
1036 /* Convert ~X ^ ~Y to X ^ Y.  */
1037 (simplify
1038  (bit_xor (convert1? (bit_not @0)) (convert2? (bit_not @1)))
1039  (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1040       && element_precision (type) <= element_precision (TREE_TYPE (@1)))
1041   (bit_xor (convert @0) (convert @1))))
1043 /* Convert ~X ^ C to X ^ ~C.  */
1044 (simplify
1045  (bit_xor (convert? (bit_not @0)) INTEGER_CST@1)
1046  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1047   (bit_xor (convert @0) (bit_not @1))))
1049 /* Fold (X & Y) ^ Y and (X ^ Y) & Y as ~X & Y.  */
1050 (for opo (bit_and bit_xor)
1051      opi (bit_xor bit_and)
1052  (simplify
1053   (opo:c (opi:cs @0 @1) @1)
1054   (bit_and (bit_not @0) @1)))
1056 /* Given a bit-wise operation CODE applied to ARG0 and ARG1, see if both
1057    operands are another bit-wise operation with a common input.  If so,
1058    distribute the bit operations to save an operation and possibly two if
1059    constants are involved.  For example, convert
1060      (A | B) & (A | C) into A | (B & C)
1061    Further simplification will occur if B and C are constants.  */
1062 (for op (bit_and bit_ior bit_xor)
1063      rop (bit_ior bit_and bit_and)
1064  (simplify
1065   (op (convert? (rop:c @@0 @1)) (convert? (rop:c @0 @2)))
1066   (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1067        && tree_nop_conversion_p (type, TREE_TYPE (@2)))
1068    (rop (convert @0) (op (convert @1) (convert @2))))))
1070 /* Some simple reassociation for bit operations, also handled in reassoc.  */
1071 /* (X & Y) & Y -> X & Y
1072    (X | Y) | Y -> X | Y  */
1073 (for op (bit_and bit_ior)
1074  (simplify
1075   (op:c (convert1?@2 (op:c @0 @@1)) (convert2? @1))
1076   @2))
1077 /* (X ^ Y) ^ Y -> X  */
1078 (simplify
1079  (bit_xor:c (convert1? (bit_xor:c @0 @@1)) (convert2? @1))
1080  (convert @0))
1081 /* (X & Y) & (X & Z) -> (X & Y) & Z
1082    (X | Y) | (X | Z) -> (X | Y) | Z  */
1083 (for op (bit_and bit_ior)
1084  (simplify
1085   (op (convert1?@3 (op:c@4 @0 @1)) (convert2?@5 (op:c@6 @0 @2)))
1086   (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1087        && tree_nop_conversion_p (type, TREE_TYPE (@2)))
1088    (if (single_use (@5) && single_use (@6))
1089     (op @3 (convert @2))
1090     (if (single_use (@3) && single_use (@4))
1091      (op (convert @1) @5))))))
1092 /* (X ^ Y) ^ (X ^ Z) -> Y ^ Z  */
1093 (simplify
1094  (bit_xor (convert1? (bit_xor:c @0 @1)) (convert2? (bit_xor:c @0 @2)))
1095  (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1096       && tree_nop_conversion_p (type, TREE_TYPE (@2)))
1097   (bit_xor (convert @1) (convert @2))))
1099 (simplify
1100  (abs (abs@1 @0))
1101  @1)
1102 (simplify
1103  (abs (negate @0))
1104  (abs @0))
1105 (simplify
1106  (abs tree_expr_nonnegative_p@0)
1107  @0)
1109 /* A few cases of fold-const.c negate_expr_p predicate.  */
1110 (match negate_expr_p
1111  INTEGER_CST
1112  (if ((INTEGRAL_TYPE_P (type)
1113        && TYPE_UNSIGNED (type))
1114       || (!TYPE_OVERFLOW_SANITIZED (type)
1115           && may_negate_without_overflow_p (t)))))
1116 (match negate_expr_p
1117  FIXED_CST)
1118 (match negate_expr_p
1119  (negate @0)
1120  (if (!TYPE_OVERFLOW_SANITIZED (type))))
1121 (match negate_expr_p
1122  REAL_CST
1123  (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (t)))))
1124 /* VECTOR_CST handling of non-wrapping types would recurse in unsupported
1125    ways.  */
1126 (match negate_expr_p
1127  VECTOR_CST
1128  (if (FLOAT_TYPE_P (TREE_TYPE (type)) || TYPE_OVERFLOW_WRAPS (type))))
1129 (match negate_expr_p
1130  (minus @0 @1)
1131  (if ((ANY_INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_WRAPS (type))
1132       || (FLOAT_TYPE_P (type)
1133           && !HONOR_SIGN_DEPENDENT_ROUNDING (type)
1134           && !HONOR_SIGNED_ZEROS (type)))))
1136 /* (-A) * (-B) -> A * B  */
1137 (simplify
1138  (mult:c (convert1? (negate @0)) (convert2? negate_expr_p@1))
1139   (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
1140        && tree_nop_conversion_p (type, TREE_TYPE (@1)))
1141    (mult (convert @0) (convert (negate @1)))))
1143 /* -(A + B) -> (-B) - A.  */
1144 (simplify
1145  (negate (plus:c @0 negate_expr_p@1))
1146  (if (!HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type))
1147       && !HONOR_SIGNED_ZEROS (element_mode (type)))
1148   (minus (negate @1) @0)))
1150 /* -(A - B) -> B - A.  */
1151 (simplify
1152  (negate (minus @0 @1))
1153  (if ((ANY_INTEGRAL_TYPE_P (type) && !TYPE_OVERFLOW_SANITIZED (type))
1154       || (FLOAT_TYPE_P (type)
1155           && !HONOR_SIGN_DEPENDENT_ROUNDING (type)
1156           && !HONOR_SIGNED_ZEROS (type)))
1157   (minus @1 @0)))
1158 (simplify
1159  (negate (pointer_diff @0 @1))
1160  (if (TYPE_OVERFLOW_UNDEFINED (type))
1161   (pointer_diff @1 @0)))
1163 /* A - B -> A + (-B) if B is easily negatable.  */
1164 (simplify
1165  (minus @0 negate_expr_p@1)
1166  (if (!FIXED_POINT_TYPE_P (type))
1167  (plus @0 (negate @1))))
1169 /* Try to fold (type) X op CST -> (type) (X op ((type-x) CST))
1170    when profitable.
1171    For bitwise binary operations apply operand conversions to the
1172    binary operation result instead of to the operands.  This allows
1173    to combine successive conversions and bitwise binary operations.
1174    We combine the above two cases by using a conditional convert.  */
1175 (for bitop (bit_and bit_ior bit_xor)
1176  (simplify
1177   (bitop (convert @0) (convert? @1))
1178   (if (((TREE_CODE (@1) == INTEGER_CST
1179          && INTEGRAL_TYPE_P (TREE_TYPE (@0))
1180          && int_fits_type_p (@1, TREE_TYPE (@0)))
1181         || types_match (@0, @1))
1182        /* ???  This transform conflicts with fold-const.c doing
1183           Convert (T)(x & c) into (T)x & (T)c, if c is an integer
1184           constants (if x has signed type, the sign bit cannot be set
1185           in c).  This folds extension into the BIT_AND_EXPR.
1186           Restrict it to GIMPLE to avoid endless recursions.  */
1187        && (bitop != BIT_AND_EXPR || GIMPLE)
1188        && (/* That's a good idea if the conversion widens the operand, thus
1189               after hoisting the conversion the operation will be narrower.  */
1190            TYPE_PRECISION (TREE_TYPE (@0)) < TYPE_PRECISION (type)
1191            /* It's also a good idea if the conversion is to a non-integer
1192               mode.  */
1193            || GET_MODE_CLASS (TYPE_MODE (type)) != MODE_INT
1194            /* Or if the precision of TO is not the same as the precision
1195               of its mode.  */
1196            || !type_has_mode_precision_p (type)))
1197    (convert (bitop @0 (convert @1))))))
1199 (for bitop (bit_and bit_ior)
1200      rbitop (bit_ior bit_and)
1201   /* (x | y) & x -> x */
1202   /* (x & y) | x -> x */
1203  (simplify
1204   (bitop:c (rbitop:c @0 @1) @0)
1205   @0)
1206  /* (~x | y) & x -> x & y */
1207  /* (~x & y) | x -> x | y */
1208  (simplify
1209   (bitop:c (rbitop:c (bit_not @0) @1) @0)
1210   (bitop @0 @1)))
1212 /* (x | CST1) & CST2 -> (x & CST2) | (CST1 & CST2) */
1213 (simplify
1214   (bit_and (bit_ior @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
1215   (bit_ior (bit_and @0 @2) (bit_and @1 @2)))
1217 /* Combine successive equal operations with constants.  */
1218 (for bitop (bit_and bit_ior bit_xor)
1219  (simplify
1220   (bitop (bitop @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
1221   (if (!CONSTANT_CLASS_P (@0))
1222    /* This is the canonical form regardless of whether (bitop @1 @2) can be
1223       folded to a constant.  */
1224    (bitop @0 (bitop @1 @2))
1225    /* In this case we have three constants and (bitop @0 @1) doesn't fold
1226       to a constant.  This can happen if @0 or @1 is a POLY_INT_CST and if
1227       the values involved are such that the operation can't be decided at
1228       compile time.  Try folding one of @0 or @1 with @2 to see whether
1229       that combination can be decided at compile time.
1231       Keep the existing form if both folds fail, to avoid endless
1232       oscillation.  */
1233    (with { tree cst1 = const_binop (bitop, type, @0, @2); }
1234     (if (cst1)
1235      (bitop @1 { cst1; })
1236      (with { tree cst2 = const_binop (bitop, type, @1, @2); }
1237       (if (cst2)
1238        (bitop @0 { cst2; }))))))))
1240 /* Try simple folding for X op !X, and X op X with the help
1241    of the truth_valued_p and logical_inverted_value predicates.  */
1242 (match truth_valued_p
1243  @0
1244  (if (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1)))
1245 (for op (tcc_comparison truth_and truth_andif truth_or truth_orif truth_xor)
1246  (match truth_valued_p
1247   (op @0 @1)))
1248 (match truth_valued_p
1249   (truth_not @0))
1251 (match (logical_inverted_value @0)
1252  (truth_not @0))
1253 (match (logical_inverted_value @0)
1254  (bit_not truth_valued_p@0))
1255 (match (logical_inverted_value @0)
1256  (eq @0 integer_zerop))
1257 (match (logical_inverted_value @0)
1258  (ne truth_valued_p@0 integer_truep))
1259 (match (logical_inverted_value @0)
1260  (bit_xor truth_valued_p@0 integer_truep))
1262 /* X & !X -> 0.  */
1263 (simplify
1264  (bit_and:c @0 (logical_inverted_value @0))
1265  { build_zero_cst (type); })
1266 /* X | !X and X ^ !X -> 1, , if X is truth-valued.  */
1267 (for op (bit_ior bit_xor)
1268  (simplify
1269   (op:c truth_valued_p@0 (logical_inverted_value @0))
1270   { constant_boolean_node (true, type); }))
1271 /* X ==/!= !X is false/true.  */
1272 (for op (eq ne)
1273  (simplify
1274   (op:c truth_valued_p@0 (logical_inverted_value @0))
1275   { constant_boolean_node (op == NE_EXPR ? true : false, type); }))
1277 /* ~~x -> x */
1278 (simplify
1279   (bit_not (bit_not @0))
1280   @0)
1282 /* Convert ~ (-A) to A - 1.  */
1283 (simplify
1284  (bit_not (convert? (negate @0)))
1285  (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1286       || !TYPE_UNSIGNED (TREE_TYPE (@0)))
1287   (convert (minus @0 { build_each_one_cst (TREE_TYPE (@0)); }))))
1289 /* Convert - (~A) to A + 1.  */
1290 (simplify
1291  (negate (nop_convert (bit_not @0)))
1292  (plus (view_convert @0) { build_each_one_cst (type); }))
1294 /* Convert ~ (A - 1) or ~ (A + -1) to -A.  */
1295 (simplify
1296  (bit_not (convert? (minus @0 integer_each_onep)))
1297  (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1298       || !TYPE_UNSIGNED (TREE_TYPE (@0)))
1299   (convert (negate @0))))
1300 (simplify
1301  (bit_not (convert? (plus @0 integer_all_onesp)))
1302  (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1303       || !TYPE_UNSIGNED (TREE_TYPE (@0)))
1304   (convert (negate @0))))
1306 /* Part of convert ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify.  */
1307 (simplify
1308  (bit_not (convert? (bit_xor @0 INTEGER_CST@1)))
1309  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1310   (convert (bit_xor @0 (bit_not @1)))))
1311 (simplify
1312  (bit_not (convert? (bit_xor:c (bit_not @0) @1)))
1313  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1314   (convert (bit_xor @0 @1))))
1316 /* Otherwise prefer ~(X ^ Y) to ~X ^ Y as more canonical.  */
1317 (simplify
1318  (bit_xor:c (nop_convert:s (bit_not:s @0)) @1)
1319  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1320   (bit_not (bit_xor (view_convert @0) @1))))
1322 /* (x & ~m) | (y & m) -> ((x ^ y) & m) ^ x */
1323 (simplify
1324  (bit_ior:c (bit_and:cs @0 (bit_not @2)) (bit_and:cs @1 @2))
1325  (bit_xor (bit_and (bit_xor @0 @1) @2) @0))
1327 /* Fold A - (A & B) into ~B & A.  */
1328 (simplify
1329  (minus (convert1? @0) (convert2?:s (bit_and:cs @@0 @1)))
1330  (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
1331       && tree_nop_conversion_p (type, TREE_TYPE (@1)))
1332   (convert (bit_and (bit_not @1) @0))))
1334 /* (m1 CMP m2) * d -> (m1 CMP m2) ? d : 0  */
1335 (for cmp (gt lt ge le)
1336 (simplify
1337  (mult (convert (cmp @0 @1)) @2)
1338   (cond (cmp @0 @1) @2 { build_zero_cst (type); })))
1340 /* For integral types with undefined overflow and C != 0 fold
1341    x * C EQ/NE y * C into x EQ/NE y.  */
1342 (for cmp (eq ne)
1343  (simplify
1344   (cmp (mult:c @0 @1) (mult:c @2 @1))
1345   (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
1346        && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1347        && tree_expr_nonzero_p (@1))
1348    (cmp @0 @2))))
1350 /* For integral types with wrapping overflow and C odd fold
1351    x * C EQ/NE y * C into x EQ/NE y.  */
1352 (for cmp (eq ne)
1353  (simplify
1354   (cmp (mult @0 INTEGER_CST@1) (mult @2 @1))
1355   (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
1356        && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))
1357        && (TREE_INT_CST_LOW (@1) & 1) != 0)
1358    (cmp @0 @2))))
1360 /* For integral types with undefined overflow and C != 0 fold
1361    x * C RELOP y * C into:
1363    x RELOP y for nonnegative C
1364    y RELOP x for negative C  */
1365 (for cmp (lt gt le ge)
1366  (simplify
1367   (cmp (mult:c @0 @1) (mult:c @2 @1))
1368   (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
1369        && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1370    (if (tree_expr_nonnegative_p (@1) && tree_expr_nonzero_p (@1))
1371     (cmp @0 @2)
1372    (if (TREE_CODE (@1) == INTEGER_CST
1373         && wi::neg_p (wi::to_wide (@1), TYPE_SIGN (TREE_TYPE (@1))))
1374     (cmp @2 @0))))))
1376 /* (X - 1U) <= INT_MAX-1U into (int) X > 0.  */
1377 (for cmp (le gt)
1378      icmp (gt le)
1379  (simplify
1380   (cmp (plus @0 integer_minus_onep@1) INTEGER_CST@2)
1381    (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1382         && TYPE_UNSIGNED (TREE_TYPE (@0))
1383         && TYPE_PRECISION (TREE_TYPE (@0)) > 1
1384         && (wi::to_wide (@2)
1385             == wi::max_value (TYPE_PRECISION (TREE_TYPE (@0)), SIGNED) - 1))
1386     (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
1387      (icmp (convert:stype @0) { build_int_cst (stype, 0); })))))
1389 /* X / 4 < Y / 4 iff X < Y when the division is known to be exact.  */
1390 (for cmp (simple_comparison)
1391  (simplify
1392   (cmp (exact_div @0 INTEGER_CST@2) (exact_div @1 @2))
1393   (if (wi::gt_p (wi::to_wide (@2), 0, TYPE_SIGN (TREE_TYPE (@2))))
1394    (cmp @0 @1))))
1396 /* X / C1 op C2 into a simple range test.  */
1397 (for cmp (simple_comparison)
1398  (simplify
1399   (cmp (trunc_div:s @0 INTEGER_CST@1) INTEGER_CST@2)
1400   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1401        && integer_nonzerop (@1)
1402        && !TREE_OVERFLOW (@1)
1403        && !TREE_OVERFLOW (@2))
1404    (with { tree lo, hi; bool neg_overflow;
1405            enum tree_code code = fold_div_compare (cmp, @1, @2, &lo, &hi,
1406                                                    &neg_overflow); }
1407     (switch
1408      (if (code == LT_EXPR || code == GE_EXPR)
1409        (if (TREE_OVERFLOW (lo))
1410         { build_int_cst (type, (code == LT_EXPR) ^ neg_overflow); }
1411         (if (code == LT_EXPR)
1412          (lt @0 { lo; })
1413          (ge @0 { lo; }))))
1414      (if (code == LE_EXPR || code == GT_EXPR)
1415        (if (TREE_OVERFLOW (hi))
1416         { build_int_cst (type, (code == LE_EXPR) ^ neg_overflow); }
1417         (if (code == LE_EXPR)
1418          (le @0 { hi; })
1419          (gt @0 { hi; }))))
1420      (if (!lo && !hi)
1421       { build_int_cst (type, code == NE_EXPR); })
1422      (if (code == EQ_EXPR && !hi)
1423       (ge @0 { lo; }))
1424      (if (code == EQ_EXPR && !lo)
1425       (le @0 { hi; }))
1426      (if (code == NE_EXPR && !hi)
1427       (lt @0 { lo; }))
1428      (if (code == NE_EXPR && !lo)
1429       (gt @0 { hi; }))
1430      (if (GENERIC)
1431       { build_range_check (UNKNOWN_LOCATION, type, @0, code == EQ_EXPR,
1432                            lo, hi); })
1433      (with
1434       {
1435         tree etype = range_check_type (TREE_TYPE (@0));
1436         if (etype)
1437           {
1438             if (! TYPE_UNSIGNED (etype))
1439               etype = unsigned_type_for (etype);
1440             hi = fold_convert (etype, hi);
1441             lo = fold_convert (etype, lo);
1442             hi = const_binop (MINUS_EXPR, etype, hi, lo);
1443           }
1444       }
1445       (if (etype && hi && !TREE_OVERFLOW (hi))
1446        (if (code == EQ_EXPR)
1447         (le (minus (convert:etype @0) { lo; }) { hi; })
1448         (gt (minus (convert:etype @0) { lo; }) { hi; })))))))))
1450 /* X + Z < Y + Z is the same as X < Y when there is no overflow.  */
1451 (for op (lt le ge gt)
1452  (simplify
1453   (op (plus:c @0 @2) (plus:c @1 @2))
1454   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1455        && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1456    (op @0 @1))))
1457 /* For equality and subtraction, this is also true with wrapping overflow.  */
1458 (for op (eq ne minus)
1459  (simplify
1460   (op (plus:c @0 @2) (plus:c @1 @2))
1461   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1462        && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1463            || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
1464    (op @0 @1))))
1466 /* X - Z < Y - Z is the same as X < Y when there is no overflow.  */
1467 (for op (lt le ge gt)
1468  (simplify
1469   (op (minus @0 @2) (minus @1 @2))
1470   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1471        && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1472    (op @0 @1))))
1473 /* For equality and subtraction, this is also true with wrapping overflow.  */
1474 (for op (eq ne minus)
1475  (simplify
1476   (op (minus @0 @2) (minus @1 @2))
1477   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1478        && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1479            || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
1480    (op @0 @1))))
1481 /* And for pointers...  */
1482 (for op (simple_comparison)
1483  (simplify
1484   (op (pointer_diff@3 @0 @2) (pointer_diff @1 @2))
1485   (if (!TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
1486    (op @0 @1))))
1487 (simplify
1488  (minus (pointer_diff@3 @0 @2) (pointer_diff @1 @2))
1489  (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@3))
1490       && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
1491   (pointer_diff @0 @1)))
1493 /* Z - X < Z - Y is the same as Y < X when there is no overflow.  */
1494 (for op (lt le ge gt)
1495  (simplify
1496   (op (minus @2 @0) (minus @2 @1))
1497   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1498        && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1499    (op @1 @0))))
1500 /* For equality and subtraction, this is also true with wrapping overflow.  */
1501 (for op (eq ne minus)
1502  (simplify
1503   (op (minus @2 @0) (minus @2 @1))
1504   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1505        && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1506            || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
1507    (op @1 @0))))
1508 /* And for pointers...  */
1509 (for op (simple_comparison)
1510  (simplify
1511   (op (pointer_diff@3 @2 @0) (pointer_diff @2 @1))
1512   (if (!TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
1513    (op @1 @0))))
1514 (simplify
1515  (minus (pointer_diff@3 @2 @0) (pointer_diff @2 @1))
1516  (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@3))
1517       && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
1518   (pointer_diff @1 @0)))
1520 /* X + Y < Y is the same as X < 0 when there is no overflow.  */
1521 (for op (lt le gt ge)
1522  (simplify
1523   (op:c (plus:c@2 @0 @1) @1)
1524   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1525        && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1526        && (CONSTANT_CLASS_P (@0) || single_use (@2)))
1527    (op @0 { build_zero_cst (TREE_TYPE (@0)); }))))
1528 /* For equality, this is also true with wrapping overflow.  */
1529 (for op (eq ne)
1530  (simplify
1531   (op:c (nop_convert@3 (plus:c@2 @0 (convert1? @1))) (convert2? @1))
1532   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1533        && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1534            || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
1535        && (CONSTANT_CLASS_P (@0) || (single_use (@2) && single_use (@3)))
1536        && tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@2))
1537        && tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@1)))
1538    (op @0 { build_zero_cst (TREE_TYPE (@0)); })))
1539  (simplify
1540   (op:c (nop_convert@3 (pointer_plus@2 (convert1? @0) @1)) (convert2? @0))
1541   (if (tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@0))
1542        && tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0))
1543        && (CONSTANT_CLASS_P (@1) || (single_use (@2) && single_use (@3))))
1544    (op @1 { build_zero_cst (TREE_TYPE (@1)); }))))
1546 /* X - Y < X is the same as Y > 0 when there is no overflow.
1547    For equality, this is also true with wrapping overflow.  */
1548 (for op (simple_comparison)
1549  (simplify
1550   (op:c @0 (minus@2 @0 @1))
1551   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1552        && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1553            || ((op == EQ_EXPR || op == NE_EXPR)
1554                && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
1555        && (CONSTANT_CLASS_P (@1) || single_use (@2)))
1556    (op @1 { build_zero_cst (TREE_TYPE (@1)); }))))
1558 /* Transform:
1559    (X / Y) == 0 -> X < Y if X, Y are unsigned.
1560    (X / Y) != 0 -> X >= Y, if X, Y are unsigned.  */
1561 (for cmp (eq ne)
1562      ocmp (lt ge)
1563  (simplify
1564   (cmp (trunc_div @0 @1) integer_zerop)
1565   (if (TYPE_UNSIGNED (TREE_TYPE (@0))
1566        /* Complex ==/!= is allowed, but not </>=.  */
1567        && TREE_CODE (TREE_TYPE (@0)) != COMPLEX_TYPE
1568        && (VECTOR_TYPE_P (type) || !VECTOR_TYPE_P (TREE_TYPE (@0))))
1569    (ocmp @0 @1))))
1571 /* X == C - X can never be true if C is odd.  */
1572 (for cmp (eq ne)
1573  (simplify
1574   (cmp:c (convert? @0) (convert1? (minus INTEGER_CST@1 (convert2? @0))))
1575   (if (TREE_INT_CST_LOW (@1) & 1)
1576    { constant_boolean_node (cmp == NE_EXPR, type); })))
1578 /* Arguments on which one can call get_nonzero_bits to get the bits
1579    possibly set.  */
1580 (match with_possible_nonzero_bits
1581  INTEGER_CST@0)
1582 (match with_possible_nonzero_bits
1583  SSA_NAME@0
1584  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))))
1585 /* Slightly extended version, do not make it recursive to keep it cheap.  */
1586 (match (with_possible_nonzero_bits2 @0)
1587  with_possible_nonzero_bits@0)
1588 (match (with_possible_nonzero_bits2 @0)
1589  (bit_and:c with_possible_nonzero_bits@0 @2))
1591 /* Same for bits that are known to be set, but we do not have
1592    an equivalent to get_nonzero_bits yet.  */
1593 (match (with_certain_nonzero_bits2 @0)
1594  INTEGER_CST@0)
1595 (match (with_certain_nonzero_bits2 @0)
1596  (bit_ior @1 INTEGER_CST@0))
1598 /* X == C (or X & Z == Y | C) is impossible if ~nonzero(X) & C != 0.  */
1599 (for cmp (eq ne)
1600  (simplify
1601   (cmp:c (with_possible_nonzero_bits2 @0) (with_certain_nonzero_bits2 @1))
1602   (if (wi::bit_and_not (wi::to_wide (@1), get_nonzero_bits (@0)) != 0)
1603    { constant_boolean_node (cmp == NE_EXPR, type); })))
1605 /* ((X inner_op C0) outer_op C1)
1606    With X being a tree where value_range has reasoned certain bits to always be
1607    zero throughout its computed value range,
1608    inner_op = {|,^}, outer_op = {|,^} and inner_op != outer_op
1609    where zero_mask has 1's for all bits that are sure to be 0 in
1610    and 0's otherwise.
1611    if (inner_op == '^') C0 &= ~C1;
1612    if ((C0 & ~zero_mask) == 0) then emit (X outer_op (C0 outer_op C1)
1613    if ((C1 & ~zero_mask) == 0) then emit (X inner_op (C0 outer_op C1)
1615 (for inner_op (bit_ior bit_xor)
1616      outer_op (bit_xor bit_ior)
1617 (simplify
1618  (outer_op
1619   (inner_op:s @2 INTEGER_CST@0) INTEGER_CST@1)
1620  (with
1621   {
1622     bool fail = false;
1623     wide_int zero_mask_not;
1624     wide_int C0;
1625     wide_int cst_emit;
1627     if (TREE_CODE (@2) == SSA_NAME)
1628       zero_mask_not = get_nonzero_bits (@2);
1629     else
1630       fail = true;
1632     if (inner_op == BIT_XOR_EXPR)
1633       {
1634         C0 = wi::bit_and_not (wi::to_wide (@0), wi::to_wide (@1));
1635         cst_emit = C0 | wi::to_wide (@1);
1636       }
1637     else
1638       {
1639         C0 = wi::to_wide (@0);
1640         cst_emit = C0 ^ wi::to_wide (@1);
1641       }
1642   }
1643   (if (!fail && (C0 & zero_mask_not) == 0)
1644    (outer_op @2 { wide_int_to_tree (type, cst_emit); })
1645    (if (!fail && (wi::to_wide (@1) & zero_mask_not) == 0)
1646     (inner_op @2 { wide_int_to_tree (type, cst_emit); }))))))
1648 /* Associate (p +p off1) +p off2 as (p +p (off1 + off2)).  */
1649 (simplify
1650   (pointer_plus (pointer_plus:s @0 @1) @3)
1651   (pointer_plus @0 (plus @1 @3)))
1653 /* Pattern match
1654      tem1 = (long) ptr1;
1655      tem2 = (long) ptr2;
1656      tem3 = tem2 - tem1;
1657      tem4 = (unsigned long) tem3;
1658      tem5 = ptr1 + tem4;
1659    and produce
1660      tem5 = ptr2;  */
1661 (simplify
1662   (pointer_plus @0 (convert?@2 (minus@3 (convert @1) (convert @0))))
1663   /* Conditionally look through a sign-changing conversion.  */
1664   (if (TYPE_PRECISION (TREE_TYPE (@2)) == TYPE_PRECISION (TREE_TYPE (@3))
1665        && ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@1)))
1666             || (GENERIC && type == TREE_TYPE (@1))))
1667    @1))
1668 (simplify
1669   (pointer_plus @0 (convert?@2 (pointer_diff@3 @1 @@0)))
1670   (if (TYPE_PRECISION (TREE_TYPE (@2)) >= TYPE_PRECISION (TREE_TYPE (@3)))
1671    (convert @1)))
1673 /* Pattern match
1674      tem = (sizetype) ptr;
1675      tem = tem & algn;
1676      tem = -tem;
1677      ... = ptr p+ tem;
1678    and produce the simpler and easier to analyze with respect to alignment
1679      ... = ptr & ~algn;  */
1680 (simplify
1681   (pointer_plus @0 (negate (bit_and (convert @0) INTEGER_CST@1)))
1682   (with { tree algn = wide_int_to_tree (TREE_TYPE (@0), ~wi::to_wide (@1)); }
1683    (bit_and @0 { algn; })))
1685 /* Try folding difference of addresses.  */
1686 (simplify
1687  (minus (convert ADDR_EXPR@0) (convert @1))
1688  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1689   (with { poly_int64 diff; }
1690    (if (ptr_difference_const (@0, @1, &diff))
1691     { build_int_cst_type (type, diff); }))))
1692 (simplify
1693  (minus (convert @0) (convert ADDR_EXPR@1))
1694  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1695   (with { poly_int64 diff; }
1696    (if (ptr_difference_const (@0, @1, &diff))
1697     { build_int_cst_type (type, diff); }))))
1698 (simplify
1699  (pointer_diff (convert?@2 ADDR_EXPR@0) (convert1?@3 @1))
1700  (if (tree_nop_conversion_p (TREE_TYPE(@2), TREE_TYPE (@0))
1701       && tree_nop_conversion_p (TREE_TYPE(@3), TREE_TYPE (@1)))
1702   (with { poly_int64 diff; }
1703    (if (ptr_difference_const (@0, @1, &diff))
1704     { build_int_cst_type (type, diff); }))))
1705 (simplify
1706  (pointer_diff (convert?@2 @0) (convert1?@3 ADDR_EXPR@1))
1707  (if (tree_nop_conversion_p (TREE_TYPE(@2), TREE_TYPE (@0))
1708       && tree_nop_conversion_p (TREE_TYPE(@3), TREE_TYPE (@1)))
1709   (with { poly_int64 diff; }
1710    (if (ptr_difference_const (@0, @1, &diff))
1711     { build_int_cst_type (type, diff); }))))
1713 /* If arg0 is derived from the address of an object or function, we may
1714    be able to fold this expression using the object or function's
1715    alignment.  */
1716 (simplify
1717  (bit_and (convert? @0) INTEGER_CST@1)
1718  (if (POINTER_TYPE_P (TREE_TYPE (@0))
1719       && tree_nop_conversion_p (type, TREE_TYPE (@0)))
1720   (with
1721    {
1722      unsigned int align;
1723      unsigned HOST_WIDE_INT bitpos;
1724      get_pointer_alignment_1 (@0, &align, &bitpos);
1725    }
1726    (if (wi::ltu_p (wi::to_wide (@1), align / BITS_PER_UNIT))
1727     { wide_int_to_tree (type, (wi::to_wide (@1)
1728                                & (bitpos / BITS_PER_UNIT))); }))))
1731 /* We can't reassociate at all for saturating types.  */
1732 (if (!TYPE_SATURATING (type))
1734  /* Contract negates.  */
1735  /* A + (-B) -> A - B */
1736  (simplify
1737   (plus:c @0 (convert? (negate @1)))
1738   /* Apply STRIP_NOPS on the negate.  */
1739   (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1740        && !TYPE_OVERFLOW_SANITIZED (type))
1741    (with
1742     {
1743      tree t1 = type;
1744      if (INTEGRAL_TYPE_P (type)
1745          && TYPE_OVERFLOW_WRAPS (type) != TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
1746        t1 = TYPE_OVERFLOW_WRAPS (type) ? type : TREE_TYPE (@1);
1747     }
1748     (convert (minus (convert:t1 @0) (convert:t1 @1))))))
1749  /* A - (-B) -> A + B */
1750  (simplify
1751   (minus @0 (convert? (negate @1)))
1752   (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1753        && !TYPE_OVERFLOW_SANITIZED (type))
1754    (with
1755     {
1756      tree t1 = type;
1757      if (INTEGRAL_TYPE_P (type)
1758          && TYPE_OVERFLOW_WRAPS (type) != TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
1759        t1 = TYPE_OVERFLOW_WRAPS (type) ? type : TREE_TYPE (@1);
1760     }
1761     (convert (plus (convert:t1 @0) (convert:t1 @1))))))
1762  /* -(T)(-A) -> (T)A
1763     Sign-extension is ok except for INT_MIN, which thankfully cannot
1764     happen without overflow.  */
1765  (simplify
1766   (negate (convert (negate @1)))
1767   (if (INTEGRAL_TYPE_P (type)
1768        && (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@1))
1769            || (!TYPE_UNSIGNED (TREE_TYPE (@1))
1770                && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))))
1771        && !TYPE_OVERFLOW_SANITIZED (type)
1772        && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@1)))
1773    (convert @1)))
1774  (simplify
1775   (negate (convert negate_expr_p@1))
1776   (if (SCALAR_FLOAT_TYPE_P (type)
1777        && ((DECIMAL_FLOAT_TYPE_P (type)
1778             == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1))
1779             && TYPE_PRECISION (type) >= TYPE_PRECISION (TREE_TYPE (@1)))
1780            || !HONOR_SIGN_DEPENDENT_ROUNDING (type)))
1781    (convert (negate @1))))
1782  (simplify
1783   (negate (nop_convert (negate @1)))
1784   (if (!TYPE_OVERFLOW_SANITIZED (type)
1785        && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@1)))
1786    (view_convert @1)))
1788  /* We can't reassociate floating-point unless -fassociative-math
1789     or fixed-point plus or minus because of saturation to +-Inf.  */
1790  (if ((!FLOAT_TYPE_P (type) || flag_associative_math)
1791       && !FIXED_POINT_TYPE_P (type))
1793   /* Match patterns that allow contracting a plus-minus pair
1794      irrespective of overflow issues.  */
1795   /* (A +- B) - A       ->  +- B */
1796   /* (A +- B) -+ B      ->  A */
1797   /* A - (A +- B)       -> -+ B */
1798   /* A +- (B -+ A)      ->  +- B */
1799   (simplify
1800     (minus (plus:c @0 @1) @0)
1801     @1)
1802   (simplify
1803     (minus (minus @0 @1) @0)
1804     (negate @1))
1805   (simplify
1806     (plus:c (minus @0 @1) @1)
1807     @0)
1808   (simplify
1809    (minus @0 (plus:c @0 @1))
1810    (negate @1))
1811   (simplify
1812    (minus @0 (minus @0 @1))
1813    @1)
1814   /* (A +- B) + (C - A)   -> C +- B */
1815   /* (A +  B) - (A - C)   -> B + C */
1816   /* More cases are handled with comparisons.  */
1817   (simplify
1818    (plus:c (plus:c @0 @1) (minus @2 @0))
1819    (plus @2 @1))
1820   (simplify
1821    (plus:c (minus @0 @1) (minus @2 @0))
1822    (minus @2 @1))
1823   (simplify
1824    (plus:c (pointer_diff @0 @1) (pointer_diff @2 @0))
1825    (if (TYPE_OVERFLOW_UNDEFINED (type)
1826         && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0)))
1827     (pointer_diff @2 @1)))
1828   (simplify
1829    (minus (plus:c @0 @1) (minus @0 @2))
1830    (plus @1 @2))
1832   /* (A +- CST1) +- CST2 -> A + CST3
1833      Use view_convert because it is safe for vectors and equivalent for
1834      scalars.  */
1835   (for outer_op (plus minus)
1836    (for inner_op (plus minus)
1837         neg_inner_op (minus plus)
1838     (simplify
1839      (outer_op (nop_convert (inner_op @0 CONSTANT_CLASS_P@1))
1840                CONSTANT_CLASS_P@2)
1841      /* If one of the types wraps, use that one.  */
1842      (if (!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_WRAPS (type))
1843       /* If all 3 captures are CONSTANT_CLASS_P, punt, as we might recurse
1844          forever if something doesn't simplify into a constant.  */
1845       (if (!CONSTANT_CLASS_P (@0))
1846        (if (outer_op == PLUS_EXPR)
1847         (plus (view_convert @0) (inner_op @2 (view_convert @1)))
1848         (minus (view_convert @0) (neg_inner_op @2 (view_convert @1)))))
1849       (if (!ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1850            || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
1851        (if (outer_op == PLUS_EXPR)
1852         (view_convert (plus @0 (inner_op (view_convert @2) @1)))
1853         (view_convert (minus @0 (neg_inner_op (view_convert @2) @1))))
1854        /* If the constant operation overflows we cannot do the transform
1855           directly as we would introduce undefined overflow, for example
1856           with (a - 1) + INT_MIN.  */
1857        (if (types_match (type, @0))
1858         (with { tree cst = const_binop (outer_op == inner_op
1859                                         ? PLUS_EXPR : MINUS_EXPR,
1860                                         type, @1, @2); }
1861          (if (cst && !TREE_OVERFLOW (cst))
1862           (inner_op @0 { cst; } )
1863           /* X+INT_MAX+1 is X-INT_MIN.  */
1864           (if (INTEGRAL_TYPE_P (type) && cst
1865                && wi::to_wide (cst) == wi::min_value (type))
1866            (neg_inner_op @0 { wide_int_to_tree (type, wi::to_wide (cst)); })
1867            /* Last resort, use some unsigned type.  */
1868            (with { tree utype = unsigned_type_for (type); }
1869             (if (utype)
1870              (view_convert (inner_op
1871                             (view_convert:utype @0)
1872                             (view_convert:utype
1873                              { drop_tree_overflow (cst); }))))))))))))))
1875   /* (CST1 - A) +- CST2 -> CST3 - A  */
1876   (for outer_op (plus minus)
1877    (simplify
1878     (outer_op (minus CONSTANT_CLASS_P@1 @0) CONSTANT_CLASS_P@2)
1879     (with { tree cst = const_binop (outer_op, type, @1, @2); }
1880      (if (cst && !TREE_OVERFLOW (cst))
1881       (minus { cst; } @0)))))
1883   /* CST1 - (CST2 - A) -> CST3 + A  */
1884   (simplify
1885    (minus CONSTANT_CLASS_P@1 (minus CONSTANT_CLASS_P@2 @0))
1886    (with { tree cst = const_binop (MINUS_EXPR, type, @1, @2); }
1887     (if (cst && !TREE_OVERFLOW (cst))
1888      (plus { cst; } @0))))
1890   /* ~A + A -> -1 */
1891   (simplify
1892    (plus:c (bit_not @0) @0)
1893    (if (!TYPE_OVERFLOW_TRAPS (type))
1894     { build_all_ones_cst (type); }))
1896   /* ~A + 1 -> -A */
1897   (simplify
1898    (plus (convert? (bit_not @0)) integer_each_onep)
1899    (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1900     (negate (convert @0))))
1902   /* -A - 1 -> ~A */
1903   (simplify
1904    (minus (convert? (negate @0)) integer_each_onep)
1905    (if (!TYPE_OVERFLOW_TRAPS (type)
1906         && tree_nop_conversion_p (type, TREE_TYPE (@0)))
1907     (bit_not (convert @0))))
1909   /* -1 - A -> ~A */
1910   (simplify
1911    (minus integer_all_onesp @0)
1912    (bit_not @0))
1914   /* (T)(P + A) - (T)P -> (T) A */
1915   (simplify
1916    (minus (convert (plus:c @@0 @1))
1917     (convert? @0))
1918    (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
1919         /* For integer types, if A has a smaller type
1920            than T the result depends on the possible
1921            overflow in P + A.
1922            E.g. T=size_t, A=(unsigned)429497295, P>0.
1923            However, if an overflow in P + A would cause
1924            undefined behavior, we can assume that there
1925            is no overflow.  */
1926         || (INTEGRAL_TYPE_P (TREE_TYPE (@1))
1927             && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))))
1928     (convert @1)))
1929   (simplify
1930    (minus (convert (pointer_plus @@0 @1))
1931     (convert @0))
1932    (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
1933         /* For pointer types, if the conversion of A to the
1934            final type requires a sign- or zero-extension,
1935            then we have to punt - it is not defined which
1936            one is correct.  */
1937         || (POINTER_TYPE_P (TREE_TYPE (@0))
1938             && TREE_CODE (@1) == INTEGER_CST
1939             && tree_int_cst_sign_bit (@1) == 0))
1940     (convert @1)))
1941    (simplify
1942     (pointer_diff (pointer_plus @@0 @1) @0)
1943     /* The second argument of pointer_plus must be interpreted as signed, and
1944        thus sign-extended if necessary.  */
1945     (with { tree stype = signed_type_for (TREE_TYPE (@1)); }
1946      /* Use view_convert instead of convert here, as POINTER_PLUS_EXPR
1947         second arg is unsigned even when we need to consider it as signed,
1948         we don't want to diagnose overflow here.  */
1949      (convert (view_convert:stype @1))))
1951   /* (T)P - (T)(P + A) -> -(T) A */
1952   (simplify
1953    (minus (convert? @0)
1954     (convert (plus:c @@0 @1)))
1955    (if (INTEGRAL_TYPE_P (type)
1956         && TYPE_OVERFLOW_UNDEFINED (type)
1957         && element_precision (type) <= element_precision (TREE_TYPE (@1)))
1958     (with { tree utype = unsigned_type_for (type); }
1959      (convert (negate (convert:utype @1))))
1960     (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
1961          /* For integer types, if A has a smaller type
1962             than T the result depends on the possible
1963             overflow in P + A.
1964             E.g. T=size_t, A=(unsigned)429497295, P>0.
1965             However, if an overflow in P + A would cause
1966             undefined behavior, we can assume that there
1967             is no overflow.  */
1968          || (INTEGRAL_TYPE_P (TREE_TYPE (@1))
1969              && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))))
1970      (negate (convert @1)))))
1971   (simplify
1972    (minus (convert @0)
1973     (convert (pointer_plus @@0 @1)))
1974    (if (INTEGRAL_TYPE_P (type)
1975         && TYPE_OVERFLOW_UNDEFINED (type)
1976         && element_precision (type) <= element_precision (TREE_TYPE (@1)))
1977     (with { tree utype = unsigned_type_for (type); }
1978      (convert (negate (convert:utype @1))))
1979     (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
1980          /* For pointer types, if the conversion of A to the
1981             final type requires a sign- or zero-extension,
1982             then we have to punt - it is not defined which
1983             one is correct.  */
1984          || (POINTER_TYPE_P (TREE_TYPE (@0))
1985              && TREE_CODE (@1) == INTEGER_CST
1986              && tree_int_cst_sign_bit (@1) == 0))
1987      (negate (convert @1)))))
1988    (simplify
1989     (pointer_diff @0 (pointer_plus @@0 @1))
1990     /* The second argument of pointer_plus must be interpreted as signed, and
1991        thus sign-extended if necessary.  */
1992     (with { tree stype = signed_type_for (TREE_TYPE (@1)); }
1993      /* Use view_convert instead of convert here, as POINTER_PLUS_EXPR
1994         second arg is unsigned even when we need to consider it as signed,
1995         we don't want to diagnose overflow here.  */
1996      (negate (convert (view_convert:stype @1)))))
1998   /* (T)(P + A) - (T)(P + B) -> (T)A - (T)B */
1999   (simplify
2000    (minus (convert (plus:c @@0 @1))
2001     (convert (plus:c @0 @2)))
2002    (if (INTEGRAL_TYPE_P (type)
2003         && TYPE_OVERFLOW_UNDEFINED (type)
2004         && element_precision (type) <= element_precision (TREE_TYPE (@1))
2005         && element_precision (type) <= element_precision (TREE_TYPE (@2)))
2006     (with { tree utype = unsigned_type_for (type); }
2007      (convert (minus (convert:utype @1) (convert:utype @2))))
2008     (if (((element_precision (type) <= element_precision (TREE_TYPE (@1)))
2009           == (element_precision (type) <= element_precision (TREE_TYPE (@2))))
2010          && (element_precision (type) <= element_precision (TREE_TYPE (@1))
2011              /* For integer types, if A has a smaller type
2012                 than T the result depends on the possible
2013                 overflow in P + A.
2014                 E.g. T=size_t, A=(unsigned)429497295, P>0.
2015                 However, if an overflow in P + A would cause
2016                 undefined behavior, we can assume that there
2017                 is no overflow.  */
2018              || (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2019                  && INTEGRAL_TYPE_P (TREE_TYPE (@2))
2020                  && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))
2021                  && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@2)))))
2022      (minus (convert @1) (convert @2)))))
2023   (simplify
2024    (minus (convert (pointer_plus @@0 @1))
2025     (convert (pointer_plus @0 @2)))
2026    (if (INTEGRAL_TYPE_P (type)
2027         && TYPE_OVERFLOW_UNDEFINED (type)
2028         && element_precision (type) <= element_precision (TREE_TYPE (@1)))
2029     (with { tree utype = unsigned_type_for (type); }
2030      (convert (minus (convert:utype @1) (convert:utype @2))))
2031     (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
2032          /* For pointer types, if the conversion of A to the
2033             final type requires a sign- or zero-extension,
2034             then we have to punt - it is not defined which
2035             one is correct.  */
2036          || (POINTER_TYPE_P (TREE_TYPE (@0))
2037              && TREE_CODE (@1) == INTEGER_CST
2038              && tree_int_cst_sign_bit (@1) == 0
2039              && TREE_CODE (@2) == INTEGER_CST
2040              && tree_int_cst_sign_bit (@2) == 0))
2041      (minus (convert @1) (convert @2)))))
2042    (simplify
2043     (pointer_diff (pointer_plus @@0 @1) (pointer_plus @0 @2))
2044     /* The second argument of pointer_plus must be interpreted as signed, and
2045        thus sign-extended if necessary.  */
2046     (with { tree stype = signed_type_for (TREE_TYPE (@1)); }
2047      /* Use view_convert instead of convert here, as POINTER_PLUS_EXPR
2048         second arg is unsigned even when we need to consider it as signed,
2049         we don't want to diagnose overflow here.  */
2050      (minus (convert (view_convert:stype @1))
2051             (convert (view_convert:stype @2)))))))
2053 /* (A * C) +- (B * C) -> (A+-B) * C and (A * C) +- A -> A * (C+-1).
2054     Modeled after fold_plusminus_mult_expr.  */
2055 (if (!TYPE_SATURATING (type)
2056      && (!FLOAT_TYPE_P (type) || flag_associative_math))
2057  (for plusminus (plus minus)
2058   (simplify
2059    (plusminus (mult:cs@3 @0 @1) (mult:cs@4 @0 @2))
2060    (if ((!ANY_INTEGRAL_TYPE_P (type)
2061          || TYPE_OVERFLOW_WRAPS (type)
2062          || (INTEGRAL_TYPE_P (type)
2063              && tree_expr_nonzero_p (@0)
2064              && expr_not_equal_to (@0, wi::minus_one (TYPE_PRECISION (type)))))
2065         /* If @1 +- @2 is constant require a hard single-use on either
2066            original operand (but not on both).  */
2067         && (single_use (@3) || single_use (@4)))
2068     (mult (plusminus @1 @2) @0)))
2069   /* We cannot generate constant 1 for fract.  */
2070   (if (!ALL_FRACT_MODE_P (TYPE_MODE (type)))
2071    (simplify
2072     (plusminus @0 (mult:c@3 @0 @2))
2073     (if ((!ANY_INTEGRAL_TYPE_P (type)
2074           || TYPE_OVERFLOW_WRAPS (type)
2075           || (INTEGRAL_TYPE_P (type)
2076               && tree_expr_nonzero_p (@0)
2077               && expr_not_equal_to (@0, wi::minus_one (TYPE_PRECISION (type)))))
2078          && single_use (@3))
2079      (mult (plusminus { build_one_cst (type); } @2) @0)))
2080    (simplify
2081     (plusminus (mult:c@3 @0 @2) @0)
2082     (if ((!ANY_INTEGRAL_TYPE_P (type)
2083           || TYPE_OVERFLOW_WRAPS (type)
2084           || (INTEGRAL_TYPE_P (type)
2085               && tree_expr_nonzero_p (@0)
2086               && expr_not_equal_to (@0, wi::minus_one (TYPE_PRECISION (type)))))
2087          && single_use (@3))
2088      (mult (plusminus @2 { build_one_cst (type); }) @0))))))
2090 /* Simplifications of MIN_EXPR, MAX_EXPR, fmin() and fmax().  */
2092 (for minmax (min max FMIN_ALL FMAX_ALL)
2093  (simplify
2094   (minmax @0 @0)
2095   @0))
2096 /* min(max(x,y),y) -> y.  */
2097 (simplify
2098  (min:c (max:c @0 @1) @1)
2099  @1)
2100 /* max(min(x,y),y) -> y.  */
2101 (simplify
2102  (max:c (min:c @0 @1) @1)
2103  @1)
2104 /* max(a,-a) -> abs(a).  */
2105 (simplify
2106  (max:c @0 (negate @0))
2107  (if (TREE_CODE (type) != COMPLEX_TYPE
2108       && (! ANY_INTEGRAL_TYPE_P (type)
2109           || TYPE_OVERFLOW_UNDEFINED (type)))
2110   (abs @0)))
2111 /* min(a,-a) -> -abs(a).  */
2112 (simplify
2113  (min:c @0 (negate @0))
2114  (if (TREE_CODE (type) != COMPLEX_TYPE
2115       && (! ANY_INTEGRAL_TYPE_P (type)
2116           || TYPE_OVERFLOW_UNDEFINED (type)))
2117   (negate (abs @0))))
2118 (simplify
2119  (min @0 @1)
2120  (switch
2121   (if (INTEGRAL_TYPE_P (type)
2122        && TYPE_MIN_VALUE (type)
2123        && operand_equal_p (@1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
2124    @1)
2125   (if (INTEGRAL_TYPE_P (type)
2126        && TYPE_MAX_VALUE (type)
2127        && operand_equal_p (@1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
2128    @0)))
2129 (simplify
2130  (max @0 @1)
2131  (switch
2132   (if (INTEGRAL_TYPE_P (type)
2133        && TYPE_MAX_VALUE (type)
2134        && operand_equal_p (@1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
2135    @1)
2136   (if (INTEGRAL_TYPE_P (type)
2137        && TYPE_MIN_VALUE (type)
2138        && operand_equal_p (@1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
2139    @0)))
2141 /* max (a, a + CST) -> a + CST where CST is positive.  */
2142 /* max (a, a + CST) -> a where CST is negative.  */
2143 (simplify
2144  (max:c @0 (plus@2 @0 INTEGER_CST@1))
2145   (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
2146    (if (tree_int_cst_sgn (@1) > 0)
2147     @2
2148     @0)))
2150 /* min (a, a + CST) -> a where CST is positive.  */
2151 /* min (a, a + CST) -> a + CST where CST is negative. */
2152 (simplify
2153  (min:c @0 (plus@2 @0 INTEGER_CST@1))
2154   (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
2155    (if (tree_int_cst_sgn (@1) > 0)
2156     @0
2157     @2)))
2159 /* (convert (minmax ((convert (x) c)))) -> minmax (x c) if x is promoted
2160    and the outer convert demotes the expression back to x's type.  */
2161 (for minmax (min max)
2162  (simplify
2163   (convert (minmax@0 (convert @1) INTEGER_CST@2))
2164   (if (INTEGRAL_TYPE_P (type)
2165        && types_match (@1, type) && int_fits_type_p (@2, type)
2166        && TYPE_SIGN (TREE_TYPE (@0)) == TYPE_SIGN (type)
2167        && TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type))
2168    (minmax @1 (convert @2)))))
2170 (for minmax (FMIN_ALL FMAX_ALL)
2171  /* If either argument is NaN, return the other one.  Avoid the
2172     transformation if we get (and honor) a signalling NaN.  */
2173  (simplify
2174   (minmax:c @0 REAL_CST@1)
2175   (if (real_isnan (TREE_REAL_CST_PTR (@1))
2176        && (!HONOR_SNANS (@1) || !TREE_REAL_CST (@1).signalling))
2177    @0)))
2178 /* Convert fmin/fmax to MIN_EXPR/MAX_EXPR.  C99 requires these
2179    functions to return the numeric arg if the other one is NaN.
2180    MIN and MAX don't honor that, so only transform if -ffinite-math-only
2181    is set.  C99 doesn't require -0.0 to be handled, so we don't have to
2182    worry about it either.  */
2183 (if (flag_finite_math_only)
2184  (simplify
2185   (FMIN_ALL @0 @1)
2186   (min @0 @1))
2187  (simplify
2188   (FMAX_ALL @0 @1)
2189   (max @0 @1)))
2190 /* min (-A, -B) -> -max (A, B)  */
2191 (for minmax (min max FMIN_ALL FMAX_ALL)
2192      maxmin (max min FMAX_ALL FMIN_ALL)
2193  (simplify
2194   (minmax (negate:s@2 @0) (negate:s@3 @1))
2195   (if (FLOAT_TYPE_P (TREE_TYPE (@0))
2196        || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2197            && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
2198    (negate (maxmin @0 @1)))))
2199 /* MIN (~X, ~Y) -> ~MAX (X, Y)
2200    MAX (~X, ~Y) -> ~MIN (X, Y)  */
2201 (for minmax (min max)
2202  maxmin (max min)
2203  (simplify
2204   (minmax (bit_not:s@2 @0) (bit_not:s@3 @1))
2205   (bit_not (maxmin @0 @1))))
2207 /* MIN (X, Y) == X -> X <= Y  */
2208 (for minmax (min min max max)
2209      cmp    (eq  ne  eq  ne )
2210      out    (le  gt  ge  lt )
2211  (simplify
2212   (cmp:c (minmax:c @0 @1) @0)
2213   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0)))
2214    (out @0 @1))))
2215 /* MIN (X, 5) == 0 -> X == 0
2216    MIN (X, 5) == 7 -> false  */
2217 (for cmp (eq ne)
2218  (simplify
2219   (cmp (min @0 INTEGER_CST@1) INTEGER_CST@2)
2220   (if (wi::lt_p (wi::to_wide (@1), wi::to_wide (@2),
2221                  TYPE_SIGN (TREE_TYPE (@0))))
2222    { constant_boolean_node (cmp == NE_EXPR, type); }
2223    (if (wi::gt_p (wi::to_wide (@1), wi::to_wide (@2),
2224                   TYPE_SIGN (TREE_TYPE (@0))))
2225     (cmp @0 @2)))))
2226 (for cmp (eq ne)
2227  (simplify
2228   (cmp (max @0 INTEGER_CST@1) INTEGER_CST@2)
2229   (if (wi::gt_p (wi::to_wide (@1), wi::to_wide (@2),
2230                  TYPE_SIGN (TREE_TYPE (@0))))
2231    { constant_boolean_node (cmp == NE_EXPR, type); }
2232    (if (wi::lt_p (wi::to_wide (@1), wi::to_wide (@2),
2233                   TYPE_SIGN (TREE_TYPE (@0))))
2234     (cmp @0 @2)))))
2235 /* MIN (X, C1) < C2 -> X < C2 || C1 < C2  */
2236 (for minmax (min     min     max     max     min     min     max     max    )
2237      cmp    (lt      le      gt      ge      gt      ge      lt      le     )
2238      comb   (bit_ior bit_ior bit_ior bit_ior bit_and bit_and bit_and bit_and)
2239  (simplify
2240   (cmp (minmax @0 INTEGER_CST@1) INTEGER_CST@2)
2241   (comb (cmp @0 @2) (cmp @1 @2))))
2243 /* Simplifications of shift and rotates.  */
2245 (for rotate (lrotate rrotate)
2246  (simplify
2247   (rotate integer_all_onesp@0 @1)
2248   @0))
2250 /* Optimize -1 >> x for arithmetic right shifts.  */
2251 (simplify
2252  (rshift integer_all_onesp@0 @1)
2253  (if (!TYPE_UNSIGNED (type)
2254       && tree_expr_nonnegative_p (@1))
2255   @0))
2257 /* Optimize (x >> c) << c into x & (-1<<c).  */
2258 (simplify
2259  (lshift (rshift @0 INTEGER_CST@1) @1)
2260  (if (wi::ltu_p (wi::to_wide (@1), element_precision (type)))
2261   (bit_and @0 (lshift { build_minus_one_cst (type); } @1))))
2263 /* Optimize (x << c) >> c into x & ((unsigned)-1 >> c) for unsigned
2264    types.  */
2265 (simplify
2266  (rshift (lshift @0 INTEGER_CST@1) @1)
2267  (if (TYPE_UNSIGNED (type)
2268       && (wi::ltu_p (wi::to_wide (@1), element_precision (type))))
2269   (bit_and @0 (rshift { build_minus_one_cst (type); } @1))))
2271 (for shiftrotate (lrotate rrotate lshift rshift)
2272  (simplify
2273   (shiftrotate @0 integer_zerop)
2274   (non_lvalue @0))
2275  (simplify
2276   (shiftrotate integer_zerop@0 @1)
2277   @0)
2278  /* Prefer vector1 << scalar to vector1 << vector2
2279     if vector2 is uniform.  */
2280  (for vec (VECTOR_CST CONSTRUCTOR)
2281   (simplify
2282    (shiftrotate @0 vec@1)
2283    (with { tree tem = uniform_vector_p (@1); }
2284     (if (tem)
2285      (shiftrotate @0 { tem; }))))))
2287 /* Simplify X << Y where Y's low width bits are 0 to X, as only valid
2288    Y is 0.  Similarly for X >> Y.  */
2289 #if GIMPLE
2290 (for shift (lshift rshift)
2291  (simplify
2292   (shift @0 SSA_NAME@1)
2293    (if (INTEGRAL_TYPE_P (TREE_TYPE (@1)))
2294     (with {
2295       int width = ceil_log2 (element_precision (TREE_TYPE (@0)));
2296       int prec = TYPE_PRECISION (TREE_TYPE (@1));
2297      }
2298      (if ((get_nonzero_bits (@1) & wi::mask (width, false, prec)) == 0)
2299       @0)))))
2300 #endif
2302 /* Rewrite an LROTATE_EXPR by a constant into an
2303    RROTATE_EXPR by a new constant.  */
2304 (simplify
2305  (lrotate @0 INTEGER_CST@1)
2306  (rrotate @0 { const_binop (MINUS_EXPR, TREE_TYPE (@1),
2307                             build_int_cst (TREE_TYPE (@1),
2308                                            element_precision (type)), @1); }))
2310 /* Turn (a OP c1) OP c2 into a OP (c1+c2).  */
2311 (for op (lrotate rrotate rshift lshift)
2312  (simplify
2313   (op (op @0 INTEGER_CST@1) INTEGER_CST@2)
2314   (with { unsigned int prec = element_precision (type); }
2315    (if (wi::ge_p (wi::to_wide (@1), 0, TYPE_SIGN (TREE_TYPE (@1)))
2316         && wi::lt_p (wi::to_wide (@1), prec, TYPE_SIGN (TREE_TYPE (@1)))
2317         && wi::ge_p (wi::to_wide (@2), 0, TYPE_SIGN (TREE_TYPE (@2)))
2318         && wi::lt_p (wi::to_wide (@2), prec, TYPE_SIGN (TREE_TYPE (@2))))
2319     (with { unsigned int low = (tree_to_uhwi (@1)
2320                                 + tree_to_uhwi (@2)); }
2321      /* Deal with a OP (c1 + c2) being undefined but (a OP c1) OP c2
2322         being well defined.  */
2323      (if (low >= prec)
2324       (if (op == LROTATE_EXPR || op == RROTATE_EXPR)
2325        (op @0 { build_int_cst (TREE_TYPE (@1), low % prec); })
2326        (if (TYPE_UNSIGNED (type) || op == LSHIFT_EXPR)
2327         { build_zero_cst (type); }
2328         (op @0 { build_int_cst (TREE_TYPE (@1), prec - 1); })))
2329       (op @0 { build_int_cst (TREE_TYPE (@1), low); })))))))
2332 /* ((1 << A) & 1) != 0 -> A == 0
2333    ((1 << A) & 1) == 0 -> A != 0 */
2334 (for cmp (ne eq)
2335      icmp (eq ne)
2336  (simplify
2337   (cmp (bit_and (lshift integer_onep @0) integer_onep) integer_zerop)
2338   (icmp @0 { build_zero_cst (TREE_TYPE (@0)); })))
2340 /* (CST1 << A) == CST2 -> A == ctz (CST2) - ctz (CST1)
2341    (CST1 << A) != CST2 -> A != ctz (CST2) - ctz (CST1)
2342    if CST2 != 0.  */
2343 (for cmp (ne eq)
2344  (simplify
2345   (cmp (lshift INTEGER_CST@0 @1) INTEGER_CST@2)
2346   (with { int cand = wi::ctz (wi::to_wide (@2)) - wi::ctz (wi::to_wide (@0)); }
2347    (if (cand < 0
2348         || (!integer_zerop (@2)
2349             && wi::lshift (wi::to_wide (@0), cand) != wi::to_wide (@2)))
2350     { constant_boolean_node (cmp == NE_EXPR, type); }
2351     (if (!integer_zerop (@2)
2352          && wi::lshift (wi::to_wide (@0), cand) == wi::to_wide (@2))
2353      (cmp @1 { build_int_cst (TREE_TYPE (@1), cand); }))))))
2355 /* Fold (X << C1) & C2 into (X << C1) & (C2 | ((1 << C1) - 1))
2356         (X >> C1) & C2 into (X >> C1) & (C2 | ~((type) -1 >> C1))
2357    if the new mask might be further optimized.  */
2358 (for shift (lshift rshift)
2359  (simplify
2360   (bit_and (convert?:s@4 (shift:s@5 (convert1?@3 @0) INTEGER_CST@1))
2361            INTEGER_CST@2)
2362    (if (tree_nop_conversion_p (TREE_TYPE (@4), TREE_TYPE (@5))
2363         && TYPE_PRECISION (type) <= HOST_BITS_PER_WIDE_INT
2364         && tree_fits_uhwi_p (@1)
2365         && tree_to_uhwi (@1) > 0
2366         && tree_to_uhwi (@1) < TYPE_PRECISION (type))
2367     (with
2368      {
2369        unsigned int shiftc = tree_to_uhwi (@1);
2370        unsigned HOST_WIDE_INT mask = TREE_INT_CST_LOW (@2);
2371        unsigned HOST_WIDE_INT newmask, zerobits = 0;
2372        tree shift_type = TREE_TYPE (@3);
2373        unsigned int prec;
2375        if (shift == LSHIFT_EXPR)
2376          zerobits = ((HOST_WIDE_INT_1U << shiftc) - 1);
2377        else if (shift == RSHIFT_EXPR
2378                 && type_has_mode_precision_p (shift_type))
2379          {
2380            prec = TYPE_PRECISION (TREE_TYPE (@3));
2381            tree arg00 = @0;
2382            /* See if more bits can be proven as zero because of
2383               zero extension.  */
2384            if (@3 != @0
2385                && TYPE_UNSIGNED (TREE_TYPE (@0)))
2386              {
2387                tree inner_type = TREE_TYPE (@0);
2388                if (type_has_mode_precision_p (inner_type)
2389                    && TYPE_PRECISION (inner_type) < prec)
2390                  {
2391                    prec = TYPE_PRECISION (inner_type);
2392                    /* See if we can shorten the right shift.  */
2393                    if (shiftc < prec)
2394                      shift_type = inner_type;
2395                    /* Otherwise X >> C1 is all zeros, so we'll optimize
2396                       it into (X, 0) later on by making sure zerobits
2397                       is all ones.  */
2398                  }
2399              }
2400            zerobits = HOST_WIDE_INT_M1U;
2401            if (shiftc < prec)
2402              {
2403                zerobits >>= HOST_BITS_PER_WIDE_INT - shiftc;
2404                zerobits <<= prec - shiftc;
2405              }
2406            /* For arithmetic shift if sign bit could be set, zerobits
2407               can contain actually sign bits, so no transformation is
2408               possible, unless MASK masks them all away.  In that
2409               case the shift needs to be converted into logical shift.  */
2410            if (!TYPE_UNSIGNED (TREE_TYPE (@3))
2411                && prec == TYPE_PRECISION (TREE_TYPE (@3)))
2412              {
2413                if ((mask & zerobits) == 0)
2414                  shift_type = unsigned_type_for (TREE_TYPE (@3));
2415                else
2416                  zerobits = 0;
2417              }
2418          }
2419      }
2420      /* ((X << 16) & 0xff00) is (X, 0).  */
2421      (if ((mask & zerobits) == mask)
2422       { build_int_cst (type, 0); }
2423       (with { newmask = mask | zerobits; }
2424        (if (newmask != mask && (newmask & (newmask + 1)) == 0)
2425         (with
2426          {
2427            /* Only do the transformation if NEWMASK is some integer
2428               mode's mask.  */
2429            for (prec = BITS_PER_UNIT;
2430                 prec < HOST_BITS_PER_WIDE_INT; prec <<= 1)
2431              if (newmask == (HOST_WIDE_INT_1U << prec) - 1)
2432                break;
2433          }
2434          (if (prec < HOST_BITS_PER_WIDE_INT
2435               || newmask == HOST_WIDE_INT_M1U)
2436           (with
2437            { tree newmaskt = build_int_cst_type (TREE_TYPE (@2), newmask); }
2438            (if (!tree_int_cst_equal (newmaskt, @2))
2439             (if (shift_type != TREE_TYPE (@3))
2440              (bit_and (convert (shift:shift_type (convert @3) @1)) { newmaskt; })
2441              (bit_and @4 { newmaskt; })))))))))))))
2443 /* Fold (X {&,^,|} C2) << C1 into (X << C1) {&,^,|} (C2 << C1)
2444    (X {&,^,|} C2) >> C1 into (X >> C1) & (C2 >> C1).  */
2445 (for shift (lshift rshift)
2446  (for bit_op (bit_and bit_xor bit_ior)
2447   (simplify
2448    (shift (convert?:s (bit_op:s @0 INTEGER_CST@2)) INTEGER_CST@1)
2449    (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2450     (with { tree mask = int_const_binop (shift, fold_convert (type, @2), @1); }
2451      (bit_op (shift (convert @0) @1) { mask; }))))))
2453 /* ~(~X >> Y) -> X >> Y (for arithmetic shift).  */
2454 (simplify
2455  (bit_not (convert1?:s (rshift:s (convert2?@0 (bit_not @1)) @2)))
2456   (if (!TYPE_UNSIGNED (TREE_TYPE (@0))
2457        && (element_precision (TREE_TYPE (@0))
2458            <= element_precision (TREE_TYPE (@1))
2459            || !TYPE_UNSIGNED (TREE_TYPE (@1))))
2460    (with
2461     { tree shift_type = TREE_TYPE (@0); }
2462      (convert (rshift (convert:shift_type @1) @2)))))
2464 /* ~(~X >>r Y) -> X >>r Y
2465    ~(~X <<r Y) -> X <<r Y */
2466 (for rotate (lrotate rrotate)
2467  (simplify
2468   (bit_not (convert1?:s (rotate:s (convert2?@0 (bit_not @1)) @2)))
2469    (if ((element_precision (TREE_TYPE (@0))
2470          <= element_precision (TREE_TYPE (@1))
2471          || !TYPE_UNSIGNED (TREE_TYPE (@1)))
2472         && (element_precision (type) <= element_precision (TREE_TYPE (@0))
2473             || !TYPE_UNSIGNED (TREE_TYPE (@0))))
2474     (with
2475      { tree rotate_type = TREE_TYPE (@0); }
2476       (convert (rotate (convert:rotate_type @1) @2))))))
2478 /* Simplifications of conversions.  */
2480 /* Basic strip-useless-type-conversions / strip_nops.  */
2481 (for cvt (convert view_convert float fix_trunc)
2482  (simplify
2483   (cvt @0)
2484   (if ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@0)))
2485        || (GENERIC && type == TREE_TYPE (@0)))
2486    @0)))
2488 /* Contract view-conversions.  */
2489 (simplify
2490   (view_convert (view_convert @0))
2491   (view_convert @0))
2493 /* For integral conversions with the same precision or pointer
2494    conversions use a NOP_EXPR instead.  */
2495 (simplify
2496   (view_convert @0)
2497   (if ((INTEGRAL_TYPE_P (type) || POINTER_TYPE_P (type))
2498        && (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
2499        && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (@0)))
2500    (convert @0)))
2502 /* Strip inner integral conversions that do not change precision or size, or
2503    zero-extend while keeping the same size (for bool-to-char).  */
2504 (simplify
2505   (view_convert (convert@0 @1))
2506   (if ((INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
2507        && (INTEGRAL_TYPE_P (TREE_TYPE (@1)) || POINTER_TYPE_P (TREE_TYPE (@1)))
2508        && TYPE_SIZE (TREE_TYPE (@0)) == TYPE_SIZE (TREE_TYPE (@1))
2509        && (TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1))
2510            || (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (TREE_TYPE (@1))
2511                && TYPE_UNSIGNED (TREE_TYPE (@1)))))
2512    (view_convert @1)))
2514 /* Re-association barriers around constants and other re-association
2515    barriers can be removed.  */
2516 (simplify
2517  (paren CONSTANT_CLASS_P@0)
2518  @0)
2519 (simplify
2520  (paren (paren@1 @0))
2521  @1)
2523 /* Handle cases of two conversions in a row.  */
2524 (for ocvt (convert float fix_trunc)
2525  (for icvt (convert float)
2526   (simplify
2527    (ocvt (icvt@1 @0))
2528    (with
2529     {
2530       tree inside_type = TREE_TYPE (@0);
2531       tree inter_type = TREE_TYPE (@1);
2532       int inside_int = INTEGRAL_TYPE_P (inside_type);
2533       int inside_ptr = POINTER_TYPE_P (inside_type);
2534       int inside_float = FLOAT_TYPE_P (inside_type);
2535       int inside_vec = VECTOR_TYPE_P (inside_type);
2536       unsigned int inside_prec = TYPE_PRECISION (inside_type);
2537       int inside_unsignedp = TYPE_UNSIGNED (inside_type);
2538       int inter_int = INTEGRAL_TYPE_P (inter_type);
2539       int inter_ptr = POINTER_TYPE_P (inter_type);
2540       int inter_float = FLOAT_TYPE_P (inter_type);
2541       int inter_vec = VECTOR_TYPE_P (inter_type);
2542       unsigned int inter_prec = TYPE_PRECISION (inter_type);
2543       int inter_unsignedp = TYPE_UNSIGNED (inter_type);
2544       int final_int = INTEGRAL_TYPE_P (type);
2545       int final_ptr = POINTER_TYPE_P (type);
2546       int final_float = FLOAT_TYPE_P (type);
2547       int final_vec = VECTOR_TYPE_P (type);
2548       unsigned int final_prec = TYPE_PRECISION (type);
2549       int final_unsignedp = TYPE_UNSIGNED (type);
2550     }
2551    (switch
2552     /* In addition to the cases of two conversions in a row
2553        handled below, if we are converting something to its own
2554        type via an object of identical or wider precision, neither
2555        conversion is needed.  */
2556     (if (((GIMPLE && useless_type_conversion_p (type, inside_type))
2557           || (GENERIC
2558               && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (inside_type)))
2559          && (((inter_int || inter_ptr) && final_int)
2560              || (inter_float && final_float))
2561          && inter_prec >= final_prec)
2562      (ocvt @0))
2564     /* Likewise, if the intermediate and initial types are either both
2565        float or both integer, we don't need the middle conversion if the
2566        former is wider than the latter and doesn't change the signedness
2567        (for integers).  Avoid this if the final type is a pointer since
2568        then we sometimes need the middle conversion.  */
2569     (if (((inter_int && inside_int) || (inter_float && inside_float))
2570          && (final_int || final_float)
2571          && inter_prec >= inside_prec
2572          && (inter_float || inter_unsignedp == inside_unsignedp))
2573      (ocvt @0))
2575     /* If we have a sign-extension of a zero-extended value, we can
2576        replace that by a single zero-extension.  Likewise if the
2577        final conversion does not change precision we can drop the
2578        intermediate conversion.  */
2579     (if (inside_int && inter_int && final_int
2580          && ((inside_prec < inter_prec && inter_prec < final_prec
2581               && inside_unsignedp && !inter_unsignedp)
2582              || final_prec == inter_prec))
2583      (ocvt @0))
2585     /* Two conversions in a row are not needed unless:
2586         - some conversion is floating-point (overstrict for now), or
2587         - some conversion is a vector (overstrict for now), or
2588         - the intermediate type is narrower than both initial and
2589           final, or
2590         - the intermediate type and innermost type differ in signedness,
2591           and the outermost type is wider than the intermediate, or
2592         - the initial type is a pointer type and the precisions of the
2593           intermediate and final types differ, or
2594         - the final type is a pointer type and the precisions of the
2595           initial and intermediate types differ.  */
2596     (if (! inside_float && ! inter_float && ! final_float
2597          && ! inside_vec && ! inter_vec && ! final_vec
2598          && (inter_prec >= inside_prec || inter_prec >= final_prec)
2599          && ! (inside_int && inter_int
2600                && inter_unsignedp != inside_unsignedp
2601                && inter_prec < final_prec)
2602          && ((inter_unsignedp && inter_prec > inside_prec)
2603              == (final_unsignedp && final_prec > inter_prec))
2604          && ! (inside_ptr && inter_prec != final_prec)
2605          && ! (final_ptr && inside_prec != inter_prec))
2606      (ocvt @0))
2608     /* A truncation to an unsigned type (a zero-extension) should be
2609        canonicalized as bitwise and of a mask.  */
2610     (if (GIMPLE /* PR70366: doing this in GENERIC breaks -Wconversion.  */
2611          && final_int && inter_int && inside_int
2612          && final_prec == inside_prec
2613          && final_prec > inter_prec
2614          && inter_unsignedp)
2615      (convert (bit_and @0 { wide_int_to_tree
2616                               (inside_type,
2617                                wi::mask (inter_prec, false,
2618                                          TYPE_PRECISION (inside_type))); })))
2620     /* If we are converting an integer to a floating-point that can
2621        represent it exactly and back to an integer, we can skip the
2622        floating-point conversion.  */
2623     (if (GIMPLE /* PR66211 */
2624          && inside_int && inter_float && final_int &&
2625          (unsigned) significand_size (TYPE_MODE (inter_type))
2626          >= inside_prec - !inside_unsignedp)
2627      (convert @0)))))))
2629 /* If we have a narrowing conversion to an integral type that is fed by a
2630    BIT_AND_EXPR, we might be able to remove the BIT_AND_EXPR if it merely
2631    masks off bits outside the final type (and nothing else).  */
2632 (simplify
2633   (convert (bit_and @0 INTEGER_CST@1))
2634   (if (INTEGRAL_TYPE_P (type)
2635        && INTEGRAL_TYPE_P (TREE_TYPE (@0))
2636        && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))
2637        && operand_equal_p (@1, build_low_bits_mask (TREE_TYPE (@1),
2638                                                     TYPE_PRECISION (type)), 0))
2639    (convert @0)))
2642 /* (X /[ex] A) * A -> X.  */
2643 (simplify
2644   (mult (convert1? (exact_div @0 @@1)) (convert2? @1))
2645   (convert @0))
2647 /* Canonicalization of binary operations.  */
2649 /* Convert X + -C into X - C.  */
2650 (simplify
2651  (plus @0 REAL_CST@1)
2652  (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
2653   (with { tree tem = const_unop (NEGATE_EXPR, type, @1); }
2654    (if (!TREE_OVERFLOW (tem) || !flag_trapping_math)
2655     (minus @0 { tem; })))))
2657 /* Convert x+x into x*2.  */
2658 (simplify
2659  (plus @0 @0)
2660  (if (SCALAR_FLOAT_TYPE_P (type))
2661   (mult @0 { build_real (type, dconst2); })
2662   (if (INTEGRAL_TYPE_P (type))
2663    (mult @0 { build_int_cst (type, 2); }))))
2665 /* 0 - X  ->  -X.  */
2666 (simplify
2667  (minus integer_zerop @1)
2668  (negate @1))
2669 (simplify
2670  (pointer_diff integer_zerop @1)
2671  (negate (convert @1)))
2673 /* (ARG0 - ARG1) is the same as (-ARG1 + ARG0).  So check whether
2674    ARG0 is zero and X + ARG0 reduces to X, since that would mean
2675    (-ARG1 + ARG0) reduces to -ARG1.  */
2676 (simplify
2677  (minus real_zerop@0 @1)
2678  (if (fold_real_zero_addition_p (type, @0, 0))
2679   (negate @1)))
2681 /* Transform x * -1 into -x.  */
2682 (simplify
2683  (mult @0 integer_minus_onep)
2684  (negate @0))
2686 /* Reassociate (X * CST) * Y to (X * Y) * CST.  This does not introduce
2687    signed overflow for CST != 0 && CST != -1.  */
2688 (simplify
2689  (mult:c (mult:s@3 @0 INTEGER_CST@1) @2)
2690  (if (TREE_CODE (@2) != INTEGER_CST
2691       && single_use (@3)
2692       && !integer_zerop (@1) && !integer_minus_onep (@1))
2693   (mult (mult @0 @2) @1)))
2695 /* True if we can easily extract the real and imaginary parts of a complex
2696    number.  */
2697 (match compositional_complex
2698  (convert? (complex @0 @1)))
2700 /* COMPLEX_EXPR and REALPART/IMAGPART_EXPR cancellations.  */
2701 (simplify
2702  (complex (realpart @0) (imagpart @0))
2703  @0)
2704 (simplify
2705  (realpart (complex @0 @1))
2706  @0)
2707 (simplify
2708  (imagpart (complex @0 @1))
2709  @1)
2711 /* Sometimes we only care about half of a complex expression.  */
2712 (simplify
2713  (realpart (convert?:s (conj:s @0)))
2714  (convert (realpart @0)))
2715 (simplify
2716  (imagpart (convert?:s (conj:s @0)))
2717  (convert (negate (imagpart @0))))
2718 (for part (realpart imagpart)
2719  (for op (plus minus)
2720   (simplify
2721    (part (convert?:s@2 (op:s @0 @1)))
2722    (convert (op (part @0) (part @1))))))
2723 (simplify
2724  (realpart (convert?:s (CEXPI:s @0)))
2725  (convert (COS @0)))
2726 (simplify
2727  (imagpart (convert?:s (CEXPI:s @0)))
2728  (convert (SIN @0)))
2730 /* conj(conj(x)) -> x  */
2731 (simplify
2732  (conj (convert? (conj @0)))
2733  (if (tree_nop_conversion_p (TREE_TYPE (@0), type))
2734   (convert @0)))
2736 /* conj({x,y}) -> {x,-y}  */
2737 (simplify
2738  (conj (convert?:s (complex:s @0 @1)))
2739  (with { tree itype = TREE_TYPE (type); }
2740   (complex (convert:itype @0) (negate (convert:itype @1)))))
2742 /* BSWAP simplifications, transforms checked by gcc.dg/builtin-bswap-8.c.  */
2743 (for bswap (BUILT_IN_BSWAP16 BUILT_IN_BSWAP32 BUILT_IN_BSWAP64)
2744  (simplify
2745   (bswap (bswap @0))
2746   @0)
2747  (simplify
2748   (bswap (bit_not (bswap @0)))
2749   (bit_not @0))
2750  (for bitop (bit_xor bit_ior bit_and)
2751   (simplify
2752    (bswap (bitop:c (bswap @0) @1))
2753    (bitop @0 (bswap @1)))))
2756 /* Combine COND_EXPRs and VEC_COND_EXPRs.  */
2758 /* Simplify constant conditions.
2759    Only optimize constant conditions when the selected branch
2760    has the same type as the COND_EXPR.  This avoids optimizing
2761    away "c ? x : throw", where the throw has a void type.
2762    Note that we cannot throw away the fold-const.c variant nor
2763    this one as we depend on doing this transform before possibly
2764    A ? B : B -> B triggers and the fold-const.c one can optimize
2765    0 ? A : B to B even if A has side-effects.  Something
2766    genmatch cannot handle.  */
2767 (simplify
2768  (cond INTEGER_CST@0 @1 @2)
2769  (if (integer_zerop (@0))
2770   (if (!VOID_TYPE_P (TREE_TYPE (@2)) || VOID_TYPE_P (type))
2771    @2)
2772   (if (!VOID_TYPE_P (TREE_TYPE (@1)) || VOID_TYPE_P (type))
2773    @1)))
2774 (simplify
2775  (vec_cond VECTOR_CST@0 @1 @2)
2776  (if (integer_all_onesp (@0))
2777   @1
2778   (if (integer_zerop (@0))
2779    @2)))
2781 /* Simplification moved from fold_cond_expr_with_comparison.  It may also
2782    be extended.  */
2783 /* This pattern implements two kinds simplification:
2785    Case 1)
2786    (cond (cmp (convert1? x) c1) (convert2? x) c2) -> (minmax (x c)) if:
2787      1) Conversions are type widening from smaller type.
2788      2) Const c1 equals to c2 after canonicalizing comparison.
2789      3) Comparison has tree code LT, LE, GT or GE.
2790    This specific pattern is needed when (cmp (convert x) c) may not
2791    be simplified by comparison patterns because of multiple uses of
2792    x.  It also makes sense here because simplifying across multiple
2793    referred var is always benefitial for complicated cases.
2795    Case 2)
2796    (cond (eq (convert1? x) c1) (convert2? x) c2) -> (cond (eq x c1) c1 c2).  */
2797 (for cmp (lt le gt ge eq)
2798  (simplify
2799   (cond (cmp (convert1? @1) INTEGER_CST@3) (convert2? @1) INTEGER_CST@2)
2800   (with
2801    {
2802      tree from_type = TREE_TYPE (@1);
2803      tree c1_type = TREE_TYPE (@3), c2_type = TREE_TYPE (@2);
2804      enum tree_code code = ERROR_MARK;
2806      if (INTEGRAL_TYPE_P (from_type)
2807          && int_fits_type_p (@2, from_type)
2808          && (types_match (c1_type, from_type)
2809              || (TYPE_PRECISION (c1_type) > TYPE_PRECISION (from_type)
2810                  && (TYPE_UNSIGNED (from_type)
2811                      || TYPE_SIGN (c1_type) == TYPE_SIGN (from_type))))
2812          && (types_match (c2_type, from_type)
2813              || (TYPE_PRECISION (c2_type) > TYPE_PRECISION (from_type)
2814                  && (TYPE_UNSIGNED (from_type)
2815                      || TYPE_SIGN (c2_type) == TYPE_SIGN (from_type)))))
2816        {
2817          if (cmp != EQ_EXPR)
2818            {
2819              if (wi::to_widest (@3) == (wi::to_widest (@2) - 1))
2820                {
2821                  /* X <= Y - 1 equals to X < Y.  */
2822                  if (cmp == LE_EXPR)
2823                    code = LT_EXPR;
2824                  /* X > Y - 1 equals to X >= Y.  */
2825                  if (cmp == GT_EXPR)
2826                    code = GE_EXPR;
2827                }
2828              if (wi::to_widest (@3) == (wi::to_widest (@2) + 1))
2829                {
2830                  /* X < Y + 1 equals to X <= Y.  */
2831                  if (cmp == LT_EXPR)
2832                    code = LE_EXPR;
2833                  /* X >= Y + 1 equals to X > Y.  */
2834                  if (cmp == GE_EXPR)
2835                    code = GT_EXPR;
2836                }
2837              if (code != ERROR_MARK
2838                  || wi::to_widest (@2) == wi::to_widest (@3))
2839                {
2840                  if (cmp == LT_EXPR || cmp == LE_EXPR)
2841                    code = MIN_EXPR;
2842                  if (cmp == GT_EXPR || cmp == GE_EXPR)
2843                    code = MAX_EXPR;
2844                }
2845            }
2846          /* Can do A == C1 ? A : C2  ->  A == C1 ? C1 : C2?  */
2847          else if (int_fits_type_p (@3, from_type))
2848            code = EQ_EXPR;
2849        }
2850    }
2851    (if (code == MAX_EXPR)
2852     (convert (max @1 (convert @2)))
2853     (if (code == MIN_EXPR)
2854      (convert (min @1 (convert @2)))
2855      (if (code == EQ_EXPR)
2856       (convert (cond (eq @1 (convert @3))
2857                      (convert:from_type @3) (convert:from_type @2)))))))))
2859 /* (cond (cmp (convert? x) c1) (op x c2) c3) -> (op (minmax x c1) c2) if:
2861      1) OP is PLUS or MINUS.
2862      2) CMP is LT, LE, GT or GE.
2863      3) C3 == (C1 op C2), and computation doesn't have undefined behavior.
2865    This pattern also handles special cases like:
2867      A) Operand x is a unsigned to signed type conversion and c1 is
2868         integer zero.  In this case,
2869           (signed type)x  < 0  <=>  x  > MAX_VAL(signed type)
2870           (signed type)x >= 0  <=>  x <= MAX_VAL(signed type)
2871      B) Const c1 may not equal to (C3 op' C2).  In this case we also
2872         check equality for (c1+1) and (c1-1) by adjusting comparison
2873         code.
2875    TODO: Though signed type is handled by this pattern, it cannot be
2876    simplified at the moment because C standard requires additional
2877    type promotion.  In order to match&simplify it here, the IR needs
2878    to be cleaned up by other optimizers, i.e, VRP.  */
2879 (for op (plus minus)
2880  (for cmp (lt le gt ge)
2881   (simplify
2882    (cond (cmp (convert? @X) INTEGER_CST@1) (op @X INTEGER_CST@2) INTEGER_CST@3)
2883    (with { tree from_type = TREE_TYPE (@X), to_type = TREE_TYPE (@1); }
2884     (if (types_match (from_type, to_type)
2885          /* Check if it is special case A).  */
2886          || (TYPE_UNSIGNED (from_type)
2887              && !TYPE_UNSIGNED (to_type)
2888              && TYPE_PRECISION (from_type) == TYPE_PRECISION (to_type)
2889              && integer_zerop (@1)
2890              && (cmp == LT_EXPR || cmp == GE_EXPR)))
2891      (with
2892       {
2893         wi::overflow_type overflow = wi::OVF_NONE;
2894         enum tree_code code, cmp_code = cmp;
2895         wide_int real_c1;
2896         wide_int c1 = wi::to_wide (@1);
2897         wide_int c2 = wi::to_wide (@2);
2898         wide_int c3 = wi::to_wide (@3);
2899         signop sgn = TYPE_SIGN (from_type);
2901         /* Handle special case A), given x of unsigned type:
2902             ((signed type)x  < 0) <=> (x  > MAX_VAL(signed type))
2903             ((signed type)x >= 0) <=> (x <= MAX_VAL(signed type))  */
2904         if (!types_match (from_type, to_type))
2905           {
2906             if (cmp_code == LT_EXPR)
2907               cmp_code = GT_EXPR;
2908             if (cmp_code == GE_EXPR)
2909               cmp_code = LE_EXPR;
2910             c1 = wi::max_value (to_type);
2911           }
2912         /* To simplify this pattern, we require c3 = (c1 op c2).  Here we
2913            compute (c3 op' c2) and check if it equals to c1 with op' being
2914            the inverted operator of op.  Make sure overflow doesn't happen
2915            if it is undefined.  */
2916         if (op == PLUS_EXPR)
2917           real_c1 = wi::sub (c3, c2, sgn, &overflow);
2918         else
2919           real_c1 = wi::add (c3, c2, sgn, &overflow);
2921         code = cmp_code;
2922         if (!overflow || !TYPE_OVERFLOW_UNDEFINED (from_type))
2923           {
2924             /* Check if c1 equals to real_c1.  Boundary condition is handled
2925                by adjusting comparison operation if necessary.  */
2926             if (!wi::cmp (wi::sub (real_c1, 1, sgn, &overflow), c1, sgn)
2927                 && !overflow)
2928               {
2929                 /* X <= Y - 1 equals to X < Y.  */
2930                 if (cmp_code == LE_EXPR)
2931                   code = LT_EXPR;
2932                 /* X > Y - 1 equals to X >= Y.  */
2933                 if (cmp_code == GT_EXPR)
2934                   code = GE_EXPR;
2935               }
2936             if (!wi::cmp (wi::add (real_c1, 1, sgn, &overflow), c1, sgn)
2937                 && !overflow)
2938               {
2939                 /* X < Y + 1 equals to X <= Y.  */
2940                 if (cmp_code == LT_EXPR)
2941                   code = LE_EXPR;
2942                 /* X >= Y + 1 equals to X > Y.  */
2943                 if (cmp_code == GE_EXPR)
2944                   code = GT_EXPR;
2945               }
2946             if (code != cmp_code || !wi::cmp (real_c1, c1, sgn))
2947               {
2948                 if (cmp_code == LT_EXPR || cmp_code == LE_EXPR)
2949                   code = MIN_EXPR;
2950                 if (cmp_code == GT_EXPR || cmp_code == GE_EXPR)
2951                   code = MAX_EXPR;
2952               }
2953           }
2954       }
2955       (if (code == MAX_EXPR)
2956        (op (max @X { wide_int_to_tree (from_type, real_c1); })
2957            { wide_int_to_tree (from_type, c2); })
2958        (if (code == MIN_EXPR)
2959         (op (min @X { wide_int_to_tree (from_type, real_c1); })
2960             { wide_int_to_tree (from_type, c2); })))))))))
2962 (for cnd (cond vec_cond)
2963  /* A ? B : (A ? X : C) -> A ? B : C.  */
2964  (simplify
2965   (cnd @0 (cnd @0 @1 @2) @3)
2966   (cnd @0 @1 @3))
2967  (simplify
2968   (cnd @0 @1 (cnd @0 @2 @3))
2969   (cnd @0 @1 @3))
2970  /* A ? B : (!A ? C : X) -> A ? B : C.  */
2971  /* ???  This matches embedded conditions open-coded because genmatch
2972     would generate matching code for conditions in separate stmts only.
2973     The following is still important to merge then and else arm cases
2974     from if-conversion.  */
2975  (simplify
2976   (cnd @0 @1 (cnd @2 @3 @4))
2977   (if (inverse_conditions_p (@0, @2))
2978    (cnd @0 @1 @3)))
2979  (simplify
2980   (cnd @0 (cnd @1 @2 @3) @4)
2981   (if (inverse_conditions_p (@0, @1))
2982    (cnd @0 @3 @4)))
2984  /* A ? B : B -> B.  */
2985  (simplify
2986   (cnd @0 @1 @1)
2987   @1)
2989  /* !A ? B : C -> A ? C : B.  */
2990  (simplify
2991   (cnd (logical_inverted_value truth_valued_p@0) @1 @2)
2992   (cnd @0 @2 @1)))
2994 /* A + (B vcmp C ? 1 : 0) -> A - (B vcmp C ? -1 : 0), since vector comparisons
2995    return all -1 or all 0 results.  */
2996 /* ??? We could instead convert all instances of the vec_cond to negate,
2997    but that isn't necessarily a win on its own.  */
2998 (simplify
2999  (plus:c @3 (view_convert? (vec_cond:s @0 integer_each_onep@1 integer_zerop@2)))
3000  (if (VECTOR_TYPE_P (type)
3001       && known_eq (TYPE_VECTOR_SUBPARTS (type),
3002                    TYPE_VECTOR_SUBPARTS (TREE_TYPE (@1)))
3003       && (TYPE_MODE (TREE_TYPE (type))
3004           == TYPE_MODE (TREE_TYPE (TREE_TYPE (@1)))))
3005   (minus @3 (view_convert (vec_cond @0 (negate @1) @2)))))
3007 /* ... likewise A - (B vcmp C ? 1 : 0) -> A + (B vcmp C ? -1 : 0).  */
3008 (simplify
3009  (minus @3 (view_convert? (vec_cond:s @0 integer_each_onep@1 integer_zerop@2)))
3010  (if (VECTOR_TYPE_P (type)
3011       && known_eq (TYPE_VECTOR_SUBPARTS (type),
3012                    TYPE_VECTOR_SUBPARTS (TREE_TYPE (@1)))
3013       && (TYPE_MODE (TREE_TYPE (type))
3014           == TYPE_MODE (TREE_TYPE (TREE_TYPE (@1)))))
3015   (plus @3 (view_convert (vec_cond @0 (negate @1) @2)))))
3018 /* Simplifications of comparisons.  */
3020 /* See if we can reduce the magnitude of a constant involved in a
3021    comparison by changing the comparison code.  This is a canonicalization
3022    formerly done by maybe_canonicalize_comparison_1.  */
3023 (for cmp  (le gt)
3024      acmp (lt ge)
3025  (simplify
3026   (cmp @0 INTEGER_CST@1)
3027   (if (tree_int_cst_sgn (@1) == -1)
3028    (acmp @0 { wide_int_to_tree (TREE_TYPE (@1), wi::to_wide (@1) + 1); }))))
3029 (for cmp  (ge lt)
3030      acmp (gt le)
3031  (simplify
3032   (cmp @0 INTEGER_CST@1)
3033   (if (tree_int_cst_sgn (@1) == 1)
3034    (acmp @0 { wide_int_to_tree (TREE_TYPE (@1), wi::to_wide (@1) - 1); }))))
3037 /* We can simplify a logical negation of a comparison to the
3038    inverted comparison.  As we cannot compute an expression
3039    operator using invert_tree_comparison we have to simulate
3040    that with expression code iteration.  */
3041 (for cmp (tcc_comparison)
3042      icmp (inverted_tcc_comparison)
3043      ncmp (inverted_tcc_comparison_with_nans)
3044  /* Ideally we'd like to combine the following two patterns
3045     and handle some more cases by using
3046       (logical_inverted_value (cmp @0 @1))
3047     here but for that genmatch would need to "inline" that.
3048     For now implement what forward_propagate_comparison did.  */
3049  (simplify
3050   (bit_not (cmp @0 @1))
3051   (if (VECTOR_TYPE_P (type)
3052        || (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1))
3053    /* Comparison inversion may be impossible for trapping math,
3054       invert_tree_comparison will tell us.  But we can't use
3055       a computed operator in the replacement tree thus we have
3056       to play the trick below.  */
3057    (with { enum tree_code ic = invert_tree_comparison
3058              (cmp, HONOR_NANS (@0)); }
3059     (if (ic == icmp)
3060      (icmp @0 @1)
3061      (if (ic == ncmp)
3062       (ncmp @0 @1))))))
3063  (simplify
3064   (bit_xor (cmp @0 @1) integer_truep)
3065   (with { enum tree_code ic = invert_tree_comparison
3066             (cmp, HONOR_NANS (@0)); }
3067    (if (ic == icmp)
3068     (icmp @0 @1)
3069     (if (ic == ncmp)
3070      (ncmp @0 @1))))))
3072 /* Transform comparisons of the form X - Y CMP 0 to X CMP Y.
3073    ??? The transformation is valid for the other operators if overflow
3074    is undefined for the type, but performing it here badly interacts
3075    with the transformation in fold_cond_expr_with_comparison which
3076    attempts to synthetize ABS_EXPR.  */
3077 (for cmp (eq ne)
3078  (for sub (minus pointer_diff)
3079   (simplify
3080    (cmp (sub@2 @0 @1) integer_zerop)
3081    (if (single_use (@2))
3082     (cmp @0 @1)))))
3084 /* Transform comparisons of the form X * C1 CMP 0 to X CMP 0 in the
3085    signed arithmetic case.  That form is created by the compiler
3086    often enough for folding it to be of value.  One example is in
3087    computing loop trip counts after Operator Strength Reduction.  */
3088 (for cmp (simple_comparison)
3089      scmp (swapped_simple_comparison)
3090  (simplify
3091   (cmp (mult@3 @0 INTEGER_CST@1) integer_zerop@2)
3092   /* Handle unfolded multiplication by zero.  */
3093   (if (integer_zerop (@1))
3094    (cmp @1 @2)
3095    (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3096         && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
3097         && single_use (@3))
3098     /* If @1 is negative we swap the sense of the comparison.  */
3099     (if (tree_int_cst_sgn (@1) < 0)
3100      (scmp @0 @2)
3101      (cmp @0 @2))))))
3103 /* Simplify comparison of something with itself.  For IEEE
3104    floating-point, we can only do some of these simplifications.  */
3105 (for cmp (eq ge le)
3106  (simplify
3107   (cmp @0 @0)
3108   (if (! FLOAT_TYPE_P (TREE_TYPE (@0))
3109        || ! HONOR_NANS (@0))
3110    { constant_boolean_node (true, type); }
3111    (if (cmp != EQ_EXPR)
3112     (eq @0 @0)))))
3113 (for cmp (ne gt lt)
3114  (simplify
3115   (cmp @0 @0)
3116   (if (cmp != NE_EXPR
3117        || ! FLOAT_TYPE_P (TREE_TYPE (@0))
3118        || ! HONOR_NANS (@0))
3119    { constant_boolean_node (false, type); })))
3120 (for cmp (unle unge uneq)
3121  (simplify
3122   (cmp @0 @0)
3123   { constant_boolean_node (true, type); }))
3124 (for cmp (unlt ungt)
3125  (simplify
3126   (cmp @0 @0)
3127   (unordered @0 @0)))
3128 (simplify
3129  (ltgt @0 @0)
3130  (if (!flag_trapping_math)
3131   { constant_boolean_node (false, type); }))
3133 /* Fold ~X op ~Y as Y op X.  */
3134 (for cmp (simple_comparison)
3135  (simplify
3136   (cmp (bit_not@2 @0) (bit_not@3 @1))
3137   (if (single_use (@2) && single_use (@3))
3138    (cmp @1 @0))))
3140 /* Fold ~X op C as X op' ~C, where op' is the swapped comparison.  */
3141 (for cmp (simple_comparison)
3142      scmp (swapped_simple_comparison)
3143  (simplify
3144   (cmp (bit_not@2 @0) CONSTANT_CLASS_P@1)
3145   (if (single_use (@2)
3146        && (TREE_CODE (@1) == INTEGER_CST || TREE_CODE (@1) == VECTOR_CST))
3147    (scmp @0 (bit_not @1)))))
3149 (for cmp (simple_comparison)
3150  /* Fold (double)float1 CMP (double)float2 into float1 CMP float2.  */
3151  (simplify
3152   (cmp (convert@2 @0) (convert? @1))
3153   (if (FLOAT_TYPE_P (TREE_TYPE (@0))
3154        && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@2))
3155            == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0)))
3156        && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@2))
3157            == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1))))
3158    (with
3159     {
3160       tree type1 = TREE_TYPE (@1);
3161       if (TREE_CODE (@1) == REAL_CST && !DECIMAL_FLOAT_TYPE_P (type1))
3162         {
3163           REAL_VALUE_TYPE orig = TREE_REAL_CST (@1);
3164           if (TYPE_PRECISION (type1) > TYPE_PRECISION (float_type_node)
3165               && exact_real_truncate (TYPE_MODE (float_type_node), &orig))
3166             type1 = float_type_node;
3167           if (TYPE_PRECISION (type1) > TYPE_PRECISION (double_type_node)
3168               && exact_real_truncate (TYPE_MODE (double_type_node), &orig))
3169             type1 = double_type_node;
3170         }
3171       tree newtype
3172         = (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type1)
3173            ? TREE_TYPE (@0) : type1); 
3174     }
3175     (if (TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (newtype))
3176      (cmp (convert:newtype @0) (convert:newtype @1))))))
3178  (simplify
3179   (cmp @0 REAL_CST@1)
3180   /* IEEE doesn't distinguish +0 and -0 in comparisons.  */
3181   (switch
3182    /* a CMP (-0) -> a CMP 0  */
3183    (if (REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@1)))
3184     (cmp @0 { build_real (TREE_TYPE (@1), dconst0); }))
3185    /* x != NaN is always true, other ops are always false.  */
3186    (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
3187         && ! HONOR_SNANS (@1))
3188     { constant_boolean_node (cmp == NE_EXPR, type); })
3189    /* Fold comparisons against infinity.  */
3190    (if (REAL_VALUE_ISINF (TREE_REAL_CST (@1))
3191         && MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (@1))))
3192     (with
3193      {
3194        REAL_VALUE_TYPE max;
3195        enum tree_code code = cmp;
3196        bool neg = REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1));
3197        if (neg)
3198          code = swap_tree_comparison (code);
3199      }
3200      (switch
3201       /* x > +Inf is always false, if we ignore NaNs or exceptions.  */
3202       (if (code == GT_EXPR
3203            && !(HONOR_NANS (@0) && flag_trapping_math))
3204        { constant_boolean_node (false, type); })
3205       (if (code == LE_EXPR)
3206        /* x <= +Inf is always true, if we don't care about NaNs.  */
3207        (if (! HONOR_NANS (@0))
3208         { constant_boolean_node (true, type); }
3209         /* x <= +Inf is the same as x == x, i.e. !isnan(x), but this loses
3210            an "invalid" exception.  */
3211         (if (!flag_trapping_math)
3212          (eq @0 @0))))
3213       /* x == +Inf and x >= +Inf are always equal to x > DBL_MAX, but
3214          for == this introduces an exception for x a NaN.  */
3215       (if ((code == EQ_EXPR && !(HONOR_NANS (@0) && flag_trapping_math))
3216            || code == GE_EXPR)
3217        (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
3218         (if (neg)
3219          (lt @0 { build_real (TREE_TYPE (@0), max); })
3220          (gt @0 { build_real (TREE_TYPE (@0), max); }))))
3221       /* x < +Inf is always equal to x <= DBL_MAX.  */
3222       (if (code == LT_EXPR)
3223        (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
3224         (if (neg)
3225          (ge @0 { build_real (TREE_TYPE (@0), max); })
3226          (le @0 { build_real (TREE_TYPE (@0), max); }))))
3227       /* x != +Inf is always equal to !(x > DBL_MAX), but this introduces
3228          an exception for x a NaN so use an unordered comparison.  */
3229       (if (code == NE_EXPR)
3230        (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
3231         (if (! HONOR_NANS (@0))
3232          (if (neg)
3233           (ge @0 { build_real (TREE_TYPE (@0), max); })
3234           (le @0 { build_real (TREE_TYPE (@0), max); }))
3235          (if (neg)
3236           (unge @0 { build_real (TREE_TYPE (@0), max); })
3237           (unle @0 { build_real (TREE_TYPE (@0), max); }))))))))))
3239  /* If this is a comparison of a real constant with a PLUS_EXPR
3240     or a MINUS_EXPR of a real constant, we can convert it into a
3241     comparison with a revised real constant as long as no overflow
3242     occurs when unsafe_math_optimizations are enabled.  */
3243  (if (flag_unsafe_math_optimizations)
3244   (for op (plus minus)
3245    (simplify
3246     (cmp (op @0 REAL_CST@1) REAL_CST@2)
3247     (with
3248      {
3249        tree tem = const_binop (op == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR,
3250                                TREE_TYPE (@1), @2, @1);
3251      }
3252      (if (tem && !TREE_OVERFLOW (tem))
3253       (cmp @0 { tem; }))))))
3255  /* Likewise, we can simplify a comparison of a real constant with
3256     a MINUS_EXPR whose first operand is also a real constant, i.e.
3257     (c1 - x) < c2 becomes x > c1-c2.  Reordering is allowed on
3258     floating-point types only if -fassociative-math is set.  */
3259  (if (flag_associative_math)
3260   (simplify
3261    (cmp (minus REAL_CST@0 @1) REAL_CST@2)
3262    (with { tree tem = const_binop (MINUS_EXPR, TREE_TYPE (@1), @0, @2); }
3263     (if (tem && !TREE_OVERFLOW (tem))
3264      (cmp { tem; } @1)))))
3266  /* Fold comparisons against built-in math functions.  */
3267  (if (flag_unsafe_math_optimizations
3268       && ! flag_errno_math)
3269   (for sq (SQRT)
3270    (simplify
3271     (cmp (sq @0) REAL_CST@1)
3272     (switch
3273      (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
3274       (switch
3275        /* sqrt(x) < y is always false, if y is negative.  */
3276        (if (cmp == EQ_EXPR || cmp == LT_EXPR || cmp == LE_EXPR)
3277         { constant_boolean_node (false, type); })
3278        /* sqrt(x) > y is always true, if y is negative and we
3279           don't care about NaNs, i.e. negative values of x.  */
3280        (if (cmp == NE_EXPR || !HONOR_NANS (@0))
3281         { constant_boolean_node (true, type); })
3282        /* sqrt(x) > y is the same as x >= 0, if y is negative.  */
3283        (ge @0 { build_real (TREE_TYPE (@0), dconst0); })))
3284      (if (real_equal (TREE_REAL_CST_PTR (@1), &dconst0))
3285       (switch
3286        /* sqrt(x) < 0 is always false.  */
3287        (if (cmp == LT_EXPR)
3288         { constant_boolean_node (false, type); })
3289        /* sqrt(x) >= 0 is always true if we don't care about NaNs.  */
3290        (if (cmp == GE_EXPR && !HONOR_NANS (@0))
3291         { constant_boolean_node (true, type); })
3292        /* sqrt(x) <= 0 -> x == 0.  */
3293        (if (cmp == LE_EXPR)
3294         (eq @0 @1))
3295        /* Otherwise sqrt(x) cmp 0 -> x cmp 0.  Here cmp can be >=, >,
3296           == or !=.  In the last case:
3298             (sqrt(x) != 0) == (NaN != 0) == true == (x != 0)
3300           if x is negative or NaN.  Due to -funsafe-math-optimizations,
3301           the results for other x follow from natural arithmetic.  */
3302        (cmp @0 @1)))
3303      (if (cmp == GT_EXPR || cmp == GE_EXPR)
3304       (with
3305        {
3306          REAL_VALUE_TYPE c2;
3307          real_arithmetic (&c2, MULT_EXPR,
3308                           &TREE_REAL_CST (@1), &TREE_REAL_CST (@1));
3309          real_convert (&c2, TYPE_MODE (TREE_TYPE (@0)), &c2);
3310        }
3311        (if (REAL_VALUE_ISINF (c2))
3312         /* sqrt(x) > y is x == +Inf, when y is very large.  */
3313         (if (HONOR_INFINITIES (@0))
3314          (eq @0 { build_real (TREE_TYPE (@0), c2); })
3315          { constant_boolean_node (false, type); })
3316         /* sqrt(x) > c is the same as x > c*c.  */
3317         (cmp @0 { build_real (TREE_TYPE (@0), c2); }))))
3318      (if (cmp == LT_EXPR || cmp == LE_EXPR)
3319       (with
3320        {
3321          REAL_VALUE_TYPE c2;
3322          real_arithmetic (&c2, MULT_EXPR,
3323                           &TREE_REAL_CST (@1), &TREE_REAL_CST (@1));
3324          real_convert (&c2, TYPE_MODE (TREE_TYPE (@0)), &c2);
3325        }
3326        (if (REAL_VALUE_ISINF (c2))
3327         (switch
3328          /* sqrt(x) < y is always true, when y is a very large
3329             value and we don't care about NaNs or Infinities.  */
3330          (if (! HONOR_NANS (@0) && ! HONOR_INFINITIES (@0))
3331           { constant_boolean_node (true, type); })
3332          /* sqrt(x) < y is x != +Inf when y is very large and we
3333             don't care about NaNs.  */
3334          (if (! HONOR_NANS (@0))
3335           (ne @0 { build_real (TREE_TYPE (@0), c2); }))
3336          /* sqrt(x) < y is x >= 0 when y is very large and we
3337             don't care about Infinities.  */
3338          (if (! HONOR_INFINITIES (@0))
3339           (ge @0 { build_real (TREE_TYPE (@0), dconst0); }))
3340          /* sqrt(x) < y is x >= 0 && x != +Inf, when y is large.  */
3341          (if (GENERIC)
3342           (truth_andif
3343            (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
3344            (ne @0 { build_real (TREE_TYPE (@0), c2); }))))
3345         /* sqrt(x) < c is the same as x < c*c, if we ignore NaNs.  */
3346         (if (! HONOR_NANS (@0))
3347          (cmp @0 { build_real (TREE_TYPE (@0), c2); })
3348          /* sqrt(x) < c is the same as x >= 0 && x < c*c.  */
3349          (if (GENERIC)
3350           (truth_andif
3351            (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
3352            (cmp @0 { build_real (TREE_TYPE (@0), c2); })))))))))
3353    /* Transform sqrt(x) cmp sqrt(y) -> x cmp y.  */
3354    (simplify
3355     (cmp (sq @0) (sq @1))
3356       (if (! HONOR_NANS (@0))
3357         (cmp @0 @1))))))
3359 /* Optimize various special cases of (FTYPE) N CMP CST.  */
3360 (for cmp  (lt le eq ne ge gt)
3361      icmp (le le eq ne ge ge)
3362  (simplify
3363   (cmp (float @0) REAL_CST@1)
3364    (if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (@1))
3365         && ! DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1)))
3366     (with
3367      {
3368        tree itype = TREE_TYPE (@0);
3369        signop isign = TYPE_SIGN (itype);
3370        format_helper fmt (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (@1))));
3371        const REAL_VALUE_TYPE *cst = TREE_REAL_CST_PTR (@1);
3372        /* Be careful to preserve any potential exceptions due to
3373           NaNs.  qNaNs are ok in == or != context.
3374           TODO: relax under -fno-trapping-math or
3375           -fno-signaling-nans.  */
3376        bool exception_p
3377          = real_isnan (cst) && (cst->signalling
3378                                 || (cmp != EQ_EXPR && cmp != NE_EXPR));
3379        /* INT?_MIN is power-of-two so it takes
3380           only one mantissa bit.  */
3381        bool signed_p = isign == SIGNED;
3382        bool itype_fits_ftype_p
3383          = TYPE_PRECISION (itype) - signed_p <= significand_size (fmt);
3384      }
3385      /* TODO: allow non-fitting itype and SNaNs when
3386         -fno-trapping-math.  */
3387      (if (itype_fits_ftype_p && ! exception_p)
3388       (with
3389        {
3390          REAL_VALUE_TYPE imin, imax;
3391          real_from_integer (&imin, fmt, wi::min_value (itype), isign);
3392          real_from_integer (&imax, fmt, wi::max_value (itype), isign);
3394          REAL_VALUE_TYPE icst;
3395          if (cmp == GT_EXPR || cmp == GE_EXPR)
3396            real_ceil (&icst, fmt, cst);
3397          else if (cmp == LT_EXPR || cmp == LE_EXPR)
3398            real_floor (&icst, fmt, cst);
3399          else
3400            real_trunc (&icst, fmt, cst);
3402          bool cst_int_p = !real_isnan (cst) && real_identical (&icst, cst);
3404          bool overflow_p = false;
3405          wide_int icst_val
3406            = real_to_integer (&icst, &overflow_p, TYPE_PRECISION (itype));
3407        }
3408        (switch
3409         /* Optimize cases when CST is outside of ITYPE's range.  */
3410         (if (real_compare (LT_EXPR, cst, &imin))
3411          { constant_boolean_node (cmp == GT_EXPR || cmp == GE_EXPR || cmp == NE_EXPR,
3412                                   type); })
3413         (if (real_compare (GT_EXPR, cst, &imax))
3414          { constant_boolean_node (cmp == LT_EXPR || cmp == LE_EXPR || cmp == NE_EXPR,
3415                                   type); })
3416         /* Remove cast if CST is an integer representable by ITYPE.  */
3417         (if (cst_int_p)
3418          (cmp @0 { gcc_assert (!overflow_p);
3419                    wide_int_to_tree (itype, icst_val); })
3420         )
3421         /* When CST is fractional, optimize
3422             (FTYPE) N == CST -> 0
3423             (FTYPE) N != CST -> 1.  */
3424         (if (cmp == EQ_EXPR || cmp == NE_EXPR)
3425          { constant_boolean_node (cmp == NE_EXPR, type); }) 
3426         /* Otherwise replace with sensible integer constant.  */
3427         (with
3428          {
3429            gcc_checking_assert (!overflow_p);
3430          }
3431          (icmp @0 { wide_int_to_tree (itype, icst_val); })))))))))
3433 /* Fold A /[ex] B CMP C to A CMP B * C.  */
3434 (for cmp (eq ne)
3435  (simplify
3436   (cmp (exact_div @0 @1) INTEGER_CST@2)
3437   (if (!integer_zerop (@1))
3438    (if (wi::to_wide (@2) == 0)
3439     (cmp @0 @2)
3440     (if (TREE_CODE (@1) == INTEGER_CST)
3441      (with
3442       {
3443         wi::overflow_type ovf;
3444         wide_int prod = wi::mul (wi::to_wide (@2), wi::to_wide (@1),
3445                                  TYPE_SIGN (TREE_TYPE (@1)), &ovf);
3446       }
3447       (if (ovf)
3448        { constant_boolean_node (cmp == NE_EXPR, type); }
3449        (cmp @0 { wide_int_to_tree (TREE_TYPE (@0), prod); }))))))))
3450 (for cmp (lt le gt ge)
3451  (simplify
3452   (cmp (exact_div @0 INTEGER_CST@1) INTEGER_CST@2)
3453   (if (wi::gt_p (wi::to_wide (@1), 0, TYPE_SIGN (TREE_TYPE (@1))))
3454    (with
3455     {
3456       wi::overflow_type ovf;
3457       wide_int prod = wi::mul (wi::to_wide (@2), wi::to_wide (@1),
3458                                TYPE_SIGN (TREE_TYPE (@1)), &ovf);
3459     }
3460     (if (ovf)
3461      { constant_boolean_node (wi::lt_p (wi::to_wide (@2), 0,
3462                                         TYPE_SIGN (TREE_TYPE (@2)))
3463                               != (cmp == LT_EXPR || cmp == LE_EXPR), type); }
3464      (cmp @0 { wide_int_to_tree (TREE_TYPE (@0), prod); }))))))
3466 /* Unordered tests if either argument is a NaN.  */
3467 (simplify
3468  (bit_ior (unordered @0 @0) (unordered @1 @1))
3469  (if (types_match (@0, @1))
3470   (unordered @0 @1)))
3471 (simplify
3472  (bit_and (ordered @0 @0) (ordered @1 @1))
3473  (if (types_match (@0, @1))
3474   (ordered @0 @1)))
3475 (simplify
3476  (bit_ior:c (unordered @0 @0) (unordered:c@2 @0 @1))
3477  @2)
3478 (simplify
3479  (bit_and:c (ordered @0 @0) (ordered:c@2 @0 @1))
3480  @2)
3482 /* Simple range test simplifications.  */
3483 /* A < B || A >= B -> true.  */
3484 (for test1 (lt le le le ne ge)
3485      test2 (ge gt ge ne eq ne)
3486  (simplify
3487   (bit_ior:c (test1 @0 @1) (test2 @0 @1))
3488   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
3489        || VECTOR_INTEGER_TYPE_P (TREE_TYPE (@0)))
3490    { constant_boolean_node (true, type); })))
3491 /* A < B && A >= B -> false.  */
3492 (for test1 (lt lt lt le ne eq)
3493      test2 (ge gt eq gt eq gt)
3494  (simplify
3495   (bit_and:c (test1 @0 @1) (test2 @0 @1))
3496   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
3497        || VECTOR_INTEGER_TYPE_P (TREE_TYPE (@0)))
3498    { constant_boolean_node (false, type); })))
3500 /* A & (2**N - 1) <= 2**K - 1 -> A & (2**N - 2**K) == 0
3501    A & (2**N - 1) >  2**K - 1 -> A & (2**N - 2**K) != 0
3503    Note that comparisons
3504      A & (2**N - 1) <  2**K   -> A & (2**N - 2**K) == 0
3505      A & (2**N - 1) >= 2**K   -> A & (2**N - 2**K) != 0
3506    will be canonicalized to above so there's no need to
3507    consider them here.
3508  */
3510 (for cmp (le gt)
3511      eqcmp (eq ne)
3512  (simplify
3513   (cmp (bit_and@0 @1 INTEGER_CST@2) INTEGER_CST@3)
3514   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)))
3515    (with
3516     {
3517      tree ty = TREE_TYPE (@0);
3518      unsigned prec = TYPE_PRECISION (ty);
3519      wide_int mask = wi::to_wide (@2, prec);
3520      wide_int rhs = wi::to_wide (@3, prec);
3521      signop sgn = TYPE_SIGN (ty);
3522     }
3523     (if ((mask & (mask + 1)) == 0 && wi::gt_p (rhs, 0, sgn)
3524          && (rhs & (rhs + 1)) == 0 && wi::ge_p (mask, rhs, sgn))
3525       (eqcmp (bit_and @1 { wide_int_to_tree (ty, mask - rhs); })
3526              { build_zero_cst (ty); }))))))
3528 /* -A CMP -B -> B CMP A.  */
3529 (for cmp (tcc_comparison)
3530      scmp (swapped_tcc_comparison)
3531  (simplify
3532   (cmp (negate @0) (negate @1))
3533   (if (FLOAT_TYPE_P (TREE_TYPE (@0))
3534        || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3535            && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
3536    (scmp @0 @1)))
3537  (simplify
3538   (cmp (negate @0) CONSTANT_CLASS_P@1)
3539   (if (FLOAT_TYPE_P (TREE_TYPE (@0))
3540        || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3541            && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
3542    (with { tree tem = const_unop (NEGATE_EXPR, TREE_TYPE (@0), @1); }
3543     (if (tem && !TREE_OVERFLOW (tem))
3544      (scmp @0 { tem; }))))))
3546 /* Convert ABS_EXPR<x> == 0 or ABS_EXPR<x> != 0 to x == 0 or x != 0.  */
3547 (for op (eq ne)
3548  (simplify
3549   (op (abs @0) zerop@1)
3550   (op @0 @1)))
3552 /* From fold_sign_changed_comparison and fold_widened_comparison.
3553    FIXME: the lack of symmetry is disturbing.  */
3554 (for cmp (simple_comparison)
3555  (simplify
3556   (cmp (convert@0 @00) (convert?@1 @10))
3557   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
3558        /* Disable this optimization if we're casting a function pointer
3559           type on targets that require function pointer canonicalization.  */
3560        && !(targetm.have_canonicalize_funcptr_for_compare ()
3561             && POINTER_TYPE_P (TREE_TYPE (@00))
3562             && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@00))))
3563        && single_use (@0))
3564    (if (TYPE_PRECISION (TREE_TYPE (@00)) == TYPE_PRECISION (TREE_TYPE (@0))
3565         && (TREE_CODE (@10) == INTEGER_CST
3566             || @1 != @10)
3567         && (TYPE_UNSIGNED (TREE_TYPE (@00)) == TYPE_UNSIGNED (TREE_TYPE (@0))
3568             || cmp == NE_EXPR
3569             || cmp == EQ_EXPR)
3570         && !POINTER_TYPE_P (TREE_TYPE (@00)))
3571     /* ???  The special-casing of INTEGER_CST conversion was in the original
3572        code and here to avoid a spurious overflow flag on the resulting
3573        constant which fold_convert produces.  */
3574     (if (TREE_CODE (@1) == INTEGER_CST)
3575      (cmp @00 { force_fit_type (TREE_TYPE (@00), wi::to_widest (@1), 0,
3576                                 TREE_OVERFLOW (@1)); })
3577      (cmp @00 (convert @1)))
3579     (if (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (TREE_TYPE (@00)))
3580      /* If possible, express the comparison in the shorter mode.  */
3581      (if ((cmp == EQ_EXPR || cmp == NE_EXPR
3582            || TYPE_UNSIGNED (TREE_TYPE (@0)) == TYPE_UNSIGNED (TREE_TYPE (@00))
3583            || (!TYPE_UNSIGNED (TREE_TYPE (@0))
3584                && TYPE_UNSIGNED (TREE_TYPE (@00))))
3585           && (types_match (TREE_TYPE (@10), TREE_TYPE (@00))
3586               || ((TYPE_PRECISION (TREE_TYPE (@00))
3587                    >= TYPE_PRECISION (TREE_TYPE (@10)))
3588                   && (TYPE_UNSIGNED (TREE_TYPE (@00))
3589                       == TYPE_UNSIGNED (TREE_TYPE (@10))))
3590               || (TREE_CODE (@10) == INTEGER_CST
3591                   && INTEGRAL_TYPE_P (TREE_TYPE (@00))
3592                   && int_fits_type_p (@10, TREE_TYPE (@00)))))
3593       (cmp @00 (convert @10))
3594       (if (TREE_CODE (@10) == INTEGER_CST
3595            && INTEGRAL_TYPE_P (TREE_TYPE (@00))
3596            && !int_fits_type_p (@10, TREE_TYPE (@00)))
3597        (with
3598         {
3599           tree min = lower_bound_in_type (TREE_TYPE (@10), TREE_TYPE (@00));
3600           tree max = upper_bound_in_type (TREE_TYPE (@10), TREE_TYPE (@00));
3601           bool above = integer_nonzerop (const_binop (LT_EXPR, type, max, @10));
3602           bool below = integer_nonzerop (const_binop (LT_EXPR, type, @10, min));
3603         }
3604         (if (above || below)
3605          (if (cmp == EQ_EXPR || cmp == NE_EXPR)
3606           { constant_boolean_node (cmp == EQ_EXPR ? false : true, type); }
3607           (if (cmp == LT_EXPR || cmp == LE_EXPR)
3608            { constant_boolean_node (above ? true : false, type); }
3609            (if (cmp == GT_EXPR || cmp == GE_EXPR)
3610             { constant_boolean_node (above ? false : true, type); }))))))))))))
3612 (for cmp (eq ne)
3613  /* A local variable can never be pointed to by
3614     the default SSA name of an incoming parameter.
3615     SSA names are canonicalized to 2nd place.  */
3616  (simplify
3617   (cmp addr@0 SSA_NAME@1)
3618   (if (SSA_NAME_IS_DEFAULT_DEF (@1)
3619        && TREE_CODE (SSA_NAME_VAR (@1)) == PARM_DECL)
3620    (with { tree base = get_base_address (TREE_OPERAND (@0, 0)); }
3621     (if (TREE_CODE (base) == VAR_DECL
3622          && auto_var_in_fn_p (base, current_function_decl))
3623      (if (cmp == NE_EXPR)
3624       { constant_boolean_node (true, type); }
3625       { constant_boolean_node (false, type); }))))))
3627 /* Equality compare simplifications from fold_binary  */
3628 (for cmp (eq ne)
3630  /* If we have (A | C) == D where C & ~D != 0, convert this into 0.
3631     Similarly for NE_EXPR.  */
3632  (simplify
3633   (cmp (convert?@3 (bit_ior @0 INTEGER_CST@1)) INTEGER_CST@2)
3634   (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0))
3635        && wi::bit_and_not (wi::to_wide (@1), wi::to_wide (@2)) != 0)
3636    { constant_boolean_node (cmp == NE_EXPR, type); }))
3638  /* (X ^ Y) == 0 becomes X == Y, and (X ^ Y) != 0 becomes X != Y.  */
3639  (simplify
3640   (cmp (bit_xor @0 @1) integer_zerop)
3641   (cmp @0 @1))
3643  /* (X ^ Y) == Y becomes X == 0.
3644     Likewise (X ^ Y) == X becomes Y == 0.  */
3645  (simplify
3646   (cmp:c (bit_xor:c @0 @1) @0)
3647   (cmp @1 { build_zero_cst (TREE_TYPE (@1)); }))
3649  /* (X ^ C1) op C2 can be rewritten as X op (C1 ^ C2).  */
3650  (simplify
3651   (cmp (convert?@3 (bit_xor @0 INTEGER_CST@1)) INTEGER_CST@2)
3652   (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0)))
3653    (cmp @0 (bit_xor @1 (convert @2)))))
3655  (simplify
3656   (cmp (convert? addr@0) integer_zerop)
3657   (if (tree_single_nonzero_warnv_p (@0, NULL))
3658    { constant_boolean_node (cmp == NE_EXPR, type); })))
3660 /* If we have (A & C) == C where C is a power of 2, convert this into
3661    (A & C) != 0.  Similarly for NE_EXPR.  */
3662 (for cmp (eq ne)
3663      icmp (ne eq)
3664  (simplify
3665   (cmp (bit_and@2 @0 integer_pow2p@1) @1)
3666   (icmp @2 { build_zero_cst (TREE_TYPE (@0)); })))
3668 /* If we have (A & C) != 0 ? D : 0 where C and D are powers of 2,
3669    convert this into a shift followed by ANDing with D.  */
3670 (simplify
3671  (cond
3672   (ne (bit_and @0 integer_pow2p@1) integer_zerop)
3673   INTEGER_CST@2 integer_zerop)
3674  (if (integer_pow2p (@2))
3675   (with {
3676      int shift = (wi::exact_log2 (wi::to_wide (@2))
3677                   - wi::exact_log2 (wi::to_wide (@1)));
3678    }
3679    (if (shift > 0)
3680     (bit_and
3681      (lshift (convert @0) { build_int_cst (integer_type_node, shift); }) @2)
3682     (bit_and
3683      (convert (rshift @0 { build_int_cst (integer_type_node, -shift); }))
3684      @2)))))
3686 /* If we have (A & C) != 0 where C is the sign bit of A, convert
3687    this into A < 0.  Similarly for (A & C) == 0 into A >= 0.  */
3688 (for cmp (eq ne)
3689      ncmp (ge lt)
3690  (simplify
3691   (cmp (bit_and (convert?@2 @0) integer_pow2p@1) integer_zerop)
3692   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
3693        && type_has_mode_precision_p (TREE_TYPE (@0))
3694        && element_precision (@2) >= element_precision (@0)
3695        && wi::only_sign_bit_p (wi::to_wide (@1), element_precision (@0)))
3696    (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
3697     (ncmp (convert:stype @0) { build_zero_cst (stype); })))))
3699 /* If we have A < 0 ? C : 0 where C is a power of 2, convert
3700    this into a right shift or sign extension followed by ANDing with C.  */
3701 (simplify
3702  (cond
3703   (lt @0 integer_zerop)
3704   INTEGER_CST@1 integer_zerop)
3705  (if (integer_pow2p (@1)
3706       && !TYPE_UNSIGNED (TREE_TYPE (@0)))
3707   (with {
3708     int shift = element_precision (@0) - wi::exact_log2 (wi::to_wide (@1)) - 1;
3709    }
3710    (if (shift >= 0)
3711     (bit_and
3712      (convert (rshift @0 { build_int_cst (integer_type_node, shift); }))
3713      @1)
3714     /* Otherwise ctype must be wider than TREE_TYPE (@0) and pure
3715        sign extension followed by AND with C will achieve the effect.  */
3716     (bit_and (convert @0) @1)))))
3718 /* When the addresses are not directly of decls compare base and offset.
3719    This implements some remaining parts of fold_comparison address
3720    comparisons but still no complete part of it.  Still it is good
3721    enough to make fold_stmt not regress when not dispatching to fold_binary.  */
3722 (for cmp (simple_comparison)
3723  (simplify
3724   (cmp (convert1?@2 addr@0) (convert2? addr@1))
3725   (with
3726    {
3727      poly_int64 off0, off1;
3728      tree base0 = get_addr_base_and_unit_offset (TREE_OPERAND (@0, 0), &off0);
3729      tree base1 = get_addr_base_and_unit_offset (TREE_OPERAND (@1, 0), &off1);
3730      if (base0 && TREE_CODE (base0) == MEM_REF)
3731        {
3732          off0 += mem_ref_offset (base0).force_shwi ();
3733          base0 = TREE_OPERAND (base0, 0);
3734        }
3735      if (base1 && TREE_CODE (base1) == MEM_REF)
3736        {
3737          off1 += mem_ref_offset (base1).force_shwi ();
3738          base1 = TREE_OPERAND (base1, 0);
3739        }
3740    }
3741    (if (base0 && base1)
3742     (with
3743      {
3744        int equal = 2;
3745        /* Punt in GENERIC on variables with value expressions;
3746           the value expressions might point to fields/elements
3747           of other vars etc.  */
3748        if (GENERIC
3749            && ((VAR_P (base0) && DECL_HAS_VALUE_EXPR_P (base0))
3750                || (VAR_P (base1) && DECL_HAS_VALUE_EXPR_P (base1))))
3751          ;
3752        else if (decl_in_symtab_p (base0)
3753                 && decl_in_symtab_p (base1))
3754          equal = symtab_node::get_create (base0)
3755                    ->equal_address_to (symtab_node::get_create (base1));
3756        else if ((DECL_P (base0)
3757                  || TREE_CODE (base0) == SSA_NAME
3758                  || TREE_CODE (base0) == STRING_CST)
3759                 && (DECL_P (base1)
3760                     || TREE_CODE (base1) == SSA_NAME
3761                     || TREE_CODE (base1) == STRING_CST))
3762          equal = (base0 == base1);
3763      }
3764      (if (equal == 1
3765           && (cmp == EQ_EXPR || cmp == NE_EXPR
3766               /* If the offsets are equal we can ignore overflow.  */
3767               || known_eq (off0, off1)
3768               || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
3769                  /* Or if we compare using pointers to decls or strings.  */
3770               || (POINTER_TYPE_P (TREE_TYPE (@2))
3771                   && (DECL_P (base0) || TREE_CODE (base0) == STRING_CST))))
3772       (switch
3773        (if (cmp == EQ_EXPR && (known_eq (off0, off1) || known_ne (off0, off1)))
3774         { constant_boolean_node (known_eq (off0, off1), type); })
3775        (if (cmp == NE_EXPR && (known_eq (off0, off1) || known_ne (off0, off1)))
3776         { constant_boolean_node (known_ne (off0, off1), type); })
3777        (if (cmp == LT_EXPR && (known_lt (off0, off1) || known_ge (off0, off1)))
3778         { constant_boolean_node (known_lt (off0, off1), type); })
3779        (if (cmp == LE_EXPR && (known_le (off0, off1) || known_gt (off0, off1)))
3780         { constant_boolean_node (known_le (off0, off1), type); })
3781        (if (cmp == GE_EXPR && (known_ge (off0, off1) || known_lt (off0, off1)))
3782         { constant_boolean_node (known_ge (off0, off1), type); })
3783        (if (cmp == GT_EXPR && (known_gt (off0, off1) || known_le (off0, off1)))
3784         { constant_boolean_node (known_gt (off0, off1), type); }))
3785       (if (equal == 0
3786            && DECL_P (base0) && DECL_P (base1)
3787            /* If we compare this as integers require equal offset.  */
3788            && (!INTEGRAL_TYPE_P (TREE_TYPE (@2))
3789                || known_eq (off0, off1)))
3790        (switch
3791         (if (cmp == EQ_EXPR)
3792          { constant_boolean_node (false, type); })
3793         (if (cmp == NE_EXPR)
3794          { constant_boolean_node (true, type); })))))))))
3796 /* Simplify pointer equality compares using PTA.  */
3797 (for neeq (ne eq)
3798  (simplify
3799   (neeq @0 @1)
3800   (if (POINTER_TYPE_P (TREE_TYPE (@0))
3801        && ptrs_compare_unequal (@0, @1))
3802    { constant_boolean_node (neeq != EQ_EXPR, type); })))
3804 /* PR70920: Transform (intptr_t)x eq/ne CST to x eq/ne (typeof x) CST.
3805    and (typeof ptr_cst) x eq/ne ptr_cst to x eq/ne (typeof x) CST.
3806    Disable the transform if either operand is pointer to function.
3807    This broke pr22051-2.c for arm where function pointer
3808    canonicalizaion is not wanted.  */
3810 (for cmp (ne eq)
3811  (simplify
3812   (cmp (convert @0) INTEGER_CST@1)
3813   (if (((POINTER_TYPE_P (TREE_TYPE (@0))
3814          && !FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@0)))
3815          && INTEGRAL_TYPE_P (TREE_TYPE (@1)))
3816         || (INTEGRAL_TYPE_P (TREE_TYPE (@0))
3817             && POINTER_TYPE_P (TREE_TYPE (@1))
3818             && !FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@1)))))
3819        && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
3820    (cmp @0 (convert @1)))))
3822 /* Non-equality compare simplifications from fold_binary  */
3823 (for cmp (lt gt le ge)
3824  /* Comparisons with the highest or lowest possible integer of
3825     the specified precision will have known values.  */
3826  (simplify
3827   (cmp (convert?@2 @0) INTEGER_CST@1)
3828   (if ((INTEGRAL_TYPE_P (TREE_TYPE (@1)) || POINTER_TYPE_P (TREE_TYPE (@1)))
3829        && tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@0)))
3830    (with
3831     {
3832       tree arg1_type = TREE_TYPE (@1);
3833       unsigned int prec = TYPE_PRECISION (arg1_type);
3834       wide_int max = wi::max_value (arg1_type);
3835       wide_int signed_max = wi::max_value (prec, SIGNED);
3836       wide_int min = wi::min_value (arg1_type);
3837     }
3838     (switch
3839      (if (wi::to_wide (@1) == max)
3840       (switch
3841        (if (cmp == GT_EXPR)
3842         { constant_boolean_node (false, type); })
3843        (if (cmp == GE_EXPR)
3844         (eq @2 @1))
3845        (if (cmp == LE_EXPR)
3846         { constant_boolean_node (true, type); })
3847        (if (cmp == LT_EXPR)
3848         (ne @2 @1))))
3849      (if (wi::to_wide (@1) == min)
3850       (switch
3851        (if (cmp == LT_EXPR)
3852         { constant_boolean_node (false, type); })
3853        (if (cmp == LE_EXPR)
3854         (eq @2 @1))
3855        (if (cmp == GE_EXPR)
3856         { constant_boolean_node (true, type); })
3857        (if (cmp == GT_EXPR)
3858         (ne @2 @1))))
3859      (if (wi::to_wide (@1) == max - 1)
3860       (switch
3861        (if (cmp == GT_EXPR)
3862         (eq @2 { wide_int_to_tree (TREE_TYPE (@1), wi::to_wide (@1) + 1); }))
3863        (if (cmp == LE_EXPR)
3864         (ne @2 { wide_int_to_tree (TREE_TYPE (@1), wi::to_wide (@1) + 1); }))))
3865      (if (wi::to_wide (@1) == min + 1)
3866       (switch
3867        (if (cmp == GE_EXPR)
3868         (ne @2 { wide_int_to_tree (TREE_TYPE (@1), wi::to_wide (@1) - 1); }))
3869        (if (cmp == LT_EXPR)
3870         (eq @2 { wide_int_to_tree (TREE_TYPE (@1), wi::to_wide (@1) - 1); }))))
3871      (if (wi::to_wide (@1) == signed_max
3872           && TYPE_UNSIGNED (arg1_type)
3873           /* We will flip the signedness of the comparison operator
3874              associated with the mode of @1, so the sign bit is
3875              specified by this mode.  Check that @1 is the signed
3876              max associated with this sign bit.  */
3877           && prec == GET_MODE_PRECISION (SCALAR_INT_TYPE_MODE (arg1_type))
3878           /* signed_type does not work on pointer types.  */
3879           && INTEGRAL_TYPE_P (arg1_type))
3880       /* The following case also applies to X < signed_max+1
3881          and X >= signed_max+1 because previous transformations.  */
3882       (if (cmp == LE_EXPR || cmp == GT_EXPR)
3883        (with { tree st = signed_type_for (arg1_type); }
3884         (if (cmp == LE_EXPR)
3885          (ge (convert:st @0) { build_zero_cst (st); })
3886          (lt (convert:st @0) { build_zero_cst (st); }))))))))))
3888 (for cmp (unordered ordered unlt unle ungt unge uneq ltgt)
3889  /* If the second operand is NaN, the result is constant.  */
3890  (simplify
3891   (cmp @0 REAL_CST@1)
3892   (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
3893        && (cmp != LTGT_EXPR || ! flag_trapping_math))
3894    { constant_boolean_node (cmp == ORDERED_EXPR || cmp == LTGT_EXPR
3895                             ? false : true, type); })))
3897 /* bool_var != 0 becomes bool_var.  */
3898 (simplify
3899  (ne @0 integer_zerop)
3900  (if (TREE_CODE (TREE_TYPE (@0)) == BOOLEAN_TYPE
3901       && types_match (type, TREE_TYPE (@0)))
3902   (non_lvalue @0)))
3903 /* bool_var == 1 becomes bool_var.  */
3904 (simplify
3905  (eq @0 integer_onep)
3906  (if (TREE_CODE (TREE_TYPE (@0)) == BOOLEAN_TYPE
3907       && types_match (type, TREE_TYPE (@0)))
3908   (non_lvalue @0)))
3909 /* Do not handle
3910    bool_var == 0 becomes !bool_var or
3911    bool_var != 1 becomes !bool_var
3912    here because that only is good in assignment context as long
3913    as we require a tcc_comparison in GIMPLE_CONDs where we'd
3914    replace if (x == 0) with tem = ~x; if (tem != 0) which is
3915    clearly less optimal and which we'll transform again in forwprop.  */
3917 /* When one argument is a constant, overflow detection can be simplified.
3918    Currently restricted to single use so as not to interfere too much with
3919    ADD_OVERFLOW detection in tree-ssa-math-opts.c.
3920    A + CST CMP A  ->  A CMP' CST' */
3921 (for cmp (lt le ge gt)
3922      out (gt gt le le)
3923  (simplify
3924   (cmp:c (plus@2 @0 INTEGER_CST@1) @0)
3925   (if (TYPE_UNSIGNED (TREE_TYPE (@0))
3926        && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))
3927        && wi::to_wide (@1) != 0
3928        && single_use (@2))
3929    (with { unsigned int prec = TYPE_PRECISION (TREE_TYPE (@0)); }
3930     (out @0 { wide_int_to_tree (TREE_TYPE (@0),
3931                                 wi::max_value (prec, UNSIGNED)
3932                                 - wi::to_wide (@1)); })))))
3934 /* To detect overflow in unsigned A - B, A < B is simpler than A - B > A.
3935    However, the detection logic for SUB_OVERFLOW in tree-ssa-math-opts.c
3936    expects the long form, so we restrict the transformation for now.  */
3937 (for cmp (gt le)
3938  (simplify
3939   (cmp:c (minus@2 @0 @1) @0)
3940   (if (single_use (@2)
3941        && ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3942        && TYPE_UNSIGNED (TREE_TYPE (@0))
3943        && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
3944    (cmp @1 @0))))
3946 /* Testing for overflow is unnecessary if we already know the result.  */
3947 /* A - B > A  */
3948 (for cmp (gt le)
3949      out (ne eq)
3950  (simplify
3951   (cmp:c (realpart (IFN_SUB_OVERFLOW@2 @0 @1)) @0)
3952   (if (TYPE_UNSIGNED (TREE_TYPE (@0))
3953        && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
3954    (out (imagpart @2) { build_zero_cst (TREE_TYPE (@0)); }))))
3955 /* A + B < A  */
3956 (for cmp (lt ge)
3957      out (ne eq)
3958  (simplify
3959   (cmp:c (realpart (IFN_ADD_OVERFLOW:c@2 @0 @1)) @0)
3960   (if (TYPE_UNSIGNED (TREE_TYPE (@0))
3961        && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
3962    (out (imagpart @2) { build_zero_cst (TREE_TYPE (@0)); }))))
3964 /* For unsigned operands, -1 / B < A checks whether A * B would overflow.
3965    Simplify it to __builtin_mul_overflow (A, B, <unused>).  */
3966 (for cmp (lt ge)
3967      out (ne eq)
3968  (simplify
3969   (cmp:c (trunc_div:s integer_all_onesp @1) @0)
3970   (if (TYPE_UNSIGNED (TREE_TYPE (@0)) && !VECTOR_TYPE_P (TREE_TYPE (@0)))
3971    (with { tree t = TREE_TYPE (@0), cpx = build_complex_type (t); }
3972     (out (imagpart (IFN_MUL_OVERFLOW:cpx @0 @1)) { build_zero_cst (t); })))))
3974 /* Simplification of math builtins.  These rules must all be optimizations
3975    as well as IL simplifications.  If there is a possibility that the new
3976    form could be a pessimization, the rule should go in the canonicalization
3977    section that follows this one.
3979    Rules can generally go in this section if they satisfy one of
3980    the following:
3982    - the rule describes an identity
3984    - the rule replaces calls with something as simple as addition or
3985      multiplication
3987    - the rule contains unary calls only and simplifies the surrounding
3988      arithmetic.  (The idea here is to exclude non-unary calls in which
3989      one operand is constant and in which the call is known to be cheap
3990      when the operand has that value.)  */
3992 (if (flag_unsafe_math_optimizations)
3993  /* Simplify sqrt(x) * sqrt(x) -> x.  */
3994  (simplify
3995   (mult (SQRT_ALL@1 @0) @1)
3996   (if (!HONOR_SNANS (type))
3997    @0))
3999  (for op (plus minus)
4000   /* Simplify (A / C) +- (B / C) -> (A +- B) / C.  */
4001   (simplify
4002    (op (rdiv @0 @1)
4003        (rdiv @2 @1))
4004    (rdiv (op @0 @2) @1)))
4006  /* Simplify sqrt(x) * sqrt(y) -> sqrt(x*y).  */
4007  (for root (SQRT CBRT)
4008   (simplify
4009    (mult (root:s @0) (root:s @1))
4010     (root (mult @0 @1))))
4012  /* Simplify expN(x) * expN(y) -> expN(x+y). */
4013  (for exps (EXP EXP2 EXP10 POW10)
4014   (simplify
4015    (mult (exps:s @0) (exps:s @1))
4016     (exps (plus @0 @1))))
4018  /* Simplify a/root(b/c) into a*root(c/b).  */
4019  (for root (SQRT CBRT)
4020   (simplify
4021    (rdiv @0 (root:s (rdiv:s @1 @2)))
4022     (mult @0 (root (rdiv @2 @1)))))
4024  /* Simplify x/expN(y) into x*expN(-y).  */
4025  (for exps (EXP EXP2 EXP10 POW10)
4026   (simplify
4027    (rdiv @0 (exps:s @1))
4028     (mult @0 (exps (negate @1)))))
4030  (for logs (LOG LOG2 LOG10 LOG10)
4031       exps (EXP EXP2 EXP10 POW10)
4032   /* logN(expN(x)) -> x.  */
4033   (simplify
4034    (logs (exps @0))
4035    @0)
4036   /* expN(logN(x)) -> x.  */
4037   (simplify
4038    (exps (logs @0))
4039    @0))
4041  /* Optimize logN(func()) for various exponential functions.  We
4042     want to determine the value "x" and the power "exponent" in
4043     order to transform logN(x**exponent) into exponent*logN(x).  */
4044  (for logs (LOG  LOG   LOG   LOG2 LOG2  LOG2  LOG10 LOG10)
4045       exps (EXP2 EXP10 POW10 EXP  EXP10 POW10 EXP   EXP2)
4046   (simplify
4047    (logs (exps @0))
4048    (if (SCALAR_FLOAT_TYPE_P (type))
4049     (with {
4050       tree x;
4051       switch (exps)
4052         {
4053         CASE_CFN_EXP:
4054           /* Prepare to do logN(exp(exponent)) -> exponent*logN(e).  */
4055           x = build_real_truncate (type, dconst_e ());
4056           break;
4057         CASE_CFN_EXP2:
4058           /* Prepare to do logN(exp2(exponent)) -> exponent*logN(2).  */
4059           x = build_real (type, dconst2);
4060           break;
4061         CASE_CFN_EXP10:
4062         CASE_CFN_POW10:
4063           /* Prepare to do logN(exp10(exponent)) -> exponent*logN(10).  */
4064           {
4065             REAL_VALUE_TYPE dconst10;
4066             real_from_integer (&dconst10, VOIDmode, 10, SIGNED);
4067             x = build_real (type, dconst10);
4068           }
4069           break;
4070         default:
4071           gcc_unreachable ();
4072         }
4073       }
4074      (mult (logs { x; }) @0)))))
4076  (for logs (LOG LOG
4077             LOG2 LOG2
4078             LOG10 LOG10)
4079       exps (SQRT CBRT)
4080   (simplify
4081    (logs (exps @0))
4082    (if (SCALAR_FLOAT_TYPE_P (type))
4083     (with {
4084       tree x;
4085       switch (exps)
4086         {
4087         CASE_CFN_SQRT:
4088           /* Prepare to do logN(sqrt(x)) -> 0.5*logN(x).  */
4089           x = build_real (type, dconsthalf);
4090           break;
4091         CASE_CFN_CBRT:
4092           /* Prepare to do logN(cbrt(x)) -> (1/3)*logN(x).  */
4093           x = build_real_truncate (type, dconst_third ());
4094           break;
4095         default:
4096           gcc_unreachable ();
4097         }
4098       }
4099      (mult { x; } (logs @0))))))
4101  /* logN(pow(x,exponent)) -> exponent*logN(x).  */
4102  (for logs (LOG LOG2 LOG10)
4103       pows (POW)
4104   (simplify
4105    (logs (pows @0 @1))
4106    (mult @1 (logs @0))))
4108  /* pow(C,x) -> exp(log(C)*x) if C > 0,
4109     or if C is a positive power of 2,
4110     pow(C,x) -> exp2(log2(C)*x).  */
4111 #if GIMPLE
4112  (for pows (POW)
4113       exps (EXP)
4114       logs (LOG)
4115       exp2s (EXP2)
4116       log2s (LOG2)
4117   (simplify
4118    (pows REAL_CST@0 @1)
4119    (if (real_compare (GT_EXPR, TREE_REAL_CST_PTR (@0), &dconst0)
4120         && real_isfinite (TREE_REAL_CST_PTR (@0))
4121         /* As libmvec doesn't have a vectorized exp2, defer optimizing
4122            the use_exp2 case until after vectorization.  It seems actually
4123            beneficial for all constants to postpone this until later,
4124            because exp(log(C)*x), while faster, will have worse precision
4125            and if x folds into a constant too, that is unnecessary
4126            pessimization.  */
4127         && canonicalize_math_after_vectorization_p ())
4128     (with {
4129        const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (@0);
4130        bool use_exp2 = false;
4131        if (targetm.libc_has_function (function_c99_misc)
4132            && value->cl == rvc_normal)
4133          {
4134            REAL_VALUE_TYPE frac_rvt = *value;
4135            SET_REAL_EXP (&frac_rvt, 1);
4136            if (real_equal (&frac_rvt, &dconst1))
4137              use_exp2 = true;
4138          }
4139      }
4140      (if (!use_exp2)
4141       (if (optimize_pow_to_exp (@0, @1))
4142        (exps (mult (logs @0) @1)))
4143       (exp2s (mult (log2s @0) @1)))))))
4144 #endif
4146  /* pow(C,x)*expN(y) -> expN(logN(C)*x+y) if C > 0.  */
4147  (for pows (POW)
4148       exps (EXP EXP2 EXP10 POW10)
4149       logs (LOG LOG2 LOG10 LOG10)
4150   (simplify
4151    (mult:c (pows:s REAL_CST@0 @1) (exps:s @2))
4152    (if (real_compare (GT_EXPR, TREE_REAL_CST_PTR (@0), &dconst0)
4153         && real_isfinite (TREE_REAL_CST_PTR (@0)))
4154     (exps (plus (mult (logs @0) @1) @2)))))
4156  (for sqrts (SQRT)
4157       cbrts (CBRT)
4158       pows (POW)
4159       exps (EXP EXP2 EXP10 POW10)
4160   /* sqrt(expN(x)) -> expN(x*0.5).  */
4161   (simplify
4162    (sqrts (exps @0))
4163    (exps (mult @0 { build_real (type, dconsthalf); })))
4164   /* cbrt(expN(x)) -> expN(x/3).  */
4165   (simplify
4166    (cbrts (exps @0))
4167    (exps (mult @0 { build_real_truncate (type, dconst_third ()); })))
4168   /* pow(expN(x), y) -> expN(x*y).  */
4169   (simplify
4170    (pows (exps @0) @1)
4171    (exps (mult @0 @1))))
4173  /* tan(atan(x)) -> x.  */
4174  (for tans (TAN)
4175       atans (ATAN)
4176   (simplify
4177    (tans (atans @0))
4178    @0)))
4180 /* cabs(x+0i) or cabs(0+xi) -> abs(x).  */
4181 (simplify
4182  (CABS (complex:C @0 real_zerop@1))
4183  (abs @0))
4185 /* trunc(trunc(x)) -> trunc(x), etc.  */
4186 (for fns (TRUNC_ALL FLOOR_ALL CEIL_ALL ROUND_ALL NEARBYINT_ALL RINT_ALL)
4187  (simplify
4188   (fns (fns @0))
4189   (fns @0)))
4190 /* f(x) -> x if x is integer valued and f does nothing for such values.  */
4191 (for fns (TRUNC_ALL FLOOR_ALL CEIL_ALL ROUND_ALL NEARBYINT_ALL RINT_ALL)
4192  (simplify
4193   (fns integer_valued_real_p@0)
4194   @0))
4196 /* hypot(x,0) and hypot(0,x) -> abs(x).  */
4197 (simplify
4198  (HYPOT:c @0 real_zerop@1)
4199  (abs @0))
4201 /* pow(1,x) -> 1.  */
4202 (simplify
4203  (POW real_onep@0 @1)
4204  @0)
4206 (simplify
4207  /* copysign(x,x) -> x.  */
4208  (COPYSIGN_ALL @0 @0)
4209  @0)
4211 (simplify
4212  /* copysign(x,y) -> fabs(x) if y is nonnegative.  */
4213  (COPYSIGN_ALL @0 tree_expr_nonnegative_p@1)
4214  (abs @0))
4216 (for scale (LDEXP SCALBN SCALBLN)
4217  /* ldexp(0, x) -> 0.  */
4218  (simplify
4219   (scale real_zerop@0 @1)
4220   @0)
4221  /* ldexp(x, 0) -> x.  */
4222  (simplify
4223   (scale @0 integer_zerop@1)
4224   @0)
4225  /* ldexp(x, y) -> x if x is +-Inf or NaN.  */
4226  (simplify
4227   (scale REAL_CST@0 @1)
4228   (if (!real_isfinite (TREE_REAL_CST_PTR (@0)))
4229    @0)))
4231 /* Canonicalization of sequences of math builtins.  These rules represent
4232    IL simplifications but are not necessarily optimizations.
4234    The sincos pass is responsible for picking "optimal" implementations
4235    of math builtins, which may be more complicated and can sometimes go
4236    the other way, e.g. converting pow into a sequence of sqrts.
4237    We only want to do these canonicalizations before the pass has run.  */
4239 (if (flag_unsafe_math_optimizations && canonicalize_math_p ())
4240  /* Simplify tan(x) * cos(x) -> sin(x). */
4241  (simplify
4242   (mult:c (TAN:s @0) (COS:s @0))
4243    (SIN @0))
4245  /* Simplify x * pow(x,c) -> pow(x,c+1). */
4246  (simplify
4247   (mult:c @0 (POW:s @0 REAL_CST@1))
4248   (if (!TREE_OVERFLOW (@1))
4249    (POW @0 (plus @1 { build_one_cst (type); }))))
4251  /* Simplify sin(x) / cos(x) -> tan(x). */
4252  (simplify
4253   (rdiv (SIN:s @0) (COS:s @0))
4254    (TAN @0))
4256  /* Simplify cos(x) / sin(x) -> 1 / tan(x). */
4257  (simplify
4258   (rdiv (COS:s @0) (SIN:s @0))
4259    (rdiv { build_one_cst (type); } (TAN @0)))
4261  /* Simplify sin(x) / tan(x) -> cos(x). */
4262  (simplify
4263   (rdiv (SIN:s @0) (TAN:s @0))
4264   (if (! HONOR_NANS (@0)
4265        && ! HONOR_INFINITIES (@0))
4266    (COS @0)))
4268  /* Simplify tan(x) / sin(x) -> 1.0 / cos(x). */
4269  (simplify
4270   (rdiv (TAN:s @0) (SIN:s @0))
4271   (if (! HONOR_NANS (@0)
4272        && ! HONOR_INFINITIES (@0))
4273    (rdiv { build_one_cst (type); } (COS @0))))
4275  /* Simplify pow(x,y) * pow(x,z) -> pow(x,y+z). */
4276  (simplify
4277   (mult (POW:s @0 @1) (POW:s @0 @2))
4278    (POW @0 (plus @1 @2)))
4280  /* Simplify pow(x,y) * pow(z,y) -> pow(x*z,y). */
4281  (simplify
4282   (mult (POW:s @0 @1) (POW:s @2 @1))
4283    (POW (mult @0 @2) @1))
4285  /* Simplify powi(x,y) * powi(z,y) -> powi(x*z,y). */
4286  (simplify
4287   (mult (POWI:s @0 @1) (POWI:s @2 @1))
4288    (POWI (mult @0 @2) @1))
4290  /* Simplify pow(x,c) / x -> pow(x,c-1). */
4291  (simplify
4292   (rdiv (POW:s @0 REAL_CST@1) @0)
4293   (if (!TREE_OVERFLOW (@1))
4294    (POW @0 (minus @1 { build_one_cst (type); }))))
4296  /* Simplify x / pow (y,z) -> x * pow(y,-z). */
4297  (simplify
4298   (rdiv @0 (POW:s @1 @2))
4299    (mult @0 (POW @1 (negate @2))))
4301  (for sqrts (SQRT)
4302       cbrts (CBRT)
4303       pows (POW)
4304   /* sqrt(sqrt(x)) -> pow(x,1/4).  */
4305   (simplify
4306    (sqrts (sqrts @0))
4307    (pows @0 { build_real (type, dconst_quarter ()); }))
4308   /* sqrt(cbrt(x)) -> pow(x,1/6).  */
4309   (simplify
4310    (sqrts (cbrts @0))
4311    (pows @0 { build_real_truncate (type, dconst_sixth ()); }))
4312   /* cbrt(sqrt(x)) -> pow(x,1/6).  */
4313   (simplify
4314    (cbrts (sqrts @0))
4315    (pows @0 { build_real_truncate (type, dconst_sixth ()); }))
4316   /* cbrt(cbrt(x)) -> pow(x,1/9), iff x is nonnegative.  */
4317   (simplify
4318    (cbrts (cbrts tree_expr_nonnegative_p@0))
4319    (pows @0 { build_real_truncate (type, dconst_ninth ()); }))
4320   /* sqrt(pow(x,y)) -> pow(|x|,y*0.5).  */
4321   (simplify
4322    (sqrts (pows @0 @1))
4323    (pows (abs @0) (mult @1 { build_real (type, dconsthalf); })))
4324   /* cbrt(pow(x,y)) -> pow(x,y/3), iff x is nonnegative.  */
4325   (simplify
4326    (cbrts (pows tree_expr_nonnegative_p@0 @1))
4327    (pows @0 (mult @1 { build_real_truncate (type, dconst_third ()); })))
4328   /* pow(sqrt(x),y) -> pow(x,y*0.5).  */
4329   (simplify
4330    (pows (sqrts @0) @1)
4331    (pows @0 (mult @1 { build_real (type, dconsthalf); })))
4332   /* pow(cbrt(x),y) -> pow(x,y/3) iff x is nonnegative.  */
4333   (simplify
4334    (pows (cbrts tree_expr_nonnegative_p@0) @1)
4335    (pows @0 (mult @1 { build_real_truncate (type, dconst_third ()); })))
4336   /* pow(pow(x,y),z) -> pow(x,y*z) iff x is nonnegative.  */
4337   (simplify
4338    (pows (pows tree_expr_nonnegative_p@0 @1) @2)
4339    (pows @0 (mult @1 @2))))
4341  /* cabs(x+xi) -> fabs(x)*sqrt(2).  */
4342  (simplify
4343   (CABS (complex @0 @0))
4344   (mult (abs @0) { build_real_truncate (type, dconst_sqrt2 ()); }))
4346  /* hypot(x,x) -> fabs(x)*sqrt(2).  */
4347  (simplify
4348   (HYPOT @0 @0)
4349   (mult (abs @0) { build_real_truncate (type, dconst_sqrt2 ()); }))
4351  /* cexp(x+yi) -> exp(x)*cexpi(y).  */
4352  (for cexps (CEXP)
4353       exps (EXP)
4354       cexpis (CEXPI)
4355   (simplify
4356    (cexps compositional_complex@0)
4357    (if (targetm.libc_has_function (function_c99_math_complex))
4358     (complex
4359      (mult (exps@1 (realpart @0)) (realpart (cexpis:type@2 (imagpart @0))))
4360      (mult @1 (imagpart @2)))))))
4362 (if (canonicalize_math_p ())
4363  /* floor(x) -> trunc(x) if x is nonnegative.  */
4364  (for floors (FLOOR_ALL)
4365       truncs (TRUNC_ALL)
4366   (simplify
4367    (floors tree_expr_nonnegative_p@0)
4368    (truncs @0))))
4370 (match double_value_p
4371  @0
4372  (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == double_type_node)))
4373 (for froms (BUILT_IN_TRUNCL
4374             BUILT_IN_FLOORL
4375             BUILT_IN_CEILL
4376             BUILT_IN_ROUNDL
4377             BUILT_IN_NEARBYINTL
4378             BUILT_IN_RINTL)
4379      tos (BUILT_IN_TRUNC
4380           BUILT_IN_FLOOR
4381           BUILT_IN_CEIL
4382           BUILT_IN_ROUND
4383           BUILT_IN_NEARBYINT
4384           BUILT_IN_RINT)
4385  /* truncl(extend(x)) -> extend(trunc(x)), etc., if x is a double.  */
4386  (if (optimize && canonicalize_math_p ())
4387   (simplify
4388    (froms (convert double_value_p@0))
4389    (convert (tos @0)))))
4391 (match float_value_p
4392  @0
4393  (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == float_type_node)))
4394 (for froms (BUILT_IN_TRUNCL BUILT_IN_TRUNC
4395             BUILT_IN_FLOORL BUILT_IN_FLOOR
4396             BUILT_IN_CEILL BUILT_IN_CEIL
4397             BUILT_IN_ROUNDL BUILT_IN_ROUND
4398             BUILT_IN_NEARBYINTL BUILT_IN_NEARBYINT
4399             BUILT_IN_RINTL BUILT_IN_RINT)
4400      tos (BUILT_IN_TRUNCF BUILT_IN_TRUNCF
4401           BUILT_IN_FLOORF BUILT_IN_FLOORF
4402           BUILT_IN_CEILF BUILT_IN_CEILF
4403           BUILT_IN_ROUNDF BUILT_IN_ROUNDF
4404           BUILT_IN_NEARBYINTF BUILT_IN_NEARBYINTF
4405           BUILT_IN_RINTF BUILT_IN_RINTF)
4406  /* truncl(extend(x)) and trunc(extend(x)) -> extend(truncf(x)), etc.,
4407     if x is a float.  */
4408  (if (optimize && canonicalize_math_p ()
4409       && targetm.libc_has_function (function_c99_misc))
4410   (simplify
4411    (froms (convert float_value_p@0))
4412    (convert (tos @0)))))
4414 (for froms (XFLOORL XCEILL XROUNDL XRINTL)
4415      tos (XFLOOR XCEIL XROUND XRINT)
4416  /* llfloorl(extend(x)) -> llfloor(x), etc., if x is a double.  */
4417  (if (optimize && canonicalize_math_p ())
4418   (simplify
4419    (froms (convert double_value_p@0))
4420    (tos @0))))
4422 (for froms (XFLOORL XCEILL XROUNDL XRINTL
4423             XFLOOR XCEIL XROUND XRINT)
4424      tos (XFLOORF XCEILF XROUNDF XRINTF)
4425  /* llfloorl(extend(x)) and llfloor(extend(x)) -> llfloorf(x), etc.,
4426     if x is a float.  */
4427  (if (optimize && canonicalize_math_p ())
4428   (simplify
4429    (froms (convert float_value_p@0))
4430    (tos @0))))
4432 (if (canonicalize_math_p ())
4433  /* xfloor(x) -> fix_trunc(x) if x is nonnegative.  */
4434  (for floors (IFLOOR LFLOOR LLFLOOR)
4435   (simplify
4436    (floors tree_expr_nonnegative_p@0)
4437    (fix_trunc @0))))
4439 (if (canonicalize_math_p ())
4440  /* xfloor(x) -> fix_trunc(x), etc., if x is integer valued.  */
4441  (for fns (IFLOOR LFLOOR LLFLOOR
4442            ICEIL LCEIL LLCEIL
4443            IROUND LROUND LLROUND)
4444   (simplify
4445    (fns integer_valued_real_p@0)
4446    (fix_trunc @0)))
4447  (if (!flag_errno_math)
4448   /* xrint(x) -> fix_trunc(x), etc., if x is integer valued.  */
4449   (for rints (IRINT LRINT LLRINT)
4450    (simplify
4451     (rints integer_valued_real_p@0)
4452     (fix_trunc @0)))))
4454 (if (canonicalize_math_p ())
4455  (for ifn (IFLOOR ICEIL IROUND IRINT)
4456       lfn (LFLOOR LCEIL LROUND LRINT)
4457       llfn (LLFLOOR LLCEIL LLROUND LLRINT)
4458   /* Canonicalize iround (x) to lround (x) on ILP32 targets where
4459      sizeof (int) == sizeof (long).  */
4460   (if (TYPE_PRECISION (integer_type_node)
4461        == TYPE_PRECISION (long_integer_type_node))
4462    (simplify
4463     (ifn @0)
4464     (lfn:long_integer_type_node @0)))
4465   /* Canonicalize llround (x) to lround (x) on LP64 targets where
4466      sizeof (long long) == sizeof (long).  */
4467   (if (TYPE_PRECISION (long_long_integer_type_node)
4468        == TYPE_PRECISION (long_integer_type_node))
4469    (simplify
4470     (llfn @0)
4471     (lfn:long_integer_type_node @0)))))
4473 /* cproj(x) -> x if we're ignoring infinities.  */
4474 (simplify
4475  (CPROJ @0)
4476  (if (!HONOR_INFINITIES (type))
4477    @0))
4479 /* If the real part is inf and the imag part is known to be
4480    nonnegative, return (inf + 0i).  */
4481 (simplify
4482  (CPROJ (complex REAL_CST@0 tree_expr_nonnegative_p@1))
4483  (if (real_isinf (TREE_REAL_CST_PTR (@0)))
4484   { build_complex_inf (type, false); }))
4486 /* If the imag part is inf, return (inf+I*copysign(0,imag)).  */
4487 (simplify
4488  (CPROJ (complex @0 REAL_CST@1))
4489  (if (real_isinf (TREE_REAL_CST_PTR (@1)))
4490   { build_complex_inf (type, TREE_REAL_CST_PTR (@1)->sign); }))
4492 (for pows (POW)
4493      sqrts (SQRT)
4494      cbrts (CBRT)
4495  (simplify
4496   (pows @0 REAL_CST@1)
4497   (with {
4498     const REAL_VALUE_TYPE *value = TREE_REAL_CST_PTR (@1);
4499     REAL_VALUE_TYPE tmp;
4500    }
4501    (switch
4502     /* pow(x,0) -> 1.  */
4503     (if (real_equal (value, &dconst0))
4504      { build_real (type, dconst1); })
4505     /* pow(x,1) -> x.  */
4506     (if (real_equal (value, &dconst1))
4507      @0)
4508     /* pow(x,-1) -> 1/x.  */
4509     (if (real_equal (value, &dconstm1))
4510      (rdiv { build_real (type, dconst1); } @0))
4511     /* pow(x,0.5) -> sqrt(x).  */
4512     (if (flag_unsafe_math_optimizations
4513          && canonicalize_math_p ()
4514          && real_equal (value, &dconsthalf))
4515      (sqrts @0))
4516     /* pow(x,1/3) -> cbrt(x).  */
4517     (if (flag_unsafe_math_optimizations
4518          && canonicalize_math_p ()
4519          && (tmp = real_value_truncate (TYPE_MODE (type), dconst_third ()),
4520              real_equal (value, &tmp)))
4521      (cbrts @0))))))
4523 /* powi(1,x) -> 1.  */
4524 (simplify
4525  (POWI real_onep@0 @1)
4526  @0)
4528 (simplify
4529  (POWI @0 INTEGER_CST@1)
4530  (switch
4531   /* powi(x,0) -> 1.  */
4532   (if (wi::to_wide (@1) == 0)
4533    { build_real (type, dconst1); })
4534   /* powi(x,1) -> x.  */
4535   (if (wi::to_wide (@1) == 1)
4536    @0)
4537   /* powi(x,-1) -> 1/x.  */
4538   (if (wi::to_wide (@1) == -1)
4539    (rdiv { build_real (type, dconst1); } @0))))
4541 /* Narrowing of arithmetic and logical operations. 
4543    These are conceptually similar to the transformations performed for
4544    the C/C++ front-ends by shorten_binary_op and shorten_compare.  Long
4545    term we want to move all that code out of the front-ends into here.  */
4547 /* If we have a narrowing conversion of an arithmetic operation where
4548    both operands are widening conversions from the same type as the outer
4549    narrowing conversion.  Then convert the innermost operands to a suitable
4550    unsigned type (to avoid introducing undefined behavior), perform the
4551    operation and convert the result to the desired type.  */
4552 (for op (plus minus)
4553   (simplify
4554     (convert (op:s (convert@2 @0) (convert?@3 @1)))
4555     (if (INTEGRAL_TYPE_P (type)
4556          /* We check for type compatibility between @0 and @1 below,
4557             so there's no need to check that @1/@3 are integral types.  */
4558          && INTEGRAL_TYPE_P (TREE_TYPE (@0))
4559          && INTEGRAL_TYPE_P (TREE_TYPE (@2))
4560          /* The precision of the type of each operand must match the
4561             precision of the mode of each operand, similarly for the
4562             result.  */
4563          && type_has_mode_precision_p (TREE_TYPE (@0))
4564          && type_has_mode_precision_p (TREE_TYPE (@1))
4565          && type_has_mode_precision_p (type)
4566          /* The inner conversion must be a widening conversion.  */
4567          && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
4568          && types_match (@0, type)
4569          && (types_match (@0, @1)
4570              /* Or the second operand is const integer or converted const
4571                 integer from valueize.  */
4572              || TREE_CODE (@1) == INTEGER_CST))
4573       (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
4574         (op @0 (convert @1))
4575         (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
4576          (convert (op (convert:utype @0)
4577                       (convert:utype @1))))))))
4579 /* This is another case of narrowing, specifically when there's an outer
4580    BIT_AND_EXPR which masks off bits outside the type of the innermost
4581    operands.   Like the previous case we have to convert the operands
4582    to unsigned types to avoid introducing undefined behavior for the
4583    arithmetic operation.  */
4584 (for op (minus plus)
4585  (simplify
4586   (bit_and (op:s (convert@2 @0) (convert@3 @1)) INTEGER_CST@4)
4587   (if (INTEGRAL_TYPE_P (type)
4588        /* We check for type compatibility between @0 and @1 below,
4589           so there's no need to check that @1/@3 are integral types.  */
4590        && INTEGRAL_TYPE_P (TREE_TYPE (@0))
4591        && INTEGRAL_TYPE_P (TREE_TYPE (@2))
4592        /* The precision of the type of each operand must match the
4593           precision of the mode of each operand, similarly for the
4594           result.  */
4595        && type_has_mode_precision_p (TREE_TYPE (@0))
4596        && type_has_mode_precision_p (TREE_TYPE (@1))
4597        && type_has_mode_precision_p (type)
4598        /* The inner conversion must be a widening conversion.  */
4599        && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
4600        && types_match (@0, @1)
4601        && (tree_int_cst_min_precision (@4, TYPE_SIGN (TREE_TYPE (@0)))
4602            <= TYPE_PRECISION (TREE_TYPE (@0)))
4603        && (wi::to_wide (@4)
4604            & wi::mask (TYPE_PRECISION (TREE_TYPE (@0)),
4605                        true, TYPE_PRECISION (type))) == 0)
4606    (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
4607     (with { tree ntype = TREE_TYPE (@0); }
4608      (convert (bit_and (op @0 @1) (convert:ntype @4))))
4609     (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
4610      (convert (bit_and (op (convert:utype @0) (convert:utype @1))
4611                (convert:utype @4))))))))
4613 /* Transform (@0 < @1 and @0 < @2) to use min, 
4614    (@0 > @1 and @0 > @2) to use max */
4615 (for logic (bit_and bit_and bit_and bit_and bit_ior bit_ior bit_ior bit_ior)
4616      op    (lt      le      gt      ge      lt      le      gt      ge     )
4617      ext   (min     min     max     max     max     max     min     min    )
4618  (simplify
4619   (logic (op:cs @0 @1) (op:cs @0 @2))
4620   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
4621        && TREE_CODE (@0) != INTEGER_CST)
4622    (op @0 (ext @1 @2)))))
4624 (simplify
4625  /* signbit(x) -> 0 if x is nonnegative.  */
4626  (SIGNBIT tree_expr_nonnegative_p@0)
4627  { integer_zero_node; })
4629 (simplify
4630  /* signbit(x) -> x<0 if x doesn't have signed zeros.  */
4631  (SIGNBIT @0)
4632  (if (!HONOR_SIGNED_ZEROS (@0))
4633   (convert (lt @0 { build_real (TREE_TYPE (@0), dconst0); }))))
4635 /* Transform comparisons of the form X +- C1 CMP C2 to X CMP C2 -+ C1.  */
4636 (for cmp (eq ne)
4637  (for op (plus minus)
4638       rop (minus plus)
4639   (simplify
4640    (cmp (op@3 @0 INTEGER_CST@1) INTEGER_CST@2)
4641    (if (!TREE_OVERFLOW (@1) && !TREE_OVERFLOW (@2)
4642         && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0))
4643         && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@0))
4644         && !TYPE_SATURATING (TREE_TYPE (@0)))
4645     (with { tree res = int_const_binop (rop, @2, @1); }
4646      (if (TREE_OVERFLOW (res)
4647           && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
4648       { constant_boolean_node (cmp == NE_EXPR, type); }
4649       (if (single_use (@3))
4650        (cmp @0 { TREE_OVERFLOW (res)
4651                  ? drop_tree_overflow (res) : res; }))))))))
4652 (for cmp (lt le gt ge)
4653  (for op (plus minus)
4654       rop (minus plus)
4655   (simplify
4656    (cmp (op@3 @0 INTEGER_CST@1) INTEGER_CST@2)
4657    (if (!TREE_OVERFLOW (@1) && !TREE_OVERFLOW (@2)
4658         && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
4659     (with { tree res = int_const_binop (rop, @2, @1); }
4660      (if (TREE_OVERFLOW (res))
4661       {
4662         fold_overflow_warning (("assuming signed overflow does not occur "
4663                                 "when simplifying conditional to constant"),
4664                                WARN_STRICT_OVERFLOW_CONDITIONAL);
4665         bool less = cmp == LE_EXPR || cmp == LT_EXPR;
4666         /* wi::ges_p (@2, 0) should be sufficient for a signed type.  */
4667         bool ovf_high = wi::lt_p (wi::to_wide (@1), 0,
4668                                   TYPE_SIGN (TREE_TYPE (@1)))
4669                         != (op == MINUS_EXPR);
4670         constant_boolean_node (less == ovf_high, type);
4671       }
4672       (if (single_use (@3))
4673        (with
4674         {
4675           fold_overflow_warning (("assuming signed overflow does not occur "
4676                                   "when changing X +- C1 cmp C2 to "
4677                                   "X cmp C2 -+ C1"),
4678                                  WARN_STRICT_OVERFLOW_COMPARISON);
4679         }
4680         (cmp @0 { res; })))))))))
4682 /* Canonicalizations of BIT_FIELD_REFs.  */
4684 (simplify
4685  (BIT_FIELD_REF (BIT_FIELD_REF @0 @1 @2) @3 @4)
4686  (BIT_FIELD_REF @0 @3 { const_binop (PLUS_EXPR, bitsizetype, @2, @4); }))
4688 (simplify
4689  (BIT_FIELD_REF (view_convert @0) @1 @2)
4690  (BIT_FIELD_REF @0 @1 @2))
4692 (simplify
4693  (BIT_FIELD_REF @0 @1 integer_zerop)
4694  (if (tree_int_cst_equal (@1, TYPE_SIZE (TREE_TYPE (@0))))
4695   (view_convert @0)))
4697 (simplify
4698  (BIT_FIELD_REF @0 @1 @2)
4699  (switch
4700   (if (TREE_CODE (TREE_TYPE (@0)) == COMPLEX_TYPE
4701        && tree_int_cst_equal (@1, TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))))
4702    (switch
4703     (if (integer_zerop (@2))
4704      (view_convert (realpart @0)))
4705     (if (tree_int_cst_equal (@2, TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))))
4706      (view_convert (imagpart @0)))))
4707   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
4708        && INTEGRAL_TYPE_P (type)
4709        /* On GIMPLE this should only apply to register arguments.  */
4710        && (! GIMPLE || is_gimple_reg (@0))
4711        /* A bit-field-ref that referenced the full argument can be stripped.  */
4712        && ((compare_tree_int (@1, TYPE_PRECISION (TREE_TYPE (@0))) == 0
4713             && integer_zerop (@2))
4714            /* Low-parts can be reduced to integral conversions.
4715               ???  The following doesn't work for PDP endian.  */
4716            || (BYTES_BIG_ENDIAN == WORDS_BIG_ENDIAN
4717                /* Don't even think about BITS_BIG_ENDIAN.  */
4718                && TYPE_PRECISION (TREE_TYPE (@0)) % BITS_PER_UNIT == 0
4719                && TYPE_PRECISION (type) % BITS_PER_UNIT == 0
4720                && compare_tree_int (@2, (BYTES_BIG_ENDIAN
4721                                          ? (TYPE_PRECISION (TREE_TYPE (@0))
4722                                             - TYPE_PRECISION (type))
4723                                          : 0)) == 0)))
4724    (convert @0))))
4726 /* Simplify vector extracts.  */
4728 (simplify
4729  (BIT_FIELD_REF CONSTRUCTOR@0 @1 @2)
4730  (if (VECTOR_TYPE_P (TREE_TYPE (@0))
4731       && (types_match (type, TREE_TYPE (TREE_TYPE (@0)))
4732           || (VECTOR_TYPE_P (type)
4733               && types_match (TREE_TYPE (type), TREE_TYPE (TREE_TYPE (@0))))))
4734   (with
4735    {
4736      tree ctor = (TREE_CODE (@0) == SSA_NAME
4737                   ? gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0)) : @0);
4738      tree eltype = TREE_TYPE (TREE_TYPE (ctor));
4739      unsigned HOST_WIDE_INT width = tree_to_uhwi (TYPE_SIZE (eltype));
4740      unsigned HOST_WIDE_INT n = tree_to_uhwi (@1);
4741      unsigned HOST_WIDE_INT idx = tree_to_uhwi (@2);
4742    }
4743    (if (n != 0
4744         && (idx % width) == 0
4745         && (n % width) == 0
4746         && known_le ((idx + n) / width,
4747                      TYPE_VECTOR_SUBPARTS (TREE_TYPE (ctor))))
4748     (with
4749      {
4750        idx = idx / width;
4751        n = n / width;
4752        /* Constructor elements can be subvectors.  */
4753        poly_uint64 k = 1;
4754        if (CONSTRUCTOR_NELTS (ctor) != 0)
4755          {
4756            tree cons_elem = TREE_TYPE (CONSTRUCTOR_ELT (ctor, 0)->value);
4757            if (TREE_CODE (cons_elem) == VECTOR_TYPE)
4758              k = TYPE_VECTOR_SUBPARTS (cons_elem);
4759          }
4760        unsigned HOST_WIDE_INT elt, count, const_k;
4761      }
4762      (switch
4763       /* We keep an exact subset of the constructor elements.  */
4764       (if (multiple_p (idx, k, &elt) && multiple_p (n, k, &count))
4765        (if (CONSTRUCTOR_NELTS (ctor) == 0)
4766         { build_constructor (type, NULL); }
4767         (if (count == 1)
4768          (if (elt < CONSTRUCTOR_NELTS (ctor))
4769           (view_convert { CONSTRUCTOR_ELT (ctor, elt)->value; })
4770           { build_zero_cst (type); })
4771          {
4772            vec<constructor_elt, va_gc> *vals;
4773            vec_alloc (vals, count);
4774            for (unsigned i = 0;
4775                 i < count && elt + i < CONSTRUCTOR_NELTS (ctor); ++i)
4776              CONSTRUCTOR_APPEND_ELT (vals, NULL_TREE,
4777                                      CONSTRUCTOR_ELT (ctor, elt + i)->value);
4778            build_constructor (type, vals);
4779          })))
4780       /* The bitfield references a single constructor element.  */
4781       (if (k.is_constant (&const_k)
4782            && idx + n <= (idx / const_k + 1) * const_k)
4783        (switch
4784         (if (CONSTRUCTOR_NELTS (ctor) <= idx / const_k)
4785          { build_zero_cst (type); })
4786         (if (n == const_k)
4787          (view_convert { CONSTRUCTOR_ELT (ctor, idx / const_k)->value; }))
4788         (BIT_FIELD_REF { CONSTRUCTOR_ELT (ctor, idx / const_k)->value; }
4789                        @1 { bitsize_int ((idx % const_k) * width); })))))))))
4791 /* Simplify a bit extraction from a bit insertion for the cases with
4792    the inserted element fully covering the extraction or the insertion
4793    not touching the extraction.  */
4794 (simplify
4795  (BIT_FIELD_REF (bit_insert @0 @1 @ipos) @rsize @rpos)
4796  (with
4797   {
4798     unsigned HOST_WIDE_INT isize;
4799     if (INTEGRAL_TYPE_P (TREE_TYPE (@1)))
4800       isize = TYPE_PRECISION (TREE_TYPE (@1));
4801     else
4802       isize = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (@1)));
4803   }
4804   (switch
4805    (if (wi::leu_p (wi::to_wide (@ipos), wi::to_wide (@rpos))
4806         && wi::leu_p (wi::to_wide (@rpos) + wi::to_wide (@rsize),
4807                       wi::to_wide (@ipos) + isize))
4808     (BIT_FIELD_REF @1 @rsize { wide_int_to_tree (bitsizetype,
4809                                                  wi::to_wide (@rpos)
4810                                                  - wi::to_wide (@ipos)); }))
4811    (if (wi::geu_p (wi::to_wide (@ipos),
4812                    wi::to_wide (@rpos) + wi::to_wide (@rsize))
4813         || wi::geu_p (wi::to_wide (@rpos),
4814                       wi::to_wide (@ipos) + isize))
4815     (BIT_FIELD_REF @0 @rsize @rpos)))))
4817 (if (canonicalize_math_after_vectorization_p ())
4818  (for fmas (FMA)
4819   (simplify
4820    (fmas:c (negate @0) @1 @2)
4821    (IFN_FNMA @0 @1 @2))
4822   (simplify
4823    (fmas @0 @1 (negate @2))
4824    (IFN_FMS @0 @1 @2))
4825   (simplify
4826    (fmas:c (negate @0) @1 (negate @2))
4827    (IFN_FNMS @0 @1 @2))
4828   (simplify
4829    (negate (fmas@3 @0 @1 @2))
4830    (if (single_use (@3))
4831     (IFN_FNMS @0 @1 @2))))
4833  (simplify
4834   (IFN_FMS:c (negate @0) @1 @2)
4835   (IFN_FNMS @0 @1 @2))
4836  (simplify
4837   (IFN_FMS @0 @1 (negate @2))
4838   (IFN_FMA @0 @1 @2))
4839  (simplify
4840   (IFN_FMS:c (negate @0) @1 (negate @2))
4841   (IFN_FNMA @0 @1 @2))
4842  (simplify
4843   (negate (IFN_FMS@3 @0 @1 @2))
4844    (if (single_use (@3))
4845     (IFN_FNMA @0 @1 @2)))
4847  (simplify
4848   (IFN_FNMA:c (negate @0) @1 @2)
4849   (IFN_FMA @0 @1 @2))
4850  (simplify
4851   (IFN_FNMA @0 @1 (negate @2))
4852   (IFN_FNMS @0 @1 @2))
4853  (simplify
4854   (IFN_FNMA:c (negate @0) @1 (negate @2))
4855   (IFN_FMS @0 @1 @2))
4856  (simplify
4857   (negate (IFN_FNMA@3 @0 @1 @2))
4858   (if (single_use (@3))
4859    (IFN_FMS @0 @1 @2)))
4861  (simplify
4862   (IFN_FNMS:c (negate @0) @1 @2)
4863   (IFN_FMS @0 @1 @2))
4864  (simplify
4865   (IFN_FNMS @0 @1 (negate @2))
4866   (IFN_FNMA @0 @1 @2))
4867  (simplify
4868   (IFN_FNMS:c (negate @0) @1 (negate @2))
4869   (IFN_FMA @0 @1 @2))
4870  (simplify
4871   (negate (IFN_FNMS@3 @0 @1 @2))
4872   (if (single_use (@3))
4873    (IFN_FMA @0 @1 @2))))
4875 /* POPCOUNT simplifications.  */
4876 (for popcount (BUILT_IN_POPCOUNT BUILT_IN_POPCOUNTL BUILT_IN_POPCOUNTLL
4877                BUILT_IN_POPCOUNTIMAX)
4878   /* popcount(X&1) is nop_expr(X&1).  */
4879   (simplify
4880     (popcount @0)
4881     (if (tree_nonzero_bits (@0) == 1)
4882       (convert @0)))
4883   /* popcount(X) + popcount(Y) is popcount(X|Y) when X&Y must be zero.  */
4884   (simplify
4885     (plus (popcount:s @0) (popcount:s @1))
4886     (if (wi::bit_and (tree_nonzero_bits (@0), tree_nonzero_bits (@1)) == 0)
4887       (popcount (bit_ior @0 @1))))
4888   /* popcount(X) == 0 is X == 0, and related (in)equalities.  */
4889   (for cmp (le eq ne gt)
4890        rep (eq eq ne ne)
4891     (simplify
4892       (cmp (popcount @0) integer_zerop)
4893       (rep @0 { build_zero_cst (TREE_TYPE (@0)); }))))
4895 /* Simplify:
4897      a = a1 op a2
4898      r = c ? a : b;
4900    to:
4902      r = c ? a1 op a2 : b;
4904    if the target can do it in one go.  This makes the operation conditional
4905    on c, so could drop potentially-trapping arithmetic, but that's a valid
4906    simplification if the result of the operation isn't needed.  */
4907 (for uncond_op (UNCOND_BINARY)
4908      cond_op (COND_BINARY)
4909  (simplify
4910   (vec_cond @0 (view_convert? (uncond_op@4 @1 @2)) @3)
4911   (with { tree op_type = TREE_TYPE (@4); }
4912    (if (element_precision (type) == element_precision (op_type))
4913     (view_convert (cond_op @0 @1 @2 (view_convert:op_type @3))))))
4914  (simplify
4915   (vec_cond @0 @1 (view_convert? (uncond_op@4 @2 @3)))
4916   (with { tree op_type = TREE_TYPE (@4); }
4917    (if (element_precision (type) == element_precision (op_type))
4918     (view_convert (cond_op (bit_not @0) @2 @3 (view_convert:op_type @1)))))))
4920 /* Same for ternary operations.  */
4921 (for uncond_op (UNCOND_TERNARY)
4922      cond_op (COND_TERNARY)
4923  (simplify
4924   (vec_cond @0 (view_convert? (uncond_op@5 @1 @2 @3)) @4)
4925   (with { tree op_type = TREE_TYPE (@5); }
4926    (if (element_precision (type) == element_precision (op_type))
4927     (view_convert (cond_op @0 @1 @2 @3 (view_convert:op_type @4))))))
4928  (simplify
4929   (vec_cond @0 @1 (view_convert? (uncond_op@5 @2 @3 @4)))
4930   (with { tree op_type = TREE_TYPE (@5); }
4931    (if (element_precision (type) == element_precision (op_type))
4932     (view_convert (cond_op (bit_not @0) @2 @3 @4
4933                   (view_convert:op_type @1)))))))
4935 /* Detect cases in which a VEC_COND_EXPR effectively replaces the
4936    "else" value of an IFN_COND_*.  */
4937 (for cond_op (COND_BINARY)
4938  (simplify
4939   (vec_cond @0 (view_convert? (cond_op @0 @1 @2 @3)) @4)
4940   (with { tree op_type = TREE_TYPE (@3); }
4941    (if (element_precision (type) == element_precision (op_type))
4942     (view_convert (cond_op @0 @1 @2 (view_convert:op_type @4))))))
4943  (simplify
4944   (vec_cond @0 @1 (view_convert? (cond_op @2 @3 @4 @5)))
4945   (with { tree op_type = TREE_TYPE (@5); }
4946    (if (inverse_conditions_p (@0, @2)
4947         && element_precision (type) == element_precision (op_type))
4948     (view_convert (cond_op @2 @3 @4 (view_convert:op_type @1)))))))
4950 /* Same for ternary operations.  */
4951 (for cond_op (COND_TERNARY)
4952  (simplify
4953   (vec_cond @0 (view_convert? (cond_op @0 @1 @2 @3 @4)) @5)
4954   (with { tree op_type = TREE_TYPE (@4); }
4955    (if (element_precision (type) == element_precision (op_type))
4956     (view_convert (cond_op @0 @1 @2 @3 (view_convert:op_type @5))))))
4957  (simplify
4958   (vec_cond @0 @1 (view_convert? (cond_op @2 @3 @4 @5 @6)))
4959   (with { tree op_type = TREE_TYPE (@6); }
4960    (if (inverse_conditions_p (@0, @2)
4961         && element_precision (type) == element_precision (op_type))
4962     (view_convert (cond_op @2 @3 @4 @5 (view_convert:op_type @1)))))))
4964 /* For pointers @0 and @2 and nonnegative constant offset @1, look for
4965    expressions like:
4967    A: (@0 + @1 < @2) | (@2 + @1 < @0)
4968    B: (@0 + @1 <= @2) | (@2 + @1 <= @0)
4970    If pointers are known not to wrap, B checks whether @1 bytes starting
4971    at @0 and @2 do not overlap, while A tests the same thing for @1 + 1
4972    bytes.  A is more efficiently tested as:
4974    A: (sizetype) (@0 + @1 - @2) > @1 * 2
4976    The equivalent expression for B is given by replacing @1 with @1 - 1:
4978    B: (sizetype) (@0 + (@1 - 1) - @2) > (@1 - 1) * 2
4980    @0 and @2 can be swapped in both expressions without changing the result.
4982    The folds rely on sizetype's being unsigned (which is always true)
4983    and on its being the same width as the pointer (which we have to check).
4985    The fold replaces two pointer_plus expressions, two comparisons and
4986    an IOR with a pointer_plus, a pointer_diff, and a comparison, so in
4987    the best case it's a saving of two operations.  The A fold retains one
4988    of the original pointer_pluses, so is a win even if both pointer_pluses
4989    are used elsewhere.  The B fold is a wash if both pointer_pluses are
4990    used elsewhere, since all we end up doing is replacing a comparison with
4991    a pointer_plus.  We do still apply the fold under those circumstances
4992    though, in case applying it to other conditions eventually makes one of the
4993    pointer_pluses dead.  */
4994 (for ior (truth_orif truth_or bit_ior)
4995  (for cmp (le lt)
4996   (simplify
4997    (ior (cmp:cs (pointer_plus@3 @0 INTEGER_CST@1) @2)
4998         (cmp:cs (pointer_plus@4 @2 @1) @0))
4999    (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
5000         && TYPE_OVERFLOW_WRAPS (sizetype)
5001         && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (sizetype))
5002     /* Calculate the rhs constant.  */
5003     (with { offset_int off = wi::to_offset (@1) - (cmp == LE_EXPR ? 1 : 0);
5004             offset_int rhs = off * 2; }
5005      /* Always fails for negative values.  */
5006      (if (wi::min_precision (rhs, UNSIGNED) <= TYPE_PRECISION (sizetype))
5007       /* Since the order of @0 and @2 doesn't matter, let tree_swap_operands_p
5008          pick a canonical order.  This increases the chances of using the
5009          same pointer_plus in multiple checks.  */
5010       (with { bool swap_p = tree_swap_operands_p (@0, @2);
5011               tree rhs_tree = wide_int_to_tree (sizetype, rhs); }
5012        (if (cmp == LT_EXPR)
5013         (gt (convert:sizetype
5014              (pointer_diff:ssizetype { swap_p ? @4 : @3; }
5015                                      { swap_p ? @0 : @2; }))
5016             { rhs_tree; })
5017         (gt (convert:sizetype
5018              (pointer_diff:ssizetype
5019               (pointer_plus { swap_p ? @2 : @0; }
5020                             { wide_int_to_tree (sizetype, off); })
5021               { swap_p ? @0 : @2; }))
5022             { rhs_tree; })))))))))