c++: fix cxx_print_type's template-info dumping
[official-gcc.git] / gcc / match.pd
blob919651197f29c28e4521a8377fa1487014ad271c
1 /* Match-and-simplify patterns for shared GENERIC and GIMPLE folding.
2    This file is consumed by genmatch which produces gimple-match.cc
3    and generic-match.cc from it.
5    Copyright (C) 2014-2023 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
42    bitmask_inv_cst_vector_p)
44 /* Operator lists.  */
45 (define_operator_list tcc_comparison
46   lt   le   eq ne ge   gt   unordered ordered   unlt unle ungt unge uneq ltgt)
47 (define_operator_list inverted_tcc_comparison
48   ge   gt   ne eq lt   le   ordered   unordered ge   gt   le   lt   ltgt uneq)
49 (define_operator_list inverted_tcc_comparison_with_nans
50   unge ungt ne eq unlt unle ordered   unordered ge   gt   le   lt   ltgt uneq)
51 (define_operator_list swapped_tcc_comparison
52   gt   ge   eq ne le   lt   unordered ordered   ungt unge unlt unle uneq ltgt)
53 (define_operator_list simple_comparison         lt   le   eq ne ge   gt)
54 (define_operator_list swapped_simple_comparison gt   ge   eq ne le   lt)
55 (define_operator_list BSWAP BUILT_IN_BSWAP16 BUILT_IN_BSWAP32
56             BUILT_IN_BSWAP64 BUILT_IN_BSWAP128)
58 #include "cfn-operators.pd"
60 /* Define operand lists for math rounding functions {,i,l,ll}FN,
61    where the versions prefixed with "i" return an int, those prefixed with
62    "l" return a long and those prefixed with "ll" return a long long.
64    Also define operand lists:
66      X<FN>F for all float functions, in the order i, l, ll
67      X<FN> for all double functions, in the same order
68      X<FN>L for all long double functions, in the same order.  */
69 #define DEFINE_INT_AND_FLOAT_ROUND_FN(FN) \
70   (define_operator_list X##FN##F BUILT_IN_I##FN##F \
71                                  BUILT_IN_L##FN##F \
72                                  BUILT_IN_LL##FN##F) \
73   (define_operator_list X##FN BUILT_IN_I##FN \
74                               BUILT_IN_L##FN \
75                               BUILT_IN_LL##FN) \
76   (define_operator_list X##FN##L BUILT_IN_I##FN##L \
77                                  BUILT_IN_L##FN##L \
78                                  BUILT_IN_LL##FN##L)
80 DEFINE_INT_AND_FLOAT_ROUND_FN (FLOOR)
81 DEFINE_INT_AND_FLOAT_ROUND_FN (CEIL)
82 DEFINE_INT_AND_FLOAT_ROUND_FN (ROUND)
83 DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
85 /* Unary operations and their associated IFN_COND_* function.  */
86 (define_operator_list UNCOND_UNARY
87   negate bit_not)
88 (define_operator_list COND_UNARY
89   IFN_COND_NEG IFN_COND_NOT)
91 /* Binary operations and their associated IFN_COND_* function.  */
92 (define_operator_list UNCOND_BINARY
93   plus minus
94   mult trunc_div trunc_mod rdiv
95   min max
96   IFN_FMIN IFN_FMAX
97   bit_and bit_ior bit_xor
98   lshift rshift)
99 (define_operator_list COND_BINARY
100   IFN_COND_ADD IFN_COND_SUB
101   IFN_COND_MUL IFN_COND_DIV IFN_COND_MOD IFN_COND_RDIV
102   IFN_COND_MIN IFN_COND_MAX
103   IFN_COND_FMIN IFN_COND_FMAX
104   IFN_COND_AND IFN_COND_IOR IFN_COND_XOR
105   IFN_COND_SHL IFN_COND_SHR)
107 /* Same for ternary operations.  */
108 (define_operator_list UNCOND_TERNARY
109   IFN_FMA IFN_FMS IFN_FNMA IFN_FNMS)
110 (define_operator_list COND_TERNARY
111   IFN_COND_FMA IFN_COND_FMS IFN_COND_FNMA IFN_COND_FNMS)
113 /* __atomic_fetch_or_*, __atomic_fetch_xor_*, __atomic_xor_fetch_*  */
114 (define_operator_list ATOMIC_FETCH_OR_XOR_N
115   BUILT_IN_ATOMIC_FETCH_OR_1 BUILT_IN_ATOMIC_FETCH_OR_2
116   BUILT_IN_ATOMIC_FETCH_OR_4 BUILT_IN_ATOMIC_FETCH_OR_8
117   BUILT_IN_ATOMIC_FETCH_OR_16
118   BUILT_IN_ATOMIC_FETCH_XOR_1 BUILT_IN_ATOMIC_FETCH_XOR_2
119   BUILT_IN_ATOMIC_FETCH_XOR_4 BUILT_IN_ATOMIC_FETCH_XOR_8
120   BUILT_IN_ATOMIC_FETCH_XOR_16
121   BUILT_IN_ATOMIC_XOR_FETCH_1 BUILT_IN_ATOMIC_XOR_FETCH_2
122   BUILT_IN_ATOMIC_XOR_FETCH_4 BUILT_IN_ATOMIC_XOR_FETCH_8
123   BUILT_IN_ATOMIC_XOR_FETCH_16)
124 /* __sync_fetch_and_or_*, __sync_fetch_and_xor_*, __sync_xor_and_fetch_*  */
125 (define_operator_list SYNC_FETCH_OR_XOR_N
126   BUILT_IN_SYNC_FETCH_AND_OR_1 BUILT_IN_SYNC_FETCH_AND_OR_2
127   BUILT_IN_SYNC_FETCH_AND_OR_4 BUILT_IN_SYNC_FETCH_AND_OR_8
128   BUILT_IN_SYNC_FETCH_AND_OR_16
129   BUILT_IN_SYNC_FETCH_AND_XOR_1 BUILT_IN_SYNC_FETCH_AND_XOR_2
130   BUILT_IN_SYNC_FETCH_AND_XOR_4 BUILT_IN_SYNC_FETCH_AND_XOR_8
131   BUILT_IN_SYNC_FETCH_AND_XOR_16
132   BUILT_IN_SYNC_XOR_AND_FETCH_1 BUILT_IN_SYNC_XOR_AND_FETCH_2
133   BUILT_IN_SYNC_XOR_AND_FETCH_4 BUILT_IN_SYNC_XOR_AND_FETCH_8
134   BUILT_IN_SYNC_XOR_AND_FETCH_16)
135 /* __atomic_fetch_and_*.  */
136 (define_operator_list ATOMIC_FETCH_AND_N
137   BUILT_IN_ATOMIC_FETCH_AND_1 BUILT_IN_ATOMIC_FETCH_AND_2
138   BUILT_IN_ATOMIC_FETCH_AND_4 BUILT_IN_ATOMIC_FETCH_AND_8
139   BUILT_IN_ATOMIC_FETCH_AND_16)
140 /* __sync_fetch_and_and_*.  */
141 (define_operator_list SYNC_FETCH_AND_AND_N
142   BUILT_IN_SYNC_FETCH_AND_AND_1 BUILT_IN_SYNC_FETCH_AND_AND_2
143   BUILT_IN_SYNC_FETCH_AND_AND_4 BUILT_IN_SYNC_FETCH_AND_AND_8
144   BUILT_IN_SYNC_FETCH_AND_AND_16)
146 /* With nop_convert? combine convert? and view_convert? in one pattern
147    plus conditionalize on tree_nop_conversion_p conversions.  */
148 (match (nop_convert @0)
149  (convert @0)
150  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))))
151 (match (nop_convert @0)
152  (view_convert @0)
153  (if (VECTOR_TYPE_P (type) && VECTOR_TYPE_P (TREE_TYPE (@0))
154       && known_eq (TYPE_VECTOR_SUBPARTS (type),
155                    TYPE_VECTOR_SUBPARTS (TREE_TYPE (@0)))
156       && tree_nop_conversion_p (TREE_TYPE (type), TREE_TYPE (TREE_TYPE (@0))))))
158 #if GIMPLE
159 /* These are used by gimple_bitwise_inverted_equal_p to simplify
160    detection of BIT_NOT and comparisons. */
161 (match (bit_not_with_nop @0)
162  (bit_not @0))
163 (match (bit_not_with_nop @0)
164  (convert (bit_not @0))
165  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))))
166 (for cmp (tcc_comparison)
167  (match (maybe_cmp @0)
168   (cmp@0 @1 @2))
169  (match (maybe_cmp @0)
170   (convert (cmp@0 @1 @2))
171    (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))))
173 #endif
175 /* Transform likes of (char) ABS_EXPR <(int) x> into (char) ABSU_EXPR <x>
176    ABSU_EXPR returns unsigned absolute value of the operand and the operand
177    of the ABSU_EXPR will have the corresponding signed type.  */
178 (simplify (abs (convert @0))
179  (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
180       && !TYPE_UNSIGNED (TREE_TYPE (@0))
181       && element_precision (type) > element_precision (TREE_TYPE (@0)))
182   (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
183    (convert (absu:utype @0)))))
185 #if GIMPLE
186 /* Optimize (X + (X >> (prec - 1))) ^ (X >> (prec - 1)) into abs (X).  */
187 (simplify
188  (bit_xor:c (plus:c @0 (rshift@2 @0 INTEGER_CST@1)) @2)
189  (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
190       && !TYPE_UNSIGNED (TREE_TYPE (@0))
191       && wi::to_widest (@1) == element_precision (TREE_TYPE (@0)) - 1)
192   (abs @0)))
193 #endif
195 /* Simplifications of operations with one constant operand and
196    simplifications to constants or single values.  */
198 (for op (plus pointer_plus minus bit_ior bit_xor)
199   (simplify
200     (op @0 integer_zerop)
201     (non_lvalue @0)))
203 /* 0 +p index -> (type)index */
204 (simplify
205  (pointer_plus integer_zerop @1)
206  (non_lvalue (convert @1)))
208 /* ptr - 0 -> (type)ptr */
209 (simplify
210  (pointer_diff @0 integer_zerop)
211  (convert @0))
213 /* See if ARG1 is zero and X + ARG1 reduces to X.
214    Likewise if the operands are reversed.  */
215 (simplify
216  (plus:c @0 real_zerop@1)
217  (if (fold_real_zero_addition_p (type, @0, @1, 0))
218   (non_lvalue @0)))
220 /* See if ARG1 is zero and X - ARG1 reduces to X.  */
221 (simplify
222  (minus @0 real_zerop@1)
223  (if (fold_real_zero_addition_p (type, @0, @1, 1))
224   (non_lvalue @0)))
226 /* Even if the fold_real_zero_addition_p can't simplify X + 0.0
227    into X, we can optimize (X + 0.0) + 0.0 or (X + 0.0) - 0.0
228    or (X - 0.0) + 0.0 into X + 0.0 and (X - 0.0) - 0.0 into X - 0.0
229    if not -frounding-math.  For sNaNs the first operation would raise
230    exceptions but turn the result into qNan, so the second operation
231    would not raise it.   */
232 (for inner_op (plus minus)
233  (for outer_op (plus minus)
234   (simplify
235    (outer_op (inner_op@3 @0 REAL_CST@1) REAL_CST@2)
236     (if (real_zerop (@1)
237          && real_zerop (@2)
238          && !HONOR_SIGN_DEPENDENT_ROUNDING (type))
239      (with { bool inner_plus = ((inner_op == PLUS_EXPR)
240                                 ^ REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@1)));
241              bool outer_plus
242                = ((outer_op == PLUS_EXPR)
243                   ^ REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@2))); }
244       (if (outer_plus && !inner_plus)
245        (outer_op @0 @2)
246        @3))))))
248 /* Simplify x - x.
249    This is unsafe for certain floats even in non-IEEE formats.
250    In IEEE, it is unsafe because it does wrong for NaNs.
251    PR middle-end/98420: x - x may be -0.0 with FE_DOWNWARD.
252    Also note that operand_equal_p is always false if an operand
253    is volatile.  */
254 (simplify
255  (minus @0 @0)
256  (if (!FLOAT_TYPE_P (type)
257       || (!tree_expr_maybe_nan_p (@0)
258           && !tree_expr_maybe_infinite_p (@0)
259           && (!HONOR_SIGN_DEPENDENT_ROUNDING (type)
260               || !HONOR_SIGNED_ZEROS (type))))
261   { build_zero_cst (type); }))
262 (simplify
263  (pointer_diff @@0 @0)
264  { build_zero_cst (type); })
266 (simplify
267  (mult @0 integer_zerop@1)
268  @1)
270 /* -x == x -> x == 0 */
271 (for cmp (eq ne)
272  (simplify
273   (cmp:c @0 (negate @0))
274    (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
275         && !TYPE_OVERFLOW_WRAPS (TREE_TYPE(@0)))
276     (cmp @0 { build_zero_cst (TREE_TYPE(@0)); }))))
278 /* Maybe fold x * 0 to 0.  The expressions aren't the same
279    when x is NaN, since x * 0 is also NaN.  Nor are they the
280    same in modes with signed zeros, since multiplying a
281    negative value by 0 gives -0, not +0.  Nor when x is +-Inf,
282    since x * 0 is NaN.  */
283 (simplify
284  (mult @0 real_zerop@1)
285  (if (!tree_expr_maybe_nan_p (@0)
286       && (!HONOR_NANS (type) || !tree_expr_maybe_infinite_p (@0))
287       && (!HONOR_SIGNED_ZEROS (type) || tree_expr_nonnegative_p (@0)))
288   @1))
290 /* In IEEE floating point, x*1 is not equivalent to x for snans.
291    Likewise for complex arithmetic with signed zeros.  */
292 (simplify
293  (mult @0 real_onep)
294  (if (!tree_expr_maybe_signaling_nan_p (@0)
295       && (!HONOR_SIGNED_ZEROS (type)
296           || !COMPLEX_FLOAT_TYPE_P (type)))
297   (non_lvalue @0)))
299 /* Transform x * -1.0 into -x.  */
300 (simplify
301  (mult @0 real_minus_onep)
302   (if (!tree_expr_maybe_signaling_nan_p (@0)
303        && (!HONOR_SIGNED_ZEROS (type)
304            || !COMPLEX_FLOAT_TYPE_P (type)))
305    (negate @0)))
307 /* Transform x * { 0 or 1, 0 or 1, ... } into x & { 0 or -1, 0 or -1, ...},
308    unless the target has native support for the former but not the latter.  */
309 (simplify
310  (mult @0 VECTOR_CST@1)
311  (if (initializer_each_zero_or_onep (@1)
312       && !HONOR_SNANS (type)
313       && !HONOR_SIGNED_ZEROS (type))
314   (with { tree itype = FLOAT_TYPE_P (type) ? unsigned_type_for (type) : type; }
315    (if (itype
316         && (!VECTOR_MODE_P (TYPE_MODE (type))
317             || (VECTOR_MODE_P (TYPE_MODE (itype))
318                 && optab_handler (and_optab,
319                                   TYPE_MODE (itype)) != CODE_FOR_nothing)))
320     (view_convert (bit_and:itype (view_convert @0)
321                                  (ne @1 { build_zero_cst (type); })))))))
323 /* In SWAR (SIMD within a register) code a signed comparison of packed data
324    can be constructed with a particular combination of shift, bitwise and,
325    and multiplication by constants.  If that code is vectorized we can
326    convert this pattern into a more efficient vector comparison.  */
327 (simplify
328  (mult (bit_and (rshift @0 uniform_integer_cst_p@1)
329             uniform_integer_cst_p@2)
330     uniform_integer_cst_p@3)
331  (with {
332    tree rshift_cst = uniform_integer_cst_p (@1);
333    tree bit_and_cst = uniform_integer_cst_p (@2);
334    tree mult_cst = uniform_integer_cst_p (@3);
335   }
336   /* Make sure we're working with vectors and uniform vector constants.  */
337   (if (VECTOR_TYPE_P (type)
338        && tree_fits_uhwi_p (rshift_cst)
339        && tree_fits_uhwi_p (mult_cst)
340        && tree_fits_uhwi_p (bit_and_cst))
341    /* Compute what constants would be needed for this to represent a packed
342       comparison based on the shift amount denoted by RSHIFT_CST.  */
343    (with {
344      HOST_WIDE_INT vec_elem_bits = vector_element_bits (type);
345      poly_int64 vec_nelts = TYPE_VECTOR_SUBPARTS (type);
346      poly_int64 vec_bits = vec_elem_bits * vec_nelts;
347      unsigned HOST_WIDE_INT cmp_bits_i, bit_and_i, mult_i;
348      unsigned HOST_WIDE_INT target_mult_i, target_bit_and_i;
349      cmp_bits_i = tree_to_uhwi (rshift_cst) + 1;
350      mult_i = tree_to_uhwi (mult_cst);
351      target_mult_i = (HOST_WIDE_INT_1U << cmp_bits_i) - 1;
352      bit_and_i = tree_to_uhwi (bit_and_cst);
353      target_bit_and_i = 0;
355      /* The bit pattern in BIT_AND_I should be a mask for the least
356         significant bit of each packed element that is CMP_BITS wide.  */
357      for (unsigned i = 0; i < vec_elem_bits / cmp_bits_i; i++)
358        target_bit_and_i = (target_bit_and_i << cmp_bits_i) | 1U;
359     }
360     (if ((exact_log2 (cmp_bits_i)) >= 0
361          && cmp_bits_i < HOST_BITS_PER_WIDE_INT
362          && multiple_p (vec_bits, cmp_bits_i)
363          && vec_elem_bits <= HOST_BITS_PER_WIDE_INT
364          && target_mult_i == mult_i
365          && target_bit_and_i == bit_and_i)
366      /* Compute the vector shape for the comparison and check if the target is
367         able to expand the comparison with that type.  */
368      (with {
369        /* We're doing a signed comparison.  */
370        tree cmp_type = build_nonstandard_integer_type (cmp_bits_i, 0);
371        poly_int64 vector_type_nelts = exact_div (vec_bits, cmp_bits_i);
372        tree vec_cmp_type = build_vector_type (cmp_type, vector_type_nelts);
373        tree vec_truth_type = truth_type_for (vec_cmp_type);
374        tree zeros = build_zero_cst (vec_cmp_type);
375        tree ones = build_all_ones_cst (vec_cmp_type);
376       }
377       (if (expand_vec_cmp_expr_p (vec_cmp_type, vec_truth_type, LT_EXPR)
378            && expand_vec_cond_expr_p (vec_cmp_type, vec_truth_type, LT_EXPR))
379        (view_convert:type (vec_cond (lt:vec_truth_type
380                                      (view_convert:vec_cmp_type @0)
381                                      { zeros; })
382                            { ones; } { zeros; })))))))))
384 (for cmp (gt ge lt le)
385      outp (convert convert negate negate)
386      outn (negate negate convert convert)
387  /* Transform X * (X > 0.0 ? 1.0 : -1.0) into abs(X). */
388  /* Transform X * (X >= 0.0 ? 1.0 : -1.0) into abs(X). */
389  /* Transform X * (X < 0.0 ? 1.0 : -1.0) into -abs(X). */
390  /* Transform X * (X <= 0.0 ? 1.0 : -1.0) into -abs(X). */
391  (simplify
392   (mult:c @0 (cond (cmp @0 real_zerop) real_onep@1 real_minus_onep))
393   (if (!tree_expr_maybe_nan_p (@0) && !HONOR_SIGNED_ZEROS (type))
394    (outp (abs @0))))
395  /* Transform X * (X > 0.0 ? -1.0 : 1.0) into -abs(X). */
396  /* Transform X * (X >= 0.0 ? -1.0 : 1.0) into -abs(X). */
397  /* Transform X * (X < 0.0 ? -1.0 : 1.0) into abs(X). */
398  /* Transform X * (X <= 0.0 ? -1.0 : 1.0) into abs(X). */
399  (simplify
400   (mult:c @0 (cond (cmp @0 real_zerop) real_minus_onep real_onep@1))
401   (if (!tree_expr_maybe_nan_p (@0) && !HONOR_SIGNED_ZEROS (type))
402    (outn (abs @0)))))
404 /* Transform X * copysign (1.0, X) into abs(X). */
405 (simplify
406  (mult:c @0 (COPYSIGN_ALL real_onep @0))
407  (if (!tree_expr_maybe_nan_p (@0) && !HONOR_SIGNED_ZEROS (type))
408   (abs @0)))
410 /* Transform X * copysign (1.0, -X) into -abs(X). */
411 (simplify
412  (mult:c @0 (COPYSIGN_ALL real_onep (negate @0)))
413  (if (!tree_expr_maybe_nan_p (@0) && !HONOR_SIGNED_ZEROS (type))
414   (negate (abs @0))))
416 /* Transform copysign (CST, X) into copysign (ABS(CST), X). */
417 (simplify
418  (COPYSIGN_ALL REAL_CST@0 @1)
419  (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@0)))
420   (COPYSIGN_ALL (negate @0) @1)))
422 /* Transform c ? x * copysign (1, y) : z to c ? x ^ signs(y) : z.
423    tree-ssa-math-opts.cc does the corresponding optimization for
424    unconditional multiplications (via xorsign).  */
425 (simplify
426  (IFN_COND_MUL:c @0 @1 (IFN_COPYSIGN real_onep @2) @3)
427  (with { tree signs = sign_mask_for (type); }
428   (if (signs)
429    (with { tree inttype = TREE_TYPE (signs); }
430     (view_convert:type
431      (IFN_COND_XOR:inttype @0
432       (view_convert:inttype @1)
433       (bit_and (view_convert:inttype @2) { signs; })
434       (view_convert:inttype @3)))))))
436 /* (x >= 0 ? x : 0) + (x <= 0 ? -x : 0) -> abs x.  */
437 (simplify
438   (plus:c (max @0 integer_zerop) (max (negate @0) integer_zerop))
439   (abs @0))
441 /* X * 1, X / 1 -> X.  */
442 (for op (mult trunc_div ceil_div floor_div round_div exact_div)
443   (simplify
444     (op @0 integer_onep)
445     (non_lvalue @0)))
447 /* (A / (1 << B)) -> (A >> B).
448    Only for unsigned A.  For signed A, this would not preserve rounding
449    toward zero.
450    For example: (-1 / ( 1 << B)) !=  -1 >> B.
451    Also handle widening conversions, like:
452    (A / (unsigned long long) (1U << B)) -> (A >> B)
453    or
454    (A / (unsigned long long) (1 << B)) -> (A >> B).
455    If the left shift is signed, it can be done only if the upper bits
456    of A starting from shift's type sign bit are zero, as
457    (unsigned long long) (1 << 31) is -2147483648ULL, not 2147483648ULL,
458    so it is valid only if A >> 31 is zero.  */
459 (simplify
460  (trunc_div (convert?@0 @3) (convert2? (lshift integer_onep@1 @2)))
461  (if ((TYPE_UNSIGNED (type) || tree_expr_nonnegative_p (@0))
462       && (!VECTOR_TYPE_P (type)
463           || target_supports_op_p (type, RSHIFT_EXPR, optab_vector)
464           || target_supports_op_p (type, RSHIFT_EXPR, optab_scalar))
465       && (useless_type_conversion_p (type, TREE_TYPE (@1))
466           || (element_precision (type) >= element_precision (TREE_TYPE (@1))
467               && (TYPE_UNSIGNED (TREE_TYPE (@1))
468                   || (element_precision (type)
469                       == element_precision (TREE_TYPE (@1)))
470                   || (INTEGRAL_TYPE_P (type)
471                       && (tree_nonzero_bits (@0)
472                           & wi::mask (element_precision (TREE_TYPE (@1)) - 1,
473                                       true,
474                                       element_precision (type))) == 0)))))
475    (if (!VECTOR_TYPE_P (type)
476         && useless_type_conversion_p (TREE_TYPE (@3), TREE_TYPE (@1))
477         && element_precision (TREE_TYPE (@3)) < element_precision (type))
478     (convert (rshift @3 @2))
479     (rshift @0 @2))))
481 /* Preserve explicit divisions by 0: the C++ front-end wants to detect
482    undefined behavior in constexpr evaluation, and assuming that the division
483    traps enables better optimizations than these anyway.  */
484 (for div (trunc_div ceil_div floor_div round_div exact_div)
485  /* 0 / X is always zero.  */
486  (simplify
487   (div integer_zerop@0 @1)
488   /* But not for 0 / 0 so that we can get the proper warnings and errors.  */
489   (if (!integer_zerop (@1))
490    @0))
491  /* X / -1 is -X.  */
492  (simplify
493   (div @0 integer_minus_onep@1)
494   (if (!TYPE_UNSIGNED (type))
495    (negate @0)))
496  /* X / bool_range_Y is X.  */ 
497  (simplify
498   (div @0 SSA_NAME@1)
499   (if (INTEGRAL_TYPE_P (type)
500        && ssa_name_has_boolean_range (@1)
501        && !flag_non_call_exceptions)
502    @0))
503  /* X / X is one.  */
504  (simplify
505   (div @0 @0)
506   /* But not for 0 / 0 so that we can get the proper warnings and errors.
507      And not for _Fract types where we can't build 1.  */
508   (if (!ALL_FRACT_MODE_P (TYPE_MODE (type))
509        && !integer_zerop (@0)
510        && (!flag_non_call_exceptions || tree_expr_nonzero_p (@0)))
511    { build_one_cst (type); }))
512  /* X / abs (X) is X < 0 ? -1 : 1.  */
513  (simplify
514    (div:C @0 (abs @0))
515    (if (INTEGRAL_TYPE_P (type)
516         && TYPE_OVERFLOW_UNDEFINED (type)
517         && !integer_zerop (@0)
518         && (!flag_non_call_exceptions || tree_expr_nonzero_p (@0)))
519     (cond (lt @0 { build_zero_cst (type); })
520           { build_minus_one_cst (type); } { build_one_cst (type); })))
521  /* X / -X is -1.  */
522  (simplify
523    (div:C @0 (negate @0))
524    (if ((INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
525         && TYPE_OVERFLOW_UNDEFINED (type)
526         && !integer_zerop (@0)
527         && (!flag_non_call_exceptions || tree_expr_nonzero_p (@0)))
528     { build_minus_one_cst (type); })))
530 /* For unsigned integral types, FLOOR_DIV_EXPR is the same as
531    TRUNC_DIV_EXPR.  Rewrite into the latter in this case.  Similarly
532    for MOD instead of DIV.  */
533 (for floor_divmod (floor_div floor_mod)
534      trunc_divmod (trunc_div trunc_mod)
535  (simplify
536   (floor_divmod @0 @1)
537   (if ((INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
538        && TYPE_UNSIGNED (type))
539    (trunc_divmod @0 @1))))
541 /* 1 / X -> X == 1 for unsigned integer X.
542    1 / X -> X >= -1 && X <= 1 ? X : 0 for signed integer X.
543    But not for 1 / 0 so that we can get proper warnings and errors,
544    and not for 1-bit integers as they are edge cases better handled
545    elsewhere.  */
546 (simplify
547  (trunc_div integer_onep@0 @1)
548  (if (INTEGRAL_TYPE_P (type)
549       && TYPE_PRECISION (type) > 1
550       && !integer_zerop (@1)
551       && (!flag_non_call_exceptions || tree_expr_nonzero_p (@1)))
552   (if (TYPE_UNSIGNED (type))
553    (convert (eq:boolean_type_node @1 { build_one_cst (type); }))
554    (with { tree utype = unsigned_type_for (type); }
555     (cond (le (plus (convert:utype @1) { build_one_cst (utype); })
556               { build_int_cst (utype, 2); })
557      @1 { build_zero_cst (type); })))))
559 /* Combine two successive divisions.  Note that combining ceil_div
560    and floor_div is trickier and combining round_div even more so.  */
561 (for div (trunc_div exact_div)
562  (simplify
563   (div (div@3 @0 INTEGER_CST@1) INTEGER_CST@2)
564   (with {
565     wi::overflow_type overflow;
566     wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@2),
567                             TYPE_SIGN (type), &overflow);
568    }
569    (if (div == EXACT_DIV_EXPR
570         || optimize_successive_divisions_p (@2, @3))
571     (if (!overflow)
572      (div @0 { wide_int_to_tree (type, mul); })
573      (if (TYPE_UNSIGNED (type)
574           || mul != wi::min_value (TYPE_PRECISION (type), SIGNED))
575       { build_zero_cst (type); }))))))
577 /* Combine successive multiplications.  Similar to above, but handling
578    overflow is different.  */
579 (simplify
580  (mult (mult @0 INTEGER_CST@1) INTEGER_CST@2)
581  (with {
582    wi::overflow_type overflow;
583    wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@2),
584                            TYPE_SIGN (type), &overflow);
585   }
586   /* Skip folding on overflow: the only special case is @1 * @2 == -INT_MIN,
587      otherwise undefined overflow implies that @0 must be zero.  */
588   (if (!overflow || TYPE_OVERFLOW_WRAPS (type))
589    (mult @0 { wide_int_to_tree (type, mul); }))))
591 /* Similar to above, but there could be an extra add/sub between
592    successive multuiplications.  */
593 (simplify
594  (mult (plus:s (mult:s@4 @0 INTEGER_CST@1) INTEGER_CST@2) INTEGER_CST@3)
595  (with {
596    bool overflowed = true;
597    wi::overflow_type ovf1, ovf2;
598    wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@3),
599                            TYPE_SIGN (type), &ovf1);
600    wide_int add = wi::mul (wi::to_wide (@2), wi::to_wide (@3),
601                            TYPE_SIGN (type), &ovf2);
602   if (TYPE_OVERFLOW_UNDEFINED (type))
603     {
604 #if GIMPLE
605       value_range vr0;
606       if (ovf1 == wi::OVF_NONE && ovf2 == wi::OVF_NONE
607           && get_global_range_query ()->range_of_expr (vr0, @4)
608           && !vr0.varying_p () && !vr0.undefined_p ())
609         {
610           wide_int wmin0 = vr0.lower_bound ();
611           wide_int wmax0 = vr0.upper_bound ();
612           wmin0 = wi::mul (wmin0, wi::to_wide (@3), TYPE_SIGN (type), &ovf1);
613           wmax0 = wi::mul (wmax0, wi::to_wide (@3), TYPE_SIGN (type), &ovf2);
614           if (ovf1 == wi::OVF_NONE && ovf2 == wi::OVF_NONE)
615             {
616               wi::add (wmin0, add, TYPE_SIGN (type), &ovf1);
617               wi::add (wmax0, add, TYPE_SIGN (type), &ovf2);
618               if (ovf1 == wi::OVF_NONE && ovf2 == wi::OVF_NONE)
619                 overflowed = false;
620             }
621         }
622 #endif
623     }
624   else
625    overflowed = false;
627   /* Skip folding on overflow.  */
628   (if (!overflowed)
629    (plus (mult @0 { wide_int_to_tree (type, mul); })
630          { wide_int_to_tree (type, add); }))))
632 /* Similar to above, but a multiplication between successive additions.  */
633 (simplify
634  (plus (mult:s (plus:s @0 INTEGER_CST@1) INTEGER_CST@2) INTEGER_CST@3)
635  (with {
636    bool overflowed = true;
637    wi::overflow_type ovf1;
638    wi::overflow_type ovf2;
639    wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@2),
640                            TYPE_SIGN (type), &ovf1);
641    wide_int add = wi::add (mul, wi::to_wide (@3),
642                            TYPE_SIGN (type), &ovf2);
643   if (TYPE_OVERFLOW_UNDEFINED (type))
644     {
645 #if GIMPLE
646       value_range vr0;
647       if (ovf1 == wi::OVF_NONE && ovf2 == wi::OVF_NONE
648           && get_global_range_query ()->range_of_expr (vr0, @0)
649           && !vr0.varying_p () && !vr0.undefined_p ())
650         {
651           wide_int wmin0 = vr0.lower_bound ();
652           wide_int wmax0 = vr0.upper_bound ();
653           wmin0 = wi::mul (wmin0, wi::to_wide (@2), TYPE_SIGN (type), &ovf1);
654           wmax0 = wi::mul (wmax0, wi::to_wide (@2), TYPE_SIGN (type), &ovf2);
655           if (ovf1 == wi::OVF_NONE && ovf2 == wi::OVF_NONE)
656             {
657               wi::add (wmin0, mul, TYPE_SIGN (type), &ovf1);
658               wi::add (wmax0, mul, TYPE_SIGN (type), &ovf2);
659               if (ovf1 == wi::OVF_NONE && ovf2 == wi::OVF_NONE)
660                 overflowed = false;
661             }
662         }
663 #endif
664     }
665   else
666    overflowed = false;
668   /* Skip folding on overflow.  */
669   (if (!overflowed)
670    (plus (mult @0 @2) { wide_int_to_tree (type, add); }))))
672 /* Optimize A / A to 1.0 if we don't care about
673    NaNs or Infinities.  */
674 (simplify
675  (rdiv @0 @0)
676  (if (FLOAT_TYPE_P (type)
677       && ! HONOR_NANS (type)
678       && ! HONOR_INFINITIES (type))
679   { build_one_cst (type); }))
681 /* Optimize -A / A to -1.0 if we don't care about
682    NaNs or Infinities.  */
683 (simplify
684  (rdiv:C @0 (negate @0))
685  (if (FLOAT_TYPE_P (type)
686       && ! HONOR_NANS (type)
687       && ! HONOR_INFINITIES (type))
688   { build_minus_one_cst (type); }))
690 /* PR71078: x / abs(x) -> copysign (1.0, x) */
691 (simplify
692  (rdiv:C (convert? @0) (convert? (abs @0)))
693   (if (SCALAR_FLOAT_TYPE_P (type)
694        && ! HONOR_NANS (type)
695        && ! HONOR_INFINITIES (type))
696    (switch
697     (if (types_match (type, float_type_node))
698      (BUILT_IN_COPYSIGNF { build_one_cst (type); } (convert @0)))
699     (if (types_match (type, double_type_node))
700      (BUILT_IN_COPYSIGN { build_one_cst (type); } (convert @0)))
701     (if (types_match (type, long_double_type_node))
702      (BUILT_IN_COPYSIGNL { build_one_cst (type); } (convert @0))))))
704 /* In IEEE floating point, x/1 is not equivalent to x for snans.  */
705 (simplify
706  (rdiv @0 real_onep)
707  (if (!tree_expr_maybe_signaling_nan_p (@0))
708   (non_lvalue @0)))
710 /* In IEEE floating point, x/-1 is not equivalent to -x for snans.  */
711 (simplify
712  (rdiv @0 real_minus_onep)
713  (if (!tree_expr_maybe_signaling_nan_p (@0))
714   (negate @0)))
716 (if (flag_reciprocal_math)
717  /* Convert (A/B)/C to A/(B*C). */
718  (simplify
719   (rdiv (rdiv:s @0 @1) @2)
720   (rdiv @0 (mult @1 @2)))
722  /* Canonicalize x / (C1 * y) to (x * C2) / y.  */
723  (simplify
724   (rdiv @0 (mult:s @1 REAL_CST@2))
725   (with
726    { tree tem = const_binop (RDIV_EXPR, type, build_one_cst (type), @2); }
727    (if (tem)
728     (rdiv (mult @0 { tem; } ) @1))))
730  /* Convert A/(B/C) to (A/B)*C  */
731  (simplify
732   (rdiv @0 (rdiv:s @1 @2))
733    (mult (rdiv @0 @1) @2)))
735 /* Simplify x / (- y) to -x / y.  */
736 (simplify
737  (rdiv @0 (negate @1))
738  (rdiv (negate @0) @1))
740 (if (flag_unsafe_math_optimizations)
741  /* Simplify (C / x op 0.0) to x op 0.0 for C != 0, C != Inf/Nan.
742     Since C / x may underflow to zero, do this only for unsafe math.  */
743  (for op (lt le gt ge)
744       neg_op (gt ge lt le)
745   (simplify
746    (op (rdiv REAL_CST@0 @1) real_zerop@2)
747    (if (!HONOR_SIGNED_ZEROS (@1) && !HONOR_INFINITIES (@1))
748     (switch
749      (if (real_less (&dconst0, TREE_REAL_CST_PTR (@0)))
750       (op @1 @2))
751      /* For C < 0, use the inverted operator.  */
752      (if (real_less (TREE_REAL_CST_PTR (@0), &dconst0))
753       (neg_op @1 @2)))))))
755 /* Optimize (X & (-A)) / A where A is a power of 2, to X >> log2(A) */
756 (for div (trunc_div ceil_div floor_div round_div exact_div)
757  (simplify
758   (div (convert? (bit_and @0 INTEGER_CST@1)) INTEGER_CST@2)
759   (if (integer_pow2p (@2)
760        && tree_int_cst_sgn (@2) > 0
761        && tree_nop_conversion_p (type, TREE_TYPE (@0))
762        && wi::to_wide (@2) + wi::to_wide (@1) == 0)
763    (rshift (convert @0)
764            { build_int_cst (integer_type_node,
765                             wi::exact_log2 (wi::to_wide (@2))); }))))
767 /* If ARG1 is a constant, we can convert this to a multiply by the
768    reciprocal.  This does not have the same rounding properties,
769    so only do this if -freciprocal-math.  We can actually
770    always safely do it if ARG1 is a power of two, but it's hard to
771    tell if it is or not in a portable manner.  */
772 (for cst (REAL_CST COMPLEX_CST VECTOR_CST)
773  (simplify
774   (rdiv @0 cst@1)
775   (if (optimize)
776    (if (flag_reciprocal_math
777         && !real_zerop (@1))
778     (with
779      { tree tem = const_binop (RDIV_EXPR, type, build_one_cst (type), @1); }
780      (if (tem)
781       (mult @0 { tem; } )))
782     (if (cst != COMPLEX_CST)
783      (with { tree inverse = exact_inverse (type, @1); }
784       (if (inverse)
785        (mult @0 { inverse; } ))))))))
787 (for mod (ceil_mod floor_mod round_mod trunc_mod)
788  /* 0 % X is always zero.  */
789  (simplify
790   (mod integer_zerop@0 @1)
791   /* But not for 0 % 0 so that we can get the proper warnings and errors.  */
792   (if (!integer_zerop (@1))
793    @0))
794  /* X % 1 is always zero.  */
795  (simplify
796   (mod @0 integer_onep)
797   { build_zero_cst (type); })
798  /* X % -1 is zero.  */
799  (simplify
800   (mod @0 integer_minus_onep@1)
801   (if (!TYPE_UNSIGNED (type))
802    { build_zero_cst (type); }))
803  /* X % X is zero.  */
804  (simplify
805   (mod @0 @0)
806   /* But not for 0 % 0 so that we can get the proper warnings and errors.  */
807   (if (!integer_zerop (@0))
808    { build_zero_cst (type); }))
809  /* (X % Y) % Y is just X % Y.  */
810  (simplify
811   (mod (mod@2 @0 @1) @1)
812   @2)
813  /* From extract_muldiv_1: (X * C1) % C2 is zero if C1 is a multiple of C2.  */
814  (simplify
815   (mod (mult @0 INTEGER_CST@1) INTEGER_CST@2)
816   (if (ANY_INTEGRAL_TYPE_P (type)
817        && TYPE_OVERFLOW_UNDEFINED (type)
818        && wi::multiple_of_p (wi::to_wide (@1), wi::to_wide (@2),
819                              TYPE_SIGN (type)))
820    { build_zero_cst (type); }))
821  /* For (X % C) == 0, if X is signed and C is power of 2, use unsigned
822     modulo and comparison, since it is simpler and equivalent.  */
823  (for cmp (eq ne)
824   (simplify
825    (cmp (mod @0 integer_pow2p@2) integer_zerop@1)
826    (if (!TYPE_UNSIGNED (TREE_TYPE (@0)))
827     (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
828      (cmp (mod (convert:utype @0) (convert:utype @2)) (convert:utype @1)))))))
830 /* X % -C is the same as X % C.  */
831 (simplify
832  (trunc_mod @0 INTEGER_CST@1)
833   (if (TYPE_SIGN (type) == SIGNED
834        && !TREE_OVERFLOW (@1)
835        && wi::neg_p (wi::to_wide (@1))
836        && !TYPE_OVERFLOW_TRAPS (type)
837        /* Avoid this transformation if C is INT_MIN, i.e. C == -C.  */
838        && !sign_bit_p (@1, @1))
839    (trunc_mod @0 (negate @1))))
841 /* X % -Y is the same as X % Y.  */
842 (simplify
843  (trunc_mod @0 (convert? (negate @1)))
844  (if (INTEGRAL_TYPE_P (type)
845       && !TYPE_UNSIGNED (type)
846       && !TYPE_OVERFLOW_TRAPS (type)
847       && tree_nop_conversion_p (type, TREE_TYPE (@1))
848       /* Avoid this transformation if X might be INT_MIN or
849          Y might be -1, because we would then change valid
850          INT_MIN % -(-1) into invalid INT_MIN % -1.  */
851       && (expr_not_equal_to (@0, wi::to_wide (TYPE_MIN_VALUE (type)))
852           || expr_not_equal_to (@1, wi::minus_one (TYPE_PRECISION
853                                                         (TREE_TYPE (@1))))))
854   (trunc_mod @0 (convert @1))))
856 /* X - (X / Y) * Y is the same as X % Y.  */
857 (simplify
858  (minus (convert1? @0) (convert2? (mult:c (trunc_div @@0 @@1) @1)))
859  (if (INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
860   (convert (trunc_mod @0 @1))))
862 /* x * (1 + y / x) - y -> x - y % x */
863 (simplify
864  (minus (mult:cs @0 (plus:s (trunc_div:s @1 @0) integer_onep)) @1)
865  (if (INTEGRAL_TYPE_P (type))
866   (minus @0 (trunc_mod @1 @0))))
868 /* Optimize TRUNC_MOD_EXPR by a power of two into a BIT_AND_EXPR,
869    i.e. "X % C" into "X & (C - 1)", if X and C are positive.
870    Also optimize A % (C << N)  where C is a power of 2,
871    to A & ((C << N) - 1).
872    Also optimize "A shift (B % C)", if C is a power of 2, to
873    "A shift (B & (C - 1))".  SHIFT operation include "<<" and ">>"
874    and assume (B % C) is nonnegative as shifts negative values would
875    be UB.  */
876 (match (power_of_two_cand @1)
877  INTEGER_CST@1)
878 (match (power_of_two_cand @1)
879  (lshift INTEGER_CST@1 @2))
880 (for mod (trunc_mod floor_mod)
881  (for shift (lshift rshift)
882   (simplify
883    (shift @0 (mod @1 (power_of_two_cand@2 @3)))
884    (if (integer_pow2p (@3) && tree_int_cst_sgn (@3) > 0)
885     (shift @0 (bit_and @1 (minus @2 { build_int_cst (TREE_TYPE (@2),
886                                                       1); }))))))
887  (simplify
888   (mod @0 (convert? (power_of_two_cand@1 @2)))
889   (if ((TYPE_UNSIGNED (type) || tree_expr_nonnegative_p (@0))
890        /* Allow any integral conversions of the divisor, except
891           conversion from narrower signed to wider unsigned type
892           where if @1 would be negative power of two, the divisor
893           would not be a power of two.  */
894        && INTEGRAL_TYPE_P (type)
895        && INTEGRAL_TYPE_P (TREE_TYPE (@1))
896        && (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@1))
897            || TYPE_UNSIGNED (TREE_TYPE (@1))
898            || !TYPE_UNSIGNED (type))
899        && integer_pow2p (@2) && tree_int_cst_sgn (@2) > 0)
900    (with { tree utype = TREE_TYPE (@1);
901            if (!TYPE_OVERFLOW_WRAPS (utype))
902              utype = unsigned_type_for (utype); }
903     (bit_and @0 (convert (minus (convert:utype @1)
904                                 { build_one_cst (utype); })))))))
906 /* Simplify (unsigned t * 2)/2 -> unsigned t & 0x7FFFFFFF.  */
907 (simplify
908  (trunc_div (mult @0 integer_pow2p@1) @1)
909  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) && TYPE_UNSIGNED (TREE_TYPE (@0)))
910   (bit_and @0 { wide_int_to_tree
911                 (type, wi::mask (TYPE_PRECISION (type)
912                                  - wi::exact_log2 (wi::to_wide (@1)),
913                                  false, TYPE_PRECISION (type))); })))
915 /* Simplify (unsigned t / 2) * 2 -> unsigned t & ~1.  */
916 (simplify
917  (mult (trunc_div @0 integer_pow2p@1) @1)
918  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) && TYPE_UNSIGNED (TREE_TYPE (@0)))
919   (bit_and @0 (negate @1))))
921 /* Simplify (t * 2) / 2) -> t.  */
922 (for div (trunc_div ceil_div floor_div round_div exact_div)
923  (simplify
924   (div (mult:c @0 @1) @1)
925   (if (ANY_INTEGRAL_TYPE_P (type))
926    (if (TYPE_OVERFLOW_UNDEFINED (type))
927     @0
928 #if GIMPLE
929     (with
930      {
931        bool overflowed = true;
932        value_range vr0, vr1;
933        if (INTEGRAL_TYPE_P (type)
934            && get_range_query (cfun)->range_of_expr (vr0, @0)
935            && get_range_query (cfun)->range_of_expr (vr1, @1)
936            && !vr0.varying_p () && !vr0.undefined_p ()
937            && !vr1.varying_p () && !vr1.undefined_p ())
938          {
939            wide_int wmin0 = vr0.lower_bound ();
940            wide_int wmax0 = vr0.upper_bound ();
941            wide_int wmin1 = vr1.lower_bound ();
942            wide_int wmax1 = vr1.upper_bound ();
943            /* If the multiplication can't overflow/wrap around, then
944               it can be optimized too.  */
945            wi::overflow_type min_ovf, max_ovf;
946            wi::mul (wmin0, wmin1, TYPE_SIGN (type), &min_ovf);
947            wi::mul (wmax0, wmax1, TYPE_SIGN (type), &max_ovf);
948            if (min_ovf == wi::OVF_NONE && max_ovf == wi::OVF_NONE)
949              {
950                wi::mul (wmin0, wmax1, TYPE_SIGN (type), &min_ovf);
951                wi::mul (wmax0, wmin1, TYPE_SIGN (type), &max_ovf);
952                if (min_ovf == wi::OVF_NONE && max_ovf == wi::OVF_NONE)
953                  overflowed = false;
954              }
955          }
956      }
957     (if (!overflowed)
958      @0))
959 #endif
960    ))))
962 #if GIMPLE
963 (for div (trunc_div exact_div)
964  /* Simplify (X + M*N) / N -> X / N + M.  */
965  (simplify
966   (div (plus:c@4 @0 (mult:c@3 @1 @2)) @2)
967   (with {value_range vr0, vr1, vr2, vr3, vr4;}
968   (if (INTEGRAL_TYPE_P (type)
969        && get_range_query (cfun)->range_of_expr (vr1, @1)
970        && get_range_query (cfun)->range_of_expr (vr2, @2)
971        /* "N*M" doesn't overflow.  */
972        && range_op_handler (MULT_EXPR).overflow_free_p (vr1, vr2)
973        && get_range_query (cfun)->range_of_expr (vr0, @0)
974        && get_range_query (cfun)->range_of_expr (vr3, @3)
975        /* "X+(N*M)" doesn't overflow.  */
976        && range_op_handler (PLUS_EXPR).overflow_free_p (vr0, vr3)
977        && get_range_query (cfun)->range_of_expr (vr4, @4)
978        && !vr4.undefined_p ()
979        /* "X+N*M" is not with opposite sign as "X".  */
980        && (TYPE_UNSIGNED (type)
981            || (vr0.nonnegative_p () && vr4.nonnegative_p ())
982            || (vr0.nonpositive_p () && vr4.nonpositive_p ())))
983   (plus (div @0 @2) @1))))
985  /* Simplify (X - M*N) / N -> X / N - M.  */
986  (simplify
987   (div (minus@4 @0 (mult:c@3 @1 @2)) @2)
988   (with {value_range vr0, vr1, vr2, vr3, vr4;}
989   (if (INTEGRAL_TYPE_P (type)
990        && get_range_query (cfun)->range_of_expr (vr1, @1)
991        && get_range_query (cfun)->range_of_expr (vr2, @2)
992        /* "N * M" doesn't overflow.  */
993        && range_op_handler (MULT_EXPR).overflow_free_p (vr1, vr2)
994        && get_range_query (cfun)->range_of_expr (vr0, @0)
995        && get_range_query (cfun)->range_of_expr (vr3, @3)
996        /* "X - (N*M)" doesn't overflow.  */
997        && range_op_handler (MINUS_EXPR).overflow_free_p (vr0, vr3)
998        && get_range_query (cfun)->range_of_expr (vr4, @4)
999        && !vr4.undefined_p ()
1000        /* "X-N*M" is not with opposite sign as "X".  */
1001        && (TYPE_UNSIGNED (type)
1002            || (vr0.nonnegative_p () && vr4.nonnegative_p ())
1003            || (vr0.nonpositive_p () && vr4.nonpositive_p ())))
1004   (minus (div @0 @2) @1)))))
1006 /* Simplify
1007    (X + C) / N -> X / N + C / N where C is multiple of N.
1008    (X + C) >> N -> X >> N + C>>N if low N bits of C is 0.  */
1009 (for op (trunc_div exact_div rshift)
1010  (simplify
1011   (op (plus@3 @0 INTEGER_CST@1) INTEGER_CST@2)
1012    (with
1013     {
1014       wide_int c = wi::to_wide (@1);
1015       wide_int n = wi::to_wide (@2);
1016       bool shift = op == RSHIFT_EXPR;
1017 #define plus_op1(v) (shift ? wi::rshift (v, n, TYPE_SIGN (type)) \
1018                            : wi::div_trunc (v, n, TYPE_SIGN (type)))
1019 #define exact_mod(v) (shift ? wi::ctz (v) >= n.to_shwi () \
1020                             : wi::multiple_of_p (v, n, TYPE_SIGN (type)))
1021       value_range vr0, vr1, vr3;
1022     }
1023     (if (INTEGRAL_TYPE_P (type)
1024          && get_range_query (cfun)->range_of_expr (vr0, @0))
1025      (if (exact_mod (c)
1026           && get_range_query (cfun)->range_of_expr (vr1, @1)
1027           /* "X+C" doesn't overflow.  */
1028           && range_op_handler (PLUS_EXPR).overflow_free_p (vr0, vr1)
1029           && get_range_query (cfun)->range_of_expr (vr3, @3)
1030           && !vr3.undefined_p ()
1031           /* "X+C" and "X" are not of opposite sign.  */
1032           && (TYPE_UNSIGNED (type)
1033               || (vr0.nonnegative_p () && vr3.nonnegative_p ())
1034               || (vr0.nonpositive_p () && vr3.nonpositive_p ())))
1035        (plus (op @0 @2) { wide_int_to_tree (type, plus_op1 (c)); })
1036        (if (TYPE_UNSIGNED (type) && c.sign_mask () < 0
1037             && exact_mod (-c)
1038             /* unsigned "X-(-C)" doesn't underflow.  */
1039             && wi::geu_p (vr0.lower_bound (), -c))
1040          (plus (op @0 @2) { wide_int_to_tree (type, -plus_op1 (-c)); })))))))
1041 #undef plus_op1
1042 #undef exact_mod
1043 #endif
1045 /* (nop_outer_cast)-(inner_cast)var -> -(outer_cast)(var)
1046    if var is smaller in precision.
1047    This is always safe for both doing the negative in signed or unsigned
1048    as the value for undefined will not show up.  */
1049 (simplify
1050  (convert (negate:s@1 (convert:s @0)))
1051  (if (INTEGRAL_TYPE_P (type)
1052       && tree_nop_conversion_p (type, TREE_TYPE (@1))
1053       && TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (@0)))
1054     (negate (convert @0))))
1056 (for op (negate abs)
1057  /* Simplify cos(-x) and cos(|x|) -> cos(x).  Similarly for cosh.  */
1058  (for coss (COS COSH)
1059   (simplify
1060    (coss (op @0))
1061     (coss @0)))
1062  /* Simplify pow(-x, y) and pow(|x|,y) -> pow(x,y) if y is an even integer.  */
1063  (for pows (POW)
1064   (simplify
1065    (pows (op @0) REAL_CST@1)
1066    (with { HOST_WIDE_INT n; }
1067     (if (real_isinteger (&TREE_REAL_CST (@1), &n) && (n & 1) == 0)
1068      (pows @0 @1)))))
1069  /* Likewise for powi.  */
1070  (for pows (POWI)
1071   (simplify
1072    (pows (op @0) INTEGER_CST@1)
1073    (if ((wi::to_wide (@1) & 1) == 0)
1074     (pows @0 @1))))
1075  /* Strip negate and abs from both operands of hypot.  */
1076  (for hypots (HYPOT)
1077   (simplify
1078    (hypots (op @0) @1)
1079    (hypots @0 @1))
1080   (simplify
1081    (hypots @0 (op @1))
1082    (hypots @0 @1)))
1083  /* copysign(-x, y) and copysign(abs(x), y) -> copysign(x, y).  */
1084  (for copysigns (COPYSIGN_ALL)
1085   (simplify
1086    (copysigns (op @0) @1)
1087    (copysigns @0 @1))))
1089 /* abs(x)*abs(x) -> x*x.  Should be valid for all types.  */
1090 (simplify
1091  (mult (abs@1 @0) @1)
1092  (mult @0 @0))
1094 /* Convert absu(x)*absu(x) -> x*x.  */
1095 (simplify
1096  (mult (absu@1 @0) @1)
1097  (mult (convert@2 @0) @2))
1099 /* cos(copysign(x, y)) -> cos(x).  Similarly for cosh.  */
1100 (for coss (COS COSH)
1101      copysigns (COPYSIGN)
1102  (simplify
1103   (coss (copysigns @0 @1))
1104    (coss @0)))
1106 /* pow(copysign(x, y), z) -> pow(x, z) if z is an even integer.  */
1107 (for pows (POW)
1108      copysigns (COPYSIGN)
1109  (simplify
1110   (pows (copysigns @0 @2) REAL_CST@1)
1111   (with { HOST_WIDE_INT n; }
1112    (if (real_isinteger (&TREE_REAL_CST (@1), &n) && (n & 1) == 0)
1113     (pows @0 @1)))))
1114 /* Likewise for powi.  */
1115 (for pows (POWI)
1116      copysigns (COPYSIGN)
1117  (simplify
1118   (pows (copysigns @0 @2) INTEGER_CST@1)
1119   (if ((wi::to_wide (@1) & 1) == 0)
1120    (pows @0 @1))))
1122 (for hypots (HYPOT)
1123      copysigns (COPYSIGN)
1124  /* hypot(copysign(x, y), z) -> hypot(x, z).  */
1125  (simplify
1126   (hypots (copysigns @0 @1) @2)
1127   (hypots @0 @2))
1128  /* hypot(x, copysign(y, z)) -> hypot(x, y).  */
1129  (simplify
1130   (hypots @0 (copysigns @1 @2))
1131   (hypots @0 @1)))
1133 /* copysign(x, CST) -> [-]abs (x).  */
1134 (for copysigns (COPYSIGN_ALL)
1135  (simplify
1136   (copysigns @0 REAL_CST@1)
1137   (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
1138    (negate (abs @0))
1139    (abs @0))))
1141 /* copysign(copysign(x, y), z) -> copysign(x, z).  */
1142 (for copysigns (COPYSIGN_ALL)
1143  (simplify
1144   (copysigns (copysigns @0 @1) @2)
1145   (copysigns @0 @2)))
1147 /* copysign(x,y)*copysign(x,y) -> x*x.  */
1148 (for copysigns (COPYSIGN_ALL)
1149  (simplify
1150   (mult (copysigns@2 @0 @1) @2)
1151   (mult @0 @0)))
1153 /* ccos(-x) -> ccos(x).  Similarly for ccosh.  */
1154 (for ccoss (CCOS CCOSH)
1155  (simplify
1156   (ccoss (negate @0))
1157    (ccoss @0)))
1159 /* cabs(-x) and cos(conj(x)) -> cabs(x).  */
1160 (for ops (conj negate)
1161  (for cabss (CABS)
1162   (simplify
1163    (cabss (ops @0))
1164    (cabss @0))))
1166 /* Fold (a * (1 << b)) into (a << b)  */
1167 (simplify
1168  (mult:c @0 (convert? (lshift integer_onep@1 @2)))
1169   (if (! FLOAT_TYPE_P (type)
1170        && tree_nop_conversion_p (type, TREE_TYPE (@1)))
1171    (lshift @0 @2)))
1173 /* Shifts by precision or greater result in zero.  */
1174 (for shift (lshift rshift)
1175  (simplify
1176   (shift @0 uniform_integer_cst_p@1)
1177   (if ((GIMPLE || !sanitize_flags_p (SANITIZE_SHIFT_EXPONENT))
1178        /* Leave arithmetic right shifts of possibly negative values alone.  */
1179        && (TYPE_UNSIGNED (type)
1180            || shift == LSHIFT_EXPR
1181            || tree_expr_nonnegative_p (@0))
1182        /* Use a signed compare to leave negative shift counts alone.  */
1183        && wi::ges_p (wi::to_wide (uniform_integer_cst_p (@1)),
1184                      element_precision (type)))
1185    { build_zero_cst (type); })))
1187 /* Shifts by constants distribute over several binary operations,
1188    hence (X << C) + (Y << C) can be simplified to (X + Y) << C.  */
1189 (for op (plus minus)
1190   (simplify
1191     (op (lshift:s @0 @1) (lshift:s @2 @1))
1192     (if (INTEGRAL_TYPE_P (type)
1193          && TYPE_OVERFLOW_WRAPS (type)
1194          && !TYPE_SATURATING (type))
1195       (lshift (op @0 @2) @1))))
1197 (for op (bit_and bit_ior bit_xor)
1198   (simplify
1199     (op (lshift:s @0 @1) (lshift:s @2 @1))
1200     (if (INTEGRAL_TYPE_P (type))
1201       (lshift (op @0 @2) @1)))
1202   (simplify
1203     (op (rshift:s @0 @1) (rshift:s @2 @1))
1204     (if (INTEGRAL_TYPE_P (type))
1205       (rshift (op @0 @2) @1))))
1207 /* Fold (1 << (C - x)) where C = precision(type) - 1
1208    into ((1 << C) >> x). */
1209 (simplify
1210  (lshift integer_onep@0 (minus@1 INTEGER_CST@2 @3))
1211   (if (INTEGRAL_TYPE_P (type)
1212        && wi::eq_p (wi::to_wide (@2), TYPE_PRECISION (type) - 1)
1213        && single_use (@1))
1214    (if (TYPE_UNSIGNED (type))
1215      (rshift (lshift @0 @2) @3)
1216    (with
1217     { tree utype = unsigned_type_for (type); }
1218     (convert (rshift (lshift (convert:utype @0) @2) @3))))))
1220 /* Fold ((type)(a<0)) << SIGNBITOFA into ((type)a) & signbit. */
1221 (simplify
1222  (lshift (convert (lt @0 integer_zerop@1)) INTEGER_CST@2)
1223  (if (TYPE_SIGN (TREE_TYPE (@0)) == SIGNED
1224       && wi::eq_p (wi::to_wide (@2), TYPE_PRECISION (TREE_TYPE (@0)) - 1))
1225   (with { wide_int wone = wi::one (TYPE_PRECISION (type)); }
1226    (bit_and (convert @0)
1227             { wide_int_to_tree (type,
1228                                 wi::lshift (wone, wi::to_wide (@2))); }))))
1230 /* Fold (-x >> C) into -(x > 0) where C = precision(type) - 1.  */
1231 (for cst (INTEGER_CST VECTOR_CST)
1232  (simplify
1233   (rshift (negate:s @0) cst@1)
1234    (if (!TYPE_UNSIGNED (type)
1235         && TYPE_OVERFLOW_UNDEFINED (type))
1236     (with { tree stype = TREE_TYPE (@1);
1237             tree bt = truth_type_for (type);
1238             tree zeros = build_zero_cst (type);
1239             tree cst = NULL_TREE; }
1240      (switch
1241       /* Handle scalar case.  */
1242       (if (INTEGRAL_TYPE_P (type)
1243            /* If we apply the rule to the scalar type before vectorization
1244               we will enforce the result of the comparison being a bool
1245               which will require an extra AND on the result that will be
1246               indistinguishable from when the user did actually want 0
1247               or 1 as the result so it can't be removed.  */
1248            && canonicalize_math_after_vectorization_p ()
1249            && wi::eq_p (wi::to_wide (@1), TYPE_PRECISION (type) - 1))
1250        (negate (convert (gt @0 { zeros; }))))
1251       /* Handle vector case.  */
1252       (if (VECTOR_INTEGER_TYPE_P (type)
1253            /* First check whether the target has the same mode for vector
1254               comparison results as it's operands do.  */
1255            && TYPE_MODE (bt) == TYPE_MODE (type)
1256            /* Then check to see if the target is able to expand the comparison
1257               with the given type later on, otherwise we may ICE.  */
1258            && expand_vec_cmp_expr_p (type, bt, GT_EXPR)
1259            && (cst = uniform_integer_cst_p (@1)) != NULL
1260            && wi::eq_p (wi::to_wide (cst), element_precision (type) - 1))
1261        (view_convert (gt:bt @0 { zeros; }))))))))
1263 /* Fold (C1/X)*C2 into (C1*C2)/X.  */
1264 (simplify
1265  (mult (rdiv@3 REAL_CST@0 @1) REAL_CST@2)
1266   (if (flag_associative_math
1267        && single_use (@3))
1268    (with
1269     { tree tem = const_binop (MULT_EXPR, type, @0, @2); }
1270     (if (tem)
1271      (rdiv { tem; } @1)))))
1273 /* Simplify ~X & X as zero.  */
1274 (simplify
1275  (bit_and (convert? @0) (convert? @1))
1276  (with { bool wascmp; }
1277   (if (types_match (TREE_TYPE (@0), TREE_TYPE (@1))
1278        && bitwise_inverted_equal_p (@0, @1, wascmp))
1279    { wascmp ? constant_boolean_node (false, type) : build_zero_cst (type); })))
1281 /* PR71636: Transform x & ((1U << b) - 1) -> x & ~(~0U << b);  */
1282 (simplify
1283   (bit_and:c @0 (plus:s (lshift:s integer_onep @1) integer_minus_onep))
1284   (if (TYPE_UNSIGNED (type))
1285     (bit_and @0 (bit_not (lshift { build_all_ones_cst (type); } @1)))))
1287 (for bitop (bit_and bit_ior)
1288      cmp (eq ne)
1289  /* PR35691: Transform
1290     (x == 0 & y == 0) -> (x | typeof(x)(y)) == 0.
1291     (x != 0 | y != 0) -> (x | typeof(x)(y)) != 0.  */
1292  (simplify
1293   (bitop (cmp @0 integer_zerop@2) (cmp @1 integer_zerop))
1294    (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1295         && INTEGRAL_TYPE_P (TREE_TYPE (@1))
1296         && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
1297     (cmp (bit_ior @0 (convert @1)) @2)))
1298  /* Transform:
1299     (x == -1 & y == -1) -> (x & typeof(x)(y)) == -1.
1300     (x != -1 | y != -1) -> (x & typeof(x)(y)) != -1.  */
1301  (simplify
1302   (bitop (cmp @0 integer_all_onesp@2) (cmp @1 integer_all_onesp))
1303    (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1304         && INTEGRAL_TYPE_P (TREE_TYPE (@1))
1305         && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
1306     (cmp (bit_and @0 (convert @1)) @2))))
1308 /* Fold (A & ~B) - (A & B) into (A ^ B) - B.  */
1309 (simplify
1310  (minus (bit_and:cs @0 (bit_not @1)) (bit_and:cs @0 @1))
1311   (minus (bit_xor @0 @1) @1))
1312 (simplify
1313  (minus (bit_and:s @0 INTEGER_CST@2) (bit_and:s @0 INTEGER_CST@1))
1314  (if (~wi::to_wide (@2) == wi::to_wide (@1))
1315   (minus (bit_xor @0 @1) @1)))
1317 /* Fold (A & B) - (A & ~B) into B - (A ^ B).  */
1318 (simplify
1319  (minus (bit_and:cs @0 @1) (bit_and:cs @0 (bit_not @1)))
1320   (minus @1 (bit_xor @0 @1)))
1322 /* Simplify (X & ~Y) |^+ (~X & Y) -> X ^ Y.  */
1323 (for op (bit_ior bit_xor plus)
1324  (simplify
1325   (op (bit_and:c @0 @2) (bit_and:c @3 @1))
1326   (with { bool wascmp0, wascmp1; }
1327    (if (bitwise_inverted_equal_p (@2, @1, wascmp0)
1328         && bitwise_inverted_equal_p (@0, @3, wascmp1)
1329         && ((!wascmp0 && !wascmp1)
1330             || element_precision (type) == 1))
1331    (bit_xor @0 @1)))))
1333 /* PR53979: Transform ((a ^ b) | a) -> (a | b) */
1334 (simplify
1335   (bit_ior:c (bit_xor:c @0 @1) @0)
1336   (bit_ior @0 @1))
1338 /* (a & ~b) | (a ^ b)  -->  a ^ b  */
1339 (simplify
1340  (bit_ior:c (bit_and:c @0 (bit_not @1)) (bit_xor:c@2 @0 @1))
1341  @2)
1343 /* (a & ~b) ^ ~a  -->  ~(a & b)  */
1344 (simplify
1345  (bit_xor:c (bit_and:cs @0 (bit_not @1)) (bit_not @0))
1346  (bit_not (bit_and @0 @1)))
1348 /* (~a & b) ^ a  -->   (a | b)   */
1349 (simplify
1350  (bit_xor:c (bit_and:cs (bit_not @0) @1) @0)
1351  (bit_ior @0 @1))
1353 /* (a | b) & ~(a ^ b)  -->  a & b  */
1354 (simplify
1355  (bit_and:c (bit_ior @0 @1) (bit_not (bit_xor:c @0 @1)))
1356  (bit_and @0 @1))
1358 /* (a | b) & (a == b)  -->  a & b (boolean version of the above).  */
1359 (simplify
1360  (bit_and:c (bit_ior @0 @1) (nop_convert? (eq:c @0 @1)))
1361  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1362       && TYPE_PRECISION (TREE_TYPE (@0)) == 1)
1363   (bit_and @0 @1)))
1365 /* a | ~(a ^ b)  -->  a | ~b  */
1366 (simplify
1367  (bit_ior:c @0 (bit_not:s (bit_xor:c @0 @1)))
1368  (bit_ior @0 (bit_not @1)))
1370 /* a | (a == b)  -->  a | (b^1) (boolean version of the above). */
1371 (simplify
1372  (bit_ior:c @0 (nop_convert? (eq:c @0 @1)))
1373  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1374       && TYPE_PRECISION (TREE_TYPE (@0)) == 1)
1375   (bit_ior @0 (bit_xor @1 { build_one_cst (type); }))))
1377 /* (a | b) | (a &^ b)  -->  a | b  */
1378 (for op (bit_and bit_xor)
1379  (simplify
1380   (bit_ior:c (bit_ior@2 @0 @1) (op:c @0 @1))
1381   @2))
1383 /* (a & b) | ~(a ^ b)  -->  ~(a ^ b)  */
1384 (simplify
1385  (bit_ior:c (bit_and:c @0 @1) (bit_not@2 (bit_xor @0 @1)))
1386  @2)
1388 /* (a & b) | (a == b)  -->  a == b  */
1389 (simplify
1390  (bit_ior:c (bit_and:c @0 @1) (nop_convert?@2 (eq @0 @1)))
1391  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1392       && TYPE_PRECISION (TREE_TYPE (@0)) == 1)
1393   @2))
1395 /* ~(~a & b)  -->  a | ~b  */
1396 (simplify
1397  (bit_not (bit_and:cs (bit_not @0) @1))
1398  (bit_ior @0 (bit_not @1)))
1400 /* ~(~a | b) --> a & ~b */
1401 (simplify
1402  (bit_not (bit_ior:cs (bit_not @0) @1))
1403  (bit_and @0 (bit_not @1)))
1405 /* (a ^ b) & ((b ^ c) ^ a) --> (a ^ b) & ~c */
1406 (simplify
1407  (bit_and:c (bit_xor:c@3 @0 @1) (bit_xor:cs (bit_xor:cs @1 @2) @0))
1408  (bit_and @3 (bit_not @2)))
1410 /* (a ^ b) | ((b ^ c) ^ a) --> (a ^ b) | c */
1411 (simplify
1412  (bit_ior:c (bit_xor:c@3 @0 @1) (bit_xor:c (bit_xor:c @1 @2) @0))
1413  (bit_ior @3 @2))
1415 /* (~X | C) ^ D -> (X | C) ^ (~D ^ C) if (~D ^ C) can be simplified.  */
1416 (simplify
1417  (bit_xor:c (bit_ior:cs (bit_not:s @0) @1) @2)
1418   (bit_xor (bit_ior @0 @1) (bit_xor! (bit_not! @2) @1)))
1420 /* (~X & C) ^ D -> (X & C) ^ (D ^ C) if (D ^ C) can be simplified.  */
1421 (simplify
1422  (bit_xor:c (bit_and:cs (bit_not:s @0) @1) @2)
1423   (bit_xor (bit_and @0 @1) (bit_xor! @2 @1)))
1425 /* Simplify (~X & Y) to X ^ Y if we know that (X & ~Y) is 0.  */
1426 (simplify
1427  (bit_and (bit_not SSA_NAME@0) INTEGER_CST@1)
1428  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1429       && wi::bit_and_not (get_nonzero_bits (@0), wi::to_wide (@1)) == 0)
1430   (bit_xor @0 @1)))
1432 /* For constants M and N, if M == (1LL << cst) - 1 && (N & M) == M,
1433    ((A & N) + B) & M -> (A + B) & M
1434    Similarly if (N & M) == 0,
1435    ((A | N) + B) & M -> (A + B) & M
1436    and for - instead of + (or unary - instead of +)
1437    and/or ^ instead of |.
1438    If B is constant and (B & M) == 0, fold into A & M.  */
1439 (for op (plus minus)
1440  (for bitop (bit_and bit_ior bit_xor)
1441   (simplify
1442    (bit_and (op:s (bitop:s@0 @3 INTEGER_CST@4) @1) INTEGER_CST@2)
1443     (with
1444      { tree pmop[2];
1445        tree utype = fold_bit_and_mask (TREE_TYPE (@0), @2, op, @0, bitop,
1446                                        @3, @4, @1, ERROR_MARK, NULL_TREE,
1447                                        NULL_TREE, pmop); }
1448      (if (utype)
1449       (convert (bit_and (op (convert:utype { pmop[0]; })
1450                             (convert:utype { pmop[1]; }))
1451                         (convert:utype @2))))))
1452   (simplify
1453    (bit_and (op:s @0 (bitop:s@1 @3 INTEGER_CST@4)) INTEGER_CST@2)
1454     (with
1455      { tree pmop[2];
1456        tree utype = fold_bit_and_mask (TREE_TYPE (@0), @2, op, @0, ERROR_MARK,
1457                                        NULL_TREE, NULL_TREE, @1, bitop, @3,
1458                                        @4, pmop); }
1459      (if (utype)
1460       (convert (bit_and (op (convert:utype { pmop[0]; })
1461                             (convert:utype { pmop[1]; }))
1462                         (convert:utype @2)))))))
1463  (simplify
1464   (bit_and (op:s @0 @1) INTEGER_CST@2)
1465    (with
1466     { tree pmop[2];
1467       tree utype = fold_bit_and_mask (TREE_TYPE (@0), @2, op, @0, ERROR_MARK,
1468                                       NULL_TREE, NULL_TREE, @1, ERROR_MARK,
1469                                       NULL_TREE, NULL_TREE, pmop); }
1470     (if (utype)
1471      (convert (bit_and (op (convert:utype { pmop[0]; })
1472                            (convert:utype { pmop[1]; }))
1473                        (convert:utype @2)))))))
1474 (for bitop (bit_and bit_ior bit_xor)
1475  (simplify
1476   (bit_and (negate:s (bitop:s@0 @2 INTEGER_CST@3)) INTEGER_CST@1)
1477    (with
1478     { tree pmop[2];
1479       tree utype = fold_bit_and_mask (TREE_TYPE (@0), @1, NEGATE_EXPR, @0,
1480                                       bitop, @2, @3, NULL_TREE, ERROR_MARK,
1481                                       NULL_TREE, NULL_TREE, pmop); }
1482     (if (utype)
1483      (convert (bit_and (negate (convert:utype { pmop[0]; }))
1484                        (convert:utype @1)))))))
1486 /* X % Y is smaller than Y.  */
1487 (for cmp (lt ge)
1488  (simplify
1489   (cmp:c (trunc_mod @0 @1) @1)
1490   (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
1491    { constant_boolean_node (cmp == LT_EXPR, type); })))
1493 /* x | ~0 -> ~0  */
1494 (simplify
1495  (bit_ior @0 integer_all_onesp@1)
1496  @1)
1498 /* x | 0 -> x  */
1499 (simplify
1500  (bit_ior @0 integer_zerop)
1501  @0)
1503 /* x & 0 -> 0  */
1504 (simplify
1505  (bit_and @0 integer_zerop@1)
1506  @1)
1508 /* ~x | x -> -1 */
1509 /* ~x ^ x -> -1 */
1510 (for op (bit_ior bit_xor)
1511  (simplify
1512   (op (convert? @0) (convert? @1))
1513   (with { bool wascmp; }
1514    (if (types_match (TREE_TYPE (@0), TREE_TYPE (@1))
1515         && bitwise_inverted_equal_p (@0, @1, wascmp))
1516     (convert
1517      { wascmp
1518         ? constant_boolean_node (true, type)
1519         : build_all_ones_cst (TREE_TYPE (@0)); })))))
1521 /* x ^ x -> 0 */
1522 (simplify
1523   (bit_xor @0 @0)
1524   { build_zero_cst (type); })
1526 /* Canonicalize X ^ ~0 to ~X.  */
1527 (simplify
1528   (bit_xor @0 integer_all_onesp@1)
1529   (bit_not @0))
1531 /* x & ~0 -> x  */
1532 (simplify
1533  (bit_and @0 integer_all_onesp)
1534   (non_lvalue @0))
1536 /* x & x -> x,  x | x -> x  */
1537 (for bitop (bit_and bit_ior)
1538  (simplify
1539   (bitop @0 @0)
1540   (non_lvalue @0)))
1542 /* x & C -> x if we know that x & ~C == 0.  */
1543 #if GIMPLE
1544 (simplify
1545  (bit_and SSA_NAME@0 INTEGER_CST@1)
1546  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1547       && wi::bit_and_not (get_nonzero_bits (@0), wi::to_wide (@1)) == 0)
1548   @0))
1549 /* x | C -> C if we know that x & ~C == 0.  */
1550 (simplify
1551  (bit_ior SSA_NAME@0 INTEGER_CST@1)
1552  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
1553       && wi::bit_and_not (get_nonzero_bits (@0), wi::to_wide (@1)) == 0)
1554   @1))
1555 #endif
1557 /* ~(~X - Y) -> X + Y and ~(~X + Y) -> X - Y.  */
1558 (simplify
1559  (bit_not (minus (bit_not @0) @1))
1560  (plus @0 @1))
1561 (simplify
1562  (bit_not (plus:c (bit_not @0) @1))
1563  (minus @0 @1))
1564 /* (~X - ~Y) -> Y - X.  */
1565 (simplify
1566  (minus (bit_not @0) (bit_not @1))
1567   (if (!TYPE_OVERFLOW_SANITIZED (type))
1568    (with { tree utype = unsigned_type_for (type); }
1569     (convert (minus (convert:utype @1) (convert:utype @0))))))
1571 /* ~(X - Y) -> ~X + Y.  */
1572 (simplify
1573  (bit_not (minus:s @0 @1))
1574  (plus (bit_not @0) @1))
1575 (simplify
1576  (bit_not (plus:s @0 INTEGER_CST@1))
1577  (if ((INTEGRAL_TYPE_P (type)
1578        && TYPE_UNSIGNED (type))
1579       || (!TYPE_OVERFLOW_SANITIZED (type)
1580           && may_negate_without_overflow_p (@1)))
1581   (plus (bit_not @0) { const_unop (NEGATE_EXPR, type, @1); })))
1583 #if GIMPLE
1584 /* ~X + Y -> (Y - X) - 1.  */
1585 (simplify
1586  (plus:c (bit_not @0) @1)
1587   (if (ANY_INTEGRAL_TYPE_P (type)
1588        && TYPE_OVERFLOW_WRAPS (type)
1589        /* -1 - X is folded to ~X, so we'd recurse endlessly.  */
1590        && !integer_all_onesp (@1))
1591    (plus (minus @1 @0) { build_minus_one_cst (type); })
1592    (if (INTEGRAL_TYPE_P (type)
1593         && TREE_CODE (@1) == INTEGER_CST
1594         && wi::to_wide (@1) != wi::min_value (TYPE_PRECISION (type),
1595                                               SIGNED))
1596     (minus (plus @1 { build_minus_one_cst (type); }) @0))))
1597 #endif
1599 /* ~(X >> Y) -> ~X >> Y if ~X can be simplified.  */
1600 (simplify
1601  (bit_not (rshift:s @0 @1))
1602   (if (!TYPE_UNSIGNED (TREE_TYPE (@0)))
1603    (rshift (bit_not! @0) @1)
1604    /* For logical right shifts, this is possible only if @0 doesn't
1605       have MSB set and the logical right shift is changed into
1606       arithmetic shift.  */
1607    (if (INTEGRAL_TYPE_P (type)
1608         && !wi::neg_p (tree_nonzero_bits (@0)))
1609     (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
1610      (convert (rshift (bit_not! (convert:stype @0)) @1))))))
1612 /* x + (x & 1) -> (x + 1) & ~1 */
1613 (simplify
1614  (plus:c @0 (bit_and:s @0 integer_onep@1))
1615  (bit_and (plus @0 @1) (bit_not @1)))
1617 /* x & ~(x & y) -> x & ~y */
1618 /* x | ~(x | y) -> x | ~y  */
1619 (for bitop (bit_and bit_ior)
1620  (simplify
1621   (bitop:c @0 (bit_not (bitop:cs @0 @1)))
1622   (bitop @0 (bit_not @1))))
1624 /* (~x & y) | ~(x | y) -> ~x */
1625 (simplify
1626  (bit_ior:c (bit_and:c (bit_not@2 @0) @1) (bit_not (bit_ior:c @0 @1)))
1627  @2)
1629 /* (x | y) ^ (x | ~y) -> ~x */
1630 (simplify
1631  (bit_xor:c (bit_ior:c @0 @1) (bit_ior:c @0 (bit_not @1)))
1632  (bit_not @0))
1634 /* (x & y) | ~(x | y) -> ~(x ^ y) */
1635 (simplify
1636  (bit_ior:c (bit_and:s @0 @1) (bit_not:s (bit_ior:s @0 @1)))
1637  (bit_not (bit_xor @0 @1)))
1639 /* (~x | y) ^ (x ^ y) -> x | ~y */
1640 (simplify
1641  (bit_xor:c (bit_ior:cs (bit_not @0) @1) (bit_xor:s @0 @1))
1642  (bit_ior @0 (bit_not @1)))
1644 /* (x ^ y) | ~(x | y) -> ~(x & y) */
1645 (simplify
1646  (bit_ior:c (bit_xor:s @0 @1) (bit_not:s (bit_ior:s @0 @1)))
1647  (bit_not (bit_and @0 @1)))
1649 /* (x & y) ^ (x | y) -> x ^ y */
1650 (simplify
1651  (bit_xor:c (bit_and @0 @1) (bit_ior @0 @1))
1652  (bit_xor @0 @1))
1654 /* (x ^ y) ^ (x | y) -> x & y */
1655 (simplify
1656  (bit_xor:c (bit_xor @0 @1) (bit_ior @0 @1))
1657  (bit_and @0 @1))
1659 /* (x & y) + (x ^ y) -> x | y */
1660 /* (x & y) | (x ^ y) -> x | y */
1661 /* (x & y) ^ (x ^ y) -> x | y */
1662 (for op (plus bit_ior bit_xor)
1663  (simplify
1664   (op:c (bit_and @0 @1) (bit_xor @0 @1))
1665   (bit_ior @0 @1)))
1667 /* (x & y) + (x | y) -> x + y */
1668 (simplify
1669  (plus:c (bit_and @0 @1) (bit_ior @0 @1))
1670  (plus @0 @1))
1672 /* (x + y) - (x | y) -> x & y */
1673 (simplify
1674  (minus (plus @0 @1) (bit_ior @0 @1))
1675  (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1676       && !TYPE_SATURATING (type))
1677   (bit_and @0 @1)))
1679 /* (x + y) - (x & y) -> x | y */
1680 (simplify
1681  (minus (plus @0 @1) (bit_and @0 @1))
1682  (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1683       && !TYPE_SATURATING (type))
1684   (bit_ior @0 @1)))
1686 /* (x | y) - y -> (x & ~y) */
1687 (simplify
1688  (minus (bit_ior:cs @0 @1) @1)
1689  (bit_and @0 (bit_not @1)))
1691 /* (x | y) - (x ^ y) -> x & y */
1692 (simplify
1693  (minus (bit_ior @0 @1) (bit_xor @0 @1))
1694  (bit_and @0 @1))
1696 /* (x | y) - (x & y) -> x ^ y */
1697 (simplify
1698  (minus (bit_ior @0 @1) (bit_and @0 @1))
1699  (bit_xor @0 @1))
1701 /* (x | y) & ~(x & y) -> x ^ y */
1702 (simplify
1703  (bit_and:c (bit_ior @0 @1) (bit_not (bit_and @0 @1)))
1704  (bit_xor @0 @1))
1706 /* (x | y) & (~x ^ y) -> x & y */
1707 (simplify
1708  (bit_and:c (bit_ior:c @0 @1) (bit_xor:c @1 @2))
1709  (with { bool wascmp; }
1710   (if (bitwise_inverted_equal_p (@0, @2, wascmp)
1711        && (!wascmp || element_precision (type) == 1))
1712    (bit_and @0 @1))))
1714 /* (~x | y) & (x | ~y) -> ~(x ^ y) */
1715 (simplify
1716  (bit_and (bit_ior:cs (bit_not @0) @1) (bit_ior:cs @0 (bit_not @1)))
1717  (bit_not (bit_xor @0 @1)))
1719 /* (~x | y) ^ (x | ~y) -> x ^ y */
1720 (simplify
1721  (bit_xor (bit_ior:c (bit_not @0) @1) (bit_ior:c @0 (bit_not @1)))
1722  (bit_xor @0 @1))
1724 /* ((x & y) - (x | y)) - 1 -> ~(x ^ y) */
1725 (simplify
1726  (plus (nop_convert1? (minus@2 (nop_convert2? (bit_and:c @0 @1))
1727                               (nop_convert2? (bit_ior @0 @1))))
1728        integer_all_onesp)
1729  (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1730       && !TYPE_SATURATING (type) && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2))
1731       && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@2))
1732       && !TYPE_SATURATING (TREE_TYPE (@2)))
1733  (bit_not (convert (bit_xor @0 @1)))))
1734 (simplify
1735  (minus (nop_convert1? (plus@2 (nop_convert2? (bit_and:c @0 @1))
1736                                integer_all_onesp))
1737        (nop_convert3? (bit_ior @0 @1)))
1738  (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1739       && !TYPE_SATURATING (type) && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2))
1740       && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@2))
1741       && !TYPE_SATURATING (TREE_TYPE (@2)))
1742  (bit_not (convert (bit_xor @0 @1)))))
1743 (simplify
1744  (minus (nop_convert1? (bit_and @0 @1))
1745        (nop_convert2? (plus@2 (nop_convert3? (bit_ior:c @0 @1))
1746                                integer_onep)))
1747  (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
1748       && !TYPE_SATURATING (type) && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2))
1749       && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@2))
1750       && !TYPE_SATURATING (TREE_TYPE (@2)))
1751  (bit_not (convert (bit_xor @0 @1)))))
1753 /* ~x & ~y -> ~(x | y)
1754    ~x | ~y -> ~(x & y) */
1755 (for op (bit_and bit_ior)
1756      rop (bit_ior bit_and)
1757  (simplify
1758   (op (convert1? (bit_not @0)) (convert2? (bit_not @1)))
1759   (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1760        && element_precision (type) <= element_precision (TREE_TYPE (@1)))
1761    (bit_not (rop (convert @0) (convert @1))))))
1763 /* If we are XORing or adding two BIT_AND_EXPR's, both of which are and'ing
1764    with a constant, and the two constants have no bits in common,
1765    we should treat this as a BIT_IOR_EXPR since this may produce more
1766    simplifications.  */
1767 (for op (bit_xor plus)
1768  (simplify
1769   (op (convert1? (bit_and@4 @0 INTEGER_CST@1))
1770       (convert2? (bit_and@5 @2 INTEGER_CST@3)))
1771   (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
1772        && tree_nop_conversion_p (type, TREE_TYPE (@2))
1773        && (wi::to_wide (@1) & wi::to_wide (@3)) == 0)
1774    (bit_ior (convert @4) (convert @5)))))
1776 /* (X | Y) ^ X -> Y & ~ X*/
1777 (simplify
1778  (bit_xor:c (convert1? (bit_ior:c @@0 @1)) (convert2? @0))
1779  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1780   (convert (bit_and @1 (bit_not @0)))))
1782 /* (~X | Y) ^ X -> ~(X & Y).  */
1783 (simplify
1784  (bit_xor:c (nop_convert1? (bit_ior:c (nop_convert2? (bit_not @0)) @1)) @2)
1785  (if (bitwise_equal_p (@0, @2))
1786   (convert (bit_not (bit_and @0 (convert @1))))))
1788 /* Convert ~X ^ ~Y to X ^ Y.  */
1789 (simplify
1790  (bit_xor (convert1? (bit_not @0)) (convert2? (bit_not @1)))
1791  (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
1792       && element_precision (type) <= element_precision (TREE_TYPE (@1)))
1793   (bit_xor (convert @0) (convert @1))))
1795 /* Convert ~X ^ C to X ^ ~C.  */
1796 (simplify
1797  (bit_xor (convert? (bit_not @0)) INTEGER_CST@1)
1798  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
1799   (bit_xor (convert @0) (bit_not @1))))
1801 /* Fold (X & Y) ^ Y and (X ^ Y) & Y as ~X & Y.  */
1802 (for opo (bit_and bit_xor)
1803      opi (bit_xor bit_and)
1804  (simplify
1805   (opo:c (opi:cs @0 @1) @1)
1806   (bit_and (bit_not @0) @1)))
1808 /* Given a bit-wise operation CODE applied to ARG0 and ARG1, see if both
1809    operands are another bit-wise operation with a common input.  If so,
1810    distribute the bit operations to save an operation and possibly two if
1811    constants are involved.  For example, convert
1812      (A | B) & (A | C) into A | (B & C)
1813    Further simplification will occur if B and C are constants.  */
1814 (for op (bit_and bit_ior bit_xor)
1815      rop (bit_ior bit_and bit_and)
1816  (simplify
1817   (op (convert? (rop:c @@0 @1)) (convert? (rop:c @0 @2)))
1818   (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1819        && tree_nop_conversion_p (type, TREE_TYPE (@2)))
1820    (rop (convert @0) (op (convert @1) (convert @2))))))
1822 /* Some simple reassociation for bit operations, also handled in reassoc.  */
1823 /* (X & Y) & Y -> X & Y
1824    (X | Y) | Y -> X | Y  */
1825 (for op (bit_and bit_ior)
1826  (simplify
1827   (op:c (convert1?@2 (op:c @0 @@1)) (convert2? @1))
1828   @2))
1829 /* (X ^ Y) ^ Y -> X  */
1830 (simplify
1831  (bit_xor:c (convert1? (bit_xor:c @0 @@1)) (convert2? @1))
1832  (convert @0))
1833 /* (X & Y) & (X & Z) -> (X & Y) & Z
1834    (X | Y) | (X | Z) -> (X | Y) | Z  */
1835 (for op (bit_and bit_ior)
1836  (simplify
1837   (op (convert1?@3 (op:c@4 @0 @1)) (convert2?@5 (op:c@6 @0 @2)))
1838   (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1839        && tree_nop_conversion_p (type, TREE_TYPE (@2)))
1840    (if (single_use (@5) && single_use (@6))
1841     (op @3 (convert @2))
1842     (if (single_use (@3) && single_use (@4))
1843      (op (convert @1) @5))))))
1844 /* (X ^ Y) ^ (X ^ Z) -> Y ^ Z  */
1845 (simplify
1846  (bit_xor (convert1? (bit_xor:c @0 @1)) (convert2? (bit_xor:c @0 @2)))
1847  (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
1848       && tree_nop_conversion_p (type, TREE_TYPE (@2)))
1849   (bit_xor (convert @1) (convert @2))))
1851 /* Convert abs (abs (X)) into abs (X).
1852    also absu (absu (X)) into absu (X).  */
1853 (simplify
1854  (abs (abs@1 @0))
1855  @1)
1857 (simplify
1858  (absu (convert@2 (absu@1 @0)))
1859  (if (tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@1)))
1860   @1))
1862 /* Convert abs[u] (-X) -> abs[u] (X).  */
1863 (simplify
1864  (abs (negate @0))
1865  (abs @0))
1867 (simplify
1868  (absu (negate @0))
1869  (absu @0))
1871 /* Convert abs[u] (X)  where X is nonnegative -> (X).  */
1872 (simplify
1873  (abs tree_expr_nonnegative_p@0)
1874  @0)
1876 (simplify
1877  (absu tree_expr_nonnegative_p@0)
1878  (convert @0))
1880 /* Simplify (-(X < 0) | 1) * X into abs (X) or absu(X).  */
1881 (simplify
1882  (mult:c (nop_convert1?
1883           (bit_ior (nop_convert2? (negate (convert? (lt @0 integer_zerop))))
1884                     integer_onep))
1885          (nop_convert3? @0))
1886  (if (INTEGRAL_TYPE_P (type)
1887       && INTEGRAL_TYPE_P (TREE_TYPE (@0))
1888       && !TYPE_UNSIGNED (TREE_TYPE (@0)))
1889   (if (TYPE_UNSIGNED (type))
1890    (absu @0)
1891    (abs @0)
1892   )
1896 /* A few cases of fold-const.cc negate_expr_p predicate.  */
1897 (match negate_expr_p
1898  INTEGER_CST
1899  (if ((INTEGRAL_TYPE_P (type)
1900        && TYPE_UNSIGNED (type))
1901       || (!TYPE_OVERFLOW_SANITIZED (type)
1902           && may_negate_without_overflow_p (t)))))
1903 (match negate_expr_p
1904  FIXED_CST)
1905 (match negate_expr_p
1906  (negate @0)
1907  (if (!TYPE_OVERFLOW_SANITIZED (type))))
1908 (match negate_expr_p
1909  REAL_CST
1910  (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (t)))))
1911 /* VECTOR_CST handling of non-wrapping types would recurse in unsupported
1912    ways.  */
1913 (match negate_expr_p
1914  VECTOR_CST
1915  (if (FLOAT_TYPE_P (TREE_TYPE (type)) || TYPE_OVERFLOW_WRAPS (type))))
1916 (match negate_expr_p
1917  (minus @0 @1)
1918  (if ((ANY_INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_WRAPS (type))
1919       || (FLOAT_TYPE_P (type)
1920           && !HONOR_SIGN_DEPENDENT_ROUNDING (type)
1921           && !HONOR_SIGNED_ZEROS (type)))))
1923 /* (-A) * (-B) -> A * B  */
1924 (simplify
1925  (mult:c (convert1? (negate @0)) (convert2? negate_expr_p@1))
1926   (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
1927        && tree_nop_conversion_p (type, TREE_TYPE (@1)))
1928    (mult (convert @0) (convert (negate @1)))))
1930 /* -(A + B) -> (-B) - A.  */
1931 (simplify
1932  (negate (plus:c @0 negate_expr_p@1))
1933  (if (!HONOR_SIGN_DEPENDENT_ROUNDING (type)
1934       && !HONOR_SIGNED_ZEROS (type))
1935   (minus (negate @1) @0)))
1937 /* -(A - B) -> B - A.  */
1938 (simplify
1939  (negate (minus @0 @1))
1940  (if ((ANY_INTEGRAL_TYPE_P (type) && !TYPE_OVERFLOW_SANITIZED (type))
1941       || (FLOAT_TYPE_P (type)
1942           && !HONOR_SIGN_DEPENDENT_ROUNDING (type)
1943           && !HONOR_SIGNED_ZEROS (type)))
1944   (minus @1 @0)))
1945 (simplify
1946  (negate (pointer_diff @0 @1))
1947  (if (TYPE_OVERFLOW_UNDEFINED (type))
1948   (pointer_diff @1 @0)))
1950 /* A - B -> A + (-B) if B is easily negatable.  */
1951 (simplify
1952  (minus @0 negate_expr_p@1)
1953  (if (!FIXED_POINT_TYPE_P (type))
1954  (plus @0 (negate @1))))
1956 /* 1 - a is a ^ 1 if a had a bool range. */
1957 /* This is only enabled for gimple as sometimes
1958    cfun is not set for the function which contains
1959    the SSA_NAME (e.g. while IPA passes are happening,
1960    fold might be called).  */
1961 (simplify
1962  (minus integer_onep@0 SSA_NAME@1)
1963   (if (INTEGRAL_TYPE_P (type)
1964        && ssa_name_has_boolean_range (@1))
1965    (bit_xor @1 @0)))
1967 /* Other simplifications of negation (c.f. fold_negate_expr_1).  */
1968 (simplify
1969  (negate (mult:c@0 @1 negate_expr_p@2))
1970  (if (! TYPE_UNSIGNED (type)
1971       && ! HONOR_SIGN_DEPENDENT_ROUNDING (type)
1972       && single_use (@0))
1973   (mult @1 (negate @2))))
1975 (simplify
1976  (negate (rdiv@0 @1 negate_expr_p@2))
1977  (if (! HONOR_SIGN_DEPENDENT_ROUNDING (type)
1978       && single_use (@0))
1979   (rdiv @1 (negate @2))))
1981 (simplify
1982  (negate (rdiv@0 negate_expr_p@1 @2))
1983  (if (! HONOR_SIGN_DEPENDENT_ROUNDING (type)
1984       && single_use (@0))
1985   (rdiv (negate @1) @2)))
1987 /* Fold -((int)x >> (prec - 1)) into (unsigned)x >> (prec - 1).  */
1988 (simplify
1989  (negate (convert? (rshift @0 INTEGER_CST@1)))
1990  (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
1991       && wi::to_wide (@1) == element_precision (type) - 1)
1992   (with { tree stype = TREE_TYPE (@0);
1993           tree ntype = TYPE_UNSIGNED (stype) ? signed_type_for (stype)
1994                                              : unsigned_type_for (stype); }
1995    (if (VECTOR_TYPE_P (type))
1996     (view_convert (rshift (view_convert:ntype @0) @1))
1997     (convert (rshift (convert:ntype @0) @1))))))
1999 /* Try to fold (type) X op CST -> (type) (X op ((type-x) CST))
2000    when profitable.
2001    For bitwise binary operations apply operand conversions to the
2002    binary operation result instead of to the operands.  This allows
2003    to combine successive conversions and bitwise binary operations.
2004    We combine the above two cases by using a conditional convert.  */
2005 (for bitop (bit_and bit_ior bit_xor)
2006  (simplify
2007   (bitop (convert@2 @0) (convert?@3 @1))
2008   (if (((TREE_CODE (@1) == INTEGER_CST
2009          && INTEGRAL_TYPE_P (TREE_TYPE (@0))
2010          && (int_fits_type_p (@1, TREE_TYPE (@0))
2011              || tree_nop_conversion_p (TREE_TYPE (@0), type)))
2012         || types_match (@0, @1))
2013        && !POINTER_TYPE_P (TREE_TYPE (@0))
2014        && !VECTOR_TYPE_P (TREE_TYPE (@0))
2015        && TREE_CODE (TREE_TYPE (@0)) != OFFSET_TYPE
2016        /* ???  This transform conflicts with fold-const.cc doing
2017           Convert (T)(x & c) into (T)x & (T)c, if c is an integer
2018           constants (if x has signed type, the sign bit cannot be set
2019           in c).  This folds extension into the BIT_AND_EXPR.
2020           Restrict it to GIMPLE to avoid endless recursions.  */
2021        && (bitop != BIT_AND_EXPR || GIMPLE)
2022        && (/* That's a good idea if the conversion widens the operand, thus
2023               after hoisting the conversion the operation will be narrower.
2024               It is also a good if the conversion is a nop as moves the
2025               conversion to one side; allowing for combining of the conversions.  */
2026            TYPE_PRECISION (TREE_TYPE (@0)) < TYPE_PRECISION (type)
2027            /* The conversion check for being a nop can only be done at the gimple
2028               level as fold_binary has some re-association code which can conflict
2029               with this if there is a "constant" which is not a full INTEGER_CST.  */
2030            || (GIMPLE && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (type))
2031            /* It's also a good idea if the conversion is to a non-integer
2032               mode.  */
2033            || GET_MODE_CLASS (TYPE_MODE (type)) != MODE_INT
2034            /* Or if the precision of TO is not the same as the precision
2035               of its mode.  */
2036            || !type_has_mode_precision_p (type)
2037            /* In GIMPLE, getting rid of 2 conversions for one new results
2038               in smaller IL.  */
2039            || (GIMPLE
2040                && TREE_CODE (@1) != INTEGER_CST
2041                && tree_nop_conversion_p (type, TREE_TYPE (@0))
2042                && single_use (@2)
2043                && single_use (@3))))
2044    (convert (bitop @0 (convert @1)))))
2045  /* In GIMPLE, getting rid of 2 conversions for one new results
2046     in smaller IL.  */
2047  (simplify
2048   (convert (bitop:cs@2 (nop_convert:s @0) @1))
2049   (if (GIMPLE
2050        && TREE_CODE (@1) != INTEGER_CST
2051        && tree_nop_conversion_p (type, TREE_TYPE (@2))
2052        && types_match (type, @0)
2053        && !POINTER_TYPE_P (TREE_TYPE (@0))
2054        && TREE_CODE (TREE_TYPE (@0)) != OFFSET_TYPE)
2055    (bitop @0 (convert @1)))))
2057 (for bitop (bit_and bit_ior)
2058      rbitop (bit_ior bit_and)
2059   /* (x | y) & x -> x */
2060   /* (x & y) | x -> x */
2061  (simplify
2062   (bitop:c (rbitop:c @0 @1) @0)
2063   @0)
2064  /* (~x | y) & x -> x & y */
2065  /* (~x & y) | x -> x | y */
2066  (simplify
2067   (bitop:c (rbitop:c @2 @1) @0)
2068   (with { bool wascmp; }
2069    (if (bitwise_inverted_equal_p (@0, @2, wascmp)
2070         && (!wascmp || element_precision (type) == 1))
2071     (bitop @0 @1))))
2072   /* (x | y) & (x & z) -> (x & z) */
2073   /* (x & y) | (x | z) -> (x | z) */
2074  (simplify
2075   (bitop:c (rbitop:c @0 @1) (bitop:c@3 @0 @2))
2076   @3)
2077  /* (x | c) & ~(y | c) -> x & ~(y | c) */
2078  /* (x & c) | ~(y & c) -> x | ~(y & c) */
2079  (simplify
2080   (bitop:c (rbitop:c @0 @1) (bit_not@3 (rbitop:c @1 @2)))
2081   (bitop @0 @3))
2082  /* x & ~(y | x) -> 0 */
2083  /* x | ~(y & x) -> -1 */
2084  (simplify
2085   (bitop:c @0 (bit_not (rbitop:c @0 @1)))
2086   (if (bitop == BIT_AND_EXPR)
2087    { build_zero_cst (type); }
2088    { build_minus_one_cst (type); })))
2090 /* ((x | y) & z) | x -> (z & y) | x
2091    ((x ^ y) & z) | x -> (z & y) | x  */
2092 (for op (bit_ior bit_xor)
2093  (simplify
2094   (bit_ior:c (nop_convert1?:s
2095                (bit_and:cs (nop_convert2?:s (op:cs @0 @1)) @2)) @3)
2096   (if (bitwise_equal_p (@0, @3))
2097    (convert (bit_ior (bit_and @1 (convert @2)) (convert @0))))))
2099 /* (x | CST1) & CST2 -> (x & CST2) | (CST1 & CST2) */
2100 (simplify
2101   (bit_and (bit_ior @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
2102   (bit_ior (bit_and @0 @2) (bit_and! @1 @2)))
2104 /* Combine successive equal operations with constants.  */
2105 (for bitop (bit_and bit_ior bit_xor)
2106  (simplify
2107   (bitop (bitop @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2)
2108   (if (!CONSTANT_CLASS_P (@0))
2109    /* This is the canonical form regardless of whether (bitop @1 @2) can be
2110       folded to a constant.  */
2111    (bitop @0 (bitop! @1 @2))
2112    /* In this case we have three constants and (bitop @0 @1) doesn't fold
2113       to a constant.  This can happen if @0 or @1 is a POLY_INT_CST and if
2114       the values involved are such that the operation can't be decided at
2115       compile time.  Try folding one of @0 or @1 with @2 to see whether
2116       that combination can be decided at compile time.
2118       Keep the existing form if both folds fail, to avoid endless
2119       oscillation.  */
2120    (with { tree cst1 = const_binop (bitop, type, @0, @2); }
2121     (if (cst1)
2122      (bitop @1 { cst1; })
2123      (with { tree cst2 = const_binop (bitop, type, @1, @2); }
2124       (if (cst2)
2125        (bitop @0 { cst2; }))))))))
2127 /* Try simple folding for X op !X, and X op X with the help
2128    of the truth_valued_p and logical_inverted_value predicates.  */
2129 (match truth_valued_p
2130  @0
2131  (if (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1)))
2132 (for op (tcc_comparison truth_and truth_andif truth_or truth_orif truth_xor)
2133  (match truth_valued_p
2134   (op @0 @1)))
2135 (match truth_valued_p
2136   (truth_not @0))
2138 (match (logical_inverted_value @0)
2139  (truth_not @0))
2140 (match (logical_inverted_value @0)
2141  (bit_not truth_valued_p@0))
2142 (match (logical_inverted_value @0)
2143  (eq @0 integer_zerop))
2144 (match (logical_inverted_value @0)
2145  (ne truth_valued_p@0 integer_truep))
2146 (match (logical_inverted_value @0)
2147  (bit_xor truth_valued_p@0 integer_truep))
2149 /* X & !X -> 0.  */
2150 (simplify
2151  (bit_and:c @0 (logical_inverted_value @0))
2152  { build_zero_cst (type); })
2153 /* X | !X and X ^ !X -> 1, , if X is truth-valued.  */
2154 (for op (bit_ior bit_xor)
2155  (simplify
2156   (op:c truth_valued_p@0 (logical_inverted_value @0))
2157   { constant_boolean_node (true, type); }))
2158 /* X ==/!= !X is false/true.  */
2159 (for op (eq ne)
2160  (simplify
2161   (op:c truth_valued_p@0 (logical_inverted_value @0))
2162   { constant_boolean_node (op == NE_EXPR ? true : false, type); }))
2164 /* ~~x -> x */
2165 (simplify
2166   (bit_not (bit_not @0))
2167   @0)
2169 /* zero_one_valued_p will match when a value is known to be either
2170    0 or 1 including constants 0 or 1.
2171    Signed 1-bits includes -1 so they cannot match here. */
2172 (match zero_one_valued_p
2173  @0
2174  (if (INTEGRAL_TYPE_P (type)
2175       && (TYPE_UNSIGNED (type)
2176           || TYPE_PRECISION (type) > 1)
2177       && wi::leu_p (tree_nonzero_bits (@0), 1))))
2178 (match zero_one_valued_p
2179  truth_valued_p@0
2180  (if (INTEGRAL_TYPE_P (type)
2181       && (TYPE_UNSIGNED (type)
2182           || TYPE_PRECISION (type) > 1))))
2184 /* (a&1) is always [0,1] too. This is useful again when
2185    the range is not known. */
2186 /* Note this can't be recursive due to VN handling of equivalents,
2187    VN and would cause an infinite recursion. */
2188 (match zero_one_valued_p
2189  (bit_and:c@0 @1 integer_onep)
2190  (if (INTEGRAL_TYPE_P (type))))
2192 /* A conversion from an zero_one_valued_p is still a [0,1].
2193    This is useful when the range of a variable is not known */
2194 /* Note this matches can't be recursive because of the way VN handles
2195    nop conversions being equivalent and then recursive between them. */
2196 (match zero_one_valued_p
2197  (convert@0 @1)
2198  (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2199       && (TYPE_UNSIGNED (TREE_TYPE (@1))
2200           || TYPE_PRECISION (TREE_TYPE (@1)) > 1)
2201       && wi::leu_p (tree_nonzero_bits (@1), 1))))
2203 /* Transform { 0 or 1 } * { 0 or 1 } into { 0 or 1 } & { 0 or 1 }.  */
2204 (simplify
2205  (mult zero_one_valued_p@0 zero_one_valued_p@1)
2206  (if (INTEGRAL_TYPE_P (type))
2207   (bit_and @0 @1)))
2209 (for cmp (tcc_comparison)
2210      icmp (inverted_tcc_comparison)
2211  /* Fold (((a < b) & c) | ((a >= b) & d)) into (a < b ? c : d) & 1.  */
2212  (simplify
2213   (bit_ior
2214    (bit_and:c (convert? (cmp@0  @01 @02)) @3)
2215    (bit_and:c (convert? (icmp@4 @01 @02)) @5))
2216     (if (INTEGRAL_TYPE_P (type)
2217          && invert_tree_comparison (cmp, HONOR_NANS (@01)) == icmp
2218          /* The scalar version has to be canonicalized after vectorization
2219             because it makes unconditional loads conditional ones, which
2220             means we lose vectorization because the loads may trap.  */
2221          && canonicalize_math_after_vectorization_p ())
2222      (bit_and (cond @0 @3 @5) { build_one_cst (type); })))
2224  /* Fold ((-(a < b) & c) | (-(a >= b) & d)) into a < b ? c : d.  This is
2225     canonicalized further and we recognize the conditional form:
2226     (a < b ? c : 0) | (a >= b ? d : 0) into a < b ? c : d.  */
2227  (simplify
2228   (bit_ior
2229    (cond (cmp@0  @01 @02) @3 zerop)
2230    (cond (icmp@4 @01 @02) @5 zerop))
2231     (if (INTEGRAL_TYPE_P (type)
2232          && invert_tree_comparison (cmp, HONOR_NANS (@01)) == icmp
2233          /* The scalar version has to be canonicalized after vectorization
2234             because it makes unconditional loads conditional ones, which
2235             means we lose vectorization because the loads may trap.  */
2236          && canonicalize_math_after_vectorization_p ())
2237     (cond @0 @3 @5)))
2239  /* Vector Fold (((a < b) & c) | ((a >= b) & d)) into a < b ? c : d. 
2240     and ((~(a < b) & c) | (~(a >= b) & d)) into a < b ? c : d.  */
2241  (simplify
2242   (bit_ior
2243    (bit_and:c (vec_cond:s (cmp@0 @6 @7) @4 @5) @2)
2244    (bit_and:c (vec_cond:s (icmp@1 @6 @7) @4 @5) @3))
2245     (if (integer_zerop (@5)
2246          && invert_tree_comparison (cmp, HONOR_NANS (@6)) == icmp)
2247      (switch
2248       (if (integer_onep (@4))
2249        (bit_and (vec_cond @0 @2 @3) @4))
2250         (if (integer_minus_onep (@4))
2251          (vec_cond @0 @2 @3)))
2252     (if (integer_zerop (@4)
2253          && invert_tree_comparison (cmp, HONOR_NANS (@6)) == icmp)
2254      (switch
2255       (if (integer_onep (@5))
2256        (bit_and (vec_cond @0 @3 @2) @5))
2257       (if (integer_minus_onep (@5))
2258        (vec_cond @0 @3 @2))))))
2260  /* Scalar Vectorized Fold ((-(a < b) & c) | (-(a >= b) & d))
2261     into a < b ? d : c.  */
2262  (simplify
2263   (bit_ior
2264    (vec_cond:s (cmp@0 @4 @5) @2 integer_zerop)
2265    (vec_cond:s (icmp@1 @4 @5) @3 integer_zerop))
2266   (if (invert_tree_comparison (cmp, HONOR_NANS (@4)) == icmp)
2267    (vec_cond @0 @2 @3))))
2269 /* Transform X & -Y into X * Y when Y is { 0 or 1 }.  */
2270 (simplify
2271  (bit_and:c (convert? (negate zero_one_valued_p@0)) @1)
2272  (if (INTEGRAL_TYPE_P (type)
2273       && INTEGRAL_TYPE_P (TREE_TYPE (@0))
2274       && TREE_CODE (TREE_TYPE (@0)) != BOOLEAN_TYPE
2275       /* Sign extending of the neg or a truncation of the neg
2276          is needed. */
2277       && (!TYPE_UNSIGNED (TREE_TYPE (@0))
2278           || TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))))
2279   (mult (convert @0) @1)))
2281 /* Narrow integer multiplication by a zero_one_valued_p operand.
2282    Multiplication by [0,1] is guaranteed not to overflow.  */
2283 (simplify
2284  (convert (mult@0 zero_one_valued_p@1 INTEGER_CST@2))
2285  (if (INTEGRAL_TYPE_P (type)
2286       && INTEGRAL_TYPE_P (TREE_TYPE (@0))
2287       && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (@0)))
2288   (mult (convert @1) (convert @2))))
2290 /* (X << C) != 0 can be simplified to X, when C is zero_one_valued_p.
2291    Check that the shift is well-defined (C is less than TYPE_PRECISION)
2292    as some targets (such as x86's SSE) may return zero for larger C.  */
2293 (simplify
2294   (ne (lshift zero_one_valued_p@0 INTEGER_CST@1) integer_zerop@2)
2295   (if (tree_fits_shwi_p (@1)
2296        && tree_to_shwi (@1) > 0
2297        && tree_to_shwi (@1) < TYPE_PRECISION (TREE_TYPE (@0)))
2298     (convert @0)))
2300 /* (X << C) == 0 can be simplified to X == 0, when C is zero_one_valued_p.
2301    Check that the shift is well-defined (C is less than TYPE_PRECISION)
2302    as some targets (such as x86's SSE) may return zero for larger C.  */
2303 (simplify
2304   (eq (lshift zero_one_valued_p@0 INTEGER_CST@1) integer_zerop@2)
2305   (if (tree_fits_shwi_p (@1)
2306        && tree_to_shwi (@1) > 0
2307        && tree_to_shwi (@1) < TYPE_PRECISION (TREE_TYPE (@0)))
2308     (eq @0 @2)))
2310 /* Convert ~ (-A) to A - 1.  */
2311 (simplify
2312  (bit_not (convert? (negate @0)))
2313  (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
2314       || !TYPE_UNSIGNED (TREE_TYPE (@0)))
2315   (convert (minus @0 { build_each_one_cst (TREE_TYPE (@0)); }))))
2317 /* Convert - (~A) to A + 1.  */
2318 (simplify
2319  (negate (nop_convert? (bit_not @0)))
2320  (plus (view_convert @0) { build_each_one_cst (type); }))
2322 /* (a & b) ^ (a == b) -> !(a | b) */
2323 /* (a & b) == (a ^ b) -> !(a | b) */
2324 (for first_op (bit_xor eq)
2325      second_op (eq bit_xor)
2326  (simplify
2327   (first_op:c (bit_and:c truth_valued_p@0 truth_valued_p@1) (second_op:c @0 @1))
2328     (bit_not (bit_ior @0 @1))))
2330 /* Convert ~ (A - 1) or ~ (A + -1) to -A.  */
2331 (simplify
2332  (bit_not (convert? (minus @0 integer_each_onep)))
2333  (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
2334       || !TYPE_UNSIGNED (TREE_TYPE (@0)))
2335   (convert (negate @0))))
2336 (simplify
2337  (bit_not (convert? (plus @0 integer_all_onesp)))
2338  (if (element_precision (type) <= element_precision (TREE_TYPE (@0))
2339       || !TYPE_UNSIGNED (TREE_TYPE (@0)))
2340   (convert (negate @0))))
2342 /* Part of convert ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify.  */
2343 (simplify
2344  (bit_not (convert? (bit_xor @0 INTEGER_CST@1)))
2345  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2346   (convert (bit_xor @0 (bit_not @1)))))
2347 (simplify
2348  (bit_not (convert? (bit_xor:c (bit_not @0) @1)))
2349  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2350   (convert (bit_xor @0 @1))))
2352 /* Otherwise prefer ~(X ^ Y) to ~X ^ Y as more canonical.  */
2353 (simplify
2354  (bit_xor:c (nop_convert?:s (bit_not:s @0)) @1)
2355  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2356   (bit_not (bit_xor (view_convert @0) @1))))
2358 /* ~(a ^ b) is a == b for truth valued a and b.  */
2359 (simplify
2360  (bit_not (bit_xor:s truth_valued_p@0 truth_valued_p@1))
2361  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2362       && TYPE_PRECISION (TREE_TYPE (@0)) == 1)
2363   (convert (eq @0 @1))))
2365 /* (~a) == b is a ^ b for truth valued a and b.  */
2366 (simplify
2367  (eq:c (bit_not:s truth_valued_p@0) truth_valued_p@1)
2368  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2369       && TYPE_PRECISION (TREE_TYPE (@0)) == 1)
2370   (convert (bit_xor @0 @1))))
2372 /* (x & ~m) | (y & m) -> ((x ^ y) & m) ^ x */
2373 (simplify
2374  (bit_ior:c (bit_and:cs @0 (bit_not @2)) (bit_and:cs @1 @2))
2375  (bit_xor (bit_and (bit_xor @0 @1) @2) @0))
2377 /* Fold A - (A & B) into ~B & A.  */
2378 (simplify
2379  (minus (convert1? @0) (convert2?:s (bit_and:cs @@0 @1)))
2380  (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
2381       && tree_nop_conversion_p (type, TREE_TYPE (@1)))
2382   (convert (bit_and (bit_not @1) @0))))
2384 /* (m1 CMP m2) * d -> (m1 CMP m2) ? d : 0  */
2385 (if (!canonicalize_math_p ())
2386  (for cmp (tcc_comparison)
2387   (simplify
2388    (mult:c (convert (cmp@0 @1 @2)) @3)
2389    (if (INTEGRAL_TYPE_P (type)
2390         && INTEGRAL_TYPE_P (TREE_TYPE (@0)))
2391      (cond @0 @3 { build_zero_cst (type); })))
2392 /* (-(m1 CMP m2)) & d -> (m1 CMP m2) ? d : 0  */
2393   (simplify
2394    (bit_and:c (negate (convert (cmp@0 @1 @2))) @3)
2395    (if (INTEGRAL_TYPE_P (type)
2396         && INTEGRAL_TYPE_P (TREE_TYPE (@0)))
2397      (cond @0 @3 { build_zero_cst (type); })))
2401 /* For integral types with undefined overflow and C != 0 fold
2402    x * C EQ/NE y * C into x EQ/NE y.  */
2403 (for cmp (eq ne)
2404  (simplify
2405   (cmp (mult:c @0 @1) (mult:c @2 @1))
2406   (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2407        && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2408        && tree_expr_nonzero_p (@1))
2409    (cmp @0 @2))))
2411 /* For integral types with wrapping overflow and C odd fold
2412    x * C EQ/NE y * C into x EQ/NE y.  */
2413 (for cmp (eq ne)
2414  (simplify
2415   (cmp (mult @0 INTEGER_CST@1) (mult @2 @1))
2416   (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2417        && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))
2418        && (TREE_INT_CST_LOW (@1) & 1) != 0)
2419    (cmp @0 @2))))
2421 /* For integral types with undefined overflow and C != 0 fold
2422    x * C RELOP y * C into:
2424    x RELOP y for nonnegative C
2425    y RELOP x for negative C  */
2426 (for cmp (lt gt le ge)
2427  (simplify
2428   (cmp (mult:c @0 @1) (mult:c @2 @1))
2429   (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2430        && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
2431    (if (tree_expr_nonnegative_p (@1) && tree_expr_nonzero_p (@1))
2432     (cmp @0 @2)
2433    (if (TREE_CODE (@1) == INTEGER_CST
2434         && wi::neg_p (wi::to_wide (@1), TYPE_SIGN (TREE_TYPE (@1))))
2435     (cmp @2 @0))))))
2437 /* (X - 1U) <= INT_MAX-1U into (int) X > 0.  */
2438 (for cmp (le gt)
2439      icmp (gt le)
2440  (simplify
2441   (cmp (plus @0 integer_minus_onep@1) INTEGER_CST@2)
2442    (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2443         && TYPE_UNSIGNED (TREE_TYPE (@0))
2444         && TYPE_PRECISION (TREE_TYPE (@0)) > 1
2445         && (wi::to_wide (@2)
2446             == wi::max_value (TYPE_PRECISION (TREE_TYPE (@0)), SIGNED) - 1))
2447     (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
2448      (icmp (convert:stype @0) { build_int_cst (stype, 0); })))))
2450 /* X / 4 < Y / 4 iff X < Y when the division is known to be exact.  */
2451 (for cmp (simple_comparison)
2452  (simplify
2453   (cmp (convert?@3 (exact_div @0 INTEGER_CST@2)) (convert? (exact_div @1 @2)))
2454   (if (element_precision (@3) >= element_precision (@0)
2455        && types_match (@0, @1))
2456    (if (wi::lt_p (wi::to_wide (@2), 0, TYPE_SIGN (TREE_TYPE (@2))))
2457     (if (!TYPE_UNSIGNED (TREE_TYPE (@3)))
2458      (cmp @1 @0)
2459      (if (tree_expr_nonzero_p (@0) && tree_expr_nonzero_p (@1))
2460       (with
2461        {
2462         tree utype = unsigned_type_for (TREE_TYPE (@0));
2463        }
2464        (cmp (convert:utype @1) (convert:utype @0)))))
2465     (if (wi::gt_p (wi::to_wide (@2), 1, TYPE_SIGN (TREE_TYPE (@2))))
2466      (if (TYPE_UNSIGNED (TREE_TYPE (@0)) || !TYPE_UNSIGNED (TREE_TYPE (@3)))
2467       (cmp @0 @1)
2468       (with
2469        {
2470         tree utype = unsigned_type_for (TREE_TYPE (@0));
2471        }
2472        (cmp (convert:utype @0) (convert:utype @1)))))))))
2474 /* X / C1 op C2 into a simple range test.  */
2475 (for cmp (simple_comparison)
2476  (simplify
2477   (cmp (trunc_div:s @0 INTEGER_CST@1) INTEGER_CST@2)
2478   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2479        && integer_nonzerop (@1)
2480        && !TREE_OVERFLOW (@1)
2481        && !TREE_OVERFLOW (@2))
2482    (with { tree lo, hi; bool neg_overflow;
2483            enum tree_code code = fold_div_compare (cmp, @1, @2, &lo, &hi,
2484                                                    &neg_overflow); }
2485     (switch
2486      (if (code == LT_EXPR || code == GE_EXPR)
2487        (if (TREE_OVERFLOW (lo))
2488         { build_int_cst (type, (code == LT_EXPR) ^ neg_overflow); }
2489         (if (code == LT_EXPR)
2490          (lt @0 { lo; })
2491          (ge @0 { lo; }))))
2492      (if (code == LE_EXPR || code == GT_EXPR)
2493        (if (TREE_OVERFLOW (hi))
2494         { build_int_cst (type, (code == LE_EXPR) ^ neg_overflow); }
2495         (if (code == LE_EXPR)
2496          (le @0 { hi; })
2497          (gt @0 { hi; }))))
2498      (if (!lo && !hi)
2499       { build_int_cst (type, code == NE_EXPR); })
2500      (if (code == EQ_EXPR && !hi)
2501       (ge @0 { lo; }))
2502      (if (code == EQ_EXPR && !lo)
2503       (le @0 { hi; }))
2504      (if (code == NE_EXPR && !hi)
2505       (lt @0 { lo; }))
2506      (if (code == NE_EXPR && !lo)
2507       (gt @0 { hi; }))
2508      (if (GENERIC)
2509       { build_range_check (UNKNOWN_LOCATION, type, @0, code == EQ_EXPR,
2510                            lo, hi); })
2511      (with
2512       {
2513         tree etype = range_check_type (TREE_TYPE (@0));
2514         if (etype)
2515           {
2516             hi = fold_convert (etype, hi);
2517             lo = fold_convert (etype, lo);
2518             hi = const_binop (MINUS_EXPR, etype, hi, lo);
2519           }
2520       }
2521       (if (etype && hi && !TREE_OVERFLOW (hi))
2522        (if (code == EQ_EXPR)
2523         (le (minus (convert:etype @0) { lo; }) { hi; })
2524         (gt (minus (convert:etype @0) { lo; }) { hi; })))))))))
2526 /* X + Z < Y + Z is the same as X < Y when there is no overflow.  */
2527 (for op (lt le ge gt)
2528  (simplify
2529   (op (plus:c @0 @2) (plus:c @1 @2))
2530   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2531        && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
2532    (op @0 @1))))
2534 /* As a special case, X + C < Y + C is the same as (signed) X < (signed) Y
2535    when C is an unsigned integer constant with only the MSB set, and X and
2536    Y have types of equal or lower integer conversion rank than C's.  */
2537 (for op (lt le ge gt)
2538  (simplify
2539   (op (plus @1 INTEGER_CST@0) (plus @2 @0))
2540   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
2541        && TYPE_UNSIGNED (TREE_TYPE (@0))
2542        && wi::only_sign_bit_p (wi::to_wide (@0)))
2543    (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
2544     (op (convert:stype @1) (convert:stype @2))))))
2546 /* For equality and subtraction, this is also true with wrapping overflow.  */
2547 (for op (eq ne minus)
2548  (simplify
2549   (op (plus:c @0 @2) (plus:c @1 @2))
2550   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2551        && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2552            || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
2553    (op @0 @1))))
2555 /* X - Z < Y - Z is the same as X < Y when there is no overflow.  */
2556 (for op (lt le ge gt)
2557  (simplify
2558   (op (minus @0 @2) (minus @1 @2))
2559   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2560        && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
2561    (op @0 @1))))
2562 /* For equality and subtraction, this is also true with wrapping overflow.  */
2563 (for op (eq ne minus)
2564  (simplify
2565   (op (minus @0 @2) (minus @1 @2))
2566   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2567        && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2568            || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
2569    (op @0 @1))))
2570 /* And for pointers...  */
2571 (for op (simple_comparison)
2572  (simplify
2573   (op (pointer_diff@3 @0 @2) (pointer_diff @1 @2))
2574   (if (!TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
2575    (op @0 @1))))
2576 (simplify
2577  (minus (pointer_diff@3 @0 @2) (pointer_diff @1 @2))
2578  (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@3))
2579       && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
2580   (pointer_diff @0 @1)))
2582 /* Z - X < Z - Y is the same as Y < X when there is no overflow.  */
2583 (for op (lt le ge gt)
2584  (simplify
2585   (op (minus @2 @0) (minus @2 @1))
2586   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2587        && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
2588    (op @1 @0))))
2589 /* For equality and subtraction, this is also true with wrapping overflow.  */
2590 (for op (eq ne minus)
2591  (simplify
2592   (op (minus @2 @0) (minus @2 @1))
2593   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2594        && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2595            || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
2596    (op @1 @0))))
2597 /* And for pointers...  */
2598 (for op (simple_comparison)
2599  (simplify
2600   (op (pointer_diff@3 @2 @0) (pointer_diff @2 @1))
2601   (if (!TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
2602    (op @1 @0))))
2603 (simplify
2604  (minus (pointer_diff@3 @2 @0) (pointer_diff @2 @1))
2605  (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@3))
2606       && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@2)))
2607   (pointer_diff @1 @0)))
2609 /* X + Y < Y is the same as X < 0 when there is no overflow.  */
2610 (for op (lt le gt ge)
2611  (simplify
2612   (op:c (plus:c@2 @0 @1) @1)
2613   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2614        && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2615        && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0))
2616        && (CONSTANT_CLASS_P (@0) || single_use (@2)))
2617    (op @0 { build_zero_cst (TREE_TYPE (@0)); }))))
2618 /* For equality, this is also true with wrapping overflow.  */
2619 (for op (eq ne)
2620  (simplify
2621   (op:c (nop_convert?@3 (plus:c@2 @0 (convert1? @1))) (convert2? @1))
2622   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2623        && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2624            || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
2625        && (CONSTANT_CLASS_P (@0) || (single_use (@2) && single_use (@3)))
2626        && tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@2))
2627        && tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@1)))
2628    (op @0 { build_zero_cst (TREE_TYPE (@0)); })))
2629  (simplify
2630   (op:c (nop_convert?@3 (pointer_plus@2 (convert1? @0) @1)) (convert2? @0))
2631   (if (tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@0))
2632        && tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0))
2633        && (CONSTANT_CLASS_P (@1) || (single_use (@2) && single_use (@3))))
2634    (op @1 { build_zero_cst (TREE_TYPE (@1)); }))))
2636 /* (&a + b) !=/== (&a[1] + c) -> (&a[0] - &a[1]) + b !=/== c */
2637 (for neeq (ne eq)
2638  (simplify
2639   (neeq:c ADDR_EXPR@0 (pointer_plus @2 @3))
2640    (with { poly_int64 diff; tree inner_type = TREE_TYPE (@3);}
2641     (if (ptr_difference_const (@0, @2, &diff))
2642      (neeq { build_int_cst_type (inner_type, diff); } @3))))
2643  (simplify
2644   (neeq (pointer_plus ADDR_EXPR@0 @1) (pointer_plus ADDR_EXPR@2 @3))
2645    (with { poly_int64 diff; tree inner_type = TREE_TYPE (@1);}
2646     (if (ptr_difference_const (@0, @2, &diff))
2647      (neeq (plus { build_int_cst_type (inner_type, diff); } @1) @3)))))
2649 /* X - Y < X is the same as Y > 0 when there is no overflow.
2650    For equality, this is also true with wrapping overflow.  */
2651 (for op (simple_comparison)
2652  (simplify
2653   (op:c @0 (minus@2 @0 @1))
2654   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
2655        && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
2656            || ((op == EQ_EXPR || op == NE_EXPR)
2657                && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
2658        && (CONSTANT_CLASS_P (@1) || single_use (@2)))
2659    (op @1 { build_zero_cst (TREE_TYPE (@1)); }))))
2661 /* Transform:
2662    (X / Y) == 0 -> X < Y if X, Y are unsigned.
2663    (X / Y) != 0 -> X >= Y, if X, Y are unsigned.  */
2664 (for cmp (eq ne)
2665      ocmp (lt ge)
2666  (simplify
2667   (cmp (trunc_div @0 @1) integer_zerop)
2668   (if (TYPE_UNSIGNED (TREE_TYPE (@0))
2669        /* Complex ==/!= is allowed, but not </>=.  */
2670        && TREE_CODE (TREE_TYPE (@0)) != COMPLEX_TYPE
2671        && (VECTOR_TYPE_P (type) || !VECTOR_TYPE_P (TREE_TYPE (@0))))
2672    (ocmp @0 @1))))
2674 /* X == C - X can never be true if C is odd.  */
2675 (for cmp (eq ne)
2676  (simplify
2677   (cmp:c (convert? @0) (convert1? (minus INTEGER_CST@1 (convert2? @0))))
2678   (if (TREE_INT_CST_LOW (@1) & 1)
2679    { constant_boolean_node (cmp == NE_EXPR, type); })))
2681 /* Arguments on which one can call get_nonzero_bits to get the bits
2682    possibly set.  */
2683 (match with_possible_nonzero_bits
2684  INTEGER_CST@0)
2685 (match with_possible_nonzero_bits
2686  SSA_NAME@0
2687  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))))
2688 /* Slightly extended version, do not make it recursive to keep it cheap.  */
2689 (match (with_possible_nonzero_bits2 @0)
2690  with_possible_nonzero_bits@0)
2691 (match (with_possible_nonzero_bits2 @0)
2692  (bit_and:c with_possible_nonzero_bits@0 @2))
2694 /* Same for bits that are known to be set, but we do not have
2695    an equivalent to get_nonzero_bits yet.  */
2696 (match (with_certain_nonzero_bits2 @0)
2697  INTEGER_CST@0)
2698 (match (with_certain_nonzero_bits2 @0)
2699  (bit_ior @1 INTEGER_CST@0))
2701 /* X == C (or X & Z == Y | C) is impossible if ~nonzero(X) & C != 0.  */
2702 (for cmp (eq ne)
2703  (simplify
2704   (cmp:c (with_possible_nonzero_bits2 @0) (with_certain_nonzero_bits2 @1))
2705   (if (wi::bit_and_not (wi::to_wide (@1), get_nonzero_bits (@0)) != 0)
2706    { constant_boolean_node (cmp == NE_EXPR, type); })))
2708 /* ((X inner_op C0) outer_op C1)
2709    With X being a tree where value_range has reasoned certain bits to always be
2710    zero throughout its computed value range,
2711    inner_op = {|,^}, outer_op = {|,^} and inner_op != outer_op
2712    where zero_mask has 1's for all bits that are sure to be 0 in
2713    and 0's otherwise.
2714    if (inner_op == '^') C0 &= ~C1;
2715    if ((C0 & ~zero_mask) == 0) then emit (X outer_op (C0 outer_op C1)
2716    if ((C1 & ~zero_mask) == 0) then emit (X inner_op (C0 outer_op C1)
2718 (for inner_op (bit_ior bit_xor)
2719      outer_op (bit_xor bit_ior)
2720 (simplify
2721  (outer_op
2722   (inner_op:s @2 INTEGER_CST@0) INTEGER_CST@1)
2723  (with
2724   {
2725     bool fail = false;
2726     wide_int zero_mask_not;
2727     wide_int C0;
2728     wide_int cst_emit;
2730     if (TREE_CODE (@2) == SSA_NAME)
2731       zero_mask_not = get_nonzero_bits (@2);
2732     else
2733       fail = true;
2735     if (inner_op == BIT_XOR_EXPR)
2736       {
2737         C0 = wi::bit_and_not (wi::to_wide (@0), wi::to_wide (@1));
2738         cst_emit = C0 | wi::to_wide (@1);
2739       }
2740     else
2741       {
2742         C0 = wi::to_wide (@0);
2743         cst_emit = C0 ^ wi::to_wide (@1);
2744       }
2745   }
2746   (if (!fail && (C0 & zero_mask_not) == 0)
2747    (outer_op @2 { wide_int_to_tree (type, cst_emit); })
2748    (if (!fail && (wi::to_wide (@1) & zero_mask_not) == 0)
2749     (inner_op @2 { wide_int_to_tree (type, cst_emit); }))))))
2751 /* Associate (p +p off1) +p off2 as (p +p (off1 + off2)).  */
2752 (simplify
2753   (pointer_plus (pointer_plus:s @0 @1) @3)
2754   (pointer_plus @0 (plus @1 @3)))
2755 #if GENERIC
2756 (simplify
2757   (pointer_plus (convert:s (pointer_plus:s @0 @1)) @3)
2758   (convert:type (pointer_plus @0 (plus @1 @3))))
2759 #endif
2761 /* Pattern match
2762      tem1 = (long) ptr1;
2763      tem2 = (long) ptr2;
2764      tem3 = tem2 - tem1;
2765      tem4 = (unsigned long) tem3;
2766      tem5 = ptr1 + tem4;
2767    and produce
2768      tem5 = ptr2;  */
2769 (simplify
2770   (pointer_plus @0 (convert?@2 (minus@3 (convert @1) (convert @0))))
2771   /* Conditionally look through a sign-changing conversion.  */
2772   (if (TYPE_PRECISION (TREE_TYPE (@2)) == TYPE_PRECISION (TREE_TYPE (@3))
2773        && ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@1)))
2774             || (GENERIC && type == TREE_TYPE (@1))))
2775    @1))
2776 (simplify
2777   (pointer_plus @0 (convert?@2 (pointer_diff@3 @1 @@0)))
2778   (if (TYPE_PRECISION (TREE_TYPE (@2)) >= TYPE_PRECISION (TREE_TYPE (@3)))
2779    (convert @1)))
2781 /* Pattern match
2782      tem = (sizetype) ptr;
2783      tem = tem & algn;
2784      tem = -tem;
2785      ... = ptr p+ tem;
2786    and produce the simpler and easier to analyze with respect to alignment
2787      ... = ptr & ~algn;  */
2788 (simplify
2789   (pointer_plus @0 (negate (bit_and (convert @0) INTEGER_CST@1)))
2790   (with { tree algn = wide_int_to_tree (TREE_TYPE (@0), ~wi::to_wide (@1)); }
2791    (bit_and @0 { algn; })))
2793 /* Try folding difference of addresses.  */
2794 (simplify
2795  (minus (convert ADDR_EXPR@0) (convert (pointer_plus @1 @2)))
2796  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2797   (with { poly_int64 diff; }
2798    (if (ptr_difference_const (@0, @1, &diff))
2799     (minus { build_int_cst_type (type, diff); } (convert @2))))))
2800 (simplify
2801  (minus (convert (pointer_plus @0 @2)) (convert ADDR_EXPR@1))
2802  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2803   (with { poly_int64 diff; }
2804    (if (ptr_difference_const (@0, @1, &diff))
2805     (plus (convert @2) { build_int_cst_type (type, diff); })))))
2806 (simplify
2807  (minus (convert ADDR_EXPR@0) (convert @1))
2808  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2809   (with { poly_int64 diff; }
2810    (if (ptr_difference_const (@0, @1, &diff))
2811     { build_int_cst_type (type, diff); }))))
2812 (simplify
2813  (minus (convert @0) (convert ADDR_EXPR@1))
2814  (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
2815   (with { poly_int64 diff; }
2816    (if (ptr_difference_const (@0, @1, &diff))
2817     { build_int_cst_type (type, diff); }))))
2818 (simplify
2819  (pointer_diff (convert?@2 ADDR_EXPR@0) (convert1?@3 @1))
2820  (if (tree_nop_conversion_p (TREE_TYPE(@2), TREE_TYPE (@0))
2821       && tree_nop_conversion_p (TREE_TYPE(@3), TREE_TYPE (@1)))
2822   (with { poly_int64 diff; }
2823    (if (ptr_difference_const (@0, @1, &diff))
2824     { build_int_cst_type (type, diff); }))))
2825 (simplify
2826  (pointer_diff (convert?@2 @0) (convert1?@3 ADDR_EXPR@1))
2827  (if (tree_nop_conversion_p (TREE_TYPE(@2), TREE_TYPE (@0))
2828       && tree_nop_conversion_p (TREE_TYPE(@3), TREE_TYPE (@1)))
2829   (with { poly_int64 diff; }
2830    (if (ptr_difference_const (@0, @1, &diff))
2831     { build_int_cst_type (type, diff); }))))
2833 /* (&a+b) - (&a[1] + c) -> sizeof(a[0]) + (b - c) */
2834 (simplify
2835  (pointer_diff (pointer_plus ADDR_EXPR@0 @1) (pointer_plus ADDR_EXPR@2 @3))
2836  (with { poly_int64 diff; }
2837    (if (ptr_difference_const (@0, @2, &diff))
2838     (plus { build_int_cst_type (type, diff); } (convert (minus @1 @3))))))
2839 /* (p + b) - &p->d -> offsetof (*p, d) + b */
2840 (simplify
2841  (pointer_diff (pointer_plus @0 @1) ADDR_EXPR@2)
2842  (with { poly_int64 diff; }
2843    (if (ptr_difference_const (@0, @2, &diff))
2844     (plus { build_int_cst_type (type, diff); } (convert @1)))))
2845 (simplify
2846  (pointer_diff ADDR_EXPR@0 (pointer_plus @1 @2))
2847  (with { poly_int64 diff; }
2848    (if (ptr_difference_const (@0, @1, &diff))
2849     (minus { build_int_cst_type (type, diff); } (convert @2)))))
2851 /* Canonicalize (T *)(ptr - ptr-cst) to &MEM[ptr + -ptr-cst].  */
2852 (simplify
2853  (convert (pointer_diff @0 INTEGER_CST@1))
2854  (if (POINTER_TYPE_P (type))
2855   { build_fold_addr_expr_with_type
2856       (build2 (MEM_REF, char_type_node, @0,
2857                wide_int_to_tree (ptr_type_node, wi::neg (wi::to_wide (@1)))),
2858                type); }))
2860 /* If arg0 is derived from the address of an object or function, we may
2861    be able to fold this expression using the object or function's
2862    alignment.  */
2863 (simplify
2864  (bit_and (convert? @0) INTEGER_CST@1)
2865  (if (POINTER_TYPE_P (TREE_TYPE (@0))
2866       && tree_nop_conversion_p (type, TREE_TYPE (@0)))
2867   (with
2868    {
2869      unsigned int align;
2870      unsigned HOST_WIDE_INT bitpos;
2871      get_pointer_alignment_1 (@0, &align, &bitpos);
2872    }
2873    (if (wi::ltu_p (wi::to_wide (@1), align / BITS_PER_UNIT))
2874     { wide_int_to_tree (type, (wi::to_wide (@1)
2875                                & (bitpos / BITS_PER_UNIT))); }))))
2877 (match min_value
2878  uniform_integer_cst_p
2879  (with {
2880    tree int_cst = uniform_integer_cst_p (t);
2881    tree inner_type = TREE_TYPE (int_cst);
2882   }
2883   (if ((INTEGRAL_TYPE_P (inner_type)
2884         || POINTER_TYPE_P (inner_type))
2885        && wi::eq_p (wi::to_wide (int_cst), wi::min_value (inner_type))))))
2887 (match max_value
2888  uniform_integer_cst_p
2889  (with {
2890    tree int_cst = uniform_integer_cst_p (t);
2891    tree itype = TREE_TYPE (int_cst);
2892   }
2893  (if ((INTEGRAL_TYPE_P (itype)
2894        || POINTER_TYPE_P (itype))
2895       && wi::eq_p (wi::to_wide (int_cst), wi::max_value (itype))))))
2897 /* x >  y  &&  x != XXX_MIN  -->  x > y
2898    x >  y  &&  x == XXX_MIN  -->  false . */
2899 (for eqne (eq ne)
2900  (simplify
2901   (bit_and:c (gt:c@2 @0 @1) (eqne @0 min_value))
2902    (switch
2903     (if (eqne == EQ_EXPR)
2904      { constant_boolean_node (false, type); })
2905     (if (eqne == NE_EXPR)
2906      @2)
2907     )))
2909 /* x <  y  &&  x != XXX_MAX  -->  x < y
2910    x <  y  &&  x == XXX_MAX  -->  false.  */
2911 (for eqne (eq ne)
2912  (simplify
2913   (bit_and:c (lt:c@2 @0 @1) (eqne @0 max_value))
2914    (switch
2915     (if (eqne == EQ_EXPR)
2916      { constant_boolean_node (false, type); })
2917     (if (eqne == NE_EXPR)
2918      @2)
2919     )))
2921 /* x <=  y  &&  x == XXX_MIN  -->  x == XXX_MIN.  */
2922 (simplify
2923  (bit_and:c (le:c @0 @1) (eq@2 @0 min_value))
2924   @2)
2926 /* x >=  y  &&  x == XXX_MAX  -->  x == XXX_MAX.  */
2927 (simplify
2928  (bit_and:c (ge:c @0 @1) (eq@2 @0 max_value))
2929   @2)
2931 /* x >  y  ||  x != XXX_MIN   -->  x != XXX_MIN.  */
2932 (simplify
2933  (bit_ior:c (gt:c @0 @1) (ne@2 @0 min_value))
2934   @2)
2936 /* x <=  y  ||  x != XXX_MIN   -->  true.  */
2937 (simplify
2938  (bit_ior:c (le:c @0 @1) (ne @0 min_value))
2939   { constant_boolean_node (true, type); })
2941 /* x <=  y  ||  x == XXX_MIN   -->  x <= y.  */
2942 (simplify
2943  (bit_ior:c (le:c@2 @0 @1) (eq @0 min_value))
2944   @2)
2946 /* x <  y  ||  x != XXX_MAX   -->  x != XXX_MAX.  */
2947 (simplify
2948  (bit_ior:c (lt:c @0 @1) (ne@2 @0 max_value))
2949   @2)
2951 /* x >=  y  ||  x != XXX_MAX   -->  true
2952    x >=  y  ||  x == XXX_MAX   -->  x >= y.  */
2953 (for eqne (eq ne)
2954  (simplify
2955   (bit_ior:c (ge:c@2 @0 @1) (eqne @0 max_value))
2956    (switch
2957     (if (eqne == EQ_EXPR)
2958      @2)
2959     (if (eqne == NE_EXPR)
2960      { constant_boolean_node (true, type); }))))
2962 /* y == XXX_MIN || x < y --> x <= y - 1 */
2963 (simplify
2964  (bit_ior:c (eq:s @1 min_value) (lt:cs @0 @1))
2965   (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2966        && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
2967   (le @0 (minus @1 { build_int_cst (TREE_TYPE (@1), 1); }))))
2969 /* y != XXX_MIN && x >= y --> x > y - 1 */
2970 (simplify
2971  (bit_and:c (ne:s @1 min_value) (ge:cs @0 @1))
2972   (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
2973        && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
2974   (gt @0 (minus @1 { build_int_cst (TREE_TYPE (@1), 1); }))))
2976 /* Convert (X == CST1) && (X OP2 CST2) to a known value
2977    based on CST1 OP2 CST2.  Similarly for (X != CST1).  */
2978 /* Convert (X == Y) && (X OP2 Y) to a known value if X is an integral type.
2979    Similarly for (X != Y).  */
2981 (for code1 (eq ne)
2982  (for code2 (eq ne lt gt le ge)
2983   (simplify
2984    (bit_and:c (code1@3 @0 @1) (code2@4 @0 @2))
2985    (if ((TREE_CODE (@1) == INTEGER_CST
2986          && TREE_CODE (@2) == INTEGER_CST)
2987         || ((INTEGRAL_TYPE_P (TREE_TYPE (@1))
2988              || POINTER_TYPE_P (TREE_TYPE (@1)))
2989             && operand_equal_p (@1, @2)))
2990     (with
2991      {
2992       bool one_before = false;
2993       bool one_after = false;
2994       int cmp = 0;
2995       if (TREE_CODE (@1) == INTEGER_CST
2996           && TREE_CODE (@2) == INTEGER_CST)
2997         {
2998           cmp = tree_int_cst_compare (@1, @2);
2999           if (cmp < 0
3000               && wi::to_wide (@1) == wi::to_wide (@2) - 1)
3001             one_before = true;
3002           if (cmp > 0
3003               && wi::to_wide (@1) == wi::to_wide (@2) + 1)
3004             one_after = true;
3005         }
3006       bool val;
3007       switch (code2)
3008          {
3009         case EQ_EXPR: val = (cmp == 0); break;
3010         case NE_EXPR: val = (cmp != 0); break;
3011         case LT_EXPR: val = (cmp < 0); break;
3012         case GT_EXPR: val = (cmp > 0); break;
3013         case LE_EXPR: val = (cmp <= 0); break;
3014         case GE_EXPR: val = (cmp >= 0); break;
3015         default: gcc_unreachable ();
3016         }
3017      }
3018      (switch
3019       (if (code1 == EQ_EXPR && val) @3)
3020       (if (code1 == EQ_EXPR && !val) { constant_boolean_node (false, type); })
3021       (if (code1 == NE_EXPR && !val) @4)
3022       (if (code1 == NE_EXPR
3023            && code2 == GE_EXPR
3024            && cmp == 0)
3025        (gt @0 @1))
3026       (if (code1 == NE_EXPR
3027            && code2 == LE_EXPR
3028            && cmp == 0)
3029        (lt @0 @1))
3030       /* (a != (b+1)) & (a > b) -> a > (b+1) */
3031       (if (code1 == NE_EXPR
3032            && code2 == GT_EXPR
3033            && one_after)
3034        (gt @0 @1))
3035       /* (a != (b-1)) & (a < b) -> a < (b-1) */
3036       (if (code1 == NE_EXPR
3037            && code2 == LT_EXPR
3038            && one_before)
3039        (lt @0 @1))
3040      )
3041     )
3042    )
3043   )
3047 /* Convert (X OP1 CST1) && (X OP2 CST2).
3048    Convert (X OP1 Y) && (X OP2 Y).  */
3050 (for code1 (lt le gt ge)
3051  (for code2 (lt le gt ge)
3052   (simplify
3053   (bit_and (code1:c@3 @0 @1) (code2:c@4 @0 @2))
3054   (if ((TREE_CODE (@1) == INTEGER_CST
3055         && TREE_CODE (@2) == INTEGER_CST)
3056        || ((INTEGRAL_TYPE_P (TREE_TYPE (@1))
3057             || POINTER_TYPE_P (TREE_TYPE (@1)))
3058            && operand_equal_p (@1, @2)))
3059    (with
3060     {
3061      int cmp = 0;
3062      if (TREE_CODE (@1) == INTEGER_CST
3063          && TREE_CODE (@2) == INTEGER_CST)
3064        cmp = tree_int_cst_compare (@1, @2);
3065     }
3066     (switch
3067      /* Choose the more restrictive of two < or <= comparisons.  */
3068      (if ((code1 == LT_EXPR || code1 == LE_EXPR)
3069           && (code2 == LT_EXPR || code2 == LE_EXPR))
3070       (if ((cmp < 0) || (cmp == 0 && code1 == LT_EXPR))
3071        @3
3072        @4))
3073      /* Likewise chose the more restrictive of two > or >= comparisons.  */
3074      (if ((code1 == GT_EXPR || code1 == GE_EXPR)
3075           && (code2 == GT_EXPR || code2 == GE_EXPR))
3076       (if ((cmp > 0) || (cmp == 0 && code1 == GT_EXPR))
3077        @3
3078        @4))
3079      /* Check for singleton ranges.  */
3080      (if (cmp == 0
3081           && ((code1 == LE_EXPR && code2 == GE_EXPR)
3082             || (code1 == GE_EXPR && code2 == LE_EXPR)))
3083       (eq @0 @1))
3084      /* Check for disjoint ranges.  */
3085      (if (cmp <= 0
3086           && (code1 == LT_EXPR || code1 == LE_EXPR)
3087           && (code2 == GT_EXPR || code2 == GE_EXPR))
3088       { constant_boolean_node (false, type); })
3089      (if (cmp >= 0
3090           && (code1 == GT_EXPR || code1 == GE_EXPR)
3091           && (code2 == LT_EXPR || code2 == LE_EXPR))
3092       { constant_boolean_node (false, type); })
3093      ))))))
3095 /* Convert (X == CST1) || (X OP2 CST2) to a known value
3096    based on CST1 OP2 CST2.  Similarly for (X != CST1).  */
3097 /* Convert (X == Y) || (X OP2 Y) to a known value if X is an integral type.
3098    Similarly for (X != Y).  */
3100 (for code1 (eq ne)
3101  (for code2 (eq ne lt gt le ge)
3102   (simplify
3103    (bit_ior:c (code1@3 @0 @1) (code2@4 @0 @2))
3104    (if ((TREE_CODE (@1) == INTEGER_CST
3105          && TREE_CODE (@2) == INTEGER_CST)
3106         || ((INTEGRAL_TYPE_P (TREE_TYPE (@1))
3107             || POINTER_TYPE_P (TREE_TYPE (@1)))
3108             && operand_equal_p (@1, @2)))
3109     (with
3110      {
3111       bool one_before = false;
3112       bool one_after = false;
3113       int cmp = 0;
3114       if (TREE_CODE (@1) == INTEGER_CST
3115           && TREE_CODE (@2) == INTEGER_CST)
3116         {
3117           cmp = tree_int_cst_compare (@1, @2);
3118           if (cmp < 0
3119               && wi::to_wide (@1) == wi::to_wide (@2) - 1)
3120             one_before = true;
3121           if (cmp > 0
3122               && wi::to_wide (@1) == wi::to_wide (@2) + 1)
3123             one_after = true;
3124         }
3125       bool val;
3126       switch (code2)
3127         {
3128         case EQ_EXPR: val = (cmp == 0); break;
3129         case NE_EXPR: val = (cmp != 0); break;
3130         case LT_EXPR: val = (cmp < 0); break;
3131         case GT_EXPR: val = (cmp > 0); break;
3132         case LE_EXPR: val = (cmp <= 0); break;
3133         case GE_EXPR: val = (cmp >= 0); break;
3134         default: gcc_unreachable ();
3135         }
3136      }
3137      (switch
3138       (if (code1 == EQ_EXPR && val) @4)
3139       (if (code1 == NE_EXPR && val) { constant_boolean_node (true, type); })
3140       (if (code1 == NE_EXPR && !val) @3)
3141       (if (code1 == EQ_EXPR
3142            && code2 == GT_EXPR
3143            && cmp == 0)
3144        (ge @0 @1))
3145       (if (code1 == EQ_EXPR
3146            && code2 == LT_EXPR
3147            && cmp == 0)
3148        (le @0 @1))
3149       /* (a == (b-1)) | (a >= b) -> a >= (b-1) */
3150       (if (code1 == EQ_EXPR
3151            && code2 == GE_EXPR
3152            && one_before)
3153        (ge @0 @1))
3154       /* (a == (b+1)) | (a <= b) -> a <= (b-1) */
3155       (if (code1 == EQ_EXPR
3156            && code2 == LE_EXPR
3157            && one_after)
3158        (le @0 @1))
3159      )
3160     )
3161    )
3162   )
3166 /* Convert (X OP1 CST1) || (X OP2 CST2).
3167    Convert (X OP1 Y)    || (X OP2 Y).  */
3169 (for code1 (lt le gt ge)
3170  (for code2 (lt le gt ge)
3171   (simplify
3172   (bit_ior (code1@3 @0 @1) (code2@4 @0 @2))
3173   (if ((TREE_CODE (@1) == INTEGER_CST
3174         && TREE_CODE (@2) == INTEGER_CST)
3175        || ((INTEGRAL_TYPE_P (TREE_TYPE (@1))
3176             || POINTER_TYPE_P (TREE_TYPE (@1)))
3177            && operand_equal_p (@1, @2)))
3178    (with
3179     {
3180      int cmp = 0;
3181      if (TREE_CODE (@1) == INTEGER_CST
3182          && TREE_CODE (@2) == INTEGER_CST)
3183        cmp = tree_int_cst_compare (@1, @2);
3184     }
3185     (switch
3186      /* Choose the more restrictive of two < or <= comparisons.  */
3187      (if ((code1 == LT_EXPR || code1 == LE_EXPR)
3188           && (code2 == LT_EXPR || code2 == LE_EXPR))
3189       (if ((cmp < 0) || (cmp == 0 && code1 == LT_EXPR))
3190        @4
3191        @3))
3192      /* Likewise chose the more restrictive of two > or >= comparisons.  */
3193      (if ((code1 == GT_EXPR || code1 == GE_EXPR)
3194           && (code2 == GT_EXPR || code2 == GE_EXPR))
3195       (if ((cmp > 0) || (cmp == 0 && code1 == GT_EXPR))
3196        @4
3197        @3))
3198      /* Check for singleton ranges.  */
3199      (if (cmp == 0
3200           && ((code1 == LT_EXPR && code2 == GT_EXPR)
3201               || (code1 == GT_EXPR && code2 == LT_EXPR)))
3202       (ne @0 @2))
3203      /* Check for disjoint ranges.  */
3204      (if (cmp >= 0
3205           && (code1 == LT_EXPR || code1 == LE_EXPR)
3206           && (code2 == GT_EXPR || code2 == GE_EXPR))
3207       { constant_boolean_node (true, type); })
3208      (if (cmp <= 0
3209           && (code1 == GT_EXPR || code1 == GE_EXPR)
3210           && (code2 == LT_EXPR || code2 == LE_EXPR))
3211       { constant_boolean_node (true, type); })
3212      ))))))
3214 /* Optimize (a CMP b) ^ (a CMP b)  */
3215 /* Optimize (a CMP b) != (a CMP b)  */
3216 (for op (bit_xor ne)
3217  (for cmp1 (lt lt lt le le le)
3218       cmp2 (gt eq ne ge eq ne)
3219       rcmp (ne le gt ne lt ge)
3220   (simplify
3221    (op:c (cmp1:c @0 @1) (cmp2:c @0 @1))
3222    (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
3223     (rcmp @0 @1)))))
3225 /* Optimize (a CMP b) == (a CMP b)  */
3226 (for cmp1 (lt lt lt le le le)
3227      cmp2 (gt eq ne ge eq ne)
3228      rcmp (eq gt le eq ge lt)
3229  (simplify
3230   (eq:c (cmp1:c @0 @1) (cmp2:c @0 @1))
3231   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
3232     (rcmp @0 @1))))
3234 /* We can't reassociate at all for saturating types.  */
3235 (if (!TYPE_SATURATING (type))
3237  /* Contract negates.  */
3238  /* A + (-B) -> A - B */
3239  (simplify
3240   (plus:c @0 (convert? (negate @1)))
3241   /* Apply STRIP_NOPS on the negate.  */
3242   (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
3243        && !TYPE_OVERFLOW_SANITIZED (type))
3244    (with
3245     {
3246      tree t1 = type;
3247      if (INTEGRAL_TYPE_P (type)
3248          && TYPE_OVERFLOW_WRAPS (type) != TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
3249        t1 = TYPE_OVERFLOW_WRAPS (type) ? type : TREE_TYPE (@1);
3250     }
3251     (convert (minus (convert:t1 @0) (convert:t1 @1))))))
3252  /* A - (-B) -> A + B */
3253  (simplify
3254   (minus @0 (convert? (negate @1)))
3255   (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
3256        && !TYPE_OVERFLOW_SANITIZED (type))
3257    (with
3258     {
3259      tree t1 = type;
3260      if (INTEGRAL_TYPE_P (type)
3261          && TYPE_OVERFLOW_WRAPS (type) != TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
3262        t1 = TYPE_OVERFLOW_WRAPS (type) ? type : TREE_TYPE (@1);
3263     }
3264     (convert (plus (convert:t1 @0) (convert:t1 @1))))))
3265  /* -(T)(-A) -> (T)A
3266     Sign-extension is ok except for INT_MIN, which thankfully cannot
3267     happen without overflow.  */
3268  (simplify
3269   (negate (convert (negate @1)))
3270   (if (INTEGRAL_TYPE_P (type)
3271        && (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@1))
3272            || (!TYPE_UNSIGNED (TREE_TYPE (@1))
3273                && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))))
3274        && !TYPE_OVERFLOW_SANITIZED (type)
3275        && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@1)))
3276    (convert @1)))
3277  (simplify
3278   (negate (convert negate_expr_p@1))
3279   (if (SCALAR_FLOAT_TYPE_P (type)
3280        && ((DECIMAL_FLOAT_TYPE_P (type)
3281             == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1))
3282             && TYPE_PRECISION (type) >= TYPE_PRECISION (TREE_TYPE (@1)))
3283            || !HONOR_SIGN_DEPENDENT_ROUNDING (type)))
3284    (convert (negate @1))))
3285  (simplify
3286   (negate (nop_convert? (negate @1)))
3287   (if (!TYPE_OVERFLOW_SANITIZED (type)
3288        && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@1)))
3289    (view_convert @1)))
3291  /* We can't reassociate floating-point unless -fassociative-math
3292     or fixed-point plus or minus because of saturation to +-Inf.  */
3293  (if ((!FLOAT_TYPE_P (type) || flag_associative_math)
3294       && !FIXED_POINT_TYPE_P (type))
3296   /* Match patterns that allow contracting a plus-minus pair
3297      irrespective of overflow issues.  */
3298   /* (A +- B) - A       ->  +- B */
3299   /* (A +- B) -+ B      ->  A */
3300   /* A - (A +- B)       -> -+ B */
3301   /* A +- (B -+ A)      ->  +- B */
3302   (simplify
3303    (minus (nop_convert1? (plus:c (nop_convert2? @0) @1)) @0)
3304    (view_convert @1))
3305   (simplify
3306    (minus (nop_convert1? (minus (nop_convert2? @0) @1)) @0)
3307    (if (!ANY_INTEGRAL_TYPE_P (type)
3308         || TYPE_OVERFLOW_WRAPS (type))
3309    (negate (view_convert @1))
3310    (view_convert (negate @1))))
3311   (simplify
3312    (plus:c (nop_convert1? (minus @0 (nop_convert2? @1))) @1)
3313    (view_convert @0))
3314   (simplify
3315    (minus @0 (nop_convert1? (plus:c (nop_convert2? @0) @1)))
3316     (if (!ANY_INTEGRAL_TYPE_P (type)
3317          || TYPE_OVERFLOW_WRAPS (type))
3318      (negate (view_convert @1))
3319      (view_convert (negate @1))))
3320   (simplify
3321    (minus @0 (nop_convert1? (minus (nop_convert2? @0) @1)))
3322    (view_convert @1))
3323   /* (A +- B) + (C - A)   -> C +- B */
3324   /* (A +  B) - (A - C)   -> B + C */
3325   /* More cases are handled with comparisons.  */
3326   (simplify
3327    (plus:c (plus:c @0 @1) (minus @2 @0))
3328    (plus @2 @1))
3329   (simplify
3330    (plus:c (minus @0 @1) (minus @2 @0))
3331    (minus @2 @1))
3332   (simplify
3333    (plus:c (pointer_diff @0 @1) (pointer_diff @2 @0))
3334    (if (TYPE_OVERFLOW_UNDEFINED (type)
3335         && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0)))
3336     (pointer_diff @2 @1)))
3337   (simplify
3338    (minus (plus:c @0 @1) (minus @0 @2))
3339    (plus @1 @2))
3341   /* (A +- CST1) +- CST2 -> A + CST3
3342      Use view_convert because it is safe for vectors and equivalent for
3343      scalars.  */
3344   (for outer_op (plus minus)
3345    (for inner_op (plus minus)
3346         neg_inner_op (minus plus)
3347     (simplify
3348      (outer_op (nop_convert? (inner_op @0 CONSTANT_CLASS_P@1))
3349                CONSTANT_CLASS_P@2)
3350      /* If one of the types wraps, use that one.  */
3351      (if (!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_WRAPS (type))
3352       /* If all 3 captures are CONSTANT_CLASS_P, punt, as we might recurse
3353          forever if something doesn't simplify into a constant.  */
3354       (if (!CONSTANT_CLASS_P (@0))
3355        (if (outer_op == PLUS_EXPR)
3356         (plus (view_convert @0) (inner_op! @2 (view_convert @1)))
3357         (minus (view_convert @0) (neg_inner_op! @2 (view_convert @1)))))
3358       (if (!ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3359            || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
3360        (if (outer_op == PLUS_EXPR)
3361         (view_convert (plus @0 (inner_op! (view_convert @2) @1)))
3362         (view_convert (minus @0 (neg_inner_op! (view_convert @2) @1))))
3363        /* If the constant operation overflows we cannot do the transform
3364           directly as we would introduce undefined overflow, for example
3365           with (a - 1) + INT_MIN.  */
3366        (if (types_match (type, @0) && !TYPE_OVERFLOW_SANITIZED (type))
3367         (with { tree cst = const_binop (outer_op == inner_op
3368                                         ? PLUS_EXPR : MINUS_EXPR,
3369                                         type, @1, @2); }
3370          (if (cst)
3371           (if (INTEGRAL_TYPE_P (type) && !TREE_OVERFLOW (cst))
3372            (inner_op @0 { cst; } )
3373            /* X+INT_MAX+1 is X-INT_MIN.  */
3374            (if (INTEGRAL_TYPE_P (type)
3375                 && wi::to_wide (cst) == wi::min_value (type))
3376             (neg_inner_op @0 { wide_int_to_tree (type, wi::to_wide (cst)); })
3377             /* Last resort, use some unsigned type.  */
3378             (with { tree utype = unsigned_type_for (type); }
3379              (if (utype)
3380               (view_convert (inner_op
3381                              (view_convert:utype @0)
3382                              (view_convert:utype
3383                               { TREE_OVERFLOW (cst)
3384                                 ? drop_tree_overflow (cst) : cst; })))))))))))))))
3386   /* (CST1 - A) +- CST2 -> CST3 - A  */
3387   (for outer_op (plus minus)
3388    (simplify
3389     (outer_op (nop_convert? (minus CONSTANT_CLASS_P@1 @0)) CONSTANT_CLASS_P@2)
3390     /* If one of the types wraps, use that one.  */
3391     (if (!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_WRAPS (type))
3392      /* If all 3 captures are CONSTANT_CLASS_P, punt, as we might recurse
3393         forever if something doesn't simplify into a constant.  */
3394      (if (!CONSTANT_CLASS_P (@0))
3395       (minus (outer_op! (view_convert @1) @2) (view_convert @0)))
3396      (if (!ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3397           || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
3398       (view_convert (minus (outer_op! @1 (view_convert @2)) @0))
3399       (if (types_match (type, @0) && !TYPE_OVERFLOW_SANITIZED (type))
3400        (with { tree cst = const_binop (outer_op, type, @1, @2); }
3401         (if (cst && !TREE_OVERFLOW (cst))
3402          (minus { cst; } @0))))))))
3404   /* CST1 - (CST2 - A) -> CST3 + A
3405      Use view_convert because it is safe for vectors and equivalent for
3406      scalars.  */
3407   (simplify
3408    (minus CONSTANT_CLASS_P@1 (nop_convert? (minus CONSTANT_CLASS_P@2 @0)))
3409    /* If one of the types wraps, use that one.  */
3410    (if (!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_WRAPS (type))
3411     /* If all 3 captures are CONSTANT_CLASS_P, punt, as we might recurse
3412       forever if something doesn't simplify into a constant.  */
3413     (if (!CONSTANT_CLASS_P (@0))
3414      (plus (view_convert @0) (minus! @1 (view_convert @2))))
3415     (if (!ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3416          || TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
3417      (view_convert (plus @0 (minus! (view_convert @1) @2)))
3418      (if (types_match (type, @0) && !TYPE_OVERFLOW_SANITIZED (type))
3419       (with { tree cst = const_binop (MINUS_EXPR, type, @1, @2); }
3420        (if (cst && !TREE_OVERFLOW (cst))
3421         (plus { cst; } @0)))))))
3423 /* ((T)(A)) + CST -> (T)(A + CST)  */
3424 #if GIMPLE
3425   (simplify
3426    (plus (convert:s SSA_NAME@0) INTEGER_CST@1)
3427     (if (TREE_CODE (TREE_TYPE (@0)) == INTEGER_TYPE
3428          && TREE_CODE (type) == INTEGER_TYPE
3429          && TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (@0))
3430          && int_fits_type_p (@1, TREE_TYPE (@0)))
3431      /* Perform binary operation inside the cast if the constant fits
3432         and (A + CST)'s range does not overflow.  */
3433      (with
3434       {
3435         wi::overflow_type min_ovf = wi::OVF_OVERFLOW,
3436                           max_ovf = wi::OVF_OVERFLOW;
3437         tree inner_type = TREE_TYPE (@0);
3439         wide_int w1
3440           = wide_int::from (wi::to_wide (@1), TYPE_PRECISION (inner_type),
3441                             TYPE_SIGN (inner_type));
3443         value_range vr;
3444         if (get_global_range_query ()->range_of_expr (vr, @0)
3445             && !vr.varying_p () && !vr.undefined_p ())
3446           {
3447             wide_int wmin0 = vr.lower_bound ();
3448             wide_int wmax0 = vr.upper_bound ();
3449             wi::add (wmin0, w1, TYPE_SIGN (inner_type), &min_ovf);
3450             wi::add (wmax0, w1, TYPE_SIGN (inner_type), &max_ovf);
3451           }
3452       }
3453      (if (min_ovf == wi::OVF_NONE && max_ovf == wi::OVF_NONE)
3454       (convert (plus @0 { wide_int_to_tree (TREE_TYPE (@0), w1); } )))
3455      )))
3456 #endif
3458 /* ((T)(A + CST1)) + CST2 -> (T)(A) + (T)CST1 + CST2  */
3459 #if GIMPLE
3460   (for op (plus minus)
3461    (simplify
3462     (plus (convert:s (op:s @0 INTEGER_CST@1)) INTEGER_CST@2)
3463      (if (TREE_CODE (TREE_TYPE (@0)) == INTEGER_TYPE
3464           && TREE_CODE (type) == INTEGER_TYPE
3465           && TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (@0))
3466           && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
3467           && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0))
3468           && TYPE_OVERFLOW_WRAPS (type))
3469        (plus (convert @0) (op @2 (convert @1))))))
3470 #endif
3472 /* (T)(A) +- (T)(B) -> (T)(A +- B) only when (A +- B) could be simplified
3473    to a simple value.  */
3474   (for op (plus minus)
3475    (simplify
3476     (op (convert @0) (convert @1))
3477      (if (INTEGRAL_TYPE_P (type)
3478           && INTEGRAL_TYPE_P (TREE_TYPE (@0))
3479           && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))
3480           && types_match (TREE_TYPE (@0), TREE_TYPE (@1))
3481           && !TYPE_OVERFLOW_TRAPS (type)
3482           && !TYPE_OVERFLOW_SANITIZED (type))
3483       (convert (op! @0 @1)))))
3485   /* ~A + A -> -1 */
3486   (simplify
3487    (plus:c (convert? (bit_not @0)) (convert? @0))
3488    (if (!TYPE_OVERFLOW_TRAPS (type))
3489     (convert { build_all_ones_cst (TREE_TYPE (@0)); })))
3491   /* ~A + 1 -> -A */
3492   (simplify
3493    (plus (convert? (bit_not @0)) integer_each_onep)
3494    (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
3495     (negate (convert @0))))
3497   /* -A - 1 -> ~A */
3498   (simplify
3499    (minus (convert? (negate @0)) integer_each_onep)
3500    (if (!TYPE_OVERFLOW_TRAPS (type)
3501         && TREE_CODE (type) != COMPLEX_TYPE
3502         && tree_nop_conversion_p (type, TREE_TYPE (@0)))
3503     (bit_not (convert @0))))
3505   /* -1 - A -> ~A */
3506   (simplify
3507    (minus integer_all_onesp @0)
3508    (if (TREE_CODE (type) != COMPLEX_TYPE)
3509     (bit_not @0)))
3511   /* (T)(P + A) - (T)P -> (T) A */
3512   (simplify
3513    (minus (convert (plus:c @@0 @1))
3514     (convert? @0))
3515    (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
3516         /* For integer types, if A has a smaller type
3517            than T the result depends on the possible
3518            overflow in P + A.
3519            E.g. T=size_t, A=(unsigned)429497295, P>0.
3520            However, if an overflow in P + A would cause
3521            undefined behavior, we can assume that there
3522            is no overflow.  */
3523         || (INTEGRAL_TYPE_P (TREE_TYPE (@1))
3524             && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))))
3525     (convert @1)))
3526   (simplify
3527    (minus (convert (pointer_plus @@0 @1))
3528     (convert @0))
3529    (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
3530         /* For pointer types, if the conversion of A to the
3531            final type requires a sign- or zero-extension,
3532            then we have to punt - it is not defined which
3533            one is correct.  */
3534         || (POINTER_TYPE_P (TREE_TYPE (@0))
3535             && TREE_CODE (@1) == INTEGER_CST
3536             && tree_int_cst_sign_bit (@1) == 0))
3537     (convert @1)))
3538    (simplify
3539     (pointer_diff (pointer_plus @@0 @1) @0)
3540     /* The second argument of pointer_plus must be interpreted as signed, and
3541        thus sign-extended if necessary.  */
3542     (with { tree stype = signed_type_for (TREE_TYPE (@1)); }
3543      /* Use view_convert instead of convert here, as POINTER_PLUS_EXPR
3544         second arg is unsigned even when we need to consider it as signed,
3545         we don't want to diagnose overflow here.  */
3546      (convert (view_convert:stype @1))))
3548   /* (T)P - (T)(P + A) -> -(T) A */
3549   (simplify
3550    (minus (convert? @0)
3551     (convert (plus:c @@0 @1)))
3552    (if (INTEGRAL_TYPE_P (type)
3553         && TYPE_OVERFLOW_UNDEFINED (type)
3554         /* For integer literals, using an intermediate unsigned type to avoid
3555            an overflow at run time is counter-productive because it introduces
3556            spurious overflows at compile time, in the form of TREE_OVERFLOW on
3557            the result, which may be problematic in GENERIC for some front-ends:
3558              (T)P - (T)(P + 4) -> (T)(-(U)4) -> (T)(4294967292) -> -4(OVF)
3559            so we use the direct path for them.  */
3560         && TREE_CODE (@1) != INTEGER_CST
3561         && element_precision (type) <= element_precision (TREE_TYPE (@1)))
3562     (with { tree utype = unsigned_type_for (type); }
3563      (convert (negate (convert:utype @1))))
3564     (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
3565          /* For integer types, if A has a smaller type
3566             than T the result depends on the possible
3567             overflow in P + A.
3568             E.g. T=size_t, A=(unsigned)429497295, P>0.
3569             However, if an overflow in P + A would cause
3570             undefined behavior, we can assume that there
3571             is no overflow.  */
3572          || (INTEGRAL_TYPE_P (TREE_TYPE (@1))
3573              && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))))
3574      (negate (convert @1)))))
3575   (simplify
3576    (minus (convert @0)
3577     (convert (pointer_plus @@0 @1)))
3578    (if (INTEGRAL_TYPE_P (type)
3579         && TYPE_OVERFLOW_UNDEFINED (type)
3580         /* See above the rationale for this condition.  */
3581         && TREE_CODE (@1) != INTEGER_CST
3582         && element_precision (type) <= element_precision (TREE_TYPE (@1)))
3583     (with { tree utype = unsigned_type_for (type); }
3584      (convert (negate (convert:utype @1))))
3585     (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
3586          /* For pointer types, if the conversion of A to the
3587             final type requires a sign- or zero-extension,
3588             then we have to punt - it is not defined which
3589             one is correct.  */
3590          || (POINTER_TYPE_P (TREE_TYPE (@0))
3591              && TREE_CODE (@1) == INTEGER_CST
3592              && tree_int_cst_sign_bit (@1) == 0))
3593      (negate (convert @1)))))
3594    (simplify
3595     (pointer_diff @0 (pointer_plus @@0 @1))
3596     /* The second argument of pointer_plus must be interpreted as signed, and
3597        thus sign-extended if necessary.  */
3598     (with { tree stype = signed_type_for (TREE_TYPE (@1)); }
3599      /* Use view_convert instead of convert here, as POINTER_PLUS_EXPR
3600         second arg is unsigned even when we need to consider it as signed,
3601         we don't want to diagnose overflow here.  */
3602      (negate (convert (view_convert:stype @1)))))
3604   /* (T)(P + A) - (T)(P + B) -> (T)A - (T)B */
3605   (simplify
3606    (minus (convert (plus:c @@0 @1))
3607     (convert (plus:c @0 @2)))
3608    (if (INTEGRAL_TYPE_P (type)
3609         && TYPE_OVERFLOW_UNDEFINED (type)
3610         && element_precision (type) <= element_precision (TREE_TYPE (@1))
3611         && element_precision (type) <= element_precision (TREE_TYPE (@2)))
3612     (with { tree utype = unsigned_type_for (type); }
3613      (convert (minus (convert:utype @1) (convert:utype @2))))
3614     (if (((element_precision (type) <= element_precision (TREE_TYPE (@1)))
3615           == (element_precision (type) <= element_precision (TREE_TYPE (@2))))
3616          && (element_precision (type) <= element_precision (TREE_TYPE (@1))
3617              /* For integer types, if A has a smaller type
3618                 than T the result depends on the possible
3619                 overflow in P + A.
3620                 E.g. T=size_t, A=(unsigned)429497295, P>0.
3621                 However, if an overflow in P + A would cause
3622                 undefined behavior, we can assume that there
3623                 is no overflow.  */
3624              || (INTEGRAL_TYPE_P (TREE_TYPE (@1))
3625                  && INTEGRAL_TYPE_P (TREE_TYPE (@2))
3626                  && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))
3627                  && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@2)))))
3628      (minus (convert @1) (convert @2)))))
3629   (simplify
3630    (minus (convert (pointer_plus @@0 @1))
3631     (convert (pointer_plus @0 @2)))
3632    (if (INTEGRAL_TYPE_P (type)
3633         && TYPE_OVERFLOW_UNDEFINED (type)
3634         && element_precision (type) <= element_precision (TREE_TYPE (@1)))
3635     (with { tree utype = unsigned_type_for (type); }
3636      (convert (minus (convert:utype @1) (convert:utype @2))))
3637     (if (element_precision (type) <= element_precision (TREE_TYPE (@1))
3638          /* For pointer types, if the conversion of A to the
3639             final type requires a sign- or zero-extension,
3640             then we have to punt - it is not defined which
3641             one is correct.  */
3642          || (POINTER_TYPE_P (TREE_TYPE (@0))
3643              && TREE_CODE (@1) == INTEGER_CST
3644              && tree_int_cst_sign_bit (@1) == 0
3645              && TREE_CODE (@2) == INTEGER_CST
3646              && tree_int_cst_sign_bit (@2) == 0))
3647      (minus (convert @1) (convert @2)))))
3648    (simplify
3649     (pointer_diff (pointer_plus @0 @2) (pointer_plus @1 @2))
3650      (pointer_diff @0 @1))
3651    (simplify
3652     (pointer_diff (pointer_plus @@0 @1) (pointer_plus @0 @2))
3653     /* The second argument of pointer_plus must be interpreted as signed, and
3654        thus sign-extended if necessary.  */
3655     (with { tree stype = signed_type_for (TREE_TYPE (@1)); }
3656      /* Use view_convert instead of convert here, as POINTER_PLUS_EXPR
3657         second arg is unsigned even when we need to consider it as signed,
3658         we don't want to diagnose overflow here.  */
3659      (minus (convert (view_convert:stype @1))
3660             (convert (view_convert:stype @2)))))))
3662 /* (A * C) +- (B * C) -> (A+-B) * C and (A * C) +- A -> A * (C+-1).
3663     Modeled after fold_plusminus_mult_expr.  */
3664 (if (!TYPE_SATURATING (type)
3665      && (!FLOAT_TYPE_P (type) || flag_associative_math))
3666  (for plusminus (plus minus)
3667   (simplify
3668    (plusminus (mult:cs@3 @0 @1) (mult:cs@4 @0 @2))
3669    (if (!ANY_INTEGRAL_TYPE_P (type)
3670         || TYPE_OVERFLOW_WRAPS (type)
3671         || (INTEGRAL_TYPE_P (type)
3672             && tree_expr_nonzero_p (@0)
3673             && expr_not_equal_to (@0, wi::minus_one (TYPE_PRECISION (type)))))
3674     (if (single_use (@3) || single_use (@4))
3675      /* If @1 +- @2 is constant require a hard single-use on either
3676         original operand (but not on both).  */
3677      (mult (plusminus @1 @2) @0)
3678      (mult! (plusminus @1 @2) @0)
3679   )))
3680   /* We cannot generate constant 1 for fract.  */
3681   (if (!ALL_FRACT_MODE_P (TYPE_MODE (type)))
3682    (simplify
3683     (plusminus @0 (mult:c@3 @0 @2))
3684     (if ((!ANY_INTEGRAL_TYPE_P (type)
3685           || TYPE_OVERFLOW_WRAPS (type)
3686           /* For @0 + @0*@2 this transformation would introduce UB
3687              (where there was none before) for @0 in [-1,0] and @2 max.
3688              For @0 - @0*@2 this transformation would introduce UB
3689              for @0 0 and @2 in [min,min+1] or @0 -1 and @2 min+1.  */
3690           || (INTEGRAL_TYPE_P (type)
3691               && ((tree_expr_nonzero_p (@0)
3692                    && expr_not_equal_to (@0,
3693                                 wi::minus_one (TYPE_PRECISION (type))))
3694                   || (plusminus == PLUS_EXPR
3695                       ? expr_not_equal_to (@2,
3696                             wi::max_value (TYPE_PRECISION (type), SIGNED))
3697                       /* Let's ignore the @0 -1 and @2 min case.  */
3698                       : (expr_not_equal_to (@2,
3699                             wi::min_value (TYPE_PRECISION (type), SIGNED))
3700                          && expr_not_equal_to (@2,
3701                                 wi::min_value (TYPE_PRECISION (type), SIGNED)
3702                                 + 1))))))
3703          && single_use (@3))
3704      (mult (plusminus { build_one_cst (type); } @2) @0)))
3705    (simplify
3706     (plusminus (mult:c@3 @0 @2) @0)
3707     (if ((!ANY_INTEGRAL_TYPE_P (type)
3708           || TYPE_OVERFLOW_WRAPS (type)
3709           /* For @0*@2 + @0 this transformation would introduce UB
3710              (where there was none before) for @0 in [-1,0] and @2 max.
3711              For @0*@2 - @0 this transformation would introduce UB
3712              for @0 0 and @2 min.  */
3713           || (INTEGRAL_TYPE_P (type)
3714               && ((tree_expr_nonzero_p (@0)
3715                    && (plusminus == MINUS_EXPR
3716                        || expr_not_equal_to (@0,
3717                                 wi::minus_one (TYPE_PRECISION (type)))))
3718                   || expr_not_equal_to (@2,
3719                         (plusminus == PLUS_EXPR
3720                          ? wi::max_value (TYPE_PRECISION (type), SIGNED)
3721                          : wi::min_value (TYPE_PRECISION (type), SIGNED))))))
3722          && single_use (@3))
3723      (mult (plusminus @2 { build_one_cst (type); }) @0))))))
3725 #if GIMPLE
3726 /* Canonicalize X + (X << C) into X * (1 + (1 << C)) and
3727    (X << C1) + (X << C2) into X * ((1 << C1) + (1 << C2)).  */
3728 (simplify
3729  (plus:c @0 (lshift:s @0 INTEGER_CST@1))
3730   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3731        && tree_fits_uhwi_p (@1)
3732        && tree_to_uhwi (@1) < element_precision (type)
3733        && (INTEGRAL_TYPE_P (TREE_TYPE (@0))
3734            || optab_handler (smul_optab,
3735                              TYPE_MODE (type)) != CODE_FOR_nothing))
3736    (with { tree t = type;
3737            if (!TYPE_OVERFLOW_WRAPS (t)) t = unsigned_type_for (t);
3738            wide_int w = wi::set_bit_in_zero (tree_to_uhwi (@1),
3739                                              element_precision (type));
3740            w += 1;
3741            tree cst = wide_int_to_tree (VECTOR_TYPE_P (t) ? TREE_TYPE (t)
3742                                         : t, w);
3743            cst = build_uniform_cst (t, cst); }
3744     (convert (mult (convert:t @0) { cst; })))))
3745 (simplify
3746  (plus (lshift:s @0 INTEGER_CST@1) (lshift:s @0 INTEGER_CST@2))
3747   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3748        && tree_fits_uhwi_p (@1)
3749        && tree_to_uhwi (@1) < element_precision (type)
3750        && tree_fits_uhwi_p (@2)
3751        && tree_to_uhwi (@2) < element_precision (type)
3752        && (INTEGRAL_TYPE_P (TREE_TYPE (@0))
3753            || optab_handler (smul_optab,
3754                              TYPE_MODE (type)) != CODE_FOR_nothing))
3755    (with { tree t = type;
3756            if (!TYPE_OVERFLOW_WRAPS (t)) t = unsigned_type_for (t);
3757            unsigned int prec = element_precision (type);
3758            wide_int w = wi::set_bit_in_zero (tree_to_uhwi (@1), prec);
3759            w += wi::set_bit_in_zero (tree_to_uhwi (@2), prec);
3760            tree cst = wide_int_to_tree (VECTOR_TYPE_P (t) ? TREE_TYPE (t)
3761                                         : t, w);
3762            cst = build_uniform_cst (t, cst); }
3763     (convert (mult (convert:t @0) { cst; })))))
3764 #endif
3766 /* Canonicalize (X*C1)|(X*C2) and (X*C1)^(X*C2) to (C1+C2)*X when
3767    tree_nonzero_bits allows IOR and XOR to be treated like PLUS.
3768    Likewise, handle (X<<C3) and X as legitimate variants of X*C.  */
3769 (for op (bit_ior bit_xor)
3770  (simplify
3771   (op (mult:s@0 @1 INTEGER_CST@2)
3772       (mult:s@3 @1 INTEGER_CST@4))
3773   (if (INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_WRAPS (type)
3774        && (tree_nonzero_bits (@0) & tree_nonzero_bits (@3)) == 0)
3775    (mult @1
3776          { wide_int_to_tree (type, wi::to_wide (@2) + wi::to_wide (@4)); })))
3777  (simplify
3778   (op:c (mult:s@0 @1 INTEGER_CST@2)
3779         (lshift:s@3 @1 INTEGER_CST@4))
3780   (if (INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_WRAPS (type)
3781        && tree_int_cst_sgn (@4) > 0
3782        && (tree_nonzero_bits (@0) & tree_nonzero_bits (@3)) == 0)
3783    (with { wide_int wone = wi::one (TYPE_PRECISION (type));
3784            wide_int c = wi::add (wi::to_wide (@2),
3785                                  wi::lshift (wone, wi::to_wide (@4))); }
3786     (mult @1 { wide_int_to_tree (type, c); }))))
3787  (simplify
3788   (op:c (mult:s@0 @1 INTEGER_CST@2)
3789         @1)
3790   (if (INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_WRAPS (type)
3791        && (tree_nonzero_bits (@0) & tree_nonzero_bits (@1)) == 0)
3792    (mult @1
3793          { wide_int_to_tree (type,
3794                              wi::add (wi::to_wide (@2), 1)); })))
3795  (simplify
3796   (op (lshift:s@0 @1 INTEGER_CST@2)
3797       (lshift:s@3 @1 INTEGER_CST@4))
3798   (if (INTEGRAL_TYPE_P (type)
3799        && tree_int_cst_sgn (@2) > 0
3800        && tree_int_cst_sgn (@4) > 0
3801        && (tree_nonzero_bits (@0) & tree_nonzero_bits (@3)) == 0)
3802    (with { tree t = type;
3803            if (!TYPE_OVERFLOW_WRAPS (t))
3804              t = unsigned_type_for (t);
3805            wide_int wone = wi::one (TYPE_PRECISION (t));
3806            wide_int c = wi::add (wi::lshift (wone, wi::to_wide (@2)),
3807                                  wi::lshift (wone, wi::to_wide (@4))); }
3808     (convert (mult:t (convert:t @1) { wide_int_to_tree (t,c); })))))
3809  (simplify
3810   (op:c (lshift:s@0 @1 INTEGER_CST@2)
3811         @1)
3812   (if (INTEGRAL_TYPE_P (type)
3813        && tree_int_cst_sgn (@2) > 0
3814        && (tree_nonzero_bits (@0) & tree_nonzero_bits (@1)) == 0)
3815    (with { tree t = type;
3816            if (!TYPE_OVERFLOW_WRAPS (t))
3817              t = unsigned_type_for (t);
3818            wide_int wone = wi::one (TYPE_PRECISION (t));
3819            wide_int c = wi::add (wi::lshift (wone, wi::to_wide (@2)), wone); }
3820     (convert (mult:t (convert:t @1) { wide_int_to_tree (t, c); }))))))
3822 /* Simplifications of MIN_EXPR, MAX_EXPR, fmin() and fmax().  */
3824 (for minmax (min max)
3825  (simplify
3826   (minmax @0 @0)
3827   @0)
3828 /* max(max(x,y),x) -> max(x,y)  */
3829  (simplify
3830   (minmax:c (minmax:c@2 @0 @1) @0)
3831   @2))
3832 /* For fmin() and fmax(), skip folding when both are sNaN.  */
3833 (for minmax (FMIN_ALL FMAX_ALL)
3834  (simplify
3835   (minmax @0 @0)
3836   (if (!tree_expr_maybe_signaling_nan_p (@0))
3837     @0)))
3838 /* min(max(x,y),y) -> y.  */
3839 (simplify
3840  (min:c (max:c @0 @1) @1)
3841  @1)
3842 /* max(min(x,y),y) -> y.  */
3843 (simplify
3844  (max:c (min:c @0 @1) @1)
3845  @1)
3846 /* max(a,-a) -> abs(a).  */
3847 (simplify
3848  (max:c @0 (negate @0))
3849  (if (TREE_CODE (type) != COMPLEX_TYPE
3850       && (! ANY_INTEGRAL_TYPE_P (type)
3851           || TYPE_OVERFLOW_UNDEFINED (type)))
3852   (abs @0)))
3853 /* min(a,-a) -> -abs(a).  */
3854 (simplify
3855  (min:c @0 (negate @0))
3856  (if (TREE_CODE (type) != COMPLEX_TYPE
3857       && (! ANY_INTEGRAL_TYPE_P (type)
3858           || TYPE_OVERFLOW_UNDEFINED (type)))
3859   (negate (abs @0))))
3860 (simplify
3861  (min @0 @1)
3862  (switch
3863   (if (INTEGRAL_TYPE_P (type)
3864        && TYPE_MIN_VALUE (type)
3865        && operand_equal_p (@1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
3866    @1)
3867   (if (INTEGRAL_TYPE_P (type)
3868        && TYPE_MAX_VALUE (type)
3869        && operand_equal_p (@1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
3870    @0)))
3871 (simplify
3872  (max @0 @1)
3873  (switch
3874   (if (INTEGRAL_TYPE_P (type)
3875        && TYPE_MAX_VALUE (type)
3876        && operand_equal_p (@1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
3877    @1)
3878   (if (INTEGRAL_TYPE_P (type)
3879        && TYPE_MIN_VALUE (type)
3880        && operand_equal_p (@1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
3881    @0)))
3883 /* max (a, a + CST) -> a + CST where CST is positive.  */
3884 /* max (a, a + CST) -> a where CST is negative.  */
3885 (simplify
3886  (max:c @0 (plus@2 @0 INTEGER_CST@1))
3887   (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
3888    (if (tree_int_cst_sgn (@1) > 0)
3889     @2
3890     @0)))
3892 /* min (a, a + CST) -> a where CST is positive.  */
3893 /* min (a, a + CST) -> a + CST where CST is negative. */
3894 (simplify
3895  (min:c @0 (plus@2 @0 INTEGER_CST@1))
3896   (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
3897    (if (tree_int_cst_sgn (@1) > 0)
3898     @0
3899     @2)))
3901 /* Simplify min (&var[off0], &var[off1]) etc. depending on whether
3902    the addresses are known to be less, equal or greater.  */
3903 (for minmax (min max)
3904      cmp (lt gt)
3905  (simplify
3906   (minmax (convert1?@2 addr@0) (convert2?@3 addr@1))
3907   (with
3908    {
3909      poly_int64 off0, off1;
3910      tree base0, base1;
3911      int equal = address_compare (cmp, TREE_TYPE (@2), @0, @1, base0, base1,
3912                                   off0, off1, GENERIC);
3913    }
3914    (if (equal == 1)
3915     (if (minmax == MIN_EXPR)
3916      (if (known_le (off0, off1))
3917       @2
3918       (if (known_gt (off0, off1))
3919        @3))
3920      (if (known_ge (off0, off1))
3921       @2
3922       (if (known_lt (off0, off1))
3923        @3)))))))
3925 /* (convert (minmax ((convert (x) c)))) -> minmax (x c) if x is promoted
3926    and the outer convert demotes the expression back to x's type.  */
3927 (for minmax (min max)
3928  (simplify
3929   (convert (minmax@0 (convert @1) INTEGER_CST@2))
3930   (if (INTEGRAL_TYPE_P (type)
3931        && types_match (@1, type) && int_fits_type_p (@2, type)
3932        && TYPE_SIGN (TREE_TYPE (@0)) == TYPE_SIGN (type)
3933        && TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type))
3934    (minmax @1 (convert @2)))))
3936 (for minmax (FMIN_ALL FMAX_ALL)
3937  /* If either argument is NaN and other one is not sNaN, return the other
3938     one.  Avoid the transformation if we get (and honor) a signalling NaN.  */
3939  (simplify
3940   (minmax:c @0 REAL_CST@1)
3941    (if (real_isnan (TREE_REAL_CST_PTR (@1))
3942        && (!HONOR_SNANS (@1) || !TREE_REAL_CST (@1).signalling)
3943        && !tree_expr_maybe_signaling_nan_p (@0))
3944    @0)))
3945 /* Convert fmin/fmax to MIN_EXPR/MAX_EXPR.  C99 requires these
3946    functions to return the numeric arg if the other one is NaN.
3947    MIN and MAX don't honor that, so only transform if -ffinite-math-only
3948    is set.  C99 doesn't require -0.0 to be handled, so we don't have to
3949    worry about it either.  */
3950 (if (flag_finite_math_only)
3951  (simplify
3952   (FMIN_ALL @0 @1)
3953   (min @0 @1))
3954  (simplify
3955   (FMAX_ALL @0 @1)
3956   (max @0 @1)))
3957 /* min (-A, -B) -> -max (A, B)  */
3958 (for minmax (min max FMIN_ALL FMAX_ALL)
3959      maxmin (max min FMAX_ALL FMIN_ALL)
3960  (simplify
3961   (minmax (negate:s@2 @0) (negate:s@3 @1))
3962   (if (FLOAT_TYPE_P (TREE_TYPE (@0))
3963        || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
3964            && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))))
3965    (negate (maxmin @0 @1)))))
3966 /* MIN (~X, ~Y) -> ~MAX (X, Y)
3967    MAX (~X, ~Y) -> ~MIN (X, Y)  */
3968 (for minmax (min max)
3969  maxmin (max min)
3970  (simplify
3971   (minmax (bit_not:s@2 @0) (bit_not:s@3 @1))
3972   (bit_not (maxmin @0 @1)))
3973 /* ~MAX(~X, Y) --> MIN(X, ~Y) */
3974 /* ~MIN(~X, Y) --> MAX(X, ~Y) */
3975  (simplify
3976   (bit_not (minmax:cs (bit_not @0) @1))
3977   (maxmin @0 (bit_not @1))))
3979 /* MIN (X, Y) == X -> X <= Y  */
3980 /* MIN (X, Y) < X -> X > Y  */
3981 /* MIN (X, Y) >= X -> X <= Y  */
3982 (for minmax (min min min min max max max max)
3983      cmp    (eq  ne  lt  ge  eq  ne  gt  le )
3984      out    (le  gt  gt  le  ge  lt  lt  ge )
3985  (simplify
3986   (cmp:c (minmax:c @0 @1) @0)
3987   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0)))
3988    (out @0 @1))))
3989 /* MIN (X, 5) == 0 -> X == 0
3990    MIN (X, 5) == 7 -> false  */
3991 (for cmp (eq ne)
3992  (simplify
3993   (cmp (min @0 INTEGER_CST@1) INTEGER_CST@2)
3994   (if (wi::lt_p (wi::to_wide (@1), wi::to_wide (@2),
3995                  TYPE_SIGN (TREE_TYPE (@0))))
3996    { constant_boolean_node (cmp == NE_EXPR, type); }
3997    (if (wi::gt_p (wi::to_wide (@1), wi::to_wide (@2),
3998                   TYPE_SIGN (TREE_TYPE (@0))))
3999     (cmp @0 @2)))))
4000 (for cmp (eq ne)
4001  (simplify
4002   (cmp (max @0 INTEGER_CST@1) INTEGER_CST@2)
4003   (if (wi::gt_p (wi::to_wide (@1), wi::to_wide (@2),
4004                  TYPE_SIGN (TREE_TYPE (@0))))
4005    { constant_boolean_node (cmp == NE_EXPR, type); }
4006    (if (wi::lt_p (wi::to_wide (@1), wi::to_wide (@2),
4007                   TYPE_SIGN (TREE_TYPE (@0))))
4008     (cmp @0 @2)))))
4010 /* X <= MAX(X, Y) -> true
4011    X > MAX(X, Y) -> false 
4012    X >= MIN(X, Y) -> true
4013    X < MIN(X, Y) -> false */
4014 (for minmax (min     min     max     max     )
4015      cmp    (ge      lt      le      gt      )
4016  (simplify
4017   (cmp:c @0 (minmax:c @0 @1))
4018   { constant_boolean_node (cmp == GE_EXPR || cmp == LE_EXPR, type); } ))
4020 /* MIN (X, C1) < C2 -> X < C2 || C1 < C2  */
4021 (for minmax (min     min     max     max     min     min     max     max    )
4022      cmp    (lt      le      gt      ge      gt      ge      lt      le     )
4023      comb   (bit_ior bit_ior bit_ior bit_ior bit_and bit_and bit_and bit_and)
4024  (simplify
4025   (cmp (minmax @0 INTEGER_CST@1) INTEGER_CST@2)
4026   (comb (cmp @0 @2) (cmp @1 @2))))
4028 /* Undo fancy ways of writing max/min or other ?: expressions, like
4029    a - ((a - b) & -(a < b))  and  a - (a - b) * (a < b) into (a < b) ? b : a.
4030    People normally use ?: and that is what we actually try to optimize.  */
4031 /* Transform A + (B-A)*cmp into cmp ? B : A.  */
4032 (simplify
4033  (plus:c @0 (mult:c (minus @1 @0) zero_one_valued_p@2))
4034  (if (INTEGRAL_TYPE_P (type)
4035       && (GIMPLE || !TREE_SIDE_EFFECTS (@1)))
4036   (cond (convert:boolean_type_node @2) @1 @0)))
4037 /* Transform A - (A-B)*cmp into cmp ? B : A.  */
4038 (simplify
4039  (minus @0 (mult:c (minus @0 @1) zero_one_valued_p@2))
4040  (if (INTEGRAL_TYPE_P (type)
4041       && (GIMPLE || !TREE_SIDE_EFFECTS (@1)))
4042   (cond (convert:boolean_type_node @2) @1 @0)))
4043 /* Transform A ^ (A^B)*cmp into cmp ? B : A.  */
4044 (simplify
4045  (bit_xor:c @0 (mult:c (bit_xor:c @0 @1) zero_one_valued_p@2))
4046  (if (INTEGRAL_TYPE_P (type)
4047       && (GIMPLE || !TREE_SIDE_EFFECTS (@1)))
4048   (cond (convert:boolean_type_node @2) @1 @0)))
4050 /* (x <= 0 ? -x : 0) -> max(-x, 0).  */
4051 (simplify
4052   (cond (le @0 integer_zerop@1) (negate@2 @0) integer_zerop@1)
4053   (max @2 @1))
4055 /* (zero_one == 0) ? y : z <op> y -> ((typeof(y))zero_one * z) <op> y */
4056 (for op (bit_xor bit_ior plus)
4057  (simplify
4058   (cond (eq zero_one_valued_p@0
4059             integer_zerop)
4060         @1
4061         (op:c @2 @1))
4062   (if (INTEGRAL_TYPE_P (type)
4063        && TYPE_PRECISION (type) > 1
4064        && (INTEGRAL_TYPE_P (TREE_TYPE (@0))))
4065        (op (mult (convert:type @0) @2) @1))))
4067 /* (zero_one != 0) ? z <op> y : y -> ((typeof(y))zero_one * z) <op> y */
4068 (for op (bit_xor bit_ior plus)
4069  (simplify
4070   (cond (ne zero_one_valued_p@0
4071             integer_zerop)
4072        (op:c @2 @1)
4073         @1)
4074   (if (INTEGRAL_TYPE_P (type)
4075        && TYPE_PRECISION (type) > 1
4076        && (INTEGRAL_TYPE_P (TREE_TYPE (@0))))
4077        (op (mult (convert:type @0) @2) @1))))
4079 /* Simplifications of shift and rotates.  */
4081 (for rotate (lrotate rrotate)
4082  (simplify
4083   (rotate integer_all_onesp@0 @1)
4084   @0))
4086 /* Optimize -1 >> x for arithmetic right shifts.  */
4087 (simplify
4088  (rshift integer_all_onesp@0 @1)
4089  (if (!TYPE_UNSIGNED (type))
4090   @0))
4092 /* Optimize (x >> c) << c into x & (-1<<c).  */
4093 (simplify
4094  (lshift (nop_convert? (rshift @0 INTEGER_CST@1)) @1)
4095  (if (wi::ltu_p (wi::to_wide (@1), element_precision (type)))
4096   /* It doesn't matter if the right shift is arithmetic or logical.  */
4097   (bit_and (view_convert @0) (lshift { build_minus_one_cst (type); } @1))))
4099 (simplify
4100  (lshift (convert (convert@2 (rshift @0 INTEGER_CST@1))) @1)
4101  (if (wi::ltu_p (wi::to_wide (@1), element_precision (type))
4102       /* Allow intermediate conversion to integral type with whatever sign, as
4103          long as the low TYPE_PRECISION (type)
4104          - TYPE_PRECISION (TREE_TYPE (@2)) bits are preserved.  */
4105       && INTEGRAL_TYPE_P (type)
4106       && INTEGRAL_TYPE_P (TREE_TYPE (@2))
4107       && INTEGRAL_TYPE_P (TREE_TYPE (@0))
4108       && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (@0))
4109       && (TYPE_PRECISION (TREE_TYPE (@2)) >= TYPE_PRECISION (type)
4110           || wi::geu_p (wi::to_wide (@1),
4111                         TYPE_PRECISION (type)
4112                         - TYPE_PRECISION (TREE_TYPE (@2)))))
4113   (bit_and (convert @0) (lshift { build_minus_one_cst (type); } @1))))
4115 /* For (x << c) >> c, optimize into x & ((unsigned)-1 >> c) for
4116    unsigned x OR truncate into the precision(type) - c lowest bits
4117    of signed x (if they have mode precision or a precision of 1).  */
4118 (simplify
4119  (rshift (nop_convert? (lshift @0 INTEGER_CST@1)) @@1)
4120  (if (wi::ltu_p (wi::to_wide (@1), element_precision (type)))
4121   (if (TYPE_UNSIGNED (type))
4122    (bit_and (convert @0) (rshift { build_minus_one_cst (type); } @1))
4123    (if (INTEGRAL_TYPE_P (type))
4124     (with {
4125       int width = element_precision (type) - tree_to_uhwi (@1);
4126       tree stype = NULL_TREE;
4127       scalar_int_mode mode = (targetm.scalar_mode_supported_p (TImode)
4128                               ? TImode : DImode);
4129       if (width <= GET_MODE_PRECISION (mode))
4130         stype = build_nonstandard_integer_type (width, 0);
4131      }
4132      (if (stype && (width == 1 || type_has_mode_precision_p (stype)))
4133       (convert (convert:stype @0))))))))
4135 /* Optimize x >> x into 0 */
4136 (simplify
4137  (rshift @0 @0)
4138   { build_zero_cst (type); })
4140 (for shiftrotate (lrotate rrotate lshift rshift)
4141  (simplify
4142   (shiftrotate @0 integer_zerop)
4143   (non_lvalue @0))
4144  (simplify
4145   (shiftrotate integer_zerop@0 @1)
4146   @0)
4147  /* Prefer vector1 << scalar to vector1 << vector2
4148     if vector2 is uniform.  */
4149  (for vec (VECTOR_CST CONSTRUCTOR)
4150   (simplify
4151    (shiftrotate @0 vec@1)
4152    (with { tree tem = uniform_vector_p (@1); }
4153     (if (tem)
4154      (shiftrotate @0 { tem; }))))))
4156 /* Simplify X << Y where Y's low width bits are 0 to X, as only valid
4157    Y is 0.  Similarly for X >> Y.  */
4158 #if GIMPLE
4159 (for shift (lshift rshift)
4160  (simplify
4161   (shift @0 SSA_NAME@1)
4162    (if (INTEGRAL_TYPE_P (TREE_TYPE (@1)))
4163     (with {
4164       int width = ceil_log2 (element_precision (TREE_TYPE (@0)));
4165       int prec = TYPE_PRECISION (TREE_TYPE (@1));
4166      }
4167      (if ((get_nonzero_bits (@1) & wi::mask (width, false, prec)) == 0)
4168       @0)))))
4169 #endif
4171 /* Rewrite an LROTATE_EXPR by a constant into an
4172    RROTATE_EXPR by a new constant.  */
4173 (simplify
4174  (lrotate @0 INTEGER_CST@1)
4175  (rrotate @0 { const_binop (MINUS_EXPR, TREE_TYPE (@1),
4176                             build_int_cst (TREE_TYPE (@1),
4177                                            element_precision (type)), @1); }))
4179 /* Turn (a OP c1) OP c2 into a OP (c1+c2).  */
4180 (for op (lrotate rrotate rshift lshift)
4181  (simplify
4182   (op (op @0 INTEGER_CST@1) INTEGER_CST@2)
4183   (with { unsigned int prec = element_precision (type); }
4184    (if (wi::ge_p (wi::to_wide (@1), 0, TYPE_SIGN (TREE_TYPE (@1)))
4185         && wi::lt_p (wi::to_wide (@1), prec, TYPE_SIGN (TREE_TYPE (@1)))
4186         && wi::ge_p (wi::to_wide (@2), 0, TYPE_SIGN (TREE_TYPE (@2)))
4187         && wi::lt_p (wi::to_wide (@2), prec, TYPE_SIGN (TREE_TYPE (@2))))
4188     (with { unsigned int low = (tree_to_uhwi (@1)
4189                                 + tree_to_uhwi (@2)); }
4190      /* Deal with a OP (c1 + c2) being undefined but (a OP c1) OP c2
4191         being well defined.  */
4192      (if (low >= prec)
4193       (if (op == LROTATE_EXPR || op == RROTATE_EXPR)
4194        (op @0 { build_int_cst (TREE_TYPE (@1), low % prec); })
4195        (if (TYPE_UNSIGNED (type) || op == LSHIFT_EXPR)
4196         { build_zero_cst (type); }
4197         (op @0 { build_int_cst (TREE_TYPE (@1), prec - 1); })))
4198       (op @0 { build_int_cst (TREE_TYPE (@1), low); })))))))
4201 /* Simplify (CST << x) & 1 to 0 if CST is even or to x == 0 if it is odd.  */
4202 (simplify
4203  (bit_and (lshift INTEGER_CST@1 @0) integer_onep)
4204   (if ((wi::to_wide (@1) & 1) != 0)
4205    (convert (eq:boolean_type_node @0 { build_zero_cst (TREE_TYPE (@0)); }))
4206    { build_zero_cst (type); }))
4208 /* Simplify ((C << x) & D) != 0 where C and D are power of two constants,
4209    either to false if D is smaller (unsigned comparison) than C, or to
4210    x == log2 (D) - log2 (C).  Similarly for right shifts.
4211    Note for `(1 >> x)`, the & 1 has been removed so matching that seperately. */
4212 (for cmp (ne eq)
4213      icmp (eq ne)
4214  (simplify
4215   (cmp (bit_and (lshift integer_pow2p@1 @0) integer_pow2p@2) integer_zerop)
4216    (with { int c1 = wi::clz (wi::to_wide (@1));
4217            int c2 = wi::clz (wi::to_wide (@2)); }
4218     (if (c1 < c2)
4219      { constant_boolean_node (cmp == NE_EXPR ? false : true, type); }
4220      (icmp @0 { build_int_cst (TREE_TYPE (@0), c1 - c2); }))))
4221  (simplify
4222   (cmp (bit_and (rshift integer_pow2p@1 @0) integer_pow2p@2) integer_zerop)
4223    (if (tree_int_cst_sgn (@1) > 0)
4224     (with { int c1 = wi::clz (wi::to_wide (@1));
4225             int c2 = wi::clz (wi::to_wide (@2)); }
4226      (if (c1 > c2)
4227       { constant_boolean_node (cmp == NE_EXPR ? false : true, type); }
4228       (icmp @0 { build_int_cst (TREE_TYPE (@0), c2 - c1); })))))
4229  /* `(1 >> X) != 0` -> `X == 0` */
4230  /* `(1 >> X) == 0` -> `X != 0` */
4231  (simplify
4232   (cmp (rshift integer_onep@1 @0) integer_zerop)
4233    (if (INTEGRAL_TYPE_P (TREE_TYPE (@1)))
4234     (icmp @0 { build_zero_cst (TREE_TYPE (@0)); }))))
4236 /* (CST1 << A) == CST2 -> A == ctz (CST2) - ctz (CST1)
4237    (CST1 << A) != CST2 -> A != ctz (CST2) - ctz (CST1)
4238    if CST2 != 0.  */
4239 (for cmp (ne eq)
4240  (simplify
4241   (cmp (lshift INTEGER_CST@0 @1) INTEGER_CST@2)
4242   (with { int cand = wi::ctz (wi::to_wide (@2)) - wi::ctz (wi::to_wide (@0)); }
4243    (if (cand < 0
4244         || (!integer_zerop (@2)
4245             && wi::lshift (wi::to_wide (@0), cand) != wi::to_wide (@2)))
4246     { constant_boolean_node (cmp == NE_EXPR, type); }
4247     (if (!integer_zerop (@2)
4248          && wi::lshift (wi::to_wide (@0), cand) == wi::to_wide (@2))
4249      (cmp @1 { build_int_cst (TREE_TYPE (@1), cand); }))))))
4251 /* Fold ((X << C1) & C2) cmp C3 into (X & (C2 >> C1)) cmp (C3 >> C1)
4252         ((X >> C1) & C2) cmp C3 into (X & (C2 << C1)) cmp (C3 << C1).  */
4253 (for cmp (ne eq)
4254  (simplify
4255   (cmp (bit_and:s (lshift:s @0 INTEGER_CST@1) INTEGER_CST@2) INTEGER_CST@3)
4256   (if (tree_fits_shwi_p (@1)
4257        && tree_to_shwi (@1) > 0
4258        && tree_to_shwi (@1) < TYPE_PRECISION (TREE_TYPE (@0)))
4259     (if (tree_to_shwi (@1) > wi::ctz (wi::to_wide (@3)))
4260       { constant_boolean_node (cmp == NE_EXPR, type); }
4261       (with { wide_int c1 = wi::to_wide (@1);
4262               wide_int c2 = wi::lrshift (wi::to_wide (@2), c1);
4263               wide_int c3 = wi::lrshift (wi::to_wide (@3), c1); }
4264         (cmp (bit_and @0 { wide_int_to_tree (TREE_TYPE (@0), c2); })
4265              { wide_int_to_tree (TREE_TYPE (@0), c3); })))))
4266  (simplify
4267   (cmp (bit_and:s (rshift:s @0 INTEGER_CST@1) INTEGER_CST@2) INTEGER_CST@3)
4268   (if (tree_fits_shwi_p (@1)
4269        && tree_to_shwi (@1) > 0
4270        && tree_to_shwi (@1) < TYPE_PRECISION (TREE_TYPE (@0)))
4271     (with { tree t0 = TREE_TYPE (@0);
4272             unsigned int prec = TYPE_PRECISION (t0);
4273             wide_int c1 = wi::to_wide (@1);
4274             wide_int c2 = wi::to_wide (@2);
4275             wide_int c3 = wi::to_wide (@3);
4276             wide_int sb = wi::set_bit_in_zero (prec - 1, prec); }
4277       (if ((c2 & c3) != c3)
4278         { constant_boolean_node (cmp == NE_EXPR, type); }
4279         (if (TYPE_UNSIGNED (t0))
4280           (if ((c3 & wi::arshift (sb, c1 - 1)) != 0)
4281             { constant_boolean_node (cmp == NE_EXPR, type); }
4282             (cmp (bit_and @0 { wide_int_to_tree (t0, c2 << c1); })
4283                  { wide_int_to_tree (t0, c3 << c1); }))
4284           (with { wide_int smask = wi::arshift (sb, c1); }
4285             (switch
4286               (if ((c2 & smask) == 0)
4287                 (cmp (bit_and @0 { wide_int_to_tree (t0, c2 << c1); })
4288                      { wide_int_to_tree (t0, c3 << c1); }))
4289               (if ((c3 & smask) == 0)
4290                 (cmp (bit_and @0 { wide_int_to_tree (t0, (c2 << c1) | sb); })
4291                      { wide_int_to_tree (t0, c3 << c1); }))
4292               (if ((c2 & smask) != (c3 & smask))
4293                 { constant_boolean_node (cmp == NE_EXPR, type); })
4294               (cmp (bit_and @0 { wide_int_to_tree (t0, (c2 << c1) | sb); })
4295                    { wide_int_to_tree (t0, (c3 << c1) | sb); })))))))))
4297 /* Fold (X << C1) & C2 into (X << C1) & (C2 | ((1 << C1) - 1))
4298         (X >> C1) & C2 into (X >> C1) & (C2 | ~((type) -1 >> C1))
4299    if the new mask might be further optimized.  */
4300 (for shift (lshift rshift)
4301  (simplify
4302   (bit_and (convert?:s@4 (shift:s@5 (convert1?@3 @0) INTEGER_CST@1))
4303            INTEGER_CST@2)
4304    (if (tree_nop_conversion_p (TREE_TYPE (@4), TREE_TYPE (@5))
4305         && TYPE_PRECISION (type) <= HOST_BITS_PER_WIDE_INT
4306         && tree_fits_uhwi_p (@1)
4307         && tree_to_uhwi (@1) > 0
4308         && tree_to_uhwi (@1) < TYPE_PRECISION (type))
4309     (with
4310      {
4311        unsigned int shiftc = tree_to_uhwi (@1);
4312        unsigned HOST_WIDE_INT mask = TREE_INT_CST_LOW (@2);
4313        unsigned HOST_WIDE_INT newmask, zerobits = 0;
4314        tree shift_type = TREE_TYPE (@3);
4315        unsigned int prec;
4317        if (shift == LSHIFT_EXPR)
4318          zerobits = ((HOST_WIDE_INT_1U << shiftc) - 1);
4319        else if (shift == RSHIFT_EXPR
4320                 && type_has_mode_precision_p (shift_type))
4321          {
4322            prec = TYPE_PRECISION (TREE_TYPE (@3));
4323            tree arg00 = @0;
4324            /* See if more bits can be proven as zero because of
4325               zero extension.  */
4326            if (@3 != @0
4327                && TYPE_UNSIGNED (TREE_TYPE (@0)))
4328              {
4329                tree inner_type = TREE_TYPE (@0);
4330                if (type_has_mode_precision_p (inner_type)
4331                    && TYPE_PRECISION (inner_type) < prec)
4332                  {
4333                    prec = TYPE_PRECISION (inner_type);
4334                    /* See if we can shorten the right shift.  */
4335                    if (shiftc < prec)
4336                      shift_type = inner_type;
4337                    /* Otherwise X >> C1 is all zeros, so we'll optimize
4338                       it into (X, 0) later on by making sure zerobits
4339                       is all ones.  */
4340                  }
4341              }
4342            zerobits = HOST_WIDE_INT_M1U;
4343            if (shiftc < prec)
4344              {
4345                zerobits >>= HOST_BITS_PER_WIDE_INT - shiftc;
4346                zerobits <<= prec - shiftc;
4347              }
4348            /* For arithmetic shift if sign bit could be set, zerobits
4349               can contain actually sign bits, so no transformation is
4350               possible, unless MASK masks them all away.  In that
4351               case the shift needs to be converted into logical shift.  */
4352            if (!TYPE_UNSIGNED (TREE_TYPE (@3))
4353                && prec == TYPE_PRECISION (TREE_TYPE (@3)))
4354              {
4355                if ((mask & zerobits) == 0)
4356                  shift_type = unsigned_type_for (TREE_TYPE (@3));
4357                else
4358                  zerobits = 0;
4359              }
4360          }
4361      }
4362      /* ((X << 16) & 0xff00) is (X, 0).  */
4363      (if ((mask & zerobits) == mask)
4364       { build_int_cst (type, 0); }
4365       (with { newmask = mask | zerobits; }
4366        (if (newmask != mask && (newmask & (newmask + 1)) == 0)
4367         (with
4368          {
4369            /* Only do the transformation if NEWMASK is some integer
4370               mode's mask.  */
4371            for (prec = BITS_PER_UNIT;
4372                 prec < HOST_BITS_PER_WIDE_INT; prec <<= 1)
4373              if (newmask == (HOST_WIDE_INT_1U << prec) - 1)
4374                break;
4375          }
4376          (if (prec < HOST_BITS_PER_WIDE_INT
4377               || newmask == HOST_WIDE_INT_M1U)
4378           (with
4379            { tree newmaskt = build_int_cst_type (TREE_TYPE (@2), newmask); }
4380            (if (!tree_int_cst_equal (newmaskt, @2))
4381             (if (shift_type != TREE_TYPE (@3))
4382              (bit_and (convert (shift:shift_type (convert @3) @1)) { newmaskt; })
4383              (bit_and @4 { newmaskt; })))))))))))))
4385 /* ((1 << n) & M) != 0  -> n == log2 (M) */
4386 (for cmp (ne eq)
4387        icmp (eq ne)
4388  (simplify
4389   (cmp
4390    (bit_and
4391     (nop_convert? (lshift integer_onep @0)) integer_pow2p@1) integer_zerop)
4392   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)))
4393    (icmp @0 { wide_int_to_tree (TREE_TYPE (@0),
4394                                 wi::exact_log2 (wi::to_wide (@1))); }))))
4396 /* Fold (X {&,^,|} C2) << C1 into (X << C1) {&,^,|} (C2 << C1)
4397    (X {&,^,|} C2) >> C1 into (X >> C1) & (C2 >> C1).  */
4398 (for shift (lshift rshift)
4399  (for bit_op (bit_and bit_xor bit_ior)
4400   (simplify
4401    (shift (convert?:s (bit_op:s @0 INTEGER_CST@2)) INTEGER_CST@1)
4402    (if (tree_nop_conversion_p (type, TREE_TYPE (@0)))
4403     (with { tree mask = int_const_binop (shift, fold_convert (type, @2), @1); }
4404      (if (mask)
4405       (bit_op (shift (convert @0) @1) { mask; })))))))
4407 /* ~(~X >> Y) -> X >> Y (for arithmetic shift).  */
4408 (simplify
4409  (bit_not (convert1?:s (rshift:s (convert2?@0 (bit_not @1)) @2)))
4410   (if (!TYPE_UNSIGNED (TREE_TYPE (@0))
4411        && (element_precision (TREE_TYPE (@0))
4412            <= element_precision (TREE_TYPE (@1))
4413            || !TYPE_UNSIGNED (TREE_TYPE (@1))))
4414    (with
4415     { tree shift_type = TREE_TYPE (@0); }
4416      (convert (rshift (convert:shift_type @1) @2)))))
4418 /* ~(~X >>r Y) -> X >>r Y
4419    ~(~X <<r Y) -> X <<r Y */
4420 (for rotate (lrotate rrotate)
4421  (simplify
4422   (bit_not (convert1?:s (rotate:s (convert2?@0 (bit_not @1)) @2)))
4423    (if ((element_precision (TREE_TYPE (@0))
4424          <= element_precision (TREE_TYPE (@1))
4425          || !TYPE_UNSIGNED (TREE_TYPE (@1)))
4426         && (element_precision (type) <= element_precision (TREE_TYPE (@0))
4427             || !TYPE_UNSIGNED (TREE_TYPE (@0))))
4428     (with
4429      { tree rotate_type = TREE_TYPE (@0); }
4430       (convert (rotate (convert:rotate_type @1) @2))))))
4432 (for cmp (eq ne)
4433  (for rotate (lrotate rrotate)
4434       invrot (rrotate lrotate)
4435   /* (X >>r Y) cmp (Z >>r Y) may simplify to X cmp Y. */
4436   (simplify
4437    (cmp (rotate @1 @0) (rotate @2 @0))
4438    (cmp @1 @2))
4439   /* (X >>r C1) cmp C2 may simplify to X cmp C3. */
4440   (simplify
4441    (cmp (rotate @0 INTEGER_CST@1) INTEGER_CST@2)
4442    (cmp @0 { const_binop (invrot, TREE_TYPE (@0), @2, @1); }))
4443   /* (X >>r Y) cmp C where C is 0 or ~0, may simplify to X cmp C.  */
4444   (simplify
4445    (cmp (rotate @0 @1) INTEGER_CST@2)
4446     (if (integer_zerop (@2) || integer_all_onesp (@2))
4447      (cmp @0 @2)))))
4449 /* Narrow a lshift by constant.  */
4450 (simplify
4451  (convert (lshift:s@0 @1 INTEGER_CST@2))
4452  (if (INTEGRAL_TYPE_P (type)
4453       && INTEGRAL_TYPE_P (TREE_TYPE (@0))
4454       && !integer_zerop (@2)
4455       && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0)))
4456   (if (TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (@0))
4457        || wi::ltu_p (wi::to_wide (@2), TYPE_PRECISION (type)))
4458    (lshift (convert @1) @2)
4459    (if (wi::ltu_p (wi::to_wide (@2), TYPE_PRECISION (TREE_TYPE (@0))))
4460     { build_zero_cst (type); }))))
4462 /* Simplifications of conversions.  */
4464 /* Basic strip-useless-type-conversions / strip_nops.  */
4465 (for cvt (convert view_convert float fix_trunc)
4466  (simplify
4467   (cvt @0)
4468   (if ((GIMPLE && useless_type_conversion_p (type, TREE_TYPE (@0)))
4469        || (GENERIC && type == TREE_TYPE (@0)))
4470    @0)))
4472 /* Contract view-conversions.  */
4473 (simplify
4474   (view_convert (view_convert @0))
4475   (view_convert @0))
4477 /* For integral conversions with the same precision or pointer
4478    conversions use a NOP_EXPR instead.  */
4479 (simplify
4480   (view_convert @0)
4481   (if ((INTEGRAL_TYPE_P (type) || POINTER_TYPE_P (type))
4482        && (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
4483        && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (@0)))
4484    (convert @0)))
4486 /* Strip inner integral conversions that do not change precision or size, or
4487    zero-extend while keeping the same size (for bool-to-char).  */
4488 (simplify
4489   (view_convert (convert@0 @1))
4490   (if ((INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
4491        && (INTEGRAL_TYPE_P (TREE_TYPE (@1)) || POINTER_TYPE_P (TREE_TYPE (@1)))
4492        && TYPE_SIZE (TREE_TYPE (@0)) == TYPE_SIZE (TREE_TYPE (@1))
4493        && (TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1))
4494            || (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (TREE_TYPE (@1))
4495                && TYPE_UNSIGNED (TREE_TYPE (@1)))))
4496    (view_convert @1)))
4498 /* Simplify a view-converted empty or single-element constructor.  */
4499 (simplify
4500   (view_convert CONSTRUCTOR@0)
4501   (with
4502    { tree ctor = (TREE_CODE (@0) == SSA_NAME
4503                   ? gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0)) : @0); }
4504    (switch
4505     (if (CONSTRUCTOR_NELTS (ctor) == 0)
4506      { build_zero_cst (type); })
4507     (if (CONSTRUCTOR_NELTS (ctor) == 1
4508          && VECTOR_TYPE_P (TREE_TYPE (ctor))
4509          && operand_equal_p (TYPE_SIZE (type),
4510                              TYPE_SIZE (TREE_TYPE
4511                                (CONSTRUCTOR_ELT (ctor, 0)->value))))
4512      (view_convert { CONSTRUCTOR_ELT (ctor, 0)->value; })))))
4514 /* Re-association barriers around constants and other re-association
4515    barriers can be removed.  */
4516 (simplify
4517  (paren CONSTANT_CLASS_P@0)
4518  @0)
4519 (simplify
4520  (paren (paren@1 @0))
4521  @1)
4523 /* Handle cases of two conversions in a row.  */
4524 (for ocvt (convert float fix_trunc)
4525  (for icvt (convert float)
4526   (simplify
4527    (ocvt (icvt@1 @0))
4528    (with
4529     {
4530       tree inside_type = TREE_TYPE (@0);
4531       tree inter_type = TREE_TYPE (@1);
4532       int inside_int = INTEGRAL_TYPE_P (inside_type);
4533       int inside_ptr = POINTER_TYPE_P (inside_type);
4534       int inside_float = FLOAT_TYPE_P (inside_type);
4535       int inside_vec = VECTOR_TYPE_P (inside_type);
4536       unsigned int inside_prec = element_precision (inside_type);
4537       int inside_unsignedp = TYPE_UNSIGNED (inside_type);
4538       int inter_int = INTEGRAL_TYPE_P (inter_type);
4539       int inter_ptr = POINTER_TYPE_P (inter_type);
4540       int inter_float = FLOAT_TYPE_P (inter_type);
4541       int inter_vec = VECTOR_TYPE_P (inter_type);
4542       unsigned int inter_prec = element_precision (inter_type);
4543       int inter_unsignedp = TYPE_UNSIGNED (inter_type);
4544       int final_int = INTEGRAL_TYPE_P (type);
4545       int final_ptr = POINTER_TYPE_P (type);
4546       int final_float = FLOAT_TYPE_P (type);
4547       int final_vec = VECTOR_TYPE_P (type);
4548       unsigned int final_prec = element_precision (type);
4549       int final_unsignedp = TYPE_UNSIGNED (type);
4550     }
4551    (switch
4552     /* In addition to the cases of two conversions in a row
4553        handled below, if we are converting something to its own
4554        type via an object of identical or wider precision, neither
4555        conversion is needed.  */
4556     (if (((GIMPLE && useless_type_conversion_p (type, inside_type))
4557           || (GENERIC
4558               && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (inside_type)))
4559          && (((inter_int || inter_ptr) && final_int)
4560              || (inter_float && final_float))
4561          && inter_prec >= final_prec)
4562      (ocvt @0))
4564     /* Likewise, if the intermediate and initial types are either both
4565        float or both integer, we don't need the middle conversion if the
4566        former is wider than the latter and doesn't change the signedness
4567        (for integers).  Avoid this if the final type is a pointer since
4568        then we sometimes need the middle conversion.  */
4569     (if (((inter_int && inside_int) || (inter_float && inside_float))
4570          && (final_int || final_float)
4571          && inter_prec >= inside_prec
4572          && (inter_float || inter_unsignedp == inside_unsignedp))
4573      (ocvt @0))
4575     /* If we have a sign-extension of a zero-extended value, we can
4576        replace that by a single zero-extension.  Likewise if the
4577        final conversion does not change precision we can drop the
4578        intermediate conversion.  */
4579     (if (inside_int && inter_int && final_int
4580          && ((inside_prec < inter_prec && inter_prec < final_prec
4581               && inside_unsignedp && !inter_unsignedp)
4582              || final_prec == inter_prec))
4583      (ocvt @0))
4585     /* Two conversions in a row are not needed unless:
4586         - some conversion is floating-point (overstrict for now), or
4587         - some conversion is a vector (overstrict for now), or
4588         - the intermediate type is narrower than both initial and
4589           final, or
4590         - the intermediate type and innermost type differ in signedness,
4591           and the outermost type is wider than the intermediate, or
4592         - the initial type is a pointer type and the precisions of the
4593           intermediate and final types differ, or
4594         - the final type is a pointer type and the precisions of the
4595           initial and intermediate types differ.  */
4596     (if (! inside_float && ! inter_float && ! final_float
4597          && ! inside_vec && ! inter_vec && ! final_vec
4598          && (inter_prec >= inside_prec || inter_prec >= final_prec)
4599          && ! (inside_int && inter_int
4600                && inter_unsignedp != inside_unsignedp
4601                && inter_prec < final_prec)
4602          && ((inter_unsignedp && inter_prec > inside_prec)
4603              == (final_unsignedp && final_prec > inter_prec))
4604          && ! (inside_ptr && inter_prec != final_prec)
4605          && ! (final_ptr && inside_prec != inter_prec))
4606      (ocvt @0))
4608    /* `(outer:M)(inter:N) a:O`
4609       can be converted to `(outer:M) a`
4610       if M <= O && N >= O. No matter what signedness of the casts,
4611       as the final is either a truncation from the original or just
4612       a sign change of the type. */
4613    (if (inside_int && inter_int && final_int
4614         && final_prec <= inside_prec
4615         && inter_prec >= inside_prec)
4616     (convert @0))
4618     /* A truncation to an unsigned type (a zero-extension) should be
4619        canonicalized as bitwise and of a mask.  */
4620     (if (GIMPLE /* PR70366: doing this in GENERIC breaks -Wconversion.  */
4621          && final_int && inter_int && inside_int
4622          && final_prec == inside_prec
4623          && final_prec > inter_prec
4624          && inter_unsignedp)
4625      (convert (bit_and @0 { wide_int_to_tree
4626                               (inside_type,
4627                                wi::mask (inter_prec, false,
4628                                          TYPE_PRECISION (inside_type))); })))
4630     /* If we are converting an integer to a floating-point that can
4631        represent it exactly and back to an integer, we can skip the
4632        floating-point conversion.  */
4633     (if (GIMPLE /* PR66211 */
4634          && inside_int && inter_float && final_int &&
4635          (unsigned) significand_size (TYPE_MODE (inter_type))
4636          >= inside_prec - !inside_unsignedp)
4637      (convert @0)))))))
4639 /* (float_type)(integer_type) x -> trunc (x) if the type of x matches
4640    float_type.  Only do the transformation if we do not need to preserve
4641    trapping behaviour, so require !flag_trapping_math. */
4642 #if GIMPLE
4643 (simplify
4644    (float (fix_trunc @0))
4645    (if (!flag_trapping_math
4646         && types_match (type, TREE_TYPE (@0))
4647         && direct_internal_fn_supported_p (IFN_TRUNC, type,
4648                                           OPTIMIZE_FOR_BOTH))
4649       (IFN_TRUNC @0)))
4650 #endif
4652 /* If we have a narrowing conversion to an integral type that is fed by a
4653    BIT_AND_EXPR, we might be able to remove the BIT_AND_EXPR if it merely
4654    masks off bits outside the final type (and nothing else).  */
4655 (simplify
4656   (convert (bit_and @0 INTEGER_CST@1))
4657   (if (INTEGRAL_TYPE_P (type)
4658        && INTEGRAL_TYPE_P (TREE_TYPE (@0))
4659        && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))
4660        && operand_equal_p (@1, build_low_bits_mask (TREE_TYPE (@1),
4661                                                     TYPE_PRECISION (type)), 0))
4662    (convert @0)))
4665 /* (X /[ex] A) * A -> X.  */
4666 (simplify
4667   (mult (convert1? (exact_div @0 @@1)) (convert2? @1))
4668   (convert @0))
4670 /* Simplify (A / B) * B + (A % B) -> A.  */
4671 (for div (trunc_div ceil_div floor_div round_div)
4672      mod (trunc_mod ceil_mod floor_mod round_mod)
4673   (simplify
4674    (plus:c (mult:c (div @0 @1) @1) (mod @0 @1))
4675    @0))
4677 /* x / y * y == x -> x % y == 0.  */
4678 (simplify
4679   (eq:c (mult:c (trunc_div:s @0 @1) @1) @0)
4680   (if (TREE_CODE (TREE_TYPE (@0)) != COMPLEX_TYPE)
4681     (eq (trunc_mod @0 @1) { build_zero_cst (TREE_TYPE (@0)); })))
4683 /* ((X /[ex] A) +- B) * A  -->  X +- A * B.  */
4684 (for op (plus minus)
4685  (simplify
4686   (mult (convert1? (op (convert2? (exact_div @0 INTEGER_CST@@1)) INTEGER_CST@2)) @1)
4687   (if (tree_nop_conversion_p (type, TREE_TYPE (@2))
4688        && tree_nop_conversion_p (TREE_TYPE (@0), TREE_TYPE (@2)))
4689    (with
4690      {
4691        wi::overflow_type overflow;
4692        wide_int mul = wi::mul (wi::to_wide (@1), wi::to_wide (@2),
4693                                TYPE_SIGN (type), &overflow);
4694      }
4695      (if (types_match (type, TREE_TYPE (@2))
4696          && types_match (TREE_TYPE (@0), TREE_TYPE (@2)) && !overflow)
4697       (op @0 { wide_int_to_tree (type, mul); })
4698       (with { tree utype = unsigned_type_for (type); }
4699        (convert (op (convert:utype @0)
4700                     (mult (convert:utype @1) (convert:utype @2))))))))))
4702 /* Canonicalization of binary operations.  */
4704 /* Convert X + -C into X - C.  */
4705 (simplify
4706  (plus @0 REAL_CST@1)
4707  (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
4708   (with { tree tem = const_unop (NEGATE_EXPR, type, @1); }
4709    (if (!TREE_OVERFLOW (tem) || !flag_trapping_math)
4710     (minus @0 { tem; })))))
4712 /* Convert x+x into x*2.  */
4713 (simplify
4714  (plus @0 @0)
4715  (if (SCALAR_FLOAT_TYPE_P (type))
4716   (mult @0 { build_real (type, dconst2); })
4717   (if (INTEGRAL_TYPE_P (type))
4718    (mult @0 { build_int_cst (type, 2); }))))
4720 /* 0 - X  ->  -X.  */
4721 (simplify
4722  (minus integer_zerop @1)
4723  (negate @1))
4724 (simplify
4725  (pointer_diff integer_zerop @1)
4726  (negate (convert @1)))
4728 /* (ARG0 - ARG1) is the same as (-ARG1 + ARG0).  So check whether
4729    ARG0 is zero and X + ARG0 reduces to X, since that would mean
4730    (-ARG1 + ARG0) reduces to -ARG1.  */
4731 (simplify
4732  (minus real_zerop@0 @1)
4733  (if (fold_real_zero_addition_p (type, @1, @0, 0))
4734   (negate @1)))
4736 /* Transform x * -1 into -x.  */
4737 (simplify
4738  (mult @0 integer_minus_onep)
4739  (negate @0))
4741 /* Reassociate (X * CST) * Y to (X * Y) * CST.  This does not introduce
4742    signed overflow for CST != 0 && CST != -1.  */
4743 (simplify
4744  (mult:c (mult:s@3 @0 INTEGER_CST@1) @2)
4745  (if (TREE_CODE (@2) != INTEGER_CST
4746       && single_use (@3)
4747       && !integer_zerop (@1) && !integer_minus_onep (@1))
4748   (mult (mult @0 @2) @1)))
4750 /* True if we can easily extract the real and imaginary parts of a complex
4751    number.  */
4752 (match compositional_complex
4753  (convert? (complex @0 @1)))
4755 /* COMPLEX_EXPR and REALPART/IMAGPART_EXPR cancellations.  */
4756 (simplify
4757  (complex (realpart @0) (imagpart @0))
4758  @0)
4759 (simplify
4760  (realpart (complex @0 @1))
4761  @0)
4762 (simplify
4763  (imagpart (complex @0 @1))
4764  @1)
4766 /* Sometimes we only care about half of a complex expression.  */
4767 (simplify
4768  (realpart (convert?:s (conj:s @0)))
4769  (convert (realpart @0)))
4770 (simplify
4771  (imagpart (convert?:s (conj:s @0)))
4772  (convert (negate (imagpart @0))))
4773 (for part (realpart imagpart)
4774  (for op (plus minus)
4775   (simplify
4776    (part (convert?:s@2 (op:s @0 @1)))
4777    (convert (op (part @0) (part @1))))))
4778 (simplify
4779  (realpart (convert?:s (CEXPI:s @0)))
4780  (convert (COS @0)))
4781 (simplify
4782  (imagpart (convert?:s (CEXPI:s @0)))
4783  (convert (SIN @0)))
4785 /* conj(conj(x)) -> x  */
4786 (simplify
4787  (conj (convert? (conj @0)))
4788  (if (tree_nop_conversion_p (TREE_TYPE (@0), type))
4789   (convert @0)))
4791 /* conj({x,y}) -> {x,-y}  */
4792 (simplify
4793  (conj (convert?:s (complex:s @0 @1)))
4794  (with { tree itype = TREE_TYPE (type); }
4795   (complex (convert:itype @0) (negate (convert:itype @1)))))
4797 /* BSWAP simplifications, transforms checked by gcc.dg/builtin-bswap-8.c.  */
4798 (for bswap (BSWAP)
4799  (simplify
4800   (bswap (bswap @0))
4801   @0)
4802  (simplify
4803   (bswap (bit_not (bswap @0)))
4804   (bit_not @0))
4805  (for bitop (bit_xor bit_ior bit_and)
4806   (simplify
4807    (bswap (bitop:c (bswap @0) @1))
4808    (bitop @0 (bswap @1))))
4809  (for cmp (eq ne)
4810   (simplify
4811    (cmp (bswap@2 @0) (bswap @1))
4812    (with { tree ctype = TREE_TYPE (@2); }
4813     (cmp (convert:ctype @0) (convert:ctype @1))))
4814   (simplify
4815    (cmp (bswap @0) INTEGER_CST@1)
4816    (with { tree ctype = TREE_TYPE (@1); }
4817     (cmp (convert:ctype @0) (bswap! @1)))))
4818  /* (bswap(x) >> C1) & C2 can sometimes be simplified to (x >> C3) & C2.  */
4819  (simplify
4820   (bit_and (convert1? (rshift@0 (convert2? (bswap@4 @1)) INTEGER_CST@2))
4821            INTEGER_CST@3)
4822    (if (BITS_PER_UNIT == 8
4823         && tree_fits_uhwi_p (@2)
4824         && tree_fits_uhwi_p (@3))
4825     (with
4826      {
4827       unsigned HOST_WIDE_INT prec = TYPE_PRECISION (TREE_TYPE (@4));
4828       unsigned HOST_WIDE_INT bits = tree_to_uhwi (@2);
4829       unsigned HOST_WIDE_INT mask = tree_to_uhwi (@3);
4830       unsigned HOST_WIDE_INT lo = bits & 7;
4831       unsigned HOST_WIDE_INT hi = bits - lo;
4832      }
4833      (if (bits < prec
4834           && mask < (256u>>lo)
4835           && bits < TYPE_PRECISION (TREE_TYPE(@0)))
4836       (with { unsigned HOST_WIDE_INT ns = (prec - (hi + 8)) + lo; }
4837        (if (ns == 0)
4838         (bit_and (convert @1) @3)
4839         (with
4840          {
4841           tree utype = unsigned_type_for (TREE_TYPE (@1));
4842           tree nst = build_int_cst (integer_type_node, ns);
4843          }
4844          (bit_and (convert (rshift:utype (convert:utype @1) {nst;})) @3))))))))
4845  /* bswap(x) >> C1 can sometimes be simplified to (T)x >> C2.  */
4846  (simplify
4847   (rshift (convert? (bswap@2 @0)) INTEGER_CST@1)
4848    (if (BITS_PER_UNIT == 8
4849         && CHAR_TYPE_SIZE == 8
4850         && tree_fits_uhwi_p (@1))
4851     (with
4852      {
4853       unsigned HOST_WIDE_INT prec = TYPE_PRECISION (TREE_TYPE (@2));
4854       unsigned HOST_WIDE_INT bits = tree_to_uhwi (@1);
4855       /* If the bswap was extended before the original shift, this
4856          byte (shift) has the sign of the extension, not the sign of
4857          the original shift.  */
4858       tree st = TYPE_PRECISION (type) > prec ? TREE_TYPE (@2) : type;
4859      }
4860      /* Special case: logical right shift of sign-extended bswap.
4861         (unsigned)(short)bswap16(x)>>12 is (unsigned)((short)x<<8)>>12. */
4862      (if (TYPE_PRECISION (type) > prec
4863           && !TYPE_UNSIGNED (TREE_TYPE (@2))
4864           && TYPE_UNSIGNED (type)
4865           && bits < prec && bits + 8 >= prec)
4866       (with { tree nst = build_int_cst (integer_type_node, prec - 8); }
4867        (rshift (convert (lshift:st (convert:st @0) {nst;})) @1))
4868       (if (bits + 8 == prec)
4869        (if (TYPE_UNSIGNED (st))
4870         (convert (convert:unsigned_char_type_node @0))
4871         (convert (convert:signed_char_type_node @0)))
4872        (if (bits < prec && bits + 8 > prec)
4873         (with 
4874          {
4875           tree nst = build_int_cst (integer_type_node, bits & 7);
4876           tree bt = TYPE_UNSIGNED (st) ? unsigned_char_type_node
4877                                        : signed_char_type_node;
4878          }
4879          (convert (rshift:bt (convert:bt @0) {nst;})))))))))
4880  /* bswap(x) & C1 can sometimes be simplified to (x >> C2) & C1.  */
4881  (simplify
4882   (bit_and (convert? (bswap@2 @0)) INTEGER_CST@1)
4883    (if (BITS_PER_UNIT == 8
4884         && tree_fits_uhwi_p (@1)
4885         && tree_to_uhwi (@1) < 256)
4886     (with
4887      {
4888       unsigned HOST_WIDE_INT prec = TYPE_PRECISION (TREE_TYPE (@2));
4889       tree utype = unsigned_type_for (TREE_TYPE (@0));
4890       tree nst = build_int_cst (integer_type_node, prec - 8);
4891      }
4892      (bit_and (convert (rshift:utype (convert:utype @0) {nst;})) @1)))))
4895 /* Combine COND_EXPRs and VEC_COND_EXPRs.  */
4897 /* Simplify constant conditions.
4898    Only optimize constant conditions when the selected branch
4899    has the same type as the COND_EXPR.  This avoids optimizing
4900    away "c ? x : throw", where the throw has a void type.
4901    Note that we cannot throw away the fold-const.cc variant nor
4902    this one as we depend on doing this transform before possibly
4903    A ? B : B -> B triggers and the fold-const.cc one can optimize
4904    0 ? A : B to B even if A has side-effects.  Something
4905    genmatch cannot handle.  */
4906 (simplify
4907  (cond INTEGER_CST@0 @1 @2)
4908  (if (integer_zerop (@0))
4909   (if (!VOID_TYPE_P (TREE_TYPE (@2)) || VOID_TYPE_P (type))
4910    @2)
4911   (if (!VOID_TYPE_P (TREE_TYPE (@1)) || VOID_TYPE_P (type))
4912    @1)))
4913 (simplify
4914  (vec_cond VECTOR_CST@0 @1 @2)
4915  (if (integer_all_onesp (@0))
4916   @1
4917   (if (integer_zerop (@0))
4918    @2)))
4920 /* Sink unary operations to branches, but only if we do fold both.  */
4921 (for op (negate bit_not abs absu)
4922  (simplify
4923   (op (vec_cond:s @0 @1 @2))
4924   (vec_cond @0 (op! @1) (op! @2))))
4926 /* Sink unary conversions to branches, but only if we do fold both
4927    and the target's truth type is the same as we already have.  */
4928 (simplify
4929  (convert (vec_cond:s @0 @1 @2))
4930  (if (VECTOR_TYPE_P (type)
4931       && types_match (TREE_TYPE (@0), truth_type_for (type)))
4932   (vec_cond @0 (convert! @1) (convert! @2))))
4934 /* Likewise for view_convert of nop_conversions. */
4935 (simplify
4936  (view_convert (vec_cond:s @0 @1 @2))
4937  (if (VECTOR_TYPE_P (type) && VECTOR_TYPE_P (TREE_TYPE (@1))
4938       && known_eq (TYPE_VECTOR_SUBPARTS (type),
4939                    TYPE_VECTOR_SUBPARTS (TREE_TYPE (@1)))
4940       && tree_nop_conversion_p (TREE_TYPE (type), TREE_TYPE (TREE_TYPE (@1))))
4941   (vec_cond @0 (view_convert! @1) (view_convert! @2))))
4943 /* Sink binary operation to branches, but only if we can fold it.  */
4944 (for op (tcc_comparison plus minus mult bit_and bit_ior bit_xor
4945          lshift rshift rdiv trunc_div ceil_div floor_div round_div
4946          trunc_mod ceil_mod floor_mod round_mod min max)
4947 /* (c ? a : b) op (c ? d : e)  -->  c ? (a op d) : (b op e) */
4948  (simplify
4949   (op (vec_cond:s @0 @1 @2) (vec_cond:s @0 @3 @4))
4950   (vec_cond @0 (op! @1 @3) (op! @2 @4)))
4952 /* (c ? a : b) op d  -->  c ? (a op d) : (b op d) */
4953  (simplify
4954   (op (vec_cond:s @0 @1 @2) @3)
4955   (vec_cond @0 (op! @1 @3) (op! @2 @3)))
4956  (simplify
4957   (op @3 (vec_cond:s @0 @1 @2))
4958   (vec_cond @0 (op! @3 @1) (op! @3 @2))))
4960 #if GIMPLE
4961 (match (nop_atomic_bit_test_and_p @0 @1 @4)
4962  (bit_and (convert?@4 (ATOMIC_FETCH_OR_XOR_N @2 INTEGER_CST@0 @3))
4963            INTEGER_CST@1)
4964  (with {
4965          int ibit = tree_log2 (@0);
4966          int ibit2 = tree_log2 (@1);
4967        }
4968   (if (ibit == ibit2
4969       && ibit >= 0
4970       && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))))))
4972 (match (nop_atomic_bit_test_and_p @0 @1 @3)
4973  (bit_and (convert?@3 (SYNC_FETCH_OR_XOR_N @2 INTEGER_CST@0))
4974           INTEGER_CST@1)
4975  (with {
4976          int ibit = tree_log2 (@0);
4977          int ibit2 = tree_log2 (@1);
4978        }
4979   (if (ibit == ibit2
4980       && ibit >= 0
4981       && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))))))
4983 (match (nop_atomic_bit_test_and_p @0 @0 @4)
4984  (bit_and:c
4985   (convert1?@4
4986    (ATOMIC_FETCH_OR_XOR_N @2 (nop_convert? (lshift@0 integer_onep@5 @6)) @3))
4987   (convert2? @0))
4988  (if (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0)))))
4990 (match (nop_atomic_bit_test_and_p @0 @0 @4)
4991  (bit_and:c
4992   (convert1?@4
4993    (SYNC_FETCH_OR_XOR_N @2 (nop_convert? (lshift@0 integer_onep@3 @5))))
4994   (convert2? @0))
4995  (if (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0)))))
4997 (match (nop_atomic_bit_test_and_p @0 @1 @3)
4998  (bit_and@4 (convert?@3 (ATOMIC_FETCH_AND_N @2 INTEGER_CST@0 @5))
4999             INTEGER_CST@1)
5000  (with {
5001          int ibit = wi::exact_log2 (wi::zext (wi::bit_not (wi::to_wide (@0)),
5002                                               TYPE_PRECISION(type)));
5003          int ibit2 = tree_log2 (@1);
5004        }
5005   (if (ibit == ibit2
5006       && ibit >= 0
5007       && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))))))
5009 (match (nop_atomic_bit_test_and_p @0 @1 @3)
5010  (bit_and@4
5011   (convert?@3 (SYNC_FETCH_AND_AND_N @2 INTEGER_CST@0))
5012   INTEGER_CST@1)
5013  (with {
5014          int ibit = wi::exact_log2 (wi::zext (wi::bit_not (wi::to_wide (@0)),
5015                                               TYPE_PRECISION(type)));
5016          int ibit2 = tree_log2 (@1);
5017        }
5018   (if (ibit == ibit2
5019       && ibit >= 0
5020       && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@0))))))
5022 (match (nop_atomic_bit_test_and_p @4 @0 @3)
5023  (bit_and:c
5024   (convert1?@3
5025    (ATOMIC_FETCH_AND_N @2 (nop_convert?@4 (bit_not (lshift@0 integer_onep@6 @7))) @5))
5026   (convert2? @0))
5027  (if (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@4)))))
5029 (match (nop_atomic_bit_test_and_p @4 @0 @3)
5030  (bit_and:c
5031   (convert1?@3
5032    (SYNC_FETCH_AND_AND_N @2 (nop_convert?@4 (bit_not (lshift@0 integer_onep@6 @7)))))
5033   (convert2? @0))
5034   (if (TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (@4)))))
5036 #endif
5038 /* (v ? w : 0) ? a : b is just (v & w) ? a : b
5039    Currently disabled after pass lvec because ARM understands
5040    VEC_COND_EXPR<v==w,-1,0> but not a plain v==w fed to BIT_IOR_EXPR.  */
5041 (simplify
5042  (vec_cond (vec_cond:s @0 @3 integer_zerop) @1 @2)
5043  (if (optimize_vectors_before_lowering_p () && types_match (@0, @3))
5044   (vec_cond (bit_and @0 @3) @1 @2)))
5045 (simplify
5046  (vec_cond (vec_cond:s @0 integer_all_onesp @3) @1 @2)
5047  (if (optimize_vectors_before_lowering_p () && types_match (@0, @3))
5048   (vec_cond (bit_ior @0 @3) @1 @2)))
5049 (simplify
5050  (vec_cond (vec_cond:s @0 integer_zerop @3) @1 @2)
5051  (if (optimize_vectors_before_lowering_p () && types_match (@0, @3))
5052   (vec_cond (bit_ior @0 (bit_not @3)) @2 @1)))
5053 (simplify
5054  (vec_cond (vec_cond:s @0 @3 integer_all_onesp) @1 @2)
5055  (if (optimize_vectors_before_lowering_p () && types_match (@0, @3))
5056   (vec_cond (bit_and @0 (bit_not @3)) @2 @1)))
5058 /* c1 ? c2 ? a : b : b  -->  (c1 & c2) ? a : b  */
5059 (simplify
5060  (vec_cond @0 (vec_cond:s @1 @2 @3) @3)
5061  (if (optimize_vectors_before_lowering_p () && types_match (@0, @1))
5062   (vec_cond (bit_and @0 @1) @2 @3)))
5063 (simplify
5064  (vec_cond @0 @2 (vec_cond:s @1 @2 @3))
5065  (if (optimize_vectors_before_lowering_p () && types_match (@0, @1))
5066   (vec_cond (bit_ior @0 @1) @2 @3)))
5067 (simplify
5068  (vec_cond @0 (vec_cond:s @1 @2 @3) @2)
5069  (if (optimize_vectors_before_lowering_p () && types_match (@0, @1))
5070   (vec_cond (bit_ior (bit_not @0) @1) @2 @3)))
5071 (simplify
5072  (vec_cond @0 @3 (vec_cond:s @1 @2 @3))
5073  (if (optimize_vectors_before_lowering_p () && types_match (@0, @1))
5074   (vec_cond (bit_and (bit_not @0) @1) @2 @3)))
5076 /* Canonicalize mask ? { 0, ... } : { -1, ...} to ~mask if the mask
5077    types are compatible.  */
5078 (simplify
5079  (vec_cond @0 VECTOR_CST@1 VECTOR_CST@2)
5080  (if (VECTOR_BOOLEAN_TYPE_P (type)
5081       && types_match (type, TREE_TYPE (@0)))
5082   (if (integer_zerop (@1) && integer_all_onesp (@2))
5083    (bit_not @0)
5084    (if (integer_all_onesp (@1) && integer_zerop (@2))
5085     @0))))
5087 /* A few simplifications of "a ? CST1 : CST2". */
5088 /* NOTE: Only do this on gimple as the if-chain-to-switch
5089    optimization depends on the gimple to have if statements in it. */
5090 #if GIMPLE
5091 (simplify
5092  (cond @0 INTEGER_CST@1 INTEGER_CST@2)
5093  (switch
5094   (if (integer_zerop (@2))
5095    (switch
5096     /* a ? 1 : 0 -> a if 0 and 1 are integral types. */
5097     (if (integer_onep (@1))
5098      (convert (convert:boolean_type_node @0)))
5099     /* a ? powerof2cst : 0 -> a << (log2(powerof2cst)) */
5100     (if (INTEGRAL_TYPE_P (type) && integer_pow2p (@1))
5101      (with {
5102        tree shift = build_int_cst (integer_type_node, tree_log2 (@1));
5103       }
5104       (lshift (convert (convert:boolean_type_node @0)) { shift; })))
5105     /* a ? -1 : 0 -> -a.  No need to check the TYPE_PRECISION not being 1
5106        here as the powerof2cst case above will handle that case correctly.  */
5107     (if (INTEGRAL_TYPE_P (type) && integer_all_onesp (@1))
5108      (negate (convert:type (convert:boolean_type_node @0))))))
5109   (if (integer_zerop (@1))
5110    (switch
5111     /* a ? 0 : 1 -> !a. */
5112     (if (integer_onep (@2))
5113      (convert (bit_xor (convert:boolean_type_node @0) { boolean_true_node; })))
5114     /* a ? powerof2cst : 0 -> (!a) << (log2(powerof2cst)) */
5115     (if (INTEGRAL_TYPE_P (type) && integer_pow2p (@2))
5116      (with {
5117        tree shift = build_int_cst (integer_type_node, tree_log2 (@2));
5118       }
5119       (lshift (convert (bit_xor (convert:boolean_type_node @0)
5120                                 { boolean_true_node; })) { shift; })))
5121     /* a ? -1 : 0 -> -(!a).  No need to check the TYPE_PRECISION not being 1
5122        here as the powerof2cst case above will handle that case correctly.  */
5123     (if (INTEGRAL_TYPE_P (type) && integer_all_onesp (@2))
5124      (negate (convert:type (bit_xor (convert:boolean_type_node @0)
5125                                     { boolean_true_node; }))))))))
5127 /* (a > 1) ? 0 : (cast)a is the same as (cast)(a == 1)
5128    for unsigned types. */
5129 (simplify
5130  (cond (gt @0 integer_onep@1) integer_zerop (convert? @2))
5131  (if (TYPE_UNSIGNED (TREE_TYPE (@0))
5132       && bitwise_equal_p (@0, @2))
5133   (convert (eq @0 @1))
5137 /* (a <= 1) & (cast)a is the same as (cast)(a == 1)
5138    for unsigned types. */
5139 (simplify
5140  (bit_and:c (convert1? (le @0 integer_onep@1)) (convert2? @2))
5141  (if (TYPE_UNSIGNED (TREE_TYPE (@0))
5142       && bitwise_equal_p (@0, @2))
5143   (convert (eq @0 @1))
5147 /* `(a == CST) & a` can be simplified to `0` or `(a == CST)` depending
5148    on the first bit of the CST.  */
5149 (simplify
5150  (bit_and:c (convert@2 (eq @0 INTEGER_CST@1)) (convert? @0))
5151  (if ((wi::to_wide (@1) & 1) != 0)
5152   @2
5153   { build_zero_cst (type); }))
5155 /* Optimize
5156    # x_5 in range [cst1, cst2] where cst2 = cst1 + 1
5157    x_5 ? cstN ? cst4 : cst3
5158    # op is == or != and N is 1 or 2
5159    to r_6 = x_5 + (min (cst3, cst4) - cst1) or
5160    r_6 = (min (cst3, cst4) + cst1) - x_5 depending on op, N and which
5161    of cst3 and cst4 is smaller.
5162    This was originally done by two_value_replacement in phiopt (PR 88676).  */
5163 (for eqne (ne eq)
5164  (simplify
5165   (cond (eqne SSA_NAME@0 INTEGER_CST@1) INTEGER_CST@2 INTEGER_CST@3)
5166   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
5167        && INTEGRAL_TYPE_P (type)
5168        && (wi::to_widest (@2) + 1 == wi::to_widest (@3)
5169            || wi::to_widest (@2) == wi::to_widest (@3) + 1))
5170    (with {
5171      value_range r;
5172      get_range_query (cfun)->range_of_expr (r, @0);
5173      if (r.undefined_p ())
5174        r.set_varying (TREE_TYPE (@0));
5176      wide_int min = r.lower_bound ();
5177      wide_int max = r.upper_bound ();
5178     }
5179     (if (min + 1 == max
5180          && (wi::to_wide (@1) == min
5181              || wi::to_wide (@1) == max))
5182      (with {
5183        tree arg0 = @2, arg1 = @3;
5184        tree type1;
5185        if ((eqne == EQ_EXPR) ^ (wi::to_wide (@1) == min))
5186          std::swap (arg0, arg1);
5187        if (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type))
5188          type1 = TREE_TYPE (@0);
5189        else
5190          type1 = type;
5191        auto prec = TYPE_PRECISION (type1);
5192        auto unsign = TYPE_UNSIGNED (type1);
5193        type1 = build_nonstandard_integer_type (prec, unsign);
5194        min = wide_int::from (min, prec,
5195                              TYPE_SIGN (TREE_TYPE (@0)));
5196        wide_int a = wide_int::from (wi::to_wide (arg0), prec,
5197                                     TYPE_SIGN (type));
5198        enum tree_code code;
5199        wi::overflow_type ovf;
5200        if (tree_int_cst_lt (arg0, arg1))
5201          {
5202            code = PLUS_EXPR;
5203            a -= min;
5204            if (!unsign)
5205              {
5206                /* lhs is known to be in range [min, min+1] and we want to add a
5207                   to it.  Check if that operation can overflow for those 2 values
5208                   and if yes, force unsigned type.  */
5209                wi::add (min + (wi::neg_p (a) ? 0 : 1), a, SIGNED, &ovf);
5210                if (ovf)
5211                  type1 = unsigned_type_for (type1);
5212              }
5213          }
5214        else
5215          {
5216            code = MINUS_EXPR;
5217            a += min;
5218            if (!unsign)
5219              {
5220                /* lhs is known to be in range [min, min+1] and we want to subtract
5221                   it from a.  Check if that operation can overflow for those 2
5222                   values and if yes, force unsigned type.  */
5223                wi::sub (a, min + (wi::neg_p (min) ? 0 : 1), SIGNED, &ovf);
5224                if (ovf)
5225                 type1 = unsigned_type_for (type1);
5226              }
5227          }
5228        tree arg = wide_int_to_tree (type1, a);
5229       }
5230       (if (code == PLUS_EXPR)
5231        (convert (plus (convert:type1 @0) { arg; }))
5232        (convert (minus { arg; } (convert:type1 @0)))
5233       )
5234      )
5235     )
5236    )
5237   )
5240 #endif
5242 (simplify
5243  (convert (cond@0 @1 INTEGER_CST@2 INTEGER_CST@3))
5244  (if (INTEGRAL_TYPE_P (type)
5245       && INTEGRAL_TYPE_P (TREE_TYPE (@0)))
5246   (cond @1 (convert @2) (convert @3))))
5248 /* Simplification moved from fold_cond_expr_with_comparison.  It may also
5249    be extended.  */
5250 /* This pattern implements two kinds simplification:
5252    Case 1)
5253    (cond (cmp (convert1? x) c1) (convert2? x) c2) -> (minmax (x c)) if:
5254      1) Conversions are type widening from smaller type.
5255      2) Const c1 equals to c2 after canonicalizing comparison.
5256      3) Comparison has tree code LT, LE, GT or GE.
5257    This specific pattern is needed when (cmp (convert x) c) may not
5258    be simplified by comparison patterns because of multiple uses of
5259    x.  It also makes sense here because simplifying across multiple
5260    referred var is always benefitial for complicated cases.
5262    Case 2)
5263    (cond (eq (convert1? x) c1) (convert2? x) c2) -> (cond (eq x c1) c1 c2).  */
5264 (for cmp (lt le gt ge eq ne)
5265  (simplify
5266   (cond (cmp (convert1? @1) INTEGER_CST@3) (convert2? @1) INTEGER_CST@2)
5267   (with
5268    {
5269      tree from_type = TREE_TYPE (@1);
5270      tree c1_type = TREE_TYPE (@3), c2_type = TREE_TYPE (@2);
5271      enum tree_code code = ERROR_MARK;
5273      if (INTEGRAL_TYPE_P (from_type)
5274          && int_fits_type_p (@2, from_type)
5275          && (types_match (c1_type, from_type)
5276              || (TYPE_PRECISION (c1_type) > TYPE_PRECISION (from_type)
5277                  && (TYPE_UNSIGNED (from_type)
5278                      || TYPE_SIGN (c1_type) == TYPE_SIGN (from_type))))
5279          && (types_match (c2_type, from_type)
5280              || (TYPE_PRECISION (c2_type) > TYPE_PRECISION (from_type)
5281                  && (TYPE_UNSIGNED (from_type)
5282                      || TYPE_SIGN (c2_type) == TYPE_SIGN (from_type)))))
5283        {
5284          if (cmp != EQ_EXPR)
5285            code = minmax_from_comparison (cmp, @1, @3, @1, @2);
5286          /* Can do A == C1 ? A : C2  ->  A == C1 ? C1 : C2?  */
5287          else if (int_fits_type_p (@3, from_type))
5288            code = EQ_EXPR;
5289        }
5290    }
5291    (if (code == MAX_EXPR)
5292     (convert (max @1 (convert @2)))
5293     (if (code == MIN_EXPR)
5294      (convert (min @1 (convert @2)))
5295      (if (code == EQ_EXPR)
5296       (convert (cond (eq @1 (convert @3))
5297                      (convert:from_type @3) (convert:from_type @2)))))))))
5299 /* (cond (cmp (convert? x) c1) (op x c2) c3) -> (op (minmax x c1) c2) if:
5301      1) OP is PLUS or MINUS.
5302      2) CMP is LT, LE, GT or GE.
5303      3) C3 == (C1 op C2), and computation doesn't have undefined behavior.
5305    This pattern also handles special cases like:
5307      A) Operand x is a unsigned to signed type conversion and c1 is
5308         integer zero.  In this case,
5309           (signed type)x  < 0  <=>  x  > MAX_VAL(signed type)
5310           (signed type)x >= 0  <=>  x <= MAX_VAL(signed type)
5311      B) Const c1 may not equal to (C3 op' C2).  In this case we also
5312         check equality for (c1+1) and (c1-1) by adjusting comparison
5313         code.
5315    TODO: Though signed type is handled by this pattern, it cannot be
5316    simplified at the moment because C standard requires additional
5317    type promotion.  In order to match&simplify it here, the IR needs
5318    to be cleaned up by other optimizers, i.e, VRP.  */
5319 (for op (plus minus)
5320  (for cmp (lt le gt ge)
5321   (simplify
5322    (cond (cmp (convert? @X) INTEGER_CST@1) (op @X INTEGER_CST@2) INTEGER_CST@3)
5323    (with { tree from_type = TREE_TYPE (@X), to_type = TREE_TYPE (@1); }
5324     (if (types_match (from_type, to_type)
5325          /* Check if it is special case A).  */
5326          || (TYPE_UNSIGNED (from_type)
5327              && !TYPE_UNSIGNED (to_type)
5328              && TYPE_PRECISION (from_type) == TYPE_PRECISION (to_type)
5329              && integer_zerop (@1)
5330              && (cmp == LT_EXPR || cmp == GE_EXPR)))
5331      (with
5332       {
5333         wi::overflow_type overflow = wi::OVF_NONE;
5334         enum tree_code code, cmp_code = cmp;
5335         wide_int real_c1;
5336         wide_int c1 = wi::to_wide (@1);
5337         wide_int c2 = wi::to_wide (@2);
5338         wide_int c3 = wi::to_wide (@3);
5339         signop sgn = TYPE_SIGN (from_type);
5341         /* Handle special case A), given x of unsigned type:
5342             ((signed type)x  < 0) <=> (x  > MAX_VAL(signed type))
5343             ((signed type)x >= 0) <=> (x <= MAX_VAL(signed type))  */
5344         if (!types_match (from_type, to_type))
5345           {
5346             if (cmp_code == LT_EXPR)
5347               cmp_code = GT_EXPR;
5348             if (cmp_code == GE_EXPR)
5349               cmp_code = LE_EXPR;
5350             c1 = wi::max_value (to_type);
5351           }
5352         /* To simplify this pattern, we require c3 = (c1 op c2).  Here we
5353            compute (c3 op' c2) and check if it equals to c1 with op' being
5354            the inverted operator of op.  Make sure overflow doesn't happen
5355            if it is undefined.  */
5356         if (op == PLUS_EXPR)
5357           real_c1 = wi::sub (c3, c2, sgn, &overflow);
5358         else
5359           real_c1 = wi::add (c3, c2, sgn, &overflow);
5361         code = cmp_code;
5362         if (!overflow || !TYPE_OVERFLOW_UNDEFINED (from_type))
5363           {
5364             /* Check if c1 equals to real_c1.  Boundary condition is handled
5365                by adjusting comparison operation if necessary.  */
5366             if (!wi::cmp (wi::sub (real_c1, 1, sgn, &overflow), c1, sgn)
5367                 && !overflow)
5368               {
5369                 /* X <= Y - 1 equals to X < Y.  */
5370                 if (cmp_code == LE_EXPR)
5371                   code = LT_EXPR;
5372                 /* X > Y - 1 equals to X >= Y.  */
5373                 if (cmp_code == GT_EXPR)
5374                   code = GE_EXPR;
5375               }
5376             if (!wi::cmp (wi::add (real_c1, 1, sgn, &overflow), c1, sgn)
5377                 && !overflow)
5378               {
5379                 /* X < Y + 1 equals to X <= Y.  */
5380                 if (cmp_code == LT_EXPR)
5381                   code = LE_EXPR;
5382                 /* X >= Y + 1 equals to X > Y.  */
5383                 if (cmp_code == GE_EXPR)
5384                   code = GT_EXPR;
5385               }
5386             if (code != cmp_code || !wi::cmp (real_c1, c1, sgn))
5387               {
5388                 if (cmp_code == LT_EXPR || cmp_code == LE_EXPR)
5389                   code = MIN_EXPR;
5390                 if (cmp_code == GT_EXPR || cmp_code == GE_EXPR)
5391                   code = MAX_EXPR;
5392               }
5393           }
5394       }
5395       (if (code == MAX_EXPR)
5396        (op (max @X { wide_int_to_tree (from_type, real_c1); })
5397            { wide_int_to_tree (from_type, c2); })
5398        (if (code == MIN_EXPR)
5399         (op (min @X { wide_int_to_tree (from_type, real_c1); })
5400             { wide_int_to_tree (from_type, c2); })))))))))
5402 #if GIMPLE
5403 /* A >= B ? A : B -> max (A, B) and friends.  The code is still
5404    in fold_cond_expr_with_comparison for GENERIC folding with
5405    some extra constraints.  */
5406 (for cmp (eq ne le lt unle unlt ge gt unge ungt uneq ltgt)
5407  (simplify
5408   (cond (cmp:c (nop_convert1?@c0 @0) (nop_convert2?@c1 @1))
5409         (convert3? @0) (convert4? @1))
5410   (if (!HONOR_SIGNED_ZEROS (type)
5411        && (/* Allow widening conversions of the compare operands as data.  */
5412            (INTEGRAL_TYPE_P (type)
5413             && types_match (TREE_TYPE (@c0), TREE_TYPE (@0))
5414             && types_match (TREE_TYPE (@c1), TREE_TYPE (@1))
5415             && TYPE_PRECISION (TREE_TYPE (@0)) <= TYPE_PRECISION (type)
5416             && TYPE_PRECISION (TREE_TYPE (@1)) <= TYPE_PRECISION (type))
5417            /* Or sign conversions for the comparison.  */
5418            || (types_match (type, TREE_TYPE (@0))
5419                && types_match (type, TREE_TYPE (@1)))))
5420    (switch
5421     (if (cmp == EQ_EXPR)
5422      (if (VECTOR_TYPE_P (type))
5423       (view_convert @c1)
5424       (convert @c1)))
5425     (if (cmp == NE_EXPR)
5426      (if (VECTOR_TYPE_P (type))
5427       (view_convert @c0)
5428       (convert @c0)))
5429     (if (cmp == LE_EXPR || cmp == UNLE_EXPR || cmp == LT_EXPR || cmp == UNLT_EXPR)
5430      (if (!HONOR_NANS (type))
5431       (if (VECTOR_TYPE_P (type))
5432        (view_convert (min @c0 @c1))
5433        (convert (min @c0 @c1)))))
5434     (if (cmp == GE_EXPR || cmp == UNGE_EXPR || cmp == GT_EXPR || cmp == UNGT_EXPR)
5435      (if (!HONOR_NANS (type))
5436       (if (VECTOR_TYPE_P (type))
5437        (view_convert (max @c0 @c1))
5438        (convert (max @c0 @c1)))))
5439     (if (cmp == UNEQ_EXPR)
5440      (if (!HONOR_NANS (type))
5441       (if (VECTOR_TYPE_P (type))
5442        (view_convert @c1)
5443        (convert @c1))))
5444     (if (cmp == LTGT_EXPR)
5445      (if (!HONOR_NANS (type))
5446       (if (VECTOR_TYPE_P (type))
5447        (view_convert @c0)
5448        (convert @c0))))))))
5449 #endif
5451 (for cnd (cond vec_cond)
5452  /* (a != b) ? (a - b) : 0 -> (a - b) */
5453  (simplify
5454   (cnd (ne:c @0 @1) (minus@2 @0 @1) integer_zerop)
5455   @2)
5456  /* (a != b) ? (a ^ b) : 0 -> (a ^ b) */
5457  (simplify
5458   (cnd (ne:c @0 @1) (bit_xor:c@2 @0 @1) integer_zerop)
5459   @2)
5460  /* (a != b) ? (a & b) : a -> (a & b) */
5461  /* (a != b) ? (a | b) : a -> (a | b) */
5462  /* (a != b) ? min(a,b) : a -> min(a,b) */
5463  /* (a != b) ? max(a,b) : a -> max(a,b) */
5464  (for op (bit_and bit_ior min max)
5465   (simplify
5466    (cnd (ne:c @0 @1) (op:c@2 @0 @1) @0)
5467    @2))
5468  /* (a != b) ? (a * b) : (a * a) -> (a * b) */
5469  /* (a != b) ? (a + b) : (a + a) -> (a + b) */
5470  (for op (mult plus)
5471   (simplify
5472    (cnd (ne:c @0 @1) (op@2 @0 @1) (op @0 @0))
5473    (if (ANY_INTEGRAL_TYPE_P (type))
5474     @2)))
5475  /* (a != b) ? (a + b) : (2 * a) -> (a + b) */
5476  (simplify
5477   (cnd (ne:c @0 @1) (plus@2 @0 @1) (mult @0 uniform_integer_cst_p@3))
5478   (if (wi::to_wide (uniform_integer_cst_p (@3)) == 2)
5479    @2))
5482 /* These was part of minmax phiopt.  */
5483 /* Optimize (a CMP b) ? minmax<a, c> : minmax<b, c>
5484    to minmax<min/max<a, b>, c> */
5485 (for minmax (min max)
5486  (for cmp (lt le gt ge ne)
5487   (simplify
5488    (cond (cmp:c @1 @3) (minmax:c @1 @4) (minmax:c @2 @4))
5489    (with
5490     {
5491       tree_code code = minmax_from_comparison (cmp, @1, @2, @1, @3);
5492     }
5493     (if (code == MIN_EXPR)
5494      (minmax (min @1 @2) @4)
5495      (if (code == MAX_EXPR)
5496       (minmax (max @1 @2) @4)))))))
5498 /* Optimize (a CMP CST1) ? max<a,CST2> : a */
5499 (for cmp    (gt  ge  lt  le)
5500      minmax (min min max max)
5501  (simplify
5502   (cond (cmp:c @0 @1) (minmax:c@2 @0 @3) @4)
5503    (with
5504     {
5505       tree_code code = minmax_from_comparison (cmp, @0, @1, @0, @4);
5506     }
5507     (if ((cmp == LT_EXPR || cmp == LE_EXPR)
5508          && code == MIN_EXPR
5509          && integer_nonzerop (fold_build2 (LE_EXPR, boolean_type_node, @3, @4)))
5510      (min @2 @4)
5511      (if ((cmp == GT_EXPR || cmp == GE_EXPR)
5512           && code == MAX_EXPR
5513           && integer_nonzerop (fold_build2 (GE_EXPR, boolean_type_node, @3, @4)))
5514       (max @2 @4))))))
5516 #if GIMPLE
5517 /* These patterns should be after min/max detection as simplifications
5518    of `(type)(zero_one ==/!= 0)` to `(type)(zero_one)`
5519    and `(type)(zero_one^1)` are not done yet.  See PR 110637.
5520    Even without those, reaching min/max/and/ior faster is better.  */
5521 (simplify
5522  (cond @0 zero_one_valued_p@1 zero_one_valued_p@2)
5523  (switch
5524   /* bool0 ? bool1 : 0 -> bool0 & bool1 */
5525   (if (integer_zerop (@2))
5526    (bit_and (convert @0) @1))
5527   /* bool0 ? 0 : bool2 -> (bool0^1) & bool2 */
5528   (if (integer_zerop (@1))
5529    (bit_and (bit_xor (convert @0) { build_one_cst (type); } ) @2))
5530   /* bool0 ? 1 : bool2 -> bool0 | bool2 */
5531   (if (integer_onep (@1))
5532    (bit_ior (convert @0) @2))
5533   /* bool0 ? bool1 : 1 -> (bool0^1) | bool1 */
5534   (if (integer_onep (@2))
5535    (bit_ior (bit_xor (convert @0) @2) @1))
5538 #endif
5540 /* X != C1 ? -X : C2 simplifies to -X when -C1 == C2.  */
5541 (simplify
5542  (cond (ne @0 INTEGER_CST@1) (negate@3 @0) INTEGER_CST@2)
5543  (if (!TYPE_SATURATING (type)
5544       && (TYPE_OVERFLOW_WRAPS (type)
5545           || !wi::only_sign_bit_p (wi::to_wide (@1)))
5546       && wi::eq_p (wi::neg (wi::to_wide (@1)), wi::to_wide (@2)))
5547   @3))
5549 /* X != C1 ? ~X : C2 simplifies to ~X when ~C1 == C2.  */
5550 (simplify
5551  (cond (ne @0 INTEGER_CST@1) (bit_not@3 @0) INTEGER_CST@2)
5552  (if (wi::eq_p (wi::bit_not (wi::to_wide (@1)), wi::to_wide (@2)))
5553   @3))
5555 /* (X + 1) > Y ? -X : 1 simplifies to X >= Y ? -X : 1 when
5556    X is unsigned, as when X + 1 overflows, X is -1, so -X == 1.  */
5557 (simplify
5558  (cond (gt (plus @0 integer_onep) @1) (negate @0) integer_onep@2)
5559  (if (TYPE_UNSIGNED (type))
5560   (cond (ge @0 @1) (negate @0) @2)))
5562 (for cnd (cond vec_cond)
5563  /* A ? B : (A ? X : C) -> A ? B : C.  */
5564  (simplify
5565   (cnd @0 (cnd @0 @1 @2) @3)
5566   (cnd @0 @1 @3))
5567  (simplify
5568   (cnd @0 @1 (cnd @0 @2 @3))
5569   (cnd @0 @1 @3))
5570  /* A ? B : (!A ? C : X) -> A ? B : C.  */
5571  /* ???  This matches embedded conditions open-coded because genmatch
5572     would generate matching code for conditions in separate stmts only.
5573     The following is still important to merge then and else arm cases
5574     from if-conversion.  */
5575  (simplify
5576   (cnd @0 @1 (cnd @2 @3 @4))
5577   (if (inverse_conditions_p (@0, @2))
5578    (cnd @0 @1 @3)))
5579  (simplify
5580   (cnd @0 (cnd @1 @2 @3) @4)
5581   (if (inverse_conditions_p (@0, @1))
5582    (cnd @0 @3 @4)))
5584  /* A ? B : B -> B.  */
5585  (simplify
5586   (cnd @0 @1 @1)
5587   @1)
5589  /* !A ? B : C -> A ? C : B.  */
5590  (simplify
5591   (cnd (logical_inverted_value truth_valued_p@0) @1 @2)
5592   (cnd @0 @2 @1)))
5594 /* abs/negative simplifications moved from fold_cond_expr_with_comparison,
5595    Need to handle (A - B) case as fold_cond_expr_with_comparison does.
5596    Need to handle UN* comparisons.
5598    None of these transformations work for modes with signed
5599    zeros.  If A is +/-0, the first two transformations will
5600    change the sign of the result (from +0 to -0, or vice
5601    versa).  The last four will fix the sign of the result,
5602    even though the original expressions could be positive or
5603    negative, depending on the sign of A.
5605    Note that all these transformations are correct if A is
5606    NaN, since the two alternatives (A and -A) are also NaNs.  */
5608 (for cnd (cond vec_cond)
5609  /* A == 0 ? A : -A    same as -A */
5610  (for cmp (eq uneq)
5611   (simplify
5612    (cnd (cmp @0 zerop) @0 (negate@1 @0))
5613     (if (!HONOR_SIGNED_ZEROS (type))
5614      @1))
5615   (simplify
5616    (cnd (cmp @0 zerop) zerop (negate@1 @0))
5617     (if (!HONOR_SIGNED_ZEROS (type))
5618      @1))
5620  /* A != 0 ? A : -A    same as A */
5621  (for cmp (ne ltgt)
5622   (simplify
5623    (cnd (cmp @0 zerop) @0 (negate @0))
5624     (if (!HONOR_SIGNED_ZEROS (type))
5625      @0))
5626   (simplify
5627    (cnd (cmp @0 zerop) @0 integer_zerop)
5628     (if (!HONOR_SIGNED_ZEROS (type))
5629      @0))
5631  /* A >=/> 0 ? A : -A    same as abs (A) */
5632  (for cmp (ge gt)
5633   (simplify
5634    (cnd (cmp @0 zerop) @0 (negate @0))
5635     (if (!HONOR_SIGNED_ZEROS (type)
5636          && !TYPE_UNSIGNED (type))
5637      (abs @0))))
5638  /* A <=/< 0 ? A : -A    same as -abs (A) */
5639  (for cmp (le lt)
5640   (simplify
5641    (cnd (cmp @0 zerop) @0 (negate @0))
5642     (if (!HONOR_SIGNED_ZEROS (type)
5643          && !TYPE_UNSIGNED (type))
5644      (if (ANY_INTEGRAL_TYPE_P (type)
5645           && !TYPE_OVERFLOW_WRAPS (type))
5646       (with {
5647         tree utype = unsigned_type_for (type);
5648        }
5649        (convert (negate (absu:utype @0))))
5650        (negate (abs @0)))))
5654 /* -(type)!A -> (type)A - 1.  */
5655 (simplify
5656  (negate (convert?:s (logical_inverted_value:s @0)))
5657  (if (INTEGRAL_TYPE_P (type)
5658       && TREE_CODE (type) != BOOLEAN_TYPE
5659       && TYPE_PRECISION (type) > 1
5660       && TREE_CODE (@0) == SSA_NAME
5661       && ssa_name_has_boolean_range (@0))
5662   (plus (convert:type @0) { build_all_ones_cst (type); })))
5664 /* A + (B vcmp C ? 1 : 0) -> A - (B vcmp C ? -1 : 0), since vector comparisons
5665    return all -1 or all 0 results.  */
5666 /* ??? We could instead convert all instances of the vec_cond to negate,
5667    but that isn't necessarily a win on its own.  */
5668 (simplify
5669  (plus:c @3 (view_convert? (vec_cond:s @0 integer_each_onep@1 integer_zerop@2)))
5670  (if (VECTOR_TYPE_P (type)
5671       && known_eq (TYPE_VECTOR_SUBPARTS (type),
5672                    TYPE_VECTOR_SUBPARTS (TREE_TYPE (@1)))
5673       && (TYPE_MODE (TREE_TYPE (type))
5674           == TYPE_MODE (TREE_TYPE (TREE_TYPE (@1)))))
5675   (minus @3 (view_convert (vec_cond @0 (negate @1) @2)))))
5677 /* ... likewise A - (B vcmp C ? 1 : 0) -> A + (B vcmp C ? -1 : 0).  */
5678 (simplify
5679  (minus @3 (view_convert? (vec_cond:s @0 integer_each_onep@1 integer_zerop@2)))
5680  (if (VECTOR_TYPE_P (type)
5681       && known_eq (TYPE_VECTOR_SUBPARTS (type),
5682                    TYPE_VECTOR_SUBPARTS (TREE_TYPE (@1)))
5683       && (TYPE_MODE (TREE_TYPE (type))
5684           == TYPE_MODE (TREE_TYPE (TREE_TYPE (@1)))))
5685   (plus @3 (view_convert (vec_cond @0 (negate @1) @2)))))
5688 /* Simplifications of comparisons.  */
5690 /* See if we can reduce the magnitude of a constant involved in a
5691    comparison by changing the comparison code.  This is a canonicalization
5692    formerly done by maybe_canonicalize_comparison_1.  */
5693 (for cmp  (le gt)
5694      acmp (lt ge)
5695  (simplify
5696   (cmp @0 uniform_integer_cst_p@1)
5697   (with { tree cst = uniform_integer_cst_p (@1); }
5698    (if (tree_int_cst_sgn (cst) == -1)
5699      (acmp @0 { build_uniform_cst (TREE_TYPE (@1),
5700                                    wide_int_to_tree (TREE_TYPE (cst),
5701                                                      wi::to_wide (cst)
5702                                                      + 1)); })))))
5703 (for cmp  (ge lt)
5704      acmp (gt le)
5705  (simplify
5706   (cmp @0 uniform_integer_cst_p@1)
5707   (with { tree cst = uniform_integer_cst_p (@1); }
5708    (if (tree_int_cst_sgn (cst) == 1)
5709     (acmp @0 { build_uniform_cst (TREE_TYPE (@1),
5710                                   wide_int_to_tree (TREE_TYPE (cst),
5711                                   wi::to_wide (cst) - 1)); })))))
5713 /* We can simplify a logical negation of a comparison to the
5714    inverted comparison.  As we cannot compute an expression
5715    operator using invert_tree_comparison we have to simulate
5716    that with expression code iteration.  */
5717 (for cmp (tcc_comparison)
5718      icmp (inverted_tcc_comparison)
5719      ncmp (inverted_tcc_comparison_with_nans)
5720  /* Ideally we'd like to combine the following two patterns
5721     and handle some more cases by using
5722       (logical_inverted_value (cmp @0 @1))
5723     here but for that genmatch would need to "inline" that.
5724     For now implement what forward_propagate_comparison did.  */
5725  (simplify
5726   (bit_not (cmp @0 @1))
5727   (if (VECTOR_TYPE_P (type)
5728        || (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1))
5729    /* Comparison inversion may be impossible for trapping math,
5730       invert_tree_comparison will tell us.  But we can't use
5731       a computed operator in the replacement tree thus we have
5732       to play the trick below.  */
5733    (with { enum tree_code ic = invert_tree_comparison
5734              (cmp, HONOR_NANS (@0)); }
5735     (if (ic == icmp)
5736      (icmp @0 @1)
5737      (if (ic == ncmp)
5738       (ncmp @0 @1))))))
5739  (simplify
5740   (bit_xor (cmp @0 @1) integer_truep)
5741   (with { enum tree_code ic = invert_tree_comparison
5742             (cmp, HONOR_NANS (@0)); }
5743    (if (ic == icmp)
5744     (icmp @0 @1)
5745     (if (ic == ncmp)
5746      (ncmp @0 @1)))))
5747  /* The following bits are handled by fold_binary_op_with_conditional_arg.  */
5748  (simplify
5749   (ne (cmp@2 @0 @1) integer_zerop)
5750   (if (types_match (type, TREE_TYPE (@2)))
5751    (cmp @0 @1)))
5752  (simplify
5753   (eq (cmp@2 @0 @1) integer_truep)
5754   (if (types_match (type, TREE_TYPE (@2)))
5755    (cmp @0 @1)))
5756  (simplify
5757   (ne (cmp@2 @0 @1) integer_truep)
5758   (if (types_match (type, TREE_TYPE (@2)))
5759    (with { enum tree_code ic = invert_tree_comparison
5760              (cmp, HONOR_NANS (@0)); }
5761     (if (ic == icmp)
5762      (icmp @0 @1)
5763      (if (ic == ncmp)
5764       (ncmp @0 @1))))))
5765  (simplify
5766   (eq (cmp@2 @0 @1) integer_zerop)
5767   (if (types_match (type, TREE_TYPE (@2)))
5768    (with { enum tree_code ic = invert_tree_comparison
5769              (cmp, HONOR_NANS (@0)); }
5770     (if (ic == icmp)
5771      (icmp @0 @1)
5772      (if (ic == ncmp)
5773       (ncmp @0 @1)))))))
5775 /* Transform comparisons of the form X - Y CMP 0 to X CMP Y.
5776    ??? The transformation is valid for the other operators if overflow
5777    is undefined for the type, but performing it here badly interacts
5778    with the transformation in fold_cond_expr_with_comparison which
5779    attempts to synthetize ABS_EXPR.  */
5780 (for cmp (eq ne)
5781  (for sub (minus pointer_diff)
5782   (simplify
5783    (cmp (sub@2 @0 @1) integer_zerop)
5784    (if (single_use (@2))
5785     (cmp @0 @1)))))
5787 /* Simplify (x < 0) ^ (y < 0) to (x ^ y) < 0 and
5788    (x >= 0) ^ (y >= 0) to (x ^ y) < 0.  */
5789 (for cmp (lt ge)
5790  (simplify
5791   (bit_xor (cmp:s @0 integer_zerop) (cmp:s @1 integer_zerop))
5792    (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
5793         && !TYPE_UNSIGNED (TREE_TYPE (@0))
5794         && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
5795     (lt (bit_xor @0 @1) { build_zero_cst (TREE_TYPE (@0)); }))))
5796 /* Simplify (x < 0) ^ (y >= 0) to (x ^ y) >= 0 and
5797    (x >= 0) ^ (y < 0) to (x ^ y) >= 0.  */
5798 (simplify
5799  (bit_xor:c (lt:s @0 integer_zerop) (ge:s @1 integer_zerop))
5800   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
5801        && !TYPE_UNSIGNED (TREE_TYPE (@0))
5802        && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
5803    (ge (bit_xor @0 @1) { build_zero_cst (TREE_TYPE (@0)); })))
5805 /* Transform comparisons of the form X * C1 CMP 0 to X CMP 0 in the
5806    signed arithmetic case.  That form is created by the compiler
5807    often enough for folding it to be of value.  One example is in
5808    computing loop trip counts after Operator Strength Reduction.  */
5809 (for cmp (simple_comparison)
5810      scmp (swapped_simple_comparison)
5811  (simplify
5812   (cmp (mult@3 @0 INTEGER_CST@1) integer_zerop@2)
5813   /* Handle unfolded multiplication by zero.  */
5814   (if (integer_zerop (@1))
5815    (cmp @1 @2)
5816    (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
5817         && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
5818         && single_use (@3))
5819     /* If @1 is negative we swap the sense of the comparison.  */
5820     (if (tree_int_cst_sgn (@1) < 0)
5821      (scmp @0 @2)
5822      (cmp @0 @2))))))
5824 /* For integral types with undefined overflow fold
5825    x * C1 == C2 into x == C2 / C1 or false.
5826    If overflow wraps and C1 is odd, simplify to x == C2 / C1 in the ring
5827    Z / 2^n Z.  */
5828 (for cmp (eq ne)
5829  (simplify
5830   (cmp (mult @0 INTEGER_CST@1) INTEGER_CST@2)
5831   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
5832        && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
5833        && wi::to_wide (@1) != 0)
5834    (with { widest_int quot; }
5835     (if (wi::multiple_of_p (wi::to_widest (@2), wi::to_widest (@1),
5836                             TYPE_SIGN (TREE_TYPE (@0)), &quot))
5837      (cmp @0 { wide_int_to_tree (TREE_TYPE (@0), quot); })
5838      { constant_boolean_node (cmp == NE_EXPR, type); }))
5839    (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
5840         && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))
5841         && (wi::bit_and (wi::to_wide (@1), 1) == 1))
5842     (cmp @0
5843      {
5844        tree itype = TREE_TYPE (@0);
5845        int p = TYPE_PRECISION (itype);
5846        wide_int m = wi::one (p + 1) << p;
5847        wide_int a = wide_int::from (wi::to_wide (@1), p + 1, UNSIGNED);
5848        wide_int i = wide_int::from (wi::mod_inv (a, m),
5849                                     p, TYPE_SIGN (itype));
5850        wide_int_to_tree (itype, wi::mul (i, wi::to_wide (@2)));
5851      })))))
5853 /* Simplify comparison of something with itself.  For IEEE
5854    floating-point, we can only do some of these simplifications.  */
5855 (for cmp (eq ge le)
5856  (simplify
5857   (cmp @0 @0)
5858   (if (! FLOAT_TYPE_P (TREE_TYPE (@0))
5859        || ! tree_expr_maybe_nan_p (@0))
5860    { constant_boolean_node (true, type); }
5861    (if (cmp != EQ_EXPR
5862         /* With -ftrapping-math conversion to EQ loses an exception.  */
5863         && (! FLOAT_TYPE_P (TREE_TYPE (@0))
5864             || ! flag_trapping_math))
5865     (eq @0 @0)))))
5866 (for cmp (ne gt lt)
5867  (simplify
5868   (cmp @0 @0)
5869   (if (cmp != NE_EXPR
5870        || ! FLOAT_TYPE_P (TREE_TYPE (@0))
5871        || ! tree_expr_maybe_nan_p (@0))
5872    { constant_boolean_node (false, type); })))
5873 (for cmp (unle unge uneq)
5874  (simplify
5875   (cmp @0 @0)
5876   { constant_boolean_node (true, type); }))
5877 (for cmp (unlt ungt)
5878  (simplify
5879   (cmp @0 @0)
5880   (unordered @0 @0)))
5881 (simplify
5882  (ltgt @0 @0)
5883  (if (!flag_trapping_math || !tree_expr_maybe_nan_p (@0))
5884   { constant_boolean_node (false, type); }))
5886 /* x == ~x -> false */
5887 /* x != ~x -> true */
5888 (for cmp (eq ne)
5889  (simplify
5890   (cmp:c @0 (bit_not @0))
5891   { constant_boolean_node (cmp == NE_EXPR, type); }))
5893 /* Fold ~X op ~Y as Y op X.  */
5894 (for cmp (simple_comparison)
5895  (simplify
5896   (cmp (bit_not@2 @0) (bit_not@3 @1))
5897   (if (single_use (@2) && single_use (@3))
5898    (cmp @1 @0))))
5900 /* Fold ~X op C as X op' ~C, where op' is the swapped comparison.  */
5901 (for cmp (simple_comparison)
5902      scmp (swapped_simple_comparison)
5903  (simplify
5904   (cmp (bit_not@2 @0) CONSTANT_CLASS_P@1)
5905   (if (single_use (@2)
5906        && (TREE_CODE (@1) == INTEGER_CST || TREE_CODE (@1) == VECTOR_CST))
5907    (scmp @0 (bit_not @1)))))
5909 (for cmp (simple_comparison)
5910  (simplify
5911   (cmp @0 REAL_CST@1)
5912   /* IEEE doesn't distinguish +0 and -0 in comparisons.  */
5913   (switch
5914    /* a CMP (-0) -> a CMP 0  */
5915    (if (REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@1)))
5916     (cmp @0 { build_real (TREE_TYPE (@1), dconst0); }))
5917    /* (-0) CMP b -> 0 CMP b.  */
5918    (if (TREE_CODE (@0) == REAL_CST
5919         && REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (@0)))
5920     (cmp { build_real (TREE_TYPE (@0), dconst0); } @1))
5921    /* x != NaN is always true, other ops are always false.  */
5922    (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
5923         && (cmp == EQ_EXPR || cmp == NE_EXPR || !flag_trapping_math)
5924         && !tree_expr_signaling_nan_p (@1)
5925         && !tree_expr_maybe_signaling_nan_p (@0))
5926     { constant_boolean_node (cmp == NE_EXPR, type); })
5927    /* NaN != y is always true, other ops are always false.  */
5928    (if (TREE_CODE (@0) == REAL_CST
5929         && REAL_VALUE_ISNAN (TREE_REAL_CST (@0))
5930         && (cmp == EQ_EXPR || cmp == NE_EXPR || !flag_trapping_math)
5931         && !tree_expr_signaling_nan_p (@0)
5932         && !tree_expr_signaling_nan_p (@1))
5933     { constant_boolean_node (cmp == NE_EXPR, type); })
5934    /* Fold comparisons against infinity.  */
5935    (if (REAL_VALUE_ISINF (TREE_REAL_CST (@1))
5936         && MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (@1))))
5937     (with
5938      {
5939        REAL_VALUE_TYPE max;
5940        enum tree_code code = cmp;
5941        bool neg = REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1));
5942        if (neg)
5943          code = swap_tree_comparison (code);
5944      }
5945      (switch
5946       /* x > +Inf is always false, if we ignore NaNs or exceptions.  */
5947       (if (code == GT_EXPR
5948            && !(HONOR_NANS (@0) && flag_trapping_math))
5949        { constant_boolean_node (false, type); })
5950       (if (code == LE_EXPR)
5951        /* x <= +Inf is always true, if we don't care about NaNs.  */
5952        (if (! HONOR_NANS (@0))
5953         { constant_boolean_node (true, type); }
5954         /* x <= +Inf is the same as x == x, i.e. !isnan(x), but this loses
5955            an "invalid" exception.  */
5956         (if (!flag_trapping_math)
5957          (eq @0 @0))))
5958       /* x == +Inf and x >= +Inf are always equal to x > DBL_MAX, but
5959          for == this introduces an exception for x a NaN.  */
5960       (if ((code == EQ_EXPR && !(HONOR_NANS (@0) && flag_trapping_math))
5961            || code == GE_EXPR)
5962        (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
5963         (if (neg)
5964          (lt @0 { build_real (TREE_TYPE (@0), max); })
5965          (gt @0 { build_real (TREE_TYPE (@0), max); }))))
5966       /* x < +Inf is always equal to x <= DBL_MAX.  */
5967       (if (code == LT_EXPR)
5968        (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
5969         (if (neg)
5970          (ge @0 { build_real (TREE_TYPE (@0), max); })
5971          (le @0 { build_real (TREE_TYPE (@0), max); }))))
5972       /* x != +Inf is always equal to !(x > DBL_MAX), but this introduces
5973          an exception for x a NaN so use an unordered comparison.  */
5974       (if (code == NE_EXPR)
5975        (with { real_maxval (&max, neg, TYPE_MODE (TREE_TYPE (@0))); }
5976         (if (! HONOR_NANS (@0))
5977          (if (neg)
5978           (ge @0 { build_real (TREE_TYPE (@0), max); })
5979           (le @0 { build_real (TREE_TYPE (@0), max); }))
5980          (if (neg)
5981           (unge @0 { build_real (TREE_TYPE (@0), max); })
5982           (unle @0 { build_real (TREE_TYPE (@0), max); }))))))))))
5984  /* If this is a comparison of a real constant with a PLUS_EXPR
5985     or a MINUS_EXPR of a real constant, we can convert it into a
5986     comparison with a revised real constant as long as no overflow
5987     occurs when unsafe_math_optimizations are enabled.  */
5988  (if (flag_unsafe_math_optimizations)
5989   (for op (plus minus)
5990    (simplify
5991     (cmp (op @0 REAL_CST@1) REAL_CST@2)
5992     (with
5993      {
5994        tree tem = const_binop (op == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR,
5995                                TREE_TYPE (@1), @2, @1);
5996      }
5997      (if (tem && !TREE_OVERFLOW (tem))
5998       (cmp @0 { tem; }))))))
6000  /* Likewise, we can simplify a comparison of a real constant with
6001     a MINUS_EXPR whose first operand is also a real constant, i.e.
6002     (c1 - x) < c2 becomes x > c1-c2.  Reordering is allowed on
6003     floating-point types only if -fassociative-math is set.  */
6004  (if (flag_associative_math)
6005   (simplify
6006    (cmp (minus REAL_CST@0 @1) REAL_CST@2)
6007    (with { tree tem = const_binop (MINUS_EXPR, TREE_TYPE (@1), @0, @2); }
6008     (if (tem && !TREE_OVERFLOW (tem))
6009      (cmp { tem; } @1)))))
6011  /* Fold comparisons against built-in math functions.  */
6012  (if (flag_unsafe_math_optimizations && ! flag_errno_math)
6013   (for sq (SQRT)
6014    (simplify
6015     (cmp (sq @0) REAL_CST@1)
6016     (switch
6017      (if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (@1)))
6018       (switch
6019        /* sqrt(x) < y is always false, if y is negative.  */
6020        (if (cmp == EQ_EXPR || cmp == LT_EXPR || cmp == LE_EXPR)
6021         { constant_boolean_node (false, type); })
6022        /* sqrt(x) > y is always true, if y is negative and we
6023           don't care about NaNs, i.e. negative values of x.  */
6024        (if (cmp == NE_EXPR || !HONOR_NANS (@0))
6025         { constant_boolean_node (true, type); })
6026        /* sqrt(x) > y is the same as x >= 0, if y is negative.  */
6027        (ge @0 { build_real (TREE_TYPE (@0), dconst0); })))
6028      (if (real_equal (TREE_REAL_CST_PTR (@1), &dconst0))
6029       (switch
6030        /* sqrt(x) < 0 is always false.  */
6031        (if (cmp == LT_EXPR)
6032         { constant_boolean_node (false, type); })
6033        /* sqrt(x) >= 0 is always true if we don't care about NaNs.  */
6034        (if (cmp == GE_EXPR && !HONOR_NANS (@0))
6035         { constant_boolean_node (true, type); })
6036        /* sqrt(x) <= 0 -> x == 0.  */
6037        (if (cmp == LE_EXPR)
6038         (eq @0 @1))
6039        /* Otherwise sqrt(x) cmp 0 -> x cmp 0.  Here cmp can be >=, >,
6040           == or !=.  In the last case:
6042             (sqrt(x) != 0) == (NaN != 0) == true == (x != 0)
6044           if x is negative or NaN.  Due to -funsafe-math-optimizations,
6045           the results for other x follow from natural arithmetic.  */
6046        (cmp @0 @1)))
6047      (if ((cmp == LT_EXPR
6048            || cmp == LE_EXPR
6049            || cmp == GT_EXPR
6050            || cmp == GE_EXPR)
6051           && !REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
6052           /* Give up for -frounding-math.  */
6053           && !HONOR_SIGN_DEPENDENT_ROUNDING (TREE_TYPE (@0)))
6054       (with
6055        {
6056          REAL_VALUE_TYPE c2;
6057          enum tree_code ncmp = cmp;
6058          const real_format *fmt
6059            = REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (@0)));
6060          real_arithmetic (&c2, MULT_EXPR,
6061                           &TREE_REAL_CST (@1), &TREE_REAL_CST (@1));
6062          real_convert (&c2, fmt, &c2);
6063          /* See PR91734: if c2 is inexact and sqrt(c2) < c (or sqrt(c2) >= c),
6064             then change LT_EXPR into LE_EXPR or GE_EXPR into GT_EXPR.  */
6065          if (!REAL_VALUE_ISINF (c2))
6066            {
6067              tree c3 = fold_const_call (CFN_SQRT, TREE_TYPE (@0),
6068                                         build_real (TREE_TYPE (@0), c2));
6069              if (c3 == NULL_TREE || TREE_CODE (c3) != REAL_CST)
6070                ncmp = ERROR_MARK;
6071              else if ((cmp == LT_EXPR || cmp == GE_EXPR)
6072                       && real_less (&TREE_REAL_CST (c3), &TREE_REAL_CST (@1)))
6073                ncmp = cmp == LT_EXPR ? LE_EXPR : GT_EXPR;
6074              else if ((cmp == LE_EXPR || cmp == GT_EXPR)
6075                       && real_less (&TREE_REAL_CST (@1), &TREE_REAL_CST (c3)))
6076                ncmp = cmp == LE_EXPR ? LT_EXPR : GE_EXPR;
6077              else
6078                {
6079                  /* With rounding to even, sqrt of up to 3 different values
6080                     gives the same normal result, so in some cases c2 needs
6081                     to be adjusted.  */
6082                  REAL_VALUE_TYPE c2alt, tow;
6083                  if (cmp == LT_EXPR || cmp == GE_EXPR)
6084                    tow = dconst0;
6085                  else
6086                    tow = dconstinf;
6087                  real_nextafter (&c2alt, fmt, &c2, &tow);
6088                  real_convert (&c2alt, fmt, &c2alt);
6089                  if (REAL_VALUE_ISINF (c2alt))
6090                    ncmp = ERROR_MARK;
6091                  else
6092                    {
6093                      c3 = fold_const_call (CFN_SQRT, TREE_TYPE (@0),
6094                                            build_real (TREE_TYPE (@0), c2alt));
6095                      if (c3 == NULL_TREE || TREE_CODE (c3) != REAL_CST)
6096                        ncmp = ERROR_MARK;
6097                      else if (real_equal (&TREE_REAL_CST (c3),
6098                                           &TREE_REAL_CST (@1)))
6099                        c2 = c2alt;
6100                    }
6101                }
6102            }
6103        }
6104        (if (cmp == GT_EXPR || cmp == GE_EXPR)
6105         (if (REAL_VALUE_ISINF (c2))
6106          /* sqrt(x) > y is x == +Inf, when y is very large.  */
6107          (if (HONOR_INFINITIES (@0))
6108           (eq @0 { build_real (TREE_TYPE (@0), c2); })
6109           { constant_boolean_node (false, type); })
6110          /* sqrt(x) > c is the same as x > c*c.  */
6111          (if (ncmp != ERROR_MARK)
6112           (if (ncmp == GE_EXPR)
6113            (ge @0 { build_real (TREE_TYPE (@0), c2); })
6114            (gt @0 { build_real (TREE_TYPE (@0), c2); }))))
6115         /* else if (cmp == LT_EXPR || cmp == LE_EXPR)  */
6116         (if (REAL_VALUE_ISINF (c2))
6117          (switch
6118           /* sqrt(x) < y is always true, when y is a very large
6119              value and we don't care about NaNs or Infinities.  */
6120           (if (! HONOR_NANS (@0) && ! HONOR_INFINITIES (@0))
6121            { constant_boolean_node (true, type); })
6122           /* sqrt(x) < y is x != +Inf when y is very large and we
6123              don't care about NaNs.  */
6124           (if (! HONOR_NANS (@0))
6125            (ne @0 { build_real (TREE_TYPE (@0), c2); }))
6126           /* sqrt(x) < y is x >= 0 when y is very large and we
6127              don't care about Infinities.  */
6128           (if (! HONOR_INFINITIES (@0))
6129            (ge @0 { build_real (TREE_TYPE (@0), dconst0); }))
6130           /* sqrt(x) < y is x >= 0 && x != +Inf, when y is large.  */
6131           (if (GENERIC)
6132            (truth_andif
6133             (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
6134             (ne @0 { build_real (TREE_TYPE (@0), c2); }))))
6135          /* sqrt(x) < c is the same as x < c*c, if we ignore NaNs.  */
6136          (if (ncmp != ERROR_MARK && ! HONOR_NANS (@0))
6137           (if (ncmp == LT_EXPR)
6138            (lt @0 { build_real (TREE_TYPE (@0), c2); })
6139            (le @0 { build_real (TREE_TYPE (@0), c2); }))
6140           /* sqrt(x) < c is the same as x >= 0 && x < c*c.  */
6141           (if (ncmp != ERROR_MARK && GENERIC)
6142            (if (ncmp == LT_EXPR)
6143             (truth_andif
6144              (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
6145              (lt @0 { build_real (TREE_TYPE (@0), c2); }))
6146             (truth_andif
6147              (ge @0 { build_real (TREE_TYPE (@0), dconst0); })
6148              (le @0 { build_real (TREE_TYPE (@0), c2); })))))))))))
6149    /* Transform sqrt(x) cmp sqrt(y) -> x cmp y.  */
6150    (simplify
6151     (cmp (sq @0) (sq @1))
6152       (if (! HONOR_NANS (@0))
6153         (cmp @0 @1))))))
6155 /* Optimize various special cases of (FTYPE) N CMP (FTYPE) M.  */
6156 (for cmp  (lt le eq ne ge gt unordered ordered unlt unle ungt unge uneq ltgt)
6157      icmp (lt le eq ne ge gt unordered ordered lt   le   gt   ge   eq   ne)
6158  (simplify
6159   (cmp (float@0 @1) (float @2))
6160    (if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (@0))
6161         && ! DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0)))
6162     (with
6163      {
6164        format_helper fmt (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (@0))));
6165        tree type1 = TREE_TYPE (@1);
6166        bool type1_signed_p = TYPE_SIGN (type1) == SIGNED;
6167        tree type2 = TREE_TYPE (@2);
6168        bool type2_signed_p = TYPE_SIGN (type2) == SIGNED;
6169      }
6170      (if (fmt.can_represent_integral_type_p (type1)
6171           && fmt.can_represent_integral_type_p (type2))
6172       (if (cmp == ORDERED_EXPR || cmp == UNORDERED_EXPR)
6173        { constant_boolean_node (cmp == ORDERED_EXPR, type); }
6174        (if (TYPE_PRECISION (type1) > TYPE_PRECISION (type2)
6175             && type1_signed_p >= type2_signed_p)
6176         (icmp @1 (convert @2))
6177         (if (TYPE_PRECISION (type1) < TYPE_PRECISION (type2)
6178              && type1_signed_p <= type2_signed_p)
6179          (icmp (convert:type2 @1) @2)
6180          (if (TYPE_PRECISION (type1) == TYPE_PRECISION (type2)
6181               && type1_signed_p == type2_signed_p)
6182           (icmp @1 @2))))))))))
6184 /* Optimize various special cases of (FTYPE) N CMP CST.  */
6185 (for cmp  (lt le eq ne ge gt)
6186      icmp (le le eq ne ge ge)
6187  (simplify
6188   (cmp (float @0) REAL_CST@1)
6189    (if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (@1))
6190         && ! DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@1)))
6191     (with
6192      {
6193        tree itype = TREE_TYPE (@0);
6194        format_helper fmt (REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (@1))));
6195        const REAL_VALUE_TYPE *cst = TREE_REAL_CST_PTR (@1);
6196        /* Be careful to preserve any potential exceptions due to
6197           NaNs.  qNaNs are ok in == or != context.
6198           TODO: relax under -fno-trapping-math or
6199           -fno-signaling-nans.  */
6200        bool exception_p
6201          = real_isnan (cst) && (cst->signalling
6202                                 || (cmp != EQ_EXPR && cmp != NE_EXPR));
6203      }
6204      /* TODO: allow non-fitting itype and SNaNs when
6205         -fno-trapping-math.  */
6206      (if (fmt.can_represent_integral_type_p (itype) && ! exception_p)
6207       (with
6208        {
6209          signop isign = TYPE_SIGN (itype);
6210          REAL_VALUE_TYPE imin, imax;
6211          real_from_integer (&imin, fmt, wi::min_value (itype), isign);
6212          real_from_integer (&imax, fmt, wi::max_value (itype), isign);
6214          REAL_VALUE_TYPE icst;
6215          if (cmp == GT_EXPR || cmp == GE_EXPR)
6216            real_ceil (&icst, fmt, cst);
6217          else if (cmp == LT_EXPR || cmp == LE_EXPR)
6218            real_floor (&icst, fmt, cst);
6219          else
6220            real_trunc (&icst, fmt, cst);
6222          bool cst_int_p = !real_isnan (cst) && real_identical (&icst, cst);
6224          bool overflow_p = false;
6225          wide_int icst_val
6226            = real_to_integer (&icst, &overflow_p, TYPE_PRECISION (itype));
6227        }
6228        (switch
6229         /* Optimize cases when CST is outside of ITYPE's range.  */
6230         (if (real_compare (LT_EXPR, cst, &imin))
6231          { constant_boolean_node (cmp == GT_EXPR || cmp == GE_EXPR || cmp == NE_EXPR,
6232                                   type); })
6233         (if (real_compare (GT_EXPR, cst, &imax))
6234          { constant_boolean_node (cmp == LT_EXPR || cmp == LE_EXPR || cmp == NE_EXPR,
6235                                   type); })
6236         /* Remove cast if CST is an integer representable by ITYPE.  */
6237         (if (cst_int_p)
6238          (cmp @0 { gcc_assert (!overflow_p);
6239                    wide_int_to_tree (itype, icst_val); })
6240         )
6241         /* When CST is fractional, optimize
6242             (FTYPE) N == CST -> 0
6243             (FTYPE) N != CST -> 1.  */
6244         (if (cmp == EQ_EXPR || cmp == NE_EXPR)
6245          { constant_boolean_node (cmp == NE_EXPR, type); })
6246         /* Otherwise replace with sensible integer constant.  */
6247         (with
6248          {
6249            gcc_checking_assert (!overflow_p);
6250          }
6251          (icmp @0 { wide_int_to_tree (itype, icst_val); })))))))))
6253 /* Fold A /[ex] B CMP C to A CMP B * C.  */
6254 (for cmp (eq ne)
6255  (simplify
6256   (cmp (exact_div @0 @1) INTEGER_CST@2)
6257   (if (!integer_zerop (@1))
6258    (if (wi::to_wide (@2) == 0)
6259     (cmp @0 @2)
6260     (if (TREE_CODE (@1) == INTEGER_CST)
6261      (with
6262       {
6263         wi::overflow_type ovf;
6264         wide_int prod = wi::mul (wi::to_wide (@2), wi::to_wide (@1),
6265                                  TYPE_SIGN (TREE_TYPE (@1)), &ovf);
6266       }
6267       (if (ovf)
6268        { constant_boolean_node (cmp == NE_EXPR, type); }
6269        (cmp @0 { wide_int_to_tree (TREE_TYPE (@0), prod); }))))))))
6270 (for cmp (lt le gt ge)
6271  (simplify
6272   (cmp (exact_div @0 INTEGER_CST@1) INTEGER_CST@2)
6273   (if (wi::gt_p (wi::to_wide (@1), 0, TYPE_SIGN (TREE_TYPE (@1))))
6274    (with
6275     {
6276       wi::overflow_type ovf;
6277       wide_int prod = wi::mul (wi::to_wide (@2), wi::to_wide (@1),
6278                                TYPE_SIGN (TREE_TYPE (@1)), &ovf);
6279     }
6280     (if (ovf)
6281      { constant_boolean_node (wi::lt_p (wi::to_wide (@2), 0,
6282                                         TYPE_SIGN (TREE_TYPE (@2)))
6283                               != (cmp == LT_EXPR || cmp == LE_EXPR), type); }
6284      (cmp @0 { wide_int_to_tree (TREE_TYPE (@0), prod); }))))))
6286 /* Fold (size_t)(A /[ex] B) CMP C to (size_t)A CMP (size_t)B * C or A CMP' 0.
6288    For small C (less than max/B), this is (size_t)A CMP (size_t)B * C.
6289    For large C (more than min/B+2^size), this is also true, with the
6290    multiplication computed modulo 2^size.
6291    For intermediate C, this just tests the sign of A.  */
6292 (for cmp  (lt le gt ge)
6293      cmp2 (ge ge lt lt)
6294  (simplify
6295   (cmp (convert (exact_div @0 INTEGER_CST@1)) INTEGER_CST@2)
6296   (if (tree_nop_conversion_p (TREE_TYPE (@0), TREE_TYPE (@2))
6297        && TYPE_UNSIGNED (TREE_TYPE (@2)) && !TYPE_UNSIGNED (TREE_TYPE (@0))
6298        && wi::gt_p (wi::to_wide (@1), 0, TYPE_SIGN (TREE_TYPE (@1))))
6299    (with
6300     {
6301       tree utype = TREE_TYPE (@2);
6302       wide_int denom = wi::to_wide (@1);
6303       wide_int right = wi::to_wide (@2);
6304       wide_int smax = wi::sdiv_trunc (wi::max_value (TREE_TYPE (@0)), denom);
6305       wide_int smin = wi::sdiv_trunc (wi::min_value (TREE_TYPE (@0)), denom);
6306       bool small = wi::leu_p (right, smax);
6307       bool large = wi::geu_p (right, smin);
6308     }
6309     (if (small || large)
6310      (cmp (convert:utype @0) (mult @2 (convert @1)))
6311      (cmp2 @0 { build_zero_cst (TREE_TYPE (@0)); }))))))
6313 /* Unordered tests if either argument is a NaN.  */
6314 (simplify
6315  (bit_ior (unordered @0 @0) (unordered @1 @1))
6316  (if (types_match (@0, @1))
6317   (unordered @0 @1)))
6318 (simplify
6319  (bit_and (ordered @0 @0) (ordered @1 @1))
6320  (if (types_match (@0, @1))
6321   (ordered @0 @1)))
6322 (simplify
6323  (bit_ior:c (unordered @0 @0) (unordered:c@2 @0 @1))
6324  @2)
6325 (simplify
6326  (bit_and:c (ordered @0 @0) (ordered:c@2 @0 @1))
6327  @2)
6329 /* A & (2**N - 1) <= 2**K - 1 -> A & (2**N - 2**K) == 0
6330    A & (2**N - 1) >  2**K - 1 -> A & (2**N - 2**K) != 0
6332    Note that comparisons
6333      A & (2**N - 1) <  2**K   -> A & (2**N - 2**K) == 0
6334      A & (2**N - 1) >= 2**K   -> A & (2**N - 2**K) != 0
6335    will be canonicalized to above so there's no need to
6336    consider them here.
6337  */
6339 (for cmp (le gt)
6340      eqcmp (eq ne)
6341  (simplify
6342   (cmp (bit_and@0 @1 INTEGER_CST@2) INTEGER_CST@3)
6343   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)))
6344    (with
6345     {
6346      tree ty = TREE_TYPE (@0);
6347      unsigned prec = TYPE_PRECISION (ty);
6348      wide_int mask = wi::to_wide (@2, prec);
6349      wide_int rhs = wi::to_wide (@3, prec);
6350      signop sgn = TYPE_SIGN (ty);
6351     }
6352     (if ((mask & (mask + 1)) == 0 && wi::gt_p (rhs, 0, sgn)
6353          && (rhs & (rhs + 1)) == 0 && wi::ge_p (mask, rhs, sgn))
6354       (eqcmp (bit_and @1 { wide_int_to_tree (ty, mask - rhs); })
6355              { build_zero_cst (ty); }))))))
6357 /* -A CMP -B -> B CMP A.  */
6358 (for cmp (tcc_comparison)
6359      scmp (swapped_tcc_comparison)
6360  (simplify
6361   (cmp (negate @0) (negate @1))
6362   (if (FLOAT_TYPE_P (TREE_TYPE (@0))
6363        || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
6364            && (cmp == EQ_EXPR
6365                || cmp == NE_EXPR
6366                || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))))
6367    (scmp @0 @1)))
6368  (simplify
6369   (cmp (negate @0) CONSTANT_CLASS_P@1)
6370   (if (FLOAT_TYPE_P (TREE_TYPE (@0))
6371        || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
6372            && (cmp == EQ_EXPR
6373                || cmp == NE_EXPR
6374                || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))))
6375    (with { tree tem = const_unop (NEGATE_EXPR, TREE_TYPE (@0), @1); }
6376     (if (tem && !TREE_OVERFLOW (tem))
6377      (scmp @0 { tem; }))))))
6379 /* Convert ABS[U]_EXPR<x> == 0 or ABS[U]_EXPR<x> != 0 to x == 0 or x != 0.  */
6380 (for op (abs absu)
6381  (for eqne (eq ne)
6382   (simplify
6383    (eqne (op @0) zerop@1)
6384    (eqne @0 { build_zero_cst (TREE_TYPE (@0)); }))))
6386 /* From fold_sign_changed_comparison and fold_widened_comparison.
6387    FIXME: the lack of symmetry is disturbing.  */
6388 (for cmp (simple_comparison)
6389  (simplify
6390   (cmp (convert@0 @00) (convert?@1 @10))
6391   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6392        /* Disable this optimization if we're casting a function pointer
6393           type on targets that require function pointer canonicalization.  */
6394        && !(targetm.have_canonicalize_funcptr_for_compare ()
6395             && ((POINTER_TYPE_P (TREE_TYPE (@00))
6396                  && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@00))))
6397                 || (POINTER_TYPE_P (TREE_TYPE (@10))
6398                     && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@10))))))
6399        && single_use (@0))
6400    (if (TYPE_PRECISION (TREE_TYPE (@00)) == TYPE_PRECISION (TREE_TYPE (@0))
6401         && (TREE_CODE (@10) == INTEGER_CST
6402             || @1 != @10)
6403         && (TYPE_UNSIGNED (TREE_TYPE (@00)) == TYPE_UNSIGNED (TREE_TYPE (@0))
6404             || cmp == NE_EXPR
6405             || cmp == EQ_EXPR)
6406         && !POINTER_TYPE_P (TREE_TYPE (@00))
6407         /* (int)bool:32 != (int)uint is not the same as
6408            bool:32 != (bool:32)uint since boolean types only have two valid
6409            values independent of their precision.  */
6410         && (TREE_CODE (TREE_TYPE (@00)) != BOOLEAN_TYPE
6411             || TREE_CODE (TREE_TYPE (@10)) == BOOLEAN_TYPE))
6412     /* ???  The special-casing of INTEGER_CST conversion was in the original
6413        code and here to avoid a spurious overflow flag on the resulting
6414        constant which fold_convert produces.  */
6415     (if (TREE_CODE (@1) == INTEGER_CST)
6416      (cmp @00 { force_fit_type (TREE_TYPE (@00), wi::to_widest (@1), 0,
6417                                 TREE_OVERFLOW (@1)); })
6418      (cmp @00 (convert @1)))
6420     (if (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (TREE_TYPE (@00)))
6421      /* If possible, express the comparison in the shorter mode.  */
6422      (if ((cmp == EQ_EXPR || cmp == NE_EXPR
6423            || TYPE_UNSIGNED (TREE_TYPE (@0)) == TYPE_UNSIGNED (TREE_TYPE (@00))
6424            || (!TYPE_UNSIGNED (TREE_TYPE (@0))
6425                && TYPE_UNSIGNED (TREE_TYPE (@00))))
6426           && (types_match (TREE_TYPE (@10), TREE_TYPE (@00))
6427               || ((TYPE_PRECISION (TREE_TYPE (@00))
6428                    >= TYPE_PRECISION (TREE_TYPE (@10)))
6429                   && (TYPE_UNSIGNED (TREE_TYPE (@00))
6430                       == TYPE_UNSIGNED (TREE_TYPE (@10))))
6431               || (TREE_CODE (@10) == INTEGER_CST
6432                   && INTEGRAL_TYPE_P (TREE_TYPE (@00))
6433                   && int_fits_type_p (@10, TREE_TYPE (@00)))))
6434       (cmp @00 (convert @10))
6435       (if (TREE_CODE (@10) == INTEGER_CST
6436            && INTEGRAL_TYPE_P (TREE_TYPE (@00))
6437            && !int_fits_type_p (@10, TREE_TYPE (@00)))
6438        (with
6439         {
6440           tree min = lower_bound_in_type (TREE_TYPE (@10), TREE_TYPE (@00));
6441           tree max = upper_bound_in_type (TREE_TYPE (@10), TREE_TYPE (@00));
6442           bool above = integer_nonzerop (const_binop (LT_EXPR, type, max, @10));
6443           bool below = integer_nonzerop (const_binop (LT_EXPR, type, @10, min));
6444         }
6445         (if (above || below)
6446          (if (cmp == EQ_EXPR || cmp == NE_EXPR)
6447           { constant_boolean_node (cmp == EQ_EXPR ? false : true, type); }
6448           (if (cmp == LT_EXPR || cmp == LE_EXPR)
6449            { constant_boolean_node (above ? true : false, type); }
6450            (if (cmp == GT_EXPR || cmp == GE_EXPR)
6451             { constant_boolean_node (above ? false : true, type); })))))))))
6452    /* Fold (double)float1 CMP (double)float2 into float1 CMP float2.  */
6453    (if (FLOAT_TYPE_P (TREE_TYPE (@00))
6454         && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0))
6455             == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@00)))
6456         && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0))
6457             == DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@10))))
6458     (with
6459      {
6460        tree type1 = TREE_TYPE (@10);
6461        if (TREE_CODE (@10) == REAL_CST && !DECIMAL_FLOAT_TYPE_P (type1))
6462          {
6463            REAL_VALUE_TYPE orig = TREE_REAL_CST (@10);
6464            if (TYPE_PRECISION (type1) > TYPE_PRECISION (float_type_node)
6465                && exact_real_truncate (TYPE_MODE (float_type_node), &orig))
6466              type1 = float_type_node;
6467            if (TYPE_PRECISION (type1) > TYPE_PRECISION (double_type_node)
6468                && exact_real_truncate (TYPE_MODE (double_type_node), &orig))
6469              type1 = double_type_node;
6470          }
6471       tree newtype
6472         = (element_precision (TREE_TYPE (@00)) > element_precision (type1)
6473            ? TREE_TYPE (@00) : type1);
6474      }
6475      (if (element_precision (TREE_TYPE (@0)) > element_precision (newtype))
6476       (cmp (convert:newtype @00) (convert:newtype @10))))))))
6479 (for cmp (eq ne)
6480  (simplify
6481   /* SSA names are canonicalized to 2nd place.  */
6482   (cmp addr@0 SSA_NAME@1)
6483   (with
6484    {
6485      poly_int64 off; tree base;
6486      tree addr = (TREE_CODE (@0) == SSA_NAME
6487                   ? gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0)) : @0);
6488    }
6489    /* A local variable can never be pointed to by
6490       the default SSA name of an incoming parameter.  */
6491    (if (SSA_NAME_IS_DEFAULT_DEF (@1)
6492         && TREE_CODE (SSA_NAME_VAR (@1)) == PARM_DECL
6493         && (base = get_base_address (TREE_OPERAND (addr, 0)))
6494         && TREE_CODE (base) == VAR_DECL
6495         && auto_var_in_fn_p (base, current_function_decl))
6496     (if (cmp == NE_EXPR)
6497      { constant_boolean_node (true, type); }
6498      { constant_boolean_node (false, type); })
6499     /* If the address is based on @1 decide using the offset.  */
6500     (if ((base = get_addr_base_and_unit_offset (TREE_OPERAND (addr, 0), &off))
6501          && TREE_CODE (base) == MEM_REF
6502          && TREE_OPERAND (base, 0) == @1)
6503      (with { off += mem_ref_offset (base).force_shwi (); }
6504       (if (known_ne (off, 0))
6505        { constant_boolean_node (cmp == NE_EXPR, type); }
6506        (if (known_eq (off, 0))
6507         { constant_boolean_node (cmp == EQ_EXPR, type); }))))))))
6509 /* Equality compare simplifications from fold_binary  */
6510 (for cmp (eq ne)
6512  /* If we have (A | C) == D where C & ~D != 0, convert this into 0.
6513     Similarly for NE_EXPR.  */
6514  (simplify
6515   (cmp (convert?@3 (bit_ior @0 INTEGER_CST@1)) INTEGER_CST@2)
6516   (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0))
6517        && wi::bit_and_not (wi::to_wide (@1), wi::to_wide (@2)) != 0)
6518    { constant_boolean_node (cmp == NE_EXPR, type); }))
6520  /* (X ^ Y) == 0 becomes X == Y, and (X ^ Y) != 0 becomes X != Y.  */
6521  (simplify
6522   (cmp (bit_xor @0 @1) integer_zerop)
6523   (cmp @0 @1))
6525  /* (X ^ Y) == Y becomes X == 0.
6526     Likewise (X ^ Y) == X becomes Y == 0.  */
6527  (simplify
6528   (cmp:c (bit_xor:c @0 @1) @0)
6529   (cmp @1 { build_zero_cst (TREE_TYPE (@1)); }))
6531  /* (X & Y) == X becomes (X & ~Y) == 0.  */
6532  (simplify
6533   (cmp:c (bit_and:c @0 @1) @0)
6534   (cmp (bit_and @0 (bit_not! @1)) { build_zero_cst (TREE_TYPE (@0)); }))
6535  (simplify
6536   (cmp:c (convert@3 (bit_and (convert@2 @0) INTEGER_CST@1)) (convert @0))
6537   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6538        && INTEGRAL_TYPE_P (TREE_TYPE (@2))
6539        && INTEGRAL_TYPE_P (TREE_TYPE (@3))
6540        && TYPE_PRECISION (TREE_TYPE (@2)) == TYPE_PRECISION (TREE_TYPE (@0))
6541        && TYPE_PRECISION (TREE_TYPE (@3)) > TYPE_PRECISION (TREE_TYPE (@2))
6542        && !wi::neg_p (wi::to_wide (@1)))
6543    (cmp (bit_and @0 (convert (bit_not @1)))
6544         { build_zero_cst (TREE_TYPE (@0)); })))
6546  /* (X | Y) == Y becomes (X & ~Y) == 0.  */
6547  (simplify
6548   (cmp:c (bit_ior:c @0 @1) @1)
6549   (cmp (bit_and @0 (bit_not! @1)) { build_zero_cst (TREE_TYPE (@0)); }))
6551  /* (X ^ C1) op C2 can be rewritten as X op (C1 ^ C2).  */
6552  (simplify
6553   (cmp (convert?@3 (bit_xor @0 INTEGER_CST@1)) INTEGER_CST@2)
6554   (if (tree_nop_conversion_p (TREE_TYPE (@3), TREE_TYPE (@0)))
6555    (cmp @0 (bit_xor @1 (convert @2)))))
6557  (simplify
6558   (cmp (nop_convert? @0) integer_zerop)
6559   (if (tree_expr_nonzero_p (@0))
6560    { constant_boolean_node (cmp == NE_EXPR, type); }))
6562  /* (X & C) op (Y & C) into (X ^ Y) & C op 0.  */
6563  (simplify
6564   (cmp (bit_and:cs @0 @2) (bit_and:cs @1 @2))
6565   (cmp (bit_and (bit_xor @0 @1) @2) { build_zero_cst (TREE_TYPE (@2)); })))
6567 /* (X < 0) != (Y < 0) into (X ^ Y) < 0.
6568    (X >= 0) != (Y >= 0) into (X ^ Y) < 0.
6569    (X < 0) == (Y < 0) into (X ^ Y) >= 0.
6570    (X >= 0) == (Y >= 0) into (X ^ Y) >= 0.  */
6571 (for cmp (eq ne)
6572      ncmp (ge lt)
6573  (for sgncmp (ge lt)
6574   (simplify
6575    (cmp (sgncmp @0 integer_zerop@2) (sgncmp @1 integer_zerop))
6576    (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
6577         && !TYPE_UNSIGNED (TREE_TYPE (@0))
6578         && types_match (@0, @1))
6579     (ncmp (bit_xor @0 @1) @2)))))
6580 /* (X < 0) == (Y >= 0) into (X ^ Y) < 0.
6581    (X < 0) != (Y >= 0) into (X ^ Y) >= 0.  */
6582 (for cmp (eq ne)
6583      ncmp (lt ge)
6584  (simplify
6585   (cmp:c (lt @0 integer_zerop@2) (ge @1 integer_zerop))
6586    (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
6587         && !TYPE_UNSIGNED (TREE_TYPE (@0))
6588         && types_match (@0, @1))
6589     (ncmp (bit_xor @0 @1) @2))))
6591 /* If we have (A & C) == C where C is a power of 2, convert this into
6592    (A & C) != 0.  Similarly for NE_EXPR.  */
6593 (for cmp (eq ne)
6594      icmp (ne eq)
6595  (simplify
6596   (cmp (bit_and@2 @0 integer_pow2p@1) @1)
6597   (icmp @2 { build_zero_cst (TREE_TYPE (@0)); })))
6599 #if GIMPLE
6600 /* From fold_binary_op_with_conditional_arg handle the case of
6601    rewriting (a ? b : c) > d to a ? (b > d) : (c > d) when the
6602    compares simplify.  */
6603 (for cmp (simple_comparison)
6604  (simplify
6605   (cmp:c (cond @0 @1 @2) @3)
6606   /* Do not move possibly trapping operations into the conditional as this
6607      pessimizes code and causes gimplification issues when applied late.  */
6608   (if (!FLOAT_TYPE_P (TREE_TYPE (@3))
6609        || !operation_could_trap_p (cmp, true, false, @3))
6610    (cond @0 (cmp! @1 @3) (cmp! @2 @3)))))
6611 #endif
6613 (for cmp (ge lt)
6614 /* x < 0 ? ~y : y into (x >> (prec-1)) ^ y. */
6615 /* x >= 0 ? ~y : y into ~((x >> (prec-1)) ^ y). */
6616  (simplify
6617   (cond (cmp @0 integer_zerop) (bit_not @1) @1)
6618    (if (INTEGRAL_TYPE_P (type)
6619         && INTEGRAL_TYPE_P (TREE_TYPE (@0))
6620         && !TYPE_UNSIGNED (TREE_TYPE (@0))
6621         && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (type))
6622     (with
6623      {
6624        tree shifter = build_int_cst (integer_type_node, TYPE_PRECISION (type) - 1);
6625      }
6626     (if (cmp == LT_EXPR)
6627      (bit_xor (convert (rshift @0 {shifter;})) @1)
6628      (bit_not (bit_xor (convert (rshift @0 {shifter;})) @1))))))
6629 /* x < 0 ? y : ~y into ~((x >> (prec-1)) ^ y). */
6630 /* x >= 0 ? y : ~y into (x >> (prec-1)) ^ y. */
6631  (simplify
6632   (cond (cmp @0 integer_zerop) @1 (bit_not @1))
6633    (if (INTEGRAL_TYPE_P (type)
6634         && INTEGRAL_TYPE_P (TREE_TYPE (@0))
6635         && !TYPE_UNSIGNED (TREE_TYPE (@0))
6636         && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (type))
6637     (with
6638      {
6639        tree shifter = build_int_cst (integer_type_node, TYPE_PRECISION (type) - 1);
6640      }
6641     (if (cmp == GE_EXPR)
6642      (bit_xor (convert (rshift @0 {shifter;})) @1)
6643      (bit_not (bit_xor (convert (rshift @0 {shifter;})) @1)))))))
6645 /* If we have (A & C) != 0 ? D : 0 where C and D are powers of 2,
6646    convert this into a shift followed by ANDing with D.  */
6647 (simplify
6648  (cond
6649   (ne (bit_and @0 integer_pow2p@1) integer_zerop)
6650   INTEGER_CST@2 integer_zerop)
6651  (if (!POINTER_TYPE_P (type) && integer_pow2p (@2))
6652   (with {
6653      int shift = (wi::exact_log2 (wi::to_wide (@2))
6654                   - wi::exact_log2 (wi::to_wide (@1)));
6655    }
6656    (if (shift > 0)
6657     (bit_and
6658      (lshift (convert @0) { build_int_cst (integer_type_node, shift); }) @2)
6659     (bit_and
6660      (convert (rshift @0 { build_int_cst (integer_type_node, -shift); }))
6661      @2)))))
6663 /* If we have (A & C) != 0 where C is the sign bit of A, convert
6664    this into A < 0.  Similarly for (A & C) == 0 into A >= 0.  */
6665 (for cmp (eq ne)
6666      ncmp (ge lt)
6667  (simplify
6668   (cmp (bit_and (convert?@2 @0) integer_pow2p@1) integer_zerop)
6669   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6670        && type_has_mode_precision_p (TREE_TYPE (@0))
6671        && element_precision (@2) >= element_precision (@0)
6672        && wi::only_sign_bit_p (wi::to_wide (@1), element_precision (@0)))
6673    (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
6674     (ncmp (convert:stype @0) { build_zero_cst (stype); })))))
6676 /* If we have A < 0 ? C : 0 where C is a power of 2, convert
6677    this into a right shift or sign extension followed by ANDing with C.  */
6678 (simplify
6679  (cond
6680   (lt @0 integer_zerop)
6681   INTEGER_CST@1 integer_zerop)
6682  (if (integer_pow2p (@1)
6683       && !TYPE_UNSIGNED (TREE_TYPE (@0)))
6684   (with {
6685     int shift = element_precision (@0) - wi::exact_log2 (wi::to_wide (@1)) - 1;
6686    }
6687    (if (shift >= 0)
6688     (bit_and
6689      (convert (rshift @0 { build_int_cst (integer_type_node, shift); }))
6690      @1)
6691     /* Otherwise ctype must be wider than TREE_TYPE (@0) and pure
6692        sign extension followed by AND with C will achieve the effect.  */
6693     (bit_and (convert @0) @1)))))
6695 /* When the addresses are not directly of decls compare base and offset.
6696    This implements some remaining parts of fold_comparison address
6697    comparisons but still no complete part of it.  Still it is good
6698    enough to make fold_stmt not regress when not dispatching to fold_binary.  */
6699 (for cmp (simple_comparison)
6700  (simplify
6701   (cmp (convert1?@2 addr@0) (convert2? addr@1))
6702   (with
6703    {
6704      poly_int64 off0, off1;
6705      tree base0, base1;
6706      int equal = address_compare (cmp, TREE_TYPE (@2), @0, @1, base0, base1,
6707                                   off0, off1, GENERIC);
6708    }
6709    (if (equal == 1)
6710     (switch
6711      (if (cmp == EQ_EXPR && (known_eq (off0, off1) || known_ne (off0, off1)))
6712       { constant_boolean_node (known_eq (off0, off1), type); })
6713      (if (cmp == NE_EXPR && (known_eq (off0, off1) || known_ne (off0, off1)))
6714       { constant_boolean_node (known_ne (off0, off1), type); })
6715      (if (cmp == LT_EXPR && (known_lt (off0, off1) || known_ge (off0, off1)))
6716       { constant_boolean_node (known_lt (off0, off1), type); })
6717      (if (cmp == LE_EXPR && (known_le (off0, off1) || known_gt (off0, off1)))
6718       { constant_boolean_node (known_le (off0, off1), type); })
6719      (if (cmp == GE_EXPR && (known_ge (off0, off1) || known_lt (off0, off1)))
6720       { constant_boolean_node (known_ge (off0, off1), type); })
6721      (if (cmp == GT_EXPR && (known_gt (off0, off1) || known_le (off0, off1)))
6722       { constant_boolean_node (known_gt (off0, off1), type); }))
6723     (if (equal == 0)
6724      (switch
6725       (if (cmp == EQ_EXPR)
6726        { constant_boolean_node (false, type); })
6727       (if (cmp == NE_EXPR)
6728        { constant_boolean_node (true, type); })))))))
6730 #if GIMPLE
6731 /* a?~t:t -> (-(a))^t */
6732 (simplify
6733  (cond @0 @1 @2)
6734  (with { bool wascmp; }
6735   (if (INTEGRAL_TYPE_P (type)
6736        && bitwise_inverted_equal_p (@1, @2, wascmp)
6737        && (!wascmp || element_precision (type) == 1))
6738    (with {
6739      auto prec = TYPE_PRECISION (type);
6740      auto unsign = TYPE_UNSIGNED (type);
6741      tree inttype = build_nonstandard_integer_type (prec, unsign);
6742     }
6743     (convert (bit_xor (negate (convert:inttype @0)) (convert:inttype @2)))))))
6744 #endif
6746 /* Simplify pointer equality compares using PTA.  */
6747 (for neeq (ne eq)
6748  (simplify
6749   (neeq @0 @1)
6750   (if (POINTER_TYPE_P (TREE_TYPE (@0))
6751        && ptrs_compare_unequal (@0, @1))
6752    { constant_boolean_node (neeq != EQ_EXPR, type); })))
6754 /* PR70920: Transform (intptr_t)x eq/ne CST to x eq/ne (typeof x) CST.
6755    and (typeof ptr_cst) x eq/ne ptr_cst to x eq/ne (typeof x) CST.
6756    Disable the transform if either operand is pointer to function.
6757    This broke pr22051-2.c for arm where function pointer
6758    canonicalizaion is not wanted.  */
6760 (for cmp (ne eq)
6761  (simplify
6762   (cmp (convert @0) INTEGER_CST@1)
6763   (if (((POINTER_TYPE_P (TREE_TYPE (@0))
6764          && !FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@0)))
6765          && INTEGRAL_TYPE_P (TREE_TYPE (@1))
6766          /* Don't perform this optimization in GENERIC if @0 has reference
6767             type when sanitizing.  See PR101210.  */
6768          && !(GENERIC
6769               && TREE_CODE (TREE_TYPE (@0)) == REFERENCE_TYPE
6770               && (flag_sanitize & (SANITIZE_NULL | SANITIZE_ALIGNMENT))))
6771         || (INTEGRAL_TYPE_P (TREE_TYPE (@0))
6772             && POINTER_TYPE_P (TREE_TYPE (@1))
6773             && !FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (@1)))))
6774        && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (TREE_TYPE (@1)))
6775    (cmp @0 (convert @1)))))
6777 /* Non-equality compare simplifications from fold_binary  */
6778 (for cmp (lt gt le ge)
6779  /* Comparisons with the highest or lowest possible integer of
6780     the specified precision will have known values.  */
6781  (simplify
6782   (cmp (convert?@2 @0) uniform_integer_cst_p@1)
6783   (if ((INTEGRAL_TYPE_P (TREE_TYPE (@1))
6784         || POINTER_TYPE_P (TREE_TYPE (@1))
6785         || VECTOR_INTEGER_TYPE_P (TREE_TYPE (@1)))
6786        && tree_nop_conversion_p (TREE_TYPE (@2), TREE_TYPE (@0)))
6787    (with
6788     {
6789       tree cst = uniform_integer_cst_p (@1);
6790       tree arg1_type = TREE_TYPE (cst);
6791       unsigned int prec = TYPE_PRECISION (arg1_type);
6792       wide_int max = wi::max_value (arg1_type);
6793       wide_int signed_max = wi::max_value (prec, SIGNED);
6794       wide_int min = wi::min_value (arg1_type);
6795     }
6796     (switch
6797      (if (wi::to_wide (cst) == max)
6798       (switch
6799        (if (cmp == GT_EXPR)
6800         { constant_boolean_node (false, type); })
6801        (if (cmp == GE_EXPR)
6802         (eq @2 @1))
6803        (if (cmp == LE_EXPR)
6804         { constant_boolean_node (true, type); })
6805        (if (cmp == LT_EXPR)
6806         (ne @2 @1))))
6807      (if (wi::to_wide (cst) == min)
6808       (switch
6809        (if (cmp == LT_EXPR)
6810         { constant_boolean_node (false, type); })
6811        (if (cmp == LE_EXPR)
6812         (eq @2 @1))
6813        (if (cmp == GE_EXPR)
6814         { constant_boolean_node (true, type); })
6815        (if (cmp == GT_EXPR)
6816         (ne @2 @1))))
6817      (if (wi::to_wide (cst) == max - 1)
6818       (switch
6819        (if (cmp == GT_EXPR)
6820         (eq @2 { build_uniform_cst (TREE_TYPE (@1),
6821                                     wide_int_to_tree (TREE_TYPE (cst),
6822                                                       wi::to_wide (cst)
6823                                                       + 1)); }))
6824        (if (cmp == LE_EXPR)
6825         (ne @2 { build_uniform_cst (TREE_TYPE (@1),
6826                                     wide_int_to_tree (TREE_TYPE (cst),
6827                                                       wi::to_wide (cst)
6828                                                       + 1)); }))))
6829      (if (wi::to_wide (cst) == min + 1)
6830       (switch
6831        (if (cmp == GE_EXPR)
6832         (ne @2 { build_uniform_cst (TREE_TYPE (@1),
6833                                     wide_int_to_tree (TREE_TYPE (cst),
6834                                                       wi::to_wide (cst)
6835                                                       - 1)); }))
6836        (if (cmp == LT_EXPR)
6837         (eq @2 { build_uniform_cst (TREE_TYPE (@1),
6838                                     wide_int_to_tree (TREE_TYPE (cst),
6839                                                       wi::to_wide (cst)
6840                                                       - 1)); }))))
6841      (if (wi::to_wide (cst) == signed_max
6842           && TYPE_UNSIGNED (arg1_type)
6843           && TYPE_MODE (arg1_type) != BLKmode
6844           /* We will flip the signedness of the comparison operator
6845              associated with the mode of @1, so the sign bit is
6846              specified by this mode.  Check that @1 is the signed
6847              max associated with this sign bit.  */
6848           && prec == GET_MODE_PRECISION (SCALAR_INT_TYPE_MODE (arg1_type))
6849           /* signed_type does not work on pointer types.  */
6850           && INTEGRAL_TYPE_P (arg1_type))
6851       /* The following case also applies to X < signed_max+1
6852          and X >= signed_max+1 because previous transformations.  */
6853       (if (cmp == LE_EXPR || cmp == GT_EXPR)
6854        (with { tree st = signed_type_for (TREE_TYPE (@1)); }
6855         (switch
6856          (if (cst == @1 && cmp == LE_EXPR)
6857           (ge (convert:st @0) { build_zero_cst (st); }))
6858          (if (cst == @1 && cmp == GT_EXPR)
6859           (lt (convert:st @0) { build_zero_cst (st); }))
6860          (if (cmp == LE_EXPR)
6861           (ge (view_convert:st @0) { build_zero_cst (st); }))
6862          (if (cmp == GT_EXPR)
6863           (lt (view_convert:st @0) { build_zero_cst (st); })))))))))))
6865 /* unsigned < (typeof unsigned)(unsigned != 0) is always false.  */
6866 (simplify
6867  (lt:c @0 (convert (ne @0 integer_zerop)))
6868  (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
6869   { constant_boolean_node (false, type); }))
6871 /* x != (typeof x)(x == CST) -> CST == 0 ? 1 : (CST == 1 ? (x!=0&&x!=1) : x != 0) */
6872 /* x != (typeof x)(x != CST) -> CST == 1 ? 1 : (CST == 0 ? (x!=0&&x!=1) : x != 1) */
6873 /* x == (typeof x)(x == CST) -> CST == 0 ? 0 : (CST == 1 ? (x==0||x==1) : x == 0) */
6874 /* x == (typeof x)(x != CST) -> CST == 1 ? 0 : (CST == 0 ? (x==0||x==1) : x == 1) */
6875 (for outer (ne eq)
6876  (for inner (ne eq)
6877   (simplify
6878    (outer:c @0 (convert (inner @0 INTEGER_CST@1)))
6879    (with {
6880      bool cst1 = integer_onep (@1);
6881      bool cst0 = integer_zerop (@1);
6882      bool innereq = inner == EQ_EXPR;
6883      bool outereq = outer == EQ_EXPR;
6884     }
6885     (switch
6886      (if (innereq ? cst0 : cst1)
6887       { constant_boolean_node (!outereq, type); })
6888      (if (innereq ? cst1 : cst0)
6889       (with {
6890         tree utype = unsigned_type_for (TREE_TYPE (@0));
6891         tree ucst1 = build_one_cst (utype);
6892        }
6893        (if (!outereq)
6894         (gt (convert:utype @0) { ucst1; })
6895         (le (convert:utype @0) { ucst1; })
6896        )
6897       )
6898      )
6899      (with {
6900        tree value = build_int_cst (TREE_TYPE (@0), !innereq);
6901       }
6902       (if (outereq)
6903        (eq @0 { value; })
6904        (ne @0 { value; })
6905       )
6906      )
6907     )
6908    )
6909   )
6913 (for cmp (unordered ordered unlt unle ungt unge uneq ltgt)
6914  /* If the second operand is NaN, the result is constant.  */
6915  (simplify
6916   (cmp @0 REAL_CST@1)
6917   (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
6918        && (cmp != LTGT_EXPR || ! flag_trapping_math))
6919    { constant_boolean_node (cmp == ORDERED_EXPR || cmp == LTGT_EXPR
6920                             ? false : true, type); })))
6922 /* Fold UNORDERED if either operand must be NaN, or neither can be.  */
6923 (simplify
6924   (unordered @0 @1)
6925   (switch
6926     (if (tree_expr_nan_p (@0) || tree_expr_nan_p (@1))
6927         { constant_boolean_node (true, type); })
6928     (if (!tree_expr_maybe_nan_p (@0) && !tree_expr_maybe_nan_p (@1))
6929         { constant_boolean_node (false, type); })))
6931 /* Fold ORDERED if either operand must be NaN, or neither can be.  */
6932 (simplify
6933   (ordered @0 @1)
6934   (switch
6935     (if (tree_expr_nan_p (@0) || tree_expr_nan_p (@1))
6936         { constant_boolean_node (false, type); })
6937     (if (!tree_expr_maybe_nan_p (@0) && !tree_expr_maybe_nan_p (@1))
6938         { constant_boolean_node (true, type); })))
6940 /* bool_var != 0 becomes bool_var.  */
6941 (simplify
6942  (ne @0 integer_zerop)
6943  (if (TREE_CODE (TREE_TYPE (@0)) == BOOLEAN_TYPE
6944       && types_match (type, TREE_TYPE (@0)))
6945   (non_lvalue @0)))
6946 /* bool_var == 1 becomes bool_var.  */
6947 (simplify
6948  (eq @0 integer_onep)
6949  (if (TREE_CODE (TREE_TYPE (@0)) == BOOLEAN_TYPE
6950       && types_match (type, TREE_TYPE (@0)))
6951   (non_lvalue @0)))
6952 /* Do not handle
6953    bool_var == 0 becomes !bool_var or
6954    bool_var != 1 becomes !bool_var
6955    here because that only is good in assignment context as long
6956    as we require a tcc_comparison in GIMPLE_CONDs where we'd
6957    replace if (x == 0) with tem = ~x; if (tem != 0) which is
6958    clearly less optimal and which we'll transform again in forwprop.  */
6960 /* Transform comparisons of the form (X & Y) CMP 0 to X CMP2 Z
6961    where ~Y + 1 == pow2 and Z = ~Y.  */
6962 (for cst (VECTOR_CST INTEGER_CST)
6963  (for cmp (eq ne)
6964       icmp (le gt)
6965   (simplify
6966    (cmp (bit_and:c@2 @0 cst@1) integer_zerop)
6967     (with { tree csts = bitmask_inv_cst_vector_p (@1); }
6968      (if (csts && (VECTOR_TYPE_P (TREE_TYPE (@1)) || single_use (@2)))
6969       (with { auto optab = VECTOR_TYPE_P (TREE_TYPE (@1))
6970                          ? optab_vector : optab_default;
6971               tree utype = unsigned_type_for (TREE_TYPE (@1)); }
6972        (if (target_supports_op_p (utype, icmp, optab)
6973             || (optimize_vectors_before_lowering_p ()
6974                 && (!target_supports_op_p (type, cmp, optab)
6975                     || !target_supports_op_p (type, BIT_AND_EXPR, optab))))
6976         (if (TYPE_UNSIGNED (TREE_TYPE (@1)))
6977          (icmp @0 { csts; })
6978          (icmp (view_convert:utype @0) { csts; })))))))))
6980 /* When one argument is a constant, overflow detection can be simplified.
6981    Currently restricted to single use so as not to interfere too much with
6982    ADD_OVERFLOW detection in tree-ssa-math-opts.cc.
6983    CONVERT?(CONVERT?(A) + CST) CMP A  ->  A CMP' CST' */
6984 (for cmp (lt le ge gt)
6985      out (gt gt le le)
6986  (simplify
6987   (cmp:c (convert?@3 (plus@2 (convert?@4 @0) INTEGER_CST@1)) @0)
6988   (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@2))
6989        && types_match (TREE_TYPE (@0), TREE_TYPE (@3))
6990        && tree_nop_conversion_p (TREE_TYPE (@4), TREE_TYPE (@0))
6991        && wi::to_wide (@1) != 0
6992        && single_use (@2))
6993    (with {
6994      unsigned int prec = TYPE_PRECISION (TREE_TYPE (@0));
6995      signop sign = TYPE_SIGN (TREE_TYPE (@0));
6996     }
6997     (out @0 { wide_int_to_tree (TREE_TYPE (@0),
6998                                 wi::max_value (prec, sign)
6999                                 - wi::to_wide (@1)); })))))
7001 /* To detect overflow in unsigned A - B, A < B is simpler than A - B > A.
7002    However, the detection logic for SUB_OVERFLOW in tree-ssa-math-opts.cc
7003    expects the long form, so we restrict the transformation for now.  */
7004 (for cmp (gt le)
7005  (simplify
7006   (cmp:c (minus@2 @0 @1) @0)
7007   (if (single_use (@2)
7008        && ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
7009        && TYPE_UNSIGNED (TREE_TYPE (@0)))
7010    (cmp @1 @0))))
7012 /* Optimize A - B + -1 >= A into B >= A for unsigned comparisons.  */
7013 (for cmp (ge lt)
7014  (simplify
7015   (cmp:c (plus (minus @0 @1) integer_minus_onep) @0)
7016    (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
7017         && TYPE_UNSIGNED (TREE_TYPE (@0)))
7018     (cmp @1 @0))))
7020 /* Testing for overflow is unnecessary if we already know the result.  */
7021 /* A - B > A  */
7022 (for cmp (gt le)
7023      out (ne eq)
7024  (simplify
7025   (cmp:c (realpart (IFN_SUB_OVERFLOW@2 @0 @1)) @0)
7026   (if (TYPE_UNSIGNED (TREE_TYPE (@0))
7027        && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
7028    (out (imagpart @2) { build_zero_cst (TREE_TYPE (@0)); }))))
7029 /* A + B < A  */
7030 (for cmp (lt ge)
7031      out (ne eq)
7032  (simplify
7033   (cmp:c (realpart (IFN_ADD_OVERFLOW:c@2 @0 @1)) @0)
7034   (if (TYPE_UNSIGNED (TREE_TYPE (@0))
7035        && types_match (TREE_TYPE (@0), TREE_TYPE (@1)))
7036    (out (imagpart @2) { build_zero_cst (TREE_TYPE (@0)); }))))
7038 /* For unsigned operands, -1 / B < A checks whether A * B would overflow.
7039    Simplify it to __builtin_mul_overflow (A, B, <unused>).  */
7040 (for cmp (lt ge)
7041      out (ne eq)
7042  (simplify
7043   (cmp:c (trunc_div:s integer_all_onesp @1) @0)
7044   (if (TYPE_UNSIGNED (TREE_TYPE (@0)) && !VECTOR_TYPE_P (TREE_TYPE (@0)))
7045    (with { tree t = TREE_TYPE (@0), cpx = build_complex_type (t); }
7046     (out (imagpart (IFN_MUL_OVERFLOW:cpx @0 @1)) { build_zero_cst (t); })))))
7048 /* Similarly, for unsigned operands, (((type) A * B) >> prec) != 0 where type
7049    is at least twice as wide as type of A and B, simplify to
7050    __builtin_mul_overflow (A, B, <unused>).  */
7051 (for cmp (eq ne)
7052  (simplify
7053   (cmp (rshift (mult:s (convert@3 @0) (convert @1)) INTEGER_CST@2)
7054        integer_zerop)
7055   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
7056        && INTEGRAL_TYPE_P (TREE_TYPE (@3))
7057        && TYPE_UNSIGNED (TREE_TYPE (@0))
7058        && (TYPE_PRECISION (TREE_TYPE (@3))
7059            >= 2 * TYPE_PRECISION (TREE_TYPE (@0)))
7060        && tree_fits_uhwi_p (@2)
7061        && tree_to_uhwi (@2) == TYPE_PRECISION (TREE_TYPE (@0))
7062        && types_match (@0, @1)
7063        && type_has_mode_precision_p (TREE_TYPE (@0))
7064        && (optab_handler (umulv4_optab, TYPE_MODE (TREE_TYPE (@0)))
7065            != CODE_FOR_nothing))
7066    (with { tree t = TREE_TYPE (@0), cpx = build_complex_type (t); }
7067     (cmp (imagpart (IFN_MUL_OVERFLOW:cpx @0 @1)) { build_zero_cst (t); })))))
7069 /* Demote operands of IFN_{ADD,SUB,MUL}_OVERFLOW.  */
7070 (for ovf (IFN_ADD_OVERFLOW IFN_SUB_OVERFLOW IFN_MUL_OVERFLOW)
7071  (simplify
7072   (ovf (convert@2 @0) @1)
7073   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
7074        && INTEGRAL_TYPE_P (TREE_TYPE (@2))
7075        && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
7076        && (!TYPE_UNSIGNED (TREE_TYPE (@2)) || TYPE_UNSIGNED (TREE_TYPE (@0))))
7077    (ovf @0 @1)))
7078  (simplify
7079   (ovf @1 (convert@2 @0))
7080   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
7081        && INTEGRAL_TYPE_P (TREE_TYPE (@2))
7082        && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
7083        && (!TYPE_UNSIGNED (TREE_TYPE (@2)) || TYPE_UNSIGNED (TREE_TYPE (@0))))
7084    (ovf @1 @0))))
7086 /* Optimize __builtin_mul_overflow_p (x, cst, (utype) 0) if all 3 types
7087    are unsigned to x > (umax / cst).  Similarly for signed type, but
7088    in that case it needs to be outside of a range.  */
7089 (simplify
7090  (imagpart (IFN_MUL_OVERFLOW:cs@2 @0 integer_nonzerop@1))
7091   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
7092        && TYPE_MAX_VALUE (TREE_TYPE (@0))
7093        && types_match (TREE_TYPE (@0), TREE_TYPE (TREE_TYPE (@2)))
7094        && int_fits_type_p (@1, TREE_TYPE (@0)))
7095    (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
7096     (convert (gt @0 (trunc_div! { TYPE_MAX_VALUE (TREE_TYPE (@0)); } @1)))
7097     (if (TYPE_MIN_VALUE (TREE_TYPE (@0)))
7098      (if (integer_minus_onep (@1))
7099       (convert (eq @0 { TYPE_MIN_VALUE (TREE_TYPE (@0)); }))
7100       (with
7101        {
7102          tree div = fold_convert (TREE_TYPE (@0), @1);
7103          tree lo = int_const_binop (TRUNC_DIV_EXPR,
7104                                     TYPE_MIN_VALUE (TREE_TYPE (@0)), div);
7105          tree hi = int_const_binop (TRUNC_DIV_EXPR,
7106                                     TYPE_MAX_VALUE (TREE_TYPE (@0)), div);
7107          tree etype = range_check_type (TREE_TYPE (@0));
7108          if (etype)
7109            {
7110              if (wi::neg_p (wi::to_wide (div)))
7111                std::swap (lo, hi);
7112              lo = fold_convert (etype, lo);
7113              hi = fold_convert (etype, hi);
7114              hi = int_const_binop (MINUS_EXPR, hi, lo);
7115            }
7116        }
7117        (if (etype)
7118         (convert (gt (minus (convert:etype @0) { lo; }) { hi; })))))))))
7120 /* Simplification of math builtins.  These rules must all be optimizations
7121    as well as IL simplifications.  If there is a possibility that the new
7122    form could be a pessimization, the rule should go in the canonicalization
7123    section that follows this one.
7125    Rules can generally go in this section if they satisfy one of
7126    the following:
7128    - the rule describes an identity
7130    - the rule replaces calls with something as simple as addition or
7131      multiplication
7133    - the rule contains unary calls only and simplifies the surrounding
7134      arithmetic.  (The idea here is to exclude non-unary calls in which
7135      one operand is constant and in which the call is known to be cheap
7136      when the operand has that value.)  */
7138 (if (flag_unsafe_math_optimizations)
7139  /* Simplify sqrt(x) * sqrt(x) -> x.  */
7140  (simplify
7141   (mult (SQRT_ALL@1 @0) @1)
7142   (if (!tree_expr_maybe_signaling_nan_p (@0))
7143    @0))
7145  (for op (plus minus)
7146   /* Simplify (A / C) +- (B / C) -> (A +- B) / C.  */
7147   (simplify
7148    (op (rdiv @0 @1)
7149        (rdiv @2 @1))
7150    (rdiv (op @0 @2) @1)))
7152  (for cmp (lt le gt ge)
7153       neg_cmp (gt ge lt le)
7154   /* Simplify (x * C1) cmp C2 -> x cmp (C2 / C1), where C1 != 0.  */
7155   (simplify
7156    (cmp (mult @0 REAL_CST@1) REAL_CST@2)
7157    (with
7158     { tree tem = const_binop (RDIV_EXPR, type, @2, @1); }
7159     (if (tem
7160          && !(REAL_VALUE_ISINF (TREE_REAL_CST (tem))
7161               || (real_zerop (tem) && !real_zerop (@1))))
7162      (switch
7163       (if (real_less (&dconst0, TREE_REAL_CST_PTR (@1)))
7164        (cmp @0 { tem; }))
7165       (if (real_less (TREE_REAL_CST_PTR (@1), &dconst0))
7166        (neg_cmp @0 { tem; })))))))
7168  /* Simplify sqrt(x) * sqrt(y) -> sqrt(x*y).  */
7169  (for root (SQRT CBRT)
7170   (simplify
7171    (mult (root:s @0) (root:s @1))
7172     (root (mult @0 @1))))
7174  /* Simplify expN(x) * expN(y) -> expN(x+y). */
7175  (for exps (EXP EXP2 EXP10 POW10)
7176   (simplify
7177    (mult (exps:s @0) (exps:s @1))
7178     (exps (plus @0 @1))))
7180  /* Simplify a/root(b/c) into a*root(c/b).  */
7181  (for root (SQRT CBRT)
7182   (simplify
7183    (rdiv @0 (root:s (rdiv:s @1 @2)))
7184     (mult @0 (root (rdiv @2 @1)))))
7186  /* Simplify x/expN(y) into x*expN(-y).  */
7187  (for exps (EXP EXP2 EXP10 POW10)
7188   (simplify
7189    (rdiv @0 (exps:s @1))
7190     (mult @0 (exps (negate @1)))))
7192  (for logs (LOG LOG2 LOG10 LOG10)
7193       exps (EXP EXP2 EXP10 POW10)
7194   /* logN(expN(x)) -> x.  */
7195   (simplify
7196    (logs (exps @0))
7197    @0)
7198   /* expN(logN(x)) -> x.  */
7199   (simplify
7200    (exps (logs @0))
7201    @0))
7203  /* Optimize logN(func()) for various exponential functions.  We
7204     want to determine the value "x" and the power "exponent" in
7205     order to transform logN(x**exponent) into exponent*logN(x).  */
7206  (for logs (LOG  LOG   LOG   LOG2 LOG2  LOG2  LOG10 LOG10)
7207       exps (EXP2 EXP10 POW10 EXP  EXP10 POW10 EXP   EXP2)
7208   (simplify
7209    (logs (exps @0))
7210    (if (SCALAR_FLOAT_TYPE_P (type))
7211     (with {
7212       tree x;
7213       switch (exps)
7214         {
7215         CASE_CFN_EXP:
7216           /* Prepare to do logN(exp(exponent)) -> exponent*logN(e).  */
7217           x = build_real_truncate (type, dconst_e ());
7218           break;
7219         CASE_CFN_EXP2:
7220           /* Prepare to do logN(exp2(exponent)) -> exponent*logN(2).  */
7221           x = build_real (type, dconst2);
7222           break;
7223         CASE_CFN_EXP10:
7224         CASE_CFN_POW10:
7225           /* Prepare to do logN(exp10(exponent)) -> exponent*logN(10).  */
7226           {
7227             REAL_VALUE_TYPE dconst10;
7228             real_from_integer (&dconst10, VOIDmode, 10, SIGNED);
7229             x = build_real (type, dconst10);
7230           }
7231           break;
7232         default:
7233           gcc_unreachable ();
7234         }
7235       }
7236      (mult (logs { x; }) @0)))))
7238  (for logs (LOG LOG
7239             LOG2 LOG2
7240             LOG10 LOG10)
7241       exps (SQRT CBRT)
7242   (simplify
7243    (logs (exps @0))
7244    (if (SCALAR_FLOAT_TYPE_P (type))
7245     (with {
7246       tree x;
7247       switch (exps)
7248         {
7249         CASE_CFN_SQRT:
7250           /* Prepare to do logN(sqrt(x)) -> 0.5*logN(x).  */
7251           x = build_real (type, dconsthalf);
7252           break;
7253         CASE_CFN_CBRT:
7254           /* Prepare to do logN(cbrt(x)) -> (1/3)*logN(x).  */
7255           x = build_real_truncate (type, dconst_third ());
7256           break;
7257         default:
7258           gcc_unreachable ();
7259         }
7260       }
7261      (mult { x; } (logs @0))))))
7263  /* logN(pow(x,exponent)) -> exponent*logN(x).  */
7264  (for logs (LOG LOG2 LOG10)
7265       pows (POW)
7266   (simplify
7267    (logs (pows @0 @1))
7268    (mult @1 (logs @0))))
7270  /* pow(C,x) -> exp(log(C)*x) if C > 0,
7271     or if C is a positive power of 2,
7272     pow(C,x) -> exp2(log2(C)*x).  */
7273 #if GIMPLE
7274  (for pows (POW)
7275       exps (EXP)
7276       logs (LOG)
7277       exp2s (EXP2)
7278       log2s (LOG2)
7279   (simplify
7280    (pows REAL_CST@0 @1)
7281    (if (real_compare (GT_EXPR, TREE_REAL_CST_PTR (@0), &dconst0)
7282         && real_isfinite (TREE_REAL_CST_PTR (@0))
7283         /* As libmvec doesn't have a vectorized exp2, defer optimizing
7284            the use_exp2 case until after vectorization.  It seems actually
7285            beneficial for all constants to postpone this until later,
7286            because exp(log(C)*x), while faster, will have worse precision
7287            and if x folds into a constant too, that is unnecessary
7288            pessimization.  */
7289         && canonicalize_math_after_vectorization_p ())
7290     (with {
7291        const REAL_VALUE_TYPE *const value = TREE_REAL_CST_PTR (@0);
7292        bool use_exp2 = false;
7293        if (targetm.libc_has_function (function_c99_misc, TREE_TYPE (@0))
7294            && value->cl == rvc_normal)
7295          {
7296            REAL_VALUE_TYPE frac_rvt = *value;
7297            SET_REAL_EXP (&frac_rvt, 1);
7298            if (real_equal (&frac_rvt, &dconst1))
7299              use_exp2 = true;
7300          }
7301      }
7302      (if (!use_exp2)
7303       (if (optimize_pow_to_exp (@0, @1))
7304        (exps (mult (logs @0) @1)))
7305       (exp2s (mult (log2s @0) @1)))))))
7306 #endif
7308  /* pow(C,x)*expN(y) -> expN(logN(C)*x+y) if C > 0.  */
7309  (for pows (POW)
7310       exps (EXP EXP2 EXP10 POW10)
7311       logs (LOG LOG2 LOG10 LOG10)
7312   (simplify
7313    (mult:c (pows:s REAL_CST@0 @1) (exps:s @2))
7314    (if (real_compare (GT_EXPR, TREE_REAL_CST_PTR (@0), &dconst0)
7315         && real_isfinite (TREE_REAL_CST_PTR (@0)))
7316     (exps (plus (mult (logs @0) @1) @2)))))
7318  (for sqrts (SQRT)
7319       cbrts (CBRT)
7320       pows (POW)
7321       exps (EXP EXP2 EXP10 POW10)
7322   /* sqrt(expN(x)) -> expN(x*0.5).  */
7323   (simplify
7324    (sqrts (exps @0))
7325    (exps (mult @0 { build_real (type, dconsthalf); })))
7326   /* cbrt(expN(x)) -> expN(x/3).  */
7327   (simplify
7328    (cbrts (exps @0))
7329    (exps (mult @0 { build_real_truncate (type, dconst_third ()); })))
7330   /* pow(expN(x), y) -> expN(x*y).  */
7331   (simplify
7332    (pows (exps @0) @1)
7333    (exps (mult @0 @1))))
7335  /* tan(atan(x)) -> x.  */
7336  (for tans (TAN)
7337       atans (ATAN)
7338   (simplify
7339    (tans (atans @0))
7340    @0)))
7342  /* Simplify sin(atan(x)) -> x / sqrt(x*x + 1). */
7343  (for sins (SIN)
7344       atans (ATAN)
7345       sqrts (SQRT)
7346       copysigns (COPYSIGN)
7347   (simplify
7348    (sins (atans:s @0))
7349    (with
7350      {
7351       REAL_VALUE_TYPE r_cst;
7352       build_sinatan_real (&r_cst, type);
7353       tree t_cst = build_real (type, r_cst);
7354       tree t_one = build_one_cst (type);
7355      }
7356     (if (SCALAR_FLOAT_TYPE_P (type))
7357      (cond (lt (abs @0) { t_cst; })
7358       (rdiv @0 (sqrts (plus (mult @0 @0) { t_one; })))
7359       (copysigns { t_one; } @0))))))
7361 /* Simplify cos(atan(x)) -> 1 / sqrt(x*x + 1). */
7362  (for coss (COS)
7363       atans (ATAN)
7364       sqrts (SQRT)
7365       copysigns (COPYSIGN)
7366   (simplify
7367    (coss (atans:s @0))
7368    (with
7369      {
7370       REAL_VALUE_TYPE r_cst;
7371       build_sinatan_real (&r_cst, type);
7372       tree t_cst = build_real (type, r_cst);
7373       tree t_one = build_one_cst (type);
7374       tree t_zero = build_zero_cst (type);
7375      }
7376     (if (SCALAR_FLOAT_TYPE_P (type))
7377      (cond (lt (abs @0) { t_cst; })
7378       (rdiv { t_one; } (sqrts (plus (mult @0 @0) { t_one; })))
7379       (copysigns { t_zero; } @0))))))
7381  (if (!flag_errno_math)
7382   /* Simplify sinh(atanh(x)) -> x / sqrt((1 - x)*(1 + x)). */
7383   (for sinhs (SINH)
7384        atanhs (ATANH)
7385        sqrts (SQRT)
7386    (simplify
7387     (sinhs (atanhs:s @0))
7388     (with { tree t_one = build_one_cst (type); }
7389     (rdiv @0 (sqrts (mult (minus { t_one; } @0) (plus { t_one; } @0)))))))
7391   /* Simplify cosh(atanh(x)) -> 1 / sqrt((1 - x)*(1 + x)) */
7392   (for coshs (COSH)
7393        atanhs (ATANH)
7394        sqrts (SQRT)
7395    (simplify
7396     (coshs (atanhs:s @0))
7397     (with { tree t_one = build_one_cst (type); }
7398     (rdiv { t_one; } (sqrts (mult (minus { t_one; } @0) (plus { t_one; } @0))))))))
7400 /* cabs(x+0i) or cabs(0+xi) -> abs(x).  */
7401 (simplify
7402  (CABS (complex:C @0 real_zerop@1))
7403  (abs @0))
7405 /* trunc(trunc(x)) -> trunc(x), etc.  */
7406 (for fns (TRUNC_ALL FLOOR_ALL CEIL_ALL ROUND_ALL NEARBYINT_ALL RINT_ALL)
7407  (simplify
7408   (fns (fns @0))
7409   (fns @0)))
7410 /* f(x) -> x if x is integer valued and f does nothing for such values.  */
7411 (for fns (TRUNC_ALL FLOOR_ALL CEIL_ALL ROUND_ALL NEARBYINT_ALL RINT_ALL)
7412  (simplify
7413   (fns integer_valued_real_p@0)
7414   @0))
7416 /* hypot(x,0) and hypot(0,x) -> abs(x).  */
7417 (simplify
7418  (HYPOT:c @0 real_zerop@1)
7419  (abs @0))
7421 /* pow(1,x) -> 1.  */
7422 (simplify
7423  (POW real_onep@0 @1)
7424  @0)
7426 (simplify
7427  /* copysign(x,x) -> x.  */
7428  (COPYSIGN_ALL @0 @0)
7429  @0)
7431 (simplify
7432  /* copysign(x,-x) -> -x.  */
7433  (COPYSIGN_ALL @0 (negate@1 @0))
7434  @1)
7436 (simplify
7437  /* copysign(x,y) -> fabs(x) if y is nonnegative.  */
7438  (COPYSIGN_ALL @0 tree_expr_nonnegative_p@1)
7439  (abs @0))
7441 (for scale (LDEXP SCALBN SCALBLN)
7442  /* ldexp(0, x) -> 0.  */
7443  (simplify
7444   (scale real_zerop@0 @1)
7445   @0)
7446  /* ldexp(x, 0) -> x.  */
7447  (simplify
7448   (scale @0 integer_zerop@1)
7449   @0)
7450  /* ldexp(x, y) -> x if x is +-Inf or NaN.  */
7451  (simplify
7452   (scale REAL_CST@0 @1)
7453   (if (!real_isfinite (TREE_REAL_CST_PTR (@0)))
7454    @0)))
7456 /* Canonicalization of sequences of math builtins.  These rules represent
7457    IL simplifications but are not necessarily optimizations.
7459    The sincos pass is responsible for picking "optimal" implementations
7460    of math builtins, which may be more complicated and can sometimes go
7461    the other way, e.g. converting pow into a sequence of sqrts.
7462    We only want to do these canonicalizations before the pass has run.  */
7464 (if (flag_unsafe_math_optimizations && canonicalize_math_p ())
7465  /* Simplify tan(x) * cos(x) -> sin(x). */
7466  (simplify
7467   (mult:c (TAN:s @0) (COS:s @0))
7468    (SIN @0))
7470  /* Simplify x * pow(x,c) -> pow(x,c+1). */
7471  (simplify
7472   (mult:c @0 (POW:s @0 REAL_CST@1))
7473   (if (!TREE_OVERFLOW (@1))
7474    (POW @0 (plus @1 { build_one_cst (type); }))))
7476  /* Simplify sin(x) / cos(x) -> tan(x). */
7477  (simplify
7478   (rdiv (SIN:s @0) (COS:s @0))
7479    (TAN @0))
7481  /* Simplify sinh(x) / cosh(x) -> tanh(x). */
7482  (simplify
7483   (rdiv (SINH:s @0) (COSH:s @0))
7484    (TANH @0))
7486  /* Simplify tanh (x) / sinh (x) -> 1.0 / cosh (x). */
7487  (simplify
7488    (rdiv (TANH:s @0) (SINH:s @0))
7489    (rdiv {build_one_cst (type);} (COSH @0)))
7491  /* Simplify cos(x) / sin(x) -> 1 / tan(x). */
7492  (simplify
7493   (rdiv (COS:s @0) (SIN:s @0))
7494    (rdiv { build_one_cst (type); } (TAN @0)))
7496  /* Simplify sin(x) / tan(x) -> cos(x). */
7497  (simplify
7498   (rdiv (SIN:s @0) (TAN:s @0))
7499   (if (! HONOR_NANS (@0)
7500        && ! HONOR_INFINITIES (@0))
7501    (COS @0)))
7503  /* Simplify tan(x) / sin(x) -> 1.0 / cos(x). */
7504  (simplify
7505   (rdiv (TAN:s @0) (SIN:s @0))
7506   (if (! HONOR_NANS (@0)
7507        && ! HONOR_INFINITIES (@0))
7508    (rdiv { build_one_cst (type); } (COS @0))))
7510  /* Simplify pow(x,y) * pow(x,z) -> pow(x,y+z). */
7511  (simplify
7512   (mult (POW:s @0 @1) (POW:s @0 @2))
7513    (POW @0 (plus @1 @2)))
7515  /* Simplify pow(x,y) * pow(z,y) -> pow(x*z,y). */
7516  (simplify
7517   (mult (POW:s @0 @1) (POW:s @2 @1))
7518    (POW (mult @0 @2) @1))
7520  /* Simplify powi(x,y) * powi(z,y) -> powi(x*z,y). */
7521  (simplify
7522   (mult (POWI:s @0 @1) (POWI:s @2 @1))
7523    (POWI (mult @0 @2) @1))
7525  /* Simplify pow(x,c) / x -> pow(x,c-1). */
7526  (simplify
7527   (rdiv (POW:s @0 REAL_CST@1) @0)
7528   (if (!TREE_OVERFLOW (@1))
7529    (POW @0 (minus @1 { build_one_cst (type); }))))
7531  /* Simplify x / pow (y,z) -> x * pow(y,-z). */
7532  (simplify
7533   (rdiv @0 (POW:s @1 @2))
7534    (mult @0 (POW @1 (negate @2))))
7536  (for sqrts (SQRT)
7537       cbrts (CBRT)
7538       pows (POW)
7539   /* sqrt(sqrt(x)) -> pow(x,1/4).  */
7540   (simplify
7541    (sqrts (sqrts @0))
7542    (pows @0 { build_real (type, dconst_quarter ()); }))
7543   /* sqrt(cbrt(x)) -> pow(x,1/6).  */
7544   (simplify
7545    (sqrts (cbrts @0))
7546    (pows @0 { build_real_truncate (type, dconst_sixth ()); }))
7547   /* cbrt(sqrt(x)) -> pow(x,1/6).  */
7548   (simplify
7549    (cbrts (sqrts @0))
7550    (pows @0 { build_real_truncate (type, dconst_sixth ()); }))
7551   /* cbrt(cbrt(x)) -> pow(x,1/9), iff x is nonnegative.  */
7552   (simplify
7553    (cbrts (cbrts tree_expr_nonnegative_p@0))
7554    (pows @0 { build_real_truncate (type, dconst_ninth ()); }))
7555   /* sqrt(pow(x,y)) -> pow(|x|,y*0.5).  */
7556   (simplify
7557    (sqrts (pows @0 @1))
7558    (pows (abs @0) (mult @1 { build_real (type, dconsthalf); })))
7559   /* cbrt(pow(x,y)) -> pow(x,y/3), iff x is nonnegative.  */
7560   (simplify
7561    (cbrts (pows tree_expr_nonnegative_p@0 @1))
7562    (pows @0 (mult @1 { build_real_truncate (type, dconst_third ()); })))
7563   /* pow(sqrt(x),y) -> pow(x,y*0.5).  */
7564   (simplify
7565    (pows (sqrts @0) @1)
7566    (pows @0 (mult @1 { build_real (type, dconsthalf); })))
7567   /* pow(cbrt(x),y) -> pow(x,y/3) iff x is nonnegative.  */
7568   (simplify
7569    (pows (cbrts tree_expr_nonnegative_p@0) @1)
7570    (pows @0 (mult @1 { build_real_truncate (type, dconst_third ()); })))
7571   /* pow(pow(x,y),z) -> pow(x,y*z) iff x is nonnegative.  */
7572   (simplify
7573    (pows (pows tree_expr_nonnegative_p@0 @1) @2)
7574    (pows @0 (mult @1 @2))))
7576  /* cabs(x+xi) -> fabs(x)*sqrt(2).  */
7577  (simplify
7578   (CABS (complex @0 @0))
7579   (mult (abs @0) { build_real_truncate (type, dconst_sqrt2 ()); }))
7581  /* hypot(x,x) -> fabs(x)*sqrt(2).  */
7582  (simplify
7583   (HYPOT @0 @0)
7584   (mult (abs @0) { build_real_truncate (type, dconst_sqrt2 ()); }))
7586  /* cexp(x+yi) -> exp(x)*cexpi(y).  */
7587  (for cexps (CEXP)
7588       exps (EXP)
7589       cexpis (CEXPI)
7590   (simplify
7591    (cexps compositional_complex@0)
7592    (if (targetm.libc_has_function (function_c99_math_complex, TREE_TYPE (@0)))
7593     (complex
7594      (mult (exps@1 (realpart @0)) (realpart (cexpis:type@2 (imagpart @0))))
7595      (mult @1 (imagpart @2)))))))
7597 (if (canonicalize_math_p ())
7598  /* floor(x) -> trunc(x) if x is nonnegative.  */
7599  (for floors (FLOOR_ALL)
7600       truncs (TRUNC_ALL)
7601   (simplify
7602    (floors tree_expr_nonnegative_p@0)
7603    (truncs @0))))
7605 (match double_value_p
7606  @0
7607  (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == double_type_node)))
7608 (for froms (BUILT_IN_TRUNCL
7609             BUILT_IN_FLOORL
7610             BUILT_IN_CEILL
7611             BUILT_IN_ROUNDL
7612             BUILT_IN_NEARBYINTL
7613             BUILT_IN_RINTL)
7614      tos (BUILT_IN_TRUNC
7615           BUILT_IN_FLOOR
7616           BUILT_IN_CEIL
7617           BUILT_IN_ROUND
7618           BUILT_IN_NEARBYINT
7619           BUILT_IN_RINT)
7620  /* truncl(extend(x)) -> extend(trunc(x)), etc., if x is a double.  */
7621  (if (optimize && canonicalize_math_p ())
7622   (simplify
7623    (froms (convert double_value_p@0))
7624    (convert (tos @0)))))
7626 (match float_value_p
7627  @0
7628  (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == float_type_node)))
7629 (for froms (BUILT_IN_TRUNCL BUILT_IN_TRUNC
7630             BUILT_IN_FLOORL BUILT_IN_FLOOR
7631             BUILT_IN_CEILL BUILT_IN_CEIL
7632             BUILT_IN_ROUNDL BUILT_IN_ROUND
7633             BUILT_IN_NEARBYINTL BUILT_IN_NEARBYINT
7634             BUILT_IN_RINTL BUILT_IN_RINT)
7635      tos (BUILT_IN_TRUNCF BUILT_IN_TRUNCF
7636           BUILT_IN_FLOORF BUILT_IN_FLOORF
7637           BUILT_IN_CEILF BUILT_IN_CEILF
7638           BUILT_IN_ROUNDF BUILT_IN_ROUNDF
7639           BUILT_IN_NEARBYINTF BUILT_IN_NEARBYINTF
7640           BUILT_IN_RINTF BUILT_IN_RINTF)
7641  /* truncl(extend(x)) and trunc(extend(x)) -> extend(truncf(x)), etc.,
7642     if x is a float.  */
7643  (if (optimize && canonicalize_math_p ()
7644       && targetm.libc_has_function (function_c99_misc, NULL_TREE))
7645   (simplify
7646    (froms (convert float_value_p@0))
7647    (convert (tos @0)))))
7649 #if GIMPLE
7650 (match float16_value_p
7651  @0
7652  (if (TYPE_MAIN_VARIANT (TREE_TYPE (@0)) == float16_type_node)))
7653 (for froms (BUILT_IN_TRUNCL BUILT_IN_TRUNC BUILT_IN_TRUNCF
7654             BUILT_IN_FLOORL BUILT_IN_FLOOR BUILT_IN_FLOORF
7655             BUILT_IN_CEILL BUILT_IN_CEIL BUILT_IN_CEILF
7656             BUILT_IN_ROUNDEVENL BUILT_IN_ROUNDEVEN BUILT_IN_ROUNDEVENF
7657             BUILT_IN_ROUNDL BUILT_IN_ROUND BUILT_IN_ROUNDF
7658             BUILT_IN_NEARBYINTL BUILT_IN_NEARBYINT BUILT_IN_NEARBYINTF
7659             BUILT_IN_RINTL BUILT_IN_RINT BUILT_IN_RINTF
7660             BUILT_IN_SQRTL BUILT_IN_SQRT BUILT_IN_SQRTF)
7661      tos (IFN_TRUNC IFN_TRUNC IFN_TRUNC
7662           IFN_FLOOR IFN_FLOOR IFN_FLOOR
7663           IFN_CEIL IFN_CEIL IFN_CEIL
7664           IFN_ROUNDEVEN IFN_ROUNDEVEN IFN_ROUNDEVEN
7665           IFN_ROUND IFN_ROUND IFN_ROUND
7666           IFN_NEARBYINT IFN_NEARBYINT IFN_NEARBYINT
7667           IFN_RINT IFN_RINT IFN_RINT
7668           IFN_SQRT IFN_SQRT IFN_SQRT)
7669  /* (_Float16) round ((doube) x) -> __built_in_roundf16 (x), etc.,
7670     if x is a _Float16.  */
7671  (simplify
7672    (convert (froms (convert float16_value_p@0)))
7673      (if (optimize
7674           && types_match (type, TREE_TYPE (@0))
7675           && direct_internal_fn_supported_p (as_internal_fn (tos),
7676                                              type, OPTIMIZE_FOR_BOTH))
7677        (tos @0))))
7679 /* Simplify (trunc)copysign ((extend)x, (extend)y) to copysignf (x, y),
7680    x,y is float value, similar for _Float16/double.  */
7681 (for copysigns (COPYSIGN_ALL)
7682  (simplify
7683   (convert (copysigns (convert@2 @0) (convert @1)))
7684    (if (optimize
7685        && !HONOR_SNANS (@2)
7686        && types_match (type, TREE_TYPE (@0))
7687        && types_match (type, TREE_TYPE (@1))
7688        && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (@2))
7689        && direct_internal_fn_supported_p (IFN_COPYSIGN,
7690                                           type, OPTIMIZE_FOR_BOTH))
7691     (IFN_COPYSIGN @0 @1))))
7693 (for froms (BUILT_IN_FMAF BUILT_IN_FMA BUILT_IN_FMAL)
7694      tos (IFN_FMA IFN_FMA IFN_FMA)
7695  (simplify
7696   (convert (froms (convert@3 @0) (convert @1) (convert @2)))
7697    (if (flag_unsafe_math_optimizations
7698        && optimize
7699        && FLOAT_TYPE_P (type)
7700        && FLOAT_TYPE_P (TREE_TYPE (@3))
7701        && types_match (type, TREE_TYPE (@0))
7702        && types_match (type, TREE_TYPE (@1))
7703        && types_match (type, TREE_TYPE (@2))
7704        && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (@3))
7705        && direct_internal_fn_supported_p (as_internal_fn (tos),
7706                                           type, OPTIMIZE_FOR_BOTH))
7707     (tos @0 @1 @2))))
7709 (for maxmin (max min)
7710  (simplify
7711   (convert (maxmin (convert@2 @0) (convert @1)))
7712    (if (optimize
7713        && FLOAT_TYPE_P (type)
7714        && FLOAT_TYPE_P (TREE_TYPE (@2))
7715        && types_match (type, TREE_TYPE (@0))
7716        && types_match (type, TREE_TYPE (@1))
7717        && element_precision (type) < element_precision (TREE_TYPE (@2)))
7718     (maxmin @0 @1))))
7719 #endif
7721 (for froms (XFLOORL XCEILL XROUNDL XRINTL)
7722      tos (XFLOOR XCEIL XROUND XRINT)
7723  /* llfloorl(extend(x)) -> llfloor(x), etc., if x is a double.  */
7724  (if (optimize && canonicalize_math_p ())
7725   (simplify
7726    (froms (convert double_value_p@0))
7727    (tos @0))))
7729 (for froms (XFLOORL XCEILL XROUNDL XRINTL
7730             XFLOOR XCEIL XROUND XRINT)
7731      tos (XFLOORF XCEILF XROUNDF XRINTF)
7732  /* llfloorl(extend(x)) and llfloor(extend(x)) -> llfloorf(x), etc.,
7733     if x is a float.  */
7734  (if (optimize && canonicalize_math_p ())
7735   (simplify
7736    (froms (convert float_value_p@0))
7737    (tos @0))))
7739 (if (canonicalize_math_p ())
7740  /* xfloor(x) -> fix_trunc(x) if x is nonnegative.  */
7741  (for floors (IFLOOR LFLOOR LLFLOOR)
7742   (simplify
7743    (floors tree_expr_nonnegative_p@0)
7744    (fix_trunc @0))))
7746 (if (canonicalize_math_p ())
7747  /* xfloor(x) -> fix_trunc(x), etc., if x is integer valued.  */
7748  (for fns (IFLOOR LFLOOR LLFLOOR
7749            ICEIL LCEIL LLCEIL
7750            IROUND LROUND LLROUND)
7751   (simplify
7752    (fns integer_valued_real_p@0)
7753    (fix_trunc @0)))
7754  (if (!flag_errno_math)
7755   /* xrint(x) -> fix_trunc(x), etc., if x is integer valued.  */
7756   (for rints (IRINT LRINT LLRINT)
7757    (simplify
7758     (rints integer_valued_real_p@0)
7759     (fix_trunc @0)))))
7761 (if (canonicalize_math_p ())
7762  (for ifn (IFLOOR ICEIL IROUND IRINT)
7763       lfn (LFLOOR LCEIL LROUND LRINT)
7764       llfn (LLFLOOR LLCEIL LLROUND LLRINT)
7765   /* Canonicalize iround (x) to lround (x) on ILP32 targets where
7766      sizeof (int) == sizeof (long).  */
7767   (if (TYPE_PRECISION (integer_type_node)
7768        == TYPE_PRECISION (long_integer_type_node))
7769    (simplify
7770     (ifn @0)
7771     (lfn:long_integer_type_node @0)))
7772   /* Canonicalize llround (x) to lround (x) on LP64 targets where
7773      sizeof (long long) == sizeof (long).  */
7774   (if (TYPE_PRECISION (long_long_integer_type_node)
7775        == TYPE_PRECISION (long_integer_type_node))
7776    (simplify
7777     (llfn @0)
7778     (lfn:long_integer_type_node @0)))))
7780 /* cproj(x) -> x if we're ignoring infinities.  */
7781 (simplify
7782  (CPROJ @0)
7783  (if (!HONOR_INFINITIES (type))
7784    @0))
7786 /* If the real part is inf and the imag part is known to be
7787    nonnegative, return (inf + 0i).  */
7788 (simplify
7789  (CPROJ (complex REAL_CST@0 tree_expr_nonnegative_p@1))
7790  (if (real_isinf (TREE_REAL_CST_PTR (@0)))
7791   { build_complex_inf (type, false); }))
7793 /* If the imag part is inf, return (inf+I*copysign(0,imag)).  */
7794 (simplify
7795  (CPROJ (complex @0 REAL_CST@1))
7796  (if (real_isinf (TREE_REAL_CST_PTR (@1)))
7797   { build_complex_inf (type, TREE_REAL_CST_PTR (@1)->sign); }))
7799 (for pows (POW)
7800      sqrts (SQRT)
7801      cbrts (CBRT)
7802  (simplify
7803   (pows @0 REAL_CST@1)
7804   (with {
7805     const REAL_VALUE_TYPE *value = TREE_REAL_CST_PTR (@1);
7806     REAL_VALUE_TYPE tmp;
7807    }
7808    (switch
7809     /* pow(x,0) -> 1.  */
7810     (if (real_equal (value, &dconst0))
7811      { build_real (type, dconst1); })
7812     /* pow(x,1) -> x.  */
7813     (if (real_equal (value, &dconst1))
7814      @0)
7815     /* pow(x,-1) -> 1/x.  */
7816     (if (real_equal (value, &dconstm1))
7817      (rdiv { build_real (type, dconst1); } @0))
7818     /* pow(x,0.5) -> sqrt(x).  */
7819     (if (flag_unsafe_math_optimizations
7820          && canonicalize_math_p ()
7821          && real_equal (value, &dconsthalf))
7822      (sqrts @0))
7823     /* pow(x,1/3) -> cbrt(x).  */
7824     (if (flag_unsafe_math_optimizations
7825          && canonicalize_math_p ()
7826          && (tmp = real_value_truncate (TYPE_MODE (type), dconst_third ()),
7827              real_equal (value, &tmp)))
7828      (cbrts @0))))))
7830 /* powi(1,x) -> 1.  */
7831 (simplify
7832  (POWI real_onep@0 @1)
7833  @0)
7835 (simplify
7836  (POWI @0 INTEGER_CST@1)
7837  (switch
7838   /* powi(x,0) -> 1.  */
7839   (if (wi::to_wide (@1) == 0)
7840    { build_real (type, dconst1); })
7841   /* powi(x,1) -> x.  */
7842   (if (wi::to_wide (@1) == 1)
7843    @0)
7844   /* powi(x,-1) -> 1/x.  */
7845   (if (wi::to_wide (@1) == -1)
7846    (rdiv { build_real (type, dconst1); } @0))))
7848 /* Narrowing of arithmetic and logical operations.
7850    These are conceptually similar to the transformations performed for
7851    the C/C++ front-ends by shorten_binary_op and shorten_compare.  Long
7852    term we want to move all that code out of the front-ends into here.  */
7854 /* Convert (outertype)((innertype0)a+(innertype1)b)
7855    into ((newtype)a+(newtype)b) where newtype
7856    is the widest mode from all of these.  */
7857 (for op (plus minus mult rdiv)
7858  (simplify
7859    (convert (op:s@0 (convert1?@3 @1) (convert2?@4 @2)))
7860    /* If we have a narrowing conversion of an arithmetic operation where
7861       both operands are widening conversions from the same type as the outer
7862       narrowing conversion.  Then convert the innermost operands to a
7863       suitable unsigned type (to avoid introducing undefined behavior),
7864       perform the operation and convert the result to the desired type.  */
7865    (if (INTEGRAL_TYPE_P (type)
7866         && op != MULT_EXPR
7867         && op != RDIV_EXPR
7868         /* We check for type compatibility between @0 and @1 below,
7869            so there's no need to check that @2/@4 are integral types.  */
7870         && INTEGRAL_TYPE_P (TREE_TYPE (@1))
7871         && INTEGRAL_TYPE_P (TREE_TYPE (@3))
7872         /* The precision of the type of each operand must match the
7873            precision of the mode of each operand, similarly for the
7874            result.  */
7875         && type_has_mode_precision_p (TREE_TYPE (@1))
7876         && type_has_mode_precision_p (TREE_TYPE (@2))
7877         && type_has_mode_precision_p (type)
7878         /* The inner conversion must be a widening conversion.  */
7879         && TYPE_PRECISION (TREE_TYPE (@3)) > TYPE_PRECISION (TREE_TYPE (@1))
7880         && types_match (@1, type)
7881         && (types_match (@1, @2)
7882             /* Or the second operand is const integer or converted const
7883                integer from valueize.  */
7884             || poly_int_tree_p (@4)))
7885      (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
7886        (op @1 (convert @2))
7887        (with { tree utype = unsigned_type_for (TREE_TYPE (@1)); }
7888         (convert (op (convert:utype @1)
7889                      (convert:utype @2)))))
7890      (if (FLOAT_TYPE_P (type)
7891           && DECIMAL_FLOAT_TYPE_P (TREE_TYPE (@0))
7892                == DECIMAL_FLOAT_TYPE_P (type))
7893       (with { tree arg0 = strip_float_extensions (@1);
7894               tree arg1 = strip_float_extensions (@2);
7895               tree itype = TREE_TYPE (@0);
7896               tree ty1 = TREE_TYPE (arg0);
7897               tree ty2 = TREE_TYPE (arg1);
7898               enum tree_code code = TREE_CODE (itype); }
7899         (if (FLOAT_TYPE_P (ty1)
7900              && FLOAT_TYPE_P (ty2))
7901          (with { tree newtype = type;
7902                  if (TYPE_MODE (ty1) == SDmode
7903                      || TYPE_MODE (ty2) == SDmode
7904                      || TYPE_MODE (type) == SDmode)
7905                    newtype = dfloat32_type_node;
7906                  if (TYPE_MODE (ty1) == DDmode
7907                      || TYPE_MODE (ty2) == DDmode
7908                      || TYPE_MODE (type) == DDmode)
7909                    newtype = dfloat64_type_node;
7910                  if (TYPE_MODE (ty1) == TDmode
7911                      || TYPE_MODE (ty2) == TDmode
7912                      || TYPE_MODE (type) == TDmode)
7913                    newtype = dfloat128_type_node; }
7914           (if ((newtype == dfloat32_type_node
7915                 || newtype == dfloat64_type_node
7916                 || newtype == dfloat128_type_node)
7917               && newtype == type
7918               && types_match (newtype, type))
7919             (op (convert:newtype @1) (convert:newtype @2))
7920             (with { if (element_precision (ty1) > element_precision (newtype))
7921                       newtype = ty1;
7922                     if (element_precision (ty2) > element_precision (newtype))
7923                       newtype = ty2; }
7924                /* Sometimes this transformation is safe (cannot
7925                   change results through affecting double rounding
7926                   cases) and sometimes it is not.  If NEWTYPE is
7927                   wider than TYPE, e.g. (float)((long double)double
7928                   + (long double)double) converted to
7929                   (float)(double + double), the transformation is
7930                   unsafe regardless of the details of the types
7931                   involved; double rounding can arise if the result
7932                   of NEWTYPE arithmetic is a NEWTYPE value half way
7933                   between two representable TYPE values but the
7934                   exact value is sufficiently different (in the
7935                   right direction) for this difference to be
7936                   visible in ITYPE arithmetic.  If NEWTYPE is the
7937                   same as TYPE, however, the transformation may be
7938                   safe depending on the types involved: it is safe
7939                   if the ITYPE has strictly more than twice as many
7940                   mantissa bits as TYPE, can represent infinities
7941                   and NaNs if the TYPE can, and has sufficient
7942                   exponent range for the product or ratio of two
7943                   values representable in the TYPE to be within the
7944                   range of normal values of ITYPE.  */
7945               (if (element_precision (newtype) < element_precision (itype)
7946                    && (!VECTOR_MODE_P (TYPE_MODE (newtype))
7947                        || target_supports_op_p (newtype, op, optab_default))
7948                    && (flag_unsafe_math_optimizations
7949                        || (element_precision (newtype) == element_precision (type)
7950                            && real_can_shorten_arithmetic (element_mode (itype),
7951                                                            element_mode (type))
7952                            && !excess_precision_type (newtype)))
7953                    && !types_match (itype, newtype))
7954                  (convert:type (op (convert:newtype @1)
7955                                    (convert:newtype @2)))
7956          )))) )
7957    ))
7960 /* This is another case of narrowing, specifically when there's an outer
7961    BIT_AND_EXPR which masks off bits outside the type of the innermost
7962    operands.   Like the previous case we have to convert the operands
7963    to unsigned types to avoid introducing undefined behavior for the
7964    arithmetic operation.  */
7965 (for op (minus plus)
7966  (simplify
7967   (bit_and (op:s (convert@2 @0) (convert@3 @1)) INTEGER_CST@4)
7968   (if (INTEGRAL_TYPE_P (type)
7969        /* We check for type compatibility between @0 and @1 below,
7970           so there's no need to check that @1/@3 are integral types.  */
7971        && INTEGRAL_TYPE_P (TREE_TYPE (@0))
7972        && INTEGRAL_TYPE_P (TREE_TYPE (@2))
7973        /* The precision of the type of each operand must match the
7974           precision of the mode of each operand, similarly for the
7975           result.  */
7976        && type_has_mode_precision_p (TREE_TYPE (@0))
7977        && type_has_mode_precision_p (TREE_TYPE (@1))
7978        && type_has_mode_precision_p (type)
7979        /* The inner conversion must be a widening conversion.  */
7980        && TYPE_PRECISION (TREE_TYPE (@2)) > TYPE_PRECISION (TREE_TYPE (@0))
7981        && types_match (@0, @1)
7982        && (tree_int_cst_min_precision (@4, TYPE_SIGN (TREE_TYPE (@0)))
7983            <= TYPE_PRECISION (TREE_TYPE (@0)))
7984        && (wi::to_wide (@4)
7985            & wi::mask (TYPE_PRECISION (TREE_TYPE (@0)),
7986                        true, TYPE_PRECISION (type))) == 0)
7987    (if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
7988     (with { tree ntype = TREE_TYPE (@0); }
7989      (convert (bit_and (op @0 @1) (convert:ntype @4))))
7990     (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
7991      (convert (bit_and (op (convert:utype @0) (convert:utype @1))
7992                (convert:utype @4))))))))
7994 /* Transform (@0 < @1 and @0 < @2) to use min,
7995    (@0 > @1 and @0 > @2) to use max */
7996 (for logic (bit_and bit_and bit_and bit_and bit_ior bit_ior bit_ior bit_ior)
7997      op    (lt      le      gt      ge      lt      le      gt      ge     )
7998      ext   (min     min     max     max     max     max     min     min    )
7999  (simplify
8000   (logic (op:cs @0 @1) (op:cs @0 @2))
8001   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
8002        && TREE_CODE (@0) != INTEGER_CST)
8003    (op @0 (ext @1 @2)))))
8005 /* Max<bool0, bool1> -> bool0 | bool1
8006    Min<bool0, bool1> -> bool0 & bool1 */
8007 (for op    (max     min)
8008      logic (bit_ior bit_and)
8009  (simplify
8010   (op zero_one_valued_p@0 zero_one_valued_p@1)
8011   (logic @0 @1)))
8013 /* signbit(x) != 0 ? -x : x -> abs(x)
8014    signbit(x) == 0 ? -x : x -> -abs(x) */
8015 (for sign (SIGNBIT)
8016  (for neeq (ne eq)
8017   (simplify
8018    (cond (neeq (sign @0) integer_zerop) (negate @0) @0)
8019     (if (neeq == NE_EXPR)
8020      (abs @0)
8021      (negate (abs @0))))))
8023 (simplify
8024  /* signbit(x) -> 0 if x is nonnegative.  */
8025  (SIGNBIT tree_expr_nonnegative_p@0)
8026  { integer_zero_node; })
8028 (simplify
8029  /* signbit(x) -> x<0 if x doesn't have signed zeros.  */
8030  (SIGNBIT @0)
8031  (if (!HONOR_SIGNED_ZEROS (@0))
8032   (convert (lt @0 { build_real (TREE_TYPE (@0), dconst0); }))))
8034 /* Transform comparisons of the form X +- C1 CMP C2 to X CMP C2 -+ C1.  */
8035 (for cmp (eq ne)
8036  (for op (plus minus)
8037       rop (minus plus)
8038   (simplify
8039    (cmp (op@3 @0 INTEGER_CST@1) INTEGER_CST@2)
8040    (if (!TREE_OVERFLOW (@1) && !TREE_OVERFLOW (@2)
8041         && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0))
8042         && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (@0))
8043         && !TYPE_SATURATING (TREE_TYPE (@0)))
8044     (with { tree res = int_const_binop (rop, @2, @1); }
8045      (if (TREE_OVERFLOW (res)
8046           && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
8047       { constant_boolean_node (cmp == NE_EXPR, type); }
8048       (if (single_use (@3))
8049        (cmp @0 { TREE_OVERFLOW (res)
8050                  ? drop_tree_overflow (res) : res; }))))))))
8051 (for cmp (lt le gt ge)
8052  (for op (plus minus)
8053       rop (minus plus)
8054   (simplify
8055    (cmp (op@3 @0 INTEGER_CST@1) INTEGER_CST@2)
8056    (if (!TREE_OVERFLOW (@1) && !TREE_OVERFLOW (@2)
8057         && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
8058     (with { tree res = int_const_binop (rop, @2, @1); }
8059      (if (TREE_OVERFLOW (res))
8060       {
8061         fold_overflow_warning (("assuming signed overflow does not occur "
8062                                 "when simplifying conditional to constant"),
8063                                WARN_STRICT_OVERFLOW_CONDITIONAL);
8064         bool less = cmp == LE_EXPR || cmp == LT_EXPR;
8065         /* wi::ges_p (@2, 0) should be sufficient for a signed type.  */
8066         bool ovf_high = wi::lt_p (wi::to_wide (@1), 0,
8067                                   TYPE_SIGN (TREE_TYPE (@1)))
8068                         != (op == MINUS_EXPR);
8069         constant_boolean_node (less == ovf_high, type);
8070       }
8071       (if (single_use (@3))
8072        (with
8073         {
8074           fold_overflow_warning (("assuming signed overflow does not occur "
8075                                   "when changing X +- C1 cmp C2 to "
8076                                   "X cmp C2 -+ C1"),
8077                                  WARN_STRICT_OVERFLOW_COMPARISON);
8078         }
8079         (cmp @0 { res; })))))))))
8081 /* Canonicalizations of BIT_FIELD_REFs.  */
8083 (simplify
8084  (BIT_FIELD_REF (BIT_FIELD_REF @0 @1 @2) @3 @4)
8085  (BIT_FIELD_REF @0 @3 { const_binop (PLUS_EXPR, bitsizetype, @2, @4); }))
8087 (simplify
8088  (BIT_FIELD_REF (view_convert @0) @1 @2)
8089  (BIT_FIELD_REF @0 @1 @2))
8091 (simplify
8092  (BIT_FIELD_REF @0 @1 integer_zerop)
8093  (if (tree_int_cst_equal (@1, TYPE_SIZE (TREE_TYPE (@0))))
8094   (view_convert @0)))
8096 (simplify
8097  (BIT_FIELD_REF @0 @1 @2)
8098  (switch
8099   (if (TREE_CODE (TREE_TYPE (@0)) == COMPLEX_TYPE
8100        && tree_int_cst_equal (@1, TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))))
8101    (switch
8102     (if (integer_zerop (@2))
8103      (view_convert (realpart @0)))
8104     (if (tree_int_cst_equal (@2, TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))))
8105      (view_convert (imagpart @0)))))
8106   (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
8107        && INTEGRAL_TYPE_P (type)
8108        /* On GIMPLE this should only apply to register arguments.  */
8109        && (! GIMPLE || is_gimple_reg (@0))
8110        /* A bit-field-ref that referenced the full argument can be stripped.  */
8111        && ((compare_tree_int (@1, TYPE_PRECISION (TREE_TYPE (@0))) == 0
8112             && integer_zerop (@2))
8113            /* Low-parts can be reduced to integral conversions.
8114               ???  The following doesn't work for PDP endian.  */
8115            || (BYTES_BIG_ENDIAN == WORDS_BIG_ENDIAN
8116                /* But only do this after vectorization.  */
8117                && canonicalize_math_after_vectorization_p ()
8118                /* Don't even think about BITS_BIG_ENDIAN.  */
8119                && TYPE_PRECISION (TREE_TYPE (@0)) % BITS_PER_UNIT == 0
8120                && TYPE_PRECISION (type) % BITS_PER_UNIT == 0
8121                && compare_tree_int (@2, (BYTES_BIG_ENDIAN
8122                                          ? (TYPE_PRECISION (TREE_TYPE (@0))
8123                                             - TYPE_PRECISION (type))
8124                                          : 0)) == 0)))
8125    (convert @0))))
8127 /* Simplify vector extracts.  */
8129 (simplify
8130  (BIT_FIELD_REF CONSTRUCTOR@0 @1 @2)
8131  (if (VECTOR_TYPE_P (TREE_TYPE (@0))
8132       && tree_fits_uhwi_p (TYPE_SIZE (type))
8133       && ((tree_to_uhwi (TYPE_SIZE (type))
8134            == tree_to_uhwi (TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0)))))
8135           || (VECTOR_TYPE_P (type)
8136               && (tree_to_uhwi (TYPE_SIZE (TREE_TYPE (type)))
8137                   == tree_to_uhwi (TYPE_SIZE (TREE_TYPE (TREE_TYPE (@0))))))))
8138   (with
8139    {
8140      tree ctor = (TREE_CODE (@0) == SSA_NAME
8141                   ? gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0)) : @0);
8142      tree eltype = TREE_TYPE (TREE_TYPE (ctor));
8143      unsigned HOST_WIDE_INT width = tree_to_uhwi (TYPE_SIZE (eltype));
8144      unsigned HOST_WIDE_INT n = tree_to_uhwi (@1);
8145      unsigned HOST_WIDE_INT idx = tree_to_uhwi (@2);
8146    }
8147    (if (n != 0
8148         && (idx % width) == 0
8149         && (n % width) == 0
8150         && known_le ((idx + n) / width,
8151                      TYPE_VECTOR_SUBPARTS (TREE_TYPE (ctor))))
8152     (with
8153      {
8154        idx = idx / width;
8155        n = n / width;
8156        /* Constructor elements can be subvectors.  */
8157        poly_uint64 k = 1;
8158        if (CONSTRUCTOR_NELTS (ctor) != 0)
8159          {
8160            tree cons_elem = TREE_TYPE (CONSTRUCTOR_ELT (ctor, 0)->value);
8161            if (TREE_CODE (cons_elem) == VECTOR_TYPE)
8162              k = TYPE_VECTOR_SUBPARTS (cons_elem);
8163          }
8164        unsigned HOST_WIDE_INT elt, count, const_k;
8165      }
8166      (switch
8167       /* We keep an exact subset of the constructor elements.  */
8168       (if (multiple_p (idx, k, &elt) && multiple_p (n, k, &count))
8169        (if (CONSTRUCTOR_NELTS (ctor) == 0)
8170         { build_zero_cst (type); }
8171         (if (count == 1)
8172          (if (elt < CONSTRUCTOR_NELTS (ctor))
8173           (view_convert { CONSTRUCTOR_ELT (ctor, elt)->value; })
8174           { build_zero_cst (type); })
8175          /* We don't want to emit new CTORs unless the old one goes away.
8176             ???  Eventually allow this if the CTOR ends up constant or
8177             uniform.  */
8178          (if (single_use (@0))
8179           (with
8180             {
8181               vec<constructor_elt, va_gc> *vals;
8182               vec_alloc (vals, count);
8183               bool constant_p = true;
8184               tree res;
8185               for (unsigned i = 0;
8186                    i < count && elt + i < CONSTRUCTOR_NELTS (ctor); ++i)
8187                 {
8188                   tree e = CONSTRUCTOR_ELT (ctor, elt + i)->value;
8189                   CONSTRUCTOR_APPEND_ELT (vals, NULL_TREE, e);
8190                   if (!CONSTANT_CLASS_P (e))
8191                     constant_p = false;
8192                 }
8193               tree evtype = (types_match (TREE_TYPE (type),
8194                                           TREE_TYPE (TREE_TYPE (ctor)))
8195                              ? type
8196                              : build_vector_type (TREE_TYPE (TREE_TYPE (ctor)),
8197                                                   count * k));
8198               /* We used to build a CTOR in the non-constant case here
8199                  but that's not a GIMPLE value.  We'd have to expose this
8200                  operation somehow so the code generation can properly
8201                  split it out to a separate stmt.  */
8202               res = (constant_p ? build_vector_from_ctor (evtype, vals)
8203                      : (GIMPLE ? NULL_TREE : build_constructor (evtype, vals)));
8204             }
8205             (if (res)
8206              (view_convert { res; })))))))
8207       /* The bitfield references a single constructor element.  */
8208       (if (k.is_constant (&const_k)
8209            && idx + n <= (idx / const_k + 1) * const_k)
8210        (switch
8211         (if (CONSTRUCTOR_NELTS (ctor) <= idx / const_k)
8212          { build_zero_cst (type); })
8213         (if (n == const_k)
8214          (view_convert { CONSTRUCTOR_ELT (ctor, idx / const_k)->value; }))
8215         (BIT_FIELD_REF { CONSTRUCTOR_ELT (ctor, idx / const_k)->value; }
8216                        @1 { bitsize_int ((idx % const_k) * width); })))))))))
8218 /* Simplify a bit extraction from a bit insertion for the cases with
8219    the inserted element fully covering the extraction or the insertion
8220    not touching the extraction.  */
8221 (simplify
8222  (BIT_FIELD_REF (bit_insert @0 @1 @ipos) @rsize @rpos)
8223  (with
8224   {
8225     unsigned HOST_WIDE_INT isize;
8226     if (INTEGRAL_TYPE_P (TREE_TYPE (@1)))
8227       isize = TYPE_PRECISION (TREE_TYPE (@1));
8228     else
8229       isize = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (@1)));
8230   }
8231   (switch
8232    (if ((!INTEGRAL_TYPE_P (TREE_TYPE (@1))
8233          || type_has_mode_precision_p (TREE_TYPE (@1)))
8234         && wi::leu_p (wi::to_wide (@ipos), wi::to_wide (@rpos))
8235         && wi::leu_p (wi::to_wide (@rpos) + wi::to_wide (@rsize),
8236                       wi::to_wide (@ipos) + isize))
8237     (BIT_FIELD_REF @1 @rsize { wide_int_to_tree (bitsizetype,
8238                                                  wi::to_wide (@rpos)
8239                                                  - wi::to_wide (@ipos)); }))
8240    (if (wi::eq_p (wi::to_wide (@ipos), wi::to_wide (@rpos))
8241         && compare_tree_int (@rsize, isize) == 0)
8242     (convert @1))
8243    (if (wi::geu_p (wi::to_wide (@ipos),
8244                    wi::to_wide (@rpos) + wi::to_wide (@rsize))
8245         || wi::geu_p (wi::to_wide (@rpos),
8246                       wi::to_wide (@ipos) + isize))
8247     (BIT_FIELD_REF @0 @rsize @rpos)))))
8249 /* Simplify vector inserts of other vector extracts to a permute.  */
8250 (simplify
8251  (bit_insert @0 (BIT_FIELD_REF@2 @1 @rsize @rpos) @ipos)
8252  (if (VECTOR_TYPE_P (type)
8253       && types_match (@0, @1)
8254       && types_match (TREE_TYPE (TREE_TYPE (@0)), TREE_TYPE (@2))
8255       && TYPE_VECTOR_SUBPARTS (type).is_constant ())
8256   (with
8257    {
8258      unsigned HOST_WIDE_INT elsz
8259        = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (TREE_TYPE (@1))));
8260      poly_uint64 relt = exact_div (tree_to_poly_uint64 (@rpos), elsz);
8261      poly_uint64 ielt = exact_div (tree_to_poly_uint64 (@ipos), elsz);
8262      unsigned nunits = TYPE_VECTOR_SUBPARTS (type).to_constant ();
8263      vec_perm_builder builder;
8264      builder.new_vector (nunits, nunits, 1);
8265      for (unsigned i = 0; i < nunits; ++i)
8266        builder.quick_push (known_eq (ielt, i) ? nunits + relt : i);
8267      vec_perm_indices sel (builder, 2, nunits);
8268    }
8269    (if (!VECTOR_MODE_P (TYPE_MODE (type))
8270         || can_vec_perm_const_p (TYPE_MODE (type), TYPE_MODE (type), sel, false))
8271     (vec_perm @0 @1 { vec_perm_indices_to_tree
8272                         (build_vector_type (ssizetype, nunits), sel); })))))
8274 (if (canonicalize_math_after_vectorization_p ())
8275  (for fmas (FMA)
8276   (simplify
8277    (fmas:c (negate @0) @1 @2)
8278    (IFN_FNMA @0 @1 @2))
8279   (simplify
8280    (fmas @0 @1 (negate @2))
8281    (IFN_FMS @0 @1 @2))
8282   (simplify
8283    (fmas:c (negate @0) @1 (negate @2))
8284    (IFN_FNMS @0 @1 @2))
8285   (simplify
8286    (negate (fmas@3 @0 @1 @2))
8287    (if (single_use (@3))
8288     (IFN_FNMS @0 @1 @2))))
8290  (simplify
8291   (IFN_FMS:c (negate @0) @1 @2)
8292   (IFN_FNMS @0 @1 @2))
8293  (simplify
8294   (IFN_FMS @0 @1 (negate @2))
8295   (IFN_FMA @0 @1 @2))
8296  (simplify
8297   (IFN_FMS:c (negate @0) @1 (negate @2))
8298   (IFN_FNMA @0 @1 @2))
8299  (simplify
8300   (negate (IFN_FMS@3 @0 @1 @2))
8301    (if (single_use (@3))
8302     (IFN_FNMA @0 @1 @2)))
8304  (simplify
8305   (IFN_FNMA:c (negate @0) @1 @2)
8306   (IFN_FMA @0 @1 @2))
8307  (simplify
8308   (IFN_FNMA @0 @1 (negate @2))
8309   (IFN_FNMS @0 @1 @2))
8310  (simplify
8311   (IFN_FNMA:c (negate @0) @1 (negate @2))
8312   (IFN_FMS @0 @1 @2))
8313  (simplify
8314   (negate (IFN_FNMA@3 @0 @1 @2))
8315   (if (single_use (@3))
8316    (IFN_FMS @0 @1 @2)))
8318  (simplify
8319   (IFN_FNMS:c (negate @0) @1 @2)
8320   (IFN_FMS @0 @1 @2))
8321  (simplify
8322   (IFN_FNMS @0 @1 (negate @2))
8323   (IFN_FNMA @0 @1 @2))
8324  (simplify
8325   (IFN_FNMS:c (negate @0) @1 (negate @2))
8326   (IFN_FMA @0 @1 @2))
8327  (simplify
8328   (negate (IFN_FNMS@3 @0 @1 @2))
8329   (if (single_use (@3))
8330    (IFN_FMA @0 @1 @2))))
8332 /* CLZ simplifications.  */
8333 (for clz (CLZ)
8334  (for op (eq ne)
8335       cmp (lt ge)
8336   (simplify
8337    (op (clz:s@2 @0) INTEGER_CST@1)
8338    (if (integer_zerop (@1) && single_use (@2))
8339     /* clz(X) == 0 is (int)X < 0 and clz(X) != 0 is (int)X >= 0.  */
8340     (with { tree type0 = TREE_TYPE (@0);
8341             tree stype = signed_type_for (type0);
8342             HOST_WIDE_INT val = 0;
8343             /* Punt on hypothetical weird targets.  */
8344             if (clz == CFN_CLZ
8345                 && CLZ_DEFINED_VALUE_AT_ZERO (SCALAR_TYPE_MODE (type0),
8346                                               val) == 2
8347                 && val == 0)
8348               stype = NULL_TREE;
8349           }
8350      (if (stype)
8351       (cmp (convert:stype @0) { build_zero_cst (stype); })))
8352     /* clz(X) == (prec-1) is X == 1 and clz(X) != (prec-1) is X != 1.  */
8353     (with { bool ok = true;
8354             HOST_WIDE_INT val = 0;
8355             tree type0 = TREE_TYPE (@0);
8356             /* Punt on hypothetical weird targets.  */
8357             if (clz == CFN_CLZ
8358                 && CLZ_DEFINED_VALUE_AT_ZERO (SCALAR_TYPE_MODE (type0),
8359                                               val) == 2
8360                 && val == TYPE_PRECISION (type0) - 1)
8361               ok = false;
8362           }
8363      (if (ok && wi::to_wide (@1) == (TYPE_PRECISION (type0) - 1))
8364       (op @0 { build_one_cst (type0); })))))))
8366 /* CTZ simplifications.  */
8367 (for ctz (CTZ)
8368  (for op (ge gt le lt)
8369       cmp (eq eq ne ne)
8370   (simplify
8371    /* __builtin_ctz (x) >= C -> (x & ((1 << C) - 1)) == 0.  */
8372    (op (ctz:s @0) INTEGER_CST@1)
8373     (with { bool ok = true;
8374             HOST_WIDE_INT val = 0;
8375             if (!tree_fits_shwi_p (@1))
8376               ok = false;
8377             else
8378               {
8379                 val = tree_to_shwi (@1);
8380                 /* Canonicalize to >= or <.  */
8381                 if (op == GT_EXPR || op == LE_EXPR)
8382                   {
8383                     if (val == HOST_WIDE_INT_MAX)
8384                       ok = false;
8385                     else
8386                       val++;
8387                   }
8388               }
8389             bool zero_res = false;
8390             HOST_WIDE_INT zero_val = 0;
8391             tree type0 = TREE_TYPE (@0);
8392             int prec = TYPE_PRECISION (type0);
8393             if (ctz == CFN_CTZ
8394                 && CTZ_DEFINED_VALUE_AT_ZERO (SCALAR_TYPE_MODE (type0),
8395                                               zero_val) == 2)
8396               zero_res = true;
8397           }
8398      (if (val <= 0)
8399       (if (ok && (!zero_res || zero_val >= val))
8400        { constant_boolean_node (cmp == EQ_EXPR ? true : false, type); })
8401       (if (val >= prec)
8402        (if (ok && (!zero_res || zero_val < val))
8403         { constant_boolean_node (cmp == EQ_EXPR ? false : true, type); })
8404        (if (ok && (!zero_res || zero_val < 0 || zero_val >= prec))
8405         (cmp (bit_and @0 { wide_int_to_tree (type0,
8406                                              wi::mask (val, false, prec)); })
8407              { build_zero_cst (type0); })))))))
8408  (for op (eq ne)
8409   (simplify
8410    /* __builtin_ctz (x) == C -> (x & ((1 << (C + 1)) - 1)) == (1 << C).  */
8411    (op (ctz:s @0) INTEGER_CST@1)
8412     (with { bool zero_res = false;
8413             HOST_WIDE_INT zero_val = 0;
8414             tree type0 = TREE_TYPE (@0);
8415             int prec = TYPE_PRECISION (type0);
8416             if (ctz == CFN_CTZ
8417                 && CTZ_DEFINED_VALUE_AT_ZERO (SCALAR_TYPE_MODE (type0),
8418                                               zero_val) == 2)
8419               zero_res = true;
8420           }
8421      (if (tree_int_cst_sgn (@1) < 0 || wi::to_widest (@1) >= prec)
8422       (if (!zero_res || zero_val != wi::to_widest (@1))
8423        { constant_boolean_node (op == EQ_EXPR ? false : true, type); })
8424       (if (!zero_res || zero_val < 0 || zero_val >= prec)
8425        (op (bit_and @0 { wide_int_to_tree (type0,
8426                                            wi::mask (tree_to_uhwi (@1) + 1,
8427                                                      false, prec)); })
8428            { wide_int_to_tree (type0,
8429                                wi::shifted_mask (tree_to_uhwi (@1), 1,
8430                                                  false, prec)); })))))))
8432 /* POPCOUNT simplifications.  */
8433 /* popcount(X) + popcount(Y) is popcount(X|Y) when X&Y must be zero.  */
8434 (simplify
8435   (plus (POPCOUNT:s @0) (POPCOUNT:s @1))
8436   (if (INTEGRAL_TYPE_P (type)
8437        && wi::bit_and (tree_nonzero_bits (@0), tree_nonzero_bits (@1)) == 0)
8438     (POPCOUNT (bit_ior @0 @1))))
8440 /* popcount(X) == 0 is X == 0, and related (in)equalities.  */
8441 (for popcount (POPCOUNT)
8442   (for cmp (le eq ne gt)
8443        rep (eq eq ne ne)
8444     (simplify
8445       (cmp (popcount @0) integer_zerop)
8446       (rep @0 { build_zero_cst (TREE_TYPE (@0)); }))))
8448 /* popcount(bswap(x)) is popcount(x).  */
8449 (for popcount (POPCOUNT)
8450   (for bswap (BUILT_IN_BSWAP16 BUILT_IN_BSWAP32
8451               BUILT_IN_BSWAP64 BUILT_IN_BSWAP128)
8452     (simplify
8453       (popcount (convert?@0 (bswap:s@1 @2)))
8454       (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
8455            && INTEGRAL_TYPE_P (TREE_TYPE (@1)))
8456         (with { tree type0 = TREE_TYPE (@0);
8457                 tree type1 = TREE_TYPE (@1);
8458                 unsigned int prec0 = TYPE_PRECISION (type0);
8459                 unsigned int prec1 = TYPE_PRECISION (type1); }
8460           (if (prec0 == prec1 || (prec0 > prec1 && TYPE_UNSIGNED (type1)))
8461             (popcount (convert:type0 (convert:type1 @2)))))))))
8463 /* popcount(rotate(X Y)) is popcount(X).  */
8464 (for popcount (POPCOUNT)
8465   (for rot (lrotate rrotate)
8466     (simplify
8467       (popcount (convert?@0 (rot:s@1 @2 @3)))
8468       (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
8469            && INTEGRAL_TYPE_P (TREE_TYPE (@1))  
8470            && (GIMPLE || !TREE_SIDE_EFFECTS (@3)))
8471         (with { tree type0 = TREE_TYPE (@0);
8472                 tree type1 = TREE_TYPE (@1);
8473                 unsigned int prec0 = TYPE_PRECISION (type0);
8474                 unsigned int prec1 = TYPE_PRECISION (type1); }
8475           (if (prec0 == prec1 || (prec0 > prec1 && TYPE_UNSIGNED (type1)))
8476             (popcount (convert:type0 @2))))))))
8478 /* Canonicalize POPCOUNT(x)&1 as PARITY(X).  */
8479 (simplify
8480   (bit_and (POPCOUNT @0) integer_onep)
8481   (PARITY @0))
8483 /* popcount(X&Y) + popcount(X|Y) is popcount(x) + popcount(Y).  */
8484 (simplify
8485   (plus:c (POPCOUNT:s (bit_and:s @0 @1)) (POPCOUNT:s (bit_ior:cs @0 @1)))
8486   (plus (POPCOUNT @0) (POPCOUNT @1)))
8488 /* popcount(X) + popcount(Y) - popcount(X&Y) is popcount(X|Y).  */
8489 /* popcount(X) + popcount(Y) - popcount(X|Y) is popcount(X&Y).  */
8490 (for popcount (POPCOUNT)
8491   (for log1 (bit_and bit_ior)
8492        log2 (bit_ior bit_and)
8493     (simplify
8494       (minus (plus:s (popcount:s @0) (popcount:s @1))
8495              (popcount:s (log1:cs @0 @1)))
8496       (popcount (log2 @0 @1)))
8497     (simplify
8498       (plus:c (minus:s (popcount:s @0) (popcount:s (log1:cs @0 @1)))
8499               (popcount:s @1))
8500       (popcount (log2 @0 @1)))))
8502 /* PARITY simplifications.  */
8503 /* parity(~X) is parity(X).  */
8504 (simplify
8505   (PARITY (bit_not @0))
8506   (PARITY @0))
8508 /* parity(bswap(x)) is parity(x).  */
8509 (for parity (PARITY)
8510   (for bswap (BUILT_IN_BSWAP16 BUILT_IN_BSWAP32
8511               BUILT_IN_BSWAP64 BUILT_IN_BSWAP128)
8512     (simplify
8513       (parity (convert?@0 (bswap:s@1 @2)))
8514       (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
8515            && INTEGRAL_TYPE_P (TREE_TYPE (@1))
8516            && TYPE_PRECISION (TREE_TYPE (@0))
8517               >= TYPE_PRECISION (TREE_TYPE (@1)))
8518         (with { tree type0 = TREE_TYPE (@0);
8519                 tree type1 = TREE_TYPE (@1); }
8520           (parity (convert:type0 (convert:type1 @2))))))))
8522 /* parity(rotate(X Y)) is parity(X).  */
8523 (for parity (PARITY)
8524   (for rot (lrotate rrotate)
8525     (simplify
8526       (parity (convert?@0 (rot:s@1 @2 @3)))
8527       (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
8528            && INTEGRAL_TYPE_P (TREE_TYPE (@1))
8529            && (GIMPLE || !TREE_SIDE_EFFECTS (@3))
8530            && TYPE_PRECISION (TREE_TYPE (@0))
8531               >= TYPE_PRECISION (TREE_TYPE (@1)))
8532         (with { tree type0 = TREE_TYPE (@0); }
8533           (parity (convert:type0 @2)))))))
8535 /* parity(X)^parity(Y) is parity(X^Y).  */
8536 (simplify
8537   (bit_xor (PARITY:s @0) (PARITY:s @1))
8538   (PARITY (bit_xor @0 @1)))
8540 /* a != 0 ? FUN(a) : 0 -> Fun(a) for some builtin functions. */
8541 (for func (POPCOUNT BSWAP FFS PARITY)
8542  (simplify
8543   (cond (ne @0 integer_zerop@1) (func@3 (convert? @0)) integer_zerop@2)
8544   @3))
8546 /* a != 0 ? FUN(a) : CST -> Fun(a) for some CLRSB builtins
8547    where CST is precision-1. */
8548 (for func (CLRSB)
8549  (simplify
8550   (cond (ne @0 integer_zerop@1) (func@4 (convert?@3 @0)) INTEGER_CST@2)
8551   (if (wi::to_widest (@2) == TYPE_PRECISION (TREE_TYPE (@3)) - 1)
8552    @4)))
8554 #if GIMPLE
8555 /* a != 0 ? CLZ(a) : CST -> .CLZ(a) where CST is the result of the internal function for 0. */
8556 (for func (CLZ)
8557  (simplify
8558   (cond (ne @0 integer_zerop@1) (func (convert?@3 @0)) INTEGER_CST@2)
8559   (with { int val;
8560           internal_fn ifn = IFN_LAST;
8561           if (direct_internal_fn_supported_p (IFN_CLZ, type, OPTIMIZE_FOR_BOTH)
8562               && CLZ_DEFINED_VALUE_AT_ZERO (SCALAR_INT_TYPE_MODE (type),
8563                                             val) == 2)
8564             ifn = IFN_CLZ;
8565         }
8566    (if (ifn == IFN_CLZ && wi::to_widest (@2) == val)
8567     (IFN_CLZ @3)))))
8569 /* a != 0 ? CTZ(a) : CST -> .CTZ(a) where CST is the result of the internal function for 0. */
8570 (for func (CTZ)
8571  (simplify
8572   (cond (ne @0 integer_zerop@1) (func (convert?@3 @0)) INTEGER_CST@2)
8573   (with { int val;
8574           internal_fn ifn = IFN_LAST;
8575           if (direct_internal_fn_supported_p (IFN_CTZ, type, OPTIMIZE_FOR_BOTH)
8576               && CTZ_DEFINED_VALUE_AT_ZERO (SCALAR_INT_TYPE_MODE (type),
8577                                             val) == 2)
8578             ifn = IFN_CTZ;
8579         }
8580    (if (ifn == IFN_CTZ && wi::to_widest (@2) == val)
8581     (IFN_CTZ @3)))))
8582 #endif
8584 /* Common POPCOUNT/PARITY simplifications.  */
8585 /* popcount(X&C1) is (X>>C2)&1 when C1 == 1<<C2.  Same for parity(X&C1).  */
8586 (for pfun (POPCOUNT PARITY)
8587   (simplify
8588     (pfun @0)
8589     (if (INTEGRAL_TYPE_P (type))
8590      (with { wide_int nz = tree_nonzero_bits (@0); }
8591        (switch
8592          (if (nz == 1)
8593            (convert @0))
8594          (if (wi::popcount (nz) == 1)
8595            (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
8596              (convert (rshift:utype (convert:utype @0)
8597                                     { build_int_cst (integer_type_node,
8598                                                      wi::ctz (nz)); })))))))))
8600 #if GIMPLE
8601 /* 64- and 32-bits branchless implementations of popcount are detected:
8603    int popcount64c (uint64_t x)
8604    {
8605      x -= (x >> 1) & 0x5555555555555555ULL;
8606      x = (x & 0x3333333333333333ULL) + ((x >> 2) & 0x3333333333333333ULL);
8607      x = (x + (x >> 4)) & 0x0f0f0f0f0f0f0f0fULL;
8608      return (x * 0x0101010101010101ULL) >> 56;
8609    }
8611    int popcount32c (uint32_t x)
8612    {
8613      x -= (x >> 1) & 0x55555555;
8614      x = (x & 0x33333333) + ((x >> 2) & 0x33333333);
8615      x = (x + (x >> 4)) & 0x0f0f0f0f;
8616      return (x * 0x01010101) >> 24;
8617    }  */
8618 (simplify
8619  (rshift
8620   (mult
8621    (bit_and
8622     (plus:c
8623      (rshift @8 INTEGER_CST@5)
8624       (plus:c@8
8625        (bit_and @6 INTEGER_CST@7)
8626         (bit_and
8627          (rshift
8628           (minus@6 @0
8629            (bit_and (rshift @0 INTEGER_CST@4) INTEGER_CST@11))
8630           INTEGER_CST@10)
8631          INTEGER_CST@9)))
8632     INTEGER_CST@3)
8633    INTEGER_CST@2)
8634   INTEGER_CST@1)
8635   /* Check constants and optab.  */
8636   (with { unsigned prec = TYPE_PRECISION (type);
8637           int shift = (64 - prec) & 63;
8638           unsigned HOST_WIDE_INT c1
8639             = HOST_WIDE_INT_UC (0x0101010101010101) >> shift;
8640           unsigned HOST_WIDE_INT c2
8641             = HOST_WIDE_INT_UC (0x0F0F0F0F0F0F0F0F) >> shift;
8642           unsigned HOST_WIDE_INT c3
8643             = HOST_WIDE_INT_UC (0x3333333333333333) >> shift;
8644           unsigned HOST_WIDE_INT c4
8645             = HOST_WIDE_INT_UC (0x5555555555555555) >> shift;
8646    }
8647    (if (prec >= 16
8648         && prec <= 64
8649         && pow2p_hwi (prec)
8650         && TYPE_UNSIGNED (type)
8651         && integer_onep (@4)
8652         && wi::to_widest (@10) == 2
8653         && wi::to_widest (@5) == 4
8654         && wi::to_widest (@1) == prec - 8
8655         && tree_to_uhwi (@2) == c1
8656         && tree_to_uhwi (@3) == c2
8657         && tree_to_uhwi (@9) == c3
8658         && tree_to_uhwi (@7) == c3
8659         && tree_to_uhwi (@11) == c4)
8660     (if (direct_internal_fn_supported_p (IFN_POPCOUNT, type,
8661                                          OPTIMIZE_FOR_BOTH))
8662      (convert (IFN_POPCOUNT:type @0))
8663      /* Try to do popcount in two halves.  PREC must be at least
8664         five bits for this to work without extension before adding.  */
8665      (with {
8666        tree half_type = NULL_TREE;
8667        opt_machine_mode m = mode_for_size ((prec + 1) / 2, MODE_INT, 1);
8668        int half_prec = 8;
8669        if (m.exists ()
8670            && m.require () != TYPE_MODE (type))
8671          {
8672            half_prec = GET_MODE_PRECISION (as_a <scalar_int_mode> (m));
8673            half_type = build_nonstandard_integer_type (half_prec, 1);
8674          }
8675        gcc_assert (half_prec > 2);
8676       }
8677       (if (half_type != NULL_TREE
8678            && direct_internal_fn_supported_p (IFN_POPCOUNT, half_type,
8679                                               OPTIMIZE_FOR_BOTH))
8680        (convert (plus
8681          (IFN_POPCOUNT:half_type (convert @0))
8682          (IFN_POPCOUNT:half_type (convert (rshift @0
8683             { build_int_cst (integer_type_node, half_prec); } )))))))))))
8685 /* __builtin_ffs needs to deal on many targets with the possible zero
8686    argument.  If we know the argument is always non-zero, __builtin_ctz + 1
8687    should lead to better code.  */
8688 (simplify
8689  (FFS tree_expr_nonzero_p@0)
8690  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
8691       && direct_internal_fn_supported_p (IFN_CTZ, TREE_TYPE (@0),
8692                                          OPTIMIZE_FOR_SPEED))
8693   (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
8694    (plus (CTZ:type (convert:utype @0)) { build_one_cst (type); }))))
8695 #endif
8697 (for ffs (BUILT_IN_FFS BUILT_IN_FFSL BUILT_IN_FFSLL
8698           BUILT_IN_FFSIMAX)
8699  /* __builtin_ffs (X) == 0 -> X == 0.
8700     __builtin_ffs (X) == 6 -> (X & 63) == 32.  */
8701  (for cmp (eq ne)
8702   (simplify
8703    (cmp (ffs@2 @0) INTEGER_CST@1)
8704     (with { int prec = TYPE_PRECISION (TREE_TYPE (@0)); }
8705      (switch
8706       (if (integer_zerop (@1))
8707        (cmp @0 { build_zero_cst (TREE_TYPE (@0)); }))
8708       (if (tree_int_cst_sgn (@1) < 0 || wi::to_widest (@1) > prec)
8709        { constant_boolean_node (cmp == NE_EXPR ? true : false, type); })
8710       (if (single_use (@2))
8711        (cmp (bit_and @0 { wide_int_to_tree (TREE_TYPE (@0),
8712                                             wi::mask (tree_to_uhwi (@1),
8713                                                       false, prec)); })
8714             { wide_int_to_tree (TREE_TYPE (@0),
8715                                 wi::shifted_mask (tree_to_uhwi (@1) - 1, 1,
8716                                                   false, prec)); }))))))
8718  /* __builtin_ffs (X) > 6 -> X != 0 && (X & 63) == 0.  */
8719  (for cmp (gt le)
8720       cmp2 (ne eq)
8721       cmp3 (eq ne)
8722       bit_op (bit_and bit_ior)
8723   (simplify
8724    (cmp (ffs@2 @0) INTEGER_CST@1)
8725     (with { int prec = TYPE_PRECISION (TREE_TYPE (@0)); }
8726      (switch
8727       (if (integer_zerop (@1))
8728        (cmp2 @0 { build_zero_cst (TREE_TYPE (@0)); }))
8729       (if (tree_int_cst_sgn (@1) < 0)
8730        { constant_boolean_node (cmp == GT_EXPR ? true : false, type); })
8731       (if (wi::to_widest (@1) >= prec)
8732        { constant_boolean_node (cmp == GT_EXPR ? false : true, type); })
8733       (if (wi::to_widest (@1) == prec - 1)
8734        (cmp3 @0 { wide_int_to_tree (TREE_TYPE (@0),
8735                                     wi::shifted_mask (prec - 1, 1,
8736                                                       false, prec)); }))
8737       (if (single_use (@2))
8738        (bit_op (cmp2 @0 { build_zero_cst (TREE_TYPE (@0)); })
8739                (cmp3 (bit_and @0
8740                               { wide_int_to_tree (TREE_TYPE (@0),
8741                                                   wi::mask (tree_to_uhwi (@1),
8742                                                   false, prec)); })
8743                      { build_zero_cst (TREE_TYPE (@0)); }))))))))
8745 #if GIMPLE
8747 /* Simplify:
8748      a = op a1
8749      r = cond ? a : b
8750      --> r = .COND_FN (cond, a, b)
8751 and,
8752     a = op a1
8753     r = cond ? b : a
8754     --> r = .COND_FN (~cond, b, a).  */
8756 (for uncond_op (UNCOND_UNARY)
8757      cond_op (COND_UNARY)
8758  (simplify
8759   (vec_cond @0 (view_convert? (uncond_op@3 @1)) @2)
8760    (with { tree op_type = TREE_TYPE (@3); }
8761     (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
8762         && is_truth_type_for (op_type, TREE_TYPE (@0)))
8763      (cond_op @0 @1 @2))))
8764  (simplify
8765   (vec_cond @0 @1 (view_convert? (uncond_op@3 @2)))
8766    (with { tree op_type = TREE_TYPE (@3); }
8767     (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
8768         && is_truth_type_for (op_type, TREE_TYPE (@0)))
8769      (cond_op (bit_not @0) @2 @1)))))
8771 /* `(a ? -1 : 0) ^ b` can be converted into a conditional not.  */
8772 (simplify
8773  (bit_xor:c (vec_cond @0 uniform_integer_cst_p@1 uniform_integer_cst_p@2) @3)
8774  (if (canonicalize_math_after_vectorization_p ()
8775       && vectorized_internal_fn_supported_p (IFN_COND_NOT, type)
8776       && is_truth_type_for (type, TREE_TYPE (@0)))
8777  (if (integer_all_onesp (@1) && integer_zerop (@2))
8778   (IFN_COND_NOT @0 @3 @3))
8779   (if (integer_all_onesp (@2) && integer_zerop (@1))
8780    (IFN_COND_NOT (bit_not @0) @3 @3))))
8782 /* Simplify:
8784      a = a1 op a2
8785      r = c ? a : b;
8787    to:
8789      r = c ? a1 op a2 : b;
8791    if the target can do it in one go.  This makes the operation conditional
8792    on c, so could drop potentially-trapping arithmetic, but that's a valid
8793    simplification if the result of the operation isn't needed.
8795    Avoid speculatively generating a stand-alone vector comparison
8796    on targets that might not support them.  Any target implementing
8797    conditional internal functions must support the same comparisons
8798    inside and outside a VEC_COND_EXPR.  */
8800 (for uncond_op (UNCOND_BINARY)
8801      cond_op (COND_BINARY)
8802  (simplify
8803   (vec_cond @0 (view_convert? (uncond_op@4 @1 @2)) @3)
8804   (with { tree op_type = TREE_TYPE (@4); }
8805    (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
8806         && is_truth_type_for (op_type, TREE_TYPE (@0))
8807         && single_use (@4))
8808     (view_convert (cond_op @0 @1 @2 (view_convert:op_type @3))))))
8809  (simplify
8810   (vec_cond @0 @1 (view_convert? (uncond_op@4 @2 @3)))
8811   (with { tree op_type = TREE_TYPE (@4); }
8812    (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
8813         && is_truth_type_for (op_type, TREE_TYPE (@0))
8814         && single_use (@4))
8815     (view_convert (cond_op (bit_not @0) @2 @3 (view_convert:op_type @1)))))))
8817 /* Same for ternary operations.  */
8818 (for uncond_op (UNCOND_TERNARY)
8819      cond_op (COND_TERNARY)
8820  (simplify
8821   (vec_cond @0 (view_convert? (uncond_op@5 @1 @2 @3)) @4)
8822   (with { tree op_type = TREE_TYPE (@5); }
8823    (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
8824         && is_truth_type_for (op_type, TREE_TYPE (@0))
8825         && single_use (@5))
8826     (view_convert (cond_op @0 @1 @2 @3 (view_convert:op_type @4))))))
8827  (simplify
8828   (vec_cond @0 @1 (view_convert? (uncond_op@5 @2 @3 @4)))
8829   (with { tree op_type = TREE_TYPE (@5); }
8830    (if (vectorized_internal_fn_supported_p (as_internal_fn (cond_op), op_type)
8831         && is_truth_type_for (op_type, TREE_TYPE (@0))
8832         && single_use (@5))
8833     (view_convert (cond_op (bit_not @0) @2 @3 @4
8834                   (view_convert:op_type @1)))))))
8835 #endif
8837 /* Detect cases in which a VEC_COND_EXPR effectively replaces the
8838    "else" value of an IFN_COND_*.  */
8839 (for cond_op (COND_BINARY)
8840  (simplify
8841   (vec_cond @0 (view_convert? (cond_op @0 @1 @2 @3)) @4)
8842   (with { tree op_type = TREE_TYPE (@3); }
8843    (if (element_precision (type) == element_precision (op_type))
8844     (view_convert (cond_op @0 @1 @2 (view_convert:op_type @4))))))
8845  (simplify
8846   (vec_cond @0 @1 (view_convert? (cond_op @2 @3 @4 @5)))
8847   (with { tree op_type = TREE_TYPE (@5); }
8848    (if (inverse_conditions_p (@0, @2)
8849         && element_precision (type) == element_precision (op_type))
8850     (view_convert (cond_op @2 @3 @4 (view_convert:op_type @1)))))))
8852 /* Same for ternary operations.  */
8853 (for cond_op (COND_TERNARY)
8854  (simplify
8855   (vec_cond @0 (view_convert? (cond_op @0 @1 @2 @3 @4)) @5)
8856   (with { tree op_type = TREE_TYPE (@4); }
8857    (if (element_precision (type) == element_precision (op_type))
8858     (view_convert (cond_op @0 @1 @2 @3 (view_convert:op_type @5))))))
8859  (simplify
8860   (vec_cond @0 @1 (view_convert? (cond_op @2 @3 @4 @5 @6)))
8861   (with { tree op_type = TREE_TYPE (@6); }
8862    (if (inverse_conditions_p (@0, @2)
8863         && element_precision (type) == element_precision (op_type))
8864     (view_convert (cond_op @2 @3 @4 @5 (view_convert:op_type @1)))))))
8866 /* Detect simplication for a conditional reduction where
8868    a = mask1 ? b : 0
8869    c = mask2 ? d + a : d
8871    is turned into
8873    c = mask1 && mask2 ? d + b : d.  */
8874 (simplify
8875   (IFN_COND_ADD @0 @1 (vec_cond @2 @3 integer_zerop) @1)
8876    (IFN_COND_ADD (bit_and @0 @2) @1 @3 @1))
8878 /* For pointers @0 and @2 and nonnegative constant offset @1, look for
8879    expressions like:
8881    A: (@0 + @1 < @2) | (@2 + @1 < @0)
8882    B: (@0 + @1 <= @2) | (@2 + @1 <= @0)
8884    If pointers are known not to wrap, B checks whether @1 bytes starting
8885    at @0 and @2 do not overlap, while A tests the same thing for @1 + 1
8886    bytes.  A is more efficiently tested as:
8888    A: (sizetype) (@0 + @1 - @2) > @1 * 2
8890    The equivalent expression for B is given by replacing @1 with @1 - 1:
8892    B: (sizetype) (@0 + (@1 - 1) - @2) > (@1 - 1) * 2
8894    @0 and @2 can be swapped in both expressions without changing the result.
8896    The folds rely on sizetype's being unsigned (which is always true)
8897    and on its being the same width as the pointer (which we have to check).
8899    The fold replaces two pointer_plus expressions, two comparisons and
8900    an IOR with a pointer_plus, a pointer_diff, and a comparison, so in
8901    the best case it's a saving of two operations.  The A fold retains one
8902    of the original pointer_pluses, so is a win even if both pointer_pluses
8903    are used elsewhere.  The B fold is a wash if both pointer_pluses are
8904    used elsewhere, since all we end up doing is replacing a comparison with
8905    a pointer_plus.  We do still apply the fold under those circumstances
8906    though, in case applying it to other conditions eventually makes one of the
8907    pointer_pluses dead.  */
8908 (for ior (truth_orif truth_or bit_ior)
8909  (for cmp (le lt)
8910   (simplify
8911    (ior (cmp:cs (pointer_plus@3 @0 INTEGER_CST@1) @2)
8912         (cmp:cs (pointer_plus@4 @2 @1) @0))
8913    (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
8914         && TYPE_OVERFLOW_WRAPS (sizetype)
8915         && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (sizetype))
8916     /* Calculate the rhs constant.  */
8917     (with { offset_int off = wi::to_offset (@1) - (cmp == LE_EXPR ? 1 : 0);
8918             offset_int rhs = off * 2; }
8919      /* Always fails for negative values.  */
8920      (if (wi::min_precision (rhs, UNSIGNED) <= TYPE_PRECISION (sizetype))
8921       /* Since the order of @0 and @2 doesn't matter, let tree_swap_operands_p
8922          pick a canonical order.  This increases the chances of using the
8923          same pointer_plus in multiple checks.  */
8924       (with { bool swap_p = tree_swap_operands_p (@0, @2);
8925               tree rhs_tree = wide_int_to_tree (sizetype, rhs); }
8926        (if (cmp == LT_EXPR)
8927         (gt (convert:sizetype
8928              (pointer_diff:ssizetype { swap_p ? @4 : @3; }
8929                                      { swap_p ? @0 : @2; }))
8930             { rhs_tree; })
8931         (gt (convert:sizetype
8932              (pointer_diff:ssizetype
8933               (pointer_plus { swap_p ? @2 : @0; }
8934                             { wide_int_to_tree (sizetype, off); })
8935               { swap_p ? @0 : @2; }))
8936             { rhs_tree; })))))))))
8938 /* Fold REDUC (@0 & @1) -> @0[I] & @1[I] if element I is the only nonzero
8939    element of @1.  */
8940 (for reduc (IFN_REDUC_PLUS IFN_REDUC_IOR IFN_REDUC_XOR)
8941  (simplify (reduc (view_convert? (bit_and @0 VECTOR_CST@1)))
8942   (with { int i = single_nonzero_element (@1); }
8943    (if (i >= 0)
8944     (with { tree elt = vector_cst_elt (@1, i);
8945             tree elt_type = TREE_TYPE (elt);
8946             unsigned int elt_bits = tree_to_uhwi (TYPE_SIZE (elt_type));
8947             tree size = bitsize_int (elt_bits);
8948             tree pos = bitsize_int (elt_bits * i); }
8949      (view_convert
8950       (bit_and:elt_type
8951        (BIT_FIELD_REF:elt_type @0 { size; } { pos; })
8952        { elt; })))))))
8954 /* Fold reduction of a single nonzero element constructor.  */
8955 (for reduc (IFN_REDUC_PLUS IFN_REDUC_IOR IFN_REDUC_XOR)
8956   (simplify (reduc (CONSTRUCTOR@0))
8957     (with { tree ctor = (TREE_CODE (@0) == SSA_NAME
8958                          ? gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0)) : @0);
8959             tree elt = ctor_single_nonzero_element (ctor); }
8960       (if (elt
8961            && !HONOR_SNANS (type)
8962            && !HONOR_SIGNED_ZEROS (type))
8963         { elt; }))))
8965 /* Fold REDUC (@0 op VECTOR_CST) as REDUC (@0) op REDUC (VECTOR_CST).  */
8966 (for reduc (IFN_REDUC_PLUS IFN_REDUC_MAX IFN_REDUC_MIN IFN_REDUC_FMAX
8967             IFN_REDUC_FMIN IFN_REDUC_AND IFN_REDUC_IOR IFN_REDUC_XOR)
8968      op (plus max min IFN_FMAX IFN_FMIN bit_and bit_ior bit_xor)
8969   (simplify (reduc (op @0 VECTOR_CST@1))
8970     (op (reduc:type @0) (reduc:type @1))))
8972 /* Simplify vector floating point operations of alternating sub/add pairs
8973    into using an fneg of a wider element type followed by a normal add.
8974    under IEEE 754 the fneg of the wider type will negate every even entry
8975    and when doing an add we get a sub of the even and add of every odd
8976    elements.  */
8977 (for plusminus (plus minus)
8978      minusplus (minus plus)
8979  (simplify
8980   (vec_perm (plusminus @0 @1) (minusplus @2 @3) VECTOR_CST@4)
8981    (if (!VECTOR_INTEGER_TYPE_P (type)
8982         && !FLOAT_WORDS_BIG_ENDIAN
8983         /* plus is commutative, while minus is not, so :c can't be used.
8984            Do equality comparisons by hand and at the end pick the operands
8985            from the minus.  */
8986         && (operand_equal_p (@0, @2, 0)
8987             ? operand_equal_p (@1, @3, 0)
8988             : operand_equal_p (@0, @3, 0) && operand_equal_p (@1, @2, 0)))
8989    (with
8990     {
8991       /* Build a vector of integers from the tree mask.  */
8992       vec_perm_builder builder;
8993     }
8994     (if (tree_to_vec_perm_builder (&builder, @4))
8995      (with
8996       {
8997         /* Create a vec_perm_indices for the integer vector.  */
8998         poly_uint64 nelts = TYPE_VECTOR_SUBPARTS (type);
8999         vec_perm_indices sel (builder, 2, nelts);
9000         machine_mode vec_mode = TYPE_MODE (type);
9001         machine_mode wide_mode;
9002         scalar_mode wide_elt_mode;
9003         poly_uint64 wide_nunits;
9004         scalar_mode inner_mode = GET_MODE_INNER (vec_mode);
9005       }
9006       (if (VECTOR_MODE_P (vec_mode)
9007            && sel.series_p (0, 2, 0, 2)
9008            && sel.series_p (1, 2, nelts + 1, 2)
9009            && GET_MODE_2XWIDER_MODE (inner_mode).exists (&wide_elt_mode)
9010            && multiple_p (GET_MODE_NUNITS (vec_mode), 2, &wide_nunits)
9011            && related_vector_mode (vec_mode, wide_elt_mode,
9012                                    wide_nunits).exists (&wide_mode))
9013        (with
9014         {
9015           tree stype
9016             = lang_hooks.types.type_for_mode (GET_MODE_INNER (wide_mode),
9017                                               TYPE_UNSIGNED (type));
9018           tree ntype = build_vector_type_for_mode (stype, wide_mode);
9020           /* The format has to be a non-extended ieee format.  */
9021           const struct real_format *fmt_old = FLOAT_MODE_FORMAT (vec_mode);
9022           const struct real_format *fmt_new = FLOAT_MODE_FORMAT (wide_mode);
9023         }
9024         (if (TYPE_MODE (stype) != BLKmode
9025              && VECTOR_TYPE_P (ntype)
9026              && fmt_old != NULL
9027              && fmt_new != NULL)
9028          (with
9029           {
9030             /* If the target doesn't support v1xx vectors, try using
9031                scalar mode xx instead.  */
9032             if (known_eq (GET_MODE_NUNITS (wide_mode), 1)
9033                 && !target_supports_op_p (ntype, NEGATE_EXPR, optab_vector))
9034               ntype = stype;
9035           }
9036           (if (fmt_new->signbit_rw
9037                == fmt_old->signbit_rw + GET_MODE_UNIT_BITSIZE (vec_mode)
9038                && fmt_new->signbit_rw == fmt_new->signbit_ro
9039                && targetm.can_change_mode_class (TYPE_MODE (ntype),
9040                                                  TYPE_MODE (type), ALL_REGS)
9041                && ((optimize_vectors_before_lowering_p ()
9042                     && VECTOR_TYPE_P (ntype))
9043                    || target_supports_op_p (ntype, NEGATE_EXPR, optab_vector)))
9044            (if (plusminus == PLUS_EXPR)
9045             (plus (view_convert:type (negate (view_convert:ntype @3))) @2)
9046             (minus @0 (view_convert:type
9047                         (negate (view_convert:ntype @1))))))))))))))))
9049 (simplify
9050  (vec_perm @0 @1 VECTOR_CST@2)
9051  (with
9052   {
9053     tree op0 = @0, op1 = @1, op2 = @2;
9054     machine_mode result_mode = TYPE_MODE (type);
9055     machine_mode op_mode = TYPE_MODE (TREE_TYPE (op0));
9057     /* Build a vector of integers from the tree mask.  */
9058     vec_perm_builder builder;
9059   }
9060   (if (tree_to_vec_perm_builder (&builder, op2))
9061    (with
9062     {
9063       /* Create a vec_perm_indices for the integer vector.  */
9064       poly_uint64 nelts = TYPE_VECTOR_SUBPARTS (type);
9065       bool single_arg = (op0 == op1);
9066       vec_perm_indices sel (builder, single_arg ? 1 : 2, nelts);
9067     }
9068     (if (sel.series_p (0, 1, 0, 1))
9069      { op0; }
9070      (if (sel.series_p (0, 1, nelts, 1))
9071       { op1; }
9072       (with
9073        {
9074          if (!single_arg)
9075            {
9076              if (sel.all_from_input_p (0))
9077                op1 = op0;
9078              else if (sel.all_from_input_p (1))
9079                {
9080                  op0 = op1;
9081                  sel.rotate_inputs (1);
9082                }
9083              else if (known_ge (poly_uint64 (sel[0]), nelts))
9084                {
9085                  std::swap (op0, op1);
9086                  sel.rotate_inputs (1);
9087                }
9088            }
9089          gassign *def;
9090          tree cop0 = op0, cop1 = op1;
9091          if (TREE_CODE (op0) == SSA_NAME
9092              && (def = dyn_cast <gassign *> (SSA_NAME_DEF_STMT (op0)))
9093              && gimple_assign_rhs_code (def) == CONSTRUCTOR)
9094            cop0 = gimple_assign_rhs1 (def);
9095          if (TREE_CODE (op1) == SSA_NAME
9096              && (def = dyn_cast <gassign *> (SSA_NAME_DEF_STMT (op1)))
9097              && gimple_assign_rhs_code (def) == CONSTRUCTOR)
9098            cop1 = gimple_assign_rhs1 (def);
9099          tree t;
9100        }
9101        (if ((TREE_CODE (cop0) == VECTOR_CST
9102              || TREE_CODE (cop0) == CONSTRUCTOR)
9103             && (TREE_CODE (cop1) == VECTOR_CST
9104                 || TREE_CODE (cop1) == CONSTRUCTOR)
9105             && (t = fold_vec_perm (type, cop0, cop1, sel)))
9106         { t; }
9107         (with
9108          {
9109            bool changed = (op0 == op1 && !single_arg);
9110            tree ins = NULL_TREE;
9111            unsigned at = 0;
9113            /* See if the permutation is performing a single element
9114               insert from a CONSTRUCTOR or constant and use a BIT_INSERT_EXPR
9115               in that case.  But only if the vector mode is supported,
9116               otherwise this is invalid GIMPLE.  */
9117            if (op_mode != BLKmode
9118                && (TREE_CODE (cop0) == VECTOR_CST
9119                    || TREE_CODE (cop0) == CONSTRUCTOR
9120                    || TREE_CODE (cop1) == VECTOR_CST
9121                    || TREE_CODE (cop1) == CONSTRUCTOR))
9122              {
9123                bool insert_first_p = sel.series_p (1, 1, nelts + 1, 1);
9124                if (insert_first_p)
9125                  {
9126                    /* After canonicalizing the first elt to come from the
9127                       first vector we only can insert the first elt from
9128                       the first vector.  */
9129                    at = 0;
9130                    if ((ins = fold_read_from_vector (cop0, sel[0])))
9131                      op0 = op1;
9132                  }
9133                /* The above can fail for two-element vectors which always
9134                   appear to insert the first element, so try inserting
9135                   into the second lane as well.  For more than two
9136                   elements that's wasted time.  */
9137                if (!insert_first_p || (!ins && maybe_eq (nelts, 2u)))
9138                  {
9139                    unsigned int encoded_nelts = sel.encoding ().encoded_nelts ();
9140                    for (at = 0; at < encoded_nelts; ++at)
9141                      if (maybe_ne (sel[at], at))
9142                        break;
9143                    if (at < encoded_nelts
9144                        && (known_eq (at + 1, nelts)
9145                            || sel.series_p (at + 1, 1, at + 1, 1)))
9146                      {
9147                        if (known_lt (poly_uint64 (sel[at]), nelts))
9148                          ins = fold_read_from_vector (cop0, sel[at]);
9149                        else
9150                          ins = fold_read_from_vector (cop1, sel[at] - nelts);
9151                      }
9152                  }
9153              }
9155            /* Generate a canonical form of the selector.  */
9156            if (!ins && sel.encoding () != builder)
9157              {
9158                /* Some targets are deficient and fail to expand a single
9159                   argument permutation while still allowing an equivalent
9160                   2-argument version.  */
9161                tree oldop2 = op2;
9162                if (sel.ninputs () == 2
9163                    || can_vec_perm_const_p (result_mode, op_mode, sel, false))
9164                  op2 = vec_perm_indices_to_tree (TREE_TYPE (op2), sel);
9165                else
9166                  {
9167                    vec_perm_indices sel2 (builder, 2, nelts);
9168                    if (can_vec_perm_const_p (result_mode, op_mode, sel2, false))
9169                      op2 = vec_perm_indices_to_tree (TREE_TYPE (op2), sel2);
9170                    else
9171                      /* Not directly supported with either encoding,
9172                         so use the preferred form.  */
9173                      op2 = vec_perm_indices_to_tree (TREE_TYPE (op2), sel);
9174                  }
9175                if (!operand_equal_p (op2, oldop2, 0))
9176                  changed = true;
9177              }
9178          }
9179          (if (ins)
9180           (bit_insert { op0; } { ins; }
9181            { bitsize_int (at * vector_element_bits (type)); })
9182           (if (changed)
9183            (vec_perm { op0; } { op1; } { op2; }))))))))))))
9185 /* VEC_PERM_EXPR (v, v, mask) -> v where v contains same element.  */
9187 (match vec_same_elem_p
9188  (vec_duplicate @0))
9190 (match vec_same_elem_p
9191  CONSTRUCTOR@0
9192  (if (TREE_CODE (@0) == SSA_NAME
9193       && uniform_vector_p (gimple_assign_rhs1 (SSA_NAME_DEF_STMT (@0))))))
9195 (match vec_same_elem_p
9196  @0
9197  (if (uniform_vector_p (@0))))
9200 (simplify
9201  (vec_perm vec_same_elem_p@0 @0 @1)
9202  (if (types_match (type, TREE_TYPE (@0)))
9203   @0
9204   (with
9205    {
9206      tree elem = uniform_vector_p (@0);
9207    }
9208    (if (elem)
9209     { build_vector_from_val (type, elem); }))))
9211 /* Push VEC_PERM earlier if that may help FMA perception (PR101895).  */
9212 (simplify
9213  (plus:c (vec_perm:s (mult:c@0 @1 vec_same_elem_p@2) @0 @3) @4)
9214  (if (TREE_CODE (@0) == SSA_NAME && num_imm_uses (@0) == 2)
9215   (plus (mult (vec_perm @1 @1 @3) @2) @4)))
9216 (simplify
9217  (minus (vec_perm:s (mult:c@0 @1 vec_same_elem_p@2) @0 @3) @4)
9218  (if (TREE_CODE (@0) == SSA_NAME && num_imm_uses (@0) == 2)
9219   (minus (mult (vec_perm @1 @1 @3) @2) @4)))
9222 /* Merge
9223      c = VEC_PERM_EXPR <a, b, VCST0>;
9224      d = VEC_PERM_EXPR <c, c, VCST1>;
9225    to
9226      d = VEC_PERM_EXPR <a, b, NEW_VCST>;  */
9228 (simplify
9229  (vec_perm (vec_perm@0 @1 @2 VECTOR_CST@3) @0 VECTOR_CST@4)
9230  (if (TYPE_VECTOR_SUBPARTS (type).is_constant ())
9231   (with
9232    {
9233      machine_mode result_mode = TYPE_MODE (type);
9234      machine_mode op_mode = TYPE_MODE (TREE_TYPE (@1));
9235      int nelts = TYPE_VECTOR_SUBPARTS (type).to_constant ();
9236      vec_perm_builder builder0;
9237      vec_perm_builder builder1;
9238      vec_perm_builder builder2 (nelts, nelts, 1);
9239    }
9240    (if (tree_to_vec_perm_builder (&builder0, @3)
9241         && tree_to_vec_perm_builder (&builder1, @4))
9242     (with
9243      {
9244        vec_perm_indices sel0 (builder0, 2, nelts);
9245        vec_perm_indices sel1 (builder1, 1, nelts);
9247        for (int i = 0; i < nelts; i++)
9248          builder2.quick_push (sel0[sel1[i].to_constant ()]);
9250        vec_perm_indices sel2 (builder2, 2, nelts);
9252        tree op0 = NULL_TREE;
9253        /* If the new VEC_PERM_EXPR can't be handled but both
9254           original VEC_PERM_EXPRs can, punt.
9255           If one or both of the original VEC_PERM_EXPRs can't be
9256           handled and the new one can't be either, don't increase
9257           number of VEC_PERM_EXPRs that can't be handled.  */
9258        if (can_vec_perm_const_p (result_mode, op_mode, sel2, false)
9259            || (single_use (@0)
9260                ? (!can_vec_perm_const_p (result_mode, op_mode, sel0, false)
9261                   || !can_vec_perm_const_p (result_mode, op_mode, sel1, false))
9262                : !can_vec_perm_const_p (result_mode, op_mode, sel1, false)))
9263          op0 = vec_perm_indices_to_tree (TREE_TYPE (@4), sel2);
9264      }
9265      (if (op0)
9266       (vec_perm @1 @2 { op0; })))))))
9268 /* Merge
9269      c = VEC_PERM_EXPR <a, b, VCST0>;
9270      d = VEC_PERM_EXPR <x, c, VCST1>;
9271    to
9272      d = VEC_PERM_EXPR <x, {a,b}, NEW_VCST>;
9273    when all elements from a or b are replaced by the later
9274    permutation.  */
9276 (simplify
9277  (vec_perm @5 (vec_perm@0 @1 @2 VECTOR_CST@3) VECTOR_CST@4)
9278  (if (TYPE_VECTOR_SUBPARTS (type).is_constant ())
9279   (with
9280    {
9281      machine_mode result_mode = TYPE_MODE (type);
9282      machine_mode op_mode = TYPE_MODE (TREE_TYPE (@1));
9283      int nelts = TYPE_VECTOR_SUBPARTS (type).to_constant ();
9284      vec_perm_builder builder0;
9285      vec_perm_builder builder1;
9286      vec_perm_builder builder2 (nelts, nelts, 2);
9287    }
9288    (if (tree_to_vec_perm_builder (&builder0, @3)
9289         && tree_to_vec_perm_builder (&builder1, @4))
9290     (with
9291      {
9292        vec_perm_indices sel0 (builder0, 2, nelts);
9293        vec_perm_indices sel1 (builder1, 2, nelts);
9294        bool use_1 = false, use_2 = false;
9296        for (int i = 0; i < nelts; i++)
9297          {
9298            if (known_lt ((poly_uint64)sel1[i], sel1.nelts_per_input ()))
9299              builder2.quick_push (sel1[i]);
9300            else
9301              {
9302                poly_uint64 j = sel0[(sel1[i] - sel1.nelts_per_input ())
9303                                     .to_constant ()];
9304                if (known_lt (j, sel0.nelts_per_input ()))
9305                  use_1 = true;
9306                else
9307                  {
9308                    use_2 = true;
9309                    j -= sel0.nelts_per_input ();
9310                  }
9311                builder2.quick_push (j + sel1.nelts_per_input ());
9312              }
9313          }
9314      }
9315      (if (use_1 ^ use_2)
9316       (with
9317        {
9318          vec_perm_indices sel2 (builder2, 2, nelts);
9319          tree op0 = NULL_TREE;
9320          /* If the new VEC_PERM_EXPR can't be handled but both
9321             original VEC_PERM_EXPRs can, punt.
9322             If one or both of the original VEC_PERM_EXPRs can't be
9323             handled and the new one can't be either, don't increase
9324             number of VEC_PERM_EXPRs that can't be handled.  */
9325          if (can_vec_perm_const_p (result_mode, op_mode, sel2, false)
9326              || (single_use (@0)
9327                  ? (!can_vec_perm_const_p (result_mode, op_mode, sel0, false)
9328                     || !can_vec_perm_const_p (result_mode, op_mode, sel1, false))
9329                  : !can_vec_perm_const_p (result_mode, op_mode, sel1, false)))
9330            op0 = vec_perm_indices_to_tree (TREE_TYPE (@4), sel2);
9331        }
9332        (if (op0)
9333         (switch
9334          (if (use_1)
9335           (vec_perm @5 @1 { op0; }))
9336          (if (use_2)
9337           (vec_perm @5 @2 { op0; })))))))))))
9339 /* And the case with swapped outer permute sources.  */
9341 (simplify
9342  (vec_perm (vec_perm@0 @1 @2 VECTOR_CST@3) @5 VECTOR_CST@4)
9343  (if (TYPE_VECTOR_SUBPARTS (type).is_constant ())
9344   (with
9345    {
9346      machine_mode result_mode = TYPE_MODE (type);
9347      machine_mode op_mode = TYPE_MODE (TREE_TYPE (@1));
9348      int nelts = TYPE_VECTOR_SUBPARTS (type).to_constant ();
9349      vec_perm_builder builder0;
9350      vec_perm_builder builder1;
9351      vec_perm_builder builder2 (nelts, nelts, 2);
9352    }
9353    (if (tree_to_vec_perm_builder (&builder0, @3)
9354         && tree_to_vec_perm_builder (&builder1, @4))
9355     (with
9356      {
9357        vec_perm_indices sel0 (builder0, 2, nelts);
9358        vec_perm_indices sel1 (builder1, 2, nelts);
9359        bool use_1 = false, use_2 = false;
9361        for (int i = 0; i < nelts; i++)
9362          {
9363            if (known_ge ((poly_uint64)sel1[i], sel1.nelts_per_input ()))
9364              builder2.quick_push (sel1[i]);
9365            else
9366              {
9367                poly_uint64 j = sel0[sel1[i].to_constant ()];
9368                if (known_lt (j, sel0.nelts_per_input ()))
9369                  use_1 = true;
9370                else
9371                  {
9372                    use_2 = true;
9373                    j -= sel0.nelts_per_input ();
9374                  }
9375                builder2.quick_push (j);
9376              }
9377          }
9378      }
9379      (if (use_1 ^ use_2)
9380       (with
9381        {
9382          vec_perm_indices sel2 (builder2, 2, nelts);
9383          tree op0 = NULL_TREE;
9384          /* If the new VEC_PERM_EXPR can't be handled but both
9385             original VEC_PERM_EXPRs can, punt.
9386             If one or both of the original VEC_PERM_EXPRs can't be
9387             handled and the new one can't be either, don't increase
9388             number of VEC_PERM_EXPRs that can't be handled.  */
9389          if (can_vec_perm_const_p (result_mode, op_mode, sel2, false)
9390              || (single_use (@0)
9391                  ? (!can_vec_perm_const_p (result_mode, op_mode, sel0, false)
9392                     || !can_vec_perm_const_p (result_mode, op_mode, sel1, false))
9393                  : !can_vec_perm_const_p (result_mode, op_mode, sel1, false)))
9394            op0 = vec_perm_indices_to_tree (TREE_TYPE (@4), sel2);
9395        }
9396        (if (op0)
9397         (switch
9398          (if (use_1)
9399           (vec_perm @1 @5 { op0; }))
9400          (if (use_2)
9401           (vec_perm @2 @5 { op0; })))))))))))
9404 /* Match count trailing zeroes for simplify_count_trailing_zeroes in fwprop.
9405    The canonical form is array[((x & -x) * C) >> SHIFT] where C is a magic
9406    constant which when multiplied by a power of 2 contains a unique value
9407    in the top 5 or 6 bits.  This is then indexed into a table which maps it
9408    to the number of trailing zeroes.  */
9409 (match (ctz_table_index @1 @2 @3)
9410   (rshift (mult (bit_and:c (negate @1) @1) INTEGER_CST@2) INTEGER_CST@3))
9412 (match (cond_expr_convert_p @0 @2 @3 @6)
9413  (cond (simple_comparison@6 @0 @1) (convert@4 @2) (convert@5 @3))
9414   (if (INTEGRAL_TYPE_P (type)
9415        && INTEGRAL_TYPE_P (TREE_TYPE (@2))
9416        && INTEGRAL_TYPE_P (TREE_TYPE (@0))
9417        && INTEGRAL_TYPE_P (TREE_TYPE (@3))
9418        && TYPE_PRECISION (type) != TYPE_PRECISION (TREE_TYPE (@0))
9419        && TYPE_PRECISION (TREE_TYPE (@0))
9420           == TYPE_PRECISION (TREE_TYPE (@2))
9421        && TYPE_PRECISION (TREE_TYPE (@0))
9422           == TYPE_PRECISION (TREE_TYPE (@3))
9423        /* For vect_recog_cond_expr_convert_pattern, @2 and @3 can differ in
9424           signess when convert is truncation, but not ok for extension since
9425           it's sign_extend vs zero_extend.  */
9426        && (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type)
9427            || (TYPE_UNSIGNED (TREE_TYPE (@2))
9428                == TYPE_UNSIGNED (TREE_TYPE (@3))))
9429        && single_use (@4)
9430        && single_use (@5))))
9432 (for bit_op (bit_and bit_ior bit_xor)
9433  (match (bitwise_induction_p @0 @2 @3)
9434   (bit_op:c
9435    (nop_convert1? (bit_not2?@0 (convert3? (lshift integer_onep@1 @2))))
9436    @3)))
9438 (match (bitwise_induction_p @0 @2 @3)
9439  (bit_not
9440   (nop_convert1? (bit_xor@0 (convert2? (lshift integer_onep@1 @2)) @3))))
9442 /* n - (((n > C1) ? n : C1) & -C2) ->  n & C1 for unsigned case.
9443    n - (((n > C1) ? n : C1) & -C2) ->  (n <= C1) ? n : (n & C1) for signed case.  */
9444 (simplify
9445   (minus @0 (bit_and (max @0 INTEGER_CST@1) INTEGER_CST@2))
9446   (with { auto i = wi::neg (wi::to_wide (@2)); }
9447   /* Check if -C2 is a power of 2 and C1 = -C2 - 1.  */
9448     (if (wi::popcount (i) == 1
9449          && (wi::to_wide (@1)) == (i - 1))
9450       (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
9451         (bit_and @0 @1)
9452       (cond (le @0 @1) @0 (bit_and @0 @1))))))
9454 /* -x & 1 -> x & 1.  */
9455 (simplify 
9456  (bit_and (negate @0) integer_onep@1)
9457  (if (!TYPE_OVERFLOW_SANITIZED (type))
9458   (bit_and @0 @1)))
9460 /* `-a` is just `a` if the type is 1bit wide or when converting
9461    to a 1bit type; similar to the above transformation of `(-x)&1`.
9462    This is used mostly with the transformation of
9463    `a ? ~b : b` into `(-a)^b`.
9464    It also can show up with bitfields.  */
9465 (simplify
9466  (convert? (negate @0))
9467  (if (INTEGRAL_TYPE_P (type)
9468       && TYPE_PRECISION (type) == 1
9469       && !TYPE_OVERFLOW_SANITIZED (TREE_TYPE (@0)))
9470   (convert @0)))
9472 /* Optimize
9473    c1 = VEC_PERM_EXPR (a, a, mask)
9474    c2 = VEC_PERM_EXPR (b, b, mask)
9475    c3 = c1 op c2
9476    -->
9477    c = a op b
9478    c3 = VEC_PERM_EXPR (c, c, mask)
9479    For all integer non-div operations.  */
9480 (for op (plus minus mult bit_and bit_ior bit_xor
9481          lshift rshift)
9482  (simplify
9483   (op (vec_perm @0 @0 @2) (vec_perm @1 @1 @2))
9484    (if (VECTOR_INTEGER_TYPE_P (type))
9485     (vec_perm (op@3 @0 @1) @3 @2))))
9487 /* Similar for float arithmetic when permutation constant covers
9488    all vector elements.  */
9489 (for op (plus minus mult)
9490  (simplify
9491   (op (vec_perm @0 @0 VECTOR_CST@2) (vec_perm @1 @1 VECTOR_CST@2))
9492    (if (VECTOR_FLOAT_TYPE_P (type)
9493         && TYPE_VECTOR_SUBPARTS (type).is_constant ())
9494     (with
9495      {
9496        tree perm_cst = @2;
9497        vec_perm_builder builder;
9498        bool full_perm_p = false;
9499        if (tree_to_vec_perm_builder (&builder, perm_cst))
9500          {
9501            unsigned HOST_WIDE_INT nelts;
9503            nelts = TYPE_VECTOR_SUBPARTS (type).to_constant ();
9504            /* Create a vec_perm_indices for the VECTOR_CST.  */
9505            vec_perm_indices sel (builder, 1, nelts);
9507            /* Check if perm indices covers all vector elements.  */
9508            if (sel.encoding ().encoded_full_vector_p ())
9509              {
9510                auto_sbitmap seen (nelts);
9511                bitmap_clear (seen);
9513                unsigned HOST_WIDE_INT count = 0, i;
9515                for (i = 0; i < nelts; i++)
9516                  {
9517                    if (!bitmap_set_bit (seen, sel[i].to_constant ()))
9518                     break;
9519                    count++;
9520                  }
9521                full_perm_p = count == nelts;
9522              }
9523          }
9524       }
9525       (if (full_perm_p)
9526         (vec_perm (op@3 @0 @1) @3 @2))))))