Add mi_thunk support for vcalls on hppa.
[official-gcc.git] / gcc / match.pd
blob760f773cf1b13794094d11543ffa07c55769936d
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 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    (with { tree t = type;
2793            if (!TYPE_OVERFLOW_WRAPS (t)) t = unsigned_type_for (t);
2794            wide_int w = wi::set_bit_in_zero (tree_to_uhwi (@1),
2795                                              element_precision (type));
2796            w += 1;
2797            tree cst = wide_int_to_tree (VECTOR_TYPE_P (t) ? TREE_TYPE (t)
2798                                         : t, w);
2799            cst = build_uniform_cst (t, cst); }
2800     (convert (mult (convert:t @0) { cst; })))))
2801 (simplify
2802  (plus (lshift:s @0 INTEGER_CST@1) (lshift:s @0 INTEGER_CST@2))
2803   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2804        && tree_fits_uhwi_p (@1)
2805        && tree_to_uhwi (@1) < element_precision (type)
2806        && tree_fits_uhwi_p (@2)
2807        && tree_to_uhwi (@2) < element_precision (type))
2808    (with { tree t = type;
2809            if (!TYPE_OVERFLOW_WRAPS (t)) t = unsigned_type_for (t);
2810            unsigned int prec = element_precision (type);
2811            wide_int w = wi::set_bit_in_zero (tree_to_uhwi (@1), prec);
2812            w += wi::set_bit_in_zero (tree_to_uhwi (@2), prec);
2813            tree cst = wide_int_to_tree (VECTOR_TYPE_P (t) ? TREE_TYPE (t)
2814                                         : t, w);
2815            cst = build_uniform_cst (t, cst); }
2816     (convert (mult (convert:t @0) { cst; })))))
2817 #endif
2819 /* Simplifications of MIN_EXPR, MAX_EXPR, fmin() and fmax().  */
2821 (for minmax (min max FMIN_ALL FMAX_ALL)
2822  (simplify
2823   (minmax @0 @0)
2824   @0))
2825 /* min(max(x,y),y) -> y.  */
2826 (simplify
2827  (min:c (max:c @0 @1) @1)
2828  @1)
2829 /* max(min(x,y),y) -> y.  */
2830 (simplify
2831  (max:c (min:c @0 @1) @1)
2832  @1)
2833 /* max(a,-a) -> abs(a).  */
2834 (simplify
2835  (max:c @0 (negate @0))
2836  (if (TREE_CODE (type) != COMPLEX_TYPE
2837       && (! ANY_INTEGRAL_TYPE_P (type)
2838           || TYPE_OVERFLOW_UNDEFINED (type)))
2839   (abs @0)))
2840 /* min(a,-a) -> -abs(a).  */
2841 (simplify
2842  (min:c @0 (negate @0))
2843  (if (TREE_CODE (type) != COMPLEX_TYPE
2844       && (! ANY_INTEGRAL_TYPE_P (type)
2845           || TYPE_OVERFLOW_UNDEFINED (type)))
2846   (negate (abs @0))))
2847 (simplify
2848  (min @0 @1)
2849  (switch
2850   (if (INTEGRAL_TYPE_P (type)
2851        && TYPE_MIN_VALUE (type)
2852        && operand_equal_p (@1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
2853    @1)
2854   (if (INTEGRAL_TYPE_P (type)
2855        && TYPE_MAX_VALUE (type)
2856        && operand_equal_p (@1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
2857    @0)))
2858 (simplify
2859  (max @0 @1)
2860  (switch
2861   (if (INTEGRAL_TYPE_P (type)
2862        && TYPE_MAX_VALUE (type)
2863        && operand_equal_p (@1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
2864    @1)
2865   (if (INTEGRAL_TYPE_P (type)
2866        && TYPE_MIN_VALUE (type)
2867        && operand_equal_p (@1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
2868    @0)))
2870 /* max (a, a + CST) -> a + CST where CST is positive.  */
2871 /* max (a, a + CST) -> a where CST is negative.  */
2872 (simplify
2873  (max:c @0 (plus@2 @0 INTEGER_CST@1))
2874   (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
2875    (if (tree_int_cst_sgn (@1) > 0)
2876     @2
2877     @0)))
2879 /* min (a, a + CST) -> a where CST is positive.  */
2880 /* min (a, a + CST) -> a + CST where CST is negative. */
2881 (simplify
2882  (min:c @0 (plus@2 @0 INTEGER_CST@1))
2883   (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
2884    (if (tree_int_cst_sgn (@1) > 0)
2885     @0
2886     @2)))
2888 /* (convert (minmax ((convert (x) c)))) -> minmax (x c) if x is promoted
2889    and the outer convert demotes the expression back to x's type.  */
2890 (for minmax (min max)
2891  (simplify
2892   (convert (minmax@0 (convert @1) INTEGER_CST@2))
2893   (if (INTEGRAL_TYPE_P (type)
2894        && types_match (@1, type) && int_fits_type_p (@2, type)
2895        && TYPE_SIGN (TREE_TYPE (@0)) == TYPE_SIGN (type)
2896        && TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type))
2897    (minmax @1 (convert @2)))))
2899 (for minmax (FMIN_ALL FMAX_ALL)
2900  /* If either argument is NaN, return the other one.  Avoid the
2901     transformation if we get (and honor) a signalling NaN.  */
2902  (simplify
2903   (minmax:c @0 REAL_CST@1)
2904   (if (real_isnan (TREE_REAL_CST_PTR (@1))
2905        && (!HONOR_SNANS (@1) || !TREE_REAL_CST (@1).signalling))
2906    @0)))
2907 /* Convert fmin/fmax to MIN_EXPR/MAX_EXPR.  C99 requires these
2908    functions to return the numeric arg if the other one is NaN.
2909    MIN and MAX don't honor that, so only transform if -ffinite-math-only
2910    is set.  C99 doesn't require -0.0 to be handled, so we don't have to
2911    worry about it either.  */
2912 (if (flag_finite_math_only)
2913  (simplify
2914   (FMIN_ALL @0 @1)
2915   (min @0 @1))
2916  (simplify
2917   (FMAX_ALL @0 @1)
2918   (max @0 @1)))
2919 /* min (-A, -B) -> -max (A, B)  */
2920 (for minmax (min max FMIN_ALL FMAX_ALL)
2921      maxmin (max min FMAX_ALL FMIN_ALL)
2922  (simplify
2923   (minmax (negate:s@2 @0) (negate:s@3 @1))
2924   (if (FLOAT_TYPE_P (TREE_TYPE (@0))
2925        || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2926            && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
2927    (negate (maxmin @0 @1)))))
2928 /* MIN (~X, ~Y) -> ~MAX (X, Y)
2929    MAX (~X, ~Y) -> ~MIN (X, Y)  */
2930 (for minmax (min max)
2931  maxmin (max min)
2932  (simplify
2933   (minmax (bit_not:s@2 @0) (bit_not:s@3 @1))
2934   (bit_not (maxmin @0 @1))))
2936 /* MIN (X, Y) == X -> X <= Y  */
2937 (for minmax (min min max max)
2938      cmp    (eq  ne  eq  ne )
2939      out    (le  gt  ge  lt )
2940  (simplify
2941   (cmp:c (minmax:c @0 @1) @0)
2942   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0)))
2943    (out @0 @1))))
2944 /* MIN (X, 5) == 0 -> X == 0
2945    MIN (X, 5) == 7 -> false  */
2946 (for cmp (eq ne)
2947  (simplify
2948   (cmp (min @0 INTEGER_CST@1) INTEGER_CST@2)
2949   (if (wi::lt_p (wi::to_wide (@1), wi::to_wide (@2),
2950                  TYPE_SIGN (TREE_TYPE (@0))))
2951    { constant_boolean_node (cmp == NE_EXPR, type); }
2952    (if (wi::gt_p (wi::to_wide (@1), wi::to_wide (@2),
2953                   TYPE_SIGN (TREE_TYPE (@0))))
2954     (cmp @0 @2)))))
2955 (for cmp (eq ne)
2956  (simplify
2957   (cmp (max @0 INTEGER_CST@1) INTEGER_CST@2)
2958   (if (wi::gt_p (wi::to_wide (@1), wi::to_wide (@2),
2959                  TYPE_SIGN (TREE_TYPE (@0))))
2960    { constant_boolean_node (cmp == NE_EXPR, type); }
2961    (if (wi::lt_p (wi::to_wide (@1), wi::to_wide (@2),
2962                   TYPE_SIGN (TREE_TYPE (@0))))
2963     (cmp @0 @2)))))
2964 /* MIN (X, C1) < C2 -> X < C2 || C1 < C2  */
2965 (for minmax (min     min     max     max     min     min     max     max    )
2966      cmp    (lt      le      gt      ge      gt      ge      lt      le     )
2967      comb   (bit_ior bit_ior bit_ior bit_ior bit_and bit_and bit_and bit_and)
2968  (simplify
2969   (cmp (minmax @0 INTEGER_CST@1) INTEGER_CST@2)
2970   (comb (cmp @0 @2) (cmp @1 @2))))
2972 /* X <= MAX(X, Y) -> true
2973    X > MAX(X, Y) -> false 
2974    X >= MIN(X, Y) -> true
2975    X < MIN(X, Y) -> false */
2976 (for minmax (min     min     max     max     )
2977      cmp    (ge      lt      le      gt      )
2978  (simplify
2979   (cmp @0 (minmax:c @0 @1))
2980   { constant_boolean_node (cmp == GE_EXPR || cmp == LE_EXPR, type); } ))
2982 /* Undo fancy way of writing max/min or other ?: expressions,
2983    like a - ((a - b) & -(a < b)), in this case into (a < b) ? b : a.
2984    People normally use ?: and that is what we actually try to optimize.  */
2985 (for cmp (simple_comparison)
2986  (simplify
2987   (minus @0 (bit_and:c (minus @0 @1)
2988                        (convert? (negate@4 (convert? (cmp@5 @2 @3))))))
2989   (if (INTEGRAL_TYPE_P (type)
2990        && INTEGRAL_TYPE_P (TREE_TYPE (@4))
2991        && TREE_CODE (TREE_TYPE (@4)) != BOOLEAN_TYPE
2992        && INTEGRAL_TYPE_P (TREE_TYPE (@5))
2993        && (TYPE_PRECISION (TREE_TYPE (@4)) >= TYPE_PRECISION (type)
2994            || !TYPE_UNSIGNED (TREE_TYPE (@4)))
2995        && (GIMPLE || !TREE_SIDE_EFFECTS (@1)))
2996    (cond (cmp @2 @3) @1 @0)))
2997  (simplify
2998   (plus:c @0 (bit_and:c (minus @1 @0)
2999                         (convert? (negate@4 (convert? (cmp@5 @2 @3))))))
3000   (if (INTEGRAL_TYPE_P (type)
3001        && INTEGRAL_TYPE_P (TREE_TYPE (@4))
3002        && TREE_CODE (TREE_TYPE (@4)) != BOOLEAN_TYPE
3003        && INTEGRAL_TYPE_P (TREE_TYPE (@5))
3004        && (TYPE_PRECISION (TREE_TYPE (@4)) >= TYPE_PRECISION (type)
3005            || !TYPE_UNSIGNED (TREE_TYPE (@4)))
3006        && (GIMPLE || !TREE_SIDE_EFFECTS (@1)))
3007    (cond (cmp @2 @3) @1 @0)))
3008  /* Similarly with ^ instead of - though in that case with :c.  */
3009  (simplify
3010   (bit_xor:c @0 (bit_and:c (bit_xor:c @0 @1)
3011                            (convert? (negate@4 (convert? (cmp@5 @2 @3))))))
3012   (if (INTEGRAL_TYPE_P (type)
3013        && INTEGRAL_TYPE_P (TREE_TYPE (@4))
3014        && TREE_CODE (TREE_TYPE (@4)) != BOOLEAN_TYPE
3015        && INTEGRAL_TYPE_P (TREE_TYPE (@5))
3016        && (TYPE_PRECISION (TREE_TYPE (@4)) >= TYPE_PRECISION (type)
3017            || !TYPE_UNSIGNED (TREE_TYPE (@4)))
3018        && (GIMPLE || !TREE_SIDE_EFFECTS (@1)))
3019    (cond (cmp @2 @3) @1 @0))))
3021 /* Simplifications of shift and rotates.  */
3023 (for rotate (lrotate rrotate)
3024  (simplify
3025   (rotate integer_all_onesp@0 @1)
3026   @0))
3028 /* Optimize -1 >> x for arithmetic right shifts.  */
3029 (simplify
3030  (rshift integer_all_onesp@0 @1)
3031  (if (!TYPE_UNSIGNED (type))
3032   @0))
3034 /* Optimize (x >> c) << c into x & (-1<<c).  */
3035 (simplify
3036  (lshift (nop_convert? (rshift @0 INTEGER_CST@1)) @1)
3037  (if (wi::ltu_p (wi::to_wide (@1), element_precision (type)))
3038   /* It doesn't matter if the right shift is arithmetic or logical.  */
3039   (bit_and (view_convert @0) (lshift { build_minus_one_cst (type); } @1))))
3041 (simplify
3042  (lshift (convert (convert@2 (rshift @0 INTEGER_CST@1))) @1)
3043  (if (wi::ltu_p (wi::to_wide (@1), element_precision (type))
3044       /* Allow intermediate conversion to integral type with whatever sign, as
3045          long as the low TYPE_PRECISION (type)
3046          - TYPE_PRECISION (TREE_TYPE (@2)) bits are preserved.  */
3047       && INTEGRAL_TYPE_P (type)
3048       && INTEGRAL_TYPE_P (TREE_TYPE (@2))
3049       && INTEGRAL_TYPE_P (TREE_TYPE (@0))
3050       && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (@0))
3051       && (TYPE_PRECISION (TREE_TYPE (@2)) >= TYPE_PRECISION (type)
3052           || wi::geu_p (wi::to_wide (@1),
3053                         TYPE_PRECISION (type)
3054                         - TYPE_PRECISION (TREE_TYPE (@2)))))
3055   (bit_and (convert @0) (lshift { build_minus_one_cst (type); } @1))))
3057 /* Optimize (x << c) >> c into x & ((unsigned)-1 >> c) for unsigned
3058    types.  */
3059 (simplify
3060  (rshift (lshift @0 INTEGER_CST@1) @1)
3061  (if (TYPE_UNSIGNED (type)
3062       && (wi::ltu_p (wi::to_wide (@1), element_precision (type))))
3063   (bit_and @0 (rshift { build_minus_one_cst (type); } @1))))
3065 /* Optimize x >> x into 0 */
3066 (simplify
3067  (rshift @0 @0)
3068   { build_zero_cst (type); })
3070 (for shiftrotate (lrotate rrotate lshift rshift)
3071  (simplify
3072   (shiftrotate @0 integer_zerop)
3073   (non_lvalue @0))
3074  (simplify
3075   (shiftrotate integer_zerop@0 @1)
3076   @0)
3077  /* Prefer vector1 << scalar to vector1 << vector2
3078     if vector2 is uniform.  */
3079  (for vec (VECTOR_CST CONSTRUCTOR)
3080   (simplify
3081    (shiftrotate @0 vec@1)
3082    (with { tree tem = uniform_vector_p (@1); }
3083     (if (tem)
3084      (shiftrotate @0 { tem; }))))))
3086 /* Simplify X << Y where Y's low width bits are 0 to X, as only valid
3087    Y is 0.  Similarly for X >> Y.  */
3088 #if GIMPLE
3089 (for shift (lshift rshift)
3090  (simplify
3091   (shift @0 SSA_NAME@1)
3092    (if (INTEGRAL_TYPE_P (TREE_TYPE (@1)))
3093     (with {
3094       int width = ceil_log2 (element_precision (TREE_TYPE (@0)));
3095       int prec = TYPE_PRECISION (TREE_TYPE (@1));
3096      }
3097      (if ((get_nonzero_bits (@1) & wi::mask (width, false, prec)) == 0)
3098       @0)))))
3099 #endif
3101 /* Rewrite an LROTATE_EXPR by a constant into an
3102    RROTATE_EXPR by a new constant.  */
3103 (simplify
3104  (lrotate @0 INTEGER_CST@1)
3105  (rrotate @0 { const_binop (MINUS_EXPR, TREE_TYPE (@1),
3106                             build_int_cst (TREE_TYPE (@1),
3107                                            element_precision (type)), @1); }))
3109 /* Turn (a OP c1) OP c2 into a OP (c1+c2).  */
3110 (for op (lrotate rrotate rshift lshift)
3111  (simplify
3112   (op (op @0 INTEGER_CST@1) INTEGER_CST@2)
3113   (with { unsigned int prec = element_precision (type); }
3114    (if (wi::ge_p (wi::to_wide (@1), 0, TYPE_SIGN (TREE_TYPE (@1)))
3115         && wi::lt_p (wi::to_wide (@1), prec, TYPE_SIGN (TREE_TYPE (@1)))
3116         && wi::ge_p (wi::to_wide (@2), 0, TYPE_SIGN (TREE_TYPE (@2)))
3117         && wi::lt_p (wi::to_wide (@2), prec, TYPE_SIGN (TREE_TYPE (@2))))
3118     (with { unsigned int low = (tree_to_uhwi (@1)
3119                                 + tree_to_uhwi (@2)); }
3120      /* Deal with a OP (c1 + c2) being undefined but (a OP c1) OP c2
3121         being well defined.  */
3122      (if (low >= prec)
3123       (if (op == LROTATE_EXPR || op == RROTATE_EXPR)
3124        (op @0 { build_int_cst (TREE_TYPE (@1), low % prec); })
3125        (if (TYPE_UNSIGNED (type) || op == LSHIFT_EXPR)
3126         { build_zero_cst (type); }
3127         (op @0 { build_int_cst (TREE_TYPE (@1), prec - 1); })))
3128       (op @0 { build_int_cst (TREE_TYPE (@1), low); })))))))
3131 /* Simplify (CST << x) & 1 to 0 if CST is even or to x == 0 if it is odd.  */
3132 (simplify
3133  (bit_and (lshift INTEGER_CST@1 @0) integer_onep)
3134   (if ((wi::to_wide (@1) & 1) != 0)
3135    (convert (eq:boolean_type_node @0 { build_zero_cst (TREE_TYPE (@0)); }))
3136    { build_zero_cst (type); }))
3138 /* Simplify ((C << x) & D) != 0 where C and D are power of two constants,
3139    either to false if D is smaller (unsigned comparison) than C, or to
3140    x == log2 (D) - log2 (C).  Similarly for right shifts.  */
3141 (for cmp (ne eq)
3142      icmp (eq ne)
3143  (simplify
3144   (cmp (bit_and (lshift integer_pow2p@1 @0) integer_pow2p@2) integer_zerop)
3145    (with { int c1 = wi::clz (wi::to_wide (@1));
3146            int c2 = wi::clz (wi::to_wide (@2)); }
3147     (if (c1 < c2)
3148      { constant_boolean_node (cmp == NE_EXPR ? false : true, type); }
3149      (icmp @0 { build_int_cst (TREE_TYPE (@0), c1 - c2); }))))
3150  (simplify
3151   (cmp (bit_and (rshift integer_pow2p@1 @0) integer_pow2p@2) integer_zerop)
3152    (if (tree_int_cst_sgn (@1) > 0)
3153     (with { int c1 = wi::clz (wi::to_wide (@1));
3154             int c2 = wi::clz (wi::to_wide (@2)); }
3155      (if (c1 > c2)
3156       { constant_boolean_node (cmp == NE_EXPR ? false : true, type); }
3157       (icmp @0 { build_int_cst (TREE_TYPE (@0), c2 - c1); }))))))
3159 /* (CST1 << A) == CST2 -> A == ctz (CST2) - ctz (CST1)
3160    (CST1 << A) != CST2 -> A != ctz (CST2) - ctz (CST1)
3161    if CST2 != 0.  */
3162 (for cmp (ne eq)
3163  (simplify
3164   (cmp (lshift INTEGER_CST@0 @1) INTEGER_CST@2)
3165   (with { int cand = wi::ctz (wi::to_wide (@2)) - wi::ctz (wi::to_wide (@0)); }
3166    (if (cand < 0
3167         || (!integer_zerop (@2)
3168             && wi::lshift (wi::to_wide (@0), cand) != wi::to_wide (@2)))
3169     { constant_boolean_node (cmp == NE_EXPR, type); }
3170     (if (!integer_zerop (@2)
3171          && wi::lshift (wi::to_wide (@0), cand) == wi::to_wide (@2))
3172      (cmp @1 { build_int_cst (TREE_TYPE (@1), cand); }))))))
3174 /* Fold (X << C1) & C2 into (X << C1) & (C2 | ((1 << C1) - 1))
3175         (X >> C1) & C2 into (X >> C1) & (C2 | ~((type) -1 >> C1))
3176    if the new mask might be further optimized.  */
3177 (for shift (lshift rshift)
3178  (simplify
3179   (bit_and (convert?:s@4 (shift:s@5 (convert1?@3 @0) INTEGER_CST@1))
3180            INTEGER_CST@2)
3181    (if (tree_nop_conversion_p (TREE_TYPE (@4), TREE_TYPE (@5))
3182         && TYPE_PRECISION (type) <= HOST_BITS_PER_WIDE_INT
3183         && tree_fits_uhwi_p (@1)
3184         && tree_to_uhwi (@1) > 0
3185         && tree_to_uhwi (@1) < TYPE_PRECISION (type))
3186     (with
3187      {
3188        unsigned int shiftc = tree_to_uhwi (@1);
3189        unsigned HOST_WIDE_INT mask = TREE_INT_CST_LOW (@2);
3190        unsigned HOST_WIDE_INT newmask, zerobits = 0;
3191        tree shift_type = TREE_TYPE (@3);
3192        unsigned int prec;
3194        if (shift == LSHIFT_EXPR)
3195          zerobits = ((HOST_WIDE_INT_1U << shiftc) - 1);
3196        else if (shift == RSHIFT_EXPR
3197                 && type_has_mode_precision_p (shift_type))
3198          {
3199            prec = TYPE_PRECISION (TREE_TYPE (@3));
3200            tree arg00 = @0;
3201            /* See if more bits can be proven as zero because of
3202               zero extension.  */
3203            if (@3 != @0
3204                && TYPE_UNSIGNED (TREE_TYPE (@0)))
3205              {
3206                tree inner_type = TREE_TYPE (@0);
3207                if (type_has_mode_precision_p (inner_type)
3208                    && TYPE_PRECISION (inner_type) < prec)
3209                  {
3210                    prec = TYPE_PRECISION (inner_type);
3211                    /* See if we can shorten the right shift.  */
3212                    if (shiftc < prec)
3213                      shift_type = inner_type;
3214                    /* Otherwise X >> C1 is all zeros, so we'll optimize
3215                       it into (X, 0) later on by making sure zerobits
3216                       is all ones.  */
3217                  }
3218              }
3219            zerobits = HOST_WIDE_INT_M1U;
3220            if (shiftc < prec)
3221              {
3222                zerobits >>= HOST_BITS_PER_WIDE_INT - shiftc;
3223                zerobits <<= prec - shiftc;
3224              }
3225            /* For arithmetic shift if sign bit could be set, zerobits
3226               can contain actually sign bits, so no transformation is
3227               possible, unless MASK masks them all away.  In that
3228               case the shift needs to be converted into logical shift.  */
3229            if (!TYPE_UNSIGNED (TREE_TYPE (@3))
3230                && prec == TYPE_PRECISION (TREE_TYPE (@3)))
3231              {
3232                if ((mask & zerobits) == 0)
3233                  shift_type = unsigned_type_for (TREE_TYPE (@3));
3234                else
3235                  zerobits = 0;
3236              }
3237          }
3238      }
3239      /* ((X << 16) & 0xff00) is (X, 0).  */
3240      (if ((mask & zerobits) == mask)
3241       { build_int_cst (type, 0); }
3242       (with { newmask = mask | zerobits; }
3243        (if (newmask != mask && (newmask & (newmask + 1)) == 0)
3244         (with
3245          {
3246            /* Only do the transformation if NEWMASK is some integer
3247               mode's mask.  */
3248            for (prec = BITS_PER_UNIT;
3249                 prec < HOST_BITS_PER_WIDE_INT; prec <<= 1)
3250              if (newmask == (HOST_WIDE_INT_1U << prec) - 1)
3251                break;
3252          }
3253          (if (prec < HOST_BITS_PER_WIDE_INT
3254               || newmask == HOST_WIDE_INT_M1U)
3255           (with
3256            { tree newmaskt = build_int_cst_type (TREE_TYPE (@2), newmask); }
3257            (if (!tree_int_cst_equal (newmaskt, @2))
3258             (if (shift_type != TREE_TYPE (@3))
3259              (bit_and (convert (shift:shift_type (convert @3) @1)) { newmaskt; })
3260              (bit_and @4 { newmaskt; })))))))))))))
3262 /* Fold (X {&,^,|} C2) << C1 into (X << C1) {&,^,|} (C2 << C1)
3263    (X {&,^,|} C2) >> C1 into (X >> C1) & (C2 >> C1).  */
3264 (for shift (lshift rshift)
3265  (for bit_op (bit_and bit_xor bit_ior)
3266   (simplify
3267    (shift (convert?:s (bit_op:s @0 INTEGER_CST@2)) INTEGER_CST@1)
3268    (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
3269     (with { tree mask = int_const_binop (shift, fold_convert (type, @2), @1); }
3270      (if (mask)
3271       (bit_op (shift (convert @0) @1) { mask; })))))))
3273 /* ~(~X >> Y) -> X >> Y (for arithmetic shift).  */
3274 (simplify
3275  (bit_not (convert1?:s (rshift:s (convert2?@0 (bit_not @1)) @2)))
3276   (if (!TYPE_UNSIGNED (TREE_TYPE (@0))
3277        && (element_precision (TREE_TYPE (@0))
3278            <= element_precision (TREE_TYPE (@1))
3279            || !TYPE_UNSIGNED (TREE_TYPE (@1))))
3280    (with
3281     { tree shift_type = TREE_TYPE (@0); }
3282      (convert (rshift (convert:shift_type @1) @2)))))
3284 /* ~(~X >>r Y) -> X >>r Y
3285    ~(~X <<r Y) -> X <<r Y */
3286 (for rotate (lrotate rrotate)
3287  (simplify
3288   (bit_not (convert1?:s (rotate:s (convert2?@0 (bit_not @1)) @2)))
3289    (if ((element_precision (TREE_TYPE (@0))
3290          <= element_precision (TREE_TYPE (@1))
3291          || !TYPE_UNSIGNED (TREE_TYPE (@1)))
3292         && (element_precision (type) <= element_precision (TREE_TYPE (@0))
3293             || !TYPE_UNSIGNED (TREE_TYPE (@0))))
3294     (with
3295      { tree rotate_type = TREE_TYPE (@0); }
3296       (convert (rotate (convert:rotate_type @1) @2))))))
3298 /* Simplifications of conversions.  */
3300 /* Basic strip-useless-type-conversions / strip_nops.  */
3301 (for cvt (convert view_convert float fix_trunc)
3302  (simplify
3303   (cvt @0)
3304   (if ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@0)))
3305        || (GENERIC && type == TREE_TYPE (@0)))
3306    @0)))
3308 /* Contract view-conversions.  */
3309 (simplify
3310   (view_convert (view_convert @0))
3311   (view_convert @0))
3313 /* For integral conversions with the same precision or pointer
3314    conversions use a NOP_EXPR instead.  */
3315 (simplify
3316   (view_convert @0)
3317   (if ((INTEGRAL_TYPE_P (type) || POINTER_TYPE_P (type))
3318        && (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
3319        && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (@0)))
3320    (convert @0)))
3322 /* Strip inner integral conversions that do not change precision or size, or
3323    zero-extend while keeping the same size (for bool-to-char).  */
3324 (simplify
3325   (view_convert (convert@0 @1))
3326   (if ((INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
3327        && (INTEGRAL_TYPE_P (TREE_TYPE (@1)) || POINTER_TYPE_P (TREE_TYPE (@1)))
3328        && TYPE_SIZE (TREE_TYPE (@0)) == TYPE_SIZE (TREE_TYPE (@1))
3329        && (TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1))
3330            || (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (TREE_TYPE (@1))
3331                && TYPE_UNSIGNED (TREE_TYPE (@1)))))
3332    (view_convert @1)))
3334 /* Simplify a view-converted empty constructor.  */
3335 (simplify
3336   (view_convert CONSTRUCTOR@0)
3337   (if (TREE_CODE (@0) != SSA_NAME
3338        && CONSTRUCTOR_NELTS (@0) == 0)
3339    { build_zero_cst (type); }))
3341 /* Re-association barriers around constants and other re-association
3342    barriers can be removed.  */
3343 (simplify
3344  (paren CONSTANT_CLASS_P@0)
3345  @0)
3346 (simplify
3347  (paren (paren@1 @0))
3348  @1)
3350 /* Handle cases of two conversions in a row.  */
3351 (for ocvt (convert float fix_trunc)
3352  (for icvt (convert float)
3353   (simplify
3354    (ocvt (icvt@1 @0))
3355    (with
3356     {
3357       tree inside_type = TREE_TYPE (@0);
3358       tree inter_type = TREE_TYPE (@1);
3359       int inside_int = INTEGRAL_TYPE_P (inside_type);
3360       int inside_ptr = POINTER_TYPE_P (inside_type);
3361       int inside_float = FLOAT_TYPE_P (inside_type);
3362       int inside_vec = VECTOR_TYPE_P (inside_type);
3363       unsigned int inside_prec = TYPE_PRECISION (inside_type);
3364       int inside_unsignedp = TYPE_UNSIGNED (inside_type);
3365       int inter_int = INTEGRAL_TYPE_P (inter_type);
3366       int inter_ptr = POINTER_TYPE_P (inter_type);
3367       int inter_float = FLOAT_TYPE_P (inter_type);
3368       int inter_vec = VECTOR_TYPE_P (inter_type);
3369       unsigned int inter_prec = TYPE_PRECISION (inter_type);
3370       int inter_unsignedp = TYPE_UNSIGNED (inter_type);
3371       int final_int = INTEGRAL_TYPE_P (type);
3372       int final_ptr = POINTER_TYPE_P (type);
3373       int final_float = FLOAT_TYPE_P (type);
3374       int final_vec = VECTOR_TYPE_P (type);
3375       unsigned int final_prec = TYPE_PRECISION (type);
3376       int final_unsignedp = TYPE_UNSIGNED (type);
3377     }
3378    (switch
3379     /* In addition to the cases of two conversions in a row
3380        handled below, if we are converting something to its own
3381        type via an object of identical or wider precision, neither
3382        conversion is needed.  */
3383     (if (((GIMPLE && useless_type_conversion_p (type, inside_type))
3384           || (GENERIC
3385               && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (inside_type)))
3386          && (((inter_int || inter_ptr) && final_int)
3387              || (inter_float && final_float))
3388          && inter_prec >= final_prec)
3389      (ocvt @0))
3391     /* Likewise, if the intermediate and initial types are either both
3392        float or both integer, we don't need the middle conversion if the
3393        former is wider than the latter and doesn't change the signedness
3394        (for integers).  Avoid this if the final type is a pointer since
3395        then we sometimes need the middle conversion.  */
3396     (if (((inter_int && inside_int) || (inter_float && inside_float))
3397          && (final_int || final_float)
3398          && inter_prec >= inside_prec
3399          && (inter_float || inter_unsignedp == inside_unsignedp))
3400      (ocvt @0))
3402     /* If we have a sign-extension of a zero-extended value, we can
3403        replace that by a single zero-extension.  Likewise if the
3404        final conversion does not change precision we can drop the
3405        intermediate conversion.  */
3406     (if (inside_int && inter_int && final_int
3407          && ((inside_prec < inter_prec && inter_prec < final_prec
3408               && inside_unsignedp && !inter_unsignedp)
3409              || final_prec == inter_prec))
3410      (ocvt @0))
3412     /* Two conversions in a row are not needed unless:
3413         - some conversion is floating-point (overstrict for now), or
3414         - some conversion is a vector (overstrict for now), or
3415         - the intermediate type is narrower than both initial and
3416           final, or
3417         - the intermediate type and innermost type differ in signedness,
3418           and the outermost type is wider than the intermediate, or
3419         - the initial type is a pointer type and the precisions of the
3420           intermediate and final types differ, or
3421         - the final type is a pointer type and the precisions of the
3422           initial and intermediate types differ.  */
3423     (if (! inside_float && ! inter_float && ! final_float
3424          && ! inside_vec && ! inter_vec && ! final_vec
3425          && (inter_prec >= inside_prec || inter_prec >= final_prec)
3426          && ! (inside_int && inter_int
3427                && inter_unsignedp != inside_unsignedp
3428                && inter_prec < final_prec)
3429          && ((inter_unsignedp && inter_prec > inside_prec)
3430              == (final_unsignedp && final_prec > inter_prec))
3431          && ! (inside_ptr && inter_prec != final_prec)
3432          && ! (final_ptr && inside_prec != inter_prec))
3433      (ocvt @0))
3435     /* A truncation to an unsigned type (a zero-extension) should be
3436        canonicalized as bitwise and of a mask.  */
3437     (if (GIMPLE /* PR70366: doing this in GENERIC breaks -Wconversion.  */
3438          && final_int && inter_int && inside_int
3439          && final_prec == inside_prec
3440          && final_prec > inter_prec
3441          && inter_unsignedp)
3442      (convert (bit_and @0 { wide_int_to_tree
3443                               (inside_type,
3444                                wi::mask (inter_prec, false,
3445                                          TYPE_PRECISION (inside_type))); })))
3447     /* If we are converting an integer to a floating-point that can
3448        represent it exactly and back to an integer, we can skip the
3449        floating-point conversion.  */
3450     (if (GIMPLE /* PR66211 */
3451          && inside_int && inter_float && final_int &&
3452          (unsigned) significand_size (TYPE_MODE (inter_type))
3453          >= inside_prec - !inside_unsignedp)
3454      (convert @0)))))))
3456 /* If we have a narrowing conversion to an integral type that is fed by a
3457    BIT_AND_EXPR, we might be able to remove the BIT_AND_EXPR if it merely
3458    masks off bits outside the final type (and nothing else).  */
3459 (simplify
3460   (convert (bit_and @0 INTEGER_CST@1))
3461   (if (INTEGRAL_TYPE_P (type)
3462        && INTEGRAL_TYPE_P (TREE_TYPE (@0))
3463        && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))
3464        && operand_equal_p (@1, build_low_bits_mask (TREE_TYPE (@1),
3465                                                     TYPE_PRECISION (type)), 0))
3466    (convert @0)))
3469 /* (X /[ex] A) * A -> X.  */
3470 (simplify
3471   (mult (convert1? (exact_div @0 @@1)) (convert2? @1))
3472   (convert @0))
3474 /* Simplify (A / B) * B + (A % B) -> A.  */
3475 (for div (trunc_div ceil_div floor_div round_div)
3476      mod (trunc_mod ceil_mod floor_mod round_mod)
3477   (simplify
3478    (plus:c (mult:c (div @0 @1) @1) (mod @0 @1))
3479    @0))
3481 /* ((X /[ex] A) +- B) * A  -->  X +- A * B.  */
3482 (for op (plus minus)
3483  (simplify
3484   (mult (convert1? (op (convert2? (exact_div @0 INTEGER_CST@@1)) INTEGER_CST@2)) @1)
3485   (if (tree_nop_conversion_p (type, TREE_TYPE (@2))
3486        && tree_nop_conversion_p (TREE_TYPE (@0), TREE_TYPE (@2)))
3487    (with
3488      {
3489        wi::overflow_type overflow;
3490        wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@2),
3491                                TYPE_SIGN (type), &overflow);
3492      }
3493      (if (types_match (type, TREE_TYPE (@2))
3494          && types_match (TREE_TYPE (@0), TREE_TYPE (@2)) && !overflow)
3495       (op @0 { wide_int_to_tree (type, mul); })
3496       (with { tree utype = unsigned_type_for (type); }
3497        (convert (op (convert:utype @0)
3498                     (mult (convert:utype @1) (convert:utype @2))))))))))
3500 /* Canonicalization of binary operations.  */
3502 /* Convert X + -C into X - C.  */
3503 (simplify
3504  (plus @0 REAL_CST@1)
3505  (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
3506   (with { tree tem = const_unop (NEGATE_EXPR, type, @1); }
3507    (if (!TREE_OVERFLOW (tem) || !flag_trapping_math)
3508     (minus @0 { tem; })))))
3510 /* Convert x+x into x*2.  */
3511 (simplify
3512  (plus @0 @0)
3513  (if (SCALAR_FLOAT_TYPE_P (type))
3514   (mult @0 { build_real (type, dconst2); })
3515   (if (INTEGRAL_TYPE_P (type))
3516    (mult @0 { build_int_cst (type, 2); }))))
3518 /* 0 - X  ->  -X.  */
3519 (simplify
3520  (minus integer_zerop @1)
3521  (negate @1))
3522 (simplify
3523  (pointer_diff integer_zerop @1)
3524  (negate (convert @1)))
3526 /* (ARG0 - ARG1) is the same as (-ARG1 + ARG0).  So check whether
3527    ARG0 is zero and X + ARG0 reduces to X, since that would mean
3528    (-ARG1 + ARG0) reduces to -ARG1.  */
3529 (simplify
3530  (minus real_zerop@0 @1)
3531  (if (fold_real_zero_addition_p (type, @0, 0))
3532   (negate @1)))
3534 /* Transform x * -1 into -x.  */
3535 (simplify
3536  (mult @0 integer_minus_onep)
3537  (negate @0))
3539 /* Reassociate (X * CST) * Y to (X * Y) * CST.  This does not introduce
3540    signed overflow for CST != 0 && CST != -1.  */
3541 (simplify
3542  (mult:c (mult:s@3 @0 INTEGER_CST@1) @2)
3543  (if (TREE_CODE (@2) != INTEGER_CST
3544       && single_use (@3)
3545       && !integer_zerop (@1) && !integer_minus_onep (@1))
3546   (mult (mult @0 @2) @1)))
3548 /* True if we can easily extract the real and imaginary parts of a complex
3549    number.  */
3550 (match compositional_complex
3551  (convert? (complex @0 @1)))
3553 /* COMPLEX_EXPR and REALPART/IMAGPART_EXPR cancellations.  */
3554 (simplify
3555  (complex (realpart @0) (imagpart @0))
3556  @0)
3557 (simplify
3558  (realpart (complex @0 @1))
3559  @0)
3560 (simplify
3561  (imagpart (complex @0 @1))
3562  @1)
3564 /* Sometimes we only care about half of a complex expression.  */
3565 (simplify
3566  (realpart (convert?:s (conj:s @0)))
3567  (convert (realpart @0)))
3568 (simplify
3569  (imagpart (convert?:s (conj:s @0)))
3570  (convert (negate (imagpart @0))))
3571 (for part (realpart imagpart)
3572  (for op (plus minus)
3573   (simplify
3574    (part (convert?:s@2 (op:s @0 @1)))
3575    (convert (op (part @0) (part @1))))))
3576 (simplify
3577  (realpart (convert?:s (CEXPI:s @0)))
3578  (convert (COS @0)))
3579 (simplify
3580  (imagpart (convert?:s (CEXPI:s @0)))
3581  (convert (SIN @0)))
3583 /* conj(conj(x)) -> x  */
3584 (simplify
3585  (conj (convert? (conj @0)))
3586  (if (tree_nop_conversion_p (TREE_TYPE (@0), type))
3587   (convert @0)))
3589 /* conj({x,y}) -> {x,-y}  */
3590 (simplify
3591  (conj (convert?:s (complex:s @0 @1)))
3592  (with { tree itype = TREE_TYPE (type); }
3593   (complex (convert:itype @0) (negate (convert:itype @1)))))
3595 /* BSWAP simplifications, transforms checked by gcc.dg/builtin-bswap-8.c.  */
3596 (for bswap (BUILT_IN_BSWAP16 BUILT_IN_BSWAP32 BUILT_IN_BSWAP64)
3597  (simplify
3598   (bswap (bswap @0))
3599   @0)
3600  (simplify
3601   (bswap (bit_not (bswap @0)))
3602   (bit_not @0))
3603  (for bitop (bit_xor bit_ior bit_and)
3604   (simplify
3605    (bswap (bitop:c (bswap @0) @1))
3606    (bitop @0 (bswap @1)))))
3609 /* Combine COND_EXPRs and VEC_COND_EXPRs.  */
3611 /* Simplify constant conditions.
3612    Only optimize constant conditions when the selected branch
3613    has the same type as the COND_EXPR.  This avoids optimizing
3614    away "c ? x : throw", where the throw has a void type.
3615    Note that we cannot throw away the fold-const.c variant nor
3616    this one as we depend on doing this transform before possibly
3617    A ? B : B -> B triggers and the fold-const.c one can optimize
3618    0 ? A : B to B even if A has side-effects.  Something
3619    genmatch cannot handle.  */
3620 (simplify
3621  (cond INTEGER_CST@0 @1 @2)
3622  (if (integer_zerop (@0))
3623   (if (!VOID_TYPE_P (TREE_TYPE (@2)) || VOID_TYPE_P (type))
3624    @2)
3625   (if (!VOID_TYPE_P (TREE_TYPE (@1)) || VOID_TYPE_P (type))
3626    @1)))
3627 (simplify
3628  (vec_cond VECTOR_CST@0 @1 @2)
3629  (if (integer_all_onesp (@0))
3630   @1
3631   (if (integer_zerop (@0))
3632    @2)))
3634 #if GIMPLE
3635 /* Sink unary operations to branches, but only if we do fold both.  */
3636 (for op (negate bit_not abs absu)
3637  (simplify
3638   (op (vec_cond:s @0 @1 @2))
3639   (vec_cond @0 (op! @1) (op! @2))))
3641 /* Sink binary operation to branches, but only if we can fold it.  */
3642 (for op (tcc_comparison plus minus mult bit_and bit_ior bit_xor
3643          rdiv trunc_div ceil_div floor_div round_div
3644          trunc_mod ceil_mod floor_mod round_mod min max)
3645 /* (c ? a : b) op (c ? d : e)  -->  c ? (a op d) : (b op e) */
3646  (simplify
3647   (op (vec_cond:s @0 @1 @2) (vec_cond:s @0 @3 @4))
3648   (vec_cond @0 (op! @1 @3) (op! @2 @4)))
3650 /* (c ? a : b) op d  -->  c ? (a op d) : (b op d) */
3651  (simplify
3652   (op (vec_cond:s @0 @1 @2) @3)
3653   (vec_cond @0 (op! @1 @3) (op! @2 @3)))
3654  (simplify
3655   (op @3 (vec_cond:s @0 @1 @2))
3656   (vec_cond @0 (op! @3 @1) (op! @3 @2))))
3657 #endif
3659 /* (v ? w : 0) ? a : b is just (v & w) ? a : b
3660    Currently disabled after pass lvec because ARM understands
3661    VEC_COND_EXPR<v==w,-1,0> but not a plain v==w fed to BIT_IOR_EXPR.  */
3662 (simplify
3663  (vec_cond (vec_cond:s @0 @3 integer_zerop) @1 @2)
3664  (if (optimize_vectors_before_lowering_p () && types_match (@0, @3))
3665   (vec_cond (bit_and @0 @3) @1 @2)))
3666 (simplify
3667  (vec_cond (vec_cond:s @0 integer_all_onesp @3) @1 @2)
3668  (if (optimize_vectors_before_lowering_p () && types_match (@0, @3))
3669   (vec_cond (bit_ior @0 @3) @1 @2)))
3670 (simplify
3671  (vec_cond (vec_cond:s @0 integer_zerop @3) @1 @2)
3672  (if (optimize_vectors_before_lowering_p () && types_match (@0, @3))
3673   (vec_cond (bit_ior @0 (bit_not @3)) @2 @1)))
3674 (simplify
3675  (vec_cond (vec_cond:s @0 @3 integer_all_onesp) @1 @2)
3676  (if (optimize_vectors_before_lowering_p () && types_match (@0, @3))
3677   (vec_cond (bit_and @0 (bit_not @3)) @2 @1)))
3679 /* c1 ? c2 ? a : b : b  -->  (c1 & c2) ? a : b  */
3680 (simplify
3681  (vec_cond @0 (vec_cond:s @1 @2 @3) @3)
3682  (if (optimize_vectors_before_lowering_p () && types_match (@0, @1))
3683   (vec_cond (bit_and @0 @1) @2 @3)))
3684 (simplify
3685  (vec_cond @0 @2 (vec_cond:s @1 @2 @3))
3686  (if (optimize_vectors_before_lowering_p () && types_match (@0, @1))
3687   (vec_cond (bit_ior @0 @1) @2 @3)))
3688 (simplify
3689  (vec_cond @0 (vec_cond:s @1 @2 @3) @2)
3690  (if (optimize_vectors_before_lowering_p () && types_match (@0, @1))
3691   (vec_cond (bit_ior (bit_not @0) @1) @2 @3)))
3692 (simplify
3693  (vec_cond @0 @3 (vec_cond:s @1 @2 @3))
3694  (if (optimize_vectors_before_lowering_p () && types_match (@0, @1))
3695   (vec_cond (bit_and (bit_not @0) @1) @2 @3)))
3697 /* Canonicalize mask ? { 0, ... } : { -1, ...} to ~mask if the mask
3698    types are compatible.  */
3699 (simplify
3700  (vec_cond @0 VECTOR_CST@1 VECTOR_CST@2)
3701  (if (VECTOR_BOOLEAN_TYPE_P (type)
3702       && types_match (type, TREE_TYPE (@0)))
3703   (if (integer_zerop (@1) && integer_all_onesp (@2))
3704    (bit_not @0)
3705    (if (integer_all_onesp (@1) && integer_zerop (@2))
3706     @0))))
3708 /* Simplification moved from fold_cond_expr_with_comparison.  It may also
3709    be extended.  */
3710 /* This pattern implements two kinds simplification:
3712    Case 1)
3713    (cond (cmp (convert1? x) c1) (convert2? x) c2) -> (minmax (x c)) if:
3714      1) Conversions are type widening from smaller type.
3715      2) Const c1 equals to c2 after canonicalizing comparison.
3716      3) Comparison has tree code LT, LE, GT or GE.
3717    This specific pattern is needed when (cmp (convert x) c) may not
3718    be simplified by comparison patterns because of multiple uses of
3719    x.  It also makes sense here because simplifying across multiple
3720    referred var is always benefitial for complicated cases.
3722    Case 2)
3723    (cond (eq (convert1? x) c1) (convert2? x) c2) -> (cond (eq x c1) c1 c2).  */
3724 (for cmp (lt le gt ge eq)
3725  (simplify
3726   (cond (cmp (convert1? @1) INTEGER_CST@3) (convert2? @1) INTEGER_CST@2)
3727   (with
3728    {
3729      tree from_type = TREE_TYPE (@1);
3730      tree c1_type = TREE_TYPE (@3), c2_type = TREE_TYPE (@2);
3731      enum tree_code code = ERROR_MARK;
3733      if (INTEGRAL_TYPE_P (from_type)
3734          && int_fits_type_p (@2, from_type)
3735          && (types_match (c1_type, from_type)
3736              || (TYPE_PRECISION (c1_type) > TYPE_PRECISION (from_type)
3737                  && (TYPE_UNSIGNED (from_type)
3738                      || TYPE_SIGN (c1_type) == TYPE_SIGN (from_type))))
3739          && (types_match (c2_type, from_type)
3740              || (TYPE_PRECISION (c2_type) > TYPE_PRECISION (from_type)
3741                  && (TYPE_UNSIGNED (from_type)
3742                      || TYPE_SIGN (c2_type) == TYPE_SIGN (from_type)))))
3743        {
3744          if (cmp != EQ_EXPR)
3745            {
3746              if (wi::to_widest (@3) == (wi::to_widest (@2) - 1))
3747                {
3748                  /* X <= Y - 1 equals to X < Y.  */
3749                  if (cmp == LE_EXPR)
3750                    code = LT_EXPR;
3751                  /* X > Y - 1 equals to X >= Y.  */
3752                  if (cmp == GT_EXPR)
3753                    code = GE_EXPR;
3754                }
3755              if (wi::to_widest (@3) == (wi::to_widest (@2) + 1))
3756                {
3757                  /* X < Y + 1 equals to X <= Y.  */
3758                  if (cmp == LT_EXPR)
3759                    code = LE_EXPR;
3760                  /* X >= Y + 1 equals to X > Y.  */
3761                  if (cmp == GE_EXPR)
3762                    code = GT_EXPR;
3763                }
3764              if (code != ERROR_MARK
3765                  || wi::to_widest (@2) == wi::to_widest (@3))
3766                {
3767                  if (cmp == LT_EXPR || cmp == LE_EXPR)
3768                    code = MIN_EXPR;
3769                  if (cmp == GT_EXPR || cmp == GE_EXPR)
3770                    code = MAX_EXPR;
3771                }
3772            }
3773          /* Can do A == C1 ? A : C2  ->  A == C1 ? C1 : C2?  */
3774          else if (int_fits_type_p (@3, from_type))
3775            code = EQ_EXPR;
3776        }
3777    }
3778    (if (code == MAX_EXPR)
3779     (convert (max @1 (convert @2)))
3780     (if (code == MIN_EXPR)
3781      (convert (min @1 (convert @2)))
3782      (if (code == EQ_EXPR)
3783       (convert (cond (eq @1 (convert @3))
3784                      (convert:from_type @3) (convert:from_type @2)))))))))
3786 /* (cond (cmp (convert? x) c1) (op x c2) c3) -> (op (minmax x c1) c2) if:
3788      1) OP is PLUS or MINUS.
3789      2) CMP is LT, LE, GT or GE.
3790      3) C3 == (C1 op C2), and computation doesn't have undefined behavior.
3792    This pattern also handles special cases like:
3794      A) Operand x is a unsigned to signed type conversion and c1 is
3795         integer zero.  In this case,
3796           (signed type)x  < 0  <=>  x  > MAX_VAL(signed type)
3797           (signed type)x >= 0  <=>  x <= MAX_VAL(signed type)
3798      B) Const c1 may not equal to (C3 op' C2).  In this case we also
3799         check equality for (c1+1) and (c1-1) by adjusting comparison
3800         code.
3802    TODO: Though signed type is handled by this pattern, it cannot be
3803    simplified at the moment because C standard requires additional
3804    type promotion.  In order to match&simplify it here, the IR needs
3805    to be cleaned up by other optimizers, i.e, VRP.  */
3806 (for op (plus minus)
3807  (for cmp (lt le gt ge)
3808   (simplify
3809    (cond (cmp (convert? @X) INTEGER_CST@1) (op @X INTEGER_CST@2) INTEGER_CST@3)
3810    (with { tree from_type = TREE_TYPE (@X), to_type = TREE_TYPE (@1); }
3811     (if (types_match (from_type, to_type)
3812          /* Check if it is special case A).  */
3813          || (TYPE_UNSIGNED (from_type)
3814              && !TYPE_UNSIGNED (to_type)
3815              && TYPE_PRECISION (from_type) == TYPE_PRECISION (to_type)
3816              && integer_zerop (@1)
3817              && (cmp == LT_EXPR || cmp == GE_EXPR)))
3818      (with
3819       {
3820         wi::overflow_type overflow = wi::OVF_NONE;
3821         enum tree_code code, cmp_code = cmp;
3822         wide_int real_c1;
3823         wide_int c1 = wi::to_wide (@1);
3824         wide_int c2 = wi::to_wide (@2);
3825         wide_int c3 = wi::to_wide (@3);
3826         signop sgn = TYPE_SIGN (from_type);
3828         /* Handle special case A), given x of unsigned type:
3829             ((signed type)x  < 0) <=> (x  > MAX_VAL(signed type))
3830             ((signed type)x >= 0) <=> (x <= MAX_VAL(signed type))  */
3831         if (!types_match (from_type, to_type))
3832           {
3833             if (cmp_code == LT_EXPR)
3834               cmp_code = GT_EXPR;
3835             if (cmp_code == GE_EXPR)
3836               cmp_code = LE_EXPR;
3837             c1 = wi::max_value (to_type);
3838           }
3839         /* To simplify this pattern, we require c3 = (c1 op c2).  Here we
3840            compute (c3 op' c2) and check if it equals to c1 with op' being
3841            the inverted operator of op.  Make sure overflow doesn't happen
3842            if it is undefined.  */
3843         if (op == PLUS_EXPR)
3844           real_c1 = wi::sub (c3, c2, sgn, &overflow);
3845         else
3846           real_c1 = wi::add (c3, c2, sgn, &overflow);
3848         code = cmp_code;
3849         if (!overflow || !TYPE_OVERFLOW_UNDEFINED (from_type))
3850           {
3851             /* Check if c1 equals to real_c1.  Boundary condition is handled
3852                by adjusting comparison operation if necessary.  */
3853             if (!wi::cmp (wi::sub (real_c1, 1, sgn, &overflow), c1, sgn)
3854                 && !overflow)
3855               {
3856                 /* X <= Y - 1 equals to X < Y.  */
3857                 if (cmp_code == LE_EXPR)
3858                   code = LT_EXPR;
3859                 /* X > Y - 1 equals to X >= Y.  */
3860                 if (cmp_code == GT_EXPR)
3861                   code = GE_EXPR;
3862               }
3863             if (!wi::cmp (wi::add (real_c1, 1, sgn, &overflow), c1, sgn)
3864                 && !overflow)
3865               {
3866                 /* X < Y + 1 equals to X <= Y.  */
3867                 if (cmp_code == LT_EXPR)
3868                   code = LE_EXPR;
3869                 /* X >= Y + 1 equals to X > Y.  */
3870                 if (cmp_code == GE_EXPR)
3871                   code = GT_EXPR;
3872               }
3873             if (code != cmp_code || !wi::cmp (real_c1, c1, sgn))
3874               {
3875                 if (cmp_code == LT_EXPR || cmp_code == LE_EXPR)
3876                   code = MIN_EXPR;
3877                 if (cmp_code == GT_EXPR || cmp_code == GE_EXPR)
3878                   code = MAX_EXPR;
3879               }
3880           }
3881       }
3882       (if (code == MAX_EXPR)
3883        (op (max @X { wide_int_to_tree (from_type, real_c1); })
3884            { wide_int_to_tree (from_type, c2); })
3885        (if (code == MIN_EXPR)
3886         (op (min @X { wide_int_to_tree (from_type, real_c1); })
3887             { wide_int_to_tree (from_type, c2); })))))))))
3889 (for cnd (cond vec_cond)
3890  /* A ? B : (A ? X : C) -> A ? B : C.  */
3891  (simplify
3892   (cnd @0 (cnd @0 @1 @2) @3)
3893   (cnd @0 @1 @3))
3894  (simplify
3895   (cnd @0 @1 (cnd @0 @2 @3))
3896   (cnd @0 @1 @3))
3897  /* A ? B : (!A ? C : X) -> A ? B : C.  */
3898  /* ???  This matches embedded conditions open-coded because genmatch
3899     would generate matching code for conditions in separate stmts only.
3900     The following is still important to merge then and else arm cases
3901     from if-conversion.  */
3902  (simplify
3903   (cnd @0 @1 (cnd @2 @3 @4))
3904   (if (inverse_conditions_p (@0, @2))
3905    (cnd @0 @1 @3)))
3906  (simplify
3907   (cnd @0 (cnd @1 @2 @3) @4)
3908   (if (inverse_conditions_p (@0, @1))
3909    (cnd @0 @3 @4)))
3911  /* A ? B : B -> B.  */
3912  (simplify
3913   (cnd @0 @1 @1)
3914   @1)
3916  /* !A ? B : C -> A ? C : B.  */
3917  (simplify
3918   (cnd (logical_inverted_value truth_valued_p@0) @1 @2)
3919   (cnd @0 @2 @1)))
3921 /* -(type)!A -> (type)A - 1.  */
3922 (simplify
3923  (negate (convert?:s (logical_inverted_value:s @0)))
3924  (if (INTEGRAL_TYPE_P (type)
3925       && TREE_CODE (type) != BOOLEAN_TYPE
3926       && TYPE_PRECISION (type) > 1
3927       && TREE_CODE (@0) == SSA_NAME
3928       && ssa_name_has_boolean_range (@0))
3929   (plus (convert:type @0) { build_all_ones_cst (type); })))
3931 /* A + (B vcmp C ? 1 : 0) -> A - (B vcmp C ? -1 : 0), since vector comparisons
3932    return all -1 or all 0 results.  */
3933 /* ??? We could instead convert all instances of the vec_cond to negate,
3934    but that isn't necessarily a win on its own.  */
3935 (simplify
3936  (plus:c @3 (view_convert? (vec_cond:s @0 integer_each_onep@1 integer_zerop@2)))
3937  (if (VECTOR_TYPE_P (type)
3938       && known_eq (TYPE_VECTOR_SUBPARTS (type),
3939                    TYPE_VECTOR_SUBPARTS (TREE_TYPE (@1)))
3940       && (TYPE_MODE (TREE_TYPE (type))
3941           == TYPE_MODE (TREE_TYPE (TREE_TYPE (@1)))))
3942   (minus @3 (view_convert (vec_cond @0 (negate @1) @2)))))
3944 /* ... likewise A - (B vcmp C ? 1 : 0) -> A + (B vcmp C ? -1 : 0).  */
3945 (simplify
3946  (minus @3 (view_convert? (vec_cond:s @0 integer_each_onep@1 integer_zerop@2)))
3947  (if (VECTOR_TYPE_P (type)
3948       && known_eq (TYPE_VECTOR_SUBPARTS (type),
3949                    TYPE_VECTOR_SUBPARTS (TREE_TYPE (@1)))
3950       && (TYPE_MODE (TREE_TYPE (type))
3951           == TYPE_MODE (TREE_TYPE (TREE_TYPE (@1)))))
3952   (plus @3 (view_convert (vec_cond @0 (negate @1) @2)))))
3955 /* Simplifications of comparisons.  */
3957 /* See if we can reduce the magnitude of a constant involved in a
3958    comparison by changing the comparison code.  This is a canonicalization
3959    formerly done by maybe_canonicalize_comparison_1.  */
3960 (for cmp  (le gt)
3961      acmp (lt ge)
3962  (simplify
3963   (cmp @0 uniform_integer_cst_p@1)
3964   (with { tree cst = uniform_integer_cst_p (@1); }
3965    (if (tree_int_cst_sgn (cst) == -1)
3966      (acmp @0 { build_uniform_cst (TREE_TYPE (@1),
3967                                    wide_int_to_tree (TREE_TYPE (cst),
3968                                                      wi::to_wide (cst)
3969                                                      + 1)); })))))
3970 (for cmp  (ge lt)
3971      acmp (gt le)
3972  (simplify
3973   (cmp @0 uniform_integer_cst_p@1)
3974   (with { tree cst = uniform_integer_cst_p (@1); }
3975    (if (tree_int_cst_sgn (cst) == 1)
3976     (acmp @0 { build_uniform_cst (TREE_TYPE (@1),
3977                                   wide_int_to_tree (TREE_TYPE (cst),
3978                                   wi::to_wide (cst) - 1)); })))))
3980 /* We can simplify a logical negation of a comparison to the
3981    inverted comparison.  As we cannot compute an expression
3982    operator using invert_tree_comparison we have to simulate
3983    that with expression code iteration.  */
3984 (for cmp (tcc_comparison)
3985      icmp (inverted_tcc_comparison)
3986      ncmp (inverted_tcc_comparison_with_nans)
3987  /* Ideally we'd like to combine the following two patterns
3988     and handle some more cases by using
3989       (logical_inverted_value (cmp @0 @1))
3990     here but for that genmatch would need to "inline" that.
3991     For now implement what forward_propagate_comparison did.  */
3992  (simplify
3993   (bit_not (cmp @0 @1))
3994   (if (VECTOR_TYPE_P (type)
3995        || (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1))
3996    /* Comparison inversion may be impossible for trapping math,
3997       invert_tree_comparison will tell us.  But we can't use
3998       a computed operator in the replacement tree thus we have
3999       to play the trick below.  */
4000    (with { enum tree_code ic = invert_tree_comparison
4001              (cmp, HONOR_NANS (@0)); }
4002     (if (ic == icmp)
4003      (icmp @0 @1)
4004      (if (ic == ncmp)
4005       (ncmp @0 @1))))))
4006  (simplify
4007   (bit_xor (cmp @0 @1) integer_truep)
4008   (with { enum tree_code ic = invert_tree_comparison
4009             (cmp, HONOR_NANS (@0)); }
4010    (if (ic == icmp)
4011     (icmp @0 @1)
4012     (if (ic == ncmp)
4013      (ncmp @0 @1))))))
4015 /* Transform comparisons of the form X - Y CMP 0 to X CMP Y.
4016    ??? The transformation is valid for the other operators if overflow
4017    is undefined for the type, but performing it here badly interacts
4018    with the transformation in fold_cond_expr_with_comparison which
4019    attempts to synthetize ABS_EXPR.  */
4020 (for cmp (eq ne)
4021  (for sub (minus pointer_diff)
4022   (simplify
4023    (cmp (sub@2 @0 @1) integer_zerop)
4024    (if (single_use (@2))
4025     (cmp @0 @1)))))
4027 /* Simplify (x < 0) ^ (y < 0) to (x ^ y) < 0 and
4028    (x >= 0) ^ (y >= 0) to (x ^ y) < 0.  */
4029 (for cmp (lt ge)
4030  (simplify
4031   (bit_xor (cmp:s @0 integer_zerop) (cmp:s @1 integer_zerop))
4032    (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
4033         && !TYPE_UNSIGNED (TREE_TYPE (@0))
4034         && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
4035     (lt (bit_xor @0 @1) { build_zero_cst (TREE_TYPE (@0)); }))))
4036 /* Simplify (x < 0) ^ (y >= 0) to (x ^ y) >= 0 and
4037    (x >= 0) ^ (y < 0) to (x ^ y) >= 0.  */
4038 (simplify
4039  (bit_xor:c (lt:s @0 integer_zerop) (ge:s @1 integer_zerop))
4040   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
4041        && !TYPE_UNSIGNED (TREE_TYPE (@0))
4042        && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
4043    (ge (bit_xor @0 @1) { build_zero_cst (TREE_TYPE (@0)); })))
4045 /* Transform comparisons of the form X * C1 CMP 0 to X CMP 0 in the
4046    signed arithmetic case.  That form is created by the compiler
4047    often enough for folding it to be of value.  One example is in
4048    computing loop trip counts after Operator Strength Reduction.  */
4049 (for cmp (simple_comparison)
4050      scmp (swapped_simple_comparison)
4051  (simplify
4052   (cmp (mult@3 @0 INTEGER_CST@1) integer_zerop@2)
4053   /* Handle unfolded multiplication by zero.  */
4054   (if (integer_zerop (@1))
4055    (cmp @1 @2)
4056    (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
4057         && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
4058         && single_use (@3))
4059     /* If @1 is negative we swap the sense of the comparison.  */
4060     (if (tree_int_cst_sgn (@1) < 0)
4061      (scmp @0 @2)
4062      (cmp @0 @2))))))
4064 /* For integral types with undefined overflow fold
4065    x * C1 == C2 into x == C2 / C1 or false.
4066    If overflow wraps and C1 is odd, simplify to x == C2 / C1 in the ring
4067    Z / 2^n Z.  */
4068 (for cmp (eq ne)
4069  (simplify
4070   (cmp (mult @0 INTEGER_CST@1) INTEGER_CST@2)
4071   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
4072        && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
4073        && wi::to_wide (@1) != 0)
4074    (with { widest_int quot; }
4075     (if (wi::multiple_of_p (wi::to_widest (@2), wi::to_widest (@1),
4076                             TYPE_SIGN (TREE_TYPE (@0)), &quot))
4077      (cmp @0 { wide_int_to_tree (TREE_TYPE (@0), quot); })
4078      { constant_boolean_node (cmp == NE_EXPR, type); }))
4079    (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
4080         && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))
4081         && (wi::bit_and (wi::to_wide (@1), 1) == 1))
4082     (cmp @0
4083      {
4084        tree itype = TREE_TYPE (@0);
4085        int p = TYPE_PRECISION (itype);
4086        wide_int m = wi::one (p + 1) << p;
4087        wide_int a = wide_int::from (wi::to_wide (@1), p + 1, UNSIGNED);
4088        wide_int i = wide_int::from (wi::mod_inv (a, m),
4089                                     p, TYPE_SIGN (itype));
4090        wide_int_to_tree (itype, wi::mul (i, wi::to_wide (@2)));
4091      })))))
4093 /* Simplify comparison of something with itself.  For IEEE
4094    floating-point, we can only do some of these simplifications.  */
4095 (for cmp (eq ge le)
4096  (simplify
4097   (cmp @0 @0)
4098   (if (! FLOAT_TYPE_P (TREE_TYPE (@0))
4099        || ! HONOR_NANS (@0))
4100    { constant_boolean_node (true, type); }
4101    (if (cmp != EQ_EXPR)
4102     (eq @0 @0)))))
4103 (for cmp (ne gt lt)
4104  (simplify
4105   (cmp @0 @0)
4106   (if (cmp != NE_EXPR
4107        || ! FLOAT_TYPE_P (TREE_TYPE (@0))
4108        || ! HONOR_NANS (@0))
4109    { constant_boolean_node (false, type); })))
4110 (for cmp (unle unge uneq)
4111  (simplify
4112   (cmp @0 @0)
4113   { constant_boolean_node (true, type); }))
4114 (for cmp (unlt ungt)
4115  (simplify
4116   (cmp @0 @0)
4117   (unordered @0 @0)))
4118 (simplify
4119  (ltgt @0 @0)
4120  (if (!flag_trapping_math)
4121   { constant_boolean_node (false, type); }))
4123 /* x == ~x -> false */
4124 /* x != ~x -> true */
4125 (for cmp (eq ne)
4126  (simplify
4127   (cmp:c @0 (bit_not @0))
4128   { constant_boolean_node (cmp == NE_EXPR, type); }))
4130 /* Fold ~X op ~Y as Y op X.  */
4131 (for cmp (simple_comparison)
4132  (simplify
4133   (cmp (bit_not@2 @0) (bit_not@3 @1))
4134   (if (single_use (@2) && single_use (@3))
4135    (cmp @1 @0))))
4137 /* Fold ~X op C as X op' ~C, where op' is the swapped comparison.  */
4138 (for cmp (simple_comparison)
4139      scmp (swapped_simple_comparison)
4140  (simplify
4141   (cmp (bit_not@2 @0) CONSTANT_CLASS_P@1)
4142   (if (single_use (@2)
4143        && (TREE_CODE (@1) == INTEGER_CST || TREE_CODE (@1) == VECTOR_CST))
4144    (scmp @0 (bit_not @1)))))
4146 (for cmp (simple_comparison)
4147  /* Fold (double)float1 CMP (double)float2 into float1 CMP float2.  */
4148  (simplify
4149   (cmp (convert@2 @0) (convert? @1))
4150   (if (FLOAT_TYPE_P (TREE_TYPE (@0))
4151        && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@2))
4152            == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0)))
4153        && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@2))
4154            == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1))))
4155    (with
4156     {
4157       tree type1 = TREE_TYPE (@1);
4158       if (TREE_CODE (@1) == REAL_CST && !DECIMAL_FLOAT_TYPE_P (type1))
4159         {
4160           REAL_VALUE_TYPE orig = TREE_REAL_CST (@1);
4161           if (TYPE_PRECISION (type1) > TYPE_PRECISION (float_type_node)
4162               && exact_real_truncate (TYPE_MODE (float_type_node), &orig))
4163             type1 = float_type_node;
4164           if (TYPE_PRECISION (type1) > TYPE_PRECISION (double_type_node)
4165               && exact_real_truncate (TYPE_MODE (double_type_node), &orig))
4166             type1 = double_type_node;
4167         }
4168       tree newtype
4169         = (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type1)
4170            ? TREE_TYPE (@0) : type1);
4171     }
4172     (if (TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (newtype))
4173      (cmp (convert:newtype @0) (convert:newtype @1))))))
4175  (simplify
4176   (cmp @0 REAL_CST@1)
4177   /* IEEE doesn't distinguish +0 and -0 in comparisons.  */
4178   (switch
4179    /* a CMP (-0) -> a CMP 0  */
4180    (if (REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@1)))
4181     (cmp @0 { build_real (TREE_TYPE (@1), dconst0); }))
4182    /* x != NaN is always true, other ops are always false.  */
4183    (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
4184         && ! HONOR_SNANS (@1))
4185     { constant_boolean_node (cmp == NE_EXPR, type); })
4186    /* Fold comparisons against infinity.  */
4187    (if (REAL_VALUE_ISINF (TREE_REAL_CST (@1))
4188         && MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (@1))))
4189     (with
4190      {
4191        REAL_VALUE_TYPE max;
4192        enum tree_code code = cmp;
4193        bool neg = REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1));
4194        if (neg)
4195          code = swap_tree_comparison (code);
4196      }
4197      (switch
4198       /* x > +Inf is always false, if we ignore NaNs or exceptions.  */
4199       (if (code == GT_EXPR
4200            && !(HONOR_NANS (@0) && flag_trapping_math))
4201        { constant_boolean_node (false, type); })
4202       (if (code == LE_EXPR)
4203        /* x <= +Inf is always true, if we don't care about NaNs.  */
4204        (if (! HONOR_NANS (@0))
4205         { constant_boolean_node (true, type); }
4206         /* x <= +Inf is the same as x == x, i.e. !isnan(x), but this loses
4207            an "invalid" exception.  */
4208         (if (!flag_trapping_math)
4209          (eq @0 @0))))
4210       /* x == +Inf and x >= +Inf are always equal to x > DBL_MAX, but
4211          for == this introduces an exception for x a NaN.  */
4212       (if ((code == EQ_EXPR && !(HONOR_NANS (@0) && flag_trapping_math))
4213            || code == GE_EXPR)
4214        (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
4215         (if (neg)
4216          (lt @0 { build_real (TREE_TYPE (@0), max); })
4217          (gt @0 { build_real (TREE_TYPE (@0), max); }))))
4218       /* x < +Inf is always equal to x <= DBL_MAX.  */
4219       (if (code == LT_EXPR)
4220        (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
4221         (if (neg)
4222          (ge @0 { build_real (TREE_TYPE (@0), max); })
4223          (le @0 { build_real (TREE_TYPE (@0), max); }))))
4224       /* x != +Inf is always equal to !(x > DBL_MAX), but this introduces
4225          an exception for x a NaN so use an unordered comparison.  */
4226       (if (code == NE_EXPR)
4227        (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
4228         (if (! HONOR_NANS (@0))
4229          (if (neg)
4230           (ge @0 { build_real (TREE_TYPE (@0), max); })
4231           (le @0 { build_real (TREE_TYPE (@0), max); }))
4232          (if (neg)
4233           (unge @0 { build_real (TREE_TYPE (@0), max); })
4234           (unle @0 { build_real (TREE_TYPE (@0), max); }))))))))))
4236  /* If this is a comparison of a real constant with a PLUS_EXPR
4237     or a MINUS_EXPR of a real constant, we can convert it into a
4238     comparison with a revised real constant as long as no overflow
4239     occurs when unsafe_math_optimizations are enabled.  */
4240  (if (flag_unsafe_math_optimizations)
4241   (for op (plus minus)
4242    (simplify
4243     (cmp (op @0 REAL_CST@1) REAL_CST@2)
4244     (with
4245      {
4246        tree tem = const_binop (op == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR,
4247                                TREE_TYPE (@1), @2, @1);
4248      }
4249      (if (tem && !TREE_OVERFLOW (tem))
4250       (cmp @0 { tem; }))))))
4252  /* Likewise, we can simplify a comparison of a real constant with
4253     a MINUS_EXPR whose first operand is also a real constant, i.e.
4254     (c1 - x) < c2 becomes x > c1-c2.  Reordering is allowed on
4255     floating-point types only if -fassociative-math is set.  */
4256  (if (flag_associative_math)
4257   (simplify
4258    (cmp (minus REAL_CST@0 @1) REAL_CST@2)
4259    (with { tree tem = const_binop (MINUS_EXPR, TREE_TYPE (@1), @0, @2); }
4260     (if (tem && !TREE_OVERFLOW (tem))
4261      (cmp { tem; } @1)))))
4263  /* Fold comparisons against built-in math functions.  */
4264  (if (flag_unsafe_math_optimizations && ! flag_errno_math)
4265   (for sq (SQRT)
4266    (simplify
4267     (cmp (sq @0) REAL_CST@1)
4268     (switch
4269      (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
4270       (switch
4271        /* sqrt(x) < y is always false, if y is negative.  */
4272        (if (cmp == EQ_EXPR || cmp == LT_EXPR || cmp == LE_EXPR)
4273         { constant_boolean_node (false, type); })
4274        /* sqrt(x) > y is always true, if y is negative and we
4275           don't care about NaNs, i.e. negative values of x.  */
4276        (if (cmp == NE_EXPR || !HONOR_NANS (@0))
4277         { constant_boolean_node (true, type); })
4278        /* sqrt(x) > y is the same as x >= 0, if y is negative.  */
4279        (ge @0 { build_real (TREE_TYPE (@0), dconst0); })))
4280      (if (real_equal (TREE_REAL_CST_PTR (@1), &dconst0))
4281       (switch
4282        /* sqrt(x) < 0 is always false.  */
4283        (if (cmp == LT_EXPR)
4284         { constant_boolean_node (false, type); })
4285        /* sqrt(x) >= 0 is always true if we don't care about NaNs.  */
4286        (if (cmp == GE_EXPR && !HONOR_NANS (@0))
4287         { constant_boolean_node (true, type); })
4288        /* sqrt(x) <= 0 -> x == 0.  */
4289        (if (cmp == LE_EXPR)
4290         (eq @0 @1))
4291        /* Otherwise sqrt(x) cmp 0 -> x cmp 0.  Here cmp can be >=, >,
4292           == or !=.  In the last case:
4294             (sqrt(x) != 0) == (NaN != 0) == true == (x != 0)
4296           if x is negative or NaN.  Due to -funsafe-math-optimizations,
4297           the results for other x follow from natural arithmetic.  */
4298        (cmp @0 @1)))
4299      (if ((cmp == LT_EXPR
4300            || cmp == LE_EXPR
4301            || cmp == GT_EXPR
4302            || cmp == GE_EXPR)
4303           && !REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
4304           /* Give up for -frounding-math.  */
4305           && !HONOR_SIGN_DEPENDENT_ROUNDING (TREE_TYPE (@0)))
4306       (with
4307        {
4308          REAL_VALUE_TYPE c2;
4309          enum tree_code ncmp = cmp;
4310          const real_format *fmt
4311            = REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (@0)));
4312          real_arithmetic (&c2, MULT_EXPR,
4313                           &TREE_REAL_CST (@1), &TREE_REAL_CST (@1));
4314          real_convert (&c2, fmt, &c2);
4315          /* See PR91734: if c2 is inexact and sqrt(c2) < c (or sqrt(c2) >= c),
4316             then change LT_EXPR into LE_EXPR or GE_EXPR into GT_EXPR.  */
4317          if (!REAL_VALUE_ISINF (c2))
4318            {
4319              tree c3 = fold_const_call (CFN_SQRT, TREE_TYPE (@0),
4320                                         build_real (TREE_TYPE (@0), c2));
4321              if (c3 == NULL_TREE || TREE_CODE (c3) != REAL_CST)
4322                ncmp = ERROR_MARK;
4323              else if ((cmp == LT_EXPR || cmp == GE_EXPR)
4324                       && real_less (&TREE_REAL_CST (c3), &TREE_REAL_CST (@1)))
4325                ncmp = cmp == LT_EXPR ? LE_EXPR : GT_EXPR;
4326              else if ((cmp == LE_EXPR || cmp == GT_EXPR)
4327                       && real_less (&TREE_REAL_CST (@1), &TREE_REAL_CST (c3)))
4328                ncmp = cmp == LE_EXPR ? LT_EXPR : GE_EXPR;
4329              else
4330                {
4331                  /* With rounding to even, sqrt of up to 3 different values
4332                     gives the same normal result, so in some cases c2 needs
4333                     to be adjusted.  */
4334                  REAL_VALUE_TYPE c2alt, tow;
4335                  if (cmp == LT_EXPR || cmp == GE_EXPR)
4336                    tow = dconst0;
4337                  else
4338                    real_inf (&tow);
4339                  real_nextafter (&c2alt, fmt, &c2, &tow);
4340                  real_convert (&c2alt, fmt, &c2alt);
4341                  if (REAL_VALUE_ISINF (c2alt))
4342                    ncmp = ERROR_MARK;
4343                  else
4344                    {
4345                      c3 = fold_const_call (CFN_SQRT, TREE_TYPE (@0),
4346                                            build_real (TREE_TYPE (@0), c2alt));
4347                      if (c3 == NULL_TREE || TREE_CODE (c3) != REAL_CST)
4348                        ncmp = ERROR_MARK;
4349                      else if (real_equal (&TREE_REAL_CST (c3),
4350                                           &TREE_REAL_CST (@1)))
4351                        c2 = c2alt;
4352                    }
4353                }
4354            }
4355        }
4356        (if (cmp == GT_EXPR || cmp == GE_EXPR)
4357         (if (REAL_VALUE_ISINF (c2))
4358          /* sqrt(x) > y is x == +Inf, when y is very large.  */
4359          (if (HONOR_INFINITIES (@0))
4360           (eq @0 { build_real (TREE_TYPE (@0), c2); })
4361           { constant_boolean_node (false, type); })
4362          /* sqrt(x) > c is the same as x > c*c.  */
4363          (if (ncmp != ERROR_MARK)
4364           (if (ncmp == GE_EXPR)
4365            (ge @0 { build_real (TREE_TYPE (@0), c2); })
4366            (gt @0 { build_real (TREE_TYPE (@0), c2); }))))
4367         /* else if (cmp == LT_EXPR || cmp == LE_EXPR)  */
4368         (if (REAL_VALUE_ISINF (c2))
4369          (switch
4370           /* sqrt(x) < y is always true, when y is a very large
4371              value and we don't care about NaNs or Infinities.  */
4372           (if (! HONOR_NANS (@0) && ! HONOR_INFINITIES (@0))
4373            { constant_boolean_node (true, type); })
4374           /* sqrt(x) < y is x != +Inf when y is very large and we
4375              don't care about NaNs.  */
4376           (if (! HONOR_NANS (@0))
4377            (ne @0 { build_real (TREE_TYPE (@0), c2); }))
4378           /* sqrt(x) < y is x >= 0 when y is very large and we
4379              don't care about Infinities.  */
4380           (if (! HONOR_INFINITIES (@0))
4381            (ge @0 { build_real (TREE_TYPE (@0), dconst0); }))
4382           /* sqrt(x) < y is x >= 0 && x != +Inf, when y is large.  */
4383           (if (GENERIC)
4384            (truth_andif
4385             (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
4386             (ne @0 { build_real (TREE_TYPE (@0), c2); }))))
4387          /* sqrt(x) < c is the same as x < c*c, if we ignore NaNs.  */
4388          (if (ncmp != ERROR_MARK && ! HONOR_NANS (@0))
4389           (if (ncmp == LT_EXPR)
4390            (lt @0 { build_real (TREE_TYPE (@0), c2); })
4391            (le @0 { build_real (TREE_TYPE (@0), c2); }))
4392           /* sqrt(x) < c is the same as x >= 0 && x < c*c.  */
4393           (if (ncmp != ERROR_MARK && GENERIC)
4394            (if (ncmp == LT_EXPR)
4395             (truth_andif
4396              (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
4397              (lt @0 { build_real (TREE_TYPE (@0), c2); }))
4398             (truth_andif
4399              (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
4400              (le @0 { build_real (TREE_TYPE (@0), c2); })))))))))))
4401    /* Transform sqrt(x) cmp sqrt(y) -> x cmp y.  */
4402    (simplify
4403     (cmp (sq @0) (sq @1))
4404       (if (! HONOR_NANS (@0))
4405         (cmp @0 @1))))))
4407 /* Optimize various special cases of (FTYPE) N CMP (FTYPE) M.  */
4408 (for cmp  (lt le eq ne ge gt unordered ordered unlt unle ungt unge uneq ltgt)
4409      icmp (lt le eq ne ge gt unordered ordered lt   le   gt   ge   eq   ne)
4410  (simplify
4411   (cmp (float@0 @1) (float @2))
4412    (if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (@0))
4413         && ! DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0)))
4414     (with
4415      {
4416        format_helper fmt (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (@0))));
4417        tree type1 = TREE_TYPE (@1);
4418        bool type1_signed_p = TYPE_SIGN (type1) == SIGNED;
4419        tree type2 = TREE_TYPE (@2);
4420        bool type2_signed_p = TYPE_SIGN (type2) == SIGNED;
4421      }
4422      (if (fmt.can_represent_integral_type_p (type1)
4423           && fmt.can_represent_integral_type_p (type2))
4424       (if (cmp == ORDERED_EXPR || cmp == UNORDERED_EXPR)
4425        { constant_boolean_node (cmp == ORDERED_EXPR, type); }
4426        (if (TYPE_PRECISION (type1) > TYPE_PRECISION (type2)
4427             && type1_signed_p >= type2_signed_p)
4428         (icmp @1 (convert @2))
4429         (if (TYPE_PRECISION (type1) < TYPE_PRECISION (type2)
4430              && type1_signed_p <= type2_signed_p)
4431          (icmp (convert:type2 @1) @2)
4432          (if (TYPE_PRECISION (type1) == TYPE_PRECISION (type2)
4433               && type1_signed_p == type2_signed_p)
4434           (icmp @1 @2))))))))))
4436 /* Optimize various special cases of (FTYPE) N CMP CST.  */
4437 (for cmp  (lt le eq ne ge gt)
4438      icmp (le le eq ne ge ge)
4439  (simplify
4440   (cmp (float @0) REAL_CST@1)
4441    (if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (@1))
4442         && ! DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1)))
4443     (with
4444      {
4445        tree itype = TREE_TYPE (@0);
4446        format_helper fmt (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (@1))));
4447        const REAL_VALUE_TYPE *cst = TREE_REAL_CST_PTR (@1);
4448        /* Be careful to preserve any potential exceptions due to
4449           NaNs.  qNaNs are ok in == or != context.
4450           TODO: relax under -fno-trapping-math or
4451           -fno-signaling-nans.  */
4452        bool exception_p
4453          = real_isnan (cst) && (cst->signalling
4454                                 || (cmp != EQ_EXPR && cmp != NE_EXPR));
4455      }
4456      /* TODO: allow non-fitting itype and SNaNs when
4457         -fno-trapping-math.  */
4458      (if (fmt.can_represent_integral_type_p (itype) && ! exception_p)
4459       (with
4460        {
4461          signop isign = TYPE_SIGN (itype);
4462          REAL_VALUE_TYPE imin, imax;
4463          real_from_integer (&imin, fmt, wi::min_value (itype), isign);
4464          real_from_integer (&imax, fmt, wi::max_value (itype), isign);
4466          REAL_VALUE_TYPE icst;
4467          if (cmp == GT_EXPR || cmp == GE_EXPR)
4468            real_ceil (&icst, fmt, cst);
4469          else if (cmp == LT_EXPR || cmp == LE_EXPR)
4470            real_floor (&icst, fmt, cst);
4471          else
4472            real_trunc (&icst, fmt, cst);
4474          bool cst_int_p = !real_isnan (cst) && real_identical (&icst, cst);
4476          bool overflow_p = false;
4477          wide_int icst_val
4478            = real_to_integer (&icst, &overflow_p, TYPE_PRECISION (itype));
4479        }
4480        (switch
4481         /* Optimize cases when CST is outside of ITYPE's range.  */
4482         (if (real_compare (LT_EXPR, cst, &imin))
4483          { constant_boolean_node (cmp == GT_EXPR || cmp == GE_EXPR || cmp == NE_EXPR,
4484                                   type); })
4485         (if (real_compare (GT_EXPR, cst, &imax))
4486          { constant_boolean_node (cmp == LT_EXPR || cmp == LE_EXPR || cmp == NE_EXPR,
4487                                   type); })
4488         /* Remove cast if CST is an integer representable by ITYPE.  */
4489         (if (cst_int_p)
4490          (cmp @0 { gcc_assert (!overflow_p);
4491                    wide_int_to_tree (itype, icst_val); })
4492         )
4493         /* When CST is fractional, optimize
4494             (FTYPE) N == CST -> 0
4495             (FTYPE) N != CST -> 1.  */
4496         (if (cmp == EQ_EXPR || cmp == NE_EXPR)
4497          { constant_boolean_node (cmp == NE_EXPR, type); })
4498         /* Otherwise replace with sensible integer constant.  */
4499         (with
4500          {
4501            gcc_checking_assert (!overflow_p);
4502          }
4503          (icmp @0 { wide_int_to_tree (itype, icst_val); })))))))))
4505 /* Fold A /[ex] B CMP C to A CMP B * C.  */
4506 (for cmp (eq ne)
4507  (simplify
4508   (cmp (exact_div @0 @1) INTEGER_CST@2)
4509   (if (!integer_zerop (@1))
4510    (if (wi::to_wide (@2) == 0)
4511     (cmp @0 @2)
4512     (if (TREE_CODE (@1) == INTEGER_CST)
4513      (with
4514       {
4515         wi::overflow_type ovf;
4516         wide_int prod = wi::mul (wi::to_wide (@2), wi::to_wide (@1),
4517                                  TYPE_SIGN (TREE_TYPE (@1)), &ovf);
4518       }
4519       (if (ovf)
4520        { constant_boolean_node (cmp == NE_EXPR, type); }
4521        (cmp @0 { wide_int_to_tree (TREE_TYPE (@0), prod); }))))))))
4522 (for cmp (lt le gt ge)
4523  (simplify
4524   (cmp (exact_div @0 INTEGER_CST@1) INTEGER_CST@2)
4525   (if (wi::gt_p (wi::to_wide (@1), 0, TYPE_SIGN (TREE_TYPE (@1))))
4526    (with
4527     {
4528       wi::overflow_type ovf;
4529       wide_int prod = wi::mul (wi::to_wide (@2), wi::to_wide (@1),
4530                                TYPE_SIGN (TREE_TYPE (@1)), &ovf);
4531     }
4532     (if (ovf)
4533      { constant_boolean_node (wi::lt_p (wi::to_wide (@2), 0,
4534                                         TYPE_SIGN (TREE_TYPE (@2)))
4535                               != (cmp == LT_EXPR || cmp == LE_EXPR), type); }
4536      (cmp @0 { wide_int_to_tree (TREE_TYPE (@0), prod); }))))))
4538 /* Fold (size_t)(A /[ex] B) CMP C to (size_t)A CMP (size_t)B * C or A CMP' 0.
4540    For small C (less than max/B), this is (size_t)A CMP (size_t)B * C.
4541    For large C (more than min/B+2^size), this is also true, with the
4542    multiplication computed modulo 2^size.
4543    For intermediate C, this just tests the sign of A.  */
4544 (for cmp  (lt le gt ge)
4545      cmp2 (ge ge lt lt)
4546  (simplify
4547   (cmp (convert (exact_div @0 INTEGER_CST@1)) INTEGER_CST@2)
4548   (if (tree_nop_conversion_p (TREE_TYPE (@0), TREE_TYPE (@2))
4549        && TYPE_UNSIGNED (TREE_TYPE (@2)) && !TYPE_UNSIGNED (TREE_TYPE (@0))
4550        && wi::gt_p (wi::to_wide (@1), 0, TYPE_SIGN (TREE_TYPE (@1))))
4551    (with
4552     {
4553       tree utype = TREE_TYPE (@2);
4554       wide_int denom = wi::to_wide (@1);
4555       wide_int right = wi::to_wide (@2);
4556       wide_int smax = wi::sdiv_trunc (wi::max_value (TREE_TYPE (@0)), denom);
4557       wide_int smin = wi::sdiv_trunc (wi::min_value (TREE_TYPE (@0)), denom);
4558       bool small = wi::leu_p (right, smax);
4559       bool large = wi::geu_p (right, smin);
4560     }
4561     (if (small || large)
4562      (cmp (convert:utype @0) (mult @2 (convert @1)))
4563      (cmp2 @0 { build_zero_cst (TREE_TYPE (@0)); }))))))
4565 /* Unordered tests if either argument is a NaN.  */
4566 (simplify
4567  (bit_ior (unordered @0 @0) (unordered @1 @1))
4568  (if (types_match (@0, @1))
4569   (unordered @0 @1)))
4570 (simplify
4571  (bit_and (ordered @0 @0) (ordered @1 @1))
4572  (if (types_match (@0, @1))
4573   (ordered @0 @1)))
4574 (simplify
4575  (bit_ior:c (unordered @0 @0) (unordered:c@2 @0 @1))
4576  @2)
4577 (simplify
4578  (bit_and:c (ordered @0 @0) (ordered:c@2 @0 @1))
4579  @2)
4581 /* Simple range test simplifications.  */
4582 /* A < B || A >= B -> true.  */
4583 (for test1 (lt le le le ne ge)
4584      test2 (ge gt ge ne eq ne)
4585  (simplify
4586   (bit_ior:c (test1 @0 @1) (test2 @0 @1))
4587   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
4588        || VECTOR_INTEGER_TYPE_P (TREE_TYPE (@0)))
4589    { constant_boolean_node (true, type); })))
4590 /* A < B && A >= B -> false.  */
4591 (for test1 (lt lt lt le ne eq)
4592      test2 (ge gt eq gt eq gt)
4593  (simplify
4594   (bit_and:c (test1 @0 @1) (test2 @0 @1))
4595   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
4596        || VECTOR_INTEGER_TYPE_P (TREE_TYPE (@0)))
4597    { constant_boolean_node (false, type); })))
4599 /* A & (2**N - 1) <= 2**K - 1 -> A & (2**N - 2**K) == 0
4600    A & (2**N - 1) >  2**K - 1 -> A & (2**N - 2**K) != 0
4602    Note that comparisons
4603      A & (2**N - 1) <  2**K   -> A & (2**N - 2**K) == 0
4604      A & (2**N - 1) >= 2**K   -> A & (2**N - 2**K) != 0
4605    will be canonicalized to above so there's no need to
4606    consider them here.
4607  */
4609 (for cmp (le gt)
4610      eqcmp (eq ne)
4611  (simplify
4612   (cmp (bit_and@0 @1 INTEGER_CST@2) INTEGER_CST@3)
4613   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)))
4614    (with
4615     {
4616      tree ty = TREE_TYPE (@0);
4617      unsigned prec = TYPE_PRECISION (ty);
4618      wide_int mask = wi::to_wide (@2, prec);
4619      wide_int rhs = wi::to_wide (@3, prec);
4620      signop sgn = TYPE_SIGN (ty);
4621     }
4622     (if ((mask & (mask + 1)) == 0 && wi::gt_p (rhs, 0, sgn)
4623          && (rhs & (rhs + 1)) == 0 && wi::ge_p (mask, rhs, sgn))
4624       (eqcmp (bit_and @1 { wide_int_to_tree (ty, mask - rhs); })
4625              { build_zero_cst (ty); }))))))
4627 /* -A CMP -B -> B CMP A.  */
4628 (for cmp (tcc_comparison)
4629      scmp (swapped_tcc_comparison)
4630  (simplify
4631   (cmp (negate @0) (negate @1))
4632   (if (FLOAT_TYPE_P (TREE_TYPE (@0))
4633        || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
4634            && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
4635    (scmp @0 @1)))
4636  (simplify
4637   (cmp (negate @0) CONSTANT_CLASS_P@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    (with { tree tem = const_unop (NEGATE_EXPR, TREE_TYPE (@0), @1); }
4642     (if (tem && !TREE_OVERFLOW (tem))
4643      (scmp @0 { tem; }))))))
4645 /* Convert ABS_EXPR<x> == 0 or ABS_EXPR<x> != 0 to x == 0 or x != 0.  */
4646 (for op (eq ne)
4647  (simplify
4648   (op (abs @0) zerop@1)
4649   (op @0 @1)))
4651 /* From fold_sign_changed_comparison and fold_widened_comparison.
4652    FIXME: the lack of symmetry is disturbing.  */
4653 (for cmp (simple_comparison)
4654  (simplify
4655   (cmp (convert@0 @00) (convert?@1 @10))
4656   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
4657        /* Disable this optimization if we're casting a function pointer
4658           type on targets that require function pointer canonicalization.  */
4659        && !(targetm.have_canonicalize_funcptr_for_compare ()
4660             && ((POINTER_TYPE_P (TREE_TYPE (@00))
4661                  && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@00))))
4662                 || (POINTER_TYPE_P (TREE_TYPE (@10))
4663                     && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@10))))))
4664        && single_use (@0))
4665    (if (TYPE_PRECISION (TREE_TYPE (@00)) == TYPE_PRECISION (TREE_TYPE (@0))
4666         && (TREE_CODE (@10) == INTEGER_CST
4667             || @1 != @10)
4668         && (TYPE_UNSIGNED (TREE_TYPE (@00)) == TYPE_UNSIGNED (TREE_TYPE (@0))
4669             || cmp == NE_EXPR
4670             || cmp == EQ_EXPR)
4671         && !POINTER_TYPE_P (TREE_TYPE (@00)))
4672     /* ???  The special-casing of INTEGER_CST conversion was in the original
4673        code and here to avoid a spurious overflow flag on the resulting
4674        constant which fold_convert produces.  */
4675     (if (TREE_CODE (@1) == INTEGER_CST)
4676      (cmp @00 { force_fit_type (TREE_TYPE (@00), wi::to_widest (@1), 0,
4677                                 TREE_OVERFLOW (@1)); })
4678      (cmp @00 (convert @1)))
4680     (if (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (TREE_TYPE (@00)))
4681      /* If possible, express the comparison in the shorter mode.  */
4682      (if ((cmp == EQ_EXPR || cmp == NE_EXPR
4683            || TYPE_UNSIGNED (TREE_TYPE (@0)) == TYPE_UNSIGNED (TREE_TYPE (@00))
4684            || (!TYPE_UNSIGNED (TREE_TYPE (@0))
4685                && TYPE_UNSIGNED (TREE_TYPE (@00))))
4686           && (types_match (TREE_TYPE (@10), TREE_TYPE (@00))
4687               || ((TYPE_PRECISION (TREE_TYPE (@00))
4688                    >= TYPE_PRECISION (TREE_TYPE (@10)))
4689                   && (TYPE_UNSIGNED (TREE_TYPE (@00))
4690                       == TYPE_UNSIGNED (TREE_TYPE (@10))))
4691               || (TREE_CODE (@10) == INTEGER_CST
4692                   && INTEGRAL_TYPE_P (TREE_TYPE (@00))
4693                   && int_fits_type_p (@10, TREE_TYPE (@00)))))
4694       (cmp @00 (convert @10))
4695       (if (TREE_CODE (@10) == INTEGER_CST
4696            && INTEGRAL_TYPE_P (TREE_TYPE (@00))
4697            && !int_fits_type_p (@10, TREE_TYPE (@00)))
4698        (with
4699         {
4700           tree min = lower_bound_in_type (TREE_TYPE (@10), TREE_TYPE (@00));
4701           tree max = upper_bound_in_type (TREE_TYPE (@10), TREE_TYPE (@00));
4702           bool above = integer_nonzerop (const_binop (LT_EXPR, type, max, @10));
4703           bool below = integer_nonzerop (const_binop (LT_EXPR, type, @10, min));
4704         }
4705         (if (above || below)
4706          (if (cmp == EQ_EXPR || cmp == NE_EXPR)
4707           { constant_boolean_node (cmp == EQ_EXPR ? false : true, type); }
4708           (if (cmp == LT_EXPR || cmp == LE_EXPR)
4709            { constant_boolean_node (above ? true : false, type); }
4710            (if (cmp == GT_EXPR || cmp == GE_EXPR)
4711             { constant_boolean_node (above ? false : true, type); }))))))))))))
4713 (for cmp (eq ne)
4714  (simplify
4715   /* SSA names are canonicalized to 2nd place.  */
4716   (cmp addr@0 SSA_NAME@1)
4717   (with
4718    { poly_int64 off; tree base; }
4719    /* A local variable can never be pointed to by
4720       the default SSA name of an incoming parameter.  */
4721    (if (SSA_NAME_IS_DEFAULT_DEF (@1)
4722         && TREE_CODE (SSA_NAME_VAR (@1)) == PARM_DECL
4723         && (base = get_base_address (TREE_OPERAND (@0, 0)))
4724         && TREE_CODE (base) == VAR_DECL
4725         && auto_var_in_fn_p (base, current_function_decl))
4726     (if (cmp == NE_EXPR)
4727      { constant_boolean_node (true, type); }
4728      { constant_boolean_node (false, type); })
4729     /* If the address is based on @1 decide using the offset.  */
4730     (if ((base = get_addr_base_and_unit_offset (TREE_OPERAND (@0, 0), &off))
4731          && TREE_CODE (base) == MEM_REF
4732          && TREE_OPERAND (base, 0) == @1)
4733      (with { off += mem_ref_offset (base).force_shwi (); }
4734       (if (known_ne (off, 0))
4735        { constant_boolean_node (cmp == NE_EXPR, type); }
4736        (if (known_eq (off, 0))
4737         { constant_boolean_node (cmp == EQ_EXPR, type); }))))))))
4739 /* Equality compare simplifications from fold_binary  */
4740 (for cmp (eq ne)
4742  /* If we have (A | C) == D where C & ~D != 0, convert this into 0.
4743     Similarly for NE_EXPR.  */
4744  (simplify
4745   (cmp (convert?@3 (bit_ior @0 INTEGER_CST@1)) INTEGER_CST@2)
4746   (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0))
4747        && wi::bit_and_not (wi::to_wide (@1), wi::to_wide (@2)) != 0)
4748    { constant_boolean_node (cmp == NE_EXPR, type); }))
4750  /* (X ^ Y) == 0 becomes X == Y, and (X ^ Y) != 0 becomes X != Y.  */
4751  (simplify
4752   (cmp (bit_xor @0 @1) integer_zerop)
4753   (cmp @0 @1))
4755  /* (X ^ Y) == Y becomes X == 0.
4756     Likewise (X ^ Y) == X becomes Y == 0.  */
4757  (simplify
4758   (cmp:c (bit_xor:c @0 @1) @0)
4759   (cmp @1 { build_zero_cst (TREE_TYPE (@1)); }))
4761  /* (X ^ C1) op C2 can be rewritten as X op (C1 ^ C2).  */
4762  (simplify
4763   (cmp (convert?@3 (bit_xor @0 INTEGER_CST@1)) INTEGER_CST@2)
4764   (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0)))
4765    (cmp @0 (bit_xor @1 (convert @2)))))
4767  (simplify
4768   (cmp (convert? addr@0) integer_zerop)
4769   (if (tree_single_nonzero_warnv_p (@0, NULL))
4770    { constant_boolean_node (cmp == NE_EXPR, type); }))
4772  /* (X & C) op (Y & C) into (X ^ Y) & C op 0.  */
4773  (simplify
4774   (cmp (bit_and:cs @0 @2) (bit_and:cs @1 @2))
4775   (cmp (bit_and (bit_xor @0 @1) @2) { build_zero_cst (TREE_TYPE (@2)); })))
4777 /* (X < 0) != (Y < 0) into (X ^ Y) < 0.
4778    (X >= 0) != (Y >= 0) into (X ^ Y) < 0.
4779    (X < 0) == (Y < 0) into (X ^ Y) >= 0.
4780    (X >= 0) == (Y >= 0) into (X ^ Y) >= 0.  */
4781 (for cmp (eq ne)
4782      ncmp (ge lt)
4783  (for sgncmp (ge lt)
4784   (simplify
4785    (cmp (sgncmp @0 integer_zerop@2) (sgncmp @1 integer_zerop))
4786    (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
4787         && !TYPE_UNSIGNED (TREE_TYPE (@0))
4788         && types_match (@0, @1))
4789     (ncmp (bit_xor @0 @1) @2)))))
4790 /* (X < 0) == (Y >= 0) into (X ^ Y) < 0.
4791    (X < 0) != (Y >= 0) into (X ^ Y) >= 0.  */
4792 (for cmp (eq ne)
4793      ncmp (lt ge)
4794  (simplify
4795   (cmp:c (lt @0 integer_zerop@2) (ge @1 integer_zerop))
4796    (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
4797         && !TYPE_UNSIGNED (TREE_TYPE (@0))
4798         && types_match (@0, @1))
4799     (ncmp (bit_xor @0 @1) @2))))
4801 /* If we have (A & C) == C where C is a power of 2, convert this into
4802    (A & C) != 0.  Similarly for NE_EXPR.  */
4803 (for cmp (eq ne)
4804      icmp (ne eq)
4805  (simplify
4806   (cmp (bit_and@2 @0 integer_pow2p@1) @1)
4807   (icmp @2 { build_zero_cst (TREE_TYPE (@0)); })))
4809 /* If we have (A & C) != 0 ? D : 0 where C and D are powers of 2,
4810    convert this into a shift followed by ANDing with D.  */
4811 (simplify
4812  (cond
4813   (ne (bit_and @0 integer_pow2p@1) integer_zerop)
4814   INTEGER_CST@2 integer_zerop)
4815  (if (integer_pow2p (@2))
4816   (with {
4817      int shift = (wi::exact_log2 (wi::to_wide (@2))
4818                   - wi::exact_log2 (wi::to_wide (@1)));
4819    }
4820    (if (shift > 0)
4821     (bit_and
4822      (lshift (convert @0) { build_int_cst (integer_type_node, shift); }) @2)
4823     (bit_and
4824      (convert (rshift @0 { build_int_cst (integer_type_node, -shift); }))
4825      @2)))))
4827 /* If we have (A & C) != 0 where C is the sign bit of A, convert
4828    this into A < 0.  Similarly for (A & C) == 0 into A >= 0.  */
4829 (for cmp (eq ne)
4830      ncmp (ge lt)
4831  (simplify
4832   (cmp (bit_and (convert?@2 @0) integer_pow2p@1) integer_zerop)
4833   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
4834        && type_has_mode_precision_p (TREE_TYPE (@0))
4835        && element_precision (@2) >= element_precision (@0)
4836        && wi::only_sign_bit_p (wi::to_wide (@1), element_precision (@0)))
4837    (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
4838     (ncmp (convert:stype @0) { build_zero_cst (stype); })))))
4840 /* If we have A < 0 ? C : 0 where C is a power of 2, convert
4841    this into a right shift or sign extension followed by ANDing with C.  */
4842 (simplify
4843  (cond
4844   (lt @0 integer_zerop)
4845   INTEGER_CST@1 integer_zerop)
4846  (if (integer_pow2p (@1)
4847       && !TYPE_UNSIGNED (TREE_TYPE (@0)))
4848   (with {
4849     int shift = element_precision (@0) - wi::exact_log2 (wi::to_wide (@1)) - 1;
4850    }
4851    (if (shift >= 0)
4852     (bit_and
4853      (convert (rshift @0 { build_int_cst (integer_type_node, shift); }))
4854      @1)
4855     /* Otherwise ctype must be wider than TREE_TYPE (@0) and pure
4856        sign extension followed by AND with C will achieve the effect.  */
4857     (bit_and (convert @0) @1)))))
4859 /* When the addresses are not directly of decls compare base and offset.
4860    This implements some remaining parts of fold_comparison address
4861    comparisons but still no complete part of it.  Still it is good
4862    enough to make fold_stmt not regress when not dispatching to fold_binary.  */
4863 (for cmp (simple_comparison)
4864  (simplify
4865   (cmp (convert1?@2 addr@0) (convert2? addr@1))
4866   (with
4867    {
4868      poly_int64 off0, off1;
4869      tree base0 = get_addr_base_and_unit_offset (TREE_OPERAND (@0, 0), &off0);
4870      tree base1 = get_addr_base_and_unit_offset (TREE_OPERAND (@1, 0), &off1);
4871      if (base0 && TREE_CODE (base0) == MEM_REF)
4872        {
4873          off0 += mem_ref_offset (base0).force_shwi ();
4874          base0 = TREE_OPERAND (base0, 0);
4875        }
4876      if (base1 && TREE_CODE (base1) == MEM_REF)
4877        {
4878          off1 += mem_ref_offset (base1).force_shwi ();
4879          base1 = TREE_OPERAND (base1, 0);
4880        }
4881    }
4882    (if (base0 && base1)
4883     (with
4884      {
4885        int equal = 2;
4886        /* Punt in GENERIC on variables with value expressions;
4887           the value expressions might point to fields/elements
4888           of other vars etc.  */
4889        if (GENERIC
4890            && ((VAR_P (base0) && DECL_HAS_VALUE_EXPR_P (base0))
4891                || (VAR_P (base1) && DECL_HAS_VALUE_EXPR_P (base1))))
4892          ;
4893        else if (decl_in_symtab_p (base0)
4894                 && decl_in_symtab_p (base1))
4895          equal = symtab_node::get_create (base0)
4896                    ->equal_address_to (symtab_node::get_create (base1));
4897        else if ((DECL_P (base0)
4898                  || TREE_CODE (base0) == SSA_NAME
4899                  || TREE_CODE (base0) == STRING_CST)
4900                 && (DECL_P (base1)
4901                     || TREE_CODE (base1) == SSA_NAME
4902                     || TREE_CODE (base1) == STRING_CST))
4903          equal = (base0 == base1);
4904        if (equal == 0)
4905          {
4906            HOST_WIDE_INT ioff0 = -1, ioff1 = -1;
4907            off0.is_constant (&ioff0);
4908            off1.is_constant (&ioff1);
4909            if ((DECL_P (base0) && TREE_CODE (base1) == STRING_CST)
4910                || (TREE_CODE (base0) == STRING_CST && DECL_P (base1))
4911                || (TREE_CODE (base0) == STRING_CST
4912                    && TREE_CODE (base1) == STRING_CST
4913                    && ioff0 >= 0 && ioff1 >= 0
4914                    && ioff0 < TREE_STRING_LENGTH (base0)
4915                    && ioff1 < TREE_STRING_LENGTH (base1)
4916                    /* This is a too conservative test that the STRING_CSTs
4917                       will not end up being string-merged.  */
4918                    && strncmp (TREE_STRING_POINTER (base0) + ioff0,
4919                                TREE_STRING_POINTER (base1) + ioff1,
4920                                MIN (TREE_STRING_LENGTH (base0) - ioff0,
4921                                     TREE_STRING_LENGTH (base1) - ioff1)) != 0))
4922              ;
4923            else if (!DECL_P (base0) || !DECL_P (base1))
4924              equal = 2;
4925            else if (cmp != EQ_EXPR && cmp != NE_EXPR)
4926              equal = 2;
4927            /* If this is a pointer comparison, ignore for now even
4928               valid equalities where one pointer is the offset zero
4929               of one object and the other to one past end of another one.  */
4930            else if (!INTEGRAL_TYPE_P (TREE_TYPE (@2)))
4931              ;
4932            /* Assume that automatic variables can't be adjacent to global
4933               variables.  */
4934            else if (is_global_var (base0) != is_global_var (base1))
4935              ;
4936            else
4937              {
4938                tree sz0 = DECL_SIZE_UNIT (base0);
4939                tree sz1 = DECL_SIZE_UNIT (base1);
4940                /* If sizes are unknown, e.g. VLA or not representable,
4941                   punt.  */
4942                if (!tree_fits_poly_int64_p (sz0)
4943                    || !tree_fits_poly_int64_p (sz1))
4944                  equal = 2;
4945                else
4946                  {
4947                    poly_int64 size0 = tree_to_poly_int64 (sz0);
4948                    poly_int64 size1 = tree_to_poly_int64 (sz1);
4949                    /* If one offset is pointing (or could be) to the beginning
4950                       of one object and the other is pointing to one past the
4951                       last byte of the other object, punt.  */
4952                    if (maybe_eq (off0, 0) && maybe_eq (off1, size1))
4953                      equal = 2;
4954                    else if (maybe_eq (off1, 0) && maybe_eq (off0, size0))
4955                      equal = 2;
4956                    /* If both offsets are the same, there are some cases
4957                       we know that are ok.  Either if we know they aren't
4958                       zero, or if we know both sizes are no zero.  */
4959                    if (equal == 2
4960                        && known_eq (off0, off1)
4961                        && (known_ne (off0, 0)
4962                            || (known_ne (size0, 0) && known_ne (size1, 0))))
4963                      equal = 0;
4964                  }
4965              }
4966          }
4967      }
4968      (if (equal == 1
4969           && (cmp == EQ_EXPR || cmp == NE_EXPR
4970               /* If the offsets are equal we can ignore overflow.  */
4971               || known_eq (off0, off1)
4972               || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
4973                  /* Or if we compare using pointers to decls or strings.  */
4974               || (POINTER_TYPE_P (TREE_TYPE (@2))
4975                   && (DECL_P (base0) || TREE_CODE (base0) == STRING_CST))))
4976       (switch
4977        (if (cmp == EQ_EXPR && (known_eq (off0, off1) || known_ne (off0, off1)))
4978         { constant_boolean_node (known_eq (off0, off1), type); })
4979        (if (cmp == NE_EXPR && (known_eq (off0, off1) || known_ne (off0, off1)))
4980         { constant_boolean_node (known_ne (off0, off1), type); })
4981        (if (cmp == LT_EXPR && (known_lt (off0, off1) || known_ge (off0, off1)))
4982         { constant_boolean_node (known_lt (off0, off1), type); })
4983        (if (cmp == LE_EXPR && (known_le (off0, off1) || known_gt (off0, off1)))
4984         { constant_boolean_node (known_le (off0, off1), type); })
4985        (if (cmp == GE_EXPR && (known_ge (off0, off1) || known_lt (off0, off1)))
4986         { constant_boolean_node (known_ge (off0, off1), type); })
4987        (if (cmp == GT_EXPR && (known_gt (off0, off1) || known_le (off0, off1)))
4988         { constant_boolean_node (known_gt (off0, off1), type); }))
4989       (if (equal == 0)
4990         (switch
4991          (if (cmp == EQ_EXPR)
4992           { constant_boolean_node (false, type); })
4993          (if (cmp == NE_EXPR)
4994           { constant_boolean_node (true, type); })))))))))
4996 /* Simplify pointer equality compares using PTA.  */
4997 (for neeq (ne eq)
4998  (simplify
4999   (neeq @0 @1)
5000   (if (POINTER_TYPE_P (TREE_TYPE (@0))
5001        && ptrs_compare_unequal (@0, @1))
5002    { constant_boolean_node (neeq != EQ_EXPR, type); })))
5004 /* PR70920: Transform (intptr_t)x eq/ne CST to x eq/ne (typeof x) CST.
5005    and (typeof ptr_cst) x eq/ne ptr_cst to x eq/ne (typeof x) CST.
5006    Disable the transform if either operand is pointer to function.
5007    This broke pr22051-2.c for arm where function pointer
5008    canonicalizaion is not wanted.  */
5010 (for cmp (ne eq)
5011  (simplify
5012   (cmp (convert @0) INTEGER_CST@1)
5013   (if (((POINTER_TYPE_P (TREE_TYPE (@0))
5014          && !FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@0)))
5015          && INTEGRAL_TYPE_P (TREE_TYPE (@1)))
5016         || (INTEGRAL_TYPE_P (TREE_TYPE (@0))
5017             && POINTER_TYPE_P (TREE_TYPE (@1))
5018             && !FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@1)))))
5019        && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
5020    (cmp @0 (convert @1)))))
5022 /* Non-equality compare simplifications from fold_binary  */
5023 (for cmp (lt gt le ge)
5024  /* Comparisons with the highest or lowest possible integer of
5025     the specified precision will have known values.  */
5026  (simplify
5027   (cmp (convert?@2 @0) uniform_integer_cst_p@1)
5028   (if ((INTEGRAL_TYPE_P (TREE_TYPE (@1))
5029         || POINTER_TYPE_P (TREE_TYPE (@1))
5030         || VECTOR_INTEGER_TYPE_P (TREE_TYPE (@1)))
5031        && tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@0)))
5032    (with
5033     {
5034       tree cst = uniform_integer_cst_p (@1);
5035       tree arg1_type = TREE_TYPE (cst);
5036       unsigned int prec = TYPE_PRECISION (arg1_type);
5037       wide_int max = wi::max_value (arg1_type);
5038       wide_int signed_max = wi::max_value (prec, SIGNED);
5039       wide_int min = wi::min_value (arg1_type);
5040     }
5041     (switch
5042      (if (wi::to_wide (cst) == max)
5043       (switch
5044        (if (cmp == GT_EXPR)
5045         { constant_boolean_node (false, type); })
5046        (if (cmp == GE_EXPR)
5047         (eq @2 @1))
5048        (if (cmp == LE_EXPR)
5049         { constant_boolean_node (true, type); })
5050        (if (cmp == LT_EXPR)
5051         (ne @2 @1))))
5052      (if (wi::to_wide (cst) == min)
5053       (switch
5054        (if (cmp == LT_EXPR)
5055         { constant_boolean_node (false, type); })
5056        (if (cmp == LE_EXPR)
5057         (eq @2 @1))
5058        (if (cmp == GE_EXPR)
5059         { constant_boolean_node (true, type); })
5060        (if (cmp == GT_EXPR)
5061         (ne @2 @1))))
5062      (if (wi::to_wide (cst) == max - 1)
5063       (switch
5064        (if (cmp == GT_EXPR)
5065         (eq @2 { build_uniform_cst (TREE_TYPE (@1),
5066                                     wide_int_to_tree (TREE_TYPE (cst),
5067                                                       wi::to_wide (cst)
5068                                                       + 1)); }))
5069        (if (cmp == LE_EXPR)
5070         (ne @2 { build_uniform_cst (TREE_TYPE (@1),
5071                                     wide_int_to_tree (TREE_TYPE (cst),
5072                                                       wi::to_wide (cst)
5073                                                       + 1)); }))))
5074      (if (wi::to_wide (cst) == min + 1)
5075       (switch
5076        (if (cmp == GE_EXPR)
5077         (ne @2 { build_uniform_cst (TREE_TYPE (@1),
5078                                     wide_int_to_tree (TREE_TYPE (cst),
5079                                                       wi::to_wide (cst)
5080                                                       - 1)); }))
5081        (if (cmp == LT_EXPR)
5082         (eq @2 { build_uniform_cst (TREE_TYPE (@1),
5083                                     wide_int_to_tree (TREE_TYPE (cst),
5084                                                       wi::to_wide (cst)
5085                                                       - 1)); }))))
5086      (if (wi::to_wide (cst) == signed_max
5087           && TYPE_UNSIGNED (arg1_type)
5088           /* We will flip the signedness of the comparison operator
5089              associated with the mode of @1, so the sign bit is
5090              specified by this mode.  Check that @1 is the signed
5091              max associated with this sign bit.  */
5092           && prec == GET_MODE_PRECISION (SCALAR_INT_TYPE_MODE (arg1_type))
5093           /* signed_type does not work on pointer types.  */
5094           && INTEGRAL_TYPE_P (arg1_type))
5095       /* The following case also applies to X < signed_max+1
5096          and X >= signed_max+1 because previous transformations.  */
5097       (if (cmp == LE_EXPR || cmp == GT_EXPR)
5098        (with { tree st = signed_type_for (TREE_TYPE (@1)); }
5099         (switch
5100          (if (cst == @1 && cmp == LE_EXPR)
5101           (ge (convert:st @0) { build_zero_cst (st); }))
5102          (if (cst == @1 && cmp == GT_EXPR)
5103           (lt (convert:st @0) { build_zero_cst (st); }))
5104          (if (cmp == LE_EXPR)
5105           (ge (view_convert:st @0) { build_zero_cst (st); }))
5106          (if (cmp == GT_EXPR)
5107           (lt (view_convert:st @0) { build_zero_cst (st); })))))))))))
5109 (for cmp (unordered ordered unlt unle ungt unge uneq ltgt)
5110  /* If the second operand is NaN, the result is constant.  */
5111  (simplify
5112   (cmp @0 REAL_CST@1)
5113   (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
5114        && (cmp != LTGT_EXPR || ! flag_trapping_math))
5115    { constant_boolean_node (cmp == ORDERED_EXPR || cmp == LTGT_EXPR
5116                             ? false : true, type); })))
5118 /* Fold UNORDERED if either operand must be NaN, or neither can be.  */
5119 (simplify
5120   (unordered @0 @1)
5121   (switch
5122     (if (tree_expr_nan_p (@0) || tree_expr_nan_p (@1))
5123         { constant_boolean_node (true, type); })
5124     (if (!tree_expr_maybe_nan_p (@0) && !tree_expr_maybe_nan_p (@1))
5125         { constant_boolean_node (false, type); })))
5127 /* Fold ORDERED if either operand must be NaN, or neither can be.  */
5128 (simplify
5129   (ordered @0 @1)
5130   (switch
5131     (if (tree_expr_nan_p (@0) || tree_expr_nan_p (@1))
5132         { constant_boolean_node (false, type); })
5133     (if (!tree_expr_maybe_nan_p (@0) && !tree_expr_maybe_nan_p (@1))
5134         { constant_boolean_node (true, type); })))
5136 /* bool_var != 0 becomes bool_var.  */
5137 (simplify
5138  (ne @0 integer_zerop)
5139  (if (TREE_CODE (TREE_TYPE (@0)) == BOOLEAN_TYPE
5140       && types_match (type, TREE_TYPE (@0)))
5141   (non_lvalue @0)))
5142 /* bool_var == 1 becomes bool_var.  */
5143 (simplify
5144  (eq @0 integer_onep)
5145  (if (TREE_CODE (TREE_TYPE (@0)) == BOOLEAN_TYPE
5146       && types_match (type, TREE_TYPE (@0)))
5147   (non_lvalue @0)))
5148 /* Do not handle
5149    bool_var == 0 becomes !bool_var or
5150    bool_var != 1 becomes !bool_var
5151    here because that only is good in assignment context as long
5152    as we require a tcc_comparison in GIMPLE_CONDs where we'd
5153    replace if (x == 0) with tem = ~x; if (tem != 0) which is
5154    clearly less optimal and which we'll transform again in forwprop.  */
5156 /* When one argument is a constant, overflow detection can be simplified.
5157    Currently restricted to single use so as not to interfere too much with
5158    ADD_OVERFLOW detection in tree-ssa-math-opts.c.
5159    CONVERT?(CONVERT?(A) + CST) CMP A  ->  A CMP' CST' */
5160 (for cmp (lt le ge gt)
5161      out (gt gt le le)
5162  (simplify
5163   (cmp:c (convert?@3 (plus@2 (convert?@4 @0) INTEGER_CST@1)) @0)
5164   (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@2))
5165        && types_match (TREE_TYPE (@0), TREE_TYPE (@3))
5166        && tree_nop_conversion_p (TREE_TYPE (@4), TREE_TYPE (@0))
5167        && wi::to_wide (@1) != 0
5168        && single_use (@2))
5169    (with {
5170      unsigned int prec = TYPE_PRECISION (TREE_TYPE (@0));
5171      signop sign = TYPE_SIGN (TREE_TYPE (@0));
5172     }
5173     (out @0 { wide_int_to_tree (TREE_TYPE (@0),
5174                                 wi::max_value (prec, sign)
5175                                 - wi::to_wide (@1)); })))))
5177 /* To detect overflow in unsigned A - B, A < B is simpler than A - B > A.
5178    However, the detection logic for SUB_OVERFLOW in tree-ssa-math-opts.c
5179    expects the long form, so we restrict the transformation for now.  */
5180 (for cmp (gt le)
5181  (simplify
5182   (cmp:c (minus@2 @0 @1) @0)
5183   (if (single_use (@2)
5184        && ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
5185        && TYPE_UNSIGNED (TREE_TYPE (@0)))
5186    (cmp @1 @0))))
5188 /* Optimize A - B + -1 >= A into B >= A for unsigned comparisons.  */
5189 (for cmp (ge lt)
5190  (simplify
5191   (cmp:c (plus (minus @0 @1) integer_minus_onep) @0)
5192    (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
5193         && TYPE_UNSIGNED (TREE_TYPE (@0)))
5194     (cmp @1 @0))))
5196 /* Testing for overflow is unnecessary if we already know the result.  */
5197 /* A - B > A  */
5198 (for cmp (gt le)
5199      out (ne eq)
5200  (simplify
5201   (cmp:c (realpart (IFN_SUB_OVERFLOW@2 @0 @1)) @0)
5202   (if (TYPE_UNSIGNED (TREE_TYPE (@0))
5203        && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
5204    (out (imagpart @2) { build_zero_cst (TREE_TYPE (@0)); }))))
5205 /* A + B < A  */
5206 (for cmp (lt ge)
5207      out (ne eq)
5208  (simplify
5209   (cmp:c (realpart (IFN_ADD_OVERFLOW:c@2 @0 @1)) @0)
5210   (if (TYPE_UNSIGNED (TREE_TYPE (@0))
5211        && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
5212    (out (imagpart @2) { build_zero_cst (TREE_TYPE (@0)); }))))
5214 /* For unsigned operands, -1 / B < A checks whether A * B would overflow.
5215    Simplify it to __builtin_mul_overflow (A, B, <unused>).  */
5216 (for cmp (lt ge)
5217      out (ne eq)
5218  (simplify
5219   (cmp:c (trunc_div:s integer_all_onesp @1) @0)
5220   (if (TYPE_UNSIGNED (TREE_TYPE (@0)) && !VECTOR_TYPE_P (TREE_TYPE (@0)))
5221    (with { tree t = TREE_TYPE (@0), cpx = build_complex_type (t); }
5222     (out (imagpart (IFN_MUL_OVERFLOW:cpx @0 @1)) { build_zero_cst (t); })))))
5224 /* Similarly, for unsigned operands, (((type) A * B) >> prec) != 0 where type
5225    is at least twice as wide as type of A and B, simplify to
5226    __builtin_mul_overflow (A, B, <unused>).  */
5227 (for cmp (eq ne)
5228  (simplify
5229   (cmp (rshift (mult:s (convert@3 @0) (convert @1)) INTEGER_CST@2)
5230        integer_zerop)
5231   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
5232        && INTEGRAL_TYPE_P (TREE_TYPE (@3))
5233        && TYPE_UNSIGNED (TREE_TYPE (@0))
5234        && (TYPE_PRECISION (TREE_TYPE (@3))
5235            >= 2 * TYPE_PRECISION (TREE_TYPE (@0)))
5236        && tree_fits_uhwi_p (@2)
5237        && tree_to_uhwi (@2) == TYPE_PRECISION (TREE_TYPE (@0))
5238        && types_match (@0, @1)
5239        && type_has_mode_precision_p (TREE_TYPE (@0))
5240        && (optab_handler (umulv4_optab, TYPE_MODE (TREE_TYPE (@0)))
5241            != CODE_FOR_nothing))
5242    (with { tree t = TREE_TYPE (@0), cpx = build_complex_type (t); }
5243     (cmp (imagpart (IFN_MUL_OVERFLOW:cpx @0 @1)) { build_zero_cst (t); })))))
5245 /* Simplification of math builtins.  These rules must all be optimizations
5246    as well as IL simplifications.  If there is a possibility that the new
5247    form could be a pessimization, the rule should go in the canonicalization
5248    section that follows this one.
5250    Rules can generally go in this section if they satisfy one of
5251    the following:
5253    - the rule describes an identity
5255    - the rule replaces calls with something as simple as addition or
5256      multiplication
5258    - the rule contains unary calls only and simplifies the surrounding
5259      arithmetic.  (The idea here is to exclude non-unary calls in which
5260      one operand is constant and in which the call is known to be cheap
5261      when the operand has that value.)  */
5263 (if (flag_unsafe_math_optimizations)
5264  /* Simplify sqrt(x) * sqrt(x) -> x.  */
5265  (simplify
5266   (mult (SQRT_ALL@1 @0) @1)
5267   (if (!tree_expr_maybe_signaling_nan_p (@0))
5268    @0))
5270  (for op (plus minus)
5271   /* Simplify (A / C) +- (B / C) -> (A +- B) / C.  */
5272   (simplify
5273    (op (rdiv @0 @1)
5274        (rdiv @2 @1))
5275    (rdiv (op @0 @2) @1)))
5277  (for cmp (lt le gt ge)
5278       neg_cmp (gt ge lt le)
5279   /* Simplify (x * C1) cmp C2 -> x cmp (C2 / C1), where C1 != 0.  */
5280   (simplify
5281    (cmp (mult @0 REAL_CST@1) REAL_CST@2)
5282    (with
5283     { tree tem = const_binop (RDIV_EXPR, type, @2, @1); }
5284     (if (tem
5285          && !(REAL_VALUE_ISINF (TREE_REAL_CST (tem))
5286               || (real_zerop (tem) && !real_zerop (@1))))
5287      (switch
5288       (if (real_less (&dconst0, TREE_REAL_CST_PTR (@1)))
5289        (cmp @0 { tem; }))
5290       (if (real_less (TREE_REAL_CST_PTR (@1), &dconst0))
5291        (neg_cmp @0 { tem; })))))))
5293  /* Simplify sqrt(x) * sqrt(y) -> sqrt(x*y).  */
5294  (for root (SQRT CBRT)
5295   (simplify
5296    (mult (root:s @0) (root:s @1))
5297     (root (mult @0 @1))))
5299  /* Simplify expN(x) * expN(y) -> expN(x+y). */
5300  (for exps (EXP EXP2 EXP10 POW10)
5301   (simplify
5302    (mult (exps:s @0) (exps:s @1))
5303     (exps (plus @0 @1))))
5305  /* Simplify a/root(b/c) into a*root(c/b).  */
5306  (for root (SQRT CBRT)
5307   (simplify
5308    (rdiv @0 (root:s (rdiv:s @1 @2)))
5309     (mult @0 (root (rdiv @2 @1)))))
5311  /* Simplify x/expN(y) into x*expN(-y).  */
5312  (for exps (EXP EXP2 EXP10 POW10)
5313   (simplify
5314    (rdiv @0 (exps:s @1))
5315     (mult @0 (exps (negate @1)))))
5317  (for logs (LOG LOG2 LOG10 LOG10)
5318       exps (EXP EXP2 EXP10 POW10)
5319   /* logN(expN(x)) -> x.  */
5320   (simplify
5321    (logs (exps @0))
5322    @0)
5323   /* expN(logN(x)) -> x.  */
5324   (simplify
5325    (exps (logs @0))
5326    @0))
5328  /* Optimize logN(func()) for various exponential functions.  We
5329     want to determine the value "x" and the power "exponent" in
5330     order to transform logN(x**exponent) into exponent*logN(x).  */
5331  (for logs (LOG  LOG   LOG   LOG2 LOG2  LOG2  LOG10 LOG10)
5332       exps (EXP2 EXP10 POW10 EXP  EXP10 POW10 EXP   EXP2)
5333   (simplify
5334    (logs (exps @0))
5335    (if (SCALAR_FLOAT_TYPE_P (type))
5336     (with {
5337       tree x;
5338       switch (exps)
5339         {
5340         CASE_CFN_EXP:
5341           /* Prepare to do logN(exp(exponent)) -> exponent*logN(e).  */
5342           x = build_real_truncate (type, dconst_e ());
5343           break;
5344         CASE_CFN_EXP2:
5345           /* Prepare to do logN(exp2(exponent)) -> exponent*logN(2).  */
5346           x = build_real (type, dconst2);
5347           break;
5348         CASE_CFN_EXP10:
5349         CASE_CFN_POW10:
5350           /* Prepare to do logN(exp10(exponent)) -> exponent*logN(10).  */
5351           {
5352             REAL_VALUE_TYPE dconst10;
5353             real_from_integer (&dconst10, VOIDmode, 10, SIGNED);
5354             x = build_real (type, dconst10);
5355           }
5356           break;
5357         default:
5358           gcc_unreachable ();
5359         }
5360       }
5361      (mult (logs { x; }) @0)))))
5363  (for logs (LOG LOG
5364             LOG2 LOG2
5365             LOG10 LOG10)
5366       exps (SQRT CBRT)
5367   (simplify
5368    (logs (exps @0))
5369    (if (SCALAR_FLOAT_TYPE_P (type))
5370     (with {
5371       tree x;
5372       switch (exps)
5373         {
5374         CASE_CFN_SQRT:
5375           /* Prepare to do logN(sqrt(x)) -> 0.5*logN(x).  */
5376           x = build_real (type, dconsthalf);
5377           break;
5378         CASE_CFN_CBRT:
5379           /* Prepare to do logN(cbrt(x)) -> (1/3)*logN(x).  */
5380           x = build_real_truncate (type, dconst_third ());
5381           break;
5382         default:
5383           gcc_unreachable ();
5384         }
5385       }
5386      (mult { x; } (logs @0))))))
5388  /* logN(pow(x,exponent)) -> exponent*logN(x).  */
5389  (for logs (LOG LOG2 LOG10)
5390       pows (POW)
5391   (simplify
5392    (logs (pows @0 @1))
5393    (mult @1 (logs @0))))
5395  /* pow(C,x) -> exp(log(C)*x) if C > 0,
5396     or if C is a positive power of 2,
5397     pow(C,x) -> exp2(log2(C)*x).  */
5398 #if GIMPLE
5399  (for pows (POW)
5400       exps (EXP)
5401       logs (LOG)
5402       exp2s (EXP2)
5403       log2s (LOG2)
5404   (simplify
5405    (pows REAL_CST@0 @1)
5406    (if (real_compare (GT_EXPR, TREE_REAL_CST_PTR (@0), &dconst0)
5407         && real_isfinite (TREE_REAL_CST_PTR (@0))
5408         /* As libmvec doesn't have a vectorized exp2, defer optimizing
5409            the use_exp2 case until after vectorization.  It seems actually
5410            beneficial for all constants to postpone this until later,
5411            because exp(log(C)*x), while faster, will have worse precision
5412            and if x folds into a constant too, that is unnecessary
5413            pessimization.  */
5414         && canonicalize_math_after_vectorization_p ())
5415     (with {
5416        const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (@0);
5417        bool use_exp2 = false;
5418        if (targetm.libc_has_function (function_c99_misc, TREE_TYPE (@0))
5419            && value->cl == rvc_normal)
5420          {
5421            REAL_VALUE_TYPE frac_rvt = *value;
5422            SET_REAL_EXP (&frac_rvt, 1);
5423            if (real_equal (&frac_rvt, &dconst1))
5424              use_exp2 = true;
5425          }
5426      }
5427      (if (!use_exp2)
5428       (if (optimize_pow_to_exp (@0, @1))
5429        (exps (mult (logs @0) @1)))
5430       (exp2s (mult (log2s @0) @1)))))))
5431 #endif
5433  /* pow(C,x)*expN(y) -> expN(logN(C)*x+y) if C > 0.  */
5434  (for pows (POW)
5435       exps (EXP EXP2 EXP10 POW10)
5436       logs (LOG LOG2 LOG10 LOG10)
5437   (simplify
5438    (mult:c (pows:s REAL_CST@0 @1) (exps:s @2))
5439    (if (real_compare (GT_EXPR, TREE_REAL_CST_PTR (@0), &dconst0)
5440         && real_isfinite (TREE_REAL_CST_PTR (@0)))
5441     (exps (plus (mult (logs @0) @1) @2)))))
5443  (for sqrts (SQRT)
5444       cbrts (CBRT)
5445       pows (POW)
5446       exps (EXP EXP2 EXP10 POW10)
5447   /* sqrt(expN(x)) -> expN(x*0.5).  */
5448   (simplify
5449    (sqrts (exps @0))
5450    (exps (mult @0 { build_real (type, dconsthalf); })))
5451   /* cbrt(expN(x)) -> expN(x/3).  */
5452   (simplify
5453    (cbrts (exps @0))
5454    (exps (mult @0 { build_real_truncate (type, dconst_third ()); })))
5455   /* pow(expN(x), y) -> expN(x*y).  */
5456   (simplify
5457    (pows (exps @0) @1)
5458    (exps (mult @0 @1))))
5460  /* tan(atan(x)) -> x.  */
5461  (for tans (TAN)
5462       atans (ATAN)
5463   (simplify
5464    (tans (atans @0))
5465    @0)))
5467  /* Simplify sin(atan(x)) -> x / sqrt(x*x + 1). */
5468  (for sins (SIN)
5469       atans (ATAN)
5470       sqrts (SQRT)
5471       copysigns (COPYSIGN)
5472   (simplify
5473    (sins (atans:s @0))
5474    (with
5475      {
5476       REAL_VALUE_TYPE r_cst;
5477       build_sinatan_real (&r_cst, type);
5478       tree t_cst = build_real (type, r_cst);
5479       tree t_one = build_one_cst (type);
5480      }
5481     (if (SCALAR_FLOAT_TYPE_P (type))
5482      (cond (lt (abs @0) { t_cst; })
5483       (rdiv @0 (sqrts (plus (mult @0 @0) { t_one; })))
5484       (copysigns { t_one; } @0))))))
5486 /* Simplify cos(atan(x)) -> 1 / sqrt(x*x + 1). */
5487  (for coss (COS)
5488       atans (ATAN)
5489       sqrts (SQRT)
5490       copysigns (COPYSIGN)
5491   (simplify
5492    (coss (atans:s @0))
5493    (with
5494      {
5495       REAL_VALUE_TYPE r_cst;
5496       build_sinatan_real (&r_cst, type);
5497       tree t_cst = build_real (type, r_cst);
5498       tree t_one = build_one_cst (type);
5499       tree t_zero = build_zero_cst (type);
5500      }
5501     (if (SCALAR_FLOAT_TYPE_P (type))
5502      (cond (lt (abs @0) { t_cst; })
5503       (rdiv { t_one; } (sqrts (plus (mult @0 @0) { t_one; })))
5504       (copysigns { t_zero; } @0))))))
5506  (if (!flag_errno_math)
5507   /* Simplify sinh(atanh(x)) -> x / sqrt((1 - x)*(1 + x)). */
5508   (for sinhs (SINH)
5509        atanhs (ATANH)
5510        sqrts (SQRT)
5511    (simplify
5512     (sinhs (atanhs:s @0))
5513     (with { tree t_one = build_one_cst (type); }
5514     (rdiv @0 (sqrts (mult (minus { t_one; } @0) (plus { t_one; } @0)))))))
5516   /* Simplify cosh(atanh(x)) -> 1 / sqrt((1 - x)*(1 + x)) */
5517   (for coshs (COSH)
5518        atanhs (ATANH)
5519        sqrts (SQRT)
5520    (simplify
5521     (coshs (atanhs:s @0))
5522     (with { tree t_one = build_one_cst (type); }
5523     (rdiv { t_one; } (sqrts (mult (minus { t_one; } @0) (plus { t_one; } @0))))))))
5525 /* cabs(x+0i) or cabs(0+xi) -> abs(x).  */
5526 (simplify
5527  (CABS (complex:C @0 real_zerop@1))
5528  (abs @0))
5530 /* trunc(trunc(x)) -> trunc(x), etc.  */
5531 (for fns (TRUNC_ALL FLOOR_ALL CEIL_ALL ROUND_ALL NEARBYINT_ALL RINT_ALL)
5532  (simplify
5533   (fns (fns @0))
5534   (fns @0)))
5535 /* f(x) -> x if x is integer valued and f does nothing for such values.  */
5536 (for fns (TRUNC_ALL FLOOR_ALL CEIL_ALL ROUND_ALL NEARBYINT_ALL RINT_ALL)
5537  (simplify
5538   (fns integer_valued_real_p@0)
5539   @0))
5541 /* hypot(x,0) and hypot(0,x) -> abs(x).  */
5542 (simplify
5543  (HYPOT:c @0 real_zerop@1)
5544  (abs @0))
5546 /* pow(1,x) -> 1.  */
5547 (simplify
5548  (POW real_onep@0 @1)
5549  @0)
5551 (simplify
5552  /* copysign(x,x) -> x.  */
5553  (COPYSIGN_ALL @0 @0)
5554  @0)
5556 (simplify
5557  /* copysign(x,-x) -> -x.  */
5558  (COPYSIGN_ALL @0 (negate@1 @0))
5559  @1)
5561 (simplify
5562  /* copysign(x,y) -> fabs(x) if y is nonnegative.  */
5563  (COPYSIGN_ALL @0 tree_expr_nonnegative_p@1)
5564  (abs @0))
5566 (for scale (LDEXP SCALBN SCALBLN)
5567  /* ldexp(0, x) -> 0.  */
5568  (simplify
5569   (scale real_zerop@0 @1)
5570   @0)
5571  /* ldexp(x, 0) -> x.  */
5572  (simplify
5573   (scale @0 integer_zerop@1)
5574   @0)
5575  /* ldexp(x, y) -> x if x is +-Inf or NaN.  */
5576  (simplify
5577   (scale REAL_CST@0 @1)
5578   (if (!real_isfinite (TREE_REAL_CST_PTR (@0)))
5579    @0)))
5581 /* Canonicalization of sequences of math builtins.  These rules represent
5582    IL simplifications but are not necessarily optimizations.
5584    The sincos pass is responsible for picking "optimal" implementations
5585    of math builtins, which may be more complicated and can sometimes go
5586    the other way, e.g. converting pow into a sequence of sqrts.
5587    We only want to do these canonicalizations before the pass has run.  */
5589 (if (flag_unsafe_math_optimizations && canonicalize_math_p ())
5590  /* Simplify tan(x) * cos(x) -> sin(x). */
5591  (simplify
5592   (mult:c (TAN:s @0) (COS:s @0))
5593    (SIN @0))
5595  /* Simplify x * pow(x,c) -> pow(x,c+1). */
5596  (simplify
5597   (mult:c @0 (POW:s @0 REAL_CST@1))
5598   (if (!TREE_OVERFLOW (@1))
5599    (POW @0 (plus @1 { build_one_cst (type); }))))
5601  /* Simplify sin(x) / cos(x) -> tan(x). */
5602  (simplify
5603   (rdiv (SIN:s @0) (COS:s @0))
5604    (TAN @0))
5606  /* Simplify sinh(x) / cosh(x) -> tanh(x). */
5607  (simplify
5608   (rdiv (SINH:s @0) (COSH:s @0))
5609    (TANH @0))
5611  /* Simplify tanh (x) / sinh (x) -> 1.0 / cosh (x). */
5612  (simplify
5613    (rdiv (TANH:s @0) (SINH:s @0))
5614    (rdiv {build_one_cst (type);} (COSH @0)))
5616  /* Simplify cos(x) / sin(x) -> 1 / tan(x). */
5617  (simplify
5618   (rdiv (COS:s @0) (SIN:s @0))
5619    (rdiv { build_one_cst (type); } (TAN @0)))
5621  /* Simplify sin(x) / tan(x) -> cos(x). */
5622  (simplify
5623   (rdiv (SIN:s @0) (TAN:s @0))
5624   (if (! HONOR_NANS (@0)
5625        && ! HONOR_INFINITIES (@0))
5626    (COS @0)))
5628  /* Simplify tan(x) / sin(x) -> 1.0 / cos(x). */
5629  (simplify
5630   (rdiv (TAN:s @0) (SIN:s @0))
5631   (if (! HONOR_NANS (@0)
5632        && ! HONOR_INFINITIES (@0))
5633    (rdiv { build_one_cst (type); } (COS @0))))
5635  /* Simplify pow(x,y) * pow(x,z) -> pow(x,y+z). */
5636  (simplify
5637   (mult (POW:s @0 @1) (POW:s @0 @2))
5638    (POW @0 (plus @1 @2)))
5640  /* Simplify pow(x,y) * pow(z,y) -> pow(x*z,y). */
5641  (simplify
5642   (mult (POW:s @0 @1) (POW:s @2 @1))
5643    (POW (mult @0 @2) @1))
5645  /* Simplify powi(x,y) * powi(z,y) -> powi(x*z,y). */
5646  (simplify
5647   (mult (POWI:s @0 @1) (POWI:s @2 @1))
5648    (POWI (mult @0 @2) @1))
5650  /* Simplify pow(x,c) / x -> pow(x,c-1). */
5651  (simplify
5652   (rdiv (POW:s @0 REAL_CST@1) @0)
5653   (if (!TREE_OVERFLOW (@1))
5654    (POW @0 (minus @1 { build_one_cst (type); }))))
5656  /* Simplify x / pow (y,z) -> x * pow(y,-z). */
5657  (simplify
5658   (rdiv @0 (POW:s @1 @2))
5659    (mult @0 (POW @1 (negate @2))))
5661  (for sqrts (SQRT)
5662       cbrts (CBRT)
5663       pows (POW)
5664   /* sqrt(sqrt(x)) -> pow(x,1/4).  */
5665   (simplify
5666    (sqrts (sqrts @0))
5667    (pows @0 { build_real (type, dconst_quarter ()); }))
5668   /* sqrt(cbrt(x)) -> pow(x,1/6).  */
5669   (simplify
5670    (sqrts (cbrts @0))
5671    (pows @0 { build_real_truncate (type, dconst_sixth ()); }))
5672   /* cbrt(sqrt(x)) -> pow(x,1/6).  */
5673   (simplify
5674    (cbrts (sqrts @0))
5675    (pows @0 { build_real_truncate (type, dconst_sixth ()); }))
5676   /* cbrt(cbrt(x)) -> pow(x,1/9), iff x is nonnegative.  */
5677   (simplify
5678    (cbrts (cbrts tree_expr_nonnegative_p@0))
5679    (pows @0 { build_real_truncate (type, dconst_ninth ()); }))
5680   /* sqrt(pow(x,y)) -> pow(|x|,y*0.5).  */
5681   (simplify
5682    (sqrts (pows @0 @1))
5683    (pows (abs @0) (mult @1 { build_real (type, dconsthalf); })))
5684   /* cbrt(pow(x,y)) -> pow(x,y/3), iff x is nonnegative.  */
5685   (simplify
5686    (cbrts (pows tree_expr_nonnegative_p@0 @1))
5687    (pows @0 (mult @1 { build_real_truncate (type, dconst_third ()); })))
5688   /* pow(sqrt(x),y) -> pow(x,y*0.5).  */
5689   (simplify
5690    (pows (sqrts @0) @1)
5691    (pows @0 (mult @1 { build_real (type, dconsthalf); })))
5692   /* pow(cbrt(x),y) -> pow(x,y/3) iff x is nonnegative.  */
5693   (simplify
5694    (pows (cbrts tree_expr_nonnegative_p@0) @1)
5695    (pows @0 (mult @1 { build_real_truncate (type, dconst_third ()); })))
5696   /* pow(pow(x,y),z) -> pow(x,y*z) iff x is nonnegative.  */
5697   (simplify
5698    (pows (pows tree_expr_nonnegative_p@0 @1) @2)
5699    (pows @0 (mult @1 @2))))
5701  /* cabs(x+xi) -> fabs(x)*sqrt(2).  */
5702  (simplify
5703   (CABS (complex @0 @0))
5704   (mult (abs @0) { build_real_truncate (type, dconst_sqrt2 ()); }))
5706  /* hypot(x,x) -> fabs(x)*sqrt(2).  */
5707  (simplify
5708   (HYPOT @0 @0)
5709   (mult (abs @0) { build_real_truncate (type, dconst_sqrt2 ()); }))
5711  /* cexp(x+yi) -> exp(x)*cexpi(y).  */
5712  (for cexps (CEXP)
5713       exps (EXP)
5714       cexpis (CEXPI)
5715   (simplify
5716    (cexps compositional_complex@0)
5717    (if (targetm.libc_has_function (function_c99_math_complex, TREE_TYPE (@0)))
5718     (complex
5719      (mult (exps@1 (realpart @0)) (realpart (cexpis:type@2 (imagpart @0))))
5720      (mult @1 (imagpart @2)))))))
5722 (if (canonicalize_math_p ())
5723  /* floor(x) -> trunc(x) if x is nonnegative.  */
5724  (for floors (FLOOR_ALL)
5725       truncs (TRUNC_ALL)
5726   (simplify
5727    (floors tree_expr_nonnegative_p@0)
5728    (truncs @0))))
5730 (match double_value_p
5731  @0
5732  (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == double_type_node)))
5733 (for froms (BUILT_IN_TRUNCL
5734             BUILT_IN_FLOORL
5735             BUILT_IN_CEILL
5736             BUILT_IN_ROUNDL
5737             BUILT_IN_NEARBYINTL
5738             BUILT_IN_RINTL)
5739      tos (BUILT_IN_TRUNC
5740           BUILT_IN_FLOOR
5741           BUILT_IN_CEIL
5742           BUILT_IN_ROUND
5743           BUILT_IN_NEARBYINT
5744           BUILT_IN_RINT)
5745  /* truncl(extend(x)) -> extend(trunc(x)), etc., if x is a double.  */
5746  (if (optimize && canonicalize_math_p ())
5747   (simplify
5748    (froms (convert double_value_p@0))
5749    (convert (tos @0)))))
5751 (match float_value_p
5752  @0
5753  (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == float_type_node)))
5754 (for froms (BUILT_IN_TRUNCL BUILT_IN_TRUNC
5755             BUILT_IN_FLOORL BUILT_IN_FLOOR
5756             BUILT_IN_CEILL BUILT_IN_CEIL
5757             BUILT_IN_ROUNDL BUILT_IN_ROUND
5758             BUILT_IN_NEARBYINTL BUILT_IN_NEARBYINT
5759             BUILT_IN_RINTL BUILT_IN_RINT)
5760      tos (BUILT_IN_TRUNCF BUILT_IN_TRUNCF
5761           BUILT_IN_FLOORF BUILT_IN_FLOORF
5762           BUILT_IN_CEILF BUILT_IN_CEILF
5763           BUILT_IN_ROUNDF BUILT_IN_ROUNDF
5764           BUILT_IN_NEARBYINTF BUILT_IN_NEARBYINTF
5765           BUILT_IN_RINTF BUILT_IN_RINTF)
5766  /* truncl(extend(x)) and trunc(extend(x)) -> extend(truncf(x)), etc.,
5767     if x is a float.  */
5768  (if (optimize && canonicalize_math_p ()
5769       && targetm.libc_has_function (function_c99_misc, NULL_TREE))
5770   (simplify
5771    (froms (convert float_value_p@0))
5772    (convert (tos @0)))))
5774 (for froms (XFLOORL XCEILL XROUNDL XRINTL)
5775      tos (XFLOOR XCEIL XROUND XRINT)
5776  /* llfloorl(extend(x)) -> llfloor(x), etc., if x is a double.  */
5777  (if (optimize && canonicalize_math_p ())
5778   (simplify
5779    (froms (convert double_value_p@0))
5780    (tos @0))))
5782 (for froms (XFLOORL XCEILL XROUNDL XRINTL
5783             XFLOOR XCEIL XROUND XRINT)
5784      tos (XFLOORF XCEILF XROUNDF XRINTF)
5785  /* llfloorl(extend(x)) and llfloor(extend(x)) -> llfloorf(x), etc.,
5786     if x is a float.  */
5787  (if (optimize && canonicalize_math_p ())
5788   (simplify
5789    (froms (convert float_value_p@0))
5790    (tos @0))))
5792 (if (canonicalize_math_p ())
5793  /* xfloor(x) -> fix_trunc(x) if x is nonnegative.  */
5794  (for floors (IFLOOR LFLOOR LLFLOOR)
5795   (simplify
5796    (floors tree_expr_nonnegative_p@0)
5797    (fix_trunc @0))))
5799 (if (canonicalize_math_p ())
5800  /* xfloor(x) -> fix_trunc(x), etc., if x is integer valued.  */
5801  (for fns (IFLOOR LFLOOR LLFLOOR
5802            ICEIL LCEIL LLCEIL
5803            IROUND LROUND LLROUND)
5804   (simplify
5805    (fns integer_valued_real_p@0)
5806    (fix_trunc @0)))
5807  (if (!flag_errno_math)
5808   /* xrint(x) -> fix_trunc(x), etc., if x is integer valued.  */
5809   (for rints (IRINT LRINT LLRINT)
5810    (simplify
5811     (rints integer_valued_real_p@0)
5812     (fix_trunc @0)))))
5814 (if (canonicalize_math_p ())
5815  (for ifn (IFLOOR ICEIL IROUND IRINT)
5816       lfn (LFLOOR LCEIL LROUND LRINT)
5817       llfn (LLFLOOR LLCEIL LLROUND LLRINT)
5818   /* Canonicalize iround (x) to lround (x) on ILP32 targets where
5819      sizeof (int) == sizeof (long).  */
5820   (if (TYPE_PRECISION (integer_type_node)
5821        == TYPE_PRECISION (long_integer_type_node))
5822    (simplify
5823     (ifn @0)
5824     (lfn:long_integer_type_node @0)))
5825   /* Canonicalize llround (x) to lround (x) on LP64 targets where
5826      sizeof (long long) == sizeof (long).  */
5827   (if (TYPE_PRECISION (long_long_integer_type_node)
5828        == TYPE_PRECISION (long_integer_type_node))
5829    (simplify
5830     (llfn @0)
5831     (lfn:long_integer_type_node @0)))))
5833 /* cproj(x) -> x if we're ignoring infinities.  */
5834 (simplify
5835  (CPROJ @0)
5836  (if (!HONOR_INFINITIES (type))
5837    @0))
5839 /* If the real part is inf and the imag part is known to be
5840    nonnegative, return (inf + 0i).  */
5841 (simplify
5842  (CPROJ (complex REAL_CST@0 tree_expr_nonnegative_p@1))
5843  (if (real_isinf (TREE_REAL_CST_PTR (@0)))
5844   { build_complex_inf (type, false); }))
5846 /* If the imag part is inf, return (inf+I*copysign(0,imag)).  */
5847 (simplify
5848  (CPROJ (complex @0 REAL_CST@1))
5849  (if (real_isinf (TREE_REAL_CST_PTR (@1)))
5850   { build_complex_inf (type, TREE_REAL_CST_PTR (@1)->sign); }))
5852 (for pows (POW)
5853      sqrts (SQRT)
5854      cbrts (CBRT)
5855  (simplify
5856   (pows @0 REAL_CST@1)
5857   (with {
5858     const REAL_VALUE_TYPE *value = TREE_REAL_CST_PTR (@1);
5859     REAL_VALUE_TYPE tmp;
5860    }
5861    (switch
5862     /* pow(x,0) -> 1.  */
5863     (if (real_equal (value, &dconst0))
5864      { build_real (type, dconst1); })
5865     /* pow(x,1) -> x.  */
5866     (if (real_equal (value, &dconst1))
5867      @0)
5868     /* pow(x,-1) -> 1/x.  */
5869     (if (real_equal (value, &dconstm1))
5870      (rdiv { build_real (type, dconst1); } @0))
5871     /* pow(x,0.5) -> sqrt(x).  */
5872     (if (flag_unsafe_math_optimizations
5873          && canonicalize_math_p ()
5874          && real_equal (value, &dconsthalf))
5875      (sqrts @0))
5876     /* pow(x,1/3) -> cbrt(x).  */
5877     (if (flag_unsafe_math_optimizations
5878          && canonicalize_math_p ()
5879          && (tmp = real_value_truncate (TYPE_MODE (type), dconst_third ()),
5880              real_equal (value, &tmp)))
5881      (cbrts @0))))))
5883 /* powi(1,x) -> 1.  */
5884 (simplify
5885  (POWI real_onep@0 @1)
5886  @0)
5888 (simplify
5889  (POWI @0 INTEGER_CST@1)
5890  (switch
5891   /* powi(x,0) -> 1.  */
5892   (if (wi::to_wide (@1) == 0)
5893    { build_real (type, dconst1); })
5894   /* powi(x,1) -> x.  */
5895   (if (wi::to_wide (@1) == 1)
5896    @0)
5897   /* powi(x,-1) -> 1/x.  */
5898   (if (wi::to_wide (@1) == -1)
5899    (rdiv { build_real (type, dconst1); } @0))))
5901 /* Narrowing of arithmetic and logical operations.
5903    These are conceptually similar to the transformations performed for
5904    the C/C++ front-ends by shorten_binary_op and shorten_compare.  Long
5905    term we want to move all that code out of the front-ends into here.  */
5907 /* Convert (outertype)((innertype0)a+(innertype1)b)
5908    into ((newtype)a+(newtype)b) where newtype
5909    is the widest mode from all of these.  */
5910 (for op (plus minus mult rdiv)
5911  (simplify
5912    (convert (op:s@0 (convert1?@3 @1) (convert2?@4 @2)))
5913    /* If we have a narrowing conversion of an arithmetic operation where
5914       both operands are widening conversions from the same type as the outer
5915       narrowing conversion.  Then convert the innermost operands to a
5916       suitable unsigned type (to avoid introducing undefined behavior),
5917       perform the operation and convert the result to the desired type.  */
5918    (if (INTEGRAL_TYPE_P (type)
5919         && op != MULT_EXPR
5920         && op != RDIV_EXPR
5921         /* We check for type compatibility between @0 and @1 below,
5922            so there's no need to check that @2/@4 are integral types.  */
5923         && INTEGRAL_TYPE_P (TREE_TYPE (@1))
5924         && INTEGRAL_TYPE_P (TREE_TYPE (@3))
5925         /* The precision of the type of each operand must match the
5926            precision of the mode of each operand, similarly for the
5927            result.  */
5928         && type_has_mode_precision_p (TREE_TYPE (@1))
5929         && type_has_mode_precision_p (TREE_TYPE (@2))
5930         && type_has_mode_precision_p (type)
5931         /* The inner conversion must be a widening conversion.  */
5932         && TYPE_PRECISION (TREE_TYPE (@3)) > TYPE_PRECISION (TREE_TYPE (@1))
5933         && types_match (@1, type)
5934         && (types_match (@1, @2)
5935             /* Or the second operand is const integer or converted const
5936                integer from valueize.  */
5937             || TREE_CODE (@2) == INTEGER_CST))
5938      (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
5939        (op @1 (convert @2))
5940        (with { tree utype = unsigned_type_for (TREE_TYPE (@1)); }
5941         (convert (op (convert:utype @1)
5942                      (convert:utype @2)))))
5943      (if (FLOAT_TYPE_P (type)
5944           && DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0))
5945                == DECIMAL_FLOAT_TYPE_P (type))
5946       (with { tree arg0 = strip_float_extensions (@1);
5947               tree arg1 = strip_float_extensions (@2);
5948               tree itype = TREE_TYPE (@0);
5949               tree ty1 = TREE_TYPE (arg0);
5950               tree ty2 = TREE_TYPE (arg1);
5951               enum tree_code code = TREE_CODE (itype); }
5952         (if (FLOAT_TYPE_P (ty1)
5953              && FLOAT_TYPE_P (ty2))
5954          (with { tree newtype = type;
5955                  if (TYPE_MODE (ty1) == SDmode
5956                      || TYPE_MODE (ty2) == SDmode
5957                      || TYPE_MODE (type) == SDmode)
5958                    newtype = dfloat32_type_node;
5959                  if (TYPE_MODE (ty1) == DDmode
5960                      || TYPE_MODE (ty2) == DDmode
5961                      || TYPE_MODE (type) == DDmode)
5962                    newtype = dfloat64_type_node;
5963                  if (TYPE_MODE (ty1) == TDmode
5964                      || TYPE_MODE (ty2) == TDmode
5965                      || TYPE_MODE (type) == TDmode)
5966                    newtype = dfloat128_type_node; }
5967           (if ((newtype == dfloat32_type_node
5968                 || newtype == dfloat64_type_node
5969                 || newtype == dfloat128_type_node)
5970               && newtype == type
5971               && types_match (newtype, type))
5972             (op (convert:newtype @1) (convert:newtype @2))
5973             (with { if (TYPE_PRECISION (ty1) > TYPE_PRECISION (newtype))
5974                       newtype = ty1;
5975                     if (TYPE_PRECISION (ty2) > TYPE_PRECISION (newtype))
5976                       newtype = ty2; }
5977                /* Sometimes this transformation is safe (cannot
5978                   change results through affecting double rounding
5979                   cases) and sometimes it is not.  If NEWTYPE is
5980                   wider than TYPE, e.g. (float)((long double)double
5981                   + (long double)double) converted to
5982                   (float)(double + double), the transformation is
5983                   unsafe regardless of the details of the types
5984                   involved; double rounding can arise if the result
5985                   of NEWTYPE arithmetic is a NEWTYPE value half way
5986                   between two representable TYPE values but the
5987                   exact value is sufficiently different (in the
5988                   right direction) for this difference to be
5989                   visible in ITYPE arithmetic.  If NEWTYPE is the
5990                   same as TYPE, however, the transformation may be
5991                   safe depending on the types involved: it is safe
5992                   if the ITYPE has strictly more than twice as many
5993                   mantissa bits as TYPE, can represent infinities
5994                   and NaNs if the TYPE can, and has sufficient
5995                   exponent range for the product or ratio of two
5996                   values representable in the TYPE to be within the
5997                   range of normal values of ITYPE.  */
5998               (if (TYPE_PRECISION (newtype) < TYPE_PRECISION (itype)
5999                    && (flag_unsafe_math_optimizations
6000                        || (TYPE_PRECISION (newtype) == TYPE_PRECISION (type)
6001                            && real_can_shorten_arithmetic (TYPE_MODE (itype),
6002                                                            TYPE_MODE (type))
6003                            && !excess_precision_type (newtype)))
6004                    && !types_match (itype, newtype))
6005                  (convert:type (op (convert:newtype @1)
6006                                    (convert:newtype @2)))
6007          )))) )
6008    ))
6011 /* This is another case of narrowing, specifically when there's an outer
6012    BIT_AND_EXPR which masks off bits outside the type of the innermost
6013    operands.   Like the previous case we have to convert the operands
6014    to unsigned types to avoid introducing undefined behavior for the
6015    arithmetic operation.  */
6016 (for op (minus plus)
6017  (simplify
6018   (bit_and (op:s (convert@2 @0) (convert@3 @1)) INTEGER_CST@4)
6019   (if (INTEGRAL_TYPE_P (type)
6020        /* We check for type compatibility between @0 and @1 below,
6021           so there's no need to check that @1/@3 are integral types.  */
6022        && INTEGRAL_TYPE_P (TREE_TYPE (@0))
6023        && INTEGRAL_TYPE_P (TREE_TYPE (@2))
6024        /* The precision of the type of each operand must match the
6025           precision of the mode of each operand, similarly for the
6026           result.  */
6027        && type_has_mode_precision_p (TREE_TYPE (@0))
6028        && type_has_mode_precision_p (TREE_TYPE (@1))
6029        && type_has_mode_precision_p (type)
6030        /* The inner conversion must be a widening conversion.  */
6031        && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
6032        && types_match (@0, @1)
6033        && (tree_int_cst_min_precision (@4, TYPE_SIGN (TREE_TYPE (@0)))
6034            <= TYPE_PRECISION (TREE_TYPE (@0)))
6035        && (wi::to_wide (@4)
6036            & wi::mask (TYPE_PRECISION (TREE_TYPE (@0)),
6037                        true, TYPE_PRECISION (type))) == 0)
6038    (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
6039     (with { tree ntype = TREE_TYPE (@0); }
6040      (convert (bit_and (op @0 @1) (convert:ntype @4))))
6041     (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
6042      (convert (bit_and (op (convert:utype @0) (convert:utype @1))
6043                (convert:utype @4))))))))
6045 /* Transform (@0 < @1 and @0 < @2) to use min,
6046    (@0 > @1 and @0 > @2) to use max */
6047 (for logic (bit_and bit_and bit_and bit_and bit_ior bit_ior bit_ior bit_ior)
6048      op    (lt      le      gt      ge      lt      le      gt      ge     )
6049      ext   (min     min     max     max     max     max     min     min    )
6050  (simplify
6051   (logic (op:cs @0 @1) (op:cs @0 @2))
6052   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6053        && TREE_CODE (@0) != INTEGER_CST)
6054    (op @0 (ext @1 @2)))))
6056 (simplify
6057  /* signbit(x) -> 0 if x is nonnegative.  */
6058  (SIGNBIT tree_expr_nonnegative_p@0)
6059  { integer_zero_node; })
6061 (simplify
6062  /* signbit(x) -> x<0 if x doesn't have signed zeros.  */
6063  (SIGNBIT @0)
6064  (if (!HONOR_SIGNED_ZEROS (@0))
6065   (convert (lt @0 { build_real (TREE_TYPE (@0), dconst0); }))))
6067 /* Transform comparisons of the form X +- C1 CMP C2 to X CMP C2 -+ C1.  */
6068 (for cmp (eq ne)
6069  (for op (plus minus)
6070       rop (minus plus)
6071   (simplify
6072    (cmp (op@3 @0 INTEGER_CST@1) INTEGER_CST@2)
6073    (if (!TREE_OVERFLOW (@1) && !TREE_OVERFLOW (@2)
6074         && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0))
6075         && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@0))
6076         && !TYPE_SATURATING (TREE_TYPE (@0)))
6077     (with { tree res = int_const_binop (rop, @2, @1); }
6078      (if (TREE_OVERFLOW (res)
6079           && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
6080       { constant_boolean_node (cmp == NE_EXPR, type); }
6081       (if (single_use (@3))
6082        (cmp @0 { TREE_OVERFLOW (res)
6083                  ? drop_tree_overflow (res) : res; }))))))))
6084 (for cmp (lt le gt ge)
6085  (for op (plus minus)
6086       rop (minus plus)
6087   (simplify
6088    (cmp (op@3 @0 INTEGER_CST@1) INTEGER_CST@2)
6089    (if (!TREE_OVERFLOW (@1) && !TREE_OVERFLOW (@2)
6090         && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
6091     (with { tree res = int_const_binop (rop, @2, @1); }
6092      (if (TREE_OVERFLOW (res))
6093       {
6094         fold_overflow_warning (("assuming signed overflow does not occur "
6095                                 "when simplifying conditional to constant"),
6096                                WARN_STRICT_OVERFLOW_CONDITIONAL);
6097         bool less = cmp == LE_EXPR || cmp == LT_EXPR;
6098         /* wi::ges_p (@2, 0) should be sufficient for a signed type.  */
6099         bool ovf_high = wi::lt_p (wi::to_wide (@1), 0,
6100                                   TYPE_SIGN (TREE_TYPE (@1)))
6101                         != (op == MINUS_EXPR);
6102         constant_boolean_node (less == ovf_high, type);
6103       }
6104       (if (single_use (@3))
6105        (with
6106         {
6107           fold_overflow_warning (("assuming signed overflow does not occur "
6108                                   "when changing X +- C1 cmp C2 to "
6109                                   "X cmp C2 -+ C1"),
6110                                  WARN_STRICT_OVERFLOW_COMPARISON);
6111         }
6112         (cmp @0 { res; })))))))))
6114 /* Canonicalizations of BIT_FIELD_REFs.  */
6116 (simplify
6117  (BIT_FIELD_REF (BIT_FIELD_REF @0 @1 @2) @3 @4)
6118  (BIT_FIELD_REF @0 @3 { const_binop (PLUS_EXPR, bitsizetype, @2, @4); }))
6120 (simplify
6121  (BIT_FIELD_REF (view_convert @0) @1 @2)
6122  (BIT_FIELD_REF @0 @1 @2))
6124 (simplify
6125  (BIT_FIELD_REF @0 @1 integer_zerop)
6126  (if (tree_int_cst_equal (@1, TYPE_SIZE (TREE_TYPE (@0))))
6127   (view_convert @0)))
6129 (simplify
6130  (BIT_FIELD_REF @0 @1 @2)
6131  (switch
6132   (if (TREE_CODE (TREE_TYPE (@0)) == COMPLEX_TYPE
6133        && tree_int_cst_equal (@1, TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))))
6134    (switch
6135     (if (integer_zerop (@2))
6136      (view_convert (realpart @0)))
6137     (if (tree_int_cst_equal (@2, TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))))
6138      (view_convert (imagpart @0)))))
6139   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6140        && INTEGRAL_TYPE_P (type)
6141        /* On GIMPLE this should only apply to register arguments.  */
6142        && (! GIMPLE || is_gimple_reg (@0))
6143        /* A bit-field-ref that referenced the full argument can be stripped.  */
6144        && ((compare_tree_int (@1, TYPE_PRECISION (TREE_TYPE (@0))) == 0
6145             && integer_zerop (@2))
6146            /* Low-parts can be reduced to integral conversions.
6147               ???  The following doesn't work for PDP endian.  */
6148            || (BYTES_BIG_ENDIAN == WORDS_BIG_ENDIAN
6149                /* But only do this after vectorization.  */
6150                && canonicalize_math_after_vectorization_p ()
6151                /* Don't even think about BITS_BIG_ENDIAN.  */
6152                && TYPE_PRECISION (TREE_TYPE (@0)) % BITS_PER_UNIT == 0
6153                && TYPE_PRECISION (type) % BITS_PER_UNIT == 0
6154                && compare_tree_int (@2, (BYTES_BIG_ENDIAN
6155                                          ? (TYPE_PRECISION (TREE_TYPE (@0))
6156                                             - TYPE_PRECISION (type))
6157                                          : 0)) == 0)))
6158    (convert @0))))
6160 /* Simplify vector extracts.  */
6162 (simplify
6163  (BIT_FIELD_REF CONSTRUCTOR@0 @1 @2)
6164  (if (VECTOR_TYPE_P (TREE_TYPE (@0))
6165       && (types_match (type, TREE_TYPE (TREE_TYPE (@0)))
6166           || (VECTOR_TYPE_P (type)
6167               && types_match (TREE_TYPE (type), TREE_TYPE (TREE_TYPE (@0))))))
6168   (with
6169    {
6170      tree ctor = (TREE_CODE (@0) == SSA_NAME
6171                   ? gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0)) : @0);
6172      tree eltype = TREE_TYPE (TREE_TYPE (ctor));
6173      unsigned HOST_WIDE_INT width = tree_to_uhwi (TYPE_SIZE (eltype));
6174      unsigned HOST_WIDE_INT n = tree_to_uhwi (@1);
6175      unsigned HOST_WIDE_INT idx = tree_to_uhwi (@2);
6176    }
6177    (if (n != 0
6178         && (idx % width) == 0
6179         && (n % width) == 0
6180         && known_le ((idx + n) / width,
6181                      TYPE_VECTOR_SUBPARTS (TREE_TYPE (ctor))))
6182     (with
6183      {
6184        idx = idx / width;
6185        n = n / width;
6186        /* Constructor elements can be subvectors.  */
6187        poly_uint64 k = 1;
6188        if (CONSTRUCTOR_NELTS (ctor) != 0)
6189          {
6190            tree cons_elem = TREE_TYPE (CONSTRUCTOR_ELT (ctor, 0)->value);
6191            if (TREE_CODE (cons_elem) == VECTOR_TYPE)
6192              k = TYPE_VECTOR_SUBPARTS (cons_elem);
6193          }
6194        unsigned HOST_WIDE_INT elt, count, const_k;
6195      }
6196      (switch
6197       /* We keep an exact subset of the constructor elements.  */
6198       (if (multiple_p (idx, k, &elt) && multiple_p (n, k, &count))
6199        (if (CONSTRUCTOR_NELTS (ctor) == 0)
6200         { build_constructor (type, NULL); }
6201         (if (count == 1)
6202          (if (elt < CONSTRUCTOR_NELTS (ctor))
6203           (view_convert { CONSTRUCTOR_ELT (ctor, elt)->value; })
6204           { build_zero_cst (type); })
6205          /* We don't want to emit new CTORs unless the old one goes away.
6206             ???  Eventually allow this if the CTOR ends up constant or
6207             uniform.  */
6208          (if (single_use (@0))
6209           {
6210             vec<constructor_elt, va_gc> *vals;
6211             vec_alloc (vals, count);
6212             for (unsigned i = 0;
6213                  i < count && elt + i < CONSTRUCTOR_NELTS (ctor); ++i)
6214               CONSTRUCTOR_APPEND_ELT (vals, NULL_TREE,
6215                                       CONSTRUCTOR_ELT (ctor, elt + i)->value);
6216             build_constructor (type, vals);
6217           }))))
6218       /* The bitfield references a single constructor element.  */
6219       (if (k.is_constant (&const_k)
6220            && idx + n <= (idx / const_k + 1) * const_k)
6221        (switch
6222         (if (CONSTRUCTOR_NELTS (ctor) <= idx / const_k)
6223          { build_zero_cst (type); })
6224         (if (n == const_k)
6225          (view_convert { CONSTRUCTOR_ELT (ctor, idx / const_k)->value; }))
6226         (BIT_FIELD_REF { CONSTRUCTOR_ELT (ctor, idx / const_k)->value; }
6227                        @1 { bitsize_int ((idx % const_k) * width); })))))))))
6229 /* Simplify a bit extraction from a bit insertion for the cases with
6230    the inserted element fully covering the extraction or the insertion
6231    not touching the extraction.  */
6232 (simplify
6233  (BIT_FIELD_REF (bit_insert @0 @1 @ipos) @rsize @rpos)
6234  (with
6235   {
6236     unsigned HOST_WIDE_INT isize;
6237     if (INTEGRAL_TYPE_P (TREE_TYPE (@1)))
6238       isize = TYPE_PRECISION (TREE_TYPE (@1));
6239     else
6240       isize = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (@1)));
6241   }
6242   (switch
6243    (if (wi::leu_p (wi::to_wide (@ipos), wi::to_wide (@rpos))
6244         && wi::leu_p (wi::to_wide (@rpos) + wi::to_wide (@rsize),
6245                       wi::to_wide (@ipos) + isize))
6246     (BIT_FIELD_REF @1 @rsize { wide_int_to_tree (bitsizetype,
6247                                                  wi::to_wide (@rpos)
6248                                                  - wi::to_wide (@ipos)); }))
6249    (if (wi::geu_p (wi::to_wide (@ipos),
6250                    wi::to_wide (@rpos) + wi::to_wide (@rsize))
6251         || wi::geu_p (wi::to_wide (@rpos),
6252                       wi::to_wide (@ipos) + isize))
6253     (BIT_FIELD_REF @0 @rsize @rpos)))))
6255 (if (canonicalize_math_after_vectorization_p ())
6256  (for fmas (FMA)
6257   (simplify
6258    (fmas:c (negate @0) @1 @2)
6259    (IFN_FNMA @0 @1 @2))
6260   (simplify
6261    (fmas @0 @1 (negate @2))
6262    (IFN_FMS @0 @1 @2))
6263   (simplify
6264    (fmas:c (negate @0) @1 (negate @2))
6265    (IFN_FNMS @0 @1 @2))
6266   (simplify
6267    (negate (fmas@3 @0 @1 @2))
6268    (if (single_use (@3))
6269     (IFN_FNMS @0 @1 @2))))
6271  (simplify
6272   (IFN_FMS:c (negate @0) @1 @2)
6273   (IFN_FNMS @0 @1 @2))
6274  (simplify
6275   (IFN_FMS @0 @1 (negate @2))
6276   (IFN_FMA @0 @1 @2))
6277  (simplify
6278   (IFN_FMS:c (negate @0) @1 (negate @2))
6279   (IFN_FNMA @0 @1 @2))
6280  (simplify
6281   (negate (IFN_FMS@3 @0 @1 @2))
6282    (if (single_use (@3))
6283     (IFN_FNMA @0 @1 @2)))
6285  (simplify
6286   (IFN_FNMA:c (negate @0) @1 @2)
6287   (IFN_FMA @0 @1 @2))
6288  (simplify
6289   (IFN_FNMA @0 @1 (negate @2))
6290   (IFN_FNMS @0 @1 @2))
6291  (simplify
6292   (IFN_FNMA:c (negate @0) @1 (negate @2))
6293   (IFN_FMS @0 @1 @2))
6294  (simplify
6295   (negate (IFN_FNMA@3 @0 @1 @2))
6296   (if (single_use (@3))
6297    (IFN_FMS @0 @1 @2)))
6299  (simplify
6300   (IFN_FNMS:c (negate @0) @1 @2)
6301   (IFN_FMS @0 @1 @2))
6302  (simplify
6303   (IFN_FNMS @0 @1 (negate @2))
6304   (IFN_FNMA @0 @1 @2))
6305  (simplify
6306   (IFN_FNMS:c (negate @0) @1 (negate @2))
6307   (IFN_FMA @0 @1 @2))
6308  (simplify
6309   (negate (IFN_FNMS@3 @0 @1 @2))
6310   (if (single_use (@3))
6311    (IFN_FMA @0 @1 @2))))
6313 /* CLZ simplifications.  */
6314 (for clz (CLZ)
6315  (for op (eq ne)
6316       cmp (lt ge)
6317   (simplify
6318    (op (clz:s@2 @0) INTEGER_CST@1)
6319    (if (integer_zerop (@1) && single_use (@2))
6320     /* clz(X) == 0 is (int)X < 0 and clz(X) != 0 is (int)X >= 0.  */
6321     (with { tree stype = signed_type_for (TREE_TYPE (@0));
6322             HOST_WIDE_INT val = 0;
6323 #ifdef CLZ_DEFINED_VALUE_AT_ZERO
6324             /* Punt on hypothetical weird targets.  */
6325             if (CLZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (TREE_TYPE (@0)),
6326                                            val) == 2
6327                 && val == 0)
6328               stype = NULL_TREE;
6329 #endif
6330           }
6331      (if (stype)
6332       (cmp (convert:stype @0) { build_zero_cst (stype); })))
6333     /* clz(X) == (prec-1) is X == 1 and clz(X) != (prec-1) is X != 1.  */
6334     (with { bool ok = true;
6335 #ifdef CLZ_DEFINED_VALUE_AT_ZERO
6336             /* Punt on hypothetical weird targets.  */
6337             if (CLZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (TREE_TYPE (@0)),
6338                                            val) == 2
6339                 && val == TYPE_PRECISION (TREE_TYPE (@0)) - 1)
6340               ok = false;
6341 #endif
6342           }
6343      (if (ok && wi::to_wide (@1) == (TYPE_PRECISION (TREE_TYPE (@0)) - 1))
6344       (op @0 { build_one_cst (TREE_TYPE (@0)); })))))))
6346 /* POPCOUNT simplifications.  */
6347 /* popcount(X) + popcount(Y) is popcount(X|Y) when X&Y must be zero.  */
6348 (simplify
6349   (plus (POPCOUNT:s @0) (POPCOUNT:s @1))
6350   (if (wi::bit_and (tree_nonzero_bits (@0), tree_nonzero_bits (@1)) == 0)
6351     (POPCOUNT (bit_ior @0 @1))))
6353 /* popcount(X) == 0 is X == 0, and related (in)equalities.  */
6354 (for popcount (POPCOUNT)
6355   (for cmp (le eq ne gt)
6356        rep (eq eq ne ne)
6357     (simplify
6358       (cmp (popcount @0) integer_zerop)
6359       (rep @0 { build_zero_cst (TREE_TYPE (@0)); }))))
6361 /* Canonicalize POPCOUNT(x)&1 as PARITY(X).  */
6362 (simplify
6363   (bit_and (POPCOUNT @0) integer_onep)
6364   (PARITY @0))
6366 /* PARITY simplifications.  */
6367 /* parity(~X) is parity(X).  */
6368 (simplify
6369   (PARITY (bit_not @0))
6370   (PARITY @0))
6372 /* parity(X)^parity(Y) is parity(X^Y).  */
6373 (simplify
6374   (bit_xor (PARITY:s @0) (PARITY:s @1))
6375   (PARITY (bit_xor @0 @1)))
6377 /* Common POPCOUNT/PARITY simplifications.  */
6378 /* popcount(X&C1) is (X>>C2)&1 when C1 == 1<<C2.  Same for parity(X&C1).  */
6379 (for pfun (POPCOUNT PARITY)
6380   (simplify
6381     (pfun @0)
6382     (with { wide_int nz = tree_nonzero_bits (@0); }
6383       (switch
6384         (if (nz == 1)
6385           (convert @0))
6386         (if (wi::popcount (nz) == 1)
6387           (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
6388             (convert (rshift:utype (convert:utype @0)
6389                                    { build_int_cst (integer_type_node,
6390                                                     wi::ctz (nz)); }))))))))
6392 #if GIMPLE
6393 /* 64- and 32-bits branchless implementations of popcount are detected:
6395    int popcount64c (uint64_t x)
6396    {
6397      x -= (x >> 1) & 0x5555555555555555ULL;
6398      x = (x & 0x3333333333333333ULL) + ((x >> 2) & 0x3333333333333333ULL);
6399      x = (x + (x >> 4)) & 0x0f0f0f0f0f0f0f0fULL;
6400      return (x * 0x0101010101010101ULL) >> 56;
6401    }
6403    int popcount32c (uint32_t x)
6404    {
6405      x -= (x >> 1) & 0x55555555;
6406      x = (x & 0x33333333) + ((x >> 2) & 0x33333333);
6407      x = (x + (x >> 4)) & 0x0f0f0f0f;
6408      return (x * 0x01010101) >> 24;
6409    }  */
6410 (simplify
6411  (rshift
6412   (mult
6413    (bit_and
6414     (plus:c
6415      (rshift @8 INTEGER_CST@5)
6416       (plus:c@8
6417        (bit_and @6 INTEGER_CST@7)
6418         (bit_and
6419          (rshift
6420           (minus@6 @0
6421            (bit_and (rshift @0 INTEGER_CST@4) INTEGER_CST@11))
6422           INTEGER_CST@10)
6423          INTEGER_CST@9)))
6424     INTEGER_CST@3)
6425    INTEGER_CST@2)
6426   INTEGER_CST@1)
6427   /* Check constants and optab.  */
6428   (with { unsigned prec = TYPE_PRECISION (type);
6429           int shift = (64 - prec) & 63;
6430           unsigned HOST_WIDE_INT c1
6431             = HOST_WIDE_INT_UC (0x0101010101010101) >> shift;
6432           unsigned HOST_WIDE_INT c2
6433             = HOST_WIDE_INT_UC (0x0F0F0F0F0F0F0F0F) >> shift;
6434           unsigned HOST_WIDE_INT c3
6435             = HOST_WIDE_INT_UC (0x3333333333333333) >> shift;
6436           unsigned HOST_WIDE_INT c4
6437             = HOST_WIDE_INT_UC (0x5555555555555555) >> shift;
6438    }
6439    (if (prec >= 16
6440         && prec <= 64
6441         && pow2p_hwi (prec)
6442         && TYPE_UNSIGNED (type)
6443         && integer_onep (@4)
6444         && wi::to_widest (@10) == 2
6445         && wi::to_widest (@5) == 4
6446         && wi::to_widest (@1) == prec - 8
6447         && tree_to_uhwi (@2) == c1
6448         && tree_to_uhwi (@3) == c2
6449         && tree_to_uhwi (@9) == c3
6450         && tree_to_uhwi (@7) == c3
6451         && tree_to_uhwi (@11) == c4
6452         && direct_internal_fn_supported_p (IFN_POPCOUNT, type,
6453                                            OPTIMIZE_FOR_BOTH))
6454     (convert (IFN_POPCOUNT:type @0)))))
6456 /* __builtin_ffs needs to deal on many targets with the possible zero
6457    argument.  If we know the argument is always non-zero, __builtin_ctz + 1
6458    should lead to better code.  */
6459 (simplify
6460  (FFS tree_expr_nonzero_p@0)
6461  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6462       && direct_internal_fn_supported_p (IFN_CTZ, TREE_TYPE (@0),
6463                                          OPTIMIZE_FOR_SPEED))
6464   (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
6465    (plus (CTZ:type (convert:utype @0)) { build_one_cst (type); }))))
6466 #endif
6468 (for ffs (BUILT_IN_FFS BUILT_IN_FFSL BUILT_IN_FFSLL
6469           BUILT_IN_FFSIMAX)
6470  /* __builtin_ffs (X) == 0 -> X == 0.
6471     __builtin_ffs (X) == 6 -> (X & 63) == 32.  */
6472  (for cmp (eq ne)
6473   (simplify
6474    (cmp (ffs@2 @0) INTEGER_CST@1)
6475     (with { int prec = TYPE_PRECISION (TREE_TYPE (@0)); }
6476      (switch
6477       (if (integer_zerop (@1))
6478        (cmp @0 { build_zero_cst (TREE_TYPE (@0)); }))
6479       (if (tree_int_cst_sgn (@1) < 0 || wi::to_widest (@1) > prec)
6480        { constant_boolean_node (cmp == NE_EXPR ? true : false, type); })
6481       (if (single_use (@2))
6482        (cmp (bit_and @0 { wide_int_to_tree (TREE_TYPE (@0),
6483                                             wi::mask (tree_to_uhwi (@1),
6484                                                       false, prec)); })
6485             { wide_int_to_tree (TREE_TYPE (@0),
6486                                 wi::shifted_mask (tree_to_uhwi (@1) - 1, 1,
6487                                                   false, prec)); }))))))
6489  /* __builtin_ffs (X) > 6 -> X != 0 && (X & 63) == 0.  */
6490  (for cmp (gt le)
6491       cmp2 (ne eq)
6492       cmp3 (eq ne)
6493       bit_op (bit_and bit_ior)
6494   (simplify
6495    (cmp (ffs@2 @0) INTEGER_CST@1)
6496     (with { int prec = TYPE_PRECISION (TREE_TYPE (@0)); }
6497      (switch
6498       (if (integer_zerop (@1))
6499        (cmp2 @0 { build_zero_cst (TREE_TYPE (@0)); }))
6500       (if (tree_int_cst_sgn (@1) < 0)
6501        { constant_boolean_node (cmp == GT_EXPR ? true : false, type); })
6502       (if (wi::to_widest (@1) >= prec)
6503        { constant_boolean_node (cmp == GT_EXPR ? false : true, type); })
6504       (if (wi::to_widest (@1) == prec - 1)
6505        (cmp3 @0 { wide_int_to_tree (TREE_TYPE (@0),
6506                                     wi::shifted_mask (prec - 1, 1,
6507                                                       false, prec)); }))
6508       (if (single_use (@2))
6509        (bit_op (cmp2 @0 { build_zero_cst (TREE_TYPE (@0)); })
6510                (cmp3 (bit_and @0
6511                               { wide_int_to_tree (TREE_TYPE (@0),
6512                                                   wi::mask (tree_to_uhwi (@1),
6513                                                   false, prec)); })
6514                      { build_zero_cst (TREE_TYPE (@0)); }))))))))
6516 /* Simplify:
6518      a = a1 op a2
6519      r = c ? a : b;
6521    to:
6523      r = c ? a1 op a2 : b;
6525    if the target can do it in one go.  This makes the operation conditional
6526    on c, so could drop potentially-trapping arithmetic, but that's a valid
6527    simplification if the result of the operation isn't needed.
6529    Avoid speculatively generating a stand-alone vector comparison
6530    on targets that might not support them.  Any target implementing
6531    conditional internal functions must support the same comparisons
6532    inside and outside a VEC_COND_EXPR.  */
6534 #if GIMPLE
6535 (for uncond_op (UNCOND_BINARY)
6536      cond_op (COND_BINARY)
6537  (simplify
6538   (vec_cond @0 (view_convert? (uncond_op@4 @1 @2)) @3)
6539   (with { tree op_type = TREE_TYPE (@4); }
6540    (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
6541         && element_precision (type) == element_precision (op_type))
6542     (view_convert (cond_op @0 @1 @2 (view_convert:op_type @3))))))
6543  (simplify
6544   (vec_cond @0 @1 (view_convert? (uncond_op@4 @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 (bit_not @0) @2 @3 (view_convert:op_type @1)))))))
6550 /* Same for ternary operations.  */
6551 (for uncond_op (UNCOND_TERNARY)
6552      cond_op (COND_TERNARY)
6553  (simplify
6554   (vec_cond @0 (view_convert? (uncond_op@5 @1 @2 @3)) @4)
6555   (with { tree op_type = TREE_TYPE (@5); }
6556    (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
6557         && element_precision (type) == element_precision (op_type))
6558     (view_convert (cond_op @0 @1 @2 @3 (view_convert:op_type @4))))))
6559  (simplify
6560   (vec_cond @0 @1 (view_convert? (uncond_op@5 @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 (bit_not @0) @2 @3 @4
6565                   (view_convert:op_type @1)))))))
6566 #endif
6568 /* Detect cases in which a VEC_COND_EXPR effectively replaces the
6569    "else" value of an IFN_COND_*.  */
6570 (for cond_op (COND_BINARY)
6571  (simplify
6572   (vec_cond @0 (view_convert? (cond_op @0 @1 @2 @3)) @4)
6573   (with { tree op_type = TREE_TYPE (@3); }
6574    (if (element_precision (type) == element_precision (op_type))
6575     (view_convert (cond_op @0 @1 @2 (view_convert:op_type @4))))))
6576  (simplify
6577   (vec_cond @0 @1 (view_convert? (cond_op @2 @3 @4 @5)))
6578   (with { tree op_type = TREE_TYPE (@5); }
6579    (if (inverse_conditions_p (@0, @2)
6580         && element_precision (type) == element_precision (op_type))
6581     (view_convert (cond_op @2 @3 @4 (view_convert:op_type @1)))))))
6583 /* Same for ternary operations.  */
6584 (for cond_op (COND_TERNARY)
6585  (simplify
6586   (vec_cond @0 (view_convert? (cond_op @0 @1 @2 @3 @4)) @5)
6587   (with { tree op_type = TREE_TYPE (@4); }
6588    (if (element_precision (type) == element_precision (op_type))
6589     (view_convert (cond_op @0 @1 @2 @3 (view_convert:op_type @5))))))
6590  (simplify
6591   (vec_cond @0 @1 (view_convert? (cond_op @2 @3 @4 @5 @6)))
6592   (with { tree op_type = TREE_TYPE (@6); }
6593    (if (inverse_conditions_p (@0, @2)
6594         && element_precision (type) == element_precision (op_type))
6595     (view_convert (cond_op @2 @3 @4 @5 (view_convert:op_type @1)))))))
6597 /* For pointers @0 and @2 and nonnegative constant offset @1, look for
6598    expressions like:
6600    A: (@0 + @1 < @2) | (@2 + @1 < @0)
6601    B: (@0 + @1 <= @2) | (@2 + @1 <= @0)
6603    If pointers are known not to wrap, B checks whether @1 bytes starting
6604    at @0 and @2 do not overlap, while A tests the same thing for @1 + 1
6605    bytes.  A is more efficiently tested as:
6607    A: (sizetype) (@0 + @1 - @2) > @1 * 2
6609    The equivalent expression for B is given by replacing @1 with @1 - 1:
6611    B: (sizetype) (@0 + (@1 - 1) - @2) > (@1 - 1) * 2
6613    @0 and @2 can be swapped in both expressions without changing the result.
6615    The folds rely on sizetype's being unsigned (which is always true)
6616    and on its being the same width as the pointer (which we have to check).
6618    The fold replaces two pointer_plus expressions, two comparisons and
6619    an IOR with a pointer_plus, a pointer_diff, and a comparison, so in
6620    the best case it's a saving of two operations.  The A fold retains one
6621    of the original pointer_pluses, so is a win even if both pointer_pluses
6622    are used elsewhere.  The B fold is a wash if both pointer_pluses are
6623    used elsewhere, since all we end up doing is replacing a comparison with
6624    a pointer_plus.  We do still apply the fold under those circumstances
6625    though, in case applying it to other conditions eventually makes one of the
6626    pointer_pluses dead.  */
6627 (for ior (truth_orif truth_or bit_ior)
6628  (for cmp (le lt)
6629   (simplify
6630    (ior (cmp:cs (pointer_plus@3 @0 INTEGER_CST@1) @2)
6631         (cmp:cs (pointer_plus@4 @2 @1) @0))
6632    (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
6633         && TYPE_OVERFLOW_WRAPS (sizetype)
6634         && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (sizetype))
6635     /* Calculate the rhs constant.  */
6636     (with { offset_int off = wi::to_offset (@1) - (cmp == LE_EXPR ? 1 : 0);
6637             offset_int rhs = off * 2; }
6638      /* Always fails for negative values.  */
6639      (if (wi::min_precision (rhs, UNSIGNED) <= TYPE_PRECISION (sizetype))
6640       /* Since the order of @0 and @2 doesn't matter, let tree_swap_operands_p
6641          pick a canonical order.  This increases the chances of using the
6642          same pointer_plus in multiple checks.  */
6643       (with { bool swap_p = tree_swap_operands_p (@0, @2);
6644               tree rhs_tree = wide_int_to_tree (sizetype, rhs); }
6645        (if (cmp == LT_EXPR)
6646         (gt (convert:sizetype
6647              (pointer_diff:ssizetype { swap_p ? @4 : @3; }
6648                                      { swap_p ? @0 : @2; }))
6649             { rhs_tree; })
6650         (gt (convert:sizetype
6651              (pointer_diff:ssizetype
6652               (pointer_plus { swap_p ? @2 : @0; }
6653                             { wide_int_to_tree (sizetype, off); })
6654               { swap_p ? @0 : @2; }))
6655             { rhs_tree; })))))))))
6657 /* Fold REDUC (@0 & @1) -> @0[I] & @1[I] if element I is the only nonzero
6658    element of @1.  */
6659 (for reduc (IFN_REDUC_PLUS IFN_REDUC_IOR IFN_REDUC_XOR)
6660  (simplify (reduc (view_convert? (bit_and @0 VECTOR_CST@1)))
6661   (with { int i = single_nonzero_element (@1); }
6662    (if (i >= 0)
6663     (with { tree elt = vector_cst_elt (@1, i);
6664             tree elt_type = TREE_TYPE (elt);
6665             unsigned int elt_bits = tree_to_uhwi (TYPE_SIZE (elt_type));
6666             tree size = bitsize_int (elt_bits);
6667             tree pos = bitsize_int (elt_bits * i); }
6668      (view_convert
6669       (bit_and:elt_type
6670        (BIT_FIELD_REF:elt_type @0 { size; } { pos; })
6671        { elt; })))))))
6673 (simplify
6674  (vec_perm @0 @1 VECTOR_CST@2)
6675  (with
6676   {
6677     tree op0 = @0, op1 = @1, op2 = @2;
6679     /* Build a vector of integers from the tree mask.  */
6680     vec_perm_builder builder;
6681     if (!tree_to_vec_perm_builder (&builder, op2))
6682       return NULL_TREE;
6684     /* Create a vec_perm_indices for the integer vector.  */
6685     poly_uint64 nelts = TYPE_VECTOR_SUBPARTS (type);
6686     bool single_arg = (op0 == op1);
6687     vec_perm_indices sel (builder, single_arg ? 1 : 2, nelts);
6688   }
6689   (if (sel.series_p (0, 1, 0, 1))
6690    { op0; }
6691    (if (sel.series_p (0, 1, nelts, 1))
6692     { op1; }
6693     (with
6694      {
6695        if (!single_arg)
6696          {
6697            if (sel.all_from_input_p (0))
6698              op1 = op0;
6699            else if (sel.all_from_input_p (1))
6700              {
6701                op0 = op1;
6702                sel.rotate_inputs (1);
6703              }
6704            else if (known_ge (poly_uint64 (sel[0]), nelts))
6705              {
6706                std::swap (op0, op1);
6707                sel.rotate_inputs (1);
6708              }
6709          }
6710        gassign *def;
6711        tree cop0 = op0, cop1 = op1;
6712        if (TREE_CODE (op0) == SSA_NAME
6713            && (def = dyn_cast <gassign *> (SSA_NAME_DEF_STMT (op0)))
6714            && gimple_assign_rhs_code (def) == CONSTRUCTOR)
6715          cop0 = gimple_assign_rhs1 (def);
6716        if (TREE_CODE (op1) == SSA_NAME
6717            && (def = dyn_cast <gassign *> (SSA_NAME_DEF_STMT (op1)))
6718            && gimple_assign_rhs_code (def) == CONSTRUCTOR)
6719          cop1 = gimple_assign_rhs1 (def);
6721        tree t;
6722     }
6723     (if ((TREE_CODE (cop0) == VECTOR_CST
6724           || TREE_CODE (cop0) == CONSTRUCTOR)
6725          && (TREE_CODE (cop1) == VECTOR_CST
6726              || TREE_CODE (cop1) == CONSTRUCTOR)
6727          && (t = fold_vec_perm (type, cop0, cop1, sel)))
6728      { t; }
6729      (with
6730       {
6731         bool changed = (op0 == op1 && !single_arg);
6732         tree ins = NULL_TREE;
6733         unsigned at = 0;
6735         /* See if the permutation is performing a single element
6736            insert from a CONSTRUCTOR or constant and use a BIT_INSERT_EXPR
6737            in that case.  But only if the vector mode is supported,
6738            otherwise this is invalid GIMPLE.  */
6739         if (TYPE_MODE (type) != BLKmode
6740             && (TREE_CODE (cop0) == VECTOR_CST
6741                 || TREE_CODE (cop0) == CONSTRUCTOR
6742                 || TREE_CODE (cop1) == VECTOR_CST
6743                 || TREE_CODE (cop1) == CONSTRUCTOR))
6744           {
6745             bool insert_first_p = sel.series_p (1, 1, nelts + 1, 1);
6746             if (insert_first_p)
6747               {
6748                 /* After canonicalizing the first elt to come from the
6749                    first vector we only can insert the first elt from
6750                    the first vector.  */
6751                 at = 0;
6752                 if ((ins = fold_read_from_vector (cop0, sel[0])))
6753                   op0 = op1;
6754               }
6755             /* The above can fail for two-element vectors which always
6756                appear to insert the first element, so try inserting
6757                into the second lane as well.  For more than two
6758                elements that's wasted time.  */
6759             if (!insert_first_p || (!ins && maybe_eq (nelts, 2u)))
6760               {
6761                 unsigned int encoded_nelts = sel.encoding ().encoded_nelts ();
6762                 for (at = 0; at < encoded_nelts; ++at)
6763                   if (maybe_ne (sel[at], at))
6764                     break;
6765                 if (at < encoded_nelts
6766                     && (known_eq (at + 1, nelts)
6767                         || sel.series_p (at + 1, 1, at + 1, 1)))
6768                   {
6769                     if (known_lt (poly_uint64 (sel[at]), nelts))
6770                       ins = fold_read_from_vector (cop0, sel[at]);
6771                     else
6772                       ins = fold_read_from_vector (cop1, sel[at] - nelts);
6773                   }
6774               }
6775           }
6777         /* Generate a canonical form of the selector.  */
6778         if (!ins && sel.encoding () != builder)
6779           {
6780             /* Some targets are deficient and fail to expand a single
6781                argument permutation while still allowing an equivalent
6782                2-argument version.  */
6783             tree oldop2 = op2;
6784             if (sel.ninputs () == 2
6785                || can_vec_perm_const_p (TYPE_MODE (type), sel, false))
6786               op2 = vec_perm_indices_to_tree (TREE_TYPE (op2), sel);
6787             else
6788               {
6789                 vec_perm_indices sel2 (builder, 2, nelts);
6790                 if (can_vec_perm_const_p (TYPE_MODE (type), sel2, false))
6791                   op2 = vec_perm_indices_to_tree (TREE_TYPE (op2), sel2);
6792                 else
6793                   /* Not directly supported with either encoding,
6794                      so use the preferred form.  */
6795                   op2 = vec_perm_indices_to_tree (TREE_TYPE (op2), sel);
6796               }
6797             if (!operand_equal_p (op2, oldop2, 0))
6798               changed = true;
6799           }
6800       }
6801       (if (ins)
6802        (bit_insert { op0; } { ins; }
6803          { bitsize_int (at * vector_element_bits (type)); })
6804        (if (changed)
6805         (vec_perm { op0; } { op1; } { op2; }))))))))))
6807 /* VEC_PERM_EXPR (v, v, mask) -> v where v contains same element.  */
6809 (match vec_same_elem_p
6810  @0
6811  (if (uniform_vector_p (@0))))
6813 (match vec_same_elem_p
6814  (vec_duplicate @0))
6816 (simplify
6817  (vec_perm vec_same_elem_p@0 @0 @1)
6818  @0)
6820 /* Match count trailing zeroes for simplify_count_trailing_zeroes in fwprop.
6821    The canonical form is array[((x & -x) * C) >> SHIFT] where C is a magic
6822    constant which when multiplied by a power of 2 contains a unique value
6823    in the top 5 or 6 bits.  This is then indexed into a table which maps it
6824    to the number of trailing zeroes.  */
6825 (match (ctz_table_index @1 @2 @3)
6826   (rshift (mult (bit_and:c (negate @1) @1) INTEGER_CST@2) INTEGER_CST@3))