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