fwprop: Fix single_use_p calculation
[official-gcc.git] / gcc / match.pd
blob036f92fa959e8a95ab6507ca57459fbc15857a80
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-2021 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    initializer_each_zero_or_onep
33    CONSTANT_CLASS_P
34    tree_expr_nonnegative_p
35    tree_expr_nonzero_p
36    integer_valued_real_p
37    integer_pow2p
38    uniform_integer_cst_p
39    HONOR_NANS
40    uniform_vector_p)
42 /* Operator lists.  */
43 (define_operator_list tcc_comparison
44   lt   le   eq ne ge   gt   unordered ordered   unlt unle ungt unge uneq ltgt)
45 (define_operator_list inverted_tcc_comparison
46   ge   gt   ne eq lt   le   ordered   unordered ge   gt   le   lt   ltgt uneq)
47 (define_operator_list inverted_tcc_comparison_with_nans
48   unge ungt ne eq unlt unle ordered   unordered ge   gt   le   lt   ltgt uneq)
49 (define_operator_list swapped_tcc_comparison
50   gt   ge   eq ne le   lt   unordered ordered   ungt unge unlt unle uneq ltgt)
51 (define_operator_list simple_comparison         lt   le   eq ne ge   gt)
52 (define_operator_list swapped_simple_comparison gt   ge   eq ne le   lt)
54 #include "cfn-operators.pd"
56 /* Define operand lists for math rounding functions {,i,l,ll}FN,
57    where the versions prefixed with "i" return an int, those prefixed with
58    "l" return a long and those prefixed with "ll" return a long long.
60    Also define operand lists:
62      X<FN>F for all float functions, in the order i, l, ll
63      X<FN> for all double functions, in the same order
64      X<FN>L for all long double functions, in the same order.  */
65 #define DEFINE_INT_AND_FLOAT_ROUND_FN(FN) \
66   (define_operator_list X##FN##F BUILT_IN_I##FN##F \
67                                  BUILT_IN_L##FN##F \
68                                  BUILT_IN_LL##FN##F) \
69   (define_operator_list X##FN BUILT_IN_I##FN \
70                               BUILT_IN_L##FN \
71                               BUILT_IN_LL##FN) \
72   (define_operator_list X##FN##L BUILT_IN_I##FN##L \
73                                  BUILT_IN_L##FN##L \
74                                  BUILT_IN_LL##FN##L)
76 DEFINE_INT_AND_FLOAT_ROUND_FN (FLOOR)
77 DEFINE_INT_AND_FLOAT_ROUND_FN (CEIL)
78 DEFINE_INT_AND_FLOAT_ROUND_FN (ROUND)
79 DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
81 /* Binary operations and their associated IFN_COND_* function.  */
82 (define_operator_list UNCOND_BINARY
83   plus minus
84   mult trunc_div trunc_mod rdiv
85   min max
86   bit_and bit_ior bit_xor
87   lshift rshift)
88 (define_operator_list COND_BINARY
89   IFN_COND_ADD IFN_COND_SUB
90   IFN_COND_MUL IFN_COND_DIV IFN_COND_MOD IFN_COND_RDIV
91   IFN_COND_MIN IFN_COND_MAX
92   IFN_COND_AND IFN_COND_IOR IFN_COND_XOR
93   IFN_COND_SHL IFN_COND_SHR)
95 /* Same for ternary operations.  */
96 (define_operator_list UNCOND_TERNARY
97   IFN_FMA IFN_FMS IFN_FNMA IFN_FNMS)
98 (define_operator_list COND_TERNARY
99   IFN_COND_FMA IFN_COND_FMS IFN_COND_FNMA IFN_COND_FNMS)
101 /* With nop_convert? combine convert? and view_convert? in one pattern
102    plus conditionalize on tree_nop_conversion_p conversions.  */
103 (match (nop_convert @0)
104  (convert @0)
105  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))))
106 (match (nop_convert @0)
107  (view_convert @0)
108  (if (VECTOR_TYPE_P (type) && VECTOR_TYPE_P (TREE_TYPE (@0))
109       && known_eq (TYPE_VECTOR_SUBPARTS (type),
110                    TYPE_VECTOR_SUBPARTS (TREE_TYPE (@0)))
111       && tree_nop_conversion_p (TREE_TYPE (type), TREE_TYPE (TREE_TYPE (@0))))))
113 /* Transform likes of (char) ABS_EXPR <(int) x> into (char) ABSU_EXPR <x>
114    ABSU_EXPR returns unsigned absolute value of the operand and the operand
115    of the ABSU_EXPR will have the corresponding signed type.  */
116 (simplify (abs (convert @0))
117  (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
118       && !TYPE_UNSIGNED (TREE_TYPE (@0))
119       && element_precision (type) > element_precision (TREE_TYPE (@0)))
120   (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
121    (convert (absu:utype @0)))))
123 #if GIMPLE
124 /* Optimize (X + (X >> (prec - 1))) ^ (X >> (prec - 1)) into abs (X).  */
125 (simplify
126  (bit_xor:c (plus:c @0 (rshift@2 @0 INTEGER_CST@1)) @2)
127  (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
128       && !TYPE_UNSIGNED (TREE_TYPE (@0))
129       && wi::to_widest (@1) == element_precision (TREE_TYPE (@0)) - 1)
130   (abs @0)))
131 #endif
133 /* Simplifications of operations with one constant operand and
134    simplifications to constants or single values.  */
136 (for op (plus pointer_plus minus bit_ior bit_xor)
137   (simplify
138     (op @0 integer_zerop)
139     (non_lvalue @0)))
141 /* 0 +p index -> (type)index */
142 (simplify
143  (pointer_plus integer_zerop @1)
144  (non_lvalue (convert @1)))
146 /* ptr - 0 -> (type)ptr */
147 (simplify
148  (pointer_diff @0 integer_zerop)
149  (convert @0))
151 /* See if ARG1 is zero and X + ARG1 reduces to X.
152    Likewise if the operands are reversed.  */
153 (simplify
154  (plus:c @0 real_zerop@1)
155  (if (fold_real_zero_addition_p (type, @1, 0))
156   (non_lvalue @0)))
158 /* See if ARG1 is zero and X - ARG1 reduces to X.  */
159 (simplify
160  (minus @0 real_zerop@1)
161  (if (fold_real_zero_addition_p (type, @1, 1))
162   (non_lvalue @0)))
164 /* Even if the fold_real_zero_addition_p can't simplify X + 0.0
165    into X, we can optimize (X + 0.0) + 0.0 or (X + 0.0) - 0.0
166    or (X - 0.0) + 0.0 into X + 0.0 and (X - 0.0) - 0.0 into X - 0.0
167    if not -frounding-math.  For sNaNs the first operation would raise
168    exceptions but turn the result into qNan, so the second operation
169    would not raise it.   */
170 (for inner_op (plus minus)
171  (for outer_op (plus minus)
172   (simplify
173    (outer_op (inner_op@3 @0 REAL_CST@1) REAL_CST@2)
174     (if (real_zerop (@1)
175          && real_zerop (@2)
176          && !HONOR_SIGN_DEPENDENT_ROUNDING (type))
177      (with { bool inner_plus = ((inner_op == PLUS_EXPR)
178                                 ^ REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@1)));
179              bool outer_plus
180                = ((outer_op == PLUS_EXPR)
181                   ^ REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@2))); }
182       (if (outer_plus && !inner_plus)
183        (outer_op @0 @2)
184        @3))))))
186 /* Simplify x - x.
187    This is unsafe for certain floats even in non-IEEE formats.
188    In IEEE, it is unsafe because it does wrong for NaNs.
189    Also note that operand_equal_p is always false if an operand
190    is volatile.  */
191 (simplify
192  (minus @0 @0)
193  (if (!FLOAT_TYPE_P (type) || !HONOR_NANS (type))
194   { build_zero_cst (type); }))
195 (simplify
196  (pointer_diff @@0 @0)
197  { build_zero_cst (type); })
199 (simplify
200  (mult @0 integer_zerop@1)
201  @1)
203 /* Maybe fold x * 0 to 0.  The expressions aren't the same
204    when x is NaN, since x * 0 is also NaN.  Nor are they the
205    same in modes with signed zeros, since multiplying a
206    negative value by 0 gives -0, not +0.  */
207 (simplify
208  (mult @0 real_zerop@1)
209  (if (!HONOR_NANS (type) && !HONOR_SIGNED_ZEROS (type))
210   @1))
212 /* In IEEE floating point, x*1 is not equivalent to x for snans.
213    Likewise for complex arithmetic with signed zeros.  */
214 (simplify
215  (mult @0 real_onep)
216  (if (!HONOR_SNANS (type)
217       && (!HONOR_SIGNED_ZEROS (type)
218           || !COMPLEX_FLOAT_TYPE_P (type)))
219   (non_lvalue @0)))
221 /* Transform x * -1.0 into -x.  */
222 (simplify
223  (mult @0 real_minus_onep)
224   (if (!HONOR_SNANS (type)
225        && (!HONOR_SIGNED_ZEROS (type)
226            || !COMPLEX_FLOAT_TYPE_P (type)))
227    (negate @0)))
229 /* Transform { 0 or 1 } * { 0 or 1 } into { 0 or 1 } & { 0 or 1 } */
230 (simplify
231  (mult SSA_NAME@1 SSA_NAME@2)
232   (if (INTEGRAL_TYPE_P (type)
233        && get_nonzero_bits (@1) == 1
234        && get_nonzero_bits (@2) == 1)
235    (bit_and @1 @2)))
237 /* Transform x * { 0 or 1, 0 or 1, ... } into x & { 0 or -1, 0 or -1, ...},
238    unless the target has native support for the former but not the latter.  */
239 (simplify
240  (mult @0 VECTOR_CST@1)
241  (if (initializer_each_zero_or_onep (@1)
242       && !HONOR_SNANS (type)
243       && !HONOR_SIGNED_ZEROS (type))
244   (with { tree itype = FLOAT_TYPE_P (type) ? unsigned_type_for (type) : type; }
245    (if (itype
246         && (!VECTOR_MODE_P (TYPE_MODE (type))
247             || (VECTOR_MODE_P (TYPE_MODE (itype))
248                 && optab_handler (and_optab,
249                                   TYPE_MODE (itype)) != CODE_FOR_nothing)))
250     (view_convert (bit_and:itype (view_convert @0)
251                                  (ne @1 { build_zero_cst (type); })))))))
253 (for cmp (gt ge lt le)
254      outp (convert convert negate negate)
255      outn (negate negate convert convert)
256  /* Transform X * (X > 0.0 ? 1.0 : -1.0) into abs(X). */
257  /* Transform X * (X >= 0.0 ? 1.0 : -1.0) into abs(X). */
258  /* Transform X * (X < 0.0 ? 1.0 : -1.0) into -abs(X). */
259  /* Transform X * (X <= 0.0 ? 1.0 : -1.0) into -abs(X). */
260  (simplify
261   (mult:c @0 (cond (cmp @0 real_zerop) real_onep@1 real_minus_onep))
262   (if (!HONOR_NANS (type) && !HONOR_SIGNED_ZEROS (type))
263    (outp (abs @0))))
264  /* Transform X * (X > 0.0 ? -1.0 : 1.0) into -abs(X). */
265  /* Transform X * (X >= 0.0 ? -1.0 : 1.0) into -abs(X). */
266  /* Transform X * (X < 0.0 ? -1.0 : 1.0) into abs(X). */
267  /* Transform X * (X <= 0.0 ? -1.0 : 1.0) into abs(X). */
268  (simplify
269   (mult:c @0 (cond (cmp @0 real_zerop) real_minus_onep real_onep@1))
270   (if (!HONOR_NANS (type) && !HONOR_SIGNED_ZEROS (type))
271    (outn (abs @0)))))
273 /* Transform X * copysign (1.0, X) into abs(X). */
274 (simplify
275  (mult:c @0 (COPYSIGN_ALL real_onep @0))
276  (if (!HONOR_NANS (type) && !HONOR_SIGNED_ZEROS (type))
277   (abs @0)))
279 /* Transform X * copysign (1.0, -X) into -abs(X). */
280 (simplify
281  (mult:c @0 (COPYSIGN_ALL real_onep (negate @0)))
282  (if (!HONOR_NANS (type) && !HONOR_SIGNED_ZEROS (type))
283   (negate (abs @0))))
285 /* Transform copysign (CST, X) into copysign (ABS(CST), X). */
286 (simplify
287  (COPYSIGN_ALL REAL_CST@0 @1)
288  (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@0)))
289   (COPYSIGN_ALL (negate @0) @1)))
291 /* X * 1, X / 1 -> X.  */
292 (for op (mult trunc_div ceil_div floor_div round_div exact_div)
293   (simplify
294     (op @0 integer_onep)
295     (non_lvalue @0)))
297 /* (A / (1 << B)) -> (A >> B).
298    Only for unsigned A.  For signed A, this would not preserve rounding
299    toward zero.
300    For example: (-1 / ( 1 << B)) !=  -1 >> B.
301    Also also widening conversions, like:
302    (A / (unsigned long long) (1U << B)) -> (A >> B)
303    or
304    (A / (unsigned long long) (1 << B)) -> (A >> B).
305    If the left shift is signed, it can be done only if the upper bits
306    of A starting from shift's type sign bit are zero, as
307    (unsigned long long) (1 << 31) is -2147483648ULL, not 2147483648ULL,
308    so it is valid only if A >> 31 is zero.  */
309 (simplify
310  (trunc_div (convert?@0 @3) (convert2? (lshift integer_onep@1 @2)))
311  (if ((TYPE_UNSIGNED (type) || tree_expr_nonnegative_p (@0))
312       && (!VECTOR_TYPE_P (type)
313           || target_supports_op_p (type, RSHIFT_EXPR, optab_vector)
314           || target_supports_op_p (type, RSHIFT_EXPR, optab_scalar))
315       && (useless_type_conversion_p (type, TREE_TYPE (@1))
316           || (element_precision (type) >= element_precision (TREE_TYPE (@1))
317               && (TYPE_UNSIGNED (TREE_TYPE (@1))
318                   || (element_precision (type)
319                       == element_precision (TREE_TYPE (@1)))
320                   || (INTEGRAL_TYPE_P (type)
321                       && (tree_nonzero_bits (@0)
322                           & wi::mask (element_precision (TREE_TYPE (@1)) - 1,
323                                       true,
324                                       element_precision (type))) == 0)))))
325    (if (!VECTOR_TYPE_P (type)
326         && useless_type_conversion_p (TREE_TYPE (@3), TREE_TYPE (@1))
327         && element_precision (TREE_TYPE (@3)) < element_precision (type))
328     (convert (rshift @3 @2))
329     (rshift @0 @2))))
331 /* Preserve explicit divisions by 0: the C++ front-end wants to detect
332    undefined behavior in constexpr evaluation, and assuming that the division
333    traps enables better optimizations than these anyway.  */
334 (for div (trunc_div ceil_div floor_div round_div exact_div)
335  /* 0 / X is always zero.  */
336  (simplify
337   (div integer_zerop@0 @1)
338   /* But not for 0 / 0 so that we can get the proper warnings and errors.  */
339   (if (!integer_zerop (@1))
340    @0))
341  /* X / -1 is -X.  */
342  (simplify
343   (div @0 integer_minus_onep@1)
344   (if (!TYPE_UNSIGNED (type))
345    (negate @0)))
346  /* X / bool_range_Y is X.  */ 
347  (simplify
348   (div @0 SSA_NAME@1)
349   (if (INTEGRAL_TYPE_P (type) && ssa_name_has_boolean_range (@1))
350    @0))
351  /* X / X is one.  */
352  (simplify
353   (div @0 @0)
354   /* But not for 0 / 0 so that we can get the proper warnings and errors.
355      And not for _Fract types where we can't build 1.  */
356   (if (!integer_zerop (@0) && !ALL_FRACT_MODE_P (TYPE_MODE (type)))
357    { build_one_cst (type); }))
358  /* X / abs (X) is X < 0 ? -1 : 1.  */
359  (simplify
360    (div:C @0 (abs @0))
361    (if (INTEGRAL_TYPE_P (type)
362         && TYPE_OVERFLOW_UNDEFINED (type))
363     (cond (lt @0 { build_zero_cst (type); })
364           { build_minus_one_cst (type); } { build_one_cst (type); })))
365  /* X / -X is -1.  */
366  (simplify
367    (div:C @0 (negate @0))
368    (if ((INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
369         && TYPE_OVERFLOW_UNDEFINED (type))
370     { build_minus_one_cst (type); })))
372 /* For unsigned integral types, FLOOR_DIV_EXPR is the same as
373    TRUNC_DIV_EXPR.  Rewrite into the latter in this case.  */
374 (simplify
375  (floor_div @0 @1)
376  (if ((INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
377       && TYPE_UNSIGNED (type))
378   (trunc_div @0 @1)))
380 /* Combine two successive divisions.  Note that combining ceil_div
381    and floor_div is trickier and combining round_div even more so.  */
382 (for div (trunc_div exact_div)
383  (simplify
384   (div (div@3 @0 INTEGER_CST@1) INTEGER_CST@2)
385   (with {
386     wi::overflow_type overflow;
387     wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@2),
388                             TYPE_SIGN (type), &overflow);
389    }
390    (if (div == EXACT_DIV_EXPR
391         || optimize_successive_divisions_p (@2, @3))
392     (if (!overflow)
393      (div @0 { wide_int_to_tree (type, mul); })
394      (if (TYPE_UNSIGNED (type)
395           || mul != wi::min_value (TYPE_PRECISION (type), SIGNED))
396       { build_zero_cst (type); }))))))
398 /* Combine successive multiplications.  Similar to above, but handling
399    overflow is different.  */
400 (simplify
401  (mult (mult @0 INTEGER_CST@1) INTEGER_CST@2)
402  (with {
403    wi::overflow_type overflow;
404    wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@2),
405                            TYPE_SIGN (type), &overflow);
406   }
407   /* Skip folding on overflow: the only special case is @1 * @2 == -INT_MIN,
408      otherwise undefined overflow implies that @0 must be zero.  */
409   (if (!overflow || TYPE_OVERFLOW_WRAPS (type))
410    (mult @0 { wide_int_to_tree (type, mul); }))))
412 /* Optimize A / A to 1.0 if we don't care about
413    NaNs or Infinities.  */
414 (simplify
415  (rdiv @0 @0)
416  (if (FLOAT_TYPE_P (type)
417       && ! HONOR_NANS (type)
418       && ! HONOR_INFINITIES (type))
419   { build_one_cst (type); }))
421 /* Optimize -A / A to -1.0 if we don't care about
422    NaNs or Infinities.  */
423 (simplify
424  (rdiv:C @0 (negate @0))
425  (if (FLOAT_TYPE_P (type)
426       && ! HONOR_NANS (type)
427       && ! HONOR_INFINITIES (type))
428   { build_minus_one_cst (type); }))
430 /* PR71078: x / abs(x) -> copysign (1.0, x) */
431 (simplify
432  (rdiv:C (convert? @0) (convert? (abs @0)))
433   (if (SCALAR_FLOAT_TYPE_P (type)
434        && ! HONOR_NANS (type)
435        && ! HONOR_INFINITIES (type))
436    (switch
437     (if (types_match (type, float_type_node))
438      (BUILT_IN_COPYSIGNF { build_one_cst (type); } (convert @0)))
439     (if (types_match (type, double_type_node))
440      (BUILT_IN_COPYSIGN { build_one_cst (type); } (convert @0)))
441     (if (types_match (type, long_double_type_node))
442      (BUILT_IN_COPYSIGNL { build_one_cst (type); } (convert @0))))))
444 /* In IEEE floating point, x/1 is not equivalent to x for snans.  */
445 (simplify
446  (rdiv @0 real_onep)
447  (if (!HONOR_SNANS (type))
448   (non_lvalue @0)))
450 /* In IEEE floating point, x/-1 is not equivalent to -x for snans.  */
451 (simplify
452  (rdiv @0 real_minus_onep)
453  (if (!HONOR_SNANS (type))
454   (negate @0)))
456 (if (flag_reciprocal_math)
457  /* Convert (A/B)/C to A/(B*C). */
458  (simplify
459   (rdiv (rdiv:s @0 @1) @2)
460   (rdiv @0 (mult @1 @2)))
462  /* Canonicalize x / (C1 * y) to (x * C2) / y.  */
463  (simplify
464   (rdiv @0 (mult:s @1 REAL_CST@2))
465   (with
466    { tree tem = const_binop (RDIV_EXPR, type, build_one_cst (type), @2); }
467    (if (tem)
468     (rdiv (mult @0 { tem; } ) @1))))
470  /* Convert A/(B/C) to (A/B)*C  */
471  (simplify
472   (rdiv @0 (rdiv:s @1 @2))
473    (mult (rdiv @0 @1) @2)))
475 /* Simplify x / (- y) to -x / y.  */
476 (simplify
477  (rdiv @0 (negate @1))
478  (rdiv (negate @0) @1))
480 (if (flag_unsafe_math_optimizations)
481  /* Simplify (C / x op 0.0) to x op 0.0 for C != 0, C != Inf/Nan.
482     Since C / x may underflow to zero, do this only for unsafe math.  */
483  (for op (lt le gt ge)
484       neg_op (gt ge lt le)
485   (simplify
486    (op (rdiv REAL_CST@0 @1) real_zerop@2)
487    (if (!HONOR_SIGNED_ZEROS (@1) && !HONOR_INFINITIES (@1))
488     (switch
489      (if (real_less (&dconst0, TREE_REAL_CST_PTR (@0)))
490       (op @1 @2))
491      /* For C < 0, use the inverted operator.  */
492      (if (real_less (TREE_REAL_CST_PTR (@0), &dconst0))
493       (neg_op @1 @2)))))))
495 /* Optimize (X & (-A)) / A where A is a power of 2, to X >> log2(A) */
496 (for div (trunc_div ceil_div floor_div round_div exact_div)
497  (simplify
498   (div (convert? (bit_and @0 INTEGER_CST@1)) INTEGER_CST@2)
499   (if (integer_pow2p (@2)
500        && tree_int_cst_sgn (@2) > 0
501        && tree_nop_conversion_p (type, TREE_TYPE (@0))
502        && wi::to_wide (@2) + wi::to_wide (@1) == 0)
503    (rshift (convert @0)
504            { build_int_cst (integer_type_node,
505                             wi::exact_log2 (wi::to_wide (@2))); }))))
507 /* If ARG1 is a constant, we can convert this to a multiply by the
508    reciprocal.  This does not have the same rounding properties,
509    so only do this if -freciprocal-math.  We can actually
510    always safely do it if ARG1 is a power of two, but it's hard to
511    tell if it is or not in a portable manner.  */
512 (for cst (REAL_CST COMPLEX_CST VECTOR_CST)
513  (simplify
514   (rdiv @0 cst@1)
515   (if (optimize)
516    (if (flag_reciprocal_math
517         && !real_zerop (@1))
518     (with
519      { tree tem = const_binop (RDIV_EXPR, type, build_one_cst (type), @1); }
520      (if (tem)
521       (mult @0 { tem; } )))
522     (if (cst != COMPLEX_CST)
523      (with { tree inverse = exact_inverse (type, @1); }
524       (if (inverse)
525        (mult @0 { inverse; } ))))))))
527 (for mod (ceil_mod floor_mod round_mod trunc_mod)
528  /* 0 % X is always zero.  */
529  (simplify
530   (mod integer_zerop@0 @1)
531   /* But not for 0 % 0 so that we can get the proper warnings and errors.  */
532   (if (!integer_zerop (@1))
533    @0))
534  /* X % 1 is always zero.  */
535  (simplify
536   (mod @0 integer_onep)
537   { build_zero_cst (type); })
538  /* X % -1 is zero.  */
539  (simplify
540   (mod @0 integer_minus_onep@1)
541   (if (!TYPE_UNSIGNED (type))
542    { build_zero_cst (type); }))
543  /* X % X is zero.  */
544  (simplify
545   (mod @0 @0)
546   /* But not for 0 % 0 so that we can get the proper warnings and errors.  */
547   (if (!integer_zerop (@0))
548    { build_zero_cst (type); }))
549  /* (X % Y) % Y is just X % Y.  */
550  (simplify
551   (mod (mod@2 @0 @1) @1)
552   @2)
553  /* From extract_muldiv_1: (X * C1) % C2 is zero if C1 is a multiple of C2.  */
554  (simplify
555   (mod (mult @0 INTEGER_CST@1) INTEGER_CST@2)
556   (if (ANY_INTEGRAL_TYPE_P (type)
557        && TYPE_OVERFLOW_UNDEFINED (type)
558        && wi::multiple_of_p (wi::to_wide (@1), wi::to_wide (@2),
559                              TYPE_SIGN (type)))
560    { build_zero_cst (type); }))
561  /* For (X % C) == 0, if X is signed and C is power of 2, use unsigned
562     modulo and comparison, since it is simpler and equivalent.  */
563  (for cmp (eq ne)
564   (simplify
565    (cmp (mod @0 integer_pow2p@2) integer_zerop@1)
566    (if (!TYPE_UNSIGNED (TREE_TYPE (@0)))
567     (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
568      (cmp (mod (convert:utype @0) (convert:utype @2)) (convert:utype @1)))))))
570 /* X % -C is the same as X % C.  */
571 (simplify
572  (trunc_mod @0 INTEGER_CST@1)
573   (if (TYPE_SIGN (type) == SIGNED
574        && !TREE_OVERFLOW (@1)
575        && wi::neg_p (wi::to_wide (@1))
576        && !TYPE_OVERFLOW_TRAPS (type)
577        /* Avoid this transformation if C is INT_MIN, i.e. C == -C.  */
578        && !sign_bit_p (@1, @1))
579    (trunc_mod @0 (negate @1))))
581 /* X % -Y is the same as X % Y.  */
582 (simplify
583  (trunc_mod @0 (convert? (negate @1)))
584  (if (INTEGRAL_TYPE_P (type)
585       && !TYPE_UNSIGNED (type)
586       && !TYPE_OVERFLOW_TRAPS (type)
587       && tree_nop_conversion_p (type, TREE_TYPE (@1))
588       /* Avoid this transformation if X might be INT_MIN or
589          Y might be -1, because we would then change valid
590          INT_MIN % -(-1) into invalid INT_MIN % -1.  */
591       && (expr_not_equal_to (@0, wi::to_wide (TYPE_MIN_VALUE (type)))
592           || expr_not_equal_to (@1, wi::minus_one (TYPE_PRECISION
593                                                         (TREE_TYPE (@1))))))
594   (trunc_mod @0 (convert @1))))
596 /* X - (X / Y) * Y is the same as X % Y.  */
597 (simplify
598  (minus (convert1? @0) (convert2? (mult:c (trunc_div @@0 @@1) @1)))
599  (if (INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
600   (convert (trunc_mod @0 @1))))
602 /* Optimize TRUNC_MOD_EXPR by a power of two into a BIT_AND_EXPR,
603    i.e. "X % C" into "X & (C - 1)", if X and C are positive.
604    Also optimize A % (C << N)  where C is a power of 2,
605    to A & ((C << N) - 1).
606    Also optimize "A shift (B % C)", if C is a power of 2, to
607    "A shift (B & (C - 1))".  SHIFT operation include "<<" and ">>"
608    and assume (B % C) is nonnegative as shifts negative values would
609    be UB.  */
610 (match (power_of_two_cand @1)
611  INTEGER_CST@1)
612 (match (power_of_two_cand @1)
613  (lshift INTEGER_CST@1 @2))
614 (for mod (trunc_mod floor_mod)
615  (for shift (lshift rshift)
616   (simplify
617    (shift @0 (mod @1 (power_of_two_cand@2 @3)))
618    (if (integer_pow2p (@3) && tree_int_cst_sgn (@3) > 0)
619     (shift @0 (bit_and @1 (minus @2 { build_int_cst (TREE_TYPE (@2),
620                                                       1); }))))))
621  (simplify
622   (mod @0 (convert? (power_of_two_cand@1 @2)))
623   (if ((TYPE_UNSIGNED (type) || tree_expr_nonnegative_p (@0))
624        /* Allow any integral conversions of the divisor, except
625           conversion from narrower signed to wider unsigned type
626           where if @1 would be negative power of two, the divisor
627           would not be a power of two.  */
628        && INTEGRAL_TYPE_P (type)
629        && INTEGRAL_TYPE_P (TREE_TYPE (@1))
630        && (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@1))
631            || TYPE_UNSIGNED (TREE_TYPE (@1))
632            || !TYPE_UNSIGNED (type))
633        && integer_pow2p (@2) && tree_int_cst_sgn (@2) > 0)
634    (with { tree utype = TREE_TYPE (@1);
635            if (!TYPE_OVERFLOW_WRAPS (utype))
636              utype = unsigned_type_for (utype); }
637     (bit_and @0 (convert (minus (convert:utype @1)
638                                 { build_one_cst (utype); })))))))
640 /* Simplify (unsigned t * 2)/2 -> unsigned t & 0x7FFFFFFF.  */
641 (simplify
642  (trunc_div (mult @0 integer_pow2p@1) @1)
643  (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
644   (bit_and @0 { wide_int_to_tree
645                 (type, wi::mask (TYPE_PRECISION (type)
646                                  - wi::exact_log2 (wi::to_wide (@1)),
647                                  false, TYPE_PRECISION (type))); })))
649 /* Simplify (unsigned t / 2) * 2 -> unsigned t & ~1.  */
650 (simplify
651  (mult (trunc_div @0 integer_pow2p@1) @1)
652  (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
653   (bit_and @0 (negate @1))))
655 /* Simplify (t * 2) / 2) -> t.  */
656 (for div (trunc_div ceil_div floor_div round_div exact_div)
657  (simplify
658   (div (mult:c @0 @1) @1)
659   (if (ANY_INTEGRAL_TYPE_P (type))
660    (if (TYPE_OVERFLOW_UNDEFINED (type))
661     @0
662 #if GIMPLE
663     (with
664      {
665        bool overflowed = true;
666        wide_int wmin0, wmax0, wmin1, wmax1;
667        if (INTEGRAL_TYPE_P (type)
668            && get_range_info (@0, &wmin0, &wmax0) == VR_RANGE
669            && get_range_info (@1, &wmin1, &wmax1) == VR_RANGE)
670          {
671            /* If the multiplication can't overflow/wrap around, then
672               it can be optimized too.  */
673            wi::overflow_type min_ovf, max_ovf;
674            wi::mul (wmin0, wmin1, TYPE_SIGN (type), &min_ovf);
675            wi::mul (wmax0, wmax1, TYPE_SIGN (type), &max_ovf);
676            if (min_ovf == wi::OVF_NONE && max_ovf == wi::OVF_NONE)
677              {
678                wi::mul (wmin0, wmax1, TYPE_SIGN (type), &min_ovf);
679                wi::mul (wmax0, wmin1, TYPE_SIGN (type), &max_ovf);
680                if (min_ovf == wi::OVF_NONE && max_ovf == wi::OVF_NONE)
681                  overflowed = false;
682              }
683          }
684      }
685     (if (!overflowed)
686      @0))
687 #endif
688    ))))
690 (for op (negate abs)
691  /* Simplify cos(-x) and cos(|x|) -> cos(x).  Similarly for cosh.  */
692  (for coss (COS COSH)
693   (simplify
694    (coss (op @0))
695     (coss @0)))
696  /* Simplify pow(-x, y) and pow(|x|,y) -> pow(x,y) if y is an even integer.  */
697  (for pows (POW)
698   (simplify
699    (pows (op @0) REAL_CST@1)
700    (with { HOST_WIDE_INT n; }
701     (if (real_isinteger (&TREE_REAL_CST (@1), &n) && (n & 1) == 0)
702      (pows @0 @1)))))
703  /* Likewise for powi.  */
704  (for pows (POWI)
705   (simplify
706    (pows (op @0) INTEGER_CST@1)
707    (if ((wi::to_wide (@1) & 1) == 0)
708     (pows @0 @1))))
709  /* Strip negate and abs from both operands of hypot.  */
710  (for hypots (HYPOT)
711   (simplify
712    (hypots (op @0) @1)
713    (hypots @0 @1))
714   (simplify
715    (hypots @0 (op @1))
716    (hypots @0 @1)))
717  /* copysign(-x, y) and copysign(abs(x), y) -> copysign(x, y).  */
718  (for copysigns (COPYSIGN_ALL)
719   (simplify
720    (copysigns (op @0) @1)
721    (copysigns @0 @1))))
723 /* abs(x)*abs(x) -> x*x.  Should be valid for all types.  */
724 (simplify
725  (mult (abs@1 @0) @1)
726  (mult @0 @0))
728 /* Convert absu(x)*absu(x) -> x*x.  */
729 (simplify
730  (mult (absu@1 @0) @1)
731  (mult (convert@2 @0) @2))
733 /* cos(copysign(x, y)) -> cos(x).  Similarly for cosh.  */
734 (for coss (COS COSH)
735      copysigns (COPYSIGN)
736  (simplify
737   (coss (copysigns @0 @1))
738    (coss @0)))
740 /* pow(copysign(x, y), z) -> pow(x, z) if z is an even integer.  */
741 (for pows (POW)
742      copysigns (COPYSIGN)
743  (simplify
744   (pows (copysigns @0 @2) REAL_CST@1)
745   (with { HOST_WIDE_INT n; }
746    (if (real_isinteger (&TREE_REAL_CST (@1), &n) && (n & 1) == 0)
747     (pows @0 @1)))))
748 /* Likewise for powi.  */
749 (for pows (POWI)
750      copysigns (COPYSIGN)
751  (simplify
752   (pows (copysigns @0 @2) INTEGER_CST@1)
753   (if ((wi::to_wide (@1) & 1) == 0)
754    (pows @0 @1))))
756 (for hypots (HYPOT)
757      copysigns (COPYSIGN)
758  /* hypot(copysign(x, y), z) -> hypot(x, z).  */
759  (simplify
760   (hypots (copysigns @0 @1) @2)
761   (hypots @0 @2))
762  /* hypot(x, copysign(y, z)) -> hypot(x, y).  */
763  (simplify
764   (hypots @0 (copysigns @1 @2))
765   (hypots @0 @1)))
767 /* copysign(x, CST) -> [-]abs (x).  */
768 (for copysigns (COPYSIGN_ALL)
769  (simplify
770   (copysigns @0 REAL_CST@1)
771   (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
772    (negate (abs @0))
773    (abs @0))))
775 /* copysign(copysign(x, y), z) -> copysign(x, z).  */
776 (for copysigns (COPYSIGN_ALL)
777  (simplify
778   (copysigns (copysigns @0 @1) @2)
779   (copysigns @0 @2)))
781 /* copysign(x,y)*copysign(x,y) -> x*x.  */
782 (for copysigns (COPYSIGN_ALL)
783  (simplify
784   (mult (copysigns@2 @0 @1) @2)
785   (mult @0 @0)))
787 /* ccos(-x) -> ccos(x).  Similarly for ccosh.  */
788 (for ccoss (CCOS CCOSH)
789  (simplify
790   (ccoss (negate @0))
791    (ccoss @0)))
793 /* cabs(-x) and cos(conj(x)) -> cabs(x).  */
794 (for ops (conj negate)
795  (for cabss (CABS)
796   (simplify
797    (cabss (ops @0))
798    (cabss @0))))
800 /* Fold (a * (1 << b)) into (a << b)  */
801 (simplify
802  (mult:c @0 (convert? (lshift integer_onep@1 @2)))
803   (if (! FLOAT_TYPE_P (type)
804        && tree_nop_conversion_p (type, TREE_TYPE (@1)))
805    (lshift @0 @2)))
807 /* Fold (1 << (C - x)) where C = precision(type) - 1
808    into ((1 << C) >> x). */
809 (simplify
810  (lshift integer_onep@0 (minus@1 INTEGER_CST@2 @3))
811   (if (INTEGRAL_TYPE_P (type)
812        && wi::eq_p (wi::to_wide (@2), TYPE_PRECISION (type) - 1)
813        && single_use (@1))
814    (if (TYPE_UNSIGNED (type))
815      (rshift (lshift @0 @2) @3)
816    (with
817     { tree utype = unsigned_type_for (type); }
818     (convert (rshift (lshift (convert:utype @0) @2) @3))))))
820 /* Fold (C1/X)*C2 into (C1*C2)/X.  */
821 (simplify
822  (mult (rdiv@3 REAL_CST@0 @1) REAL_CST@2)
823   (if (flag_associative_math
824        && single_use (@3))
825    (with
826     { tree tem = const_binop (MULT_EXPR, type, @0, @2); }
827     (if (tem)
828      (rdiv { tem; } @1)))))
830 /* Simplify ~X & X as zero.  */
831 (simplify
832  (bit_and:c (convert? @0) (convert? (bit_not @0)))
833   { build_zero_cst (type); })
835 /* PR71636: Transform x & ((1U << b) - 1) -> x & ~(~0U << b);  */
836 (simplify
837   (bit_and:c @0 (plus:s (lshift:s integer_onep @1) integer_minus_onep))
838   (if (TYPE_UNSIGNED (type))
839     (bit_and @0 (bit_not (lshift { build_all_ones_cst (type); } @1)))))
841 (for bitop (bit_and bit_ior)
842      cmp (eq ne)
843  /* PR35691: Transform
844     (x == 0 & y == 0) -> (x | typeof(x)(y)) == 0.
845     (x != 0 | y != 0) -> (x | typeof(x)(y)) != 0.  */
846  (simplify
847   (bitop (cmp @0 integer_zerop@2) (cmp @1 integer_zerop))
848    (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
849         && INTEGRAL_TYPE_P (TREE_TYPE (@1))
850         && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
851     (cmp (bit_ior @0 (convert @1)) @2)))
852  /* Transform:
853     (x == -1 & y == -1) -> (x & typeof(x)(y)) == -1.
854     (x != -1 | y != -1) -> (x & typeof(x)(y)) != -1.  */
855  (simplify
856   (bitop (cmp @0 integer_all_onesp@2) (cmp @1 integer_all_onesp))
857    (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
858         && INTEGRAL_TYPE_P (TREE_TYPE (@1))
859         && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
860     (cmp (bit_and @0 (convert @1)) @2))))
862 /* Fold (A & ~B) - (A & B) into (A ^ B) - B.  */
863 (simplify
864  (minus (bit_and:cs @0 (bit_not @1)) (bit_and:cs @0 @1))
865   (minus (bit_xor @0 @1) @1))
866 (simplify
867  (minus (bit_and:s @0 INTEGER_CST@2) (bit_and:s @0 INTEGER_CST@1))
868  (if (~wi::to_wide (@2) == wi::to_wide (@1))
869   (minus (bit_xor @0 @1) @1)))
871 /* Fold (A & B) - (A & ~B) into B - (A ^ B).  */
872 (simplify
873  (minus (bit_and:cs @0 @1) (bit_and:cs @0 (bit_not @1)))
874   (minus @1 (bit_xor @0 @1)))
876 /* Simplify (X & ~Y) |^+ (~X & Y) -> X ^ Y.  */
877 (for op (bit_ior bit_xor plus)
878  (simplify
879   (op (bit_and:c @0 (bit_not @1)) (bit_and:c (bit_not @0) @1))
880    (bit_xor @0 @1))
881  (simplify
882   (op:c (bit_and @0 INTEGER_CST@2) (bit_and (bit_not @0) INTEGER_CST@1))
883   (if (~wi::to_wide (@2) == wi::to_wide (@1))
884    (bit_xor @0 @1))))
886 /* PR53979: Transform ((a ^ b) | a) -> (a | b) */
887 (simplify
888   (bit_ior:c (bit_xor:c @0 @1) @0)
889   (bit_ior @0 @1))
891 /* (a & ~b) | (a ^ b)  -->  a ^ b  */
892 (simplify
893  (bit_ior:c (bit_and:c @0 (bit_not @1)) (bit_xor:c@2 @0 @1))
894  @2)
896 /* (a & ~b) ^ ~a  -->  ~(a & b)  */
897 (simplify
898  (bit_xor:c (bit_and:cs @0 (bit_not @1)) (bit_not @0))
899  (bit_not (bit_and @0 @1)))
901 /* (~a & b) ^ a  -->   (a | b)   */
902 (simplify
903  (bit_xor:c (bit_and:cs (bit_not @0) @1) @0)
904  (bit_ior @0 @1))
906 /* (a | b) & ~(a ^ b)  -->  a & b  */
907 (simplify
908  (bit_and:c (bit_ior @0 @1) (bit_not (bit_xor:c @0 @1)))
909  (bit_and @0 @1))
911 /* a | ~(a ^ b)  -->  a | ~b  */
912 (simplify
913  (bit_ior:c @0 (bit_not:s (bit_xor:c @0 @1)))
914  (bit_ior @0 (bit_not @1)))
916 /* (a | b) | (a &^ b)  -->  a | b  */
917 (for op (bit_and bit_xor)
918  (simplify
919   (bit_ior:c (bit_ior@2 @0 @1) (op:c @0 @1))
920   @2))
922 /* (a & b) | ~(a ^ b)  -->  ~(a ^ b)  */
923 (simplify
924  (bit_ior:c (bit_and:c @0 @1) (bit_not@2 (bit_xor @0 @1)))
925  @2)
927 /* ~(~a & b)  -->  a | ~b  */
928 (simplify
929  (bit_not (bit_and:cs (bit_not @0) @1))
930  (bit_ior @0 (bit_not @1)))
932 /* ~(~a | b) --> a & ~b */
933 (simplify
934  (bit_not (bit_ior:cs (bit_not @0) @1))
935  (bit_and @0 (bit_not @1)))
937 /* (a ^ b) & ((b ^ c) ^ a) --> (a ^ b) & ~c */
938 (simplify
939  (bit_and:c (bit_xor:c@3 @0 @1) (bit_xor:cs (bit_xor:cs @1 @2) @0))
940  (bit_and @3 (bit_not @2)))
942 /* (a ^ b) | ((b ^ c) ^ a) --> (a ^ b) | c */
943 (simplify
944  (bit_ior:c (bit_xor:c@3 @0 @1) (bit_xor:c (bit_xor:c @1 @2) @0))
945  (bit_ior @3 @2))
947 #if GIMPLE
948 /* (~X | C) ^ D -> (X | C) ^ (~D ^ C) if (~D ^ C) can be simplified.  */
949 (simplify
950  (bit_xor:c (bit_ior:cs (bit_not:s @0) @1) @2)
951   (bit_xor (bit_ior @0 @1) (bit_xor! (bit_not! @2) @1)))
953 /* (~X & C) ^ D -> (X & C) ^ (D ^ C) if (D ^ C) can be simplified.  */
954 (simplify
955  (bit_xor:c (bit_and:cs (bit_not:s @0) @1) @2)
956   (bit_xor (bit_and @0 @1) (bit_xor! @2 @1)))
958 /* Simplify (~X & Y) to X ^ Y if we know that (X & ~Y) is 0.  */
959 (simplify
960  (bit_and (bit_not SSA_NAME@0) INTEGER_CST@1)
961  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
962       && wi::bit_and_not (get_nonzero_bits (@0), wi::to_wide (@1)) == 0)
963   (bit_xor @0 @1)))
964 #endif
966 /* For constants M and N, if M == (1LL << cst) - 1 && (N & M) == M,
967    ((A & N) + B) & M -> (A + B) & M
968    Similarly if (N & M) == 0,
969    ((A | N) + B) & M -> (A + B) & M
970    and for - instead of + (or unary - instead of +)
971    and/or ^ instead of |.
972    If B is constant and (B & M) == 0, fold into A & M.  */
973 (for op (plus minus)
974  (for bitop (bit_and bit_ior bit_xor)
975   (simplify
976    (bit_and (op:s (bitop:s@0 @3 INTEGER_CST@4) @1) INTEGER_CST@2)
977     (with
978      { tree pmop[2];
979        tree utype = fold_bit_and_mask (TREE_TYPE (@0), @2, op, @0, bitop,
980                                        @3, @4, @1, ERROR_MARK, NULL_TREE,
981                                        NULL_TREE, pmop); }
982      (if (utype)
983       (convert (bit_and (op (convert:utype { pmop[0]; })
984                             (convert:utype { pmop[1]; }))
985                         (convert:utype @2))))))
986   (simplify
987    (bit_and (op:s @0 (bitop:s@1 @3 INTEGER_CST@4)) INTEGER_CST@2)
988     (with
989      { tree pmop[2];
990        tree utype = fold_bit_and_mask (TREE_TYPE (@0), @2, op, @0, ERROR_MARK,
991                                        NULL_TREE, NULL_TREE, @1, bitop, @3,
992                                        @4, pmop); }
993      (if (utype)
994       (convert (bit_and (op (convert:utype { pmop[0]; })
995                             (convert:utype { pmop[1]; }))
996                         (convert:utype @2)))))))
997  (simplify
998   (bit_and (op:s @0 @1) INTEGER_CST@2)
999    (with
1000     { tree pmop[2];
1001       tree utype = fold_bit_and_mask (TREE_TYPE (@0), @2, op, @0, ERROR_MARK,
1002                                       NULL_TREE, NULL_TREE, @1, ERROR_MARK,
1003                                       NULL_TREE, NULL_TREE, pmop); }
1004     (if (utype)
1005      (convert (bit_and (op (convert:utype { pmop[0]; })
1006                            (convert:utype { pmop[1]; }))
1007                        (convert:utype @2)))))))
1008 (for bitop (bit_and bit_ior bit_xor)
1009  (simplify
1010   (bit_and (negate:s (bitop:s@0 @2 INTEGER_CST@3)) INTEGER_CST@1)
1011    (with
1012     { tree pmop[2];
1013       tree utype = fold_bit_and_mask (TREE_TYPE (@0), @1, NEGATE_EXPR, @0,
1014                                       bitop, @2, @3, NULL_TREE, ERROR_MARK,
1015                                       NULL_TREE, NULL_TREE, pmop); }
1016     (if (utype)
1017      (convert (bit_and (negate (convert:utype { pmop[0]; }))
1018                        (convert:utype @1)))))))
1020 /* X % Y is smaller than Y.  */
1021 (for cmp (lt ge)
1022  (simplify
1023   (cmp (trunc_mod @0 @1) @1)
1024   (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
1025    { constant_boolean_node (cmp == LT_EXPR, type); })))
1026 (for cmp (gt le)
1027  (simplify
1028   (cmp @1 (trunc_mod @0 @1))
1029   (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
1030    { constant_boolean_node (cmp == GT_EXPR, type); })))
1032 /* x | ~0 -> ~0  */
1033 (simplify
1034  (bit_ior @0 integer_all_onesp@1)
1035  @1)
1037 /* x | 0 -> x  */
1038 (simplify
1039  (bit_ior @0 integer_zerop)
1040  @0)
1042 /* x & 0 -> 0  */
1043 (simplify
1044  (bit_and @0 integer_zerop@1)
1045  @1)
1047 /* ~x | x -> -1 */
1048 /* ~x ^ x -> -1 */
1049 /* ~x + x -> -1 */
1050 (for op (bit_ior bit_xor plus)
1051  (simplify
1052   (op:c (convert? @0) (convert? (bit_not @0)))
1053   (convert { build_all_ones_cst (TREE_TYPE (@0)); })))
1055 /* x ^ x -> 0 */
1056 (simplify
1057   (bit_xor @0 @0)
1058   { build_zero_cst (type); })
1060 /* Canonicalize X ^ ~0 to ~X.  */
1061 (simplify
1062   (bit_xor @0 integer_all_onesp@1)
1063   (bit_not @0))
1065 /* x & ~0 -> x  */
1066 (simplify
1067  (bit_and @0 integer_all_onesp)
1068   (non_lvalue @0))
1070 /* x & x -> x,  x | x -> x  */
1071 (for bitop (bit_and bit_ior)
1072  (simplify
1073   (bitop @0 @0)
1074   (non_lvalue @0)))
1076 /* x & C -> x if we know that x & ~C == 0.  */
1077 #if GIMPLE
1078 (simplify
1079  (bit_and SSA_NAME@0 INTEGER_CST@1)
1080  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1081       && wi::bit_and_not (get_nonzero_bits (@0), wi::to_wide (@1)) == 0)
1082   @0))
1083 #endif
1085 /* ~(~X - Y) -> X + Y and ~(~X + Y) -> X - Y.  */
1086 (simplify
1087  (bit_not (minus (bit_not @0) @1))
1088  (plus @0 @1))
1089 (simplify
1090  (bit_not (plus:c (bit_not @0) @1))
1091  (minus @0 @1))
1093 /* ~(X - Y) -> ~X + Y.  */
1094 (simplify
1095  (bit_not (minus:s @0 @1))
1096  (plus (bit_not @0) @1))
1097 (simplify
1098  (bit_not (plus:s @0 INTEGER_CST@1))
1099  (if ((INTEGRAL_TYPE_P (type)
1100        && TYPE_UNSIGNED (type))
1101       || (!TYPE_OVERFLOW_SANITIZED (type)
1102           && may_negate_without_overflow_p (@1)))
1103   (plus (bit_not @0) { const_unop (NEGATE_EXPR, type, @1); })))
1105 #if GIMPLE
1106 /* ~X + Y -> (Y - X) - 1.  */
1107 (simplify
1108  (plus:c (bit_not @0) @1)
1109   (if (ANY_INTEGRAL_TYPE_P (type)
1110        && TYPE_OVERFLOW_WRAPS (type)
1111        /* -1 - X is folded to ~X, so we'd recurse endlessly.  */
1112        && !integer_all_onesp (@1))
1113    (plus (minus @1 @0) { build_minus_one_cst (type); })
1114    (if (INTEGRAL_TYPE_P (type)
1115         && TREE_CODE (@1) == INTEGER_CST
1116         && wi::to_wide (@1) != wi::min_value (TYPE_PRECISION (type),
1117                                               SIGNED))
1118     (minus (plus @1 { build_minus_one_cst (type); }) @0))))
1120 /* ~(X >> Y) -> ~X >> Y if ~X can be simplified.  */
1121 (simplify
1122  (bit_not (rshift:s @0 @1))
1123   (if (!TYPE_UNSIGNED (TREE_TYPE (@0)))
1124    (rshift (bit_not! @0) @1)
1125    /* For logical right shifts, this is possible only if @0 doesn't
1126       have MSB set and the logical right shift is changed into
1127       arithmetic shift.  */
1128    (if (!wi::neg_p (tree_nonzero_bits (@0)))
1129     (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
1130      (convert (rshift (bit_not! (convert:stype @0)) @1))))))
1131 #endif
1133 /* x + (x & 1) -> (x + 1) & ~1 */
1134 (simplify
1135  (plus:c @0 (bit_and:s @0 integer_onep@1))
1136  (bit_and (plus @0 @1) (bit_not @1)))
1138 /* x & ~(x & y) -> x & ~y */
1139 /* x | ~(x | y) -> x | ~y  */
1140 (for bitop (bit_and bit_ior)
1141  (simplify
1142   (bitop:c @0 (bit_not (bitop:cs @0 @1)))
1143   (bitop @0 (bit_not @1))))
1145 /* (~x & y) | ~(x | y) -> ~x */
1146 (simplify
1147  (bit_ior:c (bit_and:c (bit_not@2 @0) @1) (bit_not (bit_ior:c @0 @1)))
1148  @2)
1150 /* (x | y) ^ (x | ~y) -> ~x */
1151 (simplify
1152  (bit_xor:c (bit_ior:c @0 @1) (bit_ior:c @0 (bit_not @1)))
1153  (bit_not @0))
1155 /* (x & y) | ~(x | y) -> ~(x ^ y) */
1156 (simplify
1157  (bit_ior:c (bit_and:s @0 @1) (bit_not:s (bit_ior:s @0 @1)))
1158  (bit_not (bit_xor @0 @1)))
1160 /* (~x | y) ^ (x ^ y) -> x | ~y */
1161 (simplify
1162  (bit_xor:c (bit_ior:cs (bit_not @0) @1) (bit_xor:s @0 @1))
1163  (bit_ior @0 (bit_not @1)))
1165 /* (x ^ y) | ~(x | y) -> ~(x & y) */
1166 (simplify
1167  (bit_ior:c (bit_xor:s @0 @1) (bit_not:s (bit_ior:s @0 @1)))
1168  (bit_not (bit_and @0 @1)))
1170 /* (x | y) & ~x -> y & ~x */
1171 /* (x & y) | ~x -> y | ~x */
1172 (for bitop (bit_and bit_ior)
1173      rbitop (bit_ior bit_and)
1174  (simplify
1175   (bitop:c (rbitop:c @0 @1) (bit_not@2 @0))
1176   (bitop @1 @2)))
1178 /* (x & y) ^ (x | y) -> x ^ y */
1179 (simplify
1180  (bit_xor:c (bit_and @0 @1) (bit_ior @0 @1))
1181  (bit_xor @0 @1))
1183 /* (x ^ y) ^ (x | y) -> x & y */
1184 (simplify
1185  (bit_xor:c (bit_xor @0 @1) (bit_ior @0 @1))
1186  (bit_and @0 @1))
1188 /* (x & y) + (x ^ y) -> x | y */
1189 /* (x & y) | (x ^ y) -> x | y */
1190 /* (x & y) ^ (x ^ y) -> x | y */
1191 (for op (plus bit_ior bit_xor)
1192  (simplify
1193   (op:c (bit_and @0 @1) (bit_xor @0 @1))
1194   (bit_ior @0 @1)))
1196 /* (x & y) + (x | y) -> x + y */
1197 (simplify
1198  (plus:c (bit_and @0 @1) (bit_ior @0 @1))
1199  (plus @0 @1))
1201 /* (x + y) - (x | y) -> x & y */
1202 (simplify
1203  (minus (plus @0 @1) (bit_ior @0 @1))
1204  (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1205       && !TYPE_SATURATING (type))
1206   (bit_and @0 @1)))
1208 /* (x + y) - (x & y) -> x | y */
1209 (simplify
1210  (minus (plus @0 @1) (bit_and @0 @1))
1211  (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1212       && !TYPE_SATURATING (type))
1213   (bit_ior @0 @1)))
1215 /* (x | y) - y -> (x & ~y) */
1216 (simplify
1217  (minus (bit_ior:cs @0 @1) @1)
1218  (bit_and @0 (bit_not @1)))
1220 /* (x | y) - (x ^ y) -> x & y */
1221 (simplify
1222  (minus (bit_ior @0 @1) (bit_xor @0 @1))
1223  (bit_and @0 @1))
1225 /* (x | y) - (x & y) -> x ^ y */
1226 (simplify
1227  (minus (bit_ior @0 @1) (bit_and @0 @1))
1228  (bit_xor @0 @1))
1230 /* (x | y) & ~(x & y) -> x ^ y */
1231 (simplify
1232  (bit_and:c (bit_ior @0 @1) (bit_not (bit_and @0 @1)))
1233  (bit_xor @0 @1))
1235 /* (x | y) & (~x ^ y) -> x & y */
1236 (simplify
1237  (bit_and:c (bit_ior:c @0 @1) (bit_xor:c @1 (bit_not @0)))
1238  (bit_and @0 @1))
1240 /* (~x | y) & (x | ~y) -> ~(x ^ y) */
1241 (simplify
1242  (bit_and (bit_ior:cs (bit_not @0) @1) (bit_ior:cs @0 (bit_not @1)))
1243  (bit_not (bit_xor @0 @1)))
1245 /* (~x | y) ^ (x | ~y) -> x ^ y */
1246 (simplify
1247  (bit_xor (bit_ior:c (bit_not @0) @1) (bit_ior:c @0 (bit_not @1)))
1248  (bit_xor @0 @1))
1250 /* ((x & y) - (x | y)) - 1 -> ~(x ^ y) */
1251 (simplify
1252  (plus (nop_convert1? (minus@2 (nop_convert2? (bit_and:c @0 @1))
1253                               (nop_convert2? (bit_ior @0 @1))))
1254        integer_all_onesp)
1255  (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1256       && !TYPE_SATURATING (type) && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2))
1257       && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@2))
1258       && !TYPE_SATURATING (TREE_TYPE (@2)))
1259  (bit_not (convert (bit_xor @0 @1)))))
1260 (simplify
1261  (minus (nop_convert1? (plus@2 (nop_convert2? (bit_and:c @0 @1))
1262                                integer_all_onesp))
1263        (nop_convert3? (bit_ior @0 @1)))
1264  (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1265       && !TYPE_SATURATING (type) && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2))
1266       && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@2))
1267       && !TYPE_SATURATING (TREE_TYPE (@2)))
1268  (bit_not (convert (bit_xor @0 @1)))))
1269 (simplify
1270  (minus (nop_convert1? (bit_and @0 @1))
1271        (nop_convert2? (plus@2 (nop_convert3? (bit_ior:c @0 @1))
1272                                integer_onep)))
1273  (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1274       && !TYPE_SATURATING (type) && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2))
1275       && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@2))
1276       && !TYPE_SATURATING (TREE_TYPE (@2)))
1277  (bit_not (convert (bit_xor @0 @1)))))
1279 /* ~x & ~y -> ~(x | y)
1280    ~x | ~y -> ~(x & y) */
1281 (for op (bit_and bit_ior)
1282      rop (bit_ior bit_and)
1283  (simplify
1284   (op (convert1? (bit_not @0)) (convert2? (bit_not @1)))
1285   (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1286        && element_precision (type) <= element_precision (TREE_TYPE (@1)))
1287    (bit_not (rop (convert @0) (convert @1))))))
1289 /* If we are XORing or adding two BIT_AND_EXPR's, both of which are and'ing
1290    with a constant, and the two constants have no bits in common,
1291    we should treat this as a BIT_IOR_EXPR since this may produce more
1292    simplifications.  */
1293 (for op (bit_xor plus)
1294  (simplify
1295   (op (convert1? (bit_and@4 @0 INTEGER_CST@1))
1296       (convert2? (bit_and@5 @2 INTEGER_CST@3)))
1297   (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
1298        && tree_nop_conversion_p (type, TREE_TYPE (@2))
1299        && (wi::to_wide (@1) & wi::to_wide (@3)) == 0)
1300    (bit_ior (convert @4) (convert @5)))))
1302 /* (X | Y) ^ X -> Y & ~ X*/
1303 (simplify
1304  (bit_xor:c (convert1? (bit_ior:c @@0 @1)) (convert2? @0))
1305  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1306   (convert (bit_and @1 (bit_not @0)))))
1308 /* Convert ~X ^ ~Y to X ^ Y.  */
1309 (simplify
1310  (bit_xor (convert1? (bit_not @0)) (convert2? (bit_not @1)))
1311  (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1312       && element_precision (type) <= element_precision (TREE_TYPE (@1)))
1313   (bit_xor (convert @0) (convert @1))))
1315 /* Convert ~X ^ C to X ^ ~C.  */
1316 (simplify
1317  (bit_xor (convert? (bit_not @0)) INTEGER_CST@1)
1318  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1319   (bit_xor (convert @0) (bit_not @1))))
1321 /* Fold (X & Y) ^ Y and (X ^ Y) & Y as ~X & Y.  */
1322 (for opo (bit_and bit_xor)
1323      opi (bit_xor bit_and)
1324  (simplify
1325   (opo:c (opi:cs @0 @1) @1)
1326   (bit_and (bit_not @0) @1)))
1328 /* Given a bit-wise operation CODE applied to ARG0 and ARG1, see if both
1329    operands are another bit-wise operation with a common input.  If so,
1330    distribute the bit operations to save an operation and possibly two if
1331    constants are involved.  For example, convert
1332      (A | B) & (A | C) into A | (B & C)
1333    Further simplification will occur if B and C are constants.  */
1334 (for op (bit_and bit_ior bit_xor)
1335      rop (bit_ior bit_and bit_and)
1336  (simplify
1337   (op (convert? (rop:c @@0 @1)) (convert? (rop:c @0 @2)))
1338   (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1339        && tree_nop_conversion_p (type, TREE_TYPE (@2)))
1340    (rop (convert @0) (op (convert @1) (convert @2))))))
1342 /* Some simple reassociation for bit operations, also handled in reassoc.  */
1343 /* (X & Y) & Y -> X & Y
1344    (X | Y) | Y -> X | Y  */
1345 (for op (bit_and bit_ior)
1346  (simplify
1347   (op:c (convert1?@2 (op:c @0 @@1)) (convert2? @1))
1348   @2))
1349 /* (X ^ Y) ^ Y -> X  */
1350 (simplify
1351  (bit_xor:c (convert1? (bit_xor:c @0 @@1)) (convert2? @1))
1352  (convert @0))
1353 /* (X & Y) & (X & Z) -> (X & Y) & Z
1354    (X | Y) | (X | Z) -> (X | Y) | Z  */
1355 (for op (bit_and bit_ior)
1356  (simplify
1357   (op (convert1?@3 (op:c@4 @0 @1)) (convert2?@5 (op:c@6 @0 @2)))
1358   (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1359        && tree_nop_conversion_p (type, TREE_TYPE (@2)))
1360    (if (single_use (@5) && single_use (@6))
1361     (op @3 (convert @2))
1362     (if (single_use (@3) && single_use (@4))
1363      (op (convert @1) @5))))))
1364 /* (X ^ Y) ^ (X ^ Z) -> Y ^ Z  */
1365 (simplify
1366  (bit_xor (convert1? (bit_xor:c @0 @1)) (convert2? (bit_xor:c @0 @2)))
1367  (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1368       && tree_nop_conversion_p (type, TREE_TYPE (@2)))
1369   (bit_xor (convert @1) (convert @2))))
1371 /* Convert abs (abs (X)) into abs (X).
1372    also absu (absu (X)) into absu (X).  */
1373 (simplify
1374  (abs (abs@1 @0))
1375  @1)
1377 (simplify
1378  (absu (convert@2 (absu@1 @0)))
1379  (if (tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@1)))
1380   @1))
1382 /* Convert abs[u] (-X) -> abs[u] (X).  */
1383 (simplify
1384  (abs (negate @0))
1385  (abs @0))
1387 (simplify
1388  (absu (negate @0))
1389  (absu @0))
1391 /* Convert abs[u] (X)  where X is nonnegative -> (X).  */
1392 (simplify
1393  (abs tree_expr_nonnegative_p@0)
1394  @0)
1396 (simplify
1397  (absu tree_expr_nonnegative_p@0)
1398  (convert @0))
1400 /* Simplify (-(X < 0) | 1) * X into abs (X).  */
1401 (simplify
1402  (mult:c (bit_ior (negate (convert? (lt @0 integer_zerop))) integer_onep) @0)
1403  (if (INTEGRAL_TYPE_P (type) && !TYPE_UNSIGNED (type))
1404   (abs @0)))
1406 /* Similarly (-(X < 0) | 1U) * X into absu (X).  */
1407 (simplify
1408  (mult:c (bit_ior (nop_convert (negate (convert? (lt @0 integer_zerop))))
1409                   integer_onep) (nop_convert @0))
1410  (if (INTEGRAL_TYPE_P (type)
1411       && TYPE_UNSIGNED (type)
1412       && INTEGRAL_TYPE_P (TREE_TYPE (@0))
1413       && !TYPE_UNSIGNED (TREE_TYPE (@0)))
1414   (absu @0)))
1416 /* A few cases of fold-const.c negate_expr_p predicate.  */
1417 (match negate_expr_p
1418  INTEGER_CST
1419  (if ((INTEGRAL_TYPE_P (type)
1420        && TYPE_UNSIGNED (type))
1421       || (!TYPE_OVERFLOW_SANITIZED (type)
1422           && may_negate_without_overflow_p (t)))))
1423 (match negate_expr_p
1424  FIXED_CST)
1425 (match negate_expr_p
1426  (negate @0)
1427  (if (!TYPE_OVERFLOW_SANITIZED (type))))
1428 (match negate_expr_p
1429  REAL_CST
1430  (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (t)))))
1431 /* VECTOR_CST handling of non-wrapping types would recurse in unsupported
1432    ways.  */
1433 (match negate_expr_p
1434  VECTOR_CST
1435  (if (FLOAT_TYPE_P (TREE_TYPE (type)) || TYPE_OVERFLOW_WRAPS (type))))
1436 (match negate_expr_p
1437  (minus @0 @1)
1438  (if ((ANY_INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_WRAPS (type))
1439       || (FLOAT_TYPE_P (type)
1440           && !HONOR_SIGN_DEPENDENT_ROUNDING (type)
1441           && !HONOR_SIGNED_ZEROS (type)))))
1443 /* (-A) * (-B) -> A * B  */
1444 (simplify
1445  (mult:c (convert1? (negate @0)) (convert2? negate_expr_p@1))
1446   (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
1447        && tree_nop_conversion_p (type, TREE_TYPE (@1)))
1448    (mult (convert @0) (convert (negate @1)))))
1450 /* -(A + B) -> (-B) - A.  */
1451 (simplify
1452  (negate (plus:c @0 negate_expr_p@1))
1453  (if (!HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type))
1454       && !HONOR_SIGNED_ZEROS (element_mode (type)))
1455   (minus (negate @1) @0)))
1457 /* -(A - B) -> B - A.  */
1458 (simplify
1459  (negate (minus @0 @1))
1460  (if ((ANY_INTEGRAL_TYPE_P (type) && !TYPE_OVERFLOW_SANITIZED (type))
1461       || (FLOAT_TYPE_P (type)
1462           && !HONOR_SIGN_DEPENDENT_ROUNDING (type)
1463           && !HONOR_SIGNED_ZEROS (type)))
1464   (minus @1 @0)))
1465 (simplify
1466  (negate (pointer_diff @0 @1))
1467  (if (TYPE_OVERFLOW_UNDEFINED (type))
1468   (pointer_diff @1 @0)))
1470 /* A - B -> A + (-B) if B is easily negatable.  */
1471 (simplify
1472  (minus @0 negate_expr_p@1)
1473  (if (!FIXED_POINT_TYPE_P (type))
1474  (plus @0 (negate @1))))
1476 /* Try to fold (type) X op CST -> (type) (X op ((type-x) CST))
1477    when profitable.
1478    For bitwise binary operations apply operand conversions to the
1479    binary operation result instead of to the operands.  This allows
1480    to combine successive conversions and bitwise binary operations.
1481    We combine the above two cases by using a conditional convert.  */
1482 (for bitop (bit_and bit_ior bit_xor)
1483  (simplify
1484   (bitop (convert@2 @0) (convert?@3 @1))
1485   (if (((TREE_CODE (@1) == INTEGER_CST
1486          && INTEGRAL_TYPE_P (TREE_TYPE (@0))
1487          && int_fits_type_p (@1, TREE_TYPE (@0)))
1488         || types_match (@0, @1))
1489        /* ???  This transform conflicts with fold-const.c doing
1490           Convert (T)(x & c) into (T)x & (T)c, if c is an integer
1491           constants (if x has signed type, the sign bit cannot be set
1492           in c).  This folds extension into the BIT_AND_EXPR.
1493           Restrict it to GIMPLE to avoid endless recursions.  */
1494        && (bitop != BIT_AND_EXPR || GIMPLE)
1495        && (/* That's a good idea if the conversion widens the operand, thus
1496               after hoisting the conversion the operation will be narrower.  */
1497            TYPE_PRECISION (TREE_TYPE (@0)) < TYPE_PRECISION (type)
1498            /* It's also a good idea if the conversion is to a non-integer
1499               mode.  */
1500            || GET_MODE_CLASS (TYPE_MODE (type)) != MODE_INT
1501            /* Or if the precision of TO is not the same as the precision
1502               of its mode.  */
1503            || !type_has_mode_precision_p (type)
1504            /* In GIMPLE, getting rid of 2 conversions for one new results
1505               in smaller IL.  */
1506            || (GIMPLE
1507                && TREE_CODE (@1) != INTEGER_CST
1508                && tree_nop_conversion_p (type, TREE_TYPE (@0))
1509                && single_use (@2)
1510                && single_use (@3))))
1511    (convert (bitop @0 (convert @1)))))
1512  /* In GIMPLE, getting rid of 2 conversions for one new results
1513     in smaller IL.  */
1514  (simplify
1515   (convert (bitop:cs@2 (nop_convert:s @0) @1))
1516   (if (GIMPLE
1517        && TREE_CODE (@1) != INTEGER_CST
1518        && tree_nop_conversion_p (type, TREE_TYPE (@2))
1519        && types_match (type, @0))
1520    (bitop @0 (convert @1)))))
1522 (for bitop (bit_and bit_ior)
1523      rbitop (bit_ior bit_and)
1524   /* (x | y) & x -> x */
1525   /* (x & y) | x -> x */
1526  (simplify
1527   (bitop:c (rbitop:c @0 @1) @0)
1528   @0)
1529  /* (~x | y) & x -> x & y */
1530  /* (~x & y) | x -> x | y */
1531  (simplify
1532   (bitop:c (rbitop:c (bit_not @0) @1) @0)
1533   (bitop @0 @1)))
1535 /* ((x | y) & z) | x -> (z & y) | x */
1536 (simplify
1537   (bit_ior:c (bit_and:cs (bit_ior:cs @0 @1) @2) @0)
1538   (bit_ior (bit_and @2 @1) @0))
1540 /* (x | CST1) & CST2 -> (x & CST2) | (CST1 & CST2) */
1541 (simplify
1542   (bit_and (bit_ior @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
1543   (bit_ior (bit_and @0 @2) (bit_and @1 @2)))
1545 /* Combine successive equal operations with constants.  */
1546 (for bitop (bit_and bit_ior bit_xor)
1547  (simplify
1548   (bitop (bitop @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
1549   (if (!CONSTANT_CLASS_P (@0))
1550    /* This is the canonical form regardless of whether (bitop @1 @2) can be
1551       folded to a constant.  */
1552    (bitop @0 (bitop @1 @2))
1553    /* In this case we have three constants and (bitop @0 @1) doesn't fold
1554       to a constant.  This can happen if @0 or @1 is a POLY_INT_CST and if
1555       the values involved are such that the operation can't be decided at
1556       compile time.  Try folding one of @0 or @1 with @2 to see whether
1557       that combination can be decided at compile time.
1559       Keep the existing form if both folds fail, to avoid endless
1560       oscillation.  */
1561    (with { tree cst1 = const_binop (bitop, type, @0, @2); }
1562     (if (cst1)
1563      (bitop @1 { cst1; })
1564      (with { tree cst2 = const_binop (bitop, type, @1, @2); }
1565       (if (cst2)
1566        (bitop @0 { cst2; }))))))))
1568 /* Try simple folding for X op !X, and X op X with the help
1569    of the truth_valued_p and logical_inverted_value predicates.  */
1570 (match truth_valued_p
1571  @0
1572  (if (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1)))
1573 (for op (tcc_comparison truth_and truth_andif truth_or truth_orif truth_xor)
1574  (match truth_valued_p
1575   (op @0 @1)))
1576 (match truth_valued_p
1577   (truth_not @0))
1579 (match (logical_inverted_value @0)
1580  (truth_not @0))
1581 (match (logical_inverted_value @0)
1582  (bit_not truth_valued_p@0))
1583 (match (logical_inverted_value @0)
1584  (eq @0 integer_zerop))
1585 (match (logical_inverted_value @0)
1586  (ne truth_valued_p@0 integer_truep))
1587 (match (logical_inverted_value @0)
1588  (bit_xor truth_valued_p@0 integer_truep))
1590 /* X & !X -> 0.  */
1591 (simplify
1592  (bit_and:c @0 (logical_inverted_value @0))
1593  { build_zero_cst (type); })
1594 /* X | !X and X ^ !X -> 1, , if X is truth-valued.  */
1595 (for op (bit_ior bit_xor)
1596  (simplify
1597   (op:c truth_valued_p@0 (logical_inverted_value @0))
1598   { constant_boolean_node (true, type); }))
1599 /* X ==/!= !X is false/true.  */
1600 (for op (eq ne)
1601  (simplify
1602   (op:c truth_valued_p@0 (logical_inverted_value @0))
1603   { constant_boolean_node (op == NE_EXPR ? true : false, type); }))
1605 /* ~~x -> x */
1606 (simplify
1607   (bit_not (bit_not @0))
1608   @0)
1610 /* Convert ~ (-A) to A - 1.  */
1611 (simplify
1612  (bit_not (convert? (negate @0)))
1613  (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1614       || !TYPE_UNSIGNED (TREE_TYPE (@0)))
1615   (convert (minus @0 { build_each_one_cst (TREE_TYPE (@0)); }))))
1617 /* Convert - (~A) to A + 1.  */
1618 (simplify
1619  (negate (nop_convert? (bit_not @0)))
1620  (plus (view_convert @0) { build_each_one_cst (type); }))
1622 /* Convert ~ (A - 1) or ~ (A + -1) to -A.  */
1623 (simplify
1624  (bit_not (convert? (minus @0 integer_each_onep)))
1625  (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1626       || !TYPE_UNSIGNED (TREE_TYPE (@0)))
1627   (convert (negate @0))))
1628 (simplify
1629  (bit_not (convert? (plus @0 integer_all_onesp)))
1630  (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1631       || !TYPE_UNSIGNED (TREE_TYPE (@0)))
1632   (convert (negate @0))))
1634 /* Part of convert ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify.  */
1635 (simplify
1636  (bit_not (convert? (bit_xor @0 INTEGER_CST@1)))
1637  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1638   (convert (bit_xor @0 (bit_not @1)))))
1639 (simplify
1640  (bit_not (convert? (bit_xor:c (bit_not @0) @1)))
1641  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1642   (convert (bit_xor @0 @1))))
1644 /* Otherwise prefer ~(X ^ Y) to ~X ^ Y as more canonical.  */
1645 (simplify
1646  (bit_xor:c (nop_convert?:s (bit_not:s @0)) @1)
1647  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1648   (bit_not (bit_xor (view_convert @0) @1))))
1650 /* (x & ~m) | (y & m) -> ((x ^ y) & m) ^ x */
1651 (simplify
1652  (bit_ior:c (bit_and:cs @0 (bit_not @2)) (bit_and:cs @1 @2))
1653  (bit_xor (bit_and (bit_xor @0 @1) @2) @0))
1655 /* Fold A - (A & B) into ~B & A.  */
1656 (simplify
1657  (minus (convert1? @0) (convert2?:s (bit_and:cs @@0 @1)))
1658  (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
1659       && tree_nop_conversion_p (type, TREE_TYPE (@1)))
1660   (convert (bit_and (bit_not @1) @0))))
1662 /* (m1 CMP m2) * d -> (m1 CMP m2) ? d : 0  */
1663 (for cmp (gt lt ge le)
1664 (simplify
1665  (mult (convert (cmp @0 @1)) @2)
1666   (if (GIMPLE || !TREE_SIDE_EFFECTS (@2))
1667    (cond (cmp @0 @1) @2 { build_zero_cst (type); }))))
1669 /* For integral types with undefined overflow and C != 0 fold
1670    x * C EQ/NE y * C into x EQ/NE y.  */
1671 (for cmp (eq ne)
1672  (simplify
1673   (cmp (mult:c @0 @1) (mult:c @2 @1))
1674   (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
1675        && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1676        && tree_expr_nonzero_p (@1))
1677    (cmp @0 @2))))
1679 /* For integral types with wrapping overflow and C odd fold
1680    x * C EQ/NE y * C into x EQ/NE y.  */
1681 (for cmp (eq ne)
1682  (simplify
1683   (cmp (mult @0 INTEGER_CST@1) (mult @2 @1))
1684   (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
1685        && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))
1686        && (TREE_INT_CST_LOW (@1) & 1) != 0)
1687    (cmp @0 @2))))
1689 /* For integral types with undefined overflow and C != 0 fold
1690    x * C RELOP y * C into:
1692    x RELOP y for nonnegative C
1693    y RELOP x for negative C  */
1694 (for cmp (lt gt le ge)
1695  (simplify
1696   (cmp (mult:c @0 @1) (mult:c @2 @1))
1697   (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
1698        && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1699    (if (tree_expr_nonnegative_p (@1) && tree_expr_nonzero_p (@1))
1700     (cmp @0 @2)
1701    (if (TREE_CODE (@1) == INTEGER_CST
1702         && wi::neg_p (wi::to_wide (@1), TYPE_SIGN (TREE_TYPE (@1))))
1703     (cmp @2 @0))))))
1705 /* (X - 1U) <= INT_MAX-1U into (int) X > 0.  */
1706 (for cmp (le gt)
1707      icmp (gt le)
1708  (simplify
1709   (cmp (plus @0 integer_minus_onep@1) INTEGER_CST@2)
1710    (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1711         && TYPE_UNSIGNED (TREE_TYPE (@0))
1712         && TYPE_PRECISION (TREE_TYPE (@0)) > 1
1713         && (wi::to_wide (@2)
1714             == wi::max_value (TYPE_PRECISION (TREE_TYPE (@0)), SIGNED) - 1))
1715     (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
1716      (icmp (convert:stype @0) { build_int_cst (stype, 0); })))))
1718 /* X / 4 < Y / 4 iff X < Y when the division is known to be exact.  */
1719 (for cmp (simple_comparison)
1720  (simplify
1721   (cmp (convert?@3 (exact_div @0 INTEGER_CST@2)) (convert? (exact_div @1 @2)))
1722   (if (element_precision (@3) >= element_precision (@0)
1723        && types_match (@0, @1))
1724    (if (wi::lt_p (wi::to_wide (@2), 0, TYPE_SIGN (TREE_TYPE (@2))))
1725     (if (!TYPE_UNSIGNED (TREE_TYPE (@3)))
1726      (cmp @1 @0)
1727      (if (tree_expr_nonzero_p (@0) && tree_expr_nonzero_p (@1))
1728       (with
1729        {
1730         tree utype = unsigned_type_for (TREE_TYPE (@0));
1731        }
1732        (cmp (convert:utype @1) (convert:utype @0)))))
1733     (if (wi::gt_p (wi::to_wide (@2), 1, TYPE_SIGN (TREE_TYPE (@2))))
1734      (if (TYPE_UNSIGNED (TREE_TYPE (@0)) || !TYPE_UNSIGNED (TREE_TYPE (@3)))
1735       (cmp @0 @1)
1736       (with
1737        {
1738         tree utype = unsigned_type_for (TREE_TYPE (@0));
1739        }
1740        (cmp (convert:utype @0) (convert:utype @1)))))))))
1742 /* X / C1 op C2 into a simple range test.  */
1743 (for cmp (simple_comparison)
1744  (simplify
1745   (cmp (trunc_div:s @0 INTEGER_CST@1) INTEGER_CST@2)
1746   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1747        && integer_nonzerop (@1)
1748        && !TREE_OVERFLOW (@1)
1749        && !TREE_OVERFLOW (@2))
1750    (with { tree lo, hi; bool neg_overflow;
1751            enum tree_code code = fold_div_compare (cmp, @1, @2, &lo, &hi,
1752                                                    &neg_overflow); }
1753     (switch
1754      (if (code == LT_EXPR || code == GE_EXPR)
1755        (if (TREE_OVERFLOW (lo))
1756         { build_int_cst (type, (code == LT_EXPR) ^ neg_overflow); }
1757         (if (code == LT_EXPR)
1758          (lt @0 { lo; })
1759          (ge @0 { lo; }))))
1760      (if (code == LE_EXPR || code == GT_EXPR)
1761        (if (TREE_OVERFLOW (hi))
1762         { build_int_cst (type, (code == LE_EXPR) ^ neg_overflow); }
1763         (if (code == LE_EXPR)
1764          (le @0 { hi; })
1765          (gt @0 { hi; }))))
1766      (if (!lo && !hi)
1767       { build_int_cst (type, code == NE_EXPR); })
1768      (if (code == EQ_EXPR && !hi)
1769       (ge @0 { lo; }))
1770      (if (code == EQ_EXPR && !lo)
1771       (le @0 { hi; }))
1772      (if (code == NE_EXPR && !hi)
1773       (lt @0 { lo; }))
1774      (if (code == NE_EXPR && !lo)
1775       (gt @0 { hi; }))
1776      (if (GENERIC)
1777       { build_range_check (UNKNOWN_LOCATION, type, @0, code == EQ_EXPR,
1778                            lo, hi); })
1779      (with
1780       {
1781         tree etype = range_check_type (TREE_TYPE (@0));
1782         if (etype)
1783           {
1784             hi = fold_convert (etype, hi);
1785             lo = fold_convert (etype, lo);
1786             hi = const_binop (MINUS_EXPR, etype, hi, lo);
1787           }
1788       }
1789       (if (etype && hi && !TREE_OVERFLOW (hi))
1790        (if (code == EQ_EXPR)
1791         (le (minus (convert:etype @0) { lo; }) { hi; })
1792         (gt (minus (convert:etype @0) { lo; }) { hi; })))))))))
1794 /* X + Z < Y + Z is the same as X < Y when there is no overflow.  */
1795 (for op (lt le ge gt)
1796  (simplify
1797   (op (plus:c @0 @2) (plus:c @1 @2))
1798   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1799        && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1800    (op @0 @1))))
1801 /* For equality and subtraction, this is also true with wrapping overflow.  */
1802 (for op (eq ne minus)
1803  (simplify
1804   (op (plus:c @0 @2) (plus:c @1 @2))
1805   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1806        && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1807            || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
1808    (op @0 @1))))
1810 /* X - Z < Y - Z is the same as X < Y when there is no overflow.  */
1811 (for op (lt le ge gt)
1812  (simplify
1813   (op (minus @0 @2) (minus @1 @2))
1814   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1815        && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1816    (op @0 @1))))
1817 /* For equality and subtraction, this is also true with wrapping overflow.  */
1818 (for op (eq ne minus)
1819  (simplify
1820   (op (minus @0 @2) (minus @1 @2))
1821   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1822        && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1823            || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
1824    (op @0 @1))))
1825 /* And for pointers...  */
1826 (for op (simple_comparison)
1827  (simplify
1828   (op (pointer_diff@3 @0 @2) (pointer_diff @1 @2))
1829   (if (!TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
1830    (op @0 @1))))
1831 (simplify
1832  (minus (pointer_diff@3 @0 @2) (pointer_diff @1 @2))
1833  (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@3))
1834       && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
1835   (pointer_diff @0 @1)))
1837 /* Z - X < Z - Y is the same as Y < X when there is no overflow.  */
1838 (for op (lt le ge gt)
1839  (simplify
1840   (op (minus @2 @0) (minus @2 @1))
1841   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1842        && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
1843    (op @1 @0))))
1844 /* For equality and subtraction, this is also true with wrapping overflow.  */
1845 (for op (eq ne minus)
1846  (simplify
1847   (op (minus @2 @0) (minus @2 @1))
1848   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1849        && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1850            || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
1851    (op @1 @0))))
1852 /* And for pointers...  */
1853 (for op (simple_comparison)
1854  (simplify
1855   (op (pointer_diff@3 @2 @0) (pointer_diff @2 @1))
1856   (if (!TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
1857    (op @1 @0))))
1858 (simplify
1859  (minus (pointer_diff@3 @2 @0) (pointer_diff @2 @1))
1860  (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@3))
1861       && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
1862   (pointer_diff @1 @0)))
1864 /* X + Y < Y is the same as X < 0 when there is no overflow.  */
1865 (for op (lt le gt ge)
1866  (simplify
1867   (op:c (plus:c@2 @0 @1) @1)
1868   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1869        && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1870        && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0))
1871        && (CONSTANT_CLASS_P (@0) || single_use (@2)))
1872    (op @0 { build_zero_cst (TREE_TYPE (@0)); }))))
1873 /* For equality, this is also true with wrapping overflow.  */
1874 (for op (eq ne)
1875  (simplify
1876   (op:c (nop_convert?@3 (plus:c@2 @0 (convert1? @1))) (convert2? @1))
1877   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1878        && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1879            || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
1880        && (CONSTANT_CLASS_P (@0) || (single_use (@2) && single_use (@3)))
1881        && tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@2))
1882        && tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@1)))
1883    (op @0 { build_zero_cst (TREE_TYPE (@0)); })))
1884  (simplify
1885   (op:c (nop_convert?@3 (pointer_plus@2 (convert1? @0) @1)) (convert2? @0))
1886   (if (tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@0))
1887        && tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0))
1888        && (CONSTANT_CLASS_P (@1) || (single_use (@2) && single_use (@3))))
1889    (op @1 { build_zero_cst (TREE_TYPE (@1)); }))))
1891 /* X - Y < X is the same as Y > 0 when there is no overflow.
1892    For equality, this is also true with wrapping overflow.  */
1893 (for op (simple_comparison)
1894  (simplify
1895   (op:c @0 (minus@2 @0 @1))
1896   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
1897        && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
1898            || ((op == EQ_EXPR || op == NE_EXPR)
1899                && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
1900        && (CONSTANT_CLASS_P (@1) || single_use (@2)))
1901    (op @1 { build_zero_cst (TREE_TYPE (@1)); }))))
1903 /* Transform:
1904    (X / Y) == 0 -> X < Y if X, Y are unsigned.
1905    (X / Y) != 0 -> X >= Y, if X, Y are unsigned.  */
1906 (for cmp (eq ne)
1907      ocmp (lt ge)
1908  (simplify
1909   (cmp (trunc_div @0 @1) integer_zerop)
1910   (if (TYPE_UNSIGNED (TREE_TYPE (@0))
1911        /* Complex ==/!= is allowed, but not </>=.  */
1912        && TREE_CODE (TREE_TYPE (@0)) != COMPLEX_TYPE
1913        && (VECTOR_TYPE_P (type) || !VECTOR_TYPE_P (TREE_TYPE (@0))))
1914    (ocmp @0 @1))))
1916 /* X == C - X can never be true if C is odd.  */
1917 (for cmp (eq ne)
1918  (simplify
1919   (cmp:c (convert? @0) (convert1? (minus INTEGER_CST@1 (convert2? @0))))
1920   (if (TREE_INT_CST_LOW (@1) & 1)
1921    { constant_boolean_node (cmp == NE_EXPR, type); })))
1923 /* Arguments on which one can call get_nonzero_bits to get the bits
1924    possibly set.  */
1925 (match with_possible_nonzero_bits
1926  INTEGER_CST@0)
1927 (match with_possible_nonzero_bits
1928  SSA_NAME@0
1929  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))))
1930 /* Slightly extended version, do not make it recursive to keep it cheap.  */
1931 (match (with_possible_nonzero_bits2 @0)
1932  with_possible_nonzero_bits@0)
1933 (match (with_possible_nonzero_bits2 @0)
1934  (bit_and:c with_possible_nonzero_bits@0 @2))
1936 /* Same for bits that are known to be set, but we do not have
1937    an equivalent to get_nonzero_bits yet.  */
1938 (match (with_certain_nonzero_bits2 @0)
1939  INTEGER_CST@0)
1940 (match (with_certain_nonzero_bits2 @0)
1941  (bit_ior @1 INTEGER_CST@0))
1943 /* X == C (or X & Z == Y | C) is impossible if ~nonzero(X) & C != 0.  */
1944 (for cmp (eq ne)
1945  (simplify
1946   (cmp:c (with_possible_nonzero_bits2 @0) (with_certain_nonzero_bits2 @1))
1947   (if (wi::bit_and_not (wi::to_wide (@1), get_nonzero_bits (@0)) != 0)
1948    { constant_boolean_node (cmp == NE_EXPR, type); })))
1950 /* ((X inner_op C0) outer_op C1)
1951    With X being a tree where value_range has reasoned certain bits to always be
1952    zero throughout its computed value range,
1953    inner_op = {|,^}, outer_op = {|,^} and inner_op != outer_op
1954    where zero_mask has 1's for all bits that are sure to be 0 in
1955    and 0's otherwise.
1956    if (inner_op == '^') C0 &= ~C1;
1957    if ((C0 & ~zero_mask) == 0) then emit (X outer_op (C0 outer_op C1)
1958    if ((C1 & ~zero_mask) == 0) then emit (X inner_op (C0 outer_op C1)
1960 (for inner_op (bit_ior bit_xor)
1961      outer_op (bit_xor bit_ior)
1962 (simplify
1963  (outer_op
1964   (inner_op:s @2 INTEGER_CST@0) INTEGER_CST@1)
1965  (with
1966   {
1967     bool fail = false;
1968     wide_int zero_mask_not;
1969     wide_int C0;
1970     wide_int cst_emit;
1972     if (TREE_CODE (@2) == SSA_NAME)
1973       zero_mask_not = get_nonzero_bits (@2);
1974     else
1975       fail = true;
1977     if (inner_op == BIT_XOR_EXPR)
1978       {
1979         C0 = wi::bit_and_not (wi::to_wide (@0), wi::to_wide (@1));
1980         cst_emit = C0 | wi::to_wide (@1);
1981       }
1982     else
1983       {
1984         C0 = wi::to_wide (@0);
1985         cst_emit = C0 ^ wi::to_wide (@1);
1986       }
1987   }
1988   (if (!fail && (C0 & zero_mask_not) == 0)
1989    (outer_op @2 { wide_int_to_tree (type, cst_emit); })
1990    (if (!fail && (wi::to_wide (@1) & zero_mask_not) == 0)
1991     (inner_op @2 { wide_int_to_tree (type, cst_emit); }))))))
1993 /* Associate (p +p off1) +p off2 as (p +p (off1 + off2)).  */
1994 (simplify
1995   (pointer_plus (pointer_plus:s @0 @1) @3)
1996   (pointer_plus @0 (plus @1 @3)))
1998 /* Pattern match
1999      tem1 = (long) ptr1;
2000      tem2 = (long) ptr2;
2001      tem3 = tem2 - tem1;
2002      tem4 = (unsigned long) tem3;
2003      tem5 = ptr1 + tem4;
2004    and produce
2005      tem5 = ptr2;  */
2006 (simplify
2007   (pointer_plus @0 (convert?@2 (minus@3 (convert @1) (convert @0))))
2008   /* Conditionally look through a sign-changing conversion.  */
2009   (if (TYPE_PRECISION (TREE_TYPE (@2)) == TYPE_PRECISION (TREE_TYPE (@3))
2010        && ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@1)))
2011             || (GENERIC && type == TREE_TYPE (@1))))
2012    @1))
2013 (simplify
2014   (pointer_plus @0 (convert?@2 (pointer_diff@3 @1 @@0)))
2015   (if (TYPE_PRECISION (TREE_TYPE (@2)) >= TYPE_PRECISION (TREE_TYPE (@3)))
2016    (convert @1)))
2018 /* Pattern match
2019      tem = (sizetype) ptr;
2020      tem = tem & algn;
2021      tem = -tem;
2022      ... = ptr p+ tem;
2023    and produce the simpler and easier to analyze with respect to alignment
2024      ... = ptr & ~algn;  */
2025 (simplify
2026   (pointer_plus @0 (negate (bit_and (convert @0) INTEGER_CST@1)))
2027   (with { tree algn = wide_int_to_tree (TREE_TYPE (@0), ~wi::to_wide (@1)); }
2028    (bit_and @0 { algn; })))
2030 /* Try folding difference of addresses.  */
2031 (simplify
2032  (minus (convert ADDR_EXPR@0) (convert @1))
2033  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2034   (with { poly_int64 diff; }
2035    (if (ptr_difference_const (@0, @1, &diff))
2036     { build_int_cst_type (type, diff); }))))
2037 (simplify
2038  (minus (convert @0) (convert ADDR_EXPR@1))
2039  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2040   (with { poly_int64 diff; }
2041    (if (ptr_difference_const (@0, @1, &diff))
2042     { build_int_cst_type (type, diff); }))))
2043 (simplify
2044  (pointer_diff (convert?@2 ADDR_EXPR@0) (convert1?@3 @1))
2045  (if (tree_nop_conversion_p (TREE_TYPE(@2), TREE_TYPE (@0))
2046       && tree_nop_conversion_p (TREE_TYPE(@3), TREE_TYPE (@1)))
2047   (with { poly_int64 diff; }
2048    (if (ptr_difference_const (@0, @1, &diff))
2049     { build_int_cst_type (type, diff); }))))
2050 (simplify
2051  (pointer_diff (convert?@2 @0) (convert1?@3 ADDR_EXPR@1))
2052  (if (tree_nop_conversion_p (TREE_TYPE(@2), TREE_TYPE (@0))
2053       && tree_nop_conversion_p (TREE_TYPE(@3), TREE_TYPE (@1)))
2054   (with { poly_int64 diff; }
2055    (if (ptr_difference_const (@0, @1, &diff))
2056     { build_int_cst_type (type, diff); }))))
2058 /* Canonicalize (T *)(ptr - ptr-cst) to &MEM[ptr + -ptr-cst].  */
2059 (simplify
2060  (convert (pointer_diff @0 INTEGER_CST@1))
2061  (if (POINTER_TYPE_P (type))
2062   { build_fold_addr_expr_with_type
2063       (build2 (MEM_REF, char_type_node, @0,
2064                wide_int_to_tree (ptr_type_node, wi::neg (wi::to_wide (@1)))),
2065                type); }))
2067 /* If arg0 is derived from the address of an object or function, we may
2068    be able to fold this expression using the object or function's
2069    alignment.  */
2070 (simplify
2071  (bit_and (convert? @0) INTEGER_CST@1)
2072  (if (POINTER_TYPE_P (TREE_TYPE (@0))
2073       && tree_nop_conversion_p (type, TREE_TYPE (@0)))
2074   (with
2075    {
2076      unsigned int align;
2077      unsigned HOST_WIDE_INT bitpos;
2078      get_pointer_alignment_1 (@0, &align, &bitpos);
2079    }
2080    (if (wi::ltu_p (wi::to_wide (@1), align / BITS_PER_UNIT))
2081     { wide_int_to_tree (type, (wi::to_wide (@1)
2082                                & (bitpos / BITS_PER_UNIT))); }))))
2084 (match min_value
2085  INTEGER_CST
2086  (if (INTEGRAL_TYPE_P (type)
2087       && wi::eq_p (wi::to_wide (t), wi::min_value (type)))))
2089 (match max_value
2090  INTEGER_CST
2091  (if (INTEGRAL_TYPE_P (type)
2092       && wi::eq_p (wi::to_wide (t), wi::max_value (type)))))
2094 /* x >  y  &&  x != XXX_MIN  -->  x > y
2095    x >  y  &&  x == XXX_MIN  -->  false . */
2096 (for eqne (eq ne)
2097  (simplify
2098   (bit_and:c (gt:c@2 @0 @1) (eqne @0 min_value))
2099    (switch
2100     (if (eqne == EQ_EXPR)
2101      { constant_boolean_node (false, type); })
2102     (if (eqne == NE_EXPR)
2103      @2)
2104     )))
2106 /* x <  y  &&  x != XXX_MAX  -->  x < y
2107    x <  y  &&  x == XXX_MAX  -->  false.  */
2108 (for eqne (eq ne)
2109  (simplify
2110   (bit_and:c (lt:c@2 @0 @1) (eqne @0 max_value))
2111    (switch
2112     (if (eqne == EQ_EXPR)
2113      { constant_boolean_node (false, type); })
2114     (if (eqne == NE_EXPR)
2115      @2)
2116     )))
2118 /* x <=  y  &&  x == XXX_MIN  -->  x == XXX_MIN.  */
2119 (simplify
2120  (bit_and:c (le:c @0 @1) (eq@2 @0 min_value))
2121   @2)
2123 /* x >=  y  &&  x == XXX_MAX  -->  x == XXX_MAX.  */
2124 (simplify
2125  (bit_and:c (ge:c @0 @1) (eq@2 @0 max_value))
2126   @2)
2128 /* x >  y  ||  x != XXX_MIN   -->  x != XXX_MIN.  */
2129 (simplify
2130  (bit_ior:c (gt:c @0 @1) (ne@2 @0 min_value))
2131   @2)
2133 /* x <=  y  ||  x != XXX_MIN   -->  true.  */
2134 (simplify
2135  (bit_ior:c (le:c @0 @1) (ne @0 min_value))
2136   { constant_boolean_node (true, type); })
2138 /* x <=  y  ||  x == XXX_MIN   -->  x <= y.  */
2139 (simplify
2140  (bit_ior:c (le:c@2 @0 @1) (eq @0 min_value))
2141   @2)
2143 /* x <  y  ||  x != XXX_MAX   -->  x != XXX_MAX.  */
2144 (simplify
2145  (bit_ior:c (lt:c @0 @1) (ne@2 @0 max_value))
2146   @2)
2148 /* x >=  y  ||  x != XXX_MAX   -->  true
2149    x >=  y  ||  x == XXX_MAX   -->  x >= y.  */
2150 (for eqne (eq ne)
2151  (simplify
2152   (bit_ior:c (ge:c@2 @0 @1) (eqne @0 max_value))
2153    (switch
2154     (if (eqne == EQ_EXPR)
2155      @2)
2156     (if (eqne == NE_EXPR)
2157      { constant_boolean_node (true, type); }))))
2159 /* y == XXX_MIN || x < y --> x <= y - 1 */
2160 (simplify
2161  (bit_ior:c (eq:s @1 min_value) (lt:s @0 @1))
2162   (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2163        && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
2164   (le @0 (minus @1 { build_int_cst (TREE_TYPE (@1), 1); }))))
2166 /* y != XXX_MIN && x >= y --> x > y - 1 */
2167 (simplify
2168  (bit_and:c (ne:s @1 min_value) (ge:s @0 @1))
2169   (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2170        && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
2171   (gt @0 (minus @1 { build_int_cst (TREE_TYPE (@1), 1); }))))
2173 /* Convert (X == CST1) && (X OP2 CST2) to a known value
2174    based on CST1 OP2 CST2.  Similarly for (X != CST1).  */
2176 (for code1 (eq ne)
2177  (for code2 (eq ne lt gt le ge)
2178   (simplify
2179    (bit_and:c (code1@3 @0 INTEGER_CST@1) (code2@4 @0 INTEGER_CST@2))
2180     (with
2181      {
2182       int cmp = tree_int_cst_compare (@1, @2);
2183       bool val;
2184       switch (code2)
2185          {
2186         case EQ_EXPR: val = (cmp == 0); break;
2187         case NE_EXPR: val = (cmp != 0); break;
2188         case LT_EXPR: val = (cmp < 0); break;
2189         case GT_EXPR: val = (cmp > 0); break;
2190         case LE_EXPR: val = (cmp <= 0); break;
2191         case GE_EXPR: val = (cmp >= 0); break;
2192         default: gcc_unreachable ();
2193         }
2194      }
2195      (switch
2196       (if (code1 == EQ_EXPR && val) @3)
2197       (if (code1 == EQ_EXPR && !val) { constant_boolean_node (false, type); })
2198       (if (code1 == NE_EXPR && !val) @4))))))
2200 /* Convert (X OP1 CST1) && (X OP2 CST2).  */
2202 (for code1 (lt le gt ge)
2203  (for code2 (lt le gt ge)
2204   (simplify
2205   (bit_and (code1:c@3 @0 INTEGER_CST@1) (code2:c@4 @0 INTEGER_CST@2))
2206    (with
2207     {
2208      int cmp = tree_int_cst_compare (@1, @2);
2209     }
2210     (switch
2211      /* Choose the more restrictive of two < or <= comparisons.  */
2212      (if ((code1 == LT_EXPR || code1 == LE_EXPR)
2213           && (code2 == LT_EXPR || code2 == LE_EXPR))
2214       (if ((cmp < 0) || (cmp == 0 && code1 == LT_EXPR))
2215        @3
2216        @4))
2217      /* Likewise chose the more restrictive of two > or >= comparisons.  */
2218      (if ((code1 == GT_EXPR || code1 == GE_EXPR)
2219           && (code2 == GT_EXPR || code2 == GE_EXPR))
2220       (if ((cmp > 0) || (cmp == 0 && code1 == GT_EXPR))
2221        @3
2222        @4))
2223      /* Check for singleton ranges.  */
2224      (if (cmp == 0
2225           && ((code1 == LE_EXPR && code2 == GE_EXPR)
2226             || (code1 == GE_EXPR && code2 == LE_EXPR)))
2227       (eq @0 @1))
2228      /* Check for disjoint ranges.  */
2229      (if (cmp <= 0
2230           && (code1 == LT_EXPR || code1 == LE_EXPR)
2231           && (code2 == GT_EXPR || code2 == GE_EXPR))
2232       { constant_boolean_node (false, type); })
2233      (if (cmp >= 0
2234           && (code1 == GT_EXPR || code1 == GE_EXPR)
2235           && (code2 == LT_EXPR || code2 == LE_EXPR))
2236       { constant_boolean_node (false, type); })
2237      )))))
2239 /* Convert (X == CST1) || (X OP2 CST2) to a known value
2240    based on CST1 OP2 CST2.  Similarly for (X != CST1).  */
2242 (for code1 (eq ne)
2243  (for code2 (eq ne lt gt le ge)
2244   (simplify
2245    (bit_ior:c (code1@3 @0 INTEGER_CST@1) (code2@4 @0 INTEGER_CST@2))
2246     (with
2247      {
2248       int cmp = tree_int_cst_compare (@1, @2);
2249       bool val;
2250       switch (code2)
2251         {
2252         case EQ_EXPR: val = (cmp == 0); break;
2253         case NE_EXPR: val = (cmp != 0); break;
2254         case LT_EXPR: val = (cmp < 0); break;
2255         case GT_EXPR: val = (cmp > 0); break;
2256         case LE_EXPR: val = (cmp <= 0); break;
2257         case GE_EXPR: val = (cmp >= 0); break;
2258         default: gcc_unreachable ();
2259         }
2260      }
2261      (switch
2262       (if (code1 == EQ_EXPR && val) @4)
2263       (if (code1 == NE_EXPR && val) { constant_boolean_node (true, type); })
2264       (if (code1 == NE_EXPR && !val) @3))))))
2266 /* Convert (X OP1 CST1) || (X OP2 CST2).  */
2268 (for code1 (lt le gt ge)
2269  (for code2 (lt le gt ge)
2270   (simplify
2271   (bit_ior (code1@3 @0 INTEGER_CST@1) (code2@4 @0 INTEGER_CST@2))
2272    (with
2273     {
2274      int cmp = tree_int_cst_compare (@1, @2);
2275     }
2276     (switch
2277      /* Choose the more restrictive of two < or <= comparisons.  */
2278      (if ((code1 == LT_EXPR || code1 == LE_EXPR)
2279           && (code2 == LT_EXPR || code2 == LE_EXPR))
2280       (if ((cmp < 0) || (cmp == 0 && code1 == LT_EXPR))
2281        @4
2282        @3))
2283      /* Likewise chose the more restrictive of two > or >= comparisons.  */
2284      (if ((code1 == GT_EXPR || code1 == GE_EXPR)
2285           && (code2 == GT_EXPR || code2 == GE_EXPR))
2286       (if ((cmp > 0) || (cmp == 0 && code1 == GT_EXPR))
2287        @4
2288        @3))
2289      /* Check for singleton ranges.  */
2290      (if (cmp == 0
2291           && ((code1 == LT_EXPR && code2 == GT_EXPR)
2292               || (code1 == GT_EXPR && code2 == LT_EXPR)))
2293       (ne @0 @2))
2294      /* Check for disjoint ranges.  */
2295      (if (cmp >= 0
2296           && (code1 == LT_EXPR || code1 == LE_EXPR)
2297           && (code2 == GT_EXPR || code2 == GE_EXPR))
2298       { constant_boolean_node (true, type); })
2299      (if (cmp <= 0
2300           && (code1 == GT_EXPR || code1 == GE_EXPR)
2301           && (code2 == LT_EXPR || code2 == LE_EXPR))
2302       { constant_boolean_node (true, type); })
2303      )))))
2305 /* We can't reassociate at all for saturating types.  */
2306 (if (!TYPE_SATURATING (type))
2308  /* Contract negates.  */
2309  /* A + (-B) -> A - B */
2310  (simplify
2311   (plus:c @0 (convert? (negate @1)))
2312   /* Apply STRIP_NOPS on the negate.  */
2313   (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
2314        && !TYPE_OVERFLOW_SANITIZED (type))
2315    (with
2316     {
2317      tree t1 = type;
2318      if (INTEGRAL_TYPE_P (type)
2319          && TYPE_OVERFLOW_WRAPS (type) != TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
2320        t1 = TYPE_OVERFLOW_WRAPS (type) ? type : TREE_TYPE (@1);
2321     }
2322     (convert (minus (convert:t1 @0) (convert:t1 @1))))))
2323  /* A - (-B) -> A + B */
2324  (simplify
2325   (minus @0 (convert? (negate @1)))
2326   (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
2327        && !TYPE_OVERFLOW_SANITIZED (type))
2328    (with
2329     {
2330      tree t1 = type;
2331      if (INTEGRAL_TYPE_P (type)
2332          && TYPE_OVERFLOW_WRAPS (type) != TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
2333        t1 = TYPE_OVERFLOW_WRAPS (type) ? type : TREE_TYPE (@1);
2334     }
2335     (convert (plus (convert:t1 @0) (convert:t1 @1))))))
2336  /* -(T)(-A) -> (T)A
2337     Sign-extension is ok except for INT_MIN, which thankfully cannot
2338     happen without overflow.  */
2339  (simplify
2340   (negate (convert (negate @1)))
2341   (if (INTEGRAL_TYPE_P (type)
2342        && (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@1))
2343            || (!TYPE_UNSIGNED (TREE_TYPE (@1))
2344                && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))))
2345        && !TYPE_OVERFLOW_SANITIZED (type)
2346        && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@1)))
2347    (convert @1)))
2348  (simplify
2349   (negate (convert negate_expr_p@1))
2350   (if (SCALAR_FLOAT_TYPE_P (type)
2351        && ((DECIMAL_FLOAT_TYPE_P (type)
2352             == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1))
2353             && TYPE_PRECISION (type) >= TYPE_PRECISION (TREE_TYPE (@1)))
2354            || !HONOR_SIGN_DEPENDENT_ROUNDING (type)))
2355    (convert (negate @1))))
2356  (simplify
2357   (negate (nop_convert? (negate @1)))
2358   (if (!TYPE_OVERFLOW_SANITIZED (type)
2359        && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@1)))
2360    (view_convert @1)))
2362  /* We can't reassociate floating-point unless -fassociative-math
2363     or fixed-point plus or minus because of saturation to +-Inf.  */
2364  (if ((!FLOAT_TYPE_P (type) || flag_associative_math)
2365       && !FIXED_POINT_TYPE_P (type))
2367   /* Match patterns that allow contracting a plus-minus pair
2368      irrespective of overflow issues.  */
2369   /* (A +- B) - A       ->  +- B */
2370   /* (A +- B) -+ B      ->  A */
2371   /* A - (A +- B)       -> -+ B */
2372   /* A +- (B -+ A)      ->  +- B */
2373   (simplify
2374    (minus (nop_convert1? (plus:c (nop_convert2? @0) @1)) @0)
2375    (view_convert @1))
2376   (simplify
2377    (minus (nop_convert1? (minus (nop_convert2? @0) @1)) @0)
2378    (if (!ANY_INTEGRAL_TYPE_P (type)
2379         || TYPE_OVERFLOW_WRAPS (type))
2380    (negate (view_convert @1))
2381    (view_convert (negate @1))))
2382   (simplify
2383    (plus:c (nop_convert1? (minus @0 (nop_convert2? @1))) @1)
2384    (view_convert @0))
2385   (simplify
2386    (minus @0 (nop_convert1? (plus:c (nop_convert2? @0) @1)))
2387     (if (!ANY_INTEGRAL_TYPE_P (type)
2388          || TYPE_OVERFLOW_WRAPS (type))
2389      (negate (view_convert @1))
2390      (view_convert (negate @1))))
2391   (simplify
2392    (minus @0 (nop_convert1? (minus (nop_convert2? @0) @1)))
2393    (view_convert @1))
2394   /* (A +- B) + (C - A)   -> C +- B */
2395   /* (A +  B) - (A - C)   -> B + C */
2396   /* More cases are handled with comparisons.  */
2397   (simplify
2398    (plus:c (plus:c @0 @1) (minus @2 @0))
2399    (plus @2 @1))
2400   (simplify
2401    (plus:c (minus @0 @1) (minus @2 @0))
2402    (minus @2 @1))
2403   (simplify
2404    (plus:c (pointer_diff @0 @1) (pointer_diff @2 @0))
2405    (if (TYPE_OVERFLOW_UNDEFINED (type)
2406         && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0)))
2407     (pointer_diff @2 @1)))
2408   (simplify
2409    (minus (plus:c @0 @1) (minus @0 @2))
2410    (plus @1 @2))
2412   /* (A +- CST1) +- CST2 -> A + CST3
2413      Use view_convert because it is safe for vectors and equivalent for
2414      scalars.  */
2415   (for outer_op (plus minus)
2416    (for inner_op (plus minus)
2417         neg_inner_op (minus plus)
2418     (simplify
2419      (outer_op (nop_convert? (inner_op @0 CONSTANT_CLASS_P@1))
2420                CONSTANT_CLASS_P@2)
2421      /* If one of the types wraps, use that one.  */
2422      (if (!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_WRAPS (type))
2423       /* If all 3 captures are CONSTANT_CLASS_P, punt, as we might recurse
2424          forever if something doesn't simplify into a constant.  */
2425       (if (!CONSTANT_CLASS_P (@0))
2426        (if (outer_op == PLUS_EXPR)
2427         (plus (view_convert @0) (inner_op @2 (view_convert @1)))
2428         (minus (view_convert @0) (neg_inner_op @2 (view_convert @1)))))
2429       (if (!ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2430            || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
2431        (if (outer_op == PLUS_EXPR)
2432         (view_convert (plus @0 (inner_op (view_convert @2) @1)))
2433         (view_convert (minus @0 (neg_inner_op (view_convert @2) @1))))
2434        /* If the constant operation overflows we cannot do the transform
2435           directly as we would introduce undefined overflow, for example
2436           with (a - 1) + INT_MIN.  */
2437        (if (types_match (type, @0))
2438         (with { tree cst = const_binop (outer_op == inner_op
2439                                         ? PLUS_EXPR : MINUS_EXPR,
2440                                         type, @1, @2); }
2441          (if (cst && !TREE_OVERFLOW (cst))
2442           (inner_op @0 { cst; } )
2443           /* X+INT_MAX+1 is X-INT_MIN.  */
2444           (if (INTEGRAL_TYPE_P (type) && cst
2445                && wi::to_wide (cst) == wi::min_value (type))
2446            (neg_inner_op @0 { wide_int_to_tree (type, wi::to_wide (cst)); })
2447            /* Last resort, use some unsigned type.  */
2448            (with { tree utype = unsigned_type_for (type); }
2449             (if (utype)
2450              (view_convert (inner_op
2451                             (view_convert:utype @0)
2452                             (view_convert:utype
2453                              { drop_tree_overflow (cst); }))))))))))))))
2455   /* (CST1 - A) +- CST2 -> CST3 - A  */
2456   (for outer_op (plus minus)
2457    (simplify
2458     (outer_op (nop_convert? (minus CONSTANT_CLASS_P@1 @0)) CONSTANT_CLASS_P@2)
2459     /* If one of the types wraps, use that one.  */
2460     (if (!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_WRAPS (type))
2461      /* If all 3 captures are CONSTANT_CLASS_P, punt, as we might recurse
2462         forever if something doesn't simplify into a constant.  */
2463      (if (!CONSTANT_CLASS_P (@0))
2464       (minus (outer_op (view_convert @1) @2) (view_convert @0)))
2465      (if (!ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2466           || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
2467       (view_convert (minus (outer_op @1 (view_convert @2)) @0))
2468       (if (types_match (type, @0))
2469        (with { tree cst = const_binop (outer_op, type, @1, @2); }
2470         (if (cst && !TREE_OVERFLOW (cst))
2471          (minus { cst; } @0))))))))
2473   /* CST1 - (CST2 - A) -> CST3 + A
2474      Use view_convert because it is safe for vectors and equivalent for
2475      scalars.  */
2476   (simplify
2477    (minus CONSTANT_CLASS_P@1 (nop_convert? (minus CONSTANT_CLASS_P@2 @0)))
2478    /* If one of the types wraps, use that one.  */
2479    (if (!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_WRAPS (type))
2480     /* If all 3 captures are CONSTANT_CLASS_P, punt, as we might recurse
2481       forever if something doesn't simplify into a constant.  */
2482     (if (!CONSTANT_CLASS_P (@0))
2483      (plus (view_convert @0) (minus @1 (view_convert @2))))
2484     (if (!ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2485          || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
2486      (view_convert (plus @0 (minus (view_convert @1) @2)))
2487      (if (types_match (type, @0))
2488       (with { tree cst = const_binop (MINUS_EXPR, type, @1, @2); }
2489        (if (cst && !TREE_OVERFLOW (cst))
2490         (plus { cst; } @0)))))))
2492 /* ((T)(A)) + CST -> (T)(A + CST)  */
2493 #if GIMPLE
2494   (simplify
2495    (plus (convert:s SSA_NAME@0) INTEGER_CST@1)
2496     (if (TREE_CODE (TREE_TYPE (@0)) == INTEGER_TYPE
2497          && TREE_CODE (type) == INTEGER_TYPE
2498          && TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (@0))
2499          && int_fits_type_p (@1, TREE_TYPE (@0)))
2500      /* Perform binary operation inside the cast if the constant fits
2501         and (A + CST)'s range does not overflow.  */
2502      (with
2503       {
2504         wi::overflow_type min_ovf = wi::OVF_OVERFLOW,
2505                           max_ovf = wi::OVF_OVERFLOW;
2506         tree inner_type = TREE_TYPE (@0);
2508         wide_int w1
2509           = wide_int::from (wi::to_wide (@1), TYPE_PRECISION (inner_type),
2510                             TYPE_SIGN (inner_type));
2512         wide_int wmin0, wmax0;
2513         if (get_range_info (@0, &wmin0, &wmax0) == VR_RANGE)
2514           {
2515             wi::add (wmin0, w1, TYPE_SIGN (inner_type), &min_ovf);
2516             wi::add (wmax0, w1, TYPE_SIGN (inner_type), &max_ovf);
2517           }
2518       }
2519      (if (min_ovf == wi::OVF_NONE && max_ovf == wi::OVF_NONE)
2520       (convert (plus @0 { wide_int_to_tree (TREE_TYPE (@0), w1); } )))
2521      )))
2522 #endif
2524 /* ((T)(A + CST1)) + CST2 -> (T)(A) + (T)CST1 + CST2  */
2525 #if GIMPLE
2526   (for op (plus minus)
2527    (simplify
2528     (plus (convert:s (op:s @0 INTEGER_CST@1)) INTEGER_CST@2)
2529      (if (TREE_CODE (TREE_TYPE (@0)) == INTEGER_TYPE
2530           && TREE_CODE (type) == INTEGER_TYPE
2531           && TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (@0))
2532           && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2533           && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0))
2534           && TYPE_OVERFLOW_WRAPS (type))
2535        (plus (convert @0) (op @2 (convert @1))))))
2536 #endif
2538 /* (T)(A) +- (T)(B) -> (T)(A +- B) only when (A +- B) could be simplified
2539    to a simple value.  */
2540 #if GIMPLE
2541   (for op (plus minus)
2542    (simplify
2543     (op (convert @0) (convert @1))
2544      (if (INTEGRAL_TYPE_P (type)
2545           && INTEGRAL_TYPE_P (TREE_TYPE (@0))
2546           && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))
2547           && types_match (TREE_TYPE (@0), TREE_TYPE (@1))
2548           && !TYPE_OVERFLOW_TRAPS (type)
2549           && !TYPE_OVERFLOW_SANITIZED (type))
2550       (convert (op! @0 @1)))))
2551 #endif
2553   /* ~A + A -> -1 */
2554   (simplify
2555    (plus:c (bit_not @0) @0)
2556    (if (!TYPE_OVERFLOW_TRAPS (type))
2557     { build_all_ones_cst (type); }))
2559   /* ~A + 1 -> -A */
2560   (simplify
2561    (plus (convert? (bit_not @0)) integer_each_onep)
2562    (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2563     (negate (convert @0))))
2565   /* -A - 1 -> ~A */
2566   (simplify
2567    (minus (convert? (negate @0)) integer_each_onep)
2568    (if (!TYPE_OVERFLOW_TRAPS (type)
2569         && tree_nop_conversion_p (type, TREE_TYPE (@0)))
2570     (bit_not (convert @0))))
2572   /* -1 - A -> ~A */
2573   (simplify
2574    (minus integer_all_onesp @0)
2575    (bit_not @0))
2577   /* (T)(P + A) - (T)P -> (T) A */
2578   (simplify
2579    (minus (convert (plus:c @@0 @1))
2580     (convert? @0))
2581    (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
2582         /* For integer types, if A has a smaller type
2583            than T the result depends on the possible
2584            overflow in P + A.
2585            E.g. T=size_t, A=(unsigned)429497295, P>0.
2586            However, if an overflow in P + A would cause
2587            undefined behavior, we can assume that there
2588            is no overflow.  */
2589         || (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2590             && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))))
2591     (convert @1)))
2592   (simplify
2593    (minus (convert (pointer_plus @@0 @1))
2594     (convert @0))
2595    (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
2596         /* For pointer types, if the conversion of A to the
2597            final type requires a sign- or zero-extension,
2598            then we have to punt - it is not defined which
2599            one is correct.  */
2600         || (POINTER_TYPE_P (TREE_TYPE (@0))
2601             && TREE_CODE (@1) == INTEGER_CST
2602             && tree_int_cst_sign_bit (@1) == 0))
2603     (convert @1)))
2604    (simplify
2605     (pointer_diff (pointer_plus @@0 @1) @0)
2606     /* The second argument of pointer_plus must be interpreted as signed, and
2607        thus sign-extended if necessary.  */
2608     (with { tree stype = signed_type_for (TREE_TYPE (@1)); }
2609      /* Use view_convert instead of convert here, as POINTER_PLUS_EXPR
2610         second arg is unsigned even when we need to consider it as signed,
2611         we don't want to diagnose overflow here.  */
2612      (convert (view_convert:stype @1))))
2614   /* (T)P - (T)(P + A) -> -(T) A */
2615   (simplify
2616    (minus (convert? @0)
2617     (convert (plus:c @@0 @1)))
2618    (if (INTEGRAL_TYPE_P (type)
2619         && TYPE_OVERFLOW_UNDEFINED (type)
2620         && element_precision (type) <= element_precision (TREE_TYPE (@1)))
2621     (with { tree utype = unsigned_type_for (type); }
2622      (convert (negate (convert:utype @1))))
2623     (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
2624          /* For integer types, if A has a smaller type
2625             than T the result depends on the possible
2626             overflow in P + A.
2627             E.g. T=size_t, A=(unsigned)429497295, P>0.
2628             However, if an overflow in P + A would cause
2629             undefined behavior, we can assume that there
2630             is no overflow.  */
2631          || (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2632              && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))))
2633      (negate (convert @1)))))
2634   (simplify
2635    (minus (convert @0)
2636     (convert (pointer_plus @@0 @1)))
2637    (if (INTEGRAL_TYPE_P (type)
2638         && TYPE_OVERFLOW_UNDEFINED (type)
2639         && element_precision (type) <= element_precision (TREE_TYPE (@1)))
2640     (with { tree utype = unsigned_type_for (type); }
2641      (convert (negate (convert:utype @1))))
2642     (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
2643          /* For pointer types, if the conversion of A to the
2644             final type requires a sign- or zero-extension,
2645             then we have to punt - it is not defined which
2646             one is correct.  */
2647          || (POINTER_TYPE_P (TREE_TYPE (@0))
2648              && TREE_CODE (@1) == INTEGER_CST
2649              && tree_int_cst_sign_bit (@1) == 0))
2650      (negate (convert @1)))))
2651    (simplify
2652     (pointer_diff @0 (pointer_plus @@0 @1))
2653     /* The second argument of pointer_plus must be interpreted as signed, and
2654        thus sign-extended if necessary.  */
2655     (with { tree stype = signed_type_for (TREE_TYPE (@1)); }
2656      /* Use view_convert instead of convert here, as POINTER_PLUS_EXPR
2657         second arg is unsigned even when we need to consider it as signed,
2658         we don't want to diagnose overflow here.  */
2659      (negate (convert (view_convert:stype @1)))))
2661   /* (T)(P + A) - (T)(P + B) -> (T)A - (T)B */
2662   (simplify
2663    (minus (convert (plus:c @@0 @1))
2664     (convert (plus:c @0 @2)))
2665    (if (INTEGRAL_TYPE_P (type)
2666         && TYPE_OVERFLOW_UNDEFINED (type)
2667         && element_precision (type) <= element_precision (TREE_TYPE (@1))
2668         && element_precision (type) <= element_precision (TREE_TYPE (@2)))
2669     (with { tree utype = unsigned_type_for (type); }
2670      (convert (minus (convert:utype @1) (convert:utype @2))))
2671     (if (((element_precision (type) <= element_precision (TREE_TYPE (@1)))
2672           == (element_precision (type) <= element_precision (TREE_TYPE (@2))))
2673          && (element_precision (type) <= element_precision (TREE_TYPE (@1))
2674              /* For integer types, if A has a smaller type
2675                 than T the result depends on the possible
2676                 overflow in P + A.
2677                 E.g. T=size_t, A=(unsigned)429497295, P>0.
2678                 However, if an overflow in P + A would cause
2679                 undefined behavior, we can assume that there
2680                 is no overflow.  */
2681              || (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2682                  && INTEGRAL_TYPE_P (TREE_TYPE (@2))
2683                  && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))
2684                  && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@2)))))
2685      (minus (convert @1) (convert @2)))))
2686   (simplify
2687    (minus (convert (pointer_plus @@0 @1))
2688     (convert (pointer_plus @0 @2)))
2689    (if (INTEGRAL_TYPE_P (type)
2690         && TYPE_OVERFLOW_UNDEFINED (type)
2691         && element_precision (type) <= element_precision (TREE_TYPE (@1)))
2692     (with { tree utype = unsigned_type_for (type); }
2693      (convert (minus (convert:utype @1) (convert:utype @2))))
2694     (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
2695          /* For pointer types, if the conversion of A to the
2696             final type requires a sign- or zero-extension,
2697             then we have to punt - it is not defined which
2698             one is correct.  */
2699          || (POINTER_TYPE_P (TREE_TYPE (@0))
2700              && TREE_CODE (@1) == INTEGER_CST
2701              && tree_int_cst_sign_bit (@1) == 0
2702              && TREE_CODE (@2) == INTEGER_CST
2703              && tree_int_cst_sign_bit (@2) == 0))
2704      (minus (convert @1) (convert @2)))))
2705    (simplify
2706     (pointer_diff (pointer_plus @0 @2) (pointer_plus @1 @2))
2707      (pointer_diff @0 @1))
2708    (simplify
2709     (pointer_diff (pointer_plus @@0 @1) (pointer_plus @0 @2))
2710     /* The second argument of pointer_plus must be interpreted as signed, and
2711        thus sign-extended if necessary.  */
2712     (with { tree stype = signed_type_for (TREE_TYPE (@1)); }
2713      /* Use view_convert instead of convert here, as POINTER_PLUS_EXPR
2714         second arg is unsigned even when we need to consider it as signed,
2715         we don't want to diagnose overflow here.  */
2716      (minus (convert (view_convert:stype @1))
2717             (convert (view_convert:stype @2)))))))
2719 /* (A * C) +- (B * C) -> (A+-B) * C and (A * C) +- A -> A * (C+-1).
2720     Modeled after fold_plusminus_mult_expr.  */
2721 (if (!TYPE_SATURATING (type)
2722      && (!FLOAT_TYPE_P (type) || flag_associative_math))
2723  (for plusminus (plus minus)
2724   (simplify
2725    (plusminus (mult:cs@3 @0 @1) (mult:cs@4 @0 @2))
2726    (if (!ANY_INTEGRAL_TYPE_P (type)
2727         || TYPE_OVERFLOW_WRAPS (type)
2728         || (INTEGRAL_TYPE_P (type)
2729             && tree_expr_nonzero_p (@0)
2730             && expr_not_equal_to (@0, wi::minus_one (TYPE_PRECISION (type)))))
2731     (if (single_use (@3) || single_use (@4))
2732      /* If @1 +- @2 is constant require a hard single-use on either
2733         original operand (but not on both).  */
2734      (mult (plusminus @1 @2) @0)
2735 #if GIMPLE
2736      (mult! (plusminus @1 @2) @0)
2737 #endif
2738   )))
2739   /* We cannot generate constant 1 for fract.  */
2740   (if (!ALL_FRACT_MODE_P (TYPE_MODE (type)))
2741    (simplify
2742     (plusminus @0 (mult:c@3 @0 @2))
2743     (if ((!ANY_INTEGRAL_TYPE_P (type)
2744           || TYPE_OVERFLOW_WRAPS (type)
2745           /* For @0 + @0*@2 this transformation would introduce UB
2746              (where there was none before) for @0 in [-1,0] and @2 max.
2747              For @0 - @0*@2 this transformation would introduce UB
2748              for @0 0 and @2 in [min,min+1] or @0 -1 and @2 min+1.  */
2749           || (INTEGRAL_TYPE_P (type)
2750               && ((tree_expr_nonzero_p (@0)
2751                    && expr_not_equal_to (@0,
2752                                 wi::minus_one (TYPE_PRECISION (type))))
2753                   || (plusminus == PLUS_EXPR
2754                       ? expr_not_equal_to (@2,
2755                             wi::max_value (TYPE_PRECISION (type), SIGNED))
2756                       /* Let's ignore the @0 -1 and @2 min case.  */
2757                       : (expr_not_equal_to (@2,
2758                             wi::min_value (TYPE_PRECISION (type), SIGNED))
2759                          && expr_not_equal_to (@2,
2760                                 wi::min_value (TYPE_PRECISION (type), SIGNED)
2761                                 + 1))))))
2762          && single_use (@3))
2763      (mult (plusminus { build_one_cst (type); } @2) @0)))
2764    (simplify
2765     (plusminus (mult:c@3 @0 @2) @0)
2766     (if ((!ANY_INTEGRAL_TYPE_P (type)
2767           || TYPE_OVERFLOW_WRAPS (type)
2768           /* For @0*@2 + @0 this transformation would introduce UB
2769              (where there was none before) for @0 in [-1,0] and @2 max.
2770              For @0*@2 - @0 this transformation would introduce UB
2771              for @0 0 and @2 min.  */
2772           || (INTEGRAL_TYPE_P (type)
2773               && ((tree_expr_nonzero_p (@0)
2774                    && (plusminus == MINUS_EXPR
2775                        || expr_not_equal_to (@0,
2776                                 wi::minus_one (TYPE_PRECISION (type)))))
2777                   || expr_not_equal_to (@2,
2778                         (plusminus == PLUS_EXPR
2779                          ? wi::max_value (TYPE_PRECISION (type), SIGNED)
2780                          : wi::min_value (TYPE_PRECISION (type), SIGNED))))))
2781          && single_use (@3))
2782      (mult (plusminus @2 { build_one_cst (type); }) @0))))))
2784 #if GIMPLE
2785 /* Canonicalize X + (X << C) into X * (1 + (1 << C)) and
2786    (X << C1) + (X << C2) into X * ((1 << C1) + (1 << C2)).  */
2787 (simplify
2788  (plus:c @0 (lshift:s @0 INTEGER_CST@1))
2789   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2790        && tree_fits_uhwi_p (@1)
2791        && tree_to_uhwi (@1) < element_precision (type)
2792        && (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2793            || optab_handler (smul_optab,
2794                              TYPE_MODE (type)) != CODE_FOR_nothing))
2795    (with { tree t = type;
2796            if (!TYPE_OVERFLOW_WRAPS (t)) t = unsigned_type_for (t);
2797            wide_int w = wi::set_bit_in_zero (tree_to_uhwi (@1),
2798                                              element_precision (type));
2799            w += 1;
2800            tree cst = wide_int_to_tree (VECTOR_TYPE_P (t) ? TREE_TYPE (t)
2801                                         : t, w);
2802            cst = build_uniform_cst (t, cst); }
2803     (convert (mult (convert:t @0) { cst; })))))
2804 (simplify
2805  (plus (lshift:s @0 INTEGER_CST@1) (lshift:s @0 INTEGER_CST@2))
2806   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2807        && tree_fits_uhwi_p (@1)
2808        && tree_to_uhwi (@1) < element_precision (type)
2809        && tree_fits_uhwi_p (@2)
2810        && tree_to_uhwi (@2) < element_precision (type)
2811        && (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2812            || optab_handler (smul_optab,
2813                              TYPE_MODE (type)) != CODE_FOR_nothing))
2814    (with { tree t = type;
2815            if (!TYPE_OVERFLOW_WRAPS (t)) t = unsigned_type_for (t);
2816            unsigned int prec = element_precision (type);
2817            wide_int w = wi::set_bit_in_zero (tree_to_uhwi (@1), prec);
2818            w += wi::set_bit_in_zero (tree_to_uhwi (@2), prec);
2819            tree cst = wide_int_to_tree (VECTOR_TYPE_P (t) ? TREE_TYPE (t)
2820                                         : t, w);
2821            cst = build_uniform_cst (t, cst); }
2822     (convert (mult (convert:t @0) { cst; })))))
2823 #endif
2825 /* Simplifications of MIN_EXPR, MAX_EXPR, fmin() and fmax().  */
2827 (for minmax (min max FMIN_ALL FMAX_ALL)
2828  (simplify
2829   (minmax @0 @0)
2830   @0))
2831 /* min(max(x,y),y) -> y.  */
2832 (simplify
2833  (min:c (max:c @0 @1) @1)
2834  @1)
2835 /* max(min(x,y),y) -> y.  */
2836 (simplify
2837  (max:c (min:c @0 @1) @1)
2838  @1)
2839 /* max(a,-a) -> abs(a).  */
2840 (simplify
2841  (max:c @0 (negate @0))
2842  (if (TREE_CODE (type) != COMPLEX_TYPE
2843       && (! ANY_INTEGRAL_TYPE_P (type)
2844           || TYPE_OVERFLOW_UNDEFINED (type)))
2845   (abs @0)))
2846 /* min(a,-a) -> -abs(a).  */
2847 (simplify
2848  (min:c @0 (negate @0))
2849  (if (TREE_CODE (type) != COMPLEX_TYPE
2850       && (! ANY_INTEGRAL_TYPE_P (type)
2851           || TYPE_OVERFLOW_UNDEFINED (type)))
2852   (negate (abs @0))))
2853 (simplify
2854  (min @0 @1)
2855  (switch
2856   (if (INTEGRAL_TYPE_P (type)
2857        && TYPE_MIN_VALUE (type)
2858        && operand_equal_p (@1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
2859    @1)
2860   (if (INTEGRAL_TYPE_P (type)
2861        && TYPE_MAX_VALUE (type)
2862        && operand_equal_p (@1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
2863    @0)))
2864 (simplify
2865  (max @0 @1)
2866  (switch
2867   (if (INTEGRAL_TYPE_P (type)
2868        && TYPE_MAX_VALUE (type)
2869        && operand_equal_p (@1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
2870    @1)
2871   (if (INTEGRAL_TYPE_P (type)
2872        && TYPE_MIN_VALUE (type)
2873        && operand_equal_p (@1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
2874    @0)))
2876 /* max (a, a + CST) -> a + CST where CST is positive.  */
2877 /* max (a, a + CST) -> a where CST is negative.  */
2878 (simplify
2879  (max:c @0 (plus@2 @0 INTEGER_CST@1))
2880   (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
2881    (if (tree_int_cst_sgn (@1) > 0)
2882     @2
2883     @0)))
2885 /* min (a, a + CST) -> a where CST is positive.  */
2886 /* min (a, a + CST) -> a + CST where CST is negative. */
2887 (simplify
2888  (min:c @0 (plus@2 @0 INTEGER_CST@1))
2889   (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
2890    (if (tree_int_cst_sgn (@1) > 0)
2891     @0
2892     @2)))
2894 /* (convert (minmax ((convert (x) c)))) -> minmax (x c) if x is promoted
2895    and the outer convert demotes the expression back to x's type.  */
2896 (for minmax (min max)
2897  (simplify
2898   (convert (minmax@0 (convert @1) INTEGER_CST@2))
2899   (if (INTEGRAL_TYPE_P (type)
2900        && types_match (@1, type) && int_fits_type_p (@2, type)
2901        && TYPE_SIGN (TREE_TYPE (@0)) == TYPE_SIGN (type)
2902        && TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type))
2903    (minmax @1 (convert @2)))))
2905 (for minmax (FMIN_ALL FMAX_ALL)
2906  /* If either argument is NaN, return the other one.  Avoid the
2907     transformation if we get (and honor) a signalling NaN.  */
2908  (simplify
2909   (minmax:c @0 REAL_CST@1)
2910   (if (real_isnan (TREE_REAL_CST_PTR (@1))
2911        && (!HONOR_SNANS (@1) || !TREE_REAL_CST (@1).signalling))
2912    @0)))
2913 /* Convert fmin/fmax to MIN_EXPR/MAX_EXPR.  C99 requires these
2914    functions to return the numeric arg if the other one is NaN.
2915    MIN and MAX don't honor that, so only transform if -ffinite-math-only
2916    is set.  C99 doesn't require -0.0 to be handled, so we don't have to
2917    worry about it either.  */
2918 (if (flag_finite_math_only)
2919  (simplify
2920   (FMIN_ALL @0 @1)
2921   (min @0 @1))
2922  (simplify
2923   (FMAX_ALL @0 @1)
2924   (max @0 @1)))
2925 /* min (-A, -B) -> -max (A, B)  */
2926 (for minmax (min max FMIN_ALL FMAX_ALL)
2927      maxmin (max min FMAX_ALL FMIN_ALL)
2928  (simplify
2929   (minmax (negate:s@2 @0) (negate:s@3 @1))
2930   (if (FLOAT_TYPE_P (TREE_TYPE (@0))
2931        || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2932            && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
2933    (negate (maxmin @0 @1)))))
2934 /* MIN (~X, ~Y) -> ~MAX (X, Y)
2935    MAX (~X, ~Y) -> ~MIN (X, Y)  */
2936 (for minmax (min max)
2937  maxmin (max min)
2938  (simplify
2939   (minmax (bit_not:s@2 @0) (bit_not:s@3 @1))
2940   (bit_not (maxmin @0 @1))))
2942 /* MIN (X, Y) == X -> X <= Y  */
2943 (for minmax (min min max max)
2944      cmp    (eq  ne  eq  ne )
2945      out    (le  gt  ge  lt )
2946  (simplify
2947   (cmp:c (minmax:c @0 @1) @0)
2948   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0)))
2949    (out @0 @1))))
2950 /* MIN (X, 5) == 0 -> X == 0
2951    MIN (X, 5) == 7 -> false  */
2952 (for cmp (eq ne)
2953  (simplify
2954   (cmp (min @0 INTEGER_CST@1) INTEGER_CST@2)
2955   (if (wi::lt_p (wi::to_wide (@1), wi::to_wide (@2),
2956                  TYPE_SIGN (TREE_TYPE (@0))))
2957    { constant_boolean_node (cmp == NE_EXPR, type); }
2958    (if (wi::gt_p (wi::to_wide (@1), wi::to_wide (@2),
2959                   TYPE_SIGN (TREE_TYPE (@0))))
2960     (cmp @0 @2)))))
2961 (for cmp (eq ne)
2962  (simplify
2963   (cmp (max @0 INTEGER_CST@1) INTEGER_CST@2)
2964   (if (wi::gt_p (wi::to_wide (@1), wi::to_wide (@2),
2965                  TYPE_SIGN (TREE_TYPE (@0))))
2966    { constant_boolean_node (cmp == NE_EXPR, type); }
2967    (if (wi::lt_p (wi::to_wide (@1), wi::to_wide (@2),
2968                   TYPE_SIGN (TREE_TYPE (@0))))
2969     (cmp @0 @2)))))
2970 /* MIN (X, C1) < C2 -> X < C2 || C1 < C2  */
2971 (for minmax (min     min     max     max     min     min     max     max    )
2972      cmp    (lt      le      gt      ge      gt      ge      lt      le     )
2973      comb   (bit_ior bit_ior bit_ior bit_ior bit_and bit_and bit_and bit_and)
2974  (simplify
2975   (cmp (minmax @0 INTEGER_CST@1) INTEGER_CST@2)
2976   (comb (cmp @0 @2) (cmp @1 @2))))
2978 /* X <= MAX(X, Y) -> true
2979    X > MAX(X, Y) -> false 
2980    X >= MIN(X, Y) -> true
2981    X < MIN(X, Y) -> false */
2982 (for minmax (min     min     max     max     )
2983      cmp    (ge      lt      le      gt      )
2984  (simplify
2985   (cmp @0 (minmax:c @0 @1))
2986   { constant_boolean_node (cmp == GE_EXPR || cmp == LE_EXPR, type); } ))
2988 /* Undo fancy way of writing max/min or other ?: expressions,
2989    like a - ((a - b) & -(a < b)), in this case into (a < b) ? b : a.
2990    People normally use ?: and that is what we actually try to optimize.  */
2991 (for cmp (simple_comparison)
2992  (simplify
2993   (minus @0 (bit_and:c (minus @0 @1)
2994                        (convert? (negate@4 (convert? (cmp@5 @2 @3))))))
2995   (if (INTEGRAL_TYPE_P (type)
2996        && INTEGRAL_TYPE_P (TREE_TYPE (@4))
2997        && TREE_CODE (TREE_TYPE (@4)) != BOOLEAN_TYPE
2998        && INTEGRAL_TYPE_P (TREE_TYPE (@5))
2999        && (TYPE_PRECISION (TREE_TYPE (@4)) >= TYPE_PRECISION (type)
3000            || !TYPE_UNSIGNED (TREE_TYPE (@4)))
3001        && (GIMPLE || !TREE_SIDE_EFFECTS (@1)))
3002    (cond (cmp @2 @3) @1 @0)))
3003  (simplify
3004   (plus:c @0 (bit_and:c (minus @1 @0)
3005                         (convert? (negate@4 (convert? (cmp@5 @2 @3))))))
3006   (if (INTEGRAL_TYPE_P (type)
3007        && INTEGRAL_TYPE_P (TREE_TYPE (@4))
3008        && TREE_CODE (TREE_TYPE (@4)) != BOOLEAN_TYPE
3009        && INTEGRAL_TYPE_P (TREE_TYPE (@5))
3010        && (TYPE_PRECISION (TREE_TYPE (@4)) >= TYPE_PRECISION (type)
3011            || !TYPE_UNSIGNED (TREE_TYPE (@4)))
3012        && (GIMPLE || !TREE_SIDE_EFFECTS (@1)))
3013    (cond (cmp @2 @3) @1 @0)))
3014  /* Similarly with ^ instead of - though in that case with :c.  */
3015  (simplify
3016   (bit_xor:c @0 (bit_and:c (bit_xor:c @0 @1)
3017                            (convert? (negate@4 (convert? (cmp@5 @2 @3))))))
3018   (if (INTEGRAL_TYPE_P (type)
3019        && INTEGRAL_TYPE_P (TREE_TYPE (@4))
3020        && TREE_CODE (TREE_TYPE (@4)) != BOOLEAN_TYPE
3021        && INTEGRAL_TYPE_P (TREE_TYPE (@5))
3022        && (TYPE_PRECISION (TREE_TYPE (@4)) >= TYPE_PRECISION (type)
3023            || !TYPE_UNSIGNED (TREE_TYPE (@4)))
3024        && (GIMPLE || !TREE_SIDE_EFFECTS (@1)))
3025    (cond (cmp @2 @3) @1 @0))))
3027 /* Simplifications of shift and rotates.  */
3029 (for rotate (lrotate rrotate)
3030  (simplify
3031   (rotate integer_all_onesp@0 @1)
3032   @0))
3034 /* Optimize -1 >> x for arithmetic right shifts.  */
3035 (simplify
3036  (rshift integer_all_onesp@0 @1)
3037  (if (!TYPE_UNSIGNED (type))
3038   @0))
3040 /* Optimize (x >> c) << c into x & (-1<<c).  */
3041 (simplify
3042  (lshift (nop_convert? (rshift @0 INTEGER_CST@1)) @1)
3043  (if (wi::ltu_p (wi::to_wide (@1), element_precision (type)))
3044   /* It doesn't matter if the right shift is arithmetic or logical.  */
3045   (bit_and (view_convert @0) (lshift { build_minus_one_cst (type); } @1))))
3047 (simplify
3048  (lshift (convert (convert@2 (rshift @0 INTEGER_CST@1))) @1)
3049  (if (wi::ltu_p (wi::to_wide (@1), element_precision (type))
3050       /* Allow intermediate conversion to integral type with whatever sign, as
3051          long as the low TYPE_PRECISION (type)
3052          - TYPE_PRECISION (TREE_TYPE (@2)) bits are preserved.  */
3053       && INTEGRAL_TYPE_P (type)
3054       && INTEGRAL_TYPE_P (TREE_TYPE (@2))
3055       && INTEGRAL_TYPE_P (TREE_TYPE (@0))
3056       && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (@0))
3057       && (TYPE_PRECISION (TREE_TYPE (@2)) >= TYPE_PRECISION (type)
3058           || wi::geu_p (wi::to_wide (@1),
3059                         TYPE_PRECISION (type)
3060                         - TYPE_PRECISION (TREE_TYPE (@2)))))
3061   (bit_and (convert @0) (lshift { build_minus_one_cst (type); } @1))))
3063 /* Optimize (x << c) >> c into x & ((unsigned)-1 >> c) for unsigned
3064    types.  */
3065 (simplify
3066  (rshift (lshift @0 INTEGER_CST@1) @1)
3067  (if (TYPE_UNSIGNED (type)
3068       && (wi::ltu_p (wi::to_wide (@1), element_precision (type))))
3069   (bit_and @0 (rshift { build_minus_one_cst (type); } @1))))
3071 /* Optimize x >> x into 0 */
3072 (simplify
3073  (rshift @0 @0)
3074   { build_zero_cst (type); })
3076 (for shiftrotate (lrotate rrotate lshift rshift)
3077  (simplify
3078   (shiftrotate @0 integer_zerop)
3079   (non_lvalue @0))
3080  (simplify
3081   (shiftrotate integer_zerop@0 @1)
3082   @0)
3083  /* Prefer vector1 << scalar to vector1 << vector2
3084     if vector2 is uniform.  */
3085  (for vec (VECTOR_CST CONSTRUCTOR)
3086   (simplify
3087    (shiftrotate @0 vec@1)
3088    (with { tree tem = uniform_vector_p (@1); }
3089     (if (tem)
3090      (shiftrotate @0 { tem; }))))))
3092 /* Simplify X << Y where Y's low width bits are 0 to X, as only valid
3093    Y is 0.  Similarly for X >> Y.  */
3094 #if GIMPLE
3095 (for shift (lshift rshift)
3096  (simplify
3097   (shift @0 SSA_NAME@1)
3098    (if (INTEGRAL_TYPE_P (TREE_TYPE (@1)))
3099     (with {
3100       int width = ceil_log2 (element_precision (TREE_TYPE (@0)));
3101       int prec = TYPE_PRECISION (TREE_TYPE (@1));
3102      }
3103      (if ((get_nonzero_bits (@1) & wi::mask (width, false, prec)) == 0)
3104       @0)))))
3105 #endif
3107 /* Rewrite an LROTATE_EXPR by a constant into an
3108    RROTATE_EXPR by a new constant.  */
3109 (simplify
3110  (lrotate @0 INTEGER_CST@1)
3111  (rrotate @0 { const_binop (MINUS_EXPR, TREE_TYPE (@1),
3112                             build_int_cst (TREE_TYPE (@1),
3113                                            element_precision (type)), @1); }))
3115 /* Turn (a OP c1) OP c2 into a OP (c1+c2).  */
3116 (for op (lrotate rrotate rshift lshift)
3117  (simplify
3118   (op (op @0 INTEGER_CST@1) INTEGER_CST@2)
3119   (with { unsigned int prec = element_precision (type); }
3120    (if (wi::ge_p (wi::to_wide (@1), 0, TYPE_SIGN (TREE_TYPE (@1)))
3121         && wi::lt_p (wi::to_wide (@1), prec, TYPE_SIGN (TREE_TYPE (@1)))
3122         && wi::ge_p (wi::to_wide (@2), 0, TYPE_SIGN (TREE_TYPE (@2)))
3123         && wi::lt_p (wi::to_wide (@2), prec, TYPE_SIGN (TREE_TYPE (@2))))
3124     (with { unsigned int low = (tree_to_uhwi (@1)
3125                                 + tree_to_uhwi (@2)); }
3126      /* Deal with a OP (c1 + c2) being undefined but (a OP c1) OP c2
3127         being well defined.  */
3128      (if (low >= prec)
3129       (if (op == LROTATE_EXPR || op == RROTATE_EXPR)
3130        (op @0 { build_int_cst (TREE_TYPE (@1), low % prec); })
3131        (if (TYPE_UNSIGNED (type) || op == LSHIFT_EXPR)
3132         { build_zero_cst (type); }
3133         (op @0 { build_int_cst (TREE_TYPE (@1), prec - 1); })))
3134       (op @0 { build_int_cst (TREE_TYPE (@1), low); })))))))
3137 /* Simplify (CST << x) & 1 to 0 if CST is even or to x == 0 if it is odd.  */
3138 (simplify
3139  (bit_and (lshift INTEGER_CST@1 @0) integer_onep)
3140   (if ((wi::to_wide (@1) & 1) != 0)
3141    (convert (eq:boolean_type_node @0 { build_zero_cst (TREE_TYPE (@0)); }))
3142    { build_zero_cst (type); }))
3144 /* Simplify ((C << x) & D) != 0 where C and D are power of two constants,
3145    either to false if D is smaller (unsigned comparison) than C, or to
3146    x == log2 (D) - log2 (C).  Similarly for right shifts.  */
3147 (for cmp (ne eq)
3148      icmp (eq ne)
3149  (simplify
3150   (cmp (bit_and (lshift integer_pow2p@1 @0) integer_pow2p@2) integer_zerop)
3151    (with { int c1 = wi::clz (wi::to_wide (@1));
3152            int c2 = wi::clz (wi::to_wide (@2)); }
3153     (if (c1 < c2)
3154      { constant_boolean_node (cmp == NE_EXPR ? false : true, type); }
3155      (icmp @0 { build_int_cst (TREE_TYPE (@0), c1 - c2); }))))
3156  (simplify
3157   (cmp (bit_and (rshift integer_pow2p@1 @0) integer_pow2p@2) integer_zerop)
3158    (if (tree_int_cst_sgn (@1) > 0)
3159     (with { int c1 = wi::clz (wi::to_wide (@1));
3160             int c2 = wi::clz (wi::to_wide (@2)); }
3161      (if (c1 > c2)
3162       { constant_boolean_node (cmp == NE_EXPR ? false : true, type); }
3163       (icmp @0 { build_int_cst (TREE_TYPE (@0), c2 - c1); }))))))
3165 /* (CST1 << A) == CST2 -> A == ctz (CST2) - ctz (CST1)
3166    (CST1 << A) != CST2 -> A != ctz (CST2) - ctz (CST1)
3167    if CST2 != 0.  */
3168 (for cmp (ne eq)
3169  (simplify
3170   (cmp (lshift INTEGER_CST@0 @1) INTEGER_CST@2)
3171   (with { int cand = wi::ctz (wi::to_wide (@2)) - wi::ctz (wi::to_wide (@0)); }
3172    (if (cand < 0
3173         || (!integer_zerop (@2)
3174             && wi::lshift (wi::to_wide (@0), cand) != wi::to_wide (@2)))
3175     { constant_boolean_node (cmp == NE_EXPR, type); }
3176     (if (!integer_zerop (@2)
3177          && wi::lshift (wi::to_wide (@0), cand) == wi::to_wide (@2))
3178      (cmp @1 { build_int_cst (TREE_TYPE (@1), cand); }))))))
3180 /* Fold (X << C1) & C2 into (X << C1) & (C2 | ((1 << C1) - 1))
3181         (X >> C1) & C2 into (X >> C1) & (C2 | ~((type) -1 >> C1))
3182    if the new mask might be further optimized.  */
3183 (for shift (lshift rshift)
3184  (simplify
3185   (bit_and (convert?:s@4 (shift:s@5 (convert1?@3 @0) INTEGER_CST@1))
3186            INTEGER_CST@2)
3187    (if (tree_nop_conversion_p (TREE_TYPE (@4), TREE_TYPE (@5))
3188         && TYPE_PRECISION (type) <= HOST_BITS_PER_WIDE_INT
3189         && tree_fits_uhwi_p (@1)
3190         && tree_to_uhwi (@1) > 0
3191         && tree_to_uhwi (@1) < TYPE_PRECISION (type))
3192     (with
3193      {
3194        unsigned int shiftc = tree_to_uhwi (@1);
3195        unsigned HOST_WIDE_INT mask = TREE_INT_CST_LOW (@2);
3196        unsigned HOST_WIDE_INT newmask, zerobits = 0;
3197        tree shift_type = TREE_TYPE (@3);
3198        unsigned int prec;
3200        if (shift == LSHIFT_EXPR)
3201          zerobits = ((HOST_WIDE_INT_1U << shiftc) - 1);
3202        else if (shift == RSHIFT_EXPR
3203                 && type_has_mode_precision_p (shift_type))
3204          {
3205            prec = TYPE_PRECISION (TREE_TYPE (@3));
3206            tree arg00 = @0;
3207            /* See if more bits can be proven as zero because of
3208               zero extension.  */
3209            if (@3 != @0
3210                && TYPE_UNSIGNED (TREE_TYPE (@0)))
3211              {
3212                tree inner_type = TREE_TYPE (@0);
3213                if (type_has_mode_precision_p (inner_type)
3214                    && TYPE_PRECISION (inner_type) < prec)
3215                  {
3216                    prec = TYPE_PRECISION (inner_type);
3217                    /* See if we can shorten the right shift.  */
3218                    if (shiftc < prec)
3219                      shift_type = inner_type;
3220                    /* Otherwise X >> C1 is all zeros, so we'll optimize
3221                       it into (X, 0) later on by making sure zerobits
3222                       is all ones.  */
3223                  }
3224              }
3225            zerobits = HOST_WIDE_INT_M1U;
3226            if (shiftc < prec)
3227              {
3228                zerobits >>= HOST_BITS_PER_WIDE_INT - shiftc;
3229                zerobits <<= prec - shiftc;
3230              }
3231            /* For arithmetic shift if sign bit could be set, zerobits
3232               can contain actually sign bits, so no transformation is
3233               possible, unless MASK masks them all away.  In that
3234               case the shift needs to be converted into logical shift.  */
3235            if (!TYPE_UNSIGNED (TREE_TYPE (@3))
3236                && prec == TYPE_PRECISION (TREE_TYPE (@3)))
3237              {
3238                if ((mask & zerobits) == 0)
3239                  shift_type = unsigned_type_for (TREE_TYPE (@3));
3240                else
3241                  zerobits = 0;
3242              }
3243          }
3244      }
3245      /* ((X << 16) & 0xff00) is (X, 0).  */
3246      (if ((mask & zerobits) == mask)
3247       { build_int_cst (type, 0); }
3248       (with { newmask = mask | zerobits; }
3249        (if (newmask != mask && (newmask & (newmask + 1)) == 0)
3250         (with
3251          {
3252            /* Only do the transformation if NEWMASK is some integer
3253               mode's mask.  */
3254            for (prec = BITS_PER_UNIT;
3255                 prec < HOST_BITS_PER_WIDE_INT; prec <<= 1)
3256              if (newmask == (HOST_WIDE_INT_1U << prec) - 1)
3257                break;
3258          }
3259          (if (prec < HOST_BITS_PER_WIDE_INT
3260               || newmask == HOST_WIDE_INT_M1U)
3261           (with
3262            { tree newmaskt = build_int_cst_type (TREE_TYPE (@2), newmask); }
3263            (if (!tree_int_cst_equal (newmaskt, @2))
3264             (if (shift_type != TREE_TYPE (@3))
3265              (bit_and (convert (shift:shift_type (convert @3) @1)) { newmaskt; })
3266              (bit_and @4 { newmaskt; })))))))))))))
3268 /* Fold (X {&,^,|} C2) << C1 into (X << C1) {&,^,|} (C2 << C1)
3269    (X {&,^,|} C2) >> C1 into (X >> C1) & (C2 >> C1).  */
3270 (for shift (lshift rshift)
3271  (for bit_op (bit_and bit_xor bit_ior)
3272   (simplify
3273    (shift (convert?:s (bit_op:s @0 INTEGER_CST@2)) INTEGER_CST@1)
3274    (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
3275     (with { tree mask = int_const_binop (shift, fold_convert (type, @2), @1); }
3276      (if (mask)
3277       (bit_op (shift (convert @0) @1) { mask; })))))))
3279 /* ~(~X >> Y) -> X >> Y (for arithmetic shift).  */
3280 (simplify
3281  (bit_not (convert1?:s (rshift:s (convert2?@0 (bit_not @1)) @2)))
3282   (if (!TYPE_UNSIGNED (TREE_TYPE (@0))
3283        && (element_precision (TREE_TYPE (@0))
3284            <= element_precision (TREE_TYPE (@1))
3285            || !TYPE_UNSIGNED (TREE_TYPE (@1))))
3286    (with
3287     { tree shift_type = TREE_TYPE (@0); }
3288      (convert (rshift (convert:shift_type @1) @2)))))
3290 /* ~(~X >>r Y) -> X >>r Y
3291    ~(~X <<r Y) -> X <<r Y */
3292 (for rotate (lrotate rrotate)
3293  (simplify
3294   (bit_not (convert1?:s (rotate:s (convert2?@0 (bit_not @1)) @2)))
3295    (if ((element_precision (TREE_TYPE (@0))
3296          <= element_precision (TREE_TYPE (@1))
3297          || !TYPE_UNSIGNED (TREE_TYPE (@1)))
3298         && (element_precision (type) <= element_precision (TREE_TYPE (@0))
3299             || !TYPE_UNSIGNED (TREE_TYPE (@0))))
3300     (with
3301      { tree rotate_type = TREE_TYPE (@0); }
3302       (convert (rotate (convert:rotate_type @1) @2))))))
3304 /* Simplifications of conversions.  */
3306 /* Basic strip-useless-type-conversions / strip_nops.  */
3307 (for cvt (convert view_convert float fix_trunc)
3308  (simplify
3309   (cvt @0)
3310   (if ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@0)))
3311        || (GENERIC && type == TREE_TYPE (@0)))
3312    @0)))
3314 /* Contract view-conversions.  */
3315 (simplify
3316   (view_convert (view_convert @0))
3317   (view_convert @0))
3319 /* For integral conversions with the same precision or pointer
3320    conversions use a NOP_EXPR instead.  */
3321 (simplify
3322   (view_convert @0)
3323   (if ((INTEGRAL_TYPE_P (type) || POINTER_TYPE_P (type))
3324        && (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
3325        && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (@0)))
3326    (convert @0)))
3328 /* Strip inner integral conversions that do not change precision or size, or
3329    zero-extend while keeping the same size (for bool-to-char).  */
3330 (simplify
3331   (view_convert (convert@0 @1))
3332   (if ((INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
3333        && (INTEGRAL_TYPE_P (TREE_TYPE (@1)) || POINTER_TYPE_P (TREE_TYPE (@1)))
3334        && TYPE_SIZE (TREE_TYPE (@0)) == TYPE_SIZE (TREE_TYPE (@1))
3335        && (TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1))
3336            || (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (TREE_TYPE (@1))
3337                && TYPE_UNSIGNED (TREE_TYPE (@1)))))
3338    (view_convert @1)))
3340 /* Simplify a view-converted empty constructor.  */
3341 (simplify
3342   (view_convert CONSTRUCTOR@0)
3343   (if (TREE_CODE (@0) != SSA_NAME
3344        && CONSTRUCTOR_NELTS (@0) == 0)
3345    { build_zero_cst (type); }))
3347 /* Re-association barriers around constants and other re-association
3348    barriers can be removed.  */
3349 (simplify
3350  (paren CONSTANT_CLASS_P@0)
3351  @0)
3352 (simplify
3353  (paren (paren@1 @0))
3354  @1)
3356 /* Handle cases of two conversions in a row.  */
3357 (for ocvt (convert float fix_trunc)
3358  (for icvt (convert float)
3359   (simplify
3360    (ocvt (icvt@1 @0))
3361    (with
3362     {
3363       tree inside_type = TREE_TYPE (@0);
3364       tree inter_type = TREE_TYPE (@1);
3365       int inside_int = INTEGRAL_TYPE_P (inside_type);
3366       int inside_ptr = POINTER_TYPE_P (inside_type);
3367       int inside_float = FLOAT_TYPE_P (inside_type);
3368       int inside_vec = VECTOR_TYPE_P (inside_type);
3369       unsigned int inside_prec = TYPE_PRECISION (inside_type);
3370       int inside_unsignedp = TYPE_UNSIGNED (inside_type);
3371       int inter_int = INTEGRAL_TYPE_P (inter_type);
3372       int inter_ptr = POINTER_TYPE_P (inter_type);
3373       int inter_float = FLOAT_TYPE_P (inter_type);
3374       int inter_vec = VECTOR_TYPE_P (inter_type);
3375       unsigned int inter_prec = TYPE_PRECISION (inter_type);
3376       int inter_unsignedp = TYPE_UNSIGNED (inter_type);
3377       int final_int = INTEGRAL_TYPE_P (type);
3378       int final_ptr = POINTER_TYPE_P (type);
3379       int final_float = FLOAT_TYPE_P (type);
3380       int final_vec = VECTOR_TYPE_P (type);
3381       unsigned int final_prec = TYPE_PRECISION (type);
3382       int final_unsignedp = TYPE_UNSIGNED (type);
3383     }
3384    (switch
3385     /* In addition to the cases of two conversions in a row
3386        handled below, if we are converting something to its own
3387        type via an object of identical or wider precision, neither
3388        conversion is needed.  */
3389     (if (((GIMPLE && useless_type_conversion_p (type, inside_type))
3390           || (GENERIC
3391               && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (inside_type)))
3392          && (((inter_int || inter_ptr) && final_int)
3393              || (inter_float && final_float))
3394          && inter_prec >= final_prec)
3395      (ocvt @0))
3397     /* Likewise, if the intermediate and initial types are either both
3398        float or both integer, we don't need the middle conversion if the
3399        former is wider than the latter and doesn't change the signedness
3400        (for integers).  Avoid this if the final type is a pointer since
3401        then we sometimes need the middle conversion.  */
3402     (if (((inter_int && inside_int) || (inter_float && inside_float))
3403          && (final_int || final_float)
3404          && inter_prec >= inside_prec
3405          && (inter_float || inter_unsignedp == inside_unsignedp))
3406      (ocvt @0))
3408     /* If we have a sign-extension of a zero-extended value, we can
3409        replace that by a single zero-extension.  Likewise if the
3410        final conversion does not change precision we can drop the
3411        intermediate conversion.  */
3412     (if (inside_int && inter_int && final_int
3413          && ((inside_prec < inter_prec && inter_prec < final_prec
3414               && inside_unsignedp && !inter_unsignedp)
3415              || final_prec == inter_prec))
3416      (ocvt @0))
3418     /* Two conversions in a row are not needed unless:
3419         - some conversion is floating-point (overstrict for now), or
3420         - some conversion is a vector (overstrict for now), or
3421         - the intermediate type is narrower than both initial and
3422           final, or
3423         - the intermediate type and innermost type differ in signedness,
3424           and the outermost type is wider than the intermediate, or
3425         - the initial type is a pointer type and the precisions of the
3426           intermediate and final types differ, or
3427         - the final type is a pointer type and the precisions of the
3428           initial and intermediate types differ.  */
3429     (if (! inside_float && ! inter_float && ! final_float
3430          && ! inside_vec && ! inter_vec && ! final_vec
3431          && (inter_prec >= inside_prec || inter_prec >= final_prec)
3432          && ! (inside_int && inter_int
3433                && inter_unsignedp != inside_unsignedp
3434                && inter_prec < final_prec)
3435          && ((inter_unsignedp && inter_prec > inside_prec)
3436              == (final_unsignedp && final_prec > inter_prec))
3437          && ! (inside_ptr && inter_prec != final_prec)
3438          && ! (final_ptr && inside_prec != inter_prec))
3439      (ocvt @0))
3441     /* A truncation to an unsigned type (a zero-extension) should be
3442        canonicalized as bitwise and of a mask.  */
3443     (if (GIMPLE /* PR70366: doing this in GENERIC breaks -Wconversion.  */
3444          && final_int && inter_int && inside_int
3445          && final_prec == inside_prec
3446          && final_prec > inter_prec
3447          && inter_unsignedp)
3448      (convert (bit_and @0 { wide_int_to_tree
3449                               (inside_type,
3450                                wi::mask (inter_prec, false,
3451                                          TYPE_PRECISION (inside_type))); })))
3453     /* If we are converting an integer to a floating-point that can
3454        represent it exactly and back to an integer, we can skip the
3455        floating-point conversion.  */
3456     (if (GIMPLE /* PR66211 */
3457          && inside_int && inter_float && final_int &&
3458          (unsigned) significand_size (TYPE_MODE (inter_type))
3459          >= inside_prec - !inside_unsignedp)
3460      (convert @0)))))))
3462 /* If we have a narrowing conversion to an integral type that is fed by a
3463    BIT_AND_EXPR, we might be able to remove the BIT_AND_EXPR if it merely
3464    masks off bits outside the final type (and nothing else).  */
3465 (simplify
3466   (convert (bit_and @0 INTEGER_CST@1))
3467   (if (INTEGRAL_TYPE_P (type)
3468        && INTEGRAL_TYPE_P (TREE_TYPE (@0))
3469        && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))
3470        && operand_equal_p (@1, build_low_bits_mask (TREE_TYPE (@1),
3471                                                     TYPE_PRECISION (type)), 0))
3472    (convert @0)))
3475 /* (X /[ex] A) * A -> X.  */
3476 (simplify
3477   (mult (convert1? (exact_div @0 @@1)) (convert2? @1))
3478   (convert @0))
3480 /* Simplify (A / B) * B + (A % B) -> A.  */
3481 (for div (trunc_div ceil_div floor_div round_div)
3482      mod (trunc_mod ceil_mod floor_mod round_mod)
3483   (simplify
3484    (plus:c (mult:c (div @0 @1) @1) (mod @0 @1))
3485    @0))
3487 /* ((X /[ex] A) +- B) * A  -->  X +- A * B.  */
3488 (for op (plus minus)
3489  (simplify
3490   (mult (convert1? (op (convert2? (exact_div @0 INTEGER_CST@@1)) INTEGER_CST@2)) @1)
3491   (if (tree_nop_conversion_p (type, TREE_TYPE (@2))
3492        && tree_nop_conversion_p (TREE_TYPE (@0), TREE_TYPE (@2)))
3493    (with
3494      {
3495        wi::overflow_type overflow;
3496        wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@2),
3497                                TYPE_SIGN (type), &overflow);
3498      }
3499      (if (types_match (type, TREE_TYPE (@2))
3500          && types_match (TREE_TYPE (@0), TREE_TYPE (@2)) && !overflow)
3501       (op @0 { wide_int_to_tree (type, mul); })
3502       (with { tree utype = unsigned_type_for (type); }
3503        (convert (op (convert:utype @0)
3504                     (mult (convert:utype @1) (convert:utype @2))))))))))
3506 /* Canonicalization of binary operations.  */
3508 /* Convert X + -C into X - C.  */
3509 (simplify
3510  (plus @0 REAL_CST@1)
3511  (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
3512   (with { tree tem = const_unop (NEGATE_EXPR, type, @1); }
3513    (if (!TREE_OVERFLOW (tem) || !flag_trapping_math)
3514     (minus @0 { tem; })))))
3516 /* Convert x+x into x*2.  */
3517 (simplify
3518  (plus @0 @0)
3519  (if (SCALAR_FLOAT_TYPE_P (type))
3520   (mult @0 { build_real (type, dconst2); })
3521   (if (INTEGRAL_TYPE_P (type))
3522    (mult @0 { build_int_cst (type, 2); }))))
3524 /* 0 - X  ->  -X.  */
3525 (simplify
3526  (minus integer_zerop @1)
3527  (negate @1))
3528 (simplify
3529  (pointer_diff integer_zerop @1)
3530  (negate (convert @1)))
3532 /* (ARG0 - ARG1) is the same as (-ARG1 + ARG0).  So check whether
3533    ARG0 is zero and X + ARG0 reduces to X, since that would mean
3534    (-ARG1 + ARG0) reduces to -ARG1.  */
3535 (simplify
3536  (minus real_zerop@0 @1)
3537  (if (fold_real_zero_addition_p (type, @0, 0))
3538   (negate @1)))
3540 /* Transform x * -1 into -x.  */
3541 (simplify
3542  (mult @0 integer_minus_onep)
3543  (negate @0))
3545 /* Reassociate (X * CST) * Y to (X * Y) * CST.  This does not introduce
3546    signed overflow for CST != 0 && CST != -1.  */
3547 (simplify
3548  (mult:c (mult:s@3 @0 INTEGER_CST@1) @2)
3549  (if (TREE_CODE (@2) != INTEGER_CST
3550       && single_use (@3)
3551       && !integer_zerop (@1) && !integer_minus_onep (@1))
3552   (mult (mult @0 @2) @1)))
3554 /* True if we can easily extract the real and imaginary parts of a complex
3555    number.  */
3556 (match compositional_complex
3557  (convert? (complex @0 @1)))
3559 /* COMPLEX_EXPR and REALPART/IMAGPART_EXPR cancellations.  */
3560 (simplify
3561  (complex (realpart @0) (imagpart @0))
3562  @0)
3563 (simplify
3564  (realpart (complex @0 @1))
3565  @0)
3566 (simplify
3567  (imagpart (complex @0 @1))
3568  @1)
3570 /* Sometimes we only care about half of a complex expression.  */
3571 (simplify
3572  (realpart (convert?:s (conj:s @0)))
3573  (convert (realpart @0)))
3574 (simplify
3575  (imagpart (convert?:s (conj:s @0)))
3576  (convert (negate (imagpart @0))))
3577 (for part (realpart imagpart)
3578  (for op (plus minus)
3579   (simplify
3580    (part (convert?:s@2 (op:s @0 @1)))
3581    (convert (op (part @0) (part @1))))))
3582 (simplify
3583  (realpart (convert?:s (CEXPI:s @0)))
3584  (convert (COS @0)))
3585 (simplify
3586  (imagpart (convert?:s (CEXPI:s @0)))
3587  (convert (SIN @0)))
3589 /* conj(conj(x)) -> x  */
3590 (simplify
3591  (conj (convert? (conj @0)))
3592  (if (tree_nop_conversion_p (TREE_TYPE (@0), type))
3593   (convert @0)))
3595 /* conj({x,y}) -> {x,-y}  */
3596 (simplify
3597  (conj (convert?:s (complex:s @0 @1)))
3598  (with { tree itype = TREE_TYPE (type); }
3599   (complex (convert:itype @0) (negate (convert:itype @1)))))
3601 /* BSWAP simplifications, transforms checked by gcc.dg/builtin-bswap-8.c.  */
3602 (for bswap (BUILT_IN_BSWAP16 BUILT_IN_BSWAP32 BUILT_IN_BSWAP64)
3603  (simplify
3604   (bswap (bswap @0))
3605   @0)
3606  (simplify
3607   (bswap (bit_not (bswap @0)))
3608   (bit_not @0))
3609  (for bitop (bit_xor bit_ior bit_and)
3610   (simplify
3611    (bswap (bitop:c (bswap @0) @1))
3612    (bitop @0 (bswap @1)))))
3615 /* Combine COND_EXPRs and VEC_COND_EXPRs.  */
3617 /* Simplify constant conditions.
3618    Only optimize constant conditions when the selected branch
3619    has the same type as the COND_EXPR.  This avoids optimizing
3620    away "c ? x : throw", where the throw has a void type.
3621    Note that we cannot throw away the fold-const.c variant nor
3622    this one as we depend on doing this transform before possibly
3623    A ? B : B -> B triggers and the fold-const.c one can optimize
3624    0 ? A : B to B even if A has side-effects.  Something
3625    genmatch cannot handle.  */
3626 (simplify
3627  (cond INTEGER_CST@0 @1 @2)
3628  (if (integer_zerop (@0))
3629   (if (!VOID_TYPE_P (TREE_TYPE (@2)) || VOID_TYPE_P (type))
3630    @2)
3631   (if (!VOID_TYPE_P (TREE_TYPE (@1)) || VOID_TYPE_P (type))
3632    @1)))
3633 (simplify
3634  (vec_cond VECTOR_CST@0 @1 @2)
3635  (if (integer_all_onesp (@0))
3636   @1
3637   (if (integer_zerop (@0))
3638    @2)))
3640 #if GIMPLE
3641 /* Sink unary operations to branches, but only if we do fold both.  */
3642 (for op (negate bit_not abs absu)
3643  (simplify
3644   (op (vec_cond:s @0 @1 @2))
3645   (vec_cond @0 (op! @1) (op! @2))))
3647 /* Sink binary operation to branches, but only if we can fold it.  */
3648 (for op (tcc_comparison plus minus mult bit_and bit_ior bit_xor
3649          rdiv trunc_div ceil_div floor_div round_div
3650          trunc_mod ceil_mod floor_mod round_mod min max)
3651 /* (c ? a : b) op (c ? d : e)  -->  c ? (a op d) : (b op e) */
3652  (simplify
3653   (op (vec_cond:s @0 @1 @2) (vec_cond:s @0 @3 @4))
3654   (vec_cond @0 (op! @1 @3) (op! @2 @4)))
3656 /* (c ? a : b) op d  -->  c ? (a op d) : (b op d) */
3657  (simplify
3658   (op (vec_cond:s @0 @1 @2) @3)
3659   (vec_cond @0 (op! @1 @3) (op! @2 @3)))
3660  (simplify
3661   (op @3 (vec_cond:s @0 @1 @2))
3662   (vec_cond @0 (op! @3 @1) (op! @3 @2))))
3663 #endif
3665 /* (v ? w : 0) ? a : b is just (v & w) ? a : b
3666    Currently disabled after pass lvec because ARM understands
3667    VEC_COND_EXPR<v==w,-1,0> but not a plain v==w fed to BIT_IOR_EXPR.  */
3668 (simplify
3669  (vec_cond (vec_cond:s @0 @3 integer_zerop) @1 @2)
3670  (if (optimize_vectors_before_lowering_p () && types_match (@0, @3))
3671   (vec_cond (bit_and @0 @3) @1 @2)))
3672 (simplify
3673  (vec_cond (vec_cond:s @0 integer_all_onesp @3) @1 @2)
3674  (if (optimize_vectors_before_lowering_p () && types_match (@0, @3))
3675   (vec_cond (bit_ior @0 @3) @1 @2)))
3676 (simplify
3677  (vec_cond (vec_cond:s @0 integer_zerop @3) @1 @2)
3678  (if (optimize_vectors_before_lowering_p () && types_match (@0, @3))
3679   (vec_cond (bit_ior @0 (bit_not @3)) @2 @1)))
3680 (simplify
3681  (vec_cond (vec_cond:s @0 @3 integer_all_onesp) @1 @2)
3682  (if (optimize_vectors_before_lowering_p () && types_match (@0, @3))
3683   (vec_cond (bit_and @0 (bit_not @3)) @2 @1)))
3685 /* c1 ? c2 ? a : b : b  -->  (c1 & c2) ? a : b  */
3686 (simplify
3687  (vec_cond @0 (vec_cond:s @1 @2 @3) @3)
3688  (if (optimize_vectors_before_lowering_p () && types_match (@0, @1))
3689   (vec_cond (bit_and @0 @1) @2 @3)))
3690 (simplify
3691  (vec_cond @0 @2 (vec_cond:s @1 @2 @3))
3692  (if (optimize_vectors_before_lowering_p () && types_match (@0, @1))
3693   (vec_cond (bit_ior @0 @1) @2 @3)))
3694 (simplify
3695  (vec_cond @0 (vec_cond:s @1 @2 @3) @2)
3696  (if (optimize_vectors_before_lowering_p () && types_match (@0, @1))
3697   (vec_cond (bit_ior (bit_not @0) @1) @2 @3)))
3698 (simplify
3699  (vec_cond @0 @3 (vec_cond:s @1 @2 @3))
3700  (if (optimize_vectors_before_lowering_p () && types_match (@0, @1))
3701   (vec_cond (bit_and (bit_not @0) @1) @2 @3)))
3703 /* Canonicalize mask ? { 0, ... } : { -1, ...} to ~mask if the mask
3704    types are compatible.  */
3705 (simplify
3706  (vec_cond @0 VECTOR_CST@1 VECTOR_CST@2)
3707  (if (VECTOR_BOOLEAN_TYPE_P (type)
3708       && types_match (type, TREE_TYPE (@0)))
3709   (if (integer_zerop (@1) && integer_all_onesp (@2))
3710    (bit_not @0)
3711    (if (integer_all_onesp (@1) && integer_zerop (@2))
3712     @0))))
3714 /* Simplification moved from fold_cond_expr_with_comparison.  It may also
3715    be extended.  */
3716 /* This pattern implements two kinds simplification:
3718    Case 1)
3719    (cond (cmp (convert1? x) c1) (convert2? x) c2) -> (minmax (x c)) if:
3720      1) Conversions are type widening from smaller type.
3721      2) Const c1 equals to c2 after canonicalizing comparison.
3722      3) Comparison has tree code LT, LE, GT or GE.
3723    This specific pattern is needed when (cmp (convert x) c) may not
3724    be simplified by comparison patterns because of multiple uses of
3725    x.  It also makes sense here because simplifying across multiple
3726    referred var is always benefitial for complicated cases.
3728    Case 2)
3729    (cond (eq (convert1? x) c1) (convert2? x) c2) -> (cond (eq x c1) c1 c2).  */
3730 (for cmp (lt le gt ge eq)
3731  (simplify
3732   (cond (cmp (convert1? @1) INTEGER_CST@3) (convert2? @1) INTEGER_CST@2)
3733   (with
3734    {
3735      tree from_type = TREE_TYPE (@1);
3736      tree c1_type = TREE_TYPE (@3), c2_type = TREE_TYPE (@2);
3737      enum tree_code code = ERROR_MARK;
3739      if (INTEGRAL_TYPE_P (from_type)
3740          && int_fits_type_p (@2, from_type)
3741          && (types_match (c1_type, from_type)
3742              || (TYPE_PRECISION (c1_type) > TYPE_PRECISION (from_type)
3743                  && (TYPE_UNSIGNED (from_type)
3744                      || TYPE_SIGN (c1_type) == TYPE_SIGN (from_type))))
3745          && (types_match (c2_type, from_type)
3746              || (TYPE_PRECISION (c2_type) > TYPE_PRECISION (from_type)
3747                  && (TYPE_UNSIGNED (from_type)
3748                      || TYPE_SIGN (c2_type) == TYPE_SIGN (from_type)))))
3749        {
3750          if (cmp != EQ_EXPR)
3751            {
3752              if (wi::to_widest (@3) == (wi::to_widest (@2) - 1))
3753                {
3754                  /* X <= Y - 1 equals to X < Y.  */
3755                  if (cmp == LE_EXPR)
3756                    code = LT_EXPR;
3757                  /* X > Y - 1 equals to X >= Y.  */
3758                  if (cmp == GT_EXPR)
3759                    code = GE_EXPR;
3760                }
3761              if (wi::to_widest (@3) == (wi::to_widest (@2) + 1))
3762                {
3763                  /* X < Y + 1 equals to X <= Y.  */
3764                  if (cmp == LT_EXPR)
3765                    code = LE_EXPR;
3766                  /* X >= Y + 1 equals to X > Y.  */
3767                  if (cmp == GE_EXPR)
3768                    code = GT_EXPR;
3769                }
3770              if (code != ERROR_MARK
3771                  || wi::to_widest (@2) == wi::to_widest (@3))
3772                {
3773                  if (cmp == LT_EXPR || cmp == LE_EXPR)
3774                    code = MIN_EXPR;
3775                  if (cmp == GT_EXPR || cmp == GE_EXPR)
3776                    code = MAX_EXPR;
3777                }
3778            }
3779          /* Can do A == C1 ? A : C2  ->  A == C1 ? C1 : C2?  */
3780          else if (int_fits_type_p (@3, from_type))
3781            code = EQ_EXPR;
3782        }
3783    }
3784    (if (code == MAX_EXPR)
3785     (convert (max @1 (convert @2)))
3786     (if (code == MIN_EXPR)
3787      (convert (min @1 (convert @2)))
3788      (if (code == EQ_EXPR)
3789       (convert (cond (eq @1 (convert @3))
3790                      (convert:from_type @3) (convert:from_type @2)))))))))
3792 /* (cond (cmp (convert? x) c1) (op x c2) c3) -> (op (minmax x c1) c2) if:
3794      1) OP is PLUS or MINUS.
3795      2) CMP is LT, LE, GT or GE.
3796      3) C3 == (C1 op C2), and computation doesn't have undefined behavior.
3798    This pattern also handles special cases like:
3800      A) Operand x is a unsigned to signed type conversion and c1 is
3801         integer zero.  In this case,
3802           (signed type)x  < 0  <=>  x  > MAX_VAL(signed type)
3803           (signed type)x >= 0  <=>  x <= MAX_VAL(signed type)
3804      B) Const c1 may not equal to (C3 op' C2).  In this case we also
3805         check equality for (c1+1) and (c1-1) by adjusting comparison
3806         code.
3808    TODO: Though signed type is handled by this pattern, it cannot be
3809    simplified at the moment because C standard requires additional
3810    type promotion.  In order to match&simplify it here, the IR needs
3811    to be cleaned up by other optimizers, i.e, VRP.  */
3812 (for op (plus minus)
3813  (for cmp (lt le gt ge)
3814   (simplify
3815    (cond (cmp (convert? @X) INTEGER_CST@1) (op @X INTEGER_CST@2) INTEGER_CST@3)
3816    (with { tree from_type = TREE_TYPE (@X), to_type = TREE_TYPE (@1); }
3817     (if (types_match (from_type, to_type)
3818          /* Check if it is special case A).  */
3819          || (TYPE_UNSIGNED (from_type)
3820              && !TYPE_UNSIGNED (to_type)
3821              && TYPE_PRECISION (from_type) == TYPE_PRECISION (to_type)
3822              && integer_zerop (@1)
3823              && (cmp == LT_EXPR || cmp == GE_EXPR)))
3824      (with
3825       {
3826         wi::overflow_type overflow = wi::OVF_NONE;
3827         enum tree_code code, cmp_code = cmp;
3828         wide_int real_c1;
3829         wide_int c1 = wi::to_wide (@1);
3830         wide_int c2 = wi::to_wide (@2);
3831         wide_int c3 = wi::to_wide (@3);
3832         signop sgn = TYPE_SIGN (from_type);
3834         /* Handle special case A), given x of unsigned type:
3835             ((signed type)x  < 0) <=> (x  > MAX_VAL(signed type))
3836             ((signed type)x >= 0) <=> (x <= MAX_VAL(signed type))  */
3837         if (!types_match (from_type, to_type))
3838           {
3839             if (cmp_code == LT_EXPR)
3840               cmp_code = GT_EXPR;
3841             if (cmp_code == GE_EXPR)
3842               cmp_code = LE_EXPR;
3843             c1 = wi::max_value (to_type);
3844           }
3845         /* To simplify this pattern, we require c3 = (c1 op c2).  Here we
3846            compute (c3 op' c2) and check if it equals to c1 with op' being
3847            the inverted operator of op.  Make sure overflow doesn't happen
3848            if it is undefined.  */
3849         if (op == PLUS_EXPR)
3850           real_c1 = wi::sub (c3, c2, sgn, &overflow);
3851         else
3852           real_c1 = wi::add (c3, c2, sgn, &overflow);
3854         code = cmp_code;
3855         if (!overflow || !TYPE_OVERFLOW_UNDEFINED (from_type))
3856           {
3857             /* Check if c1 equals to real_c1.  Boundary condition is handled
3858                by adjusting comparison operation if necessary.  */
3859             if (!wi::cmp (wi::sub (real_c1, 1, sgn, &overflow), c1, sgn)
3860                 && !overflow)
3861               {
3862                 /* X <= Y - 1 equals to X < Y.  */
3863                 if (cmp_code == LE_EXPR)
3864                   code = LT_EXPR;
3865                 /* X > Y - 1 equals to X >= Y.  */
3866                 if (cmp_code == GT_EXPR)
3867                   code = GE_EXPR;
3868               }
3869             if (!wi::cmp (wi::add (real_c1, 1, sgn, &overflow), c1, sgn)
3870                 && !overflow)
3871               {
3872                 /* X < Y + 1 equals to X <= Y.  */
3873                 if (cmp_code == LT_EXPR)
3874                   code = LE_EXPR;
3875                 /* X >= Y + 1 equals to X > Y.  */
3876                 if (cmp_code == GE_EXPR)
3877                   code = GT_EXPR;
3878               }
3879             if (code != cmp_code || !wi::cmp (real_c1, c1, sgn))
3880               {
3881                 if (cmp_code == LT_EXPR || cmp_code == LE_EXPR)
3882                   code = MIN_EXPR;
3883                 if (cmp_code == GT_EXPR || cmp_code == GE_EXPR)
3884                   code = MAX_EXPR;
3885               }
3886           }
3887       }
3888       (if (code == MAX_EXPR)
3889        (op (max @X { wide_int_to_tree (from_type, real_c1); })
3890            { wide_int_to_tree (from_type, c2); })
3891        (if (code == MIN_EXPR)
3892         (op (min @X { wide_int_to_tree (from_type, real_c1); })
3893             { wide_int_to_tree (from_type, c2); })))))))))
3895 (for cnd (cond vec_cond)
3896  /* A ? B : (A ? X : C) -> A ? B : C.  */
3897  (simplify
3898   (cnd @0 (cnd @0 @1 @2) @3)
3899   (cnd @0 @1 @3))
3900  (simplify
3901   (cnd @0 @1 (cnd @0 @2 @3))
3902   (cnd @0 @1 @3))
3903  /* A ? B : (!A ? C : X) -> A ? B : C.  */
3904  /* ???  This matches embedded conditions open-coded because genmatch
3905     would generate matching code for conditions in separate stmts only.
3906     The following is still important to merge then and else arm cases
3907     from if-conversion.  */
3908  (simplify
3909   (cnd @0 @1 (cnd @2 @3 @4))
3910   (if (inverse_conditions_p (@0, @2))
3911    (cnd @0 @1 @3)))
3912  (simplify
3913   (cnd @0 (cnd @1 @2 @3) @4)
3914   (if (inverse_conditions_p (@0, @1))
3915    (cnd @0 @3 @4)))
3917  /* A ? B : B -> B.  */
3918  (simplify
3919   (cnd @0 @1 @1)
3920   @1)
3922  /* !A ? B : C -> A ? C : B.  */
3923  (simplify
3924   (cnd (logical_inverted_value truth_valued_p@0) @1 @2)
3925   (cnd @0 @2 @1)))
3927 /* -(type)!A -> (type)A - 1.  */
3928 (simplify
3929  (negate (convert?:s (logical_inverted_value:s @0)))
3930  (if (INTEGRAL_TYPE_P (type)
3931       && TREE_CODE (type) != BOOLEAN_TYPE
3932       && TYPE_PRECISION (type) > 1
3933       && TREE_CODE (@0) == SSA_NAME
3934       && ssa_name_has_boolean_range (@0))
3935   (plus (convert:type @0) { build_all_ones_cst (type); })))
3937 /* A + (B vcmp C ? 1 : 0) -> A - (B vcmp C ? -1 : 0), since vector comparisons
3938    return all -1 or all 0 results.  */
3939 /* ??? We could instead convert all instances of the vec_cond to negate,
3940    but that isn't necessarily a win on its own.  */
3941 (simplify
3942  (plus:c @3 (view_convert? (vec_cond:s @0 integer_each_onep@1 integer_zerop@2)))
3943  (if (VECTOR_TYPE_P (type)
3944       && known_eq (TYPE_VECTOR_SUBPARTS (type),
3945                    TYPE_VECTOR_SUBPARTS (TREE_TYPE (@1)))
3946       && (TYPE_MODE (TREE_TYPE (type))
3947           == TYPE_MODE (TREE_TYPE (TREE_TYPE (@1)))))
3948   (minus @3 (view_convert (vec_cond @0 (negate @1) @2)))))
3950 /* ... likewise A - (B vcmp C ? 1 : 0) -> A + (B vcmp C ? -1 : 0).  */
3951 (simplify
3952  (minus @3 (view_convert? (vec_cond:s @0 integer_each_onep@1 integer_zerop@2)))
3953  (if (VECTOR_TYPE_P (type)
3954       && known_eq (TYPE_VECTOR_SUBPARTS (type),
3955                    TYPE_VECTOR_SUBPARTS (TREE_TYPE (@1)))
3956       && (TYPE_MODE (TREE_TYPE (type))
3957           == TYPE_MODE (TREE_TYPE (TREE_TYPE (@1)))))
3958   (plus @3 (view_convert (vec_cond @0 (negate @1) @2)))))
3961 /* Simplifications of comparisons.  */
3963 /* See if we can reduce the magnitude of a constant involved in a
3964    comparison by changing the comparison code.  This is a canonicalization
3965    formerly done by maybe_canonicalize_comparison_1.  */
3966 (for cmp  (le gt)
3967      acmp (lt ge)
3968  (simplify
3969   (cmp @0 uniform_integer_cst_p@1)
3970   (with { tree cst = uniform_integer_cst_p (@1); }
3971    (if (tree_int_cst_sgn (cst) == -1)
3972      (acmp @0 { build_uniform_cst (TREE_TYPE (@1),
3973                                    wide_int_to_tree (TREE_TYPE (cst),
3974                                                      wi::to_wide (cst)
3975                                                      + 1)); })))))
3976 (for cmp  (ge lt)
3977      acmp (gt le)
3978  (simplify
3979   (cmp @0 uniform_integer_cst_p@1)
3980   (with { tree cst = uniform_integer_cst_p (@1); }
3981    (if (tree_int_cst_sgn (cst) == 1)
3982     (acmp @0 { build_uniform_cst (TREE_TYPE (@1),
3983                                   wide_int_to_tree (TREE_TYPE (cst),
3984                                   wi::to_wide (cst) - 1)); })))))
3986 /* We can simplify a logical negation of a comparison to the
3987    inverted comparison.  As we cannot compute an expression
3988    operator using invert_tree_comparison we have to simulate
3989    that with expression code iteration.  */
3990 (for cmp (tcc_comparison)
3991      icmp (inverted_tcc_comparison)
3992      ncmp (inverted_tcc_comparison_with_nans)
3993  /* Ideally we'd like to combine the following two patterns
3994     and handle some more cases by using
3995       (logical_inverted_value (cmp @0 @1))
3996     here but for that genmatch would need to "inline" that.
3997     For now implement what forward_propagate_comparison did.  */
3998  (simplify
3999   (bit_not (cmp @0 @1))
4000   (if (VECTOR_TYPE_P (type)
4001        || (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1))
4002    /* Comparison inversion may be impossible for trapping math,
4003       invert_tree_comparison will tell us.  But we can't use
4004       a computed operator in the replacement tree thus we have
4005       to play the trick below.  */
4006    (with { enum tree_code ic = invert_tree_comparison
4007              (cmp, HONOR_NANS (@0)); }
4008     (if (ic == icmp)
4009      (icmp @0 @1)
4010      (if (ic == ncmp)
4011       (ncmp @0 @1))))))
4012  (simplify
4013   (bit_xor (cmp @0 @1) integer_truep)
4014   (with { enum tree_code ic = invert_tree_comparison
4015             (cmp, HONOR_NANS (@0)); }
4016    (if (ic == icmp)
4017     (icmp @0 @1)
4018     (if (ic == ncmp)
4019      (ncmp @0 @1))))))
4021 /* Transform comparisons of the form X - Y CMP 0 to X CMP Y.
4022    ??? The transformation is valid for the other operators if overflow
4023    is undefined for the type, but performing it here badly interacts
4024    with the transformation in fold_cond_expr_with_comparison which
4025    attempts to synthetize ABS_EXPR.  */
4026 (for cmp (eq ne)
4027  (for sub (minus pointer_diff)
4028   (simplify
4029    (cmp (sub@2 @0 @1) integer_zerop)
4030    (if (single_use (@2))
4031     (cmp @0 @1)))))
4033 /* Simplify (x < 0) ^ (y < 0) to (x ^ y) < 0 and
4034    (x >= 0) ^ (y >= 0) to (x ^ y) < 0.  */
4035 (for cmp (lt ge)
4036  (simplify
4037   (bit_xor (cmp:s @0 integer_zerop) (cmp:s @1 integer_zerop))
4038    (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
4039         && !TYPE_UNSIGNED (TREE_TYPE (@0))
4040         && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
4041     (lt (bit_xor @0 @1) { build_zero_cst (TREE_TYPE (@0)); }))))
4042 /* Simplify (x < 0) ^ (y >= 0) to (x ^ y) >= 0 and
4043    (x >= 0) ^ (y < 0) to (x ^ y) >= 0.  */
4044 (simplify
4045  (bit_xor:c (lt:s @0 integer_zerop) (ge:s @1 integer_zerop))
4046   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
4047        && !TYPE_UNSIGNED (TREE_TYPE (@0))
4048        && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
4049    (ge (bit_xor @0 @1) { build_zero_cst (TREE_TYPE (@0)); })))
4051 /* Transform comparisons of the form X * C1 CMP 0 to X CMP 0 in the
4052    signed arithmetic case.  That form is created by the compiler
4053    often enough for folding it to be of value.  One example is in
4054    computing loop trip counts after Operator Strength Reduction.  */
4055 (for cmp (simple_comparison)
4056      scmp (swapped_simple_comparison)
4057  (simplify
4058   (cmp (mult@3 @0 INTEGER_CST@1) integer_zerop@2)
4059   /* Handle unfolded multiplication by zero.  */
4060   (if (integer_zerop (@1))
4061    (cmp @1 @2)
4062    (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
4063         && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
4064         && single_use (@3))
4065     /* If @1 is negative we swap the sense of the comparison.  */
4066     (if (tree_int_cst_sgn (@1) < 0)
4067      (scmp @0 @2)
4068      (cmp @0 @2))))))
4070 /* For integral types with undefined overflow fold
4071    x * C1 == C2 into x == C2 / C1 or false.
4072    If overflow wraps and C1 is odd, simplify to x == C2 / C1 in the ring
4073    Z / 2^n Z.  */
4074 (for cmp (eq ne)
4075  (simplify
4076   (cmp (mult @0 INTEGER_CST@1) INTEGER_CST@2)
4077   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
4078        && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
4079        && wi::to_wide (@1) != 0)
4080    (with { widest_int quot; }
4081     (if (wi::multiple_of_p (wi::to_widest (@2), wi::to_widest (@1),
4082                             TYPE_SIGN (TREE_TYPE (@0)), &quot))
4083      (cmp @0 { wide_int_to_tree (TREE_TYPE (@0), quot); })
4084      { constant_boolean_node (cmp == NE_EXPR, type); }))
4085    (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
4086         && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))
4087         && (wi::bit_and (wi::to_wide (@1), 1) == 1))
4088     (cmp @0
4089      {
4090        tree itype = TREE_TYPE (@0);
4091        int p = TYPE_PRECISION (itype);
4092        wide_int m = wi::one (p + 1) << p;
4093        wide_int a = wide_int::from (wi::to_wide (@1), p + 1, UNSIGNED);
4094        wide_int i = wide_int::from (wi::mod_inv (a, m),
4095                                     p, TYPE_SIGN (itype));
4096        wide_int_to_tree (itype, wi::mul (i, wi::to_wide (@2)));
4097      })))))
4099 /* Simplify comparison of something with itself.  For IEEE
4100    floating-point, we can only do some of these simplifications.  */
4101 (for cmp (eq ge le)
4102  (simplify
4103   (cmp @0 @0)
4104   (if (! FLOAT_TYPE_P (TREE_TYPE (@0))
4105        || ! HONOR_NANS (@0))
4106    { constant_boolean_node (true, type); }
4107    (if (cmp != EQ_EXPR)
4108     (eq @0 @0)))))
4109 (for cmp (ne gt lt)
4110  (simplify
4111   (cmp @0 @0)
4112   (if (cmp != NE_EXPR
4113        || ! FLOAT_TYPE_P (TREE_TYPE (@0))
4114        || ! HONOR_NANS (@0))
4115    { constant_boolean_node (false, type); })))
4116 (for cmp (unle unge uneq)
4117  (simplify
4118   (cmp @0 @0)
4119   { constant_boolean_node (true, type); }))
4120 (for cmp (unlt ungt)
4121  (simplify
4122   (cmp @0 @0)
4123   (unordered @0 @0)))
4124 (simplify
4125  (ltgt @0 @0)
4126  (if (!flag_trapping_math)
4127   { constant_boolean_node (false, type); }))
4129 /* x == ~x -> false */
4130 /* x != ~x -> true */
4131 (for cmp (eq ne)
4132  (simplify
4133   (cmp:c @0 (bit_not @0))
4134   { constant_boolean_node (cmp == NE_EXPR, type); }))
4136 /* Fold ~X op ~Y as Y op X.  */
4137 (for cmp (simple_comparison)
4138  (simplify
4139   (cmp (bit_not@2 @0) (bit_not@3 @1))
4140   (if (single_use (@2) && single_use (@3))
4141    (cmp @1 @0))))
4143 /* Fold ~X op C as X op' ~C, where op' is the swapped comparison.  */
4144 (for cmp (simple_comparison)
4145      scmp (swapped_simple_comparison)
4146  (simplify
4147   (cmp (bit_not@2 @0) CONSTANT_CLASS_P@1)
4148   (if (single_use (@2)
4149        && (TREE_CODE (@1) == INTEGER_CST || TREE_CODE (@1) == VECTOR_CST))
4150    (scmp @0 (bit_not @1)))))
4152 (for cmp (simple_comparison)
4153  /* Fold (double)float1 CMP (double)float2 into float1 CMP float2.  */
4154  (simplify
4155   (cmp (convert@2 @0) (convert? @1))
4156   (if (FLOAT_TYPE_P (TREE_TYPE (@0))
4157        && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@2))
4158            == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0)))
4159        && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@2))
4160            == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1))))
4161    (with
4162     {
4163       tree type1 = TREE_TYPE (@1);
4164       if (TREE_CODE (@1) == REAL_CST && !DECIMAL_FLOAT_TYPE_P (type1))
4165         {
4166           REAL_VALUE_TYPE orig = TREE_REAL_CST (@1);
4167           if (TYPE_PRECISION (type1) > TYPE_PRECISION (float_type_node)
4168               && exact_real_truncate (TYPE_MODE (float_type_node), &orig))
4169             type1 = float_type_node;
4170           if (TYPE_PRECISION (type1) > TYPE_PRECISION (double_type_node)
4171               && exact_real_truncate (TYPE_MODE (double_type_node), &orig))
4172             type1 = double_type_node;
4173         }
4174       tree newtype
4175         = (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type1)
4176            ? TREE_TYPE (@0) : type1);
4177     }
4178     (if (TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (newtype))
4179      (cmp (convert:newtype @0) (convert:newtype @1))))))
4181  (simplify
4182   (cmp @0 REAL_CST@1)
4183   /* IEEE doesn't distinguish +0 and -0 in comparisons.  */
4184   (switch
4185    /* a CMP (-0) -> a CMP 0  */
4186    (if (REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@1)))
4187     (cmp @0 { build_real (TREE_TYPE (@1), dconst0); }))
4188    /* x != NaN is always true, other ops are always false.  */
4189    (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
4190         && ! HONOR_SNANS (@1))
4191     { constant_boolean_node (cmp == NE_EXPR, type); })
4192    /* Fold comparisons against infinity.  */
4193    (if (REAL_VALUE_ISINF (TREE_REAL_CST (@1))
4194         && MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (@1))))
4195     (with
4196      {
4197        REAL_VALUE_TYPE max;
4198        enum tree_code code = cmp;
4199        bool neg = REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1));
4200        if (neg)
4201          code = swap_tree_comparison (code);
4202      }
4203      (switch
4204       /* x > +Inf is always false, if we ignore NaNs or exceptions.  */
4205       (if (code == GT_EXPR
4206            && !(HONOR_NANS (@0) && flag_trapping_math))
4207        { constant_boolean_node (false, type); })
4208       (if (code == LE_EXPR)
4209        /* x <= +Inf is always true, if we don't care about NaNs.  */
4210        (if (! HONOR_NANS (@0))
4211         { constant_boolean_node (true, type); }
4212         /* x <= +Inf is the same as x == x, i.e. !isnan(x), but this loses
4213            an "invalid" exception.  */
4214         (if (!flag_trapping_math)
4215          (eq @0 @0))))
4216       /* x == +Inf and x >= +Inf are always equal to x > DBL_MAX, but
4217          for == this introduces an exception for x a NaN.  */
4218       (if ((code == EQ_EXPR && !(HONOR_NANS (@0) && flag_trapping_math))
4219            || code == GE_EXPR)
4220        (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
4221         (if (neg)
4222          (lt @0 { build_real (TREE_TYPE (@0), max); })
4223          (gt @0 { build_real (TREE_TYPE (@0), max); }))))
4224       /* x < +Inf is always equal to x <= DBL_MAX.  */
4225       (if (code == LT_EXPR)
4226        (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
4227         (if (neg)
4228          (ge @0 { build_real (TREE_TYPE (@0), max); })
4229          (le @0 { build_real (TREE_TYPE (@0), max); }))))
4230       /* x != +Inf is always equal to !(x > DBL_MAX), but this introduces
4231          an exception for x a NaN so use an unordered comparison.  */
4232       (if (code == NE_EXPR)
4233        (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
4234         (if (! HONOR_NANS (@0))
4235          (if (neg)
4236           (ge @0 { build_real (TREE_TYPE (@0), max); })
4237           (le @0 { build_real (TREE_TYPE (@0), max); }))
4238          (if (neg)
4239           (unge @0 { build_real (TREE_TYPE (@0), max); })
4240           (unle @0 { build_real (TREE_TYPE (@0), max); }))))))))))
4242  /* If this is a comparison of a real constant with a PLUS_EXPR
4243     or a MINUS_EXPR of a real constant, we can convert it into a
4244     comparison with a revised real constant as long as no overflow
4245     occurs when unsafe_math_optimizations are enabled.  */
4246  (if (flag_unsafe_math_optimizations)
4247   (for op (plus minus)
4248    (simplify
4249     (cmp (op @0 REAL_CST@1) REAL_CST@2)
4250     (with
4251      {
4252        tree tem = const_binop (op == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR,
4253                                TREE_TYPE (@1), @2, @1);
4254      }
4255      (if (tem && !TREE_OVERFLOW (tem))
4256       (cmp @0 { tem; }))))))
4258  /* Likewise, we can simplify a comparison of a real constant with
4259     a MINUS_EXPR whose first operand is also a real constant, i.e.
4260     (c1 - x) < c2 becomes x > c1-c2.  Reordering is allowed on
4261     floating-point types only if -fassociative-math is set.  */
4262  (if (flag_associative_math)
4263   (simplify
4264    (cmp (minus REAL_CST@0 @1) REAL_CST@2)
4265    (with { tree tem = const_binop (MINUS_EXPR, TREE_TYPE (@1), @0, @2); }
4266     (if (tem && !TREE_OVERFLOW (tem))
4267      (cmp { tem; } @1)))))
4269  /* Fold comparisons against built-in math functions.  */
4270  (if (flag_unsafe_math_optimizations && ! flag_errno_math)
4271   (for sq (SQRT)
4272    (simplify
4273     (cmp (sq @0) REAL_CST@1)
4274     (switch
4275      (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
4276       (switch
4277        /* sqrt(x) < y is always false, if y is negative.  */
4278        (if (cmp == EQ_EXPR || cmp == LT_EXPR || cmp == LE_EXPR)
4279         { constant_boolean_node (false, type); })
4280        /* sqrt(x) > y is always true, if y is negative and we
4281           don't care about NaNs, i.e. negative values of x.  */
4282        (if (cmp == NE_EXPR || !HONOR_NANS (@0))
4283         { constant_boolean_node (true, type); })
4284        /* sqrt(x) > y is the same as x >= 0, if y is negative.  */
4285        (ge @0 { build_real (TREE_TYPE (@0), dconst0); })))
4286      (if (real_equal (TREE_REAL_CST_PTR (@1), &dconst0))
4287       (switch
4288        /* sqrt(x) < 0 is always false.  */
4289        (if (cmp == LT_EXPR)
4290         { constant_boolean_node (false, type); })
4291        /* sqrt(x) >= 0 is always true if we don't care about NaNs.  */
4292        (if (cmp == GE_EXPR && !HONOR_NANS (@0))
4293         { constant_boolean_node (true, type); })
4294        /* sqrt(x) <= 0 -> x == 0.  */
4295        (if (cmp == LE_EXPR)
4296         (eq @0 @1))
4297        /* Otherwise sqrt(x) cmp 0 -> x cmp 0.  Here cmp can be >=, >,
4298           == or !=.  In the last case:
4300             (sqrt(x) != 0) == (NaN != 0) == true == (x != 0)
4302           if x is negative or NaN.  Due to -funsafe-math-optimizations,
4303           the results for other x follow from natural arithmetic.  */
4304        (cmp @0 @1)))
4305      (if ((cmp == LT_EXPR
4306            || cmp == LE_EXPR
4307            || cmp == GT_EXPR
4308            || cmp == GE_EXPR)
4309           && !REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
4310           /* Give up for -frounding-math.  */
4311           && !HONOR_SIGN_DEPENDENT_ROUNDING (TREE_TYPE (@0)))
4312       (with
4313        {
4314          REAL_VALUE_TYPE c2;
4315          enum tree_code ncmp = cmp;
4316          const real_format *fmt
4317            = REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (@0)));
4318          real_arithmetic (&c2, MULT_EXPR,
4319                           &TREE_REAL_CST (@1), &TREE_REAL_CST (@1));
4320          real_convert (&c2, fmt, &c2);
4321          /* See PR91734: if c2 is inexact and sqrt(c2) < c (or sqrt(c2) >= c),
4322             then change LT_EXPR into LE_EXPR or GE_EXPR into GT_EXPR.  */
4323          if (!REAL_VALUE_ISINF (c2))
4324            {
4325              tree c3 = fold_const_call (CFN_SQRT, TREE_TYPE (@0),
4326                                         build_real (TREE_TYPE (@0), c2));
4327              if (c3 == NULL_TREE || TREE_CODE (c3) != REAL_CST)
4328                ncmp = ERROR_MARK;
4329              else if ((cmp == LT_EXPR || cmp == GE_EXPR)
4330                       && real_less (&TREE_REAL_CST (c3), &TREE_REAL_CST (@1)))
4331                ncmp = cmp == LT_EXPR ? LE_EXPR : GT_EXPR;
4332              else if ((cmp == LE_EXPR || cmp == GT_EXPR)
4333                       && real_less (&TREE_REAL_CST (@1), &TREE_REAL_CST (c3)))
4334                ncmp = cmp == LE_EXPR ? LT_EXPR : GE_EXPR;
4335              else
4336                {
4337                  /* With rounding to even, sqrt of up to 3 different values
4338                     gives the same normal result, so in some cases c2 needs
4339                     to be adjusted.  */
4340                  REAL_VALUE_TYPE c2alt, tow;
4341                  if (cmp == LT_EXPR || cmp == GE_EXPR)
4342                    tow = dconst0;
4343                  else
4344                    real_inf (&tow);
4345                  real_nextafter (&c2alt, fmt, &c2, &tow);
4346                  real_convert (&c2alt, fmt, &c2alt);
4347                  if (REAL_VALUE_ISINF (c2alt))
4348                    ncmp = ERROR_MARK;
4349                  else
4350                    {
4351                      c3 = fold_const_call (CFN_SQRT, TREE_TYPE (@0),
4352                                            build_real (TREE_TYPE (@0), c2alt));
4353                      if (c3 == NULL_TREE || TREE_CODE (c3) != REAL_CST)
4354                        ncmp = ERROR_MARK;
4355                      else if (real_equal (&TREE_REAL_CST (c3),
4356                                           &TREE_REAL_CST (@1)))
4357                        c2 = c2alt;
4358                    }
4359                }
4360            }
4361        }
4362        (if (cmp == GT_EXPR || cmp == GE_EXPR)
4363         (if (REAL_VALUE_ISINF (c2))
4364          /* sqrt(x) > y is x == +Inf, when y is very large.  */
4365          (if (HONOR_INFINITIES (@0))
4366           (eq @0 { build_real (TREE_TYPE (@0), c2); })
4367           { constant_boolean_node (false, type); })
4368          /* sqrt(x) > c is the same as x > c*c.  */
4369          (if (ncmp != ERROR_MARK)
4370           (if (ncmp == GE_EXPR)
4371            (ge @0 { build_real (TREE_TYPE (@0), c2); })
4372            (gt @0 { build_real (TREE_TYPE (@0), c2); }))))
4373         /* else if (cmp == LT_EXPR || cmp == LE_EXPR)  */
4374         (if (REAL_VALUE_ISINF (c2))
4375          (switch
4376           /* sqrt(x) < y is always true, when y is a very large
4377              value and we don't care about NaNs or Infinities.  */
4378           (if (! HONOR_NANS (@0) && ! HONOR_INFINITIES (@0))
4379            { constant_boolean_node (true, type); })
4380           /* sqrt(x) < y is x != +Inf when y is very large and we
4381              don't care about NaNs.  */
4382           (if (! HONOR_NANS (@0))
4383            (ne @0 { build_real (TREE_TYPE (@0), c2); }))
4384           /* sqrt(x) < y is x >= 0 when y is very large and we
4385              don't care about Infinities.  */
4386           (if (! HONOR_INFINITIES (@0))
4387            (ge @0 { build_real (TREE_TYPE (@0), dconst0); }))
4388           /* sqrt(x) < y is x >= 0 && x != +Inf, when y is large.  */
4389           (if (GENERIC)
4390            (truth_andif
4391             (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
4392             (ne @0 { build_real (TREE_TYPE (@0), c2); }))))
4393          /* sqrt(x) < c is the same as x < c*c, if we ignore NaNs.  */
4394          (if (ncmp != ERROR_MARK && ! HONOR_NANS (@0))
4395           (if (ncmp == LT_EXPR)
4396            (lt @0 { build_real (TREE_TYPE (@0), c2); })
4397            (le @0 { build_real (TREE_TYPE (@0), c2); }))
4398           /* sqrt(x) < c is the same as x >= 0 && x < c*c.  */
4399           (if (ncmp != ERROR_MARK && GENERIC)
4400            (if (ncmp == LT_EXPR)
4401             (truth_andif
4402              (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
4403              (lt @0 { build_real (TREE_TYPE (@0), c2); }))
4404             (truth_andif
4405              (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
4406              (le @0 { build_real (TREE_TYPE (@0), c2); })))))))))))
4407    /* Transform sqrt(x) cmp sqrt(y) -> x cmp y.  */
4408    (simplify
4409     (cmp (sq @0) (sq @1))
4410       (if (! HONOR_NANS (@0))
4411         (cmp @0 @1))))))
4413 /* Optimize various special cases of (FTYPE) N CMP (FTYPE) M.  */
4414 (for cmp  (lt le eq ne ge gt unordered ordered unlt unle ungt unge uneq ltgt)
4415      icmp (lt le eq ne ge gt unordered ordered lt   le   gt   ge   eq   ne)
4416  (simplify
4417   (cmp (float@0 @1) (float @2))
4418    (if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (@0))
4419         && ! DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0)))
4420     (with
4421      {
4422        format_helper fmt (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (@0))));
4423        tree type1 = TREE_TYPE (@1);
4424        bool type1_signed_p = TYPE_SIGN (type1) == SIGNED;
4425        tree type2 = TREE_TYPE (@2);
4426        bool type2_signed_p = TYPE_SIGN (type2) == SIGNED;
4427      }
4428      (if (fmt.can_represent_integral_type_p (type1)
4429           && fmt.can_represent_integral_type_p (type2))
4430       (if (cmp == ORDERED_EXPR || cmp == UNORDERED_EXPR)
4431        { constant_boolean_node (cmp == ORDERED_EXPR, type); }
4432        (if (TYPE_PRECISION (type1) > TYPE_PRECISION (type2)
4433             && type1_signed_p >= type2_signed_p)
4434         (icmp @1 (convert @2))
4435         (if (TYPE_PRECISION (type1) < TYPE_PRECISION (type2)
4436              && type1_signed_p <= type2_signed_p)
4437          (icmp (convert:type2 @1) @2)
4438          (if (TYPE_PRECISION (type1) == TYPE_PRECISION (type2)
4439               && type1_signed_p == type2_signed_p)
4440           (icmp @1 @2))))))))))
4442 /* Optimize various special cases of (FTYPE) N CMP CST.  */
4443 (for cmp  (lt le eq ne ge gt)
4444      icmp (le le eq ne ge ge)
4445  (simplify
4446   (cmp (float @0) REAL_CST@1)
4447    (if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (@1))
4448         && ! DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1)))
4449     (with
4450      {
4451        tree itype = TREE_TYPE (@0);
4452        format_helper fmt (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (@1))));
4453        const REAL_VALUE_TYPE *cst = TREE_REAL_CST_PTR (@1);
4454        /* Be careful to preserve any potential exceptions due to
4455           NaNs.  qNaNs are ok in == or != context.
4456           TODO: relax under -fno-trapping-math or
4457           -fno-signaling-nans.  */
4458        bool exception_p
4459          = real_isnan (cst) && (cst->signalling
4460                                 || (cmp != EQ_EXPR && cmp != NE_EXPR));
4461      }
4462      /* TODO: allow non-fitting itype and SNaNs when
4463         -fno-trapping-math.  */
4464      (if (fmt.can_represent_integral_type_p (itype) && ! exception_p)
4465       (with
4466        {
4467          signop isign = TYPE_SIGN (itype);
4468          REAL_VALUE_TYPE imin, imax;
4469          real_from_integer (&imin, fmt, wi::min_value (itype), isign);
4470          real_from_integer (&imax, fmt, wi::max_value (itype), isign);
4472          REAL_VALUE_TYPE icst;
4473          if (cmp == GT_EXPR || cmp == GE_EXPR)
4474            real_ceil (&icst, fmt, cst);
4475          else if (cmp == LT_EXPR || cmp == LE_EXPR)
4476            real_floor (&icst, fmt, cst);
4477          else
4478            real_trunc (&icst, fmt, cst);
4480          bool cst_int_p = !real_isnan (cst) && real_identical (&icst, cst);
4482          bool overflow_p = false;
4483          wide_int icst_val
4484            = real_to_integer (&icst, &overflow_p, TYPE_PRECISION (itype));
4485        }
4486        (switch
4487         /* Optimize cases when CST is outside of ITYPE's range.  */
4488         (if (real_compare (LT_EXPR, cst, &imin))
4489          { constant_boolean_node (cmp == GT_EXPR || cmp == GE_EXPR || cmp == NE_EXPR,
4490                                   type); })
4491         (if (real_compare (GT_EXPR, cst, &imax))
4492          { constant_boolean_node (cmp == LT_EXPR || cmp == LE_EXPR || cmp == NE_EXPR,
4493                                   type); })
4494         /* Remove cast if CST is an integer representable by ITYPE.  */
4495         (if (cst_int_p)
4496          (cmp @0 { gcc_assert (!overflow_p);
4497                    wide_int_to_tree (itype, icst_val); })
4498         )
4499         /* When CST is fractional, optimize
4500             (FTYPE) N == CST -> 0
4501             (FTYPE) N != CST -> 1.  */
4502         (if (cmp == EQ_EXPR || cmp == NE_EXPR)
4503          { constant_boolean_node (cmp == NE_EXPR, type); })
4504         /* Otherwise replace with sensible integer constant.  */
4505         (with
4506          {
4507            gcc_checking_assert (!overflow_p);
4508          }
4509          (icmp @0 { wide_int_to_tree (itype, icst_val); })))))))))
4511 /* Fold A /[ex] B CMP C to A CMP B * C.  */
4512 (for cmp (eq ne)
4513  (simplify
4514   (cmp (exact_div @0 @1) INTEGER_CST@2)
4515   (if (!integer_zerop (@1))
4516    (if (wi::to_wide (@2) == 0)
4517     (cmp @0 @2)
4518     (if (TREE_CODE (@1) == INTEGER_CST)
4519      (with
4520       {
4521         wi::overflow_type ovf;
4522         wide_int prod = wi::mul (wi::to_wide (@2), wi::to_wide (@1),
4523                                  TYPE_SIGN (TREE_TYPE (@1)), &ovf);
4524       }
4525       (if (ovf)
4526        { constant_boolean_node (cmp == NE_EXPR, type); }
4527        (cmp @0 { wide_int_to_tree (TREE_TYPE (@0), prod); }))))))))
4528 (for cmp (lt le gt ge)
4529  (simplify
4530   (cmp (exact_div @0 INTEGER_CST@1) INTEGER_CST@2)
4531   (if (wi::gt_p (wi::to_wide (@1), 0, TYPE_SIGN (TREE_TYPE (@1))))
4532    (with
4533     {
4534       wi::overflow_type ovf;
4535       wide_int prod = wi::mul (wi::to_wide (@2), wi::to_wide (@1),
4536                                TYPE_SIGN (TREE_TYPE (@1)), &ovf);
4537     }
4538     (if (ovf)
4539      { constant_boolean_node (wi::lt_p (wi::to_wide (@2), 0,
4540                                         TYPE_SIGN (TREE_TYPE (@2)))
4541                               != (cmp == LT_EXPR || cmp == LE_EXPR), type); }
4542      (cmp @0 { wide_int_to_tree (TREE_TYPE (@0), prod); }))))))
4544 /* Fold (size_t)(A /[ex] B) CMP C to (size_t)A CMP (size_t)B * C or A CMP' 0.
4546    For small C (less than max/B), this is (size_t)A CMP (size_t)B * C.
4547    For large C (more than min/B+2^size), this is also true, with the
4548    multiplication computed modulo 2^size.
4549    For intermediate C, this just tests the sign of A.  */
4550 (for cmp  (lt le gt ge)
4551      cmp2 (ge ge lt lt)
4552  (simplify
4553   (cmp (convert (exact_div @0 INTEGER_CST@1)) INTEGER_CST@2)
4554   (if (tree_nop_conversion_p (TREE_TYPE (@0), TREE_TYPE (@2))
4555        && TYPE_UNSIGNED (TREE_TYPE (@2)) && !TYPE_UNSIGNED (TREE_TYPE (@0))
4556        && wi::gt_p (wi::to_wide (@1), 0, TYPE_SIGN (TREE_TYPE (@1))))
4557    (with
4558     {
4559       tree utype = TREE_TYPE (@2);
4560       wide_int denom = wi::to_wide (@1);
4561       wide_int right = wi::to_wide (@2);
4562       wide_int smax = wi::sdiv_trunc (wi::max_value (TREE_TYPE (@0)), denom);
4563       wide_int smin = wi::sdiv_trunc (wi::min_value (TREE_TYPE (@0)), denom);
4564       bool small = wi::leu_p (right, smax);
4565       bool large = wi::geu_p (right, smin);
4566     }
4567     (if (small || large)
4568      (cmp (convert:utype @0) (mult @2 (convert @1)))
4569      (cmp2 @0 { build_zero_cst (TREE_TYPE (@0)); }))))))
4571 /* Unordered tests if either argument is a NaN.  */
4572 (simplify
4573  (bit_ior (unordered @0 @0) (unordered @1 @1))
4574  (if (types_match (@0, @1))
4575   (unordered @0 @1)))
4576 (simplify
4577  (bit_and (ordered @0 @0) (ordered @1 @1))
4578  (if (types_match (@0, @1))
4579   (ordered @0 @1)))
4580 (simplify
4581  (bit_ior:c (unordered @0 @0) (unordered:c@2 @0 @1))
4582  @2)
4583 (simplify
4584  (bit_and:c (ordered @0 @0) (ordered:c@2 @0 @1))
4585  @2)
4587 /* Simple range test simplifications.  */
4588 /* A < B || A >= B -> true.  */
4589 (for test1 (lt le le le ne ge)
4590      test2 (ge gt ge ne eq ne)
4591  (simplify
4592   (bit_ior:c (test1 @0 @1) (test2 @0 @1))
4593   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
4594        || VECTOR_INTEGER_TYPE_P (TREE_TYPE (@0)))
4595    { constant_boolean_node (true, type); })))
4596 /* A < B && A >= B -> false.  */
4597 (for test1 (lt lt lt le ne eq)
4598      test2 (ge gt eq gt eq gt)
4599  (simplify
4600   (bit_and:c (test1 @0 @1) (test2 @0 @1))
4601   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
4602        || VECTOR_INTEGER_TYPE_P (TREE_TYPE (@0)))
4603    { constant_boolean_node (false, type); })))
4605 /* A & (2**N - 1) <= 2**K - 1 -> A & (2**N - 2**K) == 0
4606    A & (2**N - 1) >  2**K - 1 -> A & (2**N - 2**K) != 0
4608    Note that comparisons
4609      A & (2**N - 1) <  2**K   -> A & (2**N - 2**K) == 0
4610      A & (2**N - 1) >= 2**K   -> A & (2**N - 2**K) != 0
4611    will be canonicalized to above so there's no need to
4612    consider them here.
4613  */
4615 (for cmp (le gt)
4616      eqcmp (eq ne)
4617  (simplify
4618   (cmp (bit_and@0 @1 INTEGER_CST@2) INTEGER_CST@3)
4619   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)))
4620    (with
4621     {
4622      tree ty = TREE_TYPE (@0);
4623      unsigned prec = TYPE_PRECISION (ty);
4624      wide_int mask = wi::to_wide (@2, prec);
4625      wide_int rhs = wi::to_wide (@3, prec);
4626      signop sgn = TYPE_SIGN (ty);
4627     }
4628     (if ((mask & (mask + 1)) == 0 && wi::gt_p (rhs, 0, sgn)
4629          && (rhs & (rhs + 1)) == 0 && wi::ge_p (mask, rhs, sgn))
4630       (eqcmp (bit_and @1 { wide_int_to_tree (ty, mask - rhs); })
4631              { build_zero_cst (ty); }))))))
4633 /* -A CMP -B -> B CMP A.  */
4634 (for cmp (tcc_comparison)
4635      scmp (swapped_tcc_comparison)
4636  (simplify
4637   (cmp (negate @0) (negate @1))
4638   (if (FLOAT_TYPE_P (TREE_TYPE (@0))
4639        || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
4640            && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
4641    (scmp @0 @1)))
4642  (simplify
4643   (cmp (negate @0) CONSTANT_CLASS_P@1)
4644   (if (FLOAT_TYPE_P (TREE_TYPE (@0))
4645        || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
4646            && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
4647    (with { tree tem = const_unop (NEGATE_EXPR, TREE_TYPE (@0), @1); }
4648     (if (tem && !TREE_OVERFLOW (tem))
4649      (scmp @0 { tem; }))))))
4651 /* Convert ABS_EXPR<x> == 0 or ABS_EXPR<x> != 0 to x == 0 or x != 0.  */
4652 (for op (eq ne)
4653  (simplify
4654   (op (abs @0) zerop@1)
4655   (op @0 @1)))
4657 /* From fold_sign_changed_comparison and fold_widened_comparison.
4658    FIXME: the lack of symmetry is disturbing.  */
4659 (for cmp (simple_comparison)
4660  (simplify
4661   (cmp (convert@0 @00) (convert?@1 @10))
4662   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
4663        /* Disable this optimization if we're casting a function pointer
4664           type on targets that require function pointer canonicalization.  */
4665        && !(targetm.have_canonicalize_funcptr_for_compare ()
4666             && ((POINTER_TYPE_P (TREE_TYPE (@00))
4667                  && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@00))))
4668                 || (POINTER_TYPE_P (TREE_TYPE (@10))
4669                     && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@10))))))
4670        && single_use (@0))
4671    (if (TYPE_PRECISION (TREE_TYPE (@00)) == TYPE_PRECISION (TREE_TYPE (@0))
4672         && (TREE_CODE (@10) == INTEGER_CST
4673             || @1 != @10)
4674         && (TYPE_UNSIGNED (TREE_TYPE (@00)) == TYPE_UNSIGNED (TREE_TYPE (@0))
4675             || cmp == NE_EXPR
4676             || cmp == EQ_EXPR)
4677         && !POINTER_TYPE_P (TREE_TYPE (@00)))
4678     /* ???  The special-casing of INTEGER_CST conversion was in the original
4679        code and here to avoid a spurious overflow flag on the resulting
4680        constant which fold_convert produces.  */
4681     (if (TREE_CODE (@1) == INTEGER_CST)
4682      (cmp @00 { force_fit_type (TREE_TYPE (@00), wi::to_widest (@1), 0,
4683                                 TREE_OVERFLOW (@1)); })
4684      (cmp @00 (convert @1)))
4686     (if (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (TREE_TYPE (@00)))
4687      /* If possible, express the comparison in the shorter mode.  */
4688      (if ((cmp == EQ_EXPR || cmp == NE_EXPR
4689            || TYPE_UNSIGNED (TREE_TYPE (@0)) == TYPE_UNSIGNED (TREE_TYPE (@00))
4690            || (!TYPE_UNSIGNED (TREE_TYPE (@0))
4691                && TYPE_UNSIGNED (TREE_TYPE (@00))))
4692           && (types_match (TREE_TYPE (@10), TREE_TYPE (@00))
4693               || ((TYPE_PRECISION (TREE_TYPE (@00))
4694                    >= TYPE_PRECISION (TREE_TYPE (@10)))
4695                   && (TYPE_UNSIGNED (TREE_TYPE (@00))
4696                       == TYPE_UNSIGNED (TREE_TYPE (@10))))
4697               || (TREE_CODE (@10) == INTEGER_CST
4698                   && INTEGRAL_TYPE_P (TREE_TYPE (@00))
4699                   && int_fits_type_p (@10, TREE_TYPE (@00)))))
4700       (cmp @00 (convert @10))
4701       (if (TREE_CODE (@10) == INTEGER_CST
4702            && INTEGRAL_TYPE_P (TREE_TYPE (@00))
4703            && !int_fits_type_p (@10, TREE_TYPE (@00)))
4704        (with
4705         {
4706           tree min = lower_bound_in_type (TREE_TYPE (@10), TREE_TYPE (@00));
4707           tree max = upper_bound_in_type (TREE_TYPE (@10), TREE_TYPE (@00));
4708           bool above = integer_nonzerop (const_binop (LT_EXPR, type, max, @10));
4709           bool below = integer_nonzerop (const_binop (LT_EXPR, type, @10, min));
4710         }
4711         (if (above || below)
4712          (if (cmp == EQ_EXPR || cmp == NE_EXPR)
4713           { constant_boolean_node (cmp == EQ_EXPR ? false : true, type); }
4714           (if (cmp == LT_EXPR || cmp == LE_EXPR)
4715            { constant_boolean_node (above ? true : false, type); }
4716            (if (cmp == GT_EXPR || cmp == GE_EXPR)
4717             { constant_boolean_node (above ? false : true, type); }))))))))))))
4719 (for cmp (eq ne)
4720  (simplify
4721   /* SSA names are canonicalized to 2nd place.  */
4722   (cmp addr@0 SSA_NAME@1)
4723   (with
4724    { poly_int64 off; tree base; }
4725    /* A local variable can never be pointed to by
4726       the default SSA name of an incoming parameter.  */
4727    (if (SSA_NAME_IS_DEFAULT_DEF (@1)
4728         && TREE_CODE (SSA_NAME_VAR (@1)) == PARM_DECL
4729         && (base = get_base_address (TREE_OPERAND (@0, 0)))
4730         && TREE_CODE (base) == VAR_DECL
4731         && auto_var_in_fn_p (base, current_function_decl))
4732     (if (cmp == NE_EXPR)
4733      { constant_boolean_node (true, type); }
4734      { constant_boolean_node (false, type); })
4735     /* If the address is based on @1 decide using the offset.  */
4736     (if ((base = get_addr_base_and_unit_offset (TREE_OPERAND (@0, 0), &off))
4737          && TREE_CODE (base) == MEM_REF
4738          && TREE_OPERAND (base, 0) == @1)
4739      (with { off += mem_ref_offset (base).force_shwi (); }
4740       (if (known_ne (off, 0))
4741        { constant_boolean_node (cmp == NE_EXPR, type); }
4742        (if (known_eq (off, 0))
4743         { constant_boolean_node (cmp == EQ_EXPR, type); }))))))))
4745 /* Equality compare simplifications from fold_binary  */
4746 (for cmp (eq ne)
4748  /* If we have (A | C) == D where C & ~D != 0, convert this into 0.
4749     Similarly for NE_EXPR.  */
4750  (simplify
4751   (cmp (convert?@3 (bit_ior @0 INTEGER_CST@1)) INTEGER_CST@2)
4752   (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0))
4753        && wi::bit_and_not (wi::to_wide (@1), wi::to_wide (@2)) != 0)
4754    { constant_boolean_node (cmp == NE_EXPR, type); }))
4756  /* (X ^ Y) == 0 becomes X == Y, and (X ^ Y) != 0 becomes X != Y.  */
4757  (simplify
4758   (cmp (bit_xor @0 @1) integer_zerop)
4759   (cmp @0 @1))
4761  /* (X ^ Y) == Y becomes X == 0.
4762     Likewise (X ^ Y) == X becomes Y == 0.  */
4763  (simplify
4764   (cmp:c (bit_xor:c @0 @1) @0)
4765   (cmp @1 { build_zero_cst (TREE_TYPE (@1)); }))
4767  /* (X ^ C1) op C2 can be rewritten as X op (C1 ^ C2).  */
4768  (simplify
4769   (cmp (convert?@3 (bit_xor @0 INTEGER_CST@1)) INTEGER_CST@2)
4770   (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0)))
4771    (cmp @0 (bit_xor @1 (convert @2)))))
4773  (simplify
4774   (cmp (convert? addr@0) integer_zerop)
4775   (if (tree_single_nonzero_warnv_p (@0, NULL))
4776    { constant_boolean_node (cmp == NE_EXPR, type); }))
4778  /* (X & C) op (Y & C) into (X ^ Y) & C op 0.  */
4779  (simplify
4780   (cmp (bit_and:cs @0 @2) (bit_and:cs @1 @2))
4781   (cmp (bit_and (bit_xor @0 @1) @2) { build_zero_cst (TREE_TYPE (@2)); })))
4783 /* (X < 0) != (Y < 0) into (X ^ Y) < 0.
4784    (X >= 0) != (Y >= 0) into (X ^ Y) < 0.
4785    (X < 0) == (Y < 0) into (X ^ Y) >= 0.
4786    (X >= 0) == (Y >= 0) into (X ^ Y) >= 0.  */
4787 (for cmp (eq ne)
4788      ncmp (ge lt)
4789  (for sgncmp (ge lt)
4790   (simplify
4791    (cmp (sgncmp @0 integer_zerop@2) (sgncmp @1 integer_zerop))
4792    (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
4793         && !TYPE_UNSIGNED (TREE_TYPE (@0))
4794         && types_match (@0, @1))
4795     (ncmp (bit_xor @0 @1) @2)))))
4796 /* (X < 0) == (Y >= 0) into (X ^ Y) < 0.
4797    (X < 0) != (Y >= 0) into (X ^ Y) >= 0.  */
4798 (for cmp (eq ne)
4799      ncmp (lt ge)
4800  (simplify
4801   (cmp:c (lt @0 integer_zerop@2) (ge @1 integer_zerop))
4802    (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
4803         && !TYPE_UNSIGNED (TREE_TYPE (@0))
4804         && types_match (@0, @1))
4805     (ncmp (bit_xor @0 @1) @2))))
4807 /* If we have (A & C) == C where C is a power of 2, convert this into
4808    (A & C) != 0.  Similarly for NE_EXPR.  */
4809 (for cmp (eq ne)
4810      icmp (ne eq)
4811  (simplify
4812   (cmp (bit_and@2 @0 integer_pow2p@1) @1)
4813   (icmp @2 { build_zero_cst (TREE_TYPE (@0)); })))
4815 /* If we have (A & C) != 0 ? D : 0 where C and D are powers of 2,
4816    convert this into a shift followed by ANDing with D.  */
4817 (simplify
4818  (cond
4819   (ne (bit_and @0 integer_pow2p@1) integer_zerop)
4820   INTEGER_CST@2 integer_zerop)
4821  (if (integer_pow2p (@2))
4822   (with {
4823      int shift = (wi::exact_log2 (wi::to_wide (@2))
4824                   - wi::exact_log2 (wi::to_wide (@1)));
4825    }
4826    (if (shift > 0)
4827     (bit_and
4828      (lshift (convert @0) { build_int_cst (integer_type_node, shift); }) @2)
4829     (bit_and
4830      (convert (rshift @0 { build_int_cst (integer_type_node, -shift); }))
4831      @2)))))
4833 /* If we have (A & C) != 0 where C is the sign bit of A, convert
4834    this into A < 0.  Similarly for (A & C) == 0 into A >= 0.  */
4835 (for cmp (eq ne)
4836      ncmp (ge lt)
4837  (simplify
4838   (cmp (bit_and (convert?@2 @0) integer_pow2p@1) integer_zerop)
4839   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
4840        && type_has_mode_precision_p (TREE_TYPE (@0))
4841        && element_precision (@2) >= element_precision (@0)
4842        && wi::only_sign_bit_p (wi::to_wide (@1), element_precision (@0)))
4843    (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
4844     (ncmp (convert:stype @0) { build_zero_cst (stype); })))))
4846 /* If we have A < 0 ? C : 0 where C is a power of 2, convert
4847    this into a right shift or sign extension followed by ANDing with C.  */
4848 (simplify
4849  (cond
4850   (lt @0 integer_zerop)
4851   INTEGER_CST@1 integer_zerop)
4852  (if (integer_pow2p (@1)
4853       && !TYPE_UNSIGNED (TREE_TYPE (@0)))
4854   (with {
4855     int shift = element_precision (@0) - wi::exact_log2 (wi::to_wide (@1)) - 1;
4856    }
4857    (if (shift >= 0)
4858     (bit_and
4859      (convert (rshift @0 { build_int_cst (integer_type_node, shift); }))
4860      @1)
4861     /* Otherwise ctype must be wider than TREE_TYPE (@0) and pure
4862        sign extension followed by AND with C will achieve the effect.  */
4863     (bit_and (convert @0) @1)))))
4865 /* When the addresses are not directly of decls compare base and offset.
4866    This implements some remaining parts of fold_comparison address
4867    comparisons but still no complete part of it.  Still it is good
4868    enough to make fold_stmt not regress when not dispatching to fold_binary.  */
4869 (for cmp (simple_comparison)
4870  (simplify
4871   (cmp (convert1?@2 addr@0) (convert2? addr@1))
4872   (with
4873    {
4874      poly_int64 off0, off1;
4875      tree base0 = get_addr_base_and_unit_offset (TREE_OPERAND (@0, 0), &off0);
4876      tree base1 = get_addr_base_and_unit_offset (TREE_OPERAND (@1, 0), &off1);
4877      if (base0 && TREE_CODE (base0) == MEM_REF)
4878        {
4879          off0 += mem_ref_offset (base0).force_shwi ();
4880          base0 = TREE_OPERAND (base0, 0);
4881        }
4882      if (base1 && TREE_CODE (base1) == MEM_REF)
4883        {
4884          off1 += mem_ref_offset (base1).force_shwi ();
4885          base1 = TREE_OPERAND (base1, 0);
4886        }
4887    }
4888    (if (base0 && base1)
4889     (with
4890      {
4891        int equal = 2;
4892        /* Punt in GENERIC on variables with value expressions;
4893           the value expressions might point to fields/elements
4894           of other vars etc.  */
4895        if (GENERIC
4896            && ((VAR_P (base0) && DECL_HAS_VALUE_EXPR_P (base0))
4897                || (VAR_P (base1) && DECL_HAS_VALUE_EXPR_P (base1))))
4898          ;
4899        else if (decl_in_symtab_p (base0)
4900                 && decl_in_symtab_p (base1))
4901          equal = symtab_node::get_create (base0)
4902                    ->equal_address_to (symtab_node::get_create (base1));
4903        else if ((DECL_P (base0)
4904                  || TREE_CODE (base0) == SSA_NAME
4905                  || TREE_CODE (base0) == STRING_CST)
4906                 && (DECL_P (base1)
4907                     || TREE_CODE (base1) == SSA_NAME
4908                     || TREE_CODE (base1) == STRING_CST))
4909          equal = (base0 == base1);
4910        if (equal == 0)
4911          {
4912            HOST_WIDE_INT ioff0 = -1, ioff1 = -1;
4913            off0.is_constant (&ioff0);
4914            off1.is_constant (&ioff1);
4915            if ((DECL_P (base0) && TREE_CODE (base1) == STRING_CST)
4916                || (TREE_CODE (base0) == STRING_CST && DECL_P (base1))
4917                || (TREE_CODE (base0) == STRING_CST
4918                    && TREE_CODE (base1) == STRING_CST
4919                    && ioff0 >= 0 && ioff1 >= 0
4920                    && ioff0 < TREE_STRING_LENGTH (base0)
4921                    && ioff1 < TREE_STRING_LENGTH (base1)
4922                    /* This is a too conservative test that the STRING_CSTs
4923                       will not end up being string-merged.  */
4924                    && strncmp (TREE_STRING_POINTER (base0) + ioff0,
4925                                TREE_STRING_POINTER (base1) + ioff1,
4926                                MIN (TREE_STRING_LENGTH (base0) - ioff0,
4927                                     TREE_STRING_LENGTH (base1) - ioff1)) != 0))
4928              ;
4929            else if (!DECL_P (base0) || !DECL_P (base1))
4930              equal = 2;
4931            else if (cmp != EQ_EXPR && cmp != NE_EXPR)
4932              equal = 2;
4933            /* If this is a pointer comparison, ignore for now even
4934               valid equalities where one pointer is the offset zero
4935               of one object and the other to one past end of another one.  */
4936            else if (!INTEGRAL_TYPE_P (TREE_TYPE (@2)))
4937              ;
4938            /* Assume that automatic variables can't be adjacent to global
4939               variables.  */
4940            else if (is_global_var (base0) != is_global_var (base1))
4941              ;
4942            else
4943              {
4944                tree sz0 = DECL_SIZE_UNIT (base0);
4945                tree sz1 = DECL_SIZE_UNIT (base1);
4946                /* If sizes are unknown, e.g. VLA or not representable,
4947                   punt.  */
4948                if (!tree_fits_poly_int64_p (sz0)
4949                    || !tree_fits_poly_int64_p (sz1))
4950                  equal = 2;
4951                else
4952                  {
4953                    poly_int64 size0 = tree_to_poly_int64 (sz0);
4954                    poly_int64 size1 = tree_to_poly_int64 (sz1);
4955                    /* If one offset is pointing (or could be) to the beginning
4956                       of one object and the other is pointing to one past the
4957                       last byte of the other object, punt.  */
4958                    if (maybe_eq (off0, 0) && maybe_eq (off1, size1))
4959                      equal = 2;
4960                    else if (maybe_eq (off1, 0) && maybe_eq (off0, size0))
4961                      equal = 2;
4962                    /* If both offsets are the same, there are some cases
4963                       we know that are ok.  Either if we know they aren't
4964                       zero, or if we know both sizes are no zero.  */
4965                    if (equal == 2
4966                        && known_eq (off0, off1)
4967                        && (known_ne (off0, 0)
4968                            || (known_ne (size0, 0) && known_ne (size1, 0))))
4969                      equal = 0;
4970                  }
4971              }
4972          }
4973      }
4974      (if (equal == 1
4975           && (cmp == EQ_EXPR || cmp == NE_EXPR
4976               /* If the offsets are equal we can ignore overflow.  */
4977               || known_eq (off0, off1)
4978               || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
4979                  /* Or if we compare using pointers to decls or strings.  */
4980               || (POINTER_TYPE_P (TREE_TYPE (@2))
4981                   && (DECL_P (base0) || TREE_CODE (base0) == STRING_CST))))
4982       (switch
4983        (if (cmp == EQ_EXPR && (known_eq (off0, off1) || known_ne (off0, off1)))
4984         { constant_boolean_node (known_eq (off0, off1), type); })
4985        (if (cmp == NE_EXPR && (known_eq (off0, off1) || known_ne (off0, off1)))
4986         { constant_boolean_node (known_ne (off0, off1), type); })
4987        (if (cmp == LT_EXPR && (known_lt (off0, off1) || known_ge (off0, off1)))
4988         { constant_boolean_node (known_lt (off0, off1), type); })
4989        (if (cmp == LE_EXPR && (known_le (off0, off1) || known_gt (off0, off1)))
4990         { constant_boolean_node (known_le (off0, off1), type); })
4991        (if (cmp == GE_EXPR && (known_ge (off0, off1) || known_lt (off0, off1)))
4992         { constant_boolean_node (known_ge (off0, off1), type); })
4993        (if (cmp == GT_EXPR && (known_gt (off0, off1) || known_le (off0, off1)))
4994         { constant_boolean_node (known_gt (off0, off1), type); }))
4995       (if (equal == 0)
4996         (switch
4997          (if (cmp == EQ_EXPR)
4998           { constant_boolean_node (false, type); })
4999          (if (cmp == NE_EXPR)
5000           { constant_boolean_node (true, type); })))))))))
5002 /* Simplify pointer equality compares using PTA.  */
5003 (for neeq (ne eq)
5004  (simplify
5005   (neeq @0 @1)
5006   (if (POINTER_TYPE_P (TREE_TYPE (@0))
5007        && ptrs_compare_unequal (@0, @1))
5008    { constant_boolean_node (neeq != EQ_EXPR, type); })))
5010 /* PR70920: Transform (intptr_t)x eq/ne CST to x eq/ne (typeof x) CST.
5011    and (typeof ptr_cst) x eq/ne ptr_cst to x eq/ne (typeof x) CST.
5012    Disable the transform if either operand is pointer to function.
5013    This broke pr22051-2.c for arm where function pointer
5014    canonicalizaion is not wanted.  */
5016 (for cmp (ne eq)
5017  (simplify
5018   (cmp (convert @0) INTEGER_CST@1)
5019   (if (((POINTER_TYPE_P (TREE_TYPE (@0))
5020          && !FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@0)))
5021          && INTEGRAL_TYPE_P (TREE_TYPE (@1)))
5022         || (INTEGRAL_TYPE_P (TREE_TYPE (@0))
5023             && POINTER_TYPE_P (TREE_TYPE (@1))
5024             && !FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@1)))))
5025        && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
5026    (cmp @0 (convert @1)))))
5028 /* Non-equality compare simplifications from fold_binary  */
5029 (for cmp (lt gt le ge)
5030  /* Comparisons with the highest or lowest possible integer of
5031     the specified precision will have known values.  */
5032  (simplify
5033   (cmp (convert?@2 @0) uniform_integer_cst_p@1)
5034   (if ((INTEGRAL_TYPE_P (TREE_TYPE (@1))
5035         || POINTER_TYPE_P (TREE_TYPE (@1))
5036         || VECTOR_INTEGER_TYPE_P (TREE_TYPE (@1)))
5037        && tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@0)))
5038    (with
5039     {
5040       tree cst = uniform_integer_cst_p (@1);
5041       tree arg1_type = TREE_TYPE (cst);
5042       unsigned int prec = TYPE_PRECISION (arg1_type);
5043       wide_int max = wi::max_value (arg1_type);
5044       wide_int signed_max = wi::max_value (prec, SIGNED);
5045       wide_int min = wi::min_value (arg1_type);
5046     }
5047     (switch
5048      (if (wi::to_wide (cst) == max)
5049       (switch
5050        (if (cmp == GT_EXPR)
5051         { constant_boolean_node (false, type); })
5052        (if (cmp == GE_EXPR)
5053         (eq @2 @1))
5054        (if (cmp == LE_EXPR)
5055         { constant_boolean_node (true, type); })
5056        (if (cmp == LT_EXPR)
5057         (ne @2 @1))))
5058      (if (wi::to_wide (cst) == min)
5059       (switch
5060        (if (cmp == LT_EXPR)
5061         { constant_boolean_node (false, type); })
5062        (if (cmp == LE_EXPR)
5063         (eq @2 @1))
5064        (if (cmp == GE_EXPR)
5065         { constant_boolean_node (true, type); })
5066        (if (cmp == GT_EXPR)
5067         (ne @2 @1))))
5068      (if (wi::to_wide (cst) == max - 1)
5069       (switch
5070        (if (cmp == GT_EXPR)
5071         (eq @2 { build_uniform_cst (TREE_TYPE (@1),
5072                                     wide_int_to_tree (TREE_TYPE (cst),
5073                                                       wi::to_wide (cst)
5074                                                       + 1)); }))
5075        (if (cmp == LE_EXPR)
5076         (ne @2 { build_uniform_cst (TREE_TYPE (@1),
5077                                     wide_int_to_tree (TREE_TYPE (cst),
5078                                                       wi::to_wide (cst)
5079                                                       + 1)); }))))
5080      (if (wi::to_wide (cst) == min + 1)
5081       (switch
5082        (if (cmp == GE_EXPR)
5083         (ne @2 { build_uniform_cst (TREE_TYPE (@1),
5084                                     wide_int_to_tree (TREE_TYPE (cst),
5085                                                       wi::to_wide (cst)
5086                                                       - 1)); }))
5087        (if (cmp == LT_EXPR)
5088         (eq @2 { build_uniform_cst (TREE_TYPE (@1),
5089                                     wide_int_to_tree (TREE_TYPE (cst),
5090                                                       wi::to_wide (cst)
5091                                                       - 1)); }))))
5092      (if (wi::to_wide (cst) == signed_max
5093           && TYPE_UNSIGNED (arg1_type)
5094           /* We will flip the signedness of the comparison operator
5095              associated with the mode of @1, so the sign bit is
5096              specified by this mode.  Check that @1 is the signed
5097              max associated with this sign bit.  */
5098           && prec == GET_MODE_PRECISION (SCALAR_INT_TYPE_MODE (arg1_type))
5099           /* signed_type does not work on pointer types.  */
5100           && INTEGRAL_TYPE_P (arg1_type))
5101       /* The following case also applies to X < signed_max+1
5102          and X >= signed_max+1 because previous transformations.  */
5103       (if (cmp == LE_EXPR || cmp == GT_EXPR)
5104        (with { tree st = signed_type_for (TREE_TYPE (@1)); }
5105         (switch
5106          (if (cst == @1 && cmp == LE_EXPR)
5107           (ge (convert:st @0) { build_zero_cst (st); }))
5108          (if (cst == @1 && cmp == GT_EXPR)
5109           (lt (convert:st @0) { build_zero_cst (st); }))
5110          (if (cmp == LE_EXPR)
5111           (ge (view_convert:st @0) { build_zero_cst (st); }))
5112          (if (cmp == GT_EXPR)
5113           (lt (view_convert:st @0) { build_zero_cst (st); })))))))))))
5115 (for cmp (unordered ordered unlt unle ungt unge uneq ltgt)
5116  /* If the second operand is NaN, the result is constant.  */
5117  (simplify
5118   (cmp @0 REAL_CST@1)
5119   (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
5120        && (cmp != LTGT_EXPR || ! flag_trapping_math))
5121    { constant_boolean_node (cmp == ORDERED_EXPR || cmp == LTGT_EXPR
5122                             ? false : true, type); })))
5124 /* Fold UNORDERED if either operand must be NaN, or neither can be.  */
5125 (simplify
5126   (unordered @0 @1)
5127   (switch
5128     (if (tree_expr_nan_p (@0) || tree_expr_nan_p (@1))
5129         { constant_boolean_node (true, type); })
5130     (if (!tree_expr_maybe_nan_p (@0) && !tree_expr_maybe_nan_p (@1))
5131         { constant_boolean_node (false, type); })))
5133 /* Fold ORDERED if either operand must be NaN, or neither can be.  */
5134 (simplify
5135   (ordered @0 @1)
5136   (switch
5137     (if (tree_expr_nan_p (@0) || tree_expr_nan_p (@1))
5138         { constant_boolean_node (false, type); })
5139     (if (!tree_expr_maybe_nan_p (@0) && !tree_expr_maybe_nan_p (@1))
5140         { constant_boolean_node (true, type); })))
5142 /* bool_var != 0 becomes bool_var.  */
5143 (simplify
5144  (ne @0 integer_zerop)
5145  (if (TREE_CODE (TREE_TYPE (@0)) == BOOLEAN_TYPE
5146       && types_match (type, TREE_TYPE (@0)))
5147   (non_lvalue @0)))
5148 /* bool_var == 1 becomes bool_var.  */
5149 (simplify
5150  (eq @0 integer_onep)
5151  (if (TREE_CODE (TREE_TYPE (@0)) == BOOLEAN_TYPE
5152       && types_match (type, TREE_TYPE (@0)))
5153   (non_lvalue @0)))
5154 /* Do not handle
5155    bool_var == 0 becomes !bool_var or
5156    bool_var != 1 becomes !bool_var
5157    here because that only is good in assignment context as long
5158    as we require a tcc_comparison in GIMPLE_CONDs where we'd
5159    replace if (x == 0) with tem = ~x; if (tem != 0) which is
5160    clearly less optimal and which we'll transform again in forwprop.  */
5162 /* When one argument is a constant, overflow detection can be simplified.
5163    Currently restricted to single use so as not to interfere too much with
5164    ADD_OVERFLOW detection in tree-ssa-math-opts.c.
5165    CONVERT?(CONVERT?(A) + CST) CMP A  ->  A CMP' CST' */
5166 (for cmp (lt le ge gt)
5167      out (gt gt le le)
5168  (simplify
5169   (cmp:c (convert?@3 (plus@2 (convert?@4 @0) INTEGER_CST@1)) @0)
5170   (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@2))
5171        && types_match (TREE_TYPE (@0), TREE_TYPE (@3))
5172        && tree_nop_conversion_p (TREE_TYPE (@4), TREE_TYPE (@0))
5173        && wi::to_wide (@1) != 0
5174        && single_use (@2))
5175    (with {
5176      unsigned int prec = TYPE_PRECISION (TREE_TYPE (@0));
5177      signop sign = TYPE_SIGN (TREE_TYPE (@0));
5178     }
5179     (out @0 { wide_int_to_tree (TREE_TYPE (@0),
5180                                 wi::max_value (prec, sign)
5181                                 - wi::to_wide (@1)); })))))
5183 /* To detect overflow in unsigned A - B, A < B is simpler than A - B > A.
5184    However, the detection logic for SUB_OVERFLOW in tree-ssa-math-opts.c
5185    expects the long form, so we restrict the transformation for now.  */
5186 (for cmp (gt le)
5187  (simplify
5188   (cmp:c (minus@2 @0 @1) @0)
5189   (if (single_use (@2)
5190        && ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
5191        && TYPE_UNSIGNED (TREE_TYPE (@0)))
5192    (cmp @1 @0))))
5194 /* Optimize A - B + -1 >= A into B >= A for unsigned comparisons.  */
5195 (for cmp (ge lt)
5196  (simplify
5197   (cmp:c (plus (minus @0 @1) integer_minus_onep) @0)
5198    (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
5199         && TYPE_UNSIGNED (TREE_TYPE (@0)))
5200     (cmp @1 @0))))
5202 /* Testing for overflow is unnecessary if we already know the result.  */
5203 /* A - B > A  */
5204 (for cmp (gt le)
5205      out (ne eq)
5206  (simplify
5207   (cmp:c (realpart (IFN_SUB_OVERFLOW@2 @0 @1)) @0)
5208   (if (TYPE_UNSIGNED (TREE_TYPE (@0))
5209        && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
5210    (out (imagpart @2) { build_zero_cst (TREE_TYPE (@0)); }))))
5211 /* A + B < A  */
5212 (for cmp (lt ge)
5213      out (ne eq)
5214  (simplify
5215   (cmp:c (realpart (IFN_ADD_OVERFLOW:c@2 @0 @1)) @0)
5216   (if (TYPE_UNSIGNED (TREE_TYPE (@0))
5217        && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
5218    (out (imagpart @2) { build_zero_cst (TREE_TYPE (@0)); }))))
5220 /* For unsigned operands, -1 / B < A checks whether A * B would overflow.
5221    Simplify it to __builtin_mul_overflow (A, B, <unused>).  */
5222 (for cmp (lt ge)
5223      out (ne eq)
5224  (simplify
5225   (cmp:c (trunc_div:s integer_all_onesp @1) @0)
5226   (if (TYPE_UNSIGNED (TREE_TYPE (@0)) && !VECTOR_TYPE_P (TREE_TYPE (@0)))
5227    (with { tree t = TREE_TYPE (@0), cpx = build_complex_type (t); }
5228     (out (imagpart (IFN_MUL_OVERFLOW:cpx @0 @1)) { build_zero_cst (t); })))))
5230 /* Similarly, for unsigned operands, (((type) A * B) >> prec) != 0 where type
5231    is at least twice as wide as type of A and B, simplify to
5232    __builtin_mul_overflow (A, B, <unused>).  */
5233 (for cmp (eq ne)
5234  (simplify
5235   (cmp (rshift (mult:s (convert@3 @0) (convert @1)) INTEGER_CST@2)
5236        integer_zerop)
5237   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
5238        && INTEGRAL_TYPE_P (TREE_TYPE (@3))
5239        && TYPE_UNSIGNED (TREE_TYPE (@0))
5240        && (TYPE_PRECISION (TREE_TYPE (@3))
5241            >= 2 * TYPE_PRECISION (TREE_TYPE (@0)))
5242        && tree_fits_uhwi_p (@2)
5243        && tree_to_uhwi (@2) == TYPE_PRECISION (TREE_TYPE (@0))
5244        && types_match (@0, @1)
5245        && type_has_mode_precision_p (TREE_TYPE (@0))
5246        && (optab_handler (umulv4_optab, TYPE_MODE (TREE_TYPE (@0)))
5247            != CODE_FOR_nothing))
5248    (with { tree t = TREE_TYPE (@0), cpx = build_complex_type (t); }
5249     (cmp (imagpart (IFN_MUL_OVERFLOW:cpx @0 @1)) { build_zero_cst (t); })))))
5251 /* Simplification of math builtins.  These rules must all be optimizations
5252    as well as IL simplifications.  If there is a possibility that the new
5253    form could be a pessimization, the rule should go in the canonicalization
5254    section that follows this one.
5256    Rules can generally go in this section if they satisfy one of
5257    the following:
5259    - the rule describes an identity
5261    - the rule replaces calls with something as simple as addition or
5262      multiplication
5264    - the rule contains unary calls only and simplifies the surrounding
5265      arithmetic.  (The idea here is to exclude non-unary calls in which
5266      one operand is constant and in which the call is known to be cheap
5267      when the operand has that value.)  */
5269 (if (flag_unsafe_math_optimizations)
5270  /* Simplify sqrt(x) * sqrt(x) -> x.  */
5271  (simplify
5272   (mult (SQRT_ALL@1 @0) @1)
5273   (if (!tree_expr_maybe_signaling_nan_p (@0))
5274    @0))
5276  (for op (plus minus)
5277   /* Simplify (A / C) +- (B / C) -> (A +- B) / C.  */
5278   (simplify
5279    (op (rdiv @0 @1)
5280        (rdiv @2 @1))
5281    (rdiv (op @0 @2) @1)))
5283  (for cmp (lt le gt ge)
5284       neg_cmp (gt ge lt le)
5285   /* Simplify (x * C1) cmp C2 -> x cmp (C2 / C1), where C1 != 0.  */
5286   (simplify
5287    (cmp (mult @0 REAL_CST@1) REAL_CST@2)
5288    (with
5289     { tree tem = const_binop (RDIV_EXPR, type, @2, @1); }
5290     (if (tem
5291          && !(REAL_VALUE_ISINF (TREE_REAL_CST (tem))
5292               || (real_zerop (tem) && !real_zerop (@1))))
5293      (switch
5294       (if (real_less (&dconst0, TREE_REAL_CST_PTR (@1)))
5295        (cmp @0 { tem; }))
5296       (if (real_less (TREE_REAL_CST_PTR (@1), &dconst0))
5297        (neg_cmp @0 { tem; })))))))
5299  /* Simplify sqrt(x) * sqrt(y) -> sqrt(x*y).  */
5300  (for root (SQRT CBRT)
5301   (simplify
5302    (mult (root:s @0) (root:s @1))
5303     (root (mult @0 @1))))
5305  /* Simplify expN(x) * expN(y) -> expN(x+y). */
5306  (for exps (EXP EXP2 EXP10 POW10)
5307   (simplify
5308    (mult (exps:s @0) (exps:s @1))
5309     (exps (plus @0 @1))))
5311  /* Simplify a/root(b/c) into a*root(c/b).  */
5312  (for root (SQRT CBRT)
5313   (simplify
5314    (rdiv @0 (root:s (rdiv:s @1 @2)))
5315     (mult @0 (root (rdiv @2 @1)))))
5317  /* Simplify x/expN(y) into x*expN(-y).  */
5318  (for exps (EXP EXP2 EXP10 POW10)
5319   (simplify
5320    (rdiv @0 (exps:s @1))
5321     (mult @0 (exps (negate @1)))))
5323  (for logs (LOG LOG2 LOG10 LOG10)
5324       exps (EXP EXP2 EXP10 POW10)
5325   /* logN(expN(x)) -> x.  */
5326   (simplify
5327    (logs (exps @0))
5328    @0)
5329   /* expN(logN(x)) -> x.  */
5330   (simplify
5331    (exps (logs @0))
5332    @0))
5334  /* Optimize logN(func()) for various exponential functions.  We
5335     want to determine the value "x" and the power "exponent" in
5336     order to transform logN(x**exponent) into exponent*logN(x).  */
5337  (for logs (LOG  LOG   LOG   LOG2 LOG2  LOG2  LOG10 LOG10)
5338       exps (EXP2 EXP10 POW10 EXP  EXP10 POW10 EXP   EXP2)
5339   (simplify
5340    (logs (exps @0))
5341    (if (SCALAR_FLOAT_TYPE_P (type))
5342     (with {
5343       tree x;
5344       switch (exps)
5345         {
5346         CASE_CFN_EXP:
5347           /* Prepare to do logN(exp(exponent)) -> exponent*logN(e).  */
5348           x = build_real_truncate (type, dconst_e ());
5349           break;
5350         CASE_CFN_EXP2:
5351           /* Prepare to do logN(exp2(exponent)) -> exponent*logN(2).  */
5352           x = build_real (type, dconst2);
5353           break;
5354         CASE_CFN_EXP10:
5355         CASE_CFN_POW10:
5356           /* Prepare to do logN(exp10(exponent)) -> exponent*logN(10).  */
5357           {
5358             REAL_VALUE_TYPE dconst10;
5359             real_from_integer (&dconst10, VOIDmode, 10, SIGNED);
5360             x = build_real (type, dconst10);
5361           }
5362           break;
5363         default:
5364           gcc_unreachable ();
5365         }
5366       }
5367      (mult (logs { x; }) @0)))))
5369  (for logs (LOG LOG
5370             LOG2 LOG2
5371             LOG10 LOG10)
5372       exps (SQRT CBRT)
5373   (simplify
5374    (logs (exps @0))
5375    (if (SCALAR_FLOAT_TYPE_P (type))
5376     (with {
5377       tree x;
5378       switch (exps)
5379         {
5380         CASE_CFN_SQRT:
5381           /* Prepare to do logN(sqrt(x)) -> 0.5*logN(x).  */
5382           x = build_real (type, dconsthalf);
5383           break;
5384         CASE_CFN_CBRT:
5385           /* Prepare to do logN(cbrt(x)) -> (1/3)*logN(x).  */
5386           x = build_real_truncate (type, dconst_third ());
5387           break;
5388         default:
5389           gcc_unreachable ();
5390         }
5391       }
5392      (mult { x; } (logs @0))))))
5394  /* logN(pow(x,exponent)) -> exponent*logN(x).  */
5395  (for logs (LOG LOG2 LOG10)
5396       pows (POW)
5397   (simplify
5398    (logs (pows @0 @1))
5399    (mult @1 (logs @0))))
5401  /* pow(C,x) -> exp(log(C)*x) if C > 0,
5402     or if C is a positive power of 2,
5403     pow(C,x) -> exp2(log2(C)*x).  */
5404 #if GIMPLE
5405  (for pows (POW)
5406       exps (EXP)
5407       logs (LOG)
5408       exp2s (EXP2)
5409       log2s (LOG2)
5410   (simplify
5411    (pows REAL_CST@0 @1)
5412    (if (real_compare (GT_EXPR, TREE_REAL_CST_PTR (@0), &dconst0)
5413         && real_isfinite (TREE_REAL_CST_PTR (@0))
5414         /* As libmvec doesn't have a vectorized exp2, defer optimizing
5415            the use_exp2 case until after vectorization.  It seems actually
5416            beneficial for all constants to postpone this until later,
5417            because exp(log(C)*x), while faster, will have worse precision
5418            and if x folds into a constant too, that is unnecessary
5419            pessimization.  */
5420         && canonicalize_math_after_vectorization_p ())
5421     (with {
5422        const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (@0);
5423        bool use_exp2 = false;
5424        if (targetm.libc_has_function (function_c99_misc, TREE_TYPE (@0))
5425            && value->cl == rvc_normal)
5426          {
5427            REAL_VALUE_TYPE frac_rvt = *value;
5428            SET_REAL_EXP (&frac_rvt, 1);
5429            if (real_equal (&frac_rvt, &dconst1))
5430              use_exp2 = true;
5431          }
5432      }
5433      (if (!use_exp2)
5434       (if (optimize_pow_to_exp (@0, @1))
5435        (exps (mult (logs @0) @1)))
5436       (exp2s (mult (log2s @0) @1)))))))
5437 #endif
5439  /* pow(C,x)*expN(y) -> expN(logN(C)*x+y) if C > 0.  */
5440  (for pows (POW)
5441       exps (EXP EXP2 EXP10 POW10)
5442       logs (LOG LOG2 LOG10 LOG10)
5443   (simplify
5444    (mult:c (pows:s REAL_CST@0 @1) (exps:s @2))
5445    (if (real_compare (GT_EXPR, TREE_REAL_CST_PTR (@0), &dconst0)
5446         && real_isfinite (TREE_REAL_CST_PTR (@0)))
5447     (exps (plus (mult (logs @0) @1) @2)))))
5449  (for sqrts (SQRT)
5450       cbrts (CBRT)
5451       pows (POW)
5452       exps (EXP EXP2 EXP10 POW10)
5453   /* sqrt(expN(x)) -> expN(x*0.5).  */
5454   (simplify
5455    (sqrts (exps @0))
5456    (exps (mult @0 { build_real (type, dconsthalf); })))
5457   /* cbrt(expN(x)) -> expN(x/3).  */
5458   (simplify
5459    (cbrts (exps @0))
5460    (exps (mult @0 { build_real_truncate (type, dconst_third ()); })))
5461   /* pow(expN(x), y) -> expN(x*y).  */
5462   (simplify
5463    (pows (exps @0) @1)
5464    (exps (mult @0 @1))))
5466  /* tan(atan(x)) -> x.  */
5467  (for tans (TAN)
5468       atans (ATAN)
5469   (simplify
5470    (tans (atans @0))
5471    @0)))
5473  /* Simplify sin(atan(x)) -> x / sqrt(x*x + 1). */
5474  (for sins (SIN)
5475       atans (ATAN)
5476       sqrts (SQRT)
5477       copysigns (COPYSIGN)
5478   (simplify
5479    (sins (atans:s @0))
5480    (with
5481      {
5482       REAL_VALUE_TYPE r_cst;
5483       build_sinatan_real (&r_cst, type);
5484       tree t_cst = build_real (type, r_cst);
5485       tree t_one = build_one_cst (type);
5486      }
5487     (if (SCALAR_FLOAT_TYPE_P (type))
5488      (cond (lt (abs @0) { t_cst; })
5489       (rdiv @0 (sqrts (plus (mult @0 @0) { t_one; })))
5490       (copysigns { t_one; } @0))))))
5492 /* Simplify cos(atan(x)) -> 1 / sqrt(x*x + 1). */
5493  (for coss (COS)
5494       atans (ATAN)
5495       sqrts (SQRT)
5496       copysigns (COPYSIGN)
5497   (simplify
5498    (coss (atans:s @0))
5499    (with
5500      {
5501       REAL_VALUE_TYPE r_cst;
5502       build_sinatan_real (&r_cst, type);
5503       tree t_cst = build_real (type, r_cst);
5504       tree t_one = build_one_cst (type);
5505       tree t_zero = build_zero_cst (type);
5506      }
5507     (if (SCALAR_FLOAT_TYPE_P (type))
5508      (cond (lt (abs @0) { t_cst; })
5509       (rdiv { t_one; } (sqrts (plus (mult @0 @0) { t_one; })))
5510       (copysigns { t_zero; } @0))))))
5512  (if (!flag_errno_math)
5513   /* Simplify sinh(atanh(x)) -> x / sqrt((1 - x)*(1 + x)). */
5514   (for sinhs (SINH)
5515        atanhs (ATANH)
5516        sqrts (SQRT)
5517    (simplify
5518     (sinhs (atanhs:s @0))
5519     (with { tree t_one = build_one_cst (type); }
5520     (rdiv @0 (sqrts (mult (minus { t_one; } @0) (plus { t_one; } @0)))))))
5522   /* Simplify cosh(atanh(x)) -> 1 / sqrt((1 - x)*(1 + x)) */
5523   (for coshs (COSH)
5524        atanhs (ATANH)
5525        sqrts (SQRT)
5526    (simplify
5527     (coshs (atanhs:s @0))
5528     (with { tree t_one = build_one_cst (type); }
5529     (rdiv { t_one; } (sqrts (mult (minus { t_one; } @0) (plus { t_one; } @0))))))))
5531 /* cabs(x+0i) or cabs(0+xi) -> abs(x).  */
5532 (simplify
5533  (CABS (complex:C @0 real_zerop@1))
5534  (abs @0))
5536 /* trunc(trunc(x)) -> trunc(x), etc.  */
5537 (for fns (TRUNC_ALL FLOOR_ALL CEIL_ALL ROUND_ALL NEARBYINT_ALL RINT_ALL)
5538  (simplify
5539   (fns (fns @0))
5540   (fns @0)))
5541 /* f(x) -> x if x is integer valued and f does nothing for such values.  */
5542 (for fns (TRUNC_ALL FLOOR_ALL CEIL_ALL ROUND_ALL NEARBYINT_ALL RINT_ALL)
5543  (simplify
5544   (fns integer_valued_real_p@0)
5545   @0))
5547 /* hypot(x,0) and hypot(0,x) -> abs(x).  */
5548 (simplify
5549  (HYPOT:c @0 real_zerop@1)
5550  (abs @0))
5552 /* pow(1,x) -> 1.  */
5553 (simplify
5554  (POW real_onep@0 @1)
5555  @0)
5557 (simplify
5558  /* copysign(x,x) -> x.  */
5559  (COPYSIGN_ALL @0 @0)
5560  @0)
5562 (simplify
5563  /* copysign(x,-x) -> -x.  */
5564  (COPYSIGN_ALL @0 (negate@1 @0))
5565  @1)
5567 (simplify
5568  /* copysign(x,y) -> fabs(x) if y is nonnegative.  */
5569  (COPYSIGN_ALL @0 tree_expr_nonnegative_p@1)
5570  (abs @0))
5572 (for scale (LDEXP SCALBN SCALBLN)
5573  /* ldexp(0, x) -> 0.  */
5574  (simplify
5575   (scale real_zerop@0 @1)
5576   @0)
5577  /* ldexp(x, 0) -> x.  */
5578  (simplify
5579   (scale @0 integer_zerop@1)
5580   @0)
5581  /* ldexp(x, y) -> x if x is +-Inf or NaN.  */
5582  (simplify
5583   (scale REAL_CST@0 @1)
5584   (if (!real_isfinite (TREE_REAL_CST_PTR (@0)))
5585    @0)))
5587 /* Canonicalization of sequences of math builtins.  These rules represent
5588    IL simplifications but are not necessarily optimizations.
5590    The sincos pass is responsible for picking "optimal" implementations
5591    of math builtins, which may be more complicated and can sometimes go
5592    the other way, e.g. converting pow into a sequence of sqrts.
5593    We only want to do these canonicalizations before the pass has run.  */
5595 (if (flag_unsafe_math_optimizations && canonicalize_math_p ())
5596  /* Simplify tan(x) * cos(x) -> sin(x). */
5597  (simplify
5598   (mult:c (TAN:s @0) (COS:s @0))
5599    (SIN @0))
5601  /* Simplify x * pow(x,c) -> pow(x,c+1). */
5602  (simplify
5603   (mult:c @0 (POW:s @0 REAL_CST@1))
5604   (if (!TREE_OVERFLOW (@1))
5605    (POW @0 (plus @1 { build_one_cst (type); }))))
5607  /* Simplify sin(x) / cos(x) -> tan(x). */
5608  (simplify
5609   (rdiv (SIN:s @0) (COS:s @0))
5610    (TAN @0))
5612  /* Simplify sinh(x) / cosh(x) -> tanh(x). */
5613  (simplify
5614   (rdiv (SINH:s @0) (COSH:s @0))
5615    (TANH @0))
5617  /* Simplify tanh (x) / sinh (x) -> 1.0 / cosh (x). */
5618  (simplify
5619    (rdiv (TANH:s @0) (SINH:s @0))
5620    (rdiv {build_one_cst (type);} (COSH @0)))
5622  /* Simplify cos(x) / sin(x) -> 1 / tan(x). */
5623  (simplify
5624   (rdiv (COS:s @0) (SIN:s @0))
5625    (rdiv { build_one_cst (type); } (TAN @0)))
5627  /* Simplify sin(x) / tan(x) -> cos(x). */
5628  (simplify
5629   (rdiv (SIN:s @0) (TAN:s @0))
5630   (if (! HONOR_NANS (@0)
5631        && ! HONOR_INFINITIES (@0))
5632    (COS @0)))
5634  /* Simplify tan(x) / sin(x) -> 1.0 / cos(x). */
5635  (simplify
5636   (rdiv (TAN:s @0) (SIN:s @0))
5637   (if (! HONOR_NANS (@0)
5638        && ! HONOR_INFINITIES (@0))
5639    (rdiv { build_one_cst (type); } (COS @0))))
5641  /* Simplify pow(x,y) * pow(x,z) -> pow(x,y+z). */
5642  (simplify
5643   (mult (POW:s @0 @1) (POW:s @0 @2))
5644    (POW @0 (plus @1 @2)))
5646  /* Simplify pow(x,y) * pow(z,y) -> pow(x*z,y). */
5647  (simplify
5648   (mult (POW:s @0 @1) (POW:s @2 @1))
5649    (POW (mult @0 @2) @1))
5651  /* Simplify powi(x,y) * powi(z,y) -> powi(x*z,y). */
5652  (simplify
5653   (mult (POWI:s @0 @1) (POWI:s @2 @1))
5654    (POWI (mult @0 @2) @1))
5656  /* Simplify pow(x,c) / x -> pow(x,c-1). */
5657  (simplify
5658   (rdiv (POW:s @0 REAL_CST@1) @0)
5659   (if (!TREE_OVERFLOW (@1))
5660    (POW @0 (minus @1 { build_one_cst (type); }))))
5662  /* Simplify x / pow (y,z) -> x * pow(y,-z). */
5663  (simplify
5664   (rdiv @0 (POW:s @1 @2))
5665    (mult @0 (POW @1 (negate @2))))
5667  (for sqrts (SQRT)
5668       cbrts (CBRT)
5669       pows (POW)
5670   /* sqrt(sqrt(x)) -> pow(x,1/4).  */
5671   (simplify
5672    (sqrts (sqrts @0))
5673    (pows @0 { build_real (type, dconst_quarter ()); }))
5674   /* sqrt(cbrt(x)) -> pow(x,1/6).  */
5675   (simplify
5676    (sqrts (cbrts @0))
5677    (pows @0 { build_real_truncate (type, dconst_sixth ()); }))
5678   /* cbrt(sqrt(x)) -> pow(x,1/6).  */
5679   (simplify
5680    (cbrts (sqrts @0))
5681    (pows @0 { build_real_truncate (type, dconst_sixth ()); }))
5682   /* cbrt(cbrt(x)) -> pow(x,1/9), iff x is nonnegative.  */
5683   (simplify
5684    (cbrts (cbrts tree_expr_nonnegative_p@0))
5685    (pows @0 { build_real_truncate (type, dconst_ninth ()); }))
5686   /* sqrt(pow(x,y)) -> pow(|x|,y*0.5).  */
5687   (simplify
5688    (sqrts (pows @0 @1))
5689    (pows (abs @0) (mult @1 { build_real (type, dconsthalf); })))
5690   /* cbrt(pow(x,y)) -> pow(x,y/3), iff x is nonnegative.  */
5691   (simplify
5692    (cbrts (pows tree_expr_nonnegative_p@0 @1))
5693    (pows @0 (mult @1 { build_real_truncate (type, dconst_third ()); })))
5694   /* pow(sqrt(x),y) -> pow(x,y*0.5).  */
5695   (simplify
5696    (pows (sqrts @0) @1)
5697    (pows @0 (mult @1 { build_real (type, dconsthalf); })))
5698   /* pow(cbrt(x),y) -> pow(x,y/3) iff x is nonnegative.  */
5699   (simplify
5700    (pows (cbrts tree_expr_nonnegative_p@0) @1)
5701    (pows @0 (mult @1 { build_real_truncate (type, dconst_third ()); })))
5702   /* pow(pow(x,y),z) -> pow(x,y*z) iff x is nonnegative.  */
5703   (simplify
5704    (pows (pows tree_expr_nonnegative_p@0 @1) @2)
5705    (pows @0 (mult @1 @2))))
5707  /* cabs(x+xi) -> fabs(x)*sqrt(2).  */
5708  (simplify
5709   (CABS (complex @0 @0))
5710   (mult (abs @0) { build_real_truncate (type, dconst_sqrt2 ()); }))
5712  /* hypot(x,x) -> fabs(x)*sqrt(2).  */
5713  (simplify
5714   (HYPOT @0 @0)
5715   (mult (abs @0) { build_real_truncate (type, dconst_sqrt2 ()); }))
5717  /* cexp(x+yi) -> exp(x)*cexpi(y).  */
5718  (for cexps (CEXP)
5719       exps (EXP)
5720       cexpis (CEXPI)
5721   (simplify
5722    (cexps compositional_complex@0)
5723    (if (targetm.libc_has_function (function_c99_math_complex, TREE_TYPE (@0)))
5724     (complex
5725      (mult (exps@1 (realpart @0)) (realpart (cexpis:type@2 (imagpart @0))))
5726      (mult @1 (imagpart @2)))))))
5728 (if (canonicalize_math_p ())
5729  /* floor(x) -> trunc(x) if x is nonnegative.  */
5730  (for floors (FLOOR_ALL)
5731       truncs (TRUNC_ALL)
5732   (simplify
5733    (floors tree_expr_nonnegative_p@0)
5734    (truncs @0))))
5736 (match double_value_p
5737  @0
5738  (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == double_type_node)))
5739 (for froms (BUILT_IN_TRUNCL
5740             BUILT_IN_FLOORL
5741             BUILT_IN_CEILL
5742             BUILT_IN_ROUNDL
5743             BUILT_IN_NEARBYINTL
5744             BUILT_IN_RINTL)
5745      tos (BUILT_IN_TRUNC
5746           BUILT_IN_FLOOR
5747           BUILT_IN_CEIL
5748           BUILT_IN_ROUND
5749           BUILT_IN_NEARBYINT
5750           BUILT_IN_RINT)
5751  /* truncl(extend(x)) -> extend(trunc(x)), etc., if x is a double.  */
5752  (if (optimize && canonicalize_math_p ())
5753   (simplify
5754    (froms (convert double_value_p@0))
5755    (convert (tos @0)))))
5757 (match float_value_p
5758  @0
5759  (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == float_type_node)))
5760 (for froms (BUILT_IN_TRUNCL BUILT_IN_TRUNC
5761             BUILT_IN_FLOORL BUILT_IN_FLOOR
5762             BUILT_IN_CEILL BUILT_IN_CEIL
5763             BUILT_IN_ROUNDL BUILT_IN_ROUND
5764             BUILT_IN_NEARBYINTL BUILT_IN_NEARBYINT
5765             BUILT_IN_RINTL BUILT_IN_RINT)
5766      tos (BUILT_IN_TRUNCF BUILT_IN_TRUNCF
5767           BUILT_IN_FLOORF BUILT_IN_FLOORF
5768           BUILT_IN_CEILF BUILT_IN_CEILF
5769           BUILT_IN_ROUNDF BUILT_IN_ROUNDF
5770           BUILT_IN_NEARBYINTF BUILT_IN_NEARBYINTF
5771           BUILT_IN_RINTF BUILT_IN_RINTF)
5772  /* truncl(extend(x)) and trunc(extend(x)) -> extend(truncf(x)), etc.,
5773     if x is a float.  */
5774  (if (optimize && canonicalize_math_p ()
5775       && targetm.libc_has_function (function_c99_misc, NULL_TREE))
5776   (simplify
5777    (froms (convert float_value_p@0))
5778    (convert (tos @0)))))
5780 (for froms (XFLOORL XCEILL XROUNDL XRINTL)
5781      tos (XFLOOR XCEIL XROUND XRINT)
5782  /* llfloorl(extend(x)) -> llfloor(x), etc., if x is a double.  */
5783  (if (optimize && canonicalize_math_p ())
5784   (simplify
5785    (froms (convert double_value_p@0))
5786    (tos @0))))
5788 (for froms (XFLOORL XCEILL XROUNDL XRINTL
5789             XFLOOR XCEIL XROUND XRINT)
5790      tos (XFLOORF XCEILF XROUNDF XRINTF)
5791  /* llfloorl(extend(x)) and llfloor(extend(x)) -> llfloorf(x), etc.,
5792     if x is a float.  */
5793  (if (optimize && canonicalize_math_p ())
5794   (simplify
5795    (froms (convert float_value_p@0))
5796    (tos @0))))
5798 (if (canonicalize_math_p ())
5799  /* xfloor(x) -> fix_trunc(x) if x is nonnegative.  */
5800  (for floors (IFLOOR LFLOOR LLFLOOR)
5801   (simplify
5802    (floors tree_expr_nonnegative_p@0)
5803    (fix_trunc @0))))
5805 (if (canonicalize_math_p ())
5806  /* xfloor(x) -> fix_trunc(x), etc., if x is integer valued.  */
5807  (for fns (IFLOOR LFLOOR LLFLOOR
5808            ICEIL LCEIL LLCEIL
5809            IROUND LROUND LLROUND)
5810   (simplify
5811    (fns integer_valued_real_p@0)
5812    (fix_trunc @0)))
5813  (if (!flag_errno_math)
5814   /* xrint(x) -> fix_trunc(x), etc., if x is integer valued.  */
5815   (for rints (IRINT LRINT LLRINT)
5816    (simplify
5817     (rints integer_valued_real_p@0)
5818     (fix_trunc @0)))))
5820 (if (canonicalize_math_p ())
5821  (for ifn (IFLOOR ICEIL IROUND IRINT)
5822       lfn (LFLOOR LCEIL LROUND LRINT)
5823       llfn (LLFLOOR LLCEIL LLROUND LLRINT)
5824   /* Canonicalize iround (x) to lround (x) on ILP32 targets where
5825      sizeof (int) == sizeof (long).  */
5826   (if (TYPE_PRECISION (integer_type_node)
5827        == TYPE_PRECISION (long_integer_type_node))
5828    (simplify
5829     (ifn @0)
5830     (lfn:long_integer_type_node @0)))
5831   /* Canonicalize llround (x) to lround (x) on LP64 targets where
5832      sizeof (long long) == sizeof (long).  */
5833   (if (TYPE_PRECISION (long_long_integer_type_node)
5834        == TYPE_PRECISION (long_integer_type_node))
5835    (simplify
5836     (llfn @0)
5837     (lfn:long_integer_type_node @0)))))
5839 /* cproj(x) -> x if we're ignoring infinities.  */
5840 (simplify
5841  (CPROJ @0)
5842  (if (!HONOR_INFINITIES (type))
5843    @0))
5845 /* If the real part is inf and the imag part is known to be
5846    nonnegative, return (inf + 0i).  */
5847 (simplify
5848  (CPROJ (complex REAL_CST@0 tree_expr_nonnegative_p@1))
5849  (if (real_isinf (TREE_REAL_CST_PTR (@0)))
5850   { build_complex_inf (type, false); }))
5852 /* If the imag part is inf, return (inf+I*copysign(0,imag)).  */
5853 (simplify
5854  (CPROJ (complex @0 REAL_CST@1))
5855  (if (real_isinf (TREE_REAL_CST_PTR (@1)))
5856   { build_complex_inf (type, TREE_REAL_CST_PTR (@1)->sign); }))
5858 (for pows (POW)
5859      sqrts (SQRT)
5860      cbrts (CBRT)
5861  (simplify
5862   (pows @0 REAL_CST@1)
5863   (with {
5864     const REAL_VALUE_TYPE *value = TREE_REAL_CST_PTR (@1);
5865     REAL_VALUE_TYPE tmp;
5866    }
5867    (switch
5868     /* pow(x,0) -> 1.  */
5869     (if (real_equal (value, &dconst0))
5870      { build_real (type, dconst1); })
5871     /* pow(x,1) -> x.  */
5872     (if (real_equal (value, &dconst1))
5873      @0)
5874     /* pow(x,-1) -> 1/x.  */
5875     (if (real_equal (value, &dconstm1))
5876      (rdiv { build_real (type, dconst1); } @0))
5877     /* pow(x,0.5) -> sqrt(x).  */
5878     (if (flag_unsafe_math_optimizations
5879          && canonicalize_math_p ()
5880          && real_equal (value, &dconsthalf))
5881      (sqrts @0))
5882     /* pow(x,1/3) -> cbrt(x).  */
5883     (if (flag_unsafe_math_optimizations
5884          && canonicalize_math_p ()
5885          && (tmp = real_value_truncate (TYPE_MODE (type), dconst_third ()),
5886              real_equal (value, &tmp)))
5887      (cbrts @0))))))
5889 /* powi(1,x) -> 1.  */
5890 (simplify
5891  (POWI real_onep@0 @1)
5892  @0)
5894 (simplify
5895  (POWI @0 INTEGER_CST@1)
5896  (switch
5897   /* powi(x,0) -> 1.  */
5898   (if (wi::to_wide (@1) == 0)
5899    { build_real (type, dconst1); })
5900   /* powi(x,1) -> x.  */
5901   (if (wi::to_wide (@1) == 1)
5902    @0)
5903   /* powi(x,-1) -> 1/x.  */
5904   (if (wi::to_wide (@1) == -1)
5905    (rdiv { build_real (type, dconst1); } @0))))
5907 /* Narrowing of arithmetic and logical operations.
5909    These are conceptually similar to the transformations performed for
5910    the C/C++ front-ends by shorten_binary_op and shorten_compare.  Long
5911    term we want to move all that code out of the front-ends into here.  */
5913 /* Convert (outertype)((innertype0)a+(innertype1)b)
5914    into ((newtype)a+(newtype)b) where newtype
5915    is the widest mode from all of these.  */
5916 (for op (plus minus mult rdiv)
5917  (simplify
5918    (convert (op:s@0 (convert1?@3 @1) (convert2?@4 @2)))
5919    /* If we have a narrowing conversion of an arithmetic operation where
5920       both operands are widening conversions from the same type as the outer
5921       narrowing conversion.  Then convert the innermost operands to a
5922       suitable unsigned type (to avoid introducing undefined behavior),
5923       perform the operation and convert the result to the desired type.  */
5924    (if (INTEGRAL_TYPE_P (type)
5925         && op != MULT_EXPR
5926         && op != RDIV_EXPR
5927         /* We check for type compatibility between @0 and @1 below,
5928            so there's no need to check that @2/@4 are integral types.  */
5929         && INTEGRAL_TYPE_P (TREE_TYPE (@1))
5930         && INTEGRAL_TYPE_P (TREE_TYPE (@3))
5931         /* The precision of the type of each operand must match the
5932            precision of the mode of each operand, similarly for the
5933            result.  */
5934         && type_has_mode_precision_p (TREE_TYPE (@1))
5935         && type_has_mode_precision_p (TREE_TYPE (@2))
5936         && type_has_mode_precision_p (type)
5937         /* The inner conversion must be a widening conversion.  */
5938         && TYPE_PRECISION (TREE_TYPE (@3)) > TYPE_PRECISION (TREE_TYPE (@1))
5939         && types_match (@1, type)
5940         && (types_match (@1, @2)
5941             /* Or the second operand is const integer or converted const
5942                integer from valueize.  */
5943             || TREE_CODE (@2) == INTEGER_CST))
5944      (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
5945        (op @1 (convert @2))
5946        (with { tree utype = unsigned_type_for (TREE_TYPE (@1)); }
5947         (convert (op (convert:utype @1)
5948                      (convert:utype @2)))))
5949      (if (FLOAT_TYPE_P (type)
5950           && DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0))
5951                == DECIMAL_FLOAT_TYPE_P (type))
5952       (with { tree arg0 = strip_float_extensions (@1);
5953               tree arg1 = strip_float_extensions (@2);
5954               tree itype = TREE_TYPE (@0);
5955               tree ty1 = TREE_TYPE (arg0);
5956               tree ty2 = TREE_TYPE (arg1);
5957               enum tree_code code = TREE_CODE (itype); }
5958         (if (FLOAT_TYPE_P (ty1)
5959              && FLOAT_TYPE_P (ty2))
5960          (with { tree newtype = type;
5961                  if (TYPE_MODE (ty1) == SDmode
5962                      || TYPE_MODE (ty2) == SDmode
5963                      || TYPE_MODE (type) == SDmode)
5964                    newtype = dfloat32_type_node;
5965                  if (TYPE_MODE (ty1) == DDmode
5966                      || TYPE_MODE (ty2) == DDmode
5967                      || TYPE_MODE (type) == DDmode)
5968                    newtype = dfloat64_type_node;
5969                  if (TYPE_MODE (ty1) == TDmode
5970                      || TYPE_MODE (ty2) == TDmode
5971                      || TYPE_MODE (type) == TDmode)
5972                    newtype = dfloat128_type_node; }
5973           (if ((newtype == dfloat32_type_node
5974                 || newtype == dfloat64_type_node
5975                 || newtype == dfloat128_type_node)
5976               && newtype == type
5977               && types_match (newtype, type))
5978             (op (convert:newtype @1) (convert:newtype @2))
5979             (with { if (TYPE_PRECISION (ty1) > TYPE_PRECISION (newtype))
5980                       newtype = ty1;
5981                     if (TYPE_PRECISION (ty2) > TYPE_PRECISION (newtype))
5982                       newtype = ty2; }
5983                /* Sometimes this transformation is safe (cannot
5984                   change results through affecting double rounding
5985                   cases) and sometimes it is not.  If NEWTYPE is
5986                   wider than TYPE, e.g. (float)((long double)double
5987                   + (long double)double) converted to
5988                   (float)(double + double), the transformation is
5989                   unsafe regardless of the details of the types
5990                   involved; double rounding can arise if the result
5991                   of NEWTYPE arithmetic is a NEWTYPE value half way
5992                   between two representable TYPE values but the
5993                   exact value is sufficiently different (in the
5994                   right direction) for this difference to be
5995                   visible in ITYPE arithmetic.  If NEWTYPE is the
5996                   same as TYPE, however, the transformation may be
5997                   safe depending on the types involved: it is safe
5998                   if the ITYPE has strictly more than twice as many
5999                   mantissa bits as TYPE, can represent infinities
6000                   and NaNs if the TYPE can, and has sufficient
6001                   exponent range for the product or ratio of two
6002                   values representable in the TYPE to be within the
6003                   range of normal values of ITYPE.  */
6004               (if (TYPE_PRECISION (newtype) < TYPE_PRECISION (itype)
6005                    && (flag_unsafe_math_optimizations
6006                        || (TYPE_PRECISION (newtype) == TYPE_PRECISION (type)
6007                            && real_can_shorten_arithmetic (TYPE_MODE (itype),
6008                                                            TYPE_MODE (type))
6009                            && !excess_precision_type (newtype)))
6010                    && !types_match (itype, newtype))
6011                  (convert:type (op (convert:newtype @1)
6012                                    (convert:newtype @2)))
6013          )))) )
6014    ))
6017 /* This is another case of narrowing, specifically when there's an outer
6018    BIT_AND_EXPR which masks off bits outside the type of the innermost
6019    operands.   Like the previous case we have to convert the operands
6020    to unsigned types to avoid introducing undefined behavior for the
6021    arithmetic operation.  */
6022 (for op (minus plus)
6023  (simplify
6024   (bit_and (op:s (convert@2 @0) (convert@3 @1)) INTEGER_CST@4)
6025   (if (INTEGRAL_TYPE_P (type)
6026        /* We check for type compatibility between @0 and @1 below,
6027           so there's no need to check that @1/@3 are integral types.  */
6028        && INTEGRAL_TYPE_P (TREE_TYPE (@0))
6029        && INTEGRAL_TYPE_P (TREE_TYPE (@2))
6030        /* The precision of the type of each operand must match the
6031           precision of the mode of each operand, similarly for the
6032           result.  */
6033        && type_has_mode_precision_p (TREE_TYPE (@0))
6034        && type_has_mode_precision_p (TREE_TYPE (@1))
6035        && type_has_mode_precision_p (type)
6036        /* The inner conversion must be a widening conversion.  */
6037        && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
6038        && types_match (@0, @1)
6039        && (tree_int_cst_min_precision (@4, TYPE_SIGN (TREE_TYPE (@0)))
6040            <= TYPE_PRECISION (TREE_TYPE (@0)))
6041        && (wi::to_wide (@4)
6042            & wi::mask (TYPE_PRECISION (TREE_TYPE (@0)),
6043                        true, TYPE_PRECISION (type))) == 0)
6044    (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
6045     (with { tree ntype = TREE_TYPE (@0); }
6046      (convert (bit_and (op @0 @1) (convert:ntype @4))))
6047     (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
6048      (convert (bit_and (op (convert:utype @0) (convert:utype @1))
6049                (convert:utype @4))))))))
6051 /* Transform (@0 < @1 and @0 < @2) to use min,
6052    (@0 > @1 and @0 > @2) to use max */
6053 (for logic (bit_and bit_and bit_and bit_and bit_ior bit_ior bit_ior bit_ior)
6054      op    (lt      le      gt      ge      lt      le      gt      ge     )
6055      ext   (min     min     max     max     max     max     min     min    )
6056  (simplify
6057   (logic (op:cs @0 @1) (op:cs @0 @2))
6058   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6059        && TREE_CODE (@0) != INTEGER_CST)
6060    (op @0 (ext @1 @2)))))
6062 (simplify
6063  /* signbit(x) -> 0 if x is nonnegative.  */
6064  (SIGNBIT tree_expr_nonnegative_p@0)
6065  { integer_zero_node; })
6067 (simplify
6068  /* signbit(x) -> x<0 if x doesn't have signed zeros.  */
6069  (SIGNBIT @0)
6070  (if (!HONOR_SIGNED_ZEROS (@0))
6071   (convert (lt @0 { build_real (TREE_TYPE (@0), dconst0); }))))
6073 /* Transform comparisons of the form X +- C1 CMP C2 to X CMP C2 -+ C1.  */
6074 (for cmp (eq ne)
6075  (for op (plus minus)
6076       rop (minus plus)
6077   (simplify
6078    (cmp (op@3 @0 INTEGER_CST@1) INTEGER_CST@2)
6079    (if (!TREE_OVERFLOW (@1) && !TREE_OVERFLOW (@2)
6080         && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0))
6081         && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@0))
6082         && !TYPE_SATURATING (TREE_TYPE (@0)))
6083     (with { tree res = int_const_binop (rop, @2, @1); }
6084      (if (TREE_OVERFLOW (res)
6085           && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
6086       { constant_boolean_node (cmp == NE_EXPR, type); }
6087       (if (single_use (@3))
6088        (cmp @0 { TREE_OVERFLOW (res)
6089                  ? drop_tree_overflow (res) : res; }))))))))
6090 (for cmp (lt le gt ge)
6091  (for op (plus minus)
6092       rop (minus plus)
6093   (simplify
6094    (cmp (op@3 @0 INTEGER_CST@1) INTEGER_CST@2)
6095    (if (!TREE_OVERFLOW (@1) && !TREE_OVERFLOW (@2)
6096         && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
6097     (with { tree res = int_const_binop (rop, @2, @1); }
6098      (if (TREE_OVERFLOW (res))
6099       {
6100         fold_overflow_warning (("assuming signed overflow does not occur "
6101                                 "when simplifying conditional to constant"),
6102                                WARN_STRICT_OVERFLOW_CONDITIONAL);
6103         bool less = cmp == LE_EXPR || cmp == LT_EXPR;
6104         /* wi::ges_p (@2, 0) should be sufficient for a signed type.  */
6105         bool ovf_high = wi::lt_p (wi::to_wide (@1), 0,
6106                                   TYPE_SIGN (TREE_TYPE (@1)))
6107                         != (op == MINUS_EXPR);
6108         constant_boolean_node (less == ovf_high, type);
6109       }
6110       (if (single_use (@3))
6111        (with
6112         {
6113           fold_overflow_warning (("assuming signed overflow does not occur "
6114                                   "when changing X +- C1 cmp C2 to "
6115                                   "X cmp C2 -+ C1"),
6116                                  WARN_STRICT_OVERFLOW_COMPARISON);
6117         }
6118         (cmp @0 { res; })))))))))
6120 /* Canonicalizations of BIT_FIELD_REFs.  */
6122 (simplify
6123  (BIT_FIELD_REF (BIT_FIELD_REF @0 @1 @2) @3 @4)
6124  (BIT_FIELD_REF @0 @3 { const_binop (PLUS_EXPR, bitsizetype, @2, @4); }))
6126 (simplify
6127  (BIT_FIELD_REF (view_convert @0) @1 @2)
6128  (BIT_FIELD_REF @0 @1 @2))
6130 (simplify
6131  (BIT_FIELD_REF @0 @1 integer_zerop)
6132  (if (tree_int_cst_equal (@1, TYPE_SIZE (TREE_TYPE (@0))))
6133   (view_convert @0)))
6135 (simplify
6136  (BIT_FIELD_REF @0 @1 @2)
6137  (switch
6138   (if (TREE_CODE (TREE_TYPE (@0)) == COMPLEX_TYPE
6139        && tree_int_cst_equal (@1, TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))))
6140    (switch
6141     (if (integer_zerop (@2))
6142      (view_convert (realpart @0)))
6143     (if (tree_int_cst_equal (@2, TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))))
6144      (view_convert (imagpart @0)))))
6145   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6146        && INTEGRAL_TYPE_P (type)
6147        /* On GIMPLE this should only apply to register arguments.  */
6148        && (! GIMPLE || is_gimple_reg (@0))
6149        /* A bit-field-ref that referenced the full argument can be stripped.  */
6150        && ((compare_tree_int (@1, TYPE_PRECISION (TREE_TYPE (@0))) == 0
6151             && integer_zerop (@2))
6152            /* Low-parts can be reduced to integral conversions.
6153               ???  The following doesn't work for PDP endian.  */
6154            || (BYTES_BIG_ENDIAN == WORDS_BIG_ENDIAN
6155                /* But only do this after vectorization.  */
6156                && canonicalize_math_after_vectorization_p ()
6157                /* Don't even think about BITS_BIG_ENDIAN.  */
6158                && TYPE_PRECISION (TREE_TYPE (@0)) % BITS_PER_UNIT == 0
6159                && TYPE_PRECISION (type) % BITS_PER_UNIT == 0
6160                && compare_tree_int (@2, (BYTES_BIG_ENDIAN
6161                                          ? (TYPE_PRECISION (TREE_TYPE (@0))
6162                                             - TYPE_PRECISION (type))
6163                                          : 0)) == 0)))
6164    (convert @0))))
6166 /* Simplify vector extracts.  */
6168 (simplify
6169  (BIT_FIELD_REF CONSTRUCTOR@0 @1 @2)
6170  (if (VECTOR_TYPE_P (TREE_TYPE (@0))
6171       && (types_match (type, TREE_TYPE (TREE_TYPE (@0)))
6172           || (VECTOR_TYPE_P (type)
6173               && types_match (TREE_TYPE (type), TREE_TYPE (TREE_TYPE (@0))))))
6174   (with
6175    {
6176      tree ctor = (TREE_CODE (@0) == SSA_NAME
6177                   ? gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0)) : @0);
6178      tree eltype = TREE_TYPE (TREE_TYPE (ctor));
6179      unsigned HOST_WIDE_INT width = tree_to_uhwi (TYPE_SIZE (eltype));
6180      unsigned HOST_WIDE_INT n = tree_to_uhwi (@1);
6181      unsigned HOST_WIDE_INT idx = tree_to_uhwi (@2);
6182    }
6183    (if (n != 0
6184         && (idx % width) == 0
6185         && (n % width) == 0
6186         && known_le ((idx + n) / width,
6187                      TYPE_VECTOR_SUBPARTS (TREE_TYPE (ctor))))
6188     (with
6189      {
6190        idx = idx / width;
6191        n = n / width;
6192        /* Constructor elements can be subvectors.  */
6193        poly_uint64 k = 1;
6194        if (CONSTRUCTOR_NELTS (ctor) != 0)
6195          {
6196            tree cons_elem = TREE_TYPE (CONSTRUCTOR_ELT (ctor, 0)->value);
6197            if (TREE_CODE (cons_elem) == VECTOR_TYPE)
6198              k = TYPE_VECTOR_SUBPARTS (cons_elem);
6199          }
6200        unsigned HOST_WIDE_INT elt, count, const_k;
6201      }
6202      (switch
6203       /* We keep an exact subset of the constructor elements.  */
6204       (if (multiple_p (idx, k, &elt) && multiple_p (n, k, &count))
6205        (if (CONSTRUCTOR_NELTS (ctor) == 0)
6206         { build_constructor (type, NULL); }
6207         (if (count == 1)
6208          (if (elt < CONSTRUCTOR_NELTS (ctor))
6209           (view_convert { CONSTRUCTOR_ELT (ctor, elt)->value; })
6210           { build_zero_cst (type); })
6211          /* We don't want to emit new CTORs unless the old one goes away.
6212             ???  Eventually allow this if the CTOR ends up constant or
6213             uniform.  */
6214          (if (single_use (@0))
6215           {
6216             vec<constructor_elt, va_gc> *vals;
6217             vec_alloc (vals, count);
6218             for (unsigned i = 0;
6219                  i < count && elt + i < CONSTRUCTOR_NELTS (ctor); ++i)
6220               CONSTRUCTOR_APPEND_ELT (vals, NULL_TREE,
6221                                       CONSTRUCTOR_ELT (ctor, elt + i)->value);
6222             build_constructor (type, vals);
6223           }))))
6224       /* The bitfield references a single constructor element.  */
6225       (if (k.is_constant (&const_k)
6226            && idx + n <= (idx / const_k + 1) * const_k)
6227        (switch
6228         (if (CONSTRUCTOR_NELTS (ctor) <= idx / const_k)
6229          { build_zero_cst (type); })
6230         (if (n == const_k)
6231          (view_convert { CONSTRUCTOR_ELT (ctor, idx / const_k)->value; }))
6232         (BIT_FIELD_REF { CONSTRUCTOR_ELT (ctor, idx / const_k)->value; }
6233                        @1 { bitsize_int ((idx % const_k) * width); })))))))))
6235 /* Simplify a bit extraction from a bit insertion for the cases with
6236    the inserted element fully covering the extraction or the insertion
6237    not touching the extraction.  */
6238 (simplify
6239  (BIT_FIELD_REF (bit_insert @0 @1 @ipos) @rsize @rpos)
6240  (with
6241   {
6242     unsigned HOST_WIDE_INT isize;
6243     if (INTEGRAL_TYPE_P (TREE_TYPE (@1)))
6244       isize = TYPE_PRECISION (TREE_TYPE (@1));
6245     else
6246       isize = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (@1)));
6247   }
6248   (switch
6249    (if (wi::leu_p (wi::to_wide (@ipos), wi::to_wide (@rpos))
6250         && wi::leu_p (wi::to_wide (@rpos) + wi::to_wide (@rsize),
6251                       wi::to_wide (@ipos) + isize))
6252     (BIT_FIELD_REF @1 @rsize { wide_int_to_tree (bitsizetype,
6253                                                  wi::to_wide (@rpos)
6254                                                  - wi::to_wide (@ipos)); }))
6255    (if (wi::geu_p (wi::to_wide (@ipos),
6256                    wi::to_wide (@rpos) + wi::to_wide (@rsize))
6257         || wi::geu_p (wi::to_wide (@rpos),
6258                       wi::to_wide (@ipos) + isize))
6259     (BIT_FIELD_REF @0 @rsize @rpos)))))
6261 (if (canonicalize_math_after_vectorization_p ())
6262  (for fmas (FMA)
6263   (simplify
6264    (fmas:c (negate @0) @1 @2)
6265    (IFN_FNMA @0 @1 @2))
6266   (simplify
6267    (fmas @0 @1 (negate @2))
6268    (IFN_FMS @0 @1 @2))
6269   (simplify
6270    (fmas:c (negate @0) @1 (negate @2))
6271    (IFN_FNMS @0 @1 @2))
6272   (simplify
6273    (negate (fmas@3 @0 @1 @2))
6274    (if (single_use (@3))
6275     (IFN_FNMS @0 @1 @2))))
6277  (simplify
6278   (IFN_FMS:c (negate @0) @1 @2)
6279   (IFN_FNMS @0 @1 @2))
6280  (simplify
6281   (IFN_FMS @0 @1 (negate @2))
6282   (IFN_FMA @0 @1 @2))
6283  (simplify
6284   (IFN_FMS:c (negate @0) @1 (negate @2))
6285   (IFN_FNMA @0 @1 @2))
6286  (simplify
6287   (negate (IFN_FMS@3 @0 @1 @2))
6288    (if (single_use (@3))
6289     (IFN_FNMA @0 @1 @2)))
6291  (simplify
6292   (IFN_FNMA:c (negate @0) @1 @2)
6293   (IFN_FMA @0 @1 @2))
6294  (simplify
6295   (IFN_FNMA @0 @1 (negate @2))
6296   (IFN_FNMS @0 @1 @2))
6297  (simplify
6298   (IFN_FNMA:c (negate @0) @1 (negate @2))
6299   (IFN_FMS @0 @1 @2))
6300  (simplify
6301   (negate (IFN_FNMA@3 @0 @1 @2))
6302   (if (single_use (@3))
6303    (IFN_FMS @0 @1 @2)))
6305  (simplify
6306   (IFN_FNMS:c (negate @0) @1 @2)
6307   (IFN_FMS @0 @1 @2))
6308  (simplify
6309   (IFN_FNMS @0 @1 (negate @2))
6310   (IFN_FNMA @0 @1 @2))
6311  (simplify
6312   (IFN_FNMS:c (negate @0) @1 (negate @2))
6313   (IFN_FMA @0 @1 @2))
6314  (simplify
6315   (negate (IFN_FNMS@3 @0 @1 @2))
6316   (if (single_use (@3))
6317    (IFN_FMA @0 @1 @2))))
6319 /* CLZ simplifications.  */
6320 (for clz (CLZ)
6321  (for op (eq ne)
6322       cmp (lt ge)
6323   (simplify
6324    (op (clz:s@2 @0) INTEGER_CST@1)
6325    (if (integer_zerop (@1) && single_use (@2))
6326     /* clz(X) == 0 is (int)X < 0 and clz(X) != 0 is (int)X >= 0.  */
6327     (with { tree stype = signed_type_for (TREE_TYPE (@0));
6328             HOST_WIDE_INT val = 0;
6329 #ifdef CLZ_DEFINED_VALUE_AT_ZERO
6330             /* Punt on hypothetical weird targets.  */
6331             if (CLZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (TREE_TYPE (@0)),
6332                                            val) == 2
6333                 && val == 0)
6334               stype = NULL_TREE;
6335 #endif
6336           }
6337      (if (stype)
6338       (cmp (convert:stype @0) { build_zero_cst (stype); })))
6339     /* clz(X) == (prec-1) is X == 1 and clz(X) != (prec-1) is X != 1.  */
6340     (with { bool ok = true;
6341 #ifdef CLZ_DEFINED_VALUE_AT_ZERO
6342             /* Punt on hypothetical weird targets.  */
6343             if (CLZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (TREE_TYPE (@0)),
6344                                            val) == 2
6345                 && val == TYPE_PRECISION (TREE_TYPE (@0)) - 1)
6346               ok = false;
6347 #endif
6348           }
6349      (if (ok && wi::to_wide (@1) == (TYPE_PRECISION (TREE_TYPE (@0)) - 1))
6350       (op @0 { build_one_cst (TREE_TYPE (@0)); })))))))
6352 /* POPCOUNT simplifications.  */
6353 /* popcount(X) + popcount(Y) is popcount(X|Y) when X&Y must be zero.  */
6354 (simplify
6355   (plus (POPCOUNT:s @0) (POPCOUNT:s @1))
6356   (if (wi::bit_and (tree_nonzero_bits (@0), tree_nonzero_bits (@1)) == 0)
6357     (POPCOUNT (bit_ior @0 @1))))
6359 /* popcount(X) == 0 is X == 0, and related (in)equalities.  */
6360 (for popcount (POPCOUNT)
6361   (for cmp (le eq ne gt)
6362        rep (eq eq ne ne)
6363     (simplify
6364       (cmp (popcount @0) integer_zerop)
6365       (rep @0 { build_zero_cst (TREE_TYPE (@0)); }))))
6367 /* Canonicalize POPCOUNT(x)&1 as PARITY(X).  */
6368 (simplify
6369   (bit_and (POPCOUNT @0) integer_onep)
6370   (PARITY @0))
6372 /* PARITY simplifications.  */
6373 /* parity(~X) is parity(X).  */
6374 (simplify
6375   (PARITY (bit_not @0))
6376   (PARITY @0))
6378 /* parity(X)^parity(Y) is parity(X^Y).  */
6379 (simplify
6380   (bit_xor (PARITY:s @0) (PARITY:s @1))
6381   (PARITY (bit_xor @0 @1)))
6383 /* Common POPCOUNT/PARITY simplifications.  */
6384 /* popcount(X&C1) is (X>>C2)&1 when C1 == 1<<C2.  Same for parity(X&C1).  */
6385 (for pfun (POPCOUNT PARITY)
6386   (simplify
6387     (pfun @0)
6388     (with { wide_int nz = tree_nonzero_bits (@0); }
6389       (switch
6390         (if (nz == 1)
6391           (convert @0))
6392         (if (wi::popcount (nz) == 1)
6393           (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
6394             (convert (rshift:utype (convert:utype @0)
6395                                    { build_int_cst (integer_type_node,
6396                                                     wi::ctz (nz)); }))))))))
6398 #if GIMPLE
6399 /* 64- and 32-bits branchless implementations of popcount are detected:
6401    int popcount64c (uint64_t x)
6402    {
6403      x -= (x >> 1) & 0x5555555555555555ULL;
6404      x = (x & 0x3333333333333333ULL) + ((x >> 2) & 0x3333333333333333ULL);
6405      x = (x + (x >> 4)) & 0x0f0f0f0f0f0f0f0fULL;
6406      return (x * 0x0101010101010101ULL) >> 56;
6407    }
6409    int popcount32c (uint32_t x)
6410    {
6411      x -= (x >> 1) & 0x55555555;
6412      x = (x & 0x33333333) + ((x >> 2) & 0x33333333);
6413      x = (x + (x >> 4)) & 0x0f0f0f0f;
6414      return (x * 0x01010101) >> 24;
6415    }  */
6416 (simplify
6417  (rshift
6418   (mult
6419    (bit_and
6420     (plus:c
6421      (rshift @8 INTEGER_CST@5)
6422       (plus:c@8
6423        (bit_and @6 INTEGER_CST@7)
6424         (bit_and
6425          (rshift
6426           (minus@6 @0
6427            (bit_and (rshift @0 INTEGER_CST@4) INTEGER_CST@11))
6428           INTEGER_CST@10)
6429          INTEGER_CST@9)))
6430     INTEGER_CST@3)
6431    INTEGER_CST@2)
6432   INTEGER_CST@1)
6433   /* Check constants and optab.  */
6434   (with { unsigned prec = TYPE_PRECISION (type);
6435           int shift = (64 - prec) & 63;
6436           unsigned HOST_WIDE_INT c1
6437             = HOST_WIDE_INT_UC (0x0101010101010101) >> shift;
6438           unsigned HOST_WIDE_INT c2
6439             = HOST_WIDE_INT_UC (0x0F0F0F0F0F0F0F0F) >> shift;
6440           unsigned HOST_WIDE_INT c3
6441             = HOST_WIDE_INT_UC (0x3333333333333333) >> shift;
6442           unsigned HOST_WIDE_INT c4
6443             = HOST_WIDE_INT_UC (0x5555555555555555) >> shift;
6444    }
6445    (if (prec >= 16
6446         && prec <= 64
6447         && pow2p_hwi (prec)
6448         && TYPE_UNSIGNED (type)
6449         && integer_onep (@4)
6450         && wi::to_widest (@10) == 2
6451         && wi::to_widest (@5) == 4
6452         && wi::to_widest (@1) == prec - 8
6453         && tree_to_uhwi (@2) == c1
6454         && tree_to_uhwi (@3) == c2
6455         && tree_to_uhwi (@9) == c3
6456         && tree_to_uhwi (@7) == c3
6457         && tree_to_uhwi (@11) == c4
6458         && direct_internal_fn_supported_p (IFN_POPCOUNT, type,
6459                                            OPTIMIZE_FOR_BOTH))
6460     (convert (IFN_POPCOUNT:type @0)))))
6462 /* __builtin_ffs needs to deal on many targets with the possible zero
6463    argument.  If we know the argument is always non-zero, __builtin_ctz + 1
6464    should lead to better code.  */
6465 (simplify
6466  (FFS tree_expr_nonzero_p@0)
6467  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6468       && direct_internal_fn_supported_p (IFN_CTZ, TREE_TYPE (@0),
6469                                          OPTIMIZE_FOR_SPEED))
6470   (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
6471    (plus (CTZ:type (convert:utype @0)) { build_one_cst (type); }))))
6472 #endif
6474 (for ffs (BUILT_IN_FFS BUILT_IN_FFSL BUILT_IN_FFSLL
6475           BUILT_IN_FFSIMAX)
6476  /* __builtin_ffs (X) == 0 -> X == 0.
6477     __builtin_ffs (X) == 6 -> (X & 63) == 32.  */
6478  (for cmp (eq ne)
6479   (simplify
6480    (cmp (ffs@2 @0) INTEGER_CST@1)
6481     (with { int prec = TYPE_PRECISION (TREE_TYPE (@0)); }
6482      (switch
6483       (if (integer_zerop (@1))
6484        (cmp @0 { build_zero_cst (TREE_TYPE (@0)); }))
6485       (if (tree_int_cst_sgn (@1) < 0 || wi::to_widest (@1) > prec)
6486        { constant_boolean_node (cmp == NE_EXPR ? true : false, type); })
6487       (if (single_use (@2))
6488        (cmp (bit_and @0 { wide_int_to_tree (TREE_TYPE (@0),
6489                                             wi::mask (tree_to_uhwi (@1),
6490                                                       false, prec)); })
6491             { wide_int_to_tree (TREE_TYPE (@0),
6492                                 wi::shifted_mask (tree_to_uhwi (@1) - 1, 1,
6493                                                   false, prec)); }))))))
6495  /* __builtin_ffs (X) > 6 -> X != 0 && (X & 63) == 0.  */
6496  (for cmp (gt le)
6497       cmp2 (ne eq)
6498       cmp3 (eq ne)
6499       bit_op (bit_and bit_ior)
6500   (simplify
6501    (cmp (ffs@2 @0) INTEGER_CST@1)
6502     (with { int prec = TYPE_PRECISION (TREE_TYPE (@0)); }
6503      (switch
6504       (if (integer_zerop (@1))
6505        (cmp2 @0 { build_zero_cst (TREE_TYPE (@0)); }))
6506       (if (tree_int_cst_sgn (@1) < 0)
6507        { constant_boolean_node (cmp == GT_EXPR ? true : false, type); })
6508       (if (wi::to_widest (@1) >= prec)
6509        { constant_boolean_node (cmp == GT_EXPR ? false : true, type); })
6510       (if (wi::to_widest (@1) == prec - 1)
6511        (cmp3 @0 { wide_int_to_tree (TREE_TYPE (@0),
6512                                     wi::shifted_mask (prec - 1, 1,
6513                                                       false, prec)); }))
6514       (if (single_use (@2))
6515        (bit_op (cmp2 @0 { build_zero_cst (TREE_TYPE (@0)); })
6516                (cmp3 (bit_and @0
6517                               { wide_int_to_tree (TREE_TYPE (@0),
6518                                                   wi::mask (tree_to_uhwi (@1),
6519                                                   false, prec)); })
6520                      { build_zero_cst (TREE_TYPE (@0)); }))))))))
6522 /* Simplify:
6524      a = a1 op a2
6525      r = c ? a : b;
6527    to:
6529      r = c ? a1 op a2 : b;
6531    if the target can do it in one go.  This makes the operation conditional
6532    on c, so could drop potentially-trapping arithmetic, but that's a valid
6533    simplification if the result of the operation isn't needed.
6535    Avoid speculatively generating a stand-alone vector comparison
6536    on targets that might not support them.  Any target implementing
6537    conditional internal functions must support the same comparisons
6538    inside and outside a VEC_COND_EXPR.  */
6540 #if GIMPLE
6541 (for uncond_op (UNCOND_BINARY)
6542      cond_op (COND_BINARY)
6543  (simplify
6544   (vec_cond @0 (view_convert? (uncond_op@4 @1 @2)) @3)
6545   (with { tree op_type = TREE_TYPE (@4); }
6546    (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
6547         && element_precision (type) == element_precision (op_type))
6548     (view_convert (cond_op @0 @1 @2 (view_convert:op_type @3))))))
6549  (simplify
6550   (vec_cond @0 @1 (view_convert? (uncond_op@4 @2 @3)))
6551   (with { tree op_type = TREE_TYPE (@4); }
6552    (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
6553         && element_precision (type) == element_precision (op_type))
6554     (view_convert (cond_op (bit_not @0) @2 @3 (view_convert:op_type @1)))))))
6556 /* Same for ternary operations.  */
6557 (for uncond_op (UNCOND_TERNARY)
6558      cond_op (COND_TERNARY)
6559  (simplify
6560   (vec_cond @0 (view_convert? (uncond_op@5 @1 @2 @3)) @4)
6561   (with { tree op_type = TREE_TYPE (@5); }
6562    (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
6563         && element_precision (type) == element_precision (op_type))
6564     (view_convert (cond_op @0 @1 @2 @3 (view_convert:op_type @4))))))
6565  (simplify
6566   (vec_cond @0 @1 (view_convert? (uncond_op@5 @2 @3 @4)))
6567   (with { tree op_type = TREE_TYPE (@5); }
6568    (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
6569         && element_precision (type) == element_precision (op_type))
6570     (view_convert (cond_op (bit_not @0) @2 @3 @4
6571                   (view_convert:op_type @1)))))))
6572 #endif
6574 /* Detect cases in which a VEC_COND_EXPR effectively replaces the
6575    "else" value of an IFN_COND_*.  */
6576 (for cond_op (COND_BINARY)
6577  (simplify
6578   (vec_cond @0 (view_convert? (cond_op @0 @1 @2 @3)) @4)
6579   (with { tree op_type = TREE_TYPE (@3); }
6580    (if (element_precision (type) == element_precision (op_type))
6581     (view_convert (cond_op @0 @1 @2 (view_convert:op_type @4))))))
6582  (simplify
6583   (vec_cond @0 @1 (view_convert? (cond_op @2 @3 @4 @5)))
6584   (with { tree op_type = TREE_TYPE (@5); }
6585    (if (inverse_conditions_p (@0, @2)
6586         && element_precision (type) == element_precision (op_type))
6587     (view_convert (cond_op @2 @3 @4 (view_convert:op_type @1)))))))
6589 /* Same for ternary operations.  */
6590 (for cond_op (COND_TERNARY)
6591  (simplify
6592   (vec_cond @0 (view_convert? (cond_op @0 @1 @2 @3 @4)) @5)
6593   (with { tree op_type = TREE_TYPE (@4); }
6594    (if (element_precision (type) == element_precision (op_type))
6595     (view_convert (cond_op @0 @1 @2 @3 (view_convert:op_type @5))))))
6596  (simplify
6597   (vec_cond @0 @1 (view_convert? (cond_op @2 @3 @4 @5 @6)))
6598   (with { tree op_type = TREE_TYPE (@6); }
6599    (if (inverse_conditions_p (@0, @2)
6600         && element_precision (type) == element_precision (op_type))
6601     (view_convert (cond_op @2 @3 @4 @5 (view_convert:op_type @1)))))))
6603 /* For pointers @0 and @2 and nonnegative constant offset @1, look for
6604    expressions like:
6606    A: (@0 + @1 < @2) | (@2 + @1 < @0)
6607    B: (@0 + @1 <= @2) | (@2 + @1 <= @0)
6609    If pointers are known not to wrap, B checks whether @1 bytes starting
6610    at @0 and @2 do not overlap, while A tests the same thing for @1 + 1
6611    bytes.  A is more efficiently tested as:
6613    A: (sizetype) (@0 + @1 - @2) > @1 * 2
6615    The equivalent expression for B is given by replacing @1 with @1 - 1:
6617    B: (sizetype) (@0 + (@1 - 1) - @2) > (@1 - 1) * 2
6619    @0 and @2 can be swapped in both expressions without changing the result.
6621    The folds rely on sizetype's being unsigned (which is always true)
6622    and on its being the same width as the pointer (which we have to check).
6624    The fold replaces two pointer_plus expressions, two comparisons and
6625    an IOR with a pointer_plus, a pointer_diff, and a comparison, so in
6626    the best case it's a saving of two operations.  The A fold retains one
6627    of the original pointer_pluses, so is a win even if both pointer_pluses
6628    are used elsewhere.  The B fold is a wash if both pointer_pluses are
6629    used elsewhere, since all we end up doing is replacing a comparison with
6630    a pointer_plus.  We do still apply the fold under those circumstances
6631    though, in case applying it to other conditions eventually makes one of the
6632    pointer_pluses dead.  */
6633 (for ior (truth_orif truth_or bit_ior)
6634  (for cmp (le lt)
6635   (simplify
6636    (ior (cmp:cs (pointer_plus@3 @0 INTEGER_CST@1) @2)
6637         (cmp:cs (pointer_plus@4 @2 @1) @0))
6638    (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
6639         && TYPE_OVERFLOW_WRAPS (sizetype)
6640         && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (sizetype))
6641     /* Calculate the rhs constant.  */
6642     (with { offset_int off = wi::to_offset (@1) - (cmp == LE_EXPR ? 1 : 0);
6643             offset_int rhs = off * 2; }
6644      /* Always fails for negative values.  */
6645      (if (wi::min_precision (rhs, UNSIGNED) <= TYPE_PRECISION (sizetype))
6646       /* Since the order of @0 and @2 doesn't matter, let tree_swap_operands_p
6647          pick a canonical order.  This increases the chances of using the
6648          same pointer_plus in multiple checks.  */
6649       (with { bool swap_p = tree_swap_operands_p (@0, @2);
6650               tree rhs_tree = wide_int_to_tree (sizetype, rhs); }
6651        (if (cmp == LT_EXPR)
6652         (gt (convert:sizetype
6653              (pointer_diff:ssizetype { swap_p ? @4 : @3; }
6654                                      { swap_p ? @0 : @2; }))
6655             { rhs_tree; })
6656         (gt (convert:sizetype
6657              (pointer_diff:ssizetype
6658               (pointer_plus { swap_p ? @2 : @0; }
6659                             { wide_int_to_tree (sizetype, off); })
6660               { swap_p ? @0 : @2; }))
6661             { rhs_tree; })))))))))
6663 /* Fold REDUC (@0 & @1) -> @0[I] & @1[I] if element I is the only nonzero
6664    element of @1.  */
6665 (for reduc (IFN_REDUC_PLUS IFN_REDUC_IOR IFN_REDUC_XOR)
6666  (simplify (reduc (view_convert? (bit_and @0 VECTOR_CST@1)))
6667   (with { int i = single_nonzero_element (@1); }
6668    (if (i >= 0)
6669     (with { tree elt = vector_cst_elt (@1, i);
6670             tree elt_type = TREE_TYPE (elt);
6671             unsigned int elt_bits = tree_to_uhwi (TYPE_SIZE (elt_type));
6672             tree size = bitsize_int (elt_bits);
6673             tree pos = bitsize_int (elt_bits * i); }
6674      (view_convert
6675       (bit_and:elt_type
6676        (BIT_FIELD_REF:elt_type @0 { size; } { pos; })
6677        { elt; })))))))
6679 (simplify
6680  (vec_perm @0 @1 VECTOR_CST@2)
6681  (with
6682   {
6683     tree op0 = @0, op1 = @1, op2 = @2;
6685     /* Build a vector of integers from the tree mask.  */
6686     vec_perm_builder builder;
6687     if (!tree_to_vec_perm_builder (&builder, op2))
6688       return NULL_TREE;
6690     /* Create a vec_perm_indices for the integer vector.  */
6691     poly_uint64 nelts = TYPE_VECTOR_SUBPARTS (type);
6692     bool single_arg = (op0 == op1);
6693     vec_perm_indices sel (builder, single_arg ? 1 : 2, nelts);
6694   }
6695   (if (sel.series_p (0, 1, 0, 1))
6696    { op0; }
6697    (if (sel.series_p (0, 1, nelts, 1))
6698     { op1; }
6699     (with
6700      {
6701        if (!single_arg)
6702          {
6703            if (sel.all_from_input_p (0))
6704              op1 = op0;
6705            else if (sel.all_from_input_p (1))
6706              {
6707                op0 = op1;
6708                sel.rotate_inputs (1);
6709              }
6710            else if (known_ge (poly_uint64 (sel[0]), nelts))
6711              {
6712                std::swap (op0, op1);
6713                sel.rotate_inputs (1);
6714              }
6715          }
6716        gassign *def;
6717        tree cop0 = op0, cop1 = op1;
6718        if (TREE_CODE (op0) == SSA_NAME
6719            && (def = dyn_cast <gassign *> (SSA_NAME_DEF_STMT (op0)))
6720            && gimple_assign_rhs_code (def) == CONSTRUCTOR)
6721          cop0 = gimple_assign_rhs1 (def);
6722        if (TREE_CODE (op1) == SSA_NAME
6723            && (def = dyn_cast <gassign *> (SSA_NAME_DEF_STMT (op1)))
6724            && gimple_assign_rhs_code (def) == CONSTRUCTOR)
6725          cop1 = gimple_assign_rhs1 (def);
6727        tree t;
6728     }
6729     (if ((TREE_CODE (cop0) == VECTOR_CST
6730           || TREE_CODE (cop0) == CONSTRUCTOR)
6731          && (TREE_CODE (cop1) == VECTOR_CST
6732              || TREE_CODE (cop1) == CONSTRUCTOR)
6733          && (t = fold_vec_perm (type, cop0, cop1, sel)))
6734      { t; }
6735      (with
6736       {
6737         bool changed = (op0 == op1 && !single_arg);
6738         tree ins = NULL_TREE;
6739         unsigned at = 0;
6741         /* See if the permutation is performing a single element
6742            insert from a CONSTRUCTOR or constant and use a BIT_INSERT_EXPR
6743            in that case.  But only if the vector mode is supported,
6744            otherwise this is invalid GIMPLE.  */
6745         if (TYPE_MODE (type) != BLKmode
6746             && (TREE_CODE (cop0) == VECTOR_CST
6747                 || TREE_CODE (cop0) == CONSTRUCTOR
6748                 || TREE_CODE (cop1) == VECTOR_CST
6749                 || TREE_CODE (cop1) == CONSTRUCTOR))
6750           {
6751             bool insert_first_p = sel.series_p (1, 1, nelts + 1, 1);
6752             if (insert_first_p)
6753               {
6754                 /* After canonicalizing the first elt to come from the
6755                    first vector we only can insert the first elt from
6756                    the first vector.  */
6757                 at = 0;
6758                 if ((ins = fold_read_from_vector (cop0, sel[0])))
6759                   op0 = op1;
6760               }
6761             /* The above can fail for two-element vectors which always
6762                appear to insert the first element, so try inserting
6763                into the second lane as well.  For more than two
6764                elements that's wasted time.  */
6765             if (!insert_first_p || (!ins && maybe_eq (nelts, 2u)))
6766               {
6767                 unsigned int encoded_nelts = sel.encoding ().encoded_nelts ();
6768                 for (at = 0; at < encoded_nelts; ++at)
6769                   if (maybe_ne (sel[at], at))
6770                     break;
6771                 if (at < encoded_nelts
6772                     && (known_eq (at + 1, nelts)
6773                         || sel.series_p (at + 1, 1, at + 1, 1)))
6774                   {
6775                     if (known_lt (poly_uint64 (sel[at]), nelts))
6776                       ins = fold_read_from_vector (cop0, sel[at]);
6777                     else
6778                       ins = fold_read_from_vector (cop1, sel[at] - nelts);
6779                   }
6780               }
6781           }
6783         /* Generate a canonical form of the selector.  */
6784         if (!ins && sel.encoding () != builder)
6785           {
6786             /* Some targets are deficient and fail to expand a single
6787                argument permutation while still allowing an equivalent
6788                2-argument version.  */
6789             tree oldop2 = op2;
6790             if (sel.ninputs () == 2
6791                || can_vec_perm_const_p (TYPE_MODE (type), sel, false))
6792               op2 = vec_perm_indices_to_tree (TREE_TYPE (op2), sel);
6793             else
6794               {
6795                 vec_perm_indices sel2 (builder, 2, nelts);
6796                 if (can_vec_perm_const_p (TYPE_MODE (type), sel2, false))
6797                   op2 = vec_perm_indices_to_tree (TREE_TYPE (op2), sel2);
6798                 else
6799                   /* Not directly supported with either encoding,
6800                      so use the preferred form.  */
6801                   op2 = vec_perm_indices_to_tree (TREE_TYPE (op2), sel);
6802               }
6803             if (!operand_equal_p (op2, oldop2, 0))
6804               changed = true;
6805           }
6806       }
6807       (if (ins)
6808        (bit_insert { op0; } { ins; }
6809          { bitsize_int (at * vector_element_bits (type)); })
6810        (if (changed)
6811         (vec_perm { op0; } { op1; } { op2; }))))))))))
6813 /* VEC_PERM_EXPR (v, v, mask) -> v where v contains same element.  */
6815 (match vec_same_elem_p
6816  @0
6817  (if (uniform_vector_p (@0))))
6819 (match vec_same_elem_p
6820  (vec_duplicate @0))
6822 (simplify
6823  (vec_perm vec_same_elem_p@0 @0 @1)
6824  @0)
6826 /* Match count trailing zeroes for simplify_count_trailing_zeroes in fwprop.
6827    The canonical form is array[((x & -x) * C) >> SHIFT] where C is a magic
6828    constant which when multiplied by a power of 2 contains a unique value
6829    in the top 5 or 6 bits.  This is then indexed into a table which maps it
6830    to the number of trailing zeroes.  */
6831 (match (ctz_table_index @1 @2 @3)
6832   (rshift (mult (bit_and:c (negate @1) @1) INTEGER_CST@2) INTEGER_CST@3))