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